]> git.proxmox.com Git - mirror_ovs.git/blame - datapath/datapath.c
datapath: Remove namespace compat support.
[mirror_ovs.git] / datapath / datapath.c
CommitLineData
064af421 1/*
a1c564be 2 * Copyright (c) 2007-2013 Nicira, Inc.
a14bc59f 3 *
a9a29d22
JG
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of version 2 of the GNU General Public
6 * License as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 * 02110-1301, USA
064af421
BP
17 */
18
dfffaef1
JP
19#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20
064af421
BP
21#include <linux/init.h>
22#include <linux/module.h>
064af421 23#include <linux/if_arp.h>
064af421
BP
24#include <linux/if_vlan.h>
25#include <linux/in.h>
26#include <linux/ip.h>
982b8810 27#include <linux/jhash.h>
064af421
BP
28#include <linux/delay.h>
29#include <linux/time.h>
30#include <linux/etherdevice.h>
ed099e92 31#include <linux/genetlink.h>
064af421
BP
32#include <linux/kernel.h>
33#include <linux/kthread.h>
064af421
BP
34#include <linux/mutex.h>
35#include <linux/percpu.h>
36#include <linux/rcupdate.h>
37#include <linux/tcp.h>
38#include <linux/udp.h>
39#include <linux/version.h>
40#include <linux/ethtool.h>
064af421 41#include <linux/wait.h>
064af421 42#include <asm/div64.h>
656a0e37 43#include <linux/highmem.h>
064af421
BP
44#include <linux/netfilter_bridge.h>
45#include <linux/netfilter_ipv4.h>
46#include <linux/inetdevice.h>
47#include <linux/list.h>
077257b8 48#include <linux/openvswitch.h>
064af421 49#include <linux/rculist.h>
064af421 50#include <linux/dmi.h>
cd2a59e9
PS
51#include <linux/genetlink.h>
52#include <net/genetlink.h>
36956a7d 53#include <net/genetlink.h>
2a4999f3
PS
54#include <net/net_namespace.h>
55#include <net/netns/generic.h>
064af421 56
dd8d6b8c 57#include "checksum.h"
064af421 58#include "datapath.h"
064af421 59#include "flow.h"
303708cc 60#include "vlan.h"
f2459fe7 61#include "vport-internal_dev.h"
d5de5b0d 62#include "vport-netdev.h"
064af421 63
acd051f1
PS
64#define REHASH_FLOW_INTERVAL (10 * 60 * HZ)
65static void rehash_flow_table(struct work_struct *work);
66static DECLARE_DELAYED_WORK(rehash_flow_wq, rehash_flow_table);
67
2a4999f3
PS
68int ovs_net_id __read_mostly;
69
e297c6b7
TG
70static void ovs_notify(struct sk_buff *skb, struct genl_info *info,
71 struct genl_multicast_group *grp)
72{
73 genl_notify(skb, genl_info_net(info), info->snd_portid,
74 grp->id, info->nlhdr, GFP_KERNEL);
75}
76
ed099e92
BP
77/**
78 * DOC: Locking:
064af421 79 *
cd2a59e9
PS
80 * All writes e.g. Writes to device state (add/remove datapath, port, set
81 * operations on vports, etc.), Writes to other state (flow table
82 * modifications, set miscellaneous datapath parameters, etc.) are protected
83 * by ovs_lock.
ed099e92
BP
84 *
85 * Reads are protected by RCU.
86 *
87 * There are a few special cases (mostly stats) that have their own
88 * synchronization but they nest under all of above and don't interact with
89 * each other.
cd2a59e9
PS
90 *
91 * The RTNL lock nests inside ovs_mutex.
064af421 92 */
ed099e92 93
cd2a59e9
PS
94static DEFINE_MUTEX(ovs_mutex);
95
96void ovs_lock(void)
97{
98 mutex_lock(&ovs_mutex);
99}
100
101void ovs_unlock(void)
102{
103 mutex_unlock(&ovs_mutex);
104}
105
106#ifdef CONFIG_LOCKDEP
107int lockdep_ovsl_is_held(void)
108{
109 if (debug_locks)
110 return lockdep_is_held(&ovs_mutex);
111 else
112 return 1;
113}
114#endif
115
c19e6535 116static struct vport *new_vport(const struct vport_parms *);
2a4999f3 117static int queue_gso_packets(struct net *, int dp_ifindex, struct sk_buff *,
7257b535 118 const struct dp_upcall_info *);
2a4999f3
PS
119static int queue_userspace_packet(struct net *, int dp_ifindex,
120 struct sk_buff *,
7257b535 121 const struct dp_upcall_info *);
064af421 122
cd2a59e9 123/* Must be called with rcu_read_lock or ovs_mutex. */
2a4999f3 124static struct datapath *get_dp(struct net *net, int dp_ifindex)
064af421 125{
254f2dc8
BP
126 struct datapath *dp = NULL;
127 struct net_device *dev;
ed099e92 128
254f2dc8 129 rcu_read_lock();
2a4999f3 130 dev = dev_get_by_index_rcu(net, dp_ifindex);
254f2dc8 131 if (dev) {
850b6b3b 132 struct vport *vport = ovs_internal_dev_get_vport(dev);
254f2dc8
BP
133 if (vport)
134 dp = vport->dp;
135 }
136 rcu_read_unlock();
137
138 return dp;
064af421 139}
064af421 140
cd2a59e9 141/* Must be called with rcu_read_lock or ovs_mutex. */
850b6b3b 142const char *ovs_dp_name(const struct datapath *dp)
f2459fe7 143{
cd2a59e9 144 struct vport *vport = ovs_vport_ovsl_rcu(dp, OVSP_LOCAL);
16b82e84 145 return vport->ops->get_name(vport);
f2459fe7
JG
146}
147
99769a40
JG
148static int get_dpifindex(struct datapath *dp)
149{
150 struct vport *local;
151 int ifindex;
152
153 rcu_read_lock();
154
95b1d73a 155 local = ovs_vport_rcu(dp, OVSP_LOCAL);
99769a40 156 if (local)
d5de5b0d 157 ifindex = netdev_vport_priv(local)->dev->ifindex;
99769a40
JG
158 else
159 ifindex = 0;
160
161 rcu_read_unlock();
162
163 return ifindex;
164}
165
46c6a11d
JG
166static void destroy_dp_rcu(struct rcu_head *rcu)
167{
168 struct datapath *dp = container_of(rcu, struct datapath, rcu);
46c6a11d 169
a1c564be 170 ovs_flow_tbl_destroy((__force struct flow_table *)dp->table, false);
46c6a11d 171 free_percpu(dp->stats_percpu);
2a4999f3 172 release_net(ovs_dp_get_net(dp));
95b1d73a 173 kfree(dp->ports);
5ca1ba48 174 kfree(dp);
46c6a11d
JG
175}
176
95b1d73a
PS
177static struct hlist_head *vport_hash_bucket(const struct datapath *dp,
178 u16 port_no)
179{
180 return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)];
181}
182
183struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no)
184{
185 struct vport *vport;
95b1d73a
PS
186 struct hlist_head *head;
187
188 head = vport_hash_bucket(dp, port_no);
f8dfbcb7 189 hlist_for_each_entry_rcu(vport, head, dp_hash_node) {
95b1d73a
PS
190 if (vport->port_no == port_no)
191 return vport;
192 }
193 return NULL;
194}
195
cd2a59e9 196/* Called with ovs_mutex. */
c19e6535 197static struct vport *new_vport(const struct vport_parms *parms)
064af421 198{
f2459fe7 199 struct vport *vport;
f2459fe7 200
850b6b3b 201 vport = ovs_vport_add(parms);
c19e6535
BP
202 if (!IS_ERR(vport)) {
203 struct datapath *dp = parms->dp;
95b1d73a 204 struct hlist_head *head = vport_hash_bucket(dp, vport->port_no);
064af421 205
95b1d73a 206 hlist_add_head_rcu(&vport->dp_hash_node, head);
c19e6535 207 }
c19e6535 208 return vport;
064af421
BP
209}
210
850b6b3b 211void ovs_dp_detach_port(struct vport *p)
064af421 212{
cd2a59e9 213 ASSERT_OVSL();
064af421 214
064af421 215 /* First drop references to device. */
95b1d73a 216 hlist_del_rcu(&p->dp_hash_node);
f2459fe7 217
7237e4f4 218 /* Then destroy it. */
850b6b3b 219 ovs_vport_del(p);
064af421
BP
220}
221
8819fac7 222/* Must be called with rcu_read_lock. */
850b6b3b 223void ovs_dp_process_received_packet(struct vport *p, struct sk_buff *skb)
064af421
BP
224{
225 struct datapath *dp = p->dp;
3544358a 226 struct sw_flow *flow;
064af421 227 struct dp_stats_percpu *stats;
52a23d92 228 struct sw_flow_key key;
e9141eec 229 u64 *stats_counter;
4c1ad233 230 int error;
064af421 231
70dbc259 232 stats = this_cpu_ptr(dp->stats_percpu);
a063b0df 233
52a23d92 234 /* Extract flow from 'skb' into 'key'. */
a1c564be 235 error = ovs_flow_extract(skb, p->port_no, &key);
52a23d92
JG
236 if (unlikely(error)) {
237 kfree_skb(skb);
238 return;
55574bb0
BP
239 }
240
52a23d92 241 /* Look up flow. */
a1c564be 242 flow = ovs_flow_lookup(rcu_dereference(dp->table), &key);
52a23d92
JG
243 if (unlikely(!flow)) {
244 struct dp_upcall_info upcall;
245
246 upcall.cmd = OVS_PACKET_CMD_MISS;
247 upcall.key = &key;
248 upcall.userdata = NULL;
249 upcall.portid = p->upcall_portid;
250 ovs_dp_upcall(dp, skb, &upcall);
251 consume_skb(skb);
252 stats_counter = &stats->n_missed;
253 goto out;
254 }
255
256 OVS_CB(skb)->flow = flow;
d1d71a36 257 OVS_CB(skb)->pkt_key = &key;
52a23d92 258
e9141eec 259 stats_counter = &stats->n_hit;
850b6b3b
JG
260 ovs_flow_used(OVS_CB(skb)->flow, skb);
261 ovs_execute_actions(dp, skb);
55574bb0 262
8819fac7 263out:
55574bb0 264 /* Update datapath statistics. */
821cb9fa 265 u64_stats_update_begin(&stats->sync);
e9141eec 266 (*stats_counter)++;
821cb9fa 267 u64_stats_update_end(&stats->sync);
064af421
BP
268}
269
aa5a8fdc
JG
270static struct genl_family dp_packet_genl_family = {
271 .id = GENL_ID_GENERATE,
df2c07f4
JP
272 .hdrsize = sizeof(struct ovs_header),
273 .name = OVS_PACKET_FAMILY,
69685a88 274 .version = OVS_PACKET_VERSION,
2a4999f3
PS
275 .maxattr = OVS_PACKET_ATTR_MAX,
276 SET_NETNSOK
14002a59 277 SET_PARALLEL_OPS
aa5a8fdc
JG
278};
279
850b6b3b
JG
280int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
281 const struct dp_upcall_info *upcall_info)
aa5a8fdc
JG
282{
283 struct dp_stats_percpu *stats;
7257b535 284 int dp_ifindex;
aa5a8fdc
JG
285 int err;
286
28aea917 287 if (upcall_info->portid == 0) {
b063d9f0 288 err = -ENOTCONN;
b063d9f0
JG
289 goto err;
290 }
291
7257b535
BP
292 dp_ifindex = get_dpifindex(dp);
293 if (!dp_ifindex) {
294 err = -ENODEV;
295 goto err;
aa5a8fdc
JG
296 }
297
7257b535 298 forward_ip_summed(skb, true);
36ce148c 299
7257b535 300 if (!skb_is_gso(skb))
2a4999f3 301 err = queue_userspace_packet(ovs_dp_get_net(dp), dp_ifindex, skb, upcall_info);
7257b535 302 else
2a4999f3 303 err = queue_gso_packets(ovs_dp_get_net(dp), dp_ifindex, skb, upcall_info);
d76195db
JG
304 if (err)
305 goto err;
306
307 return 0;
aa5a8fdc 308
aa5a8fdc 309err:
70dbc259 310 stats = this_cpu_ptr(dp->stats_percpu);
aa5a8fdc 311
821cb9fa 312 u64_stats_update_begin(&stats->sync);
aa5a8fdc 313 stats->n_lost++;
821cb9fa 314 u64_stats_update_end(&stats->sync);
aa5a8fdc 315
aa5a8fdc 316 return err;
982b8810
BP
317}
318
2a4999f3
PS
319static int queue_gso_packets(struct net *net, int dp_ifindex,
320 struct sk_buff *skb,
7257b535 321 const struct dp_upcall_info *upcall_info)
cb5087ca 322{
d4cba1f8 323 unsigned short gso_type = skb_shinfo(skb)->gso_type;
7257b535
BP
324 struct dp_upcall_info later_info;
325 struct sw_flow_key later_key;
326 struct sk_buff *segs, *nskb;
327 int err;
cb5087ca 328
0aa52d88 329 segs = __skb_gso_segment(skb, NETIF_F_SG | NETIF_F_HW_CSUM, false);
79089764
PS
330 if (IS_ERR(segs))
331 return PTR_ERR(segs);
99769a40 332
7257b535
BP
333 /* Queue all of the segments. */
334 skb = segs;
cb5087ca 335 do {
2a4999f3 336 err = queue_userspace_packet(net, dp_ifindex, skb, upcall_info);
982b8810 337 if (err)
7257b535 338 break;
856081f6 339
d4cba1f8 340 if (skb == segs && gso_type & SKB_GSO_UDP) {
e1cf87ff
JG
341 /* The initial flow key extracted by ovs_flow_extract()
342 * in this case is for a first fragment, so we need to
7257b535
BP
343 * properly mark later fragments.
344 */
345 later_key = *upcall_info->key;
9e44d715 346 later_key.ip.frag = OVS_FRAG_TYPE_LATER;
7257b535
BP
347
348 later_info = *upcall_info;
349 later_info.key = &later_key;
350 upcall_info = &later_info;
351 }
36ce148c 352 } while ((skb = skb->next));
cb5087ca 353
7257b535
BP
354 /* Free all of the segments. */
355 skb = segs;
356 do {
357 nskb = skb->next;
358 if (err)
359 kfree_skb(skb);
360 else
361 consume_skb(skb);
362 } while ((skb = nskb));
363 return err;
364}
365
0afa2373
TG
366static size_t key_attr_size(void)
367{
368 return nla_total_size(4) /* OVS_KEY_ATTR_PRIORITY */
369 + nla_total_size(0) /* OVS_KEY_ATTR_TUNNEL */
370 + nla_total_size(8) /* OVS_TUNNEL_KEY_ATTR_ID */
371 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_SRC */
372 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_DST */
373 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TOS */
374 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TTL */
375 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT */
376 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_CSUM */
377 + nla_total_size(4) /* OVS_KEY_ATTR_IN_PORT */
378 + nla_total_size(4) /* OVS_KEY_ATTR_SKB_MARK */
379 + nla_total_size(12) /* OVS_KEY_ATTR_ETHERNET */
380 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
381 + nla_total_size(4) /* OVS_KEY_ATTR_8021Q */
382 + nla_total_size(0) /* OVS_KEY_ATTR_ENCAP */
383 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
384 + nla_total_size(40) /* OVS_KEY_ATTR_IPV6 */
385 + nla_total_size(2) /* OVS_KEY_ATTR_ICMPV6 */
386 + nla_total_size(28); /* OVS_KEY_ATTR_ND */
387}
388
389static size_t upcall_msg_size(const struct sk_buff *skb,
390 const struct nlattr *userdata)
391{
392 size_t size = NLMSG_ALIGN(sizeof(struct ovs_header))
393 + nla_total_size(skb->len) /* OVS_PACKET_ATTR_PACKET */
394 + nla_total_size(key_attr_size()); /* OVS_PACKET_ATTR_KEY */
395
396 /* OVS_PACKET_ATTR_USERDATA */
397 if (userdata)
398 size += NLA_ALIGN(userdata->nla_len);
399
400 return size;
401}
402
2a4999f3
PS
403static int queue_userspace_packet(struct net *net, int dp_ifindex,
404 struct sk_buff *skb,
7257b535
BP
405 const struct dp_upcall_info *upcall_info)
406{
407 struct ovs_header *upcall;
6161d3fd 408 struct sk_buff *nskb = NULL;
7257b535
BP
409 struct sk_buff *user_skb; /* to be queued to userspace */
410 struct nlattr *nla;
7257b535
BP
411 int err;
412
6161d3fd
JG
413 if (vlan_tx_tag_present(skb)) {
414 nskb = skb_clone(skb, GFP_ATOMIC);
415 if (!nskb)
416 return -ENOMEM;
417
418 err = vlan_deaccel_tag(nskb);
419 if (err)
420 return err;
7257b535 421
6161d3fd
JG
422 skb = nskb;
423 }
424
425 if (nla_attr_size(skb->len) > USHRT_MAX) {
426 err = -EFBIG;
427 goto out;
428 }
7257b535 429
0afa2373 430 user_skb = genlmsg_new(upcall_msg_size(skb, upcall_info->userdata), GFP_ATOMIC);
6161d3fd
JG
431 if (!user_skb) {
432 err = -ENOMEM;
433 goto out;
434 }
7257b535
BP
435
436 upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
437 0, upcall_info->cmd);
438 upcall->dp_ifindex = dp_ifindex;
439
440 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
a1c564be 441 ovs_flow_to_nlattrs(upcall_info->key, upcall_info->key, user_skb);
7257b535
BP
442 nla_nest_end(user_skb, nla);
443
444 if (upcall_info->userdata)
e995e3df 445 __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA,
462a988b 446 nla_len(upcall_info->userdata),
e995e3df 447 nla_data(upcall_info->userdata));
7257b535
BP
448
449 nla = __nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, skb->len);
bed53bd1
PS
450
451 skb_copy_and_csum_dev(skb, nla_data(nla));
7257b535 452
c39b1a5c 453 genlmsg_end(user_skb, upcall);
28aea917 454 err = genlmsg_unicast(net, user_skb, upcall_info->portid);
6161d3fd
JG
455
456out:
457 kfree_skb(nskb);
458 return err;
cb5087ca
BP
459}
460
cd2a59e9 461/* Called with ovs_mutex. */
2a4999f3 462static int flush_flows(struct datapath *dp)
064af421 463{
3544358a
PS
464 struct flow_table *old_table;
465 struct flow_table *new_table;
8d5ebd83 466
cd2a59e9 467 old_table = ovsl_dereference(dp->table);
850b6b3b 468 new_table = ovs_flow_tbl_alloc(TBL_MIN_BUCKETS);
8d5ebd83 469 if (!new_table)
ed099e92 470 return -ENOMEM;
8d5ebd83
JG
471
472 rcu_assign_pointer(dp->table, new_table);
473
a1c564be 474 ovs_flow_tbl_destroy(old_table, true);
ed099e92 475 return 0;
064af421
BP
476}
477
9b405f1a
PS
478static struct nlattr *reserve_sfa_size(struct sw_flow_actions **sfa, int attr_len)
479{
480
481 struct sw_flow_actions *acts;
482 int new_acts_size;
483 int req_size = NLA_ALIGN(attr_len);
484 int next_offset = offsetof(struct sw_flow_actions, actions) +
485 (*sfa)->actions_len;
486
ba400435 487 if (req_size <= (ksize(*sfa) - next_offset))
9b405f1a
PS
488 goto out;
489
ba400435 490 new_acts_size = ksize(*sfa) * 2;
9b405f1a
PS
491
492 if (new_acts_size > MAX_ACTIONS_BUFSIZE) {
493 if ((MAX_ACTIONS_BUFSIZE - next_offset) < req_size)
494 return ERR_PTR(-EMSGSIZE);
495 new_acts_size = MAX_ACTIONS_BUFSIZE;
496 }
497
498 acts = ovs_flow_actions_alloc(new_acts_size);
499 if (IS_ERR(acts))
500 return (void *)acts;
501
502 memcpy(acts->actions, (*sfa)->actions, (*sfa)->actions_len);
503 acts->actions_len = (*sfa)->actions_len;
ba400435 504 kfree(*sfa);
9b405f1a
PS
505 *sfa = acts;
506
507out:
508 (*sfa)->actions_len += req_size;
509 return (struct nlattr *) ((unsigned char *)(*sfa) + next_offset);
510}
511
512static int add_action(struct sw_flow_actions **sfa, int attrtype, void *data, int len)
513{
514 struct nlattr *a;
515
516 a = reserve_sfa_size(sfa, nla_attr_size(len));
517 if (IS_ERR(a))
518 return PTR_ERR(a);
519
520 a->nla_type = attrtype;
521 a->nla_len = nla_attr_size(len);
522
523 if (data)
524 memcpy(nla_data(a), data, len);
525 memset((unsigned char *) a + a->nla_len, 0, nla_padlen(len));
6ff686f2 526
9b405f1a
PS
527 return 0;
528}
529
530static inline int add_nested_action_start(struct sw_flow_actions **sfa, int attrtype)
531{
532 int used = (*sfa)->actions_len;
533 int err;
534
535 err = add_action(sfa, attrtype, NULL, 0);
536 if (err)
537 return err;
538
539 return used;
540}
541
542static inline void add_nested_action_end(struct sw_flow_actions *sfa, int st_offset)
543{
544 struct nlattr *a = (struct nlattr *) ((unsigned char *)sfa->actions + st_offset);
545
546 a->nla_len = sfa->actions_len - st_offset;
547}
548
549static int validate_and_copy_actions(const struct nlattr *attr,
550 const struct sw_flow_key *key, int depth,
551 struct sw_flow_actions **sfa);
552
553static int validate_and_copy_sample(const struct nlattr *attr,
554 const struct sw_flow_key *key, int depth,
555 struct sw_flow_actions **sfa)
6ff686f2 556{
4be00e48
BP
557 const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
558 const struct nlattr *probability, *actions;
559 const struct nlattr *a;
9b405f1a 560 int rem, start, err, st_acts;
4be00e48
BP
561
562 memset(attrs, 0, sizeof(attrs));
6455100f 563 nla_for_each_nested(a, attr, rem) {
4be00e48
BP
564 int type = nla_type(a);
565 if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
566 return -EINVAL;
567 attrs[type] = a;
568 }
569 if (rem)
6ff686f2 570 return -EINVAL;
4be00e48
BP
571
572 probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
573 if (!probability || nla_len(probability) != sizeof(u32))
6ff686f2
PS
574 return -EINVAL;
575
4be00e48
BP
576 actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
577 if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
578 return -EINVAL;
9b405f1a
PS
579
580 /* validation done, copy sample action. */
581 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SAMPLE);
582 if (start < 0)
583 return start;
584 err = add_action(sfa, OVS_SAMPLE_ATTR_PROBABILITY, nla_data(probability), sizeof(u32));
585 if (err)
586 return err;
587 st_acts = add_nested_action_start(sfa, OVS_SAMPLE_ATTR_ACTIONS);
588 if (st_acts < 0)
589 return st_acts;
590
591 err = validate_and_copy_actions(actions, key, depth + 1, sfa);
592 if (err)
593 return err;
594
595 add_nested_action_end(*sfa, st_acts);
596 add_nested_action_end(*sfa, start);
597
598 return 0;
4edb9ae9
PS
599}
600
b1323f59
PS
601static int validate_tp_port(const struct sw_flow_key *flow_key)
602{
603 if (flow_key->eth.type == htons(ETH_P_IP)) {
6e9bea4d 604 if (flow_key->ipv4.tp.src || flow_key->ipv4.tp.dst)
b1323f59
PS
605 return 0;
606 } else if (flow_key->eth.type == htons(ETH_P_IPV6)) {
6e9bea4d 607 if (flow_key->ipv6.tp.src || flow_key->ipv6.tp.dst)
b1323f59
PS
608 return 0;
609 }
610
611 return -EINVAL;
612}
613
9b405f1a
PS
614static int validate_and_copy_set_tun(const struct nlattr *attr,
615 struct sw_flow_actions **sfa)
616{
a1c564be
AZ
617 struct sw_flow_match match;
618 struct sw_flow_key key;
9b405f1a
PS
619 int err, start;
620
a1c564be 621 ovs_match_init(&match, &key, NULL);
54cc3ec6 622 err = ovs_ipv4_tun_from_nlattr(nla_data(attr), &match, false);
9b405f1a
PS
623 if (err)
624 return err;
625
626 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SET);
627 if (start < 0)
628 return start;
629
a1c564be
AZ
630 err = add_action(sfa, OVS_KEY_ATTR_IPV4_TUNNEL, &match.key->tun_key,
631 sizeof(match.key->tun_key));
9b405f1a
PS
632 add_nested_action_end(*sfa, start);
633
634 return err;
635}
636
fea393b1 637static int validate_set(const struct nlattr *a,
9b405f1a
PS
638 const struct sw_flow_key *flow_key,
639 struct sw_flow_actions **sfa,
640 bool *set_tun)
4edb9ae9 641{
4edb9ae9
PS
642 const struct nlattr *ovs_key = nla_data(a);
643 int key_type = nla_type(ovs_key);
644
645 /* There can be only one key in a action */
646 if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
647 return -EINVAL;
648
649 if (key_type > OVS_KEY_ATTR_MAX ||
9b405f1a
PS
650 (ovs_key_lens[key_type] != nla_len(ovs_key) &&
651 ovs_key_lens[key_type] != -1))
4edb9ae9
PS
652 return -EINVAL;
653
fea393b1 654 switch (key_type) {
4edb9ae9 655 const struct ovs_key_ipv4 *ipv4_key;
bc7a5acd 656 const struct ovs_key_ipv6 *ipv6_key;
9b405f1a 657 int err;
4edb9ae9 658
fea393b1 659 case OVS_KEY_ATTR_PRIORITY:
fea393b1 660 case OVS_KEY_ATTR_ETHERNET:
4edb9ae9
PS
661 break;
662
72e8bf28
AA
663 case OVS_KEY_ATTR_SKB_MARK:
664#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20) && !defined(CONFIG_NETFILTER)
665 if (nla_get_u32(ovs_key) != 0)
666 return -EINVAL;
667#endif
668 break;
669
9b405f1a
PS
670 case OVS_KEY_ATTR_TUNNEL:
671 *set_tun = true;
672 err = validate_and_copy_set_tun(a, sfa);
673 if (err)
674 return err;
356af50b
KM
675 break;
676
fea393b1 677 case OVS_KEY_ATTR_IPV4:
4edb9ae9
PS
678 if (flow_key->eth.type != htons(ETH_P_IP))
679 return -EINVAL;
680
6e9bea4d 681 if (!flow_key->ip.proto)
4edb9ae9
PS
682 return -EINVAL;
683
684 ipv4_key = nla_data(ovs_key);
685 if (ipv4_key->ipv4_proto != flow_key->ip.proto)
686 return -EINVAL;
687
9e44d715 688 if (ipv4_key->ipv4_frag != flow_key->ip.frag)
7257b535
BP
689 return -EINVAL;
690
4edb9ae9
PS
691 break;
692
bc7a5acd
AA
693 case OVS_KEY_ATTR_IPV6:
694 if (flow_key->eth.type != htons(ETH_P_IPV6))
695 return -EINVAL;
696
697 if (!flow_key->ip.proto)
698 return -EINVAL;
699
700 ipv6_key = nla_data(ovs_key);
701 if (ipv6_key->ipv6_proto != flow_key->ip.proto)
702 return -EINVAL;
703
704 if (ipv6_key->ipv6_frag != flow_key->ip.frag)
705 return -EINVAL;
706
707 if (ntohl(ipv6_key->ipv6_label) & 0xFFF00000)
708 return -EINVAL;
709
710 break;
711
fea393b1 712 case OVS_KEY_ATTR_TCP:
4edb9ae9
PS
713 if (flow_key->ip.proto != IPPROTO_TCP)
714 return -EINVAL;
715
b1323f59 716 return validate_tp_port(flow_key);
4edb9ae9 717
fea393b1 718 case OVS_KEY_ATTR_UDP:
4edb9ae9
PS
719 if (flow_key->ip.proto != IPPROTO_UDP)
720 return -EINVAL;
721
b1323f59 722 return validate_tp_port(flow_key);
4edb9ae9 723
10f72e3d
JS
724 case OVS_KEY_ATTR_SCTP:
725 if (flow_key->ip.proto != IPPROTO_SCTP)
726 return -EINVAL;
727
728 return validate_tp_port(flow_key);
729
4edb9ae9
PS
730 default:
731 return -EINVAL;
732 }
fea393b1 733
4edb9ae9 734 return 0;
6ff686f2
PS
735}
736
98403001
BP
737static int validate_userspace(const struct nlattr *attr)
738{
6455100f 739 static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
98403001 740 [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
e995e3df 741 [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_UNSPEC },
98403001
BP
742 };
743 struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
744 int error;
745
6455100f
PS
746 error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX,
747 attr, userspace_policy);
98403001
BP
748 if (error)
749 return error;
750
6455100f
PS
751 if (!a[OVS_USERSPACE_ATTR_PID] ||
752 !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
98403001
BP
753 return -EINVAL;
754
755 return 0;
756}
757
9b405f1a
PS
758static int copy_action(const struct nlattr *from,
759 struct sw_flow_actions **sfa)
760{
761 int totlen = NLA_ALIGN(from->nla_len);
762 struct nlattr *to;
763
764 to = reserve_sfa_size(sfa, from->nla_len);
765 if (IS_ERR(to))
766 return PTR_ERR(to);
767
768 memcpy(to, from, totlen);
769 return 0;
770}
771
772static int validate_and_copy_actions(const struct nlattr *attr,
773 const struct sw_flow_key *key,
774 int depth,
775 struct sw_flow_actions **sfa)
064af421 776{
23cad98c 777 const struct nlattr *a;
6ff686f2
PS
778 int rem, err;
779
780 if (depth >= SAMPLE_ACTION_DEPTH)
781 return -EOVERFLOW;
23cad98c 782
37a1300c 783 nla_for_each_nested(a, attr, rem) {
98403001 784 /* Expected argument lengths, (u32)-1 for variable length. */
df2c07f4 785 static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
fea393b1 786 [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
98403001 787 [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
fea393b1
BP
788 [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
789 [OVS_ACTION_ATTR_POP_VLAN] = 0,
4edb9ae9 790 [OVS_ACTION_ATTR_SET] = (u32)-1,
98403001 791 [OVS_ACTION_ATTR_SAMPLE] = (u32)-1
23cad98c 792 };
fea393b1 793 const struct ovs_action_push_vlan *vlan;
23cad98c 794 int type = nla_type(a);
9b405f1a 795 bool skip_copy;
23cad98c 796
6ff686f2 797 if (type > OVS_ACTION_ATTR_MAX ||
98403001
BP
798 (action_lens[type] != nla_len(a) &&
799 action_lens[type] != (u32)-1))
23cad98c
BP
800 return -EINVAL;
801
9b405f1a 802 skip_copy = false;
23cad98c 803 switch (type) {
df2c07f4 804 case OVS_ACTION_ATTR_UNSPEC:
cdee00fd 805 return -EINVAL;
064af421 806
98403001
BP
807 case OVS_ACTION_ATTR_USERSPACE:
808 err = validate_userspace(a);
809 if (err)
810 return err;
811 break;
812
df2c07f4 813 case OVS_ACTION_ATTR_OUTPUT:
23cad98c
BP
814 if (nla_get_u32(a) >= DP_MAX_PORTS)
815 return -EINVAL;
3b1fc5f3 816 break;
cdee00fd 817
4edb9ae9 818
fea393b1
BP
819 case OVS_ACTION_ATTR_POP_VLAN:
820 break;
821
822 case OVS_ACTION_ATTR_PUSH_VLAN:
823 vlan = nla_data(a);
824 if (vlan->vlan_tpid != htons(ETH_P_8021Q))
825 return -EINVAL;
8ddc056d 826 if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
064af421 827 return -EINVAL;
23cad98c 828 break;
064af421 829
4edb9ae9 830 case OVS_ACTION_ATTR_SET:
9b405f1a 831 err = validate_set(a, key, sfa, &skip_copy);
4edb9ae9
PS
832 if (err)
833 return err;
23cad98c 834 break;
064af421 835
6ff686f2 836 case OVS_ACTION_ATTR_SAMPLE:
9b405f1a 837 err = validate_and_copy_sample(a, key, depth, sfa);
6ff686f2
PS
838 if (err)
839 return err;
9b405f1a 840 skip_copy = true;
6ff686f2
PS
841 break;
842
23cad98c 843 default:
4edb9ae9 844 return -EINVAL;
23cad98c 845 }
9b405f1a
PS
846 if (!skip_copy) {
847 err = copy_action(a, sfa);
848 if (err)
849 return err;
850 }
23cad98c 851 }
3c5f6de3 852
23cad98c
BP
853 if (rem > 0)
854 return -EINVAL;
064af421 855
23cad98c 856 return 0;
064af421 857}
4edb9ae9 858
064af421
BP
859static void clear_stats(struct sw_flow *flow)
860{
6bfafa55 861 flow->used = 0;
064af421 862 flow->tcp_flags = 0;
064af421
BP
863 flow->packet_count = 0;
864 flow->byte_count = 0;
865}
866
df2c07f4 867static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
064af421 868{
df2c07f4 869 struct ovs_header *ovs_header = info->userhdr;
982b8810 870 struct nlattr **a = info->attrs;
e0e57990 871 struct sw_flow_actions *acts;
982b8810 872 struct sk_buff *packet;
e0e57990 873 struct sw_flow *flow;
f7cd0081 874 struct datapath *dp;
d6569377 875 struct ethhdr *eth;
3f19d399 876 int len;
d6569377 877 int err;
064af421 878
f7cd0081 879 err = -EINVAL;
df2c07f4 880 if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
7c3072cc 881 !a[OVS_PACKET_ATTR_ACTIONS])
e5cad958 882 goto err;
064af421 883
df2c07f4 884 len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
3f19d399 885 packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
f7cd0081
BP
886 err = -ENOMEM;
887 if (!packet)
e5cad958 888 goto err;
3f19d399
BP
889 skb_reserve(packet, NET_IP_ALIGN);
890
bf3d6fce 891 nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
8d5ebd83 892
f7cd0081
BP
893 skb_reset_mac_header(packet);
894 eth = eth_hdr(packet);
064af421 895
d6569377
BP
896 /* Normally, setting the skb 'protocol' field would be handled by a
897 * call to eth_type_trans(), but it assumes there's a sending
898 * device, which we may not have. */
7cd46155 899 if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN)
f7cd0081 900 packet->protocol = eth->h_proto;
d6569377 901 else
f7cd0081 902 packet->protocol = htons(ETH_P_802_2);
d3c54451 903
e0e57990 904 /* Build an sw_flow for sending this packet. */
850b6b3b 905 flow = ovs_flow_alloc();
e0e57990
BP
906 err = PTR_ERR(flow);
907 if (IS_ERR(flow))
e5cad958 908 goto err_kfree_skb;
064af421 909
a1c564be 910 err = ovs_flow_extract(packet, -1, &flow->key);
e0e57990 911 if (err)
9321954a 912 goto err_flow_free;
e0e57990 913
a1c564be 914 err = ovs_flow_metadata_from_nlattrs(flow, a[OVS_PACKET_ATTR_KEY]);
80e5eed9 915 if (err)
9321954a 916 goto err_flow_free;
9b405f1a 917 acts = ovs_flow_actions_alloc(nla_len(a[OVS_PACKET_ATTR_ACTIONS]));
e0e57990
BP
918 err = PTR_ERR(acts);
919 if (IS_ERR(acts))
9321954a 920 goto err_flow_free;
9b405f1a
PS
921
922 err = validate_and_copy_actions(a[OVS_PACKET_ATTR_ACTIONS], &flow->key, 0, &acts);
e0e57990 923 rcu_assign_pointer(flow->sf_acts, acts);
9b405f1a
PS
924 if (err)
925 goto err_flow_free;
e0e57990
BP
926
927 OVS_CB(packet)->flow = flow;
d1d71a36 928 OVS_CB(packet)->pkt_key = &flow->key;
abff858b 929 packet->priority = flow->key.phy.priority;
72e8bf28 930 skb_set_mark(packet, flow->key.phy.skb_mark);
e0e57990 931
d6569377 932 rcu_read_lock();
2a4999f3 933 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
f7cd0081 934 err = -ENODEV;
e5cad958
BP
935 if (!dp)
936 goto err_unlock;
cc4015df 937
e9141eec 938 local_bh_disable();
850b6b3b 939 err = ovs_execute_actions(dp, packet);
e9141eec 940 local_bh_enable();
d6569377 941 rcu_read_unlock();
e0e57990 942
a1c564be 943 ovs_flow_free(flow, false);
e5cad958 944 return err;
064af421 945
e5cad958
BP
946err_unlock:
947 rcu_read_unlock();
9321954a 948err_flow_free:
a1c564be 949 ovs_flow_free(flow, false);
e5cad958
BP
950err_kfree_skb:
951 kfree_skb(packet);
952err:
d6569377 953 return err;
064af421
BP
954}
955
df2c07f4 956static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
7c3072cc
TG
957#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18)
958 [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
959#else
960 [OVS_PACKET_ATTR_PACKET] = { .minlen = ETH_HLEN },
961#endif
df2c07f4
JP
962 [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
963 [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
982b8810
BP
964};
965
966static struct genl_ops dp_packet_genl_ops[] = {
df2c07f4 967 { .cmd = OVS_PACKET_CMD_EXECUTE,
982b8810
BP
968 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
969 .policy = packet_policy,
df2c07f4 970 .doit = ovs_packet_cmd_execute
982b8810
BP
971 }
972};
973
df2c07f4 974static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats)
064af421 975{
fb93e9aa 976 struct flow_table *table;
d6569377 977 int i;
f180c2e2 978
fb93e9aa 979 table = rcu_dereference_check(dp->table, lockdep_ovsl_is_held());
850b6b3b 980 stats->n_flows = ovs_flow_tbl_count(table);
064af421 981
7257b535 982 stats->n_hit = stats->n_missed = stats->n_lost = 0;
d6569377
BP
983 for_each_possible_cpu(i) {
984 const struct dp_stats_percpu *percpu_stats;
985 struct dp_stats_percpu local_stats;
821cb9fa 986 unsigned int start;
44e05eca 987
d6569377 988 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
064af421 989
d6569377 990 do {
821cb9fa 991 start = u64_stats_fetch_begin_bh(&percpu_stats->sync);
d6569377 992 local_stats = *percpu_stats;
821cb9fa 993 } while (u64_stats_fetch_retry_bh(&percpu_stats->sync, start));
064af421 994
d6569377
BP
995 stats->n_hit += local_stats.n_hit;
996 stats->n_missed += local_stats.n_missed;
997 stats->n_lost += local_stats.n_lost;
998 }
999}
064af421 1000
df2c07f4
JP
1001static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
1002 [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
1003 [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
1004 [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
d6569377 1005};
36956a7d 1006
37a1300c
BP
1007static struct genl_family dp_flow_genl_family = {
1008 .id = GENL_ID_GENERATE,
df2c07f4
JP
1009 .hdrsize = sizeof(struct ovs_header),
1010 .name = OVS_FLOW_FAMILY,
69685a88 1011 .version = OVS_FLOW_VERSION,
2a4999f3
PS
1012 .maxattr = OVS_FLOW_ATTR_MAX,
1013 SET_NETNSOK
14002a59 1014 SET_PARALLEL_OPS
37a1300c 1015};
ed099e92 1016
850b6b3b 1017static struct genl_multicast_group ovs_dp_flow_multicast_group = {
df2c07f4 1018 .name = OVS_FLOW_MCGROUP
37a1300c
BP
1019};
1020
9b405f1a
PS
1021static int actions_to_attr(const struct nlattr *attr, int len, struct sk_buff *skb);
1022static int sample_action_to_attr(const struct nlattr *attr, struct sk_buff *skb)
1023{
1024 const struct nlattr *a;
1025 struct nlattr *start;
1026 int err = 0, rem;
1027
1028 start = nla_nest_start(skb, OVS_ACTION_ATTR_SAMPLE);
1029 if (!start)
1030 return -EMSGSIZE;
1031
1032 nla_for_each_nested(a, attr, rem) {
1033 int type = nla_type(a);
1034 struct nlattr *st_sample;
1035
1036 switch (type) {
1037 case OVS_SAMPLE_ATTR_PROBABILITY:
1038 if (nla_put(skb, OVS_SAMPLE_ATTR_PROBABILITY, sizeof(u32), nla_data(a)))
1039 return -EMSGSIZE;
1040 break;
1041 case OVS_SAMPLE_ATTR_ACTIONS:
1042 st_sample = nla_nest_start(skb, OVS_SAMPLE_ATTR_ACTIONS);
1043 if (!st_sample)
1044 return -EMSGSIZE;
1045 err = actions_to_attr(nla_data(a), nla_len(a), skb);
1046 if (err)
1047 return err;
1048 nla_nest_end(skb, st_sample);
1049 break;
1050 }
1051 }
1052
1053 nla_nest_end(skb, start);
1054 return err;
1055}
1056
1057static int set_action_to_attr(const struct nlattr *a, struct sk_buff *skb)
1058{
1059 const struct nlattr *ovs_key = nla_data(a);
1060 int key_type = nla_type(ovs_key);
1061 struct nlattr *start;
1062 int err;
1063
1064 switch (key_type) {
1065 case OVS_KEY_ATTR_IPV4_TUNNEL:
1066 start = nla_nest_start(skb, OVS_ACTION_ATTR_SET);
1067 if (!start)
1068 return -EMSGSIZE;
1069
54cc3ec6
JG
1070 err = ovs_ipv4_tun_to_nlattr(skb, nla_data(ovs_key),
1071 nla_data(ovs_key));
9b405f1a
PS
1072 if (err)
1073 return err;
1074 nla_nest_end(skb, start);
1075 break;
1076 default:
1077 if (nla_put(skb, OVS_ACTION_ATTR_SET, nla_len(a), ovs_key))
1078 return -EMSGSIZE;
1079 break;
1080 }
1081
1082 return 0;
1083}
1084
1085static int actions_to_attr(const struct nlattr *attr, int len, struct sk_buff *skb)
1086{
1087 const struct nlattr *a;
1088 int rem, err;
1089
1090 nla_for_each_attr(a, attr, len, rem) {
1091 int type = nla_type(a);
1092
1093 switch (type) {
1094 case OVS_ACTION_ATTR_SET:
1095 err = set_action_to_attr(a, skb);
1096 if (err)
1097 return err;
1098 break;
1099
1100 case OVS_ACTION_ATTR_SAMPLE:
1101 err = sample_action_to_attr(a, skb);
1102 if (err)
1103 return err;
1104 break;
1105 default:
1106 if (nla_put(skb, type, nla_len(a), nla_data(a)))
1107 return -EMSGSIZE;
1108 break;
1109 }
1110 }
1111
1112 return 0;
1113}
1114
0afa2373
TG
1115static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts)
1116{
1117 return NLMSG_ALIGN(sizeof(struct ovs_header))
1118 + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_KEY */
a1c564be 1119 + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_MASK */
0afa2373
TG
1120 + nla_total_size(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
1121 + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
1122 + nla_total_size(8) /* OVS_FLOW_ATTR_USED */
1123 + nla_total_size(acts->actions_len); /* OVS_FLOW_ATTR_ACTIONS */
1124}
1125
cd2a59e9 1126/* Called with ovs_mutex. */
df2c07f4 1127static int ovs_flow_cmd_fill_info(struct sw_flow *flow, struct datapath *dp,
28aea917 1128 struct sk_buff *skb, u32 portid,
6455100f 1129 u32 seq, u32 flags, u8 cmd)
d6569377 1130{
37a1300c 1131 const int skb_orig_len = skb->len;
9b405f1a 1132 struct nlattr *start;
df2c07f4
JP
1133 struct ovs_flow_stats stats;
1134 struct ovs_header *ovs_header;
d6569377
BP
1135 struct nlattr *nla;
1136 unsigned long used;
1137 u8 tcp_flags;
1138 int err;
064af421 1139
28aea917 1140 ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family, flags, cmd);
df2c07f4 1141 if (!ovs_header)
37a1300c 1142 return -EMSGSIZE;
d6569377 1143
99769a40 1144 ovs_header->dp_ifindex = get_dpifindex(dp);
d6569377 1145
a1c564be 1146 /* Fill flow key. */
df2c07f4 1147 nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
d6569377
BP
1148 if (!nla)
1149 goto nla_put_failure;
a1c564be
AZ
1150
1151 err = ovs_flow_to_nlattrs(&flow->unmasked_key,
1152 &flow->unmasked_key, skb);
d6569377 1153 if (err)
37a1300c 1154 goto error;
d6569377
BP
1155 nla_nest_end(skb, nla);
1156
a1c564be
AZ
1157 nla = nla_nest_start(skb, OVS_FLOW_ATTR_MASK);
1158 if (!nla)
1159 goto nla_put_failure;
1160
3d097064 1161 err = ovs_flow_to_nlattrs(&flow->key, &flow->mask->key, skb);
a1c564be
AZ
1162 if (err)
1163 goto error;
1164
1165 nla_nest_end(skb, nla);
1166
d6569377
BP
1167 spin_lock_bh(&flow->lock);
1168 used = flow->used;
1169 stats.n_packets = flow->packet_count;
1170 stats.n_bytes = flow->byte_count;
1171 tcp_flags = flow->tcp_flags;
1172 spin_unlock_bh(&flow->lock);
1173
c3cc8c03
DM
1174 if (used &&
1175 nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
1176 goto nla_put_failure;
d6569377 1177
c3cc8c03
DM
1178 if (stats.n_packets &&
1179 nla_put(skb, OVS_FLOW_ATTR_STATS,
1180 sizeof(struct ovs_flow_stats), &stats))
1181 goto nla_put_failure;
d6569377 1182
c3cc8c03
DM
1183 if (tcp_flags &&
1184 nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, tcp_flags))
1185 goto nla_put_failure;
d6569377 1186
df2c07f4 1187 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
30053024
BP
1188 * this is the first flow to be dumped into 'skb'. This is unusual for
1189 * Netlink but individual action lists can be longer than
1190 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
1191 * The userspace caller can always fetch the actions separately if it
1192 * really wants them. (Most userspace callers in fact don't care.)
1193 *
1194 * This can only fail for dump operations because the skb is always
1195 * properly sized for single flows.
1196 */
9b405f1a 1197 start = nla_nest_start(skb, OVS_FLOW_ATTR_ACTIONS);
f6f481ee 1198 if (start) {
f44ccce1
PS
1199 const struct sw_flow_actions *sf_acts;
1200
1201 sf_acts = rcu_dereference_check(flow->sf_acts,
1202 lockdep_ovsl_is_held());
1203
f6f481ee 1204 err = actions_to_attr(sf_acts->actions, sf_acts->actions_len, skb);
0a25b039
BP
1205 if (!err)
1206 nla_nest_end(skb, start);
1207 else {
1208 if (skb_orig_len)
1209 goto error;
1210
1211 nla_nest_cancel(skb, start);
1212 }
7aac03bd
JG
1213 } else if (skb_orig_len)
1214 goto nla_put_failure;
37a1300c 1215
df2c07f4 1216 return genlmsg_end(skb, ovs_header);
d6569377
BP
1217
1218nla_put_failure:
1219 err = -EMSGSIZE;
37a1300c 1220error:
df2c07f4 1221 genlmsg_cancel(skb, ovs_header);
d6569377 1222 return err;
44e05eca
BP
1223}
1224
df2c07f4 1225static struct sk_buff *ovs_flow_cmd_alloc_info(struct sw_flow *flow)
44e05eca 1226{
37a1300c 1227 const struct sw_flow_actions *sf_acts;
d6569377 1228
cd2a59e9 1229 sf_acts = ovsl_dereference(flow->sf_acts);
d6569377 1230
0afa2373 1231 return genlmsg_new(ovs_flow_cmd_msg_size(sf_acts), GFP_KERNEL);
37a1300c 1232}
8d5ebd83 1233
6455100f
PS
1234static struct sk_buff *ovs_flow_cmd_build_info(struct sw_flow *flow,
1235 struct datapath *dp,
28aea917 1236 u32 portid, u32 seq, u8 cmd)
37a1300c
BP
1237{
1238 struct sk_buff *skb;
1239 int retval;
d6569377 1240
df2c07f4 1241 skb = ovs_flow_cmd_alloc_info(flow);
37a1300c
BP
1242 if (!skb)
1243 return ERR_PTR(-ENOMEM);
d6569377 1244
28aea917 1245 retval = ovs_flow_cmd_fill_info(flow, dp, skb, portid, seq, 0, cmd);
37a1300c 1246 BUG_ON(retval < 0);
d6569377 1247 return skb;
064af421
BP
1248}
1249
df2c07f4 1250static int ovs_flow_cmd_new_or_set(struct sk_buff *skb, struct genl_info *info)
064af421 1251{
37a1300c 1252 struct nlattr **a = info->attrs;
df2c07f4 1253 struct ovs_header *ovs_header = info->userhdr;
529db635 1254 struct sw_flow_key key, masked_key;
a1c564be
AZ
1255 struct sw_flow *flow = NULL;
1256 struct sw_flow_mask mask;
37a1300c 1257 struct sk_buff *reply;
9c52546b 1258 struct datapath *dp;
3544358a 1259 struct flow_table *table;
9b405f1a 1260 struct sw_flow_actions *acts = NULL;
a1c564be 1261 struct sw_flow_match match;
bc4a05c6 1262 int error;
064af421 1263
37a1300c
BP
1264 /* Extract key. */
1265 error = -EINVAL;
df2c07f4 1266 if (!a[OVS_FLOW_ATTR_KEY])
37a1300c 1267 goto error;
a1c564be
AZ
1268
1269 ovs_match_init(&match, &key, &mask);
1270 error = ovs_match_from_nlattrs(&match,
1271 a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
37a1300c
BP
1272 if (error)
1273 goto error;
064af421 1274
37a1300c 1275 /* Validate actions. */
df2c07f4 1276 if (a[OVS_FLOW_ATTR_ACTIONS]) {
9b405f1a
PS
1277 acts = ovs_flow_actions_alloc(nla_len(a[OVS_FLOW_ATTR_ACTIONS]));
1278 error = PTR_ERR(acts);
1279 if (IS_ERR(acts))
37a1300c 1280 goto error;
9b405f1a 1281
529db635
JG
1282 ovs_flow_key_mask(&masked_key, &key, &mask);
1283 error = validate_and_copy_actions(a[OVS_FLOW_ATTR_ACTIONS],
1284 &masked_key, 0, &acts);
1285 if (error) {
1286 OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
9b405f1a 1287 goto err_kfree;
529db635 1288 }
df2c07f4 1289 } else if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW) {
37a1300c
BP
1290 error = -EINVAL;
1291 goto error;
1292 }
1293
cd2a59e9 1294 ovs_lock();
2a4999f3 1295 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
d6569377 1296 error = -ENODEV;
9c52546b 1297 if (!dp)
cd2a59e9 1298 goto err_unlock_ovs;
704a1e09 1299
cd2a59e9 1300 table = ovsl_dereference(dp->table);
a1c564be
AZ
1301
1302 /* Check if this is a duplicate flow */
1303 flow = ovs_flow_lookup(table, &key);
3544358a 1304 if (!flow) {
a1c564be 1305 struct sw_flow_mask *mask_p;
d6569377
BP
1306 /* Bail out if we're not allowed to create a new flow. */
1307 error = -ENOENT;
df2c07f4 1308 if (info->genlhdr->cmd == OVS_FLOW_CMD_SET)
cd2a59e9 1309 goto err_unlock_ovs;
d6569377
BP
1310
1311 /* Expand table, if necessary, to make room. */
850b6b3b 1312 if (ovs_flow_tbl_need_to_expand(table)) {
3544358a
PS
1313 struct flow_table *new_table;
1314
850b6b3b 1315 new_table = ovs_flow_tbl_expand(table);
3544358a
PS
1316 if (!IS_ERR(new_table)) {
1317 rcu_assign_pointer(dp->table, new_table);
a1c564be 1318 ovs_flow_tbl_destroy(table, true);
cd2a59e9 1319 table = ovsl_dereference(dp->table);
3544358a 1320 }
d6569377
BP
1321 }
1322
1323 /* Allocate flow. */
850b6b3b 1324 flow = ovs_flow_alloc();
d6569377
BP
1325 if (IS_ERR(flow)) {
1326 error = PTR_ERR(flow);
cd2a59e9 1327 goto err_unlock_ovs;
d6569377 1328 }
d6569377
BP
1329 clear_stats(flow);
1330
529db635
JG
1331 flow->key = masked_key;
1332 flow->unmasked_key = key;
1333
a1c564be
AZ
1334 /* Make sure mask is unique in the system */
1335 mask_p = ovs_sw_flow_mask_find(table, &mask);
1336 if (!mask_p) {
1337 /* Allocate a new mask if none exsits. */
1338 mask_p = ovs_sw_flow_mask_alloc();
1339 if (!mask_p)
1340 goto err_flow_free;
1341 mask_p->key = mask.key;
1342 mask_p->range = mask.range;
1343 ovs_sw_flow_mask_insert(table, mask_p);
1344 }
1345
1346 ovs_sw_flow_mask_add_ref(mask_p);
3d097064 1347 flow->mask = mask_p;
d6569377
BP
1348 rcu_assign_pointer(flow->sf_acts, acts);
1349
d6569377 1350 /* Put flow in bucket. */
529db635 1351 ovs_flow_insert(table, flow);
37a1300c 1352
28aea917 1353 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
a1c564be 1354 info->snd_seq, OVS_FLOW_CMD_NEW);
d6569377
BP
1355 } else {
1356 /* We found a matching flow. */
1357 struct sw_flow_actions *old_acts;
1358
1359 /* Bail out if we're not allowed to modify an existing flow.
1360 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
1361 * because Generic Netlink treats the latter as a dump
1362 * request. We also accept NLM_F_EXCL in case that bug ever
1363 * gets fixed.
1364 */
1365 error = -EEXIST;
df2c07f4 1366 if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW &&
37a1300c 1367 info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL))
cd2a59e9 1368 goto err_unlock_ovs;
d6569377 1369
b21e5b6a
AZ
1370 /* The unmasked key has to be the same for flow updates. */
1371 error = -EINVAL;
1b936472
AZ
1372 if (!ovs_flow_cmp_unmasked_key(flow, &key, match.range.end)) {
1373 OVS_NLERR("Flow modification message rejected, unmasked key does not match.\n");
b21e5b6a 1374 goto err_unlock_ovs;
1b936472 1375 }
b21e5b6a 1376
d6569377 1377 /* Update actions. */
cd2a59e9 1378 old_acts = ovsl_dereference(flow->sf_acts);
9b405f1a
PS
1379 rcu_assign_pointer(flow->sf_acts, acts);
1380 ovs_flow_deferred_free_acts(old_acts);
d6569377 1381
28aea917 1382 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
6455100f 1383 info->snd_seq, OVS_FLOW_CMD_NEW);
d6569377
BP
1384
1385 /* Clear stats. */
df2c07f4 1386 if (a[OVS_FLOW_ATTR_CLEAR]) {
d6569377
BP
1387 spin_lock_bh(&flow->lock);
1388 clear_stats(flow);
1389 spin_unlock_bh(&flow->lock);
1390 }
9c52546b 1391 }
cd2a59e9 1392 ovs_unlock();
37a1300c
BP
1393
1394 if (!IS_ERR(reply))
e297c6b7 1395 ovs_notify(reply, info, &ovs_dp_flow_multicast_group);
37a1300c 1396 else
2a4999f3
PS
1397 netlink_set_err(GENL_SOCK(sock_net(skb->sk)), 0,
1398 ovs_dp_flow_multicast_group.id, PTR_ERR(reply));
d6569377 1399 return 0;
704a1e09 1400
a1c564be
AZ
1401err_flow_free:
1402 ovs_flow_free(flow, false);
cd2a59e9
PS
1403err_unlock_ovs:
1404 ovs_unlock();
9b405f1a 1405err_kfree:
ba400435 1406 kfree(acts);
37a1300c 1407error:
9c52546b 1408 return error;
704a1e09
BP
1409}
1410
df2c07f4 1411static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
704a1e09 1412{
37a1300c 1413 struct nlattr **a = info->attrs;
df2c07f4 1414 struct ovs_header *ovs_header = info->userhdr;
37a1300c 1415 struct sw_flow_key key;
37a1300c 1416 struct sk_buff *reply;
704a1e09 1417 struct sw_flow *flow;
9c52546b 1418 struct datapath *dp;
3544358a 1419 struct flow_table *table;
a1c564be 1420 struct sw_flow_match match;
9c52546b 1421 int err;
704a1e09 1422
1b936472
AZ
1423 if (!a[OVS_FLOW_ATTR_KEY]) {
1424 OVS_NLERR("Flow get message rejected, Key attribute missing.\n");
37a1300c 1425 return -EINVAL;
1b936472 1426 }
a1c564be
AZ
1427
1428 ovs_match_init(&match, &key, NULL);
1429 err = ovs_match_from_nlattrs(&match, a[OVS_FLOW_ATTR_KEY], NULL);
37a1300c
BP
1430 if (err)
1431 return err;
704a1e09 1432
cd2a59e9 1433 ovs_lock();
2a4999f3 1434 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
cd2a59e9
PS
1435 if (!dp) {
1436 err = -ENODEV;
1437 goto unlock;
1438 }
704a1e09 1439
cd2a59e9 1440 table = ovsl_dereference(dp->table);
a1c564be 1441 flow = ovs_flow_lookup_unmasked_key(table, &match);
cd2a59e9
PS
1442 if (!flow) {
1443 err = -ENOENT;
1444 goto unlock;
1445 }
d6569377 1446
28aea917 1447 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
6455100f 1448 info->snd_seq, OVS_FLOW_CMD_NEW);
cd2a59e9
PS
1449 if (IS_ERR(reply)) {
1450 err = PTR_ERR(reply);
1451 goto unlock;
1452 }
36956a7d 1453
cd2a59e9 1454 ovs_unlock();
37a1300c 1455 return genlmsg_reply(reply, info);
cd2a59e9
PS
1456unlock:
1457 ovs_unlock();
1458 return err;
d6569377 1459}
9c52546b 1460
df2c07f4 1461static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
d6569377 1462{
37a1300c 1463 struct nlattr **a = info->attrs;
df2c07f4 1464 struct ovs_header *ovs_header = info->userhdr;
37a1300c 1465 struct sw_flow_key key;
37a1300c 1466 struct sk_buff *reply;
d6569377 1467 struct sw_flow *flow;
d6569377 1468 struct datapath *dp;
3544358a 1469 struct flow_table *table;
a1c564be 1470 struct sw_flow_match match;
d6569377 1471 int err;
36956a7d 1472
cd2a59e9 1473 ovs_lock();
2a4999f3 1474 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
cd2a59e9
PS
1475 if (!dp) {
1476 err = -ENODEV;
1477 goto unlock;
1478 }
2a4999f3 1479
cd2a59e9
PS
1480 if (!a[OVS_FLOW_ATTR_KEY]) {
1481 err = flush_flows(dp);
1482 goto unlock;
1483 }
a1c564be
AZ
1484
1485 ovs_match_init(&match, &key, NULL);
1486 err = ovs_match_from_nlattrs(&match, a[OVS_FLOW_ATTR_KEY], NULL);
37a1300c 1487 if (err)
cd2a59e9 1488 goto unlock;
d6569377 1489
cd2a59e9 1490 table = ovsl_dereference(dp->table);
a1c564be 1491 flow = ovs_flow_lookup_unmasked_key(table, &match);
cd2a59e9
PS
1492 if (!flow) {
1493 err = -ENOENT;
1494 goto unlock;
1495 }
d6569377 1496
df2c07f4 1497 reply = ovs_flow_cmd_alloc_info(flow);
cd2a59e9
PS
1498 if (!reply) {
1499 err = -ENOMEM;
1500 goto unlock;
1501 }
37a1300c 1502
a1c564be 1503 ovs_flow_remove(table, flow);
37a1300c 1504
28aea917 1505 err = ovs_flow_cmd_fill_info(flow, dp, reply, info->snd_portid,
df2c07f4 1506 info->snd_seq, 0, OVS_FLOW_CMD_DEL);
37a1300c
BP
1507 BUG_ON(err < 0);
1508
a1c564be 1509 ovs_flow_free(flow, true);
cd2a59e9 1510 ovs_unlock();
37a1300c 1511
e297c6b7 1512 ovs_notify(reply, info, &ovs_dp_flow_multicast_group);
37a1300c 1513 return 0;
cd2a59e9
PS
1514unlock:
1515 ovs_unlock();
1516 return err;
37a1300c
BP
1517}
1518
df2c07f4 1519static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
37a1300c 1520{
df2c07f4 1521 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
37a1300c 1522 struct datapath *dp;
20d035b2 1523 struct flow_table *table;
37a1300c 1524
f44ccce1 1525 rcu_read_lock();
2a4999f3 1526 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
cd2a59e9 1527 if (!dp) {
f44ccce1 1528 rcu_read_unlock();
37a1300c 1529 return -ENODEV;
cd2a59e9 1530 }
37a1300c 1531
f44ccce1 1532 table = rcu_dereference(dp->table);
37a1300c 1533 for (;;) {
37a1300c
BP
1534 struct sw_flow *flow;
1535 u32 bucket, obj;
1536
1537 bucket = cb->args[0];
1538 obj = cb->args[1];
a1c564be 1539 flow = ovs_flow_dump_next(table, &bucket, &obj);
3544358a 1540 if (!flow)
37a1300c
BP
1541 break;
1542
6455100f 1543 if (ovs_flow_cmd_fill_info(flow, dp, skb,
28aea917 1544 NETLINK_CB(cb->skb).portid,
37a1300c 1545 cb->nlh->nlmsg_seq, NLM_F_MULTI,
df2c07f4 1546 OVS_FLOW_CMD_NEW) < 0)
37a1300c
BP
1547 break;
1548
1549 cb->args[0] = bucket;
1550 cb->args[1] = obj;
1551 }
f44ccce1 1552 rcu_read_unlock();
37a1300c 1553 return skb->len;
704a1e09
BP
1554}
1555
37a1300c 1556static struct genl_ops dp_flow_genl_ops[] = {
df2c07f4 1557 { .cmd = OVS_FLOW_CMD_NEW,
37a1300c
BP
1558 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1559 .policy = flow_policy,
df2c07f4 1560 .doit = ovs_flow_cmd_new_or_set
37a1300c 1561 },
df2c07f4 1562 { .cmd = OVS_FLOW_CMD_DEL,
37a1300c
BP
1563 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1564 .policy = flow_policy,
df2c07f4 1565 .doit = ovs_flow_cmd_del
37a1300c 1566 },
df2c07f4 1567 { .cmd = OVS_FLOW_CMD_GET,
37a1300c
BP
1568 .flags = 0, /* OK for unprivileged users. */
1569 .policy = flow_policy,
df2c07f4
JP
1570 .doit = ovs_flow_cmd_get,
1571 .dumpit = ovs_flow_cmd_dump
37a1300c 1572 },
df2c07f4 1573 { .cmd = OVS_FLOW_CMD_SET,
37a1300c
BP
1574 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1575 .policy = flow_policy,
df2c07f4 1576 .doit = ovs_flow_cmd_new_or_set,
37a1300c
BP
1577 },
1578};
1579
df2c07f4 1580static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
aaff4b55 1581#ifdef HAVE_NLA_NUL_STRING
df2c07f4 1582 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
aaff4b55 1583#endif
b063d9f0 1584 [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
d6569377
BP
1585};
1586
aaff4b55
BP
1587static struct genl_family dp_datapath_genl_family = {
1588 .id = GENL_ID_GENERATE,
df2c07f4
JP
1589 .hdrsize = sizeof(struct ovs_header),
1590 .name = OVS_DATAPATH_FAMILY,
69685a88 1591 .version = OVS_DATAPATH_VERSION,
2a4999f3
PS
1592 .maxattr = OVS_DP_ATTR_MAX,
1593 SET_NETNSOK
14002a59 1594 SET_PARALLEL_OPS
aaff4b55
BP
1595};
1596
850b6b3b 1597static struct genl_multicast_group ovs_dp_datapath_multicast_group = {
df2c07f4 1598 .name = OVS_DATAPATH_MCGROUP
aaff4b55
BP
1599};
1600
0afa2373
TG
1601static size_t ovs_dp_cmd_msg_size(void)
1602{
1603 size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
1604
1605 msgsize += nla_total_size(IFNAMSIZ);
1606 msgsize += nla_total_size(sizeof(struct ovs_dp_stats));
1607
1608 return msgsize;
1609}
1610
df2c07f4 1611static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
28aea917 1612 u32 portid, u32 seq, u32 flags, u8 cmd)
064af421 1613{
df2c07f4 1614 struct ovs_header *ovs_header;
e926dfe3 1615 struct ovs_dp_stats dp_stats;
064af421
BP
1616 int err;
1617
28aea917 1618 ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
aaff4b55 1619 flags, cmd);
df2c07f4 1620 if (!ovs_header)
aaff4b55 1621 goto error;
064af421 1622
b063d9f0 1623 ovs_header->dp_ifindex = get_dpifindex(dp);
064af421 1624
d6569377 1625 rcu_read_lock();
850b6b3b 1626 err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
d6569377 1627 rcu_read_unlock();
064af421 1628 if (err)
d6569377 1629 goto nla_put_failure;
064af421 1630
e926dfe3 1631 get_dp_stats(dp, &dp_stats);
c3cc8c03
DM
1632 if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats), &dp_stats))
1633 goto nla_put_failure;
d6569377 1634
df2c07f4 1635 return genlmsg_end(skb, ovs_header);
d6569377
BP
1636
1637nla_put_failure:
df2c07f4 1638 genlmsg_cancel(skb, ovs_header);
aaff4b55
BP
1639error:
1640 return -EMSGSIZE;
d6569377
BP
1641}
1642
28aea917 1643static struct sk_buff *ovs_dp_cmd_build_info(struct datapath *dp, u32 portid,
aaff4b55 1644 u32 seq, u8 cmd)
d6569377 1645{
d6569377 1646 struct sk_buff *skb;
aaff4b55 1647 int retval;
d6569377 1648
0afa2373 1649 skb = genlmsg_new(ovs_dp_cmd_msg_size(), GFP_KERNEL);
064af421 1650 if (!skb)
d6569377 1651 return ERR_PTR(-ENOMEM);
659586ef 1652
28aea917 1653 retval = ovs_dp_cmd_fill_info(dp, skb, portid, seq, 0, cmd);
aaff4b55
BP
1654 if (retval < 0) {
1655 kfree_skb(skb);
1656 return ERR_PTR(retval);
1657 }
1658 return skb;
1659}
9dca7bd5 1660
df2c07f4 1661static int ovs_dp_cmd_validate(struct nlattr *a[OVS_DP_ATTR_MAX + 1])
aaff4b55 1662{
df2c07f4 1663 return CHECK_NUL_STRING(a[OVS_DP_ATTR_NAME], IFNAMSIZ - 1);
d6569377
BP
1664}
1665
cd2a59e9 1666/* Called with ovs_mutex. */
2a4999f3
PS
1667static struct datapath *lookup_datapath(struct net *net,
1668 struct ovs_header *ovs_header,
6455100f 1669 struct nlattr *a[OVS_DP_ATTR_MAX + 1])
d6569377 1670{
254f2dc8
BP
1671 struct datapath *dp;
1672
df2c07f4 1673 if (!a[OVS_DP_ATTR_NAME])
2a4999f3 1674 dp = get_dp(net, ovs_header->dp_ifindex);
254f2dc8 1675 else {
d6569377 1676 struct vport *vport;
d6569377 1677
057dd6d2 1678 rcu_read_lock();
2a4999f3 1679 vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
df2c07f4 1680 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
057dd6d2 1681 rcu_read_unlock();
d6569377 1682 }
254f2dc8 1683 return dp ? dp : ERR_PTR(-ENODEV);
d6569377
BP
1684}
1685
df2c07f4 1686static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
d6569377 1687{
aaff4b55 1688 struct nlattr **a = info->attrs;
d6569377 1689 struct vport_parms parms;
aaff4b55 1690 struct sk_buff *reply;
d6569377
BP
1691 struct datapath *dp;
1692 struct vport *vport;
2a4999f3 1693 struct ovs_net *ovs_net;
95b1d73a 1694 int err, i;
d6569377 1695
d6569377 1696 err = -EINVAL;
ea36840f 1697 if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
aaff4b55
BP
1698 goto err;
1699
df2c07f4 1700 err = ovs_dp_cmd_validate(a);
aaff4b55
BP
1701 if (err)
1702 goto err;
d6569377 1703
cd2a59e9 1704 ovs_lock();
d6569377 1705
d6569377
BP
1706 err = -ENOMEM;
1707 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1708 if (dp == NULL)
cd2a59e9 1709 goto err_unlock_ovs;
2a4999f3 1710
0ceaa66c
JG
1711 ovs_dp_set_net(dp, hold_net(sock_net(skb->sk)));
1712
d6569377
BP
1713 /* Allocate table. */
1714 err = -ENOMEM;
850b6b3b 1715 rcu_assign_pointer(dp->table, ovs_flow_tbl_alloc(TBL_MIN_BUCKETS));
d6569377
BP
1716 if (!dp->table)
1717 goto err_free_dp;
1718
99769a40
JG
1719 dp->stats_percpu = alloc_percpu(struct dp_stats_percpu);
1720 if (!dp->stats_percpu) {
1721 err = -ENOMEM;
1722 goto err_destroy_table;
1723 }
1724
95b1d73a
PS
1725 dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
1726 GFP_KERNEL);
1727 if (!dp->ports) {
1728 err = -ENOMEM;
1729 goto err_destroy_percpu;
1730 }
1731
1732 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1733 INIT_HLIST_HEAD(&dp->ports[i]);
1734
d6569377 1735 /* Set up our datapath device. */
df2c07f4
JP
1736 parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1737 parms.type = OVS_VPORT_TYPE_INTERNAL;
d6569377
BP
1738 parms.options = NULL;
1739 parms.dp = dp;
df2c07f4 1740 parms.port_no = OVSP_LOCAL;
28aea917 1741 parms.upcall_portid = nla_get_u32(a[OVS_DP_ATTR_UPCALL_PID]);
b063d9f0 1742
d6569377
BP
1743 vport = new_vport(&parms);
1744 if (IS_ERR(vport)) {
1745 err = PTR_ERR(vport);
1746 if (err == -EBUSY)
1747 err = -EEXIST;
1748
95b1d73a 1749 goto err_destroy_ports_array;
d6569377 1750 }
d6569377 1751
28aea917 1752 reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
6455100f 1753 info->snd_seq, OVS_DP_CMD_NEW);
aaff4b55
BP
1754 err = PTR_ERR(reply);
1755 if (IS_ERR(reply))
1756 goto err_destroy_local_port;
1757
2a4999f3 1758 ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
fb93e9aa 1759 list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
d6569377 1760
cd2a59e9 1761 ovs_unlock();
d6569377 1762
e297c6b7 1763 ovs_notify(reply, info, &ovs_dp_datapath_multicast_group);
d6569377
BP
1764 return 0;
1765
1766err_destroy_local_port:
cd2a59e9 1767 ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
95b1d73a
PS
1768err_destroy_ports_array:
1769 kfree(dp->ports);
99769a40
JG
1770err_destroy_percpu:
1771 free_percpu(dp->stats_percpu);
d6569377 1772err_destroy_table:
a1c564be 1773 ovs_flow_tbl_destroy(ovsl_dereference(dp->table), false);
d6569377 1774err_free_dp:
0ceaa66c 1775 release_net(ovs_dp_get_net(dp));
d6569377 1776 kfree(dp);
cd2a59e9
PS
1777err_unlock_ovs:
1778 ovs_unlock();
d6569377 1779err:
064af421
BP
1780 return err;
1781}
1782
cd2a59e9 1783/* Called with ovs_mutex. */
2a4999f3 1784static void __dp_destroy(struct datapath *dp)
44e05eca 1785{
95b1d73a 1786 int i;
44e05eca 1787
95b1d73a
PS
1788 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1789 struct vport *vport;
f8dfbcb7 1790 struct hlist_node *n;
95b1d73a 1791
f8dfbcb7 1792 hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
95b1d73a
PS
1793 if (vport->port_no != OVSP_LOCAL)
1794 ovs_dp_detach_port(vport);
1795 }
ed099e92 1796
fb93e9aa 1797 list_del_rcu(&dp->list_node);
ed099e92 1798
cd2a59e9
PS
1799 /* OVSP_LOCAL is datapath internal port. We need to make sure that
1800 * all port in datapath are destroyed first before freeing datapath.
1801 */
1802 ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
99620d2c 1803
ed099e92 1804 call_rcu(&dp->rcu, destroy_dp_rcu);
2a4999f3
PS
1805}
1806
1807static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1808{
1809 struct sk_buff *reply;
1810 struct datapath *dp;
1811 int err;
1812
1813 err = ovs_dp_cmd_validate(info->attrs);
1814 if (err)
1815 return err;
1816
cd2a59e9 1817 ovs_lock();
2a4999f3
PS
1818 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1819 err = PTR_ERR(dp);
1820 if (IS_ERR(dp))
cd2a59e9 1821 goto unlock;
2a4999f3 1822
28aea917 1823 reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
2a4999f3
PS
1824 info->snd_seq, OVS_DP_CMD_DEL);
1825 err = PTR_ERR(reply);
1826 if (IS_ERR(reply))
cd2a59e9 1827 goto unlock;
2a4999f3
PS
1828
1829 __dp_destroy(dp);
cd2a59e9 1830 ovs_unlock();
ed099e92 1831
e297c6b7 1832 ovs_notify(reply, info, &ovs_dp_datapath_multicast_group);
99620d2c
JG
1833
1834 return 0;
cd2a59e9
PS
1835unlock:
1836 ovs_unlock();
1837 return err;
44e05eca
BP
1838}
1839
df2c07f4 1840static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
064af421 1841{
aaff4b55 1842 struct sk_buff *reply;
d6569377 1843 struct datapath *dp;
d6569377 1844 int err;
064af421 1845
df2c07f4 1846 err = ovs_dp_cmd_validate(info->attrs);
aaff4b55
BP
1847 if (err)
1848 return err;
38c6ecbc 1849
cd2a59e9 1850 ovs_lock();
2a4999f3 1851 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
cd2a59e9 1852 err = PTR_ERR(dp);
d6569377 1853 if (IS_ERR(dp))
cd2a59e9 1854 goto unlock;
38c6ecbc 1855
28aea917 1856 reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
6455100f 1857 info->snd_seq, OVS_DP_CMD_NEW);
aaff4b55
BP
1858 if (IS_ERR(reply)) {
1859 err = PTR_ERR(reply);
2a4999f3 1860 netlink_set_err(GENL_SOCK(sock_net(skb->sk)), 0,
850b6b3b 1861 ovs_dp_datapath_multicast_group.id, err);
cd2a59e9
PS
1862 err = 0;
1863 goto unlock;
aaff4b55
BP
1864 }
1865
cd2a59e9 1866 ovs_unlock();
e297c6b7 1867 ovs_notify(reply, info, &ovs_dp_datapath_multicast_group);
850b6b3b 1868
aaff4b55 1869 return 0;
cd2a59e9
PS
1870unlock:
1871 ovs_unlock();
1872 return err;
064af421
BP
1873}
1874
df2c07f4 1875static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1dcf111b 1876{
aaff4b55 1877 struct sk_buff *reply;
d6569377 1878 struct datapath *dp;
d6569377 1879 int err;
1dcf111b 1880
df2c07f4 1881 err = ovs_dp_cmd_validate(info->attrs);
aaff4b55
BP
1882 if (err)
1883 return err;
1dcf111b 1884
cd2a59e9 1885 ovs_lock();
2a4999f3 1886 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
cd2a59e9
PS
1887 if (IS_ERR(dp)) {
1888 err = PTR_ERR(dp);
1889 goto unlock;
1890 }
1dcf111b 1891
28aea917 1892 reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
6455100f 1893 info->snd_seq, OVS_DP_CMD_NEW);
cd2a59e9
PS
1894 if (IS_ERR(reply)) {
1895 err = PTR_ERR(reply);
1896 goto unlock;
1897 }
aaff4b55 1898
cd2a59e9 1899 ovs_unlock();
aaff4b55 1900 return genlmsg_reply(reply, info);
cd2a59e9
PS
1901
1902unlock:
1903 ovs_unlock();
1904 return err;
1dcf111b
JP
1905}
1906
df2c07f4 1907static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
a7786963 1908{
2a4999f3 1909 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
254f2dc8
BP
1910 struct datapath *dp;
1911 int skip = cb->args[0];
1912 int i = 0;
a7786963 1913
fb93e9aa
PS
1914 rcu_read_lock();
1915 list_for_each_entry_rcu(dp, &ovs_net->dps, list_node) {
a2bab2f0 1916 if (i >= skip &&
28aea917 1917 ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
aaff4b55 1918 cb->nlh->nlmsg_seq, NLM_F_MULTI,
df2c07f4 1919 OVS_DP_CMD_NEW) < 0)
aaff4b55 1920 break;
254f2dc8 1921 i++;
a7786963 1922 }
fb93e9aa 1923 rcu_read_unlock();
aaff4b55 1924
254f2dc8
BP
1925 cb->args[0] = i;
1926
aaff4b55 1927 return skb->len;
c19e6535
BP
1928}
1929
aaff4b55 1930static struct genl_ops dp_datapath_genl_ops[] = {
df2c07f4 1931 { .cmd = OVS_DP_CMD_NEW,
aaff4b55
BP
1932 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1933 .policy = datapath_policy,
df2c07f4 1934 .doit = ovs_dp_cmd_new
aaff4b55 1935 },
df2c07f4 1936 { .cmd = OVS_DP_CMD_DEL,
aaff4b55
BP
1937 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1938 .policy = datapath_policy,
df2c07f4 1939 .doit = ovs_dp_cmd_del
aaff4b55 1940 },
df2c07f4 1941 { .cmd = OVS_DP_CMD_GET,
aaff4b55
BP
1942 .flags = 0, /* OK for unprivileged users. */
1943 .policy = datapath_policy,
df2c07f4
JP
1944 .doit = ovs_dp_cmd_get,
1945 .dumpit = ovs_dp_cmd_dump
aaff4b55 1946 },
df2c07f4 1947 { .cmd = OVS_DP_CMD_SET,
aaff4b55
BP
1948 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1949 .policy = datapath_policy,
df2c07f4 1950 .doit = ovs_dp_cmd_set,
aaff4b55
BP
1951 },
1952};
1953
df2c07f4 1954static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
f0fef760 1955#ifdef HAVE_NLA_NUL_STRING
df2c07f4 1956 [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
f613a0d7 1957 [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
f0fef760 1958#else
f613a0d7 1959 [OVS_VPORT_ATTR_STATS] = { .minlen = sizeof(struct ovs_vport_stats) },
f0fef760 1960#endif
d48c88ec
JG
1961 [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
1962 [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
b063d9f0 1963 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
df2c07f4 1964 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
c19e6535
BP
1965};
1966
f0fef760
BP
1967static struct genl_family dp_vport_genl_family = {
1968 .id = GENL_ID_GENERATE,
df2c07f4
JP
1969 .hdrsize = sizeof(struct ovs_header),
1970 .name = OVS_VPORT_FAMILY,
69685a88 1971 .version = OVS_VPORT_VERSION,
2a4999f3
PS
1972 .maxattr = OVS_VPORT_ATTR_MAX,
1973 SET_NETNSOK
14002a59 1974 SET_PARALLEL_OPS
f0fef760
BP
1975};
1976
850b6b3b 1977struct genl_multicast_group ovs_dp_vport_multicast_group = {
df2c07f4 1978 .name = OVS_VPORT_MCGROUP
f0fef760
BP
1979};
1980
cd2a59e9 1981/* Called with ovs_mutex or RCU read lock. */
df2c07f4 1982static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
28aea917 1983 u32 portid, u32 seq, u32 flags, u8 cmd)
064af421 1984{
df2c07f4 1985 struct ovs_header *ovs_header;
e926dfe3 1986 struct ovs_vport_stats vport_stats;
c19e6535
BP
1987 int err;
1988
28aea917 1989 ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
f0fef760 1990 flags, cmd);
df2c07f4 1991 if (!ovs_header)
f0fef760 1992 return -EMSGSIZE;
c19e6535 1993
99769a40 1994 ovs_header->dp_ifindex = get_dpifindex(vport->dp);
c19e6535 1995
c3cc8c03
DM
1996 if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
1997 nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
1998 nla_put_string(skb, OVS_VPORT_ATTR_NAME, vport->ops->get_name(vport)) ||
28aea917 1999 nla_put_u32(skb, OVS_VPORT_ATTR_UPCALL_PID, vport->upcall_portid))
c3cc8c03 2000 goto nla_put_failure;
c19e6535 2001
850b6b3b 2002 ovs_vport_get_stats(vport, &vport_stats);
c3cc8c03
DM
2003 if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
2004 &vport_stats))
2005 goto nla_put_failure;
c19e6535 2006
850b6b3b 2007 err = ovs_vport_get_options(vport, skb);
f0fef760
BP
2008 if (err == -EMSGSIZE)
2009 goto error;
c19e6535 2010
df2c07f4 2011 return genlmsg_end(skb, ovs_header);
c19e6535
BP
2012
2013nla_put_failure:
2014 err = -EMSGSIZE;
f0fef760 2015error:
df2c07f4 2016 genlmsg_cancel(skb, ovs_header);
f0fef760 2017 return err;
064af421
BP
2018}
2019
cd2a59e9 2020/* Called with ovs_mutex or RCU read lock. */
28aea917 2021struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
f14d8083 2022 u32 seq, u8 cmd)
064af421 2023{
c19e6535 2024 struct sk_buff *skb;
f0fef760 2025 int retval;
c19e6535 2026
f0fef760 2027 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
c19e6535
BP
2028 if (!skb)
2029 return ERR_PTR(-ENOMEM);
2030
28aea917 2031 retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
c25ea534
JG
2032 BUG_ON(retval < 0);
2033
c19e6535 2034 return skb;
f0fef760 2035}
c19e6535 2036
df2c07f4 2037static int ovs_vport_cmd_validate(struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
f0fef760 2038{
df2c07f4 2039 return CHECK_NUL_STRING(a[OVS_VPORT_ATTR_NAME], IFNAMSIZ - 1);
c19e6535 2040}
51d4d598 2041
cd2a59e9 2042/* Called with ovs_mutex or RCU read lock. */
2a4999f3
PS
2043static struct vport *lookup_vport(struct net *net,
2044 struct ovs_header *ovs_header,
df2c07f4 2045 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
c19e6535
BP
2046{
2047 struct datapath *dp;
2048 struct vport *vport;
2049
df2c07f4 2050 if (a[OVS_VPORT_ATTR_NAME]) {
2a4999f3 2051 vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
ed099e92 2052 if (!vport)
c19e6535 2053 return ERR_PTR(-ENODEV);
24ce832d
BP
2054 if (ovs_header->dp_ifindex &&
2055 ovs_header->dp_ifindex != get_dpifindex(vport->dp))
2056 return ERR_PTR(-ENODEV);
c19e6535 2057 return vport;
df2c07f4
JP
2058 } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
2059 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
c19e6535
BP
2060
2061 if (port_no >= DP_MAX_PORTS)
f0fef760 2062 return ERR_PTR(-EFBIG);
c19e6535 2063
2a4999f3 2064 dp = get_dp(net, ovs_header->dp_ifindex);
c19e6535
BP
2065 if (!dp)
2066 return ERR_PTR(-ENODEV);
f2459fe7 2067
cd2a59e9 2068 vport = ovs_vport_ovsl_rcu(dp, port_no);
ed099e92 2069 if (!vport)
17535c57 2070 return ERR_PTR(-ENODEV);
c19e6535
BP
2071 return vport;
2072 } else
2073 return ERR_PTR(-EINVAL);
064af421
BP
2074}
2075
df2c07f4 2076static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
c19e6535 2077{
f0fef760 2078 struct nlattr **a = info->attrs;
df2c07f4 2079 struct ovs_header *ovs_header = info->userhdr;
c19e6535 2080 struct vport_parms parms;
ed099e92 2081 struct sk_buff *reply;
c19e6535 2082 struct vport *vport;
c19e6535 2083 struct datapath *dp;
b0ec0f27 2084 u32 port_no;
c19e6535 2085 int err;
b0ec0f27 2086
c19e6535 2087 err = -EINVAL;
ea36840f
BP
2088 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
2089 !a[OVS_VPORT_ATTR_UPCALL_PID])
f0fef760
BP
2090 goto exit;
2091
df2c07f4 2092 err = ovs_vport_cmd_validate(a);
f0fef760
BP
2093 if (err)
2094 goto exit;
51d4d598 2095
cd2a59e9 2096 ovs_lock();
2a4999f3 2097 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
c19e6535
BP
2098 err = -ENODEV;
2099 if (!dp)
ed099e92 2100 goto exit_unlock;
c19e6535 2101
df2c07f4
JP
2102 if (a[OVS_VPORT_ATTR_PORT_NO]) {
2103 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
c19e6535
BP
2104
2105 err = -EFBIG;
2106 if (port_no >= DP_MAX_PORTS)
ed099e92 2107 goto exit_unlock;
c19e6535 2108
cd2a59e9 2109 vport = ovs_vport_ovsl(dp, port_no);
c19e6535
BP
2110 err = -EBUSY;
2111 if (vport)
ed099e92 2112 goto exit_unlock;
c19e6535
BP
2113 } else {
2114 for (port_no = 1; ; port_no++) {
2115 if (port_no >= DP_MAX_PORTS) {
2116 err = -EFBIG;
ed099e92 2117 goto exit_unlock;
c19e6535 2118 }
cd2a59e9 2119 vport = ovs_vport_ovsl(dp, port_no);
c19e6535
BP
2120 if (!vport)
2121 break;
51d4d598 2122 }
064af421 2123 }
b0ec0f27 2124
df2c07f4
JP
2125 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
2126 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
2127 parms.options = a[OVS_VPORT_ATTR_OPTIONS];
c19e6535
BP
2128 parms.dp = dp;
2129 parms.port_no = port_no;
28aea917 2130 parms.upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
c19e6535
BP
2131
2132 vport = new_vport(&parms);
2133 err = PTR_ERR(vport);
2134 if (IS_ERR(vport))
ed099e92 2135 goto exit_unlock;
c19e6535 2136
faef6d2d 2137 err = 0;
1fc7083d
JG
2138 if (a[OVS_VPORT_ATTR_STATS])
2139 ovs_vport_set_stats(vport, nla_data(a[OVS_VPORT_ATTR_STATS]));
2140
2141 reply = ovs_vport_cmd_build_info(vport, info->snd_portid, info->snd_seq,
2142 OVS_VPORT_CMD_NEW);
2143 if (IS_ERR(reply)) {
2144 err = PTR_ERR(reply);
850b6b3b 2145 ovs_dp_detach_port(vport);
ed099e92 2146 goto exit_unlock;
c19e6535 2147 }
e297c6b7
TG
2148
2149 ovs_notify(reply, info, &ovs_dp_vport_multicast_group);
c19e6535 2150
ed099e92 2151exit_unlock:
cd2a59e9 2152 ovs_unlock();
c19e6535
BP
2153exit:
2154 return err;
44e05eca
BP
2155}
2156
df2c07f4 2157static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
44e05eca 2158{
f0fef760
BP
2159 struct nlattr **a = info->attrs;
2160 struct sk_buff *reply;
c19e6535 2161 struct vport *vport;
c19e6535 2162 int err;
44e05eca 2163
df2c07f4 2164 err = ovs_vport_cmd_validate(a);
f0fef760 2165 if (err)
c19e6535
BP
2166 goto exit;
2167
cd2a59e9 2168 ovs_lock();
2a4999f3 2169 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
c19e6535
BP
2170 err = PTR_ERR(vport);
2171 if (IS_ERR(vport))
f0fef760 2172 goto exit_unlock;
44e05eca 2173
6455100f 2174 if (a[OVS_VPORT_ATTR_TYPE] &&
17ec1d04 2175 nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
4879d4c7 2176 err = -EINVAL;
17ec1d04
JG
2177 goto exit_unlock;
2178 }
6455100f 2179
c25ea534
JG
2180 reply = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2181 if (!reply) {
2182 err = -ENOMEM;
2183 goto exit_unlock;
2184 }
2185
17ec1d04 2186 if (a[OVS_VPORT_ATTR_OPTIONS]) {
850b6b3b 2187 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
17ec1d04
JG
2188 if (err)
2189 goto exit_free;
2190 }
1fc7083d
JG
2191
2192 if (a[OVS_VPORT_ATTR_STATS])
2193 ovs_vport_set_stats(vport, nla_data(a[OVS_VPORT_ATTR_STATS]));
2194
2195 if (a[OVS_VPORT_ATTR_UPCALL_PID])
28aea917 2196 vport->upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
c19e6535 2197
c25ea534
JG
2198 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
2199 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
2200 BUG_ON(err < 0);
f0fef760 2201
cd2a59e9 2202 ovs_unlock();
8680ae4d 2203 ovs_notify(reply, info, &ovs_dp_vport_multicast_group);
c25ea534
JG
2204 return 0;
2205
2206exit_free:
2207 kfree_skb(reply);
f0fef760 2208exit_unlock:
cd2a59e9 2209 ovs_unlock();
c19e6535
BP
2210exit:
2211 return err;
064af421
BP
2212}
2213
df2c07f4 2214static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
7c40efc9 2215{
f0fef760
BP
2216 struct nlattr **a = info->attrs;
2217 struct sk_buff *reply;
c19e6535 2218 struct vport *vport;
c19e6535
BP
2219 int err;
2220
df2c07f4 2221 err = ovs_vport_cmd_validate(a);
f0fef760 2222 if (err)
c19e6535
BP
2223 goto exit;
2224
cd2a59e9 2225 ovs_lock();
2a4999f3 2226 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
c19e6535 2227 err = PTR_ERR(vport);
f0fef760
BP
2228 if (IS_ERR(vport))
2229 goto exit_unlock;
c19e6535 2230
df2c07f4 2231 if (vport->port_no == OVSP_LOCAL) {
f0fef760
BP
2232 err = -EINVAL;
2233 goto exit_unlock;
2234 }
2235
28aea917
IY
2236 reply = ovs_vport_cmd_build_info(vport, info->snd_portid,
2237 info->snd_seq, OVS_VPORT_CMD_DEL);
f0fef760
BP
2238 err = PTR_ERR(reply);
2239 if (IS_ERR(reply))
2240 goto exit_unlock;
2241
b57d5819 2242 err = 0;
850b6b3b 2243 ovs_dp_detach_port(vport);
f0fef760 2244
e297c6b7 2245 ovs_notify(reply, info, &ovs_dp_vport_multicast_group);
f0fef760
BP
2246
2247exit_unlock:
cd2a59e9 2248 ovs_unlock();
c19e6535
BP
2249exit:
2250 return err;
7c40efc9
BP
2251}
2252
df2c07f4 2253static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
7c40efc9 2254{
f0fef760 2255 struct nlattr **a = info->attrs;
df2c07f4 2256 struct ovs_header *ovs_header = info->userhdr;
ed099e92 2257 struct sk_buff *reply;
c19e6535 2258 struct vport *vport;
c19e6535
BP
2259 int err;
2260
df2c07f4 2261 err = ovs_vport_cmd_validate(a);
f0fef760
BP
2262 if (err)
2263 goto exit;
c19e6535 2264
ed099e92 2265 rcu_read_lock();
2a4999f3 2266 vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
c19e6535
BP
2267 err = PTR_ERR(vport);
2268 if (IS_ERR(vport))
f0fef760 2269 goto exit_unlock;
c19e6535 2270
28aea917
IY
2271 reply = ovs_vport_cmd_build_info(vport, info->snd_portid,
2272 info->snd_seq, OVS_VPORT_CMD_NEW);
ed099e92
BP
2273 err = PTR_ERR(reply);
2274 if (IS_ERR(reply))
f0fef760 2275 goto exit_unlock;
ed099e92 2276
df2fa9b5
JG
2277 rcu_read_unlock();
2278
2279 return genlmsg_reply(reply, info);
ed099e92 2280
f0fef760 2281exit_unlock:
ed099e92 2282 rcu_read_unlock();
f0fef760 2283exit:
c19e6535
BP
2284 return err;
2285}
2286
df2c07f4 2287static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
c19e6535 2288{
df2c07f4 2289 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
c19e6535 2290 struct datapath *dp;
95b1d73a
PS
2291 int bucket = cb->args[0], skip = cb->args[1];
2292 int i, j = 0;
c19e6535 2293
2a4999f3 2294 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
c19e6535 2295 if (!dp)
f0fef760 2296 return -ENODEV;
ed099e92
BP
2297
2298 rcu_read_lock();
95b1d73a 2299 for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
ed099e92 2300 struct vport *vport;
95b1d73a
PS
2301
2302 j = 0;
f8dfbcb7 2303 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
95b1d73a
PS
2304 if (j >= skip &&
2305 ovs_vport_cmd_fill_info(vport, skb,
28aea917 2306 NETLINK_CB(cb->skb).portid,
95b1d73a
PS
2307 cb->nlh->nlmsg_seq,
2308 NLM_F_MULTI,
2309 OVS_VPORT_CMD_NEW) < 0)
2310 goto out;
2311
2312 j++;
2313 }
2314 skip = 0;
c19e6535 2315 }
95b1d73a 2316out:
ed099e92 2317 rcu_read_unlock();
c19e6535 2318
95b1d73a
PS
2319 cb->args[0] = i;
2320 cb->args[1] = j;
f0fef760 2321
95b1d73a 2322 return skb->len;
7c40efc9
BP
2323}
2324
f0fef760 2325static struct genl_ops dp_vport_genl_ops[] = {
df2c07f4 2326 { .cmd = OVS_VPORT_CMD_NEW,
f0fef760
BP
2327 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2328 .policy = vport_policy,
df2c07f4 2329 .doit = ovs_vport_cmd_new
f0fef760 2330 },
df2c07f4 2331 { .cmd = OVS_VPORT_CMD_DEL,
f0fef760
BP
2332 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2333 .policy = vport_policy,
df2c07f4 2334 .doit = ovs_vport_cmd_del
f0fef760 2335 },
df2c07f4 2336 { .cmd = OVS_VPORT_CMD_GET,
f0fef760
BP
2337 .flags = 0, /* OK for unprivileged users. */
2338 .policy = vport_policy,
df2c07f4
JP
2339 .doit = ovs_vport_cmd_get,
2340 .dumpit = ovs_vport_cmd_dump
f0fef760 2341 },
df2c07f4 2342 { .cmd = OVS_VPORT_CMD_SET,
f0fef760
BP
2343 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2344 .policy = vport_policy,
df2c07f4 2345 .doit = ovs_vport_cmd_set,
f0fef760
BP
2346 },
2347};
2348
982b8810
BP
2349struct genl_family_and_ops {
2350 struct genl_family *family;
2351 struct genl_ops *ops;
2352 int n_ops;
2353 struct genl_multicast_group *group;
2354};
ed099e92 2355
982b8810 2356static const struct genl_family_and_ops dp_genl_families[] = {
aaff4b55
BP
2357 { &dp_datapath_genl_family,
2358 dp_datapath_genl_ops, ARRAY_SIZE(dp_datapath_genl_ops),
850b6b3b 2359 &ovs_dp_datapath_multicast_group },
f0fef760
BP
2360 { &dp_vport_genl_family,
2361 dp_vport_genl_ops, ARRAY_SIZE(dp_vport_genl_ops),
850b6b3b 2362 &ovs_dp_vport_multicast_group },
37a1300c
BP
2363 { &dp_flow_genl_family,
2364 dp_flow_genl_ops, ARRAY_SIZE(dp_flow_genl_ops),
850b6b3b 2365 &ovs_dp_flow_multicast_group },
982b8810
BP
2366 { &dp_packet_genl_family,
2367 dp_packet_genl_ops, ARRAY_SIZE(dp_packet_genl_ops),
2368 NULL },
2369};
ed099e92 2370
982b8810
BP
2371static void dp_unregister_genl(int n_families)
2372{
2373 int i;
ed099e92 2374
b867ca75 2375 for (i = 0; i < n_families; i++)
982b8810 2376 genl_unregister_family(dp_genl_families[i].family);
ed099e92
BP
2377}
2378
982b8810 2379static int dp_register_genl(void)
064af421 2380{
982b8810
BP
2381 int n_registered;
2382 int err;
2383 int i;
064af421 2384
982b8810
BP
2385 n_registered = 0;
2386 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
2387 const struct genl_family_and_ops *f = &dp_genl_families[i];
064af421 2388
982b8810
BP
2389 err = genl_register_family_with_ops(f->family, f->ops,
2390 f->n_ops);
2391 if (err)
2392 goto error;
2393 n_registered++;
e22d4953 2394
982b8810
BP
2395 if (f->group) {
2396 err = genl_register_mc_group(f->family, f->group);
2397 if (err)
2398 goto error;
2399 }
2400 }
9cc8b4e4 2401
982b8810 2402 return 0;
064af421
BP
2403
2404error:
982b8810
BP
2405 dp_unregister_genl(n_registered);
2406 return err;
064af421
BP
2407}
2408
cd2a59e9 2409static void rehash_flow_table(struct work_struct *work)
acd051f1
PS
2410{
2411 struct datapath *dp;
2a4999f3
PS
2412 struct net *net;
2413
cd2a59e9 2414 ovs_lock();
2a4999f3
PS
2415 rtnl_lock();
2416 for_each_net(net) {
2417 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
acd051f1 2418
2a4999f3 2419 list_for_each_entry(dp, &ovs_net->dps, list_node) {
cd2a59e9 2420 struct flow_table *old_table = ovsl_dereference(dp->table);
2a4999f3 2421 struct flow_table *new_table;
acd051f1 2422
2a4999f3
PS
2423 new_table = ovs_flow_tbl_rehash(old_table);
2424 if (!IS_ERR(new_table)) {
2425 rcu_assign_pointer(dp->table, new_table);
a1c564be 2426 ovs_flow_tbl_destroy(old_table, true);
2a4999f3 2427 }
acd051f1
PS
2428 }
2429 }
2a4999f3 2430 rtnl_unlock();
cd2a59e9 2431 ovs_unlock();
acd051f1
PS
2432 schedule_delayed_work(&rehash_flow_wq, REHASH_FLOW_INTERVAL);
2433}
2434
2a4999f3
PS
2435static int __net_init ovs_init_net(struct net *net)
2436{
2437 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2438
2439 INIT_LIST_HEAD(&ovs_net->dps);
cd2a59e9 2440 INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
2a4999f3
PS
2441 return 0;
2442}
2443
2444static void __net_exit ovs_exit_net(struct net *net)
2445{
cd2a59e9 2446 struct datapath *dp, *dp_next;
2a4999f3
PS
2447 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2448
cd2a59e9
PS
2449 ovs_lock();
2450 list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
2451 __dp_destroy(dp);
2452 ovs_unlock();
2453
2454 cancel_work_sync(&ovs_net->dp_notify_work);
2a4999f3
PS
2455}
2456
2457static struct pernet_operations ovs_net_ops = {
2458 .init = ovs_init_net,
2459 .exit = ovs_exit_net,
2460 .id = &ovs_net_id,
2461 .size = sizeof(struct ovs_net),
2462};
2463
637c8268
PS
2464DEFINE_COMPAT_PNET_REG_FUNC(device);
2465
22d24ebf
BP
2466static int __init dp_init(void)
2467{
2468 int err;
2469
f3d85db3 2470 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
22d24ebf 2471
dc5f3fef 2472 pr_info("Open vSwitch switching datapath %s, built "__DATE__" "__TIME__"\n",
8a07709c 2473 VERSION);
064af421 2474
16d650e5 2475 err = ovs_workqueues_init();
b9c15df9 2476 if (err)
cd2a59e9 2477 goto error;
b9c15df9 2478
850b6b3b 2479 err = ovs_flow_init();
3544358a 2480 if (err)
85c9de19 2481 goto error_wq;
3544358a 2482
850b6b3b 2483 err = ovs_vport_init();
064af421
BP
2484 if (err)
2485 goto error_flow_exit;
2486
2a4999f3 2487 err = register_pernet_device(&ovs_net_ops);
f2459fe7
JG
2488 if (err)
2489 goto error_vport_exit;
2490
2a4999f3
PS
2491 err = register_netdevice_notifier(&ovs_dp_device_notifier);
2492 if (err)
2493 goto error_netns_exit;
2494
982b8810
BP
2495 err = dp_register_genl();
2496 if (err < 0)
37a1300c 2497 goto error_unreg_notifier;
982b8810 2498
acd051f1
PS
2499 schedule_delayed_work(&rehash_flow_wq, REHASH_FLOW_INTERVAL);
2500
064af421
BP
2501 return 0;
2502
2503error_unreg_notifier:
850b6b3b 2504 unregister_netdevice_notifier(&ovs_dp_device_notifier);
2a4999f3
PS
2505error_netns_exit:
2506 unregister_pernet_device(&ovs_net_ops);
f2459fe7 2507error_vport_exit:
850b6b3b 2508 ovs_vport_exit();
064af421 2509error_flow_exit:
850b6b3b 2510 ovs_flow_exit();
16d650e5
PS
2511error_wq:
2512 ovs_workqueues_exit();
064af421
BP
2513error:
2514 return err;
2515}
2516
2517static void dp_cleanup(void)
2518{
acd051f1 2519 cancel_delayed_work_sync(&rehash_flow_wq);
982b8810 2520 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
850b6b3b 2521 unregister_netdevice_notifier(&ovs_dp_device_notifier);
2a4999f3
PS
2522 unregister_pernet_device(&ovs_net_ops);
2523 rcu_barrier();
850b6b3b
JG
2524 ovs_vport_exit();
2525 ovs_flow_exit();
16d650e5 2526 ovs_workqueues_exit();
064af421
BP
2527}
2528
2529module_init(dp_init);
2530module_exit(dp_cleanup);
2531
2532MODULE_DESCRIPTION("Open vSwitch switching datapath");
2533MODULE_LICENSE("GPL");
3d0666d2 2534MODULE_VERSION(VERSION);