How can I prioritise network bandwidth on a per application basis?

22

5

Is there a way in Linux to give a specific application more/less priority for network bandwidth? Something like how nice does for CPU priority.

Context: I'm currently on a very low bandwidth connection (3G dongle). While I'm performing a quite large upgrade using aptitude, it becomes virtually impossible to browse the web since the upgrade download is hogging my Internet connection.

So what I would like to do is somehow decrease the network bandwidth priority of the aptitude process (and all its children) so that it won't use too much bandwidth while another process is using it.

Job

Posted 2011-07-11T08:50:19.007

Reputation: 383

Does aptitude use a specific port for its connections? If so, you can set the priority of that port lower using QoS in your router (if your router has that capability). – MaQleod – 2011-07-12T00:55:20.017

@MaQleod: 1) No, it uses HTTP for its downloads. 2) Since I'm on a 3G network, I don't have a router (well, not one I can access anyway). – Job – 2011-07-12T06:39:46.577

I would just rate limit the download speed of aptitude/apt instead of trying to play with its priority. Set it to a half of your max bandwidth to leave some for browsing. I've tried to state my reason for not playing with QoS in the comment to Catalin's answer. – vtest – 2011-12-05T13:33:04.753

Answers

9

You can use force_bind to set a priority for all sockets of an application, and then, using Linux QoS (tc command), you can assign applications to a priority band. Check the README file for an example.

http://kernel.embedromix.ro/us/

Disclaimer: I am the author.

Example:

14: Force priority (between 0 and 6 for non-root users). You can
        use 'tc' command from iproute to set-up 'prio' qdisc and to
        assign prio to queues:
        # 0. setup
        export FORCE_NET_VERBOSE=1
        export LD_PRELOAD=${LD_PRELOAD}:/usr/lib/force_bind.so
        # 1. Make sure you have a 'prio' qdisc attached to eth0, for example:
        tc qdisc add ev eth0 root handle 1: prio
        # 2. Assign applications to classed (bands):
        export FORCE_NET_PRIO=6 # interactive, band 0
        your_voip_program_here
        export FORCE_NET_PRIO=0 # best effort, band 1
        your_mail_program_here
        export FORCE_NET_PRIO=2 # bulk, band 2
        your_remote_backup_program_here
        # 3. Run tc statistics so you can see the classification:
        tc -s class show dev eth0

Of course, you may use htb or any other qdisc.

Catalin M. BOIE

Posted 2011-07-11T08:50:19.007

Reputation: 126

1Regarding incoming traffic, is it possible to make certain types of incoming traffic have higher priority over other types even if you cannot control the router? @vtest – CMCDragonkai – 2014-07-21T12:18:08.580

3@taneli: trickle can be use to setup a bandwidth limit, but it cannot be used to define priorities between processes – a3nm – 2015-10-11T16:27:12.533

3Thanks for the disclaimer! In the spirit of Super User, it would be great if you could provide the example here for all to see! – slhck – 2011-08-23T08:54:20.497

1Downloading generates incoming traffic. QoS can policy outgoing traffic, it can't control the amount of incoming data. For this to work, it is needed to control the router between the host and the internet. – vtest – 2011-12-05T13:28:27.820

vtest, you are right. My example can be applied on a host with a Qos on the router. But, you can still use force_bind to limit the bandwidth. See the following example: export FORCE_NET_BW=1000 # bytes / second export LD_PRELOAD=${LD_PRELOAD}:/usr/lib/force_bind.so ./start_your_program_here

Of course, if the program is not under your control (starts from cron, for example, you may want to use a wrapper script that sets up force_bind and call the real program. – Catalin M. BOIE – 2011-12-22T14:58:41.210

2trickle (available at least in ubuntu and debian) will do that for you as well, and might be a bit easier to use: trickle -d 1 -u 1 aptitude. Numbers are kilobytes. – taneli – 2012-02-17T12:25:18.223