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