A Linux Packet Journey
with Docker
Elazar Leibovich
DevOpsDays, Tel Aviv 2019
Who am I?
● Work
○ Storage @VAST Data
○ Virtualization @Ravello
○ Hadoop @Akamai
● Open Source
○ goproxy author
● elazarl @Twitter
Agenda
● A packet’s life cycle in Linux
● How it applies with containers
● Example problem & solution
Agenda
● A packet’s life cycle in Linux
● How it applies with containers
● Example problem & solution
● Overview of all network devices
● In depth in-kernel infrastructure
The Problem
I wrote a new service
● Collectd-like
● Metrics by UDP
… deployed it on my laptop
With Docker
docker run -d myapp
How do I get there from the outside?
docker run -tip8000:8000/udp myapp
How can we do that in run time?
How can we do that in run time?
Networking Basics
Network Namespace
All network components
in a box:
● Network devices
● Routing tables
● …
1 Docker container → 1 network
namespace
Network Namespace Diagnosis
Network namespace ID
Packet from Host to Container
Docker Namespace Default namespace
bridge
Veth
Pair
Veth
Pair
docker0
Packet from Host to Container
Docker Namespace Default namespace
bridge
Veth
Pair
Veth
Pair
docker0
Packet from Host to Container
Docker Namespace Default namespace
bridge
Veth
Pair
Veth
Pair
docker0
Packet from Host to Container
Docker Namespace Default namespace
bridge
Veth
Pair
Veth
Pair
docker0
Packet from Host to Container
Docker Namespace Default namespace
bridge
Veth
Pair
Veth
Pair
docker0
Veth pair Diagnosis - ip link
My ID
Veth pair
Diagnosis of Interfaces in Bridge - ip link
This is my bridgeThis is me
Bridge Diagnosis - /sys/class/net/
Back to Our Problem
Required Packet Journey
Docker Namespace Default namespace
bridge
Veth
Pair
Veth
Pair
docker0
eth0
Required Packet Journey
Docker Namespace Default namespace
bridge
Veth
Pair
Veth
Pair
docker0
eth0
Required Packet Journey
Docker Namespace Default namespace
bridge
Veth
Pair
Veth
Pair
docker0
eth0
Required Packet Journey
Docker Namespace Default namespace
bridge
Veth
Pair
Veth
Pair
docker0
eth0
Required Packet Journey
Docker Namespace Default namespace
bridge
Veth
Pair
Veth
Pair
docker0
eth0
How can we do that?
What happens to incoming packets?
Ingress Packet
Is this packet for us?
● If it is - local delivery
● If it’s for someone else - forward it
Incoming packet to eth0
Docker Namespace Root namespace
bridge
Veth
Pair
Veth
Pair
docker0
eth0
172.17.0.1
10.25.0.1
172.17.0.2
Incoming packet to eth0
Docker Namespace Root namespace
bridge
Veth
Pair
Veth
Pair
docker0
eth0
172.17.0.1
10.25.0.1
172.17.0.2
Packet
Destination IP
10.25.0.1
Incoming packet to eth0
Docker Namespace Root namespace
bridge
Veth
Pair
Veth
Pair
docker0
eth0
172.17.0.1
10.25.0.1
172.17.0.2
Destination ==
laptop IP
10.25.0.1
Local Delivery!
Docker Namespace Root namespace
bridge
Veth
Pair
Veth
Pair
docker0
eth0
172.17.0.1
10.25.0.1
172.17.0.2
UdpNoPorts
nstat | rg UdpNoPorts
● Formal documentation:
The total number of received UDP datagrams for which there was no
application at the destination port
● Simple words:
The number of UDP packet dropped
What Would Happen?
We send a packet to port 8000
What Would Happen?
It’s dropped!
Moving a Packet to docker0
iptables
● Rule-based packet manipulation
● In various point of the packet lifecycle
iptables
● Rule-based packet manipulation
● In various point of the packet lifecycle
● We want to manipulate the packet’s destination before we
route it!
Solution
iptables 
-A PREROUTING -t nat # when? before routing
Solution
iptables 
-A PREROUTING -t nat # when? before routing 
-i eth0 -p udp --dport 8000 # which? if pkt in eth0 from 8000
Solution
iptables 
-A PREROUTING -t nat # when? before routing 
-i eth0 -p udp --dport 8000 # which? if pkt in eth0 from 8000 
-j DNAT --to-destination 172.17.0.2:8000 # what? route to Docker
We’re done!
● At first, we couldn’t send packets to a running container
● Docker said: “impossible”
● With a basic iptables command - we did it!
Summary
● You can do amazing things with Linux networking
● Available by default, on any server
● Basic knowledge is required
Questions?
I have a question for you!
Can you forward packets to a
container only when no one accepts
them on the host?@elazarl
More Information
Linux packet journey,napi, hardware queue,skb:
https://youtu.be/6Fl1rsxk4JQ
The Journey of a Packet Through the Linux Network Stack
Tools used
lsd - the next gen ls command
bat - A cat(1) clone with wings.
column(1)
ripgrep recursively searches directories for a regex pattern
ip(8)
rtacct(8) nstat(8)

How Linux Processes Your Network Packet - Elazar Leibovich