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