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