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