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