datapath: Convert patch vport to use call_rcu() on destruction.
[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         struct rcu_head rcu;
26
27         char name[IFNAMSIZ];
28
29         /* Protected by RTNL lock. */
30         char peer_name[IFNAMSIZ];
31         struct hlist_node hash_node;
32
33         /* Protected by RCU. */
34         struct vport *peer;
35
36         /* Protected by RCU. */
37         struct device_config *devconf;
38 };
39
40 /* Protected by RTNL lock. */
41 static struct hlist_head *peer_table;
42 #define PEER_HASH_BUCKETS 256
43
44 static void update_peers(const char *name, struct vport *);
45
46 static inline struct patch_vport *patch_vport_priv(const struct vport *vport)
47 {
48         return vport_priv(vport);
49 }
50
51 /* RCU callback. */
52 static void 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 assign_config_rcu(struct vport *vport,
59                               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 *hash_bucket(const char *name)
70 {
71         unsigned int hash = full_name_hash(name, strlen(name));
72         return &peer_table[hash & (PEER_HASH_BUCKETS - 1)];
73 }
74
75 static int 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 patch_exit(void)
86 {
87         kfree(peer_table);
88 }
89
90 static int set_config(struct vport *vport, const void *config)
91 {
92         struct patch_vport *patch_vport = patch_vport_priv(vport);
93         char peer_name[IFNAMSIZ];
94
95         strlcpy(peer_name, config, IFNAMSIZ);
96
97         if (!strcmp(patch_vport->name, peer_name))
98                 return -EINVAL;
99
100         strcpy(patch_vport->peer_name, peer_name);
101
102         return 0;
103 }
104
105 static struct vport *patch_create(const struct vport_parms *parms)
106 {
107         struct vport *vport;
108         struct patch_vport *patch_vport;
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         err = set_config(vport, parms->config);
122         if (err)
123                 goto error_free_vport;
124
125         patch_vport->devconf = kmalloc(sizeof(struct device_config), GFP_KERNEL);
126         if (!patch_vport->devconf) {
127                 err = -ENOMEM;
128                 goto error_free_vport;
129         }
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         hlist_add_head(&patch_vport->hash_node, hash_bucket(patch_vport->peer_name));
138
139         rcu_assign_pointer(patch_vport->peer, vport_locate(patch_vport->peer_name));
140         update_peers(patch_vport->name, vport);
141
142         return vport;
143
144 error_free_vport:
145         vport_free(vport);
146 error:
147         return ERR_PTR(err);
148 }
149
150 static int patch_modify(struct vport *vport, struct odp_port *port)
151 {
152         int err = set_config(vport, port->config);
153         if (!err) {
154                 struct patch_vport *patch_vport = patch_vport_priv(vport);
155
156                 hlist_del(&patch_vport->hash_node);
157                 rcu_assign_pointer(patch_vport->peer, vport_locate(patch_vport->peer_name));
158                 hlist_add_head(&patch_vport->hash_node, hash_bucket(patch_vport->peer_name));
159         }
160         return err;
161 }
162
163 static void free_port_rcu(struct rcu_head *rcu)
164 {
165         struct patch_vport *patch_vport = container_of(rcu,
166                                                       struct patch_vport, rcu);
167
168         kfree(patch_vport->devconf);
169         vport_free(vport_from_priv(patch_vport));
170 }
171
172 static int patch_destroy(struct vport *vport)
173 {
174         struct patch_vport *patch_vport = patch_vport_priv(vport);
175
176         update_peers(patch_vport->name, NULL);
177         hlist_del(&patch_vport->hash_node);
178         call_rcu(&patch_vport->rcu, free_port_rcu);
179
180         return 0;
181 }
182
183 static void update_peers(const char *name, struct vport *vport)
184 {
185         struct hlist_head *bucket = hash_bucket(name);
186         struct patch_vport *peer_vport;
187         struct hlist_node *node;
188
189         hlist_for_each_entry(peer_vport, node, bucket, hash_node)
190                 if (!strcmp(peer_vport->peer_name, name))
191                         rcu_assign_pointer(peer_vport->peer, vport);
192 }
193
194 static int patch_set_mtu(struct vport *vport, int mtu)
195 {
196         struct patch_vport *patch_vport = patch_vport_priv(vport);
197         struct device_config *devconf;
198
199         devconf = kmemdup(patch_vport->devconf, sizeof(struct device_config), GFP_KERNEL);
200         if (!devconf)
201                 return -ENOMEM;
202
203         devconf->mtu = mtu;
204         assign_config_rcu(vport, devconf);
205
206         return 0;
207 }
208
209 static int patch_set_addr(struct vport *vport, const unsigned char *addr)
210 {
211         struct patch_vport *patch_vport = patch_vport_priv(vport);
212         struct device_config *devconf;
213
214         devconf = kmemdup(patch_vport->devconf, sizeof(struct device_config), GFP_KERNEL);
215         if (!devconf)
216                 return -ENOMEM;
217
218         memcpy(devconf->eth_addr, addr, ETH_ALEN);
219         assign_config_rcu(vport, devconf);
220
221         return 0;
222 }
223
224
225 static const char *patch_get_name(const struct vport *vport)
226 {
227         const struct patch_vport *patch_vport = patch_vport_priv(vport);
228         return patch_vport->name;
229 }
230
231 static const unsigned char *patch_get_addr(const struct vport *vport)
232 {
233         const struct patch_vport *patch_vport = patch_vport_priv(vport);
234         return rcu_dereference(patch_vport->devconf)->eth_addr;
235 }
236
237 static int patch_get_mtu(const struct vport *vport)
238 {
239         const struct patch_vport *patch_vport = patch_vport_priv(vport);
240         return rcu_dereference(patch_vport->devconf)->mtu;
241 }
242
243 static int patch_send(struct vport *vport, struct sk_buff *skb)
244 {
245         struct patch_vport *patch_vport = patch_vport_priv(vport);
246         struct vport *peer = rcu_dereference(patch_vport->peer);
247         int skb_len = skb->len;
248
249         if (!peer) {
250                 kfree_skb(skb);
251                 vport_record_error(vport, VPORT_E_TX_DROPPED);
252
253                 return 0;
254         }
255
256         vport_receive(peer, skb);
257         return skb_len;
258 }
259
260 const struct vport_ops patch_vport_ops = {
261         .type           = "patch",
262         .flags          = VPORT_F_GEN_STATS,
263         .init           = patch_init,
264         .exit           = patch_exit,
265         .create         = patch_create,
266         .modify         = patch_modify,
267         .destroy        = patch_destroy,
268         .set_mtu        = patch_set_mtu,
269         .set_addr       = patch_set_addr,
270         .get_name       = patch_get_name,
271         .get_addr       = patch_get_addr,
272         .get_dev_flags  = vport_gen_get_dev_flags,
273         .is_running     = vport_gen_is_running,
274         .get_operstate  = vport_gen_get_operstate,
275         .get_mtu        = patch_get_mtu,
276         .send           = patch_send,
277 };