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>
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.
11 #ifndef _LINUX_SWITCHDEV_H_
12 #define _LINUX_SWITCHDEV_H_
14 #include <linux/netdevice.h>
15 #include <linux/notifier.h>
16 #include <linux/list.h>
17 #include <net/ip_fib.h>
19 #define SWITCHDEV_F_NO_RECURSE BIT(0)
20 #define SWITCHDEV_F_SKIP_EOPNOTSUPP BIT(1)
21 #define SWITCHDEV_F_DEFER BIT(2)
23 struct switchdev_trans_item {
24 struct list_head list;
26 void (*destructor)(const void *data);
29 struct switchdev_trans {
30 struct list_head item_list;
34 static inline bool switchdev_trans_ph_prepare(struct switchdev_trans *trans)
36 return trans && trans->ph_prepare;
39 static inline bool switchdev_trans_ph_commit(struct switchdev_trans *trans)
41 return trans && !trans->ph_prepare;
44 enum switchdev_attr_id {
45 SWITCHDEV_ATTR_ID_UNDEFINED,
46 SWITCHDEV_ATTR_ID_PORT_PARENT_ID,
47 SWITCHDEV_ATTR_ID_PORT_STP_STATE,
48 SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS,
49 SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME,
52 struct switchdev_attr {
53 struct net_device *orig_dev;
54 enum switchdev_attr_id id;
57 struct netdev_phys_item_id ppid; /* PORT_PARENT_ID */
58 u8 stp_state; /* PORT_STP_STATE */
59 unsigned long brport_flags; /* PORT_BRIDGE_FLAGS */
60 u32 ageing_time; /* BRIDGE_AGEING_TIME */
64 enum switchdev_obj_id {
65 SWITCHDEV_OBJ_ID_UNDEFINED,
66 SWITCHDEV_OBJ_ID_PORT_VLAN,
67 SWITCHDEV_OBJ_ID_IPV4_FIB,
68 SWITCHDEV_OBJ_ID_PORT_FDB,
71 struct switchdev_obj {
72 struct net_device *orig_dev;
73 enum switchdev_obj_id id;
77 /* SWITCHDEV_OBJ_ID_PORT_VLAN */
78 struct switchdev_obj_port_vlan {
79 struct switchdev_obj obj;
85 #define SWITCHDEV_OBJ_PORT_VLAN(obj) \
86 container_of(obj, struct switchdev_obj_port_vlan, obj)
88 /* SWITCHDEV_OBJ_ID_IPV4_FIB */
89 struct switchdev_obj_ipv4_fib {
90 struct switchdev_obj obj;
100 #define SWITCHDEV_OBJ_IPV4_FIB(obj) \
101 container_of(obj, struct switchdev_obj_ipv4_fib, obj)
103 /* SWITCHDEV_OBJ_ID_PORT_FDB */
104 struct switchdev_obj_port_fdb {
105 struct switchdev_obj obj;
106 unsigned char addr[ETH_ALEN];
111 #define SWITCHDEV_OBJ_PORT_FDB(obj) \
112 container_of(obj, struct switchdev_obj_port_fdb, obj)
114 void switchdev_trans_item_enqueue(struct switchdev_trans *trans,
115 void *data, void (*destructor)(void const *),
116 struct switchdev_trans_item *tritem);
117 void *switchdev_trans_item_dequeue(struct switchdev_trans *trans);
119 typedef int switchdev_obj_dump_cb_t(struct switchdev_obj *obj);
122 * struct switchdev_ops - switchdev operations
124 * @switchdev_port_attr_get: Get a port attribute (see switchdev_attr).
126 * @switchdev_port_attr_set: Set a port attribute (see switchdev_attr).
128 * @switchdev_port_obj_add: Add an object to port (see switchdev_obj_*).
130 * @switchdev_port_obj_del: Delete an object from port (see switchdev_obj_*).
132 * @switchdev_port_obj_dump: Dump port objects (see switchdev_obj_*).
134 struct switchdev_ops {
135 int (*switchdev_port_attr_get)(struct net_device *dev,
136 struct switchdev_attr *attr);
137 int (*switchdev_port_attr_set)(struct net_device *dev,
138 const struct switchdev_attr *attr,
139 struct switchdev_trans *trans);
140 int (*switchdev_port_obj_add)(struct net_device *dev,
141 const struct switchdev_obj *obj,
142 struct switchdev_trans *trans);
143 int (*switchdev_port_obj_del)(struct net_device *dev,
144 const struct switchdev_obj *obj);
145 int (*switchdev_port_obj_dump)(struct net_device *dev,
146 struct switchdev_obj *obj,
147 switchdev_obj_dump_cb_t *cb);
150 enum switchdev_notifier_type {
151 SWITCHDEV_FDB_ADD = 1,
155 struct switchdev_notifier_info {
156 struct net_device *dev;
159 struct switchdev_notifier_fdb_info {
160 struct switchdev_notifier_info info; /* must be first */
161 const unsigned char *addr;
165 static inline struct net_device *
166 switchdev_notifier_info_to_dev(const struct switchdev_notifier_info *info)
171 #ifdef CONFIG_NET_SWITCHDEV
173 void switchdev_deferred_process(void);
174 int switchdev_port_attr_get(struct net_device *dev,
175 struct switchdev_attr *attr);
176 int switchdev_port_attr_set(struct net_device *dev,
177 const struct switchdev_attr *attr);
178 int switchdev_port_obj_add(struct net_device *dev,
179 const struct switchdev_obj *obj);
180 int switchdev_port_obj_del(struct net_device *dev,
181 const struct switchdev_obj *obj);
182 int switchdev_port_obj_dump(struct net_device *dev, struct switchdev_obj *obj,
183 switchdev_obj_dump_cb_t *cb);
184 int register_switchdev_notifier(struct notifier_block *nb);
185 int unregister_switchdev_notifier(struct notifier_block *nb);
186 int call_switchdev_notifiers(unsigned long val, struct net_device *dev,
187 struct switchdev_notifier_info *info);
188 int switchdev_port_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
189 struct net_device *dev, u32 filter_mask,
191 int switchdev_port_bridge_setlink(struct net_device *dev,
192 struct nlmsghdr *nlh, u16 flags);
193 int switchdev_port_bridge_dellink(struct net_device *dev,
194 struct nlmsghdr *nlh, u16 flags);
195 int switchdev_fib_ipv4_add(u32 dst, int dst_len, struct fib_info *fi,
196 u8 tos, u8 type, u32 nlflags, u32 tb_id);
197 int switchdev_fib_ipv4_del(u32 dst, int dst_len, struct fib_info *fi,
198 u8 tos, u8 type, u32 tb_id);
199 void switchdev_fib_ipv4_abort(struct fib_info *fi);
200 int switchdev_port_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
201 struct net_device *dev, const unsigned char *addr,
202 u16 vid, u16 nlm_flags);
203 int switchdev_port_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
204 struct net_device *dev, const unsigned char *addr,
206 int switchdev_port_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
207 struct net_device *dev,
208 struct net_device *filter_dev, int idx);
209 void switchdev_port_fwd_mark_set(struct net_device *dev,
210 struct net_device *group_dev,
215 static inline void switchdev_deferred_process(void)
219 static inline int switchdev_port_attr_get(struct net_device *dev,
220 struct switchdev_attr *attr)
225 static inline int switchdev_port_attr_set(struct net_device *dev,
226 const struct switchdev_attr *attr)
231 static inline int switchdev_port_obj_add(struct net_device *dev,
232 const struct switchdev_obj *obj)
237 static inline int switchdev_port_obj_del(struct net_device *dev,
238 const struct switchdev_obj *obj)
243 static inline int switchdev_port_obj_dump(struct net_device *dev,
244 const struct switchdev_obj *obj,
245 switchdev_obj_dump_cb_t *cb)
250 static inline int register_switchdev_notifier(struct notifier_block *nb)
255 static inline int unregister_switchdev_notifier(struct notifier_block *nb)
260 static inline int call_switchdev_notifiers(unsigned long val,
261 struct net_device *dev,
262 struct switchdev_notifier_info *info)
267 static inline int switchdev_port_bridge_getlink(struct sk_buff *skb, u32 pid,
268 u32 seq, struct net_device *dev,
269 u32 filter_mask, int nlflags)
274 static inline int switchdev_port_bridge_setlink(struct net_device *dev,
275 struct nlmsghdr *nlh,
281 static inline int switchdev_port_bridge_dellink(struct net_device *dev,
282 struct nlmsghdr *nlh,
288 static inline int switchdev_fib_ipv4_add(u32 dst, int dst_len,
291 u32 nlflags, u32 tb_id)
296 static inline int switchdev_fib_ipv4_del(u32 dst, int dst_len,
298 u8 tos, u8 type, u32 tb_id)
303 static inline void switchdev_fib_ipv4_abort(struct fib_info *fi)
307 static inline int switchdev_port_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
308 struct net_device *dev,
309 const unsigned char *addr,
310 u16 vid, u16 nlm_flags)
315 static inline int switchdev_port_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
316 struct net_device *dev,
317 const unsigned char *addr, u16 vid)
322 static inline int switchdev_port_fdb_dump(struct sk_buff *skb,
323 struct netlink_callback *cb,
324 struct net_device *dev,
325 struct net_device *filter_dev,
331 static inline void switchdev_port_fwd_mark_set(struct net_device *dev,
332 struct net_device *group_dev,
339 #endif /* _LINUX_SWITCHDEV_H_ */