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