]> git.proxmox.com Git - mirror_ovs.git/blame - datapath/datapath.c
datapath: Refactor actions in terms of match fields.
[mirror_ovs.git] / datapath / datapath.c
CommitLineData
064af421 1/*
f632c8fc 2 * Copyright (c) 2007, 2008, 2009, 2010, 2011 Nicira Networks.
a14bc59f
BP
3 * Distributed under the terms of the GNU GPL version 2.
4 *
5 * Significant portions of this file may be copied from parts of the Linux
6 * kernel, by Linus Torvalds and others.
064af421
BP
7 */
8
9/* Functions for managing the dp interface/device. */
10
dfffaef1
JP
11#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12
064af421
BP
13#include <linux/init.h>
14#include <linux/module.h>
064af421 15#include <linux/if_arp.h>
064af421
BP
16#include <linux/if_vlan.h>
17#include <linux/in.h>
18#include <linux/ip.h>
982b8810 19#include <linux/jhash.h>
064af421
BP
20#include <linux/delay.h>
21#include <linux/time.h>
22#include <linux/etherdevice.h>
ed099e92 23#include <linux/genetlink.h>
064af421
BP
24#include <linux/kernel.h>
25#include <linux/kthread.h>
064af421
BP
26#include <linux/mutex.h>
27#include <linux/percpu.h>
28#include <linux/rcupdate.h>
29#include <linux/tcp.h>
30#include <linux/udp.h>
31#include <linux/version.h>
32#include <linux/ethtool.h>
064af421
BP
33#include <linux/wait.h>
34#include <asm/system.h>
35#include <asm/div64.h>
36#include <asm/bug.h>
656a0e37 37#include <linux/highmem.h>
064af421
BP
38#include <linux/netfilter_bridge.h>
39#include <linux/netfilter_ipv4.h>
40#include <linux/inetdevice.h>
41#include <linux/list.h>
077257b8 42#include <linux/openvswitch.h>
064af421 43#include <linux/rculist.h>
064af421 44#include <linux/dmi.h>
3c5f6de3 45#include <net/inet_ecn.h>
36956a7d 46#include <net/genetlink.h>
064af421 47
dd8d6b8c 48#include "checksum.h"
064af421
BP
49#include "datapath.h"
50#include "actions.h"
064af421 51#include "flow.h"
303708cc 52#include "vlan.h"
3544358a 53#include "tunnel.h"
f2459fe7 54#include "vport-internal_dev.h"
064af421 55
4cf41591 56#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18) || \
a0003c0c
PS
57 LINUX_VERSION_CODE >= KERNEL_VERSION(3,2,0)
58#error Kernels before 2.6.18 or after 3.1 are not supported by this version of Open vSwitch.
4cf41591
JG
59#endif
60
064af421
BP
61int (*dp_ioctl_hook)(struct net_device *dev, struct ifreq *rq, int cmd);
62EXPORT_SYMBOL(dp_ioctl_hook);
63
ed099e92
BP
64/**
65 * DOC: Locking:
064af421 66 *
ed099e92
BP
67 * Writes to device state (add/remove datapath, port, set operations on vports,
68 * etc.) are protected by RTNL.
064af421 69 *
ed099e92
BP
70 * Writes to other state (flow table modifications, set miscellaneous datapath
71 * parameters such as drop frags, etc.) are protected by genl_mutex. The RTNL
72 * lock nests inside genl_mutex.
73 *
74 * Reads are protected by RCU.
75 *
76 * There are a few special cases (mostly stats) that have their own
77 * synchronization but they nest under all of above and don't interact with
78 * each other.
064af421 79 */
ed099e92 80
254f2dc8
BP
81/* Global list of datapaths to enable dumping them all out.
82 * Protected by genl_mutex.
83 */
84static LIST_HEAD(dps);
064af421 85
c19e6535 86static struct vport *new_vport(const struct vport_parms *);
98403001 87static int queue_userspace_packets(struct datapath *, struct sk_buff *,
aa5a8fdc 88 const struct dp_upcall_info *);
064af421 89
ed099e92 90/* Must be called with rcu_read_lock, genl_mutex, or RTNL lock. */
254f2dc8 91struct datapath *get_dp(int dp_ifindex)
064af421 92{
254f2dc8
BP
93 struct datapath *dp = NULL;
94 struct net_device *dev;
ed099e92 95
254f2dc8
BP
96 rcu_read_lock();
97 dev = dev_get_by_index_rcu(&init_net, dp_ifindex);
98 if (dev) {
99 struct vport *vport = internal_dev_get_vport(dev);
100 if (vport)
101 dp = vport->dp;
102 }
103 rcu_read_unlock();
104
105 return dp;
064af421
BP
106}
107EXPORT_SYMBOL_GPL(get_dp);
108
ed099e92 109/* Must be called with genl_mutex. */
3544358a 110static struct flow_table *get_table_protected(struct datapath *dp)
9abaf6b3 111{
ed099e92 112 return rcu_dereference_protected(dp->table, lockdep_genl_is_held());
1452b28c
JG
113}
114
ed099e92 115/* Must be called with rcu_read_lock or RTNL lock. */
027f9007 116static struct vport *get_vport_protected(struct datapath *dp, u16 port_no)
1452b28c 117{
ed099e92 118 return rcu_dereference_rtnl(dp->ports[port_no]);
9abaf6b3
JG
119}
120
f2459fe7
JG
121/* Must be called with rcu_read_lock or RTNL lock. */
122const char *dp_name(const struct datapath *dp)
123{
df2c07f4 124 return vport_get_name(rcu_dereference_rtnl(dp->ports[OVSP_LOCAL]));
f2459fe7
JG
125}
126
99769a40
JG
127static int get_dpifindex(struct datapath *dp)
128{
129 struct vport *local;
130 int ifindex;
131
132 rcu_read_lock();
133
134 local = get_vport_protected(dp, OVSP_LOCAL);
135 if (local)
136 ifindex = vport_get_ifindex(local);
137 else
138 ifindex = 0;
139
140 rcu_read_unlock();
141
142 return ifindex;
143}
144
064af421
BP
145static inline size_t br_nlmsg_size(void)
146{
147 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
148 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
149 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
150 + nla_total_size(4) /* IFLA_MASTER */
151 + nla_total_size(4) /* IFLA_MTU */
064af421
BP
152 + nla_total_size(1); /* IFLA_OPERSTATE */
153}
154
ed099e92 155/* Caller must hold RTNL lock. */
064af421 156static int dp_fill_ifinfo(struct sk_buff *skb,
e779d8d9 157 const struct vport *port,
064af421
BP
158 int event, unsigned int flags)
159{
027f9007 160 struct datapath *dp = port->dp;
e779d8d9 161 int ifindex = vport_get_ifindex(port);
064af421
BP
162 struct ifinfomsg *hdr;
163 struct nlmsghdr *nlh;
164
f2459fe7
JG
165 if (ifindex < 0)
166 return ifindex;
167
064af421
BP
168 nlh = nlmsg_put(skb, 0, 0, event, sizeof(*hdr), flags);
169 if (nlh == NULL)
170 return -EMSGSIZE;
171
172 hdr = nlmsg_data(nlh);
173 hdr->ifi_family = AF_BRIDGE;
174 hdr->__ifi_pad = 0;
f2459fe7
JG
175 hdr->ifi_type = ARPHRD_ETHER;
176 hdr->ifi_index = ifindex;
e779d8d9 177 hdr->ifi_flags = vport_get_flags(port);
064af421
BP
178 hdr->ifi_change = 0;
179
e779d8d9 180 NLA_PUT_STRING(skb, IFLA_IFNAME, vport_get_name(port));
99769a40 181 NLA_PUT_U32(skb, IFLA_MASTER, get_dpifindex(dp));
e779d8d9 182 NLA_PUT_U32(skb, IFLA_MTU, vport_get_mtu(port));
064af421
BP
183#ifdef IFLA_OPERSTATE
184 NLA_PUT_U8(skb, IFLA_OPERSTATE,
e779d8d9
BP
185 vport_is_running(port)
186 ? vport_get_operstate(port)
f2459fe7 187 : IF_OPER_DOWN);
064af421
BP
188#endif
189
e779d8d9 190 NLA_PUT(skb, IFLA_ADDRESS, ETH_ALEN, vport_get_addr(port));
064af421 191
064af421
BP
192 return nlmsg_end(skb, nlh);
193
194nla_put_failure:
195 nlmsg_cancel(skb, nlh);
196 return -EMSGSIZE;
197}
198
ed099e92 199/* Caller must hold RTNL lock. */
e779d8d9 200static void dp_ifinfo_notify(int event, struct vport *port)
064af421 201{
064af421
BP
202 struct sk_buff *skb;
203 int err = -ENOBUFS;
204
205 skb = nlmsg_new(br_nlmsg_size(), GFP_KERNEL);
206 if (skb == NULL)
207 goto errout;
208
209 err = dp_fill_ifinfo(skb, port, event, 0);
210 if (err < 0) {
211 /* -EMSGSIZE implies BUG in br_nlmsg_size() */
212 WARN_ON(err == -EMSGSIZE);
213 kfree_skb(skb);
214 goto errout;
215 }
f2459fe7 216 rtnl_notify(skb, &init_net, 0, RTNLGRP_LINK, NULL, GFP_KERNEL);
cfe7c1f5 217 return;
064af421
BP
218errout:
219 if (err < 0)
f2459fe7 220 rtnl_set_sk_err(&init_net, RTNLGRP_LINK, err);
064af421
BP
221}
222
58c342f6
BP
223static void release_dp(struct kobject *kobj)
224{
225 struct datapath *dp = container_of(kobj, struct datapath, ifobj);
226 kfree(dp);
227}
228
35f7605b 229static struct kobj_type dp_ktype = {
58c342f6
BP
230 .release = release_dp
231};
232
46c6a11d
JG
233static void destroy_dp_rcu(struct rcu_head *rcu)
234{
235 struct datapath *dp = container_of(rcu, struct datapath, rcu);
46c6a11d 236
3544358a 237 flow_tbl_destroy(dp->table);
46c6a11d
JG
238 free_percpu(dp->stats_percpu);
239 kobject_put(&dp->ifobj);
240}
241
ed099e92 242/* Called with RTNL lock and genl_lock. */
c19e6535 243static struct vport *new_vport(const struct vport_parms *parms)
064af421 244{
f2459fe7 245 struct vport *vport;
f2459fe7 246
c19e6535
BP
247 vport = vport_add(parms);
248 if (!IS_ERR(vport)) {
249 struct datapath *dp = parms->dp;
064af421 250
c19e6535 251 rcu_assign_pointer(dp->ports[parms->port_no], vport);
ed099e92 252 list_add(&vport->node, &dp->port_list);
064af421 253
c19e6535
BP
254 dp_ifinfo_notify(RTM_NEWLINK, vport);
255 }
064af421 256
c19e6535 257 return vport;
064af421
BP
258}
259
ed099e92 260/* Called with RTNL lock. */
3544358a 261void dp_detach_port(struct vport *p)
064af421
BP
262{
263 ASSERT_RTNL();
264
df2c07f4 265 if (p->port_no != OVSP_LOCAL)
0515ceb3 266 dp_sysfs_del_if(p);
064af421
BP
267 dp_ifinfo_notify(RTM_DELLINK, p);
268
064af421 269 /* First drop references to device. */
ed099e92 270 list_del(&p->node);
064af421 271 rcu_assign_pointer(p->dp->ports[p->port_no], NULL);
f2459fe7 272
7237e4f4 273 /* Then destroy it. */
3544358a 274 vport_del(p);
064af421
BP
275}
276
8819fac7 277/* Must be called with rcu_read_lock. */
e779d8d9 278void dp_process_received_packet(struct vport *p, struct sk_buff *skb)
064af421
BP
279{
280 struct datapath *dp = p->dp;
3544358a 281 struct sw_flow *flow;
064af421 282 struct dp_stats_percpu *stats;
e9141eec 283 u64 *stats_counter;
4c1ad233 284 int error;
064af421 285
e9141eec 286 stats = per_cpu_ptr(dp->stats_percpu, smp_processor_id());
e779d8d9 287 OVS_CB(skb)->vport = p;
a063b0df 288
3976f6d5 289 if (!OVS_CB(skb)->flow) {
36956a7d 290 struct sw_flow_key key;
76abe283 291 int key_len;
b7a31ec1 292 bool is_frag;
4c1ad233 293
3976f6d5 294 /* Extract flow from 'skb' into 'key'. */
76abe283 295 error = flow_extract(skb, p->port_no, &key, &key_len, &is_frag);
3976f6d5
JG
296 if (unlikely(error)) {
297 kfree_skb(skb);
298 return;
299 }
064af421 300
b7a31ec1 301 if (is_frag && dp->drop_frags) {
5b95ab0e 302 consume_skb(skb);
e9141eec 303 stats_counter = &stats->n_frags;
3976f6d5
JG
304 goto out;
305 }
306
307 /* Look up flow. */
3544358a
PS
308 flow = flow_tbl_lookup(rcu_dereference(dp->table), &key, key_len);
309 if (unlikely(!flow)) {
856081f6
BP
310 struct dp_upcall_info upcall;
311
df2c07f4 312 upcall.cmd = OVS_PACKET_CMD_MISS;
856081f6 313 upcall.key = &key;
98403001
BP
314 upcall.userdata = NULL;
315 upcall.pid = p->upcall_pid;
856081f6 316 dp_upcall(dp, skb, &upcall);
36ce148c 317 kfree_skb(skb);
e9141eec 318 stats_counter = &stats->n_missed;
3976f6d5
JG
319 goto out;
320 }
321
3544358a 322 OVS_CB(skb)->flow = flow;
55574bb0
BP
323 }
324
e9141eec 325 stats_counter = &stats->n_hit;
3976f6d5 326 flow_used(OVS_CB(skb)->flow, skb);
a4af2475 327 execute_actions(dp, skb);
55574bb0 328
8819fac7 329out:
55574bb0 330 /* Update datapath statistics. */
38c6ecbc
JG
331
332 write_seqcount_begin(&stats->seqlock);
e9141eec 333 (*stats_counter)++;
38c6ecbc 334 write_seqcount_end(&stats->seqlock);
064af421
BP
335}
336
856081f6
BP
337static void copy_and_csum_skb(struct sk_buff *skb, void *to)
338{
339 u16 csum_start, csum_offset;
340 __wsum csum;
341
342 get_skb_csum_pointers(skb, &csum_start, &csum_offset);
343 csum_start -= skb_headroom(skb);
856081f6
BP
344
345 skb_copy_bits(skb, 0, to, csum_start);
346
347 csum = skb_copy_and_csum_bits(skb, csum_start, to + csum_start,
348 skb->len - csum_start, 0);
349 *(__sum16 *)(to + csum_start + csum_offset) = csum_fold(csum);
350}
351
aa5a8fdc
JG
352static struct genl_family dp_packet_genl_family = {
353 .id = GENL_ID_GENERATE,
df2c07f4
JP
354 .hdrsize = sizeof(struct ovs_header),
355 .name = OVS_PACKET_FAMILY,
aa5a8fdc 356 .version = 1,
df2c07f4 357 .maxattr = OVS_PACKET_ATTR_MAX
aa5a8fdc
JG
358};
359
36ce148c
PS
360int dp_upcall(struct datapath *dp, struct sk_buff *skb,
361 const struct dp_upcall_info *upcall_info)
aa5a8fdc 362{
36ce148c 363 struct sk_buff *segs = NULL;
aa5a8fdc
JG
364 struct dp_stats_percpu *stats;
365 int err;
366
98403001 367 if (upcall_info->pid == 0) {
b063d9f0 368 err = -ENOTCONN;
b063d9f0
JG
369 goto err;
370 }
371
c3729ee4 372 forward_ip_summed(skb, true);
aa5a8fdc
JG
373
374 /* Break apart GSO packets into their component pieces. Otherwise
375 * userspace may try to stuff a 64kB packet into a 1500-byte MTU. */
376 if (skb_is_gso(skb)) {
36ce148c 377 segs = skb_gso_segment(skb, NETIF_F_SG | NETIF_F_HW_CSUM);
aa5a8fdc 378
36ce148c
PS
379 if (IS_ERR(segs)) {
380 err = PTR_ERR(segs);
aa5a8fdc
JG
381 goto err;
382 }
36ce148c 383 skb = segs;
aa5a8fdc
JG
384 }
385
98403001 386 err = queue_userspace_packets(dp, skb, upcall_info);
36ce148c
PS
387 if (segs) {
388 struct sk_buff *next;
389 /* Free GSO-segments */
390 do {
391 next = segs->next;
392 kfree_skb(segs);
393 } while ((segs = next) != NULL);
394 }
395
d76195db
JG
396 if (err)
397 goto err;
398
399 return 0;
aa5a8fdc 400
aa5a8fdc 401err:
aa5a8fdc
JG
402 stats = per_cpu_ptr(dp->stats_percpu, smp_processor_id());
403
404 write_seqcount_begin(&stats->seqlock);
405 stats->n_lost++;
406 write_seqcount_end(&stats->seqlock);
407
aa5a8fdc 408 return err;
982b8810
BP
409}
410
411/* Send each packet in the 'skb' list to userspace for 'dp' as directed by
412 * 'upcall_info'. There will be only one packet unless we broke up a GSO
413 * packet.
414 */
98403001 415static int queue_userspace_packets(struct datapath *dp, struct sk_buff *skb,
b063d9f0 416 const struct dp_upcall_info *upcall_info)
cb5087ca 417{
99769a40 418 int dp_ifindex;
cb5087ca 419
99769a40 420 dp_ifindex = get_dpifindex(dp);
36ce148c
PS
421 if (!dp_ifindex)
422 return -ENODEV;
99769a40 423
cb5087ca 424 do {
df2c07f4 425 struct ovs_header *upcall;
856081f6
BP
426 struct sk_buff *user_skb; /* to be queued to userspace */
427 struct nlattr *nla;
428 unsigned int len;
36ce148c 429 int err;
cb5087ca 430
303708cc
JG
431 err = vlan_deaccel_tag(skb);
432 if (unlikely(err))
36ce148c 433 return err;
6ce39213 434
36ce148c
PS
435 if (nla_attr_size(skb->len) > USHRT_MAX)
436 return -EFBIG;
51313015 437
df2c07f4 438 len = sizeof(struct ovs_header);
856081f6
BP
439 len += nla_total_size(skb->len);
440 len += nla_total_size(FLOW_BUFSIZE);
6ff686f2 441 if (upcall_info->cmd == OVS_PACKET_CMD_ACTION)
856081f6 442 len += nla_total_size(8);
856081f6 443
982b8810 444 user_skb = genlmsg_new(len, GFP_ATOMIC);
36ce148c
PS
445 if (!user_skb)
446 return -ENOMEM;
cb5087ca 447
36ce148c
PS
448 upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
449 0, upcall_info->cmd);
99769a40 450 upcall->dp_ifindex = dp_ifindex;
856081f6 451
df2c07f4 452 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
856081f6
BP
453 flow_to_nlattrs(upcall_info->key, user_skb);
454 nla_nest_end(user_skb, nla);
cb5087ca 455
98403001 456 if (upcall_info->userdata)
6ff686f2 457 nla_put_u64(user_skb, OVS_PACKET_ATTR_USERDATA,
98403001 458 nla_get_u64(upcall_info->userdata));
856081f6 459
df2c07f4 460 nla = __nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, skb->len);
856081f6
BP
461 if (skb->ip_summed == CHECKSUM_PARTIAL)
462 copy_and_csum_skb(skb, nla_data(nla));
463 else
464 skb_copy_bits(skb, 0, nla_data(nla), skb->len);
465
98403001 466 err = genlmsg_unicast(&init_net, user_skb, upcall_info->pid);
982b8810 467 if (err)
36ce148c 468 return err;
856081f6 469
36ce148c 470 } while ((skb = skb->next));
cb5087ca 471
36ce148c 472 return 0;
cb5087ca
BP
473}
474
ed099e92 475/* Called with genl_mutex. */
254f2dc8 476static int flush_flows(int dp_ifindex)
064af421 477{
3544358a
PS
478 struct flow_table *old_table;
479 struct flow_table *new_table;
9c52546b 480 struct datapath *dp;
9c52546b 481
254f2dc8 482 dp = get_dp(dp_ifindex);
9c52546b 483 if (!dp)
ed099e92 484 return -ENODEV;
8d5ebd83 485
9c52546b 486 old_table = get_table_protected(dp);
3544358a 487 new_table = flow_tbl_alloc(TBL_MIN_BUCKETS);
8d5ebd83 488 if (!new_table)
ed099e92 489 return -ENOMEM;
8d5ebd83
JG
490
491 rcu_assign_pointer(dp->table, new_table);
492
3544358a 493 flow_tbl_deferred_destroy(old_table);
ed099e92 494 return 0;
064af421
BP
495}
496
4edb9ae9
PS
497static int validate_actions(const struct nlattr *attr,
498 const struct sw_flow_key *key, int depth);
6ff686f2 499
4edb9ae9
PS
500static int validate_sample(const struct nlattr *attr,
501 const struct sw_flow_key *key, int depth)
6ff686f2
PS
502{
503 static const struct nla_policy sample_policy[OVS_SAMPLE_ATTR_MAX + 1] =
504 {
505 [OVS_SAMPLE_ATTR_PROBABILITY] = {.type = NLA_U32 },
506 [OVS_SAMPLE_ATTR_ACTIONS] = {.type = NLA_UNSPEC },
507 };
508 struct nlattr *a[OVS_SAMPLE_ATTR_MAX + 1];
509 int error;
510
511 error = nla_parse_nested(a, OVS_SAMPLE_ATTR_MAX, attr, sample_policy);
512 if (error)
513 return error;
514
515 if (!a[OVS_SAMPLE_ATTR_PROBABILITY])
516 return -EINVAL;
517 if (!a[OVS_SAMPLE_ATTR_ACTIONS])
518 return -EINVAL;
519
4edb9ae9
PS
520 return validate_actions(a[OVS_SAMPLE_ATTR_ACTIONS], key, (depth + 1));
521}
522
523static int validate_action_key(const struct nlattr *a,
524 const struct sw_flow_key *flow_key)
525{
526 int act_type = nla_type(a);
527 const struct nlattr *ovs_key = nla_data(a);
528 int key_type = nla_type(ovs_key);
529
530 /* There can be only one key in a action */
531 if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
532 return -EINVAL;
533
534 if (key_type > OVS_KEY_ATTR_MAX ||
535 nla_len(ovs_key) != ovs_key_lens[key_type])
536 return -EINVAL;
537
538#define ACTION(act, key) ((act << 8) | key)
539
540 switch(ACTION(act_type, key_type)) {
541 const struct ovs_key_ipv4 *ipv4_key;
542 const struct ovs_key_8021q *q_key;
543
544 case ACTION(OVS_ACTION_ATTR_SET, OVS_KEY_ATTR_TUN_ID):
545 case ACTION(OVS_ACTION_ATTR_SET, OVS_KEY_ATTR_ETHERNET):
546 break;
547
548 case ACTION(OVS_ACTION_ATTR_PUSH, OVS_KEY_ATTR_8021Q):
549 q_key = nla_data(ovs_key);
550 if (q_key->q_tpid != htons(ETH_P_8021Q))
551 return -EINVAL;
552
553 if (q_key->q_tci & htons(VLAN_TAG_PRESENT))
554 return -EINVAL;
555 break;
556
557 case ACTION(OVS_ACTION_ATTR_SET, OVS_KEY_ATTR_IPV4):
558 if (flow_key->eth.type != htons(ETH_P_IP))
559 return -EINVAL;
560
561 if (!flow_key->ipv4.addr.src || !flow_key->ipv4.addr.dst)
562 return -EINVAL;
563
564 ipv4_key = nla_data(ovs_key);
565 if (ipv4_key->ipv4_proto != flow_key->ip.proto)
566 return -EINVAL;
567
568 if (ipv4_key->ipv4_tos & INET_ECN_MASK)
569 return -EINVAL;
570 break;
571
572 case ACTION(OVS_ACTION_ATTR_SET, OVS_KEY_ATTR_TCP):
573 if (flow_key->ip.proto != IPPROTO_TCP)
574 return -EINVAL;
575
576 if (!flow_key->ipv4.tp.src || !flow_key->ipv4.tp.dst)
577 return -EINVAL;
578
579 break;
580
581 case ACTION(OVS_ACTION_ATTR_SET, OVS_KEY_ATTR_UDP):
582 if (flow_key->ip.proto != IPPROTO_UDP)
583 return -EINVAL;
584
585 if (!flow_key->ipv4.tp.src || !flow_key->ipv4.tp.dst)
586 return -EINVAL;
587 break;
588
589 default:
590 return -EINVAL;
591 }
592#undef ACTION
593 return 0;
6ff686f2
PS
594}
595
98403001
BP
596static int validate_userspace(const struct nlattr *attr)
597{
598 static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] =
599 {
600 [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
601 [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_U64 },
602 };
603 struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
604 int error;
605
606 error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX, attr, userspace_policy);
607 if (error)
608 return error;
609
610 if (!a[OVS_USERSPACE_ATTR_PID] || !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
611 return -EINVAL;
612
613 return 0;
614}
615
4edb9ae9
PS
616static int validate_actions(const struct nlattr *attr,
617 const struct sw_flow_key *key, int depth)
064af421 618{
23cad98c 619 const struct nlattr *a;
6ff686f2
PS
620 int rem, err;
621
622 if (depth >= SAMPLE_ACTION_DEPTH)
623 return -EOVERFLOW;
23cad98c 624
37a1300c 625 nla_for_each_nested(a, attr, rem) {
98403001 626 /* Expected argument lengths, (u32)-1 for variable length. */
df2c07f4
JP
627 static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
628 [OVS_ACTION_ATTR_OUTPUT] = 4,
98403001 629 [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
4edb9ae9
PS
630 [OVS_ACTION_ATTR_PUSH] = (u32)-1,
631 [OVS_ACTION_ATTR_POP] = 2,
632 [OVS_ACTION_ATTR_SET] = (u32)-1,
df2c07f4
JP
633 [OVS_ACTION_ATTR_SET_PRIORITY] = 4,
634 [OVS_ACTION_ATTR_POP_PRIORITY] = 0,
98403001 635 [OVS_ACTION_ATTR_SAMPLE] = (u32)-1
23cad98c
BP
636 };
637 int type = nla_type(a);
638
6ff686f2 639 if (type > OVS_ACTION_ATTR_MAX ||
98403001
BP
640 (action_lens[type] != nla_len(a) &&
641 action_lens[type] != (u32)-1))
23cad98c
BP
642 return -EINVAL;
643
644 switch (type) {
df2c07f4 645 case OVS_ACTION_ATTR_UNSPEC:
cdee00fd 646 return -EINVAL;
064af421 647
df2c07f4
JP
648 case OVS_ACTION_ATTR_SET_PRIORITY:
649 case OVS_ACTION_ATTR_POP_PRIORITY:
23cad98c
BP
650 /* No validation needed. */
651 break;
652
98403001
BP
653 case OVS_ACTION_ATTR_USERSPACE:
654 err = validate_userspace(a);
655 if (err)
656 return err;
657 break;
658
df2c07f4 659 case OVS_ACTION_ATTR_OUTPUT:
23cad98c
BP
660 if (nla_get_u32(a) >= DP_MAX_PORTS)
661 return -EINVAL;
3b1fc5f3 662 break;
cdee00fd 663
4edb9ae9
PS
664
665 case OVS_ACTION_ATTR_POP:
666 if (nla_get_u16(a) != OVS_KEY_ATTR_8021Q)
064af421 667 return -EINVAL;
23cad98c 668 break;
064af421 669
4edb9ae9
PS
670 case OVS_ACTION_ATTR_SET:
671 case OVS_ACTION_ATTR_PUSH:
672 err = validate_action_key(a, key);
673 if (err)
674 return err;
23cad98c 675 break;
064af421 676
6ff686f2 677 case OVS_ACTION_ATTR_SAMPLE:
4edb9ae9 678 err = validate_sample(a, key, depth);
6ff686f2
PS
679 if (err)
680 return err;
681 break;
682
23cad98c 683 default:
4edb9ae9 684 return -EINVAL;
23cad98c
BP
685 }
686 }
3c5f6de3 687
23cad98c
BP
688 if (rem > 0)
689 return -EINVAL;
064af421 690
23cad98c 691 return 0;
064af421 692}
4edb9ae9 693
064af421
BP
694static void clear_stats(struct sw_flow *flow)
695{
6bfafa55 696 flow->used = 0;
064af421 697 flow->tcp_flags = 0;
064af421
BP
698 flow->packet_count = 0;
699 flow->byte_count = 0;
700}
701
df2c07f4 702static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
064af421 703{
df2c07f4 704 struct ovs_header *ovs_header = info->userhdr;
982b8810 705 struct nlattr **a = info->attrs;
e0e57990 706 struct sw_flow_actions *acts;
982b8810 707 struct sk_buff *packet;
e0e57990 708 struct sw_flow *flow;
f7cd0081 709 struct datapath *dp;
d6569377
BP
710 struct ethhdr *eth;
711 bool is_frag;
3f19d399 712 int len;
d6569377 713 int err;
76abe283 714 int key_len;
064af421 715
f7cd0081 716 err = -EINVAL;
df2c07f4
JP
717 if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
718 !a[OVS_PACKET_ATTR_ACTIONS] ||
719 nla_len(a[OVS_PACKET_ATTR_PACKET]) < ETH_HLEN)
e5cad958 720 goto err;
064af421 721
df2c07f4 722 len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
3f19d399 723 packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
f7cd0081
BP
724 err = -ENOMEM;
725 if (!packet)
e5cad958 726 goto err;
3f19d399
BP
727 skb_reserve(packet, NET_IP_ALIGN);
728
df2c07f4 729 memcpy(__skb_put(packet, len), nla_data(a[OVS_PACKET_ATTR_PACKET]), len);
8d5ebd83 730
f7cd0081
BP
731 skb_reset_mac_header(packet);
732 eth = eth_hdr(packet);
064af421 733
d6569377
BP
734 /* Normally, setting the skb 'protocol' field would be handled by a
735 * call to eth_type_trans(), but it assumes there's a sending
736 * device, which we may not have. */
737 if (ntohs(eth->h_proto) >= 1536)
f7cd0081 738 packet->protocol = eth->h_proto;
d6569377 739 else
f7cd0081 740 packet->protocol = htons(ETH_P_802_2);
d3c54451 741
e0e57990
BP
742 /* Build an sw_flow for sending this packet. */
743 flow = flow_alloc();
744 err = PTR_ERR(flow);
745 if (IS_ERR(flow))
e5cad958 746 goto err_kfree_skb;
064af421 747
76abe283 748 err = flow_extract(packet, -1, &flow->key, &key_len, &is_frag);
e0e57990
BP
749 if (err)
750 goto err_flow_put;
e0e57990 751
80e5eed9
BP
752 err = flow_metadata_from_nlattrs(&flow->key.eth.in_port,
753 &flow->key.eth.tun_id,
df2c07f4 754 a[OVS_PACKET_ATTR_KEY]);
80e5eed9
BP
755 if (err)
756 goto err_flow_put;
757
4edb9ae9
PS
758 err = validate_actions(a[OVS_PACKET_ATTR_ACTIONS], &flow->key, 0);
759 if (err)
760 goto err_flow_put;
761
3544358a 762 flow->hash = flow_hash(&flow->key, key_len);
0fe255df 763
df2c07f4 764 acts = flow_actions_alloc(a[OVS_PACKET_ATTR_ACTIONS]);
e0e57990
BP
765 err = PTR_ERR(acts);
766 if (IS_ERR(acts))
767 goto err_flow_put;
768 rcu_assign_pointer(flow->sf_acts, acts);
769
770 OVS_CB(packet)->flow = flow;
771
d6569377 772 rcu_read_lock();
df2c07f4 773 dp = get_dp(ovs_header->dp_ifindex);
f7cd0081 774 err = -ENODEV;
e5cad958
BP
775 if (!dp)
776 goto err_unlock;
cc4015df
JG
777
778 if (flow->key.eth.in_port < DP_MAX_PORTS)
779 OVS_CB(packet)->vport = get_vport_protected(dp,
780 flow->key.eth.in_port);
781
e9141eec 782 local_bh_disable();
a4af2475 783 err = execute_actions(dp, packet);
e9141eec 784 local_bh_enable();
d6569377 785 rcu_read_unlock();
e0e57990
BP
786
787 flow_put(flow);
e5cad958 788 return err;
064af421 789
e5cad958
BP
790err_unlock:
791 rcu_read_unlock();
e0e57990
BP
792err_flow_put:
793 flow_put(flow);
e5cad958
BP
794err_kfree_skb:
795 kfree_skb(packet);
796err:
d6569377 797 return err;
064af421
BP
798}
799
df2c07f4
JP
800static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
801 [OVS_PACKET_ATTR_PACKET] = { .type = NLA_UNSPEC },
802 [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
803 [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
982b8810
BP
804};
805
806static struct genl_ops dp_packet_genl_ops[] = {
df2c07f4 807 { .cmd = OVS_PACKET_CMD_EXECUTE,
982b8810
BP
808 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
809 .policy = packet_policy,
df2c07f4 810 .doit = ovs_packet_cmd_execute
982b8810
BP
811 }
812};
813
df2c07f4 814static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats)
064af421 815{
d6569377 816 int i;
3544358a 817 struct flow_table *table = get_table_protected(dp);
f180c2e2 818
3544358a 819 stats->n_flows = flow_tbl_count(table);
064af421 820
d6569377
BP
821 stats->n_frags = stats->n_hit = stats->n_missed = stats->n_lost = 0;
822 for_each_possible_cpu(i) {
823 const struct dp_stats_percpu *percpu_stats;
824 struct dp_stats_percpu local_stats;
825 unsigned seqcount;
44e05eca 826
d6569377 827 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
064af421 828
d6569377
BP
829 do {
830 seqcount = read_seqcount_begin(&percpu_stats->seqlock);
831 local_stats = *percpu_stats;
832 } while (read_seqcount_retry(&percpu_stats->seqlock, seqcount));
064af421 833
d6569377
BP
834 stats->n_frags += local_stats.n_frags;
835 stats->n_hit += local_stats.n_hit;
836 stats->n_missed += local_stats.n_missed;
837 stats->n_lost += local_stats.n_lost;
838 }
839}
064af421 840
df2c07f4
JP
841static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
842 [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
843 [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
844 [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
d6569377 845};
36956a7d 846
37a1300c
BP
847static struct genl_family dp_flow_genl_family = {
848 .id = GENL_ID_GENERATE,
df2c07f4
JP
849 .hdrsize = sizeof(struct ovs_header),
850 .name = OVS_FLOW_FAMILY,
37a1300c 851 .version = 1,
df2c07f4 852 .maxattr = OVS_FLOW_ATTR_MAX
37a1300c 853};
ed099e92 854
37a1300c 855static struct genl_multicast_group dp_flow_multicast_group = {
df2c07f4 856 .name = OVS_FLOW_MCGROUP
37a1300c
BP
857};
858
859/* Called with genl_lock. */
df2c07f4 860static int ovs_flow_cmd_fill_info(struct sw_flow *flow, struct datapath *dp,
37a1300c 861 struct sk_buff *skb, u32 pid, u32 seq, u32 flags, u8 cmd)
d6569377 862{
37a1300c 863 const int skb_orig_len = skb->len;
d6569377 864 const struct sw_flow_actions *sf_acts;
df2c07f4
JP
865 struct ovs_flow_stats stats;
866 struct ovs_header *ovs_header;
d6569377
BP
867 struct nlattr *nla;
868 unsigned long used;
869 u8 tcp_flags;
870 int err;
064af421 871
d6569377 872 sf_acts = rcu_dereference_protected(flow->sf_acts,
ed099e92 873 lockdep_genl_is_held());
064af421 874
df2c07f4
JP
875 ovs_header = genlmsg_put(skb, pid, seq, &dp_flow_genl_family, flags, cmd);
876 if (!ovs_header)
37a1300c 877 return -EMSGSIZE;
d6569377 878
99769a40 879 ovs_header->dp_ifindex = get_dpifindex(dp);
d6569377 880
df2c07f4 881 nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
d6569377
BP
882 if (!nla)
883 goto nla_put_failure;
884 err = flow_to_nlattrs(&flow->key, skb);
885 if (err)
37a1300c 886 goto error;
d6569377
BP
887 nla_nest_end(skb, nla);
888
889 spin_lock_bh(&flow->lock);
890 used = flow->used;
891 stats.n_packets = flow->packet_count;
892 stats.n_bytes = flow->byte_count;
893 tcp_flags = flow->tcp_flags;
894 spin_unlock_bh(&flow->lock);
895
896 if (used)
df2c07f4 897 NLA_PUT_U64(skb, OVS_FLOW_ATTR_USED, flow_used_time(used));
d6569377
BP
898
899 if (stats.n_packets)
df2c07f4 900 NLA_PUT(skb, OVS_FLOW_ATTR_STATS, sizeof(struct ovs_flow_stats), &stats);
d6569377
BP
901
902 if (tcp_flags)
df2c07f4 903 NLA_PUT_U8(skb, OVS_FLOW_ATTR_TCP_FLAGS, tcp_flags);
d6569377 904
df2c07f4 905 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
30053024
BP
906 * this is the first flow to be dumped into 'skb'. This is unusual for
907 * Netlink but individual action lists can be longer than
908 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
909 * The userspace caller can always fetch the actions separately if it
910 * really wants them. (Most userspace callers in fact don't care.)
911 *
912 * This can only fail for dump operations because the skb is always
913 * properly sized for single flows.
914 */
df2c07f4 915 err = nla_put(skb, OVS_FLOW_ATTR_ACTIONS, sf_acts->actions_len,
30053024
BP
916 sf_acts->actions);
917 if (err < 0 && skb_orig_len)
918 goto error;
37a1300c 919
df2c07f4 920 return genlmsg_end(skb, ovs_header);
d6569377
BP
921
922nla_put_failure:
923 err = -EMSGSIZE;
37a1300c 924error:
df2c07f4 925 genlmsg_cancel(skb, ovs_header);
d6569377 926 return err;
44e05eca
BP
927}
928
df2c07f4 929static struct sk_buff *ovs_flow_cmd_alloc_info(struct sw_flow *flow)
44e05eca 930{
37a1300c
BP
931 const struct sw_flow_actions *sf_acts;
932 int len;
d6569377 933
37a1300c
BP
934 sf_acts = rcu_dereference_protected(flow->sf_acts,
935 lockdep_genl_is_held());
d6569377 936
df2c07f4
JP
937 len = nla_total_size(FLOW_BUFSIZE); /* OVS_FLOW_ATTR_KEY */
938 len += nla_total_size(sf_acts->actions_len); /* OVS_FLOW_ATTR_ACTIONS */
939 len += nla_total_size(sizeof(struct ovs_flow_stats)); /* OVS_FLOW_ATTR_STATS */
940 len += nla_total_size(1); /* OVS_FLOW_ATTR_TCP_FLAGS */
941 len += nla_total_size(8); /* OVS_FLOW_ATTR_USED */
942 return genlmsg_new(NLMSG_ALIGN(sizeof(struct ovs_header)) + len, GFP_KERNEL);
37a1300c 943}
8d5ebd83 944
df2c07f4 945static struct sk_buff *ovs_flow_cmd_build_info(struct sw_flow *flow, struct datapath *dp,
37a1300c
BP
946 u32 pid, u32 seq, u8 cmd)
947{
948 struct sk_buff *skb;
949 int retval;
d6569377 950
df2c07f4 951 skb = ovs_flow_cmd_alloc_info(flow);
37a1300c
BP
952 if (!skb)
953 return ERR_PTR(-ENOMEM);
d6569377 954
df2c07f4 955 retval = ovs_flow_cmd_fill_info(flow, dp, skb, pid, seq, 0, cmd);
37a1300c 956 BUG_ON(retval < 0);
d6569377 957 return skb;
064af421
BP
958}
959
df2c07f4 960static int ovs_flow_cmd_new_or_set(struct sk_buff *skb, struct genl_info *info)
064af421 961{
37a1300c 962 struct nlattr **a = info->attrs;
df2c07f4 963 struct ovs_header *ovs_header = info->userhdr;
37a1300c 964 struct sw_flow_key key;
d6569377 965 struct sw_flow *flow;
37a1300c 966 struct sk_buff *reply;
9c52546b 967 struct datapath *dp;
3544358a 968 struct flow_table *table;
bc4a05c6 969 int error;
76abe283 970 int key_len;
064af421 971
37a1300c
BP
972 /* Extract key. */
973 error = -EINVAL;
df2c07f4 974 if (!a[OVS_FLOW_ATTR_KEY])
37a1300c 975 goto error;
df2c07f4 976 error = flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
37a1300c
BP
977 if (error)
978 goto error;
064af421 979
37a1300c 980 /* Validate actions. */
df2c07f4 981 if (a[OVS_FLOW_ATTR_ACTIONS]) {
4edb9ae9 982 error = validate_actions(a[OVS_FLOW_ATTR_ACTIONS], &key, 0);
37a1300c
BP
983 if (error)
984 goto error;
df2c07f4 985 } else if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW) {
37a1300c
BP
986 error = -EINVAL;
987 goto error;
988 }
989
df2c07f4 990 dp = get_dp(ovs_header->dp_ifindex);
d6569377 991 error = -ENODEV;
9c52546b 992 if (!dp)
37a1300c 993 goto error;
704a1e09 994
d6569377 995 table = get_table_protected(dp);
3544358a
PS
996 flow = flow_tbl_lookup(table, &key, key_len);
997 if (!flow) {
d6569377
BP
998 struct sw_flow_actions *acts;
999
1000 /* Bail out if we're not allowed to create a new flow. */
1001 error = -ENOENT;
df2c07f4 1002 if (info->genlhdr->cmd == OVS_FLOW_CMD_SET)
37a1300c 1003 goto error;
d6569377
BP
1004
1005 /* Expand table, if necessary, to make room. */
3544358a
PS
1006 if (flow_tbl_need_to_expand(table)) {
1007 struct flow_table *new_table;
1008
1009 new_table = flow_tbl_expand(table);
1010 if (!IS_ERR(new_table)) {
1011 rcu_assign_pointer(dp->table, new_table);
1012 flow_tbl_deferred_destroy(table);
1013 table = get_table_protected(dp);
1014 }
d6569377
BP
1015 }
1016
1017 /* Allocate flow. */
1018 flow = flow_alloc();
1019 if (IS_ERR(flow)) {
1020 error = PTR_ERR(flow);
37a1300c 1021 goto error;
d6569377 1022 }
37a1300c 1023 flow->key = key;
d6569377
BP
1024 clear_stats(flow);
1025
1026 /* Obtain actions. */
df2c07f4 1027 acts = flow_actions_alloc(a[OVS_FLOW_ATTR_ACTIONS]);
d6569377
BP
1028 error = PTR_ERR(acts);
1029 if (IS_ERR(acts))
1030 goto error_free_flow;
1031 rcu_assign_pointer(flow->sf_acts, acts);
1032
d6569377 1033 /* Put flow in bucket. */
3544358a
PS
1034 flow->hash = flow_hash(&key, key_len);
1035 flow_tbl_insert(table, flow);
37a1300c 1036
df2c07f4
JP
1037 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_pid,
1038 info->snd_seq, OVS_FLOW_CMD_NEW);
d6569377
BP
1039 } else {
1040 /* We found a matching flow. */
1041 struct sw_flow_actions *old_acts;
1042
1043 /* Bail out if we're not allowed to modify an existing flow.
1044 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
1045 * because Generic Netlink treats the latter as a dump
1046 * request. We also accept NLM_F_EXCL in case that bug ever
1047 * gets fixed.
1048 */
1049 error = -EEXIST;
df2c07f4 1050 if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW &&
37a1300c
BP
1051 info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL))
1052 goto error;
d6569377
BP
1053
1054 /* Update actions. */
d6569377 1055 old_acts = rcu_dereference_protected(flow->sf_acts,
ed099e92 1056 lockdep_genl_is_held());
df2c07f4
JP
1057 if (a[OVS_FLOW_ATTR_ACTIONS] &&
1058 (old_acts->actions_len != nla_len(a[OVS_FLOW_ATTR_ACTIONS]) ||
1059 memcmp(old_acts->actions, nla_data(a[OVS_FLOW_ATTR_ACTIONS]),
37a1300c 1060 old_acts->actions_len))) {
d6569377
BP
1061 struct sw_flow_actions *new_acts;
1062
df2c07f4 1063 new_acts = flow_actions_alloc(a[OVS_FLOW_ATTR_ACTIONS]);
d6569377
BP
1064 error = PTR_ERR(new_acts);
1065 if (IS_ERR(new_acts))
37a1300c 1066 goto error;
d6569377
BP
1067
1068 rcu_assign_pointer(flow->sf_acts, new_acts);
1069 flow_deferred_free_acts(old_acts);
1070 }
1071
df2c07f4
JP
1072 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_pid,
1073 info->snd_seq, OVS_FLOW_CMD_NEW);
d6569377
BP
1074
1075 /* Clear stats. */
df2c07f4 1076 if (a[OVS_FLOW_ATTR_CLEAR]) {
d6569377
BP
1077 spin_lock_bh(&flow->lock);
1078 clear_stats(flow);
1079 spin_unlock_bh(&flow->lock);
1080 }
9c52546b 1081 }
37a1300c
BP
1082
1083 if (!IS_ERR(reply))
1084 genl_notify(reply, genl_info_net(info), info->snd_pid,
1085 dp_flow_multicast_group.id, info->nlhdr, GFP_KERNEL);
1086 else
1087 netlink_set_err(INIT_NET_GENL_SOCK, 0,
1088 dp_flow_multicast_group.id, PTR_ERR(reply));
d6569377 1089 return 0;
704a1e09 1090
d6569377
BP
1091error_free_flow:
1092 flow_put(flow);
37a1300c 1093error:
9c52546b 1094 return error;
704a1e09
BP
1095}
1096
df2c07f4 1097static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
704a1e09 1098{
37a1300c 1099 struct nlattr **a = info->attrs;
df2c07f4 1100 struct ovs_header *ovs_header = info->userhdr;
37a1300c 1101 struct sw_flow_key key;
37a1300c 1102 struct sk_buff *reply;
704a1e09 1103 struct sw_flow *flow;
9c52546b 1104 struct datapath *dp;
3544358a 1105 struct flow_table *table;
9c52546b 1106 int err;
76abe283 1107 int key_len;
704a1e09 1108
df2c07f4 1109 if (!a[OVS_FLOW_ATTR_KEY])
37a1300c 1110 return -EINVAL;
df2c07f4 1111 err = flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
37a1300c
BP
1112 if (err)
1113 return err;
704a1e09 1114
df2c07f4 1115 dp = get_dp(ovs_header->dp_ifindex);
9c52546b 1116 if (!dp)
ed099e92 1117 return -ENODEV;
704a1e09 1118
9c52546b 1119 table = get_table_protected(dp);
3544358a
PS
1120 flow = flow_tbl_lookup(table, &key, key_len);
1121 if (!flow)
ed099e92 1122 return -ENOENT;
d6569377 1123
df2c07f4 1124 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_pid, info->snd_seq, OVS_FLOW_CMD_NEW);
37a1300c
BP
1125 if (IS_ERR(reply))
1126 return PTR_ERR(reply);
36956a7d 1127
37a1300c 1128 return genlmsg_reply(reply, info);
d6569377 1129}
9c52546b 1130
df2c07f4 1131static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
d6569377 1132{
37a1300c 1133 struct nlattr **a = info->attrs;
df2c07f4 1134 struct ovs_header *ovs_header = info->userhdr;
37a1300c 1135 struct sw_flow_key key;
37a1300c 1136 struct sk_buff *reply;
d6569377 1137 struct sw_flow *flow;
d6569377 1138 struct datapath *dp;
3544358a 1139 struct flow_table *table;
d6569377 1140 int err;
76abe283 1141 int key_len;
36956a7d 1142
df2c07f4
JP
1143 if (!a[OVS_FLOW_ATTR_KEY])
1144 return flush_flows(ovs_header->dp_ifindex);
1145 err = flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
37a1300c
BP
1146 if (err)
1147 return err;
d6569377 1148
df2c07f4 1149 dp = get_dp(ovs_header->dp_ifindex);
d6569377 1150 if (!dp)
37a1300c 1151 return -ENODEV;
d6569377 1152
37a1300c 1153 table = get_table_protected(dp);
3544358a
PS
1154 flow = flow_tbl_lookup(table, &key, key_len);
1155 if (!flow)
37a1300c 1156 return -ENOENT;
d6569377 1157
df2c07f4 1158 reply = ovs_flow_cmd_alloc_info(flow);
37a1300c
BP
1159 if (!reply)
1160 return -ENOMEM;
1161
3544358a 1162 flow_tbl_remove(table, flow);
37a1300c 1163
df2c07f4
JP
1164 err = ovs_flow_cmd_fill_info(flow, dp, reply, info->snd_pid,
1165 info->snd_seq, 0, OVS_FLOW_CMD_DEL);
37a1300c
BP
1166 BUG_ON(err < 0);
1167
1168 flow_deferred_free(flow);
1169
1170 genl_notify(reply, genl_info_net(info), info->snd_pid,
1171 dp_flow_multicast_group.id, info->nlhdr, GFP_KERNEL);
1172 return 0;
1173}
1174
df2c07f4 1175static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
37a1300c 1176{
df2c07f4 1177 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
37a1300c
BP
1178 struct datapath *dp;
1179
df2c07f4 1180 dp = get_dp(ovs_header->dp_ifindex);
37a1300c
BP
1181 if (!dp)
1182 return -ENODEV;
1183
1184 for (;;) {
37a1300c
BP
1185 struct sw_flow *flow;
1186 u32 bucket, obj;
1187
1188 bucket = cb->args[0];
1189 obj = cb->args[1];
3544358a
PS
1190 flow = flow_tbl_next(get_table_protected(dp), &bucket, &obj);
1191 if (!flow)
37a1300c
BP
1192 break;
1193
df2c07f4 1194 if (ovs_flow_cmd_fill_info(flow, dp, skb, NETLINK_CB(cb->skb).pid,
37a1300c 1195 cb->nlh->nlmsg_seq, NLM_F_MULTI,
df2c07f4 1196 OVS_FLOW_CMD_NEW) < 0)
37a1300c
BP
1197 break;
1198
1199 cb->args[0] = bucket;
1200 cb->args[1] = obj;
1201 }
1202 return skb->len;
704a1e09
BP
1203}
1204
37a1300c 1205static struct genl_ops dp_flow_genl_ops[] = {
df2c07f4 1206 { .cmd = OVS_FLOW_CMD_NEW,
37a1300c
BP
1207 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1208 .policy = flow_policy,
df2c07f4 1209 .doit = ovs_flow_cmd_new_or_set
37a1300c 1210 },
df2c07f4 1211 { .cmd = OVS_FLOW_CMD_DEL,
37a1300c
BP
1212 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1213 .policy = flow_policy,
df2c07f4 1214 .doit = ovs_flow_cmd_del
37a1300c 1215 },
df2c07f4 1216 { .cmd = OVS_FLOW_CMD_GET,
37a1300c
BP
1217 .flags = 0, /* OK for unprivileged users. */
1218 .policy = flow_policy,
df2c07f4
JP
1219 .doit = ovs_flow_cmd_get,
1220 .dumpit = ovs_flow_cmd_dump
37a1300c 1221 },
df2c07f4 1222 { .cmd = OVS_FLOW_CMD_SET,
37a1300c
BP
1223 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1224 .policy = flow_policy,
df2c07f4 1225 .doit = ovs_flow_cmd_new_or_set,
37a1300c
BP
1226 },
1227};
1228
df2c07f4 1229static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
aaff4b55 1230#ifdef HAVE_NLA_NUL_STRING
df2c07f4 1231 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
aaff4b55 1232#endif
b063d9f0 1233 [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
df2c07f4 1234 [OVS_DP_ATTR_IPV4_FRAGS] = { .type = NLA_U32 },
d6569377
BP
1235};
1236
aaff4b55
BP
1237static struct genl_family dp_datapath_genl_family = {
1238 .id = GENL_ID_GENERATE,
df2c07f4
JP
1239 .hdrsize = sizeof(struct ovs_header),
1240 .name = OVS_DATAPATH_FAMILY,
aaff4b55 1241 .version = 1,
df2c07f4 1242 .maxattr = OVS_DP_ATTR_MAX
aaff4b55
BP
1243};
1244
1245static struct genl_multicast_group dp_datapath_multicast_group = {
df2c07f4 1246 .name = OVS_DATAPATH_MCGROUP
aaff4b55
BP
1247};
1248
df2c07f4 1249static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
aaff4b55 1250 u32 pid, u32 seq, u32 flags, u8 cmd)
064af421 1251{
df2c07f4 1252 struct ovs_header *ovs_header;
d6569377 1253 struct nlattr *nla;
064af421
BP
1254 int err;
1255
df2c07f4 1256 ovs_header = genlmsg_put(skb, pid, seq, &dp_datapath_genl_family,
aaff4b55 1257 flags, cmd);
df2c07f4 1258 if (!ovs_header)
aaff4b55 1259 goto error;
064af421 1260
b063d9f0 1261 ovs_header->dp_ifindex = get_dpifindex(dp);
064af421 1262
d6569377 1263 rcu_read_lock();
df2c07f4 1264 err = nla_put_string(skb, OVS_DP_ATTR_NAME, dp_name(dp));
d6569377 1265 rcu_read_unlock();
064af421 1266 if (err)
d6569377 1267 goto nla_put_failure;
064af421 1268
df2c07f4 1269 nla = nla_reserve(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats));
d6569377
BP
1270 if (!nla)
1271 goto nla_put_failure;
1272 get_dp_stats(dp, nla_data(nla));
1273
df2c07f4
JP
1274 NLA_PUT_U32(skb, OVS_DP_ATTR_IPV4_FRAGS,
1275 dp->drop_frags ? OVS_DP_FRAG_DROP : OVS_DP_FRAG_ZERO);
d6569377 1276
df2c07f4 1277 return genlmsg_end(skb, ovs_header);
d6569377
BP
1278
1279nla_put_failure:
df2c07f4 1280 genlmsg_cancel(skb, ovs_header);
aaff4b55
BP
1281error:
1282 return -EMSGSIZE;
d6569377
BP
1283}
1284
df2c07f4 1285static struct sk_buff *ovs_dp_cmd_build_info(struct datapath *dp, u32 pid,
aaff4b55 1286 u32 seq, u8 cmd)
d6569377 1287{
d6569377 1288 struct sk_buff *skb;
aaff4b55 1289 int retval;
d6569377 1290
aaff4b55 1291 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
064af421 1292 if (!skb)
d6569377 1293 return ERR_PTR(-ENOMEM);
659586ef 1294
df2c07f4 1295 retval = ovs_dp_cmd_fill_info(dp, skb, pid, seq, 0, cmd);
aaff4b55
BP
1296 if (retval < 0) {
1297 kfree_skb(skb);
1298 return ERR_PTR(retval);
1299 }
1300 return skb;
1301}
9dca7bd5 1302
df2c07f4 1303static int ovs_dp_cmd_validate(struct nlattr *a[OVS_DP_ATTR_MAX + 1])
aaff4b55 1304{
df2c07f4
JP
1305 if (a[OVS_DP_ATTR_IPV4_FRAGS]) {
1306 u32 frags = nla_get_u32(a[OVS_DP_ATTR_IPV4_FRAGS]);
9dca7bd5 1307
df2c07f4 1308 if (frags != OVS_DP_FRAG_ZERO && frags != OVS_DP_FRAG_DROP)
aaff4b55 1309 return -EINVAL;
d6569377
BP
1310 }
1311
df2c07f4 1312 return CHECK_NUL_STRING(a[OVS_DP_ATTR_NAME], IFNAMSIZ - 1);
d6569377
BP
1313}
1314
ed099e92 1315/* Called with genl_mutex and optionally with RTNL lock also. */
df2c07f4 1316static struct datapath *lookup_datapath(struct ovs_header *ovs_header, struct nlattr *a[OVS_DP_ATTR_MAX + 1])
d6569377 1317{
254f2dc8
BP
1318 struct datapath *dp;
1319
df2c07f4
JP
1320 if (!a[OVS_DP_ATTR_NAME])
1321 dp = get_dp(ovs_header->dp_ifindex);
254f2dc8 1322 else {
d6569377 1323 struct vport *vport;
d6569377 1324
057dd6d2 1325 rcu_read_lock();
df2c07f4
JP
1326 vport = vport_locate(nla_data(a[OVS_DP_ATTR_NAME]));
1327 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
057dd6d2 1328 rcu_read_unlock();
d6569377 1329 }
254f2dc8 1330 return dp ? dp : ERR_PTR(-ENODEV);
d6569377
BP
1331}
1332
ed099e92 1333/* Called with genl_mutex. */
df2c07f4 1334static void change_datapath(struct datapath *dp, struct nlattr *a[OVS_DP_ATTR_MAX + 1])
d6569377 1335{
df2c07f4
JP
1336 if (a[OVS_DP_ATTR_IPV4_FRAGS])
1337 dp->drop_frags = nla_get_u32(a[OVS_DP_ATTR_IPV4_FRAGS]) == OVS_DP_FRAG_DROP;
d6569377
BP
1338}
1339
df2c07f4 1340static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
d6569377 1341{
aaff4b55 1342 struct nlattr **a = info->attrs;
d6569377 1343 struct vport_parms parms;
aaff4b55 1344 struct sk_buff *reply;
d6569377
BP
1345 struct datapath *dp;
1346 struct vport *vport;
d6569377 1347 int err;
d6569377 1348
d6569377 1349 err = -EINVAL;
ea36840f 1350 if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
aaff4b55
BP
1351 goto err;
1352
df2c07f4 1353 err = ovs_dp_cmd_validate(a);
aaff4b55
BP
1354 if (err)
1355 goto err;
d6569377
BP
1356
1357 rtnl_lock();
d6569377
BP
1358 err = -ENODEV;
1359 if (!try_module_get(THIS_MODULE))
ed099e92 1360 goto err_unlock_rtnl;
d6569377 1361
d6569377
BP
1362 err = -ENOMEM;
1363 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1364 if (dp == NULL)
1365 goto err_put_module;
1366 INIT_LIST_HEAD(&dp->port_list);
d6569377
BP
1367
1368 /* Initialize kobject for bridge. This will be added as
1369 * /sys/class/net/<devname>/brif later, if sysfs is enabled. */
1370 dp->ifobj.kset = NULL;
1371 kobject_init(&dp->ifobj, &dp_ktype);
1372
1373 /* Allocate table. */
1374 err = -ENOMEM;
3544358a 1375 rcu_assign_pointer(dp->table, flow_tbl_alloc(TBL_MIN_BUCKETS));
d6569377
BP
1376 if (!dp->table)
1377 goto err_free_dp;
1378
99769a40
JG
1379 dp->drop_frags = 0;
1380 dp->stats_percpu = alloc_percpu(struct dp_stats_percpu);
1381 if (!dp->stats_percpu) {
1382 err = -ENOMEM;
1383 goto err_destroy_table;
1384 }
1385
1386 change_datapath(dp, a);
1387
d6569377 1388 /* Set up our datapath device. */
df2c07f4
JP
1389 parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1390 parms.type = OVS_VPORT_TYPE_INTERNAL;
d6569377
BP
1391 parms.options = NULL;
1392 parms.dp = dp;
df2c07f4 1393 parms.port_no = OVSP_LOCAL;
ea36840f 1394 parms.upcall_pid = nla_get_u32(a[OVS_DP_ATTR_UPCALL_PID]);
b063d9f0 1395
d6569377
BP
1396 vport = new_vport(&parms);
1397 if (IS_ERR(vport)) {
1398 err = PTR_ERR(vport);
1399 if (err == -EBUSY)
1400 err = -EEXIST;
1401
99769a40 1402 goto err_destroy_percpu;
d6569377 1403 }
d6569377 1404
df2c07f4 1405 reply = ovs_dp_cmd_build_info(dp, info->snd_pid, info->snd_seq, OVS_DP_CMD_NEW);
aaff4b55
BP
1406 err = PTR_ERR(reply);
1407 if (IS_ERR(reply))
1408 goto err_destroy_local_port;
1409
254f2dc8 1410 list_add_tail(&dp->list_node, &dps);
d6569377
BP
1411 dp_sysfs_add_dp(dp);
1412
d6569377
BP
1413 rtnl_unlock();
1414
aaff4b55
BP
1415 genl_notify(reply, genl_info_net(info), info->snd_pid,
1416 dp_datapath_multicast_group.id, info->nlhdr, GFP_KERNEL);
d6569377
BP
1417 return 0;
1418
1419err_destroy_local_port:
df2c07f4 1420 dp_detach_port(get_vport_protected(dp, OVSP_LOCAL));
99769a40
JG
1421err_destroy_percpu:
1422 free_percpu(dp->stats_percpu);
d6569377 1423err_destroy_table:
3544358a 1424 flow_tbl_destroy(get_table_protected(dp));
d6569377 1425err_free_dp:
d6569377
BP
1426 kfree(dp);
1427err_put_module:
1428 module_put(THIS_MODULE);
ed099e92 1429err_unlock_rtnl:
d6569377 1430 rtnl_unlock();
d6569377 1431err:
064af421
BP
1432 return err;
1433}
1434
df2c07f4 1435static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
44e05eca 1436{
ed099e92 1437 struct vport *vport, *next_vport;
aaff4b55 1438 struct sk_buff *reply;
9c52546b 1439 struct datapath *dp;
d6569377 1440 int err;
44e05eca 1441
df2c07f4 1442 err = ovs_dp_cmd_validate(info->attrs);
aaff4b55 1443 if (err)
d6569377 1444 goto exit;
44e05eca 1445
d6569377 1446 rtnl_lock();
aaff4b55 1447 dp = lookup_datapath(info->userhdr, info->attrs);
d6569377
BP
1448 err = PTR_ERR(dp);
1449 if (IS_ERR(dp))
aaff4b55
BP
1450 goto exit_unlock;
1451
df2c07f4 1452 reply = ovs_dp_cmd_build_info(dp, info->snd_pid, info->snd_seq, OVS_DP_CMD_DEL);
aaff4b55
BP
1453 err = PTR_ERR(reply);
1454 if (IS_ERR(reply))
1455 goto exit_unlock;
9c52546b 1456
ed099e92 1457 list_for_each_entry_safe (vport, next_vport, &dp->port_list, node)
df2c07f4 1458 if (vport->port_no != OVSP_LOCAL)
ed099e92
BP
1459 dp_detach_port(vport);
1460
1461 dp_sysfs_del_dp(dp);
254f2dc8 1462 list_del(&dp->list_node);
df2c07f4 1463 dp_detach_port(get_vport_protected(dp, OVSP_LOCAL));
ed099e92 1464
99620d2c
JG
1465 /* rtnl_unlock() will wait until all the references to devices that
1466 * are pending unregistration have been dropped. We do it here to
1467 * ensure that any internal devices (which contain DP pointers) are
1468 * fully destroyed before freeing the datapath.
1469 */
1470 rtnl_unlock();
1471
ed099e92
BP
1472 call_rcu(&dp->rcu, destroy_dp_rcu);
1473 module_put(THIS_MODULE);
1474
aaff4b55
BP
1475 genl_notify(reply, genl_info_net(info), info->snd_pid,
1476 dp_datapath_multicast_group.id, info->nlhdr, GFP_KERNEL);
99620d2c
JG
1477
1478 return 0;
d6569377 1479
aaff4b55 1480exit_unlock:
d6569377
BP
1481 rtnl_unlock();
1482exit:
1483 return err;
44e05eca
BP
1484}
1485
df2c07f4 1486static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
064af421 1487{
aaff4b55 1488 struct sk_buff *reply;
d6569377 1489 struct datapath *dp;
d6569377 1490 int err;
064af421 1491
df2c07f4 1492 err = ovs_dp_cmd_validate(info->attrs);
aaff4b55
BP
1493 if (err)
1494 return err;
38c6ecbc 1495
aaff4b55 1496 dp = lookup_datapath(info->userhdr, info->attrs);
d6569377 1497 if (IS_ERR(dp))
aaff4b55 1498 return PTR_ERR(dp);
38c6ecbc 1499
aaff4b55 1500 change_datapath(dp, info->attrs);
38c6ecbc 1501
df2c07f4 1502 reply = ovs_dp_cmd_build_info(dp, info->snd_pid, info->snd_seq, OVS_DP_CMD_NEW);
aaff4b55
BP
1503 if (IS_ERR(reply)) {
1504 err = PTR_ERR(reply);
1505 netlink_set_err(INIT_NET_GENL_SOCK, 0,
1506 dp_datapath_multicast_group.id, err);
1507 return 0;
1508 }
1509
1510 genl_notify(reply, genl_info_net(info), info->snd_pid,
1511 dp_datapath_multicast_group.id, info->nlhdr, GFP_KERNEL);
1512 return 0;
064af421
BP
1513}
1514
df2c07f4 1515static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1dcf111b 1516{
aaff4b55 1517 struct sk_buff *reply;
d6569377 1518 struct datapath *dp;
d6569377 1519 int err;
1dcf111b 1520
df2c07f4 1521 err = ovs_dp_cmd_validate(info->attrs);
aaff4b55
BP
1522 if (err)
1523 return err;
1dcf111b 1524
aaff4b55 1525 dp = lookup_datapath(info->userhdr, info->attrs);
d6569377 1526 if (IS_ERR(dp))
aaff4b55 1527 return PTR_ERR(dp);
1dcf111b 1528
df2c07f4 1529 reply = ovs_dp_cmd_build_info(dp, info->snd_pid, info->snd_seq, OVS_DP_CMD_NEW);
aaff4b55
BP
1530 if (IS_ERR(reply))
1531 return PTR_ERR(reply);
1532
1533 return genlmsg_reply(reply, info);
1dcf111b
JP
1534}
1535
df2c07f4 1536static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
a7786963 1537{
254f2dc8
BP
1538 struct datapath *dp;
1539 int skip = cb->args[0];
1540 int i = 0;
a7786963 1541
254f2dc8
BP
1542 list_for_each_entry (dp, &dps, list_node) {
1543 if (i < skip)
d6569377 1544 continue;
df2c07f4 1545 if (ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).pid,
aaff4b55 1546 cb->nlh->nlmsg_seq, NLM_F_MULTI,
df2c07f4 1547 OVS_DP_CMD_NEW) < 0)
aaff4b55 1548 break;
254f2dc8 1549 i++;
a7786963 1550 }
aaff4b55 1551
254f2dc8
BP
1552 cb->args[0] = i;
1553
aaff4b55 1554 return skb->len;
c19e6535
BP
1555}
1556
aaff4b55 1557static struct genl_ops dp_datapath_genl_ops[] = {
df2c07f4 1558 { .cmd = OVS_DP_CMD_NEW,
aaff4b55
BP
1559 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1560 .policy = datapath_policy,
df2c07f4 1561 .doit = ovs_dp_cmd_new
aaff4b55 1562 },
df2c07f4 1563 { .cmd = OVS_DP_CMD_DEL,
aaff4b55
BP
1564 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1565 .policy = datapath_policy,
df2c07f4 1566 .doit = ovs_dp_cmd_del
aaff4b55 1567 },
df2c07f4 1568 { .cmd = OVS_DP_CMD_GET,
aaff4b55
BP
1569 .flags = 0, /* OK for unprivileged users. */
1570 .policy = datapath_policy,
df2c07f4
JP
1571 .doit = ovs_dp_cmd_get,
1572 .dumpit = ovs_dp_cmd_dump
aaff4b55 1573 },
df2c07f4 1574 { .cmd = OVS_DP_CMD_SET,
aaff4b55
BP
1575 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1576 .policy = datapath_policy,
df2c07f4 1577 .doit = ovs_dp_cmd_set,
aaff4b55
BP
1578 },
1579};
1580
df2c07f4 1581static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
f0fef760 1582#ifdef HAVE_NLA_NUL_STRING
df2c07f4 1583 [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
f613a0d7 1584 [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
df2c07f4 1585 [OVS_VPORT_ATTR_ADDRESS] = { .len = ETH_ALEN },
f0fef760 1586#else
f613a0d7 1587 [OVS_VPORT_ATTR_STATS] = { .minlen = sizeof(struct ovs_vport_stats) },
df2c07f4 1588 [OVS_VPORT_ATTR_ADDRESS] = { .minlen = ETH_ALEN },
f0fef760 1589#endif
d48c88ec
JG
1590 [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
1591 [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
b063d9f0 1592 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
df2c07f4 1593 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
c19e6535
BP
1594};
1595
f0fef760
BP
1596static struct genl_family dp_vport_genl_family = {
1597 .id = GENL_ID_GENERATE,
df2c07f4
JP
1598 .hdrsize = sizeof(struct ovs_header),
1599 .name = OVS_VPORT_FAMILY,
f0fef760 1600 .version = 1,
df2c07f4 1601 .maxattr = OVS_VPORT_ATTR_MAX
f0fef760
BP
1602};
1603
f14d8083 1604struct genl_multicast_group dp_vport_multicast_group = {
df2c07f4 1605 .name = OVS_VPORT_MCGROUP
f0fef760
BP
1606};
1607
1608/* Called with RTNL lock or RCU read lock. */
df2c07f4 1609static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
f0fef760 1610 u32 pid, u32 seq, u32 flags, u8 cmd)
064af421 1611{
df2c07f4 1612 struct ovs_header *ovs_header;
c19e6535 1613 struct nlattr *nla;
c19e6535
BP
1614 int err;
1615
df2c07f4 1616 ovs_header = genlmsg_put(skb, pid, seq, &dp_vport_genl_family,
f0fef760 1617 flags, cmd);
df2c07f4 1618 if (!ovs_header)
f0fef760 1619 return -EMSGSIZE;
c19e6535 1620
99769a40 1621 ovs_header->dp_ifindex = get_dpifindex(vport->dp);
c19e6535 1622
df2c07f4
JP
1623 NLA_PUT_U32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no);
1624 NLA_PUT_U32(skb, OVS_VPORT_ATTR_TYPE, vport_get_type(vport));
1625 NLA_PUT_STRING(skb, OVS_VPORT_ATTR_NAME, vport_get_name(vport));
b063d9f0 1626 NLA_PUT_U32(skb, OVS_VPORT_ATTR_UPCALL_PID, vport->upcall_pid);
c19e6535 1627
f613a0d7 1628 nla = nla_reserve(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats));
c19e6535
BP
1629 if (!nla)
1630 goto nla_put_failure;
f613a0d7
PS
1631
1632 vport_get_stats(vport, nla_data(nla));
c19e6535 1633
df2c07f4 1634 NLA_PUT(skb, OVS_VPORT_ATTR_ADDRESS, ETH_ALEN, vport_get_addr(vport));
c19e6535 1635
c19e6535 1636 err = vport_get_options(vport, skb);
f0fef760
BP
1637 if (err == -EMSGSIZE)
1638 goto error;
c19e6535 1639
df2c07f4 1640 return genlmsg_end(skb, ovs_header);
c19e6535
BP
1641
1642nla_put_failure:
1643 err = -EMSGSIZE;
f0fef760 1644error:
df2c07f4 1645 genlmsg_cancel(skb, ovs_header);
f0fef760 1646 return err;
064af421
BP
1647}
1648
f0fef760 1649/* Called with RTNL lock or RCU read lock. */
f14d8083
EJ
1650struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 pid,
1651 u32 seq, u8 cmd)
064af421 1652{
c19e6535 1653 struct sk_buff *skb;
f0fef760 1654 int retval;
c19e6535 1655
f0fef760 1656 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
c19e6535
BP
1657 if (!skb)
1658 return ERR_PTR(-ENOMEM);
1659
df2c07f4 1660 retval = ovs_vport_cmd_fill_info(vport, skb, pid, seq, 0, cmd);
f0fef760
BP
1661 if (retval < 0) {
1662 kfree_skb(skb);
1663 return ERR_PTR(retval);
1664 }
c19e6535 1665 return skb;
f0fef760 1666}
c19e6535 1667
df2c07f4 1668static int ovs_vport_cmd_validate(struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
f0fef760 1669{
df2c07f4 1670 return CHECK_NUL_STRING(a[OVS_VPORT_ATTR_NAME], IFNAMSIZ - 1);
c19e6535 1671}
51d4d598 1672
ed099e92 1673/* Called with RTNL lock or RCU read lock. */
df2c07f4
JP
1674static struct vport *lookup_vport(struct ovs_header *ovs_header,
1675 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
c19e6535
BP
1676{
1677 struct datapath *dp;
1678 struct vport *vport;
1679
df2c07f4
JP
1680 if (a[OVS_VPORT_ATTR_NAME]) {
1681 vport = vport_locate(nla_data(a[OVS_VPORT_ATTR_NAME]));
ed099e92 1682 if (!vport)
c19e6535 1683 return ERR_PTR(-ENODEV);
c19e6535 1684 return vport;
df2c07f4
JP
1685 } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
1686 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
c19e6535
BP
1687
1688 if (port_no >= DP_MAX_PORTS)
f0fef760 1689 return ERR_PTR(-EFBIG);
c19e6535 1690
df2c07f4 1691 dp = get_dp(ovs_header->dp_ifindex);
c19e6535
BP
1692 if (!dp)
1693 return ERR_PTR(-ENODEV);
f2459fe7 1694
c19e6535 1695 vport = get_vport_protected(dp, port_no);
ed099e92 1696 if (!vport)
c19e6535 1697 return ERR_PTR(-ENOENT);
c19e6535
BP
1698 return vport;
1699 } else
1700 return ERR_PTR(-EINVAL);
064af421
BP
1701}
1702
ed099e92 1703/* Called with RTNL lock. */
df2c07f4 1704static int change_vport(struct vport *vport, struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
064af421 1705{
c19e6535 1706 int err = 0;
f613a0d7 1707
df2c07f4 1708 if (a[OVS_VPORT_ATTR_STATS])
f613a0d7
PS
1709 vport_set_stats(vport, nla_data(a[OVS_VPORT_ATTR_STATS]));
1710
1711 if (a[OVS_VPORT_ATTR_ADDRESS])
df2c07f4 1712 err = vport_set_addr(vport, nla_data(a[OVS_VPORT_ATTR_ADDRESS]));
f613a0d7 1713
c19e6535
BP
1714 return err;
1715}
1716
df2c07f4 1717static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
c19e6535 1718{
f0fef760 1719 struct nlattr **a = info->attrs;
df2c07f4 1720 struct ovs_header *ovs_header = info->userhdr;
c19e6535 1721 struct vport_parms parms;
ed099e92 1722 struct sk_buff *reply;
c19e6535 1723 struct vport *vport;
c19e6535 1724 struct datapath *dp;
b0ec0f27 1725 u32 port_no;
c19e6535 1726 int err;
b0ec0f27 1727
c19e6535 1728 err = -EINVAL;
ea36840f
BP
1729 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
1730 !a[OVS_VPORT_ATTR_UPCALL_PID])
f0fef760
BP
1731 goto exit;
1732
df2c07f4 1733 err = ovs_vport_cmd_validate(a);
f0fef760
BP
1734 if (err)
1735 goto exit;
51d4d598 1736
c19e6535 1737 rtnl_lock();
df2c07f4 1738 dp = get_dp(ovs_header->dp_ifindex);
c19e6535
BP
1739 err = -ENODEV;
1740 if (!dp)
ed099e92 1741 goto exit_unlock;
c19e6535 1742
df2c07f4
JP
1743 if (a[OVS_VPORT_ATTR_PORT_NO]) {
1744 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
c19e6535
BP
1745
1746 err = -EFBIG;
1747 if (port_no >= DP_MAX_PORTS)
ed099e92 1748 goto exit_unlock;
c19e6535
BP
1749
1750 vport = get_vport_protected(dp, port_no);
1751 err = -EBUSY;
1752 if (vport)
ed099e92 1753 goto exit_unlock;
c19e6535
BP
1754 } else {
1755 for (port_no = 1; ; port_no++) {
1756 if (port_no >= DP_MAX_PORTS) {
1757 err = -EFBIG;
ed099e92 1758 goto exit_unlock;
c19e6535
BP
1759 }
1760 vport = get_vport_protected(dp, port_no);
1761 if (!vport)
1762 break;
51d4d598 1763 }
064af421 1764 }
b0ec0f27 1765
df2c07f4
JP
1766 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
1767 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1768 parms.options = a[OVS_VPORT_ATTR_OPTIONS];
c19e6535
BP
1769 parms.dp = dp;
1770 parms.port_no = port_no;
ea36840f 1771 parms.upcall_pid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
c19e6535
BP
1772
1773 vport = new_vport(&parms);
1774 err = PTR_ERR(vport);
1775 if (IS_ERR(vport))
ed099e92 1776 goto exit_unlock;
c19e6535 1777
c19e6535
BP
1778 dp_sysfs_add_if(vport);
1779
1780 err = change_vport(vport, a);
f0fef760 1781 if (!err) {
df2c07f4
JP
1782 reply = ovs_vport_cmd_build_info(vport, info->snd_pid,
1783 info->snd_seq, OVS_VPORT_CMD_NEW);
f0fef760
BP
1784 if (IS_ERR(reply))
1785 err = PTR_ERR(reply);
1786 }
c19e6535
BP
1787 if (err) {
1788 dp_detach_port(vport);
ed099e92 1789 goto exit_unlock;
c19e6535 1790 }
f0fef760
BP
1791 genl_notify(reply, genl_info_net(info), info->snd_pid,
1792 dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
c19e6535 1793
c19e6535 1794
ed099e92 1795exit_unlock:
c19e6535 1796 rtnl_unlock();
c19e6535
BP
1797exit:
1798 return err;
44e05eca
BP
1799}
1800
df2c07f4 1801static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
44e05eca 1802{
f0fef760
BP
1803 struct nlattr **a = info->attrs;
1804 struct sk_buff *reply;
c19e6535 1805 struct vport *vport;
c19e6535 1806 int err;
44e05eca 1807
df2c07f4 1808 err = ovs_vport_cmd_validate(a);
f0fef760 1809 if (err)
c19e6535
BP
1810 goto exit;
1811
1812 rtnl_lock();
f0fef760 1813 vport = lookup_vport(info->userhdr, a);
c19e6535
BP
1814 err = PTR_ERR(vport);
1815 if (IS_ERR(vport))
f0fef760 1816 goto exit_unlock;
44e05eca 1817
c19e6535 1818 err = 0;
4879d4c7
BP
1819 if (a[OVS_VPORT_ATTR_TYPE] && nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport_get_type(vport))
1820 err = -EINVAL;
1821 if (!err && a[OVS_VPORT_ATTR_OPTIONS])
df2c07f4 1822 err = vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
c19e6535
BP
1823 if (!err)
1824 err = change_vport(vport, a);
b063d9f0
JG
1825 if (!err && a[OVS_VPORT_ATTR_UPCALL_PID])
1826 vport->upcall_pid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
c19e6535 1827
df2c07f4
JP
1828 reply = ovs_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
1829 OVS_VPORT_CMD_NEW);
f0fef760
BP
1830 if (IS_ERR(reply)) {
1831 err = PTR_ERR(reply);
1832 netlink_set_err(INIT_NET_GENL_SOCK, 0,
1833 dp_vport_multicast_group.id, err);
1834 return 0;
1835 }
1836
1837 genl_notify(reply, genl_info_net(info), info->snd_pid,
1838 dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
1839
1840exit_unlock:
c19e6535
BP
1841 rtnl_unlock();
1842exit:
1843 return err;
064af421
BP
1844}
1845
df2c07f4 1846static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
7c40efc9 1847{
f0fef760
BP
1848 struct nlattr **a = info->attrs;
1849 struct sk_buff *reply;
c19e6535 1850 struct vport *vport;
c19e6535
BP
1851 int err;
1852
df2c07f4 1853 err = ovs_vport_cmd_validate(a);
f0fef760 1854 if (err)
c19e6535
BP
1855 goto exit;
1856
1857 rtnl_lock();
f0fef760 1858 vport = lookup_vport(info->userhdr, a);
c19e6535 1859 err = PTR_ERR(vport);
f0fef760
BP
1860 if (IS_ERR(vport))
1861 goto exit_unlock;
c19e6535 1862
df2c07f4 1863 if (vport->port_no == OVSP_LOCAL) {
f0fef760
BP
1864 err = -EINVAL;
1865 goto exit_unlock;
1866 }
1867
df2c07f4
JP
1868 reply = ovs_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
1869 OVS_VPORT_CMD_DEL);
f0fef760
BP
1870 err = PTR_ERR(reply);
1871 if (IS_ERR(reply))
1872 goto exit_unlock;
1873
3544358a 1874 dp_detach_port(vport);
f0fef760
BP
1875
1876 genl_notify(reply, genl_info_net(info), info->snd_pid,
1877 dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
1878
1879exit_unlock:
c19e6535
BP
1880 rtnl_unlock();
1881exit:
1882 return err;
7c40efc9
BP
1883}
1884
df2c07f4 1885static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
7c40efc9 1886{
f0fef760 1887 struct nlattr **a = info->attrs;
df2c07f4 1888 struct ovs_header *ovs_header = info->userhdr;
ed099e92 1889 struct sk_buff *reply;
c19e6535 1890 struct vport *vport;
c19e6535
BP
1891 int err;
1892
df2c07f4 1893 err = ovs_vport_cmd_validate(a);
f0fef760
BP
1894 if (err)
1895 goto exit;
c19e6535 1896
ed099e92 1897 rcu_read_lock();
df2c07f4 1898 vport = lookup_vport(ovs_header, a);
c19e6535
BP
1899 err = PTR_ERR(vport);
1900 if (IS_ERR(vport))
f0fef760 1901 goto exit_unlock;
c19e6535 1902
df2c07f4
JP
1903 reply = ovs_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
1904 OVS_VPORT_CMD_NEW);
ed099e92
BP
1905 err = PTR_ERR(reply);
1906 if (IS_ERR(reply))
f0fef760 1907 goto exit_unlock;
ed099e92 1908
df2fa9b5
JG
1909 rcu_read_unlock();
1910
1911 return genlmsg_reply(reply, info);
ed099e92 1912
f0fef760 1913exit_unlock:
ed099e92 1914 rcu_read_unlock();
f0fef760 1915exit:
c19e6535
BP
1916 return err;
1917}
1918
df2c07f4 1919static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
c19e6535 1920{
df2c07f4 1921 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
c19e6535
BP
1922 struct datapath *dp;
1923 u32 port_no;
f0fef760 1924 int retval;
c19e6535 1925
df2c07f4 1926 dp = get_dp(ovs_header->dp_ifindex);
c19e6535 1927 if (!dp)
f0fef760 1928 return -ENODEV;
ed099e92
BP
1929
1930 rcu_read_lock();
f0fef760 1931 for (port_no = cb->args[0]; port_no < DP_MAX_PORTS; port_no++) {
ed099e92 1932 struct vport *vport;
ed099e92
BP
1933
1934 vport = get_vport_protected(dp, port_no);
1935 if (!vport)
1936 continue;
1937
df2c07f4 1938 if (ovs_vport_cmd_fill_info(vport, skb, NETLINK_CB(cb->skb).pid,
f0fef760 1939 cb->nlh->nlmsg_seq, NLM_F_MULTI,
df2c07f4 1940 OVS_VPORT_CMD_NEW) < 0)
f0fef760 1941 break;
c19e6535 1942 }
ed099e92 1943 rcu_read_unlock();
c19e6535 1944
f0fef760
BP
1945 cb->args[0] = port_no;
1946 retval = skb->len;
1947
1948 return retval;
7c40efc9
BP
1949}
1950
f0fef760 1951static struct genl_ops dp_vport_genl_ops[] = {
df2c07f4 1952 { .cmd = OVS_VPORT_CMD_NEW,
f0fef760
BP
1953 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1954 .policy = vport_policy,
df2c07f4 1955 .doit = ovs_vport_cmd_new
f0fef760 1956 },
df2c07f4 1957 { .cmd = OVS_VPORT_CMD_DEL,
f0fef760
BP
1958 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1959 .policy = vport_policy,
df2c07f4 1960 .doit = ovs_vport_cmd_del
f0fef760 1961 },
df2c07f4 1962 { .cmd = OVS_VPORT_CMD_GET,
f0fef760
BP
1963 .flags = 0, /* OK for unprivileged users. */
1964 .policy = vport_policy,
df2c07f4
JP
1965 .doit = ovs_vport_cmd_get,
1966 .dumpit = ovs_vport_cmd_dump
f0fef760 1967 },
df2c07f4 1968 { .cmd = OVS_VPORT_CMD_SET,
f0fef760
BP
1969 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1970 .policy = vport_policy,
df2c07f4 1971 .doit = ovs_vport_cmd_set,
f0fef760
BP
1972 },
1973};
1974
982b8810
BP
1975struct genl_family_and_ops {
1976 struct genl_family *family;
1977 struct genl_ops *ops;
1978 int n_ops;
1979 struct genl_multicast_group *group;
1980};
ed099e92 1981
982b8810 1982static const struct genl_family_and_ops dp_genl_families[] = {
aaff4b55
BP
1983 { &dp_datapath_genl_family,
1984 dp_datapath_genl_ops, ARRAY_SIZE(dp_datapath_genl_ops),
1985 &dp_datapath_multicast_group },
f0fef760
BP
1986 { &dp_vport_genl_family,
1987 dp_vport_genl_ops, ARRAY_SIZE(dp_vport_genl_ops),
1988 &dp_vport_multicast_group },
37a1300c
BP
1989 { &dp_flow_genl_family,
1990 dp_flow_genl_ops, ARRAY_SIZE(dp_flow_genl_ops),
1991 &dp_flow_multicast_group },
982b8810
BP
1992 { &dp_packet_genl_family,
1993 dp_packet_genl_ops, ARRAY_SIZE(dp_packet_genl_ops),
1994 NULL },
1995};
ed099e92 1996
982b8810
BP
1997static void dp_unregister_genl(int n_families)
1998{
1999 int i;
ed099e92 2000
b867ca75 2001 for (i = 0; i < n_families; i++)
982b8810 2002 genl_unregister_family(dp_genl_families[i].family);
ed099e92
BP
2003}
2004
982b8810 2005static int dp_register_genl(void)
064af421 2006{
982b8810
BP
2007 int n_registered;
2008 int err;
2009 int i;
064af421 2010
982b8810
BP
2011 n_registered = 0;
2012 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
2013 const struct genl_family_and_ops *f = &dp_genl_families[i];
064af421 2014
982b8810
BP
2015 err = genl_register_family_with_ops(f->family, f->ops,
2016 f->n_ops);
2017 if (err)
2018 goto error;
2019 n_registered++;
e22d4953 2020
982b8810
BP
2021 if (f->group) {
2022 err = genl_register_mc_group(f->family, f->group);
2023 if (err)
2024 goto error;
2025 }
2026 }
9cc8b4e4 2027
982b8810 2028 return 0;
064af421
BP
2029
2030error:
982b8810
BP
2031 dp_unregister_genl(n_registered);
2032 return err;
064af421
BP
2033}
2034
22d24ebf
BP
2035static int __init dp_init(void)
2036{
f2459fe7 2037 struct sk_buff *dummy_skb;
22d24ebf
BP
2038 int err;
2039
f2459fe7 2040 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > sizeof(dummy_skb->cb));
22d24ebf 2041
f2459fe7 2042 printk("Open vSwitch %s, built "__DATE__" "__TIME__"\n", VERSION BUILDNR);
064af421 2043
3544358a 2044 err = tnl_init();
064af421
BP
2045 if (err)
2046 goto error;
2047
3544358a
PS
2048 err = flow_init();
2049 if (err)
2050 goto error_tnl_exit;
2051
f2459fe7 2052 err = vport_init();
064af421
BP
2053 if (err)
2054 goto error_flow_exit;
2055
f2459fe7
JG
2056 err = register_netdevice_notifier(&dp_device_notifier);
2057 if (err)
2058 goto error_vport_exit;
2059
982b8810
BP
2060 err = dp_register_genl();
2061 if (err < 0)
37a1300c 2062 goto error_unreg_notifier;
982b8810 2063
064af421
BP
2064 return 0;
2065
2066error_unreg_notifier:
2067 unregister_netdevice_notifier(&dp_device_notifier);
f2459fe7
JG
2068error_vport_exit:
2069 vport_exit();
064af421
BP
2070error_flow_exit:
2071 flow_exit();
3544358a
PS
2072error_tnl_exit:
2073 tnl_exit();
064af421
BP
2074error:
2075 return err;
2076}
2077
2078static void dp_cleanup(void)
2079{
2080 rcu_barrier();
982b8810 2081 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
064af421 2082 unregister_netdevice_notifier(&dp_device_notifier);
f2459fe7 2083 vport_exit();
064af421 2084 flow_exit();
3544358a 2085 tnl_exit();
064af421
BP
2086}
2087
2088module_init(dp_init);
2089module_exit(dp_cleanup);
2090
2091MODULE_DESCRIPTION("Open vSwitch switching datapath");
2092MODULE_LICENSE("GPL");