blob: 2e31d9e7f4dc4ca1c002b3c488b838236dcdf2de [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;
62
Pravin B Shelar0c200ef2014-05-06 16:44:50 -070063static struct genl_family dp_packet_genl_family;
64static struct genl_family dp_flow_genl_family;
65static struct genl_family dp_datapath_genl_family;
66
stephen hemminger48e48a72014-07-16 11:25:52 -070067static const struct genl_multicast_group ovs_dp_flow_multicast_group = {
68 .name = OVS_FLOW_MCGROUP,
Pravin B Shelar0c200ef2014-05-06 16:44:50 -070069};
70
stephen hemminger48e48a72014-07-16 11:25:52 -070071static const struct genl_multicast_group ovs_dp_datapath_multicast_group = {
72 .name = OVS_DATAPATH_MCGROUP,
Pravin B Shelar0c200ef2014-05-06 16:44:50 -070073};
74
stephen hemminger48e48a72014-07-16 11:25:52 -070075static const struct genl_multicast_group ovs_dp_vport_multicast_group = {
76 .name = OVS_VPORT_MCGROUP,
Pravin B Shelar0c200ef2014-05-06 16:44:50 -070077};
78
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -070079/* Check if need to build a reply message.
80 * OVS userspace sets the NLM_F_ECHO flag if it needs the reply. */
Samuel Gauthier9b67aa42014-09-18 10:31:04 +020081static bool ovs_must_notify(struct genl_family *family, struct genl_info *info,
82 unsigned int group)
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -070083{
84 return info->nlhdr->nlmsg_flags & NLM_F_ECHO ||
Samuel Gauthier9b67aa42014-09-18 10:31:04 +020085 genl_has_listeners(family, genl_info_net(info)->genl_sock,
86 group);
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -070087}
88
Johannes Berg68eb5502013-11-19 15:19:38 +010089static void ovs_notify(struct genl_family *family,
Johannes Berg2a94fe42013-11-19 15:19:39 +010090 struct sk_buff *skb, struct genl_info *info)
Thomas Grafed661182013-03-29 14:46:50 +010091{
Johannes Berg68eb5502013-11-19 15:19:38 +010092 genl_notify(family, skb, genl_info_net(info), info->snd_portid,
Johannes Berg2a94fe42013-11-19 15:19:39 +010093 0, info->nlhdr, GFP_KERNEL);
Thomas Grafed661182013-03-29 14:46:50 +010094}
95
Pravin B Shelar46df7b82012-02-22 19:58:59 -080096/**
Jesse Grossccb13522011-10-25 19:26:31 -070097 * DOC: Locking:
98 *
Pravin B Shelar8e4e1712013-04-15 13:23:03 -070099 * All writes e.g. Writes to device state (add/remove datapath, port, set
100 * operations on vports, etc.), Writes to other state (flow table
101 * modifications, set miscellaneous datapath parameters, etc.) are protected
102 * by ovs_lock.
Jesse Grossccb13522011-10-25 19:26:31 -0700103 *
104 * Reads are protected by RCU.
105 *
106 * There are a few special cases (mostly stats) that have their own
107 * synchronization but they nest under all of above and don't interact with
108 * each other.
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700109 *
110 * The RTNL lock nests inside ovs_mutex.
Jesse Grossccb13522011-10-25 19:26:31 -0700111 */
112
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700113static DEFINE_MUTEX(ovs_mutex);
114
115void ovs_lock(void)
116{
117 mutex_lock(&ovs_mutex);
118}
119
120void ovs_unlock(void)
121{
122 mutex_unlock(&ovs_mutex);
123}
124
125#ifdef CONFIG_LOCKDEP
126int lockdep_ovsl_is_held(void)
127{
128 if (debug_locks)
129 return lockdep_is_held(&ovs_mutex);
130 else
131 return 1;
132}
133#endif
134
Jesse Grossccb13522011-10-25 19:26:31 -0700135static struct vport *new_vport(const struct vport_parms *);
Thomas Graf8055a892013-12-13 15:22:20 +0100136static int queue_gso_packets(struct datapath *dp, struct sk_buff *,
Jesse Grossccb13522011-10-25 19:26:31 -0700137 const struct dp_upcall_info *);
Thomas Graf8055a892013-12-13 15:22:20 +0100138static int queue_userspace_packet(struct datapath *dp, struct sk_buff *,
Jesse Grossccb13522011-10-25 19:26:31 -0700139 const struct dp_upcall_info *);
140
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700141/* Must be called with rcu_read_lock or ovs_mutex. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800142static struct datapath *get_dp(struct net *net, int dp_ifindex)
Jesse Grossccb13522011-10-25 19:26:31 -0700143{
144 struct datapath *dp = NULL;
145 struct net_device *dev;
146
147 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800148 dev = dev_get_by_index_rcu(net, dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -0700149 if (dev) {
150 struct vport *vport = ovs_internal_dev_get_vport(dev);
151 if (vport)
152 dp = vport->dp;
153 }
154 rcu_read_unlock();
155
156 return dp;
157}
158
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700159/* Must be called with rcu_read_lock or ovs_mutex. */
Andy Zhou971427f32014-09-15 19:37:25 -0700160const char *ovs_dp_name(const struct datapath *dp)
Jesse Grossccb13522011-10-25 19:26:31 -0700161{
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700162 struct vport *vport = ovs_vport_ovsl_rcu(dp, OVSP_LOCAL);
Jesse Grossccb13522011-10-25 19:26:31 -0700163 return vport->ops->get_name(vport);
164}
165
166static int get_dpifindex(struct datapath *dp)
167{
168 struct vport *local;
169 int ifindex;
170
171 rcu_read_lock();
172
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700173 local = ovs_vport_rcu(dp, OVSP_LOCAL);
Jesse Grossccb13522011-10-25 19:26:31 -0700174 if (local)
Thomas Grafcff63a52013-04-29 13:06:41 +0000175 ifindex = netdev_vport_priv(local)->dev->ifindex;
Jesse Grossccb13522011-10-25 19:26:31 -0700176 else
177 ifindex = 0;
178
179 rcu_read_unlock();
180
181 return ifindex;
182}
183
184static void destroy_dp_rcu(struct rcu_head *rcu)
185{
186 struct datapath *dp = container_of(rcu, struct datapath, rcu);
187
Jesse Grossccb13522011-10-25 19:26:31 -0700188 free_percpu(dp->stats_percpu);
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800189 release_net(ovs_dp_get_net(dp));
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700190 kfree(dp->ports);
Jesse Grossccb13522011-10-25 19:26:31 -0700191 kfree(dp);
192}
193
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700194static struct hlist_head *vport_hash_bucket(const struct datapath *dp,
195 u16 port_no)
196{
197 return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)];
198}
199
Jarno Rajahalmebb6f9a72014-05-05 11:32:17 -0700200/* Called with ovs_mutex or RCU read lock. */
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700201struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no)
202{
203 struct vport *vport;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700204 struct hlist_head *head;
205
206 head = vport_hash_bucket(dp, port_no);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800207 hlist_for_each_entry_rcu(vport, head, dp_hash_node) {
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700208 if (vport->port_no == port_no)
209 return vport;
210 }
211 return NULL;
212}
213
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700214/* Called with ovs_mutex. */
Jesse Grossccb13522011-10-25 19:26:31 -0700215static struct vport *new_vport(const struct vport_parms *parms)
216{
217 struct vport *vport;
218
219 vport = ovs_vport_add(parms);
220 if (!IS_ERR(vport)) {
221 struct datapath *dp = parms->dp;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700222 struct hlist_head *head = vport_hash_bucket(dp, vport->port_no);
Jesse Grossccb13522011-10-25 19:26:31 -0700223
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700224 hlist_add_head_rcu(&vport->dp_hash_node, head);
Jesse Grossccb13522011-10-25 19:26:31 -0700225 }
Jesse Grossccb13522011-10-25 19:26:31 -0700226 return vport;
227}
228
Jesse Grossccb13522011-10-25 19:26:31 -0700229void ovs_dp_detach_port(struct vport *p)
230{
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700231 ASSERT_OVSL();
Jesse Grossccb13522011-10-25 19:26:31 -0700232
233 /* First drop references to device. */
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700234 hlist_del_rcu(&p->dp_hash_node);
Jesse Grossccb13522011-10-25 19:26:31 -0700235
236 /* Then destroy it. */
237 ovs_vport_del(p);
238}
239
240/* Must be called with rcu_read_lock. */
Pravin B Shelar8c8b1b82014-09-15 19:28:44 -0700241void ovs_dp_process_packet(struct sk_buff *skb, struct sw_flow_key *key)
Jesse Grossccb13522011-10-25 19:26:31 -0700242{
Pravin B Shelar83c8df22014-09-15 19:20:31 -0700243 const struct vport *p = OVS_CB(skb)->input_vport;
Jesse Grossccb13522011-10-25 19:26:31 -0700244 struct datapath *dp = p->dp;
245 struct sw_flow *flow;
246 struct dp_stats_percpu *stats;
Jesse Grossccb13522011-10-25 19:26:31 -0700247 u64 *stats_counter;
Andy Zhou1bd71162013-10-22 10:42:46 -0700248 u32 n_mask_hit;
Jesse Grossccb13522011-10-25 19:26:31 -0700249
Shan Wei404f2f12012-11-13 09:52:25 +0800250 stats = this_cpu_ptr(dp->stats_percpu);
Jesse Grossccb13522011-10-25 19:26:31 -0700251
Jesse Grossccb13522011-10-25 19:26:31 -0700252 /* Look up flow. */
Pravin B Shelar8c8b1b82014-09-15 19:28:44 -0700253 flow = ovs_flow_tbl_lookup_stats(&dp->table, key, &n_mask_hit);
Jesse Grossccb13522011-10-25 19:26:31 -0700254 if (unlikely(!flow)) {
255 struct dp_upcall_info upcall;
Pravin B Shelar8c8b1b82014-09-15 19:28:44 -0700256 int error;
Jesse Grossccb13522011-10-25 19:26:31 -0700257
258 upcall.cmd = OVS_PACKET_CMD_MISS;
Pravin B Shelar8c8b1b82014-09-15 19:28:44 -0700259 upcall.key = key;
Jesse Grossccb13522011-10-25 19:26:31 -0700260 upcall.userdata = NULL;
Alex Wang5cd667b2014-07-17 15:14:13 -0700261 upcall.portid = ovs_vport_find_upcall_portid(p, skb);
Li RongQingc5eba0b2014-09-03 17:43:45 +0800262 error = ovs_dp_upcall(dp, skb, &upcall);
263 if (unlikely(error))
264 kfree_skb(skb);
265 else
266 consume_skb(skb);
Jesse Grossccb13522011-10-25 19:26:31 -0700267 stats_counter = &stats->n_missed;
268 goto out;
269 }
270
271 OVS_CB(skb)->flow = flow;
272
Pravin B Shelar8c8b1b82014-09-15 19:28:44 -0700273 ovs_flow_stats_update(OVS_CB(skb)->flow, key->tp.flags, skb);
274 ovs_execute_actions(dp, skb, key);
Pravin B Shelare298e502013-10-29 17:22:21 -0700275 stats_counter = &stats->n_hit;
Jesse Grossccb13522011-10-25 19:26:31 -0700276
277out:
278 /* Update datapath statistics. */
WANG Congdf9d9fd2014-02-14 15:10:46 -0800279 u64_stats_update_begin(&stats->syncp);
Jesse Grossccb13522011-10-25 19:26:31 -0700280 (*stats_counter)++;
Andy Zhou1bd71162013-10-22 10:42:46 -0700281 stats->n_mask_hit += n_mask_hit;
WANG Congdf9d9fd2014-02-14 15:10:46 -0800282 u64_stats_update_end(&stats->syncp);
Jesse Grossccb13522011-10-25 19:26:31 -0700283}
284
Jesse Grossccb13522011-10-25 19:26:31 -0700285int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800286 const struct dp_upcall_info *upcall_info)
Jesse Grossccb13522011-10-25 19:26:31 -0700287{
288 struct dp_stats_percpu *stats;
Jesse Grossccb13522011-10-25 19:26:31 -0700289 int err;
290
Eric W. Biederman15e47302012-09-07 20:12:54 +0000291 if (upcall_info->portid == 0) {
Jesse Grossccb13522011-10-25 19:26:31 -0700292 err = -ENOTCONN;
293 goto err;
294 }
295
Jesse Grossccb13522011-10-25 19:26:31 -0700296 if (!skb_is_gso(skb))
Thomas Graf8055a892013-12-13 15:22:20 +0100297 err = queue_userspace_packet(dp, skb, upcall_info);
Jesse Grossccb13522011-10-25 19:26:31 -0700298 else
Thomas Graf8055a892013-12-13 15:22:20 +0100299 err = queue_gso_packets(dp, skb, upcall_info);
Jesse Grossccb13522011-10-25 19:26:31 -0700300 if (err)
301 goto err;
302
303 return 0;
304
305err:
Shan Wei404f2f12012-11-13 09:52:25 +0800306 stats = this_cpu_ptr(dp->stats_percpu);
Jesse Grossccb13522011-10-25 19:26:31 -0700307
WANG Congdf9d9fd2014-02-14 15:10:46 -0800308 u64_stats_update_begin(&stats->syncp);
Jesse Grossccb13522011-10-25 19:26:31 -0700309 stats->n_lost++;
WANG Congdf9d9fd2014-02-14 15:10:46 -0800310 u64_stats_update_end(&stats->syncp);
Jesse Grossccb13522011-10-25 19:26:31 -0700311
312 return err;
313}
314
Thomas Graf8055a892013-12-13 15:22:20 +0100315static int queue_gso_packets(struct datapath *dp, struct sk_buff *skb,
Jesse Grossccb13522011-10-25 19:26:31 -0700316 const struct dp_upcall_info *upcall_info)
317{
Ben Pfaffa1b5d0d2012-07-20 14:47:54 -0700318 unsigned short gso_type = skb_shinfo(skb)->gso_type;
Jesse Grossccb13522011-10-25 19:26:31 -0700319 struct dp_upcall_info later_info;
320 struct sw_flow_key later_key;
321 struct sk_buff *segs, *nskb;
322 int err;
323
Thomas Graf09c5e602013-12-13 15:22:22 +0100324 segs = __skb_gso_segment(skb, NETIF_F_SG, false);
Pravin B Shelar92e5dfc2012-07-20 14:46:29 -0700325 if (IS_ERR(segs))
326 return PTR_ERR(segs);
Jesse Grossccb13522011-10-25 19:26:31 -0700327
328 /* Queue all of the segments. */
329 skb = segs;
330 do {
Thomas Graf8055a892013-12-13 15:22:20 +0100331 err = queue_userspace_packet(dp, skb, upcall_info);
Jesse Grossccb13522011-10-25 19:26:31 -0700332 if (err)
333 break;
334
Ben Pfaffa1b5d0d2012-07-20 14:47:54 -0700335 if (skb == segs && gso_type & SKB_GSO_UDP) {
Jesse Grossccb13522011-10-25 19:26:31 -0700336 /* The initial flow key extracted by ovs_flow_extract()
337 * in this case is for a first fragment, so we need to
338 * properly mark later fragments.
339 */
340 later_key = *upcall_info->key;
341 later_key.ip.frag = OVS_FRAG_TYPE_LATER;
342
343 later_info = *upcall_info;
344 later_info.key = &later_key;
345 upcall_info = &later_info;
346 }
347 } while ((skb = skb->next));
348
349 /* Free all of the segments. */
350 skb = segs;
351 do {
352 nskb = skb->next;
353 if (err)
354 kfree_skb(skb);
355 else
356 consume_skb(skb);
357 } while ((skb = nskb));
358 return err;
359}
360
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100361static size_t key_attr_size(void)
362{
363 return nla_total_size(4) /* OVS_KEY_ATTR_PRIORITY */
Pravin B Shelar7d5437c2013-06-17 17:50:18 -0700364 + nla_total_size(0) /* OVS_KEY_ATTR_TUNNEL */
365 + nla_total_size(8) /* OVS_TUNNEL_KEY_ATTR_ID */
366 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_SRC */
367 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_DST */
368 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TOS */
369 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TTL */
370 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT */
371 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_CSUM */
Jesse Gross67fa0342014-10-03 15:35:30 -0700372 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_OAM */
Jesse Grossf5796682014-10-03 15:35:33 -0700373 + nla_total_size(256) /* OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS */
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100374 + nla_total_size(4) /* OVS_KEY_ATTR_IN_PORT */
375 + nla_total_size(4) /* OVS_KEY_ATTR_SKB_MARK */
376 + nla_total_size(12) /* OVS_KEY_ATTR_ETHERNET */
377 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
378 + nla_total_size(4) /* OVS_KEY_ATTR_8021Q */
379 + nla_total_size(0) /* OVS_KEY_ATTR_ENCAP */
380 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
381 + nla_total_size(40) /* OVS_KEY_ATTR_IPV6 */
382 + nla_total_size(2) /* OVS_KEY_ATTR_ICMPV6 */
383 + nla_total_size(28); /* OVS_KEY_ATTR_ND */
384}
385
Thomas Grafbda56f12013-12-13 15:22:21 +0100386static size_t upcall_msg_size(const struct nlattr *userdata,
387 unsigned int hdrlen)
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100388{
389 size_t size = NLMSG_ALIGN(sizeof(struct ovs_header))
Thomas Grafbda56f12013-12-13 15:22:21 +0100390 + nla_total_size(hdrlen) /* OVS_PACKET_ATTR_PACKET */
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100391 + nla_total_size(key_attr_size()); /* OVS_PACKET_ATTR_KEY */
392
393 /* OVS_PACKET_ATTR_USERDATA */
394 if (userdata)
395 size += NLA_ALIGN(userdata->nla_len);
396
397 return size;
398}
399
Thomas Graf8055a892013-12-13 15:22:20 +0100400static int queue_userspace_packet(struct datapath *dp, struct sk_buff *skb,
Jesse Grossccb13522011-10-25 19:26:31 -0700401 const struct dp_upcall_info *upcall_info)
402{
403 struct ovs_header *upcall;
404 struct sk_buff *nskb = NULL;
Li RongQing4ee45ea2014-09-02 20:52:28 +0800405 struct sk_buff *user_skb = NULL; /* to be queued to userspace */
Jesse Grossccb13522011-10-25 19:26:31 -0700406 struct nlattr *nla;
Thomas Graf795449d2013-11-30 13:21:32 +0100407 struct genl_info info = {
Thomas Graf8055a892013-12-13 15:22:20 +0100408 .dst_sk = ovs_dp_get_net(dp)->genl_sock,
Thomas Graf795449d2013-11-30 13:21:32 +0100409 .snd_portid = upcall_info->portid,
410 };
411 size_t len;
Thomas Grafbda56f12013-12-13 15:22:21 +0100412 unsigned int hlen;
Thomas Graf8055a892013-12-13 15:22:20 +0100413 int err, dp_ifindex;
414
415 dp_ifindex = get_dpifindex(dp);
416 if (!dp_ifindex)
417 return -ENODEV;
Jesse Grossccb13522011-10-25 19:26:31 -0700418
419 if (vlan_tx_tag_present(skb)) {
420 nskb = skb_clone(skb, GFP_ATOMIC);
421 if (!nskb)
422 return -ENOMEM;
423
Patrick McHardy86a9bad2013-04-19 02:04:30 +0000424 nskb = __vlan_put_tag(nskb, nskb->vlan_proto, vlan_tx_tag_get(nskb));
Dan Carpenter8aa51d62012-05-13 08:44:18 +0000425 if (!nskb)
Jesse Grossccb13522011-10-25 19:26:31 -0700426 return -ENOMEM;
427
428 nskb->vlan_tci = 0;
429 skb = nskb;
430 }
431
432 if (nla_attr_size(skb->len) > USHRT_MAX) {
433 err = -EFBIG;
434 goto out;
435 }
436
Thomas Grafbda56f12013-12-13 15:22:21 +0100437 /* Complete checksum if needed */
438 if (skb->ip_summed == CHECKSUM_PARTIAL &&
439 (err = skb_checksum_help(skb)))
440 goto out;
441
442 /* Older versions of OVS user space enforce alignment of the last
443 * Netlink attribute to NLA_ALIGNTO which would require extensive
444 * padding logic. Only perform zerocopy if padding is not required.
445 */
446 if (dp->user_features & OVS_DP_F_UNALIGNED)
447 hlen = skb_zerocopy_headlen(skb);
448 else
449 hlen = skb->len;
450
451 len = upcall_msg_size(upcall_info->userdata, hlen);
Thomas Graf795449d2013-11-30 13:21:32 +0100452 user_skb = genlmsg_new_unicast(len, &info, GFP_ATOMIC);
Jesse Grossccb13522011-10-25 19:26:31 -0700453 if (!user_skb) {
454 err = -ENOMEM;
455 goto out;
456 }
457
458 upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
459 0, upcall_info->cmd);
460 upcall->dp_ifindex = dp_ifindex;
461
462 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
Andy Zhouf53e3832014-07-17 15:17:44 -0700463 err = ovs_nla_put_flow(upcall_info->key, upcall_info->key, user_skb);
464 BUG_ON(err);
Jesse Grossccb13522011-10-25 19:26:31 -0700465 nla_nest_end(user_skb, nla);
466
467 if (upcall_info->userdata)
Ben Pfaff44901082013-02-15 17:29:22 -0800468 __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA,
469 nla_len(upcall_info->userdata),
470 nla_data(upcall_info->userdata));
Jesse Grossccb13522011-10-25 19:26:31 -0700471
Thomas Grafbda56f12013-12-13 15:22:21 +0100472 /* Only reserve room for attribute header, packet data is added
473 * in skb_zerocopy() */
474 if (!(nla = nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, 0))) {
475 err = -ENOBUFS;
476 goto out;
477 }
478 nla->nla_len = nla_attr_size(skb->len);
Jesse Grossccb13522011-10-25 19:26:31 -0700479
Zoltan Kiss36d5fe62014-03-26 22:37:45 +0000480 err = skb_zerocopy(user_skb, skb, skb->len, hlen);
481 if (err)
482 goto out;
Jesse Grossccb13522011-10-25 19:26:31 -0700483
Thomas Grafaea0bb42014-01-14 16:27:49 +0000484 /* Pad OVS_PACKET_ATTR_PACKET if linear copy was performed */
485 if (!(dp->user_features & OVS_DP_F_UNALIGNED)) {
486 size_t plen = NLA_ALIGN(user_skb->len) - user_skb->len;
487
488 if (plen > 0)
489 memset(skb_put(user_skb, plen), 0, plen);
490 }
491
Thomas Grafbda56f12013-12-13 15:22:21 +0100492 ((struct nlmsghdr *) user_skb->data)->nlmsg_len = user_skb->len;
493
Thomas Graf8055a892013-12-13 15:22:20 +0100494 err = genlmsg_unicast(ovs_dp_get_net(dp), user_skb, upcall_info->portid);
Li RongQing4ee45ea2014-09-02 20:52:28 +0800495 user_skb = NULL;
Jesse Grossccb13522011-10-25 19:26:31 -0700496out:
Zoltan Kiss36d5fe62014-03-26 22:37:45 +0000497 if (err)
498 skb_tx_error(skb);
Li RongQing4ee45ea2014-09-02 20:52:28 +0800499 kfree_skb(user_skb);
Jesse Grossccb13522011-10-25 19:26:31 -0700500 kfree_skb(nskb);
501 return err;
502}
503
Jesse Grossccb13522011-10-25 19:26:31 -0700504static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
505{
506 struct ovs_header *ovs_header = info->userhdr;
507 struct nlattr **a = info->attrs;
508 struct sw_flow_actions *acts;
509 struct sk_buff *packet;
510 struct sw_flow *flow;
511 struct datapath *dp;
512 struct ethhdr *eth;
Pravin B Shelar83c8df22014-09-15 19:20:31 -0700513 struct vport *input_vport;
Jesse Grossccb13522011-10-25 19:26:31 -0700514 int len;
515 int err;
Jesse Grossccb13522011-10-25 19:26:31 -0700516
517 err = -EINVAL;
518 if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
Thomas Grafdded45fc2013-03-29 14:46:47 +0100519 !a[OVS_PACKET_ATTR_ACTIONS])
Jesse Grossccb13522011-10-25 19:26:31 -0700520 goto err;
521
522 len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
523 packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
524 err = -ENOMEM;
525 if (!packet)
526 goto err;
527 skb_reserve(packet, NET_IP_ALIGN);
528
Thomas Graf32686a92013-03-29 14:46:48 +0100529 nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
Jesse Grossccb13522011-10-25 19:26:31 -0700530
531 skb_reset_mac_header(packet);
532 eth = eth_hdr(packet);
533
534 /* Normally, setting the skb 'protocol' field would be handled by a
535 * call to eth_type_trans(), but it assumes there's a sending
536 * device, which we may not have. */
Simon Hormane5c5d222013-03-28 13:38:25 +0900537 if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN)
Jesse Grossccb13522011-10-25 19:26:31 -0700538 packet->protocol = eth->h_proto;
539 else
540 packet->protocol = htons(ETH_P_802_2);
541
542 /* Build an sw_flow for sending this packet. */
Jarno Rajahalme23dabf82014-03-27 12:35:23 -0700543 flow = ovs_flow_alloc();
Jesse Grossccb13522011-10-25 19:26:31 -0700544 err = PTR_ERR(flow);
545 if (IS_ERR(flow))
546 goto err_kfree_skb;
547
Pravin B Shelar83c8df22014-09-15 19:20:31 -0700548 err = ovs_flow_key_extract_userspace(a[OVS_PACKET_ATTR_KEY], packet,
549 &flow->key);
Jesse Grossccb13522011-10-25 19:26:31 -0700550 if (err)
551 goto err_flow_free;
552
Pravin B Shelare6445712013-10-03 18:16:47 -0700553 acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_PACKET_ATTR_ACTIONS]));
Jesse Grossccb13522011-10-25 19:26:31 -0700554 err = PTR_ERR(acts);
555 if (IS_ERR(acts))
556 goto err_flow_free;
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700557
Pravin B Shelare6445712013-10-03 18:16:47 -0700558 err = ovs_nla_copy_actions(a[OVS_PACKET_ATTR_ACTIONS],
559 &flow->key, 0, &acts);
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700560 if (err)
561 goto err_flow_free;
Jesse Grossccb13522011-10-25 19:26:31 -0700562
Jesse Grossf5796682014-10-03 15:35:33 -0700563 rcu_assign_pointer(flow->sf_acts, acts);
564
565 OVS_CB(packet)->egress_tun_info = NULL;
Jesse Grossccb13522011-10-25 19:26:31 -0700566 OVS_CB(packet)->flow = flow;
567 packet->priority = flow->key.phy.priority;
Ansis Atteka39c7caeb2012-11-26 11:24:11 -0800568 packet->mark = flow->key.phy.skb_mark;
Jesse Grossccb13522011-10-25 19:26:31 -0700569
570 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800571 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -0700572 err = -ENODEV;
573 if (!dp)
574 goto err_unlock;
575
Pravin B Shelar83c8df22014-09-15 19:20:31 -0700576 input_vport = ovs_vport_rcu(dp, flow->key.phy.in_port);
577 if (!input_vport)
578 input_vport = ovs_vport_rcu(dp, OVSP_LOCAL);
579
580 if (!input_vport)
581 goto err_unlock;
582
583 OVS_CB(packet)->input_vport = input_vport;
584
Jesse Grossccb13522011-10-25 19:26:31 -0700585 local_bh_disable();
Pravin B Shelar2ff3e4e2014-09-15 19:15:28 -0700586 err = ovs_execute_actions(dp, packet, &flow->key);
Jesse Grossccb13522011-10-25 19:26:31 -0700587 local_bh_enable();
588 rcu_read_unlock();
589
Andy Zhou03f0d912013-08-07 20:01:00 -0700590 ovs_flow_free(flow, false);
Jesse Grossccb13522011-10-25 19:26:31 -0700591 return err;
592
593err_unlock:
594 rcu_read_unlock();
595err_flow_free:
Andy Zhou03f0d912013-08-07 20:01:00 -0700596 ovs_flow_free(flow, false);
Jesse Grossccb13522011-10-25 19:26:31 -0700597err_kfree_skb:
598 kfree_skb(packet);
599err:
600 return err;
601}
602
603static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
Thomas Grafdded45fc2013-03-29 14:46:47 +0100604 [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
Jesse Grossccb13522011-10-25 19:26:31 -0700605 [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
606 [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
607};
608
Johannes Berg4534de82013-11-14 17:14:46 +0100609static const struct genl_ops dp_packet_genl_ops[] = {
Jesse Grossccb13522011-10-25 19:26:31 -0700610 { .cmd = OVS_PACKET_CMD_EXECUTE,
611 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
612 .policy = packet_policy,
613 .doit = ovs_packet_cmd_execute
614 }
615};
616
Pravin B Shelar0c200ef2014-05-06 16:44:50 -0700617static struct genl_family dp_packet_genl_family = {
618 .id = GENL_ID_GENERATE,
619 .hdrsize = sizeof(struct ovs_header),
620 .name = OVS_PACKET_FAMILY,
621 .version = OVS_PACKET_VERSION,
622 .maxattr = OVS_PACKET_ATTR_MAX,
623 .netnsok = true,
624 .parallel_ops = true,
625 .ops = dp_packet_genl_ops,
626 .n_ops = ARRAY_SIZE(dp_packet_genl_ops),
627};
628
Andy Zhou1bd71162013-10-22 10:42:46 -0700629static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats,
630 struct ovs_dp_megaflow_stats *mega_stats)
Jesse Grossccb13522011-10-25 19:26:31 -0700631{
632 int i;
Jesse Grossccb13522011-10-25 19:26:31 -0700633
Andy Zhou1bd71162013-10-22 10:42:46 -0700634 memset(mega_stats, 0, sizeof(*mega_stats));
635
Pravin B Shelarb637e492013-10-04 00:14:23 -0700636 stats->n_flows = ovs_flow_tbl_count(&dp->table);
Andy Zhou1bd71162013-10-22 10:42:46 -0700637 mega_stats->n_masks = ovs_flow_tbl_num_masks(&dp->table);
Jesse Grossccb13522011-10-25 19:26:31 -0700638
639 stats->n_hit = stats->n_missed = stats->n_lost = 0;
Andy Zhou1bd71162013-10-22 10:42:46 -0700640
Jesse Grossccb13522011-10-25 19:26:31 -0700641 for_each_possible_cpu(i) {
642 const struct dp_stats_percpu *percpu_stats;
643 struct dp_stats_percpu local_stats;
644 unsigned int start;
645
646 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
647
648 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700649 start = u64_stats_fetch_begin_irq(&percpu_stats->syncp);
Jesse Grossccb13522011-10-25 19:26:31 -0700650 local_stats = *percpu_stats;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700651 } while (u64_stats_fetch_retry_irq(&percpu_stats->syncp, start));
Jesse Grossccb13522011-10-25 19:26:31 -0700652
653 stats->n_hit += local_stats.n_hit;
654 stats->n_missed += local_stats.n_missed;
655 stats->n_lost += local_stats.n_lost;
Andy Zhou1bd71162013-10-22 10:42:46 -0700656 mega_stats->n_mask_hit += local_stats.n_mask_hit;
Jesse Grossccb13522011-10-25 19:26:31 -0700657 }
658}
659
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100660static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts)
661{
662 return NLMSG_ALIGN(sizeof(struct ovs_header))
663 + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_KEY */
Andy Zhou03f0d912013-08-07 20:01:00 -0700664 + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_MASK */
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100665 + nla_total_size(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
666 + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
667 + nla_total_size(8) /* OVS_FLOW_ATTR_USED */
668 + nla_total_size(acts->actions_len); /* OVS_FLOW_ATTR_ACTIONS */
669}
670
Jarno Rajahalmebb6f9a72014-05-05 11:32:17 -0700671/* Called with ovs_mutex or RCU read lock. */
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700672static int ovs_flow_cmd_fill_info(const struct sw_flow *flow, int dp_ifindex,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000673 struct sk_buff *skb, u32 portid,
Jesse Grossccb13522011-10-25 19:26:31 -0700674 u32 seq, u32 flags, u8 cmd)
675{
676 const int skb_orig_len = skb->len;
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700677 struct nlattr *start;
Jesse Grossccb13522011-10-25 19:26:31 -0700678 struct ovs_flow_stats stats;
Pravin B Shelare298e502013-10-29 17:22:21 -0700679 __be16 tcp_flags;
680 unsigned long used;
Jesse Grossccb13522011-10-25 19:26:31 -0700681 struct ovs_header *ovs_header;
682 struct nlattr *nla;
Jesse Grossccb13522011-10-25 19:26:31 -0700683 int err;
684
Eric W. Biederman15e47302012-09-07 20:12:54 +0000685 ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family, flags, cmd);
Jesse Grossccb13522011-10-25 19:26:31 -0700686 if (!ovs_header)
687 return -EMSGSIZE;
688
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700689 ovs_header->dp_ifindex = dp_ifindex;
Jesse Grossccb13522011-10-25 19:26:31 -0700690
Andy Zhou03f0d912013-08-07 20:01:00 -0700691 /* Fill flow key. */
Jesse Grossccb13522011-10-25 19:26:31 -0700692 nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
693 if (!nla)
694 goto nla_put_failure;
Andy Zhou03f0d912013-08-07 20:01:00 -0700695
Pravin B Shelare6445712013-10-03 18:16:47 -0700696 err = ovs_nla_put_flow(&flow->unmasked_key, &flow->unmasked_key, skb);
Jesse Grossccb13522011-10-25 19:26:31 -0700697 if (err)
698 goto error;
699 nla_nest_end(skb, nla);
700
Andy Zhou03f0d912013-08-07 20:01:00 -0700701 nla = nla_nest_start(skb, OVS_FLOW_ATTR_MASK);
702 if (!nla)
703 goto nla_put_failure;
704
Pravin B Shelare6445712013-10-03 18:16:47 -0700705 err = ovs_nla_put_flow(&flow->key, &flow->mask->key, skb);
Andy Zhou03f0d912013-08-07 20:01:00 -0700706 if (err)
707 goto error;
708
709 nla_nest_end(skb, nla);
710
Pravin B Shelare298e502013-10-29 17:22:21 -0700711 ovs_flow_stats_get(flow, &stats, &used, &tcp_flags);
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700712
David S. Miller028d6a62012-03-29 23:20:48 -0400713 if (used &&
714 nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
715 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -0700716
David S. Miller028d6a62012-03-29 23:20:48 -0400717 if (stats.n_packets &&
Pravin B Shelare298e502013-10-29 17:22:21 -0700718 nla_put(skb, OVS_FLOW_ATTR_STATS, sizeof(struct ovs_flow_stats), &stats))
David S. Miller028d6a62012-03-29 23:20:48 -0400719 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -0700720
Pravin B Shelare298e502013-10-29 17:22:21 -0700721 if ((u8)ntohs(tcp_flags) &&
722 nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, (u8)ntohs(tcp_flags)))
David S. Miller028d6a62012-03-29 23:20:48 -0400723 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -0700724
725 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
726 * this is the first flow to be dumped into 'skb'. This is unusual for
727 * Netlink but individual action lists can be longer than
728 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
729 * The userspace caller can always fetch the actions separately if it
730 * really wants them. (Most userspace callers in fact don't care.)
731 *
732 * This can only fail for dump operations because the skb is always
733 * properly sized for single flows.
734 */
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700735 start = nla_nest_start(skb, OVS_FLOW_ATTR_ACTIONS);
736 if (start) {
Pravin B Shelard57170b2013-07-30 15:39:39 -0700737 const struct sw_flow_actions *sf_acts;
738
Jesse Gross663efa32013-12-03 10:58:53 -0800739 sf_acts = rcu_dereference_ovsl(flow->sf_acts);
Pravin B Shelare6445712013-10-03 18:16:47 -0700740 err = ovs_nla_put_actions(sf_acts->actions,
741 sf_acts->actions_len, skb);
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700742
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700743 if (!err)
744 nla_nest_end(skb, start);
745 else {
746 if (skb_orig_len)
747 goto error;
748
749 nla_nest_cancel(skb, start);
750 }
751 } else if (skb_orig_len)
752 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -0700753
754 return genlmsg_end(skb, ovs_header);
755
756nla_put_failure:
757 err = -EMSGSIZE;
758error:
759 genlmsg_cancel(skb, ovs_header);
760 return err;
761}
762
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700763/* May not be called with RCU read lock. */
764static struct sk_buff *ovs_flow_cmd_alloc_info(const struct sw_flow_actions *acts,
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700765 struct genl_info *info,
766 bool always)
Jesse Grossccb13522011-10-25 19:26:31 -0700767{
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700768 struct sk_buff *skb;
Jesse Grossccb13522011-10-25 19:26:31 -0700769
Samuel Gauthier9b67aa42014-09-18 10:31:04 +0200770 if (!always && !ovs_must_notify(&dp_flow_genl_family, info, 0))
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700771 return NULL;
772
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700773 skb = genlmsg_new_unicast(ovs_flow_cmd_msg_size(acts), info, GFP_KERNEL);
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700774 if (!skb)
775 return ERR_PTR(-ENOMEM);
776
777 return skb;
Jesse Grossccb13522011-10-25 19:26:31 -0700778}
779
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700780/* Called with ovs_mutex. */
781static struct sk_buff *ovs_flow_cmd_build_info(const struct sw_flow *flow,
782 int dp_ifindex,
783 struct genl_info *info, u8 cmd,
784 bool always)
Jesse Grossccb13522011-10-25 19:26:31 -0700785{
786 struct sk_buff *skb;
787 int retval;
788
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700789 skb = ovs_flow_cmd_alloc_info(ovsl_dereference(flow->sf_acts), info,
790 always);
Himangi Saraogid0e992a2014-07-27 12:37:46 +0530791 if (IS_ERR_OR_NULL(skb))
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700792 return skb;
Jesse Grossccb13522011-10-25 19:26:31 -0700793
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700794 retval = ovs_flow_cmd_fill_info(flow, dp_ifindex, skb,
795 info->snd_portid, info->snd_seq, 0,
796 cmd);
Jesse Grossccb13522011-10-25 19:26:31 -0700797 BUG_ON(retval < 0);
798 return skb;
799}
800
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700801static int ovs_flow_cmd_new(struct sk_buff *skb, struct genl_info *info)
Jesse Grossccb13522011-10-25 19:26:31 -0700802{
803 struct nlattr **a = info->attrs;
804 struct ovs_header *ovs_header = info->userhdr;
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700805 struct sw_flow *flow, *new_flow;
Andy Zhou03f0d912013-08-07 20:01:00 -0700806 struct sw_flow_mask mask;
Jesse Grossccb13522011-10-25 19:26:31 -0700807 struct sk_buff *reply;
808 struct datapath *dp;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700809 struct sw_flow_actions *acts;
810 struct sw_flow_match match;
811 int error;
812
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700813 /* Must have key and actions. */
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700814 error = -EINVAL;
815 if (!a[OVS_FLOW_ATTR_KEY])
816 goto error;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700817 if (!a[OVS_FLOW_ATTR_ACTIONS])
818 goto error;
819
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700820 /* Most of the time we need to allocate a new flow, do it before
821 * locking.
822 */
823 new_flow = ovs_flow_alloc();
824 if (IS_ERR(new_flow)) {
825 error = PTR_ERR(new_flow);
826 goto error;
827 }
828
829 /* Extract key. */
830 ovs_match_init(&match, &new_flow->unmasked_key, &mask);
831 error = ovs_nla_get_match(&match,
832 a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
833 if (error)
834 goto err_kfree_flow;
835
836 ovs_flow_mask_key(&new_flow->key, &new_flow->unmasked_key, &mask);
837
838 /* Validate actions. */
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700839 acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_FLOW_ATTR_ACTIONS]));
840 error = PTR_ERR(acts);
841 if (IS_ERR(acts))
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700842 goto err_kfree_flow;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700843
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700844 error = ovs_nla_copy_actions(a[OVS_FLOW_ATTR_ACTIONS], &new_flow->key,
845 0, &acts);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700846 if (error) {
847 OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700848 goto err_kfree_acts;
849 }
850
851 reply = ovs_flow_cmd_alloc_info(acts, info, false);
852 if (IS_ERR(reply)) {
853 error = PTR_ERR(reply);
854 goto err_kfree_acts;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700855 }
856
857 ovs_lock();
858 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700859 if (unlikely(!dp)) {
860 error = -ENODEV;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700861 goto err_unlock_ovs;
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700862 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700863 /* Check if this is a duplicate flow */
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700864 flow = ovs_flow_tbl_lookup(&dp->table, &new_flow->unmasked_key);
865 if (likely(!flow)) {
866 rcu_assign_pointer(new_flow->sf_acts, acts);
867
868 /* Put flow in bucket. */
869 error = ovs_flow_tbl_insert(&dp->table, new_flow, &mask);
870 if (unlikely(error)) {
871 acts = NULL;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700872 goto err_unlock_ovs;
873 }
874
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700875 if (unlikely(reply)) {
876 error = ovs_flow_cmd_fill_info(new_flow,
877 ovs_header->dp_ifindex,
878 reply, info->snd_portid,
879 info->snd_seq, 0,
880 OVS_FLOW_CMD_NEW);
881 BUG_ON(error < 0);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700882 }
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700883 ovs_unlock();
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700884 } else {
885 struct sw_flow_actions *old_acts;
886
887 /* Bail out if we're not allowed to modify an existing flow.
888 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
889 * because Generic Netlink treats the latter as a dump
890 * request. We also accept NLM_F_EXCL in case that bug ever
891 * gets fixed.
892 */
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700893 if (unlikely(info->nlhdr->nlmsg_flags & (NLM_F_CREATE
894 | NLM_F_EXCL))) {
895 error = -EEXIST;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700896 goto err_unlock_ovs;
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700897 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700898 /* The unmasked key has to be the same for flow updates. */
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700899 if (unlikely(!ovs_flow_cmp_unmasked_key(flow, &match))) {
Alex Wang4a46b242014-06-30 20:30:29 -0700900 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
901 if (!flow) {
902 error = -ENOENT;
903 goto err_unlock_ovs;
904 }
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700905 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700906 /* Update actions. */
907 old_acts = ovsl_dereference(flow->sf_acts);
908 rcu_assign_pointer(flow->sf_acts, acts);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700909
910 if (unlikely(reply)) {
911 error = ovs_flow_cmd_fill_info(flow,
912 ovs_header->dp_ifindex,
913 reply, info->snd_portid,
914 info->snd_seq, 0,
915 OVS_FLOW_CMD_NEW);
916 BUG_ON(error < 0);
917 }
918 ovs_unlock();
919
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700920 ovs_nla_free_flow_actions(old_acts);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700921 ovs_flow_free(new_flow, false);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700922 }
923
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700924 if (reply)
925 ovs_notify(&dp_flow_genl_family, reply, info);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700926 return 0;
927
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700928err_unlock_ovs:
929 ovs_unlock();
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700930 kfree_skb(reply);
931err_kfree_acts:
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700932 kfree(acts);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700933err_kfree_flow:
934 ovs_flow_free(new_flow, false);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700935error:
936 return error;
937}
938
Jesse Gross6b205b22014-10-03 15:35:32 -0700939static struct sw_flow_actions *get_flow_actions(const struct nlattr *a,
940 const struct sw_flow_key *key,
941 const struct sw_flow_mask *mask)
942{
943 struct sw_flow_actions *acts;
944 struct sw_flow_key masked_key;
945 int error;
946
947 acts = ovs_nla_alloc_flow_actions(nla_len(a));
948 if (IS_ERR(acts))
949 return acts;
950
951 ovs_flow_mask_key(&masked_key, key, mask);
952 error = ovs_nla_copy_actions(a, &masked_key, 0, &acts);
953 if (error) {
954 OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
955 kfree(acts);
956 return ERR_PTR(error);
957 }
958
959 return acts;
960}
961
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700962static int ovs_flow_cmd_set(struct sk_buff *skb, struct genl_info *info)
963{
964 struct nlattr **a = info->attrs;
965 struct ovs_header *ovs_header = info->userhdr;
Jesse Gross6b205b22014-10-03 15:35:32 -0700966 struct sw_flow_key key;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700967 struct sw_flow *flow;
968 struct sw_flow_mask mask;
969 struct sk_buff *reply = NULL;
970 struct datapath *dp;
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700971 struct sw_flow_actions *old_acts = NULL, *acts = NULL;
Andy Zhou03f0d912013-08-07 20:01:00 -0700972 struct sw_flow_match match;
Jesse Grossccb13522011-10-25 19:26:31 -0700973 int error;
Jesse Grossccb13522011-10-25 19:26:31 -0700974
975 /* Extract key. */
976 error = -EINVAL;
977 if (!a[OVS_FLOW_ATTR_KEY])
978 goto error;
Andy Zhou03f0d912013-08-07 20:01:00 -0700979
980 ovs_match_init(&match, &key, &mask);
Jarno Rajahalme23dabf82014-03-27 12:35:23 -0700981 error = ovs_nla_get_match(&match,
Pravin B Shelare6445712013-10-03 18:16:47 -0700982 a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
Jesse Grossccb13522011-10-25 19:26:31 -0700983 if (error)
984 goto error;
985
986 /* Validate actions. */
987 if (a[OVS_FLOW_ATTR_ACTIONS]) {
Jesse Gross6b205b22014-10-03 15:35:32 -0700988 acts = get_flow_actions(a[OVS_FLOW_ATTR_ACTIONS], &key, &mask);
989 if (IS_ERR(acts)) {
990 error = PTR_ERR(acts);
Jesse Grossccb13522011-10-25 19:26:31 -0700991 goto error;
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700992 }
993 }
994
995 /* Can allocate before locking if have acts. */
996 if (acts) {
997 reply = ovs_flow_cmd_alloc_info(acts, info, false);
998 if (IS_ERR(reply)) {
999 error = PTR_ERR(reply);
1000 goto err_kfree_acts;
Andy Zhou03f0d912013-08-07 20:01:00 -07001001 }
Jesse Grossccb13522011-10-25 19:26:31 -07001002 }
1003
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001004 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001005 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001006 if (unlikely(!dp)) {
1007 error = -ENODEV;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001008 goto err_unlock_ovs;
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001009 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001010 /* Check that the flow exists. */
Alex Wang4a46b242014-06-30 20:30:29 -07001011 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001012 if (unlikely(!flow)) {
1013 error = -ENOENT;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001014 goto err_unlock_ovs;
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001015 }
Alex Wang4a46b242014-06-30 20:30:29 -07001016
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001017 /* Update actions, if present. */
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001018 if (likely(acts)) {
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001019 old_acts = ovsl_dereference(flow->sf_acts);
Jesse Grossccb13522011-10-25 19:26:31 -07001020 rcu_assign_pointer(flow->sf_acts, acts);
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001021
1022 if (unlikely(reply)) {
1023 error = ovs_flow_cmd_fill_info(flow,
1024 ovs_header->dp_ifindex,
1025 reply, info->snd_portid,
1026 info->snd_seq, 0,
1027 OVS_FLOW_CMD_NEW);
1028 BUG_ON(error < 0);
1029 }
1030 } else {
1031 /* Could not alloc without acts before locking. */
1032 reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex,
1033 info, OVS_FLOW_CMD_NEW, false);
1034 if (unlikely(IS_ERR(reply))) {
1035 error = PTR_ERR(reply);
1036 goto err_unlock_ovs;
1037 }
Jesse Grossccb13522011-10-25 19:26:31 -07001038 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001039
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001040 /* Clear stats. */
1041 if (a[OVS_FLOW_ATTR_CLEAR])
1042 ovs_flow_stats_clear(flow);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001043 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001044
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001045 if (reply)
1046 ovs_notify(&dp_flow_genl_family, reply, info);
1047 if (old_acts)
1048 ovs_nla_free_flow_actions(old_acts);
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -07001049
Jesse Grossccb13522011-10-25 19:26:31 -07001050 return 0;
1051
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001052err_unlock_ovs:
1053 ovs_unlock();
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001054 kfree_skb(reply);
1055err_kfree_acts:
Pravin B Shelar74f84a52013-06-17 17:50:12 -07001056 kfree(acts);
Jesse Grossccb13522011-10-25 19:26:31 -07001057error:
1058 return error;
1059}
1060
1061static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1062{
1063 struct nlattr **a = info->attrs;
1064 struct ovs_header *ovs_header = info->userhdr;
1065 struct sw_flow_key key;
1066 struct sk_buff *reply;
1067 struct sw_flow *flow;
1068 struct datapath *dp;
Andy Zhou03f0d912013-08-07 20:01:00 -07001069 struct sw_flow_match match;
Jesse Grossccb13522011-10-25 19:26:31 -07001070 int err;
Jesse Grossccb13522011-10-25 19:26:31 -07001071
Andy Zhou03f0d912013-08-07 20:01:00 -07001072 if (!a[OVS_FLOW_ATTR_KEY]) {
1073 OVS_NLERR("Flow get message rejected, Key attribute missing.\n");
Jesse Grossccb13522011-10-25 19:26:31 -07001074 return -EINVAL;
Andy Zhou03f0d912013-08-07 20:01:00 -07001075 }
1076
1077 ovs_match_init(&match, &key, NULL);
Jarno Rajahalme23dabf82014-03-27 12:35:23 -07001078 err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL);
Jesse Grossccb13522011-10-25 19:26:31 -07001079 if (err)
1080 return err;
1081
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001082 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001083 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001084 if (!dp) {
1085 err = -ENODEV;
1086 goto unlock;
1087 }
Jesse Grossccb13522011-10-25 19:26:31 -07001088
Alex Wang4a46b242014-06-30 20:30:29 -07001089 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1090 if (!flow) {
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001091 err = -ENOENT;
1092 goto unlock;
1093 }
Jesse Grossccb13522011-10-25 19:26:31 -07001094
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -07001095 reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex, info,
1096 OVS_FLOW_CMD_NEW, true);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001097 if (IS_ERR(reply)) {
1098 err = PTR_ERR(reply);
1099 goto unlock;
1100 }
Jesse Grossccb13522011-10-25 19:26:31 -07001101
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001102 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001103 return genlmsg_reply(reply, info);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001104unlock:
1105 ovs_unlock();
1106 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001107}
1108
1109static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1110{
1111 struct nlattr **a = info->attrs;
1112 struct ovs_header *ovs_header = info->userhdr;
1113 struct sw_flow_key key;
1114 struct sk_buff *reply;
1115 struct sw_flow *flow;
1116 struct datapath *dp;
Andy Zhou03f0d912013-08-07 20:01:00 -07001117 struct sw_flow_match match;
Jesse Grossccb13522011-10-25 19:26:31 -07001118 int err;
Jesse Grossccb13522011-10-25 19:26:31 -07001119
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001120 if (likely(a[OVS_FLOW_ATTR_KEY])) {
1121 ovs_match_init(&match, &key, NULL);
1122 err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL);
1123 if (unlikely(err))
1124 return err;
1125 }
1126
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001127 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001128 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001129 if (unlikely(!dp)) {
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001130 err = -ENODEV;
1131 goto unlock;
1132 }
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001133
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001134 if (unlikely(!a[OVS_FLOW_ATTR_KEY])) {
Pravin B Shelarb637e492013-10-04 00:14:23 -07001135 err = ovs_flow_tbl_flush(&dp->table);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001136 goto unlock;
1137 }
Andy Zhou03f0d912013-08-07 20:01:00 -07001138
Alex Wang4a46b242014-06-30 20:30:29 -07001139 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1140 if (unlikely(!flow)) {
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001141 err = -ENOENT;
1142 goto unlock;
1143 }
Jesse Grossccb13522011-10-25 19:26:31 -07001144
Pravin B Shelarb637e492013-10-04 00:14:23 -07001145 ovs_flow_tbl_remove(&dp->table, flow);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001146 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001147
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001148 reply = ovs_flow_cmd_alloc_info((const struct sw_flow_actions __force *) flow->sf_acts,
1149 info, false);
1150 if (likely(reply)) {
1151 if (likely(!IS_ERR(reply))) {
1152 rcu_read_lock(); /*To keep RCU checker happy. */
1153 err = ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex,
1154 reply, info->snd_portid,
1155 info->snd_seq, 0,
1156 OVS_FLOW_CMD_DEL);
1157 rcu_read_unlock();
1158 BUG_ON(err < 0);
1159
1160 ovs_notify(&dp_flow_genl_family, reply, info);
1161 } else {
1162 netlink_set_err(sock_net(skb->sk)->genl_sock, 0, 0, PTR_ERR(reply));
1163 }
1164 }
1165
1166 ovs_flow_free(flow, true);
Jesse Grossccb13522011-10-25 19:26:31 -07001167 return 0;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001168unlock:
1169 ovs_unlock();
1170 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001171}
1172
1173static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1174{
1175 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
Pravin B Shelarb637e492013-10-04 00:14:23 -07001176 struct table_instance *ti;
Jesse Grossccb13522011-10-25 19:26:31 -07001177 struct datapath *dp;
Jesse Grossccb13522011-10-25 19:26:31 -07001178
Pravin B Shelard57170b2013-07-30 15:39:39 -07001179 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001180 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001181 if (!dp) {
Pravin B Shelard57170b2013-07-30 15:39:39 -07001182 rcu_read_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001183 return -ENODEV;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001184 }
Jesse Grossccb13522011-10-25 19:26:31 -07001185
Pravin B Shelarb637e492013-10-04 00:14:23 -07001186 ti = rcu_dereference(dp->table.ti);
Jesse Grossccb13522011-10-25 19:26:31 -07001187 for (;;) {
1188 struct sw_flow *flow;
1189 u32 bucket, obj;
1190
1191 bucket = cb->args[0];
1192 obj = cb->args[1];
Pravin B Shelarb637e492013-10-04 00:14:23 -07001193 flow = ovs_flow_tbl_dump_next(ti, &bucket, &obj);
Jesse Grossccb13522011-10-25 19:26:31 -07001194 if (!flow)
1195 break;
1196
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -07001197 if (ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex, skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001198 NETLINK_CB(cb->skb).portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001199 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1200 OVS_FLOW_CMD_NEW) < 0)
1201 break;
1202
1203 cb->args[0] = bucket;
1204 cb->args[1] = obj;
1205 }
Pravin B Shelard57170b2013-07-30 15:39:39 -07001206 rcu_read_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001207 return skb->len;
1208}
1209
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001210static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
1211 [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
1212 [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
1213 [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
1214};
1215
stephen hemminger48e48a72014-07-16 11:25:52 -07001216static const struct genl_ops dp_flow_genl_ops[] = {
Jesse Grossccb13522011-10-25 19:26:31 -07001217 { .cmd = OVS_FLOW_CMD_NEW,
1218 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1219 .policy = flow_policy,
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001220 .doit = ovs_flow_cmd_new
Jesse Grossccb13522011-10-25 19:26:31 -07001221 },
1222 { .cmd = OVS_FLOW_CMD_DEL,
1223 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1224 .policy = flow_policy,
1225 .doit = ovs_flow_cmd_del
1226 },
1227 { .cmd = OVS_FLOW_CMD_GET,
1228 .flags = 0, /* OK for unprivileged users. */
1229 .policy = flow_policy,
1230 .doit = ovs_flow_cmd_get,
1231 .dumpit = ovs_flow_cmd_dump
1232 },
1233 { .cmd = OVS_FLOW_CMD_SET,
1234 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1235 .policy = flow_policy,
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001236 .doit = ovs_flow_cmd_set,
Jesse Grossccb13522011-10-25 19:26:31 -07001237 },
1238};
1239
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001240static struct genl_family dp_flow_genl_family = {
Jesse Grossccb13522011-10-25 19:26:31 -07001241 .id = GENL_ID_GENERATE,
1242 .hdrsize = sizeof(struct ovs_header),
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001243 .name = OVS_FLOW_FAMILY,
1244 .version = OVS_FLOW_VERSION,
1245 .maxattr = OVS_FLOW_ATTR_MAX,
Pravin B Shelar3a4e0d62013-04-23 07:48:48 +00001246 .netnsok = true,
1247 .parallel_ops = true,
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001248 .ops = dp_flow_genl_ops,
1249 .n_ops = ARRAY_SIZE(dp_flow_genl_ops),
1250 .mcgrps = &ovs_dp_flow_multicast_group,
1251 .n_mcgrps = 1,
Jesse Grossccb13522011-10-25 19:26:31 -07001252};
1253
Thomas Grafc3ff8cf2013-03-29 14:46:49 +01001254static size_t ovs_dp_cmd_msg_size(void)
1255{
1256 size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
1257
1258 msgsize += nla_total_size(IFNAMSIZ);
1259 msgsize += nla_total_size(sizeof(struct ovs_dp_stats));
Andy Zhou1bd71162013-10-22 10:42:46 -07001260 msgsize += nla_total_size(sizeof(struct ovs_dp_megaflow_stats));
Daniele Di Proietto45fb9c32014-01-23 10:47:35 -08001261 msgsize += nla_total_size(sizeof(u32)); /* OVS_DP_ATTR_USER_FEATURES */
Thomas Grafc3ff8cf2013-03-29 14:46:49 +01001262
1263 return msgsize;
1264}
1265
Jarno Rajahalmebb6f9a72014-05-05 11:32:17 -07001266/* Called with ovs_mutex or RCU read lock. */
Jesse Grossccb13522011-10-25 19:26:31 -07001267static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001268 u32 portid, u32 seq, u32 flags, u8 cmd)
Jesse Grossccb13522011-10-25 19:26:31 -07001269{
1270 struct ovs_header *ovs_header;
1271 struct ovs_dp_stats dp_stats;
Andy Zhou1bd71162013-10-22 10:42:46 -07001272 struct ovs_dp_megaflow_stats dp_megaflow_stats;
Jesse Grossccb13522011-10-25 19:26:31 -07001273 int err;
1274
Eric W. Biederman15e47302012-09-07 20:12:54 +00001275 ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
Jesse Grossccb13522011-10-25 19:26:31 -07001276 flags, cmd);
1277 if (!ovs_header)
1278 goto error;
1279
1280 ovs_header->dp_ifindex = get_dpifindex(dp);
1281
Jesse Grossccb13522011-10-25 19:26:31 -07001282 err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
Jesse Grossccb13522011-10-25 19:26:31 -07001283 if (err)
1284 goto nla_put_failure;
1285
Andy Zhou1bd71162013-10-22 10:42:46 -07001286 get_dp_stats(dp, &dp_stats, &dp_megaflow_stats);
1287 if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats),
1288 &dp_stats))
1289 goto nla_put_failure;
1290
1291 if (nla_put(skb, OVS_DP_ATTR_MEGAFLOW_STATS,
1292 sizeof(struct ovs_dp_megaflow_stats),
1293 &dp_megaflow_stats))
David S. Miller028d6a62012-03-29 23:20:48 -04001294 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001295
Thomas Graf43d4be92013-12-13 15:22:18 +01001296 if (nla_put_u32(skb, OVS_DP_ATTR_USER_FEATURES, dp->user_features))
1297 goto nla_put_failure;
1298
Jesse Grossccb13522011-10-25 19:26:31 -07001299 return genlmsg_end(skb, ovs_header);
1300
1301nla_put_failure:
1302 genlmsg_cancel(skb, ovs_header);
1303error:
1304 return -EMSGSIZE;
1305}
1306
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001307static struct sk_buff *ovs_dp_cmd_alloc_info(struct genl_info *info)
Jesse Grossccb13522011-10-25 19:26:31 -07001308{
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001309 return genlmsg_new_unicast(ovs_dp_cmd_msg_size(), info, GFP_KERNEL);
Jesse Grossccb13522011-10-25 19:26:31 -07001310}
1311
Jarno Rajahalmebb6f9a72014-05-05 11:32:17 -07001312/* Called with rcu_read_lock or ovs_mutex. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001313static struct datapath *lookup_datapath(struct net *net,
1314 struct ovs_header *ovs_header,
Jesse Grossccb13522011-10-25 19:26:31 -07001315 struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1316{
1317 struct datapath *dp;
1318
1319 if (!a[OVS_DP_ATTR_NAME])
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001320 dp = get_dp(net, ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001321 else {
1322 struct vport *vport;
1323
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001324 vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
Jesse Grossccb13522011-10-25 19:26:31 -07001325 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
Jesse Grossccb13522011-10-25 19:26:31 -07001326 }
1327 return dp ? dp : ERR_PTR(-ENODEV);
1328}
1329
Thomas Graf44da5ae2013-12-13 15:22:19 +01001330static void ovs_dp_reset_user_features(struct sk_buff *skb, struct genl_info *info)
1331{
1332 struct datapath *dp;
1333
1334 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
Jiri Pirko3c7eacf2014-02-14 11:42:36 +01001335 if (IS_ERR(dp))
Thomas Graf44da5ae2013-12-13 15:22:19 +01001336 return;
1337
1338 WARN(dp->user_features, "Dropping previously announced user features\n");
1339 dp->user_features = 0;
1340}
1341
Thomas Graf43d4be92013-12-13 15:22:18 +01001342static void ovs_dp_change(struct datapath *dp, struct nlattr **a)
1343{
1344 if (a[OVS_DP_ATTR_USER_FEATURES])
1345 dp->user_features = nla_get_u32(a[OVS_DP_ATTR_USER_FEATURES]);
1346}
1347
Jesse Grossccb13522011-10-25 19:26:31 -07001348static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1349{
1350 struct nlattr **a = info->attrs;
1351 struct vport_parms parms;
1352 struct sk_buff *reply;
1353 struct datapath *dp;
1354 struct vport *vport;
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001355 struct ovs_net *ovs_net;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001356 int err, i;
Jesse Grossccb13522011-10-25 19:26:31 -07001357
1358 err = -EINVAL;
1359 if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1360 goto err;
1361
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001362 reply = ovs_dp_cmd_alloc_info(info);
1363 if (!reply)
1364 return -ENOMEM;
Jesse Grossccb13522011-10-25 19:26:31 -07001365
1366 err = -ENOMEM;
1367 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1368 if (dp == NULL)
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001369 goto err_free_reply;
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001370
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001371 ovs_dp_set_net(dp, hold_net(sock_net(skb->sk)));
Jesse Grossccb13522011-10-25 19:26:31 -07001372
1373 /* Allocate table. */
Pravin B Shelarb637e492013-10-04 00:14:23 -07001374 err = ovs_flow_tbl_init(&dp->table);
1375 if (err)
Jesse Grossccb13522011-10-25 19:26:31 -07001376 goto err_free_dp;
1377
WANG Cong1c213bd2014-02-13 11:46:28 -08001378 dp->stats_percpu = netdev_alloc_pcpu_stats(struct dp_stats_percpu);
Jesse Grossccb13522011-10-25 19:26:31 -07001379 if (!dp->stats_percpu) {
1380 err = -ENOMEM;
1381 goto err_destroy_table;
1382 }
1383
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001384 dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
Pravin B Shelare6445712013-10-03 18:16:47 -07001385 GFP_KERNEL);
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001386 if (!dp->ports) {
1387 err = -ENOMEM;
1388 goto err_destroy_percpu;
1389 }
1390
1391 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1392 INIT_HLIST_HEAD(&dp->ports[i]);
1393
Jesse Grossccb13522011-10-25 19:26:31 -07001394 /* Set up our datapath device. */
1395 parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1396 parms.type = OVS_VPORT_TYPE_INTERNAL;
1397 parms.options = NULL;
1398 parms.dp = dp;
1399 parms.port_no = OVSP_LOCAL;
Alex Wang5cd667b2014-07-17 15:14:13 -07001400 parms.upcall_portids = a[OVS_DP_ATTR_UPCALL_PID];
Jesse Grossccb13522011-10-25 19:26:31 -07001401
Thomas Graf43d4be92013-12-13 15:22:18 +01001402 ovs_dp_change(dp, a);
1403
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001404 /* So far only local changes have been made, now need the lock. */
1405 ovs_lock();
1406
Jesse Grossccb13522011-10-25 19:26:31 -07001407 vport = new_vport(&parms);
1408 if (IS_ERR(vport)) {
1409 err = PTR_ERR(vport);
1410 if (err == -EBUSY)
1411 err = -EEXIST;
1412
Thomas Graf44da5ae2013-12-13 15:22:19 +01001413 if (err == -EEXIST) {
1414 /* An outdated user space instance that does not understand
1415 * the concept of user_features has attempted to create a new
1416 * datapath and is likely to reuse it. Drop all user features.
1417 */
1418 if (info->genlhdr->version < OVS_DP_VER_FEATURES)
1419 ovs_dp_reset_user_features(skb, info);
1420 }
1421
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001422 goto err_destroy_ports_array;
Jesse Grossccb13522011-10-25 19:26:31 -07001423 }
1424
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001425 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1426 info->snd_seq, 0, OVS_DP_CMD_NEW);
1427 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001428
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001429 ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001430 list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001431
1432 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001433
Johannes Berg2a94fe42013-11-19 15:19:39 +01001434 ovs_notify(&dp_datapath_genl_family, reply, info);
Jesse Grossccb13522011-10-25 19:26:31 -07001435 return 0;
1436
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001437err_destroy_ports_array:
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001438 ovs_unlock();
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001439 kfree(dp->ports);
Jesse Grossccb13522011-10-25 19:26:31 -07001440err_destroy_percpu:
1441 free_percpu(dp->stats_percpu);
1442err_destroy_table:
Andy Zhoue80857c2014-01-21 09:31:04 -08001443 ovs_flow_tbl_destroy(&dp->table, false);
Jesse Grossccb13522011-10-25 19:26:31 -07001444err_free_dp:
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001445 release_net(ovs_dp_get_net(dp));
Jesse Grossccb13522011-10-25 19:26:31 -07001446 kfree(dp);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001447err_free_reply:
1448 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001449err:
1450 return err;
1451}
1452
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001453/* Called with ovs_mutex. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001454static void __dp_destroy(struct datapath *dp)
Jesse Grossccb13522011-10-25 19:26:31 -07001455{
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001456 int i;
Jesse Grossccb13522011-10-25 19:26:31 -07001457
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001458 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1459 struct vport *vport;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001460 struct hlist_node *n;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001461
Sasha Levinb67bfe02013-02-27 17:06:00 -08001462 hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001463 if (vport->port_no != OVSP_LOCAL)
1464 ovs_dp_detach_port(vport);
1465 }
Jesse Grossccb13522011-10-25 19:26:31 -07001466
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001467 list_del_rcu(&dp->list_node);
Jesse Grossccb13522011-10-25 19:26:31 -07001468
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001469 /* OVSP_LOCAL is datapath internal port. We need to make sure that
Andy Zhoue80857c2014-01-21 09:31:04 -08001470 * all ports in datapath are destroyed first before freeing datapath.
Jesse Grossccb13522011-10-25 19:26:31 -07001471 */
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001472 ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
Jesse Grossccb13522011-10-25 19:26:31 -07001473
Andy Zhoue80857c2014-01-21 09:31:04 -08001474 /* RCU destroy the flow table */
1475 ovs_flow_tbl_destroy(&dp->table, true);
1476
Jesse Grossccb13522011-10-25 19:26:31 -07001477 call_rcu(&dp->rcu, destroy_dp_rcu);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001478}
1479
1480static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1481{
1482 struct sk_buff *reply;
1483 struct datapath *dp;
1484 int err;
1485
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001486 reply = ovs_dp_cmd_alloc_info(info);
1487 if (!reply)
1488 return -ENOMEM;
1489
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001490 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001491 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1492 err = PTR_ERR(dp);
1493 if (IS_ERR(dp))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001494 goto err_unlock_free;
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001495
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001496 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1497 info->snd_seq, 0, OVS_DP_CMD_DEL);
1498 BUG_ON(err < 0);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001499
1500 __dp_destroy(dp);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001501 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001502
Johannes Berg2a94fe42013-11-19 15:19:39 +01001503 ovs_notify(&dp_datapath_genl_family, reply, info);
Jesse Grossccb13522011-10-25 19:26:31 -07001504
1505 return 0;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001506
1507err_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001508 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001509 kfree_skb(reply);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001510 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001511}
1512
1513static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1514{
1515 struct sk_buff *reply;
1516 struct datapath *dp;
1517 int err;
1518
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001519 reply = ovs_dp_cmd_alloc_info(info);
1520 if (!reply)
1521 return -ENOMEM;
1522
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001523 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001524 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001525 err = PTR_ERR(dp);
Jesse Grossccb13522011-10-25 19:26:31 -07001526 if (IS_ERR(dp))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001527 goto err_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001528
Thomas Graf43d4be92013-12-13 15:22:18 +01001529 ovs_dp_change(dp, info->attrs);
1530
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001531 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1532 info->snd_seq, 0, OVS_DP_CMD_NEW);
1533 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001534
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001535 ovs_unlock();
Johannes Berg2a94fe42013-11-19 15:19:39 +01001536 ovs_notify(&dp_datapath_genl_family, reply, info);
Jesse Grossccb13522011-10-25 19:26:31 -07001537
1538 return 0;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001539
1540err_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001541 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001542 kfree_skb(reply);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001543 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001544}
1545
1546static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1547{
1548 struct sk_buff *reply;
1549 struct datapath *dp;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001550 int err;
Jesse Grossccb13522011-10-25 19:26:31 -07001551
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001552 reply = ovs_dp_cmd_alloc_info(info);
1553 if (!reply)
1554 return -ENOMEM;
1555
1556 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001557 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001558 if (IS_ERR(dp)) {
1559 err = PTR_ERR(dp);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001560 goto err_unlock_free;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001561 }
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001562 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1563 info->snd_seq, 0, OVS_DP_CMD_NEW);
1564 BUG_ON(err < 0);
1565 rcu_read_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001566
Jesse Grossccb13522011-10-25 19:26:31 -07001567 return genlmsg_reply(reply, info);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001568
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001569err_unlock_free:
1570 rcu_read_unlock();
1571 kfree_skb(reply);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001572 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001573}
1574
1575static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1576{
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001577 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
Jesse Grossccb13522011-10-25 19:26:31 -07001578 struct datapath *dp;
1579 int skip = cb->args[0];
1580 int i = 0;
1581
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001582 rcu_read_lock();
1583 list_for_each_entry_rcu(dp, &ovs_net->dps, list_node) {
Ben Pfaff77676fd2012-01-17 13:33:39 +00001584 if (i >= skip &&
Eric W. Biederman15e47302012-09-07 20:12:54 +00001585 ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001586 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1587 OVS_DP_CMD_NEW) < 0)
1588 break;
1589 i++;
1590 }
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001591 rcu_read_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001592
1593 cb->args[0] = i;
1594
1595 return skb->len;
1596}
1597
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001598static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1599 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1600 [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1601 [OVS_DP_ATTR_USER_FEATURES] = { .type = NLA_U32 },
1602};
1603
stephen hemminger48e48a72014-07-16 11:25:52 -07001604static const struct genl_ops dp_datapath_genl_ops[] = {
Jesse Grossccb13522011-10-25 19:26:31 -07001605 { .cmd = OVS_DP_CMD_NEW,
1606 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1607 .policy = datapath_policy,
1608 .doit = ovs_dp_cmd_new
1609 },
1610 { .cmd = OVS_DP_CMD_DEL,
1611 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1612 .policy = datapath_policy,
1613 .doit = ovs_dp_cmd_del
1614 },
1615 { .cmd = OVS_DP_CMD_GET,
1616 .flags = 0, /* OK for unprivileged users. */
1617 .policy = datapath_policy,
1618 .doit = ovs_dp_cmd_get,
1619 .dumpit = ovs_dp_cmd_dump
1620 },
1621 { .cmd = OVS_DP_CMD_SET,
1622 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1623 .policy = datapath_policy,
1624 .doit = ovs_dp_cmd_set,
1625 },
1626};
1627
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001628static struct genl_family dp_datapath_genl_family = {
Jesse Grossccb13522011-10-25 19:26:31 -07001629 .id = GENL_ID_GENERATE,
1630 .hdrsize = sizeof(struct ovs_header),
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001631 .name = OVS_DATAPATH_FAMILY,
1632 .version = OVS_DATAPATH_VERSION,
1633 .maxattr = OVS_DP_ATTR_MAX,
Pravin B Shelar3a4e0d62013-04-23 07:48:48 +00001634 .netnsok = true,
1635 .parallel_ops = true,
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001636 .ops = dp_datapath_genl_ops,
1637 .n_ops = ARRAY_SIZE(dp_datapath_genl_ops),
1638 .mcgrps = &ovs_dp_datapath_multicast_group,
1639 .n_mcgrps = 1,
Jesse Grossccb13522011-10-25 19:26:31 -07001640};
1641
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001642/* Called with ovs_mutex or RCU read lock. */
Jesse Grossccb13522011-10-25 19:26:31 -07001643static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001644 u32 portid, u32 seq, u32 flags, u8 cmd)
Jesse Grossccb13522011-10-25 19:26:31 -07001645{
1646 struct ovs_header *ovs_header;
1647 struct ovs_vport_stats vport_stats;
1648 int err;
1649
Eric W. Biederman15e47302012-09-07 20:12:54 +00001650 ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
Jesse Grossccb13522011-10-25 19:26:31 -07001651 flags, cmd);
1652 if (!ovs_header)
1653 return -EMSGSIZE;
1654
1655 ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1656
David S. Miller028d6a62012-03-29 23:20:48 -04001657 if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
1658 nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
Alex Wang5cd667b2014-07-17 15:14:13 -07001659 nla_put_string(skb, OVS_VPORT_ATTR_NAME,
1660 vport->ops->get_name(vport)))
David S. Miller028d6a62012-03-29 23:20:48 -04001661 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001662
1663 ovs_vport_get_stats(vport, &vport_stats);
David S. Miller028d6a62012-03-29 23:20:48 -04001664 if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
1665 &vport_stats))
1666 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001667
Alex Wang5cd667b2014-07-17 15:14:13 -07001668 if (ovs_vport_get_upcall_portids(vport, skb))
1669 goto nla_put_failure;
1670
Jesse Grossccb13522011-10-25 19:26:31 -07001671 err = ovs_vport_get_options(vport, skb);
1672 if (err == -EMSGSIZE)
1673 goto error;
1674
1675 return genlmsg_end(skb, ovs_header);
1676
1677nla_put_failure:
1678 err = -EMSGSIZE;
1679error:
1680 genlmsg_cancel(skb, ovs_header);
1681 return err;
1682}
1683
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001684static struct sk_buff *ovs_vport_cmd_alloc_info(void)
1685{
1686 return nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1687}
1688
1689/* Called with ovs_mutex, only via ovs_dp_notify_wq(). */
Eric W. Biederman15e47302012-09-07 20:12:54 +00001690struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001691 u32 seq, u8 cmd)
1692{
1693 struct sk_buff *skb;
1694 int retval;
1695
1696 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1697 if (!skb)
1698 return ERR_PTR(-ENOMEM);
1699
Eric W. Biederman15e47302012-09-07 20:12:54 +00001700 retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
Jesse Grossa9341512013-03-26 15:48:38 -07001701 BUG_ON(retval < 0);
1702
Jesse Grossccb13522011-10-25 19:26:31 -07001703 return skb;
1704}
1705
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001706/* Called with ovs_mutex or RCU read lock. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001707static struct vport *lookup_vport(struct net *net,
1708 struct ovs_header *ovs_header,
Jesse Grossccb13522011-10-25 19:26:31 -07001709 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1710{
1711 struct datapath *dp;
1712 struct vport *vport;
1713
1714 if (a[OVS_VPORT_ATTR_NAME]) {
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001715 vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
Jesse Grossccb13522011-10-25 19:26:31 -07001716 if (!vport)
1717 return ERR_PTR(-ENODEV);
Ben Pfaff651a68e2012-03-06 15:04:04 -08001718 if (ovs_header->dp_ifindex &&
1719 ovs_header->dp_ifindex != get_dpifindex(vport->dp))
1720 return ERR_PTR(-ENODEV);
Jesse Grossccb13522011-10-25 19:26:31 -07001721 return vport;
1722 } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
1723 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1724
1725 if (port_no >= DP_MAX_PORTS)
1726 return ERR_PTR(-EFBIG);
1727
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001728 dp = get_dp(net, ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001729 if (!dp)
1730 return ERR_PTR(-ENODEV);
1731
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001732 vport = ovs_vport_ovsl_rcu(dp, port_no);
Jesse Grossccb13522011-10-25 19:26:31 -07001733 if (!vport)
Jarno Rajahalme14408db2013-01-09 14:27:35 -08001734 return ERR_PTR(-ENODEV);
Jesse Grossccb13522011-10-25 19:26:31 -07001735 return vport;
1736 } else
1737 return ERR_PTR(-EINVAL);
1738}
1739
1740static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
1741{
1742 struct nlattr **a = info->attrs;
1743 struct ovs_header *ovs_header = info->userhdr;
1744 struct vport_parms parms;
1745 struct sk_buff *reply;
1746 struct vport *vport;
1747 struct datapath *dp;
1748 u32 port_no;
1749 int err;
1750
Jesse Grossccb13522011-10-25 19:26:31 -07001751 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
1752 !a[OVS_VPORT_ATTR_UPCALL_PID])
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001753 return -EINVAL;
1754
1755 port_no = a[OVS_VPORT_ATTR_PORT_NO]
1756 ? nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]) : 0;
1757 if (port_no >= DP_MAX_PORTS)
1758 return -EFBIG;
1759
1760 reply = ovs_vport_cmd_alloc_info();
1761 if (!reply)
1762 return -ENOMEM;
Jesse Grossccb13522011-10-25 19:26:31 -07001763
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001764 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001765 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001766 err = -ENODEV;
1767 if (!dp)
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001768 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001769
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001770 if (port_no) {
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001771 vport = ovs_vport_ovsl(dp, port_no);
Jesse Grossccb13522011-10-25 19:26:31 -07001772 err = -EBUSY;
1773 if (vport)
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001774 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001775 } else {
1776 for (port_no = 1; ; port_no++) {
1777 if (port_no >= DP_MAX_PORTS) {
1778 err = -EFBIG;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001779 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001780 }
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001781 vport = ovs_vport_ovsl(dp, port_no);
Jesse Grossccb13522011-10-25 19:26:31 -07001782 if (!vport)
1783 break;
1784 }
1785 }
1786
1787 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
1788 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1789 parms.options = a[OVS_VPORT_ATTR_OPTIONS];
1790 parms.dp = dp;
1791 parms.port_no = port_no;
Alex Wang5cd667b2014-07-17 15:14:13 -07001792 parms.upcall_portids = a[OVS_VPORT_ATTR_UPCALL_PID];
Jesse Grossccb13522011-10-25 19:26:31 -07001793
1794 vport = new_vport(&parms);
1795 err = PTR_ERR(vport);
1796 if (IS_ERR(vport))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001797 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001798
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001799 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1800 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1801 BUG_ON(err < 0);
1802 ovs_unlock();
Thomas Grafed661182013-03-29 14:46:50 +01001803
Johannes Berg2a94fe42013-11-19 15:19:39 +01001804 ovs_notify(&dp_vport_genl_family, reply, info);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001805 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001806
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001807exit_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001808 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001809 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001810 return err;
1811}
1812
1813static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
1814{
1815 struct nlattr **a = info->attrs;
1816 struct sk_buff *reply;
1817 struct vport *vport;
1818 int err;
1819
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001820 reply = ovs_vport_cmd_alloc_info();
1821 if (!reply)
1822 return -ENOMEM;
1823
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001824 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001825 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
Jesse Grossccb13522011-10-25 19:26:31 -07001826 err = PTR_ERR(vport);
1827 if (IS_ERR(vport))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001828 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001829
Jesse Grossccb13522011-10-25 19:26:31 -07001830 if (a[OVS_VPORT_ATTR_TYPE] &&
Jesse Grossf44f3402013-05-13 08:15:26 -07001831 nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
Jesse Grossccb13522011-10-25 19:26:31 -07001832 err = -EINVAL;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001833 goto exit_unlock_free;
Jesse Grossa9341512013-03-26 15:48:38 -07001834 }
1835
Jesse Grossf44f3402013-05-13 08:15:26 -07001836 if (a[OVS_VPORT_ATTR_OPTIONS]) {
Jesse Grossccb13522011-10-25 19:26:31 -07001837 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
Jesse Grossf44f3402013-05-13 08:15:26 -07001838 if (err)
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001839 goto exit_unlock_free;
Jesse Grossf44f3402013-05-13 08:15:26 -07001840 }
Jesse Grossa9341512013-03-26 15:48:38 -07001841
Alex Wang5cd667b2014-07-17 15:14:13 -07001842
1843 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
1844 struct nlattr *ids = a[OVS_VPORT_ATTR_UPCALL_PID];
1845
1846 err = ovs_vport_set_upcall_portids(vport, ids);
1847 if (err)
1848 goto exit_unlock_free;
1849 }
Jesse Grossccb13522011-10-25 19:26:31 -07001850
Jesse Grossa9341512013-03-26 15:48:38 -07001851 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1852 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1853 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001854
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001855 ovs_unlock();
Johannes Berg2a94fe42013-11-19 15:19:39 +01001856 ovs_notify(&dp_vport_genl_family, reply, info);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001857 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001858
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001859exit_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001860 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001861 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001862 return err;
1863}
1864
1865static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
1866{
1867 struct nlattr **a = info->attrs;
1868 struct sk_buff *reply;
1869 struct vport *vport;
1870 int err;
1871
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001872 reply = ovs_vport_cmd_alloc_info();
1873 if (!reply)
1874 return -ENOMEM;
1875
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001876 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001877 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
Jesse Grossccb13522011-10-25 19:26:31 -07001878 err = PTR_ERR(vport);
1879 if (IS_ERR(vport))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001880 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001881
1882 if (vport->port_no == OVSP_LOCAL) {
1883 err = -EINVAL;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001884 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001885 }
1886
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001887 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1888 info->snd_seq, 0, OVS_VPORT_CMD_DEL);
1889 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001890 ovs_dp_detach_port(vport);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001891 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001892
Johannes Berg2a94fe42013-11-19 15:19:39 +01001893 ovs_notify(&dp_vport_genl_family, reply, info);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001894 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001895
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001896exit_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001897 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001898 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001899 return err;
1900}
1901
1902static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
1903{
1904 struct nlattr **a = info->attrs;
1905 struct ovs_header *ovs_header = info->userhdr;
1906 struct sk_buff *reply;
1907 struct vport *vport;
1908 int err;
1909
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001910 reply = ovs_vport_cmd_alloc_info();
1911 if (!reply)
1912 return -ENOMEM;
1913
Jesse Grossccb13522011-10-25 19:26:31 -07001914 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001915 vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
Jesse Grossccb13522011-10-25 19:26:31 -07001916 err = PTR_ERR(vport);
1917 if (IS_ERR(vport))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001918 goto exit_unlock_free;
1919 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1920 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1921 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001922 rcu_read_unlock();
1923
1924 return genlmsg_reply(reply, info);
1925
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001926exit_unlock_free:
Jesse Grossccb13522011-10-25 19:26:31 -07001927 rcu_read_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001928 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001929 return err;
1930}
1931
1932static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1933{
1934 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1935 struct datapath *dp;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001936 int bucket = cb->args[0], skip = cb->args[1];
1937 int i, j = 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001938
Jesse Grossccb13522011-10-25 19:26:31 -07001939 rcu_read_lock();
Jarno Rajahalme42ee19e2014-02-15 17:42:29 -08001940 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1941 if (!dp) {
1942 rcu_read_unlock();
1943 return -ENODEV;
1944 }
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001945 for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
Jesse Grossccb13522011-10-25 19:26:31 -07001946 struct vport *vport;
1947
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001948 j = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001949 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001950 if (j >= skip &&
1951 ovs_vport_cmd_fill_info(vport, skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001952 NETLINK_CB(cb->skb).portid,
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001953 cb->nlh->nlmsg_seq,
1954 NLM_F_MULTI,
1955 OVS_VPORT_CMD_NEW) < 0)
1956 goto out;
Jesse Grossccb13522011-10-25 19:26:31 -07001957
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001958 j++;
1959 }
1960 skip = 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001961 }
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001962out:
Jesse Grossccb13522011-10-25 19:26:31 -07001963 rcu_read_unlock();
1964
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001965 cb->args[0] = i;
1966 cb->args[1] = j;
Jesse Grossccb13522011-10-25 19:26:31 -07001967
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001968 return skb->len;
Jesse Grossccb13522011-10-25 19:26:31 -07001969}
1970
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001971static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
1972 [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1973 [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
1974 [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
1975 [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
1976 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1977 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
1978};
1979
stephen hemminger48e48a72014-07-16 11:25:52 -07001980static const struct genl_ops dp_vport_genl_ops[] = {
Jesse Grossccb13522011-10-25 19:26:31 -07001981 { .cmd = OVS_VPORT_CMD_NEW,
1982 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1983 .policy = vport_policy,
1984 .doit = ovs_vport_cmd_new
1985 },
1986 { .cmd = OVS_VPORT_CMD_DEL,
1987 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1988 .policy = vport_policy,
1989 .doit = ovs_vport_cmd_del
1990 },
1991 { .cmd = OVS_VPORT_CMD_GET,
1992 .flags = 0, /* OK for unprivileged users. */
1993 .policy = vport_policy,
1994 .doit = ovs_vport_cmd_get,
1995 .dumpit = ovs_vport_cmd_dump
1996 },
1997 { .cmd = OVS_VPORT_CMD_SET,
1998 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1999 .policy = vport_policy,
2000 .doit = ovs_vport_cmd_set,
2001 },
2002};
2003
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002004struct genl_family dp_vport_genl_family = {
2005 .id = GENL_ID_GENERATE,
2006 .hdrsize = sizeof(struct ovs_header),
2007 .name = OVS_VPORT_FAMILY,
2008 .version = OVS_VPORT_VERSION,
2009 .maxattr = OVS_VPORT_ATTR_MAX,
2010 .netnsok = true,
2011 .parallel_ops = true,
2012 .ops = dp_vport_genl_ops,
2013 .n_ops = ARRAY_SIZE(dp_vport_genl_ops),
2014 .mcgrps = &ovs_dp_vport_multicast_group,
2015 .n_mcgrps = 1,
Jesse Grossccb13522011-10-25 19:26:31 -07002016};
2017
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002018static struct genl_family * const dp_genl_families[] = {
2019 &dp_datapath_genl_family,
2020 &dp_vport_genl_family,
2021 &dp_flow_genl_family,
2022 &dp_packet_genl_family,
Jesse Grossccb13522011-10-25 19:26:31 -07002023};
2024
2025static void dp_unregister_genl(int n_families)
2026{
2027 int i;
2028
2029 for (i = 0; i < n_families; i++)
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002030 genl_unregister_family(dp_genl_families[i]);
Jesse Grossccb13522011-10-25 19:26:31 -07002031}
2032
2033static int dp_register_genl(void)
2034{
Jesse Grossccb13522011-10-25 19:26:31 -07002035 int err;
2036 int i;
2037
Jesse Grossccb13522011-10-25 19:26:31 -07002038 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
Jesse Grossccb13522011-10-25 19:26:31 -07002039
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002040 err = genl_register_family(dp_genl_families[i]);
Jesse Grossccb13522011-10-25 19:26:31 -07002041 if (err)
2042 goto error;
Jesse Grossccb13522011-10-25 19:26:31 -07002043 }
2044
2045 return 0;
2046
2047error:
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002048 dp_unregister_genl(i);
Jesse Grossccb13522011-10-25 19:26:31 -07002049 return err;
2050}
2051
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002052static int __net_init ovs_init_net(struct net *net)
2053{
2054 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2055
2056 INIT_LIST_HEAD(&ovs_net->dps);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002057 INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002058 return 0;
2059}
2060
2061static void __net_exit ovs_exit_net(struct net *net)
2062{
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002063 struct datapath *dp, *dp_next;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002064 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002065
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002066 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002067 list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
2068 __dp_destroy(dp);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002069 ovs_unlock();
2070
2071 cancel_work_sync(&ovs_net->dp_notify_work);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002072}
2073
2074static struct pernet_operations ovs_net_ops = {
2075 .init = ovs_init_net,
2076 .exit = ovs_exit_net,
2077 .id = &ovs_net_id,
2078 .size = sizeof(struct ovs_net),
2079};
2080
Jesse Grossccb13522011-10-25 19:26:31 -07002081static int __init dp_init(void)
2082{
Jesse Grossccb13522011-10-25 19:26:31 -07002083 int err;
2084
YOSHIFUJI Hideaki / 吉藤英明3523b292013-01-09 07:19:55 +00002085 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
Jesse Grossccb13522011-10-25 19:26:31 -07002086
2087 pr_info("Open vSwitch switching datapath\n");
2088
Andy Zhou971427f32014-09-15 19:37:25 -07002089 err = action_fifos_init();
Jesse Grossccb13522011-10-25 19:26:31 -07002090 if (err)
2091 goto error;
2092
Andy Zhou971427f32014-09-15 19:37:25 -07002093 err = ovs_internal_dev_rtnl_link_register();
2094 if (err)
2095 goto error_action_fifos_exit;
2096
Jiri Pirko5b9e7e12014-06-26 09:58:26 +02002097 err = ovs_flow_init();
2098 if (err)
2099 goto error_unreg_rtnl_link;
2100
Jesse Grossccb13522011-10-25 19:26:31 -07002101 err = ovs_vport_init();
2102 if (err)
2103 goto error_flow_exit;
2104
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002105 err = register_pernet_device(&ovs_net_ops);
Jesse Grossccb13522011-10-25 19:26:31 -07002106 if (err)
2107 goto error_vport_exit;
2108
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002109 err = register_netdevice_notifier(&ovs_dp_device_notifier);
2110 if (err)
2111 goto error_netns_exit;
2112
Jesse Grossccb13522011-10-25 19:26:31 -07002113 err = dp_register_genl();
2114 if (err < 0)
2115 goto error_unreg_notifier;
2116
Jesse Grossccb13522011-10-25 19:26:31 -07002117 return 0;
2118
2119error_unreg_notifier:
2120 unregister_netdevice_notifier(&ovs_dp_device_notifier);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002121error_netns_exit:
2122 unregister_pernet_device(&ovs_net_ops);
Jesse Grossccb13522011-10-25 19:26:31 -07002123error_vport_exit:
2124 ovs_vport_exit();
2125error_flow_exit:
2126 ovs_flow_exit();
Jiri Pirko5b9e7e12014-06-26 09:58:26 +02002127error_unreg_rtnl_link:
2128 ovs_internal_dev_rtnl_link_unregister();
Andy Zhou971427f32014-09-15 19:37:25 -07002129error_action_fifos_exit:
2130 action_fifos_exit();
Jesse Grossccb13522011-10-25 19:26:31 -07002131error:
2132 return err;
2133}
2134
2135static void dp_cleanup(void)
2136{
Jesse Grossccb13522011-10-25 19:26:31 -07002137 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
2138 unregister_netdevice_notifier(&ovs_dp_device_notifier);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002139 unregister_pernet_device(&ovs_net_ops);
2140 rcu_barrier();
Jesse Grossccb13522011-10-25 19:26:31 -07002141 ovs_vport_exit();
2142 ovs_flow_exit();
Jiri Pirko5b9e7e12014-06-26 09:58:26 +02002143 ovs_internal_dev_rtnl_link_unregister();
Andy Zhou971427f32014-09-15 19:37:25 -07002144 action_fifos_exit();
Jesse Grossccb13522011-10-25 19:26:31 -07002145}
2146
2147module_init(dp_init);
2148module_exit(dp_cleanup);
2149
2150MODULE_DESCRIPTION("Open vSwitch switching datapath");
2151MODULE_LICENSE("GPL");