blob: 9e3a2fae6a8f04d946c12a875f5d56a83a4fa4c0 [file] [log] [blame]
Jesse Grossccb13522011-10-25 19:26:31 -07001/*
Ben Pfaffad552002014-05-06 16:48:38 -07002 * Copyright (c) 2007-2014 Nicira, Inc.
Jesse Grossccb13522011-10-25 19:26:31 -07003 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of version 2 of the GNU General Public
6 * License as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 * 02110-1301, USA
17 */
18
19#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20
21#include <linux/init.h>
22#include <linux/module.h>
23#include <linux/if_arp.h>
24#include <linux/if_vlan.h>
25#include <linux/in.h>
26#include <linux/ip.h>
27#include <linux/jhash.h>
28#include <linux/delay.h>
29#include <linux/time.h>
30#include <linux/etherdevice.h>
31#include <linux/genetlink.h>
32#include <linux/kernel.h>
33#include <linux/kthread.h>
34#include <linux/mutex.h>
35#include <linux/percpu.h>
36#include <linux/rcupdate.h>
37#include <linux/tcp.h>
38#include <linux/udp.h>
Jesse Grossccb13522011-10-25 19:26:31 -070039#include <linux/ethtool.h>
40#include <linux/wait.h>
Jesse Grossccb13522011-10-25 19:26:31 -070041#include <asm/div64.h>
42#include <linux/highmem.h>
43#include <linux/netfilter_bridge.h>
44#include <linux/netfilter_ipv4.h>
45#include <linux/inetdevice.h>
46#include <linux/list.h>
47#include <linux/openvswitch.h>
48#include <linux/rculist.h>
49#include <linux/dmi.h>
Jesse Grossccb13522011-10-25 19:26:31 -070050#include <net/genetlink.h>
Pravin B Shelar46df7b82012-02-22 19:58:59 -080051#include <net/net_namespace.h>
52#include <net/netns/generic.h>
Jesse Grossccb13522011-10-25 19:26:31 -070053
54#include "datapath.h"
55#include "flow.h"
Andy Zhoue80857c2014-01-21 09:31:04 -080056#include "flow_table.h"
Pravin B Shelare6445712013-10-03 18:16:47 -070057#include "flow_netlink.h"
Jesse Grossccb13522011-10-25 19:26:31 -070058#include "vport-internal_dev.h"
Thomas Grafcff63a52013-04-29 13:06:41 +000059#include "vport-netdev.h"
Jesse Grossccb13522011-10-25 19:26:31 -070060
Pravin B Shelar8e4e1712013-04-15 13:23:03 -070061int ovs_net_id __read_mostly;
62
Pravin B Shelar0c200ef2014-05-06 16:44:50 -070063static struct genl_family dp_packet_genl_family;
64static struct genl_family dp_flow_genl_family;
65static struct genl_family dp_datapath_genl_family;
66
stephen hemminger48e48a72014-07-16 11:25:52 -070067static const struct genl_multicast_group ovs_dp_flow_multicast_group = {
68 .name = OVS_FLOW_MCGROUP,
Pravin B Shelar0c200ef2014-05-06 16:44:50 -070069};
70
stephen hemminger48e48a72014-07-16 11:25:52 -070071static const struct genl_multicast_group ovs_dp_datapath_multicast_group = {
72 .name = OVS_DATAPATH_MCGROUP,
Pravin B Shelar0c200ef2014-05-06 16:44:50 -070073};
74
stephen hemminger48e48a72014-07-16 11:25:52 -070075static const struct genl_multicast_group ovs_dp_vport_multicast_group = {
76 .name = OVS_VPORT_MCGROUP,
Pravin B Shelar0c200ef2014-05-06 16:44:50 -070077};
78
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -070079/* Check if need to build a reply message.
80 * OVS userspace sets the NLM_F_ECHO flag if it needs the reply. */
Samuel Gauthier9b67aa42014-09-18 10:31:04 +020081static bool ovs_must_notify(struct genl_family *family, struct genl_info *info,
82 unsigned int group)
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -070083{
84 return info->nlhdr->nlmsg_flags & NLM_F_ECHO ||
Samuel Gauthier9b67aa42014-09-18 10:31:04 +020085 genl_has_listeners(family, genl_info_net(info)->genl_sock,
86 group);
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -070087}
88
Johannes Berg68eb5502013-11-19 15:19:38 +010089static void ovs_notify(struct genl_family *family,
Johannes Berg2a94fe42013-11-19 15:19:39 +010090 struct sk_buff *skb, struct genl_info *info)
Thomas Grafed661182013-03-29 14:46:50 +010091{
Johannes Berg68eb5502013-11-19 15:19:38 +010092 genl_notify(family, skb, genl_info_net(info), info->snd_portid,
Johannes Berg2a94fe42013-11-19 15:19:39 +010093 0, info->nlhdr, GFP_KERNEL);
Thomas Grafed661182013-03-29 14:46:50 +010094}
95
Pravin B Shelar46df7b82012-02-22 19:58:59 -080096/**
Jesse Grossccb13522011-10-25 19:26:31 -070097 * DOC: Locking:
98 *
Pravin B Shelar8e4e1712013-04-15 13:23:03 -070099 * All writes e.g. Writes to device state (add/remove datapath, port, set
100 * operations on vports, etc.), Writes to other state (flow table
101 * modifications, set miscellaneous datapath parameters, etc.) are protected
102 * by ovs_lock.
Jesse Grossccb13522011-10-25 19:26:31 -0700103 *
104 * Reads are protected by RCU.
105 *
106 * There are a few special cases (mostly stats) that have their own
107 * synchronization but they nest under all of above and don't interact with
108 * each other.
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700109 *
110 * The RTNL lock nests inside ovs_mutex.
Jesse Grossccb13522011-10-25 19:26:31 -0700111 */
112
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700113static DEFINE_MUTEX(ovs_mutex);
114
115void ovs_lock(void)
116{
117 mutex_lock(&ovs_mutex);
118}
119
120void ovs_unlock(void)
121{
122 mutex_unlock(&ovs_mutex);
123}
124
125#ifdef CONFIG_LOCKDEP
126int lockdep_ovsl_is_held(void)
127{
128 if (debug_locks)
129 return lockdep_is_held(&ovs_mutex);
130 else
131 return 1;
132}
133#endif
134
Jesse Grossccb13522011-10-25 19:26:31 -0700135static struct vport *new_vport(const struct vport_parms *);
Thomas Graf8055a892013-12-13 15:22:20 +0100136static int queue_gso_packets(struct datapath *dp, struct sk_buff *,
Jesse Grossccb13522011-10-25 19:26:31 -0700137 const struct dp_upcall_info *);
Thomas Graf8055a892013-12-13 15:22:20 +0100138static int queue_userspace_packet(struct datapath *dp, struct sk_buff *,
Jesse Grossccb13522011-10-25 19:26:31 -0700139 const struct dp_upcall_info *);
140
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700141/* Must be called with rcu_read_lock or ovs_mutex. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800142static struct datapath *get_dp(struct net *net, int dp_ifindex)
Jesse Grossccb13522011-10-25 19:26:31 -0700143{
144 struct datapath *dp = NULL;
145 struct net_device *dev;
146
147 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800148 dev = dev_get_by_index_rcu(net, dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -0700149 if (dev) {
150 struct vport *vport = ovs_internal_dev_get_vport(dev);
151 if (vport)
152 dp = vport->dp;
153 }
154 rcu_read_unlock();
155
156 return dp;
157}
158
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700159/* Must be called with rcu_read_lock or ovs_mutex. */
Andy Zhou971427f32014-09-15 19:37:25 -0700160const char *ovs_dp_name(const struct datapath *dp)
Jesse Grossccb13522011-10-25 19:26:31 -0700161{
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700162 struct vport *vport = ovs_vport_ovsl_rcu(dp, OVSP_LOCAL);
Jesse Grossccb13522011-10-25 19:26:31 -0700163 return vport->ops->get_name(vport);
164}
165
166static int get_dpifindex(struct datapath *dp)
167{
168 struct vport *local;
169 int ifindex;
170
171 rcu_read_lock();
172
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700173 local = ovs_vport_rcu(dp, OVSP_LOCAL);
Jesse Grossccb13522011-10-25 19:26:31 -0700174 if (local)
Thomas Grafcff63a52013-04-29 13:06:41 +0000175 ifindex = netdev_vport_priv(local)->dev->ifindex;
Jesse Grossccb13522011-10-25 19:26:31 -0700176 else
177 ifindex = 0;
178
179 rcu_read_unlock();
180
181 return ifindex;
182}
183
184static void destroy_dp_rcu(struct rcu_head *rcu)
185{
186 struct datapath *dp = container_of(rcu, struct datapath, rcu);
187
Jesse Grossccb13522011-10-25 19:26:31 -0700188 free_percpu(dp->stats_percpu);
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800189 release_net(ovs_dp_get_net(dp));
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700190 kfree(dp->ports);
Jesse Grossccb13522011-10-25 19:26:31 -0700191 kfree(dp);
192}
193
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700194static struct hlist_head *vport_hash_bucket(const struct datapath *dp,
195 u16 port_no)
196{
197 return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)];
198}
199
Jarno Rajahalmebb6f9a72014-05-05 11:32:17 -0700200/* Called with ovs_mutex or RCU read lock. */
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700201struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no)
202{
203 struct vport *vport;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700204 struct hlist_head *head;
205
206 head = vport_hash_bucket(dp, port_no);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800207 hlist_for_each_entry_rcu(vport, head, dp_hash_node) {
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700208 if (vport->port_no == port_no)
209 return vport;
210 }
211 return NULL;
212}
213
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700214/* Called with ovs_mutex. */
Jesse Grossccb13522011-10-25 19:26:31 -0700215static struct vport *new_vport(const struct vport_parms *parms)
216{
217 struct vport *vport;
218
219 vport = ovs_vport_add(parms);
220 if (!IS_ERR(vport)) {
221 struct datapath *dp = parms->dp;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700222 struct hlist_head *head = vport_hash_bucket(dp, vport->port_no);
Jesse Grossccb13522011-10-25 19:26:31 -0700223
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700224 hlist_add_head_rcu(&vport->dp_hash_node, head);
Jesse Grossccb13522011-10-25 19:26:31 -0700225 }
Jesse Grossccb13522011-10-25 19:26:31 -0700226 return vport;
227}
228
Jesse Grossccb13522011-10-25 19:26:31 -0700229void ovs_dp_detach_port(struct vport *p)
230{
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700231 ASSERT_OVSL();
Jesse Grossccb13522011-10-25 19:26:31 -0700232
233 /* First drop references to device. */
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700234 hlist_del_rcu(&p->dp_hash_node);
Jesse Grossccb13522011-10-25 19:26:31 -0700235
236 /* Then destroy it. */
237 ovs_vport_del(p);
238}
239
240/* Must be called with rcu_read_lock. */
Pravin B Shelar8c8b1b82014-09-15 19:28:44 -0700241void ovs_dp_process_packet(struct sk_buff *skb, struct sw_flow_key *key)
Jesse Grossccb13522011-10-25 19:26:31 -0700242{
Pravin B Shelar83c8df22014-09-15 19:20:31 -0700243 const struct vport *p = OVS_CB(skb)->input_vport;
Jesse Grossccb13522011-10-25 19:26:31 -0700244 struct datapath *dp = p->dp;
245 struct sw_flow *flow;
246 struct dp_stats_percpu *stats;
Jesse Grossccb13522011-10-25 19:26:31 -0700247 u64 *stats_counter;
Andy Zhou1bd71162013-10-22 10:42:46 -0700248 u32 n_mask_hit;
Jesse Grossccb13522011-10-25 19:26:31 -0700249
Shan Wei404f2f12012-11-13 09:52:25 +0800250 stats = this_cpu_ptr(dp->stats_percpu);
Jesse Grossccb13522011-10-25 19:26:31 -0700251
Jesse Grossccb13522011-10-25 19:26:31 -0700252 /* Look up flow. */
Pravin B Shelar8c8b1b82014-09-15 19:28:44 -0700253 flow = ovs_flow_tbl_lookup_stats(&dp->table, key, &n_mask_hit);
Jesse Grossccb13522011-10-25 19:26:31 -0700254 if (unlikely(!flow)) {
255 struct dp_upcall_info upcall;
Pravin B Shelar8c8b1b82014-09-15 19:28:44 -0700256 int error;
Jesse Grossccb13522011-10-25 19:26:31 -0700257
258 upcall.cmd = OVS_PACKET_CMD_MISS;
Pravin B Shelar8c8b1b82014-09-15 19:28:44 -0700259 upcall.key = key;
Jesse Grossccb13522011-10-25 19:26:31 -0700260 upcall.userdata = NULL;
Alex Wang5cd667b2014-07-17 15:14:13 -0700261 upcall.portid = ovs_vport_find_upcall_portid(p, skb);
Li RongQingc5eba0b2014-09-03 17:43:45 +0800262 error = ovs_dp_upcall(dp, skb, &upcall);
263 if (unlikely(error))
264 kfree_skb(skb);
265 else
266 consume_skb(skb);
Jesse Grossccb13522011-10-25 19:26:31 -0700267 stats_counter = &stats->n_missed;
268 goto out;
269 }
270
271 OVS_CB(skb)->flow = flow;
272
Pravin B Shelar8c8b1b82014-09-15 19:28:44 -0700273 ovs_flow_stats_update(OVS_CB(skb)->flow, key->tp.flags, skb);
274 ovs_execute_actions(dp, skb, key);
Pravin B Shelare298e502013-10-29 17:22:21 -0700275 stats_counter = &stats->n_hit;
Jesse Grossccb13522011-10-25 19:26:31 -0700276
277out:
278 /* Update datapath statistics. */
WANG Congdf9d9fd2014-02-14 15:10:46 -0800279 u64_stats_update_begin(&stats->syncp);
Jesse Grossccb13522011-10-25 19:26:31 -0700280 (*stats_counter)++;
Andy Zhou1bd71162013-10-22 10:42:46 -0700281 stats->n_mask_hit += n_mask_hit;
WANG Congdf9d9fd2014-02-14 15:10:46 -0800282 u64_stats_update_end(&stats->syncp);
Jesse Grossccb13522011-10-25 19:26:31 -0700283}
284
Jesse Grossccb13522011-10-25 19:26:31 -0700285int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800286 const struct dp_upcall_info *upcall_info)
Jesse Grossccb13522011-10-25 19:26:31 -0700287{
288 struct dp_stats_percpu *stats;
Jesse Grossccb13522011-10-25 19:26:31 -0700289 int err;
290
Eric W. Biederman15e47302012-09-07 20:12:54 +0000291 if (upcall_info->portid == 0) {
Jesse Grossccb13522011-10-25 19:26:31 -0700292 err = -ENOTCONN;
293 goto err;
294 }
295
Jesse Grossccb13522011-10-25 19:26:31 -0700296 if (!skb_is_gso(skb))
Thomas Graf8055a892013-12-13 15:22:20 +0100297 err = queue_userspace_packet(dp, skb, upcall_info);
Jesse Grossccb13522011-10-25 19:26:31 -0700298 else
Thomas Graf8055a892013-12-13 15:22:20 +0100299 err = queue_gso_packets(dp, skb, upcall_info);
Jesse Grossccb13522011-10-25 19:26:31 -0700300 if (err)
301 goto err;
302
303 return 0;
304
305err:
Shan Wei404f2f12012-11-13 09:52:25 +0800306 stats = this_cpu_ptr(dp->stats_percpu);
Jesse Grossccb13522011-10-25 19:26:31 -0700307
WANG Congdf9d9fd2014-02-14 15:10:46 -0800308 u64_stats_update_begin(&stats->syncp);
Jesse Grossccb13522011-10-25 19:26:31 -0700309 stats->n_lost++;
WANG Congdf9d9fd2014-02-14 15:10:46 -0800310 u64_stats_update_end(&stats->syncp);
Jesse Grossccb13522011-10-25 19:26:31 -0700311
312 return err;
313}
314
Thomas Graf8055a892013-12-13 15:22:20 +0100315static int queue_gso_packets(struct datapath *dp, struct sk_buff *skb,
Jesse Grossccb13522011-10-25 19:26:31 -0700316 const struct dp_upcall_info *upcall_info)
317{
Ben Pfaffa1b5d0d2012-07-20 14:47:54 -0700318 unsigned short gso_type = skb_shinfo(skb)->gso_type;
Jesse Grossccb13522011-10-25 19:26:31 -0700319 struct dp_upcall_info later_info;
320 struct sw_flow_key later_key;
321 struct sk_buff *segs, *nskb;
322 int err;
323
Thomas Graf09c5e602013-12-13 15:22:22 +0100324 segs = __skb_gso_segment(skb, NETIF_F_SG, false);
Pravin B Shelar92e5dfc2012-07-20 14:46:29 -0700325 if (IS_ERR(segs))
326 return PTR_ERR(segs);
Jesse Grossccb13522011-10-25 19:26:31 -0700327
328 /* Queue all of the segments. */
329 skb = segs;
330 do {
Thomas Graf8055a892013-12-13 15:22:20 +0100331 err = queue_userspace_packet(dp, skb, upcall_info);
Jesse Grossccb13522011-10-25 19:26:31 -0700332 if (err)
333 break;
334
Ben Pfaffa1b5d0d2012-07-20 14:47:54 -0700335 if (skb == segs && gso_type & SKB_GSO_UDP) {
Jesse Grossccb13522011-10-25 19:26:31 -0700336 /* The initial flow key extracted by ovs_flow_extract()
337 * in this case is for a first fragment, so we need to
338 * properly mark later fragments.
339 */
340 later_key = *upcall_info->key;
341 later_key.ip.frag = OVS_FRAG_TYPE_LATER;
342
343 later_info = *upcall_info;
344 later_info.key = &later_key;
345 upcall_info = &later_info;
346 }
347 } while ((skb = skb->next));
348
349 /* Free all of the segments. */
350 skb = segs;
351 do {
352 nskb = skb->next;
353 if (err)
354 kfree_skb(skb);
355 else
356 consume_skb(skb);
357 } while ((skb = nskb));
358 return err;
359}
360
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100361static size_t key_attr_size(void)
362{
363 return nla_total_size(4) /* OVS_KEY_ATTR_PRIORITY */
Pravin B Shelar7d5437c2013-06-17 17:50:18 -0700364 + nla_total_size(0) /* OVS_KEY_ATTR_TUNNEL */
365 + nla_total_size(8) /* OVS_TUNNEL_KEY_ATTR_ID */
366 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_SRC */
367 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_DST */
368 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TOS */
369 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TTL */
370 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT */
371 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_CSUM */
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100372 + nla_total_size(4) /* OVS_KEY_ATTR_IN_PORT */
373 + nla_total_size(4) /* OVS_KEY_ATTR_SKB_MARK */
374 + nla_total_size(12) /* OVS_KEY_ATTR_ETHERNET */
375 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
376 + nla_total_size(4) /* OVS_KEY_ATTR_8021Q */
377 + nla_total_size(0) /* OVS_KEY_ATTR_ENCAP */
378 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
379 + nla_total_size(40) /* OVS_KEY_ATTR_IPV6 */
380 + nla_total_size(2) /* OVS_KEY_ATTR_ICMPV6 */
381 + nla_total_size(28); /* OVS_KEY_ATTR_ND */
382}
383
Thomas Grafbda56f12013-12-13 15:22:21 +0100384static size_t upcall_msg_size(const struct nlattr *userdata,
385 unsigned int hdrlen)
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100386{
387 size_t size = NLMSG_ALIGN(sizeof(struct ovs_header))
Thomas Grafbda56f12013-12-13 15:22:21 +0100388 + nla_total_size(hdrlen) /* OVS_PACKET_ATTR_PACKET */
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100389 + nla_total_size(key_attr_size()); /* OVS_PACKET_ATTR_KEY */
390
391 /* OVS_PACKET_ATTR_USERDATA */
392 if (userdata)
393 size += NLA_ALIGN(userdata->nla_len);
394
395 return size;
396}
397
Thomas Graf8055a892013-12-13 15:22:20 +0100398static int queue_userspace_packet(struct datapath *dp, struct sk_buff *skb,
Jesse Grossccb13522011-10-25 19:26:31 -0700399 const struct dp_upcall_info *upcall_info)
400{
401 struct ovs_header *upcall;
402 struct sk_buff *nskb = NULL;
Li RongQing4ee45ea2014-09-02 20:52:28 +0800403 struct sk_buff *user_skb = NULL; /* to be queued to userspace */
Jesse Grossccb13522011-10-25 19:26:31 -0700404 struct nlattr *nla;
Thomas Graf795449d2013-11-30 13:21:32 +0100405 struct genl_info info = {
Thomas Graf8055a892013-12-13 15:22:20 +0100406 .dst_sk = ovs_dp_get_net(dp)->genl_sock,
Thomas Graf795449d2013-11-30 13:21:32 +0100407 .snd_portid = upcall_info->portid,
408 };
409 size_t len;
Thomas Grafbda56f12013-12-13 15:22:21 +0100410 unsigned int hlen;
Thomas Graf8055a892013-12-13 15:22:20 +0100411 int err, dp_ifindex;
412
413 dp_ifindex = get_dpifindex(dp);
414 if (!dp_ifindex)
415 return -ENODEV;
Jesse Grossccb13522011-10-25 19:26:31 -0700416
417 if (vlan_tx_tag_present(skb)) {
418 nskb = skb_clone(skb, GFP_ATOMIC);
419 if (!nskb)
420 return -ENOMEM;
421
Patrick McHardy86a9bad2013-04-19 02:04:30 +0000422 nskb = __vlan_put_tag(nskb, nskb->vlan_proto, vlan_tx_tag_get(nskb));
Dan Carpenter8aa51d62012-05-13 08:44:18 +0000423 if (!nskb)
Jesse Grossccb13522011-10-25 19:26:31 -0700424 return -ENOMEM;
425
426 nskb->vlan_tci = 0;
427 skb = nskb;
428 }
429
430 if (nla_attr_size(skb->len) > USHRT_MAX) {
431 err = -EFBIG;
432 goto out;
433 }
434
Thomas Grafbda56f12013-12-13 15:22:21 +0100435 /* Complete checksum if needed */
436 if (skb->ip_summed == CHECKSUM_PARTIAL &&
437 (err = skb_checksum_help(skb)))
438 goto out;
439
440 /* Older versions of OVS user space enforce alignment of the last
441 * Netlink attribute to NLA_ALIGNTO which would require extensive
442 * padding logic. Only perform zerocopy if padding is not required.
443 */
444 if (dp->user_features & OVS_DP_F_UNALIGNED)
445 hlen = skb_zerocopy_headlen(skb);
446 else
447 hlen = skb->len;
448
449 len = upcall_msg_size(upcall_info->userdata, hlen);
Thomas Graf795449d2013-11-30 13:21:32 +0100450 user_skb = genlmsg_new_unicast(len, &info, GFP_ATOMIC);
Jesse Grossccb13522011-10-25 19:26:31 -0700451 if (!user_skb) {
452 err = -ENOMEM;
453 goto out;
454 }
455
456 upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
457 0, upcall_info->cmd);
458 upcall->dp_ifindex = dp_ifindex;
459
460 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
Andy Zhouf53e3832014-07-17 15:17:44 -0700461 err = ovs_nla_put_flow(upcall_info->key, upcall_info->key, user_skb);
462 BUG_ON(err);
Jesse Grossccb13522011-10-25 19:26:31 -0700463 nla_nest_end(user_skb, nla);
464
465 if (upcall_info->userdata)
Ben Pfaff44901082013-02-15 17:29:22 -0800466 __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA,
467 nla_len(upcall_info->userdata),
468 nla_data(upcall_info->userdata));
Jesse Grossccb13522011-10-25 19:26:31 -0700469
Thomas Grafbda56f12013-12-13 15:22:21 +0100470 /* Only reserve room for attribute header, packet data is added
471 * in skb_zerocopy() */
472 if (!(nla = nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, 0))) {
473 err = -ENOBUFS;
474 goto out;
475 }
476 nla->nla_len = nla_attr_size(skb->len);
Jesse Grossccb13522011-10-25 19:26:31 -0700477
Zoltan Kiss36d5fe62014-03-26 22:37:45 +0000478 err = skb_zerocopy(user_skb, skb, skb->len, hlen);
479 if (err)
480 goto out;
Jesse Grossccb13522011-10-25 19:26:31 -0700481
Thomas Grafaea0bb42014-01-14 16:27:49 +0000482 /* Pad OVS_PACKET_ATTR_PACKET if linear copy was performed */
483 if (!(dp->user_features & OVS_DP_F_UNALIGNED)) {
484 size_t plen = NLA_ALIGN(user_skb->len) - user_skb->len;
485
486 if (plen > 0)
487 memset(skb_put(user_skb, plen), 0, plen);
488 }
489
Thomas Grafbda56f12013-12-13 15:22:21 +0100490 ((struct nlmsghdr *) user_skb->data)->nlmsg_len = user_skb->len;
491
Thomas Graf8055a892013-12-13 15:22:20 +0100492 err = genlmsg_unicast(ovs_dp_get_net(dp), user_skb, upcall_info->portid);
Li RongQing4ee45ea2014-09-02 20:52:28 +0800493 user_skb = NULL;
Jesse Grossccb13522011-10-25 19:26:31 -0700494out:
Zoltan Kiss36d5fe62014-03-26 22:37:45 +0000495 if (err)
496 skb_tx_error(skb);
Li RongQing4ee45ea2014-09-02 20:52:28 +0800497 kfree_skb(user_skb);
Jesse Grossccb13522011-10-25 19:26:31 -0700498 kfree_skb(nskb);
499 return err;
500}
501
Jesse Grossccb13522011-10-25 19:26:31 -0700502static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
503{
504 struct ovs_header *ovs_header = info->userhdr;
505 struct nlattr **a = info->attrs;
506 struct sw_flow_actions *acts;
507 struct sk_buff *packet;
508 struct sw_flow *flow;
509 struct datapath *dp;
510 struct ethhdr *eth;
Pravin B Shelar83c8df22014-09-15 19:20:31 -0700511 struct vport *input_vport;
Jesse Grossccb13522011-10-25 19:26:31 -0700512 int len;
513 int err;
Jesse Grossccb13522011-10-25 19:26:31 -0700514
515 err = -EINVAL;
516 if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
Thomas Grafdded45fc2013-03-29 14:46:47 +0100517 !a[OVS_PACKET_ATTR_ACTIONS])
Jesse Grossccb13522011-10-25 19:26:31 -0700518 goto err;
519
520 len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
521 packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
522 err = -ENOMEM;
523 if (!packet)
524 goto err;
525 skb_reserve(packet, NET_IP_ALIGN);
526
Thomas Graf32686a92013-03-29 14:46:48 +0100527 nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
Jesse Grossccb13522011-10-25 19:26:31 -0700528
529 skb_reset_mac_header(packet);
530 eth = eth_hdr(packet);
531
532 /* Normally, setting the skb 'protocol' field would be handled by a
533 * call to eth_type_trans(), but it assumes there's a sending
534 * device, which we may not have. */
Simon Hormane5c5d222013-03-28 13:38:25 +0900535 if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN)
Jesse Grossccb13522011-10-25 19:26:31 -0700536 packet->protocol = eth->h_proto;
537 else
538 packet->protocol = htons(ETH_P_802_2);
539
540 /* Build an sw_flow for sending this packet. */
Jarno Rajahalme23dabf82014-03-27 12:35:23 -0700541 flow = ovs_flow_alloc();
Jesse Grossccb13522011-10-25 19:26:31 -0700542 err = PTR_ERR(flow);
543 if (IS_ERR(flow))
544 goto err_kfree_skb;
545
Pravin B Shelar83c8df22014-09-15 19:20:31 -0700546 err = ovs_flow_key_extract_userspace(a[OVS_PACKET_ATTR_KEY], packet,
547 &flow->key);
Jesse Grossccb13522011-10-25 19:26:31 -0700548 if (err)
549 goto err_flow_free;
550
Pravin B Shelare6445712013-10-03 18:16:47 -0700551 acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_PACKET_ATTR_ACTIONS]));
Jesse Grossccb13522011-10-25 19:26:31 -0700552 err = PTR_ERR(acts);
553 if (IS_ERR(acts))
554 goto err_flow_free;
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700555
Pravin B Shelare6445712013-10-03 18:16:47 -0700556 err = ovs_nla_copy_actions(a[OVS_PACKET_ATTR_ACTIONS],
557 &flow->key, 0, &acts);
Jesse Grossccb13522011-10-25 19:26:31 -0700558 rcu_assign_pointer(flow->sf_acts, acts);
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700559 if (err)
560 goto err_flow_free;
Jesse Grossccb13522011-10-25 19:26:31 -0700561
562 OVS_CB(packet)->flow = flow;
563 packet->priority = flow->key.phy.priority;
Ansis Atteka39c7caeb2012-11-26 11:24:11 -0800564 packet->mark = flow->key.phy.skb_mark;
Jesse Grossccb13522011-10-25 19:26:31 -0700565
566 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800567 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -0700568 err = -ENODEV;
569 if (!dp)
570 goto err_unlock;
571
Pravin B Shelar83c8df22014-09-15 19:20:31 -0700572 input_vport = ovs_vport_rcu(dp, flow->key.phy.in_port);
573 if (!input_vport)
574 input_vport = ovs_vport_rcu(dp, OVSP_LOCAL);
575
576 if (!input_vport)
577 goto err_unlock;
578
579 OVS_CB(packet)->input_vport = input_vport;
580
Jesse Grossccb13522011-10-25 19:26:31 -0700581 local_bh_disable();
Pravin B Shelar2ff3e4e2014-09-15 19:15:28 -0700582 err = ovs_execute_actions(dp, packet, &flow->key);
Jesse Grossccb13522011-10-25 19:26:31 -0700583 local_bh_enable();
584 rcu_read_unlock();
585
Andy Zhou03f0d912013-08-07 20:01:00 -0700586 ovs_flow_free(flow, false);
Jesse Grossccb13522011-10-25 19:26:31 -0700587 return err;
588
589err_unlock:
590 rcu_read_unlock();
591err_flow_free:
Andy Zhou03f0d912013-08-07 20:01:00 -0700592 ovs_flow_free(flow, false);
Jesse Grossccb13522011-10-25 19:26:31 -0700593err_kfree_skb:
594 kfree_skb(packet);
595err:
596 return err;
597}
598
599static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
Thomas Grafdded45fc2013-03-29 14:46:47 +0100600 [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
Jesse Grossccb13522011-10-25 19:26:31 -0700601 [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
602 [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
603};
604
Johannes Berg4534de82013-11-14 17:14:46 +0100605static const struct genl_ops dp_packet_genl_ops[] = {
Jesse Grossccb13522011-10-25 19:26:31 -0700606 { .cmd = OVS_PACKET_CMD_EXECUTE,
607 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
608 .policy = packet_policy,
609 .doit = ovs_packet_cmd_execute
610 }
611};
612
Pravin B Shelar0c200ef2014-05-06 16:44:50 -0700613static struct genl_family dp_packet_genl_family = {
614 .id = GENL_ID_GENERATE,
615 .hdrsize = sizeof(struct ovs_header),
616 .name = OVS_PACKET_FAMILY,
617 .version = OVS_PACKET_VERSION,
618 .maxattr = OVS_PACKET_ATTR_MAX,
619 .netnsok = true,
620 .parallel_ops = true,
621 .ops = dp_packet_genl_ops,
622 .n_ops = ARRAY_SIZE(dp_packet_genl_ops),
623};
624
Andy Zhou1bd71162013-10-22 10:42:46 -0700625static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats,
626 struct ovs_dp_megaflow_stats *mega_stats)
Jesse Grossccb13522011-10-25 19:26:31 -0700627{
628 int i;
Jesse Grossccb13522011-10-25 19:26:31 -0700629
Andy Zhou1bd71162013-10-22 10:42:46 -0700630 memset(mega_stats, 0, sizeof(*mega_stats));
631
Pravin B Shelarb637e492013-10-04 00:14:23 -0700632 stats->n_flows = ovs_flow_tbl_count(&dp->table);
Andy Zhou1bd71162013-10-22 10:42:46 -0700633 mega_stats->n_masks = ovs_flow_tbl_num_masks(&dp->table);
Jesse Grossccb13522011-10-25 19:26:31 -0700634
635 stats->n_hit = stats->n_missed = stats->n_lost = 0;
Andy Zhou1bd71162013-10-22 10:42:46 -0700636
Jesse Grossccb13522011-10-25 19:26:31 -0700637 for_each_possible_cpu(i) {
638 const struct dp_stats_percpu *percpu_stats;
639 struct dp_stats_percpu local_stats;
640 unsigned int start;
641
642 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
643
644 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700645 start = u64_stats_fetch_begin_irq(&percpu_stats->syncp);
Jesse Grossccb13522011-10-25 19:26:31 -0700646 local_stats = *percpu_stats;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700647 } while (u64_stats_fetch_retry_irq(&percpu_stats->syncp, start));
Jesse Grossccb13522011-10-25 19:26:31 -0700648
649 stats->n_hit += local_stats.n_hit;
650 stats->n_missed += local_stats.n_missed;
651 stats->n_lost += local_stats.n_lost;
Andy Zhou1bd71162013-10-22 10:42:46 -0700652 mega_stats->n_mask_hit += local_stats.n_mask_hit;
Jesse Grossccb13522011-10-25 19:26:31 -0700653 }
654}
655
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100656static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts)
657{
658 return NLMSG_ALIGN(sizeof(struct ovs_header))
659 + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_KEY */
Andy Zhou03f0d912013-08-07 20:01:00 -0700660 + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_MASK */
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100661 + nla_total_size(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
662 + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
663 + nla_total_size(8) /* OVS_FLOW_ATTR_USED */
664 + nla_total_size(acts->actions_len); /* OVS_FLOW_ATTR_ACTIONS */
665}
666
Jarno Rajahalmebb6f9a72014-05-05 11:32:17 -0700667/* Called with ovs_mutex or RCU read lock. */
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700668static int ovs_flow_cmd_fill_info(const struct sw_flow *flow, int dp_ifindex,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000669 struct sk_buff *skb, u32 portid,
Jesse Grossccb13522011-10-25 19:26:31 -0700670 u32 seq, u32 flags, u8 cmd)
671{
672 const int skb_orig_len = skb->len;
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700673 struct nlattr *start;
Jesse Grossccb13522011-10-25 19:26:31 -0700674 struct ovs_flow_stats stats;
Pravin B Shelare298e502013-10-29 17:22:21 -0700675 __be16 tcp_flags;
676 unsigned long used;
Jesse Grossccb13522011-10-25 19:26:31 -0700677 struct ovs_header *ovs_header;
678 struct nlattr *nla;
Jesse Grossccb13522011-10-25 19:26:31 -0700679 int err;
680
Eric W. Biederman15e47302012-09-07 20:12:54 +0000681 ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family, flags, cmd);
Jesse Grossccb13522011-10-25 19:26:31 -0700682 if (!ovs_header)
683 return -EMSGSIZE;
684
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700685 ovs_header->dp_ifindex = dp_ifindex;
Jesse Grossccb13522011-10-25 19:26:31 -0700686
Andy Zhou03f0d912013-08-07 20:01:00 -0700687 /* Fill flow key. */
Jesse Grossccb13522011-10-25 19:26:31 -0700688 nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
689 if (!nla)
690 goto nla_put_failure;
Andy Zhou03f0d912013-08-07 20:01:00 -0700691
Pravin B Shelare6445712013-10-03 18:16:47 -0700692 err = ovs_nla_put_flow(&flow->unmasked_key, &flow->unmasked_key, skb);
Jesse Grossccb13522011-10-25 19:26:31 -0700693 if (err)
694 goto error;
695 nla_nest_end(skb, nla);
696
Andy Zhou03f0d912013-08-07 20:01:00 -0700697 nla = nla_nest_start(skb, OVS_FLOW_ATTR_MASK);
698 if (!nla)
699 goto nla_put_failure;
700
Pravin B Shelare6445712013-10-03 18:16:47 -0700701 err = ovs_nla_put_flow(&flow->key, &flow->mask->key, skb);
Andy Zhou03f0d912013-08-07 20:01:00 -0700702 if (err)
703 goto error;
704
705 nla_nest_end(skb, nla);
706
Pravin B Shelare298e502013-10-29 17:22:21 -0700707 ovs_flow_stats_get(flow, &stats, &used, &tcp_flags);
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700708
David S. Miller028d6a62012-03-29 23:20:48 -0400709 if (used &&
710 nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
711 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -0700712
David S. Miller028d6a62012-03-29 23:20:48 -0400713 if (stats.n_packets &&
Pravin B Shelare298e502013-10-29 17:22:21 -0700714 nla_put(skb, OVS_FLOW_ATTR_STATS, sizeof(struct ovs_flow_stats), &stats))
David S. Miller028d6a62012-03-29 23:20:48 -0400715 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -0700716
Pravin B Shelare298e502013-10-29 17:22:21 -0700717 if ((u8)ntohs(tcp_flags) &&
718 nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, (u8)ntohs(tcp_flags)))
David S. Miller028d6a62012-03-29 23:20:48 -0400719 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -0700720
721 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
722 * this is the first flow to be dumped into 'skb'. This is unusual for
723 * Netlink but individual action lists can be longer than
724 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
725 * The userspace caller can always fetch the actions separately if it
726 * really wants them. (Most userspace callers in fact don't care.)
727 *
728 * This can only fail for dump operations because the skb is always
729 * properly sized for single flows.
730 */
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700731 start = nla_nest_start(skb, OVS_FLOW_ATTR_ACTIONS);
732 if (start) {
Pravin B Shelard57170b2013-07-30 15:39:39 -0700733 const struct sw_flow_actions *sf_acts;
734
Jesse Gross663efa32013-12-03 10:58:53 -0800735 sf_acts = rcu_dereference_ovsl(flow->sf_acts);
Pravin B Shelare6445712013-10-03 18:16:47 -0700736 err = ovs_nla_put_actions(sf_acts->actions,
737 sf_acts->actions_len, skb);
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700738
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700739 if (!err)
740 nla_nest_end(skb, start);
741 else {
742 if (skb_orig_len)
743 goto error;
744
745 nla_nest_cancel(skb, start);
746 }
747 } else if (skb_orig_len)
748 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -0700749
750 return genlmsg_end(skb, ovs_header);
751
752nla_put_failure:
753 err = -EMSGSIZE;
754error:
755 genlmsg_cancel(skb, ovs_header);
756 return err;
757}
758
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700759/* May not be called with RCU read lock. */
760static struct sk_buff *ovs_flow_cmd_alloc_info(const struct sw_flow_actions *acts,
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700761 struct genl_info *info,
762 bool always)
Jesse Grossccb13522011-10-25 19:26:31 -0700763{
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700764 struct sk_buff *skb;
Jesse Grossccb13522011-10-25 19:26:31 -0700765
Samuel Gauthier9b67aa42014-09-18 10:31:04 +0200766 if (!always && !ovs_must_notify(&dp_flow_genl_family, info, 0))
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700767 return NULL;
768
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700769 skb = genlmsg_new_unicast(ovs_flow_cmd_msg_size(acts), info, GFP_KERNEL);
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700770 if (!skb)
771 return ERR_PTR(-ENOMEM);
772
773 return skb;
Jesse Grossccb13522011-10-25 19:26:31 -0700774}
775
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700776/* Called with ovs_mutex. */
777static struct sk_buff *ovs_flow_cmd_build_info(const struct sw_flow *flow,
778 int dp_ifindex,
779 struct genl_info *info, u8 cmd,
780 bool always)
Jesse Grossccb13522011-10-25 19:26:31 -0700781{
782 struct sk_buff *skb;
783 int retval;
784
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700785 skb = ovs_flow_cmd_alloc_info(ovsl_dereference(flow->sf_acts), info,
786 always);
Himangi Saraogid0e992a2014-07-27 12:37:46 +0530787 if (IS_ERR_OR_NULL(skb))
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700788 return skb;
Jesse Grossccb13522011-10-25 19:26:31 -0700789
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700790 retval = ovs_flow_cmd_fill_info(flow, dp_ifindex, skb,
791 info->snd_portid, info->snd_seq, 0,
792 cmd);
Jesse Grossccb13522011-10-25 19:26:31 -0700793 BUG_ON(retval < 0);
794 return skb;
795}
796
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700797static int ovs_flow_cmd_new(struct sk_buff *skb, struct genl_info *info)
Jesse Grossccb13522011-10-25 19:26:31 -0700798{
799 struct nlattr **a = info->attrs;
800 struct ovs_header *ovs_header = info->userhdr;
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700801 struct sw_flow *flow, *new_flow;
Andy Zhou03f0d912013-08-07 20:01:00 -0700802 struct sw_flow_mask mask;
Jesse Grossccb13522011-10-25 19:26:31 -0700803 struct sk_buff *reply;
804 struct datapath *dp;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700805 struct sw_flow_actions *acts;
806 struct sw_flow_match match;
807 int error;
808
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700809 /* Must have key and actions. */
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700810 error = -EINVAL;
811 if (!a[OVS_FLOW_ATTR_KEY])
812 goto error;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700813 if (!a[OVS_FLOW_ATTR_ACTIONS])
814 goto error;
815
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700816 /* Most of the time we need to allocate a new flow, do it before
817 * locking.
818 */
819 new_flow = ovs_flow_alloc();
820 if (IS_ERR(new_flow)) {
821 error = PTR_ERR(new_flow);
822 goto error;
823 }
824
825 /* Extract key. */
826 ovs_match_init(&match, &new_flow->unmasked_key, &mask);
827 error = ovs_nla_get_match(&match,
828 a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
829 if (error)
830 goto err_kfree_flow;
831
832 ovs_flow_mask_key(&new_flow->key, &new_flow->unmasked_key, &mask);
833
834 /* Validate actions. */
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700835 acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_FLOW_ATTR_ACTIONS]));
836 error = PTR_ERR(acts);
837 if (IS_ERR(acts))
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700838 goto err_kfree_flow;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700839
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700840 error = ovs_nla_copy_actions(a[OVS_FLOW_ATTR_ACTIONS], &new_flow->key,
841 0, &acts);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700842 if (error) {
843 OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700844 goto err_kfree_acts;
845 }
846
847 reply = ovs_flow_cmd_alloc_info(acts, info, false);
848 if (IS_ERR(reply)) {
849 error = PTR_ERR(reply);
850 goto err_kfree_acts;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700851 }
852
853 ovs_lock();
854 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700855 if (unlikely(!dp)) {
856 error = -ENODEV;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700857 goto err_unlock_ovs;
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700858 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700859 /* Check if this is a duplicate flow */
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700860 flow = ovs_flow_tbl_lookup(&dp->table, &new_flow->unmasked_key);
861 if (likely(!flow)) {
862 rcu_assign_pointer(new_flow->sf_acts, acts);
863
864 /* Put flow in bucket. */
865 error = ovs_flow_tbl_insert(&dp->table, new_flow, &mask);
866 if (unlikely(error)) {
867 acts = NULL;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700868 goto err_unlock_ovs;
869 }
870
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700871 if (unlikely(reply)) {
872 error = ovs_flow_cmd_fill_info(new_flow,
873 ovs_header->dp_ifindex,
874 reply, info->snd_portid,
875 info->snd_seq, 0,
876 OVS_FLOW_CMD_NEW);
877 BUG_ON(error < 0);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700878 }
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700879 ovs_unlock();
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700880 } else {
881 struct sw_flow_actions *old_acts;
882
883 /* Bail out if we're not allowed to modify an existing flow.
884 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
885 * because Generic Netlink treats the latter as a dump
886 * request. We also accept NLM_F_EXCL in case that bug ever
887 * gets fixed.
888 */
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700889 if (unlikely(info->nlhdr->nlmsg_flags & (NLM_F_CREATE
890 | NLM_F_EXCL))) {
891 error = -EEXIST;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700892 goto err_unlock_ovs;
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700893 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700894 /* The unmasked key has to be the same for flow updates. */
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700895 if (unlikely(!ovs_flow_cmp_unmasked_key(flow, &match))) {
Alex Wang4a46b242014-06-30 20:30:29 -0700896 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
897 if (!flow) {
898 error = -ENOENT;
899 goto err_unlock_ovs;
900 }
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700901 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700902 /* Update actions. */
903 old_acts = ovsl_dereference(flow->sf_acts);
904 rcu_assign_pointer(flow->sf_acts, acts);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700905
906 if (unlikely(reply)) {
907 error = ovs_flow_cmd_fill_info(flow,
908 ovs_header->dp_ifindex,
909 reply, info->snd_portid,
910 info->snd_seq, 0,
911 OVS_FLOW_CMD_NEW);
912 BUG_ON(error < 0);
913 }
914 ovs_unlock();
915
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700916 ovs_nla_free_flow_actions(old_acts);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700917 ovs_flow_free(new_flow, false);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700918 }
919
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700920 if (reply)
921 ovs_notify(&dp_flow_genl_family, reply, info);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700922 return 0;
923
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700924err_unlock_ovs:
925 ovs_unlock();
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700926 kfree_skb(reply);
927err_kfree_acts:
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700928 kfree(acts);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700929err_kfree_flow:
930 ovs_flow_free(new_flow, false);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700931error:
932 return error;
933}
934
935static int ovs_flow_cmd_set(struct sk_buff *skb, struct genl_info *info)
936{
937 struct nlattr **a = info->attrs;
938 struct ovs_header *ovs_header = info->userhdr;
939 struct sw_flow_key key, masked_key;
940 struct sw_flow *flow;
941 struct sw_flow_mask mask;
942 struct sk_buff *reply = NULL;
943 struct datapath *dp;
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700944 struct sw_flow_actions *old_acts = NULL, *acts = NULL;
Andy Zhou03f0d912013-08-07 20:01:00 -0700945 struct sw_flow_match match;
Jesse Grossccb13522011-10-25 19:26:31 -0700946 int error;
Jesse Grossccb13522011-10-25 19:26:31 -0700947
948 /* Extract key. */
949 error = -EINVAL;
950 if (!a[OVS_FLOW_ATTR_KEY])
951 goto error;
Andy Zhou03f0d912013-08-07 20:01:00 -0700952
953 ovs_match_init(&match, &key, &mask);
Jarno Rajahalme23dabf82014-03-27 12:35:23 -0700954 error = ovs_nla_get_match(&match,
Pravin B Shelare6445712013-10-03 18:16:47 -0700955 a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
Jesse Grossccb13522011-10-25 19:26:31 -0700956 if (error)
957 goto error;
958
959 /* Validate actions. */
960 if (a[OVS_FLOW_ATTR_ACTIONS]) {
Pravin B Shelare6445712013-10-03 18:16:47 -0700961 acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_FLOW_ATTR_ACTIONS]));
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700962 error = PTR_ERR(acts);
963 if (IS_ERR(acts))
Jesse Grossccb13522011-10-25 19:26:31 -0700964 goto error;
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700965
Pravin B Shelare6445712013-10-03 18:16:47 -0700966 ovs_flow_mask_key(&masked_key, &key, &mask);
967 error = ovs_nla_copy_actions(a[OVS_FLOW_ATTR_ACTIONS],
968 &masked_key, 0, &acts);
Andy Zhou03f0d912013-08-07 20:01:00 -0700969 if (error) {
970 OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700971 goto err_kfree_acts;
972 }
973 }
974
975 /* Can allocate before locking if have acts. */
976 if (acts) {
977 reply = ovs_flow_cmd_alloc_info(acts, info, false);
978 if (IS_ERR(reply)) {
979 error = PTR_ERR(reply);
980 goto err_kfree_acts;
Andy Zhou03f0d912013-08-07 20:01:00 -0700981 }
Jesse Grossccb13522011-10-25 19:26:31 -0700982 }
983
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700984 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800985 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700986 if (unlikely(!dp)) {
987 error = -ENODEV;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700988 goto err_unlock_ovs;
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700989 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700990 /* Check that the flow exists. */
Alex Wang4a46b242014-06-30 20:30:29 -0700991 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700992 if (unlikely(!flow)) {
993 error = -ENOENT;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700994 goto err_unlock_ovs;
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700995 }
Alex Wang4a46b242014-06-30 20:30:29 -0700996
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700997 /* Update actions, if present. */
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700998 if (likely(acts)) {
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700999 old_acts = ovsl_dereference(flow->sf_acts);
Jesse Grossccb13522011-10-25 19:26:31 -07001000 rcu_assign_pointer(flow->sf_acts, acts);
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001001
1002 if (unlikely(reply)) {
1003 error = ovs_flow_cmd_fill_info(flow,
1004 ovs_header->dp_ifindex,
1005 reply, info->snd_portid,
1006 info->snd_seq, 0,
1007 OVS_FLOW_CMD_NEW);
1008 BUG_ON(error < 0);
1009 }
1010 } else {
1011 /* Could not alloc without acts before locking. */
1012 reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex,
1013 info, OVS_FLOW_CMD_NEW, false);
1014 if (unlikely(IS_ERR(reply))) {
1015 error = PTR_ERR(reply);
1016 goto err_unlock_ovs;
1017 }
Jesse Grossccb13522011-10-25 19:26:31 -07001018 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001019
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001020 /* Clear stats. */
1021 if (a[OVS_FLOW_ATTR_CLEAR])
1022 ovs_flow_stats_clear(flow);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001023 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001024
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001025 if (reply)
1026 ovs_notify(&dp_flow_genl_family, reply, info);
1027 if (old_acts)
1028 ovs_nla_free_flow_actions(old_acts);
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -07001029
Jesse Grossccb13522011-10-25 19:26:31 -07001030 return 0;
1031
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001032err_unlock_ovs:
1033 ovs_unlock();
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001034 kfree_skb(reply);
1035err_kfree_acts:
Pravin B Shelar74f84a52013-06-17 17:50:12 -07001036 kfree(acts);
Jesse Grossccb13522011-10-25 19:26:31 -07001037error:
1038 return error;
1039}
1040
1041static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1042{
1043 struct nlattr **a = info->attrs;
1044 struct ovs_header *ovs_header = info->userhdr;
1045 struct sw_flow_key key;
1046 struct sk_buff *reply;
1047 struct sw_flow *flow;
1048 struct datapath *dp;
Andy Zhou03f0d912013-08-07 20:01:00 -07001049 struct sw_flow_match match;
Jesse Grossccb13522011-10-25 19:26:31 -07001050 int err;
Jesse Grossccb13522011-10-25 19:26:31 -07001051
Andy Zhou03f0d912013-08-07 20:01:00 -07001052 if (!a[OVS_FLOW_ATTR_KEY]) {
1053 OVS_NLERR("Flow get message rejected, Key attribute missing.\n");
Jesse Grossccb13522011-10-25 19:26:31 -07001054 return -EINVAL;
Andy Zhou03f0d912013-08-07 20:01:00 -07001055 }
1056
1057 ovs_match_init(&match, &key, NULL);
Jarno Rajahalme23dabf82014-03-27 12:35:23 -07001058 err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL);
Jesse Grossccb13522011-10-25 19:26:31 -07001059 if (err)
1060 return err;
1061
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001062 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001063 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001064 if (!dp) {
1065 err = -ENODEV;
1066 goto unlock;
1067 }
Jesse Grossccb13522011-10-25 19:26:31 -07001068
Alex Wang4a46b242014-06-30 20:30:29 -07001069 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1070 if (!flow) {
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001071 err = -ENOENT;
1072 goto unlock;
1073 }
Jesse Grossccb13522011-10-25 19:26:31 -07001074
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -07001075 reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex, info,
1076 OVS_FLOW_CMD_NEW, true);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001077 if (IS_ERR(reply)) {
1078 err = PTR_ERR(reply);
1079 goto unlock;
1080 }
Jesse Grossccb13522011-10-25 19:26:31 -07001081
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001082 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001083 return genlmsg_reply(reply, info);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001084unlock:
1085 ovs_unlock();
1086 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001087}
1088
1089static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1090{
1091 struct nlattr **a = info->attrs;
1092 struct ovs_header *ovs_header = info->userhdr;
1093 struct sw_flow_key key;
1094 struct sk_buff *reply;
1095 struct sw_flow *flow;
1096 struct datapath *dp;
Andy Zhou03f0d912013-08-07 20:01:00 -07001097 struct sw_flow_match match;
Jesse Grossccb13522011-10-25 19:26:31 -07001098 int err;
Jesse Grossccb13522011-10-25 19:26:31 -07001099
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001100 if (likely(a[OVS_FLOW_ATTR_KEY])) {
1101 ovs_match_init(&match, &key, NULL);
1102 err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL);
1103 if (unlikely(err))
1104 return err;
1105 }
1106
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001107 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001108 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001109 if (unlikely(!dp)) {
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001110 err = -ENODEV;
1111 goto unlock;
1112 }
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001113
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001114 if (unlikely(!a[OVS_FLOW_ATTR_KEY])) {
Pravin B Shelarb637e492013-10-04 00:14:23 -07001115 err = ovs_flow_tbl_flush(&dp->table);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001116 goto unlock;
1117 }
Andy Zhou03f0d912013-08-07 20:01:00 -07001118
Alex Wang4a46b242014-06-30 20:30:29 -07001119 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1120 if (unlikely(!flow)) {
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001121 err = -ENOENT;
1122 goto unlock;
1123 }
Jesse Grossccb13522011-10-25 19:26:31 -07001124
Pravin B Shelarb637e492013-10-04 00:14:23 -07001125 ovs_flow_tbl_remove(&dp->table, flow);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001126 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001127
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001128 reply = ovs_flow_cmd_alloc_info((const struct sw_flow_actions __force *) flow->sf_acts,
1129 info, false);
1130 if (likely(reply)) {
1131 if (likely(!IS_ERR(reply))) {
1132 rcu_read_lock(); /*To keep RCU checker happy. */
1133 err = ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex,
1134 reply, info->snd_portid,
1135 info->snd_seq, 0,
1136 OVS_FLOW_CMD_DEL);
1137 rcu_read_unlock();
1138 BUG_ON(err < 0);
1139
1140 ovs_notify(&dp_flow_genl_family, reply, info);
1141 } else {
1142 netlink_set_err(sock_net(skb->sk)->genl_sock, 0, 0, PTR_ERR(reply));
1143 }
1144 }
1145
1146 ovs_flow_free(flow, true);
Jesse Grossccb13522011-10-25 19:26:31 -07001147 return 0;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001148unlock:
1149 ovs_unlock();
1150 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001151}
1152
1153static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1154{
1155 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
Pravin B Shelarb637e492013-10-04 00:14:23 -07001156 struct table_instance *ti;
Jesse Grossccb13522011-10-25 19:26:31 -07001157 struct datapath *dp;
Jesse Grossccb13522011-10-25 19:26:31 -07001158
Pravin B Shelard57170b2013-07-30 15:39:39 -07001159 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001160 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001161 if (!dp) {
Pravin B Shelard57170b2013-07-30 15:39:39 -07001162 rcu_read_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001163 return -ENODEV;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001164 }
Jesse Grossccb13522011-10-25 19:26:31 -07001165
Pravin B Shelarb637e492013-10-04 00:14:23 -07001166 ti = rcu_dereference(dp->table.ti);
Jesse Grossccb13522011-10-25 19:26:31 -07001167 for (;;) {
1168 struct sw_flow *flow;
1169 u32 bucket, obj;
1170
1171 bucket = cb->args[0];
1172 obj = cb->args[1];
Pravin B Shelarb637e492013-10-04 00:14:23 -07001173 flow = ovs_flow_tbl_dump_next(ti, &bucket, &obj);
Jesse Grossccb13522011-10-25 19:26:31 -07001174 if (!flow)
1175 break;
1176
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -07001177 if (ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex, skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001178 NETLINK_CB(cb->skb).portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001179 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1180 OVS_FLOW_CMD_NEW) < 0)
1181 break;
1182
1183 cb->args[0] = bucket;
1184 cb->args[1] = obj;
1185 }
Pravin B Shelard57170b2013-07-30 15:39:39 -07001186 rcu_read_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001187 return skb->len;
1188}
1189
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001190static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
1191 [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
1192 [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
1193 [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
1194};
1195
stephen hemminger48e48a72014-07-16 11:25:52 -07001196static const struct genl_ops dp_flow_genl_ops[] = {
Jesse Grossccb13522011-10-25 19:26:31 -07001197 { .cmd = OVS_FLOW_CMD_NEW,
1198 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1199 .policy = flow_policy,
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001200 .doit = ovs_flow_cmd_new
Jesse Grossccb13522011-10-25 19:26:31 -07001201 },
1202 { .cmd = OVS_FLOW_CMD_DEL,
1203 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1204 .policy = flow_policy,
1205 .doit = ovs_flow_cmd_del
1206 },
1207 { .cmd = OVS_FLOW_CMD_GET,
1208 .flags = 0, /* OK for unprivileged users. */
1209 .policy = flow_policy,
1210 .doit = ovs_flow_cmd_get,
1211 .dumpit = ovs_flow_cmd_dump
1212 },
1213 { .cmd = OVS_FLOW_CMD_SET,
1214 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1215 .policy = flow_policy,
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001216 .doit = ovs_flow_cmd_set,
Jesse Grossccb13522011-10-25 19:26:31 -07001217 },
1218};
1219
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001220static struct genl_family dp_flow_genl_family = {
Jesse Grossccb13522011-10-25 19:26:31 -07001221 .id = GENL_ID_GENERATE,
1222 .hdrsize = sizeof(struct ovs_header),
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001223 .name = OVS_FLOW_FAMILY,
1224 .version = OVS_FLOW_VERSION,
1225 .maxattr = OVS_FLOW_ATTR_MAX,
Pravin B Shelar3a4e0d62013-04-23 07:48:48 +00001226 .netnsok = true,
1227 .parallel_ops = true,
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001228 .ops = dp_flow_genl_ops,
1229 .n_ops = ARRAY_SIZE(dp_flow_genl_ops),
1230 .mcgrps = &ovs_dp_flow_multicast_group,
1231 .n_mcgrps = 1,
Jesse Grossccb13522011-10-25 19:26:31 -07001232};
1233
Thomas Grafc3ff8cf2013-03-29 14:46:49 +01001234static size_t ovs_dp_cmd_msg_size(void)
1235{
1236 size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
1237
1238 msgsize += nla_total_size(IFNAMSIZ);
1239 msgsize += nla_total_size(sizeof(struct ovs_dp_stats));
Andy Zhou1bd71162013-10-22 10:42:46 -07001240 msgsize += nla_total_size(sizeof(struct ovs_dp_megaflow_stats));
Daniele Di Proietto45fb9c32014-01-23 10:47:35 -08001241 msgsize += nla_total_size(sizeof(u32)); /* OVS_DP_ATTR_USER_FEATURES */
Thomas Grafc3ff8cf2013-03-29 14:46:49 +01001242
1243 return msgsize;
1244}
1245
Jarno Rajahalmebb6f9a72014-05-05 11:32:17 -07001246/* Called with ovs_mutex or RCU read lock. */
Jesse Grossccb13522011-10-25 19:26:31 -07001247static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001248 u32 portid, u32 seq, u32 flags, u8 cmd)
Jesse Grossccb13522011-10-25 19:26:31 -07001249{
1250 struct ovs_header *ovs_header;
1251 struct ovs_dp_stats dp_stats;
Andy Zhou1bd71162013-10-22 10:42:46 -07001252 struct ovs_dp_megaflow_stats dp_megaflow_stats;
Jesse Grossccb13522011-10-25 19:26:31 -07001253 int err;
1254
Eric W. Biederman15e47302012-09-07 20:12:54 +00001255 ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
Jesse Grossccb13522011-10-25 19:26:31 -07001256 flags, cmd);
1257 if (!ovs_header)
1258 goto error;
1259
1260 ovs_header->dp_ifindex = get_dpifindex(dp);
1261
Jesse Grossccb13522011-10-25 19:26:31 -07001262 err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
Jesse Grossccb13522011-10-25 19:26:31 -07001263 if (err)
1264 goto nla_put_failure;
1265
Andy Zhou1bd71162013-10-22 10:42:46 -07001266 get_dp_stats(dp, &dp_stats, &dp_megaflow_stats);
1267 if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats),
1268 &dp_stats))
1269 goto nla_put_failure;
1270
1271 if (nla_put(skb, OVS_DP_ATTR_MEGAFLOW_STATS,
1272 sizeof(struct ovs_dp_megaflow_stats),
1273 &dp_megaflow_stats))
David S. Miller028d6a62012-03-29 23:20:48 -04001274 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001275
Thomas Graf43d4be92013-12-13 15:22:18 +01001276 if (nla_put_u32(skb, OVS_DP_ATTR_USER_FEATURES, dp->user_features))
1277 goto nla_put_failure;
1278
Jesse Grossccb13522011-10-25 19:26:31 -07001279 return genlmsg_end(skb, ovs_header);
1280
1281nla_put_failure:
1282 genlmsg_cancel(skb, ovs_header);
1283error:
1284 return -EMSGSIZE;
1285}
1286
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001287static struct sk_buff *ovs_dp_cmd_alloc_info(struct genl_info *info)
Jesse Grossccb13522011-10-25 19:26:31 -07001288{
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001289 return genlmsg_new_unicast(ovs_dp_cmd_msg_size(), info, GFP_KERNEL);
Jesse Grossccb13522011-10-25 19:26:31 -07001290}
1291
Jarno Rajahalmebb6f9a72014-05-05 11:32:17 -07001292/* Called with rcu_read_lock or ovs_mutex. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001293static struct datapath *lookup_datapath(struct net *net,
1294 struct ovs_header *ovs_header,
Jesse Grossccb13522011-10-25 19:26:31 -07001295 struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1296{
1297 struct datapath *dp;
1298
1299 if (!a[OVS_DP_ATTR_NAME])
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001300 dp = get_dp(net, ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001301 else {
1302 struct vport *vport;
1303
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001304 vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
Jesse Grossccb13522011-10-25 19:26:31 -07001305 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
Jesse Grossccb13522011-10-25 19:26:31 -07001306 }
1307 return dp ? dp : ERR_PTR(-ENODEV);
1308}
1309
Thomas Graf44da5ae2013-12-13 15:22:19 +01001310static void ovs_dp_reset_user_features(struct sk_buff *skb, struct genl_info *info)
1311{
1312 struct datapath *dp;
1313
1314 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
Jiri Pirko3c7eacf2014-02-14 11:42:36 +01001315 if (IS_ERR(dp))
Thomas Graf44da5ae2013-12-13 15:22:19 +01001316 return;
1317
1318 WARN(dp->user_features, "Dropping previously announced user features\n");
1319 dp->user_features = 0;
1320}
1321
Thomas Graf43d4be92013-12-13 15:22:18 +01001322static void ovs_dp_change(struct datapath *dp, struct nlattr **a)
1323{
1324 if (a[OVS_DP_ATTR_USER_FEATURES])
1325 dp->user_features = nla_get_u32(a[OVS_DP_ATTR_USER_FEATURES]);
1326}
1327
Jesse Grossccb13522011-10-25 19:26:31 -07001328static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1329{
1330 struct nlattr **a = info->attrs;
1331 struct vport_parms parms;
1332 struct sk_buff *reply;
1333 struct datapath *dp;
1334 struct vport *vport;
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001335 struct ovs_net *ovs_net;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001336 int err, i;
Jesse Grossccb13522011-10-25 19:26:31 -07001337
1338 err = -EINVAL;
1339 if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1340 goto err;
1341
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001342 reply = ovs_dp_cmd_alloc_info(info);
1343 if (!reply)
1344 return -ENOMEM;
Jesse Grossccb13522011-10-25 19:26:31 -07001345
1346 err = -ENOMEM;
1347 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1348 if (dp == NULL)
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001349 goto err_free_reply;
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001350
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001351 ovs_dp_set_net(dp, hold_net(sock_net(skb->sk)));
Jesse Grossccb13522011-10-25 19:26:31 -07001352
1353 /* Allocate table. */
Pravin B Shelarb637e492013-10-04 00:14:23 -07001354 err = ovs_flow_tbl_init(&dp->table);
1355 if (err)
Jesse Grossccb13522011-10-25 19:26:31 -07001356 goto err_free_dp;
1357
WANG Cong1c213bd2014-02-13 11:46:28 -08001358 dp->stats_percpu = netdev_alloc_pcpu_stats(struct dp_stats_percpu);
Jesse Grossccb13522011-10-25 19:26:31 -07001359 if (!dp->stats_percpu) {
1360 err = -ENOMEM;
1361 goto err_destroy_table;
1362 }
1363
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001364 dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
Pravin B Shelare6445712013-10-03 18:16:47 -07001365 GFP_KERNEL);
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001366 if (!dp->ports) {
1367 err = -ENOMEM;
1368 goto err_destroy_percpu;
1369 }
1370
1371 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1372 INIT_HLIST_HEAD(&dp->ports[i]);
1373
Jesse Grossccb13522011-10-25 19:26:31 -07001374 /* Set up our datapath device. */
1375 parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1376 parms.type = OVS_VPORT_TYPE_INTERNAL;
1377 parms.options = NULL;
1378 parms.dp = dp;
1379 parms.port_no = OVSP_LOCAL;
Alex Wang5cd667b2014-07-17 15:14:13 -07001380 parms.upcall_portids = a[OVS_DP_ATTR_UPCALL_PID];
Jesse Grossccb13522011-10-25 19:26:31 -07001381
Thomas Graf43d4be92013-12-13 15:22:18 +01001382 ovs_dp_change(dp, a);
1383
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001384 /* So far only local changes have been made, now need the lock. */
1385 ovs_lock();
1386
Jesse Grossccb13522011-10-25 19:26:31 -07001387 vport = new_vport(&parms);
1388 if (IS_ERR(vport)) {
1389 err = PTR_ERR(vport);
1390 if (err == -EBUSY)
1391 err = -EEXIST;
1392
Thomas Graf44da5ae2013-12-13 15:22:19 +01001393 if (err == -EEXIST) {
1394 /* An outdated user space instance that does not understand
1395 * the concept of user_features has attempted to create a new
1396 * datapath and is likely to reuse it. Drop all user features.
1397 */
1398 if (info->genlhdr->version < OVS_DP_VER_FEATURES)
1399 ovs_dp_reset_user_features(skb, info);
1400 }
1401
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001402 goto err_destroy_ports_array;
Jesse Grossccb13522011-10-25 19:26:31 -07001403 }
1404
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001405 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1406 info->snd_seq, 0, OVS_DP_CMD_NEW);
1407 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001408
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001409 ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001410 list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001411
1412 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001413
Johannes Berg2a94fe42013-11-19 15:19:39 +01001414 ovs_notify(&dp_datapath_genl_family, reply, info);
Jesse Grossccb13522011-10-25 19:26:31 -07001415 return 0;
1416
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001417err_destroy_ports_array:
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001418 ovs_unlock();
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001419 kfree(dp->ports);
Jesse Grossccb13522011-10-25 19:26:31 -07001420err_destroy_percpu:
1421 free_percpu(dp->stats_percpu);
1422err_destroy_table:
Andy Zhoue80857c2014-01-21 09:31:04 -08001423 ovs_flow_tbl_destroy(&dp->table, false);
Jesse Grossccb13522011-10-25 19:26:31 -07001424err_free_dp:
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001425 release_net(ovs_dp_get_net(dp));
Jesse Grossccb13522011-10-25 19:26:31 -07001426 kfree(dp);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001427err_free_reply:
1428 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001429err:
1430 return err;
1431}
1432
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001433/* Called with ovs_mutex. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001434static void __dp_destroy(struct datapath *dp)
Jesse Grossccb13522011-10-25 19:26:31 -07001435{
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001436 int i;
Jesse Grossccb13522011-10-25 19:26:31 -07001437
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001438 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1439 struct vport *vport;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001440 struct hlist_node *n;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001441
Sasha Levinb67bfe02013-02-27 17:06:00 -08001442 hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001443 if (vport->port_no != OVSP_LOCAL)
1444 ovs_dp_detach_port(vport);
1445 }
Jesse Grossccb13522011-10-25 19:26:31 -07001446
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001447 list_del_rcu(&dp->list_node);
Jesse Grossccb13522011-10-25 19:26:31 -07001448
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001449 /* OVSP_LOCAL is datapath internal port. We need to make sure that
Andy Zhoue80857c2014-01-21 09:31:04 -08001450 * all ports in datapath are destroyed first before freeing datapath.
Jesse Grossccb13522011-10-25 19:26:31 -07001451 */
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001452 ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
Jesse Grossccb13522011-10-25 19:26:31 -07001453
Andy Zhoue80857c2014-01-21 09:31:04 -08001454 /* RCU destroy the flow table */
1455 ovs_flow_tbl_destroy(&dp->table, true);
1456
Jesse Grossccb13522011-10-25 19:26:31 -07001457 call_rcu(&dp->rcu, destroy_dp_rcu);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001458}
1459
1460static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1461{
1462 struct sk_buff *reply;
1463 struct datapath *dp;
1464 int err;
1465
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001466 reply = ovs_dp_cmd_alloc_info(info);
1467 if (!reply)
1468 return -ENOMEM;
1469
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001470 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001471 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1472 err = PTR_ERR(dp);
1473 if (IS_ERR(dp))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001474 goto err_unlock_free;
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001475
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001476 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1477 info->snd_seq, 0, OVS_DP_CMD_DEL);
1478 BUG_ON(err < 0);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001479
1480 __dp_destroy(dp);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001481 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001482
Johannes Berg2a94fe42013-11-19 15:19:39 +01001483 ovs_notify(&dp_datapath_genl_family, reply, info);
Jesse Grossccb13522011-10-25 19:26:31 -07001484
1485 return 0;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001486
1487err_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001488 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001489 kfree_skb(reply);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001490 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001491}
1492
1493static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1494{
1495 struct sk_buff *reply;
1496 struct datapath *dp;
1497 int err;
1498
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001499 reply = ovs_dp_cmd_alloc_info(info);
1500 if (!reply)
1501 return -ENOMEM;
1502
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001503 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001504 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001505 err = PTR_ERR(dp);
Jesse Grossccb13522011-10-25 19:26:31 -07001506 if (IS_ERR(dp))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001507 goto err_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001508
Thomas Graf43d4be92013-12-13 15:22:18 +01001509 ovs_dp_change(dp, info->attrs);
1510
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001511 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1512 info->snd_seq, 0, OVS_DP_CMD_NEW);
1513 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001514
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001515 ovs_unlock();
Johannes Berg2a94fe42013-11-19 15:19:39 +01001516 ovs_notify(&dp_datapath_genl_family, reply, info);
Jesse Grossccb13522011-10-25 19:26:31 -07001517
1518 return 0;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001519
1520err_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001521 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001522 kfree_skb(reply);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001523 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001524}
1525
1526static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1527{
1528 struct sk_buff *reply;
1529 struct datapath *dp;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001530 int err;
Jesse Grossccb13522011-10-25 19:26:31 -07001531
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001532 reply = ovs_dp_cmd_alloc_info(info);
1533 if (!reply)
1534 return -ENOMEM;
1535
1536 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001537 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001538 if (IS_ERR(dp)) {
1539 err = PTR_ERR(dp);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001540 goto err_unlock_free;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001541 }
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001542 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1543 info->snd_seq, 0, OVS_DP_CMD_NEW);
1544 BUG_ON(err < 0);
1545 rcu_read_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001546
Jesse Grossccb13522011-10-25 19:26:31 -07001547 return genlmsg_reply(reply, info);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001548
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001549err_unlock_free:
1550 rcu_read_unlock();
1551 kfree_skb(reply);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001552 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001553}
1554
1555static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1556{
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001557 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
Jesse Grossccb13522011-10-25 19:26:31 -07001558 struct datapath *dp;
1559 int skip = cb->args[0];
1560 int i = 0;
1561
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001562 rcu_read_lock();
1563 list_for_each_entry_rcu(dp, &ovs_net->dps, list_node) {
Ben Pfaff77676fd2012-01-17 13:33:39 +00001564 if (i >= skip &&
Eric W. Biederman15e47302012-09-07 20:12:54 +00001565 ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001566 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1567 OVS_DP_CMD_NEW) < 0)
1568 break;
1569 i++;
1570 }
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001571 rcu_read_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001572
1573 cb->args[0] = i;
1574
1575 return skb->len;
1576}
1577
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001578static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1579 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1580 [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1581 [OVS_DP_ATTR_USER_FEATURES] = { .type = NLA_U32 },
1582};
1583
stephen hemminger48e48a72014-07-16 11:25:52 -07001584static const struct genl_ops dp_datapath_genl_ops[] = {
Jesse Grossccb13522011-10-25 19:26:31 -07001585 { .cmd = OVS_DP_CMD_NEW,
1586 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1587 .policy = datapath_policy,
1588 .doit = ovs_dp_cmd_new
1589 },
1590 { .cmd = OVS_DP_CMD_DEL,
1591 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1592 .policy = datapath_policy,
1593 .doit = ovs_dp_cmd_del
1594 },
1595 { .cmd = OVS_DP_CMD_GET,
1596 .flags = 0, /* OK for unprivileged users. */
1597 .policy = datapath_policy,
1598 .doit = ovs_dp_cmd_get,
1599 .dumpit = ovs_dp_cmd_dump
1600 },
1601 { .cmd = OVS_DP_CMD_SET,
1602 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1603 .policy = datapath_policy,
1604 .doit = ovs_dp_cmd_set,
1605 },
1606};
1607
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001608static struct genl_family dp_datapath_genl_family = {
Jesse Grossccb13522011-10-25 19:26:31 -07001609 .id = GENL_ID_GENERATE,
1610 .hdrsize = sizeof(struct ovs_header),
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001611 .name = OVS_DATAPATH_FAMILY,
1612 .version = OVS_DATAPATH_VERSION,
1613 .maxattr = OVS_DP_ATTR_MAX,
Pravin B Shelar3a4e0d62013-04-23 07:48:48 +00001614 .netnsok = true,
1615 .parallel_ops = true,
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001616 .ops = dp_datapath_genl_ops,
1617 .n_ops = ARRAY_SIZE(dp_datapath_genl_ops),
1618 .mcgrps = &ovs_dp_datapath_multicast_group,
1619 .n_mcgrps = 1,
Jesse Grossccb13522011-10-25 19:26:31 -07001620};
1621
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001622/* Called with ovs_mutex or RCU read lock. */
Jesse Grossccb13522011-10-25 19:26:31 -07001623static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001624 u32 portid, u32 seq, u32 flags, u8 cmd)
Jesse Grossccb13522011-10-25 19:26:31 -07001625{
1626 struct ovs_header *ovs_header;
1627 struct ovs_vport_stats vport_stats;
1628 int err;
1629
Eric W. Biederman15e47302012-09-07 20:12:54 +00001630 ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
Jesse Grossccb13522011-10-25 19:26:31 -07001631 flags, cmd);
1632 if (!ovs_header)
1633 return -EMSGSIZE;
1634
1635 ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1636
David S. Miller028d6a62012-03-29 23:20:48 -04001637 if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
1638 nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
Alex Wang5cd667b2014-07-17 15:14:13 -07001639 nla_put_string(skb, OVS_VPORT_ATTR_NAME,
1640 vport->ops->get_name(vport)))
David S. Miller028d6a62012-03-29 23:20:48 -04001641 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001642
1643 ovs_vport_get_stats(vport, &vport_stats);
David S. Miller028d6a62012-03-29 23:20:48 -04001644 if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
1645 &vport_stats))
1646 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001647
Alex Wang5cd667b2014-07-17 15:14:13 -07001648 if (ovs_vport_get_upcall_portids(vport, skb))
1649 goto nla_put_failure;
1650
Jesse Grossccb13522011-10-25 19:26:31 -07001651 err = ovs_vport_get_options(vport, skb);
1652 if (err == -EMSGSIZE)
1653 goto error;
1654
1655 return genlmsg_end(skb, ovs_header);
1656
1657nla_put_failure:
1658 err = -EMSGSIZE;
1659error:
1660 genlmsg_cancel(skb, ovs_header);
1661 return err;
1662}
1663
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001664static struct sk_buff *ovs_vport_cmd_alloc_info(void)
1665{
1666 return nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1667}
1668
1669/* Called with ovs_mutex, only via ovs_dp_notify_wq(). */
Eric W. Biederman15e47302012-09-07 20:12:54 +00001670struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001671 u32 seq, u8 cmd)
1672{
1673 struct sk_buff *skb;
1674 int retval;
1675
1676 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1677 if (!skb)
1678 return ERR_PTR(-ENOMEM);
1679
Eric W. Biederman15e47302012-09-07 20:12:54 +00001680 retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
Jesse Grossa9341512013-03-26 15:48:38 -07001681 BUG_ON(retval < 0);
1682
Jesse Grossccb13522011-10-25 19:26:31 -07001683 return skb;
1684}
1685
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001686/* Called with ovs_mutex or RCU read lock. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001687static struct vport *lookup_vport(struct net *net,
1688 struct ovs_header *ovs_header,
Jesse Grossccb13522011-10-25 19:26:31 -07001689 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1690{
1691 struct datapath *dp;
1692 struct vport *vport;
1693
1694 if (a[OVS_VPORT_ATTR_NAME]) {
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001695 vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
Jesse Grossccb13522011-10-25 19:26:31 -07001696 if (!vport)
1697 return ERR_PTR(-ENODEV);
Ben Pfaff651a68e2012-03-06 15:04:04 -08001698 if (ovs_header->dp_ifindex &&
1699 ovs_header->dp_ifindex != get_dpifindex(vport->dp))
1700 return ERR_PTR(-ENODEV);
Jesse Grossccb13522011-10-25 19:26:31 -07001701 return vport;
1702 } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
1703 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1704
1705 if (port_no >= DP_MAX_PORTS)
1706 return ERR_PTR(-EFBIG);
1707
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001708 dp = get_dp(net, ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001709 if (!dp)
1710 return ERR_PTR(-ENODEV);
1711
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001712 vport = ovs_vport_ovsl_rcu(dp, port_no);
Jesse Grossccb13522011-10-25 19:26:31 -07001713 if (!vport)
Jarno Rajahalme14408db2013-01-09 14:27:35 -08001714 return ERR_PTR(-ENODEV);
Jesse Grossccb13522011-10-25 19:26:31 -07001715 return vport;
1716 } else
1717 return ERR_PTR(-EINVAL);
1718}
1719
1720static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
1721{
1722 struct nlattr **a = info->attrs;
1723 struct ovs_header *ovs_header = info->userhdr;
1724 struct vport_parms parms;
1725 struct sk_buff *reply;
1726 struct vport *vport;
1727 struct datapath *dp;
1728 u32 port_no;
1729 int err;
1730
Jesse Grossccb13522011-10-25 19:26:31 -07001731 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
1732 !a[OVS_VPORT_ATTR_UPCALL_PID])
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001733 return -EINVAL;
1734
1735 port_no = a[OVS_VPORT_ATTR_PORT_NO]
1736 ? nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]) : 0;
1737 if (port_no >= DP_MAX_PORTS)
1738 return -EFBIG;
1739
1740 reply = ovs_vport_cmd_alloc_info();
1741 if (!reply)
1742 return -ENOMEM;
Jesse Grossccb13522011-10-25 19:26:31 -07001743
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001744 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001745 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001746 err = -ENODEV;
1747 if (!dp)
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001748 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001749
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001750 if (port_no) {
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001751 vport = ovs_vport_ovsl(dp, port_no);
Jesse Grossccb13522011-10-25 19:26:31 -07001752 err = -EBUSY;
1753 if (vport)
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001754 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001755 } else {
1756 for (port_no = 1; ; port_no++) {
1757 if (port_no >= DP_MAX_PORTS) {
1758 err = -EFBIG;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001759 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001760 }
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001761 vport = ovs_vport_ovsl(dp, port_no);
Jesse Grossccb13522011-10-25 19:26:31 -07001762 if (!vport)
1763 break;
1764 }
1765 }
1766
1767 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
1768 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1769 parms.options = a[OVS_VPORT_ATTR_OPTIONS];
1770 parms.dp = dp;
1771 parms.port_no = port_no;
Alex Wang5cd667b2014-07-17 15:14:13 -07001772 parms.upcall_portids = a[OVS_VPORT_ATTR_UPCALL_PID];
Jesse Grossccb13522011-10-25 19:26:31 -07001773
1774 vport = new_vport(&parms);
1775 err = PTR_ERR(vport);
1776 if (IS_ERR(vport))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001777 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001778
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001779 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1780 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1781 BUG_ON(err < 0);
1782 ovs_unlock();
Thomas Grafed661182013-03-29 14:46:50 +01001783
Johannes Berg2a94fe42013-11-19 15:19:39 +01001784 ovs_notify(&dp_vport_genl_family, reply, info);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001785 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001786
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001787exit_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001788 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001789 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001790 return err;
1791}
1792
1793static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
1794{
1795 struct nlattr **a = info->attrs;
1796 struct sk_buff *reply;
1797 struct vport *vport;
1798 int err;
1799
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001800 reply = ovs_vport_cmd_alloc_info();
1801 if (!reply)
1802 return -ENOMEM;
1803
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001804 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001805 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
Jesse Grossccb13522011-10-25 19:26:31 -07001806 err = PTR_ERR(vport);
1807 if (IS_ERR(vport))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001808 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001809
Jesse Grossccb13522011-10-25 19:26:31 -07001810 if (a[OVS_VPORT_ATTR_TYPE] &&
Jesse Grossf44f3402013-05-13 08:15:26 -07001811 nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
Jesse Grossccb13522011-10-25 19:26:31 -07001812 err = -EINVAL;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001813 goto exit_unlock_free;
Jesse Grossa9341512013-03-26 15:48:38 -07001814 }
1815
Jesse Grossf44f3402013-05-13 08:15:26 -07001816 if (a[OVS_VPORT_ATTR_OPTIONS]) {
Jesse Grossccb13522011-10-25 19:26:31 -07001817 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
Jesse Grossf44f3402013-05-13 08:15:26 -07001818 if (err)
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001819 goto exit_unlock_free;
Jesse Grossf44f3402013-05-13 08:15:26 -07001820 }
Jesse Grossa9341512013-03-26 15:48:38 -07001821
Alex Wang5cd667b2014-07-17 15:14:13 -07001822
1823 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
1824 struct nlattr *ids = a[OVS_VPORT_ATTR_UPCALL_PID];
1825
1826 err = ovs_vport_set_upcall_portids(vport, ids);
1827 if (err)
1828 goto exit_unlock_free;
1829 }
Jesse Grossccb13522011-10-25 19:26:31 -07001830
Jesse Grossa9341512013-03-26 15:48:38 -07001831 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1832 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1833 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001834
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001835 ovs_unlock();
Johannes Berg2a94fe42013-11-19 15:19:39 +01001836 ovs_notify(&dp_vport_genl_family, reply, info);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001837 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001838
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001839exit_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001840 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001841 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001842 return err;
1843}
1844
1845static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
1846{
1847 struct nlattr **a = info->attrs;
1848 struct sk_buff *reply;
1849 struct vport *vport;
1850 int err;
1851
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001852 reply = ovs_vport_cmd_alloc_info();
1853 if (!reply)
1854 return -ENOMEM;
1855
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001856 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001857 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
Jesse Grossccb13522011-10-25 19:26:31 -07001858 err = PTR_ERR(vport);
1859 if (IS_ERR(vport))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001860 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001861
1862 if (vport->port_no == OVSP_LOCAL) {
1863 err = -EINVAL;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001864 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001865 }
1866
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001867 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1868 info->snd_seq, 0, OVS_VPORT_CMD_DEL);
1869 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001870 ovs_dp_detach_port(vport);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001871 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001872
Johannes Berg2a94fe42013-11-19 15:19:39 +01001873 ovs_notify(&dp_vport_genl_family, reply, info);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001874 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001875
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001876exit_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001877 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001878 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001879 return err;
1880}
1881
1882static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
1883{
1884 struct nlattr **a = info->attrs;
1885 struct ovs_header *ovs_header = info->userhdr;
1886 struct sk_buff *reply;
1887 struct vport *vport;
1888 int err;
1889
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001890 reply = ovs_vport_cmd_alloc_info();
1891 if (!reply)
1892 return -ENOMEM;
1893
Jesse Grossccb13522011-10-25 19:26:31 -07001894 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001895 vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
Jesse Grossccb13522011-10-25 19:26:31 -07001896 err = PTR_ERR(vport);
1897 if (IS_ERR(vport))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001898 goto exit_unlock_free;
1899 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1900 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1901 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001902 rcu_read_unlock();
1903
1904 return genlmsg_reply(reply, info);
1905
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001906exit_unlock_free:
Jesse Grossccb13522011-10-25 19:26:31 -07001907 rcu_read_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001908 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001909 return err;
1910}
1911
1912static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1913{
1914 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1915 struct datapath *dp;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001916 int bucket = cb->args[0], skip = cb->args[1];
1917 int i, j = 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001918
Jesse Grossccb13522011-10-25 19:26:31 -07001919 rcu_read_lock();
Jarno Rajahalme42ee19e2014-02-15 17:42:29 -08001920 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1921 if (!dp) {
1922 rcu_read_unlock();
1923 return -ENODEV;
1924 }
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001925 for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
Jesse Grossccb13522011-10-25 19:26:31 -07001926 struct vport *vport;
1927
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001928 j = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001929 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001930 if (j >= skip &&
1931 ovs_vport_cmd_fill_info(vport, skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001932 NETLINK_CB(cb->skb).portid,
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001933 cb->nlh->nlmsg_seq,
1934 NLM_F_MULTI,
1935 OVS_VPORT_CMD_NEW) < 0)
1936 goto out;
Jesse Grossccb13522011-10-25 19:26:31 -07001937
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001938 j++;
1939 }
1940 skip = 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001941 }
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001942out:
Jesse Grossccb13522011-10-25 19:26:31 -07001943 rcu_read_unlock();
1944
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001945 cb->args[0] = i;
1946 cb->args[1] = j;
Jesse Grossccb13522011-10-25 19:26:31 -07001947
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001948 return skb->len;
Jesse Grossccb13522011-10-25 19:26:31 -07001949}
1950
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001951static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
1952 [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1953 [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
1954 [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
1955 [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
1956 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1957 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
1958};
1959
stephen hemminger48e48a72014-07-16 11:25:52 -07001960static const struct genl_ops dp_vport_genl_ops[] = {
Jesse Grossccb13522011-10-25 19:26:31 -07001961 { .cmd = OVS_VPORT_CMD_NEW,
1962 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1963 .policy = vport_policy,
1964 .doit = ovs_vport_cmd_new
1965 },
1966 { .cmd = OVS_VPORT_CMD_DEL,
1967 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1968 .policy = vport_policy,
1969 .doit = ovs_vport_cmd_del
1970 },
1971 { .cmd = OVS_VPORT_CMD_GET,
1972 .flags = 0, /* OK for unprivileged users. */
1973 .policy = vport_policy,
1974 .doit = ovs_vport_cmd_get,
1975 .dumpit = ovs_vport_cmd_dump
1976 },
1977 { .cmd = OVS_VPORT_CMD_SET,
1978 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1979 .policy = vport_policy,
1980 .doit = ovs_vport_cmd_set,
1981 },
1982};
1983
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001984struct genl_family dp_vport_genl_family = {
1985 .id = GENL_ID_GENERATE,
1986 .hdrsize = sizeof(struct ovs_header),
1987 .name = OVS_VPORT_FAMILY,
1988 .version = OVS_VPORT_VERSION,
1989 .maxattr = OVS_VPORT_ATTR_MAX,
1990 .netnsok = true,
1991 .parallel_ops = true,
1992 .ops = dp_vport_genl_ops,
1993 .n_ops = ARRAY_SIZE(dp_vport_genl_ops),
1994 .mcgrps = &ovs_dp_vport_multicast_group,
1995 .n_mcgrps = 1,
Jesse Grossccb13522011-10-25 19:26:31 -07001996};
1997
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001998static struct genl_family * const dp_genl_families[] = {
1999 &dp_datapath_genl_family,
2000 &dp_vport_genl_family,
2001 &dp_flow_genl_family,
2002 &dp_packet_genl_family,
Jesse Grossccb13522011-10-25 19:26:31 -07002003};
2004
2005static void dp_unregister_genl(int n_families)
2006{
2007 int i;
2008
2009 for (i = 0; i < n_families; i++)
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002010 genl_unregister_family(dp_genl_families[i]);
Jesse Grossccb13522011-10-25 19:26:31 -07002011}
2012
2013static int dp_register_genl(void)
2014{
Jesse Grossccb13522011-10-25 19:26:31 -07002015 int err;
2016 int i;
2017
Jesse Grossccb13522011-10-25 19:26:31 -07002018 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
Jesse Grossccb13522011-10-25 19:26:31 -07002019
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002020 err = genl_register_family(dp_genl_families[i]);
Jesse Grossccb13522011-10-25 19:26:31 -07002021 if (err)
2022 goto error;
Jesse Grossccb13522011-10-25 19:26:31 -07002023 }
2024
2025 return 0;
2026
2027error:
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002028 dp_unregister_genl(i);
Jesse Grossccb13522011-10-25 19:26:31 -07002029 return err;
2030}
2031
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002032static int __net_init ovs_init_net(struct net *net)
2033{
2034 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2035
2036 INIT_LIST_HEAD(&ovs_net->dps);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002037 INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002038 return 0;
2039}
2040
2041static void __net_exit ovs_exit_net(struct net *net)
2042{
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002043 struct datapath *dp, *dp_next;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002044 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002045
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002046 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002047 list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
2048 __dp_destroy(dp);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002049 ovs_unlock();
2050
2051 cancel_work_sync(&ovs_net->dp_notify_work);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002052}
2053
2054static struct pernet_operations ovs_net_ops = {
2055 .init = ovs_init_net,
2056 .exit = ovs_exit_net,
2057 .id = &ovs_net_id,
2058 .size = sizeof(struct ovs_net),
2059};
2060
Jesse Grossccb13522011-10-25 19:26:31 -07002061static int __init dp_init(void)
2062{
Jesse Grossccb13522011-10-25 19:26:31 -07002063 int err;
2064
YOSHIFUJI Hideaki / 吉藤英明3523b292013-01-09 07:19:55 +00002065 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
Jesse Grossccb13522011-10-25 19:26:31 -07002066
2067 pr_info("Open vSwitch switching datapath\n");
2068
Andy Zhou971427f32014-09-15 19:37:25 -07002069 err = action_fifos_init();
Jesse Grossccb13522011-10-25 19:26:31 -07002070 if (err)
2071 goto error;
2072
Andy Zhou971427f32014-09-15 19:37:25 -07002073 err = ovs_internal_dev_rtnl_link_register();
2074 if (err)
2075 goto error_action_fifos_exit;
2076
Jiri Pirko5b9e7e12014-06-26 09:58:26 +02002077 err = ovs_flow_init();
2078 if (err)
2079 goto error_unreg_rtnl_link;
2080
Jesse Grossccb13522011-10-25 19:26:31 -07002081 err = ovs_vport_init();
2082 if (err)
2083 goto error_flow_exit;
2084
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002085 err = register_pernet_device(&ovs_net_ops);
Jesse Grossccb13522011-10-25 19:26:31 -07002086 if (err)
2087 goto error_vport_exit;
2088
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002089 err = register_netdevice_notifier(&ovs_dp_device_notifier);
2090 if (err)
2091 goto error_netns_exit;
2092
Jesse Grossccb13522011-10-25 19:26:31 -07002093 err = dp_register_genl();
2094 if (err < 0)
2095 goto error_unreg_notifier;
2096
Jesse Grossccb13522011-10-25 19:26:31 -07002097 return 0;
2098
2099error_unreg_notifier:
2100 unregister_netdevice_notifier(&ovs_dp_device_notifier);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002101error_netns_exit:
2102 unregister_pernet_device(&ovs_net_ops);
Jesse Grossccb13522011-10-25 19:26:31 -07002103error_vport_exit:
2104 ovs_vport_exit();
2105error_flow_exit:
2106 ovs_flow_exit();
Jiri Pirko5b9e7e12014-06-26 09:58:26 +02002107error_unreg_rtnl_link:
2108 ovs_internal_dev_rtnl_link_unregister();
Andy Zhou971427f32014-09-15 19:37:25 -07002109error_action_fifos_exit:
2110 action_fifos_exit();
Jesse Grossccb13522011-10-25 19:26:31 -07002111error:
2112 return err;
2113}
2114
2115static void dp_cleanup(void)
2116{
Jesse Grossccb13522011-10-25 19:26:31 -07002117 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
2118 unregister_netdevice_notifier(&ovs_dp_device_notifier);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002119 unregister_pernet_device(&ovs_net_ops);
2120 rcu_barrier();
Jesse Grossccb13522011-10-25 19:26:31 -07002121 ovs_vport_exit();
2122 ovs_flow_exit();
Jiri Pirko5b9e7e12014-06-26 09:58:26 +02002123 ovs_internal_dev_rtnl_link_unregister();
Andy Zhou971427f32014-09-15 19:37:25 -07002124 action_fifos_exit();
Jesse Grossccb13522011-10-25 19:26:31 -07002125}
2126
2127module_init(dp_init);
2128module_exit(dp_cleanup);
2129
2130MODULE_DESCRIPTION("Open vSwitch switching datapath");
2131MODULE_LICENSE("GPL");