switchdev.h 8.6 KB

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