]> git.proxmox.com Git - mirror_ovs.git/blame - datapath/datapath.c
datapath: support asymmetric conntrack
[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 1416 rcu_read_unlock();
096043f4
PA
1417 if (WARN_ON_ONCE(err < 0)) {
1418 kfree_skb(reply);
1419 goto out_free;
1420 }
cb25142c 1421 ovs_notify(&dp_flow_genl_family, &ovs_dp_flow_multicast_group, reply, info);
cde7f3ba 1422 } else {
cb25142c
PS
1423 genl_set_err(&dp_flow_genl_family, sock_net(skb->sk), 0,
1424 GROUP_ID(&ovs_dp_flow_multicast_group), PTR_ERR(reply));
1425
cde7f3ba 1426 }
afad3556 1427 }
37a1300c 1428
096043f4 1429out_free:
a1c564be 1430 ovs_flow_free(flow, true);
37a1300c 1431 return 0;
cd2a59e9
PS
1432unlock:
1433 ovs_unlock();
1434 return err;
37a1300c
BP
1435}
1436
df2c07f4 1437static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
37a1300c 1438{
bc619e29 1439 struct nlattr *a[__OVS_FLOW_ATTR_MAX];
df2c07f4 1440 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
994dc286 1441 struct table_instance *ti;
37a1300c 1442 struct datapath *dp;
bc619e29
JS
1443 u32 ufid_flags;
1444 int err;
1445
6db0f72d
JB
1446 err = genlmsg_parse_deprecated(cb->nlh, &dp_flow_genl_family, a,
1447 OVS_FLOW_ATTR_MAX, flow_policy, NULL);
bc619e29
JS
1448 if (err)
1449 return err;
1450 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
37a1300c 1451
f44ccce1 1452 rcu_read_lock();
01ac0970 1453 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
cd2a59e9 1454 if (!dp) {
f44ccce1 1455 rcu_read_unlock();
37a1300c 1456 return -ENODEV;
cd2a59e9 1457 }
37a1300c 1458
994dc286 1459 ti = rcu_dereference(dp->table.ti);
37a1300c 1460 for (;;) {
37a1300c
BP
1461 struct sw_flow *flow;
1462 u32 bucket, obj;
1463
1464 bucket = cb->args[0];
1465 obj = cb->args[1];
994dc286 1466 flow = ovs_flow_tbl_dump_next(ti, &bucket, &obj);
3544358a 1467 if (!flow)
37a1300c
BP
1468 break;
1469
2c622e5a 1470 if (ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex, skb,
28aea917 1471 NETLINK_CB(cb->skb).portid,
37a1300c 1472 cb->nlh->nlmsg_seq, NLM_F_MULTI,
229486c2 1473 OVS_FLOW_CMD_GET, ufid_flags) < 0)
37a1300c
BP
1474 break;
1475
1476 cb->args[0] = bucket;
1477 cb->args[1] = obj;
1478 }
f44ccce1 1479 rcu_read_unlock();
37a1300c 1480 return skb->len;
704a1e09
BP
1481}
1482
cb25142c
PS
1483static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
1484 [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
9233cef7 1485 [OVS_FLOW_ATTR_MASK] = { .type = NLA_NESTED },
cb25142c
PS
1486 [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
1487 [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
9233cef7 1488 [OVS_FLOW_ATTR_PROBE] = { .type = NLA_FLAG },
bc619e29
JS
1489 [OVS_FLOW_ATTR_UFID] = { .type = NLA_UNSPEC, .len = 1 },
1490 [OVS_FLOW_ATTR_UFID_FLAGS] = { .type = NLA_U32 },
cb25142c
PS
1491};
1492
e515e66a 1493static const struct genl_ops dp_flow_genl_ops[] = {
df2c07f4 1494 { .cmd = OVS_FLOW_CMD_NEW,
f1e9590e
YHW
1495#ifdef HAVE_GENL_VALIDATE_FLAGS
1496 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1497#endif
a6a8674d 1498 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2ef0f1c2 1499#ifdef HAVE_GENL_OPS_POLICY
37a1300c 1500 .policy = flow_policy,
2ef0f1c2 1501#endif
0c9fd022 1502 .doit = ovs_flow_cmd_new
37a1300c 1503 },
df2c07f4 1504 { .cmd = OVS_FLOW_CMD_DEL,
f1e9590e
YHW
1505#ifdef HAVE_GENL_VALIDATE_FLAGS
1506 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1507#endif
a6a8674d 1508 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2ef0f1c2 1509#ifdef HAVE_GENL_OPS_POLICY
37a1300c 1510 .policy = flow_policy,
2ef0f1c2 1511#endif
df2c07f4 1512 .doit = ovs_flow_cmd_del
37a1300c 1513 },
df2c07f4 1514 { .cmd = OVS_FLOW_CMD_GET,
f1e9590e
YHW
1515#ifdef HAVE_GENL_VALIDATE_FLAGS
1516 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1517#endif
37a1300c 1518 .flags = 0, /* OK for unprivileged users. */
2ef0f1c2 1519#ifdef HAVE_GENL_OPS_POLICY
37a1300c 1520 .policy = flow_policy,
2ef0f1c2 1521#endif
df2c07f4
JP
1522 .doit = ovs_flow_cmd_get,
1523 .dumpit = ovs_flow_cmd_dump
37a1300c 1524 },
df2c07f4 1525 { .cmd = OVS_FLOW_CMD_SET,
f1e9590e
YHW
1526#ifdef HAVE_GENL_VALIDATE_FLAGS
1527 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1528#endif
a6a8674d 1529 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2ef0f1c2 1530#ifdef HAVE_GENL_OPS_POLICY
37a1300c 1531 .policy = flow_policy,
2ef0f1c2 1532#endif
0c9fd022 1533 .doit = ovs_flow_cmd_set,
37a1300c
BP
1534 },
1535};
1536
ba63fe26 1537static struct genl_family dp_flow_genl_family __ro_after_init = {
df2c07f4 1538 .hdrsize = sizeof(struct ovs_header),
cb25142c
PS
1539 .name = OVS_FLOW_FAMILY,
1540 .version = OVS_FLOW_VERSION,
1541 .maxattr = OVS_FLOW_ATTR_MAX,
2ef0f1c2
JB
1542#ifndef HAVE_GENL_OPS_POLICY
1543 .policy = flow_policy,
1544#endif
b3dcb73c 1545 .netnsok = true,
cb25142c
PS
1546 .parallel_ops = true,
1547 .ops = dp_flow_genl_ops,
1548 .n_ops = ARRAY_SIZE(dp_flow_genl_ops),
1549 .mcgrps = &ovs_dp_flow_multicast_group,
1550 .n_mcgrps = 1,
ba63fe26 1551 .module = THIS_MODULE,
aaff4b55
BP
1552};
1553
0afa2373
TG
1554static size_t ovs_dp_cmd_msg_size(void)
1555{
1556 size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
1557
1558 msgsize += nla_total_size(IFNAMSIZ);
91b37647
PS
1559 msgsize += nla_total_size_64bit(sizeof(struct ovs_dp_stats));
1560 msgsize += nla_total_size_64bit(sizeof(struct ovs_dp_megaflow_stats));
300af20a 1561 msgsize += nla_total_size(sizeof(u32)); /* OVS_DP_ATTR_USER_FEATURES */
0afa2373
TG
1562
1563 return msgsize;
1564}
1565
d637497c 1566/* Called with ovs_mutex. */
df2c07f4 1567static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
28aea917 1568 u32 portid, u32 seq, u32 flags, u8 cmd)
064af421 1569{
df2c07f4 1570 struct ovs_header *ovs_header;
e926dfe3 1571 struct ovs_dp_stats dp_stats;
4fa72a95 1572 struct ovs_dp_megaflow_stats dp_megaflow_stats;
064af421
BP
1573 int err;
1574
28aea917 1575 ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
aaff4b55 1576 flags, cmd);
df2c07f4 1577 if (!ovs_header)
aaff4b55 1578 goto error;
064af421 1579
b063d9f0 1580 ovs_header->dp_ifindex = get_dpifindex(dp);
064af421 1581
850b6b3b 1582 err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
064af421 1583 if (err)
d6569377 1584 goto nla_put_failure;
064af421 1585
4fa72a95 1586 get_dp_stats(dp, &dp_stats, &dp_megaflow_stats);
91b37647
PS
1587 if (nla_put_64bit(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats),
1588 &dp_stats, OVS_DP_ATTR_PAD))
4fa72a95
AZ
1589 goto nla_put_failure;
1590
91b37647
PS
1591 if (nla_put_64bit(skb, OVS_DP_ATTR_MEGAFLOW_STATS,
1592 sizeof(struct ovs_dp_megaflow_stats),
1593 &dp_megaflow_stats, OVS_DP_ATTR_PAD))
c3cc8c03 1594 goto nla_put_failure;
d6569377 1595
c58cc9a4
TG
1596 if (nla_put_u32(skb, OVS_DP_ATTR_USER_FEATURES, dp->user_features))
1597 goto nla_put_failure;
1598
23b48dc1
TG
1599 genlmsg_end(skb, ovs_header);
1600 return 0;
d6569377
BP
1601
1602nla_put_failure:
df2c07f4 1603 genlmsg_cancel(skb, ovs_header);
aaff4b55
BP
1604error:
1605 return -EMSGSIZE;
d6569377
BP
1606}
1607
40c08cda 1608static struct sk_buff *ovs_dp_cmd_alloc_info(void)
d6569377 1609{
40c08cda 1610 return genlmsg_new(ovs_dp_cmd_msg_size(), GFP_KERNEL);
aaff4b55 1611}
9dca7bd5 1612
aa917006 1613/* Called with rcu_read_lock or ovs_mutex. */
2a4999f3 1614static struct datapath *lookup_datapath(struct net *net,
f1f60b85 1615 const struct ovs_header *ovs_header,
6455100f 1616 struct nlattr *a[OVS_DP_ATTR_MAX + 1])
d6569377 1617{
254f2dc8
BP
1618 struct datapath *dp;
1619
df2c07f4 1620 if (!a[OVS_DP_ATTR_NAME])
2a4999f3 1621 dp = get_dp(net, ovs_header->dp_ifindex);
254f2dc8 1622 else {
d6569377 1623 struct vport *vport;
d6569377 1624
2a4999f3 1625 vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
df2c07f4 1626 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
d6569377 1627 }
254f2dc8 1628 return dp ? dp : ERR_PTR(-ENODEV);
d6569377
BP
1629}
1630
94358dcf
TG
1631static void ovs_dp_reset_user_features(struct sk_buff *skb, struct genl_info *info)
1632{
1633 struct datapath *dp;
1634
1635 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
09350a3d 1636 if (IS_ERR(dp))
94358dcf
TG
1637 return;
1638
1639 WARN(dp->user_features, "Dropping previously announced user features\n");
1640 dp->user_features = 0;
1641}
1642
dca96842
PB
1643DEFINE_STATIC_KEY_FALSE(tc_recirc_sharing_support);
1644
1645static int ovs_dp_change(struct datapath *dp, struct nlattr *a[])
c58cc9a4 1646{
dca96842
PB
1647 u32 user_features = 0;
1648
1649 if (a[OVS_DP_ATTR_USER_FEATURES]) {
1650 user_features = nla_get_u32(a[OVS_DP_ATTR_USER_FEATURES]);
1651
1652 if (user_features & ~(OVS_DP_F_VPORT_PIDS |
1653 OVS_DP_F_UNALIGNED |
1654 OVS_DP_F_TC_RECIRC_SHARING))
1655 return -EOPNOTSUPP;
1656
1657#if !IS_ENABLED(CONFIG_NET_TC_SKB_EXT)
1658 if (user_features & OVS_DP_F_TC_RECIRC_SHARING)
1659 return -EOPNOTSUPP;
1660#endif
1661 }
1662
1663 dp->user_features = user_features;
1664
1665 if (dp->user_features & OVS_DP_F_TC_RECIRC_SHARING)
1666 static_branch_enable(&tc_recirc_sharing_support);
1667 else
1668 static_branch_disable(&tc_recirc_sharing_support);
1669
1670 return 0;
c58cc9a4
TG
1671}
1672
a3839b26
TZ
1673static int ovs_dp_stats_init(struct datapath *dp)
1674{
1675 dp->stats_percpu = netdev_alloc_pcpu_stats(struct dp_stats_percpu);
1676 if (!dp->stats_percpu)
1677 return -ENOMEM;
1678
1679 return 0;
1680}
1681
1682static int ovs_dp_vport_init(struct datapath *dp)
1683{
1684 int i;
1685
1686 dp->ports = kmalloc_array(DP_VPORT_HASH_BUCKETS,
1687 sizeof(struct hlist_head),
1688 GFP_KERNEL);
1689 if (!dp->ports)
1690 return -ENOMEM;
1691
1692 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1693 INIT_HLIST_HEAD(&dp->ports[i]);
1694
1695 return 0;
1696}
1697
df2c07f4 1698static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
d6569377 1699{
aaff4b55 1700 struct nlattr **a = info->attrs;
d6569377 1701 struct vport_parms parms;
aaff4b55 1702 struct sk_buff *reply;
d6569377
BP
1703 struct datapath *dp;
1704 struct vport *vport;
2a4999f3 1705 struct ovs_net *ovs_net;
a3839b26 1706 int err;
d6569377 1707
d6569377 1708 err = -EINVAL;
ea36840f 1709 if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
aaff4b55
BP
1710 goto err;
1711
40c08cda 1712 reply = ovs_dp_cmd_alloc_info();
d81eef1b
JR
1713 if (!reply)
1714 return -ENOMEM;
d6569377 1715
d6569377
BP
1716 err = -ENOMEM;
1717 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1718 if (dp == NULL)
a3839b26 1719 goto err_destroy_reply;
2a4999f3 1720
c0cddcec 1721 ovs_dp_set_net(dp, sock_net(skb->sk));
0ceaa66c 1722
d6569377 1723 /* Allocate table. */
994dc286
PS
1724 err = ovs_flow_tbl_init(&dp->table);
1725 if (err)
a3839b26 1726 goto err_destroy_dp;
d6569377 1727
a3839b26
TZ
1728 err = ovs_dp_stats_init(dp);
1729 if (err)
99769a40 1730 goto err_destroy_table;
99769a40 1731
a3839b26
TZ
1732 err = ovs_dp_vport_init(dp);
1733 if (err)
1734 goto err_destroy_stats;
95b1d73a 1735
1cb57039
AZ
1736 err = ovs_meters_init(dp);
1737 if (err)
a3839b26 1738 goto err_destroy_ports;
1cb57039 1739
d6569377 1740 /* Set up our datapath device. */
df2c07f4
JP
1741 parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1742 parms.type = OVS_VPORT_TYPE_INTERNAL;
d6569377
BP
1743 parms.options = NULL;
1744 parms.dp = dp;
df2c07f4 1745 parms.port_no = OVSP_LOCAL;
beb1c69a 1746 parms.upcall_portids = a[OVS_DP_ATTR_UPCALL_PID];
b063d9f0 1747
dca96842
PB
1748 err = ovs_dp_change(dp, a);
1749 if (err)
1750 goto err_destroy_meters;
c58cc9a4 1751
d81eef1b
JR
1752 /* So far only local changes have been made, now need the lock. */
1753 ovs_lock();
1754
d6569377
BP
1755 vport = new_vport(&parms);
1756 if (IS_ERR(vport)) {
1757 err = PTR_ERR(vport);
1758 if (err == -EBUSY)
1759 err = -EEXIST;
1760
94358dcf
TG
1761 if (err == -EEXIST) {
1762 /* An outdated user space instance that does not understand
1763 * the concept of user_features has attempted to create a new
1764 * datapath and is likely to reuse it. Drop all user features.
1765 */
1766 if (info->genlhdr->version < OVS_DP_VER_FEATURES)
1767 ovs_dp_reset_user_features(skb, info);
1768 }
1769
c07157ca 1770 ovs_unlock();
1cb57039 1771 goto err_destroy_meters;
d6569377 1772 }
d6569377 1773
d81eef1b
JR
1774 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1775 info->snd_seq, 0, OVS_DP_CMD_NEW);
1776 BUG_ON(err < 0);
aaff4b55 1777
2a4999f3 1778 ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
fb93e9aa 1779 list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
a0fb56c1 1780
cd2a59e9 1781 ovs_unlock();
d6569377 1782
cb25142c 1783 ovs_notify(&dp_datapath_genl_family, &ovs_dp_datapath_multicast_group, reply, info);
d6569377
BP
1784 return 0;
1785
1cb57039 1786err_destroy_meters:
1cb57039 1787 ovs_meters_exit(dp);
a3839b26 1788err_destroy_ports:
95b1d73a 1789 kfree(dp->ports);
a3839b26 1790err_destroy_stats:
99769a40 1791 free_percpu(dp->stats_percpu);
d6569377 1792err_destroy_table:
e379e4d1 1793 ovs_flow_tbl_destroy(&dp->table);
a3839b26 1794err_destroy_dp:
d6569377 1795 kfree(dp);
a3839b26 1796err_destroy_reply:
d81eef1b 1797 kfree_skb(reply);
d6569377 1798err:
064af421
BP
1799 return err;
1800}
1801
cd2a59e9 1802/* Called with ovs_mutex. */
2a4999f3 1803static void __dp_destroy(struct datapath *dp)
44e05eca 1804{
95b1d73a 1805 int i;
44e05eca 1806
95b1d73a
PS
1807 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1808 struct vport *vport;
f8dfbcb7 1809 struct hlist_node *n;
95b1d73a 1810
f8dfbcb7 1811 hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
95b1d73a
PS
1812 if (vport->port_no != OVSP_LOCAL)
1813 ovs_dp_detach_port(vport);
1814 }
ed099e92 1815
fb93e9aa 1816 list_del_rcu(&dp->list_node);
ed099e92 1817
cd2a59e9 1818 /* OVSP_LOCAL is datapath internal port. We need to make sure that
d103f479
AZ
1819 * all ports in datapath are destroyed first before freeing datapath.
1820 */
cd2a59e9 1821 ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
99620d2c 1822
d103f479 1823 /* RCU destroy the flow table */
ed099e92 1824 call_rcu(&dp->rcu, destroy_dp_rcu);
2a4999f3
PS
1825}
1826
1827static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1828{
1829 struct sk_buff *reply;
1830 struct datapath *dp;
1831 int err;
1832
40c08cda 1833 reply = ovs_dp_cmd_alloc_info();
d81eef1b
JR
1834 if (!reply)
1835 return -ENOMEM;
1836
cd2a59e9 1837 ovs_lock();
2a4999f3
PS
1838 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1839 err = PTR_ERR(dp);
1840 if (IS_ERR(dp))
d81eef1b 1841 goto err_unlock_free;
2a4999f3 1842
d81eef1b
JR
1843 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1844 info->snd_seq, 0, OVS_DP_CMD_DEL);
1845 BUG_ON(err < 0);
2a4999f3
PS
1846
1847 __dp_destroy(dp);
d81eef1b 1848 ovs_unlock();
7d16c847 1849
cb25142c 1850 ovs_notify(&dp_datapath_genl_family, &ovs_dp_datapath_multicast_group, reply, info);
99620d2c 1851 return 0;
d81eef1b
JR
1852
1853err_unlock_free:
cd2a59e9 1854 ovs_unlock();
d81eef1b 1855 kfree_skb(reply);
cd2a59e9 1856 return err;
44e05eca
BP
1857}
1858
df2c07f4 1859static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
064af421 1860{
aaff4b55 1861 struct sk_buff *reply;
d6569377 1862 struct datapath *dp;
d6569377 1863 int err;
064af421 1864
40c08cda 1865 reply = ovs_dp_cmd_alloc_info();
d81eef1b
JR
1866 if (!reply)
1867 return -ENOMEM;
1868
cd2a59e9 1869 ovs_lock();
2a4999f3 1870 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
cd2a59e9 1871 err = PTR_ERR(dp);
d6569377 1872 if (IS_ERR(dp))
d81eef1b 1873 goto err_unlock_free;
38c6ecbc 1874
dca96842
PB
1875 err = ovs_dp_change(dp, info->attrs);
1876 if (err)
1877 goto err_unlock_free;
c58cc9a4 1878
d81eef1b 1879 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
229486c2 1880 info->snd_seq, 0, OVS_DP_CMD_GET);
d81eef1b 1881 BUG_ON(err < 0);
a0fb56c1 1882
cd2a59e9 1883 ovs_unlock();
7d16c847 1884
cb25142c 1885 ovs_notify(&dp_datapath_genl_family, &ovs_dp_datapath_multicast_group, reply, info);
aaff4b55 1886 return 0;
d81eef1b
JR
1887
1888err_unlock_free:
cd2a59e9 1889 ovs_unlock();
d81eef1b 1890 kfree_skb(reply);
cd2a59e9 1891 return err;
064af421
BP
1892}
1893
df2c07f4 1894static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1dcf111b 1895{
aaff4b55 1896 struct sk_buff *reply;
d6569377 1897 struct datapath *dp;
d6569377 1898 int err;
1dcf111b 1899
40c08cda 1900 reply = ovs_dp_cmd_alloc_info();
d81eef1b
JR
1901 if (!reply)
1902 return -ENOMEM;
1903
d637497c 1904 ovs_lock();
2a4999f3 1905 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
cd2a59e9
PS
1906 if (IS_ERR(dp)) {
1907 err = PTR_ERR(dp);
d81eef1b 1908 goto err_unlock_free;
cd2a59e9 1909 }
d81eef1b 1910 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
229486c2 1911 info->snd_seq, 0, OVS_DP_CMD_GET);
d81eef1b 1912 BUG_ON(err < 0);
d637497c 1913 ovs_unlock();
aaff4b55
BP
1914
1915 return genlmsg_reply(reply, info);
cd2a59e9 1916
d81eef1b 1917err_unlock_free:
d637497c 1918 ovs_unlock();
d81eef1b 1919 kfree_skb(reply);
cd2a59e9 1920 return err;
1dcf111b
JP
1921}
1922
df2c07f4 1923static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
a7786963 1924{
2a4999f3 1925 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
254f2dc8
BP
1926 struct datapath *dp;
1927 int skip = cb->args[0];
1928 int i = 0;
a7786963 1929
d637497c
PS
1930 ovs_lock();
1931 list_for_each_entry(dp, &ovs_net->dps, list_node) {
a2bab2f0 1932 if (i >= skip &&
28aea917 1933 ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
aaff4b55 1934 cb->nlh->nlmsg_seq, NLM_F_MULTI,
229486c2 1935 OVS_DP_CMD_GET) < 0)
aaff4b55 1936 break;
254f2dc8 1937 i++;
a7786963 1938 }
d637497c 1939 ovs_unlock();
aaff4b55 1940
254f2dc8
BP
1941 cb->args[0] = i;
1942
aaff4b55 1943 return skb->len;
c19e6535
BP
1944}
1945
cb25142c
PS
1946static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1947 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1948 [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1949 [OVS_DP_ATTR_USER_FEATURES] = { .type = NLA_U32 },
1950};
1951
e515e66a 1952static const struct genl_ops dp_datapath_genl_ops[] = {
df2c07f4 1953 { .cmd = OVS_DP_CMD_NEW,
f1e9590e
YHW
1954#ifdef HAVE_GENL_VALIDATE_FLAGS
1955 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1956#endif
a6a8674d 1957 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2ef0f1c2 1958#ifdef HAVE_GENL_OPS_POLICY
aaff4b55 1959 .policy = datapath_policy,
2ef0f1c2 1960#endif
df2c07f4 1961 .doit = ovs_dp_cmd_new
aaff4b55 1962 },
df2c07f4 1963 { .cmd = OVS_DP_CMD_DEL,
f1e9590e
YHW
1964#ifdef HAVE_GENL_VALIDATE_FLAGS
1965 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1966#endif
a6a8674d 1967 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2ef0f1c2 1968#ifdef HAVE_GENL_OPS_POLICY
aaff4b55 1969 .policy = datapath_policy,
2ef0f1c2 1970#endif
df2c07f4 1971 .doit = ovs_dp_cmd_del
aaff4b55 1972 },
df2c07f4 1973 { .cmd = OVS_DP_CMD_GET,
f1e9590e
YHW
1974#ifdef HAVE_GENL_VALIDATE_FLAGS
1975 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1976#endif
aaff4b55 1977 .flags = 0, /* OK for unprivileged users. */
2ef0f1c2 1978#ifdef HAVE_GENL_OPS_POLICY
aaff4b55 1979 .policy = datapath_policy,
2ef0f1c2 1980#endif
df2c07f4
JP
1981 .doit = ovs_dp_cmd_get,
1982 .dumpit = ovs_dp_cmd_dump
aaff4b55 1983 },
df2c07f4 1984 { .cmd = OVS_DP_CMD_SET,
f1e9590e
YHW
1985#ifdef HAVE_GENL_VALIDATE_FLAGS
1986 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
1987#endif
a6a8674d 1988 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2ef0f1c2 1989#ifdef HAVE_GENL_OPS_POLICY
aaff4b55 1990 .policy = datapath_policy,
2ef0f1c2 1991#endif
df2c07f4 1992 .doit = ovs_dp_cmd_set,
aaff4b55
BP
1993 },
1994};
1995
ba63fe26 1996static struct genl_family dp_datapath_genl_family __ro_after_init = {
df2c07f4 1997 .hdrsize = sizeof(struct ovs_header),
cb25142c
PS
1998 .name = OVS_DATAPATH_FAMILY,
1999 .version = OVS_DATAPATH_VERSION,
2000 .maxattr = OVS_DP_ATTR_MAX,
2ef0f1c2
JB
2001#ifndef HAVE_GENL_OPS_POLICY
2002 .policy = datapath_policy,
2003#endif
b3dcb73c 2004 .netnsok = true,
cb25142c
PS
2005 .parallel_ops = true,
2006 .ops = dp_datapath_genl_ops,
2007 .n_ops = ARRAY_SIZE(dp_datapath_genl_ops),
2008 .mcgrps = &ovs_dp_datapath_multicast_group,
2009 .n_mcgrps = 1,
ba63fe26 2010 .module = THIS_MODULE,
f0fef760
BP
2011};
2012
cd2a59e9 2013/* Called with ovs_mutex or RCU read lock. */
df2c07f4 2014static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
b147f2e9 2015 struct net *net, u32 portid, u32 seq,
b7465cac 2016 u32 flags, u8 cmd, gfp_t gfp)
064af421 2017{
df2c07f4 2018 struct ovs_header *ovs_header;
e926dfe3 2019 struct ovs_vport_stats vport_stats;
c19e6535
BP
2020 int err;
2021
28aea917 2022 ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
f0fef760 2023 flags, cmd);
df2c07f4 2024 if (!ovs_header)
f0fef760 2025 return -EMSGSIZE;
c19e6535 2026
99769a40 2027 ovs_header->dp_ifindex = get_dpifindex(vport->dp);
c19e6535 2028
c3cc8c03
DM
2029 if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
2030 nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
e23775f2 2031 nla_put_string(skb, OVS_VPORT_ATTR_NAME,
b147f2e9
JB
2032 ovs_vport_name(vport)) ||
2033 nla_put_u32(skb, OVS_VPORT_ATTR_IFINDEX, vport->dev->ifindex))
c3cc8c03 2034 goto nla_put_failure;
c19e6535 2035
b147f2e9
JB
2036#ifdef HAVE_PEERNET2ID_ALLOC
2037 if (!net_eq(net, dev_net(vport->dev))) {
b7465cac 2038 int id = peernet2id_alloc(net, dev_net(vport->dev), gfp);
b147f2e9
JB
2039
2040 if (nla_put_s32(skb, OVS_VPORT_ATTR_NETNSID, id))
2041 goto nla_put_failure;
2042 }
2043
2044#endif
850b6b3b 2045 ovs_vport_get_stats(vport, &vport_stats);
91b37647
PS
2046 if (nla_put_64bit(skb, OVS_VPORT_ATTR_STATS,
2047 sizeof(struct ovs_vport_stats), &vport_stats,
2048 OVS_VPORT_ATTR_PAD))
c3cc8c03 2049 goto nla_put_failure;
c19e6535 2050
beb1c69a
AW
2051 if (ovs_vport_get_upcall_portids(vport, skb))
2052 goto nla_put_failure;
2053
850b6b3b 2054 err = ovs_vport_get_options(vport, skb);
f0fef760
BP
2055 if (err == -EMSGSIZE)
2056 goto error;
c19e6535 2057
23b48dc1
TG
2058 genlmsg_end(skb, ovs_header);
2059 return 0;
c19e6535
BP
2060
2061nla_put_failure:
2062 err = -EMSGSIZE;
f0fef760 2063error:
df2c07f4 2064 genlmsg_cancel(skb, ovs_header);
f0fef760 2065 return err;
064af421
BP
2066}
2067
d81eef1b
JR
2068static struct sk_buff *ovs_vport_cmd_alloc_info(void)
2069{
2070 return nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2071}
2072
2073/* Called with ovs_mutex, only via ovs_dp_notify_wq(). */
b147f2e9
JB
2074struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, struct net *net,
2075 u32 portid, u32 seq, u8 cmd)
064af421 2076{
c19e6535 2077 struct sk_buff *skb;
f0fef760 2078 int retval;
c19e6535 2079
b7465cac 2080 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
c19e6535
BP
2081 if (!skb)
2082 return ERR_PTR(-ENOMEM);
2083
b7465cac
GN
2084 retval = ovs_vport_cmd_fill_info(vport, skb, net, portid, seq, 0, cmd,
2085 GFP_KERNEL);
c25ea534
JG
2086 BUG_ON(retval < 0);
2087
c19e6535 2088 return skb;
f0fef760 2089}
c19e6535 2090
cd2a59e9 2091/* Called with ovs_mutex or RCU read lock. */
2a4999f3 2092static struct vport *lookup_vport(struct net *net,
f1f60b85 2093 const struct ovs_header *ovs_header,
df2c07f4 2094 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
c19e6535
BP
2095{
2096 struct datapath *dp;
2097 struct vport *vport;
2098
b147f2e9
JB
2099 if (a[OVS_VPORT_ATTR_IFINDEX])
2100 return ERR_PTR(-EOPNOTSUPP);
df2c07f4 2101 if (a[OVS_VPORT_ATTR_NAME]) {
2a4999f3 2102 vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
ed099e92 2103 if (!vport)
c19e6535 2104 return ERR_PTR(-ENODEV);
24ce832d
BP
2105 if (ovs_header->dp_ifindex &&
2106 ovs_header->dp_ifindex != get_dpifindex(vport->dp))
2107 return ERR_PTR(-ENODEV);
c19e6535 2108 return vport;
df2c07f4
JP
2109 } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
2110 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
c19e6535
BP
2111
2112 if (port_no >= DP_MAX_PORTS)
f0fef760 2113 return ERR_PTR(-EFBIG);
c19e6535 2114
2a4999f3 2115 dp = get_dp(net, ovs_header->dp_ifindex);
c19e6535
BP
2116 if (!dp)
2117 return ERR_PTR(-ENODEV);
f2459fe7 2118
cd2a59e9 2119 vport = ovs_vport_ovsl_rcu(dp, port_no);
ed099e92 2120 if (!vport)
17535c57 2121 return ERR_PTR(-ENODEV);
c19e6535
BP
2122 return vport;
2123 } else
2124 return ERR_PTR(-EINVAL);
b147f2e9 2125
064af421
BP
2126}
2127
ec335e0b 2128static unsigned int ovs_get_max_headroom(struct datapath *dp)
8ce37339 2129{
ec335e0b 2130 unsigned int dev_headroom, max_headroom = 0;
8ce37339
PS
2131 struct net_device *dev;
2132 struct vport *vport;
2133 int i;
2134
2135 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
2136 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
2137 dev = vport->dev;
2138 dev_headroom = netdev_get_fwd_headroom(dev);
2139 if (dev_headroom > max_headroom)
2140 max_headroom = dev_headroom;
2141 }
2142 }
2143
ec335e0b
TY
2144 return max_headroom;
2145}
2146
2147/* Called with ovs_mutex */
2148static void ovs_update_headroom(struct datapath *dp, unsigned int new_headroom)
2149{
2150 struct vport *vport;
2151 int i;
2152
2153 dp->max_headroom = new_headroom;
8ce37339
PS
2154 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
2155 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node)
ec335e0b 2156 netdev_set_rx_headroom(vport->dev, new_headroom);
8ce37339
PS
2157}
2158
df2c07f4 2159static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
c19e6535 2160{
f0fef760 2161 struct nlattr **a = info->attrs;
df2c07f4 2162 struct ovs_header *ovs_header = info->userhdr;
c19e6535 2163 struct vport_parms parms;
ed099e92 2164 struct sk_buff *reply;
c19e6535 2165 struct vport *vport;
c19e6535 2166 struct datapath *dp;
ec335e0b 2167 unsigned int new_headroom;
b0ec0f27 2168 u32 port_no;
c19e6535 2169 int err;
b0ec0f27 2170
ea36840f
BP
2171 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
2172 !a[OVS_VPORT_ATTR_UPCALL_PID])
d81eef1b 2173 return -EINVAL;
b147f2e9
JB
2174 if (a[OVS_VPORT_ATTR_IFINDEX])
2175 return -EOPNOTSUPP;
d81eef1b
JR
2176
2177 port_no = a[OVS_VPORT_ATTR_PORT_NO]
2178 ? nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]) : 0;
2179 if (port_no >= DP_MAX_PORTS)
2180 return -EFBIG;
2181
2182 reply = ovs_vport_cmd_alloc_info();
2183 if (!reply)
2184 return -ENOMEM;
f0fef760 2185
cd2a59e9 2186 ovs_lock();
5a38795f 2187restart:
2a4999f3 2188 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
c19e6535
BP
2189 err = -ENODEV;
2190 if (!dp)
d81eef1b 2191 goto exit_unlock_free;
c19e6535 2192
d81eef1b 2193 if (port_no) {
cd2a59e9 2194 vport = ovs_vport_ovsl(dp, port_no);
c19e6535
BP
2195 err = -EBUSY;
2196 if (vport)
d81eef1b 2197 goto exit_unlock_free;
c19e6535
BP
2198 } else {
2199 for (port_no = 1; ; port_no++) {
2200 if (port_no >= DP_MAX_PORTS) {
2201 err = -EFBIG;
d81eef1b 2202 goto exit_unlock_free;
c19e6535 2203 }
cd2a59e9 2204 vport = ovs_vport_ovsl(dp, port_no);
c19e6535
BP
2205 if (!vport)
2206 break;
51d4d598 2207 }
064af421 2208 }
b0ec0f27 2209
df2c07f4
JP
2210 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
2211 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
2212 parms.options = a[OVS_VPORT_ATTR_OPTIONS];
c19e6535
BP
2213 parms.dp = dp;
2214 parms.port_no = port_no;
beb1c69a 2215 parms.upcall_portids = a[OVS_VPORT_ATTR_UPCALL_PID];
c19e6535
BP
2216
2217 vport = new_vport(&parms);
2218 err = PTR_ERR(vport);
5a38795f
TG
2219 if (IS_ERR(vport)) {
2220 if (err == -EAGAIN)
2221 goto restart;
d81eef1b 2222 goto exit_unlock_free;
5a38795f 2223 }
c19e6535 2224
b147f2e9
JB
2225 err = ovs_vport_cmd_fill_info(vport, reply, genl_info_net(info),
2226 info->snd_portid, info->snd_seq, 0,
b7465cac 2227 OVS_VPORT_CMD_NEW, GFP_KERNEL);
d81eef1b 2228 BUG_ON(err < 0);
8ce37339 2229
ec335e0b
TY
2230 new_headroom = netdev_get_fwd_headroom(vport->dev);
2231
2232 if (new_headroom > dp->max_headroom)
2233 ovs_update_headroom(dp, new_headroom);
8ce37339
PS
2234 else
2235 netdev_set_rx_headroom(vport->dev, dp->max_headroom);
2236
d81eef1b 2237 ovs_unlock();
e297c6b7 2238
cb25142c 2239 ovs_notify(&dp_vport_genl_family, &ovs_dp_vport_multicast_group, reply, info);
d81eef1b 2240 return 0;
c19e6535 2241
d81eef1b 2242exit_unlock_free:
cd2a59e9 2243 ovs_unlock();
d81eef1b 2244 kfree_skb(reply);
c19e6535 2245 return err;
44e05eca
BP
2246}
2247
df2c07f4 2248static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
44e05eca 2249{
f0fef760
BP
2250 struct nlattr **a = info->attrs;
2251 struct sk_buff *reply;
c19e6535 2252 struct vport *vport;
c19e6535 2253 int err;
44e05eca 2254
d81eef1b
JR
2255 reply = ovs_vport_cmd_alloc_info();
2256 if (!reply)
2257 return -ENOMEM;
2258
cd2a59e9 2259 ovs_lock();
2a4999f3 2260 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
c19e6535
BP
2261 err = PTR_ERR(vport);
2262 if (IS_ERR(vport))
d81eef1b 2263 goto exit_unlock_free;
44e05eca 2264
6455100f 2265 if (a[OVS_VPORT_ATTR_TYPE] &&
17ec1d04 2266 nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
4879d4c7 2267 err = -EINVAL;
d81eef1b 2268 goto exit_unlock_free;
c25ea534
JG
2269 }
2270
17ec1d04 2271 if (a[OVS_VPORT_ATTR_OPTIONS]) {
850b6b3b 2272 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
17ec1d04 2273 if (err)
d81eef1b 2274 goto exit_unlock_free;
17ec1d04 2275 }
1fc7083d 2276
beb1c69a 2277 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
7d16c847
PS
2278 struct nlattr *ids = a[OVS_VPORT_ATTR_UPCALL_PID];
2279
2280 err = ovs_vport_set_upcall_portids(vport, ids);
beb1c69a
AW
2281 if (err)
2282 goto exit_unlock_free;
2283 }
c19e6535 2284
b147f2e9
JB
2285 err = ovs_vport_cmd_fill_info(vport, reply, genl_info_net(info),
2286 info->snd_portid, info->snd_seq, 0,
b7465cac 2287 OVS_VPORT_CMD_SET, GFP_KERNEL);
c25ea534 2288 BUG_ON(err < 0);
cd2a59e9 2289 ovs_unlock();
d81eef1b 2290
cb25142c 2291 ovs_notify(&dp_vport_genl_family, &ovs_dp_vport_multicast_group, reply, info);
c25ea534
JG
2292 return 0;
2293
d81eef1b 2294exit_unlock_free:
cd2a59e9 2295 ovs_unlock();
d81eef1b 2296 kfree_skb(reply);
c19e6535 2297 return err;
064af421
BP
2298}
2299
df2c07f4 2300static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
7c40efc9 2301{
ec335e0b 2302 bool update_headroom = false;
f0fef760
BP
2303 struct nlattr **a = info->attrs;
2304 struct sk_buff *reply;
8ce37339 2305 struct datapath *dp;
c19e6535 2306 struct vport *vport;
ec335e0b 2307 unsigned int new_headroom;
c19e6535
BP
2308 int err;
2309
d81eef1b
JR
2310 reply = ovs_vport_cmd_alloc_info();
2311 if (!reply)
2312 return -ENOMEM;
2313
cd2a59e9 2314 ovs_lock();
2a4999f3 2315 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
c19e6535 2316 err = PTR_ERR(vport);
f0fef760 2317 if (IS_ERR(vport))
d81eef1b 2318 goto exit_unlock_free;
c19e6535 2319
df2c07f4 2320 if (vport->port_no == OVSP_LOCAL) {
f0fef760 2321 err = -EINVAL;
d81eef1b 2322 goto exit_unlock_free;
f0fef760
BP
2323 }
2324
b147f2e9
JB
2325 err = ovs_vport_cmd_fill_info(vport, reply, genl_info_net(info),
2326 info->snd_portid, info->snd_seq, 0,
b7465cac 2327 OVS_VPORT_CMD_DEL, GFP_KERNEL);
d81eef1b 2328 BUG_ON(err < 0);
8ce37339
PS
2329
2330 /* the vport deletion may trigger dp headroom update */
2331 dp = vport->dp;
2332 if (netdev_get_fwd_headroom(vport->dev) == dp->max_headroom)
ec335e0b
TY
2333 update_headroom = true;
2334
8ce37339 2335 netdev_reset_rx_headroom(vport->dev);
850b6b3b 2336 ovs_dp_detach_port(vport);
8ce37339 2337
ec335e0b
TY
2338 if (update_headroom) {
2339 new_headroom = ovs_get_max_headroom(dp);
8ce37339 2340
ec335e0b
TY
2341 if (new_headroom < dp->max_headroom)
2342 ovs_update_headroom(dp, new_headroom);
2343 }
d81eef1b 2344 ovs_unlock();
f0fef760 2345
cb25142c 2346 ovs_notify(&dp_vport_genl_family, &ovs_dp_vport_multicast_group, reply, info);
d81eef1b 2347 return 0;
f0fef760 2348
d81eef1b 2349exit_unlock_free:
cd2a59e9 2350 ovs_unlock();
d81eef1b 2351 kfree_skb(reply);
c19e6535 2352 return err;
7c40efc9
BP
2353}
2354
df2c07f4 2355static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
7c40efc9 2356{
f0fef760 2357 struct nlattr **a = info->attrs;
df2c07f4 2358 struct ovs_header *ovs_header = info->userhdr;
ed099e92 2359 struct sk_buff *reply;
c19e6535 2360 struct vport *vport;
c19e6535
BP
2361 int err;
2362
d81eef1b
JR
2363 reply = ovs_vport_cmd_alloc_info();
2364 if (!reply)
2365 return -ENOMEM;
2366
ed099e92 2367 rcu_read_lock();
2a4999f3 2368 vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
c19e6535
BP
2369 err = PTR_ERR(vport);
2370 if (IS_ERR(vport))
d81eef1b 2371 goto exit_unlock_free;
b147f2e9
JB
2372 err = ovs_vport_cmd_fill_info(vport, reply, genl_info_net(info),
2373 info->snd_portid, info->snd_seq, 0,
b7465cac 2374 OVS_VPORT_CMD_GET, GFP_ATOMIC);
d81eef1b 2375 BUG_ON(err < 0);
df2fa9b5
JG
2376 rcu_read_unlock();
2377
2378 return genlmsg_reply(reply, info);
ed099e92 2379
d81eef1b 2380exit_unlock_free:
ed099e92 2381 rcu_read_unlock();
d81eef1b 2382 kfree_skb(reply);
c19e6535
BP
2383 return err;
2384}
2385
df2c07f4 2386static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
c19e6535 2387{
df2c07f4 2388 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
c19e6535 2389 struct datapath *dp;
95b1d73a
PS
2390 int bucket = cb->args[0], skip = cb->args[1];
2391 int i, j = 0;
c19e6535 2392
03fc2881 2393 rcu_read_lock();
01ac0970 2394 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
03fc2881
JR
2395 if (!dp) {
2396 rcu_read_unlock();
f0fef760 2397 return -ENODEV;
03fc2881 2398 }
95b1d73a 2399 for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
ed099e92 2400 struct vport *vport;
95b1d73a
PS
2401
2402 j = 0;
f8dfbcb7 2403 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
95b1d73a
PS
2404 if (j >= skip &&
2405 ovs_vport_cmd_fill_info(vport, skb,
b147f2e9 2406 sock_net(skb->sk),
28aea917 2407 NETLINK_CB(cb->skb).portid,
95b1d73a
PS
2408 cb->nlh->nlmsg_seq,
2409 NLM_F_MULTI,
b7465cac
GN
2410 OVS_VPORT_CMD_GET,
2411 GFP_ATOMIC) < 0)
95b1d73a
PS
2412 goto out;
2413
2414 j++;
2415 }
2416 skip = 0;
c19e6535 2417 }
95b1d73a 2418out:
ed099e92 2419 rcu_read_unlock();
c19e6535 2420
95b1d73a
PS
2421 cb->args[0] = i;
2422 cb->args[1] = j;
f0fef760 2423
95b1d73a 2424 return skb->len;
7c40efc9
BP
2425}
2426
cb25142c
PS
2427static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
2428 [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
2429 [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
2430 [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
2431 [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
8fc8986d 2432 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_UNSPEC },
cb25142c 2433 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
b147f2e9
JB
2434 [OVS_VPORT_ATTR_IFINDEX] = { .type = NLA_U32 },
2435 [OVS_VPORT_ATTR_NETNSID] = { .type = NLA_S32 },
cb25142c
PS
2436};
2437
e515e66a 2438static const struct genl_ops dp_vport_genl_ops[] = {
df2c07f4 2439 { .cmd = OVS_VPORT_CMD_NEW,
f1e9590e
YHW
2440#ifdef HAVE_GENL_VALIDATE_FLAGS
2441 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2442#endif
a6a8674d 2443 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2ef0f1c2 2444#ifdef HAVE_GENL_OPS_POLICY
f0fef760 2445 .policy = vport_policy,
2ef0f1c2 2446#endif
df2c07f4 2447 .doit = ovs_vport_cmd_new
f0fef760 2448 },
df2c07f4 2449 { .cmd = OVS_VPORT_CMD_DEL,
f1e9590e
YHW
2450#ifdef HAVE_GENL_VALIDATE_FLAGS
2451 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2452#endif
a6a8674d 2453 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2ef0f1c2 2454#ifdef HAVE_GENL_OPS_POLICY
f0fef760 2455 .policy = vport_policy,
2ef0f1c2 2456#endif
df2c07f4 2457 .doit = ovs_vport_cmd_del
f0fef760 2458 },
df2c07f4 2459 { .cmd = OVS_VPORT_CMD_GET,
f1e9590e
YHW
2460#ifdef HAVE_GENL_VALIDATE_FLAGS
2461 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2462#endif
f0fef760 2463 .flags = 0, /* OK for unprivileged users. */
2ef0f1c2 2464#ifdef HAVE_GENL_OPS_POLICY
f0fef760 2465 .policy = vport_policy,
2ef0f1c2 2466#endif
df2c07f4
JP
2467 .doit = ovs_vport_cmd_get,
2468 .dumpit = ovs_vport_cmd_dump
f0fef760 2469 },
df2c07f4 2470 { .cmd = OVS_VPORT_CMD_SET,
f1e9590e
YHW
2471#ifdef HAVE_GENL_VALIDATE_FLAGS
2472 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2473#endif
a6a8674d 2474 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2ef0f1c2 2475#ifdef HAVE_GENL_OPS_POLICY
f0fef760 2476 .policy = vport_policy,
2ef0f1c2 2477#endif
df2c07f4 2478 .doit = ovs_vport_cmd_set,
f0fef760
BP
2479 },
2480};
2481
ba63fe26 2482struct genl_family dp_vport_genl_family __ro_after_init = {
cb25142c
PS
2483 .hdrsize = sizeof(struct ovs_header),
2484 .name = OVS_VPORT_FAMILY,
2485 .version = OVS_VPORT_VERSION,
2486 .maxattr = OVS_VPORT_ATTR_MAX,
2ef0f1c2
JB
2487#ifndef HAVE_GENL_OPS_POLICY
2488 .policy = vport_policy,
2489#endif
cb25142c
PS
2490 .netnsok = true,
2491 .parallel_ops = true,
2492 .ops = dp_vport_genl_ops,
2493 .n_ops = ARRAY_SIZE(dp_vport_genl_ops),
2494 .mcgrps = &ovs_dp_vport_multicast_group,
2495 .n_mcgrps = 1,
ba63fe26 2496 .module = THIS_MODULE,
982b8810 2497};
ed099e92 2498
18fd3a52 2499static struct genl_family *dp_genl_families[] = {
cb25142c
PS
2500 &dp_datapath_genl_family,
2501 &dp_vport_genl_family,
2502 &dp_flow_genl_family,
2503 &dp_packet_genl_family,
1cb57039 2504 &dp_meter_genl_family,
cb2a5486
YHW
2505#if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
2506 &dp_ct_limit_genl_family,
2507#endif
982b8810 2508};
ed099e92 2509
982b8810
BP
2510static void dp_unregister_genl(int n_families)
2511{
2512 int i;
ed099e92 2513
b867ca75 2514 for (i = 0; i < n_families; i++)
cb25142c 2515 genl_unregister_family(dp_genl_families[i]);
ed099e92
BP
2516}
2517
ba63fe26 2518static int __init dp_register_genl(void)
064af421 2519{
982b8810
BP
2520 int err;
2521 int i;
064af421 2522
982b8810 2523 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
064af421 2524
cb25142c 2525 err = genl_register_family(dp_genl_families[i]);
982b8810
BP
2526 if (err)
2527 goto error;
982b8810 2528 }
9cc8b4e4 2529
982b8810 2530 return 0;
064af421
BP
2531
2532error:
cb25142c 2533 dp_unregister_genl(i);
982b8810 2534 return err;
064af421
BP
2535}
2536
2a4999f3
PS
2537static int __net_init ovs_init_net(struct net *net)
2538{
2539 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2540
2541 INIT_LIST_HEAD(&ovs_net->dps);
cd2a59e9 2542 INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
7f4a5d68 2543 ovs_netns_frags_init(net);
2544 ovs_netns_frags6_init(net);
cb2a5486 2545 return ovs_ct_init(net);
2a4999f3
PS
2546}
2547
cabd5516
PS
2548static void __net_exit list_vports_from_net(struct net *net, struct net *dnet,
2549 struct list_head *head)
2a4999f3
PS
2550{
2551 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
cabd5516
PS
2552 struct datapath *dp;
2553
2554 list_for_each_entry(dp, &ovs_net->dps, list_node) {
2555 int i;
2556
2557 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
2558 struct vport *vport;
2559
2560 hlist_for_each_entry(vport, &dp->ports[i], dp_hash_node) {
cabd5516
PS
2561
2562 if (vport->ops->type != OVS_VPORT_TYPE_INTERNAL)
2563 continue;
2564
e23775f2 2565 if (dev_net(vport->dev) == dnet)
cabd5516
PS
2566 list_add(&vport->detach_list, head);
2567 }
2568 }
2569 }
2570}
2571
2572static void __net_exit ovs_exit_net(struct net *dnet)
2573{
2574 struct datapath *dp, *dp_next;
2575 struct ovs_net *ovs_net = net_generic(dnet, ovs_net_id);
2576 struct vport *vport, *vport_next;
2577 struct net *net;
2578 LIST_HEAD(head);
2a4999f3 2579
7f4a5d68 2580 ovs_netns_frags6_exit(dnet);
2581 ovs_netns_frags_exit(dnet);
038e34ab 2582 ovs_ct_exit(dnet);
cd2a59e9
PS
2583 ovs_lock();
2584 list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
2585 __dp_destroy(dp);
cabd5516 2586
3d10a0c8
YHW
2587#ifdef HAVE_NET_RWSEM
2588 down_read(&net_rwsem);
2589#else
cabd5516 2590 rtnl_lock();
3d10a0c8 2591#endif
cabd5516
PS
2592 for_each_net(net)
2593 list_vports_from_net(net, dnet, &head);
3d10a0c8
YHW
2594#ifdef HAVE_NET_RWSEM
2595 up_read(&net_rwsem);
2596#else
cabd5516 2597 rtnl_unlock();
3d10a0c8 2598#endif
cabd5516
PS
2599
2600 /* Detach all vports from given namespace. */
2601 list_for_each_entry_safe(vport, vport_next, &head, detach_list) {
2602 list_del(&vport->detach_list);
2603 ovs_dp_detach_port(vport);
2604 }
2605
cd2a59e9
PS
2606 ovs_unlock();
2607
2608 cancel_work_sync(&ovs_net->dp_notify_work);
2a4999f3
PS
2609}
2610
2611static struct pernet_operations ovs_net_ops = {
2612 .init = ovs_init_net,
2613 .exit = ovs_exit_net,
2614 .id = &ovs_net_id,
2615 .size = sizeof(struct ovs_net),
2616};
2617
22d24ebf
BP
2618static int __init dp_init(void)
2619{
2620 int err;
2621
9cfa4718 2622 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > sizeof_field(struct sk_buff, cb));
22d24ebf 2623
26bfaeaa 2624 pr_info("Open vSwitch switching datapath %s\n", VERSION);
064af421 2625
907c26a8 2626 ovs_nsh_init();
595e069a
JS
2627 err = action_fifos_init();
2628 if (err)
7f4a5d68 2629 goto error;
595e069a 2630
5282e284 2631 err = ovs_internal_dev_rtnl_link_register();
2c8c4fb7
AZ
2632 if (err)
2633 goto error_action_fifos_exit;
2634
5282e284
TG
2635 err = ovs_flow_init();
2636 if (err)
2637 goto error_unreg_rtnl_link;
2638
850b6b3b 2639 err = ovs_vport_init();
064af421
BP
2640 if (err)
2641 goto error_flow_exit;
2642
2a4999f3 2643 err = register_pernet_device(&ovs_net_ops);
f2459fe7
JG
2644 if (err)
2645 goto error_vport_exit;
2646
7f4a5d68 2647 err = compat_init();
2a4999f3
PS
2648 if (err)
2649 goto error_netns_exit;
2650
7f4a5d68 2651 err = register_netdevice_notifier(&ovs_dp_device_notifier);
2652 if (err)
2653 goto error_compat_exit;
2654
5a38795f
TG
2655 err = ovs_netdev_init();
2656 if (err)
2657 goto error_unreg_notifier;
2658
982b8810
BP
2659 err = dp_register_genl();
2660 if (err < 0)
5a38795f 2661 goto error_unreg_netdev;
982b8810 2662
064af421
BP
2663 return 0;
2664
5a38795f
TG
2665error_unreg_netdev:
2666 ovs_netdev_exit();
064af421 2667error_unreg_notifier:
850b6b3b 2668 unregister_netdevice_notifier(&ovs_dp_device_notifier);
7f4a5d68 2669error_compat_exit:
2670 compat_exit();
2a4999f3
PS
2671error_netns_exit:
2672 unregister_pernet_device(&ovs_net_ops);
f2459fe7 2673error_vport_exit:
850b6b3b 2674 ovs_vport_exit();
064af421 2675error_flow_exit:
850b6b3b 2676 ovs_flow_exit();
5282e284
TG
2677error_unreg_rtnl_link:
2678 ovs_internal_dev_rtnl_link_unregister();
2c8c4fb7
AZ
2679error_action_fifos_exit:
2680 action_fifos_exit();
064af421 2681error:
907c26a8 2682 ovs_nsh_cleanup();
064af421
BP
2683 return err;
2684}
2685
2686static void dp_cleanup(void)
2687{
982b8810 2688 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
5a38795f 2689 ovs_netdev_exit();
850b6b3b 2690 unregister_netdevice_notifier(&ovs_dp_device_notifier);
7f4a5d68 2691 compat_exit();
2a4999f3
PS
2692 unregister_pernet_device(&ovs_net_ops);
2693 rcu_barrier();
850b6b3b
JG
2694 ovs_vport_exit();
2695 ovs_flow_exit();
5282e284 2696 ovs_internal_dev_rtnl_link_unregister();
2c8c4fb7 2697 action_fifos_exit();
907c26a8 2698 ovs_nsh_cleanup();
064af421
BP
2699}
2700
2701module_init(dp_init);
2702module_exit(dp_cleanup);
2703
2704MODULE_DESCRIPTION("Open vSwitch switching datapath");
2705MODULE_LICENSE("GPL");
3d0666d2 2706MODULE_VERSION(VERSION);
75e2077e
TLSC
2707MODULE_ALIAS_GENL_FAMILY(OVS_DATAPATH_FAMILY);
2708MODULE_ALIAS_GENL_FAMILY(OVS_VPORT_FAMILY);
2709MODULE_ALIAS_GENL_FAMILY(OVS_FLOW_FAMILY);
2710MODULE_ALIAS_GENL_FAMILY(OVS_PACKET_FAMILY);
1cb57039 2711MODULE_ALIAS_GENL_FAMILY(OVS_METER_FAMILY);
cb2a5486 2712MODULE_ALIAS_GENL_FAMILY(OVS_CT_LIMIT_FAMILY);