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