switchdev.h 8.8 KB

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