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