blob: c63e60e4d9476dd6db27503d756f4cbabc7efa3a [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 *,
Pravin B Shelare8eedb82014-11-06 06:57:27 -0800139 const struct sw_flow_key *,
Jesse Grossccb13522011-10-25 19:26:31 -0700140 const struct dp_upcall_info *);
Thomas Graf8055a892013-12-13 15:22:20 +0100141static int queue_userspace_packet(struct datapath *dp, struct sk_buff *,
Pravin B Shelare8eedb82014-11-06 06:57:27 -0800142 const struct sw_flow_key *,
Jesse Grossccb13522011-10-25 19:26:31 -0700143 const struct dp_upcall_info *);
144
Andy Zhoucc3a5ae2014-09-08 13:14:22 -0700145/* Must be called with rcu_read_lock. */
146static struct datapath *get_dp_rcu(struct net *net, int dp_ifindex)
Jesse Grossccb13522011-10-25 19:26:31 -0700147{
Andy Zhoucc3a5ae2014-09-08 13:14:22 -0700148 struct net_device *dev = dev_get_by_index_rcu(net, dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -0700149
Jesse Grossccb13522011-10-25 19:26:31 -0700150 if (dev) {
151 struct vport *vport = ovs_internal_dev_get_vport(dev);
152 if (vport)
Andy Zhoucc3a5ae2014-09-08 13:14:22 -0700153 return vport->dp;
Jesse Grossccb13522011-10-25 19:26:31 -0700154 }
Andy Zhoucc3a5ae2014-09-08 13:14:22 -0700155
156 return NULL;
157}
158
159/* The caller must hold either ovs_mutex or rcu_read_lock to keep the
160 * returned dp pointer valid.
161 */
162static inline struct datapath *get_dp(struct net *net, int dp_ifindex)
163{
164 struct datapath *dp;
165
166 WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_ovsl_is_held());
167 rcu_read_lock();
168 dp = get_dp_rcu(net, dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -0700169 rcu_read_unlock();
170
171 return dp;
172}
173
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700174/* Must be called with rcu_read_lock or ovs_mutex. */
Andy Zhou971427f32014-09-15 19:37:25 -0700175const char *ovs_dp_name(const struct datapath *dp)
Jesse Grossccb13522011-10-25 19:26:31 -0700176{
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700177 struct vport *vport = ovs_vport_ovsl_rcu(dp, OVSP_LOCAL);
Jesse Grossccb13522011-10-25 19:26:31 -0700178 return vport->ops->get_name(vport);
179}
180
Thomas Graf12eb18f2014-11-06 06:58:52 -0800181static int get_dpifindex(const struct datapath *dp)
Jesse Grossccb13522011-10-25 19:26:31 -0700182{
183 struct vport *local;
184 int ifindex;
185
186 rcu_read_lock();
187
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700188 local = ovs_vport_rcu(dp, OVSP_LOCAL);
Jesse Grossccb13522011-10-25 19:26:31 -0700189 if (local)
Thomas Grafcff63a52013-04-29 13:06:41 +0000190 ifindex = netdev_vport_priv(local)->dev->ifindex;
Jesse Grossccb13522011-10-25 19:26:31 -0700191 else
192 ifindex = 0;
193
194 rcu_read_unlock();
195
196 return ifindex;
197}
198
199static void destroy_dp_rcu(struct rcu_head *rcu)
200{
201 struct datapath *dp = container_of(rcu, struct datapath, rcu);
202
Pravin B Shelar9b996e52014-05-06 18:41:20 -0700203 ovs_flow_tbl_destroy(&dp->table);
Jesse Grossccb13522011-10-25 19:26:31 -0700204 free_percpu(dp->stats_percpu);
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800205 release_net(ovs_dp_get_net(dp));
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700206 kfree(dp->ports);
Jesse Grossccb13522011-10-25 19:26:31 -0700207 kfree(dp);
208}
209
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700210static struct hlist_head *vport_hash_bucket(const struct datapath *dp,
211 u16 port_no)
212{
213 return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)];
214}
215
Jarno Rajahalmebb6f9a72014-05-05 11:32:17 -0700216/* Called with ovs_mutex or RCU read lock. */
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700217struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no)
218{
219 struct vport *vport;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700220 struct hlist_head *head;
221
222 head = vport_hash_bucket(dp, port_no);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800223 hlist_for_each_entry_rcu(vport, head, dp_hash_node) {
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700224 if (vport->port_no == port_no)
225 return vport;
226 }
227 return NULL;
228}
229
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700230/* Called with ovs_mutex. */
Jesse Grossccb13522011-10-25 19:26:31 -0700231static struct vport *new_vport(const struct vport_parms *parms)
232{
233 struct vport *vport;
234
235 vport = ovs_vport_add(parms);
236 if (!IS_ERR(vport)) {
237 struct datapath *dp = parms->dp;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700238 struct hlist_head *head = vport_hash_bucket(dp, vport->port_no);
Jesse Grossccb13522011-10-25 19:26:31 -0700239
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700240 hlist_add_head_rcu(&vport->dp_hash_node, head);
Jesse Grossccb13522011-10-25 19:26:31 -0700241 }
Jesse Grossccb13522011-10-25 19:26:31 -0700242 return vport;
243}
244
Jesse Grossccb13522011-10-25 19:26:31 -0700245void ovs_dp_detach_port(struct vport *p)
246{
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700247 ASSERT_OVSL();
Jesse Grossccb13522011-10-25 19:26:31 -0700248
249 /* First drop references to device. */
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700250 hlist_del_rcu(&p->dp_hash_node);
Jesse Grossccb13522011-10-25 19:26:31 -0700251
252 /* Then destroy it. */
253 ovs_vport_del(p);
254}
255
256/* Must be called with rcu_read_lock. */
Pravin B Shelar8c8b1b82014-09-15 19:28:44 -0700257void ovs_dp_process_packet(struct sk_buff *skb, struct sw_flow_key *key)
Jesse Grossccb13522011-10-25 19:26:31 -0700258{
Pravin B Shelar83c8df22014-09-15 19:20:31 -0700259 const struct vport *p = OVS_CB(skb)->input_vport;
Jesse Grossccb13522011-10-25 19:26:31 -0700260 struct datapath *dp = p->dp;
261 struct sw_flow *flow;
Lorand Jakabd98612b2014-10-06 05:45:32 -0700262 struct sw_flow_actions *sf_acts;
Jesse Grossccb13522011-10-25 19:26:31 -0700263 struct dp_stats_percpu *stats;
Jesse Grossccb13522011-10-25 19:26:31 -0700264 u64 *stats_counter;
Andy Zhou1bd71162013-10-22 10:42:46 -0700265 u32 n_mask_hit;
Jesse Grossccb13522011-10-25 19:26:31 -0700266
Shan Wei404f2f12012-11-13 09:52:25 +0800267 stats = this_cpu_ptr(dp->stats_percpu);
Jesse Grossccb13522011-10-25 19:26:31 -0700268
Jesse Grossccb13522011-10-25 19:26:31 -0700269 /* Look up flow. */
Pravin B Shelar8c8b1b82014-09-15 19:28:44 -0700270 flow = ovs_flow_tbl_lookup_stats(&dp->table, key, &n_mask_hit);
Jesse Grossccb13522011-10-25 19:26:31 -0700271 if (unlikely(!flow)) {
272 struct dp_upcall_info upcall;
Pravin B Shelar8c8b1b82014-09-15 19:28:44 -0700273 int error;
Jesse Grossccb13522011-10-25 19:26:31 -0700274
275 upcall.cmd = OVS_PACKET_CMD_MISS;
Jesse Grossccb13522011-10-25 19:26:31 -0700276 upcall.userdata = NULL;
Alex Wang5cd667b2014-07-17 15:14:13 -0700277 upcall.portid = ovs_vport_find_upcall_portid(p, skb);
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800278 upcall.egress_tun_info = NULL;
Pravin B Shelare8eedb82014-11-06 06:57:27 -0800279 error = ovs_dp_upcall(dp, skb, key, &upcall);
Li RongQingc5eba0b2014-09-03 17:43:45 +0800280 if (unlikely(error))
281 kfree_skb(skb);
282 else
283 consume_skb(skb);
Jesse Grossccb13522011-10-25 19:26:31 -0700284 stats_counter = &stats->n_missed;
285 goto out;
286 }
287
Lorand Jakabd98612b2014-10-06 05:45:32 -0700288 ovs_flow_stats_update(flow, key->tp.flags, skb);
289 sf_acts = rcu_dereference(flow->sf_acts);
290 ovs_execute_actions(dp, skb, sf_acts, key);
Jesse Grossccb13522011-10-25 19:26:31 -0700291
Pravin B Shelare298e502013-10-29 17:22:21 -0700292 stats_counter = &stats->n_hit;
Jesse Grossccb13522011-10-25 19:26:31 -0700293
294out:
295 /* Update datapath statistics. */
WANG Congdf9d9fd2014-02-14 15:10:46 -0800296 u64_stats_update_begin(&stats->syncp);
Jesse Grossccb13522011-10-25 19:26:31 -0700297 (*stats_counter)++;
Andy Zhou1bd71162013-10-22 10:42:46 -0700298 stats->n_mask_hit += n_mask_hit;
WANG Congdf9d9fd2014-02-14 15:10:46 -0800299 u64_stats_update_end(&stats->syncp);
Jesse Grossccb13522011-10-25 19:26:31 -0700300}
301
Jesse Grossccb13522011-10-25 19:26:31 -0700302int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
Pravin B Shelare8eedb82014-11-06 06:57:27 -0800303 const struct sw_flow_key *key,
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800304 const struct dp_upcall_info *upcall_info)
Jesse Grossccb13522011-10-25 19:26:31 -0700305{
306 struct dp_stats_percpu *stats;
Jesse Grossccb13522011-10-25 19:26:31 -0700307 int err;
308
Eric W. Biederman15e47302012-09-07 20:12:54 +0000309 if (upcall_info->portid == 0) {
Jesse Grossccb13522011-10-25 19:26:31 -0700310 err = -ENOTCONN;
311 goto err;
312 }
313
Jesse Grossccb13522011-10-25 19:26:31 -0700314 if (!skb_is_gso(skb))
Pravin B Shelare8eedb82014-11-06 06:57:27 -0800315 err = queue_userspace_packet(dp, skb, key, upcall_info);
Jesse Grossccb13522011-10-25 19:26:31 -0700316 else
Pravin B Shelare8eedb82014-11-06 06:57:27 -0800317 err = queue_gso_packets(dp, skb, key, upcall_info);
Jesse Grossccb13522011-10-25 19:26:31 -0700318 if (err)
319 goto err;
320
321 return 0;
322
323err:
Shan Wei404f2f12012-11-13 09:52:25 +0800324 stats = this_cpu_ptr(dp->stats_percpu);
Jesse Grossccb13522011-10-25 19:26:31 -0700325
WANG Congdf9d9fd2014-02-14 15:10:46 -0800326 u64_stats_update_begin(&stats->syncp);
Jesse Grossccb13522011-10-25 19:26:31 -0700327 stats->n_lost++;
WANG Congdf9d9fd2014-02-14 15:10:46 -0800328 u64_stats_update_end(&stats->syncp);
Jesse Grossccb13522011-10-25 19:26:31 -0700329
330 return err;
331}
332
Thomas Graf8055a892013-12-13 15:22:20 +0100333static int queue_gso_packets(struct datapath *dp, struct sk_buff *skb,
Pravin B Shelare8eedb82014-11-06 06:57:27 -0800334 const struct sw_flow_key *key,
Jesse Grossccb13522011-10-25 19:26:31 -0700335 const struct dp_upcall_info *upcall_info)
336{
Ben Pfaffa1b5d0d2012-07-20 14:47:54 -0700337 unsigned short gso_type = skb_shinfo(skb)->gso_type;
Jesse Grossccb13522011-10-25 19:26:31 -0700338 struct sw_flow_key later_key;
339 struct sk_buff *segs, *nskb;
Pravin B Shelare8eedb82014-11-06 06:57:27 -0800340 struct ovs_skb_cb ovs_cb;
Jesse Grossccb13522011-10-25 19:26:31 -0700341 int err;
342
Pravin B Shelare8eedb82014-11-06 06:57:27 -0800343 ovs_cb = *OVS_CB(skb);
Thomas Graf09c5e602013-12-13 15:22:22 +0100344 segs = __skb_gso_segment(skb, NETIF_F_SG, false);
Pravin B Shelare8eedb82014-11-06 06:57:27 -0800345 *OVS_CB(skb) = ovs_cb;
Pravin B Shelar92e5dfc2012-07-20 14:46:29 -0700346 if (IS_ERR(segs))
347 return PTR_ERR(segs);
Florian Westphal330966e2014-10-20 13:49:17 +0200348 if (segs == NULL)
349 return -EINVAL;
Jesse Grossccb13522011-10-25 19:26:31 -0700350
Pravin B Shelare8eedb82014-11-06 06:57:27 -0800351 if (gso_type & SKB_GSO_UDP) {
352 /* The initial flow key extracted by ovs_flow_key_extract()
353 * in this case is for a first fragment, so we need to
354 * properly mark later fragments.
355 */
356 later_key = *key;
357 later_key.ip.frag = OVS_FRAG_TYPE_LATER;
358 }
359
Jesse Grossccb13522011-10-25 19:26:31 -0700360 /* Queue all of the segments. */
361 skb = segs;
362 do {
Pravin B Shelare8eedb82014-11-06 06:57:27 -0800363 *OVS_CB(skb) = ovs_cb;
364 if (gso_type & SKB_GSO_UDP && skb != segs)
365 key = &later_key;
366
367 err = queue_userspace_packet(dp, skb, key, upcall_info);
Jesse Grossccb13522011-10-25 19:26:31 -0700368 if (err)
369 break;
370
Jesse Grossccb13522011-10-25 19:26:31 -0700371 } while ((skb = skb->next));
372
373 /* Free all of the segments. */
374 skb = segs;
375 do {
376 nskb = skb->next;
377 if (err)
378 kfree_skb(skb);
379 else
380 consume_skb(skb);
381 } while ((skb = nskb));
382 return err;
383}
384
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800385static size_t upcall_msg_size(const struct dp_upcall_info *upcall_info,
Thomas Grafbda56f12013-12-13 15:22:21 +0100386 unsigned int hdrlen)
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100387{
388 size_t size = NLMSG_ALIGN(sizeof(struct ovs_header))
Thomas Grafbda56f12013-12-13 15:22:21 +0100389 + nla_total_size(hdrlen) /* OVS_PACKET_ATTR_PACKET */
Joe Stringer41af73e2014-10-18 16:14:14 -0700390 + nla_total_size(ovs_key_attr_size()); /* OVS_PACKET_ATTR_KEY */
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100391
392 /* OVS_PACKET_ATTR_USERDATA */
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800393 if (upcall_info->userdata)
394 size += NLA_ALIGN(upcall_info->userdata->nla_len);
395
396 /* OVS_PACKET_ATTR_EGRESS_TUN_KEY */
397 if (upcall_info->egress_tun_info)
398 size += nla_total_size(ovs_tun_key_attr_size());
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100399
400 return size;
401}
402
Thomas Graf8055a892013-12-13 15:22:20 +0100403static int queue_userspace_packet(struct datapath *dp, struct sk_buff *skb,
Pravin B Shelare8eedb82014-11-06 06:57:27 -0800404 const struct sw_flow_key *key,
Jesse Grossccb13522011-10-25 19:26:31 -0700405 const struct dp_upcall_info *upcall_info)
406{
407 struct ovs_header *upcall;
408 struct sk_buff *nskb = NULL;
Li RongQing4ee45ea2014-09-02 20:52:28 +0800409 struct sk_buff *user_skb = NULL; /* to be queued to userspace */
Jesse Grossccb13522011-10-25 19:26:31 -0700410 struct nlattr *nla;
Thomas Graf795449d2013-11-30 13:21:32 +0100411 struct genl_info info = {
Thomas Graf8055a892013-12-13 15:22:20 +0100412 .dst_sk = ovs_dp_get_net(dp)->genl_sock,
Thomas Graf795449d2013-11-30 13:21:32 +0100413 .snd_portid = upcall_info->portid,
414 };
415 size_t len;
Thomas Grafbda56f12013-12-13 15:22:21 +0100416 unsigned int hlen;
Thomas Graf8055a892013-12-13 15:22:20 +0100417 int err, dp_ifindex;
418
419 dp_ifindex = get_dpifindex(dp);
420 if (!dp_ifindex)
421 return -ENODEV;
Jesse Grossccb13522011-10-25 19:26:31 -0700422
423 if (vlan_tx_tag_present(skb)) {
424 nskb = skb_clone(skb, GFP_ATOMIC);
425 if (!nskb)
426 return -ENOMEM;
427
Jiri Pirko62749e22014-11-19 14:04:58 +0100428 nskb = vlan_insert_tag_set_proto(nskb, nskb->vlan_proto,
429 vlan_tx_tag_get(nskb));
Dan Carpenter8aa51d62012-05-13 08:44:18 +0000430 if (!nskb)
Jesse Grossccb13522011-10-25 19:26:31 -0700431 return -ENOMEM;
432
433 nskb->vlan_tci = 0;
434 skb = nskb;
435 }
436
437 if (nla_attr_size(skb->len) > USHRT_MAX) {
438 err = -EFBIG;
439 goto out;
440 }
441
Thomas Grafbda56f12013-12-13 15:22:21 +0100442 /* Complete checksum if needed */
443 if (skb->ip_summed == CHECKSUM_PARTIAL &&
444 (err = skb_checksum_help(skb)))
445 goto out;
446
447 /* Older versions of OVS user space enforce alignment of the last
448 * Netlink attribute to NLA_ALIGNTO which would require extensive
449 * padding logic. Only perform zerocopy if padding is not required.
450 */
451 if (dp->user_features & OVS_DP_F_UNALIGNED)
452 hlen = skb_zerocopy_headlen(skb);
453 else
454 hlen = skb->len;
455
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800456 len = upcall_msg_size(upcall_info, hlen);
Thomas Graf795449d2013-11-30 13:21:32 +0100457 user_skb = genlmsg_new_unicast(len, &info, GFP_ATOMIC);
Jesse Grossccb13522011-10-25 19:26:31 -0700458 if (!user_skb) {
459 err = -ENOMEM;
460 goto out;
461 }
462
463 upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
464 0, upcall_info->cmd);
465 upcall->dp_ifindex = dp_ifindex;
466
467 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
Pravin B Shelare8eedb82014-11-06 06:57:27 -0800468 err = ovs_nla_put_flow(key, key, user_skb);
Andy Zhouf53e3832014-07-17 15:17:44 -0700469 BUG_ON(err);
Jesse Grossccb13522011-10-25 19:26:31 -0700470 nla_nest_end(user_skb, nla);
471
472 if (upcall_info->userdata)
Ben Pfaff44901082013-02-15 17:29:22 -0800473 __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA,
474 nla_len(upcall_info->userdata),
475 nla_data(upcall_info->userdata));
Jesse Grossccb13522011-10-25 19:26:31 -0700476
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800477 if (upcall_info->egress_tun_info) {
478 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_EGRESS_TUN_KEY);
479 err = ovs_nla_put_egress_tunnel_key(user_skb,
480 upcall_info->egress_tun_info);
481 BUG_ON(err);
482 nla_nest_end(user_skb, nla);
483 }
484
Thomas Grafbda56f12013-12-13 15:22:21 +0100485 /* Only reserve room for attribute header, packet data is added
486 * in skb_zerocopy() */
487 if (!(nla = nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, 0))) {
488 err = -ENOBUFS;
489 goto out;
490 }
491 nla->nla_len = nla_attr_size(skb->len);
Jesse Grossccb13522011-10-25 19:26:31 -0700492
Zoltan Kiss36d5fe62014-03-26 22:37:45 +0000493 err = skb_zerocopy(user_skb, skb, skb->len, hlen);
494 if (err)
495 goto out;
Jesse Grossccb13522011-10-25 19:26:31 -0700496
Thomas Grafaea0bb42014-01-14 16:27:49 +0000497 /* Pad OVS_PACKET_ATTR_PACKET if linear copy was performed */
498 if (!(dp->user_features & OVS_DP_F_UNALIGNED)) {
499 size_t plen = NLA_ALIGN(user_skb->len) - user_skb->len;
500
501 if (plen > 0)
502 memset(skb_put(user_skb, plen), 0, plen);
503 }
504
Thomas Grafbda56f12013-12-13 15:22:21 +0100505 ((struct nlmsghdr *) user_skb->data)->nlmsg_len = user_skb->len;
506
Thomas Graf8055a892013-12-13 15:22:20 +0100507 err = genlmsg_unicast(ovs_dp_get_net(dp), user_skb, upcall_info->portid);
Li RongQing4ee45ea2014-09-02 20:52:28 +0800508 user_skb = NULL;
Jesse Grossccb13522011-10-25 19:26:31 -0700509out:
Zoltan Kiss36d5fe62014-03-26 22:37:45 +0000510 if (err)
511 skb_tx_error(skb);
Li RongQing4ee45ea2014-09-02 20:52:28 +0800512 kfree_skb(user_skb);
Jesse Grossccb13522011-10-25 19:26:31 -0700513 kfree_skb(nskb);
514 return err;
515}
516
Jesse Grossccb13522011-10-25 19:26:31 -0700517static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
518{
519 struct ovs_header *ovs_header = info->userhdr;
520 struct nlattr **a = info->attrs;
521 struct sw_flow_actions *acts;
522 struct sk_buff *packet;
523 struct sw_flow *flow;
Lorand Jakabd98612b2014-10-06 05:45:32 -0700524 struct sw_flow_actions *sf_acts;
Jesse Grossccb13522011-10-25 19:26:31 -0700525 struct datapath *dp;
526 struct ethhdr *eth;
Pravin B Shelar83c8df22014-09-15 19:20:31 -0700527 struct vport *input_vport;
Jesse Grossccb13522011-10-25 19:26:31 -0700528 int len;
529 int err;
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800530 bool log = !a[OVS_FLOW_ATTR_PROBE];
Jesse Grossccb13522011-10-25 19:26:31 -0700531
532 err = -EINVAL;
533 if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
Thomas Grafdded45fc2013-03-29 14:46:47 +0100534 !a[OVS_PACKET_ATTR_ACTIONS])
Jesse Grossccb13522011-10-25 19:26:31 -0700535 goto err;
536
537 len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
538 packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
539 err = -ENOMEM;
540 if (!packet)
541 goto err;
542 skb_reserve(packet, NET_IP_ALIGN);
543
Thomas Graf32686a92013-03-29 14:46:48 +0100544 nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
Jesse Grossccb13522011-10-25 19:26:31 -0700545
546 skb_reset_mac_header(packet);
547 eth = eth_hdr(packet);
548
549 /* Normally, setting the skb 'protocol' field would be handled by a
550 * call to eth_type_trans(), but it assumes there's a sending
551 * device, which we may not have. */
Simon Hormane5c5d222013-03-28 13:38:25 +0900552 if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN)
Jesse Grossccb13522011-10-25 19:26:31 -0700553 packet->protocol = eth->h_proto;
554 else
555 packet->protocol = htons(ETH_P_802_2);
556
557 /* Build an sw_flow for sending this packet. */
Jarno Rajahalme23dabf82014-03-27 12:35:23 -0700558 flow = ovs_flow_alloc();
Jesse Grossccb13522011-10-25 19:26:31 -0700559 err = PTR_ERR(flow);
560 if (IS_ERR(flow))
561 goto err_kfree_skb;
562
Pravin B Shelar83c8df22014-09-15 19:20:31 -0700563 err = ovs_flow_key_extract_userspace(a[OVS_PACKET_ATTR_KEY], packet,
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800564 &flow->key, log);
Jesse Grossccb13522011-10-25 19:26:31 -0700565 if (err)
566 goto err_flow_free;
567
Pravin B Shelare6445712013-10-03 18:16:47 -0700568 err = ovs_nla_copy_actions(a[OVS_PACKET_ATTR_ACTIONS],
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800569 &flow->key, &acts, log);
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700570 if (err)
571 goto err_flow_free;
Jesse Grossccb13522011-10-25 19:26:31 -0700572
Jesse Grossf5796682014-10-03 15:35:33 -0700573 rcu_assign_pointer(flow->sf_acts, acts);
Jesse Grossf5796682014-10-03 15:35:33 -0700574 OVS_CB(packet)->egress_tun_info = NULL;
Jesse Grossccb13522011-10-25 19:26:31 -0700575 packet->priority = flow->key.phy.priority;
Ansis Atteka39c7caeb2012-11-26 11:24:11 -0800576 packet->mark = flow->key.phy.skb_mark;
Jesse Grossccb13522011-10-25 19:26:31 -0700577
578 rcu_read_lock();
Andy Zhoucc3a5ae2014-09-08 13:14:22 -0700579 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -0700580 err = -ENODEV;
581 if (!dp)
582 goto err_unlock;
583
Pravin B Shelar83c8df22014-09-15 19:20:31 -0700584 input_vport = ovs_vport_rcu(dp, flow->key.phy.in_port);
585 if (!input_vport)
586 input_vport = ovs_vport_rcu(dp, OVSP_LOCAL);
587
588 if (!input_vport)
589 goto err_unlock;
590
591 OVS_CB(packet)->input_vport = input_vport;
Lorand Jakabd98612b2014-10-06 05:45:32 -0700592 sf_acts = rcu_dereference(flow->sf_acts);
Pravin B Shelar83c8df22014-09-15 19:20:31 -0700593
Jesse Grossccb13522011-10-25 19:26:31 -0700594 local_bh_disable();
Lorand Jakabd98612b2014-10-06 05:45:32 -0700595 err = ovs_execute_actions(dp, packet, sf_acts, &flow->key);
Jesse Grossccb13522011-10-25 19:26:31 -0700596 local_bh_enable();
597 rcu_read_unlock();
598
Andy Zhou03f0d912013-08-07 20:01:00 -0700599 ovs_flow_free(flow, false);
Jesse Grossccb13522011-10-25 19:26:31 -0700600 return err;
601
602err_unlock:
603 rcu_read_unlock();
604err_flow_free:
Andy Zhou03f0d912013-08-07 20:01:00 -0700605 ovs_flow_free(flow, false);
Jesse Grossccb13522011-10-25 19:26:31 -0700606err_kfree_skb:
607 kfree_skb(packet);
608err:
609 return err;
610}
611
612static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
Thomas Grafdded45fc2013-03-29 14:46:47 +0100613 [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
Jesse Grossccb13522011-10-25 19:26:31 -0700614 [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
615 [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
616};
617
Johannes Berg4534de82013-11-14 17:14:46 +0100618static const struct genl_ops dp_packet_genl_ops[] = {
Jesse Grossccb13522011-10-25 19:26:31 -0700619 { .cmd = OVS_PACKET_CMD_EXECUTE,
620 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
621 .policy = packet_policy,
622 .doit = ovs_packet_cmd_execute
623 }
624};
625
Pravin B Shelar0c200ef2014-05-06 16:44:50 -0700626static struct genl_family dp_packet_genl_family = {
627 .id = GENL_ID_GENERATE,
628 .hdrsize = sizeof(struct ovs_header),
629 .name = OVS_PACKET_FAMILY,
630 .version = OVS_PACKET_VERSION,
631 .maxattr = OVS_PACKET_ATTR_MAX,
632 .netnsok = true,
633 .parallel_ops = true,
634 .ops = dp_packet_genl_ops,
635 .n_ops = ARRAY_SIZE(dp_packet_genl_ops),
636};
637
Thomas Graf12eb18f2014-11-06 06:58:52 -0800638static void get_dp_stats(const struct datapath *dp, struct ovs_dp_stats *stats,
Andy Zhou1bd71162013-10-22 10:42:46 -0700639 struct ovs_dp_megaflow_stats *mega_stats)
Jesse Grossccb13522011-10-25 19:26:31 -0700640{
641 int i;
Jesse Grossccb13522011-10-25 19:26:31 -0700642
Andy Zhou1bd71162013-10-22 10:42:46 -0700643 memset(mega_stats, 0, sizeof(*mega_stats));
644
Pravin B Shelarb637e492013-10-04 00:14:23 -0700645 stats->n_flows = ovs_flow_tbl_count(&dp->table);
Andy Zhou1bd71162013-10-22 10:42:46 -0700646 mega_stats->n_masks = ovs_flow_tbl_num_masks(&dp->table);
Jesse Grossccb13522011-10-25 19:26:31 -0700647
648 stats->n_hit = stats->n_missed = stats->n_lost = 0;
Andy Zhou1bd71162013-10-22 10:42:46 -0700649
Jesse Grossccb13522011-10-25 19:26:31 -0700650 for_each_possible_cpu(i) {
651 const struct dp_stats_percpu *percpu_stats;
652 struct dp_stats_percpu local_stats;
653 unsigned int start;
654
655 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
656
657 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700658 start = u64_stats_fetch_begin_irq(&percpu_stats->syncp);
Jesse Grossccb13522011-10-25 19:26:31 -0700659 local_stats = *percpu_stats;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700660 } while (u64_stats_fetch_retry_irq(&percpu_stats->syncp, start));
Jesse Grossccb13522011-10-25 19:26:31 -0700661
662 stats->n_hit += local_stats.n_hit;
663 stats->n_missed += local_stats.n_missed;
664 stats->n_lost += local_stats.n_lost;
Andy Zhou1bd71162013-10-22 10:42:46 -0700665 mega_stats->n_mask_hit += local_stats.n_mask_hit;
Jesse Grossccb13522011-10-25 19:26:31 -0700666 }
667}
668
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100669static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts)
670{
671 return NLMSG_ALIGN(sizeof(struct ovs_header))
Joe Stringer41af73e2014-10-18 16:14:14 -0700672 + nla_total_size(ovs_key_attr_size()) /* OVS_FLOW_ATTR_KEY */
673 + nla_total_size(ovs_key_attr_size()) /* OVS_FLOW_ATTR_MASK */
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100674 + nla_total_size(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
675 + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
676 + nla_total_size(8) /* OVS_FLOW_ATTR_USED */
677 + nla_total_size(acts->actions_len); /* OVS_FLOW_ATTR_ACTIONS */
678}
679
Jarno Rajahalmebb6f9a72014-05-05 11:32:17 -0700680/* Called with ovs_mutex or RCU read lock. */
Joe Stringerca7105f2014-09-08 13:09:37 -0700681static int ovs_flow_cmd_fill_match(const struct sw_flow *flow,
682 struct sk_buff *skb)
Jesse Grossccb13522011-10-25 19:26:31 -0700683{
Jesse Grossccb13522011-10-25 19:26:31 -0700684 struct nlattr *nla;
Jesse Grossccb13522011-10-25 19:26:31 -0700685 int err;
686
Andy Zhou03f0d912013-08-07 20:01:00 -0700687 /* Fill flow key. */
Jesse Grossccb13522011-10-25 19:26:31 -0700688 nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
689 if (!nla)
Joe Stringerca7105f2014-09-08 13:09:37 -0700690 return -EMSGSIZE;
Andy Zhou03f0d912013-08-07 20:01:00 -0700691
Pravin B Shelare6445712013-10-03 18:16:47 -0700692 err = ovs_nla_put_flow(&flow->unmasked_key, &flow->unmasked_key, skb);
Jesse Grossccb13522011-10-25 19:26:31 -0700693 if (err)
Joe Stringerca7105f2014-09-08 13:09:37 -0700694 return err;
695
Jesse Grossccb13522011-10-25 19:26:31 -0700696 nla_nest_end(skb, nla);
697
Joe Stringerca7105f2014-09-08 13:09:37 -0700698 /* Fill flow mask. */
Andy Zhou03f0d912013-08-07 20:01:00 -0700699 nla = nla_nest_start(skb, OVS_FLOW_ATTR_MASK);
700 if (!nla)
Joe Stringerca7105f2014-09-08 13:09:37 -0700701 return -EMSGSIZE;
Andy Zhou03f0d912013-08-07 20:01:00 -0700702
Pravin B Shelare6445712013-10-03 18:16:47 -0700703 err = ovs_nla_put_flow(&flow->key, &flow->mask->key, skb);
Andy Zhou03f0d912013-08-07 20:01:00 -0700704 if (err)
Joe Stringerca7105f2014-09-08 13:09:37 -0700705 return err;
Andy Zhou03f0d912013-08-07 20:01:00 -0700706
707 nla_nest_end(skb, nla);
Joe Stringerca7105f2014-09-08 13:09:37 -0700708 return 0;
709}
710
711/* Called with ovs_mutex or RCU read lock. */
712static int ovs_flow_cmd_fill_stats(const struct sw_flow *flow,
713 struct sk_buff *skb)
714{
715 struct ovs_flow_stats stats;
716 __be16 tcp_flags;
717 unsigned long used;
Andy Zhou03f0d912013-08-07 20:01:00 -0700718
Pravin B Shelare298e502013-10-29 17:22:21 -0700719 ovs_flow_stats_get(flow, &stats, &used, &tcp_flags);
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700720
David S. Miller028d6a62012-03-29 23:20:48 -0400721 if (used &&
722 nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
Joe Stringerca7105f2014-09-08 13:09:37 -0700723 return -EMSGSIZE;
Jesse Grossccb13522011-10-25 19:26:31 -0700724
David S. Miller028d6a62012-03-29 23:20:48 -0400725 if (stats.n_packets &&
Pravin B Shelare298e502013-10-29 17:22:21 -0700726 nla_put(skb, OVS_FLOW_ATTR_STATS, sizeof(struct ovs_flow_stats), &stats))
Joe Stringerca7105f2014-09-08 13:09:37 -0700727 return -EMSGSIZE;
Jesse Grossccb13522011-10-25 19:26:31 -0700728
Pravin B Shelare298e502013-10-29 17:22:21 -0700729 if ((u8)ntohs(tcp_flags) &&
730 nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, (u8)ntohs(tcp_flags)))
Joe Stringerca7105f2014-09-08 13:09:37 -0700731 return -EMSGSIZE;
732
733 return 0;
734}
735
736/* Called with ovs_mutex or RCU read lock. */
737static int ovs_flow_cmd_fill_actions(const struct sw_flow *flow,
738 struct sk_buff *skb, int skb_orig_len)
739{
740 struct nlattr *start;
741 int err;
Jesse Grossccb13522011-10-25 19:26:31 -0700742
743 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
744 * this is the first flow to be dumped into 'skb'. This is unusual for
745 * Netlink but individual action lists can be longer than
746 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
747 * The userspace caller can always fetch the actions separately if it
748 * really wants them. (Most userspace callers in fact don't care.)
749 *
750 * This can only fail for dump operations because the skb is always
751 * properly sized for single flows.
752 */
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700753 start = nla_nest_start(skb, OVS_FLOW_ATTR_ACTIONS);
754 if (start) {
Pravin B Shelard57170b2013-07-30 15:39:39 -0700755 const struct sw_flow_actions *sf_acts;
756
Jesse Gross663efa32013-12-03 10:58:53 -0800757 sf_acts = rcu_dereference_ovsl(flow->sf_acts);
Pravin B Shelare6445712013-10-03 18:16:47 -0700758 err = ovs_nla_put_actions(sf_acts->actions,
759 sf_acts->actions_len, skb);
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700760
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700761 if (!err)
762 nla_nest_end(skb, start);
763 else {
764 if (skb_orig_len)
Joe Stringerca7105f2014-09-08 13:09:37 -0700765 return err;
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700766
767 nla_nest_cancel(skb, start);
768 }
Joe Stringerca7105f2014-09-08 13:09:37 -0700769 } else if (skb_orig_len) {
770 return -EMSGSIZE;
771 }
772
773 return 0;
774}
775
776/* Called with ovs_mutex or RCU read lock. */
777static int ovs_flow_cmd_fill_info(const struct sw_flow *flow, int dp_ifindex,
778 struct sk_buff *skb, u32 portid,
779 u32 seq, u32 flags, u8 cmd)
780{
781 const int skb_orig_len = skb->len;
782 struct ovs_header *ovs_header;
783 int err;
784
785 ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family,
786 flags, cmd);
787 if (!ovs_header)
788 return -EMSGSIZE;
789
790 ovs_header->dp_ifindex = dp_ifindex;
791
792 err = ovs_flow_cmd_fill_match(flow, skb);
793 if (err)
794 goto error;
795
796 err = ovs_flow_cmd_fill_stats(flow, skb);
797 if (err)
798 goto error;
799
800 err = ovs_flow_cmd_fill_actions(flow, skb, skb_orig_len);
801 if (err)
802 goto error;
Jesse Grossccb13522011-10-25 19:26:31 -0700803
804 return genlmsg_end(skb, ovs_header);
805
Jesse Grossccb13522011-10-25 19:26:31 -0700806error:
807 genlmsg_cancel(skb, ovs_header);
808 return err;
809}
810
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700811/* May not be called with RCU read lock. */
812static struct sk_buff *ovs_flow_cmd_alloc_info(const struct sw_flow_actions *acts,
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700813 struct genl_info *info,
814 bool always)
Jesse Grossccb13522011-10-25 19:26:31 -0700815{
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700816 struct sk_buff *skb;
Jesse Grossccb13522011-10-25 19:26:31 -0700817
Samuel Gauthier9b67aa42014-09-18 10:31:04 +0200818 if (!always && !ovs_must_notify(&dp_flow_genl_family, info, 0))
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700819 return NULL;
820
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700821 skb = genlmsg_new_unicast(ovs_flow_cmd_msg_size(acts), info, GFP_KERNEL);
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700822 if (!skb)
823 return ERR_PTR(-ENOMEM);
824
825 return skb;
Jesse Grossccb13522011-10-25 19:26:31 -0700826}
827
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700828/* Called with ovs_mutex. */
829static struct sk_buff *ovs_flow_cmd_build_info(const struct sw_flow *flow,
830 int dp_ifindex,
831 struct genl_info *info, u8 cmd,
832 bool always)
Jesse Grossccb13522011-10-25 19:26:31 -0700833{
834 struct sk_buff *skb;
835 int retval;
836
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700837 skb = ovs_flow_cmd_alloc_info(ovsl_dereference(flow->sf_acts), info,
838 always);
Himangi Saraogid0e992a2014-07-27 12:37:46 +0530839 if (IS_ERR_OR_NULL(skb))
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700840 return skb;
Jesse Grossccb13522011-10-25 19:26:31 -0700841
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700842 retval = ovs_flow_cmd_fill_info(flow, dp_ifindex, skb,
843 info->snd_portid, info->snd_seq, 0,
844 cmd);
Jesse Grossccb13522011-10-25 19:26:31 -0700845 BUG_ON(retval < 0);
846 return skb;
847}
848
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700849static int ovs_flow_cmd_new(struct sk_buff *skb, struct genl_info *info)
Jesse Grossccb13522011-10-25 19:26:31 -0700850{
851 struct nlattr **a = info->attrs;
852 struct ovs_header *ovs_header = info->userhdr;
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700853 struct sw_flow *flow, *new_flow;
Andy Zhou03f0d912013-08-07 20:01:00 -0700854 struct sw_flow_mask mask;
Jesse Grossccb13522011-10-25 19:26:31 -0700855 struct sk_buff *reply;
856 struct datapath *dp;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700857 struct sw_flow_actions *acts;
858 struct sw_flow_match match;
859 int error;
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800860 bool log = !a[OVS_FLOW_ATTR_PROBE];
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700861
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700862 /* Must have key and actions. */
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700863 error = -EINVAL;
Jesse Gross426cda52014-10-06 05:08:38 -0700864 if (!a[OVS_FLOW_ATTR_KEY]) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800865 OVS_NLERR(log, "Flow key attr not present in new flow.");
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700866 goto error;
Jesse Gross426cda52014-10-06 05:08:38 -0700867 }
868 if (!a[OVS_FLOW_ATTR_ACTIONS]) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800869 OVS_NLERR(log, "Flow actions attr not present in new flow.");
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700870 goto error;
Jesse Gross426cda52014-10-06 05:08:38 -0700871 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700872
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700873 /* Most of the time we need to allocate a new flow, do it before
874 * locking.
875 */
876 new_flow = ovs_flow_alloc();
877 if (IS_ERR(new_flow)) {
878 error = PTR_ERR(new_flow);
879 goto error;
880 }
881
882 /* Extract key. */
883 ovs_match_init(&match, &new_flow->unmasked_key, &mask);
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800884 error = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY],
885 a[OVS_FLOW_ATTR_MASK], log);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700886 if (error)
887 goto err_kfree_flow;
888
889 ovs_flow_mask_key(&new_flow->key, &new_flow->unmasked_key, &mask);
890
891 /* Validate actions. */
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700892 error = ovs_nla_copy_actions(a[OVS_FLOW_ATTR_ACTIONS], &new_flow->key,
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800893 &acts, log);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700894 if (error) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800895 OVS_NLERR(log, "Flow actions may not be safe on all matching packets.");
Pravin B Shelar2fdb9572014-10-19 11:19:51 -0700896 goto err_kfree_flow;
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700897 }
898
899 reply = ovs_flow_cmd_alloc_info(acts, info, false);
900 if (IS_ERR(reply)) {
901 error = PTR_ERR(reply);
902 goto err_kfree_acts;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700903 }
904
905 ovs_lock();
906 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700907 if (unlikely(!dp)) {
908 error = -ENODEV;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700909 goto err_unlock_ovs;
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700910 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700911 /* Check if this is a duplicate flow */
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700912 flow = ovs_flow_tbl_lookup(&dp->table, &new_flow->unmasked_key);
913 if (likely(!flow)) {
914 rcu_assign_pointer(new_flow->sf_acts, acts);
915
916 /* Put flow in bucket. */
917 error = ovs_flow_tbl_insert(&dp->table, new_flow, &mask);
918 if (unlikely(error)) {
919 acts = NULL;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700920 goto err_unlock_ovs;
921 }
922
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700923 if (unlikely(reply)) {
924 error = ovs_flow_cmd_fill_info(new_flow,
925 ovs_header->dp_ifindex,
926 reply, info->snd_portid,
927 info->snd_seq, 0,
928 OVS_FLOW_CMD_NEW);
929 BUG_ON(error < 0);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700930 }
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700931 ovs_unlock();
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700932 } else {
933 struct sw_flow_actions *old_acts;
934
935 /* Bail out if we're not allowed to modify an existing flow.
936 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
937 * because Generic Netlink treats the latter as a dump
938 * request. We also accept NLM_F_EXCL in case that bug ever
939 * gets fixed.
940 */
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700941 if (unlikely(info->nlhdr->nlmsg_flags & (NLM_F_CREATE
942 | NLM_F_EXCL))) {
943 error = -EEXIST;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700944 goto err_unlock_ovs;
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700945 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700946 /* The unmasked key has to be the same for flow updates. */
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700947 if (unlikely(!ovs_flow_cmp_unmasked_key(flow, &match))) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800948 /* Look for any overlapping flow. */
Alex Wang4a46b242014-06-30 20:30:29 -0700949 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
950 if (!flow) {
951 error = -ENOENT;
952 goto err_unlock_ovs;
953 }
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700954 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700955 /* Update actions. */
956 old_acts = ovsl_dereference(flow->sf_acts);
957 rcu_assign_pointer(flow->sf_acts, acts);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700958
959 if (unlikely(reply)) {
960 error = ovs_flow_cmd_fill_info(flow,
961 ovs_header->dp_ifindex,
962 reply, info->snd_portid,
963 info->snd_seq, 0,
964 OVS_FLOW_CMD_NEW);
965 BUG_ON(error < 0);
966 }
967 ovs_unlock();
968
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700969 ovs_nla_free_flow_actions(old_acts);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700970 ovs_flow_free(new_flow, false);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700971 }
972
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700973 if (reply)
974 ovs_notify(&dp_flow_genl_family, reply, info);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700975 return 0;
976
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700977err_unlock_ovs:
978 ovs_unlock();
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700979 kfree_skb(reply);
980err_kfree_acts:
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700981 kfree(acts);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700982err_kfree_flow:
983 ovs_flow_free(new_flow, false);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700984error:
985 return error;
986}
987
Pravin B Shelar2fdb9572014-10-19 11:19:51 -0700988/* Factor out action copy to avoid "Wframe-larger-than=1024" warning. */
Jesse Gross6b205b22014-10-03 15:35:32 -0700989static struct sw_flow_actions *get_flow_actions(const struct nlattr *a,
990 const struct sw_flow_key *key,
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800991 const struct sw_flow_mask *mask,
992 bool log)
Jesse Gross6b205b22014-10-03 15:35:32 -0700993{
994 struct sw_flow_actions *acts;
995 struct sw_flow_key masked_key;
996 int error;
997
Jesse Gross6b205b22014-10-03 15:35:32 -0700998 ovs_flow_mask_key(&masked_key, key, mask);
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800999 error = ovs_nla_copy_actions(a, &masked_key, &acts, log);
Jesse Gross6b205b22014-10-03 15:35:32 -07001000 if (error) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001001 OVS_NLERR(log,
1002 "Actions may not be safe on all matching packets");
Jesse Gross6b205b22014-10-03 15:35:32 -07001003 return ERR_PTR(error);
1004 }
1005
1006 return acts;
1007}
1008
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001009static int ovs_flow_cmd_set(struct sk_buff *skb, struct genl_info *info)
1010{
1011 struct nlattr **a = info->attrs;
1012 struct ovs_header *ovs_header = info->userhdr;
Jesse Gross6b205b22014-10-03 15:35:32 -07001013 struct sw_flow_key key;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001014 struct sw_flow *flow;
1015 struct sw_flow_mask mask;
1016 struct sk_buff *reply = NULL;
1017 struct datapath *dp;
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001018 struct sw_flow_actions *old_acts = NULL, *acts = NULL;
Andy Zhou03f0d912013-08-07 20:01:00 -07001019 struct sw_flow_match match;
Jesse Grossccb13522011-10-25 19:26:31 -07001020 int error;
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001021 bool log = !a[OVS_FLOW_ATTR_PROBE];
Jesse Grossccb13522011-10-25 19:26:31 -07001022
1023 /* Extract key. */
1024 error = -EINVAL;
Jesse Gross426cda52014-10-06 05:08:38 -07001025 if (!a[OVS_FLOW_ATTR_KEY]) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001026 OVS_NLERR(log, "Flow key attribute not present in set flow.");
Jesse Grossccb13522011-10-25 19:26:31 -07001027 goto error;
Jesse Gross426cda52014-10-06 05:08:38 -07001028 }
Andy Zhou03f0d912013-08-07 20:01:00 -07001029
1030 ovs_match_init(&match, &key, &mask);
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001031 error = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY],
1032 a[OVS_FLOW_ATTR_MASK], log);
Jesse Grossccb13522011-10-25 19:26:31 -07001033 if (error)
1034 goto error;
1035
1036 /* Validate actions. */
1037 if (a[OVS_FLOW_ATTR_ACTIONS]) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001038 acts = get_flow_actions(a[OVS_FLOW_ATTR_ACTIONS], &key, &mask,
1039 log);
Jesse Gross6b205b22014-10-03 15:35:32 -07001040 if (IS_ERR(acts)) {
1041 error = PTR_ERR(acts);
Jesse Grossccb13522011-10-25 19:26:31 -07001042 goto error;
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001043 }
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001044
Pravin B Shelar2fdb9572014-10-19 11:19:51 -07001045 /* Can allocate before locking if have acts. */
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001046 reply = ovs_flow_cmd_alloc_info(acts, info, false);
1047 if (IS_ERR(reply)) {
1048 error = PTR_ERR(reply);
1049 goto err_kfree_acts;
Andy Zhou03f0d912013-08-07 20:01:00 -07001050 }
Jesse Grossccb13522011-10-25 19:26:31 -07001051 }
1052
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001053 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001054 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001055 if (unlikely(!dp)) {
1056 error = -ENODEV;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001057 goto err_unlock_ovs;
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001058 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001059 /* Check that the flow exists. */
Alex Wang4a46b242014-06-30 20:30:29 -07001060 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001061 if (unlikely(!flow)) {
1062 error = -ENOENT;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001063 goto err_unlock_ovs;
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001064 }
Alex Wang4a46b242014-06-30 20:30:29 -07001065
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001066 /* Update actions, if present. */
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001067 if (likely(acts)) {
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001068 old_acts = ovsl_dereference(flow->sf_acts);
Jesse Grossccb13522011-10-25 19:26:31 -07001069 rcu_assign_pointer(flow->sf_acts, acts);
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001070
1071 if (unlikely(reply)) {
1072 error = ovs_flow_cmd_fill_info(flow,
1073 ovs_header->dp_ifindex,
1074 reply, info->snd_portid,
1075 info->snd_seq, 0,
1076 OVS_FLOW_CMD_NEW);
1077 BUG_ON(error < 0);
1078 }
1079 } else {
1080 /* Could not alloc without acts before locking. */
1081 reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex,
1082 info, OVS_FLOW_CMD_NEW, false);
1083 if (unlikely(IS_ERR(reply))) {
1084 error = PTR_ERR(reply);
1085 goto err_unlock_ovs;
1086 }
Jesse Grossccb13522011-10-25 19:26:31 -07001087 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001088
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001089 /* Clear stats. */
1090 if (a[OVS_FLOW_ATTR_CLEAR])
1091 ovs_flow_stats_clear(flow);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001092 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001093
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001094 if (reply)
1095 ovs_notify(&dp_flow_genl_family, reply, info);
1096 if (old_acts)
1097 ovs_nla_free_flow_actions(old_acts);
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -07001098
Jesse Grossccb13522011-10-25 19:26:31 -07001099 return 0;
1100
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001101err_unlock_ovs:
1102 ovs_unlock();
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001103 kfree_skb(reply);
1104err_kfree_acts:
Pravin B Shelar74f84a52013-06-17 17:50:12 -07001105 kfree(acts);
Jesse Grossccb13522011-10-25 19:26:31 -07001106error:
1107 return error;
1108}
1109
1110static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1111{
1112 struct nlattr **a = info->attrs;
1113 struct ovs_header *ovs_header = info->userhdr;
1114 struct sw_flow_key key;
1115 struct sk_buff *reply;
1116 struct sw_flow *flow;
1117 struct datapath *dp;
Andy Zhou03f0d912013-08-07 20:01:00 -07001118 struct sw_flow_match match;
Jesse Grossccb13522011-10-25 19:26:31 -07001119 int err;
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001120 bool log = !a[OVS_FLOW_ATTR_PROBE];
Jesse Grossccb13522011-10-25 19:26:31 -07001121
Andy Zhou03f0d912013-08-07 20:01:00 -07001122 if (!a[OVS_FLOW_ATTR_KEY]) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001123 OVS_NLERR(log,
1124 "Flow get message rejected, Key attribute missing.");
Jesse Grossccb13522011-10-25 19:26:31 -07001125 return -EINVAL;
Andy Zhou03f0d912013-08-07 20:01:00 -07001126 }
1127
1128 ovs_match_init(&match, &key, NULL);
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001129 err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL, log);
Jesse Grossccb13522011-10-25 19:26:31 -07001130 if (err)
1131 return err;
1132
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001133 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001134 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001135 if (!dp) {
1136 err = -ENODEV;
1137 goto unlock;
1138 }
Jesse Grossccb13522011-10-25 19:26:31 -07001139
Alex Wang4a46b242014-06-30 20:30:29 -07001140 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1141 if (!flow) {
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001142 err = -ENOENT;
1143 goto unlock;
1144 }
Jesse Grossccb13522011-10-25 19:26:31 -07001145
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -07001146 reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex, info,
1147 OVS_FLOW_CMD_NEW, true);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001148 if (IS_ERR(reply)) {
1149 err = PTR_ERR(reply);
1150 goto unlock;
1151 }
Jesse Grossccb13522011-10-25 19:26:31 -07001152
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001153 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001154 return genlmsg_reply(reply, info);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001155unlock:
1156 ovs_unlock();
1157 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001158}
1159
1160static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1161{
1162 struct nlattr **a = info->attrs;
1163 struct ovs_header *ovs_header = info->userhdr;
1164 struct sw_flow_key key;
1165 struct sk_buff *reply;
1166 struct sw_flow *flow;
1167 struct datapath *dp;
Andy Zhou03f0d912013-08-07 20:01:00 -07001168 struct sw_flow_match match;
Jesse Grossccb13522011-10-25 19:26:31 -07001169 int err;
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001170 bool log = !a[OVS_FLOW_ATTR_PROBE];
Jesse Grossccb13522011-10-25 19:26:31 -07001171
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001172 if (likely(a[OVS_FLOW_ATTR_KEY])) {
1173 ovs_match_init(&match, &key, NULL);
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001174 err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL,
1175 log);
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001176 if (unlikely(err))
1177 return err;
1178 }
1179
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001180 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001181 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001182 if (unlikely(!dp)) {
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001183 err = -ENODEV;
1184 goto unlock;
1185 }
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001186
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001187 if (unlikely(!a[OVS_FLOW_ATTR_KEY])) {
Pravin B Shelarb637e492013-10-04 00:14:23 -07001188 err = ovs_flow_tbl_flush(&dp->table);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001189 goto unlock;
1190 }
Andy Zhou03f0d912013-08-07 20:01:00 -07001191
Alex Wang4a46b242014-06-30 20:30:29 -07001192 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1193 if (unlikely(!flow)) {
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001194 err = -ENOENT;
1195 goto unlock;
1196 }
Jesse Grossccb13522011-10-25 19:26:31 -07001197
Pravin B Shelarb637e492013-10-04 00:14:23 -07001198 ovs_flow_tbl_remove(&dp->table, flow);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001199 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001200
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001201 reply = ovs_flow_cmd_alloc_info((const struct sw_flow_actions __force *) flow->sf_acts,
1202 info, false);
1203 if (likely(reply)) {
1204 if (likely(!IS_ERR(reply))) {
1205 rcu_read_lock(); /*To keep RCU checker happy. */
1206 err = ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex,
1207 reply, info->snd_portid,
1208 info->snd_seq, 0,
1209 OVS_FLOW_CMD_DEL);
1210 rcu_read_unlock();
1211 BUG_ON(err < 0);
1212
1213 ovs_notify(&dp_flow_genl_family, reply, info);
1214 } else {
1215 netlink_set_err(sock_net(skb->sk)->genl_sock, 0, 0, PTR_ERR(reply));
1216 }
1217 }
1218
1219 ovs_flow_free(flow, true);
Jesse Grossccb13522011-10-25 19:26:31 -07001220 return 0;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001221unlock:
1222 ovs_unlock();
1223 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001224}
1225
1226static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1227{
1228 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
Pravin B Shelarb637e492013-10-04 00:14:23 -07001229 struct table_instance *ti;
Jesse Grossccb13522011-10-25 19:26:31 -07001230 struct datapath *dp;
Jesse Grossccb13522011-10-25 19:26:31 -07001231
Pravin B Shelard57170b2013-07-30 15:39:39 -07001232 rcu_read_lock();
Andy Zhoucc3a5ae2014-09-08 13:14:22 -07001233 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001234 if (!dp) {
Pravin B Shelard57170b2013-07-30 15:39:39 -07001235 rcu_read_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001236 return -ENODEV;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001237 }
Jesse Grossccb13522011-10-25 19:26:31 -07001238
Pravin B Shelarb637e492013-10-04 00:14:23 -07001239 ti = rcu_dereference(dp->table.ti);
Jesse Grossccb13522011-10-25 19:26:31 -07001240 for (;;) {
1241 struct sw_flow *flow;
1242 u32 bucket, obj;
1243
1244 bucket = cb->args[0];
1245 obj = cb->args[1];
Pravin B Shelarb637e492013-10-04 00:14:23 -07001246 flow = ovs_flow_tbl_dump_next(ti, &bucket, &obj);
Jesse Grossccb13522011-10-25 19:26:31 -07001247 if (!flow)
1248 break;
1249
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -07001250 if (ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex, skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001251 NETLINK_CB(cb->skb).portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001252 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1253 OVS_FLOW_CMD_NEW) < 0)
1254 break;
1255
1256 cb->args[0] = bucket;
1257 cb->args[1] = obj;
1258 }
Pravin B Shelard57170b2013-07-30 15:39:39 -07001259 rcu_read_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001260 return skb->len;
1261}
1262
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001263static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
1264 [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001265 [OVS_FLOW_ATTR_MASK] = { .type = NLA_NESTED },
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001266 [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
1267 [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001268 [OVS_FLOW_ATTR_PROBE] = { .type = NLA_FLAG },
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001269};
1270
stephen hemminger48e48a72014-07-16 11:25:52 -07001271static const struct genl_ops dp_flow_genl_ops[] = {
Jesse Grossccb13522011-10-25 19:26:31 -07001272 { .cmd = OVS_FLOW_CMD_NEW,
1273 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1274 .policy = flow_policy,
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001275 .doit = ovs_flow_cmd_new
Jesse Grossccb13522011-10-25 19:26:31 -07001276 },
1277 { .cmd = OVS_FLOW_CMD_DEL,
1278 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1279 .policy = flow_policy,
1280 .doit = ovs_flow_cmd_del
1281 },
1282 { .cmd = OVS_FLOW_CMD_GET,
1283 .flags = 0, /* OK for unprivileged users. */
1284 .policy = flow_policy,
1285 .doit = ovs_flow_cmd_get,
1286 .dumpit = ovs_flow_cmd_dump
1287 },
1288 { .cmd = OVS_FLOW_CMD_SET,
1289 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1290 .policy = flow_policy,
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001291 .doit = ovs_flow_cmd_set,
Jesse Grossccb13522011-10-25 19:26:31 -07001292 },
1293};
1294
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001295static struct genl_family dp_flow_genl_family = {
Jesse Grossccb13522011-10-25 19:26:31 -07001296 .id = GENL_ID_GENERATE,
1297 .hdrsize = sizeof(struct ovs_header),
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001298 .name = OVS_FLOW_FAMILY,
1299 .version = OVS_FLOW_VERSION,
1300 .maxattr = OVS_FLOW_ATTR_MAX,
Pravin B Shelar3a4e0d62013-04-23 07:48:48 +00001301 .netnsok = true,
1302 .parallel_ops = true,
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001303 .ops = dp_flow_genl_ops,
1304 .n_ops = ARRAY_SIZE(dp_flow_genl_ops),
1305 .mcgrps = &ovs_dp_flow_multicast_group,
1306 .n_mcgrps = 1,
Jesse Grossccb13522011-10-25 19:26:31 -07001307};
1308
Thomas Grafc3ff8cf2013-03-29 14:46:49 +01001309static size_t ovs_dp_cmd_msg_size(void)
1310{
1311 size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
1312
1313 msgsize += nla_total_size(IFNAMSIZ);
1314 msgsize += nla_total_size(sizeof(struct ovs_dp_stats));
Andy Zhou1bd71162013-10-22 10:42:46 -07001315 msgsize += nla_total_size(sizeof(struct ovs_dp_megaflow_stats));
Daniele Di Proietto45fb9c32014-01-23 10:47:35 -08001316 msgsize += nla_total_size(sizeof(u32)); /* OVS_DP_ATTR_USER_FEATURES */
Thomas Grafc3ff8cf2013-03-29 14:46:49 +01001317
1318 return msgsize;
1319}
1320
Jarno Rajahalmebb6f9a72014-05-05 11:32:17 -07001321/* Called with ovs_mutex or RCU read lock. */
Jesse Grossccb13522011-10-25 19:26:31 -07001322static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001323 u32 portid, u32 seq, u32 flags, u8 cmd)
Jesse Grossccb13522011-10-25 19:26:31 -07001324{
1325 struct ovs_header *ovs_header;
1326 struct ovs_dp_stats dp_stats;
Andy Zhou1bd71162013-10-22 10:42:46 -07001327 struct ovs_dp_megaflow_stats dp_megaflow_stats;
Jesse Grossccb13522011-10-25 19:26:31 -07001328 int err;
1329
Eric W. Biederman15e47302012-09-07 20:12:54 +00001330 ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
Jesse Grossccb13522011-10-25 19:26:31 -07001331 flags, cmd);
1332 if (!ovs_header)
1333 goto error;
1334
1335 ovs_header->dp_ifindex = get_dpifindex(dp);
1336
Jesse Grossccb13522011-10-25 19:26:31 -07001337 err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
Jesse Grossccb13522011-10-25 19:26:31 -07001338 if (err)
1339 goto nla_put_failure;
1340
Andy Zhou1bd71162013-10-22 10:42:46 -07001341 get_dp_stats(dp, &dp_stats, &dp_megaflow_stats);
1342 if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats),
1343 &dp_stats))
1344 goto nla_put_failure;
1345
1346 if (nla_put(skb, OVS_DP_ATTR_MEGAFLOW_STATS,
1347 sizeof(struct ovs_dp_megaflow_stats),
1348 &dp_megaflow_stats))
David S. Miller028d6a62012-03-29 23:20:48 -04001349 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001350
Thomas Graf43d4be92013-12-13 15:22:18 +01001351 if (nla_put_u32(skb, OVS_DP_ATTR_USER_FEATURES, dp->user_features))
1352 goto nla_put_failure;
1353
Jesse Grossccb13522011-10-25 19:26:31 -07001354 return genlmsg_end(skb, ovs_header);
1355
1356nla_put_failure:
1357 genlmsg_cancel(skb, ovs_header);
1358error:
1359 return -EMSGSIZE;
1360}
1361
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001362static struct sk_buff *ovs_dp_cmd_alloc_info(struct genl_info *info)
Jesse Grossccb13522011-10-25 19:26:31 -07001363{
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001364 return genlmsg_new_unicast(ovs_dp_cmd_msg_size(), info, GFP_KERNEL);
Jesse Grossccb13522011-10-25 19:26:31 -07001365}
1366
Jarno Rajahalmebb6f9a72014-05-05 11:32:17 -07001367/* Called with rcu_read_lock or ovs_mutex. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001368static struct datapath *lookup_datapath(struct net *net,
Thomas Graf12eb18f2014-11-06 06:58:52 -08001369 const struct ovs_header *ovs_header,
Jesse Grossccb13522011-10-25 19:26:31 -07001370 struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1371{
1372 struct datapath *dp;
1373
1374 if (!a[OVS_DP_ATTR_NAME])
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001375 dp = get_dp(net, ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001376 else {
1377 struct vport *vport;
1378
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001379 vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
Jesse Grossccb13522011-10-25 19:26:31 -07001380 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
Jesse Grossccb13522011-10-25 19:26:31 -07001381 }
1382 return dp ? dp : ERR_PTR(-ENODEV);
1383}
1384
Thomas Graf44da5ae2013-12-13 15:22:19 +01001385static void ovs_dp_reset_user_features(struct sk_buff *skb, struct genl_info *info)
1386{
1387 struct datapath *dp;
1388
1389 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
Jiri Pirko3c7eacf2014-02-14 11:42:36 +01001390 if (IS_ERR(dp))
Thomas Graf44da5ae2013-12-13 15:22:19 +01001391 return;
1392
1393 WARN(dp->user_features, "Dropping previously announced user features\n");
1394 dp->user_features = 0;
1395}
1396
Thomas Graf12eb18f2014-11-06 06:58:52 -08001397static void ovs_dp_change(struct datapath *dp, struct nlattr *a[])
Thomas Graf43d4be92013-12-13 15:22:18 +01001398{
1399 if (a[OVS_DP_ATTR_USER_FEATURES])
1400 dp->user_features = nla_get_u32(a[OVS_DP_ATTR_USER_FEATURES]);
1401}
1402
Jesse Grossccb13522011-10-25 19:26:31 -07001403static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1404{
1405 struct nlattr **a = info->attrs;
1406 struct vport_parms parms;
1407 struct sk_buff *reply;
1408 struct datapath *dp;
1409 struct vport *vport;
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001410 struct ovs_net *ovs_net;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001411 int err, i;
Jesse Grossccb13522011-10-25 19:26:31 -07001412
1413 err = -EINVAL;
1414 if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1415 goto err;
1416
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001417 reply = ovs_dp_cmd_alloc_info(info);
1418 if (!reply)
1419 return -ENOMEM;
Jesse Grossccb13522011-10-25 19:26:31 -07001420
1421 err = -ENOMEM;
1422 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1423 if (dp == NULL)
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001424 goto err_free_reply;
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001425
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001426 ovs_dp_set_net(dp, hold_net(sock_net(skb->sk)));
Jesse Grossccb13522011-10-25 19:26:31 -07001427
1428 /* Allocate table. */
Pravin B Shelarb637e492013-10-04 00:14:23 -07001429 err = ovs_flow_tbl_init(&dp->table);
1430 if (err)
Jesse Grossccb13522011-10-25 19:26:31 -07001431 goto err_free_dp;
1432
WANG Cong1c213bd2014-02-13 11:46:28 -08001433 dp->stats_percpu = netdev_alloc_pcpu_stats(struct dp_stats_percpu);
Jesse Grossccb13522011-10-25 19:26:31 -07001434 if (!dp->stats_percpu) {
1435 err = -ENOMEM;
1436 goto err_destroy_table;
1437 }
1438
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001439 dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
Pravin B Shelare6445712013-10-03 18:16:47 -07001440 GFP_KERNEL);
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001441 if (!dp->ports) {
1442 err = -ENOMEM;
1443 goto err_destroy_percpu;
1444 }
1445
1446 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1447 INIT_HLIST_HEAD(&dp->ports[i]);
1448
Jesse Grossccb13522011-10-25 19:26:31 -07001449 /* Set up our datapath device. */
1450 parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1451 parms.type = OVS_VPORT_TYPE_INTERNAL;
1452 parms.options = NULL;
1453 parms.dp = dp;
1454 parms.port_no = OVSP_LOCAL;
Alex Wang5cd667b2014-07-17 15:14:13 -07001455 parms.upcall_portids = a[OVS_DP_ATTR_UPCALL_PID];
Jesse Grossccb13522011-10-25 19:26:31 -07001456
Thomas Graf43d4be92013-12-13 15:22:18 +01001457 ovs_dp_change(dp, a);
1458
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001459 /* So far only local changes have been made, now need the lock. */
1460 ovs_lock();
1461
Jesse Grossccb13522011-10-25 19:26:31 -07001462 vport = new_vport(&parms);
1463 if (IS_ERR(vport)) {
1464 err = PTR_ERR(vport);
1465 if (err == -EBUSY)
1466 err = -EEXIST;
1467
Thomas Graf44da5ae2013-12-13 15:22:19 +01001468 if (err == -EEXIST) {
1469 /* An outdated user space instance that does not understand
1470 * the concept of user_features has attempted to create a new
1471 * datapath and is likely to reuse it. Drop all user features.
1472 */
1473 if (info->genlhdr->version < OVS_DP_VER_FEATURES)
1474 ovs_dp_reset_user_features(skb, info);
1475 }
1476
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001477 goto err_destroy_ports_array;
Jesse Grossccb13522011-10-25 19:26:31 -07001478 }
1479
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001480 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1481 info->snd_seq, 0, OVS_DP_CMD_NEW);
1482 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001483
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001484 ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001485 list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001486
1487 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001488
Johannes Berg2a94fe42013-11-19 15:19:39 +01001489 ovs_notify(&dp_datapath_genl_family, reply, info);
Jesse Grossccb13522011-10-25 19:26:31 -07001490 return 0;
1491
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001492err_destroy_ports_array:
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001493 ovs_unlock();
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001494 kfree(dp->ports);
Jesse Grossccb13522011-10-25 19:26:31 -07001495err_destroy_percpu:
1496 free_percpu(dp->stats_percpu);
1497err_destroy_table:
Pravin B Shelar9b996e52014-05-06 18:41:20 -07001498 ovs_flow_tbl_destroy(&dp->table);
Jesse Grossccb13522011-10-25 19:26:31 -07001499err_free_dp:
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001500 release_net(ovs_dp_get_net(dp));
Jesse Grossccb13522011-10-25 19:26:31 -07001501 kfree(dp);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001502err_free_reply:
1503 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001504err:
1505 return err;
1506}
1507
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001508/* Called with ovs_mutex. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001509static void __dp_destroy(struct datapath *dp)
Jesse Grossccb13522011-10-25 19:26:31 -07001510{
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001511 int i;
Jesse Grossccb13522011-10-25 19:26:31 -07001512
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001513 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1514 struct vport *vport;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001515 struct hlist_node *n;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001516
Sasha Levinb67bfe02013-02-27 17:06:00 -08001517 hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001518 if (vport->port_no != OVSP_LOCAL)
1519 ovs_dp_detach_port(vport);
1520 }
Jesse Grossccb13522011-10-25 19:26:31 -07001521
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001522 list_del_rcu(&dp->list_node);
Jesse Grossccb13522011-10-25 19:26:31 -07001523
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001524 /* OVSP_LOCAL is datapath internal port. We need to make sure that
Andy Zhoue80857c2014-01-21 09:31:04 -08001525 * all ports in datapath are destroyed first before freeing datapath.
Jesse Grossccb13522011-10-25 19:26:31 -07001526 */
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001527 ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
Jesse Grossccb13522011-10-25 19:26:31 -07001528
Andy Zhoue80857c2014-01-21 09:31:04 -08001529 /* RCU destroy the flow table */
Jesse Grossccb13522011-10-25 19:26:31 -07001530 call_rcu(&dp->rcu, destroy_dp_rcu);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001531}
1532
1533static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1534{
1535 struct sk_buff *reply;
1536 struct datapath *dp;
1537 int err;
1538
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001539 reply = ovs_dp_cmd_alloc_info(info);
1540 if (!reply)
1541 return -ENOMEM;
1542
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001543 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001544 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1545 err = PTR_ERR(dp);
1546 if (IS_ERR(dp))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001547 goto err_unlock_free;
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001548
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001549 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1550 info->snd_seq, 0, OVS_DP_CMD_DEL);
1551 BUG_ON(err < 0);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001552
1553 __dp_destroy(dp);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001554 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001555
Johannes Berg2a94fe42013-11-19 15:19:39 +01001556 ovs_notify(&dp_datapath_genl_family, reply, info);
Jesse Grossccb13522011-10-25 19:26:31 -07001557
1558 return 0;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001559
1560err_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001561 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001562 kfree_skb(reply);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001563 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001564}
1565
1566static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1567{
1568 struct sk_buff *reply;
1569 struct datapath *dp;
1570 int err;
1571
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001572 reply = ovs_dp_cmd_alloc_info(info);
1573 if (!reply)
1574 return -ENOMEM;
1575
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001576 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001577 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001578 err = PTR_ERR(dp);
Jesse Grossccb13522011-10-25 19:26:31 -07001579 if (IS_ERR(dp))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001580 goto err_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001581
Thomas Graf43d4be92013-12-13 15:22:18 +01001582 ovs_dp_change(dp, info->attrs);
1583
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001584 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1585 info->snd_seq, 0, OVS_DP_CMD_NEW);
1586 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001587
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001588 ovs_unlock();
Johannes Berg2a94fe42013-11-19 15:19:39 +01001589 ovs_notify(&dp_datapath_genl_family, reply, info);
Jesse Grossccb13522011-10-25 19:26:31 -07001590
1591 return 0;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001592
1593err_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001594 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001595 kfree_skb(reply);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001596 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001597}
1598
1599static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1600{
1601 struct sk_buff *reply;
1602 struct datapath *dp;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001603 int err;
Jesse Grossccb13522011-10-25 19:26:31 -07001604
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001605 reply = ovs_dp_cmd_alloc_info(info);
1606 if (!reply)
1607 return -ENOMEM;
1608
1609 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001610 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001611 if (IS_ERR(dp)) {
1612 err = PTR_ERR(dp);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001613 goto err_unlock_free;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001614 }
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001615 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1616 info->snd_seq, 0, OVS_DP_CMD_NEW);
1617 BUG_ON(err < 0);
1618 rcu_read_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001619
Jesse Grossccb13522011-10-25 19:26:31 -07001620 return genlmsg_reply(reply, info);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001621
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001622err_unlock_free:
1623 rcu_read_unlock();
1624 kfree_skb(reply);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001625 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001626}
1627
1628static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1629{
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001630 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
Jesse Grossccb13522011-10-25 19:26:31 -07001631 struct datapath *dp;
1632 int skip = cb->args[0];
1633 int i = 0;
1634
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001635 rcu_read_lock();
1636 list_for_each_entry_rcu(dp, &ovs_net->dps, list_node) {
Ben Pfaff77676fd2012-01-17 13:33:39 +00001637 if (i >= skip &&
Eric W. Biederman15e47302012-09-07 20:12:54 +00001638 ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001639 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1640 OVS_DP_CMD_NEW) < 0)
1641 break;
1642 i++;
1643 }
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001644 rcu_read_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001645
1646 cb->args[0] = i;
1647
1648 return skb->len;
1649}
1650
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001651static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1652 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1653 [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1654 [OVS_DP_ATTR_USER_FEATURES] = { .type = NLA_U32 },
1655};
1656
stephen hemminger48e48a72014-07-16 11:25:52 -07001657static const struct genl_ops dp_datapath_genl_ops[] = {
Jesse Grossccb13522011-10-25 19:26:31 -07001658 { .cmd = OVS_DP_CMD_NEW,
1659 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1660 .policy = datapath_policy,
1661 .doit = ovs_dp_cmd_new
1662 },
1663 { .cmd = OVS_DP_CMD_DEL,
1664 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1665 .policy = datapath_policy,
1666 .doit = ovs_dp_cmd_del
1667 },
1668 { .cmd = OVS_DP_CMD_GET,
1669 .flags = 0, /* OK for unprivileged users. */
1670 .policy = datapath_policy,
1671 .doit = ovs_dp_cmd_get,
1672 .dumpit = ovs_dp_cmd_dump
1673 },
1674 { .cmd = OVS_DP_CMD_SET,
1675 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1676 .policy = datapath_policy,
1677 .doit = ovs_dp_cmd_set,
1678 },
1679};
1680
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001681static struct genl_family dp_datapath_genl_family = {
Jesse Grossccb13522011-10-25 19:26:31 -07001682 .id = GENL_ID_GENERATE,
1683 .hdrsize = sizeof(struct ovs_header),
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001684 .name = OVS_DATAPATH_FAMILY,
1685 .version = OVS_DATAPATH_VERSION,
1686 .maxattr = OVS_DP_ATTR_MAX,
Pravin B Shelar3a4e0d62013-04-23 07:48:48 +00001687 .netnsok = true,
1688 .parallel_ops = true,
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001689 .ops = dp_datapath_genl_ops,
1690 .n_ops = ARRAY_SIZE(dp_datapath_genl_ops),
1691 .mcgrps = &ovs_dp_datapath_multicast_group,
1692 .n_mcgrps = 1,
Jesse Grossccb13522011-10-25 19:26:31 -07001693};
1694
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001695/* Called with ovs_mutex or RCU read lock. */
Jesse Grossccb13522011-10-25 19:26:31 -07001696static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001697 u32 portid, u32 seq, u32 flags, u8 cmd)
Jesse Grossccb13522011-10-25 19:26:31 -07001698{
1699 struct ovs_header *ovs_header;
1700 struct ovs_vport_stats vport_stats;
1701 int err;
1702
Eric W. Biederman15e47302012-09-07 20:12:54 +00001703 ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
Jesse Grossccb13522011-10-25 19:26:31 -07001704 flags, cmd);
1705 if (!ovs_header)
1706 return -EMSGSIZE;
1707
1708 ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1709
David S. Miller028d6a62012-03-29 23:20:48 -04001710 if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
1711 nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
Alex Wang5cd667b2014-07-17 15:14:13 -07001712 nla_put_string(skb, OVS_VPORT_ATTR_NAME,
1713 vport->ops->get_name(vport)))
David S. Miller028d6a62012-03-29 23:20:48 -04001714 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001715
1716 ovs_vport_get_stats(vport, &vport_stats);
David S. Miller028d6a62012-03-29 23:20:48 -04001717 if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
1718 &vport_stats))
1719 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001720
Alex Wang5cd667b2014-07-17 15:14:13 -07001721 if (ovs_vport_get_upcall_portids(vport, skb))
1722 goto nla_put_failure;
1723
Jesse Grossccb13522011-10-25 19:26:31 -07001724 err = ovs_vport_get_options(vport, skb);
1725 if (err == -EMSGSIZE)
1726 goto error;
1727
1728 return genlmsg_end(skb, ovs_header);
1729
1730nla_put_failure:
1731 err = -EMSGSIZE;
1732error:
1733 genlmsg_cancel(skb, ovs_header);
1734 return err;
1735}
1736
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001737static struct sk_buff *ovs_vport_cmd_alloc_info(void)
1738{
1739 return nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1740}
1741
1742/* Called with ovs_mutex, only via ovs_dp_notify_wq(). */
Eric W. Biederman15e47302012-09-07 20:12:54 +00001743struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001744 u32 seq, u8 cmd)
1745{
1746 struct sk_buff *skb;
1747 int retval;
1748
1749 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1750 if (!skb)
1751 return ERR_PTR(-ENOMEM);
1752
Eric W. Biederman15e47302012-09-07 20:12:54 +00001753 retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
Jesse Grossa9341512013-03-26 15:48:38 -07001754 BUG_ON(retval < 0);
1755
Jesse Grossccb13522011-10-25 19:26:31 -07001756 return skb;
1757}
1758
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001759/* Called with ovs_mutex or RCU read lock. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001760static struct vport *lookup_vport(struct net *net,
Thomas Graf12eb18f2014-11-06 06:58:52 -08001761 const struct ovs_header *ovs_header,
Jesse Grossccb13522011-10-25 19:26:31 -07001762 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1763{
1764 struct datapath *dp;
1765 struct vport *vport;
1766
1767 if (a[OVS_VPORT_ATTR_NAME]) {
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001768 vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
Jesse Grossccb13522011-10-25 19:26:31 -07001769 if (!vport)
1770 return ERR_PTR(-ENODEV);
Ben Pfaff651a68e2012-03-06 15:04:04 -08001771 if (ovs_header->dp_ifindex &&
1772 ovs_header->dp_ifindex != get_dpifindex(vport->dp))
1773 return ERR_PTR(-ENODEV);
Jesse Grossccb13522011-10-25 19:26:31 -07001774 return vport;
1775 } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
1776 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1777
1778 if (port_no >= DP_MAX_PORTS)
1779 return ERR_PTR(-EFBIG);
1780
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001781 dp = get_dp(net, ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001782 if (!dp)
1783 return ERR_PTR(-ENODEV);
1784
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001785 vport = ovs_vport_ovsl_rcu(dp, port_no);
Jesse Grossccb13522011-10-25 19:26:31 -07001786 if (!vport)
Jarno Rajahalme14408db2013-01-09 14:27:35 -08001787 return ERR_PTR(-ENODEV);
Jesse Grossccb13522011-10-25 19:26:31 -07001788 return vport;
1789 } else
1790 return ERR_PTR(-EINVAL);
1791}
1792
1793static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
1794{
1795 struct nlattr **a = info->attrs;
1796 struct ovs_header *ovs_header = info->userhdr;
1797 struct vport_parms parms;
1798 struct sk_buff *reply;
1799 struct vport *vport;
1800 struct datapath *dp;
1801 u32 port_no;
1802 int err;
1803
Jesse Grossccb13522011-10-25 19:26:31 -07001804 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
1805 !a[OVS_VPORT_ATTR_UPCALL_PID])
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001806 return -EINVAL;
1807
1808 port_no = a[OVS_VPORT_ATTR_PORT_NO]
1809 ? nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]) : 0;
1810 if (port_no >= DP_MAX_PORTS)
1811 return -EFBIG;
1812
1813 reply = ovs_vport_cmd_alloc_info();
1814 if (!reply)
1815 return -ENOMEM;
Jesse Grossccb13522011-10-25 19:26:31 -07001816
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001817 ovs_lock();
Thomas Graf62b9c8d2014-10-22 17:29:06 +02001818restart:
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001819 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001820 err = -ENODEV;
1821 if (!dp)
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001822 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001823
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001824 if (port_no) {
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001825 vport = ovs_vport_ovsl(dp, port_no);
Jesse Grossccb13522011-10-25 19:26:31 -07001826 err = -EBUSY;
1827 if (vport)
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001828 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001829 } else {
1830 for (port_no = 1; ; port_no++) {
1831 if (port_no >= DP_MAX_PORTS) {
1832 err = -EFBIG;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001833 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001834 }
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001835 vport = ovs_vport_ovsl(dp, port_no);
Jesse Grossccb13522011-10-25 19:26:31 -07001836 if (!vport)
1837 break;
1838 }
1839 }
1840
1841 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
1842 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1843 parms.options = a[OVS_VPORT_ATTR_OPTIONS];
1844 parms.dp = dp;
1845 parms.port_no = port_no;
Alex Wang5cd667b2014-07-17 15:14:13 -07001846 parms.upcall_portids = a[OVS_VPORT_ATTR_UPCALL_PID];
Jesse Grossccb13522011-10-25 19:26:31 -07001847
1848 vport = new_vport(&parms);
1849 err = PTR_ERR(vport);
Thomas Graf62b9c8d2014-10-22 17:29:06 +02001850 if (IS_ERR(vport)) {
1851 if (err == -EAGAIN)
1852 goto restart;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001853 goto exit_unlock_free;
Thomas Graf62b9c8d2014-10-22 17:29:06 +02001854 }
Jesse Grossccb13522011-10-25 19:26:31 -07001855
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001856 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1857 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1858 BUG_ON(err < 0);
1859 ovs_unlock();
Thomas Grafed6611852013-03-29 14:46:50 +01001860
Johannes Berg2a94fe42013-11-19 15:19:39 +01001861 ovs_notify(&dp_vport_genl_family, reply, info);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001862 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001863
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001864exit_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001865 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001866 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001867 return err;
1868}
1869
1870static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
1871{
1872 struct nlattr **a = info->attrs;
1873 struct sk_buff *reply;
1874 struct vport *vport;
1875 int err;
1876
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001877 reply = ovs_vport_cmd_alloc_info();
1878 if (!reply)
1879 return -ENOMEM;
1880
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001881 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001882 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
Jesse Grossccb13522011-10-25 19:26:31 -07001883 err = PTR_ERR(vport);
1884 if (IS_ERR(vport))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001885 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001886
Jesse Grossccb13522011-10-25 19:26:31 -07001887 if (a[OVS_VPORT_ATTR_TYPE] &&
Jesse Grossf44f3402013-05-13 08:15:26 -07001888 nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
Jesse Grossccb13522011-10-25 19:26:31 -07001889 err = -EINVAL;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001890 goto exit_unlock_free;
Jesse Grossa9341512013-03-26 15:48:38 -07001891 }
1892
Jesse Grossf44f3402013-05-13 08:15:26 -07001893 if (a[OVS_VPORT_ATTR_OPTIONS]) {
Jesse Grossccb13522011-10-25 19:26:31 -07001894 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
Jesse Grossf44f3402013-05-13 08:15:26 -07001895 if (err)
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001896 goto exit_unlock_free;
Jesse Grossf44f3402013-05-13 08:15:26 -07001897 }
Jesse Grossa9341512013-03-26 15:48:38 -07001898
Alex Wang5cd667b2014-07-17 15:14:13 -07001899
1900 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
1901 struct nlattr *ids = a[OVS_VPORT_ATTR_UPCALL_PID];
1902
1903 err = ovs_vport_set_upcall_portids(vport, ids);
1904 if (err)
1905 goto exit_unlock_free;
1906 }
Jesse Grossccb13522011-10-25 19:26:31 -07001907
Jesse Grossa9341512013-03-26 15:48:38 -07001908 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1909 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1910 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001911
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001912 ovs_unlock();
Johannes Berg2a94fe42013-11-19 15:19:39 +01001913 ovs_notify(&dp_vport_genl_family, reply, info);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001914 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001915
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001916exit_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001917 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001918 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001919 return err;
1920}
1921
1922static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
1923{
1924 struct nlattr **a = info->attrs;
1925 struct sk_buff *reply;
1926 struct vport *vport;
1927 int err;
1928
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001929 reply = ovs_vport_cmd_alloc_info();
1930 if (!reply)
1931 return -ENOMEM;
1932
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001933 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001934 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
Jesse Grossccb13522011-10-25 19:26:31 -07001935 err = PTR_ERR(vport);
1936 if (IS_ERR(vport))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001937 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001938
1939 if (vport->port_no == OVSP_LOCAL) {
1940 err = -EINVAL;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001941 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001942 }
1943
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001944 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1945 info->snd_seq, 0, OVS_VPORT_CMD_DEL);
1946 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001947 ovs_dp_detach_port(vport);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001948 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001949
Johannes Berg2a94fe42013-11-19 15:19:39 +01001950 ovs_notify(&dp_vport_genl_family, reply, info);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001951 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001952
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001953exit_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001954 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001955 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001956 return err;
1957}
1958
1959static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
1960{
1961 struct nlattr **a = info->attrs;
1962 struct ovs_header *ovs_header = info->userhdr;
1963 struct sk_buff *reply;
1964 struct vport *vport;
1965 int err;
1966
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001967 reply = ovs_vport_cmd_alloc_info();
1968 if (!reply)
1969 return -ENOMEM;
1970
Jesse Grossccb13522011-10-25 19:26:31 -07001971 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001972 vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
Jesse Grossccb13522011-10-25 19:26:31 -07001973 err = PTR_ERR(vport);
1974 if (IS_ERR(vport))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001975 goto exit_unlock_free;
1976 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1977 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1978 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001979 rcu_read_unlock();
1980
1981 return genlmsg_reply(reply, info);
1982
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001983exit_unlock_free:
Jesse Grossccb13522011-10-25 19:26:31 -07001984 rcu_read_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001985 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001986 return err;
1987}
1988
1989static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1990{
1991 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1992 struct datapath *dp;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001993 int bucket = cb->args[0], skip = cb->args[1];
1994 int i, j = 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001995
Jesse Grossccb13522011-10-25 19:26:31 -07001996 rcu_read_lock();
Andy Zhoucc3a5ae2014-09-08 13:14:22 -07001997 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
Jarno Rajahalme42ee19e2014-02-15 17:42:29 -08001998 if (!dp) {
1999 rcu_read_unlock();
2000 return -ENODEV;
2001 }
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07002002 for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
Jesse Grossccb13522011-10-25 19:26:31 -07002003 struct vport *vport;
2004
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07002005 j = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -08002006 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07002007 if (j >= skip &&
2008 ovs_vport_cmd_fill_info(vport, skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00002009 NETLINK_CB(cb->skb).portid,
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07002010 cb->nlh->nlmsg_seq,
2011 NLM_F_MULTI,
2012 OVS_VPORT_CMD_NEW) < 0)
2013 goto out;
Jesse Grossccb13522011-10-25 19:26:31 -07002014
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07002015 j++;
2016 }
2017 skip = 0;
Jesse Grossccb13522011-10-25 19:26:31 -07002018 }
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07002019out:
Jesse Grossccb13522011-10-25 19:26:31 -07002020 rcu_read_unlock();
2021
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07002022 cb->args[0] = i;
2023 cb->args[1] = j;
Jesse Grossccb13522011-10-25 19:26:31 -07002024
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07002025 return skb->len;
Jesse Grossccb13522011-10-25 19:26:31 -07002026}
2027
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002028static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
2029 [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
2030 [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
2031 [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
2032 [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
2033 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
2034 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
2035};
2036
stephen hemminger48e48a72014-07-16 11:25:52 -07002037static const struct genl_ops dp_vport_genl_ops[] = {
Jesse Grossccb13522011-10-25 19:26:31 -07002038 { .cmd = OVS_VPORT_CMD_NEW,
2039 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2040 .policy = vport_policy,
2041 .doit = ovs_vport_cmd_new
2042 },
2043 { .cmd = OVS_VPORT_CMD_DEL,
2044 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2045 .policy = vport_policy,
2046 .doit = ovs_vport_cmd_del
2047 },
2048 { .cmd = OVS_VPORT_CMD_GET,
2049 .flags = 0, /* OK for unprivileged users. */
2050 .policy = vport_policy,
2051 .doit = ovs_vport_cmd_get,
2052 .dumpit = ovs_vport_cmd_dump
2053 },
2054 { .cmd = OVS_VPORT_CMD_SET,
2055 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2056 .policy = vport_policy,
2057 .doit = ovs_vport_cmd_set,
2058 },
2059};
2060
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002061struct genl_family dp_vport_genl_family = {
2062 .id = GENL_ID_GENERATE,
2063 .hdrsize = sizeof(struct ovs_header),
2064 .name = OVS_VPORT_FAMILY,
2065 .version = OVS_VPORT_VERSION,
2066 .maxattr = OVS_VPORT_ATTR_MAX,
2067 .netnsok = true,
2068 .parallel_ops = true,
2069 .ops = dp_vport_genl_ops,
2070 .n_ops = ARRAY_SIZE(dp_vport_genl_ops),
2071 .mcgrps = &ovs_dp_vport_multicast_group,
2072 .n_mcgrps = 1,
Jesse Grossccb13522011-10-25 19:26:31 -07002073};
2074
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002075static struct genl_family * const dp_genl_families[] = {
2076 &dp_datapath_genl_family,
2077 &dp_vport_genl_family,
2078 &dp_flow_genl_family,
2079 &dp_packet_genl_family,
Jesse Grossccb13522011-10-25 19:26:31 -07002080};
2081
2082static void dp_unregister_genl(int n_families)
2083{
2084 int i;
2085
2086 for (i = 0; i < n_families; i++)
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002087 genl_unregister_family(dp_genl_families[i]);
Jesse Grossccb13522011-10-25 19:26:31 -07002088}
2089
2090static int dp_register_genl(void)
2091{
Jesse Grossccb13522011-10-25 19:26:31 -07002092 int err;
2093 int i;
2094
Jesse Grossccb13522011-10-25 19:26:31 -07002095 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
Jesse Grossccb13522011-10-25 19:26:31 -07002096
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002097 err = genl_register_family(dp_genl_families[i]);
Jesse Grossccb13522011-10-25 19:26:31 -07002098 if (err)
2099 goto error;
Jesse Grossccb13522011-10-25 19:26:31 -07002100 }
2101
2102 return 0;
2103
2104error:
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002105 dp_unregister_genl(i);
Jesse Grossccb13522011-10-25 19:26:31 -07002106 return err;
2107}
2108
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002109static int __net_init ovs_init_net(struct net *net)
2110{
2111 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2112
2113 INIT_LIST_HEAD(&ovs_net->dps);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002114 INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002115 return 0;
2116}
2117
2118static void __net_exit ovs_exit_net(struct net *net)
2119{
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002120 struct datapath *dp, *dp_next;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002121 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002122
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002123 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002124 list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
2125 __dp_destroy(dp);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002126 ovs_unlock();
2127
2128 cancel_work_sync(&ovs_net->dp_notify_work);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002129}
2130
2131static struct pernet_operations ovs_net_ops = {
2132 .init = ovs_init_net,
2133 .exit = ovs_exit_net,
2134 .id = &ovs_net_id,
2135 .size = sizeof(struct ovs_net),
2136};
2137
Jesse Grossccb13522011-10-25 19:26:31 -07002138static int __init dp_init(void)
2139{
Jesse Grossccb13522011-10-25 19:26:31 -07002140 int err;
2141
YOSHIFUJI Hideaki / 吉藤英明3523b292013-01-09 07:19:55 +00002142 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
Jesse Grossccb13522011-10-25 19:26:31 -07002143
2144 pr_info("Open vSwitch switching datapath\n");
2145
Andy Zhou971427f32014-09-15 19:37:25 -07002146 err = action_fifos_init();
Jesse Grossccb13522011-10-25 19:26:31 -07002147 if (err)
2148 goto error;
2149
Andy Zhou971427f32014-09-15 19:37:25 -07002150 err = ovs_internal_dev_rtnl_link_register();
2151 if (err)
2152 goto error_action_fifos_exit;
2153
Jiri Pirko5b9e7e12014-06-26 09:58:26 +02002154 err = ovs_flow_init();
2155 if (err)
2156 goto error_unreg_rtnl_link;
2157
Jesse Grossccb13522011-10-25 19:26:31 -07002158 err = ovs_vport_init();
2159 if (err)
2160 goto error_flow_exit;
2161
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002162 err = register_pernet_device(&ovs_net_ops);
Jesse Grossccb13522011-10-25 19:26:31 -07002163 if (err)
2164 goto error_vport_exit;
2165
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002166 err = register_netdevice_notifier(&ovs_dp_device_notifier);
2167 if (err)
2168 goto error_netns_exit;
2169
Thomas Graf62b9c8d2014-10-22 17:29:06 +02002170 err = ovs_netdev_init();
2171 if (err)
2172 goto error_unreg_notifier;
2173
Jesse Grossccb13522011-10-25 19:26:31 -07002174 err = dp_register_genl();
2175 if (err < 0)
Thomas Graf62b9c8d2014-10-22 17:29:06 +02002176 goto error_unreg_netdev;
Jesse Grossccb13522011-10-25 19:26:31 -07002177
Jesse Grossccb13522011-10-25 19:26:31 -07002178 return 0;
2179
Thomas Graf62b9c8d2014-10-22 17:29:06 +02002180error_unreg_netdev:
2181 ovs_netdev_exit();
Jesse Grossccb13522011-10-25 19:26:31 -07002182error_unreg_notifier:
2183 unregister_netdevice_notifier(&ovs_dp_device_notifier);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002184error_netns_exit:
2185 unregister_pernet_device(&ovs_net_ops);
Jesse Grossccb13522011-10-25 19:26:31 -07002186error_vport_exit:
2187 ovs_vport_exit();
2188error_flow_exit:
2189 ovs_flow_exit();
Jiri Pirko5b9e7e12014-06-26 09:58:26 +02002190error_unreg_rtnl_link:
2191 ovs_internal_dev_rtnl_link_unregister();
Andy Zhou971427f32014-09-15 19:37:25 -07002192error_action_fifos_exit:
2193 action_fifos_exit();
Jesse Grossccb13522011-10-25 19:26:31 -07002194error:
2195 return err;
2196}
2197
2198static void dp_cleanup(void)
2199{
Jesse Grossccb13522011-10-25 19:26:31 -07002200 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
Thomas Graf62b9c8d2014-10-22 17:29:06 +02002201 ovs_netdev_exit();
Jesse Grossccb13522011-10-25 19:26:31 -07002202 unregister_netdevice_notifier(&ovs_dp_device_notifier);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002203 unregister_pernet_device(&ovs_net_ops);
2204 rcu_barrier();
Jesse Grossccb13522011-10-25 19:26:31 -07002205 ovs_vport_exit();
2206 ovs_flow_exit();
Jiri Pirko5b9e7e12014-06-26 09:58:26 +02002207 ovs_internal_dev_rtnl_link_unregister();
Andy Zhou971427f32014-09-15 19:37:25 -07002208 action_fifos_exit();
Jesse Grossccb13522011-10-25 19:26:31 -07002209}
2210
2211module_init(dp_init);
2212module_exit(dp_cleanup);
2213
2214MODULE_DESCRIPTION("Open vSwitch switching datapath");
2215MODULE_LICENSE("GPL");