datapath: Change ENOENT return value to ENODEV in lookup_vport().
[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,8,0)
65 #error Kernels before 2.6.18 or after 3.7 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);
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         err = genlmsg_unicast(net, user_skb, upcall_info->portid);
403
404 out:
405         kfree_skb(nskb);
406         return err;
407 }
408
409 /* Called with genl_mutex. */
410 static int flush_flows(struct datapath *dp)
411 {
412         struct flow_table *old_table;
413         struct flow_table *new_table;
414
415         old_table = genl_dereference(dp->table);
416         new_table = ovs_flow_tbl_alloc(TBL_MIN_BUCKETS);
417         if (!new_table)
418                 return -ENOMEM;
419
420         rcu_assign_pointer(dp->table, new_table);
421
422         ovs_flow_tbl_deferred_destroy(old_table);
423         return 0;
424 }
425
426 static int validate_actions(const struct nlattr *attr,
427                                 const struct sw_flow_key *key, int depth);
428
429 static int validate_sample(const struct nlattr *attr,
430                                 const struct sw_flow_key *key, int depth)
431 {
432         const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
433         const struct nlattr *probability, *actions;
434         const struct nlattr *a;
435         int rem;
436
437         memset(attrs, 0, sizeof(attrs));
438         nla_for_each_nested(a, attr, rem) {
439                 int type = nla_type(a);
440                 if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
441                         return -EINVAL;
442                 attrs[type] = a;
443         }
444         if (rem)
445                 return -EINVAL;
446
447         probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
448         if (!probability || nla_len(probability) != sizeof(u32))
449                 return -EINVAL;
450
451         actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
452         if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
453                 return -EINVAL;
454         return validate_actions(actions, key, depth + 1);
455 }
456
457 static int validate_tp_port(const struct sw_flow_key *flow_key)
458 {
459         if (flow_key->eth.type == htons(ETH_P_IP)) {
460                 if (flow_key->ipv4.tp.src || flow_key->ipv4.tp.dst)
461                         return 0;
462         } else if (flow_key->eth.type == htons(ETH_P_IPV6)) {
463                 if (flow_key->ipv6.tp.src || flow_key->ipv6.tp.dst)
464                         return 0;
465         }
466
467         return -EINVAL;
468 }
469
470 static int validate_set(const struct nlattr *a,
471                         const struct sw_flow_key *flow_key)
472 {
473         const struct nlattr *ovs_key = nla_data(a);
474         int key_type = nla_type(ovs_key);
475
476         /* There can be only one key in a action */
477         if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
478                 return -EINVAL;
479
480         if (key_type > OVS_KEY_ATTR_MAX ||
481             nla_len(ovs_key) != ovs_key_lens[key_type])
482                 return -EINVAL;
483
484         switch (key_type) {
485         const struct ovs_key_ipv4 *ipv4_key;
486         const struct ovs_key_ipv4_tunnel *tun_key;
487         const struct ovs_key_ipv6 *ipv6_key;
488
489         case OVS_KEY_ATTR_PRIORITY:
490         case OVS_KEY_ATTR_TUN_ID:
491         case OVS_KEY_ATTR_ETHERNET:
492                 break;
493
494         case OVS_KEY_ATTR_SKB_MARK:
495 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20) && !defined(CONFIG_NETFILTER)
496                 if (nla_get_u32(ovs_key) != 0)
497                         return -EINVAL;
498 #endif
499                 break;
500
501         case OVS_KEY_ATTR_IPV4_TUNNEL:
502                 tun_key = nla_data(ovs_key);
503                 if (!tun_key->ipv4_dst)
504                         return -EINVAL;
505                 break;
506
507         case OVS_KEY_ATTR_IPV4:
508                 if (flow_key->eth.type != htons(ETH_P_IP))
509                         return -EINVAL;
510
511                 if (!flow_key->ip.proto)
512                         return -EINVAL;
513
514                 ipv4_key = nla_data(ovs_key);
515                 if (ipv4_key->ipv4_proto != flow_key->ip.proto)
516                         return -EINVAL;
517
518                 if (ipv4_key->ipv4_frag != flow_key->ip.frag)
519                         return -EINVAL;
520
521                 break;
522
523         case OVS_KEY_ATTR_IPV6:
524                 if (flow_key->eth.type != htons(ETH_P_IPV6))
525                         return -EINVAL;
526
527                 if (!flow_key->ip.proto)
528                         return -EINVAL;
529
530                 ipv6_key = nla_data(ovs_key);
531                 if (ipv6_key->ipv6_proto != flow_key->ip.proto)
532                         return -EINVAL;
533
534                 if (ipv6_key->ipv6_frag != flow_key->ip.frag)
535                         return -EINVAL;
536
537                 if (ntohl(ipv6_key->ipv6_label) & 0xFFF00000)
538                         return -EINVAL;
539
540                 break;
541
542         case OVS_KEY_ATTR_TCP:
543                 if (flow_key->ip.proto != IPPROTO_TCP)
544                         return -EINVAL;
545
546                 return validate_tp_port(flow_key);
547
548         case OVS_KEY_ATTR_UDP:
549                 if (flow_key->ip.proto != IPPROTO_UDP)
550                         return -EINVAL;
551
552                 return validate_tp_port(flow_key);
553
554         default:
555                 return -EINVAL;
556         }
557
558         return 0;
559 }
560
561 static int validate_userspace(const struct nlattr *attr)
562 {
563         static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] =   {
564                 [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
565                 [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_U64 },
566         };
567         struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
568         int error;
569
570         error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX,
571                                  attr, userspace_policy);
572         if (error)
573                 return error;
574
575         if (!a[OVS_USERSPACE_ATTR_PID] ||
576             !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
577                 return -EINVAL;
578
579         return 0;
580 }
581
582 static int validate_actions(const struct nlattr *attr,
583                                 const struct sw_flow_key *key,  int depth)
584 {
585         const struct nlattr *a;
586         int rem, err;
587
588         if (depth >= SAMPLE_ACTION_DEPTH)
589                 return -EOVERFLOW;
590
591         nla_for_each_nested(a, attr, rem) {
592                 /* Expected argument lengths, (u32)-1 for variable length. */
593                 static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
594                         [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
595                         [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
596                         [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
597                         [OVS_ACTION_ATTR_POP_VLAN] = 0,
598                         [OVS_ACTION_ATTR_SET] = (u32)-1,
599                         [OVS_ACTION_ATTR_SAMPLE] = (u32)-1
600                 };
601                 const struct ovs_action_push_vlan *vlan;
602                 int type = nla_type(a);
603
604                 if (type > OVS_ACTION_ATTR_MAX ||
605                     (action_lens[type] != nla_len(a) &&
606                      action_lens[type] != (u32)-1))
607                         return -EINVAL;
608
609                 switch (type) {
610                 case OVS_ACTION_ATTR_UNSPEC:
611                         return -EINVAL;
612
613                 case OVS_ACTION_ATTR_USERSPACE:
614                         err = validate_userspace(a);
615                         if (err)
616                                 return err;
617                         break;
618
619                 case OVS_ACTION_ATTR_OUTPUT:
620                         if (nla_get_u32(a) >= DP_MAX_PORTS)
621                                 return -EINVAL;
622                         break;
623
624
625                 case OVS_ACTION_ATTR_POP_VLAN:
626                         break;
627
628                 case OVS_ACTION_ATTR_PUSH_VLAN:
629                         vlan = nla_data(a);
630                         if (vlan->vlan_tpid != htons(ETH_P_8021Q))
631                                 return -EINVAL;
632                         if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
633                                 return -EINVAL;
634                         break;
635
636                 case OVS_ACTION_ATTR_SET:
637                         err = validate_set(a, key);
638                         if (err)
639                                 return err;
640                         break;
641
642                 case OVS_ACTION_ATTR_SAMPLE:
643                         err = validate_sample(a, key, depth);
644                         if (err)
645                                 return err;
646                         break;
647
648                 default:
649                         return -EINVAL;
650                 }
651         }
652
653         if (rem > 0)
654                 return -EINVAL;
655
656         return 0;
657 }
658
659 static void clear_stats(struct sw_flow *flow)
660 {
661         flow->used = 0;
662         flow->tcp_flags = 0;
663         flow->packet_count = 0;
664         flow->byte_count = 0;
665 }
666
667 static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
668 {
669         struct ovs_header *ovs_header = info->userhdr;
670         struct nlattr **a = info->attrs;
671         struct sw_flow_actions *acts;
672         struct sk_buff *packet;
673         struct sw_flow *flow;
674         struct datapath *dp;
675         struct ethhdr *eth;
676         int len;
677         int err;
678         int key_len;
679
680         err = -EINVAL;
681         if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
682             !a[OVS_PACKET_ATTR_ACTIONS] ||
683             nla_len(a[OVS_PACKET_ATTR_PACKET]) < ETH_HLEN)
684                 goto err;
685
686         len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
687         packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
688         err = -ENOMEM;
689         if (!packet)
690                 goto err;
691         skb_reserve(packet, NET_IP_ALIGN);
692
693         memcpy(__skb_put(packet, len), nla_data(a[OVS_PACKET_ATTR_PACKET]), len);
694
695         skb_reset_mac_header(packet);
696         eth = eth_hdr(packet);
697
698         /* Normally, setting the skb 'protocol' field would be handled by a
699          * call to eth_type_trans(), but it assumes there's a sending
700          * device, which we may not have. */
701         if (ntohs(eth->h_proto) >= 1536)
702                 packet->protocol = eth->h_proto;
703         else
704                 packet->protocol = htons(ETH_P_802_2);
705
706         /* Build an sw_flow for sending this packet. */
707         flow = ovs_flow_alloc();
708         err = PTR_ERR(flow);
709         if (IS_ERR(flow))
710                 goto err_kfree_skb;
711
712         err = ovs_flow_extract(packet, -1, &flow->key, &key_len);
713         if (err)
714                 goto err_flow_free;
715
716         err = ovs_flow_metadata_from_nlattrs(flow, key_len, a[OVS_PACKET_ATTR_KEY]);
717         if (err)
718                 goto err_flow_free;
719
720         err = validate_actions(a[OVS_PACKET_ATTR_ACTIONS], &flow->key, 0);
721         if (err)
722                 goto err_flow_free;
723
724         acts = ovs_flow_actions_alloc(a[OVS_PACKET_ATTR_ACTIONS]);
725         err = PTR_ERR(acts);
726         if (IS_ERR(acts))
727                 goto err_flow_free;
728         rcu_assign_pointer(flow->sf_acts, acts);
729
730         OVS_CB(packet)->flow = flow;
731         packet->priority = flow->key.phy.priority;
732         skb_set_mark(packet, flow->key.phy.skb_mark);
733
734         rcu_read_lock();
735         dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
736         err = -ENODEV;
737         if (!dp)
738                 goto err_unlock;
739
740         local_bh_disable();
741         err = ovs_execute_actions(dp, packet);
742         local_bh_enable();
743         rcu_read_unlock();
744
745         ovs_flow_free(flow);
746         return err;
747
748 err_unlock:
749         rcu_read_unlock();
750 err_flow_free:
751         ovs_flow_free(flow);
752 err_kfree_skb:
753         kfree_skb(packet);
754 err:
755         return err;
756 }
757
758 static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
759         [OVS_PACKET_ATTR_PACKET] = { .type = NLA_UNSPEC },
760         [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
761         [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
762 };
763
764 static struct genl_ops dp_packet_genl_ops[] = {
765         { .cmd = OVS_PACKET_CMD_EXECUTE,
766           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
767           .policy = packet_policy,
768           .doit = ovs_packet_cmd_execute
769         }
770 };
771
772 static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats)
773 {
774         int i;
775         struct flow_table *table = genl_dereference(dp->table);
776
777         stats->n_flows = ovs_flow_tbl_count(table);
778
779         stats->n_hit = stats->n_missed = stats->n_lost = 0;
780         for_each_possible_cpu(i) {
781                 const struct dp_stats_percpu *percpu_stats;
782                 struct dp_stats_percpu local_stats;
783                 unsigned int start;
784
785                 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
786
787                 do {
788                         start = u64_stats_fetch_begin_bh(&percpu_stats->sync);
789                         local_stats = *percpu_stats;
790                 } while (u64_stats_fetch_retry_bh(&percpu_stats->sync, start));
791
792                 stats->n_hit += local_stats.n_hit;
793                 stats->n_missed += local_stats.n_missed;
794                 stats->n_lost += local_stats.n_lost;
795         }
796 }
797
798 static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
799         [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
800         [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
801         [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
802 };
803
804 static struct genl_family dp_flow_genl_family = {
805         .id = GENL_ID_GENERATE,
806         .hdrsize = sizeof(struct ovs_header),
807         .name = OVS_FLOW_FAMILY,
808         .version = OVS_FLOW_VERSION,
809         .maxattr = OVS_FLOW_ATTR_MAX,
810          SET_NETNSOK
811 };
812
813 static struct genl_multicast_group ovs_dp_flow_multicast_group = {
814         .name = OVS_FLOW_MCGROUP
815 };
816
817 /* Called with genl_lock. */
818 static int ovs_flow_cmd_fill_info(struct sw_flow *flow, struct datapath *dp,
819                                   struct sk_buff *skb, u32 portid,
820                                   u32 seq, u32 flags, u8 cmd)
821 {
822         const int skb_orig_len = skb->len;
823         const struct sw_flow_actions *sf_acts;
824         struct ovs_flow_stats stats;
825         struct ovs_header *ovs_header;
826         struct nlattr *nla;
827         unsigned long used;
828         u8 tcp_flags;
829         int err;
830
831         sf_acts = rcu_dereference_protected(flow->sf_acts,
832                                             lockdep_genl_is_held());
833
834         ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family, flags, cmd);
835         if (!ovs_header)
836                 return -EMSGSIZE;
837
838         ovs_header->dp_ifindex = get_dpifindex(dp);
839
840         nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
841         if (!nla)
842                 goto nla_put_failure;
843         err = ovs_flow_to_nlattrs(&flow->key, skb);
844         if (err)
845                 goto error;
846         nla_nest_end(skb, nla);
847
848         spin_lock_bh(&flow->lock);
849         used = flow->used;
850         stats.n_packets = flow->packet_count;
851         stats.n_bytes = flow->byte_count;
852         tcp_flags = flow->tcp_flags;
853         spin_unlock_bh(&flow->lock);
854
855         if (used &&
856             nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
857                 goto nla_put_failure;
858
859         if (stats.n_packets &&
860             nla_put(skb, OVS_FLOW_ATTR_STATS,
861                     sizeof(struct ovs_flow_stats), &stats))
862                 goto nla_put_failure;
863
864         if (tcp_flags &&
865             nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, tcp_flags))
866                 goto nla_put_failure;
867
868         /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
869          * this is the first flow to be dumped into 'skb'.  This is unusual for
870          * Netlink but individual action lists can be longer than
871          * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
872          * The userspace caller can always fetch the actions separately if it
873          * really wants them.  (Most userspace callers in fact don't care.)
874          *
875          * This can only fail for dump operations because the skb is always
876          * properly sized for single flows.
877          */
878         err = nla_put(skb, OVS_FLOW_ATTR_ACTIONS, sf_acts->actions_len,
879                       sf_acts->actions);
880         if (err < 0 && skb_orig_len)
881                 goto error;
882
883         return genlmsg_end(skb, ovs_header);
884
885 nla_put_failure:
886         err = -EMSGSIZE;
887 error:
888         genlmsg_cancel(skb, ovs_header);
889         return err;
890 }
891
892 static struct sk_buff *ovs_flow_cmd_alloc_info(struct sw_flow *flow)
893 {
894         const struct sw_flow_actions *sf_acts;
895         int len;
896
897         sf_acts = rcu_dereference_protected(flow->sf_acts,
898                                             lockdep_genl_is_held());
899
900         /* OVS_FLOW_ATTR_KEY */
901         len = nla_total_size(FLOW_BUFSIZE);
902         /* OVS_FLOW_ATTR_ACTIONS */
903         len += nla_total_size(sf_acts->actions_len);
904         /* OVS_FLOW_ATTR_STATS */
905         len += nla_total_size(sizeof(struct ovs_flow_stats));
906         /* OVS_FLOW_ATTR_TCP_FLAGS */
907         len += nla_total_size(1);
908         /* OVS_FLOW_ATTR_USED */
909         len += nla_total_size(8);
910
911         len += NLMSG_ALIGN(sizeof(struct ovs_header));
912
913         return genlmsg_new(len, GFP_KERNEL);
914 }
915
916 static struct sk_buff *ovs_flow_cmd_build_info(struct sw_flow *flow,
917                                                struct datapath *dp,
918                                                u32 portid, u32 seq, u8 cmd)
919 {
920         struct sk_buff *skb;
921         int retval;
922
923         skb = ovs_flow_cmd_alloc_info(flow);
924         if (!skb)
925                 return ERR_PTR(-ENOMEM);
926
927         retval = ovs_flow_cmd_fill_info(flow, dp, skb, portid, seq, 0, cmd);
928         BUG_ON(retval < 0);
929         return skb;
930 }
931
932 static int ovs_flow_cmd_new_or_set(struct sk_buff *skb, struct genl_info *info)
933 {
934         struct nlattr **a = info->attrs;
935         struct ovs_header *ovs_header = info->userhdr;
936         struct sw_flow_key key;
937         struct sw_flow *flow;
938         struct sk_buff *reply;
939         struct datapath *dp;
940         struct flow_table *table;
941         int error;
942         int key_len;
943
944         /* Extract key. */
945         error = -EINVAL;
946         if (!a[OVS_FLOW_ATTR_KEY])
947                 goto error;
948         error = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
949         if (error)
950                 goto error;
951
952         /* Validate actions. */
953         if (a[OVS_FLOW_ATTR_ACTIONS]) {
954                 error = validate_actions(a[OVS_FLOW_ATTR_ACTIONS], &key,  0);
955                 if (error)
956                         goto error;
957         } else if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW) {
958                 error = -EINVAL;
959                 goto error;
960         }
961
962         dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
963         error = -ENODEV;
964         if (!dp)
965                 goto error;
966
967         table = genl_dereference(dp->table);
968         flow = ovs_flow_tbl_lookup(table, &key, key_len);
969         if (!flow) {
970                 struct sw_flow_actions *acts;
971
972                 /* Bail out if we're not allowed to create a new flow. */
973                 error = -ENOENT;
974                 if (info->genlhdr->cmd == OVS_FLOW_CMD_SET)
975                         goto error;
976
977                 /* Expand table, if necessary, to make room. */
978                 if (ovs_flow_tbl_need_to_expand(table)) {
979                         struct flow_table *new_table;
980
981                         new_table = ovs_flow_tbl_expand(table);
982                         if (!IS_ERR(new_table)) {
983                                 rcu_assign_pointer(dp->table, new_table);
984                                 ovs_flow_tbl_deferred_destroy(table);
985                                 table = genl_dereference(dp->table);
986                         }
987                 }
988
989                 /* Allocate flow. */
990                 flow = ovs_flow_alloc();
991                 if (IS_ERR(flow)) {
992                         error = PTR_ERR(flow);
993                         goto error;
994                 }
995                 clear_stats(flow);
996
997                 /* Obtain actions. */
998                 acts = ovs_flow_actions_alloc(a[OVS_FLOW_ATTR_ACTIONS]);
999                 error = PTR_ERR(acts);
1000                 if (IS_ERR(acts))
1001                         goto error_free_flow;
1002                 rcu_assign_pointer(flow->sf_acts, acts);
1003
1004                 /* Put flow in bucket. */
1005                 ovs_flow_tbl_insert(table, flow, &key, key_len);
1006
1007                 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
1008                                                 info->snd_seq,
1009                                                 OVS_FLOW_CMD_NEW);
1010         } else {
1011                 /* We found a matching flow. */
1012                 struct sw_flow_actions *old_acts;
1013                 struct nlattr *acts_attrs;
1014
1015                 /* Bail out if we're not allowed to modify an existing flow.
1016                  * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
1017                  * because Generic Netlink treats the latter as a dump
1018                  * request.  We also accept NLM_F_EXCL in case that bug ever
1019                  * gets fixed.
1020                  */
1021                 error = -EEXIST;
1022                 if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW &&
1023                     info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL))
1024                         goto error;
1025
1026                 /* Update actions. */
1027                 old_acts = rcu_dereference_protected(flow->sf_acts,
1028                                                      lockdep_genl_is_held());
1029                 acts_attrs = a[OVS_FLOW_ATTR_ACTIONS];
1030                 if (acts_attrs &&
1031                    (old_acts->actions_len != nla_len(acts_attrs) ||
1032                    memcmp(old_acts->actions, nla_data(acts_attrs),
1033                           old_acts->actions_len))) {
1034                         struct sw_flow_actions *new_acts;
1035
1036                         new_acts = ovs_flow_actions_alloc(acts_attrs);
1037                         error = PTR_ERR(new_acts);
1038                         if (IS_ERR(new_acts))
1039                                 goto error;
1040
1041                         rcu_assign_pointer(flow->sf_acts, new_acts);
1042                         ovs_flow_deferred_free_acts(old_acts);
1043                 }
1044
1045                 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
1046                                                info->snd_seq, OVS_FLOW_CMD_NEW);
1047
1048                 /* Clear stats. */
1049                 if (a[OVS_FLOW_ATTR_CLEAR]) {
1050                         spin_lock_bh(&flow->lock);
1051                         clear_stats(flow);
1052                         spin_unlock_bh(&flow->lock);
1053                 }
1054         }
1055
1056         if (!IS_ERR(reply))
1057                 genl_notify(reply, genl_info_net(info), info->snd_portid,
1058                            ovs_dp_flow_multicast_group.id, info->nlhdr,
1059                            GFP_KERNEL);
1060         else
1061                 netlink_set_err(GENL_SOCK(sock_net(skb->sk)), 0,
1062                                 ovs_dp_flow_multicast_group.id, PTR_ERR(reply));
1063         return 0;
1064
1065 error_free_flow:
1066         ovs_flow_free(flow);
1067 error:
1068         return error;
1069 }
1070
1071 static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1072 {
1073         struct nlattr **a = info->attrs;
1074         struct ovs_header *ovs_header = info->userhdr;
1075         struct sw_flow_key key;
1076         struct sk_buff *reply;
1077         struct sw_flow *flow;
1078         struct datapath *dp;
1079         struct flow_table *table;
1080         int err;
1081         int key_len;
1082
1083         if (!a[OVS_FLOW_ATTR_KEY])
1084                 return -EINVAL;
1085         err = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
1086         if (err)
1087                 return err;
1088
1089         dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1090         if (!dp)
1091                 return -ENODEV;
1092
1093         table = genl_dereference(dp->table);
1094         flow = ovs_flow_tbl_lookup(table, &key, key_len);
1095         if (!flow)
1096                 return -ENOENT;
1097
1098         reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
1099                                         info->snd_seq, OVS_FLOW_CMD_NEW);
1100         if (IS_ERR(reply))
1101                 return PTR_ERR(reply);
1102
1103         return genlmsg_reply(reply, info);
1104 }
1105
1106 static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1107 {
1108         struct nlattr **a = info->attrs;
1109         struct ovs_header *ovs_header = info->userhdr;
1110         struct sw_flow_key key;
1111         struct sk_buff *reply;
1112         struct sw_flow *flow;
1113         struct datapath *dp;
1114         struct flow_table *table;
1115         int err;
1116         int key_len;
1117
1118         dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1119         if (!dp)
1120                 return -ENODEV;
1121
1122         if (!a[OVS_FLOW_ATTR_KEY])
1123                 return flush_flows(dp);
1124
1125         err = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
1126         if (err)
1127                 return err;
1128
1129         table = genl_dereference(dp->table);
1130         flow = ovs_flow_tbl_lookup(table, &key, key_len);
1131         if (!flow)
1132                 return -ENOENT;
1133
1134         reply = ovs_flow_cmd_alloc_info(flow);
1135         if (!reply)
1136                 return -ENOMEM;
1137
1138         ovs_flow_tbl_remove(table, flow);
1139
1140         err = ovs_flow_cmd_fill_info(flow, dp, reply, info->snd_portid,
1141                                      info->snd_seq, 0, OVS_FLOW_CMD_DEL);
1142         BUG_ON(err < 0);
1143
1144         ovs_flow_deferred_free(flow);
1145
1146         genl_notify(reply, genl_info_net(info), info->snd_portid,
1147                     ovs_dp_flow_multicast_group.id, info->nlhdr, GFP_KERNEL);
1148         return 0;
1149 }
1150
1151 static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1152 {
1153         struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1154         struct datapath *dp;
1155         struct flow_table *table;
1156
1157         dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1158         if (!dp)
1159                 return -ENODEV;
1160
1161         table = genl_dereference(dp->table);
1162
1163         for (;;) {
1164                 struct sw_flow *flow;
1165                 u32 bucket, obj;
1166
1167                 bucket = cb->args[0];
1168                 obj = cb->args[1];
1169                 flow = ovs_flow_tbl_next(table, &bucket, &obj);
1170                 if (!flow)
1171                         break;
1172
1173                 if (ovs_flow_cmd_fill_info(flow, dp, skb,
1174                                            NETLINK_CB(cb->skb).portid,
1175                                            cb->nlh->nlmsg_seq, NLM_F_MULTI,
1176                                            OVS_FLOW_CMD_NEW) < 0)
1177                         break;
1178
1179                 cb->args[0] = bucket;
1180                 cb->args[1] = obj;
1181         }
1182         return skb->len;
1183 }
1184
1185 static struct genl_ops dp_flow_genl_ops[] = {
1186         { .cmd = OVS_FLOW_CMD_NEW,
1187           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1188           .policy = flow_policy,
1189           .doit = ovs_flow_cmd_new_or_set
1190         },
1191         { .cmd = OVS_FLOW_CMD_DEL,
1192           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1193           .policy = flow_policy,
1194           .doit = ovs_flow_cmd_del
1195         },
1196         { .cmd = OVS_FLOW_CMD_GET,
1197           .flags = 0,               /* OK for unprivileged users. */
1198           .policy = flow_policy,
1199           .doit = ovs_flow_cmd_get,
1200           .dumpit = ovs_flow_cmd_dump
1201         },
1202         { .cmd = OVS_FLOW_CMD_SET,
1203           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1204           .policy = flow_policy,
1205           .doit = ovs_flow_cmd_new_or_set,
1206         },
1207 };
1208
1209 static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1210 #ifdef HAVE_NLA_NUL_STRING
1211         [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1212 #endif
1213         [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1214 };
1215
1216 static struct genl_family dp_datapath_genl_family = {
1217         .id = GENL_ID_GENERATE,
1218         .hdrsize = sizeof(struct ovs_header),
1219         .name = OVS_DATAPATH_FAMILY,
1220         .version = OVS_DATAPATH_VERSION,
1221         .maxattr = OVS_DP_ATTR_MAX,
1222          SET_NETNSOK
1223 };
1224
1225 static struct genl_multicast_group ovs_dp_datapath_multicast_group = {
1226         .name = OVS_DATAPATH_MCGROUP
1227 };
1228
1229 static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
1230                                 u32 portid, u32 seq, u32 flags, u8 cmd)
1231 {
1232         struct ovs_header *ovs_header;
1233         struct ovs_dp_stats dp_stats;
1234         int err;
1235
1236         ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
1237                                    flags, cmd);
1238         if (!ovs_header)
1239                 goto error;
1240
1241         ovs_header->dp_ifindex = get_dpifindex(dp);
1242
1243         rcu_read_lock();
1244         err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
1245         rcu_read_unlock();
1246         if (err)
1247                 goto nla_put_failure;
1248
1249         get_dp_stats(dp, &dp_stats);
1250         if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats), &dp_stats))
1251                 goto nla_put_failure;
1252
1253         return genlmsg_end(skb, ovs_header);
1254
1255 nla_put_failure:
1256         genlmsg_cancel(skb, ovs_header);
1257 error:
1258         return -EMSGSIZE;
1259 }
1260
1261 static struct sk_buff *ovs_dp_cmd_build_info(struct datapath *dp, u32 portid,
1262                                              u32 seq, u8 cmd)
1263 {
1264         struct sk_buff *skb;
1265         int retval;
1266
1267         skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1268         if (!skb)
1269                 return ERR_PTR(-ENOMEM);
1270
1271         retval = ovs_dp_cmd_fill_info(dp, skb, portid, seq, 0, cmd);
1272         if (retval < 0) {
1273                 kfree_skb(skb);
1274                 return ERR_PTR(retval);
1275         }
1276         return skb;
1277 }
1278
1279 static int ovs_dp_cmd_validate(struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1280 {
1281         return CHECK_NUL_STRING(a[OVS_DP_ATTR_NAME], IFNAMSIZ - 1);
1282 }
1283
1284 /* Called with genl_mutex and optionally with RTNL lock also. */
1285 static struct datapath *lookup_datapath(struct net *net,
1286                                         struct ovs_header *ovs_header,
1287                                         struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1288 {
1289         struct datapath *dp;
1290
1291         if (!a[OVS_DP_ATTR_NAME])
1292                 dp = get_dp(net, ovs_header->dp_ifindex);
1293         else {
1294                 struct vport *vport;
1295
1296                 rcu_read_lock();
1297                 vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
1298                 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
1299                 rcu_read_unlock();
1300         }
1301         return dp ? dp : ERR_PTR(-ENODEV);
1302 }
1303
1304 static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1305 {
1306         struct nlattr **a = info->attrs;
1307         struct vport_parms parms;
1308         struct sk_buff *reply;
1309         struct datapath *dp;
1310         struct vport *vport;
1311         struct ovs_net *ovs_net;
1312         int err, i;
1313
1314         err = -EINVAL;
1315         if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1316                 goto err;
1317
1318         err = ovs_dp_cmd_validate(a);
1319         if (err)
1320                 goto err;
1321
1322         rtnl_lock();
1323
1324         err = -ENOMEM;
1325         dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1326         if (dp == NULL)
1327                 goto err_unlock_rtnl;
1328
1329         ovs_dp_set_net(dp, hold_net(sock_net(skb->sk)));
1330
1331         /* Allocate table. */
1332         err = -ENOMEM;
1333         rcu_assign_pointer(dp->table, ovs_flow_tbl_alloc(TBL_MIN_BUCKETS));
1334         if (!dp->table)
1335                 goto err_free_dp;
1336
1337         dp->stats_percpu = alloc_percpu(struct dp_stats_percpu);
1338         if (!dp->stats_percpu) {
1339                 err = -ENOMEM;
1340                 goto err_destroy_table;
1341         }
1342
1343         dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
1344                             GFP_KERNEL);
1345         if (!dp->ports) {
1346                 err = -ENOMEM;
1347                 goto err_destroy_percpu;
1348         }
1349
1350         for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1351                 INIT_HLIST_HEAD(&dp->ports[i]);
1352
1353         /* Set up our datapath device. */
1354         parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1355         parms.type = OVS_VPORT_TYPE_INTERNAL;
1356         parms.options = NULL;
1357         parms.dp = dp;
1358         parms.port_no = OVSP_LOCAL;
1359         parms.upcall_portid = nla_get_u32(a[OVS_DP_ATTR_UPCALL_PID]);
1360
1361         vport = new_vport(&parms);
1362         if (IS_ERR(vport)) {
1363                 err = PTR_ERR(vport);
1364                 if (err == -EBUSY)
1365                         err = -EEXIST;
1366
1367                 goto err_destroy_ports_array;
1368         }
1369
1370         reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
1371                                       info->snd_seq, OVS_DP_CMD_NEW);
1372         err = PTR_ERR(reply);
1373         if (IS_ERR(reply))
1374                 goto err_destroy_local_port;
1375
1376         ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
1377         list_add_tail(&dp->list_node, &ovs_net->dps);
1378
1379         rtnl_unlock();
1380
1381         genl_notify(reply, genl_info_net(info), info->snd_portid,
1382                     ovs_dp_datapath_multicast_group.id, info->nlhdr,
1383                     GFP_KERNEL);
1384         return 0;
1385
1386 err_destroy_local_port:
1387         ovs_dp_detach_port(ovs_vport_rtnl(dp, OVSP_LOCAL));
1388 err_destroy_ports_array:
1389         kfree(dp->ports);
1390 err_destroy_percpu:
1391         free_percpu(dp->stats_percpu);
1392 err_destroy_table:
1393         ovs_flow_tbl_destroy(genl_dereference(dp->table));
1394 err_free_dp:
1395         release_net(ovs_dp_get_net(dp));
1396         kfree(dp);
1397 err_unlock_rtnl:
1398         rtnl_unlock();
1399 err:
1400         return err;
1401 }
1402
1403 /* Called with genl_mutex. */
1404 static void __dp_destroy(struct datapath *dp)
1405 {
1406         int i;
1407
1408         rtnl_lock();
1409
1410         for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1411                 struct vport *vport;
1412                 struct hlist_node *node, *n;
1413
1414                 hlist_for_each_entry_safe(vport, node, n, &dp->ports[i], dp_hash_node)
1415                         if (vport->port_no != OVSP_LOCAL)
1416                                 ovs_dp_detach_port(vport);
1417         }
1418
1419         list_del(&dp->list_node);
1420         ovs_dp_detach_port(ovs_vport_rtnl(dp, OVSP_LOCAL));
1421
1422         /* rtnl_unlock() will wait until all the references to devices that
1423          * are pending unregistration have been dropped.  We do it here to
1424          * ensure that any internal devices (which contain DP pointers) are
1425          * fully destroyed before freeing the datapath.
1426          */
1427         rtnl_unlock();
1428
1429         call_rcu(&dp->rcu, destroy_dp_rcu);
1430 }
1431
1432 static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1433 {
1434         struct sk_buff *reply;
1435         struct datapath *dp;
1436         int err;
1437
1438         err = ovs_dp_cmd_validate(info->attrs);
1439         if (err)
1440                 return err;
1441
1442         dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1443         err = PTR_ERR(dp);
1444         if (IS_ERR(dp))
1445                 return err;
1446
1447         reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
1448                                       info->snd_seq, OVS_DP_CMD_DEL);
1449         err = PTR_ERR(reply);
1450         if (IS_ERR(reply))
1451                 return err;
1452
1453         __dp_destroy(dp);
1454
1455         genl_notify(reply, genl_info_net(info), info->snd_portid,
1456                     ovs_dp_datapath_multicast_group.id, info->nlhdr,
1457                     GFP_KERNEL);
1458
1459         return 0;
1460 }
1461
1462 static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1463 {
1464         struct sk_buff *reply;
1465         struct datapath *dp;
1466         int err;
1467
1468         err = ovs_dp_cmd_validate(info->attrs);
1469         if (err)
1470                 return err;
1471
1472         dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1473         if (IS_ERR(dp))
1474                 return PTR_ERR(dp);
1475
1476         reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
1477                                       info->snd_seq, OVS_DP_CMD_NEW);
1478         if (IS_ERR(reply)) {
1479                 err = PTR_ERR(reply);
1480                 netlink_set_err(GENL_SOCK(sock_net(skb->sk)), 0,
1481                                 ovs_dp_datapath_multicast_group.id, err);
1482                 return 0;
1483         }
1484
1485         genl_notify(reply, genl_info_net(info), info->snd_portid,
1486                     ovs_dp_datapath_multicast_group.id, info->nlhdr,
1487                     GFP_KERNEL);
1488
1489         return 0;
1490 }
1491
1492 static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1493 {
1494         struct sk_buff *reply;
1495         struct datapath *dp;
1496         int err;
1497
1498         err = ovs_dp_cmd_validate(info->attrs);
1499         if (err)
1500                 return err;
1501
1502         dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1503         if (IS_ERR(dp))
1504                 return PTR_ERR(dp);
1505
1506         reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
1507                                       info->snd_seq, OVS_DP_CMD_NEW);
1508         if (IS_ERR(reply))
1509                 return PTR_ERR(reply);
1510
1511         return genlmsg_reply(reply, info);
1512 }
1513
1514 static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1515 {
1516         struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
1517         struct datapath *dp;
1518         int skip = cb->args[0];
1519         int i = 0;
1520
1521         list_for_each_entry(dp, &ovs_net->dps, list_node) {
1522                 if (i >= skip &&
1523                     ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
1524                                          cb->nlh->nlmsg_seq, NLM_F_MULTI,
1525                                          OVS_DP_CMD_NEW) < 0)
1526                         break;
1527                 i++;
1528         }
1529
1530         cb->args[0] = i;
1531
1532         return skb->len;
1533 }
1534
1535 static struct genl_ops dp_datapath_genl_ops[] = {
1536         { .cmd = OVS_DP_CMD_NEW,
1537           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1538           .policy = datapath_policy,
1539           .doit = ovs_dp_cmd_new
1540         },
1541         { .cmd = OVS_DP_CMD_DEL,
1542           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1543           .policy = datapath_policy,
1544           .doit = ovs_dp_cmd_del
1545         },
1546         { .cmd = OVS_DP_CMD_GET,
1547           .flags = 0,               /* OK for unprivileged users. */
1548           .policy = datapath_policy,
1549           .doit = ovs_dp_cmd_get,
1550           .dumpit = ovs_dp_cmd_dump
1551         },
1552         { .cmd = OVS_DP_CMD_SET,
1553           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1554           .policy = datapath_policy,
1555           .doit = ovs_dp_cmd_set,
1556         },
1557 };
1558
1559 static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
1560 #ifdef HAVE_NLA_NUL_STRING
1561         [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1562         [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
1563         [OVS_VPORT_ATTR_ADDRESS] = { .len = ETH_ALEN },
1564 #else
1565         [OVS_VPORT_ATTR_STATS] = { .minlen = sizeof(struct ovs_vport_stats) },
1566         [OVS_VPORT_ATTR_ADDRESS] = { .minlen = ETH_ALEN },
1567 #endif
1568         [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
1569         [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
1570         [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1571         [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
1572 };
1573
1574 static struct genl_family dp_vport_genl_family = {
1575         .id = GENL_ID_GENERATE,
1576         .hdrsize = sizeof(struct ovs_header),
1577         .name = OVS_VPORT_FAMILY,
1578         .version = OVS_VPORT_VERSION,
1579         .maxattr = OVS_VPORT_ATTR_MAX,
1580          SET_NETNSOK
1581 };
1582
1583 struct genl_multicast_group ovs_dp_vport_multicast_group = {
1584         .name = OVS_VPORT_MCGROUP
1585 };
1586
1587 /* Called with RTNL lock or RCU read lock. */
1588 static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
1589                                    u32 portid, u32 seq, u32 flags, u8 cmd)
1590 {
1591         struct ovs_header *ovs_header;
1592         struct ovs_vport_stats vport_stats;
1593         int err;
1594
1595         ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
1596                                  flags, cmd);
1597         if (!ovs_header)
1598                 return -EMSGSIZE;
1599
1600         ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1601
1602         if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
1603             nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
1604             nla_put_string(skb, OVS_VPORT_ATTR_NAME, vport->ops->get_name(vport)) ||
1605             nla_put_u32(skb, OVS_VPORT_ATTR_UPCALL_PID, vport->upcall_portid))
1606                 goto nla_put_failure;
1607
1608         ovs_vport_get_stats(vport, &vport_stats);
1609         if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
1610                     &vport_stats))
1611                 goto nla_put_failure;
1612
1613         if (nla_put(skb, OVS_VPORT_ATTR_ADDRESS, ETH_ALEN,
1614                     vport->ops->get_addr(vport)))
1615                 goto nla_put_failure;
1616
1617         err = ovs_vport_get_options(vport, skb);
1618         if (err == -EMSGSIZE)
1619                 goto error;
1620
1621         return genlmsg_end(skb, ovs_header);
1622
1623 nla_put_failure:
1624         err = -EMSGSIZE;
1625 error:
1626         genlmsg_cancel(skb, ovs_header);
1627         return err;
1628 }
1629
1630 /* Called with RTNL lock or RCU read lock. */
1631 struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
1632                                          u32 seq, u8 cmd)
1633 {
1634         struct sk_buff *skb;
1635         int retval;
1636
1637         skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1638         if (!skb)
1639                 return ERR_PTR(-ENOMEM);
1640
1641         retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
1642         if (retval < 0) {
1643                 kfree_skb(skb);
1644                 return ERR_PTR(retval);
1645         }
1646         return skb;
1647 }
1648
1649 static int ovs_vport_cmd_validate(struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1650 {
1651         return CHECK_NUL_STRING(a[OVS_VPORT_ATTR_NAME], IFNAMSIZ - 1);
1652 }
1653
1654 /* Called with RTNL lock or RCU read lock. */
1655 static struct vport *lookup_vport(struct net *net,
1656                                   struct ovs_header *ovs_header,
1657                                   struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1658 {
1659         struct datapath *dp;
1660         struct vport *vport;
1661
1662         if (a[OVS_VPORT_ATTR_NAME]) {
1663                 vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
1664                 if (!vport)
1665                         return ERR_PTR(-ENODEV);
1666                 if (ovs_header->dp_ifindex &&
1667                     ovs_header->dp_ifindex != get_dpifindex(vport->dp))
1668                         return ERR_PTR(-ENODEV);
1669                 return vport;
1670         } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
1671                 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1672
1673                 if (port_no >= DP_MAX_PORTS)
1674                         return ERR_PTR(-EFBIG);
1675
1676                 dp = get_dp(net, ovs_header->dp_ifindex);
1677                 if (!dp)
1678                         return ERR_PTR(-ENODEV);
1679
1680                 vport = ovs_vport_rtnl_rcu(dp, port_no);
1681                 if (!vport)
1682                         return ERR_PTR(-ENODEV);
1683                 return vport;
1684         } else
1685                 return ERR_PTR(-EINVAL);
1686 }
1687
1688 /* Called with RTNL lock. */
1689 static int change_vport(struct vport *vport,
1690                         struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1691 {
1692         int err = 0;
1693
1694         if (a[OVS_VPORT_ATTR_STATS])
1695                 ovs_vport_set_stats(vport, nla_data(a[OVS_VPORT_ATTR_STATS]));
1696
1697         if (a[OVS_VPORT_ATTR_ADDRESS])
1698                 err = ovs_vport_set_addr(vport, nla_data(a[OVS_VPORT_ATTR_ADDRESS]));
1699
1700         return err;
1701 }
1702
1703 static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
1704 {
1705         struct nlattr **a = info->attrs;
1706         struct ovs_header *ovs_header = info->userhdr;
1707         struct vport_parms parms;
1708         struct sk_buff *reply;
1709         struct vport *vport;
1710         struct datapath *dp;
1711         u32 port_no;
1712         int err;
1713
1714         err = -EINVAL;
1715         if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
1716             !a[OVS_VPORT_ATTR_UPCALL_PID])
1717                 goto exit;
1718
1719         err = ovs_vport_cmd_validate(a);
1720         if (err)
1721                 goto exit;
1722
1723         rtnl_lock();
1724         dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1725         err = -ENODEV;
1726         if (!dp)
1727                 goto exit_unlock;
1728
1729         if (a[OVS_VPORT_ATTR_PORT_NO]) {
1730                 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1731
1732                 err = -EFBIG;
1733                 if (port_no >= DP_MAX_PORTS)
1734                         goto exit_unlock;
1735
1736                 vport = ovs_vport_rtnl(dp, port_no);
1737                 err = -EBUSY;
1738                 if (vport)
1739                         goto exit_unlock;
1740         } else {
1741                 for (port_no = 1; ; port_no++) {
1742                         if (port_no >= DP_MAX_PORTS) {
1743                                 err = -EFBIG;
1744                                 goto exit_unlock;
1745                         }
1746                         vport = ovs_vport_rtnl(dp, port_no);
1747                         if (!vport)
1748                                 break;
1749                 }
1750         }
1751
1752         parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
1753         parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1754         parms.options = a[OVS_VPORT_ATTR_OPTIONS];
1755         parms.dp = dp;
1756         parms.port_no = port_no;
1757         parms.upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
1758
1759         vport = new_vport(&parms);
1760         err = PTR_ERR(vport);
1761         if (IS_ERR(vport))
1762                 goto exit_unlock;
1763
1764         err = change_vport(vport, a);
1765         if (!err) {
1766                 reply = ovs_vport_cmd_build_info(vport, info->snd_portid,
1767                                                  info->snd_seq,
1768                                                  OVS_VPORT_CMD_NEW);
1769                 if (IS_ERR(reply))
1770                         err = PTR_ERR(reply);
1771         }
1772         if (err) {
1773                 ovs_dp_detach_port(vport);
1774                 goto exit_unlock;
1775         }
1776         genl_notify(reply, genl_info_net(info), info->snd_portid,
1777                     ovs_dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
1778
1779 exit_unlock:
1780         rtnl_unlock();
1781 exit:
1782         return err;
1783 }
1784
1785 static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
1786 {
1787         struct nlattr **a = info->attrs;
1788         struct sk_buff *reply;
1789         struct vport *vport;
1790         int err;
1791
1792         err = ovs_vport_cmd_validate(a);
1793         if (err)
1794                 goto exit;
1795
1796         rtnl_lock();
1797         vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
1798         err = PTR_ERR(vport);
1799         if (IS_ERR(vport))
1800                 goto exit_unlock;
1801
1802         err = 0;
1803         if (a[OVS_VPORT_ATTR_TYPE] &&
1804             nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type)
1805                 err = -EINVAL;
1806
1807         if (!err && a[OVS_VPORT_ATTR_OPTIONS])
1808                 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
1809         if (!err)
1810                 err = change_vport(vport, a);
1811         else
1812                 goto exit_unlock;
1813         if (!err && a[OVS_VPORT_ATTR_UPCALL_PID])
1814                 vport->upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
1815
1816         reply = ovs_vport_cmd_build_info(vport, info->snd_portid,
1817                                          info->snd_seq, OVS_VPORT_CMD_NEW);
1818         if (IS_ERR(reply)) {
1819                 netlink_set_err(GENL_SOCK(sock_net(skb->sk)), 0,
1820                                 ovs_dp_vport_multicast_group.id, PTR_ERR(reply));
1821                 goto exit_unlock;
1822         }
1823
1824         genl_notify(reply, genl_info_net(info), info->snd_portid,
1825                     ovs_dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
1826
1827 exit_unlock:
1828         rtnl_unlock();
1829 exit:
1830         return err;
1831 }
1832
1833 static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
1834 {
1835         struct nlattr **a = info->attrs;
1836         struct sk_buff *reply;
1837         struct vport *vport;
1838         int err;
1839
1840         err = ovs_vport_cmd_validate(a);
1841         if (err)
1842                 goto exit;
1843
1844         rtnl_lock();
1845         vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
1846         err = PTR_ERR(vport);
1847         if (IS_ERR(vport))
1848                 goto exit_unlock;
1849
1850         if (vport->port_no == OVSP_LOCAL) {
1851                 err = -EINVAL;
1852                 goto exit_unlock;
1853         }
1854
1855         reply = ovs_vport_cmd_build_info(vport, info->snd_portid,
1856                                          info->snd_seq, OVS_VPORT_CMD_DEL);
1857         err = PTR_ERR(reply);
1858         if (IS_ERR(reply))
1859                 goto exit_unlock;
1860
1861         ovs_dp_detach_port(vport);
1862
1863         genl_notify(reply, genl_info_net(info), info->snd_portid,
1864                     ovs_dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
1865
1866 exit_unlock:
1867         rtnl_unlock();
1868 exit:
1869         return err;
1870 }
1871
1872 static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
1873 {
1874         struct nlattr **a = info->attrs;
1875         struct ovs_header *ovs_header = info->userhdr;
1876         struct sk_buff *reply;
1877         struct vport *vport;
1878         int err;
1879
1880         err = ovs_vport_cmd_validate(a);
1881         if (err)
1882                 goto exit;
1883
1884         rcu_read_lock();
1885         vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
1886         err = PTR_ERR(vport);
1887         if (IS_ERR(vport))
1888                 goto exit_unlock;
1889
1890         reply = ovs_vport_cmd_build_info(vport, info->snd_portid,
1891                                          info->snd_seq, OVS_VPORT_CMD_NEW);
1892         err = PTR_ERR(reply);
1893         if (IS_ERR(reply))
1894                 goto exit_unlock;
1895
1896         rcu_read_unlock();
1897
1898         return genlmsg_reply(reply, info);
1899
1900 exit_unlock:
1901         rcu_read_unlock();
1902 exit:
1903         return err;
1904 }
1905
1906 static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1907 {
1908         struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1909         struct datapath *dp;
1910         int bucket = cb->args[0], skip = cb->args[1];
1911         int i, j = 0;
1912
1913         dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1914         if (!dp)
1915                 return -ENODEV;
1916
1917         rcu_read_lock();
1918         for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
1919                 struct vport *vport;
1920                 struct hlist_node *n;
1921
1922                 j = 0;
1923                 hlist_for_each_entry_rcu(vport, n, &dp->ports[i], dp_hash_node) {
1924                         if (j >= skip &&
1925                             ovs_vport_cmd_fill_info(vport, skb,
1926                                                     NETLINK_CB(cb->skb).portid,
1927                                                     cb->nlh->nlmsg_seq,
1928                                                     NLM_F_MULTI,
1929                                                     OVS_VPORT_CMD_NEW) < 0)
1930                                 goto out;
1931
1932                         j++;
1933                 }
1934                 skip = 0;
1935         }
1936 out:
1937         rcu_read_unlock();
1938
1939         cb->args[0] = i;
1940         cb->args[1] = j;
1941
1942         return skb->len;
1943 }
1944
1945 static struct genl_ops dp_vport_genl_ops[] = {
1946         { .cmd = OVS_VPORT_CMD_NEW,
1947           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1948           .policy = vport_policy,
1949           .doit = ovs_vport_cmd_new
1950         },
1951         { .cmd = OVS_VPORT_CMD_DEL,
1952           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1953           .policy = vport_policy,
1954           .doit = ovs_vport_cmd_del
1955         },
1956         { .cmd = OVS_VPORT_CMD_GET,
1957           .flags = 0,               /* OK for unprivileged users. */
1958           .policy = vport_policy,
1959           .doit = ovs_vport_cmd_get,
1960           .dumpit = ovs_vport_cmd_dump
1961         },
1962         { .cmd = OVS_VPORT_CMD_SET,
1963           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1964           .policy = vport_policy,
1965           .doit = ovs_vport_cmd_set,
1966         },
1967 };
1968
1969 struct genl_family_and_ops {
1970         struct genl_family *family;
1971         struct genl_ops *ops;
1972         int n_ops;
1973         struct genl_multicast_group *group;
1974 };
1975
1976 static const struct genl_family_and_ops dp_genl_families[] = {
1977         { &dp_datapath_genl_family,
1978           dp_datapath_genl_ops, ARRAY_SIZE(dp_datapath_genl_ops),
1979           &ovs_dp_datapath_multicast_group },
1980         { &dp_vport_genl_family,
1981           dp_vport_genl_ops, ARRAY_SIZE(dp_vport_genl_ops),
1982           &ovs_dp_vport_multicast_group },
1983         { &dp_flow_genl_family,
1984           dp_flow_genl_ops, ARRAY_SIZE(dp_flow_genl_ops),
1985           &ovs_dp_flow_multicast_group },
1986         { &dp_packet_genl_family,
1987           dp_packet_genl_ops, ARRAY_SIZE(dp_packet_genl_ops),
1988           NULL },
1989 };
1990
1991 static void dp_unregister_genl(int n_families)
1992 {
1993         int i;
1994
1995         for (i = 0; i < n_families; i++)
1996                 genl_unregister_family(dp_genl_families[i].family);
1997 }
1998
1999 static int dp_register_genl(void)
2000 {
2001         int n_registered;
2002         int err;
2003         int i;
2004
2005         n_registered = 0;
2006         for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
2007                 const struct genl_family_and_ops *f = &dp_genl_families[i];
2008
2009                 err = genl_register_family_with_ops(f->family, f->ops,
2010                                                     f->n_ops);
2011                 if (err)
2012                         goto error;
2013                 n_registered++;
2014
2015                 if (f->group) {
2016                         err = genl_register_mc_group(f->family, f->group);
2017                         if (err)
2018                                 goto error;
2019                 }
2020         }
2021
2022         return 0;
2023
2024 error:
2025         dp_unregister_genl(n_registered);
2026         return err;
2027 }
2028
2029 static int __rehash_flow_table(void *dummy)
2030 {
2031         struct datapath *dp;
2032         struct net *net;
2033
2034         rtnl_lock();
2035         for_each_net(net) {
2036                 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2037
2038                 list_for_each_entry(dp, &ovs_net->dps, list_node) {
2039                         struct flow_table *old_table = genl_dereference(dp->table);
2040                         struct flow_table *new_table;
2041
2042                         new_table = ovs_flow_tbl_rehash(old_table);
2043                         if (!IS_ERR(new_table)) {
2044                                 rcu_assign_pointer(dp->table, new_table);
2045                                 ovs_flow_tbl_deferred_destroy(old_table);
2046                         }
2047                 }
2048         }
2049         rtnl_unlock();
2050         return 0;
2051 }
2052
2053 static void rehash_flow_table(struct work_struct *work)
2054 {
2055         genl_exec(__rehash_flow_table, NULL);
2056         schedule_delayed_work(&rehash_flow_wq, REHASH_FLOW_INTERVAL);
2057 }
2058
2059 static int dp_destroy_all(void *data)
2060 {
2061         struct datapath *dp, *dp_next;
2062         struct ovs_net *ovs_net = data;
2063
2064         list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
2065                 __dp_destroy(dp);
2066
2067         return 0;
2068 }
2069
2070 static int __net_init ovs_init_net(struct net *net)
2071 {
2072         struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2073
2074         INIT_LIST_HEAD(&ovs_net->dps);
2075         return 0;
2076 }
2077
2078 static void __net_exit ovs_exit_net(struct net *net)
2079 {
2080         struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2081
2082         genl_exec(dp_destroy_all, ovs_net);
2083 }
2084
2085 static struct pernet_operations ovs_net_ops = {
2086         .init = ovs_init_net,
2087         .exit = ovs_exit_net,
2088         .id   = &ovs_net_id,
2089         .size = sizeof(struct ovs_net),
2090 };
2091
2092 static int __init dp_init(void)
2093 {
2094         struct sk_buff *dummy_skb;
2095         int err;
2096
2097         BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > sizeof(dummy_skb->cb));
2098
2099         pr_info("Open vSwitch switching datapath %s, built "__DATE__" "__TIME__"\n",
2100                 VERSION);
2101
2102         err = genl_exec_init();
2103         if (err)
2104                 goto error;
2105
2106         err = ovs_workqueues_init();
2107         if (err)
2108                 goto error_genl_exec;
2109
2110         err = ovs_tnl_init();
2111         if (err)
2112                 goto error_wq;
2113
2114         err = ovs_flow_init();
2115         if (err)
2116                 goto error_tnl_exit;
2117
2118         err = ovs_vport_init();
2119         if (err)
2120                 goto error_flow_exit;
2121
2122         err = register_pernet_device(&ovs_net_ops);
2123         if (err)
2124                 goto error_vport_exit;
2125
2126         err = register_netdevice_notifier(&ovs_dp_device_notifier);
2127         if (err)
2128                 goto error_netns_exit;
2129
2130         err = dp_register_genl();
2131         if (err < 0)
2132                 goto error_unreg_notifier;
2133
2134         schedule_delayed_work(&rehash_flow_wq, REHASH_FLOW_INTERVAL);
2135
2136         return 0;
2137
2138 error_unreg_notifier:
2139         unregister_netdevice_notifier(&ovs_dp_device_notifier);
2140 error_netns_exit:
2141         unregister_pernet_device(&ovs_net_ops);
2142 error_vport_exit:
2143         ovs_vport_exit();
2144 error_flow_exit:
2145         ovs_flow_exit();
2146 error_tnl_exit:
2147         ovs_tnl_exit();
2148 error_wq:
2149         ovs_workqueues_exit();
2150 error_genl_exec:
2151         genl_exec_exit();
2152 error:
2153         return err;
2154 }
2155
2156 static void dp_cleanup(void)
2157 {
2158         cancel_delayed_work_sync(&rehash_flow_wq);
2159         dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
2160         unregister_netdevice_notifier(&ovs_dp_device_notifier);
2161         unregister_pernet_device(&ovs_net_ops);
2162         rcu_barrier();
2163         ovs_vport_exit();
2164         ovs_flow_exit();
2165         ovs_tnl_exit();
2166         ovs_workqueues_exit();
2167         genl_exec_exit();
2168 }
2169
2170 module_init(dp_init);
2171 module_exit(dp_cleanup);
2172
2173 MODULE_DESCRIPTION("Open vSwitch switching datapath");
2174 MODULE_LICENSE("GPL");
2175 MODULE_VERSION(VERSION);