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