xref: /linux/net/bridge/br_switchdev.c (revision 6015fb905d89063231ed33bc15be19ef0fc339b8)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/kernel.h>
3 #include <linux/list.h>
4 #include <linux/netdevice.h>
5 #include <linux/rtnetlink.h>
6 #include <linux/skbuff.h>
7 #include <net/ip.h>
8 #include <net/switchdev.h>
9 
10 #include "br_private.h"
11 
12 static struct static_key_false br_switchdev_tx_fwd_offload;
13 
14 static bool nbp_switchdev_can_offload_tx_fwd(const struct net_bridge_port *p,
15 					     const struct sk_buff *skb)
16 {
17 	if (!static_branch_unlikely(&br_switchdev_tx_fwd_offload))
18 		return false;
19 
20 	return (p->flags & BR_TX_FWD_OFFLOAD) &&
21 	       (p->hwdom != BR_INPUT_SKB_CB(skb)->src_hwdom);
22 }
23 
24 bool br_switchdev_frame_uses_tx_fwd_offload(struct sk_buff *skb)
25 {
26 	if (!static_branch_unlikely(&br_switchdev_tx_fwd_offload))
27 		return false;
28 
29 	return BR_INPUT_SKB_CB(skb)->tx_fwd_offload;
30 }
31 
32 void br_switchdev_frame_set_offload_fwd_mark(struct sk_buff *skb)
33 {
34 	skb->offload_fwd_mark = br_switchdev_frame_uses_tx_fwd_offload(skb);
35 }
36 
37 /* Mark the frame for TX forwarding offload if this egress port supports it */
38 void nbp_switchdev_frame_mark_tx_fwd_offload(const struct net_bridge_port *p,
39 					     struct sk_buff *skb)
40 {
41 	if (nbp_switchdev_can_offload_tx_fwd(p, skb))
42 		BR_INPUT_SKB_CB(skb)->tx_fwd_offload = true;
43 }
44 
45 /* Lazily adds the hwdom of the egress bridge port to the bit mask of hwdoms
46  * that the skb has been already forwarded to, to avoid further cloning to
47  * other ports in the same hwdom by making nbp_switchdev_allowed_egress()
48  * return false.
49  */
50 void nbp_switchdev_frame_mark_tx_fwd_to_hwdom(const struct net_bridge_port *p,
51 					      struct sk_buff *skb)
52 {
53 	if (nbp_switchdev_can_offload_tx_fwd(p, skb))
54 		set_bit(p->hwdom, &BR_INPUT_SKB_CB(skb)->fwd_hwdoms);
55 }
56 
57 void nbp_switchdev_frame_mark(const struct net_bridge_port *p,
58 			      struct sk_buff *skb)
59 {
60 	if (p->hwdom)
61 		BR_INPUT_SKB_CB(skb)->src_hwdom = p->hwdom;
62 }
63 
64 bool nbp_switchdev_allowed_egress(const struct net_bridge_port *p,
65 				  const struct sk_buff *skb)
66 {
67 	struct br_input_skb_cb *cb = BR_INPUT_SKB_CB(skb);
68 
69 	return !test_bit(p->hwdom, &cb->fwd_hwdoms) &&
70 		(!skb->offload_fwd_mark || cb->src_hwdom != p->hwdom);
71 }
72 
73 /* Flags that can be offloaded to hardware */
74 #define BR_PORT_FLAGS_HW_OFFLOAD (BR_LEARNING | BR_FLOOD | \
75 				  BR_MCAST_FLOOD | BR_BCAST_FLOOD | BR_PORT_LOCKED)
76 
77 int br_switchdev_set_port_flag(struct net_bridge_port *p,
78 			       unsigned long flags,
79 			       unsigned long mask,
80 			       struct netlink_ext_ack *extack)
81 {
82 	struct switchdev_attr attr = {
83 		.orig_dev = p->dev,
84 	};
85 	struct switchdev_notifier_port_attr_info info = {
86 		.attr = &attr,
87 	};
88 	int err;
89 
90 	mask &= BR_PORT_FLAGS_HW_OFFLOAD;
91 	if (!mask)
92 		return 0;
93 
94 	attr.id = SWITCHDEV_ATTR_ID_PORT_PRE_BRIDGE_FLAGS;
95 	attr.u.brport_flags.val = flags;
96 	attr.u.brport_flags.mask = mask;
97 
98 	/* We run from atomic context here */
99 	err = call_switchdev_notifiers(SWITCHDEV_PORT_ATTR_SET, p->dev,
100 				       &info.info, extack);
101 	err = notifier_to_errno(err);
102 	if (err == -EOPNOTSUPP)
103 		return 0;
104 
105 	if (err) {
106 		if (extack && !extack->_msg)
107 			NL_SET_ERR_MSG_MOD(extack,
108 					   "bridge flag offload is not supported");
109 		return -EOPNOTSUPP;
110 	}
111 
112 	attr.id = SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS;
113 	attr.flags = SWITCHDEV_F_DEFER;
114 
115 	err = switchdev_port_attr_set(p->dev, &attr, extack);
116 	if (err) {
117 		if (extack && !extack->_msg)
118 			NL_SET_ERR_MSG_MOD(extack,
119 					   "error setting offload flag on port");
120 		return err;
121 	}
122 
123 	return 0;
124 }
125 
126 static void br_switchdev_fdb_populate(struct net_bridge *br,
127 				      struct switchdev_notifier_fdb_info *item,
128 				      const struct net_bridge_fdb_entry *fdb,
129 				      const void *ctx)
130 {
131 	const struct net_bridge_port *p = READ_ONCE(fdb->dst);
132 
133 	item->addr = fdb->key.addr.addr;
134 	item->vid = fdb->key.vlan_id;
135 	item->added_by_user = test_bit(BR_FDB_ADDED_BY_USER, &fdb->flags);
136 	item->offloaded = test_bit(BR_FDB_OFFLOADED, &fdb->flags);
137 	item->is_local = test_bit(BR_FDB_LOCAL, &fdb->flags);
138 	item->info.dev = (!p || item->is_local) ? br->dev : p->dev;
139 	item->info.ctx = ctx;
140 }
141 
142 void
143 br_switchdev_fdb_notify(struct net_bridge *br,
144 			const struct net_bridge_fdb_entry *fdb, int type)
145 {
146 	struct switchdev_notifier_fdb_info item;
147 
148 	br_switchdev_fdb_populate(br, &item, fdb, NULL);
149 
150 	switch (type) {
151 	case RTM_DELNEIGH:
152 		call_switchdev_notifiers(SWITCHDEV_FDB_DEL_TO_DEVICE,
153 					 item.info.dev, &item.info, NULL);
154 		break;
155 	case RTM_NEWNEIGH:
156 		call_switchdev_notifiers(SWITCHDEV_FDB_ADD_TO_DEVICE,
157 					 item.info.dev, &item.info, NULL);
158 		break;
159 	}
160 }
161 
162 int br_switchdev_port_vlan_add(struct net_device *dev, u16 vid, u16 flags,
163 			       bool changed, struct netlink_ext_ack *extack)
164 {
165 	struct switchdev_obj_port_vlan v = {
166 		.obj.orig_dev = dev,
167 		.obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
168 		.flags = flags,
169 		.vid = vid,
170 		.changed = changed,
171 	};
172 
173 	return switchdev_port_obj_add(dev, &v.obj, extack);
174 }
175 
176 int br_switchdev_port_vlan_del(struct net_device *dev, u16 vid)
177 {
178 	struct switchdev_obj_port_vlan v = {
179 		.obj.orig_dev = dev,
180 		.obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
181 		.vid = vid,
182 	};
183 
184 	return switchdev_port_obj_del(dev, &v.obj);
185 }
186 
187 static int nbp_switchdev_hwdom_set(struct net_bridge_port *joining)
188 {
189 	struct net_bridge *br = joining->br;
190 	struct net_bridge_port *p;
191 	int hwdom;
192 
193 	/* joining is yet to be added to the port list. */
194 	list_for_each_entry(p, &br->port_list, list) {
195 		if (netdev_phys_item_id_same(&joining->ppid, &p->ppid)) {
196 			joining->hwdom = p->hwdom;
197 			return 0;
198 		}
199 	}
200 
201 	hwdom = find_next_zero_bit(&br->busy_hwdoms, BR_HWDOM_MAX, 1);
202 	if (hwdom >= BR_HWDOM_MAX)
203 		return -EBUSY;
204 
205 	set_bit(hwdom, &br->busy_hwdoms);
206 	joining->hwdom = hwdom;
207 	return 0;
208 }
209 
210 static void nbp_switchdev_hwdom_put(struct net_bridge_port *leaving)
211 {
212 	struct net_bridge *br = leaving->br;
213 	struct net_bridge_port *p;
214 
215 	/* leaving is no longer in the port list. */
216 	list_for_each_entry(p, &br->port_list, list) {
217 		if (p->hwdom == leaving->hwdom)
218 			return;
219 	}
220 
221 	clear_bit(leaving->hwdom, &br->busy_hwdoms);
222 }
223 
224 static int nbp_switchdev_add(struct net_bridge_port *p,
225 			     struct netdev_phys_item_id ppid,
226 			     bool tx_fwd_offload,
227 			     struct netlink_ext_ack *extack)
228 {
229 	int err;
230 
231 	if (p->offload_count) {
232 		/* Prevent unsupported configurations such as a bridge port
233 		 * which is a bonding interface, and the member ports are from
234 		 * different hardware switches.
235 		 */
236 		if (!netdev_phys_item_id_same(&p->ppid, &ppid)) {
237 			NL_SET_ERR_MSG_MOD(extack,
238 					   "Same bridge port cannot be offloaded by two physical switches");
239 			return -EBUSY;
240 		}
241 
242 		/* Tolerate drivers that call switchdev_bridge_port_offload()
243 		 * more than once for the same bridge port, such as when the
244 		 * bridge port is an offloaded bonding/team interface.
245 		 */
246 		p->offload_count++;
247 
248 		return 0;
249 	}
250 
251 	p->ppid = ppid;
252 	p->offload_count = 1;
253 
254 	err = nbp_switchdev_hwdom_set(p);
255 	if (err)
256 		return err;
257 
258 	if (tx_fwd_offload) {
259 		p->flags |= BR_TX_FWD_OFFLOAD;
260 		static_branch_inc(&br_switchdev_tx_fwd_offload);
261 	}
262 
263 	return 0;
264 }
265 
266 static void nbp_switchdev_del(struct net_bridge_port *p)
267 {
268 	if (WARN_ON(!p->offload_count))
269 		return;
270 
271 	p->offload_count--;
272 
273 	if (p->offload_count)
274 		return;
275 
276 	if (p->hwdom)
277 		nbp_switchdev_hwdom_put(p);
278 
279 	if (p->flags & BR_TX_FWD_OFFLOAD) {
280 		p->flags &= ~BR_TX_FWD_OFFLOAD;
281 		static_branch_dec(&br_switchdev_tx_fwd_offload);
282 	}
283 }
284 
285 static int
286 br_switchdev_fdb_replay_one(struct net_bridge *br, struct notifier_block *nb,
287 			    const struct net_bridge_fdb_entry *fdb,
288 			    unsigned long action, const void *ctx)
289 {
290 	struct switchdev_notifier_fdb_info item;
291 	int err;
292 
293 	br_switchdev_fdb_populate(br, &item, fdb, ctx);
294 
295 	err = nb->notifier_call(nb, action, &item);
296 	return notifier_to_errno(err);
297 }
298 
299 static int
300 br_switchdev_fdb_replay(const struct net_device *br_dev, const void *ctx,
301 			bool adding, struct notifier_block *nb)
302 {
303 	struct net_bridge_fdb_entry *fdb;
304 	struct net_bridge *br;
305 	unsigned long action;
306 	int err = 0;
307 
308 	if (!nb)
309 		return 0;
310 
311 	if (!netif_is_bridge_master(br_dev))
312 		return -EINVAL;
313 
314 	br = netdev_priv(br_dev);
315 
316 	if (adding)
317 		action = SWITCHDEV_FDB_ADD_TO_DEVICE;
318 	else
319 		action = SWITCHDEV_FDB_DEL_TO_DEVICE;
320 
321 	rcu_read_lock();
322 
323 	hlist_for_each_entry_rcu(fdb, &br->fdb_list, fdb_node) {
324 		err = br_switchdev_fdb_replay_one(br, nb, fdb, action, ctx);
325 		if (err)
326 			break;
327 	}
328 
329 	rcu_read_unlock();
330 
331 	return err;
332 }
333 
334 static int
335 br_switchdev_vlan_replay_one(struct notifier_block *nb,
336 			     struct net_device *dev,
337 			     struct switchdev_obj_port_vlan *vlan,
338 			     const void *ctx, unsigned long action,
339 			     struct netlink_ext_ack *extack)
340 {
341 	struct switchdev_notifier_port_obj_info obj_info = {
342 		.info = {
343 			.dev = dev,
344 			.extack = extack,
345 			.ctx = ctx,
346 		},
347 		.obj = &vlan->obj,
348 	};
349 	int err;
350 
351 	err = nb->notifier_call(nb, action, &obj_info);
352 	return notifier_to_errno(err);
353 }
354 
355 static int br_switchdev_vlan_replay_group(struct notifier_block *nb,
356 					  struct net_device *dev,
357 					  struct net_bridge_vlan_group *vg,
358 					  const void *ctx, unsigned long action,
359 					  struct netlink_ext_ack *extack)
360 {
361 	struct net_bridge_vlan *v;
362 	int err = 0;
363 	u16 pvid;
364 
365 	if (!vg)
366 		return 0;
367 
368 	pvid = br_get_pvid(vg);
369 
370 	list_for_each_entry(v, &vg->vlan_list, vlist) {
371 		struct switchdev_obj_port_vlan vlan = {
372 			.obj.orig_dev = dev,
373 			.obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
374 			.flags = br_vlan_flags(v, pvid),
375 			.vid = v->vid,
376 		};
377 
378 		if (!br_vlan_should_use(v))
379 			continue;
380 
381 		err = br_switchdev_vlan_replay_one(nb, dev, &vlan, ctx,
382 						   action, extack);
383 		if (err)
384 			return err;
385 	}
386 
387 	return 0;
388 }
389 
390 static int br_switchdev_vlan_replay(struct net_device *br_dev,
391 				    const void *ctx, bool adding,
392 				    struct notifier_block *nb,
393 				    struct netlink_ext_ack *extack)
394 {
395 	struct net_bridge *br = netdev_priv(br_dev);
396 	struct net_bridge_port *p;
397 	unsigned long action;
398 	int err;
399 
400 	ASSERT_RTNL();
401 
402 	if (!nb)
403 		return 0;
404 
405 	if (!netif_is_bridge_master(br_dev))
406 		return -EINVAL;
407 
408 	if (adding)
409 		action = SWITCHDEV_PORT_OBJ_ADD;
410 	else
411 		action = SWITCHDEV_PORT_OBJ_DEL;
412 
413 	err = br_switchdev_vlan_replay_group(nb, br_dev, br_vlan_group(br),
414 					     ctx, action, extack);
415 	if (err)
416 		return err;
417 
418 	list_for_each_entry(p, &br->port_list, list) {
419 		struct net_device *dev = p->dev;
420 
421 		err = br_switchdev_vlan_replay_group(nb, dev,
422 						     nbp_vlan_group(p),
423 						     ctx, action, extack);
424 		if (err)
425 			return err;
426 	}
427 
428 	return 0;
429 }
430 
431 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
432 struct br_switchdev_mdb_complete_info {
433 	struct net_bridge_port *port;
434 	struct br_ip ip;
435 };
436 
437 static void br_switchdev_mdb_complete(struct net_device *dev, int err, void *priv)
438 {
439 	struct br_switchdev_mdb_complete_info *data = priv;
440 	struct net_bridge_port_group __rcu **pp;
441 	struct net_bridge_port_group *p;
442 	struct net_bridge_mdb_entry *mp;
443 	struct net_bridge_port *port = data->port;
444 	struct net_bridge *br = port->br;
445 
446 	if (err)
447 		goto err;
448 
449 	spin_lock_bh(&br->multicast_lock);
450 	mp = br_mdb_ip_get(br, &data->ip);
451 	if (!mp)
452 		goto out;
453 	for (pp = &mp->ports; (p = mlock_dereference(*pp, br)) != NULL;
454 	     pp = &p->next) {
455 		if (p->key.port != port)
456 			continue;
457 		p->flags |= MDB_PG_FLAGS_OFFLOAD;
458 	}
459 out:
460 	spin_unlock_bh(&br->multicast_lock);
461 err:
462 	kfree(priv);
463 }
464 
465 static void br_switchdev_mdb_populate(struct switchdev_obj_port_mdb *mdb,
466 				      const struct net_bridge_mdb_entry *mp)
467 {
468 	if (mp->addr.proto == htons(ETH_P_IP))
469 		ip_eth_mc_map(mp->addr.dst.ip4, mdb->addr);
470 #if IS_ENABLED(CONFIG_IPV6)
471 	else if (mp->addr.proto == htons(ETH_P_IPV6))
472 		ipv6_eth_mc_map(&mp->addr.dst.ip6, mdb->addr);
473 #endif
474 	else
475 		ether_addr_copy(mdb->addr, mp->addr.dst.mac_addr);
476 
477 	mdb->vid = mp->addr.vid;
478 }
479 
480 static void br_switchdev_host_mdb_one(struct net_device *dev,
481 				      struct net_device *lower_dev,
482 				      struct net_bridge_mdb_entry *mp,
483 				      int type)
484 {
485 	struct switchdev_obj_port_mdb mdb = {
486 		.obj = {
487 			.id = SWITCHDEV_OBJ_ID_HOST_MDB,
488 			.flags = SWITCHDEV_F_DEFER,
489 			.orig_dev = dev,
490 		},
491 	};
492 
493 	br_switchdev_mdb_populate(&mdb, mp);
494 
495 	switch (type) {
496 	case RTM_NEWMDB:
497 		switchdev_port_obj_add(lower_dev, &mdb.obj, NULL);
498 		break;
499 	case RTM_DELMDB:
500 		switchdev_port_obj_del(lower_dev, &mdb.obj);
501 		break;
502 	}
503 }
504 
505 static void br_switchdev_host_mdb(struct net_device *dev,
506 				  struct net_bridge_mdb_entry *mp, int type)
507 {
508 	struct net_device *lower_dev;
509 	struct list_head *iter;
510 
511 	netdev_for_each_lower_dev(dev, lower_dev, iter)
512 		br_switchdev_host_mdb_one(dev, lower_dev, mp, type);
513 }
514 
515 static int
516 br_switchdev_mdb_replay_one(struct notifier_block *nb, struct net_device *dev,
517 			    const struct switchdev_obj_port_mdb *mdb,
518 			    unsigned long action, const void *ctx,
519 			    struct netlink_ext_ack *extack)
520 {
521 	struct switchdev_notifier_port_obj_info obj_info = {
522 		.info = {
523 			.dev = dev,
524 			.extack = extack,
525 			.ctx = ctx,
526 		},
527 		.obj = &mdb->obj,
528 	};
529 	int err;
530 
531 	err = nb->notifier_call(nb, action, &obj_info);
532 	return notifier_to_errno(err);
533 }
534 
535 static int br_switchdev_mdb_queue_one(struct list_head *mdb_list,
536 				      enum switchdev_obj_id id,
537 				      const struct net_bridge_mdb_entry *mp,
538 				      struct net_device *orig_dev)
539 {
540 	struct switchdev_obj_port_mdb *mdb;
541 
542 	mdb = kzalloc(sizeof(*mdb), GFP_ATOMIC);
543 	if (!mdb)
544 		return -ENOMEM;
545 
546 	mdb->obj.id = id;
547 	mdb->obj.orig_dev = orig_dev;
548 	br_switchdev_mdb_populate(mdb, mp);
549 	list_add_tail(&mdb->obj.list, mdb_list);
550 
551 	return 0;
552 }
553 
554 void br_switchdev_mdb_notify(struct net_device *dev,
555 			     struct net_bridge_mdb_entry *mp,
556 			     struct net_bridge_port_group *pg,
557 			     int type)
558 {
559 	struct br_switchdev_mdb_complete_info *complete_info;
560 	struct switchdev_obj_port_mdb mdb = {
561 		.obj = {
562 			.id = SWITCHDEV_OBJ_ID_PORT_MDB,
563 			.flags = SWITCHDEV_F_DEFER,
564 		},
565 	};
566 
567 	if (!pg)
568 		return br_switchdev_host_mdb(dev, mp, type);
569 
570 	br_switchdev_mdb_populate(&mdb, mp);
571 
572 	mdb.obj.orig_dev = pg->key.port->dev;
573 	switch (type) {
574 	case RTM_NEWMDB:
575 		complete_info = kmalloc(sizeof(*complete_info), GFP_ATOMIC);
576 		if (!complete_info)
577 			break;
578 		complete_info->port = pg->key.port;
579 		complete_info->ip = mp->addr;
580 		mdb.obj.complete_priv = complete_info;
581 		mdb.obj.complete = br_switchdev_mdb_complete;
582 		if (switchdev_port_obj_add(pg->key.port->dev, &mdb.obj, NULL))
583 			kfree(complete_info);
584 		break;
585 	case RTM_DELMDB:
586 		switchdev_port_obj_del(pg->key.port->dev, &mdb.obj);
587 		break;
588 	}
589 }
590 #endif
591 
592 static int
593 br_switchdev_mdb_replay(struct net_device *br_dev, struct net_device *dev,
594 			const void *ctx, bool adding, struct notifier_block *nb,
595 			struct netlink_ext_ack *extack)
596 {
597 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
598 	const struct net_bridge_mdb_entry *mp;
599 	struct switchdev_obj *obj, *tmp;
600 	struct net_bridge *br;
601 	unsigned long action;
602 	LIST_HEAD(mdb_list);
603 	int err = 0;
604 
605 	ASSERT_RTNL();
606 
607 	if (!nb)
608 		return 0;
609 
610 	if (!netif_is_bridge_master(br_dev) || !netif_is_bridge_port(dev))
611 		return -EINVAL;
612 
613 	br = netdev_priv(br_dev);
614 
615 	if (!br_opt_get(br, BROPT_MULTICAST_ENABLED))
616 		return 0;
617 
618 	/* We cannot walk over br->mdb_list protected just by the rtnl_mutex,
619 	 * because the write-side protection is br->multicast_lock. But we
620 	 * need to emulate the [ blocking ] calling context of a regular
621 	 * switchdev event, so since both br->multicast_lock and RCU read side
622 	 * critical sections are atomic, we have no choice but to pick the RCU
623 	 * read side lock, queue up all our events, leave the critical section
624 	 * and notify switchdev from blocking context.
625 	 */
626 	rcu_read_lock();
627 
628 	hlist_for_each_entry_rcu(mp, &br->mdb_list, mdb_node) {
629 		struct net_bridge_port_group __rcu * const *pp;
630 		const struct net_bridge_port_group *p;
631 
632 		if (mp->host_joined) {
633 			err = br_switchdev_mdb_queue_one(&mdb_list,
634 							 SWITCHDEV_OBJ_ID_HOST_MDB,
635 							 mp, br_dev);
636 			if (err) {
637 				rcu_read_unlock();
638 				goto out_free_mdb;
639 			}
640 		}
641 
642 		for (pp = &mp->ports; (p = rcu_dereference(*pp)) != NULL;
643 		     pp = &p->next) {
644 			if (p->key.port->dev != dev)
645 				continue;
646 
647 			err = br_switchdev_mdb_queue_one(&mdb_list,
648 							 SWITCHDEV_OBJ_ID_PORT_MDB,
649 							 mp, dev);
650 			if (err) {
651 				rcu_read_unlock();
652 				goto out_free_mdb;
653 			}
654 		}
655 	}
656 
657 	rcu_read_unlock();
658 
659 	if (adding)
660 		action = SWITCHDEV_PORT_OBJ_ADD;
661 	else
662 		action = SWITCHDEV_PORT_OBJ_DEL;
663 
664 	list_for_each_entry(obj, &mdb_list, list) {
665 		err = br_switchdev_mdb_replay_one(nb, dev,
666 						  SWITCHDEV_OBJ_PORT_MDB(obj),
667 						  action, ctx, extack);
668 		if (err)
669 			goto out_free_mdb;
670 	}
671 
672 out_free_mdb:
673 	list_for_each_entry_safe(obj, tmp, &mdb_list, list) {
674 		list_del(&obj->list);
675 		kfree(SWITCHDEV_OBJ_PORT_MDB(obj));
676 	}
677 
678 	if (err)
679 		return err;
680 #endif
681 
682 	return 0;
683 }
684 
685 static int nbp_switchdev_sync_objs(struct net_bridge_port *p, const void *ctx,
686 				   struct notifier_block *atomic_nb,
687 				   struct notifier_block *blocking_nb,
688 				   struct netlink_ext_ack *extack)
689 {
690 	struct net_device *br_dev = p->br->dev;
691 	struct net_device *dev = p->dev;
692 	int err;
693 
694 	err = br_switchdev_vlan_replay(br_dev, ctx, true, blocking_nb, extack);
695 	if (err && err != -EOPNOTSUPP)
696 		return err;
697 
698 	err = br_switchdev_mdb_replay(br_dev, dev, ctx, true, blocking_nb,
699 				      extack);
700 	if (err && err != -EOPNOTSUPP)
701 		return err;
702 
703 	err = br_switchdev_fdb_replay(br_dev, ctx, true, atomic_nb);
704 	if (err && err != -EOPNOTSUPP)
705 		return err;
706 
707 	return 0;
708 }
709 
710 static void nbp_switchdev_unsync_objs(struct net_bridge_port *p,
711 				      const void *ctx,
712 				      struct notifier_block *atomic_nb,
713 				      struct notifier_block *blocking_nb)
714 {
715 	struct net_device *br_dev = p->br->dev;
716 	struct net_device *dev = p->dev;
717 
718 	br_switchdev_fdb_replay(br_dev, ctx, false, atomic_nb);
719 
720 	br_switchdev_mdb_replay(br_dev, dev, ctx, false, blocking_nb, NULL);
721 
722 	br_switchdev_vlan_replay(br_dev, ctx, false, blocking_nb, NULL);
723 }
724 
725 /* Let the bridge know that this port is offloaded, so that it can assign a
726  * switchdev hardware domain to it.
727  */
728 int br_switchdev_port_offload(struct net_bridge_port *p,
729 			      struct net_device *dev, const void *ctx,
730 			      struct notifier_block *atomic_nb,
731 			      struct notifier_block *blocking_nb,
732 			      bool tx_fwd_offload,
733 			      struct netlink_ext_ack *extack)
734 {
735 	struct netdev_phys_item_id ppid;
736 	int err;
737 
738 	err = dev_get_port_parent_id(dev, &ppid, false);
739 	if (err)
740 		return err;
741 
742 	err = nbp_switchdev_add(p, ppid, tx_fwd_offload, extack);
743 	if (err)
744 		return err;
745 
746 	err = nbp_switchdev_sync_objs(p, ctx, atomic_nb, blocking_nb, extack);
747 	if (err)
748 		goto out_switchdev_del;
749 
750 	return 0;
751 
752 out_switchdev_del:
753 	nbp_switchdev_del(p);
754 
755 	return err;
756 }
757 
758 void br_switchdev_port_unoffload(struct net_bridge_port *p, const void *ctx,
759 				 struct notifier_block *atomic_nb,
760 				 struct notifier_block *blocking_nb)
761 {
762 	nbp_switchdev_unsync_objs(p, ctx, atomic_nb, blocking_nb);
763 
764 	nbp_switchdev_del(p);
765 }
766