blob: 22665a6144fc9afc1e3b25a2c49c04ddb959fa3f [file] [log] [blame]
Jesse Grossccb13522011-10-25 19:26:31 -07001/*
Andy Zhou03f0d912013-08-07 20:01:00 -07002 * Copyright (c) 2007-2013 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>
Pravin B Shelar8e4e1712013-04-15 13:23:03 -070047#include <linux/lockdep.h>
Jesse Grossccb13522011-10-25 19:26:31 -070048#include <linux/openvswitch.h>
49#include <linux/rculist.h>
50#include <linux/dmi.h>
51#include <linux/workqueue.h>
52#include <net/genetlink.h>
Pravin B Shelar46df7b82012-02-22 19:58:59 -080053#include <net/net_namespace.h>
54#include <net/netns/generic.h>
Jesse Grossccb13522011-10-25 19:26:31 -070055
56#include "datapath.h"
57#include "flow.h"
Andy Zhoue80857c2014-01-21 09:31:04 -080058#include "flow_table.h"
Pravin B Shelare6445712013-10-03 18:16:47 -070059#include "flow_netlink.h"
Jesse Grossccb13522011-10-25 19:26:31 -070060#include "vport-internal_dev.h"
Thomas Grafcff63a52013-04-29 13:06:41 +000061#include "vport-netdev.h"
Jesse Grossccb13522011-10-25 19:26:31 -070062
Pravin B Shelar8e4e1712013-04-15 13:23:03 -070063int ovs_net_id __read_mostly;
64
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -070065/* Check if need to build a reply message.
66 * OVS userspace sets the NLM_F_ECHO flag if it needs the reply. */
67static bool ovs_must_notify(struct genl_info *info,
68 const struct genl_multicast_group *grp)
69{
70 return info->nlhdr->nlmsg_flags & NLM_F_ECHO ||
71 netlink_has_listeners(genl_info_net(info)->genl_sock, 0);
72}
73
Johannes Berg68eb5502013-11-19 15:19:38 +010074static void ovs_notify(struct genl_family *family,
Johannes Berg2a94fe42013-11-19 15:19:39 +010075 struct sk_buff *skb, struct genl_info *info)
Thomas Grafed6611852013-03-29 14:46:50 +010076{
Johannes Berg68eb5502013-11-19 15:19:38 +010077 genl_notify(family, skb, genl_info_net(info), info->snd_portid,
Johannes Berg2a94fe42013-11-19 15:19:39 +010078 0, info->nlhdr, GFP_KERNEL);
Thomas Grafed6611852013-03-29 14:46:50 +010079}
80
Pravin B Shelar46df7b82012-02-22 19:58:59 -080081/**
Jesse Grossccb13522011-10-25 19:26:31 -070082 * DOC: Locking:
83 *
Pravin B Shelar8e4e1712013-04-15 13:23:03 -070084 * All writes e.g. Writes to device state (add/remove datapath, port, set
85 * operations on vports, etc.), Writes to other state (flow table
86 * modifications, set miscellaneous datapath parameters, etc.) are protected
87 * by ovs_lock.
Jesse Grossccb13522011-10-25 19:26:31 -070088 *
89 * Reads are protected by RCU.
90 *
91 * There are a few special cases (mostly stats) that have their own
92 * synchronization but they nest under all of above and don't interact with
93 * each other.
Pravin B Shelar8e4e1712013-04-15 13:23:03 -070094 *
95 * The RTNL lock nests inside ovs_mutex.
Jesse Grossccb13522011-10-25 19:26:31 -070096 */
97
Pravin B Shelar8e4e1712013-04-15 13:23:03 -070098static DEFINE_MUTEX(ovs_mutex);
99
100void ovs_lock(void)
101{
102 mutex_lock(&ovs_mutex);
103}
104
105void ovs_unlock(void)
106{
107 mutex_unlock(&ovs_mutex);
108}
109
110#ifdef CONFIG_LOCKDEP
111int lockdep_ovsl_is_held(void)
112{
113 if (debug_locks)
114 return lockdep_is_held(&ovs_mutex);
115 else
116 return 1;
117}
118#endif
119
Jesse Grossccb13522011-10-25 19:26:31 -0700120static struct vport *new_vport(const struct vport_parms *);
Thomas Graf8055a892013-12-13 15:22:20 +0100121static int queue_gso_packets(struct datapath *dp, struct sk_buff *,
Jesse Grossccb13522011-10-25 19:26:31 -0700122 const struct dp_upcall_info *);
Thomas Graf8055a892013-12-13 15:22:20 +0100123static int queue_userspace_packet(struct datapath *dp, struct sk_buff *,
Jesse Grossccb13522011-10-25 19:26:31 -0700124 const struct dp_upcall_info *);
125
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700126/* Must be called with rcu_read_lock or ovs_mutex. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800127static struct datapath *get_dp(struct net *net, int dp_ifindex)
Jesse Grossccb13522011-10-25 19:26:31 -0700128{
129 struct datapath *dp = NULL;
130 struct net_device *dev;
131
132 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800133 dev = dev_get_by_index_rcu(net, dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -0700134 if (dev) {
135 struct vport *vport = ovs_internal_dev_get_vport(dev);
136 if (vport)
137 dp = vport->dp;
138 }
139 rcu_read_unlock();
140
141 return dp;
142}
143
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700144/* Must be called with rcu_read_lock or ovs_mutex. */
Stephen Hemminger443cd882013-12-17 19:22:48 +0000145static const char *ovs_dp_name(const struct datapath *dp)
Jesse Grossccb13522011-10-25 19:26:31 -0700146{
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700147 struct vport *vport = ovs_vport_ovsl_rcu(dp, OVSP_LOCAL);
Jesse Grossccb13522011-10-25 19:26:31 -0700148 return vport->ops->get_name(vport);
149}
150
151static int get_dpifindex(struct datapath *dp)
152{
153 struct vport *local;
154 int ifindex;
155
156 rcu_read_lock();
157
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700158 local = ovs_vport_rcu(dp, OVSP_LOCAL);
Jesse Grossccb13522011-10-25 19:26:31 -0700159 if (local)
Thomas Grafcff63a52013-04-29 13:06:41 +0000160 ifindex = netdev_vport_priv(local)->dev->ifindex;
Jesse Grossccb13522011-10-25 19:26:31 -0700161 else
162 ifindex = 0;
163
164 rcu_read_unlock();
165
166 return ifindex;
167}
168
169static void destroy_dp_rcu(struct rcu_head *rcu)
170{
171 struct datapath *dp = container_of(rcu, struct datapath, rcu);
172
Jesse Grossccb13522011-10-25 19:26:31 -0700173 free_percpu(dp->stats_percpu);
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800174 release_net(ovs_dp_get_net(dp));
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700175 kfree(dp->ports);
Jesse Grossccb13522011-10-25 19:26:31 -0700176 kfree(dp);
177}
178
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700179static struct hlist_head *vport_hash_bucket(const struct datapath *dp,
180 u16 port_no)
181{
182 return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)];
183}
184
Jarno Rajahalmebb6f9a72014-05-05 11:32:17 -0700185/* Called with ovs_mutex or RCU read lock. */
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700186struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no)
187{
188 struct vport *vport;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700189 struct hlist_head *head;
190
191 head = vport_hash_bucket(dp, port_no);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800192 hlist_for_each_entry_rcu(vport, head, dp_hash_node) {
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700193 if (vport->port_no == port_no)
194 return vport;
195 }
196 return NULL;
197}
198
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700199/* Called with ovs_mutex. */
Jesse Grossccb13522011-10-25 19:26:31 -0700200static struct vport *new_vport(const struct vport_parms *parms)
201{
202 struct vport *vport;
203
204 vport = ovs_vport_add(parms);
205 if (!IS_ERR(vport)) {
206 struct datapath *dp = parms->dp;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700207 struct hlist_head *head = vport_hash_bucket(dp, vport->port_no);
Jesse Grossccb13522011-10-25 19:26:31 -0700208
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700209 hlist_add_head_rcu(&vport->dp_hash_node, head);
Jesse Grossccb13522011-10-25 19:26:31 -0700210 }
Jesse Grossccb13522011-10-25 19:26:31 -0700211 return vport;
212}
213
Jesse Grossccb13522011-10-25 19:26:31 -0700214void ovs_dp_detach_port(struct vport *p)
215{
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700216 ASSERT_OVSL();
Jesse Grossccb13522011-10-25 19:26:31 -0700217
218 /* First drop references to device. */
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700219 hlist_del_rcu(&p->dp_hash_node);
Jesse Grossccb13522011-10-25 19:26:31 -0700220
221 /* Then destroy it. */
222 ovs_vport_del(p);
223}
224
225/* Must be called with rcu_read_lock. */
226void ovs_dp_process_received_packet(struct vport *p, struct sk_buff *skb)
227{
228 struct datapath *dp = p->dp;
229 struct sw_flow *flow;
230 struct dp_stats_percpu *stats;
231 struct sw_flow_key key;
232 u64 *stats_counter;
Andy Zhou1bd71162013-10-22 10:42:46 -0700233 u32 n_mask_hit;
Jesse Grossccb13522011-10-25 19:26:31 -0700234 int error;
Jesse Grossccb13522011-10-25 19:26:31 -0700235
Shan Wei404f2f12012-11-13 09:52:25 +0800236 stats = this_cpu_ptr(dp->stats_percpu);
Jesse Grossccb13522011-10-25 19:26:31 -0700237
238 /* Extract flow from 'skb' into 'key'. */
Andy Zhou03f0d912013-08-07 20:01:00 -0700239 error = ovs_flow_extract(skb, p->port_no, &key);
Jesse Grossccb13522011-10-25 19:26:31 -0700240 if (unlikely(error)) {
241 kfree_skb(skb);
242 return;
243 }
244
245 /* Look up flow. */
Andy Zhou5bb50632013-11-25 10:42:46 -0800246 flow = ovs_flow_tbl_lookup_stats(&dp->table, &key, &n_mask_hit);
Jesse Grossccb13522011-10-25 19:26:31 -0700247 if (unlikely(!flow)) {
248 struct dp_upcall_info upcall;
249
250 upcall.cmd = OVS_PACKET_CMD_MISS;
251 upcall.key = &key;
252 upcall.userdata = NULL;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000253 upcall.portid = p->upcall_portid;
Jesse Grossccb13522011-10-25 19:26:31 -0700254 ovs_dp_upcall(dp, skb, &upcall);
255 consume_skb(skb);
256 stats_counter = &stats->n_missed;
257 goto out;
258 }
259
260 OVS_CB(skb)->flow = flow;
Andy Zhou03f0d912013-08-07 20:01:00 -0700261 OVS_CB(skb)->pkt_key = &key;
Jesse Grossccb13522011-10-25 19:26:31 -0700262
Pravin B Shelare298e502013-10-29 17:22:21 -0700263 ovs_flow_stats_update(OVS_CB(skb)->flow, skb);
Jesse Grossccb13522011-10-25 19:26:31 -0700264 ovs_execute_actions(dp, skb);
Pravin B Shelare298e502013-10-29 17:22:21 -0700265 stats_counter = &stats->n_hit;
Jesse Grossccb13522011-10-25 19:26:31 -0700266
267out:
268 /* Update datapath statistics. */
WANG Congdf9d9fd2014-02-14 15:10:46 -0800269 u64_stats_update_begin(&stats->syncp);
Jesse Grossccb13522011-10-25 19:26:31 -0700270 (*stats_counter)++;
Andy Zhou1bd71162013-10-22 10:42:46 -0700271 stats->n_mask_hit += n_mask_hit;
WANG Congdf9d9fd2014-02-14 15:10:46 -0800272 u64_stats_update_end(&stats->syncp);
Jesse Grossccb13522011-10-25 19:26:31 -0700273}
274
275static struct genl_family dp_packet_genl_family = {
276 .id = GENL_ID_GENERATE,
277 .hdrsize = sizeof(struct ovs_header),
278 .name = OVS_PACKET_FAMILY,
279 .version = OVS_PACKET_VERSION,
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800280 .maxattr = OVS_PACKET_ATTR_MAX,
Pravin B Shelar3a4e0d62013-04-23 07:48:48 +0000281 .netnsok = true,
282 .parallel_ops = true,
Jesse Grossccb13522011-10-25 19:26:31 -0700283};
284
285int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800286 const struct dp_upcall_info *upcall_info)
Jesse Grossccb13522011-10-25 19:26:31 -0700287{
288 struct dp_stats_percpu *stats;
Jesse Grossccb13522011-10-25 19:26:31 -0700289 int err;
290
Eric W. Biederman15e47302012-09-07 20:12:54 +0000291 if (upcall_info->portid == 0) {
Jesse Grossccb13522011-10-25 19:26:31 -0700292 err = -ENOTCONN;
293 goto err;
294 }
295
Jesse Grossccb13522011-10-25 19:26:31 -0700296 if (!skb_is_gso(skb))
Thomas Graf8055a892013-12-13 15:22:20 +0100297 err = queue_userspace_packet(dp, skb, upcall_info);
Jesse Grossccb13522011-10-25 19:26:31 -0700298 else
Thomas Graf8055a892013-12-13 15:22:20 +0100299 err = queue_gso_packets(dp, skb, upcall_info);
Jesse Grossccb13522011-10-25 19:26:31 -0700300 if (err)
301 goto err;
302
303 return 0;
304
305err:
Shan Wei404f2f12012-11-13 09:52:25 +0800306 stats = this_cpu_ptr(dp->stats_percpu);
Jesse Grossccb13522011-10-25 19:26:31 -0700307
WANG Congdf9d9fd2014-02-14 15:10:46 -0800308 u64_stats_update_begin(&stats->syncp);
Jesse Grossccb13522011-10-25 19:26:31 -0700309 stats->n_lost++;
WANG Congdf9d9fd2014-02-14 15:10:46 -0800310 u64_stats_update_end(&stats->syncp);
Jesse Grossccb13522011-10-25 19:26:31 -0700311
312 return err;
313}
314
Thomas Graf8055a892013-12-13 15:22:20 +0100315static int queue_gso_packets(struct datapath *dp, struct sk_buff *skb,
Jesse Grossccb13522011-10-25 19:26:31 -0700316 const struct dp_upcall_info *upcall_info)
317{
Ben Pfaffa1b5d0d2012-07-20 14:47:54 -0700318 unsigned short gso_type = skb_shinfo(skb)->gso_type;
Jesse Grossccb13522011-10-25 19:26:31 -0700319 struct dp_upcall_info later_info;
320 struct sw_flow_key later_key;
321 struct sk_buff *segs, *nskb;
322 int err;
323
Thomas Graf09c5e602013-12-13 15:22:22 +0100324 segs = __skb_gso_segment(skb, NETIF_F_SG, false);
Pravin B Shelar92e5dfc2012-07-20 14:46:29 -0700325 if (IS_ERR(segs))
326 return PTR_ERR(segs);
Jesse Grossccb13522011-10-25 19:26:31 -0700327
328 /* Queue all of the segments. */
329 skb = segs;
330 do {
Thomas Graf8055a892013-12-13 15:22:20 +0100331 err = queue_userspace_packet(dp, skb, upcall_info);
Jesse Grossccb13522011-10-25 19:26:31 -0700332 if (err)
333 break;
334
Ben Pfaffa1b5d0d2012-07-20 14:47:54 -0700335 if (skb == segs && gso_type & SKB_GSO_UDP) {
Jesse Grossccb13522011-10-25 19:26:31 -0700336 /* The initial flow key extracted by ovs_flow_extract()
337 * in this case is for a first fragment, so we need to
338 * properly mark later fragments.
339 */
340 later_key = *upcall_info->key;
341 later_key.ip.frag = OVS_FRAG_TYPE_LATER;
342
343 later_info = *upcall_info;
344 later_info.key = &later_key;
345 upcall_info = &later_info;
346 }
347 } while ((skb = skb->next));
348
349 /* Free all of the segments. */
350 skb = segs;
351 do {
352 nskb = skb->next;
353 if (err)
354 kfree_skb(skb);
355 else
356 consume_skb(skb);
357 } while ((skb = nskb));
358 return err;
359}
360
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100361static size_t key_attr_size(void)
362{
363 return nla_total_size(4) /* OVS_KEY_ATTR_PRIORITY */
Pravin B Shelar7d5437c2013-06-17 17:50:18 -0700364 + nla_total_size(0) /* OVS_KEY_ATTR_TUNNEL */
365 + nla_total_size(8) /* OVS_TUNNEL_KEY_ATTR_ID */
366 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_SRC */
367 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_DST */
368 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TOS */
369 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TTL */
370 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT */
371 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_CSUM */
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100372 + nla_total_size(4) /* OVS_KEY_ATTR_IN_PORT */
373 + nla_total_size(4) /* OVS_KEY_ATTR_SKB_MARK */
374 + nla_total_size(12) /* OVS_KEY_ATTR_ETHERNET */
375 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
376 + nla_total_size(4) /* OVS_KEY_ATTR_8021Q */
377 + nla_total_size(0) /* OVS_KEY_ATTR_ENCAP */
378 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
379 + nla_total_size(40) /* OVS_KEY_ATTR_IPV6 */
380 + nla_total_size(2) /* OVS_KEY_ATTR_ICMPV6 */
381 + nla_total_size(28); /* OVS_KEY_ATTR_ND */
382}
383
Thomas Grafbda56f12013-12-13 15:22:21 +0100384static size_t upcall_msg_size(const struct nlattr *userdata,
385 unsigned int hdrlen)
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100386{
387 size_t size = NLMSG_ALIGN(sizeof(struct ovs_header))
Thomas Grafbda56f12013-12-13 15:22:21 +0100388 + nla_total_size(hdrlen) /* OVS_PACKET_ATTR_PACKET */
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100389 + nla_total_size(key_attr_size()); /* OVS_PACKET_ATTR_KEY */
390
391 /* OVS_PACKET_ATTR_USERDATA */
392 if (userdata)
393 size += NLA_ALIGN(userdata->nla_len);
394
395 return size;
396}
397
Thomas Graf8055a892013-12-13 15:22:20 +0100398static int queue_userspace_packet(struct datapath *dp, struct sk_buff *skb,
Jesse Grossccb13522011-10-25 19:26:31 -0700399 const struct dp_upcall_info *upcall_info)
400{
401 struct ovs_header *upcall;
402 struct sk_buff *nskb = NULL;
403 struct sk_buff *user_skb; /* to be queued to userspace */
404 struct nlattr *nla;
Thomas Graf795449d2013-11-30 13:21:32 +0100405 struct genl_info info = {
Thomas Graf8055a892013-12-13 15:22:20 +0100406 .dst_sk = ovs_dp_get_net(dp)->genl_sock,
Thomas Graf795449d2013-11-30 13:21:32 +0100407 .snd_portid = upcall_info->portid,
408 };
409 size_t len;
Thomas Grafbda56f12013-12-13 15:22:21 +0100410 unsigned int hlen;
Thomas Graf8055a892013-12-13 15:22:20 +0100411 int err, dp_ifindex;
412
413 dp_ifindex = get_dpifindex(dp);
414 if (!dp_ifindex)
415 return -ENODEV;
Jesse Grossccb13522011-10-25 19:26:31 -0700416
417 if (vlan_tx_tag_present(skb)) {
418 nskb = skb_clone(skb, GFP_ATOMIC);
419 if (!nskb)
420 return -ENOMEM;
421
Patrick McHardy86a9bad2013-04-19 02:04:30 +0000422 nskb = __vlan_put_tag(nskb, nskb->vlan_proto, vlan_tx_tag_get(nskb));
Dan Carpenter8aa51d62012-05-13 08:44:18 +0000423 if (!nskb)
Jesse Grossccb13522011-10-25 19:26:31 -0700424 return -ENOMEM;
425
426 nskb->vlan_tci = 0;
427 skb = nskb;
428 }
429
430 if (nla_attr_size(skb->len) > USHRT_MAX) {
431 err = -EFBIG;
432 goto out;
433 }
434
Thomas Grafbda56f12013-12-13 15:22:21 +0100435 /* Complete checksum if needed */
436 if (skb->ip_summed == CHECKSUM_PARTIAL &&
437 (err = skb_checksum_help(skb)))
438 goto out;
439
440 /* Older versions of OVS user space enforce alignment of the last
441 * Netlink attribute to NLA_ALIGNTO which would require extensive
442 * padding logic. Only perform zerocopy if padding is not required.
443 */
444 if (dp->user_features & OVS_DP_F_UNALIGNED)
445 hlen = skb_zerocopy_headlen(skb);
446 else
447 hlen = skb->len;
448
449 len = upcall_msg_size(upcall_info->userdata, hlen);
Thomas Graf795449d2013-11-30 13:21:32 +0100450 user_skb = genlmsg_new_unicast(len, &info, GFP_ATOMIC);
Jesse Grossccb13522011-10-25 19:26:31 -0700451 if (!user_skb) {
452 err = -ENOMEM;
453 goto out;
454 }
455
456 upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
457 0, upcall_info->cmd);
458 upcall->dp_ifindex = dp_ifindex;
459
460 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
Pravin B Shelare6445712013-10-03 18:16:47 -0700461 ovs_nla_put_flow(upcall_info->key, upcall_info->key, user_skb);
Jesse Grossccb13522011-10-25 19:26:31 -0700462 nla_nest_end(user_skb, nla);
463
464 if (upcall_info->userdata)
Ben Pfaff44901082013-02-15 17:29:22 -0800465 __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA,
466 nla_len(upcall_info->userdata),
467 nla_data(upcall_info->userdata));
Jesse Grossccb13522011-10-25 19:26:31 -0700468
Thomas Grafbda56f12013-12-13 15:22:21 +0100469 /* Only reserve room for attribute header, packet data is added
470 * in skb_zerocopy() */
471 if (!(nla = nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, 0))) {
472 err = -ENOBUFS;
473 goto out;
474 }
475 nla->nla_len = nla_attr_size(skb->len);
Jesse Grossccb13522011-10-25 19:26:31 -0700476
Zoltan Kiss36d5fe62014-03-26 22:37:45 +0000477 err = skb_zerocopy(user_skb, skb, skb->len, hlen);
478 if (err)
479 goto out;
Jesse Grossccb13522011-10-25 19:26:31 -0700480
Thomas Grafaea0bb42014-01-14 16:27:49 +0000481 /* Pad OVS_PACKET_ATTR_PACKET if linear copy was performed */
482 if (!(dp->user_features & OVS_DP_F_UNALIGNED)) {
483 size_t plen = NLA_ALIGN(user_skb->len) - user_skb->len;
484
485 if (plen > 0)
486 memset(skb_put(user_skb, plen), 0, plen);
487 }
488
Thomas Grafbda56f12013-12-13 15:22:21 +0100489 ((struct nlmsghdr *) user_skb->data)->nlmsg_len = user_skb->len;
490
Thomas Graf8055a892013-12-13 15:22:20 +0100491 err = genlmsg_unicast(ovs_dp_get_net(dp), user_skb, upcall_info->portid);
Jesse Grossccb13522011-10-25 19:26:31 -0700492out:
Zoltan Kiss36d5fe62014-03-26 22:37:45 +0000493 if (err)
494 skb_tx_error(skb);
Jesse Grossccb13522011-10-25 19:26:31 -0700495 kfree_skb(nskb);
496 return err;
497}
498
Jesse Grossccb13522011-10-25 19:26:31 -0700499static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
500{
501 struct ovs_header *ovs_header = info->userhdr;
502 struct nlattr **a = info->attrs;
503 struct sw_flow_actions *acts;
504 struct sk_buff *packet;
505 struct sw_flow *flow;
506 struct datapath *dp;
507 struct ethhdr *eth;
508 int len;
509 int err;
Jesse Grossccb13522011-10-25 19:26:31 -0700510
511 err = -EINVAL;
512 if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
Thomas Grafdded45fc2013-03-29 14:46:47 +0100513 !a[OVS_PACKET_ATTR_ACTIONS])
Jesse Grossccb13522011-10-25 19:26:31 -0700514 goto err;
515
516 len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
517 packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
518 err = -ENOMEM;
519 if (!packet)
520 goto err;
521 skb_reserve(packet, NET_IP_ALIGN);
522
Thomas Graf32686a92013-03-29 14:46:48 +0100523 nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
Jesse Grossccb13522011-10-25 19:26:31 -0700524
525 skb_reset_mac_header(packet);
526 eth = eth_hdr(packet);
527
528 /* Normally, setting the skb 'protocol' field would be handled by a
529 * call to eth_type_trans(), but it assumes there's a sending
530 * device, which we may not have. */
Simon Hormane5c5d222013-03-28 13:38:25 +0900531 if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN)
Jesse Grossccb13522011-10-25 19:26:31 -0700532 packet->protocol = eth->h_proto;
533 else
534 packet->protocol = htons(ETH_P_802_2);
535
536 /* Build an sw_flow for sending this packet. */
Jarno Rajahalme23dabf82014-03-27 12:35:23 -0700537 flow = ovs_flow_alloc();
Jesse Grossccb13522011-10-25 19:26:31 -0700538 err = PTR_ERR(flow);
539 if (IS_ERR(flow))
540 goto err_kfree_skb;
541
Andy Zhou03f0d912013-08-07 20:01:00 -0700542 err = ovs_flow_extract(packet, -1, &flow->key);
Jesse Grossccb13522011-10-25 19:26:31 -0700543 if (err)
544 goto err_flow_free;
545
Pravin B Shelare6445712013-10-03 18:16:47 -0700546 err = ovs_nla_get_flow_metadata(flow, a[OVS_PACKET_ATTR_KEY]);
Jesse Grossccb13522011-10-25 19:26:31 -0700547 if (err)
548 goto err_flow_free;
Pravin B Shelare6445712013-10-03 18:16:47 -0700549 acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_PACKET_ATTR_ACTIONS]));
Jesse Grossccb13522011-10-25 19:26:31 -0700550 err = PTR_ERR(acts);
551 if (IS_ERR(acts))
552 goto err_flow_free;
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700553
Pravin B Shelare6445712013-10-03 18:16:47 -0700554 err = ovs_nla_copy_actions(a[OVS_PACKET_ATTR_ACTIONS],
555 &flow->key, 0, &acts);
Jesse Grossccb13522011-10-25 19:26:31 -0700556 rcu_assign_pointer(flow->sf_acts, acts);
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700557 if (err)
558 goto err_flow_free;
Jesse Grossccb13522011-10-25 19:26:31 -0700559
560 OVS_CB(packet)->flow = flow;
Andy Zhou03f0d912013-08-07 20:01:00 -0700561 OVS_CB(packet)->pkt_key = &flow->key;
Jesse Grossccb13522011-10-25 19:26:31 -0700562 packet->priority = flow->key.phy.priority;
Ansis Atteka39c7caeb2012-11-26 11:24:11 -0800563 packet->mark = flow->key.phy.skb_mark;
Jesse Grossccb13522011-10-25 19:26:31 -0700564
565 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800566 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -0700567 err = -ENODEV;
568 if (!dp)
569 goto err_unlock;
570
571 local_bh_disable();
572 err = ovs_execute_actions(dp, packet);
573 local_bh_enable();
574 rcu_read_unlock();
575
Andy Zhou03f0d912013-08-07 20:01:00 -0700576 ovs_flow_free(flow, false);
Jesse Grossccb13522011-10-25 19:26:31 -0700577 return err;
578
579err_unlock:
580 rcu_read_unlock();
581err_flow_free:
Andy Zhou03f0d912013-08-07 20:01:00 -0700582 ovs_flow_free(flow, false);
Jesse Grossccb13522011-10-25 19:26:31 -0700583err_kfree_skb:
584 kfree_skb(packet);
585err:
586 return err;
587}
588
589static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
Thomas Grafdded45fc2013-03-29 14:46:47 +0100590 [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
Jesse Grossccb13522011-10-25 19:26:31 -0700591 [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
592 [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
593};
594
Johannes Berg4534de82013-11-14 17:14:46 +0100595static const struct genl_ops dp_packet_genl_ops[] = {
Jesse Grossccb13522011-10-25 19:26:31 -0700596 { .cmd = OVS_PACKET_CMD_EXECUTE,
597 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
598 .policy = packet_policy,
599 .doit = ovs_packet_cmd_execute
600 }
601};
602
Andy Zhou1bd71162013-10-22 10:42:46 -0700603static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats,
604 struct ovs_dp_megaflow_stats *mega_stats)
Jesse Grossccb13522011-10-25 19:26:31 -0700605{
606 int i;
Jesse Grossccb13522011-10-25 19:26:31 -0700607
Andy Zhou1bd71162013-10-22 10:42:46 -0700608 memset(mega_stats, 0, sizeof(*mega_stats));
609
Pravin B Shelarb637e492013-10-04 00:14:23 -0700610 stats->n_flows = ovs_flow_tbl_count(&dp->table);
Andy Zhou1bd71162013-10-22 10:42:46 -0700611 mega_stats->n_masks = ovs_flow_tbl_num_masks(&dp->table);
Jesse Grossccb13522011-10-25 19:26:31 -0700612
613 stats->n_hit = stats->n_missed = stats->n_lost = 0;
Andy Zhou1bd71162013-10-22 10:42:46 -0700614
Jesse Grossccb13522011-10-25 19:26:31 -0700615 for_each_possible_cpu(i) {
616 const struct dp_stats_percpu *percpu_stats;
617 struct dp_stats_percpu local_stats;
618 unsigned int start;
619
620 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
621
622 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700623 start = u64_stats_fetch_begin_irq(&percpu_stats->syncp);
Jesse Grossccb13522011-10-25 19:26:31 -0700624 local_stats = *percpu_stats;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700625 } while (u64_stats_fetch_retry_irq(&percpu_stats->syncp, start));
Jesse Grossccb13522011-10-25 19:26:31 -0700626
627 stats->n_hit += local_stats.n_hit;
628 stats->n_missed += local_stats.n_missed;
629 stats->n_lost += local_stats.n_lost;
Andy Zhou1bd71162013-10-22 10:42:46 -0700630 mega_stats->n_mask_hit += local_stats.n_mask_hit;
Jesse Grossccb13522011-10-25 19:26:31 -0700631 }
632}
633
634static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
635 [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
636 [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
637 [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
638};
639
640static struct genl_family dp_flow_genl_family = {
641 .id = GENL_ID_GENERATE,
642 .hdrsize = sizeof(struct ovs_header),
643 .name = OVS_FLOW_FAMILY,
644 .version = OVS_FLOW_VERSION,
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800645 .maxattr = OVS_FLOW_ATTR_MAX,
Pravin B Shelar3a4e0d62013-04-23 07:48:48 +0000646 .netnsok = true,
647 .parallel_ops = true,
Jesse Grossccb13522011-10-25 19:26:31 -0700648};
649
650static struct genl_multicast_group ovs_dp_flow_multicast_group = {
651 .name = OVS_FLOW_MCGROUP
652};
653
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100654static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts)
655{
656 return NLMSG_ALIGN(sizeof(struct ovs_header))
657 + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_KEY */
Andy Zhou03f0d912013-08-07 20:01:00 -0700658 + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_MASK */
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100659 + nla_total_size(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
660 + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
661 + nla_total_size(8) /* OVS_FLOW_ATTR_USED */
662 + nla_total_size(acts->actions_len); /* OVS_FLOW_ATTR_ACTIONS */
663}
664
Jarno Rajahalmebb6f9a72014-05-05 11:32:17 -0700665/* Called with ovs_mutex or RCU read lock. */
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700666static int ovs_flow_cmd_fill_info(const struct sw_flow *flow, int dp_ifindex,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000667 struct sk_buff *skb, u32 portid,
Jesse Grossccb13522011-10-25 19:26:31 -0700668 u32 seq, u32 flags, u8 cmd)
669{
670 const int skb_orig_len = skb->len;
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700671 struct nlattr *start;
Jesse Grossccb13522011-10-25 19:26:31 -0700672 struct ovs_flow_stats stats;
Pravin B Shelare298e502013-10-29 17:22:21 -0700673 __be16 tcp_flags;
674 unsigned long used;
Jesse Grossccb13522011-10-25 19:26:31 -0700675 struct ovs_header *ovs_header;
676 struct nlattr *nla;
Jesse Grossccb13522011-10-25 19:26:31 -0700677 int err;
678
Eric W. Biederman15e47302012-09-07 20:12:54 +0000679 ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family, flags, cmd);
Jesse Grossccb13522011-10-25 19:26:31 -0700680 if (!ovs_header)
681 return -EMSGSIZE;
682
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700683 ovs_header->dp_ifindex = dp_ifindex;
Jesse Grossccb13522011-10-25 19:26:31 -0700684
Andy Zhou03f0d912013-08-07 20:01:00 -0700685 /* Fill flow key. */
Jesse Grossccb13522011-10-25 19:26:31 -0700686 nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
687 if (!nla)
688 goto nla_put_failure;
Andy Zhou03f0d912013-08-07 20:01:00 -0700689
Pravin B Shelare6445712013-10-03 18:16:47 -0700690 err = ovs_nla_put_flow(&flow->unmasked_key, &flow->unmasked_key, skb);
Jesse Grossccb13522011-10-25 19:26:31 -0700691 if (err)
692 goto error;
693 nla_nest_end(skb, nla);
694
Andy Zhou03f0d912013-08-07 20:01:00 -0700695 nla = nla_nest_start(skb, OVS_FLOW_ATTR_MASK);
696 if (!nla)
697 goto nla_put_failure;
698
Pravin B Shelare6445712013-10-03 18:16:47 -0700699 err = ovs_nla_put_flow(&flow->key, &flow->mask->key, skb);
Andy Zhou03f0d912013-08-07 20:01:00 -0700700 if (err)
701 goto error;
702
703 nla_nest_end(skb, nla);
704
Pravin B Shelare298e502013-10-29 17:22:21 -0700705 ovs_flow_stats_get(flow, &stats, &used, &tcp_flags);
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700706
David S. Miller028d6a62012-03-29 23:20:48 -0400707 if (used &&
708 nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
709 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -0700710
David S. Miller028d6a62012-03-29 23:20:48 -0400711 if (stats.n_packets &&
Pravin B Shelare298e502013-10-29 17:22:21 -0700712 nla_put(skb, OVS_FLOW_ATTR_STATS, sizeof(struct ovs_flow_stats), &stats))
David S. Miller028d6a62012-03-29 23:20:48 -0400713 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -0700714
Pravin B Shelare298e502013-10-29 17:22:21 -0700715 if ((u8)ntohs(tcp_flags) &&
716 nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, (u8)ntohs(tcp_flags)))
David S. Miller028d6a62012-03-29 23:20:48 -0400717 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -0700718
719 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
720 * this is the first flow to be dumped into 'skb'. This is unusual for
721 * Netlink but individual action lists can be longer than
722 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
723 * The userspace caller can always fetch the actions separately if it
724 * really wants them. (Most userspace callers in fact don't care.)
725 *
726 * This can only fail for dump operations because the skb is always
727 * properly sized for single flows.
728 */
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700729 start = nla_nest_start(skb, OVS_FLOW_ATTR_ACTIONS);
730 if (start) {
Pravin B Shelard57170b2013-07-30 15:39:39 -0700731 const struct sw_flow_actions *sf_acts;
732
Jesse Gross663efa32013-12-03 10:58:53 -0800733 sf_acts = rcu_dereference_ovsl(flow->sf_acts);
Pravin B Shelare6445712013-10-03 18:16:47 -0700734 err = ovs_nla_put_actions(sf_acts->actions,
735 sf_acts->actions_len, skb);
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700736
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700737 if (!err)
738 nla_nest_end(skb, start);
739 else {
740 if (skb_orig_len)
741 goto error;
742
743 nla_nest_cancel(skb, start);
744 }
745 } else if (skb_orig_len)
746 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -0700747
748 return genlmsg_end(skb, ovs_header);
749
750nla_put_failure:
751 err = -EMSGSIZE;
752error:
753 genlmsg_cancel(skb, ovs_header);
754 return err;
755}
756
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700757/* May not be called with RCU read lock. */
758static struct sk_buff *ovs_flow_cmd_alloc_info(const struct sw_flow_actions *acts,
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700759 struct genl_info *info,
760 bool always)
Jesse Grossccb13522011-10-25 19:26:31 -0700761{
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700762 struct sk_buff *skb;
Jesse Grossccb13522011-10-25 19:26:31 -0700763
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700764 if (!always && !ovs_must_notify(info, &ovs_dp_flow_multicast_group))
765 return NULL;
766
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700767 skb = genlmsg_new_unicast(ovs_flow_cmd_msg_size(acts), info, GFP_KERNEL);
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700768 if (!skb)
769 return ERR_PTR(-ENOMEM);
770
771 return skb;
Jesse Grossccb13522011-10-25 19:26:31 -0700772}
773
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700774/* Called with ovs_mutex. */
775static struct sk_buff *ovs_flow_cmd_build_info(const struct sw_flow *flow,
776 int dp_ifindex,
777 struct genl_info *info, u8 cmd,
778 bool always)
Jesse Grossccb13522011-10-25 19:26:31 -0700779{
780 struct sk_buff *skb;
781 int retval;
782
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700783 skb = ovs_flow_cmd_alloc_info(ovsl_dereference(flow->sf_acts), info,
784 always);
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700785 if (!skb || IS_ERR(skb))
786 return skb;
Jesse Grossccb13522011-10-25 19:26:31 -0700787
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700788 retval = ovs_flow_cmd_fill_info(flow, dp_ifindex, skb,
789 info->snd_portid, info->snd_seq, 0,
790 cmd);
Jesse Grossccb13522011-10-25 19:26:31 -0700791 BUG_ON(retval < 0);
792 return skb;
793}
794
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700795static int ovs_flow_cmd_new(struct sk_buff *skb, struct genl_info *info)
Jesse Grossccb13522011-10-25 19:26:31 -0700796{
797 struct nlattr **a = info->attrs;
798 struct ovs_header *ovs_header = info->userhdr;
Andy Zhou03f0d912013-08-07 20:01:00 -0700799 struct sw_flow_key key, masked_key;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700800 struct sw_flow *flow;
Andy Zhou03f0d912013-08-07 20:01:00 -0700801 struct sw_flow_mask mask;
Jesse Grossccb13522011-10-25 19:26:31 -0700802 struct sk_buff *reply;
803 struct datapath *dp;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700804 struct sw_flow_actions *acts;
805 struct sw_flow_match match;
806 int error;
807
808 /* Extract key. */
809 error = -EINVAL;
810 if (!a[OVS_FLOW_ATTR_KEY])
811 goto error;
812
813 ovs_match_init(&match, &key, &mask);
814 error = ovs_nla_get_match(&match,
815 a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
816 if (error)
817 goto error;
818
819 /* Validate actions. */
820 error = -EINVAL;
821 if (!a[OVS_FLOW_ATTR_ACTIONS])
822 goto error;
823
824 acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_FLOW_ATTR_ACTIONS]));
825 error = PTR_ERR(acts);
826 if (IS_ERR(acts))
827 goto error;
828
829 ovs_flow_mask_key(&masked_key, &key, &mask);
830 error = ovs_nla_copy_actions(a[OVS_FLOW_ATTR_ACTIONS],
831 &masked_key, 0, &acts);
832 if (error) {
833 OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
834 goto err_kfree;
835 }
836
837 ovs_lock();
838 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
839 error = -ENODEV;
840 if (!dp)
841 goto err_unlock_ovs;
842
843 /* Check if this is a duplicate flow */
844 flow = ovs_flow_tbl_lookup(&dp->table, &key);
845 if (!flow) {
846 /* Allocate flow. */
847 flow = ovs_flow_alloc();
848 if (IS_ERR(flow)) {
849 error = PTR_ERR(flow);
850 goto err_unlock_ovs;
851 }
852
853 flow->key = masked_key;
854 flow->unmasked_key = key;
855 rcu_assign_pointer(flow->sf_acts, acts);
856
857 /* Put flow in bucket. */
858 error = ovs_flow_tbl_insert(&dp->table, flow, &mask);
859 if (error) {
860 acts = NULL;
861 goto err_flow_free;
862 }
863 } else {
864 struct sw_flow_actions *old_acts;
865
866 /* Bail out if we're not allowed to modify an existing flow.
867 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
868 * because Generic Netlink treats the latter as a dump
869 * request. We also accept NLM_F_EXCL in case that bug ever
870 * gets fixed.
871 */
872 error = -EEXIST;
873 if (info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL))
874 goto err_unlock_ovs;
875
876 /* The unmasked key has to be the same for flow updates. */
877 if (!ovs_flow_cmp_unmasked_key(flow, &match))
878 goto err_unlock_ovs;
879
880 /* Update actions. */
881 old_acts = ovsl_dereference(flow->sf_acts);
882 rcu_assign_pointer(flow->sf_acts, acts);
883 ovs_nla_free_flow_actions(old_acts);
884 }
885
886 reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex,
887 info, OVS_FLOW_CMD_NEW, false);
888 ovs_unlock();
889
890 if (reply) {
891 if (!IS_ERR(reply))
892 ovs_notify(&dp_flow_genl_family, reply, info);
893 else
894 netlink_set_err(sock_net(skb->sk)->genl_sock, 0, 0,
895 PTR_ERR(reply));
896 }
897 return 0;
898
899err_flow_free:
900 ovs_flow_free(flow, false);
901err_unlock_ovs:
902 ovs_unlock();
903err_kfree:
904 kfree(acts);
905error:
906 return error;
907}
908
909static int ovs_flow_cmd_set(struct sk_buff *skb, struct genl_info *info)
910{
911 struct nlattr **a = info->attrs;
912 struct ovs_header *ovs_header = info->userhdr;
913 struct sw_flow_key key, masked_key;
914 struct sw_flow *flow;
915 struct sw_flow_mask mask;
916 struct sk_buff *reply = NULL;
917 struct datapath *dp;
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700918 struct sw_flow_actions *acts = NULL;
Andy Zhou03f0d912013-08-07 20:01:00 -0700919 struct sw_flow_match match;
Jesse Grossccb13522011-10-25 19:26:31 -0700920 int error;
Jesse Grossccb13522011-10-25 19:26:31 -0700921
922 /* Extract key. */
923 error = -EINVAL;
924 if (!a[OVS_FLOW_ATTR_KEY])
925 goto error;
Andy Zhou03f0d912013-08-07 20:01:00 -0700926
927 ovs_match_init(&match, &key, &mask);
Jarno Rajahalme23dabf82014-03-27 12:35:23 -0700928 error = ovs_nla_get_match(&match,
Pravin B Shelare6445712013-10-03 18:16:47 -0700929 a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
Jesse Grossccb13522011-10-25 19:26:31 -0700930 if (error)
931 goto error;
932
933 /* Validate actions. */
934 if (a[OVS_FLOW_ATTR_ACTIONS]) {
Pravin B Shelare6445712013-10-03 18:16:47 -0700935 acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_FLOW_ATTR_ACTIONS]));
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700936 error = PTR_ERR(acts);
937 if (IS_ERR(acts))
Jesse Grossccb13522011-10-25 19:26:31 -0700938 goto error;
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700939
Pravin B Shelare6445712013-10-03 18:16:47 -0700940 ovs_flow_mask_key(&masked_key, &key, &mask);
941 error = ovs_nla_copy_actions(a[OVS_FLOW_ATTR_ACTIONS],
942 &masked_key, 0, &acts);
Andy Zhou03f0d912013-08-07 20:01:00 -0700943 if (error) {
944 OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700945 goto err_kfree;
Andy Zhou03f0d912013-08-07 20:01:00 -0700946 }
Jesse Grossccb13522011-10-25 19:26:31 -0700947 }
948
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700949 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800950 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -0700951 error = -ENODEV;
952 if (!dp)
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700953 goto err_unlock_ovs;
Jesse Grossccb13522011-10-25 19:26:31 -0700954
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700955 /* Check that the flow exists. */
Andy Zhou5bb50632013-11-25 10:42:46 -0800956 flow = ovs_flow_tbl_lookup(&dp->table, &key);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700957 error = -ENOENT;
958 if (!flow)
959 goto err_unlock_ovs;
Jesse Grossccb13522011-10-25 19:26:31 -0700960
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700961 /* The unmasked key has to be the same for flow updates. */
962 error = -EEXIST;
963 if (!ovs_flow_cmp_unmasked_key(flow, &match))
964 goto err_unlock_ovs;
Jesse Grossccb13522011-10-25 19:26:31 -0700965
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700966 /* Update actions, if present. */
967 if (acts) {
968 struct sw_flow_actions *old_acts;
969
970 old_acts = ovsl_dereference(flow->sf_acts);
Jesse Grossccb13522011-10-25 19:26:31 -0700971 rcu_assign_pointer(flow->sf_acts, acts);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700972 ovs_nla_free_flow_actions(old_acts);
Jesse Grossccb13522011-10-25 19:26:31 -0700973 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700974
975 reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex,
976 info, OVS_FLOW_CMD_NEW, false);
977 /* Clear stats. */
978 if (a[OVS_FLOW_ATTR_CLEAR])
979 ovs_flow_stats_clear(flow);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700980 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -0700981
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700982 if (reply) {
983 if (!IS_ERR(reply))
984 ovs_notify(&dp_flow_genl_family, reply, info);
985 else
986 genl_set_err(&dp_flow_genl_family, sock_net(skb->sk), 0,
987 0, PTR_ERR(reply));
988 }
989
Jesse Grossccb13522011-10-25 19:26:31 -0700990 return 0;
991
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700992err_unlock_ovs:
993 ovs_unlock();
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700994err_kfree:
995 kfree(acts);
Jesse Grossccb13522011-10-25 19:26:31 -0700996error:
997 return error;
998}
999
1000static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1001{
1002 struct nlattr **a = info->attrs;
1003 struct ovs_header *ovs_header = info->userhdr;
1004 struct sw_flow_key key;
1005 struct sk_buff *reply;
1006 struct sw_flow *flow;
1007 struct datapath *dp;
Andy Zhou03f0d912013-08-07 20:01:00 -07001008 struct sw_flow_match match;
Jesse Grossccb13522011-10-25 19:26:31 -07001009 int err;
Jesse Grossccb13522011-10-25 19:26:31 -07001010
Andy Zhou03f0d912013-08-07 20:01:00 -07001011 if (!a[OVS_FLOW_ATTR_KEY]) {
1012 OVS_NLERR("Flow get message rejected, Key attribute missing.\n");
Jesse Grossccb13522011-10-25 19:26:31 -07001013 return -EINVAL;
Andy Zhou03f0d912013-08-07 20:01:00 -07001014 }
1015
1016 ovs_match_init(&match, &key, NULL);
Jarno Rajahalme23dabf82014-03-27 12:35:23 -07001017 err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL);
Jesse Grossccb13522011-10-25 19:26:31 -07001018 if (err)
1019 return err;
1020
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001021 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001022 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001023 if (!dp) {
1024 err = -ENODEV;
1025 goto unlock;
1026 }
Jesse Grossccb13522011-10-25 19:26:31 -07001027
Andy Zhou5bb50632013-11-25 10:42:46 -08001028 flow = ovs_flow_tbl_lookup(&dp->table, &key);
Pravin B Shelare6445712013-10-03 18:16:47 -07001029 if (!flow || !ovs_flow_cmp_unmasked_key(flow, &match)) {
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001030 err = -ENOENT;
1031 goto unlock;
1032 }
Jesse Grossccb13522011-10-25 19:26:31 -07001033
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -07001034 reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex, info,
1035 OVS_FLOW_CMD_NEW, true);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001036 if (IS_ERR(reply)) {
1037 err = PTR_ERR(reply);
1038 goto unlock;
1039 }
Jesse Grossccb13522011-10-25 19:26:31 -07001040
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001041 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001042 return genlmsg_reply(reply, info);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001043unlock:
1044 ovs_unlock();
1045 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001046}
1047
1048static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1049{
1050 struct nlattr **a = info->attrs;
1051 struct ovs_header *ovs_header = info->userhdr;
1052 struct sw_flow_key key;
1053 struct sk_buff *reply;
1054 struct sw_flow *flow;
1055 struct datapath *dp;
Andy Zhou03f0d912013-08-07 20:01:00 -07001056 struct sw_flow_match match;
Jesse Grossccb13522011-10-25 19:26:31 -07001057 int err;
Jesse Grossccb13522011-10-25 19:26:31 -07001058
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001059 if (likely(a[OVS_FLOW_ATTR_KEY])) {
1060 ovs_match_init(&match, &key, NULL);
1061 err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL);
1062 if (unlikely(err))
1063 return err;
1064 }
1065
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001066 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001067 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001068 if (unlikely(!dp)) {
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001069 err = -ENODEV;
1070 goto unlock;
1071 }
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001072
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001073 if (unlikely(!a[OVS_FLOW_ATTR_KEY])) {
Pravin B Shelarb637e492013-10-04 00:14:23 -07001074 err = ovs_flow_tbl_flush(&dp->table);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001075 goto unlock;
1076 }
Andy Zhou03f0d912013-08-07 20:01:00 -07001077
Andy Zhou5bb50632013-11-25 10:42:46 -08001078 flow = ovs_flow_tbl_lookup(&dp->table, &key);
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001079 if (unlikely(!flow || !ovs_flow_cmp_unmasked_key(flow, &match))) {
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001080 err = -ENOENT;
1081 goto unlock;
1082 }
Jesse Grossccb13522011-10-25 19:26:31 -07001083
Pravin B Shelarb637e492013-10-04 00:14:23 -07001084 ovs_flow_tbl_remove(&dp->table, flow);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001085 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001086
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001087 reply = ovs_flow_cmd_alloc_info((const struct sw_flow_actions __force *) flow->sf_acts,
1088 info, false);
1089 if (likely(reply)) {
1090 if (likely(!IS_ERR(reply))) {
1091 rcu_read_lock(); /*To keep RCU checker happy. */
1092 err = ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex,
1093 reply, info->snd_portid,
1094 info->snd_seq, 0,
1095 OVS_FLOW_CMD_DEL);
1096 rcu_read_unlock();
1097 BUG_ON(err < 0);
1098
1099 ovs_notify(&dp_flow_genl_family, reply, info);
1100 } else {
1101 netlink_set_err(sock_net(skb->sk)->genl_sock, 0, 0, PTR_ERR(reply));
1102 }
1103 }
1104
1105 ovs_flow_free(flow, true);
Jesse Grossccb13522011-10-25 19:26:31 -07001106 return 0;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001107unlock:
1108 ovs_unlock();
1109 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001110}
1111
1112static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1113{
1114 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
Pravin B Shelarb637e492013-10-04 00:14:23 -07001115 struct table_instance *ti;
Jesse Grossccb13522011-10-25 19:26:31 -07001116 struct datapath *dp;
Jesse Grossccb13522011-10-25 19:26:31 -07001117
Pravin B Shelard57170b2013-07-30 15:39:39 -07001118 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001119 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001120 if (!dp) {
Pravin B Shelard57170b2013-07-30 15:39:39 -07001121 rcu_read_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001122 return -ENODEV;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001123 }
Jesse Grossccb13522011-10-25 19:26:31 -07001124
Pravin B Shelarb637e492013-10-04 00:14:23 -07001125 ti = rcu_dereference(dp->table.ti);
Jesse Grossccb13522011-10-25 19:26:31 -07001126 for (;;) {
1127 struct sw_flow *flow;
1128 u32 bucket, obj;
1129
1130 bucket = cb->args[0];
1131 obj = cb->args[1];
Pravin B Shelarb637e492013-10-04 00:14:23 -07001132 flow = ovs_flow_tbl_dump_next(ti, &bucket, &obj);
Jesse Grossccb13522011-10-25 19:26:31 -07001133 if (!flow)
1134 break;
1135
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -07001136 if (ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex, skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001137 NETLINK_CB(cb->skb).portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001138 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1139 OVS_FLOW_CMD_NEW) < 0)
1140 break;
1141
1142 cb->args[0] = bucket;
1143 cb->args[1] = obj;
1144 }
Pravin B Shelard57170b2013-07-30 15:39:39 -07001145 rcu_read_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001146 return skb->len;
1147}
1148
Johannes Berg4534de82013-11-14 17:14:46 +01001149static const struct genl_ops dp_flow_genl_ops[] = {
Jesse Grossccb13522011-10-25 19:26:31 -07001150 { .cmd = OVS_FLOW_CMD_NEW,
1151 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1152 .policy = flow_policy,
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001153 .doit = ovs_flow_cmd_new
Jesse Grossccb13522011-10-25 19:26:31 -07001154 },
1155 { .cmd = OVS_FLOW_CMD_DEL,
1156 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1157 .policy = flow_policy,
1158 .doit = ovs_flow_cmd_del
1159 },
1160 { .cmd = OVS_FLOW_CMD_GET,
1161 .flags = 0, /* OK for unprivileged users. */
1162 .policy = flow_policy,
1163 .doit = ovs_flow_cmd_get,
1164 .dumpit = ovs_flow_cmd_dump
1165 },
1166 { .cmd = OVS_FLOW_CMD_SET,
1167 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1168 .policy = flow_policy,
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001169 .doit = ovs_flow_cmd_set,
Jesse Grossccb13522011-10-25 19:26:31 -07001170 },
1171};
1172
1173static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1174 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1175 [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
Thomas Graf43d4be92013-12-13 15:22:18 +01001176 [OVS_DP_ATTR_USER_FEATURES] = { .type = NLA_U32 },
Jesse Grossccb13522011-10-25 19:26:31 -07001177};
1178
1179static struct genl_family dp_datapath_genl_family = {
1180 .id = GENL_ID_GENERATE,
1181 .hdrsize = sizeof(struct ovs_header),
1182 .name = OVS_DATAPATH_FAMILY,
1183 .version = OVS_DATAPATH_VERSION,
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001184 .maxattr = OVS_DP_ATTR_MAX,
Pravin B Shelar3a4e0d62013-04-23 07:48:48 +00001185 .netnsok = true,
1186 .parallel_ops = true,
Jesse Grossccb13522011-10-25 19:26:31 -07001187};
1188
1189static struct genl_multicast_group ovs_dp_datapath_multicast_group = {
1190 .name = OVS_DATAPATH_MCGROUP
1191};
1192
Thomas Grafc3ff8cf2013-03-29 14:46:49 +01001193static size_t ovs_dp_cmd_msg_size(void)
1194{
1195 size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
1196
1197 msgsize += nla_total_size(IFNAMSIZ);
1198 msgsize += nla_total_size(sizeof(struct ovs_dp_stats));
Andy Zhou1bd71162013-10-22 10:42:46 -07001199 msgsize += nla_total_size(sizeof(struct ovs_dp_megaflow_stats));
Daniele Di Proietto45fb9c32014-01-23 10:47:35 -08001200 msgsize += nla_total_size(sizeof(u32)); /* OVS_DP_ATTR_USER_FEATURES */
Thomas Grafc3ff8cf2013-03-29 14:46:49 +01001201
1202 return msgsize;
1203}
1204
Jarno Rajahalmebb6f9a72014-05-05 11:32:17 -07001205/* Called with ovs_mutex or RCU read lock. */
Jesse Grossccb13522011-10-25 19:26:31 -07001206static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001207 u32 portid, u32 seq, u32 flags, u8 cmd)
Jesse Grossccb13522011-10-25 19:26:31 -07001208{
1209 struct ovs_header *ovs_header;
1210 struct ovs_dp_stats dp_stats;
Andy Zhou1bd71162013-10-22 10:42:46 -07001211 struct ovs_dp_megaflow_stats dp_megaflow_stats;
Jesse Grossccb13522011-10-25 19:26:31 -07001212 int err;
1213
Eric W. Biederman15e47302012-09-07 20:12:54 +00001214 ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
Jesse Grossccb13522011-10-25 19:26:31 -07001215 flags, cmd);
1216 if (!ovs_header)
1217 goto error;
1218
1219 ovs_header->dp_ifindex = get_dpifindex(dp);
1220
Jesse Grossccb13522011-10-25 19:26:31 -07001221 err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
Jesse Grossccb13522011-10-25 19:26:31 -07001222 if (err)
1223 goto nla_put_failure;
1224
Andy Zhou1bd71162013-10-22 10:42:46 -07001225 get_dp_stats(dp, &dp_stats, &dp_megaflow_stats);
1226 if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats),
1227 &dp_stats))
1228 goto nla_put_failure;
1229
1230 if (nla_put(skb, OVS_DP_ATTR_MEGAFLOW_STATS,
1231 sizeof(struct ovs_dp_megaflow_stats),
1232 &dp_megaflow_stats))
David S. Miller028d6a62012-03-29 23:20:48 -04001233 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001234
Thomas Graf43d4be92013-12-13 15:22:18 +01001235 if (nla_put_u32(skb, OVS_DP_ATTR_USER_FEATURES, dp->user_features))
1236 goto nla_put_failure;
1237
Jesse Grossccb13522011-10-25 19:26:31 -07001238 return genlmsg_end(skb, ovs_header);
1239
1240nla_put_failure:
1241 genlmsg_cancel(skb, ovs_header);
1242error:
1243 return -EMSGSIZE;
1244}
1245
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001246static struct sk_buff *ovs_dp_cmd_alloc_info(struct genl_info *info)
Jesse Grossccb13522011-10-25 19:26:31 -07001247{
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001248 return genlmsg_new_unicast(ovs_dp_cmd_msg_size(), info, GFP_KERNEL);
Jesse Grossccb13522011-10-25 19:26:31 -07001249}
1250
Jarno Rajahalmebb6f9a72014-05-05 11:32:17 -07001251/* Called with rcu_read_lock or ovs_mutex. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001252static struct datapath *lookup_datapath(struct net *net,
1253 struct ovs_header *ovs_header,
Jesse Grossccb13522011-10-25 19:26:31 -07001254 struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1255{
1256 struct datapath *dp;
1257
1258 if (!a[OVS_DP_ATTR_NAME])
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001259 dp = get_dp(net, ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001260 else {
1261 struct vport *vport;
1262
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001263 vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
Jesse Grossccb13522011-10-25 19:26:31 -07001264 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
Jesse Grossccb13522011-10-25 19:26:31 -07001265 }
1266 return dp ? dp : ERR_PTR(-ENODEV);
1267}
1268
Thomas Graf44da5ae2013-12-13 15:22:19 +01001269static void ovs_dp_reset_user_features(struct sk_buff *skb, struct genl_info *info)
1270{
1271 struct datapath *dp;
1272
1273 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
Jiri Pirko3c7eacf2014-02-14 11:42:36 +01001274 if (IS_ERR(dp))
Thomas Graf44da5ae2013-12-13 15:22:19 +01001275 return;
1276
1277 WARN(dp->user_features, "Dropping previously announced user features\n");
1278 dp->user_features = 0;
1279}
1280
Thomas Graf43d4be92013-12-13 15:22:18 +01001281static void ovs_dp_change(struct datapath *dp, struct nlattr **a)
1282{
1283 if (a[OVS_DP_ATTR_USER_FEATURES])
1284 dp->user_features = nla_get_u32(a[OVS_DP_ATTR_USER_FEATURES]);
1285}
1286
Jesse Grossccb13522011-10-25 19:26:31 -07001287static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1288{
1289 struct nlattr **a = info->attrs;
1290 struct vport_parms parms;
1291 struct sk_buff *reply;
1292 struct datapath *dp;
1293 struct vport *vport;
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001294 struct ovs_net *ovs_net;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001295 int err, i;
Jesse Grossccb13522011-10-25 19:26:31 -07001296
1297 err = -EINVAL;
1298 if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1299 goto err;
1300
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001301 reply = ovs_dp_cmd_alloc_info(info);
1302 if (!reply)
1303 return -ENOMEM;
Jesse Grossccb13522011-10-25 19:26:31 -07001304
1305 err = -ENOMEM;
1306 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1307 if (dp == NULL)
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001308 goto err_free_reply;
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001309
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001310 ovs_dp_set_net(dp, hold_net(sock_net(skb->sk)));
Jesse Grossccb13522011-10-25 19:26:31 -07001311
1312 /* Allocate table. */
Pravin B Shelarb637e492013-10-04 00:14:23 -07001313 err = ovs_flow_tbl_init(&dp->table);
1314 if (err)
Jesse Grossccb13522011-10-25 19:26:31 -07001315 goto err_free_dp;
1316
WANG Cong1c213bd2014-02-13 11:46:28 -08001317 dp->stats_percpu = netdev_alloc_pcpu_stats(struct dp_stats_percpu);
Jesse Grossccb13522011-10-25 19:26:31 -07001318 if (!dp->stats_percpu) {
1319 err = -ENOMEM;
1320 goto err_destroy_table;
1321 }
1322
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001323 dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
Pravin B Shelare6445712013-10-03 18:16:47 -07001324 GFP_KERNEL);
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001325 if (!dp->ports) {
1326 err = -ENOMEM;
1327 goto err_destroy_percpu;
1328 }
1329
1330 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1331 INIT_HLIST_HEAD(&dp->ports[i]);
1332
Jesse Grossccb13522011-10-25 19:26:31 -07001333 /* Set up our datapath device. */
1334 parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1335 parms.type = OVS_VPORT_TYPE_INTERNAL;
1336 parms.options = NULL;
1337 parms.dp = dp;
1338 parms.port_no = OVSP_LOCAL;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001339 parms.upcall_portid = nla_get_u32(a[OVS_DP_ATTR_UPCALL_PID]);
Jesse Grossccb13522011-10-25 19:26:31 -07001340
Thomas Graf43d4be92013-12-13 15:22:18 +01001341 ovs_dp_change(dp, a);
1342
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001343 /* So far only local changes have been made, now need the lock. */
1344 ovs_lock();
1345
Jesse Grossccb13522011-10-25 19:26:31 -07001346 vport = new_vport(&parms);
1347 if (IS_ERR(vport)) {
1348 err = PTR_ERR(vport);
1349 if (err == -EBUSY)
1350 err = -EEXIST;
1351
Thomas Graf44da5ae2013-12-13 15:22:19 +01001352 if (err == -EEXIST) {
1353 /* An outdated user space instance that does not understand
1354 * the concept of user_features has attempted to create a new
1355 * datapath and is likely to reuse it. Drop all user features.
1356 */
1357 if (info->genlhdr->version < OVS_DP_VER_FEATURES)
1358 ovs_dp_reset_user_features(skb, info);
1359 }
1360
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001361 goto err_destroy_ports_array;
Jesse Grossccb13522011-10-25 19:26:31 -07001362 }
1363
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001364 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1365 info->snd_seq, 0, OVS_DP_CMD_NEW);
1366 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001367
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001368 ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001369 list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001370
1371 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001372
Johannes Berg2a94fe42013-11-19 15:19:39 +01001373 ovs_notify(&dp_datapath_genl_family, reply, info);
Jesse Grossccb13522011-10-25 19:26:31 -07001374 return 0;
1375
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001376err_destroy_ports_array:
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001377 ovs_unlock();
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001378 kfree(dp->ports);
Jesse Grossccb13522011-10-25 19:26:31 -07001379err_destroy_percpu:
1380 free_percpu(dp->stats_percpu);
1381err_destroy_table:
Andy Zhoue80857c2014-01-21 09:31:04 -08001382 ovs_flow_tbl_destroy(&dp->table, false);
Jesse Grossccb13522011-10-25 19:26:31 -07001383err_free_dp:
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001384 release_net(ovs_dp_get_net(dp));
Jesse Grossccb13522011-10-25 19:26:31 -07001385 kfree(dp);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001386err_free_reply:
1387 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001388err:
1389 return err;
1390}
1391
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001392/* Called with ovs_mutex. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001393static void __dp_destroy(struct datapath *dp)
Jesse Grossccb13522011-10-25 19:26:31 -07001394{
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001395 int i;
Jesse Grossccb13522011-10-25 19:26:31 -07001396
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001397 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1398 struct vport *vport;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001399 struct hlist_node *n;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001400
Sasha Levinb67bfe02013-02-27 17:06:00 -08001401 hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001402 if (vport->port_no != OVSP_LOCAL)
1403 ovs_dp_detach_port(vport);
1404 }
Jesse Grossccb13522011-10-25 19:26:31 -07001405
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001406 list_del_rcu(&dp->list_node);
Jesse Grossccb13522011-10-25 19:26:31 -07001407
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001408 /* OVSP_LOCAL is datapath internal port. We need to make sure that
Andy Zhoue80857c2014-01-21 09:31:04 -08001409 * all ports in datapath are destroyed first before freeing datapath.
Jesse Grossccb13522011-10-25 19:26:31 -07001410 */
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001411 ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
Jesse Grossccb13522011-10-25 19:26:31 -07001412
Andy Zhoue80857c2014-01-21 09:31:04 -08001413 /* RCU destroy the flow table */
1414 ovs_flow_tbl_destroy(&dp->table, true);
1415
Jesse Grossccb13522011-10-25 19:26:31 -07001416 call_rcu(&dp->rcu, destroy_dp_rcu);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001417}
1418
1419static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1420{
1421 struct sk_buff *reply;
1422 struct datapath *dp;
1423 int err;
1424
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001425 reply = ovs_dp_cmd_alloc_info(info);
1426 if (!reply)
1427 return -ENOMEM;
1428
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001429 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001430 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1431 err = PTR_ERR(dp);
1432 if (IS_ERR(dp))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001433 goto err_unlock_free;
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001434
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001435 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1436 info->snd_seq, 0, OVS_DP_CMD_DEL);
1437 BUG_ON(err < 0);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001438
1439 __dp_destroy(dp);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001440 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001441
Johannes Berg2a94fe42013-11-19 15:19:39 +01001442 ovs_notify(&dp_datapath_genl_family, reply, info);
Jesse Grossccb13522011-10-25 19:26:31 -07001443
1444 return 0;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001445
1446err_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001447 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001448 kfree_skb(reply);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001449 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001450}
1451
1452static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1453{
1454 struct sk_buff *reply;
1455 struct datapath *dp;
1456 int err;
1457
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001458 reply = ovs_dp_cmd_alloc_info(info);
1459 if (!reply)
1460 return -ENOMEM;
1461
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001462 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001463 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001464 err = PTR_ERR(dp);
Jesse Grossccb13522011-10-25 19:26:31 -07001465 if (IS_ERR(dp))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001466 goto err_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001467
Thomas Graf43d4be92013-12-13 15:22:18 +01001468 ovs_dp_change(dp, info->attrs);
1469
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001470 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1471 info->snd_seq, 0, OVS_DP_CMD_NEW);
1472 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001473
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001474 ovs_unlock();
Johannes Berg2a94fe42013-11-19 15:19:39 +01001475 ovs_notify(&dp_datapath_genl_family, reply, info);
Jesse Grossccb13522011-10-25 19:26:31 -07001476
1477 return 0;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001478
1479err_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001480 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001481 kfree_skb(reply);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001482 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001483}
1484
1485static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1486{
1487 struct sk_buff *reply;
1488 struct datapath *dp;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001489 int err;
Jesse Grossccb13522011-10-25 19:26:31 -07001490
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001491 reply = ovs_dp_cmd_alloc_info(info);
1492 if (!reply)
1493 return -ENOMEM;
1494
1495 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001496 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001497 if (IS_ERR(dp)) {
1498 err = PTR_ERR(dp);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001499 goto err_unlock_free;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001500 }
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001501 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1502 info->snd_seq, 0, OVS_DP_CMD_NEW);
1503 BUG_ON(err < 0);
1504 rcu_read_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001505
Jesse Grossccb13522011-10-25 19:26:31 -07001506 return genlmsg_reply(reply, info);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001507
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001508err_unlock_free:
1509 rcu_read_unlock();
1510 kfree_skb(reply);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001511 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001512}
1513
1514static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1515{
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001516 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
Jesse Grossccb13522011-10-25 19:26:31 -07001517 struct datapath *dp;
1518 int skip = cb->args[0];
1519 int i = 0;
1520
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001521 rcu_read_lock();
1522 list_for_each_entry_rcu(dp, &ovs_net->dps, list_node) {
Ben Pfaff77676fd2012-01-17 13:33:39 +00001523 if (i >= skip &&
Eric W. Biederman15e47302012-09-07 20:12:54 +00001524 ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001525 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1526 OVS_DP_CMD_NEW) < 0)
1527 break;
1528 i++;
1529 }
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001530 rcu_read_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001531
1532 cb->args[0] = i;
1533
1534 return skb->len;
1535}
1536
Johannes Berg4534de82013-11-14 17:14:46 +01001537static const struct genl_ops dp_datapath_genl_ops[] = {
Jesse Grossccb13522011-10-25 19:26:31 -07001538 { .cmd = OVS_DP_CMD_NEW,
1539 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1540 .policy = datapath_policy,
1541 .doit = ovs_dp_cmd_new
1542 },
1543 { .cmd = OVS_DP_CMD_DEL,
1544 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1545 .policy = datapath_policy,
1546 .doit = ovs_dp_cmd_del
1547 },
1548 { .cmd = OVS_DP_CMD_GET,
1549 .flags = 0, /* OK for unprivileged users. */
1550 .policy = datapath_policy,
1551 .doit = ovs_dp_cmd_get,
1552 .dumpit = ovs_dp_cmd_dump
1553 },
1554 { .cmd = OVS_DP_CMD_SET,
1555 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1556 .policy = datapath_policy,
1557 .doit = ovs_dp_cmd_set,
1558 },
1559};
1560
1561static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
1562 [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1563 [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
1564 [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
1565 [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
1566 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1567 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
1568};
1569
Johannes Berg68eb5502013-11-19 15:19:38 +01001570struct genl_family dp_vport_genl_family = {
Jesse Grossccb13522011-10-25 19:26:31 -07001571 .id = GENL_ID_GENERATE,
1572 .hdrsize = sizeof(struct ovs_header),
1573 .name = OVS_VPORT_FAMILY,
1574 .version = OVS_VPORT_VERSION,
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001575 .maxattr = OVS_VPORT_ATTR_MAX,
Pravin B Shelar3a4e0d62013-04-23 07:48:48 +00001576 .netnsok = true,
1577 .parallel_ops = true,
Jesse Grossccb13522011-10-25 19:26:31 -07001578};
1579
Stephen Hemminger443cd882013-12-17 19:22:48 +00001580static struct genl_multicast_group ovs_dp_vport_multicast_group = {
Jesse Grossccb13522011-10-25 19:26:31 -07001581 .name = OVS_VPORT_MCGROUP
1582};
1583
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001584/* Called with ovs_mutex or RCU read lock. */
Jesse Grossccb13522011-10-25 19:26:31 -07001585static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001586 u32 portid, u32 seq, u32 flags, u8 cmd)
Jesse Grossccb13522011-10-25 19:26:31 -07001587{
1588 struct ovs_header *ovs_header;
1589 struct ovs_vport_stats vport_stats;
1590 int err;
1591
Eric W. Biederman15e47302012-09-07 20:12:54 +00001592 ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
Jesse Grossccb13522011-10-25 19:26:31 -07001593 flags, cmd);
1594 if (!ovs_header)
1595 return -EMSGSIZE;
1596
1597 ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1598
David S. Miller028d6a62012-03-29 23:20:48 -04001599 if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
1600 nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
1601 nla_put_string(skb, OVS_VPORT_ATTR_NAME, vport->ops->get_name(vport)) ||
Eric W. Biederman15e47302012-09-07 20:12:54 +00001602 nla_put_u32(skb, OVS_VPORT_ATTR_UPCALL_PID, vport->upcall_portid))
David S. Miller028d6a62012-03-29 23:20:48 -04001603 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001604
1605 ovs_vport_get_stats(vport, &vport_stats);
David S. Miller028d6a62012-03-29 23:20:48 -04001606 if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
1607 &vport_stats))
1608 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001609
1610 err = ovs_vport_get_options(vport, skb);
1611 if (err == -EMSGSIZE)
1612 goto error;
1613
1614 return genlmsg_end(skb, ovs_header);
1615
1616nla_put_failure:
1617 err = -EMSGSIZE;
1618error:
1619 genlmsg_cancel(skb, ovs_header);
1620 return err;
1621}
1622
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001623static struct sk_buff *ovs_vport_cmd_alloc_info(void)
1624{
1625 return nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1626}
1627
1628/* Called with ovs_mutex, only via ovs_dp_notify_wq(). */
Eric W. Biederman15e47302012-09-07 20:12:54 +00001629struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001630 u32 seq, u8 cmd)
1631{
1632 struct sk_buff *skb;
1633 int retval;
1634
1635 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1636 if (!skb)
1637 return ERR_PTR(-ENOMEM);
1638
Eric W. Biederman15e47302012-09-07 20:12:54 +00001639 retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
Jesse Grossa9341512013-03-26 15:48:38 -07001640 BUG_ON(retval < 0);
1641
Jesse Grossccb13522011-10-25 19:26:31 -07001642 return skb;
1643}
1644
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001645/* Called with ovs_mutex or RCU read lock. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001646static struct vport *lookup_vport(struct net *net,
1647 struct ovs_header *ovs_header,
Jesse Grossccb13522011-10-25 19:26:31 -07001648 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1649{
1650 struct datapath *dp;
1651 struct vport *vport;
1652
1653 if (a[OVS_VPORT_ATTR_NAME]) {
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001654 vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
Jesse Grossccb13522011-10-25 19:26:31 -07001655 if (!vport)
1656 return ERR_PTR(-ENODEV);
Ben Pfaff651a68e2012-03-06 15:04:04 -08001657 if (ovs_header->dp_ifindex &&
1658 ovs_header->dp_ifindex != get_dpifindex(vport->dp))
1659 return ERR_PTR(-ENODEV);
Jesse Grossccb13522011-10-25 19:26:31 -07001660 return vport;
1661 } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
1662 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1663
1664 if (port_no >= DP_MAX_PORTS)
1665 return ERR_PTR(-EFBIG);
1666
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001667 dp = get_dp(net, ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001668 if (!dp)
1669 return ERR_PTR(-ENODEV);
1670
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001671 vport = ovs_vport_ovsl_rcu(dp, port_no);
Jesse Grossccb13522011-10-25 19:26:31 -07001672 if (!vport)
Jarno Rajahalme14408db2013-01-09 14:27:35 -08001673 return ERR_PTR(-ENODEV);
Jesse Grossccb13522011-10-25 19:26:31 -07001674 return vport;
1675 } else
1676 return ERR_PTR(-EINVAL);
1677}
1678
1679static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
1680{
1681 struct nlattr **a = info->attrs;
1682 struct ovs_header *ovs_header = info->userhdr;
1683 struct vport_parms parms;
1684 struct sk_buff *reply;
1685 struct vport *vport;
1686 struct datapath *dp;
1687 u32 port_no;
1688 int err;
1689
Jesse Grossccb13522011-10-25 19:26:31 -07001690 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
1691 !a[OVS_VPORT_ATTR_UPCALL_PID])
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001692 return -EINVAL;
1693
1694 port_no = a[OVS_VPORT_ATTR_PORT_NO]
1695 ? nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]) : 0;
1696 if (port_no >= DP_MAX_PORTS)
1697 return -EFBIG;
1698
1699 reply = ovs_vport_cmd_alloc_info();
1700 if (!reply)
1701 return -ENOMEM;
Jesse Grossccb13522011-10-25 19:26:31 -07001702
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001703 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001704 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001705 err = -ENODEV;
1706 if (!dp)
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001707 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001708
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001709 if (port_no) {
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001710 vport = ovs_vport_ovsl(dp, port_no);
Jesse Grossccb13522011-10-25 19:26:31 -07001711 err = -EBUSY;
1712 if (vport)
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001713 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001714 } else {
1715 for (port_no = 1; ; port_no++) {
1716 if (port_no >= DP_MAX_PORTS) {
1717 err = -EFBIG;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001718 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001719 }
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001720 vport = ovs_vport_ovsl(dp, port_no);
Jesse Grossccb13522011-10-25 19:26:31 -07001721 if (!vport)
1722 break;
1723 }
1724 }
1725
1726 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
1727 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1728 parms.options = a[OVS_VPORT_ATTR_OPTIONS];
1729 parms.dp = dp;
1730 parms.port_no = port_no;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001731 parms.upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
Jesse Grossccb13522011-10-25 19:26:31 -07001732
1733 vport = new_vport(&parms);
1734 err = PTR_ERR(vport);
1735 if (IS_ERR(vport))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001736 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001737
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001738 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1739 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1740 BUG_ON(err < 0);
1741 ovs_unlock();
Thomas Grafed6611852013-03-29 14:46:50 +01001742
Johannes Berg2a94fe42013-11-19 15:19:39 +01001743 ovs_notify(&dp_vport_genl_family, reply, info);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001744 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001745
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001746exit_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001747 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001748 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001749 return err;
1750}
1751
1752static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
1753{
1754 struct nlattr **a = info->attrs;
1755 struct sk_buff *reply;
1756 struct vport *vport;
1757 int err;
1758
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001759 reply = ovs_vport_cmd_alloc_info();
1760 if (!reply)
1761 return -ENOMEM;
1762
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001763 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001764 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
Jesse Grossccb13522011-10-25 19:26:31 -07001765 err = PTR_ERR(vport);
1766 if (IS_ERR(vport))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001767 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001768
Jesse Grossccb13522011-10-25 19:26:31 -07001769 if (a[OVS_VPORT_ATTR_TYPE] &&
Jesse Grossf44f3402013-05-13 08:15:26 -07001770 nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
Jesse Grossccb13522011-10-25 19:26:31 -07001771 err = -EINVAL;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001772 goto exit_unlock_free;
Jesse Grossa9341512013-03-26 15:48:38 -07001773 }
1774
Jesse Grossf44f3402013-05-13 08:15:26 -07001775 if (a[OVS_VPORT_ATTR_OPTIONS]) {
Jesse Grossccb13522011-10-25 19:26:31 -07001776 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
Jesse Grossf44f3402013-05-13 08:15:26 -07001777 if (err)
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001778 goto exit_unlock_free;
Jesse Grossf44f3402013-05-13 08:15:26 -07001779 }
Jesse Grossa9341512013-03-26 15:48:38 -07001780
Ansis Atteka03fbf8b2012-04-09 12:12:12 -07001781 if (a[OVS_VPORT_ATTR_UPCALL_PID])
Eric W. Biederman15e47302012-09-07 20:12:54 +00001782 vport->upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
Jesse Grossccb13522011-10-25 19:26:31 -07001783
Jesse Grossa9341512013-03-26 15:48:38 -07001784 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1785 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1786 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001787
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001788 ovs_unlock();
Johannes Berg2a94fe42013-11-19 15:19:39 +01001789 ovs_notify(&dp_vport_genl_family, reply, info);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001790 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001791
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001792exit_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001793 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001794 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001795 return err;
1796}
1797
1798static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
1799{
1800 struct nlattr **a = info->attrs;
1801 struct sk_buff *reply;
1802 struct vport *vport;
1803 int err;
1804
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001805 reply = ovs_vport_cmd_alloc_info();
1806 if (!reply)
1807 return -ENOMEM;
1808
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001809 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001810 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
Jesse Grossccb13522011-10-25 19:26:31 -07001811 err = PTR_ERR(vport);
1812 if (IS_ERR(vport))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001813 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001814
1815 if (vport->port_no == OVSP_LOCAL) {
1816 err = -EINVAL;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001817 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001818 }
1819
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001820 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1821 info->snd_seq, 0, OVS_VPORT_CMD_DEL);
1822 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001823 ovs_dp_detach_port(vport);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001824 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001825
Johannes Berg2a94fe42013-11-19 15:19:39 +01001826 ovs_notify(&dp_vport_genl_family, reply, info);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001827 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001828
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001829exit_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001830 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001831 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001832 return err;
1833}
1834
1835static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
1836{
1837 struct nlattr **a = info->attrs;
1838 struct ovs_header *ovs_header = info->userhdr;
1839 struct sk_buff *reply;
1840 struct vport *vport;
1841 int err;
1842
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001843 reply = ovs_vport_cmd_alloc_info();
1844 if (!reply)
1845 return -ENOMEM;
1846
Jesse Grossccb13522011-10-25 19:26:31 -07001847 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001848 vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
Jesse Grossccb13522011-10-25 19:26:31 -07001849 err = PTR_ERR(vport);
1850 if (IS_ERR(vport))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001851 goto exit_unlock_free;
1852 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1853 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1854 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001855 rcu_read_unlock();
1856
1857 return genlmsg_reply(reply, info);
1858
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001859exit_unlock_free:
Jesse Grossccb13522011-10-25 19:26:31 -07001860 rcu_read_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001861 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001862 return err;
1863}
1864
1865static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1866{
1867 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1868 struct datapath *dp;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001869 int bucket = cb->args[0], skip = cb->args[1];
1870 int i, j = 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001871
Jesse Grossccb13522011-10-25 19:26:31 -07001872 rcu_read_lock();
Jarno Rajahalme42ee19e2014-02-15 17:42:29 -08001873 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1874 if (!dp) {
1875 rcu_read_unlock();
1876 return -ENODEV;
1877 }
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001878 for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
Jesse Grossccb13522011-10-25 19:26:31 -07001879 struct vport *vport;
1880
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001881 j = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001882 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001883 if (j >= skip &&
1884 ovs_vport_cmd_fill_info(vport, skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001885 NETLINK_CB(cb->skb).portid,
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001886 cb->nlh->nlmsg_seq,
1887 NLM_F_MULTI,
1888 OVS_VPORT_CMD_NEW) < 0)
1889 goto out;
Jesse Grossccb13522011-10-25 19:26:31 -07001890
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001891 j++;
1892 }
1893 skip = 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001894 }
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001895out:
Jesse Grossccb13522011-10-25 19:26:31 -07001896 rcu_read_unlock();
1897
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001898 cb->args[0] = i;
1899 cb->args[1] = j;
Jesse Grossccb13522011-10-25 19:26:31 -07001900
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001901 return skb->len;
Jesse Grossccb13522011-10-25 19:26:31 -07001902}
1903
Johannes Berg4534de82013-11-14 17:14:46 +01001904static const struct genl_ops dp_vport_genl_ops[] = {
Jesse Grossccb13522011-10-25 19:26:31 -07001905 { .cmd = OVS_VPORT_CMD_NEW,
1906 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1907 .policy = vport_policy,
1908 .doit = ovs_vport_cmd_new
1909 },
1910 { .cmd = OVS_VPORT_CMD_DEL,
1911 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1912 .policy = vport_policy,
1913 .doit = ovs_vport_cmd_del
1914 },
1915 { .cmd = OVS_VPORT_CMD_GET,
1916 .flags = 0, /* OK for unprivileged users. */
1917 .policy = vport_policy,
1918 .doit = ovs_vport_cmd_get,
1919 .dumpit = ovs_vport_cmd_dump
1920 },
1921 { .cmd = OVS_VPORT_CMD_SET,
1922 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1923 .policy = vport_policy,
1924 .doit = ovs_vport_cmd_set,
1925 },
1926};
1927
1928struct genl_family_and_ops {
1929 struct genl_family *family;
Johannes Berg4534de82013-11-14 17:14:46 +01001930 const struct genl_ops *ops;
Jesse Grossccb13522011-10-25 19:26:31 -07001931 int n_ops;
Johannes Berg2a94fe42013-11-19 15:19:39 +01001932 const struct genl_multicast_group *group;
Jesse Grossccb13522011-10-25 19:26:31 -07001933};
1934
1935static const struct genl_family_and_ops dp_genl_families[] = {
1936 { &dp_datapath_genl_family,
1937 dp_datapath_genl_ops, ARRAY_SIZE(dp_datapath_genl_ops),
1938 &ovs_dp_datapath_multicast_group },
1939 { &dp_vport_genl_family,
1940 dp_vport_genl_ops, ARRAY_SIZE(dp_vport_genl_ops),
1941 &ovs_dp_vport_multicast_group },
1942 { &dp_flow_genl_family,
1943 dp_flow_genl_ops, ARRAY_SIZE(dp_flow_genl_ops),
1944 &ovs_dp_flow_multicast_group },
1945 { &dp_packet_genl_family,
1946 dp_packet_genl_ops, ARRAY_SIZE(dp_packet_genl_ops),
1947 NULL },
1948};
1949
1950static void dp_unregister_genl(int n_families)
1951{
1952 int i;
1953
1954 for (i = 0; i < n_families; i++)
1955 genl_unregister_family(dp_genl_families[i].family);
1956}
1957
1958static int dp_register_genl(void)
1959{
1960 int n_registered;
1961 int err;
1962 int i;
1963
1964 n_registered = 0;
1965 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
1966 const struct genl_family_and_ops *f = &dp_genl_families[i];
1967
Johannes Bergc53ed742013-11-19 15:19:31 +01001968 f->family->ops = f->ops;
1969 f->family->n_ops = f->n_ops;
Johannes Berg2a94fe42013-11-19 15:19:39 +01001970 f->family->mcgrps = f->group;
1971 f->family->n_mcgrps = f->group ? 1 : 0;
Johannes Bergc53ed742013-11-19 15:19:31 +01001972 err = genl_register_family(f->family);
Jesse Grossccb13522011-10-25 19:26:31 -07001973 if (err)
1974 goto error;
1975 n_registered++;
Jesse Grossccb13522011-10-25 19:26:31 -07001976 }
1977
1978 return 0;
1979
1980error:
1981 dp_unregister_genl(n_registered);
1982 return err;
1983}
1984
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001985static int __net_init ovs_init_net(struct net *net)
1986{
1987 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
1988
1989 INIT_LIST_HEAD(&ovs_net->dps);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001990 INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001991 return 0;
1992}
1993
1994static void __net_exit ovs_exit_net(struct net *net)
1995{
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001996 struct datapath *dp, *dp_next;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001997 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001998
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001999 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002000 list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
2001 __dp_destroy(dp);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002002 ovs_unlock();
2003
2004 cancel_work_sync(&ovs_net->dp_notify_work);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002005}
2006
2007static struct pernet_operations ovs_net_ops = {
2008 .init = ovs_init_net,
2009 .exit = ovs_exit_net,
2010 .id = &ovs_net_id,
2011 .size = sizeof(struct ovs_net),
2012};
2013
Jesse Grossccb13522011-10-25 19:26:31 -07002014static int __init dp_init(void)
2015{
Jesse Grossccb13522011-10-25 19:26:31 -07002016 int err;
2017
YOSHIFUJI Hideaki / 吉藤英明3523b292013-01-09 07:19:55 +00002018 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
Jesse Grossccb13522011-10-25 19:26:31 -07002019
2020 pr_info("Open vSwitch switching datapath\n");
2021
2022 err = ovs_flow_init();
2023 if (err)
2024 goto error;
2025
2026 err = ovs_vport_init();
2027 if (err)
2028 goto error_flow_exit;
2029
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002030 err = register_pernet_device(&ovs_net_ops);
Jesse Grossccb13522011-10-25 19:26:31 -07002031 if (err)
2032 goto error_vport_exit;
2033
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002034 err = register_netdevice_notifier(&ovs_dp_device_notifier);
2035 if (err)
2036 goto error_netns_exit;
2037
Jesse Grossccb13522011-10-25 19:26:31 -07002038 err = dp_register_genl();
2039 if (err < 0)
2040 goto error_unreg_notifier;
2041
Jesse Grossccb13522011-10-25 19:26:31 -07002042 return 0;
2043
2044error_unreg_notifier:
2045 unregister_netdevice_notifier(&ovs_dp_device_notifier);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002046error_netns_exit:
2047 unregister_pernet_device(&ovs_net_ops);
Jesse Grossccb13522011-10-25 19:26:31 -07002048error_vport_exit:
2049 ovs_vport_exit();
2050error_flow_exit:
2051 ovs_flow_exit();
2052error:
2053 return err;
2054}
2055
2056static void dp_cleanup(void)
2057{
Jesse Grossccb13522011-10-25 19:26:31 -07002058 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
2059 unregister_netdevice_notifier(&ovs_dp_device_notifier);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002060 unregister_pernet_device(&ovs_net_ops);
2061 rcu_barrier();
Jesse Grossccb13522011-10-25 19:26:31 -07002062 ovs_vport_exit();
2063 ovs_flow_exit();
2064}
2065
2066module_init(dp_init);
2067module_exit(dp_cleanup);
2068
2069MODULE_DESCRIPTION("Open vSwitch switching datapath");
2070MODULE_LICENSE("GPL");