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