1 // SPDX-License-Identifier: GPL-2.0-or-later 2 3 #include <linux/mrp_bridge.h> 4 #include "br_private_mrp.h" 5 6 static const u8 mrp_test_dmac[ETH_ALEN] = { 0x1, 0x15, 0x4e, 0x0, 0x0, 0x1 }; 7 8 static struct net_bridge_port *br_mrp_get_port(struct net_bridge *br, 9 u32 ifindex) 10 { 11 struct net_bridge_port *res = NULL; 12 struct net_bridge_port *port; 13 14 list_for_each_entry(port, &br->port_list, list) { 15 if (port->dev->ifindex == ifindex) { 16 res = port; 17 break; 18 } 19 } 20 21 return res; 22 } 23 24 static struct br_mrp *br_mrp_find_id(struct net_bridge *br, u32 ring_id) 25 { 26 struct br_mrp *res = NULL; 27 struct br_mrp *mrp; 28 29 list_for_each_entry_rcu(mrp, &br->mrp_list, list, 30 lockdep_rtnl_is_held()) { 31 if (mrp->ring_id == ring_id) { 32 res = mrp; 33 break; 34 } 35 } 36 37 return res; 38 } 39 40 static bool br_mrp_unique_ifindex(struct net_bridge *br, u32 ifindex) 41 { 42 struct br_mrp *mrp; 43 44 list_for_each_entry_rcu(mrp, &br->mrp_list, list, 45 lockdep_rtnl_is_held()) { 46 struct net_bridge_port *p; 47 48 p = rtnl_dereference(mrp->p_port); 49 if (p && p->dev->ifindex == ifindex) 50 return false; 51 52 p = rtnl_dereference(mrp->s_port); 53 if (p && p->dev->ifindex == ifindex) 54 return false; 55 } 56 57 return true; 58 } 59 60 static struct br_mrp *br_mrp_find_port(struct net_bridge *br, 61 struct net_bridge_port *p) 62 { 63 struct br_mrp *res = NULL; 64 struct br_mrp *mrp; 65 66 list_for_each_entry_rcu(mrp, &br->mrp_list, list, 67 lockdep_rtnl_is_held()) { 68 if (rcu_access_pointer(mrp->p_port) == p || 69 rcu_access_pointer(mrp->s_port) == p) { 70 res = mrp; 71 break; 72 } 73 } 74 75 return res; 76 } 77 78 static int br_mrp_next_seq(struct br_mrp *mrp) 79 { 80 mrp->seq_id++; 81 return mrp->seq_id; 82 } 83 84 static struct sk_buff *br_mrp_skb_alloc(struct net_bridge_port *p, 85 const u8 *src, const u8 *dst) 86 { 87 struct ethhdr *eth_hdr; 88 struct sk_buff *skb; 89 u16 *version; 90 91 skb = dev_alloc_skb(MRP_MAX_FRAME_LENGTH); 92 if (!skb) 93 return NULL; 94 95 skb->dev = p->dev; 96 skb->protocol = htons(ETH_P_MRP); 97 skb->priority = MRP_FRAME_PRIO; 98 skb_reserve(skb, sizeof(*eth_hdr)); 99 100 eth_hdr = skb_push(skb, sizeof(*eth_hdr)); 101 ether_addr_copy(eth_hdr->h_dest, dst); 102 ether_addr_copy(eth_hdr->h_source, src); 103 eth_hdr->h_proto = htons(ETH_P_MRP); 104 105 version = skb_put(skb, sizeof(*version)); 106 *version = cpu_to_be16(MRP_VERSION); 107 108 return skb; 109 } 110 111 static void br_mrp_skb_tlv(struct sk_buff *skb, 112 enum br_mrp_tlv_header_type type, 113 u8 length) 114 { 115 struct br_mrp_tlv_hdr *hdr; 116 117 hdr = skb_put(skb, sizeof(*hdr)); 118 hdr->type = type; 119 hdr->length = length; 120 } 121 122 static void br_mrp_skb_common(struct sk_buff *skb, struct br_mrp *mrp) 123 { 124 struct br_mrp_common_hdr *hdr; 125 126 br_mrp_skb_tlv(skb, BR_MRP_TLV_HEADER_COMMON, sizeof(*hdr)); 127 128 hdr = skb_put(skb, sizeof(*hdr)); 129 hdr->seq_id = cpu_to_be16(br_mrp_next_seq(mrp)); 130 memset(hdr->domain, 0xff, MRP_DOMAIN_UUID_LENGTH); 131 } 132 133 static struct sk_buff *br_mrp_alloc_test_skb(struct br_mrp *mrp, 134 struct net_bridge_port *p, 135 enum br_mrp_port_role_type port_role) 136 { 137 struct br_mrp_ring_test_hdr *hdr = NULL; 138 struct sk_buff *skb = NULL; 139 140 if (!p) 141 return NULL; 142 143 skb = br_mrp_skb_alloc(p, p->dev->dev_addr, mrp_test_dmac); 144 if (!skb) 145 return NULL; 146 147 br_mrp_skb_tlv(skb, BR_MRP_TLV_HEADER_RING_TEST, sizeof(*hdr)); 148 hdr = skb_put(skb, sizeof(*hdr)); 149 150 hdr->prio = cpu_to_be16(mrp->prio); 151 ether_addr_copy(hdr->sa, p->br->dev->dev_addr); 152 hdr->port_role = cpu_to_be16(port_role); 153 hdr->state = cpu_to_be16(mrp->ring_state); 154 hdr->transitions = cpu_to_be16(mrp->ring_transitions); 155 hdr->timestamp = cpu_to_be32(jiffies_to_msecs(jiffies)); 156 157 br_mrp_skb_common(skb, mrp); 158 br_mrp_skb_tlv(skb, BR_MRP_TLV_HEADER_END, 0x0); 159 160 return skb; 161 } 162 163 /* This function is continuously called in the following cases: 164 * - when node role is MRM, in this case test_monitor is always set to false 165 * because it needs to notify the userspace that the ring is open and needs to 166 * send MRP_Test frames 167 * - when node role is MRA, there are 2 subcases: 168 * - when MRA behaves as MRM, in this case is similar with MRM role 169 * - when MRA behaves as MRC, in this case test_monitor is set to true, 170 * because it needs to detect when it stops seeing MRP_Test frames 171 * from MRM node but it doesn't need to send MRP_Test frames. 172 */ 173 static void br_mrp_test_work_expired(struct work_struct *work) 174 { 175 struct delayed_work *del_work = to_delayed_work(work); 176 struct br_mrp *mrp = container_of(del_work, struct br_mrp, test_work); 177 struct net_bridge_port *p; 178 bool notify_open = false; 179 struct sk_buff *skb; 180 181 if (time_before_eq(mrp->test_end, jiffies)) 182 return; 183 184 if (mrp->test_count_miss < mrp->test_max_miss) { 185 mrp->test_count_miss++; 186 } else { 187 /* Notify that the ring is open only if the ring state is 188 * closed, otherwise it would continue to notify at every 189 * interval. 190 * Also notify that the ring is open when the node has the 191 * role MRA and behaves as MRC. The reason is that the 192 * userspace needs to know when the MRM stopped sending 193 * MRP_Test frames so that the current node to try to take 194 * the role of a MRM. 195 */ 196 if (mrp->ring_state == BR_MRP_RING_STATE_CLOSED || 197 mrp->test_monitor) 198 notify_open = true; 199 } 200 201 rcu_read_lock(); 202 203 p = rcu_dereference(mrp->p_port); 204 if (p) { 205 if (!mrp->test_monitor) { 206 skb = br_mrp_alloc_test_skb(mrp, p, 207 BR_MRP_PORT_ROLE_PRIMARY); 208 if (!skb) 209 goto out; 210 211 skb_reset_network_header(skb); 212 dev_queue_xmit(skb); 213 } 214 215 if (notify_open && !mrp->ring_role_offloaded) 216 br_mrp_port_open(p->dev, true); 217 } 218 219 p = rcu_dereference(mrp->s_port); 220 if (p) { 221 if (!mrp->test_monitor) { 222 skb = br_mrp_alloc_test_skb(mrp, p, 223 BR_MRP_PORT_ROLE_SECONDARY); 224 if (!skb) 225 goto out; 226 227 skb_reset_network_header(skb); 228 dev_queue_xmit(skb); 229 } 230 231 if (notify_open && !mrp->ring_role_offloaded) 232 br_mrp_port_open(p->dev, true); 233 } 234 235 out: 236 rcu_read_unlock(); 237 238 queue_delayed_work(system_wq, &mrp->test_work, 239 usecs_to_jiffies(mrp->test_interval)); 240 } 241 242 /* Deletes the MRP instance. 243 * note: called under rtnl_lock 244 */ 245 static void br_mrp_del_impl(struct net_bridge *br, struct br_mrp *mrp) 246 { 247 struct net_bridge_port *p; 248 u8 state; 249 250 /* Stop sending MRP_Test frames */ 251 cancel_delayed_work_sync(&mrp->test_work); 252 br_mrp_switchdev_send_ring_test(br, mrp, 0, 0, 0, 0); 253 254 br_mrp_switchdev_del(br, mrp); 255 256 /* Reset the ports */ 257 p = rtnl_dereference(mrp->p_port); 258 if (p) { 259 spin_lock_bh(&br->lock); 260 state = netif_running(br->dev) ? 261 BR_STATE_FORWARDING : BR_STATE_DISABLED; 262 p->state = state; 263 p->flags &= ~BR_MRP_AWARE; 264 spin_unlock_bh(&br->lock); 265 br_mrp_port_switchdev_set_state(p, state); 266 rcu_assign_pointer(mrp->p_port, NULL); 267 } 268 269 p = rtnl_dereference(mrp->s_port); 270 if (p) { 271 spin_lock_bh(&br->lock); 272 state = netif_running(br->dev) ? 273 BR_STATE_FORWARDING : BR_STATE_DISABLED; 274 p->state = state; 275 p->flags &= ~BR_MRP_AWARE; 276 spin_unlock_bh(&br->lock); 277 br_mrp_port_switchdev_set_state(p, state); 278 rcu_assign_pointer(mrp->s_port, NULL); 279 } 280 281 list_del_rcu(&mrp->list); 282 kfree_rcu(mrp, rcu); 283 } 284 285 /* Adds a new MRP instance. 286 * note: called under rtnl_lock 287 */ 288 int br_mrp_add(struct net_bridge *br, struct br_mrp_instance *instance) 289 { 290 struct net_bridge_port *p; 291 struct br_mrp *mrp; 292 int err; 293 294 /* If the ring exists, it is not possible to create another one with the 295 * same ring_id 296 */ 297 mrp = br_mrp_find_id(br, instance->ring_id); 298 if (mrp) 299 return -EINVAL; 300 301 if (!br_mrp_get_port(br, instance->p_ifindex) || 302 !br_mrp_get_port(br, instance->s_ifindex)) 303 return -EINVAL; 304 305 /* It is not possible to have the same port part of multiple rings */ 306 if (!br_mrp_unique_ifindex(br, instance->p_ifindex) || 307 !br_mrp_unique_ifindex(br, instance->s_ifindex)) 308 return -EINVAL; 309 310 mrp = kzalloc(sizeof(*mrp), GFP_KERNEL); 311 if (!mrp) 312 return -ENOMEM; 313 314 mrp->ring_id = instance->ring_id; 315 mrp->prio = instance->prio; 316 317 p = br_mrp_get_port(br, instance->p_ifindex); 318 spin_lock_bh(&br->lock); 319 p->state = BR_STATE_FORWARDING; 320 p->flags |= BR_MRP_AWARE; 321 spin_unlock_bh(&br->lock); 322 rcu_assign_pointer(mrp->p_port, p); 323 324 p = br_mrp_get_port(br, instance->s_ifindex); 325 spin_lock_bh(&br->lock); 326 p->state = BR_STATE_FORWARDING; 327 p->flags |= BR_MRP_AWARE; 328 spin_unlock_bh(&br->lock); 329 rcu_assign_pointer(mrp->s_port, p); 330 331 INIT_DELAYED_WORK(&mrp->test_work, br_mrp_test_work_expired); 332 list_add_tail_rcu(&mrp->list, &br->mrp_list); 333 334 err = br_mrp_switchdev_add(br, mrp); 335 if (err) 336 goto delete_mrp; 337 338 return 0; 339 340 delete_mrp: 341 br_mrp_del_impl(br, mrp); 342 343 return err; 344 } 345 346 /* Deletes the MRP instance from which the port is part of 347 * note: called under rtnl_lock 348 */ 349 void br_mrp_port_del(struct net_bridge *br, struct net_bridge_port *p) 350 { 351 struct br_mrp *mrp = br_mrp_find_port(br, p); 352 353 /* If the port is not part of a MRP instance just bail out */ 354 if (!mrp) 355 return; 356 357 br_mrp_del_impl(br, mrp); 358 } 359 360 /* Deletes existing MRP instance based on ring_id 361 * note: called under rtnl_lock 362 */ 363 int br_mrp_del(struct net_bridge *br, struct br_mrp_instance *instance) 364 { 365 struct br_mrp *mrp = br_mrp_find_id(br, instance->ring_id); 366 367 if (!mrp) 368 return -EINVAL; 369 370 br_mrp_del_impl(br, mrp); 371 372 return 0; 373 } 374 375 /* Set port state, port state can be forwarding, blocked or disabled 376 * note: already called with rtnl_lock 377 */ 378 int br_mrp_set_port_state(struct net_bridge_port *p, 379 enum br_mrp_port_state_type state) 380 { 381 if (!p || !(p->flags & BR_MRP_AWARE)) 382 return -EINVAL; 383 384 spin_lock_bh(&p->br->lock); 385 386 if (state == BR_MRP_PORT_STATE_FORWARDING) 387 p->state = BR_STATE_FORWARDING; 388 else 389 p->state = BR_STATE_BLOCKING; 390 391 spin_unlock_bh(&p->br->lock); 392 393 br_mrp_port_switchdev_set_state(p, state); 394 395 return 0; 396 } 397 398 /* Set port role, port role can be primary or secondary 399 * note: already called with rtnl_lock 400 */ 401 int br_mrp_set_port_role(struct net_bridge_port *p, 402 enum br_mrp_port_role_type role) 403 { 404 struct br_mrp *mrp; 405 406 if (!p || !(p->flags & BR_MRP_AWARE)) 407 return -EINVAL; 408 409 mrp = br_mrp_find_port(p->br, p); 410 411 if (!mrp) 412 return -EINVAL; 413 414 switch (role) { 415 case BR_MRP_PORT_ROLE_PRIMARY: 416 rcu_assign_pointer(mrp->p_port, p); 417 break; 418 case BR_MRP_PORT_ROLE_SECONDARY: 419 rcu_assign_pointer(mrp->s_port, p); 420 break; 421 default: 422 return -EINVAL; 423 } 424 425 br_mrp_port_switchdev_set_role(p, role); 426 427 return 0; 428 } 429 430 /* Set ring state, ring state can be only Open or Closed 431 * note: already called with rtnl_lock 432 */ 433 int br_mrp_set_ring_state(struct net_bridge *br, 434 struct br_mrp_ring_state *state) 435 { 436 struct br_mrp *mrp = br_mrp_find_id(br, state->ring_id); 437 438 if (!mrp) 439 return -EINVAL; 440 441 if (mrp->ring_state == BR_MRP_RING_STATE_CLOSED && 442 state->ring_state != BR_MRP_RING_STATE_CLOSED) 443 mrp->ring_transitions++; 444 445 mrp->ring_state = state->ring_state; 446 447 br_mrp_switchdev_set_ring_state(br, mrp, state->ring_state); 448 449 return 0; 450 } 451 452 /* Set ring role, ring role can be only MRM(Media Redundancy Manager) or 453 * MRC(Media Redundancy Client). 454 * note: already called with rtnl_lock 455 */ 456 int br_mrp_set_ring_role(struct net_bridge *br, 457 struct br_mrp_ring_role *role) 458 { 459 struct br_mrp *mrp = br_mrp_find_id(br, role->ring_id); 460 int err; 461 462 if (!mrp) 463 return -EINVAL; 464 465 mrp->ring_role = role->ring_role; 466 467 /* If there is an error just bailed out */ 468 err = br_mrp_switchdev_set_ring_role(br, mrp, role->ring_role); 469 if (err && err != -EOPNOTSUPP) 470 return err; 471 472 /* Now detect if the HW actually applied the role or not. If the HW 473 * applied the role it means that the SW will not to do those operations 474 * anymore. For example if the role ir MRM then the HW will notify the 475 * SW when ring is open, but if the is not pushed to the HW the SW will 476 * need to detect when the ring is open 477 */ 478 mrp->ring_role_offloaded = err == -EOPNOTSUPP ? 0 : 1; 479 480 return 0; 481 } 482 483 /* Start to generate or monitor MRP test frames, the frames are generated by 484 * HW and if it fails, they are generated by the SW. 485 * note: already called with rtnl_lock 486 */ 487 int br_mrp_start_test(struct net_bridge *br, 488 struct br_mrp_start_test *test) 489 { 490 struct br_mrp *mrp = br_mrp_find_id(br, test->ring_id); 491 492 if (!mrp) 493 return -EINVAL; 494 495 /* Try to push it to the HW and if it fails then continue with SW 496 * implementation and if that also fails then return error. 497 */ 498 if (!br_mrp_switchdev_send_ring_test(br, mrp, test->interval, 499 test->max_miss, test->period, 500 test->monitor)) 501 return 0; 502 503 mrp->test_interval = test->interval; 504 mrp->test_end = jiffies + usecs_to_jiffies(test->period); 505 mrp->test_max_miss = test->max_miss; 506 mrp->test_monitor = test->monitor; 507 mrp->test_count_miss = 0; 508 queue_delayed_work(system_wq, &mrp->test_work, 509 usecs_to_jiffies(test->interval)); 510 511 return 0; 512 } 513 514 /* Process only MRP Test frame. All the other MRP frames are processed by 515 * userspace application 516 * note: already called with rcu_read_lock 517 */ 518 static void br_mrp_mrm_process(struct br_mrp *mrp, struct net_bridge_port *port, 519 struct sk_buff *skb) 520 { 521 const struct br_mrp_tlv_hdr *hdr; 522 struct br_mrp_tlv_hdr _hdr; 523 524 /* Each MRP header starts with a version field which is 16 bits. 525 * Therefore skip the version and get directly the TLV header. 526 */ 527 hdr = skb_header_pointer(skb, sizeof(uint16_t), sizeof(_hdr), &_hdr); 528 if (!hdr) 529 return; 530 531 if (hdr->type != BR_MRP_TLV_HEADER_RING_TEST) 532 return; 533 534 mrp->test_count_miss = 0; 535 536 /* Notify the userspace that the ring is closed only when the ring is 537 * not closed 538 */ 539 if (mrp->ring_state != BR_MRP_RING_STATE_CLOSED) 540 br_mrp_port_open(port->dev, false); 541 } 542 543 /* Determin if the test hdr has a better priority than the node */ 544 static bool br_mrp_test_better_than_own(struct br_mrp *mrp, 545 struct net_bridge *br, 546 const struct br_mrp_ring_test_hdr *hdr) 547 { 548 u16 prio = be16_to_cpu(hdr->prio); 549 550 if (prio < mrp->prio || 551 (prio == mrp->prio && 552 ether_addr_to_u64(hdr->sa) < ether_addr_to_u64(br->dev->dev_addr))) 553 return true; 554 555 return false; 556 } 557 558 /* Process only MRP Test frame. All the other MRP frames are processed by 559 * userspace application 560 * note: already called with rcu_read_lock 561 */ 562 static void br_mrp_mra_process(struct br_mrp *mrp, struct net_bridge *br, 563 struct net_bridge_port *port, 564 struct sk_buff *skb) 565 { 566 const struct br_mrp_ring_test_hdr *test_hdr; 567 struct br_mrp_ring_test_hdr _test_hdr; 568 const struct br_mrp_tlv_hdr *hdr; 569 struct br_mrp_tlv_hdr _hdr; 570 571 /* Each MRP header starts with a version field which is 16 bits. 572 * Therefore skip the version and get directly the TLV header. 573 */ 574 hdr = skb_header_pointer(skb, sizeof(uint16_t), sizeof(_hdr), &_hdr); 575 if (!hdr) 576 return; 577 578 if (hdr->type != BR_MRP_TLV_HEADER_RING_TEST) 579 return; 580 581 test_hdr = skb_header_pointer(skb, sizeof(uint16_t) + sizeof(_hdr), 582 sizeof(_test_hdr), &_test_hdr); 583 if (!test_hdr) 584 return; 585 586 /* Only frames that have a better priority than the node will 587 * clear the miss counter because otherwise the node will need to behave 588 * as MRM. 589 */ 590 if (br_mrp_test_better_than_own(mrp, br, test_hdr)) 591 mrp->test_count_miss = 0; 592 } 593 594 /* This will just forward the frame to the other mrp ring port(MRC role) or will 595 * not do anything. 596 * note: already called with rcu_read_lock 597 */ 598 static int br_mrp_rcv(struct net_bridge_port *p, 599 struct sk_buff *skb, struct net_device *dev) 600 { 601 struct net_device *s_dev, *p_dev, *d_dev; 602 struct net_bridge_port *p_port, *s_port; 603 struct net_bridge *br; 604 struct sk_buff *nskb; 605 struct br_mrp *mrp; 606 607 /* If port is disabled don't accept any frames */ 608 if (p->state == BR_STATE_DISABLED) 609 return 0; 610 611 br = p->br; 612 mrp = br_mrp_find_port(br, p); 613 if (unlikely(!mrp)) 614 return 0; 615 616 p_port = rcu_dereference(mrp->p_port); 617 if (!p_port) 618 return 0; 619 620 s_port = rcu_dereference(mrp->s_port); 621 if (!s_port) 622 return 0; 623 624 /* If the role is MRM then don't forward the frames */ 625 if (mrp->ring_role == BR_MRP_RING_ROLE_MRM) { 626 br_mrp_mrm_process(mrp, p, skb); 627 return 1; 628 } 629 630 /* If the role is MRA then don't forward the frames if it behaves as 631 * MRM node 632 */ 633 if (mrp->ring_role == BR_MRP_RING_ROLE_MRA) { 634 if (!mrp->test_monitor) { 635 br_mrp_mrm_process(mrp, p, skb); 636 return 1; 637 } 638 639 br_mrp_mra_process(mrp, br, p, skb); 640 } 641 642 /* Clone the frame and forward it on the other MRP port */ 643 nskb = skb_clone(skb, GFP_ATOMIC); 644 if (!nskb) 645 return 0; 646 647 p_dev = p_port->dev; 648 s_dev = s_port->dev; 649 650 if (p_dev == dev) 651 d_dev = s_dev; 652 else 653 d_dev = p_dev; 654 655 nskb->dev = d_dev; 656 skb_push(nskb, ETH_HLEN); 657 dev_queue_xmit(nskb); 658 659 return 1; 660 } 661 662 /* Check if the frame was received on a port that is part of MRP ring 663 * and if the frame has MRP eth. In that case process the frame otherwise do 664 * normal forwarding. 665 * note: already called with rcu_read_lock 666 */ 667 int br_mrp_process(struct net_bridge_port *p, struct sk_buff *skb) 668 { 669 /* If there is no MRP instance do normal forwarding */ 670 if (likely(!(p->flags & BR_MRP_AWARE))) 671 goto out; 672 673 if (unlikely(skb->protocol == htons(ETH_P_MRP))) 674 return br_mrp_rcv(p, skb, p->dev); 675 676 out: 677 return 0; 678 } 679 680 bool br_mrp_enabled(struct net_bridge *br) 681 { 682 return !list_empty(&br->mrp_list); 683 } 684