br_switchdev.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876
  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. u8 old_flags;
  419. if (err == -EOPNOTSUPP)
  420. goto out_free;
  421. spin_lock_bh(&br->multicast_lock);
  422. mp = br_mdb_ip_get(br, &data->ip);
  423. if (!mp)
  424. goto out;
  425. for (pp = &mp->ports; (p = mlock_dereference(*pp, br)) != NULL;
  426. pp = &p->next) {
  427. if (p->key.port != port)
  428. continue;
  429. old_flags = p->flags;
  430. br_multicast_set_pg_offload_flags(p, !err);
  431. if (br_mdb_should_notify(br, old_flags ^ p->flags))
  432. br_mdb_flag_change_notify(br->dev, mp, p);
  433. }
  434. out:
  435. spin_unlock_bh(&br->multicast_lock);
  436. out_free:
  437. kfree(priv);
  438. }
  439. static void br_switchdev_mdb_populate(struct switchdev_obj_port_mdb *mdb,
  440. const struct net_bridge_mdb_entry *mp)
  441. {
  442. if (mp->addr.proto == htons(ETH_P_IP))
  443. ip_eth_mc_map(mp->addr.dst.ip4, mdb->addr);
  444. #if IS_ENABLED(CONFIG_IPV6)
  445. else if (mp->addr.proto == htons(ETH_P_IPV6))
  446. ipv6_eth_mc_map(&mp->addr.dst.ip6, mdb->addr);
  447. #endif
  448. else
  449. ether_addr_copy(mdb->addr, mp->addr.dst.mac_addr);
  450. mdb->vid = mp->addr.vid;
  451. }
  452. static void br_switchdev_host_mdb_one(struct net_device *dev,
  453. struct net_device *lower_dev,
  454. struct net_bridge_mdb_entry *mp,
  455. int type)
  456. {
  457. struct switchdev_obj_port_mdb mdb = {
  458. .obj = {
  459. .id = SWITCHDEV_OBJ_ID_HOST_MDB,
  460. .flags = SWITCHDEV_F_DEFER,
  461. .orig_dev = dev,
  462. },
  463. };
  464. br_switchdev_mdb_populate(&mdb, mp);
  465. switch (type) {
  466. case RTM_NEWMDB:
  467. switchdev_port_obj_add(lower_dev, &mdb.obj, NULL);
  468. break;
  469. case RTM_DELMDB:
  470. switchdev_port_obj_del(lower_dev, &mdb.obj);
  471. break;
  472. }
  473. }
  474. static void br_switchdev_host_mdb(struct net_device *dev,
  475. struct net_bridge_mdb_entry *mp, int type)
  476. {
  477. struct net_device *lower_dev;
  478. struct list_head *iter;
  479. netdev_for_each_lower_dev(dev, lower_dev, iter)
  480. br_switchdev_host_mdb_one(dev, lower_dev, mp, type);
  481. }
  482. static int
  483. br_switchdev_mdb_replay_one(struct notifier_block *nb, struct net_device *dev,
  484. const struct switchdev_obj_port_mdb *mdb,
  485. unsigned long action, const void *ctx,
  486. struct netlink_ext_ack *extack)
  487. {
  488. struct switchdev_notifier_port_obj_info obj_info = {
  489. .info = {
  490. .dev = dev,
  491. .extack = extack,
  492. .ctx = ctx,
  493. },
  494. .obj = &mdb->obj,
  495. };
  496. int err;
  497. err = nb->notifier_call(nb, action, &obj_info);
  498. return notifier_to_errno(err);
  499. }
  500. static int br_switchdev_mdb_queue_one(struct list_head *mdb_list,
  501. struct net_device *dev,
  502. unsigned long action,
  503. enum switchdev_obj_id id,
  504. const struct net_bridge_mdb_entry *mp,
  505. struct net_device *orig_dev)
  506. {
  507. struct switchdev_obj_port_mdb mdb = {
  508. .obj = {
  509. .id = id,
  510. .orig_dev = orig_dev,
  511. },
  512. };
  513. struct switchdev_obj_port_mdb *pmdb;
  514. br_switchdev_mdb_populate(&mdb, mp);
  515. if (action == SWITCHDEV_PORT_OBJ_ADD &&
  516. switchdev_port_obj_act_is_deferred(dev, action, &mdb.obj)) {
  517. /* This event is already in the deferred queue of
  518. * events, so this replay must be elided, lest the
  519. * driver receives duplicate events for it. This can
  520. * only happen when replaying additions, since
  521. * modifications are always immediately visible in
  522. * br->mdb_list, whereas actual event delivery may be
  523. * delayed.
  524. */
  525. return 0;
  526. }
  527. pmdb = kmemdup(&mdb, sizeof(mdb), GFP_ATOMIC);
  528. if (!pmdb)
  529. return -ENOMEM;
  530. list_add_tail(&pmdb->obj.list, mdb_list);
  531. return 0;
  532. }
  533. void br_switchdev_mdb_notify(struct net_device *dev,
  534. struct net_bridge_mdb_entry *mp,
  535. struct net_bridge_port_group *pg,
  536. int type)
  537. {
  538. struct br_switchdev_mdb_complete_info *complete_info;
  539. struct switchdev_obj_port_mdb mdb = {
  540. .obj = {
  541. .id = SWITCHDEV_OBJ_ID_PORT_MDB,
  542. .flags = SWITCHDEV_F_DEFER,
  543. },
  544. };
  545. if (!pg)
  546. return br_switchdev_host_mdb(dev, mp, type);
  547. br_switchdev_mdb_populate(&mdb, mp);
  548. mdb.obj.orig_dev = pg->key.port->dev;
  549. switch (type) {
  550. case RTM_NEWMDB:
  551. complete_info = kmalloc_obj(*complete_info, GFP_ATOMIC);
  552. if (!complete_info)
  553. break;
  554. complete_info->port = pg->key.port;
  555. complete_info->ip = mp->addr;
  556. mdb.obj.complete_priv = complete_info;
  557. mdb.obj.complete = br_switchdev_mdb_complete;
  558. if (switchdev_port_obj_add(pg->key.port->dev, &mdb.obj, NULL))
  559. kfree(complete_info);
  560. break;
  561. case RTM_DELMDB:
  562. switchdev_port_obj_del(pg->key.port->dev, &mdb.obj);
  563. break;
  564. }
  565. }
  566. #endif
  567. static int
  568. br_switchdev_mdb_replay(struct net_device *br_dev, struct net_device *dev,
  569. const void *ctx, bool adding, struct notifier_block *nb,
  570. struct netlink_ext_ack *extack)
  571. {
  572. #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
  573. const struct net_bridge_mdb_entry *mp;
  574. struct switchdev_obj *obj, *tmp;
  575. struct net_bridge *br;
  576. unsigned long action;
  577. LIST_HEAD(mdb_list);
  578. int err = 0;
  579. ASSERT_RTNL();
  580. if (!nb)
  581. return 0;
  582. if (!netif_is_bridge_master(br_dev) || !netif_is_bridge_port(dev))
  583. return -EINVAL;
  584. br = netdev_priv(br_dev);
  585. if (!br_opt_get(br, BROPT_MULTICAST_ENABLED))
  586. return 0;
  587. if (adding)
  588. action = SWITCHDEV_PORT_OBJ_ADD;
  589. else
  590. action = SWITCHDEV_PORT_OBJ_DEL;
  591. /* br_switchdev_mdb_queue_one() will take care to not queue a
  592. * replay of an event that is already pending in the switchdev
  593. * deferred queue. In order to safely determine that, there
  594. * must be no new deferred MDB notifications enqueued for the
  595. * duration of the MDB scan. Therefore, grab the write-side
  596. * lock to avoid racing with any concurrent IGMP/MLD snooping.
  597. */
  598. spin_lock_bh(&br->multicast_lock);
  599. hlist_for_each_entry(mp, &br->mdb_list, mdb_node) {
  600. struct net_bridge_port_group __rcu * const *pp;
  601. const struct net_bridge_port_group *p;
  602. if (mp->host_joined) {
  603. err = br_switchdev_mdb_queue_one(&mdb_list, dev, action,
  604. SWITCHDEV_OBJ_ID_HOST_MDB,
  605. mp, br_dev);
  606. if (err) {
  607. spin_unlock_bh(&br->multicast_lock);
  608. goto out_free_mdb;
  609. }
  610. }
  611. for (pp = &mp->ports; (p = mlock_dereference(*pp, br)) != NULL;
  612. pp = &p->next) {
  613. if (p->key.port->dev != dev)
  614. continue;
  615. err = br_switchdev_mdb_queue_one(&mdb_list, dev, action,
  616. SWITCHDEV_OBJ_ID_PORT_MDB,
  617. mp, dev);
  618. if (err) {
  619. spin_unlock_bh(&br->multicast_lock);
  620. goto out_free_mdb;
  621. }
  622. }
  623. }
  624. spin_unlock_bh(&br->multicast_lock);
  625. list_for_each_entry(obj, &mdb_list, list) {
  626. err = br_switchdev_mdb_replay_one(nb, dev,
  627. SWITCHDEV_OBJ_PORT_MDB(obj),
  628. action, ctx, extack);
  629. if (err == -EOPNOTSUPP)
  630. err = 0;
  631. if (err)
  632. goto out_free_mdb;
  633. }
  634. out_free_mdb:
  635. list_for_each_entry_safe(obj, tmp, &mdb_list, list) {
  636. list_del(&obj->list);
  637. kfree(SWITCHDEV_OBJ_PORT_MDB(obj));
  638. }
  639. if (err)
  640. return err;
  641. #endif
  642. return 0;
  643. }
  644. static int nbp_switchdev_sync_objs(struct net_bridge_port *p, const void *ctx,
  645. struct notifier_block *atomic_nb,
  646. struct notifier_block *blocking_nb,
  647. struct netlink_ext_ack *extack)
  648. {
  649. struct net_device *br_dev = p->br->dev;
  650. struct net_device *dev = p->dev;
  651. int err;
  652. err = br_switchdev_vlan_replay(br_dev, ctx, true, blocking_nb, extack);
  653. if (err && err != -EOPNOTSUPP)
  654. return err;
  655. err = br_switchdev_mdb_replay(br_dev, dev, ctx, true, blocking_nb,
  656. extack);
  657. if (err) {
  658. /* -EOPNOTSUPP not propagated from MDB replay. */
  659. return err;
  660. }
  661. err = br_switchdev_fdb_replay(br_dev, ctx, true, atomic_nb);
  662. if (err && err != -EOPNOTSUPP)
  663. return err;
  664. return 0;
  665. }
  666. static void nbp_switchdev_unsync_objs(struct net_bridge_port *p,
  667. const void *ctx,
  668. struct notifier_block *atomic_nb,
  669. struct notifier_block *blocking_nb)
  670. {
  671. struct net_device *br_dev = p->br->dev;
  672. struct net_device *dev = p->dev;
  673. br_switchdev_fdb_replay(br_dev, ctx, false, atomic_nb);
  674. br_switchdev_mdb_replay(br_dev, dev, ctx, false, blocking_nb, NULL);
  675. br_switchdev_vlan_replay(br_dev, ctx, false, blocking_nb, NULL);
  676. /* Make sure that the device leaving this bridge has seen all
  677. * relevant events before it is disassociated. In the normal
  678. * case, when the device is directly attached to the bridge,
  679. * this is covered by del_nbp(). If the association was indirect
  680. * however, e.g. via a team or bond, and the device is leaving
  681. * that intermediate device, then the bridge port remains in
  682. * place.
  683. */
  684. switchdev_deferred_process();
  685. }
  686. /* Let the bridge know that this port is offloaded, so that it can assign a
  687. * switchdev hardware domain to it.
  688. */
  689. int br_switchdev_port_offload(struct net_bridge_port *p,
  690. struct net_device *dev, const void *ctx,
  691. struct notifier_block *atomic_nb,
  692. struct notifier_block *blocking_nb,
  693. bool tx_fwd_offload,
  694. struct netlink_ext_ack *extack)
  695. {
  696. struct netdev_phys_item_id ppid;
  697. int err;
  698. err = netif_get_port_parent_id(dev, &ppid, false);
  699. if (err)
  700. return err;
  701. err = nbp_switchdev_add(p, ppid, tx_fwd_offload, extack);
  702. if (err)
  703. return err;
  704. err = nbp_switchdev_sync_objs(p, ctx, atomic_nb, blocking_nb, extack);
  705. if (err)
  706. goto out_switchdev_del;
  707. return 0;
  708. out_switchdev_del:
  709. nbp_switchdev_del(p);
  710. return err;
  711. }
  712. void br_switchdev_port_unoffload(struct net_bridge_port *p, const void *ctx,
  713. struct notifier_block *atomic_nb,
  714. struct notifier_block *blocking_nb)
  715. {
  716. nbp_switchdev_unsync_objs(p, ctx, atomic_nb, blocking_nb);
  717. nbp_switchdev_del(p);
  718. }
  719. int br_switchdev_port_replay(struct net_bridge_port *p,
  720. struct net_device *dev, const void *ctx,
  721. struct notifier_block *atomic_nb,
  722. struct notifier_block *blocking_nb,
  723. struct netlink_ext_ack *extack)
  724. {
  725. return nbp_switchdev_sync_objs(p, ctx, atomic_nb, blocking_nb, extack);
  726. }