WIP FPC-III support
[linux/fpc-iii.git] / net / bridge / br_mrp.c
blobcec2c4e4561d06540a31a47ca6ae1a8f47c168b2
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)
20 if (port == p_port ||
21 port == s_port)
22 return true;
24 return false;
27 static bool br_mrp_is_in_port(struct net_bridge_port *i_port,
28 struct net_bridge_port *port)
30 if (port == i_port)
31 return true;
33 return false;
36 static struct net_bridge_port *br_mrp_get_port(struct net_bridge *br,
37 u32 ifindex)
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) {
44 res = port;
45 break;
49 return res;
52 static struct br_mrp *br_mrp_find_id(struct net_bridge *br, u32 ring_id)
54 struct br_mrp *res = NULL;
55 struct br_mrp *mrp;
57 hlist_for_each_entry_rcu(mrp, &br->mrp_list, list,
58 lockdep_rtnl_is_held()) {
59 if (mrp->ring_id == ring_id) {
60 res = mrp;
61 break;
65 return res;
68 static struct br_mrp *br_mrp_find_in_id(struct net_bridge *br, u32 in_id)
70 struct br_mrp *res = NULL;
71 struct br_mrp *mrp;
73 hlist_for_each_entry_rcu(mrp, &br->mrp_list, list,
74 lockdep_rtnl_is_held()) {
75 if (mrp->in_id == in_id) {
76 res = mrp;
77 break;
81 return res;
84 static bool br_mrp_unique_ifindex(struct net_bridge *br, u32 ifindex)
86 struct br_mrp *mrp;
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)
94 return false;
96 p = rtnl_dereference(mrp->s_port);
97 if (p && p->dev->ifindex == ifindex)
98 return false;
100 p = rtnl_dereference(mrp->i_port);
101 if (p && p->dev->ifindex == ifindex)
102 return false;
105 return true;
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;
112 struct br_mrp *mrp;
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) {
119 res = mrp;
120 break;
124 return res;
127 static int br_mrp_next_seq(struct br_mrp *mrp)
129 mrp->seq_id++;
130 return mrp->seq_id;
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;
137 struct sk_buff *skb;
138 __be16 *version;
140 skb = dev_alloc_skb(MRP_MAX_FRAME_LENGTH);
141 if (!skb)
142 return NULL;
144 skb->dev = p->dev;
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);
157 return skb;
160 static void br_mrp_skb_tlv(struct sk_buff *skb,
161 enum br_mrp_tlv_header_type type,
162 u8 length)
164 struct br_mrp_tlv_hdr *hdr;
166 hdr = skb_put(skb, sizeof(*hdr));
167 hdr->type = type;
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;
189 if (!p)
190 return NULL;
192 skb = br_mrp_skb_alloc(p, p->dev->dev_addr, mrp_test_dmac);
193 if (!skb)
194 return NULL;
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);
209 return skb;
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;
219 if (!p)
220 return NULL;
222 skb = br_mrp_skb_alloc(p, p->dev->dev_addr, mrp_in_test_dmac);
223 if (!skb)
224 return NULL;
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);
239 return skb;
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;
258 struct sk_buff *skb;
260 if (time_before_eq(mrp->test_end, jiffies))
261 return;
263 if (mrp->test_count_miss < mrp->test_max_miss) {
264 mrp->test_count_miss++;
265 } else {
266 /* Notify that the ring is open only if the ring state is
267 * closed, otherwise it would continue to notify at every
268 * interval.
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
273 * the role of a MRM.
275 if (mrp->ring_state == BR_MRP_RING_STATE_CLOSED ||
276 mrp->test_monitor)
277 notify_open = true;
280 rcu_read_lock();
282 p = rcu_dereference(mrp->p_port);
283 if (p) {
284 if (!mrp->test_monitor) {
285 skb = br_mrp_alloc_test_skb(mrp, p,
286 BR_MRP_PORT_ROLE_PRIMARY);
287 if (!skb)
288 goto out;
290 skb_reset_network_header(skb);
291 dev_queue_xmit(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);
299 if (p) {
300 if (!mrp->test_monitor) {
301 skb = br_mrp_alloc_test_skb(mrp, p,
302 BR_MRP_PORT_ROLE_SECONDARY);
303 if (!skb)
304 goto out;
306 skb_reset_network_header(skb);
307 dev_queue_xmit(skb);
310 if (notify_open && !mrp->ring_role_offloaded)
311 br_mrp_ring_port_open(p->dev, true);
314 out:
315 rcu_read_unlock();
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;
331 struct sk_buff *skb;
333 if (time_before_eq(mrp->in_test_end, jiffies))
334 return;
336 if (mrp->in_test_count_miss < mrp->in_test_max_miss) {
337 mrp->in_test_count_miss++;
338 } else {
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)
344 notify_open = true;
347 rcu_read_lock();
349 p = rcu_dereference(mrp->p_port);
350 if (p) {
351 skb = br_mrp_alloc_in_test_skb(mrp, p,
352 BR_MRP_PORT_ROLE_PRIMARY);
353 if (!skb)
354 goto out;
356 skb_reset_network_header(skb);
357 dev_queue_xmit(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);
364 if (p) {
365 skb = br_mrp_alloc_in_test_skb(mrp, p,
366 BR_MRP_PORT_ROLE_SECONDARY);
367 if (!skb)
368 goto out;
370 skb_reset_network_header(skb);
371 dev_queue_xmit(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);
378 if (p) {
379 skb = br_mrp_alloc_in_test_skb(mrp, p,
380 BR_MRP_PORT_ROLE_INTER);
381 if (!skb)
382 goto out;
384 skb_reset_network_header(skb);
385 dev_queue_xmit(skb);
387 if (notify_open && !mrp->in_role_offloaded)
388 br_mrp_in_port_open(p->dev, true);
391 out:
392 rcu_read_unlock();
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;
404 u8 state;
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);
418 if (p) {
419 spin_lock_bh(&br->lock);
420 state = netif_running(br->dev) ?
421 BR_STATE_FORWARDING : BR_STATE_DISABLED;
422 p->state = state;
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);
430 if (p) {
431 spin_lock_bh(&br->lock);
432 state = netif_running(br->dev) ?
433 BR_STATE_FORWARDING : BR_STATE_DISABLED;
434 p->state = state;
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);
442 if (p) {
443 spin_lock_bh(&br->lock);
444 state = netif_running(br->dev) ?
445 BR_STATE_FORWARDING : BR_STATE_DISABLED;
446 p->state = state;
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);
454 kfree_rcu(mrp, rcu);
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;
466 struct br_mrp *mrp;
467 int err;
469 /* If the ring exists, it is not possible to create another one with the
470 * same ring_id
472 mrp = br_mrp_find_id(br, instance->ring_id);
473 if (mrp)
474 return -EINVAL;
476 if (!br_mrp_get_port(br, instance->p_ifindex) ||
477 !br_mrp_get_port(br, instance->s_ifindex))
478 return -EINVAL;
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))
483 return -EINVAL;
485 mrp = kzalloc(sizeof(*mrp), GFP_KERNEL);
486 if (!mrp)
487 return -ENOMEM;
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);
514 if (err)
515 goto delete_mrp;
517 return 0;
519 delete_mrp:
520 br_mrp_del_impl(br, mrp);
522 return err;
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 */
533 if (!mrp)
534 return;
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);
546 if (!mrp)
547 return -EINVAL;
549 br_mrp_del_impl(br, mrp);
551 return 0;
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)
560 if (!p || !(p->flags & BR_MRP_AWARE))
561 return -EINVAL;
563 spin_lock_bh(&p->br->lock);
565 if (state == BR_MRP_PORT_STATE_FORWARDING)
566 p->state = BR_STATE_FORWARDING;
567 else
568 p->state = BR_STATE_BLOCKING;
570 spin_unlock_bh(&p->br->lock);
572 br_mrp_port_switchdev_set_state(p, state);
574 return 0;
577 /* Set port role, port role can be primary or secondary
578 * note: already called with rtnl_lock
580 int br_mrp_set_port_role(struct net_bridge_port *p,
581 enum br_mrp_port_role_type role)
583 struct br_mrp *mrp;
585 if (!p || !(p->flags & BR_MRP_AWARE))
586 return -EINVAL;
588 mrp = br_mrp_find_port(p->br, p);
590 if (!mrp)
591 return -EINVAL;
593 switch (role) {
594 case BR_MRP_PORT_ROLE_PRIMARY:
595 rcu_assign_pointer(mrp->p_port, p);
596 break;
597 case BR_MRP_PORT_ROLE_SECONDARY:
598 rcu_assign_pointer(mrp->s_port, p);
599 break;
600 default:
601 return -EINVAL;
604 br_mrp_port_switchdev_set_role(p, role);
606 return 0;
609 /* Set ring state, ring state can be only Open or Closed
610 * note: already called with rtnl_lock
612 int br_mrp_set_ring_state(struct net_bridge *br,
613 struct br_mrp_ring_state *state)
615 struct br_mrp *mrp = br_mrp_find_id(br, state->ring_id);
617 if (!mrp)
618 return -EINVAL;
620 if (mrp->ring_state == BR_MRP_RING_STATE_CLOSED &&
621 state->ring_state != BR_MRP_RING_STATE_CLOSED)
622 mrp->ring_transitions++;
624 mrp->ring_state = state->ring_state;
626 br_mrp_switchdev_set_ring_state(br, mrp, state->ring_state);
628 return 0;
631 /* Set ring role, ring role can be only MRM(Media Redundancy Manager) or
632 * MRC(Media Redundancy Client).
633 * note: already called with rtnl_lock
635 int br_mrp_set_ring_role(struct net_bridge *br,
636 struct br_mrp_ring_role *role)
638 struct br_mrp *mrp = br_mrp_find_id(br, role->ring_id);
639 int err;
641 if (!mrp)
642 return -EINVAL;
644 mrp->ring_role = role->ring_role;
646 /* If there is an error just bailed out */
647 err = br_mrp_switchdev_set_ring_role(br, mrp, role->ring_role);
648 if (err && err != -EOPNOTSUPP)
649 return err;
651 /* Now detect if the HW actually applied the role or not. If the HW
652 * applied the role it means that the SW will not to do those operations
653 * anymore. For example if the role ir MRM then the HW will notify the
654 * SW when ring is open, but if the is not pushed to the HW the SW will
655 * need to detect when the ring is open
657 mrp->ring_role_offloaded = err == -EOPNOTSUPP ? 0 : 1;
659 return 0;
662 /* Start to generate or monitor MRP test frames, the frames are generated by
663 * HW and if it fails, they are generated by the SW.
664 * note: already called with rtnl_lock
666 int br_mrp_start_test(struct net_bridge *br,
667 struct br_mrp_start_test *test)
669 struct br_mrp *mrp = br_mrp_find_id(br, test->ring_id);
671 if (!mrp)
672 return -EINVAL;
674 /* Try to push it to the HW and if it fails then continue with SW
675 * implementation and if that also fails then return error.
677 if (!br_mrp_switchdev_send_ring_test(br, mrp, test->interval,
678 test->max_miss, test->period,
679 test->monitor))
680 return 0;
682 mrp->test_interval = test->interval;
683 mrp->test_end = jiffies + usecs_to_jiffies(test->period);
684 mrp->test_max_miss = test->max_miss;
685 mrp->test_monitor = test->monitor;
686 mrp->test_count_miss = 0;
687 queue_delayed_work(system_wq, &mrp->test_work,
688 usecs_to_jiffies(test->interval));
690 return 0;
693 /* Set in state, int state can be only Open or Closed
694 * note: already called with rtnl_lock
696 int br_mrp_set_in_state(struct net_bridge *br, struct br_mrp_in_state *state)
698 struct br_mrp *mrp = br_mrp_find_in_id(br, state->in_id);
700 if (!mrp)
701 return -EINVAL;
703 if (mrp->in_state == BR_MRP_IN_STATE_CLOSED &&
704 state->in_state != BR_MRP_IN_STATE_CLOSED)
705 mrp->in_transitions++;
707 mrp->in_state = state->in_state;
709 br_mrp_switchdev_set_in_state(br, mrp, state->in_state);
711 return 0;
714 /* Set in role, in role can be only MIM(Media Interconnection Manager) or
715 * MIC(Media Interconnection Client).
716 * note: already called with rtnl_lock
718 int br_mrp_set_in_role(struct net_bridge *br, struct br_mrp_in_role *role)
720 struct br_mrp *mrp = br_mrp_find_id(br, role->ring_id);
721 struct net_bridge_port *p;
722 int err;
724 if (!mrp)
725 return -EINVAL;
727 if (!br_mrp_get_port(br, role->i_ifindex))
728 return -EINVAL;
730 if (role->in_role == BR_MRP_IN_ROLE_DISABLED) {
731 u8 state;
733 /* It is not allowed to disable a port that doesn't exist */
734 p = rtnl_dereference(mrp->i_port);
735 if (!p)
736 return -EINVAL;
738 /* Stop the generating MRP_InTest frames */
739 cancel_delayed_work_sync(&mrp->in_test_work);
740 br_mrp_switchdev_send_in_test(br, mrp, 0, 0, 0);
742 /* Remove the port */
743 spin_lock_bh(&br->lock);
744 state = netif_running(br->dev) ?
745 BR_STATE_FORWARDING : BR_STATE_DISABLED;
746 p->state = state;
747 p->flags &= ~BR_MRP_AWARE;
748 spin_unlock_bh(&br->lock);
749 br_mrp_port_switchdev_set_state(p, state);
750 rcu_assign_pointer(mrp->i_port, NULL);
752 mrp->in_role = role->in_role;
753 mrp->in_id = 0;
755 return 0;
758 /* It is not possible to have the same port part of multiple rings */
759 if (!br_mrp_unique_ifindex(br, role->i_ifindex))
760 return -EINVAL;
762 /* It is not allowed to set a different interconnect port if the mrp
763 * instance has already one. First it needs to be disabled and after
764 * that set the new port
766 if (rcu_access_pointer(mrp->i_port))
767 return -EINVAL;
769 p = br_mrp_get_port(br, role->i_ifindex);
770 spin_lock_bh(&br->lock);
771 p->state = BR_STATE_FORWARDING;
772 p->flags |= BR_MRP_AWARE;
773 spin_unlock_bh(&br->lock);
774 rcu_assign_pointer(mrp->i_port, p);
776 mrp->in_role = role->in_role;
777 mrp->in_id = role->in_id;
779 /* If there is an error just bailed out */
780 err = br_mrp_switchdev_set_in_role(br, mrp, role->in_id,
781 role->ring_id, role->in_role);
782 if (err && err != -EOPNOTSUPP)
783 return err;
785 /* Now detect if the HW actually applied the role or not. If the HW
786 * applied the role it means that the SW will not to do those operations
787 * anymore. For example if the role is MIM then the HW will notify the
788 * SW when interconnect ring is open, but if the is not pushed to the HW
789 * the SW will need to detect when the interconnect ring is open.
791 mrp->in_role_offloaded = err == -EOPNOTSUPP ? 0 : 1;
793 return 0;
796 /* Start to generate MRP_InTest frames, the frames are generated by
797 * HW and if it fails, they are generated by the SW.
798 * note: already called with rtnl_lock
800 int br_mrp_start_in_test(struct net_bridge *br,
801 struct br_mrp_start_in_test *in_test)
803 struct br_mrp *mrp = br_mrp_find_in_id(br, in_test->in_id);
805 if (!mrp)
806 return -EINVAL;
808 if (mrp->in_role != BR_MRP_IN_ROLE_MIM)
809 return -EINVAL;
811 /* Try to push it to the HW and if it fails then continue with SW
812 * implementation and if that also fails then return error.
814 if (!br_mrp_switchdev_send_in_test(br, mrp, in_test->interval,
815 in_test->max_miss, in_test->period))
816 return 0;
818 mrp->in_test_interval = in_test->interval;
819 mrp->in_test_end = jiffies + usecs_to_jiffies(in_test->period);
820 mrp->in_test_max_miss = in_test->max_miss;
821 mrp->in_test_count_miss = 0;
822 queue_delayed_work(system_wq, &mrp->in_test_work,
823 usecs_to_jiffies(in_test->interval));
825 return 0;
828 /* Determin if the frame type is a ring frame */
829 static bool br_mrp_ring_frame(struct sk_buff *skb)
831 const struct br_mrp_tlv_hdr *hdr;
832 struct br_mrp_tlv_hdr _hdr;
834 hdr = skb_header_pointer(skb, sizeof(uint16_t), sizeof(_hdr), &_hdr);
835 if (!hdr)
836 return false;
838 if (hdr->type == BR_MRP_TLV_HEADER_RING_TEST ||
839 hdr->type == BR_MRP_TLV_HEADER_RING_TOPO ||
840 hdr->type == BR_MRP_TLV_HEADER_RING_LINK_DOWN ||
841 hdr->type == BR_MRP_TLV_HEADER_RING_LINK_UP ||
842 hdr->type == BR_MRP_TLV_HEADER_OPTION)
843 return true;
845 return false;
848 /* Determin if the frame type is an interconnect frame */
849 static bool br_mrp_in_frame(struct sk_buff *skb)
851 const struct br_mrp_tlv_hdr *hdr;
852 struct br_mrp_tlv_hdr _hdr;
854 hdr = skb_header_pointer(skb, sizeof(uint16_t), sizeof(_hdr), &_hdr);
855 if (!hdr)
856 return false;
858 if (hdr->type == BR_MRP_TLV_HEADER_IN_TEST ||
859 hdr->type == BR_MRP_TLV_HEADER_IN_TOPO ||
860 hdr->type == BR_MRP_TLV_HEADER_IN_LINK_DOWN ||
861 hdr->type == BR_MRP_TLV_HEADER_IN_LINK_UP ||
862 hdr->type == BR_MRP_TLV_HEADER_IN_LINK_STATUS)
863 return true;
865 return false;
868 /* Process only MRP Test frame. All the other MRP frames are processed by
869 * userspace application
870 * note: already called with rcu_read_lock
872 static void br_mrp_mrm_process(struct br_mrp *mrp, struct net_bridge_port *port,
873 struct sk_buff *skb)
875 const struct br_mrp_tlv_hdr *hdr;
876 struct br_mrp_tlv_hdr _hdr;
878 /* Each MRP header starts with a version field which is 16 bits.
879 * Therefore skip the version and get directly the TLV header.
881 hdr = skb_header_pointer(skb, sizeof(uint16_t), sizeof(_hdr), &_hdr);
882 if (!hdr)
883 return;
885 if (hdr->type != BR_MRP_TLV_HEADER_RING_TEST)
886 return;
888 mrp->test_count_miss = 0;
890 /* Notify the userspace that the ring is closed only when the ring is
891 * not closed
893 if (mrp->ring_state != BR_MRP_RING_STATE_CLOSED)
894 br_mrp_ring_port_open(port->dev, false);
897 /* Determin if the test hdr has a better priority than the node */
898 static bool br_mrp_test_better_than_own(struct br_mrp *mrp,
899 struct net_bridge *br,
900 const struct br_mrp_ring_test_hdr *hdr)
902 u16 prio = be16_to_cpu(hdr->prio);
904 if (prio < mrp->prio ||
905 (prio == mrp->prio &&
906 ether_addr_to_u64(hdr->sa) < ether_addr_to_u64(br->dev->dev_addr)))
907 return true;
909 return false;
912 /* Process only MRP Test frame. All the other MRP frames are processed by
913 * userspace application
914 * note: already called with rcu_read_lock
916 static void br_mrp_mra_process(struct br_mrp *mrp, struct net_bridge *br,
917 struct net_bridge_port *port,
918 struct sk_buff *skb)
920 const struct br_mrp_ring_test_hdr *test_hdr;
921 struct br_mrp_ring_test_hdr _test_hdr;
922 const struct br_mrp_tlv_hdr *hdr;
923 struct br_mrp_tlv_hdr _hdr;
925 /* Each MRP header starts with a version field which is 16 bits.
926 * Therefore skip the version and get directly the TLV header.
928 hdr = skb_header_pointer(skb, sizeof(uint16_t), sizeof(_hdr), &_hdr);
929 if (!hdr)
930 return;
932 if (hdr->type != BR_MRP_TLV_HEADER_RING_TEST)
933 return;
935 test_hdr = skb_header_pointer(skb, sizeof(uint16_t) + sizeof(_hdr),
936 sizeof(_test_hdr), &_test_hdr);
937 if (!test_hdr)
938 return;
940 /* Only frames that have a better priority than the node will
941 * clear the miss counter because otherwise the node will need to behave
942 * as MRM.
944 if (br_mrp_test_better_than_own(mrp, br, test_hdr))
945 mrp->test_count_miss = 0;
948 /* Process only MRP InTest frame. All the other MRP frames are processed by
949 * userspace application
950 * note: already called with rcu_read_lock
952 static bool br_mrp_mim_process(struct br_mrp *mrp, struct net_bridge_port *port,
953 struct sk_buff *skb)
955 const struct br_mrp_in_test_hdr *in_hdr;
956 struct br_mrp_in_test_hdr _in_hdr;
957 const struct br_mrp_tlv_hdr *hdr;
958 struct br_mrp_tlv_hdr _hdr;
960 /* Each MRP header starts with a version field which is 16 bits.
961 * Therefore skip the version and get directly the TLV header.
963 hdr = skb_header_pointer(skb, sizeof(uint16_t), sizeof(_hdr), &_hdr);
964 if (!hdr)
965 return false;
967 /* The check for InTest frame type was already done */
968 in_hdr = skb_header_pointer(skb, sizeof(uint16_t) + sizeof(_hdr),
969 sizeof(_in_hdr), &_in_hdr);
970 if (!in_hdr)
971 return false;
973 /* It needs to process only it's own InTest frames. */
974 if (mrp->in_id != ntohs(in_hdr->id))
975 return false;
977 mrp->in_test_count_miss = 0;
979 /* Notify the userspace that the ring is closed only when the ring is
980 * not closed
982 if (mrp->in_state != BR_MRP_IN_STATE_CLOSED)
983 br_mrp_in_port_open(port->dev, false);
985 return true;
988 /* Get the MRP frame type
989 * note: already called with rcu_read_lock
991 static u8 br_mrp_get_frame_type(struct sk_buff *skb)
993 const struct br_mrp_tlv_hdr *hdr;
994 struct br_mrp_tlv_hdr _hdr;
996 /* Each MRP header starts with a version field which is 16 bits.
997 * Therefore skip the version and get directly the TLV header.
999 hdr = skb_header_pointer(skb, sizeof(uint16_t), sizeof(_hdr), &_hdr);
1000 if (!hdr)
1001 return 0xff;
1003 return hdr->type;
1006 static bool br_mrp_mrm_behaviour(struct br_mrp *mrp)
1008 if (mrp->ring_role == BR_MRP_RING_ROLE_MRM ||
1009 (mrp->ring_role == BR_MRP_RING_ROLE_MRA && !mrp->test_monitor))
1010 return true;
1012 return false;
1015 static bool br_mrp_mrc_behaviour(struct br_mrp *mrp)
1017 if (mrp->ring_role == BR_MRP_RING_ROLE_MRC ||
1018 (mrp->ring_role == BR_MRP_RING_ROLE_MRA && mrp->test_monitor))
1019 return true;
1021 return false;
1024 /* This will just forward the frame to the other mrp ring ports, depending on
1025 * the frame type, ring role and interconnect role
1026 * note: already called with rcu_read_lock
1028 static int br_mrp_rcv(struct net_bridge_port *p,
1029 struct sk_buff *skb, struct net_device *dev)
1031 struct net_bridge_port *p_port, *s_port, *i_port = NULL;
1032 struct net_bridge_port *p_dst, *s_dst, *i_dst = NULL;
1033 struct net_bridge *br;
1034 struct br_mrp *mrp;
1036 /* If port is disabled don't accept any frames */
1037 if (p->state == BR_STATE_DISABLED)
1038 return 0;
1040 br = p->br;
1041 mrp = br_mrp_find_port(br, p);
1042 if (unlikely(!mrp))
1043 return 0;
1045 p_port = rcu_dereference(mrp->p_port);
1046 if (!p_port)
1047 return 0;
1048 p_dst = p_port;
1050 s_port = rcu_dereference(mrp->s_port);
1051 if (!s_port)
1052 return 0;
1053 s_dst = s_port;
1055 /* If the frame is a ring frame then it is not required to check the
1056 * interconnect role and ports to process or forward the frame
1058 if (br_mrp_ring_frame(skb)) {
1059 /* If the role is MRM then don't forward the frames */
1060 if (mrp->ring_role == BR_MRP_RING_ROLE_MRM) {
1061 br_mrp_mrm_process(mrp, p, skb);
1062 goto no_forward;
1065 /* If the role is MRA then don't forward the frames if it
1066 * behaves as MRM node
1068 if (mrp->ring_role == BR_MRP_RING_ROLE_MRA) {
1069 if (!mrp->test_monitor) {
1070 br_mrp_mrm_process(mrp, p, skb);
1071 goto no_forward;
1074 br_mrp_mra_process(mrp, br, p, skb);
1077 goto forward;
1080 if (br_mrp_in_frame(skb)) {
1081 u8 in_type = br_mrp_get_frame_type(skb);
1083 i_port = rcu_dereference(mrp->i_port);
1084 i_dst = i_port;
1086 /* If the ring port is in block state it should not forward
1087 * In_Test frames
1089 if (br_mrp_is_ring_port(p_port, s_port, p) &&
1090 p->state == BR_STATE_BLOCKING &&
1091 in_type == BR_MRP_TLV_HEADER_IN_TEST)
1092 goto no_forward;
1094 /* Nodes that behaves as MRM needs to stop forwarding the
1095 * frames in case the ring is closed, otherwise will be a loop.
1096 * In this case the frame is no forward between the ring ports.
1098 if (br_mrp_mrm_behaviour(mrp) &&
1099 br_mrp_is_ring_port(p_port, s_port, p) &&
1100 (s_port->state != BR_STATE_FORWARDING ||
1101 p_port->state != BR_STATE_FORWARDING)) {
1102 p_dst = NULL;
1103 s_dst = NULL;
1106 /* A node that behaves as MRC and doesn't have a interconnect
1107 * role then it should forward all frames between the ring ports
1108 * because it doesn't have an interconnect port
1110 if (br_mrp_mrc_behaviour(mrp) &&
1111 mrp->in_role == BR_MRP_IN_ROLE_DISABLED)
1112 goto forward;
1114 if (mrp->in_role == BR_MRP_IN_ROLE_MIM) {
1115 if (in_type == BR_MRP_TLV_HEADER_IN_TEST) {
1116 /* MIM should not forward it's own InTest
1117 * frames
1119 if (br_mrp_mim_process(mrp, p, skb)) {
1120 goto no_forward;
1121 } else {
1122 if (br_mrp_is_ring_port(p_port, s_port,
1124 i_dst = NULL;
1126 if (br_mrp_is_in_port(i_port, p))
1127 goto no_forward;
1129 } else {
1130 /* MIM should forward IntLinkChange/Status and
1131 * IntTopoChange between ring ports but MIM
1132 * should not forward IntLinkChange/Status and
1133 * IntTopoChange if the frame was received at
1134 * the interconnect port
1136 if (br_mrp_is_ring_port(p_port, s_port, p))
1137 i_dst = NULL;
1139 if (br_mrp_is_in_port(i_port, p))
1140 goto no_forward;
1144 if (mrp->in_role == BR_MRP_IN_ROLE_MIC) {
1145 /* MIC should forward InTest frames on all ports
1146 * regardless of the received port
1148 if (in_type == BR_MRP_TLV_HEADER_IN_TEST)
1149 goto forward;
1151 /* MIC should forward IntLinkChange frames only if they
1152 * are received on ring ports to all the ports
1154 if (br_mrp_is_ring_port(p_port, s_port, p) &&
1155 (in_type == BR_MRP_TLV_HEADER_IN_LINK_UP ||
1156 in_type == BR_MRP_TLV_HEADER_IN_LINK_DOWN))
1157 goto forward;
1159 /* MIC should forward IntLinkStatus frames only to
1160 * interconnect port if it was received on a ring port.
1161 * If it is received on interconnect port then, it
1162 * should be forward on both ring ports
1164 if (br_mrp_is_ring_port(p_port, s_port, p) &&
1165 in_type == BR_MRP_TLV_HEADER_IN_LINK_STATUS) {
1166 p_dst = NULL;
1167 s_dst = NULL;
1170 /* Should forward the InTopo frames only between the
1171 * ring ports
1173 if (in_type == BR_MRP_TLV_HEADER_IN_TOPO) {
1174 i_dst = NULL;
1175 goto forward;
1178 /* In all the other cases don't forward the frames */
1179 goto no_forward;
1183 forward:
1184 if (p_dst)
1185 br_forward(p_dst, skb, true, false);
1186 if (s_dst)
1187 br_forward(s_dst, skb, true, false);
1188 if (i_dst)
1189 br_forward(i_dst, skb, true, false);
1191 no_forward:
1192 return 1;
1195 /* Check if the frame was received on a port that is part of MRP ring
1196 * and if the frame has MRP eth. In that case process the frame otherwise do
1197 * normal forwarding.
1198 * note: already called with rcu_read_lock
1200 static int br_mrp_process(struct net_bridge_port *p, struct sk_buff *skb)
1202 /* If there is no MRP instance do normal forwarding */
1203 if (likely(!(p->flags & BR_MRP_AWARE)))
1204 goto out;
1206 return br_mrp_rcv(p, skb, p->dev);
1207 out:
1208 return 0;
1211 bool br_mrp_enabled(struct net_bridge *br)
1213 return !hlist_empty(&br->mrp_list);