blob: 6cfb44f3a7f0fd12130820c39ad40bbd75202796 [file] [log] [blame]
Jesse Grossccb13522011-10-25 19:26:31 -07001/*
Ben Pfaffad552002014-05-06 16:48:38 -07002 * Copyright (c) 2007-2014 Nicira, Inc.
Jesse Grossccb13522011-10-25 19:26:31 -07003 *
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>
Jesse Grossccb13522011-10-25 19:26:31 -070039#include <linux/ethtool.h>
40#include <linux/wait.h>
Jesse Grossccb13522011-10-25 19:26:31 -070041#include <asm/div64.h>
42#include <linux/highmem.h>
43#include <linux/netfilter_bridge.h>
44#include <linux/netfilter_ipv4.h>
45#include <linux/inetdevice.h>
46#include <linux/list.h>
47#include <linux/openvswitch.h>
48#include <linux/rculist.h>
49#include <linux/dmi.h>
Jesse Grossccb13522011-10-25 19:26:31 -070050#include <net/genetlink.h>
Pravin B Shelar46df7b82012-02-22 19:58:59 -080051#include <net/net_namespace.h>
52#include <net/netns/generic.h>
Jesse Grossccb13522011-10-25 19:26:31 -070053
54#include "datapath.h"
55#include "flow.h"
Andy Zhoue80857c2014-01-21 09:31:04 -080056#include "flow_table.h"
Pravin B Shelare6445712013-10-03 18:16:47 -070057#include "flow_netlink.h"
Jesse Grossccb13522011-10-25 19:26:31 -070058#include "vport-internal_dev.h"
Thomas Grafcff63a52013-04-29 13:06:41 +000059#include "vport-netdev.h"
Jesse Grossccb13522011-10-25 19:26:31 -070060
Pravin B Shelar8e4e1712013-04-15 13:23:03 -070061int ovs_net_id __read_mostly;
Pravin B Shelar9ba559d2014-11-06 06:44:27 -080062EXPORT_SYMBOL_GPL(ovs_net_id);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -070063
Pravin B Shelar0c200ef2014-05-06 16:44:50 -070064static struct genl_family dp_packet_genl_family;
65static struct genl_family dp_flow_genl_family;
66static struct genl_family dp_datapath_genl_family;
67
stephen hemminger48e48a72014-07-16 11:25:52 -070068static const struct genl_multicast_group ovs_dp_flow_multicast_group = {
69 .name = OVS_FLOW_MCGROUP,
Pravin B Shelar0c200ef2014-05-06 16:44:50 -070070};
71
stephen hemminger48e48a72014-07-16 11:25:52 -070072static const struct genl_multicast_group ovs_dp_datapath_multicast_group = {
73 .name = OVS_DATAPATH_MCGROUP,
Pravin B Shelar0c200ef2014-05-06 16:44:50 -070074};
75
stephen hemminger48e48a72014-07-16 11:25:52 -070076static const struct genl_multicast_group ovs_dp_vport_multicast_group = {
77 .name = OVS_VPORT_MCGROUP,
Pravin B Shelar0c200ef2014-05-06 16:44:50 -070078};
79
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -070080/* Check if need to build a reply message.
81 * OVS userspace sets the NLM_F_ECHO flag if it needs the reply. */
Samuel Gauthier9b67aa42014-09-18 10:31:04 +020082static bool ovs_must_notify(struct genl_family *family, struct genl_info *info,
83 unsigned int group)
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -070084{
85 return info->nlhdr->nlmsg_flags & NLM_F_ECHO ||
Samuel Gauthier9b67aa42014-09-18 10:31:04 +020086 genl_has_listeners(family, genl_info_net(info)->genl_sock,
87 group);
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -070088}
89
Johannes Berg68eb5502013-11-19 15:19:38 +010090static void ovs_notify(struct genl_family *family,
Johannes Berg2a94fe42013-11-19 15:19:39 +010091 struct sk_buff *skb, struct genl_info *info)
Thomas Grafed661182013-03-29 14:46:50 +010092{
Johannes Berg68eb5502013-11-19 15:19:38 +010093 genl_notify(family, skb, genl_info_net(info), info->snd_portid,
Johannes Berg2a94fe42013-11-19 15:19:39 +010094 0, info->nlhdr, GFP_KERNEL);
Thomas Grafed661182013-03-29 14:46:50 +010095}
96
Pravin B Shelar46df7b82012-02-22 19:58:59 -080097/**
Jesse Grossccb13522011-10-25 19:26:31 -070098 * DOC: Locking:
99 *
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700100 * All writes e.g. Writes to device state (add/remove datapath, port, set
101 * operations on vports, etc.), Writes to other state (flow table
102 * modifications, set miscellaneous datapath parameters, etc.) are protected
103 * by ovs_lock.
Jesse Grossccb13522011-10-25 19:26:31 -0700104 *
105 * Reads are protected by RCU.
106 *
107 * There are a few special cases (mostly stats) that have their own
108 * synchronization but they nest under all of above and don't interact with
109 * each other.
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700110 *
111 * The RTNL lock nests inside ovs_mutex.
Jesse Grossccb13522011-10-25 19:26:31 -0700112 */
113
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700114static DEFINE_MUTEX(ovs_mutex);
115
116void ovs_lock(void)
117{
118 mutex_lock(&ovs_mutex);
119}
120
121void ovs_unlock(void)
122{
123 mutex_unlock(&ovs_mutex);
124}
125
126#ifdef CONFIG_LOCKDEP
127int lockdep_ovsl_is_held(void)
128{
129 if (debug_locks)
130 return lockdep_is_held(&ovs_mutex);
131 else
132 return 1;
133}
Pravin B Shelar9ba559d2014-11-06 06:44:27 -0800134EXPORT_SYMBOL_GPL(lockdep_ovsl_is_held);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700135#endif
136
Jesse Grossccb13522011-10-25 19:26:31 -0700137static struct vport *new_vport(const struct vport_parms *);
Thomas Graf8055a892013-12-13 15:22:20 +0100138static int queue_gso_packets(struct datapath *dp, struct sk_buff *,
Jesse Grossccb13522011-10-25 19:26:31 -0700139 const struct dp_upcall_info *);
Thomas Graf8055a892013-12-13 15:22:20 +0100140static int queue_userspace_packet(struct datapath *dp, struct sk_buff *,
Jesse Grossccb13522011-10-25 19:26:31 -0700141 const struct dp_upcall_info *);
142
Andy Zhoucc3a5ae2014-09-08 13:14:22 -0700143/* Must be called with rcu_read_lock. */
144static struct datapath *get_dp_rcu(struct net *net, int dp_ifindex)
Jesse Grossccb13522011-10-25 19:26:31 -0700145{
Andy Zhoucc3a5ae2014-09-08 13:14:22 -0700146 struct net_device *dev = dev_get_by_index_rcu(net, dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -0700147
Jesse Grossccb13522011-10-25 19:26:31 -0700148 if (dev) {
149 struct vport *vport = ovs_internal_dev_get_vport(dev);
150 if (vport)
Andy Zhoucc3a5ae2014-09-08 13:14:22 -0700151 return vport->dp;
Jesse Grossccb13522011-10-25 19:26:31 -0700152 }
Andy Zhoucc3a5ae2014-09-08 13:14:22 -0700153
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 */
160static inline struct datapath *get_dp(struct net *net, int dp_ifindex)
161{
162 struct datapath *dp;
163
164 WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_ovsl_is_held());
165 rcu_read_lock();
166 dp = get_dp_rcu(net, dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -0700167 rcu_read_unlock();
168
169 return dp;
170}
171
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700172/* Must be called with rcu_read_lock or ovs_mutex. */
Andy Zhou971427f32014-09-15 19:37:25 -0700173const char *ovs_dp_name(const struct datapath *dp)
Jesse Grossccb13522011-10-25 19:26:31 -0700174{
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700175 struct vport *vport = ovs_vport_ovsl_rcu(dp, OVSP_LOCAL);
Jesse Grossccb13522011-10-25 19:26:31 -0700176 return vport->ops->get_name(vport);
177}
178
179static int get_dpifindex(struct datapath *dp)
180{
181 struct vport *local;
182 int ifindex;
183
184 rcu_read_lock();
185
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700186 local = ovs_vport_rcu(dp, OVSP_LOCAL);
Jesse Grossccb13522011-10-25 19:26:31 -0700187 if (local)
Thomas Grafcff63a52013-04-29 13:06:41 +0000188 ifindex = netdev_vport_priv(local)->dev->ifindex;
Jesse Grossccb13522011-10-25 19:26:31 -0700189 else
190 ifindex = 0;
191
192 rcu_read_unlock();
193
194 return ifindex;
195}
196
197static void destroy_dp_rcu(struct rcu_head *rcu)
198{
199 struct datapath *dp = container_of(rcu, struct datapath, rcu);
200
Pravin B Shelar9b996e52014-05-06 18:41:20 -0700201 ovs_flow_tbl_destroy(&dp->table);
Jesse Grossccb13522011-10-25 19:26:31 -0700202 free_percpu(dp->stats_percpu);
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800203 release_net(ovs_dp_get_net(dp));
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700204 kfree(dp->ports);
Jesse Grossccb13522011-10-25 19:26:31 -0700205 kfree(dp);
206}
207
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700208static struct hlist_head *vport_hash_bucket(const struct datapath *dp,
209 u16 port_no)
210{
211 return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)];
212}
213
Jarno Rajahalmebb6f9a72014-05-05 11:32:17 -0700214/* Called with ovs_mutex or RCU read lock. */
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700215struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no)
216{
217 struct vport *vport;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700218 struct hlist_head *head;
219
220 head = vport_hash_bucket(dp, port_no);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800221 hlist_for_each_entry_rcu(vport, head, dp_hash_node) {
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700222 if (vport->port_no == port_no)
223 return vport;
224 }
225 return NULL;
226}
227
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700228/* Called with ovs_mutex. */
Jesse Grossccb13522011-10-25 19:26:31 -0700229static struct vport *new_vport(const struct vport_parms *parms)
230{
231 struct vport *vport;
232
233 vport = ovs_vport_add(parms);
234 if (!IS_ERR(vport)) {
235 struct datapath *dp = parms->dp;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700236 struct hlist_head *head = vport_hash_bucket(dp, vport->port_no);
Jesse Grossccb13522011-10-25 19:26:31 -0700237
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700238 hlist_add_head_rcu(&vport->dp_hash_node, head);
Jesse Grossccb13522011-10-25 19:26:31 -0700239 }
Jesse Grossccb13522011-10-25 19:26:31 -0700240 return vport;
241}
242
Jesse Grossccb13522011-10-25 19:26:31 -0700243void ovs_dp_detach_port(struct vport *p)
244{
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700245 ASSERT_OVSL();
Jesse Grossccb13522011-10-25 19:26:31 -0700246
247 /* First drop references to device. */
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700248 hlist_del_rcu(&p->dp_hash_node);
Jesse Grossccb13522011-10-25 19:26:31 -0700249
250 /* Then destroy it. */
251 ovs_vport_del(p);
252}
253
254/* Must be called with rcu_read_lock. */
Pravin B Shelar8c8b1b82014-09-15 19:28:44 -0700255void ovs_dp_process_packet(struct sk_buff *skb, struct sw_flow_key *key)
Jesse Grossccb13522011-10-25 19:26:31 -0700256{
Pravin B Shelar83c8df22014-09-15 19:20:31 -0700257 const struct vport *p = OVS_CB(skb)->input_vport;
Jesse Grossccb13522011-10-25 19:26:31 -0700258 struct datapath *dp = p->dp;
259 struct sw_flow *flow;
Lorand Jakabd98612b2014-10-06 05:45:32 -0700260 struct sw_flow_actions *sf_acts;
Jesse Grossccb13522011-10-25 19:26:31 -0700261 struct dp_stats_percpu *stats;
Jesse Grossccb13522011-10-25 19:26:31 -0700262 u64 *stats_counter;
Andy Zhou1bd71162013-10-22 10:42:46 -0700263 u32 n_mask_hit;
Jesse Grossccb13522011-10-25 19:26:31 -0700264
Shan Wei404f2f12012-11-13 09:52:25 +0800265 stats = this_cpu_ptr(dp->stats_percpu);
Jesse Grossccb13522011-10-25 19:26:31 -0700266
Jesse Grossccb13522011-10-25 19:26:31 -0700267 /* Look up flow. */
Pravin B Shelar8c8b1b82014-09-15 19:28:44 -0700268 flow = ovs_flow_tbl_lookup_stats(&dp->table, key, &n_mask_hit);
Jesse Grossccb13522011-10-25 19:26:31 -0700269 if (unlikely(!flow)) {
270 struct dp_upcall_info upcall;
Pravin B Shelar8c8b1b82014-09-15 19:28:44 -0700271 int error;
Jesse Grossccb13522011-10-25 19:26:31 -0700272
273 upcall.cmd = OVS_PACKET_CMD_MISS;
Pravin B Shelar8c8b1b82014-09-15 19:28:44 -0700274 upcall.key = key;
Jesse Grossccb13522011-10-25 19:26:31 -0700275 upcall.userdata = NULL;
Alex Wang5cd667b2014-07-17 15:14:13 -0700276 upcall.portid = ovs_vport_find_upcall_portid(p, skb);
Li RongQingc5eba0b2014-09-03 17:43:45 +0800277 error = ovs_dp_upcall(dp, skb, &upcall);
278 if (unlikely(error))
279 kfree_skb(skb);
280 else
281 consume_skb(skb);
Jesse Grossccb13522011-10-25 19:26:31 -0700282 stats_counter = &stats->n_missed;
283 goto out;
284 }
285
Lorand Jakabd98612b2014-10-06 05:45:32 -0700286 ovs_flow_stats_update(flow, key->tp.flags, skb);
287 sf_acts = rcu_dereference(flow->sf_acts);
288 ovs_execute_actions(dp, skb, sf_acts, key);
Jesse Grossccb13522011-10-25 19:26:31 -0700289
Pravin B Shelare298e502013-10-29 17:22:21 -0700290 stats_counter = &stats->n_hit;
Jesse Grossccb13522011-10-25 19:26:31 -0700291
292out:
293 /* Update datapath statistics. */
WANG Congdf9d9fd2014-02-14 15:10:46 -0800294 u64_stats_update_begin(&stats->syncp);
Jesse Grossccb13522011-10-25 19:26:31 -0700295 (*stats_counter)++;
Andy Zhou1bd71162013-10-22 10:42:46 -0700296 stats->n_mask_hit += n_mask_hit;
WANG Congdf9d9fd2014-02-14 15:10:46 -0800297 u64_stats_update_end(&stats->syncp);
Jesse Grossccb13522011-10-25 19:26:31 -0700298}
299
Jesse Grossccb13522011-10-25 19:26:31 -0700300int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800301 const struct dp_upcall_info *upcall_info)
Jesse Grossccb13522011-10-25 19:26:31 -0700302{
303 struct dp_stats_percpu *stats;
Jesse Grossccb13522011-10-25 19:26:31 -0700304 int err;
305
Eric W. Biederman15e47302012-09-07 20:12:54 +0000306 if (upcall_info->portid == 0) {
Jesse Grossccb13522011-10-25 19:26:31 -0700307 err = -ENOTCONN;
308 goto err;
309 }
310
Jesse Grossccb13522011-10-25 19:26:31 -0700311 if (!skb_is_gso(skb))
Thomas Graf8055a892013-12-13 15:22:20 +0100312 err = queue_userspace_packet(dp, skb, upcall_info);
Jesse Grossccb13522011-10-25 19:26:31 -0700313 else
Thomas Graf8055a892013-12-13 15:22:20 +0100314 err = queue_gso_packets(dp, skb, upcall_info);
Jesse Grossccb13522011-10-25 19:26:31 -0700315 if (err)
316 goto err;
317
318 return 0;
319
320err:
Shan Wei404f2f12012-11-13 09:52:25 +0800321 stats = this_cpu_ptr(dp->stats_percpu);
Jesse Grossccb13522011-10-25 19:26:31 -0700322
WANG Congdf9d9fd2014-02-14 15:10:46 -0800323 u64_stats_update_begin(&stats->syncp);
Jesse Grossccb13522011-10-25 19:26:31 -0700324 stats->n_lost++;
WANG Congdf9d9fd2014-02-14 15:10:46 -0800325 u64_stats_update_end(&stats->syncp);
Jesse Grossccb13522011-10-25 19:26:31 -0700326
327 return err;
328}
329
Thomas Graf8055a892013-12-13 15:22:20 +0100330static int queue_gso_packets(struct datapath *dp, struct sk_buff *skb,
Jesse Grossccb13522011-10-25 19:26:31 -0700331 const struct dp_upcall_info *upcall_info)
332{
Ben Pfaffa1b5d0d2012-07-20 14:47:54 -0700333 unsigned short gso_type = skb_shinfo(skb)->gso_type;
Jesse Grossccb13522011-10-25 19:26:31 -0700334 struct dp_upcall_info later_info;
335 struct sw_flow_key later_key;
336 struct sk_buff *segs, *nskb;
337 int err;
338
Thomas Graf09c5e602013-12-13 15:22:22 +0100339 segs = __skb_gso_segment(skb, NETIF_F_SG, false);
Pravin B Shelar92e5dfc2012-07-20 14:46:29 -0700340 if (IS_ERR(segs))
341 return PTR_ERR(segs);
Florian Westphal330966e2014-10-20 13:49:17 +0200342 if (segs == NULL)
343 return -EINVAL;
Jesse Grossccb13522011-10-25 19:26:31 -0700344
345 /* Queue all of the segments. */
346 skb = segs;
347 do {
Thomas Graf8055a892013-12-13 15:22:20 +0100348 err = queue_userspace_packet(dp, skb, upcall_info);
Jesse Grossccb13522011-10-25 19:26:31 -0700349 if (err)
350 break;
351
Ben Pfaffa1b5d0d2012-07-20 14:47:54 -0700352 if (skb == segs && gso_type & SKB_GSO_UDP) {
Jesse Grossccb13522011-10-25 19:26:31 -0700353 /* The initial flow key extracted by ovs_flow_extract()
354 * in this case is for a first fragment, so we need to
355 * properly mark later fragments.
356 */
357 later_key = *upcall_info->key;
358 later_key.ip.frag = OVS_FRAG_TYPE_LATER;
359
360 later_info = *upcall_info;
361 later_info.key = &later_key;
362 upcall_info = &later_info;
363 }
364 } while ((skb = skb->next));
365
366 /* Free all of the segments. */
367 skb = segs;
368 do {
369 nskb = skb->next;
370 if (err)
371 kfree_skb(skb);
372 else
373 consume_skb(skb);
374 } while ((skb = nskb));
375 return err;
376}
377
Thomas Grafbda56f12013-12-13 15:22:21 +0100378static size_t upcall_msg_size(const struct nlattr *userdata,
379 unsigned int hdrlen)
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100380{
381 size_t size = NLMSG_ALIGN(sizeof(struct ovs_header))
Thomas Grafbda56f12013-12-13 15:22:21 +0100382 + nla_total_size(hdrlen) /* OVS_PACKET_ATTR_PACKET */
Joe Stringer41af73e2014-10-18 16:14:14 -0700383 + nla_total_size(ovs_key_attr_size()); /* OVS_PACKET_ATTR_KEY */
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100384
385 /* OVS_PACKET_ATTR_USERDATA */
386 if (userdata)
387 size += NLA_ALIGN(userdata->nla_len);
388
389 return size;
390}
391
Thomas Graf8055a892013-12-13 15:22:20 +0100392static int queue_userspace_packet(struct datapath *dp, struct sk_buff *skb,
Jesse Grossccb13522011-10-25 19:26:31 -0700393 const struct dp_upcall_info *upcall_info)
394{
395 struct ovs_header *upcall;
396 struct sk_buff *nskb = NULL;
Li RongQing4ee45ea2014-09-02 20:52:28 +0800397 struct sk_buff *user_skb = NULL; /* to be queued to userspace */
Jesse Grossccb13522011-10-25 19:26:31 -0700398 struct nlattr *nla;
Thomas Graf795449d2013-11-30 13:21:32 +0100399 struct genl_info info = {
Thomas Graf8055a892013-12-13 15:22:20 +0100400 .dst_sk = ovs_dp_get_net(dp)->genl_sock,
Thomas Graf795449d2013-11-30 13:21:32 +0100401 .snd_portid = upcall_info->portid,
402 };
403 size_t len;
Thomas Grafbda56f12013-12-13 15:22:21 +0100404 unsigned int hlen;
Thomas Graf8055a892013-12-13 15:22:20 +0100405 int err, dp_ifindex;
406
407 dp_ifindex = get_dpifindex(dp);
408 if (!dp_ifindex)
409 return -ENODEV;
Jesse Grossccb13522011-10-25 19:26:31 -0700410
411 if (vlan_tx_tag_present(skb)) {
412 nskb = skb_clone(skb, GFP_ATOMIC);
413 if (!nskb)
414 return -ENOMEM;
415
Patrick McHardy86a9bad2013-04-19 02:04:30 +0000416 nskb = __vlan_put_tag(nskb, nskb->vlan_proto, vlan_tx_tag_get(nskb));
Dan Carpenter8aa51d62012-05-13 08:44:18 +0000417 if (!nskb)
Jesse Grossccb13522011-10-25 19:26:31 -0700418 return -ENOMEM;
419
420 nskb->vlan_tci = 0;
421 skb = nskb;
422 }
423
424 if (nla_attr_size(skb->len) > USHRT_MAX) {
425 err = -EFBIG;
426 goto out;
427 }
428
Thomas Grafbda56f12013-12-13 15:22:21 +0100429 /* Complete checksum if needed */
430 if (skb->ip_summed == CHECKSUM_PARTIAL &&
431 (err = skb_checksum_help(skb)))
432 goto out;
433
434 /* Older versions of OVS user space enforce alignment of the last
435 * Netlink attribute to NLA_ALIGNTO which would require extensive
436 * padding logic. Only perform zerocopy if padding is not required.
437 */
438 if (dp->user_features & OVS_DP_F_UNALIGNED)
439 hlen = skb_zerocopy_headlen(skb);
440 else
441 hlen = skb->len;
442
443 len = upcall_msg_size(upcall_info->userdata, hlen);
Thomas Graf795449d2013-11-30 13:21:32 +0100444 user_skb = genlmsg_new_unicast(len, &info, GFP_ATOMIC);
Jesse Grossccb13522011-10-25 19:26:31 -0700445 if (!user_skb) {
446 err = -ENOMEM;
447 goto out;
448 }
449
450 upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
451 0, upcall_info->cmd);
452 upcall->dp_ifindex = dp_ifindex;
453
454 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
Andy Zhouf53e3832014-07-17 15:17:44 -0700455 err = ovs_nla_put_flow(upcall_info->key, upcall_info->key, user_skb);
456 BUG_ON(err);
Jesse Grossccb13522011-10-25 19:26:31 -0700457 nla_nest_end(user_skb, nla);
458
459 if (upcall_info->userdata)
Ben Pfaff44901082013-02-15 17:29:22 -0800460 __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA,
461 nla_len(upcall_info->userdata),
462 nla_data(upcall_info->userdata));
Jesse Grossccb13522011-10-25 19:26:31 -0700463
Thomas Grafbda56f12013-12-13 15:22:21 +0100464 /* Only reserve room for attribute header, packet data is added
465 * in skb_zerocopy() */
466 if (!(nla = nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, 0))) {
467 err = -ENOBUFS;
468 goto out;
469 }
470 nla->nla_len = nla_attr_size(skb->len);
Jesse Grossccb13522011-10-25 19:26:31 -0700471
Zoltan Kiss36d5fe62014-03-26 22:37:45 +0000472 err = skb_zerocopy(user_skb, skb, skb->len, hlen);
473 if (err)
474 goto out;
Jesse Grossccb13522011-10-25 19:26:31 -0700475
Thomas Grafaea0bb42014-01-14 16:27:49 +0000476 /* Pad OVS_PACKET_ATTR_PACKET if linear copy was performed */
477 if (!(dp->user_features & OVS_DP_F_UNALIGNED)) {
478 size_t plen = NLA_ALIGN(user_skb->len) - user_skb->len;
479
480 if (plen > 0)
481 memset(skb_put(user_skb, plen), 0, plen);
482 }
483
Thomas Grafbda56f12013-12-13 15:22:21 +0100484 ((struct nlmsghdr *) user_skb->data)->nlmsg_len = user_skb->len;
485
Thomas Graf8055a892013-12-13 15:22:20 +0100486 err = genlmsg_unicast(ovs_dp_get_net(dp), user_skb, upcall_info->portid);
Li RongQing4ee45ea2014-09-02 20:52:28 +0800487 user_skb = NULL;
Jesse Grossccb13522011-10-25 19:26:31 -0700488out:
Zoltan Kiss36d5fe62014-03-26 22:37:45 +0000489 if (err)
490 skb_tx_error(skb);
Li RongQing4ee45ea2014-09-02 20:52:28 +0800491 kfree_skb(user_skb);
Jesse Grossccb13522011-10-25 19:26:31 -0700492 kfree_skb(nskb);
493 return err;
494}
495
Jesse Grossccb13522011-10-25 19:26:31 -0700496static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
497{
498 struct ovs_header *ovs_header = info->userhdr;
499 struct nlattr **a = info->attrs;
500 struct sw_flow_actions *acts;
501 struct sk_buff *packet;
502 struct sw_flow *flow;
Lorand Jakabd98612b2014-10-06 05:45:32 -0700503 struct sw_flow_actions *sf_acts;
Jesse Grossccb13522011-10-25 19:26:31 -0700504 struct datapath *dp;
505 struct ethhdr *eth;
Pravin B Shelar83c8df22014-09-15 19:20:31 -0700506 struct vport *input_vport;
Jesse Grossccb13522011-10-25 19:26:31 -0700507 int len;
508 int err;
Jesse Grossccb13522011-10-25 19:26:31 -0700509
510 err = -EINVAL;
511 if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
Thomas Grafdded45fc2013-03-29 14:46:47 +0100512 !a[OVS_PACKET_ATTR_ACTIONS])
Jesse Grossccb13522011-10-25 19:26:31 -0700513 goto err;
514
515 len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
516 packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
517 err = -ENOMEM;
518 if (!packet)
519 goto err;
520 skb_reserve(packet, NET_IP_ALIGN);
521
Thomas Graf32686a92013-03-29 14:46:48 +0100522 nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
Jesse Grossccb13522011-10-25 19:26:31 -0700523
524 skb_reset_mac_header(packet);
525 eth = eth_hdr(packet);
526
527 /* Normally, setting the skb 'protocol' field would be handled by a
528 * call to eth_type_trans(), but it assumes there's a sending
529 * device, which we may not have. */
Simon Hormane5c5d222013-03-28 13:38:25 +0900530 if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN)
Jesse Grossccb13522011-10-25 19:26:31 -0700531 packet->protocol = eth->h_proto;
532 else
533 packet->protocol = htons(ETH_P_802_2);
534
535 /* Build an sw_flow for sending this packet. */
Jarno Rajahalme23dabf82014-03-27 12:35:23 -0700536 flow = ovs_flow_alloc();
Jesse Grossccb13522011-10-25 19:26:31 -0700537 err = PTR_ERR(flow);
538 if (IS_ERR(flow))
539 goto err_kfree_skb;
540
Pravin B Shelar83c8df22014-09-15 19:20:31 -0700541 err = ovs_flow_key_extract_userspace(a[OVS_PACKET_ATTR_KEY], packet,
542 &flow->key);
Jesse Grossccb13522011-10-25 19:26:31 -0700543 if (err)
544 goto err_flow_free;
545
Pravin B Shelare6445712013-10-03 18:16:47 -0700546 err = ovs_nla_copy_actions(a[OVS_PACKET_ATTR_ACTIONS],
Simon Horman25cd9ba2014-10-06 05:05:13 -0700547 &flow->key, &acts);
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700548 if (err)
549 goto err_flow_free;
Jesse Grossccb13522011-10-25 19:26:31 -0700550
Jesse Grossf5796682014-10-03 15:35:33 -0700551 rcu_assign_pointer(flow->sf_acts, acts);
Jesse Grossf5796682014-10-03 15:35:33 -0700552 OVS_CB(packet)->egress_tun_info = NULL;
Jesse Grossccb13522011-10-25 19:26:31 -0700553 packet->priority = flow->key.phy.priority;
Ansis Atteka39c7caeb2012-11-26 11:24:11 -0800554 packet->mark = flow->key.phy.skb_mark;
Jesse Grossccb13522011-10-25 19:26:31 -0700555
556 rcu_read_lock();
Andy Zhoucc3a5ae2014-09-08 13:14:22 -0700557 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -0700558 err = -ENODEV;
559 if (!dp)
560 goto err_unlock;
561
Pravin B Shelar83c8df22014-09-15 19:20:31 -0700562 input_vport = ovs_vport_rcu(dp, flow->key.phy.in_port);
563 if (!input_vport)
564 input_vport = ovs_vport_rcu(dp, OVSP_LOCAL);
565
566 if (!input_vport)
567 goto err_unlock;
568
569 OVS_CB(packet)->input_vport = input_vport;
Lorand Jakabd98612b2014-10-06 05:45:32 -0700570 sf_acts = rcu_dereference(flow->sf_acts);
Pravin B Shelar83c8df22014-09-15 19:20:31 -0700571
Jesse Grossccb13522011-10-25 19:26:31 -0700572 local_bh_disable();
Lorand Jakabd98612b2014-10-06 05:45:32 -0700573 err = ovs_execute_actions(dp, packet, sf_acts, &flow->key);
Jesse Grossccb13522011-10-25 19:26:31 -0700574 local_bh_enable();
575 rcu_read_unlock();
576
Andy Zhou03f0d912013-08-07 20:01:00 -0700577 ovs_flow_free(flow, false);
Jesse Grossccb13522011-10-25 19:26:31 -0700578 return err;
579
580err_unlock:
581 rcu_read_unlock();
582err_flow_free:
Andy Zhou03f0d912013-08-07 20:01:00 -0700583 ovs_flow_free(flow, false);
Jesse Grossccb13522011-10-25 19:26:31 -0700584err_kfree_skb:
585 kfree_skb(packet);
586err:
587 return err;
588}
589
590static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
Thomas Grafdded45fc2013-03-29 14:46:47 +0100591 [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
Jesse Grossccb13522011-10-25 19:26:31 -0700592 [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
593 [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
594};
595
Johannes Berg4534de82013-11-14 17:14:46 +0100596static const struct genl_ops dp_packet_genl_ops[] = {
Jesse Grossccb13522011-10-25 19:26:31 -0700597 { .cmd = OVS_PACKET_CMD_EXECUTE,
598 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
599 .policy = packet_policy,
600 .doit = ovs_packet_cmd_execute
601 }
602};
603
Pravin B Shelar0c200ef2014-05-06 16:44:50 -0700604static struct genl_family dp_packet_genl_family = {
605 .id = GENL_ID_GENERATE,
606 .hdrsize = sizeof(struct ovs_header),
607 .name = OVS_PACKET_FAMILY,
608 .version = OVS_PACKET_VERSION,
609 .maxattr = OVS_PACKET_ATTR_MAX,
610 .netnsok = true,
611 .parallel_ops = true,
612 .ops = dp_packet_genl_ops,
613 .n_ops = ARRAY_SIZE(dp_packet_genl_ops),
614};
615
Andy Zhou1bd71162013-10-22 10:42:46 -0700616static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats,
617 struct ovs_dp_megaflow_stats *mega_stats)
Jesse Grossccb13522011-10-25 19:26:31 -0700618{
619 int i;
Jesse Grossccb13522011-10-25 19:26:31 -0700620
Andy Zhou1bd71162013-10-22 10:42:46 -0700621 memset(mega_stats, 0, sizeof(*mega_stats));
622
Pravin B Shelarb637e492013-10-04 00:14:23 -0700623 stats->n_flows = ovs_flow_tbl_count(&dp->table);
Andy Zhou1bd71162013-10-22 10:42:46 -0700624 mega_stats->n_masks = ovs_flow_tbl_num_masks(&dp->table);
Jesse Grossccb13522011-10-25 19:26:31 -0700625
626 stats->n_hit = stats->n_missed = stats->n_lost = 0;
Andy Zhou1bd71162013-10-22 10:42:46 -0700627
Jesse Grossccb13522011-10-25 19:26:31 -0700628 for_each_possible_cpu(i) {
629 const struct dp_stats_percpu *percpu_stats;
630 struct dp_stats_percpu local_stats;
631 unsigned int start;
632
633 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
634
635 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700636 start = u64_stats_fetch_begin_irq(&percpu_stats->syncp);
Jesse Grossccb13522011-10-25 19:26:31 -0700637 local_stats = *percpu_stats;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700638 } while (u64_stats_fetch_retry_irq(&percpu_stats->syncp, start));
Jesse Grossccb13522011-10-25 19:26:31 -0700639
640 stats->n_hit += local_stats.n_hit;
641 stats->n_missed += local_stats.n_missed;
642 stats->n_lost += local_stats.n_lost;
Andy Zhou1bd71162013-10-22 10:42:46 -0700643 mega_stats->n_mask_hit += local_stats.n_mask_hit;
Jesse Grossccb13522011-10-25 19:26:31 -0700644 }
645}
646
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100647static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts)
648{
649 return NLMSG_ALIGN(sizeof(struct ovs_header))
Joe Stringer41af73e2014-10-18 16:14:14 -0700650 + nla_total_size(ovs_key_attr_size()) /* OVS_FLOW_ATTR_KEY */
651 + nla_total_size(ovs_key_attr_size()) /* OVS_FLOW_ATTR_MASK */
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100652 + nla_total_size(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
653 + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
654 + nla_total_size(8) /* OVS_FLOW_ATTR_USED */
655 + nla_total_size(acts->actions_len); /* OVS_FLOW_ATTR_ACTIONS */
656}
657
Jarno Rajahalmebb6f9a72014-05-05 11:32:17 -0700658/* Called with ovs_mutex or RCU read lock. */
Joe Stringerca7105f2014-09-08 13:09:37 -0700659static int ovs_flow_cmd_fill_match(const struct sw_flow *flow,
660 struct sk_buff *skb)
Jesse Grossccb13522011-10-25 19:26:31 -0700661{
Jesse Grossccb13522011-10-25 19:26:31 -0700662 struct nlattr *nla;
Jesse Grossccb13522011-10-25 19:26:31 -0700663 int err;
664
Andy Zhou03f0d912013-08-07 20:01:00 -0700665 /* Fill flow key. */
Jesse Grossccb13522011-10-25 19:26:31 -0700666 nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
667 if (!nla)
Joe Stringerca7105f2014-09-08 13:09:37 -0700668 return -EMSGSIZE;
Andy Zhou03f0d912013-08-07 20:01:00 -0700669
Pravin B Shelare6445712013-10-03 18:16:47 -0700670 err = ovs_nla_put_flow(&flow->unmasked_key, &flow->unmasked_key, skb);
Jesse Grossccb13522011-10-25 19:26:31 -0700671 if (err)
Joe Stringerca7105f2014-09-08 13:09:37 -0700672 return err;
673
Jesse Grossccb13522011-10-25 19:26:31 -0700674 nla_nest_end(skb, nla);
675
Joe Stringerca7105f2014-09-08 13:09:37 -0700676 /* Fill flow mask. */
Andy Zhou03f0d912013-08-07 20:01:00 -0700677 nla = nla_nest_start(skb, OVS_FLOW_ATTR_MASK);
678 if (!nla)
Joe Stringerca7105f2014-09-08 13:09:37 -0700679 return -EMSGSIZE;
Andy Zhou03f0d912013-08-07 20:01:00 -0700680
Pravin B Shelare6445712013-10-03 18:16:47 -0700681 err = ovs_nla_put_flow(&flow->key, &flow->mask->key, skb);
Andy Zhou03f0d912013-08-07 20:01:00 -0700682 if (err)
Joe Stringerca7105f2014-09-08 13:09:37 -0700683 return err;
Andy Zhou03f0d912013-08-07 20:01:00 -0700684
685 nla_nest_end(skb, nla);
Joe Stringerca7105f2014-09-08 13:09:37 -0700686 return 0;
687}
688
689/* Called with ovs_mutex or RCU read lock. */
690static int ovs_flow_cmd_fill_stats(const struct sw_flow *flow,
691 struct sk_buff *skb)
692{
693 struct ovs_flow_stats stats;
694 __be16 tcp_flags;
695 unsigned long used;
Andy Zhou03f0d912013-08-07 20:01:00 -0700696
Pravin B Shelare298e502013-10-29 17:22:21 -0700697 ovs_flow_stats_get(flow, &stats, &used, &tcp_flags);
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700698
David S. Miller028d6a62012-03-29 23:20:48 -0400699 if (used &&
700 nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
Joe Stringerca7105f2014-09-08 13:09:37 -0700701 return -EMSGSIZE;
Jesse Grossccb13522011-10-25 19:26:31 -0700702
David S. Miller028d6a62012-03-29 23:20:48 -0400703 if (stats.n_packets &&
Pravin B Shelare298e502013-10-29 17:22:21 -0700704 nla_put(skb, OVS_FLOW_ATTR_STATS, sizeof(struct ovs_flow_stats), &stats))
Joe Stringerca7105f2014-09-08 13:09:37 -0700705 return -EMSGSIZE;
Jesse Grossccb13522011-10-25 19:26:31 -0700706
Pravin B Shelare298e502013-10-29 17:22:21 -0700707 if ((u8)ntohs(tcp_flags) &&
708 nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, (u8)ntohs(tcp_flags)))
Joe Stringerca7105f2014-09-08 13:09:37 -0700709 return -EMSGSIZE;
710
711 return 0;
712}
713
714/* Called with ovs_mutex or RCU read lock. */
715static int ovs_flow_cmd_fill_actions(const struct sw_flow *flow,
716 struct sk_buff *skb, int skb_orig_len)
717{
718 struct nlattr *start;
719 int err;
Jesse Grossccb13522011-10-25 19:26:31 -0700720
721 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
722 * this is the first flow to be dumped into 'skb'. This is unusual for
723 * Netlink but individual action lists can be longer than
724 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
725 * The userspace caller can always fetch the actions separately if it
726 * really wants them. (Most userspace callers in fact don't care.)
727 *
728 * This can only fail for dump operations because the skb is always
729 * properly sized for single flows.
730 */
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700731 start = nla_nest_start(skb, OVS_FLOW_ATTR_ACTIONS);
732 if (start) {
Pravin B Shelard57170b2013-07-30 15:39:39 -0700733 const struct sw_flow_actions *sf_acts;
734
Jesse Gross663efa32013-12-03 10:58:53 -0800735 sf_acts = rcu_dereference_ovsl(flow->sf_acts);
Pravin B Shelare6445712013-10-03 18:16:47 -0700736 err = ovs_nla_put_actions(sf_acts->actions,
737 sf_acts->actions_len, skb);
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700738
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700739 if (!err)
740 nla_nest_end(skb, start);
741 else {
742 if (skb_orig_len)
Joe Stringerca7105f2014-09-08 13:09:37 -0700743 return err;
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700744
745 nla_nest_cancel(skb, start);
746 }
Joe Stringerca7105f2014-09-08 13:09:37 -0700747 } else if (skb_orig_len) {
748 return -EMSGSIZE;
749 }
750
751 return 0;
752}
753
754/* Called with ovs_mutex or RCU read lock. */
755static int ovs_flow_cmd_fill_info(const struct sw_flow *flow, int dp_ifindex,
756 struct sk_buff *skb, u32 portid,
757 u32 seq, u32 flags, u8 cmd)
758{
759 const int skb_orig_len = skb->len;
760 struct ovs_header *ovs_header;
761 int err;
762
763 ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family,
764 flags, cmd);
765 if (!ovs_header)
766 return -EMSGSIZE;
767
768 ovs_header->dp_ifindex = dp_ifindex;
769
770 err = ovs_flow_cmd_fill_match(flow, skb);
771 if (err)
772 goto error;
773
774 err = ovs_flow_cmd_fill_stats(flow, skb);
775 if (err)
776 goto error;
777
778 err = ovs_flow_cmd_fill_actions(flow, skb, skb_orig_len);
779 if (err)
780 goto error;
Jesse Grossccb13522011-10-25 19:26:31 -0700781
782 return genlmsg_end(skb, ovs_header);
783
Jesse Grossccb13522011-10-25 19:26:31 -0700784error:
785 genlmsg_cancel(skb, ovs_header);
786 return err;
787}
788
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700789/* May not be called with RCU read lock. */
790static struct sk_buff *ovs_flow_cmd_alloc_info(const struct sw_flow_actions *acts,
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700791 struct genl_info *info,
792 bool always)
Jesse Grossccb13522011-10-25 19:26:31 -0700793{
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700794 struct sk_buff *skb;
Jesse Grossccb13522011-10-25 19:26:31 -0700795
Samuel Gauthier9b67aa42014-09-18 10:31:04 +0200796 if (!always && !ovs_must_notify(&dp_flow_genl_family, info, 0))
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700797 return NULL;
798
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700799 skb = genlmsg_new_unicast(ovs_flow_cmd_msg_size(acts), info, GFP_KERNEL);
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700800 if (!skb)
801 return ERR_PTR(-ENOMEM);
802
803 return skb;
Jesse Grossccb13522011-10-25 19:26:31 -0700804}
805
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700806/* Called with ovs_mutex. */
807static struct sk_buff *ovs_flow_cmd_build_info(const struct sw_flow *flow,
808 int dp_ifindex,
809 struct genl_info *info, u8 cmd,
810 bool always)
Jesse Grossccb13522011-10-25 19:26:31 -0700811{
812 struct sk_buff *skb;
813 int retval;
814
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700815 skb = ovs_flow_cmd_alloc_info(ovsl_dereference(flow->sf_acts), info,
816 always);
Himangi Saraogid0e992a2014-07-27 12:37:46 +0530817 if (IS_ERR_OR_NULL(skb))
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700818 return skb;
Jesse Grossccb13522011-10-25 19:26:31 -0700819
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700820 retval = ovs_flow_cmd_fill_info(flow, dp_ifindex, skb,
821 info->snd_portid, info->snd_seq, 0,
822 cmd);
Jesse Grossccb13522011-10-25 19:26:31 -0700823 BUG_ON(retval < 0);
824 return skb;
825}
826
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700827static int ovs_flow_cmd_new(struct sk_buff *skb, struct genl_info *info)
Jesse Grossccb13522011-10-25 19:26:31 -0700828{
829 struct nlattr **a = info->attrs;
830 struct ovs_header *ovs_header = info->userhdr;
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700831 struct sw_flow *flow, *new_flow;
Andy Zhou03f0d912013-08-07 20:01:00 -0700832 struct sw_flow_mask mask;
Jesse Grossccb13522011-10-25 19:26:31 -0700833 struct sk_buff *reply;
834 struct datapath *dp;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700835 struct sw_flow_actions *acts;
836 struct sw_flow_match match;
837 int error;
838
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700839 /* Must have key and actions. */
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700840 error = -EINVAL;
Jesse Gross426cda52014-10-06 05:08:38 -0700841 if (!a[OVS_FLOW_ATTR_KEY]) {
842 OVS_NLERR("Flow key attribute not present in new flow.\n");
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700843 goto error;
Jesse Gross426cda52014-10-06 05:08:38 -0700844 }
845 if (!a[OVS_FLOW_ATTR_ACTIONS]) {
846 OVS_NLERR("Flow actions attribute not present in new flow.\n");
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700847 goto error;
Jesse Gross426cda52014-10-06 05:08:38 -0700848 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700849
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700850 /* Most of the time we need to allocate a new flow, do it before
851 * locking.
852 */
853 new_flow = ovs_flow_alloc();
854 if (IS_ERR(new_flow)) {
855 error = PTR_ERR(new_flow);
856 goto error;
857 }
858
859 /* Extract key. */
860 ovs_match_init(&match, &new_flow->unmasked_key, &mask);
861 error = ovs_nla_get_match(&match,
862 a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
863 if (error)
864 goto err_kfree_flow;
865
866 ovs_flow_mask_key(&new_flow->key, &new_flow->unmasked_key, &mask);
867
868 /* Validate actions. */
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700869 error = ovs_nla_copy_actions(a[OVS_FLOW_ATTR_ACTIONS], &new_flow->key,
Simon Horman25cd9ba2014-10-06 05:05:13 -0700870 &acts);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700871 if (error) {
872 OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
Pravin B Shelar2fdb9572014-10-19 11:19:51 -0700873 goto err_kfree_flow;
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700874 }
875
876 reply = ovs_flow_cmd_alloc_info(acts, info, false);
877 if (IS_ERR(reply)) {
878 error = PTR_ERR(reply);
879 goto err_kfree_acts;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700880 }
881
882 ovs_lock();
883 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700884 if (unlikely(!dp)) {
885 error = -ENODEV;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700886 goto err_unlock_ovs;
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700887 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700888 /* Check if this is a duplicate flow */
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700889 flow = ovs_flow_tbl_lookup(&dp->table, &new_flow->unmasked_key);
890 if (likely(!flow)) {
891 rcu_assign_pointer(new_flow->sf_acts, acts);
892
893 /* Put flow in bucket. */
894 error = ovs_flow_tbl_insert(&dp->table, new_flow, &mask);
895 if (unlikely(error)) {
896 acts = NULL;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700897 goto err_unlock_ovs;
898 }
899
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700900 if (unlikely(reply)) {
901 error = ovs_flow_cmd_fill_info(new_flow,
902 ovs_header->dp_ifindex,
903 reply, info->snd_portid,
904 info->snd_seq, 0,
905 OVS_FLOW_CMD_NEW);
906 BUG_ON(error < 0);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700907 }
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700908 ovs_unlock();
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700909 } else {
910 struct sw_flow_actions *old_acts;
911
912 /* Bail out if we're not allowed to modify an existing flow.
913 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
914 * because Generic Netlink treats the latter as a dump
915 * request. We also accept NLM_F_EXCL in case that bug ever
916 * gets fixed.
917 */
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700918 if (unlikely(info->nlhdr->nlmsg_flags & (NLM_F_CREATE
919 | NLM_F_EXCL))) {
920 error = -EEXIST;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700921 goto err_unlock_ovs;
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700922 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700923 /* The unmasked key has to be the same for flow updates. */
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700924 if (unlikely(!ovs_flow_cmp_unmasked_key(flow, &match))) {
Alex Wang4a46b242014-06-30 20:30:29 -0700925 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
926 if (!flow) {
927 error = -ENOENT;
928 goto err_unlock_ovs;
929 }
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700930 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700931 /* Update actions. */
932 old_acts = ovsl_dereference(flow->sf_acts);
933 rcu_assign_pointer(flow->sf_acts, acts);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700934
935 if (unlikely(reply)) {
936 error = ovs_flow_cmd_fill_info(flow,
937 ovs_header->dp_ifindex,
938 reply, info->snd_portid,
939 info->snd_seq, 0,
940 OVS_FLOW_CMD_NEW);
941 BUG_ON(error < 0);
942 }
943 ovs_unlock();
944
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700945 ovs_nla_free_flow_actions(old_acts);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700946 ovs_flow_free(new_flow, false);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700947 }
948
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700949 if (reply)
950 ovs_notify(&dp_flow_genl_family, reply, info);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700951 return 0;
952
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700953err_unlock_ovs:
954 ovs_unlock();
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700955 kfree_skb(reply);
956err_kfree_acts:
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700957 kfree(acts);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700958err_kfree_flow:
959 ovs_flow_free(new_flow, false);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700960error:
961 return error;
962}
963
Pravin B Shelar2fdb9572014-10-19 11:19:51 -0700964/* Factor out action copy to avoid "Wframe-larger-than=1024" warning. */
Jesse Gross6b205b22014-10-03 15:35:32 -0700965static struct sw_flow_actions *get_flow_actions(const struct nlattr *a,
966 const struct sw_flow_key *key,
967 const struct sw_flow_mask *mask)
968{
969 struct sw_flow_actions *acts;
970 struct sw_flow_key masked_key;
971 int error;
972
Jesse Gross6b205b22014-10-03 15:35:32 -0700973 ovs_flow_mask_key(&masked_key, key, mask);
Simon Horman25cd9ba2014-10-06 05:05:13 -0700974 error = ovs_nla_copy_actions(a, &masked_key, &acts);
Jesse Gross6b205b22014-10-03 15:35:32 -0700975 if (error) {
Pravin B Shelar2fdb9572014-10-19 11:19:51 -0700976 OVS_NLERR("Actions may not be safe on all matching packets.\n");
Jesse Gross6b205b22014-10-03 15:35:32 -0700977 return ERR_PTR(error);
978 }
979
980 return acts;
981}
982
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700983static int ovs_flow_cmd_set(struct sk_buff *skb, struct genl_info *info)
984{
985 struct nlattr **a = info->attrs;
986 struct ovs_header *ovs_header = info->userhdr;
Jesse Gross6b205b22014-10-03 15:35:32 -0700987 struct sw_flow_key key;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700988 struct sw_flow *flow;
989 struct sw_flow_mask mask;
990 struct sk_buff *reply = NULL;
991 struct datapath *dp;
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700992 struct sw_flow_actions *old_acts = NULL, *acts = NULL;
Andy Zhou03f0d912013-08-07 20:01:00 -0700993 struct sw_flow_match match;
Jesse Grossccb13522011-10-25 19:26:31 -0700994 int error;
Jesse Grossccb13522011-10-25 19:26:31 -0700995
996 /* Extract key. */
997 error = -EINVAL;
Jesse Gross426cda52014-10-06 05:08:38 -0700998 if (!a[OVS_FLOW_ATTR_KEY]) {
999 OVS_NLERR("Flow key attribute not present in set flow.\n");
Jesse Grossccb13522011-10-25 19:26:31 -07001000 goto error;
Jesse Gross426cda52014-10-06 05:08:38 -07001001 }
Andy Zhou03f0d912013-08-07 20:01:00 -07001002
1003 ovs_match_init(&match, &key, &mask);
Jarno Rajahalme23dabf82014-03-27 12:35:23 -07001004 error = ovs_nla_get_match(&match,
Pravin B Shelare6445712013-10-03 18:16:47 -07001005 a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
Jesse Grossccb13522011-10-25 19:26:31 -07001006 if (error)
1007 goto error;
1008
1009 /* Validate actions. */
1010 if (a[OVS_FLOW_ATTR_ACTIONS]) {
Jesse Gross6b205b22014-10-03 15:35:32 -07001011 acts = get_flow_actions(a[OVS_FLOW_ATTR_ACTIONS], &key, &mask);
1012 if (IS_ERR(acts)) {
1013 error = PTR_ERR(acts);
Jesse Grossccb13522011-10-25 19:26:31 -07001014 goto error;
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001015 }
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001016
Pravin B Shelar2fdb9572014-10-19 11:19:51 -07001017 /* Can allocate before locking if have acts. */
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001018 reply = ovs_flow_cmd_alloc_info(acts, info, false);
1019 if (IS_ERR(reply)) {
1020 error = PTR_ERR(reply);
1021 goto err_kfree_acts;
Andy Zhou03f0d912013-08-07 20:01:00 -07001022 }
Jesse Grossccb13522011-10-25 19:26:31 -07001023 }
1024
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001025 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001026 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001027 if (unlikely(!dp)) {
1028 error = -ENODEV;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001029 goto err_unlock_ovs;
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001030 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001031 /* Check that the flow exists. */
Alex Wang4a46b242014-06-30 20:30:29 -07001032 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001033 if (unlikely(!flow)) {
1034 error = -ENOENT;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001035 goto err_unlock_ovs;
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001036 }
Alex Wang4a46b242014-06-30 20:30:29 -07001037
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001038 /* Update actions, if present. */
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001039 if (likely(acts)) {
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001040 old_acts = ovsl_dereference(flow->sf_acts);
Jesse Grossccb13522011-10-25 19:26:31 -07001041 rcu_assign_pointer(flow->sf_acts, acts);
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001042
1043 if (unlikely(reply)) {
1044 error = ovs_flow_cmd_fill_info(flow,
1045 ovs_header->dp_ifindex,
1046 reply, info->snd_portid,
1047 info->snd_seq, 0,
1048 OVS_FLOW_CMD_NEW);
1049 BUG_ON(error < 0);
1050 }
1051 } else {
1052 /* Could not alloc without acts before locking. */
1053 reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex,
1054 info, OVS_FLOW_CMD_NEW, false);
1055 if (unlikely(IS_ERR(reply))) {
1056 error = PTR_ERR(reply);
1057 goto err_unlock_ovs;
1058 }
Jesse Grossccb13522011-10-25 19:26:31 -07001059 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001060
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001061 /* Clear stats. */
1062 if (a[OVS_FLOW_ATTR_CLEAR])
1063 ovs_flow_stats_clear(flow);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001064 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001065
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001066 if (reply)
1067 ovs_notify(&dp_flow_genl_family, reply, info);
1068 if (old_acts)
1069 ovs_nla_free_flow_actions(old_acts);
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -07001070
Jesse Grossccb13522011-10-25 19:26:31 -07001071 return 0;
1072
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001073err_unlock_ovs:
1074 ovs_unlock();
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001075 kfree_skb(reply);
1076err_kfree_acts:
Pravin B Shelar74f84a52013-06-17 17:50:12 -07001077 kfree(acts);
Jesse Grossccb13522011-10-25 19:26:31 -07001078error:
1079 return error;
1080}
1081
1082static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1083{
1084 struct nlattr **a = info->attrs;
1085 struct ovs_header *ovs_header = info->userhdr;
1086 struct sw_flow_key key;
1087 struct sk_buff *reply;
1088 struct sw_flow *flow;
1089 struct datapath *dp;
Andy Zhou03f0d912013-08-07 20:01:00 -07001090 struct sw_flow_match match;
Jesse Grossccb13522011-10-25 19:26:31 -07001091 int err;
Jesse Grossccb13522011-10-25 19:26:31 -07001092
Andy Zhou03f0d912013-08-07 20:01:00 -07001093 if (!a[OVS_FLOW_ATTR_KEY]) {
1094 OVS_NLERR("Flow get message rejected, Key attribute missing.\n");
Jesse Grossccb13522011-10-25 19:26:31 -07001095 return -EINVAL;
Andy Zhou03f0d912013-08-07 20:01:00 -07001096 }
1097
1098 ovs_match_init(&match, &key, NULL);
Jarno Rajahalme23dabf82014-03-27 12:35:23 -07001099 err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL);
Jesse Grossccb13522011-10-25 19:26:31 -07001100 if (err)
1101 return err;
1102
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001103 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001104 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001105 if (!dp) {
1106 err = -ENODEV;
1107 goto unlock;
1108 }
Jesse Grossccb13522011-10-25 19:26:31 -07001109
Alex Wang4a46b242014-06-30 20:30:29 -07001110 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1111 if (!flow) {
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001112 err = -ENOENT;
1113 goto unlock;
1114 }
Jesse Grossccb13522011-10-25 19:26:31 -07001115
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -07001116 reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex, info,
1117 OVS_FLOW_CMD_NEW, true);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001118 if (IS_ERR(reply)) {
1119 err = PTR_ERR(reply);
1120 goto unlock;
1121 }
Jesse Grossccb13522011-10-25 19:26:31 -07001122
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001123 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001124 return genlmsg_reply(reply, info);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001125unlock:
1126 ovs_unlock();
1127 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001128}
1129
1130static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1131{
1132 struct nlattr **a = info->attrs;
1133 struct ovs_header *ovs_header = info->userhdr;
1134 struct sw_flow_key key;
1135 struct sk_buff *reply;
1136 struct sw_flow *flow;
1137 struct datapath *dp;
Andy Zhou03f0d912013-08-07 20:01:00 -07001138 struct sw_flow_match match;
Jesse Grossccb13522011-10-25 19:26:31 -07001139 int err;
Jesse Grossccb13522011-10-25 19:26:31 -07001140
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001141 if (likely(a[OVS_FLOW_ATTR_KEY])) {
1142 ovs_match_init(&match, &key, NULL);
1143 err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL);
1144 if (unlikely(err))
1145 return err;
1146 }
1147
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001148 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001149 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001150 if (unlikely(!dp)) {
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001151 err = -ENODEV;
1152 goto unlock;
1153 }
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001154
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001155 if (unlikely(!a[OVS_FLOW_ATTR_KEY])) {
Pravin B Shelarb637e492013-10-04 00:14:23 -07001156 err = ovs_flow_tbl_flush(&dp->table);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001157 goto unlock;
1158 }
Andy Zhou03f0d912013-08-07 20:01:00 -07001159
Alex Wang4a46b242014-06-30 20:30:29 -07001160 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1161 if (unlikely(!flow)) {
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001162 err = -ENOENT;
1163 goto unlock;
1164 }
Jesse Grossccb13522011-10-25 19:26:31 -07001165
Pravin B Shelarb637e492013-10-04 00:14:23 -07001166 ovs_flow_tbl_remove(&dp->table, flow);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001167 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001168
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001169 reply = ovs_flow_cmd_alloc_info((const struct sw_flow_actions __force *) flow->sf_acts,
1170 info, false);
1171 if (likely(reply)) {
1172 if (likely(!IS_ERR(reply))) {
1173 rcu_read_lock(); /*To keep RCU checker happy. */
1174 err = ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex,
1175 reply, info->snd_portid,
1176 info->snd_seq, 0,
1177 OVS_FLOW_CMD_DEL);
1178 rcu_read_unlock();
1179 BUG_ON(err < 0);
1180
1181 ovs_notify(&dp_flow_genl_family, reply, info);
1182 } else {
1183 netlink_set_err(sock_net(skb->sk)->genl_sock, 0, 0, PTR_ERR(reply));
1184 }
1185 }
1186
1187 ovs_flow_free(flow, true);
Jesse Grossccb13522011-10-25 19:26:31 -07001188 return 0;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001189unlock:
1190 ovs_unlock();
1191 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001192}
1193
1194static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1195{
1196 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
Pravin B Shelarb637e492013-10-04 00:14:23 -07001197 struct table_instance *ti;
Jesse Grossccb13522011-10-25 19:26:31 -07001198 struct datapath *dp;
Jesse Grossccb13522011-10-25 19:26:31 -07001199
Pravin B Shelard57170b2013-07-30 15:39:39 -07001200 rcu_read_lock();
Andy Zhoucc3a5ae2014-09-08 13:14:22 -07001201 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001202 if (!dp) {
Pravin B Shelard57170b2013-07-30 15:39:39 -07001203 rcu_read_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001204 return -ENODEV;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001205 }
Jesse Grossccb13522011-10-25 19:26:31 -07001206
Pravin B Shelarb637e492013-10-04 00:14:23 -07001207 ti = rcu_dereference(dp->table.ti);
Jesse Grossccb13522011-10-25 19:26:31 -07001208 for (;;) {
1209 struct sw_flow *flow;
1210 u32 bucket, obj;
1211
1212 bucket = cb->args[0];
1213 obj = cb->args[1];
Pravin B Shelarb637e492013-10-04 00:14:23 -07001214 flow = ovs_flow_tbl_dump_next(ti, &bucket, &obj);
Jesse Grossccb13522011-10-25 19:26:31 -07001215 if (!flow)
1216 break;
1217
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -07001218 if (ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex, skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001219 NETLINK_CB(cb->skb).portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001220 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1221 OVS_FLOW_CMD_NEW) < 0)
1222 break;
1223
1224 cb->args[0] = bucket;
1225 cb->args[1] = obj;
1226 }
Pravin B Shelard57170b2013-07-30 15:39:39 -07001227 rcu_read_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001228 return skb->len;
1229}
1230
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001231static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
1232 [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
1233 [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
1234 [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
1235};
1236
stephen hemminger48e48a72014-07-16 11:25:52 -07001237static const struct genl_ops dp_flow_genl_ops[] = {
Jesse Grossccb13522011-10-25 19:26:31 -07001238 { .cmd = OVS_FLOW_CMD_NEW,
1239 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1240 .policy = flow_policy,
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001241 .doit = ovs_flow_cmd_new
Jesse Grossccb13522011-10-25 19:26:31 -07001242 },
1243 { .cmd = OVS_FLOW_CMD_DEL,
1244 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1245 .policy = flow_policy,
1246 .doit = ovs_flow_cmd_del
1247 },
1248 { .cmd = OVS_FLOW_CMD_GET,
1249 .flags = 0, /* OK for unprivileged users. */
1250 .policy = flow_policy,
1251 .doit = ovs_flow_cmd_get,
1252 .dumpit = ovs_flow_cmd_dump
1253 },
1254 { .cmd = OVS_FLOW_CMD_SET,
1255 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1256 .policy = flow_policy,
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001257 .doit = ovs_flow_cmd_set,
Jesse Grossccb13522011-10-25 19:26:31 -07001258 },
1259};
1260
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001261static struct genl_family dp_flow_genl_family = {
Jesse Grossccb13522011-10-25 19:26:31 -07001262 .id = GENL_ID_GENERATE,
1263 .hdrsize = sizeof(struct ovs_header),
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001264 .name = OVS_FLOW_FAMILY,
1265 .version = OVS_FLOW_VERSION,
1266 .maxattr = OVS_FLOW_ATTR_MAX,
Pravin B Shelar3a4e0d62013-04-23 07:48:48 +00001267 .netnsok = true,
1268 .parallel_ops = true,
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001269 .ops = dp_flow_genl_ops,
1270 .n_ops = ARRAY_SIZE(dp_flow_genl_ops),
1271 .mcgrps = &ovs_dp_flow_multicast_group,
1272 .n_mcgrps = 1,
Jesse Grossccb13522011-10-25 19:26:31 -07001273};
1274
Thomas Grafc3ff8cf2013-03-29 14:46:49 +01001275static size_t ovs_dp_cmd_msg_size(void)
1276{
1277 size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
1278
1279 msgsize += nla_total_size(IFNAMSIZ);
1280 msgsize += nla_total_size(sizeof(struct ovs_dp_stats));
Andy Zhou1bd71162013-10-22 10:42:46 -07001281 msgsize += nla_total_size(sizeof(struct ovs_dp_megaflow_stats));
Daniele Di Proietto45fb9c32014-01-23 10:47:35 -08001282 msgsize += nla_total_size(sizeof(u32)); /* OVS_DP_ATTR_USER_FEATURES */
Thomas Grafc3ff8cf2013-03-29 14:46:49 +01001283
1284 return msgsize;
1285}
1286
Jarno Rajahalmebb6f9a72014-05-05 11:32:17 -07001287/* Called with ovs_mutex or RCU read lock. */
Jesse Grossccb13522011-10-25 19:26:31 -07001288static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001289 u32 portid, u32 seq, u32 flags, u8 cmd)
Jesse Grossccb13522011-10-25 19:26:31 -07001290{
1291 struct ovs_header *ovs_header;
1292 struct ovs_dp_stats dp_stats;
Andy Zhou1bd71162013-10-22 10:42:46 -07001293 struct ovs_dp_megaflow_stats dp_megaflow_stats;
Jesse Grossccb13522011-10-25 19:26:31 -07001294 int err;
1295
Eric W. Biederman15e47302012-09-07 20:12:54 +00001296 ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
Jesse Grossccb13522011-10-25 19:26:31 -07001297 flags, cmd);
1298 if (!ovs_header)
1299 goto error;
1300
1301 ovs_header->dp_ifindex = get_dpifindex(dp);
1302
Jesse Grossccb13522011-10-25 19:26:31 -07001303 err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
Jesse Grossccb13522011-10-25 19:26:31 -07001304 if (err)
1305 goto nla_put_failure;
1306
Andy Zhou1bd71162013-10-22 10:42:46 -07001307 get_dp_stats(dp, &dp_stats, &dp_megaflow_stats);
1308 if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats),
1309 &dp_stats))
1310 goto nla_put_failure;
1311
1312 if (nla_put(skb, OVS_DP_ATTR_MEGAFLOW_STATS,
1313 sizeof(struct ovs_dp_megaflow_stats),
1314 &dp_megaflow_stats))
David S. Miller028d6a62012-03-29 23:20:48 -04001315 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001316
Thomas Graf43d4be92013-12-13 15:22:18 +01001317 if (nla_put_u32(skb, OVS_DP_ATTR_USER_FEATURES, dp->user_features))
1318 goto nla_put_failure;
1319
Jesse Grossccb13522011-10-25 19:26:31 -07001320 return genlmsg_end(skb, ovs_header);
1321
1322nla_put_failure:
1323 genlmsg_cancel(skb, ovs_header);
1324error:
1325 return -EMSGSIZE;
1326}
1327
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001328static struct sk_buff *ovs_dp_cmd_alloc_info(struct genl_info *info)
Jesse Grossccb13522011-10-25 19:26:31 -07001329{
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001330 return genlmsg_new_unicast(ovs_dp_cmd_msg_size(), info, GFP_KERNEL);
Jesse Grossccb13522011-10-25 19:26:31 -07001331}
1332
Jarno Rajahalmebb6f9a72014-05-05 11:32:17 -07001333/* Called with rcu_read_lock or ovs_mutex. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001334static struct datapath *lookup_datapath(struct net *net,
1335 struct ovs_header *ovs_header,
Jesse Grossccb13522011-10-25 19:26:31 -07001336 struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1337{
1338 struct datapath *dp;
1339
1340 if (!a[OVS_DP_ATTR_NAME])
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001341 dp = get_dp(net, ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001342 else {
1343 struct vport *vport;
1344
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001345 vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
Jesse Grossccb13522011-10-25 19:26:31 -07001346 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
Jesse Grossccb13522011-10-25 19:26:31 -07001347 }
1348 return dp ? dp : ERR_PTR(-ENODEV);
1349}
1350
Thomas Graf44da5ae2013-12-13 15:22:19 +01001351static void ovs_dp_reset_user_features(struct sk_buff *skb, struct genl_info *info)
1352{
1353 struct datapath *dp;
1354
1355 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
Jiri Pirko3c7eacf2014-02-14 11:42:36 +01001356 if (IS_ERR(dp))
Thomas Graf44da5ae2013-12-13 15:22:19 +01001357 return;
1358
1359 WARN(dp->user_features, "Dropping previously announced user features\n");
1360 dp->user_features = 0;
1361}
1362
Thomas Graf43d4be92013-12-13 15:22:18 +01001363static void ovs_dp_change(struct datapath *dp, struct nlattr **a)
1364{
1365 if (a[OVS_DP_ATTR_USER_FEATURES])
1366 dp->user_features = nla_get_u32(a[OVS_DP_ATTR_USER_FEATURES]);
1367}
1368
Jesse Grossccb13522011-10-25 19:26:31 -07001369static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1370{
1371 struct nlattr **a = info->attrs;
1372 struct vport_parms parms;
1373 struct sk_buff *reply;
1374 struct datapath *dp;
1375 struct vport *vport;
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001376 struct ovs_net *ovs_net;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001377 int err, i;
Jesse Grossccb13522011-10-25 19:26:31 -07001378
1379 err = -EINVAL;
1380 if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1381 goto err;
1382
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001383 reply = ovs_dp_cmd_alloc_info(info);
1384 if (!reply)
1385 return -ENOMEM;
Jesse Grossccb13522011-10-25 19:26:31 -07001386
1387 err = -ENOMEM;
1388 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1389 if (dp == NULL)
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001390 goto err_free_reply;
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001391
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001392 ovs_dp_set_net(dp, hold_net(sock_net(skb->sk)));
Jesse Grossccb13522011-10-25 19:26:31 -07001393
1394 /* Allocate table. */
Pravin B Shelarb637e492013-10-04 00:14:23 -07001395 err = ovs_flow_tbl_init(&dp->table);
1396 if (err)
Jesse Grossccb13522011-10-25 19:26:31 -07001397 goto err_free_dp;
1398
WANG Cong1c213bd2014-02-13 11:46:28 -08001399 dp->stats_percpu = netdev_alloc_pcpu_stats(struct dp_stats_percpu);
Jesse Grossccb13522011-10-25 19:26:31 -07001400 if (!dp->stats_percpu) {
1401 err = -ENOMEM;
1402 goto err_destroy_table;
1403 }
1404
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001405 dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
Pravin B Shelare6445712013-10-03 18:16:47 -07001406 GFP_KERNEL);
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001407 if (!dp->ports) {
1408 err = -ENOMEM;
1409 goto err_destroy_percpu;
1410 }
1411
1412 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1413 INIT_HLIST_HEAD(&dp->ports[i]);
1414
Jesse Grossccb13522011-10-25 19:26:31 -07001415 /* Set up our datapath device. */
1416 parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1417 parms.type = OVS_VPORT_TYPE_INTERNAL;
1418 parms.options = NULL;
1419 parms.dp = dp;
1420 parms.port_no = OVSP_LOCAL;
Alex Wang5cd667b2014-07-17 15:14:13 -07001421 parms.upcall_portids = a[OVS_DP_ATTR_UPCALL_PID];
Jesse Grossccb13522011-10-25 19:26:31 -07001422
Thomas Graf43d4be92013-12-13 15:22:18 +01001423 ovs_dp_change(dp, a);
1424
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001425 /* So far only local changes have been made, now need the lock. */
1426 ovs_lock();
1427
Jesse Grossccb13522011-10-25 19:26:31 -07001428 vport = new_vport(&parms);
1429 if (IS_ERR(vport)) {
1430 err = PTR_ERR(vport);
1431 if (err == -EBUSY)
1432 err = -EEXIST;
1433
Thomas Graf44da5ae2013-12-13 15:22:19 +01001434 if (err == -EEXIST) {
1435 /* An outdated user space instance that does not understand
1436 * the concept of user_features has attempted to create a new
1437 * datapath and is likely to reuse it. Drop all user features.
1438 */
1439 if (info->genlhdr->version < OVS_DP_VER_FEATURES)
1440 ovs_dp_reset_user_features(skb, info);
1441 }
1442
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001443 goto err_destroy_ports_array;
Jesse Grossccb13522011-10-25 19:26:31 -07001444 }
1445
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001446 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1447 info->snd_seq, 0, OVS_DP_CMD_NEW);
1448 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001449
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001450 ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001451 list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001452
1453 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001454
Johannes Berg2a94fe42013-11-19 15:19:39 +01001455 ovs_notify(&dp_datapath_genl_family, reply, info);
Jesse Grossccb13522011-10-25 19:26:31 -07001456 return 0;
1457
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001458err_destroy_ports_array:
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001459 ovs_unlock();
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001460 kfree(dp->ports);
Jesse Grossccb13522011-10-25 19:26:31 -07001461err_destroy_percpu:
1462 free_percpu(dp->stats_percpu);
1463err_destroy_table:
Pravin B Shelar9b996e52014-05-06 18:41:20 -07001464 ovs_flow_tbl_destroy(&dp->table);
Jesse Grossccb13522011-10-25 19:26:31 -07001465err_free_dp:
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001466 release_net(ovs_dp_get_net(dp));
Jesse Grossccb13522011-10-25 19:26:31 -07001467 kfree(dp);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001468err_free_reply:
1469 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001470err:
1471 return err;
1472}
1473
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001474/* Called with ovs_mutex. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001475static void __dp_destroy(struct datapath *dp)
Jesse Grossccb13522011-10-25 19:26:31 -07001476{
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001477 int i;
Jesse Grossccb13522011-10-25 19:26:31 -07001478
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001479 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1480 struct vport *vport;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001481 struct hlist_node *n;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001482
Sasha Levinb67bfe02013-02-27 17:06:00 -08001483 hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001484 if (vport->port_no != OVSP_LOCAL)
1485 ovs_dp_detach_port(vport);
1486 }
Jesse Grossccb13522011-10-25 19:26:31 -07001487
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001488 list_del_rcu(&dp->list_node);
Jesse Grossccb13522011-10-25 19:26:31 -07001489
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001490 /* OVSP_LOCAL is datapath internal port. We need to make sure that
Andy Zhoue80857c2014-01-21 09:31:04 -08001491 * all ports in datapath are destroyed first before freeing datapath.
Jesse Grossccb13522011-10-25 19:26:31 -07001492 */
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001493 ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
Jesse Grossccb13522011-10-25 19:26:31 -07001494
Andy Zhoue80857c2014-01-21 09:31:04 -08001495 /* RCU destroy the flow table */
Jesse Grossccb13522011-10-25 19:26:31 -07001496 call_rcu(&dp->rcu, destroy_dp_rcu);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001497}
1498
1499static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1500{
1501 struct sk_buff *reply;
1502 struct datapath *dp;
1503 int err;
1504
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001505 reply = ovs_dp_cmd_alloc_info(info);
1506 if (!reply)
1507 return -ENOMEM;
1508
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001509 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001510 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1511 err = PTR_ERR(dp);
1512 if (IS_ERR(dp))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001513 goto err_unlock_free;
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001514
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001515 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1516 info->snd_seq, 0, OVS_DP_CMD_DEL);
1517 BUG_ON(err < 0);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001518
1519 __dp_destroy(dp);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001520 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001521
Johannes Berg2a94fe42013-11-19 15:19:39 +01001522 ovs_notify(&dp_datapath_genl_family, reply, info);
Jesse Grossccb13522011-10-25 19:26:31 -07001523
1524 return 0;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001525
1526err_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001527 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001528 kfree_skb(reply);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001529 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001530}
1531
1532static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1533{
1534 struct sk_buff *reply;
1535 struct datapath *dp;
1536 int err;
1537
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001538 reply = ovs_dp_cmd_alloc_info(info);
1539 if (!reply)
1540 return -ENOMEM;
1541
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001542 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001543 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001544 err = PTR_ERR(dp);
Jesse Grossccb13522011-10-25 19:26:31 -07001545 if (IS_ERR(dp))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001546 goto err_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001547
Thomas Graf43d4be92013-12-13 15:22:18 +01001548 ovs_dp_change(dp, info->attrs);
1549
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001550 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1551 info->snd_seq, 0, OVS_DP_CMD_NEW);
1552 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001553
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001554 ovs_unlock();
Johannes Berg2a94fe42013-11-19 15:19:39 +01001555 ovs_notify(&dp_datapath_genl_family, reply, info);
Jesse Grossccb13522011-10-25 19:26:31 -07001556
1557 return 0;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001558
1559err_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001560 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001561 kfree_skb(reply);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001562 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001563}
1564
1565static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1566{
1567 struct sk_buff *reply;
1568 struct datapath *dp;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001569 int err;
Jesse Grossccb13522011-10-25 19:26:31 -07001570
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001571 reply = ovs_dp_cmd_alloc_info(info);
1572 if (!reply)
1573 return -ENOMEM;
1574
1575 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001576 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001577 if (IS_ERR(dp)) {
1578 err = PTR_ERR(dp);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001579 goto err_unlock_free;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001580 }
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001581 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1582 info->snd_seq, 0, OVS_DP_CMD_NEW);
1583 BUG_ON(err < 0);
1584 rcu_read_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001585
Jesse Grossccb13522011-10-25 19:26:31 -07001586 return genlmsg_reply(reply, info);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001587
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001588err_unlock_free:
1589 rcu_read_unlock();
1590 kfree_skb(reply);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001591 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001592}
1593
1594static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1595{
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001596 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
Jesse Grossccb13522011-10-25 19:26:31 -07001597 struct datapath *dp;
1598 int skip = cb->args[0];
1599 int i = 0;
1600
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001601 rcu_read_lock();
1602 list_for_each_entry_rcu(dp, &ovs_net->dps, list_node) {
Ben Pfaff77676fd2012-01-17 13:33:39 +00001603 if (i >= skip &&
Eric W. Biederman15e47302012-09-07 20:12:54 +00001604 ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001605 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1606 OVS_DP_CMD_NEW) < 0)
1607 break;
1608 i++;
1609 }
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001610 rcu_read_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001611
1612 cb->args[0] = i;
1613
1614 return skb->len;
1615}
1616
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001617static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1618 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1619 [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1620 [OVS_DP_ATTR_USER_FEATURES] = { .type = NLA_U32 },
1621};
1622
stephen hemminger48e48a72014-07-16 11:25:52 -07001623static const struct genl_ops dp_datapath_genl_ops[] = {
Jesse Grossccb13522011-10-25 19:26:31 -07001624 { .cmd = OVS_DP_CMD_NEW,
1625 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1626 .policy = datapath_policy,
1627 .doit = ovs_dp_cmd_new
1628 },
1629 { .cmd = OVS_DP_CMD_DEL,
1630 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1631 .policy = datapath_policy,
1632 .doit = ovs_dp_cmd_del
1633 },
1634 { .cmd = OVS_DP_CMD_GET,
1635 .flags = 0, /* OK for unprivileged users. */
1636 .policy = datapath_policy,
1637 .doit = ovs_dp_cmd_get,
1638 .dumpit = ovs_dp_cmd_dump
1639 },
1640 { .cmd = OVS_DP_CMD_SET,
1641 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1642 .policy = datapath_policy,
1643 .doit = ovs_dp_cmd_set,
1644 },
1645};
1646
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001647static struct genl_family dp_datapath_genl_family = {
Jesse Grossccb13522011-10-25 19:26:31 -07001648 .id = GENL_ID_GENERATE,
1649 .hdrsize = sizeof(struct ovs_header),
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001650 .name = OVS_DATAPATH_FAMILY,
1651 .version = OVS_DATAPATH_VERSION,
1652 .maxattr = OVS_DP_ATTR_MAX,
Pravin B Shelar3a4e0d62013-04-23 07:48:48 +00001653 .netnsok = true,
1654 .parallel_ops = true,
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001655 .ops = dp_datapath_genl_ops,
1656 .n_ops = ARRAY_SIZE(dp_datapath_genl_ops),
1657 .mcgrps = &ovs_dp_datapath_multicast_group,
1658 .n_mcgrps = 1,
Jesse Grossccb13522011-10-25 19:26:31 -07001659};
1660
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001661/* Called with ovs_mutex or RCU read lock. */
Jesse Grossccb13522011-10-25 19:26:31 -07001662static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001663 u32 portid, u32 seq, u32 flags, u8 cmd)
Jesse Grossccb13522011-10-25 19:26:31 -07001664{
1665 struct ovs_header *ovs_header;
1666 struct ovs_vport_stats vport_stats;
1667 int err;
1668
Eric W. Biederman15e47302012-09-07 20:12:54 +00001669 ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
Jesse Grossccb13522011-10-25 19:26:31 -07001670 flags, cmd);
1671 if (!ovs_header)
1672 return -EMSGSIZE;
1673
1674 ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1675
David S. Miller028d6a62012-03-29 23:20:48 -04001676 if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
1677 nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
Alex Wang5cd667b2014-07-17 15:14:13 -07001678 nla_put_string(skb, OVS_VPORT_ATTR_NAME,
1679 vport->ops->get_name(vport)))
David S. Miller028d6a62012-03-29 23:20:48 -04001680 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001681
1682 ovs_vport_get_stats(vport, &vport_stats);
David S. Miller028d6a62012-03-29 23:20:48 -04001683 if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
1684 &vport_stats))
1685 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001686
Alex Wang5cd667b2014-07-17 15:14:13 -07001687 if (ovs_vport_get_upcall_portids(vport, skb))
1688 goto nla_put_failure;
1689
Jesse Grossccb13522011-10-25 19:26:31 -07001690 err = ovs_vport_get_options(vport, skb);
1691 if (err == -EMSGSIZE)
1692 goto error;
1693
1694 return genlmsg_end(skb, ovs_header);
1695
1696nla_put_failure:
1697 err = -EMSGSIZE;
1698error:
1699 genlmsg_cancel(skb, ovs_header);
1700 return err;
1701}
1702
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001703static struct sk_buff *ovs_vport_cmd_alloc_info(void)
1704{
1705 return nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1706}
1707
1708/* Called with ovs_mutex, only via ovs_dp_notify_wq(). */
Eric W. Biederman15e47302012-09-07 20:12:54 +00001709struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001710 u32 seq, u8 cmd)
1711{
1712 struct sk_buff *skb;
1713 int retval;
1714
1715 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1716 if (!skb)
1717 return ERR_PTR(-ENOMEM);
1718
Eric W. Biederman15e47302012-09-07 20:12:54 +00001719 retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
Jesse Grossa9341512013-03-26 15:48:38 -07001720 BUG_ON(retval < 0);
1721
Jesse Grossccb13522011-10-25 19:26:31 -07001722 return skb;
1723}
1724
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001725/* Called with ovs_mutex or RCU read lock. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001726static struct vport *lookup_vport(struct net *net,
1727 struct ovs_header *ovs_header,
Jesse Grossccb13522011-10-25 19:26:31 -07001728 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1729{
1730 struct datapath *dp;
1731 struct vport *vport;
1732
1733 if (a[OVS_VPORT_ATTR_NAME]) {
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001734 vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
Jesse Grossccb13522011-10-25 19:26:31 -07001735 if (!vport)
1736 return ERR_PTR(-ENODEV);
Ben Pfaff651a68e2012-03-06 15:04:04 -08001737 if (ovs_header->dp_ifindex &&
1738 ovs_header->dp_ifindex != get_dpifindex(vport->dp))
1739 return ERR_PTR(-ENODEV);
Jesse Grossccb13522011-10-25 19:26:31 -07001740 return vport;
1741 } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
1742 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1743
1744 if (port_no >= DP_MAX_PORTS)
1745 return ERR_PTR(-EFBIG);
1746
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001747 dp = get_dp(net, ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001748 if (!dp)
1749 return ERR_PTR(-ENODEV);
1750
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001751 vport = ovs_vport_ovsl_rcu(dp, port_no);
Jesse Grossccb13522011-10-25 19:26:31 -07001752 if (!vport)
Jarno Rajahalme14408db2013-01-09 14:27:35 -08001753 return ERR_PTR(-ENODEV);
Jesse Grossccb13522011-10-25 19:26:31 -07001754 return vport;
1755 } else
1756 return ERR_PTR(-EINVAL);
1757}
1758
1759static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
1760{
1761 struct nlattr **a = info->attrs;
1762 struct ovs_header *ovs_header = info->userhdr;
1763 struct vport_parms parms;
1764 struct sk_buff *reply;
1765 struct vport *vport;
1766 struct datapath *dp;
1767 u32 port_no;
1768 int err;
1769
Jesse Grossccb13522011-10-25 19:26:31 -07001770 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
1771 !a[OVS_VPORT_ATTR_UPCALL_PID])
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001772 return -EINVAL;
1773
1774 port_no = a[OVS_VPORT_ATTR_PORT_NO]
1775 ? nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]) : 0;
1776 if (port_no >= DP_MAX_PORTS)
1777 return -EFBIG;
1778
1779 reply = ovs_vport_cmd_alloc_info();
1780 if (!reply)
1781 return -ENOMEM;
Jesse Grossccb13522011-10-25 19:26:31 -07001782
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001783 ovs_lock();
Thomas Graf62b9c8d2014-10-22 17:29:06 +02001784restart:
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001785 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001786 err = -ENODEV;
1787 if (!dp)
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001788 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001789
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001790 if (port_no) {
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001791 vport = ovs_vport_ovsl(dp, port_no);
Jesse Grossccb13522011-10-25 19:26:31 -07001792 err = -EBUSY;
1793 if (vport)
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001794 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001795 } else {
1796 for (port_no = 1; ; port_no++) {
1797 if (port_no >= DP_MAX_PORTS) {
1798 err = -EFBIG;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001799 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001800 }
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001801 vport = ovs_vport_ovsl(dp, port_no);
Jesse Grossccb13522011-10-25 19:26:31 -07001802 if (!vport)
1803 break;
1804 }
1805 }
1806
1807 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
1808 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1809 parms.options = a[OVS_VPORT_ATTR_OPTIONS];
1810 parms.dp = dp;
1811 parms.port_no = port_no;
Alex Wang5cd667b2014-07-17 15:14:13 -07001812 parms.upcall_portids = a[OVS_VPORT_ATTR_UPCALL_PID];
Jesse Grossccb13522011-10-25 19:26:31 -07001813
1814 vport = new_vport(&parms);
1815 err = PTR_ERR(vport);
Thomas Graf62b9c8d2014-10-22 17:29:06 +02001816 if (IS_ERR(vport)) {
1817 if (err == -EAGAIN)
1818 goto restart;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001819 goto exit_unlock_free;
Thomas Graf62b9c8d2014-10-22 17:29:06 +02001820 }
Jesse Grossccb13522011-10-25 19:26:31 -07001821
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001822 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1823 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1824 BUG_ON(err < 0);
1825 ovs_unlock();
Thomas Grafed661182013-03-29 14:46:50 +01001826
Johannes Berg2a94fe42013-11-19 15:19:39 +01001827 ovs_notify(&dp_vport_genl_family, reply, info);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001828 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001829
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001830exit_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001831 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001832 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001833 return err;
1834}
1835
1836static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
1837{
1838 struct nlattr **a = info->attrs;
1839 struct sk_buff *reply;
1840 struct vport *vport;
1841 int err;
1842
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001843 reply = ovs_vport_cmd_alloc_info();
1844 if (!reply)
1845 return -ENOMEM;
1846
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001847 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001848 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
Jesse Grossccb13522011-10-25 19:26:31 -07001849 err = PTR_ERR(vport);
1850 if (IS_ERR(vport))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001851 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001852
Jesse Grossccb13522011-10-25 19:26:31 -07001853 if (a[OVS_VPORT_ATTR_TYPE] &&
Jesse Grossf44f3402013-05-13 08:15:26 -07001854 nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
Jesse Grossccb13522011-10-25 19:26:31 -07001855 err = -EINVAL;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001856 goto exit_unlock_free;
Jesse Grossa9341512013-03-26 15:48:38 -07001857 }
1858
Jesse Grossf44f3402013-05-13 08:15:26 -07001859 if (a[OVS_VPORT_ATTR_OPTIONS]) {
Jesse Grossccb13522011-10-25 19:26:31 -07001860 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
Jesse Grossf44f3402013-05-13 08:15:26 -07001861 if (err)
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001862 goto exit_unlock_free;
Jesse Grossf44f3402013-05-13 08:15:26 -07001863 }
Jesse Grossa9341512013-03-26 15:48:38 -07001864
Alex Wang5cd667b2014-07-17 15:14:13 -07001865
1866 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
1867 struct nlattr *ids = a[OVS_VPORT_ATTR_UPCALL_PID];
1868
1869 err = ovs_vport_set_upcall_portids(vport, ids);
1870 if (err)
1871 goto exit_unlock_free;
1872 }
Jesse Grossccb13522011-10-25 19:26:31 -07001873
Jesse Grossa9341512013-03-26 15:48:38 -07001874 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1875 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1876 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001877
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001878 ovs_unlock();
Johannes Berg2a94fe42013-11-19 15:19:39 +01001879 ovs_notify(&dp_vport_genl_family, reply, info);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001880 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001881
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001882exit_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001883 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001884 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001885 return err;
1886}
1887
1888static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
1889{
1890 struct nlattr **a = info->attrs;
1891 struct sk_buff *reply;
1892 struct vport *vport;
1893 int err;
1894
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001895 reply = ovs_vport_cmd_alloc_info();
1896 if (!reply)
1897 return -ENOMEM;
1898
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001899 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001900 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
Jesse Grossccb13522011-10-25 19:26:31 -07001901 err = PTR_ERR(vport);
1902 if (IS_ERR(vport))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001903 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001904
1905 if (vport->port_no == OVSP_LOCAL) {
1906 err = -EINVAL;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001907 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001908 }
1909
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001910 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1911 info->snd_seq, 0, OVS_VPORT_CMD_DEL);
1912 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001913 ovs_dp_detach_port(vport);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001914 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001915
Johannes Berg2a94fe42013-11-19 15:19:39 +01001916 ovs_notify(&dp_vport_genl_family, reply, info);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001917 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001918
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001919exit_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001920 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001921 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001922 return err;
1923}
1924
1925static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
1926{
1927 struct nlattr **a = info->attrs;
1928 struct ovs_header *ovs_header = info->userhdr;
1929 struct sk_buff *reply;
1930 struct vport *vport;
1931 int err;
1932
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001933 reply = ovs_vport_cmd_alloc_info();
1934 if (!reply)
1935 return -ENOMEM;
1936
Jesse Grossccb13522011-10-25 19:26:31 -07001937 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001938 vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
Jesse Grossccb13522011-10-25 19:26:31 -07001939 err = PTR_ERR(vport);
1940 if (IS_ERR(vport))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001941 goto exit_unlock_free;
1942 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1943 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1944 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001945 rcu_read_unlock();
1946
1947 return genlmsg_reply(reply, info);
1948
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001949exit_unlock_free:
Jesse Grossccb13522011-10-25 19:26:31 -07001950 rcu_read_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001951 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001952 return err;
1953}
1954
1955static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1956{
1957 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1958 struct datapath *dp;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001959 int bucket = cb->args[0], skip = cb->args[1];
1960 int i, j = 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001961
Jesse Grossccb13522011-10-25 19:26:31 -07001962 rcu_read_lock();
Andy Zhoucc3a5ae2014-09-08 13:14:22 -07001963 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
Jarno Rajahalme42ee19e2014-02-15 17:42:29 -08001964 if (!dp) {
1965 rcu_read_unlock();
1966 return -ENODEV;
1967 }
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001968 for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
Jesse Grossccb13522011-10-25 19:26:31 -07001969 struct vport *vport;
1970
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001971 j = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001972 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001973 if (j >= skip &&
1974 ovs_vport_cmd_fill_info(vport, skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001975 NETLINK_CB(cb->skb).portid,
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001976 cb->nlh->nlmsg_seq,
1977 NLM_F_MULTI,
1978 OVS_VPORT_CMD_NEW) < 0)
1979 goto out;
Jesse Grossccb13522011-10-25 19:26:31 -07001980
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001981 j++;
1982 }
1983 skip = 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001984 }
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001985out:
Jesse Grossccb13522011-10-25 19:26:31 -07001986 rcu_read_unlock();
1987
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001988 cb->args[0] = i;
1989 cb->args[1] = j;
Jesse Grossccb13522011-10-25 19:26:31 -07001990
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001991 return skb->len;
Jesse Grossccb13522011-10-25 19:26:31 -07001992}
1993
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001994static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
1995 [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1996 [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
1997 [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
1998 [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
1999 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
2000 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
2001};
2002
stephen hemminger48e48a72014-07-16 11:25:52 -07002003static const struct genl_ops dp_vport_genl_ops[] = {
Jesse Grossccb13522011-10-25 19:26:31 -07002004 { .cmd = OVS_VPORT_CMD_NEW,
2005 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2006 .policy = vport_policy,
2007 .doit = ovs_vport_cmd_new
2008 },
2009 { .cmd = OVS_VPORT_CMD_DEL,
2010 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2011 .policy = vport_policy,
2012 .doit = ovs_vport_cmd_del
2013 },
2014 { .cmd = OVS_VPORT_CMD_GET,
2015 .flags = 0, /* OK for unprivileged users. */
2016 .policy = vport_policy,
2017 .doit = ovs_vport_cmd_get,
2018 .dumpit = ovs_vport_cmd_dump
2019 },
2020 { .cmd = OVS_VPORT_CMD_SET,
2021 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2022 .policy = vport_policy,
2023 .doit = ovs_vport_cmd_set,
2024 },
2025};
2026
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002027struct genl_family dp_vport_genl_family = {
2028 .id = GENL_ID_GENERATE,
2029 .hdrsize = sizeof(struct ovs_header),
2030 .name = OVS_VPORT_FAMILY,
2031 .version = OVS_VPORT_VERSION,
2032 .maxattr = OVS_VPORT_ATTR_MAX,
2033 .netnsok = true,
2034 .parallel_ops = true,
2035 .ops = dp_vport_genl_ops,
2036 .n_ops = ARRAY_SIZE(dp_vport_genl_ops),
2037 .mcgrps = &ovs_dp_vport_multicast_group,
2038 .n_mcgrps = 1,
Jesse Grossccb13522011-10-25 19:26:31 -07002039};
2040
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002041static struct genl_family * const dp_genl_families[] = {
2042 &dp_datapath_genl_family,
2043 &dp_vport_genl_family,
2044 &dp_flow_genl_family,
2045 &dp_packet_genl_family,
Jesse Grossccb13522011-10-25 19:26:31 -07002046};
2047
2048static void dp_unregister_genl(int n_families)
2049{
2050 int i;
2051
2052 for (i = 0; i < n_families; i++)
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002053 genl_unregister_family(dp_genl_families[i]);
Jesse Grossccb13522011-10-25 19:26:31 -07002054}
2055
2056static int dp_register_genl(void)
2057{
Jesse Grossccb13522011-10-25 19:26:31 -07002058 int err;
2059 int i;
2060
Jesse Grossccb13522011-10-25 19:26:31 -07002061 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
Jesse Grossccb13522011-10-25 19:26:31 -07002062
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002063 err = genl_register_family(dp_genl_families[i]);
Jesse Grossccb13522011-10-25 19:26:31 -07002064 if (err)
2065 goto error;
Jesse Grossccb13522011-10-25 19:26:31 -07002066 }
2067
2068 return 0;
2069
2070error:
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002071 dp_unregister_genl(i);
Jesse Grossccb13522011-10-25 19:26:31 -07002072 return err;
2073}
2074
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002075static int __net_init ovs_init_net(struct net *net)
2076{
2077 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2078
2079 INIT_LIST_HEAD(&ovs_net->dps);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002080 INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002081 return 0;
2082}
2083
2084static void __net_exit ovs_exit_net(struct net *net)
2085{
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002086 struct datapath *dp, *dp_next;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002087 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002088
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002089 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002090 list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
2091 __dp_destroy(dp);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002092 ovs_unlock();
2093
2094 cancel_work_sync(&ovs_net->dp_notify_work);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002095}
2096
2097static struct pernet_operations ovs_net_ops = {
2098 .init = ovs_init_net,
2099 .exit = ovs_exit_net,
2100 .id = &ovs_net_id,
2101 .size = sizeof(struct ovs_net),
2102};
2103
Jesse Grossccb13522011-10-25 19:26:31 -07002104static int __init dp_init(void)
2105{
Jesse Grossccb13522011-10-25 19:26:31 -07002106 int err;
2107
YOSHIFUJI Hideaki / 吉藤英明3523b292013-01-09 07:19:55 +00002108 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
Jesse Grossccb13522011-10-25 19:26:31 -07002109
2110 pr_info("Open vSwitch switching datapath\n");
2111
Andy Zhou971427f32014-09-15 19:37:25 -07002112 err = action_fifos_init();
Jesse Grossccb13522011-10-25 19:26:31 -07002113 if (err)
2114 goto error;
2115
Andy Zhou971427f32014-09-15 19:37:25 -07002116 err = ovs_internal_dev_rtnl_link_register();
2117 if (err)
2118 goto error_action_fifos_exit;
2119
Jiri Pirko5b9e7e12014-06-26 09:58:26 +02002120 err = ovs_flow_init();
2121 if (err)
2122 goto error_unreg_rtnl_link;
2123
Jesse Grossccb13522011-10-25 19:26:31 -07002124 err = ovs_vport_init();
2125 if (err)
2126 goto error_flow_exit;
2127
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002128 err = register_pernet_device(&ovs_net_ops);
Jesse Grossccb13522011-10-25 19:26:31 -07002129 if (err)
2130 goto error_vport_exit;
2131
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002132 err = register_netdevice_notifier(&ovs_dp_device_notifier);
2133 if (err)
2134 goto error_netns_exit;
2135
Thomas Graf62b9c8d2014-10-22 17:29:06 +02002136 err = ovs_netdev_init();
2137 if (err)
2138 goto error_unreg_notifier;
2139
Jesse Grossccb13522011-10-25 19:26:31 -07002140 err = dp_register_genl();
2141 if (err < 0)
Thomas Graf62b9c8d2014-10-22 17:29:06 +02002142 goto error_unreg_netdev;
Jesse Grossccb13522011-10-25 19:26:31 -07002143
Jesse Grossccb13522011-10-25 19:26:31 -07002144 return 0;
2145
Thomas Graf62b9c8d2014-10-22 17:29:06 +02002146error_unreg_netdev:
2147 ovs_netdev_exit();
Jesse Grossccb13522011-10-25 19:26:31 -07002148error_unreg_notifier:
2149 unregister_netdevice_notifier(&ovs_dp_device_notifier);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002150error_netns_exit:
2151 unregister_pernet_device(&ovs_net_ops);
Jesse Grossccb13522011-10-25 19:26:31 -07002152error_vport_exit:
2153 ovs_vport_exit();
2154error_flow_exit:
2155 ovs_flow_exit();
Jiri Pirko5b9e7e12014-06-26 09:58:26 +02002156error_unreg_rtnl_link:
2157 ovs_internal_dev_rtnl_link_unregister();
Andy Zhou971427f32014-09-15 19:37:25 -07002158error_action_fifos_exit:
2159 action_fifos_exit();
Jesse Grossccb13522011-10-25 19:26:31 -07002160error:
2161 return err;
2162}
2163
2164static void dp_cleanup(void)
2165{
Jesse Grossccb13522011-10-25 19:26:31 -07002166 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
Thomas Graf62b9c8d2014-10-22 17:29:06 +02002167 ovs_netdev_exit();
Jesse Grossccb13522011-10-25 19:26:31 -07002168 unregister_netdevice_notifier(&ovs_dp_device_notifier);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002169 unregister_pernet_device(&ovs_net_ops);
2170 rcu_barrier();
Jesse Grossccb13522011-10-25 19:26:31 -07002171 ovs_vport_exit();
2172 ovs_flow_exit();
Jiri Pirko5b9e7e12014-06-26 09:58:26 +02002173 ovs_internal_dev_rtnl_link_unregister();
Andy Zhou971427f32014-09-15 19:37:25 -07002174 action_fifos_exit();
Jesse Grossccb13522011-10-25 19:26:31 -07002175}
2176
2177module_init(dp_init);
2178module_exit(dp_cleanup);
2179
2180MODULE_DESCRIPTION("Open vSwitch switching datapath");
2181MODULE_LICENSE("GPL");