aa4c1097e79da09fd8f857e5d072411336fb01d2
[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         free_percpu(dp->stats_percpu);
191         release_net(ovs_dp_get_net(dp));
192         kfree(dp->ports);
193         kfree(dp);
194 }
195
196 static struct hlist_head *vport_hash_bucket(const struct datapath *dp,
197                                             u16 port_no)
198 {
199         return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)];
200 }
201
202 /* Called with ovs_mutex or RCU read lock. */
203 struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no)
204 {
205         struct vport *vport;
206         struct hlist_head *head;
207
208         head = vport_hash_bucket(dp, port_no);
209         hlist_for_each_entry_rcu(vport, head, dp_hash_node) {
210                 if (vport->port_no == port_no)
211                         return vport;
212         }
213         return NULL;
214 }
215
216 /* Called with ovs_mutex. */
217 static struct vport *new_vport(const struct vport_parms *parms)
218 {
219         struct vport *vport;
220
221         vport = ovs_vport_add(parms);
222         if (!IS_ERR(vport)) {
223                 struct datapath *dp = parms->dp;
224                 struct hlist_head *head = vport_hash_bucket(dp, vport->port_no);
225
226                 hlist_add_head_rcu(&vport->dp_hash_node, head);
227         }
228         return vport;
229 }
230
231 void ovs_dp_detach_port(struct vport *p)
232 {
233         ASSERT_OVSL();
234
235         /* First drop references to device. */
236         hlist_del_rcu(&p->dp_hash_node);
237
238         /* Then destroy it. */
239         ovs_vport_del(p);
240 }
241
242 void ovs_dp_process_packet_with_key(struct sk_buff *skb,
243                 struct sw_flow_key *pkt_key)
244 {
245         const struct vport *p = OVS_CB(skb)->input_vport;
246         struct datapath *dp = p->dp;
247         struct sw_flow *flow;
248         struct dp_stats_percpu *stats;
249         u64 *stats_counter;
250         u32 n_mask_hit;
251
252         stats = this_cpu_ptr(dp->stats_percpu);
253
254         /* Look up flow. */
255         flow = ovs_flow_tbl_lookup_stats(&dp->table, pkt_key, &n_mask_hit);
256         if (unlikely(!flow)) {
257                 struct dp_upcall_info upcall;
258
259                 upcall.cmd = OVS_PACKET_CMD_MISS;
260                 upcall.key = pkt_key;
261                 upcall.userdata = NULL;
262                 upcall.portid = ovs_vport_find_upcall_portid(p, skb);
263                 ovs_dp_upcall(dp, skb, &upcall);
264                 consume_skb(skb);
265                 stats_counter = &stats->n_missed;
266                 goto out;
267         }
268
269         OVS_CB(skb)->pkt_key = pkt_key;
270         OVS_CB(skb)->flow = flow;
271
272         ovs_flow_stats_update(OVS_CB(skb)->flow, pkt_key->tp.flags, skb);
273         ovs_execute_actions(dp, skb);
274         stats_counter = &stats->n_hit;
275
276 out:
277         /* Update datapath statistics. */
278         u64_stats_update_begin(&stats->sync);
279         (*stats_counter)++;
280         stats->n_mask_hit += n_mask_hit;
281         u64_stats_update_end(&stats->sync);
282 }
283
284 /* Must be called with rcu_read_lock. */
285 void ovs_dp_process_received_packet(struct vport *p, struct sk_buff *skb)
286 {
287         int error;
288         struct sw_flow_key key;
289
290         OVS_CB(skb)->input_vport = p;
291
292         /* Extract flow from 'skb' into 'key'. */
293         error = ovs_flow_extract(skb, p->port_no, &key);
294         if (unlikely(error)) {
295                 kfree_skb(skb);
296                 return;
297         }
298
299         ovs_dp_process_packet_with_key(skb, &key);
300 }
301
302 int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
303                   const struct dp_upcall_info *upcall_info)
304 {
305         struct dp_stats_percpu *stats;
306         int err;
307
308         if (upcall_info->portid == 0) {
309                 err = -ENOTCONN;
310                 goto err;
311         }
312
313         if (!skb_is_gso(skb))
314                 err = queue_userspace_packet(dp, skb, upcall_info);
315         else
316                 err = queue_gso_packets(dp, skb, upcall_info);
317         if (err)
318                 goto err;
319
320         return 0;
321
322 err:
323         stats = this_cpu_ptr(dp->stats_percpu);
324
325         u64_stats_update_begin(&stats->sync);
326         stats->n_lost++;
327         u64_stats_update_end(&stats->sync);
328
329         return err;
330 }
331
332 static int queue_gso_packets(struct datapath *dp, struct sk_buff *skb,
333                              const struct dp_upcall_info *upcall_info)
334 {
335         unsigned short gso_type = skb_shinfo(skb)->gso_type;
336         struct dp_upcall_info later_info;
337         struct sw_flow_key later_key;
338         struct sk_buff *segs, *nskb;
339         int err;
340
341         segs = __skb_gso_segment(skb, NETIF_F_SG, false);
342         if (IS_ERR(segs))
343                 return PTR_ERR(segs);
344
345         /* Queue all of the segments. */
346         skb = segs;
347         do {
348                 err = queue_userspace_packet(dp, skb, upcall_info);
349                 if (err)
350                         break;
351
352                 if (skb == segs && gso_type & SKB_GSO_UDP) {
353                         /* The initial flow key extracted by ovs_flow_extract()
354                          * in this case is for a first fragment, so we need to
355                          * properly mark later fragments.
356                          */
357                         later_key = *upcall_info->key;
358                         later_key.ip.frag = OVS_FRAG_TYPE_LATER;
359
360                         later_info = *upcall_info;
361                         later_info.key = &later_key;
362                         upcall_info = &later_info;
363                 }
364         } while ((skb = skb->next));
365
366         /* Free all of the segments. */
367         skb = segs;
368         do {
369                 nskb = skb->next;
370                 if (err)
371                         kfree_skb(skb);
372                 else
373                         consume_skb(skb);
374         } while ((skb = nskb));
375         return err;
376 }
377
378 static size_t key_attr_size(void)
379 {
380         return    nla_total_size(4)   /* OVS_KEY_ATTR_PRIORITY */
381                 + nla_total_size(0)   /* OVS_KEY_ATTR_TUNNEL */
382                   + nla_total_size(8)   /* OVS_TUNNEL_KEY_ATTR_ID */
383                   + nla_total_size(4)   /* OVS_TUNNEL_KEY_ATTR_IPV4_SRC */
384                   + nla_total_size(4)   /* OVS_TUNNEL_KEY_ATTR_IPV4_DST */
385                   + nla_total_size(1)   /* OVS_TUNNEL_KEY_ATTR_TOS */
386                   + nla_total_size(1)   /* OVS_TUNNEL_KEY_ATTR_TTL */
387                   + nla_total_size(0)   /* OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT */
388                   + nla_total_size(0)   /* OVS_TUNNEL_KEY_ATTR_CSUM */
389                 + nla_total_size(4)   /* OVS_KEY_ATTR_IN_PORT */
390                 + nla_total_size(4)   /* OVS_KEY_ATTR_SKB_MARK */
391                 + nla_total_size(12)  /* OVS_KEY_ATTR_ETHERNET */
392                 + nla_total_size(2)   /* OVS_KEY_ATTR_ETHERTYPE */
393                 + nla_total_size(4)   /* OVS_KEY_ATTR_8021Q */
394                 + nla_total_size(0)   /* OVS_KEY_ATTR_ENCAP */
395                 + nla_total_size(2)   /* OVS_KEY_ATTR_ETHERTYPE */
396                 + nla_total_size(40)  /* OVS_KEY_ATTR_IPV6 */
397                 + nla_total_size(2)   /* OVS_KEY_ATTR_ICMPV6 */
398                 + nla_total_size(28); /* OVS_KEY_ATTR_ND */
399 }
400
401 static size_t upcall_msg_size(const struct nlattr *userdata,
402                               unsigned int hdrlen)
403 {
404         size_t size = NLMSG_ALIGN(sizeof(struct ovs_header))
405                 + nla_total_size(hdrlen) /* OVS_PACKET_ATTR_PACKET */
406                 + nla_total_size(key_attr_size()); /* OVS_PACKET_ATTR_KEY */
407
408         /* OVS_PACKET_ATTR_USERDATA */
409         if (userdata)
410                 size += NLA_ALIGN(userdata->nla_len);
411
412         return size;
413 }
414
415 static int queue_userspace_packet(struct datapath *dp, struct sk_buff *skb,
416                                   const struct dp_upcall_info *upcall_info)
417 {
418         struct ovs_header *upcall;
419         struct sk_buff *nskb = NULL;
420         struct sk_buff *user_skb; /* to be queued to userspace */
421         struct nlattr *nla;
422         struct genl_info info = {
423 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,14,0)
424                 .dst_sk = ovs_dp_get_net(dp)->genl_sock,
425 #endif
426                 .snd_portid = upcall_info->portid,
427         };
428         size_t len;
429         unsigned int hlen;
430         int err, dp_ifindex;
431
432         dp_ifindex = get_dpifindex(dp);
433         if (!dp_ifindex)
434                 return -ENODEV;
435
436         if (vlan_tx_tag_present(skb)) {
437                 nskb = skb_clone(skb, GFP_ATOMIC);
438                 if (!nskb)
439                         return -ENOMEM;
440
441                 nskb = __vlan_put_tag(nskb, nskb->vlan_proto, vlan_tx_tag_get(nskb));
442                 if (!nskb)
443                         return -ENOMEM;
444
445                 vlan_set_tci(nskb, 0);
446
447                 skb = nskb;
448         }
449
450         if (nla_attr_size(skb->len) > USHRT_MAX) {
451                 err = -EFBIG;
452                 goto out;
453         }
454
455         /* Complete checksum if needed */
456         if (skb->ip_summed == CHECKSUM_PARTIAL &&
457             (err = skb_checksum_help(skb)))
458                 goto out;
459
460         /* Older versions of OVS user space enforce alignment of the last
461          * Netlink attribute to NLA_ALIGNTO which would require extensive
462          * padding logic. Only perform zerocopy if padding is not required.
463          */
464         if (dp->user_features & OVS_DP_F_UNALIGNED)
465                 hlen = skb_zerocopy_headlen(skb);
466         else
467                 hlen = skb->len;
468
469         len = upcall_msg_size(upcall_info->userdata, hlen);
470         user_skb = genlmsg_new_unicast(len, &info, GFP_ATOMIC);
471         if (!user_skb) {
472                 err = -ENOMEM;
473                 goto out;
474         }
475
476         upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
477                              0, upcall_info->cmd);
478         upcall->dp_ifindex = dp_ifindex;
479
480         nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
481         ovs_nla_put_flow(upcall_info->key, upcall_info->key, user_skb);
482         nla_nest_end(user_skb, nla);
483
484         if (upcall_info->userdata)
485                 __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA,
486                           nla_len(upcall_info->userdata),
487                           nla_data(upcall_info->userdata));
488
489         /* Only reserve room for attribute header, packet data is added
490          * in skb_zerocopy() */
491         if (!(nla = nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, 0))) {
492                 err = -ENOBUFS;
493                 goto out;
494         }
495         nla->nla_len = nla_attr_size(skb->len);
496
497         err = skb_zerocopy(user_skb, skb, skb->len, hlen);
498         if (err)
499                 goto out;
500
501         /* Pad OVS_PACKET_ATTR_PACKET if linear copy was performed */
502         if (!(dp->user_features & OVS_DP_F_UNALIGNED)) {
503                 size_t plen = NLA_ALIGN(user_skb->len) - user_skb->len;
504
505                 if (plen > 0)
506                         memset(skb_put(user_skb, plen), 0, plen);
507         }
508
509         ((struct nlmsghdr *) user_skb->data)->nlmsg_len = user_skb->len;
510
511         err = genlmsg_unicast(ovs_dp_get_net(dp), user_skb, upcall_info->portid);
512 out:
513         if (err)
514                 skb_tx_error(skb);
515         kfree_skb(nskb);
516         return err;
517 }
518
519 static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
520 {
521         struct ovs_header *ovs_header = info->userhdr;
522         struct nlattr **a = info->attrs;
523         struct sw_flow_actions *acts;
524         struct sk_buff *packet;
525         struct sw_flow *flow;
526         struct datapath *dp;
527         struct ethhdr *eth;
528         struct vport *input_vport;
529         int len;
530         int err;
531
532         err = -EINVAL;
533         if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
534             !a[OVS_PACKET_ATTR_ACTIONS])
535                 goto err;
536
537         len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
538         packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
539         err = -ENOMEM;
540         if (!packet)
541                 goto err;
542         skb_reserve(packet, NET_IP_ALIGN);
543
544         nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
545
546         skb_reset_mac_header(packet);
547         eth = eth_hdr(packet);
548
549         /* Normally, setting the skb 'protocol' field would be handled by a
550          * call to eth_type_trans(), but it assumes there's a sending
551          * device, which we may not have. */
552         if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN)
553                 packet->protocol = eth->h_proto;
554         else
555                 packet->protocol = htons(ETH_P_802_2);
556
557         /* Build an sw_flow for sending this packet. */
558         flow = ovs_flow_alloc();
559         err = PTR_ERR(flow);
560         if (IS_ERR(flow))
561                 goto err_kfree_skb;
562
563         err = ovs_flow_extract(packet, -1, &flow->key);
564         if (err)
565                 goto err_flow_free;
566
567         err = ovs_nla_get_flow_metadata(flow, a[OVS_PACKET_ATTR_KEY]);
568         if (err)
569                 goto err_flow_free;
570         acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_PACKET_ATTR_ACTIONS]));
571         err = PTR_ERR(acts);
572         if (IS_ERR(acts))
573                 goto err_flow_free;
574
575         err = ovs_nla_copy_actions(a[OVS_PACKET_ATTR_ACTIONS],
576                                    &flow->key, 0, &acts);
577         rcu_assign_pointer(flow->sf_acts, acts);
578         if (err)
579                 goto err_flow_free;
580
581         OVS_CB(packet)->flow = flow;
582         OVS_CB(packet)->pkt_key = &flow->key;
583         packet->priority = flow->key.phy.priority;
584         packet->mark = flow->key.phy.skb_mark;
585
586         rcu_read_lock();
587         dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
588         err = -ENODEV;
589         if (!dp)
590                 goto err_unlock;
591
592         input_vport = ovs_vport_rcu(dp, flow->key.phy.in_port);
593         if (!input_vport)
594                 input_vport = ovs_vport_rcu(dp, OVSP_LOCAL);
595
596         if (!input_vport)
597                 goto err_unlock;
598
599         OVS_CB(packet)->input_vport = input_vport;
600
601         local_bh_disable();
602         err = ovs_execute_actions(dp, packet);
603         local_bh_enable();
604         rcu_read_unlock();
605
606         ovs_flow_free(flow, false);
607         return err;
608
609 err_unlock:
610         rcu_read_unlock();
611 err_flow_free:
612         ovs_flow_free(flow, false);
613 err_kfree_skb:
614         kfree_skb(packet);
615 err:
616         return err;
617 }
618
619 static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
620         [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
621         [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
622         [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
623 };
624
625 static struct genl_ops dp_packet_genl_ops[] = {
626         { .cmd = OVS_PACKET_CMD_EXECUTE,
627           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
628           .policy = packet_policy,
629           .doit = ovs_packet_cmd_execute
630         }
631 };
632
633 static struct genl_family dp_packet_genl_family = {
634         .id = GENL_ID_GENERATE,
635         .hdrsize = sizeof(struct ovs_header),
636         .name = OVS_PACKET_FAMILY,
637         .version = OVS_PACKET_VERSION,
638         .maxattr = OVS_PACKET_ATTR_MAX,
639         .netnsok = true,
640         .parallel_ops = true,
641         .ops = dp_packet_genl_ops,
642         .n_ops = ARRAY_SIZE(dp_packet_genl_ops),
643 };
644
645 static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats,
646                          struct ovs_dp_megaflow_stats *mega_stats)
647 {
648         int i;
649
650         memset(mega_stats, 0, sizeof(*mega_stats));
651
652         stats->n_flows = ovs_flow_tbl_count(&dp->table);
653         mega_stats->n_masks = ovs_flow_tbl_num_masks(&dp->table);
654
655         stats->n_hit = stats->n_missed = stats->n_lost = 0;
656
657         for_each_possible_cpu(i) {
658                 const struct dp_stats_percpu *percpu_stats;
659                 struct dp_stats_percpu local_stats;
660                 unsigned int start;
661
662                 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
663
664                 do {
665                         start = u64_stats_fetch_begin_bh(&percpu_stats->sync);
666                         local_stats = *percpu_stats;
667                 } while (u64_stats_fetch_retry_bh(&percpu_stats->sync, start));
668
669                 stats->n_hit += local_stats.n_hit;
670                 stats->n_missed += local_stats.n_missed;
671                 stats->n_lost += local_stats.n_lost;
672                 mega_stats->n_mask_hit += local_stats.n_mask_hit;
673         }
674 }
675
676 static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts)
677 {
678         return NLMSG_ALIGN(sizeof(struct ovs_header))
679                 + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_KEY */
680                 + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_MASK */
681                 + nla_total_size(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
682                 + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
683                 + nla_total_size(8) /* OVS_FLOW_ATTR_USED */
684                 + nla_total_size(acts->actions_len); /* OVS_FLOW_ATTR_ACTIONS */
685 }
686
687 /* Called with ovs_mutex or RCU read lock. */
688 static int ovs_flow_cmd_fill_info(const struct sw_flow *flow, int dp_ifindex,
689                                   struct sk_buff *skb, u32 portid,
690                                   u32 seq, u32 flags, u8 cmd)
691 {
692         const int skb_orig_len = skb->len;
693         struct nlattr *start;
694         struct ovs_flow_stats stats;
695         __be16 tcp_flags;
696         unsigned long used;
697         struct ovs_header *ovs_header;
698         struct nlattr *nla;
699         int err;
700
701         ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family, flags, cmd);
702         if (!ovs_header)
703                 return -EMSGSIZE;
704
705         ovs_header->dp_ifindex = dp_ifindex;
706
707         /* Fill flow key. */
708         nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
709         if (!nla)
710                 goto nla_put_failure;
711
712         err = ovs_nla_put_flow(&flow->unmasked_key, &flow->unmasked_key, skb);
713         if (err)
714                 goto error;
715         nla_nest_end(skb, nla);
716
717         nla = nla_nest_start(skb, OVS_FLOW_ATTR_MASK);
718         if (!nla)
719                 goto nla_put_failure;
720
721         err = ovs_nla_put_flow(&flow->key, &flow->mask->key, skb);
722         if (err)
723                 goto error;
724
725         nla_nest_end(skb, nla);
726
727         ovs_flow_stats_get(flow, &stats, &used, &tcp_flags);
728
729         if (used &&
730             nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
731                 goto nla_put_failure;
732
733         if (stats.n_packets &&
734             nla_put(skb, OVS_FLOW_ATTR_STATS, sizeof(struct ovs_flow_stats), &stats))
735                 goto nla_put_failure;
736
737         if ((u8)ntohs(tcp_flags) &&
738              nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, (u8)ntohs(tcp_flags)))
739                 goto nla_put_failure;
740
741         /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
742          * this is the first flow to be dumped into 'skb'.  This is unusual for
743          * Netlink but individual action lists can be longer than
744          * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
745          * The userspace caller can always fetch the actions separately if it
746          * really wants them.  (Most userspace callers in fact don't care.)
747          *
748          * This can only fail for dump operations because the skb is always
749          * properly sized for single flows.
750          */
751         start = nla_nest_start(skb, OVS_FLOW_ATTR_ACTIONS);
752         if (start) {
753                 const struct sw_flow_actions *sf_acts;
754
755                 sf_acts = rcu_dereference_ovsl(flow->sf_acts);
756                 err = ovs_nla_put_actions(sf_acts->actions,
757                                           sf_acts->actions_len, skb);
758
759                 if (!err)
760                         nla_nest_end(skb, start);
761                 else {
762                         if (skb_orig_len)
763                                 goto error;
764
765                         nla_nest_cancel(skb, start);
766                 }
767         } else if (skb_orig_len)
768                 goto nla_put_failure;
769
770         return genlmsg_end(skb, ovs_header);
771
772 nla_put_failure:
773         err = -EMSGSIZE;
774 error:
775         genlmsg_cancel(skb, ovs_header);
776         return err;
777 }
778
779 /* May not be called with RCU read lock. */
780 static struct sk_buff *ovs_flow_cmd_alloc_info(const struct sw_flow_actions *acts,
781                                                struct genl_info *info,
782                                                bool always)
783 {
784         struct sk_buff *skb;
785
786         if (!always && !ovs_must_notify(info, &ovs_dp_flow_multicast_group))
787                 return NULL;
788
789         skb = genlmsg_new_unicast(ovs_flow_cmd_msg_size(acts), info, GFP_KERNEL);
790
791         if (!skb)
792                 return ERR_PTR(-ENOMEM);
793
794         return skb;
795 }
796
797 /* Called with ovs_mutex. */
798 static struct sk_buff *ovs_flow_cmd_build_info(const struct sw_flow *flow,
799                                                int dp_ifindex,
800                                                struct genl_info *info, u8 cmd,
801                                                bool always)
802 {
803         struct sk_buff *skb;
804         int retval;
805
806         skb = ovs_flow_cmd_alloc_info(ovsl_dereference(flow->sf_acts), info,
807                                       always);
808         if (!skb || IS_ERR(skb))
809                 return skb;
810
811         retval = ovs_flow_cmd_fill_info(flow, dp_ifindex, skb,
812                                         info->snd_portid, info->snd_seq, 0,
813                                         cmd);
814         BUG_ON(retval < 0);
815         return skb;
816 }
817
818 static int ovs_flow_cmd_new(struct sk_buff *skb, struct genl_info *info)
819 {
820         struct nlattr **a = info->attrs;
821         struct ovs_header *ovs_header = info->userhdr;
822         struct sw_flow *flow, *new_flow;
823         struct sw_flow_mask mask;
824         struct sk_buff *reply;
825         struct datapath *dp;
826         struct sw_flow_actions *acts;
827         struct sw_flow_match match;
828         int error;
829
830         /* Must have key and actions. */
831         error = -EINVAL;
832         if (!a[OVS_FLOW_ATTR_KEY])
833                 goto error;
834         if (!a[OVS_FLOW_ATTR_ACTIONS])
835                 goto error;
836
837         /* Most of the time we need to allocate a new flow, do it before
838          * locking. */
839         new_flow = ovs_flow_alloc();
840         if (IS_ERR(new_flow)) {
841                 error = PTR_ERR(new_flow);
842                 goto error;
843         }
844
845         /* Extract key. */
846         ovs_match_init(&match, &new_flow->unmasked_key, &mask);
847         error = ovs_nla_get_match(&match,
848                                   a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
849         if (error)
850                 goto err_kfree_flow;
851
852         ovs_flow_mask_key(&new_flow->key, &new_flow->unmasked_key, &mask);
853
854         /* Validate actions. */
855         acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_FLOW_ATTR_ACTIONS]));
856         error = PTR_ERR(acts);
857         if (IS_ERR(acts))
858                 goto err_kfree_flow;
859
860         error = ovs_nla_copy_actions(a[OVS_FLOW_ATTR_ACTIONS], &new_flow->key,
861                                      0, &acts);
862         if (error) {
863                 OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
864                 goto err_kfree_acts;
865         }
866
867         reply = ovs_flow_cmd_alloc_info(acts, info, false);
868         if (IS_ERR(reply)) {
869                 error = PTR_ERR(reply);
870                 goto err_kfree_acts;
871         }
872
873         ovs_lock();
874         dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
875         if (unlikely(!dp)) {
876                 error = -ENODEV;
877                 goto err_unlock_ovs;
878         }
879         /* Check if this is a duplicate flow */
880         flow = ovs_flow_tbl_lookup(&dp->table, &new_flow->unmasked_key);
881         if (likely(!flow)) {
882                 rcu_assign_pointer(new_flow->sf_acts, acts);
883
884                 /* Put flow in bucket. */
885                 error = ovs_flow_tbl_insert(&dp->table, new_flow, &mask);
886                 if (unlikely(error)) {
887                         acts = NULL;
888                         goto err_unlock_ovs;
889                 }
890
891                 if (unlikely(reply)) {
892                         error = ovs_flow_cmd_fill_info(new_flow,
893                                                        ovs_header->dp_ifindex,
894                                                        reply, info->snd_portid,
895                                                        info->snd_seq, 0,
896                                                        OVS_FLOW_CMD_NEW);
897                         BUG_ON(error < 0);
898                 }
899                 ovs_unlock();
900         } else {
901                 struct sw_flow_actions *old_acts;
902
903                 /* Bail out if we're not allowed to modify an existing flow.
904                  * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
905                  * because Generic Netlink treats the latter as a dump
906                  * request.  We also accept NLM_F_EXCL in case that bug ever
907                  * gets fixed.
908                  */
909                 if (unlikely(info->nlhdr->nlmsg_flags & (NLM_F_CREATE
910                                                          | NLM_F_EXCL))) {
911                         error = -EEXIST;
912                         goto err_unlock_ovs;
913                 }
914                 /* The unmasked key has to be the same for flow updates. */
915                 if (unlikely(!ovs_flow_cmp_unmasked_key(flow, &match))) {
916                         error = -EEXIST;
917                         goto err_unlock_ovs;
918                 }
919                 /* Update actions. */
920                 old_acts = ovsl_dereference(flow->sf_acts);
921                 rcu_assign_pointer(flow->sf_acts, acts);
922
923                 if (unlikely(reply)) {
924                         error = ovs_flow_cmd_fill_info(flow,
925                                                        ovs_header->dp_ifindex,
926                                                        reply, info->snd_portid,
927                                                        info->snd_seq, 0,
928                                                        OVS_FLOW_CMD_NEW);
929                         BUG_ON(error < 0);
930                 }
931                 ovs_unlock();
932
933                 ovs_nla_free_flow_actions(old_acts);
934                 ovs_flow_free(new_flow, false);
935         }
936
937         if (reply)
938                 ovs_notify(&dp_flow_genl_family, &ovs_dp_flow_multicast_group, reply, info);
939         return 0;
940
941 err_unlock_ovs:
942         ovs_unlock();
943         kfree_skb(reply);
944 err_kfree_acts:
945         kfree(acts);
946 err_kfree_flow:
947         ovs_flow_free(new_flow, false);
948 error:
949         return error;
950 }
951
952 static int ovs_flow_cmd_set(struct sk_buff *skb, struct genl_info *info)
953 {
954         struct nlattr **a = info->attrs;
955         struct ovs_header *ovs_header = info->userhdr;
956         struct sw_flow_key key, masked_key;
957         struct sw_flow *flow;
958         struct sw_flow_mask mask;
959         struct sk_buff *reply = NULL;
960         struct datapath *dp;
961         struct sw_flow_actions *old_acts = NULL, *acts = NULL;
962         struct sw_flow_match match;
963         int error;
964
965         /* Extract key. */
966         error = -EINVAL;
967         if (!a[OVS_FLOW_ATTR_KEY])
968                 goto error;
969
970         ovs_match_init(&match, &key, &mask);
971         error = ovs_nla_get_match(&match,
972                                   a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
973         if (error)
974                 goto error;
975
976         /* Validate actions. */
977         if (a[OVS_FLOW_ATTR_ACTIONS]) {
978                 acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_FLOW_ATTR_ACTIONS]));
979                 error = PTR_ERR(acts);
980                 if (IS_ERR(acts))
981                         goto error;
982
983                 ovs_flow_mask_key(&masked_key, &key, &mask);
984                 error = ovs_nla_copy_actions(a[OVS_FLOW_ATTR_ACTIONS],
985                                              &masked_key, 0, &acts);
986                 if (error) {
987                         OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
988                         goto err_kfree_acts;
989                 }
990         }
991
992         /* Can allocate before locking if have acts. */
993         if (acts) {
994                 reply = ovs_flow_cmd_alloc_info(acts, info, false);
995                 if (IS_ERR(reply)) {
996                         error = PTR_ERR(reply);
997                         goto err_kfree_acts;
998                 }
999         }
1000
1001         ovs_lock();
1002         dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1003         if (unlikely(!dp)) {
1004                 error = -ENODEV;
1005                 goto err_unlock_ovs;
1006         }
1007         /* Check that the flow exists. */
1008         flow = ovs_flow_tbl_lookup(&dp->table, &key);
1009         if (unlikely(!flow)) {
1010                 error = -ENOENT;
1011                 goto err_unlock_ovs;
1012         }
1013         /* The unmasked key has to be the same for flow updates. */
1014         if (unlikely(!ovs_flow_cmp_unmasked_key(flow, &match))) {
1015                 error = -EEXIST;
1016                 goto err_unlock_ovs;
1017         }
1018         /* Update actions, if present. */
1019         if (likely(acts)) {
1020                 old_acts = ovsl_dereference(flow->sf_acts);
1021                 rcu_assign_pointer(flow->sf_acts, acts);
1022
1023                 if (unlikely(reply)) {
1024                         error = ovs_flow_cmd_fill_info(flow,
1025                                                        ovs_header->dp_ifindex,
1026                                                        reply, info->snd_portid,
1027                                                        info->snd_seq, 0,
1028                                                        OVS_FLOW_CMD_NEW);
1029                         BUG_ON(error < 0);
1030                 }
1031         } else {
1032                 /* Could not alloc without acts before locking. */
1033                 reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex,
1034                                                 info, OVS_FLOW_CMD_NEW, false);
1035                 if (unlikely(IS_ERR(reply))) {
1036                         error = PTR_ERR(reply);
1037                         goto err_unlock_ovs;
1038                 }
1039         }
1040
1041         /* Clear stats. */
1042         if (a[OVS_FLOW_ATTR_CLEAR])
1043                 ovs_flow_stats_clear(flow);
1044         ovs_unlock();
1045
1046         if (reply)
1047                 ovs_notify(&dp_flow_genl_family, &ovs_dp_flow_multicast_group, reply, info);
1048         if (old_acts)
1049                 ovs_nla_free_flow_actions(old_acts);
1050         return 0;
1051
1052 err_unlock_ovs:
1053         ovs_unlock();
1054         kfree_skb(reply);
1055 err_kfree_acts:
1056         kfree(acts);
1057 error:
1058         return error;
1059 }
1060
1061 static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1062 {
1063         struct nlattr **a = info->attrs;
1064         struct ovs_header *ovs_header = info->userhdr;
1065         struct sw_flow_key key;
1066         struct sk_buff *reply;
1067         struct sw_flow *flow;
1068         struct datapath *dp;
1069         struct sw_flow_match match;
1070         int err;
1071
1072         if (!a[OVS_FLOW_ATTR_KEY]) {
1073                 OVS_NLERR("Flow get message rejected, Key attribute missing.\n");
1074                 return -EINVAL;
1075         }
1076
1077         ovs_match_init(&match, &key, NULL);
1078         err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL);
1079         if (err)
1080                 return err;
1081
1082         ovs_lock();
1083         dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1084         if (!dp) {
1085                 err = -ENODEV;
1086                 goto unlock;
1087         }
1088
1089         flow = ovs_flow_tbl_lookup(&dp->table, &key);
1090         if (!flow || !ovs_flow_cmp_unmasked_key(flow, &match)) {
1091                 err = -ENOENT;
1092                 goto unlock;
1093         }
1094
1095         reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex, info,
1096                                         OVS_FLOW_CMD_NEW, true);
1097         if (IS_ERR(reply)) {
1098                 err = PTR_ERR(reply);
1099                 goto unlock;
1100         }
1101
1102         ovs_unlock();
1103         return genlmsg_reply(reply, info);
1104 unlock:
1105         ovs_unlock();
1106         return err;
1107 }
1108
1109 static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1110 {
1111         struct nlattr **a = info->attrs;
1112         struct ovs_header *ovs_header = info->userhdr;
1113         struct sw_flow_key key;
1114         struct sk_buff *reply;
1115         struct sw_flow *flow;
1116         struct datapath *dp;
1117         struct sw_flow_match match;
1118         int err;
1119
1120         if (likely(a[OVS_FLOW_ATTR_KEY])) {
1121                 ovs_match_init(&match, &key, NULL);
1122                 err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL);
1123                 if (unlikely(err))
1124                         return err;
1125         }
1126
1127         ovs_lock();
1128         dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1129         if (unlikely(!dp)) {
1130                 err = -ENODEV;
1131                 goto unlock;
1132         }
1133         if (unlikely(!a[OVS_FLOW_ATTR_KEY])) {
1134                 err = ovs_flow_tbl_flush(&dp->table);
1135                 goto unlock;
1136         }
1137         flow = ovs_flow_tbl_lookup(&dp->table, &key);
1138         if (unlikely(!flow || !ovs_flow_cmp_unmasked_key(flow, &match))) {
1139                 err = -ENOENT;
1140                 goto unlock;
1141         }
1142
1143         ovs_flow_tbl_remove(&dp->table, flow);
1144         ovs_unlock();
1145
1146         reply = ovs_flow_cmd_alloc_info((const struct sw_flow_actions __force *)flow->sf_acts,
1147                                         info, false);
1148
1149         if (likely(reply)) {
1150                 if (likely(!IS_ERR(reply))) {
1151                         rcu_read_lock(); /* Keep RCU checker happy. */
1152                         err = ovs_flow_cmd_fill_info(flow,
1153                                                      ovs_header->dp_ifindex,
1154                                                      reply, info->snd_portid,
1155                                                      info->snd_seq, 0,
1156                                                      OVS_FLOW_CMD_DEL);
1157                         rcu_read_unlock();
1158                         BUG_ON(err < 0);
1159                         ovs_notify(&dp_flow_genl_family, &ovs_dp_flow_multicast_group, reply, info);
1160                 } else {
1161                         genl_set_err(&dp_flow_genl_family, sock_net(skb->sk), 0,
1162                                      GROUP_ID(&ovs_dp_flow_multicast_group), PTR_ERR(reply));
1163
1164                 }
1165         }
1166
1167         ovs_flow_free(flow, true);
1168         return 0;
1169 unlock:
1170         ovs_unlock();
1171         return err;
1172 }
1173
1174 static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1175 {
1176         struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1177         struct table_instance *ti;
1178         struct datapath *dp;
1179
1180         rcu_read_lock();
1181         dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1182         if (!dp) {
1183                 rcu_read_unlock();
1184                 return -ENODEV;
1185         }
1186
1187         ti = rcu_dereference(dp->table.ti);
1188         for (;;) {
1189                 struct sw_flow *flow;
1190                 u32 bucket, obj;
1191
1192                 bucket = cb->args[0];
1193                 obj = cb->args[1];
1194                 flow = ovs_flow_tbl_dump_next(ti, &bucket, &obj);
1195                 if (!flow)
1196                         break;
1197
1198                 if (ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex, skb,
1199                                            NETLINK_CB(cb->skb).portid,
1200                                            cb->nlh->nlmsg_seq, NLM_F_MULTI,
1201                                            OVS_FLOW_CMD_NEW) < 0)
1202                         break;
1203
1204                 cb->args[0] = bucket;
1205                 cb->args[1] = obj;
1206         }
1207         rcu_read_unlock();
1208         return skb->len;
1209 }
1210
1211 static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
1212         [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
1213         [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
1214         [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
1215 };
1216
1217 static struct genl_ops dp_flow_genl_ops[] = {
1218         { .cmd = OVS_FLOW_CMD_NEW,
1219           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1220           .policy = flow_policy,
1221           .doit = ovs_flow_cmd_new
1222         },
1223         { .cmd = OVS_FLOW_CMD_DEL,
1224           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1225           .policy = flow_policy,
1226           .doit = ovs_flow_cmd_del
1227         },
1228         { .cmd = OVS_FLOW_CMD_GET,
1229           .flags = 0,               /* OK for unprivileged users. */
1230           .policy = flow_policy,
1231           .doit = ovs_flow_cmd_get,
1232           .dumpit = ovs_flow_cmd_dump
1233         },
1234         { .cmd = OVS_FLOW_CMD_SET,
1235           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1236           .policy = flow_policy,
1237           .doit = ovs_flow_cmd_set,
1238         },
1239 };
1240
1241 static struct genl_family dp_flow_genl_family = {
1242         .id = GENL_ID_GENERATE,
1243         .hdrsize = sizeof(struct ovs_header),
1244         .name = OVS_FLOW_FAMILY,
1245         .version = OVS_FLOW_VERSION,
1246         .maxattr = OVS_FLOW_ATTR_MAX,
1247         .netnsok = true,
1248         .parallel_ops = true,
1249         .ops = dp_flow_genl_ops,
1250         .n_ops = ARRAY_SIZE(dp_flow_genl_ops),
1251         .mcgrps = &ovs_dp_flow_multicast_group,
1252         .n_mcgrps = 1,
1253 };
1254
1255 static size_t ovs_dp_cmd_msg_size(void)
1256 {
1257         size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
1258
1259         msgsize += nla_total_size(IFNAMSIZ);
1260         msgsize += nla_total_size(sizeof(struct ovs_dp_stats));
1261         msgsize += nla_total_size(sizeof(struct ovs_dp_megaflow_stats));
1262         msgsize += nla_total_size(sizeof(u32)); /* OVS_DP_ATTR_USER_FEATURES */
1263
1264         return msgsize;
1265 }
1266
1267 /* Called with ovs_mutex or RCU read lock. */
1268 static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
1269                                 u32 portid, u32 seq, u32 flags, u8 cmd)
1270 {
1271         struct ovs_header *ovs_header;
1272         struct ovs_dp_stats dp_stats;
1273         struct ovs_dp_megaflow_stats dp_megaflow_stats;
1274         int err;
1275
1276         ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
1277                                    flags, cmd);
1278         if (!ovs_header)
1279                 goto error;
1280
1281         ovs_header->dp_ifindex = get_dpifindex(dp);
1282
1283         err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
1284         if (err)
1285                 goto nla_put_failure;
1286
1287         get_dp_stats(dp, &dp_stats, &dp_megaflow_stats);
1288         if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats),
1289                         &dp_stats))
1290                 goto nla_put_failure;
1291
1292         if (nla_put(skb, OVS_DP_ATTR_MEGAFLOW_STATS,
1293                         sizeof(struct ovs_dp_megaflow_stats),
1294                         &dp_megaflow_stats))
1295                 goto nla_put_failure;
1296
1297         if (nla_put_u32(skb, OVS_DP_ATTR_USER_FEATURES, dp->user_features))
1298                 goto nla_put_failure;
1299
1300         return genlmsg_end(skb, ovs_header);
1301
1302 nla_put_failure:
1303         genlmsg_cancel(skb, ovs_header);
1304 error:
1305         return -EMSGSIZE;
1306 }
1307
1308 static struct sk_buff *ovs_dp_cmd_alloc_info(struct genl_info *info)
1309 {
1310         return genlmsg_new_unicast(ovs_dp_cmd_msg_size(), info, GFP_KERNEL);
1311 }
1312
1313 /* Called with rcu_read_lock or ovs_mutex. */
1314 static struct datapath *lookup_datapath(struct net *net,
1315                                         struct ovs_header *ovs_header,
1316                                         struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1317 {
1318         struct datapath *dp;
1319
1320         if (!a[OVS_DP_ATTR_NAME])
1321                 dp = get_dp(net, ovs_header->dp_ifindex);
1322         else {
1323                 struct vport *vport;
1324
1325                 vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
1326                 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
1327         }
1328         return dp ? dp : ERR_PTR(-ENODEV);
1329 }
1330
1331 static void ovs_dp_reset_user_features(struct sk_buff *skb, struct genl_info *info)
1332 {
1333         struct datapath *dp;
1334
1335         dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1336         if (IS_ERR(dp))
1337                 return;
1338
1339         WARN(dp->user_features, "Dropping previously announced user features\n");
1340         dp->user_features = 0;
1341 }
1342
1343 static void ovs_dp_change(struct datapath *dp, struct nlattr **a)
1344 {
1345         if (a[OVS_DP_ATTR_USER_FEATURES])
1346                 dp->user_features = nla_get_u32(a[OVS_DP_ATTR_USER_FEATURES]);
1347 }
1348
1349 static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1350 {
1351         struct nlattr **a = info->attrs;
1352         struct vport_parms parms;
1353         struct sk_buff *reply;
1354         struct datapath *dp;
1355         struct vport *vport;
1356         struct ovs_net *ovs_net;
1357         int err, i;
1358
1359         err = -EINVAL;
1360         if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1361                 goto err;
1362
1363         reply = ovs_dp_cmd_alloc_info(info);
1364         if (!reply)
1365                 return -ENOMEM;
1366
1367         err = -ENOMEM;
1368         dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1369         if (dp == NULL)
1370                 goto err_free_reply;
1371
1372         ovs_dp_set_net(dp, hold_net(sock_net(skb->sk)));
1373
1374         /* Allocate table. */
1375         err = ovs_flow_tbl_init(&dp->table);
1376         if (err)
1377                 goto err_free_dp;
1378
1379         dp->stats_percpu = alloc_percpu(struct dp_stats_percpu);
1380         if (!dp->stats_percpu) {
1381                 err = -ENOMEM;
1382                 goto err_destroy_table;
1383         }
1384
1385         for_each_possible_cpu(i) {
1386                 struct dp_stats_percpu *dpath_stats;
1387                 dpath_stats = per_cpu_ptr(dp->stats_percpu, i);
1388                 u64_stats_init(&dpath_stats->sync);
1389         }
1390
1391         dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
1392                             GFP_KERNEL);
1393         if (!dp->ports) {
1394                 err = -ENOMEM;
1395                 goto err_destroy_percpu;
1396         }
1397
1398         for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1399                 INIT_HLIST_HEAD(&dp->ports[i]);
1400
1401         /* Set up our datapath device. */
1402         parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1403         parms.type = OVS_VPORT_TYPE_INTERNAL;
1404         parms.options = NULL;
1405         parms.dp = dp;
1406         parms.port_no = OVSP_LOCAL;
1407         parms.upcall_portids = a[OVS_DP_ATTR_UPCALL_PID];
1408
1409         ovs_dp_change(dp, a);
1410
1411         /* So far only local changes have been made, now need the lock. */
1412         ovs_lock();
1413
1414         vport = new_vport(&parms);
1415         if (IS_ERR(vport)) {
1416                 err = PTR_ERR(vport);
1417                 if (err == -EBUSY)
1418                         err = -EEXIST;
1419
1420                 if (err == -EEXIST) {
1421                         /* An outdated user space instance that does not understand
1422                          * the concept of user_features has attempted to create a new
1423                          * datapath and is likely to reuse it. Drop all user features.
1424                          */
1425                         if (info->genlhdr->version < OVS_DP_VER_FEATURES)
1426                                 ovs_dp_reset_user_features(skb, info);
1427                 }
1428
1429                 goto err_destroy_ports_array;
1430         }
1431
1432         err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1433                                    info->snd_seq, 0, OVS_DP_CMD_NEW);
1434         BUG_ON(err < 0);
1435
1436         ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
1437         list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
1438
1439         ovs_unlock();
1440
1441         ovs_notify(&dp_datapath_genl_family, &ovs_dp_datapath_multicast_group, reply, info);
1442         return 0;
1443
1444 err_destroy_ports_array:
1445         ovs_unlock();
1446         kfree(dp->ports);
1447 err_destroy_percpu:
1448         free_percpu(dp->stats_percpu);
1449 err_destroy_table:
1450         ovs_flow_tbl_destroy(&dp->table, false);
1451 err_free_dp:
1452         release_net(ovs_dp_get_net(dp));
1453         kfree(dp);
1454 err_free_reply:
1455         kfree_skb(reply);
1456 err:
1457         return err;
1458 }
1459
1460 /* Called with ovs_mutex. */
1461 static void __dp_destroy(struct datapath *dp)
1462 {
1463         int i;
1464
1465         for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1466                 struct vport *vport;
1467                 struct hlist_node *n;
1468
1469                 hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
1470                         if (vport->port_no != OVSP_LOCAL)
1471                                 ovs_dp_detach_port(vport);
1472         }
1473
1474         list_del_rcu(&dp->list_node);
1475
1476         /* OVSP_LOCAL is datapath internal port. We need to make sure that
1477          * all ports in datapath are destroyed first before freeing datapath.
1478          */
1479         ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
1480
1481         /* RCU destroy the flow table */
1482         ovs_flow_tbl_destroy(&dp->table, true);
1483
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);