blob: 010125c48244686d1444460ed4249820e7ba1b49 [file] [log] [blame]
Jesse Grossccb13522011-10-25 19:26:31 -07001/*
Ben Pfaffad552002014-05-06 16:48:38 -07002 * Copyright (c) 2007-2014 Nicira, Inc.
Jesse Grossccb13522011-10-25 19:26:31 -07003 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of version 2 of the GNU General Public
6 * License as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 * 02110-1301, USA
17 */
18
19#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20
21#include <linux/init.h>
22#include <linux/module.h>
23#include <linux/if_arp.h>
24#include <linux/if_vlan.h>
25#include <linux/in.h>
26#include <linux/ip.h>
27#include <linux/jhash.h>
28#include <linux/delay.h>
29#include <linux/time.h>
30#include <linux/etherdevice.h>
31#include <linux/genetlink.h>
32#include <linux/kernel.h>
33#include <linux/kthread.h>
34#include <linux/mutex.h>
35#include <linux/percpu.h>
36#include <linux/rcupdate.h>
37#include <linux/tcp.h>
38#include <linux/udp.h>
Jesse Grossccb13522011-10-25 19:26:31 -070039#include <linux/ethtool.h>
40#include <linux/wait.h>
Jesse Grossccb13522011-10-25 19:26:31 -070041#include <asm/div64.h>
42#include <linux/highmem.h>
43#include <linux/netfilter_bridge.h>
44#include <linux/netfilter_ipv4.h>
45#include <linux/inetdevice.h>
46#include <linux/list.h>
47#include <linux/openvswitch.h>
48#include <linux/rculist.h>
49#include <linux/dmi.h>
Jesse Grossccb13522011-10-25 19:26:31 -070050#include <net/genetlink.h>
Pravin B Shelar46df7b82012-02-22 19:58:59 -080051#include <net/net_namespace.h>
52#include <net/netns/generic.h>
Jesse Grossccb13522011-10-25 19:26:31 -070053
54#include "datapath.h"
55#include "flow.h"
Andy Zhoue80857c2014-01-21 09:31:04 -080056#include "flow_table.h"
Pravin B Shelare6445712013-10-03 18:16:47 -070057#include "flow_netlink.h"
Jesse Grossccb13522011-10-25 19:26:31 -070058#include "vport-internal_dev.h"
Thomas Grafcff63a52013-04-29 13:06:41 +000059#include "vport-netdev.h"
Jesse Grossccb13522011-10-25 19:26:31 -070060
Pravin B Shelar8e4e1712013-04-15 13:23:03 -070061int ovs_net_id __read_mostly;
62
Pravin B Shelar0c200ef2014-05-06 16:44:50 -070063static struct genl_family dp_packet_genl_family;
64static struct genl_family dp_flow_genl_family;
65static struct genl_family dp_datapath_genl_family;
66
stephen hemminger48e48a72014-07-16 11:25:52 -070067static const struct genl_multicast_group ovs_dp_flow_multicast_group = {
68 .name = OVS_FLOW_MCGROUP,
Pravin B Shelar0c200ef2014-05-06 16:44:50 -070069};
70
stephen hemminger48e48a72014-07-16 11:25:52 -070071static const struct genl_multicast_group ovs_dp_datapath_multicast_group = {
72 .name = OVS_DATAPATH_MCGROUP,
Pravin B Shelar0c200ef2014-05-06 16:44:50 -070073};
74
stephen hemminger48e48a72014-07-16 11:25:52 -070075static const struct genl_multicast_group ovs_dp_vport_multicast_group = {
76 .name = OVS_VPORT_MCGROUP,
Pravin B Shelar0c200ef2014-05-06 16:44:50 -070077};
78
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -070079/* Check if need to build a reply message.
80 * OVS userspace sets the NLM_F_ECHO flag if it needs the reply. */
Samuel Gauthier9b67aa42014-09-18 10:31:04 +020081static bool ovs_must_notify(struct genl_family *family, struct genl_info *info,
82 unsigned int group)
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -070083{
84 return info->nlhdr->nlmsg_flags & NLM_F_ECHO ||
Samuel Gauthier9b67aa42014-09-18 10:31:04 +020085 genl_has_listeners(family, genl_info_net(info)->genl_sock,
86 group);
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -070087}
88
Johannes Berg68eb5502013-11-19 15:19:38 +010089static void ovs_notify(struct genl_family *family,
Johannes Berg2a94fe42013-11-19 15:19:39 +010090 struct sk_buff *skb, struct genl_info *info)
Thomas Grafed661182013-03-29 14:46:50 +010091{
Johannes Berg68eb5502013-11-19 15:19:38 +010092 genl_notify(family, skb, genl_info_net(info), info->snd_portid,
Johannes Berg2a94fe42013-11-19 15:19:39 +010093 0, info->nlhdr, GFP_KERNEL);
Thomas Grafed661182013-03-29 14:46:50 +010094}
95
Pravin B Shelar46df7b82012-02-22 19:58:59 -080096/**
Jesse Grossccb13522011-10-25 19:26:31 -070097 * DOC: Locking:
98 *
Pravin B Shelar8e4e1712013-04-15 13:23:03 -070099 * All writes e.g. Writes to device state (add/remove datapath, port, set
100 * operations on vports, etc.), Writes to other state (flow table
101 * modifications, set miscellaneous datapath parameters, etc.) are protected
102 * by ovs_lock.
Jesse Grossccb13522011-10-25 19:26:31 -0700103 *
104 * Reads are protected by RCU.
105 *
106 * There are a few special cases (mostly stats) that have their own
107 * synchronization but they nest under all of above and don't interact with
108 * each other.
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700109 *
110 * The RTNL lock nests inside ovs_mutex.
Jesse Grossccb13522011-10-25 19:26:31 -0700111 */
112
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700113static DEFINE_MUTEX(ovs_mutex);
114
115void ovs_lock(void)
116{
117 mutex_lock(&ovs_mutex);
118}
119
120void ovs_unlock(void)
121{
122 mutex_unlock(&ovs_mutex);
123}
124
125#ifdef CONFIG_LOCKDEP
126int lockdep_ovsl_is_held(void)
127{
128 if (debug_locks)
129 return lockdep_is_held(&ovs_mutex);
130 else
131 return 1;
132}
133#endif
134
Jesse Grossccb13522011-10-25 19:26:31 -0700135static struct vport *new_vport(const struct vport_parms *);
Thomas Graf8055a892013-12-13 15:22:20 +0100136static int queue_gso_packets(struct datapath *dp, struct sk_buff *,
Jesse Grossccb13522011-10-25 19:26:31 -0700137 const struct dp_upcall_info *);
Thomas Graf8055a892013-12-13 15:22:20 +0100138static int queue_userspace_packet(struct datapath *dp, struct sk_buff *,
Jesse Grossccb13522011-10-25 19:26:31 -0700139 const struct dp_upcall_info *);
140
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700141/* Must be called with rcu_read_lock or ovs_mutex. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800142static struct datapath *get_dp(struct net *net, int dp_ifindex)
Jesse Grossccb13522011-10-25 19:26:31 -0700143{
144 struct datapath *dp = NULL;
145 struct net_device *dev;
146
147 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800148 dev = dev_get_by_index_rcu(net, dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -0700149 if (dev) {
150 struct vport *vport = ovs_internal_dev_get_vport(dev);
151 if (vport)
152 dp = vport->dp;
153 }
154 rcu_read_unlock();
155
156 return dp;
157}
158
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700159/* Must be called with rcu_read_lock or ovs_mutex. */
Andy Zhou971427f32014-09-15 19:37:25 -0700160const char *ovs_dp_name(const struct datapath *dp)
Jesse Grossccb13522011-10-25 19:26:31 -0700161{
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700162 struct vport *vport = ovs_vport_ovsl_rcu(dp, OVSP_LOCAL);
Jesse Grossccb13522011-10-25 19:26:31 -0700163 return vport->ops->get_name(vport);
164}
165
166static int get_dpifindex(struct datapath *dp)
167{
168 struct vport *local;
169 int ifindex;
170
171 rcu_read_lock();
172
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700173 local = ovs_vport_rcu(dp, OVSP_LOCAL);
Jesse Grossccb13522011-10-25 19:26:31 -0700174 if (local)
Thomas Grafcff63a52013-04-29 13:06:41 +0000175 ifindex = netdev_vport_priv(local)->dev->ifindex;
Jesse Grossccb13522011-10-25 19:26:31 -0700176 else
177 ifindex = 0;
178
179 rcu_read_unlock();
180
181 return ifindex;
182}
183
184static void destroy_dp_rcu(struct rcu_head *rcu)
185{
186 struct datapath *dp = container_of(rcu, struct datapath, rcu);
187
Jesse Grossccb13522011-10-25 19:26:31 -0700188 free_percpu(dp->stats_percpu);
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800189 release_net(ovs_dp_get_net(dp));
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700190 kfree(dp->ports);
Jesse Grossccb13522011-10-25 19:26:31 -0700191 kfree(dp);
192}
193
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700194static struct hlist_head *vport_hash_bucket(const struct datapath *dp,
195 u16 port_no)
196{
197 return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)];
198}
199
Jarno Rajahalmebb6f9a72014-05-05 11:32:17 -0700200/* Called with ovs_mutex or RCU read lock. */
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700201struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no)
202{
203 struct vport *vport;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700204 struct hlist_head *head;
205
206 head = vport_hash_bucket(dp, port_no);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800207 hlist_for_each_entry_rcu(vport, head, dp_hash_node) {
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700208 if (vport->port_no == port_no)
209 return vport;
210 }
211 return NULL;
212}
213
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700214/* Called with ovs_mutex. */
Jesse Grossccb13522011-10-25 19:26:31 -0700215static struct vport *new_vport(const struct vport_parms *parms)
216{
217 struct vport *vport;
218
219 vport = ovs_vport_add(parms);
220 if (!IS_ERR(vport)) {
221 struct datapath *dp = parms->dp;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700222 struct hlist_head *head = vport_hash_bucket(dp, vport->port_no);
Jesse Grossccb13522011-10-25 19:26:31 -0700223
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700224 hlist_add_head_rcu(&vport->dp_hash_node, head);
Jesse Grossccb13522011-10-25 19:26:31 -0700225 }
Jesse Grossccb13522011-10-25 19:26:31 -0700226 return vport;
227}
228
Jesse Grossccb13522011-10-25 19:26:31 -0700229void ovs_dp_detach_port(struct vport *p)
230{
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700231 ASSERT_OVSL();
Jesse Grossccb13522011-10-25 19:26:31 -0700232
233 /* First drop references to device. */
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700234 hlist_del_rcu(&p->dp_hash_node);
Jesse Grossccb13522011-10-25 19:26:31 -0700235
236 /* Then destroy it. */
237 ovs_vport_del(p);
238}
239
240/* Must be called with rcu_read_lock. */
Pravin B Shelar8c8b1b82014-09-15 19:28:44 -0700241void ovs_dp_process_packet(struct sk_buff *skb, struct sw_flow_key *key)
Jesse Grossccb13522011-10-25 19:26:31 -0700242{
Pravin B Shelar83c8df22014-09-15 19:20:31 -0700243 const struct vport *p = OVS_CB(skb)->input_vport;
Jesse Grossccb13522011-10-25 19:26:31 -0700244 struct datapath *dp = p->dp;
245 struct sw_flow *flow;
246 struct dp_stats_percpu *stats;
Jesse Grossccb13522011-10-25 19:26:31 -0700247 u64 *stats_counter;
Andy Zhou1bd71162013-10-22 10:42:46 -0700248 u32 n_mask_hit;
Jesse Grossccb13522011-10-25 19:26:31 -0700249
Shan Wei404f2f12012-11-13 09:52:25 +0800250 stats = this_cpu_ptr(dp->stats_percpu);
Jesse Grossccb13522011-10-25 19:26:31 -0700251
Jesse Grossccb13522011-10-25 19:26:31 -0700252 /* Look up flow. */
Pravin B Shelar8c8b1b82014-09-15 19:28:44 -0700253 flow = ovs_flow_tbl_lookup_stats(&dp->table, key, &n_mask_hit);
Jesse Grossccb13522011-10-25 19:26:31 -0700254 if (unlikely(!flow)) {
255 struct dp_upcall_info upcall;
Pravin B Shelar8c8b1b82014-09-15 19:28:44 -0700256 int error;
Jesse Grossccb13522011-10-25 19:26:31 -0700257
258 upcall.cmd = OVS_PACKET_CMD_MISS;
Pravin B Shelar8c8b1b82014-09-15 19:28:44 -0700259 upcall.key = key;
Jesse Grossccb13522011-10-25 19:26:31 -0700260 upcall.userdata = NULL;
Alex Wang5cd667b2014-07-17 15:14:13 -0700261 upcall.portid = ovs_vport_find_upcall_portid(p, skb);
Li RongQingc5eba0b2014-09-03 17:43:45 +0800262 error = ovs_dp_upcall(dp, skb, &upcall);
263 if (unlikely(error))
264 kfree_skb(skb);
265 else
266 consume_skb(skb);
Jesse Grossccb13522011-10-25 19:26:31 -0700267 stats_counter = &stats->n_missed;
268 goto out;
269 }
270
271 OVS_CB(skb)->flow = flow;
272
Pravin B Shelar8c8b1b82014-09-15 19:28:44 -0700273 ovs_flow_stats_update(OVS_CB(skb)->flow, key->tp.flags, skb);
274 ovs_execute_actions(dp, skb, key);
Pravin B Shelare298e502013-10-29 17:22:21 -0700275 stats_counter = &stats->n_hit;
Jesse Grossccb13522011-10-25 19:26:31 -0700276
277out:
278 /* Update datapath statistics. */
WANG Congdf9d9fd2014-02-14 15:10:46 -0800279 u64_stats_update_begin(&stats->syncp);
Jesse Grossccb13522011-10-25 19:26:31 -0700280 (*stats_counter)++;
Andy Zhou1bd71162013-10-22 10:42:46 -0700281 stats->n_mask_hit += n_mask_hit;
WANG Congdf9d9fd2014-02-14 15:10:46 -0800282 u64_stats_update_end(&stats->syncp);
Jesse Grossccb13522011-10-25 19:26:31 -0700283}
284
Jesse Grossccb13522011-10-25 19:26:31 -0700285int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800286 const struct dp_upcall_info *upcall_info)
Jesse Grossccb13522011-10-25 19:26:31 -0700287{
288 struct dp_stats_percpu *stats;
Jesse Grossccb13522011-10-25 19:26:31 -0700289 int err;
290
Eric W. Biederman15e47302012-09-07 20:12:54 +0000291 if (upcall_info->portid == 0) {
Jesse Grossccb13522011-10-25 19:26:31 -0700292 err = -ENOTCONN;
293 goto err;
294 }
295
Jesse Grossccb13522011-10-25 19:26:31 -0700296 if (!skb_is_gso(skb))
Thomas Graf8055a892013-12-13 15:22:20 +0100297 err = queue_userspace_packet(dp, skb, upcall_info);
Jesse Grossccb13522011-10-25 19:26:31 -0700298 else
Thomas Graf8055a892013-12-13 15:22:20 +0100299 err = queue_gso_packets(dp, skb, upcall_info);
Jesse Grossccb13522011-10-25 19:26:31 -0700300 if (err)
301 goto err;
302
303 return 0;
304
305err:
Shan Wei404f2f12012-11-13 09:52:25 +0800306 stats = this_cpu_ptr(dp->stats_percpu);
Jesse Grossccb13522011-10-25 19:26:31 -0700307
WANG Congdf9d9fd2014-02-14 15:10:46 -0800308 u64_stats_update_begin(&stats->syncp);
Jesse Grossccb13522011-10-25 19:26:31 -0700309 stats->n_lost++;
WANG Congdf9d9fd2014-02-14 15:10:46 -0800310 u64_stats_update_end(&stats->syncp);
Jesse Grossccb13522011-10-25 19:26:31 -0700311
312 return err;
313}
314
Thomas Graf8055a892013-12-13 15:22:20 +0100315static int queue_gso_packets(struct datapath *dp, struct sk_buff *skb,
Jesse Grossccb13522011-10-25 19:26:31 -0700316 const struct dp_upcall_info *upcall_info)
317{
Ben Pfaffa1b5d0d2012-07-20 14:47:54 -0700318 unsigned short gso_type = skb_shinfo(skb)->gso_type;
Jesse Grossccb13522011-10-25 19:26:31 -0700319 struct dp_upcall_info later_info;
320 struct sw_flow_key later_key;
321 struct sk_buff *segs, *nskb;
322 int err;
323
Thomas Graf09c5e602013-12-13 15:22:22 +0100324 segs = __skb_gso_segment(skb, NETIF_F_SG, false);
Pravin B Shelar92e5dfc2012-07-20 14:46:29 -0700325 if (IS_ERR(segs))
326 return PTR_ERR(segs);
Jesse Grossccb13522011-10-25 19:26:31 -0700327
328 /* Queue all of the segments. */
329 skb = segs;
330 do {
Thomas Graf8055a892013-12-13 15:22:20 +0100331 err = queue_userspace_packet(dp, skb, upcall_info);
Jesse Grossccb13522011-10-25 19:26:31 -0700332 if (err)
333 break;
334
Ben Pfaffa1b5d0d2012-07-20 14:47:54 -0700335 if (skb == segs && gso_type & SKB_GSO_UDP) {
Jesse Grossccb13522011-10-25 19:26:31 -0700336 /* The initial flow key extracted by ovs_flow_extract()
337 * in this case is for a first fragment, so we need to
338 * properly mark later fragments.
339 */
340 later_key = *upcall_info->key;
341 later_key.ip.frag = OVS_FRAG_TYPE_LATER;
342
343 later_info = *upcall_info;
344 later_info.key = &later_key;
345 upcall_info = &later_info;
346 }
347 } while ((skb = skb->next));
348
349 /* Free all of the segments. */
350 skb = segs;
351 do {
352 nskb = skb->next;
353 if (err)
354 kfree_skb(skb);
355 else
356 consume_skb(skb);
357 } while ((skb = nskb));
358 return err;
359}
360
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100361static size_t key_attr_size(void)
362{
363 return nla_total_size(4) /* OVS_KEY_ATTR_PRIORITY */
Pravin B Shelar7d5437c2013-06-17 17:50:18 -0700364 + nla_total_size(0) /* OVS_KEY_ATTR_TUNNEL */
365 + nla_total_size(8) /* OVS_TUNNEL_KEY_ATTR_ID */
366 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_SRC */
367 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_DST */
368 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TOS */
369 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TTL */
370 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT */
371 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_CSUM */
Jesse Gross67fa0342014-10-03 15:35:30 -0700372 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_OAM */
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100373 + nla_total_size(4) /* OVS_KEY_ATTR_IN_PORT */
374 + nla_total_size(4) /* OVS_KEY_ATTR_SKB_MARK */
375 + nla_total_size(12) /* OVS_KEY_ATTR_ETHERNET */
376 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
377 + nla_total_size(4) /* OVS_KEY_ATTR_8021Q */
378 + nla_total_size(0) /* OVS_KEY_ATTR_ENCAP */
379 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
380 + nla_total_size(40) /* OVS_KEY_ATTR_IPV6 */
381 + nla_total_size(2) /* OVS_KEY_ATTR_ICMPV6 */
382 + nla_total_size(28); /* OVS_KEY_ATTR_ND */
383}
384
Thomas Grafbda56f12013-12-13 15:22:21 +0100385static size_t upcall_msg_size(const struct nlattr *userdata,
386 unsigned int hdrlen)
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100387{
388 size_t size = NLMSG_ALIGN(sizeof(struct ovs_header))
Thomas Grafbda56f12013-12-13 15:22:21 +0100389 + nla_total_size(hdrlen) /* OVS_PACKET_ATTR_PACKET */
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100390 + nla_total_size(key_attr_size()); /* OVS_PACKET_ATTR_KEY */
391
392 /* OVS_PACKET_ATTR_USERDATA */
393 if (userdata)
394 size += NLA_ALIGN(userdata->nla_len);
395
396 return size;
397}
398
Thomas Graf8055a892013-12-13 15:22:20 +0100399static int queue_userspace_packet(struct datapath *dp, struct sk_buff *skb,
Jesse Grossccb13522011-10-25 19:26:31 -0700400 const struct dp_upcall_info *upcall_info)
401{
402 struct ovs_header *upcall;
403 struct sk_buff *nskb = NULL;
Li RongQing4ee45ea2014-09-02 20:52:28 +0800404 struct sk_buff *user_skb = NULL; /* to be queued to userspace */
Jesse Grossccb13522011-10-25 19:26:31 -0700405 struct nlattr *nla;
Thomas Graf795449d2013-11-30 13:21:32 +0100406 struct genl_info info = {
Thomas Graf8055a892013-12-13 15:22:20 +0100407 .dst_sk = ovs_dp_get_net(dp)->genl_sock,
Thomas Graf795449d2013-11-30 13:21:32 +0100408 .snd_portid = upcall_info->portid,
409 };
410 size_t len;
Thomas Grafbda56f12013-12-13 15:22:21 +0100411 unsigned int hlen;
Thomas Graf8055a892013-12-13 15:22:20 +0100412 int err, dp_ifindex;
413
414 dp_ifindex = get_dpifindex(dp);
415 if (!dp_ifindex)
416 return -ENODEV;
Jesse Grossccb13522011-10-25 19:26:31 -0700417
418 if (vlan_tx_tag_present(skb)) {
419 nskb = skb_clone(skb, GFP_ATOMIC);
420 if (!nskb)
421 return -ENOMEM;
422
Patrick McHardy86a9bad2013-04-19 02:04:30 +0000423 nskb = __vlan_put_tag(nskb, nskb->vlan_proto, vlan_tx_tag_get(nskb));
Dan Carpenter8aa51d62012-05-13 08:44:18 +0000424 if (!nskb)
Jesse Grossccb13522011-10-25 19:26:31 -0700425 return -ENOMEM;
426
427 nskb->vlan_tci = 0;
428 skb = nskb;
429 }
430
431 if (nla_attr_size(skb->len) > USHRT_MAX) {
432 err = -EFBIG;
433 goto out;
434 }
435
Thomas Grafbda56f12013-12-13 15:22:21 +0100436 /* Complete checksum if needed */
437 if (skb->ip_summed == CHECKSUM_PARTIAL &&
438 (err = skb_checksum_help(skb)))
439 goto out;
440
441 /* Older versions of OVS user space enforce alignment of the last
442 * Netlink attribute to NLA_ALIGNTO which would require extensive
443 * padding logic. Only perform zerocopy if padding is not required.
444 */
445 if (dp->user_features & OVS_DP_F_UNALIGNED)
446 hlen = skb_zerocopy_headlen(skb);
447 else
448 hlen = skb->len;
449
450 len = upcall_msg_size(upcall_info->userdata, hlen);
Thomas Graf795449d2013-11-30 13:21:32 +0100451 user_skb = genlmsg_new_unicast(len, &info, GFP_ATOMIC);
Jesse Grossccb13522011-10-25 19:26:31 -0700452 if (!user_skb) {
453 err = -ENOMEM;
454 goto out;
455 }
456
457 upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
458 0, upcall_info->cmd);
459 upcall->dp_ifindex = dp_ifindex;
460
461 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
Andy Zhouf53e3832014-07-17 15:17:44 -0700462 err = ovs_nla_put_flow(upcall_info->key, upcall_info->key, user_skb);
463 BUG_ON(err);
Jesse Grossccb13522011-10-25 19:26:31 -0700464 nla_nest_end(user_skb, nla);
465
466 if (upcall_info->userdata)
Ben Pfaff44901082013-02-15 17:29:22 -0800467 __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA,
468 nla_len(upcall_info->userdata),
469 nla_data(upcall_info->userdata));
Jesse Grossccb13522011-10-25 19:26:31 -0700470
Thomas Grafbda56f12013-12-13 15:22:21 +0100471 /* Only reserve room for attribute header, packet data is added
472 * in skb_zerocopy() */
473 if (!(nla = nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, 0))) {
474 err = -ENOBUFS;
475 goto out;
476 }
477 nla->nla_len = nla_attr_size(skb->len);
Jesse Grossccb13522011-10-25 19:26:31 -0700478
Zoltan Kiss36d5fe62014-03-26 22:37:45 +0000479 err = skb_zerocopy(user_skb, skb, skb->len, hlen);
480 if (err)
481 goto out;
Jesse Grossccb13522011-10-25 19:26:31 -0700482
Thomas Grafaea0bb42014-01-14 16:27:49 +0000483 /* Pad OVS_PACKET_ATTR_PACKET if linear copy was performed */
484 if (!(dp->user_features & OVS_DP_F_UNALIGNED)) {
485 size_t plen = NLA_ALIGN(user_skb->len) - user_skb->len;
486
487 if (plen > 0)
488 memset(skb_put(user_skb, plen), 0, plen);
489 }
490
Thomas Grafbda56f12013-12-13 15:22:21 +0100491 ((struct nlmsghdr *) user_skb->data)->nlmsg_len = user_skb->len;
492
Thomas Graf8055a892013-12-13 15:22:20 +0100493 err = genlmsg_unicast(ovs_dp_get_net(dp), user_skb, upcall_info->portid);
Li RongQing4ee45ea2014-09-02 20:52:28 +0800494 user_skb = NULL;
Jesse Grossccb13522011-10-25 19:26:31 -0700495out:
Zoltan Kiss36d5fe62014-03-26 22:37:45 +0000496 if (err)
497 skb_tx_error(skb);
Li RongQing4ee45ea2014-09-02 20:52:28 +0800498 kfree_skb(user_skb);
Jesse Grossccb13522011-10-25 19:26:31 -0700499 kfree_skb(nskb);
500 return err;
501}
502
Jesse Grossccb13522011-10-25 19:26:31 -0700503static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
504{
505 struct ovs_header *ovs_header = info->userhdr;
506 struct nlattr **a = info->attrs;
507 struct sw_flow_actions *acts;
508 struct sk_buff *packet;
509 struct sw_flow *flow;
510 struct datapath *dp;
511 struct ethhdr *eth;
Pravin B Shelar83c8df22014-09-15 19:20:31 -0700512 struct vport *input_vport;
Jesse Grossccb13522011-10-25 19:26:31 -0700513 int len;
514 int err;
Jesse Grossccb13522011-10-25 19:26:31 -0700515
516 err = -EINVAL;
517 if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
Thomas Grafdded45fc2013-03-29 14:46:47 +0100518 !a[OVS_PACKET_ATTR_ACTIONS])
Jesse Grossccb13522011-10-25 19:26:31 -0700519 goto err;
520
521 len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
522 packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
523 err = -ENOMEM;
524 if (!packet)
525 goto err;
526 skb_reserve(packet, NET_IP_ALIGN);
527
Thomas Graf32686a92013-03-29 14:46:48 +0100528 nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
Jesse Grossccb13522011-10-25 19:26:31 -0700529
530 skb_reset_mac_header(packet);
531 eth = eth_hdr(packet);
532
533 /* Normally, setting the skb 'protocol' field would be handled by a
534 * call to eth_type_trans(), but it assumes there's a sending
535 * device, which we may not have. */
Simon Hormane5c5d222013-03-28 13:38:25 +0900536 if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN)
Jesse Grossccb13522011-10-25 19:26:31 -0700537 packet->protocol = eth->h_proto;
538 else
539 packet->protocol = htons(ETH_P_802_2);
540
541 /* Build an sw_flow for sending this packet. */
Jarno Rajahalme23dabf82014-03-27 12:35:23 -0700542 flow = ovs_flow_alloc();
Jesse Grossccb13522011-10-25 19:26:31 -0700543 err = PTR_ERR(flow);
544 if (IS_ERR(flow))
545 goto err_kfree_skb;
546
Pravin B Shelar83c8df22014-09-15 19:20:31 -0700547 err = ovs_flow_key_extract_userspace(a[OVS_PACKET_ATTR_KEY], packet,
548 &flow->key);
Jesse Grossccb13522011-10-25 19:26:31 -0700549 if (err)
550 goto err_flow_free;
551
Pravin B Shelare6445712013-10-03 18:16:47 -0700552 acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_PACKET_ATTR_ACTIONS]));
Jesse Grossccb13522011-10-25 19:26:31 -0700553 err = PTR_ERR(acts);
554 if (IS_ERR(acts))
555 goto err_flow_free;
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700556
Pravin B Shelare6445712013-10-03 18:16:47 -0700557 err = ovs_nla_copy_actions(a[OVS_PACKET_ATTR_ACTIONS],
558 &flow->key, 0, &acts);
Jesse Grossccb13522011-10-25 19:26:31 -0700559 rcu_assign_pointer(flow->sf_acts, acts);
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700560 if (err)
561 goto err_flow_free;
Jesse Grossccb13522011-10-25 19:26:31 -0700562
563 OVS_CB(packet)->flow = flow;
564 packet->priority = flow->key.phy.priority;
Ansis Atteka39c7caeb2012-11-26 11:24:11 -0800565 packet->mark = flow->key.phy.skb_mark;
Jesse Grossccb13522011-10-25 19:26:31 -0700566
567 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800568 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -0700569 err = -ENODEV;
570 if (!dp)
571 goto err_unlock;
572
Pravin B Shelar83c8df22014-09-15 19:20:31 -0700573 input_vport = ovs_vport_rcu(dp, flow->key.phy.in_port);
574 if (!input_vport)
575 input_vport = ovs_vport_rcu(dp, OVSP_LOCAL);
576
577 if (!input_vport)
578 goto err_unlock;
579
580 OVS_CB(packet)->input_vport = input_vport;
581
Jesse Grossccb13522011-10-25 19:26:31 -0700582 local_bh_disable();
Pravin B Shelar2ff3e4e2014-09-15 19:15:28 -0700583 err = ovs_execute_actions(dp, packet, &flow->key);
Jesse Grossccb13522011-10-25 19:26:31 -0700584 local_bh_enable();
585 rcu_read_unlock();
586
Andy Zhou03f0d912013-08-07 20:01:00 -0700587 ovs_flow_free(flow, false);
Jesse Grossccb13522011-10-25 19:26:31 -0700588 return err;
589
590err_unlock:
591 rcu_read_unlock();
592err_flow_free:
Andy Zhou03f0d912013-08-07 20:01:00 -0700593 ovs_flow_free(flow, false);
Jesse Grossccb13522011-10-25 19:26:31 -0700594err_kfree_skb:
595 kfree_skb(packet);
596err:
597 return err;
598}
599
600static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
Thomas Grafdded45fc2013-03-29 14:46:47 +0100601 [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
Jesse Grossccb13522011-10-25 19:26:31 -0700602 [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
603 [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
604};
605
Johannes Berg4534de82013-11-14 17:14:46 +0100606static const struct genl_ops dp_packet_genl_ops[] = {
Jesse Grossccb13522011-10-25 19:26:31 -0700607 { .cmd = OVS_PACKET_CMD_EXECUTE,
608 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
609 .policy = packet_policy,
610 .doit = ovs_packet_cmd_execute
611 }
612};
613
Pravin B Shelar0c200ef2014-05-06 16:44:50 -0700614static struct genl_family dp_packet_genl_family = {
615 .id = GENL_ID_GENERATE,
616 .hdrsize = sizeof(struct ovs_header),
617 .name = OVS_PACKET_FAMILY,
618 .version = OVS_PACKET_VERSION,
619 .maxattr = OVS_PACKET_ATTR_MAX,
620 .netnsok = true,
621 .parallel_ops = true,
622 .ops = dp_packet_genl_ops,
623 .n_ops = ARRAY_SIZE(dp_packet_genl_ops),
624};
625
Andy Zhou1bd71162013-10-22 10:42:46 -0700626static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats,
627 struct ovs_dp_megaflow_stats *mega_stats)
Jesse Grossccb13522011-10-25 19:26:31 -0700628{
629 int i;
Jesse Grossccb13522011-10-25 19:26:31 -0700630
Andy Zhou1bd71162013-10-22 10:42:46 -0700631 memset(mega_stats, 0, sizeof(*mega_stats));
632
Pravin B Shelarb637e492013-10-04 00:14:23 -0700633 stats->n_flows = ovs_flow_tbl_count(&dp->table);
Andy Zhou1bd71162013-10-22 10:42:46 -0700634 mega_stats->n_masks = ovs_flow_tbl_num_masks(&dp->table);
Jesse Grossccb13522011-10-25 19:26:31 -0700635
636 stats->n_hit = stats->n_missed = stats->n_lost = 0;
Andy Zhou1bd71162013-10-22 10:42:46 -0700637
Jesse Grossccb13522011-10-25 19:26:31 -0700638 for_each_possible_cpu(i) {
639 const struct dp_stats_percpu *percpu_stats;
640 struct dp_stats_percpu local_stats;
641 unsigned int start;
642
643 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
644
645 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700646 start = u64_stats_fetch_begin_irq(&percpu_stats->syncp);
Jesse Grossccb13522011-10-25 19:26:31 -0700647 local_stats = *percpu_stats;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700648 } while (u64_stats_fetch_retry_irq(&percpu_stats->syncp, start));
Jesse Grossccb13522011-10-25 19:26:31 -0700649
650 stats->n_hit += local_stats.n_hit;
651 stats->n_missed += local_stats.n_missed;
652 stats->n_lost += local_stats.n_lost;
Andy Zhou1bd71162013-10-22 10:42:46 -0700653 mega_stats->n_mask_hit += local_stats.n_mask_hit;
Jesse Grossccb13522011-10-25 19:26:31 -0700654 }
655}
656
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100657static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts)
658{
659 return NLMSG_ALIGN(sizeof(struct ovs_header))
660 + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_KEY */
Andy Zhou03f0d912013-08-07 20:01:00 -0700661 + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_MASK */
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100662 + nla_total_size(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
663 + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
664 + nla_total_size(8) /* OVS_FLOW_ATTR_USED */
665 + nla_total_size(acts->actions_len); /* OVS_FLOW_ATTR_ACTIONS */
666}
667
Jarno Rajahalmebb6f9a72014-05-05 11:32:17 -0700668/* Called with ovs_mutex or RCU read lock. */
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700669static int ovs_flow_cmd_fill_info(const struct sw_flow *flow, int dp_ifindex,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000670 struct sk_buff *skb, u32 portid,
Jesse Grossccb13522011-10-25 19:26:31 -0700671 u32 seq, u32 flags, u8 cmd)
672{
673 const int skb_orig_len = skb->len;
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700674 struct nlattr *start;
Jesse Grossccb13522011-10-25 19:26:31 -0700675 struct ovs_flow_stats stats;
Pravin B Shelare298e502013-10-29 17:22:21 -0700676 __be16 tcp_flags;
677 unsigned long used;
Jesse Grossccb13522011-10-25 19:26:31 -0700678 struct ovs_header *ovs_header;
679 struct nlattr *nla;
Jesse Grossccb13522011-10-25 19:26:31 -0700680 int err;
681
Eric W. Biederman15e47302012-09-07 20:12:54 +0000682 ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family, flags, cmd);
Jesse Grossccb13522011-10-25 19:26:31 -0700683 if (!ovs_header)
684 return -EMSGSIZE;
685
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700686 ovs_header->dp_ifindex = dp_ifindex;
Jesse Grossccb13522011-10-25 19:26:31 -0700687
Andy Zhou03f0d912013-08-07 20:01:00 -0700688 /* Fill flow key. */
Jesse Grossccb13522011-10-25 19:26:31 -0700689 nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
690 if (!nla)
691 goto nla_put_failure;
Andy Zhou03f0d912013-08-07 20:01:00 -0700692
Pravin B Shelare6445712013-10-03 18:16:47 -0700693 err = ovs_nla_put_flow(&flow->unmasked_key, &flow->unmasked_key, skb);
Jesse Grossccb13522011-10-25 19:26:31 -0700694 if (err)
695 goto error;
696 nla_nest_end(skb, nla);
697
Andy Zhou03f0d912013-08-07 20:01:00 -0700698 nla = nla_nest_start(skb, OVS_FLOW_ATTR_MASK);
699 if (!nla)
700 goto nla_put_failure;
701
Pravin B Shelare6445712013-10-03 18:16:47 -0700702 err = ovs_nla_put_flow(&flow->key, &flow->mask->key, skb);
Andy Zhou03f0d912013-08-07 20:01:00 -0700703 if (err)
704 goto error;
705
706 nla_nest_end(skb, nla);
707
Pravin B Shelare298e502013-10-29 17:22:21 -0700708 ovs_flow_stats_get(flow, &stats, &used, &tcp_flags);
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700709
David S. Miller028d6a62012-03-29 23:20:48 -0400710 if (used &&
711 nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
712 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -0700713
David S. Miller028d6a62012-03-29 23:20:48 -0400714 if (stats.n_packets &&
Pravin B Shelare298e502013-10-29 17:22:21 -0700715 nla_put(skb, OVS_FLOW_ATTR_STATS, sizeof(struct ovs_flow_stats), &stats))
David S. Miller028d6a62012-03-29 23:20:48 -0400716 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -0700717
Pravin B Shelare298e502013-10-29 17:22:21 -0700718 if ((u8)ntohs(tcp_flags) &&
719 nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, (u8)ntohs(tcp_flags)))
David S. Miller028d6a62012-03-29 23:20:48 -0400720 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -0700721
722 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
723 * this is the first flow to be dumped into 'skb'. This is unusual for
724 * Netlink but individual action lists can be longer than
725 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
726 * The userspace caller can always fetch the actions separately if it
727 * really wants them. (Most userspace callers in fact don't care.)
728 *
729 * This can only fail for dump operations because the skb is always
730 * properly sized for single flows.
731 */
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700732 start = nla_nest_start(skb, OVS_FLOW_ATTR_ACTIONS);
733 if (start) {
Pravin B Shelard57170b2013-07-30 15:39:39 -0700734 const struct sw_flow_actions *sf_acts;
735
Jesse Gross663efa32013-12-03 10:58:53 -0800736 sf_acts = rcu_dereference_ovsl(flow->sf_acts);
Pravin B Shelare6445712013-10-03 18:16:47 -0700737 err = ovs_nla_put_actions(sf_acts->actions,
738 sf_acts->actions_len, skb);
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700739
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700740 if (!err)
741 nla_nest_end(skb, start);
742 else {
743 if (skb_orig_len)
744 goto error;
745
746 nla_nest_cancel(skb, start);
747 }
748 } else if (skb_orig_len)
749 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -0700750
751 return genlmsg_end(skb, ovs_header);
752
753nla_put_failure:
754 err = -EMSGSIZE;
755error:
756 genlmsg_cancel(skb, ovs_header);
757 return err;
758}
759
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700760/* May not be called with RCU read lock. */
761static struct sk_buff *ovs_flow_cmd_alloc_info(const struct sw_flow_actions *acts,
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700762 struct genl_info *info,
763 bool always)
Jesse Grossccb13522011-10-25 19:26:31 -0700764{
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700765 struct sk_buff *skb;
Jesse Grossccb13522011-10-25 19:26:31 -0700766
Samuel Gauthier9b67aa42014-09-18 10:31:04 +0200767 if (!always && !ovs_must_notify(&dp_flow_genl_family, info, 0))
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700768 return NULL;
769
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700770 skb = genlmsg_new_unicast(ovs_flow_cmd_msg_size(acts), info, GFP_KERNEL);
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700771 if (!skb)
772 return ERR_PTR(-ENOMEM);
773
774 return skb;
Jesse Grossccb13522011-10-25 19:26:31 -0700775}
776
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700777/* Called with ovs_mutex. */
778static struct sk_buff *ovs_flow_cmd_build_info(const struct sw_flow *flow,
779 int dp_ifindex,
780 struct genl_info *info, u8 cmd,
781 bool always)
Jesse Grossccb13522011-10-25 19:26:31 -0700782{
783 struct sk_buff *skb;
784 int retval;
785
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700786 skb = ovs_flow_cmd_alloc_info(ovsl_dereference(flow->sf_acts), info,
787 always);
Himangi Saraogid0e992a2014-07-27 12:37:46 +0530788 if (IS_ERR_OR_NULL(skb))
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700789 return skb;
Jesse Grossccb13522011-10-25 19:26:31 -0700790
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700791 retval = ovs_flow_cmd_fill_info(flow, dp_ifindex, skb,
792 info->snd_portid, info->snd_seq, 0,
793 cmd);
Jesse Grossccb13522011-10-25 19:26:31 -0700794 BUG_ON(retval < 0);
795 return skb;
796}
797
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700798static int ovs_flow_cmd_new(struct sk_buff *skb, struct genl_info *info)
Jesse Grossccb13522011-10-25 19:26:31 -0700799{
800 struct nlattr **a = info->attrs;
801 struct ovs_header *ovs_header = info->userhdr;
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700802 struct sw_flow *flow, *new_flow;
Andy Zhou03f0d912013-08-07 20:01:00 -0700803 struct sw_flow_mask mask;
Jesse Grossccb13522011-10-25 19:26:31 -0700804 struct sk_buff *reply;
805 struct datapath *dp;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700806 struct sw_flow_actions *acts;
807 struct sw_flow_match match;
808 int error;
809
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700810 /* Must have key and actions. */
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700811 error = -EINVAL;
812 if (!a[OVS_FLOW_ATTR_KEY])
813 goto error;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700814 if (!a[OVS_FLOW_ATTR_ACTIONS])
815 goto error;
816
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700817 /* Most of the time we need to allocate a new flow, do it before
818 * locking.
819 */
820 new_flow = ovs_flow_alloc();
821 if (IS_ERR(new_flow)) {
822 error = PTR_ERR(new_flow);
823 goto error;
824 }
825
826 /* Extract key. */
827 ovs_match_init(&match, &new_flow->unmasked_key, &mask);
828 error = ovs_nla_get_match(&match,
829 a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
830 if (error)
831 goto err_kfree_flow;
832
833 ovs_flow_mask_key(&new_flow->key, &new_flow->unmasked_key, &mask);
834
835 /* Validate actions. */
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700836 acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_FLOW_ATTR_ACTIONS]));
837 error = PTR_ERR(acts);
838 if (IS_ERR(acts))
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700839 goto err_kfree_flow;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700840
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700841 error = ovs_nla_copy_actions(a[OVS_FLOW_ATTR_ACTIONS], &new_flow->key,
842 0, &acts);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700843 if (error) {
844 OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700845 goto err_kfree_acts;
846 }
847
848 reply = ovs_flow_cmd_alloc_info(acts, info, false);
849 if (IS_ERR(reply)) {
850 error = PTR_ERR(reply);
851 goto err_kfree_acts;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700852 }
853
854 ovs_lock();
855 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700856 if (unlikely(!dp)) {
857 error = -ENODEV;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700858 goto err_unlock_ovs;
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700859 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700860 /* Check if this is a duplicate flow */
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700861 flow = ovs_flow_tbl_lookup(&dp->table, &new_flow->unmasked_key);
862 if (likely(!flow)) {
863 rcu_assign_pointer(new_flow->sf_acts, acts);
864
865 /* Put flow in bucket. */
866 error = ovs_flow_tbl_insert(&dp->table, new_flow, &mask);
867 if (unlikely(error)) {
868 acts = NULL;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700869 goto err_unlock_ovs;
870 }
871
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700872 if (unlikely(reply)) {
873 error = ovs_flow_cmd_fill_info(new_flow,
874 ovs_header->dp_ifindex,
875 reply, info->snd_portid,
876 info->snd_seq, 0,
877 OVS_FLOW_CMD_NEW);
878 BUG_ON(error < 0);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700879 }
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700880 ovs_unlock();
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700881 } else {
882 struct sw_flow_actions *old_acts;
883
884 /* Bail out if we're not allowed to modify an existing flow.
885 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
886 * because Generic Netlink treats the latter as a dump
887 * request. We also accept NLM_F_EXCL in case that bug ever
888 * gets fixed.
889 */
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700890 if (unlikely(info->nlhdr->nlmsg_flags & (NLM_F_CREATE
891 | NLM_F_EXCL))) {
892 error = -EEXIST;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700893 goto err_unlock_ovs;
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700894 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700895 /* The unmasked key has to be the same for flow updates. */
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700896 if (unlikely(!ovs_flow_cmp_unmasked_key(flow, &match))) {
Alex Wang4a46b242014-06-30 20:30:29 -0700897 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
898 if (!flow) {
899 error = -ENOENT;
900 goto err_unlock_ovs;
901 }
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700902 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700903 /* Update actions. */
904 old_acts = ovsl_dereference(flow->sf_acts);
905 rcu_assign_pointer(flow->sf_acts, acts);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700906
907 if (unlikely(reply)) {
908 error = ovs_flow_cmd_fill_info(flow,
909 ovs_header->dp_ifindex,
910 reply, info->snd_portid,
911 info->snd_seq, 0,
912 OVS_FLOW_CMD_NEW);
913 BUG_ON(error < 0);
914 }
915 ovs_unlock();
916
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700917 ovs_nla_free_flow_actions(old_acts);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700918 ovs_flow_free(new_flow, false);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700919 }
920
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700921 if (reply)
922 ovs_notify(&dp_flow_genl_family, reply, info);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700923 return 0;
924
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700925err_unlock_ovs:
926 ovs_unlock();
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700927 kfree_skb(reply);
928err_kfree_acts:
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700929 kfree(acts);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700930err_kfree_flow:
931 ovs_flow_free(new_flow, false);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700932error:
933 return error;
934}
935
Jesse Gross6b205b22014-10-03 15:35:32 -0700936static struct sw_flow_actions *get_flow_actions(const struct nlattr *a,
937 const struct sw_flow_key *key,
938 const struct sw_flow_mask *mask)
939{
940 struct sw_flow_actions *acts;
941 struct sw_flow_key masked_key;
942 int error;
943
944 acts = ovs_nla_alloc_flow_actions(nla_len(a));
945 if (IS_ERR(acts))
946 return acts;
947
948 ovs_flow_mask_key(&masked_key, key, mask);
949 error = ovs_nla_copy_actions(a, &masked_key, 0, &acts);
950 if (error) {
951 OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
952 kfree(acts);
953 return ERR_PTR(error);
954 }
955
956 return acts;
957}
958
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700959static int ovs_flow_cmd_set(struct sk_buff *skb, struct genl_info *info)
960{
961 struct nlattr **a = info->attrs;
962 struct ovs_header *ovs_header = info->userhdr;
Jesse Gross6b205b22014-10-03 15:35:32 -0700963 struct sw_flow_key key;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700964 struct sw_flow *flow;
965 struct sw_flow_mask mask;
966 struct sk_buff *reply = NULL;
967 struct datapath *dp;
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700968 struct sw_flow_actions *old_acts = NULL, *acts = NULL;
Andy Zhou03f0d912013-08-07 20:01:00 -0700969 struct sw_flow_match match;
Jesse Grossccb13522011-10-25 19:26:31 -0700970 int error;
Jesse Grossccb13522011-10-25 19:26:31 -0700971
972 /* Extract key. */
973 error = -EINVAL;
974 if (!a[OVS_FLOW_ATTR_KEY])
975 goto error;
Andy Zhou03f0d912013-08-07 20:01:00 -0700976
977 ovs_match_init(&match, &key, &mask);
Jarno Rajahalme23dabf82014-03-27 12:35:23 -0700978 error = ovs_nla_get_match(&match,
Pravin B Shelare6445712013-10-03 18:16:47 -0700979 a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
Jesse Grossccb13522011-10-25 19:26:31 -0700980 if (error)
981 goto error;
982
983 /* Validate actions. */
984 if (a[OVS_FLOW_ATTR_ACTIONS]) {
Jesse Gross6b205b22014-10-03 15:35:32 -0700985 acts = get_flow_actions(a[OVS_FLOW_ATTR_ACTIONS], &key, &mask);
986 if (IS_ERR(acts)) {
987 error = PTR_ERR(acts);
Jesse Grossccb13522011-10-25 19:26:31 -0700988 goto error;
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700989 }
990 }
991
992 /* Can allocate before locking if have acts. */
993 if (acts) {
994 reply = ovs_flow_cmd_alloc_info(acts, info, false);
995 if (IS_ERR(reply)) {
996 error = PTR_ERR(reply);
997 goto err_kfree_acts;
Andy Zhou03f0d912013-08-07 20:01:00 -0700998 }
Jesse Grossccb13522011-10-25 19:26:31 -0700999 }
1000
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001001 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001002 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001003 if (unlikely(!dp)) {
1004 error = -ENODEV;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001005 goto err_unlock_ovs;
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001006 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001007 /* Check that the flow exists. */
Alex Wang4a46b242014-06-30 20:30:29 -07001008 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001009 if (unlikely(!flow)) {
1010 error = -ENOENT;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001011 goto err_unlock_ovs;
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001012 }
Alex Wang4a46b242014-06-30 20:30:29 -07001013
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001014 /* Update actions, if present. */
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001015 if (likely(acts)) {
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001016 old_acts = ovsl_dereference(flow->sf_acts);
Jesse Grossccb13522011-10-25 19:26:31 -07001017 rcu_assign_pointer(flow->sf_acts, acts);
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001018
1019 if (unlikely(reply)) {
1020 error = ovs_flow_cmd_fill_info(flow,
1021 ovs_header->dp_ifindex,
1022 reply, info->snd_portid,
1023 info->snd_seq, 0,
1024 OVS_FLOW_CMD_NEW);
1025 BUG_ON(error < 0);
1026 }
1027 } else {
1028 /* Could not alloc without acts before locking. */
1029 reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex,
1030 info, OVS_FLOW_CMD_NEW, false);
1031 if (unlikely(IS_ERR(reply))) {
1032 error = PTR_ERR(reply);
1033 goto err_unlock_ovs;
1034 }
Jesse Grossccb13522011-10-25 19:26:31 -07001035 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001036
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001037 /* Clear stats. */
1038 if (a[OVS_FLOW_ATTR_CLEAR])
1039 ovs_flow_stats_clear(flow);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001040 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001041
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001042 if (reply)
1043 ovs_notify(&dp_flow_genl_family, reply, info);
1044 if (old_acts)
1045 ovs_nla_free_flow_actions(old_acts);
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -07001046
Jesse Grossccb13522011-10-25 19:26:31 -07001047 return 0;
1048
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001049err_unlock_ovs:
1050 ovs_unlock();
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001051 kfree_skb(reply);
1052err_kfree_acts:
Pravin B Shelar74f84a52013-06-17 17:50:12 -07001053 kfree(acts);
Jesse Grossccb13522011-10-25 19:26:31 -07001054error:
1055 return error;
1056}
1057
1058static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1059{
1060 struct nlattr **a = info->attrs;
1061 struct ovs_header *ovs_header = info->userhdr;
1062 struct sw_flow_key key;
1063 struct sk_buff *reply;
1064 struct sw_flow *flow;
1065 struct datapath *dp;
Andy Zhou03f0d912013-08-07 20:01:00 -07001066 struct sw_flow_match match;
Jesse Grossccb13522011-10-25 19:26:31 -07001067 int err;
Jesse Grossccb13522011-10-25 19:26:31 -07001068
Andy Zhou03f0d912013-08-07 20:01:00 -07001069 if (!a[OVS_FLOW_ATTR_KEY]) {
1070 OVS_NLERR("Flow get message rejected, Key attribute missing.\n");
Jesse Grossccb13522011-10-25 19:26:31 -07001071 return -EINVAL;
Andy Zhou03f0d912013-08-07 20:01:00 -07001072 }
1073
1074 ovs_match_init(&match, &key, NULL);
Jarno Rajahalme23dabf82014-03-27 12:35:23 -07001075 err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL);
Jesse Grossccb13522011-10-25 19:26:31 -07001076 if (err)
1077 return err;
1078
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001079 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001080 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001081 if (!dp) {
1082 err = -ENODEV;
1083 goto unlock;
1084 }
Jesse Grossccb13522011-10-25 19:26:31 -07001085
Alex Wang4a46b242014-06-30 20:30:29 -07001086 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1087 if (!flow) {
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001088 err = -ENOENT;
1089 goto unlock;
1090 }
Jesse Grossccb13522011-10-25 19:26:31 -07001091
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -07001092 reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex, info,
1093 OVS_FLOW_CMD_NEW, true);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001094 if (IS_ERR(reply)) {
1095 err = PTR_ERR(reply);
1096 goto unlock;
1097 }
Jesse Grossccb13522011-10-25 19:26:31 -07001098
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001099 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001100 return genlmsg_reply(reply, info);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001101unlock:
1102 ovs_unlock();
1103 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001104}
1105
1106static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1107{
1108 struct nlattr **a = info->attrs;
1109 struct ovs_header *ovs_header = info->userhdr;
1110 struct sw_flow_key key;
1111 struct sk_buff *reply;
1112 struct sw_flow *flow;
1113 struct datapath *dp;
Andy Zhou03f0d912013-08-07 20:01:00 -07001114 struct sw_flow_match match;
Jesse Grossccb13522011-10-25 19:26:31 -07001115 int err;
Jesse Grossccb13522011-10-25 19:26:31 -07001116
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001117 if (likely(a[OVS_FLOW_ATTR_KEY])) {
1118 ovs_match_init(&match, &key, NULL);
1119 err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL);
1120 if (unlikely(err))
1121 return err;
1122 }
1123
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001124 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001125 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001126 if (unlikely(!dp)) {
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001127 err = -ENODEV;
1128 goto unlock;
1129 }
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001130
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001131 if (unlikely(!a[OVS_FLOW_ATTR_KEY])) {
Pravin B Shelarb637e492013-10-04 00:14:23 -07001132 err = ovs_flow_tbl_flush(&dp->table);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001133 goto unlock;
1134 }
Andy Zhou03f0d912013-08-07 20:01:00 -07001135
Alex Wang4a46b242014-06-30 20:30:29 -07001136 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1137 if (unlikely(!flow)) {
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001138 err = -ENOENT;
1139 goto unlock;
1140 }
Jesse Grossccb13522011-10-25 19:26:31 -07001141
Pravin B Shelarb637e492013-10-04 00:14:23 -07001142 ovs_flow_tbl_remove(&dp->table, flow);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001143 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001144
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001145 reply = ovs_flow_cmd_alloc_info((const struct sw_flow_actions __force *) flow->sf_acts,
1146 info, false);
1147 if (likely(reply)) {
1148 if (likely(!IS_ERR(reply))) {
1149 rcu_read_lock(); /*To keep RCU checker happy. */
1150 err = ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex,
1151 reply, info->snd_portid,
1152 info->snd_seq, 0,
1153 OVS_FLOW_CMD_DEL);
1154 rcu_read_unlock();
1155 BUG_ON(err < 0);
1156
1157 ovs_notify(&dp_flow_genl_family, reply, info);
1158 } else {
1159 netlink_set_err(sock_net(skb->sk)->genl_sock, 0, 0, PTR_ERR(reply));
1160 }
1161 }
1162
1163 ovs_flow_free(flow, true);
Jesse Grossccb13522011-10-25 19:26:31 -07001164 return 0;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001165unlock:
1166 ovs_unlock();
1167 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001168}
1169
1170static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1171{
1172 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
Pravin B Shelarb637e492013-10-04 00:14:23 -07001173 struct table_instance *ti;
Jesse Grossccb13522011-10-25 19:26:31 -07001174 struct datapath *dp;
Jesse Grossccb13522011-10-25 19:26:31 -07001175
Pravin B Shelard57170b2013-07-30 15:39:39 -07001176 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001177 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001178 if (!dp) {
Pravin B Shelard57170b2013-07-30 15:39:39 -07001179 rcu_read_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001180 return -ENODEV;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001181 }
Jesse Grossccb13522011-10-25 19:26:31 -07001182
Pravin B Shelarb637e492013-10-04 00:14:23 -07001183 ti = rcu_dereference(dp->table.ti);
Jesse Grossccb13522011-10-25 19:26:31 -07001184 for (;;) {
1185 struct sw_flow *flow;
1186 u32 bucket, obj;
1187
1188 bucket = cb->args[0];
1189 obj = cb->args[1];
Pravin B Shelarb637e492013-10-04 00:14:23 -07001190 flow = ovs_flow_tbl_dump_next(ti, &bucket, &obj);
Jesse Grossccb13522011-10-25 19:26:31 -07001191 if (!flow)
1192 break;
1193
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -07001194 if (ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex, skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001195 NETLINK_CB(cb->skb).portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001196 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1197 OVS_FLOW_CMD_NEW) < 0)
1198 break;
1199
1200 cb->args[0] = bucket;
1201 cb->args[1] = obj;
1202 }
Pravin B Shelard57170b2013-07-30 15:39:39 -07001203 rcu_read_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001204 return skb->len;
1205}
1206
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001207static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
1208 [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
1209 [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
1210 [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
1211};
1212
stephen hemminger48e48a72014-07-16 11:25:52 -07001213static const struct genl_ops dp_flow_genl_ops[] = {
Jesse Grossccb13522011-10-25 19:26:31 -07001214 { .cmd = OVS_FLOW_CMD_NEW,
1215 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1216 .policy = flow_policy,
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001217 .doit = ovs_flow_cmd_new
Jesse Grossccb13522011-10-25 19:26:31 -07001218 },
1219 { .cmd = OVS_FLOW_CMD_DEL,
1220 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1221 .policy = flow_policy,
1222 .doit = ovs_flow_cmd_del
1223 },
1224 { .cmd = OVS_FLOW_CMD_GET,
1225 .flags = 0, /* OK for unprivileged users. */
1226 .policy = flow_policy,
1227 .doit = ovs_flow_cmd_get,
1228 .dumpit = ovs_flow_cmd_dump
1229 },
1230 { .cmd = OVS_FLOW_CMD_SET,
1231 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1232 .policy = flow_policy,
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001233 .doit = ovs_flow_cmd_set,
Jesse Grossccb13522011-10-25 19:26:31 -07001234 },
1235};
1236
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001237static struct genl_family dp_flow_genl_family = {
Jesse Grossccb13522011-10-25 19:26:31 -07001238 .id = GENL_ID_GENERATE,
1239 .hdrsize = sizeof(struct ovs_header),
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001240 .name = OVS_FLOW_FAMILY,
1241 .version = OVS_FLOW_VERSION,
1242 .maxattr = OVS_FLOW_ATTR_MAX,
Pravin B Shelar3a4e0d62013-04-23 07:48:48 +00001243 .netnsok = true,
1244 .parallel_ops = true,
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001245 .ops = dp_flow_genl_ops,
1246 .n_ops = ARRAY_SIZE(dp_flow_genl_ops),
1247 .mcgrps = &ovs_dp_flow_multicast_group,
1248 .n_mcgrps = 1,
Jesse Grossccb13522011-10-25 19:26:31 -07001249};
1250
Thomas Grafc3ff8cf2013-03-29 14:46:49 +01001251static size_t ovs_dp_cmd_msg_size(void)
1252{
1253 size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
1254
1255 msgsize += nla_total_size(IFNAMSIZ);
1256 msgsize += nla_total_size(sizeof(struct ovs_dp_stats));
Andy Zhou1bd71162013-10-22 10:42:46 -07001257 msgsize += nla_total_size(sizeof(struct ovs_dp_megaflow_stats));
Daniele Di Proietto45fb9c32014-01-23 10:47:35 -08001258 msgsize += nla_total_size(sizeof(u32)); /* OVS_DP_ATTR_USER_FEATURES */
Thomas Grafc3ff8cf2013-03-29 14:46:49 +01001259
1260 return msgsize;
1261}
1262
Jarno Rajahalmebb6f9a72014-05-05 11:32:17 -07001263/* Called with ovs_mutex or RCU read lock. */
Jesse Grossccb13522011-10-25 19:26:31 -07001264static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001265 u32 portid, u32 seq, u32 flags, u8 cmd)
Jesse Grossccb13522011-10-25 19:26:31 -07001266{
1267 struct ovs_header *ovs_header;
1268 struct ovs_dp_stats dp_stats;
Andy Zhou1bd71162013-10-22 10:42:46 -07001269 struct ovs_dp_megaflow_stats dp_megaflow_stats;
Jesse Grossccb13522011-10-25 19:26:31 -07001270 int err;
1271
Eric W. Biederman15e47302012-09-07 20:12:54 +00001272 ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
Jesse Grossccb13522011-10-25 19:26:31 -07001273 flags, cmd);
1274 if (!ovs_header)
1275 goto error;
1276
1277 ovs_header->dp_ifindex = get_dpifindex(dp);
1278
Jesse Grossccb13522011-10-25 19:26:31 -07001279 err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
Jesse Grossccb13522011-10-25 19:26:31 -07001280 if (err)
1281 goto nla_put_failure;
1282
Andy Zhou1bd71162013-10-22 10:42:46 -07001283 get_dp_stats(dp, &dp_stats, &dp_megaflow_stats);
1284 if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats),
1285 &dp_stats))
1286 goto nla_put_failure;
1287
1288 if (nla_put(skb, OVS_DP_ATTR_MEGAFLOW_STATS,
1289 sizeof(struct ovs_dp_megaflow_stats),
1290 &dp_megaflow_stats))
David S. Miller028d6a62012-03-29 23:20:48 -04001291 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001292
Thomas Graf43d4be92013-12-13 15:22:18 +01001293 if (nla_put_u32(skb, OVS_DP_ATTR_USER_FEATURES, dp->user_features))
1294 goto nla_put_failure;
1295
Jesse Grossccb13522011-10-25 19:26:31 -07001296 return genlmsg_end(skb, ovs_header);
1297
1298nla_put_failure:
1299 genlmsg_cancel(skb, ovs_header);
1300error:
1301 return -EMSGSIZE;
1302}
1303
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001304static struct sk_buff *ovs_dp_cmd_alloc_info(struct genl_info *info)
Jesse Grossccb13522011-10-25 19:26:31 -07001305{
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001306 return genlmsg_new_unicast(ovs_dp_cmd_msg_size(), info, GFP_KERNEL);
Jesse Grossccb13522011-10-25 19:26:31 -07001307}
1308
Jarno Rajahalmebb6f9a72014-05-05 11:32:17 -07001309/* Called with rcu_read_lock or ovs_mutex. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001310static struct datapath *lookup_datapath(struct net *net,
1311 struct ovs_header *ovs_header,
Jesse Grossccb13522011-10-25 19:26:31 -07001312 struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1313{
1314 struct datapath *dp;
1315
1316 if (!a[OVS_DP_ATTR_NAME])
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001317 dp = get_dp(net, ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001318 else {
1319 struct vport *vport;
1320
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001321 vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
Jesse Grossccb13522011-10-25 19:26:31 -07001322 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
Jesse Grossccb13522011-10-25 19:26:31 -07001323 }
1324 return dp ? dp : ERR_PTR(-ENODEV);
1325}
1326
Thomas Graf44da5ae2013-12-13 15:22:19 +01001327static void ovs_dp_reset_user_features(struct sk_buff *skb, struct genl_info *info)
1328{
1329 struct datapath *dp;
1330
1331 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
Jiri Pirko3c7eacf2014-02-14 11:42:36 +01001332 if (IS_ERR(dp))
Thomas Graf44da5ae2013-12-13 15:22:19 +01001333 return;
1334
1335 WARN(dp->user_features, "Dropping previously announced user features\n");
1336 dp->user_features = 0;
1337}
1338
Thomas Graf43d4be92013-12-13 15:22:18 +01001339static void ovs_dp_change(struct datapath *dp, struct nlattr **a)
1340{
1341 if (a[OVS_DP_ATTR_USER_FEATURES])
1342 dp->user_features = nla_get_u32(a[OVS_DP_ATTR_USER_FEATURES]);
1343}
1344
Jesse Grossccb13522011-10-25 19:26:31 -07001345static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1346{
1347 struct nlattr **a = info->attrs;
1348 struct vport_parms parms;
1349 struct sk_buff *reply;
1350 struct datapath *dp;
1351 struct vport *vport;
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001352 struct ovs_net *ovs_net;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001353 int err, i;
Jesse Grossccb13522011-10-25 19:26:31 -07001354
1355 err = -EINVAL;
1356 if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1357 goto err;
1358
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001359 reply = ovs_dp_cmd_alloc_info(info);
1360 if (!reply)
1361 return -ENOMEM;
Jesse Grossccb13522011-10-25 19:26:31 -07001362
1363 err = -ENOMEM;
1364 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1365 if (dp == NULL)
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001366 goto err_free_reply;
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001367
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001368 ovs_dp_set_net(dp, hold_net(sock_net(skb->sk)));
Jesse Grossccb13522011-10-25 19:26:31 -07001369
1370 /* Allocate table. */
Pravin B Shelarb637e492013-10-04 00:14:23 -07001371 err = ovs_flow_tbl_init(&dp->table);
1372 if (err)
Jesse Grossccb13522011-10-25 19:26:31 -07001373 goto err_free_dp;
1374
WANG Cong1c213bd2014-02-13 11:46:28 -08001375 dp->stats_percpu = netdev_alloc_pcpu_stats(struct dp_stats_percpu);
Jesse Grossccb13522011-10-25 19:26:31 -07001376 if (!dp->stats_percpu) {
1377 err = -ENOMEM;
1378 goto err_destroy_table;
1379 }
1380
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001381 dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
Pravin B Shelare6445712013-10-03 18:16:47 -07001382 GFP_KERNEL);
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001383 if (!dp->ports) {
1384 err = -ENOMEM;
1385 goto err_destroy_percpu;
1386 }
1387
1388 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1389 INIT_HLIST_HEAD(&dp->ports[i]);
1390
Jesse Grossccb13522011-10-25 19:26:31 -07001391 /* Set up our datapath device. */
1392 parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1393 parms.type = OVS_VPORT_TYPE_INTERNAL;
1394 parms.options = NULL;
1395 parms.dp = dp;
1396 parms.port_no = OVSP_LOCAL;
Alex Wang5cd667b2014-07-17 15:14:13 -07001397 parms.upcall_portids = a[OVS_DP_ATTR_UPCALL_PID];
Jesse Grossccb13522011-10-25 19:26:31 -07001398
Thomas Graf43d4be92013-12-13 15:22:18 +01001399 ovs_dp_change(dp, a);
1400
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001401 /* So far only local changes have been made, now need the lock. */
1402 ovs_lock();
1403
Jesse Grossccb13522011-10-25 19:26:31 -07001404 vport = new_vport(&parms);
1405 if (IS_ERR(vport)) {
1406 err = PTR_ERR(vport);
1407 if (err == -EBUSY)
1408 err = -EEXIST;
1409
Thomas Graf44da5ae2013-12-13 15:22:19 +01001410 if (err == -EEXIST) {
1411 /* An outdated user space instance that does not understand
1412 * the concept of user_features has attempted to create a new
1413 * datapath and is likely to reuse it. Drop all user features.
1414 */
1415 if (info->genlhdr->version < OVS_DP_VER_FEATURES)
1416 ovs_dp_reset_user_features(skb, info);
1417 }
1418
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001419 goto err_destroy_ports_array;
Jesse Grossccb13522011-10-25 19:26:31 -07001420 }
1421
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001422 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1423 info->snd_seq, 0, OVS_DP_CMD_NEW);
1424 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001425
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001426 ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001427 list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001428
1429 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001430
Johannes Berg2a94fe42013-11-19 15:19:39 +01001431 ovs_notify(&dp_datapath_genl_family, reply, info);
Jesse Grossccb13522011-10-25 19:26:31 -07001432 return 0;
1433
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001434err_destroy_ports_array:
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001435 ovs_unlock();
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001436 kfree(dp->ports);
Jesse Grossccb13522011-10-25 19:26:31 -07001437err_destroy_percpu:
1438 free_percpu(dp->stats_percpu);
1439err_destroy_table:
Andy Zhoue80857c2014-01-21 09:31:04 -08001440 ovs_flow_tbl_destroy(&dp->table, false);
Jesse Grossccb13522011-10-25 19:26:31 -07001441err_free_dp:
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001442 release_net(ovs_dp_get_net(dp));
Jesse Grossccb13522011-10-25 19:26:31 -07001443 kfree(dp);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001444err_free_reply:
1445 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001446err:
1447 return err;
1448}
1449
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001450/* Called with ovs_mutex. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001451static void __dp_destroy(struct datapath *dp)
Jesse Grossccb13522011-10-25 19:26:31 -07001452{
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001453 int i;
Jesse Grossccb13522011-10-25 19:26:31 -07001454
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001455 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1456 struct vport *vport;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001457 struct hlist_node *n;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001458
Sasha Levinb67bfe02013-02-27 17:06:00 -08001459 hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001460 if (vport->port_no != OVSP_LOCAL)
1461 ovs_dp_detach_port(vport);
1462 }
Jesse Grossccb13522011-10-25 19:26:31 -07001463
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001464 list_del_rcu(&dp->list_node);
Jesse Grossccb13522011-10-25 19:26:31 -07001465
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001466 /* OVSP_LOCAL is datapath internal port. We need to make sure that
Andy Zhoue80857c2014-01-21 09:31:04 -08001467 * all ports in datapath are destroyed first before freeing datapath.
Jesse Grossccb13522011-10-25 19:26:31 -07001468 */
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001469 ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
Jesse Grossccb13522011-10-25 19:26:31 -07001470
Andy Zhoue80857c2014-01-21 09:31:04 -08001471 /* RCU destroy the flow table */
1472 ovs_flow_tbl_destroy(&dp->table, true);
1473
Jesse Grossccb13522011-10-25 19:26:31 -07001474 call_rcu(&dp->rcu, destroy_dp_rcu);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001475}
1476
1477static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1478{
1479 struct sk_buff *reply;
1480 struct datapath *dp;
1481 int err;
1482
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001483 reply = ovs_dp_cmd_alloc_info(info);
1484 if (!reply)
1485 return -ENOMEM;
1486
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001487 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001488 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1489 err = PTR_ERR(dp);
1490 if (IS_ERR(dp))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001491 goto err_unlock_free;
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001492
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001493 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1494 info->snd_seq, 0, OVS_DP_CMD_DEL);
1495 BUG_ON(err < 0);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001496
1497 __dp_destroy(dp);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001498 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001499
Johannes Berg2a94fe42013-11-19 15:19:39 +01001500 ovs_notify(&dp_datapath_genl_family, reply, info);
Jesse Grossccb13522011-10-25 19:26:31 -07001501
1502 return 0;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001503
1504err_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001505 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001506 kfree_skb(reply);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001507 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001508}
1509
1510static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1511{
1512 struct sk_buff *reply;
1513 struct datapath *dp;
1514 int err;
1515
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001516 reply = ovs_dp_cmd_alloc_info(info);
1517 if (!reply)
1518 return -ENOMEM;
1519
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001520 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001521 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001522 err = PTR_ERR(dp);
Jesse Grossccb13522011-10-25 19:26:31 -07001523 if (IS_ERR(dp))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001524 goto err_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001525
Thomas Graf43d4be92013-12-13 15:22:18 +01001526 ovs_dp_change(dp, info->attrs);
1527
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001528 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1529 info->snd_seq, 0, OVS_DP_CMD_NEW);
1530 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001531
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001532 ovs_unlock();
Johannes Berg2a94fe42013-11-19 15:19:39 +01001533 ovs_notify(&dp_datapath_genl_family, reply, info);
Jesse Grossccb13522011-10-25 19:26:31 -07001534
1535 return 0;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001536
1537err_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001538 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001539 kfree_skb(reply);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001540 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001541}
1542
1543static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1544{
1545 struct sk_buff *reply;
1546 struct datapath *dp;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001547 int err;
Jesse Grossccb13522011-10-25 19:26:31 -07001548
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001549 reply = ovs_dp_cmd_alloc_info(info);
1550 if (!reply)
1551 return -ENOMEM;
1552
1553 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001554 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001555 if (IS_ERR(dp)) {
1556 err = PTR_ERR(dp);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001557 goto err_unlock_free;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001558 }
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001559 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1560 info->snd_seq, 0, OVS_DP_CMD_NEW);
1561 BUG_ON(err < 0);
1562 rcu_read_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001563
Jesse Grossccb13522011-10-25 19:26:31 -07001564 return genlmsg_reply(reply, info);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001565
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001566err_unlock_free:
1567 rcu_read_unlock();
1568 kfree_skb(reply);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001569 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001570}
1571
1572static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1573{
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001574 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
Jesse Grossccb13522011-10-25 19:26:31 -07001575 struct datapath *dp;
1576 int skip = cb->args[0];
1577 int i = 0;
1578
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001579 rcu_read_lock();
1580 list_for_each_entry_rcu(dp, &ovs_net->dps, list_node) {
Ben Pfaff77676fd2012-01-17 13:33:39 +00001581 if (i >= skip &&
Eric W. Biederman15e47302012-09-07 20:12:54 +00001582 ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001583 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1584 OVS_DP_CMD_NEW) < 0)
1585 break;
1586 i++;
1587 }
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001588 rcu_read_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001589
1590 cb->args[0] = i;
1591
1592 return skb->len;
1593}
1594
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001595static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1596 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1597 [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1598 [OVS_DP_ATTR_USER_FEATURES] = { .type = NLA_U32 },
1599};
1600
stephen hemminger48e48a72014-07-16 11:25:52 -07001601static const struct genl_ops dp_datapath_genl_ops[] = {
Jesse Grossccb13522011-10-25 19:26:31 -07001602 { .cmd = OVS_DP_CMD_NEW,
1603 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1604 .policy = datapath_policy,
1605 .doit = ovs_dp_cmd_new
1606 },
1607 { .cmd = OVS_DP_CMD_DEL,
1608 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1609 .policy = datapath_policy,
1610 .doit = ovs_dp_cmd_del
1611 },
1612 { .cmd = OVS_DP_CMD_GET,
1613 .flags = 0, /* OK for unprivileged users. */
1614 .policy = datapath_policy,
1615 .doit = ovs_dp_cmd_get,
1616 .dumpit = ovs_dp_cmd_dump
1617 },
1618 { .cmd = OVS_DP_CMD_SET,
1619 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1620 .policy = datapath_policy,
1621 .doit = ovs_dp_cmd_set,
1622 },
1623};
1624
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001625static struct genl_family dp_datapath_genl_family = {
Jesse Grossccb13522011-10-25 19:26:31 -07001626 .id = GENL_ID_GENERATE,
1627 .hdrsize = sizeof(struct ovs_header),
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001628 .name = OVS_DATAPATH_FAMILY,
1629 .version = OVS_DATAPATH_VERSION,
1630 .maxattr = OVS_DP_ATTR_MAX,
Pravin B Shelar3a4e0d62013-04-23 07:48:48 +00001631 .netnsok = true,
1632 .parallel_ops = true,
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001633 .ops = dp_datapath_genl_ops,
1634 .n_ops = ARRAY_SIZE(dp_datapath_genl_ops),
1635 .mcgrps = &ovs_dp_datapath_multicast_group,
1636 .n_mcgrps = 1,
Jesse Grossccb13522011-10-25 19:26:31 -07001637};
1638
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001639/* Called with ovs_mutex or RCU read lock. */
Jesse Grossccb13522011-10-25 19:26:31 -07001640static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001641 u32 portid, u32 seq, u32 flags, u8 cmd)
Jesse Grossccb13522011-10-25 19:26:31 -07001642{
1643 struct ovs_header *ovs_header;
1644 struct ovs_vport_stats vport_stats;
1645 int err;
1646
Eric W. Biederman15e47302012-09-07 20:12:54 +00001647 ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
Jesse Grossccb13522011-10-25 19:26:31 -07001648 flags, cmd);
1649 if (!ovs_header)
1650 return -EMSGSIZE;
1651
1652 ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1653
David S. Miller028d6a62012-03-29 23:20:48 -04001654 if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
1655 nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
Alex Wang5cd667b2014-07-17 15:14:13 -07001656 nla_put_string(skb, OVS_VPORT_ATTR_NAME,
1657 vport->ops->get_name(vport)))
David S. Miller028d6a62012-03-29 23:20:48 -04001658 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001659
1660 ovs_vport_get_stats(vport, &vport_stats);
David S. Miller028d6a62012-03-29 23:20:48 -04001661 if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
1662 &vport_stats))
1663 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001664
Alex Wang5cd667b2014-07-17 15:14:13 -07001665 if (ovs_vport_get_upcall_portids(vport, skb))
1666 goto nla_put_failure;
1667
Jesse Grossccb13522011-10-25 19:26:31 -07001668 err = ovs_vport_get_options(vport, skb);
1669 if (err == -EMSGSIZE)
1670 goto error;
1671
1672 return genlmsg_end(skb, ovs_header);
1673
1674nla_put_failure:
1675 err = -EMSGSIZE;
1676error:
1677 genlmsg_cancel(skb, ovs_header);
1678 return err;
1679}
1680
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001681static struct sk_buff *ovs_vport_cmd_alloc_info(void)
1682{
1683 return nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1684}
1685
1686/* Called with ovs_mutex, only via ovs_dp_notify_wq(). */
Eric W. Biederman15e47302012-09-07 20:12:54 +00001687struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001688 u32 seq, u8 cmd)
1689{
1690 struct sk_buff *skb;
1691 int retval;
1692
1693 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1694 if (!skb)
1695 return ERR_PTR(-ENOMEM);
1696
Eric W. Biederman15e47302012-09-07 20:12:54 +00001697 retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
Jesse Grossa9341512013-03-26 15:48:38 -07001698 BUG_ON(retval < 0);
1699
Jesse Grossccb13522011-10-25 19:26:31 -07001700 return skb;
1701}
1702
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001703/* Called with ovs_mutex or RCU read lock. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001704static struct vport *lookup_vport(struct net *net,
1705 struct ovs_header *ovs_header,
Jesse Grossccb13522011-10-25 19:26:31 -07001706 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1707{
1708 struct datapath *dp;
1709 struct vport *vport;
1710
1711 if (a[OVS_VPORT_ATTR_NAME]) {
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001712 vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
Jesse Grossccb13522011-10-25 19:26:31 -07001713 if (!vport)
1714 return ERR_PTR(-ENODEV);
Ben Pfaff651a68e2012-03-06 15:04:04 -08001715 if (ovs_header->dp_ifindex &&
1716 ovs_header->dp_ifindex != get_dpifindex(vport->dp))
1717 return ERR_PTR(-ENODEV);
Jesse Grossccb13522011-10-25 19:26:31 -07001718 return vport;
1719 } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
1720 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1721
1722 if (port_no >= DP_MAX_PORTS)
1723 return ERR_PTR(-EFBIG);
1724
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001725 dp = get_dp(net, ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001726 if (!dp)
1727 return ERR_PTR(-ENODEV);
1728
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001729 vport = ovs_vport_ovsl_rcu(dp, port_no);
Jesse Grossccb13522011-10-25 19:26:31 -07001730 if (!vport)
Jarno Rajahalme14408db2013-01-09 14:27:35 -08001731 return ERR_PTR(-ENODEV);
Jesse Grossccb13522011-10-25 19:26:31 -07001732 return vport;
1733 } else
1734 return ERR_PTR(-EINVAL);
1735}
1736
1737static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
1738{
1739 struct nlattr **a = info->attrs;
1740 struct ovs_header *ovs_header = info->userhdr;
1741 struct vport_parms parms;
1742 struct sk_buff *reply;
1743 struct vport *vport;
1744 struct datapath *dp;
1745 u32 port_no;
1746 int err;
1747
Jesse Grossccb13522011-10-25 19:26:31 -07001748 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
1749 !a[OVS_VPORT_ATTR_UPCALL_PID])
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001750 return -EINVAL;
1751
1752 port_no = a[OVS_VPORT_ATTR_PORT_NO]
1753 ? nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]) : 0;
1754 if (port_no >= DP_MAX_PORTS)
1755 return -EFBIG;
1756
1757 reply = ovs_vport_cmd_alloc_info();
1758 if (!reply)
1759 return -ENOMEM;
Jesse Grossccb13522011-10-25 19:26:31 -07001760
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001761 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001762 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001763 err = -ENODEV;
1764 if (!dp)
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001765 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001766
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001767 if (port_no) {
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001768 vport = ovs_vport_ovsl(dp, port_no);
Jesse Grossccb13522011-10-25 19:26:31 -07001769 err = -EBUSY;
1770 if (vport)
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001771 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001772 } else {
1773 for (port_no = 1; ; port_no++) {
1774 if (port_no >= DP_MAX_PORTS) {
1775 err = -EFBIG;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001776 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001777 }
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001778 vport = ovs_vport_ovsl(dp, port_no);
Jesse Grossccb13522011-10-25 19:26:31 -07001779 if (!vport)
1780 break;
1781 }
1782 }
1783
1784 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
1785 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1786 parms.options = a[OVS_VPORT_ATTR_OPTIONS];
1787 parms.dp = dp;
1788 parms.port_no = port_no;
Alex Wang5cd667b2014-07-17 15:14:13 -07001789 parms.upcall_portids = a[OVS_VPORT_ATTR_UPCALL_PID];
Jesse Grossccb13522011-10-25 19:26:31 -07001790
1791 vport = new_vport(&parms);
1792 err = PTR_ERR(vport);
1793 if (IS_ERR(vport))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001794 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001795
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001796 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1797 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1798 BUG_ON(err < 0);
1799 ovs_unlock();
Thomas Grafed661182013-03-29 14:46:50 +01001800
Johannes Berg2a94fe42013-11-19 15:19:39 +01001801 ovs_notify(&dp_vport_genl_family, reply, info);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001802 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001803
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001804exit_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001805 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001806 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001807 return err;
1808}
1809
1810static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
1811{
1812 struct nlattr **a = info->attrs;
1813 struct sk_buff *reply;
1814 struct vport *vport;
1815 int err;
1816
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001817 reply = ovs_vport_cmd_alloc_info();
1818 if (!reply)
1819 return -ENOMEM;
1820
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001821 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001822 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
Jesse Grossccb13522011-10-25 19:26:31 -07001823 err = PTR_ERR(vport);
1824 if (IS_ERR(vport))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001825 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001826
Jesse Grossccb13522011-10-25 19:26:31 -07001827 if (a[OVS_VPORT_ATTR_TYPE] &&
Jesse Grossf44f3402013-05-13 08:15:26 -07001828 nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
Jesse Grossccb13522011-10-25 19:26:31 -07001829 err = -EINVAL;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001830 goto exit_unlock_free;
Jesse Grossa9341512013-03-26 15:48:38 -07001831 }
1832
Jesse Grossf44f3402013-05-13 08:15:26 -07001833 if (a[OVS_VPORT_ATTR_OPTIONS]) {
Jesse Grossccb13522011-10-25 19:26:31 -07001834 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
Jesse Grossf44f3402013-05-13 08:15:26 -07001835 if (err)
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001836 goto exit_unlock_free;
Jesse Grossf44f3402013-05-13 08:15:26 -07001837 }
Jesse Grossa9341512013-03-26 15:48:38 -07001838
Alex Wang5cd667b2014-07-17 15:14:13 -07001839
1840 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
1841 struct nlattr *ids = a[OVS_VPORT_ATTR_UPCALL_PID];
1842
1843 err = ovs_vport_set_upcall_portids(vport, ids);
1844 if (err)
1845 goto exit_unlock_free;
1846 }
Jesse Grossccb13522011-10-25 19:26:31 -07001847
Jesse Grossa9341512013-03-26 15:48:38 -07001848 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1849 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1850 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001851
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001852 ovs_unlock();
Johannes Berg2a94fe42013-11-19 15:19:39 +01001853 ovs_notify(&dp_vport_genl_family, reply, info);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001854 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001855
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001856exit_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001857 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001858 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001859 return err;
1860}
1861
1862static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
1863{
1864 struct nlattr **a = info->attrs;
1865 struct sk_buff *reply;
1866 struct vport *vport;
1867 int err;
1868
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001869 reply = ovs_vport_cmd_alloc_info();
1870 if (!reply)
1871 return -ENOMEM;
1872
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001873 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001874 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
Jesse Grossccb13522011-10-25 19:26:31 -07001875 err = PTR_ERR(vport);
1876 if (IS_ERR(vport))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001877 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001878
1879 if (vport->port_no == OVSP_LOCAL) {
1880 err = -EINVAL;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001881 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001882 }
1883
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001884 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1885 info->snd_seq, 0, OVS_VPORT_CMD_DEL);
1886 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001887 ovs_dp_detach_port(vport);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001888 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001889
Johannes Berg2a94fe42013-11-19 15:19:39 +01001890 ovs_notify(&dp_vport_genl_family, reply, info);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001891 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001892
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001893exit_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001894 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001895 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001896 return err;
1897}
1898
1899static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
1900{
1901 struct nlattr **a = info->attrs;
1902 struct ovs_header *ovs_header = info->userhdr;
1903 struct sk_buff *reply;
1904 struct vport *vport;
1905 int err;
1906
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001907 reply = ovs_vport_cmd_alloc_info();
1908 if (!reply)
1909 return -ENOMEM;
1910
Jesse Grossccb13522011-10-25 19:26:31 -07001911 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001912 vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
Jesse Grossccb13522011-10-25 19:26:31 -07001913 err = PTR_ERR(vport);
1914 if (IS_ERR(vport))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001915 goto exit_unlock_free;
1916 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1917 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1918 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001919 rcu_read_unlock();
1920
1921 return genlmsg_reply(reply, info);
1922
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001923exit_unlock_free:
Jesse Grossccb13522011-10-25 19:26:31 -07001924 rcu_read_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001925 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001926 return err;
1927}
1928
1929static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1930{
1931 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1932 struct datapath *dp;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001933 int bucket = cb->args[0], skip = cb->args[1];
1934 int i, j = 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001935
Jesse Grossccb13522011-10-25 19:26:31 -07001936 rcu_read_lock();
Jarno Rajahalme42ee19e2014-02-15 17:42:29 -08001937 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1938 if (!dp) {
1939 rcu_read_unlock();
1940 return -ENODEV;
1941 }
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001942 for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
Jesse Grossccb13522011-10-25 19:26:31 -07001943 struct vport *vport;
1944
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001945 j = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001946 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001947 if (j >= skip &&
1948 ovs_vport_cmd_fill_info(vport, skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001949 NETLINK_CB(cb->skb).portid,
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001950 cb->nlh->nlmsg_seq,
1951 NLM_F_MULTI,
1952 OVS_VPORT_CMD_NEW) < 0)
1953 goto out;
Jesse Grossccb13522011-10-25 19:26:31 -07001954
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001955 j++;
1956 }
1957 skip = 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001958 }
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001959out:
Jesse Grossccb13522011-10-25 19:26:31 -07001960 rcu_read_unlock();
1961
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001962 cb->args[0] = i;
1963 cb->args[1] = j;
Jesse Grossccb13522011-10-25 19:26:31 -07001964
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001965 return skb->len;
Jesse Grossccb13522011-10-25 19:26:31 -07001966}
1967
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001968static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
1969 [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1970 [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
1971 [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
1972 [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
1973 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1974 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
1975};
1976
stephen hemminger48e48a72014-07-16 11:25:52 -07001977static const struct genl_ops dp_vport_genl_ops[] = {
Jesse Grossccb13522011-10-25 19:26:31 -07001978 { .cmd = OVS_VPORT_CMD_NEW,
1979 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1980 .policy = vport_policy,
1981 .doit = ovs_vport_cmd_new
1982 },
1983 { .cmd = OVS_VPORT_CMD_DEL,
1984 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1985 .policy = vport_policy,
1986 .doit = ovs_vport_cmd_del
1987 },
1988 { .cmd = OVS_VPORT_CMD_GET,
1989 .flags = 0, /* OK for unprivileged users. */
1990 .policy = vport_policy,
1991 .doit = ovs_vport_cmd_get,
1992 .dumpit = ovs_vport_cmd_dump
1993 },
1994 { .cmd = OVS_VPORT_CMD_SET,
1995 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1996 .policy = vport_policy,
1997 .doit = ovs_vport_cmd_set,
1998 },
1999};
2000
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002001struct genl_family dp_vport_genl_family = {
2002 .id = GENL_ID_GENERATE,
2003 .hdrsize = sizeof(struct ovs_header),
2004 .name = OVS_VPORT_FAMILY,
2005 .version = OVS_VPORT_VERSION,
2006 .maxattr = OVS_VPORT_ATTR_MAX,
2007 .netnsok = true,
2008 .parallel_ops = true,
2009 .ops = dp_vport_genl_ops,
2010 .n_ops = ARRAY_SIZE(dp_vport_genl_ops),
2011 .mcgrps = &ovs_dp_vport_multicast_group,
2012 .n_mcgrps = 1,
Jesse Grossccb13522011-10-25 19:26:31 -07002013};
2014
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002015static struct genl_family * const dp_genl_families[] = {
2016 &dp_datapath_genl_family,
2017 &dp_vport_genl_family,
2018 &dp_flow_genl_family,
2019 &dp_packet_genl_family,
Jesse Grossccb13522011-10-25 19:26:31 -07002020};
2021
2022static void dp_unregister_genl(int n_families)
2023{
2024 int i;
2025
2026 for (i = 0; i < n_families; i++)
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002027 genl_unregister_family(dp_genl_families[i]);
Jesse Grossccb13522011-10-25 19:26:31 -07002028}
2029
2030static int dp_register_genl(void)
2031{
Jesse Grossccb13522011-10-25 19:26:31 -07002032 int err;
2033 int i;
2034
Jesse Grossccb13522011-10-25 19:26:31 -07002035 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
Jesse Grossccb13522011-10-25 19:26:31 -07002036
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002037 err = genl_register_family(dp_genl_families[i]);
Jesse Grossccb13522011-10-25 19:26:31 -07002038 if (err)
2039 goto error;
Jesse Grossccb13522011-10-25 19:26:31 -07002040 }
2041
2042 return 0;
2043
2044error:
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002045 dp_unregister_genl(i);
Jesse Grossccb13522011-10-25 19:26:31 -07002046 return err;
2047}
2048
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002049static int __net_init ovs_init_net(struct net *net)
2050{
2051 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2052
2053 INIT_LIST_HEAD(&ovs_net->dps);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002054 INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002055 return 0;
2056}
2057
2058static void __net_exit ovs_exit_net(struct net *net)
2059{
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002060 struct datapath *dp, *dp_next;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002061 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002062
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002063 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002064 list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
2065 __dp_destroy(dp);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002066 ovs_unlock();
2067
2068 cancel_work_sync(&ovs_net->dp_notify_work);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002069}
2070
2071static struct pernet_operations ovs_net_ops = {
2072 .init = ovs_init_net,
2073 .exit = ovs_exit_net,
2074 .id = &ovs_net_id,
2075 .size = sizeof(struct ovs_net),
2076};
2077
Jesse Grossccb13522011-10-25 19:26:31 -07002078static int __init dp_init(void)
2079{
Jesse Grossccb13522011-10-25 19:26:31 -07002080 int err;
2081
YOSHIFUJI Hideaki / 吉藤英明3523b292013-01-09 07:19:55 +00002082 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
Jesse Grossccb13522011-10-25 19:26:31 -07002083
2084 pr_info("Open vSwitch switching datapath\n");
2085
Andy Zhou971427f32014-09-15 19:37:25 -07002086 err = action_fifos_init();
Jesse Grossccb13522011-10-25 19:26:31 -07002087 if (err)
2088 goto error;
2089
Andy Zhou971427f32014-09-15 19:37:25 -07002090 err = ovs_internal_dev_rtnl_link_register();
2091 if (err)
2092 goto error_action_fifos_exit;
2093
Jiri Pirko5b9e7e12014-06-26 09:58:26 +02002094 err = ovs_flow_init();
2095 if (err)
2096 goto error_unreg_rtnl_link;
2097
Jesse Grossccb13522011-10-25 19:26:31 -07002098 err = ovs_vport_init();
2099 if (err)
2100 goto error_flow_exit;
2101
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002102 err = register_pernet_device(&ovs_net_ops);
Jesse Grossccb13522011-10-25 19:26:31 -07002103 if (err)
2104 goto error_vport_exit;
2105
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002106 err = register_netdevice_notifier(&ovs_dp_device_notifier);
2107 if (err)
2108 goto error_netns_exit;
2109
Jesse Grossccb13522011-10-25 19:26:31 -07002110 err = dp_register_genl();
2111 if (err < 0)
2112 goto error_unreg_notifier;
2113
Jesse Grossccb13522011-10-25 19:26:31 -07002114 return 0;
2115
2116error_unreg_notifier:
2117 unregister_netdevice_notifier(&ovs_dp_device_notifier);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002118error_netns_exit:
2119 unregister_pernet_device(&ovs_net_ops);
Jesse Grossccb13522011-10-25 19:26:31 -07002120error_vport_exit:
2121 ovs_vport_exit();
2122error_flow_exit:
2123 ovs_flow_exit();
Jiri Pirko5b9e7e12014-06-26 09:58:26 +02002124error_unreg_rtnl_link:
2125 ovs_internal_dev_rtnl_link_unregister();
Andy Zhou971427f32014-09-15 19:37:25 -07002126error_action_fifos_exit:
2127 action_fifos_exit();
Jesse Grossccb13522011-10-25 19:26:31 -07002128error:
2129 return err;
2130}
2131
2132static void dp_cleanup(void)
2133{
Jesse Grossccb13522011-10-25 19:26:31 -07002134 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
2135 unregister_netdevice_notifier(&ovs_dp_device_notifier);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002136 unregister_pernet_device(&ovs_net_ops);
2137 rcu_barrier();
Jesse Grossccb13522011-10-25 19:26:31 -07002138 ovs_vport_exit();
2139 ovs_flow_exit();
Jiri Pirko5b9e7e12014-06-26 09:58:26 +02002140 ovs_internal_dev_rtnl_link_unregister();
Andy Zhou971427f32014-09-15 19:37:25 -07002141 action_fifos_exit();
Jesse Grossccb13522011-10-25 19:26:31 -07002142}
2143
2144module_init(dp_init);
2145module_exit(dp_cleanup);
2146
2147MODULE_DESCRIPTION("Open vSwitch switching datapath");
2148MODULE_LICENSE("GPL");