blob: 65a8e5c089e41a91bedfe80f82f257fd6d430c86 [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>
Pravin B Shelar0c200ef2014-05-06 16:44:50 -070050#include <linux/genetlink.h>
51#include <net/genetlink.h>
Jesse Grossccb13522011-10-25 19:26:31 -070052#include <net/genetlink.h>
Pravin B Shelar46df7b82012-02-22 19:58:59 -080053#include <net/net_namespace.h>
54#include <net/netns/generic.h>
Jesse Grossccb13522011-10-25 19:26:31 -070055
56#include "datapath.h"
57#include "flow.h"
Andy Zhoue80857c2014-01-21 09:31:04 -080058#include "flow_table.h"
Pravin B Shelare6445712013-10-03 18:16:47 -070059#include "flow_netlink.h"
Jesse Grossccb13522011-10-25 19:26:31 -070060#include "vport-internal_dev.h"
Thomas Grafcff63a52013-04-29 13:06:41 +000061#include "vport-netdev.h"
Jesse Grossccb13522011-10-25 19:26:31 -070062
Pravin B Shelar8e4e1712013-04-15 13:23:03 -070063int ovs_net_id __read_mostly;
64
Pravin B Shelar0c200ef2014-05-06 16:44:50 -070065static struct genl_family dp_packet_genl_family;
66static struct genl_family dp_flow_genl_family;
67static struct genl_family dp_datapath_genl_family;
68
stephen hemminger48e48a72014-07-16 11:25:52 -070069static const struct genl_multicast_group ovs_dp_flow_multicast_group = {
70 .name = OVS_FLOW_MCGROUP,
Pravin B Shelar0c200ef2014-05-06 16:44:50 -070071};
72
stephen hemminger48e48a72014-07-16 11:25:52 -070073static const struct genl_multicast_group ovs_dp_datapath_multicast_group = {
74 .name = OVS_DATAPATH_MCGROUP,
Pravin B Shelar0c200ef2014-05-06 16:44:50 -070075};
76
stephen hemminger48e48a72014-07-16 11:25:52 -070077static const struct genl_multicast_group ovs_dp_vport_multicast_group = {
78 .name = OVS_VPORT_MCGROUP,
Pravin B Shelar0c200ef2014-05-06 16:44:50 -070079};
80
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -070081/* Check if need to build a reply message.
82 * OVS userspace sets the NLM_F_ECHO flag if it needs the reply. */
83static bool ovs_must_notify(struct genl_info *info,
84 const struct genl_multicast_group *grp)
85{
86 return info->nlhdr->nlmsg_flags & NLM_F_ECHO ||
87 netlink_has_listeners(genl_info_net(info)->genl_sock, 0);
88}
89
Johannes Berg68eb5502013-11-19 15:19:38 +010090static void ovs_notify(struct genl_family *family,
Johannes Berg2a94fe42013-11-19 15:19:39 +010091 struct sk_buff *skb, struct genl_info *info)
Thomas Grafed661182013-03-29 14:46:50 +010092{
Johannes Berg68eb5502013-11-19 15:19:38 +010093 genl_notify(family, skb, genl_info_net(info), info->snd_portid,
Johannes Berg2a94fe42013-11-19 15:19:39 +010094 0, info->nlhdr, GFP_KERNEL);
Thomas Grafed661182013-03-29 14:46:50 +010095}
96
Pravin B Shelar46df7b82012-02-22 19:58:59 -080097/**
Jesse Grossccb13522011-10-25 19:26:31 -070098 * DOC: Locking:
99 *
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700100 * All writes e.g. Writes to device state (add/remove datapath, port, set
101 * operations on vports, etc.), Writes to other state (flow table
102 * modifications, set miscellaneous datapath parameters, etc.) are protected
103 * by ovs_lock.
Jesse Grossccb13522011-10-25 19:26:31 -0700104 *
105 * Reads are protected by RCU.
106 *
107 * There are a few special cases (mostly stats) that have their own
108 * synchronization but they nest under all of above and don't interact with
109 * each other.
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700110 *
111 * The RTNL lock nests inside ovs_mutex.
Jesse Grossccb13522011-10-25 19:26:31 -0700112 */
113
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700114static DEFINE_MUTEX(ovs_mutex);
115
116void ovs_lock(void)
117{
118 mutex_lock(&ovs_mutex);
119}
120
121void ovs_unlock(void)
122{
123 mutex_unlock(&ovs_mutex);
124}
125
126#ifdef CONFIG_LOCKDEP
127int lockdep_ovsl_is_held(void)
128{
129 if (debug_locks)
130 return lockdep_is_held(&ovs_mutex);
131 else
132 return 1;
133}
134#endif
135
Jesse Grossccb13522011-10-25 19:26:31 -0700136static struct vport *new_vport(const struct vport_parms *);
Thomas Graf8055a892013-12-13 15:22:20 +0100137static int queue_gso_packets(struct datapath *dp, struct sk_buff *,
Jesse Grossccb13522011-10-25 19:26:31 -0700138 const struct dp_upcall_info *);
Thomas Graf8055a892013-12-13 15:22:20 +0100139static int queue_userspace_packet(struct datapath *dp, struct sk_buff *,
Jesse Grossccb13522011-10-25 19:26:31 -0700140 const struct dp_upcall_info *);
141
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700142/* Must be called with rcu_read_lock or ovs_mutex. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800143static struct datapath *get_dp(struct net *net, int dp_ifindex)
Jesse Grossccb13522011-10-25 19:26:31 -0700144{
145 struct datapath *dp = NULL;
146 struct net_device *dev;
147
148 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800149 dev = dev_get_by_index_rcu(net, dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -0700150 if (dev) {
151 struct vport *vport = ovs_internal_dev_get_vport(dev);
152 if (vport)
153 dp = vport->dp;
154 }
155 rcu_read_unlock();
156
157 return dp;
158}
159
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700160/* Must be called with rcu_read_lock or ovs_mutex. */
Stephen Hemminger443cd882013-12-17 19:22:48 +0000161static const char *ovs_dp_name(const struct datapath *dp)
Jesse Grossccb13522011-10-25 19:26:31 -0700162{
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700163 struct vport *vport = ovs_vport_ovsl_rcu(dp, OVSP_LOCAL);
Jesse Grossccb13522011-10-25 19:26:31 -0700164 return vport->ops->get_name(vport);
165}
166
167static int get_dpifindex(struct datapath *dp)
168{
169 struct vport *local;
170 int ifindex;
171
172 rcu_read_lock();
173
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700174 local = ovs_vport_rcu(dp, OVSP_LOCAL);
Jesse Grossccb13522011-10-25 19:26:31 -0700175 if (local)
Thomas Grafcff63a52013-04-29 13:06:41 +0000176 ifindex = netdev_vport_priv(local)->dev->ifindex;
Jesse Grossccb13522011-10-25 19:26:31 -0700177 else
178 ifindex = 0;
179
180 rcu_read_unlock();
181
182 return ifindex;
183}
184
185static void destroy_dp_rcu(struct rcu_head *rcu)
186{
187 struct datapath *dp = container_of(rcu, struct datapath, rcu);
188
Jesse Grossccb13522011-10-25 19:26:31 -0700189 free_percpu(dp->stats_percpu);
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800190 release_net(ovs_dp_get_net(dp));
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700191 kfree(dp->ports);
Jesse Grossccb13522011-10-25 19:26:31 -0700192 kfree(dp);
193}
194
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700195static struct hlist_head *vport_hash_bucket(const struct datapath *dp,
196 u16 port_no)
197{
198 return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)];
199}
200
Jarno Rajahalmebb6f9a72014-05-05 11:32:17 -0700201/* Called with ovs_mutex or RCU read lock. */
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700202struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no)
203{
204 struct vport *vport;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700205 struct hlist_head *head;
206
207 head = vport_hash_bucket(dp, port_no);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800208 hlist_for_each_entry_rcu(vport, head, dp_hash_node) {
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700209 if (vport->port_no == port_no)
210 return vport;
211 }
212 return NULL;
213}
214
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700215/* Called with ovs_mutex. */
Jesse Grossccb13522011-10-25 19:26:31 -0700216static struct vport *new_vport(const struct vport_parms *parms)
217{
218 struct vport *vport;
219
220 vport = ovs_vport_add(parms);
221 if (!IS_ERR(vport)) {
222 struct datapath *dp = parms->dp;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700223 struct hlist_head *head = vport_hash_bucket(dp, vport->port_no);
Jesse Grossccb13522011-10-25 19:26:31 -0700224
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700225 hlist_add_head_rcu(&vport->dp_hash_node, head);
Jesse Grossccb13522011-10-25 19:26:31 -0700226 }
Jesse Grossccb13522011-10-25 19:26:31 -0700227 return vport;
228}
229
Jesse Grossccb13522011-10-25 19:26:31 -0700230void ovs_dp_detach_port(struct vport *p)
231{
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700232 ASSERT_OVSL();
Jesse Grossccb13522011-10-25 19:26:31 -0700233
234 /* First drop references to device. */
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700235 hlist_del_rcu(&p->dp_hash_node);
Jesse Grossccb13522011-10-25 19:26:31 -0700236
237 /* Then destroy it. */
238 ovs_vport_del(p);
239}
240
241/* Must be called with rcu_read_lock. */
242void ovs_dp_process_received_packet(struct vport *p, struct sk_buff *skb)
243{
244 struct datapath *dp = p->dp;
245 struct sw_flow *flow;
246 struct dp_stats_percpu *stats;
247 struct sw_flow_key key;
248 u64 *stats_counter;
Andy Zhou1bd71162013-10-22 10:42:46 -0700249 u32 n_mask_hit;
Jesse Grossccb13522011-10-25 19:26:31 -0700250 int error;
Jesse Grossccb13522011-10-25 19:26:31 -0700251
Shan Wei404f2f12012-11-13 09:52:25 +0800252 stats = this_cpu_ptr(dp->stats_percpu);
Jesse Grossccb13522011-10-25 19:26:31 -0700253
254 /* Extract flow from 'skb' into 'key'. */
Andy Zhou03f0d912013-08-07 20:01:00 -0700255 error = ovs_flow_extract(skb, p->port_no, &key);
Jesse Grossccb13522011-10-25 19:26:31 -0700256 if (unlikely(error)) {
257 kfree_skb(skb);
258 return;
259 }
260
261 /* Look up flow. */
Andy Zhou5bb50632013-11-25 10:42:46 -0800262 flow = ovs_flow_tbl_lookup_stats(&dp->table, &key, &n_mask_hit);
Jesse Grossccb13522011-10-25 19:26:31 -0700263 if (unlikely(!flow)) {
264 struct dp_upcall_info upcall;
265
266 upcall.cmd = OVS_PACKET_CMD_MISS;
267 upcall.key = &key;
268 upcall.userdata = NULL;
Alex Wang5cd667b2014-07-17 15:14:13 -0700269 upcall.portid = ovs_vport_find_upcall_portid(p, skb);
Jesse Grossccb13522011-10-25 19:26:31 -0700270 ovs_dp_upcall(dp, skb, &upcall);
271 consume_skb(skb);
272 stats_counter = &stats->n_missed;
273 goto out;
274 }
275
276 OVS_CB(skb)->flow = flow;
Andy Zhou03f0d912013-08-07 20:01:00 -0700277 OVS_CB(skb)->pkt_key = &key;
Jesse Grossccb13522011-10-25 19:26:31 -0700278
Ben Pfaffad552002014-05-06 16:48:38 -0700279 ovs_flow_stats_update(OVS_CB(skb)->flow, key.tp.flags, skb);
Jesse Grossccb13522011-10-25 19:26:31 -0700280 ovs_execute_actions(dp, skb);
Pravin B Shelare298e502013-10-29 17:22:21 -0700281 stats_counter = &stats->n_hit;
Jesse Grossccb13522011-10-25 19:26:31 -0700282
283out:
284 /* Update datapath statistics. */
WANG Congdf9d9fd2014-02-14 15:10:46 -0800285 u64_stats_update_begin(&stats->syncp);
Jesse Grossccb13522011-10-25 19:26:31 -0700286 (*stats_counter)++;
Andy Zhou1bd71162013-10-22 10:42:46 -0700287 stats->n_mask_hit += n_mask_hit;
WANG Congdf9d9fd2014-02-14 15:10:46 -0800288 u64_stats_update_end(&stats->syncp);
Jesse Grossccb13522011-10-25 19:26:31 -0700289}
290
Jesse Grossccb13522011-10-25 19:26:31 -0700291int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800292 const struct dp_upcall_info *upcall_info)
Jesse Grossccb13522011-10-25 19:26:31 -0700293{
294 struct dp_stats_percpu *stats;
Jesse Grossccb13522011-10-25 19:26:31 -0700295 int err;
296
Eric W. Biederman15e47302012-09-07 20:12:54 +0000297 if (upcall_info->portid == 0) {
Jesse Grossccb13522011-10-25 19:26:31 -0700298 err = -ENOTCONN;
299 goto err;
300 }
301
Jesse Grossccb13522011-10-25 19:26:31 -0700302 if (!skb_is_gso(skb))
Thomas Graf8055a892013-12-13 15:22:20 +0100303 err = queue_userspace_packet(dp, skb, upcall_info);
Jesse Grossccb13522011-10-25 19:26:31 -0700304 else
Thomas Graf8055a892013-12-13 15:22:20 +0100305 err = queue_gso_packets(dp, skb, upcall_info);
Jesse Grossccb13522011-10-25 19:26:31 -0700306 if (err)
307 goto err;
308
309 return 0;
310
311err:
Shan Wei404f2f12012-11-13 09:52:25 +0800312 stats = this_cpu_ptr(dp->stats_percpu);
Jesse Grossccb13522011-10-25 19:26:31 -0700313
WANG Congdf9d9fd2014-02-14 15:10:46 -0800314 u64_stats_update_begin(&stats->syncp);
Jesse Grossccb13522011-10-25 19:26:31 -0700315 stats->n_lost++;
WANG Congdf9d9fd2014-02-14 15:10:46 -0800316 u64_stats_update_end(&stats->syncp);
Jesse Grossccb13522011-10-25 19:26:31 -0700317
318 return err;
319}
320
Thomas Graf8055a892013-12-13 15:22:20 +0100321static int queue_gso_packets(struct datapath *dp, struct sk_buff *skb,
Jesse Grossccb13522011-10-25 19:26:31 -0700322 const struct dp_upcall_info *upcall_info)
323{
Ben Pfaffa1b5d0d2012-07-20 14:47:54 -0700324 unsigned short gso_type = skb_shinfo(skb)->gso_type;
Jesse Grossccb13522011-10-25 19:26:31 -0700325 struct dp_upcall_info later_info;
326 struct sw_flow_key later_key;
327 struct sk_buff *segs, *nskb;
328 int err;
329
Thomas Graf09c5e602013-12-13 15:22:22 +0100330 segs = __skb_gso_segment(skb, NETIF_F_SG, false);
Pravin B Shelar92e5dfc2012-07-20 14:46:29 -0700331 if (IS_ERR(segs))
332 return PTR_ERR(segs);
Jesse Grossccb13522011-10-25 19:26:31 -0700333
334 /* Queue all of the segments. */
335 skb = segs;
336 do {
Thomas Graf8055a892013-12-13 15:22:20 +0100337 err = queue_userspace_packet(dp, skb, upcall_info);
Jesse Grossccb13522011-10-25 19:26:31 -0700338 if (err)
339 break;
340
Ben Pfaffa1b5d0d2012-07-20 14:47:54 -0700341 if (skb == segs && gso_type & SKB_GSO_UDP) {
Jesse Grossccb13522011-10-25 19:26:31 -0700342 /* The initial flow key extracted by ovs_flow_extract()
343 * in this case is for a first fragment, so we need to
344 * properly mark later fragments.
345 */
346 later_key = *upcall_info->key;
347 later_key.ip.frag = OVS_FRAG_TYPE_LATER;
348
349 later_info = *upcall_info;
350 later_info.key = &later_key;
351 upcall_info = &later_info;
352 }
353 } while ((skb = skb->next));
354
355 /* Free all of the segments. */
356 skb = segs;
357 do {
358 nskb = skb->next;
359 if (err)
360 kfree_skb(skb);
361 else
362 consume_skb(skb);
363 } while ((skb = nskb));
364 return err;
365}
366
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100367static size_t key_attr_size(void)
368{
369 return nla_total_size(4) /* OVS_KEY_ATTR_PRIORITY */
Pravin B Shelar7d5437c2013-06-17 17:50:18 -0700370 + nla_total_size(0) /* OVS_KEY_ATTR_TUNNEL */
371 + nla_total_size(8) /* OVS_TUNNEL_KEY_ATTR_ID */
372 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_SRC */
373 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_DST */
374 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TOS */
375 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TTL */
376 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT */
377 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_CSUM */
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100378 + nla_total_size(4) /* OVS_KEY_ATTR_IN_PORT */
379 + nla_total_size(4) /* OVS_KEY_ATTR_SKB_MARK */
380 + nla_total_size(12) /* OVS_KEY_ATTR_ETHERNET */
381 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
382 + nla_total_size(4) /* OVS_KEY_ATTR_8021Q */
383 + nla_total_size(0) /* OVS_KEY_ATTR_ENCAP */
384 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
385 + nla_total_size(40) /* OVS_KEY_ATTR_IPV6 */
386 + nla_total_size(2) /* OVS_KEY_ATTR_ICMPV6 */
387 + nla_total_size(28); /* OVS_KEY_ATTR_ND */
388}
389
Thomas Grafbda56f12013-12-13 15:22:21 +0100390static size_t upcall_msg_size(const struct nlattr *userdata,
391 unsigned int hdrlen)
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100392{
393 size_t size = NLMSG_ALIGN(sizeof(struct ovs_header))
Thomas Grafbda56f12013-12-13 15:22:21 +0100394 + nla_total_size(hdrlen) /* OVS_PACKET_ATTR_PACKET */
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100395 + nla_total_size(key_attr_size()); /* OVS_PACKET_ATTR_KEY */
396
397 /* OVS_PACKET_ATTR_USERDATA */
398 if (userdata)
399 size += NLA_ALIGN(userdata->nla_len);
400
401 return size;
402}
403
Thomas Graf8055a892013-12-13 15:22:20 +0100404static int queue_userspace_packet(struct datapath *dp, struct sk_buff *skb,
Jesse Grossccb13522011-10-25 19:26:31 -0700405 const struct dp_upcall_info *upcall_info)
406{
407 struct ovs_header *upcall;
408 struct sk_buff *nskb = NULL;
409 struct sk_buff *user_skb; /* to be queued to userspace */
410 struct nlattr *nla;
Thomas Graf795449d2013-11-30 13:21:32 +0100411 struct genl_info info = {
Thomas Graf8055a892013-12-13 15:22:20 +0100412 .dst_sk = ovs_dp_get_net(dp)->genl_sock,
Thomas Graf795449d2013-11-30 13:21:32 +0100413 .snd_portid = upcall_info->portid,
414 };
415 size_t len;
Thomas Grafbda56f12013-12-13 15:22:21 +0100416 unsigned int hlen;
Thomas Graf8055a892013-12-13 15:22:20 +0100417 int err, dp_ifindex;
418
419 dp_ifindex = get_dpifindex(dp);
420 if (!dp_ifindex)
421 return -ENODEV;
Jesse Grossccb13522011-10-25 19:26:31 -0700422
423 if (vlan_tx_tag_present(skb)) {
424 nskb = skb_clone(skb, GFP_ATOMIC);
425 if (!nskb)
426 return -ENOMEM;
427
Patrick McHardy86a9bad2013-04-19 02:04:30 +0000428 nskb = __vlan_put_tag(nskb, nskb->vlan_proto, vlan_tx_tag_get(nskb));
Dan Carpenter8aa51d62012-05-13 08:44:18 +0000429 if (!nskb)
Jesse Grossccb13522011-10-25 19:26:31 -0700430 return -ENOMEM;
431
432 nskb->vlan_tci = 0;
433 skb = nskb;
434 }
435
436 if (nla_attr_size(skb->len) > USHRT_MAX) {
437 err = -EFBIG;
438 goto out;
439 }
440
Thomas Grafbda56f12013-12-13 15:22:21 +0100441 /* Complete checksum if needed */
442 if (skb->ip_summed == CHECKSUM_PARTIAL &&
443 (err = skb_checksum_help(skb)))
444 goto out;
445
446 /* Older versions of OVS user space enforce alignment of the last
447 * Netlink attribute to NLA_ALIGNTO which would require extensive
448 * padding logic. Only perform zerocopy if padding is not required.
449 */
450 if (dp->user_features & OVS_DP_F_UNALIGNED)
451 hlen = skb_zerocopy_headlen(skb);
452 else
453 hlen = skb->len;
454
455 len = upcall_msg_size(upcall_info->userdata, hlen);
Thomas Graf795449d2013-11-30 13:21:32 +0100456 user_skb = genlmsg_new_unicast(len, &info, GFP_ATOMIC);
Jesse Grossccb13522011-10-25 19:26:31 -0700457 if (!user_skb) {
458 err = -ENOMEM;
459 goto out;
460 }
461
462 upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
463 0, upcall_info->cmd);
464 upcall->dp_ifindex = dp_ifindex;
465
466 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
Pravin B Shelare6445712013-10-03 18:16:47 -0700467 ovs_nla_put_flow(upcall_info->key, upcall_info->key, user_skb);
Jesse Grossccb13522011-10-25 19:26:31 -0700468 nla_nest_end(user_skb, nla);
469
470 if (upcall_info->userdata)
Ben Pfaff44901082013-02-15 17:29:22 -0800471 __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA,
472 nla_len(upcall_info->userdata),
473 nla_data(upcall_info->userdata));
Jesse Grossccb13522011-10-25 19:26:31 -0700474
Thomas Grafbda56f12013-12-13 15:22:21 +0100475 /* Only reserve room for attribute header, packet data is added
476 * in skb_zerocopy() */
477 if (!(nla = nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, 0))) {
478 err = -ENOBUFS;
479 goto out;
480 }
481 nla->nla_len = nla_attr_size(skb->len);
Jesse Grossccb13522011-10-25 19:26:31 -0700482
Zoltan Kiss36d5fe62014-03-26 22:37:45 +0000483 err = skb_zerocopy(user_skb, skb, skb->len, hlen);
484 if (err)
485 goto out;
Jesse Grossccb13522011-10-25 19:26:31 -0700486
Thomas Grafaea0bb42014-01-14 16:27:49 +0000487 /* Pad OVS_PACKET_ATTR_PACKET if linear copy was performed */
488 if (!(dp->user_features & OVS_DP_F_UNALIGNED)) {
489 size_t plen = NLA_ALIGN(user_skb->len) - user_skb->len;
490
491 if (plen > 0)
492 memset(skb_put(user_skb, plen), 0, plen);
493 }
494
Thomas Grafbda56f12013-12-13 15:22:21 +0100495 ((struct nlmsghdr *) user_skb->data)->nlmsg_len = user_skb->len;
496
Thomas Graf8055a892013-12-13 15:22:20 +0100497 err = genlmsg_unicast(ovs_dp_get_net(dp), user_skb, upcall_info->portid);
Jesse Grossccb13522011-10-25 19:26:31 -0700498out:
Zoltan Kiss36d5fe62014-03-26 22:37:45 +0000499 if (err)
500 skb_tx_error(skb);
Jesse Grossccb13522011-10-25 19:26:31 -0700501 kfree_skb(nskb);
502 return err;
503}
504
Jesse Grossccb13522011-10-25 19:26:31 -0700505static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
506{
507 struct ovs_header *ovs_header = info->userhdr;
508 struct nlattr **a = info->attrs;
509 struct sw_flow_actions *acts;
510 struct sk_buff *packet;
511 struct sw_flow *flow;
512 struct datapath *dp;
513 struct ethhdr *eth;
514 int len;
515 int err;
Jesse Grossccb13522011-10-25 19:26:31 -0700516
517 err = -EINVAL;
518 if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
Thomas Grafdded45fc2013-03-29 14:46:47 +0100519 !a[OVS_PACKET_ATTR_ACTIONS])
Jesse Grossccb13522011-10-25 19:26:31 -0700520 goto err;
521
522 len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
523 packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
524 err = -ENOMEM;
525 if (!packet)
526 goto err;
527 skb_reserve(packet, NET_IP_ALIGN);
528
Thomas Graf32686a92013-03-29 14:46:48 +0100529 nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
Jesse Grossccb13522011-10-25 19:26:31 -0700530
531 skb_reset_mac_header(packet);
532 eth = eth_hdr(packet);
533
534 /* Normally, setting the skb 'protocol' field would be handled by a
535 * call to eth_type_trans(), but it assumes there's a sending
536 * device, which we may not have. */
Simon Hormane5c5d222013-03-28 13:38:25 +0900537 if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN)
Jesse Grossccb13522011-10-25 19:26:31 -0700538 packet->protocol = eth->h_proto;
539 else
540 packet->protocol = htons(ETH_P_802_2);
541
542 /* Build an sw_flow for sending this packet. */
Jarno Rajahalme23dabf82014-03-27 12:35:23 -0700543 flow = ovs_flow_alloc();
Jesse Grossccb13522011-10-25 19:26:31 -0700544 err = PTR_ERR(flow);
545 if (IS_ERR(flow))
546 goto err_kfree_skb;
547
Andy Zhou03f0d912013-08-07 20:01:00 -0700548 err = ovs_flow_extract(packet, -1, &flow->key);
Jesse Grossccb13522011-10-25 19:26:31 -0700549 if (err)
550 goto err_flow_free;
551
Pravin B Shelare6445712013-10-03 18:16:47 -0700552 err = ovs_nla_get_flow_metadata(flow, a[OVS_PACKET_ATTR_KEY]);
Jesse Grossccb13522011-10-25 19:26:31 -0700553 if (err)
554 goto err_flow_free;
Pravin B Shelare6445712013-10-03 18:16:47 -0700555 acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_PACKET_ATTR_ACTIONS]));
Jesse Grossccb13522011-10-25 19:26:31 -0700556 err = PTR_ERR(acts);
557 if (IS_ERR(acts))
558 goto err_flow_free;
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700559
Pravin B Shelare6445712013-10-03 18:16:47 -0700560 err = ovs_nla_copy_actions(a[OVS_PACKET_ATTR_ACTIONS],
561 &flow->key, 0, &acts);
Jesse Grossccb13522011-10-25 19:26:31 -0700562 rcu_assign_pointer(flow->sf_acts, acts);
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700563 if (err)
564 goto err_flow_free;
Jesse Grossccb13522011-10-25 19:26:31 -0700565
566 OVS_CB(packet)->flow = flow;
Andy Zhou03f0d912013-08-07 20:01:00 -0700567 OVS_CB(packet)->pkt_key = &flow->key;
Jesse Grossccb13522011-10-25 19:26:31 -0700568 packet->priority = flow->key.phy.priority;
Ansis Atteka39c7caeb2012-11-26 11:24:11 -0800569 packet->mark = flow->key.phy.skb_mark;
Jesse Grossccb13522011-10-25 19:26:31 -0700570
571 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800572 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -0700573 err = -ENODEV;
574 if (!dp)
575 goto err_unlock;
576
577 local_bh_disable();
578 err = ovs_execute_actions(dp, packet);
579 local_bh_enable();
580 rcu_read_unlock();
581
Andy Zhou03f0d912013-08-07 20:01:00 -0700582 ovs_flow_free(flow, false);
Jesse Grossccb13522011-10-25 19:26:31 -0700583 return err;
584
585err_unlock:
586 rcu_read_unlock();
587err_flow_free:
Andy Zhou03f0d912013-08-07 20:01:00 -0700588 ovs_flow_free(flow, false);
Jesse Grossccb13522011-10-25 19:26:31 -0700589err_kfree_skb:
590 kfree_skb(packet);
591err:
592 return err;
593}
594
595static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
Thomas Grafdded45fc2013-03-29 14:46:47 +0100596 [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
Jesse Grossccb13522011-10-25 19:26:31 -0700597 [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
598 [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
599};
600
Johannes Berg4534de82013-11-14 17:14:46 +0100601static const struct genl_ops dp_packet_genl_ops[] = {
Jesse Grossccb13522011-10-25 19:26:31 -0700602 { .cmd = OVS_PACKET_CMD_EXECUTE,
603 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
604 .policy = packet_policy,
605 .doit = ovs_packet_cmd_execute
606 }
607};
608
Pravin B Shelar0c200ef2014-05-06 16:44:50 -0700609static struct genl_family dp_packet_genl_family = {
610 .id = GENL_ID_GENERATE,
611 .hdrsize = sizeof(struct ovs_header),
612 .name = OVS_PACKET_FAMILY,
613 .version = OVS_PACKET_VERSION,
614 .maxattr = OVS_PACKET_ATTR_MAX,
615 .netnsok = true,
616 .parallel_ops = true,
617 .ops = dp_packet_genl_ops,
618 .n_ops = ARRAY_SIZE(dp_packet_genl_ops),
619};
620
Andy Zhou1bd71162013-10-22 10:42:46 -0700621static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats,
622 struct ovs_dp_megaflow_stats *mega_stats)
Jesse Grossccb13522011-10-25 19:26:31 -0700623{
624 int i;
Jesse Grossccb13522011-10-25 19:26:31 -0700625
Andy Zhou1bd71162013-10-22 10:42:46 -0700626 memset(mega_stats, 0, sizeof(*mega_stats));
627
Pravin B Shelarb637e492013-10-04 00:14:23 -0700628 stats->n_flows = ovs_flow_tbl_count(&dp->table);
Andy Zhou1bd71162013-10-22 10:42:46 -0700629 mega_stats->n_masks = ovs_flow_tbl_num_masks(&dp->table);
Jesse Grossccb13522011-10-25 19:26:31 -0700630
631 stats->n_hit = stats->n_missed = stats->n_lost = 0;
Andy Zhou1bd71162013-10-22 10:42:46 -0700632
Jesse Grossccb13522011-10-25 19:26:31 -0700633 for_each_possible_cpu(i) {
634 const struct dp_stats_percpu *percpu_stats;
635 struct dp_stats_percpu local_stats;
636 unsigned int start;
637
638 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
639
640 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700641 start = u64_stats_fetch_begin_irq(&percpu_stats->syncp);
Jesse Grossccb13522011-10-25 19:26:31 -0700642 local_stats = *percpu_stats;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700643 } while (u64_stats_fetch_retry_irq(&percpu_stats->syncp, start));
Jesse Grossccb13522011-10-25 19:26:31 -0700644
645 stats->n_hit += local_stats.n_hit;
646 stats->n_missed += local_stats.n_missed;
647 stats->n_lost += local_stats.n_lost;
Andy Zhou1bd71162013-10-22 10:42:46 -0700648 mega_stats->n_mask_hit += local_stats.n_mask_hit;
Jesse Grossccb13522011-10-25 19:26:31 -0700649 }
650}
651
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100652static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts)
653{
654 return NLMSG_ALIGN(sizeof(struct ovs_header))
655 + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_KEY */
Andy Zhou03f0d912013-08-07 20:01:00 -0700656 + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_MASK */
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100657 + nla_total_size(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
658 + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
659 + nla_total_size(8) /* OVS_FLOW_ATTR_USED */
660 + nla_total_size(acts->actions_len); /* OVS_FLOW_ATTR_ACTIONS */
661}
662
Jarno Rajahalmebb6f9a72014-05-05 11:32:17 -0700663/* Called with ovs_mutex or RCU read lock. */
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700664static int ovs_flow_cmd_fill_info(const struct sw_flow *flow, int dp_ifindex,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000665 struct sk_buff *skb, u32 portid,
Jesse Grossccb13522011-10-25 19:26:31 -0700666 u32 seq, u32 flags, u8 cmd)
667{
668 const int skb_orig_len = skb->len;
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700669 struct nlattr *start;
Jesse Grossccb13522011-10-25 19:26:31 -0700670 struct ovs_flow_stats stats;
Pravin B Shelare298e502013-10-29 17:22:21 -0700671 __be16 tcp_flags;
672 unsigned long used;
Jesse Grossccb13522011-10-25 19:26:31 -0700673 struct ovs_header *ovs_header;
674 struct nlattr *nla;
Jesse Grossccb13522011-10-25 19:26:31 -0700675 int err;
676
Eric W. Biederman15e47302012-09-07 20:12:54 +0000677 ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family, flags, cmd);
Jesse Grossccb13522011-10-25 19:26:31 -0700678 if (!ovs_header)
679 return -EMSGSIZE;
680
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700681 ovs_header->dp_ifindex = dp_ifindex;
Jesse Grossccb13522011-10-25 19:26:31 -0700682
Andy Zhou03f0d912013-08-07 20:01:00 -0700683 /* Fill flow key. */
Jesse Grossccb13522011-10-25 19:26:31 -0700684 nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
685 if (!nla)
686 goto nla_put_failure;
Andy Zhou03f0d912013-08-07 20:01:00 -0700687
Pravin B Shelare6445712013-10-03 18:16:47 -0700688 err = ovs_nla_put_flow(&flow->unmasked_key, &flow->unmasked_key, skb);
Jesse Grossccb13522011-10-25 19:26:31 -0700689 if (err)
690 goto error;
691 nla_nest_end(skb, nla);
692
Andy Zhou03f0d912013-08-07 20:01:00 -0700693 nla = nla_nest_start(skb, OVS_FLOW_ATTR_MASK);
694 if (!nla)
695 goto nla_put_failure;
696
Pravin B Shelare6445712013-10-03 18:16:47 -0700697 err = ovs_nla_put_flow(&flow->key, &flow->mask->key, skb);
Andy Zhou03f0d912013-08-07 20:01:00 -0700698 if (err)
699 goto error;
700
701 nla_nest_end(skb, nla);
702
Pravin B Shelare298e502013-10-29 17:22:21 -0700703 ovs_flow_stats_get(flow, &stats, &used, &tcp_flags);
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700704
David S. Miller028d6a62012-03-29 23:20:48 -0400705 if (used &&
706 nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
707 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -0700708
David S. Miller028d6a62012-03-29 23:20:48 -0400709 if (stats.n_packets &&
Pravin B Shelare298e502013-10-29 17:22:21 -0700710 nla_put(skb, OVS_FLOW_ATTR_STATS, sizeof(struct ovs_flow_stats), &stats))
David S. Miller028d6a62012-03-29 23:20:48 -0400711 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -0700712
Pravin B Shelare298e502013-10-29 17:22:21 -0700713 if ((u8)ntohs(tcp_flags) &&
714 nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, (u8)ntohs(tcp_flags)))
David S. Miller028d6a62012-03-29 23:20:48 -0400715 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -0700716
717 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
718 * this is the first flow to be dumped into 'skb'. This is unusual for
719 * Netlink but individual action lists can be longer than
720 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
721 * The userspace caller can always fetch the actions separately if it
722 * really wants them. (Most userspace callers in fact don't care.)
723 *
724 * This can only fail for dump operations because the skb is always
725 * properly sized for single flows.
726 */
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700727 start = nla_nest_start(skb, OVS_FLOW_ATTR_ACTIONS);
728 if (start) {
Pravin B Shelard57170b2013-07-30 15:39:39 -0700729 const struct sw_flow_actions *sf_acts;
730
Jesse Gross663efa32013-12-03 10:58:53 -0800731 sf_acts = rcu_dereference_ovsl(flow->sf_acts);
Pravin B Shelare6445712013-10-03 18:16:47 -0700732 err = ovs_nla_put_actions(sf_acts->actions,
733 sf_acts->actions_len, skb);
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700734
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700735 if (!err)
736 nla_nest_end(skb, start);
737 else {
738 if (skb_orig_len)
739 goto error;
740
741 nla_nest_cancel(skb, start);
742 }
743 } else if (skb_orig_len)
744 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -0700745
746 return genlmsg_end(skb, ovs_header);
747
748nla_put_failure:
749 err = -EMSGSIZE;
750error:
751 genlmsg_cancel(skb, ovs_header);
752 return err;
753}
754
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700755/* May not be called with RCU read lock. */
756static struct sk_buff *ovs_flow_cmd_alloc_info(const struct sw_flow_actions *acts,
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700757 struct genl_info *info,
758 bool always)
Jesse Grossccb13522011-10-25 19:26:31 -0700759{
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700760 struct sk_buff *skb;
Jesse Grossccb13522011-10-25 19:26:31 -0700761
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700762 if (!always && !ovs_must_notify(info, &ovs_dp_flow_multicast_group))
763 return NULL;
764
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700765 skb = genlmsg_new_unicast(ovs_flow_cmd_msg_size(acts), info, GFP_KERNEL);
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700766 if (!skb)
767 return ERR_PTR(-ENOMEM);
768
769 return skb;
Jesse Grossccb13522011-10-25 19:26:31 -0700770}
771
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700772/* Called with ovs_mutex. */
773static struct sk_buff *ovs_flow_cmd_build_info(const struct sw_flow *flow,
774 int dp_ifindex,
775 struct genl_info *info, u8 cmd,
776 bool always)
Jesse Grossccb13522011-10-25 19:26:31 -0700777{
778 struct sk_buff *skb;
779 int retval;
780
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700781 skb = ovs_flow_cmd_alloc_info(ovsl_dereference(flow->sf_acts), info,
782 always);
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700783 if (!skb || IS_ERR(skb))
784 return skb;
Jesse Grossccb13522011-10-25 19:26:31 -0700785
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700786 retval = ovs_flow_cmd_fill_info(flow, dp_ifindex, skb,
787 info->snd_portid, info->snd_seq, 0,
788 cmd);
Jesse Grossccb13522011-10-25 19:26:31 -0700789 BUG_ON(retval < 0);
790 return skb;
791}
792
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700793static int ovs_flow_cmd_new(struct sk_buff *skb, struct genl_info *info)
Jesse Grossccb13522011-10-25 19:26:31 -0700794{
795 struct nlattr **a = info->attrs;
796 struct ovs_header *ovs_header = info->userhdr;
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700797 struct sw_flow *flow, *new_flow;
Andy Zhou03f0d912013-08-07 20:01:00 -0700798 struct sw_flow_mask mask;
Jesse Grossccb13522011-10-25 19:26:31 -0700799 struct sk_buff *reply;
800 struct datapath *dp;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700801 struct sw_flow_actions *acts;
802 struct sw_flow_match match;
803 int error;
804
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700805 /* Must have key and actions. */
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700806 error = -EINVAL;
807 if (!a[OVS_FLOW_ATTR_KEY])
808 goto error;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700809 if (!a[OVS_FLOW_ATTR_ACTIONS])
810 goto error;
811
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700812 /* Most of the time we need to allocate a new flow, do it before
813 * locking.
814 */
815 new_flow = ovs_flow_alloc();
816 if (IS_ERR(new_flow)) {
817 error = PTR_ERR(new_flow);
818 goto error;
819 }
820
821 /* Extract key. */
822 ovs_match_init(&match, &new_flow->unmasked_key, &mask);
823 error = ovs_nla_get_match(&match,
824 a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
825 if (error)
826 goto err_kfree_flow;
827
828 ovs_flow_mask_key(&new_flow->key, &new_flow->unmasked_key, &mask);
829
830 /* Validate actions. */
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700831 acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_FLOW_ATTR_ACTIONS]));
832 error = PTR_ERR(acts);
833 if (IS_ERR(acts))
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700834 goto err_kfree_flow;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700835
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700836 error = ovs_nla_copy_actions(a[OVS_FLOW_ATTR_ACTIONS], &new_flow->key,
837 0, &acts);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700838 if (error) {
839 OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700840 goto err_kfree_acts;
841 }
842
843 reply = ovs_flow_cmd_alloc_info(acts, info, false);
844 if (IS_ERR(reply)) {
845 error = PTR_ERR(reply);
846 goto err_kfree_acts;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700847 }
848
849 ovs_lock();
850 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700851 if (unlikely(!dp)) {
852 error = -ENODEV;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700853 goto err_unlock_ovs;
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700854 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700855 /* Check if this is a duplicate flow */
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700856 flow = ovs_flow_tbl_lookup(&dp->table, &new_flow->unmasked_key);
857 if (likely(!flow)) {
858 rcu_assign_pointer(new_flow->sf_acts, acts);
859
860 /* Put flow in bucket. */
861 error = ovs_flow_tbl_insert(&dp->table, new_flow, &mask);
862 if (unlikely(error)) {
863 acts = NULL;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700864 goto err_unlock_ovs;
865 }
866
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700867 if (unlikely(reply)) {
868 error = ovs_flow_cmd_fill_info(new_flow,
869 ovs_header->dp_ifindex,
870 reply, info->snd_portid,
871 info->snd_seq, 0,
872 OVS_FLOW_CMD_NEW);
873 BUG_ON(error < 0);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700874 }
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700875 ovs_unlock();
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700876 } else {
877 struct sw_flow_actions *old_acts;
878
879 /* Bail out if we're not allowed to modify an existing flow.
880 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
881 * because Generic Netlink treats the latter as a dump
882 * request. We also accept NLM_F_EXCL in case that bug ever
883 * gets fixed.
884 */
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700885 if (unlikely(info->nlhdr->nlmsg_flags & (NLM_F_CREATE
886 | NLM_F_EXCL))) {
887 error = -EEXIST;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700888 goto err_unlock_ovs;
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700889 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700890 /* The unmasked key has to be the same for flow updates. */
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700891 if (unlikely(!ovs_flow_cmp_unmasked_key(flow, &match))) {
Alex Wang4a46b242014-06-30 20:30:29 -0700892 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
893 if (!flow) {
894 error = -ENOENT;
895 goto err_unlock_ovs;
896 }
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700897 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700898 /* Update actions. */
899 old_acts = ovsl_dereference(flow->sf_acts);
900 rcu_assign_pointer(flow->sf_acts, acts);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700901
902 if (unlikely(reply)) {
903 error = ovs_flow_cmd_fill_info(flow,
904 ovs_header->dp_ifindex,
905 reply, info->snd_portid,
906 info->snd_seq, 0,
907 OVS_FLOW_CMD_NEW);
908 BUG_ON(error < 0);
909 }
910 ovs_unlock();
911
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700912 ovs_nla_free_flow_actions(old_acts);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700913 ovs_flow_free(new_flow, false);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700914 }
915
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700916 if (reply)
917 ovs_notify(&dp_flow_genl_family, reply, info);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700918 return 0;
919
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700920err_unlock_ovs:
921 ovs_unlock();
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700922 kfree_skb(reply);
923err_kfree_acts:
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700924 kfree(acts);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700925err_kfree_flow:
926 ovs_flow_free(new_flow, false);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700927error:
928 return error;
929}
930
931static int ovs_flow_cmd_set(struct sk_buff *skb, struct genl_info *info)
932{
933 struct nlattr **a = info->attrs;
934 struct ovs_header *ovs_header = info->userhdr;
935 struct sw_flow_key key, masked_key;
936 struct sw_flow *flow;
937 struct sw_flow_mask mask;
938 struct sk_buff *reply = NULL;
939 struct datapath *dp;
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700940 struct sw_flow_actions *old_acts = NULL, *acts = NULL;
Andy Zhou03f0d912013-08-07 20:01:00 -0700941 struct sw_flow_match match;
Jesse Grossccb13522011-10-25 19:26:31 -0700942 int error;
Jesse Grossccb13522011-10-25 19:26:31 -0700943
944 /* Extract key. */
945 error = -EINVAL;
946 if (!a[OVS_FLOW_ATTR_KEY])
947 goto error;
Andy Zhou03f0d912013-08-07 20:01:00 -0700948
949 ovs_match_init(&match, &key, &mask);
Jarno Rajahalme23dabf82014-03-27 12:35:23 -0700950 error = ovs_nla_get_match(&match,
Pravin B Shelare6445712013-10-03 18:16:47 -0700951 a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
Jesse Grossccb13522011-10-25 19:26:31 -0700952 if (error)
953 goto error;
954
955 /* Validate actions. */
956 if (a[OVS_FLOW_ATTR_ACTIONS]) {
Pravin B Shelare6445712013-10-03 18:16:47 -0700957 acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_FLOW_ATTR_ACTIONS]));
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700958 error = PTR_ERR(acts);
959 if (IS_ERR(acts))
Jesse Grossccb13522011-10-25 19:26:31 -0700960 goto error;
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700961
Pravin B Shelare6445712013-10-03 18:16:47 -0700962 ovs_flow_mask_key(&masked_key, &key, &mask);
963 error = ovs_nla_copy_actions(a[OVS_FLOW_ATTR_ACTIONS],
964 &masked_key, 0, &acts);
Andy Zhou03f0d912013-08-07 20:01:00 -0700965 if (error) {
966 OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700967 goto err_kfree_acts;
968 }
969 }
970
971 /* Can allocate before locking if have acts. */
972 if (acts) {
973 reply = ovs_flow_cmd_alloc_info(acts, info, false);
974 if (IS_ERR(reply)) {
975 error = PTR_ERR(reply);
976 goto err_kfree_acts;
Andy Zhou03f0d912013-08-07 20:01:00 -0700977 }
Jesse Grossccb13522011-10-25 19:26:31 -0700978 }
979
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700980 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800981 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700982 if (unlikely(!dp)) {
983 error = -ENODEV;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700984 goto err_unlock_ovs;
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700985 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700986 /* Check that the flow exists. */
Alex Wang4a46b242014-06-30 20:30:29 -0700987 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700988 if (unlikely(!flow)) {
989 error = -ENOENT;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700990 goto err_unlock_ovs;
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700991 }
Alex Wang4a46b242014-06-30 20:30:29 -0700992
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700993 /* Update actions, if present. */
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700994 if (likely(acts)) {
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700995 old_acts = ovsl_dereference(flow->sf_acts);
Jesse Grossccb13522011-10-25 19:26:31 -0700996 rcu_assign_pointer(flow->sf_acts, acts);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700997
998 if (unlikely(reply)) {
999 error = ovs_flow_cmd_fill_info(flow,
1000 ovs_header->dp_ifindex,
1001 reply, info->snd_portid,
1002 info->snd_seq, 0,
1003 OVS_FLOW_CMD_NEW);
1004 BUG_ON(error < 0);
1005 }
1006 } else {
1007 /* Could not alloc without acts before locking. */
1008 reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex,
1009 info, OVS_FLOW_CMD_NEW, false);
1010 if (unlikely(IS_ERR(reply))) {
1011 error = PTR_ERR(reply);
1012 goto err_unlock_ovs;
1013 }
Jesse Grossccb13522011-10-25 19:26:31 -07001014 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001015
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001016 /* Clear stats. */
1017 if (a[OVS_FLOW_ATTR_CLEAR])
1018 ovs_flow_stats_clear(flow);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001019 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001020
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001021 if (reply)
1022 ovs_notify(&dp_flow_genl_family, reply, info);
1023 if (old_acts)
1024 ovs_nla_free_flow_actions(old_acts);
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -07001025
Jesse Grossccb13522011-10-25 19:26:31 -07001026 return 0;
1027
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001028err_unlock_ovs:
1029 ovs_unlock();
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001030 kfree_skb(reply);
1031err_kfree_acts:
Pravin B Shelar74f84a52013-06-17 17:50:12 -07001032 kfree(acts);
Jesse Grossccb13522011-10-25 19:26:31 -07001033error:
1034 return error;
1035}
1036
1037static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1038{
1039 struct nlattr **a = info->attrs;
1040 struct ovs_header *ovs_header = info->userhdr;
1041 struct sw_flow_key key;
1042 struct sk_buff *reply;
1043 struct sw_flow *flow;
1044 struct datapath *dp;
Andy Zhou03f0d912013-08-07 20:01:00 -07001045 struct sw_flow_match match;
Jesse Grossccb13522011-10-25 19:26:31 -07001046 int err;
Jesse Grossccb13522011-10-25 19:26:31 -07001047
Andy Zhou03f0d912013-08-07 20:01:00 -07001048 if (!a[OVS_FLOW_ATTR_KEY]) {
1049 OVS_NLERR("Flow get message rejected, Key attribute missing.\n");
Jesse Grossccb13522011-10-25 19:26:31 -07001050 return -EINVAL;
Andy Zhou03f0d912013-08-07 20:01:00 -07001051 }
1052
1053 ovs_match_init(&match, &key, NULL);
Jarno Rajahalme23dabf82014-03-27 12:35:23 -07001054 err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL);
Jesse Grossccb13522011-10-25 19:26:31 -07001055 if (err)
1056 return err;
1057
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001058 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001059 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001060 if (!dp) {
1061 err = -ENODEV;
1062 goto unlock;
1063 }
Jesse Grossccb13522011-10-25 19:26:31 -07001064
Alex Wang4a46b242014-06-30 20:30:29 -07001065 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1066 if (!flow) {
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001067 err = -ENOENT;
1068 goto unlock;
1069 }
Jesse Grossccb13522011-10-25 19:26:31 -07001070
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -07001071 reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex, info,
1072 OVS_FLOW_CMD_NEW, true);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001073 if (IS_ERR(reply)) {
1074 err = PTR_ERR(reply);
1075 goto unlock;
1076 }
Jesse Grossccb13522011-10-25 19:26:31 -07001077
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001078 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001079 return genlmsg_reply(reply, info);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001080unlock:
1081 ovs_unlock();
1082 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001083}
1084
1085static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1086{
1087 struct nlattr **a = info->attrs;
1088 struct ovs_header *ovs_header = info->userhdr;
1089 struct sw_flow_key key;
1090 struct sk_buff *reply;
1091 struct sw_flow *flow;
1092 struct datapath *dp;
Andy Zhou03f0d912013-08-07 20:01:00 -07001093 struct sw_flow_match match;
Jesse Grossccb13522011-10-25 19:26:31 -07001094 int err;
Jesse Grossccb13522011-10-25 19:26:31 -07001095
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001096 if (likely(a[OVS_FLOW_ATTR_KEY])) {
1097 ovs_match_init(&match, &key, NULL);
1098 err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL);
1099 if (unlikely(err))
1100 return err;
1101 }
1102
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001103 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001104 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001105 if (unlikely(!dp)) {
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001106 err = -ENODEV;
1107 goto unlock;
1108 }
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001109
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001110 if (unlikely(!a[OVS_FLOW_ATTR_KEY])) {
Pravin B Shelarb637e492013-10-04 00:14:23 -07001111 err = ovs_flow_tbl_flush(&dp->table);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001112 goto unlock;
1113 }
Andy Zhou03f0d912013-08-07 20:01:00 -07001114
Alex Wang4a46b242014-06-30 20:30:29 -07001115 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
1116 if (unlikely(!flow)) {
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001117 err = -ENOENT;
1118 goto unlock;
1119 }
Jesse Grossccb13522011-10-25 19:26:31 -07001120
Pravin B Shelarb637e492013-10-04 00:14:23 -07001121 ovs_flow_tbl_remove(&dp->table, flow);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001122 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001123
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001124 reply = ovs_flow_cmd_alloc_info((const struct sw_flow_actions __force *) flow->sf_acts,
1125 info, false);
1126 if (likely(reply)) {
1127 if (likely(!IS_ERR(reply))) {
1128 rcu_read_lock(); /*To keep RCU checker happy. */
1129 err = ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex,
1130 reply, info->snd_portid,
1131 info->snd_seq, 0,
1132 OVS_FLOW_CMD_DEL);
1133 rcu_read_unlock();
1134 BUG_ON(err < 0);
1135
1136 ovs_notify(&dp_flow_genl_family, reply, info);
1137 } else {
1138 netlink_set_err(sock_net(skb->sk)->genl_sock, 0, 0, PTR_ERR(reply));
1139 }
1140 }
1141
1142 ovs_flow_free(flow, true);
Jesse Grossccb13522011-10-25 19:26:31 -07001143 return 0;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001144unlock:
1145 ovs_unlock();
1146 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001147}
1148
1149static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1150{
1151 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
Pravin B Shelarb637e492013-10-04 00:14:23 -07001152 struct table_instance *ti;
Jesse Grossccb13522011-10-25 19:26:31 -07001153 struct datapath *dp;
Jesse Grossccb13522011-10-25 19:26:31 -07001154
Pravin B Shelard57170b2013-07-30 15:39:39 -07001155 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001156 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001157 if (!dp) {
Pravin B Shelard57170b2013-07-30 15:39:39 -07001158 rcu_read_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001159 return -ENODEV;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001160 }
Jesse Grossccb13522011-10-25 19:26:31 -07001161
Pravin B Shelarb637e492013-10-04 00:14:23 -07001162 ti = rcu_dereference(dp->table.ti);
Jesse Grossccb13522011-10-25 19:26:31 -07001163 for (;;) {
1164 struct sw_flow *flow;
1165 u32 bucket, obj;
1166
1167 bucket = cb->args[0];
1168 obj = cb->args[1];
Pravin B Shelarb637e492013-10-04 00:14:23 -07001169 flow = ovs_flow_tbl_dump_next(ti, &bucket, &obj);
Jesse Grossccb13522011-10-25 19:26:31 -07001170 if (!flow)
1171 break;
1172
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -07001173 if (ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex, skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001174 NETLINK_CB(cb->skb).portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001175 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1176 OVS_FLOW_CMD_NEW) < 0)
1177 break;
1178
1179 cb->args[0] = bucket;
1180 cb->args[1] = obj;
1181 }
Pravin B Shelard57170b2013-07-30 15:39:39 -07001182 rcu_read_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001183 return skb->len;
1184}
1185
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001186static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
1187 [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
1188 [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
1189 [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
1190};
1191
stephen hemminger48e48a72014-07-16 11:25:52 -07001192static const struct genl_ops dp_flow_genl_ops[] = {
Jesse Grossccb13522011-10-25 19:26:31 -07001193 { .cmd = OVS_FLOW_CMD_NEW,
1194 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1195 .policy = flow_policy,
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001196 .doit = ovs_flow_cmd_new
Jesse Grossccb13522011-10-25 19:26:31 -07001197 },
1198 { .cmd = OVS_FLOW_CMD_DEL,
1199 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1200 .policy = flow_policy,
1201 .doit = ovs_flow_cmd_del
1202 },
1203 { .cmd = OVS_FLOW_CMD_GET,
1204 .flags = 0, /* OK for unprivileged users. */
1205 .policy = flow_policy,
1206 .doit = ovs_flow_cmd_get,
1207 .dumpit = ovs_flow_cmd_dump
1208 },
1209 { .cmd = OVS_FLOW_CMD_SET,
1210 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1211 .policy = flow_policy,
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001212 .doit = ovs_flow_cmd_set,
Jesse Grossccb13522011-10-25 19:26:31 -07001213 },
1214};
1215
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001216static struct genl_family dp_flow_genl_family = {
Jesse Grossccb13522011-10-25 19:26:31 -07001217 .id = GENL_ID_GENERATE,
1218 .hdrsize = sizeof(struct ovs_header),
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001219 .name = OVS_FLOW_FAMILY,
1220 .version = OVS_FLOW_VERSION,
1221 .maxattr = OVS_FLOW_ATTR_MAX,
Pravin B Shelar3a4e0d62013-04-23 07:48:48 +00001222 .netnsok = true,
1223 .parallel_ops = true,
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001224 .ops = dp_flow_genl_ops,
1225 .n_ops = ARRAY_SIZE(dp_flow_genl_ops),
1226 .mcgrps = &ovs_dp_flow_multicast_group,
1227 .n_mcgrps = 1,
Jesse Grossccb13522011-10-25 19:26:31 -07001228};
1229
Thomas Grafc3ff8cf2013-03-29 14:46:49 +01001230static size_t ovs_dp_cmd_msg_size(void)
1231{
1232 size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
1233
1234 msgsize += nla_total_size(IFNAMSIZ);
1235 msgsize += nla_total_size(sizeof(struct ovs_dp_stats));
Andy Zhou1bd71162013-10-22 10:42:46 -07001236 msgsize += nla_total_size(sizeof(struct ovs_dp_megaflow_stats));
Daniele Di Proietto45fb9c32014-01-23 10:47:35 -08001237 msgsize += nla_total_size(sizeof(u32)); /* OVS_DP_ATTR_USER_FEATURES */
Thomas Grafc3ff8cf2013-03-29 14:46:49 +01001238
1239 return msgsize;
1240}
1241
Jarno Rajahalmebb6f9a72014-05-05 11:32:17 -07001242/* Called with ovs_mutex or RCU read lock. */
Jesse Grossccb13522011-10-25 19:26:31 -07001243static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001244 u32 portid, u32 seq, u32 flags, u8 cmd)
Jesse Grossccb13522011-10-25 19:26:31 -07001245{
1246 struct ovs_header *ovs_header;
1247 struct ovs_dp_stats dp_stats;
Andy Zhou1bd71162013-10-22 10:42:46 -07001248 struct ovs_dp_megaflow_stats dp_megaflow_stats;
Jesse Grossccb13522011-10-25 19:26:31 -07001249 int err;
1250
Eric W. Biederman15e47302012-09-07 20:12:54 +00001251 ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
Jesse Grossccb13522011-10-25 19:26:31 -07001252 flags, cmd);
1253 if (!ovs_header)
1254 goto error;
1255
1256 ovs_header->dp_ifindex = get_dpifindex(dp);
1257
Jesse Grossccb13522011-10-25 19:26:31 -07001258 err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
Jesse Grossccb13522011-10-25 19:26:31 -07001259 if (err)
1260 goto nla_put_failure;
1261
Andy Zhou1bd71162013-10-22 10:42:46 -07001262 get_dp_stats(dp, &dp_stats, &dp_megaflow_stats);
1263 if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats),
1264 &dp_stats))
1265 goto nla_put_failure;
1266
1267 if (nla_put(skb, OVS_DP_ATTR_MEGAFLOW_STATS,
1268 sizeof(struct ovs_dp_megaflow_stats),
1269 &dp_megaflow_stats))
David S. Miller028d6a62012-03-29 23:20:48 -04001270 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001271
Thomas Graf43d4be92013-12-13 15:22:18 +01001272 if (nla_put_u32(skb, OVS_DP_ATTR_USER_FEATURES, dp->user_features))
1273 goto nla_put_failure;
1274
Jesse Grossccb13522011-10-25 19:26:31 -07001275 return genlmsg_end(skb, ovs_header);
1276
1277nla_put_failure:
1278 genlmsg_cancel(skb, ovs_header);
1279error:
1280 return -EMSGSIZE;
1281}
1282
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001283static struct sk_buff *ovs_dp_cmd_alloc_info(struct genl_info *info)
Jesse Grossccb13522011-10-25 19:26:31 -07001284{
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001285 return genlmsg_new_unicast(ovs_dp_cmd_msg_size(), info, GFP_KERNEL);
Jesse Grossccb13522011-10-25 19:26:31 -07001286}
1287
Jarno Rajahalmebb6f9a72014-05-05 11:32:17 -07001288/* Called with rcu_read_lock or ovs_mutex. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001289static struct datapath *lookup_datapath(struct net *net,
1290 struct ovs_header *ovs_header,
Jesse Grossccb13522011-10-25 19:26:31 -07001291 struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1292{
1293 struct datapath *dp;
1294
1295 if (!a[OVS_DP_ATTR_NAME])
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001296 dp = get_dp(net, ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001297 else {
1298 struct vport *vport;
1299
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001300 vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
Jesse Grossccb13522011-10-25 19:26:31 -07001301 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
Jesse Grossccb13522011-10-25 19:26:31 -07001302 }
1303 return dp ? dp : ERR_PTR(-ENODEV);
1304}
1305
Thomas Graf44da5ae2013-12-13 15:22:19 +01001306static void ovs_dp_reset_user_features(struct sk_buff *skb, struct genl_info *info)
1307{
1308 struct datapath *dp;
1309
1310 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
Jiri Pirko3c7eacf2014-02-14 11:42:36 +01001311 if (IS_ERR(dp))
Thomas Graf44da5ae2013-12-13 15:22:19 +01001312 return;
1313
1314 WARN(dp->user_features, "Dropping previously announced user features\n");
1315 dp->user_features = 0;
1316}
1317
Thomas Graf43d4be92013-12-13 15:22:18 +01001318static void ovs_dp_change(struct datapath *dp, struct nlattr **a)
1319{
1320 if (a[OVS_DP_ATTR_USER_FEATURES])
1321 dp->user_features = nla_get_u32(a[OVS_DP_ATTR_USER_FEATURES]);
1322}
1323
Jesse Grossccb13522011-10-25 19:26:31 -07001324static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1325{
1326 struct nlattr **a = info->attrs;
1327 struct vport_parms parms;
1328 struct sk_buff *reply;
1329 struct datapath *dp;
1330 struct vport *vport;
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001331 struct ovs_net *ovs_net;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001332 int err, i;
Jesse Grossccb13522011-10-25 19:26:31 -07001333
1334 err = -EINVAL;
1335 if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1336 goto err;
1337
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001338 reply = ovs_dp_cmd_alloc_info(info);
1339 if (!reply)
1340 return -ENOMEM;
Jesse Grossccb13522011-10-25 19:26:31 -07001341
1342 err = -ENOMEM;
1343 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1344 if (dp == NULL)
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001345 goto err_free_reply;
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001346
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001347 ovs_dp_set_net(dp, hold_net(sock_net(skb->sk)));
Jesse Grossccb13522011-10-25 19:26:31 -07001348
1349 /* Allocate table. */
Pravin B Shelarb637e492013-10-04 00:14:23 -07001350 err = ovs_flow_tbl_init(&dp->table);
1351 if (err)
Jesse Grossccb13522011-10-25 19:26:31 -07001352 goto err_free_dp;
1353
WANG Cong1c213bd2014-02-13 11:46:28 -08001354 dp->stats_percpu = netdev_alloc_pcpu_stats(struct dp_stats_percpu);
Jesse Grossccb13522011-10-25 19:26:31 -07001355 if (!dp->stats_percpu) {
1356 err = -ENOMEM;
1357 goto err_destroy_table;
1358 }
1359
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001360 dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
Pravin B Shelare6445712013-10-03 18:16:47 -07001361 GFP_KERNEL);
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001362 if (!dp->ports) {
1363 err = -ENOMEM;
1364 goto err_destroy_percpu;
1365 }
1366
1367 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1368 INIT_HLIST_HEAD(&dp->ports[i]);
1369
Jesse Grossccb13522011-10-25 19:26:31 -07001370 /* Set up our datapath device. */
1371 parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1372 parms.type = OVS_VPORT_TYPE_INTERNAL;
1373 parms.options = NULL;
1374 parms.dp = dp;
1375 parms.port_no = OVSP_LOCAL;
Alex Wang5cd667b2014-07-17 15:14:13 -07001376 parms.upcall_portids = a[OVS_DP_ATTR_UPCALL_PID];
Jesse Grossccb13522011-10-25 19:26:31 -07001377
Thomas Graf43d4be92013-12-13 15:22:18 +01001378 ovs_dp_change(dp, a);
1379
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001380 /* So far only local changes have been made, now need the lock. */
1381 ovs_lock();
1382
Jesse Grossccb13522011-10-25 19:26:31 -07001383 vport = new_vport(&parms);
1384 if (IS_ERR(vport)) {
1385 err = PTR_ERR(vport);
1386 if (err == -EBUSY)
1387 err = -EEXIST;
1388
Thomas Graf44da5ae2013-12-13 15:22:19 +01001389 if (err == -EEXIST) {
1390 /* An outdated user space instance that does not understand
1391 * the concept of user_features has attempted to create a new
1392 * datapath and is likely to reuse it. Drop all user features.
1393 */
1394 if (info->genlhdr->version < OVS_DP_VER_FEATURES)
1395 ovs_dp_reset_user_features(skb, info);
1396 }
1397
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001398 goto err_destroy_ports_array;
Jesse Grossccb13522011-10-25 19:26:31 -07001399 }
1400
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001401 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1402 info->snd_seq, 0, OVS_DP_CMD_NEW);
1403 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001404
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001405 ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001406 list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001407
1408 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001409
Johannes Berg2a94fe42013-11-19 15:19:39 +01001410 ovs_notify(&dp_datapath_genl_family, reply, info);
Jesse Grossccb13522011-10-25 19:26:31 -07001411 return 0;
1412
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001413err_destroy_ports_array:
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001414 ovs_unlock();
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001415 kfree(dp->ports);
Jesse Grossccb13522011-10-25 19:26:31 -07001416err_destroy_percpu:
1417 free_percpu(dp->stats_percpu);
1418err_destroy_table:
Andy Zhoue80857c2014-01-21 09:31:04 -08001419 ovs_flow_tbl_destroy(&dp->table, false);
Jesse Grossccb13522011-10-25 19:26:31 -07001420err_free_dp:
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001421 release_net(ovs_dp_get_net(dp));
Jesse Grossccb13522011-10-25 19:26:31 -07001422 kfree(dp);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001423err_free_reply:
1424 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001425err:
1426 return err;
1427}
1428
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001429/* Called with ovs_mutex. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001430static void __dp_destroy(struct datapath *dp)
Jesse Grossccb13522011-10-25 19:26:31 -07001431{
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001432 int i;
Jesse Grossccb13522011-10-25 19:26:31 -07001433
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001434 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1435 struct vport *vport;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001436 struct hlist_node *n;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001437
Sasha Levinb67bfe02013-02-27 17:06:00 -08001438 hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001439 if (vport->port_no != OVSP_LOCAL)
1440 ovs_dp_detach_port(vport);
1441 }
Jesse Grossccb13522011-10-25 19:26:31 -07001442
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001443 list_del_rcu(&dp->list_node);
Jesse Grossccb13522011-10-25 19:26:31 -07001444
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001445 /* OVSP_LOCAL is datapath internal port. We need to make sure that
Andy Zhoue80857c2014-01-21 09:31:04 -08001446 * all ports in datapath are destroyed first before freeing datapath.
Jesse Grossccb13522011-10-25 19:26:31 -07001447 */
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001448 ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
Jesse Grossccb13522011-10-25 19:26:31 -07001449
Andy Zhoue80857c2014-01-21 09:31:04 -08001450 /* RCU destroy the flow table */
1451 ovs_flow_tbl_destroy(&dp->table, true);
1452
Jesse Grossccb13522011-10-25 19:26:31 -07001453 call_rcu(&dp->rcu, destroy_dp_rcu);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001454}
1455
1456static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1457{
1458 struct sk_buff *reply;
1459 struct datapath *dp;
1460 int err;
1461
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001462 reply = ovs_dp_cmd_alloc_info(info);
1463 if (!reply)
1464 return -ENOMEM;
1465
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001466 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001467 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1468 err = PTR_ERR(dp);
1469 if (IS_ERR(dp))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001470 goto err_unlock_free;
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001471
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001472 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1473 info->snd_seq, 0, OVS_DP_CMD_DEL);
1474 BUG_ON(err < 0);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001475
1476 __dp_destroy(dp);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001477 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001478
Johannes Berg2a94fe42013-11-19 15:19:39 +01001479 ovs_notify(&dp_datapath_genl_family, reply, info);
Jesse Grossccb13522011-10-25 19:26:31 -07001480
1481 return 0;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001482
1483err_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001484 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001485 kfree_skb(reply);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001486 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001487}
1488
1489static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1490{
1491 struct sk_buff *reply;
1492 struct datapath *dp;
1493 int err;
1494
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001495 reply = ovs_dp_cmd_alloc_info(info);
1496 if (!reply)
1497 return -ENOMEM;
1498
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001499 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001500 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001501 err = PTR_ERR(dp);
Jesse Grossccb13522011-10-25 19:26:31 -07001502 if (IS_ERR(dp))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001503 goto err_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001504
Thomas Graf43d4be92013-12-13 15:22:18 +01001505 ovs_dp_change(dp, info->attrs);
1506
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001507 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1508 info->snd_seq, 0, OVS_DP_CMD_NEW);
1509 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001510
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001511 ovs_unlock();
Johannes Berg2a94fe42013-11-19 15:19:39 +01001512 ovs_notify(&dp_datapath_genl_family, reply, info);
Jesse Grossccb13522011-10-25 19:26:31 -07001513
1514 return 0;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001515
1516err_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001517 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001518 kfree_skb(reply);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001519 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001520}
1521
1522static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1523{
1524 struct sk_buff *reply;
1525 struct datapath *dp;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001526 int err;
Jesse Grossccb13522011-10-25 19:26:31 -07001527
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001528 reply = ovs_dp_cmd_alloc_info(info);
1529 if (!reply)
1530 return -ENOMEM;
1531
1532 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001533 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001534 if (IS_ERR(dp)) {
1535 err = PTR_ERR(dp);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001536 goto err_unlock_free;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001537 }
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001538 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1539 info->snd_seq, 0, OVS_DP_CMD_NEW);
1540 BUG_ON(err < 0);
1541 rcu_read_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001542
Jesse Grossccb13522011-10-25 19:26:31 -07001543 return genlmsg_reply(reply, info);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001544
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001545err_unlock_free:
1546 rcu_read_unlock();
1547 kfree_skb(reply);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001548 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001549}
1550
1551static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1552{
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001553 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
Jesse Grossccb13522011-10-25 19:26:31 -07001554 struct datapath *dp;
1555 int skip = cb->args[0];
1556 int i = 0;
1557
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001558 rcu_read_lock();
1559 list_for_each_entry_rcu(dp, &ovs_net->dps, list_node) {
Ben Pfaff77676fd2012-01-17 13:33:39 +00001560 if (i >= skip &&
Eric W. Biederman15e47302012-09-07 20:12:54 +00001561 ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001562 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1563 OVS_DP_CMD_NEW) < 0)
1564 break;
1565 i++;
1566 }
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001567 rcu_read_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001568
1569 cb->args[0] = i;
1570
1571 return skb->len;
1572}
1573
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001574static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1575 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1576 [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1577 [OVS_DP_ATTR_USER_FEATURES] = { .type = NLA_U32 },
1578};
1579
stephen hemminger48e48a72014-07-16 11:25:52 -07001580static const struct genl_ops dp_datapath_genl_ops[] = {
Jesse Grossccb13522011-10-25 19:26:31 -07001581 { .cmd = OVS_DP_CMD_NEW,
1582 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1583 .policy = datapath_policy,
1584 .doit = ovs_dp_cmd_new
1585 },
1586 { .cmd = OVS_DP_CMD_DEL,
1587 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1588 .policy = datapath_policy,
1589 .doit = ovs_dp_cmd_del
1590 },
1591 { .cmd = OVS_DP_CMD_GET,
1592 .flags = 0, /* OK for unprivileged users. */
1593 .policy = datapath_policy,
1594 .doit = ovs_dp_cmd_get,
1595 .dumpit = ovs_dp_cmd_dump
1596 },
1597 { .cmd = OVS_DP_CMD_SET,
1598 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1599 .policy = datapath_policy,
1600 .doit = ovs_dp_cmd_set,
1601 },
1602};
1603
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001604static struct genl_family dp_datapath_genl_family = {
Jesse Grossccb13522011-10-25 19:26:31 -07001605 .id = GENL_ID_GENERATE,
1606 .hdrsize = sizeof(struct ovs_header),
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001607 .name = OVS_DATAPATH_FAMILY,
1608 .version = OVS_DATAPATH_VERSION,
1609 .maxattr = OVS_DP_ATTR_MAX,
Pravin B Shelar3a4e0d62013-04-23 07:48:48 +00001610 .netnsok = true,
1611 .parallel_ops = true,
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001612 .ops = dp_datapath_genl_ops,
1613 .n_ops = ARRAY_SIZE(dp_datapath_genl_ops),
1614 .mcgrps = &ovs_dp_datapath_multicast_group,
1615 .n_mcgrps = 1,
Jesse Grossccb13522011-10-25 19:26:31 -07001616};
1617
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001618/* Called with ovs_mutex or RCU read lock. */
Jesse Grossccb13522011-10-25 19:26:31 -07001619static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001620 u32 portid, u32 seq, u32 flags, u8 cmd)
Jesse Grossccb13522011-10-25 19:26:31 -07001621{
1622 struct ovs_header *ovs_header;
1623 struct ovs_vport_stats vport_stats;
1624 int err;
1625
Eric W. Biederman15e47302012-09-07 20:12:54 +00001626 ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
Jesse Grossccb13522011-10-25 19:26:31 -07001627 flags, cmd);
1628 if (!ovs_header)
1629 return -EMSGSIZE;
1630
1631 ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1632
David S. Miller028d6a62012-03-29 23:20:48 -04001633 if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
1634 nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
Alex Wang5cd667b2014-07-17 15:14:13 -07001635 nla_put_string(skb, OVS_VPORT_ATTR_NAME,
1636 vport->ops->get_name(vport)))
David S. Miller028d6a62012-03-29 23:20:48 -04001637 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001638
1639 ovs_vport_get_stats(vport, &vport_stats);
David S. Miller028d6a62012-03-29 23:20:48 -04001640 if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
1641 &vport_stats))
1642 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001643
Alex Wang5cd667b2014-07-17 15:14:13 -07001644 if (ovs_vport_get_upcall_portids(vport, skb))
1645 goto nla_put_failure;
1646
Jesse Grossccb13522011-10-25 19:26:31 -07001647 err = ovs_vport_get_options(vport, skb);
1648 if (err == -EMSGSIZE)
1649 goto error;
1650
1651 return genlmsg_end(skb, ovs_header);
1652
1653nla_put_failure:
1654 err = -EMSGSIZE;
1655error:
1656 genlmsg_cancel(skb, ovs_header);
1657 return err;
1658}
1659
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001660static struct sk_buff *ovs_vport_cmd_alloc_info(void)
1661{
1662 return nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1663}
1664
1665/* Called with ovs_mutex, only via ovs_dp_notify_wq(). */
Eric W. Biederman15e47302012-09-07 20:12:54 +00001666struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001667 u32 seq, u8 cmd)
1668{
1669 struct sk_buff *skb;
1670 int retval;
1671
1672 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1673 if (!skb)
1674 return ERR_PTR(-ENOMEM);
1675
Eric W. Biederman15e47302012-09-07 20:12:54 +00001676 retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
Jesse Grossa9341512013-03-26 15:48:38 -07001677 BUG_ON(retval < 0);
1678
Jesse Grossccb13522011-10-25 19:26:31 -07001679 return skb;
1680}
1681
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001682/* Called with ovs_mutex or RCU read lock. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001683static struct vport *lookup_vport(struct net *net,
1684 struct ovs_header *ovs_header,
Jesse Grossccb13522011-10-25 19:26:31 -07001685 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1686{
1687 struct datapath *dp;
1688 struct vport *vport;
1689
1690 if (a[OVS_VPORT_ATTR_NAME]) {
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001691 vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
Jesse Grossccb13522011-10-25 19:26:31 -07001692 if (!vport)
1693 return ERR_PTR(-ENODEV);
Ben Pfaff651a68e2012-03-06 15:04:04 -08001694 if (ovs_header->dp_ifindex &&
1695 ovs_header->dp_ifindex != get_dpifindex(vport->dp))
1696 return ERR_PTR(-ENODEV);
Jesse Grossccb13522011-10-25 19:26:31 -07001697 return vport;
1698 } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
1699 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1700
1701 if (port_no >= DP_MAX_PORTS)
1702 return ERR_PTR(-EFBIG);
1703
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001704 dp = get_dp(net, ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001705 if (!dp)
1706 return ERR_PTR(-ENODEV);
1707
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001708 vport = ovs_vport_ovsl_rcu(dp, port_no);
Jesse Grossccb13522011-10-25 19:26:31 -07001709 if (!vport)
Jarno Rajahalme14408db2013-01-09 14:27:35 -08001710 return ERR_PTR(-ENODEV);
Jesse Grossccb13522011-10-25 19:26:31 -07001711 return vport;
1712 } else
1713 return ERR_PTR(-EINVAL);
1714}
1715
1716static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
1717{
1718 struct nlattr **a = info->attrs;
1719 struct ovs_header *ovs_header = info->userhdr;
1720 struct vport_parms parms;
1721 struct sk_buff *reply;
1722 struct vport *vport;
1723 struct datapath *dp;
1724 u32 port_no;
1725 int err;
1726
Jesse Grossccb13522011-10-25 19:26:31 -07001727 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
1728 !a[OVS_VPORT_ATTR_UPCALL_PID])
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001729 return -EINVAL;
1730
1731 port_no = a[OVS_VPORT_ATTR_PORT_NO]
1732 ? nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]) : 0;
1733 if (port_no >= DP_MAX_PORTS)
1734 return -EFBIG;
1735
1736 reply = ovs_vport_cmd_alloc_info();
1737 if (!reply)
1738 return -ENOMEM;
Jesse Grossccb13522011-10-25 19:26:31 -07001739
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001740 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001741 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001742 err = -ENODEV;
1743 if (!dp)
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001744 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001745
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001746 if (port_no) {
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001747 vport = ovs_vport_ovsl(dp, port_no);
Jesse Grossccb13522011-10-25 19:26:31 -07001748 err = -EBUSY;
1749 if (vport)
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001750 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001751 } else {
1752 for (port_no = 1; ; port_no++) {
1753 if (port_no >= DP_MAX_PORTS) {
1754 err = -EFBIG;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001755 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001756 }
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001757 vport = ovs_vport_ovsl(dp, port_no);
Jesse Grossccb13522011-10-25 19:26:31 -07001758 if (!vport)
1759 break;
1760 }
1761 }
1762
1763 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
1764 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1765 parms.options = a[OVS_VPORT_ATTR_OPTIONS];
1766 parms.dp = dp;
1767 parms.port_no = port_no;
Alex Wang5cd667b2014-07-17 15:14:13 -07001768 parms.upcall_portids = a[OVS_VPORT_ATTR_UPCALL_PID];
Jesse Grossccb13522011-10-25 19:26:31 -07001769
1770 vport = new_vport(&parms);
1771 err = PTR_ERR(vport);
1772 if (IS_ERR(vport))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001773 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001774
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001775 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1776 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1777 BUG_ON(err < 0);
1778 ovs_unlock();
Thomas Grafed661182013-03-29 14:46:50 +01001779
Johannes Berg2a94fe42013-11-19 15:19:39 +01001780 ovs_notify(&dp_vport_genl_family, reply, info);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001781 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001782
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001783exit_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001784 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001785 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001786 return err;
1787}
1788
1789static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
1790{
1791 struct nlattr **a = info->attrs;
1792 struct sk_buff *reply;
1793 struct vport *vport;
1794 int err;
1795
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001796 reply = ovs_vport_cmd_alloc_info();
1797 if (!reply)
1798 return -ENOMEM;
1799
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001800 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001801 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
Jesse Grossccb13522011-10-25 19:26:31 -07001802 err = PTR_ERR(vport);
1803 if (IS_ERR(vport))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001804 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001805
Jesse Grossccb13522011-10-25 19:26:31 -07001806 if (a[OVS_VPORT_ATTR_TYPE] &&
Jesse Grossf44f3402013-05-13 08:15:26 -07001807 nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
Jesse Grossccb13522011-10-25 19:26:31 -07001808 err = -EINVAL;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001809 goto exit_unlock_free;
Jesse Grossa9341512013-03-26 15:48:38 -07001810 }
1811
Jesse Grossf44f3402013-05-13 08:15:26 -07001812 if (a[OVS_VPORT_ATTR_OPTIONS]) {
Jesse Grossccb13522011-10-25 19:26:31 -07001813 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
Jesse Grossf44f3402013-05-13 08:15:26 -07001814 if (err)
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001815 goto exit_unlock_free;
Jesse Grossf44f3402013-05-13 08:15:26 -07001816 }
Jesse Grossa9341512013-03-26 15:48:38 -07001817
Alex Wang5cd667b2014-07-17 15:14:13 -07001818
1819 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
1820 struct nlattr *ids = a[OVS_VPORT_ATTR_UPCALL_PID];
1821
1822 err = ovs_vport_set_upcall_portids(vport, ids);
1823 if (err)
1824 goto exit_unlock_free;
1825 }
Jesse Grossccb13522011-10-25 19:26:31 -07001826
Jesse Grossa9341512013-03-26 15:48:38 -07001827 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1828 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1829 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001830
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001831 ovs_unlock();
Johannes Berg2a94fe42013-11-19 15:19:39 +01001832 ovs_notify(&dp_vport_genl_family, reply, info);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001833 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001834
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001835exit_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001836 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001837 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001838 return err;
1839}
1840
1841static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
1842{
1843 struct nlattr **a = info->attrs;
1844 struct sk_buff *reply;
1845 struct vport *vport;
1846 int err;
1847
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001848 reply = ovs_vport_cmd_alloc_info();
1849 if (!reply)
1850 return -ENOMEM;
1851
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001852 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001853 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
Jesse Grossccb13522011-10-25 19:26:31 -07001854 err = PTR_ERR(vport);
1855 if (IS_ERR(vport))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001856 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001857
1858 if (vport->port_no == OVSP_LOCAL) {
1859 err = -EINVAL;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001860 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001861 }
1862
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001863 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1864 info->snd_seq, 0, OVS_VPORT_CMD_DEL);
1865 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001866 ovs_dp_detach_port(vport);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001867 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001868
Johannes Berg2a94fe42013-11-19 15:19:39 +01001869 ovs_notify(&dp_vport_genl_family, reply, info);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001870 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001871
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001872exit_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001873 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001874 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001875 return err;
1876}
1877
1878static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
1879{
1880 struct nlattr **a = info->attrs;
1881 struct ovs_header *ovs_header = info->userhdr;
1882 struct sk_buff *reply;
1883 struct vport *vport;
1884 int err;
1885
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001886 reply = ovs_vport_cmd_alloc_info();
1887 if (!reply)
1888 return -ENOMEM;
1889
Jesse Grossccb13522011-10-25 19:26:31 -07001890 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001891 vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
Jesse Grossccb13522011-10-25 19:26:31 -07001892 err = PTR_ERR(vport);
1893 if (IS_ERR(vport))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001894 goto exit_unlock_free;
1895 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1896 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1897 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001898 rcu_read_unlock();
1899
1900 return genlmsg_reply(reply, info);
1901
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001902exit_unlock_free:
Jesse Grossccb13522011-10-25 19:26:31 -07001903 rcu_read_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001904 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001905 return err;
1906}
1907
1908static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1909{
1910 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1911 struct datapath *dp;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001912 int bucket = cb->args[0], skip = cb->args[1];
1913 int i, j = 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001914
Jesse Grossccb13522011-10-25 19:26:31 -07001915 rcu_read_lock();
Jarno Rajahalme42ee19e2014-02-15 17:42:29 -08001916 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1917 if (!dp) {
1918 rcu_read_unlock();
1919 return -ENODEV;
1920 }
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001921 for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
Jesse Grossccb13522011-10-25 19:26:31 -07001922 struct vport *vport;
1923
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001924 j = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001925 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001926 if (j >= skip &&
1927 ovs_vport_cmd_fill_info(vport, skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001928 NETLINK_CB(cb->skb).portid,
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001929 cb->nlh->nlmsg_seq,
1930 NLM_F_MULTI,
1931 OVS_VPORT_CMD_NEW) < 0)
1932 goto out;
Jesse Grossccb13522011-10-25 19:26:31 -07001933
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001934 j++;
1935 }
1936 skip = 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001937 }
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001938out:
Jesse Grossccb13522011-10-25 19:26:31 -07001939 rcu_read_unlock();
1940
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001941 cb->args[0] = i;
1942 cb->args[1] = j;
Jesse Grossccb13522011-10-25 19:26:31 -07001943
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001944 return skb->len;
Jesse Grossccb13522011-10-25 19:26:31 -07001945}
1946
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001947static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
1948 [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1949 [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
1950 [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
1951 [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
1952 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1953 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
1954};
1955
stephen hemminger48e48a72014-07-16 11:25:52 -07001956static const struct genl_ops dp_vport_genl_ops[] = {
Jesse Grossccb13522011-10-25 19:26:31 -07001957 { .cmd = OVS_VPORT_CMD_NEW,
1958 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1959 .policy = vport_policy,
1960 .doit = ovs_vport_cmd_new
1961 },
1962 { .cmd = OVS_VPORT_CMD_DEL,
1963 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1964 .policy = vport_policy,
1965 .doit = ovs_vport_cmd_del
1966 },
1967 { .cmd = OVS_VPORT_CMD_GET,
1968 .flags = 0, /* OK for unprivileged users. */
1969 .policy = vport_policy,
1970 .doit = ovs_vport_cmd_get,
1971 .dumpit = ovs_vport_cmd_dump
1972 },
1973 { .cmd = OVS_VPORT_CMD_SET,
1974 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1975 .policy = vport_policy,
1976 .doit = ovs_vport_cmd_set,
1977 },
1978};
1979
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001980struct genl_family dp_vport_genl_family = {
1981 .id = GENL_ID_GENERATE,
1982 .hdrsize = sizeof(struct ovs_header),
1983 .name = OVS_VPORT_FAMILY,
1984 .version = OVS_VPORT_VERSION,
1985 .maxattr = OVS_VPORT_ATTR_MAX,
1986 .netnsok = true,
1987 .parallel_ops = true,
1988 .ops = dp_vport_genl_ops,
1989 .n_ops = ARRAY_SIZE(dp_vport_genl_ops),
1990 .mcgrps = &ovs_dp_vport_multicast_group,
1991 .n_mcgrps = 1,
Jesse Grossccb13522011-10-25 19:26:31 -07001992};
1993
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001994static struct genl_family * const dp_genl_families[] = {
1995 &dp_datapath_genl_family,
1996 &dp_vport_genl_family,
1997 &dp_flow_genl_family,
1998 &dp_packet_genl_family,
Jesse Grossccb13522011-10-25 19:26:31 -07001999};
2000
2001static void dp_unregister_genl(int n_families)
2002{
2003 int i;
2004
2005 for (i = 0; i < n_families; i++)
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002006 genl_unregister_family(dp_genl_families[i]);
Jesse Grossccb13522011-10-25 19:26:31 -07002007}
2008
2009static int dp_register_genl(void)
2010{
Jesse Grossccb13522011-10-25 19:26:31 -07002011 int err;
2012 int i;
2013
Jesse Grossccb13522011-10-25 19:26:31 -07002014 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
Jesse Grossccb13522011-10-25 19:26:31 -07002015
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002016 err = genl_register_family(dp_genl_families[i]);
Jesse Grossccb13522011-10-25 19:26:31 -07002017 if (err)
2018 goto error;
Jesse Grossccb13522011-10-25 19:26:31 -07002019 }
2020
2021 return 0;
2022
2023error:
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002024 dp_unregister_genl(i);
Jesse Grossccb13522011-10-25 19:26:31 -07002025 return err;
2026}
2027
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002028static int __net_init ovs_init_net(struct net *net)
2029{
2030 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2031
2032 INIT_LIST_HEAD(&ovs_net->dps);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002033 INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002034 return 0;
2035}
2036
2037static void __net_exit ovs_exit_net(struct net *net)
2038{
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002039 struct datapath *dp, *dp_next;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002040 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002041
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002042 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002043 list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
2044 __dp_destroy(dp);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002045 ovs_unlock();
2046
2047 cancel_work_sync(&ovs_net->dp_notify_work);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002048}
2049
2050static struct pernet_operations ovs_net_ops = {
2051 .init = ovs_init_net,
2052 .exit = ovs_exit_net,
2053 .id = &ovs_net_id,
2054 .size = sizeof(struct ovs_net),
2055};
2056
Jesse Grossccb13522011-10-25 19:26:31 -07002057static int __init dp_init(void)
2058{
Jesse Grossccb13522011-10-25 19:26:31 -07002059 int err;
2060
YOSHIFUJI Hideaki / 吉藤英明3523b292013-01-09 07:19:55 +00002061 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
Jesse Grossccb13522011-10-25 19:26:31 -07002062
2063 pr_info("Open vSwitch switching datapath\n");
2064
Jiri Pirko5b9e7e12014-06-26 09:58:26 +02002065 err = ovs_internal_dev_rtnl_link_register();
Jesse Grossccb13522011-10-25 19:26:31 -07002066 if (err)
2067 goto error;
2068
Jiri Pirko5b9e7e12014-06-26 09:58:26 +02002069 err = ovs_flow_init();
2070 if (err)
2071 goto error_unreg_rtnl_link;
2072
Jesse Grossccb13522011-10-25 19:26:31 -07002073 err = ovs_vport_init();
2074 if (err)
2075 goto error_flow_exit;
2076
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002077 err = register_pernet_device(&ovs_net_ops);
Jesse Grossccb13522011-10-25 19:26:31 -07002078 if (err)
2079 goto error_vport_exit;
2080
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002081 err = register_netdevice_notifier(&ovs_dp_device_notifier);
2082 if (err)
2083 goto error_netns_exit;
2084
Jesse Grossccb13522011-10-25 19:26:31 -07002085 err = dp_register_genl();
2086 if (err < 0)
2087 goto error_unreg_notifier;
2088
Jesse Grossccb13522011-10-25 19:26:31 -07002089 return 0;
2090
2091error_unreg_notifier:
2092 unregister_netdevice_notifier(&ovs_dp_device_notifier);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002093error_netns_exit:
2094 unregister_pernet_device(&ovs_net_ops);
Jesse Grossccb13522011-10-25 19:26:31 -07002095error_vport_exit:
2096 ovs_vport_exit();
2097error_flow_exit:
2098 ovs_flow_exit();
Jiri Pirko5b9e7e12014-06-26 09:58:26 +02002099error_unreg_rtnl_link:
2100 ovs_internal_dev_rtnl_link_unregister();
Jesse Grossccb13522011-10-25 19:26:31 -07002101error:
2102 return err;
2103}
2104
2105static void dp_cleanup(void)
2106{
Jesse Grossccb13522011-10-25 19:26:31 -07002107 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
2108 unregister_netdevice_notifier(&ovs_dp_device_notifier);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002109 unregister_pernet_device(&ovs_net_ops);
2110 rcu_barrier();
Jesse Grossccb13522011-10-25 19:26:31 -07002111 ovs_vport_exit();
2112 ovs_flow_exit();
Jiri Pirko5b9e7e12014-06-26 09:58:26 +02002113 ovs_internal_dev_rtnl_link_unregister();
Jesse Grossccb13522011-10-25 19:26:31 -07002114}
2115
2116module_init(dp_init);
2117module_exit(dp_cleanup);
2118
2119MODULE_DESCRIPTION("Open vSwitch switching datapath");
2120MODULE_LICENSE("GPL");