I’ve been having trouble with libbybot (my Raspberry Pi / lamp based presence robot) in some locations. I suspect this is because the Raspberry Pi 3’s inbuilt wifi antenna isn’t as strong as that in, say a laptop, so wifi problems that go unnoticed most of the time are much more obvious.
The symptoms are these:
- Happily listening / watching remotely
- Stream dies
- I get a re-notification that libbybot is online, but can’t connect to it properly
My hypothesis is that the Raspberry Pi is briefly losing wifi connectivity, Chromium auto-reconnects, but the webRTC stream doesn’t re-initiate.
Anyway, the first step to mitigating the problem was to try and emulate it. There were a couple of ways I could have gone about this. One was use network shaping tools on my laptop to try and emulate the problems by messing with the receiving end. A more realistic way would be to shape the traffic on the Pi itself, as that’s where the problem is occurring.
Searching for network shaping tools – and specifically dropped packets and network latency, led me to the FreeBSD firewall, called dummynet and referenced by
ipfw. However, this is tightly coupled to the kernel and doesn’t seem suitable for the Raspberry Pi.
On the laptop, there is a tool for network traffic shaping on Mac OS – it used to be ipfw, but since 10.10 (details) it’s been an app called network link conditioner, available as part of Mac OS X developer tools.
Before going through the xcode palaver for something that wasn’t really what I wanted, I had one last dig for an easier way, and indeed there is: wondershaper led me to using tc to limit the bandwidth which in turn led to iptables for dropped packets.
But. None of these led to the behaviour that I wanted, in fact libbybot (which uses RTCMulticonnection for webRTC) worked perfectly under most conditions I could simulate. The same when using tc with with Netem, which can emulate network-wide delays – all fine.
Finally I twigged that the problem was probably a several-second network outage, and for that you can use iptables again. In this case using it to stop the web page (which runs on port 8443) being accessed from the Pi. Using this I managed to emulate the symptoms I’d been seeing.
Here are a few of the commands I used, for future reference.
The final, useful command: emulate a dropped network on a specific port for 20 seconds using iptables output command:
#/bin/bash echo "stopping external to 8443" iptables -A OUTPUT -p tcp --dport 8443 -j DROP sleep 20 echo "restarting external to 8443" iptables -D OUTPUT -p tcp --dport 8443 -j DROP
Other things I tried: drop 30% of (input or output) packets randomly, using iptable’s statistics plugin
sudo iptables -A INPUT -m statistic --mode random --probability 0.30 -j DROP sudo iptables -A OUTPUT -m statistic --mode random --probability 0.30 -j DROP
list current iptables rules
clear all (flush)
Delay all packets by 100ms using tc and netem
sudo tc qdisc add dev wlan0 root netem delay 100ms
change that to 2000ms
sudo tc qdisc change dev wlan0 root netem delay 2000ms 10ms 25%
All the tc rules go away when you reboot.
Context and links:
tc and netem: openWRT: Netem (Network emulator)
iptables: Using iptables to simulate service interruptions by Matt Parsons, and The Beginner’s guide to iptables, the linux firewall