switchdev.h 9.0 KB

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