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