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