switchdev.h 9.4 KB

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