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 }; 79a9f26e8SHoratiu Vultur 89a9f26e8SHoratiu Vultur static struct net_bridge_port *br_mrp_get_port(struct net_bridge *br, 99a9f26e8SHoratiu Vultur u32 ifindex) 109a9f26e8SHoratiu Vultur { 119a9f26e8SHoratiu Vultur struct net_bridge_port *res = NULL; 129a9f26e8SHoratiu Vultur struct net_bridge_port *port; 139a9f26e8SHoratiu Vultur 149a9f26e8SHoratiu Vultur list_for_each_entry(port, &br->port_list, list) { 159a9f26e8SHoratiu Vultur if (port->dev->ifindex == ifindex) { 169a9f26e8SHoratiu Vultur res = port; 179a9f26e8SHoratiu Vultur break; 189a9f26e8SHoratiu Vultur } 199a9f26e8SHoratiu Vultur } 209a9f26e8SHoratiu Vultur 219a9f26e8SHoratiu Vultur return res; 229a9f26e8SHoratiu Vultur } 239a9f26e8SHoratiu Vultur 249a9f26e8SHoratiu Vultur static struct br_mrp *br_mrp_find_id(struct net_bridge *br, u32 ring_id) 259a9f26e8SHoratiu Vultur { 269a9f26e8SHoratiu Vultur struct br_mrp *res = NULL; 279a9f26e8SHoratiu Vultur struct br_mrp *mrp; 289a9f26e8SHoratiu Vultur 299a9f26e8SHoratiu Vultur list_for_each_entry_rcu(mrp, &br->mrp_list, list, 309a9f26e8SHoratiu Vultur lockdep_rtnl_is_held()) { 319a9f26e8SHoratiu Vultur if (mrp->ring_id == ring_id) { 329a9f26e8SHoratiu Vultur res = mrp; 339a9f26e8SHoratiu Vultur break; 349a9f26e8SHoratiu Vultur } 359a9f26e8SHoratiu Vultur } 369a9f26e8SHoratiu Vultur 379a9f26e8SHoratiu Vultur return res; 389a9f26e8SHoratiu Vultur } 399a9f26e8SHoratiu Vultur 407aa38018SHoratiu Vultur static bool br_mrp_unique_ifindex(struct net_bridge *br, u32 ifindex) 417aa38018SHoratiu Vultur { 427aa38018SHoratiu Vultur struct br_mrp *mrp; 437aa38018SHoratiu Vultur 447aa38018SHoratiu Vultur list_for_each_entry_rcu(mrp, &br->mrp_list, list, 457aa38018SHoratiu Vultur lockdep_rtnl_is_held()) { 467aa38018SHoratiu Vultur struct net_bridge_port *p; 477aa38018SHoratiu Vultur 487aa38018SHoratiu Vultur p = rtnl_dereference(mrp->p_port); 497aa38018SHoratiu Vultur if (p && p->dev->ifindex == ifindex) 507aa38018SHoratiu Vultur return false; 517aa38018SHoratiu Vultur 527aa38018SHoratiu Vultur p = rtnl_dereference(mrp->s_port); 537aa38018SHoratiu Vultur if (p && p->dev->ifindex == ifindex) 547aa38018SHoratiu Vultur return false; 557aa38018SHoratiu Vultur } 567aa38018SHoratiu Vultur 577aa38018SHoratiu Vultur return true; 587aa38018SHoratiu Vultur } 597aa38018SHoratiu Vultur 609a9f26e8SHoratiu Vultur static struct br_mrp *br_mrp_find_port(struct net_bridge *br, 619a9f26e8SHoratiu Vultur struct net_bridge_port *p) 629a9f26e8SHoratiu Vultur { 639a9f26e8SHoratiu Vultur struct br_mrp *res = NULL; 649a9f26e8SHoratiu Vultur struct br_mrp *mrp; 659a9f26e8SHoratiu Vultur 669a9f26e8SHoratiu Vultur list_for_each_entry_rcu(mrp, &br->mrp_list, list, 679a9f26e8SHoratiu Vultur lockdep_rtnl_is_held()) { 689a9f26e8SHoratiu Vultur if (rcu_access_pointer(mrp->p_port) == p || 699a9f26e8SHoratiu Vultur rcu_access_pointer(mrp->s_port) == p) { 709a9f26e8SHoratiu Vultur res = mrp; 719a9f26e8SHoratiu Vultur break; 729a9f26e8SHoratiu Vultur } 739a9f26e8SHoratiu Vultur } 749a9f26e8SHoratiu Vultur 759a9f26e8SHoratiu Vultur return res; 769a9f26e8SHoratiu Vultur } 779a9f26e8SHoratiu Vultur 789a9f26e8SHoratiu Vultur static int br_mrp_next_seq(struct br_mrp *mrp) 799a9f26e8SHoratiu Vultur { 809a9f26e8SHoratiu Vultur mrp->seq_id++; 819a9f26e8SHoratiu Vultur return mrp->seq_id; 829a9f26e8SHoratiu Vultur } 839a9f26e8SHoratiu Vultur 849a9f26e8SHoratiu Vultur static struct sk_buff *br_mrp_skb_alloc(struct net_bridge_port *p, 859a9f26e8SHoratiu Vultur const u8 *src, const u8 *dst) 869a9f26e8SHoratiu Vultur { 879a9f26e8SHoratiu Vultur struct ethhdr *eth_hdr; 889a9f26e8SHoratiu Vultur struct sk_buff *skb; 899a9f26e8SHoratiu Vultur u16 *version; 909a9f26e8SHoratiu Vultur 919a9f26e8SHoratiu Vultur skb = dev_alloc_skb(MRP_MAX_FRAME_LENGTH); 929a9f26e8SHoratiu Vultur if (!skb) 939a9f26e8SHoratiu Vultur return NULL; 949a9f26e8SHoratiu Vultur 959a9f26e8SHoratiu Vultur skb->dev = p->dev; 969a9f26e8SHoratiu Vultur skb->protocol = htons(ETH_P_MRP); 979a9f26e8SHoratiu Vultur skb->priority = MRP_FRAME_PRIO; 989a9f26e8SHoratiu Vultur skb_reserve(skb, sizeof(*eth_hdr)); 999a9f26e8SHoratiu Vultur 1009a9f26e8SHoratiu Vultur eth_hdr = skb_push(skb, sizeof(*eth_hdr)); 1019a9f26e8SHoratiu Vultur ether_addr_copy(eth_hdr->h_dest, dst); 1029a9f26e8SHoratiu Vultur ether_addr_copy(eth_hdr->h_source, src); 1039a9f26e8SHoratiu Vultur eth_hdr->h_proto = htons(ETH_P_MRP); 1049a9f26e8SHoratiu Vultur 1059a9f26e8SHoratiu Vultur version = skb_put(skb, sizeof(*version)); 1069a9f26e8SHoratiu Vultur *version = cpu_to_be16(MRP_VERSION); 1079a9f26e8SHoratiu Vultur 1089a9f26e8SHoratiu Vultur return skb; 1099a9f26e8SHoratiu Vultur } 1109a9f26e8SHoratiu Vultur 1119a9f26e8SHoratiu Vultur static void br_mrp_skb_tlv(struct sk_buff *skb, 1129a9f26e8SHoratiu Vultur enum br_mrp_tlv_header_type type, 1139a9f26e8SHoratiu Vultur u8 length) 1149a9f26e8SHoratiu Vultur { 1159a9f26e8SHoratiu Vultur struct br_mrp_tlv_hdr *hdr; 1169a9f26e8SHoratiu Vultur 1179a9f26e8SHoratiu Vultur hdr = skb_put(skb, sizeof(*hdr)); 1189a9f26e8SHoratiu Vultur hdr->type = type; 1199a9f26e8SHoratiu Vultur hdr->length = length; 1209a9f26e8SHoratiu Vultur } 1219a9f26e8SHoratiu Vultur 1229a9f26e8SHoratiu Vultur static void br_mrp_skb_common(struct sk_buff *skb, struct br_mrp *mrp) 1239a9f26e8SHoratiu Vultur { 1249a9f26e8SHoratiu Vultur struct br_mrp_common_hdr *hdr; 1259a9f26e8SHoratiu Vultur 1269a9f26e8SHoratiu Vultur br_mrp_skb_tlv(skb, BR_MRP_TLV_HEADER_COMMON, sizeof(*hdr)); 1279a9f26e8SHoratiu Vultur 1289a9f26e8SHoratiu Vultur hdr = skb_put(skb, sizeof(*hdr)); 1299a9f26e8SHoratiu Vultur hdr->seq_id = cpu_to_be16(br_mrp_next_seq(mrp)); 1309a9f26e8SHoratiu Vultur memset(hdr->domain, 0xff, MRP_DOMAIN_UUID_LENGTH); 1319a9f26e8SHoratiu Vultur } 1329a9f26e8SHoratiu Vultur 1339a9f26e8SHoratiu Vultur static struct sk_buff *br_mrp_alloc_test_skb(struct br_mrp *mrp, 1349a9f26e8SHoratiu Vultur struct net_bridge_port *p, 1359a9f26e8SHoratiu Vultur enum br_mrp_port_role_type port_role) 1369a9f26e8SHoratiu Vultur { 1379a9f26e8SHoratiu Vultur struct br_mrp_ring_test_hdr *hdr = NULL; 1389a9f26e8SHoratiu Vultur struct sk_buff *skb = NULL; 1399a9f26e8SHoratiu Vultur 1409a9f26e8SHoratiu Vultur if (!p) 1419a9f26e8SHoratiu Vultur return NULL; 1429a9f26e8SHoratiu Vultur 1439a9f26e8SHoratiu Vultur skb = br_mrp_skb_alloc(p, p->dev->dev_addr, mrp_test_dmac); 1449a9f26e8SHoratiu Vultur if (!skb) 1459a9f26e8SHoratiu Vultur return NULL; 1469a9f26e8SHoratiu Vultur 1479a9f26e8SHoratiu Vultur br_mrp_skb_tlv(skb, BR_MRP_TLV_HEADER_RING_TEST, sizeof(*hdr)); 1489a9f26e8SHoratiu Vultur hdr = skb_put(skb, sizeof(*hdr)); 1499a9f26e8SHoratiu Vultur 1504b3a61b0SHoratiu Vultur hdr->prio = cpu_to_be16(mrp->prio); 1519a9f26e8SHoratiu Vultur ether_addr_copy(hdr->sa, p->br->dev->dev_addr); 1529a9f26e8SHoratiu Vultur hdr->port_role = cpu_to_be16(port_role); 1539a9f26e8SHoratiu Vultur hdr->state = cpu_to_be16(mrp->ring_state); 1549a9f26e8SHoratiu Vultur hdr->transitions = cpu_to_be16(mrp->ring_transitions); 1559a9f26e8SHoratiu Vultur hdr->timestamp = cpu_to_be32(jiffies_to_msecs(jiffies)); 1569a9f26e8SHoratiu Vultur 1579a9f26e8SHoratiu Vultur br_mrp_skb_common(skb, mrp); 1589a9f26e8SHoratiu Vultur br_mrp_skb_tlv(skb, BR_MRP_TLV_HEADER_END, 0x0); 1599a9f26e8SHoratiu Vultur 1609a9f26e8SHoratiu Vultur return skb; 1619a9f26e8SHoratiu Vultur } 1629a9f26e8SHoratiu Vultur 1639a9f26e8SHoratiu Vultur static void br_mrp_test_work_expired(struct work_struct *work) 1649a9f26e8SHoratiu Vultur { 1659a9f26e8SHoratiu Vultur struct delayed_work *del_work = to_delayed_work(work); 1669a9f26e8SHoratiu Vultur struct br_mrp *mrp = container_of(del_work, struct br_mrp, test_work); 1679a9f26e8SHoratiu Vultur struct net_bridge_port *p; 1689a9f26e8SHoratiu Vultur bool notify_open = false; 1699a9f26e8SHoratiu Vultur struct sk_buff *skb; 1709a9f26e8SHoratiu Vultur 1719a9f26e8SHoratiu Vultur if (time_before_eq(mrp->test_end, jiffies)) 1729a9f26e8SHoratiu Vultur return; 1739a9f26e8SHoratiu Vultur 1749a9f26e8SHoratiu Vultur if (mrp->test_count_miss < mrp->test_max_miss) { 1759a9f26e8SHoratiu Vultur mrp->test_count_miss++; 1769a9f26e8SHoratiu Vultur } else { 1779a9f26e8SHoratiu Vultur /* Notify that the ring is open only if the ring state is 1789a9f26e8SHoratiu Vultur * closed, otherwise it would continue to notify at every 1799a9f26e8SHoratiu Vultur * interval. 1809a9f26e8SHoratiu Vultur */ 1819a9f26e8SHoratiu Vultur if (mrp->ring_state == BR_MRP_RING_STATE_CLOSED) 1829a9f26e8SHoratiu Vultur notify_open = true; 1839a9f26e8SHoratiu Vultur } 1849a9f26e8SHoratiu Vultur 1859a9f26e8SHoratiu Vultur rcu_read_lock(); 1869a9f26e8SHoratiu Vultur 1879a9f26e8SHoratiu Vultur p = rcu_dereference(mrp->p_port); 1889a9f26e8SHoratiu Vultur if (p) { 1899a9f26e8SHoratiu Vultur skb = br_mrp_alloc_test_skb(mrp, p, BR_MRP_PORT_ROLE_PRIMARY); 1909a9f26e8SHoratiu Vultur if (!skb) 1919a9f26e8SHoratiu Vultur goto out; 1929a9f26e8SHoratiu Vultur 1939a9f26e8SHoratiu Vultur skb_reset_network_header(skb); 1949a9f26e8SHoratiu Vultur dev_queue_xmit(skb); 1959a9f26e8SHoratiu Vultur 1969a9f26e8SHoratiu Vultur if (notify_open && !mrp->ring_role_offloaded) 1979a9f26e8SHoratiu Vultur br_mrp_port_open(p->dev, true); 1989a9f26e8SHoratiu Vultur } 1999a9f26e8SHoratiu Vultur 2009a9f26e8SHoratiu Vultur p = rcu_dereference(mrp->s_port); 2019a9f26e8SHoratiu Vultur if (p) { 2029a9f26e8SHoratiu Vultur skb = br_mrp_alloc_test_skb(mrp, p, BR_MRP_PORT_ROLE_SECONDARY); 2039a9f26e8SHoratiu Vultur if (!skb) 2049a9f26e8SHoratiu Vultur goto out; 2059a9f26e8SHoratiu Vultur 2069a9f26e8SHoratiu Vultur skb_reset_network_header(skb); 2079a9f26e8SHoratiu Vultur dev_queue_xmit(skb); 2089a9f26e8SHoratiu Vultur 2099a9f26e8SHoratiu Vultur if (notify_open && !mrp->ring_role_offloaded) 2109a9f26e8SHoratiu Vultur br_mrp_port_open(p->dev, true); 2119a9f26e8SHoratiu Vultur } 2129a9f26e8SHoratiu Vultur 2139a9f26e8SHoratiu Vultur out: 2149a9f26e8SHoratiu Vultur rcu_read_unlock(); 2159a9f26e8SHoratiu Vultur 2169a9f26e8SHoratiu Vultur queue_delayed_work(system_wq, &mrp->test_work, 2179a9f26e8SHoratiu Vultur usecs_to_jiffies(mrp->test_interval)); 2189a9f26e8SHoratiu Vultur } 2199a9f26e8SHoratiu Vultur 2209a9f26e8SHoratiu Vultur /* Deletes the MRP instance. 2219a9f26e8SHoratiu Vultur * note: called under rtnl_lock 2229a9f26e8SHoratiu Vultur */ 2239a9f26e8SHoratiu Vultur static void br_mrp_del_impl(struct net_bridge *br, struct br_mrp *mrp) 2249a9f26e8SHoratiu Vultur { 2259a9f26e8SHoratiu Vultur struct net_bridge_port *p; 2264fb13499SHoratiu Vultur u8 state; 2279a9f26e8SHoratiu Vultur 2289a9f26e8SHoratiu Vultur /* Stop sending MRP_Test frames */ 2299a9f26e8SHoratiu Vultur cancel_delayed_work_sync(&mrp->test_work); 2309a9f26e8SHoratiu Vultur br_mrp_switchdev_send_ring_test(br, mrp, 0, 0, 0); 2319a9f26e8SHoratiu Vultur 2329a9f26e8SHoratiu Vultur br_mrp_switchdev_del(br, mrp); 2339a9f26e8SHoratiu Vultur 2349a9f26e8SHoratiu Vultur /* Reset the ports */ 2359a9f26e8SHoratiu Vultur p = rtnl_dereference(mrp->p_port); 2369a9f26e8SHoratiu Vultur if (p) { 2379a9f26e8SHoratiu Vultur spin_lock_bh(&br->lock); 2384fb13499SHoratiu Vultur state = netif_running(br->dev) ? 2394fb13499SHoratiu Vultur BR_STATE_FORWARDING : BR_STATE_DISABLED; 2404fb13499SHoratiu Vultur p->state = state; 2419a9f26e8SHoratiu Vultur p->flags &= ~BR_MRP_AWARE; 2429a9f26e8SHoratiu Vultur spin_unlock_bh(&br->lock); 2434fb13499SHoratiu Vultur br_mrp_port_switchdev_set_state(p, state); 2449a9f26e8SHoratiu Vultur rcu_assign_pointer(mrp->p_port, NULL); 2459a9f26e8SHoratiu Vultur } 2469a9f26e8SHoratiu Vultur 2479a9f26e8SHoratiu Vultur p = rtnl_dereference(mrp->s_port); 2489a9f26e8SHoratiu Vultur if (p) { 2499a9f26e8SHoratiu Vultur spin_lock_bh(&br->lock); 2504fb13499SHoratiu Vultur state = netif_running(br->dev) ? 2514fb13499SHoratiu Vultur BR_STATE_FORWARDING : BR_STATE_DISABLED; 2524fb13499SHoratiu Vultur p->state = state; 2539a9f26e8SHoratiu Vultur p->flags &= ~BR_MRP_AWARE; 2549a9f26e8SHoratiu Vultur spin_unlock_bh(&br->lock); 2554fb13499SHoratiu Vultur br_mrp_port_switchdev_set_state(p, state); 2569a9f26e8SHoratiu Vultur rcu_assign_pointer(mrp->s_port, NULL); 2579a9f26e8SHoratiu Vultur } 2589a9f26e8SHoratiu Vultur 2599a9f26e8SHoratiu Vultur list_del_rcu(&mrp->list); 2609a9f26e8SHoratiu Vultur kfree_rcu(mrp, rcu); 2619a9f26e8SHoratiu Vultur } 2629a9f26e8SHoratiu Vultur 2639a9f26e8SHoratiu Vultur /* Adds a new MRP instance. 2649a9f26e8SHoratiu Vultur * note: called under rtnl_lock 2659a9f26e8SHoratiu Vultur */ 2669a9f26e8SHoratiu Vultur int br_mrp_add(struct net_bridge *br, struct br_mrp_instance *instance) 2679a9f26e8SHoratiu Vultur { 2689a9f26e8SHoratiu Vultur struct net_bridge_port *p; 2699a9f26e8SHoratiu Vultur struct br_mrp *mrp; 2709a9f26e8SHoratiu Vultur int err; 2719a9f26e8SHoratiu Vultur 2729a9f26e8SHoratiu Vultur /* If the ring exists, it is not possible to create another one with the 2739a9f26e8SHoratiu Vultur * same ring_id 2749a9f26e8SHoratiu Vultur */ 2759a9f26e8SHoratiu Vultur mrp = br_mrp_find_id(br, instance->ring_id); 2769a9f26e8SHoratiu Vultur if (mrp) 2779a9f26e8SHoratiu Vultur return -EINVAL; 2789a9f26e8SHoratiu Vultur 2799a9f26e8SHoratiu Vultur if (!br_mrp_get_port(br, instance->p_ifindex) || 2809a9f26e8SHoratiu Vultur !br_mrp_get_port(br, instance->s_ifindex)) 2819a9f26e8SHoratiu Vultur return -EINVAL; 2829a9f26e8SHoratiu Vultur 2837aa38018SHoratiu Vultur /* It is not possible to have the same port part of multiple rings */ 2847aa38018SHoratiu Vultur if (!br_mrp_unique_ifindex(br, instance->p_ifindex) || 2857aa38018SHoratiu Vultur !br_mrp_unique_ifindex(br, instance->s_ifindex)) 2867aa38018SHoratiu Vultur return -EINVAL; 2877aa38018SHoratiu Vultur 2889a9f26e8SHoratiu Vultur mrp = kzalloc(sizeof(*mrp), GFP_KERNEL); 2899a9f26e8SHoratiu Vultur if (!mrp) 2909a9f26e8SHoratiu Vultur return -ENOMEM; 2919a9f26e8SHoratiu Vultur 2929a9f26e8SHoratiu Vultur mrp->ring_id = instance->ring_id; 2934b3a61b0SHoratiu Vultur mrp->prio = instance->prio; 2949a9f26e8SHoratiu Vultur 2959a9f26e8SHoratiu Vultur p = br_mrp_get_port(br, instance->p_ifindex); 2969a9f26e8SHoratiu Vultur spin_lock_bh(&br->lock); 2979a9f26e8SHoratiu Vultur p->state = BR_STATE_FORWARDING; 2989a9f26e8SHoratiu Vultur p->flags |= BR_MRP_AWARE; 2999a9f26e8SHoratiu Vultur spin_unlock_bh(&br->lock); 3009a9f26e8SHoratiu Vultur rcu_assign_pointer(mrp->p_port, p); 3019a9f26e8SHoratiu Vultur 3029a9f26e8SHoratiu Vultur p = br_mrp_get_port(br, instance->s_ifindex); 3039a9f26e8SHoratiu Vultur spin_lock_bh(&br->lock); 3049a9f26e8SHoratiu Vultur p->state = BR_STATE_FORWARDING; 3059a9f26e8SHoratiu Vultur p->flags |= BR_MRP_AWARE; 3069a9f26e8SHoratiu Vultur spin_unlock_bh(&br->lock); 3079a9f26e8SHoratiu Vultur rcu_assign_pointer(mrp->s_port, p); 3089a9f26e8SHoratiu Vultur 3099a9f26e8SHoratiu Vultur INIT_DELAYED_WORK(&mrp->test_work, br_mrp_test_work_expired); 3109a9f26e8SHoratiu Vultur list_add_tail_rcu(&mrp->list, &br->mrp_list); 3119a9f26e8SHoratiu Vultur 3129a9f26e8SHoratiu Vultur err = br_mrp_switchdev_add(br, mrp); 3139a9f26e8SHoratiu Vultur if (err) 3149a9f26e8SHoratiu Vultur goto delete_mrp; 3159a9f26e8SHoratiu Vultur 3169a9f26e8SHoratiu Vultur return 0; 3179a9f26e8SHoratiu Vultur 3189a9f26e8SHoratiu Vultur delete_mrp: 3199a9f26e8SHoratiu Vultur br_mrp_del_impl(br, mrp); 3209a9f26e8SHoratiu Vultur 3219a9f26e8SHoratiu Vultur return err; 3229a9f26e8SHoratiu Vultur } 3239a9f26e8SHoratiu Vultur 3249a9f26e8SHoratiu Vultur /* Deletes the MRP instance from which the port is part of 3259a9f26e8SHoratiu Vultur * note: called under rtnl_lock 3269a9f26e8SHoratiu Vultur */ 3279a9f26e8SHoratiu Vultur void br_mrp_port_del(struct net_bridge *br, struct net_bridge_port *p) 3289a9f26e8SHoratiu Vultur { 3299a9f26e8SHoratiu Vultur struct br_mrp *mrp = br_mrp_find_port(br, p); 3309a9f26e8SHoratiu Vultur 3319a9f26e8SHoratiu Vultur /* If the port is not part of a MRP instance just bail out */ 3329a9f26e8SHoratiu Vultur if (!mrp) 3339a9f26e8SHoratiu Vultur return; 3349a9f26e8SHoratiu Vultur 3359a9f26e8SHoratiu Vultur br_mrp_del_impl(br, mrp); 3369a9f26e8SHoratiu Vultur } 3379a9f26e8SHoratiu Vultur 3389a9f26e8SHoratiu Vultur /* Deletes existing MRP instance based on ring_id 3399a9f26e8SHoratiu Vultur * note: called under rtnl_lock 3409a9f26e8SHoratiu Vultur */ 3419a9f26e8SHoratiu Vultur int br_mrp_del(struct net_bridge *br, struct br_mrp_instance *instance) 3429a9f26e8SHoratiu Vultur { 3439a9f26e8SHoratiu Vultur struct br_mrp *mrp = br_mrp_find_id(br, instance->ring_id); 3449a9f26e8SHoratiu Vultur 3459a9f26e8SHoratiu Vultur if (!mrp) 3469a9f26e8SHoratiu Vultur return -EINVAL; 3479a9f26e8SHoratiu Vultur 3489a9f26e8SHoratiu Vultur br_mrp_del_impl(br, mrp); 3499a9f26e8SHoratiu Vultur 3509a9f26e8SHoratiu Vultur return 0; 3519a9f26e8SHoratiu Vultur } 3529a9f26e8SHoratiu Vultur 3539a9f26e8SHoratiu Vultur /* Set port state, port state can be forwarding, blocked or disabled 3549a9f26e8SHoratiu Vultur * note: already called with rtnl_lock 3559a9f26e8SHoratiu Vultur */ 3569a9f26e8SHoratiu Vultur int br_mrp_set_port_state(struct net_bridge_port *p, 3579a9f26e8SHoratiu Vultur enum br_mrp_port_state_type state) 3589a9f26e8SHoratiu Vultur { 3599a9f26e8SHoratiu Vultur if (!p || !(p->flags & BR_MRP_AWARE)) 3609a9f26e8SHoratiu Vultur return -EINVAL; 3619a9f26e8SHoratiu Vultur 3629a9f26e8SHoratiu Vultur spin_lock_bh(&p->br->lock); 3639a9f26e8SHoratiu Vultur 3649a9f26e8SHoratiu Vultur if (state == BR_MRP_PORT_STATE_FORWARDING) 3659a9f26e8SHoratiu Vultur p->state = BR_STATE_FORWARDING; 3669a9f26e8SHoratiu Vultur else 3679a9f26e8SHoratiu Vultur p->state = BR_STATE_BLOCKING; 3689a9f26e8SHoratiu Vultur 3699a9f26e8SHoratiu Vultur spin_unlock_bh(&p->br->lock); 3709a9f26e8SHoratiu Vultur 3719a9f26e8SHoratiu Vultur br_mrp_port_switchdev_set_state(p, state); 3729a9f26e8SHoratiu Vultur 3739a9f26e8SHoratiu Vultur return 0; 3749a9f26e8SHoratiu Vultur } 3759a9f26e8SHoratiu Vultur 3769a9f26e8SHoratiu Vultur /* Set port role, port role can be primary or secondary 3779a9f26e8SHoratiu Vultur * note: already called with rtnl_lock 3789a9f26e8SHoratiu Vultur */ 3799a9f26e8SHoratiu Vultur int br_mrp_set_port_role(struct net_bridge_port *p, 38020f6a05eSHoratiu Vultur enum br_mrp_port_role_type role) 3819a9f26e8SHoratiu Vultur { 3829a9f26e8SHoratiu Vultur struct br_mrp *mrp; 3839a9f26e8SHoratiu Vultur 3849a9f26e8SHoratiu Vultur if (!p || !(p->flags & BR_MRP_AWARE)) 3859a9f26e8SHoratiu Vultur return -EINVAL; 3869a9f26e8SHoratiu Vultur 38720f6a05eSHoratiu Vultur mrp = br_mrp_find_port(p->br, p); 3889a9f26e8SHoratiu Vultur 3899a9f26e8SHoratiu Vultur if (!mrp) 3909a9f26e8SHoratiu Vultur return -EINVAL; 3919a9f26e8SHoratiu Vultur 39220f6a05eSHoratiu Vultur if (role == BR_MRP_PORT_ROLE_PRIMARY) 3939a9f26e8SHoratiu Vultur rcu_assign_pointer(mrp->p_port, p); 3949a9f26e8SHoratiu Vultur else 3959a9f26e8SHoratiu Vultur rcu_assign_pointer(mrp->s_port, p); 3969a9f26e8SHoratiu Vultur 39720f6a05eSHoratiu Vultur br_mrp_port_switchdev_set_role(p, role); 3989a9f26e8SHoratiu Vultur 3999a9f26e8SHoratiu Vultur return 0; 4009a9f26e8SHoratiu Vultur } 4019a9f26e8SHoratiu Vultur 4029a9f26e8SHoratiu Vultur /* Set ring state, ring state can be only Open or Closed 4039a9f26e8SHoratiu Vultur * note: already called with rtnl_lock 4049a9f26e8SHoratiu Vultur */ 4059a9f26e8SHoratiu Vultur int br_mrp_set_ring_state(struct net_bridge *br, 4069a9f26e8SHoratiu Vultur struct br_mrp_ring_state *state) 4079a9f26e8SHoratiu Vultur { 4089a9f26e8SHoratiu Vultur struct br_mrp *mrp = br_mrp_find_id(br, state->ring_id); 4099a9f26e8SHoratiu Vultur 4109a9f26e8SHoratiu Vultur if (!mrp) 4119a9f26e8SHoratiu Vultur return -EINVAL; 4129a9f26e8SHoratiu Vultur 4139a9f26e8SHoratiu Vultur if (mrp->ring_state == BR_MRP_RING_STATE_CLOSED && 4149a9f26e8SHoratiu Vultur state->ring_state != BR_MRP_RING_STATE_CLOSED) 4159a9f26e8SHoratiu Vultur mrp->ring_transitions++; 4169a9f26e8SHoratiu Vultur 4179a9f26e8SHoratiu Vultur mrp->ring_state = state->ring_state; 4189a9f26e8SHoratiu Vultur 4199a9f26e8SHoratiu Vultur br_mrp_switchdev_set_ring_state(br, mrp, state->ring_state); 4209a9f26e8SHoratiu Vultur 4219a9f26e8SHoratiu Vultur return 0; 4229a9f26e8SHoratiu Vultur } 4239a9f26e8SHoratiu Vultur 4249a9f26e8SHoratiu Vultur /* Set ring role, ring role can be only MRM(Media Redundancy Manager) or 4259a9f26e8SHoratiu Vultur * MRC(Media Redundancy Client). 4269a9f26e8SHoratiu Vultur * note: already called with rtnl_lock 4279a9f26e8SHoratiu Vultur */ 4289a9f26e8SHoratiu Vultur int br_mrp_set_ring_role(struct net_bridge *br, 4299a9f26e8SHoratiu Vultur struct br_mrp_ring_role *role) 4309a9f26e8SHoratiu Vultur { 4319a9f26e8SHoratiu Vultur struct br_mrp *mrp = br_mrp_find_id(br, role->ring_id); 4329a9f26e8SHoratiu Vultur int err; 4339a9f26e8SHoratiu Vultur 4349a9f26e8SHoratiu Vultur if (!mrp) 4359a9f26e8SHoratiu Vultur return -EINVAL; 4369a9f26e8SHoratiu Vultur 4379a9f26e8SHoratiu Vultur mrp->ring_role = role->ring_role; 4389a9f26e8SHoratiu Vultur 4399a9f26e8SHoratiu Vultur /* If there is an error just bailed out */ 4409a9f26e8SHoratiu Vultur err = br_mrp_switchdev_set_ring_role(br, mrp, role->ring_role); 4419a9f26e8SHoratiu Vultur if (err && err != -EOPNOTSUPP) 4429a9f26e8SHoratiu Vultur return err; 4439a9f26e8SHoratiu Vultur 4449a9f26e8SHoratiu Vultur /* Now detect if the HW actually applied the role or not. If the HW 4459a9f26e8SHoratiu Vultur * applied the role it means that the SW will not to do those operations 4469a9f26e8SHoratiu Vultur * anymore. For example if the role ir MRM then the HW will notify the 4479a9f26e8SHoratiu Vultur * SW when ring is open, but if the is not pushed to the HW the SW will 4489a9f26e8SHoratiu Vultur * need to detect when the ring is open 4499a9f26e8SHoratiu Vultur */ 4509a9f26e8SHoratiu Vultur mrp->ring_role_offloaded = err == -EOPNOTSUPP ? 0 : 1; 4519a9f26e8SHoratiu Vultur 4529a9f26e8SHoratiu Vultur return 0; 4539a9f26e8SHoratiu Vultur } 4549a9f26e8SHoratiu Vultur 4559a9f26e8SHoratiu Vultur /* Start to generate MRP test frames, the frames are generated by HW and if it 4569a9f26e8SHoratiu Vultur * fails, they are generated by the SW. 4579a9f26e8SHoratiu Vultur * note: already called with rtnl_lock 4589a9f26e8SHoratiu Vultur */ 4599a9f26e8SHoratiu Vultur int br_mrp_start_test(struct net_bridge *br, 4609a9f26e8SHoratiu Vultur struct br_mrp_start_test *test) 4619a9f26e8SHoratiu Vultur { 4629a9f26e8SHoratiu Vultur struct br_mrp *mrp = br_mrp_find_id(br, test->ring_id); 4639a9f26e8SHoratiu Vultur 4649a9f26e8SHoratiu Vultur if (!mrp) 4659a9f26e8SHoratiu Vultur return -EINVAL; 4669a9f26e8SHoratiu Vultur 4679a9f26e8SHoratiu Vultur /* Try to push it to the HW and if it fails then continue to generate in 4689a9f26e8SHoratiu Vultur * SW and if that also fails then return error 4699a9f26e8SHoratiu Vultur */ 4709a9f26e8SHoratiu Vultur if (!br_mrp_switchdev_send_ring_test(br, mrp, test->interval, 4719a9f26e8SHoratiu Vultur test->max_miss, test->period)) 4729a9f26e8SHoratiu Vultur return 0; 4739a9f26e8SHoratiu Vultur 4749a9f26e8SHoratiu Vultur mrp->test_interval = test->interval; 4759a9f26e8SHoratiu Vultur mrp->test_end = jiffies + usecs_to_jiffies(test->period); 4769a9f26e8SHoratiu Vultur mrp->test_max_miss = test->max_miss; 4779a9f26e8SHoratiu Vultur mrp->test_count_miss = 0; 4789a9f26e8SHoratiu Vultur queue_delayed_work(system_wq, &mrp->test_work, 4799a9f26e8SHoratiu Vultur usecs_to_jiffies(test->interval)); 4809a9f26e8SHoratiu Vultur 4819a9f26e8SHoratiu Vultur return 0; 4829a9f26e8SHoratiu Vultur } 4839a9f26e8SHoratiu Vultur 4849a9f26e8SHoratiu Vultur /* Process only MRP Test frame. All the other MRP frames are processed by 4859a9f26e8SHoratiu Vultur * userspace application 4869a9f26e8SHoratiu Vultur * note: already called with rcu_read_lock 4879a9f26e8SHoratiu Vultur */ 4889a9f26e8SHoratiu Vultur static void br_mrp_mrm_process(struct br_mrp *mrp, struct net_bridge_port *port, 4899a9f26e8SHoratiu Vultur struct sk_buff *skb) 4909a9f26e8SHoratiu Vultur { 4919a9f26e8SHoratiu Vultur const struct br_mrp_tlv_hdr *hdr; 4929a9f26e8SHoratiu Vultur struct br_mrp_tlv_hdr _hdr; 4939a9f26e8SHoratiu Vultur 4949a9f26e8SHoratiu Vultur /* Each MRP header starts with a version field which is 16 bits. 4959a9f26e8SHoratiu Vultur * Therefore skip the version and get directly the TLV header. 4969a9f26e8SHoratiu Vultur */ 4979a9f26e8SHoratiu Vultur hdr = skb_header_pointer(skb, sizeof(uint16_t), sizeof(_hdr), &_hdr); 4989a9f26e8SHoratiu Vultur if (!hdr) 4999a9f26e8SHoratiu Vultur return; 5009a9f26e8SHoratiu Vultur 5019a9f26e8SHoratiu Vultur if (hdr->type != BR_MRP_TLV_HEADER_RING_TEST) 5029a9f26e8SHoratiu Vultur return; 5039a9f26e8SHoratiu Vultur 5049a9f26e8SHoratiu Vultur mrp->test_count_miss = 0; 5059a9f26e8SHoratiu Vultur 5069a9f26e8SHoratiu Vultur /* Notify the userspace that the ring is closed only when the ring is 5079a9f26e8SHoratiu Vultur * not closed 5089a9f26e8SHoratiu Vultur */ 5099a9f26e8SHoratiu Vultur if (mrp->ring_state != BR_MRP_RING_STATE_CLOSED) 5109a9f26e8SHoratiu Vultur br_mrp_port_open(port->dev, false); 5119a9f26e8SHoratiu Vultur } 5129a9f26e8SHoratiu Vultur 5139a9f26e8SHoratiu Vultur /* This will just forward the frame to the other mrp ring port(MRC role) or will 5149a9f26e8SHoratiu Vultur * not do anything. 5159a9f26e8SHoratiu Vultur * note: already called with rcu_read_lock 5169a9f26e8SHoratiu Vultur */ 5179a9f26e8SHoratiu Vultur static int br_mrp_rcv(struct net_bridge_port *p, 5189a9f26e8SHoratiu Vultur struct sk_buff *skb, struct net_device *dev) 5199a9f26e8SHoratiu Vultur { 5209a9f26e8SHoratiu Vultur struct net_device *s_dev, *p_dev, *d_dev; 5219a9f26e8SHoratiu Vultur struct net_bridge_port *p_port, *s_port; 5229a9f26e8SHoratiu Vultur struct net_bridge *br; 5239a9f26e8SHoratiu Vultur struct sk_buff *nskb; 5249a9f26e8SHoratiu Vultur struct br_mrp *mrp; 5259a9f26e8SHoratiu Vultur 5269a9f26e8SHoratiu Vultur /* If port is disabled don't accept any frames */ 5279a9f26e8SHoratiu Vultur if (p->state == BR_STATE_DISABLED) 5289a9f26e8SHoratiu Vultur return 0; 5299a9f26e8SHoratiu Vultur 5309a9f26e8SHoratiu Vultur br = p->br; 5319a9f26e8SHoratiu Vultur mrp = br_mrp_find_port(br, p); 5329a9f26e8SHoratiu Vultur if (unlikely(!mrp)) 5339a9f26e8SHoratiu Vultur return 0; 5349a9f26e8SHoratiu Vultur 5359a9f26e8SHoratiu Vultur p_port = rcu_dereference(mrp->p_port); 5369a9f26e8SHoratiu Vultur if (!p_port) 5379a9f26e8SHoratiu Vultur return 0; 5389a9f26e8SHoratiu Vultur 5399a9f26e8SHoratiu Vultur s_port = rcu_dereference(mrp->s_port); 5409a9f26e8SHoratiu Vultur if (!s_port) 5419a9f26e8SHoratiu Vultur return 0; 5429a9f26e8SHoratiu Vultur 5439a9f26e8SHoratiu Vultur /* If the role is MRM then don't forward the frames */ 5449a9f26e8SHoratiu Vultur if (mrp->ring_role == BR_MRP_RING_ROLE_MRM) { 5459a9f26e8SHoratiu Vultur br_mrp_mrm_process(mrp, p, skb); 5469a9f26e8SHoratiu Vultur return 1; 5479a9f26e8SHoratiu Vultur } 5489a9f26e8SHoratiu Vultur 5499a9f26e8SHoratiu Vultur /* Clone the frame and forward it on the other MRP port */ 5509a9f26e8SHoratiu Vultur nskb = skb_clone(skb, GFP_ATOMIC); 5519a9f26e8SHoratiu Vultur if (!nskb) 5529a9f26e8SHoratiu Vultur return 0; 5539a9f26e8SHoratiu Vultur 5549a9f26e8SHoratiu Vultur p_dev = p_port->dev; 5559a9f26e8SHoratiu Vultur s_dev = s_port->dev; 5569a9f26e8SHoratiu Vultur 5579a9f26e8SHoratiu Vultur if (p_dev == dev) 5589a9f26e8SHoratiu Vultur d_dev = s_dev; 5599a9f26e8SHoratiu Vultur else 5609a9f26e8SHoratiu Vultur d_dev = p_dev; 5619a9f26e8SHoratiu Vultur 5629a9f26e8SHoratiu Vultur nskb->dev = d_dev; 5639a9f26e8SHoratiu Vultur skb_push(nskb, ETH_HLEN); 5649a9f26e8SHoratiu Vultur dev_queue_xmit(nskb); 5659a9f26e8SHoratiu Vultur 5669a9f26e8SHoratiu Vultur return 1; 5679a9f26e8SHoratiu Vultur } 5689a9f26e8SHoratiu Vultur 5699a9f26e8SHoratiu Vultur /* Check if the frame was received on a port that is part of MRP ring 5709a9f26e8SHoratiu Vultur * and if the frame has MRP eth. In that case process the frame otherwise do 5719a9f26e8SHoratiu Vultur * normal forwarding. 5729a9f26e8SHoratiu Vultur * note: already called with rcu_read_lock 5739a9f26e8SHoratiu Vultur */ 5749a9f26e8SHoratiu Vultur int br_mrp_process(struct net_bridge_port *p, struct sk_buff *skb) 5759a9f26e8SHoratiu Vultur { 5769a9f26e8SHoratiu Vultur /* If there is no MRP instance do normal forwarding */ 5779a9f26e8SHoratiu Vultur if (likely(!(p->flags & BR_MRP_AWARE))) 5789a9f26e8SHoratiu Vultur goto out; 5799a9f26e8SHoratiu Vultur 5809a9f26e8SHoratiu Vultur if (unlikely(skb->protocol == htons(ETH_P_MRP))) 5819a9f26e8SHoratiu Vultur return br_mrp_rcv(p, skb, p->dev); 5829a9f26e8SHoratiu Vultur 5839a9f26e8SHoratiu Vultur out: 5849a9f26e8SHoratiu Vultur return 0; 5859a9f26e8SHoratiu Vultur } 5869a9f26e8SHoratiu Vultur 5879a9f26e8SHoratiu Vultur bool br_mrp_enabled(struct net_bridge *br) 5889a9f26e8SHoratiu Vultur { 5899a9f26e8SHoratiu Vultur return !list_empty(&br->mrp_list); 5909a9f26e8SHoratiu Vultur } 591