blob: 0cce8e60d5eda39be723599ade47ffad41b929ac [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 Grafed6611852013-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 Grafed6611852013-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. */
Pravin B Shelar83c8df22014-09-15 19:20:31 -0700240void ovs_dp_process_received_packet(struct sk_buff *skb)
Jesse Grossccb13522011-10-25 19:26:31 -0700241{
Pravin B Shelar83c8df22014-09-15 19:20:31 -0700242 const struct vport *p = OVS_CB(skb)->input_vport;
Jesse Grossccb13522011-10-25 19:26:31 -0700243 struct datapath *dp = p->dp;
244 struct sw_flow *flow;
245 struct dp_stats_percpu *stats;
246 struct sw_flow_key key;
247 u64 *stats_counter;
Andy Zhou1bd71162013-10-22 10:42:46 -0700248 u32 n_mask_hit;
Jesse Grossccb13522011-10-25 19:26:31 -0700249 int error;
Jesse Grossccb13522011-10-25 19:26:31 -0700250
Shan Wei404f2f12012-11-13 09:52:25 +0800251 stats = this_cpu_ptr(dp->stats_percpu);
Jesse Grossccb13522011-10-25 19:26:31 -0700252
253 /* Extract flow from 'skb' into 'key'. */
Pravin B Shelar83c8df22014-09-15 19:20:31 -0700254 error = ovs_flow_key_extract(skb, &key);
Jesse Grossccb13522011-10-25 19:26:31 -0700255 if (unlikely(error)) {
256 kfree_skb(skb);
257 return;
258 }
259
260 /* Look up flow. */
Andy Zhou5bb50632013-11-25 10:42:46 -0800261 flow = ovs_flow_tbl_lookup_stats(&dp->table, &key, &n_mask_hit);
Jesse Grossccb13522011-10-25 19:26:31 -0700262 if (unlikely(!flow)) {
263 struct dp_upcall_info upcall;
264
265 upcall.cmd = OVS_PACKET_CMD_MISS;
266 upcall.key = &key;
267 upcall.userdata = NULL;
Alex Wang5cd667b2014-07-17 15:14:13 -0700268 upcall.portid = ovs_vport_find_upcall_portid(p, skb);
Li RongQingc5eba0b2014-09-03 17:43:45 +0800269 error = ovs_dp_upcall(dp, skb, &upcall);
270 if (unlikely(error))
271 kfree_skb(skb);
272 else
273 consume_skb(skb);
Jesse Grossccb13522011-10-25 19:26:31 -0700274 stats_counter = &stats->n_missed;
275 goto out;
276 }
277
278 OVS_CB(skb)->flow = flow;
279
Ben Pfaffad552002014-05-06 16:48:38 -0700280 ovs_flow_stats_update(OVS_CB(skb)->flow, key.tp.flags, skb);
Pravin B Shelar2ff3e4e2014-09-15 19:15:28 -0700281 ovs_execute_actions(dp, skb, &key);
Pravin B Shelare298e502013-10-29 17:22:21 -0700282 stats_counter = &stats->n_hit;
Jesse Grossccb13522011-10-25 19:26:31 -0700283
284out:
285 /* Update datapath statistics. */
WANG Congdf9d9fd2014-02-14 15:10:46 -0800286 u64_stats_update_begin(&stats->syncp);
Jesse Grossccb13522011-10-25 19:26:31 -0700287 (*stats_counter)++;
Andy Zhou1bd71162013-10-22 10:42:46 -0700288 stats->n_mask_hit += n_mask_hit;
WANG Congdf9d9fd2014-02-14 15:10:46 -0800289 u64_stats_update_end(&stats->syncp);
Jesse Grossccb13522011-10-25 19:26:31 -0700290}
291
Jesse Grossccb13522011-10-25 19:26:31 -0700292int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800293 const struct dp_upcall_info *upcall_info)
Jesse Grossccb13522011-10-25 19:26:31 -0700294{
295 struct dp_stats_percpu *stats;
Jesse Grossccb13522011-10-25 19:26:31 -0700296 int err;
297
Eric W. Biederman15e47302012-09-07 20:12:54 +0000298 if (upcall_info->portid == 0) {
Jesse Grossccb13522011-10-25 19:26:31 -0700299 err = -ENOTCONN;
300 goto err;
301 }
302
Jesse Grossccb13522011-10-25 19:26:31 -0700303 if (!skb_is_gso(skb))
Thomas Graf8055a892013-12-13 15:22:20 +0100304 err = queue_userspace_packet(dp, skb, upcall_info);
Jesse Grossccb13522011-10-25 19:26:31 -0700305 else
Thomas Graf8055a892013-12-13 15:22:20 +0100306 err = queue_gso_packets(dp, skb, upcall_info);
Jesse Grossccb13522011-10-25 19:26:31 -0700307 if (err)
308 goto err;
309
310 return 0;
311
312err:
Shan Wei404f2f12012-11-13 09:52:25 +0800313 stats = this_cpu_ptr(dp->stats_percpu);
Jesse Grossccb13522011-10-25 19:26:31 -0700314
WANG Congdf9d9fd2014-02-14 15:10:46 -0800315 u64_stats_update_begin(&stats->syncp);
Jesse Grossccb13522011-10-25 19:26:31 -0700316 stats->n_lost++;
WANG Congdf9d9fd2014-02-14 15:10:46 -0800317 u64_stats_update_end(&stats->syncp);
Jesse Grossccb13522011-10-25 19:26:31 -0700318
319 return err;
320}
321
Thomas Graf8055a892013-12-13 15:22:20 +0100322static int queue_gso_packets(struct datapath *dp, struct sk_buff *skb,
Jesse Grossccb13522011-10-25 19:26:31 -0700323 const struct dp_upcall_info *upcall_info)
324{
Ben Pfaffa1b5d0d2012-07-20 14:47:54 -0700325 unsigned short gso_type = skb_shinfo(skb)->gso_type;
Jesse Grossccb13522011-10-25 19:26:31 -0700326 struct dp_upcall_info later_info;
327 struct sw_flow_key later_key;
328 struct sk_buff *segs, *nskb;
329 int err;
330
Thomas Graf09c5e602013-12-13 15:22:22 +0100331 segs = __skb_gso_segment(skb, NETIF_F_SG, false);
Pravin B Shelar92e5dfc2012-07-20 14:46:29 -0700332 if (IS_ERR(segs))
333 return PTR_ERR(segs);
Jesse Grossccb13522011-10-25 19:26:31 -0700334
335 /* Queue all of the segments. */
336 skb = segs;
337 do {
Thomas Graf8055a892013-12-13 15:22:20 +0100338 err = queue_userspace_packet(dp, skb, upcall_info);
Jesse Grossccb13522011-10-25 19:26:31 -0700339 if (err)
340 break;
341
Ben Pfaffa1b5d0d2012-07-20 14:47:54 -0700342 if (skb == segs && gso_type & SKB_GSO_UDP) {
Jesse Grossccb13522011-10-25 19:26:31 -0700343 /* The initial flow key extracted by ovs_flow_extract()
344 * in this case is for a first fragment, so we need to
345 * properly mark later fragments.
346 */
347 later_key = *upcall_info->key;
348 later_key.ip.frag = OVS_FRAG_TYPE_LATER;
349
350 later_info = *upcall_info;
351 later_info.key = &later_key;
352 upcall_info = &later_info;
353 }
354 } while ((skb = skb->next));
355
356 /* Free all of the segments. */
357 skb = segs;
358 do {
359 nskb = skb->next;
360 if (err)
361 kfree_skb(skb);
362 else
363 consume_skb(skb);
364 } while ((skb = nskb));
365 return err;
366}
367
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100368static size_t key_attr_size(void)
369{
370 return nla_total_size(4) /* OVS_KEY_ATTR_PRIORITY */
Pravin B Shelar7d5437c2013-06-17 17:50:18 -0700371 + nla_total_size(0) /* OVS_KEY_ATTR_TUNNEL */
372 + nla_total_size(8) /* OVS_TUNNEL_KEY_ATTR_ID */
373 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_SRC */
374 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_DST */
375 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TOS */
376 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TTL */
377 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT */
378 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_CSUM */
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100379 + nla_total_size(4) /* OVS_KEY_ATTR_IN_PORT */
380 + nla_total_size(4) /* OVS_KEY_ATTR_SKB_MARK */
381 + nla_total_size(12) /* OVS_KEY_ATTR_ETHERNET */
382 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
383 + nla_total_size(4) /* OVS_KEY_ATTR_8021Q */
384 + nla_total_size(0) /* OVS_KEY_ATTR_ENCAP */
385 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
386 + nla_total_size(40) /* OVS_KEY_ATTR_IPV6 */
387 + nla_total_size(2) /* OVS_KEY_ATTR_ICMPV6 */
388 + nla_total_size(28); /* OVS_KEY_ATTR_ND */
389}
390
Thomas Grafbda56f12013-12-13 15:22:21 +0100391static size_t upcall_msg_size(const struct nlattr *userdata,
392 unsigned int hdrlen)
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100393{
394 size_t size = NLMSG_ALIGN(sizeof(struct ovs_header))
Thomas Grafbda56f12013-12-13 15:22:21 +0100395 + nla_total_size(hdrlen) /* OVS_PACKET_ATTR_PACKET */
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100396 + nla_total_size(key_attr_size()); /* OVS_PACKET_ATTR_KEY */
397
398 /* OVS_PACKET_ATTR_USERDATA */
399 if (userdata)
400 size += NLA_ALIGN(userdata->nla_len);
401
402 return size;
403}
404
Thomas Graf8055a892013-12-13 15:22:20 +0100405static int queue_userspace_packet(struct datapath *dp, struct sk_buff *skb,
Jesse Grossccb13522011-10-25 19:26:31 -0700406 const struct dp_upcall_info *upcall_info)
407{
408 struct ovs_header *upcall;
409 struct sk_buff *nskb = NULL;
Li RongQing4ee45ea2014-09-02 20:52:28 +0800410 struct sk_buff *user_skb = NULL; /* to be queued to userspace */
Jesse Grossccb13522011-10-25 19:26:31 -0700411 struct nlattr *nla;
Thomas Graf795449d2013-11-30 13:21:32 +0100412 struct genl_info info = {
Thomas Graf8055a892013-12-13 15:22:20 +0100413 .dst_sk = ovs_dp_get_net(dp)->genl_sock,
Thomas Graf795449d2013-11-30 13:21:32 +0100414 .snd_portid = upcall_info->portid,
415 };
416 size_t len;
Thomas Grafbda56f12013-12-13 15:22:21 +0100417 unsigned int hlen;
Thomas Graf8055a892013-12-13 15:22:20 +0100418 int err, dp_ifindex;
419
420 dp_ifindex = get_dpifindex(dp);
421 if (!dp_ifindex)
422 return -ENODEV;
Jesse Grossccb13522011-10-25 19:26:31 -0700423
424 if (vlan_tx_tag_present(skb)) {
425 nskb = skb_clone(skb, GFP_ATOMIC);
426 if (!nskb)
427 return -ENOMEM;
428
Patrick McHardy86a9bad2013-04-19 02:04:30 +0000429 nskb = __vlan_put_tag(nskb, nskb->vlan_proto, vlan_tx_tag_get(nskb));
Dan Carpenter8aa51d62012-05-13 08:44:18 +0000430 if (!nskb)
Jesse Grossccb13522011-10-25 19:26:31 -0700431 return -ENOMEM;
432
433 nskb->vlan_tci = 0;
434 skb = nskb;
435 }
436
437 if (nla_attr_size(skb->len) > USHRT_MAX) {
438 err = -EFBIG;
439 goto out;
440 }
441
Thomas Grafbda56f12013-12-13 15:22:21 +0100442 /* Complete checksum if needed */
443 if (skb->ip_summed == CHECKSUM_PARTIAL &&
444 (err = skb_checksum_help(skb)))
445 goto out;
446
447 /* Older versions of OVS user space enforce alignment of the last
448 * Netlink attribute to NLA_ALIGNTO which would require extensive
449 * padding logic. Only perform zerocopy if padding is not required.
450 */
451 if (dp->user_features & OVS_DP_F_UNALIGNED)
452 hlen = skb_zerocopy_headlen(skb);
453 else
454 hlen = skb->len;
455
456 len = upcall_msg_size(upcall_info->userdata, hlen);
Thomas Graf795449d2013-11-30 13:21:32 +0100457 user_skb = genlmsg_new_unicast(len, &info, GFP_ATOMIC);
Jesse Grossccb13522011-10-25 19:26:31 -0700458 if (!user_skb) {
459 err = -ENOMEM;
460 goto out;
461 }
462
463 upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
464 0, upcall_info->cmd);
465 upcall->dp_ifindex = dp_ifindex;
466
467 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
Andy Zhouf53e3832014-07-17 15:17:44 -0700468 err = ovs_nla_put_flow(upcall_info->key, upcall_info->key, user_skb);
469 BUG_ON(err);
Jesse Grossccb13522011-10-25 19:26:31 -0700470 nla_nest_end(user_skb, nla);
471
472 if (upcall_info->userdata)
Ben Pfaff44901082013-02-15 17:29:22 -0800473 __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA,
474 nla_len(upcall_info->userdata),
475 nla_data(upcall_info->userdata));
Jesse Grossccb13522011-10-25 19:26:31 -0700476
Thomas Grafbda56f12013-12-13 15:22:21 +0100477 /* Only reserve room for attribute header, packet data is added
478 * in skb_zerocopy() */
479 if (!(nla = nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, 0))) {
480 err = -ENOBUFS;
481 goto out;
482 }
483 nla->nla_len = nla_attr_size(skb->len);
Jesse Grossccb13522011-10-25 19:26:31 -0700484
Zoltan Kiss36d5fe62014-03-26 22:37:45 +0000485 err = skb_zerocopy(user_skb, skb, skb->len, hlen);
486 if (err)
487 goto out;
Jesse Grossccb13522011-10-25 19:26:31 -0700488
Thomas Grafaea0bb42014-01-14 16:27:49 +0000489 /* Pad OVS_PACKET_ATTR_PACKET if linear copy was performed */
490 if (!(dp->user_features & OVS_DP_F_UNALIGNED)) {
491 size_t plen = NLA_ALIGN(user_skb->len) - user_skb->len;
492
493 if (plen > 0)
494 memset(skb_put(user_skb, plen), 0, plen);
495 }
496
Thomas Grafbda56f12013-12-13 15:22:21 +0100497 ((struct nlmsghdr *) user_skb->data)->nlmsg_len = user_skb->len;
498
Thomas Graf8055a892013-12-13 15:22:20 +0100499 err = genlmsg_unicast(ovs_dp_get_net(dp), user_skb, upcall_info->portid);
Li RongQing4ee45ea2014-09-02 20:52:28 +0800500 user_skb = NULL;
Jesse Grossccb13522011-10-25 19:26:31 -0700501out:
Zoltan Kiss36d5fe62014-03-26 22:37:45 +0000502 if (err)
503 skb_tx_error(skb);
Li RongQing4ee45ea2014-09-02 20:52:28 +0800504 kfree_skb(user_skb);
Jesse Grossccb13522011-10-25 19:26:31 -0700505 kfree_skb(nskb);
506 return err;
507}
508
Jesse Grossccb13522011-10-25 19:26:31 -0700509static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
510{
511 struct ovs_header *ovs_header = info->userhdr;
512 struct nlattr **a = info->attrs;
513 struct sw_flow_actions *acts;
514 struct sk_buff *packet;
515 struct sw_flow *flow;
516 struct datapath *dp;
517 struct ethhdr *eth;
Pravin B Shelar83c8df22014-09-15 19:20:31 -0700518 struct vport *input_vport;
Jesse Grossccb13522011-10-25 19:26:31 -0700519 int len;
520 int err;
Jesse Grossccb13522011-10-25 19:26:31 -0700521
522 err = -EINVAL;
523 if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
Thomas Grafdded45fc2013-03-29 14:46:47 +0100524 !a[OVS_PACKET_ATTR_ACTIONS])
Jesse Grossccb13522011-10-25 19:26:31 -0700525 goto err;
526
527 len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
528 packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
529 err = -ENOMEM;
530 if (!packet)
531 goto err;
532 skb_reserve(packet, NET_IP_ALIGN);
533
Thomas Graf32686a92013-03-29 14:46:48 +0100534 nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
Jesse Grossccb13522011-10-25 19:26:31 -0700535
536 skb_reset_mac_header(packet);
537 eth = eth_hdr(packet);
538
539 /* Normally, setting the skb 'protocol' field would be handled by a
540 * call to eth_type_trans(), but it assumes there's a sending
541 * device, which we may not have. */
Simon Hormane5c5d222013-03-28 13:38:25 +0900542 if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN)
Jesse Grossccb13522011-10-25 19:26:31 -0700543 packet->protocol = eth->h_proto;
544 else
545 packet->protocol = htons(ETH_P_802_2);
546
547 /* Build an sw_flow for sending this packet. */
Jarno Rajahalme23dabf82014-03-27 12:35:23 -0700548 flow = ovs_flow_alloc();
Jesse Grossccb13522011-10-25 19:26:31 -0700549 err = PTR_ERR(flow);
550 if (IS_ERR(flow))
551 goto err_kfree_skb;
552
Pravin B Shelar83c8df22014-09-15 19:20:31 -0700553 err = ovs_flow_key_extract_userspace(a[OVS_PACKET_ATTR_KEY], packet,
554 &flow->key);
Jesse Grossccb13522011-10-25 19:26:31 -0700555 if (err)
556 goto err_flow_free;
557
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
Pravin B Shelar83c8df22014-09-15 19:20:31 -0700579 input_vport = ovs_vport_rcu(dp, flow->key.phy.in_port);
580 if (!input_vport)
581 input_vport = ovs_vport_rcu(dp, OVSP_LOCAL);
582
583 if (!input_vport)
584 goto err_unlock;
585
586 OVS_CB(packet)->input_vport = input_vport;
587
Jesse Grossccb13522011-10-25 19:26:31 -0700588 local_bh_disable();
Pravin B Shelar2ff3e4e2014-09-15 19:15:28 -0700589 err = ovs_execute_actions(dp, packet, &flow->key);
Jesse Grossccb13522011-10-25 19:26:31 -0700590 local_bh_enable();
591 rcu_read_unlock();
592
Andy Zhou03f0d912013-08-07 20:01:00 -0700593 ovs_flow_free(flow, false);
Jesse Grossccb13522011-10-25 19:26:31 -0700594 return err;
595
596err_unlock:
597 rcu_read_unlock();
598err_flow_free:
Andy Zhou03f0d912013-08-07 20:01:00 -0700599 ovs_flow_free(flow, false);
Jesse Grossccb13522011-10-25 19:26:31 -0700600err_kfree_skb:
601 kfree_skb(packet);
602err:
603 return err;
604}
605
606static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
Thomas Grafdded45fc2013-03-29 14:46:47 +0100607 [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
Jesse Grossccb13522011-10-25 19:26:31 -0700608 [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
609 [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
610};
611
Johannes Berg4534de82013-11-14 17:14:46 +0100612static const struct genl_ops dp_packet_genl_ops[] = {
Jesse Grossccb13522011-10-25 19:26:31 -0700613 { .cmd = OVS_PACKET_CMD_EXECUTE,
614 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
615 .policy = packet_policy,
616 .doit = ovs_packet_cmd_execute
617 }
618};
619
Pravin B Shelar0c200ef2014-05-06 16:44:50 -0700620static struct genl_family dp_packet_genl_family = {
621 .id = GENL_ID_GENERATE,
622 .hdrsize = sizeof(struct ovs_header),
623 .name = OVS_PACKET_FAMILY,
624 .version = OVS_PACKET_VERSION,
625 .maxattr = OVS_PACKET_ATTR_MAX,
626 .netnsok = true,
627 .parallel_ops = true,
628 .ops = dp_packet_genl_ops,
629 .n_ops = ARRAY_SIZE(dp_packet_genl_ops),
630};
631
Andy Zhou1bd71162013-10-22 10:42:46 -0700632static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats,
633 struct ovs_dp_megaflow_stats *mega_stats)
Jesse Grossccb13522011-10-25 19:26:31 -0700634{
635 int i;
Jesse Grossccb13522011-10-25 19:26:31 -0700636
Andy Zhou1bd71162013-10-22 10:42:46 -0700637 memset(mega_stats, 0, sizeof(*mega_stats));
638
Pravin B Shelarb637e492013-10-04 00:14:23 -0700639 stats->n_flows = ovs_flow_tbl_count(&dp->table);
Andy Zhou1bd71162013-10-22 10:42:46 -0700640 mega_stats->n_masks = ovs_flow_tbl_num_masks(&dp->table);
Jesse Grossccb13522011-10-25 19:26:31 -0700641
642 stats->n_hit = stats->n_missed = stats->n_lost = 0;
Andy Zhou1bd71162013-10-22 10:42:46 -0700643
Jesse Grossccb13522011-10-25 19:26:31 -0700644 for_each_possible_cpu(i) {
645 const struct dp_stats_percpu *percpu_stats;
646 struct dp_stats_percpu local_stats;
647 unsigned int start;
648
649 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
650
651 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700652 start = u64_stats_fetch_begin_irq(&percpu_stats->syncp);
Jesse Grossccb13522011-10-25 19:26:31 -0700653 local_stats = *percpu_stats;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700654 } while (u64_stats_fetch_retry_irq(&percpu_stats->syncp, start));
Jesse Grossccb13522011-10-25 19:26:31 -0700655
656 stats->n_hit += local_stats.n_hit;
657 stats->n_missed += local_stats.n_missed;
658 stats->n_lost += local_stats.n_lost;
Andy Zhou1bd71162013-10-22 10:42:46 -0700659 mega_stats->n_mask_hit += local_stats.n_mask_hit;
Jesse Grossccb13522011-10-25 19:26:31 -0700660 }
661}
662
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100663static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts)
664{
665 return NLMSG_ALIGN(sizeof(struct ovs_header))
666 + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_KEY */
Andy Zhou03f0d912013-08-07 20:01:00 -0700667 + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_MASK */
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100668 + nla_total_size(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
669 + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
670 + nla_total_size(8) /* OVS_FLOW_ATTR_USED */
671 + nla_total_size(acts->actions_len); /* OVS_FLOW_ATTR_ACTIONS */
672}
673
Jarno Rajahalmebb6f9a72014-05-05 11:32:17 -0700674/* Called with ovs_mutex or RCU read lock. */
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700675static int ovs_flow_cmd_fill_info(const struct sw_flow *flow, int dp_ifindex,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000676 struct sk_buff *skb, u32 portid,
Jesse Grossccb13522011-10-25 19:26:31 -0700677 u32 seq, u32 flags, u8 cmd)
678{
679 const int skb_orig_len = skb->len;
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700680 struct nlattr *start;
Jesse Grossccb13522011-10-25 19:26:31 -0700681 struct ovs_flow_stats stats;
Pravin B Shelare298e502013-10-29 17:22:21 -0700682 __be16 tcp_flags;
683 unsigned long used;
Jesse Grossccb13522011-10-25 19:26:31 -0700684 struct ovs_header *ovs_header;
685 struct nlattr *nla;
Jesse Grossccb13522011-10-25 19:26:31 -0700686 int err;
687
Eric W. Biederman15e47302012-09-07 20:12:54 +0000688 ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family, flags, cmd);
Jesse Grossccb13522011-10-25 19:26:31 -0700689 if (!ovs_header)
690 return -EMSGSIZE;
691
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700692 ovs_header->dp_ifindex = dp_ifindex;
Jesse Grossccb13522011-10-25 19:26:31 -0700693
Andy Zhou03f0d912013-08-07 20:01:00 -0700694 /* Fill flow key. */
Jesse Grossccb13522011-10-25 19:26:31 -0700695 nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
696 if (!nla)
697 goto nla_put_failure;
Andy Zhou03f0d912013-08-07 20:01:00 -0700698
Pravin B Shelare6445712013-10-03 18:16:47 -0700699 err = ovs_nla_put_flow(&flow->unmasked_key, &flow->unmasked_key, skb);
Jesse Grossccb13522011-10-25 19:26:31 -0700700 if (err)
701 goto error;
702 nla_nest_end(skb, nla);
703
Andy Zhou03f0d912013-08-07 20:01:00 -0700704 nla = nla_nest_start(skb, OVS_FLOW_ATTR_MASK);
705 if (!nla)
706 goto nla_put_failure;
707
Pravin B Shelare6445712013-10-03 18:16:47 -0700708 err = ovs_nla_put_flow(&flow->key, &flow->mask->key, skb);
Andy Zhou03f0d912013-08-07 20:01:00 -0700709 if (err)
710 goto error;
711
712 nla_nest_end(skb, nla);
713
Pravin B Shelare298e502013-10-29 17:22:21 -0700714 ovs_flow_stats_get(flow, &stats, &used, &tcp_flags);
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700715
David S. Miller028d6a62012-03-29 23:20:48 -0400716 if (used &&
717 nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
718 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -0700719
David S. Miller028d6a62012-03-29 23:20:48 -0400720 if (stats.n_packets &&
Pravin B Shelare298e502013-10-29 17:22:21 -0700721 nla_put(skb, OVS_FLOW_ATTR_STATS, sizeof(struct ovs_flow_stats), &stats))
David S. Miller028d6a62012-03-29 23:20:48 -0400722 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -0700723
Pravin B Shelare298e502013-10-29 17:22:21 -0700724 if ((u8)ntohs(tcp_flags) &&
725 nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, (u8)ntohs(tcp_flags)))
David S. Miller028d6a62012-03-29 23:20:48 -0400726 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -0700727
728 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
729 * this is the first flow to be dumped into 'skb'. This is unusual for
730 * Netlink but individual action lists can be longer than
731 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
732 * The userspace caller can always fetch the actions separately if it
733 * really wants them. (Most userspace callers in fact don't care.)
734 *
735 * This can only fail for dump operations because the skb is always
736 * properly sized for single flows.
737 */
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700738 start = nla_nest_start(skb, OVS_FLOW_ATTR_ACTIONS);
739 if (start) {
Pravin B Shelard57170b2013-07-30 15:39:39 -0700740 const struct sw_flow_actions *sf_acts;
741
Jesse Gross663efa32013-12-03 10:58:53 -0800742 sf_acts = rcu_dereference_ovsl(flow->sf_acts);
Pravin B Shelare6445712013-10-03 18:16:47 -0700743 err = ovs_nla_put_actions(sf_acts->actions,
744 sf_acts->actions_len, skb);
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700745
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700746 if (!err)
747 nla_nest_end(skb, start);
748 else {
749 if (skb_orig_len)
750 goto error;
751
752 nla_nest_cancel(skb, start);
753 }
754 } else if (skb_orig_len)
755 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -0700756
757 return genlmsg_end(skb, ovs_header);
758
759nla_put_failure:
760 err = -EMSGSIZE;
761error:
762 genlmsg_cancel(skb, ovs_header);
763 return err;
764}
765
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700766/* May not be called with RCU read lock. */
767static struct sk_buff *ovs_flow_cmd_alloc_info(const struct sw_flow_actions *acts,
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700768 struct genl_info *info,
769 bool always)
Jesse Grossccb13522011-10-25 19:26:31 -0700770{
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700771 struct sk_buff *skb;
Jesse Grossccb13522011-10-25 19:26:31 -0700772
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700773 if (!always && !ovs_must_notify(info, &ovs_dp_flow_multicast_group))
774 return NULL;
775
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700776 skb = genlmsg_new_unicast(ovs_flow_cmd_msg_size(acts), info, GFP_KERNEL);
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700777 if (!skb)
778 return ERR_PTR(-ENOMEM);
779
780 return skb;
Jesse Grossccb13522011-10-25 19:26:31 -0700781}
782
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700783/* Called with ovs_mutex. */
784static struct sk_buff *ovs_flow_cmd_build_info(const struct sw_flow *flow,
785 int dp_ifindex,
786 struct genl_info *info, u8 cmd,
787 bool always)
Jesse Grossccb13522011-10-25 19:26:31 -0700788{
789 struct sk_buff *skb;
790 int retval;
791
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700792 skb = ovs_flow_cmd_alloc_info(ovsl_dereference(flow->sf_acts), info,
793 always);
Himangi Saraogid0e992a2014-07-27 12:37:46 +0530794 if (IS_ERR_OR_NULL(skb))
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700795 return skb;
Jesse Grossccb13522011-10-25 19:26:31 -0700796
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700797 retval = ovs_flow_cmd_fill_info(flow, dp_ifindex, skb,
798 info->snd_portid, info->snd_seq, 0,
799 cmd);
Jesse Grossccb13522011-10-25 19:26:31 -0700800 BUG_ON(retval < 0);
801 return skb;
802}
803
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700804static int ovs_flow_cmd_new(struct sk_buff *skb, struct genl_info *info)
Jesse Grossccb13522011-10-25 19:26:31 -0700805{
806 struct nlattr **a = info->attrs;
807 struct ovs_header *ovs_header = info->userhdr;
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700808 struct sw_flow *flow, *new_flow;
Andy Zhou03f0d912013-08-07 20:01:00 -0700809 struct sw_flow_mask mask;
Jesse Grossccb13522011-10-25 19:26:31 -0700810 struct sk_buff *reply;
811 struct datapath *dp;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700812 struct sw_flow_actions *acts;
813 struct sw_flow_match match;
814 int error;
815
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700816 /* Must have key and actions. */
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700817 error = -EINVAL;
818 if (!a[OVS_FLOW_ATTR_KEY])
819 goto error;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700820 if (!a[OVS_FLOW_ATTR_ACTIONS])
821 goto error;
822
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700823 /* Most of the time we need to allocate a new flow, do it before
824 * locking.
825 */
826 new_flow = ovs_flow_alloc();
827 if (IS_ERR(new_flow)) {
828 error = PTR_ERR(new_flow);
829 goto error;
830 }
831
832 /* Extract key. */
833 ovs_match_init(&match, &new_flow->unmasked_key, &mask);
834 error = ovs_nla_get_match(&match,
835 a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
836 if (error)
837 goto err_kfree_flow;
838
839 ovs_flow_mask_key(&new_flow->key, &new_flow->unmasked_key, &mask);
840
841 /* Validate actions. */
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700842 acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_FLOW_ATTR_ACTIONS]));
843 error = PTR_ERR(acts);
844 if (IS_ERR(acts))
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700845 goto err_kfree_flow;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700846
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700847 error = ovs_nla_copy_actions(a[OVS_FLOW_ATTR_ACTIONS], &new_flow->key,
848 0, &acts);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700849 if (error) {
850 OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700851 goto err_kfree_acts;
852 }
853
854 reply = ovs_flow_cmd_alloc_info(acts, info, false);
855 if (IS_ERR(reply)) {
856 error = PTR_ERR(reply);
857 goto err_kfree_acts;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700858 }
859
860 ovs_lock();
861 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700862 if (unlikely(!dp)) {
863 error = -ENODEV;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700864 goto err_unlock_ovs;
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700865 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700866 /* Check if this is a duplicate flow */
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700867 flow = ovs_flow_tbl_lookup(&dp->table, &new_flow->unmasked_key);
868 if (likely(!flow)) {
869 rcu_assign_pointer(new_flow->sf_acts, acts);
870
871 /* Put flow in bucket. */
872 error = ovs_flow_tbl_insert(&dp->table, new_flow, &mask);
873 if (unlikely(error)) {
874 acts = NULL;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700875 goto err_unlock_ovs;
876 }
877
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700878 if (unlikely(reply)) {
879 error = ovs_flow_cmd_fill_info(new_flow,
880 ovs_header->dp_ifindex,
881 reply, info->snd_portid,
882 info->snd_seq, 0,
883 OVS_FLOW_CMD_NEW);
884 BUG_ON(error < 0);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700885 }
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700886 ovs_unlock();
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700887 } else {
888 struct sw_flow_actions *old_acts;
889
890 /* Bail out if we're not allowed to modify an existing flow.
891 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
892 * because Generic Netlink treats the latter as a dump
893 * request. We also accept NLM_F_EXCL in case that bug ever
894 * gets fixed.
895 */
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700896 if (unlikely(info->nlhdr->nlmsg_flags & (NLM_F_CREATE
897 | NLM_F_EXCL))) {
898 error = -EEXIST;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700899 goto err_unlock_ovs;
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700900 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700901 /* The unmasked key has to be the same for flow updates. */
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700902 if (unlikely(!ovs_flow_cmp_unmasked_key(flow, &match))) {
Alex Wang4a46b242014-06-30 20:30:29 -0700903 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
904 if (!flow) {
905 error = -ENOENT;
906 goto err_unlock_ovs;
907 }
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700908 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700909 /* Update actions. */
910 old_acts = ovsl_dereference(flow->sf_acts);
911 rcu_assign_pointer(flow->sf_acts, acts);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700912
913 if (unlikely(reply)) {
914 error = ovs_flow_cmd_fill_info(flow,
915 ovs_header->dp_ifindex,
916 reply, info->snd_portid,
917 info->snd_seq, 0,
918 OVS_FLOW_CMD_NEW);
919 BUG_ON(error < 0);
920 }
921 ovs_unlock();
922
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700923 ovs_nla_free_flow_actions(old_acts);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700924 ovs_flow_free(new_flow, false);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700925 }
926
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700927 if (reply)
928 ovs_notify(&dp_flow_genl_family, reply, info);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700929 return 0;
930
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700931err_unlock_ovs:
932 ovs_unlock();
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700933 kfree_skb(reply);
934err_kfree_acts:
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700935 kfree(acts);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700936err_kfree_flow:
937 ovs_flow_free(new_flow, false);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700938error:
939 return error;
940}
941
942static int ovs_flow_cmd_set(struct sk_buff *skb, struct genl_info *info)
943{
944 struct nlattr **a = info->attrs;
945 struct ovs_header *ovs_header = info->userhdr;
946 struct sw_flow_key key, masked_key;
947 struct sw_flow *flow;
948 struct sw_flow_mask mask;
949 struct sk_buff *reply = NULL;
950 struct datapath *dp;
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700951 struct sw_flow_actions *old_acts = NULL, *acts = NULL;
Andy Zhou03f0d912013-08-07 20:01:00 -0700952 struct sw_flow_match match;
Jesse Grossccb13522011-10-25 19:26:31 -0700953 int error;
Jesse Grossccb13522011-10-25 19:26:31 -0700954
955 /* Extract key. */
956 error = -EINVAL;
957 if (!a[OVS_FLOW_ATTR_KEY])
958 goto error;
Andy Zhou03f0d912013-08-07 20:01:00 -0700959
960 ovs_match_init(&match, &key, &mask);
Jarno Rajahalme23dabf82014-03-27 12:35:23 -0700961 error = ovs_nla_get_match(&match,
Pravin B Shelare6445712013-10-03 18:16:47 -0700962 a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
Jesse Grossccb13522011-10-25 19:26:31 -0700963 if (error)
964 goto error;
965
966 /* Validate actions. */
967 if (a[OVS_FLOW_ATTR_ACTIONS]) {
Pravin B Shelare6445712013-10-03 18:16:47 -0700968 acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_FLOW_ATTR_ACTIONS]));
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700969 error = PTR_ERR(acts);
970 if (IS_ERR(acts))
Jesse Grossccb13522011-10-25 19:26:31 -0700971 goto error;
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700972
Pravin B Shelare6445712013-10-03 18:16:47 -0700973 ovs_flow_mask_key(&masked_key, &key, &mask);
974 error = ovs_nla_copy_actions(a[OVS_FLOW_ATTR_ACTIONS],
975 &masked_key, 0, &acts);
Andy Zhou03f0d912013-08-07 20:01:00 -0700976 if (error) {
977 OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700978 goto err_kfree_acts;
979 }
980 }
981
982 /* Can allocate before locking if have acts. */
983 if (acts) {
984 reply = ovs_flow_cmd_alloc_info(acts, info, false);
985 if (IS_ERR(reply)) {
986 error = PTR_ERR(reply);
987 goto err_kfree_acts;
Andy Zhou03f0d912013-08-07 20:01:00 -0700988 }
Jesse Grossccb13522011-10-25 19:26:31 -0700989 }
990
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700991 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800992 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700993 if (unlikely(!dp)) {
994 error = -ENODEV;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700995 goto err_unlock_ovs;
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700996 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700997 /* Check that the flow exists. */
Alex Wang4a46b242014-06-30 20:30:29 -0700998 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700999 if (unlikely(!flow)) {
1000 error = -ENOENT;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001001 goto err_unlock_ovs;
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001002 }
Alex Wang4a46b242014-06-30 20:30:29 -07001003
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001004 /* Update actions, if present. */
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001005 if (likely(acts)) {
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001006 old_acts = ovsl_dereference(flow->sf_acts);
Jesse Grossccb13522011-10-25 19:26:31 -07001007 rcu_assign_pointer(flow->sf_acts, acts);
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001008
1009 if (unlikely(reply)) {
1010 error = ovs_flow_cmd_fill_info(flow,
1011 ovs_header->dp_ifindex,
1012 reply, info->snd_portid,
1013 info->snd_seq, 0,
1014 OVS_FLOW_CMD_NEW);
1015 BUG_ON(error < 0);
1016 }
1017 } else {
1018 /* Could not alloc without acts before locking. */
1019 reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex,
1020 info, OVS_FLOW_CMD_NEW, false);
1021 if (unlikely(IS_ERR(reply))) {
1022 error = PTR_ERR(reply);
1023 goto err_unlock_ovs;
1024 }
Jesse Grossccb13522011-10-25 19:26:31 -07001025 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001026
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001027 /* Clear stats. */
1028 if (a[OVS_FLOW_ATTR_CLEAR])
1029 ovs_flow_stats_clear(flow);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001030 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001031
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001032 if (reply)
1033 ovs_notify(&dp_flow_genl_family, reply, info);
1034 if (old_acts)
1035 ovs_nla_free_flow_actions(old_acts);
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -07001036
Jesse Grossccb13522011-10-25 19:26:31 -07001037 return 0;
1038
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001039err_unlock_ovs:
1040 ovs_unlock();
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001041 kfree_skb(reply);
1042err_kfree_acts:
Pravin B Shelar74f84a52013-06-17 17:50:12 -07001043 kfree(acts);
Jesse Grossccb13522011-10-25 19:26:31 -07001044error:
1045 return error;
1046}
1047
1048static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1049{
1050 struct nlattr **a = info->attrs;
1051 struct ovs_header *ovs_header = info->userhdr;
1052 struct sw_flow_key key;
1053 struct sk_buff *reply;
1054 struct sw_flow *flow;
1055 struct datapath *dp;
Andy Zhou03f0d912013-08-07 20:01:00 -07001056 struct sw_flow_match match;
Jesse Grossccb13522011-10-25 19:26:31 -07001057 int err;
Jesse Grossccb13522011-10-25 19:26:31 -07001058
Andy Zhou03f0d912013-08-07 20:01:00 -07001059 if (!a[OVS_FLOW_ATTR_KEY]) {
1060 OVS_NLERR("Flow get message rejected, Key attribute missing.\n");
Jesse Grossccb13522011-10-25 19:26:31 -07001061 return -EINVAL;
Andy Zhou03f0d912013-08-07 20:01:00 -07001062 }
1063
1064 ovs_match_init(&match, &key, NULL);
Jarno Rajahalme23dabf82014-03-27 12:35:23 -07001065 err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL);
Jesse Grossccb13522011-10-25 19:26:31 -07001066 if (err)
1067 return err;
1068
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001069 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001070 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001071 if (!dp) {
1072 err = -ENODEV;
1073 goto unlock;
1074 }
Jesse Grossccb13522011-10-25 19:26:31 -07001075
Alex Wang4a46b242014-06-30 20:30:29 -07001076 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1077 if (!flow) {
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001078 err = -ENOENT;
1079 goto unlock;
1080 }
Jesse Grossccb13522011-10-25 19:26:31 -07001081
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -07001082 reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex, info,
1083 OVS_FLOW_CMD_NEW, true);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001084 if (IS_ERR(reply)) {
1085 err = PTR_ERR(reply);
1086 goto unlock;
1087 }
Jesse Grossccb13522011-10-25 19:26:31 -07001088
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001089 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001090 return genlmsg_reply(reply, info);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001091unlock:
1092 ovs_unlock();
1093 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001094}
1095
1096static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1097{
1098 struct nlattr **a = info->attrs;
1099 struct ovs_header *ovs_header = info->userhdr;
1100 struct sw_flow_key key;
1101 struct sk_buff *reply;
1102 struct sw_flow *flow;
1103 struct datapath *dp;
Andy Zhou03f0d912013-08-07 20:01:00 -07001104 struct sw_flow_match match;
Jesse Grossccb13522011-10-25 19:26:31 -07001105 int err;
Jesse Grossccb13522011-10-25 19:26:31 -07001106
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001107 if (likely(a[OVS_FLOW_ATTR_KEY])) {
1108 ovs_match_init(&match, &key, NULL);
1109 err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL);
1110 if (unlikely(err))
1111 return err;
1112 }
1113
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001114 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001115 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001116 if (unlikely(!dp)) {
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001117 err = -ENODEV;
1118 goto unlock;
1119 }
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001120
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001121 if (unlikely(!a[OVS_FLOW_ATTR_KEY])) {
Pravin B Shelarb637e492013-10-04 00:14:23 -07001122 err = ovs_flow_tbl_flush(&dp->table);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001123 goto unlock;
1124 }
Andy Zhou03f0d912013-08-07 20:01:00 -07001125
Alex Wang4a46b242014-06-30 20:30:29 -07001126 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1127 if (unlikely(!flow)) {
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001128 err = -ENOENT;
1129 goto unlock;
1130 }
Jesse Grossccb13522011-10-25 19:26:31 -07001131
Pravin B Shelarb637e492013-10-04 00:14:23 -07001132 ovs_flow_tbl_remove(&dp->table, flow);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001133 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001134
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001135 reply = ovs_flow_cmd_alloc_info((const struct sw_flow_actions __force *) flow->sf_acts,
1136 info, false);
1137 if (likely(reply)) {
1138 if (likely(!IS_ERR(reply))) {
1139 rcu_read_lock(); /*To keep RCU checker happy. */
1140 err = ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex,
1141 reply, info->snd_portid,
1142 info->snd_seq, 0,
1143 OVS_FLOW_CMD_DEL);
1144 rcu_read_unlock();
1145 BUG_ON(err < 0);
1146
1147 ovs_notify(&dp_flow_genl_family, reply, info);
1148 } else {
1149 netlink_set_err(sock_net(skb->sk)->genl_sock, 0, 0, PTR_ERR(reply));
1150 }
1151 }
1152
1153 ovs_flow_free(flow, true);
Jesse Grossccb13522011-10-25 19:26:31 -07001154 return 0;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001155unlock:
1156 ovs_unlock();
1157 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001158}
1159
1160static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1161{
1162 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
Pravin B Shelarb637e492013-10-04 00:14:23 -07001163 struct table_instance *ti;
Jesse Grossccb13522011-10-25 19:26:31 -07001164 struct datapath *dp;
Jesse Grossccb13522011-10-25 19:26:31 -07001165
Pravin B Shelard57170b2013-07-30 15:39:39 -07001166 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001167 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001168 if (!dp) {
Pravin B Shelard57170b2013-07-30 15:39:39 -07001169 rcu_read_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001170 return -ENODEV;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001171 }
Jesse Grossccb13522011-10-25 19:26:31 -07001172
Pravin B Shelarb637e492013-10-04 00:14:23 -07001173 ti = rcu_dereference(dp->table.ti);
Jesse Grossccb13522011-10-25 19:26:31 -07001174 for (;;) {
1175 struct sw_flow *flow;
1176 u32 bucket, obj;
1177
1178 bucket = cb->args[0];
1179 obj = cb->args[1];
Pravin B Shelarb637e492013-10-04 00:14:23 -07001180 flow = ovs_flow_tbl_dump_next(ti, &bucket, &obj);
Jesse Grossccb13522011-10-25 19:26:31 -07001181 if (!flow)
1182 break;
1183
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -07001184 if (ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex, skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001185 NETLINK_CB(cb->skb).portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001186 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1187 OVS_FLOW_CMD_NEW) < 0)
1188 break;
1189
1190 cb->args[0] = bucket;
1191 cb->args[1] = obj;
1192 }
Pravin B Shelard57170b2013-07-30 15:39:39 -07001193 rcu_read_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001194 return skb->len;
1195}
1196
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001197static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
1198 [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
1199 [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
1200 [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
1201};
1202
stephen hemminger48e48a72014-07-16 11:25:52 -07001203static const struct genl_ops dp_flow_genl_ops[] = {
Jesse Grossccb13522011-10-25 19:26:31 -07001204 { .cmd = OVS_FLOW_CMD_NEW,
1205 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1206 .policy = flow_policy,
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001207 .doit = ovs_flow_cmd_new
Jesse Grossccb13522011-10-25 19:26:31 -07001208 },
1209 { .cmd = OVS_FLOW_CMD_DEL,
1210 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1211 .policy = flow_policy,
1212 .doit = ovs_flow_cmd_del
1213 },
1214 { .cmd = OVS_FLOW_CMD_GET,
1215 .flags = 0, /* OK for unprivileged users. */
1216 .policy = flow_policy,
1217 .doit = ovs_flow_cmd_get,
1218 .dumpit = ovs_flow_cmd_dump
1219 },
1220 { .cmd = OVS_FLOW_CMD_SET,
1221 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1222 .policy = flow_policy,
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001223 .doit = ovs_flow_cmd_set,
Jesse Grossccb13522011-10-25 19:26:31 -07001224 },
1225};
1226
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001227static struct genl_family dp_flow_genl_family = {
Jesse Grossccb13522011-10-25 19:26:31 -07001228 .id = GENL_ID_GENERATE,
1229 .hdrsize = sizeof(struct ovs_header),
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001230 .name = OVS_FLOW_FAMILY,
1231 .version = OVS_FLOW_VERSION,
1232 .maxattr = OVS_FLOW_ATTR_MAX,
Pravin B Shelar3a4e0d62013-04-23 07:48:48 +00001233 .netnsok = true,
1234 .parallel_ops = true,
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001235 .ops = dp_flow_genl_ops,
1236 .n_ops = ARRAY_SIZE(dp_flow_genl_ops),
1237 .mcgrps = &ovs_dp_flow_multicast_group,
1238 .n_mcgrps = 1,
Jesse Grossccb13522011-10-25 19:26:31 -07001239};
1240
Thomas Grafc3ff8cf2013-03-29 14:46:49 +01001241static size_t ovs_dp_cmd_msg_size(void)
1242{
1243 size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
1244
1245 msgsize += nla_total_size(IFNAMSIZ);
1246 msgsize += nla_total_size(sizeof(struct ovs_dp_stats));
Andy Zhou1bd71162013-10-22 10:42:46 -07001247 msgsize += nla_total_size(sizeof(struct ovs_dp_megaflow_stats));
Daniele Di Proietto45fb9c32014-01-23 10:47:35 -08001248 msgsize += nla_total_size(sizeof(u32)); /* OVS_DP_ATTR_USER_FEATURES */
Thomas Grafc3ff8cf2013-03-29 14:46:49 +01001249
1250 return msgsize;
1251}
1252
Jarno Rajahalmebb6f9a72014-05-05 11:32:17 -07001253/* Called with ovs_mutex or RCU read lock. */
Jesse Grossccb13522011-10-25 19:26:31 -07001254static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001255 u32 portid, u32 seq, u32 flags, u8 cmd)
Jesse Grossccb13522011-10-25 19:26:31 -07001256{
1257 struct ovs_header *ovs_header;
1258 struct ovs_dp_stats dp_stats;
Andy Zhou1bd71162013-10-22 10:42:46 -07001259 struct ovs_dp_megaflow_stats dp_megaflow_stats;
Jesse Grossccb13522011-10-25 19:26:31 -07001260 int err;
1261
Eric W. Biederman15e47302012-09-07 20:12:54 +00001262 ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
Jesse Grossccb13522011-10-25 19:26:31 -07001263 flags, cmd);
1264 if (!ovs_header)
1265 goto error;
1266
1267 ovs_header->dp_ifindex = get_dpifindex(dp);
1268
Jesse Grossccb13522011-10-25 19:26:31 -07001269 err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
Jesse Grossccb13522011-10-25 19:26:31 -07001270 if (err)
1271 goto nla_put_failure;
1272
Andy Zhou1bd71162013-10-22 10:42:46 -07001273 get_dp_stats(dp, &dp_stats, &dp_megaflow_stats);
1274 if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats),
1275 &dp_stats))
1276 goto nla_put_failure;
1277
1278 if (nla_put(skb, OVS_DP_ATTR_MEGAFLOW_STATS,
1279 sizeof(struct ovs_dp_megaflow_stats),
1280 &dp_megaflow_stats))
David S. Miller028d6a62012-03-29 23:20:48 -04001281 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001282
Thomas Graf43d4be92013-12-13 15:22:18 +01001283 if (nla_put_u32(skb, OVS_DP_ATTR_USER_FEATURES, dp->user_features))
1284 goto nla_put_failure;
1285
Jesse Grossccb13522011-10-25 19:26:31 -07001286 return genlmsg_end(skb, ovs_header);
1287
1288nla_put_failure:
1289 genlmsg_cancel(skb, ovs_header);
1290error:
1291 return -EMSGSIZE;
1292}
1293
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001294static struct sk_buff *ovs_dp_cmd_alloc_info(struct genl_info *info)
Jesse Grossccb13522011-10-25 19:26:31 -07001295{
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001296 return genlmsg_new_unicast(ovs_dp_cmd_msg_size(), info, GFP_KERNEL);
Jesse Grossccb13522011-10-25 19:26:31 -07001297}
1298
Jarno Rajahalmebb6f9a72014-05-05 11:32:17 -07001299/* Called with rcu_read_lock or ovs_mutex. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001300static struct datapath *lookup_datapath(struct net *net,
1301 struct ovs_header *ovs_header,
Jesse Grossccb13522011-10-25 19:26:31 -07001302 struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1303{
1304 struct datapath *dp;
1305
1306 if (!a[OVS_DP_ATTR_NAME])
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001307 dp = get_dp(net, ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001308 else {
1309 struct vport *vport;
1310
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001311 vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
Jesse Grossccb13522011-10-25 19:26:31 -07001312 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
Jesse Grossccb13522011-10-25 19:26:31 -07001313 }
1314 return dp ? dp : ERR_PTR(-ENODEV);
1315}
1316
Thomas Graf44da5ae2013-12-13 15:22:19 +01001317static void ovs_dp_reset_user_features(struct sk_buff *skb, struct genl_info *info)
1318{
1319 struct datapath *dp;
1320
1321 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
Jiri Pirko3c7eacf2014-02-14 11:42:36 +01001322 if (IS_ERR(dp))
Thomas Graf44da5ae2013-12-13 15:22:19 +01001323 return;
1324
1325 WARN(dp->user_features, "Dropping previously announced user features\n");
1326 dp->user_features = 0;
1327}
1328
Thomas Graf43d4be92013-12-13 15:22:18 +01001329static void ovs_dp_change(struct datapath *dp, struct nlattr **a)
1330{
1331 if (a[OVS_DP_ATTR_USER_FEATURES])
1332 dp->user_features = nla_get_u32(a[OVS_DP_ATTR_USER_FEATURES]);
1333}
1334
Jesse Grossccb13522011-10-25 19:26:31 -07001335static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1336{
1337 struct nlattr **a = info->attrs;
1338 struct vport_parms parms;
1339 struct sk_buff *reply;
1340 struct datapath *dp;
1341 struct vport *vport;
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001342 struct ovs_net *ovs_net;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001343 int err, i;
Jesse Grossccb13522011-10-25 19:26:31 -07001344
1345 err = -EINVAL;
1346 if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1347 goto err;
1348
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001349 reply = ovs_dp_cmd_alloc_info(info);
1350 if (!reply)
1351 return -ENOMEM;
Jesse Grossccb13522011-10-25 19:26:31 -07001352
1353 err = -ENOMEM;
1354 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1355 if (dp == NULL)
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001356 goto err_free_reply;
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001357
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001358 ovs_dp_set_net(dp, hold_net(sock_net(skb->sk)));
Jesse Grossccb13522011-10-25 19:26:31 -07001359
1360 /* Allocate table. */
Pravin B Shelarb637e492013-10-04 00:14:23 -07001361 err = ovs_flow_tbl_init(&dp->table);
1362 if (err)
Jesse Grossccb13522011-10-25 19:26:31 -07001363 goto err_free_dp;
1364
WANG Cong1c213bd2014-02-13 11:46:28 -08001365 dp->stats_percpu = netdev_alloc_pcpu_stats(struct dp_stats_percpu);
Jesse Grossccb13522011-10-25 19:26:31 -07001366 if (!dp->stats_percpu) {
1367 err = -ENOMEM;
1368 goto err_destroy_table;
1369 }
1370
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001371 dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
Pravin B Shelare6445712013-10-03 18:16:47 -07001372 GFP_KERNEL);
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001373 if (!dp->ports) {
1374 err = -ENOMEM;
1375 goto err_destroy_percpu;
1376 }
1377
1378 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1379 INIT_HLIST_HEAD(&dp->ports[i]);
1380
Jesse Grossccb13522011-10-25 19:26:31 -07001381 /* Set up our datapath device. */
1382 parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1383 parms.type = OVS_VPORT_TYPE_INTERNAL;
1384 parms.options = NULL;
1385 parms.dp = dp;
1386 parms.port_no = OVSP_LOCAL;
Alex Wang5cd667b2014-07-17 15:14:13 -07001387 parms.upcall_portids = a[OVS_DP_ATTR_UPCALL_PID];
Jesse Grossccb13522011-10-25 19:26:31 -07001388
Thomas Graf43d4be92013-12-13 15:22:18 +01001389 ovs_dp_change(dp, a);
1390
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001391 /* So far only local changes have been made, now need the lock. */
1392 ovs_lock();
1393
Jesse Grossccb13522011-10-25 19:26:31 -07001394 vport = new_vport(&parms);
1395 if (IS_ERR(vport)) {
1396 err = PTR_ERR(vport);
1397 if (err == -EBUSY)
1398 err = -EEXIST;
1399
Thomas Graf44da5ae2013-12-13 15:22:19 +01001400 if (err == -EEXIST) {
1401 /* An outdated user space instance that does not understand
1402 * the concept of user_features has attempted to create a new
1403 * datapath and is likely to reuse it. Drop all user features.
1404 */
1405 if (info->genlhdr->version < OVS_DP_VER_FEATURES)
1406 ovs_dp_reset_user_features(skb, info);
1407 }
1408
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001409 goto err_destroy_ports_array;
Jesse Grossccb13522011-10-25 19:26:31 -07001410 }
1411
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001412 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1413 info->snd_seq, 0, OVS_DP_CMD_NEW);
1414 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001415
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001416 ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001417 list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001418
1419 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001420
Johannes Berg2a94fe42013-11-19 15:19:39 +01001421 ovs_notify(&dp_datapath_genl_family, reply, info);
Jesse Grossccb13522011-10-25 19:26:31 -07001422 return 0;
1423
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001424err_destroy_ports_array:
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001425 ovs_unlock();
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001426 kfree(dp->ports);
Jesse Grossccb13522011-10-25 19:26:31 -07001427err_destroy_percpu:
1428 free_percpu(dp->stats_percpu);
1429err_destroy_table:
Andy Zhoue80857c2014-01-21 09:31:04 -08001430 ovs_flow_tbl_destroy(&dp->table, false);
Jesse Grossccb13522011-10-25 19:26:31 -07001431err_free_dp:
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001432 release_net(ovs_dp_get_net(dp));
Jesse Grossccb13522011-10-25 19:26:31 -07001433 kfree(dp);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001434err_free_reply:
1435 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001436err:
1437 return err;
1438}
1439
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001440/* Called with ovs_mutex. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001441static void __dp_destroy(struct datapath *dp)
Jesse Grossccb13522011-10-25 19:26:31 -07001442{
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001443 int i;
Jesse Grossccb13522011-10-25 19:26:31 -07001444
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001445 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1446 struct vport *vport;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001447 struct hlist_node *n;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001448
Sasha Levinb67bfe02013-02-27 17:06:00 -08001449 hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001450 if (vport->port_no != OVSP_LOCAL)
1451 ovs_dp_detach_port(vport);
1452 }
Jesse Grossccb13522011-10-25 19:26:31 -07001453
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001454 list_del_rcu(&dp->list_node);
Jesse Grossccb13522011-10-25 19:26:31 -07001455
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001456 /* OVSP_LOCAL is datapath internal port. We need to make sure that
Andy Zhoue80857c2014-01-21 09:31:04 -08001457 * all ports in datapath are destroyed first before freeing datapath.
Jesse Grossccb13522011-10-25 19:26:31 -07001458 */
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001459 ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
Jesse Grossccb13522011-10-25 19:26:31 -07001460
Andy Zhoue80857c2014-01-21 09:31:04 -08001461 /* RCU destroy the flow table */
1462 ovs_flow_tbl_destroy(&dp->table, true);
1463
Jesse Grossccb13522011-10-25 19:26:31 -07001464 call_rcu(&dp->rcu, destroy_dp_rcu);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001465}
1466
1467static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1468{
1469 struct sk_buff *reply;
1470 struct datapath *dp;
1471 int err;
1472
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001473 reply = ovs_dp_cmd_alloc_info(info);
1474 if (!reply)
1475 return -ENOMEM;
1476
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001477 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001478 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1479 err = PTR_ERR(dp);
1480 if (IS_ERR(dp))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001481 goto err_unlock_free;
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001482
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001483 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1484 info->snd_seq, 0, OVS_DP_CMD_DEL);
1485 BUG_ON(err < 0);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001486
1487 __dp_destroy(dp);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001488 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001489
Johannes Berg2a94fe42013-11-19 15:19:39 +01001490 ovs_notify(&dp_datapath_genl_family, reply, info);
Jesse Grossccb13522011-10-25 19:26:31 -07001491
1492 return 0;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001493
1494err_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001495 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001496 kfree_skb(reply);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001497 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001498}
1499
1500static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1501{
1502 struct sk_buff *reply;
1503 struct datapath *dp;
1504 int err;
1505
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001506 reply = ovs_dp_cmd_alloc_info(info);
1507 if (!reply)
1508 return -ENOMEM;
1509
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001510 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001511 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001512 err = PTR_ERR(dp);
Jesse Grossccb13522011-10-25 19:26:31 -07001513 if (IS_ERR(dp))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001514 goto err_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001515
Thomas Graf43d4be92013-12-13 15:22:18 +01001516 ovs_dp_change(dp, info->attrs);
1517
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001518 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1519 info->snd_seq, 0, OVS_DP_CMD_NEW);
1520 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001521
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001522 ovs_unlock();
Johannes Berg2a94fe42013-11-19 15:19:39 +01001523 ovs_notify(&dp_datapath_genl_family, reply, info);
Jesse Grossccb13522011-10-25 19:26:31 -07001524
1525 return 0;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001526
1527err_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001528 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001529 kfree_skb(reply);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001530 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001531}
1532
1533static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1534{
1535 struct sk_buff *reply;
1536 struct datapath *dp;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001537 int err;
Jesse Grossccb13522011-10-25 19:26:31 -07001538
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001539 reply = ovs_dp_cmd_alloc_info(info);
1540 if (!reply)
1541 return -ENOMEM;
1542
1543 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001544 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001545 if (IS_ERR(dp)) {
1546 err = PTR_ERR(dp);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001547 goto err_unlock_free;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001548 }
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001549 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1550 info->snd_seq, 0, OVS_DP_CMD_NEW);
1551 BUG_ON(err < 0);
1552 rcu_read_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001553
Jesse Grossccb13522011-10-25 19:26:31 -07001554 return genlmsg_reply(reply, info);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001555
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001556err_unlock_free:
1557 rcu_read_unlock();
1558 kfree_skb(reply);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001559 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001560}
1561
1562static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1563{
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001564 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
Jesse Grossccb13522011-10-25 19:26:31 -07001565 struct datapath *dp;
1566 int skip = cb->args[0];
1567 int i = 0;
1568
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001569 rcu_read_lock();
1570 list_for_each_entry_rcu(dp, &ovs_net->dps, list_node) {
Ben Pfaff77676fd2012-01-17 13:33:39 +00001571 if (i >= skip &&
Eric W. Biederman15e47302012-09-07 20:12:54 +00001572 ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001573 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1574 OVS_DP_CMD_NEW) < 0)
1575 break;
1576 i++;
1577 }
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001578 rcu_read_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001579
1580 cb->args[0] = i;
1581
1582 return skb->len;
1583}
1584
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001585static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1586 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1587 [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1588 [OVS_DP_ATTR_USER_FEATURES] = { .type = NLA_U32 },
1589};
1590
stephen hemminger48e48a72014-07-16 11:25:52 -07001591static const struct genl_ops dp_datapath_genl_ops[] = {
Jesse Grossccb13522011-10-25 19:26:31 -07001592 { .cmd = OVS_DP_CMD_NEW,
1593 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1594 .policy = datapath_policy,
1595 .doit = ovs_dp_cmd_new
1596 },
1597 { .cmd = OVS_DP_CMD_DEL,
1598 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1599 .policy = datapath_policy,
1600 .doit = ovs_dp_cmd_del
1601 },
1602 { .cmd = OVS_DP_CMD_GET,
1603 .flags = 0, /* OK for unprivileged users. */
1604 .policy = datapath_policy,
1605 .doit = ovs_dp_cmd_get,
1606 .dumpit = ovs_dp_cmd_dump
1607 },
1608 { .cmd = OVS_DP_CMD_SET,
1609 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1610 .policy = datapath_policy,
1611 .doit = ovs_dp_cmd_set,
1612 },
1613};
1614
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001615static struct genl_family dp_datapath_genl_family = {
Jesse Grossccb13522011-10-25 19:26:31 -07001616 .id = GENL_ID_GENERATE,
1617 .hdrsize = sizeof(struct ovs_header),
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001618 .name = OVS_DATAPATH_FAMILY,
1619 .version = OVS_DATAPATH_VERSION,
1620 .maxattr = OVS_DP_ATTR_MAX,
Pravin B Shelar3a4e0d62013-04-23 07:48:48 +00001621 .netnsok = true,
1622 .parallel_ops = true,
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001623 .ops = dp_datapath_genl_ops,
1624 .n_ops = ARRAY_SIZE(dp_datapath_genl_ops),
1625 .mcgrps = &ovs_dp_datapath_multicast_group,
1626 .n_mcgrps = 1,
Jesse Grossccb13522011-10-25 19:26:31 -07001627};
1628
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001629/* Called with ovs_mutex or RCU read lock. */
Jesse Grossccb13522011-10-25 19:26:31 -07001630static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001631 u32 portid, u32 seq, u32 flags, u8 cmd)
Jesse Grossccb13522011-10-25 19:26:31 -07001632{
1633 struct ovs_header *ovs_header;
1634 struct ovs_vport_stats vport_stats;
1635 int err;
1636
Eric W. Biederman15e47302012-09-07 20:12:54 +00001637 ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
Jesse Grossccb13522011-10-25 19:26:31 -07001638 flags, cmd);
1639 if (!ovs_header)
1640 return -EMSGSIZE;
1641
1642 ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1643
David S. Miller028d6a62012-03-29 23:20:48 -04001644 if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
1645 nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
Alex Wang5cd667b2014-07-17 15:14:13 -07001646 nla_put_string(skb, OVS_VPORT_ATTR_NAME,
1647 vport->ops->get_name(vport)))
David S. Miller028d6a62012-03-29 23:20:48 -04001648 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001649
1650 ovs_vport_get_stats(vport, &vport_stats);
David S. Miller028d6a62012-03-29 23:20:48 -04001651 if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
1652 &vport_stats))
1653 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001654
Alex Wang5cd667b2014-07-17 15:14:13 -07001655 if (ovs_vport_get_upcall_portids(vport, skb))
1656 goto nla_put_failure;
1657
Jesse Grossccb13522011-10-25 19:26:31 -07001658 err = ovs_vport_get_options(vport, skb);
1659 if (err == -EMSGSIZE)
1660 goto error;
1661
1662 return genlmsg_end(skb, ovs_header);
1663
1664nla_put_failure:
1665 err = -EMSGSIZE;
1666error:
1667 genlmsg_cancel(skb, ovs_header);
1668 return err;
1669}
1670
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001671static struct sk_buff *ovs_vport_cmd_alloc_info(void)
1672{
1673 return nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1674}
1675
1676/* Called with ovs_mutex, only via ovs_dp_notify_wq(). */
Eric W. Biederman15e47302012-09-07 20:12:54 +00001677struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001678 u32 seq, u8 cmd)
1679{
1680 struct sk_buff *skb;
1681 int retval;
1682
1683 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1684 if (!skb)
1685 return ERR_PTR(-ENOMEM);
1686
Eric W. Biederman15e47302012-09-07 20:12:54 +00001687 retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
Jesse Grossa9341512013-03-26 15:48:38 -07001688 BUG_ON(retval < 0);
1689
Jesse Grossccb13522011-10-25 19:26:31 -07001690 return skb;
1691}
1692
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001693/* Called with ovs_mutex or RCU read lock. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001694static struct vport *lookup_vport(struct net *net,
1695 struct ovs_header *ovs_header,
Jesse Grossccb13522011-10-25 19:26:31 -07001696 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1697{
1698 struct datapath *dp;
1699 struct vport *vport;
1700
1701 if (a[OVS_VPORT_ATTR_NAME]) {
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001702 vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
Jesse Grossccb13522011-10-25 19:26:31 -07001703 if (!vport)
1704 return ERR_PTR(-ENODEV);
Ben Pfaff651a68e2012-03-06 15:04:04 -08001705 if (ovs_header->dp_ifindex &&
1706 ovs_header->dp_ifindex != get_dpifindex(vport->dp))
1707 return ERR_PTR(-ENODEV);
Jesse Grossccb13522011-10-25 19:26:31 -07001708 return vport;
1709 } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
1710 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1711
1712 if (port_no >= DP_MAX_PORTS)
1713 return ERR_PTR(-EFBIG);
1714
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001715 dp = get_dp(net, ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001716 if (!dp)
1717 return ERR_PTR(-ENODEV);
1718
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001719 vport = ovs_vport_ovsl_rcu(dp, port_no);
Jesse Grossccb13522011-10-25 19:26:31 -07001720 if (!vport)
Jarno Rajahalme14408db2013-01-09 14:27:35 -08001721 return ERR_PTR(-ENODEV);
Jesse Grossccb13522011-10-25 19:26:31 -07001722 return vport;
1723 } else
1724 return ERR_PTR(-EINVAL);
1725}
1726
1727static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
1728{
1729 struct nlattr **a = info->attrs;
1730 struct ovs_header *ovs_header = info->userhdr;
1731 struct vport_parms parms;
1732 struct sk_buff *reply;
1733 struct vport *vport;
1734 struct datapath *dp;
1735 u32 port_no;
1736 int err;
1737
Jesse Grossccb13522011-10-25 19:26:31 -07001738 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
1739 !a[OVS_VPORT_ATTR_UPCALL_PID])
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001740 return -EINVAL;
1741
1742 port_no = a[OVS_VPORT_ATTR_PORT_NO]
1743 ? nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]) : 0;
1744 if (port_no >= DP_MAX_PORTS)
1745 return -EFBIG;
1746
1747 reply = ovs_vport_cmd_alloc_info();
1748 if (!reply)
1749 return -ENOMEM;
Jesse Grossccb13522011-10-25 19:26:31 -07001750
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001751 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001752 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001753 err = -ENODEV;
1754 if (!dp)
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001755 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001756
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001757 if (port_no) {
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001758 vport = ovs_vport_ovsl(dp, port_no);
Jesse Grossccb13522011-10-25 19:26:31 -07001759 err = -EBUSY;
1760 if (vport)
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001761 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001762 } else {
1763 for (port_no = 1; ; port_no++) {
1764 if (port_no >= DP_MAX_PORTS) {
1765 err = -EFBIG;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001766 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001767 }
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001768 vport = ovs_vport_ovsl(dp, port_no);
Jesse Grossccb13522011-10-25 19:26:31 -07001769 if (!vport)
1770 break;
1771 }
1772 }
1773
1774 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
1775 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1776 parms.options = a[OVS_VPORT_ATTR_OPTIONS];
1777 parms.dp = dp;
1778 parms.port_no = port_no;
Alex Wang5cd667b2014-07-17 15:14:13 -07001779 parms.upcall_portids = a[OVS_VPORT_ATTR_UPCALL_PID];
Jesse Grossccb13522011-10-25 19:26:31 -07001780
1781 vport = new_vport(&parms);
1782 err = PTR_ERR(vport);
1783 if (IS_ERR(vport))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001784 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001785
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001786 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1787 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1788 BUG_ON(err < 0);
1789 ovs_unlock();
Thomas Grafed6611852013-03-29 14:46:50 +01001790
Johannes Berg2a94fe42013-11-19 15:19:39 +01001791 ovs_notify(&dp_vport_genl_family, reply, info);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001792 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001793
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001794exit_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001795 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001796 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001797 return err;
1798}
1799
1800static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
1801{
1802 struct nlattr **a = info->attrs;
1803 struct sk_buff *reply;
1804 struct vport *vport;
1805 int err;
1806
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001807 reply = ovs_vport_cmd_alloc_info();
1808 if (!reply)
1809 return -ENOMEM;
1810
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001811 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001812 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
Jesse Grossccb13522011-10-25 19:26:31 -07001813 err = PTR_ERR(vport);
1814 if (IS_ERR(vport))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001815 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001816
Jesse Grossccb13522011-10-25 19:26:31 -07001817 if (a[OVS_VPORT_ATTR_TYPE] &&
Jesse Grossf44f3402013-05-13 08:15:26 -07001818 nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
Jesse Grossccb13522011-10-25 19:26:31 -07001819 err = -EINVAL;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001820 goto exit_unlock_free;
Jesse Grossa9341512013-03-26 15:48:38 -07001821 }
1822
Jesse Grossf44f3402013-05-13 08:15:26 -07001823 if (a[OVS_VPORT_ATTR_OPTIONS]) {
Jesse Grossccb13522011-10-25 19:26:31 -07001824 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
Jesse Grossf44f3402013-05-13 08:15:26 -07001825 if (err)
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001826 goto exit_unlock_free;
Jesse Grossf44f3402013-05-13 08:15:26 -07001827 }
Jesse Grossa9341512013-03-26 15:48:38 -07001828
Alex Wang5cd667b2014-07-17 15:14:13 -07001829
1830 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
1831 struct nlattr *ids = a[OVS_VPORT_ATTR_UPCALL_PID];
1832
1833 err = ovs_vport_set_upcall_portids(vport, ids);
1834 if (err)
1835 goto exit_unlock_free;
1836 }
Jesse Grossccb13522011-10-25 19:26:31 -07001837
Jesse Grossa9341512013-03-26 15:48:38 -07001838 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1839 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1840 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001841
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001842 ovs_unlock();
Johannes Berg2a94fe42013-11-19 15:19:39 +01001843 ovs_notify(&dp_vport_genl_family, reply, info);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001844 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001845
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001846exit_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001847 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001848 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001849 return err;
1850}
1851
1852static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
1853{
1854 struct nlattr **a = info->attrs;
1855 struct sk_buff *reply;
1856 struct vport *vport;
1857 int err;
1858
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001859 reply = ovs_vport_cmd_alloc_info();
1860 if (!reply)
1861 return -ENOMEM;
1862
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001863 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001864 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
Jesse Grossccb13522011-10-25 19:26:31 -07001865 err = PTR_ERR(vport);
1866 if (IS_ERR(vport))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001867 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001868
1869 if (vport->port_no == OVSP_LOCAL) {
1870 err = -EINVAL;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001871 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001872 }
1873
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001874 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1875 info->snd_seq, 0, OVS_VPORT_CMD_DEL);
1876 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001877 ovs_dp_detach_port(vport);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001878 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001879
Johannes Berg2a94fe42013-11-19 15:19:39 +01001880 ovs_notify(&dp_vport_genl_family, reply, info);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001881 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001882
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001883exit_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001884 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001885 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001886 return err;
1887}
1888
1889static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
1890{
1891 struct nlattr **a = info->attrs;
1892 struct ovs_header *ovs_header = info->userhdr;
1893 struct sk_buff *reply;
1894 struct vport *vport;
1895 int err;
1896
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001897 reply = ovs_vport_cmd_alloc_info();
1898 if (!reply)
1899 return -ENOMEM;
1900
Jesse Grossccb13522011-10-25 19:26:31 -07001901 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001902 vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
Jesse Grossccb13522011-10-25 19:26:31 -07001903 err = PTR_ERR(vport);
1904 if (IS_ERR(vport))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001905 goto exit_unlock_free;
1906 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1907 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1908 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001909 rcu_read_unlock();
1910
1911 return genlmsg_reply(reply, info);
1912
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001913exit_unlock_free:
Jesse Grossccb13522011-10-25 19:26:31 -07001914 rcu_read_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001915 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001916 return err;
1917}
1918
1919static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1920{
1921 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1922 struct datapath *dp;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001923 int bucket = cb->args[0], skip = cb->args[1];
1924 int i, j = 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001925
Jesse Grossccb13522011-10-25 19:26:31 -07001926 rcu_read_lock();
Jarno Rajahalme42ee19e2014-02-15 17:42:29 -08001927 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1928 if (!dp) {
1929 rcu_read_unlock();
1930 return -ENODEV;
1931 }
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001932 for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
Jesse Grossccb13522011-10-25 19:26:31 -07001933 struct vport *vport;
1934
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001935 j = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001936 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001937 if (j >= skip &&
1938 ovs_vport_cmd_fill_info(vport, skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001939 NETLINK_CB(cb->skb).portid,
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001940 cb->nlh->nlmsg_seq,
1941 NLM_F_MULTI,
1942 OVS_VPORT_CMD_NEW) < 0)
1943 goto out;
Jesse Grossccb13522011-10-25 19:26:31 -07001944
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001945 j++;
1946 }
1947 skip = 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001948 }
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001949out:
Jesse Grossccb13522011-10-25 19:26:31 -07001950 rcu_read_unlock();
1951
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001952 cb->args[0] = i;
1953 cb->args[1] = j;
Jesse Grossccb13522011-10-25 19:26:31 -07001954
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001955 return skb->len;
Jesse Grossccb13522011-10-25 19:26:31 -07001956}
1957
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001958static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
1959 [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1960 [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
1961 [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
1962 [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
1963 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1964 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
1965};
1966
stephen hemminger48e48a72014-07-16 11:25:52 -07001967static const struct genl_ops dp_vport_genl_ops[] = {
Jesse Grossccb13522011-10-25 19:26:31 -07001968 { .cmd = OVS_VPORT_CMD_NEW,
1969 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1970 .policy = vport_policy,
1971 .doit = ovs_vport_cmd_new
1972 },
1973 { .cmd = OVS_VPORT_CMD_DEL,
1974 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1975 .policy = vport_policy,
1976 .doit = ovs_vport_cmd_del
1977 },
1978 { .cmd = OVS_VPORT_CMD_GET,
1979 .flags = 0, /* OK for unprivileged users. */
1980 .policy = vport_policy,
1981 .doit = ovs_vport_cmd_get,
1982 .dumpit = ovs_vport_cmd_dump
1983 },
1984 { .cmd = OVS_VPORT_CMD_SET,
1985 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1986 .policy = vport_policy,
1987 .doit = ovs_vport_cmd_set,
1988 },
1989};
1990
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001991struct genl_family dp_vport_genl_family = {
1992 .id = GENL_ID_GENERATE,
1993 .hdrsize = sizeof(struct ovs_header),
1994 .name = OVS_VPORT_FAMILY,
1995 .version = OVS_VPORT_VERSION,
1996 .maxattr = OVS_VPORT_ATTR_MAX,
1997 .netnsok = true,
1998 .parallel_ops = true,
1999 .ops = dp_vport_genl_ops,
2000 .n_ops = ARRAY_SIZE(dp_vport_genl_ops),
2001 .mcgrps = &ovs_dp_vport_multicast_group,
2002 .n_mcgrps = 1,
Jesse Grossccb13522011-10-25 19:26:31 -07002003};
2004
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002005static struct genl_family * const dp_genl_families[] = {
2006 &dp_datapath_genl_family,
2007 &dp_vport_genl_family,
2008 &dp_flow_genl_family,
2009 &dp_packet_genl_family,
Jesse Grossccb13522011-10-25 19:26:31 -07002010};
2011
2012static void dp_unregister_genl(int n_families)
2013{
2014 int i;
2015
2016 for (i = 0; i < n_families; i++)
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002017 genl_unregister_family(dp_genl_families[i]);
Jesse Grossccb13522011-10-25 19:26:31 -07002018}
2019
2020static int dp_register_genl(void)
2021{
Jesse Grossccb13522011-10-25 19:26:31 -07002022 int err;
2023 int i;
2024
Jesse Grossccb13522011-10-25 19:26:31 -07002025 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
Jesse Grossccb13522011-10-25 19:26:31 -07002026
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002027 err = genl_register_family(dp_genl_families[i]);
Jesse Grossccb13522011-10-25 19:26:31 -07002028 if (err)
2029 goto error;
Jesse Grossccb13522011-10-25 19:26:31 -07002030 }
2031
2032 return 0;
2033
2034error:
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002035 dp_unregister_genl(i);
Jesse Grossccb13522011-10-25 19:26:31 -07002036 return err;
2037}
2038
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002039static int __net_init ovs_init_net(struct net *net)
2040{
2041 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2042
2043 INIT_LIST_HEAD(&ovs_net->dps);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002044 INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002045 return 0;
2046}
2047
2048static void __net_exit ovs_exit_net(struct net *net)
2049{
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002050 struct datapath *dp, *dp_next;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002051 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002052
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002053 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002054 list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
2055 __dp_destroy(dp);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002056 ovs_unlock();
2057
2058 cancel_work_sync(&ovs_net->dp_notify_work);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002059}
2060
2061static struct pernet_operations ovs_net_ops = {
2062 .init = ovs_init_net,
2063 .exit = ovs_exit_net,
2064 .id = &ovs_net_id,
2065 .size = sizeof(struct ovs_net),
2066};
2067
Jesse Grossccb13522011-10-25 19:26:31 -07002068static int __init dp_init(void)
2069{
Jesse Grossccb13522011-10-25 19:26:31 -07002070 int err;
2071
YOSHIFUJI Hideaki / 吉藤英明3523b292013-01-09 07:19:55 +00002072 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
Jesse Grossccb13522011-10-25 19:26:31 -07002073
2074 pr_info("Open vSwitch switching datapath\n");
2075
Jiri Pirko5b9e7e12014-06-26 09:58:26 +02002076 err = ovs_internal_dev_rtnl_link_register();
Jesse Grossccb13522011-10-25 19:26:31 -07002077 if (err)
2078 goto error;
2079
Jiri Pirko5b9e7e12014-06-26 09:58:26 +02002080 err = ovs_flow_init();
2081 if (err)
2082 goto error_unreg_rtnl_link;
2083
Jesse Grossccb13522011-10-25 19:26:31 -07002084 err = ovs_vport_init();
2085 if (err)
2086 goto error_flow_exit;
2087
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002088 err = register_pernet_device(&ovs_net_ops);
Jesse Grossccb13522011-10-25 19:26:31 -07002089 if (err)
2090 goto error_vport_exit;
2091
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002092 err = register_netdevice_notifier(&ovs_dp_device_notifier);
2093 if (err)
2094 goto error_netns_exit;
2095
Jesse Grossccb13522011-10-25 19:26:31 -07002096 err = dp_register_genl();
2097 if (err < 0)
2098 goto error_unreg_notifier;
2099
Jesse Grossccb13522011-10-25 19:26:31 -07002100 return 0;
2101
2102error_unreg_notifier:
2103 unregister_netdevice_notifier(&ovs_dp_device_notifier);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002104error_netns_exit:
2105 unregister_pernet_device(&ovs_net_ops);
Jesse Grossccb13522011-10-25 19:26:31 -07002106error_vport_exit:
2107 ovs_vport_exit();
2108error_flow_exit:
2109 ovs_flow_exit();
Jiri Pirko5b9e7e12014-06-26 09:58:26 +02002110error_unreg_rtnl_link:
2111 ovs_internal_dev_rtnl_link_unregister();
Jesse Grossccb13522011-10-25 19:26:31 -07002112error:
2113 return err;
2114}
2115
2116static void dp_cleanup(void)
2117{
Jesse Grossccb13522011-10-25 19:26:31 -07002118 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
2119 unregister_netdevice_notifier(&ovs_dp_device_notifier);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002120 unregister_pernet_device(&ovs_net_ops);
2121 rcu_barrier();
Jesse Grossccb13522011-10-25 19:26:31 -07002122 ovs_vport_exit();
2123 ovs_flow_exit();
Jiri Pirko5b9e7e12014-06-26 09:58:26 +02002124 ovs_internal_dev_rtnl_link_unregister();
Jesse Grossccb13522011-10-25 19:26:31 -07002125}
2126
2127module_init(dp_init);
2128module_exit(dp_cleanup);
2129
2130MODULE_DESCRIPTION("Open vSwitch switching datapath");
2131MODULE_LICENSE("GPL");