switchdev.h 8.9 KB

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