switchdev.txt 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. Ethernet switch device driver model (switchdev)
  2. ===============================================
  3. Copyright (c) 2014 Jiri Pirko <jiri@resnulli.us>
  4. Copyright (c) 2014-2015 Scott Feldman <sfeldma@gmail.com>
  5. The Ethernet switch device driver model (switchdev) is an in-kernel driver
  6. model for switch devices which offload the forwarding (data) plane from the
  7. kernel.
  8. Figure 1 is a block diagram showing the components of the switchdev model for
  9. an example setup using a data-center-class switch ASIC chip. Other setups
  10. with SR-IOV or soft switches, such as OVS, are possible.
  11.                              User-space tools                                 
  12.                                                                               
  13.        user space                   |                                         
  14.       +-------------------------------------------------------------------+   
  15.        kernel                       | Netlink                                 
  16.                                     |                                         
  17.                      +--------------+-------------------------------+         
  18.                      |         Network stack                        |         
  19.                      |           (Linux)                            |         
  20.                      |                                              |         
  21.                      +----------------------------------------------+         
  22.                                                                               
  23. sw1p2 sw1p4 sw1p6
  24.                       sw1p1  + sw1p3 +  sw1p5 +         eth1             
  25.                         +    |    +    |    +    |            +               
  26.                         |    |    |    |    |    |            |               
  27.                      +--+----+----+----+-+--+----+---+  +-----+-----+         
  28.                      |         Switch driver         |  |    mgmt   |         
  29.                      |        (this document)        |  |   driver  |         
  30.                      |                               |  |           |         
  31.                      +--------------+----------------+  +-----------+         
  32.                                     |                                         
  33.        kernel                       | HW bus (eg PCI)                         
  34.       +-------------------------------------------------------------------+   
  35.        hardware                     |                                         
  36.                      +--------------+---+------------+                        
  37.                      |         Switch device (sw1)   |                        
  38.                      |  +----+                       +--------+               
  39.                      |  |    v offloaded data path   | mgmt port              
  40.                      |  |    |                       |                        
  41.                      +--|----|----+----+----+----+---+                        
  42.                         |    |    |    |    |    |                            
  43.                         +    +    +    +    +    +                            
  44.                        p1   p2   p3   p4   p5   p6
  45.                                        
  46.                              front-panel ports                                
  47.                                                                               
  48. Fig 1.
  49. Include Files
  50. -------------
  51. #include <linux/netdevice.h>
  52. #include <net/switchdev.h>
  53. Configuration
  54. -------------
  55. Use "depends NET_SWITCHDEV" in driver's Kconfig to ensure switchdev model
  56. support is built for driver.
  57. Switch Ports
  58. ------------
  59. On switchdev driver initialization, the driver will allocate and register a
  60. struct net_device (using register_netdev()) for each enumerated physical switch
  61. port, called the port netdev. A port netdev is the software representation of
  62. the physical port and provides a conduit for control traffic to/from the
  63. controller (the kernel) and the network, as well as an anchor point for higher
  64. level constructs such as bridges, bonds, VLANs, tunnels, and L3 routers. Using
  65. standard netdev tools (iproute2, ethtool, etc), the port netdev can also
  66. provide to the user access to the physical properties of the switch port such
  67. as PHY link state and I/O statistics.
  68. There is (currently) no higher-level kernel object for the switch beyond the
  69. port netdevs. All of the switchdev driver ops are netdev ops or switchdev ops.
  70. A switch management port is outside the scope of the switchdev driver model.
  71. Typically, the management port is not participating in offloaded data plane and
  72. is loaded with a different driver, such as a NIC driver, on the management port
  73. device.
  74. Port Netdev Naming
  75. ^^^^^^^^^^^^^^^^^^
  76. Udev rules should be used for port netdev naming, using some unique attribute
  77. of the port as a key, for example the port MAC address or the port PHYS name.
  78. Hard-coding of kernel netdev names within the driver is discouraged; let the
  79. kernel pick the default netdev name, and let udev set the final name based on a
  80. port attribute.
  81. Using port PHYS name (ndo_get_phys_port_name) for the key is particularly
  82. useful for dynamically-named ports where the device names its ports based on
  83. external configuration. For example, if a physical 40G port is split logically
  84. into 4 10G ports, resulting in 4 port netdevs, the device can give a unique
  85. name for each port using port PHYS name. The udev rule would be:
  86. SUBSYSTEM=="net", ACTION=="add", DRIVER="<driver>", ATTR{phys_port_name}!="", \
  87. NAME="$attr{phys_port_name}"
  88. Suggested naming convention is "swXpYsZ", where X is the switch name or ID, Y
  89. is the port name or ID, and Z is the sub-port name or ID. For example, sw1p1s0
  90. would be sub-port 0 on port 1 on switch 1.
  91. Switch ID
  92. ^^^^^^^^^
  93. The switchdev driver must implement the switchdev op switchdev_port_attr_get
  94. for SWITCHDEV_ATTR_ID_PORT_PARENT_ID for each port netdev, returning the same
  95. physical ID for each port of a switch. The ID must be unique between switches
  96. on the same system. The ID does not need to be unique between switches on
  97. different systems.
  98. The switch ID is used to locate ports on a switch and to know if aggregated
  99. ports belong to the same switch.
  100. Port Features
  101. ^^^^^^^^^^^^^
  102. NETIF_F_NETNS_LOCAL
  103. If the switchdev driver (and device) only supports offloading of the default
  104. network namespace (netns), the driver should set this feature flag to prevent
  105. the port netdev from being moved out of the default netns. A netns-aware
  106. driver/device would not set this flag and be responsible for partitioning
  107. hardware to preserve netns containment. This means hardware cannot forward
  108. traffic from a port in one namespace to another port in another namespace.
  109. Port Topology
  110. ^^^^^^^^^^^^^
  111. The port netdevs representing the physical switch ports can be organized into
  112. higher-level switching constructs. The default construct is a standalone
  113. router port, used to offload L3 forwarding. Two or more ports can be bonded
  114. together to form a LAG. Two or more ports (or LAGs) can be bridged to bridge
  115. L2 networks. VLANs can be applied to sub-divide L2 networks. L2-over-L3
  116. tunnels can be built on ports. These constructs are built using standard Linux
  117. tools such as the bridge driver, the bonding/team drivers, and netlink-based
  118. tools such as iproute2.
  119. The switchdev driver can know a particular port's position in the topology by
  120. monitoring NETDEV_CHANGEUPPER notifications. For example, a port moved into a
  121. bond will see it's upper master change. If that bond is moved into a bridge,
  122. the bond's upper master will change. And so on. The driver will track such
  123. movements to know what position a port is in in the overall topology by
  124. registering for netdevice events and acting on NETDEV_CHANGEUPPER.
  125. L2 Forwarding Offload
  126. ---------------------
  127. The idea is to offload the L2 data forwarding (switching) path from the kernel
  128. to the switchdev device by mirroring bridge FDB entries down to the device. An
  129. FDB entry is the {port, MAC, VLAN} tuple forwarding destination.
  130. To offloading L2 bridging, the switchdev driver/device should support:
  131. - Static FDB entries installed on a bridge port
  132. - Notification of learned/forgotten src mac/vlans from device
  133. - STP state changes on the port
  134. - VLAN flooding of multicast/broadcast and unknown unicast packets
  135. Static FDB Entries
  136. ^^^^^^^^^^^^^^^^^^
  137. The switchdev driver should implement ndo_fdb_add, ndo_fdb_del and ndo_fdb_dump
  138. to support static FDB entries installed to the device. Static bridge FDB
  139. entries are installed, for example, using iproute2 bridge cmd:
  140. bridge fdb add ADDR dev DEV [vlan VID] [self]
  141. The driver should use the helper switchdev_port_fdb_xxx ops for ndo_fdb_xxx
  142. ops, and handle add/delete/dump of SWITCHDEV_OBJ_ID_PORT_FDB object using
  143. switchdev_port_obj_xxx ops.
  144. XXX: what should be done if offloading this rule to hardware fails (for
  145. example, due to full capacity in hardware tables) ?
  146. Note: by default, the bridge does not filter on VLAN and only bridges untagged
  147. traffic. To enable VLAN support, turn on VLAN filtering:
  148. echo 1 >/sys/class/net/<bridge>/bridge/vlan_filtering
  149. Notification of Learned/Forgotten Source MAC/VLANs
  150. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  151. The switch device will learn/forget source MAC address/VLAN on ingress packets
  152. and notify the switch driver of the mac/vlan/port tuples. The switch driver,
  153. in turn, will notify the bridge driver using the switchdev notifier call:
  154. err = call_switchdev_notifiers(val, dev, info);
  155. Where val is SWITCHDEV_FDB_ADD when learning and SWITCHDEV_FDB_DEL when
  156. forgetting, and info points to a struct switchdev_notifier_fdb_info. On
  157. SWITCHDEV_FDB_ADD, the bridge driver will install the FDB entry into the
  158. bridge's FDB and mark the entry as NTF_EXT_LEARNED. The iproute2 bridge
  159. command will label these entries "offload":
  160. $ bridge fdb
  161. 52:54:00:12:35:01 dev sw1p1 master br0 permanent
  162. 00:02:00:00:02:00 dev sw1p1 master br0 offload
  163. 00:02:00:00:02:00 dev sw1p1 self
  164. 52:54:00:12:35:02 dev sw1p2 master br0 permanent
  165. 00:02:00:00:03:00 dev sw1p2 master br0 offload
  166. 00:02:00:00:03:00 dev sw1p2 self
  167. 33:33:00:00:00:01 dev eth0 self permanent
  168. 01:00:5e:00:00:01 dev eth0 self permanent
  169. 33:33:ff:00:00:00 dev eth0 self permanent
  170. 01:80:c2:00:00:0e dev eth0 self permanent
  171. 33:33:00:00:00:01 dev br0 self permanent
  172. 01:00:5e:00:00:01 dev br0 self permanent
  173. 33:33:ff:12:35:01 dev br0 self permanent
  174. Learning on the port should be disabled on the bridge using the bridge command:
  175. bridge link set dev DEV learning off
  176. Learning on the device port should be enabled, as well as learning_sync:
  177. bridge link set dev DEV learning on self
  178. bridge link set dev DEV learning_sync on self
  179. Learning_sync attribute enables syncing of the learned/forgotton FDB entry to
  180. the bridge's FDB. It's possible, but not optimal, to enable learning on the
  181. device port and on the bridge port, and disable learning_sync.
  182. To support learning and learning_sync port attributes, the driver implements
  183. switchdev op switchdev_port_attr_get/set for
  184. SWITCHDEV_ATTR_PORT_ID_BRIDGE_FLAGS. The driver should initialize the attributes
  185. to the hardware defaults.
  186. FDB Ageing
  187. ^^^^^^^^^^
  188. The bridge will skip ageing FDB entries marked with NTF_EXT_LEARNED and it is
  189. the responsibility of the port driver/device to age out these entries. If the
  190. port device supports ageing, when the FDB entry expires, it will notify the
  191. driver which in turn will notify the bridge with SWITCHDEV_FDB_DEL. If the
  192. device does not support ageing, the driver can simulate ageing using a
  193. garbage collection timer to monitor FBD entries. Expired entries will be
  194. notified to the bridge using SWITCHDEV_FDB_DEL. See rocker driver for
  195. example of driver running ageing timer.
  196. To keep an NTF_EXT_LEARNED entry "alive", the driver should refresh the FDB
  197. entry by calling call_switchdev_notifiers(SWITCHDEV_FDB_ADD, ...). The
  198. notification will reset the FDB entry's last-used time to now. The driver
  199. should rate limit refresh notifications, for example, no more than once a
  200. second. (The last-used time is visible using the bridge -s fdb option).
  201. STP State Change on Port
  202. ^^^^^^^^^^^^^^^^^^^^^^^^
  203. Internally or with a third-party STP protocol implementation (e.g. mstpd), the
  204. bridge driver maintains the STP state for ports, and will notify the switch
  205. driver of STP state change on a port using the switchdev op
  206. switchdev_attr_port_set for SWITCHDEV_ATTR_PORT_ID_STP_UPDATE.
  207. State is one of BR_STATE_*. The switch driver can use STP state updates to
  208. update ingress packet filter list for the port. For example, if port is
  209. DISABLED, no packets should pass, but if port moves to BLOCKED, then STP BPDUs
  210. and other IEEE 01:80:c2:xx:xx:xx link-local multicast packets can pass.
  211. Note that STP BDPUs are untagged and STP state applies to all VLANs on the port
  212. so packet filters should be applied consistently across untagged and tagged
  213. VLANs on the port.
  214. Flooding L2 domain
  215. ^^^^^^^^^^^^^^^^^^
  216. For a given L2 VLAN domain, the switch device should flood multicast/broadcast
  217. and unknown unicast packets to all ports in domain, if allowed by port's
  218. current STP state. The switch driver, knowing which ports are within which
  219. vlan L2 domain, can program the switch device for flooding. The packet may
  220. be sent to the port netdev for processing by the bridge driver. The
  221. bridge should not reflood the packet to the same ports the device flooded,
  222. otherwise there will be duplicate packets on the wire.
  223. To avoid duplicate packets, the device/driver should mark a packet as already
  224. forwarded using skb->offload_fwd_mark. The same mark is set on the device
  225. ports in the domain using dev->offload_fwd_mark. If the skb->offload_fwd_mark
  226. is non-zero and matches the forwarding egress port's dev->skb_mark, the kernel
  227. will drop the skb right before transmit on the egress port, with the
  228. understanding that the device already forwarded the packet on same egress port.
  229. The driver can use switchdev_port_fwd_mark_set() to set a globally unique mark
  230. for port's dev->offload_fwd_mark, based on the port's parent ID (switch ID) and
  231. a group ifindex.
  232. It is possible for the switch device to not handle flooding and push the
  233. packets up to the bridge driver for flooding. This is not ideal as the number
  234. of ports scale in the L2 domain as the device is much more efficient at
  235. flooding packets that software.
  236. If supported by the device, flood control can be offloaded to it, preventing
  237. certain netdevs from flooding unicast traffic for which there is no FDB entry.
  238. IGMP Snooping
  239. ^^^^^^^^^^^^^
  240. XXX: complete this section
  241. L3 Routing Offload
  242. ------------------
  243. Offloading L3 routing requires that device be programmed with FIB entries from
  244. the kernel, with the device doing the FIB lookup and forwarding. The device
  245. does a longest prefix match (LPM) on FIB entries matching route prefix and
  246. forwards the packet to the matching FIB entry's nexthop(s) egress ports.
  247. To program the device, the driver implements support for
  248. SWITCHDEV_OBJ_IPV[4|6]_FIB object using switchdev_port_obj_xxx ops.
  249. switchdev_port_obj_add is used for both adding a new FIB entry to the device,
  250. or modifying an existing entry on the device.
  251. XXX: Currently, only SWITCHDEV_OBJ_ID_IPV4_FIB objects are supported.
  252. SWITCHDEV_OBJ_ID_IPV4_FIB object passes:
  253. struct switchdev_obj_ipv4_fib { /* IPV4_FIB */
  254. u32 dst;
  255. int dst_len;
  256. struct fib_info *fi;
  257. u8 tos;
  258. u8 type;
  259. u32 nlflags;
  260. u32 tb_id;
  261. } ipv4_fib;
  262. to add/modify/delete IPv4 dst/dest_len prefix on table tb_id. The *fi
  263. structure holds details on the route and route's nexthops. *dev is one of the
  264. port netdevs mentioned in the routes next hop list. If the output port netdevs
  265. referenced in the route's nexthop list don't all have the same switch ID, the
  266. driver is not called to add/modify/delete the FIB entry.
  267. Routes offloaded to the device are labeled with "offload" in the ip route
  268. listing:
  269. $ ip route show
  270. default via 192.168.0.2 dev eth0
  271. 11.0.0.0/30 dev sw1p1 proto kernel scope link src 11.0.0.2 offload
  272. 11.0.0.4/30 via 11.0.0.1 dev sw1p1 proto zebra metric 20 offload
  273. 11.0.0.8/30 dev sw1p2 proto kernel scope link src 11.0.0.10 offload
  274. 11.0.0.12/30 via 11.0.0.9 dev sw1p2 proto zebra metric 20 offload
  275. 12.0.0.2 proto zebra metric 30 offload
  276. nexthop via 11.0.0.1 dev sw1p1 weight 1
  277. nexthop via 11.0.0.9 dev sw1p2 weight 1
  278. 12.0.0.3 via 11.0.0.1 dev sw1p1 proto zebra metric 20 offload
  279. 12.0.0.4 via 11.0.0.9 dev sw1p2 proto zebra metric 20 offload
  280. 192.168.0.0/24 dev eth0 proto kernel scope link src 192.168.0.15
  281. XXX: add/mod/del IPv6 FIB API
  282. Nexthop Resolution
  283. ^^^^^^^^^^^^^^^^^^
  284. The FIB entry's nexthop list contains the nexthop tuple (gateway, dev), but for
  285. the switch device to forward the packet with the correct dst mac address, the
  286. nexthop gateways must be resolved to the neighbor's mac address. Neighbor mac
  287. address discovery comes via the ARP (or ND) process and is available via the
  288. arp_tbl neighbor table. To resolve the routes nexthop gateways, the driver
  289. should trigger the kernel's neighbor resolution process. See the rocker
  290. driver's rocker_port_ipv4_resolve() for an example.
  291. The driver can monitor for updates to arp_tbl using the netevent notifier
  292. NETEVENT_NEIGH_UPDATE. The device can be programmed with resolved nexthops
  293. for the routes as arp_tbl updates. The driver implements ndo_neigh_destroy
  294. to know when arp_tbl neighbor entries are purged from the port.
  295. Transaction item queue
  296. ^^^^^^^^^^^^^^^^^^^^^^
  297. For switchdev ops attr_set and obj_add, there is a 2 phase transaction model
  298. used. First phase is to "prepare" anything needed, including various checks,
  299. memory allocation, etc. The goal is to handle the stuff that is not unlikely
  300. to fail here. The second phase is to "commit" the actual changes.
  301. Switchdev provides an inftrastructure for sharing items (for example memory
  302. allocations) between the two phases.
  303. The object created by a driver in "prepare" phase and it is queued up by:
  304. switchdev_trans_item_enqueue()
  305. During the "commit" phase, the driver gets the object by:
  306. switchdev_trans_item_dequeue()
  307. If a transaction is aborted during "prepare" phase, switchdev code will handle
  308. cleanup of the queued-up objects.