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