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