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