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