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