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