The idea: split deciding from forwarding
Every network box does two jobs. The data plane moves packets: look at the header, pick an output port, send. The control plane decides where packets should go: it learns addresses, runs routing protocols and fills the tables the data plane reads. In a classic switch or router both jobs live in the same box, and every box decides for itself.
Software-defined networking (SDN) takes the control plane out of the boxes and puts it in one program, the controller. The switch keeps only the data plane: a flow table of rules. OpenFlow is the protocol between the two. The controller uses it to add and remove rules; the switch uses it to ask what to do with a packet it has no rule for. On the canvas the controller is c0 at the top and the switch is s1 in the middle, with four hosts on its four ports.
The first packet of a flow
A new switch knows nothing. Its table holds one rule, the table-miss entry: priority 0, matches anything, action "send to the controller". So the first packet of every flow takes a detour (Demo: first packet misses, next ones match):
- The packet matches only the table-miss entry. The switch sends its headers up in a
PACKET_INmessage and keeps the packet waiting. - The controller's program looks at the headers and decides. It sends a
FLOW_MOD: a new rule for this kind of packet. - It also sends a
PACKET_OUT: what to do with the packet that is waiting. - The second packet of the flow matches the new rule in the switch. The controller never sees it.
This is called reactive flow setup. The detour costs time (a round trip to the controller), so the first packet is slow and the rest are fast. A controller can also install rules before any traffic arrives; that is proactive setup, and the firewall's drop rule on this page is installed that way.
Look at the counters after the first demo. Three packets were sent, but the new rule shows n_packets 2 and the table-miss entry shows 1. The first packet was sent by the PACKET_OUT, not by the rule.
Match + actions: one switch, four jobs
A rule has two halves. The match names header fields and the values they must have: in_port, eth_dst, ipv4_src, ipv4_dst, tcp_dst and many more. A field that is not named matches anything. The actions say what to do: send out a port, rewrite a field, lower the TTL, or nothing at all, which means drop.
The switch has no idea what kind of device it is. The rules decide that:
| controller app | match | actions | the switch behaves as |
|---|---|---|---|
| L2 switch | eth_dst=…:03 | output:3 | an Ethernet switch: forward by destination MAC |
| router | ipv4_dst=10.0.2.3 | dec_ttl, set eth_dst=…:03, output:3 | a router: forward by destination IP, lower the TTL, rewrite the MAC |
| firewall | tcp_dst=22 at priority 200 | none (drop) | a packet filter: ssh is blocked, the rest is switched |
| load balancer | ipv4_src=10.0.1.1, ipv4_dst=10.0.2.100, tcp_dst=80 | set ipv4_dst=10.0.2.3, output:3 | a load balancer: one virtual IP, clients spread over h3 and h4 |
In Demo: the same switch as a router the packet arrives with TTL 64 and leaves with TTL 63 and h3's MAC. In Demo: load balancer spreads two clients both clients send to 10.0.2.100; the controller picks h3 for h1 and h4 for h2, and after four packets each server has received 2.
Priority, counters and idle timeout
More than one rule can match a packet. Then the rule with the highest priority wins, and only that rule runs. In Demo: firewall rule wins by priority an ssh packet to h3 matches two rules: eth_dst=…:03 at priority 10 says forward, tcp_dst=22 at priority 200 says drop. It is dropped. The table-miss entry works the same way: it matches everything, but at priority 0 it only wins when nothing else fits.
Every rule has counters. n_packets goes up each time the rule wins. The controller can read them to see how much traffic each flow carries.
A rule can also carry an idle timeout. Each match restarts the timer. If no packet matches for that long, the switch removes the rule and tells the controller with a FLOW_REMOVED message that includes the final counters. On this page reactive rules have idle_timeout=10. In Demo: idle timeout removes a rule the rule is used at t = 5 s, runs out at t = 15 s with n_packets=1, and the next packet misses again. Timeouts keep the table small: a flow that ended does not hold a row for ever. (There is also a hard_timeout that removes a rule after a fixed time, used or not.)
When the controller is unreachable
The controller is one program on one machine, and the switch needs it for every new flow. What if the connection is lost? In fail-secure mode the switch changes only one thing: packets meant for the controller are dropped. Rules already in the table keep forwarding. But their timeouts keep running, and nobody installs them again.
Demo: controller unreachable shows all three effects. The known flow h1 → h3 still works. The new flow h2 → h4 misses and is dropped. Ten seconds later the h1 → h3 rule has timed out, and that flow is dropped too. When the controller comes back, the next packet misses, asks, and the flow works again. Final count: 5 packets sent, 3 delivered, 2 dropped.
The other choice is fail-standalone: the switch falls back to acting as a plain learning switch until the controller returns. Real networks also run several controllers, so that losing one is not the end.
See also hub, switch and router for the classic boxes, and the stateful firewall, which remembers connections, something a plain flow table does not do.
What the page leaves out
The controller here already knows where every host is. A real one learns that from the PACKET_INs, floods packets for unknown addresses, and answers or forwards ARP. Only one direction of each flow is shown; replies need their own rules, and a load balancer must also rewrite the reply's source back to the virtual IP and the request's destination MAC. Real switches have several flow tables chained with goto_table, group tables, meters, hard_timeout, byte counters and wildcard masks such as 10.0.2.0/24. The channel's own messages (HELLO, FEATURES_REQUEST, ECHO) and its TLS are skipped, and so are buffer ids, several controllers with roles, and the rule that overlapping matches at the same priority are undefined.