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