switchdev.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. };
  139. static inline struct net_device *
  140. switchdev_notifier_info_to_dev(const struct switchdev_notifier_info *info)
  141. {
  142. return info->dev;
  143. }
  144. #ifdef CONFIG_NET_SWITCHDEV
  145. void switchdev_deferred_process(void);
  146. int switchdev_port_attr_get(struct net_device *dev,
  147. struct switchdev_attr *attr);
  148. int switchdev_port_attr_set(struct net_device *dev,
  149. const struct switchdev_attr *attr);
  150. int switchdev_port_obj_add(struct net_device *dev,
  151. const struct switchdev_obj *obj);
  152. int switchdev_port_obj_del(struct net_device *dev,
  153. const struct switchdev_obj *obj);
  154. int register_switchdev_notifier(struct notifier_block *nb);
  155. int unregister_switchdev_notifier(struct notifier_block *nb);
  156. int call_switchdev_notifiers(unsigned long val, struct net_device *dev,
  157. struct switchdev_notifier_info *info);
  158. void switchdev_port_fwd_mark_set(struct net_device *dev,
  159. struct net_device *group_dev,
  160. bool joining);
  161. bool switchdev_port_same_parent_id(struct net_device *a,
  162. struct net_device *b);
  163. #define SWITCHDEV_SET_OPS(netdev, ops) ((netdev)->switchdev_ops = (ops))
  164. #else
  165. static inline void switchdev_deferred_process(void)
  166. {
  167. }
  168. static inline int switchdev_port_attr_get(struct net_device *dev,
  169. struct switchdev_attr *attr)
  170. {
  171. return -EOPNOTSUPP;
  172. }
  173. static inline int switchdev_port_attr_set(struct net_device *dev,
  174. const struct switchdev_attr *attr)
  175. {
  176. return -EOPNOTSUPP;
  177. }
  178. static inline int switchdev_port_obj_add(struct net_device *dev,
  179. const struct switchdev_obj *obj)
  180. {
  181. return -EOPNOTSUPP;
  182. }
  183. static inline int switchdev_port_obj_del(struct net_device *dev,
  184. const struct switchdev_obj *obj)
  185. {
  186. return -EOPNOTSUPP;
  187. }
  188. static inline int register_switchdev_notifier(struct notifier_block *nb)
  189. {
  190. return 0;
  191. }
  192. static inline int unregister_switchdev_notifier(struct notifier_block *nb)
  193. {
  194. return 0;
  195. }
  196. static inline int call_switchdev_notifiers(unsigned long val,
  197. struct net_device *dev,
  198. struct switchdev_notifier_info *info)
  199. {
  200. return NOTIFY_DONE;
  201. }
  202. static inline bool switchdev_port_same_parent_id(struct net_device *a,
  203. struct net_device *b)
  204. {
  205. return false;
  206. }
  207. #define SWITCHDEV_SET_OPS(netdev, ops) do {} while (0)
  208. #endif
  209. #endif /* _LINUX_SWITCHDEV_H_ */