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