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