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