switchdev.h 8.5 KB

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