bridge: mrp: Extend br_mrp_switchdev to detect better the errors
[linux-block.git] / net / bridge / br_mrp.c
CommitLineData
9a9f26e8
HV
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 };
537ed567
HV
7static const u8 mrp_in_test_dmac[ETH_ALEN] = { 0x1, 0x15, 0x4e, 0x0, 0x0, 0x3 };
8
90c628dd
HB
9static int br_mrp_process(struct net_bridge_port *p, struct sk_buff *skb);
10
11static struct br_frame_type mrp_frame_type __read_mostly = {
12 .type = cpu_to_be16(ETH_P_MRP),
13 .frame_handler = br_mrp_process,
14};
15
537ed567
HV
16static 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)
19{
20 if (port == p_port ||
21 port == s_port)
22 return true;
23
24 return false;
25}
26
27static bool br_mrp_is_in_port(struct net_bridge_port *i_port,
28 struct net_bridge_port *port)
29{
30 if (port == i_port)
31 return true;
32
33 return false;
34}
9a9f26e8
HV
35
36static struct net_bridge_port *br_mrp_get_port(struct net_bridge *br,
37 u32 ifindex)
38{
39 struct net_bridge_port *res = NULL;
40 struct net_bridge_port *port;
41
42 list_for_each_entry(port, &br->port_list, list) {
43 if (port->dev->ifindex == ifindex) {
44 res = port;
45 break;
46 }
47 }
48
49 return res;
50}
51
52static struct br_mrp *br_mrp_find_id(struct net_bridge *br, u32 ring_id)
53{
54 struct br_mrp *res = NULL;
55 struct br_mrp *mrp;
56
0169b820
HV
57 hlist_for_each_entry_rcu(mrp, &br->mrp_list, list,
58 lockdep_rtnl_is_held()) {
9a9f26e8
HV
59 if (mrp->ring_id == ring_id) {
60 res = mrp;
61 break;
62 }
63 }
64
65 return res;
66}
67
537ed567
HV
68static struct br_mrp *br_mrp_find_in_id(struct net_bridge *br, u32 in_id)
69{
70 struct br_mrp *res = NULL;
71 struct br_mrp *mrp;
72
0169b820
HV
73 hlist_for_each_entry_rcu(mrp, &br->mrp_list, list,
74 lockdep_rtnl_is_held()) {
537ed567
HV
75 if (mrp->in_id == in_id) {
76 res = mrp;
77 break;
78 }
79 }
80
81 return res;
82}
83
7aa38018
HV
84static bool br_mrp_unique_ifindex(struct net_bridge *br, u32 ifindex)
85{
86 struct br_mrp *mrp;
87
0169b820
HV
88 hlist_for_each_entry_rcu(mrp, &br->mrp_list, list,
89 lockdep_rtnl_is_held()) {
7aa38018
HV
90 struct net_bridge_port *p;
91
92 p = rtnl_dereference(mrp->p_port);
93 if (p && p->dev->ifindex == ifindex)
94 return false;
95
96 p = rtnl_dereference(mrp->s_port);
97 if (p && p->dev->ifindex == ifindex)
98 return false;
537ed567
HV
99
100 p = rtnl_dereference(mrp->i_port);
101 if (p && p->dev->ifindex == ifindex)
102 return false;
7aa38018
HV
103 }
104
105 return true;
106}
107
9a9f26e8
HV
108static struct br_mrp *br_mrp_find_port(struct net_bridge *br,
109 struct net_bridge_port *p)
110{
111 struct br_mrp *res = NULL;
112 struct br_mrp *mrp;
113
0169b820
HV
114 hlist_for_each_entry_rcu(mrp, &br->mrp_list, list,
115 lockdep_rtnl_is_held()) {
9a9f26e8 116 if (rcu_access_pointer(mrp->p_port) == p ||
537ed567
HV
117 rcu_access_pointer(mrp->s_port) == p ||
118 rcu_access_pointer(mrp->i_port) == p) {
9a9f26e8
HV
119 res = mrp;
120 break;
121 }
122 }
123
124 return res;
125}
126
127static int br_mrp_next_seq(struct br_mrp *mrp)
128{
129 mrp->seq_id++;
130 return mrp->seq_id;
131}
132
133static struct sk_buff *br_mrp_skb_alloc(struct net_bridge_port *p,
134 const u8 *src, const u8 *dst)
135{
136 struct ethhdr *eth_hdr;
137 struct sk_buff *skb;
9b14d1f8 138 __be16 *version;
9a9f26e8
HV
139
140 skb = dev_alloc_skb(MRP_MAX_FRAME_LENGTH);
141 if (!skb)
142 return NULL;
143
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));
148
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);
153
154 version = skb_put(skb, sizeof(*version));
155 *version = cpu_to_be16(MRP_VERSION);
156
157 return skb;
158}
159
160static void br_mrp_skb_tlv(struct sk_buff *skb,
161 enum br_mrp_tlv_header_type type,
162 u8 length)
163{
164 struct br_mrp_tlv_hdr *hdr;
165
166 hdr = skb_put(skb, sizeof(*hdr));
167 hdr->type = type;
168 hdr->length = length;
169}
170
171static void br_mrp_skb_common(struct sk_buff *skb, struct br_mrp *mrp)
172{
173 struct br_mrp_common_hdr *hdr;
174
175 br_mrp_skb_tlv(skb, BR_MRP_TLV_HEADER_COMMON, sizeof(*hdr));
176
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);
180}
181
182static 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)
185{
186 struct br_mrp_ring_test_hdr *hdr = NULL;
187 struct sk_buff *skb = NULL;
188
189 if (!p)
190 return NULL;
191
192 skb = br_mrp_skb_alloc(p, p->dev->dev_addr, mrp_test_dmac);
193 if (!skb)
194 return NULL;
195
196 br_mrp_skb_tlv(skb, BR_MRP_TLV_HEADER_RING_TEST, sizeof(*hdr));
197 hdr = skb_put(skb, sizeof(*hdr));
198
4b3a61b0 199 hdr->prio = cpu_to_be16(mrp->prio);
9a9f26e8
HV
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));
205
206 br_mrp_skb_common(skb, mrp);
207 br_mrp_skb_tlv(skb, BR_MRP_TLV_HEADER_END, 0x0);
208
209 return skb;
210}
211
537ed567
HV
212static 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)
215{
216 struct br_mrp_in_test_hdr *hdr = NULL;
217 struct sk_buff *skb = NULL;
218
219 if (!p)
220 return NULL;
221
222 skb = br_mrp_skb_alloc(p, p->dev->dev_addr, mrp_in_test_dmac);
223 if (!skb)
224 return NULL;
225
226 br_mrp_skb_tlv(skb, BR_MRP_TLV_HEADER_IN_TEST, sizeof(*hdr));
227 hdr = skb_put(skb, sizeof(*hdr));
228
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));
235
236 br_mrp_skb_common(skb, mrp);
237 br_mrp_skb_tlv(skb, BR_MRP_TLV_HEADER_END, 0x0);
238
239 return skb;
240}
241
c6676e7d
HV
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.
251 */
9a9f26e8
HV
252static void br_mrp_test_work_expired(struct work_struct *work)
253{
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;
259
260 if (time_before_eq(mrp->test_end, jiffies))
261 return;
262
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.
c6676e7d
HV
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.
9a9f26e8 274 */
c6676e7d
HV
275 if (mrp->ring_state == BR_MRP_RING_STATE_CLOSED ||
276 mrp->test_monitor)
9a9f26e8
HV
277 notify_open = true;
278 }
279
280 rcu_read_lock();
281
282 p = rcu_dereference(mrp->p_port);
283 if (p) {
c6676e7d
HV
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;
289
290 skb_reset_network_header(skb);
291 dev_queue_xmit(skb);
292 }
9a9f26e8
HV
293
294 if (notify_open && !mrp->ring_role_offloaded)
4cc625c6 295 br_mrp_ring_port_open(p->dev, true);
9a9f26e8
HV
296 }
297
298 p = rcu_dereference(mrp->s_port);
299 if (p) {
c6676e7d
HV
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;
305
306 skb_reset_network_header(skb);
307 dev_queue_xmit(skb);
308 }
9a9f26e8
HV
309
310 if (notify_open && !mrp->ring_role_offloaded)
4cc625c6 311 br_mrp_ring_port_open(p->dev, true);
9a9f26e8
HV
312 }
313
314out:
315 rcu_read_unlock();
316
317 queue_delayed_work(system_wq, &mrp->test_work,
318 usecs_to_jiffies(mrp->test_interval));
319}
320
537ed567
HV
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.
324 */
325static void br_mrp_in_test_work_expired(struct work_struct *work)
326{
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;
332
333 if (time_before_eq(mrp->in_test_end, jiffies))
334 return;
335
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.
342 */
343 if (mrp->in_state == BR_MRP_IN_STATE_CLOSED)
344 notify_open = true;
345 }
346
347 rcu_read_lock();
348
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;
355
356 skb_reset_network_header(skb);
357 dev_queue_xmit(skb);
358
359 if (notify_open && !mrp->in_role_offloaded)
360 br_mrp_in_port_open(p->dev, true);
361 }
362
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;
369
370 skb_reset_network_header(skb);
371 dev_queue_xmit(skb);
372
373 if (notify_open && !mrp->in_role_offloaded)
374 br_mrp_in_port_open(p->dev, true);
375 }
376
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;
383
384 skb_reset_network_header(skb);
385 dev_queue_xmit(skb);
386
387 if (notify_open && !mrp->in_role_offloaded)
388 br_mrp_in_port_open(p->dev, true);
389 }
390
391out:
392 rcu_read_unlock();
393
394 queue_delayed_work(system_wq, &mrp->in_test_work,
395 usecs_to_jiffies(mrp->in_test_interval));
396}
397
9a9f26e8
HV
398/* Deletes the MRP instance.
399 * note: called under rtnl_lock
400 */
401static void br_mrp_del_impl(struct net_bridge *br, struct br_mrp *mrp)
402{
403 struct net_bridge_port *p;
4fb13499 404 u8 state;
9a9f26e8
HV
405
406 /* Stop sending MRP_Test frames */
407 cancel_delayed_work_sync(&mrp->test_work);
c6676e7d 408 br_mrp_switchdev_send_ring_test(br, mrp, 0, 0, 0, 0);
9a9f26e8 409
537ed567
HV
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);
413
9a9f26e8
HV
414 br_mrp_switchdev_del(br, mrp);
415
416 /* Reset the ports */
417 p = rtnl_dereference(mrp->p_port);
418 if (p) {
419 spin_lock_bh(&br->lock);
4fb13499
HV
420 state = netif_running(br->dev) ?
421 BR_STATE_FORWARDING : BR_STATE_DISABLED;
422 p->state = state;
9a9f26e8
HV
423 p->flags &= ~BR_MRP_AWARE;
424 spin_unlock_bh(&br->lock);
4fb13499 425 br_mrp_port_switchdev_set_state(p, state);
9a9f26e8
HV
426 rcu_assign_pointer(mrp->p_port, NULL);
427 }
428
429 p = rtnl_dereference(mrp->s_port);
430 if (p) {
431 spin_lock_bh(&br->lock);
4fb13499
HV
432 state = netif_running(br->dev) ?
433 BR_STATE_FORWARDING : BR_STATE_DISABLED;
434 p->state = state;
9a9f26e8
HV
435 p->flags &= ~BR_MRP_AWARE;
436 spin_unlock_bh(&br->lock);
4fb13499 437 br_mrp_port_switchdev_set_state(p, state);
9a9f26e8
HV
438 rcu_assign_pointer(mrp->s_port, NULL);
439 }
440
537ed567
HV
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);
451 }
452
0169b820 453 hlist_del_rcu(&mrp->list);
9a9f26e8 454 kfree_rcu(mrp, rcu);
90c628dd 455
0169b820 456 if (hlist_empty(&br->mrp_list))
90c628dd 457 br_del_frame(br, &mrp_frame_type);
9a9f26e8
HV
458}
459
460/* Adds a new MRP instance.
461 * note: called under rtnl_lock
462 */
463int br_mrp_add(struct net_bridge *br, struct br_mrp_instance *instance)
464{
465 struct net_bridge_port *p;
466 struct br_mrp *mrp;
467 int err;
468
469 /* If the ring exists, it is not possible to create another one with the
470 * same ring_id
471 */
472 mrp = br_mrp_find_id(br, instance->ring_id);
473 if (mrp)
474 return -EINVAL;
475
476 if (!br_mrp_get_port(br, instance->p_ifindex) ||
477 !br_mrp_get_port(br, instance->s_ifindex))
478 return -EINVAL;
479
7aa38018
HV
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;
484
9a9f26e8
HV
485 mrp = kzalloc(sizeof(*mrp), GFP_KERNEL);
486 if (!mrp)
487 return -ENOMEM;
488
489 mrp->ring_id = instance->ring_id;
4b3a61b0 490 mrp->prio = instance->prio;
9a9f26e8
HV
491
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);
498
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);
505
0169b820 506 if (hlist_empty(&br->mrp_list))
90c628dd
HB
507 br_add_frame(br, &mrp_frame_type);
508
9a9f26e8 509 INIT_DELAYED_WORK(&mrp->test_work, br_mrp_test_work_expired);
537ed567 510 INIT_DELAYED_WORK(&mrp->in_test_work, br_mrp_in_test_work_expired);
0169b820 511 hlist_add_tail_rcu(&mrp->list, &br->mrp_list);
9a9f26e8
HV
512
513 err = br_mrp_switchdev_add(br, mrp);
514 if (err)
515 goto delete_mrp;
516
517 return 0;
518
519delete_mrp:
520 br_mrp_del_impl(br, mrp);
521
522 return err;
523}
524
525/* Deletes the MRP instance from which the port is part of
526 * note: called under rtnl_lock
527 */
528void br_mrp_port_del(struct net_bridge *br, struct net_bridge_port *p)
529{
530 struct br_mrp *mrp = br_mrp_find_port(br, p);
531
532 /* If the port is not part of a MRP instance just bail out */
533 if (!mrp)
534 return;
535
536 br_mrp_del_impl(br, mrp);
537}
538
539/* Deletes existing MRP instance based on ring_id
540 * note: called under rtnl_lock
541 */
542int br_mrp_del(struct net_bridge *br, struct br_mrp_instance *instance)
543{
544 struct br_mrp *mrp = br_mrp_find_id(br, instance->ring_id);
545
546 if (!mrp)
547 return -EINVAL;
548
549 br_mrp_del_impl(br, mrp);
550
551 return 0;
552}
553
554/* Set port state, port state can be forwarding, blocked or disabled
555 * note: already called with rtnl_lock
556 */
557int br_mrp_set_port_state(struct net_bridge_port *p,
558 enum br_mrp_port_state_type state)
559{
b2bdba1c
HV
560 u32 port_state;
561
9a9f26e8
HV
562 if (!p || !(p->flags & BR_MRP_AWARE))
563 return -EINVAL;
564
565 spin_lock_bh(&p->br->lock);
566
567 if (state == BR_MRP_PORT_STATE_FORWARDING)
b2bdba1c 568 port_state = BR_STATE_FORWARDING;
9a9f26e8 569 else
b2bdba1c 570 port_state = BR_STATE_BLOCKING;
9a9f26e8 571
b2bdba1c 572 p->state = port_state;
9a9f26e8
HV
573 spin_unlock_bh(&p->br->lock);
574
b2bdba1c 575 br_mrp_port_switchdev_set_state(p, port_state);
9a9f26e8
HV
576
577 return 0;
578}
579
580/* Set port role, port role can be primary or secondary
581 * note: already called with rtnl_lock
582 */
583int br_mrp_set_port_role(struct net_bridge_port *p,
20f6a05e 584 enum br_mrp_port_role_type role)
9a9f26e8
HV
585{
586 struct br_mrp *mrp;
587
588 if (!p || !(p->flags & BR_MRP_AWARE))
589 return -EINVAL;
590
20f6a05e 591 mrp = br_mrp_find_port(p->br, p);
9a9f26e8
HV
592
593 if (!mrp)
594 return -EINVAL;
595
7882c895
HV
596 switch (role) {
597 case BR_MRP_PORT_ROLE_PRIMARY:
9a9f26e8 598 rcu_assign_pointer(mrp->p_port, p);
7882c895
HV
599 break;
600 case BR_MRP_PORT_ROLE_SECONDARY:
9a9f26e8 601 rcu_assign_pointer(mrp->s_port, p);
7882c895
HV
602 break;
603 default:
604 return -EINVAL;
605 }
9a9f26e8 606
20f6a05e 607 br_mrp_port_switchdev_set_role(p, role);
9a9f26e8
HV
608
609 return 0;
610}
611
612/* Set ring state, ring state can be only Open or Closed
613 * note: already called with rtnl_lock
614 */
615int br_mrp_set_ring_state(struct net_bridge *br,
616 struct br_mrp_ring_state *state)
617{
618 struct br_mrp *mrp = br_mrp_find_id(br, state->ring_id);
619
620 if (!mrp)
621 return -EINVAL;
622
623 if (mrp->ring_state == BR_MRP_RING_STATE_CLOSED &&
624 state->ring_state != BR_MRP_RING_STATE_CLOSED)
625 mrp->ring_transitions++;
626
627 mrp->ring_state = state->ring_state;
628
629 br_mrp_switchdev_set_ring_state(br, mrp, state->ring_state);
630
631 return 0;
632}
633
634/* Set ring role, ring role can be only MRM(Media Redundancy Manager) or
635 * MRC(Media Redundancy Client).
636 * note: already called with rtnl_lock
637 */
638int br_mrp_set_ring_role(struct net_bridge *br,
639 struct br_mrp_ring_role *role)
640{
641 struct br_mrp *mrp = br_mrp_find_id(br, role->ring_id);
642 int err;
643
644 if (!mrp)
645 return -EINVAL;
646
647 mrp->ring_role = role->ring_role;
648
649 /* If there is an error just bailed out */
650 err = br_mrp_switchdev_set_ring_role(br, mrp, role->ring_role);
651 if (err && err != -EOPNOTSUPP)
652 return err;
653
654 /* Now detect if the HW actually applied the role or not. If the HW
655 * applied the role it means that the SW will not to do those operations
656 * anymore. For example if the role ir MRM then the HW will notify the
657 * SW when ring is open, but if the is not pushed to the HW the SW will
658 * need to detect when the ring is open
659 */
660 mrp->ring_role_offloaded = err == -EOPNOTSUPP ? 0 : 1;
661
662 return 0;
663}
664
c6676e7d
HV
665/* Start to generate or monitor MRP test frames, the frames are generated by
666 * HW and if it fails, they are generated by the SW.
9a9f26e8
HV
667 * note: already called with rtnl_lock
668 */
669int br_mrp_start_test(struct net_bridge *br,
670 struct br_mrp_start_test *test)
671{
672 struct br_mrp *mrp = br_mrp_find_id(br, test->ring_id);
673
674 if (!mrp)
675 return -EINVAL;
676
c6676e7d
HV
677 /* Try to push it to the HW and if it fails then continue with SW
678 * implementation and if that also fails then return error.
9a9f26e8
HV
679 */
680 if (!br_mrp_switchdev_send_ring_test(br, mrp, test->interval,
c6676e7d
HV
681 test->max_miss, test->period,
682 test->monitor))
9a9f26e8
HV
683 return 0;
684
685 mrp->test_interval = test->interval;
686 mrp->test_end = jiffies + usecs_to_jiffies(test->period);
687 mrp->test_max_miss = test->max_miss;
c6676e7d 688 mrp->test_monitor = test->monitor;
9a9f26e8
HV
689 mrp->test_count_miss = 0;
690 queue_delayed_work(system_wq, &mrp->test_work,
691 usecs_to_jiffies(test->interval));
692
693 return 0;
694}
695
537ed567
HV
696/* Set in state, int state can be only Open or Closed
697 * note: already called with rtnl_lock
698 */
699int br_mrp_set_in_state(struct net_bridge *br, struct br_mrp_in_state *state)
700{
701 struct br_mrp *mrp = br_mrp_find_in_id(br, state->in_id);
702
703 if (!mrp)
704 return -EINVAL;
705
706 if (mrp->in_state == BR_MRP_IN_STATE_CLOSED &&
707 state->in_state != BR_MRP_IN_STATE_CLOSED)
708 mrp->in_transitions++;
709
710 mrp->in_state = state->in_state;
711
712 br_mrp_switchdev_set_in_state(br, mrp, state->in_state);
713
714 return 0;
715}
716
717/* Set in role, in role can be only MIM(Media Interconnection Manager) or
718 * MIC(Media Interconnection Client).
719 * note: already called with rtnl_lock
720 */
721int br_mrp_set_in_role(struct net_bridge *br, struct br_mrp_in_role *role)
722{
723 struct br_mrp *mrp = br_mrp_find_id(br, role->ring_id);
724 struct net_bridge_port *p;
725 int err;
726
727 if (!mrp)
728 return -EINVAL;
729
730 if (!br_mrp_get_port(br, role->i_ifindex))
731 return -EINVAL;
732
733 if (role->in_role == BR_MRP_IN_ROLE_DISABLED) {
734 u8 state;
735
736 /* It is not allowed to disable a port that doesn't exist */
737 p = rtnl_dereference(mrp->i_port);
738 if (!p)
739 return -EINVAL;
740
741 /* Stop the generating MRP_InTest frames */
742 cancel_delayed_work_sync(&mrp->in_test_work);
743 br_mrp_switchdev_send_in_test(br, mrp, 0, 0, 0);
744
745 /* Remove the port */
746 spin_lock_bh(&br->lock);
747 state = netif_running(br->dev) ?
748 BR_STATE_FORWARDING : BR_STATE_DISABLED;
749 p->state = state;
750 p->flags &= ~BR_MRP_AWARE;
751 spin_unlock_bh(&br->lock);
752 br_mrp_port_switchdev_set_state(p, state);
753 rcu_assign_pointer(mrp->i_port, NULL);
754
755 mrp->in_role = role->in_role;
756 mrp->in_id = 0;
757
758 return 0;
759 }
760
761 /* It is not possible to have the same port part of multiple rings */
762 if (!br_mrp_unique_ifindex(br, role->i_ifindex))
763 return -EINVAL;
764
765 /* It is not allowed to set a different interconnect port if the mrp
766 * instance has already one. First it needs to be disabled and after
767 * that set the new port
768 */
769 if (rcu_access_pointer(mrp->i_port))
770 return -EINVAL;
771
772 p = br_mrp_get_port(br, role->i_ifindex);
773 spin_lock_bh(&br->lock);
774 p->state = BR_STATE_FORWARDING;
775 p->flags |= BR_MRP_AWARE;
776 spin_unlock_bh(&br->lock);
777 rcu_assign_pointer(mrp->i_port, p);
778
779 mrp->in_role = role->in_role;
780 mrp->in_id = role->in_id;
781
782 /* If there is an error just bailed out */
783 err = br_mrp_switchdev_set_in_role(br, mrp, role->in_id,
784 role->ring_id, role->in_role);
785 if (err && err != -EOPNOTSUPP)
786 return err;
787
788 /* Now detect if the HW actually applied the role or not. If the HW
789 * applied the role it means that the SW will not to do those operations
790 * anymore. For example if the role is MIM then the HW will notify the
791 * SW when interconnect ring is open, but if the is not pushed to the HW
792 * the SW will need to detect when the interconnect ring is open.
793 */
794 mrp->in_role_offloaded = err == -EOPNOTSUPP ? 0 : 1;
795
796 return 0;
797}
798
799/* Start to generate MRP_InTest frames, the frames are generated by
800 * HW and if it fails, they are generated by the SW.
801 * note: already called with rtnl_lock
802 */
803int br_mrp_start_in_test(struct net_bridge *br,
804 struct br_mrp_start_in_test *in_test)
805{
806 struct br_mrp *mrp = br_mrp_find_in_id(br, in_test->in_id);
807
808 if (!mrp)
809 return -EINVAL;
810
811 if (mrp->in_role != BR_MRP_IN_ROLE_MIM)
812 return -EINVAL;
813
814 /* Try to push it to the HW and if it fails then continue with SW
815 * implementation and if that also fails then return error.
816 */
817 if (!br_mrp_switchdev_send_in_test(br, mrp, in_test->interval,
818 in_test->max_miss, in_test->period))
819 return 0;
820
821 mrp->in_test_interval = in_test->interval;
822 mrp->in_test_end = jiffies + usecs_to_jiffies(in_test->period);
823 mrp->in_test_max_miss = in_test->max_miss;
824 mrp->in_test_count_miss = 0;
825 queue_delayed_work(system_wq, &mrp->in_test_work,
826 usecs_to_jiffies(in_test->interval));
827
828 return 0;
829}
830
efb5b338 831/* Determine if the frame type is a ring frame */
537ed567
HV
832static bool br_mrp_ring_frame(struct sk_buff *skb)
833{
834 const struct br_mrp_tlv_hdr *hdr;
835 struct br_mrp_tlv_hdr _hdr;
836
837 hdr = skb_header_pointer(skb, sizeof(uint16_t), sizeof(_hdr), &_hdr);
838 if (!hdr)
839 return false;
840
841 if (hdr->type == BR_MRP_TLV_HEADER_RING_TEST ||
842 hdr->type == BR_MRP_TLV_HEADER_RING_TOPO ||
843 hdr->type == BR_MRP_TLV_HEADER_RING_LINK_DOWN ||
844 hdr->type == BR_MRP_TLV_HEADER_RING_LINK_UP ||
845 hdr->type == BR_MRP_TLV_HEADER_OPTION)
846 return true;
847
848 return false;
849}
850
efb5b338 851/* Determine if the frame type is an interconnect frame */
537ed567
HV
852static bool br_mrp_in_frame(struct sk_buff *skb)
853{
854 const struct br_mrp_tlv_hdr *hdr;
855 struct br_mrp_tlv_hdr _hdr;
856
857 hdr = skb_header_pointer(skb, sizeof(uint16_t), sizeof(_hdr), &_hdr);
858 if (!hdr)
859 return false;
860
861 if (hdr->type == BR_MRP_TLV_HEADER_IN_TEST ||
862 hdr->type == BR_MRP_TLV_HEADER_IN_TOPO ||
863 hdr->type == BR_MRP_TLV_HEADER_IN_LINK_DOWN ||
bfd04232
HV
864 hdr->type == BR_MRP_TLV_HEADER_IN_LINK_UP ||
865 hdr->type == BR_MRP_TLV_HEADER_IN_LINK_STATUS)
537ed567
HV
866 return true;
867
868 return false;
869}
870
9a9f26e8
HV
871/* Process only MRP Test frame. All the other MRP frames are processed by
872 * userspace application
873 * note: already called with rcu_read_lock
874 */
875static void br_mrp_mrm_process(struct br_mrp *mrp, struct net_bridge_port *port,
876 struct sk_buff *skb)
877{
878 const struct br_mrp_tlv_hdr *hdr;
879 struct br_mrp_tlv_hdr _hdr;
880
881 /* Each MRP header starts with a version field which is 16 bits.
882 * Therefore skip the version and get directly the TLV header.
883 */
884 hdr = skb_header_pointer(skb, sizeof(uint16_t), sizeof(_hdr), &_hdr);
885 if (!hdr)
886 return;
887
888 if (hdr->type != BR_MRP_TLV_HEADER_RING_TEST)
889 return;
890
891 mrp->test_count_miss = 0;
892
893 /* Notify the userspace that the ring is closed only when the ring is
894 * not closed
895 */
896 if (mrp->ring_state != BR_MRP_RING_STATE_CLOSED)
4cc625c6 897 br_mrp_ring_port_open(port->dev, false);
9a9f26e8
HV
898}
899
efb5b338 900/* Determine if the test hdr has a better priority than the node */
c6676e7d
HV
901static bool br_mrp_test_better_than_own(struct br_mrp *mrp,
902 struct net_bridge *br,
903 const struct br_mrp_ring_test_hdr *hdr)
904{
905 u16 prio = be16_to_cpu(hdr->prio);
906
907 if (prio < mrp->prio ||
908 (prio == mrp->prio &&
909 ether_addr_to_u64(hdr->sa) < ether_addr_to_u64(br->dev->dev_addr)))
910 return true;
911
912 return false;
913}
914
915/* Process only MRP Test frame. All the other MRP frames are processed by
916 * userspace application
917 * note: already called with rcu_read_lock
918 */
919static void br_mrp_mra_process(struct br_mrp *mrp, struct net_bridge *br,
920 struct net_bridge_port *port,
921 struct sk_buff *skb)
922{
923 const struct br_mrp_ring_test_hdr *test_hdr;
924 struct br_mrp_ring_test_hdr _test_hdr;
925 const struct br_mrp_tlv_hdr *hdr;
926 struct br_mrp_tlv_hdr _hdr;
927
928 /* Each MRP header starts with a version field which is 16 bits.
929 * Therefore skip the version and get directly the TLV header.
930 */
931 hdr = skb_header_pointer(skb, sizeof(uint16_t), sizeof(_hdr), &_hdr);
932 if (!hdr)
933 return;
934
935 if (hdr->type != BR_MRP_TLV_HEADER_RING_TEST)
936 return;
937
938 test_hdr = skb_header_pointer(skb, sizeof(uint16_t) + sizeof(_hdr),
939 sizeof(_test_hdr), &_test_hdr);
940 if (!test_hdr)
941 return;
942
943 /* Only frames that have a better priority than the node will
944 * clear the miss counter because otherwise the node will need to behave
945 * as MRM.
946 */
947 if (br_mrp_test_better_than_own(mrp, br, test_hdr))
948 mrp->test_count_miss = 0;
949}
950
537ed567
HV
951/* Process only MRP InTest frame. All the other MRP frames are processed by
952 * userspace application
953 * note: already called with rcu_read_lock
954 */
955static bool br_mrp_mim_process(struct br_mrp *mrp, struct net_bridge_port *port,
956 struct sk_buff *skb)
957{
958 const struct br_mrp_in_test_hdr *in_hdr;
959 struct br_mrp_in_test_hdr _in_hdr;
960 const struct br_mrp_tlv_hdr *hdr;
961 struct br_mrp_tlv_hdr _hdr;
962
963 /* Each MRP header starts with a version field which is 16 bits.
964 * Therefore skip the version and get directly the TLV header.
965 */
966 hdr = skb_header_pointer(skb, sizeof(uint16_t), sizeof(_hdr), &_hdr);
967 if (!hdr)
968 return false;
969
970 /* The check for InTest frame type was already done */
971 in_hdr = skb_header_pointer(skb, sizeof(uint16_t) + sizeof(_hdr),
972 sizeof(_in_hdr), &_in_hdr);
973 if (!in_hdr)
974 return false;
975
976 /* It needs to process only it's own InTest frames. */
977 if (mrp->in_id != ntohs(in_hdr->id))
978 return false;
979
980 mrp->in_test_count_miss = 0;
981
982 /* Notify the userspace that the ring is closed only when the ring is
983 * not closed
984 */
985 if (mrp->in_state != BR_MRP_IN_STATE_CLOSED)
986 br_mrp_in_port_open(port->dev, false);
987
988 return true;
989}
990
991/* Get the MRP frame type
992 * note: already called with rcu_read_lock
993 */
994static u8 br_mrp_get_frame_type(struct sk_buff *skb)
995{
996 const struct br_mrp_tlv_hdr *hdr;
997 struct br_mrp_tlv_hdr _hdr;
998
999 /* Each MRP header starts with a version field which is 16 bits.
1000 * Therefore skip the version and get directly the TLV header.
1001 */
1002 hdr = skb_header_pointer(skb, sizeof(uint16_t), sizeof(_hdr), &_hdr);
1003 if (!hdr)
1004 return 0xff;
1005
1006 return hdr->type;
1007}
1008
1009static bool br_mrp_mrm_behaviour(struct br_mrp *mrp)
1010{
1011 if (mrp->ring_role == BR_MRP_RING_ROLE_MRM ||
1012 (mrp->ring_role == BR_MRP_RING_ROLE_MRA && !mrp->test_monitor))
1013 return true;
1014
1015 return false;
1016}
1017
1018static bool br_mrp_mrc_behaviour(struct br_mrp *mrp)
1019{
1020 if (mrp->ring_role == BR_MRP_RING_ROLE_MRC ||
1021 (mrp->ring_role == BR_MRP_RING_ROLE_MRA && mrp->test_monitor))
1022 return true;
1023
1024 return false;
1025}
1026
1027/* This will just forward the frame to the other mrp ring ports, depending on
1028 * the frame type, ring role and interconnect role
9a9f26e8
HV
1029 * note: already called with rcu_read_lock
1030 */
1031static int br_mrp_rcv(struct net_bridge_port *p,
1032 struct sk_buff *skb, struct net_device *dev)
1033{
537ed567
HV
1034 struct net_bridge_port *p_port, *s_port, *i_port = NULL;
1035 struct net_bridge_port *p_dst, *s_dst, *i_dst = NULL;
9a9f26e8 1036 struct net_bridge *br;
9a9f26e8
HV
1037 struct br_mrp *mrp;
1038
1039 /* If port is disabled don't accept any frames */
1040 if (p->state == BR_STATE_DISABLED)
1041 return 0;
1042
1043 br = p->br;
1044 mrp = br_mrp_find_port(br, p);
1045 if (unlikely(!mrp))
1046 return 0;
1047
1048 p_port = rcu_dereference(mrp->p_port);
1049 if (!p_port)
1050 return 0;
537ed567 1051 p_dst = p_port;
9a9f26e8
HV
1052
1053 s_port = rcu_dereference(mrp->s_port);
1054 if (!s_port)
1055 return 0;
537ed567 1056 s_dst = s_port;
9a9f26e8 1057
537ed567
HV
1058 /* If the frame is a ring frame then it is not required to check the
1059 * interconnect role and ports to process or forward the frame
c6676e7d 1060 */
537ed567
HV
1061 if (br_mrp_ring_frame(skb)) {
1062 /* If the role is MRM then don't forward the frames */
1063 if (mrp->ring_role == BR_MRP_RING_ROLE_MRM) {
c6676e7d 1064 br_mrp_mrm_process(mrp, p, skb);
537ed567 1065 goto no_forward;
c6676e7d
HV
1066 }
1067
537ed567
HV
1068 /* If the role is MRA then don't forward the frames if it
1069 * behaves as MRM node
1070 */
1071 if (mrp->ring_role == BR_MRP_RING_ROLE_MRA) {
1072 if (!mrp->test_monitor) {
1073 br_mrp_mrm_process(mrp, p, skb);
1074 goto no_forward;
1075 }
1076
1077 br_mrp_mra_process(mrp, br, p, skb);
1078 }
1079
1080 goto forward;
c6676e7d
HV
1081 }
1082
537ed567
HV
1083 if (br_mrp_in_frame(skb)) {
1084 u8 in_type = br_mrp_get_frame_type(skb);
9a9f26e8 1085
537ed567
HV
1086 i_port = rcu_dereference(mrp->i_port);
1087 i_dst = i_port;
9a9f26e8 1088
537ed567
HV
1089 /* If the ring port is in block state it should not forward
1090 * In_Test frames
1091 */
1092 if (br_mrp_is_ring_port(p_port, s_port, p) &&
1093 p->state == BR_STATE_BLOCKING &&
1094 in_type == BR_MRP_TLV_HEADER_IN_TEST)
1095 goto no_forward;
1096
1097 /* Nodes that behaves as MRM needs to stop forwarding the
1098 * frames in case the ring is closed, otherwise will be a loop.
1099 * In this case the frame is no forward between the ring ports.
1100 */
1101 if (br_mrp_mrm_behaviour(mrp) &&
1102 br_mrp_is_ring_port(p_port, s_port, p) &&
1103 (s_port->state != BR_STATE_FORWARDING ||
1104 p_port->state != BR_STATE_FORWARDING)) {
1105 p_dst = NULL;
1106 s_dst = NULL;
1107 }
1108
1109 /* A node that behaves as MRC and doesn't have a interconnect
1110 * role then it should forward all frames between the ring ports
1111 * because it doesn't have an interconnect port
1112 */
1113 if (br_mrp_mrc_behaviour(mrp) &&
1114 mrp->in_role == BR_MRP_IN_ROLE_DISABLED)
1115 goto forward;
1116
1117 if (mrp->in_role == BR_MRP_IN_ROLE_MIM) {
1118 if (in_type == BR_MRP_TLV_HEADER_IN_TEST) {
1119 /* MIM should not forward it's own InTest
1120 * frames
1121 */
1122 if (br_mrp_mim_process(mrp, p, skb)) {
1123 goto no_forward;
1124 } else {
1125 if (br_mrp_is_ring_port(p_port, s_port,
1126 p))
1127 i_dst = NULL;
1128
1129 if (br_mrp_is_in_port(i_port, p))
1130 goto no_forward;
1131 }
1132 } else {
bfd04232 1133 /* MIM should forward IntLinkChange/Status and
537ed567 1134 * IntTopoChange between ring ports but MIM
bfd04232 1135 * should not forward IntLinkChange/Status and
537ed567
HV
1136 * IntTopoChange if the frame was received at
1137 * the interconnect port
1138 */
1139 if (br_mrp_is_ring_port(p_port, s_port, p))
1140 i_dst = NULL;
1141
1142 if (br_mrp_is_in_port(i_port, p))
1143 goto no_forward;
1144 }
1145 }
1146
1147 if (mrp->in_role == BR_MRP_IN_ROLE_MIC) {
1148 /* MIC should forward InTest frames on all ports
1149 * regardless of the received port
1150 */
1151 if (in_type == BR_MRP_TLV_HEADER_IN_TEST)
1152 goto forward;
1153
1154 /* MIC should forward IntLinkChange frames only if they
1155 * are received on ring ports to all the ports
1156 */
1157 if (br_mrp_is_ring_port(p_port, s_port, p) &&
1158 (in_type == BR_MRP_TLV_HEADER_IN_LINK_UP ||
1159 in_type == BR_MRP_TLV_HEADER_IN_LINK_DOWN))
1160 goto forward;
1161
bfd04232
HV
1162 /* MIC should forward IntLinkStatus frames only to
1163 * interconnect port if it was received on a ring port.
1164 * If it is received on interconnect port then, it
1165 * should be forward on both ring ports
1166 */
1167 if (br_mrp_is_ring_port(p_port, s_port, p) &&
1168 in_type == BR_MRP_TLV_HEADER_IN_LINK_STATUS) {
1169 p_dst = NULL;
1170 s_dst = NULL;
1171 }
1172
537ed567
HV
1173 /* Should forward the InTopo frames only between the
1174 * ring ports
1175 */
1176 if (in_type == BR_MRP_TLV_HEADER_IN_TOPO) {
1177 i_dst = NULL;
1178 goto forward;
1179 }
1180
1181 /* In all the other cases don't forward the frames */
1182 goto no_forward;
1183 }
1184 }
9a9f26e8 1185
537ed567
HV
1186forward:
1187 if (p_dst)
1188 br_forward(p_dst, skb, true, false);
1189 if (s_dst)
1190 br_forward(s_dst, skb, true, false);
1191 if (i_dst)
1192 br_forward(i_dst, skb, true, false);
9a9f26e8 1193
537ed567 1194no_forward:
9a9f26e8
HV
1195 return 1;
1196}
1197
1198/* Check if the frame was received on a port that is part of MRP ring
1199 * and if the frame has MRP eth. In that case process the frame otherwise do
1200 * normal forwarding.
1201 * note: already called with rcu_read_lock
1202 */
90c628dd 1203static int br_mrp_process(struct net_bridge_port *p, struct sk_buff *skb)
9a9f26e8
HV
1204{
1205 /* If there is no MRP instance do normal forwarding */
1206 if (likely(!(p->flags & BR_MRP_AWARE)))
1207 goto out;
1208
90c628dd 1209 return br_mrp_rcv(p, skb, p->dev);
9a9f26e8
HV
1210out:
1211 return 0;
1212}
1213
1214bool br_mrp_enabled(struct net_bridge *br)
1215{
0169b820 1216 return !hlist_empty(&br->mrp_list);
9a9f26e8 1217}