2 * Bridge per vlan tunnel port dst_metadata handling code
5 * Roopa Prabhu <roopa@cumulusnetworks.com>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
13 #include <linux/kernel.h>
14 #include <linux/netdevice.h>
15 #include <linux/rtnetlink.h>
16 #include <linux/slab.h>
17 #include <net/switchdev.h>
18 #include <net/dst_metadata.h>
20 #include "br_private.h"
21 #include "br_private_tunnel.h"
23 static inline int br_vlan_tunid_cmp(struct rhashtable_compare_arg
*arg
,
26 const struct net_bridge_vlan
*vle
= ptr
;
27 __be64 tunid
= *(__be64
*)arg
->key
;
29 return vle
->tinfo
.tunnel_id
!= tunid
;
32 static const struct rhashtable_params br_vlan_tunnel_rht_params
= {
33 .head_offset
= offsetof(struct net_bridge_vlan
, tnode
),
34 .key_offset
= offsetof(struct net_bridge_vlan
, tinfo
.tunnel_id
),
35 .key_len
= sizeof(__be64
),
38 .obj_cmpfn
= br_vlan_tunid_cmp
,
39 .automatic_shrinking
= true,
42 static struct net_bridge_vlan
*br_vlan_tunnel_lookup(struct rhashtable
*tbl
,
45 return rhashtable_lookup_fast(tbl
, &tunnel_id
,
46 br_vlan_tunnel_rht_params
);
49 void vlan_tunnel_info_del(struct net_bridge_vlan_group
*vg
,
50 struct net_bridge_vlan
*vlan
)
52 if (!vlan
->tinfo
.tunnel_dst
)
54 rhashtable_remove_fast(&vg
->tunnel_hash
, &vlan
->tnode
,
55 br_vlan_tunnel_rht_params
);
56 vlan
->tinfo
.tunnel_id
= 0;
57 dst_release(&vlan
->tinfo
.tunnel_dst
->dst
);
58 vlan
->tinfo
.tunnel_dst
= NULL
;
61 static int __vlan_tunnel_info_add(struct net_bridge_vlan_group
*vg
,
62 struct net_bridge_vlan
*vlan
, u32 tun_id
)
64 struct metadata_dst
*metadata
= NULL
;
65 __be64 key
= key32_to_tunnel_id(cpu_to_be32(tun_id
));
68 if (vlan
->tinfo
.tunnel_dst
)
71 metadata
= __ip_tun_set_dst(0, 0, 0, 0, 0, TUNNEL_KEY
,
76 metadata
->u
.tun_info
.mode
|= IP_TUNNEL_INFO_TX
| IP_TUNNEL_INFO_BRIDGE
;
77 vlan
->tinfo
.tunnel_dst
= metadata
;
78 vlan
->tinfo
.tunnel_id
= key
;
80 err
= rhashtable_lookup_insert_fast(&vg
->tunnel_hash
, &vlan
->tnode
,
81 br_vlan_tunnel_rht_params
);
87 dst_release(&vlan
->tinfo
.tunnel_dst
->dst
);
88 vlan
->tinfo
.tunnel_dst
= NULL
;
89 vlan
->tinfo
.tunnel_id
= 0;
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(struct net_bridge_port
*port
, u16 vid
, u32 tun_id
)
99 struct net_bridge_vlan_group
*vg
;
100 struct net_bridge_vlan
*vlan
;
104 vg
= nbp_vlan_group(port
);
105 vlan
= br_vlan_find(vg
, vid
);
109 return __vlan_tunnel_info_add(vg
, vlan
, tun_id
);
112 /* Must be protected by RTNL.
113 * Must be called with vid in range from 1 to 4094 inclusive.
115 int nbp_vlan_tunnel_info_delete(struct net_bridge_port
*port
, u16 vid
)
117 struct net_bridge_vlan_group
*vg
;
118 struct net_bridge_vlan
*v
;
122 vg
= nbp_vlan_group(port
);
123 v
= br_vlan_find(vg
, vid
);
127 vlan_tunnel_info_del(vg
, v
);
132 static void __vlan_tunnel_info_flush(struct net_bridge_vlan_group
*vg
)
134 struct net_bridge_vlan
*vlan
, *tmp
;
136 list_for_each_entry_safe(vlan
, tmp
, &vg
->vlan_list
, vlist
)
137 vlan_tunnel_info_del(vg
, vlan
);
140 void nbp_vlan_tunnel_info_flush(struct net_bridge_port
*port
)
142 struct net_bridge_vlan_group
*vg
;
146 vg
= nbp_vlan_group(port
);
147 __vlan_tunnel_info_flush(vg
);
150 int vlan_tunnel_init(struct net_bridge_vlan_group
*vg
)
152 return rhashtable_init(&vg
->tunnel_hash
, &br_vlan_tunnel_rht_params
);
155 void vlan_tunnel_deinit(struct net_bridge_vlan_group
*vg
)
157 rhashtable_destroy(&vg
->tunnel_hash
);
160 int br_handle_ingress_vlan_tunnel(struct sk_buff
*skb
,
161 struct net_bridge_port
*p
,
162 struct net_bridge_vlan_group
*vg
)
164 struct ip_tunnel_info
*tinfo
= skb_tunnel_info(skb
);
165 struct net_bridge_vlan
*vlan
;
170 /* if already tagged, ignore */
171 if (skb_vlan_tagged(skb
))
174 /* lookup vid, given tunnel id */
175 vlan
= br_vlan_tunnel_lookup(&vg
->tunnel_hash
, tinfo
->key
.tun_id
);
181 __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
)
191 if (!vlan
|| !vlan
->tinfo
.tunnel_id
)
194 if (unlikely(!skb_vlan_tag_present(skb
)))
198 err
= skb_vlan_pop(skb
);
202 skb_dst_set(skb
, dst_clone(&vlan
->tinfo
.tunnel_dst
->dst
));