blob: c2ac340e19fbfd69d6890ac012e866da2af4d105 [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;
Pravin B Shelar9ba559d92014-11-06 06:44:27 -080062EXPORT_SYMBOL_GPL(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}
Pravin B Shelar9ba559d92014-11-06 06:44:27 -0800134EXPORT_SYMBOL_GPL(lockdep_ovsl_is_held);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700135#endif
136
Jesse Grossccb13522011-10-25 19:26:31 -0700137static struct vport *new_vport(const struct vport_parms *);
Thomas Graf8055a892013-12-13 15:22:20 +0100138static int queue_gso_packets(struct datapath *dp, struct sk_buff *,
Jesse Grossccb13522011-10-25 19:26:31 -0700139 const struct dp_upcall_info *);
Thomas Graf8055a892013-12-13 15:22:20 +0100140static int queue_userspace_packet(struct datapath *dp, struct sk_buff *,
Jesse Grossccb13522011-10-25 19:26:31 -0700141 const struct dp_upcall_info *);
142
Andy Zhoucc3a5ae2014-09-08 13:14:22 -0700143/* Must be called with rcu_read_lock. */
144static struct datapath *get_dp_rcu(struct net *net, int dp_ifindex)
Jesse Grossccb13522011-10-25 19:26:31 -0700145{
Andy Zhoucc3a5ae2014-09-08 13:14:22 -0700146 struct net_device *dev = dev_get_by_index_rcu(net, dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -0700147
Jesse Grossccb13522011-10-25 19:26:31 -0700148 if (dev) {
149 struct vport *vport = ovs_internal_dev_get_vport(dev);
150 if (vport)
Andy Zhoucc3a5ae2014-09-08 13:14:22 -0700151 return vport->dp;
Jesse Grossccb13522011-10-25 19:26:31 -0700152 }
Andy Zhoucc3a5ae2014-09-08 13:14:22 -0700153
154 return NULL;
155}
156
157/* The caller must hold either ovs_mutex or rcu_read_lock to keep the
158 * returned dp pointer valid.
159 */
160static inline struct datapath *get_dp(struct net *net, int dp_ifindex)
161{
162 struct datapath *dp;
163
164 WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_ovsl_is_held());
165 rcu_read_lock();
166 dp = get_dp_rcu(net, dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -0700167 rcu_read_unlock();
168
169 return dp;
170}
171
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700172/* Must be called with rcu_read_lock or ovs_mutex. */
Andy Zhou971427f32014-09-15 19:37:25 -0700173const char *ovs_dp_name(const struct datapath *dp)
Jesse Grossccb13522011-10-25 19:26:31 -0700174{
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700175 struct vport *vport = ovs_vport_ovsl_rcu(dp, OVSP_LOCAL);
Jesse Grossccb13522011-10-25 19:26:31 -0700176 return vport->ops->get_name(vport);
177}
178
179static int get_dpifindex(struct datapath *dp)
180{
181 struct vport *local;
182 int ifindex;
183
184 rcu_read_lock();
185
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700186 local = ovs_vport_rcu(dp, OVSP_LOCAL);
Jesse Grossccb13522011-10-25 19:26:31 -0700187 if (local)
Thomas Grafcff63a52013-04-29 13:06:41 +0000188 ifindex = netdev_vport_priv(local)->dev->ifindex;
Jesse Grossccb13522011-10-25 19:26:31 -0700189 else
190 ifindex = 0;
191
192 rcu_read_unlock();
193
194 return ifindex;
195}
196
197static void destroy_dp_rcu(struct rcu_head *rcu)
198{
199 struct datapath *dp = container_of(rcu, struct datapath, rcu);
200
Pravin B Shelar9b996e52014-05-06 18:41:20 -0700201 ovs_flow_tbl_destroy(&dp->table);
Jesse Grossccb13522011-10-25 19:26:31 -0700202 free_percpu(dp->stats_percpu);
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800203 release_net(ovs_dp_get_net(dp));
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700204 kfree(dp->ports);
Jesse Grossccb13522011-10-25 19:26:31 -0700205 kfree(dp);
206}
207
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700208static struct hlist_head *vport_hash_bucket(const struct datapath *dp,
209 u16 port_no)
210{
211 return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)];
212}
213
Jarno Rajahalmebb6f9a72014-05-05 11:32:17 -0700214/* Called with ovs_mutex or RCU read lock. */
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700215struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no)
216{
217 struct vport *vport;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700218 struct hlist_head *head;
219
220 head = vport_hash_bucket(dp, port_no);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800221 hlist_for_each_entry_rcu(vport, head, dp_hash_node) {
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700222 if (vport->port_no == port_no)
223 return vport;
224 }
225 return NULL;
226}
227
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700228/* Called with ovs_mutex. */
Jesse Grossccb13522011-10-25 19:26:31 -0700229static struct vport *new_vport(const struct vport_parms *parms)
230{
231 struct vport *vport;
232
233 vport = ovs_vport_add(parms);
234 if (!IS_ERR(vport)) {
235 struct datapath *dp = parms->dp;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700236 struct hlist_head *head = vport_hash_bucket(dp, vport->port_no);
Jesse Grossccb13522011-10-25 19:26:31 -0700237
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700238 hlist_add_head_rcu(&vport->dp_hash_node, head);
Jesse Grossccb13522011-10-25 19:26:31 -0700239 }
Jesse Grossccb13522011-10-25 19:26:31 -0700240 return vport;
241}
242
Jesse Grossccb13522011-10-25 19:26:31 -0700243void ovs_dp_detach_port(struct vport *p)
244{
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700245 ASSERT_OVSL();
Jesse Grossccb13522011-10-25 19:26:31 -0700246
247 /* First drop references to device. */
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700248 hlist_del_rcu(&p->dp_hash_node);
Jesse Grossccb13522011-10-25 19:26:31 -0700249
250 /* Then destroy it. */
251 ovs_vport_del(p);
252}
253
254/* Must be called with rcu_read_lock. */
Pravin B Shelar8c8b1b82014-09-15 19:28:44 -0700255void ovs_dp_process_packet(struct sk_buff *skb, struct sw_flow_key *key)
Jesse Grossccb13522011-10-25 19:26:31 -0700256{
Pravin B Shelar83c8df22014-09-15 19:20:31 -0700257 const struct vport *p = OVS_CB(skb)->input_vport;
Jesse Grossccb13522011-10-25 19:26:31 -0700258 struct datapath *dp = p->dp;
259 struct sw_flow *flow;
Lorand Jakabd98612b2014-10-06 05:45:32 -0700260 struct sw_flow_actions *sf_acts;
Jesse Grossccb13522011-10-25 19:26:31 -0700261 struct dp_stats_percpu *stats;
Jesse Grossccb13522011-10-25 19:26:31 -0700262 u64 *stats_counter;
Andy Zhou1bd71162013-10-22 10:42:46 -0700263 u32 n_mask_hit;
Jesse Grossccb13522011-10-25 19:26:31 -0700264
Shan Wei404f2f12012-11-13 09:52:25 +0800265 stats = this_cpu_ptr(dp->stats_percpu);
Jesse Grossccb13522011-10-25 19:26:31 -0700266
Jesse Grossccb13522011-10-25 19:26:31 -0700267 /* Look up flow. */
Pravin B Shelar8c8b1b82014-09-15 19:28:44 -0700268 flow = ovs_flow_tbl_lookup_stats(&dp->table, key, &n_mask_hit);
Jesse Grossccb13522011-10-25 19:26:31 -0700269 if (unlikely(!flow)) {
270 struct dp_upcall_info upcall;
Pravin B Shelar8c8b1b82014-09-15 19:28:44 -0700271 int error;
Jesse Grossccb13522011-10-25 19:26:31 -0700272
273 upcall.cmd = OVS_PACKET_CMD_MISS;
Pravin B Shelar8c8b1b82014-09-15 19:28:44 -0700274 upcall.key = key;
Jesse Grossccb13522011-10-25 19:26:31 -0700275 upcall.userdata = NULL;
Alex Wang5cd667b2014-07-17 15:14:13 -0700276 upcall.portid = ovs_vport_find_upcall_portid(p, skb);
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800277 upcall.egress_tun_info = NULL;
Li RongQingc5eba0b2014-09-03 17:43:45 +0800278 error = ovs_dp_upcall(dp, skb, &upcall);
279 if (unlikely(error))
280 kfree_skb(skb);
281 else
282 consume_skb(skb);
Jesse Grossccb13522011-10-25 19:26:31 -0700283 stats_counter = &stats->n_missed;
284 goto out;
285 }
286
Lorand Jakabd98612b2014-10-06 05:45:32 -0700287 ovs_flow_stats_update(flow, key->tp.flags, skb);
288 sf_acts = rcu_dereference(flow->sf_acts);
289 ovs_execute_actions(dp, skb, sf_acts, key);
Jesse Grossccb13522011-10-25 19:26:31 -0700290
Pravin B Shelare298e502013-10-29 17:22:21 -0700291 stats_counter = &stats->n_hit;
Jesse Grossccb13522011-10-25 19:26:31 -0700292
293out:
294 /* Update datapath statistics. */
WANG Congdf9d9fd2014-02-14 15:10:46 -0800295 u64_stats_update_begin(&stats->syncp);
Jesse Grossccb13522011-10-25 19:26:31 -0700296 (*stats_counter)++;
Andy Zhou1bd71162013-10-22 10:42:46 -0700297 stats->n_mask_hit += n_mask_hit;
WANG Congdf9d9fd2014-02-14 15:10:46 -0800298 u64_stats_update_end(&stats->syncp);
Jesse Grossccb13522011-10-25 19:26:31 -0700299}
300
Jesse Grossccb13522011-10-25 19:26:31 -0700301int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800302 const struct dp_upcall_info *upcall_info)
Jesse Grossccb13522011-10-25 19:26:31 -0700303{
304 struct dp_stats_percpu *stats;
Jesse Grossccb13522011-10-25 19:26:31 -0700305 int err;
306
Eric W. Biederman15e47302012-09-07 20:12:54 +0000307 if (upcall_info->portid == 0) {
Jesse Grossccb13522011-10-25 19:26:31 -0700308 err = -ENOTCONN;
309 goto err;
310 }
311
Jesse Grossccb13522011-10-25 19:26:31 -0700312 if (!skb_is_gso(skb))
Thomas Graf8055a892013-12-13 15:22:20 +0100313 err = queue_userspace_packet(dp, skb, upcall_info);
Jesse Grossccb13522011-10-25 19:26:31 -0700314 else
Thomas Graf8055a892013-12-13 15:22:20 +0100315 err = queue_gso_packets(dp, skb, upcall_info);
Jesse Grossccb13522011-10-25 19:26:31 -0700316 if (err)
317 goto err;
318
319 return 0;
320
321err:
Shan Wei404f2f12012-11-13 09:52:25 +0800322 stats = this_cpu_ptr(dp->stats_percpu);
Jesse Grossccb13522011-10-25 19:26:31 -0700323
WANG Congdf9d9fd2014-02-14 15:10:46 -0800324 u64_stats_update_begin(&stats->syncp);
Jesse Grossccb13522011-10-25 19:26:31 -0700325 stats->n_lost++;
WANG Congdf9d9fd2014-02-14 15:10:46 -0800326 u64_stats_update_end(&stats->syncp);
Jesse Grossccb13522011-10-25 19:26:31 -0700327
328 return err;
329}
330
Thomas Graf8055a892013-12-13 15:22:20 +0100331static int queue_gso_packets(struct datapath *dp, struct sk_buff *skb,
Jesse Grossccb13522011-10-25 19:26:31 -0700332 const struct dp_upcall_info *upcall_info)
333{
Ben Pfaffa1b5d0d2012-07-20 14:47:54 -0700334 unsigned short gso_type = skb_shinfo(skb)->gso_type;
Jesse Grossccb13522011-10-25 19:26:31 -0700335 struct dp_upcall_info later_info;
336 struct sw_flow_key later_key;
337 struct sk_buff *segs, *nskb;
338 int err;
339
Thomas Graf09c5e602013-12-13 15:22:22 +0100340 segs = __skb_gso_segment(skb, NETIF_F_SG, false);
Pravin B Shelar92e5dfc2012-07-20 14:46:29 -0700341 if (IS_ERR(segs))
342 return PTR_ERR(segs);
Florian Westphal330966e2014-10-20 13:49:17 +0200343 if (segs == NULL)
344 return -EINVAL;
Jesse Grossccb13522011-10-25 19:26:31 -0700345
346 /* Queue all of the segments. */
347 skb = segs;
348 do {
Thomas Graf8055a892013-12-13 15:22:20 +0100349 err = queue_userspace_packet(dp, skb, upcall_info);
Jesse Grossccb13522011-10-25 19:26:31 -0700350 if (err)
351 break;
352
Ben Pfaffa1b5d0d2012-07-20 14:47:54 -0700353 if (skb == segs && gso_type & SKB_GSO_UDP) {
Jesse Grossccb13522011-10-25 19:26:31 -0700354 /* The initial flow key extracted by ovs_flow_extract()
355 * in this case is for a first fragment, so we need to
356 * properly mark later fragments.
357 */
358 later_key = *upcall_info->key;
359 later_key.ip.frag = OVS_FRAG_TYPE_LATER;
360
361 later_info = *upcall_info;
362 later_info.key = &later_key;
363 upcall_info = &later_info;
364 }
365 } while ((skb = skb->next));
366
367 /* Free all of the segments. */
368 skb = segs;
369 do {
370 nskb = skb->next;
371 if (err)
372 kfree_skb(skb);
373 else
374 consume_skb(skb);
375 } while ((skb = nskb));
376 return err;
377}
378
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800379static size_t upcall_msg_size(const struct dp_upcall_info *upcall_info,
Thomas Grafbda56f12013-12-13 15:22:21 +0100380 unsigned int hdrlen)
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100381{
382 size_t size = NLMSG_ALIGN(sizeof(struct ovs_header))
Thomas Grafbda56f12013-12-13 15:22:21 +0100383 + nla_total_size(hdrlen) /* OVS_PACKET_ATTR_PACKET */
Joe Stringer41af73e2014-10-18 16:14:14 -0700384 + nla_total_size(ovs_key_attr_size()); /* OVS_PACKET_ATTR_KEY */
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100385
386 /* OVS_PACKET_ATTR_USERDATA */
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800387 if (upcall_info->userdata)
388 size += NLA_ALIGN(upcall_info->userdata->nla_len);
389
390 /* OVS_PACKET_ATTR_EGRESS_TUN_KEY */
391 if (upcall_info->egress_tun_info)
392 size += nla_total_size(ovs_tun_key_attr_size());
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100393
394 return size;
395}
396
Thomas Graf8055a892013-12-13 15:22:20 +0100397static int queue_userspace_packet(struct datapath *dp, struct sk_buff *skb,
Jesse Grossccb13522011-10-25 19:26:31 -0700398 const struct dp_upcall_info *upcall_info)
399{
400 struct ovs_header *upcall;
401 struct sk_buff *nskb = NULL;
Li RongQing4ee45ea2014-09-02 20:52:28 +0800402 struct sk_buff *user_skb = NULL; /* to be queued to userspace */
Jesse Grossccb13522011-10-25 19:26:31 -0700403 struct nlattr *nla;
Thomas Graf795449d2013-11-30 13:21:32 +0100404 struct genl_info info = {
Thomas Graf8055a892013-12-13 15:22:20 +0100405 .dst_sk = ovs_dp_get_net(dp)->genl_sock,
Thomas Graf795449d2013-11-30 13:21:32 +0100406 .snd_portid = upcall_info->portid,
407 };
408 size_t len;
Thomas Grafbda56f12013-12-13 15:22:21 +0100409 unsigned int hlen;
Thomas Graf8055a892013-12-13 15:22:20 +0100410 int err, dp_ifindex;
411
412 dp_ifindex = get_dpifindex(dp);
413 if (!dp_ifindex)
414 return -ENODEV;
Jesse Grossccb13522011-10-25 19:26:31 -0700415
416 if (vlan_tx_tag_present(skb)) {
417 nskb = skb_clone(skb, GFP_ATOMIC);
418 if (!nskb)
419 return -ENOMEM;
420
Patrick McHardy86a9bad2013-04-19 02:04:30 +0000421 nskb = __vlan_put_tag(nskb, nskb->vlan_proto, vlan_tx_tag_get(nskb));
Dan Carpenter8aa51d62012-05-13 08:44:18 +0000422 if (!nskb)
Jesse Grossccb13522011-10-25 19:26:31 -0700423 return -ENOMEM;
424
425 nskb->vlan_tci = 0;
426 skb = nskb;
427 }
428
429 if (nla_attr_size(skb->len) > USHRT_MAX) {
430 err = -EFBIG;
431 goto out;
432 }
433
Thomas Grafbda56f12013-12-13 15:22:21 +0100434 /* Complete checksum if needed */
435 if (skb->ip_summed == CHECKSUM_PARTIAL &&
436 (err = skb_checksum_help(skb)))
437 goto out;
438
439 /* Older versions of OVS user space enforce alignment of the last
440 * Netlink attribute to NLA_ALIGNTO which would require extensive
441 * padding logic. Only perform zerocopy if padding is not required.
442 */
443 if (dp->user_features & OVS_DP_F_UNALIGNED)
444 hlen = skb_zerocopy_headlen(skb);
445 else
446 hlen = skb->len;
447
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800448 len = upcall_msg_size(upcall_info, hlen);
Thomas Graf795449d2013-11-30 13:21:32 +0100449 user_skb = genlmsg_new_unicast(len, &info, GFP_ATOMIC);
Jesse Grossccb13522011-10-25 19:26:31 -0700450 if (!user_skb) {
451 err = -ENOMEM;
452 goto out;
453 }
454
455 upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
456 0, upcall_info->cmd);
457 upcall->dp_ifindex = dp_ifindex;
458
459 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
Andy Zhouf53e3832014-07-17 15:17:44 -0700460 err = ovs_nla_put_flow(upcall_info->key, upcall_info->key, user_skb);
461 BUG_ON(err);
Jesse Grossccb13522011-10-25 19:26:31 -0700462 nla_nest_end(user_skb, nla);
463
464 if (upcall_info->userdata)
Ben Pfaff44901082013-02-15 17:29:22 -0800465 __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA,
466 nla_len(upcall_info->userdata),
467 nla_data(upcall_info->userdata));
Jesse Grossccb13522011-10-25 19:26:31 -0700468
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800469 if (upcall_info->egress_tun_info) {
470 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_EGRESS_TUN_KEY);
471 err = ovs_nla_put_egress_tunnel_key(user_skb,
472 upcall_info->egress_tun_info);
473 BUG_ON(err);
474 nla_nest_end(user_skb, nla);
475 }
476
Thomas Grafbda56f12013-12-13 15:22:21 +0100477 /* Only reserve room for attribute header, packet data is added
478 * in skb_zerocopy() */
479 if (!(nla = nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, 0))) {
480 err = -ENOBUFS;
481 goto out;
482 }
483 nla->nla_len = nla_attr_size(skb->len);
Jesse Grossccb13522011-10-25 19:26:31 -0700484
Zoltan Kiss36d5fe62014-03-26 22:37:45 +0000485 err = skb_zerocopy(user_skb, skb, skb->len, hlen);
486 if (err)
487 goto out;
Jesse Grossccb13522011-10-25 19:26:31 -0700488
Thomas Grafaea0bb42014-01-14 16:27:49 +0000489 /* Pad OVS_PACKET_ATTR_PACKET if linear copy was performed */
490 if (!(dp->user_features & OVS_DP_F_UNALIGNED)) {
491 size_t plen = NLA_ALIGN(user_skb->len) - user_skb->len;
492
493 if (plen > 0)
494 memset(skb_put(user_skb, plen), 0, plen);
495 }
496
Thomas Grafbda56f12013-12-13 15:22:21 +0100497 ((struct nlmsghdr *) user_skb->data)->nlmsg_len = user_skb->len;
498
Thomas Graf8055a892013-12-13 15:22:20 +0100499 err = genlmsg_unicast(ovs_dp_get_net(dp), user_skb, upcall_info->portid);
Li RongQing4ee45ea2014-09-02 20:52:28 +0800500 user_skb = NULL;
Jesse Grossccb13522011-10-25 19:26:31 -0700501out:
Zoltan Kiss36d5fe62014-03-26 22:37:45 +0000502 if (err)
503 skb_tx_error(skb);
Li RongQing4ee45ea2014-09-02 20:52:28 +0800504 kfree_skb(user_skb);
Jesse Grossccb13522011-10-25 19:26:31 -0700505 kfree_skb(nskb);
506 return err;
507}
508
Jesse Grossccb13522011-10-25 19:26:31 -0700509static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
510{
511 struct ovs_header *ovs_header = info->userhdr;
512 struct nlattr **a = info->attrs;
513 struct sw_flow_actions *acts;
514 struct sk_buff *packet;
515 struct sw_flow *flow;
Lorand Jakabd98612b2014-10-06 05:45:32 -0700516 struct sw_flow_actions *sf_acts;
Jesse Grossccb13522011-10-25 19:26:31 -0700517 struct datapath *dp;
518 struct ethhdr *eth;
Pravin B Shelar83c8df22014-09-15 19:20:31 -0700519 struct vport *input_vport;
Jesse Grossccb13522011-10-25 19:26:31 -0700520 int len;
521 int err;
Jesse Grossccb13522011-10-25 19:26:31 -0700522
523 err = -EINVAL;
524 if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
Thomas Grafdded45fc2013-03-29 14:46:47 +0100525 !a[OVS_PACKET_ATTR_ACTIONS])
Jesse Grossccb13522011-10-25 19:26:31 -0700526 goto err;
527
528 len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
529 packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
530 err = -ENOMEM;
531 if (!packet)
532 goto err;
533 skb_reserve(packet, NET_IP_ALIGN);
534
Thomas Graf32686a92013-03-29 14:46:48 +0100535 nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
Jesse Grossccb13522011-10-25 19:26:31 -0700536
537 skb_reset_mac_header(packet);
538 eth = eth_hdr(packet);
539
540 /* Normally, setting the skb 'protocol' field would be handled by a
541 * call to eth_type_trans(), but it assumes there's a sending
542 * device, which we may not have. */
Simon Hormane5c5d222013-03-28 13:38:25 +0900543 if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN)
Jesse Grossccb13522011-10-25 19:26:31 -0700544 packet->protocol = eth->h_proto;
545 else
546 packet->protocol = htons(ETH_P_802_2);
547
548 /* Build an sw_flow for sending this packet. */
Jarno Rajahalme23dabf82014-03-27 12:35:23 -0700549 flow = ovs_flow_alloc();
Jesse Grossccb13522011-10-25 19:26:31 -0700550 err = PTR_ERR(flow);
551 if (IS_ERR(flow))
552 goto err_kfree_skb;
553
Pravin B Shelar83c8df22014-09-15 19:20:31 -0700554 err = ovs_flow_key_extract_userspace(a[OVS_PACKET_ATTR_KEY], packet,
555 &flow->key);
Jesse Grossccb13522011-10-25 19:26:31 -0700556 if (err)
557 goto err_flow_free;
558
Pravin B Shelare6445712013-10-03 18:16:47 -0700559 err = ovs_nla_copy_actions(a[OVS_PACKET_ATTR_ACTIONS],
Simon Horman25cd9ba2014-10-06 05:05:13 -0700560 &flow->key, &acts);
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700561 if (err)
562 goto err_flow_free;
Jesse Grossccb13522011-10-25 19:26:31 -0700563
Jesse Grossf5796682014-10-03 15:35:33 -0700564 rcu_assign_pointer(flow->sf_acts, acts);
Jesse Grossf5796682014-10-03 15:35:33 -0700565 OVS_CB(packet)->egress_tun_info = NULL;
Jesse Grossccb13522011-10-25 19:26:31 -0700566 packet->priority = flow->key.phy.priority;
Ansis Atteka39c7caeb2012-11-26 11:24:11 -0800567 packet->mark = flow->key.phy.skb_mark;
Jesse Grossccb13522011-10-25 19:26:31 -0700568
569 rcu_read_lock();
Andy Zhoucc3a5ae2014-09-08 13:14:22 -0700570 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -0700571 err = -ENODEV;
572 if (!dp)
573 goto err_unlock;
574
Pravin B Shelar83c8df22014-09-15 19:20:31 -0700575 input_vport = ovs_vport_rcu(dp, flow->key.phy.in_port);
576 if (!input_vport)
577 input_vport = ovs_vport_rcu(dp, OVSP_LOCAL);
578
579 if (!input_vport)
580 goto err_unlock;
581
582 OVS_CB(packet)->input_vport = input_vport;
Lorand Jakabd98612b2014-10-06 05:45:32 -0700583 sf_acts = rcu_dereference(flow->sf_acts);
Pravin B Shelar83c8df22014-09-15 19:20:31 -0700584
Jesse Grossccb13522011-10-25 19:26:31 -0700585 local_bh_disable();
Lorand Jakabd98612b2014-10-06 05:45:32 -0700586 err = ovs_execute_actions(dp, packet, sf_acts, &flow->key);
Jesse Grossccb13522011-10-25 19:26:31 -0700587 local_bh_enable();
588 rcu_read_unlock();
589
Andy Zhou03f0d912013-08-07 20:01:00 -0700590 ovs_flow_free(flow, false);
Jesse Grossccb13522011-10-25 19:26:31 -0700591 return err;
592
593err_unlock:
594 rcu_read_unlock();
595err_flow_free:
Andy Zhou03f0d912013-08-07 20:01:00 -0700596 ovs_flow_free(flow, false);
Jesse Grossccb13522011-10-25 19:26:31 -0700597err_kfree_skb:
598 kfree_skb(packet);
599err:
600 return err;
601}
602
603static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
Thomas Grafdded45fc2013-03-29 14:46:47 +0100604 [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
Jesse Grossccb13522011-10-25 19:26:31 -0700605 [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
606 [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
607};
608
Johannes Berg4534de82013-11-14 17:14:46 +0100609static const struct genl_ops dp_packet_genl_ops[] = {
Jesse Grossccb13522011-10-25 19:26:31 -0700610 { .cmd = OVS_PACKET_CMD_EXECUTE,
611 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
612 .policy = packet_policy,
613 .doit = ovs_packet_cmd_execute
614 }
615};
616
Pravin B Shelar0c200ef2014-05-06 16:44:50 -0700617static struct genl_family dp_packet_genl_family = {
618 .id = GENL_ID_GENERATE,
619 .hdrsize = sizeof(struct ovs_header),
620 .name = OVS_PACKET_FAMILY,
621 .version = OVS_PACKET_VERSION,
622 .maxattr = OVS_PACKET_ATTR_MAX,
623 .netnsok = true,
624 .parallel_ops = true,
625 .ops = dp_packet_genl_ops,
626 .n_ops = ARRAY_SIZE(dp_packet_genl_ops),
627};
628
Andy Zhou1bd71162013-10-22 10:42:46 -0700629static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats,
630 struct ovs_dp_megaflow_stats *mega_stats)
Jesse Grossccb13522011-10-25 19:26:31 -0700631{
632 int i;
Jesse Grossccb13522011-10-25 19:26:31 -0700633
Andy Zhou1bd71162013-10-22 10:42:46 -0700634 memset(mega_stats, 0, sizeof(*mega_stats));
635
Pravin B Shelarb637e492013-10-04 00:14:23 -0700636 stats->n_flows = ovs_flow_tbl_count(&dp->table);
Andy Zhou1bd71162013-10-22 10:42:46 -0700637 mega_stats->n_masks = ovs_flow_tbl_num_masks(&dp->table);
Jesse Grossccb13522011-10-25 19:26:31 -0700638
639 stats->n_hit = stats->n_missed = stats->n_lost = 0;
Andy Zhou1bd71162013-10-22 10:42:46 -0700640
Jesse Grossccb13522011-10-25 19:26:31 -0700641 for_each_possible_cpu(i) {
642 const struct dp_stats_percpu *percpu_stats;
643 struct dp_stats_percpu local_stats;
644 unsigned int start;
645
646 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
647
648 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700649 start = u64_stats_fetch_begin_irq(&percpu_stats->syncp);
Jesse Grossccb13522011-10-25 19:26:31 -0700650 local_stats = *percpu_stats;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700651 } while (u64_stats_fetch_retry_irq(&percpu_stats->syncp, start));
Jesse Grossccb13522011-10-25 19:26:31 -0700652
653 stats->n_hit += local_stats.n_hit;
654 stats->n_missed += local_stats.n_missed;
655 stats->n_lost += local_stats.n_lost;
Andy Zhou1bd71162013-10-22 10:42:46 -0700656 mega_stats->n_mask_hit += local_stats.n_mask_hit;
Jesse Grossccb13522011-10-25 19:26:31 -0700657 }
658}
659
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100660static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts)
661{
662 return NLMSG_ALIGN(sizeof(struct ovs_header))
Joe Stringer41af73e2014-10-18 16:14:14 -0700663 + nla_total_size(ovs_key_attr_size()) /* OVS_FLOW_ATTR_KEY */
664 + nla_total_size(ovs_key_attr_size()) /* OVS_FLOW_ATTR_MASK */
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100665 + nla_total_size(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
666 + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
667 + nla_total_size(8) /* OVS_FLOW_ATTR_USED */
668 + nla_total_size(acts->actions_len); /* OVS_FLOW_ATTR_ACTIONS */
669}
670
Jarno Rajahalmebb6f9a72014-05-05 11:32:17 -0700671/* Called with ovs_mutex or RCU read lock. */
Joe Stringerca7105f2014-09-08 13:09:37 -0700672static int ovs_flow_cmd_fill_match(const struct sw_flow *flow,
673 struct sk_buff *skb)
Jesse Grossccb13522011-10-25 19:26:31 -0700674{
Jesse Grossccb13522011-10-25 19:26:31 -0700675 struct nlattr *nla;
Jesse Grossccb13522011-10-25 19:26:31 -0700676 int err;
677
Andy Zhou03f0d912013-08-07 20:01:00 -0700678 /* Fill flow key. */
Jesse Grossccb13522011-10-25 19:26:31 -0700679 nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
680 if (!nla)
Joe Stringerca7105f2014-09-08 13:09:37 -0700681 return -EMSGSIZE;
Andy Zhou03f0d912013-08-07 20:01:00 -0700682
Pravin B Shelare6445712013-10-03 18:16:47 -0700683 err = ovs_nla_put_flow(&flow->unmasked_key, &flow->unmasked_key, skb);
Jesse Grossccb13522011-10-25 19:26:31 -0700684 if (err)
Joe Stringerca7105f2014-09-08 13:09:37 -0700685 return err;
686
Jesse Grossccb13522011-10-25 19:26:31 -0700687 nla_nest_end(skb, nla);
688
Joe Stringerca7105f2014-09-08 13:09:37 -0700689 /* Fill flow mask. */
Andy Zhou03f0d912013-08-07 20:01:00 -0700690 nla = nla_nest_start(skb, OVS_FLOW_ATTR_MASK);
691 if (!nla)
Joe Stringerca7105f2014-09-08 13:09:37 -0700692 return -EMSGSIZE;
Andy Zhou03f0d912013-08-07 20:01:00 -0700693
Pravin B Shelare6445712013-10-03 18:16:47 -0700694 err = ovs_nla_put_flow(&flow->key, &flow->mask->key, skb);
Andy Zhou03f0d912013-08-07 20:01:00 -0700695 if (err)
Joe Stringerca7105f2014-09-08 13:09:37 -0700696 return err;
Andy Zhou03f0d912013-08-07 20:01:00 -0700697
698 nla_nest_end(skb, nla);
Joe Stringerca7105f2014-09-08 13:09:37 -0700699 return 0;
700}
701
702/* Called with ovs_mutex or RCU read lock. */
703static int ovs_flow_cmd_fill_stats(const struct sw_flow *flow,
704 struct sk_buff *skb)
705{
706 struct ovs_flow_stats stats;
707 __be16 tcp_flags;
708 unsigned long used;
Andy Zhou03f0d912013-08-07 20:01:00 -0700709
Pravin B Shelare298e502013-10-29 17:22:21 -0700710 ovs_flow_stats_get(flow, &stats, &used, &tcp_flags);
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700711
David S. Miller028d6a62012-03-29 23:20:48 -0400712 if (used &&
713 nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
Joe Stringerca7105f2014-09-08 13:09:37 -0700714 return -EMSGSIZE;
Jesse Grossccb13522011-10-25 19:26:31 -0700715
David S. Miller028d6a62012-03-29 23:20:48 -0400716 if (stats.n_packets &&
Pravin B Shelare298e502013-10-29 17:22:21 -0700717 nla_put(skb, OVS_FLOW_ATTR_STATS, sizeof(struct ovs_flow_stats), &stats))
Joe Stringerca7105f2014-09-08 13:09:37 -0700718 return -EMSGSIZE;
Jesse Grossccb13522011-10-25 19:26:31 -0700719
Pravin B Shelare298e502013-10-29 17:22:21 -0700720 if ((u8)ntohs(tcp_flags) &&
721 nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, (u8)ntohs(tcp_flags)))
Joe Stringerca7105f2014-09-08 13:09:37 -0700722 return -EMSGSIZE;
723
724 return 0;
725}
726
727/* Called with ovs_mutex or RCU read lock. */
728static int ovs_flow_cmd_fill_actions(const struct sw_flow *flow,
729 struct sk_buff *skb, int skb_orig_len)
730{
731 struct nlattr *start;
732 int err;
Jesse Grossccb13522011-10-25 19:26:31 -0700733
734 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
735 * this is the first flow to be dumped into 'skb'. This is unusual for
736 * Netlink but individual action lists can be longer than
737 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
738 * The userspace caller can always fetch the actions separately if it
739 * really wants them. (Most userspace callers in fact don't care.)
740 *
741 * This can only fail for dump operations because the skb is always
742 * properly sized for single flows.
743 */
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700744 start = nla_nest_start(skb, OVS_FLOW_ATTR_ACTIONS);
745 if (start) {
Pravin B Shelard57170b2013-07-30 15:39:39 -0700746 const struct sw_flow_actions *sf_acts;
747
Jesse Gross663efa32013-12-03 10:58:53 -0800748 sf_acts = rcu_dereference_ovsl(flow->sf_acts);
Pravin B Shelare6445712013-10-03 18:16:47 -0700749 err = ovs_nla_put_actions(sf_acts->actions,
750 sf_acts->actions_len, skb);
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700751
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700752 if (!err)
753 nla_nest_end(skb, start);
754 else {
755 if (skb_orig_len)
Joe Stringerca7105f2014-09-08 13:09:37 -0700756 return err;
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700757
758 nla_nest_cancel(skb, start);
759 }
Joe Stringerca7105f2014-09-08 13:09:37 -0700760 } else if (skb_orig_len) {
761 return -EMSGSIZE;
762 }
763
764 return 0;
765}
766
767/* Called with ovs_mutex or RCU read lock. */
768static int ovs_flow_cmd_fill_info(const struct sw_flow *flow, int dp_ifindex,
769 struct sk_buff *skb, u32 portid,
770 u32 seq, u32 flags, u8 cmd)
771{
772 const int skb_orig_len = skb->len;
773 struct ovs_header *ovs_header;
774 int err;
775
776 ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family,
777 flags, cmd);
778 if (!ovs_header)
779 return -EMSGSIZE;
780
781 ovs_header->dp_ifindex = dp_ifindex;
782
783 err = ovs_flow_cmd_fill_match(flow, skb);
784 if (err)
785 goto error;
786
787 err = ovs_flow_cmd_fill_stats(flow, skb);
788 if (err)
789 goto error;
790
791 err = ovs_flow_cmd_fill_actions(flow, skb, skb_orig_len);
792 if (err)
793 goto error;
Jesse Grossccb13522011-10-25 19:26:31 -0700794
795 return genlmsg_end(skb, ovs_header);
796
Jesse Grossccb13522011-10-25 19:26:31 -0700797error:
798 genlmsg_cancel(skb, ovs_header);
799 return err;
800}
801
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700802/* May not be called with RCU read lock. */
803static struct sk_buff *ovs_flow_cmd_alloc_info(const struct sw_flow_actions *acts,
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700804 struct genl_info *info,
805 bool always)
Jesse Grossccb13522011-10-25 19:26:31 -0700806{
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700807 struct sk_buff *skb;
Jesse Grossccb13522011-10-25 19:26:31 -0700808
Samuel Gauthier9b67aa42014-09-18 10:31:04 +0200809 if (!always && !ovs_must_notify(&dp_flow_genl_family, info, 0))
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700810 return NULL;
811
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700812 skb = genlmsg_new_unicast(ovs_flow_cmd_msg_size(acts), info, GFP_KERNEL);
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700813 if (!skb)
814 return ERR_PTR(-ENOMEM);
815
816 return skb;
Jesse Grossccb13522011-10-25 19:26:31 -0700817}
818
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700819/* Called with ovs_mutex. */
820static struct sk_buff *ovs_flow_cmd_build_info(const struct sw_flow *flow,
821 int dp_ifindex,
822 struct genl_info *info, u8 cmd,
823 bool always)
Jesse Grossccb13522011-10-25 19:26:31 -0700824{
825 struct sk_buff *skb;
826 int retval;
827
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700828 skb = ovs_flow_cmd_alloc_info(ovsl_dereference(flow->sf_acts), info,
829 always);
Himangi Saraogid0e992a2014-07-27 12:37:46 +0530830 if (IS_ERR_OR_NULL(skb))
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700831 return skb;
Jesse Grossccb13522011-10-25 19:26:31 -0700832
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700833 retval = ovs_flow_cmd_fill_info(flow, dp_ifindex, skb,
834 info->snd_portid, info->snd_seq, 0,
835 cmd);
Jesse Grossccb13522011-10-25 19:26:31 -0700836 BUG_ON(retval < 0);
837 return skb;
838}
839
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700840static int ovs_flow_cmd_new(struct sk_buff *skb, struct genl_info *info)
Jesse Grossccb13522011-10-25 19:26:31 -0700841{
842 struct nlattr **a = info->attrs;
843 struct ovs_header *ovs_header = info->userhdr;
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700844 struct sw_flow *flow, *new_flow;
Andy Zhou03f0d912013-08-07 20:01:00 -0700845 struct sw_flow_mask mask;
Jesse Grossccb13522011-10-25 19:26:31 -0700846 struct sk_buff *reply;
847 struct datapath *dp;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700848 struct sw_flow_actions *acts;
849 struct sw_flow_match match;
850 int error;
851
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700852 /* Must have key and actions. */
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700853 error = -EINVAL;
Jesse Gross426cda52014-10-06 05:08:38 -0700854 if (!a[OVS_FLOW_ATTR_KEY]) {
855 OVS_NLERR("Flow key attribute not present in new flow.\n");
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700856 goto error;
Jesse Gross426cda52014-10-06 05:08:38 -0700857 }
858 if (!a[OVS_FLOW_ATTR_ACTIONS]) {
859 OVS_NLERR("Flow actions attribute not present in new flow.\n");
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700860 goto error;
Jesse Gross426cda52014-10-06 05:08:38 -0700861 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700862
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700863 /* Most of the time we need to allocate a new flow, do it before
864 * locking.
865 */
866 new_flow = ovs_flow_alloc();
867 if (IS_ERR(new_flow)) {
868 error = PTR_ERR(new_flow);
869 goto error;
870 }
871
872 /* Extract key. */
873 ovs_match_init(&match, &new_flow->unmasked_key, &mask);
874 error = ovs_nla_get_match(&match,
875 a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
876 if (error)
877 goto err_kfree_flow;
878
879 ovs_flow_mask_key(&new_flow->key, &new_flow->unmasked_key, &mask);
880
881 /* Validate actions. */
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700882 error = ovs_nla_copy_actions(a[OVS_FLOW_ATTR_ACTIONS], &new_flow->key,
Simon Horman25cd9ba2014-10-06 05:05:13 -0700883 &acts);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700884 if (error) {
885 OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
Pravin B Shelar2fdb9572014-10-19 11:19:51 -0700886 goto err_kfree_flow;
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700887 }
888
889 reply = ovs_flow_cmd_alloc_info(acts, info, false);
890 if (IS_ERR(reply)) {
891 error = PTR_ERR(reply);
892 goto err_kfree_acts;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700893 }
894
895 ovs_lock();
896 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700897 if (unlikely(!dp)) {
898 error = -ENODEV;
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 /* Check if this is a duplicate flow */
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700902 flow = ovs_flow_tbl_lookup(&dp->table, &new_flow->unmasked_key);
903 if (likely(!flow)) {
904 rcu_assign_pointer(new_flow->sf_acts, acts);
905
906 /* Put flow in bucket. */
907 error = ovs_flow_tbl_insert(&dp->table, new_flow, &mask);
908 if (unlikely(error)) {
909 acts = NULL;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700910 goto err_unlock_ovs;
911 }
912
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700913 if (unlikely(reply)) {
914 error = ovs_flow_cmd_fill_info(new_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);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700920 }
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700921 ovs_unlock();
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700922 } else {
923 struct sw_flow_actions *old_acts;
924
925 /* Bail out if we're not allowed to modify an existing flow.
926 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
927 * because Generic Netlink treats the latter as a dump
928 * request. We also accept NLM_F_EXCL in case that bug ever
929 * gets fixed.
930 */
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700931 if (unlikely(info->nlhdr->nlmsg_flags & (NLM_F_CREATE
932 | NLM_F_EXCL))) {
933 error = -EEXIST;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700934 goto err_unlock_ovs;
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700935 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700936 /* The unmasked key has to be the same for flow updates. */
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700937 if (unlikely(!ovs_flow_cmp_unmasked_key(flow, &match))) {
Alex Wang4a46b242014-06-30 20:30:29 -0700938 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
939 if (!flow) {
940 error = -ENOENT;
941 goto err_unlock_ovs;
942 }
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700943 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700944 /* Update actions. */
945 old_acts = ovsl_dereference(flow->sf_acts);
946 rcu_assign_pointer(flow->sf_acts, acts);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700947
948 if (unlikely(reply)) {
949 error = ovs_flow_cmd_fill_info(flow,
950 ovs_header->dp_ifindex,
951 reply, info->snd_portid,
952 info->snd_seq, 0,
953 OVS_FLOW_CMD_NEW);
954 BUG_ON(error < 0);
955 }
956 ovs_unlock();
957
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700958 ovs_nla_free_flow_actions(old_acts);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700959 ovs_flow_free(new_flow, false);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700960 }
961
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700962 if (reply)
963 ovs_notify(&dp_flow_genl_family, reply, info);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700964 return 0;
965
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700966err_unlock_ovs:
967 ovs_unlock();
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700968 kfree_skb(reply);
969err_kfree_acts:
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700970 kfree(acts);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700971err_kfree_flow:
972 ovs_flow_free(new_flow, false);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700973error:
974 return error;
975}
976
Pravin B Shelar2fdb9572014-10-19 11:19:51 -0700977/* Factor out action copy to avoid "Wframe-larger-than=1024" warning. */
Jesse Gross6b205b22014-10-03 15:35:32 -0700978static struct sw_flow_actions *get_flow_actions(const struct nlattr *a,
979 const struct sw_flow_key *key,
980 const struct sw_flow_mask *mask)
981{
982 struct sw_flow_actions *acts;
983 struct sw_flow_key masked_key;
984 int error;
985
Jesse Gross6b205b22014-10-03 15:35:32 -0700986 ovs_flow_mask_key(&masked_key, key, mask);
Simon Horman25cd9ba2014-10-06 05:05:13 -0700987 error = ovs_nla_copy_actions(a, &masked_key, &acts);
Jesse Gross6b205b22014-10-03 15:35:32 -0700988 if (error) {
Pravin B Shelar2fdb9572014-10-19 11:19:51 -0700989 OVS_NLERR("Actions may not be safe on all matching packets.\n");
Jesse Gross6b205b22014-10-03 15:35:32 -0700990 return ERR_PTR(error);
991 }
992
993 return acts;
994}
995
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700996static int ovs_flow_cmd_set(struct sk_buff *skb, struct genl_info *info)
997{
998 struct nlattr **a = info->attrs;
999 struct ovs_header *ovs_header = info->userhdr;
Jesse Gross6b205b22014-10-03 15:35:32 -07001000 struct sw_flow_key key;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001001 struct sw_flow *flow;
1002 struct sw_flow_mask mask;
1003 struct sk_buff *reply = NULL;
1004 struct datapath *dp;
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001005 struct sw_flow_actions *old_acts = NULL, *acts = NULL;
Andy Zhou03f0d912013-08-07 20:01:00 -07001006 struct sw_flow_match match;
Jesse Grossccb13522011-10-25 19:26:31 -07001007 int error;
Jesse Grossccb13522011-10-25 19:26:31 -07001008
1009 /* Extract key. */
1010 error = -EINVAL;
Jesse Gross426cda52014-10-06 05:08:38 -07001011 if (!a[OVS_FLOW_ATTR_KEY]) {
1012 OVS_NLERR("Flow key attribute not present in set flow.\n");
Jesse Grossccb13522011-10-25 19:26:31 -07001013 goto error;
Jesse Gross426cda52014-10-06 05:08:38 -07001014 }
Andy Zhou03f0d912013-08-07 20:01:00 -07001015
1016 ovs_match_init(&match, &key, &mask);
Jarno Rajahalme23dabf82014-03-27 12:35:23 -07001017 error = ovs_nla_get_match(&match,
Pravin B Shelare6445712013-10-03 18:16:47 -07001018 a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
Jesse Grossccb13522011-10-25 19:26:31 -07001019 if (error)
1020 goto error;
1021
1022 /* Validate actions. */
1023 if (a[OVS_FLOW_ATTR_ACTIONS]) {
Jesse Gross6b205b22014-10-03 15:35:32 -07001024 acts = get_flow_actions(a[OVS_FLOW_ATTR_ACTIONS], &key, &mask);
1025 if (IS_ERR(acts)) {
1026 error = PTR_ERR(acts);
Jesse Grossccb13522011-10-25 19:26:31 -07001027 goto error;
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001028 }
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001029
Pravin B Shelar2fdb9572014-10-19 11:19:51 -07001030 /* Can allocate before locking if have acts. */
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001031 reply = ovs_flow_cmd_alloc_info(acts, info, false);
1032 if (IS_ERR(reply)) {
1033 error = PTR_ERR(reply);
1034 goto err_kfree_acts;
Andy Zhou03f0d912013-08-07 20:01:00 -07001035 }
Jesse Grossccb13522011-10-25 19:26:31 -07001036 }
1037
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001038 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001039 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001040 if (unlikely(!dp)) {
1041 error = -ENODEV;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001042 goto err_unlock_ovs;
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001043 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001044 /* Check that the flow exists. */
Alex Wang4a46b242014-06-30 20:30:29 -07001045 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001046 if (unlikely(!flow)) {
1047 error = -ENOENT;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001048 goto err_unlock_ovs;
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001049 }
Alex Wang4a46b242014-06-30 20:30:29 -07001050
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001051 /* Update actions, if present. */
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001052 if (likely(acts)) {
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001053 old_acts = ovsl_dereference(flow->sf_acts);
Jesse Grossccb13522011-10-25 19:26:31 -07001054 rcu_assign_pointer(flow->sf_acts, acts);
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001055
1056 if (unlikely(reply)) {
1057 error = ovs_flow_cmd_fill_info(flow,
1058 ovs_header->dp_ifindex,
1059 reply, info->snd_portid,
1060 info->snd_seq, 0,
1061 OVS_FLOW_CMD_NEW);
1062 BUG_ON(error < 0);
1063 }
1064 } else {
1065 /* Could not alloc without acts before locking. */
1066 reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex,
1067 info, OVS_FLOW_CMD_NEW, false);
1068 if (unlikely(IS_ERR(reply))) {
1069 error = PTR_ERR(reply);
1070 goto err_unlock_ovs;
1071 }
Jesse Grossccb13522011-10-25 19:26:31 -07001072 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001073
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001074 /* Clear stats. */
1075 if (a[OVS_FLOW_ATTR_CLEAR])
1076 ovs_flow_stats_clear(flow);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001077 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001078
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001079 if (reply)
1080 ovs_notify(&dp_flow_genl_family, reply, info);
1081 if (old_acts)
1082 ovs_nla_free_flow_actions(old_acts);
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -07001083
Jesse Grossccb13522011-10-25 19:26:31 -07001084 return 0;
1085
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001086err_unlock_ovs:
1087 ovs_unlock();
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001088 kfree_skb(reply);
1089err_kfree_acts:
Pravin B Shelar74f84a52013-06-17 17:50:12 -07001090 kfree(acts);
Jesse Grossccb13522011-10-25 19:26:31 -07001091error:
1092 return error;
1093}
1094
1095static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1096{
1097 struct nlattr **a = info->attrs;
1098 struct ovs_header *ovs_header = info->userhdr;
1099 struct sw_flow_key key;
1100 struct sk_buff *reply;
1101 struct sw_flow *flow;
1102 struct datapath *dp;
Andy Zhou03f0d912013-08-07 20:01:00 -07001103 struct sw_flow_match match;
Jesse Grossccb13522011-10-25 19:26:31 -07001104 int err;
Jesse Grossccb13522011-10-25 19:26:31 -07001105
Andy Zhou03f0d912013-08-07 20:01:00 -07001106 if (!a[OVS_FLOW_ATTR_KEY]) {
1107 OVS_NLERR("Flow get message rejected, Key attribute missing.\n");
Jesse Grossccb13522011-10-25 19:26:31 -07001108 return -EINVAL;
Andy Zhou03f0d912013-08-07 20:01:00 -07001109 }
1110
1111 ovs_match_init(&match, &key, NULL);
Jarno Rajahalme23dabf82014-03-27 12:35:23 -07001112 err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL);
Jesse Grossccb13522011-10-25 19:26:31 -07001113 if (err)
1114 return err;
1115
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001116 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001117 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001118 if (!dp) {
1119 err = -ENODEV;
1120 goto unlock;
1121 }
Jesse Grossccb13522011-10-25 19:26:31 -07001122
Alex Wang4a46b242014-06-30 20:30:29 -07001123 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1124 if (!flow) {
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001125 err = -ENOENT;
1126 goto unlock;
1127 }
Jesse Grossccb13522011-10-25 19:26:31 -07001128
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -07001129 reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex, info,
1130 OVS_FLOW_CMD_NEW, true);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001131 if (IS_ERR(reply)) {
1132 err = PTR_ERR(reply);
1133 goto unlock;
1134 }
Jesse Grossccb13522011-10-25 19:26:31 -07001135
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001136 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001137 return genlmsg_reply(reply, info);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001138unlock:
1139 ovs_unlock();
1140 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001141}
1142
1143static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1144{
1145 struct nlattr **a = info->attrs;
1146 struct ovs_header *ovs_header = info->userhdr;
1147 struct sw_flow_key key;
1148 struct sk_buff *reply;
1149 struct sw_flow *flow;
1150 struct datapath *dp;
Andy Zhou03f0d912013-08-07 20:01:00 -07001151 struct sw_flow_match match;
Jesse Grossccb13522011-10-25 19:26:31 -07001152 int err;
Jesse Grossccb13522011-10-25 19:26:31 -07001153
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001154 if (likely(a[OVS_FLOW_ATTR_KEY])) {
1155 ovs_match_init(&match, &key, NULL);
1156 err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL);
1157 if (unlikely(err))
1158 return err;
1159 }
1160
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001161 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001162 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001163 if (unlikely(!dp)) {
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001164 err = -ENODEV;
1165 goto unlock;
1166 }
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001167
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001168 if (unlikely(!a[OVS_FLOW_ATTR_KEY])) {
Pravin B Shelarb637e492013-10-04 00:14:23 -07001169 err = ovs_flow_tbl_flush(&dp->table);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001170 goto unlock;
1171 }
Andy Zhou03f0d912013-08-07 20:01:00 -07001172
Alex Wang4a46b242014-06-30 20:30:29 -07001173 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1174 if (unlikely(!flow)) {
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001175 err = -ENOENT;
1176 goto unlock;
1177 }
Jesse Grossccb13522011-10-25 19:26:31 -07001178
Pravin B Shelarb637e492013-10-04 00:14:23 -07001179 ovs_flow_tbl_remove(&dp->table, flow);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001180 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001181
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001182 reply = ovs_flow_cmd_alloc_info((const struct sw_flow_actions __force *) flow->sf_acts,
1183 info, false);
1184 if (likely(reply)) {
1185 if (likely(!IS_ERR(reply))) {
1186 rcu_read_lock(); /*To keep RCU checker happy. */
1187 err = ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex,
1188 reply, info->snd_portid,
1189 info->snd_seq, 0,
1190 OVS_FLOW_CMD_DEL);
1191 rcu_read_unlock();
1192 BUG_ON(err < 0);
1193
1194 ovs_notify(&dp_flow_genl_family, reply, info);
1195 } else {
1196 netlink_set_err(sock_net(skb->sk)->genl_sock, 0, 0, PTR_ERR(reply));
1197 }
1198 }
1199
1200 ovs_flow_free(flow, true);
Jesse Grossccb13522011-10-25 19:26:31 -07001201 return 0;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001202unlock:
1203 ovs_unlock();
1204 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001205}
1206
1207static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1208{
1209 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
Pravin B Shelarb637e492013-10-04 00:14:23 -07001210 struct table_instance *ti;
Jesse Grossccb13522011-10-25 19:26:31 -07001211 struct datapath *dp;
Jesse Grossccb13522011-10-25 19:26:31 -07001212
Pravin B Shelard57170b2013-07-30 15:39:39 -07001213 rcu_read_lock();
Andy Zhoucc3a5ae2014-09-08 13:14:22 -07001214 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001215 if (!dp) {
Pravin B Shelard57170b2013-07-30 15:39:39 -07001216 rcu_read_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001217 return -ENODEV;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001218 }
Jesse Grossccb13522011-10-25 19:26:31 -07001219
Pravin B Shelarb637e492013-10-04 00:14:23 -07001220 ti = rcu_dereference(dp->table.ti);
Jesse Grossccb13522011-10-25 19:26:31 -07001221 for (;;) {
1222 struct sw_flow *flow;
1223 u32 bucket, obj;
1224
1225 bucket = cb->args[0];
1226 obj = cb->args[1];
Pravin B Shelarb637e492013-10-04 00:14:23 -07001227 flow = ovs_flow_tbl_dump_next(ti, &bucket, &obj);
Jesse Grossccb13522011-10-25 19:26:31 -07001228 if (!flow)
1229 break;
1230
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -07001231 if (ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex, skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001232 NETLINK_CB(cb->skb).portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001233 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1234 OVS_FLOW_CMD_NEW) < 0)
1235 break;
1236
1237 cb->args[0] = bucket;
1238 cb->args[1] = obj;
1239 }
Pravin B Shelard57170b2013-07-30 15:39:39 -07001240 rcu_read_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001241 return skb->len;
1242}
1243
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001244static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
1245 [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
1246 [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
1247 [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
1248};
1249
stephen hemminger48e48a72014-07-16 11:25:52 -07001250static const struct genl_ops dp_flow_genl_ops[] = {
Jesse Grossccb13522011-10-25 19:26:31 -07001251 { .cmd = OVS_FLOW_CMD_NEW,
1252 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1253 .policy = flow_policy,
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001254 .doit = ovs_flow_cmd_new
Jesse Grossccb13522011-10-25 19:26:31 -07001255 },
1256 { .cmd = OVS_FLOW_CMD_DEL,
1257 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1258 .policy = flow_policy,
1259 .doit = ovs_flow_cmd_del
1260 },
1261 { .cmd = OVS_FLOW_CMD_GET,
1262 .flags = 0, /* OK for unprivileged users. */
1263 .policy = flow_policy,
1264 .doit = ovs_flow_cmd_get,
1265 .dumpit = ovs_flow_cmd_dump
1266 },
1267 { .cmd = OVS_FLOW_CMD_SET,
1268 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1269 .policy = flow_policy,
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001270 .doit = ovs_flow_cmd_set,
Jesse Grossccb13522011-10-25 19:26:31 -07001271 },
1272};
1273
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001274static struct genl_family dp_flow_genl_family = {
Jesse Grossccb13522011-10-25 19:26:31 -07001275 .id = GENL_ID_GENERATE,
1276 .hdrsize = sizeof(struct ovs_header),
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001277 .name = OVS_FLOW_FAMILY,
1278 .version = OVS_FLOW_VERSION,
1279 .maxattr = OVS_FLOW_ATTR_MAX,
Pravin B Shelar3a4e0d62013-04-23 07:48:48 +00001280 .netnsok = true,
1281 .parallel_ops = true,
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001282 .ops = dp_flow_genl_ops,
1283 .n_ops = ARRAY_SIZE(dp_flow_genl_ops),
1284 .mcgrps = &ovs_dp_flow_multicast_group,
1285 .n_mcgrps = 1,
Jesse Grossccb13522011-10-25 19:26:31 -07001286};
1287
Thomas Grafc3ff8cf2013-03-29 14:46:49 +01001288static size_t ovs_dp_cmd_msg_size(void)
1289{
1290 size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
1291
1292 msgsize += nla_total_size(IFNAMSIZ);
1293 msgsize += nla_total_size(sizeof(struct ovs_dp_stats));
Andy Zhou1bd71162013-10-22 10:42:46 -07001294 msgsize += nla_total_size(sizeof(struct ovs_dp_megaflow_stats));
Daniele Di Proietto45fb9c32014-01-23 10:47:35 -08001295 msgsize += nla_total_size(sizeof(u32)); /* OVS_DP_ATTR_USER_FEATURES */
Thomas Grafc3ff8cf2013-03-29 14:46:49 +01001296
1297 return msgsize;
1298}
1299
Jarno Rajahalmebb6f9a72014-05-05 11:32:17 -07001300/* Called with ovs_mutex or RCU read lock. */
Jesse Grossccb13522011-10-25 19:26:31 -07001301static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001302 u32 portid, u32 seq, u32 flags, u8 cmd)
Jesse Grossccb13522011-10-25 19:26:31 -07001303{
1304 struct ovs_header *ovs_header;
1305 struct ovs_dp_stats dp_stats;
Andy Zhou1bd71162013-10-22 10:42:46 -07001306 struct ovs_dp_megaflow_stats dp_megaflow_stats;
Jesse Grossccb13522011-10-25 19:26:31 -07001307 int err;
1308
Eric W. Biederman15e47302012-09-07 20:12:54 +00001309 ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
Jesse Grossccb13522011-10-25 19:26:31 -07001310 flags, cmd);
1311 if (!ovs_header)
1312 goto error;
1313
1314 ovs_header->dp_ifindex = get_dpifindex(dp);
1315
Jesse Grossccb13522011-10-25 19:26:31 -07001316 err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
Jesse Grossccb13522011-10-25 19:26:31 -07001317 if (err)
1318 goto nla_put_failure;
1319
Andy Zhou1bd71162013-10-22 10:42:46 -07001320 get_dp_stats(dp, &dp_stats, &dp_megaflow_stats);
1321 if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats),
1322 &dp_stats))
1323 goto nla_put_failure;
1324
1325 if (nla_put(skb, OVS_DP_ATTR_MEGAFLOW_STATS,
1326 sizeof(struct ovs_dp_megaflow_stats),
1327 &dp_megaflow_stats))
David S. Miller028d6a62012-03-29 23:20:48 -04001328 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001329
Thomas Graf43d4be92013-12-13 15:22:18 +01001330 if (nla_put_u32(skb, OVS_DP_ATTR_USER_FEATURES, dp->user_features))
1331 goto nla_put_failure;
1332
Jesse Grossccb13522011-10-25 19:26:31 -07001333 return genlmsg_end(skb, ovs_header);
1334
1335nla_put_failure:
1336 genlmsg_cancel(skb, ovs_header);
1337error:
1338 return -EMSGSIZE;
1339}
1340
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001341static struct sk_buff *ovs_dp_cmd_alloc_info(struct genl_info *info)
Jesse Grossccb13522011-10-25 19:26:31 -07001342{
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001343 return genlmsg_new_unicast(ovs_dp_cmd_msg_size(), info, GFP_KERNEL);
Jesse Grossccb13522011-10-25 19:26:31 -07001344}
1345
Jarno Rajahalmebb6f9a72014-05-05 11:32:17 -07001346/* Called with rcu_read_lock or ovs_mutex. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001347static struct datapath *lookup_datapath(struct net *net,
1348 struct ovs_header *ovs_header,
Jesse Grossccb13522011-10-25 19:26:31 -07001349 struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1350{
1351 struct datapath *dp;
1352
1353 if (!a[OVS_DP_ATTR_NAME])
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001354 dp = get_dp(net, ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001355 else {
1356 struct vport *vport;
1357
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001358 vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
Jesse Grossccb13522011-10-25 19:26:31 -07001359 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
Jesse Grossccb13522011-10-25 19:26:31 -07001360 }
1361 return dp ? dp : ERR_PTR(-ENODEV);
1362}
1363
Thomas Graf44da5ae2013-12-13 15:22:19 +01001364static void ovs_dp_reset_user_features(struct sk_buff *skb, struct genl_info *info)
1365{
1366 struct datapath *dp;
1367
1368 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
Jiri Pirko3c7eacf2014-02-14 11:42:36 +01001369 if (IS_ERR(dp))
Thomas Graf44da5ae2013-12-13 15:22:19 +01001370 return;
1371
1372 WARN(dp->user_features, "Dropping previously announced user features\n");
1373 dp->user_features = 0;
1374}
1375
Thomas Graf43d4be92013-12-13 15:22:18 +01001376static void ovs_dp_change(struct datapath *dp, struct nlattr **a)
1377{
1378 if (a[OVS_DP_ATTR_USER_FEATURES])
1379 dp->user_features = nla_get_u32(a[OVS_DP_ATTR_USER_FEATURES]);
1380}
1381
Jesse Grossccb13522011-10-25 19:26:31 -07001382static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1383{
1384 struct nlattr **a = info->attrs;
1385 struct vport_parms parms;
1386 struct sk_buff *reply;
1387 struct datapath *dp;
1388 struct vport *vport;
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001389 struct ovs_net *ovs_net;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001390 int err, i;
Jesse Grossccb13522011-10-25 19:26:31 -07001391
1392 err = -EINVAL;
1393 if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1394 goto err;
1395
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001396 reply = ovs_dp_cmd_alloc_info(info);
1397 if (!reply)
1398 return -ENOMEM;
Jesse Grossccb13522011-10-25 19:26:31 -07001399
1400 err = -ENOMEM;
1401 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1402 if (dp == NULL)
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001403 goto err_free_reply;
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001404
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001405 ovs_dp_set_net(dp, hold_net(sock_net(skb->sk)));
Jesse Grossccb13522011-10-25 19:26:31 -07001406
1407 /* Allocate table. */
Pravin B Shelarb637e492013-10-04 00:14:23 -07001408 err = ovs_flow_tbl_init(&dp->table);
1409 if (err)
Jesse Grossccb13522011-10-25 19:26:31 -07001410 goto err_free_dp;
1411
WANG Cong1c213bd2014-02-13 11:46:28 -08001412 dp->stats_percpu = netdev_alloc_pcpu_stats(struct dp_stats_percpu);
Jesse Grossccb13522011-10-25 19:26:31 -07001413 if (!dp->stats_percpu) {
1414 err = -ENOMEM;
1415 goto err_destroy_table;
1416 }
1417
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001418 dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
Pravin B Shelare6445712013-10-03 18:16:47 -07001419 GFP_KERNEL);
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001420 if (!dp->ports) {
1421 err = -ENOMEM;
1422 goto err_destroy_percpu;
1423 }
1424
1425 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1426 INIT_HLIST_HEAD(&dp->ports[i]);
1427
Jesse Grossccb13522011-10-25 19:26:31 -07001428 /* Set up our datapath device. */
1429 parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1430 parms.type = OVS_VPORT_TYPE_INTERNAL;
1431 parms.options = NULL;
1432 parms.dp = dp;
1433 parms.port_no = OVSP_LOCAL;
Alex Wang5cd667b2014-07-17 15:14:13 -07001434 parms.upcall_portids = a[OVS_DP_ATTR_UPCALL_PID];
Jesse Grossccb13522011-10-25 19:26:31 -07001435
Thomas Graf43d4be92013-12-13 15:22:18 +01001436 ovs_dp_change(dp, a);
1437
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001438 /* So far only local changes have been made, now need the lock. */
1439 ovs_lock();
1440
Jesse Grossccb13522011-10-25 19:26:31 -07001441 vport = new_vport(&parms);
1442 if (IS_ERR(vport)) {
1443 err = PTR_ERR(vport);
1444 if (err == -EBUSY)
1445 err = -EEXIST;
1446
Thomas Graf44da5ae2013-12-13 15:22:19 +01001447 if (err == -EEXIST) {
1448 /* An outdated user space instance that does not understand
1449 * the concept of user_features has attempted to create a new
1450 * datapath and is likely to reuse it. Drop all user features.
1451 */
1452 if (info->genlhdr->version < OVS_DP_VER_FEATURES)
1453 ovs_dp_reset_user_features(skb, info);
1454 }
1455
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001456 goto err_destroy_ports_array;
Jesse Grossccb13522011-10-25 19:26:31 -07001457 }
1458
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001459 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1460 info->snd_seq, 0, OVS_DP_CMD_NEW);
1461 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001462
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001463 ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001464 list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001465
1466 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001467
Johannes Berg2a94fe42013-11-19 15:19:39 +01001468 ovs_notify(&dp_datapath_genl_family, reply, info);
Jesse Grossccb13522011-10-25 19:26:31 -07001469 return 0;
1470
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001471err_destroy_ports_array:
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001472 ovs_unlock();
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001473 kfree(dp->ports);
Jesse Grossccb13522011-10-25 19:26:31 -07001474err_destroy_percpu:
1475 free_percpu(dp->stats_percpu);
1476err_destroy_table:
Pravin B Shelar9b996e52014-05-06 18:41:20 -07001477 ovs_flow_tbl_destroy(&dp->table);
Jesse Grossccb13522011-10-25 19:26:31 -07001478err_free_dp:
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001479 release_net(ovs_dp_get_net(dp));
Jesse Grossccb13522011-10-25 19:26:31 -07001480 kfree(dp);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001481err_free_reply:
1482 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001483err:
1484 return err;
1485}
1486
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001487/* Called with ovs_mutex. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001488static void __dp_destroy(struct datapath *dp)
Jesse Grossccb13522011-10-25 19:26:31 -07001489{
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001490 int i;
Jesse Grossccb13522011-10-25 19:26:31 -07001491
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001492 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1493 struct vport *vport;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001494 struct hlist_node *n;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001495
Sasha Levinb67bfe02013-02-27 17:06:00 -08001496 hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001497 if (vport->port_no != OVSP_LOCAL)
1498 ovs_dp_detach_port(vport);
1499 }
Jesse Grossccb13522011-10-25 19:26:31 -07001500
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001501 list_del_rcu(&dp->list_node);
Jesse Grossccb13522011-10-25 19:26:31 -07001502
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001503 /* OVSP_LOCAL is datapath internal port. We need to make sure that
Andy Zhoue80857c2014-01-21 09:31:04 -08001504 * all ports in datapath are destroyed first before freeing datapath.
Jesse Grossccb13522011-10-25 19:26:31 -07001505 */
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001506 ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
Jesse Grossccb13522011-10-25 19:26:31 -07001507
Andy Zhoue80857c2014-01-21 09:31:04 -08001508 /* RCU destroy the flow table */
Jesse Grossccb13522011-10-25 19:26:31 -07001509 call_rcu(&dp->rcu, destroy_dp_rcu);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001510}
1511
1512static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1513{
1514 struct sk_buff *reply;
1515 struct datapath *dp;
1516 int err;
1517
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001518 reply = ovs_dp_cmd_alloc_info(info);
1519 if (!reply)
1520 return -ENOMEM;
1521
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001522 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001523 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1524 err = PTR_ERR(dp);
1525 if (IS_ERR(dp))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001526 goto err_unlock_free;
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001527
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001528 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1529 info->snd_seq, 0, OVS_DP_CMD_DEL);
1530 BUG_ON(err < 0);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001531
1532 __dp_destroy(dp);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001533 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001534
Johannes Berg2a94fe42013-11-19 15:19:39 +01001535 ovs_notify(&dp_datapath_genl_family, reply, info);
Jesse Grossccb13522011-10-25 19:26:31 -07001536
1537 return 0;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001538
1539err_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001540 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001541 kfree_skb(reply);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001542 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001543}
1544
1545static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1546{
1547 struct sk_buff *reply;
1548 struct datapath *dp;
1549 int err;
1550
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001551 reply = ovs_dp_cmd_alloc_info(info);
1552 if (!reply)
1553 return -ENOMEM;
1554
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001555 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001556 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001557 err = PTR_ERR(dp);
Jesse Grossccb13522011-10-25 19:26:31 -07001558 if (IS_ERR(dp))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001559 goto err_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001560
Thomas Graf43d4be92013-12-13 15:22:18 +01001561 ovs_dp_change(dp, info->attrs);
1562
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001563 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1564 info->snd_seq, 0, OVS_DP_CMD_NEW);
1565 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001566
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001567 ovs_unlock();
Johannes Berg2a94fe42013-11-19 15:19:39 +01001568 ovs_notify(&dp_datapath_genl_family, reply, info);
Jesse Grossccb13522011-10-25 19:26:31 -07001569
1570 return 0;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001571
1572err_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001573 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001574 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_get(struct sk_buff *skb, struct genl_info *info)
1579{
1580 struct sk_buff *reply;
1581 struct datapath *dp;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001582 int err;
Jesse Grossccb13522011-10-25 19:26:31 -07001583
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001584 reply = ovs_dp_cmd_alloc_info(info);
1585 if (!reply)
1586 return -ENOMEM;
1587
1588 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001589 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001590 if (IS_ERR(dp)) {
1591 err = PTR_ERR(dp);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001592 goto err_unlock_free;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001593 }
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001594 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1595 info->snd_seq, 0, OVS_DP_CMD_NEW);
1596 BUG_ON(err < 0);
1597 rcu_read_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001598
Jesse Grossccb13522011-10-25 19:26:31 -07001599 return genlmsg_reply(reply, info);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001600
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001601err_unlock_free:
1602 rcu_read_unlock();
1603 kfree_skb(reply);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001604 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001605}
1606
1607static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1608{
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001609 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
Jesse Grossccb13522011-10-25 19:26:31 -07001610 struct datapath *dp;
1611 int skip = cb->args[0];
1612 int i = 0;
1613
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001614 rcu_read_lock();
1615 list_for_each_entry_rcu(dp, &ovs_net->dps, list_node) {
Ben Pfaff77676fd2012-01-17 13:33:39 +00001616 if (i >= skip &&
Eric W. Biederman15e47302012-09-07 20:12:54 +00001617 ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001618 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1619 OVS_DP_CMD_NEW) < 0)
1620 break;
1621 i++;
1622 }
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001623 rcu_read_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001624
1625 cb->args[0] = i;
1626
1627 return skb->len;
1628}
1629
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001630static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1631 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1632 [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1633 [OVS_DP_ATTR_USER_FEATURES] = { .type = NLA_U32 },
1634};
1635
stephen hemminger48e48a72014-07-16 11:25:52 -07001636static const struct genl_ops dp_datapath_genl_ops[] = {
Jesse Grossccb13522011-10-25 19:26:31 -07001637 { .cmd = OVS_DP_CMD_NEW,
1638 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1639 .policy = datapath_policy,
1640 .doit = ovs_dp_cmd_new
1641 },
1642 { .cmd = OVS_DP_CMD_DEL,
1643 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1644 .policy = datapath_policy,
1645 .doit = ovs_dp_cmd_del
1646 },
1647 { .cmd = OVS_DP_CMD_GET,
1648 .flags = 0, /* OK for unprivileged users. */
1649 .policy = datapath_policy,
1650 .doit = ovs_dp_cmd_get,
1651 .dumpit = ovs_dp_cmd_dump
1652 },
1653 { .cmd = OVS_DP_CMD_SET,
1654 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1655 .policy = datapath_policy,
1656 .doit = ovs_dp_cmd_set,
1657 },
1658};
1659
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001660static struct genl_family dp_datapath_genl_family = {
Jesse Grossccb13522011-10-25 19:26:31 -07001661 .id = GENL_ID_GENERATE,
1662 .hdrsize = sizeof(struct ovs_header),
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001663 .name = OVS_DATAPATH_FAMILY,
1664 .version = OVS_DATAPATH_VERSION,
1665 .maxattr = OVS_DP_ATTR_MAX,
Pravin B Shelar3a4e0d62013-04-23 07:48:48 +00001666 .netnsok = true,
1667 .parallel_ops = true,
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001668 .ops = dp_datapath_genl_ops,
1669 .n_ops = ARRAY_SIZE(dp_datapath_genl_ops),
1670 .mcgrps = &ovs_dp_datapath_multicast_group,
1671 .n_mcgrps = 1,
Jesse Grossccb13522011-10-25 19:26:31 -07001672};
1673
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001674/* Called with ovs_mutex or RCU read lock. */
Jesse Grossccb13522011-10-25 19:26:31 -07001675static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001676 u32 portid, u32 seq, u32 flags, u8 cmd)
Jesse Grossccb13522011-10-25 19:26:31 -07001677{
1678 struct ovs_header *ovs_header;
1679 struct ovs_vport_stats vport_stats;
1680 int err;
1681
Eric W. Biederman15e47302012-09-07 20:12:54 +00001682 ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
Jesse Grossccb13522011-10-25 19:26:31 -07001683 flags, cmd);
1684 if (!ovs_header)
1685 return -EMSGSIZE;
1686
1687 ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1688
David S. Miller028d6a62012-03-29 23:20:48 -04001689 if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
1690 nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
Alex Wang5cd667b2014-07-17 15:14:13 -07001691 nla_put_string(skb, OVS_VPORT_ATTR_NAME,
1692 vport->ops->get_name(vport)))
David S. Miller028d6a62012-03-29 23:20:48 -04001693 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001694
1695 ovs_vport_get_stats(vport, &vport_stats);
David S. Miller028d6a62012-03-29 23:20:48 -04001696 if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
1697 &vport_stats))
1698 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001699
Alex Wang5cd667b2014-07-17 15:14:13 -07001700 if (ovs_vport_get_upcall_portids(vport, skb))
1701 goto nla_put_failure;
1702
Jesse Grossccb13522011-10-25 19:26:31 -07001703 err = ovs_vport_get_options(vport, skb);
1704 if (err == -EMSGSIZE)
1705 goto error;
1706
1707 return genlmsg_end(skb, ovs_header);
1708
1709nla_put_failure:
1710 err = -EMSGSIZE;
1711error:
1712 genlmsg_cancel(skb, ovs_header);
1713 return err;
1714}
1715
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001716static struct sk_buff *ovs_vport_cmd_alloc_info(void)
1717{
1718 return nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1719}
1720
1721/* Called with ovs_mutex, only via ovs_dp_notify_wq(). */
Eric W. Biederman15e47302012-09-07 20:12:54 +00001722struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001723 u32 seq, u8 cmd)
1724{
1725 struct sk_buff *skb;
1726 int retval;
1727
1728 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1729 if (!skb)
1730 return ERR_PTR(-ENOMEM);
1731
Eric W. Biederman15e47302012-09-07 20:12:54 +00001732 retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
Jesse Grossa9341512013-03-26 15:48:38 -07001733 BUG_ON(retval < 0);
1734
Jesse Grossccb13522011-10-25 19:26:31 -07001735 return skb;
1736}
1737
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001738/* Called with ovs_mutex or RCU read lock. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001739static struct vport *lookup_vport(struct net *net,
1740 struct ovs_header *ovs_header,
Jesse Grossccb13522011-10-25 19:26:31 -07001741 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1742{
1743 struct datapath *dp;
1744 struct vport *vport;
1745
1746 if (a[OVS_VPORT_ATTR_NAME]) {
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001747 vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
Jesse Grossccb13522011-10-25 19:26:31 -07001748 if (!vport)
1749 return ERR_PTR(-ENODEV);
Ben Pfaff651a68e2012-03-06 15:04:04 -08001750 if (ovs_header->dp_ifindex &&
1751 ovs_header->dp_ifindex != get_dpifindex(vport->dp))
1752 return ERR_PTR(-ENODEV);
Jesse Grossccb13522011-10-25 19:26:31 -07001753 return vport;
1754 } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
1755 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1756
1757 if (port_no >= DP_MAX_PORTS)
1758 return ERR_PTR(-EFBIG);
1759
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001760 dp = get_dp(net, ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001761 if (!dp)
1762 return ERR_PTR(-ENODEV);
1763
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001764 vport = ovs_vport_ovsl_rcu(dp, port_no);
Jesse Grossccb13522011-10-25 19:26:31 -07001765 if (!vport)
Jarno Rajahalme14408db2013-01-09 14:27:35 -08001766 return ERR_PTR(-ENODEV);
Jesse Grossccb13522011-10-25 19:26:31 -07001767 return vport;
1768 } else
1769 return ERR_PTR(-EINVAL);
1770}
1771
1772static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
1773{
1774 struct nlattr **a = info->attrs;
1775 struct ovs_header *ovs_header = info->userhdr;
1776 struct vport_parms parms;
1777 struct sk_buff *reply;
1778 struct vport *vport;
1779 struct datapath *dp;
1780 u32 port_no;
1781 int err;
1782
Jesse Grossccb13522011-10-25 19:26:31 -07001783 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
1784 !a[OVS_VPORT_ATTR_UPCALL_PID])
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001785 return -EINVAL;
1786
1787 port_no = a[OVS_VPORT_ATTR_PORT_NO]
1788 ? nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]) : 0;
1789 if (port_no >= DP_MAX_PORTS)
1790 return -EFBIG;
1791
1792 reply = ovs_vport_cmd_alloc_info();
1793 if (!reply)
1794 return -ENOMEM;
Jesse Grossccb13522011-10-25 19:26:31 -07001795
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001796 ovs_lock();
Thomas Graf62b9c8d2014-10-22 17:29:06 +02001797restart:
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001798 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001799 err = -ENODEV;
1800 if (!dp)
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001801 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001802
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001803 if (port_no) {
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001804 vport = ovs_vport_ovsl(dp, port_no);
Jesse Grossccb13522011-10-25 19:26:31 -07001805 err = -EBUSY;
1806 if (vport)
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001807 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001808 } else {
1809 for (port_no = 1; ; port_no++) {
1810 if (port_no >= DP_MAX_PORTS) {
1811 err = -EFBIG;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001812 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001813 }
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001814 vport = ovs_vport_ovsl(dp, port_no);
Jesse Grossccb13522011-10-25 19:26:31 -07001815 if (!vport)
1816 break;
1817 }
1818 }
1819
1820 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
1821 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1822 parms.options = a[OVS_VPORT_ATTR_OPTIONS];
1823 parms.dp = dp;
1824 parms.port_no = port_no;
Alex Wang5cd667b2014-07-17 15:14:13 -07001825 parms.upcall_portids = a[OVS_VPORT_ATTR_UPCALL_PID];
Jesse Grossccb13522011-10-25 19:26:31 -07001826
1827 vport = new_vport(&parms);
1828 err = PTR_ERR(vport);
Thomas Graf62b9c8d2014-10-22 17:29:06 +02001829 if (IS_ERR(vport)) {
1830 if (err == -EAGAIN)
1831 goto restart;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001832 goto exit_unlock_free;
Thomas Graf62b9c8d2014-10-22 17:29:06 +02001833 }
Jesse Grossccb13522011-10-25 19:26:31 -07001834
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001835 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1836 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1837 BUG_ON(err < 0);
1838 ovs_unlock();
Thomas Grafed6611852013-03-29 14:46:50 +01001839
Johannes Berg2a94fe42013-11-19 15:19:39 +01001840 ovs_notify(&dp_vport_genl_family, reply, info);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001841 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001842
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001843exit_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001844 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001845 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001846 return err;
1847}
1848
1849static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
1850{
1851 struct nlattr **a = info->attrs;
1852 struct sk_buff *reply;
1853 struct vport *vport;
1854 int err;
1855
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001856 reply = ovs_vport_cmd_alloc_info();
1857 if (!reply)
1858 return -ENOMEM;
1859
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001860 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001861 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
Jesse Grossccb13522011-10-25 19:26:31 -07001862 err = PTR_ERR(vport);
1863 if (IS_ERR(vport))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001864 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001865
Jesse Grossccb13522011-10-25 19:26:31 -07001866 if (a[OVS_VPORT_ATTR_TYPE] &&
Jesse Grossf44f3402013-05-13 08:15:26 -07001867 nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
Jesse Grossccb13522011-10-25 19:26:31 -07001868 err = -EINVAL;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001869 goto exit_unlock_free;
Jesse Grossa9341512013-03-26 15:48:38 -07001870 }
1871
Jesse Grossf44f3402013-05-13 08:15:26 -07001872 if (a[OVS_VPORT_ATTR_OPTIONS]) {
Jesse Grossccb13522011-10-25 19:26:31 -07001873 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
Jesse Grossf44f3402013-05-13 08:15:26 -07001874 if (err)
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001875 goto exit_unlock_free;
Jesse Grossf44f3402013-05-13 08:15:26 -07001876 }
Jesse Grossa9341512013-03-26 15:48:38 -07001877
Alex Wang5cd667b2014-07-17 15:14:13 -07001878
1879 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
1880 struct nlattr *ids = a[OVS_VPORT_ATTR_UPCALL_PID];
1881
1882 err = ovs_vport_set_upcall_portids(vport, ids);
1883 if (err)
1884 goto exit_unlock_free;
1885 }
Jesse Grossccb13522011-10-25 19:26:31 -07001886
Jesse Grossa9341512013-03-26 15:48:38 -07001887 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1888 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1889 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001890
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001891 ovs_unlock();
Johannes Berg2a94fe42013-11-19 15:19:39 +01001892 ovs_notify(&dp_vport_genl_family, reply, info);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001893 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001894
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001895exit_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001896 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001897 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001898 return err;
1899}
1900
1901static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
1902{
1903 struct nlattr **a = info->attrs;
1904 struct sk_buff *reply;
1905 struct vport *vport;
1906 int err;
1907
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001908 reply = ovs_vport_cmd_alloc_info();
1909 if (!reply)
1910 return -ENOMEM;
1911
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001912 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001913 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
Jesse Grossccb13522011-10-25 19:26:31 -07001914 err = PTR_ERR(vport);
1915 if (IS_ERR(vport))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001916 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001917
1918 if (vport->port_no == OVSP_LOCAL) {
1919 err = -EINVAL;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001920 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001921 }
1922
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001923 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1924 info->snd_seq, 0, OVS_VPORT_CMD_DEL);
1925 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001926 ovs_dp_detach_port(vport);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001927 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001928
Johannes Berg2a94fe42013-11-19 15:19:39 +01001929 ovs_notify(&dp_vport_genl_family, reply, info);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001930 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001931
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001932exit_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001933 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001934 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001935 return err;
1936}
1937
1938static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
1939{
1940 struct nlattr **a = info->attrs;
1941 struct ovs_header *ovs_header = info->userhdr;
1942 struct sk_buff *reply;
1943 struct vport *vport;
1944 int err;
1945
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001946 reply = ovs_vport_cmd_alloc_info();
1947 if (!reply)
1948 return -ENOMEM;
1949
Jesse Grossccb13522011-10-25 19:26:31 -07001950 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001951 vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
Jesse Grossccb13522011-10-25 19:26:31 -07001952 err = PTR_ERR(vport);
1953 if (IS_ERR(vport))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001954 goto exit_unlock_free;
1955 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1956 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1957 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001958 rcu_read_unlock();
1959
1960 return genlmsg_reply(reply, info);
1961
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001962exit_unlock_free:
Jesse Grossccb13522011-10-25 19:26:31 -07001963 rcu_read_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001964 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001965 return err;
1966}
1967
1968static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1969{
1970 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1971 struct datapath *dp;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001972 int bucket = cb->args[0], skip = cb->args[1];
1973 int i, j = 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001974
Jesse Grossccb13522011-10-25 19:26:31 -07001975 rcu_read_lock();
Andy Zhoucc3a5ae2014-09-08 13:14:22 -07001976 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
Jarno Rajahalme42ee19e2014-02-15 17:42:29 -08001977 if (!dp) {
1978 rcu_read_unlock();
1979 return -ENODEV;
1980 }
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001981 for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
Jesse Grossccb13522011-10-25 19:26:31 -07001982 struct vport *vport;
1983
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001984 j = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001985 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001986 if (j >= skip &&
1987 ovs_vport_cmd_fill_info(vport, skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001988 NETLINK_CB(cb->skb).portid,
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001989 cb->nlh->nlmsg_seq,
1990 NLM_F_MULTI,
1991 OVS_VPORT_CMD_NEW) < 0)
1992 goto out;
Jesse Grossccb13522011-10-25 19:26:31 -07001993
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001994 j++;
1995 }
1996 skip = 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001997 }
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001998out:
Jesse Grossccb13522011-10-25 19:26:31 -07001999 rcu_read_unlock();
2000
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07002001 cb->args[0] = i;
2002 cb->args[1] = j;
Jesse Grossccb13522011-10-25 19:26:31 -07002003
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07002004 return skb->len;
Jesse Grossccb13522011-10-25 19:26:31 -07002005}
2006
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002007static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
2008 [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
2009 [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
2010 [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
2011 [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
2012 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
2013 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
2014};
2015
stephen hemminger48e48a72014-07-16 11:25:52 -07002016static const struct genl_ops dp_vport_genl_ops[] = {
Jesse Grossccb13522011-10-25 19:26:31 -07002017 { .cmd = OVS_VPORT_CMD_NEW,
2018 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2019 .policy = vport_policy,
2020 .doit = ovs_vport_cmd_new
2021 },
2022 { .cmd = OVS_VPORT_CMD_DEL,
2023 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2024 .policy = vport_policy,
2025 .doit = ovs_vport_cmd_del
2026 },
2027 { .cmd = OVS_VPORT_CMD_GET,
2028 .flags = 0, /* OK for unprivileged users. */
2029 .policy = vport_policy,
2030 .doit = ovs_vport_cmd_get,
2031 .dumpit = ovs_vport_cmd_dump
2032 },
2033 { .cmd = OVS_VPORT_CMD_SET,
2034 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2035 .policy = vport_policy,
2036 .doit = ovs_vport_cmd_set,
2037 },
2038};
2039
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002040struct genl_family dp_vport_genl_family = {
2041 .id = GENL_ID_GENERATE,
2042 .hdrsize = sizeof(struct ovs_header),
2043 .name = OVS_VPORT_FAMILY,
2044 .version = OVS_VPORT_VERSION,
2045 .maxattr = OVS_VPORT_ATTR_MAX,
2046 .netnsok = true,
2047 .parallel_ops = true,
2048 .ops = dp_vport_genl_ops,
2049 .n_ops = ARRAY_SIZE(dp_vport_genl_ops),
2050 .mcgrps = &ovs_dp_vport_multicast_group,
2051 .n_mcgrps = 1,
Jesse Grossccb13522011-10-25 19:26:31 -07002052};
2053
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002054static struct genl_family * const dp_genl_families[] = {
2055 &dp_datapath_genl_family,
2056 &dp_vport_genl_family,
2057 &dp_flow_genl_family,
2058 &dp_packet_genl_family,
Jesse Grossccb13522011-10-25 19:26:31 -07002059};
2060
2061static void dp_unregister_genl(int n_families)
2062{
2063 int i;
2064
2065 for (i = 0; i < n_families; i++)
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002066 genl_unregister_family(dp_genl_families[i]);
Jesse Grossccb13522011-10-25 19:26:31 -07002067}
2068
2069static int dp_register_genl(void)
2070{
Jesse Grossccb13522011-10-25 19:26:31 -07002071 int err;
2072 int i;
2073
Jesse Grossccb13522011-10-25 19:26:31 -07002074 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
Jesse Grossccb13522011-10-25 19:26:31 -07002075
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002076 err = genl_register_family(dp_genl_families[i]);
Jesse Grossccb13522011-10-25 19:26:31 -07002077 if (err)
2078 goto error;
Jesse Grossccb13522011-10-25 19:26:31 -07002079 }
2080
2081 return 0;
2082
2083error:
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002084 dp_unregister_genl(i);
Jesse Grossccb13522011-10-25 19:26:31 -07002085 return err;
2086}
2087
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002088static int __net_init ovs_init_net(struct net *net)
2089{
2090 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2091
2092 INIT_LIST_HEAD(&ovs_net->dps);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002093 INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002094 return 0;
2095}
2096
2097static void __net_exit ovs_exit_net(struct net *net)
2098{
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002099 struct datapath *dp, *dp_next;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002100 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002101
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002102 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002103 list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
2104 __dp_destroy(dp);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002105 ovs_unlock();
2106
2107 cancel_work_sync(&ovs_net->dp_notify_work);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002108}
2109
2110static struct pernet_operations ovs_net_ops = {
2111 .init = ovs_init_net,
2112 .exit = ovs_exit_net,
2113 .id = &ovs_net_id,
2114 .size = sizeof(struct ovs_net),
2115};
2116
Jesse Grossccb13522011-10-25 19:26:31 -07002117static int __init dp_init(void)
2118{
Jesse Grossccb13522011-10-25 19:26:31 -07002119 int err;
2120
YOSHIFUJI Hideaki / 吉藤英明3523b292013-01-09 07:19:55 +00002121 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
Jesse Grossccb13522011-10-25 19:26:31 -07002122
2123 pr_info("Open vSwitch switching datapath\n");
2124
Andy Zhou971427f32014-09-15 19:37:25 -07002125 err = action_fifos_init();
Jesse Grossccb13522011-10-25 19:26:31 -07002126 if (err)
2127 goto error;
2128
Andy Zhou971427f32014-09-15 19:37:25 -07002129 err = ovs_internal_dev_rtnl_link_register();
2130 if (err)
2131 goto error_action_fifos_exit;
2132
Jiri Pirko5b9e7e12014-06-26 09:58:26 +02002133 err = ovs_flow_init();
2134 if (err)
2135 goto error_unreg_rtnl_link;
2136
Jesse Grossccb13522011-10-25 19:26:31 -07002137 err = ovs_vport_init();
2138 if (err)
2139 goto error_flow_exit;
2140
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002141 err = register_pernet_device(&ovs_net_ops);
Jesse Grossccb13522011-10-25 19:26:31 -07002142 if (err)
2143 goto error_vport_exit;
2144
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002145 err = register_netdevice_notifier(&ovs_dp_device_notifier);
2146 if (err)
2147 goto error_netns_exit;
2148
Thomas Graf62b9c8d2014-10-22 17:29:06 +02002149 err = ovs_netdev_init();
2150 if (err)
2151 goto error_unreg_notifier;
2152
Jesse Grossccb13522011-10-25 19:26:31 -07002153 err = dp_register_genl();
2154 if (err < 0)
Thomas Graf62b9c8d2014-10-22 17:29:06 +02002155 goto error_unreg_netdev;
Jesse Grossccb13522011-10-25 19:26:31 -07002156
Jesse Grossccb13522011-10-25 19:26:31 -07002157 return 0;
2158
Thomas Graf62b9c8d2014-10-22 17:29:06 +02002159error_unreg_netdev:
2160 ovs_netdev_exit();
Jesse Grossccb13522011-10-25 19:26:31 -07002161error_unreg_notifier:
2162 unregister_netdevice_notifier(&ovs_dp_device_notifier);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002163error_netns_exit:
2164 unregister_pernet_device(&ovs_net_ops);
Jesse Grossccb13522011-10-25 19:26:31 -07002165error_vport_exit:
2166 ovs_vport_exit();
2167error_flow_exit:
2168 ovs_flow_exit();
Jiri Pirko5b9e7e12014-06-26 09:58:26 +02002169error_unreg_rtnl_link:
2170 ovs_internal_dev_rtnl_link_unregister();
Andy Zhou971427f32014-09-15 19:37:25 -07002171error_action_fifos_exit:
2172 action_fifos_exit();
Jesse Grossccb13522011-10-25 19:26:31 -07002173error:
2174 return err;
2175}
2176
2177static void dp_cleanup(void)
2178{
Jesse Grossccb13522011-10-25 19:26:31 -07002179 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
Thomas Graf62b9c8d2014-10-22 17:29:06 +02002180 ovs_netdev_exit();
Jesse Grossccb13522011-10-25 19:26:31 -07002181 unregister_netdevice_notifier(&ovs_dp_device_notifier);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002182 unregister_pernet_device(&ovs_net_ops);
2183 rcu_barrier();
Jesse Grossccb13522011-10-25 19:26:31 -07002184 ovs_vport_exit();
2185 ovs_flow_exit();
Jiri Pirko5b9e7e12014-06-26 09:58:26 +02002186 ovs_internal_dev_rtnl_link_unregister();
Andy Zhou971427f32014-09-15 19:37:25 -07002187 action_fifos_exit();
Jesse Grossccb13522011-10-25 19:26:31 -07002188}
2189
2190module_init(dp_init);
2191module_exit(dp_cleanup);
2192
2193MODULE_DESCRIPTION("Open vSwitch switching datapath");
2194MODULE_LICENSE("GPL");