]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blob - net/bridge/br_mrp.c
Merge tag 'nfs-for-5.8-1' of git://git.linux-nfs.org/projects/anna/linux-nfs
[mirror_ubuntu-jammy-kernel.git] / net / bridge / br_mrp.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2
3 #include <linux/mrp_bridge.h>
4 #include "br_private_mrp.h"
5
6 static const u8 mrp_test_dmac[ETH_ALEN] = { 0x1, 0x15, 0x4e, 0x0, 0x0, 0x1 };
7
8 static struct net_bridge_port *br_mrp_get_port(struct net_bridge *br,
9 u32 ifindex)
10 {
11 struct net_bridge_port *res = NULL;
12 struct net_bridge_port *port;
13
14 list_for_each_entry(port, &br->port_list, list) {
15 if (port->dev->ifindex == ifindex) {
16 res = port;
17 break;
18 }
19 }
20
21 return res;
22 }
23
24 static struct br_mrp *br_mrp_find_id(struct net_bridge *br, u32 ring_id)
25 {
26 struct br_mrp *res = NULL;
27 struct br_mrp *mrp;
28
29 list_for_each_entry_rcu(mrp, &br->mrp_list, list,
30 lockdep_rtnl_is_held()) {
31 if (mrp->ring_id == ring_id) {
32 res = mrp;
33 break;
34 }
35 }
36
37 return res;
38 }
39
40 static bool br_mrp_unique_ifindex(struct net_bridge *br, u32 ifindex)
41 {
42 struct br_mrp *mrp;
43
44 list_for_each_entry_rcu(mrp, &br->mrp_list, list,
45 lockdep_rtnl_is_held()) {
46 struct net_bridge_port *p;
47
48 p = rtnl_dereference(mrp->p_port);
49 if (p && p->dev->ifindex == ifindex)
50 return false;
51
52 p = rtnl_dereference(mrp->s_port);
53 if (p && p->dev->ifindex == ifindex)
54 return false;
55 }
56
57 return true;
58 }
59
60 static struct br_mrp *br_mrp_find_port(struct net_bridge *br,
61 struct net_bridge_port *p)
62 {
63 struct br_mrp *res = NULL;
64 struct br_mrp *mrp;
65
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) {
70 res = mrp;
71 break;
72 }
73 }
74
75 return res;
76 }
77
78 static int br_mrp_next_seq(struct br_mrp *mrp)
79 {
80 mrp->seq_id++;
81 return mrp->seq_id;
82 }
83
84 static struct sk_buff *br_mrp_skb_alloc(struct net_bridge_port *p,
85 const u8 *src, const u8 *dst)
86 {
87 struct ethhdr *eth_hdr;
88 struct sk_buff *skb;
89 u16 *version;
90
91 skb = dev_alloc_skb(MRP_MAX_FRAME_LENGTH);
92 if (!skb)
93 return NULL;
94
95 skb->dev = p->dev;
96 skb->protocol = htons(ETH_P_MRP);
97 skb->priority = MRP_FRAME_PRIO;
98 skb_reserve(skb, sizeof(*eth_hdr));
99
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);
104
105 version = skb_put(skb, sizeof(*version));
106 *version = cpu_to_be16(MRP_VERSION);
107
108 return skb;
109 }
110
111 static void br_mrp_skb_tlv(struct sk_buff *skb,
112 enum br_mrp_tlv_header_type type,
113 u8 length)
114 {
115 struct br_mrp_tlv_hdr *hdr;
116
117 hdr = skb_put(skb, sizeof(*hdr));
118 hdr->type = type;
119 hdr->length = length;
120 }
121
122 static void br_mrp_skb_common(struct sk_buff *skb, struct br_mrp *mrp)
123 {
124 struct br_mrp_common_hdr *hdr;
125
126 br_mrp_skb_tlv(skb, BR_MRP_TLV_HEADER_COMMON, sizeof(*hdr));
127
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);
131 }
132
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)
136 {
137 struct br_mrp_ring_test_hdr *hdr = NULL;
138 struct sk_buff *skb = NULL;
139
140 if (!p)
141 return NULL;
142
143 skb = br_mrp_skb_alloc(p, p->dev->dev_addr, mrp_test_dmac);
144 if (!skb)
145 return NULL;
146
147 br_mrp_skb_tlv(skb, BR_MRP_TLV_HEADER_RING_TEST, sizeof(*hdr));
148 hdr = skb_put(skb, sizeof(*hdr));
149
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));
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 */
173 static 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.
195 */
196 if (mrp->ring_state == BR_MRP_RING_STATE_CLOSED ||
197 mrp->test_monitor)
198 notify_open = true;
199 }
200
201 rcu_read_lock();
202
203 p = rcu_dereference(mrp->p_port);
204 if (p) {
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;
210
211 skb_reset_network_header(skb);
212 dev_queue_xmit(skb);
213 }
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) {
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;
226
227 skb_reset_network_header(skb);
228 dev_queue_xmit(skb);
229 }
230
231 if (notify_open && !mrp->ring_role_offloaded)
232 br_mrp_port_open(p->dev, true);
233 }
234
235 out:
236 rcu_read_unlock();
237
238 queue_delayed_work(system_wq, &mrp->test_work,
239 usecs_to_jiffies(mrp->test_interval));
240 }
241
242 /* Deletes the MRP instance.
243 * note: called under rtnl_lock
244 */
245 static 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);
252 br_mrp_switchdev_send_ring_test(br, mrp, 0, 0, 0, 0);
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) ?
261 BR_STATE_FORWARDING : BR_STATE_DISABLED;
262 p->state = state;
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);
267 }
268
269 p = rtnl_dereference(mrp->s_port);
270 if (p) {
271 spin_lock_bh(&br->lock);
272 state = netif_running(br->dev) ?
273 BR_STATE_FORWARDING : BR_STATE_DISABLED;
274 p->state = state;
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);
279 }
280
281 list_del_rcu(&mrp->list);
282 kfree_rcu(mrp, rcu);
283 }
284
285 /* Adds a new MRP instance.
286 * note: called under rtnl_lock
287 */
288 int br_mrp_add(struct net_bridge *br, struct br_mrp_instance *instance)
289 {
290 struct net_bridge_port *p;
291 struct br_mrp *mrp;
292 int err;
293
294 /* If the ring exists, it is not possible to create another one with the
295 * same ring_id
296 */
297 mrp = br_mrp_find_id(br, instance->ring_id);
298 if (mrp)
299 return -EINVAL;
300
301 if (!br_mrp_get_port(br, instance->p_ifindex) ||
302 !br_mrp_get_port(br, instance->s_ifindex))
303 return -EINVAL;
304
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))
308 return -EINVAL;
309
310 mrp = kzalloc(sizeof(*mrp), GFP_KERNEL);
311 if (!mrp)
312 return -ENOMEM;
313
314 mrp->ring_id = instance->ring_id;
315 mrp->prio = instance->prio;
316
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);
323
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);
330
331 INIT_DELAYED_WORK(&mrp->test_work, br_mrp_test_work_expired);
332 list_add_tail_rcu(&mrp->list, &br->mrp_list);
333
334 err = br_mrp_switchdev_add(br, mrp);
335 if (err)
336 goto delete_mrp;
337
338 return 0;
339
340 delete_mrp:
341 br_mrp_del_impl(br, mrp);
342
343 return err;
344 }
345
346 /* Deletes the MRP instance from which the port is part of
347 * note: called under rtnl_lock
348 */
349 void br_mrp_port_del(struct net_bridge *br, struct net_bridge_port *p)
350 {
351 struct br_mrp *mrp = br_mrp_find_port(br, p);
352
353 /* If the port is not part of a MRP instance just bail out */
354 if (!mrp)
355 return;
356
357 br_mrp_del_impl(br, mrp);
358 }
359
360 /* Deletes existing MRP instance based on ring_id
361 * note: called under rtnl_lock
362 */
363 int br_mrp_del(struct net_bridge *br, struct br_mrp_instance *instance)
364 {
365 struct br_mrp *mrp = br_mrp_find_id(br, instance->ring_id);
366
367 if (!mrp)
368 return -EINVAL;
369
370 br_mrp_del_impl(br, mrp);
371
372 return 0;
373 }
374
375 /* Set port state, port state can be forwarding, blocked or disabled
376 * note: already called with rtnl_lock
377 */
378 int br_mrp_set_port_state(struct net_bridge_port *p,
379 enum br_mrp_port_state_type state)
380 {
381 if (!p || !(p->flags & BR_MRP_AWARE))
382 return -EINVAL;
383
384 spin_lock_bh(&p->br->lock);
385
386 if (state == BR_MRP_PORT_STATE_FORWARDING)
387 p->state = BR_STATE_FORWARDING;
388 else
389 p->state = BR_STATE_BLOCKING;
390
391 spin_unlock_bh(&p->br->lock);
392
393 br_mrp_port_switchdev_set_state(p, state);
394
395 return 0;
396 }
397
398 /* Set port role, port role can be primary or secondary
399 * note: already called with rtnl_lock
400 */
401 int br_mrp_set_port_role(struct net_bridge_port *p,
402 enum br_mrp_port_role_type role)
403 {
404 struct br_mrp *mrp;
405
406 if (!p || !(p->flags & BR_MRP_AWARE))
407 return -EINVAL;
408
409 mrp = br_mrp_find_port(p->br, p);
410
411 if (!mrp)
412 return -EINVAL;
413
414 if (role == BR_MRP_PORT_ROLE_PRIMARY)
415 rcu_assign_pointer(mrp->p_port, p);
416 else
417 rcu_assign_pointer(mrp->s_port, p);
418
419 br_mrp_port_switchdev_set_role(p, role);
420
421 return 0;
422 }
423
424 /* Set ring state, ring state can be only Open or Closed
425 * note: already called with rtnl_lock
426 */
427 int br_mrp_set_ring_state(struct net_bridge *br,
428 struct br_mrp_ring_state *state)
429 {
430 struct br_mrp *mrp = br_mrp_find_id(br, state->ring_id);
431
432 if (!mrp)
433 return -EINVAL;
434
435 if (mrp->ring_state == BR_MRP_RING_STATE_CLOSED &&
436 state->ring_state != BR_MRP_RING_STATE_CLOSED)
437 mrp->ring_transitions++;
438
439 mrp->ring_state = state->ring_state;
440
441 br_mrp_switchdev_set_ring_state(br, mrp, state->ring_state);
442
443 return 0;
444 }
445
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
449 */
450 int br_mrp_set_ring_role(struct net_bridge *br,
451 struct br_mrp_ring_role *role)
452 {
453 struct br_mrp *mrp = br_mrp_find_id(br, role->ring_id);
454 int err;
455
456 if (!mrp)
457 return -EINVAL;
458
459 mrp->ring_role = role->ring_role;
460
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)
464 return err;
465
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
471 */
472 mrp->ring_role_offloaded = err == -EOPNOTSUPP ? 0 : 1;
473
474 return 0;
475 }
476
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
480 */
481 int 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
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.
491 */
492 if (!br_mrp_switchdev_send_ring_test(br, mrp, test->interval,
493 test->max_miss, test->period,
494 test->monitor))
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;
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
509 * userspace application
510 * note: already called with rcu_read_lock
511 */
512 static void br_mrp_mrm_process(struct br_mrp *mrp, struct net_bridge_port *port,
513 struct sk_buff *skb)
514 {
515 const struct br_mrp_tlv_hdr *hdr;
516 struct br_mrp_tlv_hdr _hdr;
517
518 /* Each MRP header starts with a version field which is 16 bits.
519 * Therefore skip the version and get directly the TLV header.
520 */
521 hdr = skb_header_pointer(skb, sizeof(uint16_t), sizeof(_hdr), &_hdr);
522 if (!hdr)
523 return;
524
525 if (hdr->type != BR_MRP_TLV_HEADER_RING_TEST)
526 return;
527
528 mrp->test_count_miss = 0;
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 */
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)
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 */
556 static 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
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 */
592 static 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;
596 struct net_bridge_port *p_port, *s_port;
597 struct net_bridge *br;
598 struct sk_buff *nskb;
599 struct br_mrp *mrp;
600
601 /* If port is disabled don't accept any frames */
602 if (p->state == BR_STATE_DISABLED)
603 return 0;
604
605 br = p->br;
606 mrp = br_mrp_find_port(br, p);
607 if (unlikely(!mrp))
608 return 0;
609
610 p_port = rcu_dereference(mrp->p_port);
611 if (!p_port)
612 return 0;
613
614 s_port = rcu_dereference(mrp->s_port);
615 if (!s_port)
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
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
644 if (p_dev == dev)
645 d_dev = s_dev;
646 else
647 d_dev = p_dev;
648
649 nskb->dev = d_dev;
650 skb_push(nskb, ETH_HLEN);
651 dev_queue_xmit(nskb);
652
653 return 1;
654 }
655
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
658 * normal forwarding.
659 * note: already called with rcu_read_lock
660 */
661 int br_mrp_process(struct net_bridge_port *p, struct sk_buff *skb)
662 {
663 /* If there is no MRP instance do normal forwarding */
664 if (likely(!(p->flags & BR_MRP_AWARE)))
665 goto out;
666
667 if (unlikely(skb->protocol == htons(ETH_P_MRP)))
668 return br_mrp_rcv(p, skb, p->dev);
669
670 out:
671 return 0;
672 }
673
674 bool br_mrp_enabled(struct net_bridge *br)
675 {
676 return !list_empty(&br->mrp_list);
677 }