]> git.proxmox.com Git - mirror_ovs.git/blame - datapath/datapath.c
datapath: do not update max_headroom if new headroom is equal to old headroom
[mirror_ovs.git] / datapath / datapath.c
CommitLineData
064af421 1/*
e23775f2 2 * Copyright (c) 2007-2015 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>
907c26a8 54#include <net/nsh.h>
064af421 55
064af421 56#include "datapath.h"
038e34ab 57#include "conntrack.h"
064af421 58#include "flow.h"
d103f479 59#include "flow_table.h"
a097c0b2 60#include "flow_netlink.h"
1cb57039 61#include "meter.h"
e23775f2 62#include "gso.h"
f2459fe7 63#include "vport-internal_dev.h"
d5de5b0d 64#include "vport-netdev.h"
064af421 65
f56f0b73 66unsigned int ovs_net_id __read_mostly;
2a4999f3 67
cb25142c
PS
68static struct genl_family dp_packet_genl_family;
69static struct genl_family dp_flow_genl_family;
70static struct genl_family dp_datapath_genl_family;
71
bc619e29
JS
72static const struct nla_policy flow_policy[];
73
e515e66a
GR
74static const struct genl_multicast_group ovs_dp_flow_multicast_group = {
75 .name = OVS_FLOW_MCGROUP,
cb25142c
PS
76};
77
e515e66a
GR
78static const struct genl_multicast_group ovs_dp_datapath_multicast_group = {
79 .name = OVS_DATAPATH_MCGROUP,
cb25142c
PS
80};
81
e515e66a
GR
82const struct genl_multicast_group ovs_dp_vport_multicast_group = {
83 .name = OVS_VPORT_MCGROUP,
cb25142c
PS
84};
85
afad3556 86/* Check if need to build a reply message.
af465b67
PS
87 * OVS userspace sets the NLM_F_ECHO flag if it needs the reply.
88 */
114fce23
SG
89static bool ovs_must_notify(struct genl_family *family, struct genl_info *info,
90 unsigned int group)
afad3556
JR
91{
92 return info->nlhdr->nlmsg_flags & NLM_F_ECHO ||
6233a1bd 93 genl_has_listeners(family, genl_info_net(info), group);
afad3556
JR
94}
95
e515e66a
GR
96static void ovs_notify(struct genl_family *family,
97 const struct genl_multicast_group *grp,
cb25142c 98 struct sk_buff *skb, struct genl_info *info)
e297c6b7 99{
0643a78b 100 genl_notify(family, skb, info, GROUP_ID(grp), GFP_KERNEL);
e297c6b7
TG
101}
102
ed099e92
BP
103/**
104 * DOC: Locking:
064af421 105 *
cd2a59e9
PS
106 * All writes e.g. Writes to device state (add/remove datapath, port, set
107 * operations on vports, etc.), Writes to other state (flow table
108 * modifications, set miscellaneous datapath parameters, etc.) are protected
109 * by ovs_lock.
ed099e92
BP
110 *
111 * Reads are protected by RCU.
112 *
113 * There are a few special cases (mostly stats) that have their own
114 * synchronization but they nest under all of above and don't interact with
115 * each other.
cd2a59e9
PS
116 *
117 * The RTNL lock nests inside ovs_mutex.
064af421 118 */
ed099e92 119
cd2a59e9
PS
120static DEFINE_MUTEX(ovs_mutex);
121
122void ovs_lock(void)
123{
124 mutex_lock(&ovs_mutex);
125}
126
127void ovs_unlock(void)
128{
129 mutex_unlock(&ovs_mutex);
130}
131
132#ifdef CONFIG_LOCKDEP
133int lockdep_ovsl_is_held(void)
134{
135 if (debug_locks)
136 return lockdep_is_held(&ovs_mutex);
137 else
138 return 1;
139}
140#endif
141
5ae440c3 142static int queue_gso_packets(struct datapath *dp, struct sk_buff *,
f1f60b85 143 const struct sw_flow_key *,
4c7804f1
WT
144 const struct dp_upcall_info *,
145 uint32_t cutlen);
5ae440c3 146static int queue_userspace_packet(struct datapath *dp, struct sk_buff *,
7d16c847 147 const struct sw_flow_key *,
4c7804f1
WT
148 const struct dp_upcall_info *,
149 uint32_t cutlen);
064af421 150
cd2a59e9 151/* Must be called with rcu_read_lock or ovs_mutex. */
850b6b3b 152const char *ovs_dp_name(const struct datapath *dp)
f2459fe7 153{
cd2a59e9 154 struct vport *vport = ovs_vport_ovsl_rcu(dp, OVSP_LOCAL);
e23775f2 155 return ovs_vport_name(vport);
f2459fe7
JG
156}
157
f1f60b85 158static int get_dpifindex(const struct datapath *dp)
99769a40
JG
159{
160 struct vport *local;
161 int ifindex;
162
163 rcu_read_lock();
164
95b1d73a 165 local = ovs_vport_rcu(dp, OVSP_LOCAL);
99769a40 166 if (local)
e23775f2 167 ifindex = local->dev->ifindex;
99769a40
JG
168 else
169 ifindex = 0;
170
171 rcu_read_unlock();
172
173 return ifindex;
174}
175
46c6a11d
JG
176static void destroy_dp_rcu(struct rcu_head *rcu)
177{
178 struct datapath *dp = container_of(rcu, struct datapath, rcu);
46c6a11d 179
e379e4d1 180 ovs_flow_tbl_destroy(&dp->table);
46c6a11d 181 free_percpu(dp->stats_percpu);
95b1d73a 182 kfree(dp->ports);
1cb57039 183 ovs_meters_exit(dp);
5ca1ba48 184 kfree(dp);
46c6a11d
JG
185}
186
95b1d73a
PS
187static struct hlist_head *vport_hash_bucket(const struct datapath *dp,
188 u16 port_no)
189{
190 return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)];
191}
192
aa917006 193/* Called with ovs_mutex or RCU read lock. */
95b1d73a
PS
194struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no)
195{
196 struct vport *vport;
95b1d73a
PS
197 struct hlist_head *head;
198
199 head = vport_hash_bucket(dp, port_no);
f8dfbcb7 200 hlist_for_each_entry_rcu(vport, head, dp_hash_node) {
95b1d73a
PS
201 if (vport->port_no == port_no)
202 return vport;
203 }
204 return NULL;
205}
206
cd2a59e9 207/* Called with ovs_mutex. */
c19e6535 208static struct vport *new_vport(const struct vport_parms *parms)
064af421 209{
f2459fe7 210 struct vport *vport;
f2459fe7 211
850b6b3b 212 vport = ovs_vport_add(parms);
c19e6535
BP
213 if (!IS_ERR(vport)) {
214 struct datapath *dp = parms->dp;
95b1d73a 215 struct hlist_head *head = vport_hash_bucket(dp, vport->port_no);
064af421 216
95b1d73a 217 hlist_add_head_rcu(&vport->dp_hash_node, head);
c19e6535 218 }
c19e6535 219 return vport;
064af421
BP
220}
221
850b6b3b 222void ovs_dp_detach_port(struct vport *p)
064af421 223{
cd2a59e9 224 ASSERT_OVSL();
064af421 225
064af421 226 /* First drop references to device. */
95b1d73a 227 hlist_del_rcu(&p->dp_hash_node);
f2459fe7 228
7237e4f4 229 /* Then destroy it. */
850b6b3b 230 ovs_vport_del(p);
064af421
BP
231}
232
fb66fbd1 233/* Must be called with rcu_read_lock. */
e74d4817 234void ovs_dp_process_packet(struct sk_buff *skb, struct sw_flow_key *key)
064af421 235{
a6059080 236 const struct vport *p = OVS_CB(skb)->input_vport;
064af421 237 struct datapath *dp = p->dp;
3544358a 238 struct sw_flow *flow;
ad50cb60 239 struct sw_flow_actions *sf_acts;
064af421 240 struct dp_stats_percpu *stats;
e9141eec 241 u64 *stats_counter;
4fa72a95 242 u32 n_mask_hit;
064af421 243
70dbc259 244 stats = this_cpu_ptr(dp->stats_percpu);
a063b0df 245
52a23d92 246 /* Look up flow. */
e74d4817 247 flow = ovs_flow_tbl_lookup_stats(&dp->table, key, skb_get_hash(skb),
5604935e 248 &n_mask_hit);
52a23d92
JG
249 if (unlikely(!flow)) {
250 struct dp_upcall_info upcall;
a7d607c5 251 int error;
52a23d92 252
0e469d3b 253 memset(&upcall, 0, sizeof(upcall));
52a23d92 254 upcall.cmd = OVS_PACKET_CMD_MISS;
beb1c69a 255 upcall.portid = ovs_vport_find_upcall_portid(p, skb);
a94ebc39 256 upcall.mru = OVS_CB(skb)->mru;
4c7804f1 257 error = ovs_dp_upcall(dp, skb, key, &upcall, 0);
a7d607c5
LR
258 if (unlikely(error))
259 kfree_skb(skb);
260 else
261 consume_skb(skb);
52a23d92
JG
262 stats_counter = &stats->n_missed;
263 goto out;
264 }
265
e74d4817 266 ovs_flow_stats_update(flow, key->tp.flags, skb);
ad50cb60 267 sf_acts = rcu_dereference(flow->sf_acts);
7d16c847
PS
268 ovs_execute_actions(dp, skb, sf_acts, key);
269
b0b906cc 270 stats_counter = &stats->n_hit;
55574bb0 271
8819fac7 272out:
55574bb0 273 /* Update datapath statistics. */
b81deb15 274 u64_stats_update_begin(&stats->syncp);
e9141eec 275 (*stats_counter)++;
4fa72a95 276 stats->n_mask_hit += n_mask_hit;
b81deb15 277 u64_stats_update_end(&stats->syncp);
064af421
BP
278}
279
850b6b3b 280int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
f1f60b85 281 const struct sw_flow_key *key,
4c7804f1
WT
282 const struct dp_upcall_info *upcall_info,
283 uint32_t cutlen)
aa5a8fdc
JG
284{
285 struct dp_stats_percpu *stats;
286 int err;
287
28aea917 288 if (upcall_info->portid == 0) {
b063d9f0 289 err = -ENOTCONN;
b063d9f0
JG
290 goto err;
291 }
292
7257b535 293 if (!skb_is_gso(skb))
4c7804f1 294 err = queue_userspace_packet(dp, skb, key, upcall_info, cutlen);
7257b535 295 else
4c7804f1 296 err = queue_gso_packets(dp, skb, key, upcall_info, cutlen);
d76195db
JG
297 if (err)
298 goto err;
299
300 return 0;
aa5a8fdc 301
aa5a8fdc 302err:
70dbc259 303 stats = this_cpu_ptr(dp->stats_percpu);
aa5a8fdc 304
b81deb15 305 u64_stats_update_begin(&stats->syncp);
aa5a8fdc 306 stats->n_lost++;
b81deb15 307 u64_stats_update_end(&stats->syncp);
aa5a8fdc 308
aa5a8fdc 309 return err;
982b8810
BP
310}
311
5ae440c3 312static int queue_gso_packets(struct datapath *dp, struct sk_buff *skb,
f1f60b85 313 const struct sw_flow_key *key,
4c7804f1
WT
314 const struct dp_upcall_info *upcall_info,
315 uint32_t cutlen)
cb5087ca 316{
969b8e6b 317#ifdef HAVE_SKB_GSO_UDP
f54a7a5d 318 unsigned int gso_type = skb_shinfo(skb)->gso_type;
7257b535 319 struct sw_flow_key later_key;
969b8e6b 320#endif
7257b535 321 struct sk_buff *segs, *nskb;
b2a23c4e 322 struct ovs_skb_cb ovs_cb;
7257b535 323 int err;
cb5087ca 324
b2a23c4e 325 ovs_cb = *OVS_CB(skb);
1d04cd4e 326 segs = __skb_gso_segment(skb, NETIF_F_SG, false);
b2a23c4e 327 *OVS_CB(skb) = ovs_cb;
79089764
PS
328 if (IS_ERR(segs))
329 return PTR_ERR(segs);
d1da7669
PS
330 if (segs == NULL)
331 return -EINVAL;
969b8e6b 332#ifdef HAVE_SKB_GSO_UDP
9b277b39 333 if (gso_type & SKB_GSO_UDP) {
c135bba1 334 /* The initial flow key extracted by ovs_flow_key_extract()
9b277b39
PS
335 * in this case is for a first fragment, so we need to
336 * properly mark later fragments.
337 */
e74d4817 338 later_key = *key;
9b277b39
PS
339 later_key.ip.frag = OVS_FRAG_TYPE_LATER;
340 }
969b8e6b 341#endif
7257b535
BP
342 /* Queue all of the segments. */
343 skb = segs;
cb5087ca 344 do {
b2a23c4e 345 *OVS_CB(skb) = ovs_cb;
969b8e6b 346#ifdef HAVE_SKB_GSO_UDP
9b277b39 347 if (gso_type & SKB_GSO_UDP && skb != segs)
e74d4817 348 key = &later_key;
969b8e6b 349#endif
4c7804f1 350 err = queue_userspace_packet(dp, skb, key, upcall_info, cutlen);
982b8810 351 if (err)
7257b535 352 break;
856081f6 353
36ce148c 354 } while ((skb = skb->next));
cb5087ca 355
7257b535
BP
356 /* Free all of the segments. */
357 skb = segs;
358 do {
359 nskb = skb->next;
360 if (err)
361 kfree_skb(skb);
362 else
363 consume_skb(skb);
364 } while ((skb = nskb));
365 return err;
366}
367
8b7ea2d4 368static size_t upcall_msg_size(const struct dp_upcall_info *upcall_info,
6b330a60 369 unsigned int hdrlen, int actions_attrlen)
0afa2373
TG
370{
371 size_t size = NLMSG_ALIGN(sizeof(struct ovs_header))
533bea51 372 + nla_total_size(hdrlen) /* OVS_PACKET_ATTR_PACKET */
039fb36c 373 + nla_total_size(ovs_key_attr_size()) /* OVS_PACKET_ATTR_KEY */
9ba57fc7
HZ
374 + nla_total_size(sizeof(unsigned int)) /* OVS_PACKET_ATTR_LEN */
375 + nla_total_size(sizeof(u64)); /* OVS_PACKET_ATTR_HASH */
0afa2373
TG
376
377 /* OVS_PACKET_ATTR_USERDATA */
8b7ea2d4
WZ
378 if (upcall_info->userdata)
379 size += NLA_ALIGN(upcall_info->userdata->nla_len);
380
381 /* OVS_PACKET_ATTR_EGRESS_TUN_KEY */
382 if (upcall_info->egress_tun_info)
383 size += nla_total_size(ovs_tun_key_attr_size());
0afa2373 384
0e469d3b
NM
385 /* OVS_PACKET_ATTR_ACTIONS */
386 if (upcall_info->actions_len)
6b330a60 387 size += nla_total_size(actions_attrlen);
0e469d3b 388
a94ebc39
JS
389 /* OVS_PACKET_ATTR_MRU */
390 if (upcall_info->mru)
391 size += nla_total_size(sizeof(upcall_info->mru));
392
0afa2373
TG
393 return size;
394}
395
a94ebc39
JS
396static void pad_packet(struct datapath *dp, struct sk_buff *skb)
397{
398 if (!(dp->user_features & OVS_DP_F_UNALIGNED)) {
399 size_t plen = NLA_ALIGN(skb->len) - skb->len;
400
401 if (plen > 0)
0ace0a26 402 skb_put_zero(skb, plen);
a94ebc39
JS
403 }
404}
405
5ae440c3 406static int queue_userspace_packet(struct datapath *dp, struct sk_buff *skb,
f1f60b85 407 const struct sw_flow_key *key,
4c7804f1
WT
408 const struct dp_upcall_info *upcall_info,
409 uint32_t cutlen)
7257b535
BP
410{
411 struct ovs_header *upcall;
6161d3fd 412 struct sk_buff *nskb = NULL;
82706a6f 413 struct sk_buff *user_skb = NULL; /* to be queued to userspace */
7257b535 414 struct nlattr *nla;
978188b2 415 size_t len;
533bea51 416 unsigned int hlen;
5ae440c3 417 int err, dp_ifindex;
9ba57fc7 418 u64 hash;
5ae440c3
TG
419
420 dp_ifindex = get_dpifindex(dp);
421 if (!dp_ifindex)
422 return -ENODEV;
7257b535 423
efd8a18e 424 if (skb_vlan_tag_present(skb)) {
6161d3fd
JG
425 nskb = skb_clone(skb, GFP_ATOMIC);
426 if (!nskb)
427 return -ENOMEM;
07ac71ea 428
8063e095 429 nskb = __vlan_hwaccel_push_inside(nskb);
07ac71ea
PS
430 if (!nskb)
431 return -ENOMEM;
432
6161d3fd
JG
433 skb = nskb;
434 }
435
436 if (nla_attr_size(skb->len) > USHRT_MAX) {
437 err = -EFBIG;
438 goto out;
439 }
7257b535 440
533bea51
TG
441 /* Complete checksum if needed */
442 if (skb->ip_summed == CHECKSUM_PARTIAL &&
a0c9fedc 443 (err = skb_csum_hwoffload_help(skb, 0)))
533bea51
TG
444 goto out;
445
446 /* Older versions of OVS user space enforce alignment of the last
447 * Netlink attribute to NLA_ALIGNTO which would require extensive
448 * padding logic. Only perform zerocopy if padding is not required.
449 */
450 if (dp->user_features & OVS_DP_F_UNALIGNED)
451 hlen = skb_zerocopy_headlen(skb);
452 else
453 hlen = skb->len;
454
6b330a60
GR
455 len = upcall_msg_size(upcall_info, hlen - cutlen,
456 OVS_CB(skb)->acts_origlen);
40c08cda 457 user_skb = genlmsg_new(len, GFP_ATOMIC);
6161d3fd
JG
458 if (!user_skb) {
459 err = -ENOMEM;
460 goto out;
461 }
7257b535
BP
462
463 upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
464 0, upcall_info->cmd);
f463bdc9
KL
465 if (!upcall) {
466 err = -EINVAL;
467 goto out;
468 }
7257b535
BP
469 upcall->dp_ifindex = dp_ifindex;
470
db7f2238 471 err = ovs_nla_put_key(key, key, OVS_PACKET_ATTR_KEY, false, user_skb);
5821a592
EC
472 if (err)
473 goto out;
7257b535
BP
474
475 if (upcall_info->userdata)
e995e3df 476 __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA,
462a988b 477 nla_len(upcall_info->userdata),
e995e3df 478 nla_data(upcall_info->userdata));
7257b535 479
e23775f2 480
8b7ea2d4 481 if (upcall_info->egress_tun_info) {
09c33996
YHW
482 nla = nla_nest_start_noflag(user_skb,
483 OVS_PACKET_ATTR_EGRESS_TUN_KEY);
eee4c8eb
KL
484 if (!nla) {
485 err = -EMSGSIZE;
486 goto out;
487 }
aad7cb91
PS
488 err = ovs_nla_put_tunnel_info(user_skb,
489 upcall_info->egress_tun_info);
5821a592
EC
490 if (err)
491 goto out;
492
8b7ea2d4
WZ
493 nla_nest_end(user_skb, nla);
494 }
495
0e469d3b 496 if (upcall_info->actions_len) {
09c33996 497 nla = nla_nest_start_noflag(user_skb, OVS_PACKET_ATTR_ACTIONS);
eee4c8eb
KL
498 if (!nla) {
499 err = -EMSGSIZE;
500 goto out;
501 }
0e469d3b
NM
502 err = ovs_nla_put_actions(upcall_info->actions,
503 upcall_info->actions_len,
504 user_skb);
505 if (!err)
506 nla_nest_end(user_skb, nla);
507 else
508 nla_nest_cancel(user_skb, nla);
509 }
510
a94ebc39
JS
511 /* Add OVS_PACKET_ATTR_MRU */
512 if (upcall_info->mru) {
513 if (nla_put_u16(user_skb, OVS_PACKET_ATTR_MRU,
514 upcall_info->mru)) {
515 err = -ENOBUFS;
516 goto out;
517 }
518 pad_packet(dp, user_skb);
519 }
520
039fb36c
WT
521 /* Add OVS_PACKET_ATTR_LEN when packet is truncated */
522 if (cutlen > 0) {
523 if (nla_put_u32(user_skb, OVS_PACKET_ATTR_LEN,
524 skb->len)) {
525 err = -ENOBUFS;
526 goto out;
527 }
528 pad_packet(dp, user_skb);
529 }
530
9ba57fc7
HZ
531 /* Add OVS_PACKET_ATTR_HASH */
532 hash = skb_get_hash_raw(skb);
533#ifdef HAVE_SW_HASH
534 if (skb->sw_hash)
535 hash |= OVS_PACKET_HASH_SW_BIT;
536#endif
537
538#ifdef HAVE_L4_RXHASH
539 if (skb->l4_rxhash)
540#else
541 if (skb->l4_hash)
542#endif
543 hash |= OVS_PACKET_HASH_L4_BIT;
544
545 if (nla_put(user_skb, OVS_PACKET_ATTR_HASH, sizeof (u64), &hash)) {
546 err = -ENOBUFS;
547 goto out;
548 }
549
533bea51 550 /* Only reserve room for attribute header, packet data is added
af465b67
PS
551 * in skb_zerocopy()
552 */
533bea51
TG
553 if (!(nla = nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, 0))) {
554 err = -ENOBUFS;
555 goto out;
556 }
4c7804f1 557 nla->nla_len = nla_attr_size(skb->len - cutlen);
bed53bd1 558
4c7804f1 559 err = skb_zerocopy(user_skb, skb, skb->len - cutlen, hlen);
2c272bd9
ZK
560 if (err)
561 goto out;
7257b535 562
ef507cec 563 /* Pad OVS_PACKET_ATTR_PACKET if linear copy was performed */
a94ebc39 564 pad_packet(dp, user_skb);
ef507cec 565
533bea51 566 ((struct nlmsghdr *) user_skb->data)->nlmsg_len = user_skb->len;
6161d3fd 567
533bea51 568 err = genlmsg_unicast(ovs_dp_get_net(dp), user_skb, upcall_info->portid);
82706a6f 569 user_skb = NULL;
6161d3fd 570out:
2c272bd9
ZK
571 if (err)
572 skb_tx_error(skb);
82706a6f 573 kfree_skb(user_skb);
6161d3fd
JG
574 kfree_skb(nskb);
575 return err;
cb5087ca
BP
576}
577
df2c07f4 578static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
064af421 579{
df2c07f4 580 struct ovs_header *ovs_header = info->userhdr;
a94ebc39 581 struct net *net = sock_net(skb->sk);
982b8810 582 struct nlattr **a = info->attrs;
e0e57990 583 struct sw_flow_actions *acts;
982b8810 584 struct sk_buff *packet;
e0e57990 585 struct sw_flow *flow;
ad50cb60 586 struct sw_flow_actions *sf_acts;
f7cd0081 587 struct datapath *dp;
a6059080 588 struct vport *input_vport;
a94ebc39 589 u16 mru = 0;
9ba57fc7 590 u64 hash;
3f19d399 591 int len;
d6569377 592 int err;
2e460098 593 bool log = !a[OVS_PACKET_ATTR_PROBE];
064af421 594
f7cd0081 595 err = -EINVAL;
df2c07f4 596 if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
7c3072cc 597 !a[OVS_PACKET_ATTR_ACTIONS])
e5cad958 598 goto err;
064af421 599
df2c07f4 600 len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
3f19d399 601 packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
f7cd0081
BP
602 err = -ENOMEM;
603 if (!packet)
e5cad958 604 goto err;
3f19d399
BP
605 skb_reserve(packet, NET_IP_ALIGN);
606
bf3d6fce 607 nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
8d5ebd83 608
a94ebc39
JS
609 /* Set packet's mru */
610 if (a[OVS_PACKET_ATTR_MRU]) {
611 mru = nla_get_u16(a[OVS_PACKET_ATTR_MRU]);
612 packet->ignore_df = 1;
613 }
614 OVS_CB(packet)->mru = mru;
615
9ba57fc7
HZ
616 if (a[OVS_PACKET_ATTR_HASH]) {
617 hash = nla_get_u64(a[OVS_PACKET_ATTR_HASH]);
618
619 __skb_set_hash(packet, hash & 0xFFFFFFFFULL,
620 !!(hash & OVS_PACKET_HASH_SW_BIT),
621 !!(hash & OVS_PACKET_HASH_L4_BIT));
622 }
623
e0e57990 624 /* Build an sw_flow for sending this packet. */
df65fec1 625 flow = ovs_flow_alloc();
e0e57990
BP
626 err = PTR_ERR(flow);
627 if (IS_ERR(flow))
e5cad958 628 goto err_kfree_skb;
064af421 629
038e34ab
JS
630 err = ovs_flow_key_extract_userspace(net, a[OVS_PACKET_ATTR_KEY],
631 packet, &flow->key, log);
e0e57990 632 if (err)
9321954a 633 goto err_flow_free;
e0e57990 634
a94ebc39 635 err = ovs_nla_copy_actions(net, a[OVS_PACKET_ATTR_ACTIONS],
9233cef7 636 &flow->key, &acts, log);
9b405f1a
PS
637 if (err)
638 goto err_flow_free;
e0e57990 639
ff27161e 640 rcu_assign_pointer(flow->sf_acts, acts);
abff858b 641 packet->priority = flow->key.phy.priority;
3025a772 642 packet->mark = flow->key.phy.skb_mark;
e0e57990 643
d6569377 644 rcu_read_lock();
a94ebc39 645 dp = get_dp_rcu(net, ovs_header->dp_ifindex);
f7cd0081 646 err = -ENODEV;
e5cad958
BP
647 if (!dp)
648 goto err_unlock;
cc4015df 649
a6059080
AZ
650 input_vport = ovs_vport_rcu(dp, flow->key.phy.in_port);
651 if (!input_vport)
652 input_vport = ovs_vport_rcu(dp, OVSP_LOCAL);
653
654 if (!input_vport)
655 goto err_unlock;
656
e23775f2 657 packet->dev = input_vport->dev;
a6059080 658 OVS_CB(packet)->input_vport = input_vport;
ad50cb60 659 sf_acts = rcu_dereference(flow->sf_acts);
a6059080 660
e9141eec 661 local_bh_disable();
7d16c847 662 err = ovs_execute_actions(dp, packet, sf_acts, &flow->key);
e9141eec 663 local_bh_enable();
d6569377 664 rcu_read_unlock();
e0e57990 665
a1c564be 666 ovs_flow_free(flow, false);
e5cad958 667 return err;
064af421 668
e5cad958
BP
669err_unlock:
670 rcu_read_unlock();
9321954a 671err_flow_free:
a1c564be 672 ovs_flow_free(flow, false);
e5cad958
BP
673err_kfree_skb:
674 kfree_skb(packet);
675err:
d6569377 676 return err;
064af421
BP
677}
678
df2c07f4 679static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
7c3072cc 680 [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
df2c07f4
JP
681 [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
682 [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
2e460098 683 [OVS_PACKET_ATTR_PROBE] = { .type = NLA_FLAG },
a94ebc39 684 [OVS_PACKET_ATTR_MRU] = { .type = NLA_U16 },
9ba57fc7 685 [OVS_PACKET_ATTR_HASH] = { .type = NLA_U64 },
982b8810
BP
686};
687
18fd3a52 688static struct genl_ops dp_packet_genl_ops[] = {
df2c07f4 689 { .cmd = OVS_PACKET_CMD_EXECUTE,
f1e9590e
YHW
690#ifdef HAVE_GENL_VALIDATE_FLAGS
691 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
692#endif
a6a8674d 693 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2ef0f1c2 694#ifdef HAVE_GENL_OPS_POLICY
982b8810 695 .policy = packet_policy,
2ef0f1c2 696#endif
df2c07f4 697 .doit = ovs_packet_cmd_execute
982b8810
BP
698 }
699};
700
ba63fe26 701static struct genl_family dp_packet_genl_family __ro_after_init = {
cb25142c
PS
702 .hdrsize = sizeof(struct ovs_header),
703 .name = OVS_PACKET_FAMILY,
704 .version = OVS_PACKET_VERSION,
705 .maxattr = OVS_PACKET_ATTR_MAX,
2ef0f1c2
JB
706#ifndef HAVE_GENL_OPS_POLICY
707 .policy = packet_policy,
708#endif
cb25142c
PS
709 .netnsok = true,
710 .parallel_ops = true,
711 .ops = dp_packet_genl_ops,
712 .n_ops = ARRAY_SIZE(dp_packet_genl_ops),
ba63fe26 713 .module = THIS_MODULE,
cb25142c
PS
714};
715
f1f60b85 716static void get_dp_stats(const struct datapath *dp, struct ovs_dp_stats *stats,
4fa72a95 717 struct ovs_dp_megaflow_stats *mega_stats)
064af421 718{
d6569377 719 int i;
f180c2e2 720
4fa72a95
AZ
721 memset(mega_stats, 0, sizeof(*mega_stats));
722
994dc286 723 stats->n_flows = ovs_flow_tbl_count(&dp->table);
4fa72a95 724 mega_stats->n_masks = ovs_flow_tbl_num_masks(&dp->table);
064af421 725
7257b535 726 stats->n_hit = stats->n_missed = stats->n_lost = 0;
4fa72a95 727
d6569377
BP
728 for_each_possible_cpu(i) {
729 const struct dp_stats_percpu *percpu_stats;
730 struct dp_stats_percpu local_stats;
821cb9fa 731 unsigned int start;
44e05eca 732
d6569377 733 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
064af421 734
d6569377 735 do {
b81deb15 736 start = u64_stats_fetch_begin_irq(&percpu_stats->syncp);
d6569377 737 local_stats = *percpu_stats;
b81deb15 738 } while (u64_stats_fetch_retry_irq(&percpu_stats->syncp, start));
064af421 739
d6569377
BP
740 stats->n_hit += local_stats.n_hit;
741 stats->n_missed += local_stats.n_missed;
742 stats->n_lost += local_stats.n_lost;
4fa72a95 743 mega_stats->n_mask_hit += local_stats.n_mask_hit;
d6569377
BP
744 }
745}
064af421 746
bc619e29
JS
747static bool should_fill_key(const struct sw_flow_id *sfid, uint32_t ufid_flags)
748{
749 return ovs_identifier_is_ufid(sfid) &&
750 !(ufid_flags & OVS_UFID_F_OMIT_KEY);
751}
752
753static bool should_fill_mask(uint32_t ufid_flags)
754{
755 return !(ufid_flags & OVS_UFID_F_OMIT_MASK);
756}
757
758static bool should_fill_actions(uint32_t ufid_flags)
0afa2373 759{
bc619e29
JS
760 return !(ufid_flags & OVS_UFID_F_OMIT_ACTIONS);
761}
762
763static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts,
764 const struct sw_flow_id *sfid,
765 uint32_t ufid_flags)
766{
767 size_t len = NLMSG_ALIGN(sizeof(struct ovs_header));
768
769 /* OVS_FLOW_ATTR_UFID */
770 if (sfid && ovs_identifier_is_ufid(sfid))
771 len += nla_total_size(sfid->ufid_len);
772
773 /* OVS_FLOW_ATTR_KEY */
774 if (!sfid || should_fill_key(sfid, ufid_flags))
775 len += nla_total_size(ovs_key_attr_size());
776
777 /* OVS_FLOW_ATTR_MASK */
778 if (should_fill_mask(ufid_flags))
779 len += nla_total_size(ovs_key_attr_size());
780
781 /* OVS_FLOW_ATTR_ACTIONS */
782 if (should_fill_actions(ufid_flags))
c3bb15b3 783 len += nla_total_size(acts->orig_len);
bc619e29
JS
784
785 return len
91b37647 786 + nla_total_size_64bit(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
0afa2373 787 + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
91b37647 788 + nla_total_size_64bit(8); /* OVS_FLOW_ATTR_USED */
0afa2373
TG
789}
790
f1948bb9
JS
791/* Called with ovs_mutex or RCU read lock. */
792static int ovs_flow_cmd_fill_stats(const struct sw_flow *flow,
793 struct sk_buff *skb)
794{
795 struct ovs_flow_stats stats;
796 __be16 tcp_flags;
797 unsigned long used;
798
b0f3a2fe 799 ovs_flow_stats_get(flow, &stats, &used, &tcp_flags);
f71db6b1 800
b0f3a2fe 801 if (used &&
89be7da8
PS
802 nla_put_u64_64bit(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used),
803 OVS_FLOW_ATTR_PAD))
f1948bb9 804 return -EMSGSIZE;
d6569377 805
b0f3a2fe 806 if (stats.n_packets &&
91b37647
PS
807 nla_put_64bit(skb, OVS_FLOW_ATTR_STATS,
808 sizeof(struct ovs_flow_stats), &stats,
809 OVS_FLOW_ATTR_PAD))
f1948bb9 810 return -EMSGSIZE;
b0b906cc 811
b0f3a2fe
PS
812 if ((u8)ntohs(tcp_flags) &&
813 nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, (u8)ntohs(tcp_flags)))
f1948bb9
JS
814 return -EMSGSIZE;
815
816 return 0;
817}
818
819/* Called with ovs_mutex or RCU read lock. */
820static int ovs_flow_cmd_fill_actions(const struct sw_flow *flow,
821 struct sk_buff *skb, int skb_orig_len)
822{
823 struct nlattr *start;
824 int err;
d6569377 825
df2c07f4 826 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
30053024
BP
827 * this is the first flow to be dumped into 'skb'. This is unusual for
828 * Netlink but individual action lists can be longer than
829 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
830 * The userspace caller can always fetch the actions separately if it
831 * really wants them. (Most userspace callers in fact don't care.)
832 *
833 * This can only fail for dump operations because the skb is always
834 * properly sized for single flows.
835 */
09c33996 836 start = nla_nest_start_noflag(skb, OVS_FLOW_ATTR_ACTIONS);
f6f481ee 837 if (start) {
f44ccce1
PS
838 const struct sw_flow_actions *sf_acts;
839
780ec6ae 840 sf_acts = rcu_dereference_ovsl(flow->sf_acts);
a097c0b2
PS
841 err = ovs_nla_put_actions(sf_acts->actions,
842 sf_acts->actions_len, skb);
f71db6b1 843
0a25b039
BP
844 if (!err)
845 nla_nest_end(skb, start);
846 else {
847 if (skb_orig_len)
f1948bb9 848 return err;
0a25b039
BP
849
850 nla_nest_cancel(skb, start);
851 }
f1948bb9
JS
852 } else if (skb_orig_len) {
853 return -EMSGSIZE;
854 }
855
856 return 0;
857}
858
859/* Called with ovs_mutex or RCU read lock. */
2c622e5a 860static int ovs_flow_cmd_fill_info(const struct sw_flow *flow, int dp_ifindex,
f1948bb9 861 struct sk_buff *skb, u32 portid,
bc619e29 862 u32 seq, u32 flags, u8 cmd, u32 ufid_flags)
f1948bb9
JS
863{
864 const int skb_orig_len = skb->len;
865 struct ovs_header *ovs_header;
866 int err;
867
7d16c847
PS
868 ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family,
869 flags, cmd);
f1948bb9
JS
870 if (!ovs_header)
871 return -EMSGSIZE;
7d16c847 872
f1948bb9
JS
873 ovs_header->dp_ifindex = dp_ifindex;
874
bc619e29 875 err = ovs_nla_put_identifier(flow, skb);
db7f2238
JS
876 if (err)
877 goto error;
878
bc619e29
JS
879 if (should_fill_key(&flow->id, ufid_flags)) {
880 err = ovs_nla_put_masked_key(flow, skb);
881 if (err)
882 goto error;
883 }
884
885 if (should_fill_mask(ufid_flags)) {
886 err = ovs_nla_put_mask(flow, skb);
887 if (err)
888 goto error;
889 }
f1948bb9
JS
890
891 err = ovs_flow_cmd_fill_stats(flow, skb);
892 if (err)
893 goto error;
894
bc619e29
JS
895 if (should_fill_actions(ufid_flags)) {
896 err = ovs_flow_cmd_fill_actions(flow, skb, skb_orig_len);
897 if (err)
898 goto error;
899 }
37a1300c 900
23b48dc1
TG
901 genlmsg_end(skb, ovs_header);
902 return 0;
d6569377 903
37a1300c 904error:
df2c07f4 905 genlmsg_cancel(skb, ovs_header);
d6569377 906 return err;
44e05eca
BP
907}
908
f71db6b1
JR
909/* May not be called with RCU read lock. */
910static struct sk_buff *ovs_flow_cmd_alloc_info(const struct sw_flow_actions *acts,
bc619e29 911 const struct sw_flow_id *sfid,
afad3556 912 struct genl_info *info,
bc619e29
JS
913 bool always,
914 uint32_t ufid_flags)
44e05eca 915{
afad3556 916 struct sk_buff *skb;
bc619e29 917 size_t len;
d6569377 918
114fce23
SG
919 if (!always && !ovs_must_notify(&dp_flow_genl_family, info,
920 GROUP_ID(&ovs_dp_flow_multicast_group)))
afad3556
JR
921 return NULL;
922
bc619e29 923 len = ovs_flow_cmd_msg_size(acts, sfid, ufid_flags);
40c08cda 924 skb = genlmsg_new(len, GFP_KERNEL);
afad3556
JR
925 if (!skb)
926 return ERR_PTR(-ENOMEM);
927
928 return skb;
37a1300c 929}
8d5ebd83 930
f71db6b1 931/* Called with ovs_mutex. */
7d16c847 932static struct sk_buff *ovs_flow_cmd_build_info(const struct sw_flow *flow,
f71db6b1
JR
933 int dp_ifindex,
934 struct genl_info *info, u8 cmd,
bc619e29 935 bool always, u32 ufid_flags)
37a1300c
BP
936{
937 struct sk_buff *skb;
938 int retval;
d6569377 939
bc619e29
JS
940 skb = ovs_flow_cmd_alloc_info(ovsl_dereference(flow->sf_acts),
941 &flow->id, info, always, ufid_flags);
a6ddcc9a 942 if (IS_ERR_OR_NULL(skb))
afad3556 943 return skb;
d6569377 944
2c622e5a 945 retval = ovs_flow_cmd_fill_info(flow, dp_ifindex, skb,
f71db6b1 946 info->snd_portid, info->snd_seq, 0,
bc619e29 947 cmd, ufid_flags);
37a1300c 948 BUG_ON(retval < 0);
d6569377 949 return skb;
064af421
BP
950}
951
0c9fd022 952static int ovs_flow_cmd_new(struct sk_buff *skb, struct genl_info *info)
064af421 953{
a94ebc39 954 struct net *net = sock_net(skb->sk);
37a1300c 955 struct nlattr **a = info->attrs;
df2c07f4 956 struct ovs_header *ovs_header = info->userhdr;
bc619e29 957 struct sw_flow *flow = NULL, *new_flow;
a1c564be 958 struct sw_flow_mask mask;
37a1300c 959 struct sk_buff *reply;
9c52546b 960 struct datapath *dp;
0c9fd022 961 struct sw_flow_actions *acts;
a1c564be 962 struct sw_flow_match match;
bc619e29 963 u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
bc4a05c6 964 int error;
9233cef7 965 bool log = !a[OVS_FLOW_ATTR_PROBE];
064af421 966
6740b721 967 /* Must have key and actions. */
37a1300c 968 error = -EINVAL;
a473df5b 969 if (!a[OVS_FLOW_ATTR_KEY]) {
7d16c847 970 OVS_NLERR(log, "Flow key attr not present in new flow.");
37a1300c 971 goto error;
a473df5b
JG
972 }
973 if (!a[OVS_FLOW_ATTR_ACTIONS]) {
7d16c847 974 OVS_NLERR(log, "Flow actions attr not present in new flow.");
6740b721 975 goto error;
a473df5b 976 }
a1c564be 977
6740b721 978 /* Most of the time we need to allocate a new flow, do it before
af465b67
PS
979 * locking.
980 */
6740b721
JR
981 new_flow = ovs_flow_alloc();
982 if (IS_ERR(new_flow)) {
983 error = PTR_ERR(new_flow);
984 goto error;
985 }
986
987 /* Extract key. */
9b94fa6c 988 ovs_match_init(&match, &new_flow->key, false, &mask);
038e34ab 989 error = ovs_nla_get_match(net, &match, a[OVS_FLOW_ATTR_KEY],
9233cef7 990 a[OVS_FLOW_ATTR_MASK], log);
37a1300c 991 if (error)
6740b721 992 goto err_kfree_flow;
064af421 993
bc619e29
JS
994 /* Extract flow identifier. */
995 error = ovs_nla_get_identifier(&new_flow->id, a[OVS_FLOW_ATTR_UFID],
1d334d4f 996 &new_flow->key, log);
bc619e29
JS
997 if (error)
998 goto err_kfree_flow;
9b405f1a 999
1d334d4f 1000 /* unmasked key is needed to match when ufid is not used. */
1001 if (ovs_identifier_is_key(&new_flow->id))
1002 match.key = new_flow->id.unmasked_key;
1003
1004 ovs_flow_mask_key(&new_flow->key, &new_flow->key, true, &mask);
1005
6740b721 1006 /* Validate actions. */
a94ebc39
JS
1007 error = ovs_nla_copy_actions(net, a[OVS_FLOW_ATTR_ACTIONS],
1008 &new_flow->key, &acts, log);
0c9fd022 1009 if (error) {
7d16c847 1010 OVS_NLERR(log, "Flow actions may not be safe on all matching packets.");
4f67b12a 1011 goto err_kfree_flow;
6740b721
JR
1012 }
1013
bc619e29
JS
1014 reply = ovs_flow_cmd_alloc_info(acts, &new_flow->id, info, false,
1015 ufid_flags);
6740b721
JR
1016 if (IS_ERR(reply)) {
1017 error = PTR_ERR(reply);
1018 goto err_kfree_acts;
37a1300c
BP
1019 }
1020
cd2a59e9 1021 ovs_lock();
a94ebc39 1022 dp = get_dp(net, ovs_header->dp_ifindex);
6740b721
JR
1023 if (unlikely(!dp)) {
1024 error = -ENODEV;
cd2a59e9 1025 goto err_unlock_ovs;
6740b721 1026 }
bc619e29 1027
a1c564be 1028 /* Check if this is a duplicate flow */
bc619e29
JS
1029 if (ovs_identifier_is_ufid(&new_flow->id))
1030 flow = ovs_flow_tbl_lookup_ufid(&dp->table, &new_flow->id);
1031 if (!flow)
1d334d4f 1032 flow = ovs_flow_tbl_lookup(&dp->table, &new_flow->key);
6740b721
JR
1033 if (likely(!flow)) {
1034 rcu_assign_pointer(new_flow->sf_acts, acts);
d6569377 1035
d6569377 1036 /* Put flow in bucket. */
6740b721
JR
1037 error = ovs_flow_tbl_insert(&dp->table, new_flow, &mask);
1038 if (unlikely(error)) {
0585f7a8 1039 acts = NULL;
6740b721
JR
1040 goto err_unlock_ovs;
1041 }
1042
1043 if (unlikely(reply)) {
2c622e5a 1044 error = ovs_flow_cmd_fill_info(new_flow,
6740b721
JR
1045 ovs_header->dp_ifindex,
1046 reply, info->snd_portid,
1047 info->snd_seq, 0,
bc619e29
JS
1048 OVS_FLOW_CMD_NEW,
1049 ufid_flags);
6740b721 1050 BUG_ON(error < 0);
0585f7a8 1051 }
6740b721 1052 ovs_unlock();
d6569377 1053 } else {
0c9fd022
JR
1054 struct sw_flow_actions *old_acts;
1055
d6569377
BP
1056 /* Bail out if we're not allowed to modify an existing flow.
1057 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
1058 * because Generic Netlink treats the latter as a dump
1059 * request. We also accept NLM_F_EXCL in case that bug ever
1060 * gets fixed.
1061 */
6740b721
JR
1062 if (unlikely(info->nlhdr->nlmsg_flags & (NLM_F_CREATE
1063 | NLM_F_EXCL))) {
1064 error = -EEXIST;
cd2a59e9 1065 goto err_unlock_ovs;
6740b721 1066 }
bc619e29
JS
1067 /* The flow identifier has to be the same for flow updates.
1068 * Look for any overlapping flow.
1069 */
1070 if (unlikely(!ovs_flow_cmp(flow, &match))) {
1071 if (ovs_identifier_is_key(&flow->id))
1072 flow = ovs_flow_tbl_lookup_exact(&dp->table,
1073 &match);
1074 else /* UFID matches but key is different */
1075 flow = NULL;
3440e4bc
AW
1076 if (!flow) {
1077 error = -ENOENT;
1078 goto err_unlock_ovs;
1079 }
6740b721 1080 }
0c9fd022
JR
1081 /* Update actions. */
1082 old_acts = ovsl_dereference(flow->sf_acts);
1083 rcu_assign_pointer(flow->sf_acts, acts);
0c9fd022 1084
6740b721 1085 if (unlikely(reply)) {
2c622e5a 1086 error = ovs_flow_cmd_fill_info(flow,
6740b721
JR
1087 ovs_header->dp_ifindex,
1088 reply, info->snd_portid,
1089 info->snd_seq, 0,
bc619e29
JS
1090 OVS_FLOW_CMD_NEW,
1091 ufid_flags);
6740b721
JR
1092 BUG_ON(error < 0);
1093 }
1094 ovs_unlock();
0c9fd022 1095
e23775f2 1096 ovs_nla_free_flow_actions_rcu(old_acts);
6740b721 1097 ovs_flow_free(new_flow, false);
0c9fd022 1098 }
6740b721
JR
1099
1100 if (reply)
cb25142c 1101 ovs_notify(&dp_flow_genl_family, &ovs_dp_flow_multicast_group, reply, info);
0c9fd022
JR
1102 return 0;
1103
0c9fd022
JR
1104err_unlock_ovs:
1105 ovs_unlock();
6740b721
JR
1106 kfree_skb(reply);
1107err_kfree_acts:
e23775f2 1108 ovs_nla_free_flow_actions(acts);
6740b721
JR
1109err_kfree_flow:
1110 ovs_flow_free(new_flow, false);
0c9fd022
JR
1111error:
1112 return error;
1113}
1114
cc561abf 1115/* Factor out action copy to avoid "Wframe-larger-than=1024" warning. */
19b6110d 1116static noinline_for_stack struct sw_flow_actions *get_flow_actions(struct net *net,
a94ebc39 1117 const struct nlattr *a,
cc561abf 1118 const struct sw_flow_key *key,
9233cef7
JR
1119 const struct sw_flow_mask *mask,
1120 bool log)
cc561abf
PS
1121{
1122 struct sw_flow_actions *acts;
1123 struct sw_flow_key masked_key;
1124 int error;
1125
ad4adec2 1126 ovs_flow_mask_key(&masked_key, key, true, mask);
a94ebc39 1127 error = ovs_nla_copy_actions(net, a, &masked_key, &acts, log);
cc561abf 1128 if (error) {
9233cef7 1129 OVS_NLERR(log,
7d16c847 1130 "Actions may not be safe on all matching packets");
cc561abf
PS
1131 return ERR_PTR(error);
1132 }
1133
1134 return acts;
1135}
1136
850c2a4d
TZ
1137/* Factor out match-init and action-copy to avoid
1138 * "Wframe-larger-than=1024" warning. Because mask is only
1139 * used to get actions, we new a function to save some
1140 * stack space.
1141 *
1142 * If there are not key and action attrs, we return 0
1143 * directly. In the case, the caller will also not use the
1144 * match as before. If there is action attr, we try to get
1145 * actions and save them to *acts. Before returning from
1146 * the function, we reset the match->mask pointer. Because
1147 * we should not to return match object with dangling reference
1148 * to mask.
1149 * */
19b6110d
AB
1150static noinline_for_stack int
1151ovs_nla_init_match_and_action(struct net *net,
1152 struct sw_flow_match *match,
1153 struct sw_flow_key *key,
1154 struct nlattr **a,
1155 struct sw_flow_actions **acts,
1156 bool log)
850c2a4d
TZ
1157{
1158 struct sw_flow_mask mask;
1159 int error = 0;
1160
1161 if (a[OVS_FLOW_ATTR_KEY]) {
1162 ovs_match_init(match, key, true, &mask);
1163 error = ovs_nla_get_match(net, match, a[OVS_FLOW_ATTR_KEY],
1164 a[OVS_FLOW_ATTR_MASK], log);
1165 if (error)
1166 goto error;
1167 }
1168
1169 if (a[OVS_FLOW_ATTR_ACTIONS]) {
1170 if (!a[OVS_FLOW_ATTR_KEY]) {
1171 OVS_NLERR(log,
1172 "Flow key attribute not present in set flow.");
497895ca
CJ
1173 error = -EINVAL;
1174 goto error;
850c2a4d
TZ
1175 }
1176
1177 *acts = get_flow_actions(net, a[OVS_FLOW_ATTR_ACTIONS], key,
1178 &mask, log);
1179 if (IS_ERR(*acts)) {
1180 error = PTR_ERR(*acts);
1181 goto error;
1182 }
1183 }
1184
1185 /* On success, error is 0. */
1186error:
1187 match->mask = NULL;
1188 return error;
1189}
1190
0c9fd022
JR
1191static int ovs_flow_cmd_set(struct sk_buff *skb, struct genl_info *info)
1192{
a94ebc39 1193 struct net *net = sock_net(skb->sk);
0c9fd022
JR
1194 struct nlattr **a = info->attrs;
1195 struct ovs_header *ovs_header = info->userhdr;
1d2a1b5f 1196 struct sw_flow_key key;
0c9fd022 1197 struct sw_flow *flow;
0c9fd022
JR
1198 struct sk_buff *reply = NULL;
1199 struct datapath *dp;
6740b721 1200 struct sw_flow_actions *old_acts = NULL, *acts = NULL;
0c9fd022 1201 struct sw_flow_match match;
bc619e29
JS
1202 struct sw_flow_id sfid;
1203 u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
b24baa1a 1204 int error = 0;
9233cef7 1205 bool log = !a[OVS_FLOW_ATTR_PROBE];
bc619e29 1206 bool ufid_present;
0c9fd022 1207
bc619e29 1208 ufid_present = ovs_nla_get_ufid(&sfid, a[OVS_FLOW_ATTR_UFID], log);
850c2a4d 1209 if (!a[OVS_FLOW_ATTR_KEY] && !ufid_present) {
b24baa1a
PS
1210 OVS_NLERR(log,
1211 "Flow set message rejected, Key attribute missing.");
850c2a4d 1212 return -EINVAL;
b24baa1a 1213 }
850c2a4d
TZ
1214
1215 error = ovs_nla_init_match_and_action(net, &match, &key, a,
1216 &acts, log);
0c9fd022
JR
1217 if (error)
1218 goto error;
d6569377 1219
850c2a4d 1220 if (acts) {
ff27161e 1221 /* Can allocate before locking if have acts. */
bc619e29
JS
1222 reply = ovs_flow_cmd_alloc_info(acts, &sfid, info, false,
1223 ufid_flags);
6740b721
JR
1224 if (IS_ERR(reply)) {
1225 error = PTR_ERR(reply);
1226 goto err_kfree_acts;
90b8c2f7 1227 }
0c9fd022
JR
1228 }
1229
1230 ovs_lock();
a94ebc39 1231 dp = get_dp(net, ovs_header->dp_ifindex);
6740b721
JR
1232 if (unlikely(!dp)) {
1233 error = -ENODEV;
0c9fd022 1234 goto err_unlock_ovs;
6740b721 1235 }
0c9fd022 1236 /* Check that the flow exists. */
bc619e29
JS
1237 if (ufid_present)
1238 flow = ovs_flow_tbl_lookup_ufid(&dp->table, &sfid);
1239 else
1240 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
6740b721
JR
1241 if (unlikely(!flow)) {
1242 error = -ENOENT;
0c9fd022 1243 goto err_unlock_ovs;
6740b721 1244 }
3440e4bc 1245
0c9fd022 1246 /* Update actions, if present. */
6740b721 1247 if (likely(acts)) {
0c9fd022
JR
1248 old_acts = ovsl_dereference(flow->sf_acts);
1249 rcu_assign_pointer(flow->sf_acts, acts);
6740b721
JR
1250
1251 if (unlikely(reply)) {
2c622e5a 1252 error = ovs_flow_cmd_fill_info(flow,
6740b721
JR
1253 ovs_header->dp_ifindex,
1254 reply, info->snd_portid,
1255 info->snd_seq, 0,
229486c2 1256 OVS_FLOW_CMD_SET,
bc619e29 1257 ufid_flags);
6740b721
JR
1258 BUG_ON(error < 0);
1259 }
1260 } else {
1261 /* Could not alloc without acts before locking. */
7d16c847 1262 reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex,
229486c2 1263 info, OVS_FLOW_CMD_SET, false,
bc619e29
JS
1264 ufid_flags);
1265
6740b721
JR
1266 if (unlikely(IS_ERR(reply))) {
1267 error = PTR_ERR(reply);
1268 goto err_unlock_ovs;
1269 }
9c52546b 1270 }
0c9fd022 1271
0c9fd022
JR
1272 /* Clear stats. */
1273 if (a[OVS_FLOW_ATTR_CLEAR])
1274 ovs_flow_stats_clear(flow);
cd2a59e9 1275 ovs_unlock();
37a1300c 1276
6740b721 1277 if (reply)
cb25142c 1278 ovs_notify(&dp_flow_genl_family, &ovs_dp_flow_multicast_group, reply, info);
6740b721 1279 if (old_acts)
e23775f2 1280 ovs_nla_free_flow_actions_rcu(old_acts);
7d16c847 1281
d6569377 1282 return 0;
704a1e09 1283
cd2a59e9
PS
1284err_unlock_ovs:
1285 ovs_unlock();
6740b721
JR
1286 kfree_skb(reply);
1287err_kfree_acts:
e23775f2 1288 ovs_nla_free_flow_actions(acts);
37a1300c 1289error:
9c52546b 1290 return error;
704a1e09
BP
1291}
1292
df2c07f4 1293static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
704a1e09 1294{
37a1300c 1295 struct nlattr **a = info->attrs;
df2c07f4 1296 struct ovs_header *ovs_header = info->userhdr;
038e34ab 1297 struct net *net = sock_net(skb->sk);
37a1300c 1298 struct sw_flow_key key;
37a1300c 1299 struct sk_buff *reply;
704a1e09 1300 struct sw_flow *flow;
9c52546b 1301 struct datapath *dp;
a1c564be 1302 struct sw_flow_match match;
bc619e29
JS
1303 struct sw_flow_id ufid;
1304 u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
1305 int err = 0;
9233cef7 1306 bool log = !a[OVS_FLOW_ATTR_PROBE];
bc619e29 1307 bool ufid_present;
704a1e09 1308
bc619e29
JS
1309 ufid_present = ovs_nla_get_ufid(&ufid, a[OVS_FLOW_ATTR_UFID], log);
1310 if (a[OVS_FLOW_ATTR_KEY]) {
9b94fa6c 1311 ovs_match_init(&match, &key, true, NULL);
038e34ab 1312 err = ovs_nla_get_match(net, &match, a[OVS_FLOW_ATTR_KEY], NULL,
bc619e29
JS
1313 log);
1314 } else if (!ufid_present) {
9233cef7
JR
1315 OVS_NLERR(log,
1316 "Flow get message rejected, Key attribute missing.");
bc619e29 1317 err = -EINVAL;
1b936472 1318 }
37a1300c
BP
1319 if (err)
1320 return err;
704a1e09 1321
cd2a59e9 1322 ovs_lock();
2a4999f3 1323 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
cd2a59e9
PS
1324 if (!dp) {
1325 err = -ENODEV;
1326 goto unlock;
1327 }
704a1e09 1328
bc619e29
JS
1329 if (ufid_present)
1330 flow = ovs_flow_tbl_lookup_ufid(&dp->table, &ufid);
1331 else
1332 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
3440e4bc 1333 if (!flow) {
cd2a59e9
PS
1334 err = -ENOENT;
1335 goto unlock;
1336 }
d6569377 1337
7d16c847 1338 reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex, info,
229486c2 1339 OVS_FLOW_CMD_GET, true, ufid_flags);
cd2a59e9
PS
1340 if (IS_ERR(reply)) {
1341 err = PTR_ERR(reply);
1342 goto unlock;
1343 }
36956a7d 1344
cd2a59e9 1345 ovs_unlock();
37a1300c 1346 return genlmsg_reply(reply, info);
cd2a59e9
PS
1347unlock:
1348 ovs_unlock();
1349 return err;
d6569377 1350}
9c52546b 1351
df2c07f4 1352static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
d6569377 1353{
37a1300c 1354 struct nlattr **a = info->attrs;
df2c07f4 1355 struct ovs_header *ovs_header = info->userhdr;
038e34ab 1356 struct net *net = sock_net(skb->sk);
37a1300c 1357 struct sw_flow_key key;
37a1300c 1358 struct sk_buff *reply;
bc619e29 1359 struct sw_flow *flow = NULL;
d6569377 1360 struct datapath *dp;
a1c564be 1361 struct sw_flow_match match;
bc619e29
JS
1362 struct sw_flow_id ufid;
1363 u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
d6569377 1364 int err;
9233cef7 1365 bool log = !a[OVS_FLOW_ATTR_PROBE];
bc619e29 1366 bool ufid_present;
36956a7d 1367
bc619e29
JS
1368 ufid_present = ovs_nla_get_ufid(&ufid, a[OVS_FLOW_ATTR_UFID], log);
1369 if (a[OVS_FLOW_ATTR_KEY]) {
9b94fa6c 1370 ovs_match_init(&match, &key, true, NULL);
038e34ab
JS
1371 err = ovs_nla_get_match(net, &match, a[OVS_FLOW_ATTR_KEY],
1372 NULL, log);
cde7f3ba
JR
1373 if (unlikely(err))
1374 return err;
1375 }
1376
cd2a59e9 1377 ovs_lock();
2a4999f3 1378 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
cde7f3ba 1379 if (unlikely(!dp)) {
cd2a59e9
PS
1380 err = -ENODEV;
1381 goto unlock;
1382 }
7d16c847 1383
bc619e29 1384 if (unlikely(!a[OVS_FLOW_ATTR_KEY] && !ufid_present)) {
994dc286 1385 err = ovs_flow_tbl_flush(&dp->table);
cd2a59e9
PS
1386 goto unlock;
1387 }
7d16c847 1388
bc619e29
JS
1389 if (ufid_present)
1390 flow = ovs_flow_tbl_lookup_ufid(&dp->table, &ufid);
1391 else
1392 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
3440e4bc 1393 if (unlikely(!flow)) {
cd2a59e9
PS
1394 err = -ENOENT;
1395 goto unlock;
1396 }
d6569377 1397
994dc286 1398 ovs_flow_tbl_remove(&dp->table, flow);
cde7f3ba 1399 ovs_unlock();
37a1300c 1400
46051cf8 1401 reply = ovs_flow_cmd_alloc_info(rcu_dereference_raw(flow->sf_acts),
bc619e29 1402 &flow->id, info, false, ufid_flags);
cde7f3ba
JR
1403
1404 if (likely(reply)) {
fa764e8f 1405 if (!IS_ERR(reply)) {
7d16c847
PS
1406 rcu_read_lock(); /*To keep RCU checker happy. */
1407 err = ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex,
cde7f3ba
JR
1408 reply, info->snd_portid,
1409 info->snd_seq, 0,
bc619e29
JS
1410 OVS_FLOW_CMD_DEL,
1411 ufid_flags);
cde7f3ba
JR
1412 rcu_read_unlock();
1413 BUG_ON(err < 0);
cb25142c 1414 ovs_notify(&dp_flow_genl_family, &ovs_dp_flow_multicast_group, reply, info);
cde7f3ba 1415 } else {
cb25142c
PS
1416 genl_set_err(&dp_flow_genl_family, sock_net(skb->sk), 0,
1417 GROUP_ID(&ovs_dp_flow_multicast_group), PTR_ERR(reply));
1418
cde7f3ba 1419 }
afad3556 1420 }
37a1300c 1421
a1c564be 1422 ovs_flow_free(flow, true);
37a1300c 1423 return 0;
cd2a59e9
PS
1424unlock:
1425 ovs_unlock();
1426 return err;
37a1300c
BP
1427}
1428
df2c07f4 1429static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
37a1300c 1430{
bc619e29 1431 struct nlattr *a[__OVS_FLOW_ATTR_MAX];
df2c07f4 1432 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
994dc286 1433 struct table_instance *ti;
37a1300c 1434 struct datapath *dp;
bc619e29
JS
1435 u32 ufid_flags;
1436 int err;
1437
6db0f72d
JB
1438 err = genlmsg_parse_deprecated(cb->nlh, &dp_flow_genl_family, a,
1439 OVS_FLOW_ATTR_MAX, flow_policy, NULL);
bc619e29
JS
1440 if (err)
1441 return err;
1442 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
37a1300c 1443
f44ccce1 1444 rcu_read_lock();
01ac0970 1445 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
cd2a59e9 1446 if (!dp) {
f44ccce1 1447 rcu_read_unlock();
37a1300c 1448 return -ENODEV;
cd2a59e9 1449 }
37a1300c 1450
994dc286 1451 ti = rcu_dereference(dp->table.ti);
37a1300c 1452 for (;;) {
37a1300c
BP
1453 struct sw_flow *flow;
1454 u32 bucket, obj;
1455
1456 bucket = cb->args[0];
1457 obj = cb->args[1];
994dc286 1458 flow = ovs_flow_tbl_dump_next(ti, &bucket, &obj);
3544358a 1459 if (!flow)
37a1300c
BP
1460 break;
1461
2c622e5a 1462 if (ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex, skb,
28aea917 1463 NETLINK_CB(cb->skb).portid,
37a1300c 1464 cb->nlh->nlmsg_seq, NLM_F_MULTI,
229486c2 1465 OVS_FLOW_CMD_GET, ufid_flags) < 0)
37a1300c
BP
1466 break;
1467
1468 cb->args[0] = bucket;
1469 cb->args[1] = obj;
1470 }
f44ccce1 1471 rcu_read_unlock();
37a1300c 1472 return skb->len;
704a1e09
BP
1473}
1474
cb25142c
PS
1475static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
1476 [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
9233cef7 1477 [OVS_FLOW_ATTR_MASK] = { .type = NLA_NESTED },
cb25142c
PS
1478 [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
1479 [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
9233cef7 1480 [OVS_FLOW_ATTR_PROBE] = { .type = NLA_FLAG },
bc619e29
JS
1481 [OVS_FLOW_ATTR_UFID] = { .type = NLA_UNSPEC, .len = 1 },
1482 [OVS_FLOW_ATTR_UFID_FLAGS] = { .type = NLA_U32 },
cb25142c
PS
1483};
1484
e515e66a 1485static const struct genl_ops dp_flow_genl_ops[] = {
df2c07f4 1486 { .cmd = OVS_FLOW_CMD_NEW,
f1e9590e
YHW
1487#ifdef HAVE_GENL_VALIDATE_FLAGS
1488 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1489#endif
a6a8674d 1490 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2ef0f1c2 1491#ifdef HAVE_GENL_OPS_POLICY
37a1300c 1492 .policy = flow_policy,
2ef0f1c2 1493#endif
0c9fd022 1494 .doit = ovs_flow_cmd_new
37a1300c 1495 },
df2c07f4 1496 { .cmd = OVS_FLOW_CMD_DEL,
f1e9590e
YHW
1497#ifdef HAVE_GENL_VALIDATE_FLAGS
1498 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1499#endif
a6a8674d 1500 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2ef0f1c2 1501#ifdef HAVE_GENL_OPS_POLICY
37a1300c 1502 .policy = flow_policy,
2ef0f1c2 1503#endif
df2c07f4 1504 .doit = ovs_flow_cmd_del
37a1300c 1505 },
df2c07f4 1506 { .cmd = OVS_FLOW_CMD_GET,
f1e9590e
YHW
1507#ifdef HAVE_GENL_VALIDATE_FLAGS
1508 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1509#endif
37a1300c 1510 .flags = 0, /* OK for unprivileged users. */
2ef0f1c2 1511#ifdef HAVE_GENL_OPS_POLICY
37a1300c 1512 .policy = flow_policy,
2ef0f1c2 1513#endif
df2c07f4
JP
1514 .doit = ovs_flow_cmd_get,
1515 .dumpit = ovs_flow_cmd_dump
37a1300c 1516 },
df2c07f4 1517 { .cmd = OVS_FLOW_CMD_SET,
f1e9590e
YHW
1518#ifdef HAVE_GENL_VALIDATE_FLAGS
1519 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1520#endif
a6a8674d 1521 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2ef0f1c2 1522#ifdef HAVE_GENL_OPS_POLICY
37a1300c 1523 .policy = flow_policy,
2ef0f1c2 1524#endif
0c9fd022 1525 .doit = ovs_flow_cmd_set,
37a1300c
BP
1526 },
1527};
1528
ba63fe26 1529static struct genl_family dp_flow_genl_family __ro_after_init = {
df2c07f4 1530 .hdrsize = sizeof(struct ovs_header),
cb25142c
PS
1531 .name = OVS_FLOW_FAMILY,
1532 .version = OVS_FLOW_VERSION,
1533 .maxattr = OVS_FLOW_ATTR_MAX,
2ef0f1c2
JB
1534#ifndef HAVE_GENL_OPS_POLICY
1535 .policy = flow_policy,
1536#endif
b3dcb73c 1537 .netnsok = true,
cb25142c
PS
1538 .parallel_ops = true,
1539 .ops = dp_flow_genl_ops,
1540 .n_ops = ARRAY_SIZE(dp_flow_genl_ops),
1541 .mcgrps = &ovs_dp_flow_multicast_group,
1542 .n_mcgrps = 1,
ba63fe26 1543 .module = THIS_MODULE,
aaff4b55
BP
1544};
1545
0afa2373
TG
1546static size_t ovs_dp_cmd_msg_size(void)
1547{
1548 size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
1549
1550 msgsize += nla_total_size(IFNAMSIZ);
91b37647
PS
1551 msgsize += nla_total_size_64bit(sizeof(struct ovs_dp_stats));
1552 msgsize += nla_total_size_64bit(sizeof(struct ovs_dp_megaflow_stats));
300af20a 1553 msgsize += nla_total_size(sizeof(u32)); /* OVS_DP_ATTR_USER_FEATURES */
0afa2373
TG
1554
1555 return msgsize;
1556}
1557
d637497c 1558/* Called with ovs_mutex. */
df2c07f4 1559static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
28aea917 1560 u32 portid, u32 seq, u32 flags, u8 cmd)
064af421 1561{
df2c07f4 1562 struct ovs_header *ovs_header;
e926dfe3 1563 struct ovs_dp_stats dp_stats;
4fa72a95 1564 struct ovs_dp_megaflow_stats dp_megaflow_stats;
064af421
BP
1565 int err;
1566
28aea917 1567 ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
aaff4b55 1568 flags, cmd);
df2c07f4 1569 if (!ovs_header)
aaff4b55 1570 goto error;
064af421 1571
b063d9f0 1572 ovs_header->dp_ifindex = get_dpifindex(dp);
064af421 1573
850b6b3b 1574 err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
064af421 1575 if (err)
d6569377 1576 goto nla_put_failure;
064af421 1577
4fa72a95 1578 get_dp_stats(dp, &dp_stats, &dp_megaflow_stats);
91b37647
PS
1579 if (nla_put_64bit(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats),
1580 &dp_stats, OVS_DP_ATTR_PAD))
4fa72a95
AZ
1581 goto nla_put_failure;
1582
91b37647
PS
1583 if (nla_put_64bit(skb, OVS_DP_ATTR_MEGAFLOW_STATS,
1584 sizeof(struct ovs_dp_megaflow_stats),
1585 &dp_megaflow_stats, OVS_DP_ATTR_PAD))
c3cc8c03 1586 goto nla_put_failure;
d6569377 1587
c58cc9a4
TG
1588 if (nla_put_u32(skb, OVS_DP_ATTR_USER_FEATURES, dp->user_features))
1589 goto nla_put_failure;
1590
23b48dc1
TG
1591 genlmsg_end(skb, ovs_header);
1592 return 0;
d6569377
BP
1593
1594nla_put_failure:
df2c07f4 1595 genlmsg_cancel(skb, ovs_header);
aaff4b55
BP
1596error:
1597 return -EMSGSIZE;
d6569377
BP
1598}
1599
40c08cda 1600static struct sk_buff *ovs_dp_cmd_alloc_info(void)
d6569377 1601{
40c08cda 1602 return genlmsg_new(ovs_dp_cmd_msg_size(), GFP_KERNEL);
aaff4b55 1603}
9dca7bd5 1604
aa917006 1605/* Called with rcu_read_lock or ovs_mutex. */
2a4999f3 1606static struct datapath *lookup_datapath(struct net *net,
f1f60b85 1607 const struct ovs_header *ovs_header,
6455100f 1608 struct nlattr *a[OVS_DP_ATTR_MAX + 1])
d6569377 1609{
254f2dc8
BP
1610 struct datapath *dp;
1611
df2c07f4 1612 if (!a[OVS_DP_ATTR_NAME])
2a4999f3 1613 dp = get_dp(net, ovs_header->dp_ifindex);
254f2dc8 1614 else {
d6569377 1615 struct vport *vport;
d6569377 1616
2a4999f3 1617 vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
df2c07f4 1618 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
d6569377 1619 }
254f2dc8 1620 return dp ? dp : ERR_PTR(-ENODEV);
d6569377
BP
1621}
1622
94358dcf
TG
1623static void ovs_dp_reset_user_features(struct sk_buff *skb, struct genl_info *info)
1624{
1625 struct datapath *dp;
1626
1627 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
09350a3d 1628 if (IS_ERR(dp))
94358dcf
TG
1629 return;
1630
1631 WARN(dp->user_features, "Dropping previously announced user features\n");
1632 dp->user_features = 0;
1633}
1634
f1f60b85 1635static void ovs_dp_change(struct datapath *dp, struct nlattr *a[])
c58cc9a4
TG
1636{
1637 if (a[OVS_DP_ATTR_USER_FEATURES])
1638 dp->user_features = nla_get_u32(a[OVS_DP_ATTR_USER_FEATURES]);
1639}
1640
df2c07f4 1641static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
d6569377 1642{
aaff4b55 1643 struct nlattr **a = info->attrs;
d6569377 1644 struct vport_parms parms;
aaff4b55 1645 struct sk_buff *reply;
d6569377
BP
1646 struct datapath *dp;
1647 struct vport *vport;
2a4999f3 1648 struct ovs_net *ovs_net;
95b1d73a 1649 int err, i;
d6569377 1650
d6569377 1651 err = -EINVAL;
ea36840f 1652 if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
aaff4b55
BP
1653 goto err;
1654
40c08cda 1655 reply = ovs_dp_cmd_alloc_info();
d81eef1b
JR
1656 if (!reply)
1657 return -ENOMEM;
d6569377 1658
d6569377
BP
1659 err = -ENOMEM;
1660 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1661 if (dp == NULL)
d81eef1b 1662 goto err_free_reply;
2a4999f3 1663
c0cddcec 1664 ovs_dp_set_net(dp, sock_net(skb->sk));
0ceaa66c 1665
d6569377 1666 /* Allocate table. */
994dc286
PS
1667 err = ovs_flow_tbl_init(&dp->table);
1668 if (err)
d6569377
BP
1669 goto err_free_dp;
1670
08fb1bbd 1671 dp->stats_percpu = netdev_alloc_pcpu_stats(struct dp_stats_percpu);
99769a40
JG
1672 if (!dp->stats_percpu) {
1673 err = -ENOMEM;
1674 goto err_destroy_table;
1675 }
1676
8401bb6a
KC
1677 dp->ports = kmalloc_array(DP_VPORT_HASH_BUCKETS,
1678 sizeof(struct hlist_head),
1679 GFP_KERNEL);
95b1d73a
PS
1680 if (!dp->ports) {
1681 err = -ENOMEM;
1682 goto err_destroy_percpu;
1683 }
1684
1685 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1686 INIT_HLIST_HEAD(&dp->ports[i]);
1687
1cb57039
AZ
1688 err = ovs_meters_init(dp);
1689 if (err)
1690 goto err_destroy_ports_array;
1691
d6569377 1692 /* Set up our datapath device. */
df2c07f4
JP
1693 parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1694 parms.type = OVS_VPORT_TYPE_INTERNAL;
d6569377
BP
1695 parms.options = NULL;
1696 parms.dp = dp;
df2c07f4 1697 parms.port_no = OVSP_LOCAL;
beb1c69a 1698 parms.upcall_portids = a[OVS_DP_ATTR_UPCALL_PID];
b063d9f0 1699
c58cc9a4
TG
1700 ovs_dp_change(dp, a);
1701
d81eef1b
JR
1702 /* So far only local changes have been made, now need the lock. */
1703 ovs_lock();
1704
d6569377
BP
1705 vport = new_vport(&parms);
1706 if (IS_ERR(vport)) {
1707 err = PTR_ERR(vport);
1708 if (err == -EBUSY)
1709 err = -EEXIST;
1710
94358dcf
TG
1711 if (err == -EEXIST) {
1712 /* An outdated user space instance that does not understand
1713 * the concept of user_features has attempted to create a new
1714 * datapath and is likely to reuse it. Drop all user features.
1715 */
1716 if (info->genlhdr->version < OVS_DP_VER_FEATURES)
1717 ovs_dp_reset_user_features(skb, info);
1718 }
1719
1cb57039 1720 goto err_destroy_meters;
d6569377 1721 }
d6569377 1722
d81eef1b
JR
1723 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1724 info->snd_seq, 0, OVS_DP_CMD_NEW);
1725 BUG_ON(err < 0);
aaff4b55 1726
2a4999f3 1727 ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
fb93e9aa 1728 list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
a0fb56c1 1729
cd2a59e9 1730 ovs_unlock();
d6569377 1731
cb25142c 1732 ovs_notify(&dp_datapath_genl_family, &ovs_dp_datapath_multicast_group, reply, info);
d6569377
BP
1733 return 0;
1734
1cb57039 1735err_destroy_meters:
d81eef1b 1736 ovs_unlock();
1cb57039
AZ
1737 ovs_meters_exit(dp);
1738err_destroy_ports_array:
95b1d73a 1739 kfree(dp->ports);
99769a40
JG
1740err_destroy_percpu:
1741 free_percpu(dp->stats_percpu);
d6569377 1742err_destroy_table:
e379e4d1 1743 ovs_flow_tbl_destroy(&dp->table);
d6569377 1744err_free_dp:
d6569377 1745 kfree(dp);
d81eef1b
JR
1746err_free_reply:
1747 kfree_skb(reply);
d6569377 1748err:
064af421
BP
1749 return err;
1750}
1751
cd2a59e9 1752/* Called with ovs_mutex. */
2a4999f3 1753static void __dp_destroy(struct datapath *dp)
44e05eca 1754{
95b1d73a 1755 int i;
44e05eca 1756
95b1d73a
PS
1757 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1758 struct vport *vport;
f8dfbcb7 1759 struct hlist_node *n;
95b1d73a 1760
f8dfbcb7 1761 hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
95b1d73a
PS
1762 if (vport->port_no != OVSP_LOCAL)
1763 ovs_dp_detach_port(vport);
1764 }
ed099e92 1765
fb93e9aa 1766 list_del_rcu(&dp->list_node);
ed099e92 1767
cd2a59e9 1768 /* OVSP_LOCAL is datapath internal port. We need to make sure that
d103f479
AZ
1769 * all ports in datapath are destroyed first before freeing datapath.
1770 */
cd2a59e9 1771 ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
99620d2c 1772
d103f479 1773 /* RCU destroy the flow table */
ed099e92 1774 call_rcu(&dp->rcu, destroy_dp_rcu);
2a4999f3
PS
1775}
1776
1777static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1778{
1779 struct sk_buff *reply;
1780 struct datapath *dp;
1781 int err;
1782
40c08cda 1783 reply = ovs_dp_cmd_alloc_info();
d81eef1b
JR
1784 if (!reply)
1785 return -ENOMEM;
1786
cd2a59e9 1787 ovs_lock();
2a4999f3
PS
1788 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1789 err = PTR_ERR(dp);
1790 if (IS_ERR(dp))
d81eef1b 1791 goto err_unlock_free;
2a4999f3 1792
d81eef1b
JR
1793 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1794 info->snd_seq, 0, OVS_DP_CMD_DEL);
1795 BUG_ON(err < 0);
2a4999f3
PS
1796
1797 __dp_destroy(dp);
d81eef1b 1798 ovs_unlock();
7d16c847 1799
cb25142c 1800 ovs_notify(&dp_datapath_genl_family, &ovs_dp_datapath_multicast_group, reply, info);
99620d2c 1801 return 0;
d81eef1b
JR
1802
1803err_unlock_free:
cd2a59e9 1804 ovs_unlock();
d81eef1b 1805 kfree_skb(reply);
cd2a59e9 1806 return err;
44e05eca
BP
1807}
1808
df2c07f4 1809static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
064af421 1810{
aaff4b55 1811 struct sk_buff *reply;
d6569377 1812 struct datapath *dp;
d6569377 1813 int err;
064af421 1814
40c08cda 1815 reply = ovs_dp_cmd_alloc_info();
d81eef1b
JR
1816 if (!reply)
1817 return -ENOMEM;
1818
cd2a59e9 1819 ovs_lock();
2a4999f3 1820 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
cd2a59e9 1821 err = PTR_ERR(dp);
d6569377 1822 if (IS_ERR(dp))
d81eef1b 1823 goto err_unlock_free;
38c6ecbc 1824
c58cc9a4
TG
1825 ovs_dp_change(dp, info->attrs);
1826
d81eef1b 1827 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
229486c2 1828 info->snd_seq, 0, OVS_DP_CMD_GET);
d81eef1b 1829 BUG_ON(err < 0);
a0fb56c1 1830
cd2a59e9 1831 ovs_unlock();
7d16c847 1832
cb25142c 1833 ovs_notify(&dp_datapath_genl_family, &ovs_dp_datapath_multicast_group, reply, info);
aaff4b55 1834 return 0;
d81eef1b
JR
1835
1836err_unlock_free:
cd2a59e9 1837 ovs_unlock();
d81eef1b 1838 kfree_skb(reply);
cd2a59e9 1839 return err;
064af421
BP
1840}
1841
df2c07f4 1842static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1dcf111b 1843{
aaff4b55 1844 struct sk_buff *reply;
d6569377 1845 struct datapath *dp;
d6569377 1846 int err;
1dcf111b 1847
40c08cda 1848 reply = ovs_dp_cmd_alloc_info();
d81eef1b
JR
1849 if (!reply)
1850 return -ENOMEM;
1851
d637497c 1852 ovs_lock();
2a4999f3 1853 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
cd2a59e9
PS
1854 if (IS_ERR(dp)) {
1855 err = PTR_ERR(dp);
d81eef1b 1856 goto err_unlock_free;
cd2a59e9 1857 }
d81eef1b 1858 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
229486c2 1859 info->snd_seq, 0, OVS_DP_CMD_GET);
d81eef1b 1860 BUG_ON(err < 0);
d637497c 1861 ovs_unlock();
aaff4b55
BP
1862
1863 return genlmsg_reply(reply, info);
cd2a59e9 1864
d81eef1b 1865err_unlock_free:
d637497c 1866 ovs_unlock();
d81eef1b 1867 kfree_skb(reply);
cd2a59e9 1868 return err;
1dcf111b
JP
1869}
1870
df2c07f4 1871static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
a7786963 1872{
2a4999f3 1873 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
254f2dc8
BP
1874 struct datapath *dp;
1875 int skip = cb->args[0];
1876 int i = 0;
a7786963 1877
d637497c
PS
1878 ovs_lock();
1879 list_for_each_entry(dp, &ovs_net->dps, list_node) {
a2bab2f0 1880 if (i >= skip &&
28aea917 1881 ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
aaff4b55 1882 cb->nlh->nlmsg_seq, NLM_F_MULTI,
229486c2 1883 OVS_DP_CMD_GET) < 0)
aaff4b55 1884 break;
254f2dc8 1885 i++;
a7786963 1886 }
d637497c 1887 ovs_unlock();
aaff4b55 1888
254f2dc8
BP
1889 cb->args[0] = i;
1890
aaff4b55 1891 return skb->len;
c19e6535
BP
1892}
1893
cb25142c
PS
1894static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1895 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1896 [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1897 [OVS_DP_ATTR_USER_FEATURES] = { .type = NLA_U32 },
1898};
1899
e515e66a 1900static const struct genl_ops dp_datapath_genl_ops[] = {
df2c07f4 1901 { .cmd = OVS_DP_CMD_NEW,
f1e9590e
YHW
1902#ifdef HAVE_GENL_VALIDATE_FLAGS
1903 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1904#endif
a6a8674d 1905 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2ef0f1c2 1906#ifdef HAVE_GENL_OPS_POLICY
aaff4b55 1907 .policy = datapath_policy,
2ef0f1c2 1908#endif
df2c07f4 1909 .doit = ovs_dp_cmd_new
aaff4b55 1910 },
df2c07f4 1911 { .cmd = OVS_DP_CMD_DEL,
f1e9590e
YHW
1912#ifdef HAVE_GENL_VALIDATE_FLAGS
1913 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1914#endif
a6a8674d 1915 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2ef0f1c2 1916#ifdef HAVE_GENL_OPS_POLICY
aaff4b55 1917 .policy = datapath_policy,
2ef0f1c2 1918#endif
df2c07f4 1919 .doit = ovs_dp_cmd_del
aaff4b55 1920 },
df2c07f4 1921 { .cmd = OVS_DP_CMD_GET,
f1e9590e
YHW
1922#ifdef HAVE_GENL_VALIDATE_FLAGS
1923 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1924#endif
aaff4b55 1925 .flags = 0, /* OK for unprivileged users. */
2ef0f1c2 1926#ifdef HAVE_GENL_OPS_POLICY
aaff4b55 1927 .policy = datapath_policy,
2ef0f1c2 1928#endif
df2c07f4
JP
1929 .doit = ovs_dp_cmd_get,
1930 .dumpit = ovs_dp_cmd_dump
aaff4b55 1931 },
df2c07f4 1932 { .cmd = OVS_DP_CMD_SET,
f1e9590e
YHW
1933#ifdef HAVE_GENL_VALIDATE_FLAGS
1934 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1935#endif
a6a8674d 1936 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2ef0f1c2 1937#ifdef HAVE_GENL_OPS_POLICY
aaff4b55 1938 .policy = datapath_policy,
2ef0f1c2 1939#endif
df2c07f4 1940 .doit = ovs_dp_cmd_set,
aaff4b55
BP
1941 },
1942};
1943
ba63fe26 1944static struct genl_family dp_datapath_genl_family __ro_after_init = {
df2c07f4 1945 .hdrsize = sizeof(struct ovs_header),
cb25142c
PS
1946 .name = OVS_DATAPATH_FAMILY,
1947 .version = OVS_DATAPATH_VERSION,
1948 .maxattr = OVS_DP_ATTR_MAX,
2ef0f1c2
JB
1949#ifndef HAVE_GENL_OPS_POLICY
1950 .policy = datapath_policy,
1951#endif
b3dcb73c 1952 .netnsok = true,
cb25142c
PS
1953 .parallel_ops = true,
1954 .ops = dp_datapath_genl_ops,
1955 .n_ops = ARRAY_SIZE(dp_datapath_genl_ops),
1956 .mcgrps = &ovs_dp_datapath_multicast_group,
1957 .n_mcgrps = 1,
ba63fe26 1958 .module = THIS_MODULE,
f0fef760
BP
1959};
1960
cd2a59e9 1961/* Called with ovs_mutex or RCU read lock. */
df2c07f4 1962static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
b147f2e9
JB
1963 struct net *net, u32 portid, u32 seq,
1964 u32 flags, u8 cmd)
064af421 1965{
df2c07f4 1966 struct ovs_header *ovs_header;
e926dfe3 1967 struct ovs_vport_stats vport_stats;
c19e6535
BP
1968 int err;
1969
28aea917 1970 ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
f0fef760 1971 flags, cmd);
df2c07f4 1972 if (!ovs_header)
f0fef760 1973 return -EMSGSIZE;
c19e6535 1974
99769a40 1975 ovs_header->dp_ifindex = get_dpifindex(vport->dp);
c19e6535 1976
c3cc8c03
DM
1977 if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
1978 nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
e23775f2 1979 nla_put_string(skb, OVS_VPORT_ATTR_NAME,
b147f2e9
JB
1980 ovs_vport_name(vport)) ||
1981 nla_put_u32(skb, OVS_VPORT_ATTR_IFINDEX, vport->dev->ifindex))
c3cc8c03 1982 goto nla_put_failure;
c19e6535 1983
b147f2e9
JB
1984#ifdef HAVE_PEERNET2ID_ALLOC
1985 if (!net_eq(net, dev_net(vport->dev))) {
1986 int id = peernet2id_alloc(net, dev_net(vport->dev));
1987
1988 if (nla_put_s32(skb, OVS_VPORT_ATTR_NETNSID, id))
1989 goto nla_put_failure;
1990 }
1991
1992#endif
850b6b3b 1993 ovs_vport_get_stats(vport, &vport_stats);
91b37647
PS
1994 if (nla_put_64bit(skb, OVS_VPORT_ATTR_STATS,
1995 sizeof(struct ovs_vport_stats), &vport_stats,
1996 OVS_VPORT_ATTR_PAD))
c3cc8c03 1997 goto nla_put_failure;
c19e6535 1998
beb1c69a
AW
1999 if (ovs_vport_get_upcall_portids(vport, skb))
2000 goto nla_put_failure;
2001
850b6b3b 2002 err = ovs_vport_get_options(vport, skb);
f0fef760
BP
2003 if (err == -EMSGSIZE)
2004 goto error;
c19e6535 2005
23b48dc1
TG
2006 genlmsg_end(skb, ovs_header);
2007 return 0;
c19e6535
BP
2008
2009nla_put_failure:
2010 err = -EMSGSIZE;
f0fef760 2011error:
df2c07f4 2012 genlmsg_cancel(skb, ovs_header);
f0fef760 2013 return err;
064af421
BP
2014}
2015
d81eef1b
JR
2016static struct sk_buff *ovs_vport_cmd_alloc_info(void)
2017{
2018 return nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2019}
2020
2021/* Called with ovs_mutex, only via ovs_dp_notify_wq(). */
b147f2e9
JB
2022struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, struct net *net,
2023 u32 portid, u32 seq, u8 cmd)
064af421 2024{
c19e6535 2025 struct sk_buff *skb;
f0fef760 2026 int retval;
c19e6535 2027
f0fef760 2028 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
c19e6535
BP
2029 if (!skb)
2030 return ERR_PTR(-ENOMEM);
2031
b147f2e9 2032 retval = ovs_vport_cmd_fill_info(vport, skb, net, portid, seq, 0, cmd);
c25ea534
JG
2033 BUG_ON(retval < 0);
2034
c19e6535 2035 return skb;
f0fef760 2036}
c19e6535 2037
cd2a59e9 2038/* Called with ovs_mutex or RCU read lock. */
2a4999f3 2039static struct vport *lookup_vport(struct net *net,
f1f60b85 2040 const struct ovs_header *ovs_header,
df2c07f4 2041 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
c19e6535
BP
2042{
2043 struct datapath *dp;
2044 struct vport *vport;
2045
b147f2e9
JB
2046 if (a[OVS_VPORT_ATTR_IFINDEX])
2047 return ERR_PTR(-EOPNOTSUPP);
df2c07f4 2048 if (a[OVS_VPORT_ATTR_NAME]) {
2a4999f3 2049 vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
ed099e92 2050 if (!vport)
c19e6535 2051 return ERR_PTR(-ENODEV);
24ce832d
BP
2052 if (ovs_header->dp_ifindex &&
2053 ovs_header->dp_ifindex != get_dpifindex(vport->dp))
2054 return ERR_PTR(-ENODEV);
c19e6535 2055 return vport;
df2c07f4
JP
2056 } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
2057 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
c19e6535
BP
2058
2059 if (port_no >= DP_MAX_PORTS)
f0fef760 2060 return ERR_PTR(-EFBIG);
c19e6535 2061
2a4999f3 2062 dp = get_dp(net, ovs_header->dp_ifindex);
c19e6535
BP
2063 if (!dp)
2064 return ERR_PTR(-ENODEV);
f2459fe7 2065
cd2a59e9 2066 vport = ovs_vport_ovsl_rcu(dp, port_no);
ed099e92 2067 if (!vport)
17535c57 2068 return ERR_PTR(-ENODEV);
c19e6535
BP
2069 return vport;
2070 } else
2071 return ERR_PTR(-EINVAL);
b147f2e9 2072
064af421
BP
2073}
2074
ec335e0b 2075static unsigned int ovs_get_max_headroom(struct datapath *dp)
8ce37339 2076{
ec335e0b 2077 unsigned int dev_headroom, max_headroom = 0;
8ce37339
PS
2078 struct net_device *dev;
2079 struct vport *vport;
2080 int i;
2081
2082 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
2083 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
2084 dev = vport->dev;
2085 dev_headroom = netdev_get_fwd_headroom(dev);
2086 if (dev_headroom > max_headroom)
2087 max_headroom = dev_headroom;
2088 }
2089 }
2090
ec335e0b
TY
2091 return max_headroom;
2092}
2093
2094/* Called with ovs_mutex */
2095static void ovs_update_headroom(struct datapath *dp, unsigned int new_headroom)
2096{
2097 struct vport *vport;
2098 int i;
2099
2100 dp->max_headroom = new_headroom;
8ce37339
PS
2101 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
2102 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node)
ec335e0b 2103 netdev_set_rx_headroom(vport->dev, new_headroom);
8ce37339
PS
2104}
2105
df2c07f4 2106static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
c19e6535 2107{
f0fef760 2108 struct nlattr **a = info->attrs;
df2c07f4 2109 struct ovs_header *ovs_header = info->userhdr;
c19e6535 2110 struct vport_parms parms;
ed099e92 2111 struct sk_buff *reply;
c19e6535 2112 struct vport *vport;
c19e6535 2113 struct datapath *dp;
ec335e0b 2114 unsigned int new_headroom;
b0ec0f27 2115 u32 port_no;
c19e6535 2116 int err;
b0ec0f27 2117
ea36840f
BP
2118 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
2119 !a[OVS_VPORT_ATTR_UPCALL_PID])
d81eef1b 2120 return -EINVAL;
b147f2e9
JB
2121 if (a[OVS_VPORT_ATTR_IFINDEX])
2122 return -EOPNOTSUPP;
d81eef1b
JR
2123
2124 port_no = a[OVS_VPORT_ATTR_PORT_NO]
2125 ? nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]) : 0;
2126 if (port_no >= DP_MAX_PORTS)
2127 return -EFBIG;
2128
2129 reply = ovs_vport_cmd_alloc_info();
2130 if (!reply)
2131 return -ENOMEM;
f0fef760 2132
cd2a59e9 2133 ovs_lock();
5a38795f 2134restart:
2a4999f3 2135 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
c19e6535
BP
2136 err = -ENODEV;
2137 if (!dp)
d81eef1b 2138 goto exit_unlock_free;
c19e6535 2139
d81eef1b 2140 if (port_no) {
cd2a59e9 2141 vport = ovs_vport_ovsl(dp, port_no);
c19e6535
BP
2142 err = -EBUSY;
2143 if (vport)
d81eef1b 2144 goto exit_unlock_free;
c19e6535
BP
2145 } else {
2146 for (port_no = 1; ; port_no++) {
2147 if (port_no >= DP_MAX_PORTS) {
2148 err = -EFBIG;
d81eef1b 2149 goto exit_unlock_free;
c19e6535 2150 }
cd2a59e9 2151 vport = ovs_vport_ovsl(dp, port_no);
c19e6535
BP
2152 if (!vport)
2153 break;
51d4d598 2154 }
064af421 2155 }
b0ec0f27 2156
df2c07f4
JP
2157 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
2158 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
2159 parms.options = a[OVS_VPORT_ATTR_OPTIONS];
c19e6535
BP
2160 parms.dp = dp;
2161 parms.port_no = port_no;
beb1c69a 2162 parms.upcall_portids = a[OVS_VPORT_ATTR_UPCALL_PID];
c19e6535
BP
2163
2164 vport = new_vport(&parms);
2165 err = PTR_ERR(vport);
5a38795f
TG
2166 if (IS_ERR(vport)) {
2167 if (err == -EAGAIN)
2168 goto restart;
d81eef1b 2169 goto exit_unlock_free;
5a38795f 2170 }
c19e6535 2171
b147f2e9
JB
2172 err = ovs_vport_cmd_fill_info(vport, reply, genl_info_net(info),
2173 info->snd_portid, info->snd_seq, 0,
2174 OVS_VPORT_CMD_NEW);
d81eef1b 2175 BUG_ON(err < 0);
8ce37339 2176
ec335e0b
TY
2177 new_headroom = netdev_get_fwd_headroom(vport->dev);
2178
2179 if (new_headroom > dp->max_headroom)
2180 ovs_update_headroom(dp, new_headroom);
8ce37339
PS
2181 else
2182 netdev_set_rx_headroom(vport->dev, dp->max_headroom);
2183
d81eef1b 2184 ovs_unlock();
e297c6b7 2185
cb25142c 2186 ovs_notify(&dp_vport_genl_family, &ovs_dp_vport_multicast_group, reply, info);
d81eef1b 2187 return 0;
c19e6535 2188
d81eef1b 2189exit_unlock_free:
cd2a59e9 2190 ovs_unlock();
d81eef1b 2191 kfree_skb(reply);
c19e6535 2192 return err;
44e05eca
BP
2193}
2194
df2c07f4 2195static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
44e05eca 2196{
f0fef760
BP
2197 struct nlattr **a = info->attrs;
2198 struct sk_buff *reply;
c19e6535 2199 struct vport *vport;
c19e6535 2200 int err;
44e05eca 2201
d81eef1b
JR
2202 reply = ovs_vport_cmd_alloc_info();
2203 if (!reply)
2204 return -ENOMEM;
2205
cd2a59e9 2206 ovs_lock();
2a4999f3 2207 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
c19e6535
BP
2208 err = PTR_ERR(vport);
2209 if (IS_ERR(vport))
d81eef1b 2210 goto exit_unlock_free;
44e05eca 2211
6455100f 2212 if (a[OVS_VPORT_ATTR_TYPE] &&
17ec1d04 2213 nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
4879d4c7 2214 err = -EINVAL;
d81eef1b 2215 goto exit_unlock_free;
c25ea534
JG
2216 }
2217
17ec1d04 2218 if (a[OVS_VPORT_ATTR_OPTIONS]) {
850b6b3b 2219 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
17ec1d04 2220 if (err)
d81eef1b 2221 goto exit_unlock_free;
17ec1d04 2222 }
1fc7083d 2223
beb1c69a 2224 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
7d16c847
PS
2225 struct nlattr *ids = a[OVS_VPORT_ATTR_UPCALL_PID];
2226
2227 err = ovs_vport_set_upcall_portids(vport, ids);
beb1c69a
AW
2228 if (err)
2229 goto exit_unlock_free;
2230 }
c19e6535 2231
b147f2e9
JB
2232 err = ovs_vport_cmd_fill_info(vport, reply, genl_info_net(info),
2233 info->snd_portid, info->snd_seq, 0,
229486c2 2234 OVS_VPORT_CMD_SET);
c25ea534 2235 BUG_ON(err < 0);
cd2a59e9 2236 ovs_unlock();
d81eef1b 2237
cb25142c 2238 ovs_notify(&dp_vport_genl_family, &ovs_dp_vport_multicast_group, reply, info);
c25ea534
JG
2239 return 0;
2240
d81eef1b 2241exit_unlock_free:
cd2a59e9 2242 ovs_unlock();
d81eef1b 2243 kfree_skb(reply);
c19e6535 2244 return err;
064af421
BP
2245}
2246
df2c07f4 2247static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
7c40efc9 2248{
ec335e0b 2249 bool update_headroom = false;
f0fef760
BP
2250 struct nlattr **a = info->attrs;
2251 struct sk_buff *reply;
8ce37339 2252 struct datapath *dp;
c19e6535 2253 struct vport *vport;
ec335e0b 2254 unsigned int new_headroom;
c19e6535
BP
2255 int err;
2256
d81eef1b
JR
2257 reply = ovs_vport_cmd_alloc_info();
2258 if (!reply)
2259 return -ENOMEM;
2260
cd2a59e9 2261 ovs_lock();
2a4999f3 2262 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
c19e6535 2263 err = PTR_ERR(vport);
f0fef760 2264 if (IS_ERR(vport))
d81eef1b 2265 goto exit_unlock_free;
c19e6535 2266
df2c07f4 2267 if (vport->port_no == OVSP_LOCAL) {
f0fef760 2268 err = -EINVAL;
d81eef1b 2269 goto exit_unlock_free;
f0fef760
BP
2270 }
2271
b147f2e9
JB
2272 err = ovs_vport_cmd_fill_info(vport, reply, genl_info_net(info),
2273 info->snd_portid, info->snd_seq, 0,
2274 OVS_VPORT_CMD_DEL);
d81eef1b 2275 BUG_ON(err < 0);
8ce37339
PS
2276
2277 /* the vport deletion may trigger dp headroom update */
2278 dp = vport->dp;
2279 if (netdev_get_fwd_headroom(vport->dev) == dp->max_headroom)
ec335e0b
TY
2280 update_headroom = true;
2281
8ce37339 2282 netdev_reset_rx_headroom(vport->dev);
850b6b3b 2283 ovs_dp_detach_port(vport);
8ce37339 2284
ec335e0b
TY
2285 if (update_headroom) {
2286 new_headroom = ovs_get_max_headroom(dp);
8ce37339 2287
ec335e0b
TY
2288 if (new_headroom < dp->max_headroom)
2289 ovs_update_headroom(dp, new_headroom);
2290 }
d81eef1b 2291 ovs_unlock();
f0fef760 2292
cb25142c 2293 ovs_notify(&dp_vport_genl_family, &ovs_dp_vport_multicast_group, reply, info);
d81eef1b 2294 return 0;
f0fef760 2295
d81eef1b 2296exit_unlock_free:
cd2a59e9 2297 ovs_unlock();
d81eef1b 2298 kfree_skb(reply);
c19e6535 2299 return err;
7c40efc9
BP
2300}
2301
df2c07f4 2302static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
7c40efc9 2303{
f0fef760 2304 struct nlattr **a = info->attrs;
df2c07f4 2305 struct ovs_header *ovs_header = info->userhdr;
ed099e92 2306 struct sk_buff *reply;
c19e6535 2307 struct vport *vport;
c19e6535
BP
2308 int err;
2309
d81eef1b
JR
2310 reply = ovs_vport_cmd_alloc_info();
2311 if (!reply)
2312 return -ENOMEM;
2313
ed099e92 2314 rcu_read_lock();
2a4999f3 2315 vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
c19e6535
BP
2316 err = PTR_ERR(vport);
2317 if (IS_ERR(vport))
d81eef1b 2318 goto exit_unlock_free;
b147f2e9
JB
2319 err = ovs_vport_cmd_fill_info(vport, reply, genl_info_net(info),
2320 info->snd_portid, info->snd_seq, 0,
229486c2 2321 OVS_VPORT_CMD_GET);
d81eef1b 2322 BUG_ON(err < 0);
df2fa9b5
JG
2323 rcu_read_unlock();
2324
2325 return genlmsg_reply(reply, info);
ed099e92 2326
d81eef1b 2327exit_unlock_free:
ed099e92 2328 rcu_read_unlock();
d81eef1b 2329 kfree_skb(reply);
c19e6535
BP
2330 return err;
2331}
2332
df2c07f4 2333static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
c19e6535 2334{
df2c07f4 2335 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
c19e6535 2336 struct datapath *dp;
95b1d73a
PS
2337 int bucket = cb->args[0], skip = cb->args[1];
2338 int i, j = 0;
c19e6535 2339
03fc2881 2340 rcu_read_lock();
01ac0970 2341 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
03fc2881
JR
2342 if (!dp) {
2343 rcu_read_unlock();
f0fef760 2344 return -ENODEV;
03fc2881 2345 }
95b1d73a 2346 for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
ed099e92 2347 struct vport *vport;
95b1d73a
PS
2348
2349 j = 0;
f8dfbcb7 2350 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
95b1d73a
PS
2351 if (j >= skip &&
2352 ovs_vport_cmd_fill_info(vport, skb,
b147f2e9 2353 sock_net(skb->sk),
28aea917 2354 NETLINK_CB(cb->skb).portid,
95b1d73a
PS
2355 cb->nlh->nlmsg_seq,
2356 NLM_F_MULTI,
229486c2 2357 OVS_VPORT_CMD_GET) < 0)
95b1d73a
PS
2358 goto out;
2359
2360 j++;
2361 }
2362 skip = 0;
c19e6535 2363 }
95b1d73a 2364out:
ed099e92 2365 rcu_read_unlock();
c19e6535 2366
95b1d73a
PS
2367 cb->args[0] = i;
2368 cb->args[1] = j;
f0fef760 2369
95b1d73a 2370 return skb->len;
7c40efc9
BP
2371}
2372
cb25142c
PS
2373static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
2374 [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
2375 [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
2376 [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
2377 [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
8fc8986d 2378 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_UNSPEC },
cb25142c 2379 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
b147f2e9
JB
2380 [OVS_VPORT_ATTR_IFINDEX] = { .type = NLA_U32 },
2381 [OVS_VPORT_ATTR_NETNSID] = { .type = NLA_S32 },
cb25142c
PS
2382};
2383
e515e66a 2384static const struct genl_ops dp_vport_genl_ops[] = {
df2c07f4 2385 { .cmd = OVS_VPORT_CMD_NEW,
f1e9590e
YHW
2386#ifdef HAVE_GENL_VALIDATE_FLAGS
2387 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2388#endif
a6a8674d 2389 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2ef0f1c2 2390#ifdef HAVE_GENL_OPS_POLICY
f0fef760 2391 .policy = vport_policy,
2ef0f1c2 2392#endif
df2c07f4 2393 .doit = ovs_vport_cmd_new
f0fef760 2394 },
df2c07f4 2395 { .cmd = OVS_VPORT_CMD_DEL,
f1e9590e
YHW
2396#ifdef HAVE_GENL_VALIDATE_FLAGS
2397 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2398#endif
a6a8674d 2399 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2ef0f1c2 2400#ifdef HAVE_GENL_OPS_POLICY
f0fef760 2401 .policy = vport_policy,
2ef0f1c2 2402#endif
df2c07f4 2403 .doit = ovs_vport_cmd_del
f0fef760 2404 },
df2c07f4 2405 { .cmd = OVS_VPORT_CMD_GET,
f1e9590e
YHW
2406#ifdef HAVE_GENL_VALIDATE_FLAGS
2407 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2408#endif
f0fef760 2409 .flags = 0, /* OK for unprivileged users. */
2ef0f1c2 2410#ifdef HAVE_GENL_OPS_POLICY
f0fef760 2411 .policy = vport_policy,
2ef0f1c2 2412#endif
df2c07f4
JP
2413 .doit = ovs_vport_cmd_get,
2414 .dumpit = ovs_vport_cmd_dump
f0fef760 2415 },
df2c07f4 2416 { .cmd = OVS_VPORT_CMD_SET,
f1e9590e
YHW
2417#ifdef HAVE_GENL_VALIDATE_FLAGS
2418 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2419#endif
a6a8674d 2420 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2ef0f1c2 2421#ifdef HAVE_GENL_OPS_POLICY
f0fef760 2422 .policy = vport_policy,
2ef0f1c2 2423#endif
df2c07f4 2424 .doit = ovs_vport_cmd_set,
f0fef760
BP
2425 },
2426};
2427
ba63fe26 2428struct genl_family dp_vport_genl_family __ro_after_init = {
cb25142c
PS
2429 .hdrsize = sizeof(struct ovs_header),
2430 .name = OVS_VPORT_FAMILY,
2431 .version = OVS_VPORT_VERSION,
2432 .maxattr = OVS_VPORT_ATTR_MAX,
2ef0f1c2
JB
2433#ifndef HAVE_GENL_OPS_POLICY
2434 .policy = vport_policy,
2435#endif
cb25142c
PS
2436 .netnsok = true,
2437 .parallel_ops = true,
2438 .ops = dp_vport_genl_ops,
2439 .n_ops = ARRAY_SIZE(dp_vport_genl_ops),
2440 .mcgrps = &ovs_dp_vport_multicast_group,
2441 .n_mcgrps = 1,
ba63fe26 2442 .module = THIS_MODULE,
982b8810 2443};
ed099e92 2444
18fd3a52 2445static struct genl_family *dp_genl_families[] = {
cb25142c
PS
2446 &dp_datapath_genl_family,
2447 &dp_vport_genl_family,
2448 &dp_flow_genl_family,
2449 &dp_packet_genl_family,
1cb57039 2450 &dp_meter_genl_family,
cb2a5486
YHW
2451#if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
2452 &dp_ct_limit_genl_family,
2453#endif
982b8810 2454};
ed099e92 2455
982b8810
BP
2456static void dp_unregister_genl(int n_families)
2457{
2458 int i;
ed099e92 2459
b867ca75 2460 for (i = 0; i < n_families; i++)
cb25142c 2461 genl_unregister_family(dp_genl_families[i]);
ed099e92
BP
2462}
2463
ba63fe26 2464static int __init dp_register_genl(void)
064af421 2465{
982b8810
BP
2466 int err;
2467 int i;
064af421 2468
982b8810 2469 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
064af421 2470
cb25142c 2471 err = genl_register_family(dp_genl_families[i]);
982b8810
BP
2472 if (err)
2473 goto error;
982b8810 2474 }
9cc8b4e4 2475
982b8810 2476 return 0;
064af421
BP
2477
2478error:
cb25142c 2479 dp_unregister_genl(i);
982b8810 2480 return err;
064af421
BP
2481}
2482
2a4999f3
PS
2483static int __net_init ovs_init_net(struct net *net)
2484{
2485 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2486
2487 INIT_LIST_HEAD(&ovs_net->dps);
cd2a59e9 2488 INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
7f4a5d68 2489 ovs_netns_frags_init(net);
2490 ovs_netns_frags6_init(net);
cb2a5486 2491 return ovs_ct_init(net);
2a4999f3
PS
2492}
2493
cabd5516
PS
2494static void __net_exit list_vports_from_net(struct net *net, struct net *dnet,
2495 struct list_head *head)
2a4999f3
PS
2496{
2497 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
cabd5516
PS
2498 struct datapath *dp;
2499
2500 list_for_each_entry(dp, &ovs_net->dps, list_node) {
2501 int i;
2502
2503 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
2504 struct vport *vport;
2505
2506 hlist_for_each_entry(vport, &dp->ports[i], dp_hash_node) {
cabd5516
PS
2507
2508 if (vport->ops->type != OVS_VPORT_TYPE_INTERNAL)
2509 continue;
2510
e23775f2 2511 if (dev_net(vport->dev) == dnet)
cabd5516
PS
2512 list_add(&vport->detach_list, head);
2513 }
2514 }
2515 }
2516}
2517
2518static void __net_exit ovs_exit_net(struct net *dnet)
2519{
2520 struct datapath *dp, *dp_next;
2521 struct ovs_net *ovs_net = net_generic(dnet, ovs_net_id);
2522 struct vport *vport, *vport_next;
2523 struct net *net;
2524 LIST_HEAD(head);
2a4999f3 2525
7f4a5d68 2526 ovs_netns_frags6_exit(dnet);
2527 ovs_netns_frags_exit(dnet);
038e34ab 2528 ovs_ct_exit(dnet);
cd2a59e9
PS
2529 ovs_lock();
2530 list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
2531 __dp_destroy(dp);
cabd5516 2532
3d10a0c8
YHW
2533#ifdef HAVE_NET_RWSEM
2534 down_read(&net_rwsem);
2535#else
cabd5516 2536 rtnl_lock();
3d10a0c8 2537#endif
cabd5516
PS
2538 for_each_net(net)
2539 list_vports_from_net(net, dnet, &head);
3d10a0c8
YHW
2540#ifdef HAVE_NET_RWSEM
2541 up_read(&net_rwsem);
2542#else
cabd5516 2543 rtnl_unlock();
3d10a0c8 2544#endif
cabd5516
PS
2545
2546 /* Detach all vports from given namespace. */
2547 list_for_each_entry_safe(vport, vport_next, &head, detach_list) {
2548 list_del(&vport->detach_list);
2549 ovs_dp_detach_port(vport);
2550 }
2551
cd2a59e9
PS
2552 ovs_unlock();
2553
2554 cancel_work_sync(&ovs_net->dp_notify_work);
2a4999f3
PS
2555}
2556
2557static struct pernet_operations ovs_net_ops = {
2558 .init = ovs_init_net,
2559 .exit = ovs_exit_net,
2560 .id = &ovs_net_id,
2561 .size = sizeof(struct ovs_net),
2562};
2563
22d24ebf
BP
2564static int __init dp_init(void)
2565{
2566 int err;
2567
9cfa4718 2568 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > sizeof_field(struct sk_buff, cb));
22d24ebf 2569
26bfaeaa 2570 pr_info("Open vSwitch switching datapath %s\n", VERSION);
064af421 2571
907c26a8 2572 ovs_nsh_init();
595e069a
JS
2573 err = action_fifos_init();
2574 if (err)
7f4a5d68 2575 goto error;
595e069a 2576
5282e284 2577 err = ovs_internal_dev_rtnl_link_register();
2c8c4fb7
AZ
2578 if (err)
2579 goto error_action_fifos_exit;
2580
5282e284
TG
2581 err = ovs_flow_init();
2582 if (err)
2583 goto error_unreg_rtnl_link;
2584
850b6b3b 2585 err = ovs_vport_init();
064af421
BP
2586 if (err)
2587 goto error_flow_exit;
2588
2a4999f3 2589 err = register_pernet_device(&ovs_net_ops);
f2459fe7
JG
2590 if (err)
2591 goto error_vport_exit;
2592
7f4a5d68 2593 err = compat_init();
2a4999f3
PS
2594 if (err)
2595 goto error_netns_exit;
2596
7f4a5d68 2597 err = register_netdevice_notifier(&ovs_dp_device_notifier);
2598 if (err)
2599 goto error_compat_exit;
2600
5a38795f
TG
2601 err = ovs_netdev_init();
2602 if (err)
2603 goto error_unreg_notifier;
2604
982b8810
BP
2605 err = dp_register_genl();
2606 if (err < 0)
5a38795f 2607 goto error_unreg_netdev;
982b8810 2608
064af421
BP
2609 return 0;
2610
5a38795f
TG
2611error_unreg_netdev:
2612 ovs_netdev_exit();
064af421 2613error_unreg_notifier:
850b6b3b 2614 unregister_netdevice_notifier(&ovs_dp_device_notifier);
7f4a5d68 2615error_compat_exit:
2616 compat_exit();
2a4999f3
PS
2617error_netns_exit:
2618 unregister_pernet_device(&ovs_net_ops);
f2459fe7 2619error_vport_exit:
850b6b3b 2620 ovs_vport_exit();
064af421 2621error_flow_exit:
850b6b3b 2622 ovs_flow_exit();
5282e284
TG
2623error_unreg_rtnl_link:
2624 ovs_internal_dev_rtnl_link_unregister();
2c8c4fb7
AZ
2625error_action_fifos_exit:
2626 action_fifos_exit();
064af421 2627error:
907c26a8 2628 ovs_nsh_cleanup();
064af421
BP
2629 return err;
2630}
2631
2632static void dp_cleanup(void)
2633{
982b8810 2634 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
5a38795f 2635 ovs_netdev_exit();
850b6b3b 2636 unregister_netdevice_notifier(&ovs_dp_device_notifier);
7f4a5d68 2637 compat_exit();
2a4999f3
PS
2638 unregister_pernet_device(&ovs_net_ops);
2639 rcu_barrier();
850b6b3b
JG
2640 ovs_vport_exit();
2641 ovs_flow_exit();
5282e284 2642 ovs_internal_dev_rtnl_link_unregister();
2c8c4fb7 2643 action_fifos_exit();
907c26a8 2644 ovs_nsh_cleanup();
064af421
BP
2645}
2646
2647module_init(dp_init);
2648module_exit(dp_cleanup);
2649
2650MODULE_DESCRIPTION("Open vSwitch switching datapath");
2651MODULE_LICENSE("GPL");
3d0666d2 2652MODULE_VERSION(VERSION);
75e2077e
TLSC
2653MODULE_ALIAS_GENL_FAMILY(OVS_DATAPATH_FAMILY);
2654MODULE_ALIAS_GENL_FAMILY(OVS_VPORT_FAMILY);
2655MODULE_ALIAS_GENL_FAMILY(OVS_FLOW_FAMILY);
2656MODULE_ALIAS_GENL_FAMILY(OVS_PACKET_FAMILY);
1cb57039 2657MODULE_ALIAS_GENL_FAMILY(OVS_METER_FAMILY);
cb2a5486 2658MODULE_ALIAS_GENL_FAMILY(OVS_CT_LIMIT_FAMILY);