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