blob: b2b79a0701626a4fd8dc7a2e91d52cbe54b04444 [file] [log] [blame]
Roopa Prabhuefa53562017-01-31 22:59:54 -08001/*
2 * Bridge per vlan tunnel port dst_metadata handling code
3 *
4 * Authors:
5 * Roopa Prabhu <roopa@cumulusnetworks.com>
6 *
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.
11 */
12
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>
19
20#include "br_private.h"
21#include "br_private_tunnel.h"
22
23static inline int br_vlan_tunid_cmp(struct rhashtable_compare_arg *arg,
24 const void *ptr)
25{
26 const struct net_bridge_vlan *vle = ptr;
27 __be64 tunid = *(__be64 *)arg->key;
28
29 return vle->tinfo.tunnel_id != tunid;
30}
31
32static 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),
36 .nelem_hint = 3,
37 .locks_mul = 1,
38 .obj_cmpfn = br_vlan_tunid_cmp,
39 .automatic_shrinking = true,
40};
41
Roopa Prabhu11538d02017-01-31 22:59:55 -080042static struct net_bridge_vlan *br_vlan_tunnel_lookup(struct rhashtable *tbl,
43 u64 tunnel_id)
44{
45 return rhashtable_lookup_fast(tbl, &tunnel_id,
46 br_vlan_tunnel_rht_params);
47}
48
Roopa Prabhuefa53562017-01-31 22:59:54 -080049void vlan_tunnel_info_del(struct net_bridge_vlan_group *vg,
50 struct net_bridge_vlan *vlan)
51{
52 if (!vlan->tinfo.tunnel_dst)
53 return;
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;
59}
60
61static int __vlan_tunnel_info_add(struct net_bridge_vlan_group *vg,
62 struct net_bridge_vlan *vlan, u32 tun_id)
63{
64 struct metadata_dst *metadata = NULL;
65 __be64 key = key32_to_tunnel_id(cpu_to_be32(tun_id));
66 int err;
67
68 if (vlan->tinfo.tunnel_dst)
69 return -EEXIST;
70
71 metadata = __ip_tun_set_dst(0, 0, 0, 0, 0, TUNNEL_KEY,
72 key, 0);
73 if (!metadata)
74 return -EINVAL;
75
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;
79
80 err = rhashtable_lookup_insert_fast(&vg->tunnel_hash, &vlan->tnode,
81 br_vlan_tunnel_rht_params);
82 if (err)
83 goto out;
84
85 return 0;
86out:
87 dst_release(&vlan->tinfo.tunnel_dst->dst);
88
89 return err;
90}
91
92/* Must be protected by RTNL.
93 * Must be called with vid in range from 1 to 4094 inclusive.
94 */
95int nbp_vlan_tunnel_info_add(struct net_bridge_port *port, u16 vid, u32 tun_id)
96{
97 struct net_bridge_vlan_group *vg;
98 struct net_bridge_vlan *vlan;
99
100 ASSERT_RTNL();
101
102 vg = nbp_vlan_group(port);
103 vlan = br_vlan_find(vg, vid);
104 if (!vlan)
105 return -EINVAL;
106
107 return __vlan_tunnel_info_add(vg, vlan, tun_id);
108}
109
110/* Must be protected by RTNL.
111 * Must be called with vid in range from 1 to 4094 inclusive.
112 */
113int nbp_vlan_tunnel_info_delete(struct net_bridge_port *port, u16 vid)
114{
115 struct net_bridge_vlan_group *vg;
116 struct net_bridge_vlan *v;
117
118 ASSERT_RTNL();
119
120 vg = nbp_vlan_group(port);
121 v = br_vlan_find(vg, vid);
122 if (!v)
123 return -ENOENT;
124
125 vlan_tunnel_info_del(vg, v);
126
127 return 0;
128}
129
130static void __vlan_tunnel_info_flush(struct net_bridge_vlan_group *vg)
131{
132 struct net_bridge_vlan *vlan, *tmp;
133
134 list_for_each_entry_safe(vlan, tmp, &vg->vlan_list, vlist)
135 vlan_tunnel_info_del(vg, vlan);
136}
137
138void nbp_vlan_tunnel_info_flush(struct net_bridge_port *port)
139{
140 struct net_bridge_vlan_group *vg;
141
142 ASSERT_RTNL();
143
144 vg = nbp_vlan_group(port);
145 __vlan_tunnel_info_flush(vg);
146}
147
148int vlan_tunnel_init(struct net_bridge_vlan_group *vg)
149{
150 return rhashtable_init(&vg->tunnel_hash, &br_vlan_tunnel_rht_params);
151}
152
153void vlan_tunnel_deinit(struct net_bridge_vlan_group *vg)
154{
155 rhashtable_destroy(&vg->tunnel_hash);
156}
Roopa Prabhu11538d02017-01-31 22:59:55 -0800157
158int br_handle_ingress_vlan_tunnel(struct sk_buff *skb,
159 struct net_bridge_port *p,
160 struct net_bridge_vlan_group *vg)
161{
162 struct ip_tunnel_info *tinfo = skb_tunnel_info(skb);
163 struct net_bridge_vlan *vlan;
164
165 if (!vg || !tinfo)
166 return 0;
167
168 /* if already tagged, ignore */
169 if (skb_vlan_tagged(skb))
170 return 0;
171
172 /* lookup vid, given tunnel id */
173 vlan = br_vlan_tunnel_lookup(&vg->tunnel_hash, tinfo->key.tun_id);
174 if (!vlan)
175 return 0;
176
177 skb_dst_drop(skb);
178
179 __vlan_hwaccel_put_tag(skb, p->br->vlan_proto, vlan->vid);
180
181 return 0;
182}
183
184int br_handle_egress_vlan_tunnel(struct sk_buff *skb,
185 struct net_bridge_vlan *vlan)
186{
187 int err;
188
189 if (!vlan || !vlan->tinfo.tunnel_id)
190 return 0;
191
192 if (unlikely(!skb_vlan_tag_present(skb)))
193 return 0;
194
195 skb_dst_drop(skb);
196 err = skb_vlan_pop(skb);
197 if (err)
198 return err;
199
200 skb_dst_set(skb, dst_clone(&vlan->tinfo.tunnel_dst->dst));
201
202 return 0;
203}