br_switchdev.c 21 KB

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