datapath: Add 'patch' vport.
[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
13 #include "datapath.h"
14 #include "vport.h"
15 #include "vport-generic.h"
16
17 struct device_config {
18         struct rcu_head rcu;
19
20         unsigned char eth_addr[ETH_ALEN];
21         unsigned int mtu;
22 };
23
24 struct patch_vport {
25         char name[IFNAMSIZ];
26
27         /* Protected by RTNL lock. */
28         char peer_name[IFNAMSIZ];
29         struct hlist_node hash_node;
30
31         /* Protected by RCU. */
32         struct vport *peer;
33
34         /* Protected by RCU. */
35         struct device_config *devconf;
36 };
37
38 struct vport_ops patch_vport_ops;
39
40 /* Protected by RTNL lock. */
41 static struct hlist_head *peer_table;
42 #define PEER_HASH_BUCKETS 256
43
44 static inline struct patch_vport *
45 patch_vport_priv(const struct vport *vport)
46 {
47         return vport_priv(vport);
48 }
49
50 /* RCU callback. */
51 static void
52 free_config(struct rcu_head *rcu)
53 {
54         struct device_config *c = container_of(rcu, struct device_config, rcu);
55         kfree(c);
56 }
57
58 static void
59 assign_config_rcu(struct vport *vport, struct device_config *new_config)
60 {
61         struct patch_vport *patch_vport = patch_vport_priv(vport);
62         struct device_config *old_config;
63
64         old_config = rcu_dereference(patch_vport->devconf);
65         rcu_assign_pointer(patch_vport->devconf, new_config);
66         call_rcu(&old_config->rcu, free_config);
67 }
68
69 static struct hlist_head *
70 hash_bucket(const char *name)
71 {
72         unsigned int hash = full_name_hash(name, strlen(name));
73         return &peer_table[hash & (PEER_HASH_BUCKETS - 1)];
74 }
75
76 static int
77 patch_init(void)
78 {
79         peer_table = kzalloc(PEER_HASH_BUCKETS * sizeof(struct hlist_head),
80                             GFP_KERNEL);
81         if (!peer_table)
82                 return -ENOMEM;
83
84         return 0;
85 }
86
87 static void
88 patch_exit(void)
89 {
90         kfree(peer_table);
91 }
92
93 static int
94 set_config(struct vport *vport, const void __user *uconfig)
95 {
96         struct patch_vport *patch_vport = patch_vport_priv(vport);
97         char peer_name[IFNAMSIZ];
98         int retval;
99
100         retval = strncpy_from_user(peer_name, uconfig, IFNAMSIZ);
101         if (retval < 0)
102                 return -EFAULT;
103         else if (retval >= IFNAMSIZ)
104                 return -ENAMETOOLONG;
105
106         if (!strcmp(patch_vport->name, peer_name))
107                 return -EINVAL;
108
109         strcpy(patch_vport->peer_name, peer_name);
110
111         if (vport_get_dp_port(vport)) {
112                 hlist_del(&patch_vport->hash_node);
113                 rcu_assign_pointer(patch_vport->peer, vport_locate(patch_vport->peer_name));
114                 hlist_add_head(&patch_vport->hash_node, hash_bucket(patch_vport->peer_name));
115         }
116
117         return 0;
118 }
119
120 static struct vport *
121 patch_create(const char *name, const void __user *config)
122 {
123         struct vport *vport;
124         struct patch_vport *patch_vport;
125         int err;
126
127         vport = vport_alloc(sizeof(struct patch_vport), &patch_vport_ops);
128         if (IS_ERR(vport)) {
129                 err = PTR_ERR(vport);
130                 goto error;
131         }
132
133         patch_vport = patch_vport_priv(vport);
134
135         strcpy(patch_vport->name, name);
136
137         err = set_config(vport, config);
138         if (err)
139                 goto error_free_vport;
140
141         patch_vport->devconf = kmalloc(sizeof(struct device_config), GFP_KERNEL);
142         if (!patch_vport->devconf) {
143                 err = -ENOMEM;
144                 goto error_free_vport;
145         }
146
147         vport_gen_rand_ether_addr(patch_vport->devconf->eth_addr);
148         patch_vport->devconf->mtu = ETH_DATA_LEN;
149
150         return vport;
151
152 error_free_vport:
153         vport_free(vport);
154 error:
155         return ERR_PTR(err);
156 }
157
158 static int
159 patch_modify(struct vport *vport, const void __user *config)
160 {
161         return set_config(vport, config);
162 }
163
164 static int
165 patch_destroy(struct vport *vport)
166 {
167         struct patch_vport *patch_vport = patch_vport_priv(vport);
168
169         kfree(patch_vport->devconf);
170         vport_free(vport);
171
172         return 0;
173 }
174
175 static void
176 update_peers(const char *name, struct vport *vport)
177 {
178         struct hlist_head *bucket = hash_bucket(name);
179         struct patch_vport *peer_vport;
180         struct hlist_node *node;
181
182         hlist_for_each_entry(peer_vport, node, bucket, hash_node)
183                 if (!strcmp(peer_vport->peer_name, name))
184                         rcu_assign_pointer(peer_vport->peer, vport);
185 }
186
187 static int
188 patch_attach(struct vport *vport)
189 {
190         struct patch_vport *patch_vport = patch_vport_priv(vport);
191
192         hlist_add_head(&patch_vport->hash_node, hash_bucket(patch_vport->peer_name));
193
194         rcu_assign_pointer(patch_vport->peer, vport_locate(patch_vport->peer_name));
195         update_peers(patch_vport->name, vport);
196
197         return 0;
198 }
199
200 static int
201 patch_detach(struct vport *vport)
202 {
203         struct patch_vport *patch_vport = patch_vport_priv(vport);
204
205         update_peers(patch_vport->name, NULL);
206         rcu_assign_pointer(patch_vport->peer, NULL);
207
208         hlist_del(&patch_vport->hash_node);
209
210         return 0;
211 }
212
213 static int
214 patch_set_mtu(struct vport *vport, int mtu)
215 {
216         struct patch_vport *patch_vport = patch_vport_priv(vport);
217         struct device_config *devconf;
218
219         devconf = kmemdup(patch_vport->devconf, sizeof(struct device_config), GFP_KERNEL);
220         if (!devconf)
221                 return -ENOMEM;
222
223         devconf->mtu = mtu;
224         assign_config_rcu(vport, devconf);
225
226         return 0;
227 }
228
229 static int
230 patch_set_addr(struct vport *vport, const unsigned char *addr)
231 {
232         struct patch_vport *patch_vport = patch_vport_priv(vport);
233         struct device_config *devconf;
234
235         devconf = kmemdup(patch_vport->devconf, sizeof(struct device_config), GFP_KERNEL);
236         if (!devconf)
237                 return -ENOMEM;
238
239         memcpy(devconf->eth_addr, addr, ETH_ALEN);
240         assign_config_rcu(vport, devconf);
241
242         return 0;
243 }
244
245
246 static const char *
247 patch_get_name(const struct vport *vport)
248 {
249         const struct patch_vport *patch_vport = patch_vport_priv(vport);
250         return patch_vport->name;
251 }
252
253 static const unsigned char *
254 patch_get_addr(const struct vport *vport)
255 {
256         const struct patch_vport *patch_vport = patch_vport_priv(vport);
257         return rcu_dereference(patch_vport->devconf)->eth_addr;
258 }
259
260 static int
261 patch_get_mtu(const struct vport *vport)
262 {
263         const struct patch_vport *patch_vport = patch_vport_priv(vport);
264         return rcu_dereference(patch_vport->devconf)->mtu;
265 }
266
267 static int
268 patch_send(struct vport *vport, struct sk_buff *skb)
269 {
270         struct patch_vport *patch_vport = patch_vport_priv(vport);
271         struct vport *peer = rcu_dereference(patch_vport->peer);
272         int skb_len = skb->len;
273
274         if (!peer) {
275                 kfree_skb(skb);
276                 vport_record_error(vport, VPORT_E_TX_DROPPED);
277
278                 return 0;
279         }
280
281         vport_receive(peer, skb);
282         return skb_len;
283 }
284
285 struct vport_ops patch_vport_ops = {
286         .type           = "patch",
287         .flags          = VPORT_F_GEN_STATS,
288         .init           = patch_init,
289         .exit           = patch_exit,
290         .create         = patch_create,
291         .modify         = patch_modify,
292         .destroy        = patch_destroy,
293         .attach         = patch_attach,
294         .detach         = patch_detach,
295         .set_mtu        = patch_set_mtu,
296         .set_addr       = patch_set_addr,
297         .get_name       = patch_get_name,
298         .get_addr       = patch_get_addr,
299         .get_dev_flags  = vport_gen_get_dev_flags,
300         .is_running     = vport_gen_is_running,
301         .get_operstate  = vport_gen_get_operstate,
302         .get_mtu        = patch_get_mtu,
303         .send           = patch_send,
304 };