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