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 int br_mrp_process(struct net_bridge_port *p, struct sk_buff *skb);
11 static struct br_frame_type mrp_frame_type __read_mostly = {
12 .type = cpu_to_be16(ETH_P_MRP),
13 .frame_handler = br_mrp_process,
16 static bool br_mrp_is_ring_port(struct net_bridge_port *p_port,
17 struct net_bridge_port *s_port,
18 struct net_bridge_port *port)
27 static bool br_mrp_is_in_port(struct net_bridge_port *i_port,
28 struct net_bridge_port *port)
36 static struct net_bridge_port *br_mrp_get_port(struct net_bridge *br,
39 struct net_bridge_port *res = NULL;
40 struct net_bridge_port *port;
42 list_for_each_entry(port, &br->port_list, list) {
43 if (port->dev->ifindex == ifindex) {
52 static struct br_mrp *br_mrp_find_id(struct net_bridge *br, u32 ring_id)
54 struct br_mrp *res = NULL;
57 hlist_for_each_entry_rcu(mrp, &br->mrp_list, list,
58 lockdep_rtnl_is_held()) {
59 if (mrp->ring_id == ring_id) {
68 static struct br_mrp *br_mrp_find_in_id(struct net_bridge *br, u32 in_id)
70 struct br_mrp *res = NULL;
73 hlist_for_each_entry_rcu(mrp, &br->mrp_list, list,
74 lockdep_rtnl_is_held()) {
75 if (mrp->in_id == in_id) {
84 static bool br_mrp_unique_ifindex(struct net_bridge *br, u32 ifindex)
88 hlist_for_each_entry_rcu(mrp, &br->mrp_list, list,
89 lockdep_rtnl_is_held()) {
90 struct net_bridge_port *p;
92 p = rtnl_dereference(mrp->p_port);
93 if (p && p->dev->ifindex == ifindex)
96 p = rtnl_dereference(mrp->s_port);
97 if (p && p->dev->ifindex == ifindex)
100 p = rtnl_dereference(mrp->i_port);
101 if (p && p->dev->ifindex == ifindex)
108 static struct br_mrp *br_mrp_find_port(struct net_bridge *br,
109 struct net_bridge_port *p)
111 struct br_mrp *res = NULL;
114 hlist_for_each_entry_rcu(mrp, &br->mrp_list, list,
115 lockdep_rtnl_is_held()) {
116 if (rcu_access_pointer(mrp->p_port) == p ||
117 rcu_access_pointer(mrp->s_port) == p ||
118 rcu_access_pointer(mrp->i_port) == p) {
127 static int br_mrp_next_seq(struct br_mrp *mrp)
133 static struct sk_buff *br_mrp_skb_alloc(struct net_bridge_port *p,
134 const u8 *src, const u8 *dst)
136 struct ethhdr *eth_hdr;
140 skb = dev_alloc_skb(MRP_MAX_FRAME_LENGTH);
145 skb->protocol = htons(ETH_P_MRP);
146 skb->priority = MRP_FRAME_PRIO;
147 skb_reserve(skb, sizeof(*eth_hdr));
149 eth_hdr = skb_push(skb, sizeof(*eth_hdr));
150 ether_addr_copy(eth_hdr->h_dest, dst);
151 ether_addr_copy(eth_hdr->h_source, src);
152 eth_hdr->h_proto = htons(ETH_P_MRP);
154 version = skb_put(skb, sizeof(*version));
155 *version = cpu_to_be16(MRP_VERSION);
160 static void br_mrp_skb_tlv(struct sk_buff *skb,
161 enum br_mrp_tlv_header_type type,
164 struct br_mrp_tlv_hdr *hdr;
166 hdr = skb_put(skb, sizeof(*hdr));
168 hdr->length = length;
171 static void br_mrp_skb_common(struct sk_buff *skb, struct br_mrp *mrp)
173 struct br_mrp_common_hdr *hdr;
175 br_mrp_skb_tlv(skb, BR_MRP_TLV_HEADER_COMMON, sizeof(*hdr));
177 hdr = skb_put(skb, sizeof(*hdr));
178 hdr->seq_id = cpu_to_be16(br_mrp_next_seq(mrp));
179 memset(hdr->domain, 0xff, MRP_DOMAIN_UUID_LENGTH);
182 static struct sk_buff *br_mrp_alloc_test_skb(struct br_mrp *mrp,
183 struct net_bridge_port *p,
184 enum br_mrp_port_role_type port_role)
186 struct br_mrp_ring_test_hdr *hdr = NULL;
187 struct sk_buff *skb = NULL;
192 skb = br_mrp_skb_alloc(p, p->dev->dev_addr, mrp_test_dmac);
196 br_mrp_skb_tlv(skb, BR_MRP_TLV_HEADER_RING_TEST, sizeof(*hdr));
197 hdr = skb_put(skb, sizeof(*hdr));
199 hdr->prio = cpu_to_be16(mrp->prio);
200 ether_addr_copy(hdr->sa, p->br->dev->dev_addr);
201 hdr->port_role = cpu_to_be16(port_role);
202 hdr->state = cpu_to_be16(mrp->ring_state);
203 hdr->transitions = cpu_to_be16(mrp->ring_transitions);
204 hdr->timestamp = cpu_to_be32(jiffies_to_msecs(jiffies));
206 br_mrp_skb_common(skb, mrp);
207 br_mrp_skb_tlv(skb, BR_MRP_TLV_HEADER_END, 0x0);
212 static struct sk_buff *br_mrp_alloc_in_test_skb(struct br_mrp *mrp,
213 struct net_bridge_port *p,
214 enum br_mrp_port_role_type port_role)
216 struct br_mrp_in_test_hdr *hdr = NULL;
217 struct sk_buff *skb = NULL;
222 skb = br_mrp_skb_alloc(p, p->dev->dev_addr, mrp_in_test_dmac);
226 br_mrp_skb_tlv(skb, BR_MRP_TLV_HEADER_IN_TEST, sizeof(*hdr));
227 hdr = skb_put(skb, sizeof(*hdr));
229 hdr->id = cpu_to_be16(mrp->in_id);
230 ether_addr_copy(hdr->sa, p->br->dev->dev_addr);
231 hdr->port_role = cpu_to_be16(port_role);
232 hdr->state = cpu_to_be16(mrp->in_state);
233 hdr->transitions = cpu_to_be16(mrp->in_transitions);
234 hdr->timestamp = cpu_to_be32(jiffies_to_msecs(jiffies));
236 br_mrp_skb_common(skb, mrp);
237 br_mrp_skb_tlv(skb, BR_MRP_TLV_HEADER_END, 0x0);
242 /* This function is continuously called in the following cases:
243 * - when node role is MRM, in this case test_monitor is always set to false
244 * because it needs to notify the userspace that the ring is open and needs to
245 * send MRP_Test frames
246 * - when node role is MRA, there are 2 subcases:
247 * - when MRA behaves as MRM, in this case is similar with MRM role
248 * - when MRA behaves as MRC, in this case test_monitor is set to true,
249 * because it needs to detect when it stops seeing MRP_Test frames
250 * from MRM node but it doesn't need to send MRP_Test frames.
252 static void br_mrp_test_work_expired(struct work_struct *work)
254 struct delayed_work *del_work = to_delayed_work(work);
255 struct br_mrp *mrp = container_of(del_work, struct br_mrp, test_work);
256 struct net_bridge_port *p;
257 bool notify_open = false;
260 if (time_before_eq(mrp->test_end, jiffies))
263 if (mrp->test_count_miss < mrp->test_max_miss) {
264 mrp->test_count_miss++;
266 /* Notify that the ring is open only if the ring state is
267 * closed, otherwise it would continue to notify at every
269 * Also notify that the ring is open when the node has the
270 * role MRA and behaves as MRC. The reason is that the
271 * userspace needs to know when the MRM stopped sending
272 * MRP_Test frames so that the current node to try to take
275 if (mrp->ring_state == BR_MRP_RING_STATE_CLOSED ||
282 p = rcu_dereference(mrp->p_port);
284 if (!mrp->test_monitor) {
285 skb = br_mrp_alloc_test_skb(mrp, p,
286 BR_MRP_PORT_ROLE_PRIMARY);
290 skb_reset_network_header(skb);
294 if (notify_open && !mrp->ring_role_offloaded)
295 br_mrp_ring_port_open(p->dev, true);
298 p = rcu_dereference(mrp->s_port);
300 if (!mrp->test_monitor) {
301 skb = br_mrp_alloc_test_skb(mrp, p,
302 BR_MRP_PORT_ROLE_SECONDARY);
306 skb_reset_network_header(skb);
310 if (notify_open && !mrp->ring_role_offloaded)
311 br_mrp_ring_port_open(p->dev, true);
317 queue_delayed_work(system_wq, &mrp->test_work,
318 usecs_to_jiffies(mrp->test_interval));
321 /* This function is continuously called when the node has the interconnect role
322 * MIM. It would generate interconnect test frames and will send them on all 3
323 * ports. But will also check if it stop receiving interconnect test frames.
325 static void br_mrp_in_test_work_expired(struct work_struct *work)
327 struct delayed_work *del_work = to_delayed_work(work);
328 struct br_mrp *mrp = container_of(del_work, struct br_mrp, in_test_work);
329 struct net_bridge_port *p;
330 bool notify_open = false;
333 if (time_before_eq(mrp->in_test_end, jiffies))
336 if (mrp->in_test_count_miss < mrp->in_test_max_miss) {
337 mrp->in_test_count_miss++;
339 /* Notify that the interconnect ring is open only if the
340 * interconnect ring state is closed, otherwise it would
341 * continue to notify at every interval.
343 if (mrp->in_state == BR_MRP_IN_STATE_CLOSED)
349 p = rcu_dereference(mrp->p_port);
351 skb = br_mrp_alloc_in_test_skb(mrp, p,
352 BR_MRP_PORT_ROLE_PRIMARY);
356 skb_reset_network_header(skb);
359 if (notify_open && !mrp->in_role_offloaded)
360 br_mrp_in_port_open(p->dev, true);
363 p = rcu_dereference(mrp->s_port);
365 skb = br_mrp_alloc_in_test_skb(mrp, p,
366 BR_MRP_PORT_ROLE_SECONDARY);
370 skb_reset_network_header(skb);
373 if (notify_open && !mrp->in_role_offloaded)
374 br_mrp_in_port_open(p->dev, true);
377 p = rcu_dereference(mrp->i_port);
379 skb = br_mrp_alloc_in_test_skb(mrp, p,
380 BR_MRP_PORT_ROLE_INTER);
384 skb_reset_network_header(skb);
387 if (notify_open && !mrp->in_role_offloaded)
388 br_mrp_in_port_open(p->dev, true);
394 queue_delayed_work(system_wq, &mrp->in_test_work,
395 usecs_to_jiffies(mrp->in_test_interval));
398 /* Deletes the MRP instance.
399 * note: called under rtnl_lock
401 static void br_mrp_del_impl(struct net_bridge *br, struct br_mrp *mrp)
403 struct net_bridge_port *p;
406 /* Stop sending MRP_Test frames */
407 cancel_delayed_work_sync(&mrp->test_work);
408 br_mrp_switchdev_send_ring_test(br, mrp, 0, 0, 0, 0);
410 /* Stop sending MRP_InTest frames if has an interconnect role */
411 cancel_delayed_work_sync(&mrp->in_test_work);
412 br_mrp_switchdev_send_in_test(br, mrp, 0, 0, 0);
414 br_mrp_switchdev_del(br, mrp);
416 /* Reset the ports */
417 p = rtnl_dereference(mrp->p_port);
419 spin_lock_bh(&br->lock);
420 state = netif_running(br->dev) ?
421 BR_STATE_FORWARDING : BR_STATE_DISABLED;
423 p->flags &= ~BR_MRP_AWARE;
424 spin_unlock_bh(&br->lock);
425 br_mrp_port_switchdev_set_state(p, state);
426 rcu_assign_pointer(mrp->p_port, NULL);
429 p = rtnl_dereference(mrp->s_port);
431 spin_lock_bh(&br->lock);
432 state = netif_running(br->dev) ?
433 BR_STATE_FORWARDING : BR_STATE_DISABLED;
435 p->flags &= ~BR_MRP_AWARE;
436 spin_unlock_bh(&br->lock);
437 br_mrp_port_switchdev_set_state(p, state);
438 rcu_assign_pointer(mrp->s_port, NULL);
441 p = rtnl_dereference(mrp->i_port);
443 spin_lock_bh(&br->lock);
444 state = netif_running(br->dev) ?
445 BR_STATE_FORWARDING : BR_STATE_DISABLED;
447 p->flags &= ~BR_MRP_AWARE;
448 spin_unlock_bh(&br->lock);
449 br_mrp_port_switchdev_set_state(p, state);
450 rcu_assign_pointer(mrp->i_port, NULL);
453 hlist_del_rcu(&mrp->list);
456 if (hlist_empty(&br->mrp_list))
457 br_del_frame(br, &mrp_frame_type);
460 /* Adds a new MRP instance.
461 * note: called under rtnl_lock
463 int br_mrp_add(struct net_bridge *br, struct br_mrp_instance *instance)
465 struct net_bridge_port *p;
469 /* If the ring exists, it is not possible to create another one with the
472 mrp = br_mrp_find_id(br, instance->ring_id);
476 if (!br_mrp_get_port(br, instance->p_ifindex) ||
477 !br_mrp_get_port(br, instance->s_ifindex))
480 /* It is not possible to have the same port part of multiple rings */
481 if (!br_mrp_unique_ifindex(br, instance->p_ifindex) ||
482 !br_mrp_unique_ifindex(br, instance->s_ifindex))
485 mrp = kzalloc(sizeof(*mrp), GFP_KERNEL);
489 mrp->ring_id = instance->ring_id;
490 mrp->prio = instance->prio;
492 p = br_mrp_get_port(br, instance->p_ifindex);
493 spin_lock_bh(&br->lock);
494 p->state = BR_STATE_FORWARDING;
495 p->flags |= BR_MRP_AWARE;
496 spin_unlock_bh(&br->lock);
497 rcu_assign_pointer(mrp->p_port, p);
499 p = br_mrp_get_port(br, instance->s_ifindex);
500 spin_lock_bh(&br->lock);
501 p->state = BR_STATE_FORWARDING;
502 p->flags |= BR_MRP_AWARE;
503 spin_unlock_bh(&br->lock);
504 rcu_assign_pointer(mrp->s_port, p);
506 if (hlist_empty(&br->mrp_list))
507 br_add_frame(br, &mrp_frame_type);
509 INIT_DELAYED_WORK(&mrp->test_work, br_mrp_test_work_expired);
510 INIT_DELAYED_WORK(&mrp->in_test_work, br_mrp_in_test_work_expired);
511 hlist_add_tail_rcu(&mrp->list, &br->mrp_list);
513 err = br_mrp_switchdev_add(br, mrp);
520 br_mrp_del_impl(br, mrp);
525 /* Deletes the MRP instance from which the port is part of
526 * note: called under rtnl_lock
528 void br_mrp_port_del(struct net_bridge *br, struct net_bridge_port *p)
530 struct br_mrp *mrp = br_mrp_find_port(br, p);
532 /* If the port is not part of a MRP instance just bail out */
536 br_mrp_del_impl(br, mrp);
539 /* Deletes existing MRP instance based on ring_id
540 * note: called under rtnl_lock
542 int br_mrp_del(struct net_bridge *br, struct br_mrp_instance *instance)
544 struct br_mrp *mrp = br_mrp_find_id(br, instance->ring_id);
549 br_mrp_del_impl(br, mrp);
554 /* Set port state, port state can be forwarding, blocked or disabled
555 * note: already called with rtnl_lock
557 int br_mrp_set_port_state(struct net_bridge_port *p,
558 enum br_mrp_port_state_type state)
562 if (!p || !(p->flags & BR_MRP_AWARE))
565 spin_lock_bh(&p->br->lock);
567 if (state == BR_MRP_PORT_STATE_FORWARDING)
568 port_state = BR_STATE_FORWARDING;
570 port_state = BR_STATE_BLOCKING;
572 p->state = port_state;
573 spin_unlock_bh(&p->br->lock);
575 br_mrp_port_switchdev_set_state(p, port_state);
580 /* Set port role, port role can be primary or secondary
581 * note: already called with rtnl_lock
583 int br_mrp_set_port_role(struct net_bridge_port *p,
584 enum br_mrp_port_role_type role)
588 if (!p || !(p->flags & BR_MRP_AWARE))
591 mrp = br_mrp_find_port(p->br, p);
597 case BR_MRP_PORT_ROLE_PRIMARY:
598 rcu_assign_pointer(mrp->p_port, p);
600 case BR_MRP_PORT_ROLE_SECONDARY:
601 rcu_assign_pointer(mrp->s_port, p);
607 br_mrp_port_switchdev_set_role(p, role);
612 /* Set ring state, ring state can be only Open or Closed
613 * note: already called with rtnl_lock
615 int br_mrp_set_ring_state(struct net_bridge *br,
616 struct br_mrp_ring_state *state)
618 struct br_mrp *mrp = br_mrp_find_id(br, state->ring_id);
623 if (mrp->ring_state == BR_MRP_RING_STATE_CLOSED &&
624 state->ring_state != BR_MRP_RING_STATE_CLOSED)
625 mrp->ring_transitions++;
627 mrp->ring_state = state->ring_state;
629 br_mrp_switchdev_set_ring_state(br, mrp, state->ring_state);
634 /* Set ring role, ring role can be only MRM(Media Redundancy Manager) or
635 * MRC(Media Redundancy Client).
636 * note: already called with rtnl_lock
638 int br_mrp_set_ring_role(struct net_bridge *br,
639 struct br_mrp_ring_role *role)
641 struct br_mrp *mrp = br_mrp_find_id(br, role->ring_id);
642 enum br_mrp_hw_support support;
647 mrp->ring_role = role->ring_role;
649 /* If there is an error just bailed out */
650 support = br_mrp_switchdev_set_ring_role(br, mrp, role->ring_role);
651 if (support == BR_MRP_NONE)
654 /* Now detect if the HW actually applied the role or not. If the HW
655 * applied the role it means that the SW will not to do those operations
656 * anymore. For example if the role ir MRM then the HW will notify the
657 * SW when ring is open, but if the is not pushed to the HW the SW will
658 * need to detect when the ring is open
660 mrp->ring_role_offloaded = support == BR_MRP_SW ? 0 : 1;
665 /* Start to generate or monitor MRP test frames, the frames are generated by
666 * HW and if it fails, they are generated by the SW.
667 * note: already called with rtnl_lock
669 int br_mrp_start_test(struct net_bridge *br,
670 struct br_mrp_start_test *test)
672 struct br_mrp *mrp = br_mrp_find_id(br, test->ring_id);
673 enum br_mrp_hw_support support;
678 /* Try to push it to the HW and if it fails then continue with SW
679 * implementation and if that also fails then return error.
681 support = br_mrp_switchdev_send_ring_test(br, mrp, test->interval,
682 test->max_miss, test->period,
684 if (support == BR_MRP_NONE)
687 if (support == BR_MRP_HW)
690 mrp->test_interval = test->interval;
691 mrp->test_end = jiffies + usecs_to_jiffies(test->period);
692 mrp->test_max_miss = test->max_miss;
693 mrp->test_monitor = test->monitor;
694 mrp->test_count_miss = 0;
695 queue_delayed_work(system_wq, &mrp->test_work,
696 usecs_to_jiffies(test->interval));
701 /* Set in state, int state can be only Open or Closed
702 * note: already called with rtnl_lock
704 int br_mrp_set_in_state(struct net_bridge *br, struct br_mrp_in_state *state)
706 struct br_mrp *mrp = br_mrp_find_in_id(br, state->in_id);
711 if (mrp->in_state == BR_MRP_IN_STATE_CLOSED &&
712 state->in_state != BR_MRP_IN_STATE_CLOSED)
713 mrp->in_transitions++;
715 mrp->in_state = state->in_state;
717 br_mrp_switchdev_set_in_state(br, mrp, state->in_state);
722 /* Set in role, in role can be only MIM(Media Interconnection Manager) or
723 * MIC(Media Interconnection Client).
724 * note: already called with rtnl_lock
726 int br_mrp_set_in_role(struct net_bridge *br, struct br_mrp_in_role *role)
728 struct br_mrp *mrp = br_mrp_find_id(br, role->ring_id);
729 enum br_mrp_hw_support support;
730 struct net_bridge_port *p;
735 if (!br_mrp_get_port(br, role->i_ifindex))
738 if (role->in_role == BR_MRP_IN_ROLE_DISABLED) {
741 /* It is not allowed to disable a port that doesn't exist */
742 p = rtnl_dereference(mrp->i_port);
746 /* Stop the generating MRP_InTest frames */
747 cancel_delayed_work_sync(&mrp->in_test_work);
748 br_mrp_switchdev_send_in_test(br, mrp, 0, 0, 0);
750 /* Remove the port */
751 spin_lock_bh(&br->lock);
752 state = netif_running(br->dev) ?
753 BR_STATE_FORWARDING : BR_STATE_DISABLED;
755 p->flags &= ~BR_MRP_AWARE;
756 spin_unlock_bh(&br->lock);
757 br_mrp_port_switchdev_set_state(p, state);
758 rcu_assign_pointer(mrp->i_port, NULL);
760 mrp->in_role = role->in_role;
766 /* It is not possible to have the same port part of multiple rings */
767 if (!br_mrp_unique_ifindex(br, role->i_ifindex))
770 /* It is not allowed to set a different interconnect port if the mrp
771 * instance has already one. First it needs to be disabled and after
772 * that set the new port
774 if (rcu_access_pointer(mrp->i_port))
777 p = br_mrp_get_port(br, role->i_ifindex);
778 spin_lock_bh(&br->lock);
779 p->state = BR_STATE_FORWARDING;
780 p->flags |= BR_MRP_AWARE;
781 spin_unlock_bh(&br->lock);
782 rcu_assign_pointer(mrp->i_port, p);
784 mrp->in_role = role->in_role;
785 mrp->in_id = role->in_id;
787 /* If there is an error just bailed out */
788 support = br_mrp_switchdev_set_in_role(br, mrp, role->in_id,
789 role->ring_id, role->in_role);
790 if (support == BR_MRP_NONE)
793 /* Now detect if the HW actually applied the role or not. If the HW
794 * applied the role it means that the SW will not to do those operations
795 * anymore. For example if the role is MIM then the HW will notify the
796 * SW when interconnect ring is open, but if the is not pushed to the HW
797 * the SW will need to detect when the interconnect ring is open.
799 mrp->in_role_offloaded = support == BR_MRP_SW ? 0 : 1;
804 /* Start to generate MRP_InTest frames, the frames are generated by
805 * HW and if it fails, they are generated by the SW.
806 * note: already called with rtnl_lock
808 int br_mrp_start_in_test(struct net_bridge *br,
809 struct br_mrp_start_in_test *in_test)
811 struct br_mrp *mrp = br_mrp_find_in_id(br, in_test->in_id);
812 enum br_mrp_hw_support support;
817 if (mrp->in_role != BR_MRP_IN_ROLE_MIM)
820 /* Try to push it to the HW and if it fails then continue with SW
821 * implementation and if that also fails then return error.
823 support = br_mrp_switchdev_send_in_test(br, mrp, in_test->interval,
826 if (support == BR_MRP_NONE)
829 if (support == BR_MRP_HW)
832 mrp->in_test_interval = in_test->interval;
833 mrp->in_test_end = jiffies + usecs_to_jiffies(in_test->period);
834 mrp->in_test_max_miss = in_test->max_miss;
835 mrp->in_test_count_miss = 0;
836 queue_delayed_work(system_wq, &mrp->in_test_work,
837 usecs_to_jiffies(in_test->interval));
842 /* Determine if the frame type is a ring frame */
843 static bool br_mrp_ring_frame(struct sk_buff *skb)
845 const struct br_mrp_tlv_hdr *hdr;
846 struct br_mrp_tlv_hdr _hdr;
848 hdr = skb_header_pointer(skb, sizeof(uint16_t), sizeof(_hdr), &_hdr);
852 if (hdr->type == BR_MRP_TLV_HEADER_RING_TEST ||
853 hdr->type == BR_MRP_TLV_HEADER_RING_TOPO ||
854 hdr->type == BR_MRP_TLV_HEADER_RING_LINK_DOWN ||
855 hdr->type == BR_MRP_TLV_HEADER_RING_LINK_UP ||
856 hdr->type == BR_MRP_TLV_HEADER_OPTION)
862 /* Determine if the frame type is an interconnect frame */
863 static bool br_mrp_in_frame(struct sk_buff *skb)
865 const struct br_mrp_tlv_hdr *hdr;
866 struct br_mrp_tlv_hdr _hdr;
868 hdr = skb_header_pointer(skb, sizeof(uint16_t), sizeof(_hdr), &_hdr);
872 if (hdr->type == BR_MRP_TLV_HEADER_IN_TEST ||
873 hdr->type == BR_MRP_TLV_HEADER_IN_TOPO ||
874 hdr->type == BR_MRP_TLV_HEADER_IN_LINK_DOWN ||
875 hdr->type == BR_MRP_TLV_HEADER_IN_LINK_UP ||
876 hdr->type == BR_MRP_TLV_HEADER_IN_LINK_STATUS)
882 /* Process only MRP Test frame. All the other MRP frames are processed by
883 * userspace application
884 * note: already called with rcu_read_lock
886 static void br_mrp_mrm_process(struct br_mrp *mrp, struct net_bridge_port *port,
889 const struct br_mrp_tlv_hdr *hdr;
890 struct br_mrp_tlv_hdr _hdr;
892 /* Each MRP header starts with a version field which is 16 bits.
893 * Therefore skip the version and get directly the TLV header.
895 hdr = skb_header_pointer(skb, sizeof(uint16_t), sizeof(_hdr), &_hdr);
899 if (hdr->type != BR_MRP_TLV_HEADER_RING_TEST)
902 mrp->test_count_miss = 0;
904 /* Notify the userspace that the ring is closed only when the ring is
907 if (mrp->ring_state != BR_MRP_RING_STATE_CLOSED)
908 br_mrp_ring_port_open(port->dev, false);
911 /* Determine if the test hdr has a better priority than the node */
912 static bool br_mrp_test_better_than_own(struct br_mrp *mrp,
913 struct net_bridge *br,
914 const struct br_mrp_ring_test_hdr *hdr)
916 u16 prio = be16_to_cpu(hdr->prio);
918 if (prio < mrp->prio ||
919 (prio == mrp->prio &&
920 ether_addr_to_u64(hdr->sa) < ether_addr_to_u64(br->dev->dev_addr)))
926 /* Process only MRP Test frame. All the other MRP frames are processed by
927 * userspace application
928 * note: already called with rcu_read_lock
930 static void br_mrp_mra_process(struct br_mrp *mrp, struct net_bridge *br,
931 struct net_bridge_port *port,
934 const struct br_mrp_ring_test_hdr *test_hdr;
935 struct br_mrp_ring_test_hdr _test_hdr;
936 const struct br_mrp_tlv_hdr *hdr;
937 struct br_mrp_tlv_hdr _hdr;
939 /* Each MRP header starts with a version field which is 16 bits.
940 * Therefore skip the version and get directly the TLV header.
942 hdr = skb_header_pointer(skb, sizeof(uint16_t), sizeof(_hdr), &_hdr);
946 if (hdr->type != BR_MRP_TLV_HEADER_RING_TEST)
949 test_hdr = skb_header_pointer(skb, sizeof(uint16_t) + sizeof(_hdr),
950 sizeof(_test_hdr), &_test_hdr);
954 /* Only frames that have a better priority than the node will
955 * clear the miss counter because otherwise the node will need to behave
958 if (br_mrp_test_better_than_own(mrp, br, test_hdr))
959 mrp->test_count_miss = 0;
962 /* Process only MRP InTest frame. All the other MRP frames are processed by
963 * userspace application
964 * note: already called with rcu_read_lock
966 static bool br_mrp_mim_process(struct br_mrp *mrp, struct net_bridge_port *port,
969 const struct br_mrp_in_test_hdr *in_hdr;
970 struct br_mrp_in_test_hdr _in_hdr;
971 const struct br_mrp_tlv_hdr *hdr;
972 struct br_mrp_tlv_hdr _hdr;
974 /* Each MRP header starts with a version field which is 16 bits.
975 * Therefore skip the version and get directly the TLV header.
977 hdr = skb_header_pointer(skb, sizeof(uint16_t), sizeof(_hdr), &_hdr);
981 /* The check for InTest frame type was already done */
982 in_hdr = skb_header_pointer(skb, sizeof(uint16_t) + sizeof(_hdr),
983 sizeof(_in_hdr), &_in_hdr);
987 /* It needs to process only it's own InTest frames. */
988 if (mrp->in_id != ntohs(in_hdr->id))
991 mrp->in_test_count_miss = 0;
993 /* Notify the userspace that the ring is closed only when the ring is
996 if (mrp->in_state != BR_MRP_IN_STATE_CLOSED)
997 br_mrp_in_port_open(port->dev, false);
1002 /* Get the MRP frame type
1003 * note: already called with rcu_read_lock
1005 static u8 br_mrp_get_frame_type(struct sk_buff *skb)
1007 const struct br_mrp_tlv_hdr *hdr;
1008 struct br_mrp_tlv_hdr _hdr;
1010 /* Each MRP header starts with a version field which is 16 bits.
1011 * Therefore skip the version and get directly the TLV header.
1013 hdr = skb_header_pointer(skb, sizeof(uint16_t), sizeof(_hdr), &_hdr);
1020 static bool br_mrp_mrm_behaviour(struct br_mrp *mrp)
1022 if (mrp->ring_role == BR_MRP_RING_ROLE_MRM ||
1023 (mrp->ring_role == BR_MRP_RING_ROLE_MRA && !mrp->test_monitor))
1029 static bool br_mrp_mrc_behaviour(struct br_mrp *mrp)
1031 if (mrp->ring_role == BR_MRP_RING_ROLE_MRC ||
1032 (mrp->ring_role == BR_MRP_RING_ROLE_MRA && mrp->test_monitor))
1038 /* This will just forward the frame to the other mrp ring ports, depending on
1039 * the frame type, ring role and interconnect role
1040 * note: already called with rcu_read_lock
1042 static int br_mrp_rcv(struct net_bridge_port *p,
1043 struct sk_buff *skb, struct net_device *dev)
1045 struct net_bridge_port *p_port, *s_port, *i_port = NULL;
1046 struct net_bridge_port *p_dst, *s_dst, *i_dst = NULL;
1047 struct net_bridge *br;
1050 /* If port is disabled don't accept any frames */
1051 if (p->state == BR_STATE_DISABLED)
1055 mrp = br_mrp_find_port(br, p);
1059 p_port = rcu_dereference(mrp->p_port);
1064 s_port = rcu_dereference(mrp->s_port);
1069 /* If the frame is a ring frame then it is not required to check the
1070 * interconnect role and ports to process or forward the frame
1072 if (br_mrp_ring_frame(skb)) {
1073 /* If the role is MRM then don't forward the frames */
1074 if (mrp->ring_role == BR_MRP_RING_ROLE_MRM) {
1075 br_mrp_mrm_process(mrp, p, skb);
1079 /* If the role is MRA then don't forward the frames if it
1080 * behaves as MRM node
1082 if (mrp->ring_role == BR_MRP_RING_ROLE_MRA) {
1083 if (!mrp->test_monitor) {
1084 br_mrp_mrm_process(mrp, p, skb);
1088 br_mrp_mra_process(mrp, br, p, skb);
1094 if (br_mrp_in_frame(skb)) {
1095 u8 in_type = br_mrp_get_frame_type(skb);
1097 i_port = rcu_dereference(mrp->i_port);
1100 /* If the ring port is in block state it should not forward
1103 if (br_mrp_is_ring_port(p_port, s_port, p) &&
1104 p->state == BR_STATE_BLOCKING &&
1105 in_type == BR_MRP_TLV_HEADER_IN_TEST)
1108 /* Nodes that behaves as MRM needs to stop forwarding the
1109 * frames in case the ring is closed, otherwise will be a loop.
1110 * In this case the frame is no forward between the ring ports.
1112 if (br_mrp_mrm_behaviour(mrp) &&
1113 br_mrp_is_ring_port(p_port, s_port, p) &&
1114 (s_port->state != BR_STATE_FORWARDING ||
1115 p_port->state != BR_STATE_FORWARDING)) {
1120 /* A node that behaves as MRC and doesn't have a interconnect
1121 * role then it should forward all frames between the ring ports
1122 * because it doesn't have an interconnect port
1124 if (br_mrp_mrc_behaviour(mrp) &&
1125 mrp->in_role == BR_MRP_IN_ROLE_DISABLED)
1128 if (mrp->in_role == BR_MRP_IN_ROLE_MIM) {
1129 if (in_type == BR_MRP_TLV_HEADER_IN_TEST) {
1130 /* MIM should not forward it's own InTest
1133 if (br_mrp_mim_process(mrp, p, skb)) {
1136 if (br_mrp_is_ring_port(p_port, s_port,
1140 if (br_mrp_is_in_port(i_port, p))
1144 /* MIM should forward IntLinkChange/Status and
1145 * IntTopoChange between ring ports but MIM
1146 * should not forward IntLinkChange/Status and
1147 * IntTopoChange if the frame was received at
1148 * the interconnect port
1150 if (br_mrp_is_ring_port(p_port, s_port, p))
1153 if (br_mrp_is_in_port(i_port, p))
1158 if (mrp->in_role == BR_MRP_IN_ROLE_MIC) {
1159 /* MIC should forward InTest frames on all ports
1160 * regardless of the received port
1162 if (in_type == BR_MRP_TLV_HEADER_IN_TEST)
1165 /* MIC should forward IntLinkChange frames only if they
1166 * are received on ring ports to all the ports
1168 if (br_mrp_is_ring_port(p_port, s_port, p) &&
1169 (in_type == BR_MRP_TLV_HEADER_IN_LINK_UP ||
1170 in_type == BR_MRP_TLV_HEADER_IN_LINK_DOWN))
1173 /* MIC should forward IntLinkStatus frames only to
1174 * interconnect port if it was received on a ring port.
1175 * If it is received on interconnect port then, it
1176 * should be forward on both ring ports
1178 if (br_mrp_is_ring_port(p_port, s_port, p) &&
1179 in_type == BR_MRP_TLV_HEADER_IN_LINK_STATUS) {
1184 /* Should forward the InTopo frames only between the
1187 if (in_type == BR_MRP_TLV_HEADER_IN_TOPO) {
1192 /* In all the other cases don't forward the frames */
1199 br_forward(p_dst, skb, true, false);
1201 br_forward(s_dst, skb, true, false);
1203 br_forward(i_dst, skb, true, false);
1209 /* Check if the frame was received on a port that is part of MRP ring
1210 * and if the frame has MRP eth. In that case process the frame otherwise do
1211 * normal forwarding.
1212 * note: already called with rcu_read_lock
1214 static int br_mrp_process(struct net_bridge_port *p, struct sk_buff *skb)
1216 /* If there is no MRP instance do normal forwarding */
1217 if (likely(!(p->flags & BR_MRP_AWARE)))
1220 return br_mrp_rcv(p, skb, p->dev);
1225 bool br_mrp_enabled(struct net_bridge *br)
1227 return !hlist_empty(&br->mrp_list);