1 // SPDX-License-Identifier: GPL-2.0-or-later
3 #include <linux/mrp_bridge.h>
4 #include "br_private_mrp.h"
6 static const u8 mrp_test_dmac[ETH_ALEN] = { 0x1, 0x15, 0x4e, 0x0, 0x0, 0x1 };
7 static const u8 mrp_in_test_dmac[ETH_ALEN] = { 0x1, 0x15, 0x4e, 0x0, 0x0, 0x3 };
9 static bool br_mrp_is_ring_port(struct net_bridge_port *p_port,
10 struct net_bridge_port *s_port,
11 struct net_bridge_port *port)
20 static bool br_mrp_is_in_port(struct net_bridge_port *i_port,
21 struct net_bridge_port *port)
29 static struct net_bridge_port *br_mrp_get_port(struct net_bridge *br,
32 struct net_bridge_port *res = NULL;
33 struct net_bridge_port *port;
35 list_for_each_entry(port, &br->port_list, list) {
36 if (port->dev->ifindex == ifindex) {
45 static struct br_mrp *br_mrp_find_id(struct net_bridge *br, u32 ring_id)
47 struct br_mrp *res = NULL;
50 list_for_each_entry_rcu(mrp, &br->mrp_list, list,
51 lockdep_rtnl_is_held()) {
52 if (mrp->ring_id == ring_id) {
61 static struct br_mrp *br_mrp_find_in_id(struct net_bridge *br, u32 in_id)
63 struct br_mrp *res = NULL;
66 list_for_each_entry_rcu(mrp, &br->mrp_list, list,
67 lockdep_rtnl_is_held()) {
68 if (mrp->in_id == in_id) {
77 static bool br_mrp_unique_ifindex(struct net_bridge *br, u32 ifindex)
81 list_for_each_entry_rcu(mrp, &br->mrp_list, list,
82 lockdep_rtnl_is_held()) {
83 struct net_bridge_port *p;
85 p = rtnl_dereference(mrp->p_port);
86 if (p && p->dev->ifindex == ifindex)
89 p = rtnl_dereference(mrp->s_port);
90 if (p && p->dev->ifindex == ifindex)
93 p = rtnl_dereference(mrp->i_port);
94 if (p && p->dev->ifindex == ifindex)
101 static struct br_mrp *br_mrp_find_port(struct net_bridge *br,
102 struct net_bridge_port *p)
104 struct br_mrp *res = NULL;
107 list_for_each_entry_rcu(mrp, &br->mrp_list, list,
108 lockdep_rtnl_is_held()) {
109 if (rcu_access_pointer(mrp->p_port) == p ||
110 rcu_access_pointer(mrp->s_port) == p ||
111 rcu_access_pointer(mrp->i_port) == p) {
120 static int br_mrp_next_seq(struct br_mrp *mrp)
126 static struct sk_buff *br_mrp_skb_alloc(struct net_bridge_port *p,
127 const u8 *src, const u8 *dst)
129 struct ethhdr *eth_hdr;
133 skb = dev_alloc_skb(MRP_MAX_FRAME_LENGTH);
138 skb->protocol = htons(ETH_P_MRP);
139 skb->priority = MRP_FRAME_PRIO;
140 skb_reserve(skb, sizeof(*eth_hdr));
142 eth_hdr = skb_push(skb, sizeof(*eth_hdr));
143 ether_addr_copy(eth_hdr->h_dest, dst);
144 ether_addr_copy(eth_hdr->h_source, src);
145 eth_hdr->h_proto = htons(ETH_P_MRP);
147 version = skb_put(skb, sizeof(*version));
148 *version = cpu_to_be16(MRP_VERSION);
153 static void br_mrp_skb_tlv(struct sk_buff *skb,
154 enum br_mrp_tlv_header_type type,
157 struct br_mrp_tlv_hdr *hdr;
159 hdr = skb_put(skb, sizeof(*hdr));
161 hdr->length = length;
164 static void br_mrp_skb_common(struct sk_buff *skb, struct br_mrp *mrp)
166 struct br_mrp_common_hdr *hdr;
168 br_mrp_skb_tlv(skb, BR_MRP_TLV_HEADER_COMMON, sizeof(*hdr));
170 hdr = skb_put(skb, sizeof(*hdr));
171 hdr->seq_id = cpu_to_be16(br_mrp_next_seq(mrp));
172 memset(hdr->domain, 0xff, MRP_DOMAIN_UUID_LENGTH);
175 static struct sk_buff *br_mrp_alloc_test_skb(struct br_mrp *mrp,
176 struct net_bridge_port *p,
177 enum br_mrp_port_role_type port_role)
179 struct br_mrp_ring_test_hdr *hdr = NULL;
180 struct sk_buff *skb = NULL;
185 skb = br_mrp_skb_alloc(p, p->dev->dev_addr, mrp_test_dmac);
189 br_mrp_skb_tlv(skb, BR_MRP_TLV_HEADER_RING_TEST, sizeof(*hdr));
190 hdr = skb_put(skb, sizeof(*hdr));
192 hdr->prio = cpu_to_be16(mrp->prio);
193 ether_addr_copy(hdr->sa, p->br->dev->dev_addr);
194 hdr->port_role = cpu_to_be16(port_role);
195 hdr->state = cpu_to_be16(mrp->ring_state);
196 hdr->transitions = cpu_to_be16(mrp->ring_transitions);
197 hdr->timestamp = cpu_to_be32(jiffies_to_msecs(jiffies));
199 br_mrp_skb_common(skb, mrp);
200 br_mrp_skb_tlv(skb, BR_MRP_TLV_HEADER_END, 0x0);
205 static struct sk_buff *br_mrp_alloc_in_test_skb(struct br_mrp *mrp,
206 struct net_bridge_port *p,
207 enum br_mrp_port_role_type port_role)
209 struct br_mrp_in_test_hdr *hdr = NULL;
210 struct sk_buff *skb = NULL;
215 skb = br_mrp_skb_alloc(p, p->dev->dev_addr, mrp_in_test_dmac);
219 br_mrp_skb_tlv(skb, BR_MRP_TLV_HEADER_IN_TEST, sizeof(*hdr));
220 hdr = skb_put(skb, sizeof(*hdr));
222 hdr->id = cpu_to_be16(mrp->in_id);
223 ether_addr_copy(hdr->sa, p->br->dev->dev_addr);
224 hdr->port_role = cpu_to_be16(port_role);
225 hdr->state = cpu_to_be16(mrp->in_state);
226 hdr->transitions = cpu_to_be16(mrp->in_transitions);
227 hdr->timestamp = cpu_to_be32(jiffies_to_msecs(jiffies));
229 br_mrp_skb_common(skb, mrp);
230 br_mrp_skb_tlv(skb, BR_MRP_TLV_HEADER_END, 0x0);
235 /* This function is continuously called in the following cases:
236 * - when node role is MRM, in this case test_monitor is always set to false
237 * because it needs to notify the userspace that the ring is open and needs to
238 * send MRP_Test frames
239 * - when node role is MRA, there are 2 subcases:
240 * - when MRA behaves as MRM, in this case is similar with MRM role
241 * - when MRA behaves as MRC, in this case test_monitor is set to true,
242 * because it needs to detect when it stops seeing MRP_Test frames
243 * from MRM node but it doesn't need to send MRP_Test frames.
245 static void br_mrp_test_work_expired(struct work_struct *work)
247 struct delayed_work *del_work = to_delayed_work(work);
248 struct br_mrp *mrp = container_of(del_work, struct br_mrp, test_work);
249 struct net_bridge_port *p;
250 bool notify_open = false;
253 if (time_before_eq(mrp->test_end, jiffies))
256 if (mrp->test_count_miss < mrp->test_max_miss) {
257 mrp->test_count_miss++;
259 /* Notify that the ring is open only if the ring state is
260 * closed, otherwise it would continue to notify at every
262 * Also notify that the ring is open when the node has the
263 * role MRA and behaves as MRC. The reason is that the
264 * userspace needs to know when the MRM stopped sending
265 * MRP_Test frames so that the current node to try to take
268 if (mrp->ring_state == BR_MRP_RING_STATE_CLOSED ||
275 p = rcu_dereference(mrp->p_port);
277 if (!mrp->test_monitor) {
278 skb = br_mrp_alloc_test_skb(mrp, p,
279 BR_MRP_PORT_ROLE_PRIMARY);
283 skb_reset_network_header(skb);
287 if (notify_open && !mrp->ring_role_offloaded)
288 br_mrp_ring_port_open(p->dev, true);
291 p = rcu_dereference(mrp->s_port);
293 if (!mrp->test_monitor) {
294 skb = br_mrp_alloc_test_skb(mrp, p,
295 BR_MRP_PORT_ROLE_SECONDARY);
299 skb_reset_network_header(skb);
303 if (notify_open && !mrp->ring_role_offloaded)
304 br_mrp_ring_port_open(p->dev, true);
310 queue_delayed_work(system_wq, &mrp->test_work,
311 usecs_to_jiffies(mrp->test_interval));
314 /* This function is continuously called when the node has the interconnect role
315 * MIM. It would generate interconnect test frames and will send them on all 3
316 * ports. But will also check if it stop receiving interconnect test frames.
318 static void br_mrp_in_test_work_expired(struct work_struct *work)
320 struct delayed_work *del_work = to_delayed_work(work);
321 struct br_mrp *mrp = container_of(del_work, struct br_mrp, in_test_work);
322 struct net_bridge_port *p;
323 bool notify_open = false;
326 if (time_before_eq(mrp->in_test_end, jiffies))
329 if (mrp->in_test_count_miss < mrp->in_test_max_miss) {
330 mrp->in_test_count_miss++;
332 /* Notify that the interconnect ring is open only if the
333 * interconnect ring state is closed, otherwise it would
334 * continue to notify at every interval.
336 if (mrp->in_state == BR_MRP_IN_STATE_CLOSED)
342 p = rcu_dereference(mrp->p_port);
344 skb = br_mrp_alloc_in_test_skb(mrp, p,
345 BR_MRP_PORT_ROLE_PRIMARY);
349 skb_reset_network_header(skb);
352 if (notify_open && !mrp->in_role_offloaded)
353 br_mrp_in_port_open(p->dev, true);
356 p = rcu_dereference(mrp->s_port);
358 skb = br_mrp_alloc_in_test_skb(mrp, p,
359 BR_MRP_PORT_ROLE_SECONDARY);
363 skb_reset_network_header(skb);
366 if (notify_open && !mrp->in_role_offloaded)
367 br_mrp_in_port_open(p->dev, true);
370 p = rcu_dereference(mrp->i_port);
372 skb = br_mrp_alloc_in_test_skb(mrp, p,
373 BR_MRP_PORT_ROLE_INTER);
377 skb_reset_network_header(skb);
380 if (notify_open && !mrp->in_role_offloaded)
381 br_mrp_in_port_open(p->dev, true);
387 queue_delayed_work(system_wq, &mrp->in_test_work,
388 usecs_to_jiffies(mrp->in_test_interval));
391 /* Deletes the MRP instance.
392 * note: called under rtnl_lock
394 static void br_mrp_del_impl(struct net_bridge *br, struct br_mrp *mrp)
396 struct net_bridge_port *p;
399 /* Stop sending MRP_Test frames */
400 cancel_delayed_work_sync(&mrp->test_work);
401 br_mrp_switchdev_send_ring_test(br, mrp, 0, 0, 0, 0);
403 /* Stop sending MRP_InTest frames if has an interconnect role */
404 cancel_delayed_work_sync(&mrp->in_test_work);
405 br_mrp_switchdev_send_in_test(br, mrp, 0, 0, 0);
407 br_mrp_switchdev_del(br, mrp);
409 /* Reset the ports */
410 p = rtnl_dereference(mrp->p_port);
412 spin_lock_bh(&br->lock);
413 state = netif_running(br->dev) ?
414 BR_STATE_FORWARDING : BR_STATE_DISABLED;
416 p->flags &= ~BR_MRP_AWARE;
417 spin_unlock_bh(&br->lock);
418 br_mrp_port_switchdev_set_state(p, state);
419 rcu_assign_pointer(mrp->p_port, NULL);
422 p = rtnl_dereference(mrp->s_port);
424 spin_lock_bh(&br->lock);
425 state = netif_running(br->dev) ?
426 BR_STATE_FORWARDING : BR_STATE_DISABLED;
428 p->flags &= ~BR_MRP_AWARE;
429 spin_unlock_bh(&br->lock);
430 br_mrp_port_switchdev_set_state(p, state);
431 rcu_assign_pointer(mrp->s_port, NULL);
434 p = rtnl_dereference(mrp->i_port);
436 spin_lock_bh(&br->lock);
437 state = netif_running(br->dev) ?
438 BR_STATE_FORWARDING : BR_STATE_DISABLED;
440 p->flags &= ~BR_MRP_AWARE;
441 spin_unlock_bh(&br->lock);
442 br_mrp_port_switchdev_set_state(p, state);
443 rcu_assign_pointer(mrp->i_port, NULL);
446 list_del_rcu(&mrp->list);
450 /* Adds a new MRP instance.
451 * note: called under rtnl_lock
453 int br_mrp_add(struct net_bridge *br, struct br_mrp_instance *instance)
455 struct net_bridge_port *p;
459 /* If the ring exists, it is not possible to create another one with the
462 mrp = br_mrp_find_id(br, instance->ring_id);
466 if (!br_mrp_get_port(br, instance->p_ifindex) ||
467 !br_mrp_get_port(br, instance->s_ifindex))
470 /* It is not possible to have the same port part of multiple rings */
471 if (!br_mrp_unique_ifindex(br, instance->p_ifindex) ||
472 !br_mrp_unique_ifindex(br, instance->s_ifindex))
475 mrp = kzalloc(sizeof(*mrp), GFP_KERNEL);
479 mrp->ring_id = instance->ring_id;
480 mrp->prio = instance->prio;
482 p = br_mrp_get_port(br, instance->p_ifindex);
483 spin_lock_bh(&br->lock);
484 p->state = BR_STATE_FORWARDING;
485 p->flags |= BR_MRP_AWARE;
486 spin_unlock_bh(&br->lock);
487 rcu_assign_pointer(mrp->p_port, p);
489 p = br_mrp_get_port(br, instance->s_ifindex);
490 spin_lock_bh(&br->lock);
491 p->state = BR_STATE_FORWARDING;
492 p->flags |= BR_MRP_AWARE;
493 spin_unlock_bh(&br->lock);
494 rcu_assign_pointer(mrp->s_port, p);
496 INIT_DELAYED_WORK(&mrp->test_work, br_mrp_test_work_expired);
497 INIT_DELAYED_WORK(&mrp->in_test_work, br_mrp_in_test_work_expired);
498 list_add_tail_rcu(&mrp->list, &br->mrp_list);
500 err = br_mrp_switchdev_add(br, mrp);
507 br_mrp_del_impl(br, mrp);
512 /* Deletes the MRP instance from which the port is part of
513 * note: called under rtnl_lock
515 void br_mrp_port_del(struct net_bridge *br, struct net_bridge_port *p)
517 struct br_mrp *mrp = br_mrp_find_port(br, p);
519 /* If the port is not part of a MRP instance just bail out */
523 br_mrp_del_impl(br, mrp);
526 /* Deletes existing MRP instance based on ring_id
527 * note: called under rtnl_lock
529 int br_mrp_del(struct net_bridge *br, struct br_mrp_instance *instance)
531 struct br_mrp *mrp = br_mrp_find_id(br, instance->ring_id);
536 br_mrp_del_impl(br, mrp);
541 /* Set port state, port state can be forwarding, blocked or disabled
542 * note: already called with rtnl_lock
544 int br_mrp_set_port_state(struct net_bridge_port *p,
545 enum br_mrp_port_state_type state)
547 if (!p || !(p->flags & BR_MRP_AWARE))
550 spin_lock_bh(&p->br->lock);
552 if (state == BR_MRP_PORT_STATE_FORWARDING)
553 p->state = BR_STATE_FORWARDING;
555 p->state = BR_STATE_BLOCKING;
557 spin_unlock_bh(&p->br->lock);
559 br_mrp_port_switchdev_set_state(p, state);
564 /* Set port role, port role can be primary or secondary
565 * note: already called with rtnl_lock
567 int br_mrp_set_port_role(struct net_bridge_port *p,
568 enum br_mrp_port_role_type role)
572 if (!p || !(p->flags & BR_MRP_AWARE))
575 mrp = br_mrp_find_port(p->br, p);
581 case BR_MRP_PORT_ROLE_PRIMARY:
582 rcu_assign_pointer(mrp->p_port, p);
584 case BR_MRP_PORT_ROLE_SECONDARY:
585 rcu_assign_pointer(mrp->s_port, p);
591 br_mrp_port_switchdev_set_role(p, role);
596 /* Set ring state, ring state can be only Open or Closed
597 * note: already called with rtnl_lock
599 int br_mrp_set_ring_state(struct net_bridge *br,
600 struct br_mrp_ring_state *state)
602 struct br_mrp *mrp = br_mrp_find_id(br, state->ring_id);
607 if (mrp->ring_state == BR_MRP_RING_STATE_CLOSED &&
608 state->ring_state != BR_MRP_RING_STATE_CLOSED)
609 mrp->ring_transitions++;
611 mrp->ring_state = state->ring_state;
613 br_mrp_switchdev_set_ring_state(br, mrp, state->ring_state);
618 /* Set ring role, ring role can be only MRM(Media Redundancy Manager) or
619 * MRC(Media Redundancy Client).
620 * note: already called with rtnl_lock
622 int br_mrp_set_ring_role(struct net_bridge *br,
623 struct br_mrp_ring_role *role)
625 struct br_mrp *mrp = br_mrp_find_id(br, role->ring_id);
631 mrp->ring_role = role->ring_role;
633 /* If there is an error just bailed out */
634 err = br_mrp_switchdev_set_ring_role(br, mrp, role->ring_role);
635 if (err && err != -EOPNOTSUPP)
638 /* Now detect if the HW actually applied the role or not. If the HW
639 * applied the role it means that the SW will not to do those operations
640 * anymore. For example if the role ir MRM then the HW will notify the
641 * SW when ring is open, but if the is not pushed to the HW the SW will
642 * need to detect when the ring is open
644 mrp->ring_role_offloaded = err == -EOPNOTSUPP ? 0 : 1;
649 /* Start to generate or monitor MRP test frames, the frames are generated by
650 * HW and if it fails, they are generated by the SW.
651 * note: already called with rtnl_lock
653 int br_mrp_start_test(struct net_bridge *br,
654 struct br_mrp_start_test *test)
656 struct br_mrp *mrp = br_mrp_find_id(br, test->ring_id);
661 /* Try to push it to the HW and if it fails then continue with SW
662 * implementation and if that also fails then return error.
664 if (!br_mrp_switchdev_send_ring_test(br, mrp, test->interval,
665 test->max_miss, test->period,
669 mrp->test_interval = test->interval;
670 mrp->test_end = jiffies + usecs_to_jiffies(test->period);
671 mrp->test_max_miss = test->max_miss;
672 mrp->test_monitor = test->monitor;
673 mrp->test_count_miss = 0;
674 queue_delayed_work(system_wq, &mrp->test_work,
675 usecs_to_jiffies(test->interval));
680 /* Set in state, int state can be only Open or Closed
681 * note: already called with rtnl_lock
683 int br_mrp_set_in_state(struct net_bridge *br, struct br_mrp_in_state *state)
685 struct br_mrp *mrp = br_mrp_find_in_id(br, state->in_id);
690 if (mrp->in_state == BR_MRP_IN_STATE_CLOSED &&
691 state->in_state != BR_MRP_IN_STATE_CLOSED)
692 mrp->in_transitions++;
694 mrp->in_state = state->in_state;
696 br_mrp_switchdev_set_in_state(br, mrp, state->in_state);
701 /* Set in role, in role can be only MIM(Media Interconnection Manager) or
702 * MIC(Media Interconnection Client).
703 * note: already called with rtnl_lock
705 int br_mrp_set_in_role(struct net_bridge *br, struct br_mrp_in_role *role)
707 struct br_mrp *mrp = br_mrp_find_id(br, role->ring_id);
708 struct net_bridge_port *p;
714 if (!br_mrp_get_port(br, role->i_ifindex))
717 if (role->in_role == BR_MRP_IN_ROLE_DISABLED) {
720 /* It is not allowed to disable a port that doesn't exist */
721 p = rtnl_dereference(mrp->i_port);
725 /* Stop the generating MRP_InTest frames */
726 cancel_delayed_work_sync(&mrp->in_test_work);
727 br_mrp_switchdev_send_in_test(br, mrp, 0, 0, 0);
729 /* Remove the port */
730 spin_lock_bh(&br->lock);
731 state = netif_running(br->dev) ?
732 BR_STATE_FORWARDING : BR_STATE_DISABLED;
734 p->flags &= ~BR_MRP_AWARE;
735 spin_unlock_bh(&br->lock);
736 br_mrp_port_switchdev_set_state(p, state);
737 rcu_assign_pointer(mrp->i_port, NULL);
739 mrp->in_role = role->in_role;
745 /* It is not possible to have the same port part of multiple rings */
746 if (!br_mrp_unique_ifindex(br, role->i_ifindex))
749 /* It is not allowed to set a different interconnect port if the mrp
750 * instance has already one. First it needs to be disabled and after
751 * that set the new port
753 if (rcu_access_pointer(mrp->i_port))
756 p = br_mrp_get_port(br, role->i_ifindex);
757 spin_lock_bh(&br->lock);
758 p->state = BR_STATE_FORWARDING;
759 p->flags |= BR_MRP_AWARE;
760 spin_unlock_bh(&br->lock);
761 rcu_assign_pointer(mrp->i_port, p);
763 mrp->in_role = role->in_role;
764 mrp->in_id = role->in_id;
766 /* If there is an error just bailed out */
767 err = br_mrp_switchdev_set_in_role(br, mrp, role->in_id,
768 role->ring_id, role->in_role);
769 if (err && err != -EOPNOTSUPP)
772 /* Now detect if the HW actually applied the role or not. If the HW
773 * applied the role it means that the SW will not to do those operations
774 * anymore. For example if the role is MIM then the HW will notify the
775 * SW when interconnect ring is open, but if the is not pushed to the HW
776 * the SW will need to detect when the interconnect ring is open.
778 mrp->in_role_offloaded = err == -EOPNOTSUPP ? 0 : 1;
783 /* Start to generate MRP_InTest frames, the frames are generated by
784 * HW and if it fails, they are generated by the SW.
785 * note: already called with rtnl_lock
787 int br_mrp_start_in_test(struct net_bridge *br,
788 struct br_mrp_start_in_test *in_test)
790 struct br_mrp *mrp = br_mrp_find_in_id(br, in_test->in_id);
795 if (mrp->in_role != BR_MRP_IN_ROLE_MIM)
798 /* Try to push it to the HW and if it fails then continue with SW
799 * implementation and if that also fails then return error.
801 if (!br_mrp_switchdev_send_in_test(br, mrp, in_test->interval,
802 in_test->max_miss, in_test->period))
805 mrp->in_test_interval = in_test->interval;
806 mrp->in_test_end = jiffies + usecs_to_jiffies(in_test->period);
807 mrp->in_test_max_miss = in_test->max_miss;
808 mrp->in_test_count_miss = 0;
809 queue_delayed_work(system_wq, &mrp->in_test_work,
810 usecs_to_jiffies(in_test->interval));
815 /* Determin if the frame type is a ring frame */
816 static bool br_mrp_ring_frame(struct sk_buff *skb)
818 const struct br_mrp_tlv_hdr *hdr;
819 struct br_mrp_tlv_hdr _hdr;
821 hdr = skb_header_pointer(skb, sizeof(uint16_t), sizeof(_hdr), &_hdr);
825 if (hdr->type == BR_MRP_TLV_HEADER_RING_TEST ||
826 hdr->type == BR_MRP_TLV_HEADER_RING_TOPO ||
827 hdr->type == BR_MRP_TLV_HEADER_RING_LINK_DOWN ||
828 hdr->type == BR_MRP_TLV_HEADER_RING_LINK_UP ||
829 hdr->type == BR_MRP_TLV_HEADER_OPTION)
835 /* Determin if the frame type is an interconnect frame */
836 static bool br_mrp_in_frame(struct sk_buff *skb)
838 const struct br_mrp_tlv_hdr *hdr;
839 struct br_mrp_tlv_hdr _hdr;
841 hdr = skb_header_pointer(skb, sizeof(uint16_t), sizeof(_hdr), &_hdr);
845 if (hdr->type == BR_MRP_TLV_HEADER_IN_TEST ||
846 hdr->type == BR_MRP_TLV_HEADER_IN_TOPO ||
847 hdr->type == BR_MRP_TLV_HEADER_IN_LINK_DOWN ||
848 hdr->type == BR_MRP_TLV_HEADER_IN_LINK_UP)
854 /* Process only MRP Test frame. All the other MRP frames are processed by
855 * userspace application
856 * note: already called with rcu_read_lock
858 static void br_mrp_mrm_process(struct br_mrp *mrp, struct net_bridge_port *port,
861 const struct br_mrp_tlv_hdr *hdr;
862 struct br_mrp_tlv_hdr _hdr;
864 /* Each MRP header starts with a version field which is 16 bits.
865 * Therefore skip the version and get directly the TLV header.
867 hdr = skb_header_pointer(skb, sizeof(uint16_t), sizeof(_hdr), &_hdr);
871 if (hdr->type != BR_MRP_TLV_HEADER_RING_TEST)
874 mrp->test_count_miss = 0;
876 /* Notify the userspace that the ring is closed only when the ring is
879 if (mrp->ring_state != BR_MRP_RING_STATE_CLOSED)
880 br_mrp_ring_port_open(port->dev, false);
883 /* Determin if the test hdr has a better priority than the node */
884 static bool br_mrp_test_better_than_own(struct br_mrp *mrp,
885 struct net_bridge *br,
886 const struct br_mrp_ring_test_hdr *hdr)
888 u16 prio = be16_to_cpu(hdr->prio);
890 if (prio < mrp->prio ||
891 (prio == mrp->prio &&
892 ether_addr_to_u64(hdr->sa) < ether_addr_to_u64(br->dev->dev_addr)))
898 /* Process only MRP Test frame. All the other MRP frames are processed by
899 * userspace application
900 * note: already called with rcu_read_lock
902 static void br_mrp_mra_process(struct br_mrp *mrp, struct net_bridge *br,
903 struct net_bridge_port *port,
906 const struct br_mrp_ring_test_hdr *test_hdr;
907 struct br_mrp_ring_test_hdr _test_hdr;
908 const struct br_mrp_tlv_hdr *hdr;
909 struct br_mrp_tlv_hdr _hdr;
911 /* Each MRP header starts with a version field which is 16 bits.
912 * Therefore skip the version and get directly the TLV header.
914 hdr = skb_header_pointer(skb, sizeof(uint16_t), sizeof(_hdr), &_hdr);
918 if (hdr->type != BR_MRP_TLV_HEADER_RING_TEST)
921 test_hdr = skb_header_pointer(skb, sizeof(uint16_t) + sizeof(_hdr),
922 sizeof(_test_hdr), &_test_hdr);
926 /* Only frames that have a better priority than the node will
927 * clear the miss counter because otherwise the node will need to behave
930 if (br_mrp_test_better_than_own(mrp, br, test_hdr))
931 mrp->test_count_miss = 0;
934 /* Process only MRP InTest frame. All the other MRP frames are processed by
935 * userspace application
936 * note: already called with rcu_read_lock
938 static bool br_mrp_mim_process(struct br_mrp *mrp, struct net_bridge_port *port,
941 const struct br_mrp_in_test_hdr *in_hdr;
942 struct br_mrp_in_test_hdr _in_hdr;
943 const struct br_mrp_tlv_hdr *hdr;
944 struct br_mrp_tlv_hdr _hdr;
946 /* Each MRP header starts with a version field which is 16 bits.
947 * Therefore skip the version and get directly the TLV header.
949 hdr = skb_header_pointer(skb, sizeof(uint16_t), sizeof(_hdr), &_hdr);
953 /* The check for InTest frame type was already done */
954 in_hdr = skb_header_pointer(skb, sizeof(uint16_t) + sizeof(_hdr),
955 sizeof(_in_hdr), &_in_hdr);
959 /* It needs to process only it's own InTest frames. */
960 if (mrp->in_id != ntohs(in_hdr->id))
963 mrp->in_test_count_miss = 0;
965 /* Notify the userspace that the ring is closed only when the ring is
968 if (mrp->in_state != BR_MRP_IN_STATE_CLOSED)
969 br_mrp_in_port_open(port->dev, false);
974 /* Get the MRP frame type
975 * note: already called with rcu_read_lock
977 static u8 br_mrp_get_frame_type(struct sk_buff *skb)
979 const struct br_mrp_tlv_hdr *hdr;
980 struct br_mrp_tlv_hdr _hdr;
982 /* Each MRP header starts with a version field which is 16 bits.
983 * Therefore skip the version and get directly the TLV header.
985 hdr = skb_header_pointer(skb, sizeof(uint16_t), sizeof(_hdr), &_hdr);
992 static bool br_mrp_mrm_behaviour(struct br_mrp *mrp)
994 if (mrp->ring_role == BR_MRP_RING_ROLE_MRM ||
995 (mrp->ring_role == BR_MRP_RING_ROLE_MRA && !mrp->test_monitor))
1001 static bool br_mrp_mrc_behaviour(struct br_mrp *mrp)
1003 if (mrp->ring_role == BR_MRP_RING_ROLE_MRC ||
1004 (mrp->ring_role == BR_MRP_RING_ROLE_MRA && mrp->test_monitor))
1010 /* This will just forward the frame to the other mrp ring ports, depending on
1011 * the frame type, ring role and interconnect role
1012 * note: already called with rcu_read_lock
1014 static int br_mrp_rcv(struct net_bridge_port *p,
1015 struct sk_buff *skb, struct net_device *dev)
1017 struct net_bridge_port *p_port, *s_port, *i_port = NULL;
1018 struct net_bridge_port *p_dst, *s_dst, *i_dst = NULL;
1019 struct net_bridge *br;
1022 /* If port is disabled don't accept any frames */
1023 if (p->state == BR_STATE_DISABLED)
1027 mrp = br_mrp_find_port(br, p);
1031 p_port = rcu_dereference(mrp->p_port);
1036 s_port = rcu_dereference(mrp->s_port);
1041 /* If the frame is a ring frame then it is not required to check the
1042 * interconnect role and ports to process or forward the frame
1044 if (br_mrp_ring_frame(skb)) {
1045 /* If the role is MRM then don't forward the frames */
1046 if (mrp->ring_role == BR_MRP_RING_ROLE_MRM) {
1047 br_mrp_mrm_process(mrp, p, skb);
1051 /* If the role is MRA then don't forward the frames if it
1052 * behaves as MRM node
1054 if (mrp->ring_role == BR_MRP_RING_ROLE_MRA) {
1055 if (!mrp->test_monitor) {
1056 br_mrp_mrm_process(mrp, p, skb);
1060 br_mrp_mra_process(mrp, br, p, skb);
1066 if (br_mrp_in_frame(skb)) {
1067 u8 in_type = br_mrp_get_frame_type(skb);
1069 i_port = rcu_dereference(mrp->i_port);
1072 /* If the ring port is in block state it should not forward
1075 if (br_mrp_is_ring_port(p_port, s_port, p) &&
1076 p->state == BR_STATE_BLOCKING &&
1077 in_type == BR_MRP_TLV_HEADER_IN_TEST)
1080 /* Nodes that behaves as MRM needs to stop forwarding the
1081 * frames in case the ring is closed, otherwise will be a loop.
1082 * In this case the frame is no forward between the ring ports.
1084 if (br_mrp_mrm_behaviour(mrp) &&
1085 br_mrp_is_ring_port(p_port, s_port, p) &&
1086 (s_port->state != BR_STATE_FORWARDING ||
1087 p_port->state != BR_STATE_FORWARDING)) {
1092 /* A node that behaves as MRC and doesn't have a interconnect
1093 * role then it should forward all frames between the ring ports
1094 * because it doesn't have an interconnect port
1096 if (br_mrp_mrc_behaviour(mrp) &&
1097 mrp->in_role == BR_MRP_IN_ROLE_DISABLED)
1100 if (mrp->in_role == BR_MRP_IN_ROLE_MIM) {
1101 if (in_type == BR_MRP_TLV_HEADER_IN_TEST) {
1102 /* MIM should not forward it's own InTest
1105 if (br_mrp_mim_process(mrp, p, skb)) {
1108 if (br_mrp_is_ring_port(p_port, s_port,
1112 if (br_mrp_is_in_port(i_port, p))
1116 /* MIM should forward IntLinkChange and
1117 * IntTopoChange between ring ports but MIM
1118 * should not forward IntLinkChange and
1119 * IntTopoChange if the frame was received at
1120 * the interconnect port
1122 if (br_mrp_is_ring_port(p_port, s_port, p))
1125 if (br_mrp_is_in_port(i_port, p))
1130 if (mrp->in_role == BR_MRP_IN_ROLE_MIC) {
1131 /* MIC should forward InTest frames on all ports
1132 * regardless of the received port
1134 if (in_type == BR_MRP_TLV_HEADER_IN_TEST)
1137 /* MIC should forward IntLinkChange frames only if they
1138 * are received on ring ports to all the ports
1140 if (br_mrp_is_ring_port(p_port, s_port, p) &&
1141 (in_type == BR_MRP_TLV_HEADER_IN_LINK_UP ||
1142 in_type == BR_MRP_TLV_HEADER_IN_LINK_DOWN))
1145 /* Should forward the InTopo frames only between the
1148 if (in_type == BR_MRP_TLV_HEADER_IN_TOPO) {
1153 /* In all the other cases don't forward the frames */
1160 br_forward(p_dst, skb, true, false);
1162 br_forward(s_dst, skb, true, false);
1164 br_forward(i_dst, skb, true, false);
1170 /* Check if the frame was received on a port that is part of MRP ring
1171 * and if the frame has MRP eth. In that case process the frame otherwise do
1172 * normal forwarding.
1173 * note: already called with rcu_read_lock
1175 int br_mrp_process(struct net_bridge_port *p, struct sk_buff *skb)
1177 /* If there is no MRP instance do normal forwarding */
1178 if (likely(!(p->flags & BR_MRP_AWARE)))
1181 if (unlikely(skb->protocol == htons(ETH_P_MRP)))
1182 return br_mrp_rcv(p, skb, p->dev);
1188 bool br_mrp_enabled(struct net_bridge *br)
1190 return !list_empty(&br->mrp_list);