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