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
br_mrp_is_ring_port(struct net_bridge_port * p_port,struct net_bridge_port * s_port,struct net_bridge_port * port)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
br_mrp_is_in_port(struct net_bridge_port * i_port,struct net_bridge_port * port)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
br_mrp_get_port(struct net_bridge * br,u32 ifindex)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
br_mrp_find_id(struct net_bridge * br,u32 ring_id)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
br_mrp_find_in_id(struct net_bridge * br,u32 in_id)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
br_mrp_unique_ifindex(struct net_bridge * br,u32 ifindex)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
br_mrp_find_port(struct net_bridge * br,struct net_bridge_port * p)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
br_mrp_next_seq(struct br_mrp * mrp)127 static int br_mrp_next_seq(struct br_mrp *mrp)
128 {
129 mrp->seq_id++;
130 return mrp->seq_id;
131 }
132
br_mrp_skb_alloc(struct net_bridge_port * p,const u8 * src,const u8 * dst)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
br_mrp_skb_tlv(struct sk_buff * skb,enum br_mrp_tlv_header_type type,u8 length)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
br_mrp_skb_common(struct sk_buff * skb,struct br_mrp * mrp)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
br_mrp_alloc_test_skb(struct br_mrp * mrp,struct net_bridge_port * p,enum br_mrp_port_role_type port_role)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
208 /* In case the node behaves as MRA then the Test frame needs to have
209 * an Option TLV which includes eventually a sub-option TLV that has
210 * the type AUTO_MGR
211 */
212 if (mrp->ring_role == BR_MRP_RING_ROLE_MRA) {
213 struct br_mrp_sub_option1_hdr *sub_opt = NULL;
214 struct br_mrp_tlv_hdr *sub_tlv = NULL;
215 struct br_mrp_oui_hdr *oui = NULL;
216 u8 length;
217
218 length = sizeof(*sub_opt) + sizeof(*sub_tlv) + sizeof(*oui) +
219 MRP_OPT_PADDING;
220 br_mrp_skb_tlv(skb, BR_MRP_TLV_HEADER_OPTION, length);
221
222 oui = skb_put(skb, sizeof(*oui));
223 memset(oui, 0x0, sizeof(*oui));
224 sub_opt = skb_put(skb, sizeof(*sub_opt));
225 memset(sub_opt, 0x0, sizeof(*sub_opt));
226
227 /* 32 bit alligment shall be ensured therefore add 2 bytes */
228 sub_tlv = skb_put_zero(skb, sizeof(*sub_tlv) + MRP_OPT_PADDING);
229 sub_tlv->type = BR_MRP_SUB_TLV_HEADER_TEST_AUTO_MGR;
230 }
231
232 br_mrp_skb_tlv(skb, BR_MRP_TLV_HEADER_END, 0x0);
233
234 return skb;
235 }
236
br_mrp_alloc_in_test_skb(struct br_mrp * mrp,struct net_bridge_port * p,enum br_mrp_port_role_type port_role)237 static struct sk_buff *br_mrp_alloc_in_test_skb(struct br_mrp *mrp,
238 struct net_bridge_port *p,
239 enum br_mrp_port_role_type port_role)
240 {
241 struct br_mrp_in_test_hdr *hdr = NULL;
242 struct sk_buff *skb = NULL;
243
244 if (!p)
245 return NULL;
246
247 skb = br_mrp_skb_alloc(p, p->dev->dev_addr, mrp_in_test_dmac);
248 if (!skb)
249 return NULL;
250
251 br_mrp_skb_tlv(skb, BR_MRP_TLV_HEADER_IN_TEST, sizeof(*hdr));
252 hdr = skb_put(skb, sizeof(*hdr));
253
254 hdr->id = cpu_to_be16(mrp->in_id);
255 ether_addr_copy(hdr->sa, p->br->dev->dev_addr);
256 hdr->port_role = cpu_to_be16(port_role);
257 hdr->state = cpu_to_be16(mrp->in_state);
258 hdr->transitions = cpu_to_be16(mrp->in_transitions);
259 hdr->timestamp = cpu_to_be32(jiffies_to_msecs(jiffies));
260
261 br_mrp_skb_common(skb, mrp);
262 br_mrp_skb_tlv(skb, BR_MRP_TLV_HEADER_END, 0x0);
263
264 return skb;
265 }
266
267 /* This function is continuously called in the following cases:
268 * - when node role is MRM, in this case test_monitor is always set to false
269 * because it needs to notify the userspace that the ring is open and needs to
270 * send MRP_Test frames
271 * - when node role is MRA, there are 2 subcases:
272 * - when MRA behaves as MRM, in this case is similar with MRM role
273 * - when MRA behaves as MRC, in this case test_monitor is set to true,
274 * because it needs to detect when it stops seeing MRP_Test frames
275 * from MRM node but it doesn't need to send MRP_Test frames.
276 */
br_mrp_test_work_expired(struct work_struct * work)277 static void br_mrp_test_work_expired(struct work_struct *work)
278 {
279 struct delayed_work *del_work = to_delayed_work(work);
280 struct br_mrp *mrp = container_of(del_work, struct br_mrp, test_work);
281 struct net_bridge_port *p;
282 bool notify_open = false;
283 struct sk_buff *skb;
284
285 if (time_before_eq(mrp->test_end, jiffies))
286 return;
287
288 if (mrp->test_count_miss < mrp->test_max_miss) {
289 mrp->test_count_miss++;
290 } else {
291 /* Notify that the ring is open only if the ring state is
292 * closed, otherwise it would continue to notify at every
293 * interval.
294 * Also notify that the ring is open when the node has the
295 * role MRA and behaves as MRC. The reason is that the
296 * userspace needs to know when the MRM stopped sending
297 * MRP_Test frames so that the current node to try to take
298 * the role of a MRM.
299 */
300 if (mrp->ring_state == BR_MRP_RING_STATE_CLOSED ||
301 mrp->test_monitor)
302 notify_open = true;
303 }
304
305 rcu_read_lock();
306
307 p = rcu_dereference(mrp->p_port);
308 if (p) {
309 if (!mrp->test_monitor) {
310 skb = br_mrp_alloc_test_skb(mrp, p,
311 BR_MRP_PORT_ROLE_PRIMARY);
312 if (!skb)
313 goto out;
314
315 skb_reset_network_header(skb);
316 dev_queue_xmit(skb);
317 }
318
319 if (notify_open && !mrp->ring_role_offloaded)
320 br_mrp_ring_port_open(p->dev, true);
321 }
322
323 p = rcu_dereference(mrp->s_port);
324 if (p) {
325 if (!mrp->test_monitor) {
326 skb = br_mrp_alloc_test_skb(mrp, p,
327 BR_MRP_PORT_ROLE_SECONDARY);
328 if (!skb)
329 goto out;
330
331 skb_reset_network_header(skb);
332 dev_queue_xmit(skb);
333 }
334
335 if (notify_open && !mrp->ring_role_offloaded)
336 br_mrp_ring_port_open(p->dev, true);
337 }
338
339 out:
340 rcu_read_unlock();
341
342 queue_delayed_work(system_percpu_wq, &mrp->test_work,
343 usecs_to_jiffies(mrp->test_interval));
344 }
345
346 /* This function is continuously called when the node has the interconnect role
347 * MIM. It would generate interconnect test frames and will send them on all 3
348 * ports. But will also check if it stop receiving interconnect test frames.
349 */
br_mrp_in_test_work_expired(struct work_struct * work)350 static void br_mrp_in_test_work_expired(struct work_struct *work)
351 {
352 struct delayed_work *del_work = to_delayed_work(work);
353 struct br_mrp *mrp = container_of(del_work, struct br_mrp, in_test_work);
354 struct net_bridge_port *p;
355 bool notify_open = false;
356 struct sk_buff *skb;
357
358 if (time_before_eq(mrp->in_test_end, jiffies))
359 return;
360
361 if (mrp->in_test_count_miss < mrp->in_test_max_miss) {
362 mrp->in_test_count_miss++;
363 } else {
364 /* Notify that the interconnect ring is open only if the
365 * interconnect ring state is closed, otherwise it would
366 * continue to notify at every interval.
367 */
368 if (mrp->in_state == BR_MRP_IN_STATE_CLOSED)
369 notify_open = true;
370 }
371
372 rcu_read_lock();
373
374 p = rcu_dereference(mrp->p_port);
375 if (p) {
376 skb = br_mrp_alloc_in_test_skb(mrp, p,
377 BR_MRP_PORT_ROLE_PRIMARY);
378 if (!skb)
379 goto out;
380
381 skb_reset_network_header(skb);
382 dev_queue_xmit(skb);
383
384 if (notify_open && !mrp->in_role_offloaded)
385 br_mrp_in_port_open(p->dev, true);
386 }
387
388 p = rcu_dereference(mrp->s_port);
389 if (p) {
390 skb = br_mrp_alloc_in_test_skb(mrp, p,
391 BR_MRP_PORT_ROLE_SECONDARY);
392 if (!skb)
393 goto out;
394
395 skb_reset_network_header(skb);
396 dev_queue_xmit(skb);
397
398 if (notify_open && !mrp->in_role_offloaded)
399 br_mrp_in_port_open(p->dev, true);
400 }
401
402 p = rcu_dereference(mrp->i_port);
403 if (p) {
404 skb = br_mrp_alloc_in_test_skb(mrp, p,
405 BR_MRP_PORT_ROLE_INTER);
406 if (!skb)
407 goto out;
408
409 skb_reset_network_header(skb);
410 dev_queue_xmit(skb);
411
412 if (notify_open && !mrp->in_role_offloaded)
413 br_mrp_in_port_open(p->dev, true);
414 }
415
416 out:
417 rcu_read_unlock();
418
419 queue_delayed_work(system_percpu_wq, &mrp->in_test_work,
420 usecs_to_jiffies(mrp->in_test_interval));
421 }
422
423 /* Deletes the MRP instance.
424 * note: called under rtnl_lock
425 */
br_mrp_del_impl(struct net_bridge * br,struct br_mrp * mrp)426 static void br_mrp_del_impl(struct net_bridge *br, struct br_mrp *mrp)
427 {
428 struct net_bridge_port *p;
429 u8 state;
430
431 /* Stop sending MRP_Test frames */
432 cancel_delayed_work_sync(&mrp->test_work);
433 br_mrp_switchdev_send_ring_test(br, mrp, 0, 0, 0, 0);
434
435 /* Stop sending MRP_InTest frames if has an interconnect role */
436 cancel_delayed_work_sync(&mrp->in_test_work);
437 br_mrp_switchdev_send_in_test(br, mrp, 0, 0, 0);
438
439 /* Disable the roles */
440 br_mrp_switchdev_set_ring_role(br, mrp, BR_MRP_RING_ROLE_DISABLED);
441 p = rtnl_dereference(mrp->i_port);
442 if (p)
443 br_mrp_switchdev_set_in_role(br, mrp, mrp->in_id, mrp->ring_id,
444 BR_MRP_IN_ROLE_DISABLED);
445
446 br_mrp_switchdev_del(br, mrp);
447
448 /* Reset the ports */
449 p = rtnl_dereference(mrp->p_port);
450 if (p) {
451 spin_lock_bh(&br->lock);
452 state = netif_running(br->dev) ?
453 BR_STATE_FORWARDING : BR_STATE_DISABLED;
454 p->state = state;
455 clear_bit(BR_MRP_AWARE_BIT, &p->flags);
456 spin_unlock_bh(&br->lock);
457 br_mrp_port_switchdev_set_state(p, state);
458 rcu_assign_pointer(mrp->p_port, NULL);
459 }
460
461 p = rtnl_dereference(mrp->s_port);
462 if (p) {
463 spin_lock_bh(&br->lock);
464 state = netif_running(br->dev) ?
465 BR_STATE_FORWARDING : BR_STATE_DISABLED;
466 p->state = state;
467 clear_bit(BR_MRP_AWARE_BIT, &p->flags);
468 spin_unlock_bh(&br->lock);
469 br_mrp_port_switchdev_set_state(p, state);
470 rcu_assign_pointer(mrp->s_port, NULL);
471 }
472
473 p = rtnl_dereference(mrp->i_port);
474 if (p) {
475 spin_lock_bh(&br->lock);
476 state = netif_running(br->dev) ?
477 BR_STATE_FORWARDING : BR_STATE_DISABLED;
478 p->state = state;
479 clear_bit(BR_MRP_AWARE_BIT, &p->flags);
480 spin_unlock_bh(&br->lock);
481 br_mrp_port_switchdev_set_state(p, state);
482 rcu_assign_pointer(mrp->i_port, NULL);
483 }
484
485 hlist_del_rcu(&mrp->list);
486 kfree_rcu(mrp, rcu);
487
488 if (hlist_empty(&br->mrp_list))
489 br_del_frame(br, &mrp_frame_type);
490 }
491
492 /* Adds a new MRP instance.
493 * note: called under rtnl_lock
494 */
br_mrp_add(struct net_bridge * br,struct br_mrp_instance * instance)495 int br_mrp_add(struct net_bridge *br, struct br_mrp_instance *instance)
496 {
497 struct net_bridge_port *p;
498 struct br_mrp *mrp;
499 int err;
500
501 /* If the ring exists, it is not possible to create another one with the
502 * same ring_id
503 */
504 mrp = br_mrp_find_id(br, instance->ring_id);
505 if (mrp)
506 return -EINVAL;
507
508 if (!br_mrp_get_port(br, instance->p_ifindex) ||
509 !br_mrp_get_port(br, instance->s_ifindex))
510 return -EINVAL;
511
512 /* It is not possible to have the same port part of multiple rings */
513 if (!br_mrp_unique_ifindex(br, instance->p_ifindex) ||
514 !br_mrp_unique_ifindex(br, instance->s_ifindex))
515 return -EINVAL;
516
517 mrp = kzalloc_obj(*mrp);
518 if (!mrp)
519 return -ENOMEM;
520
521 mrp->ring_id = instance->ring_id;
522 mrp->prio = instance->prio;
523
524 p = br_mrp_get_port(br, instance->p_ifindex);
525 spin_lock_bh(&br->lock);
526 p->state = BR_STATE_FORWARDING;
527 set_bit(BR_MRP_AWARE_BIT, &p->flags);
528 spin_unlock_bh(&br->lock);
529 rcu_assign_pointer(mrp->p_port, p);
530
531 p = br_mrp_get_port(br, instance->s_ifindex);
532 spin_lock_bh(&br->lock);
533 p->state = BR_STATE_FORWARDING;
534 set_bit(BR_MRP_AWARE_BIT, &p->flags);
535 spin_unlock_bh(&br->lock);
536 rcu_assign_pointer(mrp->s_port, p);
537
538 if (hlist_empty(&br->mrp_list))
539 br_add_frame(br, &mrp_frame_type);
540
541 INIT_DELAYED_WORK(&mrp->test_work, br_mrp_test_work_expired);
542 INIT_DELAYED_WORK(&mrp->in_test_work, br_mrp_in_test_work_expired);
543 hlist_add_tail_rcu(&mrp->list, &br->mrp_list);
544
545 err = br_mrp_switchdev_add(br, mrp);
546 if (err)
547 goto delete_mrp;
548
549 return 0;
550
551 delete_mrp:
552 br_mrp_del_impl(br, mrp);
553
554 return err;
555 }
556
557 /* Deletes the MRP instance from which the port is part of
558 * note: called under rtnl_lock
559 */
br_mrp_port_del(struct net_bridge * br,struct net_bridge_port * p)560 void br_mrp_port_del(struct net_bridge *br, struct net_bridge_port *p)
561 {
562 struct br_mrp *mrp = br_mrp_find_port(br, p);
563
564 /* If the port is not part of a MRP instance just bail out */
565 if (!mrp)
566 return;
567
568 br_mrp_del_impl(br, mrp);
569 }
570
571 /* Deletes existing MRP instance based on ring_id
572 * note: called under rtnl_lock
573 */
br_mrp_del(struct net_bridge * br,struct br_mrp_instance * instance)574 int br_mrp_del(struct net_bridge *br, struct br_mrp_instance *instance)
575 {
576 struct br_mrp *mrp = br_mrp_find_id(br, instance->ring_id);
577
578 if (!mrp)
579 return -EINVAL;
580
581 br_mrp_del_impl(br, mrp);
582
583 return 0;
584 }
585
586 /* Set port state, port state can be forwarding, blocked or disabled
587 * note: already called with rtnl_lock
588 */
br_mrp_set_port_state(struct net_bridge_port * p,enum br_mrp_port_state_type state)589 int br_mrp_set_port_state(struct net_bridge_port *p,
590 enum br_mrp_port_state_type state)
591 {
592 u32 port_state;
593
594 if (!p || !test_bit(BR_MRP_AWARE_BIT, &p->flags))
595 return -EINVAL;
596
597 spin_lock_bh(&p->br->lock);
598
599 if (state == BR_MRP_PORT_STATE_FORWARDING)
600 port_state = BR_STATE_FORWARDING;
601 else
602 port_state = BR_STATE_BLOCKING;
603
604 p->state = port_state;
605 spin_unlock_bh(&p->br->lock);
606
607 br_mrp_port_switchdev_set_state(p, port_state);
608
609 return 0;
610 }
611
612 /* Set port role, port role can be primary or secondary
613 * note: already called with rtnl_lock
614 */
br_mrp_set_port_role(struct net_bridge_port * p,enum br_mrp_port_role_type role)615 int br_mrp_set_port_role(struct net_bridge_port *p,
616 enum br_mrp_port_role_type role)
617 {
618 struct br_mrp *mrp;
619
620 if (!p || !test_bit(BR_MRP_AWARE_BIT, &p->flags))
621 return -EINVAL;
622
623 mrp = br_mrp_find_port(p->br, p);
624
625 if (!mrp)
626 return -EINVAL;
627
628 switch (role) {
629 case BR_MRP_PORT_ROLE_PRIMARY:
630 rcu_assign_pointer(mrp->p_port, p);
631 break;
632 case BR_MRP_PORT_ROLE_SECONDARY:
633 rcu_assign_pointer(mrp->s_port, p);
634 break;
635 default:
636 return -EINVAL;
637 }
638
639 br_mrp_port_switchdev_set_role(p, role);
640
641 return 0;
642 }
643
644 /* Set ring state, ring state can be only Open or Closed
645 * note: already called with rtnl_lock
646 */
br_mrp_set_ring_state(struct net_bridge * br,struct br_mrp_ring_state * state)647 int br_mrp_set_ring_state(struct net_bridge *br,
648 struct br_mrp_ring_state *state)
649 {
650 struct br_mrp *mrp = br_mrp_find_id(br, state->ring_id);
651
652 if (!mrp)
653 return -EINVAL;
654
655 if (mrp->ring_state != state->ring_state)
656 mrp->ring_transitions++;
657
658 mrp->ring_state = state->ring_state;
659
660 br_mrp_switchdev_set_ring_state(br, mrp, state->ring_state);
661
662 return 0;
663 }
664
665 /* Set ring role, ring role can be only MRM(Media Redundancy Manager) or
666 * MRC(Media Redundancy Client).
667 * note: already called with rtnl_lock
668 */
br_mrp_set_ring_role(struct net_bridge * br,struct br_mrp_ring_role * role)669 int br_mrp_set_ring_role(struct net_bridge *br,
670 struct br_mrp_ring_role *role)
671 {
672 struct br_mrp *mrp = br_mrp_find_id(br, role->ring_id);
673 enum br_mrp_hw_support support;
674
675 if (!mrp)
676 return -EINVAL;
677
678 mrp->ring_role = role->ring_role;
679
680 /* If there is an error just bailed out */
681 support = br_mrp_switchdev_set_ring_role(br, mrp, role->ring_role);
682 if (support == BR_MRP_NONE)
683 return -EOPNOTSUPP;
684
685 /* Now detect if the HW actually applied the role or not. If the HW
686 * applied the role it means that the SW will not to do those operations
687 * anymore. For example if the role ir MRM then the HW will notify the
688 * SW when ring is open, but if the is not pushed to the HW the SW will
689 * need to detect when the ring is open
690 */
691 mrp->ring_role_offloaded = support == BR_MRP_SW ? 0 : 1;
692
693 return 0;
694 }
695
696 /* Start to generate or monitor MRP test frames, the frames are generated by
697 * HW and if it fails, they are generated by the SW.
698 * note: already called with rtnl_lock
699 */
br_mrp_start_test(struct net_bridge * br,struct br_mrp_start_test * test)700 int br_mrp_start_test(struct net_bridge *br,
701 struct br_mrp_start_test *test)
702 {
703 struct br_mrp *mrp = br_mrp_find_id(br, test->ring_id);
704 enum br_mrp_hw_support support;
705
706 if (!mrp)
707 return -EINVAL;
708
709 /* Try to push it to the HW and if it fails then continue with SW
710 * implementation and if that also fails then return error.
711 */
712 support = br_mrp_switchdev_send_ring_test(br, mrp, test->interval,
713 test->max_miss, test->period,
714 test->monitor);
715 if (support == BR_MRP_NONE)
716 return -EOPNOTSUPP;
717
718 if (support == BR_MRP_HW)
719 return 0;
720
721 mrp->test_interval = test->interval;
722 mrp->test_end = jiffies + usecs_to_jiffies(test->period);
723 mrp->test_max_miss = test->max_miss;
724 mrp->test_monitor = test->monitor;
725 mrp->test_count_miss = 0;
726 queue_delayed_work(system_percpu_wq, &mrp->test_work,
727 usecs_to_jiffies(test->interval));
728
729 return 0;
730 }
731
732 /* Set in state, int state can be only Open or Closed
733 * note: already called with rtnl_lock
734 */
br_mrp_set_in_state(struct net_bridge * br,struct br_mrp_in_state * state)735 int br_mrp_set_in_state(struct net_bridge *br, struct br_mrp_in_state *state)
736 {
737 struct br_mrp *mrp = br_mrp_find_in_id(br, state->in_id);
738
739 if (!mrp)
740 return -EINVAL;
741
742 if (mrp->in_state != state->in_state)
743 mrp->in_transitions++;
744
745 mrp->in_state = state->in_state;
746
747 br_mrp_switchdev_set_in_state(br, mrp, state->in_state);
748
749 return 0;
750 }
751
752 /* Set in role, in role can be only MIM(Media Interconnection Manager) or
753 * MIC(Media Interconnection Client).
754 * note: already called with rtnl_lock
755 */
br_mrp_set_in_role(struct net_bridge * br,struct br_mrp_in_role * role)756 int br_mrp_set_in_role(struct net_bridge *br, struct br_mrp_in_role *role)
757 {
758 struct br_mrp *mrp = br_mrp_find_id(br, role->ring_id);
759 enum br_mrp_hw_support support;
760 struct net_bridge_port *p;
761
762 if (!mrp)
763 return -EINVAL;
764
765 if (!br_mrp_get_port(br, role->i_ifindex))
766 return -EINVAL;
767
768 if (role->in_role == BR_MRP_IN_ROLE_DISABLED) {
769 u8 state;
770
771 /* It is not allowed to disable a port that doesn't exist */
772 p = rtnl_dereference(mrp->i_port);
773 if (!p)
774 return -EINVAL;
775
776 /* Stop the generating MRP_InTest frames */
777 cancel_delayed_work_sync(&mrp->in_test_work);
778 br_mrp_switchdev_send_in_test(br, mrp, 0, 0, 0);
779
780 /* Remove the port */
781 spin_lock_bh(&br->lock);
782 state = netif_running(br->dev) ?
783 BR_STATE_FORWARDING : BR_STATE_DISABLED;
784 p->state = state;
785 clear_bit(BR_MRP_AWARE_BIT, &p->flags);
786 spin_unlock_bh(&br->lock);
787 br_mrp_port_switchdev_set_state(p, state);
788 rcu_assign_pointer(mrp->i_port, NULL);
789
790 mrp->in_role = role->in_role;
791 mrp->in_id = 0;
792
793 return 0;
794 }
795
796 /* It is not possible to have the same port part of multiple rings */
797 if (!br_mrp_unique_ifindex(br, role->i_ifindex))
798 return -EINVAL;
799
800 /* It is not allowed to set a different interconnect port if the mrp
801 * instance has already one. First it needs to be disabled and after
802 * that set the new port
803 */
804 if (rcu_access_pointer(mrp->i_port))
805 return -EINVAL;
806
807 p = br_mrp_get_port(br, role->i_ifindex);
808 spin_lock_bh(&br->lock);
809 p->state = BR_STATE_FORWARDING;
810 set_bit(BR_MRP_AWARE_BIT, &p->flags);
811 spin_unlock_bh(&br->lock);
812 rcu_assign_pointer(mrp->i_port, p);
813
814 mrp->in_role = role->in_role;
815 mrp->in_id = role->in_id;
816
817 /* If there is an error just bailed out */
818 support = br_mrp_switchdev_set_in_role(br, mrp, role->in_id,
819 role->ring_id, role->in_role);
820 if (support == BR_MRP_NONE)
821 return -EOPNOTSUPP;
822
823 /* Now detect if the HW actually applied the role or not. If the HW
824 * applied the role it means that the SW will not to do those operations
825 * anymore. For example if the role is MIM then the HW will notify the
826 * SW when interconnect ring is open, but if the is not pushed to the HW
827 * the SW will need to detect when the interconnect ring is open.
828 */
829 mrp->in_role_offloaded = support == BR_MRP_SW ? 0 : 1;
830
831 return 0;
832 }
833
834 /* Start to generate MRP_InTest frames, the frames are generated by
835 * HW and if it fails, they are generated by the SW.
836 * note: already called with rtnl_lock
837 */
br_mrp_start_in_test(struct net_bridge * br,struct br_mrp_start_in_test * in_test)838 int br_mrp_start_in_test(struct net_bridge *br,
839 struct br_mrp_start_in_test *in_test)
840 {
841 struct br_mrp *mrp = br_mrp_find_in_id(br, in_test->in_id);
842 enum br_mrp_hw_support support;
843
844 if (!mrp)
845 return -EINVAL;
846
847 if (mrp->in_role != BR_MRP_IN_ROLE_MIM)
848 return -EINVAL;
849
850 /* Try to push it to the HW and if it fails then continue with SW
851 * implementation and if that also fails then return error.
852 */
853 support = br_mrp_switchdev_send_in_test(br, mrp, in_test->interval,
854 in_test->max_miss,
855 in_test->period);
856 if (support == BR_MRP_NONE)
857 return -EOPNOTSUPP;
858
859 if (support == BR_MRP_HW)
860 return 0;
861
862 mrp->in_test_interval = in_test->interval;
863 mrp->in_test_end = jiffies + usecs_to_jiffies(in_test->period);
864 mrp->in_test_max_miss = in_test->max_miss;
865 mrp->in_test_count_miss = 0;
866 queue_delayed_work(system_percpu_wq, &mrp->in_test_work,
867 usecs_to_jiffies(in_test->interval));
868
869 return 0;
870 }
871
872 /* Determine if the frame type is a ring frame */
br_mrp_ring_frame(struct sk_buff * skb)873 static bool br_mrp_ring_frame(struct sk_buff *skb)
874 {
875 const struct br_mrp_tlv_hdr *hdr;
876 struct br_mrp_tlv_hdr _hdr;
877
878 hdr = skb_header_pointer(skb, sizeof(uint16_t), sizeof(_hdr), &_hdr);
879 if (!hdr)
880 return false;
881
882 if (hdr->type == BR_MRP_TLV_HEADER_RING_TEST ||
883 hdr->type == BR_MRP_TLV_HEADER_RING_TOPO ||
884 hdr->type == BR_MRP_TLV_HEADER_RING_LINK_DOWN ||
885 hdr->type == BR_MRP_TLV_HEADER_RING_LINK_UP ||
886 hdr->type == BR_MRP_TLV_HEADER_OPTION)
887 return true;
888
889 return false;
890 }
891
892 /* Determine if the frame type is an interconnect frame */
br_mrp_in_frame(struct sk_buff * skb)893 static bool br_mrp_in_frame(struct sk_buff *skb)
894 {
895 const struct br_mrp_tlv_hdr *hdr;
896 struct br_mrp_tlv_hdr _hdr;
897
898 hdr = skb_header_pointer(skb, sizeof(uint16_t), sizeof(_hdr), &_hdr);
899 if (!hdr)
900 return false;
901
902 if (hdr->type == BR_MRP_TLV_HEADER_IN_TEST ||
903 hdr->type == BR_MRP_TLV_HEADER_IN_TOPO ||
904 hdr->type == BR_MRP_TLV_HEADER_IN_LINK_DOWN ||
905 hdr->type == BR_MRP_TLV_HEADER_IN_LINK_UP ||
906 hdr->type == BR_MRP_TLV_HEADER_IN_LINK_STATUS)
907 return true;
908
909 return false;
910 }
911
912 /* Process only MRP Test frame. All the other MRP frames are processed by
913 * userspace application
914 * note: already called with rcu_read_lock
915 */
br_mrp_mrm_process(struct br_mrp * mrp,struct net_bridge_port * port,struct sk_buff * skb)916 static void br_mrp_mrm_process(struct br_mrp *mrp, struct net_bridge_port *port,
917 struct sk_buff *skb)
918 {
919 const struct br_mrp_tlv_hdr *hdr;
920 struct br_mrp_tlv_hdr _hdr;
921
922 /* Each MRP header starts with a version field which is 16 bits.
923 * Therefore skip the version and get directly the TLV header.
924 */
925 hdr = skb_header_pointer(skb, sizeof(uint16_t), sizeof(_hdr), &_hdr);
926 if (!hdr)
927 return;
928
929 if (hdr->type != BR_MRP_TLV_HEADER_RING_TEST)
930 return;
931
932 mrp->test_count_miss = 0;
933
934 /* Notify the userspace that the ring is closed only when the ring is
935 * not closed
936 */
937 if (mrp->ring_state != BR_MRP_RING_STATE_CLOSED)
938 br_mrp_ring_port_open(port->dev, false);
939 }
940
941 /* Determine if the test hdr has a better priority than the node */
br_mrp_test_better_than_own(struct br_mrp * mrp,struct net_bridge * br,const struct br_mrp_ring_test_hdr * hdr)942 static bool br_mrp_test_better_than_own(struct br_mrp *mrp,
943 struct net_bridge *br,
944 const struct br_mrp_ring_test_hdr *hdr)
945 {
946 u16 prio = be16_to_cpu(hdr->prio);
947
948 if (prio < mrp->prio ||
949 (prio == mrp->prio &&
950 ether_addr_to_u64(hdr->sa) < ether_addr_to_u64(br->dev->dev_addr)))
951 return true;
952
953 return false;
954 }
955
956 /* Process only MRP Test frame. All the other MRP frames are processed by
957 * userspace application
958 * note: already called with rcu_read_lock
959 */
br_mrp_mra_process(struct br_mrp * mrp,struct net_bridge * br,struct net_bridge_port * port,struct sk_buff * skb)960 static void br_mrp_mra_process(struct br_mrp *mrp, struct net_bridge *br,
961 struct net_bridge_port *port,
962 struct sk_buff *skb)
963 {
964 const struct br_mrp_ring_test_hdr *test_hdr;
965 struct br_mrp_ring_test_hdr _test_hdr;
966 const struct br_mrp_tlv_hdr *hdr;
967 struct br_mrp_tlv_hdr _hdr;
968
969 /* Each MRP header starts with a version field which is 16 bits.
970 * Therefore skip the version and get directly the TLV header.
971 */
972 hdr = skb_header_pointer(skb, sizeof(uint16_t), sizeof(_hdr), &_hdr);
973 if (!hdr)
974 return;
975
976 if (hdr->type != BR_MRP_TLV_HEADER_RING_TEST)
977 return;
978
979 test_hdr = skb_header_pointer(skb, sizeof(uint16_t) + sizeof(_hdr),
980 sizeof(_test_hdr), &_test_hdr);
981 if (!test_hdr)
982 return;
983
984 /* Only frames that have a better priority than the node will
985 * clear the miss counter because otherwise the node will need to behave
986 * as MRM.
987 */
988 if (br_mrp_test_better_than_own(mrp, br, test_hdr))
989 mrp->test_count_miss = 0;
990 }
991
992 /* Process only MRP InTest frame. All the other MRP frames are processed by
993 * userspace application
994 * note: already called with rcu_read_lock
995 */
br_mrp_mim_process(struct br_mrp * mrp,struct net_bridge_port * port,struct sk_buff * skb)996 static bool br_mrp_mim_process(struct br_mrp *mrp, struct net_bridge_port *port,
997 struct sk_buff *skb)
998 {
999 const struct br_mrp_in_test_hdr *in_hdr;
1000 struct br_mrp_in_test_hdr _in_hdr;
1001 const struct br_mrp_tlv_hdr *hdr;
1002 struct br_mrp_tlv_hdr _hdr;
1003
1004 /* Each MRP header starts with a version field which is 16 bits.
1005 * Therefore skip the version and get directly the TLV header.
1006 */
1007 hdr = skb_header_pointer(skb, sizeof(uint16_t), sizeof(_hdr), &_hdr);
1008 if (!hdr)
1009 return false;
1010
1011 /* The check for InTest frame type was already done */
1012 in_hdr = skb_header_pointer(skb, sizeof(uint16_t) + sizeof(_hdr),
1013 sizeof(_in_hdr), &_in_hdr);
1014 if (!in_hdr)
1015 return false;
1016
1017 /* It needs to process only it's own InTest frames. */
1018 if (mrp->in_id != ntohs(in_hdr->id))
1019 return false;
1020
1021 mrp->in_test_count_miss = 0;
1022
1023 /* Notify the userspace that the ring is closed only when the ring is
1024 * not closed
1025 */
1026 if (mrp->in_state != BR_MRP_IN_STATE_CLOSED)
1027 br_mrp_in_port_open(port->dev, false);
1028
1029 return true;
1030 }
1031
1032 /* Get the MRP frame type
1033 * note: already called with rcu_read_lock
1034 */
br_mrp_get_frame_type(struct sk_buff * skb)1035 static u8 br_mrp_get_frame_type(struct sk_buff *skb)
1036 {
1037 const struct br_mrp_tlv_hdr *hdr;
1038 struct br_mrp_tlv_hdr _hdr;
1039
1040 /* Each MRP header starts with a version field which is 16 bits.
1041 * Therefore skip the version and get directly the TLV header.
1042 */
1043 hdr = skb_header_pointer(skb, sizeof(uint16_t), sizeof(_hdr), &_hdr);
1044 if (!hdr)
1045 return 0xff;
1046
1047 return hdr->type;
1048 }
1049
br_mrp_mrm_behaviour(struct br_mrp * mrp)1050 static bool br_mrp_mrm_behaviour(struct br_mrp *mrp)
1051 {
1052 if (mrp->ring_role == BR_MRP_RING_ROLE_MRM ||
1053 (mrp->ring_role == BR_MRP_RING_ROLE_MRA && !mrp->test_monitor))
1054 return true;
1055
1056 return false;
1057 }
1058
br_mrp_mrc_behaviour(struct br_mrp * mrp)1059 static bool br_mrp_mrc_behaviour(struct br_mrp *mrp)
1060 {
1061 if (mrp->ring_role == BR_MRP_RING_ROLE_MRC ||
1062 (mrp->ring_role == BR_MRP_RING_ROLE_MRA && mrp->test_monitor))
1063 return true;
1064
1065 return false;
1066 }
1067
1068 /* This will just forward the frame to the other mrp ring ports, depending on
1069 * the frame type, ring role and interconnect role
1070 * note: already called with rcu_read_lock
1071 */
br_mrp_rcv(struct net_bridge_port * p,struct sk_buff * skb,struct net_device * dev)1072 static int br_mrp_rcv(struct net_bridge_port *p,
1073 struct sk_buff *skb, struct net_device *dev)
1074 {
1075 struct net_bridge_port *p_port, *s_port, *i_port = NULL;
1076 struct net_bridge_port *p_dst, *s_dst, *i_dst = NULL;
1077 struct net_bridge *br;
1078 struct br_mrp *mrp;
1079
1080 /* If port is disabled don't accept any frames */
1081 if (p->state == BR_STATE_DISABLED)
1082 return 0;
1083
1084 br = p->br;
1085 mrp = br_mrp_find_port(br, p);
1086 if (unlikely(!mrp))
1087 return 0;
1088
1089 p_port = rcu_dereference(mrp->p_port);
1090 if (!p_port)
1091 return 0;
1092 p_dst = p_port;
1093
1094 s_port = rcu_dereference(mrp->s_port);
1095 if (!s_port)
1096 return 0;
1097 s_dst = s_port;
1098
1099 /* If the frame is a ring frame then it is not required to check the
1100 * interconnect role and ports to process or forward the frame
1101 */
1102 if (br_mrp_ring_frame(skb)) {
1103 /* If the role is MRM then don't forward the frames */
1104 if (mrp->ring_role == BR_MRP_RING_ROLE_MRM) {
1105 br_mrp_mrm_process(mrp, p, skb);
1106 goto no_forward;
1107 }
1108
1109 /* If the role is MRA then don't forward the frames if it
1110 * behaves as MRM node
1111 */
1112 if (mrp->ring_role == BR_MRP_RING_ROLE_MRA) {
1113 if (!mrp->test_monitor) {
1114 br_mrp_mrm_process(mrp, p, skb);
1115 goto no_forward;
1116 }
1117
1118 br_mrp_mra_process(mrp, br, p, skb);
1119 }
1120
1121 goto forward;
1122 }
1123
1124 if (br_mrp_in_frame(skb)) {
1125 u8 in_type = br_mrp_get_frame_type(skb);
1126
1127 i_port = rcu_dereference(mrp->i_port);
1128 i_dst = i_port;
1129
1130 /* If the ring port is in block state it should not forward
1131 * In_Test frames
1132 */
1133 if (br_mrp_is_ring_port(p_port, s_port, p) &&
1134 p->state == BR_STATE_BLOCKING &&
1135 in_type == BR_MRP_TLV_HEADER_IN_TEST)
1136 goto no_forward;
1137
1138 /* Nodes that behaves as MRM needs to stop forwarding the
1139 * frames in case the ring is closed, otherwise will be a loop.
1140 * In this case the frame is no forward between the ring ports.
1141 */
1142 if (br_mrp_mrm_behaviour(mrp) &&
1143 br_mrp_is_ring_port(p_port, s_port, p) &&
1144 (s_port->state != BR_STATE_FORWARDING ||
1145 p_port->state != BR_STATE_FORWARDING)) {
1146 p_dst = NULL;
1147 s_dst = NULL;
1148 }
1149
1150 /* A node that behaves as MRC and doesn't have a interconnect
1151 * role then it should forward all frames between the ring ports
1152 * because it doesn't have an interconnect port
1153 */
1154 if (br_mrp_mrc_behaviour(mrp) &&
1155 mrp->in_role == BR_MRP_IN_ROLE_DISABLED)
1156 goto forward;
1157
1158 if (mrp->in_role == BR_MRP_IN_ROLE_MIM) {
1159 if (in_type == BR_MRP_TLV_HEADER_IN_TEST) {
1160 /* MIM should not forward it's own InTest
1161 * frames
1162 */
1163 if (br_mrp_mim_process(mrp, p, skb)) {
1164 goto no_forward;
1165 } else {
1166 if (br_mrp_is_ring_port(p_port, s_port,
1167 p))
1168 i_dst = NULL;
1169
1170 if (br_mrp_is_in_port(i_port, p))
1171 goto no_forward;
1172 }
1173 } else {
1174 /* MIM should forward IntLinkChange/Status and
1175 * IntTopoChange between ring ports but MIM
1176 * should not forward IntLinkChange/Status and
1177 * IntTopoChange if the frame was received at
1178 * the interconnect port
1179 */
1180 if (br_mrp_is_ring_port(p_port, s_port, p))
1181 i_dst = NULL;
1182
1183 if (br_mrp_is_in_port(i_port, p))
1184 goto no_forward;
1185 }
1186 }
1187
1188 if (mrp->in_role == BR_MRP_IN_ROLE_MIC) {
1189 /* MIC should forward InTest frames on all ports
1190 * regardless of the received port
1191 */
1192 if (in_type == BR_MRP_TLV_HEADER_IN_TEST)
1193 goto forward;
1194
1195 /* MIC should forward IntLinkChange frames only if they
1196 * are received on ring ports to all the ports
1197 */
1198 if (br_mrp_is_ring_port(p_port, s_port, p) &&
1199 (in_type == BR_MRP_TLV_HEADER_IN_LINK_UP ||
1200 in_type == BR_MRP_TLV_HEADER_IN_LINK_DOWN))
1201 goto forward;
1202
1203 /* MIC should forward IntLinkStatus frames only to
1204 * interconnect port if it was received on a ring port.
1205 * If it is received on interconnect port then, it
1206 * should be forward on both ring ports
1207 */
1208 if (br_mrp_is_ring_port(p_port, s_port, p) &&
1209 in_type == BR_MRP_TLV_HEADER_IN_LINK_STATUS) {
1210 p_dst = NULL;
1211 s_dst = NULL;
1212 }
1213
1214 /* Should forward the InTopo frames only between the
1215 * ring ports
1216 */
1217 if (in_type == BR_MRP_TLV_HEADER_IN_TOPO) {
1218 i_dst = NULL;
1219 goto forward;
1220 }
1221
1222 /* In all the other cases don't forward the frames */
1223 goto no_forward;
1224 }
1225 }
1226
1227 forward:
1228 if (p_dst)
1229 br_forward(p_dst, skb, true, false);
1230 if (s_dst)
1231 br_forward(s_dst, skb, true, false);
1232 if (i_dst)
1233 br_forward(i_dst, skb, true, false);
1234
1235 no_forward:
1236 return 1;
1237 }
1238
1239 /* Check if the frame was received on a port that is part of MRP ring
1240 * and if the frame has MRP eth. In that case process the frame otherwise do
1241 * normal forwarding.
1242 * note: already called with rcu_read_lock
1243 */
br_mrp_process(struct net_bridge_port * p,struct sk_buff * skb)1244 static int br_mrp_process(struct net_bridge_port *p, struct sk_buff *skb)
1245 {
1246 /* If there is no MRP instance do normal forwarding */
1247 if (likely(!test_bit(BR_MRP_AWARE_BIT, &p->flags)))
1248 goto out;
1249
1250 return br_mrp_rcv(p, skb, p->dev);
1251 out:
1252 return 0;
1253 }
1254
br_mrp_enabled(struct net_bridge * br)1255 bool br_mrp_enabled(struct net_bridge *br)
1256 {
1257 return !hlist_empty(&br->mrp_list);
1258 }
1259