blob: 5bae7243c5777e38df7be95454b8164724c769cf [file] [log] [blame]
Jesse Grossccb13522011-10-25 19:26:31 -07001/*
Ben Pfaffad552002014-05-06 16:48:38 -07002 * Copyright (c) 2007-2014 Nicira, Inc.
Jesse Grossccb13522011-10-25 19:26:31 -07003 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of version 2 of the GNU General Public
6 * License as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 * 02110-1301, USA
17 */
18
19#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20
21#include <linux/init.h>
22#include <linux/module.h>
23#include <linux/if_arp.h>
24#include <linux/if_vlan.h>
25#include <linux/in.h>
26#include <linux/ip.h>
27#include <linux/jhash.h>
28#include <linux/delay.h>
29#include <linux/time.h>
30#include <linux/etherdevice.h>
31#include <linux/genetlink.h>
32#include <linux/kernel.h>
33#include <linux/kthread.h>
34#include <linux/mutex.h>
35#include <linux/percpu.h>
36#include <linux/rcupdate.h>
37#include <linux/tcp.h>
38#include <linux/udp.h>
Jesse Grossccb13522011-10-25 19:26:31 -070039#include <linux/ethtool.h>
40#include <linux/wait.h>
Jesse Grossccb13522011-10-25 19:26:31 -070041#include <asm/div64.h>
42#include <linux/highmem.h>
43#include <linux/netfilter_bridge.h>
44#include <linux/netfilter_ipv4.h>
45#include <linux/inetdevice.h>
46#include <linux/list.h>
47#include <linux/openvswitch.h>
48#include <linux/rculist.h>
49#include <linux/dmi.h>
Jesse Grossccb13522011-10-25 19:26:31 -070050#include <net/genetlink.h>
Pravin B Shelar46df7b82012-02-22 19:58:59 -080051#include <net/net_namespace.h>
52#include <net/netns/generic.h>
Jesse Grossccb13522011-10-25 19:26:31 -070053
54#include "datapath.h"
55#include "flow.h"
Andy Zhoue80857c2014-01-21 09:31:04 -080056#include "flow_table.h"
Pravin B Shelare6445712013-10-03 18:16:47 -070057#include "flow_netlink.h"
Jesse Grossccb13522011-10-25 19:26:31 -070058#include "vport-internal_dev.h"
Thomas Grafcff63a52013-04-29 13:06:41 +000059#include "vport-netdev.h"
Jesse Grossccb13522011-10-25 19:26:31 -070060
Pravin B Shelar8e4e1712013-04-15 13:23:03 -070061int ovs_net_id __read_mostly;
Pravin B Shelar9ba559d2014-11-06 06:44:27 -080062EXPORT_SYMBOL_GPL(ovs_net_id);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -070063
Pravin B Shelar0c200ef2014-05-06 16:44:50 -070064static struct genl_family dp_packet_genl_family;
65static struct genl_family dp_flow_genl_family;
66static struct genl_family dp_datapath_genl_family;
67
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 Grafed661182013-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 Grafed661182013-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 Shelar9ba559d2014-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 Shelar46df7b82012-02-22 19:58:59 -0800206 release_net(ovs_dp_get_net(dp));
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700207 kfree(dp->ports);
Jesse Grossccb13522011-10-25 19:26:31 -0700208 kfree(dp);
209}
210
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700211static struct hlist_head *vport_hash_bucket(const struct datapath *dp,
212 u16 port_no)
213{
214 return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)];
215}
216
Jarno Rajahalmebb6f9a72014-05-05 11:32:17 -0700217/* Called with ovs_mutex or RCU read lock. */
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700218struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no)
219{
220 struct vport *vport;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700221 struct hlist_head *head;
222
223 head = vport_hash_bucket(dp, port_no);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800224 hlist_for_each_entry_rcu(vport, head, dp_hash_node) {
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700225 if (vport->port_no == port_no)
226 return vport;
227 }
228 return NULL;
229}
230
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700231/* Called with ovs_mutex. */
Jesse Grossccb13522011-10-25 19:26:31 -0700232static struct vport *new_vport(const struct vport_parms *parms)
233{
234 struct vport *vport;
235
236 vport = ovs_vport_add(parms);
237 if (!IS_ERR(vport)) {
238 struct datapath *dp = parms->dp;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700239 struct hlist_head *head = vport_hash_bucket(dp, vport->port_no);
Jesse Grossccb13522011-10-25 19:26:31 -0700240
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700241 hlist_add_head_rcu(&vport->dp_hash_node, head);
Jesse Grossccb13522011-10-25 19:26:31 -0700242 }
Jesse Grossccb13522011-10-25 19:26:31 -0700243 return vport;
244}
245
Jesse Grossccb13522011-10-25 19:26:31 -0700246void ovs_dp_detach_port(struct vport *p)
247{
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700248 ASSERT_OVSL();
Jesse Grossccb13522011-10-25 19:26:31 -0700249
250 /* First drop references to device. */
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700251 hlist_del_rcu(&p->dp_hash_node);
Jesse Grossccb13522011-10-25 19:26:31 -0700252
253 /* Then destroy it. */
254 ovs_vport_del(p);
255}
256
257/* Must be called with rcu_read_lock. */
Pravin B Shelar8c8b1b82014-09-15 19:28:44 -0700258void ovs_dp_process_packet(struct sk_buff *skb, struct sw_flow_key *key)
Jesse Grossccb13522011-10-25 19:26:31 -0700259{
Pravin B Shelar83c8df22014-09-15 19:20:31 -0700260 const struct vport *p = OVS_CB(skb)->input_vport;
Jesse Grossccb13522011-10-25 19:26:31 -0700261 struct datapath *dp = p->dp;
262 struct sw_flow *flow;
Lorand Jakabd98612b2014-10-06 05:45:32 -0700263 struct sw_flow_actions *sf_acts;
Jesse Grossccb13522011-10-25 19:26:31 -0700264 struct dp_stats_percpu *stats;
Jesse Grossccb13522011-10-25 19:26:31 -0700265 u64 *stats_counter;
Andy Zhou1bd71162013-10-22 10:42:46 -0700266 u32 n_mask_hit;
Jesse Grossccb13522011-10-25 19:26:31 -0700267
Shan Wei404f2f12012-11-13 09:52:25 +0800268 stats = this_cpu_ptr(dp->stats_percpu);
Jesse Grossccb13522011-10-25 19:26:31 -0700269
Jesse Grossccb13522011-10-25 19:26:31 -0700270 /* Look up flow. */
Pravin B Shelar8c8b1b82014-09-15 19:28:44 -0700271 flow = ovs_flow_tbl_lookup_stats(&dp->table, key, &n_mask_hit);
Jesse Grossccb13522011-10-25 19:26:31 -0700272 if (unlikely(!flow)) {
273 struct dp_upcall_info upcall;
Pravin B Shelar8c8b1b82014-09-15 19:28:44 -0700274 int error;
Jesse Grossccb13522011-10-25 19:26:31 -0700275
276 upcall.cmd = OVS_PACKET_CMD_MISS;
Jesse Grossccb13522011-10-25 19:26:31 -0700277 upcall.userdata = NULL;
Alex Wang5cd667b2014-07-17 15:14:13 -0700278 upcall.portid = ovs_vport_find_upcall_portid(p, skb);
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800279 upcall.egress_tun_info = NULL;
Pravin B Shelare8eedb82014-11-06 06:57:27 -0800280 error = ovs_dp_upcall(dp, skb, key, &upcall);
Li RongQingc5eba0b2014-09-03 17:43:45 +0800281 if (unlikely(error))
282 kfree_skb(skb);
283 else
284 consume_skb(skb);
Jesse Grossccb13522011-10-25 19:26:31 -0700285 stats_counter = &stats->n_missed;
286 goto out;
287 }
288
Lorand Jakabd98612b2014-10-06 05:45:32 -0700289 ovs_flow_stats_update(flow, key->tp.flags, skb);
290 sf_acts = rcu_dereference(flow->sf_acts);
291 ovs_execute_actions(dp, skb, sf_acts, key);
Jesse Grossccb13522011-10-25 19:26:31 -0700292
Pravin B Shelare298e502013-10-29 17:22:21 -0700293 stats_counter = &stats->n_hit;
Jesse Grossccb13522011-10-25 19:26:31 -0700294
295out:
296 /* Update datapath statistics. */
WANG Congdf9d9fd2014-02-14 15:10:46 -0800297 u64_stats_update_begin(&stats->syncp);
Jesse Grossccb13522011-10-25 19:26:31 -0700298 (*stats_counter)++;
Andy Zhou1bd71162013-10-22 10:42:46 -0700299 stats->n_mask_hit += n_mask_hit;
WANG Congdf9d9fd2014-02-14 15:10:46 -0800300 u64_stats_update_end(&stats->syncp);
Jesse Grossccb13522011-10-25 19:26:31 -0700301}
302
Jesse Grossccb13522011-10-25 19:26:31 -0700303int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
Pravin B Shelare8eedb82014-11-06 06:57:27 -0800304 const struct sw_flow_key *key,
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800305 const struct dp_upcall_info *upcall_info)
Jesse Grossccb13522011-10-25 19:26:31 -0700306{
307 struct dp_stats_percpu *stats;
Jesse Grossccb13522011-10-25 19:26:31 -0700308 int err;
309
Eric W. Biederman15e47302012-09-07 20:12:54 +0000310 if (upcall_info->portid == 0) {
Jesse Grossccb13522011-10-25 19:26:31 -0700311 err = -ENOTCONN;
312 goto err;
313 }
314
Jesse Grossccb13522011-10-25 19:26:31 -0700315 if (!skb_is_gso(skb))
Pravin B Shelare8eedb82014-11-06 06:57:27 -0800316 err = queue_userspace_packet(dp, skb, key, upcall_info);
Jesse Grossccb13522011-10-25 19:26:31 -0700317 else
Pravin B Shelare8eedb82014-11-06 06:57:27 -0800318 err = queue_gso_packets(dp, skb, key, upcall_info);
Jesse Grossccb13522011-10-25 19:26:31 -0700319 if (err)
320 goto err;
321
322 return 0;
323
324err:
Shan Wei404f2f12012-11-13 09:52:25 +0800325 stats = this_cpu_ptr(dp->stats_percpu);
Jesse Grossccb13522011-10-25 19:26:31 -0700326
WANG Congdf9d9fd2014-02-14 15:10:46 -0800327 u64_stats_update_begin(&stats->syncp);
Jesse Grossccb13522011-10-25 19:26:31 -0700328 stats->n_lost++;
WANG Congdf9d9fd2014-02-14 15:10:46 -0800329 u64_stats_update_end(&stats->syncp);
Jesse Grossccb13522011-10-25 19:26:31 -0700330
331 return err;
332}
333
Thomas Graf8055a892013-12-13 15:22:20 +0100334static int queue_gso_packets(struct datapath *dp, struct sk_buff *skb,
Pravin B Shelare8eedb82014-11-06 06:57:27 -0800335 const struct sw_flow_key *key,
Jesse Grossccb13522011-10-25 19:26:31 -0700336 const struct dp_upcall_info *upcall_info)
337{
Ben Pfaffa1b5d0d2012-07-20 14:47:54 -0700338 unsigned short gso_type = skb_shinfo(skb)->gso_type;
Jesse Grossccb13522011-10-25 19:26:31 -0700339 struct sw_flow_key later_key;
340 struct sk_buff *segs, *nskb;
Pravin B Shelare8eedb82014-11-06 06:57:27 -0800341 struct ovs_skb_cb ovs_cb;
Jesse Grossccb13522011-10-25 19:26:31 -0700342 int err;
343
Pravin B Shelare8eedb82014-11-06 06:57:27 -0800344 ovs_cb = *OVS_CB(skb);
Thomas Graf09c5e602013-12-13 15:22:22 +0100345 segs = __skb_gso_segment(skb, NETIF_F_SG, false);
Pravin B Shelare8eedb82014-11-06 06:57:27 -0800346 *OVS_CB(skb) = ovs_cb;
Pravin B Shelar92e5dfc2012-07-20 14:46:29 -0700347 if (IS_ERR(segs))
348 return PTR_ERR(segs);
Florian Westphal330966e2014-10-20 13:49:17 +0200349 if (segs == NULL)
350 return -EINVAL;
Jesse Grossccb13522011-10-25 19:26:31 -0700351
Pravin B Shelare8eedb82014-11-06 06:57:27 -0800352 if (gso_type & SKB_GSO_UDP) {
353 /* The initial flow key extracted by ovs_flow_key_extract()
354 * in this case is for a first fragment, so we need to
355 * properly mark later fragments.
356 */
357 later_key = *key;
358 later_key.ip.frag = OVS_FRAG_TYPE_LATER;
359 }
360
Jesse Grossccb13522011-10-25 19:26:31 -0700361 /* Queue all of the segments. */
362 skb = segs;
363 do {
Pravin B Shelare8eedb82014-11-06 06:57:27 -0800364 *OVS_CB(skb) = ovs_cb;
365 if (gso_type & SKB_GSO_UDP && skb != segs)
366 key = &later_key;
367
368 err = queue_userspace_packet(dp, skb, key, upcall_info);
Jesse Grossccb13522011-10-25 19:26:31 -0700369 if (err)
370 break;
371
Jesse Grossccb13522011-10-25 19:26:31 -0700372 } while ((skb = skb->next));
373
374 /* Free all of the segments. */
375 skb = segs;
376 do {
377 nskb = skb->next;
378 if (err)
379 kfree_skb(skb);
380 else
381 consume_skb(skb);
382 } while ((skb = nskb));
383 return err;
384}
385
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800386static size_t upcall_msg_size(const struct dp_upcall_info *upcall_info,
Thomas Grafbda56f12013-12-13 15:22:21 +0100387 unsigned int hdrlen)
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100388{
389 size_t size = NLMSG_ALIGN(sizeof(struct ovs_header))
Thomas Grafbda56f12013-12-13 15:22:21 +0100390 + nla_total_size(hdrlen) /* OVS_PACKET_ATTR_PACKET */
Joe Stringer41af73e2014-10-18 16:14:14 -0700391 + nla_total_size(ovs_key_attr_size()); /* OVS_PACKET_ATTR_KEY */
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100392
393 /* OVS_PACKET_ATTR_USERDATA */
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800394 if (upcall_info->userdata)
395 size += NLA_ALIGN(upcall_info->userdata->nla_len);
396
397 /* OVS_PACKET_ATTR_EGRESS_TUN_KEY */
398 if (upcall_info->egress_tun_info)
399 size += nla_total_size(ovs_tun_key_attr_size());
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100400
401 return size;
402}
403
Thomas Graf8055a892013-12-13 15:22:20 +0100404static int queue_userspace_packet(struct datapath *dp, struct sk_buff *skb,
Pravin B Shelare8eedb82014-11-06 06:57:27 -0800405 const struct sw_flow_key *key,
Jesse Grossccb13522011-10-25 19:26:31 -0700406 const struct dp_upcall_info *upcall_info)
407{
408 struct ovs_header *upcall;
409 struct sk_buff *nskb = NULL;
Li RongQing4ee45ea2014-09-02 20:52:28 +0800410 struct sk_buff *user_skb = NULL; /* to be queued to userspace */
Jesse Grossccb13522011-10-25 19:26:31 -0700411 struct nlattr *nla;
Thomas Graf795449d2013-11-30 13:21:32 +0100412 struct genl_info info = {
Thomas Graf8055a892013-12-13 15:22:20 +0100413 .dst_sk = ovs_dp_get_net(dp)->genl_sock,
Thomas Graf795449d2013-11-30 13:21:32 +0100414 .snd_portid = upcall_info->portid,
415 };
416 size_t len;
Thomas Grafbda56f12013-12-13 15:22:21 +0100417 unsigned int hlen;
Thomas Graf8055a892013-12-13 15:22:20 +0100418 int err, dp_ifindex;
419
420 dp_ifindex = get_dpifindex(dp);
421 if (!dp_ifindex)
422 return -ENODEV;
Jesse Grossccb13522011-10-25 19:26:31 -0700423
Jiri Pirkodf8a39d2015-01-13 17:13:44 +0100424 if (skb_vlan_tag_present(skb)) {
Jesse Grossccb13522011-10-25 19:26:31 -0700425 nskb = skb_clone(skb, GFP_ATOMIC);
426 if (!nskb)
427 return -ENOMEM;
428
Jiri Pirko59682502014-11-19 14:04:59 +0100429 nskb = __vlan_hwaccel_push_inside(nskb);
Dan Carpenter8aa51d62012-05-13 08:44:18 +0000430 if (!nskb)
Jesse Grossccb13522011-10-25 19:26:31 -0700431 return -ENOMEM;
432
Jesse Grossccb13522011-10-25 19:26:31 -0700433 skb = nskb;
434 }
435
436 if (nla_attr_size(skb->len) > USHRT_MAX) {
437 err = -EFBIG;
438 goto out;
439 }
440
Thomas Grafbda56f12013-12-13 15:22:21 +0100441 /* Complete checksum if needed */
442 if (skb->ip_summed == CHECKSUM_PARTIAL &&
443 (err = skb_checksum_help(skb)))
444 goto out;
445
446 /* Older versions of OVS user space enforce alignment of the last
447 * Netlink attribute to NLA_ALIGNTO which would require extensive
448 * padding logic. Only perform zerocopy if padding is not required.
449 */
450 if (dp->user_features & OVS_DP_F_UNALIGNED)
451 hlen = skb_zerocopy_headlen(skb);
452 else
453 hlen = skb->len;
454
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800455 len = upcall_msg_size(upcall_info, hlen);
Thomas Graf795449d2013-11-30 13:21:32 +0100456 user_skb = genlmsg_new_unicast(len, &info, GFP_ATOMIC);
Jesse Grossccb13522011-10-25 19:26:31 -0700457 if (!user_skb) {
458 err = -ENOMEM;
459 goto out;
460 }
461
462 upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
463 0, upcall_info->cmd);
464 upcall->dp_ifindex = dp_ifindex;
465
Joe Stringer5b4237b2015-01-21 16:42:48 -0800466 err = ovs_nla_put_key(key, key, OVS_PACKET_ATTR_KEY, false, user_skb);
Andy Zhouf53e3832014-07-17 15:17:44 -0700467 BUG_ON(err);
Jesse Grossccb13522011-10-25 19:26:31 -0700468
469 if (upcall_info->userdata)
Ben Pfaff44901082013-02-15 17:29:22 -0800470 __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA,
471 nla_len(upcall_info->userdata),
472 nla_data(upcall_info->userdata));
Jesse Grossccb13522011-10-25 19:26:31 -0700473
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800474 if (upcall_info->egress_tun_info) {
475 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_EGRESS_TUN_KEY);
476 err = ovs_nla_put_egress_tunnel_key(user_skb,
477 upcall_info->egress_tun_info);
478 BUG_ON(err);
479 nla_nest_end(user_skb, nla);
480 }
481
Thomas Grafbda56f12013-12-13 15:22:21 +0100482 /* Only reserve room for attribute header, packet data is added
483 * in skb_zerocopy() */
484 if (!(nla = nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, 0))) {
485 err = -ENOBUFS;
486 goto out;
487 }
488 nla->nla_len = nla_attr_size(skb->len);
Jesse Grossccb13522011-10-25 19:26:31 -0700489
Zoltan Kiss36d5fe62014-03-26 22:37:45 +0000490 err = skb_zerocopy(user_skb, skb, skb->len, hlen);
491 if (err)
492 goto out;
Jesse Grossccb13522011-10-25 19:26:31 -0700493
Thomas Grafaea0bb42014-01-14 16:27:49 +0000494 /* Pad OVS_PACKET_ATTR_PACKET if linear copy was performed */
495 if (!(dp->user_features & OVS_DP_F_UNALIGNED)) {
496 size_t plen = NLA_ALIGN(user_skb->len) - user_skb->len;
497
498 if (plen > 0)
499 memset(skb_put(user_skb, plen), 0, plen);
500 }
501
Thomas Grafbda56f12013-12-13 15:22:21 +0100502 ((struct nlmsghdr *) user_skb->data)->nlmsg_len = user_skb->len;
503
Thomas Graf8055a892013-12-13 15:22:20 +0100504 err = genlmsg_unicast(ovs_dp_get_net(dp), user_skb, upcall_info->portid);
Li RongQing4ee45ea2014-09-02 20:52:28 +0800505 user_skb = NULL;
Jesse Grossccb13522011-10-25 19:26:31 -0700506out:
Zoltan Kiss36d5fe62014-03-26 22:37:45 +0000507 if (err)
508 skb_tx_error(skb);
Li RongQing4ee45ea2014-09-02 20:52:28 +0800509 kfree_skb(user_skb);
Jesse Grossccb13522011-10-25 19:26:31 -0700510 kfree_skb(nskb);
511 return err;
512}
513
Jesse Grossccb13522011-10-25 19:26:31 -0700514static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
515{
516 struct ovs_header *ovs_header = info->userhdr;
517 struct nlattr **a = info->attrs;
518 struct sw_flow_actions *acts;
519 struct sk_buff *packet;
520 struct sw_flow *flow;
Lorand Jakabd98612b2014-10-06 05:45:32 -0700521 struct sw_flow_actions *sf_acts;
Jesse Grossccb13522011-10-25 19:26:31 -0700522 struct datapath *dp;
523 struct ethhdr *eth;
Pravin B Shelar83c8df22014-09-15 19:20:31 -0700524 struct vport *input_vport;
Jesse Grossccb13522011-10-25 19:26:31 -0700525 int len;
526 int err;
Thomas Graf1ba39802015-01-14 13:56:19 +0000527 bool log = !a[OVS_PACKET_ATTR_PROBE];
Jesse Grossccb13522011-10-25 19:26:31 -0700528
529 err = -EINVAL;
530 if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
Thomas Grafdded45fc2013-03-29 14:46:47 +0100531 !a[OVS_PACKET_ATTR_ACTIONS])
Jesse Grossccb13522011-10-25 19:26:31 -0700532 goto err;
533
534 len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
535 packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
536 err = -ENOMEM;
537 if (!packet)
538 goto err;
539 skb_reserve(packet, NET_IP_ALIGN);
540
Thomas Graf32686a92013-03-29 14:46:48 +0100541 nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
Jesse Grossccb13522011-10-25 19:26:31 -0700542
543 skb_reset_mac_header(packet);
544 eth = eth_hdr(packet);
545
546 /* Normally, setting the skb 'protocol' field would be handled by a
547 * call to eth_type_trans(), but it assumes there's a sending
548 * device, which we may not have. */
Simon Hormane5c5d222013-03-28 13:38:25 +0900549 if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN)
Jesse Grossccb13522011-10-25 19:26:31 -0700550 packet->protocol = eth->h_proto;
551 else
552 packet->protocol = htons(ETH_P_802_2);
553
554 /* Build an sw_flow for sending this packet. */
Jarno Rajahalme23dabf82014-03-27 12:35:23 -0700555 flow = ovs_flow_alloc();
Jesse Grossccb13522011-10-25 19:26:31 -0700556 err = PTR_ERR(flow);
557 if (IS_ERR(flow))
558 goto err_kfree_skb;
559
Pravin B Shelar83c8df22014-09-15 19:20:31 -0700560 err = ovs_flow_key_extract_userspace(a[OVS_PACKET_ATTR_KEY], packet,
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800561 &flow->key, log);
Jesse Grossccb13522011-10-25 19:26:31 -0700562 if (err)
563 goto err_flow_free;
564
Pravin B Shelare6445712013-10-03 18:16:47 -0700565 err = ovs_nla_copy_actions(a[OVS_PACKET_ATTR_ACTIONS],
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800566 &flow->key, &acts, log);
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700567 if (err)
568 goto err_flow_free;
Jesse Grossccb13522011-10-25 19:26:31 -0700569
Jesse Grossf5796682014-10-03 15:35:33 -0700570 rcu_assign_pointer(flow->sf_acts, acts);
Jesse Grossf5796682014-10-03 15:35:33 -0700571 OVS_CB(packet)->egress_tun_info = NULL;
Jesse Grossccb13522011-10-25 19:26:31 -0700572 packet->priority = flow->key.phy.priority;
Ansis Atteka39c7caeb2012-11-26 11:24:11 -0800573 packet->mark = flow->key.phy.skb_mark;
Jesse Grossccb13522011-10-25 19:26:31 -0700574
575 rcu_read_lock();
Andy Zhoucc3a5ae2014-09-08 13:14:22 -0700576 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -0700577 err = -ENODEV;
578 if (!dp)
579 goto err_unlock;
580
Pravin B Shelar83c8df22014-09-15 19:20:31 -0700581 input_vport = ovs_vport_rcu(dp, flow->key.phy.in_port);
582 if (!input_vport)
583 input_vport = ovs_vport_rcu(dp, OVSP_LOCAL);
584
585 if (!input_vport)
586 goto err_unlock;
587
588 OVS_CB(packet)->input_vport = input_vport;
Lorand Jakabd98612b2014-10-06 05:45:32 -0700589 sf_acts = rcu_dereference(flow->sf_acts);
Pravin B Shelar83c8df22014-09-15 19:20:31 -0700590
Jesse Grossccb13522011-10-25 19:26:31 -0700591 local_bh_disable();
Lorand Jakabd98612b2014-10-06 05:45:32 -0700592 err = ovs_execute_actions(dp, packet, sf_acts, &flow->key);
Jesse Grossccb13522011-10-25 19:26:31 -0700593 local_bh_enable();
594 rcu_read_unlock();
595
Andy Zhou03f0d912013-08-07 20:01:00 -0700596 ovs_flow_free(flow, false);
Jesse Grossccb13522011-10-25 19:26:31 -0700597 return err;
598
599err_unlock:
600 rcu_read_unlock();
601err_flow_free:
Andy Zhou03f0d912013-08-07 20:01:00 -0700602 ovs_flow_free(flow, false);
Jesse Grossccb13522011-10-25 19:26:31 -0700603err_kfree_skb:
604 kfree_skb(packet);
605err:
606 return err;
607}
608
609static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
Thomas Grafdded45fc2013-03-29 14:46:47 +0100610 [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
Jesse Grossccb13522011-10-25 19:26:31 -0700611 [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
612 [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
Thomas Graf1ba39802015-01-14 13:56:19 +0000613 [OVS_PACKET_ATTR_PROBE] = { .type = NLA_FLAG },
Jesse Grossccb13522011-10-25 19:26:31 -0700614};
615
Johannes Berg4534de82013-11-14 17:14:46 +0100616static const struct genl_ops dp_packet_genl_ops[] = {
Jesse Grossccb13522011-10-25 19:26:31 -0700617 { .cmd = OVS_PACKET_CMD_EXECUTE,
618 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
619 .policy = packet_policy,
620 .doit = ovs_packet_cmd_execute
621 }
622};
623
Pravin B Shelar0c200ef2014-05-06 16:44:50 -0700624static struct genl_family dp_packet_genl_family = {
625 .id = GENL_ID_GENERATE,
626 .hdrsize = sizeof(struct ovs_header),
627 .name = OVS_PACKET_FAMILY,
628 .version = OVS_PACKET_VERSION,
629 .maxattr = OVS_PACKET_ATTR_MAX,
630 .netnsok = true,
631 .parallel_ops = true,
632 .ops = dp_packet_genl_ops,
633 .n_ops = ARRAY_SIZE(dp_packet_genl_ops),
634};
635
Thomas Graf12eb18f2014-11-06 06:58:52 -0800636static void get_dp_stats(const struct datapath *dp, struct ovs_dp_stats *stats,
Andy Zhou1bd71162013-10-22 10:42:46 -0700637 struct ovs_dp_megaflow_stats *mega_stats)
Jesse Grossccb13522011-10-25 19:26:31 -0700638{
639 int i;
Jesse Grossccb13522011-10-25 19:26:31 -0700640
Andy Zhou1bd71162013-10-22 10:42:46 -0700641 memset(mega_stats, 0, sizeof(*mega_stats));
642
Pravin B Shelarb637e492013-10-04 00:14:23 -0700643 stats->n_flows = ovs_flow_tbl_count(&dp->table);
Andy Zhou1bd71162013-10-22 10:42:46 -0700644 mega_stats->n_masks = ovs_flow_tbl_num_masks(&dp->table);
Jesse Grossccb13522011-10-25 19:26:31 -0700645
646 stats->n_hit = stats->n_missed = stats->n_lost = 0;
Andy Zhou1bd71162013-10-22 10:42:46 -0700647
Jesse Grossccb13522011-10-25 19:26:31 -0700648 for_each_possible_cpu(i) {
649 const struct dp_stats_percpu *percpu_stats;
650 struct dp_stats_percpu local_stats;
651 unsigned int start;
652
653 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
654
655 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700656 start = u64_stats_fetch_begin_irq(&percpu_stats->syncp);
Jesse Grossccb13522011-10-25 19:26:31 -0700657 local_stats = *percpu_stats;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700658 } while (u64_stats_fetch_retry_irq(&percpu_stats->syncp, start));
Jesse Grossccb13522011-10-25 19:26:31 -0700659
660 stats->n_hit += local_stats.n_hit;
661 stats->n_missed += local_stats.n_missed;
662 stats->n_lost += local_stats.n_lost;
Andy Zhou1bd71162013-10-22 10:42:46 -0700663 mega_stats->n_mask_hit += local_stats.n_mask_hit;
Jesse Grossccb13522011-10-25 19:26:31 -0700664 }
665}
666
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800667static bool should_fill_key(const struct sw_flow_id *sfid, uint32_t ufid_flags)
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100668{
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800669 return ovs_identifier_is_ufid(sfid) &&
670 !(ufid_flags & OVS_UFID_F_OMIT_KEY);
671}
672
673static bool should_fill_mask(uint32_t ufid_flags)
674{
675 return !(ufid_flags & OVS_UFID_F_OMIT_MASK);
676}
677
678static bool should_fill_actions(uint32_t ufid_flags)
679{
680 return !(ufid_flags & OVS_UFID_F_OMIT_ACTIONS);
681}
682
683static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts,
684 const struct sw_flow_id *sfid,
685 uint32_t ufid_flags)
686{
687 size_t len = NLMSG_ALIGN(sizeof(struct ovs_header));
688
689 /* OVS_FLOW_ATTR_UFID */
690 if (sfid && ovs_identifier_is_ufid(sfid))
691 len += nla_total_size(sfid->ufid_len);
692
693 /* OVS_FLOW_ATTR_KEY */
694 if (!sfid || should_fill_key(sfid, ufid_flags))
695 len += nla_total_size(ovs_key_attr_size());
696
697 /* OVS_FLOW_ATTR_MASK */
698 if (should_fill_mask(ufid_flags))
699 len += nla_total_size(ovs_key_attr_size());
700
701 /* OVS_FLOW_ATTR_ACTIONS */
702 if (should_fill_actions(ufid_flags))
703 len += nla_total_size(acts->actions_len);
704
705 return len
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100706 + nla_total_size(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
707 + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800708 + nla_total_size(8); /* OVS_FLOW_ATTR_USED */
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100709}
710
Jarno Rajahalmebb6f9a72014-05-05 11:32:17 -0700711/* Called with ovs_mutex or RCU read lock. */
Joe Stringerca7105f2014-09-08 13:09:37 -0700712static int ovs_flow_cmd_fill_stats(const struct sw_flow *flow,
713 struct sk_buff *skb)
714{
715 struct ovs_flow_stats stats;
716 __be16 tcp_flags;
717 unsigned long used;
Andy Zhou03f0d912013-08-07 20:01:00 -0700718
Pravin B Shelare298e502013-10-29 17:22:21 -0700719 ovs_flow_stats_get(flow, &stats, &used, &tcp_flags);
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700720
David S. Miller028d6a62012-03-29 23:20:48 -0400721 if (used &&
722 nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
Joe Stringerca7105f2014-09-08 13:09:37 -0700723 return -EMSGSIZE;
Jesse Grossccb13522011-10-25 19:26:31 -0700724
David S. Miller028d6a62012-03-29 23:20:48 -0400725 if (stats.n_packets &&
Pravin B Shelare298e502013-10-29 17:22:21 -0700726 nla_put(skb, OVS_FLOW_ATTR_STATS, sizeof(struct ovs_flow_stats), &stats))
Joe Stringerca7105f2014-09-08 13:09:37 -0700727 return -EMSGSIZE;
Jesse Grossccb13522011-10-25 19:26:31 -0700728
Pravin B Shelare298e502013-10-29 17:22:21 -0700729 if ((u8)ntohs(tcp_flags) &&
730 nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, (u8)ntohs(tcp_flags)))
Joe Stringerca7105f2014-09-08 13:09:37 -0700731 return -EMSGSIZE;
732
733 return 0;
734}
735
736/* Called with ovs_mutex or RCU read lock. */
737static int ovs_flow_cmd_fill_actions(const struct sw_flow *flow,
738 struct sk_buff *skb, int skb_orig_len)
739{
740 struct nlattr *start;
741 int err;
Jesse Grossccb13522011-10-25 19:26:31 -0700742
743 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
744 * this is the first flow to be dumped into 'skb'. This is unusual for
745 * Netlink but individual action lists can be longer than
746 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
747 * The userspace caller can always fetch the actions separately if it
748 * really wants them. (Most userspace callers in fact don't care.)
749 *
750 * This can only fail for dump operations because the skb is always
751 * properly sized for single flows.
752 */
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700753 start = nla_nest_start(skb, OVS_FLOW_ATTR_ACTIONS);
754 if (start) {
Pravin B Shelard57170b2013-07-30 15:39:39 -0700755 const struct sw_flow_actions *sf_acts;
756
Jesse Gross663efa32013-12-03 10:58:53 -0800757 sf_acts = rcu_dereference_ovsl(flow->sf_acts);
Pravin B Shelare6445712013-10-03 18:16:47 -0700758 err = ovs_nla_put_actions(sf_acts->actions,
759 sf_acts->actions_len, skb);
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700760
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700761 if (!err)
762 nla_nest_end(skb, start);
763 else {
764 if (skb_orig_len)
Joe Stringerca7105f2014-09-08 13:09:37 -0700765 return err;
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700766
767 nla_nest_cancel(skb, start);
768 }
Joe Stringerca7105f2014-09-08 13:09:37 -0700769 } else if (skb_orig_len) {
770 return -EMSGSIZE;
771 }
772
773 return 0;
774}
775
776/* Called with ovs_mutex or RCU read lock. */
777static int ovs_flow_cmd_fill_info(const struct sw_flow *flow, int dp_ifindex,
778 struct sk_buff *skb, u32 portid,
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800779 u32 seq, u32 flags, u8 cmd, u32 ufid_flags)
Joe Stringerca7105f2014-09-08 13:09:37 -0700780{
781 const int skb_orig_len = skb->len;
782 struct ovs_header *ovs_header;
783 int err;
784
785 ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family,
786 flags, cmd);
787 if (!ovs_header)
788 return -EMSGSIZE;
789
790 ovs_header->dp_ifindex = dp_ifindex;
791
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800792 err = ovs_nla_put_identifier(flow, skb);
Joe Stringer5b4237b2015-01-21 16:42:48 -0800793 if (err)
794 goto error;
795
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800796 if (should_fill_key(&flow->id, ufid_flags)) {
797 err = ovs_nla_put_masked_key(flow, skb);
798 if (err)
799 goto error;
800 }
801
802 if (should_fill_mask(ufid_flags)) {
803 err = ovs_nla_put_mask(flow, skb);
804 if (err)
805 goto error;
806 }
Joe Stringerca7105f2014-09-08 13:09:37 -0700807
808 err = ovs_flow_cmd_fill_stats(flow, skb);
809 if (err)
810 goto error;
811
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800812 if (should_fill_actions(ufid_flags)) {
813 err = ovs_flow_cmd_fill_actions(flow, skb, skb_orig_len);
814 if (err)
815 goto error;
816 }
Jesse Grossccb13522011-10-25 19:26:31 -0700817
Johannes Berg053c0952015-01-16 22:09:00 +0100818 genlmsg_end(skb, ovs_header);
819 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -0700820
Jesse Grossccb13522011-10-25 19:26:31 -0700821error:
822 genlmsg_cancel(skb, ovs_header);
823 return err;
824}
825
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700826/* May not be called with RCU read lock. */
827static struct sk_buff *ovs_flow_cmd_alloc_info(const struct sw_flow_actions *acts,
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800828 const struct sw_flow_id *sfid,
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700829 struct genl_info *info,
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800830 bool always,
831 uint32_t ufid_flags)
Jesse Grossccb13522011-10-25 19:26:31 -0700832{
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700833 struct sk_buff *skb;
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800834 size_t len;
Jesse Grossccb13522011-10-25 19:26:31 -0700835
Samuel Gauthier9b67aa42014-09-18 10:31:04 +0200836 if (!always && !ovs_must_notify(&dp_flow_genl_family, info, 0))
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700837 return NULL;
838
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800839 len = ovs_flow_cmd_msg_size(acts, sfid, ufid_flags);
840 skb = genlmsg_new_unicast(len, info, GFP_KERNEL);
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700841 if (!skb)
842 return ERR_PTR(-ENOMEM);
843
844 return skb;
Jesse Grossccb13522011-10-25 19:26:31 -0700845}
846
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700847/* Called with ovs_mutex. */
848static struct sk_buff *ovs_flow_cmd_build_info(const struct sw_flow *flow,
849 int dp_ifindex,
850 struct genl_info *info, u8 cmd,
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800851 bool always, u32 ufid_flags)
Jesse Grossccb13522011-10-25 19:26:31 -0700852{
853 struct sk_buff *skb;
854 int retval;
855
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800856 skb = ovs_flow_cmd_alloc_info(ovsl_dereference(flow->sf_acts),
857 &flow->id, info, always, ufid_flags);
Himangi Saraogid0e992a2014-07-27 12:37:46 +0530858 if (IS_ERR_OR_NULL(skb))
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700859 return skb;
Jesse Grossccb13522011-10-25 19:26:31 -0700860
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700861 retval = ovs_flow_cmd_fill_info(flow, dp_ifindex, skb,
862 info->snd_portid, info->snd_seq, 0,
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800863 cmd, ufid_flags);
Jesse Grossccb13522011-10-25 19:26:31 -0700864 BUG_ON(retval < 0);
865 return skb;
866}
867
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700868static int ovs_flow_cmd_new(struct sk_buff *skb, struct genl_info *info)
Jesse Grossccb13522011-10-25 19:26:31 -0700869{
870 struct nlattr **a = info->attrs;
871 struct ovs_header *ovs_header = info->userhdr;
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800872 struct sw_flow *flow = NULL, *new_flow;
Andy Zhou03f0d912013-08-07 20:01:00 -0700873 struct sw_flow_mask mask;
Jesse Grossccb13522011-10-25 19:26:31 -0700874 struct sk_buff *reply;
875 struct datapath *dp;
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800876 struct sw_flow_key key;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700877 struct sw_flow_actions *acts;
878 struct sw_flow_match match;
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800879 u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700880 int error;
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800881 bool log = !a[OVS_FLOW_ATTR_PROBE];
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700882
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700883 /* Must have key and actions. */
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700884 error = -EINVAL;
Jesse Gross426cda52014-10-06 05:08:38 -0700885 if (!a[OVS_FLOW_ATTR_KEY]) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800886 OVS_NLERR(log, "Flow key attr not present in new flow.");
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700887 goto error;
Jesse Gross426cda52014-10-06 05:08:38 -0700888 }
889 if (!a[OVS_FLOW_ATTR_ACTIONS]) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800890 OVS_NLERR(log, "Flow actions attr not present in new flow.");
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700891 goto error;
Jesse Gross426cda52014-10-06 05:08:38 -0700892 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700893
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700894 /* Most of the time we need to allocate a new flow, do it before
895 * locking.
896 */
897 new_flow = ovs_flow_alloc();
898 if (IS_ERR(new_flow)) {
899 error = PTR_ERR(new_flow);
900 goto error;
901 }
902
903 /* Extract key. */
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800904 ovs_match_init(&match, &key, &mask);
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800905 error = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY],
906 a[OVS_FLOW_ATTR_MASK], log);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700907 if (error)
908 goto err_kfree_flow;
909
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800910 ovs_flow_mask_key(&new_flow->key, &key, &mask);
911
912 /* Extract flow identifier. */
913 error = ovs_nla_get_identifier(&new_flow->id, a[OVS_FLOW_ATTR_UFID],
914 &key, log);
915 if (error)
916 goto err_kfree_flow;
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700917
918 /* Validate actions. */
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700919 error = ovs_nla_copy_actions(a[OVS_FLOW_ATTR_ACTIONS], &new_flow->key,
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800920 &acts, log);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700921 if (error) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800922 OVS_NLERR(log, "Flow actions may not be safe on all matching packets.");
Pravin B Shelar2fdb9572014-10-19 11:19:51 -0700923 goto err_kfree_flow;
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700924 }
925
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800926 reply = ovs_flow_cmd_alloc_info(acts, &new_flow->id, info, false,
927 ufid_flags);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700928 if (IS_ERR(reply)) {
929 error = PTR_ERR(reply);
930 goto err_kfree_acts;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700931 }
932
933 ovs_lock();
934 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700935 if (unlikely(!dp)) {
936 error = -ENODEV;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700937 goto err_unlock_ovs;
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700938 }
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800939
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700940 /* Check if this is a duplicate flow */
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800941 if (ovs_identifier_is_ufid(&new_flow->id))
942 flow = ovs_flow_tbl_lookup_ufid(&dp->table, &new_flow->id);
943 if (!flow)
944 flow = ovs_flow_tbl_lookup(&dp->table, &key);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700945 if (likely(!flow)) {
946 rcu_assign_pointer(new_flow->sf_acts, acts);
947
948 /* Put flow in bucket. */
949 error = ovs_flow_tbl_insert(&dp->table, new_flow, &mask);
950 if (unlikely(error)) {
951 acts = NULL;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700952 goto err_unlock_ovs;
953 }
954
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700955 if (unlikely(reply)) {
956 error = ovs_flow_cmd_fill_info(new_flow,
957 ovs_header->dp_ifindex,
958 reply, info->snd_portid,
959 info->snd_seq, 0,
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800960 OVS_FLOW_CMD_NEW,
961 ufid_flags);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700962 BUG_ON(error < 0);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700963 }
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700964 ovs_unlock();
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700965 } else {
966 struct sw_flow_actions *old_acts;
967
968 /* Bail out if we're not allowed to modify an existing flow.
969 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
970 * because Generic Netlink treats the latter as a dump
971 * request. We also accept NLM_F_EXCL in case that bug ever
972 * gets fixed.
973 */
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700974 if (unlikely(info->nlhdr->nlmsg_flags & (NLM_F_CREATE
975 | NLM_F_EXCL))) {
976 error = -EEXIST;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700977 goto err_unlock_ovs;
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700978 }
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800979 /* The flow identifier has to be the same for flow updates.
980 * Look for any overlapping flow.
981 */
982 if (unlikely(!ovs_flow_cmp(flow, &match))) {
983 if (ovs_identifier_is_key(&flow->id))
984 flow = ovs_flow_tbl_lookup_exact(&dp->table,
985 &match);
986 else /* UFID matches but key is different */
987 flow = NULL;
Alex Wang4a46b242014-06-30 20:30:29 -0700988 if (!flow) {
989 error = -ENOENT;
990 goto err_unlock_ovs;
991 }
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700992 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700993 /* Update actions. */
994 old_acts = ovsl_dereference(flow->sf_acts);
995 rcu_assign_pointer(flow->sf_acts, acts);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700996
997 if (unlikely(reply)) {
998 error = ovs_flow_cmd_fill_info(flow,
999 ovs_header->dp_ifindex,
1000 reply, info->snd_portid,
1001 info->snd_seq, 0,
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001002 OVS_FLOW_CMD_NEW,
1003 ufid_flags);
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001004 BUG_ON(error < 0);
1005 }
1006 ovs_unlock();
1007
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001008 ovs_nla_free_flow_actions(old_acts);
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001009 ovs_flow_free(new_flow, false);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001010 }
1011
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001012 if (reply)
1013 ovs_notify(&dp_flow_genl_family, reply, info);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001014 return 0;
1015
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001016err_unlock_ovs:
1017 ovs_unlock();
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001018 kfree_skb(reply);
1019err_kfree_acts:
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001020 kfree(acts);
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001021err_kfree_flow:
1022 ovs_flow_free(new_flow, false);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001023error:
1024 return error;
1025}
1026
Pravin B Shelar2fdb9572014-10-19 11:19:51 -07001027/* Factor out action copy to avoid "Wframe-larger-than=1024" warning. */
Jesse Gross6b205b22014-10-03 15:35:32 -07001028static struct sw_flow_actions *get_flow_actions(const struct nlattr *a,
1029 const struct sw_flow_key *key,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001030 const struct sw_flow_mask *mask,
1031 bool log)
Jesse Gross6b205b22014-10-03 15:35:32 -07001032{
1033 struct sw_flow_actions *acts;
1034 struct sw_flow_key masked_key;
1035 int error;
1036
Jesse Gross6b205b22014-10-03 15:35:32 -07001037 ovs_flow_mask_key(&masked_key, key, mask);
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001038 error = ovs_nla_copy_actions(a, &masked_key, &acts, log);
Jesse Gross6b205b22014-10-03 15:35:32 -07001039 if (error) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001040 OVS_NLERR(log,
1041 "Actions may not be safe on all matching packets");
Jesse Gross6b205b22014-10-03 15:35:32 -07001042 return ERR_PTR(error);
1043 }
1044
1045 return acts;
1046}
1047
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001048static int ovs_flow_cmd_set(struct sk_buff *skb, struct genl_info *info)
1049{
1050 struct nlattr **a = info->attrs;
1051 struct ovs_header *ovs_header = info->userhdr;
Jesse Gross6b205b22014-10-03 15:35:32 -07001052 struct sw_flow_key key;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001053 struct sw_flow *flow;
1054 struct sw_flow_mask mask;
1055 struct sk_buff *reply = NULL;
1056 struct datapath *dp;
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001057 struct sw_flow_actions *old_acts = NULL, *acts = NULL;
Andy Zhou03f0d912013-08-07 20:01:00 -07001058 struct sw_flow_match match;
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001059 struct sw_flow_id sfid;
1060 u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
Jesse Grossccb13522011-10-25 19:26:31 -07001061 int error;
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001062 bool log = !a[OVS_FLOW_ATTR_PROBE];
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001063 bool ufid_present;
Jesse Grossccb13522011-10-25 19:26:31 -07001064
1065 /* Extract key. */
1066 error = -EINVAL;
Jesse Gross426cda52014-10-06 05:08:38 -07001067 if (!a[OVS_FLOW_ATTR_KEY]) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001068 OVS_NLERR(log, "Flow key attribute not present in set flow.");
Jesse Grossccb13522011-10-25 19:26:31 -07001069 goto error;
Jesse Gross426cda52014-10-06 05:08:38 -07001070 }
Andy Zhou03f0d912013-08-07 20:01:00 -07001071
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001072 ufid_present = ovs_nla_get_ufid(&sfid, a[OVS_FLOW_ATTR_UFID], log);
Andy Zhou03f0d912013-08-07 20:01:00 -07001073 ovs_match_init(&match, &key, &mask);
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001074 error = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY],
1075 a[OVS_FLOW_ATTR_MASK], log);
Jesse Grossccb13522011-10-25 19:26:31 -07001076 if (error)
1077 goto error;
1078
1079 /* Validate actions. */
1080 if (a[OVS_FLOW_ATTR_ACTIONS]) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001081 acts = get_flow_actions(a[OVS_FLOW_ATTR_ACTIONS], &key, &mask,
1082 log);
Jesse Gross6b205b22014-10-03 15:35:32 -07001083 if (IS_ERR(acts)) {
1084 error = PTR_ERR(acts);
Jesse Grossccb13522011-10-25 19:26:31 -07001085 goto error;
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001086 }
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001087
Pravin B Shelar2fdb9572014-10-19 11:19:51 -07001088 /* Can allocate before locking if have acts. */
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001089 reply = ovs_flow_cmd_alloc_info(acts, &sfid, info, false,
1090 ufid_flags);
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001091 if (IS_ERR(reply)) {
1092 error = PTR_ERR(reply);
1093 goto err_kfree_acts;
Andy Zhou03f0d912013-08-07 20:01:00 -07001094 }
Jesse Grossccb13522011-10-25 19:26:31 -07001095 }
1096
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001097 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001098 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001099 if (unlikely(!dp)) {
1100 error = -ENODEV;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001101 goto err_unlock_ovs;
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001102 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001103 /* Check that the flow exists. */
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001104 if (ufid_present)
1105 flow = ovs_flow_tbl_lookup_ufid(&dp->table, &sfid);
1106 else
1107 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001108 if (unlikely(!flow)) {
1109 error = -ENOENT;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001110 goto err_unlock_ovs;
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001111 }
Alex Wang4a46b242014-06-30 20:30:29 -07001112
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001113 /* Update actions, if present. */
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001114 if (likely(acts)) {
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001115 old_acts = ovsl_dereference(flow->sf_acts);
Jesse Grossccb13522011-10-25 19:26:31 -07001116 rcu_assign_pointer(flow->sf_acts, acts);
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001117
1118 if (unlikely(reply)) {
1119 error = ovs_flow_cmd_fill_info(flow,
1120 ovs_header->dp_ifindex,
1121 reply, info->snd_portid,
1122 info->snd_seq, 0,
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001123 OVS_FLOW_CMD_NEW,
1124 ufid_flags);
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001125 BUG_ON(error < 0);
1126 }
1127 } else {
1128 /* Could not alloc without acts before locking. */
1129 reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex,
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001130 info, OVS_FLOW_CMD_NEW, false,
1131 ufid_flags);
1132
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001133 if (unlikely(IS_ERR(reply))) {
1134 error = PTR_ERR(reply);
1135 goto err_unlock_ovs;
1136 }
Jesse Grossccb13522011-10-25 19:26:31 -07001137 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001138
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001139 /* Clear stats. */
1140 if (a[OVS_FLOW_ATTR_CLEAR])
1141 ovs_flow_stats_clear(flow);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001142 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001143
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001144 if (reply)
1145 ovs_notify(&dp_flow_genl_family, reply, info);
1146 if (old_acts)
1147 ovs_nla_free_flow_actions(old_acts);
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -07001148
Jesse Grossccb13522011-10-25 19:26:31 -07001149 return 0;
1150
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001151err_unlock_ovs:
1152 ovs_unlock();
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001153 kfree_skb(reply);
1154err_kfree_acts:
Pravin B Shelar74f84a52013-06-17 17:50:12 -07001155 kfree(acts);
Jesse Grossccb13522011-10-25 19:26:31 -07001156error:
1157 return error;
1158}
1159
1160static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1161{
1162 struct nlattr **a = info->attrs;
1163 struct ovs_header *ovs_header = info->userhdr;
1164 struct sw_flow_key key;
1165 struct sk_buff *reply;
1166 struct sw_flow *flow;
1167 struct datapath *dp;
Andy Zhou03f0d912013-08-07 20:01:00 -07001168 struct sw_flow_match match;
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001169 struct sw_flow_id ufid;
1170 u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
1171 int err = 0;
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001172 bool log = !a[OVS_FLOW_ATTR_PROBE];
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001173 bool ufid_present;
Jesse Grossccb13522011-10-25 19:26:31 -07001174
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001175 ufid_present = ovs_nla_get_ufid(&ufid, a[OVS_FLOW_ATTR_UFID], log);
1176 if (a[OVS_FLOW_ATTR_KEY]) {
1177 ovs_match_init(&match, &key, NULL);
1178 err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL,
1179 log);
1180 } else if (!ufid_present) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001181 OVS_NLERR(log,
1182 "Flow get message rejected, Key attribute missing.");
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001183 err = -EINVAL;
Andy Zhou03f0d912013-08-07 20:01:00 -07001184 }
Jesse Grossccb13522011-10-25 19:26:31 -07001185 if (err)
1186 return err;
1187
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001188 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001189 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001190 if (!dp) {
1191 err = -ENODEV;
1192 goto unlock;
1193 }
Jesse Grossccb13522011-10-25 19:26:31 -07001194
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001195 if (ufid_present)
1196 flow = ovs_flow_tbl_lookup_ufid(&dp->table, &ufid);
1197 else
1198 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
Alex Wang4a46b242014-06-30 20:30:29 -07001199 if (!flow) {
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001200 err = -ENOENT;
1201 goto unlock;
1202 }
Jesse Grossccb13522011-10-25 19:26:31 -07001203
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -07001204 reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex, info,
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001205 OVS_FLOW_CMD_NEW, true, ufid_flags);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001206 if (IS_ERR(reply)) {
1207 err = PTR_ERR(reply);
1208 goto unlock;
1209 }
Jesse Grossccb13522011-10-25 19:26:31 -07001210
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001211 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001212 return genlmsg_reply(reply, info);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001213unlock:
1214 ovs_unlock();
1215 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001216}
1217
1218static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1219{
1220 struct nlattr **a = info->attrs;
1221 struct ovs_header *ovs_header = info->userhdr;
1222 struct sw_flow_key key;
1223 struct sk_buff *reply;
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001224 struct sw_flow *flow = NULL;
Jesse Grossccb13522011-10-25 19:26:31 -07001225 struct datapath *dp;
Andy Zhou03f0d912013-08-07 20:01:00 -07001226 struct sw_flow_match match;
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001227 struct sw_flow_id ufid;
1228 u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
Jesse Grossccb13522011-10-25 19:26:31 -07001229 int err;
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001230 bool log = !a[OVS_FLOW_ATTR_PROBE];
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001231 bool ufid_present;
Jesse Grossccb13522011-10-25 19:26:31 -07001232
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001233 ufid_present = ovs_nla_get_ufid(&ufid, a[OVS_FLOW_ATTR_UFID], log);
1234 if (a[OVS_FLOW_ATTR_KEY]) {
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001235 ovs_match_init(&match, &key, NULL);
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001236 err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL,
1237 log);
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001238 if (unlikely(err))
1239 return err;
1240 }
1241
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001242 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001243 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001244 if (unlikely(!dp)) {
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001245 err = -ENODEV;
1246 goto unlock;
1247 }
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001248
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001249 if (unlikely(!a[OVS_FLOW_ATTR_KEY] && !ufid_present)) {
Pravin B Shelarb637e492013-10-04 00:14:23 -07001250 err = ovs_flow_tbl_flush(&dp->table);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001251 goto unlock;
1252 }
Andy Zhou03f0d912013-08-07 20:01:00 -07001253
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001254 if (ufid_present)
1255 flow = ovs_flow_tbl_lookup_ufid(&dp->table, &ufid);
1256 else
1257 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
Alex Wang4a46b242014-06-30 20:30:29 -07001258 if (unlikely(!flow)) {
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001259 err = -ENOENT;
1260 goto unlock;
1261 }
Jesse Grossccb13522011-10-25 19:26:31 -07001262
Pravin B Shelarb637e492013-10-04 00:14:23 -07001263 ovs_flow_tbl_remove(&dp->table, flow);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001264 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001265
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001266 reply = ovs_flow_cmd_alloc_info((const struct sw_flow_actions __force *) flow->sf_acts,
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001267 &flow->id, info, false, ufid_flags);
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001268 if (likely(reply)) {
1269 if (likely(!IS_ERR(reply))) {
1270 rcu_read_lock(); /*To keep RCU checker happy. */
1271 err = ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex,
1272 reply, info->snd_portid,
1273 info->snd_seq, 0,
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001274 OVS_FLOW_CMD_DEL,
1275 ufid_flags);
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001276 rcu_read_unlock();
1277 BUG_ON(err < 0);
1278
1279 ovs_notify(&dp_flow_genl_family, reply, info);
1280 } else {
1281 netlink_set_err(sock_net(skb->sk)->genl_sock, 0, 0, PTR_ERR(reply));
1282 }
1283 }
1284
1285 ovs_flow_free(flow, true);
Jesse Grossccb13522011-10-25 19:26:31 -07001286 return 0;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001287unlock:
1288 ovs_unlock();
1289 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001290}
1291
1292static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1293{
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001294 struct nlattr *a[__OVS_FLOW_ATTR_MAX];
Jesse Grossccb13522011-10-25 19:26:31 -07001295 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
Pravin B Shelarb637e492013-10-04 00:14:23 -07001296 struct table_instance *ti;
Jesse Grossccb13522011-10-25 19:26:31 -07001297 struct datapath *dp;
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001298 u32 ufid_flags;
1299 int err;
1300
1301 err = genlmsg_parse(cb->nlh, &dp_flow_genl_family, a,
1302 OVS_FLOW_ATTR_MAX, flow_policy);
1303 if (err)
1304 return err;
1305 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
Jesse Grossccb13522011-10-25 19:26:31 -07001306
Pravin B Shelard57170b2013-07-30 15:39:39 -07001307 rcu_read_lock();
Andy Zhoucc3a5ae2014-09-08 13:14:22 -07001308 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001309 if (!dp) {
Pravin B Shelard57170b2013-07-30 15:39:39 -07001310 rcu_read_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001311 return -ENODEV;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001312 }
Jesse Grossccb13522011-10-25 19:26:31 -07001313
Pravin B Shelarb637e492013-10-04 00:14:23 -07001314 ti = rcu_dereference(dp->table.ti);
Jesse Grossccb13522011-10-25 19:26:31 -07001315 for (;;) {
1316 struct sw_flow *flow;
1317 u32 bucket, obj;
1318
1319 bucket = cb->args[0];
1320 obj = cb->args[1];
Pravin B Shelarb637e492013-10-04 00:14:23 -07001321 flow = ovs_flow_tbl_dump_next(ti, &bucket, &obj);
Jesse Grossccb13522011-10-25 19:26:31 -07001322 if (!flow)
1323 break;
1324
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -07001325 if (ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex, skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001326 NETLINK_CB(cb->skb).portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001327 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001328 OVS_FLOW_CMD_NEW, ufid_flags) < 0)
Jesse Grossccb13522011-10-25 19:26:31 -07001329 break;
1330
1331 cb->args[0] = bucket;
1332 cb->args[1] = obj;
1333 }
Pravin B Shelard57170b2013-07-30 15:39:39 -07001334 rcu_read_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001335 return skb->len;
1336}
1337
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001338static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
1339 [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001340 [OVS_FLOW_ATTR_MASK] = { .type = NLA_NESTED },
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001341 [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
1342 [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001343 [OVS_FLOW_ATTR_PROBE] = { .type = NLA_FLAG },
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001344 [OVS_FLOW_ATTR_UFID] = { .type = NLA_UNSPEC, .len = 1 },
1345 [OVS_FLOW_ATTR_UFID_FLAGS] = { .type = NLA_U32 },
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001346};
1347
stephen hemminger48e48a72014-07-16 11:25:52 -07001348static const struct genl_ops dp_flow_genl_ops[] = {
Jesse Grossccb13522011-10-25 19:26:31 -07001349 { .cmd = OVS_FLOW_CMD_NEW,
1350 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1351 .policy = flow_policy,
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001352 .doit = ovs_flow_cmd_new
Jesse Grossccb13522011-10-25 19:26:31 -07001353 },
1354 { .cmd = OVS_FLOW_CMD_DEL,
1355 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1356 .policy = flow_policy,
1357 .doit = ovs_flow_cmd_del
1358 },
1359 { .cmd = OVS_FLOW_CMD_GET,
1360 .flags = 0, /* OK for unprivileged users. */
1361 .policy = flow_policy,
1362 .doit = ovs_flow_cmd_get,
1363 .dumpit = ovs_flow_cmd_dump
1364 },
1365 { .cmd = OVS_FLOW_CMD_SET,
1366 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1367 .policy = flow_policy,
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001368 .doit = ovs_flow_cmd_set,
Jesse Grossccb13522011-10-25 19:26:31 -07001369 },
1370};
1371
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001372static struct genl_family dp_flow_genl_family = {
Jesse Grossccb13522011-10-25 19:26:31 -07001373 .id = GENL_ID_GENERATE,
1374 .hdrsize = sizeof(struct ovs_header),
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001375 .name = OVS_FLOW_FAMILY,
1376 .version = OVS_FLOW_VERSION,
1377 .maxattr = OVS_FLOW_ATTR_MAX,
Pravin B Shelar3a4e0d62013-04-23 07:48:48 +00001378 .netnsok = true,
1379 .parallel_ops = true,
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001380 .ops = dp_flow_genl_ops,
1381 .n_ops = ARRAY_SIZE(dp_flow_genl_ops),
1382 .mcgrps = &ovs_dp_flow_multicast_group,
1383 .n_mcgrps = 1,
Jesse Grossccb13522011-10-25 19:26:31 -07001384};
1385
Thomas Grafc3ff8cf2013-03-29 14:46:49 +01001386static size_t ovs_dp_cmd_msg_size(void)
1387{
1388 size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
1389
1390 msgsize += nla_total_size(IFNAMSIZ);
1391 msgsize += nla_total_size(sizeof(struct ovs_dp_stats));
Andy Zhou1bd71162013-10-22 10:42:46 -07001392 msgsize += nla_total_size(sizeof(struct ovs_dp_megaflow_stats));
Daniele Di Proietto45fb9c32014-01-23 10:47:35 -08001393 msgsize += nla_total_size(sizeof(u32)); /* OVS_DP_ATTR_USER_FEATURES */
Thomas Grafc3ff8cf2013-03-29 14:46:49 +01001394
1395 return msgsize;
1396}
1397
Pravin B Shelar8ec609d2014-11-11 15:55:16 -08001398/* Called with ovs_mutex. */
Jesse Grossccb13522011-10-25 19:26:31 -07001399static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001400 u32 portid, u32 seq, u32 flags, u8 cmd)
Jesse Grossccb13522011-10-25 19:26:31 -07001401{
1402 struct ovs_header *ovs_header;
1403 struct ovs_dp_stats dp_stats;
Andy Zhou1bd71162013-10-22 10:42:46 -07001404 struct ovs_dp_megaflow_stats dp_megaflow_stats;
Jesse Grossccb13522011-10-25 19:26:31 -07001405 int err;
1406
Eric W. Biederman15e47302012-09-07 20:12:54 +00001407 ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
Jesse Grossccb13522011-10-25 19:26:31 -07001408 flags, cmd);
1409 if (!ovs_header)
1410 goto error;
1411
1412 ovs_header->dp_ifindex = get_dpifindex(dp);
1413
Jesse Grossccb13522011-10-25 19:26:31 -07001414 err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
Jesse Grossccb13522011-10-25 19:26:31 -07001415 if (err)
1416 goto nla_put_failure;
1417
Andy Zhou1bd71162013-10-22 10:42:46 -07001418 get_dp_stats(dp, &dp_stats, &dp_megaflow_stats);
1419 if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats),
1420 &dp_stats))
1421 goto nla_put_failure;
1422
1423 if (nla_put(skb, OVS_DP_ATTR_MEGAFLOW_STATS,
1424 sizeof(struct ovs_dp_megaflow_stats),
1425 &dp_megaflow_stats))
David S. Miller028d6a62012-03-29 23:20:48 -04001426 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001427
Thomas Graf43d4be92013-12-13 15:22:18 +01001428 if (nla_put_u32(skb, OVS_DP_ATTR_USER_FEATURES, dp->user_features))
1429 goto nla_put_failure;
1430
Johannes Berg053c0952015-01-16 22:09:00 +01001431 genlmsg_end(skb, ovs_header);
1432 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001433
1434nla_put_failure:
1435 genlmsg_cancel(skb, ovs_header);
1436error:
1437 return -EMSGSIZE;
1438}
1439
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001440static struct sk_buff *ovs_dp_cmd_alloc_info(struct genl_info *info)
Jesse Grossccb13522011-10-25 19:26:31 -07001441{
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001442 return genlmsg_new_unicast(ovs_dp_cmd_msg_size(), info, GFP_KERNEL);
Jesse Grossccb13522011-10-25 19:26:31 -07001443}
1444
Jarno Rajahalmebb6f9a72014-05-05 11:32:17 -07001445/* Called with rcu_read_lock or ovs_mutex. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001446static struct datapath *lookup_datapath(struct net *net,
Thomas Graf12eb18f2014-11-06 06:58:52 -08001447 const struct ovs_header *ovs_header,
Jesse Grossccb13522011-10-25 19:26:31 -07001448 struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1449{
1450 struct datapath *dp;
1451
1452 if (!a[OVS_DP_ATTR_NAME])
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001453 dp = get_dp(net, ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001454 else {
1455 struct vport *vport;
1456
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001457 vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
Jesse Grossccb13522011-10-25 19:26:31 -07001458 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
Jesse Grossccb13522011-10-25 19:26:31 -07001459 }
1460 return dp ? dp : ERR_PTR(-ENODEV);
1461}
1462
Thomas Graf44da5ae2013-12-13 15:22:19 +01001463static void ovs_dp_reset_user_features(struct sk_buff *skb, struct genl_info *info)
1464{
1465 struct datapath *dp;
1466
1467 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
Jiri Pirko3c7eacf2014-02-14 11:42:36 +01001468 if (IS_ERR(dp))
Thomas Graf44da5ae2013-12-13 15:22:19 +01001469 return;
1470
1471 WARN(dp->user_features, "Dropping previously announced user features\n");
1472 dp->user_features = 0;
1473}
1474
Thomas Graf12eb18f2014-11-06 06:58:52 -08001475static void ovs_dp_change(struct datapath *dp, struct nlattr *a[])
Thomas Graf43d4be92013-12-13 15:22:18 +01001476{
1477 if (a[OVS_DP_ATTR_USER_FEATURES])
1478 dp->user_features = nla_get_u32(a[OVS_DP_ATTR_USER_FEATURES]);
1479}
1480
Jesse Grossccb13522011-10-25 19:26:31 -07001481static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1482{
1483 struct nlattr **a = info->attrs;
1484 struct vport_parms parms;
1485 struct sk_buff *reply;
1486 struct datapath *dp;
1487 struct vport *vport;
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001488 struct ovs_net *ovs_net;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001489 int err, i;
Jesse Grossccb13522011-10-25 19:26:31 -07001490
1491 err = -EINVAL;
1492 if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1493 goto err;
1494
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001495 reply = ovs_dp_cmd_alloc_info(info);
1496 if (!reply)
1497 return -ENOMEM;
Jesse Grossccb13522011-10-25 19:26:31 -07001498
1499 err = -ENOMEM;
1500 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1501 if (dp == NULL)
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001502 goto err_free_reply;
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001503
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001504 ovs_dp_set_net(dp, hold_net(sock_net(skb->sk)));
Jesse Grossccb13522011-10-25 19:26:31 -07001505
1506 /* Allocate table. */
Pravin B Shelarb637e492013-10-04 00:14:23 -07001507 err = ovs_flow_tbl_init(&dp->table);
1508 if (err)
Jesse Grossccb13522011-10-25 19:26:31 -07001509 goto err_free_dp;
1510
WANG Cong1c213bd2014-02-13 11:46:28 -08001511 dp->stats_percpu = netdev_alloc_pcpu_stats(struct dp_stats_percpu);
Jesse Grossccb13522011-10-25 19:26:31 -07001512 if (!dp->stats_percpu) {
1513 err = -ENOMEM;
1514 goto err_destroy_table;
1515 }
1516
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001517 dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
Pravin B Shelare6445712013-10-03 18:16:47 -07001518 GFP_KERNEL);
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001519 if (!dp->ports) {
1520 err = -ENOMEM;
1521 goto err_destroy_percpu;
1522 }
1523
1524 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1525 INIT_HLIST_HEAD(&dp->ports[i]);
1526
Jesse Grossccb13522011-10-25 19:26:31 -07001527 /* Set up our datapath device. */
1528 parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1529 parms.type = OVS_VPORT_TYPE_INTERNAL;
1530 parms.options = NULL;
1531 parms.dp = dp;
1532 parms.port_no = OVSP_LOCAL;
Alex Wang5cd667b2014-07-17 15:14:13 -07001533 parms.upcall_portids = a[OVS_DP_ATTR_UPCALL_PID];
Jesse Grossccb13522011-10-25 19:26:31 -07001534
Thomas Graf43d4be92013-12-13 15:22:18 +01001535 ovs_dp_change(dp, a);
1536
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001537 /* So far only local changes have been made, now need the lock. */
1538 ovs_lock();
1539
Jesse Grossccb13522011-10-25 19:26:31 -07001540 vport = new_vport(&parms);
1541 if (IS_ERR(vport)) {
1542 err = PTR_ERR(vport);
1543 if (err == -EBUSY)
1544 err = -EEXIST;
1545
Thomas Graf44da5ae2013-12-13 15:22:19 +01001546 if (err == -EEXIST) {
1547 /* An outdated user space instance that does not understand
1548 * the concept of user_features has attempted to create a new
1549 * datapath and is likely to reuse it. Drop all user features.
1550 */
1551 if (info->genlhdr->version < OVS_DP_VER_FEATURES)
1552 ovs_dp_reset_user_features(skb, info);
1553 }
1554
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001555 goto err_destroy_ports_array;
Jesse Grossccb13522011-10-25 19:26:31 -07001556 }
1557
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001558 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1559 info->snd_seq, 0, OVS_DP_CMD_NEW);
1560 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001561
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001562 ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001563 list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001564
1565 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001566
Johannes Berg2a94fe42013-11-19 15:19:39 +01001567 ovs_notify(&dp_datapath_genl_family, reply, info);
Jesse Grossccb13522011-10-25 19:26:31 -07001568 return 0;
1569
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001570err_destroy_ports_array:
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001571 ovs_unlock();
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001572 kfree(dp->ports);
Jesse Grossccb13522011-10-25 19:26:31 -07001573err_destroy_percpu:
1574 free_percpu(dp->stats_percpu);
1575err_destroy_table:
Pravin B Shelar9b996e52014-05-06 18:41:20 -07001576 ovs_flow_tbl_destroy(&dp->table);
Jesse Grossccb13522011-10-25 19:26:31 -07001577err_free_dp:
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001578 release_net(ovs_dp_get_net(dp));
Jesse Grossccb13522011-10-25 19:26:31 -07001579 kfree(dp);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001580err_free_reply:
1581 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001582err:
1583 return err;
1584}
1585
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001586/* Called with ovs_mutex. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001587static void __dp_destroy(struct datapath *dp)
Jesse Grossccb13522011-10-25 19:26:31 -07001588{
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001589 int i;
Jesse Grossccb13522011-10-25 19:26:31 -07001590
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001591 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1592 struct vport *vport;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001593 struct hlist_node *n;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001594
Sasha Levinb67bfe02013-02-27 17:06:00 -08001595 hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001596 if (vport->port_no != OVSP_LOCAL)
1597 ovs_dp_detach_port(vport);
1598 }
Jesse Grossccb13522011-10-25 19:26:31 -07001599
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001600 list_del_rcu(&dp->list_node);
Jesse Grossccb13522011-10-25 19:26:31 -07001601
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001602 /* OVSP_LOCAL is datapath internal port. We need to make sure that
Andy Zhoue80857c2014-01-21 09:31:04 -08001603 * all ports in datapath are destroyed first before freeing datapath.
Jesse Grossccb13522011-10-25 19:26:31 -07001604 */
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001605 ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
Jesse Grossccb13522011-10-25 19:26:31 -07001606
Andy Zhoue80857c2014-01-21 09:31:04 -08001607 /* RCU destroy the flow table */
Jesse Grossccb13522011-10-25 19:26:31 -07001608 call_rcu(&dp->rcu, destroy_dp_rcu);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001609}
1610
1611static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1612{
1613 struct sk_buff *reply;
1614 struct datapath *dp;
1615 int err;
1616
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001617 reply = ovs_dp_cmd_alloc_info(info);
1618 if (!reply)
1619 return -ENOMEM;
1620
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001621 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001622 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1623 err = PTR_ERR(dp);
1624 if (IS_ERR(dp))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001625 goto err_unlock_free;
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001626
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001627 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1628 info->snd_seq, 0, OVS_DP_CMD_DEL);
1629 BUG_ON(err < 0);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001630
1631 __dp_destroy(dp);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001632 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001633
Johannes Berg2a94fe42013-11-19 15:19:39 +01001634 ovs_notify(&dp_datapath_genl_family, reply, info);
Jesse Grossccb13522011-10-25 19:26:31 -07001635
1636 return 0;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001637
1638err_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001639 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001640 kfree_skb(reply);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001641 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001642}
1643
1644static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1645{
1646 struct sk_buff *reply;
1647 struct datapath *dp;
1648 int err;
1649
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001650 reply = ovs_dp_cmd_alloc_info(info);
1651 if (!reply)
1652 return -ENOMEM;
1653
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001654 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001655 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001656 err = PTR_ERR(dp);
Jesse Grossccb13522011-10-25 19:26:31 -07001657 if (IS_ERR(dp))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001658 goto err_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001659
Thomas Graf43d4be92013-12-13 15:22:18 +01001660 ovs_dp_change(dp, info->attrs);
1661
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001662 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1663 info->snd_seq, 0, OVS_DP_CMD_NEW);
1664 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001665
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001666 ovs_unlock();
Johannes Berg2a94fe42013-11-19 15:19:39 +01001667 ovs_notify(&dp_datapath_genl_family, reply, info);
Jesse Grossccb13522011-10-25 19:26:31 -07001668
1669 return 0;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001670
1671err_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001672 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001673 kfree_skb(reply);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001674 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001675}
1676
1677static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1678{
1679 struct sk_buff *reply;
1680 struct datapath *dp;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001681 int err;
Jesse Grossccb13522011-10-25 19:26:31 -07001682
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001683 reply = ovs_dp_cmd_alloc_info(info);
1684 if (!reply)
1685 return -ENOMEM;
1686
Pravin B Shelar8ec609d2014-11-11 15:55:16 -08001687 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001688 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001689 if (IS_ERR(dp)) {
1690 err = PTR_ERR(dp);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001691 goto err_unlock_free;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001692 }
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001693 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1694 info->snd_seq, 0, OVS_DP_CMD_NEW);
1695 BUG_ON(err < 0);
Pravin B Shelar8ec609d2014-11-11 15:55:16 -08001696 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001697
Jesse Grossccb13522011-10-25 19:26:31 -07001698 return genlmsg_reply(reply, info);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001699
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001700err_unlock_free:
Pravin B Shelar8ec609d2014-11-11 15:55:16 -08001701 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001702 kfree_skb(reply);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001703 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001704}
1705
1706static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1707{
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001708 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
Jesse Grossccb13522011-10-25 19:26:31 -07001709 struct datapath *dp;
1710 int skip = cb->args[0];
1711 int i = 0;
1712
Pravin B Shelar8ec609d2014-11-11 15:55:16 -08001713 ovs_lock();
1714 list_for_each_entry(dp, &ovs_net->dps, list_node) {
Ben Pfaff77676fd2012-01-17 13:33:39 +00001715 if (i >= skip &&
Eric W. Biederman15e47302012-09-07 20:12:54 +00001716 ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001717 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1718 OVS_DP_CMD_NEW) < 0)
1719 break;
1720 i++;
1721 }
Pravin B Shelar8ec609d2014-11-11 15:55:16 -08001722 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001723
1724 cb->args[0] = i;
1725
1726 return skb->len;
1727}
1728
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001729static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1730 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1731 [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1732 [OVS_DP_ATTR_USER_FEATURES] = { .type = NLA_U32 },
1733};
1734
stephen hemminger48e48a72014-07-16 11:25:52 -07001735static const struct genl_ops dp_datapath_genl_ops[] = {
Jesse Grossccb13522011-10-25 19:26:31 -07001736 { .cmd = OVS_DP_CMD_NEW,
1737 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1738 .policy = datapath_policy,
1739 .doit = ovs_dp_cmd_new
1740 },
1741 { .cmd = OVS_DP_CMD_DEL,
1742 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1743 .policy = datapath_policy,
1744 .doit = ovs_dp_cmd_del
1745 },
1746 { .cmd = OVS_DP_CMD_GET,
1747 .flags = 0, /* OK for unprivileged users. */
1748 .policy = datapath_policy,
1749 .doit = ovs_dp_cmd_get,
1750 .dumpit = ovs_dp_cmd_dump
1751 },
1752 { .cmd = OVS_DP_CMD_SET,
1753 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1754 .policy = datapath_policy,
1755 .doit = ovs_dp_cmd_set,
1756 },
1757};
1758
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001759static struct genl_family dp_datapath_genl_family = {
Jesse Grossccb13522011-10-25 19:26:31 -07001760 .id = GENL_ID_GENERATE,
1761 .hdrsize = sizeof(struct ovs_header),
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001762 .name = OVS_DATAPATH_FAMILY,
1763 .version = OVS_DATAPATH_VERSION,
1764 .maxattr = OVS_DP_ATTR_MAX,
Pravin B Shelar3a4e0d62013-04-23 07:48:48 +00001765 .netnsok = true,
1766 .parallel_ops = true,
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001767 .ops = dp_datapath_genl_ops,
1768 .n_ops = ARRAY_SIZE(dp_datapath_genl_ops),
1769 .mcgrps = &ovs_dp_datapath_multicast_group,
1770 .n_mcgrps = 1,
Jesse Grossccb13522011-10-25 19:26:31 -07001771};
1772
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001773/* Called with ovs_mutex or RCU read lock. */
Jesse Grossccb13522011-10-25 19:26:31 -07001774static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001775 u32 portid, u32 seq, u32 flags, u8 cmd)
Jesse Grossccb13522011-10-25 19:26:31 -07001776{
1777 struct ovs_header *ovs_header;
1778 struct ovs_vport_stats vport_stats;
1779 int err;
1780
Eric W. Biederman15e47302012-09-07 20:12:54 +00001781 ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
Jesse Grossccb13522011-10-25 19:26:31 -07001782 flags, cmd);
1783 if (!ovs_header)
1784 return -EMSGSIZE;
1785
1786 ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1787
David S. Miller028d6a62012-03-29 23:20:48 -04001788 if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
1789 nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
Alex Wang5cd667b2014-07-17 15:14:13 -07001790 nla_put_string(skb, OVS_VPORT_ATTR_NAME,
1791 vport->ops->get_name(vport)))
David S. Miller028d6a62012-03-29 23:20:48 -04001792 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001793
1794 ovs_vport_get_stats(vport, &vport_stats);
David S. Miller028d6a62012-03-29 23:20:48 -04001795 if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
1796 &vport_stats))
1797 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001798
Alex Wang5cd667b2014-07-17 15:14:13 -07001799 if (ovs_vport_get_upcall_portids(vport, skb))
1800 goto nla_put_failure;
1801
Jesse Grossccb13522011-10-25 19:26:31 -07001802 err = ovs_vport_get_options(vport, skb);
1803 if (err == -EMSGSIZE)
1804 goto error;
1805
Johannes Berg053c0952015-01-16 22:09:00 +01001806 genlmsg_end(skb, ovs_header);
1807 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001808
1809nla_put_failure:
1810 err = -EMSGSIZE;
1811error:
1812 genlmsg_cancel(skb, ovs_header);
1813 return err;
1814}
1815
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001816static struct sk_buff *ovs_vport_cmd_alloc_info(void)
1817{
1818 return nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1819}
1820
1821/* Called with ovs_mutex, only via ovs_dp_notify_wq(). */
Eric W. Biederman15e47302012-09-07 20:12:54 +00001822struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001823 u32 seq, u8 cmd)
1824{
1825 struct sk_buff *skb;
1826 int retval;
1827
1828 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1829 if (!skb)
1830 return ERR_PTR(-ENOMEM);
1831
Eric W. Biederman15e47302012-09-07 20:12:54 +00001832 retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
Jesse Grossa9341512013-03-26 15:48:38 -07001833 BUG_ON(retval < 0);
1834
Jesse Grossccb13522011-10-25 19:26:31 -07001835 return skb;
1836}
1837
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001838/* Called with ovs_mutex or RCU read lock. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001839static struct vport *lookup_vport(struct net *net,
Thomas Graf12eb18f2014-11-06 06:58:52 -08001840 const struct ovs_header *ovs_header,
Jesse Grossccb13522011-10-25 19:26:31 -07001841 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1842{
1843 struct datapath *dp;
1844 struct vport *vport;
1845
1846 if (a[OVS_VPORT_ATTR_NAME]) {
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001847 vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
Jesse Grossccb13522011-10-25 19:26:31 -07001848 if (!vport)
1849 return ERR_PTR(-ENODEV);
Ben Pfaff651a68e2012-03-06 15:04:04 -08001850 if (ovs_header->dp_ifindex &&
1851 ovs_header->dp_ifindex != get_dpifindex(vport->dp))
1852 return ERR_PTR(-ENODEV);
Jesse Grossccb13522011-10-25 19:26:31 -07001853 return vport;
1854 } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
1855 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1856
1857 if (port_no >= DP_MAX_PORTS)
1858 return ERR_PTR(-EFBIG);
1859
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001860 dp = get_dp(net, ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001861 if (!dp)
1862 return ERR_PTR(-ENODEV);
1863
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001864 vport = ovs_vport_ovsl_rcu(dp, port_no);
Jesse Grossccb13522011-10-25 19:26:31 -07001865 if (!vport)
Jarno Rajahalme14408db2013-01-09 14:27:35 -08001866 return ERR_PTR(-ENODEV);
Jesse Grossccb13522011-10-25 19:26:31 -07001867 return vport;
1868 } else
1869 return ERR_PTR(-EINVAL);
1870}
1871
1872static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
1873{
1874 struct nlattr **a = info->attrs;
1875 struct ovs_header *ovs_header = info->userhdr;
1876 struct vport_parms parms;
1877 struct sk_buff *reply;
1878 struct vport *vport;
1879 struct datapath *dp;
1880 u32 port_no;
1881 int err;
1882
Jesse Grossccb13522011-10-25 19:26:31 -07001883 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
1884 !a[OVS_VPORT_ATTR_UPCALL_PID])
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001885 return -EINVAL;
1886
1887 port_no = a[OVS_VPORT_ATTR_PORT_NO]
1888 ? nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]) : 0;
1889 if (port_no >= DP_MAX_PORTS)
1890 return -EFBIG;
1891
1892 reply = ovs_vport_cmd_alloc_info();
1893 if (!reply)
1894 return -ENOMEM;
Jesse Grossccb13522011-10-25 19:26:31 -07001895
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001896 ovs_lock();
Thomas Graf62b9c8d2014-10-22 17:29:06 +02001897restart:
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001898 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001899 err = -ENODEV;
1900 if (!dp)
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001901 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001902
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001903 if (port_no) {
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001904 vport = ovs_vport_ovsl(dp, port_no);
Jesse Grossccb13522011-10-25 19:26:31 -07001905 err = -EBUSY;
1906 if (vport)
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001907 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001908 } else {
1909 for (port_no = 1; ; port_no++) {
1910 if (port_no >= DP_MAX_PORTS) {
1911 err = -EFBIG;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001912 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001913 }
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001914 vport = ovs_vport_ovsl(dp, port_no);
Jesse Grossccb13522011-10-25 19:26:31 -07001915 if (!vport)
1916 break;
1917 }
1918 }
1919
1920 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
1921 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1922 parms.options = a[OVS_VPORT_ATTR_OPTIONS];
1923 parms.dp = dp;
1924 parms.port_no = port_no;
Alex Wang5cd667b2014-07-17 15:14:13 -07001925 parms.upcall_portids = a[OVS_VPORT_ATTR_UPCALL_PID];
Jesse Grossccb13522011-10-25 19:26:31 -07001926
1927 vport = new_vport(&parms);
1928 err = PTR_ERR(vport);
Thomas Graf62b9c8d2014-10-22 17:29:06 +02001929 if (IS_ERR(vport)) {
1930 if (err == -EAGAIN)
1931 goto restart;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001932 goto exit_unlock_free;
Thomas Graf62b9c8d2014-10-22 17:29:06 +02001933 }
Jesse Grossccb13522011-10-25 19:26:31 -07001934
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001935 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1936 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1937 BUG_ON(err < 0);
1938 ovs_unlock();
Thomas Grafed661182013-03-29 14:46:50 +01001939
Johannes Berg2a94fe42013-11-19 15:19:39 +01001940 ovs_notify(&dp_vport_genl_family, reply, info);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001941 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001942
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001943exit_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001944 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001945 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001946 return err;
1947}
1948
1949static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
1950{
1951 struct nlattr **a = info->attrs;
1952 struct sk_buff *reply;
1953 struct vport *vport;
1954 int err;
1955
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001956 reply = ovs_vport_cmd_alloc_info();
1957 if (!reply)
1958 return -ENOMEM;
1959
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001960 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001961 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
Jesse Grossccb13522011-10-25 19:26:31 -07001962 err = PTR_ERR(vport);
1963 if (IS_ERR(vport))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001964 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001965
Jesse Grossccb13522011-10-25 19:26:31 -07001966 if (a[OVS_VPORT_ATTR_TYPE] &&
Jesse Grossf44f3402013-05-13 08:15:26 -07001967 nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
Jesse Grossccb13522011-10-25 19:26:31 -07001968 err = -EINVAL;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001969 goto exit_unlock_free;
Jesse Grossa9341512013-03-26 15:48:38 -07001970 }
1971
Jesse Grossf44f3402013-05-13 08:15:26 -07001972 if (a[OVS_VPORT_ATTR_OPTIONS]) {
Jesse Grossccb13522011-10-25 19:26:31 -07001973 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
Jesse Grossf44f3402013-05-13 08:15:26 -07001974 if (err)
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001975 goto exit_unlock_free;
Jesse Grossf44f3402013-05-13 08:15:26 -07001976 }
Jesse Grossa9341512013-03-26 15:48:38 -07001977
Alex Wang5cd667b2014-07-17 15:14:13 -07001978
1979 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
1980 struct nlattr *ids = a[OVS_VPORT_ATTR_UPCALL_PID];
1981
1982 err = ovs_vport_set_upcall_portids(vport, ids);
1983 if (err)
1984 goto exit_unlock_free;
1985 }
Jesse Grossccb13522011-10-25 19:26:31 -07001986
Jesse Grossa9341512013-03-26 15:48:38 -07001987 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1988 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1989 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001990
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001991 ovs_unlock();
Johannes Berg2a94fe42013-11-19 15:19:39 +01001992 ovs_notify(&dp_vport_genl_family, reply, info);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001993 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001994
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001995exit_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001996 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001997 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001998 return err;
1999}
2000
2001static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
2002{
2003 struct nlattr **a = info->attrs;
2004 struct sk_buff *reply;
2005 struct vport *vport;
2006 int err;
2007
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07002008 reply = ovs_vport_cmd_alloc_info();
2009 if (!reply)
2010 return -ENOMEM;
2011
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002012 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002013 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
Jesse Grossccb13522011-10-25 19:26:31 -07002014 err = PTR_ERR(vport);
2015 if (IS_ERR(vport))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07002016 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07002017
2018 if (vport->port_no == OVSP_LOCAL) {
2019 err = -EINVAL;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07002020 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07002021 }
2022
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07002023 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
2024 info->snd_seq, 0, OVS_VPORT_CMD_DEL);
2025 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07002026 ovs_dp_detach_port(vport);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07002027 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07002028
Johannes Berg2a94fe42013-11-19 15:19:39 +01002029 ovs_notify(&dp_vport_genl_family, reply, info);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07002030 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -07002031
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07002032exit_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002033 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07002034 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07002035 return err;
2036}
2037
2038static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
2039{
2040 struct nlattr **a = info->attrs;
2041 struct ovs_header *ovs_header = info->userhdr;
2042 struct sk_buff *reply;
2043 struct vport *vport;
2044 int err;
2045
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07002046 reply = ovs_vport_cmd_alloc_info();
2047 if (!reply)
2048 return -ENOMEM;
2049
Jesse Grossccb13522011-10-25 19:26:31 -07002050 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002051 vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
Jesse Grossccb13522011-10-25 19:26:31 -07002052 err = PTR_ERR(vport);
2053 if (IS_ERR(vport))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07002054 goto exit_unlock_free;
2055 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
2056 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
2057 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07002058 rcu_read_unlock();
2059
2060 return genlmsg_reply(reply, info);
2061
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07002062exit_unlock_free:
Jesse Grossccb13522011-10-25 19:26:31 -07002063 rcu_read_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07002064 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07002065 return err;
2066}
2067
2068static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
2069{
2070 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
2071 struct datapath *dp;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07002072 int bucket = cb->args[0], skip = cb->args[1];
2073 int i, j = 0;
Jesse Grossccb13522011-10-25 19:26:31 -07002074
Jesse Grossccb13522011-10-25 19:26:31 -07002075 rcu_read_lock();
Andy Zhoucc3a5ae2014-09-08 13:14:22 -07002076 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
Jarno Rajahalme42ee19e2014-02-15 17:42:29 -08002077 if (!dp) {
2078 rcu_read_unlock();
2079 return -ENODEV;
2080 }
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07002081 for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
Jesse Grossccb13522011-10-25 19:26:31 -07002082 struct vport *vport;
2083
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07002084 j = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -08002085 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07002086 if (j >= skip &&
2087 ovs_vport_cmd_fill_info(vport, skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00002088 NETLINK_CB(cb->skb).portid,
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07002089 cb->nlh->nlmsg_seq,
2090 NLM_F_MULTI,
2091 OVS_VPORT_CMD_NEW) < 0)
2092 goto out;
Jesse Grossccb13522011-10-25 19:26:31 -07002093
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07002094 j++;
2095 }
2096 skip = 0;
Jesse Grossccb13522011-10-25 19:26:31 -07002097 }
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07002098out:
Jesse Grossccb13522011-10-25 19:26:31 -07002099 rcu_read_unlock();
2100
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07002101 cb->args[0] = i;
2102 cb->args[1] = j;
Jesse Grossccb13522011-10-25 19:26:31 -07002103
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07002104 return skb->len;
Jesse Grossccb13522011-10-25 19:26:31 -07002105}
2106
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002107static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
2108 [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
2109 [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
2110 [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
2111 [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
2112 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
2113 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
2114};
2115
stephen hemminger48e48a72014-07-16 11:25:52 -07002116static const struct genl_ops dp_vport_genl_ops[] = {
Jesse Grossccb13522011-10-25 19:26:31 -07002117 { .cmd = OVS_VPORT_CMD_NEW,
2118 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2119 .policy = vport_policy,
2120 .doit = ovs_vport_cmd_new
2121 },
2122 { .cmd = OVS_VPORT_CMD_DEL,
2123 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2124 .policy = vport_policy,
2125 .doit = ovs_vport_cmd_del
2126 },
2127 { .cmd = OVS_VPORT_CMD_GET,
2128 .flags = 0, /* OK for unprivileged users. */
2129 .policy = vport_policy,
2130 .doit = ovs_vport_cmd_get,
2131 .dumpit = ovs_vport_cmd_dump
2132 },
2133 { .cmd = OVS_VPORT_CMD_SET,
2134 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2135 .policy = vport_policy,
2136 .doit = ovs_vport_cmd_set,
2137 },
2138};
2139
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002140struct genl_family dp_vport_genl_family = {
2141 .id = GENL_ID_GENERATE,
2142 .hdrsize = sizeof(struct ovs_header),
2143 .name = OVS_VPORT_FAMILY,
2144 .version = OVS_VPORT_VERSION,
2145 .maxattr = OVS_VPORT_ATTR_MAX,
2146 .netnsok = true,
2147 .parallel_ops = true,
2148 .ops = dp_vport_genl_ops,
2149 .n_ops = ARRAY_SIZE(dp_vport_genl_ops),
2150 .mcgrps = &ovs_dp_vport_multicast_group,
2151 .n_mcgrps = 1,
Jesse Grossccb13522011-10-25 19:26:31 -07002152};
2153
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002154static struct genl_family * const dp_genl_families[] = {
2155 &dp_datapath_genl_family,
2156 &dp_vport_genl_family,
2157 &dp_flow_genl_family,
2158 &dp_packet_genl_family,
Jesse Grossccb13522011-10-25 19:26:31 -07002159};
2160
2161static void dp_unregister_genl(int n_families)
2162{
2163 int i;
2164
2165 for (i = 0; i < n_families; i++)
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002166 genl_unregister_family(dp_genl_families[i]);
Jesse Grossccb13522011-10-25 19:26:31 -07002167}
2168
2169static int dp_register_genl(void)
2170{
Jesse Grossccb13522011-10-25 19:26:31 -07002171 int err;
2172 int i;
2173
Jesse Grossccb13522011-10-25 19:26:31 -07002174 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
Jesse Grossccb13522011-10-25 19:26:31 -07002175
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002176 err = genl_register_family(dp_genl_families[i]);
Jesse Grossccb13522011-10-25 19:26:31 -07002177 if (err)
2178 goto error;
Jesse Grossccb13522011-10-25 19:26:31 -07002179 }
2180
2181 return 0;
2182
2183error:
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002184 dp_unregister_genl(i);
Jesse Grossccb13522011-10-25 19:26:31 -07002185 return err;
2186}
2187
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002188static int __net_init ovs_init_net(struct net *net)
2189{
2190 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2191
2192 INIT_LIST_HEAD(&ovs_net->dps);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002193 INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002194 return 0;
2195}
2196
Pravin B Shelar7b4577a2015-02-17 11:23:10 -08002197static void __net_exit list_vports_from_net(struct net *net, struct net *dnet,
2198 struct list_head *head)
2199{
2200 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2201 struct datapath *dp;
2202
2203 list_for_each_entry(dp, &ovs_net->dps, list_node) {
2204 int i;
2205
2206 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
2207 struct vport *vport;
2208
2209 hlist_for_each_entry(vport, &dp->ports[i], dp_hash_node) {
2210 struct netdev_vport *netdev_vport;
2211
2212 if (vport->ops->type != OVS_VPORT_TYPE_INTERNAL)
2213 continue;
2214
2215 netdev_vport = netdev_vport_priv(vport);
2216 if (dev_net(netdev_vport->dev) == dnet)
2217 list_add(&vport->detach_list, head);
2218 }
2219 }
2220 }
2221}
2222
2223static void __net_exit ovs_exit_net(struct net *dnet)
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002224{
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002225 struct datapath *dp, *dp_next;
Pravin B Shelar7b4577a2015-02-17 11:23:10 -08002226 struct ovs_net *ovs_net = net_generic(dnet, ovs_net_id);
2227 struct vport *vport, *vport_next;
2228 struct net *net;
2229 LIST_HEAD(head);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002230
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002231 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002232 list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
2233 __dp_destroy(dp);
Pravin B Shelar7b4577a2015-02-17 11:23:10 -08002234
2235 rtnl_lock();
2236 for_each_net(net)
2237 list_vports_from_net(net, dnet, &head);
2238 rtnl_unlock();
2239
2240 /* Detach all vports from given namespace. */
2241 list_for_each_entry_safe(vport, vport_next, &head, detach_list) {
2242 list_del(&vport->detach_list);
2243 ovs_dp_detach_port(vport);
2244 }
2245
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002246 ovs_unlock();
2247
2248 cancel_work_sync(&ovs_net->dp_notify_work);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002249}
2250
2251static struct pernet_operations ovs_net_ops = {
2252 .init = ovs_init_net,
2253 .exit = ovs_exit_net,
2254 .id = &ovs_net_id,
2255 .size = sizeof(struct ovs_net),
2256};
2257
Jesse Grossccb13522011-10-25 19:26:31 -07002258static int __init dp_init(void)
2259{
Jesse Grossccb13522011-10-25 19:26:31 -07002260 int err;
2261
YOSHIFUJI Hideaki / 吉藤英明3523b292013-01-09 07:19:55 +00002262 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
Jesse Grossccb13522011-10-25 19:26:31 -07002263
2264 pr_info("Open vSwitch switching datapath\n");
2265
Andy Zhou971427f32014-09-15 19:37:25 -07002266 err = action_fifos_init();
Jesse Grossccb13522011-10-25 19:26:31 -07002267 if (err)
2268 goto error;
2269
Andy Zhou971427f32014-09-15 19:37:25 -07002270 err = ovs_internal_dev_rtnl_link_register();
2271 if (err)
2272 goto error_action_fifos_exit;
2273
Jiri Pirko5b9e7e12014-06-26 09:58:26 +02002274 err = ovs_flow_init();
2275 if (err)
2276 goto error_unreg_rtnl_link;
2277
Jesse Grossccb13522011-10-25 19:26:31 -07002278 err = ovs_vport_init();
2279 if (err)
2280 goto error_flow_exit;
2281
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002282 err = register_pernet_device(&ovs_net_ops);
Jesse Grossccb13522011-10-25 19:26:31 -07002283 if (err)
2284 goto error_vport_exit;
2285
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002286 err = register_netdevice_notifier(&ovs_dp_device_notifier);
2287 if (err)
2288 goto error_netns_exit;
2289
Thomas Graf62b9c8d2014-10-22 17:29:06 +02002290 err = ovs_netdev_init();
2291 if (err)
2292 goto error_unreg_notifier;
2293
Jesse Grossccb13522011-10-25 19:26:31 -07002294 err = dp_register_genl();
2295 if (err < 0)
Thomas Graf62b9c8d2014-10-22 17:29:06 +02002296 goto error_unreg_netdev;
Jesse Grossccb13522011-10-25 19:26:31 -07002297
Jesse Grossccb13522011-10-25 19:26:31 -07002298 return 0;
2299
Thomas Graf62b9c8d2014-10-22 17:29:06 +02002300error_unreg_netdev:
2301 ovs_netdev_exit();
Jesse Grossccb13522011-10-25 19:26:31 -07002302error_unreg_notifier:
2303 unregister_netdevice_notifier(&ovs_dp_device_notifier);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002304error_netns_exit:
2305 unregister_pernet_device(&ovs_net_ops);
Jesse Grossccb13522011-10-25 19:26:31 -07002306error_vport_exit:
2307 ovs_vport_exit();
2308error_flow_exit:
2309 ovs_flow_exit();
Jiri Pirko5b9e7e12014-06-26 09:58:26 +02002310error_unreg_rtnl_link:
2311 ovs_internal_dev_rtnl_link_unregister();
Andy Zhou971427f32014-09-15 19:37:25 -07002312error_action_fifos_exit:
2313 action_fifos_exit();
Jesse Grossccb13522011-10-25 19:26:31 -07002314error:
2315 return err;
2316}
2317
2318static void dp_cleanup(void)
2319{
Jesse Grossccb13522011-10-25 19:26:31 -07002320 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
Thomas Graf62b9c8d2014-10-22 17:29:06 +02002321 ovs_netdev_exit();
Jesse Grossccb13522011-10-25 19:26:31 -07002322 unregister_netdevice_notifier(&ovs_dp_device_notifier);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002323 unregister_pernet_device(&ovs_net_ops);
2324 rcu_barrier();
Jesse Grossccb13522011-10-25 19:26:31 -07002325 ovs_vport_exit();
2326 ovs_flow_exit();
Jiri Pirko5b9e7e12014-06-26 09:58:26 +02002327 ovs_internal_dev_rtnl_link_unregister();
Andy Zhou971427f32014-09-15 19:37:25 -07002328 action_fifos_exit();
Jesse Grossccb13522011-10-25 19:26:31 -07002329}
2330
2331module_init(dp_init);
2332module_exit(dp_cleanup);
2333
2334MODULE_DESCRIPTION("Open vSwitch switching datapath");
2335MODULE_LICENSE("GPL");