]>
Commit | Line | Data |
---|---|---|
ccb1352e | 1 | /* |
caf2ee14 | 2 | * Copyright (c) 2007-2012 Nicira, Inc. |
ccb1352e JG |
3 | * |
4 | * This program is free software; you can redistribute it and/or | |
5 | * modify it under the terms of version 2 of the GNU General Public | |
6 | * License as published by the Free Software Foundation. | |
7 | * | |
8 | * This program is distributed in the hope that it will be useful, but | |
9 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
11 | * General Public License for more details. | |
12 | * | |
13 | * You should have received a copy of the GNU General Public License | |
14 | * along with this program; if not, write to the Free Software | |
15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | |
16 | * 02110-1301, USA | |
17 | */ | |
18 | ||
19 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt | |
20 | ||
21 | #include <linux/init.h> | |
22 | #include <linux/module.h> | |
23 | #include <linux/if_arp.h> | |
24 | #include <linux/if_vlan.h> | |
25 | #include <linux/in.h> | |
26 | #include <linux/ip.h> | |
27 | #include <linux/jhash.h> | |
28 | #include <linux/delay.h> | |
29 | #include <linux/time.h> | |
30 | #include <linux/etherdevice.h> | |
31 | #include <linux/genetlink.h> | |
32 | #include <linux/kernel.h> | |
33 | #include <linux/kthread.h> | |
34 | #include <linux/mutex.h> | |
35 | #include <linux/percpu.h> | |
36 | #include <linux/rcupdate.h> | |
37 | #include <linux/tcp.h> | |
38 | #include <linux/udp.h> | |
ccb1352e JG |
39 | #include <linux/ethtool.h> |
40 | #include <linux/wait.h> | |
ccb1352e JG |
41 | #include <asm/div64.h> |
42 | #include <linux/highmem.h> | |
43 | #include <linux/netfilter_bridge.h> | |
44 | #include <linux/netfilter_ipv4.h> | |
45 | #include <linux/inetdevice.h> | |
46 | #include <linux/list.h> | |
8e4e1713 | 47 | #include <linux/lockdep.h> |
ccb1352e JG |
48 | #include <linux/openvswitch.h> |
49 | #include <linux/rculist.h> | |
50 | #include <linux/dmi.h> | |
51 | #include <linux/workqueue.h> | |
52 | #include <net/genetlink.h> | |
46df7b81 PS |
53 | #include <net/net_namespace.h> |
54 | #include <net/netns/generic.h> | |
ccb1352e JG |
55 | |
56 | #include "datapath.h" | |
57 | #include "flow.h" | |
58 | #include "vport-internal_dev.h" | |
cff63a52 | 59 | #include "vport-netdev.h" |
ccb1352e | 60 | |
46df7b81 PS |
61 | |
62 | #define REHASH_FLOW_INTERVAL (10 * 60 * HZ) | |
63 | static void rehash_flow_table(struct work_struct *work); | |
64 | static DECLARE_DELAYED_WORK(rehash_flow_wq, rehash_flow_table); | |
65 | ||
8e4e1713 PS |
66 | int ovs_net_id __read_mostly; |
67 | ||
ed661185 TG |
68 | static void ovs_notify(struct sk_buff *skb, struct genl_info *info, |
69 | struct genl_multicast_group *grp) | |
70 | { | |
71 | genl_notify(skb, genl_info_net(info), info->snd_portid, | |
72 | grp->id, info->nlhdr, GFP_KERNEL); | |
73 | } | |
74 | ||
ccb1352e JG |
75 | /** |
76 | * DOC: Locking: | |
77 | * | |
8e4e1713 PS |
78 | * All writes e.g. Writes to device state (add/remove datapath, port, set |
79 | * operations on vports, etc.), Writes to other state (flow table | |
80 | * modifications, set miscellaneous datapath parameters, etc.) are protected | |
81 | * by ovs_lock. | |
ccb1352e JG |
82 | * |
83 | * Reads are protected by RCU. | |
84 | * | |
85 | * There are a few special cases (mostly stats) that have their own | |
86 | * synchronization but they nest under all of above and don't interact with | |
87 | * each other. | |
8e4e1713 PS |
88 | * |
89 | * The RTNL lock nests inside ovs_mutex. | |
ccb1352e JG |
90 | */ |
91 | ||
8e4e1713 PS |
92 | static DEFINE_MUTEX(ovs_mutex); |
93 | ||
94 | void ovs_lock(void) | |
95 | { | |
96 | mutex_lock(&ovs_mutex); | |
97 | } | |
98 | ||
99 | void ovs_unlock(void) | |
100 | { | |
101 | mutex_unlock(&ovs_mutex); | |
102 | } | |
103 | ||
104 | #ifdef CONFIG_LOCKDEP | |
105 | int lockdep_ovsl_is_held(void) | |
106 | { | |
107 | if (debug_locks) | |
108 | return lockdep_is_held(&ovs_mutex); | |
109 | else | |
110 | return 1; | |
111 | } | |
112 | #endif | |
113 | ||
ccb1352e | 114 | static struct vport *new_vport(const struct vport_parms *); |
46df7b81 | 115 | static int queue_gso_packets(struct net *, int dp_ifindex, struct sk_buff *, |
ccb1352e | 116 | const struct dp_upcall_info *); |
46df7b81 PS |
117 | static int queue_userspace_packet(struct net *, int dp_ifindex, |
118 | struct sk_buff *, | |
ccb1352e JG |
119 | const struct dp_upcall_info *); |
120 | ||
8e4e1713 | 121 | /* Must be called with rcu_read_lock or ovs_mutex. */ |
46df7b81 | 122 | static struct datapath *get_dp(struct net *net, int dp_ifindex) |
ccb1352e JG |
123 | { |
124 | struct datapath *dp = NULL; | |
125 | struct net_device *dev; | |
126 | ||
127 | rcu_read_lock(); | |
46df7b81 | 128 | dev = dev_get_by_index_rcu(net, dp_ifindex); |
ccb1352e JG |
129 | if (dev) { |
130 | struct vport *vport = ovs_internal_dev_get_vport(dev); | |
131 | if (vport) | |
132 | dp = vport->dp; | |
133 | } | |
134 | rcu_read_unlock(); | |
135 | ||
136 | return dp; | |
137 | } | |
138 | ||
8e4e1713 | 139 | /* Must be called with rcu_read_lock or ovs_mutex. */ |
ccb1352e JG |
140 | const char *ovs_dp_name(const struct datapath *dp) |
141 | { | |
8e4e1713 | 142 | struct vport *vport = ovs_vport_ovsl_rcu(dp, OVSP_LOCAL); |
ccb1352e JG |
143 | return vport->ops->get_name(vport); |
144 | } | |
145 | ||
146 | static int get_dpifindex(struct datapath *dp) | |
147 | { | |
148 | struct vport *local; | |
149 | int ifindex; | |
150 | ||
151 | rcu_read_lock(); | |
152 | ||
15eac2a7 | 153 | local = ovs_vport_rcu(dp, OVSP_LOCAL); |
ccb1352e | 154 | if (local) |
cff63a52 | 155 | ifindex = netdev_vport_priv(local)->dev->ifindex; |
ccb1352e JG |
156 | else |
157 | ifindex = 0; | |
158 | ||
159 | rcu_read_unlock(); | |
160 | ||
161 | return ifindex; | |
162 | } | |
163 | ||
164 | static void destroy_dp_rcu(struct rcu_head *rcu) | |
165 | { | |
166 | struct datapath *dp = container_of(rcu, struct datapath, rcu); | |
167 | ||
168 | ovs_flow_tbl_destroy((__force struct flow_table *)dp->table); | |
169 | free_percpu(dp->stats_percpu); | |
46df7b81 | 170 | release_net(ovs_dp_get_net(dp)); |
15eac2a7 | 171 | kfree(dp->ports); |
ccb1352e JG |
172 | kfree(dp); |
173 | } | |
174 | ||
15eac2a7 PS |
175 | static struct hlist_head *vport_hash_bucket(const struct datapath *dp, |
176 | u16 port_no) | |
177 | { | |
178 | return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)]; | |
179 | } | |
180 | ||
181 | struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no) | |
182 | { | |
183 | struct vport *vport; | |
15eac2a7 PS |
184 | struct hlist_head *head; |
185 | ||
186 | head = vport_hash_bucket(dp, port_no); | |
b67bfe0d | 187 | hlist_for_each_entry_rcu(vport, head, dp_hash_node) { |
15eac2a7 PS |
188 | if (vport->port_no == port_no) |
189 | return vport; | |
190 | } | |
191 | return NULL; | |
192 | } | |
193 | ||
8e4e1713 | 194 | /* Called with ovs_mutex. */ |
ccb1352e JG |
195 | static struct vport *new_vport(const struct vport_parms *parms) |
196 | { | |
197 | struct vport *vport; | |
198 | ||
199 | vport = ovs_vport_add(parms); | |
200 | if (!IS_ERR(vport)) { | |
201 | struct datapath *dp = parms->dp; | |
15eac2a7 | 202 | struct hlist_head *head = vport_hash_bucket(dp, vport->port_no); |
ccb1352e | 203 | |
15eac2a7 | 204 | hlist_add_head_rcu(&vport->dp_hash_node, head); |
ccb1352e | 205 | } |
ccb1352e JG |
206 | return vport; |
207 | } | |
208 | ||
ccb1352e JG |
209 | void ovs_dp_detach_port(struct vport *p) |
210 | { | |
8e4e1713 | 211 | ASSERT_OVSL(); |
ccb1352e JG |
212 | |
213 | /* First drop references to device. */ | |
15eac2a7 | 214 | hlist_del_rcu(&p->dp_hash_node); |
ccb1352e JG |
215 | |
216 | /* Then destroy it. */ | |
217 | ovs_vport_del(p); | |
218 | } | |
219 | ||
220 | /* Must be called with rcu_read_lock. */ | |
221 | void ovs_dp_process_received_packet(struct vport *p, struct sk_buff *skb) | |
222 | { | |
223 | struct datapath *dp = p->dp; | |
224 | struct sw_flow *flow; | |
225 | struct dp_stats_percpu *stats; | |
226 | struct sw_flow_key key; | |
227 | u64 *stats_counter; | |
228 | int error; | |
229 | int key_len; | |
230 | ||
404f2f10 | 231 | stats = this_cpu_ptr(dp->stats_percpu); |
ccb1352e JG |
232 | |
233 | /* Extract flow from 'skb' into 'key'. */ | |
234 | error = ovs_flow_extract(skb, p->port_no, &key, &key_len); | |
235 | if (unlikely(error)) { | |
236 | kfree_skb(skb); | |
237 | return; | |
238 | } | |
239 | ||
240 | /* Look up flow. */ | |
241 | flow = ovs_flow_tbl_lookup(rcu_dereference(dp->table), &key, key_len); | |
242 | if (unlikely(!flow)) { | |
243 | struct dp_upcall_info upcall; | |
244 | ||
245 | upcall.cmd = OVS_PACKET_CMD_MISS; | |
246 | upcall.key = &key; | |
247 | upcall.userdata = NULL; | |
15e47304 | 248 | upcall.portid = p->upcall_portid; |
ccb1352e JG |
249 | ovs_dp_upcall(dp, skb, &upcall); |
250 | consume_skb(skb); | |
251 | stats_counter = &stats->n_missed; | |
252 | goto out; | |
253 | } | |
254 | ||
255 | OVS_CB(skb)->flow = flow; | |
256 | ||
257 | stats_counter = &stats->n_hit; | |
258 | ovs_flow_used(OVS_CB(skb)->flow, skb); | |
259 | ovs_execute_actions(dp, skb); | |
260 | ||
261 | out: | |
262 | /* Update datapath statistics. */ | |
263 | u64_stats_update_begin(&stats->sync); | |
264 | (*stats_counter)++; | |
265 | u64_stats_update_end(&stats->sync); | |
266 | } | |
267 | ||
268 | static struct genl_family dp_packet_genl_family = { | |
269 | .id = GENL_ID_GENERATE, | |
270 | .hdrsize = sizeof(struct ovs_header), | |
271 | .name = OVS_PACKET_FAMILY, | |
272 | .version = OVS_PACKET_VERSION, | |
46df7b81 | 273 | .maxattr = OVS_PACKET_ATTR_MAX, |
3a4e0d6a PS |
274 | .netnsok = true, |
275 | .parallel_ops = true, | |
ccb1352e JG |
276 | }; |
277 | ||
278 | int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb, | |
46df7b81 | 279 | const struct dp_upcall_info *upcall_info) |
ccb1352e JG |
280 | { |
281 | struct dp_stats_percpu *stats; | |
282 | int dp_ifindex; | |
283 | int err; | |
284 | ||
15e47304 | 285 | if (upcall_info->portid == 0) { |
ccb1352e JG |
286 | err = -ENOTCONN; |
287 | goto err; | |
288 | } | |
289 | ||
290 | dp_ifindex = get_dpifindex(dp); | |
291 | if (!dp_ifindex) { | |
292 | err = -ENODEV; | |
293 | goto err; | |
294 | } | |
295 | ||
296 | if (!skb_is_gso(skb)) | |
46df7b81 | 297 | err = queue_userspace_packet(ovs_dp_get_net(dp), dp_ifindex, skb, upcall_info); |
ccb1352e | 298 | else |
46df7b81 | 299 | err = queue_gso_packets(ovs_dp_get_net(dp), dp_ifindex, skb, upcall_info); |
ccb1352e JG |
300 | if (err) |
301 | goto err; | |
302 | ||
303 | return 0; | |
304 | ||
305 | err: | |
404f2f10 | 306 | stats = this_cpu_ptr(dp->stats_percpu); |
ccb1352e JG |
307 | |
308 | u64_stats_update_begin(&stats->sync); | |
309 | stats->n_lost++; | |
310 | u64_stats_update_end(&stats->sync); | |
311 | ||
312 | return err; | |
313 | } | |
314 | ||
46df7b81 PS |
315 | static int queue_gso_packets(struct net *net, int dp_ifindex, |
316 | struct sk_buff *skb, | |
ccb1352e JG |
317 | const struct dp_upcall_info *upcall_info) |
318 | { | |
a1b5d0dd | 319 | unsigned short gso_type = skb_shinfo(skb)->gso_type; |
ccb1352e JG |
320 | struct dp_upcall_info later_info; |
321 | struct sw_flow_key later_key; | |
322 | struct sk_buff *segs, *nskb; | |
323 | int err; | |
324 | ||
12b0004d | 325 | segs = __skb_gso_segment(skb, NETIF_F_SG | NETIF_F_HW_CSUM, false); |
92e5dfc3 PS |
326 | if (IS_ERR(segs)) |
327 | return PTR_ERR(segs); | |
ccb1352e JG |
328 | |
329 | /* Queue all of the segments. */ | |
330 | skb = segs; | |
331 | do { | |
46df7b81 | 332 | err = queue_userspace_packet(net, dp_ifindex, skb, upcall_info); |
ccb1352e JG |
333 | if (err) |
334 | break; | |
335 | ||
a1b5d0dd | 336 | if (skb == segs && gso_type & SKB_GSO_UDP) { |
ccb1352e JG |
337 | /* The initial flow key extracted by ovs_flow_extract() |
338 | * in this case is for a first fragment, so we need to | |
339 | * properly mark later fragments. | |
340 | */ | |
341 | later_key = *upcall_info->key; | |
342 | later_key.ip.frag = OVS_FRAG_TYPE_LATER; | |
343 | ||
344 | later_info = *upcall_info; | |
345 | later_info.key = &later_key; | |
346 | upcall_info = &later_info; | |
347 | } | |
348 | } while ((skb = skb->next)); | |
349 | ||
350 | /* Free all of the segments. */ | |
351 | skb = segs; | |
352 | do { | |
353 | nskb = skb->next; | |
354 | if (err) | |
355 | kfree_skb(skb); | |
356 | else | |
357 | consume_skb(skb); | |
358 | } while ((skb = nskb)); | |
359 | return err; | |
360 | } | |
361 | ||
c3ff8cfe TG |
362 | static size_t key_attr_size(void) |
363 | { | |
364 | return nla_total_size(4) /* OVS_KEY_ATTR_PRIORITY */ | |
365 | + nla_total_size(4) /* OVS_KEY_ATTR_IN_PORT */ | |
366 | + nla_total_size(4) /* OVS_KEY_ATTR_SKB_MARK */ | |
367 | + nla_total_size(12) /* OVS_KEY_ATTR_ETHERNET */ | |
368 | + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */ | |
369 | + nla_total_size(4) /* OVS_KEY_ATTR_8021Q */ | |
370 | + nla_total_size(0) /* OVS_KEY_ATTR_ENCAP */ | |
371 | + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */ | |
372 | + nla_total_size(40) /* OVS_KEY_ATTR_IPV6 */ | |
373 | + nla_total_size(2) /* OVS_KEY_ATTR_ICMPV6 */ | |
374 | + nla_total_size(28); /* OVS_KEY_ATTR_ND */ | |
375 | } | |
376 | ||
377 | static size_t upcall_msg_size(const struct sk_buff *skb, | |
378 | const struct nlattr *userdata) | |
379 | { | |
380 | size_t size = NLMSG_ALIGN(sizeof(struct ovs_header)) | |
381 | + nla_total_size(skb->len) /* OVS_PACKET_ATTR_PACKET */ | |
382 | + nla_total_size(key_attr_size()); /* OVS_PACKET_ATTR_KEY */ | |
383 | ||
384 | /* OVS_PACKET_ATTR_USERDATA */ | |
385 | if (userdata) | |
386 | size += NLA_ALIGN(userdata->nla_len); | |
387 | ||
388 | return size; | |
389 | } | |
390 | ||
46df7b81 PS |
391 | static int queue_userspace_packet(struct net *net, int dp_ifindex, |
392 | struct sk_buff *skb, | |
ccb1352e JG |
393 | const struct dp_upcall_info *upcall_info) |
394 | { | |
395 | struct ovs_header *upcall; | |
396 | struct sk_buff *nskb = NULL; | |
397 | struct sk_buff *user_skb; /* to be queued to userspace */ | |
398 | struct nlattr *nla; | |
ccb1352e JG |
399 | int err; |
400 | ||
401 | if (vlan_tx_tag_present(skb)) { | |
402 | nskb = skb_clone(skb, GFP_ATOMIC); | |
403 | if (!nskb) | |
404 | return -ENOMEM; | |
405 | ||
86a9bad3 | 406 | nskb = __vlan_put_tag(nskb, nskb->vlan_proto, vlan_tx_tag_get(nskb)); |
8aa51d64 | 407 | if (!nskb) |
ccb1352e JG |
408 | return -ENOMEM; |
409 | ||
410 | nskb->vlan_tci = 0; | |
411 | skb = nskb; | |
412 | } | |
413 | ||
414 | if (nla_attr_size(skb->len) > USHRT_MAX) { | |
415 | err = -EFBIG; | |
416 | goto out; | |
417 | } | |
418 | ||
c3ff8cfe | 419 | user_skb = genlmsg_new(upcall_msg_size(skb, upcall_info->userdata), GFP_ATOMIC); |
ccb1352e JG |
420 | if (!user_skb) { |
421 | err = -ENOMEM; | |
422 | goto out; | |
423 | } | |
424 | ||
425 | upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family, | |
426 | 0, upcall_info->cmd); | |
427 | upcall->dp_ifindex = dp_ifindex; | |
428 | ||
429 | nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY); | |
430 | ovs_flow_to_nlattrs(upcall_info->key, user_skb); | |
431 | nla_nest_end(user_skb, nla); | |
432 | ||
433 | if (upcall_info->userdata) | |
4490108b BP |
434 | __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA, |
435 | nla_len(upcall_info->userdata), | |
436 | nla_data(upcall_info->userdata)); | |
ccb1352e JG |
437 | |
438 | nla = __nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, skb->len); | |
439 | ||
440 | skb_copy_and_csum_dev(skb, nla_data(nla)); | |
441 | ||
a15ff76c | 442 | genlmsg_end(user_skb, upcall); |
15e47304 | 443 | err = genlmsg_unicast(net, user_skb, upcall_info->portid); |
ccb1352e JG |
444 | |
445 | out: | |
446 | kfree_skb(nskb); | |
447 | return err; | |
448 | } | |
449 | ||
8e4e1713 | 450 | /* Called with ovs_mutex. */ |
46df7b81 | 451 | static int flush_flows(struct datapath *dp) |
ccb1352e JG |
452 | { |
453 | struct flow_table *old_table; | |
454 | struct flow_table *new_table; | |
ccb1352e | 455 | |
8e4e1713 | 456 | old_table = ovsl_dereference(dp->table); |
ccb1352e JG |
457 | new_table = ovs_flow_tbl_alloc(TBL_MIN_BUCKETS); |
458 | if (!new_table) | |
459 | return -ENOMEM; | |
460 | ||
461 | rcu_assign_pointer(dp->table, new_table); | |
462 | ||
463 | ovs_flow_tbl_deferred_destroy(old_table); | |
464 | return 0; | |
465 | } | |
466 | ||
467 | static int validate_actions(const struct nlattr *attr, | |
468 | const struct sw_flow_key *key, int depth); | |
469 | ||
470 | static int validate_sample(const struct nlattr *attr, | |
471 | const struct sw_flow_key *key, int depth) | |
472 | { | |
473 | const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1]; | |
474 | const struct nlattr *probability, *actions; | |
475 | const struct nlattr *a; | |
476 | int rem; | |
477 | ||
478 | memset(attrs, 0, sizeof(attrs)); | |
479 | nla_for_each_nested(a, attr, rem) { | |
480 | int type = nla_type(a); | |
481 | if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type]) | |
482 | return -EINVAL; | |
483 | attrs[type] = a; | |
484 | } | |
485 | if (rem) | |
486 | return -EINVAL; | |
487 | ||
488 | probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY]; | |
489 | if (!probability || nla_len(probability) != sizeof(u32)) | |
490 | return -EINVAL; | |
491 | ||
492 | actions = attrs[OVS_SAMPLE_ATTR_ACTIONS]; | |
493 | if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN)) | |
494 | return -EINVAL; | |
495 | return validate_actions(actions, key, depth + 1); | |
496 | } | |
497 | ||
072ae631 PS |
498 | static int validate_tp_port(const struct sw_flow_key *flow_key) |
499 | { | |
500 | if (flow_key->eth.type == htons(ETH_P_IP)) { | |
4185392d | 501 | if (flow_key->ipv4.tp.src || flow_key->ipv4.tp.dst) |
072ae631 PS |
502 | return 0; |
503 | } else if (flow_key->eth.type == htons(ETH_P_IPV6)) { | |
4185392d | 504 | if (flow_key->ipv6.tp.src || flow_key->ipv6.tp.dst) |
072ae631 PS |
505 | return 0; |
506 | } | |
507 | ||
508 | return -EINVAL; | |
509 | } | |
510 | ||
ccb1352e JG |
511 | static int validate_set(const struct nlattr *a, |
512 | const struct sw_flow_key *flow_key) | |
513 | { | |
514 | const struct nlattr *ovs_key = nla_data(a); | |
515 | int key_type = nla_type(ovs_key); | |
516 | ||
517 | /* There can be only one key in a action */ | |
518 | if (nla_total_size(nla_len(ovs_key)) != nla_len(a)) | |
519 | return -EINVAL; | |
520 | ||
521 | if (key_type > OVS_KEY_ATTR_MAX || | |
522 | nla_len(ovs_key) != ovs_key_lens[key_type]) | |
523 | return -EINVAL; | |
524 | ||
525 | switch (key_type) { | |
526 | const struct ovs_key_ipv4 *ipv4_key; | |
3fdbd1ce | 527 | const struct ovs_key_ipv6 *ipv6_key; |
ccb1352e JG |
528 | |
529 | case OVS_KEY_ATTR_PRIORITY: | |
39c7caeb | 530 | case OVS_KEY_ATTR_SKB_MARK: |
ccb1352e JG |
531 | case OVS_KEY_ATTR_ETHERNET: |
532 | break; | |
533 | ||
534 | case OVS_KEY_ATTR_IPV4: | |
535 | if (flow_key->eth.type != htons(ETH_P_IP)) | |
536 | return -EINVAL; | |
537 | ||
4185392d | 538 | if (!flow_key->ip.proto) |
ccb1352e JG |
539 | return -EINVAL; |
540 | ||
541 | ipv4_key = nla_data(ovs_key); | |
542 | if (ipv4_key->ipv4_proto != flow_key->ip.proto) | |
543 | return -EINVAL; | |
544 | ||
545 | if (ipv4_key->ipv4_frag != flow_key->ip.frag) | |
546 | return -EINVAL; | |
547 | ||
548 | break; | |
549 | ||
3fdbd1ce AA |
550 | case OVS_KEY_ATTR_IPV6: |
551 | if (flow_key->eth.type != htons(ETH_P_IPV6)) | |
552 | return -EINVAL; | |
553 | ||
554 | if (!flow_key->ip.proto) | |
555 | return -EINVAL; | |
556 | ||
557 | ipv6_key = nla_data(ovs_key); | |
558 | if (ipv6_key->ipv6_proto != flow_key->ip.proto) | |
559 | return -EINVAL; | |
560 | ||
561 | if (ipv6_key->ipv6_frag != flow_key->ip.frag) | |
562 | return -EINVAL; | |
563 | ||
564 | if (ntohl(ipv6_key->ipv6_label) & 0xFFF00000) | |
565 | return -EINVAL; | |
566 | ||
567 | break; | |
568 | ||
ccb1352e JG |
569 | case OVS_KEY_ATTR_TCP: |
570 | if (flow_key->ip.proto != IPPROTO_TCP) | |
571 | return -EINVAL; | |
572 | ||
072ae631 | 573 | return validate_tp_port(flow_key); |
ccb1352e JG |
574 | |
575 | case OVS_KEY_ATTR_UDP: | |
576 | if (flow_key->ip.proto != IPPROTO_UDP) | |
577 | return -EINVAL; | |
578 | ||
072ae631 | 579 | return validate_tp_port(flow_key); |
ccb1352e JG |
580 | |
581 | default: | |
582 | return -EINVAL; | |
583 | } | |
584 | ||
585 | return 0; | |
586 | } | |
587 | ||
588 | static int validate_userspace(const struct nlattr *attr) | |
589 | { | |
590 | static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = { | |
591 | [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 }, | |
4490108b | 592 | [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_UNSPEC }, |
ccb1352e JG |
593 | }; |
594 | struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1]; | |
595 | int error; | |
596 | ||
597 | error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX, | |
598 | attr, userspace_policy); | |
599 | if (error) | |
600 | return error; | |
601 | ||
602 | if (!a[OVS_USERSPACE_ATTR_PID] || | |
603 | !nla_get_u32(a[OVS_USERSPACE_ATTR_PID])) | |
604 | return -EINVAL; | |
605 | ||
606 | return 0; | |
607 | } | |
608 | ||
609 | static int validate_actions(const struct nlattr *attr, | |
610 | const struct sw_flow_key *key, int depth) | |
611 | { | |
612 | const struct nlattr *a; | |
613 | int rem, err; | |
614 | ||
615 | if (depth >= SAMPLE_ACTION_DEPTH) | |
616 | return -EOVERFLOW; | |
617 | ||
618 | nla_for_each_nested(a, attr, rem) { | |
619 | /* Expected argument lengths, (u32)-1 for variable length. */ | |
620 | static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = { | |
621 | [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32), | |
622 | [OVS_ACTION_ATTR_USERSPACE] = (u32)-1, | |
623 | [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan), | |
624 | [OVS_ACTION_ATTR_POP_VLAN] = 0, | |
625 | [OVS_ACTION_ATTR_SET] = (u32)-1, | |
626 | [OVS_ACTION_ATTR_SAMPLE] = (u32)-1 | |
627 | }; | |
628 | const struct ovs_action_push_vlan *vlan; | |
629 | int type = nla_type(a); | |
630 | ||
631 | if (type > OVS_ACTION_ATTR_MAX || | |
632 | (action_lens[type] != nla_len(a) && | |
633 | action_lens[type] != (u32)-1)) | |
634 | return -EINVAL; | |
635 | ||
636 | switch (type) { | |
637 | case OVS_ACTION_ATTR_UNSPEC: | |
638 | return -EINVAL; | |
639 | ||
640 | case OVS_ACTION_ATTR_USERSPACE: | |
641 | err = validate_userspace(a); | |
642 | if (err) | |
643 | return err; | |
644 | break; | |
645 | ||
646 | case OVS_ACTION_ATTR_OUTPUT: | |
647 | if (nla_get_u32(a) >= DP_MAX_PORTS) | |
648 | return -EINVAL; | |
649 | break; | |
650 | ||
651 | ||
652 | case OVS_ACTION_ATTR_POP_VLAN: | |
653 | break; | |
654 | ||
655 | case OVS_ACTION_ATTR_PUSH_VLAN: | |
656 | vlan = nla_data(a); | |
657 | if (vlan->vlan_tpid != htons(ETH_P_8021Q)) | |
658 | return -EINVAL; | |
659 | if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT))) | |
660 | return -EINVAL; | |
661 | break; | |
662 | ||
663 | case OVS_ACTION_ATTR_SET: | |
664 | err = validate_set(a, key); | |
665 | if (err) | |
666 | return err; | |
667 | break; | |
668 | ||
669 | case OVS_ACTION_ATTR_SAMPLE: | |
670 | err = validate_sample(a, key, depth); | |
671 | if (err) | |
672 | return err; | |
673 | break; | |
674 | ||
675 | default: | |
676 | return -EINVAL; | |
677 | } | |
678 | } | |
679 | ||
680 | if (rem > 0) | |
681 | return -EINVAL; | |
682 | ||
683 | return 0; | |
684 | } | |
685 | ||
686 | static void clear_stats(struct sw_flow *flow) | |
687 | { | |
688 | flow->used = 0; | |
689 | flow->tcp_flags = 0; | |
690 | flow->packet_count = 0; | |
691 | flow->byte_count = 0; | |
692 | } | |
693 | ||
694 | static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info) | |
695 | { | |
696 | struct ovs_header *ovs_header = info->userhdr; | |
697 | struct nlattr **a = info->attrs; | |
698 | struct sw_flow_actions *acts; | |
699 | struct sk_buff *packet; | |
700 | struct sw_flow *flow; | |
701 | struct datapath *dp; | |
702 | struct ethhdr *eth; | |
703 | int len; | |
704 | int err; | |
705 | int key_len; | |
706 | ||
707 | err = -EINVAL; | |
708 | if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] || | |
dded45fc | 709 | !a[OVS_PACKET_ATTR_ACTIONS]) |
ccb1352e JG |
710 | goto err; |
711 | ||
712 | len = nla_len(a[OVS_PACKET_ATTR_PACKET]); | |
713 | packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL); | |
714 | err = -ENOMEM; | |
715 | if (!packet) | |
716 | goto err; | |
717 | skb_reserve(packet, NET_IP_ALIGN); | |
718 | ||
32686a9d | 719 | nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len); |
ccb1352e JG |
720 | |
721 | skb_reset_mac_header(packet); | |
722 | eth = eth_hdr(packet); | |
723 | ||
724 | /* Normally, setting the skb 'protocol' field would be handled by a | |
725 | * call to eth_type_trans(), but it assumes there's a sending | |
726 | * device, which we may not have. */ | |
e5c5d22e | 727 | if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN) |
ccb1352e JG |
728 | packet->protocol = eth->h_proto; |
729 | else | |
730 | packet->protocol = htons(ETH_P_802_2); | |
731 | ||
732 | /* Build an sw_flow for sending this packet. */ | |
733 | flow = ovs_flow_alloc(); | |
734 | err = PTR_ERR(flow); | |
735 | if (IS_ERR(flow)) | |
736 | goto err_kfree_skb; | |
737 | ||
738 | err = ovs_flow_extract(packet, -1, &flow->key, &key_len); | |
739 | if (err) | |
740 | goto err_flow_free; | |
741 | ||
93d8fd15 | 742 | err = ovs_flow_metadata_from_nlattrs(flow, a[OVS_PACKET_ATTR_KEY]); |
ccb1352e JG |
743 | if (err) |
744 | goto err_flow_free; | |
745 | ||
746 | err = validate_actions(a[OVS_PACKET_ATTR_ACTIONS], &flow->key, 0); | |
747 | if (err) | |
748 | goto err_flow_free; | |
749 | ||
750 | flow->hash = ovs_flow_hash(&flow->key, key_len); | |
751 | ||
752 | acts = ovs_flow_actions_alloc(a[OVS_PACKET_ATTR_ACTIONS]); | |
753 | err = PTR_ERR(acts); | |
754 | if (IS_ERR(acts)) | |
755 | goto err_flow_free; | |
756 | rcu_assign_pointer(flow->sf_acts, acts); | |
757 | ||
758 | OVS_CB(packet)->flow = flow; | |
759 | packet->priority = flow->key.phy.priority; | |
39c7caeb | 760 | packet->mark = flow->key.phy.skb_mark; |
ccb1352e JG |
761 | |
762 | rcu_read_lock(); | |
46df7b81 | 763 | dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex); |
ccb1352e JG |
764 | err = -ENODEV; |
765 | if (!dp) | |
766 | goto err_unlock; | |
767 | ||
768 | local_bh_disable(); | |
769 | err = ovs_execute_actions(dp, packet); | |
770 | local_bh_enable(); | |
771 | rcu_read_unlock(); | |
772 | ||
773 | ovs_flow_free(flow); | |
774 | return err; | |
775 | ||
776 | err_unlock: | |
777 | rcu_read_unlock(); | |
778 | err_flow_free: | |
779 | ovs_flow_free(flow); | |
780 | err_kfree_skb: | |
781 | kfree_skb(packet); | |
782 | err: | |
783 | return err; | |
784 | } | |
785 | ||
786 | static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = { | |
dded45fc | 787 | [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN }, |
ccb1352e JG |
788 | [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED }, |
789 | [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED }, | |
790 | }; | |
791 | ||
792 | static struct genl_ops dp_packet_genl_ops[] = { | |
793 | { .cmd = OVS_PACKET_CMD_EXECUTE, | |
794 | .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ | |
795 | .policy = packet_policy, | |
796 | .doit = ovs_packet_cmd_execute | |
797 | } | |
798 | }; | |
799 | ||
800 | static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats) | |
801 | { | |
802 | int i; | |
8e4e1713 | 803 | struct flow_table *table = ovsl_dereference(dp->table); |
ccb1352e JG |
804 | |
805 | stats->n_flows = ovs_flow_tbl_count(table); | |
806 | ||
807 | stats->n_hit = stats->n_missed = stats->n_lost = 0; | |
808 | for_each_possible_cpu(i) { | |
809 | const struct dp_stats_percpu *percpu_stats; | |
810 | struct dp_stats_percpu local_stats; | |
811 | unsigned int start; | |
812 | ||
813 | percpu_stats = per_cpu_ptr(dp->stats_percpu, i); | |
814 | ||
815 | do { | |
816 | start = u64_stats_fetch_begin_bh(&percpu_stats->sync); | |
817 | local_stats = *percpu_stats; | |
818 | } while (u64_stats_fetch_retry_bh(&percpu_stats->sync, start)); | |
819 | ||
820 | stats->n_hit += local_stats.n_hit; | |
821 | stats->n_missed += local_stats.n_missed; | |
822 | stats->n_lost += local_stats.n_lost; | |
823 | } | |
824 | } | |
825 | ||
826 | static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = { | |
827 | [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED }, | |
828 | [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED }, | |
829 | [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG }, | |
830 | }; | |
831 | ||
832 | static struct genl_family dp_flow_genl_family = { | |
833 | .id = GENL_ID_GENERATE, | |
834 | .hdrsize = sizeof(struct ovs_header), | |
835 | .name = OVS_FLOW_FAMILY, | |
836 | .version = OVS_FLOW_VERSION, | |
46df7b81 | 837 | .maxattr = OVS_FLOW_ATTR_MAX, |
3a4e0d6a PS |
838 | .netnsok = true, |
839 | .parallel_ops = true, | |
ccb1352e JG |
840 | }; |
841 | ||
842 | static struct genl_multicast_group ovs_dp_flow_multicast_group = { | |
843 | .name = OVS_FLOW_MCGROUP | |
844 | }; | |
845 | ||
c3ff8cfe TG |
846 | static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts) |
847 | { | |
848 | return NLMSG_ALIGN(sizeof(struct ovs_header)) | |
849 | + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_KEY */ | |
850 | + nla_total_size(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */ | |
851 | + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */ | |
852 | + nla_total_size(8) /* OVS_FLOW_ATTR_USED */ | |
853 | + nla_total_size(acts->actions_len); /* OVS_FLOW_ATTR_ACTIONS */ | |
854 | } | |
855 | ||
8e4e1713 | 856 | /* Called with ovs_mutex. */ |
ccb1352e | 857 | static int ovs_flow_cmd_fill_info(struct sw_flow *flow, struct datapath *dp, |
15e47304 | 858 | struct sk_buff *skb, u32 portid, |
ccb1352e JG |
859 | u32 seq, u32 flags, u8 cmd) |
860 | { | |
861 | const int skb_orig_len = skb->len; | |
862 | const struct sw_flow_actions *sf_acts; | |
863 | struct ovs_flow_stats stats; | |
864 | struct ovs_header *ovs_header; | |
865 | struct nlattr *nla; | |
866 | unsigned long used; | |
867 | u8 tcp_flags; | |
868 | int err; | |
869 | ||
8e4e1713 | 870 | sf_acts = ovsl_dereference(flow->sf_acts); |
ccb1352e | 871 | |
15e47304 | 872 | ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family, flags, cmd); |
ccb1352e JG |
873 | if (!ovs_header) |
874 | return -EMSGSIZE; | |
875 | ||
876 | ovs_header->dp_ifindex = get_dpifindex(dp); | |
877 | ||
878 | nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY); | |
879 | if (!nla) | |
880 | goto nla_put_failure; | |
881 | err = ovs_flow_to_nlattrs(&flow->key, skb); | |
882 | if (err) | |
883 | goto error; | |
884 | nla_nest_end(skb, nla); | |
885 | ||
886 | spin_lock_bh(&flow->lock); | |
887 | used = flow->used; | |
888 | stats.n_packets = flow->packet_count; | |
889 | stats.n_bytes = flow->byte_count; | |
890 | tcp_flags = flow->tcp_flags; | |
891 | spin_unlock_bh(&flow->lock); | |
892 | ||
028d6a67 DM |
893 | if (used && |
894 | nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used))) | |
895 | goto nla_put_failure; | |
ccb1352e | 896 | |
028d6a67 DM |
897 | if (stats.n_packets && |
898 | nla_put(skb, OVS_FLOW_ATTR_STATS, | |
899 | sizeof(struct ovs_flow_stats), &stats)) | |
900 | goto nla_put_failure; | |
ccb1352e | 901 | |
028d6a67 DM |
902 | if (tcp_flags && |
903 | nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, tcp_flags)) | |
904 | goto nla_put_failure; | |
ccb1352e JG |
905 | |
906 | /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if | |
907 | * this is the first flow to be dumped into 'skb'. This is unusual for | |
908 | * Netlink but individual action lists can be longer than | |
909 | * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this. | |
910 | * The userspace caller can always fetch the actions separately if it | |
911 | * really wants them. (Most userspace callers in fact don't care.) | |
912 | * | |
913 | * This can only fail for dump operations because the skb is always | |
914 | * properly sized for single flows. | |
915 | */ | |
916 | err = nla_put(skb, OVS_FLOW_ATTR_ACTIONS, sf_acts->actions_len, | |
917 | sf_acts->actions); | |
918 | if (err < 0 && skb_orig_len) | |
919 | goto error; | |
920 | ||
921 | return genlmsg_end(skb, ovs_header); | |
922 | ||
923 | nla_put_failure: | |
924 | err = -EMSGSIZE; | |
925 | error: | |
926 | genlmsg_cancel(skb, ovs_header); | |
927 | return err; | |
928 | } | |
929 | ||
930 | static struct sk_buff *ovs_flow_cmd_alloc_info(struct sw_flow *flow) | |
931 | { | |
932 | const struct sw_flow_actions *sf_acts; | |
ccb1352e | 933 | |
8e4e1713 | 934 | sf_acts = ovsl_dereference(flow->sf_acts); |
ccb1352e | 935 | |
c3ff8cfe | 936 | return genlmsg_new(ovs_flow_cmd_msg_size(sf_acts), GFP_KERNEL); |
ccb1352e JG |
937 | } |
938 | ||
939 | static struct sk_buff *ovs_flow_cmd_build_info(struct sw_flow *flow, | |
940 | struct datapath *dp, | |
15e47304 | 941 | u32 portid, u32 seq, u8 cmd) |
ccb1352e JG |
942 | { |
943 | struct sk_buff *skb; | |
944 | int retval; | |
945 | ||
946 | skb = ovs_flow_cmd_alloc_info(flow); | |
947 | if (!skb) | |
948 | return ERR_PTR(-ENOMEM); | |
949 | ||
15e47304 | 950 | retval = ovs_flow_cmd_fill_info(flow, dp, skb, portid, seq, 0, cmd); |
ccb1352e JG |
951 | BUG_ON(retval < 0); |
952 | return skb; | |
953 | } | |
954 | ||
955 | static int ovs_flow_cmd_new_or_set(struct sk_buff *skb, struct genl_info *info) | |
956 | { | |
957 | struct nlattr **a = info->attrs; | |
958 | struct ovs_header *ovs_header = info->userhdr; | |
959 | struct sw_flow_key key; | |
960 | struct sw_flow *flow; | |
961 | struct sk_buff *reply; | |
962 | struct datapath *dp; | |
963 | struct flow_table *table; | |
964 | int error; | |
965 | int key_len; | |
966 | ||
967 | /* Extract key. */ | |
968 | error = -EINVAL; | |
969 | if (!a[OVS_FLOW_ATTR_KEY]) | |
970 | goto error; | |
971 | error = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]); | |
972 | if (error) | |
973 | goto error; | |
974 | ||
975 | /* Validate actions. */ | |
976 | if (a[OVS_FLOW_ATTR_ACTIONS]) { | |
977 | error = validate_actions(a[OVS_FLOW_ATTR_ACTIONS], &key, 0); | |
978 | if (error) | |
979 | goto error; | |
980 | } else if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW) { | |
981 | error = -EINVAL; | |
982 | goto error; | |
983 | } | |
984 | ||
8e4e1713 | 985 | ovs_lock(); |
46df7b81 | 986 | dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex); |
ccb1352e JG |
987 | error = -ENODEV; |
988 | if (!dp) | |
8e4e1713 | 989 | goto err_unlock_ovs; |
ccb1352e | 990 | |
8e4e1713 | 991 | table = ovsl_dereference(dp->table); |
ccb1352e JG |
992 | flow = ovs_flow_tbl_lookup(table, &key, key_len); |
993 | if (!flow) { | |
994 | struct sw_flow_actions *acts; | |
995 | ||
996 | /* Bail out if we're not allowed to create a new flow. */ | |
997 | error = -ENOENT; | |
998 | if (info->genlhdr->cmd == OVS_FLOW_CMD_SET) | |
8e4e1713 | 999 | goto err_unlock_ovs; |
ccb1352e JG |
1000 | |
1001 | /* Expand table, if necessary, to make room. */ | |
1002 | if (ovs_flow_tbl_need_to_expand(table)) { | |
1003 | struct flow_table *new_table; | |
1004 | ||
1005 | new_table = ovs_flow_tbl_expand(table); | |
1006 | if (!IS_ERR(new_table)) { | |
1007 | rcu_assign_pointer(dp->table, new_table); | |
1008 | ovs_flow_tbl_deferred_destroy(table); | |
8e4e1713 | 1009 | table = ovsl_dereference(dp->table); |
ccb1352e JG |
1010 | } |
1011 | } | |
1012 | ||
1013 | /* Allocate flow. */ | |
1014 | flow = ovs_flow_alloc(); | |
1015 | if (IS_ERR(flow)) { | |
1016 | error = PTR_ERR(flow); | |
8e4e1713 | 1017 | goto err_unlock_ovs; |
ccb1352e JG |
1018 | } |
1019 | flow->key = key; | |
1020 | clear_stats(flow); | |
1021 | ||
1022 | /* Obtain actions. */ | |
1023 | acts = ovs_flow_actions_alloc(a[OVS_FLOW_ATTR_ACTIONS]); | |
1024 | error = PTR_ERR(acts); | |
1025 | if (IS_ERR(acts)) | |
1026 | goto error_free_flow; | |
1027 | rcu_assign_pointer(flow->sf_acts, acts); | |
1028 | ||
1029 | /* Put flow in bucket. */ | |
1030 | flow->hash = ovs_flow_hash(&key, key_len); | |
1031 | ovs_flow_tbl_insert(table, flow); | |
1032 | ||
15e47304 | 1033 | reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid, |
ccb1352e JG |
1034 | info->snd_seq, |
1035 | OVS_FLOW_CMD_NEW); | |
1036 | } else { | |
1037 | /* We found a matching flow. */ | |
1038 | struct sw_flow_actions *old_acts; | |
1039 | struct nlattr *acts_attrs; | |
1040 | ||
1041 | /* Bail out if we're not allowed to modify an existing flow. | |
1042 | * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL | |
1043 | * because Generic Netlink treats the latter as a dump | |
1044 | * request. We also accept NLM_F_EXCL in case that bug ever | |
1045 | * gets fixed. | |
1046 | */ | |
1047 | error = -EEXIST; | |
1048 | if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW && | |
1049 | info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL)) | |
8e4e1713 | 1050 | goto err_unlock_ovs; |
ccb1352e JG |
1051 | |
1052 | /* Update actions. */ | |
8e4e1713 | 1053 | old_acts = ovsl_dereference(flow->sf_acts); |
ccb1352e JG |
1054 | acts_attrs = a[OVS_FLOW_ATTR_ACTIONS]; |
1055 | if (acts_attrs && | |
1056 | (old_acts->actions_len != nla_len(acts_attrs) || | |
1057 | memcmp(old_acts->actions, nla_data(acts_attrs), | |
1058 | old_acts->actions_len))) { | |
1059 | struct sw_flow_actions *new_acts; | |
1060 | ||
1061 | new_acts = ovs_flow_actions_alloc(acts_attrs); | |
1062 | error = PTR_ERR(new_acts); | |
1063 | if (IS_ERR(new_acts)) | |
8e4e1713 | 1064 | goto err_unlock_ovs; |
ccb1352e JG |
1065 | |
1066 | rcu_assign_pointer(flow->sf_acts, new_acts); | |
1067 | ovs_flow_deferred_free_acts(old_acts); | |
1068 | } | |
1069 | ||
15e47304 | 1070 | reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid, |
ccb1352e JG |
1071 | info->snd_seq, OVS_FLOW_CMD_NEW); |
1072 | ||
1073 | /* Clear stats. */ | |
1074 | if (a[OVS_FLOW_ATTR_CLEAR]) { | |
1075 | spin_lock_bh(&flow->lock); | |
1076 | clear_stats(flow); | |
1077 | spin_unlock_bh(&flow->lock); | |
1078 | } | |
1079 | } | |
8e4e1713 | 1080 | ovs_unlock(); |
ccb1352e JG |
1081 | |
1082 | if (!IS_ERR(reply)) | |
ed661185 | 1083 | ovs_notify(reply, info, &ovs_dp_flow_multicast_group); |
ccb1352e | 1084 | else |
46df7b81 | 1085 | netlink_set_err(sock_net(skb->sk)->genl_sock, 0, |
ccb1352e JG |
1086 | ovs_dp_flow_multicast_group.id, PTR_ERR(reply)); |
1087 | return 0; | |
1088 | ||
1089 | error_free_flow: | |
1090 | ovs_flow_free(flow); | |
8e4e1713 PS |
1091 | err_unlock_ovs: |
1092 | ovs_unlock(); | |
ccb1352e JG |
1093 | error: |
1094 | return error; | |
1095 | } | |
1096 | ||
1097 | static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info) | |
1098 | { | |
1099 | struct nlattr **a = info->attrs; | |
1100 | struct ovs_header *ovs_header = info->userhdr; | |
1101 | struct sw_flow_key key; | |
1102 | struct sk_buff *reply; | |
1103 | struct sw_flow *flow; | |
1104 | struct datapath *dp; | |
1105 | struct flow_table *table; | |
1106 | int err; | |
1107 | int key_len; | |
1108 | ||
1109 | if (!a[OVS_FLOW_ATTR_KEY]) | |
1110 | return -EINVAL; | |
1111 | err = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]); | |
1112 | if (err) | |
1113 | return err; | |
1114 | ||
8e4e1713 | 1115 | ovs_lock(); |
46df7b81 | 1116 | dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex); |
8e4e1713 PS |
1117 | if (!dp) { |
1118 | err = -ENODEV; | |
1119 | goto unlock; | |
1120 | } | |
ccb1352e | 1121 | |
8e4e1713 | 1122 | table = ovsl_dereference(dp->table); |
ccb1352e | 1123 | flow = ovs_flow_tbl_lookup(table, &key, key_len); |
8e4e1713 PS |
1124 | if (!flow) { |
1125 | err = -ENOENT; | |
1126 | goto unlock; | |
1127 | } | |
ccb1352e | 1128 | |
15e47304 | 1129 | reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid, |
ccb1352e | 1130 | info->snd_seq, OVS_FLOW_CMD_NEW); |
8e4e1713 PS |
1131 | if (IS_ERR(reply)) { |
1132 | err = PTR_ERR(reply); | |
1133 | goto unlock; | |
1134 | } | |
ccb1352e | 1135 | |
8e4e1713 | 1136 | ovs_unlock(); |
ccb1352e | 1137 | return genlmsg_reply(reply, info); |
8e4e1713 PS |
1138 | unlock: |
1139 | ovs_unlock(); | |
1140 | return err; | |
ccb1352e JG |
1141 | } |
1142 | ||
1143 | static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info) | |
1144 | { | |
1145 | struct nlattr **a = info->attrs; | |
1146 | struct ovs_header *ovs_header = info->userhdr; | |
1147 | struct sw_flow_key key; | |
1148 | struct sk_buff *reply; | |
1149 | struct sw_flow *flow; | |
1150 | struct datapath *dp; | |
1151 | struct flow_table *table; | |
1152 | int err; | |
1153 | int key_len; | |
1154 | ||
8e4e1713 | 1155 | ovs_lock(); |
46df7b81 | 1156 | dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex); |
8e4e1713 PS |
1157 | if (!dp) { |
1158 | err = -ENODEV; | |
1159 | goto unlock; | |
1160 | } | |
46df7b81 | 1161 | |
8e4e1713 PS |
1162 | if (!a[OVS_FLOW_ATTR_KEY]) { |
1163 | err = flush_flows(dp); | |
1164 | goto unlock; | |
1165 | } | |
ccb1352e JG |
1166 | err = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]); |
1167 | if (err) | |
8e4e1713 | 1168 | goto unlock; |
ccb1352e | 1169 | |
8e4e1713 | 1170 | table = ovsl_dereference(dp->table); |
ccb1352e | 1171 | flow = ovs_flow_tbl_lookup(table, &key, key_len); |
8e4e1713 PS |
1172 | if (!flow) { |
1173 | err = -ENOENT; | |
1174 | goto unlock; | |
1175 | } | |
ccb1352e JG |
1176 | |
1177 | reply = ovs_flow_cmd_alloc_info(flow); | |
8e4e1713 PS |
1178 | if (!reply) { |
1179 | err = -ENOMEM; | |
1180 | goto unlock; | |
1181 | } | |
ccb1352e JG |
1182 | |
1183 | ovs_flow_tbl_remove(table, flow); | |
1184 | ||
15e47304 | 1185 | err = ovs_flow_cmd_fill_info(flow, dp, reply, info->snd_portid, |
ccb1352e JG |
1186 | info->snd_seq, 0, OVS_FLOW_CMD_DEL); |
1187 | BUG_ON(err < 0); | |
1188 | ||
1189 | ovs_flow_deferred_free(flow); | |
8e4e1713 | 1190 | ovs_unlock(); |
ccb1352e | 1191 | |
ed661185 | 1192 | ovs_notify(reply, info, &ovs_dp_flow_multicast_group); |
ccb1352e | 1193 | return 0; |
8e4e1713 PS |
1194 | unlock: |
1195 | ovs_unlock(); | |
1196 | return err; | |
ccb1352e JG |
1197 | } |
1198 | ||
1199 | static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb) | |
1200 | { | |
1201 | struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh)); | |
1202 | struct datapath *dp; | |
1203 | struct flow_table *table; | |
1204 | ||
8e4e1713 | 1205 | ovs_lock(); |
46df7b81 | 1206 | dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex); |
8e4e1713 PS |
1207 | if (!dp) { |
1208 | ovs_unlock(); | |
ccb1352e | 1209 | return -ENODEV; |
8e4e1713 | 1210 | } |
ccb1352e | 1211 | |
8e4e1713 | 1212 | table = ovsl_dereference(dp->table); |
ccb1352e JG |
1213 | |
1214 | for (;;) { | |
1215 | struct sw_flow *flow; | |
1216 | u32 bucket, obj; | |
1217 | ||
1218 | bucket = cb->args[0]; | |
1219 | obj = cb->args[1]; | |
1220 | flow = ovs_flow_tbl_next(table, &bucket, &obj); | |
1221 | if (!flow) | |
1222 | break; | |
1223 | ||
1224 | if (ovs_flow_cmd_fill_info(flow, dp, skb, | |
15e47304 | 1225 | NETLINK_CB(cb->skb).portid, |
ccb1352e JG |
1226 | cb->nlh->nlmsg_seq, NLM_F_MULTI, |
1227 | OVS_FLOW_CMD_NEW) < 0) | |
1228 | break; | |
1229 | ||
1230 | cb->args[0] = bucket; | |
1231 | cb->args[1] = obj; | |
1232 | } | |
8e4e1713 | 1233 | ovs_unlock(); |
ccb1352e JG |
1234 | return skb->len; |
1235 | } | |
1236 | ||
1237 | static struct genl_ops dp_flow_genl_ops[] = { | |
1238 | { .cmd = OVS_FLOW_CMD_NEW, | |
1239 | .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ | |
1240 | .policy = flow_policy, | |
1241 | .doit = ovs_flow_cmd_new_or_set | |
1242 | }, | |
1243 | { .cmd = OVS_FLOW_CMD_DEL, | |
1244 | .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ | |
1245 | .policy = flow_policy, | |
1246 | .doit = ovs_flow_cmd_del | |
1247 | }, | |
1248 | { .cmd = OVS_FLOW_CMD_GET, | |
1249 | .flags = 0, /* OK for unprivileged users. */ | |
1250 | .policy = flow_policy, | |
1251 | .doit = ovs_flow_cmd_get, | |
1252 | .dumpit = ovs_flow_cmd_dump | |
1253 | }, | |
1254 | { .cmd = OVS_FLOW_CMD_SET, | |
1255 | .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ | |
1256 | .policy = flow_policy, | |
1257 | .doit = ovs_flow_cmd_new_or_set, | |
1258 | }, | |
1259 | }; | |
1260 | ||
1261 | static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = { | |
1262 | [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 }, | |
1263 | [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 }, | |
1264 | }; | |
1265 | ||
1266 | static struct genl_family dp_datapath_genl_family = { | |
1267 | .id = GENL_ID_GENERATE, | |
1268 | .hdrsize = sizeof(struct ovs_header), | |
1269 | .name = OVS_DATAPATH_FAMILY, | |
1270 | .version = OVS_DATAPATH_VERSION, | |
46df7b81 | 1271 | .maxattr = OVS_DP_ATTR_MAX, |
3a4e0d6a PS |
1272 | .netnsok = true, |
1273 | .parallel_ops = true, | |
ccb1352e JG |
1274 | }; |
1275 | ||
1276 | static struct genl_multicast_group ovs_dp_datapath_multicast_group = { | |
1277 | .name = OVS_DATAPATH_MCGROUP | |
1278 | }; | |
1279 | ||
c3ff8cfe TG |
1280 | static size_t ovs_dp_cmd_msg_size(void) |
1281 | { | |
1282 | size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header)); | |
1283 | ||
1284 | msgsize += nla_total_size(IFNAMSIZ); | |
1285 | msgsize += nla_total_size(sizeof(struct ovs_dp_stats)); | |
1286 | ||
1287 | return msgsize; | |
1288 | } | |
1289 | ||
ccb1352e | 1290 | static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb, |
15e47304 | 1291 | u32 portid, u32 seq, u32 flags, u8 cmd) |
ccb1352e JG |
1292 | { |
1293 | struct ovs_header *ovs_header; | |
1294 | struct ovs_dp_stats dp_stats; | |
1295 | int err; | |
1296 | ||
15e47304 | 1297 | ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family, |
ccb1352e JG |
1298 | flags, cmd); |
1299 | if (!ovs_header) | |
1300 | goto error; | |
1301 | ||
1302 | ovs_header->dp_ifindex = get_dpifindex(dp); | |
1303 | ||
1304 | rcu_read_lock(); | |
1305 | err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp)); | |
1306 | rcu_read_unlock(); | |
1307 | if (err) | |
1308 | goto nla_put_failure; | |
1309 | ||
1310 | get_dp_stats(dp, &dp_stats); | |
028d6a67 DM |
1311 | if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats), &dp_stats)) |
1312 | goto nla_put_failure; | |
ccb1352e JG |
1313 | |
1314 | return genlmsg_end(skb, ovs_header); | |
1315 | ||
1316 | nla_put_failure: | |
1317 | genlmsg_cancel(skb, ovs_header); | |
1318 | error: | |
1319 | return -EMSGSIZE; | |
1320 | } | |
1321 | ||
15e47304 | 1322 | static struct sk_buff *ovs_dp_cmd_build_info(struct datapath *dp, u32 portid, |
ccb1352e JG |
1323 | u32 seq, u8 cmd) |
1324 | { | |
1325 | struct sk_buff *skb; | |
1326 | int retval; | |
1327 | ||
c3ff8cfe | 1328 | skb = genlmsg_new(ovs_dp_cmd_msg_size(), GFP_KERNEL); |
ccb1352e JG |
1329 | if (!skb) |
1330 | return ERR_PTR(-ENOMEM); | |
1331 | ||
15e47304 | 1332 | retval = ovs_dp_cmd_fill_info(dp, skb, portid, seq, 0, cmd); |
ccb1352e JG |
1333 | if (retval < 0) { |
1334 | kfree_skb(skb); | |
1335 | return ERR_PTR(retval); | |
1336 | } | |
1337 | return skb; | |
1338 | } | |
1339 | ||
8e4e1713 | 1340 | /* Called with ovs_mutex. */ |
46df7b81 PS |
1341 | static struct datapath *lookup_datapath(struct net *net, |
1342 | struct ovs_header *ovs_header, | |
ccb1352e JG |
1343 | struct nlattr *a[OVS_DP_ATTR_MAX + 1]) |
1344 | { | |
1345 | struct datapath *dp; | |
1346 | ||
1347 | if (!a[OVS_DP_ATTR_NAME]) | |
46df7b81 | 1348 | dp = get_dp(net, ovs_header->dp_ifindex); |
ccb1352e JG |
1349 | else { |
1350 | struct vport *vport; | |
1351 | ||
1352 | rcu_read_lock(); | |
46df7b81 | 1353 | vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME])); |
ccb1352e JG |
1354 | dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL; |
1355 | rcu_read_unlock(); | |
1356 | } | |
1357 | return dp ? dp : ERR_PTR(-ENODEV); | |
1358 | } | |
1359 | ||
1360 | static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info) | |
1361 | { | |
1362 | struct nlattr **a = info->attrs; | |
1363 | struct vport_parms parms; | |
1364 | struct sk_buff *reply; | |
1365 | struct datapath *dp; | |
1366 | struct vport *vport; | |
46df7b81 | 1367 | struct ovs_net *ovs_net; |
15eac2a7 | 1368 | int err, i; |
ccb1352e JG |
1369 | |
1370 | err = -EINVAL; | |
1371 | if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID]) | |
1372 | goto err; | |
1373 | ||
8e4e1713 | 1374 | ovs_lock(); |
ccb1352e JG |
1375 | |
1376 | err = -ENOMEM; | |
1377 | dp = kzalloc(sizeof(*dp), GFP_KERNEL); | |
1378 | if (dp == NULL) | |
8e4e1713 | 1379 | goto err_unlock_ovs; |
46df7b81 | 1380 | |
46df7b81 | 1381 | ovs_dp_set_net(dp, hold_net(sock_net(skb->sk))); |
ccb1352e JG |
1382 | |
1383 | /* Allocate table. */ | |
1384 | err = -ENOMEM; | |
1385 | rcu_assign_pointer(dp->table, ovs_flow_tbl_alloc(TBL_MIN_BUCKETS)); | |
1386 | if (!dp->table) | |
1387 | goto err_free_dp; | |
1388 | ||
1389 | dp->stats_percpu = alloc_percpu(struct dp_stats_percpu); | |
1390 | if (!dp->stats_percpu) { | |
1391 | err = -ENOMEM; | |
1392 | goto err_destroy_table; | |
1393 | } | |
1394 | ||
15eac2a7 PS |
1395 | dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head), |
1396 | GFP_KERNEL); | |
1397 | if (!dp->ports) { | |
1398 | err = -ENOMEM; | |
1399 | goto err_destroy_percpu; | |
1400 | } | |
1401 | ||
1402 | for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) | |
1403 | INIT_HLIST_HEAD(&dp->ports[i]); | |
1404 | ||
ccb1352e JG |
1405 | /* Set up our datapath device. */ |
1406 | parms.name = nla_data(a[OVS_DP_ATTR_NAME]); | |
1407 | parms.type = OVS_VPORT_TYPE_INTERNAL; | |
1408 | parms.options = NULL; | |
1409 | parms.dp = dp; | |
1410 | parms.port_no = OVSP_LOCAL; | |
15e47304 | 1411 | parms.upcall_portid = nla_get_u32(a[OVS_DP_ATTR_UPCALL_PID]); |
ccb1352e JG |
1412 | |
1413 | vport = new_vport(&parms); | |
1414 | if (IS_ERR(vport)) { | |
1415 | err = PTR_ERR(vport); | |
1416 | if (err == -EBUSY) | |
1417 | err = -EEXIST; | |
1418 | ||
15eac2a7 | 1419 | goto err_destroy_ports_array; |
ccb1352e JG |
1420 | } |
1421 | ||
15e47304 | 1422 | reply = ovs_dp_cmd_build_info(dp, info->snd_portid, |
ccb1352e JG |
1423 | info->snd_seq, OVS_DP_CMD_NEW); |
1424 | err = PTR_ERR(reply); | |
1425 | if (IS_ERR(reply)) | |
1426 | goto err_destroy_local_port; | |
1427 | ||
46df7b81 PS |
1428 | ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id); |
1429 | list_add_tail(&dp->list_node, &ovs_net->dps); | |
8e4e1713 PS |
1430 | |
1431 | ovs_unlock(); | |
ccb1352e | 1432 | |
ed661185 | 1433 | ovs_notify(reply, info, &ovs_dp_datapath_multicast_group); |
ccb1352e JG |
1434 | return 0; |
1435 | ||
1436 | err_destroy_local_port: | |
8e4e1713 | 1437 | ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL)); |
15eac2a7 PS |
1438 | err_destroy_ports_array: |
1439 | kfree(dp->ports); | |
ccb1352e JG |
1440 | err_destroy_percpu: |
1441 | free_percpu(dp->stats_percpu); | |
1442 | err_destroy_table: | |
8e4e1713 | 1443 | ovs_flow_tbl_destroy(ovsl_dereference(dp->table)); |
ccb1352e | 1444 | err_free_dp: |
46df7b81 | 1445 | release_net(ovs_dp_get_net(dp)); |
ccb1352e | 1446 | kfree(dp); |
8e4e1713 PS |
1447 | err_unlock_ovs: |
1448 | ovs_unlock(); | |
ccb1352e JG |
1449 | err: |
1450 | return err; | |
1451 | } | |
1452 | ||
8e4e1713 | 1453 | /* Called with ovs_mutex. */ |
46df7b81 | 1454 | static void __dp_destroy(struct datapath *dp) |
ccb1352e | 1455 | { |
15eac2a7 | 1456 | int i; |
ccb1352e | 1457 | |
15eac2a7 PS |
1458 | for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) { |
1459 | struct vport *vport; | |
b67bfe0d | 1460 | struct hlist_node *n; |
15eac2a7 | 1461 | |
b67bfe0d | 1462 | hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node) |
15eac2a7 PS |
1463 | if (vport->port_no != OVSP_LOCAL) |
1464 | ovs_dp_detach_port(vport); | |
1465 | } | |
ccb1352e JG |
1466 | |
1467 | list_del(&dp->list_node); | |
ccb1352e | 1468 | |
8e4e1713 PS |
1469 | /* OVSP_LOCAL is datapath internal port. We need to make sure that |
1470 | * all port in datapath are destroyed first before freeing datapath. | |
ccb1352e | 1471 | */ |
8e4e1713 | 1472 | ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL)); |
ccb1352e JG |
1473 | |
1474 | call_rcu(&dp->rcu, destroy_dp_rcu); | |
46df7b81 PS |
1475 | } |
1476 | ||
1477 | static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info) | |
1478 | { | |
1479 | struct sk_buff *reply; | |
1480 | struct datapath *dp; | |
1481 | int err; | |
1482 | ||
8e4e1713 | 1483 | ovs_lock(); |
46df7b81 PS |
1484 | dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs); |
1485 | err = PTR_ERR(dp); | |
1486 | if (IS_ERR(dp)) | |
8e4e1713 | 1487 | goto unlock; |
46df7b81 | 1488 | |
15e47304 | 1489 | reply = ovs_dp_cmd_build_info(dp, info->snd_portid, |
46df7b81 PS |
1490 | info->snd_seq, OVS_DP_CMD_DEL); |
1491 | err = PTR_ERR(reply); | |
1492 | if (IS_ERR(reply)) | |
8e4e1713 | 1493 | goto unlock; |
46df7b81 PS |
1494 | |
1495 | __dp_destroy(dp); | |
8e4e1713 | 1496 | ovs_unlock(); |
ccb1352e | 1497 | |
ed661185 | 1498 | ovs_notify(reply, info, &ovs_dp_datapath_multicast_group); |
ccb1352e JG |
1499 | |
1500 | return 0; | |
8e4e1713 PS |
1501 | unlock: |
1502 | ovs_unlock(); | |
1503 | return err; | |
ccb1352e JG |
1504 | } |
1505 | ||
1506 | static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info) | |
1507 | { | |
1508 | struct sk_buff *reply; | |
1509 | struct datapath *dp; | |
1510 | int err; | |
1511 | ||
8e4e1713 | 1512 | ovs_lock(); |
46df7b81 | 1513 | dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs); |
8e4e1713 | 1514 | err = PTR_ERR(dp); |
ccb1352e | 1515 | if (IS_ERR(dp)) |
8e4e1713 | 1516 | goto unlock; |
ccb1352e | 1517 | |
15e47304 | 1518 | reply = ovs_dp_cmd_build_info(dp, info->snd_portid, |
ccb1352e JG |
1519 | info->snd_seq, OVS_DP_CMD_NEW); |
1520 | if (IS_ERR(reply)) { | |
1521 | err = PTR_ERR(reply); | |
46df7b81 | 1522 | netlink_set_err(sock_net(skb->sk)->genl_sock, 0, |
ccb1352e | 1523 | ovs_dp_datapath_multicast_group.id, err); |
8e4e1713 PS |
1524 | err = 0; |
1525 | goto unlock; | |
ccb1352e JG |
1526 | } |
1527 | ||
8e4e1713 | 1528 | ovs_unlock(); |
ed661185 | 1529 | ovs_notify(reply, info, &ovs_dp_datapath_multicast_group); |
ccb1352e JG |
1530 | |
1531 | return 0; | |
8e4e1713 PS |
1532 | unlock: |
1533 | ovs_unlock(); | |
1534 | return err; | |
ccb1352e JG |
1535 | } |
1536 | ||
1537 | static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info) | |
1538 | { | |
1539 | struct sk_buff *reply; | |
1540 | struct datapath *dp; | |
8e4e1713 | 1541 | int err; |
ccb1352e | 1542 | |
8e4e1713 | 1543 | ovs_lock(); |
46df7b81 | 1544 | dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs); |
8e4e1713 PS |
1545 | if (IS_ERR(dp)) { |
1546 | err = PTR_ERR(dp); | |
1547 | goto unlock; | |
1548 | } | |
ccb1352e | 1549 | |
15e47304 | 1550 | reply = ovs_dp_cmd_build_info(dp, info->snd_portid, |
ccb1352e | 1551 | info->snd_seq, OVS_DP_CMD_NEW); |
8e4e1713 PS |
1552 | if (IS_ERR(reply)) { |
1553 | err = PTR_ERR(reply); | |
1554 | goto unlock; | |
1555 | } | |
ccb1352e | 1556 | |
8e4e1713 | 1557 | ovs_unlock(); |
ccb1352e | 1558 | return genlmsg_reply(reply, info); |
8e4e1713 PS |
1559 | |
1560 | unlock: | |
1561 | ovs_unlock(); | |
1562 | return err; | |
ccb1352e JG |
1563 | } |
1564 | ||
1565 | static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb) | |
1566 | { | |
46df7b81 | 1567 | struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id); |
ccb1352e JG |
1568 | struct datapath *dp; |
1569 | int skip = cb->args[0]; | |
1570 | int i = 0; | |
1571 | ||
8e4e1713 | 1572 | ovs_lock(); |
46df7b81 | 1573 | list_for_each_entry(dp, &ovs_net->dps, list_node) { |
77676fdb | 1574 | if (i >= skip && |
15e47304 | 1575 | ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid, |
ccb1352e JG |
1576 | cb->nlh->nlmsg_seq, NLM_F_MULTI, |
1577 | OVS_DP_CMD_NEW) < 0) | |
1578 | break; | |
1579 | i++; | |
1580 | } | |
8e4e1713 | 1581 | ovs_unlock(); |
ccb1352e JG |
1582 | |
1583 | cb->args[0] = i; | |
1584 | ||
1585 | return skb->len; | |
1586 | } | |
1587 | ||
1588 | static struct genl_ops dp_datapath_genl_ops[] = { | |
1589 | { .cmd = OVS_DP_CMD_NEW, | |
1590 | .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ | |
1591 | .policy = datapath_policy, | |
1592 | .doit = ovs_dp_cmd_new | |
1593 | }, | |
1594 | { .cmd = OVS_DP_CMD_DEL, | |
1595 | .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ | |
1596 | .policy = datapath_policy, | |
1597 | .doit = ovs_dp_cmd_del | |
1598 | }, | |
1599 | { .cmd = OVS_DP_CMD_GET, | |
1600 | .flags = 0, /* OK for unprivileged users. */ | |
1601 | .policy = datapath_policy, | |
1602 | .doit = ovs_dp_cmd_get, | |
1603 | .dumpit = ovs_dp_cmd_dump | |
1604 | }, | |
1605 | { .cmd = OVS_DP_CMD_SET, | |
1606 | .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ | |
1607 | .policy = datapath_policy, | |
1608 | .doit = ovs_dp_cmd_set, | |
1609 | }, | |
1610 | }; | |
1611 | ||
1612 | static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = { | |
1613 | [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 }, | |
1614 | [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) }, | |
1615 | [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 }, | |
1616 | [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 }, | |
1617 | [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 }, | |
1618 | [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED }, | |
1619 | }; | |
1620 | ||
1621 | static struct genl_family dp_vport_genl_family = { | |
1622 | .id = GENL_ID_GENERATE, | |
1623 | .hdrsize = sizeof(struct ovs_header), | |
1624 | .name = OVS_VPORT_FAMILY, | |
1625 | .version = OVS_VPORT_VERSION, | |
46df7b81 | 1626 | .maxattr = OVS_VPORT_ATTR_MAX, |
3a4e0d6a PS |
1627 | .netnsok = true, |
1628 | .parallel_ops = true, | |
ccb1352e JG |
1629 | }; |
1630 | ||
1631 | struct genl_multicast_group ovs_dp_vport_multicast_group = { | |
1632 | .name = OVS_VPORT_MCGROUP | |
1633 | }; | |
1634 | ||
8e4e1713 | 1635 | /* Called with ovs_mutex or RCU read lock. */ |
ccb1352e | 1636 | static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb, |
15e47304 | 1637 | u32 portid, u32 seq, u32 flags, u8 cmd) |
ccb1352e JG |
1638 | { |
1639 | struct ovs_header *ovs_header; | |
1640 | struct ovs_vport_stats vport_stats; | |
1641 | int err; | |
1642 | ||
15e47304 | 1643 | ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family, |
ccb1352e JG |
1644 | flags, cmd); |
1645 | if (!ovs_header) | |
1646 | return -EMSGSIZE; | |
1647 | ||
1648 | ovs_header->dp_ifindex = get_dpifindex(vport->dp); | |
1649 | ||
028d6a67 DM |
1650 | if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) || |
1651 | nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) || | |
1652 | nla_put_string(skb, OVS_VPORT_ATTR_NAME, vport->ops->get_name(vport)) || | |
15e47304 | 1653 | nla_put_u32(skb, OVS_VPORT_ATTR_UPCALL_PID, vport->upcall_portid)) |
028d6a67 | 1654 | goto nla_put_failure; |
ccb1352e JG |
1655 | |
1656 | ovs_vport_get_stats(vport, &vport_stats); | |
028d6a67 DM |
1657 | if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats), |
1658 | &vport_stats)) | |
1659 | goto nla_put_failure; | |
ccb1352e JG |
1660 | |
1661 | err = ovs_vport_get_options(vport, skb); | |
1662 | if (err == -EMSGSIZE) | |
1663 | goto error; | |
1664 | ||
1665 | return genlmsg_end(skb, ovs_header); | |
1666 | ||
1667 | nla_put_failure: | |
1668 | err = -EMSGSIZE; | |
1669 | error: | |
1670 | genlmsg_cancel(skb, ovs_header); | |
1671 | return err; | |
1672 | } | |
1673 | ||
8e4e1713 | 1674 | /* Called with ovs_mutex or RCU read lock. */ |
15e47304 | 1675 | struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid, |
ccb1352e JG |
1676 | u32 seq, u8 cmd) |
1677 | { | |
1678 | struct sk_buff *skb; | |
1679 | int retval; | |
1680 | ||
1681 | skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC); | |
1682 | if (!skb) | |
1683 | return ERR_PTR(-ENOMEM); | |
1684 | ||
15e47304 | 1685 | retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd); |
a9341512 JG |
1686 | BUG_ON(retval < 0); |
1687 | ||
ccb1352e JG |
1688 | return skb; |
1689 | } | |
1690 | ||
8e4e1713 | 1691 | /* Called with ovs_mutex or RCU read lock. */ |
46df7b81 PS |
1692 | static struct vport *lookup_vport(struct net *net, |
1693 | struct ovs_header *ovs_header, | |
ccb1352e JG |
1694 | struct nlattr *a[OVS_VPORT_ATTR_MAX + 1]) |
1695 | { | |
1696 | struct datapath *dp; | |
1697 | struct vport *vport; | |
1698 | ||
1699 | if (a[OVS_VPORT_ATTR_NAME]) { | |
46df7b81 | 1700 | vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME])); |
ccb1352e JG |
1701 | if (!vport) |
1702 | return ERR_PTR(-ENODEV); | |
651a68ea BP |
1703 | if (ovs_header->dp_ifindex && |
1704 | ovs_header->dp_ifindex != get_dpifindex(vport->dp)) | |
1705 | return ERR_PTR(-ENODEV); | |
ccb1352e JG |
1706 | return vport; |
1707 | } else if (a[OVS_VPORT_ATTR_PORT_NO]) { | |
1708 | u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]); | |
1709 | ||
1710 | if (port_no >= DP_MAX_PORTS) | |
1711 | return ERR_PTR(-EFBIG); | |
1712 | ||
46df7b81 | 1713 | dp = get_dp(net, ovs_header->dp_ifindex); |
ccb1352e JG |
1714 | if (!dp) |
1715 | return ERR_PTR(-ENODEV); | |
1716 | ||
8e4e1713 | 1717 | vport = ovs_vport_ovsl_rcu(dp, port_no); |
ccb1352e | 1718 | if (!vport) |
14408dba | 1719 | return ERR_PTR(-ENODEV); |
ccb1352e JG |
1720 | return vport; |
1721 | } else | |
1722 | return ERR_PTR(-EINVAL); | |
1723 | } | |
1724 | ||
1725 | static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info) | |
1726 | { | |
1727 | struct nlattr **a = info->attrs; | |
1728 | struct ovs_header *ovs_header = info->userhdr; | |
1729 | struct vport_parms parms; | |
1730 | struct sk_buff *reply; | |
1731 | struct vport *vport; | |
1732 | struct datapath *dp; | |
1733 | u32 port_no; | |
1734 | int err; | |
1735 | ||
1736 | err = -EINVAL; | |
1737 | if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] || | |
1738 | !a[OVS_VPORT_ATTR_UPCALL_PID]) | |
1739 | goto exit; | |
1740 | ||
8e4e1713 | 1741 | ovs_lock(); |
46df7b81 | 1742 | dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex); |
ccb1352e JG |
1743 | err = -ENODEV; |
1744 | if (!dp) | |
1745 | goto exit_unlock; | |
1746 | ||
1747 | if (a[OVS_VPORT_ATTR_PORT_NO]) { | |
1748 | port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]); | |
1749 | ||
1750 | err = -EFBIG; | |
1751 | if (port_no >= DP_MAX_PORTS) | |
1752 | goto exit_unlock; | |
1753 | ||
8e4e1713 | 1754 | vport = ovs_vport_ovsl(dp, port_no); |
ccb1352e JG |
1755 | err = -EBUSY; |
1756 | if (vport) | |
1757 | goto exit_unlock; | |
1758 | } else { | |
1759 | for (port_no = 1; ; port_no++) { | |
1760 | if (port_no >= DP_MAX_PORTS) { | |
1761 | err = -EFBIG; | |
1762 | goto exit_unlock; | |
1763 | } | |
8e4e1713 | 1764 | vport = ovs_vport_ovsl(dp, port_no); |
ccb1352e JG |
1765 | if (!vport) |
1766 | break; | |
1767 | } | |
1768 | } | |
1769 | ||
1770 | parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]); | |
1771 | parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]); | |
1772 | parms.options = a[OVS_VPORT_ATTR_OPTIONS]; | |
1773 | parms.dp = dp; | |
1774 | parms.port_no = port_no; | |
15e47304 | 1775 | parms.upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]); |
ccb1352e JG |
1776 | |
1777 | vport = new_vport(&parms); | |
1778 | err = PTR_ERR(vport); | |
1779 | if (IS_ERR(vport)) | |
1780 | goto exit_unlock; | |
1781 | ||
cb7c5bdf | 1782 | err = 0; |
15e47304 | 1783 | reply = ovs_vport_cmd_build_info(vport, info->snd_portid, info->snd_seq, |
ccb1352e JG |
1784 | OVS_VPORT_CMD_NEW); |
1785 | if (IS_ERR(reply)) { | |
1786 | err = PTR_ERR(reply); | |
1787 | ovs_dp_detach_port(vport); | |
1788 | goto exit_unlock; | |
1789 | } | |
ed661185 TG |
1790 | |
1791 | ovs_notify(reply, info, &ovs_dp_vport_multicast_group); | |
ccb1352e JG |
1792 | |
1793 | exit_unlock: | |
8e4e1713 | 1794 | ovs_unlock(); |
ccb1352e JG |
1795 | exit: |
1796 | return err; | |
1797 | } | |
1798 | ||
1799 | static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info) | |
1800 | { | |
1801 | struct nlattr **a = info->attrs; | |
1802 | struct sk_buff *reply; | |
1803 | struct vport *vport; | |
1804 | int err; | |
1805 | ||
8e4e1713 | 1806 | ovs_lock(); |
46df7b81 | 1807 | vport = lookup_vport(sock_net(skb->sk), info->userhdr, a); |
ccb1352e JG |
1808 | err = PTR_ERR(vport); |
1809 | if (IS_ERR(vport)) | |
1810 | goto exit_unlock; | |
1811 | ||
ccb1352e | 1812 | if (a[OVS_VPORT_ATTR_TYPE] && |
f44f3408 | 1813 | nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) { |
ccb1352e | 1814 | err = -EINVAL; |
f44f3408 JG |
1815 | goto exit_unlock; |
1816 | } | |
ccb1352e | 1817 | |
a9341512 JG |
1818 | reply = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
1819 | if (!reply) { | |
1820 | err = -ENOMEM; | |
1821 | goto exit_unlock; | |
1822 | } | |
1823 | ||
f44f3408 | 1824 | if (a[OVS_VPORT_ATTR_OPTIONS]) { |
ccb1352e | 1825 | err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]); |
f44f3408 JG |
1826 | if (err) |
1827 | goto exit_free; | |
1828 | } | |
a9341512 | 1829 | |
03fbf8b3 | 1830 | if (a[OVS_VPORT_ATTR_UPCALL_PID]) |
15e47304 | 1831 | vport->upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]); |
ccb1352e | 1832 | |
a9341512 JG |
1833 | err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid, |
1834 | info->snd_seq, 0, OVS_VPORT_CMD_NEW); | |
1835 | BUG_ON(err < 0); | |
ccb1352e | 1836 | |
8e4e1713 | 1837 | ovs_unlock(); |
ed661185 | 1838 | ovs_notify(reply, info, &ovs_dp_vport_multicast_group); |
8e4e1713 | 1839 | return 0; |
ccb1352e | 1840 | |
a9341512 JG |
1841 | rtnl_unlock(); |
1842 | return 0; | |
1843 | ||
1844 | exit_free: | |
1845 | kfree_skb(reply); | |
ccb1352e | 1846 | exit_unlock: |
8e4e1713 | 1847 | ovs_unlock(); |
ccb1352e JG |
1848 | return err; |
1849 | } | |
1850 | ||
1851 | static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info) | |
1852 | { | |
1853 | struct nlattr **a = info->attrs; | |
1854 | struct sk_buff *reply; | |
1855 | struct vport *vport; | |
1856 | int err; | |
1857 | ||
8e4e1713 | 1858 | ovs_lock(); |
46df7b81 | 1859 | vport = lookup_vport(sock_net(skb->sk), info->userhdr, a); |
ccb1352e JG |
1860 | err = PTR_ERR(vport); |
1861 | if (IS_ERR(vport)) | |
1862 | goto exit_unlock; | |
1863 | ||
1864 | if (vport->port_no == OVSP_LOCAL) { | |
1865 | err = -EINVAL; | |
1866 | goto exit_unlock; | |
1867 | } | |
1868 | ||
15e47304 | 1869 | reply = ovs_vport_cmd_build_info(vport, info->snd_portid, info->snd_seq, |
ccb1352e JG |
1870 | OVS_VPORT_CMD_DEL); |
1871 | err = PTR_ERR(reply); | |
1872 | if (IS_ERR(reply)) | |
1873 | goto exit_unlock; | |
1874 | ||
734907e8 | 1875 | err = 0; |
ccb1352e JG |
1876 | ovs_dp_detach_port(vport); |
1877 | ||
ed661185 | 1878 | ovs_notify(reply, info, &ovs_dp_vport_multicast_group); |
ccb1352e JG |
1879 | |
1880 | exit_unlock: | |
8e4e1713 | 1881 | ovs_unlock(); |
ccb1352e JG |
1882 | return err; |
1883 | } | |
1884 | ||
1885 | static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info) | |
1886 | { | |
1887 | struct nlattr **a = info->attrs; | |
1888 | struct ovs_header *ovs_header = info->userhdr; | |
1889 | struct sk_buff *reply; | |
1890 | struct vport *vport; | |
1891 | int err; | |
1892 | ||
1893 | rcu_read_lock(); | |
46df7b81 | 1894 | vport = lookup_vport(sock_net(skb->sk), ovs_header, a); |
ccb1352e JG |
1895 | err = PTR_ERR(vport); |
1896 | if (IS_ERR(vport)) | |
1897 | goto exit_unlock; | |
1898 | ||
15e47304 | 1899 | reply = ovs_vport_cmd_build_info(vport, info->snd_portid, info->snd_seq, |
ccb1352e JG |
1900 | OVS_VPORT_CMD_NEW); |
1901 | err = PTR_ERR(reply); | |
1902 | if (IS_ERR(reply)) | |
1903 | goto exit_unlock; | |
1904 | ||
1905 | rcu_read_unlock(); | |
1906 | ||
1907 | return genlmsg_reply(reply, info); | |
1908 | ||
1909 | exit_unlock: | |
1910 | rcu_read_unlock(); | |
1911 | return err; | |
1912 | } | |
1913 | ||
1914 | static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb) | |
1915 | { | |
1916 | struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh)); | |
1917 | struct datapath *dp; | |
15eac2a7 PS |
1918 | int bucket = cb->args[0], skip = cb->args[1]; |
1919 | int i, j = 0; | |
ccb1352e | 1920 | |
46df7b81 | 1921 | dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex); |
ccb1352e JG |
1922 | if (!dp) |
1923 | return -ENODEV; | |
1924 | ||
1925 | rcu_read_lock(); | |
15eac2a7 | 1926 | for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) { |
ccb1352e | 1927 | struct vport *vport; |
15eac2a7 PS |
1928 | |
1929 | j = 0; | |
b67bfe0d | 1930 | hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) { |
15eac2a7 PS |
1931 | if (j >= skip && |
1932 | ovs_vport_cmd_fill_info(vport, skb, | |
15e47304 | 1933 | NETLINK_CB(cb->skb).portid, |
15eac2a7 PS |
1934 | cb->nlh->nlmsg_seq, |
1935 | NLM_F_MULTI, | |
1936 | OVS_VPORT_CMD_NEW) < 0) | |
1937 | goto out; | |
1938 | ||
1939 | j++; | |
1940 | } | |
1941 | skip = 0; | |
ccb1352e | 1942 | } |
15eac2a7 | 1943 | out: |
ccb1352e JG |
1944 | rcu_read_unlock(); |
1945 | ||
15eac2a7 PS |
1946 | cb->args[0] = i; |
1947 | cb->args[1] = j; | |
ccb1352e | 1948 | |
15eac2a7 | 1949 | return skb->len; |
ccb1352e JG |
1950 | } |
1951 | ||
ccb1352e JG |
1952 | static struct genl_ops dp_vport_genl_ops[] = { |
1953 | { .cmd = OVS_VPORT_CMD_NEW, | |
1954 | .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ | |
1955 | .policy = vport_policy, | |
1956 | .doit = ovs_vport_cmd_new | |
1957 | }, | |
1958 | { .cmd = OVS_VPORT_CMD_DEL, | |
1959 | .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ | |
1960 | .policy = vport_policy, | |
1961 | .doit = ovs_vport_cmd_del | |
1962 | }, | |
1963 | { .cmd = OVS_VPORT_CMD_GET, | |
1964 | .flags = 0, /* OK for unprivileged users. */ | |
1965 | .policy = vport_policy, | |
1966 | .doit = ovs_vport_cmd_get, | |
1967 | .dumpit = ovs_vport_cmd_dump | |
1968 | }, | |
1969 | { .cmd = OVS_VPORT_CMD_SET, | |
1970 | .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */ | |
1971 | .policy = vport_policy, | |
1972 | .doit = ovs_vport_cmd_set, | |
1973 | }, | |
1974 | }; | |
1975 | ||
1976 | struct genl_family_and_ops { | |
1977 | struct genl_family *family; | |
1978 | struct genl_ops *ops; | |
1979 | int n_ops; | |
1980 | struct genl_multicast_group *group; | |
1981 | }; | |
1982 | ||
1983 | static const struct genl_family_and_ops dp_genl_families[] = { | |
1984 | { &dp_datapath_genl_family, | |
1985 | dp_datapath_genl_ops, ARRAY_SIZE(dp_datapath_genl_ops), | |
1986 | &ovs_dp_datapath_multicast_group }, | |
1987 | { &dp_vport_genl_family, | |
1988 | dp_vport_genl_ops, ARRAY_SIZE(dp_vport_genl_ops), | |
1989 | &ovs_dp_vport_multicast_group }, | |
1990 | { &dp_flow_genl_family, | |
1991 | dp_flow_genl_ops, ARRAY_SIZE(dp_flow_genl_ops), | |
1992 | &ovs_dp_flow_multicast_group }, | |
1993 | { &dp_packet_genl_family, | |
1994 | dp_packet_genl_ops, ARRAY_SIZE(dp_packet_genl_ops), | |
1995 | NULL }, | |
1996 | }; | |
1997 | ||
1998 | static void dp_unregister_genl(int n_families) | |
1999 | { | |
2000 | int i; | |
2001 | ||
2002 | for (i = 0; i < n_families; i++) | |
2003 | genl_unregister_family(dp_genl_families[i].family); | |
2004 | } | |
2005 | ||
2006 | static int dp_register_genl(void) | |
2007 | { | |
2008 | int n_registered; | |
2009 | int err; | |
2010 | int i; | |
2011 | ||
2012 | n_registered = 0; | |
2013 | for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) { | |
2014 | const struct genl_family_and_ops *f = &dp_genl_families[i]; | |
2015 | ||
2016 | err = genl_register_family_with_ops(f->family, f->ops, | |
2017 | f->n_ops); | |
2018 | if (err) | |
2019 | goto error; | |
2020 | n_registered++; | |
2021 | ||
2022 | if (f->group) { | |
2023 | err = genl_register_mc_group(f->family, f->group); | |
2024 | if (err) | |
2025 | goto error; | |
2026 | } | |
2027 | } | |
2028 | ||
2029 | return 0; | |
2030 | ||
2031 | error: | |
2032 | dp_unregister_genl(n_registered); | |
2033 | return err; | |
2034 | } | |
2035 | ||
46df7b81 PS |
2036 | static void rehash_flow_table(struct work_struct *work) |
2037 | { | |
2038 | struct datapath *dp; | |
2039 | struct net *net; | |
2040 | ||
8e4e1713 | 2041 | ovs_lock(); |
46df7b81 PS |
2042 | rtnl_lock(); |
2043 | for_each_net(net) { | |
2044 | struct ovs_net *ovs_net = net_generic(net, ovs_net_id); | |
2045 | ||
2046 | list_for_each_entry(dp, &ovs_net->dps, list_node) { | |
8e4e1713 | 2047 | struct flow_table *old_table = ovsl_dereference(dp->table); |
46df7b81 PS |
2048 | struct flow_table *new_table; |
2049 | ||
2050 | new_table = ovs_flow_tbl_rehash(old_table); | |
2051 | if (!IS_ERR(new_table)) { | |
2052 | rcu_assign_pointer(dp->table, new_table); | |
2053 | ovs_flow_tbl_deferred_destroy(old_table); | |
2054 | } | |
2055 | } | |
2056 | } | |
2057 | rtnl_unlock(); | |
8e4e1713 | 2058 | ovs_unlock(); |
46df7b81 PS |
2059 | schedule_delayed_work(&rehash_flow_wq, REHASH_FLOW_INTERVAL); |
2060 | } | |
2061 | ||
2062 | static int __net_init ovs_init_net(struct net *net) | |
2063 | { | |
2064 | struct ovs_net *ovs_net = net_generic(net, ovs_net_id); | |
2065 | ||
2066 | INIT_LIST_HEAD(&ovs_net->dps); | |
8e4e1713 | 2067 | INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq); |
46df7b81 PS |
2068 | return 0; |
2069 | } | |
2070 | ||
2071 | static void __net_exit ovs_exit_net(struct net *net) | |
2072 | { | |
46df7b81 | 2073 | struct datapath *dp, *dp_next; |
8e4e1713 | 2074 | struct ovs_net *ovs_net = net_generic(net, ovs_net_id); |
46df7b81 | 2075 | |
8e4e1713 | 2076 | ovs_lock(); |
46df7b81 PS |
2077 | list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node) |
2078 | __dp_destroy(dp); | |
8e4e1713 PS |
2079 | ovs_unlock(); |
2080 | ||
2081 | cancel_work_sync(&ovs_net->dp_notify_work); | |
46df7b81 PS |
2082 | } |
2083 | ||
2084 | static struct pernet_operations ovs_net_ops = { | |
2085 | .init = ovs_init_net, | |
2086 | .exit = ovs_exit_net, | |
2087 | .id = &ovs_net_id, | |
2088 | .size = sizeof(struct ovs_net), | |
2089 | }; | |
2090 | ||
ccb1352e JG |
2091 | static int __init dp_init(void) |
2092 | { | |
ccb1352e JG |
2093 | int err; |
2094 | ||
3523b29b | 2095 | BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb)); |
ccb1352e JG |
2096 | |
2097 | pr_info("Open vSwitch switching datapath\n"); | |
2098 | ||
2099 | err = ovs_flow_init(); | |
2100 | if (err) | |
2101 | goto error; | |
2102 | ||
2103 | err = ovs_vport_init(); | |
2104 | if (err) | |
2105 | goto error_flow_exit; | |
2106 | ||
46df7b81 | 2107 | err = register_pernet_device(&ovs_net_ops); |
ccb1352e JG |
2108 | if (err) |
2109 | goto error_vport_exit; | |
2110 | ||
46df7b81 PS |
2111 | err = register_netdevice_notifier(&ovs_dp_device_notifier); |
2112 | if (err) | |
2113 | goto error_netns_exit; | |
2114 | ||
ccb1352e JG |
2115 | err = dp_register_genl(); |
2116 | if (err < 0) | |
2117 | goto error_unreg_notifier; | |
2118 | ||
2119 | schedule_delayed_work(&rehash_flow_wq, REHASH_FLOW_INTERVAL); | |
2120 | ||
2121 | return 0; | |
2122 | ||
2123 | error_unreg_notifier: | |
2124 | unregister_netdevice_notifier(&ovs_dp_device_notifier); | |
46df7b81 PS |
2125 | error_netns_exit: |
2126 | unregister_pernet_device(&ovs_net_ops); | |
ccb1352e JG |
2127 | error_vport_exit: |
2128 | ovs_vport_exit(); | |
2129 | error_flow_exit: | |
2130 | ovs_flow_exit(); | |
2131 | error: | |
2132 | return err; | |
2133 | } | |
2134 | ||
2135 | static void dp_cleanup(void) | |
2136 | { | |
2137 | cancel_delayed_work_sync(&rehash_flow_wq); | |
ccb1352e JG |
2138 | dp_unregister_genl(ARRAY_SIZE(dp_genl_families)); |
2139 | unregister_netdevice_notifier(&ovs_dp_device_notifier); | |
46df7b81 PS |
2140 | unregister_pernet_device(&ovs_net_ops); |
2141 | rcu_barrier(); | |
ccb1352e JG |
2142 | ovs_vport_exit(); |
2143 | ovs_flow_exit(); | |
2144 | } | |
2145 | ||
2146 | module_init(dp_init); | |
2147 | module_exit(dp_cleanup); | |
2148 | ||
2149 | MODULE_DESCRIPTION("Open vSwitch switching datapath"); | |
2150 | MODULE_LICENSE("GPL"); |