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