blob: cdbc44c18714bea1fb648fb4a0587a803b9d7a46 [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 Grafed6611852013-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 Grafed6611852013-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;
260 struct dp_stats_percpu *stats;
Jesse Grossccb13522011-10-25 19:26:31 -0700261 u64 *stats_counter;
Andy Zhou1bd71162013-10-22 10:42:46 -0700262 u32 n_mask_hit;
Jesse Grossccb13522011-10-25 19:26:31 -0700263
Shan Wei404f2f12012-11-13 09:52:25 +0800264 stats = this_cpu_ptr(dp->stats_percpu);
Jesse Grossccb13522011-10-25 19:26:31 -0700265
Jesse Grossccb13522011-10-25 19:26:31 -0700266 /* Look up flow. */
Pravin B Shelar8c8b1b82014-09-15 19:28:44 -0700267 flow = ovs_flow_tbl_lookup_stats(&dp->table, key, &n_mask_hit);
Jesse Grossccb13522011-10-25 19:26:31 -0700268 if (unlikely(!flow)) {
269 struct dp_upcall_info upcall;
Pravin B Shelar8c8b1b82014-09-15 19:28:44 -0700270 int error;
Jesse Grossccb13522011-10-25 19:26:31 -0700271
272 upcall.cmd = OVS_PACKET_CMD_MISS;
Pravin B Shelar8c8b1b82014-09-15 19:28:44 -0700273 upcall.key = key;
Jesse Grossccb13522011-10-25 19:26:31 -0700274 upcall.userdata = NULL;
Alex Wang5cd667b2014-07-17 15:14:13 -0700275 upcall.portid = ovs_vport_find_upcall_portid(p, skb);
Li RongQingc5eba0b2014-09-03 17:43:45 +0800276 error = ovs_dp_upcall(dp, skb, &upcall);
277 if (unlikely(error))
278 kfree_skb(skb);
279 else
280 consume_skb(skb);
Jesse Grossccb13522011-10-25 19:26:31 -0700281 stats_counter = &stats->n_missed;
282 goto out;
283 }
284
285 OVS_CB(skb)->flow = flow;
286
Pravin B Shelar8c8b1b82014-09-15 19:28:44 -0700287 ovs_flow_stats_update(OVS_CB(skb)->flow, key->tp.flags, skb);
288 ovs_execute_actions(dp, skb, key);
Pravin B Shelare298e502013-10-29 17:22:21 -0700289 stats_counter = &stats->n_hit;
Jesse Grossccb13522011-10-25 19:26:31 -0700290
291out:
292 /* Update datapath statistics. */
WANG Congdf9d9fd2014-02-14 15:10:46 -0800293 u64_stats_update_begin(&stats->syncp);
Jesse Grossccb13522011-10-25 19:26:31 -0700294 (*stats_counter)++;
Andy Zhou1bd71162013-10-22 10:42:46 -0700295 stats->n_mask_hit += n_mask_hit;
WANG Congdf9d9fd2014-02-14 15:10:46 -0800296 u64_stats_update_end(&stats->syncp);
Jesse Grossccb13522011-10-25 19:26:31 -0700297}
298
Jesse Grossccb13522011-10-25 19:26:31 -0700299int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800300 const struct dp_upcall_info *upcall_info)
Jesse Grossccb13522011-10-25 19:26:31 -0700301{
302 struct dp_stats_percpu *stats;
Jesse Grossccb13522011-10-25 19:26:31 -0700303 int err;
304
Eric W. Biederman15e47302012-09-07 20:12:54 +0000305 if (upcall_info->portid == 0) {
Jesse Grossccb13522011-10-25 19:26:31 -0700306 err = -ENOTCONN;
307 goto err;
308 }
309
Jesse Grossccb13522011-10-25 19:26:31 -0700310 if (!skb_is_gso(skb))
Thomas Graf8055a892013-12-13 15:22:20 +0100311 err = queue_userspace_packet(dp, skb, upcall_info);
Jesse Grossccb13522011-10-25 19:26:31 -0700312 else
Thomas Graf8055a892013-12-13 15:22:20 +0100313 err = queue_gso_packets(dp, skb, upcall_info);
Jesse Grossccb13522011-10-25 19:26:31 -0700314 if (err)
315 goto err;
316
317 return 0;
318
319err:
Shan Wei404f2f12012-11-13 09:52:25 +0800320 stats = this_cpu_ptr(dp->stats_percpu);
Jesse Grossccb13522011-10-25 19:26:31 -0700321
WANG Congdf9d9fd2014-02-14 15:10:46 -0800322 u64_stats_update_begin(&stats->syncp);
Jesse Grossccb13522011-10-25 19:26:31 -0700323 stats->n_lost++;
WANG Congdf9d9fd2014-02-14 15:10:46 -0800324 u64_stats_update_end(&stats->syncp);
Jesse Grossccb13522011-10-25 19:26:31 -0700325
326 return err;
327}
328
Thomas Graf8055a892013-12-13 15:22:20 +0100329static int queue_gso_packets(struct datapath *dp, struct sk_buff *skb,
Jesse Grossccb13522011-10-25 19:26:31 -0700330 const struct dp_upcall_info *upcall_info)
331{
Ben Pfaffa1b5d0d2012-07-20 14:47:54 -0700332 unsigned short gso_type = skb_shinfo(skb)->gso_type;
Jesse Grossccb13522011-10-25 19:26:31 -0700333 struct dp_upcall_info later_info;
334 struct sw_flow_key later_key;
335 struct sk_buff *segs, *nskb;
336 int err;
337
Thomas Graf09c5e602013-12-13 15:22:22 +0100338 segs = __skb_gso_segment(skb, NETIF_F_SG, false);
Pravin B Shelar92e5dfc2012-07-20 14:46:29 -0700339 if (IS_ERR(segs))
340 return PTR_ERR(segs);
Florian Westphal330966e2014-10-20 13:49:17 +0200341 if (segs == NULL)
342 return -EINVAL;
Jesse Grossccb13522011-10-25 19:26:31 -0700343
344 /* Queue all of the segments. */
345 skb = segs;
346 do {
Thomas Graf8055a892013-12-13 15:22:20 +0100347 err = queue_userspace_packet(dp, skb, upcall_info);
Jesse Grossccb13522011-10-25 19:26:31 -0700348 if (err)
349 break;
350
Ben Pfaffa1b5d0d2012-07-20 14:47:54 -0700351 if (skb == segs && gso_type & SKB_GSO_UDP) {
Jesse Grossccb13522011-10-25 19:26:31 -0700352 /* The initial flow key extracted by ovs_flow_extract()
353 * in this case is for a first fragment, so we need to
354 * properly mark later fragments.
355 */
356 later_key = *upcall_info->key;
357 later_key.ip.frag = OVS_FRAG_TYPE_LATER;
358
359 later_info = *upcall_info;
360 later_info.key = &later_key;
361 upcall_info = &later_info;
362 }
363 } while ((skb = skb->next));
364
365 /* Free all of the segments. */
366 skb = segs;
367 do {
368 nskb = skb->next;
369 if (err)
370 kfree_skb(skb);
371 else
372 consume_skb(skb);
373 } while ((skb = nskb));
374 return err;
375}
376
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100377static size_t key_attr_size(void)
378{
379 return nla_total_size(4) /* OVS_KEY_ATTR_PRIORITY */
Pravin B Shelar7d5437c2013-06-17 17:50:18 -0700380 + nla_total_size(0) /* OVS_KEY_ATTR_TUNNEL */
381 + nla_total_size(8) /* OVS_TUNNEL_KEY_ATTR_ID */
382 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_SRC */
383 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_DST */
384 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TOS */
385 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TTL */
386 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT */
387 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_CSUM */
Jesse Gross67fa0342014-10-03 15:35:30 -0700388 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_OAM */
Jesse Grossf5796682014-10-03 15:35:33 -0700389 + nla_total_size(256) /* OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS */
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100390 + nla_total_size(4) /* OVS_KEY_ATTR_IN_PORT */
391 + nla_total_size(4) /* OVS_KEY_ATTR_SKB_MARK */
392 + nla_total_size(12) /* OVS_KEY_ATTR_ETHERNET */
393 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
394 + nla_total_size(4) /* OVS_KEY_ATTR_8021Q */
395 + nla_total_size(0) /* OVS_KEY_ATTR_ENCAP */
396 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
397 + nla_total_size(40) /* OVS_KEY_ATTR_IPV6 */
398 + nla_total_size(2) /* OVS_KEY_ATTR_ICMPV6 */
399 + nla_total_size(28); /* OVS_KEY_ATTR_ND */
400}
401
Thomas Grafbda56f12013-12-13 15:22:21 +0100402static size_t upcall_msg_size(const struct nlattr *userdata,
403 unsigned int hdrlen)
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100404{
405 size_t size = NLMSG_ALIGN(sizeof(struct ovs_header))
Thomas Grafbda56f12013-12-13 15:22:21 +0100406 + nla_total_size(hdrlen) /* OVS_PACKET_ATTR_PACKET */
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100407 + nla_total_size(key_attr_size()); /* OVS_PACKET_ATTR_KEY */
408
409 /* OVS_PACKET_ATTR_USERDATA */
410 if (userdata)
411 size += NLA_ALIGN(userdata->nla_len);
412
413 return size;
414}
415
Thomas Graf8055a892013-12-13 15:22:20 +0100416static int queue_userspace_packet(struct datapath *dp, struct sk_buff *skb,
Jesse Grossccb13522011-10-25 19:26:31 -0700417 const struct dp_upcall_info *upcall_info)
418{
419 struct ovs_header *upcall;
420 struct sk_buff *nskb = NULL;
Li RongQing4ee45ea2014-09-02 20:52:28 +0800421 struct sk_buff *user_skb = NULL; /* to be queued to userspace */
Jesse Grossccb13522011-10-25 19:26:31 -0700422 struct nlattr *nla;
Thomas Graf795449d2013-11-30 13:21:32 +0100423 struct genl_info info = {
Thomas Graf8055a892013-12-13 15:22:20 +0100424 .dst_sk = ovs_dp_get_net(dp)->genl_sock,
Thomas Graf795449d2013-11-30 13:21:32 +0100425 .snd_portid = upcall_info->portid,
426 };
427 size_t len;
Thomas Grafbda56f12013-12-13 15:22:21 +0100428 unsigned int hlen;
Thomas Graf8055a892013-12-13 15:22:20 +0100429 int err, dp_ifindex;
430
431 dp_ifindex = get_dpifindex(dp);
432 if (!dp_ifindex)
433 return -ENODEV;
Jesse Grossccb13522011-10-25 19:26:31 -0700434
435 if (vlan_tx_tag_present(skb)) {
436 nskb = skb_clone(skb, GFP_ATOMIC);
437 if (!nskb)
438 return -ENOMEM;
439
Patrick McHardy86a9bad2013-04-19 02:04:30 +0000440 nskb = __vlan_put_tag(nskb, nskb->vlan_proto, vlan_tx_tag_get(nskb));
Dan Carpenter8aa51d62012-05-13 08:44:18 +0000441 if (!nskb)
Jesse Grossccb13522011-10-25 19:26:31 -0700442 return -ENOMEM;
443
444 nskb->vlan_tci = 0;
445 skb = nskb;
446 }
447
448 if (nla_attr_size(skb->len) > USHRT_MAX) {
449 err = -EFBIG;
450 goto out;
451 }
452
Thomas Grafbda56f12013-12-13 15:22:21 +0100453 /* Complete checksum if needed */
454 if (skb->ip_summed == CHECKSUM_PARTIAL &&
455 (err = skb_checksum_help(skb)))
456 goto out;
457
458 /* Older versions of OVS user space enforce alignment of the last
459 * Netlink attribute to NLA_ALIGNTO which would require extensive
460 * padding logic. Only perform zerocopy if padding is not required.
461 */
462 if (dp->user_features & OVS_DP_F_UNALIGNED)
463 hlen = skb_zerocopy_headlen(skb);
464 else
465 hlen = skb->len;
466
467 len = upcall_msg_size(upcall_info->userdata, hlen);
Thomas Graf795449d2013-11-30 13:21:32 +0100468 user_skb = genlmsg_new_unicast(len, &info, GFP_ATOMIC);
Jesse Grossccb13522011-10-25 19:26:31 -0700469 if (!user_skb) {
470 err = -ENOMEM;
471 goto out;
472 }
473
474 upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
475 0, upcall_info->cmd);
476 upcall->dp_ifindex = dp_ifindex;
477
478 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
Andy Zhouf53e3832014-07-17 15:17:44 -0700479 err = ovs_nla_put_flow(upcall_info->key, upcall_info->key, user_skb);
480 BUG_ON(err);
Jesse Grossccb13522011-10-25 19:26:31 -0700481 nla_nest_end(user_skb, nla);
482
483 if (upcall_info->userdata)
Ben Pfaff44901082013-02-15 17:29:22 -0800484 __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA,
485 nla_len(upcall_info->userdata),
486 nla_data(upcall_info->userdata));
Jesse Grossccb13522011-10-25 19:26:31 -0700487
Thomas Grafbda56f12013-12-13 15:22:21 +0100488 /* Only reserve room for attribute header, packet data is added
489 * in skb_zerocopy() */
490 if (!(nla = nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, 0))) {
491 err = -ENOBUFS;
492 goto out;
493 }
494 nla->nla_len = nla_attr_size(skb->len);
Jesse Grossccb13522011-10-25 19:26:31 -0700495
Zoltan Kiss36d5fe62014-03-26 22:37:45 +0000496 err = skb_zerocopy(user_skb, skb, skb->len, hlen);
497 if (err)
498 goto out;
Jesse Grossccb13522011-10-25 19:26:31 -0700499
Thomas Grafaea0bb42014-01-14 16:27:49 +0000500 /* Pad OVS_PACKET_ATTR_PACKET if linear copy was performed */
501 if (!(dp->user_features & OVS_DP_F_UNALIGNED)) {
502 size_t plen = NLA_ALIGN(user_skb->len) - user_skb->len;
503
504 if (plen > 0)
505 memset(skb_put(user_skb, plen), 0, plen);
506 }
507
Thomas Grafbda56f12013-12-13 15:22:21 +0100508 ((struct nlmsghdr *) user_skb->data)->nlmsg_len = user_skb->len;
509
Thomas Graf8055a892013-12-13 15:22:20 +0100510 err = genlmsg_unicast(ovs_dp_get_net(dp), user_skb, upcall_info->portid);
Li RongQing4ee45ea2014-09-02 20:52:28 +0800511 user_skb = NULL;
Jesse Grossccb13522011-10-25 19:26:31 -0700512out:
Zoltan Kiss36d5fe62014-03-26 22:37:45 +0000513 if (err)
514 skb_tx_error(skb);
Li RongQing4ee45ea2014-09-02 20:52:28 +0800515 kfree_skb(user_skb);
Jesse Grossccb13522011-10-25 19:26:31 -0700516 kfree_skb(nskb);
517 return err;
518}
519
Jesse Grossccb13522011-10-25 19:26:31 -0700520static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
521{
522 struct ovs_header *ovs_header = info->userhdr;
523 struct nlattr **a = info->attrs;
524 struct sw_flow_actions *acts;
525 struct sk_buff *packet;
526 struct sw_flow *flow;
527 struct datapath *dp;
528 struct ethhdr *eth;
Pravin B Shelar83c8df22014-09-15 19:20:31 -0700529 struct vport *input_vport;
Jesse Grossccb13522011-10-25 19:26:31 -0700530 int len;
531 int err;
Jesse Grossccb13522011-10-25 19:26:31 -0700532
533 err = -EINVAL;
534 if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
Thomas Grafdded45fc2013-03-29 14:46:47 +0100535 !a[OVS_PACKET_ATTR_ACTIONS])
Jesse Grossccb13522011-10-25 19:26:31 -0700536 goto err;
537
538 len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
539 packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
540 err = -ENOMEM;
541 if (!packet)
542 goto err;
543 skb_reserve(packet, NET_IP_ALIGN);
544
Thomas Graf32686a92013-03-29 14:46:48 +0100545 nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
Jesse Grossccb13522011-10-25 19:26:31 -0700546
547 skb_reset_mac_header(packet);
548 eth = eth_hdr(packet);
549
550 /* Normally, setting the skb 'protocol' field would be handled by a
551 * call to eth_type_trans(), but it assumes there's a sending
552 * device, which we may not have. */
Simon Hormane5c5d222013-03-28 13:38:25 +0900553 if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN)
Jesse Grossccb13522011-10-25 19:26:31 -0700554 packet->protocol = eth->h_proto;
555 else
556 packet->protocol = htons(ETH_P_802_2);
557
558 /* Build an sw_flow for sending this packet. */
Jarno Rajahalme23dabf82014-03-27 12:35:23 -0700559 flow = ovs_flow_alloc();
Jesse Grossccb13522011-10-25 19:26:31 -0700560 err = PTR_ERR(flow);
561 if (IS_ERR(flow))
562 goto err_kfree_skb;
563
Pravin B Shelar83c8df22014-09-15 19:20:31 -0700564 err = ovs_flow_key_extract_userspace(a[OVS_PACKET_ATTR_KEY], packet,
565 &flow->key);
Jesse Grossccb13522011-10-25 19:26:31 -0700566 if (err)
567 goto err_flow_free;
568
Pravin B Shelare6445712013-10-03 18:16:47 -0700569 acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_PACKET_ATTR_ACTIONS]));
Jesse Grossccb13522011-10-25 19:26:31 -0700570 err = PTR_ERR(acts);
571 if (IS_ERR(acts))
572 goto err_flow_free;
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700573
Pravin B Shelare6445712013-10-03 18:16:47 -0700574 err = ovs_nla_copy_actions(a[OVS_PACKET_ATTR_ACTIONS],
Simon Horman25cd9ba2014-10-06 05:05:13 -0700575 &flow->key, &acts);
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700576 if (err)
577 goto err_flow_free;
Jesse Grossccb13522011-10-25 19:26:31 -0700578
Jesse Grossf5796682014-10-03 15:35:33 -0700579 rcu_assign_pointer(flow->sf_acts, acts);
580
581 OVS_CB(packet)->egress_tun_info = NULL;
Jesse Grossccb13522011-10-25 19:26:31 -0700582 OVS_CB(packet)->flow = flow;
583 packet->priority = flow->key.phy.priority;
Ansis Atteka39c7caeb2012-11-26 11:24:11 -0800584 packet->mark = flow->key.phy.skb_mark;
Jesse Grossccb13522011-10-25 19:26:31 -0700585
586 rcu_read_lock();
Andy Zhoucc3a5ae2014-09-08 13:14:22 -0700587 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -0700588 err = -ENODEV;
589 if (!dp)
590 goto err_unlock;
591
Pravin B Shelar83c8df22014-09-15 19:20:31 -0700592 input_vport = ovs_vport_rcu(dp, flow->key.phy.in_port);
593 if (!input_vport)
594 input_vport = ovs_vport_rcu(dp, OVSP_LOCAL);
595
596 if (!input_vport)
597 goto err_unlock;
598
599 OVS_CB(packet)->input_vport = input_vport;
600
Jesse Grossccb13522011-10-25 19:26:31 -0700601 local_bh_disable();
Pravin B Shelar2ff3e4e2014-09-15 19:15:28 -0700602 err = ovs_execute_actions(dp, packet, &flow->key);
Jesse Grossccb13522011-10-25 19:26:31 -0700603 local_bh_enable();
604 rcu_read_unlock();
605
Andy Zhou03f0d912013-08-07 20:01:00 -0700606 ovs_flow_free(flow, false);
Jesse Grossccb13522011-10-25 19:26:31 -0700607 return err;
608
609err_unlock:
610 rcu_read_unlock();
611err_flow_free:
Andy Zhou03f0d912013-08-07 20:01:00 -0700612 ovs_flow_free(flow, false);
Jesse Grossccb13522011-10-25 19:26:31 -0700613err_kfree_skb:
614 kfree_skb(packet);
615err:
616 return err;
617}
618
619static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
Thomas Grafdded45fc2013-03-29 14:46:47 +0100620 [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
Jesse Grossccb13522011-10-25 19:26:31 -0700621 [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
622 [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
623};
624
Johannes Berg4534de82013-11-14 17:14:46 +0100625static const struct genl_ops dp_packet_genl_ops[] = {
Jesse Grossccb13522011-10-25 19:26:31 -0700626 { .cmd = OVS_PACKET_CMD_EXECUTE,
627 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
628 .policy = packet_policy,
629 .doit = ovs_packet_cmd_execute
630 }
631};
632
Pravin B Shelar0c200ef2014-05-06 16:44:50 -0700633static struct genl_family dp_packet_genl_family = {
634 .id = GENL_ID_GENERATE,
635 .hdrsize = sizeof(struct ovs_header),
636 .name = OVS_PACKET_FAMILY,
637 .version = OVS_PACKET_VERSION,
638 .maxattr = OVS_PACKET_ATTR_MAX,
639 .netnsok = true,
640 .parallel_ops = true,
641 .ops = dp_packet_genl_ops,
642 .n_ops = ARRAY_SIZE(dp_packet_genl_ops),
643};
644
Andy Zhou1bd71162013-10-22 10:42:46 -0700645static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats,
646 struct ovs_dp_megaflow_stats *mega_stats)
Jesse Grossccb13522011-10-25 19:26:31 -0700647{
648 int i;
Jesse Grossccb13522011-10-25 19:26:31 -0700649
Andy Zhou1bd71162013-10-22 10:42:46 -0700650 memset(mega_stats, 0, sizeof(*mega_stats));
651
Pravin B Shelarb637e492013-10-04 00:14:23 -0700652 stats->n_flows = ovs_flow_tbl_count(&dp->table);
Andy Zhou1bd71162013-10-22 10:42:46 -0700653 mega_stats->n_masks = ovs_flow_tbl_num_masks(&dp->table);
Jesse Grossccb13522011-10-25 19:26:31 -0700654
655 stats->n_hit = stats->n_missed = stats->n_lost = 0;
Andy Zhou1bd71162013-10-22 10:42:46 -0700656
Jesse Grossccb13522011-10-25 19:26:31 -0700657 for_each_possible_cpu(i) {
658 const struct dp_stats_percpu *percpu_stats;
659 struct dp_stats_percpu local_stats;
660 unsigned int start;
661
662 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
663
664 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700665 start = u64_stats_fetch_begin_irq(&percpu_stats->syncp);
Jesse Grossccb13522011-10-25 19:26:31 -0700666 local_stats = *percpu_stats;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700667 } while (u64_stats_fetch_retry_irq(&percpu_stats->syncp, start));
Jesse Grossccb13522011-10-25 19:26:31 -0700668
669 stats->n_hit += local_stats.n_hit;
670 stats->n_missed += local_stats.n_missed;
671 stats->n_lost += local_stats.n_lost;
Andy Zhou1bd71162013-10-22 10:42:46 -0700672 mega_stats->n_mask_hit += local_stats.n_mask_hit;
Jesse Grossccb13522011-10-25 19:26:31 -0700673 }
674}
675
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100676static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts)
677{
678 return NLMSG_ALIGN(sizeof(struct ovs_header))
679 + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_KEY */
Andy Zhou03f0d912013-08-07 20:01:00 -0700680 + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_MASK */
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100681 + nla_total_size(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
682 + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
683 + nla_total_size(8) /* OVS_FLOW_ATTR_USED */
684 + nla_total_size(acts->actions_len); /* OVS_FLOW_ATTR_ACTIONS */
685}
686
Jarno Rajahalmebb6f9a72014-05-05 11:32:17 -0700687/* Called with ovs_mutex or RCU read lock. */
Joe Stringerca7105f2014-09-08 13:09:37 -0700688static int ovs_flow_cmd_fill_match(const struct sw_flow *flow,
689 struct sk_buff *skb)
Jesse Grossccb13522011-10-25 19:26:31 -0700690{
Jesse Grossccb13522011-10-25 19:26:31 -0700691 struct nlattr *nla;
Jesse Grossccb13522011-10-25 19:26:31 -0700692 int err;
693
Andy Zhou03f0d912013-08-07 20:01:00 -0700694 /* Fill flow key. */
Jesse Grossccb13522011-10-25 19:26:31 -0700695 nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
696 if (!nla)
Joe Stringerca7105f2014-09-08 13:09:37 -0700697 return -EMSGSIZE;
Andy Zhou03f0d912013-08-07 20:01:00 -0700698
Pravin B Shelare6445712013-10-03 18:16:47 -0700699 err = ovs_nla_put_flow(&flow->unmasked_key, &flow->unmasked_key, skb);
Jesse Grossccb13522011-10-25 19:26:31 -0700700 if (err)
Joe Stringerca7105f2014-09-08 13:09:37 -0700701 return err;
702
Jesse Grossccb13522011-10-25 19:26:31 -0700703 nla_nest_end(skb, nla);
704
Joe Stringerca7105f2014-09-08 13:09:37 -0700705 /* Fill flow mask. */
Andy Zhou03f0d912013-08-07 20:01:00 -0700706 nla = nla_nest_start(skb, OVS_FLOW_ATTR_MASK);
707 if (!nla)
Joe Stringerca7105f2014-09-08 13:09:37 -0700708 return -EMSGSIZE;
Andy Zhou03f0d912013-08-07 20:01:00 -0700709
Pravin B Shelare6445712013-10-03 18:16:47 -0700710 err = ovs_nla_put_flow(&flow->key, &flow->mask->key, skb);
Andy Zhou03f0d912013-08-07 20:01:00 -0700711 if (err)
Joe Stringerca7105f2014-09-08 13:09:37 -0700712 return err;
Andy Zhou03f0d912013-08-07 20:01:00 -0700713
714 nla_nest_end(skb, nla);
Joe Stringerca7105f2014-09-08 13:09:37 -0700715 return 0;
716}
717
718/* Called with ovs_mutex or RCU read lock. */
719static int ovs_flow_cmd_fill_stats(const struct sw_flow *flow,
720 struct sk_buff *skb)
721{
722 struct ovs_flow_stats stats;
723 __be16 tcp_flags;
724 unsigned long used;
Andy Zhou03f0d912013-08-07 20:01:00 -0700725
Pravin B Shelare298e502013-10-29 17:22:21 -0700726 ovs_flow_stats_get(flow, &stats, &used, &tcp_flags);
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700727
David S. Miller028d6a62012-03-29 23:20:48 -0400728 if (used &&
729 nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
Joe Stringerca7105f2014-09-08 13:09:37 -0700730 return -EMSGSIZE;
Jesse Grossccb13522011-10-25 19:26:31 -0700731
David S. Miller028d6a62012-03-29 23:20:48 -0400732 if (stats.n_packets &&
Pravin B Shelare298e502013-10-29 17:22:21 -0700733 nla_put(skb, OVS_FLOW_ATTR_STATS, sizeof(struct ovs_flow_stats), &stats))
Joe Stringerca7105f2014-09-08 13:09:37 -0700734 return -EMSGSIZE;
Jesse Grossccb13522011-10-25 19:26:31 -0700735
Pravin B Shelare298e502013-10-29 17:22:21 -0700736 if ((u8)ntohs(tcp_flags) &&
737 nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, (u8)ntohs(tcp_flags)))
Joe Stringerca7105f2014-09-08 13:09:37 -0700738 return -EMSGSIZE;
739
740 return 0;
741}
742
743/* Called with ovs_mutex or RCU read lock. */
744static int ovs_flow_cmd_fill_actions(const struct sw_flow *flow,
745 struct sk_buff *skb, int skb_orig_len)
746{
747 struct nlattr *start;
748 int err;
Jesse Grossccb13522011-10-25 19:26:31 -0700749
750 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
751 * this is the first flow to be dumped into 'skb'. This is unusual for
752 * Netlink but individual action lists can be longer than
753 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
754 * The userspace caller can always fetch the actions separately if it
755 * really wants them. (Most userspace callers in fact don't care.)
756 *
757 * This can only fail for dump operations because the skb is always
758 * properly sized for single flows.
759 */
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700760 start = nla_nest_start(skb, OVS_FLOW_ATTR_ACTIONS);
761 if (start) {
Pravin B Shelard57170b2013-07-30 15:39:39 -0700762 const struct sw_flow_actions *sf_acts;
763
Jesse Gross663efa32013-12-03 10:58:53 -0800764 sf_acts = rcu_dereference_ovsl(flow->sf_acts);
Pravin B Shelare6445712013-10-03 18:16:47 -0700765 err = ovs_nla_put_actions(sf_acts->actions,
766 sf_acts->actions_len, skb);
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700767
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700768 if (!err)
769 nla_nest_end(skb, start);
770 else {
771 if (skb_orig_len)
Joe Stringerca7105f2014-09-08 13:09:37 -0700772 return err;
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700773
774 nla_nest_cancel(skb, start);
775 }
Joe Stringerca7105f2014-09-08 13:09:37 -0700776 } else if (skb_orig_len) {
777 return -EMSGSIZE;
778 }
779
780 return 0;
781}
782
783/* Called with ovs_mutex or RCU read lock. */
784static int ovs_flow_cmd_fill_info(const struct sw_flow *flow, int dp_ifindex,
785 struct sk_buff *skb, u32 portid,
786 u32 seq, u32 flags, u8 cmd)
787{
788 const int skb_orig_len = skb->len;
789 struct ovs_header *ovs_header;
790 int err;
791
792 ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family,
793 flags, cmd);
794 if (!ovs_header)
795 return -EMSGSIZE;
796
797 ovs_header->dp_ifindex = dp_ifindex;
798
799 err = ovs_flow_cmd_fill_match(flow, skb);
800 if (err)
801 goto error;
802
803 err = ovs_flow_cmd_fill_stats(flow, skb);
804 if (err)
805 goto error;
806
807 err = ovs_flow_cmd_fill_actions(flow, skb, skb_orig_len);
808 if (err)
809 goto error;
Jesse Grossccb13522011-10-25 19:26:31 -0700810
811 return genlmsg_end(skb, ovs_header);
812
Jesse Grossccb13522011-10-25 19:26:31 -0700813error:
814 genlmsg_cancel(skb, ovs_header);
815 return err;
816}
817
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700818/* May not be called with RCU read lock. */
819static struct sk_buff *ovs_flow_cmd_alloc_info(const struct sw_flow_actions *acts,
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700820 struct genl_info *info,
821 bool always)
Jesse Grossccb13522011-10-25 19:26:31 -0700822{
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700823 struct sk_buff *skb;
Jesse Grossccb13522011-10-25 19:26:31 -0700824
Samuel Gauthier9b67aa42014-09-18 10:31:04 +0200825 if (!always && !ovs_must_notify(&dp_flow_genl_family, info, 0))
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700826 return NULL;
827
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700828 skb = genlmsg_new_unicast(ovs_flow_cmd_msg_size(acts), info, GFP_KERNEL);
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700829 if (!skb)
830 return ERR_PTR(-ENOMEM);
831
832 return skb;
Jesse Grossccb13522011-10-25 19:26:31 -0700833}
834
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700835/* Called with ovs_mutex. */
836static struct sk_buff *ovs_flow_cmd_build_info(const struct sw_flow *flow,
837 int dp_ifindex,
838 struct genl_info *info, u8 cmd,
839 bool always)
Jesse Grossccb13522011-10-25 19:26:31 -0700840{
841 struct sk_buff *skb;
842 int retval;
843
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700844 skb = ovs_flow_cmd_alloc_info(ovsl_dereference(flow->sf_acts), info,
845 always);
Himangi Saraogid0e992a2014-07-27 12:37:46 +0530846 if (IS_ERR_OR_NULL(skb))
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700847 return skb;
Jesse Grossccb13522011-10-25 19:26:31 -0700848
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700849 retval = ovs_flow_cmd_fill_info(flow, dp_ifindex, skb,
850 info->snd_portid, info->snd_seq, 0,
851 cmd);
Jesse Grossccb13522011-10-25 19:26:31 -0700852 BUG_ON(retval < 0);
853 return skb;
854}
855
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700856static int ovs_flow_cmd_new(struct sk_buff *skb, struct genl_info *info)
Jesse Grossccb13522011-10-25 19:26:31 -0700857{
858 struct nlattr **a = info->attrs;
859 struct ovs_header *ovs_header = info->userhdr;
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700860 struct sw_flow *flow, *new_flow;
Andy Zhou03f0d912013-08-07 20:01:00 -0700861 struct sw_flow_mask mask;
Jesse Grossccb13522011-10-25 19:26:31 -0700862 struct sk_buff *reply;
863 struct datapath *dp;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700864 struct sw_flow_actions *acts;
865 struct sw_flow_match match;
866 int error;
867
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700868 /* Must have key and actions. */
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700869 error = -EINVAL;
Jesse Gross426cda52014-10-06 05:08:38 -0700870 if (!a[OVS_FLOW_ATTR_KEY]) {
871 OVS_NLERR("Flow key attribute not present in new flow.\n");
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700872 goto error;
Jesse Gross426cda52014-10-06 05:08:38 -0700873 }
874 if (!a[OVS_FLOW_ATTR_ACTIONS]) {
875 OVS_NLERR("Flow actions attribute not present in new flow.\n");
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700876 goto error;
Jesse Gross426cda52014-10-06 05:08:38 -0700877 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700878
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700879 /* Most of the time we need to allocate a new flow, do it before
880 * locking.
881 */
882 new_flow = ovs_flow_alloc();
883 if (IS_ERR(new_flow)) {
884 error = PTR_ERR(new_flow);
885 goto error;
886 }
887
888 /* Extract key. */
889 ovs_match_init(&match, &new_flow->unmasked_key, &mask);
890 error = ovs_nla_get_match(&match,
891 a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
892 if (error)
893 goto err_kfree_flow;
894
895 ovs_flow_mask_key(&new_flow->key, &new_flow->unmasked_key, &mask);
896
897 /* Validate actions. */
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700898 acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_FLOW_ATTR_ACTIONS]));
899 error = PTR_ERR(acts);
900 if (IS_ERR(acts))
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700901 goto err_kfree_flow;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700902
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700903 error = ovs_nla_copy_actions(a[OVS_FLOW_ATTR_ACTIONS], &new_flow->key,
Simon Horman25cd9ba2014-10-06 05:05:13 -0700904 &acts);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700905 if (error) {
906 OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700907 goto err_kfree_acts;
908 }
909
910 reply = ovs_flow_cmd_alloc_info(acts, info, false);
911 if (IS_ERR(reply)) {
912 error = PTR_ERR(reply);
913 goto err_kfree_acts;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700914 }
915
916 ovs_lock();
917 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700918 if (unlikely(!dp)) {
919 error = -ENODEV;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700920 goto err_unlock_ovs;
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700921 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700922 /* Check if this is a duplicate flow */
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700923 flow = ovs_flow_tbl_lookup(&dp->table, &new_flow->unmasked_key);
924 if (likely(!flow)) {
925 rcu_assign_pointer(new_flow->sf_acts, acts);
926
927 /* Put flow in bucket. */
928 error = ovs_flow_tbl_insert(&dp->table, new_flow, &mask);
929 if (unlikely(error)) {
930 acts = NULL;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700931 goto err_unlock_ovs;
932 }
933
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700934 if (unlikely(reply)) {
935 error = ovs_flow_cmd_fill_info(new_flow,
936 ovs_header->dp_ifindex,
937 reply, info->snd_portid,
938 info->snd_seq, 0,
939 OVS_FLOW_CMD_NEW);
940 BUG_ON(error < 0);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700941 }
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700942 ovs_unlock();
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700943 } else {
944 struct sw_flow_actions *old_acts;
945
946 /* Bail out if we're not allowed to modify an existing flow.
947 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
948 * because Generic Netlink treats the latter as a dump
949 * request. We also accept NLM_F_EXCL in case that bug ever
950 * gets fixed.
951 */
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700952 if (unlikely(info->nlhdr->nlmsg_flags & (NLM_F_CREATE
953 | NLM_F_EXCL))) {
954 error = -EEXIST;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700955 goto err_unlock_ovs;
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700956 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700957 /* The unmasked key has to be the same for flow updates. */
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700958 if (unlikely(!ovs_flow_cmp_unmasked_key(flow, &match))) {
Alex Wang4a46b242014-06-30 20:30:29 -0700959 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
960 if (!flow) {
961 error = -ENOENT;
962 goto err_unlock_ovs;
963 }
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700964 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700965 /* Update actions. */
966 old_acts = ovsl_dereference(flow->sf_acts);
967 rcu_assign_pointer(flow->sf_acts, acts);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700968
969 if (unlikely(reply)) {
970 error = ovs_flow_cmd_fill_info(flow,
971 ovs_header->dp_ifindex,
972 reply, info->snd_portid,
973 info->snd_seq, 0,
974 OVS_FLOW_CMD_NEW);
975 BUG_ON(error < 0);
976 }
977 ovs_unlock();
978
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700979 ovs_nla_free_flow_actions(old_acts);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700980 ovs_flow_free(new_flow, false);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700981 }
982
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700983 if (reply)
984 ovs_notify(&dp_flow_genl_family, reply, info);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700985 return 0;
986
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700987err_unlock_ovs:
988 ovs_unlock();
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700989 kfree_skb(reply);
990err_kfree_acts:
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700991 kfree(acts);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700992err_kfree_flow:
993 ovs_flow_free(new_flow, false);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700994error:
995 return error;
996}
997
Jesse Gross6b205b22014-10-03 15:35:32 -0700998static struct sw_flow_actions *get_flow_actions(const struct nlattr *a,
999 const struct sw_flow_key *key,
1000 const struct sw_flow_mask *mask)
1001{
1002 struct sw_flow_actions *acts;
1003 struct sw_flow_key masked_key;
1004 int error;
1005
1006 acts = ovs_nla_alloc_flow_actions(nla_len(a));
1007 if (IS_ERR(acts))
1008 return acts;
1009
1010 ovs_flow_mask_key(&masked_key, key, mask);
Simon Horman25cd9ba2014-10-06 05:05:13 -07001011 error = ovs_nla_copy_actions(a, &masked_key, &acts);
Jesse Gross6b205b22014-10-03 15:35:32 -07001012 if (error) {
1013 OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
1014 kfree(acts);
1015 return ERR_PTR(error);
1016 }
1017
1018 return acts;
1019}
1020
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001021static int ovs_flow_cmd_set(struct sk_buff *skb, struct genl_info *info)
1022{
1023 struct nlattr **a = info->attrs;
1024 struct ovs_header *ovs_header = info->userhdr;
Jesse Gross6b205b22014-10-03 15:35:32 -07001025 struct sw_flow_key key;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001026 struct sw_flow *flow;
1027 struct sw_flow_mask mask;
1028 struct sk_buff *reply = NULL;
1029 struct datapath *dp;
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001030 struct sw_flow_actions *old_acts = NULL, *acts = NULL;
Andy Zhou03f0d912013-08-07 20:01:00 -07001031 struct sw_flow_match match;
Jesse Grossccb13522011-10-25 19:26:31 -07001032 int error;
Jesse Grossccb13522011-10-25 19:26:31 -07001033
1034 /* Extract key. */
1035 error = -EINVAL;
Jesse Gross426cda52014-10-06 05:08:38 -07001036 if (!a[OVS_FLOW_ATTR_KEY]) {
1037 OVS_NLERR("Flow key attribute not present in set flow.\n");
Jesse Grossccb13522011-10-25 19:26:31 -07001038 goto error;
Jesse Gross426cda52014-10-06 05:08:38 -07001039 }
Andy Zhou03f0d912013-08-07 20:01:00 -07001040
1041 ovs_match_init(&match, &key, &mask);
Jarno Rajahalme23dabf82014-03-27 12:35:23 -07001042 error = ovs_nla_get_match(&match,
Pravin B Shelare6445712013-10-03 18:16:47 -07001043 a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
Jesse Grossccb13522011-10-25 19:26:31 -07001044 if (error)
1045 goto error;
1046
1047 /* Validate actions. */
1048 if (a[OVS_FLOW_ATTR_ACTIONS]) {
Jesse Gross6b205b22014-10-03 15:35:32 -07001049 acts = get_flow_actions(a[OVS_FLOW_ATTR_ACTIONS], &key, &mask);
1050 if (IS_ERR(acts)) {
1051 error = PTR_ERR(acts);
Jesse Grossccb13522011-10-25 19:26:31 -07001052 goto error;
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001053 }
1054 }
1055
1056 /* Can allocate before locking if have acts. */
1057 if (acts) {
1058 reply = ovs_flow_cmd_alloc_info(acts, info, false);
1059 if (IS_ERR(reply)) {
1060 error = PTR_ERR(reply);
1061 goto err_kfree_acts;
Andy Zhou03f0d912013-08-07 20:01:00 -07001062 }
Jesse Grossccb13522011-10-25 19:26:31 -07001063 }
1064
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001065 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001066 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001067 if (unlikely(!dp)) {
1068 error = -ENODEV;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001069 goto err_unlock_ovs;
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001070 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001071 /* Check that the flow exists. */
Alex Wang4a46b242014-06-30 20:30:29 -07001072 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001073 if (unlikely(!flow)) {
1074 error = -ENOENT;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001075 goto err_unlock_ovs;
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001076 }
Alex Wang4a46b242014-06-30 20:30:29 -07001077
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001078 /* Update actions, if present. */
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001079 if (likely(acts)) {
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001080 old_acts = ovsl_dereference(flow->sf_acts);
Jesse Grossccb13522011-10-25 19:26:31 -07001081 rcu_assign_pointer(flow->sf_acts, acts);
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001082
1083 if (unlikely(reply)) {
1084 error = ovs_flow_cmd_fill_info(flow,
1085 ovs_header->dp_ifindex,
1086 reply, info->snd_portid,
1087 info->snd_seq, 0,
1088 OVS_FLOW_CMD_NEW);
1089 BUG_ON(error < 0);
1090 }
1091 } else {
1092 /* Could not alloc without acts before locking. */
1093 reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex,
1094 info, OVS_FLOW_CMD_NEW, false);
1095 if (unlikely(IS_ERR(reply))) {
1096 error = PTR_ERR(reply);
1097 goto err_unlock_ovs;
1098 }
Jesse Grossccb13522011-10-25 19:26:31 -07001099 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001100
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001101 /* Clear stats. */
1102 if (a[OVS_FLOW_ATTR_CLEAR])
1103 ovs_flow_stats_clear(flow);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001104 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001105
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001106 if (reply)
1107 ovs_notify(&dp_flow_genl_family, reply, info);
1108 if (old_acts)
1109 ovs_nla_free_flow_actions(old_acts);
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -07001110
Jesse Grossccb13522011-10-25 19:26:31 -07001111 return 0;
1112
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001113err_unlock_ovs:
1114 ovs_unlock();
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001115 kfree_skb(reply);
1116err_kfree_acts:
Pravin B Shelar74f84a52013-06-17 17:50:12 -07001117 kfree(acts);
Jesse Grossccb13522011-10-25 19:26:31 -07001118error:
1119 return error;
1120}
1121
1122static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1123{
1124 struct nlattr **a = info->attrs;
1125 struct ovs_header *ovs_header = info->userhdr;
1126 struct sw_flow_key key;
1127 struct sk_buff *reply;
1128 struct sw_flow *flow;
1129 struct datapath *dp;
Andy Zhou03f0d912013-08-07 20:01:00 -07001130 struct sw_flow_match match;
Jesse Grossccb13522011-10-25 19:26:31 -07001131 int err;
Jesse Grossccb13522011-10-25 19:26:31 -07001132
Andy Zhou03f0d912013-08-07 20:01:00 -07001133 if (!a[OVS_FLOW_ATTR_KEY]) {
1134 OVS_NLERR("Flow get message rejected, Key attribute missing.\n");
Jesse Grossccb13522011-10-25 19:26:31 -07001135 return -EINVAL;
Andy Zhou03f0d912013-08-07 20:01:00 -07001136 }
1137
1138 ovs_match_init(&match, &key, NULL);
Jarno Rajahalme23dabf82014-03-27 12:35:23 -07001139 err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL);
Jesse Grossccb13522011-10-25 19:26:31 -07001140 if (err)
1141 return err;
1142
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001143 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001144 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001145 if (!dp) {
1146 err = -ENODEV;
1147 goto unlock;
1148 }
Jesse Grossccb13522011-10-25 19:26:31 -07001149
Alex Wang4a46b242014-06-30 20:30:29 -07001150 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1151 if (!flow) {
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001152 err = -ENOENT;
1153 goto unlock;
1154 }
Jesse Grossccb13522011-10-25 19:26:31 -07001155
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -07001156 reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex, info,
1157 OVS_FLOW_CMD_NEW, true);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001158 if (IS_ERR(reply)) {
1159 err = PTR_ERR(reply);
1160 goto unlock;
1161 }
Jesse Grossccb13522011-10-25 19:26:31 -07001162
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001163 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001164 return genlmsg_reply(reply, info);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001165unlock:
1166 ovs_unlock();
1167 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001168}
1169
1170static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1171{
1172 struct nlattr **a = info->attrs;
1173 struct ovs_header *ovs_header = info->userhdr;
1174 struct sw_flow_key key;
1175 struct sk_buff *reply;
1176 struct sw_flow *flow;
1177 struct datapath *dp;
Andy Zhou03f0d912013-08-07 20:01:00 -07001178 struct sw_flow_match match;
Jesse Grossccb13522011-10-25 19:26:31 -07001179 int err;
Jesse Grossccb13522011-10-25 19:26:31 -07001180
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001181 if (likely(a[OVS_FLOW_ATTR_KEY])) {
1182 ovs_match_init(&match, &key, NULL);
1183 err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL);
1184 if (unlikely(err))
1185 return err;
1186 }
1187
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001188 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001189 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001190 if (unlikely(!dp)) {
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001191 err = -ENODEV;
1192 goto unlock;
1193 }
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001194
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001195 if (unlikely(!a[OVS_FLOW_ATTR_KEY])) {
Pravin B Shelarb637e492013-10-04 00:14:23 -07001196 err = ovs_flow_tbl_flush(&dp->table);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001197 goto unlock;
1198 }
Andy Zhou03f0d912013-08-07 20:01:00 -07001199
Alex Wang4a46b242014-06-30 20:30:29 -07001200 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1201 if (unlikely(!flow)) {
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001202 err = -ENOENT;
1203 goto unlock;
1204 }
Jesse Grossccb13522011-10-25 19:26:31 -07001205
Pravin B Shelarb637e492013-10-04 00:14:23 -07001206 ovs_flow_tbl_remove(&dp->table, flow);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001207 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001208
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001209 reply = ovs_flow_cmd_alloc_info((const struct sw_flow_actions __force *) flow->sf_acts,
1210 info, false);
1211 if (likely(reply)) {
1212 if (likely(!IS_ERR(reply))) {
1213 rcu_read_lock(); /*To keep RCU checker happy. */
1214 err = ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex,
1215 reply, info->snd_portid,
1216 info->snd_seq, 0,
1217 OVS_FLOW_CMD_DEL);
1218 rcu_read_unlock();
1219 BUG_ON(err < 0);
1220
1221 ovs_notify(&dp_flow_genl_family, reply, info);
1222 } else {
1223 netlink_set_err(sock_net(skb->sk)->genl_sock, 0, 0, PTR_ERR(reply));
1224 }
1225 }
1226
1227 ovs_flow_free(flow, true);
Jesse Grossccb13522011-10-25 19:26:31 -07001228 return 0;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001229unlock:
1230 ovs_unlock();
1231 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001232}
1233
1234static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1235{
1236 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
Pravin B Shelarb637e492013-10-04 00:14:23 -07001237 struct table_instance *ti;
Jesse Grossccb13522011-10-25 19:26:31 -07001238 struct datapath *dp;
Jesse Grossccb13522011-10-25 19:26:31 -07001239
Pravin B Shelard57170b2013-07-30 15:39:39 -07001240 rcu_read_lock();
Andy Zhoucc3a5ae2014-09-08 13:14:22 -07001241 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001242 if (!dp) {
Pravin B Shelard57170b2013-07-30 15:39:39 -07001243 rcu_read_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001244 return -ENODEV;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001245 }
Jesse Grossccb13522011-10-25 19:26:31 -07001246
Pravin B Shelarb637e492013-10-04 00:14:23 -07001247 ti = rcu_dereference(dp->table.ti);
Jesse Grossccb13522011-10-25 19:26:31 -07001248 for (;;) {
1249 struct sw_flow *flow;
1250 u32 bucket, obj;
1251
1252 bucket = cb->args[0];
1253 obj = cb->args[1];
Pravin B Shelarb637e492013-10-04 00:14:23 -07001254 flow = ovs_flow_tbl_dump_next(ti, &bucket, &obj);
Jesse Grossccb13522011-10-25 19:26:31 -07001255 if (!flow)
1256 break;
1257
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -07001258 if (ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex, skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001259 NETLINK_CB(cb->skb).portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001260 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1261 OVS_FLOW_CMD_NEW) < 0)
1262 break;
1263
1264 cb->args[0] = bucket;
1265 cb->args[1] = obj;
1266 }
Pravin B Shelard57170b2013-07-30 15:39:39 -07001267 rcu_read_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001268 return skb->len;
1269}
1270
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001271static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
1272 [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
1273 [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
1274 [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
1275};
1276
stephen hemminger48e48a72014-07-16 11:25:52 -07001277static const struct genl_ops dp_flow_genl_ops[] = {
Jesse Grossccb13522011-10-25 19:26:31 -07001278 { .cmd = OVS_FLOW_CMD_NEW,
1279 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1280 .policy = flow_policy,
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001281 .doit = ovs_flow_cmd_new
Jesse Grossccb13522011-10-25 19:26:31 -07001282 },
1283 { .cmd = OVS_FLOW_CMD_DEL,
1284 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1285 .policy = flow_policy,
1286 .doit = ovs_flow_cmd_del
1287 },
1288 { .cmd = OVS_FLOW_CMD_GET,
1289 .flags = 0, /* OK for unprivileged users. */
1290 .policy = flow_policy,
1291 .doit = ovs_flow_cmd_get,
1292 .dumpit = ovs_flow_cmd_dump
1293 },
1294 { .cmd = OVS_FLOW_CMD_SET,
1295 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1296 .policy = flow_policy,
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001297 .doit = ovs_flow_cmd_set,
Jesse Grossccb13522011-10-25 19:26:31 -07001298 },
1299};
1300
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001301static struct genl_family dp_flow_genl_family = {
Jesse Grossccb13522011-10-25 19:26:31 -07001302 .id = GENL_ID_GENERATE,
1303 .hdrsize = sizeof(struct ovs_header),
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001304 .name = OVS_FLOW_FAMILY,
1305 .version = OVS_FLOW_VERSION,
1306 .maxattr = OVS_FLOW_ATTR_MAX,
Pravin B Shelar3a4e0d62013-04-23 07:48:48 +00001307 .netnsok = true,
1308 .parallel_ops = true,
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001309 .ops = dp_flow_genl_ops,
1310 .n_ops = ARRAY_SIZE(dp_flow_genl_ops),
1311 .mcgrps = &ovs_dp_flow_multicast_group,
1312 .n_mcgrps = 1,
Jesse Grossccb13522011-10-25 19:26:31 -07001313};
1314
Thomas Grafc3ff8cf2013-03-29 14:46:49 +01001315static size_t ovs_dp_cmd_msg_size(void)
1316{
1317 size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
1318
1319 msgsize += nla_total_size(IFNAMSIZ);
1320 msgsize += nla_total_size(sizeof(struct ovs_dp_stats));
Andy Zhou1bd71162013-10-22 10:42:46 -07001321 msgsize += nla_total_size(sizeof(struct ovs_dp_megaflow_stats));
Daniele Di Proietto45fb9c32014-01-23 10:47:35 -08001322 msgsize += nla_total_size(sizeof(u32)); /* OVS_DP_ATTR_USER_FEATURES */
Thomas Grafc3ff8cf2013-03-29 14:46:49 +01001323
1324 return msgsize;
1325}
1326
Jarno Rajahalmebb6f9a72014-05-05 11:32:17 -07001327/* Called with ovs_mutex or RCU read lock. */
Jesse Grossccb13522011-10-25 19:26:31 -07001328static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001329 u32 portid, u32 seq, u32 flags, u8 cmd)
Jesse Grossccb13522011-10-25 19:26:31 -07001330{
1331 struct ovs_header *ovs_header;
1332 struct ovs_dp_stats dp_stats;
Andy Zhou1bd71162013-10-22 10:42:46 -07001333 struct ovs_dp_megaflow_stats dp_megaflow_stats;
Jesse Grossccb13522011-10-25 19:26:31 -07001334 int err;
1335
Eric W. Biederman15e47302012-09-07 20:12:54 +00001336 ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
Jesse Grossccb13522011-10-25 19:26:31 -07001337 flags, cmd);
1338 if (!ovs_header)
1339 goto error;
1340
1341 ovs_header->dp_ifindex = get_dpifindex(dp);
1342
Jesse Grossccb13522011-10-25 19:26:31 -07001343 err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
Jesse Grossccb13522011-10-25 19:26:31 -07001344 if (err)
1345 goto nla_put_failure;
1346
Andy Zhou1bd71162013-10-22 10:42:46 -07001347 get_dp_stats(dp, &dp_stats, &dp_megaflow_stats);
1348 if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats),
1349 &dp_stats))
1350 goto nla_put_failure;
1351
1352 if (nla_put(skb, OVS_DP_ATTR_MEGAFLOW_STATS,
1353 sizeof(struct ovs_dp_megaflow_stats),
1354 &dp_megaflow_stats))
David S. Miller028d6a62012-03-29 23:20:48 -04001355 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001356
Thomas Graf43d4be92013-12-13 15:22:18 +01001357 if (nla_put_u32(skb, OVS_DP_ATTR_USER_FEATURES, dp->user_features))
1358 goto nla_put_failure;
1359
Jesse Grossccb13522011-10-25 19:26:31 -07001360 return genlmsg_end(skb, ovs_header);
1361
1362nla_put_failure:
1363 genlmsg_cancel(skb, ovs_header);
1364error:
1365 return -EMSGSIZE;
1366}
1367
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001368static struct sk_buff *ovs_dp_cmd_alloc_info(struct genl_info *info)
Jesse Grossccb13522011-10-25 19:26:31 -07001369{
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001370 return genlmsg_new_unicast(ovs_dp_cmd_msg_size(), info, GFP_KERNEL);
Jesse Grossccb13522011-10-25 19:26:31 -07001371}
1372
Jarno Rajahalmebb6f9a72014-05-05 11:32:17 -07001373/* Called with rcu_read_lock or ovs_mutex. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001374static struct datapath *lookup_datapath(struct net *net,
1375 struct ovs_header *ovs_header,
Jesse Grossccb13522011-10-25 19:26:31 -07001376 struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1377{
1378 struct datapath *dp;
1379
1380 if (!a[OVS_DP_ATTR_NAME])
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001381 dp = get_dp(net, ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001382 else {
1383 struct vport *vport;
1384
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001385 vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
Jesse Grossccb13522011-10-25 19:26:31 -07001386 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
Jesse Grossccb13522011-10-25 19:26:31 -07001387 }
1388 return dp ? dp : ERR_PTR(-ENODEV);
1389}
1390
Thomas Graf44da5ae2013-12-13 15:22:19 +01001391static void ovs_dp_reset_user_features(struct sk_buff *skb, struct genl_info *info)
1392{
1393 struct datapath *dp;
1394
1395 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
Jiri Pirko3c7eacf2014-02-14 11:42:36 +01001396 if (IS_ERR(dp))
Thomas Graf44da5ae2013-12-13 15:22:19 +01001397 return;
1398
1399 WARN(dp->user_features, "Dropping previously announced user features\n");
1400 dp->user_features = 0;
1401}
1402
Thomas Graf43d4be92013-12-13 15:22:18 +01001403static void ovs_dp_change(struct datapath *dp, struct nlattr **a)
1404{
1405 if (a[OVS_DP_ATTR_USER_FEATURES])
1406 dp->user_features = nla_get_u32(a[OVS_DP_ATTR_USER_FEATURES]);
1407}
1408
Jesse Grossccb13522011-10-25 19:26:31 -07001409static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1410{
1411 struct nlattr **a = info->attrs;
1412 struct vport_parms parms;
1413 struct sk_buff *reply;
1414 struct datapath *dp;
1415 struct vport *vport;
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001416 struct ovs_net *ovs_net;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001417 int err, i;
Jesse Grossccb13522011-10-25 19:26:31 -07001418
1419 err = -EINVAL;
1420 if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1421 goto err;
1422
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001423 reply = ovs_dp_cmd_alloc_info(info);
1424 if (!reply)
1425 return -ENOMEM;
Jesse Grossccb13522011-10-25 19:26:31 -07001426
1427 err = -ENOMEM;
1428 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1429 if (dp == NULL)
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001430 goto err_free_reply;
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001431
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001432 ovs_dp_set_net(dp, hold_net(sock_net(skb->sk)));
Jesse Grossccb13522011-10-25 19:26:31 -07001433
1434 /* Allocate table. */
Pravin B Shelarb637e492013-10-04 00:14:23 -07001435 err = ovs_flow_tbl_init(&dp->table);
1436 if (err)
Jesse Grossccb13522011-10-25 19:26:31 -07001437 goto err_free_dp;
1438
WANG Cong1c213bd2014-02-13 11:46:28 -08001439 dp->stats_percpu = netdev_alloc_pcpu_stats(struct dp_stats_percpu);
Jesse Grossccb13522011-10-25 19:26:31 -07001440 if (!dp->stats_percpu) {
1441 err = -ENOMEM;
1442 goto err_destroy_table;
1443 }
1444
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001445 dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
Pravin B Shelare6445712013-10-03 18:16:47 -07001446 GFP_KERNEL);
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001447 if (!dp->ports) {
1448 err = -ENOMEM;
1449 goto err_destroy_percpu;
1450 }
1451
1452 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1453 INIT_HLIST_HEAD(&dp->ports[i]);
1454
Jesse Grossccb13522011-10-25 19:26:31 -07001455 /* Set up our datapath device. */
1456 parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1457 parms.type = OVS_VPORT_TYPE_INTERNAL;
1458 parms.options = NULL;
1459 parms.dp = dp;
1460 parms.port_no = OVSP_LOCAL;
Alex Wang5cd667b2014-07-17 15:14:13 -07001461 parms.upcall_portids = a[OVS_DP_ATTR_UPCALL_PID];
Jesse Grossccb13522011-10-25 19:26:31 -07001462
Thomas Graf43d4be92013-12-13 15:22:18 +01001463 ovs_dp_change(dp, a);
1464
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001465 /* So far only local changes have been made, now need the lock. */
1466 ovs_lock();
1467
Jesse Grossccb13522011-10-25 19:26:31 -07001468 vport = new_vport(&parms);
1469 if (IS_ERR(vport)) {
1470 err = PTR_ERR(vport);
1471 if (err == -EBUSY)
1472 err = -EEXIST;
1473
Thomas Graf44da5ae2013-12-13 15:22:19 +01001474 if (err == -EEXIST) {
1475 /* An outdated user space instance that does not understand
1476 * the concept of user_features has attempted to create a new
1477 * datapath and is likely to reuse it. Drop all user features.
1478 */
1479 if (info->genlhdr->version < OVS_DP_VER_FEATURES)
1480 ovs_dp_reset_user_features(skb, info);
1481 }
1482
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001483 goto err_destroy_ports_array;
Jesse Grossccb13522011-10-25 19:26:31 -07001484 }
1485
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001486 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1487 info->snd_seq, 0, OVS_DP_CMD_NEW);
1488 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001489
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001490 ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001491 list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001492
1493 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001494
Johannes Berg2a94fe42013-11-19 15:19:39 +01001495 ovs_notify(&dp_datapath_genl_family, reply, info);
Jesse Grossccb13522011-10-25 19:26:31 -07001496 return 0;
1497
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001498err_destroy_ports_array:
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001499 ovs_unlock();
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001500 kfree(dp->ports);
Jesse Grossccb13522011-10-25 19:26:31 -07001501err_destroy_percpu:
1502 free_percpu(dp->stats_percpu);
1503err_destroy_table:
Pravin B Shelar9b996e52014-05-06 18:41:20 -07001504 ovs_flow_tbl_destroy(&dp->table);
Jesse Grossccb13522011-10-25 19:26:31 -07001505err_free_dp:
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001506 release_net(ovs_dp_get_net(dp));
Jesse Grossccb13522011-10-25 19:26:31 -07001507 kfree(dp);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001508err_free_reply:
1509 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001510err:
1511 return err;
1512}
1513
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001514/* Called with ovs_mutex. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001515static void __dp_destroy(struct datapath *dp)
Jesse Grossccb13522011-10-25 19:26:31 -07001516{
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001517 int i;
Jesse Grossccb13522011-10-25 19:26:31 -07001518
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001519 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1520 struct vport *vport;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001521 struct hlist_node *n;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001522
Sasha Levinb67bfe02013-02-27 17:06:00 -08001523 hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001524 if (vport->port_no != OVSP_LOCAL)
1525 ovs_dp_detach_port(vport);
1526 }
Jesse Grossccb13522011-10-25 19:26:31 -07001527
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001528 list_del_rcu(&dp->list_node);
Jesse Grossccb13522011-10-25 19:26:31 -07001529
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001530 /* OVSP_LOCAL is datapath internal port. We need to make sure that
Andy Zhoue80857c2014-01-21 09:31:04 -08001531 * all ports in datapath are destroyed first before freeing datapath.
Jesse Grossccb13522011-10-25 19:26:31 -07001532 */
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001533 ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
Jesse Grossccb13522011-10-25 19:26:31 -07001534
Andy Zhoue80857c2014-01-21 09:31:04 -08001535 /* RCU destroy the flow table */
Jesse Grossccb13522011-10-25 19:26:31 -07001536 call_rcu(&dp->rcu, destroy_dp_rcu);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001537}
1538
1539static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1540{
1541 struct sk_buff *reply;
1542 struct datapath *dp;
1543 int err;
1544
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001545 reply = ovs_dp_cmd_alloc_info(info);
1546 if (!reply)
1547 return -ENOMEM;
1548
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001549 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001550 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1551 err = PTR_ERR(dp);
1552 if (IS_ERR(dp))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001553 goto err_unlock_free;
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001554
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001555 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1556 info->snd_seq, 0, OVS_DP_CMD_DEL);
1557 BUG_ON(err < 0);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001558
1559 __dp_destroy(dp);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001560 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001561
Johannes Berg2a94fe42013-11-19 15:19:39 +01001562 ovs_notify(&dp_datapath_genl_family, reply, info);
Jesse Grossccb13522011-10-25 19:26:31 -07001563
1564 return 0;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001565
1566err_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001567 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001568 kfree_skb(reply);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001569 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001570}
1571
1572static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1573{
1574 struct sk_buff *reply;
1575 struct datapath *dp;
1576 int err;
1577
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001578 reply = ovs_dp_cmd_alloc_info(info);
1579 if (!reply)
1580 return -ENOMEM;
1581
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001582 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001583 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001584 err = PTR_ERR(dp);
Jesse Grossccb13522011-10-25 19:26:31 -07001585 if (IS_ERR(dp))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001586 goto err_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001587
Thomas Graf43d4be92013-12-13 15:22:18 +01001588 ovs_dp_change(dp, info->attrs);
1589
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001590 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1591 info->snd_seq, 0, OVS_DP_CMD_NEW);
1592 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001593
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001594 ovs_unlock();
Johannes Berg2a94fe42013-11-19 15:19:39 +01001595 ovs_notify(&dp_datapath_genl_family, reply, info);
Jesse Grossccb13522011-10-25 19:26:31 -07001596
1597 return 0;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001598
1599err_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001600 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001601 kfree_skb(reply);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001602 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001603}
1604
1605static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1606{
1607 struct sk_buff *reply;
1608 struct datapath *dp;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001609 int err;
Jesse Grossccb13522011-10-25 19:26:31 -07001610
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001611 reply = ovs_dp_cmd_alloc_info(info);
1612 if (!reply)
1613 return -ENOMEM;
1614
1615 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001616 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001617 if (IS_ERR(dp)) {
1618 err = PTR_ERR(dp);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001619 goto err_unlock_free;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001620 }
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001621 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1622 info->snd_seq, 0, OVS_DP_CMD_NEW);
1623 BUG_ON(err < 0);
1624 rcu_read_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001625
Jesse Grossccb13522011-10-25 19:26:31 -07001626 return genlmsg_reply(reply, info);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001627
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001628err_unlock_free:
1629 rcu_read_unlock();
1630 kfree_skb(reply);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001631 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001632}
1633
1634static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1635{
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001636 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
Jesse Grossccb13522011-10-25 19:26:31 -07001637 struct datapath *dp;
1638 int skip = cb->args[0];
1639 int i = 0;
1640
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001641 rcu_read_lock();
1642 list_for_each_entry_rcu(dp, &ovs_net->dps, list_node) {
Ben Pfaff77676fd2012-01-17 13:33:39 +00001643 if (i >= skip &&
Eric W. Biederman15e47302012-09-07 20:12:54 +00001644 ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001645 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1646 OVS_DP_CMD_NEW) < 0)
1647 break;
1648 i++;
1649 }
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001650 rcu_read_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001651
1652 cb->args[0] = i;
1653
1654 return skb->len;
1655}
1656
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001657static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1658 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1659 [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1660 [OVS_DP_ATTR_USER_FEATURES] = { .type = NLA_U32 },
1661};
1662
stephen hemminger48e48a72014-07-16 11:25:52 -07001663static const struct genl_ops dp_datapath_genl_ops[] = {
Jesse Grossccb13522011-10-25 19:26:31 -07001664 { .cmd = OVS_DP_CMD_NEW,
1665 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1666 .policy = datapath_policy,
1667 .doit = ovs_dp_cmd_new
1668 },
1669 { .cmd = OVS_DP_CMD_DEL,
1670 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1671 .policy = datapath_policy,
1672 .doit = ovs_dp_cmd_del
1673 },
1674 { .cmd = OVS_DP_CMD_GET,
1675 .flags = 0, /* OK for unprivileged users. */
1676 .policy = datapath_policy,
1677 .doit = ovs_dp_cmd_get,
1678 .dumpit = ovs_dp_cmd_dump
1679 },
1680 { .cmd = OVS_DP_CMD_SET,
1681 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1682 .policy = datapath_policy,
1683 .doit = ovs_dp_cmd_set,
1684 },
1685};
1686
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001687static struct genl_family dp_datapath_genl_family = {
Jesse Grossccb13522011-10-25 19:26:31 -07001688 .id = GENL_ID_GENERATE,
1689 .hdrsize = sizeof(struct ovs_header),
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001690 .name = OVS_DATAPATH_FAMILY,
1691 .version = OVS_DATAPATH_VERSION,
1692 .maxattr = OVS_DP_ATTR_MAX,
Pravin B Shelar3a4e0d62013-04-23 07:48:48 +00001693 .netnsok = true,
1694 .parallel_ops = true,
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001695 .ops = dp_datapath_genl_ops,
1696 .n_ops = ARRAY_SIZE(dp_datapath_genl_ops),
1697 .mcgrps = &ovs_dp_datapath_multicast_group,
1698 .n_mcgrps = 1,
Jesse Grossccb13522011-10-25 19:26:31 -07001699};
1700
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001701/* Called with ovs_mutex or RCU read lock. */
Jesse Grossccb13522011-10-25 19:26:31 -07001702static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001703 u32 portid, u32 seq, u32 flags, u8 cmd)
Jesse Grossccb13522011-10-25 19:26:31 -07001704{
1705 struct ovs_header *ovs_header;
1706 struct ovs_vport_stats vport_stats;
1707 int err;
1708
Eric W. Biederman15e47302012-09-07 20:12:54 +00001709 ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
Jesse Grossccb13522011-10-25 19:26:31 -07001710 flags, cmd);
1711 if (!ovs_header)
1712 return -EMSGSIZE;
1713
1714 ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1715
David S. Miller028d6a62012-03-29 23:20:48 -04001716 if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
1717 nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
Alex Wang5cd667b2014-07-17 15:14:13 -07001718 nla_put_string(skb, OVS_VPORT_ATTR_NAME,
1719 vport->ops->get_name(vport)))
David S. Miller028d6a62012-03-29 23:20:48 -04001720 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001721
1722 ovs_vport_get_stats(vport, &vport_stats);
David S. Miller028d6a62012-03-29 23:20:48 -04001723 if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
1724 &vport_stats))
1725 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001726
Alex Wang5cd667b2014-07-17 15:14:13 -07001727 if (ovs_vport_get_upcall_portids(vport, skb))
1728 goto nla_put_failure;
1729
Jesse Grossccb13522011-10-25 19:26:31 -07001730 err = ovs_vport_get_options(vport, skb);
1731 if (err == -EMSGSIZE)
1732 goto error;
1733
1734 return genlmsg_end(skb, ovs_header);
1735
1736nla_put_failure:
1737 err = -EMSGSIZE;
1738error:
1739 genlmsg_cancel(skb, ovs_header);
1740 return err;
1741}
1742
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001743static struct sk_buff *ovs_vport_cmd_alloc_info(void)
1744{
1745 return nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1746}
1747
1748/* Called with ovs_mutex, only via ovs_dp_notify_wq(). */
Eric W. Biederman15e47302012-09-07 20:12:54 +00001749struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001750 u32 seq, u8 cmd)
1751{
1752 struct sk_buff *skb;
1753 int retval;
1754
1755 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1756 if (!skb)
1757 return ERR_PTR(-ENOMEM);
1758
Eric W. Biederman15e47302012-09-07 20:12:54 +00001759 retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
Jesse Grossa9341512013-03-26 15:48:38 -07001760 BUG_ON(retval < 0);
1761
Jesse Grossccb13522011-10-25 19:26:31 -07001762 return skb;
1763}
1764
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001765/* Called with ovs_mutex or RCU read lock. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001766static struct vport *lookup_vport(struct net *net,
1767 struct ovs_header *ovs_header,
Jesse Grossccb13522011-10-25 19:26:31 -07001768 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1769{
1770 struct datapath *dp;
1771 struct vport *vport;
1772
1773 if (a[OVS_VPORT_ATTR_NAME]) {
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001774 vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
Jesse Grossccb13522011-10-25 19:26:31 -07001775 if (!vport)
1776 return ERR_PTR(-ENODEV);
Ben Pfaff651a68e2012-03-06 15:04:04 -08001777 if (ovs_header->dp_ifindex &&
1778 ovs_header->dp_ifindex != get_dpifindex(vport->dp))
1779 return ERR_PTR(-ENODEV);
Jesse Grossccb13522011-10-25 19:26:31 -07001780 return vport;
1781 } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
1782 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1783
1784 if (port_no >= DP_MAX_PORTS)
1785 return ERR_PTR(-EFBIG);
1786
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001787 dp = get_dp(net, ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001788 if (!dp)
1789 return ERR_PTR(-ENODEV);
1790
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001791 vport = ovs_vport_ovsl_rcu(dp, port_no);
Jesse Grossccb13522011-10-25 19:26:31 -07001792 if (!vport)
Jarno Rajahalme14408db2013-01-09 14:27:35 -08001793 return ERR_PTR(-ENODEV);
Jesse Grossccb13522011-10-25 19:26:31 -07001794 return vport;
1795 } else
1796 return ERR_PTR(-EINVAL);
1797}
1798
1799static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
1800{
1801 struct nlattr **a = info->attrs;
1802 struct ovs_header *ovs_header = info->userhdr;
1803 struct vport_parms parms;
1804 struct sk_buff *reply;
1805 struct vport *vport;
1806 struct datapath *dp;
1807 u32 port_no;
1808 int err;
1809
Jesse Grossccb13522011-10-25 19:26:31 -07001810 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
1811 !a[OVS_VPORT_ATTR_UPCALL_PID])
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001812 return -EINVAL;
1813
1814 port_no = a[OVS_VPORT_ATTR_PORT_NO]
1815 ? nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]) : 0;
1816 if (port_no >= DP_MAX_PORTS)
1817 return -EFBIG;
1818
1819 reply = ovs_vport_cmd_alloc_info();
1820 if (!reply)
1821 return -ENOMEM;
Jesse Grossccb13522011-10-25 19:26:31 -07001822
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001823 ovs_lock();
Thomas Graf62b9c8d2014-10-22 17:29:06 +02001824restart:
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001825 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001826 err = -ENODEV;
1827 if (!dp)
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001828 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001829
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001830 if (port_no) {
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001831 vport = ovs_vport_ovsl(dp, port_no);
Jesse Grossccb13522011-10-25 19:26:31 -07001832 err = -EBUSY;
1833 if (vport)
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001834 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001835 } else {
1836 for (port_no = 1; ; port_no++) {
1837 if (port_no >= DP_MAX_PORTS) {
1838 err = -EFBIG;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001839 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001840 }
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001841 vport = ovs_vport_ovsl(dp, port_no);
Jesse Grossccb13522011-10-25 19:26:31 -07001842 if (!vport)
1843 break;
1844 }
1845 }
1846
1847 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
1848 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1849 parms.options = a[OVS_VPORT_ATTR_OPTIONS];
1850 parms.dp = dp;
1851 parms.port_no = port_no;
Alex Wang5cd667b2014-07-17 15:14:13 -07001852 parms.upcall_portids = a[OVS_VPORT_ATTR_UPCALL_PID];
Jesse Grossccb13522011-10-25 19:26:31 -07001853
1854 vport = new_vport(&parms);
1855 err = PTR_ERR(vport);
Thomas Graf62b9c8d2014-10-22 17:29:06 +02001856 if (IS_ERR(vport)) {
1857 if (err == -EAGAIN)
1858 goto restart;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001859 goto exit_unlock_free;
Thomas Graf62b9c8d2014-10-22 17:29:06 +02001860 }
Jesse Grossccb13522011-10-25 19:26:31 -07001861
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001862 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1863 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1864 BUG_ON(err < 0);
1865 ovs_unlock();
Thomas Grafed6611852013-03-29 14:46:50 +01001866
Johannes Berg2a94fe42013-11-19 15:19:39 +01001867 ovs_notify(&dp_vport_genl_family, reply, info);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001868 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001869
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001870exit_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001871 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001872 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001873 return err;
1874}
1875
1876static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
1877{
1878 struct nlattr **a = info->attrs;
1879 struct sk_buff *reply;
1880 struct vport *vport;
1881 int err;
1882
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001883 reply = ovs_vport_cmd_alloc_info();
1884 if (!reply)
1885 return -ENOMEM;
1886
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001887 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001888 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
Jesse Grossccb13522011-10-25 19:26:31 -07001889 err = PTR_ERR(vport);
1890 if (IS_ERR(vport))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001891 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001892
Jesse Grossccb13522011-10-25 19:26:31 -07001893 if (a[OVS_VPORT_ATTR_TYPE] &&
Jesse Grossf44f3402013-05-13 08:15:26 -07001894 nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
Jesse Grossccb13522011-10-25 19:26:31 -07001895 err = -EINVAL;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001896 goto exit_unlock_free;
Jesse Grossa9341512013-03-26 15:48:38 -07001897 }
1898
Jesse Grossf44f3402013-05-13 08:15:26 -07001899 if (a[OVS_VPORT_ATTR_OPTIONS]) {
Jesse Grossccb13522011-10-25 19:26:31 -07001900 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
Jesse Grossf44f3402013-05-13 08:15:26 -07001901 if (err)
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001902 goto exit_unlock_free;
Jesse Grossf44f3402013-05-13 08:15:26 -07001903 }
Jesse Grossa9341512013-03-26 15:48:38 -07001904
Alex Wang5cd667b2014-07-17 15:14:13 -07001905
1906 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
1907 struct nlattr *ids = a[OVS_VPORT_ATTR_UPCALL_PID];
1908
1909 err = ovs_vport_set_upcall_portids(vport, ids);
1910 if (err)
1911 goto exit_unlock_free;
1912 }
Jesse Grossccb13522011-10-25 19:26:31 -07001913
Jesse Grossa9341512013-03-26 15:48:38 -07001914 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1915 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1916 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001917
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001918 ovs_unlock();
Johannes Berg2a94fe42013-11-19 15:19:39 +01001919 ovs_notify(&dp_vport_genl_family, reply, info);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001920 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001921
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001922exit_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001923 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001924 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001925 return err;
1926}
1927
1928static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
1929{
1930 struct nlattr **a = info->attrs;
1931 struct sk_buff *reply;
1932 struct vport *vport;
1933 int err;
1934
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001935 reply = ovs_vport_cmd_alloc_info();
1936 if (!reply)
1937 return -ENOMEM;
1938
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001939 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001940 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
Jesse Grossccb13522011-10-25 19:26:31 -07001941 err = PTR_ERR(vport);
1942 if (IS_ERR(vport))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001943 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001944
1945 if (vport->port_no == OVSP_LOCAL) {
1946 err = -EINVAL;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001947 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001948 }
1949
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001950 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1951 info->snd_seq, 0, OVS_VPORT_CMD_DEL);
1952 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001953 ovs_dp_detach_port(vport);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001954 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001955
Johannes Berg2a94fe42013-11-19 15:19:39 +01001956 ovs_notify(&dp_vport_genl_family, reply, info);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001957 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001958
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001959exit_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001960 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001961 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001962 return err;
1963}
1964
1965static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
1966{
1967 struct nlattr **a = info->attrs;
1968 struct ovs_header *ovs_header = info->userhdr;
1969 struct sk_buff *reply;
1970 struct vport *vport;
1971 int err;
1972
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001973 reply = ovs_vport_cmd_alloc_info();
1974 if (!reply)
1975 return -ENOMEM;
1976
Jesse Grossccb13522011-10-25 19:26:31 -07001977 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001978 vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
Jesse Grossccb13522011-10-25 19:26:31 -07001979 err = PTR_ERR(vport);
1980 if (IS_ERR(vport))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001981 goto exit_unlock_free;
1982 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1983 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1984 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001985 rcu_read_unlock();
1986
1987 return genlmsg_reply(reply, info);
1988
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001989exit_unlock_free:
Jesse Grossccb13522011-10-25 19:26:31 -07001990 rcu_read_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001991 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001992 return err;
1993}
1994
1995static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1996{
1997 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1998 struct datapath *dp;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001999 int bucket = cb->args[0], skip = cb->args[1];
2000 int i, j = 0;
Jesse Grossccb13522011-10-25 19:26:31 -07002001
Jesse Grossccb13522011-10-25 19:26:31 -07002002 rcu_read_lock();
Andy Zhoucc3a5ae2014-09-08 13:14:22 -07002003 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
Jarno Rajahalme42ee19e2014-02-15 17:42:29 -08002004 if (!dp) {
2005 rcu_read_unlock();
2006 return -ENODEV;
2007 }
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07002008 for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
Jesse Grossccb13522011-10-25 19:26:31 -07002009 struct vport *vport;
2010
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07002011 j = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -08002012 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07002013 if (j >= skip &&
2014 ovs_vport_cmd_fill_info(vport, skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00002015 NETLINK_CB(cb->skb).portid,
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07002016 cb->nlh->nlmsg_seq,
2017 NLM_F_MULTI,
2018 OVS_VPORT_CMD_NEW) < 0)
2019 goto out;
Jesse Grossccb13522011-10-25 19:26:31 -07002020
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07002021 j++;
2022 }
2023 skip = 0;
Jesse Grossccb13522011-10-25 19:26:31 -07002024 }
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07002025out:
Jesse Grossccb13522011-10-25 19:26:31 -07002026 rcu_read_unlock();
2027
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07002028 cb->args[0] = i;
2029 cb->args[1] = j;
Jesse Grossccb13522011-10-25 19:26:31 -07002030
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07002031 return skb->len;
Jesse Grossccb13522011-10-25 19:26:31 -07002032}
2033
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002034static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
2035 [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
2036 [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
2037 [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
2038 [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
2039 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
2040 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
2041};
2042
stephen hemminger48e48a72014-07-16 11:25:52 -07002043static const struct genl_ops dp_vport_genl_ops[] = {
Jesse Grossccb13522011-10-25 19:26:31 -07002044 { .cmd = OVS_VPORT_CMD_NEW,
2045 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2046 .policy = vport_policy,
2047 .doit = ovs_vport_cmd_new
2048 },
2049 { .cmd = OVS_VPORT_CMD_DEL,
2050 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2051 .policy = vport_policy,
2052 .doit = ovs_vport_cmd_del
2053 },
2054 { .cmd = OVS_VPORT_CMD_GET,
2055 .flags = 0, /* OK for unprivileged users. */
2056 .policy = vport_policy,
2057 .doit = ovs_vport_cmd_get,
2058 .dumpit = ovs_vport_cmd_dump
2059 },
2060 { .cmd = OVS_VPORT_CMD_SET,
2061 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2062 .policy = vport_policy,
2063 .doit = ovs_vport_cmd_set,
2064 },
2065};
2066
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002067struct genl_family dp_vport_genl_family = {
2068 .id = GENL_ID_GENERATE,
2069 .hdrsize = sizeof(struct ovs_header),
2070 .name = OVS_VPORT_FAMILY,
2071 .version = OVS_VPORT_VERSION,
2072 .maxattr = OVS_VPORT_ATTR_MAX,
2073 .netnsok = true,
2074 .parallel_ops = true,
2075 .ops = dp_vport_genl_ops,
2076 .n_ops = ARRAY_SIZE(dp_vport_genl_ops),
2077 .mcgrps = &ovs_dp_vport_multicast_group,
2078 .n_mcgrps = 1,
Jesse Grossccb13522011-10-25 19:26:31 -07002079};
2080
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002081static struct genl_family * const dp_genl_families[] = {
2082 &dp_datapath_genl_family,
2083 &dp_vport_genl_family,
2084 &dp_flow_genl_family,
2085 &dp_packet_genl_family,
Jesse Grossccb13522011-10-25 19:26:31 -07002086};
2087
2088static void dp_unregister_genl(int n_families)
2089{
2090 int i;
2091
2092 for (i = 0; i < n_families; i++)
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002093 genl_unregister_family(dp_genl_families[i]);
Jesse Grossccb13522011-10-25 19:26:31 -07002094}
2095
2096static int dp_register_genl(void)
2097{
Jesse Grossccb13522011-10-25 19:26:31 -07002098 int err;
2099 int i;
2100
Jesse Grossccb13522011-10-25 19:26:31 -07002101 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
Jesse Grossccb13522011-10-25 19:26:31 -07002102
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002103 err = genl_register_family(dp_genl_families[i]);
Jesse Grossccb13522011-10-25 19:26:31 -07002104 if (err)
2105 goto error;
Jesse Grossccb13522011-10-25 19:26:31 -07002106 }
2107
2108 return 0;
2109
2110error:
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002111 dp_unregister_genl(i);
Jesse Grossccb13522011-10-25 19:26:31 -07002112 return err;
2113}
2114
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002115static int __net_init ovs_init_net(struct net *net)
2116{
2117 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2118
2119 INIT_LIST_HEAD(&ovs_net->dps);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002120 INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002121 return 0;
2122}
2123
2124static void __net_exit ovs_exit_net(struct net *net)
2125{
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002126 struct datapath *dp, *dp_next;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002127 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002128
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002129 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002130 list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
2131 __dp_destroy(dp);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002132 ovs_unlock();
2133
2134 cancel_work_sync(&ovs_net->dp_notify_work);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002135}
2136
2137static struct pernet_operations ovs_net_ops = {
2138 .init = ovs_init_net,
2139 .exit = ovs_exit_net,
2140 .id = &ovs_net_id,
2141 .size = sizeof(struct ovs_net),
2142};
2143
Jesse Grossccb13522011-10-25 19:26:31 -07002144static int __init dp_init(void)
2145{
Jesse Grossccb13522011-10-25 19:26:31 -07002146 int err;
2147
YOSHIFUJI Hideaki / 吉藤英明3523b292013-01-09 07:19:55 +00002148 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
Jesse Grossccb13522011-10-25 19:26:31 -07002149
2150 pr_info("Open vSwitch switching datapath\n");
2151
Andy Zhou971427f32014-09-15 19:37:25 -07002152 err = action_fifos_init();
Jesse Grossccb13522011-10-25 19:26:31 -07002153 if (err)
2154 goto error;
2155
Andy Zhou971427f32014-09-15 19:37:25 -07002156 err = ovs_internal_dev_rtnl_link_register();
2157 if (err)
2158 goto error_action_fifos_exit;
2159
Jiri Pirko5b9e7e12014-06-26 09:58:26 +02002160 err = ovs_flow_init();
2161 if (err)
2162 goto error_unreg_rtnl_link;
2163
Jesse Grossccb13522011-10-25 19:26:31 -07002164 err = ovs_vport_init();
2165 if (err)
2166 goto error_flow_exit;
2167
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002168 err = register_pernet_device(&ovs_net_ops);
Jesse Grossccb13522011-10-25 19:26:31 -07002169 if (err)
2170 goto error_vport_exit;
2171
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002172 err = register_netdevice_notifier(&ovs_dp_device_notifier);
2173 if (err)
2174 goto error_netns_exit;
2175
Thomas Graf62b9c8d2014-10-22 17:29:06 +02002176 err = ovs_netdev_init();
2177 if (err)
2178 goto error_unreg_notifier;
2179
Jesse Grossccb13522011-10-25 19:26:31 -07002180 err = dp_register_genl();
2181 if (err < 0)
Thomas Graf62b9c8d2014-10-22 17:29:06 +02002182 goto error_unreg_netdev;
Jesse Grossccb13522011-10-25 19:26:31 -07002183
Jesse Grossccb13522011-10-25 19:26:31 -07002184 return 0;
2185
Thomas Graf62b9c8d2014-10-22 17:29:06 +02002186error_unreg_netdev:
2187 ovs_netdev_exit();
Jesse Grossccb13522011-10-25 19:26:31 -07002188error_unreg_notifier:
2189 unregister_netdevice_notifier(&ovs_dp_device_notifier);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002190error_netns_exit:
2191 unregister_pernet_device(&ovs_net_ops);
Jesse Grossccb13522011-10-25 19:26:31 -07002192error_vport_exit:
2193 ovs_vport_exit();
2194error_flow_exit:
2195 ovs_flow_exit();
Jiri Pirko5b9e7e12014-06-26 09:58:26 +02002196error_unreg_rtnl_link:
2197 ovs_internal_dev_rtnl_link_unregister();
Andy Zhou971427f32014-09-15 19:37:25 -07002198error_action_fifos_exit:
2199 action_fifos_exit();
Jesse Grossccb13522011-10-25 19:26:31 -07002200error:
2201 return err;
2202}
2203
2204static void dp_cleanup(void)
2205{
Jesse Grossccb13522011-10-25 19:26:31 -07002206 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
Thomas Graf62b9c8d2014-10-22 17:29:06 +02002207 ovs_netdev_exit();
Jesse Grossccb13522011-10-25 19:26:31 -07002208 unregister_netdevice_notifier(&ovs_dp_device_notifier);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002209 unregister_pernet_device(&ovs_net_ops);
2210 rcu_barrier();
Jesse Grossccb13522011-10-25 19:26:31 -07002211 ovs_vport_exit();
2212 ovs_flow_exit();
Jiri Pirko5b9e7e12014-06-26 09:58:26 +02002213 ovs_internal_dev_rtnl_link_unregister();
Andy Zhou971427f32014-09-15 19:37:25 -07002214 action_fifos_exit();
Jesse Grossccb13522011-10-25 19:26:31 -07002215}
2216
2217module_init(dp_init);
2218module_exit(dp_cleanup);
2219
2220MODULE_DESCRIPTION("Open vSwitch switching datapath");
2221MODULE_LICENSE("GPL");