xref: /openbmc/linux/include/net/switchdev.h (revision 8f762fe5)
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 
14 #include <linux/netdevice.h>
15 #include <linux/notifier.h>
16 #include <linux/list.h>
17 #include <net/ip_fib.h>
18 
19 #define SWITCHDEV_F_NO_RECURSE		BIT(0)
20 #define SWITCHDEV_F_SKIP_EOPNOTSUPP	BIT(1)
21 #define SWITCHDEV_F_DEFER		BIT(2)
22 
23 struct switchdev_trans {
24 	bool ph_prepare;
25 };
26 
27 static inline bool switchdev_trans_ph_prepare(struct switchdev_trans *trans)
28 {
29 	return trans && trans->ph_prepare;
30 }
31 
32 static inline bool switchdev_trans_ph_commit(struct switchdev_trans *trans)
33 {
34 	return trans && !trans->ph_prepare;
35 }
36 
37 enum switchdev_attr_id {
38 	SWITCHDEV_ATTR_ID_UNDEFINED,
39 	SWITCHDEV_ATTR_ID_PORT_STP_STATE,
40 	SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS,
41 	SWITCHDEV_ATTR_ID_PORT_PRE_BRIDGE_FLAGS,
42 	SWITCHDEV_ATTR_ID_PORT_MROUTER,
43 	SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME,
44 	SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING,
45 	SWITCHDEV_ATTR_ID_BRIDGE_MC_DISABLED,
46 	SWITCHDEV_ATTR_ID_BRIDGE_MROUTER,
47 };
48 
49 struct switchdev_attr {
50 	struct net_device *orig_dev;
51 	enum switchdev_attr_id id;
52 	u32 flags;
53 	void *complete_priv;
54 	void (*complete)(struct net_device *dev, int err, void *priv);
55 	union {
56 		u8 stp_state;				/* PORT_STP_STATE */
57 		unsigned long brport_flags;		/* PORT_{PRE}_BRIDGE_FLAGS */
58 		bool mrouter;				/* PORT_MROUTER */
59 		clock_t ageing_time;			/* BRIDGE_AGEING_TIME */
60 		bool vlan_filtering;			/* BRIDGE_VLAN_FILTERING */
61 		bool mc_disabled;			/* MC_DISABLED */
62 	} u;
63 };
64 
65 enum switchdev_obj_id {
66 	SWITCHDEV_OBJ_ID_UNDEFINED,
67 	SWITCHDEV_OBJ_ID_PORT_VLAN,
68 	SWITCHDEV_OBJ_ID_PORT_MDB,
69 	SWITCHDEV_OBJ_ID_HOST_MDB,
70 };
71 
72 struct switchdev_obj {
73 	struct net_device *orig_dev;
74 	enum switchdev_obj_id id;
75 	u32 flags;
76 	void *complete_priv;
77 	void (*complete)(struct net_device *dev, int err, void *priv);
78 };
79 
80 /* SWITCHDEV_OBJ_ID_PORT_VLAN */
81 struct switchdev_obj_port_vlan {
82 	struct switchdev_obj obj;
83 	u16 flags;
84 	u16 vid_begin;
85 	u16 vid_end;
86 };
87 
88 #define SWITCHDEV_OBJ_PORT_VLAN(OBJ) \
89 	container_of((OBJ), struct switchdev_obj_port_vlan, obj)
90 
91 /* SWITCHDEV_OBJ_ID_PORT_MDB */
92 struct switchdev_obj_port_mdb {
93 	struct switchdev_obj obj;
94 	unsigned char addr[ETH_ALEN];
95 	u16 vid;
96 };
97 
98 #define SWITCHDEV_OBJ_PORT_MDB(OBJ) \
99 	container_of((OBJ), struct switchdev_obj_port_mdb, obj)
100 
101 typedef int switchdev_obj_dump_cb_t(struct switchdev_obj *obj);
102 
103 enum switchdev_notifier_type {
104 	SWITCHDEV_FDB_ADD_TO_BRIDGE = 1,
105 	SWITCHDEV_FDB_DEL_TO_BRIDGE,
106 	SWITCHDEV_FDB_ADD_TO_DEVICE,
107 	SWITCHDEV_FDB_DEL_TO_DEVICE,
108 	SWITCHDEV_FDB_OFFLOADED,
109 
110 	SWITCHDEV_PORT_OBJ_ADD, /* Blocking. */
111 	SWITCHDEV_PORT_OBJ_DEL, /* Blocking. */
112 	SWITCHDEV_PORT_ATTR_SET, /* May be blocking . */
113 
114 	SWITCHDEV_VXLAN_FDB_ADD_TO_BRIDGE,
115 	SWITCHDEV_VXLAN_FDB_DEL_TO_BRIDGE,
116 	SWITCHDEV_VXLAN_FDB_ADD_TO_DEVICE,
117 	SWITCHDEV_VXLAN_FDB_DEL_TO_DEVICE,
118 	SWITCHDEV_VXLAN_FDB_OFFLOADED,
119 };
120 
121 struct switchdev_notifier_info {
122 	struct net_device *dev;
123 	struct netlink_ext_ack *extack;
124 };
125 
126 struct switchdev_notifier_fdb_info {
127 	struct switchdev_notifier_info info; /* must be first */
128 	const unsigned char *addr;
129 	u16 vid;
130 	u8 added_by_user:1,
131 	   offloaded:1;
132 };
133 
134 struct switchdev_notifier_port_obj_info {
135 	struct switchdev_notifier_info info; /* must be first */
136 	const struct switchdev_obj *obj;
137 	struct switchdev_trans *trans;
138 	bool handled;
139 };
140 
141 struct switchdev_notifier_port_attr_info {
142 	struct switchdev_notifier_info info; /* must be first */
143 	const struct switchdev_attr *attr;
144 	struct switchdev_trans *trans;
145 	bool handled;
146 };
147 
148 static inline struct net_device *
149 switchdev_notifier_info_to_dev(const struct switchdev_notifier_info *info)
150 {
151 	return info->dev;
152 }
153 
154 static inline struct netlink_ext_ack *
155 switchdev_notifier_info_to_extack(const struct switchdev_notifier_info *info)
156 {
157 	return info->extack;
158 }
159 
160 #ifdef CONFIG_NET_SWITCHDEV
161 
162 void switchdev_deferred_process(void);
163 int switchdev_port_attr_set(struct net_device *dev,
164 			    const struct switchdev_attr *attr);
165 int switchdev_port_obj_add(struct net_device *dev,
166 			   const struct switchdev_obj *obj,
167 			   struct netlink_ext_ack *extack);
168 int switchdev_port_obj_del(struct net_device *dev,
169 			   const struct switchdev_obj *obj);
170 
171 int register_switchdev_notifier(struct notifier_block *nb);
172 int unregister_switchdev_notifier(struct notifier_block *nb);
173 int call_switchdev_notifiers(unsigned long val, struct net_device *dev,
174 			     struct switchdev_notifier_info *info,
175 			     struct netlink_ext_ack *extack);
176 
177 int register_switchdev_blocking_notifier(struct notifier_block *nb);
178 int unregister_switchdev_blocking_notifier(struct notifier_block *nb);
179 int call_switchdev_blocking_notifiers(unsigned long val, struct net_device *dev,
180 				      struct switchdev_notifier_info *info,
181 				      struct netlink_ext_ack *extack);
182 
183 void switchdev_port_fwd_mark_set(struct net_device *dev,
184 				 struct net_device *group_dev,
185 				 bool joining);
186 
187 int switchdev_handle_port_obj_add(struct net_device *dev,
188 			struct switchdev_notifier_port_obj_info *port_obj_info,
189 			bool (*check_cb)(const struct net_device *dev),
190 			int (*add_cb)(struct net_device *dev,
191 				      const struct switchdev_obj *obj,
192 				      struct switchdev_trans *trans,
193 				      struct netlink_ext_ack *extack));
194 int switchdev_handle_port_obj_del(struct net_device *dev,
195 			struct switchdev_notifier_port_obj_info *port_obj_info,
196 			bool (*check_cb)(const struct net_device *dev),
197 			int (*del_cb)(struct net_device *dev,
198 				      const struct switchdev_obj *obj));
199 
200 int switchdev_handle_port_attr_set(struct net_device *dev,
201 			struct switchdev_notifier_port_attr_info *port_attr_info,
202 			bool (*check_cb)(const struct net_device *dev),
203 			int (*set_cb)(struct net_device *dev,
204 				      const struct switchdev_attr *attr,
205 				      struct switchdev_trans *trans));
206 #else
207 
208 static inline void switchdev_deferred_process(void)
209 {
210 }
211 
212 static inline int switchdev_port_attr_set(struct net_device *dev,
213 					  const struct switchdev_attr *attr)
214 {
215 	return -EOPNOTSUPP;
216 }
217 
218 static inline int switchdev_port_obj_add(struct net_device *dev,
219 					 const struct switchdev_obj *obj,
220 					 struct netlink_ext_ack *extack)
221 {
222 	return -EOPNOTSUPP;
223 }
224 
225 static inline int switchdev_port_obj_del(struct net_device *dev,
226 					 const struct switchdev_obj *obj)
227 {
228 	return -EOPNOTSUPP;
229 }
230 
231 static inline int register_switchdev_notifier(struct notifier_block *nb)
232 {
233 	return 0;
234 }
235 
236 static inline int unregister_switchdev_notifier(struct notifier_block *nb)
237 {
238 	return 0;
239 }
240 
241 static inline int call_switchdev_notifiers(unsigned long val,
242 					   struct net_device *dev,
243 					   struct switchdev_notifier_info *info,
244 					   struct netlink_ext_ack *extack)
245 {
246 	return NOTIFY_DONE;
247 }
248 
249 static inline int
250 register_switchdev_blocking_notifier(struct notifier_block *nb)
251 {
252 	return 0;
253 }
254 
255 static inline int
256 unregister_switchdev_blocking_notifier(struct notifier_block *nb)
257 {
258 	return 0;
259 }
260 
261 static inline int
262 call_switchdev_blocking_notifiers(unsigned long val,
263 				  struct net_device *dev,
264 				  struct switchdev_notifier_info *info,
265 				  struct netlink_ext_ack *extack)
266 {
267 	return NOTIFY_DONE;
268 }
269 
270 static inline int
271 switchdev_handle_port_obj_add(struct net_device *dev,
272 			struct switchdev_notifier_port_obj_info *port_obj_info,
273 			bool (*check_cb)(const struct net_device *dev),
274 			int (*add_cb)(struct net_device *dev,
275 				      const struct switchdev_obj *obj,
276 				      struct switchdev_trans *trans,
277 				      struct netlink_ext_ack *extack))
278 {
279 	return 0;
280 }
281 
282 static inline int
283 switchdev_handle_port_obj_del(struct net_device *dev,
284 			struct switchdev_notifier_port_obj_info *port_obj_info,
285 			bool (*check_cb)(const struct net_device *dev),
286 			int (*del_cb)(struct net_device *dev,
287 				      const struct switchdev_obj *obj))
288 {
289 	return 0;
290 }
291 
292 static inline int
293 switchdev_handle_port_attr_set(struct net_device *dev,
294 			struct switchdev_notifier_port_attr_info *port_attr_info,
295 			bool (*check_cb)(const struct net_device *dev),
296 			int (*set_cb)(struct net_device *dev,
297 				      const struct switchdev_attr *attr,
298 				      struct switchdev_trans *trans))
299 {
300 	return 0;
301 }
302 #endif
303 
304 #endif /* _LINUX_SWITCHDEV_H_ */
305