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