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