switchdev.h 9.2 KB

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