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