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