Merge branches 'pm-cpufreq-fixes' and 'pm-cpuidle-fixes'
[linux/fpc-iii.git] / net / bridge / br_vlan_tunnel.c
blob6d2c4eed2dc892b15a99f4741dc8639b25a63e32
1 /*
2 * Bridge per vlan tunnel port dst_metadata handling code
4 * Authors:
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,
24 const void *ptr)
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),
36 .nelem_hint = 3,
37 .locks_mul = 1,
38 .obj_cmpfn = br_vlan_tunid_cmp,
39 .automatic_shrinking = true,
42 static struct net_bridge_vlan *br_vlan_tunnel_lookup(struct rhashtable *tbl,
43 u64 tunnel_id)
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)
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;
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));
66 int err;
68 if (vlan->tinfo.tunnel_dst)
69 return -EEXIST;
71 metadata = __ip_tun_set_dst(0, 0, 0, 0, 0, TUNNEL_KEY,
72 key, 0);
73 if (!metadata)
74 return -EINVAL;
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);
82 if (err)
83 goto out;
85 return 0;
86 out:
87 dst_release(&vlan->tinfo.tunnel_dst->dst);
88 vlan->tinfo.tunnel_dst = NULL;
89 vlan->tinfo.tunnel_id = 0;
91 return err;
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;
102 ASSERT_RTNL();
104 vg = nbp_vlan_group(port);
105 vlan = br_vlan_find(vg, vid);
106 if (!vlan)
107 return -EINVAL;
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;
120 ASSERT_RTNL();
122 vg = nbp_vlan_group(port);
123 v = br_vlan_find(vg, vid);
124 if (!v)
125 return -ENOENT;
127 vlan_tunnel_info_del(vg, v);
129 return 0;
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;
144 ASSERT_RTNL();
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;
167 if (!vg || !tinfo)
168 return 0;
170 /* if already tagged, ignore */
171 if (skb_vlan_tagged(skb))
172 return 0;
174 /* lookup vid, given tunnel id */
175 vlan = br_vlan_tunnel_lookup(&vg->tunnel_hash, tinfo->key.tun_id);
176 if (!vlan)
177 return 0;
179 skb_dst_drop(skb);
181 __vlan_hwaccel_put_tag(skb, p->br->vlan_proto, vlan->vid);
183 return 0;
186 int br_handle_egress_vlan_tunnel(struct sk_buff *skb,
187 struct net_bridge_vlan *vlan)
189 int err;
191 if (!vlan || !vlan->tinfo.tunnel_id)
192 return 0;
194 if (unlikely(!skb_vlan_tag_present(skb)))
195 return 0;
197 skb_dst_drop(skb);
198 err = skb_vlan_pop(skb);
199 if (err)
200 return err;
202 skb_dst_set(skb, dst_clone(&vlan->tinfo.tunnel_dst->dst));
204 return 0;