]> git.proxmox.com Git - ovs.git/blob - datapath/vport-patch.c
ovsdb: Don't check "date" before assignment in ovsdb_file_txn_from_json().
[ovs.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 };
24
25 struct patch_vport {
26 struct rcu_head rcu;
27
28 char name[IFNAMSIZ];
29
30 /* Protected by RTNL lock. */
31 struct hlist_node hash_node;
32
33 struct vport __rcu *peer;
34 struct patch_config __rcu *patchconf;
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 patch_config *c = container_of(rcu, struct patch_config, rcu);
52 kfree(c);
53 }
54
55 static void assign_config_rcu(struct vport *vport,
56 struct patch_config *new_config)
57 {
58 struct patch_vport *patch_vport = patch_vport_priv(vport);
59 struct patch_config *old_config;
60
61 old_config = rtnl_dereference(patch_vport->patchconf);
62 rcu_assign_pointer(patch_vport->patchconf, 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 const struct nla_policy patch_policy[ODP_PATCH_ATTR_MAX + 1] = {
88 #ifdef HAVE_NLA_NUL_STRING
89 [ODP_PATCH_ATTR_PEER] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
90 #endif
91 };
92
93 static int patch_set_config(struct vport *vport, const struct nlattr *options,
94 struct patch_config *patchconf)
95 {
96 struct patch_vport *patch_vport = patch_vport_priv(vport);
97 struct nlattr *a[ODP_PATCH_ATTR_MAX + 1];
98 const char *peer_name;
99 int err;
100
101 if (!options)
102 return -EINVAL;
103
104 err = nla_parse_nested(a, ODP_PATCH_ATTR_MAX, options, patch_policy);
105 if (err)
106 return err;
107
108 if (!a[ODP_PATCH_ATTR_PEER] || VERIFY_NUL_STRING(a[ODP_PATCH_ATTR_PEER], IFNAMSIZ - 1))
109 return -EINVAL;
110
111 peer_name = nla_data(a[ODP_PATCH_ATTR_PEER]);
112 if (!strcmp(patch_vport->name, peer_name))
113 return -EINVAL;
114
115 strcpy(patchconf->peer_name, peer_name);
116
117 return 0;
118 }
119
120 static struct vport *patch_create(const struct vport_parms *parms)
121 {
122 struct vport *vport;
123 struct patch_vport *patch_vport;
124 const char *peer_name;
125 struct patch_config *patchconf;
126 int err;
127
128 vport = vport_alloc(sizeof(struct patch_vport), &patch_vport_ops, parms);
129 if (IS_ERR(vport)) {
130 err = PTR_ERR(vport);
131 goto error;
132 }
133
134 patch_vport = patch_vport_priv(vport);
135
136 strcpy(patch_vport->name, parms->name);
137
138 patchconf = kmalloc(sizeof(struct patch_config), GFP_KERNEL);
139 if (!patchconf) {
140 err = -ENOMEM;
141 goto error_free_vport;
142 }
143
144 err = patch_set_config(vport, parms->options, patchconf);
145 if (err)
146 goto error_free_patchconf;
147
148 vport_gen_rand_ether_addr(patchconf->eth_addr);
149
150 rcu_assign_pointer(patch_vport->patchconf, patchconf);
151
152 peer_name = patchconf->peer_name;
153 hlist_add_head(&patch_vport->hash_node, hash_bucket(peer_name));
154 rcu_assign_pointer(patch_vport->peer, vport_locate(peer_name));
155 update_peers(patch_vport->name, vport);
156
157 return vport;
158
159 error_free_patchconf:
160 kfree(patchconf);
161 error_free_vport:
162 vport_free(vport);
163 error:
164 return ERR_PTR(err);
165 }
166
167 static void free_port_rcu(struct rcu_head *rcu)
168 {
169 struct patch_vport *patch_vport = container_of(rcu,
170 struct patch_vport, rcu);
171
172 kfree((struct patch_config __force *)patch_vport->patchconf);
173 vport_free(vport_from_priv(patch_vport));
174 }
175
176 static int patch_destroy(struct vport *vport)
177 {
178 struct patch_vport *patch_vport = patch_vport_priv(vport);
179
180 update_peers(patch_vport->name, NULL);
181 hlist_del(&patch_vport->hash_node);
182 call_rcu(&patch_vport->rcu, free_port_rcu);
183
184 return 0;
185 }
186
187 static int patch_set_options(struct vport *vport, struct nlattr *options)
188 {
189 struct patch_vport *patch_vport = patch_vport_priv(vport);
190 struct patch_config *patchconf;
191 int err;
192
193 patchconf = kmemdup(rtnl_dereference(patch_vport->patchconf),
194 sizeof(struct patch_config), GFP_KERNEL);
195 if (!patchconf) {
196 err = -ENOMEM;
197 goto error;
198 }
199
200 err = patch_set_config(vport, options, patchconf);
201 if (err)
202 goto error_free;
203
204 assign_config_rcu(vport, patchconf);
205
206 hlist_del(&patch_vport->hash_node);
207
208 rcu_assign_pointer(patch_vport->peer, vport_locate(patchconf->peer_name));
209 hlist_add_head(&patch_vport->hash_node, hash_bucket(patchconf->peer_name));
210
211 return 0;
212
213 error_free:
214 kfree(patchconf);
215 error:
216 return err;
217 }
218
219 static void update_peers(const char *name, struct vport *vport)
220 {
221 struct hlist_head *bucket = hash_bucket(name);
222 struct patch_vport *peer_vport;
223 struct hlist_node *node;
224
225 hlist_for_each_entry(peer_vport, node, bucket, hash_node) {
226 const char *peer_name;
227
228 peer_name = rtnl_dereference(peer_vport->patchconf)->peer_name;
229 if (!strcmp(peer_name, name))
230 rcu_assign_pointer(peer_vport->peer, vport);
231 }
232 }
233
234 static int patch_set_addr(struct vport *vport, const unsigned char *addr)
235 {
236 struct patch_vport *patch_vport = patch_vport_priv(vport);
237 struct patch_config *patchconf;
238
239 patchconf = kmemdup(rtnl_dereference(patch_vport->patchconf),
240 sizeof(struct patch_config), GFP_KERNEL);
241 if (!patchconf)
242 return -ENOMEM;
243
244 memcpy(patchconf->eth_addr, addr, ETH_ALEN);
245 assign_config_rcu(vport, patchconf);
246
247 return 0;
248 }
249
250
251 static const char *patch_get_name(const struct vport *vport)
252 {
253 const struct patch_vport *patch_vport = patch_vport_priv(vport);
254 return patch_vport->name;
255 }
256
257 static const unsigned char *patch_get_addr(const struct vport *vport)
258 {
259 const struct patch_vport *patch_vport = patch_vport_priv(vport);
260 return rcu_dereference_rtnl(patch_vport->patchconf)->eth_addr;
261 }
262
263 static int patch_get_options(const struct vport *vport, struct sk_buff *skb)
264 {
265 struct patch_vport *patch_vport = patch_vport_priv(vport);
266 struct patch_config *patchconf = rcu_dereference_rtnl(patch_vport->patchconf);
267
268 return nla_put_string(skb, ODP_PATCH_ATTR_PEER, patchconf->peer_name);
269 }
270
271 static int patch_send(struct vport *vport, struct sk_buff *skb)
272 {
273 struct patch_vport *patch_vport = patch_vport_priv(vport);
274 struct vport *peer = rcu_dereference(patch_vport->peer);
275 int skb_len = skb->len;
276
277 if (!peer) {
278 kfree_skb(skb);
279 vport_record_error(vport, VPORT_E_TX_DROPPED);
280
281 return 0;
282 }
283
284 vport_receive(peer, skb);
285 return skb_len;
286 }
287
288 const struct vport_ops patch_vport_ops = {
289 .type = ODP_VPORT_TYPE_PATCH,
290 .flags = VPORT_F_GEN_STATS,
291 .init = patch_init,
292 .exit = patch_exit,
293 .create = patch_create,
294 .destroy = patch_destroy,
295 .set_addr = patch_set_addr,
296 .get_name = patch_get_name,
297 .get_addr = patch_get_addr,
298 .get_options = patch_get_options,
299 .set_options = patch_set_options,
300 .get_dev_flags = vport_gen_get_dev_flags,
301 .is_running = vport_gen_is_running,
302 .get_operstate = vport_gen_get_operstate,
303 .send = patch_send,
304 };