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