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