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