xref: /openbmc/linux/net/bridge/br_switchdev.c (revision bacf743e)
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 br_switchdev_vlan_attr_replay(struct net_device *br_dev,
335 					 const void *ctx,
336 					 struct notifier_block *nb,
337 					 struct netlink_ext_ack *extack)
338 {
339 	struct switchdev_notifier_port_attr_info attr_info = {
340 		.info = {
341 			.dev = br_dev,
342 			.extack = extack,
343 			.ctx = ctx,
344 		},
345 	};
346 	struct net_bridge *br = netdev_priv(br_dev);
347 	struct net_bridge_vlan_group *vg;
348 	struct switchdev_attr attr;
349 	struct net_bridge_vlan *v;
350 	int err;
351 
352 	attr_info.attr = &attr;
353 	attr.orig_dev = br_dev;
354 
355 	vg = br_vlan_group(br);
356 
357 	list_for_each_entry(v, &vg->vlan_list, vlist) {
358 		if (v->msti) {
359 			attr.id = SWITCHDEV_ATTR_ID_VLAN_MSTI;
360 			attr.u.vlan_msti.vid = v->vid;
361 			attr.u.vlan_msti.msti = v->msti;
362 
363 			err = nb->notifier_call(nb, SWITCHDEV_PORT_ATTR_SET,
364 						&attr_info);
365 			err = notifier_to_errno(err);
366 			if (err)
367 				return err;
368 		}
369 	}
370 
371 	return 0;
372 }
373 
374 static int
375 br_switchdev_vlan_replay_one(struct notifier_block *nb,
376 			     struct net_device *dev,
377 			     struct switchdev_obj_port_vlan *vlan,
378 			     const void *ctx, unsigned long action,
379 			     struct netlink_ext_ack *extack)
380 {
381 	struct switchdev_notifier_port_obj_info obj_info = {
382 		.info = {
383 			.dev = dev,
384 			.extack = extack,
385 			.ctx = ctx,
386 		},
387 		.obj = &vlan->obj,
388 	};
389 	int err;
390 
391 	err = nb->notifier_call(nb, action, &obj_info);
392 	return notifier_to_errno(err);
393 }
394 
395 static int br_switchdev_vlan_replay_group(struct notifier_block *nb,
396 					  struct net_device *dev,
397 					  struct net_bridge_vlan_group *vg,
398 					  const void *ctx, unsigned long action,
399 					  struct netlink_ext_ack *extack)
400 {
401 	struct net_bridge_vlan *v;
402 	int err = 0;
403 	u16 pvid;
404 
405 	if (!vg)
406 		return 0;
407 
408 	pvid = br_get_pvid(vg);
409 
410 	list_for_each_entry(v, &vg->vlan_list, vlist) {
411 		struct switchdev_obj_port_vlan vlan = {
412 			.obj.orig_dev = dev,
413 			.obj.id = SWITCHDEV_OBJ_ID_PORT_VLAN,
414 			.flags = br_vlan_flags(v, pvid),
415 			.vid = v->vid,
416 		};
417 
418 		if (!br_vlan_should_use(v))
419 			continue;
420 
421 		err = br_switchdev_vlan_replay_one(nb, dev, &vlan, ctx,
422 						   action, extack);
423 		if (err)
424 			return err;
425 	}
426 
427 	return 0;
428 }
429 
430 static int br_switchdev_vlan_replay(struct net_device *br_dev,
431 				    const void *ctx, bool adding,
432 				    struct notifier_block *nb,
433 				    struct netlink_ext_ack *extack)
434 {
435 	struct net_bridge *br = netdev_priv(br_dev);
436 	struct net_bridge_port *p;
437 	unsigned long action;
438 	int err;
439 
440 	ASSERT_RTNL();
441 
442 	if (!nb)
443 		return 0;
444 
445 	if (!netif_is_bridge_master(br_dev))
446 		return -EINVAL;
447 
448 	if (adding)
449 		action = SWITCHDEV_PORT_OBJ_ADD;
450 	else
451 		action = SWITCHDEV_PORT_OBJ_DEL;
452 
453 	err = br_switchdev_vlan_replay_group(nb, br_dev, br_vlan_group(br),
454 					     ctx, action, extack);
455 	if (err)
456 		return err;
457 
458 	list_for_each_entry(p, &br->port_list, list) {
459 		struct net_device *dev = p->dev;
460 
461 		err = br_switchdev_vlan_replay_group(nb, dev,
462 						     nbp_vlan_group(p),
463 						     ctx, action, extack);
464 		if (err)
465 			return err;
466 	}
467 
468 	if (adding) {
469 		err = br_switchdev_vlan_attr_replay(br_dev, ctx, nb, extack);
470 		if (err)
471 			return err;
472 	}
473 
474 	return 0;
475 }
476 
477 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
478 struct br_switchdev_mdb_complete_info {
479 	struct net_bridge_port *port;
480 	struct br_ip ip;
481 };
482 
483 static void br_switchdev_mdb_complete(struct net_device *dev, int err, void *priv)
484 {
485 	struct br_switchdev_mdb_complete_info *data = priv;
486 	struct net_bridge_port_group __rcu **pp;
487 	struct net_bridge_port_group *p;
488 	struct net_bridge_mdb_entry *mp;
489 	struct net_bridge_port *port = data->port;
490 	struct net_bridge *br = port->br;
491 
492 	if (err)
493 		goto err;
494 
495 	spin_lock_bh(&br->multicast_lock);
496 	mp = br_mdb_ip_get(br, &data->ip);
497 	if (!mp)
498 		goto out;
499 	for (pp = &mp->ports; (p = mlock_dereference(*pp, br)) != NULL;
500 	     pp = &p->next) {
501 		if (p->key.port != port)
502 			continue;
503 		p->flags |= MDB_PG_FLAGS_OFFLOAD;
504 	}
505 out:
506 	spin_unlock_bh(&br->multicast_lock);
507 err:
508 	kfree(priv);
509 }
510 
511 static void br_switchdev_mdb_populate(struct switchdev_obj_port_mdb *mdb,
512 				      const struct net_bridge_mdb_entry *mp)
513 {
514 	if (mp->addr.proto == htons(ETH_P_IP))
515 		ip_eth_mc_map(mp->addr.dst.ip4, mdb->addr);
516 #if IS_ENABLED(CONFIG_IPV6)
517 	else if (mp->addr.proto == htons(ETH_P_IPV6))
518 		ipv6_eth_mc_map(&mp->addr.dst.ip6, mdb->addr);
519 #endif
520 	else
521 		ether_addr_copy(mdb->addr, mp->addr.dst.mac_addr);
522 
523 	mdb->vid = mp->addr.vid;
524 }
525 
526 static void br_switchdev_host_mdb_one(struct net_device *dev,
527 				      struct net_device *lower_dev,
528 				      struct net_bridge_mdb_entry *mp,
529 				      int type)
530 {
531 	struct switchdev_obj_port_mdb mdb = {
532 		.obj = {
533 			.id = SWITCHDEV_OBJ_ID_HOST_MDB,
534 			.flags = SWITCHDEV_F_DEFER,
535 			.orig_dev = dev,
536 		},
537 	};
538 
539 	br_switchdev_mdb_populate(&mdb, mp);
540 
541 	switch (type) {
542 	case RTM_NEWMDB:
543 		switchdev_port_obj_add(lower_dev, &mdb.obj, NULL);
544 		break;
545 	case RTM_DELMDB:
546 		switchdev_port_obj_del(lower_dev, &mdb.obj);
547 		break;
548 	}
549 }
550 
551 static void br_switchdev_host_mdb(struct net_device *dev,
552 				  struct net_bridge_mdb_entry *mp, int type)
553 {
554 	struct net_device *lower_dev;
555 	struct list_head *iter;
556 
557 	netdev_for_each_lower_dev(dev, lower_dev, iter)
558 		br_switchdev_host_mdb_one(dev, lower_dev, mp, type);
559 }
560 
561 static int
562 br_switchdev_mdb_replay_one(struct notifier_block *nb, struct net_device *dev,
563 			    const struct switchdev_obj_port_mdb *mdb,
564 			    unsigned long action, const void *ctx,
565 			    struct netlink_ext_ack *extack)
566 {
567 	struct switchdev_notifier_port_obj_info obj_info = {
568 		.info = {
569 			.dev = dev,
570 			.extack = extack,
571 			.ctx = ctx,
572 		},
573 		.obj = &mdb->obj,
574 	};
575 	int err;
576 
577 	err = nb->notifier_call(nb, action, &obj_info);
578 	return notifier_to_errno(err);
579 }
580 
581 static int br_switchdev_mdb_queue_one(struct list_head *mdb_list,
582 				      enum switchdev_obj_id id,
583 				      const struct net_bridge_mdb_entry *mp,
584 				      struct net_device *orig_dev)
585 {
586 	struct switchdev_obj_port_mdb *mdb;
587 
588 	mdb = kzalloc(sizeof(*mdb), GFP_ATOMIC);
589 	if (!mdb)
590 		return -ENOMEM;
591 
592 	mdb->obj.id = id;
593 	mdb->obj.orig_dev = orig_dev;
594 	br_switchdev_mdb_populate(mdb, mp);
595 	list_add_tail(&mdb->obj.list, mdb_list);
596 
597 	return 0;
598 }
599 
600 void br_switchdev_mdb_notify(struct net_device *dev,
601 			     struct net_bridge_mdb_entry *mp,
602 			     struct net_bridge_port_group *pg,
603 			     int type)
604 {
605 	struct br_switchdev_mdb_complete_info *complete_info;
606 	struct switchdev_obj_port_mdb mdb = {
607 		.obj = {
608 			.id = SWITCHDEV_OBJ_ID_PORT_MDB,
609 			.flags = SWITCHDEV_F_DEFER,
610 		},
611 	};
612 
613 	if (!pg)
614 		return br_switchdev_host_mdb(dev, mp, type);
615 
616 	br_switchdev_mdb_populate(&mdb, mp);
617 
618 	mdb.obj.orig_dev = pg->key.port->dev;
619 	switch (type) {
620 	case RTM_NEWMDB:
621 		complete_info = kmalloc(sizeof(*complete_info), GFP_ATOMIC);
622 		if (!complete_info)
623 			break;
624 		complete_info->port = pg->key.port;
625 		complete_info->ip = mp->addr;
626 		mdb.obj.complete_priv = complete_info;
627 		mdb.obj.complete = br_switchdev_mdb_complete;
628 		if (switchdev_port_obj_add(pg->key.port->dev, &mdb.obj, NULL))
629 			kfree(complete_info);
630 		break;
631 	case RTM_DELMDB:
632 		switchdev_port_obj_del(pg->key.port->dev, &mdb.obj);
633 		break;
634 	}
635 }
636 #endif
637 
638 static int
639 br_switchdev_mdb_replay(struct net_device *br_dev, struct net_device *dev,
640 			const void *ctx, bool adding, struct notifier_block *nb,
641 			struct netlink_ext_ack *extack)
642 {
643 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
644 	const struct net_bridge_mdb_entry *mp;
645 	struct switchdev_obj *obj, *tmp;
646 	struct net_bridge *br;
647 	unsigned long action;
648 	LIST_HEAD(mdb_list);
649 	int err = 0;
650 
651 	ASSERT_RTNL();
652 
653 	if (!nb)
654 		return 0;
655 
656 	if (!netif_is_bridge_master(br_dev) || !netif_is_bridge_port(dev))
657 		return -EINVAL;
658 
659 	br = netdev_priv(br_dev);
660 
661 	if (!br_opt_get(br, BROPT_MULTICAST_ENABLED))
662 		return 0;
663 
664 	/* We cannot walk over br->mdb_list protected just by the rtnl_mutex,
665 	 * because the write-side protection is br->multicast_lock. But we
666 	 * need to emulate the [ blocking ] calling context of a regular
667 	 * switchdev event, so since both br->multicast_lock and RCU read side
668 	 * critical sections are atomic, we have no choice but to pick the RCU
669 	 * read side lock, queue up all our events, leave the critical section
670 	 * and notify switchdev from blocking context.
671 	 */
672 	rcu_read_lock();
673 
674 	hlist_for_each_entry_rcu(mp, &br->mdb_list, mdb_node) {
675 		struct net_bridge_port_group __rcu * const *pp;
676 		const struct net_bridge_port_group *p;
677 
678 		if (mp->host_joined) {
679 			err = br_switchdev_mdb_queue_one(&mdb_list,
680 							 SWITCHDEV_OBJ_ID_HOST_MDB,
681 							 mp, br_dev);
682 			if (err) {
683 				rcu_read_unlock();
684 				goto out_free_mdb;
685 			}
686 		}
687 
688 		for (pp = &mp->ports; (p = rcu_dereference(*pp)) != NULL;
689 		     pp = &p->next) {
690 			if (p->key.port->dev != dev)
691 				continue;
692 
693 			err = br_switchdev_mdb_queue_one(&mdb_list,
694 							 SWITCHDEV_OBJ_ID_PORT_MDB,
695 							 mp, dev);
696 			if (err) {
697 				rcu_read_unlock();
698 				goto out_free_mdb;
699 			}
700 		}
701 	}
702 
703 	rcu_read_unlock();
704 
705 	if (adding)
706 		action = SWITCHDEV_PORT_OBJ_ADD;
707 	else
708 		action = SWITCHDEV_PORT_OBJ_DEL;
709 
710 	list_for_each_entry(obj, &mdb_list, list) {
711 		err = br_switchdev_mdb_replay_one(nb, dev,
712 						  SWITCHDEV_OBJ_PORT_MDB(obj),
713 						  action, ctx, extack);
714 		if (err)
715 			goto out_free_mdb;
716 	}
717 
718 out_free_mdb:
719 	list_for_each_entry_safe(obj, tmp, &mdb_list, list) {
720 		list_del(&obj->list);
721 		kfree(SWITCHDEV_OBJ_PORT_MDB(obj));
722 	}
723 
724 	if (err)
725 		return err;
726 #endif
727 
728 	return 0;
729 }
730 
731 static int nbp_switchdev_sync_objs(struct net_bridge_port *p, const void *ctx,
732 				   struct notifier_block *atomic_nb,
733 				   struct notifier_block *blocking_nb,
734 				   struct netlink_ext_ack *extack)
735 {
736 	struct net_device *br_dev = p->br->dev;
737 	struct net_device *dev = p->dev;
738 	int err;
739 
740 	err = br_switchdev_vlan_replay(br_dev, ctx, true, blocking_nb, extack);
741 	if (err && err != -EOPNOTSUPP)
742 		return err;
743 
744 	err = br_switchdev_mdb_replay(br_dev, dev, ctx, true, blocking_nb,
745 				      extack);
746 	if (err && err != -EOPNOTSUPP)
747 		return err;
748 
749 	err = br_switchdev_fdb_replay(br_dev, ctx, true, atomic_nb);
750 	if (err && err != -EOPNOTSUPP)
751 		return err;
752 
753 	return 0;
754 }
755 
756 static void nbp_switchdev_unsync_objs(struct net_bridge_port *p,
757 				      const void *ctx,
758 				      struct notifier_block *atomic_nb,
759 				      struct notifier_block *blocking_nb)
760 {
761 	struct net_device *br_dev = p->br->dev;
762 	struct net_device *dev = p->dev;
763 
764 	br_switchdev_fdb_replay(br_dev, ctx, false, atomic_nb);
765 
766 	br_switchdev_mdb_replay(br_dev, dev, ctx, false, blocking_nb, NULL);
767 
768 	br_switchdev_vlan_replay(br_dev, ctx, false, blocking_nb, NULL);
769 }
770 
771 /* Let the bridge know that this port is offloaded, so that it can assign a
772  * switchdev hardware domain to it.
773  */
774 int br_switchdev_port_offload(struct net_bridge_port *p,
775 			      struct net_device *dev, const void *ctx,
776 			      struct notifier_block *atomic_nb,
777 			      struct notifier_block *blocking_nb,
778 			      bool tx_fwd_offload,
779 			      struct netlink_ext_ack *extack)
780 {
781 	struct netdev_phys_item_id ppid;
782 	int err;
783 
784 	err = dev_get_port_parent_id(dev, &ppid, false);
785 	if (err)
786 		return err;
787 
788 	err = nbp_switchdev_add(p, ppid, tx_fwd_offload, extack);
789 	if (err)
790 		return err;
791 
792 	err = nbp_switchdev_sync_objs(p, ctx, atomic_nb, blocking_nb, extack);
793 	if (err)
794 		goto out_switchdev_del;
795 
796 	return 0;
797 
798 out_switchdev_del:
799 	nbp_switchdev_del(p);
800 
801 	return err;
802 }
803 
804 void br_switchdev_port_unoffload(struct net_bridge_port *p, const void *ctx,
805 				 struct notifier_block *atomic_nb,
806 				 struct notifier_block *blocking_nb)
807 {
808 	nbp_switchdev_unsync_objs(p, ctx, atomic_nb, blocking_nb);
809 
810 	nbp_switchdev_del(p);
811 }
812