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