switchdev.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. * include/net/switchdev.h - Switch device API
  3. * Copyright (c) 2014-2015 Jiri Pirko <jiri@resnulli.us>
  4. * Copyright (c) 2014-2015 Scott Feldman <sfeldma@gmail.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. */
  11. #ifndef _LINUX_SWITCHDEV_H_
  12. #define _LINUX_SWITCHDEV_H_
  13. #include <linux/netdevice.h>
  14. #include <linux/notifier.h>
  15. #include <linux/list.h>
  16. #include <net/ip_fib.h>
  17. #define SWITCHDEV_F_NO_RECURSE BIT(0)
  18. #define SWITCHDEV_F_SKIP_EOPNOTSUPP BIT(1)
  19. #define SWITCHDEV_F_DEFER BIT(2)
  20. struct switchdev_trans_item {
  21. struct list_head list;
  22. void *data;
  23. void (*destructor)(const void *data);
  24. };
  25. struct switchdev_trans {
  26. struct list_head item_list;
  27. bool ph_prepare;
  28. };
  29. static inline bool switchdev_trans_ph_prepare(struct switchdev_trans *trans)
  30. {
  31. return trans && trans->ph_prepare;
  32. }
  33. static inline bool switchdev_trans_ph_commit(struct switchdev_trans *trans)
  34. {
  35. return trans && !trans->ph_prepare;
  36. }
  37. enum switchdev_attr_id {
  38. SWITCHDEV_ATTR_ID_UNDEFINED,
  39. SWITCHDEV_ATTR_ID_PORT_PARENT_ID,
  40. SWITCHDEV_ATTR_ID_PORT_STP_STATE,
  41. SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS,
  42. SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS_SUPPORT,
  43. SWITCHDEV_ATTR_ID_PORT_MROUTER,
  44. SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME,
  45. SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING,
  46. SWITCHDEV_ATTR_ID_BRIDGE_MC_DISABLED,
  47. SWITCHDEV_ATTR_ID_BRIDGE_MROUTER,
  48. };
  49. struct switchdev_attr {
  50. struct net_device *orig_dev;
  51. enum switchdev_attr_id id;
  52. u32 flags;
  53. void *complete_priv;
  54. void (*complete)(struct net_device *dev, int err, void *priv);
  55. union {
  56. struct netdev_phys_item_id ppid; /* PORT_PARENT_ID */
  57. u8 stp_state; /* PORT_STP_STATE */
  58. unsigned long brport_flags; /* PORT_BRIDGE_FLAGS */
  59. unsigned long brport_flags_support; /* PORT_BRIDGE_FLAGS_SUPPORT */
  60. bool mrouter; /* PORT_MROUTER */
  61. clock_t ageing_time; /* BRIDGE_AGEING_TIME */
  62. bool vlan_filtering; /* BRIDGE_VLAN_FILTERING */
  63. bool mc_disabled; /* MC_DISABLED */
  64. } u;
  65. };
  66. enum switchdev_obj_id {
  67. SWITCHDEV_OBJ_ID_UNDEFINED,
  68. SWITCHDEV_OBJ_ID_PORT_VLAN,
  69. SWITCHDEV_OBJ_ID_PORT_MDB,
  70. SWITCHDEV_OBJ_ID_HOST_MDB,
  71. };
  72. struct switchdev_obj {
  73. struct net_device *orig_dev;
  74. enum switchdev_obj_id id;
  75. u32 flags;
  76. void *complete_priv;
  77. void (*complete)(struct net_device *dev, int err, void *priv);
  78. };
  79. /* SWITCHDEV_OBJ_ID_PORT_VLAN */
  80. struct switchdev_obj_port_vlan {
  81. struct switchdev_obj obj;
  82. u16 flags;
  83. u16 vid_begin;
  84. u16 vid_end;
  85. };
  86. #define SWITCHDEV_OBJ_PORT_VLAN(obj) \
  87. container_of(obj, struct switchdev_obj_port_vlan, obj)
  88. /* SWITCHDEV_OBJ_ID_PORT_MDB */
  89. struct switchdev_obj_port_mdb {
  90. struct switchdev_obj obj;
  91. unsigned char addr[ETH_ALEN];
  92. u16 vid;
  93. };
  94. #define SWITCHDEV_OBJ_PORT_MDB(obj) \
  95. container_of(obj, struct switchdev_obj_port_mdb, obj)
  96. void switchdev_trans_item_enqueue(struct switchdev_trans *trans,
  97. void *data, void (*destructor)(void const *),
  98. struct switchdev_trans_item *tritem);
  99. void *switchdev_trans_item_dequeue(struct switchdev_trans *trans);
  100. typedef int switchdev_obj_dump_cb_t(struct switchdev_obj *obj);
  101. /**
  102. * struct switchdev_ops - switchdev operations
  103. *
  104. * @switchdev_port_attr_get: Get a port attribute (see switchdev_attr).
  105. *
  106. * @switchdev_port_attr_set: Set a port attribute (see switchdev_attr).
  107. *
  108. * @switchdev_port_obj_add: Add an object to port (see switchdev_obj_*).
  109. *
  110. * @switchdev_port_obj_del: Delete an object from port (see switchdev_obj_*).
  111. */
  112. struct switchdev_ops {
  113. int (*switchdev_port_attr_get)(struct net_device *dev,
  114. struct switchdev_attr *attr);
  115. int (*switchdev_port_attr_set)(struct net_device *dev,
  116. const struct switchdev_attr *attr,
  117. struct switchdev_trans *trans);
  118. int (*switchdev_port_obj_add)(struct net_device *dev,
  119. const struct switchdev_obj *obj,
  120. struct switchdev_trans *trans);
  121. int (*switchdev_port_obj_del)(struct net_device *dev,
  122. const struct switchdev_obj *obj);
  123. };
  124. enum switchdev_notifier_type {
  125. SWITCHDEV_FDB_ADD_TO_BRIDGE = 1,
  126. SWITCHDEV_FDB_DEL_TO_BRIDGE,
  127. SWITCHDEV_FDB_ADD_TO_DEVICE,
  128. SWITCHDEV_FDB_DEL_TO_DEVICE,
  129. SWITCHDEV_FDB_OFFLOADED,
  130. };
  131. struct switchdev_notifier_info {
  132. struct net_device *dev;
  133. };
  134. struct switchdev_notifier_fdb_info {
  135. struct switchdev_notifier_info info; /* must be first */
  136. const unsigned char *addr;
  137. u16 vid;
  138. bool added_by_user;
  139. };
  140. static inline struct net_device *
  141. switchdev_notifier_info_to_dev(const struct switchdev_notifier_info *info)
  142. {
  143. return info->dev;
  144. }
  145. #ifdef CONFIG_NET_SWITCHDEV
  146. void switchdev_deferred_process(void);
  147. int switchdev_port_attr_get(struct net_device *dev,
  148. struct switchdev_attr *attr);
  149. int switchdev_port_attr_set(struct net_device *dev,
  150. const struct switchdev_attr *attr);
  151. int switchdev_port_obj_add(struct net_device *dev,
  152. const struct switchdev_obj *obj);
  153. int switchdev_port_obj_del(struct net_device *dev,
  154. const struct switchdev_obj *obj);
  155. int register_switchdev_notifier(struct notifier_block *nb);
  156. int unregister_switchdev_notifier(struct notifier_block *nb);
  157. int call_switchdev_notifiers(unsigned long val, struct net_device *dev,
  158. struct switchdev_notifier_info *info);
  159. void switchdev_port_fwd_mark_set(struct net_device *dev,
  160. struct net_device *group_dev,
  161. bool joining);
  162. bool switchdev_port_same_parent_id(struct net_device *a,
  163. struct net_device *b);
  164. #define SWITCHDEV_SET_OPS(netdev, ops) ((netdev)->switchdev_ops = (ops))
  165. #else
  166. static inline void switchdev_deferred_process(void)
  167. {
  168. }
  169. static inline int switchdev_port_attr_get(struct net_device *dev,
  170. struct switchdev_attr *attr)
  171. {
  172. return -EOPNOTSUPP;
  173. }
  174. static inline int switchdev_port_attr_set(struct net_device *dev,
  175. const struct switchdev_attr *attr)
  176. {
  177. return -EOPNOTSUPP;
  178. }
  179. static inline int switchdev_port_obj_add(struct net_device *dev,
  180. const struct switchdev_obj *obj)
  181. {
  182. return -EOPNOTSUPP;
  183. }
  184. static inline int switchdev_port_obj_del(struct net_device *dev,
  185. const struct switchdev_obj *obj)
  186. {
  187. return -EOPNOTSUPP;
  188. }
  189. static inline int register_switchdev_notifier(struct notifier_block *nb)
  190. {
  191. return 0;
  192. }
  193. static inline int unregister_switchdev_notifier(struct notifier_block *nb)
  194. {
  195. return 0;
  196. }
  197. static inline int call_switchdev_notifiers(unsigned long val,
  198. struct net_device *dev,
  199. struct switchdev_notifier_info *info)
  200. {
  201. return NOTIFY_DONE;
  202. }
  203. static inline bool switchdev_port_same_parent_id(struct net_device *a,
  204. struct net_device *b)
  205. {
  206. return false;
  207. }
  208. #define SWITCHDEV_SET_OPS(netdev, ops) do {} while (0)
  209. #endif
  210. #endif /* _LINUX_SWITCHDEV_H_ */