19a9f26e8SHoratiu Vultur // SPDX-License-Identifier: GPL-2.0-or-later
29a9f26e8SHoratiu Vultur
39a9f26e8SHoratiu Vultur #include <linux/mrp_bridge.h>
49a9f26e8SHoratiu Vultur #include "br_private_mrp.h"
59a9f26e8SHoratiu Vultur
69a9f26e8SHoratiu Vultur static const u8 mrp_test_dmac[ETH_ALEN] = { 0x1, 0x15, 0x4e, 0x0, 0x0, 0x1 };
7537ed567SHoratiu Vultur static const u8 mrp_in_test_dmac[ETH_ALEN] = { 0x1, 0x15, 0x4e, 0x0, 0x0, 0x3 };
8537ed567SHoratiu Vultur
990c628ddSHenrik Bjoernlund static int br_mrp_process(struct net_bridge_port *p, struct sk_buff *skb);
1090c628ddSHenrik Bjoernlund
1190c628ddSHenrik Bjoernlund static struct br_frame_type mrp_frame_type __read_mostly = {
1290c628ddSHenrik Bjoernlund .type = cpu_to_be16(ETH_P_MRP),
1390c628ddSHenrik Bjoernlund .frame_handler = br_mrp_process,
1490c628ddSHenrik Bjoernlund };
1590c628ddSHenrik Bjoernlund
br_mrp_is_ring_port(struct net_bridge_port * p_port,struct net_bridge_port * s_port,struct net_bridge_port * port)16537ed567SHoratiu Vultur static bool br_mrp_is_ring_port(struct net_bridge_port *p_port,
17537ed567SHoratiu Vultur struct net_bridge_port *s_port,
18537ed567SHoratiu Vultur struct net_bridge_port *port)
19537ed567SHoratiu Vultur {
20537ed567SHoratiu Vultur if (port == p_port ||
21537ed567SHoratiu Vultur port == s_port)
22537ed567SHoratiu Vultur return true;
23537ed567SHoratiu Vultur
24537ed567SHoratiu Vultur return false;
25537ed567SHoratiu Vultur }
26537ed567SHoratiu Vultur
br_mrp_is_in_port(struct net_bridge_port * i_port,struct net_bridge_port * port)27537ed567SHoratiu Vultur static bool br_mrp_is_in_port(struct net_bridge_port *i_port,
28537ed567SHoratiu Vultur struct net_bridge_port *port)
29537ed567SHoratiu Vultur {
30537ed567SHoratiu Vultur if (port == i_port)
31537ed567SHoratiu Vultur return true;
32537ed567SHoratiu Vultur
33537ed567SHoratiu Vultur return false;
34537ed567SHoratiu Vultur }
359a9f26e8SHoratiu Vultur
br_mrp_get_port(struct net_bridge * br,u32 ifindex)369a9f26e8SHoratiu Vultur static struct net_bridge_port *br_mrp_get_port(struct net_bridge *br,
379a9f26e8SHoratiu Vultur u32 ifindex)
389a9f26e8SHoratiu Vultur {
399a9f26e8SHoratiu Vultur struct net_bridge_port *res = NULL;
409a9f26e8SHoratiu Vultur struct net_bridge_port *port;
419a9f26e8SHoratiu Vultur
429a9f26e8SHoratiu Vultur list_for_each_entry(port, &br->port_list, list) {
439a9f26e8SHoratiu Vultur if (port->dev->ifindex == ifindex) {
449a9f26e8SHoratiu Vultur res = port;
459a9f26e8SHoratiu Vultur break;
469a9f26e8SHoratiu Vultur }
479a9f26e8SHoratiu Vultur }
489a9f26e8SHoratiu Vultur
499a9f26e8SHoratiu Vultur return res;
509a9f26e8SHoratiu Vultur }
519a9f26e8SHoratiu Vultur
br_mrp_find_id(struct net_bridge * br,u32 ring_id)529a9f26e8SHoratiu Vultur static struct br_mrp *br_mrp_find_id(struct net_bridge *br, u32 ring_id)
539a9f26e8SHoratiu Vultur {
549a9f26e8SHoratiu Vultur struct br_mrp *res = NULL;
559a9f26e8SHoratiu Vultur struct br_mrp *mrp;
569a9f26e8SHoratiu Vultur
570169b820SHoratiu Vultur hlist_for_each_entry_rcu(mrp, &br->mrp_list, list,
589a9f26e8SHoratiu Vultur lockdep_rtnl_is_held()) {
599a9f26e8SHoratiu Vultur if (mrp->ring_id == ring_id) {
609a9f26e8SHoratiu Vultur res = mrp;
619a9f26e8SHoratiu Vultur break;
629a9f26e8SHoratiu Vultur }
639a9f26e8SHoratiu Vultur }
649a9f26e8SHoratiu Vultur
659a9f26e8SHoratiu Vultur return res;
669a9f26e8SHoratiu Vultur }
679a9f26e8SHoratiu Vultur
br_mrp_find_in_id(struct net_bridge * br,u32 in_id)68537ed567SHoratiu Vultur static struct br_mrp *br_mrp_find_in_id(struct net_bridge *br, u32 in_id)
69537ed567SHoratiu Vultur {
70537ed567SHoratiu Vultur struct br_mrp *res = NULL;
71537ed567SHoratiu Vultur struct br_mrp *mrp;
72537ed567SHoratiu Vultur
730169b820SHoratiu Vultur hlist_for_each_entry_rcu(mrp, &br->mrp_list, list,
74537ed567SHoratiu Vultur lockdep_rtnl_is_held()) {
75537ed567SHoratiu Vultur if (mrp->in_id == in_id) {
76537ed567SHoratiu Vultur res = mrp;
77537ed567SHoratiu Vultur break;
78537ed567SHoratiu Vultur }
79537ed567SHoratiu Vultur }
80537ed567SHoratiu Vultur
81537ed567SHoratiu Vultur return res;
82537ed567SHoratiu Vultur }
83537ed567SHoratiu Vultur
br_mrp_unique_ifindex(struct net_bridge * br,u32 ifindex)847aa38018SHoratiu Vultur static bool br_mrp_unique_ifindex(struct net_bridge *br, u32 ifindex)
857aa38018SHoratiu Vultur {
867aa38018SHoratiu Vultur struct br_mrp *mrp;
877aa38018SHoratiu Vultur
880169b820SHoratiu Vultur hlist_for_each_entry_rcu(mrp, &br->mrp_list, list,
897aa38018SHoratiu Vultur lockdep_rtnl_is_held()) {
907aa38018SHoratiu Vultur struct net_bridge_port *p;
917aa38018SHoratiu Vultur
927aa38018SHoratiu Vultur p = rtnl_dereference(mrp->p_port);
937aa38018SHoratiu Vultur if (p && p->dev->ifindex == ifindex)
947aa38018SHoratiu Vultur return false;
957aa38018SHoratiu Vultur
967aa38018SHoratiu Vultur p = rtnl_dereference(mrp->s_port);
977aa38018SHoratiu Vultur if (p && p->dev->ifindex == ifindex)
987aa38018SHoratiu Vultur return false;
99537ed567SHoratiu Vultur
100537ed567SHoratiu Vultur p = rtnl_dereference(mrp->i_port);
101537ed567SHoratiu Vultur if (p && p->dev->ifindex == ifindex)
102537ed567SHoratiu Vultur return false;
1037aa38018SHoratiu Vultur }
1047aa38018SHoratiu Vultur
1057aa38018SHoratiu Vultur return true;
1067aa38018SHoratiu Vultur }
1077aa38018SHoratiu Vultur
br_mrp_find_port(struct net_bridge * br,struct net_bridge_port * p)1089a9f26e8SHoratiu Vultur static struct br_mrp *br_mrp_find_port(struct net_bridge *br,
1099a9f26e8SHoratiu Vultur struct net_bridge_port *p)
1109a9f26e8SHoratiu Vultur {
1119a9f26e8SHoratiu Vultur struct br_mrp *res = NULL;
1129a9f26e8SHoratiu Vultur struct br_mrp *mrp;
1139a9f26e8SHoratiu Vultur
1140169b820SHoratiu Vultur hlist_for_each_entry_rcu(mrp, &br->mrp_list, list,
1159a9f26e8SHoratiu Vultur lockdep_rtnl_is_held()) {
1169a9f26e8SHoratiu Vultur if (rcu_access_pointer(mrp->p_port) == p ||
117537ed567SHoratiu Vultur rcu_access_pointer(mrp->s_port) == p ||
118537ed567SHoratiu Vultur rcu_access_pointer(mrp->i_port) == p) {
1199a9f26e8SHoratiu Vultur res = mrp;
1209a9f26e8SHoratiu Vultur break;
1219a9f26e8SHoratiu Vultur }
1229a9f26e8SHoratiu Vultur }
1239a9f26e8SHoratiu Vultur
1249a9f26e8SHoratiu Vultur return res;
1259a9f26e8SHoratiu Vultur }
1269a9f26e8SHoratiu Vultur
br_mrp_next_seq(struct br_mrp * mrp)1279a9f26e8SHoratiu Vultur static int br_mrp_next_seq(struct br_mrp *mrp)
1289a9f26e8SHoratiu Vultur {
1299a9f26e8SHoratiu Vultur mrp->seq_id++;
1309a9f26e8SHoratiu Vultur return mrp->seq_id;
1319a9f26e8SHoratiu Vultur }
1329a9f26e8SHoratiu Vultur
br_mrp_skb_alloc(struct net_bridge_port * p,const u8 * src,const u8 * dst)1339a9f26e8SHoratiu Vultur static struct sk_buff *br_mrp_skb_alloc(struct net_bridge_port *p,
1349a9f26e8SHoratiu Vultur const u8 *src, const u8 *dst)
1359a9f26e8SHoratiu Vultur {
1369a9f26e8SHoratiu Vultur struct ethhdr *eth_hdr;
1379a9f26e8SHoratiu Vultur struct sk_buff *skb;
1389b14d1f8SHoratiu Vultur __be16 *version;
1399a9f26e8SHoratiu Vultur
1409a9f26e8SHoratiu Vultur skb = dev_alloc_skb(MRP_MAX_FRAME_LENGTH);
1419a9f26e8SHoratiu Vultur if (!skb)
1429a9f26e8SHoratiu Vultur return NULL;
1439a9f26e8SHoratiu Vultur
1449a9f26e8SHoratiu Vultur skb->dev = p->dev;
1459a9f26e8SHoratiu Vultur skb->protocol = htons(ETH_P_MRP);
1469a9f26e8SHoratiu Vultur skb->priority = MRP_FRAME_PRIO;
1479a9f26e8SHoratiu Vultur skb_reserve(skb, sizeof(*eth_hdr));
1489a9f26e8SHoratiu Vultur
1499a9f26e8SHoratiu Vultur eth_hdr = skb_push(skb, sizeof(*eth_hdr));
1509a9f26e8SHoratiu Vultur ether_addr_copy(eth_hdr->h_dest, dst);
1519a9f26e8SHoratiu Vultur ether_addr_copy(eth_hdr->h_source, src);
1529a9f26e8SHoratiu Vultur eth_hdr->h_proto = htons(ETH_P_MRP);
1539a9f26e8SHoratiu Vultur
1549a9f26e8SHoratiu Vultur version = skb_put(skb, sizeof(*version));
1559a9f26e8SHoratiu Vultur *version = cpu_to_be16(MRP_VERSION);
1569a9f26e8SHoratiu Vultur
1579a9f26e8SHoratiu Vultur return skb;
1589a9f26e8SHoratiu Vultur }
1599a9f26e8SHoratiu Vultur
br_mrp_skb_tlv(struct sk_buff * skb,enum br_mrp_tlv_header_type type,u8 length)1609a9f26e8SHoratiu Vultur static void br_mrp_skb_tlv(struct sk_buff *skb,
1619a9f26e8SHoratiu Vultur enum br_mrp_tlv_header_type type,
1629a9f26e8SHoratiu Vultur u8 length)
1639a9f26e8SHoratiu Vultur {
1649a9f26e8SHoratiu Vultur struct br_mrp_tlv_hdr *hdr;
1659a9f26e8SHoratiu Vultur
1669a9f26e8SHoratiu Vultur hdr = skb_put(skb, sizeof(*hdr));
1679a9f26e8SHoratiu Vultur hdr->type = type;
1689a9f26e8SHoratiu Vultur hdr->length = length;
1699a9f26e8SHoratiu Vultur }
1709a9f26e8SHoratiu Vultur
br_mrp_skb_common(struct sk_buff * skb,struct br_mrp * mrp)1719a9f26e8SHoratiu Vultur static void br_mrp_skb_common(struct sk_buff *skb, struct br_mrp *mrp)
1729a9f26e8SHoratiu Vultur {
1739a9f26e8SHoratiu Vultur struct br_mrp_common_hdr *hdr;
1749a9f26e8SHoratiu Vultur
1759a9f26e8SHoratiu Vultur br_mrp_skb_tlv(skb, BR_MRP_TLV_HEADER_COMMON, sizeof(*hdr));
1769a9f26e8SHoratiu Vultur
1779a9f26e8SHoratiu Vultur hdr = skb_put(skb, sizeof(*hdr));
1789a9f26e8SHoratiu Vultur hdr->seq_id = cpu_to_be16(br_mrp_next_seq(mrp));
1799a9f26e8SHoratiu Vultur memset(hdr->domain, 0xff, MRP_DOMAIN_UUID_LENGTH);
1809a9f26e8SHoratiu Vultur }
1819a9f26e8SHoratiu Vultur
br_mrp_alloc_test_skb(struct br_mrp * mrp,struct net_bridge_port * p,enum br_mrp_port_role_type port_role)1829a9f26e8SHoratiu Vultur static struct sk_buff *br_mrp_alloc_test_skb(struct br_mrp *mrp,
1839a9f26e8SHoratiu Vultur struct net_bridge_port *p,
1849a9f26e8SHoratiu Vultur enum br_mrp_port_role_type port_role)
1859a9f26e8SHoratiu Vultur {
1869a9f26e8SHoratiu Vultur struct br_mrp_ring_test_hdr *hdr = NULL;
1879a9f26e8SHoratiu Vultur struct sk_buff *skb = NULL;
1889a9f26e8SHoratiu Vultur
1899a9f26e8SHoratiu Vultur if (!p)
1909a9f26e8SHoratiu Vultur return NULL;
1919a9f26e8SHoratiu Vultur
1929a9f26e8SHoratiu Vultur skb = br_mrp_skb_alloc(p, p->dev->dev_addr, mrp_test_dmac);
1939a9f26e8SHoratiu Vultur if (!skb)
1949a9f26e8SHoratiu Vultur return NULL;
1959a9f26e8SHoratiu Vultur
1969a9f26e8SHoratiu Vultur br_mrp_skb_tlv(skb, BR_MRP_TLV_HEADER_RING_TEST, sizeof(*hdr));
1979a9f26e8SHoratiu Vultur hdr = skb_put(skb, sizeof(*hdr));
1989a9f26e8SHoratiu Vultur
1994b3a61b0SHoratiu Vultur hdr->prio = cpu_to_be16(mrp->prio);
2009a9f26e8SHoratiu Vultur ether_addr_copy(hdr->sa, p->br->dev->dev_addr);
2019a9f26e8SHoratiu Vultur hdr->port_role = cpu_to_be16(port_role);
2029a9f26e8SHoratiu Vultur hdr->state = cpu_to_be16(mrp->ring_state);
2039a9f26e8SHoratiu Vultur hdr->transitions = cpu_to_be16(mrp->ring_transitions);
2049a9f26e8SHoratiu Vultur hdr->timestamp = cpu_to_be32(jiffies_to_msecs(jiffies));
2059a9f26e8SHoratiu Vultur
2069a9f26e8SHoratiu Vultur br_mrp_skb_common(skb, mrp);
207*f7458934SHoratiu Vultur
208*f7458934SHoratiu Vultur /* In case the node behaves as MRA then the Test frame needs to have
209*f7458934SHoratiu Vultur * an Option TLV which includes eventually a sub-option TLV that has
210*f7458934SHoratiu Vultur * the type AUTO_MGR
211*f7458934SHoratiu Vultur */
212*f7458934SHoratiu Vultur if (mrp->ring_role == BR_MRP_RING_ROLE_MRA) {
213*f7458934SHoratiu Vultur struct br_mrp_sub_option1_hdr *sub_opt = NULL;
214*f7458934SHoratiu Vultur struct br_mrp_tlv_hdr *sub_tlv = NULL;
215*f7458934SHoratiu Vultur struct br_mrp_oui_hdr *oui = NULL;
216*f7458934SHoratiu Vultur u8 length;
217*f7458934SHoratiu Vultur
218*f7458934SHoratiu Vultur length = sizeof(*sub_opt) + sizeof(*sub_tlv) + sizeof(oui) +
219*f7458934SHoratiu Vultur MRP_OPT_PADDING;
220*f7458934SHoratiu Vultur br_mrp_skb_tlv(skb, BR_MRP_TLV_HEADER_OPTION, length);
221*f7458934SHoratiu Vultur
222*f7458934SHoratiu Vultur oui = skb_put(skb, sizeof(*oui));
223*f7458934SHoratiu Vultur memset(oui, 0x0, sizeof(*oui));
224*f7458934SHoratiu Vultur sub_opt = skb_put(skb, sizeof(*sub_opt));
225*f7458934SHoratiu Vultur memset(sub_opt, 0x0, sizeof(*sub_opt));
226*f7458934SHoratiu Vultur
227*f7458934SHoratiu Vultur sub_tlv = skb_put(skb, sizeof(*sub_tlv));
228*f7458934SHoratiu Vultur sub_tlv->type = BR_MRP_SUB_TLV_HEADER_TEST_AUTO_MGR;
229*f7458934SHoratiu Vultur
230*f7458934SHoratiu Vultur /* 32 bit alligment shall be ensured therefore add 2 bytes */
231*f7458934SHoratiu Vultur skb_put(skb, MRP_OPT_PADDING);
232*f7458934SHoratiu Vultur }
233*f7458934SHoratiu Vultur
2349a9f26e8SHoratiu Vultur br_mrp_skb_tlv(skb, BR_MRP_TLV_HEADER_END, 0x0);
2359a9f26e8SHoratiu Vultur
2369a9f26e8SHoratiu Vultur return skb;
2379a9f26e8SHoratiu Vultur }
2389a9f26e8SHoratiu Vultur
br_mrp_alloc_in_test_skb(struct br_mrp * mrp,struct net_bridge_port * p,enum br_mrp_port_role_type port_role)239537ed567SHoratiu Vultur static struct sk_buff *br_mrp_alloc_in_test_skb(struct br_mrp *mrp,
240537ed567SHoratiu Vultur struct net_bridge_port *p,
241537ed567SHoratiu Vultur enum br_mrp_port_role_type port_role)
242537ed567SHoratiu Vultur {
243537ed567SHoratiu Vultur struct br_mrp_in_test_hdr *hdr = NULL;
244537ed567SHoratiu Vultur struct sk_buff *skb = NULL;
245537ed567SHoratiu Vultur
246537ed567SHoratiu Vultur if (!p)
247537ed567SHoratiu Vultur return NULL;
248537ed567SHoratiu Vultur
249537ed567SHoratiu Vultur skb = br_mrp_skb_alloc(p, p->dev->dev_addr, mrp_in_test_dmac);
250537ed567SHoratiu Vultur if (!skb)
251537ed567SHoratiu Vultur return NULL;
252537ed567SHoratiu Vultur
253537ed567SHoratiu Vultur br_mrp_skb_tlv(skb, BR_MRP_TLV_HEADER_IN_TEST, sizeof(*hdr));
254537ed567SHoratiu Vultur hdr = skb_put(skb, sizeof(*hdr));
255537ed567SHoratiu Vultur
256537ed567SHoratiu Vultur hdr->id = cpu_to_be16(mrp->in_id);
257537ed567SHoratiu Vultur ether_addr_copy(hdr->sa, p->br->dev->dev_addr);
258537ed567SHoratiu Vultur hdr->port_role = cpu_to_be16(port_role);
259537ed567SHoratiu Vultur hdr->state = cpu_to_be16(mrp->in_state);
260537ed567SHoratiu Vultur hdr->transitions = cpu_to_be16(mrp->in_transitions);
261537ed567SHoratiu Vultur hdr->timestamp = cpu_to_be32(jiffies_to_msecs(jiffies));
262537ed567SHoratiu Vultur
263537ed567SHoratiu Vultur br_mrp_skb_common(skb, mrp);
264537ed567SHoratiu Vultur br_mrp_skb_tlv(skb, BR_MRP_TLV_HEADER_END, 0x0);
265537ed567SHoratiu Vultur
266537ed567SHoratiu Vultur return skb;
267537ed567SHoratiu Vultur }
268537ed567SHoratiu Vultur
269c6676e7dSHoratiu Vultur /* This function is continuously called in the following cases:
270c6676e7dSHoratiu Vultur * - when node role is MRM, in this case test_monitor is always set to false
271c6676e7dSHoratiu Vultur * because it needs to notify the userspace that the ring is open and needs to
272c6676e7dSHoratiu Vultur * send MRP_Test frames
273c6676e7dSHoratiu Vultur * - when node role is MRA, there are 2 subcases:
274c6676e7dSHoratiu Vultur * - when MRA behaves as MRM, in this case is similar with MRM role
275c6676e7dSHoratiu Vultur * - when MRA behaves as MRC, in this case test_monitor is set to true,
276c6676e7dSHoratiu Vultur * because it needs to detect when it stops seeing MRP_Test frames
277c6676e7dSHoratiu Vultur * from MRM node but it doesn't need to send MRP_Test frames.
278c6676e7dSHoratiu Vultur */
br_mrp_test_work_expired(struct work_struct * work)2799a9f26e8SHoratiu Vultur static void br_mrp_test_work_expired(struct work_struct *work)
2809a9f26e8SHoratiu Vultur {
2819a9f26e8SHoratiu Vultur struct delayed_work *del_work = to_delayed_work(work);
2829a9f26e8SHoratiu Vultur struct br_mrp *mrp = container_of(del_work, struct br_mrp, test_work);
2839a9f26e8SHoratiu Vultur struct net_bridge_port *p;
2849a9f26e8SHoratiu Vultur bool notify_open = false;
2859a9f26e8SHoratiu Vultur struct sk_buff *skb;
2869a9f26e8SHoratiu Vultur
2879a9f26e8SHoratiu Vultur if (time_before_eq(mrp->test_end, jiffies))
2889a9f26e8SHoratiu Vultur return;
2899a9f26e8SHoratiu Vultur
2909a9f26e8SHoratiu Vultur if (mrp->test_count_miss < mrp->test_max_miss) {
2919a9f26e8SHoratiu Vultur mrp->test_count_miss++;
2929a9f26e8SHoratiu Vultur } else {
2939a9f26e8SHoratiu Vultur /* Notify that the ring is open only if the ring state is
2949a9f26e8SHoratiu Vultur * closed, otherwise it would continue to notify at every
2959a9f26e8SHoratiu Vultur * interval.
296c6676e7dSHoratiu Vultur * Also notify that the ring is open when the node has the
297c6676e7dSHoratiu Vultur * role MRA and behaves as MRC. The reason is that the
298c6676e7dSHoratiu Vultur * userspace needs to know when the MRM stopped sending
299c6676e7dSHoratiu Vultur * MRP_Test frames so that the current node to try to take
300c6676e7dSHoratiu Vultur * the role of a MRM.
3019a9f26e8SHoratiu Vultur */
302c6676e7dSHoratiu Vultur if (mrp->ring_state == BR_MRP_RING_STATE_CLOSED ||
303c6676e7dSHoratiu Vultur mrp->test_monitor)
3049a9f26e8SHoratiu Vultur notify_open = true;
3059a9f26e8SHoratiu Vultur }
3069a9f26e8SHoratiu Vultur
3079a9f26e8SHoratiu Vultur rcu_read_lock();
3089a9f26e8SHoratiu Vultur
3099a9f26e8SHoratiu Vultur p = rcu_dereference(mrp->p_port);
3109a9f26e8SHoratiu Vultur if (p) {
311c6676e7dSHoratiu Vultur if (!mrp->test_monitor) {
312c6676e7dSHoratiu Vultur skb = br_mrp_alloc_test_skb(mrp, p,
313c6676e7dSHoratiu Vultur BR_MRP_PORT_ROLE_PRIMARY);
3149a9f26e8SHoratiu Vultur if (!skb)
3159a9f26e8SHoratiu Vultur goto out;
3169a9f26e8SHoratiu Vultur
3179a9f26e8SHoratiu Vultur skb_reset_network_header(skb);
3189a9f26e8SHoratiu Vultur dev_queue_xmit(skb);
319c6676e7dSHoratiu Vultur }
3209a9f26e8SHoratiu Vultur
3219a9f26e8SHoratiu Vultur if (notify_open && !mrp->ring_role_offloaded)
3224cc625c6SHoratiu Vultur br_mrp_ring_port_open(p->dev, true);
3239a9f26e8SHoratiu Vultur }
3249a9f26e8SHoratiu Vultur
3259a9f26e8SHoratiu Vultur p = rcu_dereference(mrp->s_port);
3269a9f26e8SHoratiu Vultur if (p) {
327c6676e7dSHoratiu Vultur if (!mrp->test_monitor) {
328c6676e7dSHoratiu Vultur skb = br_mrp_alloc_test_skb(mrp, p,
329c6676e7dSHoratiu Vultur BR_MRP_PORT_ROLE_SECONDARY);
3309a9f26e8SHoratiu Vultur if (!skb)
3319a9f26e8SHoratiu Vultur goto out;
3329a9f26e8SHoratiu Vultur
3339a9f26e8SHoratiu Vultur skb_reset_network_header(skb);
3349a9f26e8SHoratiu Vultur dev_queue_xmit(skb);
335c6676e7dSHoratiu Vultur }
3369a9f26e8SHoratiu Vultur
3379a9f26e8SHoratiu Vultur if (notify_open && !mrp->ring_role_offloaded)
3384cc625c6SHoratiu Vultur br_mrp_ring_port_open(p->dev, true);
3399a9f26e8SHoratiu Vultur }
3409a9f26e8SHoratiu Vultur
3419a9f26e8SHoratiu Vultur out:
3429a9f26e8SHoratiu Vultur rcu_read_unlock();
3439a9f26e8SHoratiu Vultur
3449a9f26e8SHoratiu Vultur queue_delayed_work(system_wq, &mrp->test_work,
3459a9f26e8SHoratiu Vultur usecs_to_jiffies(mrp->test_interval));
3469a9f26e8SHoratiu Vultur }
3479a9f26e8SHoratiu Vultur
348537ed567SHoratiu Vultur /* This function is continuously called when the node has the interconnect role
349537ed567SHoratiu Vultur * MIM. It would generate interconnect test frames and will send them on all 3
350537ed567SHoratiu Vultur * ports. But will also check if it stop receiving interconnect test frames.
351537ed567SHoratiu Vultur */
br_mrp_in_test_work_expired(struct work_struct * work)352537ed567SHoratiu Vultur static void br_mrp_in_test_work_expired(struct work_struct *work)
353537ed567SHoratiu Vultur {
354537ed567SHoratiu Vultur struct delayed_work *del_work = to_delayed_work(work);
355537ed567SHoratiu Vultur struct br_mrp *mrp = container_of(del_work, struct br_mrp, in_test_work);
356537ed567SHoratiu Vultur struct net_bridge_port *p;
357537ed567SHoratiu Vultur bool notify_open = false;
358537ed567SHoratiu Vultur struct sk_buff *skb;
359537ed567SHoratiu Vultur
360537ed567SHoratiu Vultur if (time_before_eq(mrp->in_test_end, jiffies))
361537ed567SHoratiu Vultur return;
362537ed567SHoratiu Vultur
363537ed567SHoratiu Vultur if (mrp->in_test_count_miss < mrp->in_test_max_miss) {
364537ed567SHoratiu Vultur mrp->in_test_count_miss++;
365537ed567SHoratiu Vultur } else {
366537ed567SHoratiu Vultur /* Notify that the interconnect ring is open only if the
367537ed567SHoratiu Vultur * interconnect ring state is closed, otherwise it would
368537ed567SHoratiu Vultur * continue to notify at every interval.
369537ed567SHoratiu Vultur */
370537ed567SHoratiu Vultur if (mrp->in_state == BR_MRP_IN_STATE_CLOSED)
371537ed567SHoratiu Vultur notify_open = true;
372537ed567SHoratiu Vultur }
373537ed567SHoratiu Vultur
374537ed567SHoratiu Vultur rcu_read_lock();
375537ed567SHoratiu Vultur
376537ed567SHoratiu Vultur p = rcu_dereference(mrp->p_port);
377537ed567SHoratiu Vultur if (p) {
378537ed567SHoratiu Vultur skb = br_mrp_alloc_in_test_skb(mrp, p,
379537ed567SHoratiu Vultur BR_MRP_PORT_ROLE_PRIMARY);
380537ed567SHoratiu Vultur if (!skb)
381537ed567SHoratiu Vultur goto out;
382537ed567SHoratiu Vultur
383537ed567SHoratiu Vultur skb_reset_network_header(skb);
384537ed567SHoratiu Vultur dev_queue_xmit(skb);
385537ed567SHoratiu Vultur
386537ed567SHoratiu Vultur if (notify_open && !mrp->in_role_offloaded)
387537ed567SHoratiu Vultur br_mrp_in_port_open(p->dev, true);
388537ed567SHoratiu Vultur }
389537ed567SHoratiu Vultur
390537ed567SHoratiu Vultur p = rcu_dereference(mrp->s_port);
391537ed567SHoratiu Vultur if (p) {
392537ed567SHoratiu Vultur skb = br_mrp_alloc_in_test_skb(mrp, p,
393537ed567SHoratiu Vultur BR_MRP_PORT_ROLE_SECONDARY);
394537ed567SHoratiu Vultur if (!skb)
395537ed567SHoratiu Vultur goto out;
396537ed567SHoratiu Vultur
397537ed567SHoratiu Vultur skb_reset_network_header(skb);
398537ed567SHoratiu Vultur dev_queue_xmit(skb);
399537ed567SHoratiu Vultur
400537ed567SHoratiu Vultur if (notify_open && !mrp->in_role_offloaded)
401537ed567SHoratiu Vultur br_mrp_in_port_open(p->dev, true);
402537ed567SHoratiu Vultur }
403537ed567SHoratiu Vultur
404537ed567SHoratiu Vultur p = rcu_dereference(mrp->i_port);
405537ed567SHoratiu Vultur if (p) {
406537ed567SHoratiu Vultur skb = br_mrp_alloc_in_test_skb(mrp, p,
407537ed567SHoratiu Vultur BR_MRP_PORT_ROLE_INTER);
408537ed567SHoratiu Vultur if (!skb)
409537ed567SHoratiu Vultur goto out;
410537ed567SHoratiu Vultur
411537ed567SHoratiu Vultur skb_reset_network_header(skb);
412537ed567SHoratiu Vultur dev_queue_xmit(skb);
413537ed567SHoratiu Vultur
414537ed567SHoratiu Vultur if (notify_open && !mrp->in_role_offloaded)
415537ed567SHoratiu Vultur br_mrp_in_port_open(p->dev, true);
416537ed567SHoratiu Vultur }
417537ed567SHoratiu Vultur
418537ed567SHoratiu Vultur out:
419537ed567SHoratiu Vultur rcu_read_unlock();
420537ed567SHoratiu Vultur
421537ed567SHoratiu Vultur queue_delayed_work(system_wq, &mrp->in_test_work,
422537ed567SHoratiu Vultur usecs_to_jiffies(mrp->in_test_interval));
423537ed567SHoratiu Vultur }
424537ed567SHoratiu Vultur
4259a9f26e8SHoratiu Vultur /* Deletes the MRP instance.
4269a9f26e8SHoratiu Vultur * note: called under rtnl_lock
4279a9f26e8SHoratiu Vultur */
br_mrp_del_impl(struct net_bridge * br,struct br_mrp * mrp)4289a9f26e8SHoratiu Vultur static void br_mrp_del_impl(struct net_bridge *br, struct br_mrp *mrp)
4299a9f26e8SHoratiu Vultur {
4309a9f26e8SHoratiu Vultur struct net_bridge_port *p;
4314fb13499SHoratiu Vultur u8 state;
4329a9f26e8SHoratiu Vultur
4339a9f26e8SHoratiu Vultur /* Stop sending MRP_Test frames */
4349a9f26e8SHoratiu Vultur cancel_delayed_work_sync(&mrp->test_work);
435c6676e7dSHoratiu Vultur br_mrp_switchdev_send_ring_test(br, mrp, 0, 0, 0, 0);
4369a9f26e8SHoratiu Vultur
437537ed567SHoratiu Vultur /* Stop sending MRP_InTest frames if has an interconnect role */
438537ed567SHoratiu Vultur cancel_delayed_work_sync(&mrp->in_test_work);
439537ed567SHoratiu Vultur br_mrp_switchdev_send_in_test(br, mrp, 0, 0, 0);
440537ed567SHoratiu Vultur
441b3cb91b9SHoratiu Vultur /* Disable the roles */
442b3cb91b9SHoratiu Vultur br_mrp_switchdev_set_ring_role(br, mrp, BR_MRP_RING_ROLE_DISABLED);
443b3cb91b9SHoratiu Vultur p = rtnl_dereference(mrp->i_port);
444b3cb91b9SHoratiu Vultur if (p)
445b3cb91b9SHoratiu Vultur br_mrp_switchdev_set_in_role(br, mrp, mrp->in_id, mrp->ring_id,
446b3cb91b9SHoratiu Vultur BR_MRP_IN_ROLE_DISABLED);
447b3cb91b9SHoratiu Vultur
4489a9f26e8SHoratiu Vultur br_mrp_switchdev_del(br, mrp);
4499a9f26e8SHoratiu Vultur
4509a9f26e8SHoratiu Vultur /* Reset the ports */
4519a9f26e8SHoratiu Vultur p = rtnl_dereference(mrp->p_port);
4529a9f26e8SHoratiu Vultur if (p) {
4539a9f26e8SHoratiu Vultur spin_lock_bh(&br->lock);
4544fb13499SHoratiu Vultur state = netif_running(br->dev) ?
4554fb13499SHoratiu Vultur BR_STATE_FORWARDING : BR_STATE_DISABLED;
4564fb13499SHoratiu Vultur p->state = state;
4579a9f26e8SHoratiu Vultur p->flags &= ~BR_MRP_AWARE;
4589a9f26e8SHoratiu Vultur spin_unlock_bh(&br->lock);
4594fb13499SHoratiu Vultur br_mrp_port_switchdev_set_state(p, state);
4609a9f26e8SHoratiu Vultur rcu_assign_pointer(mrp->p_port, NULL);
4619a9f26e8SHoratiu Vultur }
4629a9f26e8SHoratiu Vultur
4639a9f26e8SHoratiu Vultur p = rtnl_dereference(mrp->s_port);
4649a9f26e8SHoratiu Vultur if (p) {
4659a9f26e8SHoratiu Vultur spin_lock_bh(&br->lock);
4664fb13499SHoratiu Vultur state = netif_running(br->dev) ?
4674fb13499SHoratiu Vultur BR_STATE_FORWARDING : BR_STATE_DISABLED;
4684fb13499SHoratiu Vultur p->state = state;
4699a9f26e8SHoratiu Vultur p->flags &= ~BR_MRP_AWARE;
4709a9f26e8SHoratiu Vultur spin_unlock_bh(&br->lock);
4714fb13499SHoratiu Vultur br_mrp_port_switchdev_set_state(p, state);
4729a9f26e8SHoratiu Vultur rcu_assign_pointer(mrp->s_port, NULL);
4739a9f26e8SHoratiu Vultur }
4749a9f26e8SHoratiu Vultur
475537ed567SHoratiu Vultur p = rtnl_dereference(mrp->i_port);
476537ed567SHoratiu Vultur if (p) {
477537ed567SHoratiu Vultur spin_lock_bh(&br->lock);
478537ed567SHoratiu Vultur state = netif_running(br->dev) ?
479537ed567SHoratiu Vultur BR_STATE_FORWARDING : BR_STATE_DISABLED;
480537ed567SHoratiu Vultur p->state = state;
481537ed567SHoratiu Vultur p->flags &= ~BR_MRP_AWARE;
482537ed567SHoratiu Vultur spin_unlock_bh(&br->lock);
483537ed567SHoratiu Vultur br_mrp_port_switchdev_set_state(p, state);
484537ed567SHoratiu Vultur rcu_assign_pointer(mrp->i_port, NULL);
485537ed567SHoratiu Vultur }
486537ed567SHoratiu Vultur
4870169b820SHoratiu Vultur hlist_del_rcu(&mrp->list);
4889a9f26e8SHoratiu Vultur kfree_rcu(mrp, rcu);
48990c628ddSHenrik Bjoernlund
4900169b820SHoratiu Vultur if (hlist_empty(&br->mrp_list))
49190c628ddSHenrik Bjoernlund br_del_frame(br, &mrp_frame_type);
4929a9f26e8SHoratiu Vultur }
4939a9f26e8SHoratiu Vultur
4949a9f26e8SHoratiu Vultur /* Adds a new MRP instance.
4959a9f26e8SHoratiu Vultur * note: called under rtnl_lock
4969a9f26e8SHoratiu Vultur */
br_mrp_add(struct net_bridge * br,struct br_mrp_instance * instance)4979a9f26e8SHoratiu Vultur int br_mrp_add(struct net_bridge *br, struct br_mrp_instance *instance)
4989a9f26e8SHoratiu Vultur {
4999a9f26e8SHoratiu Vultur struct net_bridge_port *p;
5009a9f26e8SHoratiu Vultur struct br_mrp *mrp;
5019a9f26e8SHoratiu Vultur int err;
5029a9f26e8SHoratiu Vultur
5039a9f26e8SHoratiu Vultur /* If the ring exists, it is not possible to create another one with the
5049a9f26e8SHoratiu Vultur * same ring_id
5059a9f26e8SHoratiu Vultur */
5069a9f26e8SHoratiu Vultur mrp = br_mrp_find_id(br, instance->ring_id);
5079a9f26e8SHoratiu Vultur if (mrp)
5089a9f26e8SHoratiu Vultur return -EINVAL;
5099a9f26e8SHoratiu Vultur
5109a9f26e8SHoratiu Vultur if (!br_mrp_get_port(br, instance->p_ifindex) ||
5119a9f26e8SHoratiu Vultur !br_mrp_get_port(br, instance->s_ifindex))
5129a9f26e8SHoratiu Vultur return -EINVAL;
5139a9f26e8SHoratiu Vultur
5147aa38018SHoratiu Vultur /* It is not possible to have the same port part of multiple rings */
5157aa38018SHoratiu Vultur if (!br_mrp_unique_ifindex(br, instance->p_ifindex) ||
5167aa38018SHoratiu Vultur !br_mrp_unique_ifindex(br, instance->s_ifindex))
5177aa38018SHoratiu Vultur return -EINVAL;
5187aa38018SHoratiu Vultur
5199a9f26e8SHoratiu Vultur mrp = kzalloc(sizeof(*mrp), GFP_KERNEL);
5209a9f26e8SHoratiu Vultur if (!mrp)
5219a9f26e8SHoratiu Vultur return -ENOMEM;
5229a9f26e8SHoratiu Vultur
5239a9f26e8SHoratiu Vultur mrp->ring_id = instance->ring_id;
5244b3a61b0SHoratiu Vultur mrp->prio = instance->prio;
5259a9f26e8SHoratiu Vultur
5269a9f26e8SHoratiu Vultur p = br_mrp_get_port(br, instance->p_ifindex);
5279a9f26e8SHoratiu Vultur spin_lock_bh(&br->lock);
5289a9f26e8SHoratiu Vultur p->state = BR_STATE_FORWARDING;
5299a9f26e8SHoratiu Vultur p->flags |= BR_MRP_AWARE;
5309a9f26e8SHoratiu Vultur spin_unlock_bh(&br->lock);
5319a9f26e8SHoratiu Vultur rcu_assign_pointer(mrp->p_port, p);
5329a9f26e8SHoratiu Vultur
5339a9f26e8SHoratiu Vultur p = br_mrp_get_port(br, instance->s_ifindex);
5349a9f26e8SHoratiu Vultur spin_lock_bh(&br->lock);
5359a9f26e8SHoratiu Vultur p->state = BR_STATE_FORWARDING;
5369a9f26e8SHoratiu Vultur p->flags |= BR_MRP_AWARE;
5379a9f26e8SHoratiu Vultur spin_unlock_bh(&br->lock);
5389a9f26e8SHoratiu Vultur rcu_assign_pointer(mrp->s_port, p);
5399a9f26e8SHoratiu Vultur
5400169b820SHoratiu Vultur if (hlist_empty(&br->mrp_list))
54190c628ddSHenrik Bjoernlund br_add_frame(br, &mrp_frame_type);
54290c628ddSHenrik Bjoernlund
5439a9f26e8SHoratiu Vultur INIT_DELAYED_WORK(&mrp->test_work, br_mrp_test_work_expired);
544537ed567SHoratiu Vultur INIT_DELAYED_WORK(&mrp->in_test_work, br_mrp_in_test_work_expired);
5450169b820SHoratiu Vultur hlist_add_tail_rcu(&mrp->list, &br->mrp_list);
5469a9f26e8SHoratiu Vultur
5479a9f26e8SHoratiu Vultur err = br_mrp_switchdev_add(br, mrp);
5489a9f26e8SHoratiu Vultur if (err)
5499a9f26e8SHoratiu Vultur goto delete_mrp;
5509a9f26e8SHoratiu Vultur
5519a9f26e8SHoratiu Vultur return 0;
5529a9f26e8SHoratiu Vultur
5539a9f26e8SHoratiu Vultur delete_mrp:
5549a9f26e8SHoratiu Vultur br_mrp_del_impl(br, mrp);
5559a9f26e8SHoratiu Vultur
5569a9f26e8SHoratiu Vultur return err;
5579a9f26e8SHoratiu Vultur }
5589a9f26e8SHoratiu Vultur
5599a9f26e8SHoratiu Vultur /* Deletes the MRP instance from which the port is part of
5609a9f26e8SHoratiu Vultur * note: called under rtnl_lock
5619a9f26e8SHoratiu Vultur */
br_mrp_port_del(struct net_bridge * br,struct net_bridge_port * p)5629a9f26e8SHoratiu Vultur void br_mrp_port_del(struct net_bridge *br, struct net_bridge_port *p)
5639a9f26e8SHoratiu Vultur {
5649a9f26e8SHoratiu Vultur struct br_mrp *mrp = br_mrp_find_port(br, p);
5659a9f26e8SHoratiu Vultur
5669a9f26e8SHoratiu Vultur /* If the port is not part of a MRP instance just bail out */
5679a9f26e8SHoratiu Vultur if (!mrp)
5689a9f26e8SHoratiu Vultur return;
5699a9f26e8SHoratiu Vultur
5709a9f26e8SHoratiu Vultur br_mrp_del_impl(br, mrp);
5719a9f26e8SHoratiu Vultur }
5729a9f26e8SHoratiu Vultur
5739a9f26e8SHoratiu Vultur /* Deletes existing MRP instance based on ring_id
5749a9f26e8SHoratiu Vultur * note: called under rtnl_lock
5759a9f26e8SHoratiu Vultur */
br_mrp_del(struct net_bridge * br,struct br_mrp_instance * instance)5769a9f26e8SHoratiu Vultur int br_mrp_del(struct net_bridge *br, struct br_mrp_instance *instance)
5779a9f26e8SHoratiu Vultur {
5789a9f26e8SHoratiu Vultur struct br_mrp *mrp = br_mrp_find_id(br, instance->ring_id);
5799a9f26e8SHoratiu Vultur
5809a9f26e8SHoratiu Vultur if (!mrp)
5819a9f26e8SHoratiu Vultur return -EINVAL;
5829a9f26e8SHoratiu Vultur
5839a9f26e8SHoratiu Vultur br_mrp_del_impl(br, mrp);
5849a9f26e8SHoratiu Vultur
5859a9f26e8SHoratiu Vultur return 0;
5869a9f26e8SHoratiu Vultur }
5879a9f26e8SHoratiu Vultur
5889a9f26e8SHoratiu Vultur /* Set port state, port state can be forwarding, blocked or disabled
5899a9f26e8SHoratiu Vultur * note: already called with rtnl_lock
5909a9f26e8SHoratiu Vultur */
br_mrp_set_port_state(struct net_bridge_port * p,enum br_mrp_port_state_type state)5919a9f26e8SHoratiu Vultur int br_mrp_set_port_state(struct net_bridge_port *p,
5929a9f26e8SHoratiu Vultur enum br_mrp_port_state_type state)
5939a9f26e8SHoratiu Vultur {
594b2bdba1cSHoratiu Vultur u32 port_state;
595b2bdba1cSHoratiu Vultur
5969a9f26e8SHoratiu Vultur if (!p || !(p->flags & BR_MRP_AWARE))
5979a9f26e8SHoratiu Vultur return -EINVAL;
5989a9f26e8SHoratiu Vultur
5999a9f26e8SHoratiu Vultur spin_lock_bh(&p->br->lock);
6009a9f26e8SHoratiu Vultur
6019a9f26e8SHoratiu Vultur if (state == BR_MRP_PORT_STATE_FORWARDING)
602b2bdba1cSHoratiu Vultur port_state = BR_STATE_FORWARDING;
6039a9f26e8SHoratiu Vultur else
604b2bdba1cSHoratiu Vultur port_state = BR_STATE_BLOCKING;
6059a9f26e8SHoratiu Vultur
606b2bdba1cSHoratiu Vultur p->state = port_state;
6079a9f26e8SHoratiu Vultur spin_unlock_bh(&p->br->lock);
6089a9f26e8SHoratiu Vultur
609b2bdba1cSHoratiu Vultur br_mrp_port_switchdev_set_state(p, port_state);
6109a9f26e8SHoratiu Vultur
6119a9f26e8SHoratiu Vultur return 0;
6129a9f26e8SHoratiu Vultur }
6139a9f26e8SHoratiu Vultur
6149a9f26e8SHoratiu Vultur /* Set port role, port role can be primary or secondary
6159a9f26e8SHoratiu Vultur * note: already called with rtnl_lock
6169a9f26e8SHoratiu Vultur */
br_mrp_set_port_role(struct net_bridge_port * p,enum br_mrp_port_role_type role)6179a9f26e8SHoratiu Vultur int br_mrp_set_port_role(struct net_bridge_port *p,
61820f6a05eSHoratiu Vultur enum br_mrp_port_role_type role)
6199a9f26e8SHoratiu Vultur {
6209a9f26e8SHoratiu Vultur struct br_mrp *mrp;
6219a9f26e8SHoratiu Vultur
6229a9f26e8SHoratiu Vultur if (!p || !(p->flags & BR_MRP_AWARE))
6239a9f26e8SHoratiu Vultur return -EINVAL;
6249a9f26e8SHoratiu Vultur
62520f6a05eSHoratiu Vultur mrp = br_mrp_find_port(p->br, p);
6269a9f26e8SHoratiu Vultur
6279a9f26e8SHoratiu Vultur if (!mrp)
6289a9f26e8SHoratiu Vultur return -EINVAL;
6299a9f26e8SHoratiu Vultur
6307882c895SHoratiu Vultur switch (role) {
6317882c895SHoratiu Vultur case BR_MRP_PORT_ROLE_PRIMARY:
6329a9f26e8SHoratiu Vultur rcu_assign_pointer(mrp->p_port, p);
6337882c895SHoratiu Vultur break;
6347882c895SHoratiu Vultur case BR_MRP_PORT_ROLE_SECONDARY:
6359a9f26e8SHoratiu Vultur rcu_assign_pointer(mrp->s_port, p);
6367882c895SHoratiu Vultur break;
6377882c895SHoratiu Vultur default:
6387882c895SHoratiu Vultur return -EINVAL;
6397882c895SHoratiu Vultur }
6409a9f26e8SHoratiu Vultur
64120f6a05eSHoratiu Vultur br_mrp_port_switchdev_set_role(p, role);
6429a9f26e8SHoratiu Vultur
6439a9f26e8SHoratiu Vultur return 0;
6449a9f26e8SHoratiu Vultur }
6459a9f26e8SHoratiu Vultur
6469a9f26e8SHoratiu Vultur /* Set ring state, ring state can be only Open or Closed
6479a9f26e8SHoratiu Vultur * note: already called with rtnl_lock
6489a9f26e8SHoratiu Vultur */
br_mrp_set_ring_state(struct net_bridge * br,struct br_mrp_ring_state * state)6499a9f26e8SHoratiu Vultur int br_mrp_set_ring_state(struct net_bridge *br,
6509a9f26e8SHoratiu Vultur struct br_mrp_ring_state *state)
6519a9f26e8SHoratiu Vultur {
6529a9f26e8SHoratiu Vultur struct br_mrp *mrp = br_mrp_find_id(br, state->ring_id);
6539a9f26e8SHoratiu Vultur
6549a9f26e8SHoratiu Vultur if (!mrp)
6559a9f26e8SHoratiu Vultur return -EINVAL;
6569a9f26e8SHoratiu Vultur
657fcb34635SHoratiu Vultur if (mrp->ring_state != state->ring_state)
6589a9f26e8SHoratiu Vultur mrp->ring_transitions++;
6599a9f26e8SHoratiu Vultur
6609a9f26e8SHoratiu Vultur mrp->ring_state = state->ring_state;
6619a9f26e8SHoratiu Vultur
6629a9f26e8SHoratiu Vultur br_mrp_switchdev_set_ring_state(br, mrp, state->ring_state);
6639a9f26e8SHoratiu Vultur
6649a9f26e8SHoratiu Vultur return 0;
6659a9f26e8SHoratiu Vultur }
6669a9f26e8SHoratiu Vultur
6679a9f26e8SHoratiu Vultur /* Set ring role, ring role can be only MRM(Media Redundancy Manager) or
6689a9f26e8SHoratiu Vultur * MRC(Media Redundancy Client).
6699a9f26e8SHoratiu Vultur * note: already called with rtnl_lock
6709a9f26e8SHoratiu Vultur */
br_mrp_set_ring_role(struct net_bridge * br,struct br_mrp_ring_role * role)6719a9f26e8SHoratiu Vultur int br_mrp_set_ring_role(struct net_bridge *br,
6729a9f26e8SHoratiu Vultur struct br_mrp_ring_role *role)
6739a9f26e8SHoratiu Vultur {
6749a9f26e8SHoratiu Vultur struct br_mrp *mrp = br_mrp_find_id(br, role->ring_id);
675cd605d45SHoratiu Vultur enum br_mrp_hw_support support;
6769a9f26e8SHoratiu Vultur
6779a9f26e8SHoratiu Vultur if (!mrp)
6789a9f26e8SHoratiu Vultur return -EINVAL;
6799a9f26e8SHoratiu Vultur
6809a9f26e8SHoratiu Vultur mrp->ring_role = role->ring_role;
6819a9f26e8SHoratiu Vultur
6829a9f26e8SHoratiu Vultur /* If there is an error just bailed out */
683cd605d45SHoratiu Vultur support = br_mrp_switchdev_set_ring_role(br, mrp, role->ring_role);
684cd605d45SHoratiu Vultur if (support == BR_MRP_NONE)
685cd605d45SHoratiu Vultur return -EOPNOTSUPP;
6869a9f26e8SHoratiu Vultur
6879a9f26e8SHoratiu Vultur /* Now detect if the HW actually applied the role or not. If the HW
6889a9f26e8SHoratiu Vultur * applied the role it means that the SW will not to do those operations
6899a9f26e8SHoratiu Vultur * anymore. For example if the role ir MRM then the HW will notify the
6909a9f26e8SHoratiu Vultur * SW when ring is open, but if the is not pushed to the HW the SW will
6919a9f26e8SHoratiu Vultur * need to detect when the ring is open
6929a9f26e8SHoratiu Vultur */
693cd605d45SHoratiu Vultur mrp->ring_role_offloaded = support == BR_MRP_SW ? 0 : 1;
6949a9f26e8SHoratiu Vultur
6959a9f26e8SHoratiu Vultur return 0;
6969a9f26e8SHoratiu Vultur }
6979a9f26e8SHoratiu Vultur
698c6676e7dSHoratiu Vultur /* Start to generate or monitor MRP test frames, the frames are generated by
699c6676e7dSHoratiu Vultur * HW and if it fails, they are generated by the SW.
7009a9f26e8SHoratiu Vultur * note: already called with rtnl_lock
7019a9f26e8SHoratiu Vultur */
br_mrp_start_test(struct net_bridge * br,struct br_mrp_start_test * test)7029a9f26e8SHoratiu Vultur int br_mrp_start_test(struct net_bridge *br,
7039a9f26e8SHoratiu Vultur struct br_mrp_start_test *test)
7049a9f26e8SHoratiu Vultur {
7059a9f26e8SHoratiu Vultur struct br_mrp *mrp = br_mrp_find_id(br, test->ring_id);
706cd605d45SHoratiu Vultur enum br_mrp_hw_support support;
7079a9f26e8SHoratiu Vultur
7089a9f26e8SHoratiu Vultur if (!mrp)
7099a9f26e8SHoratiu Vultur return -EINVAL;
7109a9f26e8SHoratiu Vultur
711c6676e7dSHoratiu Vultur /* Try to push it to the HW and if it fails then continue with SW
712c6676e7dSHoratiu Vultur * implementation and if that also fails then return error.
7139a9f26e8SHoratiu Vultur */
714cd605d45SHoratiu Vultur support = br_mrp_switchdev_send_ring_test(br, mrp, test->interval,
715c6676e7dSHoratiu Vultur test->max_miss, test->period,
716cd605d45SHoratiu Vultur test->monitor);
717cd605d45SHoratiu Vultur if (support == BR_MRP_NONE)
718cd605d45SHoratiu Vultur return -EOPNOTSUPP;
719cd605d45SHoratiu Vultur
720cd605d45SHoratiu Vultur if (support == BR_MRP_HW)
7219a9f26e8SHoratiu Vultur return 0;
7229a9f26e8SHoratiu Vultur
7239a9f26e8SHoratiu Vultur mrp->test_interval = test->interval;
7249a9f26e8SHoratiu Vultur mrp->test_end = jiffies + usecs_to_jiffies(test->period);
7259a9f26e8SHoratiu Vultur mrp->test_max_miss = test->max_miss;
726c6676e7dSHoratiu Vultur mrp->test_monitor = test->monitor;
7279a9f26e8SHoratiu Vultur mrp->test_count_miss = 0;
7289a9f26e8SHoratiu Vultur queue_delayed_work(system_wq, &mrp->test_work,
7299a9f26e8SHoratiu Vultur usecs_to_jiffies(test->interval));
7309a9f26e8SHoratiu Vultur
7319a9f26e8SHoratiu Vultur return 0;
7329a9f26e8SHoratiu Vultur }
7339a9f26e8SHoratiu Vultur
734537ed567SHoratiu Vultur /* Set in state, int state can be only Open or Closed
735537ed567SHoratiu Vultur * note: already called with rtnl_lock
736537ed567SHoratiu Vultur */
br_mrp_set_in_state(struct net_bridge * br,struct br_mrp_in_state * state)737537ed567SHoratiu Vultur int br_mrp_set_in_state(struct net_bridge *br, struct br_mrp_in_state *state)
738537ed567SHoratiu Vultur {
739537ed567SHoratiu Vultur struct br_mrp *mrp = br_mrp_find_in_id(br, state->in_id);
740537ed567SHoratiu Vultur
741537ed567SHoratiu Vultur if (!mrp)
742537ed567SHoratiu Vultur return -EINVAL;
743537ed567SHoratiu Vultur
744fcb34635SHoratiu Vultur if (mrp->in_state != state->in_state)
745537ed567SHoratiu Vultur mrp->in_transitions++;
746537ed567SHoratiu Vultur
747537ed567SHoratiu Vultur mrp->in_state = state->in_state;
748537ed567SHoratiu Vultur
749537ed567SHoratiu Vultur br_mrp_switchdev_set_in_state(br, mrp, state->in_state);
750537ed567SHoratiu Vultur
751537ed567SHoratiu Vultur return 0;
752537ed567SHoratiu Vultur }
753537ed567SHoratiu Vultur
754537ed567SHoratiu Vultur /* Set in role, in role can be only MIM(Media Interconnection Manager) or
755537ed567SHoratiu Vultur * MIC(Media Interconnection Client).
756537ed567SHoratiu Vultur * note: already called with rtnl_lock
757537ed567SHoratiu Vultur */
br_mrp_set_in_role(struct net_bridge * br,struct br_mrp_in_role * role)758537ed567SHoratiu Vultur int br_mrp_set_in_role(struct net_bridge *br, struct br_mrp_in_role *role)
759537ed567SHoratiu Vultur {
760537ed567SHoratiu Vultur struct br_mrp *mrp = br_mrp_find_id(br, role->ring_id);
761cd605d45SHoratiu Vultur enum br_mrp_hw_support support;
762537ed567SHoratiu Vultur struct net_bridge_port *p;
763537ed567SHoratiu Vultur
764537ed567SHoratiu Vultur if (!mrp)
765537ed567SHoratiu Vultur return -EINVAL;
766537ed567SHoratiu Vultur
767537ed567SHoratiu Vultur if (!br_mrp_get_port(br, role->i_ifindex))
768537ed567SHoratiu Vultur return -EINVAL;
769537ed567SHoratiu Vultur
770537ed567SHoratiu Vultur if (role->in_role == BR_MRP_IN_ROLE_DISABLED) {
771537ed567SHoratiu Vultur u8 state;
772537ed567SHoratiu Vultur
773537ed567SHoratiu Vultur /* It is not allowed to disable a port that doesn't exist */
774537ed567SHoratiu Vultur p = rtnl_dereference(mrp->i_port);
775537ed567SHoratiu Vultur if (!p)
776537ed567SHoratiu Vultur return -EINVAL;
777537ed567SHoratiu Vultur
778537ed567SHoratiu Vultur /* Stop the generating MRP_InTest frames */
779537ed567SHoratiu Vultur cancel_delayed_work_sync(&mrp->in_test_work);
780537ed567SHoratiu Vultur br_mrp_switchdev_send_in_test(br, mrp, 0, 0, 0);
781537ed567SHoratiu Vultur
782537ed567SHoratiu Vultur /* Remove the port */
783537ed567SHoratiu Vultur spin_lock_bh(&br->lock);
784537ed567SHoratiu Vultur state = netif_running(br->dev) ?
785537ed567SHoratiu Vultur BR_STATE_FORWARDING : BR_STATE_DISABLED;
786537ed567SHoratiu Vultur p->state = state;
787537ed567SHoratiu Vultur p->flags &= ~BR_MRP_AWARE;
788537ed567SHoratiu Vultur spin_unlock_bh(&br->lock);
789537ed567SHoratiu Vultur br_mrp_port_switchdev_set_state(p, state);
790537ed567SHoratiu Vultur rcu_assign_pointer(mrp->i_port, NULL);
791537ed567SHoratiu Vultur
792537ed567SHoratiu Vultur mrp->in_role = role->in_role;
793537ed567SHoratiu Vultur mrp->in_id = 0;
794537ed567SHoratiu Vultur
795537ed567SHoratiu Vultur return 0;
796537ed567SHoratiu Vultur }
797537ed567SHoratiu Vultur
798537ed567SHoratiu Vultur /* It is not possible to have the same port part of multiple rings */
799537ed567SHoratiu Vultur if (!br_mrp_unique_ifindex(br, role->i_ifindex))
800537ed567SHoratiu Vultur return -EINVAL;
801537ed567SHoratiu Vultur
802537ed567SHoratiu Vultur /* It is not allowed to set a different interconnect port if the mrp
803537ed567SHoratiu Vultur * instance has already one. First it needs to be disabled and after
804537ed567SHoratiu Vultur * that set the new port
805537ed567SHoratiu Vultur */
806537ed567SHoratiu Vultur if (rcu_access_pointer(mrp->i_port))
807537ed567SHoratiu Vultur return -EINVAL;
808537ed567SHoratiu Vultur
809537ed567SHoratiu Vultur p = br_mrp_get_port(br, role->i_ifindex);
810537ed567SHoratiu Vultur spin_lock_bh(&br->lock);
811537ed567SHoratiu Vultur p->state = BR_STATE_FORWARDING;
812537ed567SHoratiu Vultur p->flags |= BR_MRP_AWARE;
813537ed567SHoratiu Vultur spin_unlock_bh(&br->lock);
814537ed567SHoratiu Vultur rcu_assign_pointer(mrp->i_port, p);
815537ed567SHoratiu Vultur
816537ed567SHoratiu Vultur mrp->in_role = role->in_role;
817537ed567SHoratiu Vultur mrp->in_id = role->in_id;
818537ed567SHoratiu Vultur
819537ed567SHoratiu Vultur /* If there is an error just bailed out */
820cd605d45SHoratiu Vultur support = br_mrp_switchdev_set_in_role(br, mrp, role->in_id,
821537ed567SHoratiu Vultur role->ring_id, role->in_role);
822cd605d45SHoratiu Vultur if (support == BR_MRP_NONE)
823cd605d45SHoratiu Vultur return -EOPNOTSUPP;
824537ed567SHoratiu Vultur
825537ed567SHoratiu Vultur /* Now detect if the HW actually applied the role or not. If the HW
826537ed567SHoratiu Vultur * applied the role it means that the SW will not to do those operations
827537ed567SHoratiu Vultur * anymore. For example if the role is MIM then the HW will notify the
828537ed567SHoratiu Vultur * SW when interconnect ring is open, but if the is not pushed to the HW
829537ed567SHoratiu Vultur * the SW will need to detect when the interconnect ring is open.
830537ed567SHoratiu Vultur */
831cd605d45SHoratiu Vultur mrp->in_role_offloaded = support == BR_MRP_SW ? 0 : 1;
832537ed567SHoratiu Vultur
833537ed567SHoratiu Vultur return 0;
834537ed567SHoratiu Vultur }
835537ed567SHoratiu Vultur
836537ed567SHoratiu Vultur /* Start to generate MRP_InTest frames, the frames are generated by
837537ed567SHoratiu Vultur * HW and if it fails, they are generated by the SW.
838537ed567SHoratiu Vultur * note: already called with rtnl_lock
839537ed567SHoratiu Vultur */
br_mrp_start_in_test(struct net_bridge * br,struct br_mrp_start_in_test * in_test)840537ed567SHoratiu Vultur int br_mrp_start_in_test(struct net_bridge *br,
841537ed567SHoratiu Vultur struct br_mrp_start_in_test *in_test)
842537ed567SHoratiu Vultur {
843537ed567SHoratiu Vultur struct br_mrp *mrp = br_mrp_find_in_id(br, in_test->in_id);
844cd605d45SHoratiu Vultur enum br_mrp_hw_support support;
845537ed567SHoratiu Vultur
846537ed567SHoratiu Vultur if (!mrp)
847537ed567SHoratiu Vultur return -EINVAL;
848537ed567SHoratiu Vultur
849537ed567SHoratiu Vultur if (mrp->in_role != BR_MRP_IN_ROLE_MIM)
850537ed567SHoratiu Vultur return -EINVAL;
851537ed567SHoratiu Vultur
852537ed567SHoratiu Vultur /* Try to push it to the HW and if it fails then continue with SW
853537ed567SHoratiu Vultur * implementation and if that also fails then return error.
854537ed567SHoratiu Vultur */
855cd605d45SHoratiu Vultur support = br_mrp_switchdev_send_in_test(br, mrp, in_test->interval,
856cd605d45SHoratiu Vultur in_test->max_miss,
857cd605d45SHoratiu Vultur in_test->period);
858cd605d45SHoratiu Vultur if (support == BR_MRP_NONE)
859cd605d45SHoratiu Vultur return -EOPNOTSUPP;
860cd605d45SHoratiu Vultur
861cd605d45SHoratiu Vultur if (support == BR_MRP_HW)
862537ed567SHoratiu Vultur return 0;
863537ed567SHoratiu Vultur
864537ed567SHoratiu Vultur mrp->in_test_interval = in_test->interval;
865537ed567SHoratiu Vultur mrp->in_test_end = jiffies + usecs_to_jiffies(in_test->period);
866537ed567SHoratiu Vultur mrp->in_test_max_miss = in_test->max_miss;
867537ed567SHoratiu Vultur mrp->in_test_count_miss = 0;
868537ed567SHoratiu Vultur queue_delayed_work(system_wq, &mrp->in_test_work,
869537ed567SHoratiu Vultur usecs_to_jiffies(in_test->interval));
870537ed567SHoratiu Vultur
871537ed567SHoratiu Vultur return 0;
872537ed567SHoratiu Vultur }
873537ed567SHoratiu Vultur
874efb5b338SMenglong Dong /* Determine if the frame type is a ring frame */
br_mrp_ring_frame(struct sk_buff * skb)875537ed567SHoratiu Vultur static bool br_mrp_ring_frame(struct sk_buff *skb)
876537ed567SHoratiu Vultur {
877537ed567SHoratiu Vultur const struct br_mrp_tlv_hdr *hdr;
878537ed567SHoratiu Vultur struct br_mrp_tlv_hdr _hdr;
879537ed567SHoratiu Vultur
880537ed567SHoratiu Vultur hdr = skb_header_pointer(skb, sizeof(uint16_t), sizeof(_hdr), &_hdr);
881537ed567SHoratiu Vultur if (!hdr)
882537ed567SHoratiu Vultur return false;
883537ed567SHoratiu Vultur
884537ed567SHoratiu Vultur if (hdr->type == BR_MRP_TLV_HEADER_RING_TEST ||
885537ed567SHoratiu Vultur hdr->type == BR_MRP_TLV_HEADER_RING_TOPO ||
886537ed567SHoratiu Vultur hdr->type == BR_MRP_TLV_HEADER_RING_LINK_DOWN ||
887537ed567SHoratiu Vultur hdr->type == BR_MRP_TLV_HEADER_RING_LINK_UP ||
888537ed567SHoratiu Vultur hdr->type == BR_MRP_TLV_HEADER_OPTION)
889537ed567SHoratiu Vultur return true;
890537ed567SHoratiu Vultur
891537ed567SHoratiu Vultur return false;
892537ed567SHoratiu Vultur }
893537ed567SHoratiu Vultur
894efb5b338SMenglong Dong /* Determine if the frame type is an interconnect frame */
br_mrp_in_frame(struct sk_buff * skb)895537ed567SHoratiu Vultur static bool br_mrp_in_frame(struct sk_buff *skb)
896537ed567SHoratiu Vultur {
897537ed567SHoratiu Vultur const struct br_mrp_tlv_hdr *hdr;
898537ed567SHoratiu Vultur struct br_mrp_tlv_hdr _hdr;
899537ed567SHoratiu Vultur
900537ed567SHoratiu Vultur hdr = skb_header_pointer(skb, sizeof(uint16_t), sizeof(_hdr), &_hdr);
901537ed567SHoratiu Vultur if (!hdr)
902537ed567SHoratiu Vultur return false;
903537ed567SHoratiu Vultur
904537ed567SHoratiu Vultur if (hdr->type == BR_MRP_TLV_HEADER_IN_TEST ||
905537ed567SHoratiu Vultur hdr->type == BR_MRP_TLV_HEADER_IN_TOPO ||
906537ed567SHoratiu Vultur hdr->type == BR_MRP_TLV_HEADER_IN_LINK_DOWN ||
907bfd04232SHoratiu Vultur hdr->type == BR_MRP_TLV_HEADER_IN_LINK_UP ||
908bfd04232SHoratiu Vultur hdr->type == BR_MRP_TLV_HEADER_IN_LINK_STATUS)
909537ed567SHoratiu Vultur return true;
910537ed567SHoratiu Vultur
911537ed567SHoratiu Vultur return false;
912537ed567SHoratiu Vultur }
913537ed567SHoratiu Vultur
9149a9f26e8SHoratiu Vultur /* Process only MRP Test frame. All the other MRP frames are processed by
9159a9f26e8SHoratiu Vultur * userspace application
9169a9f26e8SHoratiu Vultur * note: already called with rcu_read_lock
9179a9f26e8SHoratiu Vultur */
br_mrp_mrm_process(struct br_mrp * mrp,struct net_bridge_port * port,struct sk_buff * skb)9189a9f26e8SHoratiu Vultur static void br_mrp_mrm_process(struct br_mrp *mrp, struct net_bridge_port *port,
9199a9f26e8SHoratiu Vultur struct sk_buff *skb)
9209a9f26e8SHoratiu Vultur {
9219a9f26e8SHoratiu Vultur const struct br_mrp_tlv_hdr *hdr;
9229a9f26e8SHoratiu Vultur struct br_mrp_tlv_hdr _hdr;
9239a9f26e8SHoratiu Vultur
9249a9f26e8SHoratiu Vultur /* Each MRP header starts with a version field which is 16 bits.
9259a9f26e8SHoratiu Vultur * Therefore skip the version and get directly the TLV header.
9269a9f26e8SHoratiu Vultur */
9279a9f26e8SHoratiu Vultur hdr = skb_header_pointer(skb, sizeof(uint16_t), sizeof(_hdr), &_hdr);
9289a9f26e8SHoratiu Vultur if (!hdr)
9299a9f26e8SHoratiu Vultur return;
9309a9f26e8SHoratiu Vultur
9319a9f26e8SHoratiu Vultur if (hdr->type != BR_MRP_TLV_HEADER_RING_TEST)
9329a9f26e8SHoratiu Vultur return;
9339a9f26e8SHoratiu Vultur
9349a9f26e8SHoratiu Vultur mrp->test_count_miss = 0;
9359a9f26e8SHoratiu Vultur
9369a9f26e8SHoratiu Vultur /* Notify the userspace that the ring is closed only when the ring is
9379a9f26e8SHoratiu Vultur * not closed
9389a9f26e8SHoratiu Vultur */
9399a9f26e8SHoratiu Vultur if (mrp->ring_state != BR_MRP_RING_STATE_CLOSED)
9404cc625c6SHoratiu Vultur br_mrp_ring_port_open(port->dev, false);
9419a9f26e8SHoratiu Vultur }
9429a9f26e8SHoratiu Vultur
943efb5b338SMenglong Dong /* 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)944c6676e7dSHoratiu Vultur static bool br_mrp_test_better_than_own(struct br_mrp *mrp,
945c6676e7dSHoratiu Vultur struct net_bridge *br,
946c6676e7dSHoratiu Vultur const struct br_mrp_ring_test_hdr *hdr)
947c6676e7dSHoratiu Vultur {
948c6676e7dSHoratiu Vultur u16 prio = be16_to_cpu(hdr->prio);
949c6676e7dSHoratiu Vultur
950c6676e7dSHoratiu Vultur if (prio < mrp->prio ||
951c6676e7dSHoratiu Vultur (prio == mrp->prio &&
952c6676e7dSHoratiu Vultur ether_addr_to_u64(hdr->sa) < ether_addr_to_u64(br->dev->dev_addr)))
953c6676e7dSHoratiu Vultur return true;
954c6676e7dSHoratiu Vultur
955c6676e7dSHoratiu Vultur return false;
956c6676e7dSHoratiu Vultur }
957c6676e7dSHoratiu Vultur
958c6676e7dSHoratiu Vultur /* Process only MRP Test frame. All the other MRP frames are processed by
959c6676e7dSHoratiu Vultur * userspace application
960c6676e7dSHoratiu Vultur * note: already called with rcu_read_lock
961c6676e7dSHoratiu Vultur */
br_mrp_mra_process(struct br_mrp * mrp,struct net_bridge * br,struct net_bridge_port * port,struct sk_buff * skb)962c6676e7dSHoratiu Vultur static void br_mrp_mra_process(struct br_mrp *mrp, struct net_bridge *br,
963c6676e7dSHoratiu Vultur struct net_bridge_port *port,
964c6676e7dSHoratiu Vultur struct sk_buff *skb)
965c6676e7dSHoratiu Vultur {
966c6676e7dSHoratiu Vultur const struct br_mrp_ring_test_hdr *test_hdr;
967c6676e7dSHoratiu Vultur struct br_mrp_ring_test_hdr _test_hdr;
968c6676e7dSHoratiu Vultur const struct br_mrp_tlv_hdr *hdr;
969c6676e7dSHoratiu Vultur struct br_mrp_tlv_hdr _hdr;
970c6676e7dSHoratiu Vultur
971c6676e7dSHoratiu Vultur /* Each MRP header starts with a version field which is 16 bits.
972c6676e7dSHoratiu Vultur * Therefore skip the version and get directly the TLV header.
973c6676e7dSHoratiu Vultur */
974c6676e7dSHoratiu Vultur hdr = skb_header_pointer(skb, sizeof(uint16_t), sizeof(_hdr), &_hdr);
975c6676e7dSHoratiu Vultur if (!hdr)
976c6676e7dSHoratiu Vultur return;
977c6676e7dSHoratiu Vultur
978c6676e7dSHoratiu Vultur if (hdr->type != BR_MRP_TLV_HEADER_RING_TEST)
979c6676e7dSHoratiu Vultur return;
980c6676e7dSHoratiu Vultur
981c6676e7dSHoratiu Vultur test_hdr = skb_header_pointer(skb, sizeof(uint16_t) + sizeof(_hdr),
982c6676e7dSHoratiu Vultur sizeof(_test_hdr), &_test_hdr);
983c6676e7dSHoratiu Vultur if (!test_hdr)
984c6676e7dSHoratiu Vultur return;
985c6676e7dSHoratiu Vultur
986c6676e7dSHoratiu Vultur /* Only frames that have a better priority than the node will
987c6676e7dSHoratiu Vultur * clear the miss counter because otherwise the node will need to behave
988c6676e7dSHoratiu Vultur * as MRM.
989c6676e7dSHoratiu Vultur */
990c6676e7dSHoratiu Vultur if (br_mrp_test_better_than_own(mrp, br, test_hdr))
991c6676e7dSHoratiu Vultur mrp->test_count_miss = 0;
992c6676e7dSHoratiu Vultur }
993c6676e7dSHoratiu Vultur
994537ed567SHoratiu Vultur /* Process only MRP InTest frame. All the other MRP frames are processed by
995537ed567SHoratiu Vultur * userspace application
996537ed567SHoratiu Vultur * note: already called with rcu_read_lock
997537ed567SHoratiu Vultur */
br_mrp_mim_process(struct br_mrp * mrp,struct net_bridge_port * port,struct sk_buff * skb)998537ed567SHoratiu Vultur static bool br_mrp_mim_process(struct br_mrp *mrp, struct net_bridge_port *port,
999537ed567SHoratiu Vultur struct sk_buff *skb)
1000537ed567SHoratiu Vultur {
1001537ed567SHoratiu Vultur const struct br_mrp_in_test_hdr *in_hdr;
1002537ed567SHoratiu Vultur struct br_mrp_in_test_hdr _in_hdr;
1003537ed567SHoratiu Vultur const struct br_mrp_tlv_hdr *hdr;
1004537ed567SHoratiu Vultur struct br_mrp_tlv_hdr _hdr;
1005537ed567SHoratiu Vultur
1006537ed567SHoratiu Vultur /* Each MRP header starts with a version field which is 16 bits.
1007537ed567SHoratiu Vultur * Therefore skip the version and get directly the TLV header.
1008537ed567SHoratiu Vultur */
1009537ed567SHoratiu Vultur hdr = skb_header_pointer(skb, sizeof(uint16_t), sizeof(_hdr), &_hdr);
1010537ed567SHoratiu Vultur if (!hdr)
1011537ed567SHoratiu Vultur return false;
1012537ed567SHoratiu Vultur
1013537ed567SHoratiu Vultur /* The check for InTest frame type was already done */
1014537ed567SHoratiu Vultur in_hdr = skb_header_pointer(skb, sizeof(uint16_t) + sizeof(_hdr),
1015537ed567SHoratiu Vultur sizeof(_in_hdr), &_in_hdr);
1016537ed567SHoratiu Vultur if (!in_hdr)
1017537ed567SHoratiu Vultur return false;
1018537ed567SHoratiu Vultur
1019537ed567SHoratiu Vultur /* It needs to process only it's own InTest frames. */
1020537ed567SHoratiu Vultur if (mrp->in_id != ntohs(in_hdr->id))
1021537ed567SHoratiu Vultur return false;
1022537ed567SHoratiu Vultur
1023537ed567SHoratiu Vultur mrp->in_test_count_miss = 0;
1024537ed567SHoratiu Vultur
1025537ed567SHoratiu Vultur /* Notify the userspace that the ring is closed only when the ring is
1026537ed567SHoratiu Vultur * not closed
1027537ed567SHoratiu Vultur */
1028537ed567SHoratiu Vultur if (mrp->in_state != BR_MRP_IN_STATE_CLOSED)
1029537ed567SHoratiu Vultur br_mrp_in_port_open(port->dev, false);
1030537ed567SHoratiu Vultur
1031537ed567SHoratiu Vultur return true;
1032537ed567SHoratiu Vultur }
1033537ed567SHoratiu Vultur
1034537ed567SHoratiu Vultur /* Get the MRP frame type
1035537ed567SHoratiu Vultur * note: already called with rcu_read_lock
1036537ed567SHoratiu Vultur */
br_mrp_get_frame_type(struct sk_buff * skb)1037537ed567SHoratiu Vultur static u8 br_mrp_get_frame_type(struct sk_buff *skb)
1038537ed567SHoratiu Vultur {
1039537ed567SHoratiu Vultur const struct br_mrp_tlv_hdr *hdr;
1040537ed567SHoratiu Vultur struct br_mrp_tlv_hdr _hdr;
1041537ed567SHoratiu Vultur
1042537ed567SHoratiu Vultur /* Each MRP header starts with a version field which is 16 bits.
1043537ed567SHoratiu Vultur * Therefore skip the version and get directly the TLV header.
1044537ed567SHoratiu Vultur */
1045537ed567SHoratiu Vultur hdr = skb_header_pointer(skb, sizeof(uint16_t), sizeof(_hdr), &_hdr);
1046537ed567SHoratiu Vultur if (!hdr)
1047537ed567SHoratiu Vultur return 0xff;
1048537ed567SHoratiu Vultur
1049537ed567SHoratiu Vultur return hdr->type;
1050537ed567SHoratiu Vultur }
1051537ed567SHoratiu Vultur
br_mrp_mrm_behaviour(struct br_mrp * mrp)1052537ed567SHoratiu Vultur static bool br_mrp_mrm_behaviour(struct br_mrp *mrp)
1053537ed567SHoratiu Vultur {
1054537ed567SHoratiu Vultur if (mrp->ring_role == BR_MRP_RING_ROLE_MRM ||
1055537ed567SHoratiu Vultur (mrp->ring_role == BR_MRP_RING_ROLE_MRA && !mrp->test_monitor))
1056537ed567SHoratiu Vultur return true;
1057537ed567SHoratiu Vultur
1058537ed567SHoratiu Vultur return false;
1059537ed567SHoratiu Vultur }
1060537ed567SHoratiu Vultur
br_mrp_mrc_behaviour(struct br_mrp * mrp)1061537ed567SHoratiu Vultur static bool br_mrp_mrc_behaviour(struct br_mrp *mrp)
1062537ed567SHoratiu Vultur {
1063537ed567SHoratiu Vultur if (mrp->ring_role == BR_MRP_RING_ROLE_MRC ||
1064537ed567SHoratiu Vultur (mrp->ring_role == BR_MRP_RING_ROLE_MRA && mrp->test_monitor))
1065537ed567SHoratiu Vultur return true;
1066537ed567SHoratiu Vultur
1067537ed567SHoratiu Vultur return false;
1068537ed567SHoratiu Vultur }
1069537ed567SHoratiu Vultur
1070537ed567SHoratiu Vultur /* This will just forward the frame to the other mrp ring ports, depending on
1071537ed567SHoratiu Vultur * the frame type, ring role and interconnect role
10729a9f26e8SHoratiu Vultur * note: already called with rcu_read_lock
10739a9f26e8SHoratiu Vultur */
br_mrp_rcv(struct net_bridge_port * p,struct sk_buff * skb,struct net_device * dev)10749a9f26e8SHoratiu Vultur static int br_mrp_rcv(struct net_bridge_port *p,
10759a9f26e8SHoratiu Vultur struct sk_buff *skb, struct net_device *dev)
10769a9f26e8SHoratiu Vultur {
1077537ed567SHoratiu Vultur struct net_bridge_port *p_port, *s_port, *i_port = NULL;
1078537ed567SHoratiu Vultur struct net_bridge_port *p_dst, *s_dst, *i_dst = NULL;
10799a9f26e8SHoratiu Vultur struct net_bridge *br;
10809a9f26e8SHoratiu Vultur struct br_mrp *mrp;
10819a9f26e8SHoratiu Vultur
10829a9f26e8SHoratiu Vultur /* If port is disabled don't accept any frames */
10839a9f26e8SHoratiu Vultur if (p->state == BR_STATE_DISABLED)
10849a9f26e8SHoratiu Vultur return 0;
10859a9f26e8SHoratiu Vultur
10869a9f26e8SHoratiu Vultur br = p->br;
10879a9f26e8SHoratiu Vultur mrp = br_mrp_find_port(br, p);
10889a9f26e8SHoratiu Vultur if (unlikely(!mrp))
10899a9f26e8SHoratiu Vultur return 0;
10909a9f26e8SHoratiu Vultur
10919a9f26e8SHoratiu Vultur p_port = rcu_dereference(mrp->p_port);
10929a9f26e8SHoratiu Vultur if (!p_port)
10939a9f26e8SHoratiu Vultur return 0;
1094537ed567SHoratiu Vultur p_dst = p_port;
10959a9f26e8SHoratiu Vultur
10969a9f26e8SHoratiu Vultur s_port = rcu_dereference(mrp->s_port);
10979a9f26e8SHoratiu Vultur if (!s_port)
10989a9f26e8SHoratiu Vultur return 0;
1099537ed567SHoratiu Vultur s_dst = s_port;
11009a9f26e8SHoratiu Vultur
1101537ed567SHoratiu Vultur /* If the frame is a ring frame then it is not required to check the
1102537ed567SHoratiu Vultur * interconnect role and ports to process or forward the frame
1103537ed567SHoratiu Vultur */
1104537ed567SHoratiu Vultur if (br_mrp_ring_frame(skb)) {
11059a9f26e8SHoratiu Vultur /* If the role is MRM then don't forward the frames */
11069a9f26e8SHoratiu Vultur if (mrp->ring_role == BR_MRP_RING_ROLE_MRM) {
11079a9f26e8SHoratiu Vultur br_mrp_mrm_process(mrp, p, skb);
1108537ed567SHoratiu Vultur goto no_forward;
11099a9f26e8SHoratiu Vultur }
11109a9f26e8SHoratiu Vultur
1111537ed567SHoratiu Vultur /* If the role is MRA then don't forward the frames if it
1112537ed567SHoratiu Vultur * behaves as MRM node
1113c6676e7dSHoratiu Vultur */
1114c6676e7dSHoratiu Vultur if (mrp->ring_role == BR_MRP_RING_ROLE_MRA) {
1115c6676e7dSHoratiu Vultur if (!mrp->test_monitor) {
1116c6676e7dSHoratiu Vultur br_mrp_mrm_process(mrp, p, skb);
1117537ed567SHoratiu Vultur goto no_forward;
1118c6676e7dSHoratiu Vultur }
1119c6676e7dSHoratiu Vultur
1120c6676e7dSHoratiu Vultur br_mrp_mra_process(mrp, br, p, skb);
1121c6676e7dSHoratiu Vultur }
1122c6676e7dSHoratiu Vultur
1123537ed567SHoratiu Vultur goto forward;
1124537ed567SHoratiu Vultur }
11259a9f26e8SHoratiu Vultur
1126537ed567SHoratiu Vultur if (br_mrp_in_frame(skb)) {
1127537ed567SHoratiu Vultur u8 in_type = br_mrp_get_frame_type(skb);
11289a9f26e8SHoratiu Vultur
1129537ed567SHoratiu Vultur i_port = rcu_dereference(mrp->i_port);
1130537ed567SHoratiu Vultur i_dst = i_port;
11319a9f26e8SHoratiu Vultur
1132537ed567SHoratiu Vultur /* If the ring port is in block state it should not forward
1133537ed567SHoratiu Vultur * In_Test frames
1134537ed567SHoratiu Vultur */
1135537ed567SHoratiu Vultur if (br_mrp_is_ring_port(p_port, s_port, p) &&
1136537ed567SHoratiu Vultur p->state == BR_STATE_BLOCKING &&
1137537ed567SHoratiu Vultur in_type == BR_MRP_TLV_HEADER_IN_TEST)
1138537ed567SHoratiu Vultur goto no_forward;
11399a9f26e8SHoratiu Vultur
1140537ed567SHoratiu Vultur /* Nodes that behaves as MRM needs to stop forwarding the
1141537ed567SHoratiu Vultur * frames in case the ring is closed, otherwise will be a loop.
1142537ed567SHoratiu Vultur * In this case the frame is no forward between the ring ports.
1143537ed567SHoratiu Vultur */
1144537ed567SHoratiu Vultur if (br_mrp_mrm_behaviour(mrp) &&
1145537ed567SHoratiu Vultur br_mrp_is_ring_port(p_port, s_port, p) &&
1146537ed567SHoratiu Vultur (s_port->state != BR_STATE_FORWARDING ||
1147537ed567SHoratiu Vultur p_port->state != BR_STATE_FORWARDING)) {
1148537ed567SHoratiu Vultur p_dst = NULL;
1149537ed567SHoratiu Vultur s_dst = NULL;
1150537ed567SHoratiu Vultur }
1151537ed567SHoratiu Vultur
1152537ed567SHoratiu Vultur /* A node that behaves as MRC and doesn't have a interconnect
1153537ed567SHoratiu Vultur * role then it should forward all frames between the ring ports
1154537ed567SHoratiu Vultur * because it doesn't have an interconnect port
1155537ed567SHoratiu Vultur */
1156537ed567SHoratiu Vultur if (br_mrp_mrc_behaviour(mrp) &&
1157537ed567SHoratiu Vultur mrp->in_role == BR_MRP_IN_ROLE_DISABLED)
1158537ed567SHoratiu Vultur goto forward;
1159537ed567SHoratiu Vultur
1160537ed567SHoratiu Vultur if (mrp->in_role == BR_MRP_IN_ROLE_MIM) {
1161537ed567SHoratiu Vultur if (in_type == BR_MRP_TLV_HEADER_IN_TEST) {
1162537ed567SHoratiu Vultur /* MIM should not forward it's own InTest
1163537ed567SHoratiu Vultur * frames
1164537ed567SHoratiu Vultur */
1165537ed567SHoratiu Vultur if (br_mrp_mim_process(mrp, p, skb)) {
1166537ed567SHoratiu Vultur goto no_forward;
1167537ed567SHoratiu Vultur } else {
1168537ed567SHoratiu Vultur if (br_mrp_is_ring_port(p_port, s_port,
1169537ed567SHoratiu Vultur p))
1170537ed567SHoratiu Vultur i_dst = NULL;
1171537ed567SHoratiu Vultur
1172537ed567SHoratiu Vultur if (br_mrp_is_in_port(i_port, p))
1173537ed567SHoratiu Vultur goto no_forward;
1174537ed567SHoratiu Vultur }
1175537ed567SHoratiu Vultur } else {
1176bfd04232SHoratiu Vultur /* MIM should forward IntLinkChange/Status and
1177537ed567SHoratiu Vultur * IntTopoChange between ring ports but MIM
1178bfd04232SHoratiu Vultur * should not forward IntLinkChange/Status and
1179537ed567SHoratiu Vultur * IntTopoChange if the frame was received at
1180537ed567SHoratiu Vultur * the interconnect port
1181537ed567SHoratiu Vultur */
1182537ed567SHoratiu Vultur if (br_mrp_is_ring_port(p_port, s_port, p))
1183537ed567SHoratiu Vultur i_dst = NULL;
1184537ed567SHoratiu Vultur
1185537ed567SHoratiu Vultur if (br_mrp_is_in_port(i_port, p))
1186537ed567SHoratiu Vultur goto no_forward;
1187537ed567SHoratiu Vultur }
1188537ed567SHoratiu Vultur }
1189537ed567SHoratiu Vultur
1190537ed567SHoratiu Vultur if (mrp->in_role == BR_MRP_IN_ROLE_MIC) {
1191537ed567SHoratiu Vultur /* MIC should forward InTest frames on all ports
1192537ed567SHoratiu Vultur * regardless of the received port
1193537ed567SHoratiu Vultur */
1194537ed567SHoratiu Vultur if (in_type == BR_MRP_TLV_HEADER_IN_TEST)
1195537ed567SHoratiu Vultur goto forward;
1196537ed567SHoratiu Vultur
1197537ed567SHoratiu Vultur /* MIC should forward IntLinkChange frames only if they
1198537ed567SHoratiu Vultur * are received on ring ports to all the ports
1199537ed567SHoratiu Vultur */
1200537ed567SHoratiu Vultur if (br_mrp_is_ring_port(p_port, s_port, p) &&
1201537ed567SHoratiu Vultur (in_type == BR_MRP_TLV_HEADER_IN_LINK_UP ||
1202537ed567SHoratiu Vultur in_type == BR_MRP_TLV_HEADER_IN_LINK_DOWN))
1203537ed567SHoratiu Vultur goto forward;
1204537ed567SHoratiu Vultur
1205bfd04232SHoratiu Vultur /* MIC should forward IntLinkStatus frames only to
1206bfd04232SHoratiu Vultur * interconnect port if it was received on a ring port.
1207bfd04232SHoratiu Vultur * If it is received on interconnect port then, it
1208bfd04232SHoratiu Vultur * should be forward on both ring ports
1209bfd04232SHoratiu Vultur */
1210bfd04232SHoratiu Vultur if (br_mrp_is_ring_port(p_port, s_port, p) &&
1211bfd04232SHoratiu Vultur in_type == BR_MRP_TLV_HEADER_IN_LINK_STATUS) {
1212bfd04232SHoratiu Vultur p_dst = NULL;
1213bfd04232SHoratiu Vultur s_dst = NULL;
1214bfd04232SHoratiu Vultur }
1215bfd04232SHoratiu Vultur
1216537ed567SHoratiu Vultur /* Should forward the InTopo frames only between the
1217537ed567SHoratiu Vultur * ring ports
1218537ed567SHoratiu Vultur */
1219537ed567SHoratiu Vultur if (in_type == BR_MRP_TLV_HEADER_IN_TOPO) {
1220537ed567SHoratiu Vultur i_dst = NULL;
1221537ed567SHoratiu Vultur goto forward;
1222537ed567SHoratiu Vultur }
1223537ed567SHoratiu Vultur
1224537ed567SHoratiu Vultur /* In all the other cases don't forward the frames */
1225537ed567SHoratiu Vultur goto no_forward;
1226537ed567SHoratiu Vultur }
1227537ed567SHoratiu Vultur }
1228537ed567SHoratiu Vultur
1229537ed567SHoratiu Vultur forward:
1230537ed567SHoratiu Vultur if (p_dst)
1231537ed567SHoratiu Vultur br_forward(p_dst, skb, true, false);
1232537ed567SHoratiu Vultur if (s_dst)
1233537ed567SHoratiu Vultur br_forward(s_dst, skb, true, false);
1234537ed567SHoratiu Vultur if (i_dst)
1235537ed567SHoratiu Vultur br_forward(i_dst, skb, true, false);
1236537ed567SHoratiu Vultur
1237537ed567SHoratiu Vultur no_forward:
12389a9f26e8SHoratiu Vultur return 1;
12399a9f26e8SHoratiu Vultur }
12409a9f26e8SHoratiu Vultur
12419a9f26e8SHoratiu Vultur /* Check if the frame was received on a port that is part of MRP ring
12429a9f26e8SHoratiu Vultur * and if the frame has MRP eth. In that case process the frame otherwise do
12439a9f26e8SHoratiu Vultur * normal forwarding.
12449a9f26e8SHoratiu Vultur * note: already called with rcu_read_lock
12459a9f26e8SHoratiu Vultur */
br_mrp_process(struct net_bridge_port * p,struct sk_buff * skb)124690c628ddSHenrik Bjoernlund static int br_mrp_process(struct net_bridge_port *p, struct sk_buff *skb)
12479a9f26e8SHoratiu Vultur {
12489a9f26e8SHoratiu Vultur /* If there is no MRP instance do normal forwarding */
12499a9f26e8SHoratiu Vultur if (likely(!(p->flags & BR_MRP_AWARE)))
12509a9f26e8SHoratiu Vultur goto out;
12519a9f26e8SHoratiu Vultur
12529a9f26e8SHoratiu Vultur return br_mrp_rcv(p, skb, p->dev);
12539a9f26e8SHoratiu Vultur out:
12549a9f26e8SHoratiu Vultur return 0;
12559a9f26e8SHoratiu Vultur }
12569a9f26e8SHoratiu Vultur
br_mrp_enabled(struct net_bridge * br)12579a9f26e8SHoratiu Vultur bool br_mrp_enabled(struct net_bridge *br)
12589a9f26e8SHoratiu Vultur {
12590169b820SHoratiu Vultur return !hlist_empty(&br->mrp_list);
12609a9f26e8SHoratiu Vultur }
1261