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