datapath: Constify various function arguments
[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 #if LINUX_VERSION_CODE >= KERNEL_VERSION(3,14,0)
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
533         err = -EINVAL;
534         if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
535             !a[OVS_PACKET_ATTR_ACTIONS])
536                 goto err;
537
538         len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
539         packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
540         err = -ENOMEM;
541         if (!packet)
542                 goto err;
543         skb_reserve(packet, NET_IP_ALIGN);
544
545         nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
546
547         skb_reset_mac_header(packet);
548         eth = eth_hdr(packet);
549
550         /* Normally, setting the skb 'protocol' field would be handled by a
551          * call to eth_type_trans(), but it assumes there's a sending
552          * device, which we may not have. */
553         if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN)
554                 packet->protocol = eth->h_proto;
555         else
556                 packet->protocol = htons(ETH_P_802_2);
557
558         /* Build an sw_flow for sending this packet. */
559         flow = ovs_flow_alloc();
560         err = PTR_ERR(flow);
561         if (IS_ERR(flow))
562                 goto err_kfree_skb;
563
564         err = ovs_flow_key_extract_userspace(a[OVS_PACKET_ATTR_KEY], packet,
565                                              &flow->key);
566         if (err)
567                 goto err_flow_free;
568
569         err = ovs_nla_copy_actions(a[OVS_PACKET_ATTR_ACTIONS],
570                                    &flow->key, &acts);
571         if (err)
572                 goto err_flow_free;
573
574         rcu_assign_pointer(flow->sf_acts, acts);
575         OVS_CB(packet)->egress_tun_info = NULL;
576         packet->priority = flow->key.phy.priority;
577         packet->mark = flow->key.phy.skb_mark;
578
579         rcu_read_lock();
580         dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
581         err = -ENODEV;
582         if (!dp)
583                 goto err_unlock;
584
585         input_vport = ovs_vport_rcu(dp, flow->key.phy.in_port);
586         if (!input_vport)
587                 input_vport = ovs_vport_rcu(dp, OVSP_LOCAL);
588
589         if (!input_vport)
590                 goto err_unlock;
591
592         OVS_CB(packet)->input_vport = input_vport;
593         sf_acts = rcu_dereference(flow->sf_acts);
594
595         local_bh_disable();
596         err = ovs_execute_actions(dp, packet, &flow->key, sf_acts);
597         local_bh_enable();
598         rcu_read_unlock();
599
600         ovs_flow_free(flow, false);
601         return err;
602
603 err_unlock:
604         rcu_read_unlock();
605 err_flow_free:
606         ovs_flow_free(flow, false);
607 err_kfree_skb:
608         kfree_skb(packet);
609 err:
610         return err;
611 }
612
613 static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
614         [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
615         [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
616         [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
617 };
618
619 static struct genl_ops dp_packet_genl_ops[] = {
620         { .cmd = OVS_PACKET_CMD_EXECUTE,
621           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
622           .policy = packet_policy,
623           .doit = ovs_packet_cmd_execute
624         }
625 };
626
627 static struct genl_family dp_packet_genl_family = {
628         .id = GENL_ID_GENERATE,
629         .hdrsize = sizeof(struct ovs_header),
630         .name = OVS_PACKET_FAMILY,
631         .version = OVS_PACKET_VERSION,
632         .maxattr = OVS_PACKET_ATTR_MAX,
633         .netnsok = true,
634         .parallel_ops = true,
635         .ops = dp_packet_genl_ops,
636         .n_ops = ARRAY_SIZE(dp_packet_genl_ops),
637 };
638
639 static void get_dp_stats(const struct datapath *dp, struct ovs_dp_stats *stats,
640                          struct ovs_dp_megaflow_stats *mega_stats)
641 {
642         int i;
643
644         memset(mega_stats, 0, sizeof(*mega_stats));
645
646         stats->n_flows = ovs_flow_tbl_count(&dp->table);
647         mega_stats->n_masks = ovs_flow_tbl_num_masks(&dp->table);
648
649         stats->n_hit = stats->n_missed = stats->n_lost = 0;
650
651         for_each_possible_cpu(i) {
652                 const struct dp_stats_percpu *percpu_stats;
653                 struct dp_stats_percpu local_stats;
654                 unsigned int start;
655
656                 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
657
658                 do {
659                         start = u64_stats_fetch_begin_irq(&percpu_stats->syncp);
660                         local_stats = *percpu_stats;
661                 } while (u64_stats_fetch_retry_irq(&percpu_stats->syncp, start));
662
663                 stats->n_hit += local_stats.n_hit;
664                 stats->n_missed += local_stats.n_missed;
665                 stats->n_lost += local_stats.n_lost;
666                 mega_stats->n_mask_hit += local_stats.n_mask_hit;
667         }
668 }
669
670 static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts)
671 {
672         return NLMSG_ALIGN(sizeof(struct ovs_header))
673                 + nla_total_size(ovs_key_attr_size()) /* OVS_FLOW_ATTR_KEY */
674                 + nla_total_size(ovs_key_attr_size()) /* OVS_FLOW_ATTR_MASK */
675                 + nla_total_size(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
676                 + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
677                 + nla_total_size(8) /* OVS_FLOW_ATTR_USED */
678                 + nla_total_size(acts->actions_len); /* OVS_FLOW_ATTR_ACTIONS */
679 }
680
681 /* Called with ovs_mutex or RCU read lock. */
682 static int ovs_flow_cmd_fill_match(const struct sw_flow *flow,
683                                    struct sk_buff *skb)
684 {
685         struct nlattr *nla;
686         int err;
687
688         /* Fill flow key. */
689         nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
690         if (!nla)
691                 return -EMSGSIZE;
692
693         err = ovs_nla_put_flow(&flow->unmasked_key,
694                                &flow->unmasked_key, skb);
695         if (err)
696                 return err;
697         nla_nest_end(skb, nla);
698
699         /* Fill flow mask. */
700         nla = nla_nest_start(skb, OVS_FLOW_ATTR_MASK);
701         if (!nla)
702                 return -EMSGSIZE;
703
704         err = ovs_nla_put_flow(&flow->key, &flow->mask->key, skb);
705         if (err)
706                 return err;
707         nla_nest_end(skb, nla);
708
709         return 0;
710 }
711
712 /* Called with ovs_mutex or RCU read lock. */
713 static int ovs_flow_cmd_fill_stats(const struct sw_flow *flow,
714                                    struct sk_buff *skb)
715 {
716         struct ovs_flow_stats stats;
717         __be16 tcp_flags;
718         unsigned long used;
719
720         ovs_flow_stats_get(flow, &stats, &used, &tcp_flags);
721
722         if (used &&
723             nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
724                 return -EMSGSIZE;
725
726         if (stats.n_packets &&
727             nla_put(skb, OVS_FLOW_ATTR_STATS, sizeof(struct ovs_flow_stats), &stats))
728                 return -EMSGSIZE;
729
730         if ((u8)ntohs(tcp_flags) &&
731              nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, (u8)ntohs(tcp_flags)))
732                 return -EMSGSIZE;
733
734         return 0;
735 }
736
737 /* Called with ovs_mutex or RCU read lock. */
738 static int ovs_flow_cmd_fill_actions(const struct sw_flow *flow,
739                                      struct sk_buff *skb, int skb_orig_len)
740 {
741         struct nlattr *start;
742         int err;
743
744         /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
745          * this is the first flow to be dumped into 'skb'.  This is unusual for
746          * Netlink but individual action lists can be longer than
747          * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
748          * The userspace caller can always fetch the actions separately if it
749          * really wants them.  (Most userspace callers in fact don't care.)
750          *
751          * This can only fail for dump operations because the skb is always
752          * properly sized for single flows.
753          */
754         start = nla_nest_start(skb, OVS_FLOW_ATTR_ACTIONS);
755         if (start) {
756                 const struct sw_flow_actions *sf_acts;
757
758                 sf_acts = rcu_dereference_ovsl(flow->sf_acts);
759                 err = ovs_nla_put_actions(sf_acts->actions,
760                                           sf_acts->actions_len, skb);
761
762                 if (!err)
763                         nla_nest_end(skb, start);
764                 else {
765                         if (skb_orig_len)
766                                 return err;
767
768                         nla_nest_cancel(skb, start);
769                 }
770         } else if (skb_orig_len) {
771                 return -EMSGSIZE;
772         }
773
774         return 0;
775 }
776
777 /* Called with ovs_mutex or RCU read lock. */
778 static int ovs_flow_cmd_fill_info(const struct sw_flow *flow, int dp_ifindex,
779                                   struct sk_buff *skb, u32 portid,
780                                   u32 seq, u32 flags, u8 cmd)
781 {
782         const int skb_orig_len = skb->len;
783         struct ovs_header *ovs_header;
784         int err;
785
786         ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family, flags, cmd);
787         if (!ovs_header)
788                 return -EMSGSIZE;
789         ovs_header->dp_ifindex = dp_ifindex;
790
791         err = ovs_flow_cmd_fill_match(flow, skb);
792         if (err)
793                 goto error;
794
795         err = ovs_flow_cmd_fill_stats(flow, skb);
796         if (err)
797                 goto error;
798
799         err = ovs_flow_cmd_fill_actions(flow, skb, skb_orig_len);
800         if (err)
801                 goto error;
802
803         return genlmsg_end(skb, ovs_header);
804
805 error:
806         genlmsg_cancel(skb, ovs_header);
807         return err;
808 }
809
810 /* May not be called with RCU read lock. */
811 static struct sk_buff *ovs_flow_cmd_alloc_info(const struct sw_flow_actions *acts,
812                                                struct genl_info *info,
813                                                bool always)
814 {
815         struct sk_buff *skb;
816
817         if (!always && !ovs_must_notify(&dp_flow_genl_family, info,
818                                         GROUP_ID(&ovs_dp_flow_multicast_group)))
819                 return NULL;
820
821         skb = genlmsg_new_unicast(ovs_flow_cmd_msg_size(acts), info, GFP_KERNEL);
822
823         if (!skb)
824                 return ERR_PTR(-ENOMEM);
825
826         return skb;
827 }
828
829 /* Called with ovs_mutex. */
830 static struct sk_buff *ovs_flow_cmd_build_info(struct datapath *dp,
831                                                const struct sw_flow *flow,
832                                                int dp_ifindex,
833                                                struct genl_info *info, u8 cmd,
834                                                bool always)
835 {
836         struct sk_buff *skb;
837         int retval;
838
839         skb = ovs_flow_cmd_alloc_info(ovsl_dereference(flow->sf_acts), info,
840                                       always);
841         if (IS_ERR_OR_NULL(skb))
842                 return skb;
843
844         retval = ovs_flow_cmd_fill_info(flow, dp_ifindex, skb,
845                                         info->snd_portid, info->snd_seq, 0,
846                                         cmd);
847         BUG_ON(retval < 0);
848         return skb;
849 }
850
851 static int ovs_flow_cmd_new(struct sk_buff *skb, struct genl_info *info)
852 {
853         struct nlattr **a = info->attrs;
854         struct ovs_header *ovs_header = info->userhdr;
855         struct sw_flow *flow, *new_flow;
856         struct sw_flow_mask mask;
857         struct sk_buff *reply;
858         struct datapath *dp;
859         struct sw_flow_actions *acts;
860         struct sw_flow_match match;
861         int error;
862
863         /* Must have key and actions. */
864         error = -EINVAL;
865         if (!a[OVS_FLOW_ATTR_KEY]) {
866                 OVS_NLERR("Flow key attribute not present in new flow.\n");
867                 goto error;
868         }
869         if (!a[OVS_FLOW_ATTR_ACTIONS]) {
870                 OVS_NLERR("Flow actions attribute not present in new flow.\n");
871                 goto error;
872         }
873
874         /* Most of the time we need to allocate a new flow, do it before
875          * locking. */
876         new_flow = ovs_flow_alloc();
877         if (IS_ERR(new_flow)) {
878                 error = PTR_ERR(new_flow);
879                 goto error;
880         }
881
882         /* Extract key. */
883         ovs_match_init(&match, &new_flow->unmasked_key, &mask);
884         error = ovs_nla_get_match(&match,
885                                   a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
886         if (error)
887                 goto err_kfree_flow;
888
889         ovs_flow_mask_key(&new_flow->key, &new_flow->unmasked_key, &mask);
890
891         /* Validate actions. */
892         error = ovs_nla_copy_actions(a[OVS_FLOW_ATTR_ACTIONS], &new_flow->key,
893                                      &acts);
894         if (error) {
895                 OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
896                 goto err_kfree_flow;
897         }
898
899         reply = ovs_flow_cmd_alloc_info(acts, info, false);
900         if (IS_ERR(reply)) {
901                 error = PTR_ERR(reply);
902                 goto err_kfree_acts;
903         }
904
905         ovs_lock();
906         dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
907         if (unlikely(!dp)) {
908                 error = -ENODEV;
909                 goto err_unlock_ovs;
910         }
911         /* Check if this is a duplicate flow */
912         flow = ovs_flow_tbl_lookup(&dp->table, &new_flow->unmasked_key);
913         if (likely(!flow)) {
914                 rcu_assign_pointer(new_flow->sf_acts, acts);
915
916                 /* Put flow in bucket. */
917                 error = ovs_flow_tbl_insert(&dp->table, new_flow, &mask);
918                 if (unlikely(error)) {
919                         acts = NULL;
920                         goto err_unlock_ovs;
921                 }
922
923                 if (unlikely(reply)) {
924                         error = ovs_flow_cmd_fill_info(new_flow,
925                                                        ovs_header->dp_ifindex,
926                                                        reply, info->snd_portid,
927                                                        info->snd_seq, 0,
928                                                        OVS_FLOW_CMD_NEW);
929                         BUG_ON(error < 0);
930                 }
931                 ovs_unlock();
932         } else {
933                 struct sw_flow_actions *old_acts;
934
935                 /* Bail out if we're not allowed to modify an existing flow.
936                  * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
937                  * because Generic Netlink treats the latter as a dump
938                  * request.  We also accept NLM_F_EXCL in case that bug ever
939                  * gets fixed.
940                  */
941                 if (unlikely(info->nlhdr->nlmsg_flags & (NLM_F_CREATE
942                                                          | NLM_F_EXCL))) {
943                         error = -EEXIST;
944                         goto err_unlock_ovs;
945                 }
946                 /* The unmasked key has to be the same for flow updates. */
947                 if (unlikely(!ovs_flow_cmp_unmasked_key(flow, &match))) {
948                         /* Look for any overlapping flow. */
949                         flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
950                         if (!flow) {
951                                 error = -ENOENT;
952                                 goto err_unlock_ovs;
953                         }
954                 }
955                 /* Update actions. */
956                 old_acts = ovsl_dereference(flow->sf_acts);
957                 rcu_assign_pointer(flow->sf_acts, acts);
958
959                 if (unlikely(reply)) {
960                         error = ovs_flow_cmd_fill_info(flow,
961                                                        ovs_header->dp_ifindex,
962                                                        reply, info->snd_portid,
963                                                        info->snd_seq, 0,
964                                                        OVS_FLOW_CMD_NEW);
965                         BUG_ON(error < 0);
966                 }
967                 ovs_unlock();
968
969                 ovs_nla_free_flow_actions(old_acts);
970                 ovs_flow_free(new_flow, false);
971         }
972
973         if (reply)
974                 ovs_notify(&dp_flow_genl_family, &ovs_dp_flow_multicast_group, reply, info);
975         return 0;
976
977 err_unlock_ovs:
978         ovs_unlock();
979         kfree_skb(reply);
980 err_kfree_acts:
981         kfree(acts);
982 err_kfree_flow:
983         ovs_flow_free(new_flow, false);
984 error:
985         return error;
986 }
987
988 /* Factor out action copy to avoid "Wframe-larger-than=1024" warning. */
989 static struct sw_flow_actions *get_flow_actions(const struct nlattr *a,
990                                                 const struct sw_flow_key *key,
991                                                 const struct sw_flow_mask *mask)
992 {
993         struct sw_flow_actions *acts;
994         struct sw_flow_key masked_key;
995         int error;
996
997         ovs_flow_mask_key(&masked_key, key, mask);
998         error = ovs_nla_copy_actions(a, &masked_key, &acts);
999         if (error) {
1000                 OVS_NLERR("Actions may not be safe on all matching packets.\n");
1001                 return ERR_PTR(error);
1002         }
1003
1004         return acts;
1005 }
1006
1007 static int ovs_flow_cmd_set(struct sk_buff *skb, struct genl_info *info)
1008 {
1009         struct nlattr **a = info->attrs;
1010         struct ovs_header *ovs_header = info->userhdr;
1011         struct sw_flow_key key;
1012         struct sw_flow *flow;
1013         struct sw_flow_mask mask;
1014         struct sk_buff *reply = NULL;
1015         struct datapath *dp;
1016         struct sw_flow_actions *old_acts = NULL, *acts = NULL;
1017         struct sw_flow_match match;
1018         int error;
1019
1020         /* Extract key. */
1021         error = -EINVAL;
1022         if (!a[OVS_FLOW_ATTR_KEY]) {
1023                 OVS_NLERR("Flow key attribute not present in set flow.\n");
1024                 goto error;
1025         }
1026
1027         ovs_match_init(&match, &key, &mask);
1028         error = ovs_nla_get_match(&match,
1029                                   a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
1030         if (error)
1031                 goto error;
1032
1033         /* Validate actions. */
1034         if (a[OVS_FLOW_ATTR_ACTIONS]) {
1035                 acts = get_flow_actions(a[OVS_FLOW_ATTR_ACTIONS], &key, &mask);
1036                 if (IS_ERR(acts)) {
1037                         error = PTR_ERR(acts);
1038                         goto error;
1039                 }
1040
1041                 /* Can allocate before locking if have acts. */
1042                 reply = ovs_flow_cmd_alloc_info(acts, info, false);
1043                 if (IS_ERR(reply)) {
1044                         error = PTR_ERR(reply);
1045                         goto err_kfree_acts;
1046                 }
1047         }
1048
1049         ovs_lock();
1050         dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1051         if (unlikely(!dp)) {
1052                 error = -ENODEV;
1053                 goto err_unlock_ovs;
1054         }
1055         /* Check that the flow exists. */
1056         flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1057         if (unlikely(!flow)) {
1058                 error = -ENOENT;
1059                 goto err_unlock_ovs;
1060         }
1061
1062         /* Update actions, if present. */
1063         if (likely(acts)) {
1064                 old_acts = ovsl_dereference(flow->sf_acts);
1065                 rcu_assign_pointer(flow->sf_acts, acts);
1066
1067                 if (unlikely(reply)) {
1068                         error = ovs_flow_cmd_fill_info(flow,
1069                                                        ovs_header->dp_ifindex,
1070                                                        reply, info->snd_portid,
1071                                                        info->snd_seq, 0,
1072                                                        OVS_FLOW_CMD_NEW);
1073                         BUG_ON(error < 0);
1074                 }
1075         } else {
1076                 /* Could not alloc without acts before locking. */
1077                 reply = ovs_flow_cmd_build_info(dp, flow,
1078                                                 ovs_header->dp_ifindex,
1079                                                 info, OVS_FLOW_CMD_NEW, false);
1080                 if (unlikely(IS_ERR(reply))) {
1081                         error = PTR_ERR(reply);
1082                         goto err_unlock_ovs;
1083                 }
1084         }
1085
1086         /* Clear stats. */
1087         if (a[OVS_FLOW_ATTR_CLEAR])
1088                 ovs_flow_stats_clear(flow);
1089         ovs_unlock();
1090
1091         if (reply)
1092                 ovs_notify(&dp_flow_genl_family, &ovs_dp_flow_multicast_group, reply, info);
1093         if (old_acts)
1094                 ovs_nla_free_flow_actions(old_acts);
1095         return 0;
1096
1097 err_unlock_ovs:
1098         ovs_unlock();
1099         kfree_skb(reply);
1100 err_kfree_acts:
1101         kfree(acts);
1102 error:
1103         return error;
1104 }
1105
1106 static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1107 {
1108         struct nlattr **a = info->attrs;
1109         struct ovs_header *ovs_header = info->userhdr;
1110         struct sw_flow_key key;
1111         struct sk_buff *reply;
1112         struct sw_flow *flow;
1113         struct datapath *dp;
1114         struct sw_flow_match match;
1115         int err;
1116
1117         if (!a[OVS_FLOW_ATTR_KEY]) {
1118                 OVS_NLERR("Flow get message rejected, Key attribute missing.\n");
1119                 return -EINVAL;
1120         }
1121
1122         ovs_match_init(&match, &key, NULL);
1123         err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL);
1124         if (err)
1125                 return err;
1126
1127         ovs_lock();
1128         dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1129         if (!dp) {
1130                 err = -ENODEV;
1131                 goto unlock;
1132         }
1133
1134         flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1135         if (!flow) {
1136                 err = -ENOENT;
1137                 goto unlock;
1138         }
1139
1140         reply = ovs_flow_cmd_build_info(dp, flow, ovs_header->dp_ifindex, info,
1141                                         OVS_FLOW_CMD_NEW, true);
1142         if (IS_ERR(reply)) {
1143                 err = PTR_ERR(reply);
1144                 goto unlock;
1145         }
1146
1147         ovs_unlock();
1148         return genlmsg_reply(reply, info);
1149 unlock:
1150         ovs_unlock();
1151         return err;
1152 }
1153
1154 static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1155 {
1156         struct nlattr **a = info->attrs;
1157         struct ovs_header *ovs_header = info->userhdr;
1158         struct sw_flow_key key;
1159         struct sk_buff *reply;
1160         struct sw_flow *flow;
1161         struct datapath *dp;
1162         struct sw_flow_match match;
1163         int err;
1164
1165         if (likely(a[OVS_FLOW_ATTR_KEY])) {
1166                 ovs_match_init(&match, &key, NULL);
1167                 err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL);
1168                 if (unlikely(err))
1169                         return err;
1170         }
1171
1172         ovs_lock();
1173         dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1174         if (unlikely(!dp)) {
1175                 err = -ENODEV;
1176                 goto unlock;
1177         }
1178         if (unlikely(!a[OVS_FLOW_ATTR_KEY])) {
1179                 err = ovs_flow_tbl_flush(&dp->table);
1180                 goto unlock;
1181         }
1182         flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1183         if (unlikely(!flow)) {
1184                 err = -ENOENT;
1185                 goto unlock;
1186         }
1187
1188         ovs_flow_tbl_remove(&dp->table, flow);
1189         ovs_unlock();
1190
1191         reply = ovs_flow_cmd_alloc_info((const struct sw_flow_actions __force *)flow->sf_acts,
1192                                         info, false);
1193
1194         if (likely(reply)) {
1195                 if (likely(!IS_ERR(reply))) {
1196                         rcu_read_lock(); /* Keep RCU checker happy. */
1197                         err = ovs_flow_cmd_fill_info(flow,
1198                                                      ovs_header->dp_ifindex,
1199                                                      reply, info->snd_portid,
1200                                                      info->snd_seq, 0,
1201                                                      OVS_FLOW_CMD_DEL);
1202                         rcu_read_unlock();
1203                         BUG_ON(err < 0);
1204                         ovs_notify(&dp_flow_genl_family, &ovs_dp_flow_multicast_group, reply, info);
1205                 } else {
1206                         genl_set_err(&dp_flow_genl_family, sock_net(skb->sk), 0,
1207                                      GROUP_ID(&ovs_dp_flow_multicast_group), PTR_ERR(reply));
1208
1209                 }
1210         }
1211
1212         ovs_flow_free(flow, true);
1213         return 0;
1214 unlock:
1215         ovs_unlock();
1216         return err;
1217 }
1218
1219 static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1220 {
1221         struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1222         struct table_instance *ti;
1223         struct datapath *dp;
1224
1225         rcu_read_lock();
1226         dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
1227         if (!dp) {
1228                 rcu_read_unlock();
1229                 return -ENODEV;
1230         }
1231
1232         ti = rcu_dereference(dp->table.ti);
1233         for (;;) {
1234                 struct sw_flow *flow;
1235                 u32 bucket, obj;
1236
1237                 bucket = cb->args[0];
1238                 obj = cb->args[1];
1239                 flow = ovs_flow_tbl_dump_next(ti, &bucket, &obj);
1240                 if (!flow)
1241                         break;
1242
1243                 if (ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex, skb,
1244                                            NETLINK_CB(cb->skb).portid,
1245                                            cb->nlh->nlmsg_seq, NLM_F_MULTI,
1246                                            OVS_FLOW_CMD_NEW) < 0)
1247                         break;
1248
1249                 cb->args[0] = bucket;
1250                 cb->args[1] = obj;
1251         }
1252         rcu_read_unlock();
1253         return skb->len;
1254 }
1255
1256 static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
1257         [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
1258         [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
1259         [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
1260 };
1261
1262 static struct genl_ops dp_flow_genl_ops[] = {
1263         { .cmd = OVS_FLOW_CMD_NEW,
1264           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1265           .policy = flow_policy,
1266           .doit = ovs_flow_cmd_new
1267         },
1268         { .cmd = OVS_FLOW_CMD_DEL,
1269           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1270           .policy = flow_policy,
1271           .doit = ovs_flow_cmd_del
1272         },
1273         { .cmd = OVS_FLOW_CMD_GET,
1274           .flags = 0,               /* OK for unprivileged users. */
1275           .policy = flow_policy,
1276           .doit = ovs_flow_cmd_get,
1277           .dumpit = ovs_flow_cmd_dump
1278         },
1279         { .cmd = OVS_FLOW_CMD_SET,
1280           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1281           .policy = flow_policy,
1282           .doit = ovs_flow_cmd_set,
1283         },
1284 };
1285
1286 static struct genl_family dp_flow_genl_family = {
1287         .id = GENL_ID_GENERATE,
1288         .hdrsize = sizeof(struct ovs_header),
1289         .name = OVS_FLOW_FAMILY,
1290         .version = OVS_FLOW_VERSION,
1291         .maxattr = OVS_FLOW_ATTR_MAX,
1292         .netnsok = true,
1293         .parallel_ops = true,
1294         .ops = dp_flow_genl_ops,
1295         .n_ops = ARRAY_SIZE(dp_flow_genl_ops),
1296         .mcgrps = &ovs_dp_flow_multicast_group,
1297         .n_mcgrps = 1,
1298 };
1299
1300 static size_t ovs_dp_cmd_msg_size(void)
1301 {
1302         size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
1303
1304         msgsize += nla_total_size(IFNAMSIZ);
1305         msgsize += nla_total_size(sizeof(struct ovs_dp_stats));
1306         msgsize += nla_total_size(sizeof(struct ovs_dp_megaflow_stats));
1307         msgsize += nla_total_size(sizeof(u32)); /* OVS_DP_ATTR_USER_FEATURES */
1308
1309         return msgsize;
1310 }
1311
1312 /* Called with ovs_mutex or RCU read lock. */
1313 static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
1314                                 u32 portid, u32 seq, u32 flags, u8 cmd)
1315 {
1316         struct ovs_header *ovs_header;
1317         struct ovs_dp_stats dp_stats;
1318         struct ovs_dp_megaflow_stats dp_megaflow_stats;
1319         int err;
1320
1321         ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
1322                                    flags, cmd);
1323         if (!ovs_header)
1324                 goto error;
1325
1326         ovs_header->dp_ifindex = get_dpifindex(dp);
1327
1328         err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
1329         if (err)
1330                 goto nla_put_failure;
1331
1332         get_dp_stats(dp, &dp_stats, &dp_megaflow_stats);
1333         if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats),
1334                         &dp_stats))
1335                 goto nla_put_failure;
1336
1337         if (nla_put(skb, OVS_DP_ATTR_MEGAFLOW_STATS,
1338                         sizeof(struct ovs_dp_megaflow_stats),
1339                         &dp_megaflow_stats))
1340                 goto nla_put_failure;
1341
1342         if (nla_put_u32(skb, OVS_DP_ATTR_USER_FEATURES, dp->user_features))
1343                 goto nla_put_failure;
1344
1345         return genlmsg_end(skb, ovs_header);
1346
1347 nla_put_failure:
1348         genlmsg_cancel(skb, ovs_header);
1349 error:
1350         return -EMSGSIZE;
1351 }
1352
1353 static struct sk_buff *ovs_dp_cmd_alloc_info(struct genl_info *info)
1354 {
1355         return genlmsg_new_unicast(ovs_dp_cmd_msg_size(), info, GFP_KERNEL);
1356 }
1357
1358 /* Called with rcu_read_lock or ovs_mutex. */
1359 static struct datapath *lookup_datapath(struct net *net,
1360                                         const struct ovs_header *ovs_header,
1361                                         struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1362 {
1363         struct datapath *dp;
1364
1365         if (!a[OVS_DP_ATTR_NAME])
1366                 dp = get_dp(net, ovs_header->dp_ifindex);
1367         else {
1368                 struct vport *vport;
1369
1370                 vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
1371                 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
1372         }
1373         return dp ? dp : ERR_PTR(-ENODEV);
1374 }
1375
1376 static void ovs_dp_reset_user_features(struct sk_buff *skb, struct genl_info *info)
1377 {
1378         struct datapath *dp;
1379
1380         dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1381         if (IS_ERR(dp))
1382                 return;
1383
1384         WARN(dp->user_features, "Dropping previously announced user features\n");
1385         dp->user_features = 0;
1386 }
1387
1388 static void ovs_dp_change(struct datapath *dp, struct nlattr *a[])
1389 {
1390         if (a[OVS_DP_ATTR_USER_FEATURES])
1391                 dp->user_features = nla_get_u32(a[OVS_DP_ATTR_USER_FEATURES]);
1392 }
1393
1394 static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1395 {
1396         struct nlattr **a = info->attrs;
1397         struct vport_parms parms;
1398         struct sk_buff *reply;
1399         struct datapath *dp;
1400         struct vport *vport;
1401         struct ovs_net *ovs_net;
1402         int err, i;
1403
1404         err = -EINVAL;
1405         if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1406                 goto err;
1407
1408         reply = ovs_dp_cmd_alloc_info(info);
1409         if (!reply)
1410                 return -ENOMEM;
1411
1412         err = -ENOMEM;
1413         dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1414         if (dp == NULL)
1415                 goto err_free_reply;
1416
1417         ovs_dp_set_net(dp, hold_net(sock_net(skb->sk)));
1418
1419         /* Allocate table. */
1420         err = ovs_flow_tbl_init(&dp->table);
1421         if (err)
1422                 goto err_free_dp;
1423
1424         dp->stats_percpu = netdev_alloc_pcpu_stats(struct dp_stats_percpu);
1425         if (!dp->stats_percpu) {
1426                 err = -ENOMEM;
1427                 goto err_destroy_table;
1428         }
1429
1430         dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
1431                             GFP_KERNEL);
1432         if (!dp->ports) {
1433                 err = -ENOMEM;
1434                 goto err_destroy_percpu;
1435         }
1436
1437         for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1438                 INIT_HLIST_HEAD(&dp->ports[i]);
1439
1440         /* Set up our datapath device. */
1441         parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1442         parms.type = OVS_VPORT_TYPE_INTERNAL;
1443         parms.options = NULL;
1444         parms.dp = dp;
1445         parms.port_no = OVSP_LOCAL;
1446         parms.upcall_portids = a[OVS_DP_ATTR_UPCALL_PID];
1447
1448         ovs_dp_change(dp, a);
1449
1450         /* So far only local changes have been made, now need the lock. */
1451         ovs_lock();
1452
1453         vport = new_vport(&parms);
1454         if (IS_ERR(vport)) {
1455                 err = PTR_ERR(vport);
1456                 if (err == -EBUSY)
1457                         err = -EEXIST;
1458
1459                 if (err == -EEXIST) {
1460                         /* An outdated user space instance that does not understand
1461                          * the concept of user_features has attempted to create a new
1462                          * datapath and is likely to reuse it. Drop all user features.
1463                          */
1464                         if (info->genlhdr->version < OVS_DP_VER_FEATURES)
1465                                 ovs_dp_reset_user_features(skb, info);
1466                 }
1467
1468                 goto err_destroy_ports_array;
1469         }
1470
1471         err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1472                                    info->snd_seq, 0, OVS_DP_CMD_NEW);
1473         BUG_ON(err < 0);
1474
1475         ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
1476         list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
1477
1478         ovs_unlock();
1479
1480         ovs_notify(&dp_datapath_genl_family, &ovs_dp_datapath_multicast_group, reply, info);
1481         return 0;
1482
1483 err_destroy_ports_array:
1484         ovs_unlock();
1485         kfree(dp->ports);
1486 err_destroy_percpu:
1487         free_percpu(dp->stats_percpu);
1488 err_destroy_table:
1489         ovs_flow_tbl_destroy(&dp->table);
1490 err_free_dp:
1491         release_net(ovs_dp_get_net(dp));
1492         kfree(dp);
1493 err_free_reply:
1494         kfree_skb(reply);
1495 err:
1496         return err;
1497 }
1498
1499 /* Called with ovs_mutex. */
1500 static void __dp_destroy(struct datapath *dp)
1501 {
1502         int i;
1503
1504         for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1505                 struct vport *vport;
1506                 struct hlist_node *n;
1507
1508                 hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
1509                         if (vport->port_no != OVSP_LOCAL)
1510                                 ovs_dp_detach_port(vport);
1511         }
1512
1513         list_del_rcu(&dp->list_node);
1514
1515         /* OVSP_LOCAL is datapath internal port. We need to make sure that
1516          * all ports in datapath are destroyed first before freeing datapath.
1517          */
1518         ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
1519
1520         /* RCU destroy the flow table */
1521         call_rcu(&dp->rcu, destroy_dp_rcu);
1522 }
1523
1524 static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1525 {
1526         struct sk_buff *reply;
1527         struct datapath *dp;
1528         int err;
1529
1530         reply = ovs_dp_cmd_alloc_info(info);
1531         if (!reply)
1532                 return -ENOMEM;
1533
1534         ovs_lock();
1535         dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1536         err = PTR_ERR(dp);
1537         if (IS_ERR(dp))
1538                 goto err_unlock_free;
1539
1540         err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1541                                    info->snd_seq, 0, OVS_DP_CMD_DEL);
1542         BUG_ON(err < 0);
1543
1544         __dp_destroy(dp);
1545
1546         ovs_unlock();
1547         ovs_notify(&dp_datapath_genl_family, &ovs_dp_datapath_multicast_group, reply, info);
1548         return 0;
1549
1550 err_unlock_free:
1551         ovs_unlock();
1552         kfree_skb(reply);
1553         return err;
1554 }
1555
1556 static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1557 {
1558         struct sk_buff *reply;
1559         struct datapath *dp;
1560         int err;
1561
1562         reply = ovs_dp_cmd_alloc_info(info);
1563         if (!reply)
1564                 return -ENOMEM;
1565
1566         ovs_lock();
1567         dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1568         err = PTR_ERR(dp);
1569         if (IS_ERR(dp))
1570                 goto err_unlock_free;
1571
1572         ovs_dp_change(dp, info->attrs);
1573
1574         err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1575                                    info->snd_seq, 0, OVS_DP_CMD_NEW);
1576         BUG_ON(err < 0);
1577
1578         ovs_unlock();
1579         ovs_notify(&dp_datapath_genl_family, &ovs_dp_datapath_multicast_group, reply, info);
1580         return 0;
1581
1582 err_unlock_free:
1583         ovs_unlock();
1584         kfree_skb(reply);
1585         return err;
1586 }
1587
1588 static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1589 {
1590         struct sk_buff *reply;
1591         struct datapath *dp;
1592         int err;
1593
1594         reply = ovs_dp_cmd_alloc_info(info);
1595         if (!reply)
1596                 return -ENOMEM;
1597
1598         rcu_read_lock();
1599         dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1600         if (IS_ERR(dp)) {
1601                 err = PTR_ERR(dp);
1602                 goto err_unlock_free;
1603         }
1604         err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1605                                    info->snd_seq, 0, OVS_DP_CMD_NEW);
1606         BUG_ON(err < 0);
1607         rcu_read_unlock();
1608
1609         return genlmsg_reply(reply, info);
1610
1611 err_unlock_free:
1612         rcu_read_unlock();
1613         kfree_skb(reply);
1614         return err;
1615 }
1616
1617 static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1618 {
1619         struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
1620         struct datapath *dp;
1621         int skip = cb->args[0];
1622         int i = 0;
1623
1624         rcu_read_lock();
1625         list_for_each_entry_rcu(dp, &ovs_net->dps, list_node) {
1626                 if (i >= skip &&
1627                     ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
1628                                          cb->nlh->nlmsg_seq, NLM_F_MULTI,
1629                                          OVS_DP_CMD_NEW) < 0)
1630                         break;
1631                 i++;
1632         }
1633         rcu_read_unlock();
1634
1635         cb->args[0] = i;
1636
1637         return skb->len;
1638 }
1639
1640 static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1641         [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1642         [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1643         [OVS_DP_ATTR_USER_FEATURES] = { .type = NLA_U32 },
1644 };
1645
1646 static struct genl_ops dp_datapath_genl_ops[] = {
1647         { .cmd = OVS_DP_CMD_NEW,
1648           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1649           .policy = datapath_policy,
1650           .doit = ovs_dp_cmd_new
1651         },
1652         { .cmd = OVS_DP_CMD_DEL,
1653           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1654           .policy = datapath_policy,
1655           .doit = ovs_dp_cmd_del
1656         },
1657         { .cmd = OVS_DP_CMD_GET,
1658           .flags = 0,               /* OK for unprivileged users. */
1659           .policy = datapath_policy,
1660           .doit = ovs_dp_cmd_get,
1661           .dumpit = ovs_dp_cmd_dump
1662         },
1663         { .cmd = OVS_DP_CMD_SET,
1664           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1665           .policy = datapath_policy,
1666           .doit = ovs_dp_cmd_set,
1667         },
1668 };
1669
1670 static struct genl_family dp_datapath_genl_family = {
1671         .id = GENL_ID_GENERATE,
1672         .hdrsize = sizeof(struct ovs_header),
1673         .name = OVS_DATAPATH_FAMILY,
1674         .version = OVS_DATAPATH_VERSION,
1675         .maxattr = OVS_DP_ATTR_MAX,
1676         .netnsok = true,
1677         .parallel_ops = true,
1678         .ops = dp_datapath_genl_ops,
1679         .n_ops = ARRAY_SIZE(dp_datapath_genl_ops),
1680         .mcgrps = &ovs_dp_datapath_multicast_group,
1681         .n_mcgrps = 1,
1682 };
1683
1684 /* Called with ovs_mutex or RCU read lock. */
1685 static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
1686                                    u32 portid, u32 seq, u32 flags, u8 cmd)
1687 {
1688         struct ovs_header *ovs_header;
1689         struct ovs_vport_stats vport_stats;
1690         int err;
1691
1692         ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
1693                                  flags, cmd);
1694         if (!ovs_header)
1695                 return -EMSGSIZE;
1696
1697         ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1698
1699         if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
1700             nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
1701             nla_put_string(skb, OVS_VPORT_ATTR_NAME, vport->ops->get_name(vport)))
1702                 goto nla_put_failure;
1703
1704         ovs_vport_get_stats(vport, &vport_stats);
1705         if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
1706                     &vport_stats))
1707                 goto nla_put_failure;
1708
1709         if (ovs_vport_get_upcall_portids(vport, skb))
1710                 goto nla_put_failure;
1711
1712         err = ovs_vport_get_options(vport, skb);
1713         if (err == -EMSGSIZE)
1714                 goto error;
1715
1716         return genlmsg_end(skb, ovs_header);
1717
1718 nla_put_failure:
1719         err = -EMSGSIZE;
1720 error:
1721         genlmsg_cancel(skb, ovs_header);
1722         return err;
1723 }
1724
1725 static struct sk_buff *ovs_vport_cmd_alloc_info(void)
1726 {
1727         return nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1728 }
1729
1730 /* Called with ovs_mutex, only via ovs_dp_notify_wq(). */
1731 struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
1732                                          u32 seq, u8 cmd)
1733 {
1734         struct sk_buff *skb;
1735         int retval;
1736
1737         skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1738         if (!skb)
1739                 return ERR_PTR(-ENOMEM);
1740
1741         retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
1742         BUG_ON(retval < 0);
1743
1744         return skb;
1745 }
1746
1747 /* Called with ovs_mutex or RCU read lock. */
1748 static struct vport *lookup_vport(struct net *net,
1749                                   const struct ovs_header *ovs_header,
1750                                   struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1751 {
1752         struct datapath *dp;
1753         struct vport *vport;
1754
1755         if (a[OVS_VPORT_ATTR_NAME]) {
1756                 vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
1757                 if (!vport)
1758                         return ERR_PTR(-ENODEV);
1759                 if (ovs_header->dp_ifindex &&
1760                     ovs_header->dp_ifindex != get_dpifindex(vport->dp))
1761                         return ERR_PTR(-ENODEV);
1762                 return vport;
1763         } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
1764                 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1765
1766                 if (port_no >= DP_MAX_PORTS)
1767                         return ERR_PTR(-EFBIG);
1768
1769                 dp = get_dp(net, ovs_header->dp_ifindex);
1770                 if (!dp)
1771                         return ERR_PTR(-ENODEV);
1772
1773                 vport = ovs_vport_ovsl_rcu(dp, port_no);
1774                 if (!vport)
1775                         return ERR_PTR(-ENODEV);
1776                 return vport;
1777         } else
1778                 return ERR_PTR(-EINVAL);
1779 }
1780
1781 static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
1782 {
1783         struct nlattr **a = info->attrs;
1784         struct ovs_header *ovs_header = info->userhdr;
1785         struct vport_parms parms;
1786         struct sk_buff *reply;
1787         struct vport *vport;
1788         struct datapath *dp;
1789         u32 port_no;
1790         int err;
1791
1792         if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
1793             !a[OVS_VPORT_ATTR_UPCALL_PID])
1794                 return -EINVAL;
1795
1796         port_no = a[OVS_VPORT_ATTR_PORT_NO]
1797                 ? nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]) : 0;
1798         if (port_no >= DP_MAX_PORTS)
1799                 return -EFBIG;
1800
1801         reply = ovs_vport_cmd_alloc_info();
1802         if (!reply)
1803                 return -ENOMEM;
1804
1805         ovs_lock();
1806         dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1807         err = -ENODEV;
1808         if (!dp)
1809                 goto exit_unlock_free;
1810
1811         if (port_no) {
1812                 vport = ovs_vport_ovsl(dp, port_no);
1813                 err = -EBUSY;
1814                 if (vport)
1815                         goto exit_unlock_free;
1816         } else {
1817                 for (port_no = 1; ; port_no++) {
1818                         if (port_no >= DP_MAX_PORTS) {
1819                                 err = -EFBIG;
1820                                 goto exit_unlock_free;
1821                         }
1822                         vport = ovs_vport_ovsl(dp, port_no);
1823                         if (!vport)
1824                                 break;
1825                 }
1826         }
1827
1828         parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
1829         parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1830         parms.options = a[OVS_VPORT_ATTR_OPTIONS];
1831         parms.dp = dp;
1832         parms.port_no = port_no;
1833         parms.upcall_portids = a[OVS_VPORT_ATTR_UPCALL_PID];
1834
1835         vport = new_vport(&parms);
1836         err = PTR_ERR(vport);
1837         if (IS_ERR(vport))
1838                 goto exit_unlock_free;
1839
1840         err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1841                                       info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1842         BUG_ON(err < 0);
1843         ovs_unlock();
1844
1845         ovs_notify(&dp_vport_genl_family, &ovs_dp_vport_multicast_group, reply, info);
1846         return 0;
1847
1848 exit_unlock_free:
1849         ovs_unlock();
1850         kfree_skb(reply);
1851         return err;
1852 }
1853
1854 static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
1855 {
1856         struct nlattr **a = info->attrs;
1857         struct sk_buff *reply;
1858         struct vport *vport;
1859         int err;
1860
1861         reply = ovs_vport_cmd_alloc_info();
1862         if (!reply)
1863                 return -ENOMEM;
1864
1865         ovs_lock();
1866         vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
1867         err = PTR_ERR(vport);
1868         if (IS_ERR(vport))
1869                 goto exit_unlock_free;
1870
1871         if (a[OVS_VPORT_ATTR_TYPE] &&
1872             nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
1873                 err = -EINVAL;
1874                 goto exit_unlock_free;
1875         }
1876
1877         if (a[OVS_VPORT_ATTR_OPTIONS]) {
1878                 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
1879                 if (err)
1880                         goto exit_unlock_free;
1881         }
1882
1883         if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
1884                 err = ovs_vport_set_upcall_portids(vport,
1885                                                    a[OVS_VPORT_ATTR_UPCALL_PID]);
1886                 if (err)
1887                         goto exit_unlock_free;
1888         }
1889
1890         err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1891                                       info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1892         BUG_ON(err < 0);
1893         ovs_unlock();
1894
1895         ovs_notify(&dp_vport_genl_family, &ovs_dp_vport_multicast_group, reply, info);
1896         return 0;
1897
1898 exit_unlock_free:
1899         ovs_unlock();
1900         kfree_skb(reply);
1901         return err;
1902 }
1903
1904 static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
1905 {
1906         struct nlattr **a = info->attrs;
1907         struct sk_buff *reply;
1908         struct vport *vport;
1909         int err;
1910
1911         reply = ovs_vport_cmd_alloc_info();
1912         if (!reply)
1913                 return -ENOMEM;
1914
1915         ovs_lock();
1916         vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
1917         err = PTR_ERR(vport);
1918         if (IS_ERR(vport))
1919                 goto exit_unlock_free;
1920
1921         if (vport->port_no == OVSP_LOCAL) {
1922                 err = -EINVAL;
1923                 goto exit_unlock_free;
1924         }
1925
1926         err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1927                                       info->snd_seq, 0, OVS_VPORT_CMD_DEL);
1928         BUG_ON(err < 0);
1929         ovs_dp_detach_port(vport);
1930         ovs_unlock();
1931
1932         ovs_notify(&dp_vport_genl_family, &ovs_dp_vport_multicast_group, reply, info);
1933         return 0;
1934
1935 exit_unlock_free:
1936         ovs_unlock();
1937         kfree_skb(reply);
1938         return err;
1939 }
1940
1941 static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
1942 {
1943         struct nlattr **a = info->attrs;
1944         struct ovs_header *ovs_header = info->userhdr;
1945         struct sk_buff *reply;
1946         struct vport *vport;
1947         int err;
1948
1949         reply = ovs_vport_cmd_alloc_info();
1950         if (!reply)
1951                 return -ENOMEM;
1952
1953         rcu_read_lock();
1954         vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
1955         err = PTR_ERR(vport);
1956         if (IS_ERR(vport))
1957                 goto exit_unlock_free;
1958         err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1959                                       info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1960         BUG_ON(err < 0);
1961         rcu_read_unlock();
1962
1963         return genlmsg_reply(reply, info);
1964
1965 exit_unlock_free:
1966         rcu_read_unlock();
1967         kfree_skb(reply);
1968         return err;
1969 }
1970
1971 static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1972 {
1973         struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1974         struct datapath *dp;
1975         int bucket = cb->args[0], skip = cb->args[1];
1976         int i, j = 0;
1977
1978         rcu_read_lock();
1979         dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
1980         if (!dp) {
1981                 rcu_read_unlock();
1982                 return -ENODEV;
1983         }
1984         for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
1985                 struct vport *vport;
1986
1987                 j = 0;
1988                 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
1989                         if (j >= skip &&
1990                             ovs_vport_cmd_fill_info(vport, skb,
1991                                                     NETLINK_CB(cb->skb).portid,
1992                                                     cb->nlh->nlmsg_seq,
1993                                                     NLM_F_MULTI,
1994                                                     OVS_VPORT_CMD_NEW) < 0)
1995                                 goto out;
1996
1997                         j++;
1998                 }
1999                 skip = 0;
2000         }
2001 out:
2002         rcu_read_unlock();
2003
2004         cb->args[0] = i;
2005         cb->args[1] = j;
2006
2007         return skb->len;
2008 }
2009
2010 static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
2011         [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
2012         [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
2013         [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
2014         [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
2015         [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
2016         [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
2017 };
2018
2019 static struct genl_ops dp_vport_genl_ops[] = {
2020         { .cmd = OVS_VPORT_CMD_NEW,
2021           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2022           .policy = vport_policy,
2023           .doit = ovs_vport_cmd_new
2024         },
2025         { .cmd = OVS_VPORT_CMD_DEL,
2026           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2027           .policy = vport_policy,
2028           .doit = ovs_vport_cmd_del
2029         },
2030         { .cmd = OVS_VPORT_CMD_GET,
2031           .flags = 0,               /* OK for unprivileged users. */
2032           .policy = vport_policy,
2033           .doit = ovs_vport_cmd_get,
2034           .dumpit = ovs_vport_cmd_dump
2035         },
2036         { .cmd = OVS_VPORT_CMD_SET,
2037           .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2038           .policy = vport_policy,
2039           .doit = ovs_vport_cmd_set,
2040         },
2041 };
2042
2043 struct genl_family dp_vport_genl_family = {
2044         .id = GENL_ID_GENERATE,
2045         .hdrsize = sizeof(struct ovs_header),
2046         .name = OVS_VPORT_FAMILY,
2047         .version = OVS_VPORT_VERSION,
2048         .maxattr = OVS_VPORT_ATTR_MAX,
2049         .netnsok = true,
2050         .parallel_ops = true,
2051         .ops = dp_vport_genl_ops,
2052         .n_ops = ARRAY_SIZE(dp_vport_genl_ops),
2053         .mcgrps = &ovs_dp_vport_multicast_group,
2054         .n_mcgrps = 1,
2055 };
2056
2057 static struct genl_family *dp_genl_families[] = {
2058         &dp_datapath_genl_family,
2059         &dp_vport_genl_family,
2060         &dp_flow_genl_family,
2061         &dp_packet_genl_family,
2062 };
2063
2064 static void dp_unregister_genl(int n_families)
2065 {
2066         int i;
2067
2068         for (i = 0; i < n_families; i++)
2069                 genl_unregister_family(dp_genl_families[i]);
2070 }
2071
2072 static int dp_register_genl(void)
2073 {
2074         int err;
2075         int i;
2076
2077         for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
2078
2079                 err = genl_register_family(dp_genl_families[i]);
2080                 if (err)
2081                         goto error;
2082         }
2083
2084         return 0;
2085
2086 error:
2087         dp_unregister_genl(i);
2088         return err;
2089 }
2090
2091 static int __net_init ovs_init_net(struct net *net)
2092 {
2093         struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2094
2095         INIT_LIST_HEAD(&ovs_net->dps);
2096         INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
2097         return 0;
2098 }
2099
2100 static void __net_exit ovs_exit_net(struct net *net)
2101 {
2102         struct datapath *dp, *dp_next;
2103         struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2104
2105         ovs_lock();
2106         list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
2107                 __dp_destroy(dp);
2108         ovs_unlock();
2109
2110         cancel_work_sync(&ovs_net->dp_notify_work);
2111 }
2112
2113 static struct pernet_operations ovs_net_ops = {
2114         .init = ovs_init_net,
2115         .exit = ovs_exit_net,
2116         .id   = &ovs_net_id,
2117         .size = sizeof(struct ovs_net),
2118 };
2119
2120 DEFINE_COMPAT_PNET_REG_FUNC(device);
2121
2122 static int __init dp_init(void)
2123 {
2124         int err;
2125
2126         BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
2127
2128         pr_info("Open vSwitch switching datapath %s, built "__DATE__" "__TIME__"\n",
2129                 VERSION);
2130
2131         err = action_fifos_init();
2132         if (err)
2133                 goto error;
2134
2135         err = ovs_flow_init();
2136         if (err)
2137                 goto error_action_fifos_exit;
2138
2139         err = ovs_vport_init();
2140         if (err)
2141                 goto error_flow_exit;
2142
2143         err = register_pernet_device(&ovs_net_ops);
2144         if (err)
2145                 goto error_vport_exit;
2146
2147         err = register_netdevice_notifier(&ovs_dp_device_notifier);
2148         if (err)
2149                 goto error_netns_exit;
2150
2151         err = dp_register_genl();
2152         if (err < 0)
2153                 goto error_unreg_notifier;
2154
2155         return 0;
2156
2157 error_unreg_notifier:
2158         unregister_netdevice_notifier(&ovs_dp_device_notifier);
2159 error_netns_exit:
2160         unregister_pernet_device(&ovs_net_ops);
2161 error_vport_exit:
2162         ovs_vport_exit();
2163 error_flow_exit:
2164         ovs_flow_exit();
2165 error_action_fifos_exit:
2166         action_fifos_exit();
2167 error:
2168         return err;
2169 }
2170
2171 static void dp_cleanup(void)
2172 {
2173         dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
2174         unregister_netdevice_notifier(&ovs_dp_device_notifier);
2175         unregister_pernet_device(&ovs_net_ops);
2176         rcu_barrier();
2177         ovs_vport_exit();
2178         ovs_flow_exit();
2179         action_fifos_exit();
2180 }
2181
2182 module_init(dp_init);
2183 module_exit(dp_cleanup);
2184
2185 MODULE_DESCRIPTION("Open vSwitch switching datapath");
2186 MODULE_LICENSE("GPL");
2187 MODULE_VERSION(VERSION);