]> git.proxmox.com Git - mirror_ovs.git/blame - datapath/datapath.c
datapath: Change u64_stats_* to use _irq instead of _bh().
[mirror_ovs.git] / datapath / datapath.c
CommitLineData
064af421 1/*
a5b8d49b 2 * Copyright (c) 2007-2014 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>
cd2a59e9
PS
51#include <linux/genetlink.h>
52#include <net/genetlink.h>
36956a7d 53#include <net/genetlink.h>
2a4999f3
PS
54#include <net/net_namespace.h>
55#include <net/netns/generic.h>
064af421 56
064af421 57#include "datapath.h"
064af421 58#include "flow.h"
d103f479 59#include "flow_table.h"
a097c0b2 60#include "flow_netlink.h"
303708cc 61#include "vlan.h"
f2459fe7 62#include "vport-internal_dev.h"
d5de5b0d 63#include "vport-netdev.h"
064af421 64
2a4999f3
PS
65int ovs_net_id __read_mostly;
66
cb25142c
PS
67static struct genl_family dp_packet_genl_family;
68static struct genl_family dp_flow_genl_family;
69static struct genl_family dp_datapath_genl_family;
70
71static struct genl_multicast_group ovs_dp_flow_multicast_group = {
72 .name = OVS_FLOW_MCGROUP
73};
74
75static struct genl_multicast_group ovs_dp_datapath_multicast_group = {
76 .name = OVS_DATAPATH_MCGROUP
77};
78
79struct genl_multicast_group ovs_dp_vport_multicast_group = {
80 .name = OVS_VPORT_MCGROUP
81};
82
afad3556
JR
83/* Check if need to build a reply message.
84 * OVS userspace sets the NLM_F_ECHO flag if it needs the reply. */
85static bool ovs_must_notify(struct genl_info *info,
86 const struct genl_multicast_group *grp)
87{
88 return info->nlhdr->nlmsg_flags & NLM_F_ECHO ||
cb25142c 89 netlink_has_listeners(genl_info_net(info)->genl_sock, GROUP_ID(grp));
afad3556
JR
90}
91
cb25142c
PS
92static void ovs_notify(struct genl_family *family, struct genl_multicast_group *grp,
93 struct sk_buff *skb, struct genl_info *info)
e297c6b7 94{
cb25142c
PS
95 genl_notify(family, skb, genl_info_net(info),
96 info->snd_portid, GROUP_ID(grp), info->nlhdr, GFP_KERNEL);
e297c6b7
TG
97}
98
ed099e92
BP
99/**
100 * DOC: Locking:
064af421 101 *
cd2a59e9
PS
102 * All writes e.g. Writes to device state (add/remove datapath, port, set
103 * operations on vports, etc.), Writes to other state (flow table
104 * modifications, set miscellaneous datapath parameters, etc.) are protected
105 * by ovs_lock.
ed099e92
BP
106 *
107 * Reads are protected by RCU.
108 *
109 * There are a few special cases (mostly stats) that have their own
110 * synchronization but they nest under all of above and don't interact with
111 * each other.
cd2a59e9
PS
112 *
113 * The RTNL lock nests inside ovs_mutex.
064af421 114 */
ed099e92 115
cd2a59e9
PS
116static DEFINE_MUTEX(ovs_mutex);
117
118void ovs_lock(void)
119{
120 mutex_lock(&ovs_mutex);
121}
122
123void ovs_unlock(void)
124{
125 mutex_unlock(&ovs_mutex);
126}
127
128#ifdef CONFIG_LOCKDEP
129int lockdep_ovsl_is_held(void)
130{
131 if (debug_locks)
132 return lockdep_is_held(&ovs_mutex);
133 else
134 return 1;
135}
136#endif
137
5ae440c3 138static int queue_gso_packets(struct datapath *dp, struct sk_buff *,
7257b535 139 const struct dp_upcall_info *);
5ae440c3 140static int queue_userspace_packet(struct datapath *dp, struct sk_buff *,
7257b535 141 const struct dp_upcall_info *);
064af421 142
cd2a59e9 143/* Must be called with rcu_read_lock or ovs_mutex. */
2a4999f3 144static struct datapath *get_dp(struct net *net, int dp_ifindex)
064af421 145{
254f2dc8
BP
146 struct datapath *dp = NULL;
147 struct net_device *dev;
ed099e92 148
254f2dc8 149 rcu_read_lock();
2a4999f3 150 dev = dev_get_by_index_rcu(net, dp_ifindex);
254f2dc8 151 if (dev) {
850b6b3b 152 struct vport *vport = ovs_internal_dev_get_vport(dev);
254f2dc8
BP
153 if (vport)
154 dp = vport->dp;
155 }
156 rcu_read_unlock();
157
158 return dp;
064af421 159}
064af421 160
cd2a59e9 161/* Must be called with rcu_read_lock or ovs_mutex. */
850b6b3b 162const char *ovs_dp_name(const struct datapath *dp)
f2459fe7 163{
cd2a59e9 164 struct vport *vport = ovs_vport_ovsl_rcu(dp, OVSP_LOCAL);
16b82e84 165 return vport->ops->get_name(vport);
f2459fe7
JG
166}
167
99769a40
JG
168static int get_dpifindex(struct datapath *dp)
169{
170 struct vport *local;
171 int ifindex;
172
173 rcu_read_lock();
174
95b1d73a 175 local = ovs_vport_rcu(dp, OVSP_LOCAL);
99769a40 176 if (local)
d5de5b0d 177 ifindex = netdev_vport_priv(local)->dev->ifindex;
99769a40
JG
178 else
179 ifindex = 0;
180
181 rcu_read_unlock();
182
183 return ifindex;
184}
185
46c6a11d
JG
186static void destroy_dp_rcu(struct rcu_head *rcu)
187{
188 struct datapath *dp = container_of(rcu, struct datapath, rcu);
46c6a11d 189
e379e4d1 190 ovs_flow_tbl_destroy(&dp->table);
46c6a11d 191 free_percpu(dp->stats_percpu);
2a4999f3 192 release_net(ovs_dp_get_net(dp));
95b1d73a 193 kfree(dp->ports);
5ca1ba48 194 kfree(dp);
46c6a11d
JG
195}
196
95b1d73a
PS
197static struct hlist_head *vport_hash_bucket(const struct datapath *dp,
198 u16 port_no)
199{
200 return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)];
201}
202
aa917006 203/* Called with ovs_mutex or RCU read lock. */
95b1d73a
PS
204struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no)
205{
206 struct vport *vport;
95b1d73a
PS
207 struct hlist_head *head;
208
209 head = vport_hash_bucket(dp, port_no);
f8dfbcb7 210 hlist_for_each_entry_rcu(vport, head, dp_hash_node) {
95b1d73a
PS
211 if (vport->port_no == port_no)
212 return vport;
213 }
214 return NULL;
215}
216
cd2a59e9 217/* Called with ovs_mutex. */
c19e6535 218static struct vport *new_vport(const struct vport_parms *parms)
064af421 219{
f2459fe7 220 struct vport *vport;
f2459fe7 221
850b6b3b 222 vport = ovs_vport_add(parms);
c19e6535
BP
223 if (!IS_ERR(vport)) {
224 struct datapath *dp = parms->dp;
95b1d73a 225 struct hlist_head *head = vport_hash_bucket(dp, vport->port_no);
064af421 226
95b1d73a 227 hlist_add_head_rcu(&vport->dp_hash_node, head);
c19e6535 228 }
c19e6535 229 return vport;
064af421
BP
230}
231
850b6b3b 232void ovs_dp_detach_port(struct vport *p)
064af421 233{
cd2a59e9 234 ASSERT_OVSL();
064af421 235
064af421 236 /* First drop references to device. */
95b1d73a 237 hlist_del_rcu(&p->dp_hash_node);
f2459fe7 238
7237e4f4 239 /* Then destroy it. */
850b6b3b 240 ovs_vport_del(p);
064af421
BP
241}
242
a6059080 243void ovs_dp_process_packet_with_key(struct sk_buff *skb,
ca93abce
AZ
244 struct sw_flow_key *pkt_key,
245 bool recirc)
064af421 246{
a6059080 247 const struct vport *p = OVS_CB(skb)->input_vport;
064af421 248 struct datapath *dp = p->dp;
3544358a 249 struct sw_flow *flow;
064af421 250 struct dp_stats_percpu *stats;
e9141eec 251 u64 *stats_counter;
4fa72a95 252 u32 n_mask_hit;
064af421 253
70dbc259 254 stats = this_cpu_ptr(dp->stats_percpu);
a063b0df 255
52a23d92 256 /* Look up flow. */
e2f3178f 257 flow = ovs_flow_tbl_lookup_stats(&dp->table, pkt_key, skb_get_hash(skb),
5604935e 258 &n_mask_hit);
52a23d92
JG
259 if (unlikely(!flow)) {
260 struct dp_upcall_info upcall;
261
262 upcall.cmd = OVS_PACKET_CMD_MISS;
a6059080 263 upcall.key = pkt_key;
52a23d92 264 upcall.userdata = NULL;
beb1c69a 265 upcall.portid = ovs_vport_find_upcall_portid(p, skb);
52a23d92
JG
266 ovs_dp_upcall(dp, skb, &upcall);
267 consume_skb(skb);
268 stats_counter = &stats->n_missed;
269 goto out;
270 }
271
a6059080 272 OVS_CB(skb)->pkt_key = pkt_key;
52a23d92
JG
273 OVS_CB(skb)->flow = flow;
274
a6059080 275 ovs_flow_stats_update(OVS_CB(skb)->flow, pkt_key->tp.flags, skb);
ca93abce 276 ovs_execute_actions(dp, skb, recirc);
b0b906cc 277 stats_counter = &stats->n_hit;
55574bb0 278
8819fac7 279out:
55574bb0 280 /* Update datapath statistics. */
821cb9fa 281 u64_stats_update_begin(&stats->sync);
e9141eec 282 (*stats_counter)++;
4fa72a95 283 stats->n_mask_hit += n_mask_hit;
821cb9fa 284 u64_stats_update_end(&stats->sync);
064af421
BP
285}
286
a6059080 287/* Must be called with rcu_read_lock. */
c450371e 288void ovs_dp_process_received_packet(struct sk_buff *skb)
a6059080
AZ
289{
290 int error;
291 struct sw_flow_key key;
292
a6059080 293 /* Extract flow from 'skb' into 'key'. */
c450371e 294 error = ovs_flow_extract(skb, OVS_CB(skb)->input_vport->port_no, &key);
a6059080
AZ
295 if (unlikely(error)) {
296 kfree_skb(skb);
297 return;
298 }
299
ca93abce 300 ovs_dp_process_packet_with_key(skb, &key, false);
a6059080
AZ
301}
302
850b6b3b
JG
303int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
304 const struct dp_upcall_info *upcall_info)
aa5a8fdc
JG
305{
306 struct dp_stats_percpu *stats;
307 int err;
308
28aea917 309 if (upcall_info->portid == 0) {
b063d9f0 310 err = -ENOTCONN;
b063d9f0
JG
311 goto err;
312 }
313
7257b535 314 if (!skb_is_gso(skb))
5ae440c3 315 err = queue_userspace_packet(dp, skb, upcall_info);
7257b535 316 else
5ae440c3 317 err = queue_gso_packets(dp, skb, upcall_info);
d76195db
JG
318 if (err)
319 goto err;
320
321 return 0;
aa5a8fdc 322
aa5a8fdc 323err:
70dbc259 324 stats = this_cpu_ptr(dp->stats_percpu);
aa5a8fdc 325
821cb9fa 326 u64_stats_update_begin(&stats->sync);
aa5a8fdc 327 stats->n_lost++;
821cb9fa 328 u64_stats_update_end(&stats->sync);
aa5a8fdc 329
aa5a8fdc 330 return err;
982b8810
BP
331}
332
5ae440c3 333static int queue_gso_packets(struct datapath *dp, struct sk_buff *skb,
7257b535 334 const struct dp_upcall_info *upcall_info)
cb5087ca 335{
d4cba1f8 336 unsigned short gso_type = skb_shinfo(skb)->gso_type;
7257b535
BP
337 struct dp_upcall_info later_info;
338 struct sw_flow_key later_key;
339 struct sk_buff *segs, *nskb;
340 int err;
cb5087ca 341
1d04cd4e 342 segs = __skb_gso_segment(skb, NETIF_F_SG, false);
79089764
PS
343 if (IS_ERR(segs))
344 return PTR_ERR(segs);
99769a40 345
7257b535
BP
346 /* Queue all of the segments. */
347 skb = segs;
cb5087ca 348 do {
5ae440c3 349 err = queue_userspace_packet(dp, skb, upcall_info);
982b8810 350 if (err)
7257b535 351 break;
856081f6 352
d4cba1f8 353 if (skb == segs && gso_type & SKB_GSO_UDP) {
e1cf87ff
JG
354 /* The initial flow key extracted by ovs_flow_extract()
355 * in this case is for a first fragment, so we need to
7257b535
BP
356 * properly mark later fragments.
357 */
358 later_key = *upcall_info->key;
9e44d715 359 later_key.ip.frag = OVS_FRAG_TYPE_LATER;
7257b535
BP
360
361 later_info = *upcall_info;
362 later_info.key = &later_key;
363 upcall_info = &later_info;
364 }
36ce148c 365 } while ((skb = skb->next));
cb5087ca 366
7257b535
BP
367 /* Free all of the segments. */
368 skb = segs;
369 do {
370 nskb = skb->next;
371 if (err)
372 kfree_skb(skb);
373 else
374 consume_skb(skb);
375 } while ((skb = nskb));
376 return err;
377}
378
0afa2373
TG
379static size_t key_attr_size(void)
380{
6d8af7ab
AZ
381 /* Whenever adding new OVS_KEY_ FIELDS, we should consider
382 * updating this function. */
ccf43786 383 BUILD_BUG_ON(OVS_KEY_ATTR_TUNNEL_INFO != 22);
6d8af7ab 384
0afa2373
TG
385 return nla_total_size(4) /* OVS_KEY_ATTR_PRIORITY */
386 + nla_total_size(0) /* OVS_KEY_ATTR_TUNNEL */
387 + nla_total_size(8) /* OVS_TUNNEL_KEY_ATTR_ID */
388 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_SRC */
389 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_DST */
390 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TOS */
391 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TTL */
392 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT */
393 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_CSUM */
94872594 394 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_OAM */
c1fc1411 395 + nla_total_size(256) /* OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS */
0afa2373
TG
396 + nla_total_size(4) /* OVS_KEY_ATTR_IN_PORT */
397 + nla_total_size(4) /* OVS_KEY_ATTR_SKB_MARK */
6d8af7ab
AZ
398 + nla_total_size(4) /* OVS_KEY_ATTR_DP_HASH */
399 + nla_total_size(4) /* OVS_KEY_ATTR_RECIRC_ID */
0afa2373
TG
400 + nla_total_size(12) /* OVS_KEY_ATTR_ETHERNET */
401 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
402 + nla_total_size(4) /* OVS_KEY_ATTR_8021Q */
403 + nla_total_size(0) /* OVS_KEY_ATTR_ENCAP */
404 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
405 + nla_total_size(40) /* OVS_KEY_ATTR_IPV6 */
406 + nla_total_size(2) /* OVS_KEY_ATTR_ICMPV6 */
407 + nla_total_size(28); /* OVS_KEY_ATTR_ND */
408}
409
533bea51
TG
410static size_t upcall_msg_size(const struct nlattr *userdata,
411 unsigned int hdrlen)
0afa2373
TG
412{
413 size_t size = NLMSG_ALIGN(sizeof(struct ovs_header))
533bea51 414 + nla_total_size(hdrlen) /* OVS_PACKET_ATTR_PACKET */
0afa2373
TG
415 + nla_total_size(key_attr_size()); /* OVS_PACKET_ATTR_KEY */
416
417 /* OVS_PACKET_ATTR_USERDATA */
418 if (userdata)
419 size += NLA_ALIGN(userdata->nla_len);
420
421 return size;
422}
423
5ae440c3 424static int queue_userspace_packet(struct datapath *dp, struct sk_buff *skb,
7257b535
BP
425 const struct dp_upcall_info *upcall_info)
426{
427 struct ovs_header *upcall;
6161d3fd 428 struct sk_buff *nskb = NULL;
7257b535
BP
429 struct sk_buff *user_skb; /* to be queued to userspace */
430 struct nlattr *nla;
68eadcf0
TG
431 struct genl_info info = {
432#if LINUX_VERSION_CODE >= KERNEL_VERSION(3,14,0)
5ae440c3 433 .dst_sk = ovs_dp_get_net(dp)->genl_sock,
68eadcf0
TG
434#endif
435 .snd_portid = upcall_info->portid,
436 };
978188b2 437 size_t len;
533bea51 438 unsigned int hlen;
5ae440c3
TG
439 int err, dp_ifindex;
440
441 dp_ifindex = get_dpifindex(dp);
442 if (!dp_ifindex)
443 return -ENODEV;
7257b535 444
6161d3fd
JG
445 if (vlan_tx_tag_present(skb)) {
446 nskb = skb_clone(skb, GFP_ATOMIC);
447 if (!nskb)
448 return -ENOMEM;
07ac71ea
PS
449
450 nskb = __vlan_put_tag(nskb, nskb->vlan_proto, vlan_tx_tag_get(nskb));
451 if (!nskb)
452 return -ENOMEM;
453
454 vlan_set_tci(nskb, 0);
7257b535 455
6161d3fd
JG
456 skb = nskb;
457 }
458
459 if (nla_attr_size(skb->len) > USHRT_MAX) {
460 err = -EFBIG;
461 goto out;
462 }
7257b535 463
533bea51
TG
464 /* Complete checksum if needed */
465 if (skb->ip_summed == CHECKSUM_PARTIAL &&
466 (err = skb_checksum_help(skb)))
467 goto out;
468
469 /* Older versions of OVS user space enforce alignment of the last
470 * Netlink attribute to NLA_ALIGNTO which would require extensive
471 * padding logic. Only perform zerocopy if padding is not required.
472 */
473 if (dp->user_features & OVS_DP_F_UNALIGNED)
474 hlen = skb_zerocopy_headlen(skb);
475 else
476 hlen = skb->len;
477
478 len = upcall_msg_size(upcall_info->userdata, hlen);
68eadcf0 479 user_skb = genlmsg_new_unicast(len, &info, GFP_ATOMIC);
6161d3fd
JG
480 if (!user_skb) {
481 err = -ENOMEM;
482 goto out;
483 }
7257b535
BP
484
485 upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
486 0, upcall_info->cmd);
487 upcall->dp_ifindex = dp_ifindex;
488
489 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
c1fc1411
JG
490 err = ovs_nla_put_flow(dp, upcall_info->key,
491 upcall_info->key, user_skb);
9a621f82 492 BUG_ON(err);
7257b535
BP
493 nla_nest_end(user_skb, nla);
494
495 if (upcall_info->userdata)
e995e3df 496 __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA,
462a988b 497 nla_len(upcall_info->userdata),
e995e3df 498 nla_data(upcall_info->userdata));
7257b535 499
533bea51
TG
500 /* Only reserve room for attribute header, packet data is added
501 * in skb_zerocopy() */
502 if (!(nla = nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, 0))) {
503 err = -ENOBUFS;
504 goto out;
505 }
506 nla->nla_len = nla_attr_size(skb->len);
bed53bd1 507
2c272bd9
ZK
508 err = skb_zerocopy(user_skb, skb, skb->len, hlen);
509 if (err)
510 goto out;
7257b535 511
ef507cec 512 /* Pad OVS_PACKET_ATTR_PACKET if linear copy was performed */
978188b2
JG
513 if (!(dp->user_features & OVS_DP_F_UNALIGNED)) {
514 size_t plen = NLA_ALIGN(user_skb->len) - user_skb->len;
515
516 if (plen > 0)
517 memset(skb_put(user_skb, plen), 0, plen);
518 }
ef507cec 519
533bea51 520 ((struct nlmsghdr *) user_skb->data)->nlmsg_len = user_skb->len;
6161d3fd 521
533bea51 522 err = genlmsg_unicast(ovs_dp_get_net(dp), user_skb, upcall_info->portid);
6161d3fd 523out:
2c272bd9
ZK
524 if (err)
525 skb_tx_error(skb);
6161d3fd
JG
526 kfree_skb(nskb);
527 return err;
cb5087ca
BP
528}
529
df2c07f4 530static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
064af421 531{
df2c07f4 532 struct ovs_header *ovs_header = info->userhdr;
982b8810 533 struct nlattr **a = info->attrs;
e0e57990 534 struct sw_flow_actions *acts;
982b8810 535 struct sk_buff *packet;
e0e57990 536 struct sw_flow *flow;
f7cd0081 537 struct datapath *dp;
d6569377 538 struct ethhdr *eth;
a6059080 539 struct vport *input_vport;
3f19d399 540 int len;
d6569377 541 int err;
064af421 542
f7cd0081 543 err = -EINVAL;
df2c07f4 544 if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
7c3072cc 545 !a[OVS_PACKET_ATTR_ACTIONS])
e5cad958 546 goto err;
064af421 547
df2c07f4 548 len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
3f19d399 549 packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
f7cd0081
BP
550 err = -ENOMEM;
551 if (!packet)
e5cad958 552 goto err;
3f19d399
BP
553 skb_reserve(packet, NET_IP_ALIGN);
554
bf3d6fce 555 nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
8d5ebd83 556
f7cd0081
BP
557 skb_reset_mac_header(packet);
558 eth = eth_hdr(packet);
064af421 559
d6569377
BP
560 /* Normally, setting the skb 'protocol' field would be handled by a
561 * call to eth_type_trans(), but it assumes there's a sending
562 * device, which we may not have. */
7cd46155 563 if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN)
f7cd0081 564 packet->protocol = eth->h_proto;
d6569377 565 else
f7cd0081 566 packet->protocol = htons(ETH_P_802_2);
d3c54451 567
e0e57990 568 /* Build an sw_flow for sending this packet. */
df65fec1 569 flow = ovs_flow_alloc();
e0e57990
BP
570 err = PTR_ERR(flow);
571 if (IS_ERR(flow))
e5cad958 572 goto err_kfree_skb;
064af421 573
a1c564be 574 err = ovs_flow_extract(packet, -1, &flow->key);
e0e57990 575 if (err)
9321954a 576 goto err_flow_free;
e0e57990 577
a097c0b2 578 err = ovs_nla_get_flow_metadata(flow, a[OVS_PACKET_ATTR_KEY]);
80e5eed9 579 if (err)
9321954a 580 goto err_flow_free;
a097c0b2 581 acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_PACKET_ATTR_ACTIONS]));
e0e57990
BP
582 err = PTR_ERR(acts);
583 if (IS_ERR(acts))
9321954a 584 goto err_flow_free;
9b405f1a 585
a097c0b2 586 err = ovs_nla_copy_actions(a[OVS_PACKET_ATTR_ACTIONS],
ccf43786 587 &flow->key, &acts);
e0e57990 588 rcu_assign_pointer(flow->sf_acts, acts);
9b405f1a
PS
589 if (err)
590 goto err_flow_free;
e0e57990
BP
591
592 OVS_CB(packet)->flow = flow;
d1d71a36 593 OVS_CB(packet)->pkt_key = &flow->key;
abff858b 594 packet->priority = flow->key.phy.priority;
3025a772 595 packet->mark = flow->key.phy.skb_mark;
e0e57990 596
d6569377 597 rcu_read_lock();
2a4999f3 598 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
f7cd0081 599 err = -ENODEV;
e5cad958
BP
600 if (!dp)
601 goto err_unlock;
cc4015df 602
a6059080
AZ
603 input_vport = ovs_vport_rcu(dp, flow->key.phy.in_port);
604 if (!input_vport)
605 input_vport = ovs_vport_rcu(dp, OVSP_LOCAL);
606
607 if (!input_vport)
608 goto err_unlock;
609
610 OVS_CB(packet)->input_vport = input_vport;
611
e9141eec 612 local_bh_disable();
ca93abce 613 err = ovs_execute_actions(dp, packet, false);
e9141eec 614 local_bh_enable();
d6569377 615 rcu_read_unlock();
e0e57990 616
a1c564be 617 ovs_flow_free(flow, false);
e5cad958 618 return err;
064af421 619
e5cad958
BP
620err_unlock:
621 rcu_read_unlock();
9321954a 622err_flow_free:
a1c564be 623 ovs_flow_free(flow, false);
e5cad958
BP
624err_kfree_skb:
625 kfree_skb(packet);
626err:
d6569377 627 return err;
064af421
BP
628}
629
df2c07f4 630static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
7c3072cc 631 [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
df2c07f4
JP
632 [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
633 [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
982b8810
BP
634};
635
636static struct genl_ops dp_packet_genl_ops[] = {
df2c07f4 637 { .cmd = OVS_PACKET_CMD_EXECUTE,
982b8810
BP
638 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
639 .policy = packet_policy,
df2c07f4 640 .doit = ovs_packet_cmd_execute
982b8810
BP
641 }
642};
643
cb25142c
PS
644static struct genl_family dp_packet_genl_family = {
645 .id = GENL_ID_GENERATE,
646 .hdrsize = sizeof(struct ovs_header),
647 .name = OVS_PACKET_FAMILY,
648 .version = OVS_PACKET_VERSION,
649 .maxattr = OVS_PACKET_ATTR_MAX,
650 .netnsok = true,
651 .parallel_ops = true,
652 .ops = dp_packet_genl_ops,
653 .n_ops = ARRAY_SIZE(dp_packet_genl_ops),
654};
655
4fa72a95
AZ
656static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats,
657 struct ovs_dp_megaflow_stats *mega_stats)
064af421 658{
d6569377 659 int i;
f180c2e2 660
4fa72a95
AZ
661 memset(mega_stats, 0, sizeof(*mega_stats));
662
994dc286 663 stats->n_flows = ovs_flow_tbl_count(&dp->table);
4fa72a95 664 mega_stats->n_masks = ovs_flow_tbl_num_masks(&dp->table);
064af421 665
7257b535 666 stats->n_hit = stats->n_missed = stats->n_lost = 0;
4fa72a95 667
d6569377
BP
668 for_each_possible_cpu(i) {
669 const struct dp_stats_percpu *percpu_stats;
670 struct dp_stats_percpu local_stats;
821cb9fa 671 unsigned int start;
44e05eca 672
d6569377 673 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
064af421 674
d6569377 675 do {
09f27436 676 start = u64_stats_fetch_begin_irq(&percpu_stats->sync);
d6569377 677 local_stats = *percpu_stats;
09f27436 678 } while (u64_stats_fetch_retry_irq(&percpu_stats->sync, start));
064af421 679
d6569377
BP
680 stats->n_hit += local_stats.n_hit;
681 stats->n_missed += local_stats.n_missed;
682 stats->n_lost += local_stats.n_lost;
4fa72a95 683 mega_stats->n_mask_hit += local_stats.n_mask_hit;
d6569377
BP
684 }
685}
064af421 686
0afa2373
TG
687static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts)
688{
689 return NLMSG_ALIGN(sizeof(struct ovs_header))
690 + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_KEY */
a1c564be 691 + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_MASK */
0afa2373
TG
692 + nla_total_size(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
693 + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
694 + nla_total_size(8) /* OVS_FLOW_ATTR_USED */
695 + nla_total_size(acts->actions_len); /* OVS_FLOW_ATTR_ACTIONS */
696}
697
aa917006 698/* Called with ovs_mutex or RCU read lock. */
c1fc1411
JG
699static int ovs_flow_cmd_fill_info(struct datapath *dp,
700 const struct sw_flow *flow, int dp_ifindex,
28aea917 701 struct sk_buff *skb, u32 portid,
6455100f 702 u32 seq, u32 flags, u8 cmd)
d6569377 703{
37a1300c 704 const int skb_orig_len = skb->len;
9b405f1a 705 struct nlattr *start;
b0f3a2fe
PS
706 struct ovs_flow_stats stats;
707 __be16 tcp_flags;
708 unsigned long used;
df2c07f4 709 struct ovs_header *ovs_header;
d6569377 710 struct nlattr *nla;
d6569377 711 int err;
064af421 712
28aea917 713 ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family, flags, cmd);
df2c07f4 714 if (!ovs_header)
37a1300c 715 return -EMSGSIZE;
d6569377 716
f71db6b1 717 ovs_header->dp_ifindex = dp_ifindex;
d6569377 718
a1c564be 719 /* Fill flow key. */
df2c07f4 720 nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
d6569377
BP
721 if (!nla)
722 goto nla_put_failure;
a1c564be 723
c1fc1411
JG
724 err = ovs_nla_put_flow(dp, &flow->unmasked_key,
725 &flow->unmasked_key, skb);
d6569377 726 if (err)
37a1300c 727 goto error;
d6569377
BP
728 nla_nest_end(skb, nla);
729
a1c564be
AZ
730 nla = nla_nest_start(skb, OVS_FLOW_ATTR_MASK);
731 if (!nla)
732 goto nla_put_failure;
733
c1fc1411 734 err = ovs_nla_put_flow(dp, &flow->key, &flow->mask->key, skb);
a1c564be
AZ
735 if (err)
736 goto error;
737
738 nla_nest_end(skb, nla);
739
b0f3a2fe 740 ovs_flow_stats_get(flow, &stats, &used, &tcp_flags);
f71db6b1 741
b0f3a2fe
PS
742 if (used &&
743 nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
c3cc8c03 744 goto nla_put_failure;
d6569377 745
b0f3a2fe
PS
746 if (stats.n_packets &&
747 nla_put(skb, OVS_FLOW_ATTR_STATS, sizeof(struct ovs_flow_stats), &stats))
748 goto nla_put_failure;
b0b906cc 749
b0f3a2fe
PS
750 if ((u8)ntohs(tcp_flags) &&
751 nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, (u8)ntohs(tcp_flags)))
c3cc8c03 752 goto nla_put_failure;
d6569377 753
df2c07f4 754 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
30053024
BP
755 * this is the first flow to be dumped into 'skb'. This is unusual for
756 * Netlink but individual action lists can be longer than
757 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
758 * The userspace caller can always fetch the actions separately if it
759 * really wants them. (Most userspace callers in fact don't care.)
760 *
761 * This can only fail for dump operations because the skb is always
762 * properly sized for single flows.
763 */
9b405f1a 764 start = nla_nest_start(skb, OVS_FLOW_ATTR_ACTIONS);
f6f481ee 765 if (start) {
f44ccce1
PS
766 const struct sw_flow_actions *sf_acts;
767
780ec6ae 768 sf_acts = rcu_dereference_ovsl(flow->sf_acts);
a097c0b2
PS
769 err = ovs_nla_put_actions(sf_acts->actions,
770 sf_acts->actions_len, skb);
f71db6b1 771
0a25b039
BP
772 if (!err)
773 nla_nest_end(skb, start);
774 else {
775 if (skb_orig_len)
776 goto error;
777
778 nla_nest_cancel(skb, start);
779 }
7aac03bd
JG
780 } else if (skb_orig_len)
781 goto nla_put_failure;
37a1300c 782
df2c07f4 783 return genlmsg_end(skb, ovs_header);
d6569377
BP
784
785nla_put_failure:
786 err = -EMSGSIZE;
37a1300c 787error:
df2c07f4 788 genlmsg_cancel(skb, ovs_header);
d6569377 789 return err;
44e05eca
BP
790}
791
f71db6b1
JR
792/* May not be called with RCU read lock. */
793static struct sk_buff *ovs_flow_cmd_alloc_info(const struct sw_flow_actions *acts,
afad3556
JR
794 struct genl_info *info,
795 bool always)
44e05eca 796{
afad3556 797 struct sk_buff *skb;
d6569377 798
afad3556
JR
799 if (!always && !ovs_must_notify(info, &ovs_dp_flow_multicast_group))
800 return NULL;
801
f71db6b1 802 skb = genlmsg_new_unicast(ovs_flow_cmd_msg_size(acts), info, GFP_KERNEL);
afad3556
JR
803
804 if (!skb)
805 return ERR_PTR(-ENOMEM);
806
807 return skb;
37a1300c 808}
8d5ebd83 809
f71db6b1 810/* Called with ovs_mutex. */
c1fc1411
JG
811static struct sk_buff *ovs_flow_cmd_build_info(struct datapath *dp,
812 const struct sw_flow *flow,
f71db6b1
JR
813 int dp_ifindex,
814 struct genl_info *info, u8 cmd,
815 bool always)
37a1300c
BP
816{
817 struct sk_buff *skb;
818 int retval;
d6569377 819
f71db6b1
JR
820 skb = ovs_flow_cmd_alloc_info(ovsl_dereference(flow->sf_acts), info,
821 always);
afad3556
JR
822 if (!skb || IS_ERR(skb))
823 return skb;
d6569377 824
c1fc1411 825 retval = ovs_flow_cmd_fill_info(dp, flow, dp_ifindex, skb,
f71db6b1
JR
826 info->snd_portid, info->snd_seq, 0,
827 cmd);
37a1300c 828 BUG_ON(retval < 0);
d6569377 829 return skb;
064af421
BP
830}
831
0c9fd022 832static int ovs_flow_cmd_new(struct sk_buff *skb, struct genl_info *info)
064af421 833{
37a1300c 834 struct nlattr **a = info->attrs;
df2c07f4 835 struct ovs_header *ovs_header = info->userhdr;
6740b721 836 struct sw_flow *flow, *new_flow;
a1c564be 837 struct sw_flow_mask mask;
37a1300c 838 struct sk_buff *reply;
9c52546b 839 struct datapath *dp;
0c9fd022 840 struct sw_flow_actions *acts;
a1c564be 841 struct sw_flow_match match;
bc4a05c6 842 int error;
064af421 843
6740b721 844 /* Must have key and actions. */
37a1300c 845 error = -EINVAL;
df2c07f4 846 if (!a[OVS_FLOW_ATTR_KEY])
37a1300c 847 goto error;
6740b721
JR
848 if (!a[OVS_FLOW_ATTR_ACTIONS])
849 goto error;
a1c564be 850
6740b721
JR
851 /* Most of the time we need to allocate a new flow, do it before
852 * locking. */
853 new_flow = ovs_flow_alloc();
854 if (IS_ERR(new_flow)) {
855 error = PTR_ERR(new_flow);
856 goto error;
857 }
858
859 /* Extract key. */
860 ovs_match_init(&match, &new_flow->unmasked_key, &mask);
df65fec1 861 error = ovs_nla_get_match(&match,
a097c0b2 862 a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
37a1300c 863 if (error)
6740b721 864 goto err_kfree_flow;
064af421 865
6740b721 866 ovs_flow_mask_key(&new_flow->key, &new_flow->unmasked_key, &mask);
9b405f1a 867
6740b721 868 /* Validate actions. */
0c9fd022
JR
869 acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_FLOW_ATTR_ACTIONS]));
870 error = PTR_ERR(acts);
871 if (IS_ERR(acts))
6740b721 872 goto err_kfree_flow;
0c9fd022 873
6740b721 874 error = ovs_nla_copy_actions(a[OVS_FLOW_ATTR_ACTIONS], &new_flow->key,
ccf43786 875 &acts);
0c9fd022
JR
876 if (error) {
877 OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
6740b721
JR
878 goto err_kfree_acts;
879 }
880
881 reply = ovs_flow_cmd_alloc_info(acts, info, false);
882 if (IS_ERR(reply)) {
883 error = PTR_ERR(reply);
884 goto err_kfree_acts;
37a1300c
BP
885 }
886
cd2a59e9 887 ovs_lock();
2a4999f3 888 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
6740b721
JR
889 if (unlikely(!dp)) {
890 error = -ENODEV;
cd2a59e9 891 goto err_unlock_ovs;
6740b721 892 }
a1c564be 893 /* Check if this is a duplicate flow */
6740b721
JR
894 flow = ovs_flow_tbl_lookup(&dp->table, &new_flow->unmasked_key);
895 if (likely(!flow)) {
896 rcu_assign_pointer(new_flow->sf_acts, acts);
d6569377 897
d6569377 898 /* Put flow in bucket. */
6740b721
JR
899 error = ovs_flow_tbl_insert(&dp->table, new_flow, &mask);
900 if (unlikely(error)) {
0585f7a8 901 acts = NULL;
6740b721
JR
902 goto err_unlock_ovs;
903 }
904
905 if (unlikely(reply)) {
c1fc1411 906 error = ovs_flow_cmd_fill_info(dp, new_flow,
6740b721
JR
907 ovs_header->dp_ifindex,
908 reply, info->snd_portid,
909 info->snd_seq, 0,
910 OVS_FLOW_CMD_NEW);
911 BUG_ON(error < 0);
0585f7a8 912 }
6740b721 913 ovs_unlock();
d6569377 914 } else {
0c9fd022
JR
915 struct sw_flow_actions *old_acts;
916
d6569377
BP
917 /* Bail out if we're not allowed to modify an existing flow.
918 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
919 * because Generic Netlink treats the latter as a dump
920 * request. We also accept NLM_F_EXCL in case that bug ever
921 * gets fixed.
922 */
6740b721
JR
923 if (unlikely(info->nlhdr->nlmsg_flags & (NLM_F_CREATE
924 | NLM_F_EXCL))) {
925 error = -EEXIST;
cd2a59e9 926 goto err_unlock_ovs;
6740b721 927 }
b21e5b6a 928 /* The unmasked key has to be the same for flow updates. */
6740b721
JR
929 if (unlikely(!ovs_flow_cmp_unmasked_key(flow, &match))) {
930 error = -EEXIST;
b21e5b6a 931 goto err_unlock_ovs;
6740b721 932 }
0c9fd022
JR
933 /* Update actions. */
934 old_acts = ovsl_dereference(flow->sf_acts);
935 rcu_assign_pointer(flow->sf_acts, acts);
0c9fd022 936
6740b721 937 if (unlikely(reply)) {
c1fc1411 938 error = ovs_flow_cmd_fill_info(dp, flow,
6740b721
JR
939 ovs_header->dp_ifindex,
940 reply, info->snd_portid,
941 info->snd_seq, 0,
942 OVS_FLOW_CMD_NEW);
943 BUG_ON(error < 0);
944 }
945 ovs_unlock();
0c9fd022 946
6740b721
JR
947 ovs_nla_free_flow_actions(old_acts);
948 ovs_flow_free(new_flow, false);
0c9fd022 949 }
6740b721
JR
950
951 if (reply)
cb25142c 952 ovs_notify(&dp_flow_genl_family, &ovs_dp_flow_multicast_group, reply, info);
0c9fd022
JR
953 return 0;
954
0c9fd022
JR
955err_unlock_ovs:
956 ovs_unlock();
6740b721
JR
957 kfree_skb(reply);
958err_kfree_acts:
0c9fd022 959 kfree(acts);
6740b721
JR
960err_kfree_flow:
961 ovs_flow_free(new_flow, false);
0c9fd022
JR
962error:
963 return error;
964}
965
1d2a1b5f
JG
966static struct sw_flow_actions *get_flow_actions(const struct nlattr *a,
967 const struct sw_flow_key *key,
968 const struct sw_flow_mask *mask)
969{
970 struct sw_flow_actions *acts;
971 struct sw_flow_key masked_key;
972 int error;
973
974 acts = ovs_nla_alloc_flow_actions(nla_len(a));
975 if (IS_ERR(acts))
976 return acts;
977
978 ovs_flow_mask_key(&masked_key, key, mask);
ccf43786 979 error = ovs_nla_copy_actions(a, &masked_key, &acts);
1d2a1b5f
JG
980 if (error) {
981 OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
982 kfree(acts);
983 return ERR_PTR(error);
984 }
985
986 return acts;
987}
988
0c9fd022
JR
989static int ovs_flow_cmd_set(struct sk_buff *skb, struct genl_info *info)
990{
991 struct nlattr **a = info->attrs;
992 struct ovs_header *ovs_header = info->userhdr;
1d2a1b5f 993 struct sw_flow_key key;
0c9fd022
JR
994 struct sw_flow *flow;
995 struct sw_flow_mask mask;
996 struct sk_buff *reply = NULL;
997 struct datapath *dp;
6740b721 998 struct sw_flow_actions *old_acts = NULL, *acts = NULL;
0c9fd022
JR
999 struct sw_flow_match match;
1000 int error;
1001
1002 /* Extract key. */
1003 error = -EINVAL;
1004 if (!a[OVS_FLOW_ATTR_KEY])
1005 goto error;
1006
1007 ovs_match_init(&match, &key, &mask);
1008 error = ovs_nla_get_match(&match,
1009 a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
1010 if (error)
1011 goto error;
d6569377 1012
0c9fd022
JR
1013 /* Validate actions. */
1014 if (a[OVS_FLOW_ATTR_ACTIONS]) {
1d2a1b5f
JG
1015 acts = get_flow_actions(a[OVS_FLOW_ATTR_ACTIONS], &key, &mask);
1016 if (IS_ERR(acts)) {
1017 error = PTR_ERR(acts);
0c9fd022 1018 goto error;
6740b721
JR
1019 }
1020 }
1021
1022 /* Can allocate before locking if have acts. */
1023 if (acts) {
1024 reply = ovs_flow_cmd_alloc_info(acts, info, false);
1025 if (IS_ERR(reply)) {
1026 error = PTR_ERR(reply);
1027 goto err_kfree_acts;
90b8c2f7 1028 }
0c9fd022
JR
1029 }
1030
1031 ovs_lock();
1032 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
6740b721
JR
1033 if (unlikely(!dp)) {
1034 error = -ENODEV;
0c9fd022 1035 goto err_unlock_ovs;
6740b721 1036 }
0c9fd022
JR
1037 /* Check that the flow exists. */
1038 flow = ovs_flow_tbl_lookup(&dp->table, &key);
6740b721
JR
1039 if (unlikely(!flow)) {
1040 error = -ENOENT;
0c9fd022 1041 goto err_unlock_ovs;
6740b721 1042 }
0c9fd022 1043 /* The unmasked key has to be the same for flow updates. */
6740b721
JR
1044 if (unlikely(!ovs_flow_cmp_unmasked_key(flow, &match))) {
1045 error = -EEXIST;
0c9fd022 1046 goto err_unlock_ovs;
6740b721 1047 }
0c9fd022 1048 /* Update actions, if present. */
6740b721 1049 if (likely(acts)) {
0c9fd022
JR
1050 old_acts = ovsl_dereference(flow->sf_acts);
1051 rcu_assign_pointer(flow->sf_acts, acts);
6740b721
JR
1052
1053 if (unlikely(reply)) {
c1fc1411 1054 error = ovs_flow_cmd_fill_info(dp, flow,
6740b721
JR
1055 ovs_header->dp_ifindex,
1056 reply, info->snd_portid,
1057 info->snd_seq, 0,
1058 OVS_FLOW_CMD_NEW);
1059 BUG_ON(error < 0);
1060 }
1061 } else {
1062 /* Could not alloc without acts before locking. */
c1fc1411
JG
1063 reply = ovs_flow_cmd_build_info(dp, flow,
1064 ovs_header->dp_ifindex,
6740b721
JR
1065 info, OVS_FLOW_CMD_NEW, false);
1066 if (unlikely(IS_ERR(reply))) {
1067 error = PTR_ERR(reply);
1068 goto err_unlock_ovs;
1069 }
9c52546b 1070 }
0c9fd022 1071
0c9fd022
JR
1072 /* Clear stats. */
1073 if (a[OVS_FLOW_ATTR_CLEAR])
1074 ovs_flow_stats_clear(flow);
cd2a59e9 1075 ovs_unlock();
37a1300c 1076
6740b721 1077 if (reply)
cb25142c 1078 ovs_notify(&dp_flow_genl_family, &ovs_dp_flow_multicast_group, reply, info);
6740b721
JR
1079 if (old_acts)
1080 ovs_nla_free_flow_actions(old_acts);
d6569377 1081 return 0;
704a1e09 1082
cd2a59e9
PS
1083err_unlock_ovs:
1084 ovs_unlock();
6740b721
JR
1085 kfree_skb(reply);
1086err_kfree_acts:
ba400435 1087 kfree(acts);
37a1300c 1088error:
9c52546b 1089 return error;
704a1e09
BP
1090}
1091
df2c07f4 1092static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
704a1e09 1093{
37a1300c 1094 struct nlattr **a = info->attrs;
df2c07f4 1095 struct ovs_header *ovs_header = info->userhdr;
37a1300c 1096 struct sw_flow_key key;
37a1300c 1097 struct sk_buff *reply;
704a1e09 1098 struct sw_flow *flow;
9c52546b 1099 struct datapath *dp;
a1c564be 1100 struct sw_flow_match match;
9c52546b 1101 int err;
704a1e09 1102
1b936472
AZ
1103 if (!a[OVS_FLOW_ATTR_KEY]) {
1104 OVS_NLERR("Flow get message rejected, Key attribute missing.\n");
37a1300c 1105 return -EINVAL;
1b936472 1106 }
a1c564be
AZ
1107
1108 ovs_match_init(&match, &key, NULL);
df65fec1 1109 err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL);
37a1300c
BP
1110 if (err)
1111 return err;
704a1e09 1112
cd2a59e9 1113 ovs_lock();
2a4999f3 1114 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
cd2a59e9
PS
1115 if (!dp) {
1116 err = -ENODEV;
1117 goto unlock;
1118 }
704a1e09 1119
4f88b5e5 1120 flow = ovs_flow_tbl_lookup(&dp->table, &key);
a097c0b2 1121 if (!flow || !ovs_flow_cmp_unmasked_key(flow, &match)) {
cd2a59e9
PS
1122 err = -ENOENT;
1123 goto unlock;
1124 }
d6569377 1125
c1fc1411 1126 reply = ovs_flow_cmd_build_info(dp, flow, ovs_header->dp_ifindex, info,
f71db6b1 1127 OVS_FLOW_CMD_NEW, true);
cd2a59e9
PS
1128 if (IS_ERR(reply)) {
1129 err = PTR_ERR(reply);
1130 goto unlock;
1131 }
36956a7d 1132
cd2a59e9 1133 ovs_unlock();
37a1300c 1134 return genlmsg_reply(reply, info);
cd2a59e9
PS
1135unlock:
1136 ovs_unlock();
1137 return err;
d6569377 1138}
9c52546b 1139
df2c07f4 1140static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
d6569377 1141{
37a1300c 1142 struct nlattr **a = info->attrs;
df2c07f4 1143 struct ovs_header *ovs_header = info->userhdr;
37a1300c 1144 struct sw_flow_key key;
37a1300c 1145 struct sk_buff *reply;
d6569377 1146 struct sw_flow *flow;
d6569377 1147 struct datapath *dp;
a1c564be 1148 struct sw_flow_match match;
d6569377 1149 int err;
36956a7d 1150
cde7f3ba
JR
1151 if (likely(a[OVS_FLOW_ATTR_KEY])) {
1152 ovs_match_init(&match, &key, NULL);
1153 err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL);
1154 if (unlikely(err))
1155 return err;
1156 }
1157
cd2a59e9 1158 ovs_lock();
2a4999f3 1159 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
cde7f3ba 1160 if (unlikely(!dp)) {
cd2a59e9
PS
1161 err = -ENODEV;
1162 goto unlock;
1163 }
cde7f3ba 1164 if (unlikely(!a[OVS_FLOW_ATTR_KEY])) {
994dc286 1165 err = ovs_flow_tbl_flush(&dp->table);
cd2a59e9
PS
1166 goto unlock;
1167 }
4f88b5e5 1168 flow = ovs_flow_tbl_lookup(&dp->table, &key);
cde7f3ba 1169 if (unlikely(!flow || !ovs_flow_cmp_unmasked_key(flow, &match))) {
cd2a59e9
PS
1170 err = -ENOENT;
1171 goto unlock;
1172 }
d6569377 1173
994dc286 1174 ovs_flow_tbl_remove(&dp->table, flow);
cde7f3ba 1175 ovs_unlock();
37a1300c 1176
cde7f3ba
JR
1177 reply = ovs_flow_cmd_alloc_info((const struct sw_flow_actions __force *)flow->sf_acts,
1178 info, false);
1179
1180 if (likely(reply)) {
1181 if (likely(!IS_ERR(reply))) {
1182 rcu_read_lock(); /* Keep RCU checker happy. */
c1fc1411 1183 err = ovs_flow_cmd_fill_info(dp, flow,
cde7f3ba
JR
1184 ovs_header->dp_ifindex,
1185 reply, info->snd_portid,
1186 info->snd_seq, 0,
1187 OVS_FLOW_CMD_DEL);
1188 rcu_read_unlock();
1189 BUG_ON(err < 0);
cb25142c 1190 ovs_notify(&dp_flow_genl_family, &ovs_dp_flow_multicast_group, reply, info);
cde7f3ba 1191 } else {
cb25142c
PS
1192 genl_set_err(&dp_flow_genl_family, sock_net(skb->sk), 0,
1193 GROUP_ID(&ovs_dp_flow_multicast_group), PTR_ERR(reply));
1194
cde7f3ba 1195 }
afad3556 1196 }
37a1300c 1197
a1c564be 1198 ovs_flow_free(flow, true);
37a1300c 1199 return 0;
cd2a59e9
PS
1200unlock:
1201 ovs_unlock();
1202 return err;
37a1300c
BP
1203}
1204
df2c07f4 1205static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
37a1300c 1206{
df2c07f4 1207 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
994dc286 1208 struct table_instance *ti;
37a1300c
BP
1209 struct datapath *dp;
1210
f44ccce1 1211 rcu_read_lock();
2a4999f3 1212 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
cd2a59e9 1213 if (!dp) {
f44ccce1 1214 rcu_read_unlock();
37a1300c 1215 return -ENODEV;
cd2a59e9 1216 }
37a1300c 1217
994dc286 1218 ti = rcu_dereference(dp->table.ti);
37a1300c 1219 for (;;) {
37a1300c
BP
1220 struct sw_flow *flow;
1221 u32 bucket, obj;
1222
1223 bucket = cb->args[0];
1224 obj = cb->args[1];
994dc286 1225 flow = ovs_flow_tbl_dump_next(ti, &bucket, &obj);
3544358a 1226 if (!flow)
37a1300c
BP
1227 break;
1228
c1fc1411 1229 if (ovs_flow_cmd_fill_info(dp, flow, ovs_header->dp_ifindex, skb,
28aea917 1230 NETLINK_CB(cb->skb).portid,
37a1300c 1231 cb->nlh->nlmsg_seq, NLM_F_MULTI,
df2c07f4 1232 OVS_FLOW_CMD_NEW) < 0)
37a1300c
BP
1233 break;
1234
1235 cb->args[0] = bucket;
1236 cb->args[1] = obj;
1237 }
f44ccce1 1238 rcu_read_unlock();
37a1300c 1239 return skb->len;
704a1e09
BP
1240}
1241
cb25142c
PS
1242static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
1243 [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
1244 [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
1245 [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
1246};
1247
37a1300c 1248static struct genl_ops dp_flow_genl_ops[] = {
df2c07f4 1249 { .cmd = OVS_FLOW_CMD_NEW,
37a1300c
BP
1250 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1251 .policy = flow_policy,
0c9fd022 1252 .doit = ovs_flow_cmd_new
37a1300c 1253 },
df2c07f4 1254 { .cmd = OVS_FLOW_CMD_DEL,
37a1300c
BP
1255 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1256 .policy = flow_policy,
df2c07f4 1257 .doit = ovs_flow_cmd_del
37a1300c 1258 },
df2c07f4 1259 { .cmd = OVS_FLOW_CMD_GET,
37a1300c
BP
1260 .flags = 0, /* OK for unprivileged users. */
1261 .policy = flow_policy,
df2c07f4
JP
1262 .doit = ovs_flow_cmd_get,
1263 .dumpit = ovs_flow_cmd_dump
37a1300c 1264 },
df2c07f4 1265 { .cmd = OVS_FLOW_CMD_SET,
37a1300c
BP
1266 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1267 .policy = flow_policy,
0c9fd022 1268 .doit = ovs_flow_cmd_set,
37a1300c
BP
1269 },
1270};
1271
cb25142c 1272static struct genl_family dp_flow_genl_family = {
aaff4b55 1273 .id = GENL_ID_GENERATE,
df2c07f4 1274 .hdrsize = sizeof(struct ovs_header),
cb25142c
PS
1275 .name = OVS_FLOW_FAMILY,
1276 .version = OVS_FLOW_VERSION,
1277 .maxattr = OVS_FLOW_ATTR_MAX,
b3dcb73c 1278 .netnsok = true,
cb25142c
PS
1279 .parallel_ops = true,
1280 .ops = dp_flow_genl_ops,
1281 .n_ops = ARRAY_SIZE(dp_flow_genl_ops),
1282 .mcgrps = &ovs_dp_flow_multicast_group,
1283 .n_mcgrps = 1,
aaff4b55
BP
1284};
1285
0afa2373
TG
1286static size_t ovs_dp_cmd_msg_size(void)
1287{
1288 size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
1289
1290 msgsize += nla_total_size(IFNAMSIZ);
1291 msgsize += nla_total_size(sizeof(struct ovs_dp_stats));
4fa72a95 1292 msgsize += nla_total_size(sizeof(struct ovs_dp_megaflow_stats));
300af20a 1293 msgsize += nla_total_size(sizeof(u32)); /* OVS_DP_ATTR_USER_FEATURES */
0afa2373
TG
1294
1295 return msgsize;
1296}
1297
aa917006 1298/* Called with ovs_mutex or RCU read lock. */
df2c07f4 1299static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
28aea917 1300 u32 portid, u32 seq, u32 flags, u8 cmd)
064af421 1301{
df2c07f4 1302 struct ovs_header *ovs_header;
e926dfe3 1303 struct ovs_dp_stats dp_stats;
4fa72a95 1304 struct ovs_dp_megaflow_stats dp_megaflow_stats;
064af421
BP
1305 int err;
1306
28aea917 1307 ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
aaff4b55 1308 flags, cmd);
df2c07f4 1309 if (!ovs_header)
aaff4b55 1310 goto error;
064af421 1311
b063d9f0 1312 ovs_header->dp_ifindex = get_dpifindex(dp);
064af421 1313
850b6b3b 1314 err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
064af421 1315 if (err)
d6569377 1316 goto nla_put_failure;
064af421 1317
4fa72a95
AZ
1318 get_dp_stats(dp, &dp_stats, &dp_megaflow_stats);
1319 if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats),
1320 &dp_stats))
1321 goto nla_put_failure;
1322
1323 if (nla_put(skb, OVS_DP_ATTR_MEGAFLOW_STATS,
1324 sizeof(struct ovs_dp_megaflow_stats),
1325 &dp_megaflow_stats))
c3cc8c03 1326 goto nla_put_failure;
d6569377 1327
c58cc9a4
TG
1328 if (nla_put_u32(skb, OVS_DP_ATTR_USER_FEATURES, dp->user_features))
1329 goto nla_put_failure;
1330
df2c07f4 1331 return genlmsg_end(skb, ovs_header);
d6569377
BP
1332
1333nla_put_failure:
df2c07f4 1334 genlmsg_cancel(skb, ovs_header);
aaff4b55
BP
1335error:
1336 return -EMSGSIZE;
d6569377
BP
1337}
1338
d81eef1b 1339static struct sk_buff *ovs_dp_cmd_alloc_info(struct genl_info *info)
d6569377 1340{
d81eef1b 1341 return genlmsg_new_unicast(ovs_dp_cmd_msg_size(), info, GFP_KERNEL);
aaff4b55 1342}
9dca7bd5 1343
aa917006 1344/* Called with rcu_read_lock or ovs_mutex. */
2a4999f3
PS
1345static struct datapath *lookup_datapath(struct net *net,
1346 struct ovs_header *ovs_header,
6455100f 1347 struct nlattr *a[OVS_DP_ATTR_MAX + 1])
d6569377 1348{
254f2dc8
BP
1349 struct datapath *dp;
1350
df2c07f4 1351 if (!a[OVS_DP_ATTR_NAME])
2a4999f3 1352 dp = get_dp(net, ovs_header->dp_ifindex);
254f2dc8 1353 else {
d6569377 1354 struct vport *vport;
d6569377 1355
2a4999f3 1356 vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
df2c07f4 1357 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
d6569377 1358 }
254f2dc8 1359 return dp ? dp : ERR_PTR(-ENODEV);
d6569377
BP
1360}
1361
94358dcf
TG
1362static void ovs_dp_reset_user_features(struct sk_buff *skb, struct genl_info *info)
1363{
1364 struct datapath *dp;
1365
1366 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
09350a3d 1367 if (IS_ERR(dp))
94358dcf
TG
1368 return;
1369
1370 WARN(dp->user_features, "Dropping previously announced user features\n");
1371 dp->user_features = 0;
1372}
1373
c58cc9a4
TG
1374static void ovs_dp_change(struct datapath *dp, struct nlattr **a)
1375{
1376 if (a[OVS_DP_ATTR_USER_FEATURES])
1377 dp->user_features = nla_get_u32(a[OVS_DP_ATTR_USER_FEATURES]);
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
d81eef1b
JR
1394 reply = ovs_dp_cmd_alloc_info(info);
1395 if (!reply)
1396 return -ENOMEM;
d6569377 1397
d6569377
BP
1398 err = -ENOMEM;
1399 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1400 if (dp == NULL)
d81eef1b 1401 goto err_free_reply;
2a4999f3 1402
0ceaa66c
JG
1403 ovs_dp_set_net(dp, hold_net(sock_net(skb->sk)));
1404
d6569377 1405 /* Allocate table. */
994dc286
PS
1406 err = ovs_flow_tbl_init(&dp->table);
1407 if (err)
d6569377
BP
1408 goto err_free_dp;
1409
99769a40
JG
1410 dp->stats_percpu = alloc_percpu(struct dp_stats_percpu);
1411 if (!dp->stats_percpu) {
1412 err = -ENOMEM;
1413 goto err_destroy_table;
1414 }
1415
cb25142c
PS
1416 for_each_possible_cpu(i) {
1417 struct dp_stats_percpu *dpath_stats;
1418 dpath_stats = per_cpu_ptr(dp->stats_percpu, i);
1419 u64_stats_init(&dpath_stats->sync);
1420 }
1421
95b1d73a
PS
1422 dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
1423 GFP_KERNEL);
1424 if (!dp->ports) {
1425 err = -ENOMEM;
1426 goto err_destroy_percpu;
1427 }
1428
1429 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1430 INIT_HLIST_HEAD(&dp->ports[i]);
1431
d6569377 1432 /* Set up our datapath device. */
df2c07f4
JP
1433 parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1434 parms.type = OVS_VPORT_TYPE_INTERNAL;
d6569377
BP
1435 parms.options = NULL;
1436 parms.dp = dp;
df2c07f4 1437 parms.port_no = OVSP_LOCAL;
beb1c69a 1438 parms.upcall_portids = a[OVS_DP_ATTR_UPCALL_PID];
b063d9f0 1439
c58cc9a4
TG
1440 ovs_dp_change(dp, a);
1441
d81eef1b
JR
1442 /* So far only local changes have been made, now need the lock. */
1443 ovs_lock();
1444
d6569377
BP
1445 vport = new_vport(&parms);
1446 if (IS_ERR(vport)) {
1447 err = PTR_ERR(vport);
1448 if (err == -EBUSY)
1449 err = -EEXIST;
1450
94358dcf
TG
1451 if (err == -EEXIST) {
1452 /* An outdated user space instance that does not understand
1453 * the concept of user_features has attempted to create a new
1454 * datapath and is likely to reuse it. Drop all user features.
1455 */
1456 if (info->genlhdr->version < OVS_DP_VER_FEATURES)
1457 ovs_dp_reset_user_features(skb, info);
1458 }
1459
95b1d73a 1460 goto err_destroy_ports_array;
d6569377 1461 }
d6569377 1462
d81eef1b
JR
1463 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1464 info->snd_seq, 0, OVS_DP_CMD_NEW);
1465 BUG_ON(err < 0);
aaff4b55 1466
2a4999f3 1467 ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
fb93e9aa 1468 list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
d6569377 1469
cd2a59e9 1470 ovs_unlock();
d6569377 1471
cb25142c 1472 ovs_notify(&dp_datapath_genl_family, &ovs_dp_datapath_multicast_group, reply, info);
d6569377
BP
1473 return 0;
1474
95b1d73a 1475err_destroy_ports_array:
d81eef1b 1476 ovs_unlock();
95b1d73a 1477 kfree(dp->ports);
99769a40
JG
1478err_destroy_percpu:
1479 free_percpu(dp->stats_percpu);
d6569377 1480err_destroy_table:
e379e4d1 1481 ovs_flow_tbl_destroy(&dp->table);
d6569377 1482err_free_dp:
0ceaa66c 1483 release_net(ovs_dp_get_net(dp));
d6569377 1484 kfree(dp);
d81eef1b
JR
1485err_free_reply:
1486 kfree_skb(reply);
d6569377 1487err:
064af421
BP
1488 return err;
1489}
1490
cd2a59e9 1491/* Called with ovs_mutex. */
2a4999f3 1492static void __dp_destroy(struct datapath *dp)
44e05eca 1493{
95b1d73a 1494 int i;
44e05eca 1495
95b1d73a
PS
1496 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1497 struct vport *vport;
f8dfbcb7 1498 struct hlist_node *n;
95b1d73a 1499
f8dfbcb7 1500 hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
95b1d73a
PS
1501 if (vport->port_no != OVSP_LOCAL)
1502 ovs_dp_detach_port(vport);
1503 }
ed099e92 1504
fb93e9aa 1505 list_del_rcu(&dp->list_node);
ed099e92 1506
cd2a59e9 1507 /* OVSP_LOCAL is datapath internal port. We need to make sure that
d103f479
AZ
1508 * all ports in datapath are destroyed first before freeing datapath.
1509 */
cd2a59e9 1510 ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
99620d2c 1511
d103f479 1512 /* RCU destroy the flow table */
ed099e92 1513 call_rcu(&dp->rcu, destroy_dp_rcu);
2a4999f3
PS
1514}
1515
1516static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1517{
1518 struct sk_buff *reply;
1519 struct datapath *dp;
1520 int err;
1521
d81eef1b
JR
1522 reply = ovs_dp_cmd_alloc_info(info);
1523 if (!reply)
1524 return -ENOMEM;
1525
cd2a59e9 1526 ovs_lock();
2a4999f3
PS
1527 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1528 err = PTR_ERR(dp);
1529 if (IS_ERR(dp))
d81eef1b 1530 goto err_unlock_free;
2a4999f3 1531
d81eef1b
JR
1532 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1533 info->snd_seq, 0, OVS_DP_CMD_DEL);
1534 BUG_ON(err < 0);
2a4999f3
PS
1535
1536 __dp_destroy(dp);
ed099e92 1537
d81eef1b 1538 ovs_unlock();
cb25142c 1539 ovs_notify(&dp_datapath_genl_family, &ovs_dp_datapath_multicast_group, reply, info);
99620d2c 1540 return 0;
d81eef1b
JR
1541
1542err_unlock_free:
cd2a59e9 1543 ovs_unlock();
d81eef1b 1544 kfree_skb(reply);
cd2a59e9 1545 return err;
44e05eca
BP
1546}
1547
df2c07f4 1548static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
064af421 1549{
aaff4b55 1550 struct sk_buff *reply;
d6569377 1551 struct datapath *dp;
d6569377 1552 int err;
064af421 1553
d81eef1b
JR
1554 reply = ovs_dp_cmd_alloc_info(info);
1555 if (!reply)
1556 return -ENOMEM;
1557
cd2a59e9 1558 ovs_lock();
2a4999f3 1559 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
cd2a59e9 1560 err = PTR_ERR(dp);
d6569377 1561 if (IS_ERR(dp))
d81eef1b 1562 goto err_unlock_free;
38c6ecbc 1563
c58cc9a4
TG
1564 ovs_dp_change(dp, info->attrs);
1565
d81eef1b
JR
1566 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1567 info->snd_seq, 0, OVS_DP_CMD_NEW);
1568 BUG_ON(err < 0);
aaff4b55 1569
cd2a59e9 1570 ovs_unlock();
cb25142c 1571 ovs_notify(&dp_datapath_genl_family, &ovs_dp_datapath_multicast_group, reply, info);
aaff4b55 1572 return 0;
d81eef1b
JR
1573
1574err_unlock_free:
cd2a59e9 1575 ovs_unlock();
d81eef1b 1576 kfree_skb(reply);
cd2a59e9 1577 return err;
064af421
BP
1578}
1579
df2c07f4 1580static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1dcf111b 1581{
aaff4b55 1582 struct sk_buff *reply;
d6569377 1583 struct datapath *dp;
d6569377 1584 int err;
1dcf111b 1585
d81eef1b
JR
1586 reply = ovs_dp_cmd_alloc_info(info);
1587 if (!reply)
1588 return -ENOMEM;
1589
1590 rcu_read_lock();
2a4999f3 1591 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
cd2a59e9
PS
1592 if (IS_ERR(dp)) {
1593 err = PTR_ERR(dp);
d81eef1b 1594 goto err_unlock_free;
cd2a59e9 1595 }
d81eef1b
JR
1596 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1597 info->snd_seq, 0, OVS_DP_CMD_NEW);
1598 BUG_ON(err < 0);
1599 rcu_read_unlock();
aaff4b55
BP
1600
1601 return genlmsg_reply(reply, info);
cd2a59e9 1602
d81eef1b
JR
1603err_unlock_free:
1604 rcu_read_unlock();
1605 kfree_skb(reply);
cd2a59e9 1606 return err;
1dcf111b
JP
1607}
1608
df2c07f4 1609static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
a7786963 1610{
2a4999f3 1611 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
254f2dc8
BP
1612 struct datapath *dp;
1613 int skip = cb->args[0];
1614 int i = 0;
a7786963 1615
fb93e9aa
PS
1616 rcu_read_lock();
1617 list_for_each_entry_rcu(dp, &ovs_net->dps, list_node) {
a2bab2f0 1618 if (i >= skip &&
28aea917 1619 ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
aaff4b55 1620 cb->nlh->nlmsg_seq, NLM_F_MULTI,
df2c07f4 1621 OVS_DP_CMD_NEW) < 0)
aaff4b55 1622 break;
254f2dc8 1623 i++;
a7786963 1624 }
fb93e9aa 1625 rcu_read_unlock();
aaff4b55 1626
254f2dc8
BP
1627 cb->args[0] = i;
1628
aaff4b55 1629 return skb->len;
c19e6535
BP
1630}
1631
cb25142c
PS
1632static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1633 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1634 [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1635 [OVS_DP_ATTR_USER_FEATURES] = { .type = NLA_U32 },
1636};
1637
aaff4b55 1638static struct genl_ops dp_datapath_genl_ops[] = {
df2c07f4 1639 { .cmd = OVS_DP_CMD_NEW,
aaff4b55
BP
1640 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1641 .policy = datapath_policy,
df2c07f4 1642 .doit = ovs_dp_cmd_new
aaff4b55 1643 },
df2c07f4 1644 { .cmd = OVS_DP_CMD_DEL,
aaff4b55
BP
1645 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1646 .policy = datapath_policy,
df2c07f4 1647 .doit = ovs_dp_cmd_del
aaff4b55 1648 },
df2c07f4 1649 { .cmd = OVS_DP_CMD_GET,
aaff4b55
BP
1650 .flags = 0, /* OK for unprivileged users. */
1651 .policy = datapath_policy,
df2c07f4
JP
1652 .doit = ovs_dp_cmd_get,
1653 .dumpit = ovs_dp_cmd_dump
aaff4b55 1654 },
df2c07f4 1655 { .cmd = OVS_DP_CMD_SET,
aaff4b55
BP
1656 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1657 .policy = datapath_policy,
df2c07f4 1658 .doit = ovs_dp_cmd_set,
aaff4b55
BP
1659 },
1660};
1661
cb25142c 1662static struct genl_family dp_datapath_genl_family = {
f0fef760 1663 .id = GENL_ID_GENERATE,
df2c07f4 1664 .hdrsize = sizeof(struct ovs_header),
cb25142c
PS
1665 .name = OVS_DATAPATH_FAMILY,
1666 .version = OVS_DATAPATH_VERSION,
1667 .maxattr = OVS_DP_ATTR_MAX,
b3dcb73c 1668 .netnsok = true,
cb25142c
PS
1669 .parallel_ops = true,
1670 .ops = dp_datapath_genl_ops,
1671 .n_ops = ARRAY_SIZE(dp_datapath_genl_ops),
1672 .mcgrps = &ovs_dp_datapath_multicast_group,
1673 .n_mcgrps = 1,
f0fef760
BP
1674};
1675
cd2a59e9 1676/* Called with ovs_mutex or RCU read lock. */
df2c07f4 1677static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
28aea917 1678 u32 portid, u32 seq, u32 flags, u8 cmd)
064af421 1679{
df2c07f4 1680 struct ovs_header *ovs_header;
e926dfe3 1681 struct ovs_vport_stats vport_stats;
c19e6535
BP
1682 int err;
1683
28aea917 1684 ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
f0fef760 1685 flags, cmd);
df2c07f4 1686 if (!ovs_header)
f0fef760 1687 return -EMSGSIZE;
c19e6535 1688
99769a40 1689 ovs_header->dp_ifindex = get_dpifindex(vport->dp);
c19e6535 1690
c3cc8c03
DM
1691 if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
1692 nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
beb1c69a 1693 nla_put_string(skb, OVS_VPORT_ATTR_NAME, vport->ops->get_name(vport)))
c3cc8c03 1694 goto nla_put_failure;
c19e6535 1695
850b6b3b 1696 ovs_vport_get_stats(vport, &vport_stats);
c3cc8c03
DM
1697 if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
1698 &vport_stats))
1699 goto nla_put_failure;
c19e6535 1700
beb1c69a
AW
1701 if (ovs_vport_get_upcall_portids(vport, skb))
1702 goto nla_put_failure;
1703
850b6b3b 1704 err = ovs_vport_get_options(vport, skb);
f0fef760
BP
1705 if (err == -EMSGSIZE)
1706 goto error;
c19e6535 1707
df2c07f4 1708 return genlmsg_end(skb, ovs_header);
c19e6535
BP
1709
1710nla_put_failure:
1711 err = -EMSGSIZE;
f0fef760 1712error:
df2c07f4 1713 genlmsg_cancel(skb, ovs_header);
f0fef760 1714 return err;
064af421
BP
1715}
1716
d81eef1b
JR
1717static struct sk_buff *ovs_vport_cmd_alloc_info(void)
1718{
1719 return nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1720}
1721
1722/* Called with ovs_mutex, only via ovs_dp_notify_wq(). */
28aea917 1723struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
f14d8083 1724 u32 seq, u8 cmd)
064af421 1725{
c19e6535 1726 struct sk_buff *skb;
f0fef760 1727 int retval;
c19e6535 1728
f0fef760 1729 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
c19e6535
BP
1730 if (!skb)
1731 return ERR_PTR(-ENOMEM);
1732
28aea917 1733 retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
c25ea534
JG
1734 BUG_ON(retval < 0);
1735
c19e6535 1736 return skb;
f0fef760 1737}
c19e6535 1738
cd2a59e9 1739/* Called with ovs_mutex or RCU read lock. */
2a4999f3
PS
1740static struct vport *lookup_vport(struct net *net,
1741 struct ovs_header *ovs_header,
df2c07f4 1742 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
c19e6535
BP
1743{
1744 struct datapath *dp;
1745 struct vport *vport;
1746
df2c07f4 1747 if (a[OVS_VPORT_ATTR_NAME]) {
2a4999f3 1748 vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
ed099e92 1749 if (!vport)
c19e6535 1750 return ERR_PTR(-ENODEV);
24ce832d
BP
1751 if (ovs_header->dp_ifindex &&
1752 ovs_header->dp_ifindex != get_dpifindex(vport->dp))
1753 return ERR_PTR(-ENODEV);
c19e6535 1754 return vport;
df2c07f4
JP
1755 } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
1756 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
c19e6535
BP
1757
1758 if (port_no >= DP_MAX_PORTS)
f0fef760 1759 return ERR_PTR(-EFBIG);
c19e6535 1760
2a4999f3 1761 dp = get_dp(net, ovs_header->dp_ifindex);
c19e6535
BP
1762 if (!dp)
1763 return ERR_PTR(-ENODEV);
f2459fe7 1764
cd2a59e9 1765 vport = ovs_vport_ovsl_rcu(dp, port_no);
ed099e92 1766 if (!vport)
17535c57 1767 return ERR_PTR(-ENODEV);
c19e6535
BP
1768 return vport;
1769 } else
1770 return ERR_PTR(-EINVAL);
064af421
BP
1771}
1772
df2c07f4 1773static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
c19e6535 1774{
f0fef760 1775 struct nlattr **a = info->attrs;
df2c07f4 1776 struct ovs_header *ovs_header = info->userhdr;
c19e6535 1777 struct vport_parms parms;
ed099e92 1778 struct sk_buff *reply;
c19e6535 1779 struct vport *vport;
c19e6535 1780 struct datapath *dp;
b0ec0f27 1781 u32 port_no;
c19e6535 1782 int err;
b0ec0f27 1783
ea36840f
BP
1784 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
1785 !a[OVS_VPORT_ATTR_UPCALL_PID])
d81eef1b
JR
1786 return -EINVAL;
1787
1788 port_no = a[OVS_VPORT_ATTR_PORT_NO]
1789 ? nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]) : 0;
1790 if (port_no >= DP_MAX_PORTS)
1791 return -EFBIG;
1792
1793 reply = ovs_vport_cmd_alloc_info();
1794 if (!reply)
1795 return -ENOMEM;
f0fef760 1796
cd2a59e9 1797 ovs_lock();
2a4999f3 1798 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
c19e6535
BP
1799 err = -ENODEV;
1800 if (!dp)
d81eef1b 1801 goto exit_unlock_free;
c19e6535 1802
d81eef1b 1803 if (port_no) {
cd2a59e9 1804 vport = ovs_vport_ovsl(dp, port_no);
c19e6535
BP
1805 err = -EBUSY;
1806 if (vport)
d81eef1b 1807 goto exit_unlock_free;
c19e6535
BP
1808 } else {
1809 for (port_no = 1; ; port_no++) {
1810 if (port_no >= DP_MAX_PORTS) {
1811 err = -EFBIG;
d81eef1b 1812 goto exit_unlock_free;
c19e6535 1813 }
cd2a59e9 1814 vport = ovs_vport_ovsl(dp, port_no);
c19e6535
BP
1815 if (!vport)
1816 break;
51d4d598 1817 }
064af421 1818 }
b0ec0f27 1819
df2c07f4
JP
1820 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
1821 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1822 parms.options = a[OVS_VPORT_ATTR_OPTIONS];
c19e6535
BP
1823 parms.dp = dp;
1824 parms.port_no = port_no;
beb1c69a 1825 parms.upcall_portids = a[OVS_VPORT_ATTR_UPCALL_PID];
c19e6535
BP
1826
1827 vport = new_vport(&parms);
1828 err = PTR_ERR(vport);
1829 if (IS_ERR(vport))
d81eef1b 1830 goto exit_unlock_free;
c19e6535 1831
faef6d2d 1832 err = 0;
1fc7083d
JG
1833 if (a[OVS_VPORT_ATTR_STATS])
1834 ovs_vport_set_stats(vport, nla_data(a[OVS_VPORT_ATTR_STATS]));
1835
d81eef1b
JR
1836 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1837 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1838 BUG_ON(err < 0);
1839 ovs_unlock();
e297c6b7 1840
cb25142c 1841 ovs_notify(&dp_vport_genl_family, &ovs_dp_vport_multicast_group, reply, info);
d81eef1b 1842 return 0;
c19e6535 1843
d81eef1b 1844exit_unlock_free:
cd2a59e9 1845 ovs_unlock();
d81eef1b 1846 kfree_skb(reply);
c19e6535 1847 return err;
44e05eca
BP
1848}
1849
df2c07f4 1850static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
44e05eca 1851{
f0fef760
BP
1852 struct nlattr **a = info->attrs;
1853 struct sk_buff *reply;
c19e6535 1854 struct vport *vport;
c19e6535 1855 int err;
44e05eca 1856
d81eef1b
JR
1857 reply = ovs_vport_cmd_alloc_info();
1858 if (!reply)
1859 return -ENOMEM;
1860
cd2a59e9 1861 ovs_lock();
2a4999f3 1862 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
c19e6535
BP
1863 err = PTR_ERR(vport);
1864 if (IS_ERR(vport))
d81eef1b 1865 goto exit_unlock_free;
44e05eca 1866
6455100f 1867 if (a[OVS_VPORT_ATTR_TYPE] &&
17ec1d04 1868 nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
4879d4c7 1869 err = -EINVAL;
d81eef1b 1870 goto exit_unlock_free;
c25ea534
JG
1871 }
1872
17ec1d04 1873 if (a[OVS_VPORT_ATTR_OPTIONS]) {
850b6b3b 1874 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
17ec1d04 1875 if (err)
d81eef1b 1876 goto exit_unlock_free;
17ec1d04 1877 }
1fc7083d
JG
1878
1879 if (a[OVS_VPORT_ATTR_STATS])
1880 ovs_vport_set_stats(vport, nla_data(a[OVS_VPORT_ATTR_STATS]));
1881
beb1c69a
AW
1882
1883 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
1884 err = ovs_vport_set_upcall_portids(vport,
1885 a[OVS_VPORT_ATTR_UPCALL_PID]);
1886 if (err)
1887 goto exit_unlock_free;
1888 }
c19e6535 1889
c25ea534
JG
1890 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1891 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1892 BUG_ON(err < 0);
cd2a59e9 1893 ovs_unlock();
d81eef1b 1894
cb25142c 1895 ovs_notify(&dp_vport_genl_family, &ovs_dp_vport_multicast_group, reply, info);
c25ea534
JG
1896 return 0;
1897
d81eef1b 1898exit_unlock_free:
cd2a59e9 1899 ovs_unlock();
d81eef1b 1900 kfree_skb(reply);
c19e6535 1901 return err;
064af421
BP
1902}
1903
df2c07f4 1904static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
7c40efc9 1905{
f0fef760
BP
1906 struct nlattr **a = info->attrs;
1907 struct sk_buff *reply;
c19e6535 1908 struct vport *vport;
c19e6535
BP
1909 int err;
1910
d81eef1b
JR
1911 reply = ovs_vport_cmd_alloc_info();
1912 if (!reply)
1913 return -ENOMEM;
1914
cd2a59e9 1915 ovs_lock();
2a4999f3 1916 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
c19e6535 1917 err = PTR_ERR(vport);
f0fef760 1918 if (IS_ERR(vport))
d81eef1b 1919 goto exit_unlock_free;
c19e6535 1920
df2c07f4 1921 if (vport->port_no == OVSP_LOCAL) {
f0fef760 1922 err = -EINVAL;
d81eef1b 1923 goto exit_unlock_free;
f0fef760
BP
1924 }
1925
d81eef1b
JR
1926 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1927 info->snd_seq, 0, OVS_VPORT_CMD_DEL);
1928 BUG_ON(err < 0);
850b6b3b 1929 ovs_dp_detach_port(vport);
d81eef1b 1930 ovs_unlock();
f0fef760 1931
cb25142c 1932 ovs_notify(&dp_vport_genl_family, &ovs_dp_vport_multicast_group, reply, info);
d81eef1b 1933 return 0;
f0fef760 1934
d81eef1b 1935exit_unlock_free:
cd2a59e9 1936 ovs_unlock();
d81eef1b 1937 kfree_skb(reply);
c19e6535 1938 return err;
7c40efc9
BP
1939}
1940
df2c07f4 1941static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
7c40efc9 1942{
f0fef760 1943 struct nlattr **a = info->attrs;
df2c07f4 1944 struct ovs_header *ovs_header = info->userhdr;
ed099e92 1945 struct sk_buff *reply;
c19e6535 1946 struct vport *vport;
c19e6535
BP
1947 int err;
1948
d81eef1b
JR
1949 reply = ovs_vport_cmd_alloc_info();
1950 if (!reply)
1951 return -ENOMEM;
1952
ed099e92 1953 rcu_read_lock();
2a4999f3 1954 vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
c19e6535
BP
1955 err = PTR_ERR(vport);
1956 if (IS_ERR(vport))
d81eef1b
JR
1957 goto exit_unlock_free;
1958 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1959 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1960 BUG_ON(err < 0);
df2fa9b5
JG
1961 rcu_read_unlock();
1962
1963 return genlmsg_reply(reply, info);
ed099e92 1964
d81eef1b 1965exit_unlock_free:
ed099e92 1966 rcu_read_unlock();
d81eef1b 1967 kfree_skb(reply);
c19e6535
BP
1968 return err;
1969}
1970
df2c07f4 1971static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
c19e6535 1972{
df2c07f4 1973 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
c19e6535 1974 struct datapath *dp;
95b1d73a
PS
1975 int bucket = cb->args[0], skip = cb->args[1];
1976 int i, j = 0;
c19e6535 1977
03fc2881 1978 rcu_read_lock();
2a4999f3 1979 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
03fc2881
JR
1980 if (!dp) {
1981 rcu_read_unlock();
f0fef760 1982 return -ENODEV;
03fc2881 1983 }
95b1d73a 1984 for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
ed099e92 1985 struct vport *vport;
95b1d73a
PS
1986
1987 j = 0;
f8dfbcb7 1988 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
95b1d73a
PS
1989 if (j >= skip &&
1990 ovs_vport_cmd_fill_info(vport, skb,
28aea917 1991 NETLINK_CB(cb->skb).portid,
95b1d73a
PS
1992 cb->nlh->nlmsg_seq,
1993 NLM_F_MULTI,
1994 OVS_VPORT_CMD_NEW) < 0)
1995 goto out;
1996
1997 j++;
1998 }
1999 skip = 0;
c19e6535 2000 }
95b1d73a 2001out:
ed099e92 2002 rcu_read_unlock();
c19e6535 2003
95b1d73a
PS
2004 cb->args[0] = i;
2005 cb->args[1] = j;
f0fef760 2006
95b1d73a 2007 return skb->len;
7c40efc9
BP
2008}
2009
cb25142c
PS
2010static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
2011 [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
2012 [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
2013 [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
2014 [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
2015 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
2016 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
2017};
2018
f0fef760 2019static struct genl_ops dp_vport_genl_ops[] = {
df2c07f4 2020 { .cmd = OVS_VPORT_CMD_NEW,
f0fef760
BP
2021 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2022 .policy = vport_policy,
df2c07f4 2023 .doit = ovs_vport_cmd_new
f0fef760 2024 },
df2c07f4 2025 { .cmd = OVS_VPORT_CMD_DEL,
f0fef760
BP
2026 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2027 .policy = vport_policy,
df2c07f4 2028 .doit = ovs_vport_cmd_del
f0fef760 2029 },
df2c07f4 2030 { .cmd = OVS_VPORT_CMD_GET,
f0fef760
BP
2031 .flags = 0, /* OK for unprivileged users. */
2032 .policy = vport_policy,
df2c07f4
JP
2033 .doit = ovs_vport_cmd_get,
2034 .dumpit = ovs_vport_cmd_dump
f0fef760 2035 },
df2c07f4 2036 { .cmd = OVS_VPORT_CMD_SET,
f0fef760
BP
2037 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2038 .policy = vport_policy,
df2c07f4 2039 .doit = ovs_vport_cmd_set,
f0fef760
BP
2040 },
2041};
2042
cb25142c
PS
2043struct genl_family dp_vport_genl_family = {
2044 .id = GENL_ID_GENERATE,
2045 .hdrsize = sizeof(struct ovs_header),
2046 .name = OVS_VPORT_FAMILY,
2047 .version = OVS_VPORT_VERSION,
2048 .maxattr = OVS_VPORT_ATTR_MAX,
2049 .netnsok = true,
2050 .parallel_ops = true,
2051 .ops = dp_vport_genl_ops,
2052 .n_ops = ARRAY_SIZE(dp_vport_genl_ops),
2053 .mcgrps = &ovs_dp_vport_multicast_group,
2054 .n_mcgrps = 1,
982b8810 2055};
ed099e92 2056
cb25142c
PS
2057static struct genl_family *dp_genl_families[] = {
2058 &dp_datapath_genl_family,
2059 &dp_vport_genl_family,
2060 &dp_flow_genl_family,
2061 &dp_packet_genl_family,
982b8810 2062};
ed099e92 2063
982b8810
BP
2064static void dp_unregister_genl(int n_families)
2065{
2066 int i;
ed099e92 2067
b867ca75 2068 for (i = 0; i < n_families; i++)
cb25142c 2069 genl_unregister_family(dp_genl_families[i]);
ed099e92
BP
2070}
2071
982b8810 2072static int dp_register_genl(void)
064af421 2073{
982b8810
BP
2074 int err;
2075 int i;
064af421 2076
982b8810 2077 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
064af421 2078
cb25142c 2079 err = genl_register_family(dp_genl_families[i]);
982b8810
BP
2080 if (err)
2081 goto error;
982b8810 2082 }
9cc8b4e4 2083
982b8810 2084 return 0;
064af421
BP
2085
2086error:
cb25142c 2087 dp_unregister_genl(i);
982b8810 2088 return err;
064af421
BP
2089}
2090
2a4999f3
PS
2091static int __net_init ovs_init_net(struct net *net)
2092{
2093 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2094
2095 INIT_LIST_HEAD(&ovs_net->dps);
cd2a59e9 2096 INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
2a4999f3
PS
2097 return 0;
2098}
2099
2100static void __net_exit ovs_exit_net(struct net *net)
2101{
cd2a59e9 2102 struct datapath *dp, *dp_next;
2a4999f3
PS
2103 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2104
cd2a59e9
PS
2105 ovs_lock();
2106 list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
2107 __dp_destroy(dp);
2108 ovs_unlock();
2109
2110 cancel_work_sync(&ovs_net->dp_notify_work);
2a4999f3
PS
2111}
2112
2113static struct pernet_operations ovs_net_ops = {
2114 .init = ovs_init_net,
2115 .exit = ovs_exit_net,
2116 .id = &ovs_net_id,
2117 .size = sizeof(struct ovs_net),
2118};
2119
637c8268
PS
2120DEFINE_COMPAT_PNET_REG_FUNC(device);
2121
22d24ebf
BP
2122static int __init dp_init(void)
2123{
2124 int err;
2125
f3d85db3 2126 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
22d24ebf 2127
dc5f3fef 2128 pr_info("Open vSwitch switching datapath %s, built "__DATE__" "__TIME__"\n",
8a07709c 2129 VERSION);
064af421 2130
850b6b3b 2131 err = ovs_flow_init();
3544358a 2132 if (err)
533e96e7 2133 goto error;
3544358a 2134
850b6b3b 2135 err = ovs_vport_init();
064af421
BP
2136 if (err)
2137 goto error_flow_exit;
2138
2a4999f3 2139 err = register_pernet_device(&ovs_net_ops);
f2459fe7
JG
2140 if (err)
2141 goto error_vport_exit;
2142
2a4999f3
PS
2143 err = register_netdevice_notifier(&ovs_dp_device_notifier);
2144 if (err)
2145 goto error_netns_exit;
2146
982b8810
BP
2147 err = dp_register_genl();
2148 if (err < 0)
37a1300c 2149 goto error_unreg_notifier;
982b8810 2150
064af421
BP
2151 return 0;
2152
2153error_unreg_notifier:
850b6b3b 2154 unregister_netdevice_notifier(&ovs_dp_device_notifier);
2a4999f3
PS
2155error_netns_exit:
2156 unregister_pernet_device(&ovs_net_ops);
f2459fe7 2157error_vport_exit:
850b6b3b 2158 ovs_vport_exit();
064af421 2159error_flow_exit:
850b6b3b 2160 ovs_flow_exit();
064af421
BP
2161error:
2162 return err;
2163}
2164
2165static void dp_cleanup(void)
2166{
982b8810 2167 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
850b6b3b 2168 unregister_netdevice_notifier(&ovs_dp_device_notifier);
2a4999f3
PS
2169 unregister_pernet_device(&ovs_net_ops);
2170 rcu_barrier();
850b6b3b
JG
2171 ovs_vport_exit();
2172 ovs_flow_exit();
064af421
BP
2173}
2174
2175module_init(dp_init);
2176module_exit(dp_cleanup);
2177
2178MODULE_DESCRIPTION("Open vSwitch switching datapath");
2179MODULE_LICENSE("GPL");
3d0666d2 2180MODULE_VERSION(VERSION);