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