blob: b3fe02a2339e4b5b307ecfd71f9374a27f3afcad [file] [log] [blame]
Jesse Grossccb13522011-10-25 19:26:31 -07001/*
Ben Pfaffad552002014-05-06 16:48:38 -07002 * Copyright (c) 2007-2014 Nicira, Inc.
Jesse Grossccb13522011-10-25 19:26:31 -07003 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of version 2 of the GNU General Public
6 * License as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 * 02110-1301, USA
17 */
18
19#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20
21#include <linux/init.h>
22#include <linux/module.h>
23#include <linux/if_arp.h>
24#include <linux/if_vlan.h>
25#include <linux/in.h>
26#include <linux/ip.h>
27#include <linux/jhash.h>
28#include <linux/delay.h>
29#include <linux/time.h>
30#include <linux/etherdevice.h>
31#include <linux/genetlink.h>
32#include <linux/kernel.h>
33#include <linux/kthread.h>
34#include <linux/mutex.h>
35#include <linux/percpu.h>
36#include <linux/rcupdate.h>
37#include <linux/tcp.h>
38#include <linux/udp.h>
Jesse Grossccb13522011-10-25 19:26:31 -070039#include <linux/ethtool.h>
40#include <linux/wait.h>
Jesse Grossccb13522011-10-25 19:26:31 -070041#include <asm/div64.h>
42#include <linux/highmem.h>
43#include <linux/netfilter_bridge.h>
44#include <linux/netfilter_ipv4.h>
45#include <linux/inetdevice.h>
46#include <linux/list.h>
47#include <linux/openvswitch.h>
48#include <linux/rculist.h>
49#include <linux/dmi.h>
Jesse Grossccb13522011-10-25 19:26:31 -070050#include <net/genetlink.h>
Pravin B Shelar46df7b82012-02-22 19:58:59 -080051#include <net/net_namespace.h>
52#include <net/netns/generic.h>
Jesse Grossccb13522011-10-25 19:26:31 -070053
54#include "datapath.h"
55#include "flow.h"
Andy Zhoue80857c2014-01-21 09:31:04 -080056#include "flow_table.h"
Pravin B Shelare6445712013-10-03 18:16:47 -070057#include "flow_netlink.h"
Jesse Grossccb13522011-10-25 19:26:31 -070058#include "vport-internal_dev.h"
Thomas Grafcff63a52013-04-29 13:06:41 +000059#include "vport-netdev.h"
Jesse Grossccb13522011-10-25 19:26:31 -070060
Pravin B Shelar8e4e1712013-04-15 13:23:03 -070061int ovs_net_id __read_mostly;
Pravin B Shelar9ba559d92014-11-06 06:44:27 -080062EXPORT_SYMBOL_GPL(ovs_net_id);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -070063
Pravin B Shelar0c200ef2014-05-06 16:44:50 -070064static struct genl_family dp_packet_genl_family;
65static struct genl_family dp_flow_genl_family;
66static struct genl_family dp_datapath_genl_family;
67
Joe Stringer74ed7ab2015-01-21 16:42:52 -080068static const struct nla_policy flow_policy[];
69
stephen hemminger48e48a72014-07-16 11:25:52 -070070static const struct genl_multicast_group ovs_dp_flow_multicast_group = {
71 .name = OVS_FLOW_MCGROUP,
Pravin B Shelar0c200ef2014-05-06 16:44:50 -070072};
73
stephen hemminger48e48a72014-07-16 11:25:52 -070074static const struct genl_multicast_group ovs_dp_datapath_multicast_group = {
75 .name = OVS_DATAPATH_MCGROUP,
Pravin B Shelar0c200ef2014-05-06 16:44:50 -070076};
77
stephen hemminger48e48a72014-07-16 11:25:52 -070078static const struct genl_multicast_group ovs_dp_vport_multicast_group = {
79 .name = OVS_VPORT_MCGROUP,
Pravin B Shelar0c200ef2014-05-06 16:44:50 -070080};
81
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -070082/* Check if need to build a reply message.
83 * OVS userspace sets the NLM_F_ECHO flag if it needs the reply. */
Samuel Gauthier9b67aa42014-09-18 10:31:04 +020084static bool ovs_must_notify(struct genl_family *family, struct genl_info *info,
85 unsigned int group)
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -070086{
87 return info->nlhdr->nlmsg_flags & NLM_F_ECHO ||
Johannes Bergf8403a22014-12-22 18:56:36 +010088 genl_has_listeners(family, genl_info_net(info), group);
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -070089}
90
Johannes Berg68eb5502013-11-19 15:19:38 +010091static void ovs_notify(struct genl_family *family,
Johannes Berg2a94fe42013-11-19 15:19:39 +010092 struct sk_buff *skb, struct genl_info *info)
Thomas Grafed6611852013-03-29 14:46:50 +010093{
Johannes Berg68eb5502013-11-19 15:19:38 +010094 genl_notify(family, skb, genl_info_net(info), info->snd_portid,
Johannes Berg2a94fe42013-11-19 15:19:39 +010095 0, info->nlhdr, GFP_KERNEL);
Thomas Grafed6611852013-03-29 14:46:50 +010096}
97
Pravin B Shelar46df7b82012-02-22 19:58:59 -080098/**
Jesse Grossccb13522011-10-25 19:26:31 -070099 * DOC: Locking:
100 *
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700101 * All writes e.g. Writes to device state (add/remove datapath, port, set
102 * operations on vports, etc.), Writes to other state (flow table
103 * modifications, set miscellaneous datapath parameters, etc.) are protected
104 * by ovs_lock.
Jesse Grossccb13522011-10-25 19:26:31 -0700105 *
106 * Reads are protected by RCU.
107 *
108 * There are a few special cases (mostly stats) that have their own
109 * synchronization but they nest under all of above and don't interact with
110 * each other.
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700111 *
112 * The RTNL lock nests inside ovs_mutex.
Jesse Grossccb13522011-10-25 19:26:31 -0700113 */
114
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700115static DEFINE_MUTEX(ovs_mutex);
116
117void ovs_lock(void)
118{
119 mutex_lock(&ovs_mutex);
120}
121
122void ovs_unlock(void)
123{
124 mutex_unlock(&ovs_mutex);
125}
126
127#ifdef CONFIG_LOCKDEP
128int lockdep_ovsl_is_held(void)
129{
130 if (debug_locks)
131 return lockdep_is_held(&ovs_mutex);
132 else
133 return 1;
134}
Pravin B Shelar9ba559d92014-11-06 06:44:27 -0800135EXPORT_SYMBOL_GPL(lockdep_ovsl_is_held);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700136#endif
137
Jesse Grossccb13522011-10-25 19:26:31 -0700138static struct vport *new_vport(const struct vport_parms *);
Thomas Graf8055a892013-12-13 15:22:20 +0100139static int queue_gso_packets(struct datapath *dp, struct sk_buff *,
Pravin B Shelare8eedb82014-11-06 06:57:27 -0800140 const struct sw_flow_key *,
Jesse Grossccb13522011-10-25 19:26:31 -0700141 const struct dp_upcall_info *);
Thomas Graf8055a892013-12-13 15:22:20 +0100142static int queue_userspace_packet(struct datapath *dp, struct sk_buff *,
Pravin B Shelare8eedb82014-11-06 06:57:27 -0800143 const struct sw_flow_key *,
Jesse Grossccb13522011-10-25 19:26:31 -0700144 const struct dp_upcall_info *);
145
Andy Zhoucc3a5ae2014-09-08 13:14:22 -0700146/* Must be called with rcu_read_lock. */
147static struct datapath *get_dp_rcu(struct net *net, int dp_ifindex)
Jesse Grossccb13522011-10-25 19:26:31 -0700148{
Andy Zhoucc3a5ae2014-09-08 13:14:22 -0700149 struct net_device *dev = dev_get_by_index_rcu(net, dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -0700150
Jesse Grossccb13522011-10-25 19:26:31 -0700151 if (dev) {
152 struct vport *vport = ovs_internal_dev_get_vport(dev);
153 if (vport)
Andy Zhoucc3a5ae2014-09-08 13:14:22 -0700154 return vport->dp;
Jesse Grossccb13522011-10-25 19:26:31 -0700155 }
Andy Zhoucc3a5ae2014-09-08 13:14:22 -0700156
157 return NULL;
158}
159
160/* The caller must hold either ovs_mutex or rcu_read_lock to keep the
161 * returned dp pointer valid.
162 */
163static inline struct datapath *get_dp(struct net *net, int dp_ifindex)
164{
165 struct datapath *dp;
166
167 WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_ovsl_is_held());
168 rcu_read_lock();
169 dp = get_dp_rcu(net, dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -0700170 rcu_read_unlock();
171
172 return dp;
173}
174
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700175/* Must be called with rcu_read_lock or ovs_mutex. */
Andy Zhou971427f32014-09-15 19:37:25 -0700176const char *ovs_dp_name(const struct datapath *dp)
Jesse Grossccb13522011-10-25 19:26:31 -0700177{
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700178 struct vport *vport = ovs_vport_ovsl_rcu(dp, OVSP_LOCAL);
Jesse Grossccb13522011-10-25 19:26:31 -0700179 return vport->ops->get_name(vport);
180}
181
Thomas Graf12eb18f2014-11-06 06:58:52 -0800182static int get_dpifindex(const struct datapath *dp)
Jesse Grossccb13522011-10-25 19:26:31 -0700183{
184 struct vport *local;
185 int ifindex;
186
187 rcu_read_lock();
188
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700189 local = ovs_vport_rcu(dp, OVSP_LOCAL);
Jesse Grossccb13522011-10-25 19:26:31 -0700190 if (local)
Thomas Grafcff63a52013-04-29 13:06:41 +0000191 ifindex = netdev_vport_priv(local)->dev->ifindex;
Jesse Grossccb13522011-10-25 19:26:31 -0700192 else
193 ifindex = 0;
194
195 rcu_read_unlock();
196
197 return ifindex;
198}
199
200static void destroy_dp_rcu(struct rcu_head *rcu)
201{
202 struct datapath *dp = container_of(rcu, struct datapath, rcu);
203
Pravin B Shelar9b996e52014-05-06 18:41:20 -0700204 ovs_flow_tbl_destroy(&dp->table);
Jesse Grossccb13522011-10-25 19:26:31 -0700205 free_percpu(dp->stats_percpu);
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;
340 int err;
341
Konstantin Khlebnikovabefd1b2016-01-08 15:21:46 +0300342 BUILD_BUG_ON(sizeof(*OVS_CB(skb)) > SKB_SGO_CB_OFFSET);
Thomas Graf09c5e602013-12-13 15:22:22 +0100343 segs = __skb_gso_segment(skb, NETIF_F_SG, false);
Pravin B Shelar92e5dfc2012-07-20 14:46:29 -0700344 if (IS_ERR(segs))
345 return PTR_ERR(segs);
Florian Westphal330966e2014-10-20 13:49:17 +0200346 if (segs == NULL)
347 return -EINVAL;
Jesse Grossccb13522011-10-25 19:26:31 -0700348
Pravin B Shelare8eedb82014-11-06 06:57:27 -0800349 if (gso_type & SKB_GSO_UDP) {
350 /* The initial flow key extracted by ovs_flow_key_extract()
351 * in this case is for a first fragment, so we need to
352 * properly mark later fragments.
353 */
354 later_key = *key;
355 later_key.ip.frag = OVS_FRAG_TYPE_LATER;
356 }
357
Jesse Grossccb13522011-10-25 19:26:31 -0700358 /* Queue all of the segments. */
359 skb = segs;
360 do {
Pravin B Shelare8eedb82014-11-06 06:57:27 -0800361 if (gso_type & SKB_GSO_UDP && skb != segs)
362 key = &later_key;
363
364 err = queue_userspace_packet(dp, skb, key, upcall_info);
Jesse Grossccb13522011-10-25 19:26:31 -0700365 if (err)
366 break;
367
Jesse Grossccb13522011-10-25 19:26:31 -0700368 } while ((skb = skb->next));
369
370 /* Free all of the segments. */
371 skb = segs;
372 do {
373 nskb = skb->next;
374 if (err)
375 kfree_skb(skb);
376 else
377 consume_skb(skb);
378 } while ((skb = nskb));
379 return err;
380}
381
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800382static size_t upcall_msg_size(const struct dp_upcall_info *upcall_info,
Thomas Grafbda56f12013-12-13 15:22:21 +0100383 unsigned int hdrlen)
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100384{
385 size_t size = NLMSG_ALIGN(sizeof(struct ovs_header))
Thomas Grafbda56f12013-12-13 15:22:21 +0100386 + nla_total_size(hdrlen) /* OVS_PACKET_ATTR_PACKET */
Joe Stringer41af73e2014-10-18 16:14:14 -0700387 + nla_total_size(ovs_key_attr_size()); /* OVS_PACKET_ATTR_KEY */
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100388
389 /* OVS_PACKET_ATTR_USERDATA */
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800390 if (upcall_info->userdata)
391 size += NLA_ALIGN(upcall_info->userdata->nla_len);
392
393 /* OVS_PACKET_ATTR_EGRESS_TUN_KEY */
394 if (upcall_info->egress_tun_info)
395 size += nla_total_size(ovs_tun_key_attr_size());
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100396
397 return size;
398}
399
Thomas Graf8055a892013-12-13 15:22:20 +0100400static int queue_userspace_packet(struct datapath *dp, struct sk_buff *skb,
Pravin B Shelare8eedb82014-11-06 06:57:27 -0800401 const struct sw_flow_key *key,
Jesse Grossccb13522011-10-25 19:26:31 -0700402 const struct dp_upcall_info *upcall_info)
403{
404 struct ovs_header *upcall;
405 struct sk_buff *nskb = NULL;
Li RongQing4ee45ea2014-09-02 20:52:28 +0800406 struct sk_buff *user_skb = NULL; /* to be queued to userspace */
Jesse Grossccb13522011-10-25 19:26:31 -0700407 struct nlattr *nla;
Thomas Graf795449d2013-11-30 13:21:32 +0100408 struct genl_info info = {
Thomas Graf8055a892013-12-13 15:22:20 +0100409 .dst_sk = ovs_dp_get_net(dp)->genl_sock,
Thomas Graf795449d2013-11-30 13:21:32 +0100410 .snd_portid = upcall_info->portid,
411 };
412 size_t len;
Thomas Grafbda56f12013-12-13 15:22:21 +0100413 unsigned int hlen;
Thomas Graf8055a892013-12-13 15:22:20 +0100414 int err, dp_ifindex;
415
416 dp_ifindex = get_dpifindex(dp);
417 if (!dp_ifindex)
418 return -ENODEV;
Jesse Grossccb13522011-10-25 19:26:31 -0700419
Jiri Pirkodf8a39d2015-01-13 17:13:44 +0100420 if (skb_vlan_tag_present(skb)) {
Jesse Grossccb13522011-10-25 19:26:31 -0700421 nskb = skb_clone(skb, GFP_ATOMIC);
422 if (!nskb)
423 return -ENOMEM;
424
Jiri Pirko59682502014-11-19 14:04:59 +0100425 nskb = __vlan_hwaccel_push_inside(nskb);
Dan Carpenter8aa51d62012-05-13 08:44:18 +0000426 if (!nskb)
Jesse Grossccb13522011-10-25 19:26:31 -0700427 return -ENOMEM;
428
Jesse Grossccb13522011-10-25 19:26:31 -0700429 skb = nskb;
430 }
431
432 if (nla_attr_size(skb->len) > USHRT_MAX) {
433 err = -EFBIG;
434 goto out;
435 }
436
Thomas Grafbda56f12013-12-13 15:22:21 +0100437 /* Complete checksum if needed */
438 if (skb->ip_summed == CHECKSUM_PARTIAL &&
439 (err = skb_checksum_help(skb)))
440 goto out;
441
442 /* Older versions of OVS user space enforce alignment of the last
443 * Netlink attribute to NLA_ALIGNTO which would require extensive
444 * padding logic. Only perform zerocopy if padding is not required.
445 */
446 if (dp->user_features & OVS_DP_F_UNALIGNED)
447 hlen = skb_zerocopy_headlen(skb);
448 else
449 hlen = skb->len;
450
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800451 len = upcall_msg_size(upcall_info, hlen);
Thomas Graf795449d2013-11-30 13:21:32 +0100452 user_skb = genlmsg_new_unicast(len, &info, GFP_ATOMIC);
Jesse Grossccb13522011-10-25 19:26:31 -0700453 if (!user_skb) {
454 err = -ENOMEM;
455 goto out;
456 }
457
458 upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
459 0, upcall_info->cmd);
460 upcall->dp_ifindex = dp_ifindex;
461
Joe Stringer5b4237b2015-01-21 16:42:48 -0800462 err = ovs_nla_put_key(key, key, OVS_PACKET_ATTR_KEY, false, user_skb);
Andy Zhouf53e3832014-07-17 15:17:44 -0700463 BUG_ON(err);
Jesse Grossccb13522011-10-25 19:26:31 -0700464
465 if (upcall_info->userdata)
Ben Pfaff44901082013-02-15 17:29:22 -0800466 __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA,
467 nla_len(upcall_info->userdata),
468 nla_data(upcall_info->userdata));
Jesse Grossccb13522011-10-25 19:26:31 -0700469
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800470 if (upcall_info->egress_tun_info) {
471 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_EGRESS_TUN_KEY);
472 err = ovs_nla_put_egress_tunnel_key(user_skb,
473 upcall_info->egress_tun_info);
474 BUG_ON(err);
475 nla_nest_end(user_skb, nla);
476 }
477
Thomas Grafbda56f12013-12-13 15:22:21 +0100478 /* Only reserve room for attribute header, packet data is added
479 * in skb_zerocopy() */
480 if (!(nla = nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, 0))) {
481 err = -ENOBUFS;
482 goto out;
483 }
484 nla->nla_len = nla_attr_size(skb->len);
Jesse Grossccb13522011-10-25 19:26:31 -0700485
Zoltan Kiss36d5fe62014-03-26 22:37:45 +0000486 err = skb_zerocopy(user_skb, skb, skb->len, hlen);
487 if (err)
488 goto out;
Jesse Grossccb13522011-10-25 19:26:31 -0700489
Thomas Grafaea0bb42014-01-14 16:27:49 +0000490 /* Pad OVS_PACKET_ATTR_PACKET if linear copy was performed */
491 if (!(dp->user_features & OVS_DP_F_UNALIGNED)) {
492 size_t plen = NLA_ALIGN(user_skb->len) - user_skb->len;
493
494 if (plen > 0)
495 memset(skb_put(user_skb, plen), 0, plen);
496 }
497
Thomas Grafbda56f12013-12-13 15:22:21 +0100498 ((struct nlmsghdr *) user_skb->data)->nlmsg_len = user_skb->len;
499
Thomas Graf8055a892013-12-13 15:22:20 +0100500 err = genlmsg_unicast(ovs_dp_get_net(dp), user_skb, upcall_info->portid);
Li RongQing4ee45ea2014-09-02 20:52:28 +0800501 user_skb = NULL;
Jesse Grossccb13522011-10-25 19:26:31 -0700502out:
Zoltan Kiss36d5fe62014-03-26 22:37:45 +0000503 if (err)
504 skb_tx_error(skb);
Li RongQing4ee45ea2014-09-02 20:52:28 +0800505 kfree_skb(user_skb);
Jesse Grossccb13522011-10-25 19:26:31 -0700506 kfree_skb(nskb);
507 return err;
508}
509
Jesse Grossccb13522011-10-25 19:26:31 -0700510static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
511{
512 struct ovs_header *ovs_header = info->userhdr;
513 struct nlattr **a = info->attrs;
514 struct sw_flow_actions *acts;
515 struct sk_buff *packet;
516 struct sw_flow *flow;
Lorand Jakabd98612b2014-10-06 05:45:32 -0700517 struct sw_flow_actions *sf_acts;
Jesse Grossccb13522011-10-25 19:26:31 -0700518 struct datapath *dp;
519 struct ethhdr *eth;
Pravin B Shelar83c8df22014-09-15 19:20:31 -0700520 struct vport *input_vport;
Jesse Grossccb13522011-10-25 19:26:31 -0700521 int len;
522 int err;
Thomas Graf1ba39802015-01-14 13:56:19 +0000523 bool log = !a[OVS_PACKET_ATTR_PROBE];
Jesse Grossccb13522011-10-25 19:26:31 -0700524
525 err = -EINVAL;
526 if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
Thomas Grafdded45fc2013-03-29 14:46:47 +0100527 !a[OVS_PACKET_ATTR_ACTIONS])
Jesse Grossccb13522011-10-25 19:26:31 -0700528 goto err;
529
530 len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
531 packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
532 err = -ENOMEM;
533 if (!packet)
534 goto err;
535 skb_reserve(packet, NET_IP_ALIGN);
536
Thomas Graf32686a92013-03-29 14:46:48 +0100537 nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
Jesse Grossccb13522011-10-25 19:26:31 -0700538
539 skb_reset_mac_header(packet);
540 eth = eth_hdr(packet);
541
542 /* Normally, setting the skb 'protocol' field would be handled by a
543 * call to eth_type_trans(), but it assumes there's a sending
544 * device, which we may not have. */
Simon Hormane5c5d222013-03-28 13:38:25 +0900545 if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN)
Jesse Grossccb13522011-10-25 19:26:31 -0700546 packet->protocol = eth->h_proto;
547 else
548 packet->protocol = htons(ETH_P_802_2);
549
550 /* Build an sw_flow for sending this packet. */
Jarno Rajahalme23dabf82014-03-27 12:35:23 -0700551 flow = ovs_flow_alloc();
Jesse Grossccb13522011-10-25 19:26:31 -0700552 err = PTR_ERR(flow);
553 if (IS_ERR(flow))
554 goto err_kfree_skb;
555
Pravin B Shelar83c8df22014-09-15 19:20:31 -0700556 err = ovs_flow_key_extract_userspace(a[OVS_PACKET_ATTR_KEY], packet,
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800557 &flow->key, log);
Jesse Grossccb13522011-10-25 19:26:31 -0700558 if (err)
559 goto err_flow_free;
560
Pravin B Shelare6445712013-10-03 18:16:47 -0700561 err = ovs_nla_copy_actions(a[OVS_PACKET_ATTR_ACTIONS],
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800562 &flow->key, &acts, log);
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700563 if (err)
564 goto err_flow_free;
Jesse Grossccb13522011-10-25 19:26:31 -0700565
Jesse Grossf5796682014-10-03 15:35:33 -0700566 rcu_assign_pointer(flow->sf_acts, acts);
Jesse Grossf5796682014-10-03 15:35:33 -0700567 OVS_CB(packet)->egress_tun_info = NULL;
Jesse Grossccb13522011-10-25 19:26:31 -0700568 packet->priority = flow->key.phy.priority;
Ansis Atteka39c7caeb2012-11-26 11:24:11 -0800569 packet->mark = flow->key.phy.skb_mark;
Jesse Grossccb13522011-10-25 19:26:31 -0700570
571 rcu_read_lock();
Andy Zhoucc3a5ae2014-09-08 13:14:22 -0700572 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -0700573 err = -ENODEV;
574 if (!dp)
575 goto err_unlock;
576
Pravin B Shelar83c8df22014-09-15 19:20:31 -0700577 input_vport = ovs_vport_rcu(dp, flow->key.phy.in_port);
578 if (!input_vport)
579 input_vport = ovs_vport_rcu(dp, OVSP_LOCAL);
580
581 if (!input_vport)
582 goto err_unlock;
583
584 OVS_CB(packet)->input_vport = input_vport;
Lorand Jakabd98612b2014-10-06 05:45:32 -0700585 sf_acts = rcu_dereference(flow->sf_acts);
Pravin B Shelar83c8df22014-09-15 19:20:31 -0700586
Jesse Grossccb13522011-10-25 19:26:31 -0700587 local_bh_disable();
Lorand Jakabd98612b2014-10-06 05:45:32 -0700588 err = ovs_execute_actions(dp, packet, sf_acts, &flow->key);
Jesse Grossccb13522011-10-25 19:26:31 -0700589 local_bh_enable();
590 rcu_read_unlock();
591
Andy Zhou03f0d912013-08-07 20:01:00 -0700592 ovs_flow_free(flow, false);
Jesse Grossccb13522011-10-25 19:26:31 -0700593 return err;
594
595err_unlock:
596 rcu_read_unlock();
597err_flow_free:
Andy Zhou03f0d912013-08-07 20:01:00 -0700598 ovs_flow_free(flow, false);
Jesse Grossccb13522011-10-25 19:26:31 -0700599err_kfree_skb:
600 kfree_skb(packet);
601err:
602 return err;
603}
604
605static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
Thomas Grafdded45fc2013-03-29 14:46:47 +0100606 [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
Jesse Grossccb13522011-10-25 19:26:31 -0700607 [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
608 [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
Thomas Graf1ba39802015-01-14 13:56:19 +0000609 [OVS_PACKET_ATTR_PROBE] = { .type = NLA_FLAG },
Jesse Grossccb13522011-10-25 19:26:31 -0700610};
611
Johannes Berg4534de82013-11-14 17:14:46 +0100612static const struct genl_ops dp_packet_genl_ops[] = {
Jesse Grossccb13522011-10-25 19:26:31 -0700613 { .cmd = OVS_PACKET_CMD_EXECUTE,
614 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
615 .policy = packet_policy,
616 .doit = ovs_packet_cmd_execute
617 }
618};
619
Pravin B Shelar0c200ef2014-05-06 16:44:50 -0700620static struct genl_family dp_packet_genl_family = {
621 .id = GENL_ID_GENERATE,
622 .hdrsize = sizeof(struct ovs_header),
623 .name = OVS_PACKET_FAMILY,
624 .version = OVS_PACKET_VERSION,
625 .maxattr = OVS_PACKET_ATTR_MAX,
626 .netnsok = true,
627 .parallel_ops = true,
628 .ops = dp_packet_genl_ops,
629 .n_ops = ARRAY_SIZE(dp_packet_genl_ops),
630};
631
Thomas Graf12eb18f2014-11-06 06:58:52 -0800632static void get_dp_stats(const struct datapath *dp, struct ovs_dp_stats *stats,
Andy Zhou1bd71162013-10-22 10:42:46 -0700633 struct ovs_dp_megaflow_stats *mega_stats)
Jesse Grossccb13522011-10-25 19:26:31 -0700634{
635 int i;
Jesse Grossccb13522011-10-25 19:26:31 -0700636
Andy Zhou1bd71162013-10-22 10:42:46 -0700637 memset(mega_stats, 0, sizeof(*mega_stats));
638
Pravin B Shelarb637e492013-10-04 00:14:23 -0700639 stats->n_flows = ovs_flow_tbl_count(&dp->table);
Andy Zhou1bd71162013-10-22 10:42:46 -0700640 mega_stats->n_masks = ovs_flow_tbl_num_masks(&dp->table);
Jesse Grossccb13522011-10-25 19:26:31 -0700641
642 stats->n_hit = stats->n_missed = stats->n_lost = 0;
Andy Zhou1bd71162013-10-22 10:42:46 -0700643
Jesse Grossccb13522011-10-25 19:26:31 -0700644 for_each_possible_cpu(i) {
645 const struct dp_stats_percpu *percpu_stats;
646 struct dp_stats_percpu local_stats;
647 unsigned int start;
648
649 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
650
651 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700652 start = u64_stats_fetch_begin_irq(&percpu_stats->syncp);
Jesse Grossccb13522011-10-25 19:26:31 -0700653 local_stats = *percpu_stats;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700654 } while (u64_stats_fetch_retry_irq(&percpu_stats->syncp, start));
Jesse Grossccb13522011-10-25 19:26:31 -0700655
656 stats->n_hit += local_stats.n_hit;
657 stats->n_missed += local_stats.n_missed;
658 stats->n_lost += local_stats.n_lost;
Andy Zhou1bd71162013-10-22 10:42:46 -0700659 mega_stats->n_mask_hit += local_stats.n_mask_hit;
Jesse Grossccb13522011-10-25 19:26:31 -0700660 }
661}
662
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800663static bool should_fill_key(const struct sw_flow_id *sfid, uint32_t ufid_flags)
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100664{
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800665 return ovs_identifier_is_ufid(sfid) &&
666 !(ufid_flags & OVS_UFID_F_OMIT_KEY);
667}
668
669static bool should_fill_mask(uint32_t ufid_flags)
670{
671 return !(ufid_flags & OVS_UFID_F_OMIT_MASK);
672}
673
674static bool should_fill_actions(uint32_t ufid_flags)
675{
676 return !(ufid_flags & OVS_UFID_F_OMIT_ACTIONS);
677}
678
679static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts,
680 const struct sw_flow_id *sfid,
681 uint32_t ufid_flags)
682{
683 size_t len = NLMSG_ALIGN(sizeof(struct ovs_header));
684
685 /* OVS_FLOW_ATTR_UFID */
686 if (sfid && ovs_identifier_is_ufid(sfid))
687 len += nla_total_size(sfid->ufid_len);
688
689 /* OVS_FLOW_ATTR_KEY */
690 if (!sfid || should_fill_key(sfid, ufid_flags))
691 len += nla_total_size(ovs_key_attr_size());
692
693 /* OVS_FLOW_ATTR_MASK */
694 if (should_fill_mask(ufid_flags))
695 len += nla_total_size(ovs_key_attr_size());
696
697 /* OVS_FLOW_ATTR_ACTIONS */
698 if (should_fill_actions(ufid_flags))
699 len += nla_total_size(acts->actions_len);
700
701 return len
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100702 + nla_total_size(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
703 + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800704 + nla_total_size(8); /* OVS_FLOW_ATTR_USED */
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100705}
706
Jarno Rajahalmebb6f9a72014-05-05 11:32:17 -0700707/* Called with ovs_mutex or RCU read lock. */
Joe Stringerca7105f2014-09-08 13:09:37 -0700708static int ovs_flow_cmd_fill_stats(const struct sw_flow *flow,
709 struct sk_buff *skb)
710{
711 struct ovs_flow_stats stats;
712 __be16 tcp_flags;
713 unsigned long used;
Andy Zhou03f0d912013-08-07 20:01:00 -0700714
Pravin B Shelare298e502013-10-29 17:22:21 -0700715 ovs_flow_stats_get(flow, &stats, &used, &tcp_flags);
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700716
David S. Miller028d6a62012-03-29 23:20:48 -0400717 if (used &&
718 nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
Joe Stringerca7105f2014-09-08 13:09:37 -0700719 return -EMSGSIZE;
Jesse Grossccb13522011-10-25 19:26:31 -0700720
David S. Miller028d6a62012-03-29 23:20:48 -0400721 if (stats.n_packets &&
Pravin B Shelare298e502013-10-29 17:22:21 -0700722 nla_put(skb, OVS_FLOW_ATTR_STATS, sizeof(struct ovs_flow_stats), &stats))
Joe Stringerca7105f2014-09-08 13:09:37 -0700723 return -EMSGSIZE;
Jesse Grossccb13522011-10-25 19:26:31 -0700724
Pravin B Shelare298e502013-10-29 17:22:21 -0700725 if ((u8)ntohs(tcp_flags) &&
726 nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, (u8)ntohs(tcp_flags)))
Joe Stringerca7105f2014-09-08 13:09:37 -0700727 return -EMSGSIZE;
728
729 return 0;
730}
731
732/* Called with ovs_mutex or RCU read lock. */
733static int ovs_flow_cmd_fill_actions(const struct sw_flow *flow,
734 struct sk_buff *skb, int skb_orig_len)
735{
736 struct nlattr *start;
737 int err;
Jesse Grossccb13522011-10-25 19:26:31 -0700738
739 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
740 * this is the first flow to be dumped into 'skb'. This is unusual for
741 * Netlink but individual action lists can be longer than
742 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
743 * The userspace caller can always fetch the actions separately if it
744 * really wants them. (Most userspace callers in fact don't care.)
745 *
746 * This can only fail for dump operations because the skb is always
747 * properly sized for single flows.
748 */
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700749 start = nla_nest_start(skb, OVS_FLOW_ATTR_ACTIONS);
750 if (start) {
Pravin B Shelard57170b2013-07-30 15:39:39 -0700751 const struct sw_flow_actions *sf_acts;
752
Jesse Gross663efa32013-12-03 10:58:53 -0800753 sf_acts = rcu_dereference_ovsl(flow->sf_acts);
Pravin B Shelare6445712013-10-03 18:16:47 -0700754 err = ovs_nla_put_actions(sf_acts->actions,
755 sf_acts->actions_len, skb);
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700756
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700757 if (!err)
758 nla_nest_end(skb, start);
759 else {
760 if (skb_orig_len)
Joe Stringerca7105f2014-09-08 13:09:37 -0700761 return err;
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700762
763 nla_nest_cancel(skb, start);
764 }
Joe Stringerca7105f2014-09-08 13:09:37 -0700765 } else if (skb_orig_len) {
766 return -EMSGSIZE;
767 }
768
769 return 0;
770}
771
772/* Called with ovs_mutex or RCU read lock. */
773static int ovs_flow_cmd_fill_info(const struct sw_flow *flow, int dp_ifindex,
774 struct sk_buff *skb, u32 portid,
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800775 u32 seq, u32 flags, u8 cmd, u32 ufid_flags)
Joe Stringerca7105f2014-09-08 13:09:37 -0700776{
777 const int skb_orig_len = skb->len;
778 struct ovs_header *ovs_header;
779 int err;
780
781 ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family,
782 flags, cmd);
783 if (!ovs_header)
784 return -EMSGSIZE;
785
786 ovs_header->dp_ifindex = dp_ifindex;
787
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800788 err = ovs_nla_put_identifier(flow, skb);
Joe Stringer5b4237b2015-01-21 16:42:48 -0800789 if (err)
790 goto error;
791
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800792 if (should_fill_key(&flow->id, ufid_flags)) {
793 err = ovs_nla_put_masked_key(flow, skb);
794 if (err)
795 goto error;
796 }
797
798 if (should_fill_mask(ufid_flags)) {
799 err = ovs_nla_put_mask(flow, skb);
800 if (err)
801 goto error;
802 }
Joe Stringerca7105f2014-09-08 13:09:37 -0700803
804 err = ovs_flow_cmd_fill_stats(flow, skb);
805 if (err)
806 goto error;
807
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800808 if (should_fill_actions(ufid_flags)) {
809 err = ovs_flow_cmd_fill_actions(flow, skb, skb_orig_len);
810 if (err)
811 goto error;
812 }
Jesse Grossccb13522011-10-25 19:26:31 -0700813
Johannes Berg053c0952015-01-16 22:09:00 +0100814 genlmsg_end(skb, ovs_header);
815 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -0700816
Jesse Grossccb13522011-10-25 19:26:31 -0700817error:
818 genlmsg_cancel(skb, ovs_header);
819 return err;
820}
821
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700822/* May not be called with RCU read lock. */
823static struct sk_buff *ovs_flow_cmd_alloc_info(const struct sw_flow_actions *acts,
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800824 const struct sw_flow_id *sfid,
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700825 struct genl_info *info,
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800826 bool always,
827 uint32_t ufid_flags)
Jesse Grossccb13522011-10-25 19:26:31 -0700828{
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700829 struct sk_buff *skb;
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800830 size_t len;
Jesse Grossccb13522011-10-25 19:26:31 -0700831
Samuel Gauthier9b67aa42014-09-18 10:31:04 +0200832 if (!always && !ovs_must_notify(&dp_flow_genl_family, info, 0))
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700833 return NULL;
834
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800835 len = ovs_flow_cmd_msg_size(acts, sfid, ufid_flags);
836 skb = genlmsg_new_unicast(len, info, GFP_KERNEL);
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700837 if (!skb)
838 return ERR_PTR(-ENOMEM);
839
840 return skb;
Jesse Grossccb13522011-10-25 19:26:31 -0700841}
842
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700843/* Called with ovs_mutex. */
844static struct sk_buff *ovs_flow_cmd_build_info(const struct sw_flow *flow,
845 int dp_ifindex,
846 struct genl_info *info, u8 cmd,
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800847 bool always, u32 ufid_flags)
Jesse Grossccb13522011-10-25 19:26:31 -0700848{
849 struct sk_buff *skb;
850 int retval;
851
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800852 skb = ovs_flow_cmd_alloc_info(ovsl_dereference(flow->sf_acts),
853 &flow->id, info, always, ufid_flags);
Himangi Saraogid0e992a2014-07-27 12:37:46 +0530854 if (IS_ERR_OR_NULL(skb))
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700855 return skb;
Jesse Grossccb13522011-10-25 19:26:31 -0700856
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700857 retval = ovs_flow_cmd_fill_info(flow, dp_ifindex, skb,
858 info->snd_portid, info->snd_seq, 0,
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800859 cmd, ufid_flags);
Jesse Grossccb13522011-10-25 19:26:31 -0700860 BUG_ON(retval < 0);
861 return skb;
862}
863
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700864static int ovs_flow_cmd_new(struct sk_buff *skb, struct genl_info *info)
Jesse Grossccb13522011-10-25 19:26:31 -0700865{
866 struct nlattr **a = info->attrs;
867 struct ovs_header *ovs_header = info->userhdr;
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800868 struct sw_flow *flow = NULL, *new_flow;
Andy Zhou03f0d912013-08-07 20:01:00 -0700869 struct sw_flow_mask mask;
Jesse Grossccb13522011-10-25 19:26:31 -0700870 struct sk_buff *reply;
871 struct datapath *dp;
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800872 struct sw_flow_key key;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700873 struct sw_flow_actions *acts;
874 struct sw_flow_match match;
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800875 u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700876 int error;
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800877 bool log = !a[OVS_FLOW_ATTR_PROBE];
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700878
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700879 /* Must have key and actions. */
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700880 error = -EINVAL;
Jesse Gross426cda52014-10-06 05:08:38 -0700881 if (!a[OVS_FLOW_ATTR_KEY]) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800882 OVS_NLERR(log, "Flow key attr not present in new flow.");
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700883 goto error;
Jesse Gross426cda52014-10-06 05:08:38 -0700884 }
885 if (!a[OVS_FLOW_ATTR_ACTIONS]) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800886 OVS_NLERR(log, "Flow actions attr not present in new flow.");
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700887 goto error;
Jesse Gross426cda52014-10-06 05:08:38 -0700888 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700889
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700890 /* Most of the time we need to allocate a new flow, do it before
891 * locking.
892 */
893 new_flow = ovs_flow_alloc();
894 if (IS_ERR(new_flow)) {
895 error = PTR_ERR(new_flow);
896 goto error;
897 }
898
899 /* Extract key. */
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800900 ovs_match_init(&match, &key, &mask);
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800901 error = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY],
902 a[OVS_FLOW_ATTR_MASK], log);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700903 if (error)
904 goto err_kfree_flow;
905
Jesse Gross6d80e352015-09-21 20:21:20 -0700906 ovs_flow_mask_key(&new_flow->key, &key, true, &mask);
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800907
908 /* Extract flow identifier. */
909 error = ovs_nla_get_identifier(&new_flow->id, a[OVS_FLOW_ATTR_UFID],
910 &key, log);
911 if (error)
912 goto err_kfree_flow;
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700913
914 /* Validate actions. */
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700915 error = ovs_nla_copy_actions(a[OVS_FLOW_ATTR_ACTIONS], &new_flow->key,
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800916 &acts, log);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700917 if (error) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800918 OVS_NLERR(log, "Flow actions may not be safe on all matching packets.");
Pravin B Shelar2fdb9572014-10-19 11:19:51 -0700919 goto err_kfree_flow;
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700920 }
921
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800922 reply = ovs_flow_cmd_alloc_info(acts, &new_flow->id, info, false,
923 ufid_flags);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700924 if (IS_ERR(reply)) {
925 error = PTR_ERR(reply);
926 goto err_kfree_acts;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700927 }
928
929 ovs_lock();
930 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700931 if (unlikely(!dp)) {
932 error = -ENODEV;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700933 goto err_unlock_ovs;
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700934 }
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800935
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700936 /* Check if this is a duplicate flow */
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800937 if (ovs_identifier_is_ufid(&new_flow->id))
938 flow = ovs_flow_tbl_lookup_ufid(&dp->table, &new_flow->id);
939 if (!flow)
940 flow = ovs_flow_tbl_lookup(&dp->table, &key);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700941 if (likely(!flow)) {
942 rcu_assign_pointer(new_flow->sf_acts, acts);
943
944 /* Put flow in bucket. */
945 error = ovs_flow_tbl_insert(&dp->table, new_flow, &mask);
946 if (unlikely(error)) {
947 acts = NULL;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700948 goto err_unlock_ovs;
949 }
950
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700951 if (unlikely(reply)) {
952 error = ovs_flow_cmd_fill_info(new_flow,
953 ovs_header->dp_ifindex,
954 reply, info->snd_portid,
955 info->snd_seq, 0,
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800956 OVS_FLOW_CMD_NEW,
957 ufid_flags);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700958 BUG_ON(error < 0);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700959 }
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700960 ovs_unlock();
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700961 } else {
962 struct sw_flow_actions *old_acts;
963
964 /* Bail out if we're not allowed to modify an existing flow.
965 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
966 * because Generic Netlink treats the latter as a dump
967 * request. We also accept NLM_F_EXCL in case that bug ever
968 * gets fixed.
969 */
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700970 if (unlikely(info->nlhdr->nlmsg_flags & (NLM_F_CREATE
971 | NLM_F_EXCL))) {
972 error = -EEXIST;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700973 goto err_unlock_ovs;
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700974 }
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800975 /* The flow identifier has to be the same for flow updates.
976 * Look for any overlapping flow.
977 */
978 if (unlikely(!ovs_flow_cmp(flow, &match))) {
979 if (ovs_identifier_is_key(&flow->id))
980 flow = ovs_flow_tbl_lookup_exact(&dp->table,
981 &match);
982 else /* UFID matches but key is different */
983 flow = NULL;
Alex Wang4a46b242014-06-30 20:30:29 -0700984 if (!flow) {
985 error = -ENOENT;
986 goto err_unlock_ovs;
987 }
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700988 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700989 /* Update actions. */
990 old_acts = ovsl_dereference(flow->sf_acts);
991 rcu_assign_pointer(flow->sf_acts, acts);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700992
993 if (unlikely(reply)) {
994 error = ovs_flow_cmd_fill_info(flow,
995 ovs_header->dp_ifindex,
996 reply, info->snd_portid,
997 info->snd_seq, 0,
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800998 OVS_FLOW_CMD_NEW,
999 ufid_flags);
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001000 BUG_ON(error < 0);
1001 }
1002 ovs_unlock();
1003
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001004 ovs_nla_free_flow_actions(old_acts);
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001005 ovs_flow_free(new_flow, false);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001006 }
1007
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001008 if (reply)
1009 ovs_notify(&dp_flow_genl_family, reply, info);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001010 return 0;
1011
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001012err_unlock_ovs:
1013 ovs_unlock();
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001014 kfree_skb(reply);
1015err_kfree_acts:
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001016 kfree(acts);
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001017err_kfree_flow:
1018 ovs_flow_free(new_flow, false);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001019error:
1020 return error;
1021}
1022
Pravin B Shelar2fdb9572014-10-19 11:19:51 -07001023/* Factor out action copy to avoid "Wframe-larger-than=1024" warning. */
Jesse Gross6b205b22014-10-03 15:35:32 -07001024static struct sw_flow_actions *get_flow_actions(const struct nlattr *a,
1025 const struct sw_flow_key *key,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001026 const struct sw_flow_mask *mask,
1027 bool log)
Jesse Gross6b205b22014-10-03 15:35:32 -07001028{
1029 struct sw_flow_actions *acts;
1030 struct sw_flow_key masked_key;
1031 int error;
1032
Jesse Gross6d80e352015-09-21 20:21:20 -07001033 ovs_flow_mask_key(&masked_key, key, true, mask);
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001034 error = ovs_nla_copy_actions(a, &masked_key, &acts, log);
Jesse Gross6b205b22014-10-03 15:35:32 -07001035 if (error) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001036 OVS_NLERR(log,
1037 "Actions may not be safe on all matching packets");
Jesse Gross6b205b22014-10-03 15:35:32 -07001038 return ERR_PTR(error);
1039 }
1040
1041 return acts;
1042}
1043
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001044static int ovs_flow_cmd_set(struct sk_buff *skb, struct genl_info *info)
1045{
1046 struct nlattr **a = info->attrs;
1047 struct ovs_header *ovs_header = info->userhdr;
Jesse Gross6b205b22014-10-03 15:35:32 -07001048 struct sw_flow_key key;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001049 struct sw_flow *flow;
1050 struct sw_flow_mask mask;
1051 struct sk_buff *reply = NULL;
1052 struct datapath *dp;
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001053 struct sw_flow_actions *old_acts = NULL, *acts = NULL;
Andy Zhou03f0d912013-08-07 20:01:00 -07001054 struct sw_flow_match match;
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001055 struct sw_flow_id sfid;
1056 u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
Jesse Grossccb13522011-10-25 19:26:31 -07001057 int error;
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001058 bool log = !a[OVS_FLOW_ATTR_PROBE];
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001059 bool ufid_present;
Jesse Grossccb13522011-10-25 19:26:31 -07001060
1061 /* Extract key. */
1062 error = -EINVAL;
Jesse Gross426cda52014-10-06 05:08:38 -07001063 if (!a[OVS_FLOW_ATTR_KEY]) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001064 OVS_NLERR(log, "Flow key attribute not present in set flow.");
Jesse Grossccb13522011-10-25 19:26:31 -07001065 goto error;
Jesse Gross426cda52014-10-06 05:08:38 -07001066 }
Andy Zhou03f0d912013-08-07 20:01:00 -07001067
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001068 ufid_present = ovs_nla_get_ufid(&sfid, a[OVS_FLOW_ATTR_UFID], log);
Andy Zhou03f0d912013-08-07 20:01:00 -07001069 ovs_match_init(&match, &key, &mask);
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001070 error = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY],
1071 a[OVS_FLOW_ATTR_MASK], log);
Jesse Grossccb13522011-10-25 19:26:31 -07001072 if (error)
1073 goto error;
1074
1075 /* Validate actions. */
1076 if (a[OVS_FLOW_ATTR_ACTIONS]) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001077 acts = get_flow_actions(a[OVS_FLOW_ATTR_ACTIONS], &key, &mask,
1078 log);
Jesse Gross6b205b22014-10-03 15:35:32 -07001079 if (IS_ERR(acts)) {
1080 error = PTR_ERR(acts);
Jesse Grossccb13522011-10-25 19:26:31 -07001081 goto error;
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001082 }
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001083
Pravin B Shelar2fdb9572014-10-19 11:19:51 -07001084 /* Can allocate before locking if have acts. */
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001085 reply = ovs_flow_cmd_alloc_info(acts, &sfid, info, false,
1086 ufid_flags);
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001087 if (IS_ERR(reply)) {
1088 error = PTR_ERR(reply);
1089 goto err_kfree_acts;
Andy Zhou03f0d912013-08-07 20:01:00 -07001090 }
Jesse Grossccb13522011-10-25 19:26:31 -07001091 }
1092
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001093 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001094 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001095 if (unlikely(!dp)) {
1096 error = -ENODEV;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001097 goto err_unlock_ovs;
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001098 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001099 /* Check that the flow exists. */
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001100 if (ufid_present)
1101 flow = ovs_flow_tbl_lookup_ufid(&dp->table, &sfid);
1102 else
1103 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001104 if (unlikely(!flow)) {
1105 error = -ENOENT;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001106 goto err_unlock_ovs;
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001107 }
Alex Wang4a46b242014-06-30 20:30:29 -07001108
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001109 /* Update actions, if present. */
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001110 if (likely(acts)) {
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001111 old_acts = ovsl_dereference(flow->sf_acts);
Jesse Grossccb13522011-10-25 19:26:31 -07001112 rcu_assign_pointer(flow->sf_acts, acts);
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001113
1114 if (unlikely(reply)) {
1115 error = ovs_flow_cmd_fill_info(flow,
1116 ovs_header->dp_ifindex,
1117 reply, info->snd_portid,
1118 info->snd_seq, 0,
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001119 OVS_FLOW_CMD_NEW,
1120 ufid_flags);
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001121 BUG_ON(error < 0);
1122 }
1123 } else {
1124 /* Could not alloc without acts before locking. */
1125 reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex,
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001126 info, OVS_FLOW_CMD_NEW, false,
1127 ufid_flags);
1128
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001129 if (unlikely(IS_ERR(reply))) {
1130 error = PTR_ERR(reply);
1131 goto err_unlock_ovs;
1132 }
Jesse Grossccb13522011-10-25 19:26:31 -07001133 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001134
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001135 /* Clear stats. */
1136 if (a[OVS_FLOW_ATTR_CLEAR])
1137 ovs_flow_stats_clear(flow);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001138 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001139
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001140 if (reply)
1141 ovs_notify(&dp_flow_genl_family, reply, info);
1142 if (old_acts)
1143 ovs_nla_free_flow_actions(old_acts);
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -07001144
Jesse Grossccb13522011-10-25 19:26:31 -07001145 return 0;
1146
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001147err_unlock_ovs:
1148 ovs_unlock();
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001149 kfree_skb(reply);
1150err_kfree_acts:
Pravin B Shelar74f84a52013-06-17 17:50:12 -07001151 kfree(acts);
Jesse Grossccb13522011-10-25 19:26:31 -07001152error:
1153 return error;
1154}
1155
1156static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1157{
1158 struct nlattr **a = info->attrs;
1159 struct ovs_header *ovs_header = info->userhdr;
1160 struct sw_flow_key key;
1161 struct sk_buff *reply;
1162 struct sw_flow *flow;
1163 struct datapath *dp;
Andy Zhou03f0d912013-08-07 20:01:00 -07001164 struct sw_flow_match match;
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001165 struct sw_flow_id ufid;
1166 u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
1167 int err = 0;
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001168 bool log = !a[OVS_FLOW_ATTR_PROBE];
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001169 bool ufid_present;
Jesse Grossccb13522011-10-25 19:26:31 -07001170
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001171 ufid_present = ovs_nla_get_ufid(&ufid, a[OVS_FLOW_ATTR_UFID], log);
1172 if (a[OVS_FLOW_ATTR_KEY]) {
1173 ovs_match_init(&match, &key, NULL);
1174 err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL,
1175 log);
1176 } else if (!ufid_present) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001177 OVS_NLERR(log,
1178 "Flow get message rejected, Key attribute missing.");
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001179 err = -EINVAL;
Andy Zhou03f0d912013-08-07 20:01:00 -07001180 }
Jesse Grossccb13522011-10-25 19:26:31 -07001181 if (err)
1182 return err;
1183
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001184 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001185 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001186 if (!dp) {
1187 err = -ENODEV;
1188 goto unlock;
1189 }
Jesse Grossccb13522011-10-25 19:26:31 -07001190
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001191 if (ufid_present)
1192 flow = ovs_flow_tbl_lookup_ufid(&dp->table, &ufid);
1193 else
1194 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
Alex Wang4a46b242014-06-30 20:30:29 -07001195 if (!flow) {
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001196 err = -ENOENT;
1197 goto unlock;
1198 }
Jesse Grossccb13522011-10-25 19:26:31 -07001199
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -07001200 reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex, info,
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001201 OVS_FLOW_CMD_NEW, true, ufid_flags);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001202 if (IS_ERR(reply)) {
1203 err = PTR_ERR(reply);
1204 goto unlock;
1205 }
Jesse Grossccb13522011-10-25 19:26:31 -07001206
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001207 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001208 return genlmsg_reply(reply, info);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001209unlock:
1210 ovs_unlock();
1211 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001212}
1213
1214static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1215{
1216 struct nlattr **a = info->attrs;
1217 struct ovs_header *ovs_header = info->userhdr;
1218 struct sw_flow_key key;
1219 struct sk_buff *reply;
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001220 struct sw_flow *flow = NULL;
Jesse Grossccb13522011-10-25 19:26:31 -07001221 struct datapath *dp;
Andy Zhou03f0d912013-08-07 20:01:00 -07001222 struct sw_flow_match match;
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001223 struct sw_flow_id ufid;
1224 u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
Jesse Grossccb13522011-10-25 19:26:31 -07001225 int err;
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001226 bool log = !a[OVS_FLOW_ATTR_PROBE];
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001227 bool ufid_present;
Jesse Grossccb13522011-10-25 19:26:31 -07001228
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001229 ufid_present = ovs_nla_get_ufid(&ufid, a[OVS_FLOW_ATTR_UFID], log);
1230 if (a[OVS_FLOW_ATTR_KEY]) {
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001231 ovs_match_init(&match, &key, NULL);
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001232 err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL,
1233 log);
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001234 if (unlikely(err))
1235 return err;
1236 }
1237
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001238 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001239 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001240 if (unlikely(!dp)) {
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001241 err = -ENODEV;
1242 goto unlock;
1243 }
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001244
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001245 if (unlikely(!a[OVS_FLOW_ATTR_KEY] && !ufid_present)) {
Pravin B Shelarb637e492013-10-04 00:14:23 -07001246 err = ovs_flow_tbl_flush(&dp->table);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001247 goto unlock;
1248 }
Andy Zhou03f0d912013-08-07 20:01:00 -07001249
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001250 if (ufid_present)
1251 flow = ovs_flow_tbl_lookup_ufid(&dp->table, &ufid);
1252 else
1253 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
Alex Wang4a46b242014-06-30 20:30:29 -07001254 if (unlikely(!flow)) {
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001255 err = -ENOENT;
1256 goto unlock;
1257 }
Jesse Grossccb13522011-10-25 19:26:31 -07001258
Pravin B Shelarb637e492013-10-04 00:14:23 -07001259 ovs_flow_tbl_remove(&dp->table, flow);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001260 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001261
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001262 reply = ovs_flow_cmd_alloc_info((const struct sw_flow_actions __force *) flow->sf_acts,
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001263 &flow->id, info, false, ufid_flags);
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001264 if (likely(reply)) {
1265 if (likely(!IS_ERR(reply))) {
1266 rcu_read_lock(); /*To keep RCU checker happy. */
1267 err = ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex,
1268 reply, info->snd_portid,
1269 info->snd_seq, 0,
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001270 OVS_FLOW_CMD_DEL,
1271 ufid_flags);
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001272 rcu_read_unlock();
1273 BUG_ON(err < 0);
1274
1275 ovs_notify(&dp_flow_genl_family, reply, info);
1276 } else {
1277 netlink_set_err(sock_net(skb->sk)->genl_sock, 0, 0, PTR_ERR(reply));
1278 }
1279 }
1280
1281 ovs_flow_free(flow, true);
Jesse Grossccb13522011-10-25 19:26:31 -07001282 return 0;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001283unlock:
1284 ovs_unlock();
1285 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001286}
1287
1288static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1289{
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001290 struct nlattr *a[__OVS_FLOW_ATTR_MAX];
Jesse Grossccb13522011-10-25 19:26:31 -07001291 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
Pravin B Shelarb637e492013-10-04 00:14:23 -07001292 struct table_instance *ti;
Jesse Grossccb13522011-10-25 19:26:31 -07001293 struct datapath *dp;
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001294 u32 ufid_flags;
1295 int err;
1296
1297 err = genlmsg_parse(cb->nlh, &dp_flow_genl_family, a,
1298 OVS_FLOW_ATTR_MAX, flow_policy);
1299 if (err)
1300 return err;
1301 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
Jesse Grossccb13522011-10-25 19:26:31 -07001302
Pravin B Shelard57170b2013-07-30 15:39:39 -07001303 rcu_read_lock();
Andy Zhoucc3a5ae2014-09-08 13:14:22 -07001304 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001305 if (!dp) {
Pravin B Shelard57170b2013-07-30 15:39:39 -07001306 rcu_read_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001307 return -ENODEV;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001308 }
Jesse Grossccb13522011-10-25 19:26:31 -07001309
Pravin B Shelarb637e492013-10-04 00:14:23 -07001310 ti = rcu_dereference(dp->table.ti);
Jesse Grossccb13522011-10-25 19:26:31 -07001311 for (;;) {
1312 struct sw_flow *flow;
1313 u32 bucket, obj;
1314
1315 bucket = cb->args[0];
1316 obj = cb->args[1];
Pravin B Shelarb637e492013-10-04 00:14:23 -07001317 flow = ovs_flow_tbl_dump_next(ti, &bucket, &obj);
Jesse Grossccb13522011-10-25 19:26:31 -07001318 if (!flow)
1319 break;
1320
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -07001321 if (ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex, skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001322 NETLINK_CB(cb->skb).portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001323 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001324 OVS_FLOW_CMD_NEW, ufid_flags) < 0)
Jesse Grossccb13522011-10-25 19:26:31 -07001325 break;
1326
1327 cb->args[0] = bucket;
1328 cb->args[1] = obj;
1329 }
Pravin B Shelard57170b2013-07-30 15:39:39 -07001330 rcu_read_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001331 return skb->len;
1332}
1333
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001334static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
1335 [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001336 [OVS_FLOW_ATTR_MASK] = { .type = NLA_NESTED },
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001337 [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
1338 [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001339 [OVS_FLOW_ATTR_PROBE] = { .type = NLA_FLAG },
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001340 [OVS_FLOW_ATTR_UFID] = { .type = NLA_UNSPEC, .len = 1 },
1341 [OVS_FLOW_ATTR_UFID_FLAGS] = { .type = NLA_U32 },
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001342};
1343
stephen hemminger48e48a72014-07-16 11:25:52 -07001344static const struct genl_ops dp_flow_genl_ops[] = {
Jesse Grossccb13522011-10-25 19:26:31 -07001345 { .cmd = OVS_FLOW_CMD_NEW,
1346 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1347 .policy = flow_policy,
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001348 .doit = ovs_flow_cmd_new
Jesse Grossccb13522011-10-25 19:26:31 -07001349 },
1350 { .cmd = OVS_FLOW_CMD_DEL,
1351 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1352 .policy = flow_policy,
1353 .doit = ovs_flow_cmd_del
1354 },
1355 { .cmd = OVS_FLOW_CMD_GET,
1356 .flags = 0, /* OK for unprivileged users. */
1357 .policy = flow_policy,
1358 .doit = ovs_flow_cmd_get,
1359 .dumpit = ovs_flow_cmd_dump
1360 },
1361 { .cmd = OVS_FLOW_CMD_SET,
1362 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1363 .policy = flow_policy,
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001364 .doit = ovs_flow_cmd_set,
Jesse Grossccb13522011-10-25 19:26:31 -07001365 },
1366};
1367
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001368static struct genl_family dp_flow_genl_family = {
Jesse Grossccb13522011-10-25 19:26:31 -07001369 .id = GENL_ID_GENERATE,
1370 .hdrsize = sizeof(struct ovs_header),
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001371 .name = OVS_FLOW_FAMILY,
1372 .version = OVS_FLOW_VERSION,
1373 .maxattr = OVS_FLOW_ATTR_MAX,
Pravin B Shelar3a4e0d62013-04-23 07:48:48 +00001374 .netnsok = true,
1375 .parallel_ops = true,
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001376 .ops = dp_flow_genl_ops,
1377 .n_ops = ARRAY_SIZE(dp_flow_genl_ops),
1378 .mcgrps = &ovs_dp_flow_multicast_group,
1379 .n_mcgrps = 1,
Jesse Grossccb13522011-10-25 19:26:31 -07001380};
1381
Thomas Grafc3ff8cf2013-03-29 14:46:49 +01001382static size_t ovs_dp_cmd_msg_size(void)
1383{
1384 size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
1385
1386 msgsize += nla_total_size(IFNAMSIZ);
1387 msgsize += nla_total_size(sizeof(struct ovs_dp_stats));
Andy Zhou1bd71162013-10-22 10:42:46 -07001388 msgsize += nla_total_size(sizeof(struct ovs_dp_megaflow_stats));
Daniele Di Proietto45fb9c32014-01-23 10:47:35 -08001389 msgsize += nla_total_size(sizeof(u32)); /* OVS_DP_ATTR_USER_FEATURES */
Thomas Grafc3ff8cf2013-03-29 14:46:49 +01001390
1391 return msgsize;
1392}
1393
Pravin B Shelar8ec609d2014-11-11 15:55:16 -08001394/* Called with ovs_mutex. */
Jesse Grossccb13522011-10-25 19:26:31 -07001395static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001396 u32 portid, u32 seq, u32 flags, u8 cmd)
Jesse Grossccb13522011-10-25 19:26:31 -07001397{
1398 struct ovs_header *ovs_header;
1399 struct ovs_dp_stats dp_stats;
Andy Zhou1bd71162013-10-22 10:42:46 -07001400 struct ovs_dp_megaflow_stats dp_megaflow_stats;
Jesse Grossccb13522011-10-25 19:26:31 -07001401 int err;
1402
Eric W. Biederman15e47302012-09-07 20:12:54 +00001403 ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
Jesse Grossccb13522011-10-25 19:26:31 -07001404 flags, cmd);
1405 if (!ovs_header)
1406 goto error;
1407
1408 ovs_header->dp_ifindex = get_dpifindex(dp);
1409
Jesse Grossccb13522011-10-25 19:26:31 -07001410 err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
Jesse Grossccb13522011-10-25 19:26:31 -07001411 if (err)
1412 goto nla_put_failure;
1413
Andy Zhou1bd71162013-10-22 10:42:46 -07001414 get_dp_stats(dp, &dp_stats, &dp_megaflow_stats);
1415 if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats),
1416 &dp_stats))
1417 goto nla_put_failure;
1418
1419 if (nla_put(skb, OVS_DP_ATTR_MEGAFLOW_STATS,
1420 sizeof(struct ovs_dp_megaflow_stats),
1421 &dp_megaflow_stats))
David S. Miller028d6a62012-03-29 23:20:48 -04001422 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001423
Thomas Graf43d4be92013-12-13 15:22:18 +01001424 if (nla_put_u32(skb, OVS_DP_ATTR_USER_FEATURES, dp->user_features))
1425 goto nla_put_failure;
1426
Johannes Berg053c0952015-01-16 22:09:00 +01001427 genlmsg_end(skb, ovs_header);
1428 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001429
1430nla_put_failure:
1431 genlmsg_cancel(skb, ovs_header);
1432error:
1433 return -EMSGSIZE;
1434}
1435
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001436static struct sk_buff *ovs_dp_cmd_alloc_info(struct genl_info *info)
Jesse Grossccb13522011-10-25 19:26:31 -07001437{
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001438 return genlmsg_new_unicast(ovs_dp_cmd_msg_size(), info, GFP_KERNEL);
Jesse Grossccb13522011-10-25 19:26:31 -07001439}
1440
Jarno Rajahalmebb6f9a72014-05-05 11:32:17 -07001441/* Called with rcu_read_lock or ovs_mutex. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001442static struct datapath *lookup_datapath(struct net *net,
Thomas Graf12eb18f2014-11-06 06:58:52 -08001443 const struct ovs_header *ovs_header,
Jesse Grossccb13522011-10-25 19:26:31 -07001444 struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1445{
1446 struct datapath *dp;
1447
1448 if (!a[OVS_DP_ATTR_NAME])
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001449 dp = get_dp(net, ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001450 else {
1451 struct vport *vport;
1452
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001453 vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
Jesse Grossccb13522011-10-25 19:26:31 -07001454 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
Jesse Grossccb13522011-10-25 19:26:31 -07001455 }
1456 return dp ? dp : ERR_PTR(-ENODEV);
1457}
1458
Thomas Graf44da5ae2013-12-13 15:22:19 +01001459static void ovs_dp_reset_user_features(struct sk_buff *skb, struct genl_info *info)
1460{
1461 struct datapath *dp;
1462
1463 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
Jiri Pirko3c7eacf2014-02-14 11:42:36 +01001464 if (IS_ERR(dp))
Thomas Graf44da5ae2013-12-13 15:22:19 +01001465 return;
1466
1467 WARN(dp->user_features, "Dropping previously announced user features\n");
1468 dp->user_features = 0;
1469}
1470
Thomas Graf12eb18f2014-11-06 06:58:52 -08001471static void ovs_dp_change(struct datapath *dp, struct nlattr *a[])
Thomas Graf43d4be92013-12-13 15:22:18 +01001472{
1473 if (a[OVS_DP_ATTR_USER_FEATURES])
1474 dp->user_features = nla_get_u32(a[OVS_DP_ATTR_USER_FEATURES]);
1475}
1476
Jesse Grossccb13522011-10-25 19:26:31 -07001477static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1478{
1479 struct nlattr **a = info->attrs;
1480 struct vport_parms parms;
1481 struct sk_buff *reply;
1482 struct datapath *dp;
1483 struct vport *vport;
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001484 struct ovs_net *ovs_net;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001485 int err, i;
Jesse Grossccb13522011-10-25 19:26:31 -07001486
1487 err = -EINVAL;
1488 if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1489 goto err;
1490
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001491 reply = ovs_dp_cmd_alloc_info(info);
1492 if (!reply)
1493 return -ENOMEM;
Jesse Grossccb13522011-10-25 19:26:31 -07001494
1495 err = -ENOMEM;
1496 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1497 if (dp == NULL)
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001498 goto err_free_reply;
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001499
Eric W. Biedermanefd7ef12015-03-11 23:04:08 -05001500 ovs_dp_set_net(dp, sock_net(skb->sk));
Jesse Grossccb13522011-10-25 19:26:31 -07001501
1502 /* Allocate table. */
Pravin B Shelarb637e492013-10-04 00:14:23 -07001503 err = ovs_flow_tbl_init(&dp->table);
1504 if (err)
Jesse Grossccb13522011-10-25 19:26:31 -07001505 goto err_free_dp;
1506
WANG Cong1c213bd2014-02-13 11:46:28 -08001507 dp->stats_percpu = netdev_alloc_pcpu_stats(struct dp_stats_percpu);
Jesse Grossccb13522011-10-25 19:26:31 -07001508 if (!dp->stats_percpu) {
1509 err = -ENOMEM;
1510 goto err_destroy_table;
1511 }
1512
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001513 dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
Pravin B Shelare6445712013-10-03 18:16:47 -07001514 GFP_KERNEL);
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001515 if (!dp->ports) {
1516 err = -ENOMEM;
1517 goto err_destroy_percpu;
1518 }
1519
1520 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1521 INIT_HLIST_HEAD(&dp->ports[i]);
1522
Jesse Grossccb13522011-10-25 19:26:31 -07001523 /* Set up our datapath device. */
1524 parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1525 parms.type = OVS_VPORT_TYPE_INTERNAL;
1526 parms.options = NULL;
1527 parms.dp = dp;
1528 parms.port_no = OVSP_LOCAL;
Alex Wang5cd667b2014-07-17 15:14:13 -07001529 parms.upcall_portids = a[OVS_DP_ATTR_UPCALL_PID];
Jesse Grossccb13522011-10-25 19:26:31 -07001530
Thomas Graf43d4be92013-12-13 15:22:18 +01001531 ovs_dp_change(dp, a);
1532
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001533 /* So far only local changes have been made, now need the lock. */
1534 ovs_lock();
1535
Jesse Grossccb13522011-10-25 19:26:31 -07001536 vport = new_vport(&parms);
1537 if (IS_ERR(vport)) {
1538 err = PTR_ERR(vport);
1539 if (err == -EBUSY)
1540 err = -EEXIST;
1541
Thomas Graf44da5ae2013-12-13 15:22:19 +01001542 if (err == -EEXIST) {
1543 /* An outdated user space instance that does not understand
1544 * the concept of user_features has attempted to create a new
1545 * datapath and is likely to reuse it. Drop all user features.
1546 */
1547 if (info->genlhdr->version < OVS_DP_VER_FEATURES)
1548 ovs_dp_reset_user_features(skb, info);
1549 }
1550
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001551 goto err_destroy_ports_array;
Jesse Grossccb13522011-10-25 19:26:31 -07001552 }
1553
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001554 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1555 info->snd_seq, 0, OVS_DP_CMD_NEW);
1556 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001557
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001558 ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001559 list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001560
1561 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001562
Johannes Berg2a94fe42013-11-19 15:19:39 +01001563 ovs_notify(&dp_datapath_genl_family, reply, info);
Jesse Grossccb13522011-10-25 19:26:31 -07001564 return 0;
1565
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001566err_destroy_ports_array:
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001567 ovs_unlock();
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001568 kfree(dp->ports);
Jesse Grossccb13522011-10-25 19:26:31 -07001569err_destroy_percpu:
1570 free_percpu(dp->stats_percpu);
1571err_destroy_table:
Pravin B Shelar9b996e52014-05-06 18:41:20 -07001572 ovs_flow_tbl_destroy(&dp->table);
Jesse Grossccb13522011-10-25 19:26:31 -07001573err_free_dp:
1574 kfree(dp);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001575err_free_reply:
1576 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001577err:
1578 return err;
1579}
1580
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001581/* Called with ovs_mutex. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001582static void __dp_destroy(struct datapath *dp)
Jesse Grossccb13522011-10-25 19:26:31 -07001583{
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001584 int i;
Jesse Grossccb13522011-10-25 19:26:31 -07001585
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001586 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1587 struct vport *vport;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001588 struct hlist_node *n;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001589
Sasha Levinb67bfe02013-02-27 17:06:00 -08001590 hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001591 if (vport->port_no != OVSP_LOCAL)
1592 ovs_dp_detach_port(vport);
1593 }
Jesse Grossccb13522011-10-25 19:26:31 -07001594
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001595 list_del_rcu(&dp->list_node);
Jesse Grossccb13522011-10-25 19:26:31 -07001596
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001597 /* OVSP_LOCAL is datapath internal port. We need to make sure that
Andy Zhoue80857c2014-01-21 09:31:04 -08001598 * all ports in datapath are destroyed first before freeing datapath.
Jesse Grossccb13522011-10-25 19:26:31 -07001599 */
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001600 ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
Jesse Grossccb13522011-10-25 19:26:31 -07001601
Andy Zhoue80857c2014-01-21 09:31:04 -08001602 /* RCU destroy the flow table */
Jesse Grossccb13522011-10-25 19:26:31 -07001603 call_rcu(&dp->rcu, destroy_dp_rcu);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001604}
1605
1606static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1607{
1608 struct sk_buff *reply;
1609 struct datapath *dp;
1610 int err;
1611
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001612 reply = ovs_dp_cmd_alloc_info(info);
1613 if (!reply)
1614 return -ENOMEM;
1615
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001616 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001617 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1618 err = PTR_ERR(dp);
1619 if (IS_ERR(dp))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001620 goto err_unlock_free;
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001621
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001622 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1623 info->snd_seq, 0, OVS_DP_CMD_DEL);
1624 BUG_ON(err < 0);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001625
1626 __dp_destroy(dp);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001627 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001628
Johannes Berg2a94fe42013-11-19 15:19:39 +01001629 ovs_notify(&dp_datapath_genl_family, reply, info);
Jesse Grossccb13522011-10-25 19:26:31 -07001630
1631 return 0;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001632
1633err_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001634 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001635 kfree_skb(reply);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001636 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001637}
1638
1639static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1640{
1641 struct sk_buff *reply;
1642 struct datapath *dp;
1643 int err;
1644
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001645 reply = ovs_dp_cmd_alloc_info(info);
1646 if (!reply)
1647 return -ENOMEM;
1648
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001649 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001650 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001651 err = PTR_ERR(dp);
Jesse Grossccb13522011-10-25 19:26:31 -07001652 if (IS_ERR(dp))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001653 goto err_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001654
Thomas Graf43d4be92013-12-13 15:22:18 +01001655 ovs_dp_change(dp, info->attrs);
1656
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001657 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1658 info->snd_seq, 0, OVS_DP_CMD_NEW);
1659 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001660
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001661 ovs_unlock();
Johannes Berg2a94fe42013-11-19 15:19:39 +01001662 ovs_notify(&dp_datapath_genl_family, reply, info);
Jesse Grossccb13522011-10-25 19:26:31 -07001663
1664 return 0;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001665
1666err_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001667 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001668 kfree_skb(reply);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001669 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001670}
1671
1672static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1673{
1674 struct sk_buff *reply;
1675 struct datapath *dp;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001676 int err;
Jesse Grossccb13522011-10-25 19:26:31 -07001677
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001678 reply = ovs_dp_cmd_alloc_info(info);
1679 if (!reply)
1680 return -ENOMEM;
1681
Pravin B Shelar8ec609d2014-11-11 15:55:16 -08001682 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001683 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001684 if (IS_ERR(dp)) {
1685 err = PTR_ERR(dp);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001686 goto err_unlock_free;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001687 }
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001688 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1689 info->snd_seq, 0, OVS_DP_CMD_NEW);
1690 BUG_ON(err < 0);
Pravin B Shelar8ec609d2014-11-11 15:55:16 -08001691 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001692
Jesse Grossccb13522011-10-25 19:26:31 -07001693 return genlmsg_reply(reply, info);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001694
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001695err_unlock_free:
Pravin B Shelar8ec609d2014-11-11 15:55:16 -08001696 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001697 kfree_skb(reply);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001698 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001699}
1700
1701static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1702{
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001703 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
Jesse Grossccb13522011-10-25 19:26:31 -07001704 struct datapath *dp;
1705 int skip = cb->args[0];
1706 int i = 0;
1707
Pravin B Shelar8ec609d2014-11-11 15:55:16 -08001708 ovs_lock();
1709 list_for_each_entry(dp, &ovs_net->dps, list_node) {
Ben Pfaff77676fd2012-01-17 13:33:39 +00001710 if (i >= skip &&
Eric W. Biederman15e47302012-09-07 20:12:54 +00001711 ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001712 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1713 OVS_DP_CMD_NEW) < 0)
1714 break;
1715 i++;
1716 }
Pravin B Shelar8ec609d2014-11-11 15:55:16 -08001717 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001718
1719 cb->args[0] = i;
1720
1721 return skb->len;
1722}
1723
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001724static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1725 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1726 [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1727 [OVS_DP_ATTR_USER_FEATURES] = { .type = NLA_U32 },
1728};
1729
stephen hemminger48e48a72014-07-16 11:25:52 -07001730static const struct genl_ops dp_datapath_genl_ops[] = {
Jesse Grossccb13522011-10-25 19:26:31 -07001731 { .cmd = OVS_DP_CMD_NEW,
1732 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1733 .policy = datapath_policy,
1734 .doit = ovs_dp_cmd_new
1735 },
1736 { .cmd = OVS_DP_CMD_DEL,
1737 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1738 .policy = datapath_policy,
1739 .doit = ovs_dp_cmd_del
1740 },
1741 { .cmd = OVS_DP_CMD_GET,
1742 .flags = 0, /* OK for unprivileged users. */
1743 .policy = datapath_policy,
1744 .doit = ovs_dp_cmd_get,
1745 .dumpit = ovs_dp_cmd_dump
1746 },
1747 { .cmd = OVS_DP_CMD_SET,
1748 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1749 .policy = datapath_policy,
1750 .doit = ovs_dp_cmd_set,
1751 },
1752};
1753
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001754static struct genl_family dp_datapath_genl_family = {
Jesse Grossccb13522011-10-25 19:26:31 -07001755 .id = GENL_ID_GENERATE,
1756 .hdrsize = sizeof(struct ovs_header),
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001757 .name = OVS_DATAPATH_FAMILY,
1758 .version = OVS_DATAPATH_VERSION,
1759 .maxattr = OVS_DP_ATTR_MAX,
Pravin B Shelar3a4e0d62013-04-23 07:48:48 +00001760 .netnsok = true,
1761 .parallel_ops = true,
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001762 .ops = dp_datapath_genl_ops,
1763 .n_ops = ARRAY_SIZE(dp_datapath_genl_ops),
1764 .mcgrps = &ovs_dp_datapath_multicast_group,
1765 .n_mcgrps = 1,
Jesse Grossccb13522011-10-25 19:26:31 -07001766};
1767
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001768/* Called with ovs_mutex or RCU read lock. */
Jesse Grossccb13522011-10-25 19:26:31 -07001769static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001770 u32 portid, u32 seq, u32 flags, u8 cmd)
Jesse Grossccb13522011-10-25 19:26:31 -07001771{
1772 struct ovs_header *ovs_header;
1773 struct ovs_vport_stats vport_stats;
1774 int err;
1775
Eric W. Biederman15e47302012-09-07 20:12:54 +00001776 ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
Jesse Grossccb13522011-10-25 19:26:31 -07001777 flags, cmd);
1778 if (!ovs_header)
1779 return -EMSGSIZE;
1780
1781 ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1782
David S. Miller028d6a62012-03-29 23:20:48 -04001783 if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
1784 nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
Alex Wang5cd667b2014-07-17 15:14:13 -07001785 nla_put_string(skb, OVS_VPORT_ATTR_NAME,
1786 vport->ops->get_name(vport)))
David S. Miller028d6a62012-03-29 23:20:48 -04001787 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001788
1789 ovs_vport_get_stats(vport, &vport_stats);
David S. Miller028d6a62012-03-29 23:20:48 -04001790 if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
1791 &vport_stats))
1792 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001793
Alex Wang5cd667b2014-07-17 15:14:13 -07001794 if (ovs_vport_get_upcall_portids(vport, skb))
1795 goto nla_put_failure;
1796
Jesse Grossccb13522011-10-25 19:26:31 -07001797 err = ovs_vport_get_options(vport, skb);
1798 if (err == -EMSGSIZE)
1799 goto error;
1800
Johannes Berg053c0952015-01-16 22:09:00 +01001801 genlmsg_end(skb, ovs_header);
1802 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001803
1804nla_put_failure:
1805 err = -EMSGSIZE;
1806error:
1807 genlmsg_cancel(skb, ovs_header);
1808 return err;
1809}
1810
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001811static struct sk_buff *ovs_vport_cmd_alloc_info(void)
1812{
1813 return nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1814}
1815
1816/* Called with ovs_mutex, only via ovs_dp_notify_wq(). */
Eric W. Biederman15e47302012-09-07 20:12:54 +00001817struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001818 u32 seq, u8 cmd)
1819{
1820 struct sk_buff *skb;
1821 int retval;
1822
1823 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1824 if (!skb)
1825 return ERR_PTR(-ENOMEM);
1826
Eric W. Biederman15e47302012-09-07 20:12:54 +00001827 retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
Jesse Grossa9341512013-03-26 15:48:38 -07001828 BUG_ON(retval < 0);
1829
Jesse Grossccb13522011-10-25 19:26:31 -07001830 return skb;
1831}
1832
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001833/* Called with ovs_mutex or RCU read lock. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001834static struct vport *lookup_vport(struct net *net,
Thomas Graf12eb18f2014-11-06 06:58:52 -08001835 const struct ovs_header *ovs_header,
Jesse Grossccb13522011-10-25 19:26:31 -07001836 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1837{
1838 struct datapath *dp;
1839 struct vport *vport;
1840
1841 if (a[OVS_VPORT_ATTR_NAME]) {
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001842 vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
Jesse Grossccb13522011-10-25 19:26:31 -07001843 if (!vport)
1844 return ERR_PTR(-ENODEV);
Ben Pfaff651a68e2012-03-06 15:04:04 -08001845 if (ovs_header->dp_ifindex &&
1846 ovs_header->dp_ifindex != get_dpifindex(vport->dp))
1847 return ERR_PTR(-ENODEV);
Jesse Grossccb13522011-10-25 19:26:31 -07001848 return vport;
1849 } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
1850 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1851
1852 if (port_no >= DP_MAX_PORTS)
1853 return ERR_PTR(-EFBIG);
1854
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001855 dp = get_dp(net, ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001856 if (!dp)
1857 return ERR_PTR(-ENODEV);
1858
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001859 vport = ovs_vport_ovsl_rcu(dp, port_no);
Jesse Grossccb13522011-10-25 19:26:31 -07001860 if (!vport)
Jarno Rajahalme14408db2013-01-09 14:27:35 -08001861 return ERR_PTR(-ENODEV);
Jesse Grossccb13522011-10-25 19:26:31 -07001862 return vport;
1863 } else
1864 return ERR_PTR(-EINVAL);
1865}
1866
1867static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
1868{
1869 struct nlattr **a = info->attrs;
1870 struct ovs_header *ovs_header = info->userhdr;
1871 struct vport_parms parms;
1872 struct sk_buff *reply;
1873 struct vport *vport;
1874 struct datapath *dp;
1875 u32 port_no;
1876 int err;
1877
Jesse Grossccb13522011-10-25 19:26:31 -07001878 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
1879 !a[OVS_VPORT_ATTR_UPCALL_PID])
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001880 return -EINVAL;
1881
1882 port_no = a[OVS_VPORT_ATTR_PORT_NO]
1883 ? nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]) : 0;
1884 if (port_no >= DP_MAX_PORTS)
1885 return -EFBIG;
1886
1887 reply = ovs_vport_cmd_alloc_info();
1888 if (!reply)
1889 return -ENOMEM;
Jesse Grossccb13522011-10-25 19:26:31 -07001890
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001891 ovs_lock();
Thomas Graf62b9c8d2014-10-22 17:29:06 +02001892restart:
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001893 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001894 err = -ENODEV;
1895 if (!dp)
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001896 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001897
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001898 if (port_no) {
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001899 vport = ovs_vport_ovsl(dp, port_no);
Jesse Grossccb13522011-10-25 19:26:31 -07001900 err = -EBUSY;
1901 if (vport)
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001902 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001903 } else {
1904 for (port_no = 1; ; port_no++) {
1905 if (port_no >= DP_MAX_PORTS) {
1906 err = -EFBIG;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001907 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001908 }
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001909 vport = ovs_vport_ovsl(dp, port_no);
Jesse Grossccb13522011-10-25 19:26:31 -07001910 if (!vport)
1911 break;
1912 }
1913 }
1914
1915 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
1916 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1917 parms.options = a[OVS_VPORT_ATTR_OPTIONS];
1918 parms.dp = dp;
1919 parms.port_no = port_no;
Alex Wang5cd667b2014-07-17 15:14:13 -07001920 parms.upcall_portids = a[OVS_VPORT_ATTR_UPCALL_PID];
Jesse Grossccb13522011-10-25 19:26:31 -07001921
1922 vport = new_vport(&parms);
1923 err = PTR_ERR(vport);
Thomas Graf62b9c8d2014-10-22 17:29:06 +02001924 if (IS_ERR(vport)) {
1925 if (err == -EAGAIN)
1926 goto restart;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001927 goto exit_unlock_free;
Thomas Graf62b9c8d2014-10-22 17:29:06 +02001928 }
Jesse Grossccb13522011-10-25 19:26:31 -07001929
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001930 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1931 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1932 BUG_ON(err < 0);
1933 ovs_unlock();
Thomas Grafed6611852013-03-29 14:46:50 +01001934
Johannes Berg2a94fe42013-11-19 15:19:39 +01001935 ovs_notify(&dp_vport_genl_family, reply, info);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001936 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001937
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001938exit_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001939 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001940 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001941 return err;
1942}
1943
1944static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
1945{
1946 struct nlattr **a = info->attrs;
1947 struct sk_buff *reply;
1948 struct vport *vport;
1949 int err;
1950
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001951 reply = ovs_vport_cmd_alloc_info();
1952 if (!reply)
1953 return -ENOMEM;
1954
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001955 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001956 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
Jesse Grossccb13522011-10-25 19:26:31 -07001957 err = PTR_ERR(vport);
1958 if (IS_ERR(vport))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001959 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001960
Jesse Grossccb13522011-10-25 19:26:31 -07001961 if (a[OVS_VPORT_ATTR_TYPE] &&
Jesse Grossf44f3402013-05-13 08:15:26 -07001962 nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
Jesse Grossccb13522011-10-25 19:26:31 -07001963 err = -EINVAL;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001964 goto exit_unlock_free;
Jesse Grossa9341512013-03-26 15:48:38 -07001965 }
1966
Jesse Grossf44f3402013-05-13 08:15:26 -07001967 if (a[OVS_VPORT_ATTR_OPTIONS]) {
Jesse Grossccb13522011-10-25 19:26:31 -07001968 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
Jesse Grossf44f3402013-05-13 08:15:26 -07001969 if (err)
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001970 goto exit_unlock_free;
Jesse Grossf44f3402013-05-13 08:15:26 -07001971 }
Jesse Grossa9341512013-03-26 15:48:38 -07001972
Alex Wang5cd667b2014-07-17 15:14:13 -07001973
1974 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
1975 struct nlattr *ids = a[OVS_VPORT_ATTR_UPCALL_PID];
1976
1977 err = ovs_vport_set_upcall_portids(vport, ids);
1978 if (err)
1979 goto exit_unlock_free;
1980 }
Jesse Grossccb13522011-10-25 19:26:31 -07001981
Jesse Grossa9341512013-03-26 15:48:38 -07001982 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1983 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1984 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001985
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001986 ovs_unlock();
Johannes Berg2a94fe42013-11-19 15:19:39 +01001987 ovs_notify(&dp_vport_genl_family, reply, info);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001988 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001989
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001990exit_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001991 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001992 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001993 return err;
1994}
1995
1996static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
1997{
1998 struct nlattr **a = info->attrs;
1999 struct sk_buff *reply;
2000 struct vport *vport;
2001 int err;
2002
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07002003 reply = ovs_vport_cmd_alloc_info();
2004 if (!reply)
2005 return -ENOMEM;
2006
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002007 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002008 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
Jesse Grossccb13522011-10-25 19:26:31 -07002009 err = PTR_ERR(vport);
2010 if (IS_ERR(vport))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07002011 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07002012
2013 if (vport->port_no == OVSP_LOCAL) {
2014 err = -EINVAL;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07002015 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07002016 }
2017
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07002018 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
2019 info->snd_seq, 0, OVS_VPORT_CMD_DEL);
2020 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07002021 ovs_dp_detach_port(vport);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07002022 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07002023
Johannes Berg2a94fe42013-11-19 15:19:39 +01002024 ovs_notify(&dp_vport_genl_family, reply, info);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07002025 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -07002026
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07002027exit_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002028 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07002029 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07002030 return err;
2031}
2032
2033static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
2034{
2035 struct nlattr **a = info->attrs;
2036 struct ovs_header *ovs_header = info->userhdr;
2037 struct sk_buff *reply;
2038 struct vport *vport;
2039 int err;
2040
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07002041 reply = ovs_vport_cmd_alloc_info();
2042 if (!reply)
2043 return -ENOMEM;
2044
Jesse Grossccb13522011-10-25 19:26:31 -07002045 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002046 vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
Jesse Grossccb13522011-10-25 19:26:31 -07002047 err = PTR_ERR(vport);
2048 if (IS_ERR(vport))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07002049 goto exit_unlock_free;
2050 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
2051 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
2052 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07002053 rcu_read_unlock();
2054
2055 return genlmsg_reply(reply, info);
2056
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07002057exit_unlock_free:
Jesse Grossccb13522011-10-25 19:26:31 -07002058 rcu_read_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07002059 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07002060 return err;
2061}
2062
2063static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
2064{
2065 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
2066 struct datapath *dp;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07002067 int bucket = cb->args[0], skip = cb->args[1];
2068 int i, j = 0;
Jesse Grossccb13522011-10-25 19:26:31 -07002069
Jesse Grossccb13522011-10-25 19:26:31 -07002070 rcu_read_lock();
Andy Zhoucc3a5ae2014-09-08 13:14:22 -07002071 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
Jarno Rajahalme42ee19e2014-02-15 17:42:29 -08002072 if (!dp) {
2073 rcu_read_unlock();
2074 return -ENODEV;
2075 }
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07002076 for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
Jesse Grossccb13522011-10-25 19:26:31 -07002077 struct vport *vport;
2078
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07002079 j = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -08002080 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07002081 if (j >= skip &&
2082 ovs_vport_cmd_fill_info(vport, skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00002083 NETLINK_CB(cb->skb).portid,
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07002084 cb->nlh->nlmsg_seq,
2085 NLM_F_MULTI,
2086 OVS_VPORT_CMD_NEW) < 0)
2087 goto out;
Jesse Grossccb13522011-10-25 19:26:31 -07002088
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07002089 j++;
2090 }
2091 skip = 0;
Jesse Grossccb13522011-10-25 19:26:31 -07002092 }
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07002093out:
Jesse Grossccb13522011-10-25 19:26:31 -07002094 rcu_read_unlock();
2095
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07002096 cb->args[0] = i;
2097 cb->args[1] = j;
Jesse Grossccb13522011-10-25 19:26:31 -07002098
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07002099 return skb->len;
Jesse Grossccb13522011-10-25 19:26:31 -07002100}
2101
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002102static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
2103 [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
2104 [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
2105 [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
2106 [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
2107 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
2108 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
2109};
2110
stephen hemminger48e48a72014-07-16 11:25:52 -07002111static const struct genl_ops dp_vport_genl_ops[] = {
Jesse Grossccb13522011-10-25 19:26:31 -07002112 { .cmd = OVS_VPORT_CMD_NEW,
2113 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2114 .policy = vport_policy,
2115 .doit = ovs_vport_cmd_new
2116 },
2117 { .cmd = OVS_VPORT_CMD_DEL,
2118 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2119 .policy = vport_policy,
2120 .doit = ovs_vport_cmd_del
2121 },
2122 { .cmd = OVS_VPORT_CMD_GET,
2123 .flags = 0, /* OK for unprivileged users. */
2124 .policy = vport_policy,
2125 .doit = ovs_vport_cmd_get,
2126 .dumpit = ovs_vport_cmd_dump
2127 },
2128 { .cmd = OVS_VPORT_CMD_SET,
2129 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2130 .policy = vport_policy,
2131 .doit = ovs_vport_cmd_set,
2132 },
2133};
2134
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002135struct genl_family dp_vport_genl_family = {
2136 .id = GENL_ID_GENERATE,
2137 .hdrsize = sizeof(struct ovs_header),
2138 .name = OVS_VPORT_FAMILY,
2139 .version = OVS_VPORT_VERSION,
2140 .maxattr = OVS_VPORT_ATTR_MAX,
2141 .netnsok = true,
2142 .parallel_ops = true,
2143 .ops = dp_vport_genl_ops,
2144 .n_ops = ARRAY_SIZE(dp_vport_genl_ops),
2145 .mcgrps = &ovs_dp_vport_multicast_group,
2146 .n_mcgrps = 1,
Jesse Grossccb13522011-10-25 19:26:31 -07002147};
2148
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002149static struct genl_family * const dp_genl_families[] = {
2150 &dp_datapath_genl_family,
2151 &dp_vport_genl_family,
2152 &dp_flow_genl_family,
2153 &dp_packet_genl_family,
Jesse Grossccb13522011-10-25 19:26:31 -07002154};
2155
2156static void dp_unregister_genl(int n_families)
2157{
2158 int i;
2159
2160 for (i = 0; i < n_families; i++)
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002161 genl_unregister_family(dp_genl_families[i]);
Jesse Grossccb13522011-10-25 19:26:31 -07002162}
2163
2164static int dp_register_genl(void)
2165{
Jesse Grossccb13522011-10-25 19:26:31 -07002166 int err;
2167 int i;
2168
Jesse Grossccb13522011-10-25 19:26:31 -07002169 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
Jesse Grossccb13522011-10-25 19:26:31 -07002170
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002171 err = genl_register_family(dp_genl_families[i]);
Jesse Grossccb13522011-10-25 19:26:31 -07002172 if (err)
2173 goto error;
Jesse Grossccb13522011-10-25 19:26:31 -07002174 }
2175
2176 return 0;
2177
2178error:
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002179 dp_unregister_genl(i);
Jesse Grossccb13522011-10-25 19:26:31 -07002180 return err;
2181}
2182
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002183static int __net_init ovs_init_net(struct net *net)
2184{
2185 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2186
2187 INIT_LIST_HEAD(&ovs_net->dps);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002188 INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002189 return 0;
2190}
2191
Pravin B Shelar7b4577a2015-02-17 11:23:10 -08002192static void __net_exit list_vports_from_net(struct net *net, struct net *dnet,
2193 struct list_head *head)
2194{
2195 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2196 struct datapath *dp;
2197
2198 list_for_each_entry(dp, &ovs_net->dps, list_node) {
2199 int i;
2200
2201 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
2202 struct vport *vport;
2203
2204 hlist_for_each_entry(vport, &dp->ports[i], dp_hash_node) {
2205 struct netdev_vport *netdev_vport;
2206
2207 if (vport->ops->type != OVS_VPORT_TYPE_INTERNAL)
2208 continue;
2209
2210 netdev_vport = netdev_vport_priv(vport);
2211 if (dev_net(netdev_vport->dev) == dnet)
2212 list_add(&vport->detach_list, head);
2213 }
2214 }
2215 }
2216}
2217
2218static void __net_exit ovs_exit_net(struct net *dnet)
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002219{
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002220 struct datapath *dp, *dp_next;
Pravin B Shelar7b4577a2015-02-17 11:23:10 -08002221 struct ovs_net *ovs_net = net_generic(dnet, ovs_net_id);
2222 struct vport *vport, *vport_next;
2223 struct net *net;
2224 LIST_HEAD(head);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002225
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002226 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002227 list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
2228 __dp_destroy(dp);
Pravin B Shelar7b4577a2015-02-17 11:23:10 -08002229
2230 rtnl_lock();
2231 for_each_net(net)
2232 list_vports_from_net(net, dnet, &head);
2233 rtnl_unlock();
2234
2235 /* Detach all vports from given namespace. */
2236 list_for_each_entry_safe(vport, vport_next, &head, detach_list) {
2237 list_del(&vport->detach_list);
2238 ovs_dp_detach_port(vport);
2239 }
2240
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002241 ovs_unlock();
2242
2243 cancel_work_sync(&ovs_net->dp_notify_work);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002244}
2245
2246static struct pernet_operations ovs_net_ops = {
2247 .init = ovs_init_net,
2248 .exit = ovs_exit_net,
2249 .id = &ovs_net_id,
2250 .size = sizeof(struct ovs_net),
2251};
2252
Jesse Grossccb13522011-10-25 19:26:31 -07002253static int __init dp_init(void)
2254{
Jesse Grossccb13522011-10-25 19:26:31 -07002255 int err;
2256
YOSHIFUJI Hideaki / 吉藤英明3523b292013-01-09 07:19:55 +00002257 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
Jesse Grossccb13522011-10-25 19:26:31 -07002258
2259 pr_info("Open vSwitch switching datapath\n");
2260
Andy Zhou971427f32014-09-15 19:37:25 -07002261 err = action_fifos_init();
Jesse Grossccb13522011-10-25 19:26:31 -07002262 if (err)
2263 goto error;
2264
Andy Zhou971427f32014-09-15 19:37:25 -07002265 err = ovs_internal_dev_rtnl_link_register();
2266 if (err)
2267 goto error_action_fifos_exit;
2268
Jiri Pirko5b9e7e12014-06-26 09:58:26 +02002269 err = ovs_flow_init();
2270 if (err)
2271 goto error_unreg_rtnl_link;
2272
Jesse Grossccb13522011-10-25 19:26:31 -07002273 err = ovs_vport_init();
2274 if (err)
2275 goto error_flow_exit;
2276
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002277 err = register_pernet_device(&ovs_net_ops);
Jesse Grossccb13522011-10-25 19:26:31 -07002278 if (err)
2279 goto error_vport_exit;
2280
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002281 err = register_netdevice_notifier(&ovs_dp_device_notifier);
2282 if (err)
2283 goto error_netns_exit;
2284
Thomas Graf62b9c8d2014-10-22 17:29:06 +02002285 err = ovs_netdev_init();
2286 if (err)
2287 goto error_unreg_notifier;
2288
Jesse Grossccb13522011-10-25 19:26:31 -07002289 err = dp_register_genl();
2290 if (err < 0)
Thomas Graf62b9c8d2014-10-22 17:29:06 +02002291 goto error_unreg_netdev;
Jesse Grossccb13522011-10-25 19:26:31 -07002292
Jesse Grossccb13522011-10-25 19:26:31 -07002293 return 0;
2294
Thomas Graf62b9c8d2014-10-22 17:29:06 +02002295error_unreg_netdev:
2296 ovs_netdev_exit();
Jesse Grossccb13522011-10-25 19:26:31 -07002297error_unreg_notifier:
2298 unregister_netdevice_notifier(&ovs_dp_device_notifier);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002299error_netns_exit:
2300 unregister_pernet_device(&ovs_net_ops);
Jesse Grossccb13522011-10-25 19:26:31 -07002301error_vport_exit:
2302 ovs_vport_exit();
2303error_flow_exit:
2304 ovs_flow_exit();
Jiri Pirko5b9e7e12014-06-26 09:58:26 +02002305error_unreg_rtnl_link:
2306 ovs_internal_dev_rtnl_link_unregister();
Andy Zhou971427f32014-09-15 19:37:25 -07002307error_action_fifos_exit:
2308 action_fifos_exit();
Jesse Grossccb13522011-10-25 19:26:31 -07002309error:
2310 return err;
2311}
2312
2313static void dp_cleanup(void)
2314{
Jesse Grossccb13522011-10-25 19:26:31 -07002315 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
Thomas Graf62b9c8d2014-10-22 17:29:06 +02002316 ovs_netdev_exit();
Jesse Grossccb13522011-10-25 19:26:31 -07002317 unregister_netdevice_notifier(&ovs_dp_device_notifier);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002318 unregister_pernet_device(&ovs_net_ops);
2319 rcu_barrier();
Jesse Grossccb13522011-10-25 19:26:31 -07002320 ovs_vport_exit();
2321 ovs_flow_exit();
Jiri Pirko5b9e7e12014-06-26 09:58:26 +02002322 ovs_internal_dev_rtnl_link_unregister();
Andy Zhou971427f32014-09-15 19:37:25 -07002323 action_fifos_exit();
Jesse Grossccb13522011-10-25 19:26:31 -07002324}
2325
2326module_init(dp_init);
2327module_exit(dp_cleanup);
2328
2329MODULE_DESCRIPTION("Open vSwitch switching datapath");
2330MODULE_LICENSE("GPL");