blob: 51017805b40b41b67733e369181f1b728d659448 [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;
Thomas Graf62b9c8d2014-10-22 17:29:06 +020062EXPORT_SYMBOL(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}
David S. Miller2c6c49d2014-10-28 17:27:23 -0400134EXPORT_SYMBOL(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 acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_PACKET_ATTR_ACTIONS]));
Jesse Grossccb13522011-10-25 19:26:31 -0700547 err = PTR_ERR(acts);
548 if (IS_ERR(acts))
549 goto err_flow_free;
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700550
Pravin B Shelare6445712013-10-03 18:16:47 -0700551 err = ovs_nla_copy_actions(a[OVS_PACKET_ATTR_ACTIONS],
Simon Horman25cd9ba2014-10-06 05:05:13 -0700552 &flow->key, &acts);
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700553 if (err)
554 goto err_flow_free;
Jesse Grossccb13522011-10-25 19:26:31 -0700555
Jesse Grossf5796682014-10-03 15:35:33 -0700556 rcu_assign_pointer(flow->sf_acts, acts);
557
558 OVS_CB(packet)->egress_tun_info = NULL;
Jesse Grossccb13522011-10-25 19:26:31 -0700559 packet->priority = flow->key.phy.priority;
Ansis Atteka39c7caeb2012-11-26 11:24:11 -0800560 packet->mark = flow->key.phy.skb_mark;
Jesse Grossccb13522011-10-25 19:26:31 -0700561
562 rcu_read_lock();
Andy Zhoucc3a5ae2014-09-08 13:14:22 -0700563 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -0700564 err = -ENODEV;
565 if (!dp)
566 goto err_unlock;
567
Pravin B Shelar83c8df22014-09-15 19:20:31 -0700568 input_vport = ovs_vport_rcu(dp, flow->key.phy.in_port);
569 if (!input_vport)
570 input_vport = ovs_vport_rcu(dp, OVSP_LOCAL);
571
572 if (!input_vport)
573 goto err_unlock;
574
575 OVS_CB(packet)->input_vport = input_vport;
Lorand Jakabd98612b2014-10-06 05:45:32 -0700576 sf_acts = rcu_dereference(flow->sf_acts);
Pravin B Shelar83c8df22014-09-15 19:20:31 -0700577
Jesse Grossccb13522011-10-25 19:26:31 -0700578 local_bh_disable();
Lorand Jakabd98612b2014-10-06 05:45:32 -0700579 err = ovs_execute_actions(dp, packet, sf_acts, &flow->key);
Jesse Grossccb13522011-10-25 19:26:31 -0700580 local_bh_enable();
581 rcu_read_unlock();
582
Andy Zhou03f0d912013-08-07 20:01:00 -0700583 ovs_flow_free(flow, false);
Jesse Grossccb13522011-10-25 19:26:31 -0700584 return err;
585
586err_unlock:
587 rcu_read_unlock();
588err_flow_free:
Andy Zhou03f0d912013-08-07 20:01:00 -0700589 ovs_flow_free(flow, false);
Jesse Grossccb13522011-10-25 19:26:31 -0700590err_kfree_skb:
591 kfree_skb(packet);
592err:
593 return err;
594}
595
596static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
Thomas Grafdded45fc2013-03-29 14:46:47 +0100597 [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
Jesse Grossccb13522011-10-25 19:26:31 -0700598 [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
599 [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
600};
601
Johannes Berg4534de82013-11-14 17:14:46 +0100602static const struct genl_ops dp_packet_genl_ops[] = {
Jesse Grossccb13522011-10-25 19:26:31 -0700603 { .cmd = OVS_PACKET_CMD_EXECUTE,
604 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
605 .policy = packet_policy,
606 .doit = ovs_packet_cmd_execute
607 }
608};
609
Pravin B Shelar0c200ef2014-05-06 16:44:50 -0700610static struct genl_family dp_packet_genl_family = {
611 .id = GENL_ID_GENERATE,
612 .hdrsize = sizeof(struct ovs_header),
613 .name = OVS_PACKET_FAMILY,
614 .version = OVS_PACKET_VERSION,
615 .maxattr = OVS_PACKET_ATTR_MAX,
616 .netnsok = true,
617 .parallel_ops = true,
618 .ops = dp_packet_genl_ops,
619 .n_ops = ARRAY_SIZE(dp_packet_genl_ops),
620};
621
Andy Zhou1bd71162013-10-22 10:42:46 -0700622static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats,
623 struct ovs_dp_megaflow_stats *mega_stats)
Jesse Grossccb13522011-10-25 19:26:31 -0700624{
625 int i;
Jesse Grossccb13522011-10-25 19:26:31 -0700626
Andy Zhou1bd71162013-10-22 10:42:46 -0700627 memset(mega_stats, 0, sizeof(*mega_stats));
628
Pravin B Shelarb637e492013-10-04 00:14:23 -0700629 stats->n_flows = ovs_flow_tbl_count(&dp->table);
Andy Zhou1bd71162013-10-22 10:42:46 -0700630 mega_stats->n_masks = ovs_flow_tbl_num_masks(&dp->table);
Jesse Grossccb13522011-10-25 19:26:31 -0700631
632 stats->n_hit = stats->n_missed = stats->n_lost = 0;
Andy Zhou1bd71162013-10-22 10:42:46 -0700633
Jesse Grossccb13522011-10-25 19:26:31 -0700634 for_each_possible_cpu(i) {
635 const struct dp_stats_percpu *percpu_stats;
636 struct dp_stats_percpu local_stats;
637 unsigned int start;
638
639 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
640
641 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700642 start = u64_stats_fetch_begin_irq(&percpu_stats->syncp);
Jesse Grossccb13522011-10-25 19:26:31 -0700643 local_stats = *percpu_stats;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700644 } while (u64_stats_fetch_retry_irq(&percpu_stats->syncp, start));
Jesse Grossccb13522011-10-25 19:26:31 -0700645
646 stats->n_hit += local_stats.n_hit;
647 stats->n_missed += local_stats.n_missed;
648 stats->n_lost += local_stats.n_lost;
Andy Zhou1bd71162013-10-22 10:42:46 -0700649 mega_stats->n_mask_hit += local_stats.n_mask_hit;
Jesse Grossccb13522011-10-25 19:26:31 -0700650 }
651}
652
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100653static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts)
654{
655 return NLMSG_ALIGN(sizeof(struct ovs_header))
Joe Stringer41af73e2014-10-18 16:14:14 -0700656 + nla_total_size(ovs_key_attr_size()) /* OVS_FLOW_ATTR_KEY */
657 + nla_total_size(ovs_key_attr_size()) /* OVS_FLOW_ATTR_MASK */
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100658 + nla_total_size(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
659 + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
660 + nla_total_size(8) /* OVS_FLOW_ATTR_USED */
661 + nla_total_size(acts->actions_len); /* OVS_FLOW_ATTR_ACTIONS */
662}
663
Jarno Rajahalmebb6f9a72014-05-05 11:32:17 -0700664/* Called with ovs_mutex or RCU read lock. */
Joe Stringerca7105f2014-09-08 13:09:37 -0700665static int ovs_flow_cmd_fill_match(const struct sw_flow *flow,
666 struct sk_buff *skb)
Jesse Grossccb13522011-10-25 19:26:31 -0700667{
Jesse Grossccb13522011-10-25 19:26:31 -0700668 struct nlattr *nla;
Jesse Grossccb13522011-10-25 19:26:31 -0700669 int err;
670
Andy Zhou03f0d912013-08-07 20:01:00 -0700671 /* Fill flow key. */
Jesse Grossccb13522011-10-25 19:26:31 -0700672 nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
673 if (!nla)
Joe Stringerca7105f2014-09-08 13:09:37 -0700674 return -EMSGSIZE;
Andy Zhou03f0d912013-08-07 20:01:00 -0700675
Pravin B Shelare6445712013-10-03 18:16:47 -0700676 err = ovs_nla_put_flow(&flow->unmasked_key, &flow->unmasked_key, skb);
Jesse Grossccb13522011-10-25 19:26:31 -0700677 if (err)
Joe Stringerca7105f2014-09-08 13:09:37 -0700678 return err;
679
Jesse Grossccb13522011-10-25 19:26:31 -0700680 nla_nest_end(skb, nla);
681
Joe Stringerca7105f2014-09-08 13:09:37 -0700682 /* Fill flow mask. */
Andy Zhou03f0d912013-08-07 20:01:00 -0700683 nla = nla_nest_start(skb, OVS_FLOW_ATTR_MASK);
684 if (!nla)
Joe Stringerca7105f2014-09-08 13:09:37 -0700685 return -EMSGSIZE;
Andy Zhou03f0d912013-08-07 20:01:00 -0700686
Pravin B Shelare6445712013-10-03 18:16:47 -0700687 err = ovs_nla_put_flow(&flow->key, &flow->mask->key, skb);
Andy Zhou03f0d912013-08-07 20:01:00 -0700688 if (err)
Joe Stringerca7105f2014-09-08 13:09:37 -0700689 return err;
Andy Zhou03f0d912013-08-07 20:01:00 -0700690
691 nla_nest_end(skb, nla);
Joe Stringerca7105f2014-09-08 13:09:37 -0700692 return 0;
693}
694
695/* Called with ovs_mutex or RCU read lock. */
696static int ovs_flow_cmd_fill_stats(const struct sw_flow *flow,
697 struct sk_buff *skb)
698{
699 struct ovs_flow_stats stats;
700 __be16 tcp_flags;
701 unsigned long used;
Andy Zhou03f0d912013-08-07 20:01:00 -0700702
Pravin B Shelare298e502013-10-29 17:22:21 -0700703 ovs_flow_stats_get(flow, &stats, &used, &tcp_flags);
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700704
David S. Miller028d6a62012-03-29 23:20:48 -0400705 if (used &&
706 nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
Joe Stringerca7105f2014-09-08 13:09:37 -0700707 return -EMSGSIZE;
Jesse Grossccb13522011-10-25 19:26:31 -0700708
David S. Miller028d6a62012-03-29 23:20:48 -0400709 if (stats.n_packets &&
Pravin B Shelare298e502013-10-29 17:22:21 -0700710 nla_put(skb, OVS_FLOW_ATTR_STATS, sizeof(struct ovs_flow_stats), &stats))
Joe Stringerca7105f2014-09-08 13:09:37 -0700711 return -EMSGSIZE;
Jesse Grossccb13522011-10-25 19:26:31 -0700712
Pravin B Shelare298e502013-10-29 17:22:21 -0700713 if ((u8)ntohs(tcp_flags) &&
714 nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, (u8)ntohs(tcp_flags)))
Joe Stringerca7105f2014-09-08 13:09:37 -0700715 return -EMSGSIZE;
716
717 return 0;
718}
719
720/* Called with ovs_mutex or RCU read lock. */
721static int ovs_flow_cmd_fill_actions(const struct sw_flow *flow,
722 struct sk_buff *skb, int skb_orig_len)
723{
724 struct nlattr *start;
725 int err;
Jesse Grossccb13522011-10-25 19:26:31 -0700726
727 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
728 * this is the first flow to be dumped into 'skb'. This is unusual for
729 * Netlink but individual action lists can be longer than
730 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
731 * The userspace caller can always fetch the actions separately if it
732 * really wants them. (Most userspace callers in fact don't care.)
733 *
734 * This can only fail for dump operations because the skb is always
735 * properly sized for single flows.
736 */
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700737 start = nla_nest_start(skb, OVS_FLOW_ATTR_ACTIONS);
738 if (start) {
Pravin B Shelard57170b2013-07-30 15:39:39 -0700739 const struct sw_flow_actions *sf_acts;
740
Jesse Gross663efa32013-12-03 10:58:53 -0800741 sf_acts = rcu_dereference_ovsl(flow->sf_acts);
Pravin B Shelare6445712013-10-03 18:16:47 -0700742 err = ovs_nla_put_actions(sf_acts->actions,
743 sf_acts->actions_len, skb);
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700744
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700745 if (!err)
746 nla_nest_end(skb, start);
747 else {
748 if (skb_orig_len)
Joe Stringerca7105f2014-09-08 13:09:37 -0700749 return err;
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700750
751 nla_nest_cancel(skb, start);
752 }
Joe Stringerca7105f2014-09-08 13:09:37 -0700753 } else if (skb_orig_len) {
754 return -EMSGSIZE;
755 }
756
757 return 0;
758}
759
760/* Called with ovs_mutex or RCU read lock. */
761static int ovs_flow_cmd_fill_info(const struct sw_flow *flow, int dp_ifindex,
762 struct sk_buff *skb, u32 portid,
763 u32 seq, u32 flags, u8 cmd)
764{
765 const int skb_orig_len = skb->len;
766 struct ovs_header *ovs_header;
767 int err;
768
769 ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family,
770 flags, cmd);
771 if (!ovs_header)
772 return -EMSGSIZE;
773
774 ovs_header->dp_ifindex = dp_ifindex;
775
776 err = ovs_flow_cmd_fill_match(flow, skb);
777 if (err)
778 goto error;
779
780 err = ovs_flow_cmd_fill_stats(flow, skb);
781 if (err)
782 goto error;
783
784 err = ovs_flow_cmd_fill_actions(flow, skb, skb_orig_len);
785 if (err)
786 goto error;
Jesse Grossccb13522011-10-25 19:26:31 -0700787
788 return genlmsg_end(skb, ovs_header);
789
Jesse Grossccb13522011-10-25 19:26:31 -0700790error:
791 genlmsg_cancel(skb, ovs_header);
792 return err;
793}
794
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700795/* May not be called with RCU read lock. */
796static struct sk_buff *ovs_flow_cmd_alloc_info(const struct sw_flow_actions *acts,
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700797 struct genl_info *info,
798 bool always)
Jesse Grossccb13522011-10-25 19:26:31 -0700799{
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700800 struct sk_buff *skb;
Jesse Grossccb13522011-10-25 19:26:31 -0700801
Samuel Gauthier9b67aa42014-09-18 10:31:04 +0200802 if (!always && !ovs_must_notify(&dp_flow_genl_family, info, 0))
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700803 return NULL;
804
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700805 skb = genlmsg_new_unicast(ovs_flow_cmd_msg_size(acts), info, GFP_KERNEL);
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700806 if (!skb)
807 return ERR_PTR(-ENOMEM);
808
809 return skb;
Jesse Grossccb13522011-10-25 19:26:31 -0700810}
811
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700812/* Called with ovs_mutex. */
813static struct sk_buff *ovs_flow_cmd_build_info(const struct sw_flow *flow,
814 int dp_ifindex,
815 struct genl_info *info, u8 cmd,
816 bool always)
Jesse Grossccb13522011-10-25 19:26:31 -0700817{
818 struct sk_buff *skb;
819 int retval;
820
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700821 skb = ovs_flow_cmd_alloc_info(ovsl_dereference(flow->sf_acts), info,
822 always);
Himangi Saraogid0e992a2014-07-27 12:37:46 +0530823 if (IS_ERR_OR_NULL(skb))
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700824 return skb;
Jesse Grossccb13522011-10-25 19:26:31 -0700825
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700826 retval = ovs_flow_cmd_fill_info(flow, dp_ifindex, skb,
827 info->snd_portid, info->snd_seq, 0,
828 cmd);
Jesse Grossccb13522011-10-25 19:26:31 -0700829 BUG_ON(retval < 0);
830 return skb;
831}
832
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700833static int ovs_flow_cmd_new(struct sk_buff *skb, struct genl_info *info)
Jesse Grossccb13522011-10-25 19:26:31 -0700834{
835 struct nlattr **a = info->attrs;
836 struct ovs_header *ovs_header = info->userhdr;
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700837 struct sw_flow *flow, *new_flow;
Andy Zhou03f0d912013-08-07 20:01:00 -0700838 struct sw_flow_mask mask;
Jesse Grossccb13522011-10-25 19:26:31 -0700839 struct sk_buff *reply;
840 struct datapath *dp;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700841 struct sw_flow_actions *acts;
842 struct sw_flow_match match;
843 int error;
844
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700845 /* Must have key and actions. */
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700846 error = -EINVAL;
Jesse Gross426cda52014-10-06 05:08:38 -0700847 if (!a[OVS_FLOW_ATTR_KEY]) {
848 OVS_NLERR("Flow key attribute not present in new flow.\n");
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700849 goto error;
Jesse Gross426cda52014-10-06 05:08:38 -0700850 }
851 if (!a[OVS_FLOW_ATTR_ACTIONS]) {
852 OVS_NLERR("Flow actions attribute not present in new flow.\n");
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700853 goto error;
Jesse Gross426cda52014-10-06 05:08:38 -0700854 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700855
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700856 /* Most of the time we need to allocate a new flow, do it before
857 * locking.
858 */
859 new_flow = ovs_flow_alloc();
860 if (IS_ERR(new_flow)) {
861 error = PTR_ERR(new_flow);
862 goto error;
863 }
864
865 /* Extract key. */
866 ovs_match_init(&match, &new_flow->unmasked_key, &mask);
867 error = ovs_nla_get_match(&match,
868 a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
869 if (error)
870 goto err_kfree_flow;
871
872 ovs_flow_mask_key(&new_flow->key, &new_flow->unmasked_key, &mask);
873
874 /* Validate actions. */
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700875 acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_FLOW_ATTR_ACTIONS]));
876 error = PTR_ERR(acts);
877 if (IS_ERR(acts))
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700878 goto err_kfree_flow;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700879
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700880 error = ovs_nla_copy_actions(a[OVS_FLOW_ATTR_ACTIONS], &new_flow->key,
Simon Horman25cd9ba2014-10-06 05:05:13 -0700881 &acts);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700882 if (error) {
883 OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700884 goto err_kfree_acts;
885 }
886
887 reply = ovs_flow_cmd_alloc_info(acts, info, false);
888 if (IS_ERR(reply)) {
889 error = PTR_ERR(reply);
890 goto err_kfree_acts;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700891 }
892
893 ovs_lock();
894 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700895 if (unlikely(!dp)) {
896 error = -ENODEV;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700897 goto err_unlock_ovs;
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700898 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700899 /* Check if this is a duplicate flow */
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700900 flow = ovs_flow_tbl_lookup(&dp->table, &new_flow->unmasked_key);
901 if (likely(!flow)) {
902 rcu_assign_pointer(new_flow->sf_acts, acts);
903
904 /* Put flow in bucket. */
905 error = ovs_flow_tbl_insert(&dp->table, new_flow, &mask);
906 if (unlikely(error)) {
907 acts = NULL;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700908 goto err_unlock_ovs;
909 }
910
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700911 if (unlikely(reply)) {
912 error = ovs_flow_cmd_fill_info(new_flow,
913 ovs_header->dp_ifindex,
914 reply, info->snd_portid,
915 info->snd_seq, 0,
916 OVS_FLOW_CMD_NEW);
917 BUG_ON(error < 0);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700918 }
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700919 ovs_unlock();
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700920 } else {
921 struct sw_flow_actions *old_acts;
922
923 /* Bail out if we're not allowed to modify an existing flow.
924 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
925 * because Generic Netlink treats the latter as a dump
926 * request. We also accept NLM_F_EXCL in case that bug ever
927 * gets fixed.
928 */
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700929 if (unlikely(info->nlhdr->nlmsg_flags & (NLM_F_CREATE
930 | NLM_F_EXCL))) {
931 error = -EEXIST;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700932 goto err_unlock_ovs;
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700933 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700934 /* The unmasked key has to be the same for flow updates. */
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700935 if (unlikely(!ovs_flow_cmp_unmasked_key(flow, &match))) {
Alex Wang4a46b242014-06-30 20:30:29 -0700936 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
937 if (!flow) {
938 error = -ENOENT;
939 goto err_unlock_ovs;
940 }
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700941 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700942 /* Update actions. */
943 old_acts = ovsl_dereference(flow->sf_acts);
944 rcu_assign_pointer(flow->sf_acts, acts);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700945
946 if (unlikely(reply)) {
947 error = ovs_flow_cmd_fill_info(flow,
948 ovs_header->dp_ifindex,
949 reply, info->snd_portid,
950 info->snd_seq, 0,
951 OVS_FLOW_CMD_NEW);
952 BUG_ON(error < 0);
953 }
954 ovs_unlock();
955
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700956 ovs_nla_free_flow_actions(old_acts);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700957 ovs_flow_free(new_flow, false);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700958 }
959
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700960 if (reply)
961 ovs_notify(&dp_flow_genl_family, reply, info);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700962 return 0;
963
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700964err_unlock_ovs:
965 ovs_unlock();
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700966 kfree_skb(reply);
967err_kfree_acts:
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700968 kfree(acts);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700969err_kfree_flow:
970 ovs_flow_free(new_flow, false);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700971error:
972 return error;
973}
974
Jesse Gross6b205b22014-10-03 15:35:32 -0700975static struct sw_flow_actions *get_flow_actions(const struct nlattr *a,
976 const struct sw_flow_key *key,
977 const struct sw_flow_mask *mask)
978{
979 struct sw_flow_actions *acts;
980 struct sw_flow_key masked_key;
981 int error;
982
983 acts = ovs_nla_alloc_flow_actions(nla_len(a));
984 if (IS_ERR(acts))
985 return acts;
986
987 ovs_flow_mask_key(&masked_key, key, mask);
Simon Horman25cd9ba2014-10-06 05:05:13 -0700988 error = ovs_nla_copy_actions(a, &masked_key, &acts);
Jesse Gross6b205b22014-10-03 15:35:32 -0700989 if (error) {
990 OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
991 kfree(acts);
992 return ERR_PTR(error);
993 }
994
995 return acts;
996}
997
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700998static int ovs_flow_cmd_set(struct sk_buff *skb, struct genl_info *info)
999{
1000 struct nlattr **a = info->attrs;
1001 struct ovs_header *ovs_header = info->userhdr;
Jesse Gross6b205b22014-10-03 15:35:32 -07001002 struct sw_flow_key key;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001003 struct sw_flow *flow;
1004 struct sw_flow_mask mask;
1005 struct sk_buff *reply = NULL;
1006 struct datapath *dp;
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001007 struct sw_flow_actions *old_acts = NULL, *acts = NULL;
Andy Zhou03f0d912013-08-07 20:01:00 -07001008 struct sw_flow_match match;
Jesse Grossccb13522011-10-25 19:26:31 -07001009 int error;
Jesse Grossccb13522011-10-25 19:26:31 -07001010
1011 /* Extract key. */
1012 error = -EINVAL;
Jesse Gross426cda52014-10-06 05:08:38 -07001013 if (!a[OVS_FLOW_ATTR_KEY]) {
1014 OVS_NLERR("Flow key attribute not present in set flow.\n");
Jesse Grossccb13522011-10-25 19:26:31 -07001015 goto error;
Jesse Gross426cda52014-10-06 05:08:38 -07001016 }
Andy Zhou03f0d912013-08-07 20:01:00 -07001017
1018 ovs_match_init(&match, &key, &mask);
Jarno Rajahalme23dabf82014-03-27 12:35:23 -07001019 error = ovs_nla_get_match(&match,
Pravin B Shelare6445712013-10-03 18:16:47 -07001020 a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
Jesse Grossccb13522011-10-25 19:26:31 -07001021 if (error)
1022 goto error;
1023
1024 /* Validate actions. */
1025 if (a[OVS_FLOW_ATTR_ACTIONS]) {
Jesse Gross6b205b22014-10-03 15:35:32 -07001026 acts = get_flow_actions(a[OVS_FLOW_ATTR_ACTIONS], &key, &mask);
1027 if (IS_ERR(acts)) {
1028 error = PTR_ERR(acts);
Jesse Grossccb13522011-10-25 19:26:31 -07001029 goto error;
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001030 }
1031 }
1032
1033 /* Can allocate before locking if have acts. */
1034 if (acts) {
1035 reply = ovs_flow_cmd_alloc_info(acts, info, false);
1036 if (IS_ERR(reply)) {
1037 error = PTR_ERR(reply);
1038 goto err_kfree_acts;
Andy Zhou03f0d912013-08-07 20:01:00 -07001039 }
Jesse Grossccb13522011-10-25 19:26:31 -07001040 }
1041
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001042 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001043 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001044 if (unlikely(!dp)) {
1045 error = -ENODEV;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001046 goto err_unlock_ovs;
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001047 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001048 /* Check that the flow exists. */
Alex Wang4a46b242014-06-30 20:30:29 -07001049 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001050 if (unlikely(!flow)) {
1051 error = -ENOENT;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001052 goto err_unlock_ovs;
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001053 }
Alex Wang4a46b242014-06-30 20:30:29 -07001054
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001055 /* Update actions, if present. */
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001056 if (likely(acts)) {
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001057 old_acts = ovsl_dereference(flow->sf_acts);
Jesse Grossccb13522011-10-25 19:26:31 -07001058 rcu_assign_pointer(flow->sf_acts, acts);
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001059
1060 if (unlikely(reply)) {
1061 error = ovs_flow_cmd_fill_info(flow,
1062 ovs_header->dp_ifindex,
1063 reply, info->snd_portid,
1064 info->snd_seq, 0,
1065 OVS_FLOW_CMD_NEW);
1066 BUG_ON(error < 0);
1067 }
1068 } else {
1069 /* Could not alloc without acts before locking. */
1070 reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex,
1071 info, OVS_FLOW_CMD_NEW, false);
1072 if (unlikely(IS_ERR(reply))) {
1073 error = PTR_ERR(reply);
1074 goto err_unlock_ovs;
1075 }
Jesse Grossccb13522011-10-25 19:26:31 -07001076 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001077
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001078 /* Clear stats. */
1079 if (a[OVS_FLOW_ATTR_CLEAR])
1080 ovs_flow_stats_clear(flow);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001081 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001082
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001083 if (reply)
1084 ovs_notify(&dp_flow_genl_family, reply, info);
1085 if (old_acts)
1086 ovs_nla_free_flow_actions(old_acts);
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -07001087
Jesse Grossccb13522011-10-25 19:26:31 -07001088 return 0;
1089
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001090err_unlock_ovs:
1091 ovs_unlock();
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001092 kfree_skb(reply);
1093err_kfree_acts:
Pravin B Shelar74f84a52013-06-17 17:50:12 -07001094 kfree(acts);
Jesse Grossccb13522011-10-25 19:26:31 -07001095error:
1096 return error;
1097}
1098
1099static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1100{
1101 struct nlattr **a = info->attrs;
1102 struct ovs_header *ovs_header = info->userhdr;
1103 struct sw_flow_key key;
1104 struct sk_buff *reply;
1105 struct sw_flow *flow;
1106 struct datapath *dp;
Andy Zhou03f0d912013-08-07 20:01:00 -07001107 struct sw_flow_match match;
Jesse Grossccb13522011-10-25 19:26:31 -07001108 int err;
Jesse Grossccb13522011-10-25 19:26:31 -07001109
Andy Zhou03f0d912013-08-07 20:01:00 -07001110 if (!a[OVS_FLOW_ATTR_KEY]) {
1111 OVS_NLERR("Flow get message rejected, Key attribute missing.\n");
Jesse Grossccb13522011-10-25 19:26:31 -07001112 return -EINVAL;
Andy Zhou03f0d912013-08-07 20:01:00 -07001113 }
1114
1115 ovs_match_init(&match, &key, NULL);
Jarno Rajahalme23dabf82014-03-27 12:35:23 -07001116 err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL);
Jesse Grossccb13522011-10-25 19:26:31 -07001117 if (err)
1118 return err;
1119
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001120 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001121 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001122 if (!dp) {
1123 err = -ENODEV;
1124 goto unlock;
1125 }
Jesse Grossccb13522011-10-25 19:26:31 -07001126
Alex Wang4a46b242014-06-30 20:30:29 -07001127 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1128 if (!flow) {
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001129 err = -ENOENT;
1130 goto unlock;
1131 }
Jesse Grossccb13522011-10-25 19:26:31 -07001132
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -07001133 reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex, info,
1134 OVS_FLOW_CMD_NEW, true);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001135 if (IS_ERR(reply)) {
1136 err = PTR_ERR(reply);
1137 goto unlock;
1138 }
Jesse Grossccb13522011-10-25 19:26:31 -07001139
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001140 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001141 return genlmsg_reply(reply, info);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001142unlock:
1143 ovs_unlock();
1144 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001145}
1146
1147static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1148{
1149 struct nlattr **a = info->attrs;
1150 struct ovs_header *ovs_header = info->userhdr;
1151 struct sw_flow_key key;
1152 struct sk_buff *reply;
1153 struct sw_flow *flow;
1154 struct datapath *dp;
Andy Zhou03f0d912013-08-07 20:01:00 -07001155 struct sw_flow_match match;
Jesse Grossccb13522011-10-25 19:26:31 -07001156 int err;
Jesse Grossccb13522011-10-25 19:26:31 -07001157
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001158 if (likely(a[OVS_FLOW_ATTR_KEY])) {
1159 ovs_match_init(&match, &key, NULL);
1160 err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL);
1161 if (unlikely(err))
1162 return err;
1163 }
1164
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001165 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001166 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001167 if (unlikely(!dp)) {
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001168 err = -ENODEV;
1169 goto unlock;
1170 }
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001171
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001172 if (unlikely(!a[OVS_FLOW_ATTR_KEY])) {
Pravin B Shelarb637e492013-10-04 00:14:23 -07001173 err = ovs_flow_tbl_flush(&dp->table);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001174 goto unlock;
1175 }
Andy Zhou03f0d912013-08-07 20:01:00 -07001176
Alex Wang4a46b242014-06-30 20:30:29 -07001177 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1178 if (unlikely(!flow)) {
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001179 err = -ENOENT;
1180 goto unlock;
1181 }
Jesse Grossccb13522011-10-25 19:26:31 -07001182
Pravin B Shelarb637e492013-10-04 00:14:23 -07001183 ovs_flow_tbl_remove(&dp->table, flow);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001184 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001185
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001186 reply = ovs_flow_cmd_alloc_info((const struct sw_flow_actions __force *) flow->sf_acts,
1187 info, false);
1188 if (likely(reply)) {
1189 if (likely(!IS_ERR(reply))) {
1190 rcu_read_lock(); /*To keep RCU checker happy. */
1191 err = ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex,
1192 reply, info->snd_portid,
1193 info->snd_seq, 0,
1194 OVS_FLOW_CMD_DEL);
1195 rcu_read_unlock();
1196 BUG_ON(err < 0);
1197
1198 ovs_notify(&dp_flow_genl_family, reply, info);
1199 } else {
1200 netlink_set_err(sock_net(skb->sk)->genl_sock, 0, 0, PTR_ERR(reply));
1201 }
1202 }
1203
1204 ovs_flow_free(flow, true);
Jesse Grossccb13522011-10-25 19:26:31 -07001205 return 0;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001206unlock:
1207 ovs_unlock();
1208 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001209}
1210
1211static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1212{
1213 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
Pravin B Shelarb637e492013-10-04 00:14:23 -07001214 struct table_instance *ti;
Jesse Grossccb13522011-10-25 19:26:31 -07001215 struct datapath *dp;
Jesse Grossccb13522011-10-25 19:26:31 -07001216
Pravin B Shelard57170b2013-07-30 15:39:39 -07001217 rcu_read_lock();
Andy Zhoucc3a5ae2014-09-08 13:14:22 -07001218 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001219 if (!dp) {
Pravin B Shelard57170b2013-07-30 15:39:39 -07001220 rcu_read_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001221 return -ENODEV;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001222 }
Jesse Grossccb13522011-10-25 19:26:31 -07001223
Pravin B Shelarb637e492013-10-04 00:14:23 -07001224 ti = rcu_dereference(dp->table.ti);
Jesse Grossccb13522011-10-25 19:26:31 -07001225 for (;;) {
1226 struct sw_flow *flow;
1227 u32 bucket, obj;
1228
1229 bucket = cb->args[0];
1230 obj = cb->args[1];
Pravin B Shelarb637e492013-10-04 00:14:23 -07001231 flow = ovs_flow_tbl_dump_next(ti, &bucket, &obj);
Jesse Grossccb13522011-10-25 19:26:31 -07001232 if (!flow)
1233 break;
1234
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -07001235 if (ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex, skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001236 NETLINK_CB(cb->skb).portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001237 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1238 OVS_FLOW_CMD_NEW) < 0)
1239 break;
1240
1241 cb->args[0] = bucket;
1242 cb->args[1] = obj;
1243 }
Pravin B Shelard57170b2013-07-30 15:39:39 -07001244 rcu_read_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001245 return skb->len;
1246}
1247
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001248static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
1249 [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
1250 [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
1251 [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
1252};
1253
stephen hemminger48e48a72014-07-16 11:25:52 -07001254static const struct genl_ops dp_flow_genl_ops[] = {
Jesse Grossccb13522011-10-25 19:26:31 -07001255 { .cmd = OVS_FLOW_CMD_NEW,
1256 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1257 .policy = flow_policy,
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001258 .doit = ovs_flow_cmd_new
Jesse Grossccb13522011-10-25 19:26:31 -07001259 },
1260 { .cmd = OVS_FLOW_CMD_DEL,
1261 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1262 .policy = flow_policy,
1263 .doit = ovs_flow_cmd_del
1264 },
1265 { .cmd = OVS_FLOW_CMD_GET,
1266 .flags = 0, /* OK for unprivileged users. */
1267 .policy = flow_policy,
1268 .doit = ovs_flow_cmd_get,
1269 .dumpit = ovs_flow_cmd_dump
1270 },
1271 { .cmd = OVS_FLOW_CMD_SET,
1272 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1273 .policy = flow_policy,
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001274 .doit = ovs_flow_cmd_set,
Jesse Grossccb13522011-10-25 19:26:31 -07001275 },
1276};
1277
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001278static struct genl_family dp_flow_genl_family = {
Jesse Grossccb13522011-10-25 19:26:31 -07001279 .id = GENL_ID_GENERATE,
1280 .hdrsize = sizeof(struct ovs_header),
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001281 .name = OVS_FLOW_FAMILY,
1282 .version = OVS_FLOW_VERSION,
1283 .maxattr = OVS_FLOW_ATTR_MAX,
Pravin B Shelar3a4e0d62013-04-23 07:48:48 +00001284 .netnsok = true,
1285 .parallel_ops = true,
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001286 .ops = dp_flow_genl_ops,
1287 .n_ops = ARRAY_SIZE(dp_flow_genl_ops),
1288 .mcgrps = &ovs_dp_flow_multicast_group,
1289 .n_mcgrps = 1,
Jesse Grossccb13522011-10-25 19:26:31 -07001290};
1291
Thomas Grafc3ff8cf2013-03-29 14:46:49 +01001292static size_t ovs_dp_cmd_msg_size(void)
1293{
1294 size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
1295
1296 msgsize += nla_total_size(IFNAMSIZ);
1297 msgsize += nla_total_size(sizeof(struct ovs_dp_stats));
Andy Zhou1bd71162013-10-22 10:42:46 -07001298 msgsize += nla_total_size(sizeof(struct ovs_dp_megaflow_stats));
Daniele Di Proietto45fb9c32014-01-23 10:47:35 -08001299 msgsize += nla_total_size(sizeof(u32)); /* OVS_DP_ATTR_USER_FEATURES */
Thomas Grafc3ff8cf2013-03-29 14:46:49 +01001300
1301 return msgsize;
1302}
1303
Jarno Rajahalmebb6f9a72014-05-05 11:32:17 -07001304/* Called with ovs_mutex or RCU read lock. */
Jesse Grossccb13522011-10-25 19:26:31 -07001305static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001306 u32 portid, u32 seq, u32 flags, u8 cmd)
Jesse Grossccb13522011-10-25 19:26:31 -07001307{
1308 struct ovs_header *ovs_header;
1309 struct ovs_dp_stats dp_stats;
Andy Zhou1bd71162013-10-22 10:42:46 -07001310 struct ovs_dp_megaflow_stats dp_megaflow_stats;
Jesse Grossccb13522011-10-25 19:26:31 -07001311 int err;
1312
Eric W. Biederman15e47302012-09-07 20:12:54 +00001313 ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
Jesse Grossccb13522011-10-25 19:26:31 -07001314 flags, cmd);
1315 if (!ovs_header)
1316 goto error;
1317
1318 ovs_header->dp_ifindex = get_dpifindex(dp);
1319
Jesse Grossccb13522011-10-25 19:26:31 -07001320 err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
Jesse Grossccb13522011-10-25 19:26:31 -07001321 if (err)
1322 goto nla_put_failure;
1323
Andy Zhou1bd71162013-10-22 10:42:46 -07001324 get_dp_stats(dp, &dp_stats, &dp_megaflow_stats);
1325 if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats),
1326 &dp_stats))
1327 goto nla_put_failure;
1328
1329 if (nla_put(skb, OVS_DP_ATTR_MEGAFLOW_STATS,
1330 sizeof(struct ovs_dp_megaflow_stats),
1331 &dp_megaflow_stats))
David S. Miller028d6a62012-03-29 23:20:48 -04001332 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001333
Thomas Graf43d4be92013-12-13 15:22:18 +01001334 if (nla_put_u32(skb, OVS_DP_ATTR_USER_FEATURES, dp->user_features))
1335 goto nla_put_failure;
1336
Jesse Grossccb13522011-10-25 19:26:31 -07001337 return genlmsg_end(skb, ovs_header);
1338
1339nla_put_failure:
1340 genlmsg_cancel(skb, ovs_header);
1341error:
1342 return -EMSGSIZE;
1343}
1344
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001345static struct sk_buff *ovs_dp_cmd_alloc_info(struct genl_info *info)
Jesse Grossccb13522011-10-25 19:26:31 -07001346{
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001347 return genlmsg_new_unicast(ovs_dp_cmd_msg_size(), info, GFP_KERNEL);
Jesse Grossccb13522011-10-25 19:26:31 -07001348}
1349
Jarno Rajahalmebb6f9a72014-05-05 11:32:17 -07001350/* Called with rcu_read_lock or ovs_mutex. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001351static struct datapath *lookup_datapath(struct net *net,
1352 struct ovs_header *ovs_header,
Jesse Grossccb13522011-10-25 19:26:31 -07001353 struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1354{
1355 struct datapath *dp;
1356
1357 if (!a[OVS_DP_ATTR_NAME])
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001358 dp = get_dp(net, ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001359 else {
1360 struct vport *vport;
1361
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001362 vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
Jesse Grossccb13522011-10-25 19:26:31 -07001363 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
Jesse Grossccb13522011-10-25 19:26:31 -07001364 }
1365 return dp ? dp : ERR_PTR(-ENODEV);
1366}
1367
Thomas Graf44da5ae2013-12-13 15:22:19 +01001368static void ovs_dp_reset_user_features(struct sk_buff *skb, struct genl_info *info)
1369{
1370 struct datapath *dp;
1371
1372 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
Jiri Pirko3c7eacf2014-02-14 11:42:36 +01001373 if (IS_ERR(dp))
Thomas Graf44da5ae2013-12-13 15:22:19 +01001374 return;
1375
1376 WARN(dp->user_features, "Dropping previously announced user features\n");
1377 dp->user_features = 0;
1378}
1379
Thomas Graf43d4be92013-12-13 15:22:18 +01001380static void ovs_dp_change(struct datapath *dp, struct nlattr **a)
1381{
1382 if (a[OVS_DP_ATTR_USER_FEATURES])
1383 dp->user_features = nla_get_u32(a[OVS_DP_ATTR_USER_FEATURES]);
1384}
1385
Jesse Grossccb13522011-10-25 19:26:31 -07001386static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1387{
1388 struct nlattr **a = info->attrs;
1389 struct vport_parms parms;
1390 struct sk_buff *reply;
1391 struct datapath *dp;
1392 struct vport *vport;
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001393 struct ovs_net *ovs_net;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001394 int err, i;
Jesse Grossccb13522011-10-25 19:26:31 -07001395
1396 err = -EINVAL;
1397 if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1398 goto err;
1399
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001400 reply = ovs_dp_cmd_alloc_info(info);
1401 if (!reply)
1402 return -ENOMEM;
Jesse Grossccb13522011-10-25 19:26:31 -07001403
1404 err = -ENOMEM;
1405 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1406 if (dp == NULL)
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001407 goto err_free_reply;
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001408
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001409 ovs_dp_set_net(dp, hold_net(sock_net(skb->sk)));
Jesse Grossccb13522011-10-25 19:26:31 -07001410
1411 /* Allocate table. */
Pravin B Shelarb637e492013-10-04 00:14:23 -07001412 err = ovs_flow_tbl_init(&dp->table);
1413 if (err)
Jesse Grossccb13522011-10-25 19:26:31 -07001414 goto err_free_dp;
1415
WANG Cong1c213bd2014-02-13 11:46:28 -08001416 dp->stats_percpu = netdev_alloc_pcpu_stats(struct dp_stats_percpu);
Jesse Grossccb13522011-10-25 19:26:31 -07001417 if (!dp->stats_percpu) {
1418 err = -ENOMEM;
1419 goto err_destroy_table;
1420 }
1421
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001422 dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
Pravin B Shelare6445712013-10-03 18:16:47 -07001423 GFP_KERNEL);
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001424 if (!dp->ports) {
1425 err = -ENOMEM;
1426 goto err_destroy_percpu;
1427 }
1428
1429 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1430 INIT_HLIST_HEAD(&dp->ports[i]);
1431
Jesse Grossccb13522011-10-25 19:26:31 -07001432 /* Set up our datapath device. */
1433 parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1434 parms.type = OVS_VPORT_TYPE_INTERNAL;
1435 parms.options = NULL;
1436 parms.dp = dp;
1437 parms.port_no = OVSP_LOCAL;
Alex Wang5cd667b2014-07-17 15:14:13 -07001438 parms.upcall_portids = a[OVS_DP_ATTR_UPCALL_PID];
Jesse Grossccb13522011-10-25 19:26:31 -07001439
Thomas Graf43d4be92013-12-13 15:22:18 +01001440 ovs_dp_change(dp, a);
1441
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001442 /* So far only local changes have been made, now need the lock. */
1443 ovs_lock();
1444
Jesse Grossccb13522011-10-25 19:26:31 -07001445 vport = new_vport(&parms);
1446 if (IS_ERR(vport)) {
1447 err = PTR_ERR(vport);
1448 if (err == -EBUSY)
1449 err = -EEXIST;
1450
Thomas Graf44da5ae2013-12-13 15:22:19 +01001451 if (err == -EEXIST) {
1452 /* An outdated user space instance that does not understand
1453 * the concept of user_features has attempted to create a new
1454 * datapath and is likely to reuse it. Drop all user features.
1455 */
1456 if (info->genlhdr->version < OVS_DP_VER_FEATURES)
1457 ovs_dp_reset_user_features(skb, info);
1458 }
1459
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001460 goto err_destroy_ports_array;
Jesse Grossccb13522011-10-25 19:26:31 -07001461 }
1462
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001463 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1464 info->snd_seq, 0, OVS_DP_CMD_NEW);
1465 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001466
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001467 ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001468 list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001469
1470 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001471
Johannes Berg2a94fe42013-11-19 15:19:39 +01001472 ovs_notify(&dp_datapath_genl_family, reply, info);
Jesse Grossccb13522011-10-25 19:26:31 -07001473 return 0;
1474
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001475err_destroy_ports_array:
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001476 ovs_unlock();
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001477 kfree(dp->ports);
Jesse Grossccb13522011-10-25 19:26:31 -07001478err_destroy_percpu:
1479 free_percpu(dp->stats_percpu);
1480err_destroy_table:
Pravin B Shelar9b996e52014-05-06 18:41:20 -07001481 ovs_flow_tbl_destroy(&dp->table);
Jesse Grossccb13522011-10-25 19:26:31 -07001482err_free_dp:
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001483 release_net(ovs_dp_get_net(dp));
Jesse Grossccb13522011-10-25 19:26:31 -07001484 kfree(dp);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001485err_free_reply:
1486 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001487err:
1488 return err;
1489}
1490
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001491/* Called with ovs_mutex. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001492static void __dp_destroy(struct datapath *dp)
Jesse Grossccb13522011-10-25 19:26:31 -07001493{
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001494 int i;
Jesse Grossccb13522011-10-25 19:26:31 -07001495
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001496 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1497 struct vport *vport;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001498 struct hlist_node *n;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001499
Sasha Levinb67bfe02013-02-27 17:06:00 -08001500 hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001501 if (vport->port_no != OVSP_LOCAL)
1502 ovs_dp_detach_port(vport);
1503 }
Jesse Grossccb13522011-10-25 19:26:31 -07001504
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001505 list_del_rcu(&dp->list_node);
Jesse Grossccb13522011-10-25 19:26:31 -07001506
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001507 /* OVSP_LOCAL is datapath internal port. We need to make sure that
Andy Zhoue80857c2014-01-21 09:31:04 -08001508 * all ports in datapath are destroyed first before freeing datapath.
Jesse Grossccb13522011-10-25 19:26:31 -07001509 */
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001510 ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
Jesse Grossccb13522011-10-25 19:26:31 -07001511
Andy Zhoue80857c2014-01-21 09:31:04 -08001512 /* RCU destroy the flow table */
Jesse Grossccb13522011-10-25 19:26:31 -07001513 call_rcu(&dp->rcu, destroy_dp_rcu);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001514}
1515
1516static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1517{
1518 struct sk_buff *reply;
1519 struct datapath *dp;
1520 int err;
1521
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001522 reply = ovs_dp_cmd_alloc_info(info);
1523 if (!reply)
1524 return -ENOMEM;
1525
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001526 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001527 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1528 err = PTR_ERR(dp);
1529 if (IS_ERR(dp))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001530 goto err_unlock_free;
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001531
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001532 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1533 info->snd_seq, 0, OVS_DP_CMD_DEL);
1534 BUG_ON(err < 0);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001535
1536 __dp_destroy(dp);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001537 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001538
Johannes Berg2a94fe42013-11-19 15:19:39 +01001539 ovs_notify(&dp_datapath_genl_family, reply, info);
Jesse Grossccb13522011-10-25 19:26:31 -07001540
1541 return 0;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001542
1543err_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001544 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001545 kfree_skb(reply);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001546 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001547}
1548
1549static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1550{
1551 struct sk_buff *reply;
1552 struct datapath *dp;
1553 int err;
1554
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001555 reply = ovs_dp_cmd_alloc_info(info);
1556 if (!reply)
1557 return -ENOMEM;
1558
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001559 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001560 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001561 err = PTR_ERR(dp);
Jesse Grossccb13522011-10-25 19:26:31 -07001562 if (IS_ERR(dp))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001563 goto err_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001564
Thomas Graf43d4be92013-12-13 15:22:18 +01001565 ovs_dp_change(dp, info->attrs);
1566
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001567 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1568 info->snd_seq, 0, OVS_DP_CMD_NEW);
1569 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001570
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001571 ovs_unlock();
Johannes Berg2a94fe42013-11-19 15:19:39 +01001572 ovs_notify(&dp_datapath_genl_family, reply, info);
Jesse Grossccb13522011-10-25 19:26:31 -07001573
1574 return 0;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001575
1576err_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001577 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001578 kfree_skb(reply);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001579 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001580}
1581
1582static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1583{
1584 struct sk_buff *reply;
1585 struct datapath *dp;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001586 int err;
Jesse Grossccb13522011-10-25 19:26:31 -07001587
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001588 reply = ovs_dp_cmd_alloc_info(info);
1589 if (!reply)
1590 return -ENOMEM;
1591
1592 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001593 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001594 if (IS_ERR(dp)) {
1595 err = PTR_ERR(dp);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001596 goto err_unlock_free;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001597 }
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001598 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1599 info->snd_seq, 0, OVS_DP_CMD_NEW);
1600 BUG_ON(err < 0);
1601 rcu_read_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001602
Jesse Grossccb13522011-10-25 19:26:31 -07001603 return genlmsg_reply(reply, info);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001604
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001605err_unlock_free:
1606 rcu_read_unlock();
1607 kfree_skb(reply);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001608 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001609}
1610
1611static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1612{
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001613 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
Jesse Grossccb13522011-10-25 19:26:31 -07001614 struct datapath *dp;
1615 int skip = cb->args[0];
1616 int i = 0;
1617
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001618 rcu_read_lock();
1619 list_for_each_entry_rcu(dp, &ovs_net->dps, list_node) {
Ben Pfaff77676fd2012-01-17 13:33:39 +00001620 if (i >= skip &&
Eric W. Biederman15e47302012-09-07 20:12:54 +00001621 ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001622 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1623 OVS_DP_CMD_NEW) < 0)
1624 break;
1625 i++;
1626 }
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001627 rcu_read_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001628
1629 cb->args[0] = i;
1630
1631 return skb->len;
1632}
1633
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001634static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1635 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1636 [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1637 [OVS_DP_ATTR_USER_FEATURES] = { .type = NLA_U32 },
1638};
1639
stephen hemminger48e48a72014-07-16 11:25:52 -07001640static const struct genl_ops dp_datapath_genl_ops[] = {
Jesse Grossccb13522011-10-25 19:26:31 -07001641 { .cmd = OVS_DP_CMD_NEW,
1642 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1643 .policy = datapath_policy,
1644 .doit = ovs_dp_cmd_new
1645 },
1646 { .cmd = OVS_DP_CMD_DEL,
1647 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1648 .policy = datapath_policy,
1649 .doit = ovs_dp_cmd_del
1650 },
1651 { .cmd = OVS_DP_CMD_GET,
1652 .flags = 0, /* OK for unprivileged users. */
1653 .policy = datapath_policy,
1654 .doit = ovs_dp_cmd_get,
1655 .dumpit = ovs_dp_cmd_dump
1656 },
1657 { .cmd = OVS_DP_CMD_SET,
1658 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1659 .policy = datapath_policy,
1660 .doit = ovs_dp_cmd_set,
1661 },
1662};
1663
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001664static struct genl_family dp_datapath_genl_family = {
Jesse Grossccb13522011-10-25 19:26:31 -07001665 .id = GENL_ID_GENERATE,
1666 .hdrsize = sizeof(struct ovs_header),
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001667 .name = OVS_DATAPATH_FAMILY,
1668 .version = OVS_DATAPATH_VERSION,
1669 .maxattr = OVS_DP_ATTR_MAX,
Pravin B Shelar3a4e0d62013-04-23 07:48:48 +00001670 .netnsok = true,
1671 .parallel_ops = true,
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001672 .ops = dp_datapath_genl_ops,
1673 .n_ops = ARRAY_SIZE(dp_datapath_genl_ops),
1674 .mcgrps = &ovs_dp_datapath_multicast_group,
1675 .n_mcgrps = 1,
Jesse Grossccb13522011-10-25 19:26:31 -07001676};
1677
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001678/* Called with ovs_mutex or RCU read lock. */
Jesse Grossccb13522011-10-25 19:26:31 -07001679static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001680 u32 portid, u32 seq, u32 flags, u8 cmd)
Jesse Grossccb13522011-10-25 19:26:31 -07001681{
1682 struct ovs_header *ovs_header;
1683 struct ovs_vport_stats vport_stats;
1684 int err;
1685
Eric W. Biederman15e47302012-09-07 20:12:54 +00001686 ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
Jesse Grossccb13522011-10-25 19:26:31 -07001687 flags, cmd);
1688 if (!ovs_header)
1689 return -EMSGSIZE;
1690
1691 ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1692
David S. Miller028d6a62012-03-29 23:20:48 -04001693 if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
1694 nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
Alex Wang5cd667b2014-07-17 15:14:13 -07001695 nla_put_string(skb, OVS_VPORT_ATTR_NAME,
1696 vport->ops->get_name(vport)))
David S. Miller028d6a62012-03-29 23:20:48 -04001697 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001698
1699 ovs_vport_get_stats(vport, &vport_stats);
David S. Miller028d6a62012-03-29 23:20:48 -04001700 if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
1701 &vport_stats))
1702 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001703
Alex Wang5cd667b2014-07-17 15:14:13 -07001704 if (ovs_vport_get_upcall_portids(vport, skb))
1705 goto nla_put_failure;
1706
Jesse Grossccb13522011-10-25 19:26:31 -07001707 err = ovs_vport_get_options(vport, skb);
1708 if (err == -EMSGSIZE)
1709 goto error;
1710
1711 return genlmsg_end(skb, ovs_header);
1712
1713nla_put_failure:
1714 err = -EMSGSIZE;
1715error:
1716 genlmsg_cancel(skb, ovs_header);
1717 return err;
1718}
1719
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001720static struct sk_buff *ovs_vport_cmd_alloc_info(void)
1721{
1722 return nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1723}
1724
1725/* Called with ovs_mutex, only via ovs_dp_notify_wq(). */
Eric W. Biederman15e47302012-09-07 20:12:54 +00001726struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001727 u32 seq, u8 cmd)
1728{
1729 struct sk_buff *skb;
1730 int retval;
1731
1732 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1733 if (!skb)
1734 return ERR_PTR(-ENOMEM);
1735
Eric W. Biederman15e47302012-09-07 20:12:54 +00001736 retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
Jesse Grossa9341512013-03-26 15:48:38 -07001737 BUG_ON(retval < 0);
1738
Jesse Grossccb13522011-10-25 19:26:31 -07001739 return skb;
1740}
1741
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001742/* Called with ovs_mutex or RCU read lock. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001743static struct vport *lookup_vport(struct net *net,
1744 struct ovs_header *ovs_header,
Jesse Grossccb13522011-10-25 19:26:31 -07001745 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1746{
1747 struct datapath *dp;
1748 struct vport *vport;
1749
1750 if (a[OVS_VPORT_ATTR_NAME]) {
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001751 vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
Jesse Grossccb13522011-10-25 19:26:31 -07001752 if (!vport)
1753 return ERR_PTR(-ENODEV);
Ben Pfaff651a68e2012-03-06 15:04:04 -08001754 if (ovs_header->dp_ifindex &&
1755 ovs_header->dp_ifindex != get_dpifindex(vport->dp))
1756 return ERR_PTR(-ENODEV);
Jesse Grossccb13522011-10-25 19:26:31 -07001757 return vport;
1758 } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
1759 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1760
1761 if (port_no >= DP_MAX_PORTS)
1762 return ERR_PTR(-EFBIG);
1763
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001764 dp = get_dp(net, ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001765 if (!dp)
1766 return ERR_PTR(-ENODEV);
1767
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001768 vport = ovs_vport_ovsl_rcu(dp, port_no);
Jesse Grossccb13522011-10-25 19:26:31 -07001769 if (!vport)
Jarno Rajahalme14408db2013-01-09 14:27:35 -08001770 return ERR_PTR(-ENODEV);
Jesse Grossccb13522011-10-25 19:26:31 -07001771 return vport;
1772 } else
1773 return ERR_PTR(-EINVAL);
1774}
1775
1776static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
1777{
1778 struct nlattr **a = info->attrs;
1779 struct ovs_header *ovs_header = info->userhdr;
1780 struct vport_parms parms;
1781 struct sk_buff *reply;
1782 struct vport *vport;
1783 struct datapath *dp;
1784 u32 port_no;
1785 int err;
1786
Jesse Grossccb13522011-10-25 19:26:31 -07001787 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
1788 !a[OVS_VPORT_ATTR_UPCALL_PID])
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001789 return -EINVAL;
1790
1791 port_no = a[OVS_VPORT_ATTR_PORT_NO]
1792 ? nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]) : 0;
1793 if (port_no >= DP_MAX_PORTS)
1794 return -EFBIG;
1795
1796 reply = ovs_vport_cmd_alloc_info();
1797 if (!reply)
1798 return -ENOMEM;
Jesse Grossccb13522011-10-25 19:26:31 -07001799
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001800 ovs_lock();
Thomas Graf62b9c8d2014-10-22 17:29:06 +02001801restart:
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001802 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001803 err = -ENODEV;
1804 if (!dp)
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001805 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001806
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001807 if (port_no) {
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001808 vport = ovs_vport_ovsl(dp, port_no);
Jesse Grossccb13522011-10-25 19:26:31 -07001809 err = -EBUSY;
1810 if (vport)
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001811 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001812 } else {
1813 for (port_no = 1; ; port_no++) {
1814 if (port_no >= DP_MAX_PORTS) {
1815 err = -EFBIG;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001816 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001817 }
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001818 vport = ovs_vport_ovsl(dp, port_no);
Jesse Grossccb13522011-10-25 19:26:31 -07001819 if (!vport)
1820 break;
1821 }
1822 }
1823
1824 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
1825 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1826 parms.options = a[OVS_VPORT_ATTR_OPTIONS];
1827 parms.dp = dp;
1828 parms.port_no = port_no;
Alex Wang5cd667b2014-07-17 15:14:13 -07001829 parms.upcall_portids = a[OVS_VPORT_ATTR_UPCALL_PID];
Jesse Grossccb13522011-10-25 19:26:31 -07001830
1831 vport = new_vport(&parms);
1832 err = PTR_ERR(vport);
Thomas Graf62b9c8d2014-10-22 17:29:06 +02001833 if (IS_ERR(vport)) {
1834 if (err == -EAGAIN)
1835 goto restart;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001836 goto exit_unlock_free;
Thomas Graf62b9c8d2014-10-22 17:29:06 +02001837 }
Jesse Grossccb13522011-10-25 19:26:31 -07001838
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001839 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1840 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1841 BUG_ON(err < 0);
1842 ovs_unlock();
Thomas Grafed661182013-03-29 14:46:50 +01001843
Johannes Berg2a94fe42013-11-19 15:19:39 +01001844 ovs_notify(&dp_vport_genl_family, reply, info);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001845 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001846
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001847exit_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001848 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001849 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001850 return err;
1851}
1852
1853static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
1854{
1855 struct nlattr **a = info->attrs;
1856 struct sk_buff *reply;
1857 struct vport *vport;
1858 int err;
1859
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001860 reply = ovs_vport_cmd_alloc_info();
1861 if (!reply)
1862 return -ENOMEM;
1863
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001864 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001865 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
Jesse Grossccb13522011-10-25 19:26:31 -07001866 err = PTR_ERR(vport);
1867 if (IS_ERR(vport))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001868 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001869
Jesse Grossccb13522011-10-25 19:26:31 -07001870 if (a[OVS_VPORT_ATTR_TYPE] &&
Jesse Grossf44f3402013-05-13 08:15:26 -07001871 nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
Jesse Grossccb13522011-10-25 19:26:31 -07001872 err = -EINVAL;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001873 goto exit_unlock_free;
Jesse Grossa9341512013-03-26 15:48:38 -07001874 }
1875
Jesse Grossf44f3402013-05-13 08:15:26 -07001876 if (a[OVS_VPORT_ATTR_OPTIONS]) {
Jesse Grossccb13522011-10-25 19:26:31 -07001877 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
Jesse Grossf44f3402013-05-13 08:15:26 -07001878 if (err)
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001879 goto exit_unlock_free;
Jesse Grossf44f3402013-05-13 08:15:26 -07001880 }
Jesse Grossa9341512013-03-26 15:48:38 -07001881
Alex Wang5cd667b2014-07-17 15:14:13 -07001882
1883 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
1884 struct nlattr *ids = a[OVS_VPORT_ATTR_UPCALL_PID];
1885
1886 err = ovs_vport_set_upcall_portids(vport, ids);
1887 if (err)
1888 goto exit_unlock_free;
1889 }
Jesse Grossccb13522011-10-25 19:26:31 -07001890
Jesse Grossa9341512013-03-26 15:48:38 -07001891 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1892 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1893 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001894
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001895 ovs_unlock();
Johannes Berg2a94fe42013-11-19 15:19:39 +01001896 ovs_notify(&dp_vport_genl_family, reply, info);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001897 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001898
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001899exit_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001900 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001901 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001902 return err;
1903}
1904
1905static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
1906{
1907 struct nlattr **a = info->attrs;
1908 struct sk_buff *reply;
1909 struct vport *vport;
1910 int err;
1911
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001912 reply = ovs_vport_cmd_alloc_info();
1913 if (!reply)
1914 return -ENOMEM;
1915
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001916 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001917 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
Jesse Grossccb13522011-10-25 19:26:31 -07001918 err = PTR_ERR(vport);
1919 if (IS_ERR(vport))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001920 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001921
1922 if (vport->port_no == OVSP_LOCAL) {
1923 err = -EINVAL;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001924 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001925 }
1926
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001927 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1928 info->snd_seq, 0, OVS_VPORT_CMD_DEL);
1929 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001930 ovs_dp_detach_port(vport);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001931 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001932
Johannes Berg2a94fe42013-11-19 15:19:39 +01001933 ovs_notify(&dp_vport_genl_family, reply, info);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001934 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001935
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001936exit_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001937 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001938 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001939 return err;
1940}
1941
1942static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
1943{
1944 struct nlattr **a = info->attrs;
1945 struct ovs_header *ovs_header = info->userhdr;
1946 struct sk_buff *reply;
1947 struct vport *vport;
1948 int err;
1949
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001950 reply = ovs_vport_cmd_alloc_info();
1951 if (!reply)
1952 return -ENOMEM;
1953
Jesse Grossccb13522011-10-25 19:26:31 -07001954 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001955 vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
Jesse Grossccb13522011-10-25 19:26:31 -07001956 err = PTR_ERR(vport);
1957 if (IS_ERR(vport))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001958 goto exit_unlock_free;
1959 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1960 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1961 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001962 rcu_read_unlock();
1963
1964 return genlmsg_reply(reply, info);
1965
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001966exit_unlock_free:
Jesse Grossccb13522011-10-25 19:26:31 -07001967 rcu_read_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001968 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001969 return err;
1970}
1971
1972static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1973{
1974 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1975 struct datapath *dp;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001976 int bucket = cb->args[0], skip = cb->args[1];
1977 int i, j = 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001978
Jesse Grossccb13522011-10-25 19:26:31 -07001979 rcu_read_lock();
Andy Zhoucc3a5ae2014-09-08 13:14:22 -07001980 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
Jarno Rajahalme42ee19e2014-02-15 17:42:29 -08001981 if (!dp) {
1982 rcu_read_unlock();
1983 return -ENODEV;
1984 }
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001985 for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
Jesse Grossccb13522011-10-25 19:26:31 -07001986 struct vport *vport;
1987
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001988 j = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001989 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001990 if (j >= skip &&
1991 ovs_vport_cmd_fill_info(vport, skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001992 NETLINK_CB(cb->skb).portid,
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001993 cb->nlh->nlmsg_seq,
1994 NLM_F_MULTI,
1995 OVS_VPORT_CMD_NEW) < 0)
1996 goto out;
Jesse Grossccb13522011-10-25 19:26:31 -07001997
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001998 j++;
1999 }
2000 skip = 0;
Jesse Grossccb13522011-10-25 19:26:31 -07002001 }
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07002002out:
Jesse Grossccb13522011-10-25 19:26:31 -07002003 rcu_read_unlock();
2004
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07002005 cb->args[0] = i;
2006 cb->args[1] = j;
Jesse Grossccb13522011-10-25 19:26:31 -07002007
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07002008 return skb->len;
Jesse Grossccb13522011-10-25 19:26:31 -07002009}
2010
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002011static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
2012 [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
2013 [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
2014 [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
2015 [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
2016 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
2017 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
2018};
2019
stephen hemminger48e48a72014-07-16 11:25:52 -07002020static const struct genl_ops dp_vport_genl_ops[] = {
Jesse Grossccb13522011-10-25 19:26:31 -07002021 { .cmd = OVS_VPORT_CMD_NEW,
2022 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2023 .policy = vport_policy,
2024 .doit = ovs_vport_cmd_new
2025 },
2026 { .cmd = OVS_VPORT_CMD_DEL,
2027 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2028 .policy = vport_policy,
2029 .doit = ovs_vport_cmd_del
2030 },
2031 { .cmd = OVS_VPORT_CMD_GET,
2032 .flags = 0, /* OK for unprivileged users. */
2033 .policy = vport_policy,
2034 .doit = ovs_vport_cmd_get,
2035 .dumpit = ovs_vport_cmd_dump
2036 },
2037 { .cmd = OVS_VPORT_CMD_SET,
2038 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2039 .policy = vport_policy,
2040 .doit = ovs_vport_cmd_set,
2041 },
2042};
2043
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002044struct genl_family dp_vport_genl_family = {
2045 .id = GENL_ID_GENERATE,
2046 .hdrsize = sizeof(struct ovs_header),
2047 .name = OVS_VPORT_FAMILY,
2048 .version = OVS_VPORT_VERSION,
2049 .maxattr = OVS_VPORT_ATTR_MAX,
2050 .netnsok = true,
2051 .parallel_ops = true,
2052 .ops = dp_vport_genl_ops,
2053 .n_ops = ARRAY_SIZE(dp_vport_genl_ops),
2054 .mcgrps = &ovs_dp_vport_multicast_group,
2055 .n_mcgrps = 1,
Jesse Grossccb13522011-10-25 19:26:31 -07002056};
2057
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002058static struct genl_family * const dp_genl_families[] = {
2059 &dp_datapath_genl_family,
2060 &dp_vport_genl_family,
2061 &dp_flow_genl_family,
2062 &dp_packet_genl_family,
Jesse Grossccb13522011-10-25 19:26:31 -07002063};
2064
2065static void dp_unregister_genl(int n_families)
2066{
2067 int i;
2068
2069 for (i = 0; i < n_families; i++)
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002070 genl_unregister_family(dp_genl_families[i]);
Jesse Grossccb13522011-10-25 19:26:31 -07002071}
2072
2073static int dp_register_genl(void)
2074{
Jesse Grossccb13522011-10-25 19:26:31 -07002075 int err;
2076 int i;
2077
Jesse Grossccb13522011-10-25 19:26:31 -07002078 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
Jesse Grossccb13522011-10-25 19:26:31 -07002079
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002080 err = genl_register_family(dp_genl_families[i]);
Jesse Grossccb13522011-10-25 19:26:31 -07002081 if (err)
2082 goto error;
Jesse Grossccb13522011-10-25 19:26:31 -07002083 }
2084
2085 return 0;
2086
2087error:
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002088 dp_unregister_genl(i);
Jesse Grossccb13522011-10-25 19:26:31 -07002089 return err;
2090}
2091
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002092static int __net_init ovs_init_net(struct net *net)
2093{
2094 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2095
2096 INIT_LIST_HEAD(&ovs_net->dps);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002097 INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002098 return 0;
2099}
2100
2101static void __net_exit ovs_exit_net(struct net *net)
2102{
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002103 struct datapath *dp, *dp_next;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002104 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002105
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002106 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002107 list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
2108 __dp_destroy(dp);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002109 ovs_unlock();
2110
2111 cancel_work_sync(&ovs_net->dp_notify_work);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002112}
2113
2114static struct pernet_operations ovs_net_ops = {
2115 .init = ovs_init_net,
2116 .exit = ovs_exit_net,
2117 .id = &ovs_net_id,
2118 .size = sizeof(struct ovs_net),
2119};
2120
Jesse Grossccb13522011-10-25 19:26:31 -07002121static int __init dp_init(void)
2122{
Jesse Grossccb13522011-10-25 19:26:31 -07002123 int err;
2124
YOSHIFUJI Hideaki / 吉藤英明3523b292013-01-09 07:19:55 +00002125 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
Jesse Grossccb13522011-10-25 19:26:31 -07002126
2127 pr_info("Open vSwitch switching datapath\n");
2128
Andy Zhou971427f32014-09-15 19:37:25 -07002129 err = action_fifos_init();
Jesse Grossccb13522011-10-25 19:26:31 -07002130 if (err)
2131 goto error;
2132
Andy Zhou971427f32014-09-15 19:37:25 -07002133 err = ovs_internal_dev_rtnl_link_register();
2134 if (err)
2135 goto error_action_fifos_exit;
2136
Jiri Pirko5b9e7e12014-06-26 09:58:26 +02002137 err = ovs_flow_init();
2138 if (err)
2139 goto error_unreg_rtnl_link;
2140
Jesse Grossccb13522011-10-25 19:26:31 -07002141 err = ovs_vport_init();
2142 if (err)
2143 goto error_flow_exit;
2144
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002145 err = register_pernet_device(&ovs_net_ops);
Jesse Grossccb13522011-10-25 19:26:31 -07002146 if (err)
2147 goto error_vport_exit;
2148
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002149 err = register_netdevice_notifier(&ovs_dp_device_notifier);
2150 if (err)
2151 goto error_netns_exit;
2152
Thomas Graf62b9c8d2014-10-22 17:29:06 +02002153 err = ovs_netdev_init();
2154 if (err)
2155 goto error_unreg_notifier;
2156
Jesse Grossccb13522011-10-25 19:26:31 -07002157 err = dp_register_genl();
2158 if (err < 0)
Thomas Graf62b9c8d2014-10-22 17:29:06 +02002159 goto error_unreg_netdev;
Jesse Grossccb13522011-10-25 19:26:31 -07002160
Jesse Grossccb13522011-10-25 19:26:31 -07002161 return 0;
2162
Thomas Graf62b9c8d2014-10-22 17:29:06 +02002163error_unreg_netdev:
2164 ovs_netdev_exit();
Jesse Grossccb13522011-10-25 19:26:31 -07002165error_unreg_notifier:
2166 unregister_netdevice_notifier(&ovs_dp_device_notifier);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002167error_netns_exit:
2168 unregister_pernet_device(&ovs_net_ops);
Jesse Grossccb13522011-10-25 19:26:31 -07002169error_vport_exit:
2170 ovs_vport_exit();
2171error_flow_exit:
2172 ovs_flow_exit();
Jiri Pirko5b9e7e12014-06-26 09:58:26 +02002173error_unreg_rtnl_link:
2174 ovs_internal_dev_rtnl_link_unregister();
Andy Zhou971427f32014-09-15 19:37:25 -07002175error_action_fifos_exit:
2176 action_fifos_exit();
Jesse Grossccb13522011-10-25 19:26:31 -07002177error:
2178 return err;
2179}
2180
2181static void dp_cleanup(void)
2182{
Jesse Grossccb13522011-10-25 19:26:31 -07002183 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
Thomas Graf62b9c8d2014-10-22 17:29:06 +02002184 ovs_netdev_exit();
Jesse Grossccb13522011-10-25 19:26:31 -07002185 unregister_netdevice_notifier(&ovs_dp_device_notifier);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002186 unregister_pernet_device(&ovs_net_ops);
2187 rcu_barrier();
Jesse Grossccb13522011-10-25 19:26:31 -07002188 ovs_vport_exit();
2189 ovs_flow_exit();
Jiri Pirko5b9e7e12014-06-26 09:58:26 +02002190 ovs_internal_dev_rtnl_link_unregister();
Andy Zhou971427f32014-09-15 19:37:25 -07002191 action_fifos_exit();
Jesse Grossccb13522011-10-25 19:26:31 -07002192}
2193
2194module_init(dp_init);
2195module_exit(dp_cleanup);
2196
2197MODULE_DESCRIPTION("Open vSwitch switching datapath");
2198MODULE_LICENSE("GPL");