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