blob: 332b5a0317392002a18b86dadfd4671e18fb6896 [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 Shelar9ba559d2014-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 Grafed661182013-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 Grafed661182013-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 Shelar9ba559d2014-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 Pirko59682502014-11-19 14:04:59 +0100428 nskb = __vlan_hwaccel_push_inside(nskb);
Dan Carpenter8aa51d62012-05-13 08:44:18 +0000429 if (!nskb)
Jesse Grossccb13522011-10-25 19:26:31 -0700430 return -ENOMEM;
431
Jesse Grossccb13522011-10-25 19:26:31 -0700432 skb = nskb;
433 }
434
435 if (nla_attr_size(skb->len) > USHRT_MAX) {
436 err = -EFBIG;
437 goto out;
438 }
439
Thomas Grafbda56f12013-12-13 15:22:21 +0100440 /* Complete checksum if needed */
441 if (skb->ip_summed == CHECKSUM_PARTIAL &&
442 (err = skb_checksum_help(skb)))
443 goto out;
444
445 /* Older versions of OVS user space enforce alignment of the last
446 * Netlink attribute to NLA_ALIGNTO which would require extensive
447 * padding logic. Only perform zerocopy if padding is not required.
448 */
449 if (dp->user_features & OVS_DP_F_UNALIGNED)
450 hlen = skb_zerocopy_headlen(skb);
451 else
452 hlen = skb->len;
453
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800454 len = upcall_msg_size(upcall_info, hlen);
Thomas Graf795449d2013-11-30 13:21:32 +0100455 user_skb = genlmsg_new_unicast(len, &info, GFP_ATOMIC);
Jesse Grossccb13522011-10-25 19:26:31 -0700456 if (!user_skb) {
457 err = -ENOMEM;
458 goto out;
459 }
460
461 upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
462 0, upcall_info->cmd);
463 upcall->dp_ifindex = dp_ifindex;
464
465 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
Pravin B Shelare8eedb82014-11-06 06:57:27 -0800466 err = ovs_nla_put_flow(key, key, user_skb);
Andy Zhouf53e3832014-07-17 15:17:44 -0700467 BUG_ON(err);
Jesse Grossccb13522011-10-25 19:26:31 -0700468 nla_nest_end(user_skb, nla);
469
470 if (upcall_info->userdata)
Ben Pfaff44901082013-02-15 17:29:22 -0800471 __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA,
472 nla_len(upcall_info->userdata),
473 nla_data(upcall_info->userdata));
Jesse Grossccb13522011-10-25 19:26:31 -0700474
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800475 if (upcall_info->egress_tun_info) {
476 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_EGRESS_TUN_KEY);
477 err = ovs_nla_put_egress_tunnel_key(user_skb,
478 upcall_info->egress_tun_info);
479 BUG_ON(err);
480 nla_nest_end(user_skb, nla);
481 }
482
Thomas Grafbda56f12013-12-13 15:22:21 +0100483 /* Only reserve room for attribute header, packet data is added
484 * in skb_zerocopy() */
485 if (!(nla = nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, 0))) {
486 err = -ENOBUFS;
487 goto out;
488 }
489 nla->nla_len = nla_attr_size(skb->len);
Jesse Grossccb13522011-10-25 19:26:31 -0700490
Zoltan Kiss36d5fe62014-03-26 22:37:45 +0000491 err = skb_zerocopy(user_skb, skb, skb->len, hlen);
492 if (err)
493 goto out;
Jesse Grossccb13522011-10-25 19:26:31 -0700494
Thomas Grafaea0bb42014-01-14 16:27:49 +0000495 /* Pad OVS_PACKET_ATTR_PACKET if linear copy was performed */
496 if (!(dp->user_features & OVS_DP_F_UNALIGNED)) {
497 size_t plen = NLA_ALIGN(user_skb->len) - user_skb->len;
498
499 if (plen > 0)
500 memset(skb_put(user_skb, plen), 0, plen);
501 }
502
Thomas Grafbda56f12013-12-13 15:22:21 +0100503 ((struct nlmsghdr *) user_skb->data)->nlmsg_len = user_skb->len;
504
Thomas Graf8055a892013-12-13 15:22:20 +0100505 err = genlmsg_unicast(ovs_dp_get_net(dp), user_skb, upcall_info->portid);
Li RongQing4ee45ea2014-09-02 20:52:28 +0800506 user_skb = NULL;
Jesse Grossccb13522011-10-25 19:26:31 -0700507out:
Zoltan Kiss36d5fe62014-03-26 22:37:45 +0000508 if (err)
509 skb_tx_error(skb);
Li RongQing4ee45ea2014-09-02 20:52:28 +0800510 kfree_skb(user_skb);
Jesse Grossccb13522011-10-25 19:26:31 -0700511 kfree_skb(nskb);
512 return err;
513}
514
Jesse Grossccb13522011-10-25 19:26:31 -0700515static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
516{
517 struct ovs_header *ovs_header = info->userhdr;
518 struct nlattr **a = info->attrs;
519 struct sw_flow_actions *acts;
520 struct sk_buff *packet;
521 struct sw_flow *flow;
Lorand Jakabd98612b2014-10-06 05:45:32 -0700522 struct sw_flow_actions *sf_acts;
Jesse Grossccb13522011-10-25 19:26:31 -0700523 struct datapath *dp;
524 struct ethhdr *eth;
Pravin B Shelar83c8df22014-09-15 19:20:31 -0700525 struct vport *input_vport;
Jesse Grossccb13522011-10-25 19:26:31 -0700526 int len;
527 int err;
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800528 bool log = !a[OVS_FLOW_ATTR_PROBE];
Jesse Grossccb13522011-10-25 19:26:31 -0700529
530 err = -EINVAL;
531 if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
Thomas Grafdded45fc2013-03-29 14:46:47 +0100532 !a[OVS_PACKET_ATTR_ACTIONS])
Jesse Grossccb13522011-10-25 19:26:31 -0700533 goto err;
534
535 len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
536 packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
537 err = -ENOMEM;
538 if (!packet)
539 goto err;
540 skb_reserve(packet, NET_IP_ALIGN);
541
Thomas Graf32686a92013-03-29 14:46:48 +0100542 nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
Jesse Grossccb13522011-10-25 19:26:31 -0700543
544 skb_reset_mac_header(packet);
545 eth = eth_hdr(packet);
546
547 /* Normally, setting the skb 'protocol' field would be handled by a
548 * call to eth_type_trans(), but it assumes there's a sending
549 * device, which we may not have. */
Simon Hormane5c5d222013-03-28 13:38:25 +0900550 if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN)
Jesse Grossccb13522011-10-25 19:26:31 -0700551 packet->protocol = eth->h_proto;
552 else
553 packet->protocol = htons(ETH_P_802_2);
554
555 /* Build an sw_flow for sending this packet. */
Jarno Rajahalme23dabf82014-03-27 12:35:23 -0700556 flow = ovs_flow_alloc();
Jesse Grossccb13522011-10-25 19:26:31 -0700557 err = PTR_ERR(flow);
558 if (IS_ERR(flow))
559 goto err_kfree_skb;
560
Pravin B Shelar83c8df22014-09-15 19:20:31 -0700561 err = ovs_flow_key_extract_userspace(a[OVS_PACKET_ATTR_KEY], packet,
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800562 &flow->key, log);
Jesse Grossccb13522011-10-25 19:26:31 -0700563 if (err)
564 goto err_flow_free;
565
Pravin B Shelare6445712013-10-03 18:16:47 -0700566 err = ovs_nla_copy_actions(a[OVS_PACKET_ATTR_ACTIONS],
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800567 &flow->key, &acts, log);
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700568 if (err)
569 goto err_flow_free;
Jesse Grossccb13522011-10-25 19:26:31 -0700570
Jesse Grossf5796682014-10-03 15:35:33 -0700571 rcu_assign_pointer(flow->sf_acts, acts);
Jesse Grossf5796682014-10-03 15:35:33 -0700572 OVS_CB(packet)->egress_tun_info = NULL;
Jesse Grossccb13522011-10-25 19:26:31 -0700573 packet->priority = flow->key.phy.priority;
Ansis Atteka39c7caeb2012-11-26 11:24:11 -0800574 packet->mark = flow->key.phy.skb_mark;
Jesse Grossccb13522011-10-25 19:26:31 -0700575
576 rcu_read_lock();
Andy Zhoucc3a5ae2014-09-08 13:14:22 -0700577 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -0700578 err = -ENODEV;
579 if (!dp)
580 goto err_unlock;
581
Pravin B Shelar83c8df22014-09-15 19:20:31 -0700582 input_vport = ovs_vport_rcu(dp, flow->key.phy.in_port);
583 if (!input_vport)
584 input_vport = ovs_vport_rcu(dp, OVSP_LOCAL);
585
586 if (!input_vport)
587 goto err_unlock;
588
589 OVS_CB(packet)->input_vport = input_vport;
Lorand Jakabd98612b2014-10-06 05:45:32 -0700590 sf_acts = rcu_dereference(flow->sf_acts);
Pravin B Shelar83c8df22014-09-15 19:20:31 -0700591
Jesse Grossccb13522011-10-25 19:26:31 -0700592 local_bh_disable();
Lorand Jakabd98612b2014-10-06 05:45:32 -0700593 err = ovs_execute_actions(dp, packet, sf_acts, &flow->key);
Jesse Grossccb13522011-10-25 19:26:31 -0700594 local_bh_enable();
595 rcu_read_unlock();
596
Andy Zhou03f0d912013-08-07 20:01:00 -0700597 ovs_flow_free(flow, false);
Jesse Grossccb13522011-10-25 19:26:31 -0700598 return err;
599
600err_unlock:
601 rcu_read_unlock();
602err_flow_free:
Andy Zhou03f0d912013-08-07 20:01:00 -0700603 ovs_flow_free(flow, false);
Jesse Grossccb13522011-10-25 19:26:31 -0700604err_kfree_skb:
605 kfree_skb(packet);
606err:
607 return err;
608}
609
610static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
Thomas Grafdded45fc2013-03-29 14:46:47 +0100611 [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
Jesse Grossccb13522011-10-25 19:26:31 -0700612 [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
613 [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
614};
615
Johannes Berg4534de82013-11-14 17:14:46 +0100616static const struct genl_ops dp_packet_genl_ops[] = {
Jesse Grossccb13522011-10-25 19:26:31 -0700617 { .cmd = OVS_PACKET_CMD_EXECUTE,
618 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
619 .policy = packet_policy,
620 .doit = ovs_packet_cmd_execute
621 }
622};
623
Pravin B Shelar0c200ef2014-05-06 16:44:50 -0700624static struct genl_family dp_packet_genl_family = {
625 .id = GENL_ID_GENERATE,
626 .hdrsize = sizeof(struct ovs_header),
627 .name = OVS_PACKET_FAMILY,
628 .version = OVS_PACKET_VERSION,
629 .maxattr = OVS_PACKET_ATTR_MAX,
630 .netnsok = true,
631 .parallel_ops = true,
632 .ops = dp_packet_genl_ops,
633 .n_ops = ARRAY_SIZE(dp_packet_genl_ops),
634};
635
Thomas Graf12eb18f2014-11-06 06:58:52 -0800636static void get_dp_stats(const struct datapath *dp, struct ovs_dp_stats *stats,
Andy Zhou1bd71162013-10-22 10:42:46 -0700637 struct ovs_dp_megaflow_stats *mega_stats)
Jesse Grossccb13522011-10-25 19:26:31 -0700638{
639 int i;
Jesse Grossccb13522011-10-25 19:26:31 -0700640
Andy Zhou1bd71162013-10-22 10:42:46 -0700641 memset(mega_stats, 0, sizeof(*mega_stats));
642
Pravin B Shelarb637e492013-10-04 00:14:23 -0700643 stats->n_flows = ovs_flow_tbl_count(&dp->table);
Andy Zhou1bd71162013-10-22 10:42:46 -0700644 mega_stats->n_masks = ovs_flow_tbl_num_masks(&dp->table);
Jesse Grossccb13522011-10-25 19:26:31 -0700645
646 stats->n_hit = stats->n_missed = stats->n_lost = 0;
Andy Zhou1bd71162013-10-22 10:42:46 -0700647
Jesse Grossccb13522011-10-25 19:26:31 -0700648 for_each_possible_cpu(i) {
649 const struct dp_stats_percpu *percpu_stats;
650 struct dp_stats_percpu local_stats;
651 unsigned int start;
652
653 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
654
655 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700656 start = u64_stats_fetch_begin_irq(&percpu_stats->syncp);
Jesse Grossccb13522011-10-25 19:26:31 -0700657 local_stats = *percpu_stats;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700658 } while (u64_stats_fetch_retry_irq(&percpu_stats->syncp, start));
Jesse Grossccb13522011-10-25 19:26:31 -0700659
660 stats->n_hit += local_stats.n_hit;
661 stats->n_missed += local_stats.n_missed;
662 stats->n_lost += local_stats.n_lost;
Andy Zhou1bd71162013-10-22 10:42:46 -0700663 mega_stats->n_mask_hit += local_stats.n_mask_hit;
Jesse Grossccb13522011-10-25 19:26:31 -0700664 }
665}
666
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100667static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts)
668{
669 return NLMSG_ALIGN(sizeof(struct ovs_header))
Joe Stringer41af73e2014-10-18 16:14:14 -0700670 + nla_total_size(ovs_key_attr_size()) /* OVS_FLOW_ATTR_KEY */
671 + nla_total_size(ovs_key_attr_size()) /* OVS_FLOW_ATTR_MASK */
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100672 + nla_total_size(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
673 + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
674 + nla_total_size(8) /* OVS_FLOW_ATTR_USED */
675 + nla_total_size(acts->actions_len); /* OVS_FLOW_ATTR_ACTIONS */
676}
677
Jarno Rajahalmebb6f9a72014-05-05 11:32:17 -0700678/* Called with ovs_mutex or RCU read lock. */
Joe Stringerca7105f2014-09-08 13:09:37 -0700679static int ovs_flow_cmd_fill_match(const struct sw_flow *flow,
680 struct sk_buff *skb)
Jesse Grossccb13522011-10-25 19:26:31 -0700681{
Jesse Grossccb13522011-10-25 19:26:31 -0700682 struct nlattr *nla;
Jesse Grossccb13522011-10-25 19:26:31 -0700683 int err;
684
Andy Zhou03f0d912013-08-07 20:01:00 -0700685 /* Fill flow key. */
Jesse Grossccb13522011-10-25 19:26:31 -0700686 nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
687 if (!nla)
Joe Stringerca7105f2014-09-08 13:09:37 -0700688 return -EMSGSIZE;
Andy Zhou03f0d912013-08-07 20:01:00 -0700689
Pravin B Shelare6445712013-10-03 18:16:47 -0700690 err = ovs_nla_put_flow(&flow->unmasked_key, &flow->unmasked_key, skb);
Jesse Grossccb13522011-10-25 19:26:31 -0700691 if (err)
Joe Stringerca7105f2014-09-08 13:09:37 -0700692 return err;
693
Jesse Grossccb13522011-10-25 19:26:31 -0700694 nla_nest_end(skb, nla);
695
Joe Stringerca7105f2014-09-08 13:09:37 -0700696 /* Fill flow mask. */
Andy Zhou03f0d912013-08-07 20:01:00 -0700697 nla = nla_nest_start(skb, OVS_FLOW_ATTR_MASK);
698 if (!nla)
Joe Stringerca7105f2014-09-08 13:09:37 -0700699 return -EMSGSIZE;
Andy Zhou03f0d912013-08-07 20:01:00 -0700700
Pravin B Shelare6445712013-10-03 18:16:47 -0700701 err = ovs_nla_put_flow(&flow->key, &flow->mask->key, skb);
Andy Zhou03f0d912013-08-07 20:01:00 -0700702 if (err)
Joe Stringerca7105f2014-09-08 13:09:37 -0700703 return err;
Andy Zhou03f0d912013-08-07 20:01:00 -0700704
705 nla_nest_end(skb, nla);
Joe Stringerca7105f2014-09-08 13:09:37 -0700706 return 0;
707}
708
709/* Called with ovs_mutex or RCU read lock. */
710static int ovs_flow_cmd_fill_stats(const struct sw_flow *flow,
711 struct sk_buff *skb)
712{
713 struct ovs_flow_stats stats;
714 __be16 tcp_flags;
715 unsigned long used;
Andy Zhou03f0d912013-08-07 20:01:00 -0700716
Pravin B Shelare298e502013-10-29 17:22:21 -0700717 ovs_flow_stats_get(flow, &stats, &used, &tcp_flags);
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700718
David S. Miller028d6a62012-03-29 23:20:48 -0400719 if (used &&
720 nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
Joe Stringerca7105f2014-09-08 13:09:37 -0700721 return -EMSGSIZE;
Jesse Grossccb13522011-10-25 19:26:31 -0700722
David S. Miller028d6a62012-03-29 23:20:48 -0400723 if (stats.n_packets &&
Pravin B Shelare298e502013-10-29 17:22:21 -0700724 nla_put(skb, OVS_FLOW_ATTR_STATS, sizeof(struct ovs_flow_stats), &stats))
Joe Stringerca7105f2014-09-08 13:09:37 -0700725 return -EMSGSIZE;
Jesse Grossccb13522011-10-25 19:26:31 -0700726
Pravin B Shelare298e502013-10-29 17:22:21 -0700727 if ((u8)ntohs(tcp_flags) &&
728 nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, (u8)ntohs(tcp_flags)))
Joe Stringerca7105f2014-09-08 13:09:37 -0700729 return -EMSGSIZE;
730
731 return 0;
732}
733
734/* Called with ovs_mutex or RCU read lock. */
735static int ovs_flow_cmd_fill_actions(const struct sw_flow *flow,
736 struct sk_buff *skb, int skb_orig_len)
737{
738 struct nlattr *start;
739 int err;
Jesse Grossccb13522011-10-25 19:26:31 -0700740
741 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
742 * this is the first flow to be dumped into 'skb'. This is unusual for
743 * Netlink but individual action lists can be longer than
744 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
745 * The userspace caller can always fetch the actions separately if it
746 * really wants them. (Most userspace callers in fact don't care.)
747 *
748 * This can only fail for dump operations because the skb is always
749 * properly sized for single flows.
750 */
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700751 start = nla_nest_start(skb, OVS_FLOW_ATTR_ACTIONS);
752 if (start) {
Pravin B Shelard57170b2013-07-30 15:39:39 -0700753 const struct sw_flow_actions *sf_acts;
754
Jesse Gross663efa32013-12-03 10:58:53 -0800755 sf_acts = rcu_dereference_ovsl(flow->sf_acts);
Pravin B Shelare6445712013-10-03 18:16:47 -0700756 err = ovs_nla_put_actions(sf_acts->actions,
757 sf_acts->actions_len, skb);
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700758
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700759 if (!err)
760 nla_nest_end(skb, start);
761 else {
762 if (skb_orig_len)
Joe Stringerca7105f2014-09-08 13:09:37 -0700763 return err;
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700764
765 nla_nest_cancel(skb, start);
766 }
Joe Stringerca7105f2014-09-08 13:09:37 -0700767 } else if (skb_orig_len) {
768 return -EMSGSIZE;
769 }
770
771 return 0;
772}
773
774/* Called with ovs_mutex or RCU read lock. */
775static int ovs_flow_cmd_fill_info(const struct sw_flow *flow, int dp_ifindex,
776 struct sk_buff *skb, u32 portid,
777 u32 seq, u32 flags, u8 cmd)
778{
779 const int skb_orig_len = skb->len;
780 struct ovs_header *ovs_header;
781 int err;
782
783 ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family,
784 flags, cmd);
785 if (!ovs_header)
786 return -EMSGSIZE;
787
788 ovs_header->dp_ifindex = dp_ifindex;
789
790 err = ovs_flow_cmd_fill_match(flow, skb);
791 if (err)
792 goto error;
793
794 err = ovs_flow_cmd_fill_stats(flow, skb);
795 if (err)
796 goto error;
797
798 err = ovs_flow_cmd_fill_actions(flow, skb, skb_orig_len);
799 if (err)
800 goto error;
Jesse Grossccb13522011-10-25 19:26:31 -0700801
802 return genlmsg_end(skb, ovs_header);
803
Jesse Grossccb13522011-10-25 19:26:31 -0700804error:
805 genlmsg_cancel(skb, ovs_header);
806 return err;
807}
808
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700809/* May not be called with RCU read lock. */
810static struct sk_buff *ovs_flow_cmd_alloc_info(const struct sw_flow_actions *acts,
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700811 struct genl_info *info,
812 bool always)
Jesse Grossccb13522011-10-25 19:26:31 -0700813{
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700814 struct sk_buff *skb;
Jesse Grossccb13522011-10-25 19:26:31 -0700815
Samuel Gauthier9b67aa42014-09-18 10:31:04 +0200816 if (!always && !ovs_must_notify(&dp_flow_genl_family, info, 0))
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700817 return NULL;
818
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700819 skb = genlmsg_new_unicast(ovs_flow_cmd_msg_size(acts), info, GFP_KERNEL);
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700820 if (!skb)
821 return ERR_PTR(-ENOMEM);
822
823 return skb;
Jesse Grossccb13522011-10-25 19:26:31 -0700824}
825
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700826/* Called with ovs_mutex. */
827static struct sk_buff *ovs_flow_cmd_build_info(const struct sw_flow *flow,
828 int dp_ifindex,
829 struct genl_info *info, u8 cmd,
830 bool always)
Jesse Grossccb13522011-10-25 19:26:31 -0700831{
832 struct sk_buff *skb;
833 int retval;
834
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700835 skb = ovs_flow_cmd_alloc_info(ovsl_dereference(flow->sf_acts), info,
836 always);
Himangi Saraogid0e992a2014-07-27 12:37:46 +0530837 if (IS_ERR_OR_NULL(skb))
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700838 return skb;
Jesse Grossccb13522011-10-25 19:26:31 -0700839
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700840 retval = ovs_flow_cmd_fill_info(flow, dp_ifindex, skb,
841 info->snd_portid, info->snd_seq, 0,
842 cmd);
Jesse Grossccb13522011-10-25 19:26:31 -0700843 BUG_ON(retval < 0);
844 return skb;
845}
846
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700847static int ovs_flow_cmd_new(struct sk_buff *skb, struct genl_info *info)
Jesse Grossccb13522011-10-25 19:26:31 -0700848{
849 struct nlattr **a = info->attrs;
850 struct ovs_header *ovs_header = info->userhdr;
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700851 struct sw_flow *flow, *new_flow;
Andy Zhou03f0d912013-08-07 20:01:00 -0700852 struct sw_flow_mask mask;
Jesse Grossccb13522011-10-25 19:26:31 -0700853 struct sk_buff *reply;
854 struct datapath *dp;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700855 struct sw_flow_actions *acts;
856 struct sw_flow_match match;
857 int error;
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800858 bool log = !a[OVS_FLOW_ATTR_PROBE];
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700859
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700860 /* Must have key and actions. */
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700861 error = -EINVAL;
Jesse Gross426cda52014-10-06 05:08:38 -0700862 if (!a[OVS_FLOW_ATTR_KEY]) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800863 OVS_NLERR(log, "Flow key attr not present in new flow.");
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700864 goto error;
Jesse Gross426cda52014-10-06 05:08:38 -0700865 }
866 if (!a[OVS_FLOW_ATTR_ACTIONS]) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800867 OVS_NLERR(log, "Flow actions attr not present in new flow.");
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700868 goto error;
Jesse Gross426cda52014-10-06 05:08:38 -0700869 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700870
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700871 /* Most of the time we need to allocate a new flow, do it before
872 * locking.
873 */
874 new_flow = ovs_flow_alloc();
875 if (IS_ERR(new_flow)) {
876 error = PTR_ERR(new_flow);
877 goto error;
878 }
879
880 /* Extract key. */
881 ovs_match_init(&match, &new_flow->unmasked_key, &mask);
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800882 error = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY],
883 a[OVS_FLOW_ATTR_MASK], log);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700884 if (error)
885 goto err_kfree_flow;
886
887 ovs_flow_mask_key(&new_flow->key, &new_flow->unmasked_key, &mask);
888
889 /* Validate actions. */
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700890 error = ovs_nla_copy_actions(a[OVS_FLOW_ATTR_ACTIONS], &new_flow->key,
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800891 &acts, log);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700892 if (error) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800893 OVS_NLERR(log, "Flow actions may not be safe on all matching packets.");
Pravin B Shelar2fdb9572014-10-19 11:19:51 -0700894 goto err_kfree_flow;
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700895 }
896
897 reply = ovs_flow_cmd_alloc_info(acts, info, false);
898 if (IS_ERR(reply)) {
899 error = PTR_ERR(reply);
900 goto err_kfree_acts;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700901 }
902
903 ovs_lock();
904 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700905 if (unlikely(!dp)) {
906 error = -ENODEV;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700907 goto err_unlock_ovs;
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700908 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700909 /* Check if this is a duplicate flow */
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700910 flow = ovs_flow_tbl_lookup(&dp->table, &new_flow->unmasked_key);
911 if (likely(!flow)) {
912 rcu_assign_pointer(new_flow->sf_acts, acts);
913
914 /* Put flow in bucket. */
915 error = ovs_flow_tbl_insert(&dp->table, new_flow, &mask);
916 if (unlikely(error)) {
917 acts = NULL;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700918 goto err_unlock_ovs;
919 }
920
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700921 if (unlikely(reply)) {
922 error = ovs_flow_cmd_fill_info(new_flow,
923 ovs_header->dp_ifindex,
924 reply, info->snd_portid,
925 info->snd_seq, 0,
926 OVS_FLOW_CMD_NEW);
927 BUG_ON(error < 0);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700928 }
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700929 ovs_unlock();
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700930 } else {
931 struct sw_flow_actions *old_acts;
932
933 /* Bail out if we're not allowed to modify an existing flow.
934 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
935 * because Generic Netlink treats the latter as a dump
936 * request. We also accept NLM_F_EXCL in case that bug ever
937 * gets fixed.
938 */
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700939 if (unlikely(info->nlhdr->nlmsg_flags & (NLM_F_CREATE
940 | NLM_F_EXCL))) {
941 error = -EEXIST;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700942 goto err_unlock_ovs;
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700943 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700944 /* The unmasked key has to be the same for flow updates. */
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700945 if (unlikely(!ovs_flow_cmp_unmasked_key(flow, &match))) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800946 /* Look for any overlapping flow. */
Alex Wang4a46b242014-06-30 20:30:29 -0700947 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
948 if (!flow) {
949 error = -ENOENT;
950 goto err_unlock_ovs;
951 }
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700952 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700953 /* Update actions. */
954 old_acts = ovsl_dereference(flow->sf_acts);
955 rcu_assign_pointer(flow->sf_acts, acts);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700956
957 if (unlikely(reply)) {
958 error = ovs_flow_cmd_fill_info(flow,
959 ovs_header->dp_ifindex,
960 reply, info->snd_portid,
961 info->snd_seq, 0,
962 OVS_FLOW_CMD_NEW);
963 BUG_ON(error < 0);
964 }
965 ovs_unlock();
966
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700967 ovs_nla_free_flow_actions(old_acts);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700968 ovs_flow_free(new_flow, false);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700969 }
970
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700971 if (reply)
972 ovs_notify(&dp_flow_genl_family, reply, info);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700973 return 0;
974
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700975err_unlock_ovs:
976 ovs_unlock();
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700977 kfree_skb(reply);
978err_kfree_acts:
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700979 kfree(acts);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700980err_kfree_flow:
981 ovs_flow_free(new_flow, false);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700982error:
983 return error;
984}
985
Pravin B Shelar2fdb9572014-10-19 11:19:51 -0700986/* Factor out action copy to avoid "Wframe-larger-than=1024" warning. */
Jesse Gross6b205b22014-10-03 15:35:32 -0700987static struct sw_flow_actions *get_flow_actions(const struct nlattr *a,
988 const struct sw_flow_key *key,
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800989 const struct sw_flow_mask *mask,
990 bool log)
Jesse Gross6b205b22014-10-03 15:35:32 -0700991{
992 struct sw_flow_actions *acts;
993 struct sw_flow_key masked_key;
994 int error;
995
Jesse Gross6b205b22014-10-03 15:35:32 -0700996 ovs_flow_mask_key(&masked_key, key, mask);
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800997 error = ovs_nla_copy_actions(a, &masked_key, &acts, log);
Jesse Gross6b205b22014-10-03 15:35:32 -0700998 if (error) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800999 OVS_NLERR(log,
1000 "Actions may not be safe on all matching packets");
Jesse Gross6b205b22014-10-03 15:35:32 -07001001 return ERR_PTR(error);
1002 }
1003
1004 return acts;
1005}
1006
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001007static int ovs_flow_cmd_set(struct sk_buff *skb, struct genl_info *info)
1008{
1009 struct nlattr **a = info->attrs;
1010 struct ovs_header *ovs_header = info->userhdr;
Jesse Gross6b205b22014-10-03 15:35:32 -07001011 struct sw_flow_key key;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001012 struct sw_flow *flow;
1013 struct sw_flow_mask mask;
1014 struct sk_buff *reply = NULL;
1015 struct datapath *dp;
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001016 struct sw_flow_actions *old_acts = NULL, *acts = NULL;
Andy Zhou03f0d912013-08-07 20:01:00 -07001017 struct sw_flow_match match;
Jesse Grossccb13522011-10-25 19:26:31 -07001018 int error;
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001019 bool log = !a[OVS_FLOW_ATTR_PROBE];
Jesse Grossccb13522011-10-25 19:26:31 -07001020
1021 /* Extract key. */
1022 error = -EINVAL;
Jesse Gross426cda52014-10-06 05:08:38 -07001023 if (!a[OVS_FLOW_ATTR_KEY]) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001024 OVS_NLERR(log, "Flow key attribute not present in set flow.");
Jesse Grossccb13522011-10-25 19:26:31 -07001025 goto error;
Jesse Gross426cda52014-10-06 05:08:38 -07001026 }
Andy Zhou03f0d912013-08-07 20:01:00 -07001027
1028 ovs_match_init(&match, &key, &mask);
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001029 error = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY],
1030 a[OVS_FLOW_ATTR_MASK], log);
Jesse Grossccb13522011-10-25 19:26:31 -07001031 if (error)
1032 goto error;
1033
1034 /* Validate actions. */
1035 if (a[OVS_FLOW_ATTR_ACTIONS]) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001036 acts = get_flow_actions(a[OVS_FLOW_ATTR_ACTIONS], &key, &mask,
1037 log);
Jesse Gross6b205b22014-10-03 15:35:32 -07001038 if (IS_ERR(acts)) {
1039 error = PTR_ERR(acts);
Jesse Grossccb13522011-10-25 19:26:31 -07001040 goto error;
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001041 }
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001042
Pravin B Shelar2fdb9572014-10-19 11:19:51 -07001043 /* Can allocate before locking if have acts. */
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001044 reply = ovs_flow_cmd_alloc_info(acts, info, false);
1045 if (IS_ERR(reply)) {
1046 error = PTR_ERR(reply);
1047 goto err_kfree_acts;
Andy Zhou03f0d912013-08-07 20:01:00 -07001048 }
Jesse Grossccb13522011-10-25 19:26:31 -07001049 }
1050
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001051 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001052 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001053 if (unlikely(!dp)) {
1054 error = -ENODEV;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001055 goto err_unlock_ovs;
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001056 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001057 /* Check that the flow exists. */
Alex Wang4a46b242014-06-30 20:30:29 -07001058 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001059 if (unlikely(!flow)) {
1060 error = -ENOENT;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001061 goto err_unlock_ovs;
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001062 }
Alex Wang4a46b242014-06-30 20:30:29 -07001063
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001064 /* Update actions, if present. */
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001065 if (likely(acts)) {
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001066 old_acts = ovsl_dereference(flow->sf_acts);
Jesse Grossccb13522011-10-25 19:26:31 -07001067 rcu_assign_pointer(flow->sf_acts, acts);
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001068
1069 if (unlikely(reply)) {
1070 error = ovs_flow_cmd_fill_info(flow,
1071 ovs_header->dp_ifindex,
1072 reply, info->snd_portid,
1073 info->snd_seq, 0,
1074 OVS_FLOW_CMD_NEW);
1075 BUG_ON(error < 0);
1076 }
1077 } else {
1078 /* Could not alloc without acts before locking. */
1079 reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex,
1080 info, OVS_FLOW_CMD_NEW, false);
1081 if (unlikely(IS_ERR(reply))) {
1082 error = PTR_ERR(reply);
1083 goto err_unlock_ovs;
1084 }
Jesse Grossccb13522011-10-25 19:26:31 -07001085 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001086
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001087 /* Clear stats. */
1088 if (a[OVS_FLOW_ATTR_CLEAR])
1089 ovs_flow_stats_clear(flow);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001090 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001091
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001092 if (reply)
1093 ovs_notify(&dp_flow_genl_family, reply, info);
1094 if (old_acts)
1095 ovs_nla_free_flow_actions(old_acts);
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -07001096
Jesse Grossccb13522011-10-25 19:26:31 -07001097 return 0;
1098
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001099err_unlock_ovs:
1100 ovs_unlock();
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001101 kfree_skb(reply);
1102err_kfree_acts:
Pravin B Shelar74f84a52013-06-17 17:50:12 -07001103 kfree(acts);
Jesse Grossccb13522011-10-25 19:26:31 -07001104error:
1105 return error;
1106}
1107
1108static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1109{
1110 struct nlattr **a = info->attrs;
1111 struct ovs_header *ovs_header = info->userhdr;
1112 struct sw_flow_key key;
1113 struct sk_buff *reply;
1114 struct sw_flow *flow;
1115 struct datapath *dp;
Andy Zhou03f0d912013-08-07 20:01:00 -07001116 struct sw_flow_match match;
Jesse Grossccb13522011-10-25 19:26:31 -07001117 int err;
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001118 bool log = !a[OVS_FLOW_ATTR_PROBE];
Jesse Grossccb13522011-10-25 19:26:31 -07001119
Andy Zhou03f0d912013-08-07 20:01:00 -07001120 if (!a[OVS_FLOW_ATTR_KEY]) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001121 OVS_NLERR(log,
1122 "Flow get message rejected, Key attribute missing.");
Jesse Grossccb13522011-10-25 19:26:31 -07001123 return -EINVAL;
Andy Zhou03f0d912013-08-07 20:01:00 -07001124 }
1125
1126 ovs_match_init(&match, &key, NULL);
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001127 err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL, log);
Jesse Grossccb13522011-10-25 19:26:31 -07001128 if (err)
1129 return err;
1130
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001131 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001132 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001133 if (!dp) {
1134 err = -ENODEV;
1135 goto unlock;
1136 }
Jesse Grossccb13522011-10-25 19:26:31 -07001137
Alex Wang4a46b242014-06-30 20:30:29 -07001138 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1139 if (!flow) {
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001140 err = -ENOENT;
1141 goto unlock;
1142 }
Jesse Grossccb13522011-10-25 19:26:31 -07001143
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -07001144 reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex, info,
1145 OVS_FLOW_CMD_NEW, true);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001146 if (IS_ERR(reply)) {
1147 err = PTR_ERR(reply);
1148 goto unlock;
1149 }
Jesse Grossccb13522011-10-25 19:26:31 -07001150
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001151 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001152 return genlmsg_reply(reply, info);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001153unlock:
1154 ovs_unlock();
1155 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001156}
1157
1158static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1159{
1160 struct nlattr **a = info->attrs;
1161 struct ovs_header *ovs_header = info->userhdr;
1162 struct sw_flow_key key;
1163 struct sk_buff *reply;
1164 struct sw_flow *flow;
1165 struct datapath *dp;
Andy Zhou03f0d912013-08-07 20:01:00 -07001166 struct sw_flow_match match;
Jesse Grossccb13522011-10-25 19:26:31 -07001167 int err;
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001168 bool log = !a[OVS_FLOW_ATTR_PROBE];
Jesse Grossccb13522011-10-25 19:26:31 -07001169
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001170 if (likely(a[OVS_FLOW_ATTR_KEY])) {
1171 ovs_match_init(&match, &key, NULL);
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001172 err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL,
1173 log);
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001174 if (unlikely(err))
1175 return err;
1176 }
1177
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001178 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001179 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001180 if (unlikely(!dp)) {
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001181 err = -ENODEV;
1182 goto unlock;
1183 }
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001184
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001185 if (unlikely(!a[OVS_FLOW_ATTR_KEY])) {
Pravin B Shelarb637e492013-10-04 00:14:23 -07001186 err = ovs_flow_tbl_flush(&dp->table);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001187 goto unlock;
1188 }
Andy Zhou03f0d912013-08-07 20:01:00 -07001189
Alex Wang4a46b242014-06-30 20:30:29 -07001190 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1191 if (unlikely(!flow)) {
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001192 err = -ENOENT;
1193 goto unlock;
1194 }
Jesse Grossccb13522011-10-25 19:26:31 -07001195
Pravin B Shelarb637e492013-10-04 00:14:23 -07001196 ovs_flow_tbl_remove(&dp->table, flow);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001197 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001198
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001199 reply = ovs_flow_cmd_alloc_info((const struct sw_flow_actions __force *) flow->sf_acts,
1200 info, false);
1201 if (likely(reply)) {
1202 if (likely(!IS_ERR(reply))) {
1203 rcu_read_lock(); /*To keep RCU checker happy. */
1204 err = ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex,
1205 reply, info->snd_portid,
1206 info->snd_seq, 0,
1207 OVS_FLOW_CMD_DEL);
1208 rcu_read_unlock();
1209 BUG_ON(err < 0);
1210
1211 ovs_notify(&dp_flow_genl_family, reply, info);
1212 } else {
1213 netlink_set_err(sock_net(skb->sk)->genl_sock, 0, 0, PTR_ERR(reply));
1214 }
1215 }
1216
1217 ovs_flow_free(flow, true);
Jesse Grossccb13522011-10-25 19:26:31 -07001218 return 0;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001219unlock:
1220 ovs_unlock();
1221 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001222}
1223
1224static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1225{
1226 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
Pravin B Shelarb637e492013-10-04 00:14:23 -07001227 struct table_instance *ti;
Jesse Grossccb13522011-10-25 19:26:31 -07001228 struct datapath *dp;
Jesse Grossccb13522011-10-25 19:26:31 -07001229
Pravin B Shelard57170b2013-07-30 15:39:39 -07001230 rcu_read_lock();
Andy Zhoucc3a5ae2014-09-08 13:14:22 -07001231 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001232 if (!dp) {
Pravin B Shelard57170b2013-07-30 15:39:39 -07001233 rcu_read_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001234 return -ENODEV;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001235 }
Jesse Grossccb13522011-10-25 19:26:31 -07001236
Pravin B Shelarb637e492013-10-04 00:14:23 -07001237 ti = rcu_dereference(dp->table.ti);
Jesse Grossccb13522011-10-25 19:26:31 -07001238 for (;;) {
1239 struct sw_flow *flow;
1240 u32 bucket, obj;
1241
1242 bucket = cb->args[0];
1243 obj = cb->args[1];
Pravin B Shelarb637e492013-10-04 00:14:23 -07001244 flow = ovs_flow_tbl_dump_next(ti, &bucket, &obj);
Jesse Grossccb13522011-10-25 19:26:31 -07001245 if (!flow)
1246 break;
1247
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -07001248 if (ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex, skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001249 NETLINK_CB(cb->skb).portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001250 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1251 OVS_FLOW_CMD_NEW) < 0)
1252 break;
1253
1254 cb->args[0] = bucket;
1255 cb->args[1] = obj;
1256 }
Pravin B Shelard57170b2013-07-30 15:39:39 -07001257 rcu_read_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001258 return skb->len;
1259}
1260
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001261static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
1262 [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001263 [OVS_FLOW_ATTR_MASK] = { .type = NLA_NESTED },
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001264 [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
1265 [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001266 [OVS_FLOW_ATTR_PROBE] = { .type = NLA_FLAG },
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001267};
1268
stephen hemminger48e48a72014-07-16 11:25:52 -07001269static const struct genl_ops dp_flow_genl_ops[] = {
Jesse Grossccb13522011-10-25 19:26:31 -07001270 { .cmd = OVS_FLOW_CMD_NEW,
1271 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1272 .policy = flow_policy,
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001273 .doit = ovs_flow_cmd_new
Jesse Grossccb13522011-10-25 19:26:31 -07001274 },
1275 { .cmd = OVS_FLOW_CMD_DEL,
1276 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1277 .policy = flow_policy,
1278 .doit = ovs_flow_cmd_del
1279 },
1280 { .cmd = OVS_FLOW_CMD_GET,
1281 .flags = 0, /* OK for unprivileged users. */
1282 .policy = flow_policy,
1283 .doit = ovs_flow_cmd_get,
1284 .dumpit = ovs_flow_cmd_dump
1285 },
1286 { .cmd = OVS_FLOW_CMD_SET,
1287 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1288 .policy = flow_policy,
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001289 .doit = ovs_flow_cmd_set,
Jesse Grossccb13522011-10-25 19:26:31 -07001290 },
1291};
1292
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001293static struct genl_family dp_flow_genl_family = {
Jesse Grossccb13522011-10-25 19:26:31 -07001294 .id = GENL_ID_GENERATE,
1295 .hdrsize = sizeof(struct ovs_header),
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001296 .name = OVS_FLOW_FAMILY,
1297 .version = OVS_FLOW_VERSION,
1298 .maxattr = OVS_FLOW_ATTR_MAX,
Pravin B Shelar3a4e0d62013-04-23 07:48:48 +00001299 .netnsok = true,
1300 .parallel_ops = true,
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001301 .ops = dp_flow_genl_ops,
1302 .n_ops = ARRAY_SIZE(dp_flow_genl_ops),
1303 .mcgrps = &ovs_dp_flow_multicast_group,
1304 .n_mcgrps = 1,
Jesse Grossccb13522011-10-25 19:26:31 -07001305};
1306
Thomas Grafc3ff8cf2013-03-29 14:46:49 +01001307static size_t ovs_dp_cmd_msg_size(void)
1308{
1309 size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
1310
1311 msgsize += nla_total_size(IFNAMSIZ);
1312 msgsize += nla_total_size(sizeof(struct ovs_dp_stats));
Andy Zhou1bd71162013-10-22 10:42:46 -07001313 msgsize += nla_total_size(sizeof(struct ovs_dp_megaflow_stats));
Daniele Di Proietto45fb9c32014-01-23 10:47:35 -08001314 msgsize += nla_total_size(sizeof(u32)); /* OVS_DP_ATTR_USER_FEATURES */
Thomas Grafc3ff8cf2013-03-29 14:46:49 +01001315
1316 return msgsize;
1317}
1318
Pravin B Shelar8ec609d2014-11-11 15:55:16 -08001319/* Called with ovs_mutex. */
Jesse Grossccb13522011-10-25 19:26:31 -07001320static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001321 u32 portid, u32 seq, u32 flags, u8 cmd)
Jesse Grossccb13522011-10-25 19:26:31 -07001322{
1323 struct ovs_header *ovs_header;
1324 struct ovs_dp_stats dp_stats;
Andy Zhou1bd71162013-10-22 10:42:46 -07001325 struct ovs_dp_megaflow_stats dp_megaflow_stats;
Jesse Grossccb13522011-10-25 19:26:31 -07001326 int err;
1327
Eric W. Biederman15e47302012-09-07 20:12:54 +00001328 ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
Jesse Grossccb13522011-10-25 19:26:31 -07001329 flags, cmd);
1330 if (!ovs_header)
1331 goto error;
1332
1333 ovs_header->dp_ifindex = get_dpifindex(dp);
1334
Jesse Grossccb13522011-10-25 19:26:31 -07001335 err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
Jesse Grossccb13522011-10-25 19:26:31 -07001336 if (err)
1337 goto nla_put_failure;
1338
Andy Zhou1bd71162013-10-22 10:42:46 -07001339 get_dp_stats(dp, &dp_stats, &dp_megaflow_stats);
1340 if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats),
1341 &dp_stats))
1342 goto nla_put_failure;
1343
1344 if (nla_put(skb, OVS_DP_ATTR_MEGAFLOW_STATS,
1345 sizeof(struct ovs_dp_megaflow_stats),
1346 &dp_megaflow_stats))
David S. Miller028d6a62012-03-29 23:20:48 -04001347 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001348
Thomas Graf43d4be92013-12-13 15:22:18 +01001349 if (nla_put_u32(skb, OVS_DP_ATTR_USER_FEATURES, dp->user_features))
1350 goto nla_put_failure;
1351
Jesse Grossccb13522011-10-25 19:26:31 -07001352 return genlmsg_end(skb, ovs_header);
1353
1354nla_put_failure:
1355 genlmsg_cancel(skb, ovs_header);
1356error:
1357 return -EMSGSIZE;
1358}
1359
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001360static struct sk_buff *ovs_dp_cmd_alloc_info(struct genl_info *info)
Jesse Grossccb13522011-10-25 19:26:31 -07001361{
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001362 return genlmsg_new_unicast(ovs_dp_cmd_msg_size(), info, GFP_KERNEL);
Jesse Grossccb13522011-10-25 19:26:31 -07001363}
1364
Jarno Rajahalmebb6f9a72014-05-05 11:32:17 -07001365/* Called with rcu_read_lock or ovs_mutex. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001366static struct datapath *lookup_datapath(struct net *net,
Thomas Graf12eb18f2014-11-06 06:58:52 -08001367 const struct ovs_header *ovs_header,
Jesse Grossccb13522011-10-25 19:26:31 -07001368 struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1369{
1370 struct datapath *dp;
1371
1372 if (!a[OVS_DP_ATTR_NAME])
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001373 dp = get_dp(net, ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001374 else {
1375 struct vport *vport;
1376
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001377 vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
Jesse Grossccb13522011-10-25 19:26:31 -07001378 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
Jesse Grossccb13522011-10-25 19:26:31 -07001379 }
1380 return dp ? dp : ERR_PTR(-ENODEV);
1381}
1382
Thomas Graf44da5ae2013-12-13 15:22:19 +01001383static void ovs_dp_reset_user_features(struct sk_buff *skb, struct genl_info *info)
1384{
1385 struct datapath *dp;
1386
1387 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
Jiri Pirko3c7eacf2014-02-14 11:42:36 +01001388 if (IS_ERR(dp))
Thomas Graf44da5ae2013-12-13 15:22:19 +01001389 return;
1390
1391 WARN(dp->user_features, "Dropping previously announced user features\n");
1392 dp->user_features = 0;
1393}
1394
Thomas Graf12eb18f2014-11-06 06:58:52 -08001395static void ovs_dp_change(struct datapath *dp, struct nlattr *a[])
Thomas Graf43d4be92013-12-13 15:22:18 +01001396{
1397 if (a[OVS_DP_ATTR_USER_FEATURES])
1398 dp->user_features = nla_get_u32(a[OVS_DP_ATTR_USER_FEATURES]);
1399}
1400
Jesse Grossccb13522011-10-25 19:26:31 -07001401static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1402{
1403 struct nlattr **a = info->attrs;
1404 struct vport_parms parms;
1405 struct sk_buff *reply;
1406 struct datapath *dp;
1407 struct vport *vport;
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001408 struct ovs_net *ovs_net;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001409 int err, i;
Jesse Grossccb13522011-10-25 19:26:31 -07001410
1411 err = -EINVAL;
1412 if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1413 goto err;
1414
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001415 reply = ovs_dp_cmd_alloc_info(info);
1416 if (!reply)
1417 return -ENOMEM;
Jesse Grossccb13522011-10-25 19:26:31 -07001418
1419 err = -ENOMEM;
1420 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1421 if (dp == NULL)
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001422 goto err_free_reply;
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001423
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001424 ovs_dp_set_net(dp, hold_net(sock_net(skb->sk)));
Jesse Grossccb13522011-10-25 19:26:31 -07001425
1426 /* Allocate table. */
Pravin B Shelarb637e492013-10-04 00:14:23 -07001427 err = ovs_flow_tbl_init(&dp->table);
1428 if (err)
Jesse Grossccb13522011-10-25 19:26:31 -07001429 goto err_free_dp;
1430
WANG Cong1c213bd2014-02-13 11:46:28 -08001431 dp->stats_percpu = netdev_alloc_pcpu_stats(struct dp_stats_percpu);
Jesse Grossccb13522011-10-25 19:26:31 -07001432 if (!dp->stats_percpu) {
1433 err = -ENOMEM;
1434 goto err_destroy_table;
1435 }
1436
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001437 dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
Pravin B Shelare6445712013-10-03 18:16:47 -07001438 GFP_KERNEL);
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001439 if (!dp->ports) {
1440 err = -ENOMEM;
1441 goto err_destroy_percpu;
1442 }
1443
1444 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1445 INIT_HLIST_HEAD(&dp->ports[i]);
1446
Jesse Grossccb13522011-10-25 19:26:31 -07001447 /* Set up our datapath device. */
1448 parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1449 parms.type = OVS_VPORT_TYPE_INTERNAL;
1450 parms.options = NULL;
1451 parms.dp = dp;
1452 parms.port_no = OVSP_LOCAL;
Alex Wang5cd667b2014-07-17 15:14:13 -07001453 parms.upcall_portids = a[OVS_DP_ATTR_UPCALL_PID];
Jesse Grossccb13522011-10-25 19:26:31 -07001454
Thomas Graf43d4be92013-12-13 15:22:18 +01001455 ovs_dp_change(dp, a);
1456
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001457 /* So far only local changes have been made, now need the lock. */
1458 ovs_lock();
1459
Jesse Grossccb13522011-10-25 19:26:31 -07001460 vport = new_vport(&parms);
1461 if (IS_ERR(vport)) {
1462 err = PTR_ERR(vport);
1463 if (err == -EBUSY)
1464 err = -EEXIST;
1465
Thomas Graf44da5ae2013-12-13 15:22:19 +01001466 if (err == -EEXIST) {
1467 /* An outdated user space instance that does not understand
1468 * the concept of user_features has attempted to create a new
1469 * datapath and is likely to reuse it. Drop all user features.
1470 */
1471 if (info->genlhdr->version < OVS_DP_VER_FEATURES)
1472 ovs_dp_reset_user_features(skb, info);
1473 }
1474
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001475 goto err_destroy_ports_array;
Jesse Grossccb13522011-10-25 19:26:31 -07001476 }
1477
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001478 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1479 info->snd_seq, 0, OVS_DP_CMD_NEW);
1480 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001481
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001482 ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001483 list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001484
1485 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001486
Johannes Berg2a94fe42013-11-19 15:19:39 +01001487 ovs_notify(&dp_datapath_genl_family, reply, info);
Jesse Grossccb13522011-10-25 19:26:31 -07001488 return 0;
1489
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001490err_destroy_ports_array:
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001491 ovs_unlock();
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001492 kfree(dp->ports);
Jesse Grossccb13522011-10-25 19:26:31 -07001493err_destroy_percpu:
1494 free_percpu(dp->stats_percpu);
1495err_destroy_table:
Pravin B Shelar9b996e52014-05-06 18:41:20 -07001496 ovs_flow_tbl_destroy(&dp->table);
Jesse Grossccb13522011-10-25 19:26:31 -07001497err_free_dp:
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001498 release_net(ovs_dp_get_net(dp));
Jesse Grossccb13522011-10-25 19:26:31 -07001499 kfree(dp);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001500err_free_reply:
1501 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001502err:
1503 return err;
1504}
1505
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001506/* Called with ovs_mutex. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001507static void __dp_destroy(struct datapath *dp)
Jesse Grossccb13522011-10-25 19:26:31 -07001508{
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001509 int i;
Jesse Grossccb13522011-10-25 19:26:31 -07001510
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001511 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1512 struct vport *vport;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001513 struct hlist_node *n;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001514
Sasha Levinb67bfe02013-02-27 17:06:00 -08001515 hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001516 if (vport->port_no != OVSP_LOCAL)
1517 ovs_dp_detach_port(vport);
1518 }
Jesse Grossccb13522011-10-25 19:26:31 -07001519
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001520 list_del_rcu(&dp->list_node);
Jesse Grossccb13522011-10-25 19:26:31 -07001521
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001522 /* OVSP_LOCAL is datapath internal port. We need to make sure that
Andy Zhoue80857c2014-01-21 09:31:04 -08001523 * all ports in datapath are destroyed first before freeing datapath.
Jesse Grossccb13522011-10-25 19:26:31 -07001524 */
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001525 ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
Jesse Grossccb13522011-10-25 19:26:31 -07001526
Andy Zhoue80857c2014-01-21 09:31:04 -08001527 /* RCU destroy the flow table */
Jesse Grossccb13522011-10-25 19:26:31 -07001528 call_rcu(&dp->rcu, destroy_dp_rcu);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001529}
1530
1531static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1532{
1533 struct sk_buff *reply;
1534 struct datapath *dp;
1535 int err;
1536
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001537 reply = ovs_dp_cmd_alloc_info(info);
1538 if (!reply)
1539 return -ENOMEM;
1540
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001541 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001542 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1543 err = PTR_ERR(dp);
1544 if (IS_ERR(dp))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001545 goto err_unlock_free;
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001546
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001547 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1548 info->snd_seq, 0, OVS_DP_CMD_DEL);
1549 BUG_ON(err < 0);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001550
1551 __dp_destroy(dp);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001552 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001553
Johannes Berg2a94fe42013-11-19 15:19:39 +01001554 ovs_notify(&dp_datapath_genl_family, reply, info);
Jesse Grossccb13522011-10-25 19:26:31 -07001555
1556 return 0;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001557
1558err_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001559 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001560 kfree_skb(reply);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001561 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001562}
1563
1564static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1565{
1566 struct sk_buff *reply;
1567 struct datapath *dp;
1568 int err;
1569
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001570 reply = ovs_dp_cmd_alloc_info(info);
1571 if (!reply)
1572 return -ENOMEM;
1573
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001574 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001575 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001576 err = PTR_ERR(dp);
Jesse Grossccb13522011-10-25 19:26:31 -07001577 if (IS_ERR(dp))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001578 goto err_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001579
Thomas Graf43d4be92013-12-13 15:22:18 +01001580 ovs_dp_change(dp, info->attrs);
1581
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001582 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1583 info->snd_seq, 0, OVS_DP_CMD_NEW);
1584 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001585
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001586 ovs_unlock();
Johannes Berg2a94fe42013-11-19 15:19:39 +01001587 ovs_notify(&dp_datapath_genl_family, reply, info);
Jesse Grossccb13522011-10-25 19:26:31 -07001588
1589 return 0;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001590
1591err_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001592 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001593 kfree_skb(reply);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001594 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001595}
1596
1597static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1598{
1599 struct sk_buff *reply;
1600 struct datapath *dp;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001601 int err;
Jesse Grossccb13522011-10-25 19:26:31 -07001602
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001603 reply = ovs_dp_cmd_alloc_info(info);
1604 if (!reply)
1605 return -ENOMEM;
1606
Pravin B Shelar8ec609d2014-11-11 15:55:16 -08001607 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001608 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001609 if (IS_ERR(dp)) {
1610 err = PTR_ERR(dp);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001611 goto err_unlock_free;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001612 }
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001613 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1614 info->snd_seq, 0, OVS_DP_CMD_NEW);
1615 BUG_ON(err < 0);
Pravin B Shelar8ec609d2014-11-11 15:55:16 -08001616 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001617
Jesse Grossccb13522011-10-25 19:26:31 -07001618 return genlmsg_reply(reply, info);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001619
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001620err_unlock_free:
Pravin B Shelar8ec609d2014-11-11 15:55:16 -08001621 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001622 kfree_skb(reply);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001623 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001624}
1625
1626static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1627{
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001628 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
Jesse Grossccb13522011-10-25 19:26:31 -07001629 struct datapath *dp;
1630 int skip = cb->args[0];
1631 int i = 0;
1632
Pravin B Shelar8ec609d2014-11-11 15:55:16 -08001633 ovs_lock();
1634 list_for_each_entry(dp, &ovs_net->dps, list_node) {
Ben Pfaff77676fd2012-01-17 13:33:39 +00001635 if (i >= skip &&
Eric W. Biederman15e47302012-09-07 20:12:54 +00001636 ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001637 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1638 OVS_DP_CMD_NEW) < 0)
1639 break;
1640 i++;
1641 }
Pravin B Shelar8ec609d2014-11-11 15:55:16 -08001642 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001643
1644 cb->args[0] = i;
1645
1646 return skb->len;
1647}
1648
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001649static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1650 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1651 [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1652 [OVS_DP_ATTR_USER_FEATURES] = { .type = NLA_U32 },
1653};
1654
stephen hemminger48e48a72014-07-16 11:25:52 -07001655static const struct genl_ops dp_datapath_genl_ops[] = {
Jesse Grossccb13522011-10-25 19:26:31 -07001656 { .cmd = OVS_DP_CMD_NEW,
1657 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1658 .policy = datapath_policy,
1659 .doit = ovs_dp_cmd_new
1660 },
1661 { .cmd = OVS_DP_CMD_DEL,
1662 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1663 .policy = datapath_policy,
1664 .doit = ovs_dp_cmd_del
1665 },
1666 { .cmd = OVS_DP_CMD_GET,
1667 .flags = 0, /* OK for unprivileged users. */
1668 .policy = datapath_policy,
1669 .doit = ovs_dp_cmd_get,
1670 .dumpit = ovs_dp_cmd_dump
1671 },
1672 { .cmd = OVS_DP_CMD_SET,
1673 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1674 .policy = datapath_policy,
1675 .doit = ovs_dp_cmd_set,
1676 },
1677};
1678
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001679static struct genl_family dp_datapath_genl_family = {
Jesse Grossccb13522011-10-25 19:26:31 -07001680 .id = GENL_ID_GENERATE,
1681 .hdrsize = sizeof(struct ovs_header),
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001682 .name = OVS_DATAPATH_FAMILY,
1683 .version = OVS_DATAPATH_VERSION,
1684 .maxattr = OVS_DP_ATTR_MAX,
Pravin B Shelar3a4e0d62013-04-23 07:48:48 +00001685 .netnsok = true,
1686 .parallel_ops = true,
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001687 .ops = dp_datapath_genl_ops,
1688 .n_ops = ARRAY_SIZE(dp_datapath_genl_ops),
1689 .mcgrps = &ovs_dp_datapath_multicast_group,
1690 .n_mcgrps = 1,
Jesse Grossccb13522011-10-25 19:26:31 -07001691};
1692
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001693/* Called with ovs_mutex or RCU read lock. */
Jesse Grossccb13522011-10-25 19:26:31 -07001694static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001695 u32 portid, u32 seq, u32 flags, u8 cmd)
Jesse Grossccb13522011-10-25 19:26:31 -07001696{
1697 struct ovs_header *ovs_header;
1698 struct ovs_vport_stats vport_stats;
1699 int err;
1700
Eric W. Biederman15e47302012-09-07 20:12:54 +00001701 ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
Jesse Grossccb13522011-10-25 19:26:31 -07001702 flags, cmd);
1703 if (!ovs_header)
1704 return -EMSGSIZE;
1705
1706 ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1707
David S. Miller028d6a62012-03-29 23:20:48 -04001708 if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
1709 nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
Alex Wang5cd667b2014-07-17 15:14:13 -07001710 nla_put_string(skb, OVS_VPORT_ATTR_NAME,
1711 vport->ops->get_name(vport)))
David S. Miller028d6a62012-03-29 23:20:48 -04001712 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001713
1714 ovs_vport_get_stats(vport, &vport_stats);
David S. Miller028d6a62012-03-29 23:20:48 -04001715 if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
1716 &vport_stats))
1717 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001718
Alex Wang5cd667b2014-07-17 15:14:13 -07001719 if (ovs_vport_get_upcall_portids(vport, skb))
1720 goto nla_put_failure;
1721
Jesse Grossccb13522011-10-25 19:26:31 -07001722 err = ovs_vport_get_options(vport, skb);
1723 if (err == -EMSGSIZE)
1724 goto error;
1725
1726 return genlmsg_end(skb, ovs_header);
1727
1728nla_put_failure:
1729 err = -EMSGSIZE;
1730error:
1731 genlmsg_cancel(skb, ovs_header);
1732 return err;
1733}
1734
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001735static struct sk_buff *ovs_vport_cmd_alloc_info(void)
1736{
1737 return nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1738}
1739
1740/* Called with ovs_mutex, only via ovs_dp_notify_wq(). */
Eric W. Biederman15e47302012-09-07 20:12:54 +00001741struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001742 u32 seq, u8 cmd)
1743{
1744 struct sk_buff *skb;
1745 int retval;
1746
1747 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1748 if (!skb)
1749 return ERR_PTR(-ENOMEM);
1750
Eric W. Biederman15e47302012-09-07 20:12:54 +00001751 retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
Jesse Grossa9341512013-03-26 15:48:38 -07001752 BUG_ON(retval < 0);
1753
Jesse Grossccb13522011-10-25 19:26:31 -07001754 return skb;
1755}
1756
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001757/* Called with ovs_mutex or RCU read lock. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001758static struct vport *lookup_vport(struct net *net,
Thomas Graf12eb18f2014-11-06 06:58:52 -08001759 const struct ovs_header *ovs_header,
Jesse Grossccb13522011-10-25 19:26:31 -07001760 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1761{
1762 struct datapath *dp;
1763 struct vport *vport;
1764
1765 if (a[OVS_VPORT_ATTR_NAME]) {
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001766 vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
Jesse Grossccb13522011-10-25 19:26:31 -07001767 if (!vport)
1768 return ERR_PTR(-ENODEV);
Ben Pfaff651a68e2012-03-06 15:04:04 -08001769 if (ovs_header->dp_ifindex &&
1770 ovs_header->dp_ifindex != get_dpifindex(vport->dp))
1771 return ERR_PTR(-ENODEV);
Jesse Grossccb13522011-10-25 19:26:31 -07001772 return vport;
1773 } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
1774 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1775
1776 if (port_no >= DP_MAX_PORTS)
1777 return ERR_PTR(-EFBIG);
1778
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001779 dp = get_dp(net, ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001780 if (!dp)
1781 return ERR_PTR(-ENODEV);
1782
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001783 vport = ovs_vport_ovsl_rcu(dp, port_no);
Jesse Grossccb13522011-10-25 19:26:31 -07001784 if (!vport)
Jarno Rajahalme14408db2013-01-09 14:27:35 -08001785 return ERR_PTR(-ENODEV);
Jesse Grossccb13522011-10-25 19:26:31 -07001786 return vport;
1787 } else
1788 return ERR_PTR(-EINVAL);
1789}
1790
1791static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
1792{
1793 struct nlattr **a = info->attrs;
1794 struct ovs_header *ovs_header = info->userhdr;
1795 struct vport_parms parms;
1796 struct sk_buff *reply;
1797 struct vport *vport;
1798 struct datapath *dp;
1799 u32 port_no;
1800 int err;
1801
Jesse Grossccb13522011-10-25 19:26:31 -07001802 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
1803 !a[OVS_VPORT_ATTR_UPCALL_PID])
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001804 return -EINVAL;
1805
1806 port_no = a[OVS_VPORT_ATTR_PORT_NO]
1807 ? nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]) : 0;
1808 if (port_no >= DP_MAX_PORTS)
1809 return -EFBIG;
1810
1811 reply = ovs_vport_cmd_alloc_info();
1812 if (!reply)
1813 return -ENOMEM;
Jesse Grossccb13522011-10-25 19:26:31 -07001814
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001815 ovs_lock();
Thomas Graf62b9c8d2014-10-22 17:29:06 +02001816restart:
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001817 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001818 err = -ENODEV;
1819 if (!dp)
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001820 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001821
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001822 if (port_no) {
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001823 vport = ovs_vport_ovsl(dp, port_no);
Jesse Grossccb13522011-10-25 19:26:31 -07001824 err = -EBUSY;
1825 if (vport)
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001826 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001827 } else {
1828 for (port_no = 1; ; port_no++) {
1829 if (port_no >= DP_MAX_PORTS) {
1830 err = -EFBIG;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001831 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001832 }
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001833 vport = ovs_vport_ovsl(dp, port_no);
Jesse Grossccb13522011-10-25 19:26:31 -07001834 if (!vport)
1835 break;
1836 }
1837 }
1838
1839 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
1840 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1841 parms.options = a[OVS_VPORT_ATTR_OPTIONS];
1842 parms.dp = dp;
1843 parms.port_no = port_no;
Alex Wang5cd667b2014-07-17 15:14:13 -07001844 parms.upcall_portids = a[OVS_VPORT_ATTR_UPCALL_PID];
Jesse Grossccb13522011-10-25 19:26:31 -07001845
1846 vport = new_vport(&parms);
1847 err = PTR_ERR(vport);
Thomas Graf62b9c8d2014-10-22 17:29:06 +02001848 if (IS_ERR(vport)) {
1849 if (err == -EAGAIN)
1850 goto restart;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001851 goto exit_unlock_free;
Thomas Graf62b9c8d2014-10-22 17:29:06 +02001852 }
Jesse Grossccb13522011-10-25 19:26:31 -07001853
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001854 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1855 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1856 BUG_ON(err < 0);
1857 ovs_unlock();
Thomas Grafed661182013-03-29 14:46:50 +01001858
Johannes Berg2a94fe42013-11-19 15:19:39 +01001859 ovs_notify(&dp_vport_genl_family, reply, info);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001860 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001861
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001862exit_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001863 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001864 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001865 return err;
1866}
1867
1868static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
1869{
1870 struct nlattr **a = info->attrs;
1871 struct sk_buff *reply;
1872 struct vport *vport;
1873 int err;
1874
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001875 reply = ovs_vport_cmd_alloc_info();
1876 if (!reply)
1877 return -ENOMEM;
1878
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001879 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001880 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
Jesse Grossccb13522011-10-25 19:26:31 -07001881 err = PTR_ERR(vport);
1882 if (IS_ERR(vport))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001883 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001884
Jesse Grossccb13522011-10-25 19:26:31 -07001885 if (a[OVS_VPORT_ATTR_TYPE] &&
Jesse Grossf44f3402013-05-13 08:15:26 -07001886 nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
Jesse Grossccb13522011-10-25 19:26:31 -07001887 err = -EINVAL;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001888 goto exit_unlock_free;
Jesse Grossa9341512013-03-26 15:48:38 -07001889 }
1890
Jesse Grossf44f3402013-05-13 08:15:26 -07001891 if (a[OVS_VPORT_ATTR_OPTIONS]) {
Jesse Grossccb13522011-10-25 19:26:31 -07001892 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
Jesse Grossf44f3402013-05-13 08:15:26 -07001893 if (err)
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001894 goto exit_unlock_free;
Jesse Grossf44f3402013-05-13 08:15:26 -07001895 }
Jesse Grossa9341512013-03-26 15:48:38 -07001896
Alex Wang5cd667b2014-07-17 15:14:13 -07001897
1898 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
1899 struct nlattr *ids = a[OVS_VPORT_ATTR_UPCALL_PID];
1900
1901 err = ovs_vport_set_upcall_portids(vport, ids);
1902 if (err)
1903 goto exit_unlock_free;
1904 }
Jesse Grossccb13522011-10-25 19:26:31 -07001905
Jesse Grossa9341512013-03-26 15:48:38 -07001906 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1907 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1908 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001909
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001910 ovs_unlock();
Johannes Berg2a94fe42013-11-19 15:19:39 +01001911 ovs_notify(&dp_vport_genl_family, reply, info);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001912 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001913
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001914exit_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001915 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001916 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001917 return err;
1918}
1919
1920static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
1921{
1922 struct nlattr **a = info->attrs;
1923 struct sk_buff *reply;
1924 struct vport *vport;
1925 int err;
1926
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001927 reply = ovs_vport_cmd_alloc_info();
1928 if (!reply)
1929 return -ENOMEM;
1930
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001931 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001932 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
Jesse Grossccb13522011-10-25 19:26:31 -07001933 err = PTR_ERR(vport);
1934 if (IS_ERR(vport))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001935 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001936
1937 if (vport->port_no == OVSP_LOCAL) {
1938 err = -EINVAL;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001939 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001940 }
1941
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001942 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1943 info->snd_seq, 0, OVS_VPORT_CMD_DEL);
1944 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001945 ovs_dp_detach_port(vport);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001946 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001947
Johannes Berg2a94fe42013-11-19 15:19:39 +01001948 ovs_notify(&dp_vport_genl_family, reply, info);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001949 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001950
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001951exit_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001952 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001953 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001954 return err;
1955}
1956
1957static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
1958{
1959 struct nlattr **a = info->attrs;
1960 struct ovs_header *ovs_header = info->userhdr;
1961 struct sk_buff *reply;
1962 struct vport *vport;
1963 int err;
1964
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001965 reply = ovs_vport_cmd_alloc_info();
1966 if (!reply)
1967 return -ENOMEM;
1968
Jesse Grossccb13522011-10-25 19:26:31 -07001969 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001970 vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
Jesse Grossccb13522011-10-25 19:26:31 -07001971 err = PTR_ERR(vport);
1972 if (IS_ERR(vport))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001973 goto exit_unlock_free;
1974 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1975 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1976 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001977 rcu_read_unlock();
1978
1979 return genlmsg_reply(reply, info);
1980
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001981exit_unlock_free:
Jesse Grossccb13522011-10-25 19:26:31 -07001982 rcu_read_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001983 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001984 return err;
1985}
1986
1987static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1988{
1989 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1990 struct datapath *dp;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001991 int bucket = cb->args[0], skip = cb->args[1];
1992 int i, j = 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001993
Jesse Grossccb13522011-10-25 19:26:31 -07001994 rcu_read_lock();
Andy Zhoucc3a5ae2014-09-08 13:14:22 -07001995 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
Jarno Rajahalme42ee19e2014-02-15 17:42:29 -08001996 if (!dp) {
1997 rcu_read_unlock();
1998 return -ENODEV;
1999 }
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07002000 for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
Jesse Grossccb13522011-10-25 19:26:31 -07002001 struct vport *vport;
2002
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07002003 j = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -08002004 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07002005 if (j >= skip &&
2006 ovs_vport_cmd_fill_info(vport, skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00002007 NETLINK_CB(cb->skb).portid,
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07002008 cb->nlh->nlmsg_seq,
2009 NLM_F_MULTI,
2010 OVS_VPORT_CMD_NEW) < 0)
2011 goto out;
Jesse Grossccb13522011-10-25 19:26:31 -07002012
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07002013 j++;
2014 }
2015 skip = 0;
Jesse Grossccb13522011-10-25 19:26:31 -07002016 }
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07002017out:
Jesse Grossccb13522011-10-25 19:26:31 -07002018 rcu_read_unlock();
2019
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07002020 cb->args[0] = i;
2021 cb->args[1] = j;
Jesse Grossccb13522011-10-25 19:26:31 -07002022
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07002023 return skb->len;
Jesse Grossccb13522011-10-25 19:26:31 -07002024}
2025
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002026static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
2027 [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
2028 [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
2029 [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
2030 [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
2031 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
2032 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
2033};
2034
stephen hemminger48e48a72014-07-16 11:25:52 -07002035static const struct genl_ops dp_vport_genl_ops[] = {
Jesse Grossccb13522011-10-25 19:26:31 -07002036 { .cmd = OVS_VPORT_CMD_NEW,
2037 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2038 .policy = vport_policy,
2039 .doit = ovs_vport_cmd_new
2040 },
2041 { .cmd = OVS_VPORT_CMD_DEL,
2042 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2043 .policy = vport_policy,
2044 .doit = ovs_vport_cmd_del
2045 },
2046 { .cmd = OVS_VPORT_CMD_GET,
2047 .flags = 0, /* OK for unprivileged users. */
2048 .policy = vport_policy,
2049 .doit = ovs_vport_cmd_get,
2050 .dumpit = ovs_vport_cmd_dump
2051 },
2052 { .cmd = OVS_VPORT_CMD_SET,
2053 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2054 .policy = vport_policy,
2055 .doit = ovs_vport_cmd_set,
2056 },
2057};
2058
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002059struct genl_family dp_vport_genl_family = {
2060 .id = GENL_ID_GENERATE,
2061 .hdrsize = sizeof(struct ovs_header),
2062 .name = OVS_VPORT_FAMILY,
2063 .version = OVS_VPORT_VERSION,
2064 .maxattr = OVS_VPORT_ATTR_MAX,
2065 .netnsok = true,
2066 .parallel_ops = true,
2067 .ops = dp_vport_genl_ops,
2068 .n_ops = ARRAY_SIZE(dp_vport_genl_ops),
2069 .mcgrps = &ovs_dp_vport_multicast_group,
2070 .n_mcgrps = 1,
Jesse Grossccb13522011-10-25 19:26:31 -07002071};
2072
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002073static struct genl_family * const dp_genl_families[] = {
2074 &dp_datapath_genl_family,
2075 &dp_vport_genl_family,
2076 &dp_flow_genl_family,
2077 &dp_packet_genl_family,
Jesse Grossccb13522011-10-25 19:26:31 -07002078};
2079
2080static void dp_unregister_genl(int n_families)
2081{
2082 int i;
2083
2084 for (i = 0; i < n_families; i++)
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002085 genl_unregister_family(dp_genl_families[i]);
Jesse Grossccb13522011-10-25 19:26:31 -07002086}
2087
2088static int dp_register_genl(void)
2089{
Jesse Grossccb13522011-10-25 19:26:31 -07002090 int err;
2091 int i;
2092
Jesse Grossccb13522011-10-25 19:26:31 -07002093 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
Jesse Grossccb13522011-10-25 19:26:31 -07002094
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002095 err = genl_register_family(dp_genl_families[i]);
Jesse Grossccb13522011-10-25 19:26:31 -07002096 if (err)
2097 goto error;
Jesse Grossccb13522011-10-25 19:26:31 -07002098 }
2099
2100 return 0;
2101
2102error:
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002103 dp_unregister_genl(i);
Jesse Grossccb13522011-10-25 19:26:31 -07002104 return err;
2105}
2106
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002107static int __net_init ovs_init_net(struct net *net)
2108{
2109 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2110
2111 INIT_LIST_HEAD(&ovs_net->dps);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002112 INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002113 return 0;
2114}
2115
2116static void __net_exit ovs_exit_net(struct net *net)
2117{
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002118 struct datapath *dp, *dp_next;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002119 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002120
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002121 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002122 list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
2123 __dp_destroy(dp);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002124 ovs_unlock();
2125
2126 cancel_work_sync(&ovs_net->dp_notify_work);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002127}
2128
2129static struct pernet_operations ovs_net_ops = {
2130 .init = ovs_init_net,
2131 .exit = ovs_exit_net,
2132 .id = &ovs_net_id,
2133 .size = sizeof(struct ovs_net),
2134};
2135
Jesse Grossccb13522011-10-25 19:26:31 -07002136static int __init dp_init(void)
2137{
Jesse Grossccb13522011-10-25 19:26:31 -07002138 int err;
2139
YOSHIFUJI Hideaki / 吉藤英明3523b292013-01-09 07:19:55 +00002140 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
Jesse Grossccb13522011-10-25 19:26:31 -07002141
2142 pr_info("Open vSwitch switching datapath\n");
2143
Andy Zhou971427f32014-09-15 19:37:25 -07002144 err = action_fifos_init();
Jesse Grossccb13522011-10-25 19:26:31 -07002145 if (err)
2146 goto error;
2147
Andy Zhou971427f32014-09-15 19:37:25 -07002148 err = ovs_internal_dev_rtnl_link_register();
2149 if (err)
2150 goto error_action_fifos_exit;
2151
Jiri Pirko5b9e7e12014-06-26 09:58:26 +02002152 err = ovs_flow_init();
2153 if (err)
2154 goto error_unreg_rtnl_link;
2155
Jesse Grossccb13522011-10-25 19:26:31 -07002156 err = ovs_vport_init();
2157 if (err)
2158 goto error_flow_exit;
2159
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002160 err = register_pernet_device(&ovs_net_ops);
Jesse Grossccb13522011-10-25 19:26:31 -07002161 if (err)
2162 goto error_vport_exit;
2163
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002164 err = register_netdevice_notifier(&ovs_dp_device_notifier);
2165 if (err)
2166 goto error_netns_exit;
2167
Thomas Graf62b9c8d2014-10-22 17:29:06 +02002168 err = ovs_netdev_init();
2169 if (err)
2170 goto error_unreg_notifier;
2171
Jesse Grossccb13522011-10-25 19:26:31 -07002172 err = dp_register_genl();
2173 if (err < 0)
Thomas Graf62b9c8d2014-10-22 17:29:06 +02002174 goto error_unreg_netdev;
Jesse Grossccb13522011-10-25 19:26:31 -07002175
Jesse Grossccb13522011-10-25 19:26:31 -07002176 return 0;
2177
Thomas Graf62b9c8d2014-10-22 17:29:06 +02002178error_unreg_netdev:
2179 ovs_netdev_exit();
Jesse Grossccb13522011-10-25 19:26:31 -07002180error_unreg_notifier:
2181 unregister_netdevice_notifier(&ovs_dp_device_notifier);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002182error_netns_exit:
2183 unregister_pernet_device(&ovs_net_ops);
Jesse Grossccb13522011-10-25 19:26:31 -07002184error_vport_exit:
2185 ovs_vport_exit();
2186error_flow_exit:
2187 ovs_flow_exit();
Jiri Pirko5b9e7e12014-06-26 09:58:26 +02002188error_unreg_rtnl_link:
2189 ovs_internal_dev_rtnl_link_unregister();
Andy Zhou971427f32014-09-15 19:37:25 -07002190error_action_fifos_exit:
2191 action_fifos_exit();
Jesse Grossccb13522011-10-25 19:26:31 -07002192error:
2193 return err;
2194}
2195
2196static void dp_cleanup(void)
2197{
Jesse Grossccb13522011-10-25 19:26:31 -07002198 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
Thomas Graf62b9c8d2014-10-22 17:29:06 +02002199 ovs_netdev_exit();
Jesse Grossccb13522011-10-25 19:26:31 -07002200 unregister_netdevice_notifier(&ovs_dp_device_notifier);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002201 unregister_pernet_device(&ovs_net_ops);
2202 rcu_barrier();
Jesse Grossccb13522011-10-25 19:26:31 -07002203 ovs_vport_exit();
2204 ovs_flow_exit();
Jiri Pirko5b9e7e12014-06-26 09:58:26 +02002205 ovs_internal_dev_rtnl_link_unregister();
Andy Zhou971427f32014-09-15 19:37:25 -07002206 action_fifos_exit();
Jesse Grossccb13522011-10-25 19:26:31 -07002207}
2208
2209module_init(dp_init);
2210module_exit(dp_cleanup);
2211
2212MODULE_DESCRIPTION("Open vSwitch switching datapath");
2213MODULE_LICENSE("GPL");