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