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