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 void vlan_tunnel_info_del(struct net_bridge_vlan_group
*vg
,
45 struct net_bridge_vlan
*vlan
)
47 if (!vlan
->tinfo
.tunnel_dst
)
49 rhashtable_remove_fast(&vg
->tunnel_hash
, &vlan
->tnode
,
50 br_vlan_tunnel_rht_params
);
51 vlan
->tinfo
.tunnel_id
= 0;
52 dst_release(&vlan
->tinfo
.tunnel_dst
->dst
);
53 vlan
->tinfo
.tunnel_dst
= NULL
;
56 static int __vlan_tunnel_info_add(struct net_bridge_vlan_group
*vg
,
57 struct net_bridge_vlan
*vlan
, u32 tun_id
)
59 struct metadata_dst
*metadata
= NULL
;
60 __be64 key
= key32_to_tunnel_id(cpu_to_be32(tun_id
));
63 if (vlan
->tinfo
.tunnel_dst
)
66 metadata
= __ip_tun_set_dst(0, 0, 0, 0, 0, TUNNEL_KEY
,
71 metadata
->u
.tun_info
.mode
|= IP_TUNNEL_INFO_TX
| IP_TUNNEL_INFO_BRIDGE
;
72 vlan
->tinfo
.tunnel_dst
= metadata
;
73 vlan
->tinfo
.tunnel_id
= key
;
75 err
= rhashtable_lookup_insert_fast(&vg
->tunnel_hash
, &vlan
->tnode
,
76 br_vlan_tunnel_rht_params
);
82 dst_release(&vlan
->tinfo
.tunnel_dst
->dst
);
83 vlan
->tinfo
.tunnel_dst
= NULL
;
84 vlan
->tinfo
.tunnel_id
= 0;
89 /* Must be protected by RTNL.
90 * Must be called with vid in range from 1 to 4094 inclusive.
92 int nbp_vlan_tunnel_info_add(const struct net_bridge_port
*port
, u16 vid
,
95 struct net_bridge_vlan_group
*vg
;
96 struct net_bridge_vlan
*vlan
;
100 vg
= nbp_vlan_group(port
);
101 vlan
= br_vlan_find(vg
, vid
);
105 return __vlan_tunnel_info_add(vg
, vlan
, tun_id
);
108 /* Must be protected by RTNL.
109 * Must be called with vid in range from 1 to 4094 inclusive.
111 int nbp_vlan_tunnel_info_delete(const struct net_bridge_port
*port
, u16 vid
)
113 struct net_bridge_vlan_group
*vg
;
114 struct net_bridge_vlan
*v
;
118 vg
= nbp_vlan_group(port
);
119 v
= br_vlan_find(vg
, vid
);
123 vlan_tunnel_info_del(vg
, v
);
128 static void __vlan_tunnel_info_flush(struct net_bridge_vlan_group
*vg
)
130 struct net_bridge_vlan
*vlan
, *tmp
;
132 list_for_each_entry_safe(vlan
, tmp
, &vg
->vlan_list
, vlist
)
133 vlan_tunnel_info_del(vg
, vlan
);
136 void nbp_vlan_tunnel_info_flush(struct net_bridge_port
*port
)
138 struct net_bridge_vlan_group
*vg
;
142 vg
= nbp_vlan_group(port
);
143 __vlan_tunnel_info_flush(vg
);
146 int vlan_tunnel_init(struct net_bridge_vlan_group
*vg
)
148 return rhashtable_init(&vg
->tunnel_hash
, &br_vlan_tunnel_rht_params
);
151 void vlan_tunnel_deinit(struct net_bridge_vlan_group
*vg
)
153 rhashtable_destroy(&vg
->tunnel_hash
);
156 int br_handle_ingress_vlan_tunnel(struct sk_buff
*skb
,
157 struct net_bridge_port
*p
,
158 struct net_bridge_vlan_group
*vg
)
160 struct ip_tunnel_info
*tinfo
= skb_tunnel_info(skb
);
161 struct net_bridge_vlan
*vlan
;
166 /* if already tagged, ignore */
167 if (skb_vlan_tagged(skb
))
170 /* lookup vid, given tunnel id */
171 vlan
= br_vlan_tunnel_lookup(&vg
->tunnel_hash
, tinfo
->key
.tun_id
);
177 __vlan_hwaccel_put_tag(skb
, p
->br
->vlan_proto
, vlan
->vid
);
182 int br_handle_egress_vlan_tunnel(struct sk_buff
*skb
,
183 struct net_bridge_vlan
*vlan
)
187 if (!vlan
|| !vlan
->tinfo
.tunnel_id
)
190 if (unlikely(!skb_vlan_tag_present(skb
)))
194 err
= skb_vlan_pop(skb
);
198 skb_dst_set(skb
, dst_clone(&vlan
->tinfo
.tunnel_dst
->dst
));