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