blob: aecddb9bb80a3f951d830d4a4fa1c64831f6c3e2 [file] [log] [blame]
Jesse Grossccb13522011-10-25 19:26:31 -07001/*
Ben Pfaffad552002014-05-06 16:48:38 -07002 * Copyright (c) 2007-2014 Nicira, Inc.
Jesse Grossccb13522011-10-25 19:26:31 -07003 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of version 2 of the GNU General Public
6 * License as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 * 02110-1301, USA
17 */
18
19#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20
21#include <linux/init.h>
22#include <linux/module.h>
23#include <linux/if_arp.h>
24#include <linux/if_vlan.h>
25#include <linux/in.h>
26#include <linux/ip.h>
27#include <linux/jhash.h>
28#include <linux/delay.h>
29#include <linux/time.h>
30#include <linux/etherdevice.h>
31#include <linux/genetlink.h>
32#include <linux/kernel.h>
33#include <linux/kthread.h>
34#include <linux/mutex.h>
35#include <linux/percpu.h>
36#include <linux/rcupdate.h>
37#include <linux/tcp.h>
38#include <linux/udp.h>
Jesse Grossccb13522011-10-25 19:26:31 -070039#include <linux/ethtool.h>
40#include <linux/wait.h>
Jesse Grossccb13522011-10-25 19:26:31 -070041#include <asm/div64.h>
42#include <linux/highmem.h>
43#include <linux/netfilter_bridge.h>
44#include <linux/netfilter_ipv4.h>
45#include <linux/inetdevice.h>
46#include <linux/list.h>
47#include <linux/openvswitch.h>
48#include <linux/rculist.h>
49#include <linux/dmi.h>
Jesse Grossccb13522011-10-25 19:26:31 -070050#include <net/genetlink.h>
Pravin B Shelar46df7b82012-02-22 19:58:59 -080051#include <net/net_namespace.h>
52#include <net/netns/generic.h>
Jesse Grossccb13522011-10-25 19:26:31 -070053
54#include "datapath.h"
55#include "flow.h"
Andy Zhoue80857c2014-01-21 09:31:04 -080056#include "flow_table.h"
Pravin B Shelare6445712013-10-03 18:16:47 -070057#include "flow_netlink.h"
Jesse Grossccb13522011-10-25 19:26:31 -070058#include "vport-internal_dev.h"
Thomas Grafcff63a52013-04-29 13:06:41 +000059#include "vport-netdev.h"
Jesse Grossccb13522011-10-25 19:26:31 -070060
Pravin B Shelar8e4e1712013-04-15 13:23:03 -070061int ovs_net_id __read_mostly;
Thomas Graf62b9c8d2014-10-22 17:29:06 +020062EXPORT_SYMBOL(ovs_net_id);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -070063
Pravin B Shelar0c200ef2014-05-06 16:44:50 -070064static struct genl_family dp_packet_genl_family;
65static struct genl_family dp_flow_genl_family;
66static struct genl_family dp_datapath_genl_family;
67
stephen hemminger48e48a72014-07-16 11:25:52 -070068static const struct genl_multicast_group ovs_dp_flow_multicast_group = {
69 .name = OVS_FLOW_MCGROUP,
Pravin B Shelar0c200ef2014-05-06 16:44:50 -070070};
71
stephen hemminger48e48a72014-07-16 11:25:52 -070072static const struct genl_multicast_group ovs_dp_datapath_multicast_group = {
73 .name = OVS_DATAPATH_MCGROUP,
Pravin B Shelar0c200ef2014-05-06 16:44:50 -070074};
75
stephen hemminger48e48a72014-07-16 11:25:52 -070076static const struct genl_multicast_group ovs_dp_vport_multicast_group = {
77 .name = OVS_VPORT_MCGROUP,
Pravin B Shelar0c200ef2014-05-06 16:44:50 -070078};
79
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -070080/* Check if need to build a reply message.
81 * OVS userspace sets the NLM_F_ECHO flag if it needs the reply. */
Samuel Gauthier9b67aa42014-09-18 10:31:04 +020082static bool ovs_must_notify(struct genl_family *family, struct genl_info *info,
83 unsigned int group)
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -070084{
85 return info->nlhdr->nlmsg_flags & NLM_F_ECHO ||
Samuel Gauthier9b67aa42014-09-18 10:31:04 +020086 genl_has_listeners(family, genl_info_net(info)->genl_sock,
87 group);
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -070088}
89
Johannes Berg68eb5502013-11-19 15:19:38 +010090static void ovs_notify(struct genl_family *family,
Johannes Berg2a94fe42013-11-19 15:19:39 +010091 struct sk_buff *skb, struct genl_info *info)
Thomas Grafed6611852013-03-29 14:46:50 +010092{
Johannes Berg68eb5502013-11-19 15:19:38 +010093 genl_notify(family, skb, genl_info_net(info), info->snd_portid,
Johannes Berg2a94fe42013-11-19 15:19:39 +010094 0, info->nlhdr, GFP_KERNEL);
Thomas Grafed6611852013-03-29 14:46:50 +010095}
96
Pravin B Shelar46df7b82012-02-22 19:58:59 -080097/**
Jesse Grossccb13522011-10-25 19:26:31 -070098 * DOC: Locking:
99 *
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700100 * All writes e.g. Writes to device state (add/remove datapath, port, set
101 * operations on vports, etc.), Writes to other state (flow table
102 * modifications, set miscellaneous datapath parameters, etc.) are protected
103 * by ovs_lock.
Jesse Grossccb13522011-10-25 19:26:31 -0700104 *
105 * Reads are protected by RCU.
106 *
107 * There are a few special cases (mostly stats) that have their own
108 * synchronization but they nest under all of above and don't interact with
109 * each other.
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700110 *
111 * The RTNL lock nests inside ovs_mutex.
Jesse Grossccb13522011-10-25 19:26:31 -0700112 */
113
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700114static DEFINE_MUTEX(ovs_mutex);
115
116void ovs_lock(void)
117{
118 mutex_lock(&ovs_mutex);
119}
120
121void ovs_unlock(void)
122{
123 mutex_unlock(&ovs_mutex);
124}
125
126#ifdef CONFIG_LOCKDEP
127int lockdep_ovsl_is_held(void)
128{
129 if (debug_locks)
130 return lockdep_is_held(&ovs_mutex);
131 else
132 return 1;
133}
134#endif
135
Jesse Grossccb13522011-10-25 19:26:31 -0700136static struct vport *new_vport(const struct vport_parms *);
Thomas Graf8055a892013-12-13 15:22:20 +0100137static int queue_gso_packets(struct datapath *dp, struct sk_buff *,
Jesse Grossccb13522011-10-25 19:26:31 -0700138 const struct dp_upcall_info *);
Thomas Graf8055a892013-12-13 15:22:20 +0100139static int queue_userspace_packet(struct datapath *dp, struct sk_buff *,
Jesse Grossccb13522011-10-25 19:26:31 -0700140 const struct dp_upcall_info *);
141
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700142/* Must be called with rcu_read_lock or ovs_mutex. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800143static struct datapath *get_dp(struct net *net, int dp_ifindex)
Jesse Grossccb13522011-10-25 19:26:31 -0700144{
145 struct datapath *dp = NULL;
146 struct net_device *dev;
147
148 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800149 dev = dev_get_by_index_rcu(net, dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -0700150 if (dev) {
151 struct vport *vport = ovs_internal_dev_get_vport(dev);
152 if (vport)
153 dp = vport->dp;
154 }
155 rcu_read_unlock();
156
157 return dp;
158}
159
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700160/* Must be called with rcu_read_lock or ovs_mutex. */
Andy Zhou971427f32014-09-15 19:37:25 -0700161const char *ovs_dp_name(const struct datapath *dp)
Jesse Grossccb13522011-10-25 19:26:31 -0700162{
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700163 struct vport *vport = ovs_vport_ovsl_rcu(dp, OVSP_LOCAL);
Jesse Grossccb13522011-10-25 19:26:31 -0700164 return vport->ops->get_name(vport);
165}
166
167static int get_dpifindex(struct datapath *dp)
168{
169 struct vport *local;
170 int ifindex;
171
172 rcu_read_lock();
173
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700174 local = ovs_vport_rcu(dp, OVSP_LOCAL);
Jesse Grossccb13522011-10-25 19:26:31 -0700175 if (local)
Thomas Grafcff63a52013-04-29 13:06:41 +0000176 ifindex = netdev_vport_priv(local)->dev->ifindex;
Jesse Grossccb13522011-10-25 19:26:31 -0700177 else
178 ifindex = 0;
179
180 rcu_read_unlock();
181
182 return ifindex;
183}
184
185static void destroy_dp_rcu(struct rcu_head *rcu)
186{
187 struct datapath *dp = container_of(rcu, struct datapath, rcu);
188
Jesse Grossccb13522011-10-25 19:26:31 -0700189 free_percpu(dp->stats_percpu);
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800190 release_net(ovs_dp_get_net(dp));
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700191 kfree(dp->ports);
Jesse Grossccb13522011-10-25 19:26:31 -0700192 kfree(dp);
193}
194
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700195static struct hlist_head *vport_hash_bucket(const struct datapath *dp,
196 u16 port_no)
197{
198 return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)];
199}
200
Jarno Rajahalmebb6f9a72014-05-05 11:32:17 -0700201/* Called with ovs_mutex or RCU read lock. */
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700202struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no)
203{
204 struct vport *vport;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700205 struct hlist_head *head;
206
207 head = vport_hash_bucket(dp, port_no);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800208 hlist_for_each_entry_rcu(vport, head, dp_hash_node) {
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700209 if (vport->port_no == port_no)
210 return vport;
211 }
212 return NULL;
213}
214
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700215/* Called with ovs_mutex. */
Jesse Grossccb13522011-10-25 19:26:31 -0700216static struct vport *new_vport(const struct vport_parms *parms)
217{
218 struct vport *vport;
219
220 vport = ovs_vport_add(parms);
221 if (!IS_ERR(vport)) {
222 struct datapath *dp = parms->dp;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700223 struct hlist_head *head = vport_hash_bucket(dp, vport->port_no);
Jesse Grossccb13522011-10-25 19:26:31 -0700224
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700225 hlist_add_head_rcu(&vport->dp_hash_node, head);
Jesse Grossccb13522011-10-25 19:26:31 -0700226 }
Jesse Grossccb13522011-10-25 19:26:31 -0700227 return vport;
228}
229
Jesse Grossccb13522011-10-25 19:26:31 -0700230void ovs_dp_detach_port(struct vport *p)
231{
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700232 ASSERT_OVSL();
Jesse Grossccb13522011-10-25 19:26:31 -0700233
234 /* First drop references to device. */
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700235 hlist_del_rcu(&p->dp_hash_node);
Jesse Grossccb13522011-10-25 19:26:31 -0700236
237 /* Then destroy it. */
238 ovs_vport_del(p);
239}
240
241/* Must be called with rcu_read_lock. */
Pravin B Shelar8c8b1b82014-09-15 19:28:44 -0700242void ovs_dp_process_packet(struct sk_buff *skb, struct sw_flow_key *key)
Jesse Grossccb13522011-10-25 19:26:31 -0700243{
Pravin B Shelar83c8df22014-09-15 19:20:31 -0700244 const struct vport *p = OVS_CB(skb)->input_vport;
Jesse Grossccb13522011-10-25 19:26:31 -0700245 struct datapath *dp = p->dp;
246 struct sw_flow *flow;
247 struct dp_stats_percpu *stats;
Jesse Grossccb13522011-10-25 19:26:31 -0700248 u64 *stats_counter;
Andy Zhou1bd71162013-10-22 10:42:46 -0700249 u32 n_mask_hit;
Jesse Grossccb13522011-10-25 19:26:31 -0700250
Shan Wei404f2f12012-11-13 09:52:25 +0800251 stats = this_cpu_ptr(dp->stats_percpu);
Jesse Grossccb13522011-10-25 19:26:31 -0700252
Jesse Grossccb13522011-10-25 19:26:31 -0700253 /* Look up flow. */
Pravin B Shelar8c8b1b82014-09-15 19:28:44 -0700254 flow = ovs_flow_tbl_lookup_stats(&dp->table, key, &n_mask_hit);
Jesse Grossccb13522011-10-25 19:26:31 -0700255 if (unlikely(!flow)) {
256 struct dp_upcall_info upcall;
Pravin B Shelar8c8b1b82014-09-15 19:28:44 -0700257 int error;
Jesse Grossccb13522011-10-25 19:26:31 -0700258
259 upcall.cmd = OVS_PACKET_CMD_MISS;
Pravin B Shelar8c8b1b82014-09-15 19:28:44 -0700260 upcall.key = key;
Jesse Grossccb13522011-10-25 19:26:31 -0700261 upcall.userdata = NULL;
Alex Wang5cd667b2014-07-17 15:14:13 -0700262 upcall.portid = ovs_vport_find_upcall_portid(p, skb);
Li RongQingc5eba0b2014-09-03 17:43:45 +0800263 error = ovs_dp_upcall(dp, skb, &upcall);
264 if (unlikely(error))
265 kfree_skb(skb);
266 else
267 consume_skb(skb);
Jesse Grossccb13522011-10-25 19:26:31 -0700268 stats_counter = &stats->n_missed;
269 goto out;
270 }
271
272 OVS_CB(skb)->flow = flow;
273
Pravin B Shelar8c8b1b82014-09-15 19:28:44 -0700274 ovs_flow_stats_update(OVS_CB(skb)->flow, key->tp.flags, skb);
275 ovs_execute_actions(dp, skb, key);
Pravin B Shelare298e502013-10-29 17:22:21 -0700276 stats_counter = &stats->n_hit;
Jesse Grossccb13522011-10-25 19:26:31 -0700277
278out:
279 /* Update datapath statistics. */
WANG Congdf9d9fd2014-02-14 15:10:46 -0800280 u64_stats_update_begin(&stats->syncp);
Jesse Grossccb13522011-10-25 19:26:31 -0700281 (*stats_counter)++;
Andy Zhou1bd71162013-10-22 10:42:46 -0700282 stats->n_mask_hit += n_mask_hit;
WANG Congdf9d9fd2014-02-14 15:10:46 -0800283 u64_stats_update_end(&stats->syncp);
Jesse Grossccb13522011-10-25 19:26:31 -0700284}
285
Jesse Grossccb13522011-10-25 19:26:31 -0700286int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800287 const struct dp_upcall_info *upcall_info)
Jesse Grossccb13522011-10-25 19:26:31 -0700288{
289 struct dp_stats_percpu *stats;
Jesse Grossccb13522011-10-25 19:26:31 -0700290 int err;
291
Eric W. Biederman15e47302012-09-07 20:12:54 +0000292 if (upcall_info->portid == 0) {
Jesse Grossccb13522011-10-25 19:26:31 -0700293 err = -ENOTCONN;
294 goto err;
295 }
296
Jesse Grossccb13522011-10-25 19:26:31 -0700297 if (!skb_is_gso(skb))
Thomas Graf8055a892013-12-13 15:22:20 +0100298 err = queue_userspace_packet(dp, skb, upcall_info);
Jesse Grossccb13522011-10-25 19:26:31 -0700299 else
Thomas Graf8055a892013-12-13 15:22:20 +0100300 err = queue_gso_packets(dp, skb, upcall_info);
Jesse Grossccb13522011-10-25 19:26:31 -0700301 if (err)
302 goto err;
303
304 return 0;
305
306err:
Shan Wei404f2f12012-11-13 09:52:25 +0800307 stats = this_cpu_ptr(dp->stats_percpu);
Jesse Grossccb13522011-10-25 19:26:31 -0700308
WANG Congdf9d9fd2014-02-14 15:10:46 -0800309 u64_stats_update_begin(&stats->syncp);
Jesse Grossccb13522011-10-25 19:26:31 -0700310 stats->n_lost++;
WANG Congdf9d9fd2014-02-14 15:10:46 -0800311 u64_stats_update_end(&stats->syncp);
Jesse Grossccb13522011-10-25 19:26:31 -0700312
313 return err;
314}
315
Thomas Graf8055a892013-12-13 15:22:20 +0100316static int queue_gso_packets(struct datapath *dp, struct sk_buff *skb,
Jesse Grossccb13522011-10-25 19:26:31 -0700317 const struct dp_upcall_info *upcall_info)
318{
Ben Pfaffa1b5d0d2012-07-20 14:47:54 -0700319 unsigned short gso_type = skb_shinfo(skb)->gso_type;
Jesse Grossccb13522011-10-25 19:26:31 -0700320 struct dp_upcall_info later_info;
321 struct sw_flow_key later_key;
322 struct sk_buff *segs, *nskb;
323 int err;
324
Thomas Graf09c5e602013-12-13 15:22:22 +0100325 segs = __skb_gso_segment(skb, NETIF_F_SG, false);
Pravin B Shelar92e5dfc2012-07-20 14:46:29 -0700326 if (IS_ERR(segs))
327 return PTR_ERR(segs);
Florian Westphal330966e2014-10-20 13:49:17 +0200328 if (segs == NULL)
329 return -EINVAL;
Jesse Grossccb13522011-10-25 19:26:31 -0700330
331 /* Queue all of the segments. */
332 skb = segs;
333 do {
Thomas Graf8055a892013-12-13 15:22:20 +0100334 err = queue_userspace_packet(dp, skb, upcall_info);
Jesse Grossccb13522011-10-25 19:26:31 -0700335 if (err)
336 break;
337
Ben Pfaffa1b5d0d2012-07-20 14:47:54 -0700338 if (skb == segs && gso_type & SKB_GSO_UDP) {
Jesse Grossccb13522011-10-25 19:26:31 -0700339 /* The initial flow key extracted by ovs_flow_extract()
340 * in this case is for a first fragment, so we need to
341 * properly mark later fragments.
342 */
343 later_key = *upcall_info->key;
344 later_key.ip.frag = OVS_FRAG_TYPE_LATER;
345
346 later_info = *upcall_info;
347 later_info.key = &later_key;
348 upcall_info = &later_info;
349 }
350 } while ((skb = skb->next));
351
352 /* Free all of the segments. */
353 skb = segs;
354 do {
355 nskb = skb->next;
356 if (err)
357 kfree_skb(skb);
358 else
359 consume_skb(skb);
360 } while ((skb = nskb));
361 return err;
362}
363
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100364static size_t key_attr_size(void)
365{
366 return nla_total_size(4) /* OVS_KEY_ATTR_PRIORITY */
Pravin B Shelar7d5437c2013-06-17 17:50:18 -0700367 + nla_total_size(0) /* OVS_KEY_ATTR_TUNNEL */
368 + nla_total_size(8) /* OVS_TUNNEL_KEY_ATTR_ID */
369 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_SRC */
370 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_DST */
371 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TOS */
372 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TTL */
373 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT */
374 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_CSUM */
Jesse Gross67fa0342014-10-03 15:35:30 -0700375 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_OAM */
Jesse Grossf5796682014-10-03 15:35:33 -0700376 + nla_total_size(256) /* OVS_TUNNEL_KEY_ATTR_GENEVE_OPTS */
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100377 + nla_total_size(4) /* OVS_KEY_ATTR_IN_PORT */
378 + nla_total_size(4) /* OVS_KEY_ATTR_SKB_MARK */
379 + nla_total_size(12) /* OVS_KEY_ATTR_ETHERNET */
380 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
381 + nla_total_size(4) /* OVS_KEY_ATTR_8021Q */
382 + nla_total_size(0) /* OVS_KEY_ATTR_ENCAP */
383 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
384 + nla_total_size(40) /* OVS_KEY_ATTR_IPV6 */
385 + nla_total_size(2) /* OVS_KEY_ATTR_ICMPV6 */
386 + nla_total_size(28); /* OVS_KEY_ATTR_ND */
387}
388
Thomas Grafbda56f12013-12-13 15:22:21 +0100389static size_t upcall_msg_size(const struct nlattr *userdata,
390 unsigned int hdrlen)
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100391{
392 size_t size = NLMSG_ALIGN(sizeof(struct ovs_header))
Thomas Grafbda56f12013-12-13 15:22:21 +0100393 + nla_total_size(hdrlen) /* OVS_PACKET_ATTR_PACKET */
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100394 + nla_total_size(key_attr_size()); /* OVS_PACKET_ATTR_KEY */
395
396 /* OVS_PACKET_ATTR_USERDATA */
397 if (userdata)
398 size += NLA_ALIGN(userdata->nla_len);
399
400 return size;
401}
402
Thomas Graf8055a892013-12-13 15:22:20 +0100403static int queue_userspace_packet(struct datapath *dp, struct sk_buff *skb,
Jesse Grossccb13522011-10-25 19:26:31 -0700404 const struct dp_upcall_info *upcall_info)
405{
406 struct ovs_header *upcall;
407 struct sk_buff *nskb = NULL;
Li RongQing4ee45ea2014-09-02 20:52:28 +0800408 struct sk_buff *user_skb = NULL; /* to be queued to userspace */
Jesse Grossccb13522011-10-25 19:26:31 -0700409 struct nlattr *nla;
Thomas Graf795449d2013-11-30 13:21:32 +0100410 struct genl_info info = {
Thomas Graf8055a892013-12-13 15:22:20 +0100411 .dst_sk = ovs_dp_get_net(dp)->genl_sock,
Thomas Graf795449d2013-11-30 13:21:32 +0100412 .snd_portid = upcall_info->portid,
413 };
414 size_t len;
Thomas Grafbda56f12013-12-13 15:22:21 +0100415 unsigned int hlen;
Thomas Graf8055a892013-12-13 15:22:20 +0100416 int err, dp_ifindex;
417
418 dp_ifindex = get_dpifindex(dp);
419 if (!dp_ifindex)
420 return -ENODEV;
Jesse Grossccb13522011-10-25 19:26:31 -0700421
422 if (vlan_tx_tag_present(skb)) {
423 nskb = skb_clone(skb, GFP_ATOMIC);
424 if (!nskb)
425 return -ENOMEM;
426
Patrick McHardy86a9bad2013-04-19 02:04:30 +0000427 nskb = __vlan_put_tag(nskb, nskb->vlan_proto, vlan_tx_tag_get(nskb));
Dan Carpenter8aa51d62012-05-13 08:44:18 +0000428 if (!nskb)
Jesse Grossccb13522011-10-25 19:26:31 -0700429 return -ENOMEM;
430
431 nskb->vlan_tci = 0;
432 skb = nskb;
433 }
434
435 if (nla_attr_size(skb->len) > USHRT_MAX) {
436 err = -EFBIG;
437 goto out;
438 }
439
Thomas Grafbda56f12013-12-13 15:22:21 +0100440 /* Complete checksum if needed */
441 if (skb->ip_summed == CHECKSUM_PARTIAL &&
442 (err = skb_checksum_help(skb)))
443 goto out;
444
445 /* Older versions of OVS user space enforce alignment of the last
446 * Netlink attribute to NLA_ALIGNTO which would require extensive
447 * padding logic. Only perform zerocopy if padding is not required.
448 */
449 if (dp->user_features & OVS_DP_F_UNALIGNED)
450 hlen = skb_zerocopy_headlen(skb);
451 else
452 hlen = skb->len;
453
454 len = upcall_msg_size(upcall_info->userdata, hlen);
Thomas Graf795449d2013-11-30 13:21:32 +0100455 user_skb = genlmsg_new_unicast(len, &info, GFP_ATOMIC);
Jesse Grossccb13522011-10-25 19:26:31 -0700456 if (!user_skb) {
457 err = -ENOMEM;
458 goto out;
459 }
460
461 upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
462 0, upcall_info->cmd);
463 upcall->dp_ifindex = dp_ifindex;
464
465 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
Andy Zhouf53e3832014-07-17 15:17:44 -0700466 err = ovs_nla_put_flow(upcall_info->key, upcall_info->key, user_skb);
467 BUG_ON(err);
Jesse Grossccb13522011-10-25 19:26:31 -0700468 nla_nest_end(user_skb, nla);
469
470 if (upcall_info->userdata)
Ben Pfaff44901082013-02-15 17:29:22 -0800471 __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA,
472 nla_len(upcall_info->userdata),
473 nla_data(upcall_info->userdata));
Jesse Grossccb13522011-10-25 19:26:31 -0700474
Thomas Grafbda56f12013-12-13 15:22:21 +0100475 /* Only reserve room for attribute header, packet data is added
476 * in skb_zerocopy() */
477 if (!(nla = nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, 0))) {
478 err = -ENOBUFS;
479 goto out;
480 }
481 nla->nla_len = nla_attr_size(skb->len);
Jesse Grossccb13522011-10-25 19:26:31 -0700482
Zoltan Kiss36d5fe62014-03-26 22:37:45 +0000483 err = skb_zerocopy(user_skb, skb, skb->len, hlen);
484 if (err)
485 goto out;
Jesse Grossccb13522011-10-25 19:26:31 -0700486
Thomas Grafaea0bb42014-01-14 16:27:49 +0000487 /* Pad OVS_PACKET_ATTR_PACKET if linear copy was performed */
488 if (!(dp->user_features & OVS_DP_F_UNALIGNED)) {
489 size_t plen = NLA_ALIGN(user_skb->len) - user_skb->len;
490
491 if (plen > 0)
492 memset(skb_put(user_skb, plen), 0, plen);
493 }
494
Thomas Grafbda56f12013-12-13 15:22:21 +0100495 ((struct nlmsghdr *) user_skb->data)->nlmsg_len = user_skb->len;
496
Thomas Graf8055a892013-12-13 15:22:20 +0100497 err = genlmsg_unicast(ovs_dp_get_net(dp), user_skb, upcall_info->portid);
Li RongQing4ee45ea2014-09-02 20:52:28 +0800498 user_skb = NULL;
Jesse Grossccb13522011-10-25 19:26:31 -0700499out:
Zoltan Kiss36d5fe62014-03-26 22:37:45 +0000500 if (err)
501 skb_tx_error(skb);
Li RongQing4ee45ea2014-09-02 20:52:28 +0800502 kfree_skb(user_skb);
Jesse Grossccb13522011-10-25 19:26:31 -0700503 kfree_skb(nskb);
504 return err;
505}
506
Jesse Grossccb13522011-10-25 19:26:31 -0700507static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
508{
509 struct ovs_header *ovs_header = info->userhdr;
510 struct nlattr **a = info->attrs;
511 struct sw_flow_actions *acts;
512 struct sk_buff *packet;
513 struct sw_flow *flow;
514 struct datapath *dp;
515 struct ethhdr *eth;
Pravin B Shelar83c8df22014-09-15 19:20:31 -0700516 struct vport *input_vport;
Jesse Grossccb13522011-10-25 19:26:31 -0700517 int len;
518 int err;
Jesse Grossccb13522011-10-25 19:26:31 -0700519
520 err = -EINVAL;
521 if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
Thomas Grafdded45fc2013-03-29 14:46:47 +0100522 !a[OVS_PACKET_ATTR_ACTIONS])
Jesse Grossccb13522011-10-25 19:26:31 -0700523 goto err;
524
525 len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
526 packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
527 err = -ENOMEM;
528 if (!packet)
529 goto err;
530 skb_reserve(packet, NET_IP_ALIGN);
531
Thomas Graf32686a92013-03-29 14:46:48 +0100532 nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
Jesse Grossccb13522011-10-25 19:26:31 -0700533
534 skb_reset_mac_header(packet);
535 eth = eth_hdr(packet);
536
537 /* Normally, setting the skb 'protocol' field would be handled by a
538 * call to eth_type_trans(), but it assumes there's a sending
539 * device, which we may not have. */
Simon Hormane5c5d222013-03-28 13:38:25 +0900540 if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN)
Jesse Grossccb13522011-10-25 19:26:31 -0700541 packet->protocol = eth->h_proto;
542 else
543 packet->protocol = htons(ETH_P_802_2);
544
545 /* Build an sw_flow for sending this packet. */
Jarno Rajahalme23dabf82014-03-27 12:35:23 -0700546 flow = ovs_flow_alloc();
Jesse Grossccb13522011-10-25 19:26:31 -0700547 err = PTR_ERR(flow);
548 if (IS_ERR(flow))
549 goto err_kfree_skb;
550
Pravin B Shelar83c8df22014-09-15 19:20:31 -0700551 err = ovs_flow_key_extract_userspace(a[OVS_PACKET_ATTR_KEY], packet,
552 &flow->key);
Jesse Grossccb13522011-10-25 19:26:31 -0700553 if (err)
554 goto err_flow_free;
555
Pravin B Shelare6445712013-10-03 18:16:47 -0700556 acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_PACKET_ATTR_ACTIONS]));
Jesse Grossccb13522011-10-25 19:26:31 -0700557 err = PTR_ERR(acts);
558 if (IS_ERR(acts))
559 goto err_flow_free;
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700560
Pravin B Shelare6445712013-10-03 18:16:47 -0700561 err = ovs_nla_copy_actions(a[OVS_PACKET_ATTR_ACTIONS],
562 &flow->key, 0, &acts);
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700563 if (err)
564 goto err_flow_free;
Jesse Grossccb13522011-10-25 19:26:31 -0700565
Jesse Grossf5796682014-10-03 15:35:33 -0700566 rcu_assign_pointer(flow->sf_acts, acts);
567
568 OVS_CB(packet)->egress_tun_info = NULL;
Jesse Grossccb13522011-10-25 19:26:31 -0700569 OVS_CB(packet)->flow = flow;
570 packet->priority = flow->key.phy.priority;
Ansis Atteka39c7caeb2012-11-26 11:24:11 -0800571 packet->mark = flow->key.phy.skb_mark;
Jesse Grossccb13522011-10-25 19:26:31 -0700572
573 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800574 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -0700575 err = -ENODEV;
576 if (!dp)
577 goto err_unlock;
578
Pravin B Shelar83c8df22014-09-15 19:20:31 -0700579 input_vport = ovs_vport_rcu(dp, flow->key.phy.in_port);
580 if (!input_vport)
581 input_vport = ovs_vport_rcu(dp, OVSP_LOCAL);
582
583 if (!input_vport)
584 goto err_unlock;
585
586 OVS_CB(packet)->input_vport = input_vport;
587
Jesse Grossccb13522011-10-25 19:26:31 -0700588 local_bh_disable();
Pravin B Shelar2ff3e4e2014-09-15 19:15:28 -0700589 err = ovs_execute_actions(dp, packet, &flow->key);
Jesse Grossccb13522011-10-25 19:26:31 -0700590 local_bh_enable();
591 rcu_read_unlock();
592
Andy Zhou03f0d912013-08-07 20:01:00 -0700593 ovs_flow_free(flow, false);
Jesse Grossccb13522011-10-25 19:26:31 -0700594 return err;
595
596err_unlock:
597 rcu_read_unlock();
598err_flow_free:
Andy Zhou03f0d912013-08-07 20:01:00 -0700599 ovs_flow_free(flow, false);
Jesse Grossccb13522011-10-25 19:26:31 -0700600err_kfree_skb:
601 kfree_skb(packet);
602err:
603 return err;
604}
605
606static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
Thomas Grafdded45fc2013-03-29 14:46:47 +0100607 [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
Jesse Grossccb13522011-10-25 19:26:31 -0700608 [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
609 [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
610};
611
Johannes Berg4534de82013-11-14 17:14:46 +0100612static const struct genl_ops dp_packet_genl_ops[] = {
Jesse Grossccb13522011-10-25 19:26:31 -0700613 { .cmd = OVS_PACKET_CMD_EXECUTE,
614 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
615 .policy = packet_policy,
616 .doit = ovs_packet_cmd_execute
617 }
618};
619
Pravin B Shelar0c200ef2014-05-06 16:44:50 -0700620static struct genl_family dp_packet_genl_family = {
621 .id = GENL_ID_GENERATE,
622 .hdrsize = sizeof(struct ovs_header),
623 .name = OVS_PACKET_FAMILY,
624 .version = OVS_PACKET_VERSION,
625 .maxattr = OVS_PACKET_ATTR_MAX,
626 .netnsok = true,
627 .parallel_ops = true,
628 .ops = dp_packet_genl_ops,
629 .n_ops = ARRAY_SIZE(dp_packet_genl_ops),
630};
631
Andy Zhou1bd71162013-10-22 10:42:46 -0700632static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats,
633 struct ovs_dp_megaflow_stats *mega_stats)
Jesse Grossccb13522011-10-25 19:26:31 -0700634{
635 int i;
Jesse Grossccb13522011-10-25 19:26:31 -0700636
Andy Zhou1bd71162013-10-22 10:42:46 -0700637 memset(mega_stats, 0, sizeof(*mega_stats));
638
Pravin B Shelarb637e492013-10-04 00:14:23 -0700639 stats->n_flows = ovs_flow_tbl_count(&dp->table);
Andy Zhou1bd71162013-10-22 10:42:46 -0700640 mega_stats->n_masks = ovs_flow_tbl_num_masks(&dp->table);
Jesse Grossccb13522011-10-25 19:26:31 -0700641
642 stats->n_hit = stats->n_missed = stats->n_lost = 0;
Andy Zhou1bd71162013-10-22 10:42:46 -0700643
Jesse Grossccb13522011-10-25 19:26:31 -0700644 for_each_possible_cpu(i) {
645 const struct dp_stats_percpu *percpu_stats;
646 struct dp_stats_percpu local_stats;
647 unsigned int start;
648
649 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
650
651 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700652 start = u64_stats_fetch_begin_irq(&percpu_stats->syncp);
Jesse Grossccb13522011-10-25 19:26:31 -0700653 local_stats = *percpu_stats;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700654 } while (u64_stats_fetch_retry_irq(&percpu_stats->syncp, start));
Jesse Grossccb13522011-10-25 19:26:31 -0700655
656 stats->n_hit += local_stats.n_hit;
657 stats->n_missed += local_stats.n_missed;
658 stats->n_lost += local_stats.n_lost;
Andy Zhou1bd71162013-10-22 10:42:46 -0700659 mega_stats->n_mask_hit += local_stats.n_mask_hit;
Jesse Grossccb13522011-10-25 19:26:31 -0700660 }
661}
662
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100663static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts)
664{
665 return NLMSG_ALIGN(sizeof(struct ovs_header))
666 + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_KEY */
Andy Zhou03f0d912013-08-07 20:01:00 -0700667 + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_MASK */
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100668 + nla_total_size(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
669 + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
670 + nla_total_size(8) /* OVS_FLOW_ATTR_USED */
671 + nla_total_size(acts->actions_len); /* OVS_FLOW_ATTR_ACTIONS */
672}
673
Jarno Rajahalmebb6f9a72014-05-05 11:32:17 -0700674/* Called with ovs_mutex or RCU read lock. */
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700675static int ovs_flow_cmd_fill_info(const struct sw_flow *flow, int dp_ifindex,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000676 struct sk_buff *skb, u32 portid,
Jesse Grossccb13522011-10-25 19:26:31 -0700677 u32 seq, u32 flags, u8 cmd)
678{
679 const int skb_orig_len = skb->len;
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700680 struct nlattr *start;
Jesse Grossccb13522011-10-25 19:26:31 -0700681 struct ovs_flow_stats stats;
Pravin B Shelare298e502013-10-29 17:22:21 -0700682 __be16 tcp_flags;
683 unsigned long used;
Jesse Grossccb13522011-10-25 19:26:31 -0700684 struct ovs_header *ovs_header;
685 struct nlattr *nla;
Jesse Grossccb13522011-10-25 19:26:31 -0700686 int err;
687
Eric W. Biederman15e47302012-09-07 20:12:54 +0000688 ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family, flags, cmd);
Jesse Grossccb13522011-10-25 19:26:31 -0700689 if (!ovs_header)
690 return -EMSGSIZE;
691
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700692 ovs_header->dp_ifindex = dp_ifindex;
Jesse Grossccb13522011-10-25 19:26:31 -0700693
Andy Zhou03f0d912013-08-07 20:01:00 -0700694 /* Fill flow key. */
Jesse Grossccb13522011-10-25 19:26:31 -0700695 nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
696 if (!nla)
697 goto nla_put_failure;
Andy Zhou03f0d912013-08-07 20:01:00 -0700698
Pravin B Shelare6445712013-10-03 18:16:47 -0700699 err = ovs_nla_put_flow(&flow->unmasked_key, &flow->unmasked_key, skb);
Jesse Grossccb13522011-10-25 19:26:31 -0700700 if (err)
701 goto error;
702 nla_nest_end(skb, nla);
703
Andy Zhou03f0d912013-08-07 20:01:00 -0700704 nla = nla_nest_start(skb, OVS_FLOW_ATTR_MASK);
705 if (!nla)
706 goto nla_put_failure;
707
Pravin B Shelare6445712013-10-03 18:16:47 -0700708 err = ovs_nla_put_flow(&flow->key, &flow->mask->key, skb);
Andy Zhou03f0d912013-08-07 20:01:00 -0700709 if (err)
710 goto error;
711
712 nla_nest_end(skb, nla);
713
Pravin B Shelare298e502013-10-29 17:22:21 -0700714 ovs_flow_stats_get(flow, &stats, &used, &tcp_flags);
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700715
David S. Miller028d6a62012-03-29 23:20:48 -0400716 if (used &&
717 nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
718 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -0700719
David S. Miller028d6a62012-03-29 23:20:48 -0400720 if (stats.n_packets &&
Pravin B Shelare298e502013-10-29 17:22:21 -0700721 nla_put(skb, OVS_FLOW_ATTR_STATS, sizeof(struct ovs_flow_stats), &stats))
David S. Miller028d6a62012-03-29 23:20:48 -0400722 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -0700723
Pravin B Shelare298e502013-10-29 17:22:21 -0700724 if ((u8)ntohs(tcp_flags) &&
725 nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, (u8)ntohs(tcp_flags)))
David S. Miller028d6a62012-03-29 23:20:48 -0400726 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -0700727
728 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
729 * this is the first flow to be dumped into 'skb'. This is unusual for
730 * Netlink but individual action lists can be longer than
731 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
732 * The userspace caller can always fetch the actions separately if it
733 * really wants them. (Most userspace callers in fact don't care.)
734 *
735 * This can only fail for dump operations because the skb is always
736 * properly sized for single flows.
737 */
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700738 start = nla_nest_start(skb, OVS_FLOW_ATTR_ACTIONS);
739 if (start) {
Pravin B Shelard57170b2013-07-30 15:39:39 -0700740 const struct sw_flow_actions *sf_acts;
741
Jesse Gross663efa32013-12-03 10:58:53 -0800742 sf_acts = rcu_dereference_ovsl(flow->sf_acts);
Pravin B Shelare6445712013-10-03 18:16:47 -0700743 err = ovs_nla_put_actions(sf_acts->actions,
744 sf_acts->actions_len, skb);
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700745
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700746 if (!err)
747 nla_nest_end(skb, start);
748 else {
749 if (skb_orig_len)
750 goto error;
751
752 nla_nest_cancel(skb, start);
753 }
754 } else if (skb_orig_len)
755 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -0700756
757 return genlmsg_end(skb, ovs_header);
758
759nla_put_failure:
760 err = -EMSGSIZE;
761error:
762 genlmsg_cancel(skb, ovs_header);
763 return err;
764}
765
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700766/* May not be called with RCU read lock. */
767static struct sk_buff *ovs_flow_cmd_alloc_info(const struct sw_flow_actions *acts,
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700768 struct genl_info *info,
769 bool always)
Jesse Grossccb13522011-10-25 19:26:31 -0700770{
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700771 struct sk_buff *skb;
Jesse Grossccb13522011-10-25 19:26:31 -0700772
Samuel Gauthier9b67aa42014-09-18 10:31:04 +0200773 if (!always && !ovs_must_notify(&dp_flow_genl_family, info, 0))
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700774 return NULL;
775
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700776 skb = genlmsg_new_unicast(ovs_flow_cmd_msg_size(acts), info, GFP_KERNEL);
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700777 if (!skb)
778 return ERR_PTR(-ENOMEM);
779
780 return skb;
Jesse Grossccb13522011-10-25 19:26:31 -0700781}
782
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700783/* Called with ovs_mutex. */
784static struct sk_buff *ovs_flow_cmd_build_info(const struct sw_flow *flow,
785 int dp_ifindex,
786 struct genl_info *info, u8 cmd,
787 bool always)
Jesse Grossccb13522011-10-25 19:26:31 -0700788{
789 struct sk_buff *skb;
790 int retval;
791
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700792 skb = ovs_flow_cmd_alloc_info(ovsl_dereference(flow->sf_acts), info,
793 always);
Himangi Saraogid0e992a2014-07-27 12:37:46 +0530794 if (IS_ERR_OR_NULL(skb))
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700795 return skb;
Jesse Grossccb13522011-10-25 19:26:31 -0700796
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700797 retval = ovs_flow_cmd_fill_info(flow, dp_ifindex, skb,
798 info->snd_portid, info->snd_seq, 0,
799 cmd);
Jesse Grossccb13522011-10-25 19:26:31 -0700800 BUG_ON(retval < 0);
801 return skb;
802}
803
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700804static int ovs_flow_cmd_new(struct sk_buff *skb, struct genl_info *info)
Jesse Grossccb13522011-10-25 19:26:31 -0700805{
806 struct nlattr **a = info->attrs;
807 struct ovs_header *ovs_header = info->userhdr;
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700808 struct sw_flow *flow, *new_flow;
Andy Zhou03f0d912013-08-07 20:01:00 -0700809 struct sw_flow_mask mask;
Jesse Grossccb13522011-10-25 19:26:31 -0700810 struct sk_buff *reply;
811 struct datapath *dp;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700812 struct sw_flow_actions *acts;
813 struct sw_flow_match match;
814 int error;
815
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700816 /* Must have key and actions. */
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700817 error = -EINVAL;
818 if (!a[OVS_FLOW_ATTR_KEY])
819 goto error;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700820 if (!a[OVS_FLOW_ATTR_ACTIONS])
821 goto error;
822
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700823 /* Most of the time we need to allocate a new flow, do it before
824 * locking.
825 */
826 new_flow = ovs_flow_alloc();
827 if (IS_ERR(new_flow)) {
828 error = PTR_ERR(new_flow);
829 goto error;
830 }
831
832 /* Extract key. */
833 ovs_match_init(&match, &new_flow->unmasked_key, &mask);
834 error = ovs_nla_get_match(&match,
835 a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
836 if (error)
837 goto err_kfree_flow;
838
839 ovs_flow_mask_key(&new_flow->key, &new_flow->unmasked_key, &mask);
840
841 /* Validate actions. */
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700842 acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_FLOW_ATTR_ACTIONS]));
843 error = PTR_ERR(acts);
844 if (IS_ERR(acts))
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700845 goto err_kfree_flow;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700846
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700847 error = ovs_nla_copy_actions(a[OVS_FLOW_ATTR_ACTIONS], &new_flow->key,
848 0, &acts);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700849 if (error) {
850 OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700851 goto err_kfree_acts;
852 }
853
854 reply = ovs_flow_cmd_alloc_info(acts, info, false);
855 if (IS_ERR(reply)) {
856 error = PTR_ERR(reply);
857 goto err_kfree_acts;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700858 }
859
860 ovs_lock();
861 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700862 if (unlikely(!dp)) {
863 error = -ENODEV;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700864 goto err_unlock_ovs;
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700865 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700866 /* Check if this is a duplicate flow */
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700867 flow = ovs_flow_tbl_lookup(&dp->table, &new_flow->unmasked_key);
868 if (likely(!flow)) {
869 rcu_assign_pointer(new_flow->sf_acts, acts);
870
871 /* Put flow in bucket. */
872 error = ovs_flow_tbl_insert(&dp->table, new_flow, &mask);
873 if (unlikely(error)) {
874 acts = NULL;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700875 goto err_unlock_ovs;
876 }
877
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700878 if (unlikely(reply)) {
879 error = ovs_flow_cmd_fill_info(new_flow,
880 ovs_header->dp_ifindex,
881 reply, info->snd_portid,
882 info->snd_seq, 0,
883 OVS_FLOW_CMD_NEW);
884 BUG_ON(error < 0);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700885 }
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700886 ovs_unlock();
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700887 } else {
888 struct sw_flow_actions *old_acts;
889
890 /* Bail out if we're not allowed to modify an existing flow.
891 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
892 * because Generic Netlink treats the latter as a dump
893 * request. We also accept NLM_F_EXCL in case that bug ever
894 * gets fixed.
895 */
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700896 if (unlikely(info->nlhdr->nlmsg_flags & (NLM_F_CREATE
897 | NLM_F_EXCL))) {
898 error = -EEXIST;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700899 goto err_unlock_ovs;
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700900 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700901 /* The unmasked key has to be the same for flow updates. */
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700902 if (unlikely(!ovs_flow_cmp_unmasked_key(flow, &match))) {
Alex Wang4a46b242014-06-30 20:30:29 -0700903 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
904 if (!flow) {
905 error = -ENOENT;
906 goto err_unlock_ovs;
907 }
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700908 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700909 /* Update actions. */
910 old_acts = ovsl_dereference(flow->sf_acts);
911 rcu_assign_pointer(flow->sf_acts, acts);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700912
913 if (unlikely(reply)) {
914 error = ovs_flow_cmd_fill_info(flow,
915 ovs_header->dp_ifindex,
916 reply, info->snd_portid,
917 info->snd_seq, 0,
918 OVS_FLOW_CMD_NEW);
919 BUG_ON(error < 0);
920 }
921 ovs_unlock();
922
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700923 ovs_nla_free_flow_actions(old_acts);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700924 ovs_flow_free(new_flow, false);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700925 }
926
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700927 if (reply)
928 ovs_notify(&dp_flow_genl_family, reply, info);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700929 return 0;
930
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700931err_unlock_ovs:
932 ovs_unlock();
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700933 kfree_skb(reply);
934err_kfree_acts:
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700935 kfree(acts);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700936err_kfree_flow:
937 ovs_flow_free(new_flow, false);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700938error:
939 return error;
940}
941
Jesse Gross6b205b22014-10-03 15:35:32 -0700942static struct sw_flow_actions *get_flow_actions(const struct nlattr *a,
943 const struct sw_flow_key *key,
944 const struct sw_flow_mask *mask)
945{
946 struct sw_flow_actions *acts;
947 struct sw_flow_key masked_key;
948 int error;
949
950 acts = ovs_nla_alloc_flow_actions(nla_len(a));
951 if (IS_ERR(acts))
952 return acts;
953
954 ovs_flow_mask_key(&masked_key, key, mask);
955 error = ovs_nla_copy_actions(a, &masked_key, 0, &acts);
956 if (error) {
957 OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
958 kfree(acts);
959 return ERR_PTR(error);
960 }
961
962 return acts;
963}
964
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700965static int ovs_flow_cmd_set(struct sk_buff *skb, struct genl_info *info)
966{
967 struct nlattr **a = info->attrs;
968 struct ovs_header *ovs_header = info->userhdr;
Jesse Gross6b205b22014-10-03 15:35:32 -0700969 struct sw_flow_key key;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700970 struct sw_flow *flow;
971 struct sw_flow_mask mask;
972 struct sk_buff *reply = NULL;
973 struct datapath *dp;
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700974 struct sw_flow_actions *old_acts = NULL, *acts = NULL;
Andy Zhou03f0d912013-08-07 20:01:00 -0700975 struct sw_flow_match match;
Jesse Grossccb13522011-10-25 19:26:31 -0700976 int error;
Jesse Grossccb13522011-10-25 19:26:31 -0700977
978 /* Extract key. */
979 error = -EINVAL;
980 if (!a[OVS_FLOW_ATTR_KEY])
981 goto error;
Andy Zhou03f0d912013-08-07 20:01:00 -0700982
983 ovs_match_init(&match, &key, &mask);
Jarno Rajahalme23dabf82014-03-27 12:35:23 -0700984 error = ovs_nla_get_match(&match,
Pravin B Shelare6445712013-10-03 18:16:47 -0700985 a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
Jesse Grossccb13522011-10-25 19:26:31 -0700986 if (error)
987 goto error;
988
989 /* Validate actions. */
990 if (a[OVS_FLOW_ATTR_ACTIONS]) {
Jesse Gross6b205b22014-10-03 15:35:32 -0700991 acts = get_flow_actions(a[OVS_FLOW_ATTR_ACTIONS], &key, &mask);
992 if (IS_ERR(acts)) {
993 error = PTR_ERR(acts);
Jesse Grossccb13522011-10-25 19:26:31 -0700994 goto error;
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700995 }
996 }
997
998 /* Can allocate before locking if have acts. */
999 if (acts) {
1000 reply = ovs_flow_cmd_alloc_info(acts, info, false);
1001 if (IS_ERR(reply)) {
1002 error = PTR_ERR(reply);
1003 goto err_kfree_acts;
Andy Zhou03f0d912013-08-07 20:01:00 -07001004 }
Jesse Grossccb13522011-10-25 19:26:31 -07001005 }
1006
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001007 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001008 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001009 if (unlikely(!dp)) {
1010 error = -ENODEV;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001011 goto err_unlock_ovs;
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001012 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001013 /* Check that the flow exists. */
Alex Wang4a46b242014-06-30 20:30:29 -07001014 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001015 if (unlikely(!flow)) {
1016 error = -ENOENT;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001017 goto err_unlock_ovs;
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001018 }
Alex Wang4a46b242014-06-30 20:30:29 -07001019
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001020 /* Update actions, if present. */
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001021 if (likely(acts)) {
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001022 old_acts = ovsl_dereference(flow->sf_acts);
Jesse Grossccb13522011-10-25 19:26:31 -07001023 rcu_assign_pointer(flow->sf_acts, acts);
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001024
1025 if (unlikely(reply)) {
1026 error = ovs_flow_cmd_fill_info(flow,
1027 ovs_header->dp_ifindex,
1028 reply, info->snd_portid,
1029 info->snd_seq, 0,
1030 OVS_FLOW_CMD_NEW);
1031 BUG_ON(error < 0);
1032 }
1033 } else {
1034 /* Could not alloc without acts before locking. */
1035 reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex,
1036 info, OVS_FLOW_CMD_NEW, false);
1037 if (unlikely(IS_ERR(reply))) {
1038 error = PTR_ERR(reply);
1039 goto err_unlock_ovs;
1040 }
Jesse Grossccb13522011-10-25 19:26:31 -07001041 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001042
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001043 /* Clear stats. */
1044 if (a[OVS_FLOW_ATTR_CLEAR])
1045 ovs_flow_stats_clear(flow);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001046 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001047
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001048 if (reply)
1049 ovs_notify(&dp_flow_genl_family, reply, info);
1050 if (old_acts)
1051 ovs_nla_free_flow_actions(old_acts);
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -07001052
Jesse Grossccb13522011-10-25 19:26:31 -07001053 return 0;
1054
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001055err_unlock_ovs:
1056 ovs_unlock();
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001057 kfree_skb(reply);
1058err_kfree_acts:
Pravin B Shelar74f84a52013-06-17 17:50:12 -07001059 kfree(acts);
Jesse Grossccb13522011-10-25 19:26:31 -07001060error:
1061 return error;
1062}
1063
1064static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1065{
1066 struct nlattr **a = info->attrs;
1067 struct ovs_header *ovs_header = info->userhdr;
1068 struct sw_flow_key key;
1069 struct sk_buff *reply;
1070 struct sw_flow *flow;
1071 struct datapath *dp;
Andy Zhou03f0d912013-08-07 20:01:00 -07001072 struct sw_flow_match match;
Jesse Grossccb13522011-10-25 19:26:31 -07001073 int err;
Jesse Grossccb13522011-10-25 19:26:31 -07001074
Andy Zhou03f0d912013-08-07 20:01:00 -07001075 if (!a[OVS_FLOW_ATTR_KEY]) {
1076 OVS_NLERR("Flow get message rejected, Key attribute missing.\n");
Jesse Grossccb13522011-10-25 19:26:31 -07001077 return -EINVAL;
Andy Zhou03f0d912013-08-07 20:01:00 -07001078 }
1079
1080 ovs_match_init(&match, &key, NULL);
Jarno Rajahalme23dabf82014-03-27 12:35:23 -07001081 err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL);
Jesse Grossccb13522011-10-25 19:26:31 -07001082 if (err)
1083 return err;
1084
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001085 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001086 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001087 if (!dp) {
1088 err = -ENODEV;
1089 goto unlock;
1090 }
Jesse Grossccb13522011-10-25 19:26:31 -07001091
Alex Wang4a46b242014-06-30 20:30:29 -07001092 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1093 if (!flow) {
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001094 err = -ENOENT;
1095 goto unlock;
1096 }
Jesse Grossccb13522011-10-25 19:26:31 -07001097
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -07001098 reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex, info,
1099 OVS_FLOW_CMD_NEW, true);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001100 if (IS_ERR(reply)) {
1101 err = PTR_ERR(reply);
1102 goto unlock;
1103 }
Jesse Grossccb13522011-10-25 19:26:31 -07001104
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001105 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001106 return genlmsg_reply(reply, info);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001107unlock:
1108 ovs_unlock();
1109 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001110}
1111
1112static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1113{
1114 struct nlattr **a = info->attrs;
1115 struct ovs_header *ovs_header = info->userhdr;
1116 struct sw_flow_key key;
1117 struct sk_buff *reply;
1118 struct sw_flow *flow;
1119 struct datapath *dp;
Andy Zhou03f0d912013-08-07 20:01:00 -07001120 struct sw_flow_match match;
Jesse Grossccb13522011-10-25 19:26:31 -07001121 int err;
Jesse Grossccb13522011-10-25 19:26:31 -07001122
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001123 if (likely(a[OVS_FLOW_ATTR_KEY])) {
1124 ovs_match_init(&match, &key, NULL);
1125 err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL);
1126 if (unlikely(err))
1127 return err;
1128 }
1129
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001130 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001131 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001132 if (unlikely(!dp)) {
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001133 err = -ENODEV;
1134 goto unlock;
1135 }
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001136
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001137 if (unlikely(!a[OVS_FLOW_ATTR_KEY])) {
Pravin B Shelarb637e492013-10-04 00:14:23 -07001138 err = ovs_flow_tbl_flush(&dp->table);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001139 goto unlock;
1140 }
Andy Zhou03f0d912013-08-07 20:01:00 -07001141
Alex Wang4a46b242014-06-30 20:30:29 -07001142 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1143 if (unlikely(!flow)) {
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001144 err = -ENOENT;
1145 goto unlock;
1146 }
Jesse Grossccb13522011-10-25 19:26:31 -07001147
Pravin B Shelarb637e492013-10-04 00:14:23 -07001148 ovs_flow_tbl_remove(&dp->table, flow);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001149 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001150
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001151 reply = ovs_flow_cmd_alloc_info((const struct sw_flow_actions __force *) flow->sf_acts,
1152 info, false);
1153 if (likely(reply)) {
1154 if (likely(!IS_ERR(reply))) {
1155 rcu_read_lock(); /*To keep RCU checker happy. */
1156 err = ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex,
1157 reply, info->snd_portid,
1158 info->snd_seq, 0,
1159 OVS_FLOW_CMD_DEL);
1160 rcu_read_unlock();
1161 BUG_ON(err < 0);
1162
1163 ovs_notify(&dp_flow_genl_family, reply, info);
1164 } else {
1165 netlink_set_err(sock_net(skb->sk)->genl_sock, 0, 0, PTR_ERR(reply));
1166 }
1167 }
1168
1169 ovs_flow_free(flow, true);
Jesse Grossccb13522011-10-25 19:26:31 -07001170 return 0;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001171unlock:
1172 ovs_unlock();
1173 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001174}
1175
1176static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1177{
1178 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
Pravin B Shelarb637e492013-10-04 00:14:23 -07001179 struct table_instance *ti;
Jesse Grossccb13522011-10-25 19:26:31 -07001180 struct datapath *dp;
Jesse Grossccb13522011-10-25 19:26:31 -07001181
Pravin B Shelard57170b2013-07-30 15:39:39 -07001182 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001183 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001184 if (!dp) {
Pravin B Shelard57170b2013-07-30 15:39:39 -07001185 rcu_read_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001186 return -ENODEV;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001187 }
Jesse Grossccb13522011-10-25 19:26:31 -07001188
Pravin B Shelarb637e492013-10-04 00:14:23 -07001189 ti = rcu_dereference(dp->table.ti);
Jesse Grossccb13522011-10-25 19:26:31 -07001190 for (;;) {
1191 struct sw_flow *flow;
1192 u32 bucket, obj;
1193
1194 bucket = cb->args[0];
1195 obj = cb->args[1];
Pravin B Shelarb637e492013-10-04 00:14:23 -07001196 flow = ovs_flow_tbl_dump_next(ti, &bucket, &obj);
Jesse Grossccb13522011-10-25 19:26:31 -07001197 if (!flow)
1198 break;
1199
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -07001200 if (ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex, skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001201 NETLINK_CB(cb->skb).portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001202 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1203 OVS_FLOW_CMD_NEW) < 0)
1204 break;
1205
1206 cb->args[0] = bucket;
1207 cb->args[1] = obj;
1208 }
Pravin B Shelard57170b2013-07-30 15:39:39 -07001209 rcu_read_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001210 return skb->len;
1211}
1212
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001213static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
1214 [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
1215 [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
1216 [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
1217};
1218
stephen hemminger48e48a72014-07-16 11:25:52 -07001219static const struct genl_ops dp_flow_genl_ops[] = {
Jesse Grossccb13522011-10-25 19:26:31 -07001220 { .cmd = OVS_FLOW_CMD_NEW,
1221 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1222 .policy = flow_policy,
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001223 .doit = ovs_flow_cmd_new
Jesse Grossccb13522011-10-25 19:26:31 -07001224 },
1225 { .cmd = OVS_FLOW_CMD_DEL,
1226 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1227 .policy = flow_policy,
1228 .doit = ovs_flow_cmd_del
1229 },
1230 { .cmd = OVS_FLOW_CMD_GET,
1231 .flags = 0, /* OK for unprivileged users. */
1232 .policy = flow_policy,
1233 .doit = ovs_flow_cmd_get,
1234 .dumpit = ovs_flow_cmd_dump
1235 },
1236 { .cmd = OVS_FLOW_CMD_SET,
1237 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1238 .policy = flow_policy,
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001239 .doit = ovs_flow_cmd_set,
Jesse Grossccb13522011-10-25 19:26:31 -07001240 },
1241};
1242
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001243static struct genl_family dp_flow_genl_family = {
Jesse Grossccb13522011-10-25 19:26:31 -07001244 .id = GENL_ID_GENERATE,
1245 .hdrsize = sizeof(struct ovs_header),
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001246 .name = OVS_FLOW_FAMILY,
1247 .version = OVS_FLOW_VERSION,
1248 .maxattr = OVS_FLOW_ATTR_MAX,
Pravin B Shelar3a4e0d62013-04-23 07:48:48 +00001249 .netnsok = true,
1250 .parallel_ops = true,
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001251 .ops = dp_flow_genl_ops,
1252 .n_ops = ARRAY_SIZE(dp_flow_genl_ops),
1253 .mcgrps = &ovs_dp_flow_multicast_group,
1254 .n_mcgrps = 1,
Jesse Grossccb13522011-10-25 19:26:31 -07001255};
1256
Thomas Grafc3ff8cf2013-03-29 14:46:49 +01001257static size_t ovs_dp_cmd_msg_size(void)
1258{
1259 size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
1260
1261 msgsize += nla_total_size(IFNAMSIZ);
1262 msgsize += nla_total_size(sizeof(struct ovs_dp_stats));
Andy Zhou1bd71162013-10-22 10:42:46 -07001263 msgsize += nla_total_size(sizeof(struct ovs_dp_megaflow_stats));
Daniele Di Proietto45fb9c32014-01-23 10:47:35 -08001264 msgsize += nla_total_size(sizeof(u32)); /* OVS_DP_ATTR_USER_FEATURES */
Thomas Grafc3ff8cf2013-03-29 14:46:49 +01001265
1266 return msgsize;
1267}
1268
Jarno Rajahalmebb6f9a72014-05-05 11:32:17 -07001269/* Called with ovs_mutex or RCU read lock. */
Jesse Grossccb13522011-10-25 19:26:31 -07001270static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001271 u32 portid, u32 seq, u32 flags, u8 cmd)
Jesse Grossccb13522011-10-25 19:26:31 -07001272{
1273 struct ovs_header *ovs_header;
1274 struct ovs_dp_stats dp_stats;
Andy Zhou1bd71162013-10-22 10:42:46 -07001275 struct ovs_dp_megaflow_stats dp_megaflow_stats;
Jesse Grossccb13522011-10-25 19:26:31 -07001276 int err;
1277
Eric W. Biederman15e47302012-09-07 20:12:54 +00001278 ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
Jesse Grossccb13522011-10-25 19:26:31 -07001279 flags, cmd);
1280 if (!ovs_header)
1281 goto error;
1282
1283 ovs_header->dp_ifindex = get_dpifindex(dp);
1284
Jesse Grossccb13522011-10-25 19:26:31 -07001285 err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
Jesse Grossccb13522011-10-25 19:26:31 -07001286 if (err)
1287 goto nla_put_failure;
1288
Andy Zhou1bd71162013-10-22 10:42:46 -07001289 get_dp_stats(dp, &dp_stats, &dp_megaflow_stats);
1290 if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats),
1291 &dp_stats))
1292 goto nla_put_failure;
1293
1294 if (nla_put(skb, OVS_DP_ATTR_MEGAFLOW_STATS,
1295 sizeof(struct ovs_dp_megaflow_stats),
1296 &dp_megaflow_stats))
David S. Miller028d6a62012-03-29 23:20:48 -04001297 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001298
Thomas Graf43d4be92013-12-13 15:22:18 +01001299 if (nla_put_u32(skb, OVS_DP_ATTR_USER_FEATURES, dp->user_features))
1300 goto nla_put_failure;
1301
Jesse Grossccb13522011-10-25 19:26:31 -07001302 return genlmsg_end(skb, ovs_header);
1303
1304nla_put_failure:
1305 genlmsg_cancel(skb, ovs_header);
1306error:
1307 return -EMSGSIZE;
1308}
1309
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001310static struct sk_buff *ovs_dp_cmd_alloc_info(struct genl_info *info)
Jesse Grossccb13522011-10-25 19:26:31 -07001311{
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001312 return genlmsg_new_unicast(ovs_dp_cmd_msg_size(), info, GFP_KERNEL);
Jesse Grossccb13522011-10-25 19:26:31 -07001313}
1314
Jarno Rajahalmebb6f9a72014-05-05 11:32:17 -07001315/* Called with rcu_read_lock or ovs_mutex. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001316static struct datapath *lookup_datapath(struct net *net,
1317 struct ovs_header *ovs_header,
Jesse Grossccb13522011-10-25 19:26:31 -07001318 struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1319{
1320 struct datapath *dp;
1321
1322 if (!a[OVS_DP_ATTR_NAME])
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001323 dp = get_dp(net, ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001324 else {
1325 struct vport *vport;
1326
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001327 vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
Jesse Grossccb13522011-10-25 19:26:31 -07001328 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
Jesse Grossccb13522011-10-25 19:26:31 -07001329 }
1330 return dp ? dp : ERR_PTR(-ENODEV);
1331}
1332
Thomas Graf44da5ae2013-12-13 15:22:19 +01001333static void ovs_dp_reset_user_features(struct sk_buff *skb, struct genl_info *info)
1334{
1335 struct datapath *dp;
1336
1337 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
Jiri Pirko3c7eacf2014-02-14 11:42:36 +01001338 if (IS_ERR(dp))
Thomas Graf44da5ae2013-12-13 15:22:19 +01001339 return;
1340
1341 WARN(dp->user_features, "Dropping previously announced user features\n");
1342 dp->user_features = 0;
1343}
1344
Thomas Graf43d4be92013-12-13 15:22:18 +01001345static void ovs_dp_change(struct datapath *dp, struct nlattr **a)
1346{
1347 if (a[OVS_DP_ATTR_USER_FEATURES])
1348 dp->user_features = nla_get_u32(a[OVS_DP_ATTR_USER_FEATURES]);
1349}
1350
Jesse Grossccb13522011-10-25 19:26:31 -07001351static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1352{
1353 struct nlattr **a = info->attrs;
1354 struct vport_parms parms;
1355 struct sk_buff *reply;
1356 struct datapath *dp;
1357 struct vport *vport;
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001358 struct ovs_net *ovs_net;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001359 int err, i;
Jesse Grossccb13522011-10-25 19:26:31 -07001360
1361 err = -EINVAL;
1362 if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1363 goto err;
1364
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001365 reply = ovs_dp_cmd_alloc_info(info);
1366 if (!reply)
1367 return -ENOMEM;
Jesse Grossccb13522011-10-25 19:26:31 -07001368
1369 err = -ENOMEM;
1370 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1371 if (dp == NULL)
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001372 goto err_free_reply;
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001373
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001374 ovs_dp_set_net(dp, hold_net(sock_net(skb->sk)));
Jesse Grossccb13522011-10-25 19:26:31 -07001375
1376 /* Allocate table. */
Pravin B Shelarb637e492013-10-04 00:14:23 -07001377 err = ovs_flow_tbl_init(&dp->table);
1378 if (err)
Jesse Grossccb13522011-10-25 19:26:31 -07001379 goto err_free_dp;
1380
WANG Cong1c213bd2014-02-13 11:46:28 -08001381 dp->stats_percpu = netdev_alloc_pcpu_stats(struct dp_stats_percpu);
Jesse Grossccb13522011-10-25 19:26:31 -07001382 if (!dp->stats_percpu) {
1383 err = -ENOMEM;
1384 goto err_destroy_table;
1385 }
1386
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001387 dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
Pravin B Shelare6445712013-10-03 18:16:47 -07001388 GFP_KERNEL);
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001389 if (!dp->ports) {
1390 err = -ENOMEM;
1391 goto err_destroy_percpu;
1392 }
1393
1394 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1395 INIT_HLIST_HEAD(&dp->ports[i]);
1396
Jesse Grossccb13522011-10-25 19:26:31 -07001397 /* Set up our datapath device. */
1398 parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1399 parms.type = OVS_VPORT_TYPE_INTERNAL;
1400 parms.options = NULL;
1401 parms.dp = dp;
1402 parms.port_no = OVSP_LOCAL;
Alex Wang5cd667b2014-07-17 15:14:13 -07001403 parms.upcall_portids = a[OVS_DP_ATTR_UPCALL_PID];
Jesse Grossccb13522011-10-25 19:26:31 -07001404
Thomas Graf43d4be92013-12-13 15:22:18 +01001405 ovs_dp_change(dp, a);
1406
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001407 /* So far only local changes have been made, now need the lock. */
1408 ovs_lock();
1409
Jesse Grossccb13522011-10-25 19:26:31 -07001410 vport = new_vport(&parms);
1411 if (IS_ERR(vport)) {
1412 err = PTR_ERR(vport);
1413 if (err == -EBUSY)
1414 err = -EEXIST;
1415
Thomas Graf44da5ae2013-12-13 15:22:19 +01001416 if (err == -EEXIST) {
1417 /* An outdated user space instance that does not understand
1418 * the concept of user_features has attempted to create a new
1419 * datapath and is likely to reuse it. Drop all user features.
1420 */
1421 if (info->genlhdr->version < OVS_DP_VER_FEATURES)
1422 ovs_dp_reset_user_features(skb, info);
1423 }
1424
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001425 goto err_destroy_ports_array;
Jesse Grossccb13522011-10-25 19:26:31 -07001426 }
1427
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001428 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1429 info->snd_seq, 0, OVS_DP_CMD_NEW);
1430 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001431
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001432 ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001433 list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001434
1435 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001436
Johannes Berg2a94fe42013-11-19 15:19:39 +01001437 ovs_notify(&dp_datapath_genl_family, reply, info);
Jesse Grossccb13522011-10-25 19:26:31 -07001438 return 0;
1439
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001440err_destroy_ports_array:
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001441 ovs_unlock();
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001442 kfree(dp->ports);
Jesse Grossccb13522011-10-25 19:26:31 -07001443err_destroy_percpu:
1444 free_percpu(dp->stats_percpu);
1445err_destroy_table:
Andy Zhoue80857c2014-01-21 09:31:04 -08001446 ovs_flow_tbl_destroy(&dp->table, false);
Jesse Grossccb13522011-10-25 19:26:31 -07001447err_free_dp:
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001448 release_net(ovs_dp_get_net(dp));
Jesse Grossccb13522011-10-25 19:26:31 -07001449 kfree(dp);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001450err_free_reply:
1451 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001452err:
1453 return err;
1454}
1455
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001456/* Called with ovs_mutex. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001457static void __dp_destroy(struct datapath *dp)
Jesse Grossccb13522011-10-25 19:26:31 -07001458{
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001459 int i;
Jesse Grossccb13522011-10-25 19:26:31 -07001460
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001461 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1462 struct vport *vport;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001463 struct hlist_node *n;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001464
Sasha Levinb67bfe02013-02-27 17:06:00 -08001465 hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001466 if (vport->port_no != OVSP_LOCAL)
1467 ovs_dp_detach_port(vport);
1468 }
Jesse Grossccb13522011-10-25 19:26:31 -07001469
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001470 list_del_rcu(&dp->list_node);
Jesse Grossccb13522011-10-25 19:26:31 -07001471
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001472 /* OVSP_LOCAL is datapath internal port. We need to make sure that
Andy Zhoue80857c2014-01-21 09:31:04 -08001473 * all ports in datapath are destroyed first before freeing datapath.
Jesse Grossccb13522011-10-25 19:26:31 -07001474 */
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001475 ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
Jesse Grossccb13522011-10-25 19:26:31 -07001476
Andy Zhoue80857c2014-01-21 09:31:04 -08001477 /* RCU destroy the flow table */
1478 ovs_flow_tbl_destroy(&dp->table, true);
1479
Jesse Grossccb13522011-10-25 19:26:31 -07001480 call_rcu(&dp->rcu, destroy_dp_rcu);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001481}
1482
1483static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1484{
1485 struct sk_buff *reply;
1486 struct datapath *dp;
1487 int err;
1488
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001489 reply = ovs_dp_cmd_alloc_info(info);
1490 if (!reply)
1491 return -ENOMEM;
1492
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001493 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001494 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1495 err = PTR_ERR(dp);
1496 if (IS_ERR(dp))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001497 goto err_unlock_free;
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001498
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001499 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1500 info->snd_seq, 0, OVS_DP_CMD_DEL);
1501 BUG_ON(err < 0);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001502
1503 __dp_destroy(dp);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001504 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001505
Johannes Berg2a94fe42013-11-19 15:19:39 +01001506 ovs_notify(&dp_datapath_genl_family, reply, info);
Jesse Grossccb13522011-10-25 19:26:31 -07001507
1508 return 0;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001509
1510err_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001511 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001512 kfree_skb(reply);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001513 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001514}
1515
1516static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1517{
1518 struct sk_buff *reply;
1519 struct datapath *dp;
1520 int err;
1521
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001522 reply = ovs_dp_cmd_alloc_info(info);
1523 if (!reply)
1524 return -ENOMEM;
1525
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001526 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001527 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001528 err = PTR_ERR(dp);
Jesse Grossccb13522011-10-25 19:26:31 -07001529 if (IS_ERR(dp))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001530 goto err_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001531
Thomas Graf43d4be92013-12-13 15:22:18 +01001532 ovs_dp_change(dp, info->attrs);
1533
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001534 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1535 info->snd_seq, 0, OVS_DP_CMD_NEW);
1536 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001537
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001538 ovs_unlock();
Johannes Berg2a94fe42013-11-19 15:19:39 +01001539 ovs_notify(&dp_datapath_genl_family, reply, info);
Jesse Grossccb13522011-10-25 19:26:31 -07001540
1541 return 0;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001542
1543err_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001544 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001545 kfree_skb(reply);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001546 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001547}
1548
1549static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1550{
1551 struct sk_buff *reply;
1552 struct datapath *dp;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001553 int err;
Jesse Grossccb13522011-10-25 19:26:31 -07001554
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001555 reply = ovs_dp_cmd_alloc_info(info);
1556 if (!reply)
1557 return -ENOMEM;
1558
1559 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001560 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001561 if (IS_ERR(dp)) {
1562 err = PTR_ERR(dp);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001563 goto err_unlock_free;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001564 }
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001565 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1566 info->snd_seq, 0, OVS_DP_CMD_NEW);
1567 BUG_ON(err < 0);
1568 rcu_read_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001569
Jesse Grossccb13522011-10-25 19:26:31 -07001570 return genlmsg_reply(reply, info);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001571
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001572err_unlock_free:
1573 rcu_read_unlock();
1574 kfree_skb(reply);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001575 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001576}
1577
1578static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1579{
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001580 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
Jesse Grossccb13522011-10-25 19:26:31 -07001581 struct datapath *dp;
1582 int skip = cb->args[0];
1583 int i = 0;
1584
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001585 rcu_read_lock();
1586 list_for_each_entry_rcu(dp, &ovs_net->dps, list_node) {
Ben Pfaff77676fd2012-01-17 13:33:39 +00001587 if (i >= skip &&
Eric W. Biederman15e47302012-09-07 20:12:54 +00001588 ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001589 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1590 OVS_DP_CMD_NEW) < 0)
1591 break;
1592 i++;
1593 }
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001594 rcu_read_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001595
1596 cb->args[0] = i;
1597
1598 return skb->len;
1599}
1600
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001601static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1602 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1603 [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1604 [OVS_DP_ATTR_USER_FEATURES] = { .type = NLA_U32 },
1605};
1606
stephen hemminger48e48a72014-07-16 11:25:52 -07001607static const struct genl_ops dp_datapath_genl_ops[] = {
Jesse Grossccb13522011-10-25 19:26:31 -07001608 { .cmd = OVS_DP_CMD_NEW,
1609 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1610 .policy = datapath_policy,
1611 .doit = ovs_dp_cmd_new
1612 },
1613 { .cmd = OVS_DP_CMD_DEL,
1614 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1615 .policy = datapath_policy,
1616 .doit = ovs_dp_cmd_del
1617 },
1618 { .cmd = OVS_DP_CMD_GET,
1619 .flags = 0, /* OK for unprivileged users. */
1620 .policy = datapath_policy,
1621 .doit = ovs_dp_cmd_get,
1622 .dumpit = ovs_dp_cmd_dump
1623 },
1624 { .cmd = OVS_DP_CMD_SET,
1625 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1626 .policy = datapath_policy,
1627 .doit = ovs_dp_cmd_set,
1628 },
1629};
1630
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001631static struct genl_family dp_datapath_genl_family = {
Jesse Grossccb13522011-10-25 19:26:31 -07001632 .id = GENL_ID_GENERATE,
1633 .hdrsize = sizeof(struct ovs_header),
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001634 .name = OVS_DATAPATH_FAMILY,
1635 .version = OVS_DATAPATH_VERSION,
1636 .maxattr = OVS_DP_ATTR_MAX,
Pravin B Shelar3a4e0d62013-04-23 07:48:48 +00001637 .netnsok = true,
1638 .parallel_ops = true,
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001639 .ops = dp_datapath_genl_ops,
1640 .n_ops = ARRAY_SIZE(dp_datapath_genl_ops),
1641 .mcgrps = &ovs_dp_datapath_multicast_group,
1642 .n_mcgrps = 1,
Jesse Grossccb13522011-10-25 19:26:31 -07001643};
1644
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001645/* Called with ovs_mutex or RCU read lock. */
Jesse Grossccb13522011-10-25 19:26:31 -07001646static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001647 u32 portid, u32 seq, u32 flags, u8 cmd)
Jesse Grossccb13522011-10-25 19:26:31 -07001648{
1649 struct ovs_header *ovs_header;
1650 struct ovs_vport_stats vport_stats;
1651 int err;
1652
Eric W. Biederman15e47302012-09-07 20:12:54 +00001653 ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
Jesse Grossccb13522011-10-25 19:26:31 -07001654 flags, cmd);
1655 if (!ovs_header)
1656 return -EMSGSIZE;
1657
1658 ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1659
David S. Miller028d6a62012-03-29 23:20:48 -04001660 if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
1661 nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
Alex Wang5cd667b2014-07-17 15:14:13 -07001662 nla_put_string(skb, OVS_VPORT_ATTR_NAME,
1663 vport->ops->get_name(vport)))
David S. Miller028d6a62012-03-29 23:20:48 -04001664 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001665
1666 ovs_vport_get_stats(vport, &vport_stats);
David S. Miller028d6a62012-03-29 23:20:48 -04001667 if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
1668 &vport_stats))
1669 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001670
Alex Wang5cd667b2014-07-17 15:14:13 -07001671 if (ovs_vport_get_upcall_portids(vport, skb))
1672 goto nla_put_failure;
1673
Jesse Grossccb13522011-10-25 19:26:31 -07001674 err = ovs_vport_get_options(vport, skb);
1675 if (err == -EMSGSIZE)
1676 goto error;
1677
1678 return genlmsg_end(skb, ovs_header);
1679
1680nla_put_failure:
1681 err = -EMSGSIZE;
1682error:
1683 genlmsg_cancel(skb, ovs_header);
1684 return err;
1685}
1686
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001687static struct sk_buff *ovs_vport_cmd_alloc_info(void)
1688{
1689 return nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1690}
1691
1692/* Called with ovs_mutex, only via ovs_dp_notify_wq(). */
Eric W. Biederman15e47302012-09-07 20:12:54 +00001693struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001694 u32 seq, u8 cmd)
1695{
1696 struct sk_buff *skb;
1697 int retval;
1698
1699 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1700 if (!skb)
1701 return ERR_PTR(-ENOMEM);
1702
Eric W. Biederman15e47302012-09-07 20:12:54 +00001703 retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
Jesse Grossa9341512013-03-26 15:48:38 -07001704 BUG_ON(retval < 0);
1705
Jesse Grossccb13522011-10-25 19:26:31 -07001706 return skb;
1707}
1708
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001709/* Called with ovs_mutex or RCU read lock. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001710static struct vport *lookup_vport(struct net *net,
1711 struct ovs_header *ovs_header,
Jesse Grossccb13522011-10-25 19:26:31 -07001712 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1713{
1714 struct datapath *dp;
1715 struct vport *vport;
1716
1717 if (a[OVS_VPORT_ATTR_NAME]) {
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001718 vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
Jesse Grossccb13522011-10-25 19:26:31 -07001719 if (!vport)
1720 return ERR_PTR(-ENODEV);
Ben Pfaff651a68e2012-03-06 15:04:04 -08001721 if (ovs_header->dp_ifindex &&
1722 ovs_header->dp_ifindex != get_dpifindex(vport->dp))
1723 return ERR_PTR(-ENODEV);
Jesse Grossccb13522011-10-25 19:26:31 -07001724 return vport;
1725 } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
1726 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1727
1728 if (port_no >= DP_MAX_PORTS)
1729 return ERR_PTR(-EFBIG);
1730
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001731 dp = get_dp(net, ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001732 if (!dp)
1733 return ERR_PTR(-ENODEV);
1734
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001735 vport = ovs_vport_ovsl_rcu(dp, port_no);
Jesse Grossccb13522011-10-25 19:26:31 -07001736 if (!vport)
Jarno Rajahalme14408db2013-01-09 14:27:35 -08001737 return ERR_PTR(-ENODEV);
Jesse Grossccb13522011-10-25 19:26:31 -07001738 return vport;
1739 } else
1740 return ERR_PTR(-EINVAL);
1741}
1742
1743static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
1744{
1745 struct nlattr **a = info->attrs;
1746 struct ovs_header *ovs_header = info->userhdr;
1747 struct vport_parms parms;
1748 struct sk_buff *reply;
1749 struct vport *vport;
1750 struct datapath *dp;
1751 u32 port_no;
1752 int err;
1753
Jesse Grossccb13522011-10-25 19:26:31 -07001754 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
1755 !a[OVS_VPORT_ATTR_UPCALL_PID])
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001756 return -EINVAL;
1757
1758 port_no = a[OVS_VPORT_ATTR_PORT_NO]
1759 ? nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]) : 0;
1760 if (port_no >= DP_MAX_PORTS)
1761 return -EFBIG;
1762
1763 reply = ovs_vport_cmd_alloc_info();
1764 if (!reply)
1765 return -ENOMEM;
Jesse Grossccb13522011-10-25 19:26:31 -07001766
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001767 ovs_lock();
Thomas Graf62b9c8d2014-10-22 17:29:06 +02001768restart:
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001769 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001770 err = -ENODEV;
1771 if (!dp)
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001772 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001773
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001774 if (port_no) {
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001775 vport = ovs_vport_ovsl(dp, port_no);
Jesse Grossccb13522011-10-25 19:26:31 -07001776 err = -EBUSY;
1777 if (vport)
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001778 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001779 } else {
1780 for (port_no = 1; ; port_no++) {
1781 if (port_no >= DP_MAX_PORTS) {
1782 err = -EFBIG;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001783 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001784 }
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001785 vport = ovs_vport_ovsl(dp, port_no);
Jesse Grossccb13522011-10-25 19:26:31 -07001786 if (!vport)
1787 break;
1788 }
1789 }
1790
1791 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
1792 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1793 parms.options = a[OVS_VPORT_ATTR_OPTIONS];
1794 parms.dp = dp;
1795 parms.port_no = port_no;
Alex Wang5cd667b2014-07-17 15:14:13 -07001796 parms.upcall_portids = a[OVS_VPORT_ATTR_UPCALL_PID];
Jesse Grossccb13522011-10-25 19:26:31 -07001797
1798 vport = new_vport(&parms);
1799 err = PTR_ERR(vport);
Thomas Graf62b9c8d2014-10-22 17:29:06 +02001800 if (IS_ERR(vport)) {
1801 if (err == -EAGAIN)
1802 goto restart;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001803 goto exit_unlock_free;
Thomas Graf62b9c8d2014-10-22 17:29:06 +02001804 }
Jesse Grossccb13522011-10-25 19:26:31 -07001805
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001806 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1807 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1808 BUG_ON(err < 0);
1809 ovs_unlock();
Thomas Grafed6611852013-03-29 14:46:50 +01001810
Johannes Berg2a94fe42013-11-19 15:19:39 +01001811 ovs_notify(&dp_vport_genl_family, reply, info);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001812 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001813
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001814exit_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001815 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001816 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001817 return err;
1818}
1819
1820static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
1821{
1822 struct nlattr **a = info->attrs;
1823 struct sk_buff *reply;
1824 struct vport *vport;
1825 int err;
1826
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001827 reply = ovs_vport_cmd_alloc_info();
1828 if (!reply)
1829 return -ENOMEM;
1830
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001831 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001832 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
Jesse Grossccb13522011-10-25 19:26:31 -07001833 err = PTR_ERR(vport);
1834 if (IS_ERR(vport))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001835 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001836
Jesse Grossccb13522011-10-25 19:26:31 -07001837 if (a[OVS_VPORT_ATTR_TYPE] &&
Jesse Grossf44f3402013-05-13 08:15:26 -07001838 nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
Jesse Grossccb13522011-10-25 19:26:31 -07001839 err = -EINVAL;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001840 goto exit_unlock_free;
Jesse Grossa9341512013-03-26 15:48:38 -07001841 }
1842
Jesse Grossf44f3402013-05-13 08:15:26 -07001843 if (a[OVS_VPORT_ATTR_OPTIONS]) {
Jesse Grossccb13522011-10-25 19:26:31 -07001844 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
Jesse Grossf44f3402013-05-13 08:15:26 -07001845 if (err)
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001846 goto exit_unlock_free;
Jesse Grossf44f3402013-05-13 08:15:26 -07001847 }
Jesse Grossa9341512013-03-26 15:48:38 -07001848
Alex Wang5cd667b2014-07-17 15:14:13 -07001849
1850 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
1851 struct nlattr *ids = a[OVS_VPORT_ATTR_UPCALL_PID];
1852
1853 err = ovs_vport_set_upcall_portids(vport, ids);
1854 if (err)
1855 goto exit_unlock_free;
1856 }
Jesse Grossccb13522011-10-25 19:26:31 -07001857
Jesse Grossa9341512013-03-26 15:48:38 -07001858 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1859 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1860 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001861
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001862 ovs_unlock();
Johannes Berg2a94fe42013-11-19 15:19:39 +01001863 ovs_notify(&dp_vport_genl_family, reply, info);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001864 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001865
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001866exit_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001867 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001868 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001869 return err;
1870}
1871
1872static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
1873{
1874 struct nlattr **a = info->attrs;
1875 struct sk_buff *reply;
1876 struct vport *vport;
1877 int err;
1878
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001879 reply = ovs_vport_cmd_alloc_info();
1880 if (!reply)
1881 return -ENOMEM;
1882
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001883 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001884 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
Jesse Grossccb13522011-10-25 19:26:31 -07001885 err = PTR_ERR(vport);
1886 if (IS_ERR(vport))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001887 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001888
1889 if (vport->port_no == OVSP_LOCAL) {
1890 err = -EINVAL;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001891 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001892 }
1893
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001894 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1895 info->snd_seq, 0, OVS_VPORT_CMD_DEL);
1896 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001897 ovs_dp_detach_port(vport);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001898 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001899
Johannes Berg2a94fe42013-11-19 15:19:39 +01001900 ovs_notify(&dp_vport_genl_family, reply, info);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001901 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001902
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001903exit_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001904 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001905 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001906 return err;
1907}
1908
1909static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
1910{
1911 struct nlattr **a = info->attrs;
1912 struct ovs_header *ovs_header = info->userhdr;
1913 struct sk_buff *reply;
1914 struct vport *vport;
1915 int err;
1916
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001917 reply = ovs_vport_cmd_alloc_info();
1918 if (!reply)
1919 return -ENOMEM;
1920
Jesse Grossccb13522011-10-25 19:26:31 -07001921 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001922 vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
Jesse Grossccb13522011-10-25 19:26:31 -07001923 err = PTR_ERR(vport);
1924 if (IS_ERR(vport))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001925 goto exit_unlock_free;
1926 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1927 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1928 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001929 rcu_read_unlock();
1930
1931 return genlmsg_reply(reply, info);
1932
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001933exit_unlock_free:
Jesse Grossccb13522011-10-25 19:26:31 -07001934 rcu_read_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001935 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001936 return err;
1937}
1938
1939static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1940{
1941 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1942 struct datapath *dp;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001943 int bucket = cb->args[0], skip = cb->args[1];
1944 int i, j = 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001945
Jesse Grossccb13522011-10-25 19:26:31 -07001946 rcu_read_lock();
Jarno Rajahalme42ee19e2014-02-15 17:42:29 -08001947 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1948 if (!dp) {
1949 rcu_read_unlock();
1950 return -ENODEV;
1951 }
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001952 for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
Jesse Grossccb13522011-10-25 19:26:31 -07001953 struct vport *vport;
1954
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001955 j = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001956 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001957 if (j >= skip &&
1958 ovs_vport_cmd_fill_info(vport, skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001959 NETLINK_CB(cb->skb).portid,
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001960 cb->nlh->nlmsg_seq,
1961 NLM_F_MULTI,
1962 OVS_VPORT_CMD_NEW) < 0)
1963 goto out;
Jesse Grossccb13522011-10-25 19:26:31 -07001964
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001965 j++;
1966 }
1967 skip = 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001968 }
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001969out:
Jesse Grossccb13522011-10-25 19:26:31 -07001970 rcu_read_unlock();
1971
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001972 cb->args[0] = i;
1973 cb->args[1] = j;
Jesse Grossccb13522011-10-25 19:26:31 -07001974
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001975 return skb->len;
Jesse Grossccb13522011-10-25 19:26:31 -07001976}
1977
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001978static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
1979 [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1980 [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
1981 [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
1982 [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
1983 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1984 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
1985};
1986
stephen hemminger48e48a72014-07-16 11:25:52 -07001987static const struct genl_ops dp_vport_genl_ops[] = {
Jesse Grossccb13522011-10-25 19:26:31 -07001988 { .cmd = OVS_VPORT_CMD_NEW,
1989 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1990 .policy = vport_policy,
1991 .doit = ovs_vport_cmd_new
1992 },
1993 { .cmd = OVS_VPORT_CMD_DEL,
1994 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1995 .policy = vport_policy,
1996 .doit = ovs_vport_cmd_del
1997 },
1998 { .cmd = OVS_VPORT_CMD_GET,
1999 .flags = 0, /* OK for unprivileged users. */
2000 .policy = vport_policy,
2001 .doit = ovs_vport_cmd_get,
2002 .dumpit = ovs_vport_cmd_dump
2003 },
2004 { .cmd = OVS_VPORT_CMD_SET,
2005 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2006 .policy = vport_policy,
2007 .doit = ovs_vport_cmd_set,
2008 },
2009};
2010
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002011struct genl_family dp_vport_genl_family = {
2012 .id = GENL_ID_GENERATE,
2013 .hdrsize = sizeof(struct ovs_header),
2014 .name = OVS_VPORT_FAMILY,
2015 .version = OVS_VPORT_VERSION,
2016 .maxattr = OVS_VPORT_ATTR_MAX,
2017 .netnsok = true,
2018 .parallel_ops = true,
2019 .ops = dp_vport_genl_ops,
2020 .n_ops = ARRAY_SIZE(dp_vport_genl_ops),
2021 .mcgrps = &ovs_dp_vport_multicast_group,
2022 .n_mcgrps = 1,
Jesse Grossccb13522011-10-25 19:26:31 -07002023};
2024
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002025static struct genl_family * const dp_genl_families[] = {
2026 &dp_datapath_genl_family,
2027 &dp_vport_genl_family,
2028 &dp_flow_genl_family,
2029 &dp_packet_genl_family,
Jesse Grossccb13522011-10-25 19:26:31 -07002030};
2031
2032static void dp_unregister_genl(int n_families)
2033{
2034 int i;
2035
2036 for (i = 0; i < n_families; i++)
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002037 genl_unregister_family(dp_genl_families[i]);
Jesse Grossccb13522011-10-25 19:26:31 -07002038}
2039
2040static int dp_register_genl(void)
2041{
Jesse Grossccb13522011-10-25 19:26:31 -07002042 int err;
2043 int i;
2044
Jesse Grossccb13522011-10-25 19:26:31 -07002045 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
Jesse Grossccb13522011-10-25 19:26:31 -07002046
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002047 err = genl_register_family(dp_genl_families[i]);
Jesse Grossccb13522011-10-25 19:26:31 -07002048 if (err)
2049 goto error;
Jesse Grossccb13522011-10-25 19:26:31 -07002050 }
2051
2052 return 0;
2053
2054error:
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002055 dp_unregister_genl(i);
Jesse Grossccb13522011-10-25 19:26:31 -07002056 return err;
2057}
2058
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002059static int __net_init ovs_init_net(struct net *net)
2060{
2061 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2062
2063 INIT_LIST_HEAD(&ovs_net->dps);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002064 INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002065 return 0;
2066}
2067
2068static void __net_exit ovs_exit_net(struct net *net)
2069{
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002070 struct datapath *dp, *dp_next;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002071 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002072
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002073 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002074 list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
2075 __dp_destroy(dp);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002076 ovs_unlock();
2077
2078 cancel_work_sync(&ovs_net->dp_notify_work);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002079}
2080
2081static struct pernet_operations ovs_net_ops = {
2082 .init = ovs_init_net,
2083 .exit = ovs_exit_net,
2084 .id = &ovs_net_id,
2085 .size = sizeof(struct ovs_net),
2086};
2087
Jesse Grossccb13522011-10-25 19:26:31 -07002088static int __init dp_init(void)
2089{
Jesse Grossccb13522011-10-25 19:26:31 -07002090 int err;
2091
YOSHIFUJI Hideaki / 吉藤英明3523b292013-01-09 07:19:55 +00002092 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
Jesse Grossccb13522011-10-25 19:26:31 -07002093
2094 pr_info("Open vSwitch switching datapath\n");
2095
Andy Zhou971427f32014-09-15 19:37:25 -07002096 err = action_fifos_init();
Jesse Grossccb13522011-10-25 19:26:31 -07002097 if (err)
2098 goto error;
2099
Andy Zhou971427f32014-09-15 19:37:25 -07002100 err = ovs_internal_dev_rtnl_link_register();
2101 if (err)
2102 goto error_action_fifos_exit;
2103
Jiri Pirko5b9e7e12014-06-26 09:58:26 +02002104 err = ovs_flow_init();
2105 if (err)
2106 goto error_unreg_rtnl_link;
2107
Jesse Grossccb13522011-10-25 19:26:31 -07002108 err = ovs_vport_init();
2109 if (err)
2110 goto error_flow_exit;
2111
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002112 err = register_pernet_device(&ovs_net_ops);
Jesse Grossccb13522011-10-25 19:26:31 -07002113 if (err)
2114 goto error_vport_exit;
2115
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002116 err = register_netdevice_notifier(&ovs_dp_device_notifier);
2117 if (err)
2118 goto error_netns_exit;
2119
Thomas Graf62b9c8d2014-10-22 17:29:06 +02002120 err = ovs_netdev_init();
2121 if (err)
2122 goto error_unreg_notifier;
2123
Jesse Grossccb13522011-10-25 19:26:31 -07002124 err = dp_register_genl();
2125 if (err < 0)
Thomas Graf62b9c8d2014-10-22 17:29:06 +02002126 goto error_unreg_netdev;
Jesse Grossccb13522011-10-25 19:26:31 -07002127
Jesse Grossccb13522011-10-25 19:26:31 -07002128 return 0;
2129
Thomas Graf62b9c8d2014-10-22 17:29:06 +02002130error_unreg_netdev:
2131 ovs_netdev_exit();
Jesse Grossccb13522011-10-25 19:26:31 -07002132error_unreg_notifier:
2133 unregister_netdevice_notifier(&ovs_dp_device_notifier);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002134error_netns_exit:
2135 unregister_pernet_device(&ovs_net_ops);
Jesse Grossccb13522011-10-25 19:26:31 -07002136error_vport_exit:
2137 ovs_vport_exit();
2138error_flow_exit:
2139 ovs_flow_exit();
Jiri Pirko5b9e7e12014-06-26 09:58:26 +02002140error_unreg_rtnl_link:
2141 ovs_internal_dev_rtnl_link_unregister();
Andy Zhou971427f32014-09-15 19:37:25 -07002142error_action_fifos_exit:
2143 action_fifos_exit();
Jesse Grossccb13522011-10-25 19:26:31 -07002144error:
2145 return err;
2146}
2147
2148static void dp_cleanup(void)
2149{
Jesse Grossccb13522011-10-25 19:26:31 -07002150 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
Thomas Graf62b9c8d2014-10-22 17:29:06 +02002151 ovs_netdev_exit();
Jesse Grossccb13522011-10-25 19:26:31 -07002152 unregister_netdevice_notifier(&ovs_dp_device_notifier);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002153 unregister_pernet_device(&ovs_net_ops);
2154 rcu_barrier();
Jesse Grossccb13522011-10-25 19:26:31 -07002155 ovs_vport_exit();
2156 ovs_flow_exit();
Jiri Pirko5b9e7e12014-06-26 09:58:26 +02002157 ovs_internal_dev_rtnl_link_unregister();
Andy Zhou971427f32014-09-15 19:37:25 -07002158 action_fifos_exit();
Jesse Grossccb13522011-10-25 19:26:31 -07002159}
2160
2161module_init(dp_init);
2162module_exit(dp_cleanup);
2163
2164MODULE_DESCRIPTION("Open vSwitch switching datapath");
2165MODULE_LICENSE("GPL");