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