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