blob: 04a26ae4a4f638789c4e1e6c4024ed22182d5760 [file] [log] [blame]
Jesse Grossccb13522011-10-25 19:26:31 -07001/*
Ben Pfaffad552002014-05-06 16:48:38 -07002 * Copyright (c) 2007-2014 Nicira, Inc.
Jesse Grossccb13522011-10-25 19:26:31 -07003 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of version 2 of the GNU General Public
6 * License as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 * 02110-1301, USA
17 */
18
19#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20
21#include <linux/init.h>
22#include <linux/module.h>
23#include <linux/if_arp.h>
24#include <linux/if_vlan.h>
25#include <linux/in.h>
26#include <linux/ip.h>
27#include <linux/jhash.h>
28#include <linux/delay.h>
29#include <linux/time.h>
30#include <linux/etherdevice.h>
31#include <linux/genetlink.h>
32#include <linux/kernel.h>
33#include <linux/kthread.h>
34#include <linux/mutex.h>
35#include <linux/percpu.h>
36#include <linux/rcupdate.h>
37#include <linux/tcp.h>
38#include <linux/udp.h>
Jesse Grossccb13522011-10-25 19:26:31 -070039#include <linux/ethtool.h>
40#include <linux/wait.h>
Jesse Grossccb13522011-10-25 19:26:31 -070041#include <asm/div64.h>
42#include <linux/highmem.h>
43#include <linux/netfilter_bridge.h>
44#include <linux/netfilter_ipv4.h>
45#include <linux/inetdevice.h>
46#include <linux/list.h>
47#include <linux/openvswitch.h>
48#include <linux/rculist.h>
49#include <linux/dmi.h>
Jesse Grossccb13522011-10-25 19:26:31 -070050#include <net/genetlink.h>
Pravin B Shelar46df7b82012-02-22 19:58:59 -080051#include <net/net_namespace.h>
52#include <net/netns/generic.h>
Jesse Grossccb13522011-10-25 19:26:31 -070053
54#include "datapath.h"
55#include "flow.h"
Andy Zhoue80857c2014-01-21 09:31:04 -080056#include "flow_table.h"
Pravin B Shelare6445712013-10-03 18:16:47 -070057#include "flow_netlink.h"
Jesse Grossccb13522011-10-25 19:26:31 -070058#include "vport-internal_dev.h"
Thomas Grafcff63a52013-04-29 13:06:41 +000059#include "vport-netdev.h"
Jesse Grossccb13522011-10-25 19:26:31 -070060
Pravin B Shelar8e4e1712013-04-15 13:23:03 -070061int ovs_net_id __read_mostly;
Thomas Graf62b9c8d2014-10-22 17:29:06 +020062EXPORT_SYMBOL(ovs_net_id);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -070063
Pravin B Shelar0c200ef2014-05-06 16:44:50 -070064static struct genl_family dp_packet_genl_family;
65static struct genl_family dp_flow_genl_family;
66static struct genl_family dp_datapath_genl_family;
67
stephen hemminger48e48a72014-07-16 11:25:52 -070068static const struct genl_multicast_group ovs_dp_flow_multicast_group = {
69 .name = OVS_FLOW_MCGROUP,
Pravin B Shelar0c200ef2014-05-06 16:44:50 -070070};
71
stephen hemminger48e48a72014-07-16 11:25:52 -070072static const struct genl_multicast_group ovs_dp_datapath_multicast_group = {
73 .name = OVS_DATAPATH_MCGROUP,
Pravin B Shelar0c200ef2014-05-06 16:44:50 -070074};
75
stephen hemminger48e48a72014-07-16 11:25:52 -070076static const struct genl_multicast_group ovs_dp_vport_multicast_group = {
77 .name = OVS_VPORT_MCGROUP,
Pravin B Shelar0c200ef2014-05-06 16:44:50 -070078};
79
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -070080/* Check if need to build a reply message.
81 * OVS userspace sets the NLM_F_ECHO flag if it needs the reply. */
Samuel Gauthier9b67aa42014-09-18 10:31:04 +020082static bool ovs_must_notify(struct genl_family *family, struct genl_info *info,
83 unsigned int group)
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -070084{
85 return info->nlhdr->nlmsg_flags & NLM_F_ECHO ||
Samuel Gauthier9b67aa42014-09-18 10:31:04 +020086 genl_has_listeners(family, genl_info_net(info)->genl_sock,
87 group);
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -070088}
89
Johannes Berg68eb5502013-11-19 15:19:38 +010090static void ovs_notify(struct genl_family *family,
Johannes Berg2a94fe42013-11-19 15:19:39 +010091 struct sk_buff *skb, struct genl_info *info)
Thomas Grafed661182013-03-29 14:46:50 +010092{
Johannes Berg68eb5502013-11-19 15:19:38 +010093 genl_notify(family, skb, genl_info_net(info), info->snd_portid,
Johannes Berg2a94fe42013-11-19 15:19:39 +010094 0, info->nlhdr, GFP_KERNEL);
Thomas Grafed661182013-03-29 14:46:50 +010095}
96
Pravin B Shelar46df7b82012-02-22 19:58:59 -080097/**
Jesse Grossccb13522011-10-25 19:26:31 -070098 * DOC: Locking:
99 *
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700100 * All writes e.g. Writes to device state (add/remove datapath, port, set
101 * operations on vports, etc.), Writes to other state (flow table
102 * modifications, set miscellaneous datapath parameters, etc.) are protected
103 * by ovs_lock.
Jesse Grossccb13522011-10-25 19:26:31 -0700104 *
105 * Reads are protected by RCU.
106 *
107 * There are a few special cases (mostly stats) that have their own
108 * synchronization but they nest under all of above and don't interact with
109 * each other.
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700110 *
111 * The RTNL lock nests inside ovs_mutex.
Jesse Grossccb13522011-10-25 19:26:31 -0700112 */
113
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700114static DEFINE_MUTEX(ovs_mutex);
115
116void ovs_lock(void)
117{
118 mutex_lock(&ovs_mutex);
119}
120
121void ovs_unlock(void)
122{
123 mutex_unlock(&ovs_mutex);
124}
125
126#ifdef CONFIG_LOCKDEP
127int lockdep_ovsl_is_held(void)
128{
129 if (debug_locks)
130 return lockdep_is_held(&ovs_mutex);
131 else
132 return 1;
133}
David S. Miller2c6c49d2014-10-28 17:27:23 -0400134EXPORT_SYMBOL(lockdep_ovsl_is_held);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700135#endif
136
Jesse Grossccb13522011-10-25 19:26:31 -0700137static struct vport *new_vport(const struct vport_parms *);
Thomas Graf8055a892013-12-13 15:22:20 +0100138static int queue_gso_packets(struct datapath *dp, struct sk_buff *,
Jesse Grossccb13522011-10-25 19:26:31 -0700139 const struct dp_upcall_info *);
Thomas Graf8055a892013-12-13 15:22:20 +0100140static int queue_userspace_packet(struct datapath *dp, struct sk_buff *,
Jesse Grossccb13522011-10-25 19:26:31 -0700141 const struct dp_upcall_info *);
142
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700143/* Must be called with rcu_read_lock or ovs_mutex. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800144static struct datapath *get_dp(struct net *net, int dp_ifindex)
Jesse Grossccb13522011-10-25 19:26:31 -0700145{
146 struct datapath *dp = NULL;
147 struct net_device *dev;
148
149 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800150 dev = dev_get_by_index_rcu(net, dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -0700151 if (dev) {
152 struct vport *vport = ovs_internal_dev_get_vport(dev);
153 if (vport)
154 dp = vport->dp;
155 }
156 rcu_read_unlock();
157
158 return dp;
159}
160
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700161/* Must be called with rcu_read_lock or ovs_mutex. */
Andy Zhou971427f32014-09-15 19:37:25 -0700162const char *ovs_dp_name(const struct datapath *dp)
Jesse Grossccb13522011-10-25 19:26:31 -0700163{
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700164 struct vport *vport = ovs_vport_ovsl_rcu(dp, OVSP_LOCAL);
Jesse Grossccb13522011-10-25 19:26:31 -0700165 return vport->ops->get_name(vport);
166}
167
168static int get_dpifindex(struct datapath *dp)
169{
170 struct vport *local;
171 int ifindex;
172
173 rcu_read_lock();
174
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700175 local = ovs_vport_rcu(dp, OVSP_LOCAL);
Jesse Grossccb13522011-10-25 19:26:31 -0700176 if (local)
Thomas Grafcff63a52013-04-29 13:06:41 +0000177 ifindex = netdev_vport_priv(local)->dev->ifindex;
Jesse Grossccb13522011-10-25 19:26:31 -0700178 else
179 ifindex = 0;
180
181 rcu_read_unlock();
182
183 return ifindex;
184}
185
186static void destroy_dp_rcu(struct rcu_head *rcu)
187{
188 struct datapath *dp = container_of(rcu, struct datapath, rcu);
189
Pravin B Shelar9b996e52014-05-06 18:41:20 -0700190 ovs_flow_tbl_destroy(&dp->table);
Jesse Grossccb13522011-10-25 19:26:31 -0700191 free_percpu(dp->stats_percpu);
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800192 release_net(ovs_dp_get_net(dp));
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700193 kfree(dp->ports);
Jesse Grossccb13522011-10-25 19:26:31 -0700194 kfree(dp);
195}
196
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700197static struct hlist_head *vport_hash_bucket(const struct datapath *dp,
198 u16 port_no)
199{
200 return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)];
201}
202
Jarno Rajahalmebb6f9a72014-05-05 11:32:17 -0700203/* Called with ovs_mutex or RCU read lock. */
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700204struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no)
205{
206 struct vport *vport;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700207 struct hlist_head *head;
208
209 head = vport_hash_bucket(dp, port_no);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800210 hlist_for_each_entry_rcu(vport, head, dp_hash_node) {
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700211 if (vport->port_no == port_no)
212 return vport;
213 }
214 return NULL;
215}
216
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700217/* Called with ovs_mutex. */
Jesse Grossccb13522011-10-25 19:26:31 -0700218static struct vport *new_vport(const struct vport_parms *parms)
219{
220 struct vport *vport;
221
222 vport = ovs_vport_add(parms);
223 if (!IS_ERR(vport)) {
224 struct datapath *dp = parms->dp;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700225 struct hlist_head *head = vport_hash_bucket(dp, vport->port_no);
Jesse Grossccb13522011-10-25 19:26:31 -0700226
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700227 hlist_add_head_rcu(&vport->dp_hash_node, head);
Jesse Grossccb13522011-10-25 19:26:31 -0700228 }
Jesse Grossccb13522011-10-25 19:26:31 -0700229 return vport;
230}
231
Jesse Grossccb13522011-10-25 19:26:31 -0700232void ovs_dp_detach_port(struct vport *p)
233{
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700234 ASSERT_OVSL();
Jesse Grossccb13522011-10-25 19:26:31 -0700235
236 /* First drop references to device. */
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700237 hlist_del_rcu(&p->dp_hash_node);
Jesse Grossccb13522011-10-25 19:26:31 -0700238
239 /* Then destroy it. */
240 ovs_vport_del(p);
241}
242
243/* Must be called with rcu_read_lock. */
Pravin B Shelar8c8b1b82014-09-15 19:28:44 -0700244void ovs_dp_process_packet(struct sk_buff *skb, struct sw_flow_key *key)
Jesse Grossccb13522011-10-25 19:26:31 -0700245{
Pravin B Shelar83c8df22014-09-15 19:20:31 -0700246 const struct vport *p = OVS_CB(skb)->input_vport;
Jesse Grossccb13522011-10-25 19:26:31 -0700247 struct datapath *dp = p->dp;
248 struct sw_flow *flow;
249 struct dp_stats_percpu *stats;
Jesse Grossccb13522011-10-25 19:26:31 -0700250 u64 *stats_counter;
Andy Zhou1bd71162013-10-22 10:42:46 -0700251 u32 n_mask_hit;
Jesse Grossccb13522011-10-25 19:26:31 -0700252
Shan Wei404f2f12012-11-13 09:52:25 +0800253 stats = this_cpu_ptr(dp->stats_percpu);
Jesse Grossccb13522011-10-25 19:26:31 -0700254
Jesse Grossccb13522011-10-25 19:26:31 -0700255 /* Look up flow. */
Pravin B Shelar8c8b1b82014-09-15 19:28:44 -0700256 flow = ovs_flow_tbl_lookup_stats(&dp->table, key, &n_mask_hit);
Jesse Grossccb13522011-10-25 19:26:31 -0700257 if (unlikely(!flow)) {
258 struct dp_upcall_info upcall;
Pravin B Shelar8c8b1b82014-09-15 19:28:44 -0700259 int error;
Jesse Grossccb13522011-10-25 19:26:31 -0700260
261 upcall.cmd = OVS_PACKET_CMD_MISS;
Pravin B Shelar8c8b1b82014-09-15 19:28:44 -0700262 upcall.key = key;
Jesse Grossccb13522011-10-25 19:26:31 -0700263 upcall.userdata = NULL;
Alex Wang5cd667b2014-07-17 15:14:13 -0700264 upcall.portid = ovs_vport_find_upcall_portid(p, skb);
Li RongQingc5eba0b2014-09-03 17:43:45 +0800265 error = ovs_dp_upcall(dp, skb, &upcall);
266 if (unlikely(error))
267 kfree_skb(skb);
268 else
269 consume_skb(skb);
Jesse Grossccb13522011-10-25 19:26:31 -0700270 stats_counter = &stats->n_missed;
271 goto out;
272 }
273
274 OVS_CB(skb)->flow = flow;
275
Pravin B Shelar8c8b1b82014-09-15 19:28:44 -0700276 ovs_flow_stats_update(OVS_CB(skb)->flow, key->tp.flags, skb);
277 ovs_execute_actions(dp, skb, key);
Pravin B Shelare298e502013-10-29 17:22:21 -0700278 stats_counter = &stats->n_hit;
Jesse Grossccb13522011-10-25 19:26:31 -0700279
280out:
281 /* Update datapath statistics. */
WANG Congdf9d9fd2014-02-14 15:10:46 -0800282 u64_stats_update_begin(&stats->syncp);
Jesse Grossccb13522011-10-25 19:26:31 -0700283 (*stats_counter)++;
Andy Zhou1bd71162013-10-22 10:42:46 -0700284 stats->n_mask_hit += n_mask_hit;
WANG Congdf9d9fd2014-02-14 15:10:46 -0800285 u64_stats_update_end(&stats->syncp);
Jesse Grossccb13522011-10-25 19:26:31 -0700286}
287
Jesse Grossccb13522011-10-25 19:26:31 -0700288int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800289 const struct dp_upcall_info *upcall_info)
Jesse Grossccb13522011-10-25 19:26:31 -0700290{
291 struct dp_stats_percpu *stats;
Jesse Grossccb13522011-10-25 19:26:31 -0700292 int err;
293
Eric W. Biederman15e47302012-09-07 20:12:54 +0000294 if (upcall_info->portid == 0) {
Jesse Grossccb13522011-10-25 19:26:31 -0700295 err = -ENOTCONN;
296 goto err;
297 }
298
Jesse Grossccb13522011-10-25 19:26:31 -0700299 if (!skb_is_gso(skb))
Thomas Graf8055a892013-12-13 15:22:20 +0100300 err = queue_userspace_packet(dp, skb, upcall_info);
Jesse Grossccb13522011-10-25 19:26:31 -0700301 else
Thomas Graf8055a892013-12-13 15:22:20 +0100302 err = queue_gso_packets(dp, skb, upcall_info);
Jesse Grossccb13522011-10-25 19:26:31 -0700303 if (err)
304 goto err;
305
306 return 0;
307
308err:
Shan Wei404f2f12012-11-13 09:52:25 +0800309 stats = this_cpu_ptr(dp->stats_percpu);
Jesse Grossccb13522011-10-25 19:26:31 -0700310
WANG Congdf9d9fd2014-02-14 15:10:46 -0800311 u64_stats_update_begin(&stats->syncp);
Jesse Grossccb13522011-10-25 19:26:31 -0700312 stats->n_lost++;
WANG Congdf9d9fd2014-02-14 15:10:46 -0800313 u64_stats_update_end(&stats->syncp);
Jesse Grossccb13522011-10-25 19:26:31 -0700314
315 return err;
316}
317
Thomas Graf8055a892013-12-13 15:22:20 +0100318static int queue_gso_packets(struct datapath *dp, struct sk_buff *skb,
Jesse Grossccb13522011-10-25 19:26:31 -0700319 const struct dp_upcall_info *upcall_info)
320{
Ben Pfaffa1b5d0d2012-07-20 14:47:54 -0700321 unsigned short gso_type = skb_shinfo(skb)->gso_type;
Jesse Grossccb13522011-10-25 19:26:31 -0700322 struct dp_upcall_info later_info;
323 struct sw_flow_key later_key;
324 struct sk_buff *segs, *nskb;
325 int err;
326
Thomas Graf09c5e602013-12-13 15:22:22 +0100327 segs = __skb_gso_segment(skb, NETIF_F_SG, false);
Pravin B Shelar92e5dfc2012-07-20 14:46:29 -0700328 if (IS_ERR(segs))
329 return PTR_ERR(segs);
Florian Westphal330966e2014-10-20 13:49:17 +0200330 if (segs == NULL)
331 return -EINVAL;
Jesse Grossccb13522011-10-25 19:26:31 -0700332
333 /* Queue all of the segments. */
334 skb = segs;
335 do {
Thomas Graf8055a892013-12-13 15:22:20 +0100336 err = queue_userspace_packet(dp, skb, upcall_info);
Jesse Grossccb13522011-10-25 19:26:31 -0700337 if (err)
338 break;
339
Ben Pfaffa1b5d0d2012-07-20 14:47:54 -0700340 if (skb == segs && gso_type & SKB_GSO_UDP) {
Jesse Grossccb13522011-10-25 19:26:31 -0700341 /* The initial flow key extracted by ovs_flow_extract()
342 * in this case is for a first fragment, so we need to
343 * properly mark later fragments.
344 */
345 later_key = *upcall_info->key;
346 later_key.ip.frag = OVS_FRAG_TYPE_LATER;
347
348 later_info = *upcall_info;
349 later_info.key = &later_key;
350 upcall_info = &later_info;
351 }
352 } while ((skb = skb->next));
353
354 /* Free all of the segments. */
355 skb = segs;
356 do {
357 nskb = skb->next;
358 if (err)
359 kfree_skb(skb);
360 else
361 consume_skb(skb);
362 } while ((skb = nskb));
363 return err;
364}
365
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100366static size_t key_attr_size(void)
367{
368 return nla_total_size(4) /* OVS_KEY_ATTR_PRIORITY */
Pravin B Shelar7d5437c2013-06-17 17:50:18 -0700369 + nla_total_size(0) /* OVS_KEY_ATTR_TUNNEL */
370 + nla_total_size(8) /* OVS_TUNNEL_KEY_ATTR_ID */
371 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_SRC */
372 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_DST */
373 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TOS */
374 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TTL */
375 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT */
376 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_CSUM */
Jesse Gross67fa0342014-10-03 15:35:30 -0700377 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_OAM */
Jesse Grossf5796682014-10-03 15:35:33 -0700378 + nla_total_size(256) /* OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS */
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100379 + nla_total_size(4) /* OVS_KEY_ATTR_IN_PORT */
380 + nla_total_size(4) /* OVS_KEY_ATTR_SKB_MARK */
381 + nla_total_size(12) /* OVS_KEY_ATTR_ETHERNET */
382 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
383 + nla_total_size(4) /* OVS_KEY_ATTR_8021Q */
384 + nla_total_size(0) /* OVS_KEY_ATTR_ENCAP */
385 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
386 + nla_total_size(40) /* OVS_KEY_ATTR_IPV6 */
387 + nla_total_size(2) /* OVS_KEY_ATTR_ICMPV6 */
388 + nla_total_size(28); /* OVS_KEY_ATTR_ND */
389}
390
Thomas Grafbda56f12013-12-13 15:22:21 +0100391static size_t upcall_msg_size(const struct nlattr *userdata,
392 unsigned int hdrlen)
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100393{
394 size_t size = NLMSG_ALIGN(sizeof(struct ovs_header))
Thomas Grafbda56f12013-12-13 15:22:21 +0100395 + nla_total_size(hdrlen) /* OVS_PACKET_ATTR_PACKET */
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100396 + nla_total_size(key_attr_size()); /* OVS_PACKET_ATTR_KEY */
397
398 /* OVS_PACKET_ATTR_USERDATA */
399 if (userdata)
400 size += NLA_ALIGN(userdata->nla_len);
401
402 return size;
403}
404
Thomas Graf8055a892013-12-13 15:22:20 +0100405static int queue_userspace_packet(struct datapath *dp, struct sk_buff *skb,
Jesse Grossccb13522011-10-25 19:26:31 -0700406 const struct dp_upcall_info *upcall_info)
407{
408 struct ovs_header *upcall;
409 struct sk_buff *nskb = NULL;
Li RongQing4ee45ea2014-09-02 20:52:28 +0800410 struct sk_buff *user_skb = NULL; /* to be queued to userspace */
Jesse Grossccb13522011-10-25 19:26:31 -0700411 struct nlattr *nla;
Thomas Graf795449d2013-11-30 13:21:32 +0100412 struct genl_info info = {
Thomas Graf8055a892013-12-13 15:22:20 +0100413 .dst_sk = ovs_dp_get_net(dp)->genl_sock,
Thomas Graf795449d2013-11-30 13:21:32 +0100414 .snd_portid = upcall_info->portid,
415 };
416 size_t len;
Thomas Grafbda56f12013-12-13 15:22:21 +0100417 unsigned int hlen;
Thomas Graf8055a892013-12-13 15:22:20 +0100418 int err, dp_ifindex;
419
420 dp_ifindex = get_dpifindex(dp);
421 if (!dp_ifindex)
422 return -ENODEV;
Jesse Grossccb13522011-10-25 19:26:31 -0700423
424 if (vlan_tx_tag_present(skb)) {
425 nskb = skb_clone(skb, GFP_ATOMIC);
426 if (!nskb)
427 return -ENOMEM;
428
Patrick McHardy86a9bad2013-04-19 02:04:30 +0000429 nskb = __vlan_put_tag(nskb, nskb->vlan_proto, vlan_tx_tag_get(nskb));
Dan Carpenter8aa51d62012-05-13 08:44:18 +0000430 if (!nskb)
Jesse Grossccb13522011-10-25 19:26:31 -0700431 return -ENOMEM;
432
433 nskb->vlan_tci = 0;
434 skb = nskb;
435 }
436
437 if (nla_attr_size(skb->len) > USHRT_MAX) {
438 err = -EFBIG;
439 goto out;
440 }
441
Thomas Grafbda56f12013-12-13 15:22:21 +0100442 /* Complete checksum if needed */
443 if (skb->ip_summed == CHECKSUM_PARTIAL &&
444 (err = skb_checksum_help(skb)))
445 goto out;
446
447 /* Older versions of OVS user space enforce alignment of the last
448 * Netlink attribute to NLA_ALIGNTO which would require extensive
449 * padding logic. Only perform zerocopy if padding is not required.
450 */
451 if (dp->user_features & OVS_DP_F_UNALIGNED)
452 hlen = skb_zerocopy_headlen(skb);
453 else
454 hlen = skb->len;
455
456 len = upcall_msg_size(upcall_info->userdata, hlen);
Thomas Graf795449d2013-11-30 13:21:32 +0100457 user_skb = genlmsg_new_unicast(len, &info, GFP_ATOMIC);
Jesse Grossccb13522011-10-25 19:26:31 -0700458 if (!user_skb) {
459 err = -ENOMEM;
460 goto out;
461 }
462
463 upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
464 0, upcall_info->cmd);
465 upcall->dp_ifindex = dp_ifindex;
466
467 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
Andy Zhouf53e3832014-07-17 15:17:44 -0700468 err = ovs_nla_put_flow(upcall_info->key, upcall_info->key, user_skb);
469 BUG_ON(err);
Jesse Grossccb13522011-10-25 19:26:31 -0700470 nla_nest_end(user_skb, nla);
471
472 if (upcall_info->userdata)
Ben Pfaff44901082013-02-15 17:29:22 -0800473 __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA,
474 nla_len(upcall_info->userdata),
475 nla_data(upcall_info->userdata));
Jesse Grossccb13522011-10-25 19:26:31 -0700476
Thomas Grafbda56f12013-12-13 15:22:21 +0100477 /* Only reserve room for attribute header, packet data is added
478 * in skb_zerocopy() */
479 if (!(nla = nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, 0))) {
480 err = -ENOBUFS;
481 goto out;
482 }
483 nla->nla_len = nla_attr_size(skb->len);
Jesse Grossccb13522011-10-25 19:26:31 -0700484
Zoltan Kiss36d5fe62014-03-26 22:37:45 +0000485 err = skb_zerocopy(user_skb, skb, skb->len, hlen);
486 if (err)
487 goto out;
Jesse Grossccb13522011-10-25 19:26:31 -0700488
Thomas Grafaea0bb42014-01-14 16:27:49 +0000489 /* Pad OVS_PACKET_ATTR_PACKET if linear copy was performed */
490 if (!(dp->user_features & OVS_DP_F_UNALIGNED)) {
491 size_t plen = NLA_ALIGN(user_skb->len) - user_skb->len;
492
493 if (plen > 0)
494 memset(skb_put(user_skb, plen), 0, plen);
495 }
496
Thomas Grafbda56f12013-12-13 15:22:21 +0100497 ((struct nlmsghdr *) user_skb->data)->nlmsg_len = user_skb->len;
498
Thomas Graf8055a892013-12-13 15:22:20 +0100499 err = genlmsg_unicast(ovs_dp_get_net(dp), user_skb, upcall_info->portid);
Li RongQing4ee45ea2014-09-02 20:52:28 +0800500 user_skb = NULL;
Jesse Grossccb13522011-10-25 19:26:31 -0700501out:
Zoltan Kiss36d5fe62014-03-26 22:37:45 +0000502 if (err)
503 skb_tx_error(skb);
Li RongQing4ee45ea2014-09-02 20:52:28 +0800504 kfree_skb(user_skb);
Jesse Grossccb13522011-10-25 19:26:31 -0700505 kfree_skb(nskb);
506 return err;
507}
508
Jesse Grossccb13522011-10-25 19:26:31 -0700509static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
510{
511 struct ovs_header *ovs_header = info->userhdr;
512 struct nlattr **a = info->attrs;
513 struct sw_flow_actions *acts;
514 struct sk_buff *packet;
515 struct sw_flow *flow;
516 struct datapath *dp;
517 struct ethhdr *eth;
Pravin B Shelar83c8df22014-09-15 19:20:31 -0700518 struct vport *input_vport;
Jesse Grossccb13522011-10-25 19:26:31 -0700519 int len;
520 int err;
Jesse Grossccb13522011-10-25 19:26:31 -0700521
522 err = -EINVAL;
523 if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
Thomas Grafdded45fc2013-03-29 14:46:47 +0100524 !a[OVS_PACKET_ATTR_ACTIONS])
Jesse Grossccb13522011-10-25 19:26:31 -0700525 goto err;
526
527 len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
528 packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
529 err = -ENOMEM;
530 if (!packet)
531 goto err;
532 skb_reserve(packet, NET_IP_ALIGN);
533
Thomas Graf32686a92013-03-29 14:46:48 +0100534 nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
Jesse Grossccb13522011-10-25 19:26:31 -0700535
536 skb_reset_mac_header(packet);
537 eth = eth_hdr(packet);
538
539 /* Normally, setting the skb 'protocol' field would be handled by a
540 * call to eth_type_trans(), but it assumes there's a sending
541 * device, which we may not have. */
Simon Hormane5c5d222013-03-28 13:38:25 +0900542 if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN)
Jesse Grossccb13522011-10-25 19:26:31 -0700543 packet->protocol = eth->h_proto;
544 else
545 packet->protocol = htons(ETH_P_802_2);
546
547 /* Build an sw_flow for sending this packet. */
Jarno Rajahalme23dabf82014-03-27 12:35:23 -0700548 flow = ovs_flow_alloc();
Jesse Grossccb13522011-10-25 19:26:31 -0700549 err = PTR_ERR(flow);
550 if (IS_ERR(flow))
551 goto err_kfree_skb;
552
Pravin B Shelar83c8df22014-09-15 19:20:31 -0700553 err = ovs_flow_key_extract_userspace(a[OVS_PACKET_ATTR_KEY], packet,
554 &flow->key);
Jesse Grossccb13522011-10-25 19:26:31 -0700555 if (err)
556 goto err_flow_free;
557
Pravin B Shelare6445712013-10-03 18:16:47 -0700558 acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_PACKET_ATTR_ACTIONS]));
Jesse Grossccb13522011-10-25 19:26:31 -0700559 err = PTR_ERR(acts);
560 if (IS_ERR(acts))
561 goto err_flow_free;
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700562
Pravin B Shelare6445712013-10-03 18:16:47 -0700563 err = ovs_nla_copy_actions(a[OVS_PACKET_ATTR_ACTIONS],
Simon Horman25cd9ba2014-10-06 05:05:13 -0700564 &flow->key, &acts);
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700565 if (err)
566 goto err_flow_free;
Jesse Grossccb13522011-10-25 19:26:31 -0700567
Jesse Grossf5796682014-10-03 15:35:33 -0700568 rcu_assign_pointer(flow->sf_acts, acts);
569
570 OVS_CB(packet)->egress_tun_info = NULL;
Jesse Grossccb13522011-10-25 19:26:31 -0700571 OVS_CB(packet)->flow = flow;
572 packet->priority = flow->key.phy.priority;
Ansis Atteka39c7caeb2012-11-26 11:24:11 -0800573 packet->mark = flow->key.phy.skb_mark;
Jesse Grossccb13522011-10-25 19:26:31 -0700574
575 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800576 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -0700577 err = -ENODEV;
578 if (!dp)
579 goto err_unlock;
580
Pravin B Shelar83c8df22014-09-15 19:20:31 -0700581 input_vport = ovs_vport_rcu(dp, flow->key.phy.in_port);
582 if (!input_vport)
583 input_vport = ovs_vport_rcu(dp, OVSP_LOCAL);
584
585 if (!input_vport)
586 goto err_unlock;
587
588 OVS_CB(packet)->input_vport = input_vport;
589
Jesse Grossccb13522011-10-25 19:26:31 -0700590 local_bh_disable();
Pravin B Shelar2ff3e4e2014-09-15 19:15:28 -0700591 err = ovs_execute_actions(dp, packet, &flow->key);
Jesse Grossccb13522011-10-25 19:26:31 -0700592 local_bh_enable();
593 rcu_read_unlock();
594
Andy Zhou03f0d912013-08-07 20:01:00 -0700595 ovs_flow_free(flow, false);
Jesse Grossccb13522011-10-25 19:26:31 -0700596 return err;
597
598err_unlock:
599 rcu_read_unlock();
600err_flow_free:
Andy Zhou03f0d912013-08-07 20:01:00 -0700601 ovs_flow_free(flow, false);
Jesse Grossccb13522011-10-25 19:26:31 -0700602err_kfree_skb:
603 kfree_skb(packet);
604err:
605 return err;
606}
607
608static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
Thomas Grafdded45fc2013-03-29 14:46:47 +0100609 [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
Jesse Grossccb13522011-10-25 19:26:31 -0700610 [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
611 [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
612};
613
Johannes Berg4534de82013-11-14 17:14:46 +0100614static const struct genl_ops dp_packet_genl_ops[] = {
Jesse Grossccb13522011-10-25 19:26:31 -0700615 { .cmd = OVS_PACKET_CMD_EXECUTE,
616 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
617 .policy = packet_policy,
618 .doit = ovs_packet_cmd_execute
619 }
620};
621
Pravin B Shelar0c200ef2014-05-06 16:44:50 -0700622static struct genl_family dp_packet_genl_family = {
623 .id = GENL_ID_GENERATE,
624 .hdrsize = sizeof(struct ovs_header),
625 .name = OVS_PACKET_FAMILY,
626 .version = OVS_PACKET_VERSION,
627 .maxattr = OVS_PACKET_ATTR_MAX,
628 .netnsok = true,
629 .parallel_ops = true,
630 .ops = dp_packet_genl_ops,
631 .n_ops = ARRAY_SIZE(dp_packet_genl_ops),
632};
633
Andy Zhou1bd71162013-10-22 10:42:46 -0700634static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats,
635 struct ovs_dp_megaflow_stats *mega_stats)
Jesse Grossccb13522011-10-25 19:26:31 -0700636{
637 int i;
Jesse Grossccb13522011-10-25 19:26:31 -0700638
Andy Zhou1bd71162013-10-22 10:42:46 -0700639 memset(mega_stats, 0, sizeof(*mega_stats));
640
Pravin B Shelarb637e492013-10-04 00:14:23 -0700641 stats->n_flows = ovs_flow_tbl_count(&dp->table);
Andy Zhou1bd71162013-10-22 10:42:46 -0700642 mega_stats->n_masks = ovs_flow_tbl_num_masks(&dp->table);
Jesse Grossccb13522011-10-25 19:26:31 -0700643
644 stats->n_hit = stats->n_missed = stats->n_lost = 0;
Andy Zhou1bd71162013-10-22 10:42:46 -0700645
Jesse Grossccb13522011-10-25 19:26:31 -0700646 for_each_possible_cpu(i) {
647 const struct dp_stats_percpu *percpu_stats;
648 struct dp_stats_percpu local_stats;
649 unsigned int start;
650
651 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
652
653 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700654 start = u64_stats_fetch_begin_irq(&percpu_stats->syncp);
Jesse Grossccb13522011-10-25 19:26:31 -0700655 local_stats = *percpu_stats;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700656 } while (u64_stats_fetch_retry_irq(&percpu_stats->syncp, start));
Jesse Grossccb13522011-10-25 19:26:31 -0700657
658 stats->n_hit += local_stats.n_hit;
659 stats->n_missed += local_stats.n_missed;
660 stats->n_lost += local_stats.n_lost;
Andy Zhou1bd71162013-10-22 10:42:46 -0700661 mega_stats->n_mask_hit += local_stats.n_mask_hit;
Jesse Grossccb13522011-10-25 19:26:31 -0700662 }
663}
664
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100665static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts)
666{
667 return NLMSG_ALIGN(sizeof(struct ovs_header))
668 + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_KEY */
Andy Zhou03f0d912013-08-07 20:01:00 -0700669 + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_MASK */
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100670 + nla_total_size(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
671 + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
672 + nla_total_size(8) /* OVS_FLOW_ATTR_USED */
673 + nla_total_size(acts->actions_len); /* OVS_FLOW_ATTR_ACTIONS */
674}
675
Jarno Rajahalmebb6f9a72014-05-05 11:32:17 -0700676/* Called with ovs_mutex or RCU read lock. */
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700677static int ovs_flow_cmd_fill_info(const struct sw_flow *flow, int dp_ifindex,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000678 struct sk_buff *skb, u32 portid,
Jesse Grossccb13522011-10-25 19:26:31 -0700679 u32 seq, u32 flags, u8 cmd)
680{
681 const int skb_orig_len = skb->len;
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700682 struct nlattr *start;
Jesse Grossccb13522011-10-25 19:26:31 -0700683 struct ovs_flow_stats stats;
Pravin B Shelare298e502013-10-29 17:22:21 -0700684 __be16 tcp_flags;
685 unsigned long used;
Jesse Grossccb13522011-10-25 19:26:31 -0700686 struct ovs_header *ovs_header;
687 struct nlattr *nla;
Jesse Grossccb13522011-10-25 19:26:31 -0700688 int err;
689
Eric W. Biederman15e47302012-09-07 20:12:54 +0000690 ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family, flags, cmd);
Jesse Grossccb13522011-10-25 19:26:31 -0700691 if (!ovs_header)
692 return -EMSGSIZE;
693
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700694 ovs_header->dp_ifindex = dp_ifindex;
Jesse Grossccb13522011-10-25 19:26:31 -0700695
Andy Zhou03f0d912013-08-07 20:01:00 -0700696 /* Fill flow key. */
Jesse Grossccb13522011-10-25 19:26:31 -0700697 nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
698 if (!nla)
699 goto nla_put_failure;
Andy Zhou03f0d912013-08-07 20:01:00 -0700700
Pravin B Shelare6445712013-10-03 18:16:47 -0700701 err = ovs_nla_put_flow(&flow->unmasked_key, &flow->unmasked_key, skb);
Jesse Grossccb13522011-10-25 19:26:31 -0700702 if (err)
703 goto error;
704 nla_nest_end(skb, nla);
705
Andy Zhou03f0d912013-08-07 20:01:00 -0700706 nla = nla_nest_start(skb, OVS_FLOW_ATTR_MASK);
707 if (!nla)
708 goto nla_put_failure;
709
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)
712 goto error;
713
714 nla_nest_end(skb, nla);
715
Pravin B Shelare298e502013-10-29 17:22:21 -0700716 ovs_flow_stats_get(flow, &stats, &used, &tcp_flags);
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700717
David S. Miller028d6a62012-03-29 23:20:48 -0400718 if (used &&
719 nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
720 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -0700721
David S. Miller028d6a62012-03-29 23:20:48 -0400722 if (stats.n_packets &&
Pravin B Shelare298e502013-10-29 17:22:21 -0700723 nla_put(skb, OVS_FLOW_ATTR_STATS, sizeof(struct ovs_flow_stats), &stats))
David S. Miller028d6a62012-03-29 23:20:48 -0400724 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -0700725
Pravin B Shelare298e502013-10-29 17:22:21 -0700726 if ((u8)ntohs(tcp_flags) &&
727 nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, (u8)ntohs(tcp_flags)))
David S. Miller028d6a62012-03-29 23:20:48 -0400728 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -0700729
730 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
731 * this is the first flow to be dumped into 'skb'. This is unusual for
732 * Netlink but individual action lists can be longer than
733 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
734 * The userspace caller can always fetch the actions separately if it
735 * really wants them. (Most userspace callers in fact don't care.)
736 *
737 * This can only fail for dump operations because the skb is always
738 * properly sized for single flows.
739 */
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700740 start = nla_nest_start(skb, OVS_FLOW_ATTR_ACTIONS);
741 if (start) {
Pravin B Shelard57170b2013-07-30 15:39:39 -0700742 const struct sw_flow_actions *sf_acts;
743
Jesse Gross663efa32013-12-03 10:58:53 -0800744 sf_acts = rcu_dereference_ovsl(flow->sf_acts);
Pravin B Shelare6445712013-10-03 18:16:47 -0700745 err = ovs_nla_put_actions(sf_acts->actions,
746 sf_acts->actions_len, skb);
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700747
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700748 if (!err)
749 nla_nest_end(skb, start);
750 else {
751 if (skb_orig_len)
752 goto error;
753
754 nla_nest_cancel(skb, start);
755 }
756 } else if (skb_orig_len)
757 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -0700758
759 return genlmsg_end(skb, ovs_header);
760
761nla_put_failure:
762 err = -EMSGSIZE;
763error:
764 genlmsg_cancel(skb, ovs_header);
765 return err;
766}
767
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700768/* May not be called with RCU read lock. */
769static struct sk_buff *ovs_flow_cmd_alloc_info(const struct sw_flow_actions *acts,
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700770 struct genl_info *info,
771 bool always)
Jesse Grossccb13522011-10-25 19:26:31 -0700772{
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700773 struct sk_buff *skb;
Jesse Grossccb13522011-10-25 19:26:31 -0700774
Samuel Gauthier9b67aa42014-09-18 10:31:04 +0200775 if (!always && !ovs_must_notify(&dp_flow_genl_family, info, 0))
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700776 return NULL;
777
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700778 skb = genlmsg_new_unicast(ovs_flow_cmd_msg_size(acts), info, GFP_KERNEL);
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700779 if (!skb)
780 return ERR_PTR(-ENOMEM);
781
782 return skb;
Jesse Grossccb13522011-10-25 19:26:31 -0700783}
784
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700785/* Called with ovs_mutex. */
786static struct sk_buff *ovs_flow_cmd_build_info(const struct sw_flow *flow,
787 int dp_ifindex,
788 struct genl_info *info, u8 cmd,
789 bool always)
Jesse Grossccb13522011-10-25 19:26:31 -0700790{
791 struct sk_buff *skb;
792 int retval;
793
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700794 skb = ovs_flow_cmd_alloc_info(ovsl_dereference(flow->sf_acts), info,
795 always);
Himangi Saraogid0e992a2014-07-27 12:37:46 +0530796 if (IS_ERR_OR_NULL(skb))
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700797 return skb;
Jesse Grossccb13522011-10-25 19:26:31 -0700798
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700799 retval = ovs_flow_cmd_fill_info(flow, dp_ifindex, skb,
800 info->snd_portid, info->snd_seq, 0,
801 cmd);
Jesse Grossccb13522011-10-25 19:26:31 -0700802 BUG_ON(retval < 0);
803 return skb;
804}
805
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700806static int ovs_flow_cmd_new(struct sk_buff *skb, struct genl_info *info)
Jesse Grossccb13522011-10-25 19:26:31 -0700807{
808 struct nlattr **a = info->attrs;
809 struct ovs_header *ovs_header = info->userhdr;
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700810 struct sw_flow *flow, *new_flow;
Andy Zhou03f0d912013-08-07 20:01:00 -0700811 struct sw_flow_mask mask;
Jesse Grossccb13522011-10-25 19:26:31 -0700812 struct sk_buff *reply;
813 struct datapath *dp;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700814 struct sw_flow_actions *acts;
815 struct sw_flow_match match;
816 int error;
817
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700818 /* Must have key and actions. */
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700819 error = -EINVAL;
Jesse Gross426cda52014-10-06 05:08:38 -0700820 if (!a[OVS_FLOW_ATTR_KEY]) {
821 OVS_NLERR("Flow key attribute not present in new flow.\n");
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700822 goto error;
Jesse Gross426cda52014-10-06 05:08:38 -0700823 }
824 if (!a[OVS_FLOW_ATTR_ACTIONS]) {
825 OVS_NLERR("Flow actions attribute not present in new flow.\n");
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700826 goto error;
Jesse Gross426cda52014-10-06 05:08:38 -0700827 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700828
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700829 /* Most of the time we need to allocate a new flow, do it before
830 * locking.
831 */
832 new_flow = ovs_flow_alloc();
833 if (IS_ERR(new_flow)) {
834 error = PTR_ERR(new_flow);
835 goto error;
836 }
837
838 /* Extract key. */
839 ovs_match_init(&match, &new_flow->unmasked_key, &mask);
840 error = ovs_nla_get_match(&match,
841 a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
842 if (error)
843 goto err_kfree_flow;
844
845 ovs_flow_mask_key(&new_flow->key, &new_flow->unmasked_key, &mask);
846
847 /* Validate actions. */
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700848 acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_FLOW_ATTR_ACTIONS]));
849 error = PTR_ERR(acts);
850 if (IS_ERR(acts))
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700851 goto err_kfree_flow;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700852
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700853 error = ovs_nla_copy_actions(a[OVS_FLOW_ATTR_ACTIONS], &new_flow->key,
Simon Horman25cd9ba2014-10-06 05:05:13 -0700854 &acts);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700855 if (error) {
856 OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700857 goto err_kfree_acts;
858 }
859
860 reply = ovs_flow_cmd_alloc_info(acts, info, false);
861 if (IS_ERR(reply)) {
862 error = PTR_ERR(reply);
863 goto err_kfree_acts;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700864 }
865
866 ovs_lock();
867 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700868 if (unlikely(!dp)) {
869 error = -ENODEV;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700870 goto err_unlock_ovs;
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700871 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700872 /* Check if this is a duplicate flow */
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700873 flow = ovs_flow_tbl_lookup(&dp->table, &new_flow->unmasked_key);
874 if (likely(!flow)) {
875 rcu_assign_pointer(new_flow->sf_acts, acts);
876
877 /* Put flow in bucket. */
878 error = ovs_flow_tbl_insert(&dp->table, new_flow, &mask);
879 if (unlikely(error)) {
880 acts = NULL;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700881 goto err_unlock_ovs;
882 }
883
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700884 if (unlikely(reply)) {
885 error = ovs_flow_cmd_fill_info(new_flow,
886 ovs_header->dp_ifindex,
887 reply, info->snd_portid,
888 info->snd_seq, 0,
889 OVS_FLOW_CMD_NEW);
890 BUG_ON(error < 0);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700891 }
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700892 ovs_unlock();
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700893 } else {
894 struct sw_flow_actions *old_acts;
895
896 /* Bail out if we're not allowed to modify an existing flow.
897 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
898 * because Generic Netlink treats the latter as a dump
899 * request. We also accept NLM_F_EXCL in case that bug ever
900 * gets fixed.
901 */
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700902 if (unlikely(info->nlhdr->nlmsg_flags & (NLM_F_CREATE
903 | NLM_F_EXCL))) {
904 error = -EEXIST;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700905 goto err_unlock_ovs;
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700906 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700907 /* The unmasked key has to be the same for flow updates. */
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700908 if (unlikely(!ovs_flow_cmp_unmasked_key(flow, &match))) {
Alex Wang4a46b242014-06-30 20:30:29 -0700909 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
910 if (!flow) {
911 error = -ENOENT;
912 goto err_unlock_ovs;
913 }
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700914 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700915 /* Update actions. */
916 old_acts = ovsl_dereference(flow->sf_acts);
917 rcu_assign_pointer(flow->sf_acts, acts);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700918
919 if (unlikely(reply)) {
920 error = ovs_flow_cmd_fill_info(flow,
921 ovs_header->dp_ifindex,
922 reply, info->snd_portid,
923 info->snd_seq, 0,
924 OVS_FLOW_CMD_NEW);
925 BUG_ON(error < 0);
926 }
927 ovs_unlock();
928
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700929 ovs_nla_free_flow_actions(old_acts);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700930 ovs_flow_free(new_flow, false);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700931 }
932
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700933 if (reply)
934 ovs_notify(&dp_flow_genl_family, reply, info);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700935 return 0;
936
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700937err_unlock_ovs:
938 ovs_unlock();
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700939 kfree_skb(reply);
940err_kfree_acts:
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700941 kfree(acts);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700942err_kfree_flow:
943 ovs_flow_free(new_flow, false);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700944error:
945 return error;
946}
947
Jesse Gross6b205b22014-10-03 15:35:32 -0700948static struct sw_flow_actions *get_flow_actions(const struct nlattr *a,
949 const struct sw_flow_key *key,
950 const struct sw_flow_mask *mask)
951{
952 struct sw_flow_actions *acts;
953 struct sw_flow_key masked_key;
954 int error;
955
956 acts = ovs_nla_alloc_flow_actions(nla_len(a));
957 if (IS_ERR(acts))
958 return acts;
959
960 ovs_flow_mask_key(&masked_key, key, mask);
Simon Horman25cd9ba2014-10-06 05:05:13 -0700961 error = ovs_nla_copy_actions(a, &masked_key, &acts);
Jesse Gross6b205b22014-10-03 15:35:32 -0700962 if (error) {
963 OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
964 kfree(acts);
965 return ERR_PTR(error);
966 }
967
968 return acts;
969}
970
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700971static int ovs_flow_cmd_set(struct sk_buff *skb, struct genl_info *info)
972{
973 struct nlattr **a = info->attrs;
974 struct ovs_header *ovs_header = info->userhdr;
Jesse Gross6b205b22014-10-03 15:35:32 -0700975 struct sw_flow_key key;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700976 struct sw_flow *flow;
977 struct sw_flow_mask mask;
978 struct sk_buff *reply = NULL;
979 struct datapath *dp;
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700980 struct sw_flow_actions *old_acts = NULL, *acts = NULL;
Andy Zhou03f0d912013-08-07 20:01:00 -0700981 struct sw_flow_match match;
Jesse Grossccb13522011-10-25 19:26:31 -0700982 int error;
Jesse Grossccb13522011-10-25 19:26:31 -0700983
984 /* Extract key. */
985 error = -EINVAL;
Jesse Gross426cda52014-10-06 05:08:38 -0700986 if (!a[OVS_FLOW_ATTR_KEY]) {
987 OVS_NLERR("Flow key attribute not present in set flow.\n");
Jesse Grossccb13522011-10-25 19:26:31 -0700988 goto error;
Jesse Gross426cda52014-10-06 05:08:38 -0700989 }
Andy Zhou03f0d912013-08-07 20:01:00 -0700990
991 ovs_match_init(&match, &key, &mask);
Jarno Rajahalme23dabf82014-03-27 12:35:23 -0700992 error = ovs_nla_get_match(&match,
Pravin B Shelare6445712013-10-03 18:16:47 -0700993 a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
Jesse Grossccb13522011-10-25 19:26:31 -0700994 if (error)
995 goto error;
996
997 /* Validate actions. */
998 if (a[OVS_FLOW_ATTR_ACTIONS]) {
Jesse Gross6b205b22014-10-03 15:35:32 -0700999 acts = get_flow_actions(a[OVS_FLOW_ATTR_ACTIONS], &key, &mask);
1000 if (IS_ERR(acts)) {
1001 error = PTR_ERR(acts);
Jesse Grossccb13522011-10-25 19:26:31 -07001002 goto error;
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001003 }
1004 }
1005
1006 /* Can allocate before locking if have acts. */
1007 if (acts) {
1008 reply = ovs_flow_cmd_alloc_info(acts, info, false);
1009 if (IS_ERR(reply)) {
1010 error = PTR_ERR(reply);
1011 goto err_kfree_acts;
Andy Zhou03f0d912013-08-07 20:01:00 -07001012 }
Jesse Grossccb13522011-10-25 19:26:31 -07001013 }
1014
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001015 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001016 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001017 if (unlikely(!dp)) {
1018 error = -ENODEV;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001019 goto err_unlock_ovs;
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001020 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001021 /* Check that the flow exists. */
Alex Wang4a46b242014-06-30 20:30:29 -07001022 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001023 if (unlikely(!flow)) {
1024 error = -ENOENT;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001025 goto err_unlock_ovs;
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001026 }
Alex Wang4a46b242014-06-30 20:30:29 -07001027
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001028 /* Update actions, if present. */
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001029 if (likely(acts)) {
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001030 old_acts = ovsl_dereference(flow->sf_acts);
Jesse Grossccb13522011-10-25 19:26:31 -07001031 rcu_assign_pointer(flow->sf_acts, acts);
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001032
1033 if (unlikely(reply)) {
1034 error = ovs_flow_cmd_fill_info(flow,
1035 ovs_header->dp_ifindex,
1036 reply, info->snd_portid,
1037 info->snd_seq, 0,
1038 OVS_FLOW_CMD_NEW);
1039 BUG_ON(error < 0);
1040 }
1041 } else {
1042 /* Could not alloc without acts before locking. */
1043 reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex,
1044 info, OVS_FLOW_CMD_NEW, false);
1045 if (unlikely(IS_ERR(reply))) {
1046 error = PTR_ERR(reply);
1047 goto err_unlock_ovs;
1048 }
Jesse Grossccb13522011-10-25 19:26:31 -07001049 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001050
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001051 /* Clear stats. */
1052 if (a[OVS_FLOW_ATTR_CLEAR])
1053 ovs_flow_stats_clear(flow);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001054 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001055
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001056 if (reply)
1057 ovs_notify(&dp_flow_genl_family, reply, info);
1058 if (old_acts)
1059 ovs_nla_free_flow_actions(old_acts);
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -07001060
Jesse Grossccb13522011-10-25 19:26:31 -07001061 return 0;
1062
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001063err_unlock_ovs:
1064 ovs_unlock();
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001065 kfree_skb(reply);
1066err_kfree_acts:
Pravin B Shelar74f84a52013-06-17 17:50:12 -07001067 kfree(acts);
Jesse Grossccb13522011-10-25 19:26:31 -07001068error:
1069 return error;
1070}
1071
1072static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1073{
1074 struct nlattr **a = info->attrs;
1075 struct ovs_header *ovs_header = info->userhdr;
1076 struct sw_flow_key key;
1077 struct sk_buff *reply;
1078 struct sw_flow *flow;
1079 struct datapath *dp;
Andy Zhou03f0d912013-08-07 20:01:00 -07001080 struct sw_flow_match match;
Jesse Grossccb13522011-10-25 19:26:31 -07001081 int err;
Jesse Grossccb13522011-10-25 19:26:31 -07001082
Andy Zhou03f0d912013-08-07 20:01:00 -07001083 if (!a[OVS_FLOW_ATTR_KEY]) {
1084 OVS_NLERR("Flow get message rejected, Key attribute missing.\n");
Jesse Grossccb13522011-10-25 19:26:31 -07001085 return -EINVAL;
Andy Zhou03f0d912013-08-07 20:01:00 -07001086 }
1087
1088 ovs_match_init(&match, &key, NULL);
Jarno Rajahalme23dabf82014-03-27 12:35:23 -07001089 err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL);
Jesse Grossccb13522011-10-25 19:26:31 -07001090 if (err)
1091 return err;
1092
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001093 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001094 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001095 if (!dp) {
1096 err = -ENODEV;
1097 goto unlock;
1098 }
Jesse Grossccb13522011-10-25 19:26:31 -07001099
Alex Wang4a46b242014-06-30 20:30:29 -07001100 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1101 if (!flow) {
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001102 err = -ENOENT;
1103 goto unlock;
1104 }
Jesse Grossccb13522011-10-25 19:26:31 -07001105
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -07001106 reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex, info,
1107 OVS_FLOW_CMD_NEW, true);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001108 if (IS_ERR(reply)) {
1109 err = PTR_ERR(reply);
1110 goto unlock;
1111 }
Jesse Grossccb13522011-10-25 19:26:31 -07001112
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001113 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001114 return genlmsg_reply(reply, info);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001115unlock:
1116 ovs_unlock();
1117 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001118}
1119
1120static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1121{
1122 struct nlattr **a = info->attrs;
1123 struct ovs_header *ovs_header = info->userhdr;
1124 struct sw_flow_key key;
1125 struct sk_buff *reply;
1126 struct sw_flow *flow;
1127 struct datapath *dp;
Andy Zhou03f0d912013-08-07 20:01:00 -07001128 struct sw_flow_match match;
Jesse Grossccb13522011-10-25 19:26:31 -07001129 int err;
Jesse Grossccb13522011-10-25 19:26:31 -07001130
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001131 if (likely(a[OVS_FLOW_ATTR_KEY])) {
1132 ovs_match_init(&match, &key, NULL);
1133 err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL);
1134 if (unlikely(err))
1135 return err;
1136 }
1137
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001138 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001139 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001140 if (unlikely(!dp)) {
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001141 err = -ENODEV;
1142 goto unlock;
1143 }
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001144
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001145 if (unlikely(!a[OVS_FLOW_ATTR_KEY])) {
Pravin B Shelarb637e492013-10-04 00:14:23 -07001146 err = ovs_flow_tbl_flush(&dp->table);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001147 goto unlock;
1148 }
Andy Zhou03f0d912013-08-07 20:01:00 -07001149
Alex Wang4a46b242014-06-30 20:30:29 -07001150 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1151 if (unlikely(!flow)) {
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001152 err = -ENOENT;
1153 goto unlock;
1154 }
Jesse Grossccb13522011-10-25 19:26:31 -07001155
Pravin B Shelarb637e492013-10-04 00:14:23 -07001156 ovs_flow_tbl_remove(&dp->table, flow);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001157 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001158
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001159 reply = ovs_flow_cmd_alloc_info((const struct sw_flow_actions __force *) flow->sf_acts,
1160 info, false);
1161 if (likely(reply)) {
1162 if (likely(!IS_ERR(reply))) {
1163 rcu_read_lock(); /*To keep RCU checker happy. */
1164 err = ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex,
1165 reply, info->snd_portid,
1166 info->snd_seq, 0,
1167 OVS_FLOW_CMD_DEL);
1168 rcu_read_unlock();
1169 BUG_ON(err < 0);
1170
1171 ovs_notify(&dp_flow_genl_family, reply, info);
1172 } else {
1173 netlink_set_err(sock_net(skb->sk)->genl_sock, 0, 0, PTR_ERR(reply));
1174 }
1175 }
1176
1177 ovs_flow_free(flow, true);
Jesse Grossccb13522011-10-25 19:26:31 -07001178 return 0;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001179unlock:
1180 ovs_unlock();
1181 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001182}
1183
1184static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1185{
1186 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
Pravin B Shelarb637e492013-10-04 00:14:23 -07001187 struct table_instance *ti;
Jesse Grossccb13522011-10-25 19:26:31 -07001188 struct datapath *dp;
Jesse Grossccb13522011-10-25 19:26:31 -07001189
Pravin B Shelard57170b2013-07-30 15:39:39 -07001190 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001191 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001192 if (!dp) {
Pravin B Shelard57170b2013-07-30 15:39:39 -07001193 rcu_read_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001194 return -ENODEV;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001195 }
Jesse Grossccb13522011-10-25 19:26:31 -07001196
Pravin B Shelarb637e492013-10-04 00:14:23 -07001197 ti = rcu_dereference(dp->table.ti);
Jesse Grossccb13522011-10-25 19:26:31 -07001198 for (;;) {
1199 struct sw_flow *flow;
1200 u32 bucket, obj;
1201
1202 bucket = cb->args[0];
1203 obj = cb->args[1];
Pravin B Shelarb637e492013-10-04 00:14:23 -07001204 flow = ovs_flow_tbl_dump_next(ti, &bucket, &obj);
Jesse Grossccb13522011-10-25 19:26:31 -07001205 if (!flow)
1206 break;
1207
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -07001208 if (ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex, skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001209 NETLINK_CB(cb->skb).portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001210 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1211 OVS_FLOW_CMD_NEW) < 0)
1212 break;
1213
1214 cb->args[0] = bucket;
1215 cb->args[1] = obj;
1216 }
Pravin B Shelard57170b2013-07-30 15:39:39 -07001217 rcu_read_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001218 return skb->len;
1219}
1220
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001221static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
1222 [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
1223 [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
1224 [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
1225};
1226
stephen hemminger48e48a72014-07-16 11:25:52 -07001227static const struct genl_ops dp_flow_genl_ops[] = {
Jesse Grossccb13522011-10-25 19:26:31 -07001228 { .cmd = OVS_FLOW_CMD_NEW,
1229 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1230 .policy = flow_policy,
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001231 .doit = ovs_flow_cmd_new
Jesse Grossccb13522011-10-25 19:26:31 -07001232 },
1233 { .cmd = OVS_FLOW_CMD_DEL,
1234 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1235 .policy = flow_policy,
1236 .doit = ovs_flow_cmd_del
1237 },
1238 { .cmd = OVS_FLOW_CMD_GET,
1239 .flags = 0, /* OK for unprivileged users. */
1240 .policy = flow_policy,
1241 .doit = ovs_flow_cmd_get,
1242 .dumpit = ovs_flow_cmd_dump
1243 },
1244 { .cmd = OVS_FLOW_CMD_SET,
1245 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1246 .policy = flow_policy,
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001247 .doit = ovs_flow_cmd_set,
Jesse Grossccb13522011-10-25 19:26:31 -07001248 },
1249};
1250
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001251static struct genl_family dp_flow_genl_family = {
Jesse Grossccb13522011-10-25 19:26:31 -07001252 .id = GENL_ID_GENERATE,
1253 .hdrsize = sizeof(struct ovs_header),
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001254 .name = OVS_FLOW_FAMILY,
1255 .version = OVS_FLOW_VERSION,
1256 .maxattr = OVS_FLOW_ATTR_MAX,
Pravin B Shelar3a4e0d62013-04-23 07:48:48 +00001257 .netnsok = true,
1258 .parallel_ops = true,
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001259 .ops = dp_flow_genl_ops,
1260 .n_ops = ARRAY_SIZE(dp_flow_genl_ops),
1261 .mcgrps = &ovs_dp_flow_multicast_group,
1262 .n_mcgrps = 1,
Jesse Grossccb13522011-10-25 19:26:31 -07001263};
1264
Thomas Grafc3ff8cf2013-03-29 14:46:49 +01001265static size_t ovs_dp_cmd_msg_size(void)
1266{
1267 size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
1268
1269 msgsize += nla_total_size(IFNAMSIZ);
1270 msgsize += nla_total_size(sizeof(struct ovs_dp_stats));
Andy Zhou1bd71162013-10-22 10:42:46 -07001271 msgsize += nla_total_size(sizeof(struct ovs_dp_megaflow_stats));
Daniele Di Proietto45fb9c32014-01-23 10:47:35 -08001272 msgsize += nla_total_size(sizeof(u32)); /* OVS_DP_ATTR_USER_FEATURES */
Thomas Grafc3ff8cf2013-03-29 14:46:49 +01001273
1274 return msgsize;
1275}
1276
Jarno Rajahalmebb6f9a72014-05-05 11:32:17 -07001277/* Called with ovs_mutex or RCU read lock. */
Jesse Grossccb13522011-10-25 19:26:31 -07001278static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001279 u32 portid, u32 seq, u32 flags, u8 cmd)
Jesse Grossccb13522011-10-25 19:26:31 -07001280{
1281 struct ovs_header *ovs_header;
1282 struct ovs_dp_stats dp_stats;
Andy Zhou1bd71162013-10-22 10:42:46 -07001283 struct ovs_dp_megaflow_stats dp_megaflow_stats;
Jesse Grossccb13522011-10-25 19:26:31 -07001284 int err;
1285
Eric W. Biederman15e47302012-09-07 20:12:54 +00001286 ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
Jesse Grossccb13522011-10-25 19:26:31 -07001287 flags, cmd);
1288 if (!ovs_header)
1289 goto error;
1290
1291 ovs_header->dp_ifindex = get_dpifindex(dp);
1292
Jesse Grossccb13522011-10-25 19:26:31 -07001293 err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
Jesse Grossccb13522011-10-25 19:26:31 -07001294 if (err)
1295 goto nla_put_failure;
1296
Andy Zhou1bd71162013-10-22 10:42:46 -07001297 get_dp_stats(dp, &dp_stats, &dp_megaflow_stats);
1298 if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats),
1299 &dp_stats))
1300 goto nla_put_failure;
1301
1302 if (nla_put(skb, OVS_DP_ATTR_MEGAFLOW_STATS,
1303 sizeof(struct ovs_dp_megaflow_stats),
1304 &dp_megaflow_stats))
David S. Miller028d6a62012-03-29 23:20:48 -04001305 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001306
Thomas Graf43d4be92013-12-13 15:22:18 +01001307 if (nla_put_u32(skb, OVS_DP_ATTR_USER_FEATURES, dp->user_features))
1308 goto nla_put_failure;
1309
Jesse Grossccb13522011-10-25 19:26:31 -07001310 return genlmsg_end(skb, ovs_header);
1311
1312nla_put_failure:
1313 genlmsg_cancel(skb, ovs_header);
1314error:
1315 return -EMSGSIZE;
1316}
1317
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001318static struct sk_buff *ovs_dp_cmd_alloc_info(struct genl_info *info)
Jesse Grossccb13522011-10-25 19:26:31 -07001319{
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001320 return genlmsg_new_unicast(ovs_dp_cmd_msg_size(), info, GFP_KERNEL);
Jesse Grossccb13522011-10-25 19:26:31 -07001321}
1322
Jarno Rajahalmebb6f9a72014-05-05 11:32:17 -07001323/* Called with rcu_read_lock or ovs_mutex. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001324static struct datapath *lookup_datapath(struct net *net,
1325 struct ovs_header *ovs_header,
Jesse Grossccb13522011-10-25 19:26:31 -07001326 struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1327{
1328 struct datapath *dp;
1329
1330 if (!a[OVS_DP_ATTR_NAME])
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001331 dp = get_dp(net, ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001332 else {
1333 struct vport *vport;
1334
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001335 vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
Jesse Grossccb13522011-10-25 19:26:31 -07001336 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
Jesse Grossccb13522011-10-25 19:26:31 -07001337 }
1338 return dp ? dp : ERR_PTR(-ENODEV);
1339}
1340
Thomas Graf44da5ae2013-12-13 15:22:19 +01001341static void ovs_dp_reset_user_features(struct sk_buff *skb, struct genl_info *info)
1342{
1343 struct datapath *dp;
1344
1345 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
Jiri Pirko3c7eacf2014-02-14 11:42:36 +01001346 if (IS_ERR(dp))
Thomas Graf44da5ae2013-12-13 15:22:19 +01001347 return;
1348
1349 WARN(dp->user_features, "Dropping previously announced user features\n");
1350 dp->user_features = 0;
1351}
1352
Thomas Graf43d4be92013-12-13 15:22:18 +01001353static void ovs_dp_change(struct datapath *dp, struct nlattr **a)
1354{
1355 if (a[OVS_DP_ATTR_USER_FEATURES])
1356 dp->user_features = nla_get_u32(a[OVS_DP_ATTR_USER_FEATURES]);
1357}
1358
Jesse Grossccb13522011-10-25 19:26:31 -07001359static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1360{
1361 struct nlattr **a = info->attrs;
1362 struct vport_parms parms;
1363 struct sk_buff *reply;
1364 struct datapath *dp;
1365 struct vport *vport;
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001366 struct ovs_net *ovs_net;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001367 int err, i;
Jesse Grossccb13522011-10-25 19:26:31 -07001368
1369 err = -EINVAL;
1370 if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1371 goto err;
1372
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001373 reply = ovs_dp_cmd_alloc_info(info);
1374 if (!reply)
1375 return -ENOMEM;
Jesse Grossccb13522011-10-25 19:26:31 -07001376
1377 err = -ENOMEM;
1378 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1379 if (dp == NULL)
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001380 goto err_free_reply;
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001381
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001382 ovs_dp_set_net(dp, hold_net(sock_net(skb->sk)));
Jesse Grossccb13522011-10-25 19:26:31 -07001383
1384 /* Allocate table. */
Pravin B Shelarb637e492013-10-04 00:14:23 -07001385 err = ovs_flow_tbl_init(&dp->table);
1386 if (err)
Jesse Grossccb13522011-10-25 19:26:31 -07001387 goto err_free_dp;
1388
WANG Cong1c213bd2014-02-13 11:46:28 -08001389 dp->stats_percpu = netdev_alloc_pcpu_stats(struct dp_stats_percpu);
Jesse Grossccb13522011-10-25 19:26:31 -07001390 if (!dp->stats_percpu) {
1391 err = -ENOMEM;
1392 goto err_destroy_table;
1393 }
1394
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001395 dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
Pravin B Shelare6445712013-10-03 18:16:47 -07001396 GFP_KERNEL);
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001397 if (!dp->ports) {
1398 err = -ENOMEM;
1399 goto err_destroy_percpu;
1400 }
1401
1402 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1403 INIT_HLIST_HEAD(&dp->ports[i]);
1404
Jesse Grossccb13522011-10-25 19:26:31 -07001405 /* Set up our datapath device. */
1406 parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1407 parms.type = OVS_VPORT_TYPE_INTERNAL;
1408 parms.options = NULL;
1409 parms.dp = dp;
1410 parms.port_no = OVSP_LOCAL;
Alex Wang5cd667b2014-07-17 15:14:13 -07001411 parms.upcall_portids = a[OVS_DP_ATTR_UPCALL_PID];
Jesse Grossccb13522011-10-25 19:26:31 -07001412
Thomas Graf43d4be92013-12-13 15:22:18 +01001413 ovs_dp_change(dp, a);
1414
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001415 /* So far only local changes have been made, now need the lock. */
1416 ovs_lock();
1417
Jesse Grossccb13522011-10-25 19:26:31 -07001418 vport = new_vport(&parms);
1419 if (IS_ERR(vport)) {
1420 err = PTR_ERR(vport);
1421 if (err == -EBUSY)
1422 err = -EEXIST;
1423
Thomas Graf44da5ae2013-12-13 15:22:19 +01001424 if (err == -EEXIST) {
1425 /* An outdated user space instance that does not understand
1426 * the concept of user_features has attempted to create a new
1427 * datapath and is likely to reuse it. Drop all user features.
1428 */
1429 if (info->genlhdr->version < OVS_DP_VER_FEATURES)
1430 ovs_dp_reset_user_features(skb, info);
1431 }
1432
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001433 goto err_destroy_ports_array;
Jesse Grossccb13522011-10-25 19:26:31 -07001434 }
1435
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001436 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1437 info->snd_seq, 0, OVS_DP_CMD_NEW);
1438 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001439
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001440 ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001441 list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001442
1443 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001444
Johannes Berg2a94fe42013-11-19 15:19:39 +01001445 ovs_notify(&dp_datapath_genl_family, reply, info);
Jesse Grossccb13522011-10-25 19:26:31 -07001446 return 0;
1447
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001448err_destroy_ports_array:
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001449 ovs_unlock();
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001450 kfree(dp->ports);
Jesse Grossccb13522011-10-25 19:26:31 -07001451err_destroy_percpu:
1452 free_percpu(dp->stats_percpu);
1453err_destroy_table:
Pravin B Shelar9b996e52014-05-06 18:41:20 -07001454 ovs_flow_tbl_destroy(&dp->table);
Jesse Grossccb13522011-10-25 19:26:31 -07001455err_free_dp:
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001456 release_net(ovs_dp_get_net(dp));
Jesse Grossccb13522011-10-25 19:26:31 -07001457 kfree(dp);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001458err_free_reply:
1459 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001460err:
1461 return err;
1462}
1463
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001464/* Called with ovs_mutex. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001465static void __dp_destroy(struct datapath *dp)
Jesse Grossccb13522011-10-25 19:26:31 -07001466{
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001467 int i;
Jesse Grossccb13522011-10-25 19:26:31 -07001468
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001469 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1470 struct vport *vport;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001471 struct hlist_node *n;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001472
Sasha Levinb67bfe02013-02-27 17:06:00 -08001473 hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001474 if (vport->port_no != OVSP_LOCAL)
1475 ovs_dp_detach_port(vport);
1476 }
Jesse Grossccb13522011-10-25 19:26:31 -07001477
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001478 list_del_rcu(&dp->list_node);
Jesse Grossccb13522011-10-25 19:26:31 -07001479
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001480 /* OVSP_LOCAL is datapath internal port. We need to make sure that
Andy Zhoue80857c2014-01-21 09:31:04 -08001481 * all ports in datapath are destroyed first before freeing datapath.
Jesse Grossccb13522011-10-25 19:26:31 -07001482 */
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001483 ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
Jesse Grossccb13522011-10-25 19:26:31 -07001484
Andy Zhoue80857c2014-01-21 09:31:04 -08001485 /* RCU destroy the flow table */
Jesse Grossccb13522011-10-25 19:26:31 -07001486 call_rcu(&dp->rcu, destroy_dp_rcu);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001487}
1488
1489static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1490{
1491 struct sk_buff *reply;
1492 struct datapath *dp;
1493 int err;
1494
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001495 reply = ovs_dp_cmd_alloc_info(info);
1496 if (!reply)
1497 return -ENOMEM;
1498
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001499 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001500 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1501 err = PTR_ERR(dp);
1502 if (IS_ERR(dp))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001503 goto err_unlock_free;
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001504
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001505 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1506 info->snd_seq, 0, OVS_DP_CMD_DEL);
1507 BUG_ON(err < 0);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001508
1509 __dp_destroy(dp);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001510 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001511
Johannes Berg2a94fe42013-11-19 15:19:39 +01001512 ovs_notify(&dp_datapath_genl_family, reply, info);
Jesse Grossccb13522011-10-25 19:26:31 -07001513
1514 return 0;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001515
1516err_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001517 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001518 kfree_skb(reply);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001519 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001520}
1521
1522static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1523{
1524 struct sk_buff *reply;
1525 struct datapath *dp;
1526 int err;
1527
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001528 reply = ovs_dp_cmd_alloc_info(info);
1529 if (!reply)
1530 return -ENOMEM;
1531
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001532 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001533 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001534 err = PTR_ERR(dp);
Jesse Grossccb13522011-10-25 19:26:31 -07001535 if (IS_ERR(dp))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001536 goto err_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001537
Thomas Graf43d4be92013-12-13 15:22:18 +01001538 ovs_dp_change(dp, info->attrs);
1539
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001540 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1541 info->snd_seq, 0, OVS_DP_CMD_NEW);
1542 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001543
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001544 ovs_unlock();
Johannes Berg2a94fe42013-11-19 15:19:39 +01001545 ovs_notify(&dp_datapath_genl_family, reply, info);
Jesse Grossccb13522011-10-25 19:26:31 -07001546
1547 return 0;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001548
1549err_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001550 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001551 kfree_skb(reply);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001552 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001553}
1554
1555static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1556{
1557 struct sk_buff *reply;
1558 struct datapath *dp;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001559 int err;
Jesse Grossccb13522011-10-25 19:26:31 -07001560
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001561 reply = ovs_dp_cmd_alloc_info(info);
1562 if (!reply)
1563 return -ENOMEM;
1564
1565 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001566 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001567 if (IS_ERR(dp)) {
1568 err = PTR_ERR(dp);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001569 goto err_unlock_free;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001570 }
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001571 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1572 info->snd_seq, 0, OVS_DP_CMD_NEW);
1573 BUG_ON(err < 0);
1574 rcu_read_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001575
Jesse Grossccb13522011-10-25 19:26:31 -07001576 return genlmsg_reply(reply, info);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001577
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001578err_unlock_free:
1579 rcu_read_unlock();
1580 kfree_skb(reply);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001581 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001582}
1583
1584static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1585{
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001586 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
Jesse Grossccb13522011-10-25 19:26:31 -07001587 struct datapath *dp;
1588 int skip = cb->args[0];
1589 int i = 0;
1590
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001591 rcu_read_lock();
1592 list_for_each_entry_rcu(dp, &ovs_net->dps, list_node) {
Ben Pfaff77676fd2012-01-17 13:33:39 +00001593 if (i >= skip &&
Eric W. Biederman15e47302012-09-07 20:12:54 +00001594 ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001595 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1596 OVS_DP_CMD_NEW) < 0)
1597 break;
1598 i++;
1599 }
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001600 rcu_read_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001601
1602 cb->args[0] = i;
1603
1604 return skb->len;
1605}
1606
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001607static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1608 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1609 [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1610 [OVS_DP_ATTR_USER_FEATURES] = { .type = NLA_U32 },
1611};
1612
stephen hemminger48e48a72014-07-16 11:25:52 -07001613static const struct genl_ops dp_datapath_genl_ops[] = {
Jesse Grossccb13522011-10-25 19:26:31 -07001614 { .cmd = OVS_DP_CMD_NEW,
1615 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1616 .policy = datapath_policy,
1617 .doit = ovs_dp_cmd_new
1618 },
1619 { .cmd = OVS_DP_CMD_DEL,
1620 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1621 .policy = datapath_policy,
1622 .doit = ovs_dp_cmd_del
1623 },
1624 { .cmd = OVS_DP_CMD_GET,
1625 .flags = 0, /* OK for unprivileged users. */
1626 .policy = datapath_policy,
1627 .doit = ovs_dp_cmd_get,
1628 .dumpit = ovs_dp_cmd_dump
1629 },
1630 { .cmd = OVS_DP_CMD_SET,
1631 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1632 .policy = datapath_policy,
1633 .doit = ovs_dp_cmd_set,
1634 },
1635};
1636
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001637static struct genl_family dp_datapath_genl_family = {
Jesse Grossccb13522011-10-25 19:26:31 -07001638 .id = GENL_ID_GENERATE,
1639 .hdrsize = sizeof(struct ovs_header),
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001640 .name = OVS_DATAPATH_FAMILY,
1641 .version = OVS_DATAPATH_VERSION,
1642 .maxattr = OVS_DP_ATTR_MAX,
Pravin B Shelar3a4e0d62013-04-23 07:48:48 +00001643 .netnsok = true,
1644 .parallel_ops = true,
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001645 .ops = dp_datapath_genl_ops,
1646 .n_ops = ARRAY_SIZE(dp_datapath_genl_ops),
1647 .mcgrps = &ovs_dp_datapath_multicast_group,
1648 .n_mcgrps = 1,
Jesse Grossccb13522011-10-25 19:26:31 -07001649};
1650
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001651/* Called with ovs_mutex or RCU read lock. */
Jesse Grossccb13522011-10-25 19:26:31 -07001652static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001653 u32 portid, u32 seq, u32 flags, u8 cmd)
Jesse Grossccb13522011-10-25 19:26:31 -07001654{
1655 struct ovs_header *ovs_header;
1656 struct ovs_vport_stats vport_stats;
1657 int err;
1658
Eric W. Biederman15e47302012-09-07 20:12:54 +00001659 ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
Jesse Grossccb13522011-10-25 19:26:31 -07001660 flags, cmd);
1661 if (!ovs_header)
1662 return -EMSGSIZE;
1663
1664 ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1665
David S. Miller028d6a62012-03-29 23:20:48 -04001666 if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
1667 nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
Alex Wang5cd667b2014-07-17 15:14:13 -07001668 nla_put_string(skb, OVS_VPORT_ATTR_NAME,
1669 vport->ops->get_name(vport)))
David S. Miller028d6a62012-03-29 23:20:48 -04001670 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001671
1672 ovs_vport_get_stats(vport, &vport_stats);
David S. Miller028d6a62012-03-29 23:20:48 -04001673 if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
1674 &vport_stats))
1675 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001676
Alex Wang5cd667b2014-07-17 15:14:13 -07001677 if (ovs_vport_get_upcall_portids(vport, skb))
1678 goto nla_put_failure;
1679
Jesse Grossccb13522011-10-25 19:26:31 -07001680 err = ovs_vport_get_options(vport, skb);
1681 if (err == -EMSGSIZE)
1682 goto error;
1683
1684 return genlmsg_end(skb, ovs_header);
1685
1686nla_put_failure:
1687 err = -EMSGSIZE;
1688error:
1689 genlmsg_cancel(skb, ovs_header);
1690 return err;
1691}
1692
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001693static struct sk_buff *ovs_vport_cmd_alloc_info(void)
1694{
1695 return nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1696}
1697
1698/* Called with ovs_mutex, only via ovs_dp_notify_wq(). */
Eric W. Biederman15e47302012-09-07 20:12:54 +00001699struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001700 u32 seq, u8 cmd)
1701{
1702 struct sk_buff *skb;
1703 int retval;
1704
1705 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1706 if (!skb)
1707 return ERR_PTR(-ENOMEM);
1708
Eric W. Biederman15e47302012-09-07 20:12:54 +00001709 retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
Jesse Grossa9341512013-03-26 15:48:38 -07001710 BUG_ON(retval < 0);
1711
Jesse Grossccb13522011-10-25 19:26:31 -07001712 return skb;
1713}
1714
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001715/* Called with ovs_mutex or RCU read lock. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001716static struct vport *lookup_vport(struct net *net,
1717 struct ovs_header *ovs_header,
Jesse Grossccb13522011-10-25 19:26:31 -07001718 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1719{
1720 struct datapath *dp;
1721 struct vport *vport;
1722
1723 if (a[OVS_VPORT_ATTR_NAME]) {
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001724 vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
Jesse Grossccb13522011-10-25 19:26:31 -07001725 if (!vport)
1726 return ERR_PTR(-ENODEV);
Ben Pfaff651a68e2012-03-06 15:04:04 -08001727 if (ovs_header->dp_ifindex &&
1728 ovs_header->dp_ifindex != get_dpifindex(vport->dp))
1729 return ERR_PTR(-ENODEV);
Jesse Grossccb13522011-10-25 19:26:31 -07001730 return vport;
1731 } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
1732 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1733
1734 if (port_no >= DP_MAX_PORTS)
1735 return ERR_PTR(-EFBIG);
1736
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001737 dp = get_dp(net, ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001738 if (!dp)
1739 return ERR_PTR(-ENODEV);
1740
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001741 vport = ovs_vport_ovsl_rcu(dp, port_no);
Jesse Grossccb13522011-10-25 19:26:31 -07001742 if (!vport)
Jarno Rajahalme14408db2013-01-09 14:27:35 -08001743 return ERR_PTR(-ENODEV);
Jesse Grossccb13522011-10-25 19:26:31 -07001744 return vport;
1745 } else
1746 return ERR_PTR(-EINVAL);
1747}
1748
1749static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
1750{
1751 struct nlattr **a = info->attrs;
1752 struct ovs_header *ovs_header = info->userhdr;
1753 struct vport_parms parms;
1754 struct sk_buff *reply;
1755 struct vport *vport;
1756 struct datapath *dp;
1757 u32 port_no;
1758 int err;
1759
Jesse Grossccb13522011-10-25 19:26:31 -07001760 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
1761 !a[OVS_VPORT_ATTR_UPCALL_PID])
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001762 return -EINVAL;
1763
1764 port_no = a[OVS_VPORT_ATTR_PORT_NO]
1765 ? nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]) : 0;
1766 if (port_no >= DP_MAX_PORTS)
1767 return -EFBIG;
1768
1769 reply = ovs_vport_cmd_alloc_info();
1770 if (!reply)
1771 return -ENOMEM;
Jesse Grossccb13522011-10-25 19:26:31 -07001772
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001773 ovs_lock();
Thomas Graf62b9c8d2014-10-22 17:29:06 +02001774restart:
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001775 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001776 err = -ENODEV;
1777 if (!dp)
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001778 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001779
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001780 if (port_no) {
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001781 vport = ovs_vport_ovsl(dp, port_no);
Jesse Grossccb13522011-10-25 19:26:31 -07001782 err = -EBUSY;
1783 if (vport)
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001784 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001785 } else {
1786 for (port_no = 1; ; port_no++) {
1787 if (port_no >= DP_MAX_PORTS) {
1788 err = -EFBIG;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001789 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001790 }
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001791 vport = ovs_vport_ovsl(dp, port_no);
Jesse Grossccb13522011-10-25 19:26:31 -07001792 if (!vport)
1793 break;
1794 }
1795 }
1796
1797 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
1798 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1799 parms.options = a[OVS_VPORT_ATTR_OPTIONS];
1800 parms.dp = dp;
1801 parms.port_no = port_no;
Alex Wang5cd667b2014-07-17 15:14:13 -07001802 parms.upcall_portids = a[OVS_VPORT_ATTR_UPCALL_PID];
Jesse Grossccb13522011-10-25 19:26:31 -07001803
1804 vport = new_vport(&parms);
1805 err = PTR_ERR(vport);
Thomas Graf62b9c8d2014-10-22 17:29:06 +02001806 if (IS_ERR(vport)) {
1807 if (err == -EAGAIN)
1808 goto restart;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001809 goto exit_unlock_free;
Thomas Graf62b9c8d2014-10-22 17:29:06 +02001810 }
Jesse Grossccb13522011-10-25 19:26:31 -07001811
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001812 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1813 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1814 BUG_ON(err < 0);
1815 ovs_unlock();
Thomas Grafed661182013-03-29 14:46:50 +01001816
Johannes Berg2a94fe42013-11-19 15:19:39 +01001817 ovs_notify(&dp_vport_genl_family, reply, info);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001818 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001819
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001820exit_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001821 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001822 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001823 return err;
1824}
1825
1826static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
1827{
1828 struct nlattr **a = info->attrs;
1829 struct sk_buff *reply;
1830 struct vport *vport;
1831 int err;
1832
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001833 reply = ovs_vport_cmd_alloc_info();
1834 if (!reply)
1835 return -ENOMEM;
1836
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001837 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001838 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
Jesse Grossccb13522011-10-25 19:26:31 -07001839 err = PTR_ERR(vport);
1840 if (IS_ERR(vport))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001841 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001842
Jesse Grossccb13522011-10-25 19:26:31 -07001843 if (a[OVS_VPORT_ATTR_TYPE] &&
Jesse Grossf44f3402013-05-13 08:15:26 -07001844 nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
Jesse Grossccb13522011-10-25 19:26:31 -07001845 err = -EINVAL;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001846 goto exit_unlock_free;
Jesse Grossa9341512013-03-26 15:48:38 -07001847 }
1848
Jesse Grossf44f3402013-05-13 08:15:26 -07001849 if (a[OVS_VPORT_ATTR_OPTIONS]) {
Jesse Grossccb13522011-10-25 19:26:31 -07001850 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
Jesse Grossf44f3402013-05-13 08:15:26 -07001851 if (err)
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001852 goto exit_unlock_free;
Jesse Grossf44f3402013-05-13 08:15:26 -07001853 }
Jesse Grossa9341512013-03-26 15:48:38 -07001854
Alex Wang5cd667b2014-07-17 15:14:13 -07001855
1856 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
1857 struct nlattr *ids = a[OVS_VPORT_ATTR_UPCALL_PID];
1858
1859 err = ovs_vport_set_upcall_portids(vport, ids);
1860 if (err)
1861 goto exit_unlock_free;
1862 }
Jesse Grossccb13522011-10-25 19:26:31 -07001863
Jesse Grossa9341512013-03-26 15:48:38 -07001864 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1865 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1866 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001867
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001868 ovs_unlock();
Johannes Berg2a94fe42013-11-19 15:19:39 +01001869 ovs_notify(&dp_vport_genl_family, reply, info);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001870 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001871
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001872exit_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001873 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001874 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001875 return err;
1876}
1877
1878static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
1879{
1880 struct nlattr **a = info->attrs;
1881 struct sk_buff *reply;
1882 struct vport *vport;
1883 int err;
1884
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001885 reply = ovs_vport_cmd_alloc_info();
1886 if (!reply)
1887 return -ENOMEM;
1888
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001889 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001890 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
Jesse Grossccb13522011-10-25 19:26:31 -07001891 err = PTR_ERR(vport);
1892 if (IS_ERR(vport))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001893 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001894
1895 if (vport->port_no == OVSP_LOCAL) {
1896 err = -EINVAL;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001897 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001898 }
1899
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001900 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1901 info->snd_seq, 0, OVS_VPORT_CMD_DEL);
1902 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001903 ovs_dp_detach_port(vport);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001904 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001905
Johannes Berg2a94fe42013-11-19 15:19:39 +01001906 ovs_notify(&dp_vport_genl_family, reply, info);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001907 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001908
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001909exit_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001910 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001911 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001912 return err;
1913}
1914
1915static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
1916{
1917 struct nlattr **a = info->attrs;
1918 struct ovs_header *ovs_header = info->userhdr;
1919 struct sk_buff *reply;
1920 struct vport *vport;
1921 int err;
1922
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001923 reply = ovs_vport_cmd_alloc_info();
1924 if (!reply)
1925 return -ENOMEM;
1926
Jesse Grossccb13522011-10-25 19:26:31 -07001927 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001928 vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
Jesse Grossccb13522011-10-25 19:26:31 -07001929 err = PTR_ERR(vport);
1930 if (IS_ERR(vport))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001931 goto exit_unlock_free;
1932 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1933 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1934 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001935 rcu_read_unlock();
1936
1937 return genlmsg_reply(reply, info);
1938
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001939exit_unlock_free:
Jesse Grossccb13522011-10-25 19:26:31 -07001940 rcu_read_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001941 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001942 return err;
1943}
1944
1945static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1946{
1947 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1948 struct datapath *dp;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001949 int bucket = cb->args[0], skip = cb->args[1];
1950 int i, j = 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001951
Jesse Grossccb13522011-10-25 19:26:31 -07001952 rcu_read_lock();
Jarno Rajahalme42ee19e2014-02-15 17:42:29 -08001953 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1954 if (!dp) {
1955 rcu_read_unlock();
1956 return -ENODEV;
1957 }
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001958 for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
Jesse Grossccb13522011-10-25 19:26:31 -07001959 struct vport *vport;
1960
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001961 j = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001962 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001963 if (j >= skip &&
1964 ovs_vport_cmd_fill_info(vport, skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001965 NETLINK_CB(cb->skb).portid,
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001966 cb->nlh->nlmsg_seq,
1967 NLM_F_MULTI,
1968 OVS_VPORT_CMD_NEW) < 0)
1969 goto out;
Jesse Grossccb13522011-10-25 19:26:31 -07001970
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001971 j++;
1972 }
1973 skip = 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001974 }
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001975out:
Jesse Grossccb13522011-10-25 19:26:31 -07001976 rcu_read_unlock();
1977
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001978 cb->args[0] = i;
1979 cb->args[1] = j;
Jesse Grossccb13522011-10-25 19:26:31 -07001980
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001981 return skb->len;
Jesse Grossccb13522011-10-25 19:26:31 -07001982}
1983
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001984static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
1985 [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1986 [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
1987 [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
1988 [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
1989 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1990 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
1991};
1992
stephen hemminger48e48a72014-07-16 11:25:52 -07001993static const struct genl_ops dp_vport_genl_ops[] = {
Jesse Grossccb13522011-10-25 19:26:31 -07001994 { .cmd = OVS_VPORT_CMD_NEW,
1995 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1996 .policy = vport_policy,
1997 .doit = ovs_vport_cmd_new
1998 },
1999 { .cmd = OVS_VPORT_CMD_DEL,
2000 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2001 .policy = vport_policy,
2002 .doit = ovs_vport_cmd_del
2003 },
2004 { .cmd = OVS_VPORT_CMD_GET,
2005 .flags = 0, /* OK for unprivileged users. */
2006 .policy = vport_policy,
2007 .doit = ovs_vport_cmd_get,
2008 .dumpit = ovs_vport_cmd_dump
2009 },
2010 { .cmd = OVS_VPORT_CMD_SET,
2011 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2012 .policy = vport_policy,
2013 .doit = ovs_vport_cmd_set,
2014 },
2015};
2016
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002017struct genl_family dp_vport_genl_family = {
2018 .id = GENL_ID_GENERATE,
2019 .hdrsize = sizeof(struct ovs_header),
2020 .name = OVS_VPORT_FAMILY,
2021 .version = OVS_VPORT_VERSION,
2022 .maxattr = OVS_VPORT_ATTR_MAX,
2023 .netnsok = true,
2024 .parallel_ops = true,
2025 .ops = dp_vport_genl_ops,
2026 .n_ops = ARRAY_SIZE(dp_vport_genl_ops),
2027 .mcgrps = &ovs_dp_vport_multicast_group,
2028 .n_mcgrps = 1,
Jesse Grossccb13522011-10-25 19:26:31 -07002029};
2030
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002031static struct genl_family * const dp_genl_families[] = {
2032 &dp_datapath_genl_family,
2033 &dp_vport_genl_family,
2034 &dp_flow_genl_family,
2035 &dp_packet_genl_family,
Jesse Grossccb13522011-10-25 19:26:31 -07002036};
2037
2038static void dp_unregister_genl(int n_families)
2039{
2040 int i;
2041
2042 for (i = 0; i < n_families; i++)
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002043 genl_unregister_family(dp_genl_families[i]);
Jesse Grossccb13522011-10-25 19:26:31 -07002044}
2045
2046static int dp_register_genl(void)
2047{
Jesse Grossccb13522011-10-25 19:26:31 -07002048 int err;
2049 int i;
2050
Jesse Grossccb13522011-10-25 19:26:31 -07002051 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
Jesse Grossccb13522011-10-25 19:26:31 -07002052
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002053 err = genl_register_family(dp_genl_families[i]);
Jesse Grossccb13522011-10-25 19:26:31 -07002054 if (err)
2055 goto error;
Jesse Grossccb13522011-10-25 19:26:31 -07002056 }
2057
2058 return 0;
2059
2060error:
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002061 dp_unregister_genl(i);
Jesse Grossccb13522011-10-25 19:26:31 -07002062 return err;
2063}
2064
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002065static int __net_init ovs_init_net(struct net *net)
2066{
2067 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2068
2069 INIT_LIST_HEAD(&ovs_net->dps);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002070 INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002071 return 0;
2072}
2073
2074static void __net_exit ovs_exit_net(struct net *net)
2075{
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002076 struct datapath *dp, *dp_next;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002077 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002078
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002079 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002080 list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
2081 __dp_destroy(dp);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002082 ovs_unlock();
2083
2084 cancel_work_sync(&ovs_net->dp_notify_work);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002085}
2086
2087static struct pernet_operations ovs_net_ops = {
2088 .init = ovs_init_net,
2089 .exit = ovs_exit_net,
2090 .id = &ovs_net_id,
2091 .size = sizeof(struct ovs_net),
2092};
2093
Jesse Grossccb13522011-10-25 19:26:31 -07002094static int __init dp_init(void)
2095{
Jesse Grossccb13522011-10-25 19:26:31 -07002096 int err;
2097
YOSHIFUJI Hideaki / 吉藤英明3523b292013-01-09 07:19:55 +00002098 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
Jesse Grossccb13522011-10-25 19:26:31 -07002099
2100 pr_info("Open vSwitch switching datapath\n");
2101
Andy Zhou971427f32014-09-15 19:37:25 -07002102 err = action_fifos_init();
Jesse Grossccb13522011-10-25 19:26:31 -07002103 if (err)
2104 goto error;
2105
Andy Zhou971427f32014-09-15 19:37:25 -07002106 err = ovs_internal_dev_rtnl_link_register();
2107 if (err)
2108 goto error_action_fifos_exit;
2109
Jiri Pirko5b9e7e12014-06-26 09:58:26 +02002110 err = ovs_flow_init();
2111 if (err)
2112 goto error_unreg_rtnl_link;
2113
Jesse Grossccb13522011-10-25 19:26:31 -07002114 err = ovs_vport_init();
2115 if (err)
2116 goto error_flow_exit;
2117
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002118 err = register_pernet_device(&ovs_net_ops);
Jesse Grossccb13522011-10-25 19:26:31 -07002119 if (err)
2120 goto error_vport_exit;
2121
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002122 err = register_netdevice_notifier(&ovs_dp_device_notifier);
2123 if (err)
2124 goto error_netns_exit;
2125
Thomas Graf62b9c8d2014-10-22 17:29:06 +02002126 err = ovs_netdev_init();
2127 if (err)
2128 goto error_unreg_notifier;
2129
Jesse Grossccb13522011-10-25 19:26:31 -07002130 err = dp_register_genl();
2131 if (err < 0)
Thomas Graf62b9c8d2014-10-22 17:29:06 +02002132 goto error_unreg_netdev;
Jesse Grossccb13522011-10-25 19:26:31 -07002133
Jesse Grossccb13522011-10-25 19:26:31 -07002134 return 0;
2135
Thomas Graf62b9c8d2014-10-22 17:29:06 +02002136error_unreg_netdev:
2137 ovs_netdev_exit();
Jesse Grossccb13522011-10-25 19:26:31 -07002138error_unreg_notifier:
2139 unregister_netdevice_notifier(&ovs_dp_device_notifier);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002140error_netns_exit:
2141 unregister_pernet_device(&ovs_net_ops);
Jesse Grossccb13522011-10-25 19:26:31 -07002142error_vport_exit:
2143 ovs_vport_exit();
2144error_flow_exit:
2145 ovs_flow_exit();
Jiri Pirko5b9e7e12014-06-26 09:58:26 +02002146error_unreg_rtnl_link:
2147 ovs_internal_dev_rtnl_link_unregister();
Andy Zhou971427f32014-09-15 19:37:25 -07002148error_action_fifos_exit:
2149 action_fifos_exit();
Jesse Grossccb13522011-10-25 19:26:31 -07002150error:
2151 return err;
2152}
2153
2154static void dp_cleanup(void)
2155{
Jesse Grossccb13522011-10-25 19:26:31 -07002156 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
Thomas Graf62b9c8d2014-10-22 17:29:06 +02002157 ovs_netdev_exit();
Jesse Grossccb13522011-10-25 19:26:31 -07002158 unregister_netdevice_notifier(&ovs_dp_device_notifier);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002159 unregister_pernet_device(&ovs_net_ops);
2160 rcu_barrier();
Jesse Grossccb13522011-10-25 19:26:31 -07002161 ovs_vport_exit();
2162 ovs_flow_exit();
Jiri Pirko5b9e7e12014-06-26 09:58:26 +02002163 ovs_internal_dev_rtnl_link_unregister();
Andy Zhou971427f32014-09-15 19:37:25 -07002164 action_fifos_exit();
Jesse Grossccb13522011-10-25 19:26:31 -07002165}
2166
2167module_init(dp_init);
2168module_exit(dp_cleanup);
2169
2170MODULE_DESCRIPTION("Open vSwitch switching datapath");
2171MODULE_LICENSE("GPL");