switchdev.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. #define SWITCHDEV_F_NO_RECURSE BIT(0)
  17. enum switchdev_trans_ph {
  18. SWITCHDEV_TRANS_PREPARE,
  19. SWITCHDEV_TRANS_ABORT,
  20. SWITCHDEV_TRANS_COMMIT,
  21. };
  22. struct switchdev_trans_item {
  23. struct list_head list;
  24. void *data;
  25. void (*destructor)(const void *data);
  26. };
  27. struct switchdev_trans {
  28. struct list_head item_list;
  29. enum switchdev_trans_ph ph;
  30. };
  31. static inline bool switchdev_trans_ph_prepare(struct switchdev_trans *trans)
  32. {
  33. return trans && trans->ph == SWITCHDEV_TRANS_PREPARE;
  34. }
  35. static inline bool switchdev_trans_ph_commit(struct switchdev_trans *trans)
  36. {
  37. return trans && trans->ph == SWITCHDEV_TRANS_COMMIT;
  38. }
  39. enum switchdev_attr_id {
  40. SWITCHDEV_ATTR_UNDEFINED,
  41. SWITCHDEV_ATTR_PORT_PARENT_ID,
  42. SWITCHDEV_ATTR_PORT_STP_STATE,
  43. SWITCHDEV_ATTR_PORT_BRIDGE_FLAGS,
  44. };
  45. struct switchdev_attr {
  46. enum switchdev_attr_id id;
  47. u32 flags;
  48. union {
  49. struct netdev_phys_item_id ppid; /* PORT_PARENT_ID */
  50. u8 stp_state; /* PORT_STP_STATE */
  51. unsigned long brport_flags; /* PORT_BRIDGE_FLAGS */
  52. } u;
  53. };
  54. struct fib_info;
  55. enum switchdev_obj_id {
  56. SWITCHDEV_OBJ_UNDEFINED,
  57. SWITCHDEV_OBJ_PORT_VLAN,
  58. SWITCHDEV_OBJ_IPV4_FIB,
  59. SWITCHDEV_OBJ_PORT_FDB,
  60. };
  61. struct switchdev_obj {
  62. enum switchdev_obj_id id;
  63. int (*cb)(struct net_device *dev, struct switchdev_obj *obj);
  64. union {
  65. struct switchdev_obj_vlan { /* PORT_VLAN */
  66. u16 flags;
  67. u16 vid_begin;
  68. u16 vid_end;
  69. } vlan;
  70. struct switchdev_obj_ipv4_fib { /* IPV4_FIB */
  71. u32 dst;
  72. int dst_len;
  73. struct fib_info *fi;
  74. u8 tos;
  75. u8 type;
  76. u32 nlflags;
  77. u32 tb_id;
  78. } ipv4_fib;
  79. struct switchdev_obj_fdb { /* PORT_FDB */
  80. const unsigned char *addr;
  81. u16 vid;
  82. u16 ndm_state;
  83. } fdb;
  84. } u;
  85. };
  86. void switchdev_trans_item_enqueue(struct switchdev_trans *trans,
  87. void *data, void (*destructor)(void const *),
  88. struct switchdev_trans_item *tritem);
  89. void *switchdev_trans_item_dequeue(struct switchdev_trans *trans);
  90. /**
  91. * struct switchdev_ops - switchdev operations
  92. *
  93. * @switchdev_port_attr_get: Get a port attribute (see switchdev_attr).
  94. *
  95. * @switchdev_port_attr_set: Set a port attribute (see switchdev_attr).
  96. *
  97. * @switchdev_port_obj_add: Add an object to port (see switchdev_obj).
  98. *
  99. * @switchdev_port_obj_del: Delete an object from port (see switchdev_obj).
  100. *
  101. * @switchdev_port_obj_dump: Dump port objects (see switchdev_obj).
  102. */
  103. struct switchdev_ops {
  104. int (*switchdev_port_attr_get)(struct net_device *dev,
  105. struct switchdev_attr *attr);
  106. int (*switchdev_port_attr_set)(struct net_device *dev,
  107. struct switchdev_attr *attr,
  108. struct switchdev_trans *trans);
  109. int (*switchdev_port_obj_add)(struct net_device *dev,
  110. struct switchdev_obj *obj,
  111. struct switchdev_trans *trans);
  112. int (*switchdev_port_obj_del)(struct net_device *dev,
  113. struct switchdev_obj *obj);
  114. int (*switchdev_port_obj_dump)(struct net_device *dev,
  115. struct switchdev_obj *obj);
  116. };
  117. enum switchdev_notifier_type {
  118. SWITCHDEV_FDB_ADD = 1,
  119. SWITCHDEV_FDB_DEL,
  120. };
  121. struct switchdev_notifier_info {
  122. struct net_device *dev;
  123. };
  124. struct switchdev_notifier_fdb_info {
  125. struct switchdev_notifier_info info; /* must be first */
  126. const unsigned char *addr;
  127. u16 vid;
  128. };
  129. static inline struct net_device *
  130. switchdev_notifier_info_to_dev(const struct switchdev_notifier_info *info)
  131. {
  132. return info->dev;
  133. }
  134. #ifdef CONFIG_NET_SWITCHDEV
  135. int switchdev_port_attr_get(struct net_device *dev,
  136. struct switchdev_attr *attr);
  137. int switchdev_port_attr_set(struct net_device *dev,
  138. struct switchdev_attr *attr);
  139. int switchdev_port_obj_add(struct net_device *dev, struct switchdev_obj *obj);
  140. int switchdev_port_obj_del(struct net_device *dev, struct switchdev_obj *obj);
  141. int switchdev_port_obj_dump(struct net_device *dev, struct switchdev_obj *obj);
  142. int register_switchdev_notifier(struct notifier_block *nb);
  143. int unregister_switchdev_notifier(struct notifier_block *nb);
  144. int call_switchdev_notifiers(unsigned long val, struct net_device *dev,
  145. struct switchdev_notifier_info *info);
  146. int switchdev_port_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
  147. struct net_device *dev, u32 filter_mask,
  148. int nlflags);
  149. int switchdev_port_bridge_setlink(struct net_device *dev,
  150. struct nlmsghdr *nlh, u16 flags);
  151. int switchdev_port_bridge_dellink(struct net_device *dev,
  152. struct nlmsghdr *nlh, u16 flags);
  153. int switchdev_fib_ipv4_add(u32 dst, int dst_len, struct fib_info *fi,
  154. u8 tos, u8 type, u32 nlflags, u32 tb_id);
  155. int switchdev_fib_ipv4_del(u32 dst, int dst_len, struct fib_info *fi,
  156. u8 tos, u8 type, u32 tb_id);
  157. void switchdev_fib_ipv4_abort(struct fib_info *fi);
  158. int switchdev_port_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
  159. struct net_device *dev, const unsigned char *addr,
  160. u16 vid, u16 nlm_flags);
  161. int switchdev_port_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
  162. struct net_device *dev, const unsigned char *addr,
  163. u16 vid);
  164. int switchdev_port_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
  165. struct net_device *dev,
  166. struct net_device *filter_dev, int idx);
  167. void switchdev_port_fwd_mark_set(struct net_device *dev,
  168. struct net_device *group_dev,
  169. bool joining);
  170. #else
  171. static inline int switchdev_port_attr_get(struct net_device *dev,
  172. struct switchdev_attr *attr)
  173. {
  174. return -EOPNOTSUPP;
  175. }
  176. static inline int switchdev_port_attr_set(struct net_device *dev,
  177. struct switchdev_attr *attr)
  178. {
  179. return -EOPNOTSUPP;
  180. }
  181. static inline int switchdev_port_obj_add(struct net_device *dev,
  182. struct switchdev_obj *obj)
  183. {
  184. return -EOPNOTSUPP;
  185. }
  186. static inline int switchdev_port_obj_del(struct net_device *dev,
  187. struct switchdev_obj *obj)
  188. {
  189. return -EOPNOTSUPP;
  190. }
  191. static inline int switchdev_port_obj_dump(struct net_device *dev,
  192. struct switchdev_obj *obj)
  193. {
  194. return -EOPNOTSUPP;
  195. }
  196. static inline int register_switchdev_notifier(struct notifier_block *nb)
  197. {
  198. return 0;
  199. }
  200. static inline int unregister_switchdev_notifier(struct notifier_block *nb)
  201. {
  202. return 0;
  203. }
  204. static inline int call_switchdev_notifiers(unsigned long val,
  205. struct net_device *dev,
  206. struct switchdev_notifier_info *info)
  207. {
  208. return NOTIFY_DONE;
  209. }
  210. static inline int switchdev_port_bridge_getlink(struct sk_buff *skb, u32 pid,
  211. u32 seq, struct net_device *dev,
  212. u32 filter_mask, int nlflags)
  213. {
  214. return -EOPNOTSUPP;
  215. }
  216. static inline int switchdev_port_bridge_setlink(struct net_device *dev,
  217. struct nlmsghdr *nlh,
  218. u16 flags)
  219. {
  220. return -EOPNOTSUPP;
  221. }
  222. static inline int switchdev_port_bridge_dellink(struct net_device *dev,
  223. struct nlmsghdr *nlh,
  224. u16 flags)
  225. {
  226. return -EOPNOTSUPP;
  227. }
  228. static inline int switchdev_fib_ipv4_add(u32 dst, int dst_len,
  229. struct fib_info *fi,
  230. u8 tos, u8 type,
  231. u32 nlflags, u32 tb_id)
  232. {
  233. return 0;
  234. }
  235. static inline int switchdev_fib_ipv4_del(u32 dst, int dst_len,
  236. struct fib_info *fi,
  237. u8 tos, u8 type, u32 tb_id)
  238. {
  239. return 0;
  240. }
  241. static inline void switchdev_fib_ipv4_abort(struct fib_info *fi)
  242. {
  243. }
  244. static inline int switchdev_port_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
  245. struct net_device *dev,
  246. const unsigned char *addr,
  247. u16 vid, u16 nlm_flags)
  248. {
  249. return -EOPNOTSUPP;
  250. }
  251. static inline int switchdev_port_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
  252. struct net_device *dev,
  253. const unsigned char *addr, u16 vid)
  254. {
  255. return -EOPNOTSUPP;
  256. }
  257. static inline int switchdev_port_fdb_dump(struct sk_buff *skb,
  258. struct netlink_callback *cb,
  259. struct net_device *dev,
  260. struct net_device *filter_dev,
  261. int idx)
  262. {
  263. return -EOPNOTSUPP;
  264. }
  265. static inline void switchdev_port_fwd_mark_set(struct net_device *dev,
  266. struct net_device *group_dev,
  267. bool joining)
  268. {
  269. }
  270. #endif
  271. #endif /* _LINUX_SWITCHDEV_H_ */