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