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