switchdev.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. };
  48. struct switchdev_attr {
  49. struct net_device *orig_dev;
  50. enum switchdev_attr_id id;
  51. u32 flags;
  52. void *complete_priv;
  53. void (*complete)(struct net_device *dev, int err, void *priv);
  54. union {
  55. struct netdev_phys_item_id ppid; /* PORT_PARENT_ID */
  56. u8 stp_state; /* PORT_STP_STATE */
  57. unsigned long brport_flags; /* PORT_BRIDGE_FLAGS */
  58. unsigned long brport_flags_support; /* PORT_BRIDGE_FLAGS_SUPPORT */
  59. bool mrouter; /* PORT_MROUTER */
  60. clock_t ageing_time; /* BRIDGE_AGEING_TIME */
  61. bool vlan_filtering; /* BRIDGE_VLAN_FILTERING */
  62. bool mc_disabled; /* MC_DISABLED */
  63. } u;
  64. };
  65. enum switchdev_obj_id {
  66. SWITCHDEV_OBJ_ID_UNDEFINED,
  67. SWITCHDEV_OBJ_ID_PORT_VLAN,
  68. SWITCHDEV_OBJ_ID_PORT_FDB,
  69. SWITCHDEV_OBJ_ID_PORT_MDB,
  70. };
  71. struct switchdev_obj {
  72. struct net_device *orig_dev;
  73. enum switchdev_obj_id id;
  74. u32 flags;
  75. void *complete_priv;
  76. void (*complete)(struct net_device *dev, int err, void *priv);
  77. };
  78. /* SWITCHDEV_OBJ_ID_PORT_VLAN */
  79. struct switchdev_obj_port_vlan {
  80. struct switchdev_obj obj;
  81. u16 flags;
  82. u16 vid_begin;
  83. u16 vid_end;
  84. };
  85. #define SWITCHDEV_OBJ_PORT_VLAN(obj) \
  86. container_of(obj, struct switchdev_obj_port_vlan, obj)
  87. /* SWITCHDEV_OBJ_ID_PORT_FDB */
  88. struct switchdev_obj_port_fdb {
  89. struct switchdev_obj obj;
  90. unsigned char addr[ETH_ALEN];
  91. u16 vid;
  92. u16 ndm_state;
  93. };
  94. #define SWITCHDEV_OBJ_PORT_FDB(obj) \
  95. container_of(obj, struct switchdev_obj_port_fdb, obj)
  96. /* SWITCHDEV_OBJ_ID_PORT_MDB */
  97. struct switchdev_obj_port_mdb {
  98. struct switchdev_obj obj;
  99. unsigned char addr[ETH_ALEN];
  100. u16 vid;
  101. };
  102. #define SWITCHDEV_OBJ_PORT_MDB(obj) \
  103. container_of(obj, struct switchdev_obj_port_mdb, obj)
  104. void switchdev_trans_item_enqueue(struct switchdev_trans *trans,
  105. void *data, void (*destructor)(void const *),
  106. struct switchdev_trans_item *tritem);
  107. void *switchdev_trans_item_dequeue(struct switchdev_trans *trans);
  108. typedef int switchdev_obj_dump_cb_t(struct switchdev_obj *obj);
  109. /**
  110. * struct switchdev_ops - switchdev operations
  111. *
  112. * @switchdev_port_attr_get: Get a port attribute (see switchdev_attr).
  113. *
  114. * @switchdev_port_attr_set: Set a port attribute (see switchdev_attr).
  115. *
  116. * @switchdev_port_obj_add: Add an object to port (see switchdev_obj_*).
  117. *
  118. * @switchdev_port_obj_del: Delete an object from port (see switchdev_obj_*).
  119. *
  120. * @switchdev_port_obj_dump: Dump port objects (see switchdev_obj_*).
  121. */
  122. struct switchdev_ops {
  123. int (*switchdev_port_attr_get)(struct net_device *dev,
  124. struct switchdev_attr *attr);
  125. int (*switchdev_port_attr_set)(struct net_device *dev,
  126. const struct switchdev_attr *attr,
  127. struct switchdev_trans *trans);
  128. int (*switchdev_port_obj_add)(struct net_device *dev,
  129. const struct switchdev_obj *obj,
  130. struct switchdev_trans *trans);
  131. int (*switchdev_port_obj_del)(struct net_device *dev,
  132. const struct switchdev_obj *obj);
  133. int (*switchdev_port_obj_dump)(struct net_device *dev,
  134. struct switchdev_obj *obj,
  135. switchdev_obj_dump_cb_t *cb);
  136. };
  137. enum switchdev_notifier_type {
  138. SWITCHDEV_FDB_ADD_TO_BRIDGE = 1,
  139. SWITCHDEV_FDB_DEL_TO_BRIDGE,
  140. SWITCHDEV_FDB_ADD_TO_DEVICE,
  141. SWITCHDEV_FDB_DEL_TO_DEVICE,
  142. SWITCHDEV_FDB_OFFLOADED,
  143. };
  144. struct switchdev_notifier_info {
  145. struct net_device *dev;
  146. };
  147. struct switchdev_notifier_fdb_info {
  148. struct switchdev_notifier_info info; /* must be first */
  149. const unsigned char *addr;
  150. u16 vid;
  151. };
  152. static inline struct net_device *
  153. switchdev_notifier_info_to_dev(const struct switchdev_notifier_info *info)
  154. {
  155. return info->dev;
  156. }
  157. #ifdef CONFIG_NET_SWITCHDEV
  158. void switchdev_deferred_process(void);
  159. int switchdev_port_attr_get(struct net_device *dev,
  160. struct switchdev_attr *attr);
  161. int switchdev_port_attr_set(struct net_device *dev,
  162. const struct switchdev_attr *attr);
  163. int switchdev_port_obj_add(struct net_device *dev,
  164. const struct switchdev_obj *obj);
  165. int switchdev_port_obj_del(struct net_device *dev,
  166. const struct switchdev_obj *obj);
  167. int switchdev_port_obj_dump(struct net_device *dev, struct switchdev_obj *obj,
  168. switchdev_obj_dump_cb_t *cb);
  169. int register_switchdev_notifier(struct notifier_block *nb);
  170. int unregister_switchdev_notifier(struct notifier_block *nb);
  171. int call_switchdev_notifiers(unsigned long val, struct net_device *dev,
  172. struct switchdev_notifier_info *info);
  173. int switchdev_port_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
  174. struct net_device *dev, u32 filter_mask,
  175. int nlflags);
  176. int switchdev_port_bridge_setlink(struct net_device *dev,
  177. struct nlmsghdr *nlh, u16 flags);
  178. int switchdev_port_bridge_dellink(struct net_device *dev,
  179. struct nlmsghdr *nlh, u16 flags);
  180. int switchdev_port_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
  181. struct net_device *dev, const unsigned char *addr,
  182. u16 vid, u16 nlm_flags);
  183. int switchdev_port_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
  184. struct net_device *dev, const unsigned char *addr,
  185. u16 vid);
  186. int switchdev_port_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
  187. struct net_device *dev,
  188. struct net_device *filter_dev, int *idx);
  189. void switchdev_port_fwd_mark_set(struct net_device *dev,
  190. struct net_device *group_dev,
  191. bool joining);
  192. bool switchdev_port_same_parent_id(struct net_device *a,
  193. struct net_device *b);
  194. #else
  195. static inline void switchdev_deferred_process(void)
  196. {
  197. }
  198. static inline int switchdev_port_attr_get(struct net_device *dev,
  199. struct switchdev_attr *attr)
  200. {
  201. return -EOPNOTSUPP;
  202. }
  203. static inline int switchdev_port_attr_set(struct net_device *dev,
  204. const struct switchdev_attr *attr)
  205. {
  206. return -EOPNOTSUPP;
  207. }
  208. static inline int switchdev_port_obj_add(struct net_device *dev,
  209. const struct switchdev_obj *obj)
  210. {
  211. return -EOPNOTSUPP;
  212. }
  213. static inline int switchdev_port_obj_del(struct net_device *dev,
  214. const struct switchdev_obj *obj)
  215. {
  216. return -EOPNOTSUPP;
  217. }
  218. static inline int switchdev_port_obj_dump(struct net_device *dev,
  219. const struct switchdev_obj *obj,
  220. switchdev_obj_dump_cb_t *cb)
  221. {
  222. return -EOPNOTSUPP;
  223. }
  224. static inline int register_switchdev_notifier(struct notifier_block *nb)
  225. {
  226. return 0;
  227. }
  228. static inline int unregister_switchdev_notifier(struct notifier_block *nb)
  229. {
  230. return 0;
  231. }
  232. static inline int call_switchdev_notifiers(unsigned long val,
  233. struct net_device *dev,
  234. struct switchdev_notifier_info *info)
  235. {
  236. return NOTIFY_DONE;
  237. }
  238. static inline int switchdev_port_bridge_getlink(struct sk_buff *skb, u32 pid,
  239. u32 seq, struct net_device *dev,
  240. u32 filter_mask, int nlflags)
  241. {
  242. return -EOPNOTSUPP;
  243. }
  244. static inline int switchdev_port_bridge_setlink(struct net_device *dev,
  245. struct nlmsghdr *nlh,
  246. u16 flags)
  247. {
  248. return -EOPNOTSUPP;
  249. }
  250. static inline int switchdev_port_bridge_dellink(struct net_device *dev,
  251. struct nlmsghdr *nlh,
  252. u16 flags)
  253. {
  254. return -EOPNOTSUPP;
  255. }
  256. static inline int switchdev_port_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
  257. struct net_device *dev,
  258. const unsigned char *addr,
  259. u16 vid, u16 nlm_flags)
  260. {
  261. return -EOPNOTSUPP;
  262. }
  263. static inline int switchdev_port_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
  264. struct net_device *dev,
  265. const unsigned char *addr, u16 vid)
  266. {
  267. return -EOPNOTSUPP;
  268. }
  269. static inline int switchdev_port_fdb_dump(struct sk_buff *skb,
  270. struct netlink_callback *cb,
  271. struct net_device *dev,
  272. struct net_device *filter_dev,
  273. int *idx)
  274. {
  275. return *idx;
  276. }
  277. static inline bool switchdev_port_same_parent_id(struct net_device *a,
  278. struct net_device *b)
  279. {
  280. return false;
  281. }
  282. #endif
  283. #endif /* _LINUX_SWITCHDEV_H_ */