blob: 35d866f036e7fdd1ec0243bf9fd5d3ee9f93028e [file] [log] [blame]
Jesse Grossccb13522011-10-25 19:26:31 -07001/*
Ben Pfaffad552002014-05-06 16:48:38 -07002 * Copyright (c) 2007-2014 Nicira, Inc.
Jesse Grossccb13522011-10-25 19:26:31 -07003 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of version 2 of the GNU General Public
6 * License as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 * 02110-1301, USA
17 */
18
19#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20
21#include <linux/init.h>
22#include <linux/module.h>
23#include <linux/if_arp.h>
24#include <linux/if_vlan.h>
25#include <linux/in.h>
26#include <linux/ip.h>
27#include <linux/jhash.h>
28#include <linux/delay.h>
29#include <linux/time.h>
30#include <linux/etherdevice.h>
31#include <linux/genetlink.h>
32#include <linux/kernel.h>
33#include <linux/kthread.h>
34#include <linux/mutex.h>
35#include <linux/percpu.h>
36#include <linux/rcupdate.h>
37#include <linux/tcp.h>
38#include <linux/udp.h>
Jesse Grossccb13522011-10-25 19:26:31 -070039#include <linux/ethtool.h>
40#include <linux/wait.h>
Jesse Grossccb13522011-10-25 19:26:31 -070041#include <asm/div64.h>
42#include <linux/highmem.h>
43#include <linux/netfilter_bridge.h>
44#include <linux/netfilter_ipv4.h>
45#include <linux/inetdevice.h>
46#include <linux/list.h>
47#include <linux/openvswitch.h>
48#include <linux/rculist.h>
49#include <linux/dmi.h>
Jesse Grossccb13522011-10-25 19:26:31 -070050#include <net/genetlink.h>
Pravin B Shelar46df7b82012-02-22 19:58:59 -080051#include <net/net_namespace.h>
52#include <net/netns/generic.h>
Jesse Grossccb13522011-10-25 19:26:31 -070053
54#include "datapath.h"
55#include "flow.h"
Andy Zhoue80857c2014-01-21 09:31:04 -080056#include "flow_table.h"
Pravin B Shelare6445712013-10-03 18:16:47 -070057#include "flow_netlink.h"
Jesse Grossccb13522011-10-25 19:26:31 -070058#include "vport-internal_dev.h"
Thomas Grafcff63a52013-04-29 13:06:41 +000059#include "vport-netdev.h"
Jesse Grossccb13522011-10-25 19:26:31 -070060
Pravin B Shelar8e4e1712013-04-15 13:23:03 -070061int ovs_net_id __read_mostly;
62
Pravin B Shelar0c200ef2014-05-06 16:44:50 -070063static struct genl_family dp_packet_genl_family;
64static struct genl_family dp_flow_genl_family;
65static struct genl_family dp_datapath_genl_family;
66
stephen hemminger48e48a72014-07-16 11:25:52 -070067static const struct genl_multicast_group ovs_dp_flow_multicast_group = {
68 .name = OVS_FLOW_MCGROUP,
Pravin B Shelar0c200ef2014-05-06 16:44:50 -070069};
70
stephen hemminger48e48a72014-07-16 11:25:52 -070071static const struct genl_multicast_group ovs_dp_datapath_multicast_group = {
72 .name = OVS_DATAPATH_MCGROUP,
Pravin B Shelar0c200ef2014-05-06 16:44:50 -070073};
74
stephen hemminger48e48a72014-07-16 11:25:52 -070075static const struct genl_multicast_group ovs_dp_vport_multicast_group = {
76 .name = OVS_VPORT_MCGROUP,
Pravin B Shelar0c200ef2014-05-06 16:44:50 -070077};
78
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -070079/* Check if need to build a reply message.
80 * OVS userspace sets the NLM_F_ECHO flag if it needs the reply. */
81static bool ovs_must_notify(struct genl_info *info,
82 const struct genl_multicast_group *grp)
83{
84 return info->nlhdr->nlmsg_flags & NLM_F_ECHO ||
85 netlink_has_listeners(genl_info_net(info)->genl_sock, 0);
86}
87
Johannes Berg68eb5502013-11-19 15:19:38 +010088static void ovs_notify(struct genl_family *family,
Johannes Berg2a94fe42013-11-19 15:19:39 +010089 struct sk_buff *skb, struct genl_info *info)
Thomas Grafed661182013-03-29 14:46:50 +010090{
Johannes Berg68eb5502013-11-19 15:19:38 +010091 genl_notify(family, skb, genl_info_net(info), info->snd_portid,
Johannes Berg2a94fe42013-11-19 15:19:39 +010092 0, info->nlhdr, GFP_KERNEL);
Thomas Grafed661182013-03-29 14:46:50 +010093}
94
Pravin B Shelar46df7b82012-02-22 19:58:59 -080095/**
Jesse Grossccb13522011-10-25 19:26:31 -070096 * DOC: Locking:
97 *
Pravin B Shelar8e4e1712013-04-15 13:23:03 -070098 * All writes e.g. Writes to device state (add/remove datapath, port, set
99 * operations on vports, etc.), Writes to other state (flow table
100 * modifications, set miscellaneous datapath parameters, etc.) are protected
101 * by ovs_lock.
Jesse Grossccb13522011-10-25 19:26:31 -0700102 *
103 * Reads are protected by RCU.
104 *
105 * There are a few special cases (mostly stats) that have their own
106 * synchronization but they nest under all of above and don't interact with
107 * each other.
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700108 *
109 * The RTNL lock nests inside ovs_mutex.
Jesse Grossccb13522011-10-25 19:26:31 -0700110 */
111
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700112static DEFINE_MUTEX(ovs_mutex);
113
114void ovs_lock(void)
115{
116 mutex_lock(&ovs_mutex);
117}
118
119void ovs_unlock(void)
120{
121 mutex_unlock(&ovs_mutex);
122}
123
124#ifdef CONFIG_LOCKDEP
125int lockdep_ovsl_is_held(void)
126{
127 if (debug_locks)
128 return lockdep_is_held(&ovs_mutex);
129 else
130 return 1;
131}
132#endif
133
Jesse Grossccb13522011-10-25 19:26:31 -0700134static struct vport *new_vport(const struct vport_parms *);
Thomas Graf8055a892013-12-13 15:22:20 +0100135static int queue_gso_packets(struct datapath *dp, struct sk_buff *,
Jesse Grossccb13522011-10-25 19:26:31 -0700136 const struct dp_upcall_info *);
Thomas Graf8055a892013-12-13 15:22:20 +0100137static int queue_userspace_packet(struct datapath *dp, struct sk_buff *,
Jesse Grossccb13522011-10-25 19:26:31 -0700138 const struct dp_upcall_info *);
139
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700140/* Must be called with rcu_read_lock or ovs_mutex. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800141static struct datapath *get_dp(struct net *net, int dp_ifindex)
Jesse Grossccb13522011-10-25 19:26:31 -0700142{
143 struct datapath *dp = NULL;
144 struct net_device *dev;
145
146 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800147 dev = dev_get_by_index_rcu(net, dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -0700148 if (dev) {
149 struct vport *vport = ovs_internal_dev_get_vport(dev);
150 if (vport)
151 dp = vport->dp;
152 }
153 rcu_read_unlock();
154
155 return dp;
156}
157
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700158/* Must be called with rcu_read_lock or ovs_mutex. */
Stephen Hemminger443cd882013-12-17 19:22:48 +0000159static const char *ovs_dp_name(const struct datapath *dp)
Jesse Grossccb13522011-10-25 19:26:31 -0700160{
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700161 struct vport *vport = ovs_vport_ovsl_rcu(dp, OVSP_LOCAL);
Jesse Grossccb13522011-10-25 19:26:31 -0700162 return vport->ops->get_name(vport);
163}
164
165static int get_dpifindex(struct datapath *dp)
166{
167 struct vport *local;
168 int ifindex;
169
170 rcu_read_lock();
171
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700172 local = ovs_vport_rcu(dp, OVSP_LOCAL);
Jesse Grossccb13522011-10-25 19:26:31 -0700173 if (local)
Thomas Grafcff63a52013-04-29 13:06:41 +0000174 ifindex = netdev_vport_priv(local)->dev->ifindex;
Jesse Grossccb13522011-10-25 19:26:31 -0700175 else
176 ifindex = 0;
177
178 rcu_read_unlock();
179
180 return ifindex;
181}
182
183static void destroy_dp_rcu(struct rcu_head *rcu)
184{
185 struct datapath *dp = container_of(rcu, struct datapath, rcu);
186
Jesse Grossccb13522011-10-25 19:26:31 -0700187 free_percpu(dp->stats_percpu);
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800188 release_net(ovs_dp_get_net(dp));
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700189 kfree(dp->ports);
Jesse Grossccb13522011-10-25 19:26:31 -0700190 kfree(dp);
191}
192
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700193static struct hlist_head *vport_hash_bucket(const struct datapath *dp,
194 u16 port_no)
195{
196 return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)];
197}
198
Jarno Rajahalmebb6f9a72014-05-05 11:32:17 -0700199/* Called with ovs_mutex or RCU read lock. */
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700200struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no)
201{
202 struct vport *vport;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700203 struct hlist_head *head;
204
205 head = vport_hash_bucket(dp, port_no);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800206 hlist_for_each_entry_rcu(vport, head, dp_hash_node) {
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700207 if (vport->port_no == port_no)
208 return vport;
209 }
210 return NULL;
211}
212
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700213/* Called with ovs_mutex. */
Jesse Grossccb13522011-10-25 19:26:31 -0700214static struct vport *new_vport(const struct vport_parms *parms)
215{
216 struct vport *vport;
217
218 vport = ovs_vport_add(parms);
219 if (!IS_ERR(vport)) {
220 struct datapath *dp = parms->dp;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700221 struct hlist_head *head = vport_hash_bucket(dp, vport->port_no);
Jesse Grossccb13522011-10-25 19:26:31 -0700222
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700223 hlist_add_head_rcu(&vport->dp_hash_node, head);
Jesse Grossccb13522011-10-25 19:26:31 -0700224 }
Jesse Grossccb13522011-10-25 19:26:31 -0700225 return vport;
226}
227
Jesse Grossccb13522011-10-25 19:26:31 -0700228void ovs_dp_detach_port(struct vport *p)
229{
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700230 ASSERT_OVSL();
Jesse Grossccb13522011-10-25 19:26:31 -0700231
232 /* First drop references to device. */
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700233 hlist_del_rcu(&p->dp_hash_node);
Jesse Grossccb13522011-10-25 19:26:31 -0700234
235 /* Then destroy it. */
236 ovs_vport_del(p);
237}
238
239/* Must be called with rcu_read_lock. */
240void ovs_dp_process_received_packet(struct vport *p, struct sk_buff *skb)
241{
242 struct datapath *dp = p->dp;
243 struct sw_flow *flow;
244 struct dp_stats_percpu *stats;
245 struct sw_flow_key key;
246 u64 *stats_counter;
Andy Zhou1bd71162013-10-22 10:42:46 -0700247 u32 n_mask_hit;
Jesse Grossccb13522011-10-25 19:26:31 -0700248 int error;
Jesse Grossccb13522011-10-25 19:26:31 -0700249
Shan Wei404f2f12012-11-13 09:52:25 +0800250 stats = this_cpu_ptr(dp->stats_percpu);
Jesse Grossccb13522011-10-25 19:26:31 -0700251
252 /* Extract flow from 'skb' into 'key'. */
Andy Zhou03f0d912013-08-07 20:01:00 -0700253 error = ovs_flow_extract(skb, p->port_no, &key);
Jesse Grossccb13522011-10-25 19:26:31 -0700254 if (unlikely(error)) {
255 kfree_skb(skb);
256 return;
257 }
258
259 /* Look up flow. */
Andy Zhou5bb50632013-11-25 10:42:46 -0800260 flow = ovs_flow_tbl_lookup_stats(&dp->table, &key, &n_mask_hit);
Jesse Grossccb13522011-10-25 19:26:31 -0700261 if (unlikely(!flow)) {
262 struct dp_upcall_info upcall;
263
264 upcall.cmd = OVS_PACKET_CMD_MISS;
265 upcall.key = &key;
266 upcall.userdata = NULL;
Alex Wang5cd667b2014-07-17 15:14:13 -0700267 upcall.portid = ovs_vport_find_upcall_portid(p, skb);
Jesse Grossccb13522011-10-25 19:26:31 -0700268 ovs_dp_upcall(dp, skb, &upcall);
269 consume_skb(skb);
270 stats_counter = &stats->n_missed;
271 goto out;
272 }
273
274 OVS_CB(skb)->flow = flow;
Andy Zhou03f0d912013-08-07 20:01:00 -0700275 OVS_CB(skb)->pkt_key = &key;
Jesse Grossccb13522011-10-25 19:26:31 -0700276
Ben Pfaffad552002014-05-06 16:48:38 -0700277 ovs_flow_stats_update(OVS_CB(skb)->flow, key.tp.flags, skb);
Jesse Grossccb13522011-10-25 19:26:31 -0700278 ovs_execute_actions(dp, skb);
Pravin B Shelare298e502013-10-29 17:22:21 -0700279 stats_counter = &stats->n_hit;
Jesse Grossccb13522011-10-25 19:26:31 -0700280
281out:
282 /* Update datapath statistics. */
WANG Congdf9d9fd2014-02-14 15:10:46 -0800283 u64_stats_update_begin(&stats->syncp);
Jesse Grossccb13522011-10-25 19:26:31 -0700284 (*stats_counter)++;
Andy Zhou1bd71162013-10-22 10:42:46 -0700285 stats->n_mask_hit += n_mask_hit;
WANG Congdf9d9fd2014-02-14 15:10:46 -0800286 u64_stats_update_end(&stats->syncp);
Jesse Grossccb13522011-10-25 19:26:31 -0700287}
288
Jesse Grossccb13522011-10-25 19:26:31 -0700289int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800290 const struct dp_upcall_info *upcall_info)
Jesse Grossccb13522011-10-25 19:26:31 -0700291{
292 struct dp_stats_percpu *stats;
Jesse Grossccb13522011-10-25 19:26:31 -0700293 int err;
294
Eric W. Biederman15e47302012-09-07 20:12:54 +0000295 if (upcall_info->portid == 0) {
Jesse Grossccb13522011-10-25 19:26:31 -0700296 err = -ENOTCONN;
297 goto err;
298 }
299
Jesse Grossccb13522011-10-25 19:26:31 -0700300 if (!skb_is_gso(skb))
Thomas Graf8055a892013-12-13 15:22:20 +0100301 err = queue_userspace_packet(dp, skb, upcall_info);
Jesse Grossccb13522011-10-25 19:26:31 -0700302 else
Thomas Graf8055a892013-12-13 15:22:20 +0100303 err = queue_gso_packets(dp, skb, upcall_info);
Jesse Grossccb13522011-10-25 19:26:31 -0700304 if (err)
305 goto err;
306
307 return 0;
308
309err:
Shan Wei404f2f12012-11-13 09:52:25 +0800310 stats = this_cpu_ptr(dp->stats_percpu);
Jesse Grossccb13522011-10-25 19:26:31 -0700311
WANG Congdf9d9fd2014-02-14 15:10:46 -0800312 u64_stats_update_begin(&stats->syncp);
Jesse Grossccb13522011-10-25 19:26:31 -0700313 stats->n_lost++;
WANG Congdf9d9fd2014-02-14 15:10:46 -0800314 u64_stats_update_end(&stats->syncp);
Jesse Grossccb13522011-10-25 19:26:31 -0700315
316 return err;
317}
318
Thomas Graf8055a892013-12-13 15:22:20 +0100319static int queue_gso_packets(struct datapath *dp, struct sk_buff *skb,
Jesse Grossccb13522011-10-25 19:26:31 -0700320 const struct dp_upcall_info *upcall_info)
321{
Ben Pfaffa1b5d0d2012-07-20 14:47:54 -0700322 unsigned short gso_type = skb_shinfo(skb)->gso_type;
Jesse Grossccb13522011-10-25 19:26:31 -0700323 struct dp_upcall_info later_info;
324 struct sw_flow_key later_key;
325 struct sk_buff *segs, *nskb;
326 int err;
327
Thomas Graf09c5e602013-12-13 15:22:22 +0100328 segs = __skb_gso_segment(skb, NETIF_F_SG, false);
Pravin B Shelar92e5dfc2012-07-20 14:46:29 -0700329 if (IS_ERR(segs))
330 return PTR_ERR(segs);
Jesse Grossccb13522011-10-25 19:26:31 -0700331
332 /* Queue all of the segments. */
333 skb = segs;
334 do {
Thomas Graf8055a892013-12-13 15:22:20 +0100335 err = queue_userspace_packet(dp, skb, upcall_info);
Jesse Grossccb13522011-10-25 19:26:31 -0700336 if (err)
337 break;
338
Ben Pfaffa1b5d0d2012-07-20 14:47:54 -0700339 if (skb == segs && gso_type & SKB_GSO_UDP) {
Jesse Grossccb13522011-10-25 19:26:31 -0700340 /* The initial flow key extracted by ovs_flow_extract()
341 * in this case is for a first fragment, so we need to
342 * properly mark later fragments.
343 */
344 later_key = *upcall_info->key;
345 later_key.ip.frag = OVS_FRAG_TYPE_LATER;
346
347 later_info = *upcall_info;
348 later_info.key = &later_key;
349 upcall_info = &later_info;
350 }
351 } while ((skb = skb->next));
352
353 /* Free all of the segments. */
354 skb = segs;
355 do {
356 nskb = skb->next;
357 if (err)
358 kfree_skb(skb);
359 else
360 consume_skb(skb);
361 } while ((skb = nskb));
362 return err;
363}
364
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100365static size_t key_attr_size(void)
366{
367 return nla_total_size(4) /* OVS_KEY_ATTR_PRIORITY */
Pravin B Shelar7d5437c2013-06-17 17:50:18 -0700368 + nla_total_size(0) /* OVS_KEY_ATTR_TUNNEL */
369 + nla_total_size(8) /* OVS_TUNNEL_KEY_ATTR_ID */
370 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_SRC */
371 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_DST */
372 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TOS */
373 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TTL */
374 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT */
375 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_CSUM */
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100376 + nla_total_size(4) /* OVS_KEY_ATTR_IN_PORT */
377 + nla_total_size(4) /* OVS_KEY_ATTR_SKB_MARK */
378 + nla_total_size(12) /* OVS_KEY_ATTR_ETHERNET */
379 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
380 + nla_total_size(4) /* OVS_KEY_ATTR_8021Q */
381 + nla_total_size(0) /* OVS_KEY_ATTR_ENCAP */
382 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
383 + nla_total_size(40) /* OVS_KEY_ATTR_IPV6 */
384 + nla_total_size(2) /* OVS_KEY_ATTR_ICMPV6 */
385 + nla_total_size(28); /* OVS_KEY_ATTR_ND */
386}
387
Thomas Grafbda56f12013-12-13 15:22:21 +0100388static size_t upcall_msg_size(const struct nlattr *userdata,
389 unsigned int hdrlen)
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100390{
391 size_t size = NLMSG_ALIGN(sizeof(struct ovs_header))
Thomas Grafbda56f12013-12-13 15:22:21 +0100392 + nla_total_size(hdrlen) /* OVS_PACKET_ATTR_PACKET */
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100393 + nla_total_size(key_attr_size()); /* OVS_PACKET_ATTR_KEY */
394
395 /* OVS_PACKET_ATTR_USERDATA */
396 if (userdata)
397 size += NLA_ALIGN(userdata->nla_len);
398
399 return size;
400}
401
Thomas Graf8055a892013-12-13 15:22:20 +0100402static int queue_userspace_packet(struct datapath *dp, struct sk_buff *skb,
Jesse Grossccb13522011-10-25 19:26:31 -0700403 const struct dp_upcall_info *upcall_info)
404{
405 struct ovs_header *upcall;
406 struct sk_buff *nskb = NULL;
Li RongQing4ee45ea2014-09-02 20:52:28 +0800407 struct sk_buff *user_skb = NULL; /* to be queued to userspace */
Jesse Grossccb13522011-10-25 19:26:31 -0700408 struct nlattr *nla;
Thomas Graf795449d2013-11-30 13:21:32 +0100409 struct genl_info info = {
Thomas Graf8055a892013-12-13 15:22:20 +0100410 .dst_sk = ovs_dp_get_net(dp)->genl_sock,
Thomas Graf795449d2013-11-30 13:21:32 +0100411 .snd_portid = upcall_info->portid,
412 };
413 size_t len;
Thomas Grafbda56f12013-12-13 15:22:21 +0100414 unsigned int hlen;
Thomas Graf8055a892013-12-13 15:22:20 +0100415 int err, dp_ifindex;
416
417 dp_ifindex = get_dpifindex(dp);
418 if (!dp_ifindex)
419 return -ENODEV;
Jesse Grossccb13522011-10-25 19:26:31 -0700420
421 if (vlan_tx_tag_present(skb)) {
422 nskb = skb_clone(skb, GFP_ATOMIC);
423 if (!nskb)
424 return -ENOMEM;
425
Patrick McHardy86a9bad2013-04-19 02:04:30 +0000426 nskb = __vlan_put_tag(nskb, nskb->vlan_proto, vlan_tx_tag_get(nskb));
Dan Carpenter8aa51d62012-05-13 08:44:18 +0000427 if (!nskb)
Jesse Grossccb13522011-10-25 19:26:31 -0700428 return -ENOMEM;
429
430 nskb->vlan_tci = 0;
431 skb = nskb;
432 }
433
434 if (nla_attr_size(skb->len) > USHRT_MAX) {
435 err = -EFBIG;
436 goto out;
437 }
438
Thomas Grafbda56f12013-12-13 15:22:21 +0100439 /* Complete checksum if needed */
440 if (skb->ip_summed == CHECKSUM_PARTIAL &&
441 (err = skb_checksum_help(skb)))
442 goto out;
443
444 /* Older versions of OVS user space enforce alignment of the last
445 * Netlink attribute to NLA_ALIGNTO which would require extensive
446 * padding logic. Only perform zerocopy if padding is not required.
447 */
448 if (dp->user_features & OVS_DP_F_UNALIGNED)
449 hlen = skb_zerocopy_headlen(skb);
450 else
451 hlen = skb->len;
452
453 len = upcall_msg_size(upcall_info->userdata, hlen);
Thomas Graf795449d2013-11-30 13:21:32 +0100454 user_skb = genlmsg_new_unicast(len, &info, GFP_ATOMIC);
Jesse Grossccb13522011-10-25 19:26:31 -0700455 if (!user_skb) {
456 err = -ENOMEM;
457 goto out;
458 }
459
460 upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
461 0, upcall_info->cmd);
462 upcall->dp_ifindex = dp_ifindex;
463
464 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
Andy Zhouf53e3832014-07-17 15:17:44 -0700465 err = ovs_nla_put_flow(upcall_info->key, upcall_info->key, user_skb);
466 BUG_ON(err);
Jesse Grossccb13522011-10-25 19:26:31 -0700467 nla_nest_end(user_skb, nla);
468
469 if (upcall_info->userdata)
Ben Pfaff44901082013-02-15 17:29:22 -0800470 __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA,
471 nla_len(upcall_info->userdata),
472 nla_data(upcall_info->userdata));
Jesse Grossccb13522011-10-25 19:26:31 -0700473
Thomas Grafbda56f12013-12-13 15:22:21 +0100474 /* Only reserve room for attribute header, packet data is added
475 * in skb_zerocopy() */
476 if (!(nla = nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, 0))) {
477 err = -ENOBUFS;
478 goto out;
479 }
480 nla->nla_len = nla_attr_size(skb->len);
Jesse Grossccb13522011-10-25 19:26:31 -0700481
Zoltan Kiss36d5fe62014-03-26 22:37:45 +0000482 err = skb_zerocopy(user_skb, skb, skb->len, hlen);
483 if (err)
484 goto out;
Jesse Grossccb13522011-10-25 19:26:31 -0700485
Thomas Grafaea0bb42014-01-14 16:27:49 +0000486 /* Pad OVS_PACKET_ATTR_PACKET if linear copy was performed */
487 if (!(dp->user_features & OVS_DP_F_UNALIGNED)) {
488 size_t plen = NLA_ALIGN(user_skb->len) - user_skb->len;
489
490 if (plen > 0)
491 memset(skb_put(user_skb, plen), 0, plen);
492 }
493
Thomas Grafbda56f12013-12-13 15:22:21 +0100494 ((struct nlmsghdr *) user_skb->data)->nlmsg_len = user_skb->len;
495
Thomas Graf8055a892013-12-13 15:22:20 +0100496 err = genlmsg_unicast(ovs_dp_get_net(dp), user_skb, upcall_info->portid);
Li RongQing4ee45ea2014-09-02 20:52:28 +0800497 user_skb = NULL;
Jesse Grossccb13522011-10-25 19:26:31 -0700498out:
Zoltan Kiss36d5fe62014-03-26 22:37:45 +0000499 if (err)
500 skb_tx_error(skb);
Li RongQing4ee45ea2014-09-02 20:52:28 +0800501 kfree_skb(user_skb);
Jesse Grossccb13522011-10-25 19:26:31 -0700502 kfree_skb(nskb);
503 return err;
504}
505
Jesse Grossccb13522011-10-25 19:26:31 -0700506static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
507{
508 struct ovs_header *ovs_header = info->userhdr;
509 struct nlattr **a = info->attrs;
510 struct sw_flow_actions *acts;
511 struct sk_buff *packet;
512 struct sw_flow *flow;
513 struct datapath *dp;
514 struct ethhdr *eth;
515 int len;
516 int err;
Jesse Grossccb13522011-10-25 19:26:31 -0700517
518 err = -EINVAL;
519 if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
Thomas Grafdded45fc2013-03-29 14:46:47 +0100520 !a[OVS_PACKET_ATTR_ACTIONS])
Jesse Grossccb13522011-10-25 19:26:31 -0700521 goto err;
522
523 len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
524 packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
525 err = -ENOMEM;
526 if (!packet)
527 goto err;
528 skb_reserve(packet, NET_IP_ALIGN);
529
Thomas Graf32686a92013-03-29 14:46:48 +0100530 nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
Jesse Grossccb13522011-10-25 19:26:31 -0700531
532 skb_reset_mac_header(packet);
533 eth = eth_hdr(packet);
534
535 /* Normally, setting the skb 'protocol' field would be handled by a
536 * call to eth_type_trans(), but it assumes there's a sending
537 * device, which we may not have. */
Simon Hormane5c5d222013-03-28 13:38:25 +0900538 if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN)
Jesse Grossccb13522011-10-25 19:26:31 -0700539 packet->protocol = eth->h_proto;
540 else
541 packet->protocol = htons(ETH_P_802_2);
542
543 /* Build an sw_flow for sending this packet. */
Jarno Rajahalme23dabf82014-03-27 12:35:23 -0700544 flow = ovs_flow_alloc();
Jesse Grossccb13522011-10-25 19:26:31 -0700545 err = PTR_ERR(flow);
546 if (IS_ERR(flow))
547 goto err_kfree_skb;
548
Andy Zhou03f0d912013-08-07 20:01:00 -0700549 err = ovs_flow_extract(packet, -1, &flow->key);
Jesse Grossccb13522011-10-25 19:26:31 -0700550 if (err)
551 goto err_flow_free;
552
Pravin B Shelare6445712013-10-03 18:16:47 -0700553 err = ovs_nla_get_flow_metadata(flow, a[OVS_PACKET_ATTR_KEY]);
Jesse Grossccb13522011-10-25 19:26:31 -0700554 if (err)
555 goto err_flow_free;
Pravin B Shelare6445712013-10-03 18:16:47 -0700556 acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_PACKET_ATTR_ACTIONS]));
Jesse Grossccb13522011-10-25 19:26:31 -0700557 err = PTR_ERR(acts);
558 if (IS_ERR(acts))
559 goto err_flow_free;
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700560
Pravin B Shelare6445712013-10-03 18:16:47 -0700561 err = ovs_nla_copy_actions(a[OVS_PACKET_ATTR_ACTIONS],
562 &flow->key, 0, &acts);
Jesse Grossccb13522011-10-25 19:26:31 -0700563 rcu_assign_pointer(flow->sf_acts, acts);
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700564 if (err)
565 goto err_flow_free;
Jesse Grossccb13522011-10-25 19:26:31 -0700566
567 OVS_CB(packet)->flow = flow;
Andy Zhou03f0d912013-08-07 20:01:00 -0700568 OVS_CB(packet)->pkt_key = &flow->key;
Jesse Grossccb13522011-10-25 19:26:31 -0700569 packet->priority = flow->key.phy.priority;
Ansis Atteka39c7caeb2012-11-26 11:24:11 -0800570 packet->mark = flow->key.phy.skb_mark;
Jesse Grossccb13522011-10-25 19:26:31 -0700571
572 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800573 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -0700574 err = -ENODEV;
575 if (!dp)
576 goto err_unlock;
577
578 local_bh_disable();
579 err = ovs_execute_actions(dp, packet);
580 local_bh_enable();
581 rcu_read_unlock();
582
Andy Zhou03f0d912013-08-07 20:01:00 -0700583 ovs_flow_free(flow, false);
Jesse Grossccb13522011-10-25 19:26:31 -0700584 return err;
585
586err_unlock:
587 rcu_read_unlock();
588err_flow_free:
Andy Zhou03f0d912013-08-07 20:01:00 -0700589 ovs_flow_free(flow, false);
Jesse Grossccb13522011-10-25 19:26:31 -0700590err_kfree_skb:
591 kfree_skb(packet);
592err:
593 return err;
594}
595
596static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
Thomas Grafdded45fc2013-03-29 14:46:47 +0100597 [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
Jesse Grossccb13522011-10-25 19:26:31 -0700598 [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
599 [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
600};
601
Johannes Berg4534de82013-11-14 17:14:46 +0100602static const struct genl_ops dp_packet_genl_ops[] = {
Jesse Grossccb13522011-10-25 19:26:31 -0700603 { .cmd = OVS_PACKET_CMD_EXECUTE,
604 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
605 .policy = packet_policy,
606 .doit = ovs_packet_cmd_execute
607 }
608};
609
Pravin B Shelar0c200ef2014-05-06 16:44:50 -0700610static struct genl_family dp_packet_genl_family = {
611 .id = GENL_ID_GENERATE,
612 .hdrsize = sizeof(struct ovs_header),
613 .name = OVS_PACKET_FAMILY,
614 .version = OVS_PACKET_VERSION,
615 .maxattr = OVS_PACKET_ATTR_MAX,
616 .netnsok = true,
617 .parallel_ops = true,
618 .ops = dp_packet_genl_ops,
619 .n_ops = ARRAY_SIZE(dp_packet_genl_ops),
620};
621
Andy Zhou1bd71162013-10-22 10:42:46 -0700622static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats,
623 struct ovs_dp_megaflow_stats *mega_stats)
Jesse Grossccb13522011-10-25 19:26:31 -0700624{
625 int i;
Jesse Grossccb13522011-10-25 19:26:31 -0700626
Andy Zhou1bd71162013-10-22 10:42:46 -0700627 memset(mega_stats, 0, sizeof(*mega_stats));
628
Pravin B Shelarb637e492013-10-04 00:14:23 -0700629 stats->n_flows = ovs_flow_tbl_count(&dp->table);
Andy Zhou1bd71162013-10-22 10:42:46 -0700630 mega_stats->n_masks = ovs_flow_tbl_num_masks(&dp->table);
Jesse Grossccb13522011-10-25 19:26:31 -0700631
632 stats->n_hit = stats->n_missed = stats->n_lost = 0;
Andy Zhou1bd71162013-10-22 10:42:46 -0700633
Jesse Grossccb13522011-10-25 19:26:31 -0700634 for_each_possible_cpu(i) {
635 const struct dp_stats_percpu *percpu_stats;
636 struct dp_stats_percpu local_stats;
637 unsigned int start;
638
639 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
640
641 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700642 start = u64_stats_fetch_begin_irq(&percpu_stats->syncp);
Jesse Grossccb13522011-10-25 19:26:31 -0700643 local_stats = *percpu_stats;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700644 } while (u64_stats_fetch_retry_irq(&percpu_stats->syncp, start));
Jesse Grossccb13522011-10-25 19:26:31 -0700645
646 stats->n_hit += local_stats.n_hit;
647 stats->n_missed += local_stats.n_missed;
648 stats->n_lost += local_stats.n_lost;
Andy Zhou1bd71162013-10-22 10:42:46 -0700649 mega_stats->n_mask_hit += local_stats.n_mask_hit;
Jesse Grossccb13522011-10-25 19:26:31 -0700650 }
651}
652
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100653static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts)
654{
655 return NLMSG_ALIGN(sizeof(struct ovs_header))
656 + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_KEY */
Andy Zhou03f0d912013-08-07 20:01:00 -0700657 + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_MASK */
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100658 + nla_total_size(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
659 + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
660 + nla_total_size(8) /* OVS_FLOW_ATTR_USED */
661 + nla_total_size(acts->actions_len); /* OVS_FLOW_ATTR_ACTIONS */
662}
663
Jarno Rajahalmebb6f9a72014-05-05 11:32:17 -0700664/* Called with ovs_mutex or RCU read lock. */
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700665static int ovs_flow_cmd_fill_info(const struct sw_flow *flow, int dp_ifindex,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000666 struct sk_buff *skb, u32 portid,
Jesse Grossccb13522011-10-25 19:26:31 -0700667 u32 seq, u32 flags, u8 cmd)
668{
669 const int skb_orig_len = skb->len;
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700670 struct nlattr *start;
Jesse Grossccb13522011-10-25 19:26:31 -0700671 struct ovs_flow_stats stats;
Pravin B Shelare298e502013-10-29 17:22:21 -0700672 __be16 tcp_flags;
673 unsigned long used;
Jesse Grossccb13522011-10-25 19:26:31 -0700674 struct ovs_header *ovs_header;
675 struct nlattr *nla;
Jesse Grossccb13522011-10-25 19:26:31 -0700676 int err;
677
Eric W. Biederman15e47302012-09-07 20:12:54 +0000678 ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family, flags, cmd);
Jesse Grossccb13522011-10-25 19:26:31 -0700679 if (!ovs_header)
680 return -EMSGSIZE;
681
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700682 ovs_header->dp_ifindex = dp_ifindex;
Jesse Grossccb13522011-10-25 19:26:31 -0700683
Andy Zhou03f0d912013-08-07 20:01:00 -0700684 /* Fill flow key. */
Jesse Grossccb13522011-10-25 19:26:31 -0700685 nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
686 if (!nla)
687 goto nla_put_failure;
Andy Zhou03f0d912013-08-07 20:01:00 -0700688
Pravin B Shelare6445712013-10-03 18:16:47 -0700689 err = ovs_nla_put_flow(&flow->unmasked_key, &flow->unmasked_key, skb);
Jesse Grossccb13522011-10-25 19:26:31 -0700690 if (err)
691 goto error;
692 nla_nest_end(skb, nla);
693
Andy Zhou03f0d912013-08-07 20:01:00 -0700694 nla = nla_nest_start(skb, OVS_FLOW_ATTR_MASK);
695 if (!nla)
696 goto nla_put_failure;
697
Pravin B Shelare6445712013-10-03 18:16:47 -0700698 err = ovs_nla_put_flow(&flow->key, &flow->mask->key, skb);
Andy Zhou03f0d912013-08-07 20:01:00 -0700699 if (err)
700 goto error;
701
702 nla_nest_end(skb, nla);
703
Pravin B Shelare298e502013-10-29 17:22:21 -0700704 ovs_flow_stats_get(flow, &stats, &used, &tcp_flags);
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700705
David S. Miller028d6a62012-03-29 23:20:48 -0400706 if (used &&
707 nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
708 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -0700709
David S. Miller028d6a62012-03-29 23:20:48 -0400710 if (stats.n_packets &&
Pravin B Shelare298e502013-10-29 17:22:21 -0700711 nla_put(skb, OVS_FLOW_ATTR_STATS, sizeof(struct ovs_flow_stats), &stats))
David S. Miller028d6a62012-03-29 23:20:48 -0400712 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -0700713
Pravin B Shelare298e502013-10-29 17:22:21 -0700714 if ((u8)ntohs(tcp_flags) &&
715 nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, (u8)ntohs(tcp_flags)))
David S. Miller028d6a62012-03-29 23:20:48 -0400716 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -0700717
718 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
719 * this is the first flow to be dumped into 'skb'. This is unusual for
720 * Netlink but individual action lists can be longer than
721 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
722 * The userspace caller can always fetch the actions separately if it
723 * really wants them. (Most userspace callers in fact don't care.)
724 *
725 * This can only fail for dump operations because the skb is always
726 * properly sized for single flows.
727 */
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700728 start = nla_nest_start(skb, OVS_FLOW_ATTR_ACTIONS);
729 if (start) {
Pravin B Shelard57170b2013-07-30 15:39:39 -0700730 const struct sw_flow_actions *sf_acts;
731
Jesse Gross663efa32013-12-03 10:58:53 -0800732 sf_acts = rcu_dereference_ovsl(flow->sf_acts);
Pravin B Shelare6445712013-10-03 18:16:47 -0700733 err = ovs_nla_put_actions(sf_acts->actions,
734 sf_acts->actions_len, skb);
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700735
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700736 if (!err)
737 nla_nest_end(skb, start);
738 else {
739 if (skb_orig_len)
740 goto error;
741
742 nla_nest_cancel(skb, start);
743 }
744 } else if (skb_orig_len)
745 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -0700746
747 return genlmsg_end(skb, ovs_header);
748
749nla_put_failure:
750 err = -EMSGSIZE;
751error:
752 genlmsg_cancel(skb, ovs_header);
753 return err;
754}
755
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700756/* May not be called with RCU read lock. */
757static struct sk_buff *ovs_flow_cmd_alloc_info(const struct sw_flow_actions *acts,
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700758 struct genl_info *info,
759 bool always)
Jesse Grossccb13522011-10-25 19:26:31 -0700760{
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700761 struct sk_buff *skb;
Jesse Grossccb13522011-10-25 19:26:31 -0700762
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700763 if (!always && !ovs_must_notify(info, &ovs_dp_flow_multicast_group))
764 return NULL;
765
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700766 skb = genlmsg_new_unicast(ovs_flow_cmd_msg_size(acts), info, GFP_KERNEL);
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700767 if (!skb)
768 return ERR_PTR(-ENOMEM);
769
770 return skb;
Jesse Grossccb13522011-10-25 19:26:31 -0700771}
772
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700773/* Called with ovs_mutex. */
774static struct sk_buff *ovs_flow_cmd_build_info(const struct sw_flow *flow,
775 int dp_ifindex,
776 struct genl_info *info, u8 cmd,
777 bool always)
Jesse Grossccb13522011-10-25 19:26:31 -0700778{
779 struct sk_buff *skb;
780 int retval;
781
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700782 skb = ovs_flow_cmd_alloc_info(ovsl_dereference(flow->sf_acts), info,
783 always);
Himangi Saraogid0e992a2014-07-27 12:37:46 +0530784 if (IS_ERR_OR_NULL(skb))
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700785 return skb;
Jesse Grossccb13522011-10-25 19:26:31 -0700786
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700787 retval = ovs_flow_cmd_fill_info(flow, dp_ifindex, skb,
788 info->snd_portid, info->snd_seq, 0,
789 cmd);
Jesse Grossccb13522011-10-25 19:26:31 -0700790 BUG_ON(retval < 0);
791 return skb;
792}
793
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700794static int ovs_flow_cmd_new(struct sk_buff *skb, struct genl_info *info)
Jesse Grossccb13522011-10-25 19:26:31 -0700795{
796 struct nlattr **a = info->attrs;
797 struct ovs_header *ovs_header = info->userhdr;
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700798 struct sw_flow *flow, *new_flow;
Andy Zhou03f0d912013-08-07 20:01:00 -0700799 struct sw_flow_mask mask;
Jesse Grossccb13522011-10-25 19:26:31 -0700800 struct sk_buff *reply;
801 struct datapath *dp;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700802 struct sw_flow_actions *acts;
803 struct sw_flow_match match;
804 int error;
805
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700806 /* Must have key and actions. */
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700807 error = -EINVAL;
808 if (!a[OVS_FLOW_ATTR_KEY])
809 goto error;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700810 if (!a[OVS_FLOW_ATTR_ACTIONS])
811 goto error;
812
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700813 /* Most of the time we need to allocate a new flow, do it before
814 * locking.
815 */
816 new_flow = ovs_flow_alloc();
817 if (IS_ERR(new_flow)) {
818 error = PTR_ERR(new_flow);
819 goto error;
820 }
821
822 /* Extract key. */
823 ovs_match_init(&match, &new_flow->unmasked_key, &mask);
824 error = ovs_nla_get_match(&match,
825 a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
826 if (error)
827 goto err_kfree_flow;
828
829 ovs_flow_mask_key(&new_flow->key, &new_flow->unmasked_key, &mask);
830
831 /* Validate actions. */
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700832 acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_FLOW_ATTR_ACTIONS]));
833 error = PTR_ERR(acts);
834 if (IS_ERR(acts))
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700835 goto err_kfree_flow;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700836
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700837 error = ovs_nla_copy_actions(a[OVS_FLOW_ATTR_ACTIONS], &new_flow->key,
838 0, &acts);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700839 if (error) {
840 OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700841 goto err_kfree_acts;
842 }
843
844 reply = ovs_flow_cmd_alloc_info(acts, info, false);
845 if (IS_ERR(reply)) {
846 error = PTR_ERR(reply);
847 goto err_kfree_acts;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700848 }
849
850 ovs_lock();
851 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700852 if (unlikely(!dp)) {
853 error = -ENODEV;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700854 goto err_unlock_ovs;
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700855 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700856 /* Check if this is a duplicate flow */
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700857 flow = ovs_flow_tbl_lookup(&dp->table, &new_flow->unmasked_key);
858 if (likely(!flow)) {
859 rcu_assign_pointer(new_flow->sf_acts, acts);
860
861 /* Put flow in bucket. */
862 error = ovs_flow_tbl_insert(&dp->table, new_flow, &mask);
863 if (unlikely(error)) {
864 acts = NULL;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700865 goto err_unlock_ovs;
866 }
867
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700868 if (unlikely(reply)) {
869 error = ovs_flow_cmd_fill_info(new_flow,
870 ovs_header->dp_ifindex,
871 reply, info->snd_portid,
872 info->snd_seq, 0,
873 OVS_FLOW_CMD_NEW);
874 BUG_ON(error < 0);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700875 }
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700876 ovs_unlock();
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700877 } else {
878 struct sw_flow_actions *old_acts;
879
880 /* Bail out if we're not allowed to modify an existing flow.
881 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
882 * because Generic Netlink treats the latter as a dump
883 * request. We also accept NLM_F_EXCL in case that bug ever
884 * gets fixed.
885 */
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700886 if (unlikely(info->nlhdr->nlmsg_flags & (NLM_F_CREATE
887 | NLM_F_EXCL))) {
888 error = -EEXIST;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700889 goto err_unlock_ovs;
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700890 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700891 /* The unmasked key has to be the same for flow updates. */
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700892 if (unlikely(!ovs_flow_cmp_unmasked_key(flow, &match))) {
Alex Wang4a46b242014-06-30 20:30:29 -0700893 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
894 if (!flow) {
895 error = -ENOENT;
896 goto err_unlock_ovs;
897 }
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700898 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700899 /* Update actions. */
900 old_acts = ovsl_dereference(flow->sf_acts);
901 rcu_assign_pointer(flow->sf_acts, acts);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700902
903 if (unlikely(reply)) {
904 error = ovs_flow_cmd_fill_info(flow,
905 ovs_header->dp_ifindex,
906 reply, info->snd_portid,
907 info->snd_seq, 0,
908 OVS_FLOW_CMD_NEW);
909 BUG_ON(error < 0);
910 }
911 ovs_unlock();
912
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700913 ovs_nla_free_flow_actions(old_acts);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700914 ovs_flow_free(new_flow, false);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700915 }
916
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700917 if (reply)
918 ovs_notify(&dp_flow_genl_family, reply, info);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700919 return 0;
920
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700921err_unlock_ovs:
922 ovs_unlock();
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700923 kfree_skb(reply);
924err_kfree_acts:
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700925 kfree(acts);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700926err_kfree_flow:
927 ovs_flow_free(new_flow, false);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700928error:
929 return error;
930}
931
932static int ovs_flow_cmd_set(struct sk_buff *skb, struct genl_info *info)
933{
934 struct nlattr **a = info->attrs;
935 struct ovs_header *ovs_header = info->userhdr;
936 struct sw_flow_key key, masked_key;
937 struct sw_flow *flow;
938 struct sw_flow_mask mask;
939 struct sk_buff *reply = NULL;
940 struct datapath *dp;
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700941 struct sw_flow_actions *old_acts = NULL, *acts = NULL;
Andy Zhou03f0d912013-08-07 20:01:00 -0700942 struct sw_flow_match match;
Jesse Grossccb13522011-10-25 19:26:31 -0700943 int error;
Jesse Grossccb13522011-10-25 19:26:31 -0700944
945 /* Extract key. */
946 error = -EINVAL;
947 if (!a[OVS_FLOW_ATTR_KEY])
948 goto error;
Andy Zhou03f0d912013-08-07 20:01:00 -0700949
950 ovs_match_init(&match, &key, &mask);
Jarno Rajahalme23dabf82014-03-27 12:35:23 -0700951 error = ovs_nla_get_match(&match,
Pravin B Shelare6445712013-10-03 18:16:47 -0700952 a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
Jesse Grossccb13522011-10-25 19:26:31 -0700953 if (error)
954 goto error;
955
956 /* Validate actions. */
957 if (a[OVS_FLOW_ATTR_ACTIONS]) {
Pravin B Shelare6445712013-10-03 18:16:47 -0700958 acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_FLOW_ATTR_ACTIONS]));
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700959 error = PTR_ERR(acts);
960 if (IS_ERR(acts))
Jesse Grossccb13522011-10-25 19:26:31 -0700961 goto error;
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700962
Pravin B Shelare6445712013-10-03 18:16:47 -0700963 ovs_flow_mask_key(&masked_key, &key, &mask);
964 error = ovs_nla_copy_actions(a[OVS_FLOW_ATTR_ACTIONS],
965 &masked_key, 0, &acts);
Andy Zhou03f0d912013-08-07 20:01:00 -0700966 if (error) {
967 OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700968 goto err_kfree_acts;
969 }
970 }
971
972 /* Can allocate before locking if have acts. */
973 if (acts) {
974 reply = ovs_flow_cmd_alloc_info(acts, info, false);
975 if (IS_ERR(reply)) {
976 error = PTR_ERR(reply);
977 goto err_kfree_acts;
Andy Zhou03f0d912013-08-07 20:01:00 -0700978 }
Jesse Grossccb13522011-10-25 19:26:31 -0700979 }
980
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700981 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800982 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700983 if (unlikely(!dp)) {
984 error = -ENODEV;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700985 goto err_unlock_ovs;
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700986 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700987 /* Check that the flow exists. */
Alex Wang4a46b242014-06-30 20:30:29 -0700988 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700989 if (unlikely(!flow)) {
990 error = -ENOENT;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700991 goto err_unlock_ovs;
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700992 }
Alex Wang4a46b242014-06-30 20:30:29 -0700993
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700994 /* Update actions, if present. */
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700995 if (likely(acts)) {
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700996 old_acts = ovsl_dereference(flow->sf_acts);
Jesse Grossccb13522011-10-25 19:26:31 -0700997 rcu_assign_pointer(flow->sf_acts, acts);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700998
999 if (unlikely(reply)) {
1000 error = ovs_flow_cmd_fill_info(flow,
1001 ovs_header->dp_ifindex,
1002 reply, info->snd_portid,
1003 info->snd_seq, 0,
1004 OVS_FLOW_CMD_NEW);
1005 BUG_ON(error < 0);
1006 }
1007 } else {
1008 /* Could not alloc without acts before locking. */
1009 reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex,
1010 info, OVS_FLOW_CMD_NEW, false);
1011 if (unlikely(IS_ERR(reply))) {
1012 error = PTR_ERR(reply);
1013 goto err_unlock_ovs;
1014 }
Jesse Grossccb13522011-10-25 19:26:31 -07001015 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001016
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001017 /* Clear stats. */
1018 if (a[OVS_FLOW_ATTR_CLEAR])
1019 ovs_flow_stats_clear(flow);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001020 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001021
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001022 if (reply)
1023 ovs_notify(&dp_flow_genl_family, reply, info);
1024 if (old_acts)
1025 ovs_nla_free_flow_actions(old_acts);
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -07001026
Jesse Grossccb13522011-10-25 19:26:31 -07001027 return 0;
1028
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001029err_unlock_ovs:
1030 ovs_unlock();
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001031 kfree_skb(reply);
1032err_kfree_acts:
Pravin B Shelar74f84a52013-06-17 17:50:12 -07001033 kfree(acts);
Jesse Grossccb13522011-10-25 19:26:31 -07001034error:
1035 return error;
1036}
1037
1038static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1039{
1040 struct nlattr **a = info->attrs;
1041 struct ovs_header *ovs_header = info->userhdr;
1042 struct sw_flow_key key;
1043 struct sk_buff *reply;
1044 struct sw_flow *flow;
1045 struct datapath *dp;
Andy Zhou03f0d912013-08-07 20:01:00 -07001046 struct sw_flow_match match;
Jesse Grossccb13522011-10-25 19:26:31 -07001047 int err;
Jesse Grossccb13522011-10-25 19:26:31 -07001048
Andy Zhou03f0d912013-08-07 20:01:00 -07001049 if (!a[OVS_FLOW_ATTR_KEY]) {
1050 OVS_NLERR("Flow get message rejected, Key attribute missing.\n");
Jesse Grossccb13522011-10-25 19:26:31 -07001051 return -EINVAL;
Andy Zhou03f0d912013-08-07 20:01:00 -07001052 }
1053
1054 ovs_match_init(&match, &key, NULL);
Jarno Rajahalme23dabf82014-03-27 12:35:23 -07001055 err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL);
Jesse Grossccb13522011-10-25 19:26:31 -07001056 if (err)
1057 return err;
1058
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001059 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001060 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001061 if (!dp) {
1062 err = -ENODEV;
1063 goto unlock;
1064 }
Jesse Grossccb13522011-10-25 19:26:31 -07001065
Alex Wang4a46b242014-06-30 20:30:29 -07001066 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1067 if (!flow) {
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001068 err = -ENOENT;
1069 goto unlock;
1070 }
Jesse Grossccb13522011-10-25 19:26:31 -07001071
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -07001072 reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex, info,
1073 OVS_FLOW_CMD_NEW, true);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001074 if (IS_ERR(reply)) {
1075 err = PTR_ERR(reply);
1076 goto unlock;
1077 }
Jesse Grossccb13522011-10-25 19:26:31 -07001078
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001079 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001080 return genlmsg_reply(reply, info);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001081unlock:
1082 ovs_unlock();
1083 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001084}
1085
1086static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1087{
1088 struct nlattr **a = info->attrs;
1089 struct ovs_header *ovs_header = info->userhdr;
1090 struct sw_flow_key key;
1091 struct sk_buff *reply;
1092 struct sw_flow *flow;
1093 struct datapath *dp;
Andy Zhou03f0d912013-08-07 20:01:00 -07001094 struct sw_flow_match match;
Jesse Grossccb13522011-10-25 19:26:31 -07001095 int err;
Jesse Grossccb13522011-10-25 19:26:31 -07001096
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001097 if (likely(a[OVS_FLOW_ATTR_KEY])) {
1098 ovs_match_init(&match, &key, NULL);
1099 err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL);
1100 if (unlikely(err))
1101 return err;
1102 }
1103
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001104 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001105 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001106 if (unlikely(!dp)) {
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001107 err = -ENODEV;
1108 goto unlock;
1109 }
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001110
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001111 if (unlikely(!a[OVS_FLOW_ATTR_KEY])) {
Pravin B Shelarb637e492013-10-04 00:14:23 -07001112 err = ovs_flow_tbl_flush(&dp->table);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001113 goto unlock;
1114 }
Andy Zhou03f0d912013-08-07 20:01:00 -07001115
Alex Wang4a46b242014-06-30 20:30:29 -07001116 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1117 if (unlikely(!flow)) {
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001118 err = -ENOENT;
1119 goto unlock;
1120 }
Jesse Grossccb13522011-10-25 19:26:31 -07001121
Pravin B Shelarb637e492013-10-04 00:14:23 -07001122 ovs_flow_tbl_remove(&dp->table, flow);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001123 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001124
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001125 reply = ovs_flow_cmd_alloc_info((const struct sw_flow_actions __force *) flow->sf_acts,
1126 info, false);
1127 if (likely(reply)) {
1128 if (likely(!IS_ERR(reply))) {
1129 rcu_read_lock(); /*To keep RCU checker happy. */
1130 err = ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex,
1131 reply, info->snd_portid,
1132 info->snd_seq, 0,
1133 OVS_FLOW_CMD_DEL);
1134 rcu_read_unlock();
1135 BUG_ON(err < 0);
1136
1137 ovs_notify(&dp_flow_genl_family, reply, info);
1138 } else {
1139 netlink_set_err(sock_net(skb->sk)->genl_sock, 0, 0, PTR_ERR(reply));
1140 }
1141 }
1142
1143 ovs_flow_free(flow, true);
Jesse Grossccb13522011-10-25 19:26:31 -07001144 return 0;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001145unlock:
1146 ovs_unlock();
1147 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001148}
1149
1150static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1151{
1152 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
Pravin B Shelarb637e492013-10-04 00:14:23 -07001153 struct table_instance *ti;
Jesse Grossccb13522011-10-25 19:26:31 -07001154 struct datapath *dp;
Jesse Grossccb13522011-10-25 19:26:31 -07001155
Pravin B Shelard57170b2013-07-30 15:39:39 -07001156 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001157 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001158 if (!dp) {
Pravin B Shelard57170b2013-07-30 15:39:39 -07001159 rcu_read_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001160 return -ENODEV;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001161 }
Jesse Grossccb13522011-10-25 19:26:31 -07001162
Pravin B Shelarb637e492013-10-04 00:14:23 -07001163 ti = rcu_dereference(dp->table.ti);
Jesse Grossccb13522011-10-25 19:26:31 -07001164 for (;;) {
1165 struct sw_flow *flow;
1166 u32 bucket, obj;
1167
1168 bucket = cb->args[0];
1169 obj = cb->args[1];
Pravin B Shelarb637e492013-10-04 00:14:23 -07001170 flow = ovs_flow_tbl_dump_next(ti, &bucket, &obj);
Jesse Grossccb13522011-10-25 19:26:31 -07001171 if (!flow)
1172 break;
1173
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -07001174 if (ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex, skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001175 NETLINK_CB(cb->skb).portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001176 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1177 OVS_FLOW_CMD_NEW) < 0)
1178 break;
1179
1180 cb->args[0] = bucket;
1181 cb->args[1] = obj;
1182 }
Pravin B Shelard57170b2013-07-30 15:39:39 -07001183 rcu_read_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001184 return skb->len;
1185}
1186
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001187static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
1188 [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
1189 [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
1190 [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
1191};
1192
stephen hemminger48e48a72014-07-16 11:25:52 -07001193static const struct genl_ops dp_flow_genl_ops[] = {
Jesse Grossccb13522011-10-25 19:26:31 -07001194 { .cmd = OVS_FLOW_CMD_NEW,
1195 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1196 .policy = flow_policy,
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001197 .doit = ovs_flow_cmd_new
Jesse Grossccb13522011-10-25 19:26:31 -07001198 },
1199 { .cmd = OVS_FLOW_CMD_DEL,
1200 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1201 .policy = flow_policy,
1202 .doit = ovs_flow_cmd_del
1203 },
1204 { .cmd = OVS_FLOW_CMD_GET,
1205 .flags = 0, /* OK for unprivileged users. */
1206 .policy = flow_policy,
1207 .doit = ovs_flow_cmd_get,
1208 .dumpit = ovs_flow_cmd_dump
1209 },
1210 { .cmd = OVS_FLOW_CMD_SET,
1211 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1212 .policy = flow_policy,
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001213 .doit = ovs_flow_cmd_set,
Jesse Grossccb13522011-10-25 19:26:31 -07001214 },
1215};
1216
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001217static struct genl_family dp_flow_genl_family = {
Jesse Grossccb13522011-10-25 19:26:31 -07001218 .id = GENL_ID_GENERATE,
1219 .hdrsize = sizeof(struct ovs_header),
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001220 .name = OVS_FLOW_FAMILY,
1221 .version = OVS_FLOW_VERSION,
1222 .maxattr = OVS_FLOW_ATTR_MAX,
Pravin B Shelar3a4e0d62013-04-23 07:48:48 +00001223 .netnsok = true,
1224 .parallel_ops = true,
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001225 .ops = dp_flow_genl_ops,
1226 .n_ops = ARRAY_SIZE(dp_flow_genl_ops),
1227 .mcgrps = &ovs_dp_flow_multicast_group,
1228 .n_mcgrps = 1,
Jesse Grossccb13522011-10-25 19:26:31 -07001229};
1230
Thomas Grafc3ff8cf2013-03-29 14:46:49 +01001231static size_t ovs_dp_cmd_msg_size(void)
1232{
1233 size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
1234
1235 msgsize += nla_total_size(IFNAMSIZ);
1236 msgsize += nla_total_size(sizeof(struct ovs_dp_stats));
Andy Zhou1bd71162013-10-22 10:42:46 -07001237 msgsize += nla_total_size(sizeof(struct ovs_dp_megaflow_stats));
Daniele Di Proietto45fb9c32014-01-23 10:47:35 -08001238 msgsize += nla_total_size(sizeof(u32)); /* OVS_DP_ATTR_USER_FEATURES */
Thomas Grafc3ff8cf2013-03-29 14:46:49 +01001239
1240 return msgsize;
1241}
1242
Jarno Rajahalmebb6f9a72014-05-05 11:32:17 -07001243/* Called with ovs_mutex or RCU read lock. */
Jesse Grossccb13522011-10-25 19:26:31 -07001244static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001245 u32 portid, u32 seq, u32 flags, u8 cmd)
Jesse Grossccb13522011-10-25 19:26:31 -07001246{
1247 struct ovs_header *ovs_header;
1248 struct ovs_dp_stats dp_stats;
Andy Zhou1bd71162013-10-22 10:42:46 -07001249 struct ovs_dp_megaflow_stats dp_megaflow_stats;
Jesse Grossccb13522011-10-25 19:26:31 -07001250 int err;
1251
Eric W. Biederman15e47302012-09-07 20:12:54 +00001252 ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
Jesse Grossccb13522011-10-25 19:26:31 -07001253 flags, cmd);
1254 if (!ovs_header)
1255 goto error;
1256
1257 ovs_header->dp_ifindex = get_dpifindex(dp);
1258
Jesse Grossccb13522011-10-25 19:26:31 -07001259 err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
Jesse Grossccb13522011-10-25 19:26:31 -07001260 if (err)
1261 goto nla_put_failure;
1262
Andy Zhou1bd71162013-10-22 10:42:46 -07001263 get_dp_stats(dp, &dp_stats, &dp_megaflow_stats);
1264 if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats),
1265 &dp_stats))
1266 goto nla_put_failure;
1267
1268 if (nla_put(skb, OVS_DP_ATTR_MEGAFLOW_STATS,
1269 sizeof(struct ovs_dp_megaflow_stats),
1270 &dp_megaflow_stats))
David S. Miller028d6a62012-03-29 23:20:48 -04001271 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001272
Thomas Graf43d4be92013-12-13 15:22:18 +01001273 if (nla_put_u32(skb, OVS_DP_ATTR_USER_FEATURES, dp->user_features))
1274 goto nla_put_failure;
1275
Jesse Grossccb13522011-10-25 19:26:31 -07001276 return genlmsg_end(skb, ovs_header);
1277
1278nla_put_failure:
1279 genlmsg_cancel(skb, ovs_header);
1280error:
1281 return -EMSGSIZE;
1282}
1283
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001284static struct sk_buff *ovs_dp_cmd_alloc_info(struct genl_info *info)
Jesse Grossccb13522011-10-25 19:26:31 -07001285{
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001286 return genlmsg_new_unicast(ovs_dp_cmd_msg_size(), info, GFP_KERNEL);
Jesse Grossccb13522011-10-25 19:26:31 -07001287}
1288
Jarno Rajahalmebb6f9a72014-05-05 11:32:17 -07001289/* Called with rcu_read_lock or ovs_mutex. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001290static struct datapath *lookup_datapath(struct net *net,
1291 struct ovs_header *ovs_header,
Jesse Grossccb13522011-10-25 19:26:31 -07001292 struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1293{
1294 struct datapath *dp;
1295
1296 if (!a[OVS_DP_ATTR_NAME])
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001297 dp = get_dp(net, ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001298 else {
1299 struct vport *vport;
1300
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001301 vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
Jesse Grossccb13522011-10-25 19:26:31 -07001302 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
Jesse Grossccb13522011-10-25 19:26:31 -07001303 }
1304 return dp ? dp : ERR_PTR(-ENODEV);
1305}
1306
Thomas Graf44da5ae2013-12-13 15:22:19 +01001307static void ovs_dp_reset_user_features(struct sk_buff *skb, struct genl_info *info)
1308{
1309 struct datapath *dp;
1310
1311 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
Jiri Pirko3c7eacf2014-02-14 11:42:36 +01001312 if (IS_ERR(dp))
Thomas Graf44da5ae2013-12-13 15:22:19 +01001313 return;
1314
1315 WARN(dp->user_features, "Dropping previously announced user features\n");
1316 dp->user_features = 0;
1317}
1318
Thomas Graf43d4be92013-12-13 15:22:18 +01001319static void ovs_dp_change(struct datapath *dp, struct nlattr **a)
1320{
1321 if (a[OVS_DP_ATTR_USER_FEATURES])
1322 dp->user_features = nla_get_u32(a[OVS_DP_ATTR_USER_FEATURES]);
1323}
1324
Jesse Grossccb13522011-10-25 19:26:31 -07001325static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1326{
1327 struct nlattr **a = info->attrs;
1328 struct vport_parms parms;
1329 struct sk_buff *reply;
1330 struct datapath *dp;
1331 struct vport *vport;
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001332 struct ovs_net *ovs_net;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001333 int err, i;
Jesse Grossccb13522011-10-25 19:26:31 -07001334
1335 err = -EINVAL;
1336 if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1337 goto err;
1338
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001339 reply = ovs_dp_cmd_alloc_info(info);
1340 if (!reply)
1341 return -ENOMEM;
Jesse Grossccb13522011-10-25 19:26:31 -07001342
1343 err = -ENOMEM;
1344 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1345 if (dp == NULL)
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001346 goto err_free_reply;
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001347
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001348 ovs_dp_set_net(dp, hold_net(sock_net(skb->sk)));
Jesse Grossccb13522011-10-25 19:26:31 -07001349
1350 /* Allocate table. */
Pravin B Shelarb637e492013-10-04 00:14:23 -07001351 err = ovs_flow_tbl_init(&dp->table);
1352 if (err)
Jesse Grossccb13522011-10-25 19:26:31 -07001353 goto err_free_dp;
1354
WANG Cong1c213bd2014-02-13 11:46:28 -08001355 dp->stats_percpu = netdev_alloc_pcpu_stats(struct dp_stats_percpu);
Jesse Grossccb13522011-10-25 19:26:31 -07001356 if (!dp->stats_percpu) {
1357 err = -ENOMEM;
1358 goto err_destroy_table;
1359 }
1360
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001361 dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
Pravin B Shelare6445712013-10-03 18:16:47 -07001362 GFP_KERNEL);
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001363 if (!dp->ports) {
1364 err = -ENOMEM;
1365 goto err_destroy_percpu;
1366 }
1367
1368 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1369 INIT_HLIST_HEAD(&dp->ports[i]);
1370
Jesse Grossccb13522011-10-25 19:26:31 -07001371 /* Set up our datapath device. */
1372 parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1373 parms.type = OVS_VPORT_TYPE_INTERNAL;
1374 parms.options = NULL;
1375 parms.dp = dp;
1376 parms.port_no = OVSP_LOCAL;
Alex Wang5cd667b2014-07-17 15:14:13 -07001377 parms.upcall_portids = a[OVS_DP_ATTR_UPCALL_PID];
Jesse Grossccb13522011-10-25 19:26:31 -07001378
Thomas Graf43d4be92013-12-13 15:22:18 +01001379 ovs_dp_change(dp, a);
1380
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001381 /* So far only local changes have been made, now need the lock. */
1382 ovs_lock();
1383
Jesse Grossccb13522011-10-25 19:26:31 -07001384 vport = new_vport(&parms);
1385 if (IS_ERR(vport)) {
1386 err = PTR_ERR(vport);
1387 if (err == -EBUSY)
1388 err = -EEXIST;
1389
Thomas Graf44da5ae2013-12-13 15:22:19 +01001390 if (err == -EEXIST) {
1391 /* An outdated user space instance that does not understand
1392 * the concept of user_features has attempted to create a new
1393 * datapath and is likely to reuse it. Drop all user features.
1394 */
1395 if (info->genlhdr->version < OVS_DP_VER_FEATURES)
1396 ovs_dp_reset_user_features(skb, info);
1397 }
1398
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001399 goto err_destroy_ports_array;
Jesse Grossccb13522011-10-25 19:26:31 -07001400 }
1401
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001402 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1403 info->snd_seq, 0, OVS_DP_CMD_NEW);
1404 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001405
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001406 ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001407 list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001408
1409 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001410
Johannes Berg2a94fe42013-11-19 15:19:39 +01001411 ovs_notify(&dp_datapath_genl_family, reply, info);
Jesse Grossccb13522011-10-25 19:26:31 -07001412 return 0;
1413
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001414err_destroy_ports_array:
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001415 ovs_unlock();
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001416 kfree(dp->ports);
Jesse Grossccb13522011-10-25 19:26:31 -07001417err_destroy_percpu:
1418 free_percpu(dp->stats_percpu);
1419err_destroy_table:
Andy Zhoue80857c2014-01-21 09:31:04 -08001420 ovs_flow_tbl_destroy(&dp->table, false);
Jesse Grossccb13522011-10-25 19:26:31 -07001421err_free_dp:
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001422 release_net(ovs_dp_get_net(dp));
Jesse Grossccb13522011-10-25 19:26:31 -07001423 kfree(dp);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001424err_free_reply:
1425 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001426err:
1427 return err;
1428}
1429
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001430/* Called with ovs_mutex. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001431static void __dp_destroy(struct datapath *dp)
Jesse Grossccb13522011-10-25 19:26:31 -07001432{
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001433 int i;
Jesse Grossccb13522011-10-25 19:26:31 -07001434
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001435 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1436 struct vport *vport;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001437 struct hlist_node *n;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001438
Sasha Levinb67bfe02013-02-27 17:06:00 -08001439 hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001440 if (vport->port_no != OVSP_LOCAL)
1441 ovs_dp_detach_port(vport);
1442 }
Jesse Grossccb13522011-10-25 19:26:31 -07001443
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001444 list_del_rcu(&dp->list_node);
Jesse Grossccb13522011-10-25 19:26:31 -07001445
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001446 /* OVSP_LOCAL is datapath internal port. We need to make sure that
Andy Zhoue80857c2014-01-21 09:31:04 -08001447 * all ports in datapath are destroyed first before freeing datapath.
Jesse Grossccb13522011-10-25 19:26:31 -07001448 */
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001449 ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
Jesse Grossccb13522011-10-25 19:26:31 -07001450
Andy Zhoue80857c2014-01-21 09:31:04 -08001451 /* RCU destroy the flow table */
1452 ovs_flow_tbl_destroy(&dp->table, true);
1453
Jesse Grossccb13522011-10-25 19:26:31 -07001454 call_rcu(&dp->rcu, destroy_dp_rcu);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001455}
1456
1457static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1458{
1459 struct sk_buff *reply;
1460 struct datapath *dp;
1461 int err;
1462
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001463 reply = ovs_dp_cmd_alloc_info(info);
1464 if (!reply)
1465 return -ENOMEM;
1466
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001467 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001468 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1469 err = PTR_ERR(dp);
1470 if (IS_ERR(dp))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001471 goto err_unlock_free;
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001472
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001473 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1474 info->snd_seq, 0, OVS_DP_CMD_DEL);
1475 BUG_ON(err < 0);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001476
1477 __dp_destroy(dp);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001478 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001479
Johannes Berg2a94fe42013-11-19 15:19:39 +01001480 ovs_notify(&dp_datapath_genl_family, reply, info);
Jesse Grossccb13522011-10-25 19:26:31 -07001481
1482 return 0;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001483
1484err_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001485 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001486 kfree_skb(reply);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001487 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001488}
1489
1490static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1491{
1492 struct sk_buff *reply;
1493 struct datapath *dp;
1494 int err;
1495
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001496 reply = ovs_dp_cmd_alloc_info(info);
1497 if (!reply)
1498 return -ENOMEM;
1499
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001500 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001501 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001502 err = PTR_ERR(dp);
Jesse Grossccb13522011-10-25 19:26:31 -07001503 if (IS_ERR(dp))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001504 goto err_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001505
Thomas Graf43d4be92013-12-13 15:22:18 +01001506 ovs_dp_change(dp, info->attrs);
1507
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001508 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1509 info->snd_seq, 0, OVS_DP_CMD_NEW);
1510 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001511
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001512 ovs_unlock();
Johannes Berg2a94fe42013-11-19 15:19:39 +01001513 ovs_notify(&dp_datapath_genl_family, reply, info);
Jesse Grossccb13522011-10-25 19:26:31 -07001514
1515 return 0;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001516
1517err_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001518 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001519 kfree_skb(reply);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001520 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001521}
1522
1523static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1524{
1525 struct sk_buff *reply;
1526 struct datapath *dp;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001527 int err;
Jesse Grossccb13522011-10-25 19:26:31 -07001528
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001529 reply = ovs_dp_cmd_alloc_info(info);
1530 if (!reply)
1531 return -ENOMEM;
1532
1533 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001534 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001535 if (IS_ERR(dp)) {
1536 err = PTR_ERR(dp);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001537 goto err_unlock_free;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001538 }
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001539 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1540 info->snd_seq, 0, OVS_DP_CMD_NEW);
1541 BUG_ON(err < 0);
1542 rcu_read_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001543
Jesse Grossccb13522011-10-25 19:26:31 -07001544 return genlmsg_reply(reply, info);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001545
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001546err_unlock_free:
1547 rcu_read_unlock();
1548 kfree_skb(reply);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001549 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001550}
1551
1552static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1553{
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001554 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
Jesse Grossccb13522011-10-25 19:26:31 -07001555 struct datapath *dp;
1556 int skip = cb->args[0];
1557 int i = 0;
1558
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001559 rcu_read_lock();
1560 list_for_each_entry_rcu(dp, &ovs_net->dps, list_node) {
Ben Pfaff77676fd2012-01-17 13:33:39 +00001561 if (i >= skip &&
Eric W. Biederman15e47302012-09-07 20:12:54 +00001562 ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001563 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1564 OVS_DP_CMD_NEW) < 0)
1565 break;
1566 i++;
1567 }
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001568 rcu_read_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001569
1570 cb->args[0] = i;
1571
1572 return skb->len;
1573}
1574
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001575static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1576 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1577 [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1578 [OVS_DP_ATTR_USER_FEATURES] = { .type = NLA_U32 },
1579};
1580
stephen hemminger48e48a72014-07-16 11:25:52 -07001581static const struct genl_ops dp_datapath_genl_ops[] = {
Jesse Grossccb13522011-10-25 19:26:31 -07001582 { .cmd = OVS_DP_CMD_NEW,
1583 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1584 .policy = datapath_policy,
1585 .doit = ovs_dp_cmd_new
1586 },
1587 { .cmd = OVS_DP_CMD_DEL,
1588 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1589 .policy = datapath_policy,
1590 .doit = ovs_dp_cmd_del
1591 },
1592 { .cmd = OVS_DP_CMD_GET,
1593 .flags = 0, /* OK for unprivileged users. */
1594 .policy = datapath_policy,
1595 .doit = ovs_dp_cmd_get,
1596 .dumpit = ovs_dp_cmd_dump
1597 },
1598 { .cmd = OVS_DP_CMD_SET,
1599 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1600 .policy = datapath_policy,
1601 .doit = ovs_dp_cmd_set,
1602 },
1603};
1604
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001605static struct genl_family dp_datapath_genl_family = {
Jesse Grossccb13522011-10-25 19:26:31 -07001606 .id = GENL_ID_GENERATE,
1607 .hdrsize = sizeof(struct ovs_header),
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001608 .name = OVS_DATAPATH_FAMILY,
1609 .version = OVS_DATAPATH_VERSION,
1610 .maxattr = OVS_DP_ATTR_MAX,
Pravin B Shelar3a4e0d62013-04-23 07:48:48 +00001611 .netnsok = true,
1612 .parallel_ops = true,
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001613 .ops = dp_datapath_genl_ops,
1614 .n_ops = ARRAY_SIZE(dp_datapath_genl_ops),
1615 .mcgrps = &ovs_dp_datapath_multicast_group,
1616 .n_mcgrps = 1,
Jesse Grossccb13522011-10-25 19:26:31 -07001617};
1618
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001619/* Called with ovs_mutex or RCU read lock. */
Jesse Grossccb13522011-10-25 19:26:31 -07001620static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001621 u32 portid, u32 seq, u32 flags, u8 cmd)
Jesse Grossccb13522011-10-25 19:26:31 -07001622{
1623 struct ovs_header *ovs_header;
1624 struct ovs_vport_stats vport_stats;
1625 int err;
1626
Eric W. Biederman15e47302012-09-07 20:12:54 +00001627 ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
Jesse Grossccb13522011-10-25 19:26:31 -07001628 flags, cmd);
1629 if (!ovs_header)
1630 return -EMSGSIZE;
1631
1632 ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1633
David S. Miller028d6a62012-03-29 23:20:48 -04001634 if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
1635 nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
Alex Wang5cd667b2014-07-17 15:14:13 -07001636 nla_put_string(skb, OVS_VPORT_ATTR_NAME,
1637 vport->ops->get_name(vport)))
David S. Miller028d6a62012-03-29 23:20:48 -04001638 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001639
1640 ovs_vport_get_stats(vport, &vport_stats);
David S. Miller028d6a62012-03-29 23:20:48 -04001641 if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
1642 &vport_stats))
1643 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001644
Alex Wang5cd667b2014-07-17 15:14:13 -07001645 if (ovs_vport_get_upcall_portids(vport, skb))
1646 goto nla_put_failure;
1647
Jesse Grossccb13522011-10-25 19:26:31 -07001648 err = ovs_vport_get_options(vport, skb);
1649 if (err == -EMSGSIZE)
1650 goto error;
1651
1652 return genlmsg_end(skb, ovs_header);
1653
1654nla_put_failure:
1655 err = -EMSGSIZE;
1656error:
1657 genlmsg_cancel(skb, ovs_header);
1658 return err;
1659}
1660
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001661static struct sk_buff *ovs_vport_cmd_alloc_info(void)
1662{
1663 return nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1664}
1665
1666/* Called with ovs_mutex, only via ovs_dp_notify_wq(). */
Eric W. Biederman15e47302012-09-07 20:12:54 +00001667struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001668 u32 seq, u8 cmd)
1669{
1670 struct sk_buff *skb;
1671 int retval;
1672
1673 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1674 if (!skb)
1675 return ERR_PTR(-ENOMEM);
1676
Eric W. Biederman15e47302012-09-07 20:12:54 +00001677 retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
Jesse Grossa9341512013-03-26 15:48:38 -07001678 BUG_ON(retval < 0);
1679
Jesse Grossccb13522011-10-25 19:26:31 -07001680 return skb;
1681}
1682
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001683/* Called with ovs_mutex or RCU read lock. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001684static struct vport *lookup_vport(struct net *net,
1685 struct ovs_header *ovs_header,
Jesse Grossccb13522011-10-25 19:26:31 -07001686 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1687{
1688 struct datapath *dp;
1689 struct vport *vport;
1690
1691 if (a[OVS_VPORT_ATTR_NAME]) {
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001692 vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
Jesse Grossccb13522011-10-25 19:26:31 -07001693 if (!vport)
1694 return ERR_PTR(-ENODEV);
Ben Pfaff651a68e2012-03-06 15:04:04 -08001695 if (ovs_header->dp_ifindex &&
1696 ovs_header->dp_ifindex != get_dpifindex(vport->dp))
1697 return ERR_PTR(-ENODEV);
Jesse Grossccb13522011-10-25 19:26:31 -07001698 return vport;
1699 } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
1700 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1701
1702 if (port_no >= DP_MAX_PORTS)
1703 return ERR_PTR(-EFBIG);
1704
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001705 dp = get_dp(net, ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001706 if (!dp)
1707 return ERR_PTR(-ENODEV);
1708
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001709 vport = ovs_vport_ovsl_rcu(dp, port_no);
Jesse Grossccb13522011-10-25 19:26:31 -07001710 if (!vport)
Jarno Rajahalme14408db2013-01-09 14:27:35 -08001711 return ERR_PTR(-ENODEV);
Jesse Grossccb13522011-10-25 19:26:31 -07001712 return vport;
1713 } else
1714 return ERR_PTR(-EINVAL);
1715}
1716
1717static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
1718{
1719 struct nlattr **a = info->attrs;
1720 struct ovs_header *ovs_header = info->userhdr;
1721 struct vport_parms parms;
1722 struct sk_buff *reply;
1723 struct vport *vport;
1724 struct datapath *dp;
1725 u32 port_no;
1726 int err;
1727
Jesse Grossccb13522011-10-25 19:26:31 -07001728 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
1729 !a[OVS_VPORT_ATTR_UPCALL_PID])
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001730 return -EINVAL;
1731
1732 port_no = a[OVS_VPORT_ATTR_PORT_NO]
1733 ? nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]) : 0;
1734 if (port_no >= DP_MAX_PORTS)
1735 return -EFBIG;
1736
1737 reply = ovs_vport_cmd_alloc_info();
1738 if (!reply)
1739 return -ENOMEM;
Jesse Grossccb13522011-10-25 19:26:31 -07001740
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001741 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001742 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001743 err = -ENODEV;
1744 if (!dp)
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001745 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001746
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001747 if (port_no) {
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001748 vport = ovs_vport_ovsl(dp, port_no);
Jesse Grossccb13522011-10-25 19:26:31 -07001749 err = -EBUSY;
1750 if (vport)
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001751 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001752 } else {
1753 for (port_no = 1; ; port_no++) {
1754 if (port_no >= DP_MAX_PORTS) {
1755 err = -EFBIG;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001756 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001757 }
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001758 vport = ovs_vport_ovsl(dp, port_no);
Jesse Grossccb13522011-10-25 19:26:31 -07001759 if (!vport)
1760 break;
1761 }
1762 }
1763
1764 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
1765 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1766 parms.options = a[OVS_VPORT_ATTR_OPTIONS];
1767 parms.dp = dp;
1768 parms.port_no = port_no;
Alex Wang5cd667b2014-07-17 15:14:13 -07001769 parms.upcall_portids = a[OVS_VPORT_ATTR_UPCALL_PID];
Jesse Grossccb13522011-10-25 19:26:31 -07001770
1771 vport = new_vport(&parms);
1772 err = PTR_ERR(vport);
1773 if (IS_ERR(vport))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001774 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001775
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001776 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1777 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1778 BUG_ON(err < 0);
1779 ovs_unlock();
Thomas Grafed661182013-03-29 14:46:50 +01001780
Johannes Berg2a94fe42013-11-19 15:19:39 +01001781 ovs_notify(&dp_vport_genl_family, reply, info);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001782 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001783
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001784exit_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001785 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001786 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001787 return err;
1788}
1789
1790static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
1791{
1792 struct nlattr **a = info->attrs;
1793 struct sk_buff *reply;
1794 struct vport *vport;
1795 int err;
1796
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001797 reply = ovs_vport_cmd_alloc_info();
1798 if (!reply)
1799 return -ENOMEM;
1800
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001801 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001802 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
Jesse Grossccb13522011-10-25 19:26:31 -07001803 err = PTR_ERR(vport);
1804 if (IS_ERR(vport))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001805 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001806
Jesse Grossccb13522011-10-25 19:26:31 -07001807 if (a[OVS_VPORT_ATTR_TYPE] &&
Jesse Grossf44f3402013-05-13 08:15:26 -07001808 nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
Jesse Grossccb13522011-10-25 19:26:31 -07001809 err = -EINVAL;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001810 goto exit_unlock_free;
Jesse Grossa9341512013-03-26 15:48:38 -07001811 }
1812
Jesse Grossf44f3402013-05-13 08:15:26 -07001813 if (a[OVS_VPORT_ATTR_OPTIONS]) {
Jesse Grossccb13522011-10-25 19:26:31 -07001814 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
Jesse Grossf44f3402013-05-13 08:15:26 -07001815 if (err)
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001816 goto exit_unlock_free;
Jesse Grossf44f3402013-05-13 08:15:26 -07001817 }
Jesse Grossa9341512013-03-26 15:48:38 -07001818
Alex Wang5cd667b2014-07-17 15:14:13 -07001819
1820 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
1821 struct nlattr *ids = a[OVS_VPORT_ATTR_UPCALL_PID];
1822
1823 err = ovs_vport_set_upcall_portids(vport, ids);
1824 if (err)
1825 goto exit_unlock_free;
1826 }
Jesse Grossccb13522011-10-25 19:26:31 -07001827
Jesse Grossa9341512013-03-26 15:48:38 -07001828 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1829 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1830 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001831
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001832 ovs_unlock();
Johannes Berg2a94fe42013-11-19 15:19:39 +01001833 ovs_notify(&dp_vport_genl_family, reply, info);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001834 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001835
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001836exit_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001837 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001838 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001839 return err;
1840}
1841
1842static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
1843{
1844 struct nlattr **a = info->attrs;
1845 struct sk_buff *reply;
1846 struct vport *vport;
1847 int err;
1848
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001849 reply = ovs_vport_cmd_alloc_info();
1850 if (!reply)
1851 return -ENOMEM;
1852
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001853 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001854 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
Jesse Grossccb13522011-10-25 19:26:31 -07001855 err = PTR_ERR(vport);
1856 if (IS_ERR(vport))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001857 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001858
1859 if (vport->port_no == OVSP_LOCAL) {
1860 err = -EINVAL;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001861 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001862 }
1863
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001864 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1865 info->snd_seq, 0, OVS_VPORT_CMD_DEL);
1866 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001867 ovs_dp_detach_port(vport);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001868 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001869
Johannes Berg2a94fe42013-11-19 15:19:39 +01001870 ovs_notify(&dp_vport_genl_family, reply, info);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001871 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001872
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001873exit_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001874 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001875 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001876 return err;
1877}
1878
1879static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
1880{
1881 struct nlattr **a = info->attrs;
1882 struct ovs_header *ovs_header = info->userhdr;
1883 struct sk_buff *reply;
1884 struct vport *vport;
1885 int err;
1886
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001887 reply = ovs_vport_cmd_alloc_info();
1888 if (!reply)
1889 return -ENOMEM;
1890
Jesse Grossccb13522011-10-25 19:26:31 -07001891 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001892 vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
Jesse Grossccb13522011-10-25 19:26:31 -07001893 err = PTR_ERR(vport);
1894 if (IS_ERR(vport))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001895 goto exit_unlock_free;
1896 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1897 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1898 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001899 rcu_read_unlock();
1900
1901 return genlmsg_reply(reply, info);
1902
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001903exit_unlock_free:
Jesse Grossccb13522011-10-25 19:26:31 -07001904 rcu_read_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001905 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001906 return err;
1907}
1908
1909static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1910{
1911 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1912 struct datapath *dp;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001913 int bucket = cb->args[0], skip = cb->args[1];
1914 int i, j = 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001915
Jesse Grossccb13522011-10-25 19:26:31 -07001916 rcu_read_lock();
Jarno Rajahalme42ee19e2014-02-15 17:42:29 -08001917 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1918 if (!dp) {
1919 rcu_read_unlock();
1920 return -ENODEV;
1921 }
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001922 for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
Jesse Grossccb13522011-10-25 19:26:31 -07001923 struct vport *vport;
1924
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001925 j = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001926 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001927 if (j >= skip &&
1928 ovs_vport_cmd_fill_info(vport, skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001929 NETLINK_CB(cb->skb).portid,
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001930 cb->nlh->nlmsg_seq,
1931 NLM_F_MULTI,
1932 OVS_VPORT_CMD_NEW) < 0)
1933 goto out;
Jesse Grossccb13522011-10-25 19:26:31 -07001934
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001935 j++;
1936 }
1937 skip = 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001938 }
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001939out:
Jesse Grossccb13522011-10-25 19:26:31 -07001940 rcu_read_unlock();
1941
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001942 cb->args[0] = i;
1943 cb->args[1] = j;
Jesse Grossccb13522011-10-25 19:26:31 -07001944
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001945 return skb->len;
Jesse Grossccb13522011-10-25 19:26:31 -07001946}
1947
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001948static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
1949 [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1950 [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
1951 [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
1952 [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
1953 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1954 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
1955};
1956
stephen hemminger48e48a72014-07-16 11:25:52 -07001957static const struct genl_ops dp_vport_genl_ops[] = {
Jesse Grossccb13522011-10-25 19:26:31 -07001958 { .cmd = OVS_VPORT_CMD_NEW,
1959 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1960 .policy = vport_policy,
1961 .doit = ovs_vport_cmd_new
1962 },
1963 { .cmd = OVS_VPORT_CMD_DEL,
1964 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1965 .policy = vport_policy,
1966 .doit = ovs_vport_cmd_del
1967 },
1968 { .cmd = OVS_VPORT_CMD_GET,
1969 .flags = 0, /* OK for unprivileged users. */
1970 .policy = vport_policy,
1971 .doit = ovs_vport_cmd_get,
1972 .dumpit = ovs_vport_cmd_dump
1973 },
1974 { .cmd = OVS_VPORT_CMD_SET,
1975 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1976 .policy = vport_policy,
1977 .doit = ovs_vport_cmd_set,
1978 },
1979};
1980
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001981struct genl_family dp_vport_genl_family = {
1982 .id = GENL_ID_GENERATE,
1983 .hdrsize = sizeof(struct ovs_header),
1984 .name = OVS_VPORT_FAMILY,
1985 .version = OVS_VPORT_VERSION,
1986 .maxattr = OVS_VPORT_ATTR_MAX,
1987 .netnsok = true,
1988 .parallel_ops = true,
1989 .ops = dp_vport_genl_ops,
1990 .n_ops = ARRAY_SIZE(dp_vport_genl_ops),
1991 .mcgrps = &ovs_dp_vport_multicast_group,
1992 .n_mcgrps = 1,
Jesse Grossccb13522011-10-25 19:26:31 -07001993};
1994
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001995static struct genl_family * const dp_genl_families[] = {
1996 &dp_datapath_genl_family,
1997 &dp_vport_genl_family,
1998 &dp_flow_genl_family,
1999 &dp_packet_genl_family,
Jesse Grossccb13522011-10-25 19:26:31 -07002000};
2001
2002static void dp_unregister_genl(int n_families)
2003{
2004 int i;
2005
2006 for (i = 0; i < n_families; i++)
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002007 genl_unregister_family(dp_genl_families[i]);
Jesse Grossccb13522011-10-25 19:26:31 -07002008}
2009
2010static int dp_register_genl(void)
2011{
Jesse Grossccb13522011-10-25 19:26:31 -07002012 int err;
2013 int i;
2014
Jesse Grossccb13522011-10-25 19:26:31 -07002015 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
Jesse Grossccb13522011-10-25 19:26:31 -07002016
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002017 err = genl_register_family(dp_genl_families[i]);
Jesse Grossccb13522011-10-25 19:26:31 -07002018 if (err)
2019 goto error;
Jesse Grossccb13522011-10-25 19:26:31 -07002020 }
2021
2022 return 0;
2023
2024error:
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002025 dp_unregister_genl(i);
Jesse Grossccb13522011-10-25 19:26:31 -07002026 return err;
2027}
2028
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002029static int __net_init ovs_init_net(struct net *net)
2030{
2031 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2032
2033 INIT_LIST_HEAD(&ovs_net->dps);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002034 INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002035 return 0;
2036}
2037
2038static void __net_exit ovs_exit_net(struct net *net)
2039{
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002040 struct datapath *dp, *dp_next;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002041 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002042
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002043 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002044 list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
2045 __dp_destroy(dp);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002046 ovs_unlock();
2047
2048 cancel_work_sync(&ovs_net->dp_notify_work);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002049}
2050
2051static struct pernet_operations ovs_net_ops = {
2052 .init = ovs_init_net,
2053 .exit = ovs_exit_net,
2054 .id = &ovs_net_id,
2055 .size = sizeof(struct ovs_net),
2056};
2057
Jesse Grossccb13522011-10-25 19:26:31 -07002058static int __init dp_init(void)
2059{
Jesse Grossccb13522011-10-25 19:26:31 -07002060 int err;
2061
YOSHIFUJI Hideaki / 吉藤英明3523b292013-01-09 07:19:55 +00002062 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
Jesse Grossccb13522011-10-25 19:26:31 -07002063
2064 pr_info("Open vSwitch switching datapath\n");
2065
Jiri Pirko5b9e7e12014-06-26 09:58:26 +02002066 err = ovs_internal_dev_rtnl_link_register();
Jesse Grossccb13522011-10-25 19:26:31 -07002067 if (err)
2068 goto error;
2069
Jiri Pirko5b9e7e12014-06-26 09:58:26 +02002070 err = ovs_flow_init();
2071 if (err)
2072 goto error_unreg_rtnl_link;
2073
Jesse Grossccb13522011-10-25 19:26:31 -07002074 err = ovs_vport_init();
2075 if (err)
2076 goto error_flow_exit;
2077
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002078 err = register_pernet_device(&ovs_net_ops);
Jesse Grossccb13522011-10-25 19:26:31 -07002079 if (err)
2080 goto error_vport_exit;
2081
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002082 err = register_netdevice_notifier(&ovs_dp_device_notifier);
2083 if (err)
2084 goto error_netns_exit;
2085
Jesse Grossccb13522011-10-25 19:26:31 -07002086 err = dp_register_genl();
2087 if (err < 0)
2088 goto error_unreg_notifier;
2089
Jesse Grossccb13522011-10-25 19:26:31 -07002090 return 0;
2091
2092error_unreg_notifier:
2093 unregister_netdevice_notifier(&ovs_dp_device_notifier);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002094error_netns_exit:
2095 unregister_pernet_device(&ovs_net_ops);
Jesse Grossccb13522011-10-25 19:26:31 -07002096error_vport_exit:
2097 ovs_vport_exit();
2098error_flow_exit:
2099 ovs_flow_exit();
Jiri Pirko5b9e7e12014-06-26 09:58:26 +02002100error_unreg_rtnl_link:
2101 ovs_internal_dev_rtnl_link_unregister();
Jesse Grossccb13522011-10-25 19:26:31 -07002102error:
2103 return err;
2104}
2105
2106static void dp_cleanup(void)
2107{
Jesse Grossccb13522011-10-25 19:26:31 -07002108 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
2109 unregister_netdevice_notifier(&ovs_dp_device_notifier);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002110 unregister_pernet_device(&ovs_net_ops);
2111 rcu_barrier();
Jesse Grossccb13522011-10-25 19:26:31 -07002112 ovs_vport_exit();
2113 ovs_flow_exit();
Jiri Pirko5b9e7e12014-06-26 09:58:26 +02002114 ovs_internal_dev_rtnl_link_unregister();
Jesse Grossccb13522011-10-25 19:26:31 -07002115}
2116
2117module_init(dp_init);
2118module_exit(dp_cleanup);
2119
2120MODULE_DESCRIPTION("Open vSwitch switching datapath");
2121MODULE_LICENSE("GPL");