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