nicira-ext: Support matching IPv6 traffic.
[sliver-openvswitch.git] / datapath / vport-patch.c
1 /*
2  * Copyright (c) 2010, 2011 Nicira Networks.
3  * Distributed under the terms of the GNU GPL version 2.
4  *
5  * Significant portions of this file may be copied from parts of the Linux
6  * kernel, by Linus Torvalds and others.
7  */
8
9 #include <linux/dcache.h>
10 #include <linux/kernel.h>
11 #include <linux/list.h>
12 #include <linux/rtnetlink.h>
13
14 #include "datapath.h"
15 #include "vport.h"
16 #include "vport-generic.h"
17
18 struct patch_config {
19         struct rcu_head rcu;
20
21         char peer_name[IFNAMSIZ];
22         unsigned char eth_addr[ETH_ALEN];
23         unsigned int mtu;
24 };
25
26 struct patch_vport {
27         struct rcu_head rcu;
28
29         char name[IFNAMSIZ];
30
31         /* Protected by RTNL lock. */
32         struct hlist_node hash_node;
33
34         struct vport __rcu *peer;
35         struct patch_config __rcu *patchconf;
36 };
37
38 /* Protected by RTNL lock. */
39 static struct hlist_head *peer_table;
40 #define PEER_HASH_BUCKETS 256
41
42 static void update_peers(const char *name, struct vport *);
43
44 static inline struct patch_vport *patch_vport_priv(const struct vport *vport)
45 {
46         return vport_priv(vport);
47 }
48
49 /* RCU callback. */
50 static void free_config(struct rcu_head *rcu)
51 {
52         struct patch_config *c = container_of(rcu, struct patch_config, rcu);
53         kfree(c);
54 }
55
56 static void assign_config_rcu(struct vport *vport,
57                               struct patch_config *new_config)
58 {
59         struct patch_vport *patch_vport = patch_vport_priv(vport);
60         struct patch_config *old_config;
61
62         old_config = rtnl_dereference(patch_vport->patchconf);
63         rcu_assign_pointer(patch_vport->patchconf, new_config);
64         call_rcu(&old_config->rcu, free_config);
65 }
66
67 static struct hlist_head *hash_bucket(const char *name)
68 {
69         unsigned int hash = full_name_hash(name, strlen(name));
70         return &peer_table[hash & (PEER_HASH_BUCKETS - 1)];
71 }
72
73 static int patch_init(void)
74 {
75         peer_table = kzalloc(PEER_HASH_BUCKETS * sizeof(struct hlist_head),
76                             GFP_KERNEL);
77         if (!peer_table)
78                 return -ENOMEM;
79
80         return 0;
81 }
82
83 static void patch_exit(void)
84 {
85         kfree(peer_table);
86 }
87
88 static const struct nla_policy patch_policy[ODP_PATCH_ATTR_MAX + 1] = {
89 #ifdef HAVE_NLA_NUL_STRING
90         [ODP_PATCH_ATTR_PEER] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
91 #endif
92 };
93
94 static int patch_set_config(struct vport *vport, const struct nlattr *options,
95                             struct patch_config *patchconf)
96 {
97         struct patch_vport *patch_vport = patch_vport_priv(vport);
98         struct nlattr *a[ODP_PATCH_ATTR_MAX + 1];
99         const char *peer_name;
100         int err;
101
102         if (!options)
103                 return -EINVAL;
104
105         err = nla_parse_nested(a, ODP_PATCH_ATTR_MAX, options, patch_policy);
106         if (err)
107                 return err;
108
109         if (!a[ODP_PATCH_ATTR_PEER] || VERIFY_NUL_STRING(a[ODP_PATCH_ATTR_PEER], IFNAMSIZ - 1))
110                 return -EINVAL;
111
112         peer_name = nla_data(a[ODP_PATCH_ATTR_PEER]);
113         if (!strcmp(patch_vport->name, peer_name))
114                 return -EINVAL;
115
116         strcpy(patchconf->peer_name, peer_name);
117
118         return 0;
119 }
120
121 static struct vport *patch_create(const struct vport_parms *parms)
122 {
123         struct vport *vport;
124         struct patch_vport *patch_vport;
125         const char *peer_name;
126         struct patch_config *patchconf;
127         int err;
128
129         vport = vport_alloc(sizeof(struct patch_vport), &patch_vport_ops, parms);
130         if (IS_ERR(vport)) {
131                 err = PTR_ERR(vport);
132                 goto error;
133         }
134
135         patch_vport = patch_vport_priv(vport);
136
137         strcpy(patch_vport->name, parms->name);
138
139         patchconf = kmalloc(sizeof(struct patch_config), GFP_KERNEL);
140         if (!patchconf) {
141                 err = -ENOMEM;
142                 goto error_free_vport;
143         }
144
145         err = patch_set_config(vport, parms->options, patchconf);
146         if (err)
147                 goto error_free_patchconf;
148
149         vport_gen_rand_ether_addr(patchconf->eth_addr);
150
151         /* Make the default MTU fairly large so that it doesn't become the
152          * bottleneck on systems using jumbo frames. */
153         patchconf->mtu = 65535;
154
155         rcu_assign_pointer(patch_vport->patchconf, patchconf);
156
157         peer_name = patchconf->peer_name;
158         hlist_add_head(&patch_vport->hash_node, hash_bucket(peer_name));
159         rcu_assign_pointer(patch_vport->peer, vport_locate(peer_name));
160         update_peers(patch_vport->name, vport);
161
162         return vport;
163
164 error_free_patchconf:
165         kfree(patchconf);
166 error_free_vport:
167         vport_free(vport);
168 error:
169         return ERR_PTR(err);
170 }
171
172 static void free_port_rcu(struct rcu_head *rcu)
173 {
174         struct patch_vport *patch_vport = container_of(rcu,
175                                           struct patch_vport, rcu);
176
177         kfree((struct patch_config __force *)patch_vport->patchconf);
178         vport_free(vport_from_priv(patch_vport));
179 }
180
181 static int patch_destroy(struct vport *vport)
182 {
183         struct patch_vport *patch_vport = patch_vport_priv(vport);
184
185         update_peers(patch_vport->name, NULL);
186         hlist_del(&patch_vport->hash_node);
187         call_rcu(&patch_vport->rcu, free_port_rcu);
188
189         return 0;
190 }
191
192 static int patch_set_options(struct vport *vport, struct nlattr *options)
193 {
194         struct patch_vport *patch_vport = patch_vport_priv(vport);
195         struct patch_config *patchconf;
196         int err;
197
198         patchconf = kmemdup(rtnl_dereference(patch_vport->patchconf),
199                           sizeof(struct patch_config), GFP_KERNEL);
200         if (!patchconf) {
201                 err = -ENOMEM;
202                 goto error;
203         }
204
205         err = patch_set_config(vport, options, patchconf);
206         if (err)
207                 goto error_free;
208
209         assign_config_rcu(vport, patchconf);
210
211         hlist_del(&patch_vport->hash_node);
212
213         rcu_assign_pointer(patch_vport->peer, vport_locate(patchconf->peer_name));
214         hlist_add_head(&patch_vport->hash_node, hash_bucket(patchconf->peer_name));
215
216         return 0;
217
218 error_free:
219         kfree(patchconf);
220 error:
221         return err;
222 }
223
224 static void update_peers(const char *name, struct vport *vport)
225 {
226         struct hlist_head *bucket = hash_bucket(name);
227         struct patch_vport *peer_vport;
228         struct hlist_node *node;
229
230         hlist_for_each_entry(peer_vport, node, bucket, hash_node) {
231                 const char *peer_name;
232
233                 peer_name = rtnl_dereference(peer_vport->patchconf)->peer_name;
234                 if (!strcmp(peer_name, name))
235                         rcu_assign_pointer(peer_vport->peer, vport);
236         }
237 }
238
239 static int patch_set_mtu(struct vport *vport, int mtu)
240 {
241         struct patch_vport *patch_vport = patch_vport_priv(vport);
242         struct patch_config *patchconf;
243
244         patchconf = kmemdup(rtnl_dereference(patch_vport->patchconf),
245                           sizeof(struct patch_config), GFP_KERNEL);
246         if (!patchconf)
247                 return -ENOMEM;
248
249         patchconf->mtu = mtu;
250         assign_config_rcu(vport, patchconf);
251
252         return 0;
253 }
254
255 static int patch_set_addr(struct vport *vport, const unsigned char *addr)
256 {
257         struct patch_vport *patch_vport = patch_vport_priv(vport);
258         struct patch_config *patchconf;
259
260         patchconf = kmemdup(rtnl_dereference(patch_vport->patchconf),
261                           sizeof(struct patch_config), GFP_KERNEL);
262         if (!patchconf)
263                 return -ENOMEM;
264
265         memcpy(patchconf->eth_addr, addr, ETH_ALEN);
266         assign_config_rcu(vport, patchconf);
267
268         return 0;
269 }
270
271
272 static const char *patch_get_name(const struct vport *vport)
273 {
274         const struct patch_vport *patch_vport = patch_vport_priv(vport);
275         return patch_vport->name;
276 }
277
278 static const unsigned char *patch_get_addr(const struct vport *vport)
279 {
280         const struct patch_vport *patch_vport = patch_vport_priv(vport);
281         return rcu_dereference_rtnl(patch_vport->patchconf)->eth_addr;
282 }
283
284 static int patch_get_options(const struct vport *vport, struct sk_buff *skb)
285 {
286         struct patch_vport *patch_vport = patch_vport_priv(vport);
287         struct patch_config *patchconf = rcu_dereference_rtnl(patch_vport->patchconf);
288
289         return nla_put_string(skb, ODP_PATCH_ATTR_PEER, patchconf->peer_name);
290 }
291
292 static int patch_get_mtu(const struct vport *vport)
293 {
294         const struct patch_vport *patch_vport = patch_vport_priv(vport);
295         return rcu_dereference_rtnl(patch_vport->patchconf)->mtu;
296 }
297
298 static int patch_send(struct vport *vport, struct sk_buff *skb)
299 {
300         struct patch_vport *patch_vport = patch_vport_priv(vport);
301         struct vport *peer = rcu_dereference(patch_vport->peer);
302         int skb_len = skb->len;
303
304         if (!peer) {
305                 kfree_skb(skb);
306                 vport_record_error(vport, VPORT_E_TX_DROPPED);
307
308                 return 0;
309         }
310
311         vport_receive(peer, skb);
312         return skb_len;
313 }
314
315 const struct vport_ops patch_vport_ops = {
316         .type           = ODP_VPORT_TYPE_PATCH,
317         .flags          = VPORT_F_GEN_STATS,
318         .init           = patch_init,
319         .exit           = patch_exit,
320         .create         = patch_create,
321         .destroy        = patch_destroy,
322         .set_mtu        = patch_set_mtu,
323         .set_addr       = patch_set_addr,
324         .get_name       = patch_get_name,
325         .get_addr       = patch_get_addr,
326         .get_options    = patch_get_options,
327         .set_options    = patch_set_options,
328         .get_dev_flags  = vport_gen_get_dev_flags,
329         .is_running     = vport_gen_is_running,
330         .get_operstate  = vport_gen_get_operstate,
331         .get_mtu        = patch_get_mtu,
332         .send           = patch_send,
333 };