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