switchdev.h 8.1 KB

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