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