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