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