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