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