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