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