switchdev.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * include/net/switchdev.h - Switch device API
  4. * Copyright (c) 2014-2015 Jiri Pirko <[email protected]>
  5. * Copyright (c) 2014-2015 Scott Feldman <[email protected]>
  6. */
  7. #ifndef _LINUX_SWITCHDEV_H_
  8. #define _LINUX_SWITCHDEV_H_
  9. #include <linux/netdevice.h>
  10. #include <linux/notifier.h>
  11. #include <linux/list.h>
  12. #include <net/ip_fib.h>
  13. #define SWITCHDEV_F_NO_RECURSE BIT(0)
  14. #define SWITCHDEV_F_SKIP_EOPNOTSUPP BIT(1)
  15. #define SWITCHDEV_F_DEFER BIT(2)
  16. enum switchdev_attr_id {
  17. SWITCHDEV_ATTR_ID_UNDEFINED,
  18. SWITCHDEV_ATTR_ID_PORT_STP_STATE,
  19. SWITCHDEV_ATTR_ID_PORT_MST_STATE,
  20. SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS,
  21. SWITCHDEV_ATTR_ID_PORT_PRE_BRIDGE_FLAGS,
  22. SWITCHDEV_ATTR_ID_PORT_MROUTER,
  23. SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME,
  24. SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING,
  25. SWITCHDEV_ATTR_ID_BRIDGE_VLAN_PROTOCOL,
  26. SWITCHDEV_ATTR_ID_BRIDGE_MC_DISABLED,
  27. SWITCHDEV_ATTR_ID_BRIDGE_MROUTER,
  28. SWITCHDEV_ATTR_ID_BRIDGE_MST,
  29. SWITCHDEV_ATTR_ID_MRP_PORT_ROLE,
  30. SWITCHDEV_ATTR_ID_VLAN_MSTI,
  31. };
  32. struct switchdev_mst_state {
  33. u16 msti;
  34. u8 state;
  35. };
  36. struct switchdev_brport_flags {
  37. unsigned long val;
  38. unsigned long mask;
  39. };
  40. struct switchdev_vlan_msti {
  41. u16 vid;
  42. u16 msti;
  43. };
  44. struct switchdev_attr {
  45. struct net_device *orig_dev;
  46. enum switchdev_attr_id id;
  47. u32 flags;
  48. void *complete_priv;
  49. void (*complete)(struct net_device *dev, int err, void *priv);
  50. union {
  51. u8 stp_state; /* PORT_STP_STATE */
  52. struct switchdev_mst_state mst_state; /* PORT_MST_STATE */
  53. struct switchdev_brport_flags brport_flags; /* PORT_BRIDGE_FLAGS */
  54. bool mrouter; /* PORT_MROUTER */
  55. clock_t ageing_time; /* BRIDGE_AGEING_TIME */
  56. bool vlan_filtering; /* BRIDGE_VLAN_FILTERING */
  57. u16 vlan_protocol; /* BRIDGE_VLAN_PROTOCOL */
  58. bool mst; /* BRIDGE_MST */
  59. bool mc_disabled; /* MC_DISABLED */
  60. u8 mrp_port_role; /* MRP_PORT_ROLE */
  61. struct switchdev_vlan_msti vlan_msti; /* VLAN_MSTI */
  62. } u;
  63. };
  64. enum switchdev_obj_id {
  65. SWITCHDEV_OBJ_ID_UNDEFINED,
  66. SWITCHDEV_OBJ_ID_PORT_VLAN,
  67. SWITCHDEV_OBJ_ID_PORT_MDB,
  68. SWITCHDEV_OBJ_ID_HOST_MDB,
  69. SWITCHDEV_OBJ_ID_MRP,
  70. SWITCHDEV_OBJ_ID_RING_TEST_MRP,
  71. SWITCHDEV_OBJ_ID_RING_ROLE_MRP,
  72. SWITCHDEV_OBJ_ID_RING_STATE_MRP,
  73. SWITCHDEV_OBJ_ID_IN_TEST_MRP,
  74. SWITCHDEV_OBJ_ID_IN_ROLE_MRP,
  75. SWITCHDEV_OBJ_ID_IN_STATE_MRP,
  76. };
  77. struct switchdev_obj {
  78. struct list_head list;
  79. struct net_device *orig_dev;
  80. enum switchdev_obj_id id;
  81. u32 flags;
  82. void *complete_priv;
  83. void (*complete)(struct net_device *dev, int err, void *priv);
  84. };
  85. /* SWITCHDEV_OBJ_ID_PORT_VLAN */
  86. struct switchdev_obj_port_vlan {
  87. struct switchdev_obj obj;
  88. u16 flags;
  89. u16 vid;
  90. /* If set, the notifier signifies a change of one of the following
  91. * flags for a VLAN that already exists:
  92. * - BRIDGE_VLAN_INFO_PVID
  93. * - BRIDGE_VLAN_INFO_UNTAGGED
  94. * Entries with BRIDGE_VLAN_INFO_BRENTRY unset are not notified at all.
  95. */
  96. bool changed;
  97. };
  98. #define SWITCHDEV_OBJ_PORT_VLAN(OBJ) \
  99. container_of((OBJ), struct switchdev_obj_port_vlan, obj)
  100. /* SWITCHDEV_OBJ_ID_PORT_MDB */
  101. struct switchdev_obj_port_mdb {
  102. struct switchdev_obj obj;
  103. unsigned char addr[ETH_ALEN];
  104. u16 vid;
  105. };
  106. #define SWITCHDEV_OBJ_PORT_MDB(OBJ) \
  107. container_of((OBJ), struct switchdev_obj_port_mdb, obj)
  108. /* SWITCHDEV_OBJ_ID_MRP */
  109. struct switchdev_obj_mrp {
  110. struct switchdev_obj obj;
  111. struct net_device *p_port;
  112. struct net_device *s_port;
  113. u32 ring_id;
  114. u16 prio;
  115. };
  116. #define SWITCHDEV_OBJ_MRP(OBJ) \
  117. container_of((OBJ), struct switchdev_obj_mrp, obj)
  118. /* SWITCHDEV_OBJ_ID_RING_TEST_MRP */
  119. struct switchdev_obj_ring_test_mrp {
  120. struct switchdev_obj obj;
  121. /* The value is in us and a value of 0 represents to stop */
  122. u32 interval;
  123. u8 max_miss;
  124. u32 ring_id;
  125. u32 period;
  126. bool monitor;
  127. };
  128. #define SWITCHDEV_OBJ_RING_TEST_MRP(OBJ) \
  129. container_of((OBJ), struct switchdev_obj_ring_test_mrp, obj)
  130. /* SWICHDEV_OBJ_ID_RING_ROLE_MRP */
  131. struct switchdev_obj_ring_role_mrp {
  132. struct switchdev_obj obj;
  133. u8 ring_role;
  134. u32 ring_id;
  135. u8 sw_backup;
  136. };
  137. #define SWITCHDEV_OBJ_RING_ROLE_MRP(OBJ) \
  138. container_of((OBJ), struct switchdev_obj_ring_role_mrp, obj)
  139. struct switchdev_obj_ring_state_mrp {
  140. struct switchdev_obj obj;
  141. u8 ring_state;
  142. u32 ring_id;
  143. };
  144. #define SWITCHDEV_OBJ_RING_STATE_MRP(OBJ) \
  145. container_of((OBJ), struct switchdev_obj_ring_state_mrp, obj)
  146. /* SWITCHDEV_OBJ_ID_IN_TEST_MRP */
  147. struct switchdev_obj_in_test_mrp {
  148. struct switchdev_obj obj;
  149. /* The value is in us and a value of 0 represents to stop */
  150. u32 interval;
  151. u32 in_id;
  152. u32 period;
  153. u8 max_miss;
  154. };
  155. #define SWITCHDEV_OBJ_IN_TEST_MRP(OBJ) \
  156. container_of((OBJ), struct switchdev_obj_in_test_mrp, obj)
  157. /* SWICHDEV_OBJ_ID_IN_ROLE_MRP */
  158. struct switchdev_obj_in_role_mrp {
  159. struct switchdev_obj obj;
  160. struct net_device *i_port;
  161. u32 ring_id;
  162. u16 in_id;
  163. u8 in_role;
  164. u8 sw_backup;
  165. };
  166. #define SWITCHDEV_OBJ_IN_ROLE_MRP(OBJ) \
  167. container_of((OBJ), struct switchdev_obj_in_role_mrp, obj)
  168. struct switchdev_obj_in_state_mrp {
  169. struct switchdev_obj obj;
  170. u32 in_id;
  171. u8 in_state;
  172. };
  173. #define SWITCHDEV_OBJ_IN_STATE_MRP(OBJ) \
  174. container_of((OBJ), struct switchdev_obj_in_state_mrp, obj)
  175. typedef int switchdev_obj_dump_cb_t(struct switchdev_obj *obj);
  176. struct switchdev_brport {
  177. struct net_device *dev;
  178. const void *ctx;
  179. struct notifier_block *atomic_nb;
  180. struct notifier_block *blocking_nb;
  181. bool tx_fwd_offload;
  182. };
  183. enum switchdev_notifier_type {
  184. SWITCHDEV_FDB_ADD_TO_BRIDGE = 1,
  185. SWITCHDEV_FDB_DEL_TO_BRIDGE,
  186. SWITCHDEV_FDB_ADD_TO_DEVICE,
  187. SWITCHDEV_FDB_DEL_TO_DEVICE,
  188. SWITCHDEV_FDB_OFFLOADED,
  189. SWITCHDEV_FDB_FLUSH_TO_BRIDGE,
  190. SWITCHDEV_PORT_OBJ_ADD, /* Blocking. */
  191. SWITCHDEV_PORT_OBJ_DEL, /* Blocking. */
  192. SWITCHDEV_PORT_ATTR_SET, /* May be blocking . */
  193. SWITCHDEV_VXLAN_FDB_ADD_TO_BRIDGE,
  194. SWITCHDEV_VXLAN_FDB_DEL_TO_BRIDGE,
  195. SWITCHDEV_VXLAN_FDB_ADD_TO_DEVICE,
  196. SWITCHDEV_VXLAN_FDB_DEL_TO_DEVICE,
  197. SWITCHDEV_VXLAN_FDB_OFFLOADED,
  198. SWITCHDEV_BRPORT_OFFLOADED,
  199. SWITCHDEV_BRPORT_UNOFFLOADED,
  200. };
  201. struct switchdev_notifier_info {
  202. struct net_device *dev;
  203. struct netlink_ext_ack *extack;
  204. const void *ctx;
  205. };
  206. /* Remember to update br_switchdev_fdb_populate() when adding
  207. * new members to this structure
  208. */
  209. struct switchdev_notifier_fdb_info {
  210. struct switchdev_notifier_info info; /* must be first */
  211. const unsigned char *addr;
  212. u16 vid;
  213. u8 added_by_user:1,
  214. is_local:1,
  215. offloaded:1;
  216. };
  217. struct switchdev_notifier_port_obj_info {
  218. struct switchdev_notifier_info info; /* must be first */
  219. const struct switchdev_obj *obj;
  220. bool handled;
  221. };
  222. struct switchdev_notifier_port_attr_info {
  223. struct switchdev_notifier_info info; /* must be first */
  224. const struct switchdev_attr *attr;
  225. bool handled;
  226. };
  227. struct switchdev_notifier_brport_info {
  228. struct switchdev_notifier_info info; /* must be first */
  229. const struct switchdev_brport brport;
  230. };
  231. static inline struct net_device *
  232. switchdev_notifier_info_to_dev(const struct switchdev_notifier_info *info)
  233. {
  234. return info->dev;
  235. }
  236. static inline struct netlink_ext_ack *
  237. switchdev_notifier_info_to_extack(const struct switchdev_notifier_info *info)
  238. {
  239. return info->extack;
  240. }
  241. static inline bool
  242. switchdev_fdb_is_dynamically_learned(const struct switchdev_notifier_fdb_info *fdb_info)
  243. {
  244. return !fdb_info->added_by_user && !fdb_info->is_local;
  245. }
  246. #ifdef CONFIG_NET_SWITCHDEV
  247. int switchdev_bridge_port_offload(struct net_device *brport_dev,
  248. struct net_device *dev, const void *ctx,
  249. struct notifier_block *atomic_nb,
  250. struct notifier_block *blocking_nb,
  251. bool tx_fwd_offload,
  252. struct netlink_ext_ack *extack);
  253. void switchdev_bridge_port_unoffload(struct net_device *brport_dev,
  254. const void *ctx,
  255. struct notifier_block *atomic_nb,
  256. struct notifier_block *blocking_nb);
  257. void switchdev_deferred_process(void);
  258. int switchdev_port_attr_set(struct net_device *dev,
  259. const struct switchdev_attr *attr,
  260. struct netlink_ext_ack *extack);
  261. int switchdev_port_obj_add(struct net_device *dev,
  262. const struct switchdev_obj *obj,
  263. struct netlink_ext_ack *extack);
  264. int switchdev_port_obj_del(struct net_device *dev,
  265. const struct switchdev_obj *obj);
  266. int register_switchdev_notifier(struct notifier_block *nb);
  267. int unregister_switchdev_notifier(struct notifier_block *nb);
  268. int call_switchdev_notifiers(unsigned long val, struct net_device *dev,
  269. struct switchdev_notifier_info *info,
  270. struct netlink_ext_ack *extack);
  271. int register_switchdev_blocking_notifier(struct notifier_block *nb);
  272. int unregister_switchdev_blocking_notifier(struct notifier_block *nb);
  273. int call_switchdev_blocking_notifiers(unsigned long val, struct net_device *dev,
  274. struct switchdev_notifier_info *info,
  275. struct netlink_ext_ack *extack);
  276. void switchdev_port_fwd_mark_set(struct net_device *dev,
  277. struct net_device *group_dev,
  278. bool joining);
  279. int switchdev_handle_fdb_event_to_device(struct net_device *dev, unsigned long event,
  280. const struct switchdev_notifier_fdb_info *fdb_info,
  281. bool (*check_cb)(const struct net_device *dev),
  282. bool (*foreign_dev_check_cb)(const struct net_device *dev,
  283. const struct net_device *foreign_dev),
  284. int (*mod_cb)(struct net_device *dev, struct net_device *orig_dev,
  285. unsigned long event, const void *ctx,
  286. const struct switchdev_notifier_fdb_info *fdb_info));
  287. int switchdev_handle_port_obj_add(struct net_device *dev,
  288. struct switchdev_notifier_port_obj_info *port_obj_info,
  289. bool (*check_cb)(const struct net_device *dev),
  290. int (*add_cb)(struct net_device *dev, const void *ctx,
  291. const struct switchdev_obj *obj,
  292. struct netlink_ext_ack *extack));
  293. int switchdev_handle_port_obj_add_foreign(struct net_device *dev,
  294. struct switchdev_notifier_port_obj_info *port_obj_info,
  295. bool (*check_cb)(const struct net_device *dev),
  296. bool (*foreign_dev_check_cb)(const struct net_device *dev,
  297. const struct net_device *foreign_dev),
  298. int (*add_cb)(struct net_device *dev, const void *ctx,
  299. const struct switchdev_obj *obj,
  300. struct netlink_ext_ack *extack));
  301. int switchdev_handle_port_obj_del(struct net_device *dev,
  302. struct switchdev_notifier_port_obj_info *port_obj_info,
  303. bool (*check_cb)(const struct net_device *dev),
  304. int (*del_cb)(struct net_device *dev, const void *ctx,
  305. const struct switchdev_obj *obj));
  306. int switchdev_handle_port_obj_del_foreign(struct net_device *dev,
  307. struct switchdev_notifier_port_obj_info *port_obj_info,
  308. bool (*check_cb)(const struct net_device *dev),
  309. bool (*foreign_dev_check_cb)(const struct net_device *dev,
  310. const struct net_device *foreign_dev),
  311. int (*del_cb)(struct net_device *dev, const void *ctx,
  312. const struct switchdev_obj *obj));
  313. int switchdev_handle_port_attr_set(struct net_device *dev,
  314. struct switchdev_notifier_port_attr_info *port_attr_info,
  315. bool (*check_cb)(const struct net_device *dev),
  316. int (*set_cb)(struct net_device *dev, const void *ctx,
  317. const struct switchdev_attr *attr,
  318. struct netlink_ext_ack *extack));
  319. #else
  320. static inline int
  321. switchdev_bridge_port_offload(struct net_device *brport_dev,
  322. struct net_device *dev, const void *ctx,
  323. struct notifier_block *atomic_nb,
  324. struct notifier_block *blocking_nb,
  325. bool tx_fwd_offload,
  326. struct netlink_ext_ack *extack)
  327. {
  328. return -EOPNOTSUPP;
  329. }
  330. static inline void
  331. switchdev_bridge_port_unoffload(struct net_device *brport_dev,
  332. const void *ctx,
  333. struct notifier_block *atomic_nb,
  334. struct notifier_block *blocking_nb)
  335. {
  336. }
  337. static inline void switchdev_deferred_process(void)
  338. {
  339. }
  340. static inline int switchdev_port_attr_set(struct net_device *dev,
  341. const struct switchdev_attr *attr,
  342. struct netlink_ext_ack *extack)
  343. {
  344. return -EOPNOTSUPP;
  345. }
  346. static inline int switchdev_port_obj_add(struct net_device *dev,
  347. const struct switchdev_obj *obj,
  348. struct netlink_ext_ack *extack)
  349. {
  350. return -EOPNOTSUPP;
  351. }
  352. static inline int switchdev_port_obj_del(struct net_device *dev,
  353. const struct switchdev_obj *obj)
  354. {
  355. return -EOPNOTSUPP;
  356. }
  357. static inline int register_switchdev_notifier(struct notifier_block *nb)
  358. {
  359. return 0;
  360. }
  361. static inline int unregister_switchdev_notifier(struct notifier_block *nb)
  362. {
  363. return 0;
  364. }
  365. static inline int call_switchdev_notifiers(unsigned long val,
  366. struct net_device *dev,
  367. struct switchdev_notifier_info *info,
  368. struct netlink_ext_ack *extack)
  369. {
  370. return NOTIFY_DONE;
  371. }
  372. static inline int
  373. register_switchdev_blocking_notifier(struct notifier_block *nb)
  374. {
  375. return 0;
  376. }
  377. static inline int
  378. unregister_switchdev_blocking_notifier(struct notifier_block *nb)
  379. {
  380. return 0;
  381. }
  382. static inline int
  383. call_switchdev_blocking_notifiers(unsigned long val,
  384. struct net_device *dev,
  385. struct switchdev_notifier_info *info,
  386. struct netlink_ext_ack *extack)
  387. {
  388. return NOTIFY_DONE;
  389. }
  390. static inline int
  391. switchdev_handle_fdb_event_to_device(struct net_device *dev, unsigned long event,
  392. const struct switchdev_notifier_fdb_info *fdb_info,
  393. bool (*check_cb)(const struct net_device *dev),
  394. bool (*foreign_dev_check_cb)(const struct net_device *dev,
  395. const struct net_device *foreign_dev),
  396. int (*mod_cb)(struct net_device *dev, struct net_device *orig_dev,
  397. unsigned long event, const void *ctx,
  398. const struct switchdev_notifier_fdb_info *fdb_info))
  399. {
  400. return 0;
  401. }
  402. static inline int
  403. switchdev_handle_port_obj_add(struct net_device *dev,
  404. struct switchdev_notifier_port_obj_info *port_obj_info,
  405. bool (*check_cb)(const struct net_device *dev),
  406. int (*add_cb)(struct net_device *dev, const void *ctx,
  407. const struct switchdev_obj *obj,
  408. struct netlink_ext_ack *extack))
  409. {
  410. return 0;
  411. }
  412. static inline int switchdev_handle_port_obj_add_foreign(struct net_device *dev,
  413. struct switchdev_notifier_port_obj_info *port_obj_info,
  414. bool (*check_cb)(const struct net_device *dev),
  415. bool (*foreign_dev_check_cb)(const struct net_device *dev,
  416. const struct net_device *foreign_dev),
  417. int (*add_cb)(struct net_device *dev, const void *ctx,
  418. const struct switchdev_obj *obj,
  419. struct netlink_ext_ack *extack))
  420. {
  421. return 0;
  422. }
  423. static inline int
  424. switchdev_handle_port_obj_del(struct net_device *dev,
  425. struct switchdev_notifier_port_obj_info *port_obj_info,
  426. bool (*check_cb)(const struct net_device *dev),
  427. int (*del_cb)(struct net_device *dev, const void *ctx,
  428. const struct switchdev_obj *obj))
  429. {
  430. return 0;
  431. }
  432. static inline int
  433. switchdev_handle_port_obj_del_foreign(struct net_device *dev,
  434. struct switchdev_notifier_port_obj_info *port_obj_info,
  435. bool (*check_cb)(const struct net_device *dev),
  436. bool (*foreign_dev_check_cb)(const struct net_device *dev,
  437. const struct net_device *foreign_dev),
  438. int (*del_cb)(struct net_device *dev, const void *ctx,
  439. const struct switchdev_obj *obj))
  440. {
  441. return 0;
  442. }
  443. static inline int
  444. switchdev_handle_port_attr_set(struct net_device *dev,
  445. struct switchdev_notifier_port_attr_info *port_attr_info,
  446. bool (*check_cb)(const struct net_device *dev),
  447. int (*set_cb)(struct net_device *dev, const void *ctx,
  448. const struct switchdev_attr *attr,
  449. struct netlink_ext_ack *extack))
  450. {
  451. return 0;
  452. }
  453. #endif
  454. #endif /* _LINUX_SWITCHDEV_H_ */