Throttling Your Bandwidth on the Mac

Problem

I ran into a computer problem this month. Visiting family here in Podunk Savannah, Georgia, fast internet is a pipe dream. It simply doesn’t exist. That creates more problems than you might expect at first glance.

I had some files I needed to upload. Gigs of videos to send to a friend in Portland….gigs of videos to upload to Youtube…Not only does small bandwidth make downloading and uploading an excruciatingly slow process but it reaches further. When you download or upload large files that inevitably take hours if not days to complete, it maxes out your bandwidth making simple tasks like browsing the web impossible for anyone in the house, yourself included.

Even just uploading files and maxing out your upstream, you’d think you could still browse the web or download files just fine. Wrong. You’ll experience that is NOT the case. This is because in order to make download requests, your computer must upload data to make requests for data to be received — and if your upstream is maxed out sending data to another server, it can’t make these requests.

You’d think this was a rudimentary problem that people have all the time and solutions must exist all over the internet. Half of that statement is true. It is a problem people complain about all over the internet, but there aren’t easy solutions all over the place. After searching for hours, I found some Windows apps that claimed to be able to do it. Having a Mac though, I didn’t explore these.

I’m really surprised a browser like Chrome or Firefox does not have this kind of functionality built in. It is even more surprising that add-ons for these browsers with that kind of functionality don’t exist either. Ideally, it would be great if Chrome had the functionality to limit upload or download  bandwidth by tab. With Chrome, each tab is already its own process. It seems logical from a user’s standpoint that you should be able to control a process’ bandwidth usage.

Solutions

For the Mac, I came across a few programs to throttle your internet. One program claimed it could throttle your bandwidth by port. This doesn’t help in our example situation of uploading files because most internet processes tend to use the same ports. Throttling data on your upload port would most likely throttle almost all other internet activity as well. Another program claimed to throttle your computer’s bandwidth usage wholly. This would effectively allow others in your household to use the internet… but you would still be dead in the water.

Speed Limit

I came across an app called Speed Limit which allows you to throttle bandwidth usage by host. Now we’re getting somewhere. If you’re uploading files to youtube or largedocument, you can throttle your bandwidth to those hosts and still be able to browse the rest of the internet at your normal speed. This tool is easy to install and straight forward to use, but lacking in features and customizability. Overall it can get the job done to a degree.

nettop

In order to use a tool that limits bandwidth by host, you need to know what hosts to limit bandwidth to. I discovered the command line tool “nettop” which displays your current internet traffic; protocols, hosts, ports, packets in, packets out. It’s fairly straight forward to use. To discover my outgoing traffic host when uploading files to largedocument, on the terminal I simply did:

nettop -m tcp

This simply lists the TCP network processes running on your machine. From this, it’s clear that my upload streams using all the bandwidth are going to the host largedocument.com on port 80. You can then plug this host and port into Speed Limit and immediately enjoy the benefits of bandwidth throttling on your Mac.

ipfw

Speed Limit worked for throttling bandwidth to a host. But I soon discovered a much better way to use command line tools already installed on your Mac to throttle your bandwith. ipfw (IP Firewall) has about 1000 times more options for managing your internet traffic than any other gui tool out there. I wont go into too much detail about how to use it but you should definitely take a look at the man page to see the multitude of uses it has to control your network.

To achieve the effects of limiting bandwidth to the host largedocument.com, I simply did the commands:

sudo ipfw pipe 1 config bw 40KByte/s
sudo ipfw add 1 pipe 1 dst-ip largedocument.com

The first command creates a pipe named “1” which limits bandwidth to 40 kb/s. The second command attaches this pipe to traffic whose destination host is largedocument.com, meaning all traffic to this host will effectively be limited to 40 kb/s. This is great! Later on, when you decide you want to remove this bandwidth limitation, you simply run:

sudo ipfw delete 1

And the pipe is removed.

If you’re interested in network management on your Mac, I encourage you to look more into ipfw. For example, when uploading lots of files to some file servers, hostnames change frequently over time. You may want to limit your bandwidth entirely on a computer that is just sitting idly uploading files. This is easily achieved with ipfw with the command:

sudo ipfw pipe 1 config bw 40KByte/s
sudo ipfw add 1 pipe 1 out

And all outbound traffic is piped through the pipe limited to 40 kb/s of bandwidth. Perhaps the next step is to write a script that automatically detects when hosts are maxing out your upload or download speed, then have it automatically throttle these hosts so you can continue to use the net freely. I thought I’d never say this but hooray for bandwidth throttling.

Posted in Computer Science | Leave a comment