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