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 };
8 static struct net_bridge_port
*br_mrp_get_port(struct net_bridge
*br
,
11 struct net_bridge_port
*res
= NULL
;
12 struct net_bridge_port
*port
;
14 list_for_each_entry(port
, &br
->port_list
, list
) {
15 if (port
->dev
->ifindex
== ifindex
) {
24 static struct br_mrp
*br_mrp_find_id(struct net_bridge
*br
, u32 ring_id
)
26 struct br_mrp
*res
= NULL
;
29 list_for_each_entry_rcu(mrp
, &br
->mrp_list
, list
,
30 lockdep_rtnl_is_held()) {
31 if (mrp
->ring_id
== ring_id
) {
40 static bool br_mrp_unique_ifindex(struct net_bridge
*br
, u32 ifindex
)
44 list_for_each_entry_rcu(mrp
, &br
->mrp_list
, list
,
45 lockdep_rtnl_is_held()) {
46 struct net_bridge_port
*p
;
48 p
= rtnl_dereference(mrp
->p_port
);
49 if (p
&& p
->dev
->ifindex
== ifindex
)
52 p
= rtnl_dereference(mrp
->s_port
);
53 if (p
&& p
->dev
->ifindex
== ifindex
)
60 static struct br_mrp
*br_mrp_find_port(struct net_bridge
*br
,
61 struct net_bridge_port
*p
)
63 struct br_mrp
*res
= NULL
;
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
) {
78 static int br_mrp_next_seq(struct br_mrp
*mrp
)
84 static struct sk_buff
*br_mrp_skb_alloc(struct net_bridge_port
*p
,
85 const u8
*src
, const u8
*dst
)
87 struct ethhdr
*eth_hdr
;
91 skb
= dev_alloc_skb(MRP_MAX_FRAME_LENGTH
);
96 skb
->protocol
= htons(ETH_P_MRP
);
97 skb
->priority
= MRP_FRAME_PRIO
;
98 skb_reserve(skb
, sizeof(*eth_hdr
));
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
);
105 version
= skb_put(skb
, sizeof(*version
));
106 *version
= cpu_to_be16(MRP_VERSION
);
111 static void br_mrp_skb_tlv(struct sk_buff
*skb
,
112 enum br_mrp_tlv_header_type type
,
115 struct br_mrp_tlv_hdr
*hdr
;
117 hdr
= skb_put(skb
, sizeof(*hdr
));
119 hdr
->length
= length
;
122 static void br_mrp_skb_common(struct sk_buff
*skb
, struct br_mrp
*mrp
)
124 struct br_mrp_common_hdr
*hdr
;
126 br_mrp_skb_tlv(skb
, BR_MRP_TLV_HEADER_COMMON
, sizeof(*hdr
));
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
);
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
)
137 struct br_mrp_ring_test_hdr
*hdr
= NULL
;
138 struct sk_buff
*skb
= NULL
;
143 skb
= br_mrp_skb_alloc(p
, p
->dev
->dev_addr
, mrp_test_dmac
);
147 br_mrp_skb_tlv(skb
, BR_MRP_TLV_HEADER_RING_TEST
, sizeof(*hdr
));
148 hdr
= skb_put(skb
, sizeof(*hdr
));
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
));
157 br_mrp_skb_common(skb
, mrp
);
158 br_mrp_skb_tlv(skb
, BR_MRP_TLV_HEADER_END
, 0x0);
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.
173 static void br_mrp_test_work_expired(struct work_struct
*work
)
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;
181 if (time_before_eq(mrp
->test_end
, jiffies
))
184 if (mrp
->test_count_miss
< mrp
->test_max_miss
) {
185 mrp
->test_count_miss
++;
187 /* Notify that the ring is open only if the ring state is
188 * closed, otherwise it would continue to notify at every
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
196 if (mrp
->ring_state
== BR_MRP_RING_STATE_CLOSED
||
203 p
= rcu_dereference(mrp
->p_port
);
205 if (!mrp
->test_monitor
) {
206 skb
= br_mrp_alloc_test_skb(mrp
, p
,
207 BR_MRP_PORT_ROLE_PRIMARY
);
211 skb_reset_network_header(skb
);
215 if (notify_open
&& !mrp
->ring_role_offloaded
)
216 br_mrp_port_open(p
->dev
, true);
219 p
= rcu_dereference(mrp
->s_port
);
221 if (!mrp
->test_monitor
) {
222 skb
= br_mrp_alloc_test_skb(mrp
, p
,
223 BR_MRP_PORT_ROLE_SECONDARY
);
227 skb_reset_network_header(skb
);
231 if (notify_open
&& !mrp
->ring_role_offloaded
)
232 br_mrp_port_open(p
->dev
, true);
238 queue_delayed_work(system_wq
, &mrp
->test_work
,
239 usecs_to_jiffies(mrp
->test_interval
));
242 /* Deletes the MRP instance.
243 * note: called under rtnl_lock
245 static void br_mrp_del_impl(struct net_bridge
*br
, struct br_mrp
*mrp
)
247 struct net_bridge_port
*p
;
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);
254 br_mrp_switchdev_del(br
, mrp
);
256 /* Reset the ports */
257 p
= rtnl_dereference(mrp
->p_port
);
259 spin_lock_bh(&br
->lock
);
260 state
= netif_running(br
->dev
) ?
261 BR_STATE_FORWARDING
: BR_STATE_DISABLED
;
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
);
269 p
= rtnl_dereference(mrp
->s_port
);
271 spin_lock_bh(&br
->lock
);
272 state
= netif_running(br
->dev
) ?
273 BR_STATE_FORWARDING
: BR_STATE_DISABLED
;
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
);
281 list_del_rcu(&mrp
->list
);
285 /* Adds a new MRP instance.
286 * note: called under rtnl_lock
288 int br_mrp_add(struct net_bridge
*br
, struct br_mrp_instance
*instance
)
290 struct net_bridge_port
*p
;
294 /* If the ring exists, it is not possible to create another one with the
297 mrp
= br_mrp_find_id(br
, instance
->ring_id
);
301 if (!br_mrp_get_port(br
, instance
->p_ifindex
) ||
302 !br_mrp_get_port(br
, instance
->s_ifindex
))
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
))
310 mrp
= kzalloc(sizeof(*mrp
), GFP_KERNEL
);
314 mrp
->ring_id
= instance
->ring_id
;
315 mrp
->prio
= instance
->prio
;
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
);
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
);
331 INIT_DELAYED_WORK(&mrp
->test_work
, br_mrp_test_work_expired
);
332 list_add_tail_rcu(&mrp
->list
, &br
->mrp_list
);
334 err
= br_mrp_switchdev_add(br
, mrp
);
341 br_mrp_del_impl(br
, mrp
);
346 /* Deletes the MRP instance from which the port is part of
347 * note: called under rtnl_lock
349 void br_mrp_port_del(struct net_bridge
*br
, struct net_bridge_port
*p
)
351 struct br_mrp
*mrp
= br_mrp_find_port(br
, p
);
353 /* If the port is not part of a MRP instance just bail out */
357 br_mrp_del_impl(br
, mrp
);
360 /* Deletes existing MRP instance based on ring_id
361 * note: called under rtnl_lock
363 int br_mrp_del(struct net_bridge
*br
, struct br_mrp_instance
*instance
)
365 struct br_mrp
*mrp
= br_mrp_find_id(br
, instance
->ring_id
);
370 br_mrp_del_impl(br
, mrp
);
375 /* Set port state, port state can be forwarding, blocked or disabled
376 * note: already called with rtnl_lock
378 int br_mrp_set_port_state(struct net_bridge_port
*p
,
379 enum br_mrp_port_state_type state
)
381 if (!p
|| !(p
->flags
& BR_MRP_AWARE
))
384 spin_lock_bh(&p
->br
->lock
);
386 if (state
== BR_MRP_PORT_STATE_FORWARDING
)
387 p
->state
= BR_STATE_FORWARDING
;
389 p
->state
= BR_STATE_BLOCKING
;
391 spin_unlock_bh(&p
->br
->lock
);
393 br_mrp_port_switchdev_set_state(p
, state
);
398 /* Set port role, port role can be primary or secondary
399 * note: already called with rtnl_lock
401 int br_mrp_set_port_role(struct net_bridge_port
*p
,
402 enum br_mrp_port_role_type role
)
406 if (!p
|| !(p
->flags
& BR_MRP_AWARE
))
409 mrp
= br_mrp_find_port(p
->br
, p
);
414 if (role
== BR_MRP_PORT_ROLE_PRIMARY
)
415 rcu_assign_pointer(mrp
->p_port
, p
);
417 rcu_assign_pointer(mrp
->s_port
, p
);
419 br_mrp_port_switchdev_set_role(p
, role
);
424 /* Set ring state, ring state can be only Open or Closed
425 * note: already called with rtnl_lock
427 int br_mrp_set_ring_state(struct net_bridge
*br
,
428 struct br_mrp_ring_state
*state
)
430 struct br_mrp
*mrp
= br_mrp_find_id(br
, state
->ring_id
);
435 if (mrp
->ring_state
== BR_MRP_RING_STATE_CLOSED
&&
436 state
->ring_state
!= BR_MRP_RING_STATE_CLOSED
)
437 mrp
->ring_transitions
++;
439 mrp
->ring_state
= state
->ring_state
;
441 br_mrp_switchdev_set_ring_state(br
, mrp
, state
->ring_state
);
446 /* Set ring role, ring role can be only MRM(Media Redundancy Manager) or
447 * MRC(Media Redundancy Client).
448 * note: already called with rtnl_lock
450 int br_mrp_set_ring_role(struct net_bridge
*br
,
451 struct br_mrp_ring_role
*role
)
453 struct br_mrp
*mrp
= br_mrp_find_id(br
, role
->ring_id
);
459 mrp
->ring_role
= role
->ring_role
;
461 /* If there is an error just bailed out */
462 err
= br_mrp_switchdev_set_ring_role(br
, mrp
, role
->ring_role
);
463 if (err
&& err
!= -EOPNOTSUPP
)
466 /* Now detect if the HW actually applied the role or not. If the HW
467 * applied the role it means that the SW will not to do those operations
468 * anymore. For example if the role ir MRM then the HW will notify the
469 * SW when ring is open, but if the is not pushed to the HW the SW will
470 * need to detect when the ring is open
472 mrp
->ring_role_offloaded
= err
== -EOPNOTSUPP
? 0 : 1;
477 /* Start to generate or monitor MRP test frames, the frames are generated by
478 * HW and if it fails, they are generated by the SW.
479 * note: already called with rtnl_lock
481 int br_mrp_start_test(struct net_bridge
*br
,
482 struct br_mrp_start_test
*test
)
484 struct br_mrp
*mrp
= br_mrp_find_id(br
, test
->ring_id
);
489 /* Try to push it to the HW and if it fails then continue with SW
490 * implementation and if that also fails then return error.
492 if (!br_mrp_switchdev_send_ring_test(br
, mrp
, test
->interval
,
493 test
->max_miss
, test
->period
,
497 mrp
->test_interval
= test
->interval
;
498 mrp
->test_end
= jiffies
+ usecs_to_jiffies(test
->period
);
499 mrp
->test_max_miss
= test
->max_miss
;
500 mrp
->test_monitor
= test
->monitor
;
501 mrp
->test_count_miss
= 0;
502 queue_delayed_work(system_wq
, &mrp
->test_work
,
503 usecs_to_jiffies(test
->interval
));
508 /* Process only MRP Test frame. All the other MRP frames are processed by
509 * userspace application
510 * note: already called with rcu_read_lock
512 static void br_mrp_mrm_process(struct br_mrp
*mrp
, struct net_bridge_port
*port
,
515 const struct br_mrp_tlv_hdr
*hdr
;
516 struct br_mrp_tlv_hdr _hdr
;
518 /* Each MRP header starts with a version field which is 16 bits.
519 * Therefore skip the version and get directly the TLV header.
521 hdr
= skb_header_pointer(skb
, sizeof(uint16_t), sizeof(_hdr
), &_hdr
);
525 if (hdr
->type
!= BR_MRP_TLV_HEADER_RING_TEST
)
528 mrp
->test_count_miss
= 0;
530 /* Notify the userspace that the ring is closed only when the ring is
533 if (mrp
->ring_state
!= BR_MRP_RING_STATE_CLOSED
)
534 br_mrp_port_open(port
->dev
, false);
537 /* Determin if the test hdr has a better priority than the node */
538 static bool br_mrp_test_better_than_own(struct br_mrp
*mrp
,
539 struct net_bridge
*br
,
540 const struct br_mrp_ring_test_hdr
*hdr
)
542 u16 prio
= be16_to_cpu(hdr
->prio
);
544 if (prio
< mrp
->prio
||
545 (prio
== mrp
->prio
&&
546 ether_addr_to_u64(hdr
->sa
) < ether_addr_to_u64(br
->dev
->dev_addr
)))
552 /* Process only MRP Test frame. All the other MRP frames are processed by
553 * userspace application
554 * note: already called with rcu_read_lock
556 static void br_mrp_mra_process(struct br_mrp
*mrp
, struct net_bridge
*br
,
557 struct net_bridge_port
*port
,
560 const struct br_mrp_ring_test_hdr
*test_hdr
;
561 struct br_mrp_ring_test_hdr _test_hdr
;
562 const struct br_mrp_tlv_hdr
*hdr
;
563 struct br_mrp_tlv_hdr _hdr
;
565 /* Each MRP header starts with a version field which is 16 bits.
566 * Therefore skip the version and get directly the TLV header.
568 hdr
= skb_header_pointer(skb
, sizeof(uint16_t), sizeof(_hdr
), &_hdr
);
572 if (hdr
->type
!= BR_MRP_TLV_HEADER_RING_TEST
)
575 test_hdr
= skb_header_pointer(skb
, sizeof(uint16_t) + sizeof(_hdr
),
576 sizeof(_test_hdr
), &_test_hdr
);
580 /* Only frames that have a better priority than the node will
581 * clear the miss counter because otherwise the node will need to behave
584 if (br_mrp_test_better_than_own(mrp
, br
, test_hdr
))
585 mrp
->test_count_miss
= 0;
588 /* This will just forward the frame to the other mrp ring port(MRC role) or will
590 * note: already called with rcu_read_lock
592 static int br_mrp_rcv(struct net_bridge_port
*p
,
593 struct sk_buff
*skb
, struct net_device
*dev
)
595 struct net_device
*s_dev
, *p_dev
, *d_dev
;
596 struct net_bridge_port
*p_port
, *s_port
;
597 struct net_bridge
*br
;
598 struct sk_buff
*nskb
;
601 /* If port is disabled don't accept any frames */
602 if (p
->state
== BR_STATE_DISABLED
)
606 mrp
= br_mrp_find_port(br
, p
);
610 p_port
= rcu_dereference(mrp
->p_port
);
614 s_port
= rcu_dereference(mrp
->s_port
);
618 /* If the role is MRM then don't forward the frames */
619 if (mrp
->ring_role
== BR_MRP_RING_ROLE_MRM
) {
620 br_mrp_mrm_process(mrp
, p
, skb
);
624 /* If the role is MRA then don't forward the frames if it behaves as
627 if (mrp
->ring_role
== BR_MRP_RING_ROLE_MRA
) {
628 if (!mrp
->test_monitor
) {
629 br_mrp_mrm_process(mrp
, p
, skb
);
633 br_mrp_mra_process(mrp
, br
, p
, skb
);
636 /* Clone the frame and forward it on the other MRP port */
637 nskb
= skb_clone(skb
, GFP_ATOMIC
);
650 skb_push(nskb
, ETH_HLEN
);
651 dev_queue_xmit(nskb
);
656 /* Check if the frame was received on a port that is part of MRP ring
657 * and if the frame has MRP eth. In that case process the frame otherwise do
659 * note: already called with rcu_read_lock
661 int br_mrp_process(struct net_bridge_port
*p
, struct sk_buff
*skb
)
663 /* If there is no MRP instance do normal forwarding */
664 if (likely(!(p
->flags
& BR_MRP_AWARE
)))
667 if (unlikely(skb
->protocol
== htons(ETH_P_MRP
)))
668 return br_mrp_rcv(p
, skb
, p
->dev
);
674 bool br_mrp_enabled(struct net_bridge
*br
)
676 return !list_empty(&br
->mrp_list
);