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