switchdev.h 8.1 KB

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