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
));
68 IP_TUNNEL_DECLARE_FLAGS(flags
) = { };
74 __set_bit(IP_TUNNEL_KEY_BIT
, flags
);
75 metadata
= __ip_tun_set_dst(0, 0, 0, 0, 0, flags
, key
, 0);
79 metadata
->u
.tun_info
.mode
|= IP_TUNNEL_INFO_TX
| IP_TUNNEL_INFO_BRIDGE
;
80 rcu_assign_pointer(vlan
->tinfo
.tunnel_dst
, metadata
);
81 WRITE_ONCE(vlan
->tinfo
.tunnel_id
, key
);
83 err
= rhashtable_lookup_insert_fast(&vg
->tunnel_hash
, &vlan
->tnode
,
84 br_vlan_tunnel_rht_params
);
90 vlan_tunnel_info_release(vlan
);
95 /* Must be protected by RTNL.
96 * Must be called with vid in range from 1 to 4094 inclusive.
98 int nbp_vlan_tunnel_info_add(const struct net_bridge_port
*port
, u16 vid
,
101 struct net_bridge_vlan_group
*vg
;
102 struct net_bridge_vlan
*vlan
;
106 vg
= nbp_vlan_group(port
);
107 vlan
= br_vlan_find(vg
, vid
);
111 return __vlan_tunnel_info_add(vg
, vlan
, tun_id
);
114 /* Must be protected by RTNL.
115 * Must be called with vid in range from 1 to 4094 inclusive.
117 int nbp_vlan_tunnel_info_delete(const struct net_bridge_port
*port
, u16 vid
)
119 struct net_bridge_vlan_group
*vg
;
120 struct net_bridge_vlan
*v
;
124 vg
= nbp_vlan_group(port
);
125 v
= br_vlan_find(vg
, vid
);
129 vlan_tunnel_info_del(vg
, v
);
134 static void __vlan_tunnel_info_flush(struct net_bridge_vlan_group
*vg
)
136 struct net_bridge_vlan
*vlan
, *tmp
;
138 list_for_each_entry_safe(vlan
, tmp
, &vg
->vlan_list
, vlist
)
139 vlan_tunnel_info_del(vg
, vlan
);
142 void nbp_vlan_tunnel_info_flush(struct net_bridge_port
*port
)
144 struct net_bridge_vlan_group
*vg
;
148 vg
= nbp_vlan_group(port
);
149 __vlan_tunnel_info_flush(vg
);
152 int vlan_tunnel_init(struct net_bridge_vlan_group
*vg
)
154 return rhashtable_init(&vg
->tunnel_hash
, &br_vlan_tunnel_rht_params
);
157 void vlan_tunnel_deinit(struct net_bridge_vlan_group
*vg
)
159 rhashtable_destroy(&vg
->tunnel_hash
);
162 void br_handle_ingress_vlan_tunnel(struct sk_buff
*skb
,
163 struct net_bridge_port
*p
,
164 struct net_bridge_vlan_group
*vg
)
166 struct ip_tunnel_info
*tinfo
= skb_tunnel_info(skb
);
167 struct net_bridge_vlan
*vlan
;
172 /* if already tagged, ignore */
173 if (skb_vlan_tagged(skb
))
176 /* lookup vid, given tunnel id */
177 vlan
= br_vlan_tunnel_lookup(&vg
->tunnel_hash
, tinfo
->key
.tun_id
);
183 __vlan_hwaccel_put_tag(skb
, p
->br
->vlan_proto
, vlan
->vid
);
186 int br_handle_egress_vlan_tunnel(struct sk_buff
*skb
,
187 struct net_bridge_vlan
*vlan
)
189 IP_TUNNEL_DECLARE_FLAGS(flags
) = { };
190 struct metadata_dst
*tunnel_dst
;
197 tunnel_id
= READ_ONCE(vlan
->tinfo
.tunnel_id
);
198 if (!tunnel_id
|| unlikely(!skb_vlan_tag_present(skb
)))
202 err
= skb_vlan_pop(skb
);
206 if (BR_INPUT_SKB_CB(skb
)->backup_nhid
) {
207 __set_bit(IP_TUNNEL_KEY_BIT
, flags
);
208 tunnel_dst
= __ip_tun_set_dst(0, 0, 0, 0, 0, flags
,
213 tunnel_dst
->u
.tun_info
.mode
|= IP_TUNNEL_INFO_TX
|
214 IP_TUNNEL_INFO_BRIDGE
;
215 tunnel_dst
->u
.tun_info
.key
.nhid
=
216 BR_INPUT_SKB_CB(skb
)->backup_nhid
;
217 skb_dst_set(skb
, &tunnel_dst
->dst
);
222 tunnel_dst
= rcu_dereference(vlan
->tinfo
.tunnel_dst
);
223 if (tunnel_dst
&& dst_hold_safe(&tunnel_dst
->dst
))
224 skb_dst_set(skb
, &tunnel_dst
->dst
);