blob: 3b90461317ec36bbc560c9ff43654846fb423c82 [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 Shelar15eac2a2012-08-23 12:40:54 -0700206 kfree(dp->ports);
Jesse Grossccb13522011-10-25 19:26:31 -0700207 kfree(dp);
208}
209
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700210static struct hlist_head *vport_hash_bucket(const struct datapath *dp,
211 u16 port_no)
212{
213 return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)];
214}
215
Jarno Rajahalmebb6f9a72014-05-05 11:32:17 -0700216/* Called with ovs_mutex or RCU read lock. */
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700217struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no)
218{
219 struct vport *vport;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700220 struct hlist_head *head;
221
222 head = vport_hash_bucket(dp, port_no);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800223 hlist_for_each_entry_rcu(vport, head, dp_hash_node) {
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700224 if (vport->port_no == port_no)
225 return vport;
226 }
227 return NULL;
228}
229
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700230/* Called with ovs_mutex. */
Jesse Grossccb13522011-10-25 19:26:31 -0700231static struct vport *new_vport(const struct vport_parms *parms)
232{
233 struct vport *vport;
234
235 vport = ovs_vport_add(parms);
236 if (!IS_ERR(vport)) {
237 struct datapath *dp = parms->dp;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700238 struct hlist_head *head = vport_hash_bucket(dp, vport->port_no);
Jesse Grossccb13522011-10-25 19:26:31 -0700239
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700240 hlist_add_head_rcu(&vport->dp_hash_node, head);
Jesse Grossccb13522011-10-25 19:26:31 -0700241 }
Jesse Grossccb13522011-10-25 19:26:31 -0700242 return vport;
243}
244
Jesse Grossccb13522011-10-25 19:26:31 -0700245void ovs_dp_detach_port(struct vport *p)
246{
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700247 ASSERT_OVSL();
Jesse Grossccb13522011-10-25 19:26:31 -0700248
249 /* First drop references to device. */
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700250 hlist_del_rcu(&p->dp_hash_node);
Jesse Grossccb13522011-10-25 19:26:31 -0700251
252 /* Then destroy it. */
253 ovs_vport_del(p);
254}
255
256/* Must be called with rcu_read_lock. */
Pravin B Shelar8c8b1b82014-09-15 19:28:44 -0700257void ovs_dp_process_packet(struct sk_buff *skb, struct sw_flow_key *key)
Jesse Grossccb13522011-10-25 19:26:31 -0700258{
Pravin B Shelar83c8df22014-09-15 19:20:31 -0700259 const struct vport *p = OVS_CB(skb)->input_vport;
Jesse Grossccb13522011-10-25 19:26:31 -0700260 struct datapath *dp = p->dp;
261 struct sw_flow *flow;
Lorand Jakabd98612b2014-10-06 05:45:32 -0700262 struct sw_flow_actions *sf_acts;
Jesse Grossccb13522011-10-25 19:26:31 -0700263 struct dp_stats_percpu *stats;
Jesse Grossccb13522011-10-25 19:26:31 -0700264 u64 *stats_counter;
Andy Zhou1bd71162013-10-22 10:42:46 -0700265 u32 n_mask_hit;
Jesse Grossccb13522011-10-25 19:26:31 -0700266
Shan Wei404f2f12012-11-13 09:52:25 +0800267 stats = this_cpu_ptr(dp->stats_percpu);
Jesse Grossccb13522011-10-25 19:26:31 -0700268
Jesse Grossccb13522011-10-25 19:26:31 -0700269 /* Look up flow. */
Pravin B Shelar8c8b1b82014-09-15 19:28:44 -0700270 flow = ovs_flow_tbl_lookup_stats(&dp->table, key, &n_mask_hit);
Jesse Grossccb13522011-10-25 19:26:31 -0700271 if (unlikely(!flow)) {
272 struct dp_upcall_info upcall;
Pravin B Shelar8c8b1b82014-09-15 19:28:44 -0700273 int error;
Jesse Grossccb13522011-10-25 19:26:31 -0700274
275 upcall.cmd = OVS_PACKET_CMD_MISS;
Jesse Grossccb13522011-10-25 19:26:31 -0700276 upcall.userdata = NULL;
Alex Wang5cd667b2014-07-17 15:14:13 -0700277 upcall.portid = ovs_vport_find_upcall_portid(p, skb);
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800278 upcall.egress_tun_info = NULL;
Pravin B Shelare8eedb82014-11-06 06:57:27 -0800279 error = ovs_dp_upcall(dp, skb, key, &upcall);
Li RongQingc5eba0b2014-09-03 17:43:45 +0800280 if (unlikely(error))
281 kfree_skb(skb);
282 else
283 consume_skb(skb);
Jesse Grossccb13522011-10-25 19:26:31 -0700284 stats_counter = &stats->n_missed;
285 goto out;
286 }
287
Lorand Jakabd98612b2014-10-06 05:45:32 -0700288 ovs_flow_stats_update(flow, key->tp.flags, skb);
289 sf_acts = rcu_dereference(flow->sf_acts);
290 ovs_execute_actions(dp, skb, sf_acts, key);
Jesse Grossccb13522011-10-25 19:26:31 -0700291
Pravin B Shelare298e502013-10-29 17:22:21 -0700292 stats_counter = &stats->n_hit;
Jesse Grossccb13522011-10-25 19:26:31 -0700293
294out:
295 /* Update datapath statistics. */
WANG Congdf9d9fd2014-02-14 15:10:46 -0800296 u64_stats_update_begin(&stats->syncp);
Jesse Grossccb13522011-10-25 19:26:31 -0700297 (*stats_counter)++;
Andy Zhou1bd71162013-10-22 10:42:46 -0700298 stats->n_mask_hit += n_mask_hit;
WANG Congdf9d9fd2014-02-14 15:10:46 -0800299 u64_stats_update_end(&stats->syncp);
Jesse Grossccb13522011-10-25 19:26:31 -0700300}
301
Jesse Grossccb13522011-10-25 19:26:31 -0700302int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
Pravin B Shelare8eedb82014-11-06 06:57:27 -0800303 const struct sw_flow_key *key,
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800304 const struct dp_upcall_info *upcall_info)
Jesse Grossccb13522011-10-25 19:26:31 -0700305{
306 struct dp_stats_percpu *stats;
Jesse Grossccb13522011-10-25 19:26:31 -0700307 int err;
308
Eric W. Biederman15e47302012-09-07 20:12:54 +0000309 if (upcall_info->portid == 0) {
Jesse Grossccb13522011-10-25 19:26:31 -0700310 err = -ENOTCONN;
311 goto err;
312 }
313
Jesse Grossccb13522011-10-25 19:26:31 -0700314 if (!skb_is_gso(skb))
Pravin B Shelare8eedb82014-11-06 06:57:27 -0800315 err = queue_userspace_packet(dp, skb, key, upcall_info);
Jesse Grossccb13522011-10-25 19:26:31 -0700316 else
Pravin B Shelare8eedb82014-11-06 06:57:27 -0800317 err = queue_gso_packets(dp, skb, key, upcall_info);
Jesse Grossccb13522011-10-25 19:26:31 -0700318 if (err)
319 goto err;
320
321 return 0;
322
323err:
Shan Wei404f2f12012-11-13 09:52:25 +0800324 stats = this_cpu_ptr(dp->stats_percpu);
Jesse Grossccb13522011-10-25 19:26:31 -0700325
WANG Congdf9d9fd2014-02-14 15:10:46 -0800326 u64_stats_update_begin(&stats->syncp);
Jesse Grossccb13522011-10-25 19:26:31 -0700327 stats->n_lost++;
WANG Congdf9d9fd2014-02-14 15:10:46 -0800328 u64_stats_update_end(&stats->syncp);
Jesse Grossccb13522011-10-25 19:26:31 -0700329
330 return err;
331}
332
Thomas Graf8055a892013-12-13 15:22:20 +0100333static int queue_gso_packets(struct datapath *dp, struct sk_buff *skb,
Pravin B Shelare8eedb82014-11-06 06:57:27 -0800334 const struct sw_flow_key *key,
Jesse Grossccb13522011-10-25 19:26:31 -0700335 const struct dp_upcall_info *upcall_info)
336{
Ben Pfaffa1b5d0d2012-07-20 14:47:54 -0700337 unsigned short gso_type = skb_shinfo(skb)->gso_type;
Jesse Grossccb13522011-10-25 19:26:31 -0700338 struct sw_flow_key later_key;
339 struct sk_buff *segs, *nskb;
Pravin B Shelare8eedb82014-11-06 06:57:27 -0800340 struct ovs_skb_cb ovs_cb;
Jesse Grossccb13522011-10-25 19:26:31 -0700341 int err;
342
Pravin B Shelare8eedb82014-11-06 06:57:27 -0800343 ovs_cb = *OVS_CB(skb);
Thomas Graf09c5e602013-12-13 15:22:22 +0100344 segs = __skb_gso_segment(skb, NETIF_F_SG, false);
Pravin B Shelare8eedb82014-11-06 06:57:27 -0800345 *OVS_CB(skb) = ovs_cb;
Pravin B Shelar92e5dfc2012-07-20 14:46:29 -0700346 if (IS_ERR(segs))
347 return PTR_ERR(segs);
Florian Westphal330966e2014-10-20 13:49:17 +0200348 if (segs == NULL)
349 return -EINVAL;
Jesse Grossccb13522011-10-25 19:26:31 -0700350
Pravin B Shelare8eedb82014-11-06 06:57:27 -0800351 if (gso_type & SKB_GSO_UDP) {
352 /* The initial flow key extracted by ovs_flow_key_extract()
353 * in this case is for a first fragment, so we need to
354 * properly mark later fragments.
355 */
356 later_key = *key;
357 later_key.ip.frag = OVS_FRAG_TYPE_LATER;
358 }
359
Jesse Grossccb13522011-10-25 19:26:31 -0700360 /* Queue all of the segments. */
361 skb = segs;
362 do {
Pravin B Shelare8eedb82014-11-06 06:57:27 -0800363 *OVS_CB(skb) = ovs_cb;
364 if (gso_type & SKB_GSO_UDP && skb != segs)
365 key = &later_key;
366
367 err = queue_userspace_packet(dp, skb, key, upcall_info);
Jesse Grossccb13522011-10-25 19:26:31 -0700368 if (err)
369 break;
370
Jesse Grossccb13522011-10-25 19:26:31 -0700371 } while ((skb = skb->next));
372
373 /* Free all of the segments. */
374 skb = segs;
375 do {
376 nskb = skb->next;
377 if (err)
378 kfree_skb(skb);
379 else
380 consume_skb(skb);
381 } while ((skb = nskb));
382 return err;
383}
384
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800385static size_t upcall_msg_size(const struct dp_upcall_info *upcall_info,
Thomas Grafbda56f12013-12-13 15:22:21 +0100386 unsigned int hdrlen)
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100387{
388 size_t size = NLMSG_ALIGN(sizeof(struct ovs_header))
Thomas Grafbda56f12013-12-13 15:22:21 +0100389 + nla_total_size(hdrlen) /* OVS_PACKET_ATTR_PACKET */
Joe Stringer41af73e2014-10-18 16:14:14 -0700390 + nla_total_size(ovs_key_attr_size()); /* OVS_PACKET_ATTR_KEY */
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100391
392 /* OVS_PACKET_ATTR_USERDATA */
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800393 if (upcall_info->userdata)
394 size += NLA_ALIGN(upcall_info->userdata->nla_len);
395
396 /* OVS_PACKET_ATTR_EGRESS_TUN_KEY */
397 if (upcall_info->egress_tun_info)
398 size += nla_total_size(ovs_tun_key_attr_size());
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100399
400 return size;
401}
402
Thomas Graf8055a892013-12-13 15:22:20 +0100403static int queue_userspace_packet(struct datapath *dp, struct sk_buff *skb,
Pravin B Shelare8eedb82014-11-06 06:57:27 -0800404 const struct sw_flow_key *key,
Jesse Grossccb13522011-10-25 19:26:31 -0700405 const struct dp_upcall_info *upcall_info)
406{
407 struct ovs_header *upcall;
408 struct sk_buff *nskb = NULL;
Li RongQing4ee45ea2014-09-02 20:52:28 +0800409 struct sk_buff *user_skb = NULL; /* to be queued to userspace */
Jesse Grossccb13522011-10-25 19:26:31 -0700410 struct nlattr *nla;
Thomas Graf795449d2013-11-30 13:21:32 +0100411 struct genl_info info = {
Thomas Graf8055a892013-12-13 15:22:20 +0100412 .dst_sk = ovs_dp_get_net(dp)->genl_sock,
Thomas Graf795449d2013-11-30 13:21:32 +0100413 .snd_portid = upcall_info->portid,
414 };
415 size_t len;
Thomas Grafbda56f12013-12-13 15:22:21 +0100416 unsigned int hlen;
Thomas Graf8055a892013-12-13 15:22:20 +0100417 int err, dp_ifindex;
418
419 dp_ifindex = get_dpifindex(dp);
420 if (!dp_ifindex)
421 return -ENODEV;
Jesse Grossccb13522011-10-25 19:26:31 -0700422
Jiri Pirkodf8a39d2015-01-13 17:13:44 +0100423 if (skb_vlan_tag_present(skb)) {
Jesse Grossccb13522011-10-25 19:26:31 -0700424 nskb = skb_clone(skb, GFP_ATOMIC);
425 if (!nskb)
426 return -ENOMEM;
427
Jiri Pirko59682502014-11-19 14:04:59 +0100428 nskb = __vlan_hwaccel_push_inside(nskb);
Dan Carpenter8aa51d62012-05-13 08:44:18 +0000429 if (!nskb)
Jesse Grossccb13522011-10-25 19:26:31 -0700430 return -ENOMEM;
431
Jesse Grossccb13522011-10-25 19:26:31 -0700432 skb = nskb;
433 }
434
435 if (nla_attr_size(skb->len) > USHRT_MAX) {
436 err = -EFBIG;
437 goto out;
438 }
439
Thomas Grafbda56f12013-12-13 15:22:21 +0100440 /* Complete checksum if needed */
441 if (skb->ip_summed == CHECKSUM_PARTIAL &&
442 (err = skb_checksum_help(skb)))
443 goto out;
444
445 /* Older versions of OVS user space enforce alignment of the last
446 * Netlink attribute to NLA_ALIGNTO which would require extensive
447 * padding logic. Only perform zerocopy if padding is not required.
448 */
449 if (dp->user_features & OVS_DP_F_UNALIGNED)
450 hlen = skb_zerocopy_headlen(skb);
451 else
452 hlen = skb->len;
453
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800454 len = upcall_msg_size(upcall_info, hlen);
Thomas Graf795449d2013-11-30 13:21:32 +0100455 user_skb = genlmsg_new_unicast(len, &info, GFP_ATOMIC);
Jesse Grossccb13522011-10-25 19:26:31 -0700456 if (!user_skb) {
457 err = -ENOMEM;
458 goto out;
459 }
460
461 upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
462 0, upcall_info->cmd);
463 upcall->dp_ifindex = dp_ifindex;
464
Joe Stringer5b4237b2015-01-21 16:42:48 -0800465 err = ovs_nla_put_key(key, key, OVS_PACKET_ATTR_KEY, false, user_skb);
Andy Zhouf53e3832014-07-17 15:17:44 -0700466 BUG_ON(err);
Jesse Grossccb13522011-10-25 19:26:31 -0700467
468 if (upcall_info->userdata)
Ben Pfaff44901082013-02-15 17:29:22 -0800469 __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA,
470 nla_len(upcall_info->userdata),
471 nla_data(upcall_info->userdata));
Jesse Grossccb13522011-10-25 19:26:31 -0700472
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800473 if (upcall_info->egress_tun_info) {
474 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_EGRESS_TUN_KEY);
475 err = ovs_nla_put_egress_tunnel_key(user_skb,
476 upcall_info->egress_tun_info);
477 BUG_ON(err);
478 nla_nest_end(user_skb, nla);
479 }
480
Thomas Grafbda56f12013-12-13 15:22:21 +0100481 /* Only reserve room for attribute header, packet data is added
482 * in skb_zerocopy() */
483 if (!(nla = nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, 0))) {
484 err = -ENOBUFS;
485 goto out;
486 }
487 nla->nla_len = nla_attr_size(skb->len);
Jesse Grossccb13522011-10-25 19:26:31 -0700488
Zoltan Kiss36d5fe62014-03-26 22:37:45 +0000489 err = skb_zerocopy(user_skb, skb, skb->len, hlen);
490 if (err)
491 goto out;
Jesse Grossccb13522011-10-25 19:26:31 -0700492
Thomas Grafaea0bb42014-01-14 16:27:49 +0000493 /* Pad OVS_PACKET_ATTR_PACKET if linear copy was performed */
494 if (!(dp->user_features & OVS_DP_F_UNALIGNED)) {
495 size_t plen = NLA_ALIGN(user_skb->len) - user_skb->len;
496
497 if (plen > 0)
498 memset(skb_put(user_skb, plen), 0, plen);
499 }
500
Thomas Grafbda56f12013-12-13 15:22:21 +0100501 ((struct nlmsghdr *) user_skb->data)->nlmsg_len = user_skb->len;
502
Thomas Graf8055a892013-12-13 15:22:20 +0100503 err = genlmsg_unicast(ovs_dp_get_net(dp), user_skb, upcall_info->portid);
Li RongQing4ee45ea2014-09-02 20:52:28 +0800504 user_skb = NULL;
Jesse Grossccb13522011-10-25 19:26:31 -0700505out:
Zoltan Kiss36d5fe62014-03-26 22:37:45 +0000506 if (err)
507 skb_tx_error(skb);
Li RongQing4ee45ea2014-09-02 20:52:28 +0800508 kfree_skb(user_skb);
Jesse Grossccb13522011-10-25 19:26:31 -0700509 kfree_skb(nskb);
510 return err;
511}
512
Jesse Grossccb13522011-10-25 19:26:31 -0700513static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
514{
515 struct ovs_header *ovs_header = info->userhdr;
516 struct nlattr **a = info->attrs;
517 struct sw_flow_actions *acts;
518 struct sk_buff *packet;
519 struct sw_flow *flow;
Lorand Jakabd98612b2014-10-06 05:45:32 -0700520 struct sw_flow_actions *sf_acts;
Jesse Grossccb13522011-10-25 19:26:31 -0700521 struct datapath *dp;
522 struct ethhdr *eth;
Pravin B Shelar83c8df22014-09-15 19:20:31 -0700523 struct vport *input_vport;
Jesse Grossccb13522011-10-25 19:26:31 -0700524 int len;
525 int err;
Thomas Graf1ba39802015-01-14 13:56:19 +0000526 bool log = !a[OVS_PACKET_ATTR_PROBE];
Jesse Grossccb13522011-10-25 19:26:31 -0700527
528 err = -EINVAL;
529 if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
Thomas Grafdded45fc2013-03-29 14:46:47 +0100530 !a[OVS_PACKET_ATTR_ACTIONS])
Jesse Grossccb13522011-10-25 19:26:31 -0700531 goto err;
532
533 len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
534 packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
535 err = -ENOMEM;
536 if (!packet)
537 goto err;
538 skb_reserve(packet, NET_IP_ALIGN);
539
Thomas Graf32686a92013-03-29 14:46:48 +0100540 nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
Jesse Grossccb13522011-10-25 19:26:31 -0700541
542 skb_reset_mac_header(packet);
543 eth = eth_hdr(packet);
544
545 /* Normally, setting the skb 'protocol' field would be handled by a
546 * call to eth_type_trans(), but it assumes there's a sending
547 * device, which we may not have. */
Alexander Duyck6713fc92015-05-04 14:34:05 -0700548 if (eth_proto_is_802_3(eth->h_proto))
Jesse Grossccb13522011-10-25 19:26:31 -0700549 packet->protocol = eth->h_proto;
550 else
551 packet->protocol = htons(ETH_P_802_2);
552
553 /* Build an sw_flow for sending this packet. */
Jarno Rajahalme23dabf82014-03-27 12:35:23 -0700554 flow = ovs_flow_alloc();
Jesse Grossccb13522011-10-25 19:26:31 -0700555 err = PTR_ERR(flow);
556 if (IS_ERR(flow))
557 goto err_kfree_skb;
558
Pravin B Shelar83c8df22014-09-15 19:20:31 -0700559 err = ovs_flow_key_extract_userspace(a[OVS_PACKET_ATTR_KEY], packet,
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800560 &flow->key, log);
Jesse Grossccb13522011-10-25 19:26:31 -0700561 if (err)
562 goto err_flow_free;
563
Pravin B Shelare6445712013-10-03 18:16:47 -0700564 err = ovs_nla_copy_actions(a[OVS_PACKET_ATTR_ACTIONS],
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800565 &flow->key, &acts, log);
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700566 if (err)
567 goto err_flow_free;
Jesse Grossccb13522011-10-25 19:26:31 -0700568
Jesse Grossf5796682014-10-03 15:35:33 -0700569 rcu_assign_pointer(flow->sf_acts, acts);
Jesse Grossf5796682014-10-03 15:35:33 -0700570 OVS_CB(packet)->egress_tun_info = NULL;
Jesse Grossccb13522011-10-25 19:26:31 -0700571 packet->priority = flow->key.phy.priority;
Ansis Atteka39c7caeb2012-11-26 11:24:11 -0800572 packet->mark = flow->key.phy.skb_mark;
Jesse Grossccb13522011-10-25 19:26:31 -0700573
574 rcu_read_lock();
Andy Zhoucc3a5ae2014-09-08 13:14:22 -0700575 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -0700576 err = -ENODEV;
577 if (!dp)
578 goto err_unlock;
579
Pravin B Shelar83c8df22014-09-15 19:20:31 -0700580 input_vport = ovs_vport_rcu(dp, flow->key.phy.in_port);
581 if (!input_vport)
582 input_vport = ovs_vport_rcu(dp, OVSP_LOCAL);
583
584 if (!input_vport)
585 goto err_unlock;
586
587 OVS_CB(packet)->input_vport = input_vport;
Lorand Jakabd98612b2014-10-06 05:45:32 -0700588 sf_acts = rcu_dereference(flow->sf_acts);
Pravin B Shelar83c8df22014-09-15 19:20:31 -0700589
Jesse Grossccb13522011-10-25 19:26:31 -0700590 local_bh_disable();
Lorand Jakabd98612b2014-10-06 05:45:32 -0700591 err = ovs_execute_actions(dp, packet, sf_acts, &flow->key);
Jesse Grossccb13522011-10-25 19:26:31 -0700592 local_bh_enable();
593 rcu_read_unlock();
594
Andy Zhou03f0d912013-08-07 20:01:00 -0700595 ovs_flow_free(flow, false);
Jesse Grossccb13522011-10-25 19:26:31 -0700596 return err;
597
598err_unlock:
599 rcu_read_unlock();
600err_flow_free:
Andy Zhou03f0d912013-08-07 20:01:00 -0700601 ovs_flow_free(flow, false);
Jesse Grossccb13522011-10-25 19:26:31 -0700602err_kfree_skb:
603 kfree_skb(packet);
604err:
605 return err;
606}
607
608static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
Thomas Grafdded45fc2013-03-29 14:46:47 +0100609 [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
Jesse Grossccb13522011-10-25 19:26:31 -0700610 [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
611 [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
Thomas Graf1ba39802015-01-14 13:56:19 +0000612 [OVS_PACKET_ATTR_PROBE] = { .type = NLA_FLAG },
Jesse Grossccb13522011-10-25 19:26:31 -0700613};
614
Johannes Berg4534de82013-11-14 17:14:46 +0100615static const struct genl_ops dp_packet_genl_ops[] = {
Jesse Grossccb13522011-10-25 19:26:31 -0700616 { .cmd = OVS_PACKET_CMD_EXECUTE,
617 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
618 .policy = packet_policy,
619 .doit = ovs_packet_cmd_execute
620 }
621};
622
Pravin B Shelar0c200ef2014-05-06 16:44:50 -0700623static struct genl_family dp_packet_genl_family = {
624 .id = GENL_ID_GENERATE,
625 .hdrsize = sizeof(struct ovs_header),
626 .name = OVS_PACKET_FAMILY,
627 .version = OVS_PACKET_VERSION,
628 .maxattr = OVS_PACKET_ATTR_MAX,
629 .netnsok = true,
630 .parallel_ops = true,
631 .ops = dp_packet_genl_ops,
632 .n_ops = ARRAY_SIZE(dp_packet_genl_ops),
633};
634
Thomas Graf12eb18f2014-11-06 06:58:52 -0800635static void get_dp_stats(const struct datapath *dp, struct ovs_dp_stats *stats,
Andy Zhou1bd71162013-10-22 10:42:46 -0700636 struct ovs_dp_megaflow_stats *mega_stats)
Jesse Grossccb13522011-10-25 19:26:31 -0700637{
638 int i;
Jesse Grossccb13522011-10-25 19:26:31 -0700639
Andy Zhou1bd71162013-10-22 10:42:46 -0700640 memset(mega_stats, 0, sizeof(*mega_stats));
641
Pravin B Shelarb637e492013-10-04 00:14:23 -0700642 stats->n_flows = ovs_flow_tbl_count(&dp->table);
Andy Zhou1bd71162013-10-22 10:42:46 -0700643 mega_stats->n_masks = ovs_flow_tbl_num_masks(&dp->table);
Jesse Grossccb13522011-10-25 19:26:31 -0700644
645 stats->n_hit = stats->n_missed = stats->n_lost = 0;
Andy Zhou1bd71162013-10-22 10:42:46 -0700646
Jesse Grossccb13522011-10-25 19:26:31 -0700647 for_each_possible_cpu(i) {
648 const struct dp_stats_percpu *percpu_stats;
649 struct dp_stats_percpu local_stats;
650 unsigned int start;
651
652 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
653
654 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700655 start = u64_stats_fetch_begin_irq(&percpu_stats->syncp);
Jesse Grossccb13522011-10-25 19:26:31 -0700656 local_stats = *percpu_stats;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700657 } while (u64_stats_fetch_retry_irq(&percpu_stats->syncp, start));
Jesse Grossccb13522011-10-25 19:26:31 -0700658
659 stats->n_hit += local_stats.n_hit;
660 stats->n_missed += local_stats.n_missed;
661 stats->n_lost += local_stats.n_lost;
Andy Zhou1bd71162013-10-22 10:42:46 -0700662 mega_stats->n_mask_hit += local_stats.n_mask_hit;
Jesse Grossccb13522011-10-25 19:26:31 -0700663 }
664}
665
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800666static bool should_fill_key(const struct sw_flow_id *sfid, uint32_t ufid_flags)
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100667{
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800668 return ovs_identifier_is_ufid(sfid) &&
669 !(ufid_flags & OVS_UFID_F_OMIT_KEY);
670}
671
672static bool should_fill_mask(uint32_t ufid_flags)
673{
674 return !(ufid_flags & OVS_UFID_F_OMIT_MASK);
675}
676
677static bool should_fill_actions(uint32_t ufid_flags)
678{
679 return !(ufid_flags & OVS_UFID_F_OMIT_ACTIONS);
680}
681
682static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts,
683 const struct sw_flow_id *sfid,
684 uint32_t ufid_flags)
685{
686 size_t len = NLMSG_ALIGN(sizeof(struct ovs_header));
687
688 /* OVS_FLOW_ATTR_UFID */
689 if (sfid && ovs_identifier_is_ufid(sfid))
690 len += nla_total_size(sfid->ufid_len);
691
692 /* OVS_FLOW_ATTR_KEY */
693 if (!sfid || should_fill_key(sfid, ufid_flags))
694 len += nla_total_size(ovs_key_attr_size());
695
696 /* OVS_FLOW_ATTR_MASK */
697 if (should_fill_mask(ufid_flags))
698 len += nla_total_size(ovs_key_attr_size());
699
700 /* OVS_FLOW_ATTR_ACTIONS */
701 if (should_fill_actions(ufid_flags))
702 len += nla_total_size(acts->actions_len);
703
704 return len
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100705 + nla_total_size(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
706 + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800707 + nla_total_size(8); /* OVS_FLOW_ATTR_USED */
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100708}
709
Jarno Rajahalmebb6f9a72014-05-05 11:32:17 -0700710/* Called with ovs_mutex or RCU read lock. */
Joe Stringerca7105f2014-09-08 13:09:37 -0700711static int ovs_flow_cmd_fill_stats(const struct sw_flow *flow,
712 struct sk_buff *skb)
713{
714 struct ovs_flow_stats stats;
715 __be16 tcp_flags;
716 unsigned long used;
Andy Zhou03f0d912013-08-07 20:01:00 -0700717
Pravin B Shelare298e502013-10-29 17:22:21 -0700718 ovs_flow_stats_get(flow, &stats, &used, &tcp_flags);
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700719
David S. Miller028d6a62012-03-29 23:20:48 -0400720 if (used &&
721 nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
Joe Stringerca7105f2014-09-08 13:09:37 -0700722 return -EMSGSIZE;
Jesse Grossccb13522011-10-25 19:26:31 -0700723
David S. Miller028d6a62012-03-29 23:20:48 -0400724 if (stats.n_packets &&
Pravin B Shelare298e502013-10-29 17:22:21 -0700725 nla_put(skb, OVS_FLOW_ATTR_STATS, sizeof(struct ovs_flow_stats), &stats))
Joe Stringerca7105f2014-09-08 13:09:37 -0700726 return -EMSGSIZE;
Jesse Grossccb13522011-10-25 19:26:31 -0700727
Pravin B Shelare298e502013-10-29 17:22:21 -0700728 if ((u8)ntohs(tcp_flags) &&
729 nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, (u8)ntohs(tcp_flags)))
Joe Stringerca7105f2014-09-08 13:09:37 -0700730 return -EMSGSIZE;
731
732 return 0;
733}
734
735/* Called with ovs_mutex or RCU read lock. */
736static int ovs_flow_cmd_fill_actions(const struct sw_flow *flow,
737 struct sk_buff *skb, int skb_orig_len)
738{
739 struct nlattr *start;
740 int err;
Jesse Grossccb13522011-10-25 19:26:31 -0700741
742 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
743 * this is the first flow to be dumped into 'skb'. This is unusual for
744 * Netlink but individual action lists can be longer than
745 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
746 * The userspace caller can always fetch the actions separately if it
747 * really wants them. (Most userspace callers in fact don't care.)
748 *
749 * This can only fail for dump operations because the skb is always
750 * properly sized for single flows.
751 */
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700752 start = nla_nest_start(skb, OVS_FLOW_ATTR_ACTIONS);
753 if (start) {
Pravin B Shelard57170b2013-07-30 15:39:39 -0700754 const struct sw_flow_actions *sf_acts;
755
Jesse Gross663efa32013-12-03 10:58:53 -0800756 sf_acts = rcu_dereference_ovsl(flow->sf_acts);
Pravin B Shelare6445712013-10-03 18:16:47 -0700757 err = ovs_nla_put_actions(sf_acts->actions,
758 sf_acts->actions_len, skb);
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700759
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700760 if (!err)
761 nla_nest_end(skb, start);
762 else {
763 if (skb_orig_len)
Joe Stringerca7105f2014-09-08 13:09:37 -0700764 return err;
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700765
766 nla_nest_cancel(skb, start);
767 }
Joe Stringerca7105f2014-09-08 13:09:37 -0700768 } else if (skb_orig_len) {
769 return -EMSGSIZE;
770 }
771
772 return 0;
773}
774
775/* Called with ovs_mutex or RCU read lock. */
776static int ovs_flow_cmd_fill_info(const struct sw_flow *flow, int dp_ifindex,
777 struct sk_buff *skb, u32 portid,
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800778 u32 seq, u32 flags, u8 cmd, u32 ufid_flags)
Joe Stringerca7105f2014-09-08 13:09:37 -0700779{
780 const int skb_orig_len = skb->len;
781 struct ovs_header *ovs_header;
782 int err;
783
784 ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family,
785 flags, cmd);
786 if (!ovs_header)
787 return -EMSGSIZE;
788
789 ovs_header->dp_ifindex = dp_ifindex;
790
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800791 err = ovs_nla_put_identifier(flow, skb);
Joe Stringer5b4237b2015-01-21 16:42:48 -0800792 if (err)
793 goto error;
794
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800795 if (should_fill_key(&flow->id, ufid_flags)) {
796 err = ovs_nla_put_masked_key(flow, skb);
797 if (err)
798 goto error;
799 }
800
801 if (should_fill_mask(ufid_flags)) {
802 err = ovs_nla_put_mask(flow, skb);
803 if (err)
804 goto error;
805 }
Joe Stringerca7105f2014-09-08 13:09:37 -0700806
807 err = ovs_flow_cmd_fill_stats(flow, skb);
808 if (err)
809 goto error;
810
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800811 if (should_fill_actions(ufid_flags)) {
812 err = ovs_flow_cmd_fill_actions(flow, skb, skb_orig_len);
813 if (err)
814 goto error;
815 }
Jesse Grossccb13522011-10-25 19:26:31 -0700816
Johannes Berg053c0952015-01-16 22:09:00 +0100817 genlmsg_end(skb, ovs_header);
818 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -0700819
Jesse Grossccb13522011-10-25 19:26:31 -0700820error:
821 genlmsg_cancel(skb, ovs_header);
822 return err;
823}
824
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700825/* May not be called with RCU read lock. */
826static struct sk_buff *ovs_flow_cmd_alloc_info(const struct sw_flow_actions *acts,
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800827 const struct sw_flow_id *sfid,
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700828 struct genl_info *info,
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800829 bool always,
830 uint32_t ufid_flags)
Jesse Grossccb13522011-10-25 19:26:31 -0700831{
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700832 struct sk_buff *skb;
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800833 size_t len;
Jesse Grossccb13522011-10-25 19:26:31 -0700834
Samuel Gauthier9b67aa42014-09-18 10:31:04 +0200835 if (!always && !ovs_must_notify(&dp_flow_genl_family, info, 0))
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700836 return NULL;
837
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800838 len = ovs_flow_cmd_msg_size(acts, sfid, ufid_flags);
839 skb = genlmsg_new_unicast(len, info, GFP_KERNEL);
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700840 if (!skb)
841 return ERR_PTR(-ENOMEM);
842
843 return skb;
Jesse Grossccb13522011-10-25 19:26:31 -0700844}
845
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700846/* Called with ovs_mutex. */
847static struct sk_buff *ovs_flow_cmd_build_info(const struct sw_flow *flow,
848 int dp_ifindex,
849 struct genl_info *info, u8 cmd,
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800850 bool always, u32 ufid_flags)
Jesse Grossccb13522011-10-25 19:26:31 -0700851{
852 struct sk_buff *skb;
853 int retval;
854
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800855 skb = ovs_flow_cmd_alloc_info(ovsl_dereference(flow->sf_acts),
856 &flow->id, info, always, ufid_flags);
Himangi Saraogid0e992a2014-07-27 12:37:46 +0530857 if (IS_ERR_OR_NULL(skb))
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700858 return skb;
Jesse Grossccb13522011-10-25 19:26:31 -0700859
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700860 retval = ovs_flow_cmd_fill_info(flow, dp_ifindex, skb,
861 info->snd_portid, info->snd_seq, 0,
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800862 cmd, ufid_flags);
Jesse Grossccb13522011-10-25 19:26:31 -0700863 BUG_ON(retval < 0);
864 return skb;
865}
866
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700867static int ovs_flow_cmd_new(struct sk_buff *skb, struct genl_info *info)
Jesse Grossccb13522011-10-25 19:26:31 -0700868{
869 struct nlattr **a = info->attrs;
870 struct ovs_header *ovs_header = info->userhdr;
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800871 struct sw_flow *flow = NULL, *new_flow;
Andy Zhou03f0d912013-08-07 20:01:00 -0700872 struct sw_flow_mask mask;
Jesse Grossccb13522011-10-25 19:26:31 -0700873 struct sk_buff *reply;
874 struct datapath *dp;
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800875 struct sw_flow_key key;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700876 struct sw_flow_actions *acts;
877 struct sw_flow_match match;
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800878 u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700879 int error;
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800880 bool log = !a[OVS_FLOW_ATTR_PROBE];
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700881
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700882 /* Must have key and actions. */
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700883 error = -EINVAL;
Jesse Gross426cda52014-10-06 05:08:38 -0700884 if (!a[OVS_FLOW_ATTR_KEY]) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800885 OVS_NLERR(log, "Flow key attr not present in new flow.");
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700886 goto error;
Jesse Gross426cda52014-10-06 05:08:38 -0700887 }
888 if (!a[OVS_FLOW_ATTR_ACTIONS]) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800889 OVS_NLERR(log, "Flow actions attr not present in new flow.");
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700890 goto error;
Jesse Gross426cda52014-10-06 05:08:38 -0700891 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700892
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700893 /* Most of the time we need to allocate a new flow, do it before
894 * locking.
895 */
896 new_flow = ovs_flow_alloc();
897 if (IS_ERR(new_flow)) {
898 error = PTR_ERR(new_flow);
899 goto error;
900 }
901
902 /* Extract key. */
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800903 ovs_match_init(&match, &key, &mask);
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800904 error = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY],
905 a[OVS_FLOW_ATTR_MASK], log);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700906 if (error)
907 goto err_kfree_flow;
908
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800909 ovs_flow_mask_key(&new_flow->key, &key, &mask);
910
911 /* Extract flow identifier. */
912 error = ovs_nla_get_identifier(&new_flow->id, a[OVS_FLOW_ATTR_UFID],
913 &key, log);
914 if (error)
915 goto err_kfree_flow;
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700916
917 /* Validate actions. */
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700918 error = ovs_nla_copy_actions(a[OVS_FLOW_ATTR_ACTIONS], &new_flow->key,
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800919 &acts, log);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700920 if (error) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800921 OVS_NLERR(log, "Flow actions may not be safe on all matching packets.");
Pravin B Shelar2fdb9572014-10-19 11:19:51 -0700922 goto err_kfree_flow;
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700923 }
924
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800925 reply = ovs_flow_cmd_alloc_info(acts, &new_flow->id, info, false,
926 ufid_flags);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700927 if (IS_ERR(reply)) {
928 error = PTR_ERR(reply);
929 goto err_kfree_acts;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700930 }
931
932 ovs_lock();
933 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700934 if (unlikely(!dp)) {
935 error = -ENODEV;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700936 goto err_unlock_ovs;
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700937 }
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800938
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700939 /* Check if this is a duplicate flow */
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800940 if (ovs_identifier_is_ufid(&new_flow->id))
941 flow = ovs_flow_tbl_lookup_ufid(&dp->table, &new_flow->id);
942 if (!flow)
943 flow = ovs_flow_tbl_lookup(&dp->table, &key);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700944 if (likely(!flow)) {
945 rcu_assign_pointer(new_flow->sf_acts, acts);
946
947 /* Put flow in bucket. */
948 error = ovs_flow_tbl_insert(&dp->table, new_flow, &mask);
949 if (unlikely(error)) {
950 acts = NULL;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700951 goto err_unlock_ovs;
952 }
953
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700954 if (unlikely(reply)) {
955 error = ovs_flow_cmd_fill_info(new_flow,
956 ovs_header->dp_ifindex,
957 reply, info->snd_portid,
958 info->snd_seq, 0,
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800959 OVS_FLOW_CMD_NEW,
960 ufid_flags);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700961 BUG_ON(error < 0);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700962 }
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700963 ovs_unlock();
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700964 } else {
965 struct sw_flow_actions *old_acts;
966
967 /* Bail out if we're not allowed to modify an existing flow.
968 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
969 * because Generic Netlink treats the latter as a dump
970 * request. We also accept NLM_F_EXCL in case that bug ever
971 * gets fixed.
972 */
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700973 if (unlikely(info->nlhdr->nlmsg_flags & (NLM_F_CREATE
974 | NLM_F_EXCL))) {
975 error = -EEXIST;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700976 goto err_unlock_ovs;
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700977 }
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800978 /* The flow identifier has to be the same for flow updates.
979 * Look for any overlapping flow.
980 */
981 if (unlikely(!ovs_flow_cmp(flow, &match))) {
982 if (ovs_identifier_is_key(&flow->id))
983 flow = ovs_flow_tbl_lookup_exact(&dp->table,
984 &match);
985 else /* UFID matches but key is different */
986 flow = NULL;
Alex Wang4a46b242014-06-30 20:30:29 -0700987 if (!flow) {
988 error = -ENOENT;
989 goto err_unlock_ovs;
990 }
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700991 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700992 /* Update actions. */
993 old_acts = ovsl_dereference(flow->sf_acts);
994 rcu_assign_pointer(flow->sf_acts, acts);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700995
996 if (unlikely(reply)) {
997 error = ovs_flow_cmd_fill_info(flow,
998 ovs_header->dp_ifindex,
999 reply, info->snd_portid,
1000 info->snd_seq, 0,
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001001 OVS_FLOW_CMD_NEW,
1002 ufid_flags);
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001003 BUG_ON(error < 0);
1004 }
1005 ovs_unlock();
1006
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001007 ovs_nla_free_flow_actions(old_acts);
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001008 ovs_flow_free(new_flow, false);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001009 }
1010
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001011 if (reply)
1012 ovs_notify(&dp_flow_genl_family, reply, info);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001013 return 0;
1014
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001015err_unlock_ovs:
1016 ovs_unlock();
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001017 kfree_skb(reply);
1018err_kfree_acts:
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001019 kfree(acts);
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001020err_kfree_flow:
1021 ovs_flow_free(new_flow, false);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001022error:
1023 return error;
1024}
1025
Pravin B Shelar2fdb9572014-10-19 11:19:51 -07001026/* Factor out action copy to avoid "Wframe-larger-than=1024" warning. */
Jesse Gross6b205b22014-10-03 15:35:32 -07001027static struct sw_flow_actions *get_flow_actions(const struct nlattr *a,
1028 const struct sw_flow_key *key,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001029 const struct sw_flow_mask *mask,
1030 bool log)
Jesse Gross6b205b22014-10-03 15:35:32 -07001031{
1032 struct sw_flow_actions *acts;
1033 struct sw_flow_key masked_key;
1034 int error;
1035
Jesse Gross6b205b22014-10-03 15:35:32 -07001036 ovs_flow_mask_key(&masked_key, key, mask);
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001037 error = ovs_nla_copy_actions(a, &masked_key, &acts, log);
Jesse Gross6b205b22014-10-03 15:35:32 -07001038 if (error) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001039 OVS_NLERR(log,
1040 "Actions may not be safe on all matching packets");
Jesse Gross6b205b22014-10-03 15:35:32 -07001041 return ERR_PTR(error);
1042 }
1043
1044 return acts;
1045}
1046
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001047static int ovs_flow_cmd_set(struct sk_buff *skb, struct genl_info *info)
1048{
1049 struct nlattr **a = info->attrs;
1050 struct ovs_header *ovs_header = info->userhdr;
Jesse Gross6b205b22014-10-03 15:35:32 -07001051 struct sw_flow_key key;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001052 struct sw_flow *flow;
1053 struct sw_flow_mask mask;
1054 struct sk_buff *reply = NULL;
1055 struct datapath *dp;
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001056 struct sw_flow_actions *old_acts = NULL, *acts = NULL;
Andy Zhou03f0d912013-08-07 20:01:00 -07001057 struct sw_flow_match match;
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001058 struct sw_flow_id sfid;
1059 u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
Jesse Grossccb13522011-10-25 19:26:31 -07001060 int error;
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001061 bool log = !a[OVS_FLOW_ATTR_PROBE];
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001062 bool ufid_present;
Jesse Grossccb13522011-10-25 19:26:31 -07001063
1064 /* Extract key. */
1065 error = -EINVAL;
Jesse Gross426cda52014-10-06 05:08:38 -07001066 if (!a[OVS_FLOW_ATTR_KEY]) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001067 OVS_NLERR(log, "Flow key attribute not present in set flow.");
Jesse Grossccb13522011-10-25 19:26:31 -07001068 goto error;
Jesse Gross426cda52014-10-06 05:08:38 -07001069 }
Andy Zhou03f0d912013-08-07 20:01:00 -07001070
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001071 ufid_present = ovs_nla_get_ufid(&sfid, a[OVS_FLOW_ATTR_UFID], log);
Andy Zhou03f0d912013-08-07 20:01:00 -07001072 ovs_match_init(&match, &key, &mask);
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001073 error = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY],
1074 a[OVS_FLOW_ATTR_MASK], log);
Jesse Grossccb13522011-10-25 19:26:31 -07001075 if (error)
1076 goto error;
1077
1078 /* Validate actions. */
1079 if (a[OVS_FLOW_ATTR_ACTIONS]) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001080 acts = get_flow_actions(a[OVS_FLOW_ATTR_ACTIONS], &key, &mask,
1081 log);
Jesse Gross6b205b22014-10-03 15:35:32 -07001082 if (IS_ERR(acts)) {
1083 error = PTR_ERR(acts);
Jesse Grossccb13522011-10-25 19:26:31 -07001084 goto error;
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001085 }
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001086
Pravin B Shelar2fdb9572014-10-19 11:19:51 -07001087 /* Can allocate before locking if have acts. */
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001088 reply = ovs_flow_cmd_alloc_info(acts, &sfid, info, false,
1089 ufid_flags);
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001090 if (IS_ERR(reply)) {
1091 error = PTR_ERR(reply);
1092 goto err_kfree_acts;
Andy Zhou03f0d912013-08-07 20:01:00 -07001093 }
Jesse Grossccb13522011-10-25 19:26:31 -07001094 }
1095
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001096 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001097 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001098 if (unlikely(!dp)) {
1099 error = -ENODEV;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001100 goto err_unlock_ovs;
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001101 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001102 /* Check that the flow exists. */
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001103 if (ufid_present)
1104 flow = ovs_flow_tbl_lookup_ufid(&dp->table, &sfid);
1105 else
1106 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001107 if (unlikely(!flow)) {
1108 error = -ENOENT;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001109 goto err_unlock_ovs;
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001110 }
Alex Wang4a46b242014-06-30 20:30:29 -07001111
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001112 /* Update actions, if present. */
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001113 if (likely(acts)) {
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001114 old_acts = ovsl_dereference(flow->sf_acts);
Jesse Grossccb13522011-10-25 19:26:31 -07001115 rcu_assign_pointer(flow->sf_acts, acts);
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001116
1117 if (unlikely(reply)) {
1118 error = ovs_flow_cmd_fill_info(flow,
1119 ovs_header->dp_ifindex,
1120 reply, info->snd_portid,
1121 info->snd_seq, 0,
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001122 OVS_FLOW_CMD_NEW,
1123 ufid_flags);
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001124 BUG_ON(error < 0);
1125 }
1126 } else {
1127 /* Could not alloc without acts before locking. */
1128 reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex,
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001129 info, OVS_FLOW_CMD_NEW, false,
1130 ufid_flags);
1131
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001132 if (unlikely(IS_ERR(reply))) {
1133 error = PTR_ERR(reply);
1134 goto err_unlock_ovs;
1135 }
Jesse Grossccb13522011-10-25 19:26:31 -07001136 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001137
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001138 /* Clear stats. */
1139 if (a[OVS_FLOW_ATTR_CLEAR])
1140 ovs_flow_stats_clear(flow);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001141 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001142
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001143 if (reply)
1144 ovs_notify(&dp_flow_genl_family, reply, info);
1145 if (old_acts)
1146 ovs_nla_free_flow_actions(old_acts);
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -07001147
Jesse Grossccb13522011-10-25 19:26:31 -07001148 return 0;
1149
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001150err_unlock_ovs:
1151 ovs_unlock();
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001152 kfree_skb(reply);
1153err_kfree_acts:
Pravin B Shelar74f84a52013-06-17 17:50:12 -07001154 kfree(acts);
Jesse Grossccb13522011-10-25 19:26:31 -07001155error:
1156 return error;
1157}
1158
1159static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1160{
1161 struct nlattr **a = info->attrs;
1162 struct ovs_header *ovs_header = info->userhdr;
1163 struct sw_flow_key key;
1164 struct sk_buff *reply;
1165 struct sw_flow *flow;
1166 struct datapath *dp;
Andy Zhou03f0d912013-08-07 20:01:00 -07001167 struct sw_flow_match match;
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001168 struct sw_flow_id ufid;
1169 u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
1170 int err = 0;
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001171 bool log = !a[OVS_FLOW_ATTR_PROBE];
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001172 bool ufid_present;
Jesse Grossccb13522011-10-25 19:26:31 -07001173
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001174 ufid_present = ovs_nla_get_ufid(&ufid, a[OVS_FLOW_ATTR_UFID], log);
1175 if (a[OVS_FLOW_ATTR_KEY]) {
1176 ovs_match_init(&match, &key, NULL);
1177 err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL,
1178 log);
1179 } else if (!ufid_present) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001180 OVS_NLERR(log,
1181 "Flow get message rejected, Key attribute missing.");
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001182 err = -EINVAL;
Andy Zhou03f0d912013-08-07 20:01:00 -07001183 }
Jesse Grossccb13522011-10-25 19:26:31 -07001184 if (err)
1185 return err;
1186
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001187 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001188 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001189 if (!dp) {
1190 err = -ENODEV;
1191 goto unlock;
1192 }
Jesse Grossccb13522011-10-25 19:26:31 -07001193
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001194 if (ufid_present)
1195 flow = ovs_flow_tbl_lookup_ufid(&dp->table, &ufid);
1196 else
1197 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
Alex Wang4a46b242014-06-30 20:30:29 -07001198 if (!flow) {
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001199 err = -ENOENT;
1200 goto unlock;
1201 }
Jesse Grossccb13522011-10-25 19:26:31 -07001202
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -07001203 reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex, info,
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001204 OVS_FLOW_CMD_NEW, true, ufid_flags);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001205 if (IS_ERR(reply)) {
1206 err = PTR_ERR(reply);
1207 goto unlock;
1208 }
Jesse Grossccb13522011-10-25 19:26:31 -07001209
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001210 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001211 return genlmsg_reply(reply, info);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001212unlock:
1213 ovs_unlock();
1214 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001215}
1216
1217static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1218{
1219 struct nlattr **a = info->attrs;
1220 struct ovs_header *ovs_header = info->userhdr;
1221 struct sw_flow_key key;
1222 struct sk_buff *reply;
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001223 struct sw_flow *flow = NULL;
Jesse Grossccb13522011-10-25 19:26:31 -07001224 struct datapath *dp;
Andy Zhou03f0d912013-08-07 20:01:00 -07001225 struct sw_flow_match match;
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001226 struct sw_flow_id ufid;
1227 u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
Jesse Grossccb13522011-10-25 19:26:31 -07001228 int err;
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001229 bool log = !a[OVS_FLOW_ATTR_PROBE];
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001230 bool ufid_present;
Jesse Grossccb13522011-10-25 19:26:31 -07001231
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001232 ufid_present = ovs_nla_get_ufid(&ufid, a[OVS_FLOW_ATTR_UFID], log);
1233 if (a[OVS_FLOW_ATTR_KEY]) {
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001234 ovs_match_init(&match, &key, NULL);
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001235 err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL,
1236 log);
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001237 if (unlikely(err))
1238 return err;
1239 }
1240
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001241 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001242 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001243 if (unlikely(!dp)) {
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001244 err = -ENODEV;
1245 goto unlock;
1246 }
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001247
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001248 if (unlikely(!a[OVS_FLOW_ATTR_KEY] && !ufid_present)) {
Pravin B Shelarb637e492013-10-04 00:14:23 -07001249 err = ovs_flow_tbl_flush(&dp->table);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001250 goto unlock;
1251 }
Andy Zhou03f0d912013-08-07 20:01:00 -07001252
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001253 if (ufid_present)
1254 flow = ovs_flow_tbl_lookup_ufid(&dp->table, &ufid);
1255 else
1256 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
Alex Wang4a46b242014-06-30 20:30:29 -07001257 if (unlikely(!flow)) {
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001258 err = -ENOENT;
1259 goto unlock;
1260 }
Jesse Grossccb13522011-10-25 19:26:31 -07001261
Pravin B Shelarb637e492013-10-04 00:14:23 -07001262 ovs_flow_tbl_remove(&dp->table, flow);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001263 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001264
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001265 reply = ovs_flow_cmd_alloc_info((const struct sw_flow_actions __force *) flow->sf_acts,
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001266 &flow->id, info, false, ufid_flags);
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001267 if (likely(reply)) {
1268 if (likely(!IS_ERR(reply))) {
1269 rcu_read_lock(); /*To keep RCU checker happy. */
1270 err = ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex,
1271 reply, info->snd_portid,
1272 info->snd_seq, 0,
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001273 OVS_FLOW_CMD_DEL,
1274 ufid_flags);
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001275 rcu_read_unlock();
1276 BUG_ON(err < 0);
1277
1278 ovs_notify(&dp_flow_genl_family, reply, info);
1279 } else {
1280 netlink_set_err(sock_net(skb->sk)->genl_sock, 0, 0, PTR_ERR(reply));
1281 }
1282 }
1283
1284 ovs_flow_free(flow, true);
Jesse Grossccb13522011-10-25 19:26:31 -07001285 return 0;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001286unlock:
1287 ovs_unlock();
1288 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001289}
1290
1291static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1292{
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001293 struct nlattr *a[__OVS_FLOW_ATTR_MAX];
Jesse Grossccb13522011-10-25 19:26:31 -07001294 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
Pravin B Shelarb637e492013-10-04 00:14:23 -07001295 struct table_instance *ti;
Jesse Grossccb13522011-10-25 19:26:31 -07001296 struct datapath *dp;
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001297 u32 ufid_flags;
1298 int err;
1299
1300 err = genlmsg_parse(cb->nlh, &dp_flow_genl_family, a,
1301 OVS_FLOW_ATTR_MAX, flow_policy);
1302 if (err)
1303 return err;
1304 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
Jesse Grossccb13522011-10-25 19:26:31 -07001305
Pravin B Shelard57170b2013-07-30 15:39:39 -07001306 rcu_read_lock();
Andy Zhoucc3a5ae2014-09-08 13:14:22 -07001307 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001308 if (!dp) {
Pravin B Shelard57170b2013-07-30 15:39:39 -07001309 rcu_read_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001310 return -ENODEV;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001311 }
Jesse Grossccb13522011-10-25 19:26:31 -07001312
Pravin B Shelarb637e492013-10-04 00:14:23 -07001313 ti = rcu_dereference(dp->table.ti);
Jesse Grossccb13522011-10-25 19:26:31 -07001314 for (;;) {
1315 struct sw_flow *flow;
1316 u32 bucket, obj;
1317
1318 bucket = cb->args[0];
1319 obj = cb->args[1];
Pravin B Shelarb637e492013-10-04 00:14:23 -07001320 flow = ovs_flow_tbl_dump_next(ti, &bucket, &obj);
Jesse Grossccb13522011-10-25 19:26:31 -07001321 if (!flow)
1322 break;
1323
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -07001324 if (ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex, skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001325 NETLINK_CB(cb->skb).portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001326 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001327 OVS_FLOW_CMD_NEW, ufid_flags) < 0)
Jesse Grossccb13522011-10-25 19:26:31 -07001328 break;
1329
1330 cb->args[0] = bucket;
1331 cb->args[1] = obj;
1332 }
Pravin B Shelard57170b2013-07-30 15:39:39 -07001333 rcu_read_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001334 return skb->len;
1335}
1336
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001337static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
1338 [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001339 [OVS_FLOW_ATTR_MASK] = { .type = NLA_NESTED },
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001340 [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
1341 [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001342 [OVS_FLOW_ATTR_PROBE] = { .type = NLA_FLAG },
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001343 [OVS_FLOW_ATTR_UFID] = { .type = NLA_UNSPEC, .len = 1 },
1344 [OVS_FLOW_ATTR_UFID_FLAGS] = { .type = NLA_U32 },
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001345};
1346
stephen hemminger48e48a72014-07-16 11:25:52 -07001347static const struct genl_ops dp_flow_genl_ops[] = {
Jesse Grossccb13522011-10-25 19:26:31 -07001348 { .cmd = OVS_FLOW_CMD_NEW,
1349 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1350 .policy = flow_policy,
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001351 .doit = ovs_flow_cmd_new
Jesse Grossccb13522011-10-25 19:26:31 -07001352 },
1353 { .cmd = OVS_FLOW_CMD_DEL,
1354 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1355 .policy = flow_policy,
1356 .doit = ovs_flow_cmd_del
1357 },
1358 { .cmd = OVS_FLOW_CMD_GET,
1359 .flags = 0, /* OK for unprivileged users. */
1360 .policy = flow_policy,
1361 .doit = ovs_flow_cmd_get,
1362 .dumpit = ovs_flow_cmd_dump
1363 },
1364 { .cmd = OVS_FLOW_CMD_SET,
1365 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1366 .policy = flow_policy,
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001367 .doit = ovs_flow_cmd_set,
Jesse Grossccb13522011-10-25 19:26:31 -07001368 },
1369};
1370
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001371static struct genl_family dp_flow_genl_family = {
Jesse Grossccb13522011-10-25 19:26:31 -07001372 .id = GENL_ID_GENERATE,
1373 .hdrsize = sizeof(struct ovs_header),
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001374 .name = OVS_FLOW_FAMILY,
1375 .version = OVS_FLOW_VERSION,
1376 .maxattr = OVS_FLOW_ATTR_MAX,
Pravin B Shelar3a4e0d62013-04-23 07:48:48 +00001377 .netnsok = true,
1378 .parallel_ops = true,
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001379 .ops = dp_flow_genl_ops,
1380 .n_ops = ARRAY_SIZE(dp_flow_genl_ops),
1381 .mcgrps = &ovs_dp_flow_multicast_group,
1382 .n_mcgrps = 1,
Jesse Grossccb13522011-10-25 19:26:31 -07001383};
1384
Thomas Grafc3ff8cf2013-03-29 14:46:49 +01001385static size_t ovs_dp_cmd_msg_size(void)
1386{
1387 size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
1388
1389 msgsize += nla_total_size(IFNAMSIZ);
1390 msgsize += nla_total_size(sizeof(struct ovs_dp_stats));
Andy Zhou1bd71162013-10-22 10:42:46 -07001391 msgsize += nla_total_size(sizeof(struct ovs_dp_megaflow_stats));
Daniele Di Proietto45fb9c32014-01-23 10:47:35 -08001392 msgsize += nla_total_size(sizeof(u32)); /* OVS_DP_ATTR_USER_FEATURES */
Thomas Grafc3ff8cf2013-03-29 14:46:49 +01001393
1394 return msgsize;
1395}
1396
Pravin B Shelar8ec609d2014-11-11 15:55:16 -08001397/* Called with ovs_mutex. */
Jesse Grossccb13522011-10-25 19:26:31 -07001398static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001399 u32 portid, u32 seq, u32 flags, u8 cmd)
Jesse Grossccb13522011-10-25 19:26:31 -07001400{
1401 struct ovs_header *ovs_header;
1402 struct ovs_dp_stats dp_stats;
Andy Zhou1bd71162013-10-22 10:42:46 -07001403 struct ovs_dp_megaflow_stats dp_megaflow_stats;
Jesse Grossccb13522011-10-25 19:26:31 -07001404 int err;
1405
Eric W. Biederman15e47302012-09-07 20:12:54 +00001406 ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
Jesse Grossccb13522011-10-25 19:26:31 -07001407 flags, cmd);
1408 if (!ovs_header)
1409 goto error;
1410
1411 ovs_header->dp_ifindex = get_dpifindex(dp);
1412
Jesse Grossccb13522011-10-25 19:26:31 -07001413 err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
Jesse Grossccb13522011-10-25 19:26:31 -07001414 if (err)
1415 goto nla_put_failure;
1416
Andy Zhou1bd71162013-10-22 10:42:46 -07001417 get_dp_stats(dp, &dp_stats, &dp_megaflow_stats);
1418 if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats),
1419 &dp_stats))
1420 goto nla_put_failure;
1421
1422 if (nla_put(skb, OVS_DP_ATTR_MEGAFLOW_STATS,
1423 sizeof(struct ovs_dp_megaflow_stats),
1424 &dp_megaflow_stats))
David S. Miller028d6a62012-03-29 23:20:48 -04001425 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001426
Thomas Graf43d4be92013-12-13 15:22:18 +01001427 if (nla_put_u32(skb, OVS_DP_ATTR_USER_FEATURES, dp->user_features))
1428 goto nla_put_failure;
1429
Johannes Berg053c0952015-01-16 22:09:00 +01001430 genlmsg_end(skb, ovs_header);
1431 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001432
1433nla_put_failure:
1434 genlmsg_cancel(skb, ovs_header);
1435error:
1436 return -EMSGSIZE;
1437}
1438
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001439static struct sk_buff *ovs_dp_cmd_alloc_info(struct genl_info *info)
Jesse Grossccb13522011-10-25 19:26:31 -07001440{
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001441 return genlmsg_new_unicast(ovs_dp_cmd_msg_size(), info, GFP_KERNEL);
Jesse Grossccb13522011-10-25 19:26:31 -07001442}
1443
Jarno Rajahalmebb6f9a72014-05-05 11:32:17 -07001444/* Called with rcu_read_lock or ovs_mutex. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001445static struct datapath *lookup_datapath(struct net *net,
Thomas Graf12eb18f2014-11-06 06:58:52 -08001446 const struct ovs_header *ovs_header,
Jesse Grossccb13522011-10-25 19:26:31 -07001447 struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1448{
1449 struct datapath *dp;
1450
1451 if (!a[OVS_DP_ATTR_NAME])
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001452 dp = get_dp(net, ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001453 else {
1454 struct vport *vport;
1455
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001456 vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
Jesse Grossccb13522011-10-25 19:26:31 -07001457 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
Jesse Grossccb13522011-10-25 19:26:31 -07001458 }
1459 return dp ? dp : ERR_PTR(-ENODEV);
1460}
1461
Thomas Graf44da5ae2013-12-13 15:22:19 +01001462static void ovs_dp_reset_user_features(struct sk_buff *skb, struct genl_info *info)
1463{
1464 struct datapath *dp;
1465
1466 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
Jiri Pirko3c7eacf2014-02-14 11:42:36 +01001467 if (IS_ERR(dp))
Thomas Graf44da5ae2013-12-13 15:22:19 +01001468 return;
1469
1470 WARN(dp->user_features, "Dropping previously announced user features\n");
1471 dp->user_features = 0;
1472}
1473
Thomas Graf12eb18f2014-11-06 06:58:52 -08001474static void ovs_dp_change(struct datapath *dp, struct nlattr *a[])
Thomas Graf43d4be92013-12-13 15:22:18 +01001475{
1476 if (a[OVS_DP_ATTR_USER_FEATURES])
1477 dp->user_features = nla_get_u32(a[OVS_DP_ATTR_USER_FEATURES]);
1478}
1479
Jesse Grossccb13522011-10-25 19:26:31 -07001480static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1481{
1482 struct nlattr **a = info->attrs;
1483 struct vport_parms parms;
1484 struct sk_buff *reply;
1485 struct datapath *dp;
1486 struct vport *vport;
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001487 struct ovs_net *ovs_net;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001488 int err, i;
Jesse Grossccb13522011-10-25 19:26:31 -07001489
1490 err = -EINVAL;
1491 if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1492 goto err;
1493
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001494 reply = ovs_dp_cmd_alloc_info(info);
1495 if (!reply)
1496 return -ENOMEM;
Jesse Grossccb13522011-10-25 19:26:31 -07001497
1498 err = -ENOMEM;
1499 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1500 if (dp == NULL)
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001501 goto err_free_reply;
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001502
Eric W. Biedermanefd7ef12015-03-11 23:04:08 -05001503 ovs_dp_set_net(dp, sock_net(skb->sk));
Jesse Grossccb13522011-10-25 19:26:31 -07001504
1505 /* Allocate table. */
Pravin B Shelarb637e492013-10-04 00:14:23 -07001506 err = ovs_flow_tbl_init(&dp->table);
1507 if (err)
Jesse Grossccb13522011-10-25 19:26:31 -07001508 goto err_free_dp;
1509
WANG Cong1c213bd2014-02-13 11:46:28 -08001510 dp->stats_percpu = netdev_alloc_pcpu_stats(struct dp_stats_percpu);
Jesse Grossccb13522011-10-25 19:26:31 -07001511 if (!dp->stats_percpu) {
1512 err = -ENOMEM;
1513 goto err_destroy_table;
1514 }
1515
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001516 dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
Pravin B Shelare6445712013-10-03 18:16:47 -07001517 GFP_KERNEL);
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001518 if (!dp->ports) {
1519 err = -ENOMEM;
1520 goto err_destroy_percpu;
1521 }
1522
1523 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1524 INIT_HLIST_HEAD(&dp->ports[i]);
1525
Jesse Grossccb13522011-10-25 19:26:31 -07001526 /* Set up our datapath device. */
1527 parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1528 parms.type = OVS_VPORT_TYPE_INTERNAL;
1529 parms.options = NULL;
1530 parms.dp = dp;
1531 parms.port_no = OVSP_LOCAL;
Alex Wang5cd667b2014-07-17 15:14:13 -07001532 parms.upcall_portids = a[OVS_DP_ATTR_UPCALL_PID];
Jesse Grossccb13522011-10-25 19:26:31 -07001533
Thomas Graf43d4be92013-12-13 15:22:18 +01001534 ovs_dp_change(dp, a);
1535
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001536 /* So far only local changes have been made, now need the lock. */
1537 ovs_lock();
1538
Jesse Grossccb13522011-10-25 19:26:31 -07001539 vport = new_vport(&parms);
1540 if (IS_ERR(vport)) {
1541 err = PTR_ERR(vport);
1542 if (err == -EBUSY)
1543 err = -EEXIST;
1544
Thomas Graf44da5ae2013-12-13 15:22:19 +01001545 if (err == -EEXIST) {
1546 /* An outdated user space instance that does not understand
1547 * the concept of user_features has attempted to create a new
1548 * datapath and is likely to reuse it. Drop all user features.
1549 */
1550 if (info->genlhdr->version < OVS_DP_VER_FEATURES)
1551 ovs_dp_reset_user_features(skb, info);
1552 }
1553
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001554 goto err_destroy_ports_array;
Jesse Grossccb13522011-10-25 19:26:31 -07001555 }
1556
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001557 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1558 info->snd_seq, 0, OVS_DP_CMD_NEW);
1559 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001560
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001561 ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001562 list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001563
1564 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001565
Johannes Berg2a94fe42013-11-19 15:19:39 +01001566 ovs_notify(&dp_datapath_genl_family, reply, info);
Jesse Grossccb13522011-10-25 19:26:31 -07001567 return 0;
1568
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001569err_destroy_ports_array:
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001570 ovs_unlock();
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001571 kfree(dp->ports);
Jesse Grossccb13522011-10-25 19:26:31 -07001572err_destroy_percpu:
1573 free_percpu(dp->stats_percpu);
1574err_destroy_table:
Pravin B Shelar9b996e52014-05-06 18:41:20 -07001575 ovs_flow_tbl_destroy(&dp->table);
Jesse Grossccb13522011-10-25 19:26:31 -07001576err_free_dp:
1577 kfree(dp);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001578err_free_reply:
1579 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001580err:
1581 return err;
1582}
1583
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001584/* Called with ovs_mutex. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001585static void __dp_destroy(struct datapath *dp)
Jesse Grossccb13522011-10-25 19:26:31 -07001586{
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001587 int i;
Jesse Grossccb13522011-10-25 19:26:31 -07001588
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001589 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1590 struct vport *vport;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001591 struct hlist_node *n;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001592
Sasha Levinb67bfe02013-02-27 17:06:00 -08001593 hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001594 if (vport->port_no != OVSP_LOCAL)
1595 ovs_dp_detach_port(vport);
1596 }
Jesse Grossccb13522011-10-25 19:26:31 -07001597
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001598 list_del_rcu(&dp->list_node);
Jesse Grossccb13522011-10-25 19:26:31 -07001599
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001600 /* OVSP_LOCAL is datapath internal port. We need to make sure that
Andy Zhoue80857c2014-01-21 09:31:04 -08001601 * all ports in datapath are destroyed first before freeing datapath.
Jesse Grossccb13522011-10-25 19:26:31 -07001602 */
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001603 ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
Jesse Grossccb13522011-10-25 19:26:31 -07001604
Andy Zhoue80857c2014-01-21 09:31:04 -08001605 /* RCU destroy the flow table */
Jesse Grossccb13522011-10-25 19:26:31 -07001606 call_rcu(&dp->rcu, destroy_dp_rcu);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001607}
1608
1609static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1610{
1611 struct sk_buff *reply;
1612 struct datapath *dp;
1613 int err;
1614
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001615 reply = ovs_dp_cmd_alloc_info(info);
1616 if (!reply)
1617 return -ENOMEM;
1618
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001619 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001620 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1621 err = PTR_ERR(dp);
1622 if (IS_ERR(dp))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001623 goto err_unlock_free;
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001624
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001625 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1626 info->snd_seq, 0, OVS_DP_CMD_DEL);
1627 BUG_ON(err < 0);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001628
1629 __dp_destroy(dp);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001630 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001631
Johannes Berg2a94fe42013-11-19 15:19:39 +01001632 ovs_notify(&dp_datapath_genl_family, reply, info);
Jesse Grossccb13522011-10-25 19:26:31 -07001633
1634 return 0;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001635
1636err_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001637 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001638 kfree_skb(reply);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001639 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001640}
1641
1642static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1643{
1644 struct sk_buff *reply;
1645 struct datapath *dp;
1646 int err;
1647
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001648 reply = ovs_dp_cmd_alloc_info(info);
1649 if (!reply)
1650 return -ENOMEM;
1651
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001652 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001653 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001654 err = PTR_ERR(dp);
Jesse Grossccb13522011-10-25 19:26:31 -07001655 if (IS_ERR(dp))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001656 goto err_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001657
Thomas Graf43d4be92013-12-13 15:22:18 +01001658 ovs_dp_change(dp, info->attrs);
1659
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001660 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1661 info->snd_seq, 0, OVS_DP_CMD_NEW);
1662 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001663
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001664 ovs_unlock();
Johannes Berg2a94fe42013-11-19 15:19:39 +01001665 ovs_notify(&dp_datapath_genl_family, reply, info);
Jesse Grossccb13522011-10-25 19:26:31 -07001666
1667 return 0;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001668
1669err_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001670 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001671 kfree_skb(reply);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001672 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001673}
1674
1675static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1676{
1677 struct sk_buff *reply;
1678 struct datapath *dp;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001679 int err;
Jesse Grossccb13522011-10-25 19:26:31 -07001680
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001681 reply = ovs_dp_cmd_alloc_info(info);
1682 if (!reply)
1683 return -ENOMEM;
1684
Pravin B Shelar8ec609d2014-11-11 15:55:16 -08001685 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001686 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001687 if (IS_ERR(dp)) {
1688 err = PTR_ERR(dp);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001689 goto err_unlock_free;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001690 }
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001691 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1692 info->snd_seq, 0, OVS_DP_CMD_NEW);
1693 BUG_ON(err < 0);
Pravin B Shelar8ec609d2014-11-11 15:55:16 -08001694 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001695
Jesse Grossccb13522011-10-25 19:26:31 -07001696 return genlmsg_reply(reply, info);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001697
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001698err_unlock_free:
Pravin B Shelar8ec609d2014-11-11 15:55:16 -08001699 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001700 kfree_skb(reply);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001701 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001702}
1703
1704static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1705{
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001706 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
Jesse Grossccb13522011-10-25 19:26:31 -07001707 struct datapath *dp;
1708 int skip = cb->args[0];
1709 int i = 0;
1710
Pravin B Shelar8ec609d2014-11-11 15:55:16 -08001711 ovs_lock();
1712 list_for_each_entry(dp, &ovs_net->dps, list_node) {
Ben Pfaff77676fd2012-01-17 13:33:39 +00001713 if (i >= skip &&
Eric W. Biederman15e47302012-09-07 20:12:54 +00001714 ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001715 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1716 OVS_DP_CMD_NEW) < 0)
1717 break;
1718 i++;
1719 }
Pravin B Shelar8ec609d2014-11-11 15:55:16 -08001720 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001721
1722 cb->args[0] = i;
1723
1724 return skb->len;
1725}
1726
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001727static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1728 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1729 [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1730 [OVS_DP_ATTR_USER_FEATURES] = { .type = NLA_U32 },
1731};
1732
stephen hemminger48e48a72014-07-16 11:25:52 -07001733static const struct genl_ops dp_datapath_genl_ops[] = {
Jesse Grossccb13522011-10-25 19:26:31 -07001734 { .cmd = OVS_DP_CMD_NEW,
1735 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1736 .policy = datapath_policy,
1737 .doit = ovs_dp_cmd_new
1738 },
1739 { .cmd = OVS_DP_CMD_DEL,
1740 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1741 .policy = datapath_policy,
1742 .doit = ovs_dp_cmd_del
1743 },
1744 { .cmd = OVS_DP_CMD_GET,
1745 .flags = 0, /* OK for unprivileged users. */
1746 .policy = datapath_policy,
1747 .doit = ovs_dp_cmd_get,
1748 .dumpit = ovs_dp_cmd_dump
1749 },
1750 { .cmd = OVS_DP_CMD_SET,
1751 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1752 .policy = datapath_policy,
1753 .doit = ovs_dp_cmd_set,
1754 },
1755};
1756
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001757static struct genl_family dp_datapath_genl_family = {
Jesse Grossccb13522011-10-25 19:26:31 -07001758 .id = GENL_ID_GENERATE,
1759 .hdrsize = sizeof(struct ovs_header),
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001760 .name = OVS_DATAPATH_FAMILY,
1761 .version = OVS_DATAPATH_VERSION,
1762 .maxattr = OVS_DP_ATTR_MAX,
Pravin B Shelar3a4e0d62013-04-23 07:48:48 +00001763 .netnsok = true,
1764 .parallel_ops = true,
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001765 .ops = dp_datapath_genl_ops,
1766 .n_ops = ARRAY_SIZE(dp_datapath_genl_ops),
1767 .mcgrps = &ovs_dp_datapath_multicast_group,
1768 .n_mcgrps = 1,
Jesse Grossccb13522011-10-25 19:26:31 -07001769};
1770
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001771/* Called with ovs_mutex or RCU read lock. */
Jesse Grossccb13522011-10-25 19:26:31 -07001772static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001773 u32 portid, u32 seq, u32 flags, u8 cmd)
Jesse Grossccb13522011-10-25 19:26:31 -07001774{
1775 struct ovs_header *ovs_header;
1776 struct ovs_vport_stats vport_stats;
1777 int err;
1778
Eric W. Biederman15e47302012-09-07 20:12:54 +00001779 ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
Jesse Grossccb13522011-10-25 19:26:31 -07001780 flags, cmd);
1781 if (!ovs_header)
1782 return -EMSGSIZE;
1783
1784 ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1785
David S. Miller028d6a62012-03-29 23:20:48 -04001786 if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
1787 nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
Alex Wang5cd667b2014-07-17 15:14:13 -07001788 nla_put_string(skb, OVS_VPORT_ATTR_NAME,
1789 vport->ops->get_name(vport)))
David S. Miller028d6a62012-03-29 23:20:48 -04001790 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001791
1792 ovs_vport_get_stats(vport, &vport_stats);
David S. Miller028d6a62012-03-29 23:20:48 -04001793 if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
1794 &vport_stats))
1795 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001796
Alex Wang5cd667b2014-07-17 15:14:13 -07001797 if (ovs_vport_get_upcall_portids(vport, skb))
1798 goto nla_put_failure;
1799
Jesse Grossccb13522011-10-25 19:26:31 -07001800 err = ovs_vport_get_options(vport, skb);
1801 if (err == -EMSGSIZE)
1802 goto error;
1803
Johannes Berg053c0952015-01-16 22:09:00 +01001804 genlmsg_end(skb, ovs_header);
1805 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001806
1807nla_put_failure:
1808 err = -EMSGSIZE;
1809error:
1810 genlmsg_cancel(skb, ovs_header);
1811 return err;
1812}
1813
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001814static struct sk_buff *ovs_vport_cmd_alloc_info(void)
1815{
1816 return nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1817}
1818
1819/* Called with ovs_mutex, only via ovs_dp_notify_wq(). */
Eric W. Biederman15e47302012-09-07 20:12:54 +00001820struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001821 u32 seq, u8 cmd)
1822{
1823 struct sk_buff *skb;
1824 int retval;
1825
1826 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1827 if (!skb)
1828 return ERR_PTR(-ENOMEM);
1829
Eric W. Biederman15e47302012-09-07 20:12:54 +00001830 retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
Jesse Grossa9341512013-03-26 15:48:38 -07001831 BUG_ON(retval < 0);
1832
Jesse Grossccb13522011-10-25 19:26:31 -07001833 return skb;
1834}
1835
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001836/* Called with ovs_mutex or RCU read lock. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001837static struct vport *lookup_vport(struct net *net,
Thomas Graf12eb18f2014-11-06 06:58:52 -08001838 const struct ovs_header *ovs_header,
Jesse Grossccb13522011-10-25 19:26:31 -07001839 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1840{
1841 struct datapath *dp;
1842 struct vport *vport;
1843
1844 if (a[OVS_VPORT_ATTR_NAME]) {
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001845 vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
Jesse Grossccb13522011-10-25 19:26:31 -07001846 if (!vport)
1847 return ERR_PTR(-ENODEV);
Ben Pfaff651a68e2012-03-06 15:04:04 -08001848 if (ovs_header->dp_ifindex &&
1849 ovs_header->dp_ifindex != get_dpifindex(vport->dp))
1850 return ERR_PTR(-ENODEV);
Jesse Grossccb13522011-10-25 19:26:31 -07001851 return vport;
1852 } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
1853 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1854
1855 if (port_no >= DP_MAX_PORTS)
1856 return ERR_PTR(-EFBIG);
1857
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001858 dp = get_dp(net, ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001859 if (!dp)
1860 return ERR_PTR(-ENODEV);
1861
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001862 vport = ovs_vport_ovsl_rcu(dp, port_no);
Jesse Grossccb13522011-10-25 19:26:31 -07001863 if (!vport)
Jarno Rajahalme14408db2013-01-09 14:27:35 -08001864 return ERR_PTR(-ENODEV);
Jesse Grossccb13522011-10-25 19:26:31 -07001865 return vport;
1866 } else
1867 return ERR_PTR(-EINVAL);
1868}
1869
1870static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
1871{
1872 struct nlattr **a = info->attrs;
1873 struct ovs_header *ovs_header = info->userhdr;
1874 struct vport_parms parms;
1875 struct sk_buff *reply;
1876 struct vport *vport;
1877 struct datapath *dp;
1878 u32 port_no;
1879 int err;
1880
Jesse Grossccb13522011-10-25 19:26:31 -07001881 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
1882 !a[OVS_VPORT_ATTR_UPCALL_PID])
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001883 return -EINVAL;
1884
1885 port_no = a[OVS_VPORT_ATTR_PORT_NO]
1886 ? nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]) : 0;
1887 if (port_no >= DP_MAX_PORTS)
1888 return -EFBIG;
1889
1890 reply = ovs_vport_cmd_alloc_info();
1891 if (!reply)
1892 return -ENOMEM;
Jesse Grossccb13522011-10-25 19:26:31 -07001893
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001894 ovs_lock();
Thomas Graf62b9c8d2014-10-22 17:29:06 +02001895restart:
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001896 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001897 err = -ENODEV;
1898 if (!dp)
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001899 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001900
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001901 if (port_no) {
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001902 vport = ovs_vport_ovsl(dp, port_no);
Jesse Grossccb13522011-10-25 19:26:31 -07001903 err = -EBUSY;
1904 if (vport)
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001905 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001906 } else {
1907 for (port_no = 1; ; port_no++) {
1908 if (port_no >= DP_MAX_PORTS) {
1909 err = -EFBIG;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001910 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001911 }
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001912 vport = ovs_vport_ovsl(dp, port_no);
Jesse Grossccb13522011-10-25 19:26:31 -07001913 if (!vport)
1914 break;
1915 }
1916 }
1917
1918 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
1919 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1920 parms.options = a[OVS_VPORT_ATTR_OPTIONS];
1921 parms.dp = dp;
1922 parms.port_no = port_no;
Alex Wang5cd667b2014-07-17 15:14:13 -07001923 parms.upcall_portids = a[OVS_VPORT_ATTR_UPCALL_PID];
Jesse Grossccb13522011-10-25 19:26:31 -07001924
1925 vport = new_vport(&parms);
1926 err = PTR_ERR(vport);
Thomas Graf62b9c8d2014-10-22 17:29:06 +02001927 if (IS_ERR(vport)) {
1928 if (err == -EAGAIN)
1929 goto restart;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001930 goto exit_unlock_free;
Thomas Graf62b9c8d2014-10-22 17:29:06 +02001931 }
Jesse Grossccb13522011-10-25 19:26:31 -07001932
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001933 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1934 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1935 BUG_ON(err < 0);
1936 ovs_unlock();
Thomas Grafed661182013-03-29 14:46:50 +01001937
Johannes Berg2a94fe42013-11-19 15:19:39 +01001938 ovs_notify(&dp_vport_genl_family, reply, info);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001939 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001940
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001941exit_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001942 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001943 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001944 return err;
1945}
1946
1947static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
1948{
1949 struct nlattr **a = info->attrs;
1950 struct sk_buff *reply;
1951 struct vport *vport;
1952 int err;
1953
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001954 reply = ovs_vport_cmd_alloc_info();
1955 if (!reply)
1956 return -ENOMEM;
1957
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001958 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001959 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
Jesse Grossccb13522011-10-25 19:26:31 -07001960 err = PTR_ERR(vport);
1961 if (IS_ERR(vport))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001962 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001963
Jesse Grossccb13522011-10-25 19:26:31 -07001964 if (a[OVS_VPORT_ATTR_TYPE] &&
Jesse Grossf44f3402013-05-13 08:15:26 -07001965 nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
Jesse Grossccb13522011-10-25 19:26:31 -07001966 err = -EINVAL;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001967 goto exit_unlock_free;
Jesse Grossa9341512013-03-26 15:48:38 -07001968 }
1969
Jesse Grossf44f3402013-05-13 08:15:26 -07001970 if (a[OVS_VPORT_ATTR_OPTIONS]) {
Jesse Grossccb13522011-10-25 19:26:31 -07001971 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
Jesse Grossf44f3402013-05-13 08:15:26 -07001972 if (err)
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001973 goto exit_unlock_free;
Jesse Grossf44f3402013-05-13 08:15:26 -07001974 }
Jesse Grossa9341512013-03-26 15:48:38 -07001975
Alex Wang5cd667b2014-07-17 15:14:13 -07001976
1977 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
1978 struct nlattr *ids = a[OVS_VPORT_ATTR_UPCALL_PID];
1979
1980 err = ovs_vport_set_upcall_portids(vport, ids);
1981 if (err)
1982 goto exit_unlock_free;
1983 }
Jesse Grossccb13522011-10-25 19:26:31 -07001984
Jesse Grossa9341512013-03-26 15:48:38 -07001985 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1986 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1987 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001988
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001989 ovs_unlock();
Johannes Berg2a94fe42013-11-19 15:19:39 +01001990 ovs_notify(&dp_vport_genl_family, reply, info);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001991 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001992
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001993exit_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001994 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001995 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001996 return err;
1997}
1998
1999static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
2000{
2001 struct nlattr **a = info->attrs;
2002 struct sk_buff *reply;
2003 struct vport *vport;
2004 int err;
2005
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07002006 reply = ovs_vport_cmd_alloc_info();
2007 if (!reply)
2008 return -ENOMEM;
2009
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002010 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002011 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
Jesse Grossccb13522011-10-25 19:26:31 -07002012 err = PTR_ERR(vport);
2013 if (IS_ERR(vport))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07002014 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07002015
2016 if (vport->port_no == OVSP_LOCAL) {
2017 err = -EINVAL;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07002018 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07002019 }
2020
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07002021 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
2022 info->snd_seq, 0, OVS_VPORT_CMD_DEL);
2023 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07002024 ovs_dp_detach_port(vport);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07002025 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07002026
Johannes Berg2a94fe42013-11-19 15:19:39 +01002027 ovs_notify(&dp_vport_genl_family, reply, info);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07002028 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -07002029
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07002030exit_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002031 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07002032 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07002033 return err;
2034}
2035
2036static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
2037{
2038 struct nlattr **a = info->attrs;
2039 struct ovs_header *ovs_header = info->userhdr;
2040 struct sk_buff *reply;
2041 struct vport *vport;
2042 int err;
2043
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07002044 reply = ovs_vport_cmd_alloc_info();
2045 if (!reply)
2046 return -ENOMEM;
2047
Jesse Grossccb13522011-10-25 19:26:31 -07002048 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002049 vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
Jesse Grossccb13522011-10-25 19:26:31 -07002050 err = PTR_ERR(vport);
2051 if (IS_ERR(vport))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07002052 goto exit_unlock_free;
2053 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
2054 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
2055 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07002056 rcu_read_unlock();
2057
2058 return genlmsg_reply(reply, info);
2059
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07002060exit_unlock_free:
Jesse Grossccb13522011-10-25 19:26:31 -07002061 rcu_read_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07002062 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07002063 return err;
2064}
2065
2066static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
2067{
2068 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
2069 struct datapath *dp;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07002070 int bucket = cb->args[0], skip = cb->args[1];
2071 int i, j = 0;
Jesse Grossccb13522011-10-25 19:26:31 -07002072
Jesse Grossccb13522011-10-25 19:26:31 -07002073 rcu_read_lock();
Andy Zhoucc3a5ae2014-09-08 13:14:22 -07002074 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
Jarno Rajahalme42ee19e2014-02-15 17:42:29 -08002075 if (!dp) {
2076 rcu_read_unlock();
2077 return -ENODEV;
2078 }
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07002079 for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
Jesse Grossccb13522011-10-25 19:26:31 -07002080 struct vport *vport;
2081
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07002082 j = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -08002083 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07002084 if (j >= skip &&
2085 ovs_vport_cmd_fill_info(vport, skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00002086 NETLINK_CB(cb->skb).portid,
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07002087 cb->nlh->nlmsg_seq,
2088 NLM_F_MULTI,
2089 OVS_VPORT_CMD_NEW) < 0)
2090 goto out;
Jesse Grossccb13522011-10-25 19:26:31 -07002091
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07002092 j++;
2093 }
2094 skip = 0;
Jesse Grossccb13522011-10-25 19:26:31 -07002095 }
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07002096out:
Jesse Grossccb13522011-10-25 19:26:31 -07002097 rcu_read_unlock();
2098
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07002099 cb->args[0] = i;
2100 cb->args[1] = j;
Jesse Grossccb13522011-10-25 19:26:31 -07002101
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07002102 return skb->len;
Jesse Grossccb13522011-10-25 19:26:31 -07002103}
2104
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002105static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
2106 [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
2107 [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
2108 [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
2109 [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
2110 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
2111 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
2112};
2113
stephen hemminger48e48a72014-07-16 11:25:52 -07002114static const struct genl_ops dp_vport_genl_ops[] = {
Jesse Grossccb13522011-10-25 19:26:31 -07002115 { .cmd = OVS_VPORT_CMD_NEW,
2116 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2117 .policy = vport_policy,
2118 .doit = ovs_vport_cmd_new
2119 },
2120 { .cmd = OVS_VPORT_CMD_DEL,
2121 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2122 .policy = vport_policy,
2123 .doit = ovs_vport_cmd_del
2124 },
2125 { .cmd = OVS_VPORT_CMD_GET,
2126 .flags = 0, /* OK for unprivileged users. */
2127 .policy = vport_policy,
2128 .doit = ovs_vport_cmd_get,
2129 .dumpit = ovs_vport_cmd_dump
2130 },
2131 { .cmd = OVS_VPORT_CMD_SET,
2132 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2133 .policy = vport_policy,
2134 .doit = ovs_vport_cmd_set,
2135 },
2136};
2137
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002138struct genl_family dp_vport_genl_family = {
2139 .id = GENL_ID_GENERATE,
2140 .hdrsize = sizeof(struct ovs_header),
2141 .name = OVS_VPORT_FAMILY,
2142 .version = OVS_VPORT_VERSION,
2143 .maxattr = OVS_VPORT_ATTR_MAX,
2144 .netnsok = true,
2145 .parallel_ops = true,
2146 .ops = dp_vport_genl_ops,
2147 .n_ops = ARRAY_SIZE(dp_vport_genl_ops),
2148 .mcgrps = &ovs_dp_vport_multicast_group,
2149 .n_mcgrps = 1,
Jesse Grossccb13522011-10-25 19:26:31 -07002150};
2151
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002152static struct genl_family * const dp_genl_families[] = {
2153 &dp_datapath_genl_family,
2154 &dp_vport_genl_family,
2155 &dp_flow_genl_family,
2156 &dp_packet_genl_family,
Jesse Grossccb13522011-10-25 19:26:31 -07002157};
2158
2159static void dp_unregister_genl(int n_families)
2160{
2161 int i;
2162
2163 for (i = 0; i < n_families; i++)
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002164 genl_unregister_family(dp_genl_families[i]);
Jesse Grossccb13522011-10-25 19:26:31 -07002165}
2166
2167static int dp_register_genl(void)
2168{
Jesse Grossccb13522011-10-25 19:26:31 -07002169 int err;
2170 int i;
2171
Jesse Grossccb13522011-10-25 19:26:31 -07002172 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
Jesse Grossccb13522011-10-25 19:26:31 -07002173
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002174 err = genl_register_family(dp_genl_families[i]);
Jesse Grossccb13522011-10-25 19:26:31 -07002175 if (err)
2176 goto error;
Jesse Grossccb13522011-10-25 19:26:31 -07002177 }
2178
2179 return 0;
2180
2181error:
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002182 dp_unregister_genl(i);
Jesse Grossccb13522011-10-25 19:26:31 -07002183 return err;
2184}
2185
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002186static int __net_init ovs_init_net(struct net *net)
2187{
2188 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2189
2190 INIT_LIST_HEAD(&ovs_net->dps);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002191 INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002192 return 0;
2193}
2194
Pravin B Shelar7b4577a2015-02-17 11:23:10 -08002195static void __net_exit list_vports_from_net(struct net *net, struct net *dnet,
2196 struct list_head *head)
2197{
2198 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2199 struct datapath *dp;
2200
2201 list_for_each_entry(dp, &ovs_net->dps, list_node) {
2202 int i;
2203
2204 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
2205 struct vport *vport;
2206
2207 hlist_for_each_entry(vport, &dp->ports[i], dp_hash_node) {
2208 struct netdev_vport *netdev_vport;
2209
2210 if (vport->ops->type != OVS_VPORT_TYPE_INTERNAL)
2211 continue;
2212
2213 netdev_vport = netdev_vport_priv(vport);
2214 if (dev_net(netdev_vport->dev) == dnet)
2215 list_add(&vport->detach_list, head);
2216 }
2217 }
2218 }
2219}
2220
2221static void __net_exit ovs_exit_net(struct net *dnet)
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002222{
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002223 struct datapath *dp, *dp_next;
Pravin B Shelar7b4577a2015-02-17 11:23:10 -08002224 struct ovs_net *ovs_net = net_generic(dnet, ovs_net_id);
2225 struct vport *vport, *vport_next;
2226 struct net *net;
2227 LIST_HEAD(head);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002228
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002229 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002230 list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
2231 __dp_destroy(dp);
Pravin B Shelar7b4577a2015-02-17 11:23:10 -08002232
2233 rtnl_lock();
2234 for_each_net(net)
2235 list_vports_from_net(net, dnet, &head);
2236 rtnl_unlock();
2237
2238 /* Detach all vports from given namespace. */
2239 list_for_each_entry_safe(vport, vport_next, &head, detach_list) {
2240 list_del(&vport->detach_list);
2241 ovs_dp_detach_port(vport);
2242 }
2243
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002244 ovs_unlock();
2245
2246 cancel_work_sync(&ovs_net->dp_notify_work);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002247}
2248
2249static struct pernet_operations ovs_net_ops = {
2250 .init = ovs_init_net,
2251 .exit = ovs_exit_net,
2252 .id = &ovs_net_id,
2253 .size = sizeof(struct ovs_net),
2254};
2255
Jesse Grossccb13522011-10-25 19:26:31 -07002256static int __init dp_init(void)
2257{
Jesse Grossccb13522011-10-25 19:26:31 -07002258 int err;
2259
YOSHIFUJI Hideaki / 吉藤英明3523b292013-01-09 07:19:55 +00002260 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
Jesse Grossccb13522011-10-25 19:26:31 -07002261
2262 pr_info("Open vSwitch switching datapath\n");
2263
Andy Zhou971427f32014-09-15 19:37:25 -07002264 err = action_fifos_init();
Jesse Grossccb13522011-10-25 19:26:31 -07002265 if (err)
2266 goto error;
2267
Andy Zhou971427f32014-09-15 19:37:25 -07002268 err = ovs_internal_dev_rtnl_link_register();
2269 if (err)
2270 goto error_action_fifos_exit;
2271
Jiri Pirko5b9e7e12014-06-26 09:58:26 +02002272 err = ovs_flow_init();
2273 if (err)
2274 goto error_unreg_rtnl_link;
2275
Jesse Grossccb13522011-10-25 19:26:31 -07002276 err = ovs_vport_init();
2277 if (err)
2278 goto error_flow_exit;
2279
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002280 err = register_pernet_device(&ovs_net_ops);
Jesse Grossccb13522011-10-25 19:26:31 -07002281 if (err)
2282 goto error_vport_exit;
2283
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002284 err = register_netdevice_notifier(&ovs_dp_device_notifier);
2285 if (err)
2286 goto error_netns_exit;
2287
Thomas Graf62b9c8d2014-10-22 17:29:06 +02002288 err = ovs_netdev_init();
2289 if (err)
2290 goto error_unreg_notifier;
2291
Jesse Grossccb13522011-10-25 19:26:31 -07002292 err = dp_register_genl();
2293 if (err < 0)
Thomas Graf62b9c8d2014-10-22 17:29:06 +02002294 goto error_unreg_netdev;
Jesse Grossccb13522011-10-25 19:26:31 -07002295
Jesse Grossccb13522011-10-25 19:26:31 -07002296 return 0;
2297
Thomas Graf62b9c8d2014-10-22 17:29:06 +02002298error_unreg_netdev:
2299 ovs_netdev_exit();
Jesse Grossccb13522011-10-25 19:26:31 -07002300error_unreg_notifier:
2301 unregister_netdevice_notifier(&ovs_dp_device_notifier);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002302error_netns_exit:
2303 unregister_pernet_device(&ovs_net_ops);
Jesse Grossccb13522011-10-25 19:26:31 -07002304error_vport_exit:
2305 ovs_vport_exit();
2306error_flow_exit:
2307 ovs_flow_exit();
Jiri Pirko5b9e7e12014-06-26 09:58:26 +02002308error_unreg_rtnl_link:
2309 ovs_internal_dev_rtnl_link_unregister();
Andy Zhou971427f32014-09-15 19:37:25 -07002310error_action_fifos_exit:
2311 action_fifos_exit();
Jesse Grossccb13522011-10-25 19:26:31 -07002312error:
2313 return err;
2314}
2315
2316static void dp_cleanup(void)
2317{
Jesse Grossccb13522011-10-25 19:26:31 -07002318 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
Thomas Graf62b9c8d2014-10-22 17:29:06 +02002319 ovs_netdev_exit();
Jesse Grossccb13522011-10-25 19:26:31 -07002320 unregister_netdevice_notifier(&ovs_dp_device_notifier);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002321 unregister_pernet_device(&ovs_net_ops);
2322 rcu_barrier();
Jesse Grossccb13522011-10-25 19:26:31 -07002323 ovs_vport_exit();
2324 ovs_flow_exit();
Jiri Pirko5b9e7e12014-06-26 09:58:26 +02002325 ovs_internal_dev_rtnl_link_unregister();
Andy Zhou971427f32014-09-15 19:37:25 -07002326 action_fifos_exit();
Jesse Grossccb13522011-10-25 19:26:31 -07002327}
2328
2329module_init(dp_init);
2330module_exit(dp_cleanup);
2331
2332MODULE_DESCRIPTION("Open vSwitch switching datapath");
2333MODULE_LICENSE("GPL");