Open vSwitch: ovs-vsctl, ovs-ofctl, ovs-appctl, ovs-dpctl (2/3)
ovs-ofctl goes one level below ovs-vsctl: it edits the OpenFlow tables of a running OVS bridge, so it decides which packet is forwarded, copied, rate-limited or dropped. This part covers the ovs-vsctl cookbook recipes, then ovs-ofctl flows, packet-out, groups and meters.
64.1 ovs-vsctl vs ovs-ofctl
| Point | ovs-vsctl | ovs-ofctl |
|---|---|---|
| Edits | OVSDB config | OpenFlow tables of the bridge |
| Objects | Bridge, port, VLAN, mirror, QoS | Flows, groups, meters |
| Survives restart | Yes (in conf.db) | No (in ovs-vswitchd memory) |
| Protocol | OVSDB | OpenFlow |
- Lab (Linux only, in Docker): br0 with namespaces vm1, vm2, vm3 = 192.168.84.11, .12, .13 on ports 1, 2, 3.
64.2 How the flow table decides
- Default: one flow, priority=0 actions=NORMAL, so br0 behaves as a normal learning switch.
- Each flow = match fields + priority + actions; the highest-priority match wins.
- A flow with an empty action list drops the packet.
- Step 1
1.br0 table 0: priority=0 actions=NORMAL; vm1 pings vm3 and it works
- Step 2
2.ovs-ofctl add-flow: priority=100,ip,nw_dst=192.168.84.13,actions=drop
- Step 3
3.Next ping from vm1 arrives at br0
- Step 4
4.priority 100 matches first: packet dropped (n_packets of the drop rule rises)
- Step 5
5.--strict del-flows removes the rule; ping works again
Higher priority matches first
64.3 Configuration cookbook (ovs-vsctl)
- protocols=OpenFlow10,OpenFlow13,OpenFlow15: groups and meters need 1.3+.
- type=internal: a port the host sees as a normal interface (here on VLAN 10).
- type=gre options:remote_ip=...: a GRE tunnel port.
- rstp_enable / mcast_snooping_enable: Rapid STP and IGMP/MLD snooping.
- Also: QoS linux-htb, NetFlow/sFlow/IPFIX export, CFM, STP tuning, Flow_Table limit (table 0 capped at 100 flows in the lab).
ovs-vsctl set bridge br0 protocols=OpenFlow10,OpenFlow13,OpenFlow15ovs-vsctl add-port br0 vlan10 tag=10 -- set Interface vlan10 type=internalovs-vsctl add-port br0 gre0 -- set Interface gre0 type=gre \ options:remote_ip=192.168.84.99ovs-vsctl set Bridge br0 rstp_enable=trueovs-vsctl set Bridge br0 mcast_snooping_enable=true$ ovs-vsctl get Bridge br0 rstp_enabletrue$ ovs-vsctl get Bridge br0 mcast_snooping_enabletrue64.4 Port mirror
- vm1-p is source (src and dst), vm3-p is the output port: a copy of every vm1 packet goes to vm3.
- Proof: vm1 pinged vm2, but tcpdump on vm3 captured both request and reply.
- clear Bridge br0 mirrors removes the mirror.
ovs-vsctl -- set Bridge br0 mirrors=@m \-- --id=@p1 get Port vm1-p \-- --id=@p3 get Port vm3-p \-- --id=@m create Mirror name=m1 \select-src-port=@p1 select-dst-port=@p1 \output-port=@p3ovs-vsctl clear Bridge br0 mirrors$ ovs-vsctl -- set Bridge br0 mirrors=@m ...806e7f5e-d987-45de-ba3b-16a48d07fc33vm1$ ping -c 1 -W 1 192.168.84.12vm3$ tcpdump -nn -c 2 -i eth0 icmpIP 192.168.84.11 > 192.168.84.12: ICMP echo requestIP 192.168.84.12 > 192.168.84.11: ICMP echo reply2 packets captured$ ovs-vsctl clear Bridge br0 mirrors$ ovs-vsctl list mirror; echo donedone64.5 Connecting to a switch
- Bridge name br0 = short form of unix:/var/run/openvswitch/br0.mgmt (a plain path also works).
- system@ovs-system lookup failed in this version.
- tcp:IP:6653 / ssl:IP:6653 for a remote switch; nobody listened here, so Connection refused.
ovs-ofctl show br0ovs-ofctl show unix:/var/run/openvswitch/br0.mgmtovs-ofctl show /var/run/openvswitch/br0.mgmtovs-ofctl show system@ovs-systemovs-ofctl show tcp:192.168.84.99:6653$ ovs-ofctl show br0OFPT_FEATURES_REPLY (xid=0x2): dpid:00001e90e2cb0b48n_tables:254, n_buffers:0$ ovs-ofctl show unix:/var/run/openvswitch/br0.mgmtOFPT_FEATURES_REPLY (xid=0x2): dpid:00001e90e2cb0b48$ ovs-ofctl show /var/run/openvswitch/br0.mgmtOFPT_FEATURES_REPLY (xid=0x2): dpid:00001e90e2cb0b48$ ovs-ofctl show system@ovs-systemovs-ofctl: system@ovs-system is not a bridge or a socket$ ovs-ofctl show tcp:192.168.84.99:6653ovs-ofctl: tcp:192.168.84.99:6653: failed toconnect to socket (Connection refused)64.6 Switch management
- show gives OpenFlow port numbers (vm1 = 1, vm3 = 3); flows use these numbers.
- dump-ports: rx/tx counters per port.
- mod-port vm3-p down: config PORT_DOWN, ping to vm3 fails; up brings it back.
- mod-table decides what happens on a table miss and can rename a table (table 0 named classifier in the lab).
- set-frags normal / drop / nx-match for IP fragments; reassemble is not implemented in OVS.
ovs-ofctl show br0ovs-ofctl dump-ports br0 vm1-povs-ofctl mod-port br0 vm3-p downip netns exec vm1 ping -c 2 -W 1 192.168.84.13ovs-ofctl mod-port br0 vm3-p up$ ovs-ofctl show br0 1(vm1-p): addr:6e:61:54:45:1a:d4 2(vm2-p): addr:ee:1a:c7:7a:4f:f4 3(vm3-p): addr:3a:3f:ca:72:82:96 LOCAL(br0): addr:1e:90:e2:cb:0b:48OFPT_GET_CONFIG_REPLY (xid=0x4): frags=normal$ ovs-ofctl dump-ports br0 vm1-pOFPST_PORT reply (xid=0x4): 1 ports port 1: rx pkts=16, bytes=1216, drop=0, errs=0 tx pkts=23, bytes=1689, drop=0, errs=0$ ovs-ofctl mod-port br0 vm3-p down 3(vm3-p): addr:3a:3f:ca:72:82:96 config: PORT_DOWN state: LINK_DOWNvm1$ ping -c 2 -W 1 192.168.84.132 packets transmitted, 0 received, 100% packet lossdump-tables stats of every tabledump-table-features table capabilitiesmod-table 0 drop|... what to do on a missmod-table 0 name:X rename a tabledump-ports-desc port detailsmod-port up|down port on / offmod-port flood|no-flood in flooding?mod-port stp|no-stp 802.1D STP on / offmod-port forward|no-forward allow / block traffic from portset-frags br0 normal|drop|nx-match (reassemble: not implemented)queue-stats br0 eth0 1 stats of eth0 queue 1ct-flush-zone, ct-flush conntrack flush64.7 Flow table commands
- add-flow / dump-flows / del-flows; --strict matches exact priority and match.
- Counters (n_packets) prove which rule matched.
ovs-ofctl dump-flows br0ovs-ofctl add-flow br0 priority=100,ip,nw_dst=192.168.84.13,actions=dropip netns exec vm1 ping -c 2 -W 1 192.168.84.13ovs-ofctl dump-flows br0ovs-ofctl --strict del-flows br0 priority=100,ip,nw_dst=192.168.84.13$ ovs-ofctl dump-flows br0 table=0, n_packets=25, priority=0 actions=NORMALvm1$ ping -c 2 -W 1 192.168.84.132 packets transmitted, 0 received, 100% packet loss$ ovs-ofctl dump-flows br0 n_packets=2, n_bytes=196, priority=100,ip, nw_dst=192.168.84.13 actions=drop n_packets=27, n_bytes=2042, priority=0 actions=NORMAL$ ovs-ofctl --strict del-flows br0 priority=100,ip,nw_dst=192.168.84.13vm1$ ping -c 1 -W 1 192.168.84.1364 bytes from 192.168.84.13: icmp_seq=1 ttl=641 packets transmitted, 1 received, 0% packet loss- Keep production flows in a file: diff-flows shows differences (exit 2 = differences found).
- replace-flows syncs the switch to the file: adds missing, removes extra, updates changed.
- --readd replace-flows re-adds every flow even if unchanged (counters may reset).
- mod-flows changes actions of matching flows; del-flows tcp removes them.
- --bundle (atomic add) is OpenFlow 1.4+: without -O you get Protocol error.
cat flows.txtovs-ofctl diff-flows br0 flows.txt; echo exit=$?ovs-ofctl replace-flows br0 flows.txtovs-ofctl mod-flows br0 tcp,actions=normalovs-ofctl del-flows br0 tcpovs-ofctl --bundle add-flows br0 flows.txtovs-ofctl -O OpenFlow15 --bundle add-flows br0 flows.txt$ cat flows.txtpriority=0,actions=normalpriority=200,tcp,tp_dst=22,actions=drop$ ovs-ofctl diff-flows br0 flows.txt; echo exit=$?+priority=200,tcp,tp_dst=22 actions=dropexit=2$ ovs-ofctl dump-flows br0 --no-stats (after replace-flows) priority=200,tcp,tp_dst=22 actions=drop priority=0 actions=NORMAL$ ovs-ofctl mod-flows br0 tcp,actions=normal priority=200,tcp,tp_dst=22 actions=NORMAL$ ovs-ofctl del-flows br0 tcp priority=0 actions=NORMAL$ ovs-ofctl --bundle add-flows br0 flows.txtvconn|WARN|...: version negotiation failedovs-ofctl: br0: failed to connect to socket (Protocol error)$ ovs-ofctl -O OpenFlow15 --bundle add-flows ... priority=200,tcp,tp_dst=22 actions=drop priority=0 actions=NORMAL64.8 Flow syntax extra fields
- cookie=V: a 64-bit tag; filter with cookie=0x10/-1.
- idle_timeout: removed when idle (the UDP flow vanished after 7 s); hard_timeout: removed after a fixed time.
- out_port / out_group filters; dump-aggregate totals matching flows.
- priority 0-65535, bigger first; send_flow_rem notifies the controller; check_overlap rejects overlaps.
ovs-ofctl add-flow br0 cookie=0x10,priority=50,icmp,actions=normalovs-ofctl add-flow br0 idle_timeout=5,priority=60,udp,actions=normalovs-ofctl add-flow br0 hard_timeout=300,priority=70,ip,\nw_src=192.168.84.12,nw_dst=192.168.84.11,actions=output:1ovs-ofctl dump-flows br0 cookie=0x10/-1ovs-ofctl dump-flows br0 out_port=1 --no-statsovs-ofctl dump-aggregate br0 icmpsleep 7; ovs-ofctl dump-flows br0 --no-stats$ ovs-ofctl dump-flows br0 cookie=0x10/-1 cookie=0x10, n_packets=6, n_bytes=588, priority=50,icmp actions=NORMAL$ ovs-ofctl dump-flows br0 out_port=1 --no-stats hard_timeout=300, priority=70,ip, nw_src=192.168.84.12,nw_dst=192.168.84.11 actions=output:1$ ovs-ofctl dump-aggregate br0 icmpNXST_AGGREGATE reply (xid=0x8): packet_count=6 byte_count=588 flow_count=1$ sleep 7; ovs-ofctl dump-flows br0 --no-stats hard_timeout=300, priority=70,ip, nw_src=192.168.84.12,nw_dst=192.168.84.11 actions=output:1 priority=0 actions=NORMAL64.9 packet-out
- Three parts: in_port (NONE, LOCAL, CONTROLLER...), packet (full Ethernet frame in hex), actions.
- A handmade ARP request was output only on port 2: vm2 captured it, vm3 captured 0 packets.
- Step 1
1.Admin builds the hex frame: dst ff:ff:ff:ff:ff:ff, src 02:00:00:00:84:11, ARP who-has .12
- Step 2
2.ovs-ofctl packet-out br0 in_port=NONE ... actions=output:2
- Step 3
3.br0 sends the frame out of port 2 only
- Step 4
4.vm2 tcpdump: who-has 192.168.84.12 tell 192.168.84.11
- Step 5
5.vm3 tcpdump: 0 packets captured
P=ffffffffffff0200000084110806\0001080006040001020000008411\c0a8540b000000000000c0a8540covs-ofctl packet-out br0 "in_port=NONE packet=$P actions=output:2"vm2$ tcpdump -nn -e -c 1 -i eth0 arp02:00:00:00:84:11 > ff:ff:ff:ff:ff:ff,ethertype ARP (0x0806), length 42: Requestwho-has 192.168.84.12 tell 192.168.84.111 packet capturedvm3$ tcpdump -nn -e -c 1 -i eth0 arp0 packets captured64.10 Groups
- A group has several buckets, each with its own actions (OpenFlow 1.3+).
- type=all: every bucket runs (copy); select: one bucket by hash (load balancing); indirect: single bucket; fast_failover: first live bucket (watch_port).
- The short form dump-group-stats br0 1 gave unknown keyword; use group_id=1.
- insert-buckets (OpenFlow 1.5) adds a bucket; mod-group replaces buckets.
- Step 1
1.Flow: priority=300,udp,tp_dst=5000,actions=group:1
- Step 2
2.vm1 sends one UDP packet to port 5000
- Step 3
3.br0 matches the flow and hands the packet to group 1
- Step 4
4.Bucket 0 outputs to port 2 (vm2), bucket 1 to port 3 (vm3)
- Step 5
5.Both vm2 and vm3 capture the packet; group stats: 1 per bucket
ovs-ofctl -O OpenFlow13 add-group br0 \ group_id=1,type=all,bucket=output:2,bucket=output:3ovs-ofctl add-flow br0 priority=300,udp,tp_dst=5000,actions=group:1ovs-ofctl -O OpenFlow13 dump-group-stats br0 1ovs-ofctl -O OpenFlow13 dump-group-stats br0 group_id=1ovs-ofctl -O OpenFlow15 insert-buckets br0 \ group_id=1,command_bucket_id=last,bucket=bucket_id=9,actions=output:1$ ovs-ofctl -O OpenFlow13 dump-groups br0 group_id=1,type=all,bucket=actions=output:2, bucket=actions=output:3 group_id=2,type=select,bucket=weight:50, actions=output:2,bucket=weight:50,... group_id=3,type=ff,bucket=watch_port:2, actions=output:2,bucket=watch_port:3,...vm1$ echo hi | nc -u -w1 192.168.84.12 5000vm2$ tcpdump -nn -c 1 -i eth0 udp port 5000IP 192.168.84.11.44449 > 192.168.84.12.5000: UDPvm3$ tcpdump -nn -c 1 -i eth0 udp port 5000IP 192.168.84.11.44449 > 192.168.84.12.5000: UDP$ ovs-ofctl -O OpenFlow13 dump-group-stats br0 1ovs-ofctl: unknown keyword 1$ ... dump-group-stats br0 group_id=1 group_id=1,ref_count=1,packet_count=1, bucket0:packet_count=1,byte_count=45, bucket1:packet_count=1,byte_count=45$ ovs-ofctl -O OpenFlow15 dump-groups br0 1 group_id=1,type=all, bucket=bucket_id:0,actions=output:2, bucket=bucket_id:1,actions=output:3, bucket=bucket_id:9,actions=output:1| Group type | Behaviour | Use |
|---|---|---|
| all | Every bucket gets a copy | Multicast / flooding |
| select | One bucket by hash (weight) | Load balancing |
| indirect | Single bucket | Shared next hop |
| fast_failover | First live bucket (watch_port) | Fast link failover |
64.11 Meters
- Meters give rate limits (OpenFlow 1.3+); meter-features shows only the drop band here.
- meter=1,pktps,band=type=drop,rate=5: drop above 5 packets per second.
- A fast ping got 15 of 40 replies; the band counter showed 25 drops.
- Units kbps or pktps; burst_size allows short bursts; attach with actions=meter:N,...
ovs-ofctl -O OpenFlow13 meter-features br0ovs-ofctl -O OpenFlow13 add-meter br0 meter=1,pktps,stats,band=type=drop,rate=5ovs-ofctl -O OpenFlow13 add-flow br0 \ priority=400,icmp,nw_src=192.168.84.11,actions=meter:1,normalip netns exec vm1 ping -c 40 -i 0.05 -q 192.168.84.13ovs-ofctl -O OpenFlow13 meter-stats br0 meter=1$ ovs-ofctl -O OpenFlow13 meter-features br0max_meter:200000 max_bands:1 max_color:0band_types: dropcapabilities: kbps pktps burst statsvm1$ ping -c 40 -i 0.05 -q 192.168.84.1340 packets transmitted, 15 received,62.5% packet loss, time 2182ms$ ovs-ofctl -O OpenFlow13 meter-stats br0 meter=1meter:1 flow_count:1 packet_in_count:40 byte_in_count:3920 duration:7.911s bands:0: packet_count:25 byte_count:245064.12 Common mistakes
- Thinking add-flow is permanent: flows live in vswitchd memory; keep them in a file and load with replace-flows, or use a controller.
- Forgetting -O OpenFlow13/15 for groups, meters and bundles.
- A drop rule on ip does not stop ARP: ARP is not an IP packet.
- del-flows without --strict deletes every flow that matches loosely.
64.13 Interview tips
- ovs-vsctl = persistent OVSDB config; ovs-ofctl = runtime OpenFlow flows, groups and meters.
- Highest priority match wins; priority=0 NORMAL is the default learning-switch flow.
- Groups: all = copy, select = load balance, fast_failover = backup path.
- Use dump-flows counters to prove which rule a packet hit.