openvswitch: Remove Linux bridge compatibility.
[sliver-openvswitch.git] / datapath / vport-patch.c
1 /*
2  * Copyright (c) 2007-2012 Nicira, Inc.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of version 2 of the GNU General Public
6  * License as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16  * 02110-1301, USA
17  */
18
19 #include <linux/kernel.h>
20 #include <linux/jhash.h>
21 #include <linux/list.h>
22 #include <linux/rtnetlink.h>
23 #include <net/net_namespace.h>
24
25 #include "compat.h"
26 #include "datapath.h"
27 #include "vport.h"
28
29 struct patch_config {
30         struct rcu_head rcu;
31
32         char peer_name[IFNAMSIZ];
33         unsigned char eth_addr[ETH_ALEN];
34 };
35
36 struct patch_vport {
37         struct rcu_head rcu;
38
39         char name[IFNAMSIZ];
40
41         /* Protected by RTNL lock. */
42         struct hlist_node hash_node;
43
44         struct vport __rcu *peer;
45         struct patch_config __rcu *patchconf;
46 };
47
48 /* Protected by RTNL lock. */
49 static struct hlist_head *peer_table;
50 #define PEER_HASH_BUCKETS 256
51
52 static void update_peers(struct net *, const char *name, struct vport *);
53
54 static struct patch_vport *patch_vport_priv(const struct vport *vport)
55 {
56         return vport_priv(vport);
57 }
58
59 /* RCU callback. */
60 static void free_config(struct rcu_head *rcu)
61 {
62         struct patch_config *c = container_of(rcu, struct patch_config, rcu);
63         kfree(c);
64 }
65
66 static void assign_config_rcu(struct vport *vport,
67                               struct patch_config *new_config)
68 {
69         struct patch_vport *patch_vport = patch_vport_priv(vport);
70         struct patch_config *old_config;
71
72         old_config = rtnl_dereference(patch_vport->patchconf);
73         rcu_assign_pointer(patch_vport->patchconf, new_config);
74         call_rcu(&old_config->rcu, free_config);
75 }
76
77 static struct hlist_head *hash_bucket(struct net *net, const char *name)
78 {
79         unsigned int hash = jhash(name, strlen(name), (unsigned long) net);
80         return &peer_table[hash & (PEER_HASH_BUCKETS - 1)];
81 }
82
83 static int patch_init(void)
84 {
85         peer_table = kzalloc(PEER_HASH_BUCKETS * sizeof(struct hlist_head),
86                             GFP_KERNEL);
87         if (!peer_table)
88                 return -ENOMEM;
89
90         return 0;
91 }
92
93 static void patch_exit(void)
94 {
95         kfree(peer_table);
96 }
97
98 static const struct nla_policy patch_policy[OVS_PATCH_ATTR_MAX + 1] = {
99 #ifdef HAVE_NLA_NUL_STRING
100         [OVS_PATCH_ATTR_PEER] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
101 #endif
102 };
103
104 static int patch_set_config(struct vport *vport, const struct nlattr *options,
105                             struct patch_config *patchconf)
106 {
107         struct patch_vport *patch_vport = patch_vport_priv(vport);
108         struct nlattr *a[OVS_PATCH_ATTR_MAX + 1];
109         const char *peer_name;
110         int err;
111
112         if (!options)
113                 return -EINVAL;
114
115         err = nla_parse_nested(a, OVS_PATCH_ATTR_MAX, options, patch_policy);
116         if (err)
117                 return err;
118
119         if (!a[OVS_PATCH_ATTR_PEER] ||
120             CHECK_NUL_STRING(a[OVS_PATCH_ATTR_PEER], IFNAMSIZ - 1))
121                 return -EINVAL;
122
123         peer_name = nla_data(a[OVS_PATCH_ATTR_PEER]);
124         if (!strcmp(patch_vport->name, peer_name))
125                 return -EINVAL;
126
127         strcpy(patchconf->peer_name, peer_name);
128
129         return 0;
130 }
131
132 static struct vport *patch_create(const struct vport_parms *parms)
133 {
134         struct vport *vport;
135         struct patch_vport *patch_vport;
136         const char *peer_name;
137         struct patch_config *patchconf;
138         struct net *net = ovs_dp_get_net(parms->dp);
139         int err;
140
141         vport = ovs_vport_alloc(sizeof(struct patch_vport),
142                                 &ovs_patch_vport_ops, parms);
143         if (IS_ERR(vport)) {
144                 err = PTR_ERR(vport);
145                 goto error;
146         }
147
148         patch_vport = patch_vport_priv(vport);
149
150         strcpy(patch_vport->name, parms->name);
151
152         patchconf = kmalloc(sizeof(struct patch_config), GFP_KERNEL);
153         if (!patchconf) {
154                 err = -ENOMEM;
155                 goto error_free_vport;
156         }
157
158         err = patch_set_config(vport, parms->options, patchconf);
159         if (err)
160                 goto error_free_patchconf;
161
162         random_ether_addr(patchconf->eth_addr);
163
164         rcu_assign_pointer(patch_vport->patchconf, patchconf);
165
166         peer_name = patchconf->peer_name;
167         hlist_add_head(&patch_vport->hash_node, hash_bucket(net, peer_name));
168         rcu_assign_pointer(patch_vport->peer, ovs_vport_locate(net, peer_name));
169         update_peers(net, patch_vport->name, vport);
170
171         return vport;
172
173 error_free_patchconf:
174         kfree(patchconf);
175 error_free_vport:
176         ovs_vport_free(vport);
177 error:
178         return ERR_PTR(err);
179 }
180
181 static void free_port_rcu(struct rcu_head *rcu)
182 {
183         struct patch_vport *patch_vport = container_of(rcu,
184                                           struct patch_vport, rcu);
185
186         kfree((struct patch_config __force *)patch_vport->patchconf);
187         ovs_vport_free(vport_from_priv(patch_vport));
188 }
189
190 static void patch_destroy(struct vport *vport)
191 {
192         struct patch_vport *patch_vport = patch_vport_priv(vport);
193
194         update_peers(ovs_dp_get_net(vport->dp), patch_vport->name, NULL);
195         hlist_del(&patch_vport->hash_node);
196         call_rcu(&patch_vport->rcu, free_port_rcu);
197 }
198
199 static int patch_set_options(struct vport *vport, struct nlattr *options)
200 {
201         struct patch_vport *patch_vport = patch_vport_priv(vport);
202         struct patch_config *patchconf;
203         int err;
204
205         patchconf = kmemdup(rtnl_dereference(patch_vport->patchconf),
206                           sizeof(struct patch_config), GFP_KERNEL);
207         if (!patchconf) {
208                 err = -ENOMEM;
209                 goto error;
210         }
211
212         err = patch_set_config(vport, options, patchconf);
213         if (err)
214                 goto error_free;
215
216         assign_config_rcu(vport, patchconf);
217
218         hlist_del(&patch_vport->hash_node);
219
220         rcu_assign_pointer(patch_vport->peer,
221                 ovs_vport_locate(ovs_dp_get_net(vport->dp), patchconf->peer_name));
222
223         hlist_add_head(&patch_vport->hash_node,
224                        hash_bucket(ovs_dp_get_net(vport->dp), patchconf->peer_name));
225
226         return 0;
227 error_free:
228         kfree(patchconf);
229 error:
230         return err;
231 }
232
233 static void update_peers(struct net *net, const char *name, struct vport *vport)
234 {
235         struct hlist_head *bucket = hash_bucket(net, name);
236         struct patch_vport *peer_vport;
237         struct hlist_node *node;
238
239         hlist_for_each_entry(peer_vport, node, bucket, hash_node) {
240                 struct vport *curr_vport = vport_from_priv(peer_vport);
241                 const char *peer_name;
242
243                 peer_name = rtnl_dereference(peer_vport->patchconf)->peer_name;
244                 if (!strcmp(peer_name, name) && net_eq(ovs_dp_get_net(curr_vport->dp), net))
245                         rcu_assign_pointer(peer_vport->peer, vport);
246         }
247 }
248
249 static int patch_set_addr(struct vport *vport, const unsigned char *addr)
250 {
251         struct patch_vport *patch_vport = patch_vport_priv(vport);
252         struct patch_config *patchconf;
253
254         patchconf = kmemdup(rtnl_dereference(patch_vport->patchconf),
255                           sizeof(struct patch_config), GFP_KERNEL);
256         if (!patchconf)
257                 return -ENOMEM;
258
259         memcpy(patchconf->eth_addr, addr, ETH_ALEN);
260         assign_config_rcu(vport, patchconf);
261
262         return 0;
263 }
264
265
266 static const char *patch_get_name(const struct vport *vport)
267 {
268         const struct patch_vport *patch_vport = patch_vport_priv(vport);
269         return patch_vport->name;
270 }
271
272 static const unsigned char *patch_get_addr(const struct vport *vport)
273 {
274         const struct patch_vport *patch_vport = patch_vport_priv(vport);
275         return rcu_dereference_rtnl(patch_vport->patchconf)->eth_addr;
276 }
277
278 static int patch_get_options(const struct vport *vport, struct sk_buff *skb)
279 {
280         struct patch_vport *patch_vport = patch_vport_priv(vport);
281         struct patch_config *patchconf = rcu_dereference_rtnl(patch_vport->patchconf);
282
283         return nla_put_string(skb, OVS_PATCH_ATTR_PEER, patchconf->peer_name);
284 }
285
286 static int patch_send(struct vport *vport, struct sk_buff *skb)
287 {
288         struct patch_vport *patch_vport = patch_vport_priv(vport);
289         struct vport *peer = rcu_dereference(patch_vport->peer);
290         int skb_len = skb->len;
291
292         if (!peer) {
293                 kfree_skb(skb);
294                 ovs_vport_record_error(vport, VPORT_E_TX_DROPPED);
295
296                 return 0;
297         }
298
299         ovs_vport_receive(peer, skb);
300         return skb_len;
301 }
302
303 const struct vport_ops ovs_patch_vport_ops = {
304         .type           = OVS_VPORT_TYPE_PATCH,
305         .init           = patch_init,
306         .exit           = patch_exit,
307         .create         = patch_create,
308         .destroy        = patch_destroy,
309         .set_addr       = patch_set_addr,
310         .get_name       = patch_get_name,
311         .get_addr       = patch_get_addr,
312         .get_options    = patch_get_options,
313         .set_options    = patch_set_options,
314         .send           = patch_send,
315 };