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