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