blob: 458096da138a3252e2132922d33ac058cfdc0e29 [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;
62
Pravin B Shelar0c200ef2014-05-06 16:44:50 -070063static struct genl_family dp_packet_genl_family;
64static struct genl_family dp_flow_genl_family;
65static struct genl_family dp_datapath_genl_family;
66
stephen hemminger48e48a72014-07-16 11:25:52 -070067static const struct genl_multicast_group ovs_dp_flow_multicast_group = {
68 .name = OVS_FLOW_MCGROUP,
Pravin B Shelar0c200ef2014-05-06 16:44:50 -070069};
70
stephen hemminger48e48a72014-07-16 11:25:52 -070071static const struct genl_multicast_group ovs_dp_datapath_multicast_group = {
72 .name = OVS_DATAPATH_MCGROUP,
Pravin B Shelar0c200ef2014-05-06 16:44:50 -070073};
74
stephen hemminger48e48a72014-07-16 11:25:52 -070075static const struct genl_multicast_group ovs_dp_vport_multicast_group = {
76 .name = OVS_VPORT_MCGROUP,
Pravin B Shelar0c200ef2014-05-06 16:44:50 -070077};
78
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -070079/* Check if need to build a reply message.
80 * OVS userspace sets the NLM_F_ECHO flag if it needs the reply. */
81static bool ovs_must_notify(struct genl_info *info,
82 const struct genl_multicast_group *grp)
83{
84 return info->nlhdr->nlmsg_flags & NLM_F_ECHO ||
85 netlink_has_listeners(genl_info_net(info)->genl_sock, 0);
86}
87
Johannes Berg68eb5502013-11-19 15:19:38 +010088static void ovs_notify(struct genl_family *family,
Johannes Berg2a94fe42013-11-19 15:19:39 +010089 struct sk_buff *skb, struct genl_info *info)
Thomas Grafed661182013-03-29 14:46:50 +010090{
Johannes Berg68eb5502013-11-19 15:19:38 +010091 genl_notify(family, skb, genl_info_net(info), info->snd_portid,
Johannes Berg2a94fe42013-11-19 15:19:39 +010092 0, info->nlhdr, GFP_KERNEL);
Thomas Grafed661182013-03-29 14:46:50 +010093}
94
Pravin B Shelar46df7b82012-02-22 19:58:59 -080095/**
Jesse Grossccb13522011-10-25 19:26:31 -070096 * DOC: Locking:
97 *
Pravin B Shelar8e4e1712013-04-15 13:23:03 -070098 * All writes e.g. Writes to device state (add/remove datapath, port, set
99 * operations on vports, etc.), Writes to other state (flow table
100 * modifications, set miscellaneous datapath parameters, etc.) are protected
101 * by ovs_lock.
Jesse Grossccb13522011-10-25 19:26:31 -0700102 *
103 * Reads are protected by RCU.
104 *
105 * There are a few special cases (mostly stats) that have their own
106 * synchronization but they nest under all of above and don't interact with
107 * each other.
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700108 *
109 * The RTNL lock nests inside ovs_mutex.
Jesse Grossccb13522011-10-25 19:26:31 -0700110 */
111
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700112static DEFINE_MUTEX(ovs_mutex);
113
114void ovs_lock(void)
115{
116 mutex_lock(&ovs_mutex);
117}
118
119void ovs_unlock(void)
120{
121 mutex_unlock(&ovs_mutex);
122}
123
124#ifdef CONFIG_LOCKDEP
125int lockdep_ovsl_is_held(void)
126{
127 if (debug_locks)
128 return lockdep_is_held(&ovs_mutex);
129 else
130 return 1;
131}
132#endif
133
Jesse Grossccb13522011-10-25 19:26:31 -0700134static struct vport *new_vport(const struct vport_parms *);
Thomas Graf8055a892013-12-13 15:22:20 +0100135static int queue_gso_packets(struct datapath *dp, struct sk_buff *,
Jesse Grossccb13522011-10-25 19:26:31 -0700136 const struct dp_upcall_info *);
Thomas Graf8055a892013-12-13 15:22:20 +0100137static int queue_userspace_packet(struct datapath *dp, struct sk_buff *,
Jesse Grossccb13522011-10-25 19:26:31 -0700138 const struct dp_upcall_info *);
139
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700140/* Must be called with rcu_read_lock or ovs_mutex. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800141static struct datapath *get_dp(struct net *net, int dp_ifindex)
Jesse Grossccb13522011-10-25 19:26:31 -0700142{
143 struct datapath *dp = NULL;
144 struct net_device *dev;
145
146 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800147 dev = dev_get_by_index_rcu(net, dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -0700148 if (dev) {
149 struct vport *vport = ovs_internal_dev_get_vport(dev);
150 if (vport)
151 dp = vport->dp;
152 }
153 rcu_read_unlock();
154
155 return dp;
156}
157
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700158/* Must be called with rcu_read_lock or ovs_mutex. */
Stephen Hemminger443cd882013-12-17 19:22:48 +0000159static const char *ovs_dp_name(const struct datapath *dp)
Jesse Grossccb13522011-10-25 19:26:31 -0700160{
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700161 struct vport *vport = ovs_vport_ovsl_rcu(dp, OVSP_LOCAL);
Jesse Grossccb13522011-10-25 19:26:31 -0700162 return vport->ops->get_name(vport);
163}
164
165static int get_dpifindex(struct datapath *dp)
166{
167 struct vport *local;
168 int ifindex;
169
170 rcu_read_lock();
171
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700172 local = ovs_vport_rcu(dp, OVSP_LOCAL);
Jesse Grossccb13522011-10-25 19:26:31 -0700173 if (local)
Thomas Grafcff63a52013-04-29 13:06:41 +0000174 ifindex = netdev_vport_priv(local)->dev->ifindex;
Jesse Grossccb13522011-10-25 19:26:31 -0700175 else
176 ifindex = 0;
177
178 rcu_read_unlock();
179
180 return ifindex;
181}
182
183static void destroy_dp_rcu(struct rcu_head *rcu)
184{
185 struct datapath *dp = container_of(rcu, struct datapath, rcu);
186
Jesse Grossccb13522011-10-25 19:26:31 -0700187 free_percpu(dp->stats_percpu);
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800188 release_net(ovs_dp_get_net(dp));
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700189 kfree(dp->ports);
Jesse Grossccb13522011-10-25 19:26:31 -0700190 kfree(dp);
191}
192
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700193static struct hlist_head *vport_hash_bucket(const struct datapath *dp,
194 u16 port_no)
195{
196 return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)];
197}
198
Jarno Rajahalmebb6f9a72014-05-05 11:32:17 -0700199/* Called with ovs_mutex or RCU read lock. */
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700200struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no)
201{
202 struct vport *vport;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700203 struct hlist_head *head;
204
205 head = vport_hash_bucket(dp, port_no);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800206 hlist_for_each_entry_rcu(vport, head, dp_hash_node) {
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700207 if (vport->port_no == port_no)
208 return vport;
209 }
210 return NULL;
211}
212
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700213/* Called with ovs_mutex. */
Jesse Grossccb13522011-10-25 19:26:31 -0700214static struct vport *new_vport(const struct vport_parms *parms)
215{
216 struct vport *vport;
217
218 vport = ovs_vport_add(parms);
219 if (!IS_ERR(vport)) {
220 struct datapath *dp = parms->dp;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700221 struct hlist_head *head = vport_hash_bucket(dp, vport->port_no);
Jesse Grossccb13522011-10-25 19:26:31 -0700222
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700223 hlist_add_head_rcu(&vport->dp_hash_node, head);
Jesse Grossccb13522011-10-25 19:26:31 -0700224 }
Jesse Grossccb13522011-10-25 19:26:31 -0700225 return vport;
226}
227
Jesse Grossccb13522011-10-25 19:26:31 -0700228void ovs_dp_detach_port(struct vport *p)
229{
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700230 ASSERT_OVSL();
Jesse Grossccb13522011-10-25 19:26:31 -0700231
232 /* First drop references to device. */
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700233 hlist_del_rcu(&p->dp_hash_node);
Jesse Grossccb13522011-10-25 19:26:31 -0700234
235 /* Then destroy it. */
236 ovs_vport_del(p);
237}
238
239/* Must be called with rcu_read_lock. */
240void ovs_dp_process_received_packet(struct vport *p, struct sk_buff *skb)
241{
242 struct datapath *dp = p->dp;
243 struct sw_flow *flow;
244 struct dp_stats_percpu *stats;
245 struct sw_flow_key key;
246 u64 *stats_counter;
Andy Zhou1bd71162013-10-22 10:42:46 -0700247 u32 n_mask_hit;
Jesse Grossccb13522011-10-25 19:26:31 -0700248 int error;
Jesse Grossccb13522011-10-25 19:26:31 -0700249
Shan Wei404f2f12012-11-13 09:52:25 +0800250 stats = this_cpu_ptr(dp->stats_percpu);
Jesse Grossccb13522011-10-25 19:26:31 -0700251
252 /* Extract flow from 'skb' into 'key'. */
Andy Zhou03f0d912013-08-07 20:01:00 -0700253 error = ovs_flow_extract(skb, p->port_no, &key);
Jesse Grossccb13522011-10-25 19:26:31 -0700254 if (unlikely(error)) {
255 kfree_skb(skb);
256 return;
257 }
258
259 /* Look up flow. */
Andy Zhou5bb50632013-11-25 10:42:46 -0800260 flow = ovs_flow_tbl_lookup_stats(&dp->table, &key, &n_mask_hit);
Jesse Grossccb13522011-10-25 19:26:31 -0700261 if (unlikely(!flow)) {
262 struct dp_upcall_info upcall;
263
264 upcall.cmd = OVS_PACKET_CMD_MISS;
265 upcall.key = &key;
266 upcall.userdata = NULL;
Alex Wang5cd667b2014-07-17 15:14:13 -0700267 upcall.portid = ovs_vport_find_upcall_portid(p, skb);
Li RongQingc5eba0b2014-09-03 17:43:45 +0800268 error = ovs_dp_upcall(dp, skb, &upcall);
269 if (unlikely(error))
270 kfree_skb(skb);
271 else
272 consume_skb(skb);
Jesse Grossccb13522011-10-25 19:26:31 -0700273 stats_counter = &stats->n_missed;
274 goto out;
275 }
276
277 OVS_CB(skb)->flow = flow;
278
Ben Pfaffad552002014-05-06 16:48:38 -0700279 ovs_flow_stats_update(OVS_CB(skb)->flow, key.tp.flags, skb);
Pravin B Shelar2ff3e4e2014-09-15 19:15:28 -0700280 ovs_execute_actions(dp, skb, &key);
Pravin B Shelare298e502013-10-29 17:22:21 -0700281 stats_counter = &stats->n_hit;
Jesse Grossccb13522011-10-25 19:26:31 -0700282
283out:
284 /* Update datapath statistics. */
WANG Congdf9d9fd2014-02-14 15:10:46 -0800285 u64_stats_update_begin(&stats->syncp);
Jesse Grossccb13522011-10-25 19:26:31 -0700286 (*stats_counter)++;
Andy Zhou1bd71162013-10-22 10:42:46 -0700287 stats->n_mask_hit += n_mask_hit;
WANG Congdf9d9fd2014-02-14 15:10:46 -0800288 u64_stats_update_end(&stats->syncp);
Jesse Grossccb13522011-10-25 19:26:31 -0700289}
290
Jesse Grossccb13522011-10-25 19:26:31 -0700291int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800292 const struct dp_upcall_info *upcall_info)
Jesse Grossccb13522011-10-25 19:26:31 -0700293{
294 struct dp_stats_percpu *stats;
Jesse Grossccb13522011-10-25 19:26:31 -0700295 int err;
296
Eric W. Biederman15e47302012-09-07 20:12:54 +0000297 if (upcall_info->portid == 0) {
Jesse Grossccb13522011-10-25 19:26:31 -0700298 err = -ENOTCONN;
299 goto err;
300 }
301
Jesse Grossccb13522011-10-25 19:26:31 -0700302 if (!skb_is_gso(skb))
Thomas Graf8055a892013-12-13 15:22:20 +0100303 err = queue_userspace_packet(dp, skb, upcall_info);
Jesse Grossccb13522011-10-25 19:26:31 -0700304 else
Thomas Graf8055a892013-12-13 15:22:20 +0100305 err = queue_gso_packets(dp, skb, upcall_info);
Jesse Grossccb13522011-10-25 19:26:31 -0700306 if (err)
307 goto err;
308
309 return 0;
310
311err:
Shan Wei404f2f12012-11-13 09:52:25 +0800312 stats = this_cpu_ptr(dp->stats_percpu);
Jesse Grossccb13522011-10-25 19:26:31 -0700313
WANG Congdf9d9fd2014-02-14 15:10:46 -0800314 u64_stats_update_begin(&stats->syncp);
Jesse Grossccb13522011-10-25 19:26:31 -0700315 stats->n_lost++;
WANG Congdf9d9fd2014-02-14 15:10:46 -0800316 u64_stats_update_end(&stats->syncp);
Jesse Grossccb13522011-10-25 19:26:31 -0700317
318 return err;
319}
320
Thomas Graf8055a892013-12-13 15:22:20 +0100321static int queue_gso_packets(struct datapath *dp, struct sk_buff *skb,
Jesse Grossccb13522011-10-25 19:26:31 -0700322 const struct dp_upcall_info *upcall_info)
323{
Ben Pfaffa1b5d0d2012-07-20 14:47:54 -0700324 unsigned short gso_type = skb_shinfo(skb)->gso_type;
Jesse Grossccb13522011-10-25 19:26:31 -0700325 struct dp_upcall_info later_info;
326 struct sw_flow_key later_key;
327 struct sk_buff *segs, *nskb;
328 int err;
329
Thomas Graf09c5e602013-12-13 15:22:22 +0100330 segs = __skb_gso_segment(skb, NETIF_F_SG, false);
Pravin B Shelar92e5dfc2012-07-20 14:46:29 -0700331 if (IS_ERR(segs))
332 return PTR_ERR(segs);
Jesse Grossccb13522011-10-25 19:26:31 -0700333
334 /* Queue all of the segments. */
335 skb = segs;
336 do {
Thomas Graf8055a892013-12-13 15:22:20 +0100337 err = queue_userspace_packet(dp, skb, upcall_info);
Jesse Grossccb13522011-10-25 19:26:31 -0700338 if (err)
339 break;
340
Ben Pfaffa1b5d0d2012-07-20 14:47:54 -0700341 if (skb == segs && gso_type & SKB_GSO_UDP) {
Jesse Grossccb13522011-10-25 19:26:31 -0700342 /* The initial flow key extracted by ovs_flow_extract()
343 * in this case is for a first fragment, so we need to
344 * properly mark later fragments.
345 */
346 later_key = *upcall_info->key;
347 later_key.ip.frag = OVS_FRAG_TYPE_LATER;
348
349 later_info = *upcall_info;
350 later_info.key = &later_key;
351 upcall_info = &later_info;
352 }
353 } while ((skb = skb->next));
354
355 /* Free all of the segments. */
356 skb = segs;
357 do {
358 nskb = skb->next;
359 if (err)
360 kfree_skb(skb);
361 else
362 consume_skb(skb);
363 } while ((skb = nskb));
364 return err;
365}
366
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100367static size_t key_attr_size(void)
368{
369 return nla_total_size(4) /* OVS_KEY_ATTR_PRIORITY */
Pravin B Shelar7d5437c2013-06-17 17:50:18 -0700370 + nla_total_size(0) /* OVS_KEY_ATTR_TUNNEL */
371 + nla_total_size(8) /* OVS_TUNNEL_KEY_ATTR_ID */
372 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_SRC */
373 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_DST */
374 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TOS */
375 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TTL */
376 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT */
377 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_CSUM */
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100378 + nla_total_size(4) /* OVS_KEY_ATTR_IN_PORT */
379 + nla_total_size(4) /* OVS_KEY_ATTR_SKB_MARK */
380 + nla_total_size(12) /* OVS_KEY_ATTR_ETHERNET */
381 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
382 + nla_total_size(4) /* OVS_KEY_ATTR_8021Q */
383 + nla_total_size(0) /* OVS_KEY_ATTR_ENCAP */
384 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
385 + nla_total_size(40) /* OVS_KEY_ATTR_IPV6 */
386 + nla_total_size(2) /* OVS_KEY_ATTR_ICMPV6 */
387 + nla_total_size(28); /* OVS_KEY_ATTR_ND */
388}
389
Thomas Grafbda56f12013-12-13 15:22:21 +0100390static size_t upcall_msg_size(const struct nlattr *userdata,
391 unsigned int hdrlen)
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100392{
393 size_t size = NLMSG_ALIGN(sizeof(struct ovs_header))
Thomas Grafbda56f12013-12-13 15:22:21 +0100394 + nla_total_size(hdrlen) /* OVS_PACKET_ATTR_PACKET */
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100395 + nla_total_size(key_attr_size()); /* OVS_PACKET_ATTR_KEY */
396
397 /* OVS_PACKET_ATTR_USERDATA */
398 if (userdata)
399 size += NLA_ALIGN(userdata->nla_len);
400
401 return size;
402}
403
Thomas Graf8055a892013-12-13 15:22:20 +0100404static int queue_userspace_packet(struct datapath *dp, struct sk_buff *skb,
Jesse Grossccb13522011-10-25 19:26:31 -0700405 const struct dp_upcall_info *upcall_info)
406{
407 struct ovs_header *upcall;
408 struct sk_buff *nskb = NULL;
Li RongQing4ee45ea2014-09-02 20:52:28 +0800409 struct sk_buff *user_skb = NULL; /* to be queued to userspace */
Jesse Grossccb13522011-10-25 19:26:31 -0700410 struct nlattr *nla;
Thomas Graf795449d2013-11-30 13:21:32 +0100411 struct genl_info info = {
Thomas Graf8055a892013-12-13 15:22:20 +0100412 .dst_sk = ovs_dp_get_net(dp)->genl_sock,
Thomas Graf795449d2013-11-30 13:21:32 +0100413 .snd_portid = upcall_info->portid,
414 };
415 size_t len;
Thomas Grafbda56f12013-12-13 15:22:21 +0100416 unsigned int hlen;
Thomas Graf8055a892013-12-13 15:22:20 +0100417 int err, dp_ifindex;
418
419 dp_ifindex = get_dpifindex(dp);
420 if (!dp_ifindex)
421 return -ENODEV;
Jesse Grossccb13522011-10-25 19:26:31 -0700422
423 if (vlan_tx_tag_present(skb)) {
424 nskb = skb_clone(skb, GFP_ATOMIC);
425 if (!nskb)
426 return -ENOMEM;
427
Patrick McHardy86a9bad2013-04-19 02:04:30 +0000428 nskb = __vlan_put_tag(nskb, nskb->vlan_proto, vlan_tx_tag_get(nskb));
Dan Carpenter8aa51d62012-05-13 08:44:18 +0000429 if (!nskb)
Jesse Grossccb13522011-10-25 19:26:31 -0700430 return -ENOMEM;
431
432 nskb->vlan_tci = 0;
433 skb = nskb;
434 }
435
436 if (nla_attr_size(skb->len) > USHRT_MAX) {
437 err = -EFBIG;
438 goto out;
439 }
440
Thomas Grafbda56f12013-12-13 15:22:21 +0100441 /* Complete checksum if needed */
442 if (skb->ip_summed == CHECKSUM_PARTIAL &&
443 (err = skb_checksum_help(skb)))
444 goto out;
445
446 /* Older versions of OVS user space enforce alignment of the last
447 * Netlink attribute to NLA_ALIGNTO which would require extensive
448 * padding logic. Only perform zerocopy if padding is not required.
449 */
450 if (dp->user_features & OVS_DP_F_UNALIGNED)
451 hlen = skb_zerocopy_headlen(skb);
452 else
453 hlen = skb->len;
454
455 len = upcall_msg_size(upcall_info->userdata, hlen);
Thomas Graf795449d2013-11-30 13:21:32 +0100456 user_skb = genlmsg_new_unicast(len, &info, GFP_ATOMIC);
Jesse Grossccb13522011-10-25 19:26:31 -0700457 if (!user_skb) {
458 err = -ENOMEM;
459 goto out;
460 }
461
462 upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
463 0, upcall_info->cmd);
464 upcall->dp_ifindex = dp_ifindex;
465
466 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
Andy Zhouf53e3832014-07-17 15:17:44 -0700467 err = ovs_nla_put_flow(upcall_info->key, upcall_info->key, user_skb);
468 BUG_ON(err);
Jesse Grossccb13522011-10-25 19:26:31 -0700469 nla_nest_end(user_skb, nla);
470
471 if (upcall_info->userdata)
Ben Pfaff44901082013-02-15 17:29:22 -0800472 __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA,
473 nla_len(upcall_info->userdata),
474 nla_data(upcall_info->userdata));
Jesse Grossccb13522011-10-25 19:26:31 -0700475
Thomas Grafbda56f12013-12-13 15:22:21 +0100476 /* Only reserve room for attribute header, packet data is added
477 * in skb_zerocopy() */
478 if (!(nla = nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, 0))) {
479 err = -ENOBUFS;
480 goto out;
481 }
482 nla->nla_len = nla_attr_size(skb->len);
Jesse Grossccb13522011-10-25 19:26:31 -0700483
Zoltan Kiss36d5fe62014-03-26 22:37:45 +0000484 err = skb_zerocopy(user_skb, skb, skb->len, hlen);
485 if (err)
486 goto out;
Jesse Grossccb13522011-10-25 19:26:31 -0700487
Thomas Grafaea0bb42014-01-14 16:27:49 +0000488 /* Pad OVS_PACKET_ATTR_PACKET if linear copy was performed */
489 if (!(dp->user_features & OVS_DP_F_UNALIGNED)) {
490 size_t plen = NLA_ALIGN(user_skb->len) - user_skb->len;
491
492 if (plen > 0)
493 memset(skb_put(user_skb, plen), 0, plen);
494 }
495
Thomas Grafbda56f12013-12-13 15:22:21 +0100496 ((struct nlmsghdr *) user_skb->data)->nlmsg_len = user_skb->len;
497
Thomas Graf8055a892013-12-13 15:22:20 +0100498 err = genlmsg_unicast(ovs_dp_get_net(dp), user_skb, upcall_info->portid);
Li RongQing4ee45ea2014-09-02 20:52:28 +0800499 user_skb = NULL;
Jesse Grossccb13522011-10-25 19:26:31 -0700500out:
Zoltan Kiss36d5fe62014-03-26 22:37:45 +0000501 if (err)
502 skb_tx_error(skb);
Li RongQing4ee45ea2014-09-02 20:52:28 +0800503 kfree_skb(user_skb);
Jesse Grossccb13522011-10-25 19:26:31 -0700504 kfree_skb(nskb);
505 return err;
506}
507
Jesse Grossccb13522011-10-25 19:26:31 -0700508static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
509{
510 struct ovs_header *ovs_header = info->userhdr;
511 struct nlattr **a = info->attrs;
512 struct sw_flow_actions *acts;
513 struct sk_buff *packet;
514 struct sw_flow *flow;
515 struct datapath *dp;
516 struct ethhdr *eth;
517 int len;
518 int err;
Jesse Grossccb13522011-10-25 19:26:31 -0700519
520 err = -EINVAL;
521 if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
Thomas Grafdded45fc2013-03-29 14:46:47 +0100522 !a[OVS_PACKET_ATTR_ACTIONS])
Jesse Grossccb13522011-10-25 19:26:31 -0700523 goto err;
524
525 len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
526 packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
527 err = -ENOMEM;
528 if (!packet)
529 goto err;
530 skb_reserve(packet, NET_IP_ALIGN);
531
Thomas Graf32686a92013-03-29 14:46:48 +0100532 nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
Jesse Grossccb13522011-10-25 19:26:31 -0700533
534 skb_reset_mac_header(packet);
535 eth = eth_hdr(packet);
536
537 /* Normally, setting the skb 'protocol' field would be handled by a
538 * call to eth_type_trans(), but it assumes there's a sending
539 * device, which we may not have. */
Simon Hormane5c5d222013-03-28 13:38:25 +0900540 if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN)
Jesse Grossccb13522011-10-25 19:26:31 -0700541 packet->protocol = eth->h_proto;
542 else
543 packet->protocol = htons(ETH_P_802_2);
544
545 /* Build an sw_flow for sending this packet. */
Jarno Rajahalme23dabf82014-03-27 12:35:23 -0700546 flow = ovs_flow_alloc();
Jesse Grossccb13522011-10-25 19:26:31 -0700547 err = PTR_ERR(flow);
548 if (IS_ERR(flow))
549 goto err_kfree_skb;
550
Andy Zhou03f0d912013-08-07 20:01:00 -0700551 err = ovs_flow_extract(packet, -1, &flow->key);
Jesse Grossccb13522011-10-25 19:26:31 -0700552 if (err)
553 goto err_flow_free;
554
Pravin B Shelare6445712013-10-03 18:16:47 -0700555 err = ovs_nla_get_flow_metadata(flow, a[OVS_PACKET_ATTR_KEY]);
Jesse Grossccb13522011-10-25 19:26:31 -0700556 if (err)
557 goto err_flow_free;
Pravin B Shelare6445712013-10-03 18:16:47 -0700558 acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_PACKET_ATTR_ACTIONS]));
Jesse Grossccb13522011-10-25 19:26:31 -0700559 err = PTR_ERR(acts);
560 if (IS_ERR(acts))
561 goto err_flow_free;
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700562
Pravin B Shelare6445712013-10-03 18:16:47 -0700563 err = ovs_nla_copy_actions(a[OVS_PACKET_ATTR_ACTIONS],
564 &flow->key, 0, &acts);
Jesse Grossccb13522011-10-25 19:26:31 -0700565 rcu_assign_pointer(flow->sf_acts, acts);
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700566 if (err)
567 goto err_flow_free;
Jesse Grossccb13522011-10-25 19:26:31 -0700568
569 OVS_CB(packet)->flow = flow;
570 packet->priority = flow->key.phy.priority;
Ansis Atteka39c7caeb2012-11-26 11:24:11 -0800571 packet->mark = flow->key.phy.skb_mark;
Jesse Grossccb13522011-10-25 19:26:31 -0700572
573 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800574 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -0700575 err = -ENODEV;
576 if (!dp)
577 goto err_unlock;
578
579 local_bh_disable();
Pravin B Shelar2ff3e4e2014-09-15 19:15:28 -0700580 err = ovs_execute_actions(dp, packet, &flow->key);
Jesse Grossccb13522011-10-25 19:26:31 -0700581 local_bh_enable();
582 rcu_read_unlock();
583
Andy Zhou03f0d912013-08-07 20:01:00 -0700584 ovs_flow_free(flow, false);
Jesse Grossccb13522011-10-25 19:26:31 -0700585 return err;
586
587err_unlock:
588 rcu_read_unlock();
589err_flow_free:
Andy Zhou03f0d912013-08-07 20:01:00 -0700590 ovs_flow_free(flow, false);
Jesse Grossccb13522011-10-25 19:26:31 -0700591err_kfree_skb:
592 kfree_skb(packet);
593err:
594 return err;
595}
596
597static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
Thomas Grafdded45fc2013-03-29 14:46:47 +0100598 [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
Jesse Grossccb13522011-10-25 19:26:31 -0700599 [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
600 [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
601};
602
Johannes Berg4534de82013-11-14 17:14:46 +0100603static const struct genl_ops dp_packet_genl_ops[] = {
Jesse Grossccb13522011-10-25 19:26:31 -0700604 { .cmd = OVS_PACKET_CMD_EXECUTE,
605 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
606 .policy = packet_policy,
607 .doit = ovs_packet_cmd_execute
608 }
609};
610
Pravin B Shelar0c200ef2014-05-06 16:44:50 -0700611static struct genl_family dp_packet_genl_family = {
612 .id = GENL_ID_GENERATE,
613 .hdrsize = sizeof(struct ovs_header),
614 .name = OVS_PACKET_FAMILY,
615 .version = OVS_PACKET_VERSION,
616 .maxattr = OVS_PACKET_ATTR_MAX,
617 .netnsok = true,
618 .parallel_ops = true,
619 .ops = dp_packet_genl_ops,
620 .n_ops = ARRAY_SIZE(dp_packet_genl_ops),
621};
622
Andy Zhou1bd71162013-10-22 10:42:46 -0700623static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats,
624 struct ovs_dp_megaflow_stats *mega_stats)
Jesse Grossccb13522011-10-25 19:26:31 -0700625{
626 int i;
Jesse Grossccb13522011-10-25 19:26:31 -0700627
Andy Zhou1bd71162013-10-22 10:42:46 -0700628 memset(mega_stats, 0, sizeof(*mega_stats));
629
Pravin B Shelarb637e492013-10-04 00:14:23 -0700630 stats->n_flows = ovs_flow_tbl_count(&dp->table);
Andy Zhou1bd71162013-10-22 10:42:46 -0700631 mega_stats->n_masks = ovs_flow_tbl_num_masks(&dp->table);
Jesse Grossccb13522011-10-25 19:26:31 -0700632
633 stats->n_hit = stats->n_missed = stats->n_lost = 0;
Andy Zhou1bd71162013-10-22 10:42:46 -0700634
Jesse Grossccb13522011-10-25 19:26:31 -0700635 for_each_possible_cpu(i) {
636 const struct dp_stats_percpu *percpu_stats;
637 struct dp_stats_percpu local_stats;
638 unsigned int start;
639
640 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
641
642 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700643 start = u64_stats_fetch_begin_irq(&percpu_stats->syncp);
Jesse Grossccb13522011-10-25 19:26:31 -0700644 local_stats = *percpu_stats;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700645 } while (u64_stats_fetch_retry_irq(&percpu_stats->syncp, start));
Jesse Grossccb13522011-10-25 19:26:31 -0700646
647 stats->n_hit += local_stats.n_hit;
648 stats->n_missed += local_stats.n_missed;
649 stats->n_lost += local_stats.n_lost;
Andy Zhou1bd71162013-10-22 10:42:46 -0700650 mega_stats->n_mask_hit += local_stats.n_mask_hit;
Jesse Grossccb13522011-10-25 19:26:31 -0700651 }
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);
Himangi Saraogid0e992a2014-07-27 12:37:46 +0530785 if (IS_ERR_OR_NULL(skb))
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700786 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;
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700799 struct sw_flow *flow, *new_flow;
Andy Zhou03f0d912013-08-07 20:01:00 -0700800 struct sw_flow_mask mask;
Jesse Grossccb13522011-10-25 19:26:31 -0700801 struct sk_buff *reply;
802 struct datapath *dp;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700803 struct sw_flow_actions *acts;
804 struct sw_flow_match match;
805 int error;
806
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700807 /* Must have key and actions. */
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700808 error = -EINVAL;
809 if (!a[OVS_FLOW_ATTR_KEY])
810 goto error;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700811 if (!a[OVS_FLOW_ATTR_ACTIONS])
812 goto error;
813
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700814 /* Most of the time we need to allocate a new flow, do it before
815 * locking.
816 */
817 new_flow = ovs_flow_alloc();
818 if (IS_ERR(new_flow)) {
819 error = PTR_ERR(new_flow);
820 goto error;
821 }
822
823 /* Extract key. */
824 ovs_match_init(&match, &new_flow->unmasked_key, &mask);
825 error = ovs_nla_get_match(&match,
826 a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
827 if (error)
828 goto err_kfree_flow;
829
830 ovs_flow_mask_key(&new_flow->key, &new_flow->unmasked_key, &mask);
831
832 /* Validate actions. */
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700833 acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_FLOW_ATTR_ACTIONS]));
834 error = PTR_ERR(acts);
835 if (IS_ERR(acts))
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700836 goto err_kfree_flow;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700837
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700838 error = ovs_nla_copy_actions(a[OVS_FLOW_ATTR_ACTIONS], &new_flow->key,
839 0, &acts);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700840 if (error) {
841 OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700842 goto err_kfree_acts;
843 }
844
845 reply = ovs_flow_cmd_alloc_info(acts, info, false);
846 if (IS_ERR(reply)) {
847 error = PTR_ERR(reply);
848 goto err_kfree_acts;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700849 }
850
851 ovs_lock();
852 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700853 if (unlikely(!dp)) {
854 error = -ENODEV;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700855 goto err_unlock_ovs;
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700856 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700857 /* Check if this is a duplicate flow */
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700858 flow = ovs_flow_tbl_lookup(&dp->table, &new_flow->unmasked_key);
859 if (likely(!flow)) {
860 rcu_assign_pointer(new_flow->sf_acts, acts);
861
862 /* Put flow in bucket. */
863 error = ovs_flow_tbl_insert(&dp->table, new_flow, &mask);
864 if (unlikely(error)) {
865 acts = NULL;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700866 goto err_unlock_ovs;
867 }
868
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700869 if (unlikely(reply)) {
870 error = ovs_flow_cmd_fill_info(new_flow,
871 ovs_header->dp_ifindex,
872 reply, info->snd_portid,
873 info->snd_seq, 0,
874 OVS_FLOW_CMD_NEW);
875 BUG_ON(error < 0);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700876 }
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700877 ovs_unlock();
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700878 } else {
879 struct sw_flow_actions *old_acts;
880
881 /* Bail out if we're not allowed to modify an existing flow.
882 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
883 * because Generic Netlink treats the latter as a dump
884 * request. We also accept NLM_F_EXCL in case that bug ever
885 * gets fixed.
886 */
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700887 if (unlikely(info->nlhdr->nlmsg_flags & (NLM_F_CREATE
888 | NLM_F_EXCL))) {
889 error = -EEXIST;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700890 goto err_unlock_ovs;
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700891 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700892 /* The unmasked key has to be the same for flow updates. */
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700893 if (unlikely(!ovs_flow_cmp_unmasked_key(flow, &match))) {
Alex Wang4a46b242014-06-30 20:30:29 -0700894 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
895 if (!flow) {
896 error = -ENOENT;
897 goto err_unlock_ovs;
898 }
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700899 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700900 /* Update actions. */
901 old_acts = ovsl_dereference(flow->sf_acts);
902 rcu_assign_pointer(flow->sf_acts, acts);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700903
904 if (unlikely(reply)) {
905 error = ovs_flow_cmd_fill_info(flow,
906 ovs_header->dp_ifindex,
907 reply, info->snd_portid,
908 info->snd_seq, 0,
909 OVS_FLOW_CMD_NEW);
910 BUG_ON(error < 0);
911 }
912 ovs_unlock();
913
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700914 ovs_nla_free_flow_actions(old_acts);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700915 ovs_flow_free(new_flow, false);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700916 }
917
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700918 if (reply)
919 ovs_notify(&dp_flow_genl_family, reply, info);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700920 return 0;
921
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700922err_unlock_ovs:
923 ovs_unlock();
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700924 kfree_skb(reply);
925err_kfree_acts:
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700926 kfree(acts);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700927err_kfree_flow:
928 ovs_flow_free(new_flow, false);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700929error:
930 return error;
931}
932
933static int ovs_flow_cmd_set(struct sk_buff *skb, struct genl_info *info)
934{
935 struct nlattr **a = info->attrs;
936 struct ovs_header *ovs_header = info->userhdr;
937 struct sw_flow_key key, masked_key;
938 struct sw_flow *flow;
939 struct sw_flow_mask mask;
940 struct sk_buff *reply = NULL;
941 struct datapath *dp;
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700942 struct sw_flow_actions *old_acts = NULL, *acts = NULL;
Andy Zhou03f0d912013-08-07 20:01:00 -0700943 struct sw_flow_match match;
Jesse Grossccb13522011-10-25 19:26:31 -0700944 int error;
Jesse Grossccb13522011-10-25 19:26:31 -0700945
946 /* Extract key. */
947 error = -EINVAL;
948 if (!a[OVS_FLOW_ATTR_KEY])
949 goto error;
Andy Zhou03f0d912013-08-07 20:01:00 -0700950
951 ovs_match_init(&match, &key, &mask);
Jarno Rajahalme23dabf82014-03-27 12:35:23 -0700952 error = ovs_nla_get_match(&match,
Pravin B Shelare6445712013-10-03 18:16:47 -0700953 a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
Jesse Grossccb13522011-10-25 19:26:31 -0700954 if (error)
955 goto error;
956
957 /* Validate actions. */
958 if (a[OVS_FLOW_ATTR_ACTIONS]) {
Pravin B Shelare6445712013-10-03 18:16:47 -0700959 acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_FLOW_ATTR_ACTIONS]));
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700960 error = PTR_ERR(acts);
961 if (IS_ERR(acts))
Jesse Grossccb13522011-10-25 19:26:31 -0700962 goto error;
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700963
Pravin B Shelare6445712013-10-03 18:16:47 -0700964 ovs_flow_mask_key(&masked_key, &key, &mask);
965 error = ovs_nla_copy_actions(a[OVS_FLOW_ATTR_ACTIONS],
966 &masked_key, 0, &acts);
Andy Zhou03f0d912013-08-07 20:01:00 -0700967 if (error) {
968 OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700969 goto err_kfree_acts;
970 }
971 }
972
973 /* Can allocate before locking if have acts. */
974 if (acts) {
975 reply = ovs_flow_cmd_alloc_info(acts, info, false);
976 if (IS_ERR(reply)) {
977 error = PTR_ERR(reply);
978 goto err_kfree_acts;
Andy Zhou03f0d912013-08-07 20:01:00 -0700979 }
Jesse Grossccb13522011-10-25 19:26:31 -0700980 }
981
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700982 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800983 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700984 if (unlikely(!dp)) {
985 error = -ENODEV;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700986 goto err_unlock_ovs;
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700987 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700988 /* Check that the flow exists. */
Alex Wang4a46b242014-06-30 20:30:29 -0700989 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700990 if (unlikely(!flow)) {
991 error = -ENOENT;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700992 goto err_unlock_ovs;
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700993 }
Alex Wang4a46b242014-06-30 20:30:29 -0700994
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700995 /* Update actions, if present. */
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700996 if (likely(acts)) {
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700997 old_acts = ovsl_dereference(flow->sf_acts);
Jesse Grossccb13522011-10-25 19:26:31 -0700998 rcu_assign_pointer(flow->sf_acts, acts);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700999
1000 if (unlikely(reply)) {
1001 error = ovs_flow_cmd_fill_info(flow,
1002 ovs_header->dp_ifindex,
1003 reply, info->snd_portid,
1004 info->snd_seq, 0,
1005 OVS_FLOW_CMD_NEW);
1006 BUG_ON(error < 0);
1007 }
1008 } else {
1009 /* Could not alloc without acts before locking. */
1010 reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex,
1011 info, OVS_FLOW_CMD_NEW, false);
1012 if (unlikely(IS_ERR(reply))) {
1013 error = PTR_ERR(reply);
1014 goto err_unlock_ovs;
1015 }
Jesse Grossccb13522011-10-25 19:26:31 -07001016 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001017
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001018 /* Clear stats. */
1019 if (a[OVS_FLOW_ATTR_CLEAR])
1020 ovs_flow_stats_clear(flow);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001021 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001022
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001023 if (reply)
1024 ovs_notify(&dp_flow_genl_family, reply, info);
1025 if (old_acts)
1026 ovs_nla_free_flow_actions(old_acts);
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -07001027
Jesse Grossccb13522011-10-25 19:26:31 -07001028 return 0;
1029
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001030err_unlock_ovs:
1031 ovs_unlock();
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001032 kfree_skb(reply);
1033err_kfree_acts:
Pravin B Shelar74f84a52013-06-17 17:50:12 -07001034 kfree(acts);
Jesse Grossccb13522011-10-25 19:26:31 -07001035error:
1036 return error;
1037}
1038
1039static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1040{
1041 struct nlattr **a = info->attrs;
1042 struct ovs_header *ovs_header = info->userhdr;
1043 struct sw_flow_key key;
1044 struct sk_buff *reply;
1045 struct sw_flow *flow;
1046 struct datapath *dp;
Andy Zhou03f0d912013-08-07 20:01:00 -07001047 struct sw_flow_match match;
Jesse Grossccb13522011-10-25 19:26:31 -07001048 int err;
Jesse Grossccb13522011-10-25 19:26:31 -07001049
Andy Zhou03f0d912013-08-07 20:01:00 -07001050 if (!a[OVS_FLOW_ATTR_KEY]) {
1051 OVS_NLERR("Flow get message rejected, Key attribute missing.\n");
Jesse Grossccb13522011-10-25 19:26:31 -07001052 return -EINVAL;
Andy Zhou03f0d912013-08-07 20:01:00 -07001053 }
1054
1055 ovs_match_init(&match, &key, NULL);
Jarno Rajahalme23dabf82014-03-27 12:35:23 -07001056 err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL);
Jesse Grossccb13522011-10-25 19:26:31 -07001057 if (err)
1058 return err;
1059
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001060 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001061 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001062 if (!dp) {
1063 err = -ENODEV;
1064 goto unlock;
1065 }
Jesse Grossccb13522011-10-25 19:26:31 -07001066
Alex Wang4a46b242014-06-30 20:30:29 -07001067 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1068 if (!flow) {
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001069 err = -ENOENT;
1070 goto unlock;
1071 }
Jesse Grossccb13522011-10-25 19:26:31 -07001072
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -07001073 reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex, info,
1074 OVS_FLOW_CMD_NEW, true);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001075 if (IS_ERR(reply)) {
1076 err = PTR_ERR(reply);
1077 goto unlock;
1078 }
Jesse Grossccb13522011-10-25 19:26:31 -07001079
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001080 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001081 return genlmsg_reply(reply, info);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001082unlock:
1083 ovs_unlock();
1084 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001085}
1086
1087static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1088{
1089 struct nlattr **a = info->attrs;
1090 struct ovs_header *ovs_header = info->userhdr;
1091 struct sw_flow_key key;
1092 struct sk_buff *reply;
1093 struct sw_flow *flow;
1094 struct datapath *dp;
Andy Zhou03f0d912013-08-07 20:01:00 -07001095 struct sw_flow_match match;
Jesse Grossccb13522011-10-25 19:26:31 -07001096 int err;
Jesse Grossccb13522011-10-25 19:26:31 -07001097
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001098 if (likely(a[OVS_FLOW_ATTR_KEY])) {
1099 ovs_match_init(&match, &key, NULL);
1100 err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL);
1101 if (unlikely(err))
1102 return err;
1103 }
1104
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001105 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001106 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001107 if (unlikely(!dp)) {
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001108 err = -ENODEV;
1109 goto unlock;
1110 }
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001111
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001112 if (unlikely(!a[OVS_FLOW_ATTR_KEY])) {
Pravin B Shelarb637e492013-10-04 00:14:23 -07001113 err = ovs_flow_tbl_flush(&dp->table);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001114 goto unlock;
1115 }
Andy Zhou03f0d912013-08-07 20:01:00 -07001116
Alex Wang4a46b242014-06-30 20:30:29 -07001117 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1118 if (unlikely(!flow)) {
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001119 err = -ENOENT;
1120 goto unlock;
1121 }
Jesse Grossccb13522011-10-25 19:26:31 -07001122
Pravin B Shelarb637e492013-10-04 00:14:23 -07001123 ovs_flow_tbl_remove(&dp->table, flow);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001124 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001125
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001126 reply = ovs_flow_cmd_alloc_info((const struct sw_flow_actions __force *) flow->sf_acts,
1127 info, false);
1128 if (likely(reply)) {
1129 if (likely(!IS_ERR(reply))) {
1130 rcu_read_lock(); /*To keep RCU checker happy. */
1131 err = ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex,
1132 reply, info->snd_portid,
1133 info->snd_seq, 0,
1134 OVS_FLOW_CMD_DEL);
1135 rcu_read_unlock();
1136 BUG_ON(err < 0);
1137
1138 ovs_notify(&dp_flow_genl_family, reply, info);
1139 } else {
1140 netlink_set_err(sock_net(skb->sk)->genl_sock, 0, 0, PTR_ERR(reply));
1141 }
1142 }
1143
1144 ovs_flow_free(flow, true);
Jesse Grossccb13522011-10-25 19:26:31 -07001145 return 0;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001146unlock:
1147 ovs_unlock();
1148 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001149}
1150
1151static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1152{
1153 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
Pravin B Shelarb637e492013-10-04 00:14:23 -07001154 struct table_instance *ti;
Jesse Grossccb13522011-10-25 19:26:31 -07001155 struct datapath *dp;
Jesse Grossccb13522011-10-25 19:26:31 -07001156
Pravin B Shelard57170b2013-07-30 15:39:39 -07001157 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001158 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001159 if (!dp) {
Pravin B Shelard57170b2013-07-30 15:39:39 -07001160 rcu_read_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001161 return -ENODEV;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001162 }
Jesse Grossccb13522011-10-25 19:26:31 -07001163
Pravin B Shelarb637e492013-10-04 00:14:23 -07001164 ti = rcu_dereference(dp->table.ti);
Jesse Grossccb13522011-10-25 19:26:31 -07001165 for (;;) {
1166 struct sw_flow *flow;
1167 u32 bucket, obj;
1168
1169 bucket = cb->args[0];
1170 obj = cb->args[1];
Pravin B Shelarb637e492013-10-04 00:14:23 -07001171 flow = ovs_flow_tbl_dump_next(ti, &bucket, &obj);
Jesse Grossccb13522011-10-25 19:26:31 -07001172 if (!flow)
1173 break;
1174
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -07001175 if (ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex, skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001176 NETLINK_CB(cb->skb).portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001177 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1178 OVS_FLOW_CMD_NEW) < 0)
1179 break;
1180
1181 cb->args[0] = bucket;
1182 cb->args[1] = obj;
1183 }
Pravin B Shelard57170b2013-07-30 15:39:39 -07001184 rcu_read_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001185 return skb->len;
1186}
1187
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001188static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
1189 [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
1190 [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
1191 [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
1192};
1193
stephen hemminger48e48a72014-07-16 11:25:52 -07001194static const struct genl_ops dp_flow_genl_ops[] = {
Jesse Grossccb13522011-10-25 19:26:31 -07001195 { .cmd = OVS_FLOW_CMD_NEW,
1196 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1197 .policy = flow_policy,
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001198 .doit = ovs_flow_cmd_new
Jesse Grossccb13522011-10-25 19:26:31 -07001199 },
1200 { .cmd = OVS_FLOW_CMD_DEL,
1201 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1202 .policy = flow_policy,
1203 .doit = ovs_flow_cmd_del
1204 },
1205 { .cmd = OVS_FLOW_CMD_GET,
1206 .flags = 0, /* OK for unprivileged users. */
1207 .policy = flow_policy,
1208 .doit = ovs_flow_cmd_get,
1209 .dumpit = ovs_flow_cmd_dump
1210 },
1211 { .cmd = OVS_FLOW_CMD_SET,
1212 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1213 .policy = flow_policy,
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001214 .doit = ovs_flow_cmd_set,
Jesse Grossccb13522011-10-25 19:26:31 -07001215 },
1216};
1217
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001218static struct genl_family dp_flow_genl_family = {
Jesse Grossccb13522011-10-25 19:26:31 -07001219 .id = GENL_ID_GENERATE,
1220 .hdrsize = sizeof(struct ovs_header),
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001221 .name = OVS_FLOW_FAMILY,
1222 .version = OVS_FLOW_VERSION,
1223 .maxattr = OVS_FLOW_ATTR_MAX,
Pravin B Shelar3a4e0d62013-04-23 07:48:48 +00001224 .netnsok = true,
1225 .parallel_ops = true,
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001226 .ops = dp_flow_genl_ops,
1227 .n_ops = ARRAY_SIZE(dp_flow_genl_ops),
1228 .mcgrps = &ovs_dp_flow_multicast_group,
1229 .n_mcgrps = 1,
Jesse Grossccb13522011-10-25 19:26:31 -07001230};
1231
Thomas Grafc3ff8cf2013-03-29 14:46:49 +01001232static size_t ovs_dp_cmd_msg_size(void)
1233{
1234 size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
1235
1236 msgsize += nla_total_size(IFNAMSIZ);
1237 msgsize += nla_total_size(sizeof(struct ovs_dp_stats));
Andy Zhou1bd71162013-10-22 10:42:46 -07001238 msgsize += nla_total_size(sizeof(struct ovs_dp_megaflow_stats));
Daniele Di Proietto45fb9c32014-01-23 10:47:35 -08001239 msgsize += nla_total_size(sizeof(u32)); /* OVS_DP_ATTR_USER_FEATURES */
Thomas Grafc3ff8cf2013-03-29 14:46:49 +01001240
1241 return msgsize;
1242}
1243
Jarno Rajahalmebb6f9a72014-05-05 11:32:17 -07001244/* Called with ovs_mutex or RCU read lock. */
Jesse Grossccb13522011-10-25 19:26:31 -07001245static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001246 u32 portid, u32 seq, u32 flags, u8 cmd)
Jesse Grossccb13522011-10-25 19:26:31 -07001247{
1248 struct ovs_header *ovs_header;
1249 struct ovs_dp_stats dp_stats;
Andy Zhou1bd71162013-10-22 10:42:46 -07001250 struct ovs_dp_megaflow_stats dp_megaflow_stats;
Jesse Grossccb13522011-10-25 19:26:31 -07001251 int err;
1252
Eric W. Biederman15e47302012-09-07 20:12:54 +00001253 ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
Jesse Grossccb13522011-10-25 19:26:31 -07001254 flags, cmd);
1255 if (!ovs_header)
1256 goto error;
1257
1258 ovs_header->dp_ifindex = get_dpifindex(dp);
1259
Jesse Grossccb13522011-10-25 19:26:31 -07001260 err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
Jesse Grossccb13522011-10-25 19:26:31 -07001261 if (err)
1262 goto nla_put_failure;
1263
Andy Zhou1bd71162013-10-22 10:42:46 -07001264 get_dp_stats(dp, &dp_stats, &dp_megaflow_stats);
1265 if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats),
1266 &dp_stats))
1267 goto nla_put_failure;
1268
1269 if (nla_put(skb, OVS_DP_ATTR_MEGAFLOW_STATS,
1270 sizeof(struct ovs_dp_megaflow_stats),
1271 &dp_megaflow_stats))
David S. Miller028d6a62012-03-29 23:20:48 -04001272 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001273
Thomas Graf43d4be92013-12-13 15:22:18 +01001274 if (nla_put_u32(skb, OVS_DP_ATTR_USER_FEATURES, dp->user_features))
1275 goto nla_put_failure;
1276
Jesse Grossccb13522011-10-25 19:26:31 -07001277 return genlmsg_end(skb, ovs_header);
1278
1279nla_put_failure:
1280 genlmsg_cancel(skb, ovs_header);
1281error:
1282 return -EMSGSIZE;
1283}
1284
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001285static struct sk_buff *ovs_dp_cmd_alloc_info(struct genl_info *info)
Jesse Grossccb13522011-10-25 19:26:31 -07001286{
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001287 return genlmsg_new_unicast(ovs_dp_cmd_msg_size(), info, GFP_KERNEL);
Jesse Grossccb13522011-10-25 19:26:31 -07001288}
1289
Jarno Rajahalmebb6f9a72014-05-05 11:32:17 -07001290/* Called with rcu_read_lock or ovs_mutex. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001291static struct datapath *lookup_datapath(struct net *net,
1292 struct ovs_header *ovs_header,
Jesse Grossccb13522011-10-25 19:26:31 -07001293 struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1294{
1295 struct datapath *dp;
1296
1297 if (!a[OVS_DP_ATTR_NAME])
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001298 dp = get_dp(net, ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001299 else {
1300 struct vport *vport;
1301
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001302 vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
Jesse Grossccb13522011-10-25 19:26:31 -07001303 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
Jesse Grossccb13522011-10-25 19:26:31 -07001304 }
1305 return dp ? dp : ERR_PTR(-ENODEV);
1306}
1307
Thomas Graf44da5ae2013-12-13 15:22:19 +01001308static void ovs_dp_reset_user_features(struct sk_buff *skb, struct genl_info *info)
1309{
1310 struct datapath *dp;
1311
1312 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
Jiri Pirko3c7eacf2014-02-14 11:42:36 +01001313 if (IS_ERR(dp))
Thomas Graf44da5ae2013-12-13 15:22:19 +01001314 return;
1315
1316 WARN(dp->user_features, "Dropping previously announced user features\n");
1317 dp->user_features = 0;
1318}
1319
Thomas Graf43d4be92013-12-13 15:22:18 +01001320static void ovs_dp_change(struct datapath *dp, struct nlattr **a)
1321{
1322 if (a[OVS_DP_ATTR_USER_FEATURES])
1323 dp->user_features = nla_get_u32(a[OVS_DP_ATTR_USER_FEATURES]);
1324}
1325
Jesse Grossccb13522011-10-25 19:26:31 -07001326static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1327{
1328 struct nlattr **a = info->attrs;
1329 struct vport_parms parms;
1330 struct sk_buff *reply;
1331 struct datapath *dp;
1332 struct vport *vport;
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001333 struct ovs_net *ovs_net;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001334 int err, i;
Jesse Grossccb13522011-10-25 19:26:31 -07001335
1336 err = -EINVAL;
1337 if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1338 goto err;
1339
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001340 reply = ovs_dp_cmd_alloc_info(info);
1341 if (!reply)
1342 return -ENOMEM;
Jesse Grossccb13522011-10-25 19:26:31 -07001343
1344 err = -ENOMEM;
1345 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1346 if (dp == NULL)
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001347 goto err_free_reply;
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001348
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001349 ovs_dp_set_net(dp, hold_net(sock_net(skb->sk)));
Jesse Grossccb13522011-10-25 19:26:31 -07001350
1351 /* Allocate table. */
Pravin B Shelarb637e492013-10-04 00:14:23 -07001352 err = ovs_flow_tbl_init(&dp->table);
1353 if (err)
Jesse Grossccb13522011-10-25 19:26:31 -07001354 goto err_free_dp;
1355
WANG Cong1c213bd2014-02-13 11:46:28 -08001356 dp->stats_percpu = netdev_alloc_pcpu_stats(struct dp_stats_percpu);
Jesse Grossccb13522011-10-25 19:26:31 -07001357 if (!dp->stats_percpu) {
1358 err = -ENOMEM;
1359 goto err_destroy_table;
1360 }
1361
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001362 dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
Pravin B Shelare6445712013-10-03 18:16:47 -07001363 GFP_KERNEL);
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001364 if (!dp->ports) {
1365 err = -ENOMEM;
1366 goto err_destroy_percpu;
1367 }
1368
1369 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1370 INIT_HLIST_HEAD(&dp->ports[i]);
1371
Jesse Grossccb13522011-10-25 19:26:31 -07001372 /* Set up our datapath device. */
1373 parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1374 parms.type = OVS_VPORT_TYPE_INTERNAL;
1375 parms.options = NULL;
1376 parms.dp = dp;
1377 parms.port_no = OVSP_LOCAL;
Alex Wang5cd667b2014-07-17 15:14:13 -07001378 parms.upcall_portids = a[OVS_DP_ATTR_UPCALL_PID];
Jesse Grossccb13522011-10-25 19:26:31 -07001379
Thomas Graf43d4be92013-12-13 15:22:18 +01001380 ovs_dp_change(dp, a);
1381
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001382 /* So far only local changes have been made, now need the lock. */
1383 ovs_lock();
1384
Jesse Grossccb13522011-10-25 19:26:31 -07001385 vport = new_vport(&parms);
1386 if (IS_ERR(vport)) {
1387 err = PTR_ERR(vport);
1388 if (err == -EBUSY)
1389 err = -EEXIST;
1390
Thomas Graf44da5ae2013-12-13 15:22:19 +01001391 if (err == -EEXIST) {
1392 /* An outdated user space instance that does not understand
1393 * the concept of user_features has attempted to create a new
1394 * datapath and is likely to reuse it. Drop all user features.
1395 */
1396 if (info->genlhdr->version < OVS_DP_VER_FEATURES)
1397 ovs_dp_reset_user_features(skb, info);
1398 }
1399
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001400 goto err_destroy_ports_array;
Jesse Grossccb13522011-10-25 19:26:31 -07001401 }
1402
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001403 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1404 info->snd_seq, 0, OVS_DP_CMD_NEW);
1405 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001406
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001407 ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001408 list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001409
1410 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001411
Johannes Berg2a94fe42013-11-19 15:19:39 +01001412 ovs_notify(&dp_datapath_genl_family, reply, info);
Jesse Grossccb13522011-10-25 19:26:31 -07001413 return 0;
1414
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001415err_destroy_ports_array:
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001416 ovs_unlock();
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001417 kfree(dp->ports);
Jesse Grossccb13522011-10-25 19:26:31 -07001418err_destroy_percpu:
1419 free_percpu(dp->stats_percpu);
1420err_destroy_table:
Andy Zhoue80857c2014-01-21 09:31:04 -08001421 ovs_flow_tbl_destroy(&dp->table, false);
Jesse Grossccb13522011-10-25 19:26:31 -07001422err_free_dp:
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001423 release_net(ovs_dp_get_net(dp));
Jesse Grossccb13522011-10-25 19:26:31 -07001424 kfree(dp);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001425err_free_reply:
1426 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001427err:
1428 return err;
1429}
1430
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001431/* Called with ovs_mutex. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001432static void __dp_destroy(struct datapath *dp)
Jesse Grossccb13522011-10-25 19:26:31 -07001433{
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001434 int i;
Jesse Grossccb13522011-10-25 19:26:31 -07001435
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001436 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1437 struct vport *vport;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001438 struct hlist_node *n;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001439
Sasha Levinb67bfe02013-02-27 17:06:00 -08001440 hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001441 if (vport->port_no != OVSP_LOCAL)
1442 ovs_dp_detach_port(vport);
1443 }
Jesse Grossccb13522011-10-25 19:26:31 -07001444
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001445 list_del_rcu(&dp->list_node);
Jesse Grossccb13522011-10-25 19:26:31 -07001446
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001447 /* OVSP_LOCAL is datapath internal port. We need to make sure that
Andy Zhoue80857c2014-01-21 09:31:04 -08001448 * all ports in datapath are destroyed first before freeing datapath.
Jesse Grossccb13522011-10-25 19:26:31 -07001449 */
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001450 ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
Jesse Grossccb13522011-10-25 19:26:31 -07001451
Andy Zhoue80857c2014-01-21 09:31:04 -08001452 /* RCU destroy the flow table */
1453 ovs_flow_tbl_destroy(&dp->table, true);
1454
Jesse Grossccb13522011-10-25 19:26:31 -07001455 call_rcu(&dp->rcu, destroy_dp_rcu);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001456}
1457
1458static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1459{
1460 struct sk_buff *reply;
1461 struct datapath *dp;
1462 int err;
1463
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001464 reply = ovs_dp_cmd_alloc_info(info);
1465 if (!reply)
1466 return -ENOMEM;
1467
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001468 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001469 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1470 err = PTR_ERR(dp);
1471 if (IS_ERR(dp))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001472 goto err_unlock_free;
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001473
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001474 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1475 info->snd_seq, 0, OVS_DP_CMD_DEL);
1476 BUG_ON(err < 0);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001477
1478 __dp_destroy(dp);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001479 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001480
Johannes Berg2a94fe42013-11-19 15:19:39 +01001481 ovs_notify(&dp_datapath_genl_family, reply, info);
Jesse Grossccb13522011-10-25 19:26:31 -07001482
1483 return 0;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001484
1485err_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001486 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001487 kfree_skb(reply);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001488 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001489}
1490
1491static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1492{
1493 struct sk_buff *reply;
1494 struct datapath *dp;
1495 int err;
1496
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001497 reply = ovs_dp_cmd_alloc_info(info);
1498 if (!reply)
1499 return -ENOMEM;
1500
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001501 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001502 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001503 err = PTR_ERR(dp);
Jesse Grossccb13522011-10-25 19:26:31 -07001504 if (IS_ERR(dp))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001505 goto err_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001506
Thomas Graf43d4be92013-12-13 15:22:18 +01001507 ovs_dp_change(dp, info->attrs);
1508
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001509 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1510 info->snd_seq, 0, OVS_DP_CMD_NEW);
1511 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001512
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001513 ovs_unlock();
Johannes Berg2a94fe42013-11-19 15:19:39 +01001514 ovs_notify(&dp_datapath_genl_family, reply, info);
Jesse Grossccb13522011-10-25 19:26:31 -07001515
1516 return 0;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001517
1518err_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001519 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001520 kfree_skb(reply);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001521 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001522}
1523
1524static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1525{
1526 struct sk_buff *reply;
1527 struct datapath *dp;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001528 int err;
Jesse Grossccb13522011-10-25 19:26:31 -07001529
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001530 reply = ovs_dp_cmd_alloc_info(info);
1531 if (!reply)
1532 return -ENOMEM;
1533
1534 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001535 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001536 if (IS_ERR(dp)) {
1537 err = PTR_ERR(dp);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001538 goto err_unlock_free;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001539 }
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001540 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1541 info->snd_seq, 0, OVS_DP_CMD_NEW);
1542 BUG_ON(err < 0);
1543 rcu_read_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001544
Jesse Grossccb13522011-10-25 19:26:31 -07001545 return genlmsg_reply(reply, info);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001546
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001547err_unlock_free:
1548 rcu_read_unlock();
1549 kfree_skb(reply);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001550 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001551}
1552
1553static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1554{
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001555 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
Jesse Grossccb13522011-10-25 19:26:31 -07001556 struct datapath *dp;
1557 int skip = cb->args[0];
1558 int i = 0;
1559
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001560 rcu_read_lock();
1561 list_for_each_entry_rcu(dp, &ovs_net->dps, list_node) {
Ben Pfaff77676fd2012-01-17 13:33:39 +00001562 if (i >= skip &&
Eric W. Biederman15e47302012-09-07 20:12:54 +00001563 ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001564 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1565 OVS_DP_CMD_NEW) < 0)
1566 break;
1567 i++;
1568 }
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001569 rcu_read_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001570
1571 cb->args[0] = i;
1572
1573 return skb->len;
1574}
1575
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001576static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1577 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1578 [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1579 [OVS_DP_ATTR_USER_FEATURES] = { .type = NLA_U32 },
1580};
1581
stephen hemminger48e48a72014-07-16 11:25:52 -07001582static const struct genl_ops dp_datapath_genl_ops[] = {
Jesse Grossccb13522011-10-25 19:26:31 -07001583 { .cmd = OVS_DP_CMD_NEW,
1584 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1585 .policy = datapath_policy,
1586 .doit = ovs_dp_cmd_new
1587 },
1588 { .cmd = OVS_DP_CMD_DEL,
1589 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1590 .policy = datapath_policy,
1591 .doit = ovs_dp_cmd_del
1592 },
1593 { .cmd = OVS_DP_CMD_GET,
1594 .flags = 0, /* OK for unprivileged users. */
1595 .policy = datapath_policy,
1596 .doit = ovs_dp_cmd_get,
1597 .dumpit = ovs_dp_cmd_dump
1598 },
1599 { .cmd = OVS_DP_CMD_SET,
1600 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1601 .policy = datapath_policy,
1602 .doit = ovs_dp_cmd_set,
1603 },
1604};
1605
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001606static struct genl_family dp_datapath_genl_family = {
Jesse Grossccb13522011-10-25 19:26:31 -07001607 .id = GENL_ID_GENERATE,
1608 .hdrsize = sizeof(struct ovs_header),
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001609 .name = OVS_DATAPATH_FAMILY,
1610 .version = OVS_DATAPATH_VERSION,
1611 .maxattr = OVS_DP_ATTR_MAX,
Pravin B Shelar3a4e0d62013-04-23 07:48:48 +00001612 .netnsok = true,
1613 .parallel_ops = true,
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001614 .ops = dp_datapath_genl_ops,
1615 .n_ops = ARRAY_SIZE(dp_datapath_genl_ops),
1616 .mcgrps = &ovs_dp_datapath_multicast_group,
1617 .n_mcgrps = 1,
Jesse Grossccb13522011-10-25 19:26:31 -07001618};
1619
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001620/* Called with ovs_mutex or RCU read lock. */
Jesse Grossccb13522011-10-25 19:26:31 -07001621static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001622 u32 portid, u32 seq, u32 flags, u8 cmd)
Jesse Grossccb13522011-10-25 19:26:31 -07001623{
1624 struct ovs_header *ovs_header;
1625 struct ovs_vport_stats vport_stats;
1626 int err;
1627
Eric W. Biederman15e47302012-09-07 20:12:54 +00001628 ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
Jesse Grossccb13522011-10-25 19:26:31 -07001629 flags, cmd);
1630 if (!ovs_header)
1631 return -EMSGSIZE;
1632
1633 ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1634
David S. Miller028d6a62012-03-29 23:20:48 -04001635 if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
1636 nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
Alex Wang5cd667b2014-07-17 15:14:13 -07001637 nla_put_string(skb, OVS_VPORT_ATTR_NAME,
1638 vport->ops->get_name(vport)))
David S. Miller028d6a62012-03-29 23:20:48 -04001639 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001640
1641 ovs_vport_get_stats(vport, &vport_stats);
David S. Miller028d6a62012-03-29 23:20:48 -04001642 if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
1643 &vport_stats))
1644 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001645
Alex Wang5cd667b2014-07-17 15:14:13 -07001646 if (ovs_vport_get_upcall_portids(vport, skb))
1647 goto nla_put_failure;
1648
Jesse Grossccb13522011-10-25 19:26:31 -07001649 err = ovs_vport_get_options(vport, skb);
1650 if (err == -EMSGSIZE)
1651 goto error;
1652
1653 return genlmsg_end(skb, ovs_header);
1654
1655nla_put_failure:
1656 err = -EMSGSIZE;
1657error:
1658 genlmsg_cancel(skb, ovs_header);
1659 return err;
1660}
1661
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001662static struct sk_buff *ovs_vport_cmd_alloc_info(void)
1663{
1664 return nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1665}
1666
1667/* Called with ovs_mutex, only via ovs_dp_notify_wq(). */
Eric W. Biederman15e47302012-09-07 20:12:54 +00001668struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001669 u32 seq, u8 cmd)
1670{
1671 struct sk_buff *skb;
1672 int retval;
1673
1674 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1675 if (!skb)
1676 return ERR_PTR(-ENOMEM);
1677
Eric W. Biederman15e47302012-09-07 20:12:54 +00001678 retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
Jesse Grossa9341512013-03-26 15:48:38 -07001679 BUG_ON(retval < 0);
1680
Jesse Grossccb13522011-10-25 19:26:31 -07001681 return skb;
1682}
1683
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001684/* Called with ovs_mutex or RCU read lock. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001685static struct vport *lookup_vport(struct net *net,
1686 struct ovs_header *ovs_header,
Jesse Grossccb13522011-10-25 19:26:31 -07001687 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1688{
1689 struct datapath *dp;
1690 struct vport *vport;
1691
1692 if (a[OVS_VPORT_ATTR_NAME]) {
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001693 vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
Jesse Grossccb13522011-10-25 19:26:31 -07001694 if (!vport)
1695 return ERR_PTR(-ENODEV);
Ben Pfaff651a68e2012-03-06 15:04:04 -08001696 if (ovs_header->dp_ifindex &&
1697 ovs_header->dp_ifindex != get_dpifindex(vport->dp))
1698 return ERR_PTR(-ENODEV);
Jesse Grossccb13522011-10-25 19:26:31 -07001699 return vport;
1700 } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
1701 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1702
1703 if (port_no >= DP_MAX_PORTS)
1704 return ERR_PTR(-EFBIG);
1705
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001706 dp = get_dp(net, ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001707 if (!dp)
1708 return ERR_PTR(-ENODEV);
1709
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001710 vport = ovs_vport_ovsl_rcu(dp, port_no);
Jesse Grossccb13522011-10-25 19:26:31 -07001711 if (!vport)
Jarno Rajahalme14408db2013-01-09 14:27:35 -08001712 return ERR_PTR(-ENODEV);
Jesse Grossccb13522011-10-25 19:26:31 -07001713 return vport;
1714 } else
1715 return ERR_PTR(-EINVAL);
1716}
1717
1718static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
1719{
1720 struct nlattr **a = info->attrs;
1721 struct ovs_header *ovs_header = info->userhdr;
1722 struct vport_parms parms;
1723 struct sk_buff *reply;
1724 struct vport *vport;
1725 struct datapath *dp;
1726 u32 port_no;
1727 int err;
1728
Jesse Grossccb13522011-10-25 19:26:31 -07001729 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
1730 !a[OVS_VPORT_ATTR_UPCALL_PID])
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001731 return -EINVAL;
1732
1733 port_no = a[OVS_VPORT_ATTR_PORT_NO]
1734 ? nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]) : 0;
1735 if (port_no >= DP_MAX_PORTS)
1736 return -EFBIG;
1737
1738 reply = ovs_vport_cmd_alloc_info();
1739 if (!reply)
1740 return -ENOMEM;
Jesse Grossccb13522011-10-25 19:26:31 -07001741
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001742 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001743 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001744 err = -ENODEV;
1745 if (!dp)
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001746 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001747
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001748 if (port_no) {
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001749 vport = ovs_vport_ovsl(dp, port_no);
Jesse Grossccb13522011-10-25 19:26:31 -07001750 err = -EBUSY;
1751 if (vport)
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001752 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001753 } else {
1754 for (port_no = 1; ; port_no++) {
1755 if (port_no >= DP_MAX_PORTS) {
1756 err = -EFBIG;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001757 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001758 }
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001759 vport = ovs_vport_ovsl(dp, port_no);
Jesse Grossccb13522011-10-25 19:26:31 -07001760 if (!vport)
1761 break;
1762 }
1763 }
1764
1765 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
1766 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1767 parms.options = a[OVS_VPORT_ATTR_OPTIONS];
1768 parms.dp = dp;
1769 parms.port_no = port_no;
Alex Wang5cd667b2014-07-17 15:14:13 -07001770 parms.upcall_portids = a[OVS_VPORT_ATTR_UPCALL_PID];
Jesse Grossccb13522011-10-25 19:26:31 -07001771
1772 vport = new_vport(&parms);
1773 err = PTR_ERR(vport);
1774 if (IS_ERR(vport))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001775 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001776
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001777 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1778 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1779 BUG_ON(err < 0);
1780 ovs_unlock();
Thomas Grafed661182013-03-29 14:46:50 +01001781
Johannes Berg2a94fe42013-11-19 15:19:39 +01001782 ovs_notify(&dp_vport_genl_family, reply, info);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001783 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001784
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001785exit_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001786 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001787 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001788 return err;
1789}
1790
1791static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
1792{
1793 struct nlattr **a = info->attrs;
1794 struct sk_buff *reply;
1795 struct vport *vport;
1796 int err;
1797
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001798 reply = ovs_vport_cmd_alloc_info();
1799 if (!reply)
1800 return -ENOMEM;
1801
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001802 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001803 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
Jesse Grossccb13522011-10-25 19:26:31 -07001804 err = PTR_ERR(vport);
1805 if (IS_ERR(vport))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001806 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001807
Jesse Grossccb13522011-10-25 19:26:31 -07001808 if (a[OVS_VPORT_ATTR_TYPE] &&
Jesse Grossf44f3402013-05-13 08:15:26 -07001809 nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
Jesse Grossccb13522011-10-25 19:26:31 -07001810 err = -EINVAL;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001811 goto exit_unlock_free;
Jesse Grossa9341512013-03-26 15:48:38 -07001812 }
1813
Jesse Grossf44f3402013-05-13 08:15:26 -07001814 if (a[OVS_VPORT_ATTR_OPTIONS]) {
Jesse Grossccb13522011-10-25 19:26:31 -07001815 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
Jesse Grossf44f3402013-05-13 08:15:26 -07001816 if (err)
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001817 goto exit_unlock_free;
Jesse Grossf44f3402013-05-13 08:15:26 -07001818 }
Jesse Grossa9341512013-03-26 15:48:38 -07001819
Alex Wang5cd667b2014-07-17 15:14:13 -07001820
1821 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
1822 struct nlattr *ids = a[OVS_VPORT_ATTR_UPCALL_PID];
1823
1824 err = ovs_vport_set_upcall_portids(vport, ids);
1825 if (err)
1826 goto exit_unlock_free;
1827 }
Jesse Grossccb13522011-10-25 19:26:31 -07001828
Jesse Grossa9341512013-03-26 15:48:38 -07001829 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1830 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1831 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001832
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001833 ovs_unlock();
Johannes Berg2a94fe42013-11-19 15:19:39 +01001834 ovs_notify(&dp_vport_genl_family, reply, info);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001835 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001836
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001837exit_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001838 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001839 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001840 return err;
1841}
1842
1843static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
1844{
1845 struct nlattr **a = info->attrs;
1846 struct sk_buff *reply;
1847 struct vport *vport;
1848 int err;
1849
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001850 reply = ovs_vport_cmd_alloc_info();
1851 if (!reply)
1852 return -ENOMEM;
1853
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001854 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001855 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
Jesse Grossccb13522011-10-25 19:26:31 -07001856 err = PTR_ERR(vport);
1857 if (IS_ERR(vport))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001858 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001859
1860 if (vport->port_no == OVSP_LOCAL) {
1861 err = -EINVAL;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001862 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001863 }
1864
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001865 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1866 info->snd_seq, 0, OVS_VPORT_CMD_DEL);
1867 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001868 ovs_dp_detach_port(vport);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001869 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001870
Johannes Berg2a94fe42013-11-19 15:19:39 +01001871 ovs_notify(&dp_vport_genl_family, reply, info);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001872 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001873
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001874exit_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001875 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001876 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001877 return err;
1878}
1879
1880static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
1881{
1882 struct nlattr **a = info->attrs;
1883 struct ovs_header *ovs_header = info->userhdr;
1884 struct sk_buff *reply;
1885 struct vport *vport;
1886 int err;
1887
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001888 reply = ovs_vport_cmd_alloc_info();
1889 if (!reply)
1890 return -ENOMEM;
1891
Jesse Grossccb13522011-10-25 19:26:31 -07001892 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001893 vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
Jesse Grossccb13522011-10-25 19:26:31 -07001894 err = PTR_ERR(vport);
1895 if (IS_ERR(vport))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001896 goto exit_unlock_free;
1897 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1898 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1899 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001900 rcu_read_unlock();
1901
1902 return genlmsg_reply(reply, info);
1903
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001904exit_unlock_free:
Jesse Grossccb13522011-10-25 19:26:31 -07001905 rcu_read_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001906 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001907 return err;
1908}
1909
1910static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1911{
1912 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1913 struct datapath *dp;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001914 int bucket = cb->args[0], skip = cb->args[1];
1915 int i, j = 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001916
Jesse Grossccb13522011-10-25 19:26:31 -07001917 rcu_read_lock();
Jarno Rajahalme42ee19e2014-02-15 17:42:29 -08001918 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1919 if (!dp) {
1920 rcu_read_unlock();
1921 return -ENODEV;
1922 }
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001923 for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
Jesse Grossccb13522011-10-25 19:26:31 -07001924 struct vport *vport;
1925
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001926 j = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001927 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001928 if (j >= skip &&
1929 ovs_vport_cmd_fill_info(vport, skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001930 NETLINK_CB(cb->skb).portid,
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001931 cb->nlh->nlmsg_seq,
1932 NLM_F_MULTI,
1933 OVS_VPORT_CMD_NEW) < 0)
1934 goto out;
Jesse Grossccb13522011-10-25 19:26:31 -07001935
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001936 j++;
1937 }
1938 skip = 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001939 }
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001940out:
Jesse Grossccb13522011-10-25 19:26:31 -07001941 rcu_read_unlock();
1942
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001943 cb->args[0] = i;
1944 cb->args[1] = j;
Jesse Grossccb13522011-10-25 19:26:31 -07001945
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001946 return skb->len;
Jesse Grossccb13522011-10-25 19:26:31 -07001947}
1948
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001949static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
1950 [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1951 [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
1952 [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
1953 [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
1954 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1955 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
1956};
1957
stephen hemminger48e48a72014-07-16 11:25:52 -07001958static const struct genl_ops dp_vport_genl_ops[] = {
Jesse Grossccb13522011-10-25 19:26:31 -07001959 { .cmd = OVS_VPORT_CMD_NEW,
1960 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1961 .policy = vport_policy,
1962 .doit = ovs_vport_cmd_new
1963 },
1964 { .cmd = OVS_VPORT_CMD_DEL,
1965 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1966 .policy = vport_policy,
1967 .doit = ovs_vport_cmd_del
1968 },
1969 { .cmd = OVS_VPORT_CMD_GET,
1970 .flags = 0, /* OK for unprivileged users. */
1971 .policy = vport_policy,
1972 .doit = ovs_vport_cmd_get,
1973 .dumpit = ovs_vport_cmd_dump
1974 },
1975 { .cmd = OVS_VPORT_CMD_SET,
1976 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1977 .policy = vport_policy,
1978 .doit = ovs_vport_cmd_set,
1979 },
1980};
1981
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001982struct genl_family dp_vport_genl_family = {
1983 .id = GENL_ID_GENERATE,
1984 .hdrsize = sizeof(struct ovs_header),
1985 .name = OVS_VPORT_FAMILY,
1986 .version = OVS_VPORT_VERSION,
1987 .maxattr = OVS_VPORT_ATTR_MAX,
1988 .netnsok = true,
1989 .parallel_ops = true,
1990 .ops = dp_vport_genl_ops,
1991 .n_ops = ARRAY_SIZE(dp_vport_genl_ops),
1992 .mcgrps = &ovs_dp_vport_multicast_group,
1993 .n_mcgrps = 1,
Jesse Grossccb13522011-10-25 19:26:31 -07001994};
1995
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001996static struct genl_family * const dp_genl_families[] = {
1997 &dp_datapath_genl_family,
1998 &dp_vport_genl_family,
1999 &dp_flow_genl_family,
2000 &dp_packet_genl_family,
Jesse Grossccb13522011-10-25 19:26:31 -07002001};
2002
2003static void dp_unregister_genl(int n_families)
2004{
2005 int i;
2006
2007 for (i = 0; i < n_families; i++)
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002008 genl_unregister_family(dp_genl_families[i]);
Jesse Grossccb13522011-10-25 19:26:31 -07002009}
2010
2011static int dp_register_genl(void)
2012{
Jesse Grossccb13522011-10-25 19:26:31 -07002013 int err;
2014 int i;
2015
Jesse Grossccb13522011-10-25 19:26:31 -07002016 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
Jesse Grossccb13522011-10-25 19:26:31 -07002017
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002018 err = genl_register_family(dp_genl_families[i]);
Jesse Grossccb13522011-10-25 19:26:31 -07002019 if (err)
2020 goto error;
Jesse Grossccb13522011-10-25 19:26:31 -07002021 }
2022
2023 return 0;
2024
2025error:
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002026 dp_unregister_genl(i);
Jesse Grossccb13522011-10-25 19:26:31 -07002027 return err;
2028}
2029
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002030static int __net_init ovs_init_net(struct net *net)
2031{
2032 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2033
2034 INIT_LIST_HEAD(&ovs_net->dps);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002035 INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002036 return 0;
2037}
2038
2039static void __net_exit ovs_exit_net(struct net *net)
2040{
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002041 struct datapath *dp, *dp_next;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002042 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002043
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002044 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002045 list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
2046 __dp_destroy(dp);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002047 ovs_unlock();
2048
2049 cancel_work_sync(&ovs_net->dp_notify_work);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002050}
2051
2052static struct pernet_operations ovs_net_ops = {
2053 .init = ovs_init_net,
2054 .exit = ovs_exit_net,
2055 .id = &ovs_net_id,
2056 .size = sizeof(struct ovs_net),
2057};
2058
Jesse Grossccb13522011-10-25 19:26:31 -07002059static int __init dp_init(void)
2060{
Jesse Grossccb13522011-10-25 19:26:31 -07002061 int err;
2062
YOSHIFUJI Hideaki / 吉藤英明3523b292013-01-09 07:19:55 +00002063 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
Jesse Grossccb13522011-10-25 19:26:31 -07002064
2065 pr_info("Open vSwitch switching datapath\n");
2066
Jiri Pirko5b9e7e12014-06-26 09:58:26 +02002067 err = ovs_internal_dev_rtnl_link_register();
Jesse Grossccb13522011-10-25 19:26:31 -07002068 if (err)
2069 goto error;
2070
Jiri Pirko5b9e7e12014-06-26 09:58:26 +02002071 err = ovs_flow_init();
2072 if (err)
2073 goto error_unreg_rtnl_link;
2074
Jesse Grossccb13522011-10-25 19:26:31 -07002075 err = ovs_vport_init();
2076 if (err)
2077 goto error_flow_exit;
2078
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002079 err = register_pernet_device(&ovs_net_ops);
Jesse Grossccb13522011-10-25 19:26:31 -07002080 if (err)
2081 goto error_vport_exit;
2082
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002083 err = register_netdevice_notifier(&ovs_dp_device_notifier);
2084 if (err)
2085 goto error_netns_exit;
2086
Jesse Grossccb13522011-10-25 19:26:31 -07002087 err = dp_register_genl();
2088 if (err < 0)
2089 goto error_unreg_notifier;
2090
Jesse Grossccb13522011-10-25 19:26:31 -07002091 return 0;
2092
2093error_unreg_notifier:
2094 unregister_netdevice_notifier(&ovs_dp_device_notifier);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002095error_netns_exit:
2096 unregister_pernet_device(&ovs_net_ops);
Jesse Grossccb13522011-10-25 19:26:31 -07002097error_vport_exit:
2098 ovs_vport_exit();
2099error_flow_exit:
2100 ovs_flow_exit();
Jiri Pirko5b9e7e12014-06-26 09:58:26 +02002101error_unreg_rtnl_link:
2102 ovs_internal_dev_rtnl_link_unregister();
Jesse Grossccb13522011-10-25 19:26:31 -07002103error:
2104 return err;
2105}
2106
2107static void dp_cleanup(void)
2108{
Jesse Grossccb13522011-10-25 19:26:31 -07002109 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
2110 unregister_netdevice_notifier(&ovs_dp_device_notifier);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002111 unregister_pernet_device(&ovs_net_ops);
2112 rcu_barrier();
Jesse Grossccb13522011-10-25 19:26:31 -07002113 ovs_vport_exit();
2114 ovs_flow_exit();
Jiri Pirko5b9e7e12014-06-26 09:58:26 +02002115 ovs_internal_dev_rtnl_link_unregister();
Jesse Grossccb13522011-10-25 19:26:31 -07002116}
2117
2118module_init(dp_init);
2119module_exit(dp_cleanup);
2120
2121MODULE_DESCRIPTION("Open vSwitch switching datapath");
2122MODULE_LICENSE("GPL");