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