br_mrp.c (4b3a61b030d1131dcf3633a276158a3d0a435a47) br_mrp.c (c6676e7d62cfb5cb7c1c5320a26f3634a11afdb0)
1// SPDX-License-Identifier: GPL-2.0-or-later
2
3#include <linux/mrp_bridge.h>
4#include "br_private_mrp.h"
5
6static const u8 mrp_test_dmac[ETH_ALEN] = { 0x1, 0x15, 0x4e, 0x0, 0x0, 0x1 };
7
8static struct net_bridge_port *br_mrp_get_port(struct net_bridge *br,

--- 146 unchanged lines hidden (view full) ---

155 hdr->timestamp = cpu_to_be32(jiffies_to_msecs(jiffies));
156
157 br_mrp_skb_common(skb, mrp);
158 br_mrp_skb_tlv(skb, BR_MRP_TLV_HEADER_END, 0x0);
159
160 return skb;
161}
162
1// SPDX-License-Identifier: GPL-2.0-or-later
2
3#include <linux/mrp_bridge.h>
4#include "br_private_mrp.h"
5
6static const u8 mrp_test_dmac[ETH_ALEN] = { 0x1, 0x15, 0x4e, 0x0, 0x0, 0x1 };
7
8static struct net_bridge_port *br_mrp_get_port(struct net_bridge *br,

--- 146 unchanged lines hidden (view full) ---

155 hdr->timestamp = cpu_to_be32(jiffies_to_msecs(jiffies));
156
157 br_mrp_skb_common(skb, mrp);
158 br_mrp_skb_tlv(skb, BR_MRP_TLV_HEADER_END, 0x0);
159
160 return skb;
161}
162
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.
172 */
163static void br_mrp_test_work_expired(struct work_struct *work)
164{
165 struct delayed_work *del_work = to_delayed_work(work);
166 struct br_mrp *mrp = container_of(del_work, struct br_mrp, test_work);
167 struct net_bridge_port *p;
168 bool notify_open = false;
169 struct sk_buff *skb;
170
171 if (time_before_eq(mrp->test_end, jiffies))
172 return;
173
174 if (mrp->test_count_miss < mrp->test_max_miss) {
175 mrp->test_count_miss++;
176 } else {
177 /* Notify that the ring is open only if the ring state is
178 * closed, otherwise it would continue to notify at every
179 * interval.
173static void br_mrp_test_work_expired(struct work_struct *work)
174{
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;
179 struct sk_buff *skb;
180
181 if (time_before_eq(mrp->test_end, jiffies))
182 return;
183
184 if (mrp->test_count_miss < mrp->test_max_miss) {
185 mrp->test_count_miss++;
186 } else {
187 /* Notify that the ring is open only if the ring state is
188 * closed, otherwise it would continue to notify at every
189 * interval.
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
194 * the role of a MRM.
180 */
195 */
181 if (mrp->ring_state == BR_MRP_RING_STATE_CLOSED)
196 if (mrp->ring_state == BR_MRP_RING_STATE_CLOSED ||
197 mrp->test_monitor)
182 notify_open = true;
183 }
184
185 rcu_read_lock();
186
187 p = rcu_dereference(mrp->p_port);
188 if (p) {
198 notify_open = true;
199 }
200
201 rcu_read_lock();
202
203 p = rcu_dereference(mrp->p_port);
204 if (p) {
189 skb = br_mrp_alloc_test_skb(mrp, p, BR_MRP_PORT_ROLE_PRIMARY);
190 if (!skb)
191 goto out;
205 if (!mrp->test_monitor) {
206 skb = br_mrp_alloc_test_skb(mrp, p,
207 BR_MRP_PORT_ROLE_PRIMARY);
208 if (!skb)
209 goto out;
192
210
193 skb_reset_network_header(skb);
194 dev_queue_xmit(skb);
211 skb_reset_network_header(skb);
212 dev_queue_xmit(skb);
213 }
195
196 if (notify_open && !mrp->ring_role_offloaded)
197 br_mrp_port_open(p->dev, true);
198 }
199
200 p = rcu_dereference(mrp->s_port);
201 if (p) {
214
215 if (notify_open && !mrp->ring_role_offloaded)
216 br_mrp_port_open(p->dev, true);
217 }
218
219 p = rcu_dereference(mrp->s_port);
220 if (p) {
202 skb = br_mrp_alloc_test_skb(mrp, p, BR_MRP_PORT_ROLE_SECONDARY);
203 if (!skb)
204 goto out;
221 if (!mrp->test_monitor) {
222 skb = br_mrp_alloc_test_skb(mrp, p,
223 BR_MRP_PORT_ROLE_SECONDARY);
224 if (!skb)
225 goto out;
205
226
206 skb_reset_network_header(skb);
207 dev_queue_xmit(skb);
227 skb_reset_network_header(skb);
228 dev_queue_xmit(skb);
229 }
208
209 if (notify_open && !mrp->ring_role_offloaded)
210 br_mrp_port_open(p->dev, true);
211 }
212
213out:
214 rcu_read_unlock();
215

--- 6 unchanged lines hidden (view full) ---

222 */
223static void br_mrp_del_impl(struct net_bridge *br, struct br_mrp *mrp)
224{
225 struct net_bridge_port *p;
226 u8 state;
227
228 /* Stop sending MRP_Test frames */
229 cancel_delayed_work_sync(&mrp->test_work);
230
231 if (notify_open && !mrp->ring_role_offloaded)
232 br_mrp_port_open(p->dev, true);
233 }
234
235out:
236 rcu_read_unlock();
237

--- 6 unchanged lines hidden (view full) ---

244 */
245static void br_mrp_del_impl(struct net_bridge *br, struct br_mrp *mrp)
246{
247 struct net_bridge_port *p;
248 u8 state;
249
250 /* Stop sending MRP_Test frames */
251 cancel_delayed_work_sync(&mrp->test_work);
230 br_mrp_switchdev_send_ring_test(br, mrp, 0, 0, 0);
252 br_mrp_switchdev_send_ring_test(br, mrp, 0, 0, 0, 0);
231
232 br_mrp_switchdev_del(br, mrp);
233
234 /* Reset the ports */
235 p = rtnl_dereference(mrp->p_port);
236 if (p) {
237 spin_lock_bh(&br->lock);
238 state = netif_running(br->dev) ?

--- 208 unchanged lines hidden (view full) ---

447 * SW when ring is open, but if the is not pushed to the HW the SW will
448 * need to detect when the ring is open
449 */
450 mrp->ring_role_offloaded = err == -EOPNOTSUPP ? 0 : 1;
451
452 return 0;
453}
454
253
254 br_mrp_switchdev_del(br, mrp);
255
256 /* Reset the ports */
257 p = rtnl_dereference(mrp->p_port);
258 if (p) {
259 spin_lock_bh(&br->lock);
260 state = netif_running(br->dev) ?

--- 208 unchanged lines hidden (view full) ---

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
471 */
472 mrp->ring_role_offloaded = err == -EOPNOTSUPP ? 0 : 1;
473
474 return 0;
475}
476
455/* Start to generate MRP test frames, the frames are generated by HW and if it
456 * fails, they are generated by the SW.
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.
457 * note: already called with rtnl_lock
458 */
459int br_mrp_start_test(struct net_bridge *br,
460 struct br_mrp_start_test *test)
461{
462 struct br_mrp *mrp = br_mrp_find_id(br, test->ring_id);
463
464 if (!mrp)
465 return -EINVAL;
466
479 * note: already called with rtnl_lock
480 */
481int br_mrp_start_test(struct net_bridge *br,
482 struct br_mrp_start_test *test)
483{
484 struct br_mrp *mrp = br_mrp_find_id(br, test->ring_id);
485
486 if (!mrp)
487 return -EINVAL;
488
467 /* Try to push it to the HW and if it fails then continue to generate in
468 * SW and if that also fails then return error
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.
469 */
470 if (!br_mrp_switchdev_send_ring_test(br, mrp, test->interval,
491 */
492 if (!br_mrp_switchdev_send_ring_test(br, mrp, test->interval,
471 test->max_miss, test->period))
493 test->max_miss, test->period,
494 test->monitor))
472 return 0;
473
474 mrp->test_interval = test->interval;
475 mrp->test_end = jiffies + usecs_to_jiffies(test->period);
476 mrp->test_max_miss = test->max_miss;
495 return 0;
496
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;
477 mrp->test_count_miss = 0;
478 queue_delayed_work(system_wq, &mrp->test_work,
479 usecs_to_jiffies(test->interval));
480
481 return 0;
482}
483
484/* Process only MRP Test frame. All the other MRP frames are processed by

--- 20 unchanged lines hidden (view full) ---

505
506 /* Notify the userspace that the ring is closed only when the ring is
507 * not closed
508 */
509 if (mrp->ring_state != BR_MRP_RING_STATE_CLOSED)
510 br_mrp_port_open(port->dev, false);
511}
512
501 mrp->test_count_miss = 0;
502 queue_delayed_work(system_wq, &mrp->test_work,
503 usecs_to_jiffies(test->interval));
504
505 return 0;
506}
507
508/* Process only MRP Test frame. All the other MRP frames are processed by

--- 20 unchanged lines hidden (view full) ---

529
530 /* Notify the userspace that the ring is closed only when the ring is
531 * not closed
532 */
533 if (mrp->ring_state != BR_MRP_RING_STATE_CLOSED)
534 br_mrp_port_open(port->dev, false);
535}
536
537/* Determin if the test hdr has a better priority than the node */
538static 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)
541{
542 u16 prio = be16_to_cpu(hdr->prio);
543
544 if (prio < mrp->prio ||
545 (prio == mrp->prio &&
546 ether_addr_to_u64(hdr->sa) < ether_addr_to_u64(br->dev->dev_addr)))
547 return true;
548
549 return false;
550}
551
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
555 */
556static void br_mrp_mra_process(struct br_mrp *mrp, struct net_bridge *br,
557 struct net_bridge_port *port,
558 struct sk_buff *skb)
559{
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;
564
565 /* Each MRP header starts with a version field which is 16 bits.
566 * Therefore skip the version and get directly the TLV header.
567 */
568 hdr = skb_header_pointer(skb, sizeof(uint16_t), sizeof(_hdr), &_hdr);
569 if (!hdr)
570 return;
571
572 if (hdr->type != BR_MRP_TLV_HEADER_RING_TEST)
573 return;
574
575 test_hdr = skb_header_pointer(skb, sizeof(uint16_t) + sizeof(_hdr),
576 sizeof(_test_hdr), &_test_hdr);
577 if (!test_hdr)
578 return;
579
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
582 * as MRM.
583 */
584 if (br_mrp_test_better_than_own(mrp, br, test_hdr))
585 mrp->test_count_miss = 0;
586}
587
513/* This will just forward the frame to the other mrp ring port(MRC role) or will
514 * not do anything.
515 * note: already called with rcu_read_lock
516 */
517static int br_mrp_rcv(struct net_bridge_port *p,
518 struct sk_buff *skb, struct net_device *dev)
519{
520 struct net_device *s_dev, *p_dev, *d_dev;

--- 20 unchanged lines hidden (view full) ---

541 return 0;
542
543 /* If the role is MRM then don't forward the frames */
544 if (mrp->ring_role == BR_MRP_RING_ROLE_MRM) {
545 br_mrp_mrm_process(mrp, p, skb);
546 return 1;
547 }
548
588/* This will just forward the frame to the other mrp ring port(MRC role) or will
589 * not do anything.
590 * note: already called with rcu_read_lock
591 */
592static int br_mrp_rcv(struct net_bridge_port *p,
593 struct sk_buff *skb, struct net_device *dev)
594{
595 struct net_device *s_dev, *p_dev, *d_dev;

--- 20 unchanged lines hidden (view full) ---

616 return 0;
617
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);
621 return 1;
622 }
623
624 /* If the role is MRA then don't forward the frames if it behaves as
625 * MRM node
626 */
627 if (mrp->ring_role == BR_MRP_RING_ROLE_MRA) {
628 if (!mrp->test_monitor) {
629 br_mrp_mrm_process(mrp, p, skb);
630 return 1;
631 }
632
633 br_mrp_mra_process(mrp, br, p, skb);
634 }
635
549 /* Clone the frame and forward it on the other MRP port */
550 nskb = skb_clone(skb, GFP_ATOMIC);
551 if (!nskb)
552 return 0;
553
554 p_dev = p_port->dev;
555 s_dev = s_port->dev;
556

--- 34 unchanged lines hidden ---
636 /* Clone the frame and forward it on the other MRP port */
637 nskb = skb_clone(skb, GFP_ATOMIC);
638 if (!nskb)
639 return 0;
640
641 p_dev = p_port->dev;
642 s_dev = s_port->dev;
643

--- 34 unchanged lines hidden ---