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