switchdev.h 7.9 KB

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