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