switchdev.h 8.1 KB

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