switchdev.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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 net_device *dev, 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. struct switchdev_obj *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, struct switchdev_obj *obj);
  135. int switchdev_port_obj_del(struct net_device *dev, struct switchdev_obj *obj);
  136. int switchdev_port_obj_dump(struct net_device *dev, struct switchdev_obj *obj);
  137. int register_switchdev_notifier(struct notifier_block *nb);
  138. int unregister_switchdev_notifier(struct notifier_block *nb);
  139. int call_switchdev_notifiers(unsigned long val, struct net_device *dev,
  140. struct switchdev_notifier_info *info);
  141. int switchdev_port_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
  142. struct net_device *dev, u32 filter_mask,
  143. int nlflags);
  144. int switchdev_port_bridge_setlink(struct net_device *dev,
  145. struct nlmsghdr *nlh, u16 flags);
  146. int switchdev_port_bridge_dellink(struct net_device *dev,
  147. struct nlmsghdr *nlh, u16 flags);
  148. int switchdev_fib_ipv4_add(u32 dst, int dst_len, struct fib_info *fi,
  149. u8 tos, u8 type, u32 nlflags, u32 tb_id);
  150. int switchdev_fib_ipv4_del(u32 dst, int dst_len, struct fib_info *fi,
  151. u8 tos, u8 type, u32 tb_id);
  152. void switchdev_fib_ipv4_abort(struct fib_info *fi);
  153. int switchdev_port_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
  154. struct net_device *dev, const unsigned char *addr,
  155. u16 vid, u16 nlm_flags);
  156. int switchdev_port_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
  157. struct net_device *dev, const unsigned char *addr,
  158. u16 vid);
  159. int switchdev_port_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
  160. struct net_device *dev,
  161. struct net_device *filter_dev, int idx);
  162. void switchdev_port_fwd_mark_set(struct net_device *dev,
  163. struct net_device *group_dev,
  164. bool joining);
  165. #else
  166. static inline int switchdev_port_attr_get(struct net_device *dev,
  167. struct switchdev_attr *attr)
  168. {
  169. return -EOPNOTSUPP;
  170. }
  171. static inline int switchdev_port_attr_set(struct net_device *dev,
  172. struct switchdev_attr *attr)
  173. {
  174. return -EOPNOTSUPP;
  175. }
  176. static inline int switchdev_port_obj_add(struct net_device *dev,
  177. struct switchdev_obj *obj)
  178. {
  179. return -EOPNOTSUPP;
  180. }
  181. static inline int switchdev_port_obj_del(struct net_device *dev,
  182. struct switchdev_obj *obj)
  183. {
  184. return -EOPNOTSUPP;
  185. }
  186. static inline int switchdev_port_obj_dump(struct net_device *dev,
  187. struct switchdev_obj *obj)
  188. {
  189. return -EOPNOTSUPP;
  190. }
  191. static inline int register_switchdev_notifier(struct notifier_block *nb)
  192. {
  193. return 0;
  194. }
  195. static inline int unregister_switchdev_notifier(struct notifier_block *nb)
  196. {
  197. return 0;
  198. }
  199. static inline int call_switchdev_notifiers(unsigned long val,
  200. struct net_device *dev,
  201. struct switchdev_notifier_info *info)
  202. {
  203. return NOTIFY_DONE;
  204. }
  205. static inline int switchdev_port_bridge_getlink(struct sk_buff *skb, u32 pid,
  206. u32 seq, struct net_device *dev,
  207. u32 filter_mask, int nlflags)
  208. {
  209. return -EOPNOTSUPP;
  210. }
  211. static inline int switchdev_port_bridge_setlink(struct net_device *dev,
  212. struct nlmsghdr *nlh,
  213. u16 flags)
  214. {
  215. return -EOPNOTSUPP;
  216. }
  217. static inline int switchdev_port_bridge_dellink(struct net_device *dev,
  218. struct nlmsghdr *nlh,
  219. u16 flags)
  220. {
  221. return -EOPNOTSUPP;
  222. }
  223. static inline int switchdev_fib_ipv4_add(u32 dst, int dst_len,
  224. struct fib_info *fi,
  225. u8 tos, u8 type,
  226. u32 nlflags, u32 tb_id)
  227. {
  228. return 0;
  229. }
  230. static inline int switchdev_fib_ipv4_del(u32 dst, int dst_len,
  231. struct fib_info *fi,
  232. u8 tos, u8 type, u32 tb_id)
  233. {
  234. return 0;
  235. }
  236. static inline void switchdev_fib_ipv4_abort(struct fib_info *fi)
  237. {
  238. }
  239. static inline int switchdev_port_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
  240. struct net_device *dev,
  241. const unsigned char *addr,
  242. u16 vid, u16 nlm_flags)
  243. {
  244. return -EOPNOTSUPP;
  245. }
  246. static inline int switchdev_port_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
  247. struct net_device *dev,
  248. const unsigned char *addr, u16 vid)
  249. {
  250. return -EOPNOTSUPP;
  251. }
  252. static inline int switchdev_port_fdb_dump(struct sk_buff *skb,
  253. struct netlink_callback *cb,
  254. struct net_device *dev,
  255. struct net_device *filter_dev,
  256. int idx)
  257. {
  258. return -EOPNOTSUPP;
  259. }
  260. static inline void switchdev_port_fwd_mark_set(struct net_device *dev,
  261. struct net_device *group_dev,
  262. bool joining)
  263. {
  264. }
  265. #endif
  266. #endif /* _LINUX_SWITCHDEV_H_ */