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