switchdev.h 8.8 KB

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