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