debian: Require ipsec-tools version 0.8~alpha20101208.
[sliver-openvswitch.git] / datapath / vport-patch.c
1 /*
2  * Copyright (c) 2010 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 device_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 device_config __rcu *devconf;
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 device_config *c = container_of(rcu, struct device_config, rcu);
53         kfree(c);
54 }
55
56 static void assign_config_rcu(struct vport *vport,
57                               struct device_config *new_config)
58 {
59         struct patch_vport *patch_vport = patch_vport_priv(vport);
60         struct device_config *old_config;
61
62         old_config = rtnl_dereference(patch_vport->devconf);
63         rcu_assign_pointer(patch_vport->devconf, 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 int set_config(struct vport *vport, const void *config, 
89                         struct device_config *devconf)
90 {
91         struct patch_vport *patch_vport = patch_vport_priv(vport);
92         char peer_name[IFNAMSIZ];
93
94         strlcpy(peer_name, config, IFNAMSIZ);
95
96         if (!strcmp(patch_vport->name, peer_name))
97                 return -EINVAL;
98
99         strcpy(devconf->peer_name, peer_name);
100
101         return 0;
102 }
103
104 static struct vport *patch_create(const struct vport_parms *parms)
105 {
106         struct vport *vport;
107         struct patch_vport *patch_vport;
108         const char *peer_name;
109         int err;
110
111         vport = vport_alloc(sizeof(struct patch_vport), &patch_vport_ops, parms);
112         if (IS_ERR(vport)) {
113                 err = PTR_ERR(vport);
114                 goto error;
115         }
116
117         patch_vport = patch_vport_priv(vport);
118
119         strcpy(patch_vport->name, parms->name);
120
121         patch_vport->devconf = kmalloc(sizeof(struct device_config), GFP_KERNEL);
122         if (!patch_vport->devconf) {
123                 err = -ENOMEM;
124                 goto error_free_vport;
125         }
126
127         err = set_config(vport, parms->config, patch_vport->devconf);
128         if (err)
129                 goto error_free_devconf;
130
131         vport_gen_rand_ether_addr(patch_vport->devconf->eth_addr);
132
133         /* Make the default MTU fairly large so that it doesn't become the
134          * bottleneck on systems using jumbo frames. */
135         patch_vport->devconf->mtu = 65535;
136
137         peer_name = patch_vport->devconf->peer_name;
138         hlist_add_head(&patch_vport->hash_node, hash_bucket(peer_name));
139
140         rcu_assign_pointer(patch_vport->peer, vport_locate(peer_name));
141         update_peers(patch_vport->name, vport);
142
143         return vport;
144
145 error_free_devconf:
146         kfree(patch_vport->devconf);
147 error_free_vport:
148         vport_free(vport);
149 error:
150         return ERR_PTR(err);
151 }
152
153 static int patch_modify(struct vport *vport, struct odp_port *port)
154 {
155         struct patch_vport *patch_vport = patch_vport_priv(vport);
156         struct device_config *devconf;
157         int err;
158
159         devconf = kmemdup(patch_vport->devconf, sizeof(struct device_config), GFP_KERNEL);
160         if (!devconf) {
161                 err = -ENOMEM;
162                 goto error;
163         }
164
165         err = set_config(vport, port->config, devconf);
166         if (err)
167                 goto error_free;
168
169         assign_config_rcu(vport, devconf);
170         
171         hlist_del(&patch_vport->hash_node);
172
173         rcu_assign_pointer(patch_vport->peer, vport_locate(devconf->peer_name));
174         hlist_add_head(&patch_vport->hash_node, hash_bucket(devconf->peer_name));
175
176         return 0;
177
178 error_free:
179         kfree(devconf);
180 error:
181         return err;
182 }
183
184 static void free_port_rcu(struct rcu_head *rcu)
185 {
186         struct patch_vport *patch_vport = container_of(rcu,
187                                                       struct patch_vport, rcu);
188
189         kfree(patch_vport->devconf);
190         vport_free(vport_from_priv(patch_vport));
191 }
192
193 static int patch_destroy(struct vport *vport)
194 {
195         struct patch_vport *patch_vport = patch_vport_priv(vport);
196
197         update_peers(patch_vport->name, NULL);
198         hlist_del(&patch_vport->hash_node);
199         call_rcu(&patch_vport->rcu, free_port_rcu);
200
201         return 0;
202 }
203
204 static void update_peers(const char *name, struct vport *vport)
205 {
206         struct hlist_head *bucket = hash_bucket(name);
207         struct patch_vport *peer_vport;
208         struct hlist_node *node;
209
210         hlist_for_each_entry(peer_vport, node, bucket, hash_node) {
211                 const char *peer_name;
212
213                 peer_name = rtnl_dereference(peer_vport->devconf)->peer_name;
214                 if (!strcmp(peer_name, name))
215                         rcu_assign_pointer(peer_vport->peer, vport);
216         }
217 }
218
219 static int patch_set_mtu(struct vport *vport, int mtu)
220 {
221         struct patch_vport *patch_vport = patch_vport_priv(vport);
222         struct device_config *devconf;
223
224         devconf = kmemdup(patch_vport->devconf, sizeof(struct device_config), GFP_KERNEL);
225         if (!devconf)
226                 return -ENOMEM;
227
228         devconf->mtu = mtu;
229         assign_config_rcu(vport, devconf);
230
231         return 0;
232 }
233
234 static int patch_set_addr(struct vport *vport, const unsigned char *addr)
235 {
236         struct patch_vport *patch_vport = patch_vport_priv(vport);
237         struct device_config *devconf;
238
239         devconf = kmemdup(patch_vport->devconf, sizeof(struct device_config), GFP_KERNEL);
240         if (!devconf)
241                 return -ENOMEM;
242
243         memcpy(devconf->eth_addr, addr, ETH_ALEN);
244         assign_config_rcu(vport, devconf);
245
246         return 0;
247 }
248
249
250 static const char *patch_get_name(const struct vport *vport)
251 {
252         const struct patch_vport *patch_vport = patch_vport_priv(vport);
253         return patch_vport->name;
254 }
255
256 static const unsigned char *patch_get_addr(const struct vport *vport)
257 {
258         const struct patch_vport *patch_vport = patch_vport_priv(vport);
259         return rcu_dereference_rtnl(patch_vport->devconf)->eth_addr;
260 }
261
262 static void patch_get_config(const struct vport *vport, void *config)
263 {
264         const struct patch_vport *patch_vport = patch_vport_priv(vport);
265         const char *peer_name;
266
267         peer_name = rcu_dereference_rtnl(patch_vport->devconf)->peer_name;
268         strlcpy(config, peer_name, VPORT_CONFIG_SIZE);
269 }
270
271 static int patch_get_mtu(const struct vport *vport)
272 {
273         const struct patch_vport *patch_vport = patch_vport_priv(vport);
274         return rcu_dereference_rtnl(patch_vport->devconf)->mtu;
275 }
276
277 static int patch_send(struct vport *vport, struct sk_buff *skb)
278 {
279         struct patch_vport *patch_vport = patch_vport_priv(vport);
280         struct vport *peer = rcu_dereference(patch_vport->peer);
281         int skb_len = skb->len;
282
283         if (!peer) {
284                 kfree_skb(skb);
285                 vport_record_error(vport, VPORT_E_TX_DROPPED);
286
287                 return 0;
288         }
289
290         vport_receive(peer, skb);
291         return skb_len;
292 }
293
294 const struct vport_ops patch_vport_ops = {
295         .type           = "patch",
296         .flags          = VPORT_F_GEN_STATS,
297         .init           = patch_init,
298         .exit           = patch_exit,
299         .create         = patch_create,
300         .modify         = patch_modify,
301         .destroy        = patch_destroy,
302         .set_mtu        = patch_set_mtu,
303         .set_addr       = patch_set_addr,
304         .get_name       = patch_get_name,
305         .get_addr       = patch_get_addr,
306         .get_config     = patch_get_config,
307         .get_dev_flags  = vport_gen_get_dev_flags,
308         .is_running     = vport_gen_is_running,
309         .get_operstate  = vport_gen_get_operstate,
310         .get_mtu        = patch_get_mtu,
311         .send           = patch_send,
312 };