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);
415 case BR_MRP_PORT_ROLE_PRIMARY:
416 rcu_assign_pointer(mrp->p_port, p);
418 case BR_MRP_PORT_ROLE_SECONDARY:
419 rcu_assign_pointer(mrp->s_port, p);
425 br_mrp_port_switchdev_set_role(p, role);
430 /* Set ring state, ring state can be only Open or Closed
431 * note: already called with rtnl_lock
433 int br_mrp_set_ring_state(struct net_bridge *br,
434 struct br_mrp_ring_state *state)
436 struct br_mrp *mrp = br_mrp_find_id(br, state->ring_id);
441 if (mrp->ring_state == BR_MRP_RING_STATE_CLOSED &&
442 state->ring_state != BR_MRP_RING_STATE_CLOSED)
443 mrp->ring_transitions++;
445 mrp->ring_state = state->ring_state;
447 br_mrp_switchdev_set_ring_state(br, mrp, state->ring_state);
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
456 int br_mrp_set_ring_role(struct net_bridge *br,
457 struct br_mrp_ring_role *role)
459 struct br_mrp *mrp = br_mrp_find_id(br, role->ring_id);
465 mrp->ring_role = role->ring_role;
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)
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
478 mrp->ring_role_offloaded = err == -EOPNOTSUPP ? 0 : 1;
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
487 int br_mrp_start_test(struct net_bridge *br,
488 struct br_mrp_start_test *test)
490 struct br_mrp *mrp = br_mrp_find_id(br, test->ring_id);
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.
498 if (!br_mrp_switchdev_send_ring_test(br, mrp, test->interval,
499 test->max_miss, test->period,
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));
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
518 static void br_mrp_mrm_process(struct br_mrp *mrp, struct net_bridge_port *port,
521 const struct br_mrp_tlv_hdr *hdr;
522 struct br_mrp_tlv_hdr _hdr;
524 /* Each MRP header starts with a version field which is 16 bits.
525 * Therefore skip the version and get directly the TLV header.
527 hdr = skb_header_pointer(skb, sizeof(uint16_t), sizeof(_hdr), &_hdr);
531 if (hdr->type != BR_MRP_TLV_HEADER_RING_TEST)
534 mrp->test_count_miss = 0;
536 /* Notify the userspace that the ring is closed only when the ring is
539 if (mrp->ring_state != BR_MRP_RING_STATE_CLOSED)
540 br_mrp_port_open(port->dev, false);
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)
548 u16 prio = be16_to_cpu(hdr->prio);
550 if (prio < mrp->prio ||
551 (prio == mrp->prio &&
552 ether_addr_to_u64(hdr->sa) < ether_addr_to_u64(br->dev->dev_addr)))
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
562 static void br_mrp_mra_process(struct br_mrp *mrp, struct net_bridge *br,
563 struct net_bridge_port *port,
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;
571 /* Each MRP header starts with a version field which is 16 bits.
572 * Therefore skip the version and get directly the TLV header.
574 hdr = skb_header_pointer(skb, sizeof(uint16_t), sizeof(_hdr), &_hdr);
578 if (hdr->type != BR_MRP_TLV_HEADER_RING_TEST)
581 test_hdr = skb_header_pointer(skb, sizeof(uint16_t) + sizeof(_hdr),
582 sizeof(_test_hdr), &_test_hdr);
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
590 if (br_mrp_test_better_than_own(mrp, br, test_hdr))
591 mrp->test_count_miss = 0;
594 /* This will just forward the frame to the other mrp ring port(MRC role) or will
596 * note: already called with rcu_read_lock
598 static int br_mrp_rcv(struct net_bridge_port *p,
599 struct sk_buff *skb, struct net_device *dev)
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;
607 /* If port is disabled don't accept any frames */
608 if (p->state == BR_STATE_DISABLED)
612 mrp = br_mrp_find_port(br, p);
616 p_port = rcu_dereference(mrp->p_port);
620 s_port = rcu_dereference(mrp->s_port);
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);
630 /* If the role is MRA then don't forward the frames if it behaves as
633 if (mrp->ring_role == BR_MRP_RING_ROLE_MRA) {
634 if (!mrp->test_monitor) {
635 br_mrp_mrm_process(mrp, p, skb);
639 br_mrp_mra_process(mrp, br, p, skb);
642 /* Clone the frame and forward it on the other MRP port */
643 nskb = skb_clone(skb, GFP_ATOMIC);
656 skb_push(nskb, ETH_HLEN);
657 dev_queue_xmit(nskb);
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
665 * note: already called with rcu_read_lock
667 int br_mrp_process(struct net_bridge_port *p, struct sk_buff *skb)
669 /* If there is no MRP instance do normal forwarding */
670 if (likely(!(p->flags & BR_MRP_AWARE)))
673 if (unlikely(skb->protocol == htons(ETH_P_MRP)))
674 return br_mrp_rcv(p, skb, p->dev);
680 bool br_mrp_enabled(struct net_bridge *br)
682 return !list_empty(&br->mrp_list);