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