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