ip (iproute2) (2/6)
ip link builds the virtual network devices that Docker, Kubernetes and cloud networks are made of: veth cables, bridges, VXLAN/Geneve overlays, GRE/ipip/sit tunnels, macvlan, MACsec, VRF and more. This part builds them in a real lab and then changes and deletes them with ip link set / delete.
18.1 Where it runs
- Linux only, iproute2 package, needs root or sudo.
- Windows and macOS do not have these link types, so the lab runs inside a Linux container.
- Lab: two network namespaces a and b act as two separate hosts (Host A, Host B).
18.2 veth and bridge
- veth pair = virtual cable: a packet entering one end comes out of the other immediately.
- type veth peer name eth0 netns a: end pa stays in the root namespace, eth0 moves into namespace a.
- vxcan is the same kind of pair for CAN bus.
- bridge br0 = virtual switch; master br0 plugs a port into it like a cable into a switch; stp_state 0 keeps spanning tree off.
- Step 1
1.Host A (ns a) eth0 192.168.38.1 pings 192.168.38.2
- Step 2
2.Frame leaves eth0, comes out of veth peer pa
- Step 3
3.br0 switches it from port pa to port pb
- Step 4
4.pb's peer eth0 in ns b receives it
- Step 5
5.Host B replies: 64 bytes from 192.168.38.2
ip netns add aip netns add bip link add pa type veth \ peer name eth0 netns aip link add pb type veth \ peer name eth0 netns bip -br link show type veth$ ip -br link show type vethpa@if9 DOWN 02:52:a7:13:83:bfpb@if9 DOWN d2:b8:d6:47:1d:a4ip link add br0 type bridge stp_state 0ip link set pa master br0ip link set pb master br0ip link set br0 upip -n a addr add 192.168.38.1/24 dev eth0ip -n b addr add 192.168.38.2/24 dev eth0ip -d link show br0ip netns exec a ping -c 2 192.168.38.2$ ip -d link show br011: br0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 bridge forward_delay 1500 hello_time 200 max_age 2000 ageing_time 30000 stp_state 0 priority 32768 vlan_filtering 0$ ip netns exec a ping -c 2 192.168.38.264 bytes from 192.168.38.2: icmp_seq=1 ttl=6464 bytes from 192.168.38.2: icmp_seq=2 ttl=642 packets transmitted, 2 received, 0% packet loss- 37 bridge options in 3 groups: STP timers, VLAN filtering (VLAN-aware switch), multicast snooping; change them with ip link set br0 type bridge ...
ageing_time T how long a MAC staysforward_delay T STP listening/learninghello_time T gap between hellosmax_age T neighbor dead timeoutstp_state 0|1 spanning tree off/onpriority N root election prioritygroup_fwd_mask M link-local forward maskgroup_address MAC STP multicast MACvlan_filtering 0|1 VLAN-aware bridgevlan_protocol P 802.1Q or 802.1advlan_default_pvid untagged VLAN idvlan_stats_* per-VLAN countersmcast_snooping 0|1 IGMP/MLD snoopingmcast_router 0-2 multicast router portmcast_*_interval query/member timers18.3 VXLAN
- Problem: two hosts in different data centres with only an IP network between them need the same LAN.
- VXLAN wraps the L2 frame in UDP port 4789; the 24-bit VNI (here 100) names the virtual network.
- The underlay forwards using only the outer header; the remote VTEP strips it and delivers the original frame.
- group instead of remote finds peers via multicast; learning toggles MAC learning; ttl/tos/df control the outer header.
- Step 1
1.Host A vx100 (10.100.0.1) sends a frame to 10.100.0.2
- Step 2
2.VTEP wraps it: outer IP 192.168.38.1 -> 192.168.38.2, UDP 4789, VNI 100
- Step 3
3.Underlay (br0) forwards on the outer header only
- Step 4
4.Host B VTEP removes the outer headers (decap)
- Step 5
5.Original frame delivered to vx100 10.100.0.2
Overhead 50 bytes: 20 IP + 8 UDP + 8 VXLAN + 14 inner Ethernet, so MTU 1500 -> 1450
ip -n a link add vx100 type vxlan id 100 \ local 192.168.38.1 remote 192.168.38.2 \ dstport 4789 dev eth0ip -n a addr add 10.100.0.1/24 dev vx100ip -n a link set vx100 upip -n a -d link show vx100ip netns exec a ping -c 2 10.100.0.2$ ip -n a -d link show vx10010: vx100: <BROADCAST,MULTICAST,UP> mtu 1450 vxlan id 100 remote 192.168.38.2 local 192.168.38.1 dev eth0 srcport 0 0 dstport 4789 ttl auto$ ip netns exec a ping -c 2 10.100.0.264 bytes from 10.100.0.2: icmp_seq=1 ttl=6464 bytes from 10.100.0.2: icmp_seq=2 ttl=642 packets transmitted, 2 received, 0% packet losstcpdump on br0 proves it: each packet has an outer header with vni 100, then the inner ARP or ICMP in plain text.
tcpdump -ni br0 -c 4 udp port 4789$ tcpdump -ni br0 -c 4 udp port 4789IP 192.168.38.1.46362 > 192.168.38.2.4789: VXLAN, flags [I] (0x08), vni 100ARP, Request who-has 10.100.0.2 tell 10.100.0.1IP 192.168.38.2.46362 > 192.168.38.1.4789: VXLAN, flags [I] (0x08), vni 100ARP, Reply 10.100.0.2 is-at ee:76:ce:59:b9:57IP 192.168.38.1.53588 > 192.168.38.2.4789: VXLAN, flags [I] (0x08), vni 100IP 10.100.0.1 > 10.100.0.2: ICMP echo requestIP 192.168.38.2.53588 > 192.168.38.1.4789: VXLAN, flags [I] (0x08), vni 100IP 10.100.0.2 > 10.100.0.1: ICMP echo replyid VNI segment id (24-bit)dev / local IP underlay port, sourceremote IP unicast peer (VTEP)group IP multicast peersdstport / srcport UDP portsttl / tos / df outer IPv4 headerflowlabel N outer IPv6 flow label[no]learning learn MACs or not[no]proxy, [no]rsc ARP proxy, short circuit[no]l2miss/l3miss netlink miss alerts[no]udpcsum UDP checksum (IPv4)udp6zerocsumtx/rx IPv6 zero checksumageing / maxaddress FDB timer and limit[no]vnifilter VNI filteringgbp / gpe VXLAN extensions18.4 GRE, ipip, sit, Geneve and other tunnels
- GRE wraps an IP packet in a small GRE header without UDP; flags POINTOPOINT,NOARP and MTU 1476.
- gretap carries the whole Ethernet frame, so it can join a bridge; key keeps several tunnels between the same hosts apart.
- ipip = IPv4 in IPv4, sit = IPv6 in IPv4; ip6gre/ip6gretap use an IPv6 outer header (hoplimit, tclass).
- Geneve: flexible VXLAN successor on UDP 6081 with metadata; ERSPAN mirrors traffic via GRE to a remote analyzer.
- bareudp: generic UDP tunnel for MPLS/IP with no extra header; AMT tunnels multicast over a unicast-only network.
ip -n a link add gre1 type gre \ local 192.168.38.1 remote 192.168.38.2 \ ttl 64ip -n a addr add 10.200.0.1/30 dev gre1ip -n a link set gre1 upip -n a -d link show gre1ip netns exec a ping -c 2 10.200.0.2$ ip -n a -d link show gre111: gre1@NONE: <POINTOPOINT,NOARP,UP> mtu 1476 link/gre 192.168.38.1 peer 192.168.38.2 gre remote 192.168.38.2 local 192.168.38.1 ttl 64$ ip netns exec a ping -c 2 10.200.0.264 bytes from 10.200.0.2: icmp_seq=1 ttl=6464 bytes from 10.200.0.2: icmp_seq=2 ttl=642 packets transmitted, 2 received, 0% packet loss# gre / gretap: extra optionskey / ikey / okey K keyed GREseq / csum (i/o) sequence, checksum[no]pmtudisc path MTU discovery[no]ignore-df, dev DF ignore, underlay# ipip / sitremote / local IP tunnel endpointsmode ipip | ip6ip IPv4 / IPv6 in IPv4mode mplsip | any MPLS or anyencap fou|gue|none extra UDP wrap# ip6gre / ip6gretaphoplimit / encaplimit outer TTL, nestingtclass / flowlabel IPv6 header fields# geneve (UDP 6081)id VNI / remote IP segment and peerinnerprotoinherit L3 without Ethernet# erspan / ip6erspanerspan_ver 0|1|2 ERSPAN type I/II/IIIerspan_dir in/egress mirror direction (v2)# bareudpdstport PORT tunnel UDP portethertype PROTO ipv4, ipv6, mpls_uc# amtmode gateway | relay AMT rolediscovery IP relay discovery IP18.5 macvlan and special types
- macvlan is not a tunnel: several virtual interfaces on one physical parent, each with its own MAC (mv0@eth0).
- Modes: private (isolated), vepa (hairpin via the outside switch), bridge (talk directly), passthru (one endpoint gets all traffic), source (allowed source MACs).
- macvtap = macvlan + a tap device used by virtual machines.
- MACsec encrypts and authenticates every L2 frame; HSR sends each frame out of both ring ports.
- VRF gives interfaces their own routing table; rmnet/gtp for mobile networks; xfrm for IPsec; ipoib for IP over InfiniBand.
ip -n a link add mv0 link eth0 \ type macvlan mode bridgeip -n a -d link show mv0$ ip -n a -d link show mv012: mv0@eth0: <BROADCAST,MULTICAST> mtu 1500 link/ether 6a:03:3e:28:ed:11 macvlan mode bridge bcqueuelen 1000# macsec (802.1AE)cipher SUITE gcm-aes-128 / 256encrypt on|off encrypt or auth onlyreplay on window N replay protectionvalidate strict bad frames drop# hsrslave1 / slave2 DEV two ring portsversion 0|1, proto HSR 2010/2012, PRP# vrf / rmnet / xfrm / gtpvrf table N routing table of VRFrmnet mux_id N modem mux id (1-254)xfrm dev DEV IPsec underlayxfrm if_id ID policy match keygtp role sgsn|ggsn mobile core tunnel# ipoibpkey PKEY InfiniBand partitionmode datagram|conn IPoIB mode| Type | Carries | Outer header | Lab MTU / note |
|---|---|---|---|
| veth | Frames between two ends (virtual cable) | None | Container <-> host/bridge |
| bridge | Switches frames between ports | None | Virtual switch br0 |
| vxlan | Whole L2 frame | IP + UDP 4789 + VXLAN (VNI 24-bit) | 1450 |
| geneve | L2 frame + metadata | IP + UDP 6081 | VXLAN successor |
| gre | IP packet | IP + GRE (no UDP) | 1476, point-to-point |
| gretap | Whole Ethernet frame | IP + GRE | Can join a bridge |
| ipip / sit | IPv4 in IPv4 / IPv6 in IPv4 | IPv4 | Simplest tunnels |
| macvlan | Sub-interface with own MAC on a parent | None | Not a tunnel |
18.6 ip link set (device attributes)
- Rename (name), mtu, address (MAC), alias, up/down; group N lets one command act on many interfaces.
- netns moves an interface to another namespace; master adds it to a bridge or bond; arp/multicast/promisc toggle flags.
- addrgenmode decides the IPv6 link-local interface ID: eui64 (from MAC), none, stable_secret (from a sysctl secret), random.
ip -n a link set mv0 name lan0ip -n a link set lan0 mtu 1400ip -n a link set lan0 address 02:38:00:00:00:10ip -n a link set lan0 alias lab-portip -n a link set lan0 upip -n a link show lan0ip -n a link set lan0 group 5ip -n a link set group 5 downip -n a -br link show group 5$ ip -n a link show lan012: lan0@eth0: <BROADCAST,MULTICAST,UP> mtu 1400 link/ether 02:38:00:00:00:10 alias lab-port$ ip -n a link set lan0 group 5$ ip -n a link set group 5 down$ ip -n a -br link show group 5lan0@eth0 DOWN 02:38:00:00:00:10up / down, change on / off, set synonymarp on|off NOARP flagmulticast, allmulti multicast flagspromisc on|off receive all framescarrier, protodown link state flagstxqueuelen N transmit queuemax_gso/gro_size offload sizesnetns NAME|PID move to namespacemaster / nomaster add to bridge, bondvrf NAME add to VRFaddrgenmode eui64 EUI-64 ID from the MACaddrgenmode none no auto IPv6 addressaddrgenmode stable_secret sysctl secret IDaddrgenmode random ID from auto random secrettype T ARGS type-specific settings18.7 ip link delete
- delete group 5 removes every link of that group at once; del vx100 type vxlan deletes one tunnel; ip link delete br0 removes the bridge.
- All these devices vanish on reboot unless created by a config tool.
ip -n a link delete group 5ip -n a link del vx100 type vxlanip -n a -br linkip link delete br0ip -br link$ ip -n a -br linklo DOWN 00:00:00:00:00:00eth0@if9 UP 42:09:d9:41:e3:efgre1@NONE UNKNOWN 192.168.38.1$ ip link delete br0$ ip -br linklo UNKNOWN 00:00:00:00:00:00pa@if9 UP 02:52:a7:13:83:bfpb@if9 UP d2:b8:d6:47:1d:a418.8 Common mistakes
- Forgetting the encapsulation overhead: VXLAN needs MTU 1450 on a 1500 underlay, else large packets fail.
- Thinking a tunnel means encryption: VXLAN and GRE only wrap (tcpdump shows inner ICMP in plain text); use IPsec, WireGuard or MACsec.
- Creating a veth end but never moving the peer into the namespace or never bringing both ends up.
- Enabling STP or vlan_filtering without understanding the forward delay / PVID effects.
18.9 Interview tips
- VXLAN: L2 frames in UDP 4789 over any L3 network; 24-bit VNI = ~16 million segments vs 4094 VLANs.
- veth = virtual cable, bridge = virtual switch: the building blocks of Docker networking.
- GRE (no UDP, MTU 1476) vs VXLAN (UDP, MTU 1450); gretap/VXLAN carry L2, GRE/ipip carry L3.
- macvlan gives containers their own MAC on the physical LAN; VRF separates routing tables per customer.