switchdev.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  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 <jiri@resnulli.us>
  5. * Copyright (c) 2014-2015 Scott Feldman <sfeldma@gmail.com>
  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. struct switchdev_brport {
  176. struct net_device *dev;
  177. const void *ctx;
  178. struct notifier_block *atomic_nb;
  179. struct notifier_block *blocking_nb;
  180. bool tx_fwd_offload;
  181. };
  182. enum switchdev_notifier_type {
  183. SWITCHDEV_FDB_ADD_TO_BRIDGE = 1,
  184. SWITCHDEV_FDB_DEL_TO_BRIDGE,
  185. SWITCHDEV_FDB_ADD_TO_DEVICE,
  186. SWITCHDEV_FDB_DEL_TO_DEVICE,
  187. SWITCHDEV_FDB_OFFLOADED,
  188. SWITCHDEV_FDB_FLUSH_TO_BRIDGE,
  189. SWITCHDEV_PORT_OBJ_ADD, /* Blocking. */
  190. SWITCHDEV_PORT_OBJ_DEL, /* Blocking. */
  191. SWITCHDEV_PORT_ATTR_SET, /* May be blocking . */
  192. SWITCHDEV_VXLAN_FDB_ADD_TO_BRIDGE,
  193. SWITCHDEV_VXLAN_FDB_DEL_TO_BRIDGE,
  194. SWITCHDEV_VXLAN_FDB_ADD_TO_DEVICE,
  195. SWITCHDEV_VXLAN_FDB_DEL_TO_DEVICE,
  196. SWITCHDEV_VXLAN_FDB_OFFLOADED,
  197. SWITCHDEV_BRPORT_OFFLOADED,
  198. SWITCHDEV_BRPORT_UNOFFLOADED,
  199. SWITCHDEV_BRPORT_REPLAY,
  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. locked:1,
  216. offloaded:1;
  217. };
  218. struct switchdev_notifier_port_obj_info {
  219. struct switchdev_notifier_info info; /* must be first */
  220. const struct switchdev_obj *obj;
  221. bool handled;
  222. };
  223. struct switchdev_notifier_port_attr_info {
  224. struct switchdev_notifier_info info; /* must be first */
  225. const struct switchdev_attr *attr;
  226. bool handled;
  227. };
  228. struct switchdev_notifier_brport_info {
  229. struct switchdev_notifier_info info; /* must be first */
  230. const struct switchdev_brport brport;
  231. };
  232. static inline struct net_device *
  233. switchdev_notifier_info_to_dev(const struct switchdev_notifier_info *info)
  234. {
  235. return info->dev;
  236. }
  237. static inline struct netlink_ext_ack *
  238. switchdev_notifier_info_to_extack(const struct switchdev_notifier_info *info)
  239. {
  240. return info->extack;
  241. }
  242. static inline bool
  243. switchdev_fdb_is_dynamically_learned(const struct switchdev_notifier_fdb_info *fdb_info)
  244. {
  245. return !fdb_info->added_by_user && !fdb_info->is_local;
  246. }
  247. #ifdef CONFIG_NET_SWITCHDEV
  248. int switchdev_bridge_port_offload(struct net_device *brport_dev,
  249. struct net_device *dev, const void *ctx,
  250. struct notifier_block *atomic_nb,
  251. struct notifier_block *blocking_nb,
  252. bool tx_fwd_offload,
  253. struct netlink_ext_ack *extack);
  254. void switchdev_bridge_port_unoffload(struct net_device *brport_dev,
  255. const void *ctx,
  256. struct notifier_block *atomic_nb,
  257. struct notifier_block *blocking_nb);
  258. int switchdev_bridge_port_replay(struct net_device *brport_dev,
  259. struct net_device *dev, const void *ctx,
  260. struct notifier_block *atomic_nb,
  261. struct notifier_block *blocking_nb,
  262. struct netlink_ext_ack *extack);
  263. void switchdev_deferred_process(void);
  264. int switchdev_port_attr_set(struct net_device *dev,
  265. const struct switchdev_attr *attr,
  266. struct netlink_ext_ack *extack);
  267. bool switchdev_port_obj_act_is_deferred(struct net_device *dev,
  268. enum switchdev_notifier_type nt,
  269. const struct switchdev_obj *obj);
  270. int switchdev_port_obj_add(struct net_device *dev,
  271. const struct switchdev_obj *obj,
  272. struct netlink_ext_ack *extack);
  273. int switchdev_port_obj_del(struct net_device *dev,
  274. const struct switchdev_obj *obj);
  275. int register_switchdev_notifier(struct notifier_block *nb);
  276. int unregister_switchdev_notifier(struct notifier_block *nb);
  277. int call_switchdev_notifiers(unsigned long val, struct net_device *dev,
  278. struct switchdev_notifier_info *info,
  279. struct netlink_ext_ack *extack);
  280. int register_switchdev_blocking_notifier(struct notifier_block *nb);
  281. int unregister_switchdev_blocking_notifier(struct notifier_block *nb);
  282. int call_switchdev_blocking_notifiers(unsigned long val, struct net_device *dev,
  283. struct switchdev_notifier_info *info,
  284. struct netlink_ext_ack *extack);
  285. int switchdev_handle_fdb_event_to_device(struct net_device *dev, unsigned long event,
  286. const struct switchdev_notifier_fdb_info *fdb_info,
  287. bool (*check_cb)(const struct net_device *dev),
  288. bool (*foreign_dev_check_cb)(const struct net_device *dev,
  289. const struct net_device *foreign_dev),
  290. int (*mod_cb)(struct net_device *dev, struct net_device *orig_dev,
  291. unsigned long event, const void *ctx,
  292. const struct switchdev_notifier_fdb_info *fdb_info));
  293. int switchdev_handle_port_obj_add(struct net_device *dev,
  294. struct switchdev_notifier_port_obj_info *port_obj_info,
  295. bool (*check_cb)(const struct net_device *dev),
  296. int (*add_cb)(struct net_device *dev, const void *ctx,
  297. const struct switchdev_obj *obj,
  298. struct netlink_ext_ack *extack));
  299. int switchdev_handle_port_obj_add_foreign(struct net_device *dev,
  300. struct switchdev_notifier_port_obj_info *port_obj_info,
  301. bool (*check_cb)(const struct net_device *dev),
  302. bool (*foreign_dev_check_cb)(const struct net_device *dev,
  303. const struct net_device *foreign_dev),
  304. int (*add_cb)(struct net_device *dev, const void *ctx,
  305. const struct switchdev_obj *obj,
  306. struct netlink_ext_ack *extack));
  307. int switchdev_handle_port_obj_del(struct net_device *dev,
  308. struct switchdev_notifier_port_obj_info *port_obj_info,
  309. bool (*check_cb)(const struct net_device *dev),
  310. int (*del_cb)(struct net_device *dev, const void *ctx,
  311. const struct switchdev_obj *obj));
  312. int switchdev_handle_port_obj_del_foreign(struct net_device *dev,
  313. struct switchdev_notifier_port_obj_info *port_obj_info,
  314. bool (*check_cb)(const struct net_device *dev),
  315. bool (*foreign_dev_check_cb)(const struct net_device *dev,
  316. const struct net_device *foreign_dev),
  317. int (*del_cb)(struct net_device *dev, const void *ctx,
  318. const struct switchdev_obj *obj));
  319. int switchdev_handle_port_attr_set(struct net_device *dev,
  320. struct switchdev_notifier_port_attr_info *port_attr_info,
  321. bool (*check_cb)(const struct net_device *dev),
  322. int (*set_cb)(struct net_device *dev, const void *ctx,
  323. const struct switchdev_attr *attr,
  324. struct netlink_ext_ack *extack));
  325. #else
  326. static inline int
  327. switchdev_bridge_port_offload(struct net_device *brport_dev,
  328. struct net_device *dev, const void *ctx,
  329. struct notifier_block *atomic_nb,
  330. struct notifier_block *blocking_nb,
  331. bool tx_fwd_offload,
  332. struct netlink_ext_ack *extack)
  333. {
  334. return -EOPNOTSUPP;
  335. }
  336. static inline void
  337. switchdev_bridge_port_unoffload(struct net_device *brport_dev,
  338. const void *ctx,
  339. struct notifier_block *atomic_nb,
  340. struct notifier_block *blocking_nb)
  341. {
  342. }
  343. static inline void switchdev_deferred_process(void)
  344. {
  345. }
  346. static inline int switchdev_port_attr_set(struct net_device *dev,
  347. const struct switchdev_attr *attr,
  348. struct netlink_ext_ack *extack)
  349. {
  350. return -EOPNOTSUPP;
  351. }
  352. static inline int switchdev_port_obj_add(struct net_device *dev,
  353. const struct switchdev_obj *obj,
  354. struct netlink_ext_ack *extack)
  355. {
  356. return -EOPNOTSUPP;
  357. }
  358. static inline int switchdev_port_obj_del(struct net_device *dev,
  359. const struct switchdev_obj *obj)
  360. {
  361. return -EOPNOTSUPP;
  362. }
  363. static inline int register_switchdev_notifier(struct notifier_block *nb)
  364. {
  365. return 0;
  366. }
  367. static inline int unregister_switchdev_notifier(struct notifier_block *nb)
  368. {
  369. return 0;
  370. }
  371. static inline int call_switchdev_notifiers(unsigned long val,
  372. struct net_device *dev,
  373. struct switchdev_notifier_info *info,
  374. struct netlink_ext_ack *extack)
  375. {
  376. return NOTIFY_DONE;
  377. }
  378. static inline int
  379. register_switchdev_blocking_notifier(struct notifier_block *nb)
  380. {
  381. return 0;
  382. }
  383. static inline int
  384. unregister_switchdev_blocking_notifier(struct notifier_block *nb)
  385. {
  386. return 0;
  387. }
  388. static inline int
  389. call_switchdev_blocking_notifiers(unsigned long val,
  390. struct net_device *dev,
  391. struct switchdev_notifier_info *info,
  392. struct netlink_ext_ack *extack)
  393. {
  394. return NOTIFY_DONE;
  395. }
  396. static inline int
  397. switchdev_handle_fdb_event_to_device(struct net_device *dev, unsigned long event,
  398. const struct switchdev_notifier_fdb_info *fdb_info,
  399. bool (*check_cb)(const struct net_device *dev),
  400. bool (*foreign_dev_check_cb)(const struct net_device *dev,
  401. const struct net_device *foreign_dev),
  402. int (*mod_cb)(struct net_device *dev, struct net_device *orig_dev,
  403. unsigned long event, const void *ctx,
  404. const struct switchdev_notifier_fdb_info *fdb_info))
  405. {
  406. return 0;
  407. }
  408. static inline int
  409. switchdev_handle_port_obj_add(struct net_device *dev,
  410. struct switchdev_notifier_port_obj_info *port_obj_info,
  411. bool (*check_cb)(const struct net_device *dev),
  412. int (*add_cb)(struct net_device *dev, const void *ctx,
  413. const struct switchdev_obj *obj,
  414. struct netlink_ext_ack *extack))
  415. {
  416. return 0;
  417. }
  418. static inline int switchdev_handle_port_obj_add_foreign(struct net_device *dev,
  419. struct switchdev_notifier_port_obj_info *port_obj_info,
  420. bool (*check_cb)(const struct net_device *dev),
  421. bool (*foreign_dev_check_cb)(const struct net_device *dev,
  422. const struct net_device *foreign_dev),
  423. int (*add_cb)(struct net_device *dev, const void *ctx,
  424. const struct switchdev_obj *obj,
  425. struct netlink_ext_ack *extack))
  426. {
  427. return 0;
  428. }
  429. static inline int
  430. switchdev_handle_port_obj_del(struct net_device *dev,
  431. struct switchdev_notifier_port_obj_info *port_obj_info,
  432. bool (*check_cb)(const struct net_device *dev),
  433. int (*del_cb)(struct net_device *dev, const void *ctx,
  434. const struct switchdev_obj *obj))
  435. {
  436. return 0;
  437. }
  438. static inline int
  439. switchdev_handle_port_obj_del_foreign(struct net_device *dev,
  440. struct switchdev_notifier_port_obj_info *port_obj_info,
  441. bool (*check_cb)(const struct net_device *dev),
  442. bool (*foreign_dev_check_cb)(const struct net_device *dev,
  443. const struct net_device *foreign_dev),
  444. int (*del_cb)(struct net_device *dev, const void *ctx,
  445. const struct switchdev_obj *obj))
  446. {
  447. return 0;
  448. }
  449. static inline int
  450. switchdev_handle_port_attr_set(struct net_device *dev,
  451. struct switchdev_notifier_port_attr_info *port_attr_info,
  452. bool (*check_cb)(const struct net_device *dev),
  453. int (*set_cb)(struct net_device *dev, const void *ctx,
  454. const struct switchdev_attr *attr,
  455. struct netlink_ext_ack *extack))
  456. {
  457. return 0;
  458. }
  459. #endif
  460. #endif /* _LINUX_SWITCHDEV_H_ */