blob: e6d7255183eba31267ec29705441a019681ca617 [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);
Florian Westphal330966e2014-10-20 13:49:17 +0200327 if (segs == NULL)
328 return -EINVAL;
Jesse Grossccb13522011-10-25 19:26:31 -0700329
330 /* Queue all of the segments. */
331 skb = segs;
332 do {
Thomas Graf8055a892013-12-13 15:22:20 +0100333 err = queue_userspace_packet(dp, skb, upcall_info);
Jesse Grossccb13522011-10-25 19:26:31 -0700334 if (err)
335 break;
336
Ben Pfaffa1b5d0d2012-07-20 14:47:54 -0700337 if (skb == segs && gso_type & SKB_GSO_UDP) {
Jesse Grossccb13522011-10-25 19:26:31 -0700338 /* The initial flow key extracted by ovs_flow_extract()
339 * in this case is for a first fragment, so we need to
340 * properly mark later fragments.
341 */
342 later_key = *upcall_info->key;
343 later_key.ip.frag = OVS_FRAG_TYPE_LATER;
344
345 later_info = *upcall_info;
346 later_info.key = &later_key;
347 upcall_info = &later_info;
348 }
349 } while ((skb = skb->next));
350
351 /* Free all of the segments. */
352 skb = segs;
353 do {
354 nskb = skb->next;
355 if (err)
356 kfree_skb(skb);
357 else
358 consume_skb(skb);
359 } while ((skb = nskb));
360 return err;
361}
362
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100363static size_t key_attr_size(void)
364{
365 return nla_total_size(4) /* OVS_KEY_ATTR_PRIORITY */
Pravin B Shelar7d5437c2013-06-17 17:50:18 -0700366 + nla_total_size(0) /* OVS_KEY_ATTR_TUNNEL */
367 + nla_total_size(8) /* OVS_TUNNEL_KEY_ATTR_ID */
368 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_SRC */
369 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_DST */
370 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TOS */
371 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TTL */
372 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT */
373 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_CSUM */
Jesse Gross67fa0342014-10-03 15:35:30 -0700374 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_OAM */
Jesse Grossf5796682014-10-03 15:35:33 -0700375 + nla_total_size(256) /* OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS */
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100376 + nla_total_size(4) /* OVS_KEY_ATTR_IN_PORT */
377 + nla_total_size(4) /* OVS_KEY_ATTR_SKB_MARK */
378 + nla_total_size(12) /* OVS_KEY_ATTR_ETHERNET */
379 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
380 + nla_total_size(4) /* OVS_KEY_ATTR_8021Q */
381 + nla_total_size(0) /* OVS_KEY_ATTR_ENCAP */
382 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
383 + nla_total_size(40) /* OVS_KEY_ATTR_IPV6 */
384 + nla_total_size(2) /* OVS_KEY_ATTR_ICMPV6 */
385 + nla_total_size(28); /* OVS_KEY_ATTR_ND */
386}
387
Thomas Grafbda56f12013-12-13 15:22:21 +0100388static size_t upcall_msg_size(const struct nlattr *userdata,
389 unsigned int hdrlen)
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100390{
391 size_t size = NLMSG_ALIGN(sizeof(struct ovs_header))
Thomas Grafbda56f12013-12-13 15:22:21 +0100392 + nla_total_size(hdrlen) /* OVS_PACKET_ATTR_PACKET */
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100393 + nla_total_size(key_attr_size()); /* OVS_PACKET_ATTR_KEY */
394
395 /* OVS_PACKET_ATTR_USERDATA */
396 if (userdata)
397 size += NLA_ALIGN(userdata->nla_len);
398
399 return size;
400}
401
Thomas Graf8055a892013-12-13 15:22:20 +0100402static int queue_userspace_packet(struct datapath *dp, struct sk_buff *skb,
Jesse Grossccb13522011-10-25 19:26:31 -0700403 const struct dp_upcall_info *upcall_info)
404{
405 struct ovs_header *upcall;
406 struct sk_buff *nskb = NULL;
Li RongQing4ee45ea2014-09-02 20:52:28 +0800407 struct sk_buff *user_skb = NULL; /* to be queued to userspace */
Jesse Grossccb13522011-10-25 19:26:31 -0700408 struct nlattr *nla;
Thomas Graf795449d2013-11-30 13:21:32 +0100409 struct genl_info info = {
Thomas Graf8055a892013-12-13 15:22:20 +0100410 .dst_sk = ovs_dp_get_net(dp)->genl_sock,
Thomas Graf795449d2013-11-30 13:21:32 +0100411 .snd_portid = upcall_info->portid,
412 };
413 size_t len;
Thomas Grafbda56f12013-12-13 15:22:21 +0100414 unsigned int hlen;
Thomas Graf8055a892013-12-13 15:22:20 +0100415 int err, dp_ifindex;
416
417 dp_ifindex = get_dpifindex(dp);
418 if (!dp_ifindex)
419 return -ENODEV;
Jesse Grossccb13522011-10-25 19:26:31 -0700420
421 if (vlan_tx_tag_present(skb)) {
422 nskb = skb_clone(skb, GFP_ATOMIC);
423 if (!nskb)
424 return -ENOMEM;
425
Patrick McHardy86a9bad2013-04-19 02:04:30 +0000426 nskb = __vlan_put_tag(nskb, nskb->vlan_proto, vlan_tx_tag_get(nskb));
Dan Carpenter8aa51d62012-05-13 08:44:18 +0000427 if (!nskb)
Jesse Grossccb13522011-10-25 19:26:31 -0700428 return -ENOMEM;
429
430 nskb->vlan_tci = 0;
431 skb = nskb;
432 }
433
434 if (nla_attr_size(skb->len) > USHRT_MAX) {
435 err = -EFBIG;
436 goto out;
437 }
438
Thomas Grafbda56f12013-12-13 15:22:21 +0100439 /* Complete checksum if needed */
440 if (skb->ip_summed == CHECKSUM_PARTIAL &&
441 (err = skb_checksum_help(skb)))
442 goto out;
443
444 /* Older versions of OVS user space enforce alignment of the last
445 * Netlink attribute to NLA_ALIGNTO which would require extensive
446 * padding logic. Only perform zerocopy if padding is not required.
447 */
448 if (dp->user_features & OVS_DP_F_UNALIGNED)
449 hlen = skb_zerocopy_headlen(skb);
450 else
451 hlen = skb->len;
452
453 len = upcall_msg_size(upcall_info->userdata, hlen);
Thomas Graf795449d2013-11-30 13:21:32 +0100454 user_skb = genlmsg_new_unicast(len, &info, GFP_ATOMIC);
Jesse Grossccb13522011-10-25 19:26:31 -0700455 if (!user_skb) {
456 err = -ENOMEM;
457 goto out;
458 }
459
460 upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
461 0, upcall_info->cmd);
462 upcall->dp_ifindex = dp_ifindex;
463
464 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
Andy Zhouf53e3832014-07-17 15:17:44 -0700465 err = ovs_nla_put_flow(upcall_info->key, upcall_info->key, user_skb);
466 BUG_ON(err);
Jesse Grossccb13522011-10-25 19:26:31 -0700467 nla_nest_end(user_skb, nla);
468
469 if (upcall_info->userdata)
Ben Pfaff44901082013-02-15 17:29:22 -0800470 __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA,
471 nla_len(upcall_info->userdata),
472 nla_data(upcall_info->userdata));
Jesse Grossccb13522011-10-25 19:26:31 -0700473
Thomas Grafbda56f12013-12-13 15:22:21 +0100474 /* Only reserve room for attribute header, packet data is added
475 * in skb_zerocopy() */
476 if (!(nla = nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, 0))) {
477 err = -ENOBUFS;
478 goto out;
479 }
480 nla->nla_len = nla_attr_size(skb->len);
Jesse Grossccb13522011-10-25 19:26:31 -0700481
Zoltan Kiss36d5fe62014-03-26 22:37:45 +0000482 err = skb_zerocopy(user_skb, skb, skb->len, hlen);
483 if (err)
484 goto out;
Jesse Grossccb13522011-10-25 19:26:31 -0700485
Thomas Grafaea0bb42014-01-14 16:27:49 +0000486 /* Pad OVS_PACKET_ATTR_PACKET if linear copy was performed */
487 if (!(dp->user_features & OVS_DP_F_UNALIGNED)) {
488 size_t plen = NLA_ALIGN(user_skb->len) - user_skb->len;
489
490 if (plen > 0)
491 memset(skb_put(user_skb, plen), 0, plen);
492 }
493
Thomas Grafbda56f12013-12-13 15:22:21 +0100494 ((struct nlmsghdr *) user_skb->data)->nlmsg_len = user_skb->len;
495
Thomas Graf8055a892013-12-13 15:22:20 +0100496 err = genlmsg_unicast(ovs_dp_get_net(dp), user_skb, upcall_info->portid);
Li RongQing4ee45ea2014-09-02 20:52:28 +0800497 user_skb = NULL;
Jesse Grossccb13522011-10-25 19:26:31 -0700498out:
Zoltan Kiss36d5fe62014-03-26 22:37:45 +0000499 if (err)
500 skb_tx_error(skb);
Li RongQing4ee45ea2014-09-02 20:52:28 +0800501 kfree_skb(user_skb);
Jesse Grossccb13522011-10-25 19:26:31 -0700502 kfree_skb(nskb);
503 return err;
504}
505
Jesse Grossccb13522011-10-25 19:26:31 -0700506static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
507{
508 struct ovs_header *ovs_header = info->userhdr;
509 struct nlattr **a = info->attrs;
510 struct sw_flow_actions *acts;
511 struct sk_buff *packet;
512 struct sw_flow *flow;
513 struct datapath *dp;
514 struct ethhdr *eth;
Pravin B Shelar83c8df22014-09-15 19:20:31 -0700515 struct vport *input_vport;
Jesse Grossccb13522011-10-25 19:26:31 -0700516 int len;
517 int err;
Jesse Grossccb13522011-10-25 19:26:31 -0700518
519 err = -EINVAL;
520 if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
Thomas Grafdded45fc2013-03-29 14:46:47 +0100521 !a[OVS_PACKET_ATTR_ACTIONS])
Jesse Grossccb13522011-10-25 19:26:31 -0700522 goto err;
523
524 len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
525 packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
526 err = -ENOMEM;
527 if (!packet)
528 goto err;
529 skb_reserve(packet, NET_IP_ALIGN);
530
Thomas Graf32686a92013-03-29 14:46:48 +0100531 nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
Jesse Grossccb13522011-10-25 19:26:31 -0700532
533 skb_reset_mac_header(packet);
534 eth = eth_hdr(packet);
535
536 /* Normally, setting the skb 'protocol' field would be handled by a
537 * call to eth_type_trans(), but it assumes there's a sending
538 * device, which we may not have. */
Simon Hormane5c5d222013-03-28 13:38:25 +0900539 if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN)
Jesse Grossccb13522011-10-25 19:26:31 -0700540 packet->protocol = eth->h_proto;
541 else
542 packet->protocol = htons(ETH_P_802_2);
543
544 /* Build an sw_flow for sending this packet. */
Jarno Rajahalme23dabf82014-03-27 12:35:23 -0700545 flow = ovs_flow_alloc();
Jesse Grossccb13522011-10-25 19:26:31 -0700546 err = PTR_ERR(flow);
547 if (IS_ERR(flow))
548 goto err_kfree_skb;
549
Pravin B Shelar83c8df22014-09-15 19:20:31 -0700550 err = ovs_flow_key_extract_userspace(a[OVS_PACKET_ATTR_KEY], packet,
551 &flow->key);
Jesse Grossccb13522011-10-25 19:26:31 -0700552 if (err)
553 goto err_flow_free;
554
Pravin B Shelare6445712013-10-03 18:16:47 -0700555 acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_PACKET_ATTR_ACTIONS]));
Jesse Grossccb13522011-10-25 19:26:31 -0700556 err = PTR_ERR(acts);
557 if (IS_ERR(acts))
558 goto err_flow_free;
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700559
Pravin B Shelare6445712013-10-03 18:16:47 -0700560 err = ovs_nla_copy_actions(a[OVS_PACKET_ATTR_ACTIONS],
561 &flow->key, 0, &acts);
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700562 if (err)
563 goto err_flow_free;
Jesse Grossccb13522011-10-25 19:26:31 -0700564
Jesse Grossf5796682014-10-03 15:35:33 -0700565 rcu_assign_pointer(flow->sf_acts, acts);
566
567 OVS_CB(packet)->egress_tun_info = NULL;
Jesse Grossccb13522011-10-25 19:26:31 -0700568 OVS_CB(packet)->flow = flow;
569 packet->priority = flow->key.phy.priority;
Ansis Atteka39c7caeb2012-11-26 11:24:11 -0800570 packet->mark = flow->key.phy.skb_mark;
Jesse Grossccb13522011-10-25 19:26:31 -0700571
572 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800573 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -0700574 err = -ENODEV;
575 if (!dp)
576 goto err_unlock;
577
Pravin B Shelar83c8df22014-09-15 19:20:31 -0700578 input_vport = ovs_vport_rcu(dp, flow->key.phy.in_port);
579 if (!input_vport)
580 input_vport = ovs_vport_rcu(dp, OVSP_LOCAL);
581
582 if (!input_vport)
583 goto err_unlock;
584
585 OVS_CB(packet)->input_vport = input_vport;
586
Jesse Grossccb13522011-10-25 19:26:31 -0700587 local_bh_disable();
Pravin B Shelar2ff3e4e2014-09-15 19:15:28 -0700588 err = ovs_execute_actions(dp, packet, &flow->key);
Jesse Grossccb13522011-10-25 19:26:31 -0700589 local_bh_enable();
590 rcu_read_unlock();
591
Andy Zhou03f0d912013-08-07 20:01:00 -0700592 ovs_flow_free(flow, false);
Jesse Grossccb13522011-10-25 19:26:31 -0700593 return err;
594
595err_unlock:
596 rcu_read_unlock();
597err_flow_free:
Andy Zhou03f0d912013-08-07 20:01:00 -0700598 ovs_flow_free(flow, false);
Jesse Grossccb13522011-10-25 19:26:31 -0700599err_kfree_skb:
600 kfree_skb(packet);
601err:
602 return err;
603}
604
605static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
Thomas Grafdded45fc2013-03-29 14:46:47 +0100606 [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
Jesse Grossccb13522011-10-25 19:26:31 -0700607 [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
608 [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
609};
610
Johannes Berg4534de82013-11-14 17:14:46 +0100611static const struct genl_ops dp_packet_genl_ops[] = {
Jesse Grossccb13522011-10-25 19:26:31 -0700612 { .cmd = OVS_PACKET_CMD_EXECUTE,
613 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
614 .policy = packet_policy,
615 .doit = ovs_packet_cmd_execute
616 }
617};
618
Pravin B Shelar0c200ef2014-05-06 16:44:50 -0700619static struct genl_family dp_packet_genl_family = {
620 .id = GENL_ID_GENERATE,
621 .hdrsize = sizeof(struct ovs_header),
622 .name = OVS_PACKET_FAMILY,
623 .version = OVS_PACKET_VERSION,
624 .maxattr = OVS_PACKET_ATTR_MAX,
625 .netnsok = true,
626 .parallel_ops = true,
627 .ops = dp_packet_genl_ops,
628 .n_ops = ARRAY_SIZE(dp_packet_genl_ops),
629};
630
Andy Zhou1bd71162013-10-22 10:42:46 -0700631static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats,
632 struct ovs_dp_megaflow_stats *mega_stats)
Jesse Grossccb13522011-10-25 19:26:31 -0700633{
634 int i;
Jesse Grossccb13522011-10-25 19:26:31 -0700635
Andy Zhou1bd71162013-10-22 10:42:46 -0700636 memset(mega_stats, 0, sizeof(*mega_stats));
637
Pravin B Shelarb637e492013-10-04 00:14:23 -0700638 stats->n_flows = ovs_flow_tbl_count(&dp->table);
Andy Zhou1bd71162013-10-22 10:42:46 -0700639 mega_stats->n_masks = ovs_flow_tbl_num_masks(&dp->table);
Jesse Grossccb13522011-10-25 19:26:31 -0700640
641 stats->n_hit = stats->n_missed = stats->n_lost = 0;
Andy Zhou1bd71162013-10-22 10:42:46 -0700642
Jesse Grossccb13522011-10-25 19:26:31 -0700643 for_each_possible_cpu(i) {
644 const struct dp_stats_percpu *percpu_stats;
645 struct dp_stats_percpu local_stats;
646 unsigned int start;
647
648 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
649
650 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700651 start = u64_stats_fetch_begin_irq(&percpu_stats->syncp);
Jesse Grossccb13522011-10-25 19:26:31 -0700652 local_stats = *percpu_stats;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700653 } while (u64_stats_fetch_retry_irq(&percpu_stats->syncp, start));
Jesse Grossccb13522011-10-25 19:26:31 -0700654
655 stats->n_hit += local_stats.n_hit;
656 stats->n_missed += local_stats.n_missed;
657 stats->n_lost += local_stats.n_lost;
Andy Zhou1bd71162013-10-22 10:42:46 -0700658 mega_stats->n_mask_hit += local_stats.n_mask_hit;
Jesse Grossccb13522011-10-25 19:26:31 -0700659 }
660}
661
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100662static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts)
663{
664 return NLMSG_ALIGN(sizeof(struct ovs_header))
665 + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_KEY */
Andy Zhou03f0d912013-08-07 20:01:00 -0700666 + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_MASK */
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100667 + nla_total_size(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
668 + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
669 + nla_total_size(8) /* OVS_FLOW_ATTR_USED */
670 + nla_total_size(acts->actions_len); /* OVS_FLOW_ATTR_ACTIONS */
671}
672
Jarno Rajahalmebb6f9a72014-05-05 11:32:17 -0700673/* Called with ovs_mutex or RCU read lock. */
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700674static int ovs_flow_cmd_fill_info(const struct sw_flow *flow, int dp_ifindex,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000675 struct sk_buff *skb, u32 portid,
Jesse Grossccb13522011-10-25 19:26:31 -0700676 u32 seq, u32 flags, u8 cmd)
677{
678 const int skb_orig_len = skb->len;
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700679 struct nlattr *start;
Jesse Grossccb13522011-10-25 19:26:31 -0700680 struct ovs_flow_stats stats;
Pravin B Shelare298e502013-10-29 17:22:21 -0700681 __be16 tcp_flags;
682 unsigned long used;
Jesse Grossccb13522011-10-25 19:26:31 -0700683 struct ovs_header *ovs_header;
684 struct nlattr *nla;
Jesse Grossccb13522011-10-25 19:26:31 -0700685 int err;
686
Eric W. Biederman15e47302012-09-07 20:12:54 +0000687 ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family, flags, cmd);
Jesse Grossccb13522011-10-25 19:26:31 -0700688 if (!ovs_header)
689 return -EMSGSIZE;
690
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700691 ovs_header->dp_ifindex = dp_ifindex;
Jesse Grossccb13522011-10-25 19:26:31 -0700692
Andy Zhou03f0d912013-08-07 20:01:00 -0700693 /* Fill flow key. */
Jesse Grossccb13522011-10-25 19:26:31 -0700694 nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
695 if (!nla)
696 goto nla_put_failure;
Andy Zhou03f0d912013-08-07 20:01:00 -0700697
Pravin B Shelare6445712013-10-03 18:16:47 -0700698 err = ovs_nla_put_flow(&flow->unmasked_key, &flow->unmasked_key, skb);
Jesse Grossccb13522011-10-25 19:26:31 -0700699 if (err)
700 goto error;
701 nla_nest_end(skb, nla);
702
Andy Zhou03f0d912013-08-07 20:01:00 -0700703 nla = nla_nest_start(skb, OVS_FLOW_ATTR_MASK);
704 if (!nla)
705 goto nla_put_failure;
706
Pravin B Shelare6445712013-10-03 18:16:47 -0700707 err = ovs_nla_put_flow(&flow->key, &flow->mask->key, skb);
Andy Zhou03f0d912013-08-07 20:01:00 -0700708 if (err)
709 goto error;
710
711 nla_nest_end(skb, nla);
712
Pravin B Shelare298e502013-10-29 17:22:21 -0700713 ovs_flow_stats_get(flow, &stats, &used, &tcp_flags);
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700714
David S. Miller028d6a62012-03-29 23:20:48 -0400715 if (used &&
716 nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
717 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -0700718
David S. Miller028d6a62012-03-29 23:20:48 -0400719 if (stats.n_packets &&
Pravin B Shelare298e502013-10-29 17:22:21 -0700720 nla_put(skb, OVS_FLOW_ATTR_STATS, sizeof(struct ovs_flow_stats), &stats))
David S. Miller028d6a62012-03-29 23:20:48 -0400721 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -0700722
Pravin B Shelare298e502013-10-29 17:22:21 -0700723 if ((u8)ntohs(tcp_flags) &&
724 nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, (u8)ntohs(tcp_flags)))
David S. Miller028d6a62012-03-29 23:20:48 -0400725 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -0700726
727 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
728 * this is the first flow to be dumped into 'skb'. This is unusual for
729 * Netlink but individual action lists can be longer than
730 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
731 * The userspace caller can always fetch the actions separately if it
732 * really wants them. (Most userspace callers in fact don't care.)
733 *
734 * This can only fail for dump operations because the skb is always
735 * properly sized for single flows.
736 */
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700737 start = nla_nest_start(skb, OVS_FLOW_ATTR_ACTIONS);
738 if (start) {
Pravin B Shelard57170b2013-07-30 15:39:39 -0700739 const struct sw_flow_actions *sf_acts;
740
Jesse Gross663efa32013-12-03 10:58:53 -0800741 sf_acts = rcu_dereference_ovsl(flow->sf_acts);
Pravin B Shelare6445712013-10-03 18:16:47 -0700742 err = ovs_nla_put_actions(sf_acts->actions,
743 sf_acts->actions_len, skb);
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700744
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700745 if (!err)
746 nla_nest_end(skb, start);
747 else {
748 if (skb_orig_len)
749 goto error;
750
751 nla_nest_cancel(skb, start);
752 }
753 } else if (skb_orig_len)
754 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -0700755
756 return genlmsg_end(skb, ovs_header);
757
758nla_put_failure:
759 err = -EMSGSIZE;
760error:
761 genlmsg_cancel(skb, ovs_header);
762 return err;
763}
764
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700765/* May not be called with RCU read lock. */
766static struct sk_buff *ovs_flow_cmd_alloc_info(const struct sw_flow_actions *acts,
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700767 struct genl_info *info,
768 bool always)
Jesse Grossccb13522011-10-25 19:26:31 -0700769{
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700770 struct sk_buff *skb;
Jesse Grossccb13522011-10-25 19:26:31 -0700771
Samuel Gauthier9b67aa42014-09-18 10:31:04 +0200772 if (!always && !ovs_must_notify(&dp_flow_genl_family, info, 0))
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700773 return NULL;
774
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700775 skb = genlmsg_new_unicast(ovs_flow_cmd_msg_size(acts), info, GFP_KERNEL);
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700776 if (!skb)
777 return ERR_PTR(-ENOMEM);
778
779 return skb;
Jesse Grossccb13522011-10-25 19:26:31 -0700780}
781
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700782/* Called with ovs_mutex. */
783static struct sk_buff *ovs_flow_cmd_build_info(const struct sw_flow *flow,
784 int dp_ifindex,
785 struct genl_info *info, u8 cmd,
786 bool always)
Jesse Grossccb13522011-10-25 19:26:31 -0700787{
788 struct sk_buff *skb;
789 int retval;
790
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700791 skb = ovs_flow_cmd_alloc_info(ovsl_dereference(flow->sf_acts), info,
792 always);
Himangi Saraogid0e992a2014-07-27 12:37:46 +0530793 if (IS_ERR_OR_NULL(skb))
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700794 return skb;
Jesse Grossccb13522011-10-25 19:26:31 -0700795
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700796 retval = ovs_flow_cmd_fill_info(flow, dp_ifindex, skb,
797 info->snd_portid, info->snd_seq, 0,
798 cmd);
Jesse Grossccb13522011-10-25 19:26:31 -0700799 BUG_ON(retval < 0);
800 return skb;
801}
802
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700803static int ovs_flow_cmd_new(struct sk_buff *skb, struct genl_info *info)
Jesse Grossccb13522011-10-25 19:26:31 -0700804{
805 struct nlattr **a = info->attrs;
806 struct ovs_header *ovs_header = info->userhdr;
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700807 struct sw_flow *flow, *new_flow;
Andy Zhou03f0d912013-08-07 20:01:00 -0700808 struct sw_flow_mask mask;
Jesse Grossccb13522011-10-25 19:26:31 -0700809 struct sk_buff *reply;
810 struct datapath *dp;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700811 struct sw_flow_actions *acts;
812 struct sw_flow_match match;
813 int error;
814
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700815 /* Must have key and actions. */
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700816 error = -EINVAL;
817 if (!a[OVS_FLOW_ATTR_KEY])
818 goto error;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700819 if (!a[OVS_FLOW_ATTR_ACTIONS])
820 goto error;
821
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700822 /* Most of the time we need to allocate a new flow, do it before
823 * locking.
824 */
825 new_flow = ovs_flow_alloc();
826 if (IS_ERR(new_flow)) {
827 error = PTR_ERR(new_flow);
828 goto error;
829 }
830
831 /* Extract key. */
832 ovs_match_init(&match, &new_flow->unmasked_key, &mask);
833 error = ovs_nla_get_match(&match,
834 a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
835 if (error)
836 goto err_kfree_flow;
837
838 ovs_flow_mask_key(&new_flow->key, &new_flow->unmasked_key, &mask);
839
840 /* Validate actions. */
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700841 acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_FLOW_ATTR_ACTIONS]));
842 error = PTR_ERR(acts);
843 if (IS_ERR(acts))
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700844 goto err_kfree_flow;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700845
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700846 error = ovs_nla_copy_actions(a[OVS_FLOW_ATTR_ACTIONS], &new_flow->key,
847 0, &acts);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700848 if (error) {
849 OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700850 goto err_kfree_acts;
851 }
852
853 reply = ovs_flow_cmd_alloc_info(acts, info, false);
854 if (IS_ERR(reply)) {
855 error = PTR_ERR(reply);
856 goto err_kfree_acts;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700857 }
858
859 ovs_lock();
860 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700861 if (unlikely(!dp)) {
862 error = -ENODEV;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700863 goto err_unlock_ovs;
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700864 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700865 /* Check if this is a duplicate flow */
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700866 flow = ovs_flow_tbl_lookup(&dp->table, &new_flow->unmasked_key);
867 if (likely(!flow)) {
868 rcu_assign_pointer(new_flow->sf_acts, acts);
869
870 /* Put flow in bucket. */
871 error = ovs_flow_tbl_insert(&dp->table, new_flow, &mask);
872 if (unlikely(error)) {
873 acts = NULL;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700874 goto err_unlock_ovs;
875 }
876
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700877 if (unlikely(reply)) {
878 error = ovs_flow_cmd_fill_info(new_flow,
879 ovs_header->dp_ifindex,
880 reply, info->snd_portid,
881 info->snd_seq, 0,
882 OVS_FLOW_CMD_NEW);
883 BUG_ON(error < 0);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700884 }
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700885 ovs_unlock();
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700886 } else {
887 struct sw_flow_actions *old_acts;
888
889 /* Bail out if we're not allowed to modify an existing flow.
890 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
891 * because Generic Netlink treats the latter as a dump
892 * request. We also accept NLM_F_EXCL in case that bug ever
893 * gets fixed.
894 */
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700895 if (unlikely(info->nlhdr->nlmsg_flags & (NLM_F_CREATE
896 | NLM_F_EXCL))) {
897 error = -EEXIST;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700898 goto err_unlock_ovs;
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700899 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700900 /* The unmasked key has to be the same for flow updates. */
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700901 if (unlikely(!ovs_flow_cmp_unmasked_key(flow, &match))) {
Alex Wang4a46b242014-06-30 20:30:29 -0700902 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
903 if (!flow) {
904 error = -ENOENT;
905 goto err_unlock_ovs;
906 }
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700907 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700908 /* Update actions. */
909 old_acts = ovsl_dereference(flow->sf_acts);
910 rcu_assign_pointer(flow->sf_acts, acts);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700911
912 if (unlikely(reply)) {
913 error = ovs_flow_cmd_fill_info(flow,
914 ovs_header->dp_ifindex,
915 reply, info->snd_portid,
916 info->snd_seq, 0,
917 OVS_FLOW_CMD_NEW);
918 BUG_ON(error < 0);
919 }
920 ovs_unlock();
921
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700922 ovs_nla_free_flow_actions(old_acts);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700923 ovs_flow_free(new_flow, false);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700924 }
925
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700926 if (reply)
927 ovs_notify(&dp_flow_genl_family, reply, info);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700928 return 0;
929
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700930err_unlock_ovs:
931 ovs_unlock();
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700932 kfree_skb(reply);
933err_kfree_acts:
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700934 kfree(acts);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700935err_kfree_flow:
936 ovs_flow_free(new_flow, false);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700937error:
938 return error;
939}
940
Jesse Gross6b205b22014-10-03 15:35:32 -0700941static struct sw_flow_actions *get_flow_actions(const struct nlattr *a,
942 const struct sw_flow_key *key,
943 const struct sw_flow_mask *mask)
944{
945 struct sw_flow_actions *acts;
946 struct sw_flow_key masked_key;
947 int error;
948
949 acts = ovs_nla_alloc_flow_actions(nla_len(a));
950 if (IS_ERR(acts))
951 return acts;
952
953 ovs_flow_mask_key(&masked_key, key, mask);
954 error = ovs_nla_copy_actions(a, &masked_key, 0, &acts);
955 if (error) {
956 OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
957 kfree(acts);
958 return ERR_PTR(error);
959 }
960
961 return acts;
962}
963
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700964static int ovs_flow_cmd_set(struct sk_buff *skb, struct genl_info *info)
965{
966 struct nlattr **a = info->attrs;
967 struct ovs_header *ovs_header = info->userhdr;
Jesse Gross6b205b22014-10-03 15:35:32 -0700968 struct sw_flow_key key;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700969 struct sw_flow *flow;
970 struct sw_flow_mask mask;
971 struct sk_buff *reply = NULL;
972 struct datapath *dp;
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700973 struct sw_flow_actions *old_acts = NULL, *acts = NULL;
Andy Zhou03f0d912013-08-07 20:01:00 -0700974 struct sw_flow_match match;
Jesse Grossccb13522011-10-25 19:26:31 -0700975 int error;
Jesse Grossccb13522011-10-25 19:26:31 -0700976
977 /* Extract key. */
978 error = -EINVAL;
979 if (!a[OVS_FLOW_ATTR_KEY])
980 goto error;
Andy Zhou03f0d912013-08-07 20:01:00 -0700981
982 ovs_match_init(&match, &key, &mask);
Jarno Rajahalme23dabf82014-03-27 12:35:23 -0700983 error = ovs_nla_get_match(&match,
Pravin B Shelare6445712013-10-03 18:16:47 -0700984 a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
Jesse Grossccb13522011-10-25 19:26:31 -0700985 if (error)
986 goto error;
987
988 /* Validate actions. */
989 if (a[OVS_FLOW_ATTR_ACTIONS]) {
Jesse Gross6b205b22014-10-03 15:35:32 -0700990 acts = get_flow_actions(a[OVS_FLOW_ATTR_ACTIONS], &key, &mask);
991 if (IS_ERR(acts)) {
992 error = PTR_ERR(acts);
Jesse Grossccb13522011-10-25 19:26:31 -0700993 goto error;
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700994 }
995 }
996
997 /* Can allocate before locking if have acts. */
998 if (acts) {
999 reply = ovs_flow_cmd_alloc_info(acts, info, false);
1000 if (IS_ERR(reply)) {
1001 error = PTR_ERR(reply);
1002 goto err_kfree_acts;
Andy Zhou03f0d912013-08-07 20:01:00 -07001003 }
Jesse Grossccb13522011-10-25 19:26:31 -07001004 }
1005
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001006 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001007 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001008 if (unlikely(!dp)) {
1009 error = -ENODEV;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001010 goto err_unlock_ovs;
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001011 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001012 /* Check that the flow exists. */
Alex Wang4a46b242014-06-30 20:30:29 -07001013 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001014 if (unlikely(!flow)) {
1015 error = -ENOENT;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001016 goto err_unlock_ovs;
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001017 }
Alex Wang4a46b242014-06-30 20:30:29 -07001018
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001019 /* Update actions, if present. */
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001020 if (likely(acts)) {
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001021 old_acts = ovsl_dereference(flow->sf_acts);
Jesse Grossccb13522011-10-25 19:26:31 -07001022 rcu_assign_pointer(flow->sf_acts, acts);
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001023
1024 if (unlikely(reply)) {
1025 error = ovs_flow_cmd_fill_info(flow,
1026 ovs_header->dp_ifindex,
1027 reply, info->snd_portid,
1028 info->snd_seq, 0,
1029 OVS_FLOW_CMD_NEW);
1030 BUG_ON(error < 0);
1031 }
1032 } else {
1033 /* Could not alloc without acts before locking. */
1034 reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex,
1035 info, OVS_FLOW_CMD_NEW, false);
1036 if (unlikely(IS_ERR(reply))) {
1037 error = PTR_ERR(reply);
1038 goto err_unlock_ovs;
1039 }
Jesse Grossccb13522011-10-25 19:26:31 -07001040 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001041
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001042 /* Clear stats. */
1043 if (a[OVS_FLOW_ATTR_CLEAR])
1044 ovs_flow_stats_clear(flow);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001045 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001046
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001047 if (reply)
1048 ovs_notify(&dp_flow_genl_family, reply, info);
1049 if (old_acts)
1050 ovs_nla_free_flow_actions(old_acts);
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -07001051
Jesse Grossccb13522011-10-25 19:26:31 -07001052 return 0;
1053
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001054err_unlock_ovs:
1055 ovs_unlock();
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001056 kfree_skb(reply);
1057err_kfree_acts:
Pravin B Shelar74f84a52013-06-17 17:50:12 -07001058 kfree(acts);
Jesse Grossccb13522011-10-25 19:26:31 -07001059error:
1060 return error;
1061}
1062
1063static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1064{
1065 struct nlattr **a = info->attrs;
1066 struct ovs_header *ovs_header = info->userhdr;
1067 struct sw_flow_key key;
1068 struct sk_buff *reply;
1069 struct sw_flow *flow;
1070 struct datapath *dp;
Andy Zhou03f0d912013-08-07 20:01:00 -07001071 struct sw_flow_match match;
Jesse Grossccb13522011-10-25 19:26:31 -07001072 int err;
Jesse Grossccb13522011-10-25 19:26:31 -07001073
Andy Zhou03f0d912013-08-07 20:01:00 -07001074 if (!a[OVS_FLOW_ATTR_KEY]) {
1075 OVS_NLERR("Flow get message rejected, Key attribute missing.\n");
Jesse Grossccb13522011-10-25 19:26:31 -07001076 return -EINVAL;
Andy Zhou03f0d912013-08-07 20:01:00 -07001077 }
1078
1079 ovs_match_init(&match, &key, NULL);
Jarno Rajahalme23dabf82014-03-27 12:35:23 -07001080 err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL);
Jesse Grossccb13522011-10-25 19:26:31 -07001081 if (err)
1082 return err;
1083
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001084 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001085 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001086 if (!dp) {
1087 err = -ENODEV;
1088 goto unlock;
1089 }
Jesse Grossccb13522011-10-25 19:26:31 -07001090
Alex Wang4a46b242014-06-30 20:30:29 -07001091 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1092 if (!flow) {
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001093 err = -ENOENT;
1094 goto unlock;
1095 }
Jesse Grossccb13522011-10-25 19:26:31 -07001096
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -07001097 reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex, info,
1098 OVS_FLOW_CMD_NEW, true);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001099 if (IS_ERR(reply)) {
1100 err = PTR_ERR(reply);
1101 goto unlock;
1102 }
Jesse Grossccb13522011-10-25 19:26:31 -07001103
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001104 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001105 return genlmsg_reply(reply, info);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001106unlock:
1107 ovs_unlock();
1108 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001109}
1110
1111static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1112{
1113 struct nlattr **a = info->attrs;
1114 struct ovs_header *ovs_header = info->userhdr;
1115 struct sw_flow_key key;
1116 struct sk_buff *reply;
1117 struct sw_flow *flow;
1118 struct datapath *dp;
Andy Zhou03f0d912013-08-07 20:01:00 -07001119 struct sw_flow_match match;
Jesse Grossccb13522011-10-25 19:26:31 -07001120 int err;
Jesse Grossccb13522011-10-25 19:26:31 -07001121
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001122 if (likely(a[OVS_FLOW_ATTR_KEY])) {
1123 ovs_match_init(&match, &key, NULL);
1124 err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL);
1125 if (unlikely(err))
1126 return err;
1127 }
1128
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001129 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001130 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001131 if (unlikely(!dp)) {
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001132 err = -ENODEV;
1133 goto unlock;
1134 }
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001135
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001136 if (unlikely(!a[OVS_FLOW_ATTR_KEY])) {
Pravin B Shelarb637e492013-10-04 00:14:23 -07001137 err = ovs_flow_tbl_flush(&dp->table);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001138 goto unlock;
1139 }
Andy Zhou03f0d912013-08-07 20:01:00 -07001140
Alex Wang4a46b242014-06-30 20:30:29 -07001141 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1142 if (unlikely(!flow)) {
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001143 err = -ENOENT;
1144 goto unlock;
1145 }
Jesse Grossccb13522011-10-25 19:26:31 -07001146
Pravin B Shelarb637e492013-10-04 00:14:23 -07001147 ovs_flow_tbl_remove(&dp->table, flow);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001148 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001149
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001150 reply = ovs_flow_cmd_alloc_info((const struct sw_flow_actions __force *) flow->sf_acts,
1151 info, false);
1152 if (likely(reply)) {
1153 if (likely(!IS_ERR(reply))) {
1154 rcu_read_lock(); /*To keep RCU checker happy. */
1155 err = ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex,
1156 reply, info->snd_portid,
1157 info->snd_seq, 0,
1158 OVS_FLOW_CMD_DEL);
1159 rcu_read_unlock();
1160 BUG_ON(err < 0);
1161
1162 ovs_notify(&dp_flow_genl_family, reply, info);
1163 } else {
1164 netlink_set_err(sock_net(skb->sk)->genl_sock, 0, 0, PTR_ERR(reply));
1165 }
1166 }
1167
1168 ovs_flow_free(flow, true);
Jesse Grossccb13522011-10-25 19:26:31 -07001169 return 0;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001170unlock:
1171 ovs_unlock();
1172 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001173}
1174
1175static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1176{
1177 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
Pravin B Shelarb637e492013-10-04 00:14:23 -07001178 struct table_instance *ti;
Jesse Grossccb13522011-10-25 19:26:31 -07001179 struct datapath *dp;
Jesse Grossccb13522011-10-25 19:26:31 -07001180
Pravin B Shelard57170b2013-07-30 15:39:39 -07001181 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001182 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001183 if (!dp) {
Pravin B Shelard57170b2013-07-30 15:39:39 -07001184 rcu_read_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001185 return -ENODEV;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001186 }
Jesse Grossccb13522011-10-25 19:26:31 -07001187
Pravin B Shelarb637e492013-10-04 00:14:23 -07001188 ti = rcu_dereference(dp->table.ti);
Jesse Grossccb13522011-10-25 19:26:31 -07001189 for (;;) {
1190 struct sw_flow *flow;
1191 u32 bucket, obj;
1192
1193 bucket = cb->args[0];
1194 obj = cb->args[1];
Pravin B Shelarb637e492013-10-04 00:14:23 -07001195 flow = ovs_flow_tbl_dump_next(ti, &bucket, &obj);
Jesse Grossccb13522011-10-25 19:26:31 -07001196 if (!flow)
1197 break;
1198
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -07001199 if (ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex, skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001200 NETLINK_CB(cb->skb).portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001201 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1202 OVS_FLOW_CMD_NEW) < 0)
1203 break;
1204
1205 cb->args[0] = bucket;
1206 cb->args[1] = obj;
1207 }
Pravin B Shelard57170b2013-07-30 15:39:39 -07001208 rcu_read_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001209 return skb->len;
1210}
1211
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001212static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
1213 [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
1214 [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
1215 [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
1216};
1217
stephen hemminger48e48a72014-07-16 11:25:52 -07001218static const struct genl_ops dp_flow_genl_ops[] = {
Jesse Grossccb13522011-10-25 19:26:31 -07001219 { .cmd = OVS_FLOW_CMD_NEW,
1220 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1221 .policy = flow_policy,
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001222 .doit = ovs_flow_cmd_new
Jesse Grossccb13522011-10-25 19:26:31 -07001223 },
1224 { .cmd = OVS_FLOW_CMD_DEL,
1225 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1226 .policy = flow_policy,
1227 .doit = ovs_flow_cmd_del
1228 },
1229 { .cmd = OVS_FLOW_CMD_GET,
1230 .flags = 0, /* OK for unprivileged users. */
1231 .policy = flow_policy,
1232 .doit = ovs_flow_cmd_get,
1233 .dumpit = ovs_flow_cmd_dump
1234 },
1235 { .cmd = OVS_FLOW_CMD_SET,
1236 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1237 .policy = flow_policy,
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001238 .doit = ovs_flow_cmd_set,
Jesse Grossccb13522011-10-25 19:26:31 -07001239 },
1240};
1241
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001242static struct genl_family dp_flow_genl_family = {
Jesse Grossccb13522011-10-25 19:26:31 -07001243 .id = GENL_ID_GENERATE,
1244 .hdrsize = sizeof(struct ovs_header),
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001245 .name = OVS_FLOW_FAMILY,
1246 .version = OVS_FLOW_VERSION,
1247 .maxattr = OVS_FLOW_ATTR_MAX,
Pravin B Shelar3a4e0d62013-04-23 07:48:48 +00001248 .netnsok = true,
1249 .parallel_ops = true,
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001250 .ops = dp_flow_genl_ops,
1251 .n_ops = ARRAY_SIZE(dp_flow_genl_ops),
1252 .mcgrps = &ovs_dp_flow_multicast_group,
1253 .n_mcgrps = 1,
Jesse Grossccb13522011-10-25 19:26:31 -07001254};
1255
Thomas Grafc3ff8cf2013-03-29 14:46:49 +01001256static size_t ovs_dp_cmd_msg_size(void)
1257{
1258 size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
1259
1260 msgsize += nla_total_size(IFNAMSIZ);
1261 msgsize += nla_total_size(sizeof(struct ovs_dp_stats));
Andy Zhou1bd71162013-10-22 10:42:46 -07001262 msgsize += nla_total_size(sizeof(struct ovs_dp_megaflow_stats));
Daniele Di Proietto45fb9c32014-01-23 10:47:35 -08001263 msgsize += nla_total_size(sizeof(u32)); /* OVS_DP_ATTR_USER_FEATURES */
Thomas Grafc3ff8cf2013-03-29 14:46:49 +01001264
1265 return msgsize;
1266}
1267
Jarno Rajahalmebb6f9a72014-05-05 11:32:17 -07001268/* Called with ovs_mutex or RCU read lock. */
Jesse Grossccb13522011-10-25 19:26:31 -07001269static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001270 u32 portid, u32 seq, u32 flags, u8 cmd)
Jesse Grossccb13522011-10-25 19:26:31 -07001271{
1272 struct ovs_header *ovs_header;
1273 struct ovs_dp_stats dp_stats;
Andy Zhou1bd71162013-10-22 10:42:46 -07001274 struct ovs_dp_megaflow_stats dp_megaflow_stats;
Jesse Grossccb13522011-10-25 19:26:31 -07001275 int err;
1276
Eric W. Biederman15e47302012-09-07 20:12:54 +00001277 ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
Jesse Grossccb13522011-10-25 19:26:31 -07001278 flags, cmd);
1279 if (!ovs_header)
1280 goto error;
1281
1282 ovs_header->dp_ifindex = get_dpifindex(dp);
1283
Jesse Grossccb13522011-10-25 19:26:31 -07001284 err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
Jesse Grossccb13522011-10-25 19:26:31 -07001285 if (err)
1286 goto nla_put_failure;
1287
Andy Zhou1bd71162013-10-22 10:42:46 -07001288 get_dp_stats(dp, &dp_stats, &dp_megaflow_stats);
1289 if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats),
1290 &dp_stats))
1291 goto nla_put_failure;
1292
1293 if (nla_put(skb, OVS_DP_ATTR_MEGAFLOW_STATS,
1294 sizeof(struct ovs_dp_megaflow_stats),
1295 &dp_megaflow_stats))
David S. Miller028d6a62012-03-29 23:20:48 -04001296 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001297
Thomas Graf43d4be92013-12-13 15:22:18 +01001298 if (nla_put_u32(skb, OVS_DP_ATTR_USER_FEATURES, dp->user_features))
1299 goto nla_put_failure;
1300
Jesse Grossccb13522011-10-25 19:26:31 -07001301 return genlmsg_end(skb, ovs_header);
1302
1303nla_put_failure:
1304 genlmsg_cancel(skb, ovs_header);
1305error:
1306 return -EMSGSIZE;
1307}
1308
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001309static struct sk_buff *ovs_dp_cmd_alloc_info(struct genl_info *info)
Jesse Grossccb13522011-10-25 19:26:31 -07001310{
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001311 return genlmsg_new_unicast(ovs_dp_cmd_msg_size(), info, GFP_KERNEL);
Jesse Grossccb13522011-10-25 19:26:31 -07001312}
1313
Jarno Rajahalmebb6f9a72014-05-05 11:32:17 -07001314/* Called with rcu_read_lock or ovs_mutex. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001315static struct datapath *lookup_datapath(struct net *net,
1316 struct ovs_header *ovs_header,
Jesse Grossccb13522011-10-25 19:26:31 -07001317 struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1318{
1319 struct datapath *dp;
1320
1321 if (!a[OVS_DP_ATTR_NAME])
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001322 dp = get_dp(net, ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001323 else {
1324 struct vport *vport;
1325
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001326 vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
Jesse Grossccb13522011-10-25 19:26:31 -07001327 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
Jesse Grossccb13522011-10-25 19:26:31 -07001328 }
1329 return dp ? dp : ERR_PTR(-ENODEV);
1330}
1331
Thomas Graf44da5ae2013-12-13 15:22:19 +01001332static void ovs_dp_reset_user_features(struct sk_buff *skb, struct genl_info *info)
1333{
1334 struct datapath *dp;
1335
1336 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
Jiri Pirko3c7eacf2014-02-14 11:42:36 +01001337 if (IS_ERR(dp))
Thomas Graf44da5ae2013-12-13 15:22:19 +01001338 return;
1339
1340 WARN(dp->user_features, "Dropping previously announced user features\n");
1341 dp->user_features = 0;
1342}
1343
Thomas Graf43d4be92013-12-13 15:22:18 +01001344static void ovs_dp_change(struct datapath *dp, struct nlattr **a)
1345{
1346 if (a[OVS_DP_ATTR_USER_FEATURES])
1347 dp->user_features = nla_get_u32(a[OVS_DP_ATTR_USER_FEATURES]);
1348}
1349
Jesse Grossccb13522011-10-25 19:26:31 -07001350static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1351{
1352 struct nlattr **a = info->attrs;
1353 struct vport_parms parms;
1354 struct sk_buff *reply;
1355 struct datapath *dp;
1356 struct vport *vport;
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001357 struct ovs_net *ovs_net;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001358 int err, i;
Jesse Grossccb13522011-10-25 19:26:31 -07001359
1360 err = -EINVAL;
1361 if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1362 goto err;
1363
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001364 reply = ovs_dp_cmd_alloc_info(info);
1365 if (!reply)
1366 return -ENOMEM;
Jesse Grossccb13522011-10-25 19:26:31 -07001367
1368 err = -ENOMEM;
1369 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1370 if (dp == NULL)
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001371 goto err_free_reply;
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001372
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001373 ovs_dp_set_net(dp, hold_net(sock_net(skb->sk)));
Jesse Grossccb13522011-10-25 19:26:31 -07001374
1375 /* Allocate table. */
Pravin B Shelarb637e492013-10-04 00:14:23 -07001376 err = ovs_flow_tbl_init(&dp->table);
1377 if (err)
Jesse Grossccb13522011-10-25 19:26:31 -07001378 goto err_free_dp;
1379
WANG Cong1c213bd2014-02-13 11:46:28 -08001380 dp->stats_percpu = netdev_alloc_pcpu_stats(struct dp_stats_percpu);
Jesse Grossccb13522011-10-25 19:26:31 -07001381 if (!dp->stats_percpu) {
1382 err = -ENOMEM;
1383 goto err_destroy_table;
1384 }
1385
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001386 dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
Pravin B Shelare6445712013-10-03 18:16:47 -07001387 GFP_KERNEL);
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001388 if (!dp->ports) {
1389 err = -ENOMEM;
1390 goto err_destroy_percpu;
1391 }
1392
1393 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1394 INIT_HLIST_HEAD(&dp->ports[i]);
1395
Jesse Grossccb13522011-10-25 19:26:31 -07001396 /* Set up our datapath device. */
1397 parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1398 parms.type = OVS_VPORT_TYPE_INTERNAL;
1399 parms.options = NULL;
1400 parms.dp = dp;
1401 parms.port_no = OVSP_LOCAL;
Alex Wang5cd667b2014-07-17 15:14:13 -07001402 parms.upcall_portids = a[OVS_DP_ATTR_UPCALL_PID];
Jesse Grossccb13522011-10-25 19:26:31 -07001403
Thomas Graf43d4be92013-12-13 15:22:18 +01001404 ovs_dp_change(dp, a);
1405
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001406 /* So far only local changes have been made, now need the lock. */
1407 ovs_lock();
1408
Jesse Grossccb13522011-10-25 19:26:31 -07001409 vport = new_vport(&parms);
1410 if (IS_ERR(vport)) {
1411 err = PTR_ERR(vport);
1412 if (err == -EBUSY)
1413 err = -EEXIST;
1414
Thomas Graf44da5ae2013-12-13 15:22:19 +01001415 if (err == -EEXIST) {
1416 /* An outdated user space instance that does not understand
1417 * the concept of user_features has attempted to create a new
1418 * datapath and is likely to reuse it. Drop all user features.
1419 */
1420 if (info->genlhdr->version < OVS_DP_VER_FEATURES)
1421 ovs_dp_reset_user_features(skb, info);
1422 }
1423
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001424 goto err_destroy_ports_array;
Jesse Grossccb13522011-10-25 19:26:31 -07001425 }
1426
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001427 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1428 info->snd_seq, 0, OVS_DP_CMD_NEW);
1429 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001430
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001431 ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001432 list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001433
1434 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001435
Johannes Berg2a94fe42013-11-19 15:19:39 +01001436 ovs_notify(&dp_datapath_genl_family, reply, info);
Jesse Grossccb13522011-10-25 19:26:31 -07001437 return 0;
1438
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001439err_destroy_ports_array:
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001440 ovs_unlock();
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001441 kfree(dp->ports);
Jesse Grossccb13522011-10-25 19:26:31 -07001442err_destroy_percpu:
1443 free_percpu(dp->stats_percpu);
1444err_destroy_table:
Andy Zhoue80857c2014-01-21 09:31:04 -08001445 ovs_flow_tbl_destroy(&dp->table, false);
Jesse Grossccb13522011-10-25 19:26:31 -07001446err_free_dp:
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001447 release_net(ovs_dp_get_net(dp));
Jesse Grossccb13522011-10-25 19:26:31 -07001448 kfree(dp);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001449err_free_reply:
1450 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001451err:
1452 return err;
1453}
1454
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001455/* Called with ovs_mutex. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001456static void __dp_destroy(struct datapath *dp)
Jesse Grossccb13522011-10-25 19:26:31 -07001457{
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001458 int i;
Jesse Grossccb13522011-10-25 19:26:31 -07001459
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001460 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1461 struct vport *vport;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001462 struct hlist_node *n;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001463
Sasha Levinb67bfe02013-02-27 17:06:00 -08001464 hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001465 if (vport->port_no != OVSP_LOCAL)
1466 ovs_dp_detach_port(vport);
1467 }
Jesse Grossccb13522011-10-25 19:26:31 -07001468
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001469 list_del_rcu(&dp->list_node);
Jesse Grossccb13522011-10-25 19:26:31 -07001470
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001471 /* OVSP_LOCAL is datapath internal port. We need to make sure that
Andy Zhoue80857c2014-01-21 09:31:04 -08001472 * all ports in datapath are destroyed first before freeing datapath.
Jesse Grossccb13522011-10-25 19:26:31 -07001473 */
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001474 ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
Jesse Grossccb13522011-10-25 19:26:31 -07001475
Andy Zhoue80857c2014-01-21 09:31:04 -08001476 /* RCU destroy the flow table */
1477 ovs_flow_tbl_destroy(&dp->table, true);
1478
Jesse Grossccb13522011-10-25 19:26:31 -07001479 call_rcu(&dp->rcu, destroy_dp_rcu);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001480}
1481
1482static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1483{
1484 struct sk_buff *reply;
1485 struct datapath *dp;
1486 int err;
1487
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001488 reply = ovs_dp_cmd_alloc_info(info);
1489 if (!reply)
1490 return -ENOMEM;
1491
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001492 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001493 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1494 err = PTR_ERR(dp);
1495 if (IS_ERR(dp))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001496 goto err_unlock_free;
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001497
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001498 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1499 info->snd_seq, 0, OVS_DP_CMD_DEL);
1500 BUG_ON(err < 0);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001501
1502 __dp_destroy(dp);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001503 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001504
Johannes Berg2a94fe42013-11-19 15:19:39 +01001505 ovs_notify(&dp_datapath_genl_family, reply, info);
Jesse Grossccb13522011-10-25 19:26:31 -07001506
1507 return 0;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001508
1509err_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001510 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001511 kfree_skb(reply);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001512 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001513}
1514
1515static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1516{
1517 struct sk_buff *reply;
1518 struct datapath *dp;
1519 int err;
1520
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001521 reply = ovs_dp_cmd_alloc_info(info);
1522 if (!reply)
1523 return -ENOMEM;
1524
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001525 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001526 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001527 err = PTR_ERR(dp);
Jesse Grossccb13522011-10-25 19:26:31 -07001528 if (IS_ERR(dp))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001529 goto err_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001530
Thomas Graf43d4be92013-12-13 15:22:18 +01001531 ovs_dp_change(dp, info->attrs);
1532
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001533 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1534 info->snd_seq, 0, OVS_DP_CMD_NEW);
1535 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001536
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001537 ovs_unlock();
Johannes Berg2a94fe42013-11-19 15:19:39 +01001538 ovs_notify(&dp_datapath_genl_family, reply, info);
Jesse Grossccb13522011-10-25 19:26:31 -07001539
1540 return 0;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001541
1542err_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001543 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001544 kfree_skb(reply);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001545 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001546}
1547
1548static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1549{
1550 struct sk_buff *reply;
1551 struct datapath *dp;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001552 int err;
Jesse Grossccb13522011-10-25 19:26:31 -07001553
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001554 reply = ovs_dp_cmd_alloc_info(info);
1555 if (!reply)
1556 return -ENOMEM;
1557
1558 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001559 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001560 if (IS_ERR(dp)) {
1561 err = PTR_ERR(dp);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001562 goto err_unlock_free;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001563 }
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001564 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1565 info->snd_seq, 0, OVS_DP_CMD_NEW);
1566 BUG_ON(err < 0);
1567 rcu_read_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001568
Jesse Grossccb13522011-10-25 19:26:31 -07001569 return genlmsg_reply(reply, info);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001570
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001571err_unlock_free:
1572 rcu_read_unlock();
1573 kfree_skb(reply);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001574 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001575}
1576
1577static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1578{
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001579 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
Jesse Grossccb13522011-10-25 19:26:31 -07001580 struct datapath *dp;
1581 int skip = cb->args[0];
1582 int i = 0;
1583
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001584 rcu_read_lock();
1585 list_for_each_entry_rcu(dp, &ovs_net->dps, list_node) {
Ben Pfaff77676fd2012-01-17 13:33:39 +00001586 if (i >= skip &&
Eric W. Biederman15e47302012-09-07 20:12:54 +00001587 ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001588 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1589 OVS_DP_CMD_NEW) < 0)
1590 break;
1591 i++;
1592 }
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001593 rcu_read_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001594
1595 cb->args[0] = i;
1596
1597 return skb->len;
1598}
1599
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001600static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1601 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1602 [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1603 [OVS_DP_ATTR_USER_FEATURES] = { .type = NLA_U32 },
1604};
1605
stephen hemminger48e48a72014-07-16 11:25:52 -07001606static const struct genl_ops dp_datapath_genl_ops[] = {
Jesse Grossccb13522011-10-25 19:26:31 -07001607 { .cmd = OVS_DP_CMD_NEW,
1608 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1609 .policy = datapath_policy,
1610 .doit = ovs_dp_cmd_new
1611 },
1612 { .cmd = OVS_DP_CMD_DEL,
1613 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1614 .policy = datapath_policy,
1615 .doit = ovs_dp_cmd_del
1616 },
1617 { .cmd = OVS_DP_CMD_GET,
1618 .flags = 0, /* OK for unprivileged users. */
1619 .policy = datapath_policy,
1620 .doit = ovs_dp_cmd_get,
1621 .dumpit = ovs_dp_cmd_dump
1622 },
1623 { .cmd = OVS_DP_CMD_SET,
1624 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1625 .policy = datapath_policy,
1626 .doit = ovs_dp_cmd_set,
1627 },
1628};
1629
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001630static struct genl_family dp_datapath_genl_family = {
Jesse Grossccb13522011-10-25 19:26:31 -07001631 .id = GENL_ID_GENERATE,
1632 .hdrsize = sizeof(struct ovs_header),
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001633 .name = OVS_DATAPATH_FAMILY,
1634 .version = OVS_DATAPATH_VERSION,
1635 .maxattr = OVS_DP_ATTR_MAX,
Pravin B Shelar3a4e0d62013-04-23 07:48:48 +00001636 .netnsok = true,
1637 .parallel_ops = true,
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001638 .ops = dp_datapath_genl_ops,
1639 .n_ops = ARRAY_SIZE(dp_datapath_genl_ops),
1640 .mcgrps = &ovs_dp_datapath_multicast_group,
1641 .n_mcgrps = 1,
Jesse Grossccb13522011-10-25 19:26:31 -07001642};
1643
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001644/* Called with ovs_mutex or RCU read lock. */
Jesse Grossccb13522011-10-25 19:26:31 -07001645static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001646 u32 portid, u32 seq, u32 flags, u8 cmd)
Jesse Grossccb13522011-10-25 19:26:31 -07001647{
1648 struct ovs_header *ovs_header;
1649 struct ovs_vport_stats vport_stats;
1650 int err;
1651
Eric W. Biederman15e47302012-09-07 20:12:54 +00001652 ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
Jesse Grossccb13522011-10-25 19:26:31 -07001653 flags, cmd);
1654 if (!ovs_header)
1655 return -EMSGSIZE;
1656
1657 ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1658
David S. Miller028d6a62012-03-29 23:20:48 -04001659 if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
1660 nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
Alex Wang5cd667b2014-07-17 15:14:13 -07001661 nla_put_string(skb, OVS_VPORT_ATTR_NAME,
1662 vport->ops->get_name(vport)))
David S. Miller028d6a62012-03-29 23:20:48 -04001663 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001664
1665 ovs_vport_get_stats(vport, &vport_stats);
David S. Miller028d6a62012-03-29 23:20:48 -04001666 if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
1667 &vport_stats))
1668 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001669
Alex Wang5cd667b2014-07-17 15:14:13 -07001670 if (ovs_vport_get_upcall_portids(vport, skb))
1671 goto nla_put_failure;
1672
Jesse Grossccb13522011-10-25 19:26:31 -07001673 err = ovs_vport_get_options(vport, skb);
1674 if (err == -EMSGSIZE)
1675 goto error;
1676
1677 return genlmsg_end(skb, ovs_header);
1678
1679nla_put_failure:
1680 err = -EMSGSIZE;
1681error:
1682 genlmsg_cancel(skb, ovs_header);
1683 return err;
1684}
1685
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001686static struct sk_buff *ovs_vport_cmd_alloc_info(void)
1687{
1688 return nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1689}
1690
1691/* Called with ovs_mutex, only via ovs_dp_notify_wq(). */
Eric W. Biederman15e47302012-09-07 20:12:54 +00001692struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001693 u32 seq, u8 cmd)
1694{
1695 struct sk_buff *skb;
1696 int retval;
1697
1698 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1699 if (!skb)
1700 return ERR_PTR(-ENOMEM);
1701
Eric W. Biederman15e47302012-09-07 20:12:54 +00001702 retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
Jesse Grossa9341512013-03-26 15:48:38 -07001703 BUG_ON(retval < 0);
1704
Jesse Grossccb13522011-10-25 19:26:31 -07001705 return skb;
1706}
1707
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001708/* Called with ovs_mutex or RCU read lock. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001709static struct vport *lookup_vport(struct net *net,
1710 struct ovs_header *ovs_header,
Jesse Grossccb13522011-10-25 19:26:31 -07001711 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1712{
1713 struct datapath *dp;
1714 struct vport *vport;
1715
1716 if (a[OVS_VPORT_ATTR_NAME]) {
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001717 vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
Jesse Grossccb13522011-10-25 19:26:31 -07001718 if (!vport)
1719 return ERR_PTR(-ENODEV);
Ben Pfaff651a68e2012-03-06 15:04:04 -08001720 if (ovs_header->dp_ifindex &&
1721 ovs_header->dp_ifindex != get_dpifindex(vport->dp))
1722 return ERR_PTR(-ENODEV);
Jesse Grossccb13522011-10-25 19:26:31 -07001723 return vport;
1724 } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
1725 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1726
1727 if (port_no >= DP_MAX_PORTS)
1728 return ERR_PTR(-EFBIG);
1729
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001730 dp = get_dp(net, ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001731 if (!dp)
1732 return ERR_PTR(-ENODEV);
1733
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001734 vport = ovs_vport_ovsl_rcu(dp, port_no);
Jesse Grossccb13522011-10-25 19:26:31 -07001735 if (!vport)
Jarno Rajahalme14408db2013-01-09 14:27:35 -08001736 return ERR_PTR(-ENODEV);
Jesse Grossccb13522011-10-25 19:26:31 -07001737 return vport;
1738 } else
1739 return ERR_PTR(-EINVAL);
1740}
1741
1742static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
1743{
1744 struct nlattr **a = info->attrs;
1745 struct ovs_header *ovs_header = info->userhdr;
1746 struct vport_parms parms;
1747 struct sk_buff *reply;
1748 struct vport *vport;
1749 struct datapath *dp;
1750 u32 port_no;
1751 int err;
1752
Jesse Grossccb13522011-10-25 19:26:31 -07001753 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
1754 !a[OVS_VPORT_ATTR_UPCALL_PID])
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001755 return -EINVAL;
1756
1757 port_no = a[OVS_VPORT_ATTR_PORT_NO]
1758 ? nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]) : 0;
1759 if (port_no >= DP_MAX_PORTS)
1760 return -EFBIG;
1761
1762 reply = ovs_vport_cmd_alloc_info();
1763 if (!reply)
1764 return -ENOMEM;
Jesse Grossccb13522011-10-25 19:26:31 -07001765
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001766 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001767 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001768 err = -ENODEV;
1769 if (!dp)
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001770 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001771
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001772 if (port_no) {
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001773 vport = ovs_vport_ovsl(dp, port_no);
Jesse Grossccb13522011-10-25 19:26:31 -07001774 err = -EBUSY;
1775 if (vport)
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001776 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001777 } else {
1778 for (port_no = 1; ; port_no++) {
1779 if (port_no >= DP_MAX_PORTS) {
1780 err = -EFBIG;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001781 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001782 }
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001783 vport = ovs_vport_ovsl(dp, port_no);
Jesse Grossccb13522011-10-25 19:26:31 -07001784 if (!vport)
1785 break;
1786 }
1787 }
1788
1789 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
1790 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1791 parms.options = a[OVS_VPORT_ATTR_OPTIONS];
1792 parms.dp = dp;
1793 parms.port_no = port_no;
Alex Wang5cd667b2014-07-17 15:14:13 -07001794 parms.upcall_portids = a[OVS_VPORT_ATTR_UPCALL_PID];
Jesse Grossccb13522011-10-25 19:26:31 -07001795
1796 vport = new_vport(&parms);
1797 err = PTR_ERR(vport);
1798 if (IS_ERR(vport))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001799 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001800
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001801 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1802 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1803 BUG_ON(err < 0);
1804 ovs_unlock();
Thomas Grafed661182013-03-29 14:46:50 +01001805
Johannes Berg2a94fe42013-11-19 15:19:39 +01001806 ovs_notify(&dp_vport_genl_family, reply, info);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001807 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001808
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001809exit_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001810 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001811 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001812 return err;
1813}
1814
1815static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
1816{
1817 struct nlattr **a = info->attrs;
1818 struct sk_buff *reply;
1819 struct vport *vport;
1820 int err;
1821
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001822 reply = ovs_vport_cmd_alloc_info();
1823 if (!reply)
1824 return -ENOMEM;
1825
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001826 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001827 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
Jesse Grossccb13522011-10-25 19:26:31 -07001828 err = PTR_ERR(vport);
1829 if (IS_ERR(vport))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001830 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001831
Jesse Grossccb13522011-10-25 19:26:31 -07001832 if (a[OVS_VPORT_ATTR_TYPE] &&
Jesse Grossf44f3402013-05-13 08:15:26 -07001833 nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
Jesse Grossccb13522011-10-25 19:26:31 -07001834 err = -EINVAL;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001835 goto exit_unlock_free;
Jesse Grossa9341512013-03-26 15:48:38 -07001836 }
1837
Jesse Grossf44f3402013-05-13 08:15:26 -07001838 if (a[OVS_VPORT_ATTR_OPTIONS]) {
Jesse Grossccb13522011-10-25 19:26:31 -07001839 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
Jesse Grossf44f3402013-05-13 08:15:26 -07001840 if (err)
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001841 goto exit_unlock_free;
Jesse Grossf44f3402013-05-13 08:15:26 -07001842 }
Jesse Grossa9341512013-03-26 15:48:38 -07001843
Alex Wang5cd667b2014-07-17 15:14:13 -07001844
1845 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
1846 struct nlattr *ids = a[OVS_VPORT_ATTR_UPCALL_PID];
1847
1848 err = ovs_vport_set_upcall_portids(vport, ids);
1849 if (err)
1850 goto exit_unlock_free;
1851 }
Jesse Grossccb13522011-10-25 19:26:31 -07001852
Jesse Grossa9341512013-03-26 15:48:38 -07001853 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1854 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1855 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001856
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001857 ovs_unlock();
Johannes Berg2a94fe42013-11-19 15:19:39 +01001858 ovs_notify(&dp_vport_genl_family, reply, info);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001859 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001860
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001861exit_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001862 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001863 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001864 return err;
1865}
1866
1867static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
1868{
1869 struct nlattr **a = info->attrs;
1870 struct sk_buff *reply;
1871 struct vport *vport;
1872 int err;
1873
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001874 reply = ovs_vport_cmd_alloc_info();
1875 if (!reply)
1876 return -ENOMEM;
1877
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001878 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001879 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
Jesse Grossccb13522011-10-25 19:26:31 -07001880 err = PTR_ERR(vport);
1881 if (IS_ERR(vport))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001882 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001883
1884 if (vport->port_no == OVSP_LOCAL) {
1885 err = -EINVAL;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001886 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001887 }
1888
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001889 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1890 info->snd_seq, 0, OVS_VPORT_CMD_DEL);
1891 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001892 ovs_dp_detach_port(vport);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001893 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001894
Johannes Berg2a94fe42013-11-19 15:19:39 +01001895 ovs_notify(&dp_vport_genl_family, reply, info);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001896 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001897
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001898exit_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001899 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001900 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001901 return err;
1902}
1903
1904static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
1905{
1906 struct nlattr **a = info->attrs;
1907 struct ovs_header *ovs_header = info->userhdr;
1908 struct sk_buff *reply;
1909 struct vport *vport;
1910 int err;
1911
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001912 reply = ovs_vport_cmd_alloc_info();
1913 if (!reply)
1914 return -ENOMEM;
1915
Jesse Grossccb13522011-10-25 19:26:31 -07001916 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001917 vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
Jesse Grossccb13522011-10-25 19:26:31 -07001918 err = PTR_ERR(vport);
1919 if (IS_ERR(vport))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001920 goto exit_unlock_free;
1921 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1922 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1923 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001924 rcu_read_unlock();
1925
1926 return genlmsg_reply(reply, info);
1927
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001928exit_unlock_free:
Jesse Grossccb13522011-10-25 19:26:31 -07001929 rcu_read_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001930 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001931 return err;
1932}
1933
1934static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1935{
1936 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1937 struct datapath *dp;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001938 int bucket = cb->args[0], skip = cb->args[1];
1939 int i, j = 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001940
Jesse Grossccb13522011-10-25 19:26:31 -07001941 rcu_read_lock();
Jarno Rajahalme42ee19e2014-02-15 17:42:29 -08001942 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1943 if (!dp) {
1944 rcu_read_unlock();
1945 return -ENODEV;
1946 }
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001947 for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
Jesse Grossccb13522011-10-25 19:26:31 -07001948 struct vport *vport;
1949
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001950 j = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001951 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001952 if (j >= skip &&
1953 ovs_vport_cmd_fill_info(vport, skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001954 NETLINK_CB(cb->skb).portid,
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001955 cb->nlh->nlmsg_seq,
1956 NLM_F_MULTI,
1957 OVS_VPORT_CMD_NEW) < 0)
1958 goto out;
Jesse Grossccb13522011-10-25 19:26:31 -07001959
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001960 j++;
1961 }
1962 skip = 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001963 }
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001964out:
Jesse Grossccb13522011-10-25 19:26:31 -07001965 rcu_read_unlock();
1966
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001967 cb->args[0] = i;
1968 cb->args[1] = j;
Jesse Grossccb13522011-10-25 19:26:31 -07001969
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001970 return skb->len;
Jesse Grossccb13522011-10-25 19:26:31 -07001971}
1972
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001973static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
1974 [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1975 [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
1976 [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
1977 [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
1978 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1979 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
1980};
1981
stephen hemminger48e48a72014-07-16 11:25:52 -07001982static const struct genl_ops dp_vport_genl_ops[] = {
Jesse Grossccb13522011-10-25 19:26:31 -07001983 { .cmd = OVS_VPORT_CMD_NEW,
1984 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1985 .policy = vport_policy,
1986 .doit = ovs_vport_cmd_new
1987 },
1988 { .cmd = OVS_VPORT_CMD_DEL,
1989 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1990 .policy = vport_policy,
1991 .doit = ovs_vport_cmd_del
1992 },
1993 { .cmd = OVS_VPORT_CMD_GET,
1994 .flags = 0, /* OK for unprivileged users. */
1995 .policy = vport_policy,
1996 .doit = ovs_vport_cmd_get,
1997 .dumpit = ovs_vport_cmd_dump
1998 },
1999 { .cmd = OVS_VPORT_CMD_SET,
2000 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2001 .policy = vport_policy,
2002 .doit = ovs_vport_cmd_set,
2003 },
2004};
2005
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002006struct genl_family dp_vport_genl_family = {
2007 .id = GENL_ID_GENERATE,
2008 .hdrsize = sizeof(struct ovs_header),
2009 .name = OVS_VPORT_FAMILY,
2010 .version = OVS_VPORT_VERSION,
2011 .maxattr = OVS_VPORT_ATTR_MAX,
2012 .netnsok = true,
2013 .parallel_ops = true,
2014 .ops = dp_vport_genl_ops,
2015 .n_ops = ARRAY_SIZE(dp_vport_genl_ops),
2016 .mcgrps = &ovs_dp_vport_multicast_group,
2017 .n_mcgrps = 1,
Jesse Grossccb13522011-10-25 19:26:31 -07002018};
2019
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002020static struct genl_family * const dp_genl_families[] = {
2021 &dp_datapath_genl_family,
2022 &dp_vport_genl_family,
2023 &dp_flow_genl_family,
2024 &dp_packet_genl_family,
Jesse Grossccb13522011-10-25 19:26:31 -07002025};
2026
2027static void dp_unregister_genl(int n_families)
2028{
2029 int i;
2030
2031 for (i = 0; i < n_families; i++)
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002032 genl_unregister_family(dp_genl_families[i]);
Jesse Grossccb13522011-10-25 19:26:31 -07002033}
2034
2035static int dp_register_genl(void)
2036{
Jesse Grossccb13522011-10-25 19:26:31 -07002037 int err;
2038 int i;
2039
Jesse Grossccb13522011-10-25 19:26:31 -07002040 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
Jesse Grossccb13522011-10-25 19:26:31 -07002041
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002042 err = genl_register_family(dp_genl_families[i]);
Jesse Grossccb13522011-10-25 19:26:31 -07002043 if (err)
2044 goto error;
Jesse Grossccb13522011-10-25 19:26:31 -07002045 }
2046
2047 return 0;
2048
2049error:
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002050 dp_unregister_genl(i);
Jesse Grossccb13522011-10-25 19:26:31 -07002051 return err;
2052}
2053
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002054static int __net_init ovs_init_net(struct net *net)
2055{
2056 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2057
2058 INIT_LIST_HEAD(&ovs_net->dps);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002059 INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002060 return 0;
2061}
2062
2063static void __net_exit ovs_exit_net(struct net *net)
2064{
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002065 struct datapath *dp, *dp_next;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002066 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002067
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002068 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002069 list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
2070 __dp_destroy(dp);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002071 ovs_unlock();
2072
2073 cancel_work_sync(&ovs_net->dp_notify_work);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002074}
2075
2076static struct pernet_operations ovs_net_ops = {
2077 .init = ovs_init_net,
2078 .exit = ovs_exit_net,
2079 .id = &ovs_net_id,
2080 .size = sizeof(struct ovs_net),
2081};
2082
Jesse Grossccb13522011-10-25 19:26:31 -07002083static int __init dp_init(void)
2084{
Jesse Grossccb13522011-10-25 19:26:31 -07002085 int err;
2086
YOSHIFUJI Hideaki / 吉藤英明3523b292013-01-09 07:19:55 +00002087 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
Jesse Grossccb13522011-10-25 19:26:31 -07002088
2089 pr_info("Open vSwitch switching datapath\n");
2090
Andy Zhou971427f32014-09-15 19:37:25 -07002091 err = action_fifos_init();
Jesse Grossccb13522011-10-25 19:26:31 -07002092 if (err)
2093 goto error;
2094
Andy Zhou971427f32014-09-15 19:37:25 -07002095 err = ovs_internal_dev_rtnl_link_register();
2096 if (err)
2097 goto error_action_fifos_exit;
2098
Jiri Pirko5b9e7e12014-06-26 09:58:26 +02002099 err = ovs_flow_init();
2100 if (err)
2101 goto error_unreg_rtnl_link;
2102
Jesse Grossccb13522011-10-25 19:26:31 -07002103 err = ovs_vport_init();
2104 if (err)
2105 goto error_flow_exit;
2106
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002107 err = register_pernet_device(&ovs_net_ops);
Jesse Grossccb13522011-10-25 19:26:31 -07002108 if (err)
2109 goto error_vport_exit;
2110
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002111 err = register_netdevice_notifier(&ovs_dp_device_notifier);
2112 if (err)
2113 goto error_netns_exit;
2114
Jesse Grossccb13522011-10-25 19:26:31 -07002115 err = dp_register_genl();
2116 if (err < 0)
2117 goto error_unreg_notifier;
2118
Jesse Grossccb13522011-10-25 19:26:31 -07002119 return 0;
2120
2121error_unreg_notifier:
2122 unregister_netdevice_notifier(&ovs_dp_device_notifier);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002123error_netns_exit:
2124 unregister_pernet_device(&ovs_net_ops);
Jesse Grossccb13522011-10-25 19:26:31 -07002125error_vport_exit:
2126 ovs_vport_exit();
2127error_flow_exit:
2128 ovs_flow_exit();
Jiri Pirko5b9e7e12014-06-26 09:58:26 +02002129error_unreg_rtnl_link:
2130 ovs_internal_dev_rtnl_link_unregister();
Andy Zhou971427f32014-09-15 19:37:25 -07002131error_action_fifos_exit:
2132 action_fifos_exit();
Jesse Grossccb13522011-10-25 19:26:31 -07002133error:
2134 return err;
2135}
2136
2137static void dp_cleanup(void)
2138{
Jesse Grossccb13522011-10-25 19:26:31 -07002139 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
2140 unregister_netdevice_notifier(&ovs_dp_device_notifier);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002141 unregister_pernet_device(&ovs_net_ops);
2142 rcu_barrier();
Jesse Grossccb13522011-10-25 19:26:31 -07002143 ovs_vport_exit();
2144 ovs_flow_exit();
Jiri Pirko5b9e7e12014-06-26 09:58:26 +02002145 ovs_internal_dev_rtnl_link_unregister();
Andy Zhou971427f32014-09-15 19:37:25 -07002146 action_fifos_exit();
Jesse Grossccb13522011-10-25 19:26:31 -07002147}
2148
2149module_init(dp_init);
2150module_exit(dp_cleanup);
2151
2152MODULE_DESCRIPTION("Open vSwitch switching datapath");
2153MODULE_LICENSE("GPL");