datapath: Call genlmsg_end in queue_userspace_packet
[sliver-openvswitch.git] / datapath / datapath.c
1 /*
2  * Copyright (c) 2007-2012 Nicira, Inc.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of version 2 of the GNU General Public
6  * License as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16  * 02110-1301, USA
17  */
18
19 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20
21 #include <linux/init.h>
22 #include <linux/module.h>
23 #include <linux/if_arp.h>
24 #include <linux/if_vlan.h>
25 #include <linux/in.h>
26 #include <linux/ip.h>
27 #include <linux/jhash.h>
28 #include <linux/delay.h>
29 #include <linux/time.h>
30 #include <linux/etherdevice.h>
31 #include <linux/genetlink.h>
32 #include <linux/kernel.h>
33 #include <linux/kthread.h>
34 #include <linux/mutex.h>
35 #include <linux/percpu.h>
36 #include <linux/rcupdate.h>
37 #include <linux/tcp.h>
38 #include <linux/udp.h>
39 #include <linux/version.h>
40 #include <linux/ethtool.h>
41 #include <linux/wait.h>
42 #include <asm/div64.h>
43 #include <linux/highmem.h>
44 #include <linux/netfilter_bridge.h>
45 #include <linux/netfilter_ipv4.h>
46 #include <linux/inetdevice.h>
47 #include <linux/list.h>
48 #include <linux/openvswitch.h>
49 #include <linux/rculist.h>
50 #include <linux/dmi.h>
51 #include <net/genetlink.h>
52 #include <net/net_namespace.h>
53 #include <net/netns/generic.h>
54
55 #include "checksum.h"
56 #include "datapath.h"
57 #include "flow.h"
58 #include "genl_exec.h"
59 #include "vlan.h"
60 #include "tunnel.h"
61 #include "vport-internal_dev.h"
62
63 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18) || \
64     LINUX_VERSION_CODE >= KERNEL_VERSION(3,9,0)
65 #error Kernels before 2.6.18 or after 3.8 are not supported by this version of Open vSwitch.
66 #endif
67
68 #define REHASH_FLOW_INTERVAL (10 * 60 * HZ)
69 static void rehash_flow_table(struct work_struct *work);
70 static DECLARE_DELAYED_WORK(rehash_flow_wq, rehash_flow_table);
71
72 int ovs_net_id __read_mostly;
73
74 /**
75  * DOC: Locking:
76  *
77  * Writes to device state (add/remove datapath, port, set operations on vports,
78  * etc.) are protected by RTNL.
79  *
80  * Writes to other state (flow table modifications, set miscellaneous datapath
81  * parameters, etc.) are protected by genl_mutex.  The RTNL lock nests inside
82  * genl_mutex.
83  *
84  * Reads are protected by RCU.
85  *
86  * There are a few special cases (mostly stats) that have their own
87  * synchronization but they nest under all of above and don't interact with
88  * each other.
89  */
90
91 static struct vport *new_vport(const struct vport_parms *);
92 static int queue_gso_packets(struct net *, int dp_ifindex, struct sk_buff *,
93                              const struct dp_upcall_info *);
94 static int queue_userspace_packet(struct net *, int dp_ifindex,
95                                   struct sk_buff *,
96                                   const struct dp_upcall_info *);
97
98 /* Must be called with rcu_read_lock, genl_mutex, or RTNL lock. */
99 static struct datapath *get_dp(struct net *net, int dp_ifindex)
100 {
101         struct datapath *dp = NULL;
102         struct net_device *dev;
103
104         rcu_read_lock();
105         dev = dev_get_by_index_rcu(net, dp_ifindex);
106         if (dev) {
107                 struct vport *vport = ovs_internal_dev_get_vport(dev);
108                 if (vport)
109                         dp = vport->dp;
110         }
111         rcu_read_unlock();
112
113         return dp;
114 }
115
116 /* Must be called with rcu_read_lock or RTNL lock. */
117 const char *ovs_dp_name(const struct datapath *dp)
118 {
119         struct vport *vport = ovs_vport_rtnl_rcu(dp, OVSP_LOCAL);
120         return vport->ops->get_name(vport);
121 }
122
123 static int get_dpifindex(struct datapath *dp)
124 {
125         struct vport *local;
126         int ifindex;
127
128         rcu_read_lock();
129
130         local = ovs_vport_rcu(dp, OVSP_LOCAL);
131         if (local)
132                 ifindex = local->ops->get_ifindex(local);
133         else
134                 ifindex = 0;
135
136         rcu_read_unlock();
137
138         return ifindex;
139 }
140
141 static void destroy_dp_rcu(struct rcu_head *rcu)
142 {
143         struct datapath *dp = container_of(rcu, struct datapath, rcu);
144
145         ovs_flow_tbl_destroy((__force struct flow_table *)dp->table);
146         free_percpu(dp->stats_percpu);
147         release_net(ovs_dp_get_net(dp));
148         kfree(dp->ports);
149         kfree(dp);
150 }
151
152 static struct hlist_head *vport_hash_bucket(const struct datapath *dp,
153                                             u16 port_no)
154 {
155         return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)];
156 }
157
158 struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no)
159 {
160         struct vport *vport;
161         struct hlist_node *n;
162         struct hlist_head *head;
163
164         head = vport_hash_bucket(dp, port_no);
165         hlist_for_each_entry_rcu(vport, n, head, dp_hash_node) {
166                 if (vport->port_no == port_no)
167                         return vport;
168         }
169         return NULL;
170 }
171
172 /* Called with RTNL lock and genl_lock. */
173 static struct vport *new_vport(const struct vport_parms *parms)
174 {
175         struct vport *vport;
176
177         vport = ovs_vport_add(parms);
178         if (!IS_ERR(vport)) {
179                 struct datapath *dp = parms->dp;
180                 struct hlist_head *head = vport_hash_bucket(dp, vport->port_no);
181
182                 hlist_add_head_rcu(&vport->dp_hash_node, head);
183         }
184         return vport;
185 }
186
187 /* Called with RTNL lock. */
188 void ovs_dp_detach_port(struct vport *p)
189 {
190         ASSERT_RTNL();
191
192         /* First drop references to device. */
193         hlist_del_rcu(&p->dp_hash_node);
194
195         /* Then destroy it. */
196         ovs_vport_del(p);
197 }
198
199 /* Must be called with rcu_read_lock. */
200 void ovs_dp_process_received_packet(struct vport *p, struct sk_buff *skb)
201 {
202         struct datapath *dp = p->dp;
203         struct sw_flow *flow;
204         struct dp_stats_percpu *stats;
205         u64 *stats_counter;
206         int error;
207
208         stats = this_cpu_ptr(dp->stats_percpu);
209
210         if (!OVS_CB(skb)->flow) {
211                 struct sw_flow_key key;
212                 int key_len;
213
214                 /* Extract flow from 'skb' into 'key'. */
215                 error = ovs_flow_extract(skb, p->port_no, &key, &key_len);
216                 if (unlikely(error)) {
217                         kfree_skb(skb);
218                         return;
219                 }
220
221                 /* Look up flow. */
222                 flow = ovs_flow_tbl_lookup(rcu_dereference(dp->table),
223                                            &key, key_len);
224                 if (unlikely(!flow)) {
225                         struct dp_upcall_info upcall;
226
227                         upcall.cmd = OVS_PACKET_CMD_MISS;
228                         upcall.key = &key;
229                         upcall.userdata = NULL;
230                         upcall.portid = p->upcall_portid;
231                         ovs_dp_upcall(dp, skb, &upcall);
232                         consume_skb(skb);
233                         stats_counter = &stats->n_missed;
234                         goto out;
235                 }
236
237                 OVS_CB(skb)->flow = flow;
238         }
239
240         stats_counter = &stats->n_hit;
241         ovs_flow_used(OVS_CB(skb)->flow, skb);
242         ovs_execute_actions(dp, skb);
243
244 out:
245         /* Update datapath statistics. */
246         u64_stats_update_begin(&stats->sync);
247         (*stats_counter)++;
248         u64_stats_update_end(&stats->sync);
249 }
250
251 static struct genl_family dp_packet_genl_family = {
252         .id = GENL_ID_GENERATE,
253         .hdrsize = sizeof(struct ovs_header),
254         .name = OVS_PACKET_FAMILY,
255         .version = OVS_PACKET_VERSION,
256         .maxattr = OVS_PACKET_ATTR_MAX,
257          SET_NETNSOK
258 };
259
260 int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
261                   const struct dp_upcall_info *upcall_info)
262 {
263         struct dp_stats_percpu *stats;
264         int dp_ifindex;
265         int err;
266
267         if (upcall_info->portid == 0) {
268                 err = -ENOTCONN;
269                 goto err;
270         }
271
272         dp_ifindex = get_dpifindex(dp);
273         if (!dp_ifindex) {
274                 err = -ENODEV;
275                 goto err;
276         }
277
278         forward_ip_summed(skb, true);
279
280         if (!skb_is_gso(skb))
281                 err = queue_userspace_packet(ovs_dp_get_net(dp), dp_ifindex, skb, upcall_info);
282         else
283                 err = queue_gso_packets(ovs_dp_get_net(dp), dp_ifindex, skb, upcall_info);
284         if (err)
285                 goto err;
286
287         return 0;
288
289 err:
290         stats = this_cpu_ptr(dp->stats_percpu);
291
292         u64_stats_update_begin(&stats->sync);
293         stats->n_lost++;
294         u64_stats_update_end(&stats->sync);
295
296         return err;
297 }
298
299 static int queue_gso_packets(struct net *net, int dp_ifindex,
300                              struct sk_buff *skb,
301                              const struct dp_upcall_info *upcall_info)
302 {
303         unsigned short gso_type = skb_shinfo(skb)->gso_type;
304         struct dp_upcall_info later_info;
305         struct sw_flow_key later_key;
306         struct sk_buff *segs, *nskb;
307         int err;
308
309         segs = __skb_gso_segment(skb, NETIF_F_SG | NETIF_F_HW_CSUM, false);
310         if (IS_ERR(segs))
311                 return PTR_ERR(segs);
312
313         /* Queue all of the segments. */
314         skb = segs;
315         do {
316                 err = queue_userspace_packet(net, dp_ifindex, skb, upcall_info);
317                 if (err)
318                         break;
319
320                 if (skb == segs && gso_type & SKB_GSO_UDP) {
321                         /* The initial flow key extracted by ovs_flow_extract()
322                          * in this case is for a first fragment, so we need to
323                          * properly mark later fragments.
324                          */
325                         later_key = *upcall_info->key;
326                         later_key.ip.frag = OVS_FRAG_TYPE_LATER;
327
328                         later_info = *upcall_info;
329                         later_info.key = &later_key;
330                         upcall_info = &later_info;
331                 }
332         } while ((skb = skb->next));
333
334         /* Free all of the segments. */
335         skb = segs;
336         do {
337                 nskb = skb->next;
338                 if (err)
339                         kfree_skb(skb);
340                 else
341                         consume_skb(skb);
342         } while ((skb = nskb));
343         return err;
344 }
345
346 static int queue_userspace_packet(struct net *net, int dp_ifindex,
347                                   struct sk_buff *skb,
348                                   const struct dp_upcall_info *upcall_info)
349 {
350         struct ovs_header *upcall;
351         struct sk_buff *nskb = NULL;
352         struct sk_buff *user_skb; /* to be queued to userspace */
353         struct nlattr *nla;
354         unsigned int len;
355         int err;
356
357         if (vlan_tx_tag_present(skb)) {
358                 nskb = skb_clone(skb, GFP_ATOMIC);
359                 if (!nskb)
360                         return -ENOMEM;
361                 
362                 err = vlan_deaccel_tag(nskb);
363                 if (err)
364                         return err;
365
366                 skb = nskb;
367         }
368
369         if (nla_attr_size(skb->len) > USHRT_MAX) {
370                 err = -EFBIG;
371                 goto out;
372         }
373
374         len = sizeof(struct ovs_header);
375         len += nla_total_size(skb->len);
376         len += nla_total_size(FLOW_BUFSIZE);
377         if (upcall_info->cmd == OVS_PACKET_CMD_ACTION)
378                 len += nla_total_size(8);
379
380         user_skb = genlmsg_new(len, GFP_ATOMIC);
381         if (!user_skb) {
382                 err = -ENOMEM;
383                 goto out;
384         }
385
386         upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
387                              0, upcall_info->cmd);
388         upcall->dp_ifindex = dp_ifindex;
389
390         nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
391         ovs_flow_to_nlattrs(upcall_info->key, user_skb);
392         nla_nest_end(user_skb, nla);
393
394         if (upcall_info->userdata)
395                 nla_put_u64(user_skb, OVS_PACKET_ATTR_USERDATA,
396                             nla_get_u64(upcall_info->userdata));
397
398         nla = __nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, skb->len);
399
400         skb_copy_and_csum_dev(skb, nla_data(nla));
401
402         genlmsg_end(user_skb, upcall);
403         err = genlmsg_unicast(net, user_skb, upcall_info->portid);
404
405 out:
406         kfree_skb(nskb);
407         return err;
408 }
409
410 /* Called with genl_mutex. */
411 static int flush_flows(struct datapath *dp)
412 {
413         struct flow_table *old_table;
414         struct flow_table *new_table;
415
416         old_table = genl_dereference(dp->table);
417         new_table = ovs_flow_tbl_alloc(TBL_MIN_BUCKETS);
418         if (!new_table)
419                 return -ENOMEM;
420
421         rcu_assign_pointer(dp->table, new_table);
422
423         ovs_flow_tbl_deferred_destroy(old_table);
424         return 0;
425 }
426
427 static struct nlattr *reserve_sfa_size(struct sw_flow_actions **sfa, int attr_len)
428 {
429
430         struct sw_flow_actions *acts;
431         int new_acts_size;
432         int req_size = NLA_ALIGN(attr_len);
433         int next_offset = offsetof(struct sw_flow_actions, actions) +
434                                         (*sfa)->actions_len;
435
436         if (req_size <= (ksize(*sfa) - next_offset))
437                 goto out;
438
439         new_acts_size = ksize(*sfa) * 2;
440
441         if (new_acts_size > MAX_ACTIONS_BUFSIZE) {
442                 if ((MAX_ACTIONS_BUFSIZE - next_offset) < req_size)
443                         return ERR_PTR(-EMSGSIZE);
444                 new_acts_size = MAX_ACTIONS_BUFSIZE;
445         }
446
447         acts = ovs_flow_actions_alloc(new_acts_size);
448         if (IS_ERR(acts))
449                 return (void *)acts;
450
451         memcpy(acts->actions, (*sfa)->actions, (*sfa)->actions_len);
452         acts->actions_len = (*sfa)->actions_len;
453         kfree(*sfa);
454         *sfa = acts;
455
456 out:
457         (*sfa)->actions_len += req_size;
458         return  (struct nlattr *) ((unsigned char *)(*sfa) + next_offset);
459 }
460
461 static int add_action(struct sw_flow_actions **sfa, int attrtype, void *data, int len)
462 {
463         struct nlattr *a;
464
465         a = reserve_sfa_size(sfa, nla_attr_size(len));
466         if (IS_ERR(a))
467                 return PTR_ERR(a);
468
469         a->nla_type = attrtype;
470         a->nla_len = nla_attr_size(len);
471
472         if (data)
473                 memcpy(nla_data(a), data, len);
474         memset((unsigned char *) a + a->nla_len, 0, nla_padlen(len));
475
476         return 0;
477 }
478
479 static inline int add_nested_action_start(struct sw_flow_actions **sfa, int attrtype)
480 {
481         int used = (*sfa)->actions_len;
482         int err;
483
484         err = add_action(sfa, attrtype, NULL, 0);
485         if (err)
486                 return err;
487
488         return used;
489 }
490
491 static inline void add_nested_action_end(struct sw_flow_actions *sfa, int st_offset)
492 {
493         struct nlattr *a = (struct nlattr *) ((unsigned char *)sfa->actions + st_offset);
494
495         a->nla_len = sfa->actions_len - st_offset;
496 }
497
498 static int validate_and_copy_actions(const struct nlattr *attr,
499                                 const struct sw_flow_key *key, int depth,
500                                 struct sw_flow_actions **sfa);
501
502 static int validate_and_copy_sample(const struct nlattr *attr,
503                            const struct sw_flow_key *key, int depth,
504                            struct sw_flow_actions **sfa)
505 {
506         const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
507         const struct nlattr *probability, *actions;
508         const struct nlattr *a;
509         int rem, start, err, st_acts;
510
511         memset(attrs, 0, sizeof(attrs));
512         nla_for_each_nested(a, attr, rem) {
513                 int type = nla_type(a);
514                 if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
515                         return -EINVAL;
516                 attrs[type] = a;
517         }
518         if (rem)
519                 return -EINVAL;
520
521         probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
522         if (!probability || nla_len(probability) != sizeof(u32))
523                 return -EINVAL;
524
525         actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
526         if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
527                 return -EINVAL;
528
529         /* validation done, copy sample action. */
530         start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SAMPLE);
531         if (start < 0)
532                 return start;
533         err = add_action(sfa, OVS_SAMPLE_ATTR_PROBABILITY, nla_data(probability), sizeof(u32));
534         if (err)
535                 return err;
536         st_acts = add_nested_action_start(sfa, OVS_SAMPLE_ATTR_ACTIONS);
537         if (st_acts < 0)
538                 return st_acts;
539
540         err = validate_and_copy_actions(actions, key, depth + 1, sfa);
541         if (err)
542                 return err;
543
544         add_nested_action_end(*sfa, st_acts);
545         add_nested_action_end(*sfa, start);
546
547         return 0;
548 }
549
550 static int validate_tp_port(const struct sw_flow_key *flow_key)
551 {
552         if (flow_key->eth.type == htons(ETH_P_IP)) {
553                 if (flow_key->ipv4.tp.src || flow_key->ipv4.tp.dst)
554                         return 0;
555         } else if (flow_key->eth.type == htons(ETH_P_IPV6)) {
556                 if (flow_key->ipv6.tp.src || flow_key->ipv6.tp.dst)
557                         return 0;
558         }
559
560         return -EINVAL;
561 }
562
563 static int validate_and_copy_set_tun(const struct nlattr *attr,
564                                      struct sw_flow_actions **sfa)
565 {
566         struct ovs_key_ipv4_tunnel tun_key;
567         int err, start;
568
569         err = ipv4_tun_from_nlattr(nla_data(attr), &tun_key);
570         if (err)
571                 return err;
572
573         start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SET);
574         if (start < 0)
575                 return start;
576
577         err = add_action(sfa, OVS_KEY_ATTR_IPV4_TUNNEL, &tun_key, sizeof(tun_key));
578         add_nested_action_end(*sfa, start);
579
580         return err;
581 }
582
583 static int validate_set(const struct nlattr *a,
584                         const struct sw_flow_key *flow_key,
585                         struct sw_flow_actions **sfa,
586                         bool *set_tun)
587 {
588         const struct nlattr *ovs_key = nla_data(a);
589         int key_type = nla_type(ovs_key);
590
591         /* There can be only one key in a action */
592         if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
593                 return -EINVAL;
594
595         if (key_type > OVS_KEY_ATTR_MAX ||
596             (ovs_key_lens[key_type] != nla_len(ovs_key) &&
597              ovs_key_lens[key_type] != -1))
598                 return -EINVAL;
599
600         switch (key_type) {
601         const struct ovs_key_ipv4 *ipv4_key;
602         const struct ovs_key_ipv6 *ipv6_key;
603         int err;
604
605         case OVS_KEY_ATTR_PRIORITY:
606         case OVS_KEY_ATTR_TUN_ID:
607         case OVS_KEY_ATTR_ETHERNET:
608                 break;
609
610         case OVS_KEY_ATTR_SKB_MARK:
611 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20) && !defined(CONFIG_NETFILTER)
612                 if (nla_get_u32(ovs_key) != 0)
613                         return -EINVAL;
614 #endif
615                 break;
616
617         case OVS_KEY_ATTR_TUNNEL:
618                 *set_tun = true;
619                 err = validate_and_copy_set_tun(a, sfa);
620                 if (err)
621                         return err;
622                 break;
623
624         case OVS_KEY_ATTR_IPV4:
625                 if (flow_key->eth.type != htons(ETH_P_IP))
626                         return -EINVAL;
627
628                 if (!flow_key->ip.proto)
629                         return -EINVAL;
630
631                 ipv4_key = nla_data(ovs_key);
632                 if (ipv4_key->ipv4_proto != flow_key->ip.proto)
633                         return -EINVAL;
634
635                 if (ipv4_key->ipv4_frag != flow_key->ip.frag)
636                         return -EINVAL;
637
638                 break;
639
640         case OVS_KEY_ATTR_IPV6:
641                 if (flow_key->eth.type != htons(ETH_P_IPV6))
642                         return -EINVAL;
643
644                 if (!flow_key->ip.proto)
645                         return -EINVAL;
646
647                 ipv6_key = nla_data(ovs_key);
648                 if (ipv6_key->ipv6_proto != flow_key->ip.proto)
649                         return -EINVAL;
650
651                 if (ipv6_key->ipv6_frag != flow_key->ip.frag)
652                         return -EINVAL;
653
654                 if (ntohl(ipv6_key->ipv6_label) & 0xFFF00000)
655                         return -EINVAL;
656
657                 break;
658
659         case OVS_KEY_ATTR_TCP:
660                 if (flow_key->ip.proto != IPPROTO_TCP)
661                         return -EINVAL;
662
663                 return validate_tp_port(flow_key);
664
665         case OVS_KEY_ATTR_UDP:
666                 if (flow_key->ip.proto != IPPROTO_UDP)
667                         return -EINVAL;
668
669                 return validate_tp_port(flow_key);
670
671         default:
672                 return -EINVAL;
673         }
674
675         return 0;
676 }
677
678 static int validate_userspace(const struct nlattr *attr)
679 {
680         static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] =   {
681                 [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
682                 [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_U64 },
683         };
684         struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
685         int error;
686
687         error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX,
688                                  attr, userspace_policy);
689         if (error)
690                 return error;
691
692         if (!a[OVS_USERSPACE_ATTR_PID] ||
693             !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
694                 return -EINVAL;
695
696         return 0;
697 }
698
699 static int copy_action(const struct nlattr *from,
700                       struct sw_flow_actions **sfa)
701 {
702         int totlen = NLA_ALIGN(from->nla_len);
703         struct nlattr *to;
704
705         to = reserve_sfa_size(sfa, from->nla_len);
706         if (IS_ERR(to))
707                 return PTR_ERR(to);
708
709         memcpy(to, from, totlen);
710         return 0;
711 }
712
713 static int validate_and_copy_actions(const struct nlattr *attr,
714                                 const struct sw_flow_key *key,
715                                 int depth,
716                                 struct sw_flow_actions **sfa)
717 {
718         const struct nlattr *a;
719         int rem, err;
720
721         if (depth >= SAMPLE_ACTION_DEPTH)
722                 return -EOVERFLOW;
723
724         nla_for_each_nested(a, attr, rem) {
725                 /* Expected argument lengths, (u32)-1 for variable length. */
726                 static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
727                         [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
728                         [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
729                         [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
730                         [OVS_ACTION_ATTR_POP_VLAN] = 0,
731                         [OVS_ACTION_ATTR_SET] = (u32)-1,
732                         [OVS_ACTION_ATTR_SAMPLE] = (u32)-1
733                 };
734                 const struct ovs_action_push_vlan *vlan;
735                 int type = nla_type(a);
736                 bool skip_copy;
737
738                 if (type > OVS_ACTION_ATTR_MAX ||
739                     (action_lens[type] != nla_len(a) &&
740                      action_lens[type] != (u32)-1))
741                         return -EINVAL;
742
743                 skip_copy = false;
744                 switch (type) {
745                 case OVS_ACTION_ATTR_UNSPEC:
746                         return -EINVAL;
747
748                 case OVS_ACTION_ATTR_USERSPACE:
749                         err = validate_userspace(a);
750                         if (err)
751                                 return err;
752                         break;
753
754                 case OVS_ACTION_ATTR_OUTPUT:
755                         if (nla_get_u32(a) >= DP_MAX_PORTS)
756                                 return -EINVAL;
757                         break;
758
759
760                 case OVS_ACTION_ATTR_POP_VLAN:
761                         break;
762
763                 case OVS_ACTION_ATTR_PUSH_VLAN:
764                         vlan = nla_data(a);
765                         if (vlan->vlan_tpid != htons(ETH_P_8021Q))
766                                 return -EINVAL;
767                         if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
768                                 return -EINVAL;
769                         break;
770
771                 case OVS_ACTION_ATTR_SET:
772                         err = validate_set(a, key, sfa, &skip_copy);
773                         if (err)
774                                 return err;
775                         break;
776
777                 case OVS_ACTION_ATTR_SAMPLE:
778                         err = validate_and_copy_sample(a, key, depth, sfa);
779                         if (err)
780                                 return err;
781                         skip_copy = true;
782                         break;
783
784                 default:
785                         return -EINVAL;
786                 }
787                 if (!skip_copy) {
788                         err = copy_action(a, sfa);
789                         if (err)
790                                 return err;
791                 }
792         }
793
794         if (rem > 0)
795                 return -EINVAL;
796
797         return 0;
798 }
799
800 static void clear_stats(struct sw_flow *flow)
801 {
802         flow->used = 0;
803         flow->tcp_flags = 0;
804         flow->packet_count = 0;
805         flow->byte_count = 0;
806 }
807
808 static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
809 {
810         struct ovs_header *ovs_header = info->userhdr;
811         struct nlattr **a = info->attrs;
812         struct sw_flow_actions *acts;
813         struct sk_buff *packet;
814         struct sw_flow *flow;
815         struct datapath *dp;
816         struct ethhdr *eth;
817         int len;
818         int err;
819         int key_len;
820
821         err = -EINVAL;
822         if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
823             !a[OVS_PACKET_ATTR_ACTIONS] ||
824             nla_len(a[OVS_PACKET_ATTR_PACKET]) < ETH_HLEN)
825                 goto err;
826
827         len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
828         packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
829         err = -ENOMEM;
830         if (!packet)
831                 goto err;
832         skb_reserve(packet, NET_IP_ALIGN);
833
834         memcpy(__skb_put(packet, len), nla_data(a[OVS_PACKET_ATTR_PACKET]), len);
835
836         skb_reset_mac_header(packet);
837         eth = eth_hdr(packet);
838
839         /* Normally, setting the skb 'protocol' field would be handled by a
840          * call to eth_type_trans(), but it assumes there's a sending
841          * device, which we may not have. */
842         if (ntohs(eth->h_proto) >= 1536)
843                 packet->protocol = eth->h_proto;
844         else
845                 packet->protocol = htons(ETH_P_802_2);
846
847         /* Build an sw_flow for sending this packet. */
848         flow = ovs_flow_alloc();
849         err = PTR_ERR(flow);
850         if (IS_ERR(flow))
851                 goto err_kfree_skb;
852
853         err = ovs_flow_extract(packet, -1, &flow->key, &key_len);
854         if (err)
855                 goto err_flow_free;
856
857         err = ovs_flow_metadata_from_nlattrs(flow, key_len, a[OVS_PACKET_ATTR_KEY]);
858         if (err)
859                 goto err_flow_free;
860         acts = ovs_flow_actions_alloc(nla_len(a[OVS_PACKET_ATTR_ACTIONS]));
861         err = PTR_ERR(acts);
862         if (IS_ERR(acts))
863                 goto err_flow_free;
864
865         err = validate_and_copy_actions(a[OVS_PACKET_ATTR_ACTIONS], &flow->key, 0, &acts);
866         rcu_assign_pointer(flow->sf_acts, acts);
867         if (err)
868                 goto err_flow_free;
869
870         OVS_CB(packet)->flow = flow;
871         packet->priority = flow->key.phy.priority;
872         skb_set_mark(packet, flow->key.phy.skb_mark);
873
874         rcu_read_lock();
875         dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
876         err = -ENODEV;
877         if (!dp)
878                 goto err_unlock;
879
880         local_bh_disable();
881         err = ovs_execute_actions(dp, packet);
882         local_bh_enable();
883         rcu_read_unlock();
884
885         ovs_flow_free(flow);
886         return err;
887
888 err_unlock:
889         rcu_read_unlock();
890 err_flow_free:
891         ovs_flow_free(flow);
892 err_kfree_skb:
893         kfree_skb(packet);
894 err:
895         return err;
896 }
897
898 static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
899         [OVS_PACKET_ATTR_PACKET] = { .type = NLA_UNSPEC },
900         [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
901         [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
902 };
903
904 static struct genl_ops dp_packet_genl_ops[] = {
905         { .cmd = OVS_PACKET_CMD_EXECUTE,
906           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
907           .policy = packet_policy,
908           .doit = ovs_packet_cmd_execute
909         }
910 };
911
912 static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats)
913 {
914         int i;
915         struct flow_table *table = genl_dereference(dp->table);
916
917         stats->n_flows = ovs_flow_tbl_count(table);
918
919         stats->n_hit = stats->n_missed = stats->n_lost = 0;
920         for_each_possible_cpu(i) {
921                 const struct dp_stats_percpu *percpu_stats;
922                 struct dp_stats_percpu local_stats;
923                 unsigned int start;
924
925                 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
926
927                 do {
928                         start = u64_stats_fetch_begin_bh(&percpu_stats->sync);
929                         local_stats = *percpu_stats;
930                 } while (u64_stats_fetch_retry_bh(&percpu_stats->sync, start));
931
932                 stats->n_hit += local_stats.n_hit;
933                 stats->n_missed += local_stats.n_missed;
934                 stats->n_lost += local_stats.n_lost;
935         }
936 }
937
938 static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
939         [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
940         [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
941         [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
942 };
943
944 static struct genl_family dp_flow_genl_family = {
945         .id = GENL_ID_GENERATE,
946         .hdrsize = sizeof(struct ovs_header),
947         .name = OVS_FLOW_FAMILY,
948         .version = OVS_FLOW_VERSION,
949         .maxattr = OVS_FLOW_ATTR_MAX,
950          SET_NETNSOK
951 };
952
953 static struct genl_multicast_group ovs_dp_flow_multicast_group = {
954         .name = OVS_FLOW_MCGROUP
955 };
956
957 static int actions_to_attr(const struct nlattr *attr, int len, struct sk_buff *skb);
958 static int sample_action_to_attr(const struct nlattr *attr, struct sk_buff *skb)
959 {
960         const struct nlattr *a;
961         struct nlattr *start;
962         int err = 0, rem;
963
964         start = nla_nest_start(skb, OVS_ACTION_ATTR_SAMPLE);
965         if (!start)
966                 return -EMSGSIZE;
967
968         nla_for_each_nested(a, attr, rem) {
969                 int type = nla_type(a);
970                 struct nlattr *st_sample;
971
972                 switch (type) {
973                 case OVS_SAMPLE_ATTR_PROBABILITY:
974                         if (nla_put(skb, OVS_SAMPLE_ATTR_PROBABILITY, sizeof(u32), nla_data(a)))
975                                 return -EMSGSIZE;
976                         break;
977                 case OVS_SAMPLE_ATTR_ACTIONS:
978                         st_sample = nla_nest_start(skb, OVS_SAMPLE_ATTR_ACTIONS);
979                         if (!st_sample)
980                                 return -EMSGSIZE;
981                         err = actions_to_attr(nla_data(a), nla_len(a), skb);
982                         if (err)
983                                 return err;
984                         nla_nest_end(skb, st_sample);
985                         break;
986                 }
987         }
988
989         nla_nest_end(skb, start);
990         return err;
991 }
992
993 static int set_action_to_attr(const struct nlattr *a, struct sk_buff *skb)
994 {
995         const struct nlattr *ovs_key = nla_data(a);
996         int key_type = nla_type(ovs_key);
997         struct nlattr *start;
998         int err;
999
1000         switch (key_type) {
1001         case OVS_KEY_ATTR_IPV4_TUNNEL:
1002                 start = nla_nest_start(skb, OVS_ACTION_ATTR_SET);
1003                 if (!start)
1004                         return -EMSGSIZE;
1005
1006                 err = ipv4_tun_to_nlattr(skb, nla_data(ovs_key));
1007                 if (err)
1008                         return err;
1009                 nla_nest_end(skb, start);
1010                 break;
1011         default:
1012                 if (nla_put(skb, OVS_ACTION_ATTR_SET, nla_len(a), ovs_key))
1013                         return -EMSGSIZE;
1014                 break;
1015         }
1016
1017         return 0;
1018 }
1019
1020 static int actions_to_attr(const struct nlattr *attr, int len, struct sk_buff *skb)
1021 {
1022         const struct nlattr *a;
1023         int rem, err;
1024
1025         nla_for_each_attr(a, attr, len, rem) {
1026                 int type = nla_type(a);
1027
1028                 switch (type) {
1029                 case OVS_ACTION_ATTR_SET:
1030                         err = set_action_to_attr(a, skb);
1031                         if (err)
1032                                 return err;
1033                         break;
1034
1035                 case OVS_ACTION_ATTR_SAMPLE:
1036                         err = sample_action_to_attr(a, skb);
1037                         if (err)
1038                                 return err;
1039                         break;
1040                 default:
1041                         if (nla_put(skb, type, nla_len(a), nla_data(a)))
1042                                 return -EMSGSIZE;
1043                         break;
1044                 }
1045         }
1046
1047         return 0;
1048 }
1049
1050 /* Called with genl_lock. */
1051 static int ovs_flow_cmd_fill_info(struct sw_flow *flow, struct datapath *dp,
1052                                   struct sk_buff *skb, u32 portid,
1053                                   u32 seq, u32 flags, u8 cmd)
1054 {
1055         const int skb_orig_len = skb->len;
1056         const struct sw_flow_actions *sf_acts;
1057         struct nlattr *start;
1058         struct ovs_flow_stats stats;
1059         struct ovs_header *ovs_header;
1060         struct nlattr *nla;
1061         unsigned long used;
1062         u8 tcp_flags;
1063         int err;
1064
1065         sf_acts = rcu_dereference_protected(flow->sf_acts,
1066                                             lockdep_genl_is_held());
1067
1068         ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family, flags, cmd);
1069         if (!ovs_header)
1070                 return -EMSGSIZE;
1071
1072         ovs_header->dp_ifindex = get_dpifindex(dp);
1073
1074         nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
1075         if (!nla)
1076                 goto nla_put_failure;
1077         err = ovs_flow_to_nlattrs(&flow->key, skb);
1078         if (err)
1079                 goto error;
1080         nla_nest_end(skb, nla);
1081
1082         spin_lock_bh(&flow->lock);
1083         used = flow->used;
1084         stats.n_packets = flow->packet_count;
1085         stats.n_bytes = flow->byte_count;
1086         tcp_flags = flow->tcp_flags;
1087         spin_unlock_bh(&flow->lock);
1088
1089         if (used &&
1090             nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
1091                 goto nla_put_failure;
1092
1093         if (stats.n_packets &&
1094             nla_put(skb, OVS_FLOW_ATTR_STATS,
1095                     sizeof(struct ovs_flow_stats), &stats))
1096                 goto nla_put_failure;
1097
1098         if (tcp_flags &&
1099             nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, tcp_flags))
1100                 goto nla_put_failure;
1101
1102         /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
1103          * this is the first flow to be dumped into 'skb'.  This is unusual for
1104          * Netlink but individual action lists can be longer than
1105          * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
1106          * The userspace caller can always fetch the actions separately if it
1107          * really wants them.  (Most userspace callers in fact don't care.)
1108          *
1109          * This can only fail for dump operations because the skb is always
1110          * properly sized for single flows.
1111          */
1112         start = nla_nest_start(skb, OVS_FLOW_ATTR_ACTIONS);
1113         if (start) {
1114                 err = actions_to_attr(sf_acts->actions, sf_acts->actions_len, skb);
1115                 if (!err)
1116                         nla_nest_end(skb, start);
1117                 else {
1118                         if (skb_orig_len)
1119                                 goto error;
1120
1121                         nla_nest_cancel(skb, start);
1122                 }
1123         } else if (skb_orig_len)
1124                 goto nla_put_failure;
1125
1126         return genlmsg_end(skb, ovs_header);
1127
1128 nla_put_failure:
1129         err = -EMSGSIZE;
1130 error:
1131         genlmsg_cancel(skb, ovs_header);
1132         return err;
1133 }
1134
1135 static struct sk_buff *ovs_flow_cmd_alloc_info(struct sw_flow *flow)
1136 {
1137         const struct sw_flow_actions *sf_acts;
1138         int len;
1139
1140         sf_acts = rcu_dereference_protected(flow->sf_acts,
1141                                             lockdep_genl_is_held());
1142
1143         /* OVS_FLOW_ATTR_KEY */
1144         len = nla_total_size(FLOW_BUFSIZE);
1145         /* OVS_FLOW_ATTR_ACTIONS */
1146         len += nla_total_size(sf_acts->actions_len);
1147         /* OVS_FLOW_ATTR_STATS */
1148         len += nla_total_size(sizeof(struct ovs_flow_stats));
1149         /* OVS_FLOW_ATTR_TCP_FLAGS */
1150         len += nla_total_size(1);
1151         /* OVS_FLOW_ATTR_USED */
1152         len += nla_total_size(8);
1153
1154         len += NLMSG_ALIGN(sizeof(struct ovs_header));
1155
1156         return genlmsg_new(len, GFP_KERNEL);
1157 }
1158
1159 static struct sk_buff *ovs_flow_cmd_build_info(struct sw_flow *flow,
1160                                                struct datapath *dp,
1161                                                u32 portid, u32 seq, u8 cmd)
1162 {
1163         struct sk_buff *skb;
1164         int retval;
1165
1166         skb = ovs_flow_cmd_alloc_info(flow);
1167         if (!skb)
1168                 return ERR_PTR(-ENOMEM);
1169
1170         retval = ovs_flow_cmd_fill_info(flow, dp, skb, portid, seq, 0, cmd);
1171         BUG_ON(retval < 0);
1172         return skb;
1173 }
1174
1175 static int ovs_flow_cmd_new_or_set(struct sk_buff *skb, struct genl_info *info)
1176 {
1177         struct nlattr **a = info->attrs;
1178         struct ovs_header *ovs_header = info->userhdr;
1179         struct sw_flow_key key;
1180         struct sw_flow *flow;
1181         struct sk_buff *reply;
1182         struct datapath *dp;
1183         struct flow_table *table;
1184         struct sw_flow_actions *acts = NULL;
1185         int error;
1186         int key_len;
1187
1188         /* Extract key. */
1189         error = -EINVAL;
1190         if (!a[OVS_FLOW_ATTR_KEY])
1191                 goto error;
1192         error = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
1193         if (error)
1194                 goto error;
1195
1196         /* Validate actions. */
1197         if (a[OVS_FLOW_ATTR_ACTIONS]) {
1198                 acts = ovs_flow_actions_alloc(nla_len(a[OVS_FLOW_ATTR_ACTIONS]));
1199                 error = PTR_ERR(acts);
1200                 if (IS_ERR(acts))
1201                         goto error;
1202
1203                 error = validate_and_copy_actions(a[OVS_FLOW_ATTR_ACTIONS], &key,  0, &acts);
1204                 if (error)
1205                         goto err_kfree;
1206         } else if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW) {
1207                 error = -EINVAL;
1208                 goto error;
1209         }
1210
1211         dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1212         error = -ENODEV;
1213         if (!dp)
1214                 goto err_kfree;
1215
1216         table = genl_dereference(dp->table);
1217         flow = ovs_flow_tbl_lookup(table, &key, key_len);
1218         if (!flow) {
1219                 /* Bail out if we're not allowed to create a new flow. */
1220                 error = -ENOENT;
1221                 if (info->genlhdr->cmd == OVS_FLOW_CMD_SET)
1222                         goto err_kfree;
1223
1224                 /* Expand table, if necessary, to make room. */
1225                 if (ovs_flow_tbl_need_to_expand(table)) {
1226                         struct flow_table *new_table;
1227
1228                         new_table = ovs_flow_tbl_expand(table);
1229                         if (!IS_ERR(new_table)) {
1230                                 rcu_assign_pointer(dp->table, new_table);
1231                                 ovs_flow_tbl_deferred_destroy(table);
1232                                 table = genl_dereference(dp->table);
1233                         }
1234                 }
1235
1236                 /* Allocate flow. */
1237                 flow = ovs_flow_alloc();
1238                 if (IS_ERR(flow)) {
1239                         error = PTR_ERR(flow);
1240                         goto err_kfree;
1241                 }
1242                 clear_stats(flow);
1243
1244                 rcu_assign_pointer(flow->sf_acts, acts);
1245
1246                 /* Put flow in bucket. */
1247                 ovs_flow_tbl_insert(table, flow, &key, key_len);
1248
1249                 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
1250                                                 info->snd_seq,
1251                                                 OVS_FLOW_CMD_NEW);
1252         } else {
1253                 /* We found a matching flow. */
1254                 struct sw_flow_actions *old_acts;
1255
1256                 /* Bail out if we're not allowed to modify an existing flow.
1257                  * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
1258                  * because Generic Netlink treats the latter as a dump
1259                  * request.  We also accept NLM_F_EXCL in case that bug ever
1260                  * gets fixed.
1261                  */
1262                 error = -EEXIST;
1263                 if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW &&
1264                     info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL))
1265                         goto err_kfree;
1266
1267                 /* Update actions. */
1268                 old_acts = rcu_dereference_protected(flow->sf_acts,
1269                                                      lockdep_genl_is_held());
1270                 rcu_assign_pointer(flow->sf_acts, acts);
1271                 ovs_flow_deferred_free_acts(old_acts);
1272
1273                 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
1274                                                info->snd_seq, OVS_FLOW_CMD_NEW);
1275
1276                 /* Clear stats. */
1277                 if (a[OVS_FLOW_ATTR_CLEAR]) {
1278                         spin_lock_bh(&flow->lock);
1279                         clear_stats(flow);
1280                         spin_unlock_bh(&flow->lock);
1281                 }
1282         }
1283
1284         if (!IS_ERR(reply))
1285                 genl_notify(reply, genl_info_net(info), info->snd_portid,
1286                            ovs_dp_flow_multicast_group.id, info->nlhdr,
1287                            GFP_KERNEL);
1288         else
1289                 netlink_set_err(GENL_SOCK(sock_net(skb->sk)), 0,
1290                                 ovs_dp_flow_multicast_group.id, PTR_ERR(reply));
1291         return 0;
1292
1293 err_kfree:
1294         kfree(acts);
1295 error:
1296         return error;
1297 }
1298
1299 static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1300 {
1301         struct nlattr **a = info->attrs;
1302         struct ovs_header *ovs_header = info->userhdr;
1303         struct sw_flow_key key;
1304         struct sk_buff *reply;
1305         struct sw_flow *flow;
1306         struct datapath *dp;
1307         struct flow_table *table;
1308         int err;
1309         int key_len;
1310
1311         if (!a[OVS_FLOW_ATTR_KEY])
1312                 return -EINVAL;
1313         err = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
1314         if (err)
1315                 return err;
1316
1317         dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1318         if (!dp)
1319                 return -ENODEV;
1320
1321         table = genl_dereference(dp->table);
1322         flow = ovs_flow_tbl_lookup(table, &key, key_len);
1323         if (!flow)
1324                 return -ENOENT;
1325
1326         reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
1327                                         info->snd_seq, OVS_FLOW_CMD_NEW);
1328         if (IS_ERR(reply))
1329                 return PTR_ERR(reply);
1330
1331         return genlmsg_reply(reply, info);
1332 }
1333
1334 static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1335 {
1336         struct nlattr **a = info->attrs;
1337         struct ovs_header *ovs_header = info->userhdr;
1338         struct sw_flow_key key;
1339         struct sk_buff *reply;
1340         struct sw_flow *flow;
1341         struct datapath *dp;
1342         struct flow_table *table;
1343         int err;
1344         int key_len;
1345
1346         dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1347         if (!dp)
1348                 return -ENODEV;
1349
1350         if (!a[OVS_FLOW_ATTR_KEY])
1351                 return flush_flows(dp);
1352
1353         err = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
1354         if (err)
1355                 return err;
1356
1357         table = genl_dereference(dp->table);
1358         flow = ovs_flow_tbl_lookup(table, &key, key_len);
1359         if (!flow)
1360                 return -ENOENT;
1361
1362         reply = ovs_flow_cmd_alloc_info(flow);
1363         if (!reply)
1364                 return -ENOMEM;
1365
1366         ovs_flow_tbl_remove(table, flow);
1367
1368         err = ovs_flow_cmd_fill_info(flow, dp, reply, info->snd_portid,
1369                                      info->snd_seq, 0, OVS_FLOW_CMD_DEL);
1370         BUG_ON(err < 0);
1371
1372         ovs_flow_deferred_free(flow);
1373
1374         genl_notify(reply, genl_info_net(info), info->snd_portid,
1375                     ovs_dp_flow_multicast_group.id, info->nlhdr, GFP_KERNEL);
1376         return 0;
1377 }
1378
1379 static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1380 {
1381         struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1382         struct datapath *dp;
1383         struct flow_table *table;
1384
1385         dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1386         if (!dp)
1387                 return -ENODEV;
1388
1389         table = genl_dereference(dp->table);
1390
1391         for (;;) {
1392                 struct sw_flow *flow;
1393                 u32 bucket, obj;
1394
1395                 bucket = cb->args[0];
1396                 obj = cb->args[1];
1397                 flow = ovs_flow_tbl_next(table, &bucket, &obj);
1398                 if (!flow)
1399                         break;
1400
1401                 if (ovs_flow_cmd_fill_info(flow, dp, skb,
1402                                            NETLINK_CB(cb->skb).portid,
1403                                            cb->nlh->nlmsg_seq, NLM_F_MULTI,
1404                                            OVS_FLOW_CMD_NEW) < 0)
1405                         break;
1406
1407                 cb->args[0] = bucket;
1408                 cb->args[1] = obj;
1409         }
1410         return skb->len;
1411 }
1412
1413 static struct genl_ops dp_flow_genl_ops[] = {
1414         { .cmd = OVS_FLOW_CMD_NEW,
1415           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1416           .policy = flow_policy,
1417           .doit = ovs_flow_cmd_new_or_set
1418         },
1419         { .cmd = OVS_FLOW_CMD_DEL,
1420           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1421           .policy = flow_policy,
1422           .doit = ovs_flow_cmd_del
1423         },
1424         { .cmd = OVS_FLOW_CMD_GET,
1425           .flags = 0,               /* OK for unprivileged users. */
1426           .policy = flow_policy,
1427           .doit = ovs_flow_cmd_get,
1428           .dumpit = ovs_flow_cmd_dump
1429         },
1430         { .cmd = OVS_FLOW_CMD_SET,
1431           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1432           .policy = flow_policy,
1433           .doit = ovs_flow_cmd_new_or_set,
1434         },
1435 };
1436
1437 static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1438 #ifdef HAVE_NLA_NUL_STRING
1439         [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1440 #endif
1441         [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1442 };
1443
1444 static struct genl_family dp_datapath_genl_family = {
1445         .id = GENL_ID_GENERATE,
1446         .hdrsize = sizeof(struct ovs_header),
1447         .name = OVS_DATAPATH_FAMILY,
1448         .version = OVS_DATAPATH_VERSION,
1449         .maxattr = OVS_DP_ATTR_MAX,
1450          SET_NETNSOK
1451 };
1452
1453 static struct genl_multicast_group ovs_dp_datapath_multicast_group = {
1454         .name = OVS_DATAPATH_MCGROUP
1455 };
1456
1457 static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
1458                                 u32 portid, u32 seq, u32 flags, u8 cmd)
1459 {
1460         struct ovs_header *ovs_header;
1461         struct ovs_dp_stats dp_stats;
1462         int err;
1463
1464         ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
1465                                    flags, cmd);
1466         if (!ovs_header)
1467                 goto error;
1468
1469         ovs_header->dp_ifindex = get_dpifindex(dp);
1470
1471         rcu_read_lock();
1472         err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
1473         rcu_read_unlock();
1474         if (err)
1475                 goto nla_put_failure;
1476
1477         get_dp_stats(dp, &dp_stats);
1478         if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats), &dp_stats))
1479                 goto nla_put_failure;
1480
1481         return genlmsg_end(skb, ovs_header);
1482
1483 nla_put_failure:
1484         genlmsg_cancel(skb, ovs_header);
1485 error:
1486         return -EMSGSIZE;
1487 }
1488
1489 static struct sk_buff *ovs_dp_cmd_build_info(struct datapath *dp, u32 portid,
1490                                              u32 seq, u8 cmd)
1491 {
1492         struct sk_buff *skb;
1493         int retval;
1494
1495         skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1496         if (!skb)
1497                 return ERR_PTR(-ENOMEM);
1498
1499         retval = ovs_dp_cmd_fill_info(dp, skb, portid, seq, 0, cmd);
1500         if (retval < 0) {
1501                 kfree_skb(skb);
1502                 return ERR_PTR(retval);
1503         }
1504         return skb;
1505 }
1506
1507 static int ovs_dp_cmd_validate(struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1508 {
1509         return CHECK_NUL_STRING(a[OVS_DP_ATTR_NAME], IFNAMSIZ - 1);
1510 }
1511
1512 /* Called with genl_mutex and optionally with RTNL lock also. */
1513 static struct datapath *lookup_datapath(struct net *net,
1514                                         struct ovs_header *ovs_header,
1515                                         struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1516 {
1517         struct datapath *dp;
1518
1519         if (!a[OVS_DP_ATTR_NAME])
1520                 dp = get_dp(net, ovs_header->dp_ifindex);
1521         else {
1522                 struct vport *vport;
1523
1524                 rcu_read_lock();
1525                 vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
1526                 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
1527                 rcu_read_unlock();
1528         }
1529         return dp ? dp : ERR_PTR(-ENODEV);
1530 }
1531
1532 static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1533 {
1534         struct nlattr **a = info->attrs;
1535         struct vport_parms parms;
1536         struct sk_buff *reply;
1537         struct datapath *dp;
1538         struct vport *vport;
1539         struct ovs_net *ovs_net;
1540         int err, i;
1541
1542         err = -EINVAL;
1543         if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1544                 goto err;
1545
1546         err = ovs_dp_cmd_validate(a);
1547         if (err)
1548                 goto err;
1549
1550         rtnl_lock();
1551
1552         err = -ENOMEM;
1553         dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1554         if (dp == NULL)
1555                 goto err_unlock_rtnl;
1556
1557         ovs_dp_set_net(dp, hold_net(sock_net(skb->sk)));
1558
1559         /* Allocate table. */
1560         err = -ENOMEM;
1561         rcu_assign_pointer(dp->table, ovs_flow_tbl_alloc(TBL_MIN_BUCKETS));
1562         if (!dp->table)
1563                 goto err_free_dp;
1564
1565         dp->stats_percpu = alloc_percpu(struct dp_stats_percpu);
1566         if (!dp->stats_percpu) {
1567                 err = -ENOMEM;
1568                 goto err_destroy_table;
1569         }
1570
1571         dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
1572                             GFP_KERNEL);
1573         if (!dp->ports) {
1574                 err = -ENOMEM;
1575                 goto err_destroy_percpu;
1576         }
1577
1578         for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1579                 INIT_HLIST_HEAD(&dp->ports[i]);
1580
1581         /* Set up our datapath device. */
1582         parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1583         parms.type = OVS_VPORT_TYPE_INTERNAL;
1584         parms.options = NULL;
1585         parms.dp = dp;
1586         parms.port_no = OVSP_LOCAL;
1587         parms.upcall_portid = nla_get_u32(a[OVS_DP_ATTR_UPCALL_PID]);
1588
1589         vport = new_vport(&parms);
1590         if (IS_ERR(vport)) {
1591                 err = PTR_ERR(vport);
1592                 if (err == -EBUSY)
1593                         err = -EEXIST;
1594
1595                 goto err_destroy_ports_array;
1596         }
1597
1598         reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
1599                                       info->snd_seq, OVS_DP_CMD_NEW);
1600         err = PTR_ERR(reply);
1601         if (IS_ERR(reply))
1602                 goto err_destroy_local_port;
1603
1604         ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
1605         list_add_tail(&dp->list_node, &ovs_net->dps);
1606
1607         rtnl_unlock();
1608
1609         genl_notify(reply, genl_info_net(info), info->snd_portid,
1610                     ovs_dp_datapath_multicast_group.id, info->nlhdr,
1611                     GFP_KERNEL);
1612         return 0;
1613
1614 err_destroy_local_port:
1615         ovs_dp_detach_port(ovs_vport_rtnl(dp, OVSP_LOCAL));
1616 err_destroy_ports_array:
1617         kfree(dp->ports);
1618 err_destroy_percpu:
1619         free_percpu(dp->stats_percpu);
1620 err_destroy_table:
1621         ovs_flow_tbl_destroy(genl_dereference(dp->table));
1622 err_free_dp:
1623         release_net(ovs_dp_get_net(dp));
1624         kfree(dp);
1625 err_unlock_rtnl:
1626         rtnl_unlock();
1627 err:
1628         return err;
1629 }
1630
1631 /* Called with genl_mutex. */
1632 static void __dp_destroy(struct datapath *dp)
1633 {
1634         int i;
1635
1636         rtnl_lock();
1637
1638         for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1639                 struct vport *vport;
1640                 struct hlist_node *node, *n;
1641
1642                 hlist_for_each_entry_safe(vport, node, n, &dp->ports[i], dp_hash_node)
1643                         if (vport->port_no != OVSP_LOCAL)
1644                                 ovs_dp_detach_port(vport);
1645         }
1646
1647         list_del(&dp->list_node);
1648         ovs_dp_detach_port(ovs_vport_rtnl(dp, OVSP_LOCAL));
1649
1650         /* rtnl_unlock() will wait until all the references to devices that
1651          * are pending unregistration have been dropped.  We do it here to
1652          * ensure that any internal devices (which contain DP pointers) are
1653          * fully destroyed before freeing the datapath.
1654          */
1655         rtnl_unlock();
1656
1657         call_rcu(&dp->rcu, destroy_dp_rcu);
1658 }
1659
1660 static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1661 {
1662         struct sk_buff *reply;
1663         struct datapath *dp;
1664         int err;
1665
1666         err = ovs_dp_cmd_validate(info->attrs);
1667         if (err)
1668                 return err;
1669
1670         dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1671         err = PTR_ERR(dp);
1672         if (IS_ERR(dp))
1673                 return err;
1674
1675         reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
1676                                       info->snd_seq, OVS_DP_CMD_DEL);
1677         err = PTR_ERR(reply);
1678         if (IS_ERR(reply))
1679                 return err;
1680
1681         __dp_destroy(dp);
1682
1683         genl_notify(reply, genl_info_net(info), info->snd_portid,
1684                     ovs_dp_datapath_multicast_group.id, info->nlhdr,
1685                     GFP_KERNEL);
1686
1687         return 0;
1688 }
1689
1690 static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1691 {
1692         struct sk_buff *reply;
1693         struct datapath *dp;
1694         int err;
1695
1696         err = ovs_dp_cmd_validate(info->attrs);
1697         if (err)
1698                 return err;
1699
1700         dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1701         if (IS_ERR(dp))
1702                 return PTR_ERR(dp);
1703
1704         reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
1705                                       info->snd_seq, OVS_DP_CMD_NEW);
1706         if (IS_ERR(reply)) {
1707                 err = PTR_ERR(reply);
1708                 netlink_set_err(GENL_SOCK(sock_net(skb->sk)), 0,
1709                                 ovs_dp_datapath_multicast_group.id, err);
1710                 return 0;
1711         }
1712
1713         genl_notify(reply, genl_info_net(info), info->snd_portid,
1714                     ovs_dp_datapath_multicast_group.id, info->nlhdr,
1715                     GFP_KERNEL);
1716
1717         return 0;
1718 }
1719
1720 static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1721 {
1722         struct sk_buff *reply;
1723         struct datapath *dp;
1724         int err;
1725
1726         err = ovs_dp_cmd_validate(info->attrs);
1727         if (err)
1728                 return err;
1729
1730         dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1731         if (IS_ERR(dp))
1732                 return PTR_ERR(dp);
1733
1734         reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
1735                                       info->snd_seq, OVS_DP_CMD_NEW);
1736         if (IS_ERR(reply))
1737                 return PTR_ERR(reply);
1738
1739         return genlmsg_reply(reply, info);
1740 }
1741
1742 static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1743 {
1744         struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
1745         struct datapath *dp;
1746         int skip = cb->args[0];
1747         int i = 0;
1748
1749         list_for_each_entry(dp, &ovs_net->dps, list_node) {
1750                 if (i >= skip &&
1751                     ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
1752                                          cb->nlh->nlmsg_seq, NLM_F_MULTI,
1753                                          OVS_DP_CMD_NEW) < 0)
1754                         break;
1755                 i++;
1756         }
1757
1758         cb->args[0] = i;
1759
1760         return skb->len;
1761 }
1762
1763 static struct genl_ops dp_datapath_genl_ops[] = {
1764         { .cmd = OVS_DP_CMD_NEW,
1765           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1766           .policy = datapath_policy,
1767           .doit = ovs_dp_cmd_new
1768         },
1769         { .cmd = OVS_DP_CMD_DEL,
1770           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1771           .policy = datapath_policy,
1772           .doit = ovs_dp_cmd_del
1773         },
1774         { .cmd = OVS_DP_CMD_GET,
1775           .flags = 0,               /* OK for unprivileged users. */
1776           .policy = datapath_policy,
1777           .doit = ovs_dp_cmd_get,
1778           .dumpit = ovs_dp_cmd_dump
1779         },
1780         { .cmd = OVS_DP_CMD_SET,
1781           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1782           .policy = datapath_policy,
1783           .doit = ovs_dp_cmd_set,
1784         },
1785 };
1786
1787 static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
1788 #ifdef HAVE_NLA_NUL_STRING
1789         [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1790         [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
1791 #else
1792         [OVS_VPORT_ATTR_STATS] = { .minlen = sizeof(struct ovs_vport_stats) },
1793 #endif
1794         [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
1795         [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
1796         [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1797         [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
1798 };
1799
1800 static struct genl_family dp_vport_genl_family = {
1801         .id = GENL_ID_GENERATE,
1802         .hdrsize = sizeof(struct ovs_header),
1803         .name = OVS_VPORT_FAMILY,
1804         .version = OVS_VPORT_VERSION,
1805         .maxattr = OVS_VPORT_ATTR_MAX,
1806          SET_NETNSOK
1807 };
1808
1809 struct genl_multicast_group ovs_dp_vport_multicast_group = {
1810         .name = OVS_VPORT_MCGROUP
1811 };
1812
1813 /* Called with RTNL lock or RCU read lock. */
1814 static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
1815                                    u32 portid, u32 seq, u32 flags, u8 cmd)
1816 {
1817         struct ovs_header *ovs_header;
1818         struct ovs_vport_stats vport_stats;
1819         int err;
1820
1821         ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
1822                                  flags, cmd);
1823         if (!ovs_header)
1824                 return -EMSGSIZE;
1825
1826         ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1827
1828         if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
1829             nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
1830             nla_put_string(skb, OVS_VPORT_ATTR_NAME, vport->ops->get_name(vport)) ||
1831             nla_put_u32(skb, OVS_VPORT_ATTR_UPCALL_PID, vport->upcall_portid))
1832                 goto nla_put_failure;
1833
1834         ovs_vport_get_stats(vport, &vport_stats);
1835         if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
1836                     &vport_stats))
1837                 goto nla_put_failure;
1838
1839         err = ovs_vport_get_options(vport, skb);
1840         if (err == -EMSGSIZE)
1841                 goto error;
1842
1843         return genlmsg_end(skb, ovs_header);
1844
1845 nla_put_failure:
1846         err = -EMSGSIZE;
1847 error:
1848         genlmsg_cancel(skb, ovs_header);
1849         return err;
1850 }
1851
1852 /* Called with RTNL lock or RCU read lock. */
1853 struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
1854                                          u32 seq, u8 cmd)
1855 {
1856         struct sk_buff *skb;
1857         int retval;
1858
1859         skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1860         if (!skb)
1861                 return ERR_PTR(-ENOMEM);
1862
1863         retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
1864         if (retval < 0) {
1865                 kfree_skb(skb);
1866                 return ERR_PTR(retval);
1867         }
1868         return skb;
1869 }
1870
1871 static int ovs_vport_cmd_validate(struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1872 {
1873         return CHECK_NUL_STRING(a[OVS_VPORT_ATTR_NAME], IFNAMSIZ - 1);
1874 }
1875
1876 /* Called with RTNL lock or RCU read lock. */
1877 static struct vport *lookup_vport(struct net *net,
1878                                   struct ovs_header *ovs_header,
1879                                   struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1880 {
1881         struct datapath *dp;
1882         struct vport *vport;
1883
1884         if (a[OVS_VPORT_ATTR_NAME]) {
1885                 vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
1886                 if (!vport)
1887                         return ERR_PTR(-ENODEV);
1888                 if (ovs_header->dp_ifindex &&
1889                     ovs_header->dp_ifindex != get_dpifindex(vport->dp))
1890                         return ERR_PTR(-ENODEV);
1891                 return vport;
1892         } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
1893                 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1894
1895                 if (port_no >= DP_MAX_PORTS)
1896                         return ERR_PTR(-EFBIG);
1897
1898                 dp = get_dp(net, ovs_header->dp_ifindex);
1899                 if (!dp)
1900                         return ERR_PTR(-ENODEV);
1901
1902                 vport = ovs_vport_rtnl_rcu(dp, port_no);
1903                 if (!vport)
1904                         return ERR_PTR(-ENODEV);
1905                 return vport;
1906         } else
1907                 return ERR_PTR(-EINVAL);
1908 }
1909
1910 static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
1911 {
1912         struct nlattr **a = info->attrs;
1913         struct ovs_header *ovs_header = info->userhdr;
1914         struct vport_parms parms;
1915         struct sk_buff *reply;
1916         struct vport *vport;
1917         struct datapath *dp;
1918         u32 port_no;
1919         int err;
1920
1921         err = -EINVAL;
1922         if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
1923             !a[OVS_VPORT_ATTR_UPCALL_PID])
1924                 goto exit;
1925
1926         err = ovs_vport_cmd_validate(a);
1927         if (err)
1928                 goto exit;
1929
1930         rtnl_lock();
1931         dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1932         err = -ENODEV;
1933         if (!dp)
1934                 goto exit_unlock;
1935
1936         if (a[OVS_VPORT_ATTR_PORT_NO]) {
1937                 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1938
1939                 err = -EFBIG;
1940                 if (port_no >= DP_MAX_PORTS)
1941                         goto exit_unlock;
1942
1943                 vport = ovs_vport_rtnl(dp, port_no);
1944                 err = -EBUSY;
1945                 if (vport)
1946                         goto exit_unlock;
1947         } else {
1948                 for (port_no = 1; ; port_no++) {
1949                         if (port_no >= DP_MAX_PORTS) {
1950                                 err = -EFBIG;
1951                                 goto exit_unlock;
1952                         }
1953                         vport = ovs_vport_rtnl(dp, port_no);
1954                         if (!vport)
1955                                 break;
1956                 }
1957         }
1958
1959         parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
1960         parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1961         parms.options = a[OVS_VPORT_ATTR_OPTIONS];
1962         parms.dp = dp;
1963         parms.port_no = port_no;
1964         parms.upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
1965
1966         vport = new_vport(&parms);
1967         err = PTR_ERR(vport);
1968         if (IS_ERR(vport))
1969                 goto exit_unlock;
1970
1971         err = 0;
1972         if (a[OVS_VPORT_ATTR_STATS])
1973                 ovs_vport_set_stats(vport, nla_data(a[OVS_VPORT_ATTR_STATS]));
1974
1975         reply = ovs_vport_cmd_build_info(vport, info->snd_portid, info->snd_seq,
1976                                          OVS_VPORT_CMD_NEW);
1977         if (IS_ERR(reply)) {
1978                 err = PTR_ERR(reply);
1979                 ovs_dp_detach_port(vport);
1980                 goto exit_unlock;
1981         }
1982         genl_notify(reply, genl_info_net(info), info->snd_portid,
1983                     ovs_dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
1984
1985 exit_unlock:
1986         rtnl_unlock();
1987 exit:
1988         return err;
1989 }
1990
1991 static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
1992 {
1993         struct nlattr **a = info->attrs;
1994         struct sk_buff *reply;
1995         struct vport *vport;
1996         int err;
1997
1998         err = ovs_vport_cmd_validate(a);
1999         if (err)
2000                 goto exit;
2001
2002         rtnl_lock();
2003         vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
2004         err = PTR_ERR(vport);
2005         if (IS_ERR(vport))
2006                 goto exit_unlock;
2007
2008         err = 0;
2009         if (a[OVS_VPORT_ATTR_TYPE] &&
2010             nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type)
2011                 err = -EINVAL;
2012
2013         if (!err && a[OVS_VPORT_ATTR_OPTIONS])
2014                 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
2015         if (err)
2016                 goto exit_unlock;
2017
2018         if (a[OVS_VPORT_ATTR_STATS])
2019                 ovs_vport_set_stats(vport, nla_data(a[OVS_VPORT_ATTR_STATS]));
2020
2021         if (a[OVS_VPORT_ATTR_UPCALL_PID])
2022                 vport->upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
2023
2024         reply = ovs_vport_cmd_build_info(vport, info->snd_portid,
2025                                          info->snd_seq, OVS_VPORT_CMD_NEW);
2026         if (IS_ERR(reply)) {
2027                 netlink_set_err(GENL_SOCK(sock_net(skb->sk)), 0,
2028                                 ovs_dp_vport_multicast_group.id, PTR_ERR(reply));
2029                 goto exit_unlock;
2030         }
2031
2032         genl_notify(reply, genl_info_net(info), info->snd_portid,
2033                     ovs_dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
2034
2035 exit_unlock:
2036         rtnl_unlock();
2037 exit:
2038         return err;
2039 }
2040
2041 static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
2042 {
2043         struct nlattr **a = info->attrs;
2044         struct sk_buff *reply;
2045         struct vport *vport;
2046         int err;
2047
2048         err = ovs_vport_cmd_validate(a);
2049         if (err)
2050                 goto exit;
2051
2052         rtnl_lock();
2053         vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
2054         err = PTR_ERR(vport);
2055         if (IS_ERR(vport))
2056                 goto exit_unlock;
2057
2058         if (vport->port_no == OVSP_LOCAL) {
2059                 err = -EINVAL;
2060                 goto exit_unlock;
2061         }
2062
2063         reply = ovs_vport_cmd_build_info(vport, info->snd_portid,
2064                                          info->snd_seq, OVS_VPORT_CMD_DEL);
2065         err = PTR_ERR(reply);
2066         if (IS_ERR(reply))
2067                 goto exit_unlock;
2068
2069         err = 0;
2070         ovs_dp_detach_port(vport);
2071
2072         genl_notify(reply, genl_info_net(info), info->snd_portid,
2073                     ovs_dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
2074
2075 exit_unlock:
2076         rtnl_unlock();
2077 exit:
2078         return err;
2079 }
2080
2081 static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
2082 {
2083         struct nlattr **a = info->attrs;
2084         struct ovs_header *ovs_header = info->userhdr;
2085         struct sk_buff *reply;
2086         struct vport *vport;
2087         int err;
2088
2089         err = ovs_vport_cmd_validate(a);
2090         if (err)
2091                 goto exit;
2092
2093         rcu_read_lock();
2094         vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
2095         err = PTR_ERR(vport);
2096         if (IS_ERR(vport))
2097                 goto exit_unlock;
2098
2099         reply = ovs_vport_cmd_build_info(vport, info->snd_portid,
2100                                          info->snd_seq, OVS_VPORT_CMD_NEW);
2101         err = PTR_ERR(reply);
2102         if (IS_ERR(reply))
2103                 goto exit_unlock;
2104
2105         rcu_read_unlock();
2106
2107         return genlmsg_reply(reply, info);
2108
2109 exit_unlock:
2110         rcu_read_unlock();
2111 exit:
2112         return err;
2113 }
2114
2115 static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
2116 {
2117         struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
2118         struct datapath *dp;
2119         int bucket = cb->args[0], skip = cb->args[1];
2120         int i, j = 0;
2121
2122         dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
2123         if (!dp)
2124                 return -ENODEV;
2125
2126         rcu_read_lock();
2127         for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
2128                 struct vport *vport;
2129                 struct hlist_node *n;
2130
2131                 j = 0;
2132                 hlist_for_each_entry_rcu(vport, n, &dp->ports[i], dp_hash_node) {
2133                         if (j >= skip &&
2134                             ovs_vport_cmd_fill_info(vport, skb,
2135                                                     NETLINK_CB(cb->skb).portid,
2136                                                     cb->nlh->nlmsg_seq,
2137                                                     NLM_F_MULTI,
2138                                                     OVS_VPORT_CMD_NEW) < 0)
2139                                 goto out;
2140
2141                         j++;
2142                 }
2143                 skip = 0;
2144         }
2145 out:
2146         rcu_read_unlock();
2147
2148         cb->args[0] = i;
2149         cb->args[1] = j;
2150
2151         return skb->len;
2152 }
2153
2154 static struct genl_ops dp_vport_genl_ops[] = {
2155         { .cmd = OVS_VPORT_CMD_NEW,
2156           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2157           .policy = vport_policy,
2158           .doit = ovs_vport_cmd_new
2159         },
2160         { .cmd = OVS_VPORT_CMD_DEL,
2161           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2162           .policy = vport_policy,
2163           .doit = ovs_vport_cmd_del
2164         },
2165         { .cmd = OVS_VPORT_CMD_GET,
2166           .flags = 0,               /* OK for unprivileged users. */
2167           .policy = vport_policy,
2168           .doit = ovs_vport_cmd_get,
2169           .dumpit = ovs_vport_cmd_dump
2170         },
2171         { .cmd = OVS_VPORT_CMD_SET,
2172           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2173           .policy = vport_policy,
2174           .doit = ovs_vport_cmd_set,
2175         },
2176 };
2177
2178 struct genl_family_and_ops {
2179         struct genl_family *family;
2180         struct genl_ops *ops;
2181         int n_ops;
2182         struct genl_multicast_group *group;
2183 };
2184
2185 static const struct genl_family_and_ops dp_genl_families[] = {
2186         { &dp_datapath_genl_family,
2187           dp_datapath_genl_ops, ARRAY_SIZE(dp_datapath_genl_ops),
2188           &ovs_dp_datapath_multicast_group },
2189         { &dp_vport_genl_family,
2190           dp_vport_genl_ops, ARRAY_SIZE(dp_vport_genl_ops),
2191           &ovs_dp_vport_multicast_group },
2192         { &dp_flow_genl_family,
2193           dp_flow_genl_ops, ARRAY_SIZE(dp_flow_genl_ops),
2194           &ovs_dp_flow_multicast_group },
2195         { &dp_packet_genl_family,
2196           dp_packet_genl_ops, ARRAY_SIZE(dp_packet_genl_ops),
2197           NULL },
2198 };
2199
2200 static void dp_unregister_genl(int n_families)
2201 {
2202         int i;
2203
2204         for (i = 0; i < n_families; i++)
2205                 genl_unregister_family(dp_genl_families[i].family);
2206 }
2207
2208 static int dp_register_genl(void)
2209 {
2210         int n_registered;
2211         int err;
2212         int i;
2213
2214         n_registered = 0;
2215         for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
2216                 const struct genl_family_and_ops *f = &dp_genl_families[i];
2217
2218                 err = genl_register_family_with_ops(f->family, f->ops,
2219                                                     f->n_ops);
2220                 if (err)
2221                         goto error;
2222                 n_registered++;
2223
2224                 if (f->group) {
2225                         err = genl_register_mc_group(f->family, f->group);
2226                         if (err)
2227                                 goto error;
2228                 }
2229         }
2230
2231         return 0;
2232
2233 error:
2234         dp_unregister_genl(n_registered);
2235         return err;
2236 }
2237
2238 static int __rehash_flow_table(void *dummy)
2239 {
2240         struct datapath *dp;
2241         struct net *net;
2242
2243         rtnl_lock();
2244         for_each_net(net) {
2245                 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2246
2247                 list_for_each_entry(dp, &ovs_net->dps, list_node) {
2248                         struct flow_table *old_table = genl_dereference(dp->table);
2249                         struct flow_table *new_table;
2250
2251                         new_table = ovs_flow_tbl_rehash(old_table);
2252                         if (!IS_ERR(new_table)) {
2253                                 rcu_assign_pointer(dp->table, new_table);
2254                                 ovs_flow_tbl_deferred_destroy(old_table);
2255                         }
2256                 }
2257         }
2258         rtnl_unlock();
2259         return 0;
2260 }
2261
2262 static void rehash_flow_table(struct work_struct *work)
2263 {
2264         genl_exec(__rehash_flow_table, NULL);
2265         schedule_delayed_work(&rehash_flow_wq, REHASH_FLOW_INTERVAL);
2266 }
2267
2268 static int dp_destroy_all(void *data)
2269 {
2270         struct datapath *dp, *dp_next;
2271         struct ovs_net *ovs_net = data;
2272
2273         list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
2274                 __dp_destroy(dp);
2275
2276         return 0;
2277 }
2278
2279 static int __net_init ovs_init_net(struct net *net)
2280 {
2281         struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2282
2283         INIT_LIST_HEAD(&ovs_net->dps);
2284         return 0;
2285 }
2286
2287 static void __net_exit ovs_exit_net(struct net *net)
2288 {
2289         struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2290
2291         genl_exec(dp_destroy_all, ovs_net);
2292 }
2293
2294 static struct pernet_operations ovs_net_ops = {
2295         .init = ovs_init_net,
2296         .exit = ovs_exit_net,
2297         .id   = &ovs_net_id,
2298         .size = sizeof(struct ovs_net),
2299 };
2300
2301 static int __init dp_init(void)
2302 {
2303         int err;
2304
2305         BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
2306
2307         pr_info("Open vSwitch switching datapath %s, built "__DATE__" "__TIME__"\n",
2308                 VERSION);
2309
2310         err = genl_exec_init();
2311         if (err)
2312                 goto error;
2313
2314         err = ovs_workqueues_init();
2315         if (err)
2316                 goto error_genl_exec;
2317
2318         err = ovs_tnl_init();
2319         if (err)
2320                 goto error_wq;
2321
2322         err = ovs_flow_init();
2323         if (err)
2324                 goto error_tnl_exit;
2325
2326         err = ovs_vport_init();
2327         if (err)
2328                 goto error_flow_exit;
2329
2330         err = register_pernet_device(&ovs_net_ops);
2331         if (err)
2332                 goto error_vport_exit;
2333
2334         err = register_netdevice_notifier(&ovs_dp_device_notifier);
2335         if (err)
2336                 goto error_netns_exit;
2337
2338         err = dp_register_genl();
2339         if (err < 0)
2340                 goto error_unreg_notifier;
2341
2342         schedule_delayed_work(&rehash_flow_wq, REHASH_FLOW_INTERVAL);
2343
2344         return 0;
2345
2346 error_unreg_notifier:
2347         unregister_netdevice_notifier(&ovs_dp_device_notifier);
2348 error_netns_exit:
2349         unregister_pernet_device(&ovs_net_ops);
2350 error_vport_exit:
2351         ovs_vport_exit();
2352 error_flow_exit:
2353         ovs_flow_exit();
2354 error_tnl_exit:
2355         ovs_tnl_exit();
2356 error_wq:
2357         ovs_workqueues_exit();
2358 error_genl_exec:
2359         genl_exec_exit();
2360 error:
2361         return err;
2362 }
2363
2364 static void dp_cleanup(void)
2365 {
2366         cancel_delayed_work_sync(&rehash_flow_wq);
2367         dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
2368         unregister_netdevice_notifier(&ovs_dp_device_notifier);
2369         unregister_pernet_device(&ovs_net_ops);
2370         rcu_barrier();
2371         ovs_vport_exit();
2372         ovs_flow_exit();
2373         ovs_tnl_exit();
2374         ovs_workqueues_exit();
2375         genl_exec_exit();
2376 }
2377
2378 module_init(dp_init);
2379 module_exit(dp_cleanup);
2380
2381 MODULE_DESCRIPTION("Open vSwitch switching datapath");
2382 MODULE_LICENSE("GPL");
2383 MODULE_VERSION(VERSION);