1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Bridge per vlan tunnel port dst_metadata handling code
6 * Roopa Prabhu <roopa@cumulusnetworks.com>
9 #include <linux/kernel.h>
10 #include <linux/netdevice.h>
11 #include <linux/rtnetlink.h>
12 #include <linux/slab.h>
13 #include <net/switchdev.h>
14 #include <net/dst_metadata.h>
16 #include "br_private.h"
17 #include "br_private_tunnel.h"
19 static inline int br_vlan_tunid_cmp(struct rhashtable_compare_arg *arg,
22 const struct net_bridge_vlan *vle = ptr;
23 __be64 tunid = *(__be64 *)arg->key;
25 return vle->tinfo.tunnel_id != tunid;
28 static const struct rhashtable_params br_vlan_tunnel_rht_params = {
29 .head_offset = offsetof(struct net_bridge_vlan, tnode),
30 .key_offset = offsetof(struct net_bridge_vlan, tinfo.tunnel_id),
31 .key_len = sizeof(__be64),
33 .obj_cmpfn = br_vlan_tunid_cmp,
34 .automatic_shrinking = true,
37 static struct net_bridge_vlan *br_vlan_tunnel_lookup(struct rhashtable *tbl,
40 return rhashtable_lookup_fast(tbl, &tunnel_id,
41 br_vlan_tunnel_rht_params);
44 static void vlan_tunnel_info_release(struct net_bridge_vlan *vlan)
46 struct metadata_dst *tdst = rtnl_dereference(vlan->tinfo.tunnel_dst);
48 WRITE_ONCE(vlan->tinfo.tunnel_id, 0);
49 RCU_INIT_POINTER(vlan->tinfo.tunnel_dst, NULL);
50 dst_release(&tdst->dst);
53 void vlan_tunnel_info_del(struct net_bridge_vlan_group *vg,
54 struct net_bridge_vlan *vlan)
56 if (!rcu_access_pointer(vlan->tinfo.tunnel_dst))
58 rhashtable_remove_fast(&vg->tunnel_hash, &vlan->tnode,
59 br_vlan_tunnel_rht_params);
60 vlan_tunnel_info_release(vlan);
63 static int __vlan_tunnel_info_add(struct net_bridge_vlan_group *vg,
64 struct net_bridge_vlan *vlan, u32 tun_id)
66 struct metadata_dst *metadata = rtnl_dereference(vlan->tinfo.tunnel_dst);
67 __be64 key = key32_to_tunnel_id(cpu_to_be32(tun_id));
73 metadata = __ip_tun_set_dst(0, 0, 0, 0, 0, TUNNEL_KEY,
78 metadata->u.tun_info.mode |= IP_TUNNEL_INFO_TX | IP_TUNNEL_INFO_BRIDGE;
79 rcu_assign_pointer(vlan->tinfo.tunnel_dst, metadata);
80 WRITE_ONCE(vlan->tinfo.tunnel_id, key);
82 err = rhashtable_lookup_insert_fast(&vg->tunnel_hash, &vlan->tnode,
83 br_vlan_tunnel_rht_params);
89 vlan_tunnel_info_release(vlan);
94 /* Must be protected by RTNL.
95 * Must be called with vid in range from 1 to 4094 inclusive.
97 int nbp_vlan_tunnel_info_add(const struct net_bridge_port *port, u16 vid,
100 struct net_bridge_vlan_group *vg;
101 struct net_bridge_vlan *vlan;
105 vg = nbp_vlan_group(port);
106 vlan = br_vlan_find(vg, vid);
110 return __vlan_tunnel_info_add(vg, vlan, tun_id);
113 /* Must be protected by RTNL.
114 * Must be called with vid in range from 1 to 4094 inclusive.
116 int nbp_vlan_tunnel_info_delete(const struct net_bridge_port *port, u16 vid)
118 struct net_bridge_vlan_group *vg;
119 struct net_bridge_vlan *v;
123 vg = nbp_vlan_group(port);
124 v = br_vlan_find(vg, vid);
128 vlan_tunnel_info_del(vg, v);
133 static void __vlan_tunnel_info_flush(struct net_bridge_vlan_group *vg)
135 struct net_bridge_vlan *vlan, *tmp;
137 list_for_each_entry_safe(vlan, tmp, &vg->vlan_list, vlist)
138 vlan_tunnel_info_del(vg, vlan);
141 void nbp_vlan_tunnel_info_flush(struct net_bridge_port *port)
143 struct net_bridge_vlan_group *vg;
147 vg = nbp_vlan_group(port);
148 __vlan_tunnel_info_flush(vg);
151 int vlan_tunnel_init(struct net_bridge_vlan_group *vg)
153 return rhashtable_init(&vg->tunnel_hash, &br_vlan_tunnel_rht_params);
156 void vlan_tunnel_deinit(struct net_bridge_vlan_group *vg)
158 rhashtable_destroy(&vg->tunnel_hash);
161 void br_handle_ingress_vlan_tunnel(struct sk_buff *skb,
162 struct net_bridge_port *p,
163 struct net_bridge_vlan_group *vg)
165 struct ip_tunnel_info *tinfo = skb_tunnel_info(skb);
166 struct net_bridge_vlan *vlan;
171 /* if already tagged, ignore */
172 if (skb_vlan_tagged(skb))
175 /* lookup vid, given tunnel id */
176 vlan = br_vlan_tunnel_lookup(&vg->tunnel_hash, tinfo->key.tun_id);
182 __vlan_hwaccel_put_tag(skb, p->br->vlan_proto, vlan->vid);
185 int br_handle_egress_vlan_tunnel(struct sk_buff *skb,
186 struct net_bridge_vlan *vlan)
188 struct metadata_dst *tunnel_dst;
195 tunnel_id = READ_ONCE(vlan->tinfo.tunnel_id);
196 if (!tunnel_id || unlikely(!skb_vlan_tag_present(skb)))
200 err = skb_vlan_pop(skb);
204 if (BR_INPUT_SKB_CB(skb)->backup_nhid) {
205 tunnel_dst = __ip_tun_set_dst(0, 0, 0, 0, 0, TUNNEL_KEY,
210 tunnel_dst->u.tun_info.mode |= IP_TUNNEL_INFO_TX |
211 IP_TUNNEL_INFO_BRIDGE;
212 tunnel_dst->u.tun_info.key.nhid =
213 BR_INPUT_SKB_CB(skb)->backup_nhid;
214 skb_dst_set(skb, &tunnel_dst->dst);
219 tunnel_dst = rcu_dereference(vlan->tinfo.tunnel_dst);
220 if (tunnel_dst && dst_hold_safe(&tunnel_dst->dst))
221 skb_dst_set(skb, &tunnel_dst->dst);