jsonrpc.py: Import "ovs.json".
[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         /* Initialize OVS_CB (it came from Netlink so might not be zeroed). */
713         OVS_CB(packet)->vport = NULL;
714         OVS_CB(packet)->flow = NULL;
715         /* execute_actions() will reset tun_id to 0 anyhow. */
716 #ifdef NEED_CSUM_NORMALIZE
717         OVS_CB(packet)->ip_summed = OVS_CSUM_NONE;
718 #endif
719         vlan_copy_skb_tci(packet);
720
721         rcu_read_lock();
722         dp = get_dp(odp_header->dp_ifindex);
723         err = -ENODEV;
724         if (dp)
725                 err = execute_actions(dp, packet, &key,
726                                       nla_data(a[ODP_PACKET_ATTR_ACTIONS]),
727                                       nla_len(a[ODP_PACKET_ATTR_ACTIONS]));
728         rcu_read_unlock();
729
730 exit:
731         return err;
732 }
733
734 static const struct nla_policy packet_policy[ODP_PACKET_ATTR_MAX + 1] = {
735         [ODP_PACKET_ATTR_PACKET] = { .type = NLA_UNSPEC },
736         [ODP_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
737 };
738
739 static struct genl_ops dp_packet_genl_ops[] = {
740         { .cmd = ODP_PACKET_CMD_EXECUTE,
741           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
742           .policy = packet_policy,
743           .doit = odp_packet_cmd_execute
744         }
745 };
746
747 static void get_dp_stats(struct datapath *dp, struct odp_stats *stats)
748 {
749         int i;
750
751         stats->n_frags = stats->n_hit = stats->n_missed = stats->n_lost = 0;
752         for_each_possible_cpu(i) {
753                 const struct dp_stats_percpu *percpu_stats;
754                 struct dp_stats_percpu local_stats;
755                 unsigned seqcount;
756
757                 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
758
759                 do {
760                         seqcount = read_seqcount_begin(&percpu_stats->seqlock);
761                         local_stats = *percpu_stats;
762                 } while (read_seqcount_retry(&percpu_stats->seqlock, seqcount));
763
764                 stats->n_frags += local_stats.n_frags;
765                 stats->n_hit += local_stats.n_hit;
766                 stats->n_missed += local_stats.n_missed;
767                 stats->n_lost += local_stats.n_lost;
768         }
769 }
770
771 /* MTU of the dp pseudo-device: ETH_DATA_LEN or the minimum of the ports.
772  * Called with RTNL lock.
773  */
774 int dp_min_mtu(const struct datapath *dp)
775 {
776         struct vport *p;
777         int mtu = 0;
778
779         ASSERT_RTNL();
780
781         list_for_each_entry (p, &dp->port_list, node) {
782                 int dev_mtu;
783
784                 /* Skip any internal ports, since that's what we're trying to
785                  * set. */
786                 if (is_internal_vport(p))
787                         continue;
788
789                 dev_mtu = vport_get_mtu(p);
790                 if (!dev_mtu)
791                         continue;
792                 if (!mtu || dev_mtu < mtu)
793                         mtu = dev_mtu;
794         }
795
796         return mtu ? mtu : ETH_DATA_LEN;
797 }
798
799 /* Sets the MTU of all datapath devices to the minimum of the ports
800  * Called with RTNL lock.
801  */
802 void set_internal_devs_mtu(const struct datapath *dp)
803 {
804         struct vport *p;
805         int mtu;
806
807         ASSERT_RTNL();
808
809         mtu = dp_min_mtu(dp);
810
811         list_for_each_entry (p, &dp->port_list, node) {
812                 if (is_internal_vport(p))
813                         vport_set_mtu(p, mtu);
814         }
815 }
816
817 static const struct nla_policy flow_policy[ODP_FLOW_ATTR_MAX + 1] = {
818         [ODP_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
819         [ODP_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
820         [ODP_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
821 };
822
823 static struct genl_family dp_flow_genl_family = {
824         .id = GENL_ID_GENERATE,
825         .hdrsize = sizeof(struct odp_header),
826         .name = ODP_FLOW_FAMILY,
827         .version = 1,
828         .maxattr = ODP_FLOW_ATTR_MAX
829 };
830
831 static struct genl_multicast_group dp_flow_multicast_group = {
832         .name = ODP_FLOW_MCGROUP
833 };
834
835 /* Called with genl_lock. */
836 static int odp_flow_cmd_fill_info(struct sw_flow *flow, struct datapath *dp,
837                                   struct sk_buff *skb, u32 pid, u32 seq, u32 flags, u8 cmd)
838 {
839         const int skb_orig_len = skb->len;
840         const struct sw_flow_actions *sf_acts;
841         struct odp_flow_stats stats;
842         struct odp_header *odp_header;
843         struct nlattr *nla;
844         unsigned long used;
845         u8 tcp_flags;
846         int err;
847
848         sf_acts = rcu_dereference_protected(flow->sf_acts,
849                                             lockdep_genl_is_held());
850
851         odp_header = genlmsg_put(skb, pid, seq, &dp_flow_genl_family, flags, cmd);
852         if (!odp_header)
853                 return -EMSGSIZE;
854
855         odp_header->dp_ifindex = dp->dp_ifindex;
856
857         nla = nla_nest_start(skb, ODP_FLOW_ATTR_KEY);
858         if (!nla)
859                 goto nla_put_failure;
860         err = flow_to_nlattrs(&flow->key, skb);
861         if (err)
862                 goto error;
863         nla_nest_end(skb, nla);
864
865         spin_lock_bh(&flow->lock);
866         used = flow->used;
867         stats.n_packets = flow->packet_count;
868         stats.n_bytes = flow->byte_count;
869         tcp_flags = flow->tcp_flags;
870         spin_unlock_bh(&flow->lock);
871
872         if (used)
873                 NLA_PUT_U64(skb, ODP_FLOW_ATTR_USED, flow_used_time(used));
874
875         if (stats.n_packets)
876                 NLA_PUT(skb, ODP_FLOW_ATTR_STATS, sizeof(struct odp_flow_stats), &stats);
877
878         if (tcp_flags)
879                 NLA_PUT_U8(skb, ODP_FLOW_ATTR_TCP_FLAGS, tcp_flags);
880
881         /* If ODP_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
882          * this is the first flow to be dumped into 'skb'.  This is unusual for
883          * Netlink but individual action lists can be longer than
884          * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
885          * The userspace caller can always fetch the actions separately if it
886          * really wants them.  (Most userspace callers in fact don't care.)
887          *
888          * This can only fail for dump operations because the skb is always
889          * properly sized for single flows.
890          */
891         err = nla_put(skb, ODP_FLOW_ATTR_ACTIONS, sf_acts->actions_len,
892                       sf_acts->actions);
893         if (err < 0 && skb_orig_len)
894                 goto error;
895
896         return genlmsg_end(skb, odp_header);
897
898 nla_put_failure:
899         err = -EMSGSIZE;
900 error:
901         genlmsg_cancel(skb, odp_header);
902         return err;
903 }
904
905 static struct sk_buff *odp_flow_cmd_alloc_info(struct sw_flow *flow)
906 {
907         const struct sw_flow_actions *sf_acts;
908         int len;
909
910         sf_acts = rcu_dereference_protected(flow->sf_acts,
911                                             lockdep_genl_is_held());
912
913         len = nla_total_size(FLOW_BUFSIZE); /* ODP_FLOW_ATTR_KEY */
914         len += nla_total_size(sf_acts->actions_len); /* ODP_FLOW_ATTR_ACTIONS */
915         len += nla_total_size(sizeof(struct odp_flow_stats)); /* ODP_FLOW_ATTR_STATS */
916         len += nla_total_size(1); /* ODP_FLOW_ATTR_TCP_FLAGS */
917         len += nla_total_size(8); /* ODP_FLOW_ATTR_USED */
918         return genlmsg_new(NLMSG_ALIGN(sizeof(struct odp_header)) + len, GFP_KERNEL);
919 }
920
921 static struct sk_buff *odp_flow_cmd_build_info(struct sw_flow *flow, struct datapath *dp,
922                                                u32 pid, u32 seq, u8 cmd)
923 {
924         struct sk_buff *skb;
925         int retval;
926
927         skb = odp_flow_cmd_alloc_info(flow);
928         if (!skb)
929                 return ERR_PTR(-ENOMEM);
930
931         retval = odp_flow_cmd_fill_info(flow, dp, skb, pid, seq, 0, cmd);
932         BUG_ON(retval < 0);
933         return skb;
934 }
935
936 static int odp_flow_cmd_new_or_set(struct sk_buff *skb, struct genl_info *info)
937 {
938         struct nlattr **a = info->attrs;
939         struct odp_header *odp_header = info->userhdr;
940         struct tbl_node *flow_node;
941         struct sw_flow_key key;
942         struct sw_flow *flow;
943         struct sk_buff *reply;
944         struct datapath *dp;
945         struct tbl *table;
946         u32 hash;
947         int error;
948
949         /* Extract key. */
950         error = -EINVAL;
951         if (!a[ODP_FLOW_ATTR_KEY])
952                 goto error;
953         error = flow_from_nlattrs(&key, a[ODP_FLOW_ATTR_KEY]);
954         if (error)
955                 goto error;
956
957         /* Validate actions. */
958         if (a[ODP_FLOW_ATTR_ACTIONS]) {
959                 error = validate_actions(a[ODP_FLOW_ATTR_ACTIONS]);
960                 if (error)
961                         goto error;
962         } else if (info->genlhdr->cmd == ODP_FLOW_CMD_NEW) {
963                 error = -EINVAL;
964                 goto error;
965         }
966
967         dp = get_dp(odp_header->dp_ifindex);
968         error = -ENODEV;
969         if (!dp)
970                 goto error;
971
972         hash = flow_hash(&key);
973         table = get_table_protected(dp);
974         flow_node = tbl_lookup(table, &key, hash, flow_cmp);
975         if (!flow_node) {
976                 struct sw_flow_actions *acts;
977
978                 /* Bail out if we're not allowed to create a new flow. */
979                 error = -ENOENT;
980                 if (info->genlhdr->cmd == ODP_FLOW_CMD_SET)
981                         goto error;
982
983                 /* Expand table, if necessary, to make room. */
984                 if (tbl_count(table) >= tbl_n_buckets(table)) {
985                         error = expand_table(dp);
986                         if (error)
987                                 goto error;
988                         table = get_table_protected(dp);
989                 }
990
991                 /* Allocate flow. */
992                 flow = flow_alloc();
993                 if (IS_ERR(flow)) {
994                         error = PTR_ERR(flow);
995                         goto error;
996                 }
997                 flow->key = key;
998                 clear_stats(flow);
999
1000                 /* Obtain actions. */
1001                 acts = flow_actions_alloc(a[ODP_FLOW_ATTR_ACTIONS]);
1002                 error = PTR_ERR(acts);
1003                 if (IS_ERR(acts))
1004                         goto error_free_flow;
1005                 rcu_assign_pointer(flow->sf_acts, acts);
1006
1007                 /* Put flow in bucket. */
1008                 error = tbl_insert(table, &flow->tbl_node, hash);
1009                 if (error)
1010                         goto error_free_flow;
1011
1012                 reply = odp_flow_cmd_build_info(flow, dp, info->snd_pid,
1013                                                 info->snd_seq, ODP_FLOW_CMD_NEW);
1014         } else {
1015                 /* We found a matching flow. */
1016                 struct sw_flow_actions *old_acts;
1017
1018                 /* Bail out if we're not allowed to modify an existing flow.
1019                  * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
1020                  * because Generic Netlink treats the latter as a dump
1021                  * request.  We also accept NLM_F_EXCL in case that bug ever
1022                  * gets fixed.
1023                  */
1024                 error = -EEXIST;
1025                 if (info->genlhdr->cmd == ODP_FLOW_CMD_NEW &&
1026                     info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL))
1027                         goto error;
1028
1029                 /* Update actions. */
1030                 flow = flow_cast(flow_node);
1031                 old_acts = rcu_dereference_protected(flow->sf_acts,
1032                                                      lockdep_genl_is_held());
1033                 if (a[ODP_FLOW_ATTR_ACTIONS] &&
1034                     (old_acts->actions_len != nla_len(a[ODP_FLOW_ATTR_ACTIONS]) ||
1035                      memcmp(old_acts->actions, nla_data(a[ODP_FLOW_ATTR_ACTIONS]),
1036                             old_acts->actions_len))) {
1037                         struct sw_flow_actions *new_acts;
1038
1039                         new_acts = flow_actions_alloc(a[ODP_FLOW_ATTR_ACTIONS]);
1040                         error = PTR_ERR(new_acts);
1041                         if (IS_ERR(new_acts))
1042                                 goto error;
1043
1044                         rcu_assign_pointer(flow->sf_acts, new_acts);
1045                         flow_deferred_free_acts(old_acts);
1046                 }
1047
1048                 reply = odp_flow_cmd_build_info(flow, dp, info->snd_pid,
1049                                                 info->snd_seq, ODP_FLOW_CMD_NEW);
1050
1051                 /* Clear stats. */
1052                 if (a[ODP_FLOW_ATTR_CLEAR]) {
1053                         spin_lock_bh(&flow->lock);
1054                         clear_stats(flow);
1055                         spin_unlock_bh(&flow->lock);
1056                 }
1057         }
1058
1059         if (!IS_ERR(reply))
1060                 genl_notify(reply, genl_info_net(info), info->snd_pid,
1061                             dp_flow_multicast_group.id, info->nlhdr, GFP_KERNEL);
1062         else
1063                 netlink_set_err(INIT_NET_GENL_SOCK, 0,
1064                                 dp_flow_multicast_group.id, PTR_ERR(reply));
1065         return 0;
1066
1067 error_free_flow:
1068         flow_put(flow);
1069 error:
1070         return error;
1071 }
1072
1073 static int odp_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1074 {
1075         struct nlattr **a = info->attrs;
1076         struct odp_header *odp_header = info->userhdr;
1077         struct sw_flow_key key;
1078         struct tbl_node *flow_node;
1079         struct sk_buff *reply;
1080         struct sw_flow *flow;
1081         struct datapath *dp;
1082         struct tbl *table;
1083         int err;
1084
1085         if (!a[ODP_FLOW_ATTR_KEY])
1086                 return -EINVAL;
1087         err = flow_from_nlattrs(&key, a[ODP_FLOW_ATTR_KEY]);
1088         if (err)
1089                 return err;
1090
1091         dp = get_dp(odp_header->dp_ifindex);
1092         if (!dp)
1093                 return -ENODEV;
1094
1095         table = get_table_protected(dp);
1096         flow_node = tbl_lookup(table, &key, flow_hash(&key), flow_cmp);
1097         if (!flow_node)
1098                 return -ENOENT;
1099
1100         flow = flow_cast(flow_node);
1101         reply = odp_flow_cmd_build_info(flow, dp, info->snd_pid, info->snd_seq, ODP_FLOW_CMD_NEW);
1102         if (IS_ERR(reply))
1103                 return PTR_ERR(reply);
1104
1105         return genlmsg_reply(reply, info);
1106 }
1107
1108 static int odp_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1109 {
1110         struct nlattr **a = info->attrs;
1111         struct odp_header *odp_header = info->userhdr;
1112         struct sw_flow_key key;
1113         struct tbl_node *flow_node;
1114         struct sk_buff *reply;
1115         struct sw_flow *flow;
1116         struct datapath *dp;
1117         struct tbl *table;
1118         int err;
1119
1120         if (!a[ODP_FLOW_ATTR_KEY])
1121                 return flush_flows(odp_header->dp_ifindex);
1122         err = flow_from_nlattrs(&key, a[ODP_FLOW_ATTR_KEY]);
1123         if (err)
1124                 return err;
1125
1126         dp = get_dp(odp_header->dp_ifindex);
1127         if (!dp)
1128                 return -ENODEV;
1129
1130         table = get_table_protected(dp);
1131         flow_node = tbl_lookup(table, &key, flow_hash(&key), flow_cmp);
1132         if (!flow_node)
1133                 return -ENOENT;
1134         flow = flow_cast(flow_node);
1135
1136         reply = odp_flow_cmd_alloc_info(flow);
1137         if (!reply)
1138                 return -ENOMEM;
1139
1140         err = tbl_remove(table, flow_node);
1141         if (err) {
1142                 kfree_skb(reply);
1143                 return err;
1144         }
1145
1146         err = odp_flow_cmd_fill_info(flow, dp, reply, info->snd_pid,
1147                                      info->snd_seq, 0, ODP_FLOW_CMD_DEL);
1148         BUG_ON(err < 0);
1149
1150         flow_deferred_free(flow);
1151
1152         genl_notify(reply, genl_info_net(info), info->snd_pid,
1153                     dp_flow_multicast_group.id, info->nlhdr, GFP_KERNEL);
1154         return 0;
1155 }
1156
1157 static int odp_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1158 {
1159         struct odp_header *odp_header = genlmsg_data(nlmsg_data(cb->nlh));
1160         struct datapath *dp;
1161
1162         dp = get_dp(odp_header->dp_ifindex);
1163         if (!dp)
1164                 return -ENODEV;
1165
1166         for (;;) {
1167                 struct tbl_node *flow_node;
1168                 struct sw_flow *flow;
1169                 u32 bucket, obj;
1170
1171                 bucket = cb->args[0];
1172                 obj = cb->args[1];
1173                 flow_node = tbl_next(get_table_protected(dp), &bucket, &obj);
1174                 if (!flow_node)
1175                         break;
1176
1177                 flow = flow_cast(flow_node);
1178                 if (odp_flow_cmd_fill_info(flow, dp, skb, NETLINK_CB(cb->skb).pid,
1179                                            cb->nlh->nlmsg_seq, NLM_F_MULTI,
1180                                            ODP_FLOW_CMD_NEW) < 0)
1181                         break;
1182
1183                 cb->args[0] = bucket;
1184                 cb->args[1] = obj;
1185         }
1186         return skb->len;
1187 }
1188
1189 static struct genl_ops dp_flow_genl_ops[] = {
1190         { .cmd = ODP_FLOW_CMD_NEW,
1191           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1192           .policy = flow_policy,
1193           .doit = odp_flow_cmd_new_or_set
1194         },
1195         { .cmd = ODP_FLOW_CMD_DEL,
1196           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1197           .policy = flow_policy,
1198           .doit = odp_flow_cmd_del
1199         },
1200         { .cmd = ODP_FLOW_CMD_GET,
1201           .flags = 0,               /* OK for unprivileged users. */
1202           .policy = flow_policy,
1203           .doit = odp_flow_cmd_get,
1204           .dumpit = odp_flow_cmd_dump
1205         },
1206         { .cmd = ODP_FLOW_CMD_SET,
1207           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1208           .policy = flow_policy,
1209           .doit = odp_flow_cmd_new_or_set,
1210         },
1211 };
1212
1213 static const struct nla_policy datapath_policy[ODP_DP_ATTR_MAX + 1] = {
1214 #ifdef HAVE_NLA_NUL_STRING
1215         [ODP_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1216 #endif
1217         [ODP_DP_ATTR_IPV4_FRAGS] = { .type = NLA_U32 },
1218         [ODP_DP_ATTR_SAMPLING] = { .type = NLA_U32 },
1219 };
1220
1221 static struct genl_family dp_datapath_genl_family = {
1222         .id = GENL_ID_GENERATE,
1223         .hdrsize = sizeof(struct odp_header),
1224         .name = ODP_DATAPATH_FAMILY,
1225         .version = 1,
1226         .maxattr = ODP_DP_ATTR_MAX
1227 };
1228
1229 static struct genl_multicast_group dp_datapath_multicast_group = {
1230         .name = ODP_DATAPATH_MCGROUP
1231 };
1232
1233 static int odp_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
1234                                 u32 pid, u32 seq, u32 flags, u8 cmd)
1235 {
1236         struct odp_header *odp_header;
1237         struct nlattr *nla;
1238         int err;
1239
1240         odp_header = genlmsg_put(skb, pid, seq, &dp_datapath_genl_family,
1241                                    flags, cmd);
1242         if (!odp_header)
1243                 goto error;
1244
1245         odp_header->dp_ifindex = dp->dp_ifindex;
1246
1247         rcu_read_lock();
1248         err = nla_put_string(skb, ODP_DP_ATTR_NAME, dp_name(dp));
1249         rcu_read_unlock();
1250         if (err)
1251                 goto nla_put_failure;
1252
1253         nla = nla_reserve(skb, ODP_DP_ATTR_STATS, sizeof(struct odp_stats));
1254         if (!nla)
1255                 goto nla_put_failure;
1256         get_dp_stats(dp, nla_data(nla));
1257
1258         NLA_PUT_U32(skb, ODP_DP_ATTR_IPV4_FRAGS,
1259                     dp->drop_frags ? ODP_DP_FRAG_DROP : ODP_DP_FRAG_ZERO);
1260
1261         if (dp->sflow_probability)
1262                 NLA_PUT_U32(skb, ODP_DP_ATTR_SAMPLING, dp->sflow_probability);
1263
1264         nla = nla_nest_start(skb, ODP_DP_ATTR_MCGROUPS);
1265         if (!nla)
1266                 goto nla_put_failure;
1267         NLA_PUT_U32(skb, ODP_PACKET_CMD_MISS, packet_mc_group(dp, ODP_PACKET_CMD_MISS));
1268         NLA_PUT_U32(skb, ODP_PACKET_CMD_ACTION, packet_mc_group(dp, ODP_PACKET_CMD_ACTION));
1269         NLA_PUT_U32(skb, ODP_PACKET_CMD_SAMPLE, packet_mc_group(dp, ODP_PACKET_CMD_SAMPLE));
1270         nla_nest_end(skb, nla);
1271
1272         return genlmsg_end(skb, odp_header);
1273
1274 nla_put_failure:
1275         genlmsg_cancel(skb, odp_header);
1276 error:
1277         return -EMSGSIZE;
1278 }
1279
1280 static struct sk_buff *odp_dp_cmd_build_info(struct datapath *dp, u32 pid,
1281                                              u32 seq, u8 cmd)
1282 {
1283         struct sk_buff *skb;
1284         int retval;
1285
1286         skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1287         if (!skb)
1288                 return ERR_PTR(-ENOMEM);
1289
1290         retval = odp_dp_cmd_fill_info(dp, skb, pid, seq, 0, cmd);
1291         if (retval < 0) {
1292                 kfree_skb(skb);
1293                 return ERR_PTR(retval);
1294         }
1295         return skb;
1296 }
1297
1298 static int odp_dp_cmd_validate(struct nlattr *a[ODP_DP_ATTR_MAX + 1])
1299 {
1300         if (a[ODP_DP_ATTR_IPV4_FRAGS]) {
1301                 u32 frags = nla_get_u32(a[ODP_DP_ATTR_IPV4_FRAGS]);
1302
1303                 if (frags != ODP_DP_FRAG_ZERO && frags != ODP_DP_FRAG_DROP)
1304                         return -EINVAL;
1305         }
1306
1307         return VERIFY_NUL_STRING(a[ODP_DP_ATTR_NAME], IFNAMSIZ - 1);
1308 }
1309
1310 /* Called with genl_mutex and optionally with RTNL lock also. */
1311 static struct datapath *lookup_datapath(struct odp_header *odp_header, struct nlattr *a[ODP_DP_ATTR_MAX + 1])
1312 {
1313         struct datapath *dp;
1314
1315         if (!a[ODP_DP_ATTR_NAME])
1316                 dp = get_dp(odp_header->dp_ifindex);
1317         else {
1318                 struct vport *vport;
1319
1320                 rcu_read_lock();
1321                 vport = vport_locate(nla_data(a[ODP_DP_ATTR_NAME]));
1322                 dp = vport && vport->port_no == ODPP_LOCAL ? vport->dp : NULL;
1323                 rcu_read_unlock();
1324         }
1325         return dp ? dp : ERR_PTR(-ENODEV);
1326 }
1327
1328 /* Called with genl_mutex. */
1329 static void change_datapath(struct datapath *dp, struct nlattr *a[ODP_DP_ATTR_MAX + 1])
1330 {
1331         if (a[ODP_DP_ATTR_IPV4_FRAGS])
1332                 dp->drop_frags = nla_get_u32(a[ODP_DP_ATTR_IPV4_FRAGS]) == ODP_DP_FRAG_DROP;
1333         if (a[ODP_DP_ATTR_SAMPLING])
1334                 dp->sflow_probability = nla_get_u32(a[ODP_DP_ATTR_SAMPLING]);
1335 }
1336
1337 static int odp_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1338 {
1339         struct nlattr **a = info->attrs;
1340         struct vport_parms parms;
1341         struct sk_buff *reply;
1342         struct datapath *dp;
1343         struct vport *vport;
1344         int err;
1345
1346         err = -EINVAL;
1347         if (!a[ODP_DP_ATTR_NAME])
1348                 goto err;
1349
1350         err = odp_dp_cmd_validate(a);
1351         if (err)
1352                 goto err;
1353
1354         rtnl_lock();
1355         err = -ENODEV;
1356         if (!try_module_get(THIS_MODULE))
1357                 goto err_unlock_rtnl;
1358
1359         err = -ENOMEM;
1360         dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1361         if (dp == NULL)
1362                 goto err_put_module;
1363         INIT_LIST_HEAD(&dp->port_list);
1364
1365         /* Initialize kobject for bridge.  This will be added as
1366          * /sys/class/net/<devname>/brif later, if sysfs is enabled. */
1367         dp->ifobj.kset = NULL;
1368         kobject_init(&dp->ifobj, &dp_ktype);
1369
1370         /* Allocate table. */
1371         err = -ENOMEM;
1372         rcu_assign_pointer(dp->table, tbl_create(TBL_MIN_BUCKETS));
1373         if (!dp->table)
1374                 goto err_free_dp;
1375
1376         /* Set up our datapath device. */
1377         parms.name = nla_data(a[ODP_DP_ATTR_NAME]);
1378         parms.type = ODP_VPORT_TYPE_INTERNAL;
1379         parms.options = NULL;
1380         parms.dp = dp;
1381         parms.port_no = ODPP_LOCAL;
1382         vport = new_vport(&parms);
1383         if (IS_ERR(vport)) {
1384                 err = PTR_ERR(vport);
1385                 if (err == -EBUSY)
1386                         err = -EEXIST;
1387
1388                 goto err_destroy_table;
1389         }
1390         dp->dp_ifindex = vport_get_ifindex(vport);
1391
1392         dp->drop_frags = 0;
1393         dp->stats_percpu = alloc_percpu(struct dp_stats_percpu);
1394         if (!dp->stats_percpu) {
1395                 err = -ENOMEM;
1396                 goto err_destroy_local_port;
1397         }
1398
1399         change_datapath(dp, a);
1400
1401         reply = odp_dp_cmd_build_info(dp, info->snd_pid, info->snd_seq, ODP_DP_CMD_NEW);
1402         err = PTR_ERR(reply);
1403         if (IS_ERR(reply))
1404                 goto err_destroy_local_port;
1405
1406         list_add_tail(&dp->list_node, &dps);
1407         dp_sysfs_add_dp(dp);
1408
1409         rtnl_unlock();
1410
1411         genl_notify(reply, genl_info_net(info), info->snd_pid,
1412                     dp_datapath_multicast_group.id, info->nlhdr, GFP_KERNEL);
1413         return 0;
1414
1415 err_destroy_local_port:
1416         dp_detach_port(get_vport_protected(dp, ODPP_LOCAL));
1417 err_destroy_table:
1418         tbl_destroy(get_table_protected(dp), NULL);
1419 err_free_dp:
1420         kfree(dp);
1421 err_put_module:
1422         module_put(THIS_MODULE);
1423 err_unlock_rtnl:
1424         rtnl_unlock();
1425 err:
1426         return err;
1427 }
1428
1429 static int odp_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1430 {
1431         struct vport *vport, *next_vport;
1432         struct sk_buff *reply;
1433         struct datapath *dp;
1434         int err;
1435
1436         err = odp_dp_cmd_validate(info->attrs);
1437         if (err)
1438                 goto exit;
1439
1440         rtnl_lock();
1441         dp = lookup_datapath(info->userhdr, info->attrs);
1442         err = PTR_ERR(dp);
1443         if (IS_ERR(dp))
1444                 goto exit_unlock;
1445
1446         reply = odp_dp_cmd_build_info(dp, info->snd_pid, info->snd_seq, ODP_DP_CMD_DEL);
1447         err = PTR_ERR(reply);
1448         if (IS_ERR(reply))
1449                 goto exit_unlock;
1450
1451         list_for_each_entry_safe (vport, next_vport, &dp->port_list, node)
1452                 if (vport->port_no != ODPP_LOCAL)
1453                         dp_detach_port(vport);
1454
1455         dp_sysfs_del_dp(dp);
1456         list_del(&dp->list_node);
1457         dp_detach_port(get_vport_protected(dp, ODPP_LOCAL));
1458
1459         call_rcu(&dp->rcu, destroy_dp_rcu);
1460         module_put(THIS_MODULE);
1461
1462         genl_notify(reply, genl_info_net(info), info->snd_pid,
1463                     dp_datapath_multicast_group.id, info->nlhdr, GFP_KERNEL);
1464         err = 0;
1465
1466 exit_unlock:
1467         rtnl_unlock();
1468 exit:
1469         return err;
1470 }
1471
1472 static int odp_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1473 {
1474         struct sk_buff *reply;
1475         struct datapath *dp;
1476         int err;
1477
1478         err = odp_dp_cmd_validate(info->attrs);
1479         if (err)
1480                 return err;
1481
1482         dp = lookup_datapath(info->userhdr, info->attrs);
1483         if (IS_ERR(dp))
1484                 return PTR_ERR(dp);
1485
1486         change_datapath(dp, info->attrs);
1487
1488         reply = odp_dp_cmd_build_info(dp, info->snd_pid, info->snd_seq, ODP_DP_CMD_NEW);
1489         if (IS_ERR(reply)) {
1490                 err = PTR_ERR(reply);
1491                 netlink_set_err(INIT_NET_GENL_SOCK, 0,
1492                                 dp_datapath_multicast_group.id, err);
1493                 return 0;
1494         }
1495
1496         genl_notify(reply, genl_info_net(info), info->snd_pid,
1497                     dp_datapath_multicast_group.id, info->nlhdr, GFP_KERNEL);
1498         return 0;
1499 }
1500
1501 static int odp_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1502 {
1503         struct sk_buff *reply;
1504         struct datapath *dp;
1505         int err;
1506
1507         err = odp_dp_cmd_validate(info->attrs);
1508         if (err)
1509                 return err;
1510
1511         dp = lookup_datapath(info->userhdr, info->attrs);
1512         if (IS_ERR(dp))
1513                 return PTR_ERR(dp);
1514
1515         reply = odp_dp_cmd_build_info(dp, info->snd_pid, info->snd_seq, ODP_DP_CMD_NEW);
1516         if (IS_ERR(reply))
1517                 return PTR_ERR(reply);
1518
1519         return genlmsg_reply(reply, info);
1520 }
1521
1522 static int odp_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1523 {
1524         struct datapath *dp;
1525         int skip = cb->args[0];
1526         int i = 0;
1527
1528         list_for_each_entry (dp, &dps, list_node) {
1529                 if (i < skip)
1530                         continue;
1531                 if (odp_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).pid,
1532                                          cb->nlh->nlmsg_seq, NLM_F_MULTI,
1533                                          ODP_DP_CMD_NEW) < 0)
1534                         break;
1535                 i++;
1536         }
1537
1538         cb->args[0] = i;
1539
1540         return skb->len;
1541 }
1542
1543 static struct genl_ops dp_datapath_genl_ops[] = {
1544         { .cmd = ODP_DP_CMD_NEW,
1545           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1546           .policy = datapath_policy,
1547           .doit = odp_dp_cmd_new
1548         },
1549         { .cmd = ODP_DP_CMD_DEL,
1550           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1551           .policy = datapath_policy,
1552           .doit = odp_dp_cmd_del
1553         },
1554         { .cmd = ODP_DP_CMD_GET,
1555           .flags = 0,               /* OK for unprivileged users. */
1556           .policy = datapath_policy,
1557           .doit = odp_dp_cmd_get,
1558           .dumpit = odp_dp_cmd_dump
1559         },
1560         { .cmd = ODP_DP_CMD_SET,
1561           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1562           .policy = datapath_policy,
1563           .doit = odp_dp_cmd_set,
1564         },
1565 };
1566
1567 static const struct nla_policy vport_policy[ODP_VPORT_ATTR_MAX + 1] = {
1568 #ifdef HAVE_NLA_NUL_STRING
1569         [ODP_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1570         [ODP_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
1571         [ODP_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
1572         [ODP_VPORT_ATTR_STATS] = { .len = sizeof(struct rtnl_link_stats64) },
1573         [ODP_VPORT_ATTR_ADDRESS] = { .len = ETH_ALEN },
1574 #else
1575         [ODP_VPORT_ATTR_STATS] = { .minlen = sizeof(struct rtnl_link_stats64) },
1576         [ODP_VPORT_ATTR_ADDRESS] = { .minlen = ETH_ALEN },
1577 #endif
1578         [ODP_VPORT_ATTR_MTU] = { .type = NLA_U32 },
1579         [ODP_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
1580 };
1581
1582 static struct genl_family dp_vport_genl_family = {
1583         .id = GENL_ID_GENERATE,
1584         .hdrsize = sizeof(struct odp_header),
1585         .name = ODP_VPORT_FAMILY,
1586         .version = 1,
1587         .maxattr = ODP_VPORT_ATTR_MAX
1588 };
1589
1590 static struct genl_multicast_group dp_vport_multicast_group = {
1591         .name = ODP_VPORT_MCGROUP
1592 };
1593
1594 /* Called with RTNL lock or RCU read lock. */
1595 static int odp_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
1596                                    u32 pid, u32 seq, u32 flags, u8 cmd)
1597 {
1598         struct odp_header *odp_header;
1599         struct nlattr *nla;
1600         int ifindex, iflink;
1601         int mtu;
1602         int err;
1603
1604         odp_header = genlmsg_put(skb, pid, seq, &dp_vport_genl_family,
1605                                  flags, cmd);
1606         if (!odp_header)
1607                 return -EMSGSIZE;
1608
1609         odp_header->dp_ifindex = vport->dp->dp_ifindex;
1610
1611         NLA_PUT_U32(skb, ODP_VPORT_ATTR_PORT_NO, vport->port_no);
1612         NLA_PUT_U32(skb, ODP_VPORT_ATTR_TYPE, vport_get_type(vport));
1613         NLA_PUT_STRING(skb, ODP_VPORT_ATTR_NAME, vport_get_name(vport));
1614
1615         nla = nla_reserve(skb, ODP_VPORT_ATTR_STATS, sizeof(struct rtnl_link_stats64));
1616         if (!nla)
1617                 goto nla_put_failure;
1618         if (vport_get_stats(vport, nla_data(nla)))
1619                 __skb_trim(skb, skb->len - nla->nla_len);
1620
1621         NLA_PUT(skb, ODP_VPORT_ATTR_ADDRESS, ETH_ALEN, vport_get_addr(vport));
1622
1623         mtu = vport_get_mtu(vport);
1624         if (mtu)
1625                 NLA_PUT_U32(skb, ODP_VPORT_ATTR_MTU, mtu);
1626
1627         err = vport_get_options(vport, skb);
1628         if (err == -EMSGSIZE)
1629                 goto error;
1630
1631         ifindex = vport_get_ifindex(vport);
1632         if (ifindex > 0)
1633                 NLA_PUT_U32(skb, ODP_VPORT_ATTR_IFINDEX, ifindex);
1634
1635         iflink = vport_get_iflink(vport);
1636         if (iflink > 0)
1637                 NLA_PUT_U32(skb, ODP_VPORT_ATTR_IFLINK, iflink);
1638
1639         return genlmsg_end(skb, odp_header);
1640
1641 nla_put_failure:
1642         err = -EMSGSIZE;
1643 error:
1644         genlmsg_cancel(skb, odp_header);
1645         return err;
1646 }
1647
1648 /* Called with RTNL lock or RCU read lock. */
1649 static struct sk_buff *odp_vport_cmd_build_info(struct vport *vport, u32 pid,
1650                                                 u32 seq, u8 cmd)
1651 {
1652         struct sk_buff *skb;
1653         int retval;
1654
1655         skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1656         if (!skb)
1657                 return ERR_PTR(-ENOMEM);
1658
1659         retval = odp_vport_cmd_fill_info(vport, skb, pid, seq, 0, cmd);
1660         if (retval < 0) {
1661                 kfree_skb(skb);
1662                 return ERR_PTR(retval);
1663         }
1664         return skb;
1665 }
1666
1667 static int odp_vport_cmd_validate(struct nlattr *a[ODP_VPORT_ATTR_MAX + 1])
1668 {
1669         return VERIFY_NUL_STRING(a[ODP_VPORT_ATTR_NAME], IFNAMSIZ - 1);
1670 }
1671
1672 /* Called with RTNL lock or RCU read lock. */
1673 static struct vport *lookup_vport(struct odp_header *odp_header,
1674                                   struct nlattr *a[ODP_VPORT_ATTR_MAX + 1])
1675 {
1676         struct datapath *dp;
1677         struct vport *vport;
1678
1679         if (a[ODP_VPORT_ATTR_NAME]) {
1680                 vport = vport_locate(nla_data(a[ODP_VPORT_ATTR_NAME]));
1681                 if (!vport)
1682                         return ERR_PTR(-ENODEV);
1683                 return vport;
1684         } else if (a[ODP_VPORT_ATTR_PORT_NO]) {
1685                 u32 port_no = nla_get_u32(a[ODP_VPORT_ATTR_PORT_NO]);
1686
1687                 if (port_no >= DP_MAX_PORTS)
1688                         return ERR_PTR(-EFBIG);
1689
1690                 dp = get_dp(odp_header->dp_ifindex);
1691                 if (!dp)
1692                         return ERR_PTR(-ENODEV);
1693
1694                 vport = get_vport_protected(dp, port_no);
1695                 if (!vport)
1696                         return ERR_PTR(-ENOENT);
1697                 return vport;
1698         } else
1699                 return ERR_PTR(-EINVAL);
1700 }
1701
1702 /* Called with RTNL lock. */
1703 static int change_vport(struct vport *vport, struct nlattr *a[ODP_VPORT_ATTR_MAX + 1])
1704 {
1705         int err = 0;
1706         if (a[ODP_VPORT_ATTR_STATS])
1707                 err = vport_set_stats(vport, nla_data(a[ODP_VPORT_ATTR_STATS]));
1708         if (!err && a[ODP_VPORT_ATTR_ADDRESS])
1709                 err = vport_set_addr(vport, nla_data(a[ODP_VPORT_ATTR_ADDRESS]));
1710         if (!err && a[ODP_VPORT_ATTR_MTU])
1711                 err = vport_set_mtu(vport, nla_get_u32(a[ODP_VPORT_ATTR_MTU]));
1712         return err;
1713 }
1714
1715 static int odp_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
1716 {
1717         struct nlattr **a = info->attrs;
1718         struct odp_header *odp_header = info->userhdr;
1719         struct vport_parms parms;
1720         struct sk_buff *reply;
1721         struct vport *vport;
1722         struct datapath *dp;
1723         u32 port_no;
1724         int err;
1725
1726         err = -EINVAL;
1727         if (!a[ODP_VPORT_ATTR_NAME] || !a[ODP_VPORT_ATTR_TYPE])
1728                 goto exit;
1729
1730         err = odp_vport_cmd_validate(a);
1731         if (err)
1732                 goto exit;
1733
1734         rtnl_lock();
1735         dp = get_dp(odp_header->dp_ifindex);
1736         err = -ENODEV;
1737         if (!dp)
1738                 goto exit_unlock;
1739
1740         if (a[ODP_VPORT_ATTR_PORT_NO]) {
1741                 port_no = nla_get_u32(a[ODP_VPORT_ATTR_PORT_NO]);
1742
1743                 err = -EFBIG;
1744                 if (port_no >= DP_MAX_PORTS)
1745                         goto exit_unlock;
1746
1747                 vport = get_vport_protected(dp, port_no);
1748                 err = -EBUSY;
1749                 if (vport)
1750                         goto exit_unlock;
1751         } else {
1752                 for (port_no = 1; ; port_no++) {
1753                         if (port_no >= DP_MAX_PORTS) {
1754                                 err = -EFBIG;
1755                                 goto exit_unlock;
1756                         }
1757                         vport = get_vport_protected(dp, port_no);
1758                         if (!vport)
1759                                 break;
1760                 }
1761         }
1762
1763         parms.name = nla_data(a[ODP_VPORT_ATTR_NAME]);
1764         parms.type = nla_get_u32(a[ODP_VPORT_ATTR_TYPE]);
1765         parms.options = a[ODP_VPORT_ATTR_OPTIONS];
1766         parms.dp = dp;
1767         parms.port_no = port_no;
1768
1769         vport = new_vport(&parms);
1770         err = PTR_ERR(vport);
1771         if (IS_ERR(vport))
1772                 goto exit_unlock;
1773
1774         set_internal_devs_mtu(dp);
1775         dp_sysfs_add_if(vport);
1776
1777         err = change_vport(vport, a);
1778         if (!err) {
1779                 reply = odp_vport_cmd_build_info(vport, info->snd_pid,
1780                                                  info->snd_seq, ODP_VPORT_CMD_NEW);
1781                 if (IS_ERR(reply))
1782                         err = PTR_ERR(reply);
1783         }
1784         if (err) {
1785                 dp_detach_port(vport);
1786                 goto exit_unlock;
1787         }
1788         genl_notify(reply, genl_info_net(info), info->snd_pid,
1789                     dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
1790
1791
1792 exit_unlock:
1793         rtnl_unlock();
1794 exit:
1795         return err;
1796 }
1797
1798 static int odp_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
1799 {
1800         struct nlattr **a = info->attrs;
1801         struct sk_buff *reply;
1802         struct vport *vport;
1803         int err;
1804
1805         err = odp_vport_cmd_validate(a);
1806         if (err)
1807                 goto exit;
1808
1809         rtnl_lock();
1810         vport = lookup_vport(info->userhdr, a);
1811         err = PTR_ERR(vport);
1812         if (IS_ERR(vport))
1813                 goto exit_unlock;
1814
1815         err = 0;
1816         if (a[ODP_VPORT_ATTR_OPTIONS])
1817                 err = vport_set_options(vport, a[ODP_VPORT_ATTR_OPTIONS]);
1818         if (!err)
1819                 err = change_vport(vport, a);
1820
1821         reply = odp_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
1822                                          ODP_VPORT_CMD_NEW);
1823         if (IS_ERR(reply)) {
1824                 err = PTR_ERR(reply);
1825                 netlink_set_err(INIT_NET_GENL_SOCK, 0,
1826                                 dp_vport_multicast_group.id, err);
1827                 return 0;
1828         }
1829
1830         genl_notify(reply, genl_info_net(info), info->snd_pid,
1831                     dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
1832
1833 exit_unlock:
1834         rtnl_unlock();
1835 exit:
1836         return err;
1837 }
1838
1839 static int odp_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
1840 {
1841         struct nlattr **a = info->attrs;
1842         struct sk_buff *reply;
1843         struct vport *vport;
1844         int err;
1845
1846         err = odp_vport_cmd_validate(a);
1847         if (err)
1848                 goto exit;
1849
1850         rtnl_lock();
1851         vport = lookup_vport(info->userhdr, a);
1852         err = PTR_ERR(vport);
1853         if (IS_ERR(vport))
1854                 goto exit_unlock;
1855
1856         if (vport->port_no == ODPP_LOCAL) {
1857                 err = -EINVAL;
1858                 goto exit_unlock;
1859         }
1860
1861         reply = odp_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
1862                                          ODP_VPORT_CMD_DEL);
1863         err = PTR_ERR(reply);
1864         if (IS_ERR(reply))
1865                 goto exit_unlock;
1866
1867         err = dp_detach_port(vport);
1868
1869         genl_notify(reply, genl_info_net(info), info->snd_pid,
1870                     dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
1871
1872 exit_unlock:
1873         rtnl_unlock();
1874 exit:
1875         return err;
1876 }
1877
1878 static int odp_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
1879 {
1880         struct nlattr **a = info->attrs;
1881         struct odp_header *odp_header = info->userhdr;
1882         struct sk_buff *reply;
1883         struct vport *vport;
1884         int err;
1885
1886         err = odp_vport_cmd_validate(a);
1887         if (err)
1888                 goto exit;
1889
1890         rcu_read_lock();
1891         vport = lookup_vport(odp_header, a);
1892         err = PTR_ERR(vport);
1893         if (IS_ERR(vport))
1894                 goto exit_unlock;
1895
1896         reply = odp_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
1897                                          ODP_VPORT_CMD_NEW);
1898         err = PTR_ERR(reply);
1899         if (IS_ERR(reply))
1900                 goto exit_unlock;
1901
1902         err = genlmsg_reply(reply, info);
1903
1904 exit_unlock:
1905         rcu_read_unlock();
1906 exit:
1907         return err;
1908 }
1909
1910 static int odp_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1911 {
1912         struct odp_header *odp_header = genlmsg_data(nlmsg_data(cb->nlh));
1913         struct datapath *dp;
1914         u32 port_no;
1915         int retval;
1916
1917         dp = get_dp(odp_header->dp_ifindex);
1918         if (!dp)
1919                 return -ENODEV;
1920
1921         rcu_read_lock();
1922         for (port_no = cb->args[0]; port_no < DP_MAX_PORTS; port_no++) {
1923                 struct vport *vport;
1924
1925                 vport = get_vport_protected(dp, port_no);
1926                 if (!vport)
1927                         continue;
1928
1929                 if (odp_vport_cmd_fill_info(vport, skb, NETLINK_CB(cb->skb).pid,
1930                                             cb->nlh->nlmsg_seq, NLM_F_MULTI,
1931                                             ODP_VPORT_CMD_NEW) < 0)
1932                         break;
1933         }
1934         rcu_read_unlock();
1935
1936         cb->args[0] = port_no;
1937         retval = skb->len;
1938
1939         return retval;
1940 }
1941
1942 static struct genl_ops dp_vport_genl_ops[] = {
1943         { .cmd = ODP_VPORT_CMD_NEW,
1944           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1945           .policy = vport_policy,
1946           .doit = odp_vport_cmd_new
1947         },
1948         { .cmd = ODP_VPORT_CMD_DEL,
1949           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1950           .policy = vport_policy,
1951           .doit = odp_vport_cmd_del
1952         },
1953         { .cmd = ODP_VPORT_CMD_GET,
1954           .flags = 0,               /* OK for unprivileged users. */
1955           .policy = vport_policy,
1956           .doit = odp_vport_cmd_get,
1957           .dumpit = odp_vport_cmd_dump
1958         },
1959         { .cmd = ODP_VPORT_CMD_SET,
1960           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1961           .policy = vport_policy,
1962           .doit = odp_vport_cmd_set,
1963         },
1964 };
1965
1966 struct genl_family_and_ops {
1967         struct genl_family *family;
1968         struct genl_ops *ops;
1969         int n_ops;
1970         struct genl_multicast_group *group;
1971 };
1972
1973 static const struct genl_family_and_ops dp_genl_families[] = {
1974         { &dp_datapath_genl_family,
1975           dp_datapath_genl_ops, ARRAY_SIZE(dp_datapath_genl_ops),
1976           &dp_datapath_multicast_group },
1977         { &dp_vport_genl_family,
1978           dp_vport_genl_ops, ARRAY_SIZE(dp_vport_genl_ops),
1979           &dp_vport_multicast_group },
1980         { &dp_flow_genl_family,
1981           dp_flow_genl_ops, ARRAY_SIZE(dp_flow_genl_ops),
1982           &dp_flow_multicast_group },
1983         { &dp_packet_genl_family,
1984           dp_packet_genl_ops, ARRAY_SIZE(dp_packet_genl_ops),
1985           NULL },
1986 };
1987
1988 static void dp_unregister_genl(int n_families)
1989 {
1990         int i;
1991
1992         for (i = 0; i < n_families; i++) {
1993                 genl_unregister_family(dp_genl_families[i].family);
1994         }
1995 }
1996
1997 static int dp_register_genl(void)
1998 {
1999         int n_registered;
2000         int err;
2001         int i;
2002
2003         n_registered = 0;
2004         for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
2005                 const struct genl_family_and_ops *f = &dp_genl_families[i];
2006
2007                 err = genl_register_family_with_ops(f->family, f->ops,
2008                                                     f->n_ops);
2009                 if (err)
2010                         goto error;
2011                 n_registered++;
2012
2013                 if (f->group) {
2014                         err = genl_register_mc_group(f->family, f->group);
2015                         if (err)
2016                                 goto error;
2017                 }
2018         }
2019
2020         err = packet_register_mc_groups();
2021         if (err)
2022                 goto error;
2023         return 0;
2024
2025 error:
2026         dp_unregister_genl(n_registered);
2027         return err;
2028 }
2029
2030 static int __init dp_init(void)
2031 {
2032         struct sk_buff *dummy_skb;
2033         int err;
2034
2035         BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > sizeof(dummy_skb->cb));
2036
2037         printk("Open vSwitch %s, built "__DATE__" "__TIME__"\n", VERSION BUILDNR);
2038
2039         err = flow_init();
2040         if (err)
2041                 goto error;
2042
2043         err = vport_init();
2044         if (err)
2045                 goto error_flow_exit;
2046
2047         err = register_netdevice_notifier(&dp_device_notifier);
2048         if (err)
2049                 goto error_vport_exit;
2050
2051         err = dp_register_genl();
2052         if (err < 0)
2053                 goto error_unreg_notifier;
2054
2055         return 0;
2056
2057 error_unreg_notifier:
2058         unregister_netdevice_notifier(&dp_device_notifier);
2059 error_vport_exit:
2060         vport_exit();
2061 error_flow_exit:
2062         flow_exit();
2063 error:
2064         return err;
2065 }
2066
2067 static void dp_cleanup(void)
2068 {
2069         rcu_barrier();
2070         dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
2071         unregister_netdevice_notifier(&dp_device_notifier);
2072         vport_exit();
2073         flow_exit();
2074 }
2075
2076 module_init(dp_init);
2077 module_exit(dp_cleanup);
2078
2079 MODULE_DESCRIPTION("Open vSwitch switching datapath");
2080 MODULE_LICENSE("GPL");