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