Home · RSS · E-Mail · GitHub · GitLab · Twitter

Quick Tip: Simulate a bad network connection on macOS (and Linux)

Yesterday, I got a tip from a colleague to fake a poor network connection (for debugging an issue) with tc (man page):

1
tc qdisc add dev eth0 root netem loss 30% delay 500ms 300ms

This works with Linux and will add a delay and packet loss to the eth0 interface.

I was curious if there is someting similar for macOS, and there is: pfctl and dnctl. This following commands are mainly copy-pasted from the posts from John Ruble and Sal Ferrarello. Check their posts and the man pages of the commands for more details.

Enable

Add a packet loss of 30%, a bandwidth limit of 100kbit/s, and a delay of 300ms to the service running on localhost:59219 using TCP:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# add the limiting network pipe
# append 'noerror' to simulate loss on the remote site)
sudo dnctl pipe 1 config plr 0.3 bw 100Kbit/s delay 300

# make use of the pipe for a specific host
echo "dummynet in  proto tcp from any to localhost port 59219 pipe 1
      dummynet out proto tcp from any to localhost port 59219 pipe 1" | sudo pfctl -f -

# enable the firewall
sudo pfctl -e

Disable

Remove the limitations again and reset everything:

1
2
3
4
5
6
7
8
# reset the firewall
sudo pfctl -F all -f /etc/pf.conf

# remove the limiting network pipe
sudo dnctl -q flush

# disable the firewall
sudo pfctl -d

Status

Get your current configurations:

1
sudo dnctl list

and

1
sudo pfctl -s all

Home · RSS · E-Mail · GitHub · GitLab · Twitter