blob: 0dab33fb9844cd0b2207c2d816780391fb08baa3 [file] [log] [blame]
Jesse Grossccb13522011-10-25 19:26:31 -07001/*
Ben Pfaffad552002014-05-06 16:48:38 -07002 * Copyright (c) 2007-2014 Nicira, Inc.
Jesse Grossccb13522011-10-25 19:26:31 -07003 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of version 2 of the GNU General Public
6 * License as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 * 02110-1301, USA
17 */
18
19#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20
21#include <linux/init.h>
22#include <linux/module.h>
23#include <linux/if_arp.h>
24#include <linux/if_vlan.h>
25#include <linux/in.h>
26#include <linux/ip.h>
27#include <linux/jhash.h>
28#include <linux/delay.h>
29#include <linux/time.h>
30#include <linux/etherdevice.h>
31#include <linux/genetlink.h>
32#include <linux/kernel.h>
33#include <linux/kthread.h>
34#include <linux/mutex.h>
35#include <linux/percpu.h>
36#include <linux/rcupdate.h>
37#include <linux/tcp.h>
38#include <linux/udp.h>
Jesse Grossccb13522011-10-25 19:26:31 -070039#include <linux/ethtool.h>
40#include <linux/wait.h>
Jesse Grossccb13522011-10-25 19:26:31 -070041#include <asm/div64.h>
42#include <linux/highmem.h>
43#include <linux/netfilter_bridge.h>
44#include <linux/netfilter_ipv4.h>
45#include <linux/inetdevice.h>
46#include <linux/list.h>
47#include <linux/openvswitch.h>
48#include <linux/rculist.h>
49#include <linux/dmi.h>
Jesse Grossccb13522011-10-25 19:26:31 -070050#include <net/genetlink.h>
Pravin B Shelar46df7b82012-02-22 19:58:59 -080051#include <net/net_namespace.h>
52#include <net/netns/generic.h>
Jesse Grossccb13522011-10-25 19:26:31 -070053
54#include "datapath.h"
55#include "flow.h"
Andy Zhoue80857c2014-01-21 09:31:04 -080056#include "flow_table.h"
Pravin B Shelare6445712013-10-03 18:16:47 -070057#include "flow_netlink.h"
Andy Zhou96fbc132017-11-10 12:09:42 -080058#include "meter.h"
Jesse Grossccb13522011-10-25 19:26:31 -070059#include "vport-internal_dev.h"
Thomas Grafcff63a52013-04-29 13:06:41 +000060#include "vport-netdev.h"
Jesse Grossccb13522011-10-25 19:26:31 -070061
Alexey Dobriyanc7d03a02016-11-17 04:58:21 +030062unsigned int ovs_net_id __read_mostly;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -070063
Pravin B Shelar0c200ef2014-05-06 16:44:50 -070064static struct genl_family dp_packet_genl_family;
65static struct genl_family dp_flow_genl_family;
66static struct genl_family dp_datapath_genl_family;
67
Joe Stringer74ed7ab2015-01-21 16:42:52 -080068static const struct nla_policy flow_policy[];
69
stephen hemminger48e48a72014-07-16 11:25:52 -070070static const struct genl_multicast_group ovs_dp_flow_multicast_group = {
71 .name = OVS_FLOW_MCGROUP,
Pravin B Shelar0c200ef2014-05-06 16:44:50 -070072};
73
stephen hemminger48e48a72014-07-16 11:25:52 -070074static const struct genl_multicast_group ovs_dp_datapath_multicast_group = {
75 .name = OVS_DATAPATH_MCGROUP,
Pravin B Shelar0c200ef2014-05-06 16:44:50 -070076};
77
stephen hemminger48e48a72014-07-16 11:25:52 -070078static const struct genl_multicast_group ovs_dp_vport_multicast_group = {
79 .name = OVS_VPORT_MCGROUP,
Pravin B Shelar0c200ef2014-05-06 16:44:50 -070080};
81
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -070082/* Check if need to build a reply message.
83 * OVS userspace sets the NLM_F_ECHO flag if it needs the reply. */
Samuel Gauthier9b67aa42014-09-18 10:31:04 +020084static bool ovs_must_notify(struct genl_family *family, struct genl_info *info,
85 unsigned int group)
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -070086{
87 return info->nlhdr->nlmsg_flags & NLM_F_ECHO ||
Johannes Bergf8403a22014-12-22 18:56:36 +010088 genl_has_listeners(family, genl_info_net(info), group);
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -070089}
90
Johannes Berg68eb5502013-11-19 15:19:38 +010091static void ovs_notify(struct genl_family *family,
Johannes Berg2a94fe42013-11-19 15:19:39 +010092 struct sk_buff *skb, struct genl_info *info)
Thomas Grafed661182013-03-29 14:46:50 +010093{
Jiri Benc92c14d92015-09-22 18:56:43 +020094 genl_notify(family, skb, info, 0, 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 *,
Pravin B Shelare8eedb82014-11-06 06:57:27 -0800138 const struct sw_flow_key *,
William Tuf2a4d082016-06-10 11:49:33 -0700139 const struct dp_upcall_info *,
140 uint32_t cutlen);
Thomas Graf8055a892013-12-13 15:22:20 +0100141static int queue_userspace_packet(struct datapath *dp, struct sk_buff *,
Pravin B Shelare8eedb82014-11-06 06:57:27 -0800142 const struct sw_flow_key *,
William Tuf2a4d082016-06-10 11:49:33 -0700143 const struct dp_upcall_info *,
144 uint32_t cutlen);
Jesse Grossccb13522011-10-25 19:26:31 -0700145
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700146/* Must be called with rcu_read_lock or ovs_mutex. */
Andy Zhou971427f32014-09-15 19:37:25 -0700147const char *ovs_dp_name(const struct datapath *dp)
Jesse Grossccb13522011-10-25 19:26:31 -0700148{
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700149 struct vport *vport = ovs_vport_ovsl_rcu(dp, OVSP_LOCAL);
Thomas Grafc9db9652015-07-21 10:44:05 +0200150 return ovs_vport_name(vport);
Jesse Grossccb13522011-10-25 19:26:31 -0700151}
152
Thomas Graf12eb18f2014-11-06 06:58:52 -0800153static int get_dpifindex(const struct datapath *dp)
Jesse Grossccb13522011-10-25 19:26:31 -0700154{
155 struct vport *local;
156 int ifindex;
157
158 rcu_read_lock();
159
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700160 local = ovs_vport_rcu(dp, OVSP_LOCAL);
Jesse Grossccb13522011-10-25 19:26:31 -0700161 if (local)
Thomas Grafbe4ace62015-07-21 10:44:04 +0200162 ifindex = local->dev->ifindex;
Jesse Grossccb13522011-10-25 19:26:31 -0700163 else
164 ifindex = 0;
165
166 rcu_read_unlock();
167
168 return ifindex;
169}
170
171static void destroy_dp_rcu(struct rcu_head *rcu)
172{
173 struct datapath *dp = container_of(rcu, struct datapath, rcu);
174
Pravin B Shelar9b996e52014-05-06 18:41:20 -0700175 ovs_flow_tbl_destroy(&dp->table);
Jesse Grossccb13522011-10-25 19:26:31 -0700176 free_percpu(dp->stats_percpu);
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700177 kfree(dp->ports);
Andy Zhou96fbc132017-11-10 12:09:42 -0800178 ovs_meters_exit(dp);
Jesse Grossccb13522011-10-25 19:26:31 -0700179 kfree(dp);
180}
181
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700182static struct hlist_head *vport_hash_bucket(const struct datapath *dp,
183 u16 port_no)
184{
185 return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)];
186}
187
Jarno Rajahalmebb6f9a72014-05-05 11:32:17 -0700188/* Called with ovs_mutex or RCU read lock. */
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700189struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no)
190{
191 struct vport *vport;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700192 struct hlist_head *head;
193
194 head = vport_hash_bucket(dp, port_no);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800195 hlist_for_each_entry_rcu(vport, head, dp_hash_node) {
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700196 if (vport->port_no == port_no)
197 return vport;
198 }
199 return NULL;
200}
201
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700202/* Called with ovs_mutex. */
Jesse Grossccb13522011-10-25 19:26:31 -0700203static struct vport *new_vport(const struct vport_parms *parms)
204{
205 struct vport *vport;
206
207 vport = ovs_vport_add(parms);
208 if (!IS_ERR(vport)) {
209 struct datapath *dp = parms->dp;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700210 struct hlist_head *head = vport_hash_bucket(dp, vport->port_no);
Jesse Grossccb13522011-10-25 19:26:31 -0700211
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700212 hlist_add_head_rcu(&vport->dp_hash_node, head);
Jesse Grossccb13522011-10-25 19:26:31 -0700213 }
Jesse Grossccb13522011-10-25 19:26:31 -0700214 return vport;
215}
216
Jesse Grossccb13522011-10-25 19:26:31 -0700217void ovs_dp_detach_port(struct vport *p)
218{
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700219 ASSERT_OVSL();
Jesse Grossccb13522011-10-25 19:26:31 -0700220
221 /* First drop references to device. */
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700222 hlist_del_rcu(&p->dp_hash_node);
Jesse Grossccb13522011-10-25 19:26:31 -0700223
224 /* Then destroy it. */
225 ovs_vport_del(p);
226}
227
228/* Must be called with rcu_read_lock. */
Pravin B Shelar8c8b1b82014-09-15 19:28:44 -0700229void ovs_dp_process_packet(struct sk_buff *skb, struct sw_flow_key *key)
Jesse Grossccb13522011-10-25 19:26:31 -0700230{
Pravin B Shelar83c8df22014-09-15 19:20:31 -0700231 const struct vport *p = OVS_CB(skb)->input_vport;
Jesse Grossccb13522011-10-25 19:26:31 -0700232 struct datapath *dp = p->dp;
233 struct sw_flow *flow;
Lorand Jakabd98612b2014-10-06 05:45:32 -0700234 struct sw_flow_actions *sf_acts;
Jesse Grossccb13522011-10-25 19:26:31 -0700235 struct dp_stats_percpu *stats;
Jesse Grossccb13522011-10-25 19:26:31 -0700236 u64 *stats_counter;
Andy Zhou1bd71162013-10-22 10:42:46 -0700237 u32 n_mask_hit;
Jesse Grossccb13522011-10-25 19:26:31 -0700238
Shan Wei404f2f12012-11-13 09:52:25 +0800239 stats = this_cpu_ptr(dp->stats_percpu);
Jesse Grossccb13522011-10-25 19:26:31 -0700240
Jesse Grossccb13522011-10-25 19:26:31 -0700241 /* Look up flow. */
Pravin B Shelar8c8b1b82014-09-15 19:28:44 -0700242 flow = ovs_flow_tbl_lookup_stats(&dp->table, key, &n_mask_hit);
Jesse Grossccb13522011-10-25 19:26:31 -0700243 if (unlikely(!flow)) {
244 struct dp_upcall_info upcall;
Pravin B Shelar8c8b1b82014-09-15 19:28:44 -0700245 int error;
Jesse Grossccb13522011-10-25 19:26:31 -0700246
Neil McKeeccea7442015-05-26 20:59:43 -0700247 memset(&upcall, 0, sizeof(upcall));
Jesse Grossccb13522011-10-25 19:26:31 -0700248 upcall.cmd = OVS_PACKET_CMD_MISS;
Alex Wang5cd667b2014-07-17 15:14:13 -0700249 upcall.portid = ovs_vport_find_upcall_portid(p, skb);
Joe Stringer7f8a4362015-08-26 11:31:48 -0700250 upcall.mru = OVS_CB(skb)->mru;
William Tuf2a4d082016-06-10 11:49:33 -0700251 error = ovs_dp_upcall(dp, skb, key, &upcall, 0);
Li RongQingc5eba0b2014-09-03 17:43:45 +0800252 if (unlikely(error))
253 kfree_skb(skb);
254 else
255 consume_skb(skb);
Jesse Grossccb13522011-10-25 19:26:31 -0700256 stats_counter = &stats->n_missed;
257 goto out;
258 }
259
Lorand Jakabd98612b2014-10-06 05:45:32 -0700260 ovs_flow_stats_update(flow, key->tp.flags, skb);
261 sf_acts = rcu_dereference(flow->sf_acts);
262 ovs_execute_actions(dp, skb, sf_acts, key);
Jesse Grossccb13522011-10-25 19:26:31 -0700263
Pravin B Shelare298e502013-10-29 17:22:21 -0700264 stats_counter = &stats->n_hit;
Jesse Grossccb13522011-10-25 19:26:31 -0700265
266out:
267 /* Update datapath statistics. */
WANG Congdf9d9fd2014-02-14 15:10:46 -0800268 u64_stats_update_begin(&stats->syncp);
Jesse Grossccb13522011-10-25 19:26:31 -0700269 (*stats_counter)++;
Andy Zhou1bd71162013-10-22 10:42:46 -0700270 stats->n_mask_hit += n_mask_hit;
WANG Congdf9d9fd2014-02-14 15:10:46 -0800271 u64_stats_update_end(&stats->syncp);
Jesse Grossccb13522011-10-25 19:26:31 -0700272}
273
Jesse Grossccb13522011-10-25 19:26:31 -0700274int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
Pravin B Shelare8eedb82014-11-06 06:57:27 -0800275 const struct sw_flow_key *key,
William Tuf2a4d082016-06-10 11:49:33 -0700276 const struct dp_upcall_info *upcall_info,
277 uint32_t cutlen)
Jesse Grossccb13522011-10-25 19:26:31 -0700278{
279 struct dp_stats_percpu *stats;
Jesse Grossccb13522011-10-25 19:26:31 -0700280 int err;
281
Eric W. Biederman15e47302012-09-07 20:12:54 +0000282 if (upcall_info->portid == 0) {
Jesse Grossccb13522011-10-25 19:26:31 -0700283 err = -ENOTCONN;
284 goto err;
285 }
286
Jesse Grossccb13522011-10-25 19:26:31 -0700287 if (!skb_is_gso(skb))
William Tuf2a4d082016-06-10 11:49:33 -0700288 err = queue_userspace_packet(dp, skb, key, upcall_info, cutlen);
Jesse Grossccb13522011-10-25 19:26:31 -0700289 else
William Tuf2a4d082016-06-10 11:49:33 -0700290 err = queue_gso_packets(dp, skb, key, upcall_info, cutlen);
Jesse Grossccb13522011-10-25 19:26:31 -0700291 if (err)
292 goto err;
293
294 return 0;
295
296err:
Shan Wei404f2f12012-11-13 09:52:25 +0800297 stats = this_cpu_ptr(dp->stats_percpu);
Jesse Grossccb13522011-10-25 19:26:31 -0700298
WANG Congdf9d9fd2014-02-14 15:10:46 -0800299 u64_stats_update_begin(&stats->syncp);
Jesse Grossccb13522011-10-25 19:26:31 -0700300 stats->n_lost++;
WANG Congdf9d9fd2014-02-14 15:10:46 -0800301 u64_stats_update_end(&stats->syncp);
Jesse Grossccb13522011-10-25 19:26:31 -0700302
303 return err;
304}
305
Thomas Graf8055a892013-12-13 15:22:20 +0100306static int queue_gso_packets(struct datapath *dp, struct sk_buff *skb,
Pravin B Shelare8eedb82014-11-06 06:57:27 -0800307 const struct sw_flow_key *key,
William Tuf2a4d082016-06-10 11:49:33 -0700308 const struct dp_upcall_info *upcall_info,
309 uint32_t cutlen)
Jesse Grossccb13522011-10-25 19:26:31 -0700310{
Jesse Grossccb13522011-10-25 19:26:31 -0700311 struct sk_buff *segs, *nskb;
312 int err;
313
Konstantin Khlebnikov9207f9d2016-01-08 15:21:46 +0300314 BUILD_BUG_ON(sizeof(*OVS_CB(skb)) > SKB_SGO_CB_OFFSET);
Thomas Graf09c5e602013-12-13 15:22:22 +0100315 segs = __skb_gso_segment(skb, NETIF_F_SG, false);
Pravin B Shelar92e5dfc2012-07-20 14:46:29 -0700316 if (IS_ERR(segs))
317 return PTR_ERR(segs);
Florian Westphal330966e2014-10-20 13:49:17 +0200318 if (segs == NULL)
319 return -EINVAL;
Jesse Grossccb13522011-10-25 19:26:31 -0700320
321 /* Queue all of the segments. */
322 skb = segs;
323 do {
William Tuf2a4d082016-06-10 11:49:33 -0700324 err = queue_userspace_packet(dp, skb, key, upcall_info, cutlen);
Jesse Grossccb13522011-10-25 19:26:31 -0700325 if (err)
326 break;
327
Jesse Grossccb13522011-10-25 19:26:31 -0700328 } while ((skb = skb->next));
329
330 /* Free all of the segments. */
331 skb = segs;
332 do {
333 nskb = skb->next;
334 if (err)
335 kfree_skb(skb);
336 else
337 consume_skb(skb);
338 } while ((skb = nskb));
339 return err;
340}
341
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800342static size_t upcall_msg_size(const struct dp_upcall_info *upcall_info,
Liping Zhang494bea32017-08-16 13:30:07 +0800343 unsigned int hdrlen, int actions_attrlen)
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100344{
345 size_t size = NLMSG_ALIGN(sizeof(struct ovs_header))
Thomas Grafbda56f12013-12-13 15:22:21 +0100346 + nla_total_size(hdrlen) /* OVS_PACKET_ATTR_PACKET */
William Tub95e5922016-06-20 07:26:17 -0700347 + nla_total_size(ovs_key_attr_size()) /* OVS_PACKET_ATTR_KEY */
348 + nla_total_size(sizeof(unsigned int)); /* OVS_PACKET_ATTR_LEN */
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100349
350 /* OVS_PACKET_ATTR_USERDATA */
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800351 if (upcall_info->userdata)
352 size += NLA_ALIGN(upcall_info->userdata->nla_len);
353
354 /* OVS_PACKET_ATTR_EGRESS_TUN_KEY */
355 if (upcall_info->egress_tun_info)
356 size += nla_total_size(ovs_tun_key_attr_size());
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100357
Neil McKeeccea7442015-05-26 20:59:43 -0700358 /* OVS_PACKET_ATTR_ACTIONS */
359 if (upcall_info->actions_len)
Liping Zhang494bea32017-08-16 13:30:07 +0800360 size += nla_total_size(actions_attrlen);
Neil McKeeccea7442015-05-26 20:59:43 -0700361
Joe Stringer7f8a4362015-08-26 11:31:48 -0700362 /* OVS_PACKET_ATTR_MRU */
363 if (upcall_info->mru)
364 size += nla_total_size(sizeof(upcall_info->mru));
365
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100366 return size;
367}
368
Joe Stringer7f8a4362015-08-26 11:31:48 -0700369static void pad_packet(struct datapath *dp, struct sk_buff *skb)
370{
371 if (!(dp->user_features & OVS_DP_F_UNALIGNED)) {
372 size_t plen = NLA_ALIGN(skb->len) - skb->len;
373
374 if (plen > 0)
Johannes Bergb080db52017-06-16 14:29:19 +0200375 skb_put_zero(skb, plen);
Joe Stringer7f8a4362015-08-26 11:31:48 -0700376 }
377}
378
Thomas Graf8055a892013-12-13 15:22:20 +0100379static int queue_userspace_packet(struct datapath *dp, struct sk_buff *skb,
Pravin B Shelare8eedb82014-11-06 06:57:27 -0800380 const struct sw_flow_key *key,
William Tuf2a4d082016-06-10 11:49:33 -0700381 const struct dp_upcall_info *upcall_info,
382 uint32_t cutlen)
Jesse Grossccb13522011-10-25 19:26:31 -0700383{
384 struct ovs_header *upcall;
385 struct sk_buff *nskb = NULL;
Li RongQing4ee45ea2014-09-02 20:52:28 +0800386 struct sk_buff *user_skb = NULL; /* to be queued to userspace */
Jesse Grossccb13522011-10-25 19:26:31 -0700387 struct nlattr *nla;
Thomas Graf795449d2013-11-30 13:21:32 +0100388 size_t len;
Thomas Grafbda56f12013-12-13 15:22:21 +0100389 unsigned int hlen;
Thomas Graf8055a892013-12-13 15:22:20 +0100390 int err, dp_ifindex;
391
392 dp_ifindex = get_dpifindex(dp);
393 if (!dp_ifindex)
394 return -ENODEV;
Jesse Grossccb13522011-10-25 19:26:31 -0700395
Jiri Pirkodf8a39de2015-01-13 17:13:44 +0100396 if (skb_vlan_tag_present(skb)) {
Jesse Grossccb13522011-10-25 19:26:31 -0700397 nskb = skb_clone(skb, GFP_ATOMIC);
398 if (!nskb)
399 return -ENOMEM;
400
Jiri Pirko59682502014-11-19 14:04:59 +0100401 nskb = __vlan_hwaccel_push_inside(nskb);
Dan Carpenter8aa51d62012-05-13 08:44:18 +0000402 if (!nskb)
Jesse Grossccb13522011-10-25 19:26:31 -0700403 return -ENOMEM;
404
Jesse Grossccb13522011-10-25 19:26:31 -0700405 skb = nskb;
406 }
407
408 if (nla_attr_size(skb->len) > USHRT_MAX) {
409 err = -EFBIG;
410 goto out;
411 }
412
Thomas Grafbda56f12013-12-13 15:22:21 +0100413 /* Complete checksum if needed */
414 if (skb->ip_summed == CHECKSUM_PARTIAL &&
Davide Caratti75293902017-05-18 15:44:42 +0200415 (err = skb_csum_hwoffload_help(skb, 0)))
Thomas Grafbda56f12013-12-13 15:22:21 +0100416 goto out;
417
418 /* Older versions of OVS user space enforce alignment of the last
419 * Netlink attribute to NLA_ALIGNTO which would require extensive
420 * padding logic. Only perform zerocopy if padding is not required.
421 */
422 if (dp->user_features & OVS_DP_F_UNALIGNED)
423 hlen = skb_zerocopy_headlen(skb);
424 else
425 hlen = skb->len;
426
Liping Zhang494bea32017-08-16 13:30:07 +0800427 len = upcall_msg_size(upcall_info, hlen - cutlen,
428 OVS_CB(skb)->acts_origlen);
Florian Westphal551ddc02016-02-18 15:03:25 +0100429 user_skb = genlmsg_new(len, GFP_ATOMIC);
Jesse Grossccb13522011-10-25 19:26:31 -0700430 if (!user_skb) {
431 err = -ENOMEM;
432 goto out;
433 }
434
435 upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
436 0, upcall_info->cmd);
437 upcall->dp_ifindex = dp_ifindex;
438
Joe Stringer5b4237b2015-01-21 16:42:48 -0800439 err = ovs_nla_put_key(key, key, OVS_PACKET_ATTR_KEY, false, user_skb);
Andy Zhouf53e3832014-07-17 15:17:44 -0700440 BUG_ON(err);
Jesse Grossccb13522011-10-25 19:26:31 -0700441
442 if (upcall_info->userdata)
Ben Pfaff44901082013-02-15 17:29:22 -0800443 __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA,
444 nla_len(upcall_info->userdata),
445 nla_data(upcall_info->userdata));
Jesse Grossccb13522011-10-25 19:26:31 -0700446
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800447 if (upcall_info->egress_tun_info) {
448 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_EGRESS_TUN_KEY);
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700449 err = ovs_nla_put_tunnel_info(user_skb,
450 upcall_info->egress_tun_info);
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800451 BUG_ON(err);
452 nla_nest_end(user_skb, nla);
453 }
454
Neil McKeeccea7442015-05-26 20:59:43 -0700455 if (upcall_info->actions_len) {
456 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_ACTIONS);
457 err = ovs_nla_put_actions(upcall_info->actions,
458 upcall_info->actions_len,
459 user_skb);
460 if (!err)
461 nla_nest_end(user_skb, nla);
462 else
463 nla_nest_cancel(user_skb, nla);
464 }
465
Joe Stringer7f8a4362015-08-26 11:31:48 -0700466 /* Add OVS_PACKET_ATTR_MRU */
467 if (upcall_info->mru) {
468 if (nla_put_u16(user_skb, OVS_PACKET_ATTR_MRU,
469 upcall_info->mru)) {
470 err = -ENOBUFS;
471 goto out;
472 }
473 pad_packet(dp, user_skb);
474 }
475
William Tub95e5922016-06-20 07:26:17 -0700476 /* Add OVS_PACKET_ATTR_LEN when packet is truncated */
477 if (cutlen > 0) {
478 if (nla_put_u32(user_skb, OVS_PACKET_ATTR_LEN,
479 skb->len)) {
480 err = -ENOBUFS;
481 goto out;
482 }
483 pad_packet(dp, user_skb);
484 }
485
Thomas Grafbda56f12013-12-13 15:22:21 +0100486 /* Only reserve room for attribute header, packet data is added
487 * in skb_zerocopy() */
488 if (!(nla = nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, 0))) {
489 err = -ENOBUFS;
490 goto out;
491 }
William Tuf2a4d082016-06-10 11:49:33 -0700492 nla->nla_len = nla_attr_size(skb->len - cutlen);
Jesse Grossccb13522011-10-25 19:26:31 -0700493
William Tuf2a4d082016-06-10 11:49:33 -0700494 err = skb_zerocopy(user_skb, skb, skb->len - cutlen, hlen);
Zoltan Kiss36d5fe62014-03-26 22:37:45 +0000495 if (err)
496 goto out;
Jesse Grossccb13522011-10-25 19:26:31 -0700497
Thomas Grafaea0bb42014-01-14 16:27:49 +0000498 /* Pad OVS_PACKET_ATTR_PACKET if linear copy was performed */
Joe Stringer7f8a4362015-08-26 11:31:48 -0700499 pad_packet(dp, user_skb);
Thomas Grafaea0bb42014-01-14 16:27:49 +0000500
Thomas Grafbda56f12013-12-13 15:22:21 +0100501 ((struct nlmsghdr *) user_skb->data)->nlmsg_len = user_skb->len;
502
Thomas Graf8055a892013-12-13 15:22:20 +0100503 err = genlmsg_unicast(ovs_dp_get_net(dp), user_skb, upcall_info->portid);
Li RongQing4ee45ea2014-09-02 20:52:28 +0800504 user_skb = NULL;
Jesse Grossccb13522011-10-25 19:26:31 -0700505out:
Zoltan Kiss36d5fe62014-03-26 22:37:45 +0000506 if (err)
507 skb_tx_error(skb);
Li RongQing4ee45ea2014-09-02 20:52:28 +0800508 kfree_skb(user_skb);
Jesse Grossccb13522011-10-25 19:26:31 -0700509 kfree_skb(nskb);
510 return err;
511}
512
Jesse Grossccb13522011-10-25 19:26:31 -0700513static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
514{
515 struct ovs_header *ovs_header = info->userhdr;
Joe Stringer7f8a4362015-08-26 11:31:48 -0700516 struct net *net = sock_net(skb->sk);
Jesse Grossccb13522011-10-25 19:26:31 -0700517 struct nlattr **a = info->attrs;
518 struct sw_flow_actions *acts;
519 struct sk_buff *packet;
520 struct sw_flow *flow;
Lorand Jakabd98612b2014-10-06 05:45:32 -0700521 struct sw_flow_actions *sf_acts;
Jesse Grossccb13522011-10-25 19:26:31 -0700522 struct datapath *dp;
Pravin B Shelar83c8df22014-09-15 19:20:31 -0700523 struct vport *input_vport;
Joe Stringer7f8a4362015-08-26 11:31:48 -0700524 u16 mru = 0;
Jesse Grossccb13522011-10-25 19:26:31 -0700525 int len;
526 int err;
Thomas Graf1ba39802015-01-14 13:56:19 +0000527 bool log = !a[OVS_PACKET_ATTR_PROBE];
Jesse Grossccb13522011-10-25 19:26:31 -0700528
529 err = -EINVAL;
530 if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
Thomas Grafdded45fc2013-03-29 14:46:47 +0100531 !a[OVS_PACKET_ATTR_ACTIONS])
Jesse Grossccb13522011-10-25 19:26:31 -0700532 goto err;
533
534 len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
535 packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
536 err = -ENOMEM;
537 if (!packet)
538 goto err;
539 skb_reserve(packet, NET_IP_ALIGN);
540
Thomas Graf32686a92013-03-29 14:46:48 +0100541 nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
Jesse Grossccb13522011-10-25 19:26:31 -0700542
Joe Stringer7f8a4362015-08-26 11:31:48 -0700543 /* Set packet's mru */
544 if (a[OVS_PACKET_ATTR_MRU]) {
545 mru = nla_get_u16(a[OVS_PACKET_ATTR_MRU]);
546 packet->ignore_df = 1;
547 }
548 OVS_CB(packet)->mru = mru;
549
Jesse Grossccb13522011-10-25 19:26:31 -0700550 /* Build an sw_flow for sending this packet. */
Jarno Rajahalme23dabf82014-03-27 12:35:23 -0700551 flow = ovs_flow_alloc();
Jesse Grossccb13522011-10-25 19:26:31 -0700552 err = PTR_ERR(flow);
553 if (IS_ERR(flow))
554 goto err_kfree_skb;
555
Joe Stringerc2ac6672015-08-26 11:31:52 -0700556 err = ovs_flow_key_extract_userspace(net, a[OVS_PACKET_ATTR_KEY],
557 packet, &flow->key, log);
Jesse Grossccb13522011-10-25 19:26:31 -0700558 if (err)
559 goto err_flow_free;
560
Joe Stringer7f8a4362015-08-26 11:31:48 -0700561 err = ovs_nla_copy_actions(net, a[OVS_PACKET_ATTR_ACTIONS],
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800562 &flow->key, &acts, log);
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700563 if (err)
564 goto err_flow_free;
Jesse Grossccb13522011-10-25 19:26:31 -0700565
Jesse Grossf5796682014-10-03 15:35:33 -0700566 rcu_assign_pointer(flow->sf_acts, acts);
Jesse Grossccb13522011-10-25 19:26:31 -0700567 packet->priority = flow->key.phy.priority;
Ansis Atteka39c7caeb2012-11-26 11:24:11 -0800568 packet->mark = flow->key.phy.skb_mark;
Jesse Grossccb13522011-10-25 19:26:31 -0700569
570 rcu_read_lock();
Joe Stringer7f8a4362015-08-26 11:31:48 -0700571 dp = get_dp_rcu(net, ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -0700572 err = -ENODEV;
573 if (!dp)
574 goto err_unlock;
575
Pravin B Shelar83c8df22014-09-15 19:20:31 -0700576 input_vport = ovs_vport_rcu(dp, flow->key.phy.in_port);
577 if (!input_vport)
578 input_vport = ovs_vport_rcu(dp, OVSP_LOCAL);
579
580 if (!input_vport)
581 goto err_unlock;
582
Joe Stringer7f8a4362015-08-26 11:31:48 -0700583 packet->dev = input_vport->dev;
Pravin B Shelar83c8df22014-09-15 19:20:31 -0700584 OVS_CB(packet)->input_vport = input_vport;
Lorand Jakabd98612b2014-10-06 05:45:32 -0700585 sf_acts = rcu_dereference(flow->sf_acts);
Pravin B Shelar83c8df22014-09-15 19:20:31 -0700586
Jesse Grossccb13522011-10-25 19:26:31 -0700587 local_bh_disable();
Lorand Jakabd98612b2014-10-06 05:45:32 -0700588 err = ovs_execute_actions(dp, packet, sf_acts, &flow->key);
Jesse Grossccb13522011-10-25 19:26:31 -0700589 local_bh_enable();
590 rcu_read_unlock();
591
Andy Zhou03f0d912013-08-07 20:01:00 -0700592 ovs_flow_free(flow, false);
Jesse Grossccb13522011-10-25 19:26:31 -0700593 return err;
594
595err_unlock:
596 rcu_read_unlock();
597err_flow_free:
Andy Zhou03f0d912013-08-07 20:01:00 -0700598 ovs_flow_free(flow, false);
Jesse Grossccb13522011-10-25 19:26:31 -0700599err_kfree_skb:
600 kfree_skb(packet);
601err:
602 return err;
603}
604
605static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
Thomas Grafdded45fc2013-03-29 14:46:47 +0100606 [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
Jesse Grossccb13522011-10-25 19:26:31 -0700607 [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
608 [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
Thomas Graf1ba39802015-01-14 13:56:19 +0000609 [OVS_PACKET_ATTR_PROBE] = { .type = NLA_FLAG },
Joe Stringer7f8a4362015-08-26 11:31:48 -0700610 [OVS_PACKET_ATTR_MRU] = { .type = NLA_U16 },
Jesse Grossccb13522011-10-25 19:26:31 -0700611};
612
Johannes Berg4534de82013-11-14 17:14:46 +0100613static const struct genl_ops dp_packet_genl_ops[] = {
Jesse Grossccb13522011-10-25 19:26:31 -0700614 { .cmd = OVS_PACKET_CMD_EXECUTE,
Tycho Andersen4a926022016-02-05 09:20:52 -0700615 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
Jesse Grossccb13522011-10-25 19:26:31 -0700616 .policy = packet_policy,
617 .doit = ovs_packet_cmd_execute
618 }
619};
620
Johannes Berg56989f62016-10-24 14:40:05 +0200621static struct genl_family dp_packet_genl_family __ro_after_init = {
Pravin B Shelar0c200ef2014-05-06 16:44:50 -0700622 .hdrsize = sizeof(struct ovs_header),
623 .name = OVS_PACKET_FAMILY,
624 .version = OVS_PACKET_VERSION,
625 .maxattr = OVS_PACKET_ATTR_MAX,
626 .netnsok = true,
627 .parallel_ops = true,
628 .ops = dp_packet_genl_ops,
629 .n_ops = ARRAY_SIZE(dp_packet_genl_ops),
Johannes Berg489111e2016-10-24 14:40:03 +0200630 .module = THIS_MODULE,
Pravin B Shelar0c200ef2014-05-06 16:44:50 -0700631};
632
Thomas Graf12eb18f2014-11-06 06:58:52 -0800633static void get_dp_stats(const struct datapath *dp, struct ovs_dp_stats *stats,
Andy Zhou1bd71162013-10-22 10:42:46 -0700634 struct ovs_dp_megaflow_stats *mega_stats)
Jesse Grossccb13522011-10-25 19:26:31 -0700635{
636 int i;
Jesse Grossccb13522011-10-25 19:26:31 -0700637
Andy Zhou1bd71162013-10-22 10:42:46 -0700638 memset(mega_stats, 0, sizeof(*mega_stats));
639
Pravin B Shelarb637e492013-10-04 00:14:23 -0700640 stats->n_flows = ovs_flow_tbl_count(&dp->table);
Andy Zhou1bd71162013-10-22 10:42:46 -0700641 mega_stats->n_masks = ovs_flow_tbl_num_masks(&dp->table);
Jesse Grossccb13522011-10-25 19:26:31 -0700642
643 stats->n_hit = stats->n_missed = stats->n_lost = 0;
Andy Zhou1bd71162013-10-22 10:42:46 -0700644
Jesse Grossccb13522011-10-25 19:26:31 -0700645 for_each_possible_cpu(i) {
646 const struct dp_stats_percpu *percpu_stats;
647 struct dp_stats_percpu local_stats;
648 unsigned int start;
649
650 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
651
652 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700653 start = u64_stats_fetch_begin_irq(&percpu_stats->syncp);
Jesse Grossccb13522011-10-25 19:26:31 -0700654 local_stats = *percpu_stats;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700655 } while (u64_stats_fetch_retry_irq(&percpu_stats->syncp, start));
Jesse Grossccb13522011-10-25 19:26:31 -0700656
657 stats->n_hit += local_stats.n_hit;
658 stats->n_missed += local_stats.n_missed;
659 stats->n_lost += local_stats.n_lost;
Andy Zhou1bd71162013-10-22 10:42:46 -0700660 mega_stats->n_mask_hit += local_stats.n_mask_hit;
Jesse Grossccb13522011-10-25 19:26:31 -0700661 }
662}
663
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800664static bool should_fill_key(const struct sw_flow_id *sfid, uint32_t ufid_flags)
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100665{
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800666 return ovs_identifier_is_ufid(sfid) &&
667 !(ufid_flags & OVS_UFID_F_OMIT_KEY);
668}
669
670static bool should_fill_mask(uint32_t ufid_flags)
671{
672 return !(ufid_flags & OVS_UFID_F_OMIT_MASK);
673}
674
675static bool should_fill_actions(uint32_t ufid_flags)
676{
677 return !(ufid_flags & OVS_UFID_F_OMIT_ACTIONS);
678}
679
680static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts,
681 const struct sw_flow_id *sfid,
682 uint32_t ufid_flags)
683{
684 size_t len = NLMSG_ALIGN(sizeof(struct ovs_header));
685
686 /* OVS_FLOW_ATTR_UFID */
687 if (sfid && ovs_identifier_is_ufid(sfid))
688 len += nla_total_size(sfid->ufid_len);
689
690 /* OVS_FLOW_ATTR_KEY */
691 if (!sfid || should_fill_key(sfid, ufid_flags))
692 len += nla_total_size(ovs_key_attr_size());
693
694 /* OVS_FLOW_ATTR_MASK */
695 if (should_fill_mask(ufid_flags))
696 len += nla_total_size(ovs_key_attr_size());
697
698 /* OVS_FLOW_ATTR_ACTIONS */
699 if (should_fill_actions(ufid_flags))
Joe Stringer8e2fed12015-08-26 11:31:44 -0700700 len += nla_total_size(acts->orig_len);
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800701
702 return len
Nicolas Dichtel66c7a5e2016-04-26 10:06:15 +0200703 + nla_total_size_64bit(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100704 + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
Nicolas Dichtel66c7a5e2016-04-26 10:06:15 +0200705 + nla_total_size_64bit(8); /* OVS_FLOW_ATTR_USED */
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100706}
707
Jarno Rajahalmebb6f9a72014-05-05 11:32:17 -0700708/* Called with ovs_mutex or RCU read lock. */
Joe Stringerca7105f2014-09-08 13:09:37 -0700709static int ovs_flow_cmd_fill_stats(const struct sw_flow *flow,
710 struct sk_buff *skb)
711{
712 struct ovs_flow_stats stats;
713 __be16 tcp_flags;
714 unsigned long used;
Andy Zhou03f0d912013-08-07 20:01:00 -0700715
Pravin B Shelare298e502013-10-29 17:22:21 -0700716 ovs_flow_stats_get(flow, &stats, &used, &tcp_flags);
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700717
David S. Miller028d6a62012-03-29 23:20:48 -0400718 if (used &&
Nicolas Dichtel0238b722016-04-25 10:25:17 +0200719 nla_put_u64_64bit(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used),
720 OVS_FLOW_ATTR_PAD))
Joe Stringerca7105f2014-09-08 13:09:37 -0700721 return -EMSGSIZE;
Jesse Grossccb13522011-10-25 19:26:31 -0700722
David S. Miller028d6a62012-03-29 23:20:48 -0400723 if (stats.n_packets &&
Nicolas Dichtel66c7a5e2016-04-26 10:06:15 +0200724 nla_put_64bit(skb, OVS_FLOW_ATTR_STATS,
725 sizeof(struct ovs_flow_stats), &stats,
726 OVS_FLOW_ATTR_PAD))
Joe Stringerca7105f2014-09-08 13:09:37 -0700727 return -EMSGSIZE;
Jesse Grossccb13522011-10-25 19:26:31 -0700728
Pravin B Shelare298e502013-10-29 17:22:21 -0700729 if ((u8)ntohs(tcp_flags) &&
730 nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, (u8)ntohs(tcp_flags)))
Joe Stringerca7105f2014-09-08 13:09:37 -0700731 return -EMSGSIZE;
732
733 return 0;
734}
735
736/* Called with ovs_mutex or RCU read lock. */
737static int ovs_flow_cmd_fill_actions(const struct sw_flow *flow,
738 struct sk_buff *skb, int skb_orig_len)
739{
740 struct nlattr *start;
741 int err;
Jesse Grossccb13522011-10-25 19:26:31 -0700742
743 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
744 * this is the first flow to be dumped into 'skb'. This is unusual for
745 * Netlink but individual action lists can be longer than
746 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
747 * The userspace caller can always fetch the actions separately if it
748 * really wants them. (Most userspace callers in fact don't care.)
749 *
750 * This can only fail for dump operations because the skb is always
751 * properly sized for single flows.
752 */
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700753 start = nla_nest_start(skb, OVS_FLOW_ATTR_ACTIONS);
754 if (start) {
Pravin B Shelard57170b2013-07-30 15:39:39 -0700755 const struct sw_flow_actions *sf_acts;
756
Jesse Gross663efa32013-12-03 10:58:53 -0800757 sf_acts = rcu_dereference_ovsl(flow->sf_acts);
Pravin B Shelare6445712013-10-03 18:16:47 -0700758 err = ovs_nla_put_actions(sf_acts->actions,
759 sf_acts->actions_len, skb);
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700760
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700761 if (!err)
762 nla_nest_end(skb, start);
763 else {
764 if (skb_orig_len)
Joe Stringerca7105f2014-09-08 13:09:37 -0700765 return err;
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700766
767 nla_nest_cancel(skb, start);
768 }
Joe Stringerca7105f2014-09-08 13:09:37 -0700769 } else if (skb_orig_len) {
770 return -EMSGSIZE;
771 }
772
773 return 0;
774}
775
776/* Called with ovs_mutex or RCU read lock. */
777static int ovs_flow_cmd_fill_info(const struct sw_flow *flow, int dp_ifindex,
778 struct sk_buff *skb, u32 portid,
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800779 u32 seq, u32 flags, u8 cmd, u32 ufid_flags)
Joe Stringerca7105f2014-09-08 13:09:37 -0700780{
781 const int skb_orig_len = skb->len;
782 struct ovs_header *ovs_header;
783 int err;
784
785 ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family,
786 flags, cmd);
787 if (!ovs_header)
788 return -EMSGSIZE;
789
790 ovs_header->dp_ifindex = dp_ifindex;
791
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800792 err = ovs_nla_put_identifier(flow, skb);
Joe Stringer5b4237b2015-01-21 16:42:48 -0800793 if (err)
794 goto error;
795
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800796 if (should_fill_key(&flow->id, ufid_flags)) {
797 err = ovs_nla_put_masked_key(flow, skb);
798 if (err)
799 goto error;
800 }
801
802 if (should_fill_mask(ufid_flags)) {
803 err = ovs_nla_put_mask(flow, skb);
804 if (err)
805 goto error;
806 }
Joe Stringerca7105f2014-09-08 13:09:37 -0700807
808 err = ovs_flow_cmd_fill_stats(flow, skb);
809 if (err)
810 goto error;
811
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800812 if (should_fill_actions(ufid_flags)) {
813 err = ovs_flow_cmd_fill_actions(flow, skb, skb_orig_len);
814 if (err)
815 goto error;
816 }
Jesse Grossccb13522011-10-25 19:26:31 -0700817
Johannes Berg053c0952015-01-16 22:09:00 +0100818 genlmsg_end(skb, ovs_header);
819 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -0700820
Jesse Grossccb13522011-10-25 19:26:31 -0700821error:
822 genlmsg_cancel(skb, ovs_header);
823 return err;
824}
825
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700826/* May not be called with RCU read lock. */
827static struct sk_buff *ovs_flow_cmd_alloc_info(const struct sw_flow_actions *acts,
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800828 const struct sw_flow_id *sfid,
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700829 struct genl_info *info,
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800830 bool always,
831 uint32_t ufid_flags)
Jesse Grossccb13522011-10-25 19:26:31 -0700832{
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700833 struct sk_buff *skb;
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800834 size_t len;
Jesse Grossccb13522011-10-25 19:26:31 -0700835
Samuel Gauthier9b67aa42014-09-18 10:31:04 +0200836 if (!always && !ovs_must_notify(&dp_flow_genl_family, info, 0))
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700837 return NULL;
838
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800839 len = ovs_flow_cmd_msg_size(acts, sfid, ufid_flags);
Florian Westphal551ddc02016-02-18 15:03:25 +0100840 skb = genlmsg_new(len, GFP_KERNEL);
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700841 if (!skb)
842 return ERR_PTR(-ENOMEM);
843
844 return skb;
Jesse Grossccb13522011-10-25 19:26:31 -0700845}
846
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700847/* Called with ovs_mutex. */
848static struct sk_buff *ovs_flow_cmd_build_info(const struct sw_flow *flow,
849 int dp_ifindex,
850 struct genl_info *info, u8 cmd,
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800851 bool always, u32 ufid_flags)
Jesse Grossccb13522011-10-25 19:26:31 -0700852{
853 struct sk_buff *skb;
854 int retval;
855
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800856 skb = ovs_flow_cmd_alloc_info(ovsl_dereference(flow->sf_acts),
857 &flow->id, info, always, ufid_flags);
Himangi Saraogid0e992a2014-07-27 12:37:46 +0530858 if (IS_ERR_OR_NULL(skb))
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700859 return skb;
Jesse Grossccb13522011-10-25 19:26:31 -0700860
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700861 retval = ovs_flow_cmd_fill_info(flow, dp_ifindex, skb,
862 info->snd_portid, info->snd_seq, 0,
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800863 cmd, ufid_flags);
Jesse Grossccb13522011-10-25 19:26:31 -0700864 BUG_ON(retval < 0);
865 return skb;
866}
867
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700868static int ovs_flow_cmd_new(struct sk_buff *skb, struct genl_info *info)
Jesse Grossccb13522011-10-25 19:26:31 -0700869{
Joe Stringer7f8a4362015-08-26 11:31:48 -0700870 struct net *net = sock_net(skb->sk);
Jesse Grossccb13522011-10-25 19:26:31 -0700871 struct nlattr **a = info->attrs;
872 struct ovs_header *ovs_header = info->userhdr;
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800873 struct sw_flow *flow = NULL, *new_flow;
Andy Zhou03f0d912013-08-07 20:01:00 -0700874 struct sw_flow_mask mask;
Jesse Grossccb13522011-10-25 19:26:31 -0700875 struct sk_buff *reply;
876 struct datapath *dp;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700877 struct sw_flow_actions *acts;
878 struct sw_flow_match match;
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800879 u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700880 int error;
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800881 bool log = !a[OVS_FLOW_ATTR_PROBE];
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700882
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700883 /* Must have key and actions. */
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700884 error = -EINVAL;
Jesse Gross426cda52014-10-06 05:08:38 -0700885 if (!a[OVS_FLOW_ATTR_KEY]) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800886 OVS_NLERR(log, "Flow key attr not present in new flow.");
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700887 goto error;
Jesse Gross426cda52014-10-06 05:08:38 -0700888 }
889 if (!a[OVS_FLOW_ATTR_ACTIONS]) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800890 OVS_NLERR(log, "Flow actions attr not present in new flow.");
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700891 goto error;
Jesse Gross426cda52014-10-06 05:08:38 -0700892 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700893
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700894 /* Most of the time we need to allocate a new flow, do it before
895 * locking.
896 */
897 new_flow = ovs_flow_alloc();
898 if (IS_ERR(new_flow)) {
899 error = PTR_ERR(new_flow);
900 goto error;
901 }
902
903 /* Extract key. */
pravin shelar22799942016-09-19 13:51:00 -0700904 ovs_match_init(&match, &new_flow->key, false, &mask);
Joe Stringerc2ac6672015-08-26 11:31:52 -0700905 error = ovs_nla_get_match(net, &match, a[OVS_FLOW_ATTR_KEY],
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800906 a[OVS_FLOW_ATTR_MASK], log);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700907 if (error)
908 goto err_kfree_flow;
909
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800910 /* Extract flow identifier. */
911 error = ovs_nla_get_identifier(&new_flow->id, a[OVS_FLOW_ATTR_UFID],
pravin shelar190aa3e72016-09-19 13:50:59 -0700912 &new_flow->key, log);
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800913 if (error)
914 goto err_kfree_flow;
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700915
pravin shelar190aa3e72016-09-19 13:50:59 -0700916 /* unmasked key is needed to match when ufid is not used. */
917 if (ovs_identifier_is_key(&new_flow->id))
918 match.key = new_flow->id.unmasked_key;
919
920 ovs_flow_mask_key(&new_flow->key, &new_flow->key, true, &mask);
921
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700922 /* Validate actions. */
Joe Stringer7f8a4362015-08-26 11:31:48 -0700923 error = ovs_nla_copy_actions(net, a[OVS_FLOW_ATTR_ACTIONS],
924 &new_flow->key, &acts, log);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700925 if (error) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800926 OVS_NLERR(log, "Flow actions may not be safe on all matching packets.");
Pravin B Shelar2fdb9572014-10-19 11:19:51 -0700927 goto err_kfree_flow;
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700928 }
929
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800930 reply = ovs_flow_cmd_alloc_info(acts, &new_flow->id, info, false,
931 ufid_flags);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700932 if (IS_ERR(reply)) {
933 error = PTR_ERR(reply);
934 goto err_kfree_acts;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700935 }
936
937 ovs_lock();
Joe Stringer7f8a4362015-08-26 11:31:48 -0700938 dp = get_dp(net, ovs_header->dp_ifindex);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700939 if (unlikely(!dp)) {
940 error = -ENODEV;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700941 goto err_unlock_ovs;
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700942 }
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800943
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700944 /* Check if this is a duplicate flow */
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800945 if (ovs_identifier_is_ufid(&new_flow->id))
946 flow = ovs_flow_tbl_lookup_ufid(&dp->table, &new_flow->id);
947 if (!flow)
pravin shelar190aa3e72016-09-19 13:50:59 -0700948 flow = ovs_flow_tbl_lookup(&dp->table, &new_flow->key);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700949 if (likely(!flow)) {
950 rcu_assign_pointer(new_flow->sf_acts, acts);
951
952 /* Put flow in bucket. */
953 error = ovs_flow_tbl_insert(&dp->table, new_flow, &mask);
954 if (unlikely(error)) {
955 acts = NULL;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700956 goto err_unlock_ovs;
957 }
958
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700959 if (unlikely(reply)) {
960 error = ovs_flow_cmd_fill_info(new_flow,
961 ovs_header->dp_ifindex,
962 reply, info->snd_portid,
963 info->snd_seq, 0,
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800964 OVS_FLOW_CMD_NEW,
965 ufid_flags);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700966 BUG_ON(error < 0);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700967 }
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700968 ovs_unlock();
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700969 } else {
970 struct sw_flow_actions *old_acts;
971
972 /* Bail out if we're not allowed to modify an existing flow.
973 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
974 * because Generic Netlink treats the latter as a dump
975 * request. We also accept NLM_F_EXCL in case that bug ever
976 * gets fixed.
977 */
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700978 if (unlikely(info->nlhdr->nlmsg_flags & (NLM_F_CREATE
979 | NLM_F_EXCL))) {
980 error = -EEXIST;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700981 goto err_unlock_ovs;
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700982 }
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800983 /* The flow identifier has to be the same for flow updates.
984 * Look for any overlapping flow.
985 */
986 if (unlikely(!ovs_flow_cmp(flow, &match))) {
987 if (ovs_identifier_is_key(&flow->id))
988 flow = ovs_flow_tbl_lookup_exact(&dp->table,
989 &match);
990 else /* UFID matches but key is different */
991 flow = NULL;
Alex Wang4a46b242014-06-30 20:30:29 -0700992 if (!flow) {
993 error = -ENOENT;
994 goto err_unlock_ovs;
995 }
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700996 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700997 /* Update actions. */
998 old_acts = ovsl_dereference(flow->sf_acts);
999 rcu_assign_pointer(flow->sf_acts, acts);
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001000
1001 if (unlikely(reply)) {
1002 error = ovs_flow_cmd_fill_info(flow,
1003 ovs_header->dp_ifindex,
1004 reply, info->snd_portid,
1005 info->snd_seq, 0,
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001006 OVS_FLOW_CMD_NEW,
1007 ufid_flags);
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001008 BUG_ON(error < 0);
1009 }
1010 ovs_unlock();
1011
Thomas Graf34ae9322015-07-21 10:44:03 +02001012 ovs_nla_free_flow_actions_rcu(old_acts);
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001013 ovs_flow_free(new_flow, false);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001014 }
1015
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001016 if (reply)
1017 ovs_notify(&dp_flow_genl_family, reply, info);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001018 return 0;
1019
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001020err_unlock_ovs:
1021 ovs_unlock();
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001022 kfree_skb(reply);
1023err_kfree_acts:
Thomas Graf34ae9322015-07-21 10:44:03 +02001024 ovs_nla_free_flow_actions(acts);
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001025err_kfree_flow:
1026 ovs_flow_free(new_flow, false);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001027error:
1028 return error;
1029}
1030
Pravin B Shelar2fdb9572014-10-19 11:19:51 -07001031/* Factor out action copy to avoid "Wframe-larger-than=1024" warning. */
Joe Stringer7f8a4362015-08-26 11:31:48 -07001032static struct sw_flow_actions *get_flow_actions(struct net *net,
1033 const struct nlattr *a,
Jesse Gross6b205b22014-10-03 15:35:32 -07001034 const struct sw_flow_key *key,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001035 const struct sw_flow_mask *mask,
1036 bool log)
Jesse Gross6b205b22014-10-03 15:35:32 -07001037{
1038 struct sw_flow_actions *acts;
1039 struct sw_flow_key masked_key;
1040 int error;
1041
Jesse Grossae5f2fb2015-09-21 20:21:20 -07001042 ovs_flow_mask_key(&masked_key, key, true, mask);
Joe Stringer7f8a4362015-08-26 11:31:48 -07001043 error = ovs_nla_copy_actions(net, a, &masked_key, &acts, log);
Jesse Gross6b205b22014-10-03 15:35:32 -07001044 if (error) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001045 OVS_NLERR(log,
1046 "Actions may not be safe on all matching packets");
Jesse Gross6b205b22014-10-03 15:35:32 -07001047 return ERR_PTR(error);
1048 }
1049
1050 return acts;
1051}
1052
Tonghao Zhang9cc9a5c2017-06-29 17:27:44 -07001053/* Factor out match-init and action-copy to avoid
1054 * "Wframe-larger-than=1024" warning. Because mask is only
1055 * used to get actions, we new a function to save some
1056 * stack space.
1057 *
1058 * If there are not key and action attrs, we return 0
1059 * directly. In the case, the caller will also not use the
1060 * match as before. If there is action attr, we try to get
1061 * actions and save them to *acts. Before returning from
1062 * the function, we reset the match->mask pointer. Because
1063 * we should not to return match object with dangling reference
1064 * to mask.
1065 * */
1066static int ovs_nla_init_match_and_action(struct net *net,
1067 struct sw_flow_match *match,
1068 struct sw_flow_key *key,
1069 struct nlattr **a,
1070 struct sw_flow_actions **acts,
1071 bool log)
1072{
1073 struct sw_flow_mask mask;
1074 int error = 0;
1075
1076 if (a[OVS_FLOW_ATTR_KEY]) {
1077 ovs_match_init(match, key, true, &mask);
1078 error = ovs_nla_get_match(net, match, a[OVS_FLOW_ATTR_KEY],
1079 a[OVS_FLOW_ATTR_MASK], log);
1080 if (error)
1081 goto error;
1082 }
1083
1084 if (a[OVS_FLOW_ATTR_ACTIONS]) {
1085 if (!a[OVS_FLOW_ATTR_KEY]) {
1086 OVS_NLERR(log,
1087 "Flow key attribute not present in set flow.");
Christophe JAILLET5829e622017-09-11 21:56:20 +02001088 error = -EINVAL;
1089 goto error;
Tonghao Zhang9cc9a5c2017-06-29 17:27:44 -07001090 }
1091
1092 *acts = get_flow_actions(net, a[OVS_FLOW_ATTR_ACTIONS], key,
1093 &mask, log);
1094 if (IS_ERR(*acts)) {
1095 error = PTR_ERR(*acts);
1096 goto error;
1097 }
1098 }
1099
1100 /* On success, error is 0. */
1101error:
1102 match->mask = NULL;
1103 return error;
1104}
1105
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001106static int ovs_flow_cmd_set(struct sk_buff *skb, struct genl_info *info)
1107{
Joe Stringer7f8a4362015-08-26 11:31:48 -07001108 struct net *net = sock_net(skb->sk);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001109 struct nlattr **a = info->attrs;
1110 struct ovs_header *ovs_header = info->userhdr;
Jesse Gross6b205b22014-10-03 15:35:32 -07001111 struct sw_flow_key key;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001112 struct sw_flow *flow;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001113 struct sk_buff *reply = NULL;
1114 struct datapath *dp;
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001115 struct sw_flow_actions *old_acts = NULL, *acts = NULL;
Andy Zhou03f0d912013-08-07 20:01:00 -07001116 struct sw_flow_match match;
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001117 struct sw_flow_id sfid;
1118 u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
Samuel Gauthier6f15cdbf82016-03-10 17:14:59 +01001119 int error = 0;
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001120 bool log = !a[OVS_FLOW_ATTR_PROBE];
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001121 bool ufid_present;
Jesse Grossccb13522011-10-25 19:26:31 -07001122
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001123 ufid_present = ovs_nla_get_ufid(&sfid, a[OVS_FLOW_ATTR_UFID], log);
Tonghao Zhang9cc9a5c2017-06-29 17:27:44 -07001124 if (!a[OVS_FLOW_ATTR_KEY] && !ufid_present) {
Samuel Gauthier6f15cdbf82016-03-10 17:14:59 +01001125 OVS_NLERR(log,
1126 "Flow set message rejected, Key attribute missing.");
Tonghao Zhang9cc9a5c2017-06-29 17:27:44 -07001127 return -EINVAL;
Samuel Gauthier6f15cdbf82016-03-10 17:14:59 +01001128 }
Tonghao Zhang9cc9a5c2017-06-29 17:27:44 -07001129
1130 error = ovs_nla_init_match_and_action(net, &match, &key, a,
1131 &acts, log);
Jesse Grossccb13522011-10-25 19:26:31 -07001132 if (error)
1133 goto error;
1134
Tonghao Zhang9cc9a5c2017-06-29 17:27:44 -07001135 if (acts) {
Pravin B Shelar2fdb9572014-10-19 11:19:51 -07001136 /* Can allocate before locking if have acts. */
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001137 reply = ovs_flow_cmd_alloc_info(acts, &sfid, info, false,
1138 ufid_flags);
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001139 if (IS_ERR(reply)) {
1140 error = PTR_ERR(reply);
1141 goto err_kfree_acts;
Andy Zhou03f0d912013-08-07 20:01:00 -07001142 }
Jesse Grossccb13522011-10-25 19:26:31 -07001143 }
1144
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001145 ovs_lock();
Joe Stringer7f8a4362015-08-26 11:31:48 -07001146 dp = get_dp(net, ovs_header->dp_ifindex);
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001147 if (unlikely(!dp)) {
1148 error = -ENODEV;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001149 goto err_unlock_ovs;
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001150 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001151 /* Check that the flow exists. */
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001152 if (ufid_present)
1153 flow = ovs_flow_tbl_lookup_ufid(&dp->table, &sfid);
1154 else
1155 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001156 if (unlikely(!flow)) {
1157 error = -ENOENT;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001158 goto err_unlock_ovs;
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001159 }
Alex Wang4a46b242014-06-30 20:30:29 -07001160
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001161 /* Update actions, if present. */
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001162 if (likely(acts)) {
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001163 old_acts = ovsl_dereference(flow->sf_acts);
Jesse Grossccb13522011-10-25 19:26:31 -07001164 rcu_assign_pointer(flow->sf_acts, acts);
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001165
1166 if (unlikely(reply)) {
1167 error = ovs_flow_cmd_fill_info(flow,
1168 ovs_header->dp_ifindex,
1169 reply, info->snd_portid,
1170 info->snd_seq, 0,
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001171 OVS_FLOW_CMD_NEW,
1172 ufid_flags);
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001173 BUG_ON(error < 0);
1174 }
1175 } else {
1176 /* Could not alloc without acts before locking. */
1177 reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex,
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001178 info, OVS_FLOW_CMD_NEW, false,
1179 ufid_flags);
1180
Viresh Kumarb5ffe632015-08-12 15:59:47 +05301181 if (IS_ERR(reply)) {
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001182 error = PTR_ERR(reply);
1183 goto err_unlock_ovs;
1184 }
Jesse Grossccb13522011-10-25 19:26:31 -07001185 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001186
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001187 /* Clear stats. */
1188 if (a[OVS_FLOW_ATTR_CLEAR])
1189 ovs_flow_stats_clear(flow);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001190 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001191
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001192 if (reply)
1193 ovs_notify(&dp_flow_genl_family, reply, info);
1194 if (old_acts)
Thomas Graf34ae9322015-07-21 10:44:03 +02001195 ovs_nla_free_flow_actions_rcu(old_acts);
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -07001196
Jesse Grossccb13522011-10-25 19:26:31 -07001197 return 0;
1198
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001199err_unlock_ovs:
1200 ovs_unlock();
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001201 kfree_skb(reply);
1202err_kfree_acts:
Thomas Graf34ae9322015-07-21 10:44:03 +02001203 ovs_nla_free_flow_actions(acts);
Jesse Grossccb13522011-10-25 19:26:31 -07001204error:
1205 return error;
1206}
1207
1208static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1209{
1210 struct nlattr **a = info->attrs;
1211 struct ovs_header *ovs_header = info->userhdr;
Joe Stringerc2ac6672015-08-26 11:31:52 -07001212 struct net *net = sock_net(skb->sk);
Jesse Grossccb13522011-10-25 19:26:31 -07001213 struct sw_flow_key key;
1214 struct sk_buff *reply;
1215 struct sw_flow *flow;
1216 struct datapath *dp;
Andy Zhou03f0d912013-08-07 20:01:00 -07001217 struct sw_flow_match match;
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001218 struct sw_flow_id ufid;
1219 u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
1220 int err = 0;
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001221 bool log = !a[OVS_FLOW_ATTR_PROBE];
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001222 bool ufid_present;
Jesse Grossccb13522011-10-25 19:26:31 -07001223
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001224 ufid_present = ovs_nla_get_ufid(&ufid, a[OVS_FLOW_ATTR_UFID], log);
1225 if (a[OVS_FLOW_ATTR_KEY]) {
pravin shelar22799942016-09-19 13:51:00 -07001226 ovs_match_init(&match, &key, true, NULL);
Joe Stringerc2ac6672015-08-26 11:31:52 -07001227 err = ovs_nla_get_match(net, &match, a[OVS_FLOW_ATTR_KEY], NULL,
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001228 log);
1229 } else if (!ufid_present) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001230 OVS_NLERR(log,
1231 "Flow get message rejected, Key attribute missing.");
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001232 err = -EINVAL;
Andy Zhou03f0d912013-08-07 20:01:00 -07001233 }
Jesse Grossccb13522011-10-25 19:26:31 -07001234 if (err)
1235 return err;
1236
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001237 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001238 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001239 if (!dp) {
1240 err = -ENODEV;
1241 goto unlock;
1242 }
Jesse Grossccb13522011-10-25 19:26:31 -07001243
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001244 if (ufid_present)
1245 flow = ovs_flow_tbl_lookup_ufid(&dp->table, &ufid);
1246 else
1247 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
Alex Wang4a46b242014-06-30 20:30:29 -07001248 if (!flow) {
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001249 err = -ENOENT;
1250 goto unlock;
1251 }
Jesse Grossccb13522011-10-25 19:26:31 -07001252
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -07001253 reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex, info,
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001254 OVS_FLOW_CMD_NEW, true, ufid_flags);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001255 if (IS_ERR(reply)) {
1256 err = PTR_ERR(reply);
1257 goto unlock;
1258 }
Jesse Grossccb13522011-10-25 19:26:31 -07001259
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001260 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001261 return genlmsg_reply(reply, info);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001262unlock:
1263 ovs_unlock();
1264 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001265}
1266
1267static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1268{
1269 struct nlattr **a = info->attrs;
1270 struct ovs_header *ovs_header = info->userhdr;
Joe Stringerc2ac6672015-08-26 11:31:52 -07001271 struct net *net = sock_net(skb->sk);
Jesse Grossccb13522011-10-25 19:26:31 -07001272 struct sw_flow_key key;
1273 struct sk_buff *reply;
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001274 struct sw_flow *flow = NULL;
Jesse Grossccb13522011-10-25 19:26:31 -07001275 struct datapath *dp;
Andy Zhou03f0d912013-08-07 20:01:00 -07001276 struct sw_flow_match match;
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001277 struct sw_flow_id ufid;
1278 u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
Jesse Grossccb13522011-10-25 19:26:31 -07001279 int err;
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001280 bool log = !a[OVS_FLOW_ATTR_PROBE];
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001281 bool ufid_present;
Jesse Grossccb13522011-10-25 19:26:31 -07001282
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001283 ufid_present = ovs_nla_get_ufid(&ufid, a[OVS_FLOW_ATTR_UFID], log);
1284 if (a[OVS_FLOW_ATTR_KEY]) {
pravin shelar22799942016-09-19 13:51:00 -07001285 ovs_match_init(&match, &key, true, NULL);
Joe Stringerc2ac6672015-08-26 11:31:52 -07001286 err = ovs_nla_get_match(net, &match, a[OVS_FLOW_ATTR_KEY],
1287 NULL, log);
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001288 if (unlikely(err))
1289 return err;
1290 }
1291
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001292 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001293 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001294 if (unlikely(!dp)) {
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001295 err = -ENODEV;
1296 goto unlock;
1297 }
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001298
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001299 if (unlikely(!a[OVS_FLOW_ATTR_KEY] && !ufid_present)) {
Pravin B Shelarb637e492013-10-04 00:14:23 -07001300 err = ovs_flow_tbl_flush(&dp->table);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001301 goto unlock;
1302 }
Andy Zhou03f0d912013-08-07 20:01:00 -07001303
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001304 if (ufid_present)
1305 flow = ovs_flow_tbl_lookup_ufid(&dp->table, &ufid);
1306 else
1307 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
Alex Wang4a46b242014-06-30 20:30:29 -07001308 if (unlikely(!flow)) {
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001309 err = -ENOENT;
1310 goto unlock;
1311 }
Jesse Grossccb13522011-10-25 19:26:31 -07001312
Pravin B Shelarb637e492013-10-04 00:14:23 -07001313 ovs_flow_tbl_remove(&dp->table, flow);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001314 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001315
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001316 reply = ovs_flow_cmd_alloc_info((const struct sw_flow_actions __force *) flow->sf_acts,
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001317 &flow->id, info, false, ufid_flags);
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001318 if (likely(reply)) {
1319 if (likely(!IS_ERR(reply))) {
1320 rcu_read_lock(); /*To keep RCU checker happy. */
1321 err = ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex,
1322 reply, info->snd_portid,
1323 info->snd_seq, 0,
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001324 OVS_FLOW_CMD_DEL,
1325 ufid_flags);
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001326 rcu_read_unlock();
1327 BUG_ON(err < 0);
1328
1329 ovs_notify(&dp_flow_genl_family, reply, info);
1330 } else {
1331 netlink_set_err(sock_net(skb->sk)->genl_sock, 0, 0, PTR_ERR(reply));
1332 }
1333 }
1334
1335 ovs_flow_free(flow, true);
Jesse Grossccb13522011-10-25 19:26:31 -07001336 return 0;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001337unlock:
1338 ovs_unlock();
1339 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001340}
1341
1342static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1343{
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001344 struct nlattr *a[__OVS_FLOW_ATTR_MAX];
Jesse Grossccb13522011-10-25 19:26:31 -07001345 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
Pravin B Shelarb637e492013-10-04 00:14:23 -07001346 struct table_instance *ti;
Jesse Grossccb13522011-10-25 19:26:31 -07001347 struct datapath *dp;
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001348 u32 ufid_flags;
1349 int err;
1350
1351 err = genlmsg_parse(cb->nlh, &dp_flow_genl_family, a,
Johannes Bergfceb6432017-04-12 14:34:07 +02001352 OVS_FLOW_ATTR_MAX, flow_policy, NULL);
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001353 if (err)
1354 return err;
1355 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
Jesse Grossccb13522011-10-25 19:26:31 -07001356
Pravin B Shelard57170b2013-07-30 15:39:39 -07001357 rcu_read_lock();
Andy Zhoucc3a5ae2014-09-08 13:14:22 -07001358 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001359 if (!dp) {
Pravin B Shelard57170b2013-07-30 15:39:39 -07001360 rcu_read_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001361 return -ENODEV;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001362 }
Jesse Grossccb13522011-10-25 19:26:31 -07001363
Pravin B Shelarb637e492013-10-04 00:14:23 -07001364 ti = rcu_dereference(dp->table.ti);
Jesse Grossccb13522011-10-25 19:26:31 -07001365 for (;;) {
1366 struct sw_flow *flow;
1367 u32 bucket, obj;
1368
1369 bucket = cb->args[0];
1370 obj = cb->args[1];
Pravin B Shelarb637e492013-10-04 00:14:23 -07001371 flow = ovs_flow_tbl_dump_next(ti, &bucket, &obj);
Jesse Grossccb13522011-10-25 19:26:31 -07001372 if (!flow)
1373 break;
1374
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -07001375 if (ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex, skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001376 NETLINK_CB(cb->skb).portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001377 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001378 OVS_FLOW_CMD_NEW, ufid_flags) < 0)
Jesse Grossccb13522011-10-25 19:26:31 -07001379 break;
1380
1381 cb->args[0] = bucket;
1382 cb->args[1] = obj;
1383 }
Pravin B Shelard57170b2013-07-30 15:39:39 -07001384 rcu_read_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001385 return skb->len;
1386}
1387
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001388static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
1389 [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001390 [OVS_FLOW_ATTR_MASK] = { .type = NLA_NESTED },
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001391 [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
1392 [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001393 [OVS_FLOW_ATTR_PROBE] = { .type = NLA_FLAG },
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001394 [OVS_FLOW_ATTR_UFID] = { .type = NLA_UNSPEC, .len = 1 },
1395 [OVS_FLOW_ATTR_UFID_FLAGS] = { .type = NLA_U32 },
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001396};
1397
stephen hemminger48e48a72014-07-16 11:25:52 -07001398static const struct genl_ops dp_flow_genl_ops[] = {
Jesse Grossccb13522011-10-25 19:26:31 -07001399 { .cmd = OVS_FLOW_CMD_NEW,
Tycho Andersen4a926022016-02-05 09:20:52 -07001400 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
Jesse Grossccb13522011-10-25 19:26:31 -07001401 .policy = flow_policy,
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001402 .doit = ovs_flow_cmd_new
Jesse Grossccb13522011-10-25 19:26:31 -07001403 },
1404 { .cmd = OVS_FLOW_CMD_DEL,
Tycho Andersen4a926022016-02-05 09:20:52 -07001405 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
Jesse Grossccb13522011-10-25 19:26:31 -07001406 .policy = flow_policy,
1407 .doit = ovs_flow_cmd_del
1408 },
1409 { .cmd = OVS_FLOW_CMD_GET,
1410 .flags = 0, /* OK for unprivileged users. */
1411 .policy = flow_policy,
1412 .doit = ovs_flow_cmd_get,
1413 .dumpit = ovs_flow_cmd_dump
1414 },
1415 { .cmd = OVS_FLOW_CMD_SET,
Tycho Andersen4a926022016-02-05 09:20:52 -07001416 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
Jesse Grossccb13522011-10-25 19:26:31 -07001417 .policy = flow_policy,
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001418 .doit = ovs_flow_cmd_set,
Jesse Grossccb13522011-10-25 19:26:31 -07001419 },
1420};
1421
Johannes Berg56989f62016-10-24 14:40:05 +02001422static struct genl_family dp_flow_genl_family __ro_after_init = {
Jesse Grossccb13522011-10-25 19:26:31 -07001423 .hdrsize = sizeof(struct ovs_header),
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001424 .name = OVS_FLOW_FAMILY,
1425 .version = OVS_FLOW_VERSION,
1426 .maxattr = OVS_FLOW_ATTR_MAX,
Pravin B Shelar3a4e0d62013-04-23 07:48:48 +00001427 .netnsok = true,
1428 .parallel_ops = true,
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001429 .ops = dp_flow_genl_ops,
1430 .n_ops = ARRAY_SIZE(dp_flow_genl_ops),
1431 .mcgrps = &ovs_dp_flow_multicast_group,
1432 .n_mcgrps = 1,
Johannes Berg489111e2016-10-24 14:40:03 +02001433 .module = THIS_MODULE,
Jesse Grossccb13522011-10-25 19:26:31 -07001434};
1435
Thomas Grafc3ff8cf2013-03-29 14:46:49 +01001436static size_t ovs_dp_cmd_msg_size(void)
1437{
1438 size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
1439
1440 msgsize += nla_total_size(IFNAMSIZ);
Nicolas Dichtel66c7a5e2016-04-26 10:06:15 +02001441 msgsize += nla_total_size_64bit(sizeof(struct ovs_dp_stats));
1442 msgsize += nla_total_size_64bit(sizeof(struct ovs_dp_megaflow_stats));
Daniele Di Proietto45fb9c32014-01-23 10:47:35 -08001443 msgsize += nla_total_size(sizeof(u32)); /* OVS_DP_ATTR_USER_FEATURES */
Thomas Grafc3ff8cf2013-03-29 14:46:49 +01001444
1445 return msgsize;
1446}
1447
Pravin B Shelar8ec609d2014-11-11 15:55:16 -08001448/* Called with ovs_mutex. */
Jesse Grossccb13522011-10-25 19:26:31 -07001449static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001450 u32 portid, u32 seq, u32 flags, u8 cmd)
Jesse Grossccb13522011-10-25 19:26:31 -07001451{
1452 struct ovs_header *ovs_header;
1453 struct ovs_dp_stats dp_stats;
Andy Zhou1bd71162013-10-22 10:42:46 -07001454 struct ovs_dp_megaflow_stats dp_megaflow_stats;
Jesse Grossccb13522011-10-25 19:26:31 -07001455 int err;
1456
Eric W. Biederman15e47302012-09-07 20:12:54 +00001457 ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
Jesse Grossccb13522011-10-25 19:26:31 -07001458 flags, cmd);
1459 if (!ovs_header)
1460 goto error;
1461
1462 ovs_header->dp_ifindex = get_dpifindex(dp);
1463
Jesse Grossccb13522011-10-25 19:26:31 -07001464 err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
Jesse Grossccb13522011-10-25 19:26:31 -07001465 if (err)
1466 goto nla_put_failure;
1467
Andy Zhou1bd71162013-10-22 10:42:46 -07001468 get_dp_stats(dp, &dp_stats, &dp_megaflow_stats);
Nicolas Dichtel66c7a5e2016-04-26 10:06:15 +02001469 if (nla_put_64bit(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats),
1470 &dp_stats, OVS_DP_ATTR_PAD))
Andy Zhou1bd71162013-10-22 10:42:46 -07001471 goto nla_put_failure;
1472
Nicolas Dichtel66c7a5e2016-04-26 10:06:15 +02001473 if (nla_put_64bit(skb, OVS_DP_ATTR_MEGAFLOW_STATS,
1474 sizeof(struct ovs_dp_megaflow_stats),
1475 &dp_megaflow_stats, OVS_DP_ATTR_PAD))
David S. Miller028d6a62012-03-29 23:20:48 -04001476 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001477
Thomas Graf43d4be92013-12-13 15:22:18 +01001478 if (nla_put_u32(skb, OVS_DP_ATTR_USER_FEATURES, dp->user_features))
1479 goto nla_put_failure;
1480
Johannes Berg053c0952015-01-16 22:09:00 +01001481 genlmsg_end(skb, ovs_header);
1482 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001483
1484nla_put_failure:
1485 genlmsg_cancel(skb, ovs_header);
1486error:
1487 return -EMSGSIZE;
1488}
1489
Florian Westphal263ea092016-02-18 15:03:26 +01001490static struct sk_buff *ovs_dp_cmd_alloc_info(void)
Jesse Grossccb13522011-10-25 19:26:31 -07001491{
Florian Westphal551ddc02016-02-18 15:03:25 +01001492 return genlmsg_new(ovs_dp_cmd_msg_size(), GFP_KERNEL);
Jesse Grossccb13522011-10-25 19:26:31 -07001493}
1494
Jarno Rajahalmebb6f9a72014-05-05 11:32:17 -07001495/* Called with rcu_read_lock or ovs_mutex. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001496static struct datapath *lookup_datapath(struct net *net,
Thomas Graf12eb18f2014-11-06 06:58:52 -08001497 const struct ovs_header *ovs_header,
Jesse Grossccb13522011-10-25 19:26:31 -07001498 struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1499{
1500 struct datapath *dp;
1501
1502 if (!a[OVS_DP_ATTR_NAME])
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001503 dp = get_dp(net, ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001504 else {
1505 struct vport *vport;
1506
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001507 vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
Jesse Grossccb13522011-10-25 19:26:31 -07001508 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
Jesse Grossccb13522011-10-25 19:26:31 -07001509 }
1510 return dp ? dp : ERR_PTR(-ENODEV);
1511}
1512
Thomas Graf44da5ae2013-12-13 15:22:19 +01001513static void ovs_dp_reset_user_features(struct sk_buff *skb, struct genl_info *info)
1514{
1515 struct datapath *dp;
1516
1517 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
Jiri Pirko3c7eacf2014-02-14 11:42:36 +01001518 if (IS_ERR(dp))
Thomas Graf44da5ae2013-12-13 15:22:19 +01001519 return;
1520
1521 WARN(dp->user_features, "Dropping previously announced user features\n");
1522 dp->user_features = 0;
1523}
1524
Thomas Graf12eb18f2014-11-06 06:58:52 -08001525static void ovs_dp_change(struct datapath *dp, struct nlattr *a[])
Thomas Graf43d4be92013-12-13 15:22:18 +01001526{
1527 if (a[OVS_DP_ATTR_USER_FEATURES])
1528 dp->user_features = nla_get_u32(a[OVS_DP_ATTR_USER_FEATURES]);
1529}
1530
Jesse Grossccb13522011-10-25 19:26:31 -07001531static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1532{
1533 struct nlattr **a = info->attrs;
1534 struct vport_parms parms;
1535 struct sk_buff *reply;
1536 struct datapath *dp;
1537 struct vport *vport;
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001538 struct ovs_net *ovs_net;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001539 int err, i;
Jesse Grossccb13522011-10-25 19:26:31 -07001540
1541 err = -EINVAL;
1542 if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1543 goto err;
1544
Florian Westphal263ea092016-02-18 15:03:26 +01001545 reply = ovs_dp_cmd_alloc_info();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001546 if (!reply)
1547 return -ENOMEM;
Jesse Grossccb13522011-10-25 19:26:31 -07001548
1549 err = -ENOMEM;
1550 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1551 if (dp == NULL)
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001552 goto err_free_reply;
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001553
Eric W. Biedermanefd7ef12015-03-11 23:04:08 -05001554 ovs_dp_set_net(dp, sock_net(skb->sk));
Jesse Grossccb13522011-10-25 19:26:31 -07001555
1556 /* Allocate table. */
Pravin B Shelarb637e492013-10-04 00:14:23 -07001557 err = ovs_flow_tbl_init(&dp->table);
1558 if (err)
Jesse Grossccb13522011-10-25 19:26:31 -07001559 goto err_free_dp;
1560
WANG Cong1c213bd2014-02-13 11:46:28 -08001561 dp->stats_percpu = netdev_alloc_pcpu_stats(struct dp_stats_percpu);
Jesse Grossccb13522011-10-25 19:26:31 -07001562 if (!dp->stats_percpu) {
1563 err = -ENOMEM;
1564 goto err_destroy_table;
1565 }
1566
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001567 dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
Pravin B Shelare6445712013-10-03 18:16:47 -07001568 GFP_KERNEL);
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001569 if (!dp->ports) {
1570 err = -ENOMEM;
1571 goto err_destroy_percpu;
1572 }
1573
1574 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1575 INIT_HLIST_HEAD(&dp->ports[i]);
1576
Andy Zhou96fbc132017-11-10 12:09:42 -08001577 err = ovs_meters_init(dp);
1578 if (err)
1579 goto err_destroy_ports_array;
1580
Jesse Grossccb13522011-10-25 19:26:31 -07001581 /* Set up our datapath device. */
1582 parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1583 parms.type = OVS_VPORT_TYPE_INTERNAL;
1584 parms.options = NULL;
1585 parms.dp = dp;
1586 parms.port_no = OVSP_LOCAL;
Alex Wang5cd667b2014-07-17 15:14:13 -07001587 parms.upcall_portids = a[OVS_DP_ATTR_UPCALL_PID];
Jesse Grossccb13522011-10-25 19:26:31 -07001588
Thomas Graf43d4be92013-12-13 15:22:18 +01001589 ovs_dp_change(dp, a);
1590
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001591 /* So far only local changes have been made, now need the lock. */
1592 ovs_lock();
1593
Jesse Grossccb13522011-10-25 19:26:31 -07001594 vport = new_vport(&parms);
1595 if (IS_ERR(vport)) {
1596 err = PTR_ERR(vport);
1597 if (err == -EBUSY)
1598 err = -EEXIST;
1599
Thomas Graf44da5ae2013-12-13 15:22:19 +01001600 if (err == -EEXIST) {
1601 /* An outdated user space instance that does not understand
1602 * the concept of user_features has attempted to create a new
1603 * datapath and is likely to reuse it. Drop all user features.
1604 */
1605 if (info->genlhdr->version < OVS_DP_VER_FEATURES)
1606 ovs_dp_reset_user_features(skb, info);
1607 }
1608
Andy Zhou96fbc132017-11-10 12:09:42 -08001609 goto err_destroy_meters;
Jesse Grossccb13522011-10-25 19:26:31 -07001610 }
1611
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001612 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1613 info->snd_seq, 0, OVS_DP_CMD_NEW);
1614 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001615
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001616 ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001617 list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001618
1619 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001620
Johannes Berg2a94fe42013-11-19 15:19:39 +01001621 ovs_notify(&dp_datapath_genl_family, reply, info);
Jesse Grossccb13522011-10-25 19:26:31 -07001622 return 0;
1623
Andy Zhou96fbc132017-11-10 12:09:42 -08001624err_destroy_meters:
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001625 ovs_unlock();
Andy Zhou96fbc132017-11-10 12:09:42 -08001626 ovs_meters_exit(dp);
1627err_destroy_ports_array:
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001628 kfree(dp->ports);
Jesse Grossccb13522011-10-25 19:26:31 -07001629err_destroy_percpu:
1630 free_percpu(dp->stats_percpu);
1631err_destroy_table:
Pravin B Shelar9b996e52014-05-06 18:41:20 -07001632 ovs_flow_tbl_destroy(&dp->table);
Jesse Grossccb13522011-10-25 19:26:31 -07001633err_free_dp:
1634 kfree(dp);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001635err_free_reply:
1636 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001637err:
1638 return err;
1639}
1640
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001641/* Called with ovs_mutex. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001642static void __dp_destroy(struct datapath *dp)
Jesse Grossccb13522011-10-25 19:26:31 -07001643{
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001644 int i;
Jesse Grossccb13522011-10-25 19:26:31 -07001645
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001646 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1647 struct vport *vport;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001648 struct hlist_node *n;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001649
Sasha Levinb67bfe02013-02-27 17:06:00 -08001650 hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001651 if (vport->port_no != OVSP_LOCAL)
1652 ovs_dp_detach_port(vport);
1653 }
Jesse Grossccb13522011-10-25 19:26:31 -07001654
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001655 list_del_rcu(&dp->list_node);
Jesse Grossccb13522011-10-25 19:26:31 -07001656
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001657 /* OVSP_LOCAL is datapath internal port. We need to make sure that
Andy Zhoue80857c2014-01-21 09:31:04 -08001658 * all ports in datapath are destroyed first before freeing datapath.
Jesse Grossccb13522011-10-25 19:26:31 -07001659 */
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001660 ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
Jesse Grossccb13522011-10-25 19:26:31 -07001661
Andy Zhoue80857c2014-01-21 09:31:04 -08001662 /* RCU destroy the flow table */
Jesse Grossccb13522011-10-25 19:26:31 -07001663 call_rcu(&dp->rcu, destroy_dp_rcu);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001664}
1665
1666static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1667{
1668 struct sk_buff *reply;
1669 struct datapath *dp;
1670 int err;
1671
Florian Westphal263ea092016-02-18 15:03:26 +01001672 reply = ovs_dp_cmd_alloc_info();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001673 if (!reply)
1674 return -ENOMEM;
1675
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001676 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001677 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1678 err = PTR_ERR(dp);
1679 if (IS_ERR(dp))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001680 goto err_unlock_free;
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001681
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001682 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1683 info->snd_seq, 0, OVS_DP_CMD_DEL);
1684 BUG_ON(err < 0);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001685
1686 __dp_destroy(dp);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001687 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001688
Johannes Berg2a94fe42013-11-19 15:19:39 +01001689 ovs_notify(&dp_datapath_genl_family, reply, info);
Jesse Grossccb13522011-10-25 19:26:31 -07001690
1691 return 0;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001692
1693err_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001694 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001695 kfree_skb(reply);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001696 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001697}
1698
1699static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1700{
1701 struct sk_buff *reply;
1702 struct datapath *dp;
1703 int err;
1704
Florian Westphal263ea092016-02-18 15:03:26 +01001705 reply = ovs_dp_cmd_alloc_info();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001706 if (!reply)
1707 return -ENOMEM;
1708
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001709 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001710 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001711 err = PTR_ERR(dp);
Jesse Grossccb13522011-10-25 19:26:31 -07001712 if (IS_ERR(dp))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001713 goto err_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001714
Thomas Graf43d4be92013-12-13 15:22:18 +01001715 ovs_dp_change(dp, info->attrs);
1716
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001717 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1718 info->snd_seq, 0, OVS_DP_CMD_NEW);
1719 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001720
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001721 ovs_unlock();
Johannes Berg2a94fe42013-11-19 15:19:39 +01001722 ovs_notify(&dp_datapath_genl_family, reply, info);
Jesse Grossccb13522011-10-25 19:26:31 -07001723
1724 return 0;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001725
1726err_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001727 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001728 kfree_skb(reply);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001729 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001730}
1731
1732static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1733{
1734 struct sk_buff *reply;
1735 struct datapath *dp;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001736 int err;
Jesse Grossccb13522011-10-25 19:26:31 -07001737
Florian Westphal263ea092016-02-18 15:03:26 +01001738 reply = ovs_dp_cmd_alloc_info();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001739 if (!reply)
1740 return -ENOMEM;
1741
Pravin B Shelar8ec609d2014-11-11 15:55:16 -08001742 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001743 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001744 if (IS_ERR(dp)) {
1745 err = PTR_ERR(dp);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001746 goto err_unlock_free;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001747 }
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001748 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1749 info->snd_seq, 0, OVS_DP_CMD_NEW);
1750 BUG_ON(err < 0);
Pravin B Shelar8ec609d2014-11-11 15:55:16 -08001751 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001752
Jesse Grossccb13522011-10-25 19:26:31 -07001753 return genlmsg_reply(reply, info);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001754
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001755err_unlock_free:
Pravin B Shelar8ec609d2014-11-11 15:55:16 -08001756 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001757 kfree_skb(reply);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001758 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001759}
1760
1761static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1762{
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001763 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
Jesse Grossccb13522011-10-25 19:26:31 -07001764 struct datapath *dp;
1765 int skip = cb->args[0];
1766 int i = 0;
1767
Pravin B Shelar8ec609d2014-11-11 15:55:16 -08001768 ovs_lock();
1769 list_for_each_entry(dp, &ovs_net->dps, list_node) {
Ben Pfaff77676fd2012-01-17 13:33:39 +00001770 if (i >= skip &&
Eric W. Biederman15e47302012-09-07 20:12:54 +00001771 ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001772 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1773 OVS_DP_CMD_NEW) < 0)
1774 break;
1775 i++;
1776 }
Pravin B Shelar8ec609d2014-11-11 15:55:16 -08001777 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001778
1779 cb->args[0] = i;
1780
1781 return skb->len;
1782}
1783
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001784static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1785 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1786 [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1787 [OVS_DP_ATTR_USER_FEATURES] = { .type = NLA_U32 },
1788};
1789
stephen hemminger48e48a72014-07-16 11:25:52 -07001790static const struct genl_ops dp_datapath_genl_ops[] = {
Jesse Grossccb13522011-10-25 19:26:31 -07001791 { .cmd = OVS_DP_CMD_NEW,
Tycho Andersen4a926022016-02-05 09:20:52 -07001792 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
Jesse Grossccb13522011-10-25 19:26:31 -07001793 .policy = datapath_policy,
1794 .doit = ovs_dp_cmd_new
1795 },
1796 { .cmd = OVS_DP_CMD_DEL,
Tycho Andersen4a926022016-02-05 09:20:52 -07001797 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
Jesse Grossccb13522011-10-25 19:26:31 -07001798 .policy = datapath_policy,
1799 .doit = ovs_dp_cmd_del
1800 },
1801 { .cmd = OVS_DP_CMD_GET,
1802 .flags = 0, /* OK for unprivileged users. */
1803 .policy = datapath_policy,
1804 .doit = ovs_dp_cmd_get,
1805 .dumpit = ovs_dp_cmd_dump
1806 },
1807 { .cmd = OVS_DP_CMD_SET,
Tycho Andersen4a926022016-02-05 09:20:52 -07001808 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
Jesse Grossccb13522011-10-25 19:26:31 -07001809 .policy = datapath_policy,
1810 .doit = ovs_dp_cmd_set,
1811 },
1812};
1813
Johannes Berg56989f62016-10-24 14:40:05 +02001814static struct genl_family dp_datapath_genl_family __ro_after_init = {
Jesse Grossccb13522011-10-25 19:26:31 -07001815 .hdrsize = sizeof(struct ovs_header),
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001816 .name = OVS_DATAPATH_FAMILY,
1817 .version = OVS_DATAPATH_VERSION,
1818 .maxattr = OVS_DP_ATTR_MAX,
Pravin B Shelar3a4e0d62013-04-23 07:48:48 +00001819 .netnsok = true,
1820 .parallel_ops = true,
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001821 .ops = dp_datapath_genl_ops,
1822 .n_ops = ARRAY_SIZE(dp_datapath_genl_ops),
1823 .mcgrps = &ovs_dp_datapath_multicast_group,
1824 .n_mcgrps = 1,
Johannes Berg489111e2016-10-24 14:40:03 +02001825 .module = THIS_MODULE,
Jesse Grossccb13522011-10-25 19:26:31 -07001826};
1827
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001828/* Called with ovs_mutex or RCU read lock. */
Jesse Grossccb13522011-10-25 19:26:31 -07001829static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
Jiri Benc9354d452017-11-02 17:04:37 -02001830 struct net *net, u32 portid, u32 seq,
1831 u32 flags, u8 cmd)
Jesse Grossccb13522011-10-25 19:26:31 -07001832{
1833 struct ovs_header *ovs_header;
1834 struct ovs_vport_stats vport_stats;
1835 int err;
1836
Eric W. Biederman15e47302012-09-07 20:12:54 +00001837 ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
Jesse Grossccb13522011-10-25 19:26:31 -07001838 flags, cmd);
1839 if (!ovs_header)
1840 return -EMSGSIZE;
1841
1842 ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1843
David S. Miller028d6a62012-03-29 23:20:48 -04001844 if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
1845 nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
Alex Wang5cd667b2014-07-17 15:14:13 -07001846 nla_put_string(skb, OVS_VPORT_ATTR_NAME,
Jiri Benc9354d452017-11-02 17:04:37 -02001847 ovs_vport_name(vport)) ||
1848 nla_put_u32(skb, OVS_VPORT_ATTR_IFINDEX, vport->dev->ifindex))
David S. Miller028d6a62012-03-29 23:20:48 -04001849 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001850
Jiri Benc9354d452017-11-02 17:04:37 -02001851 if (!net_eq(net, dev_net(vport->dev))) {
1852 int id = peernet2id_alloc(net, dev_net(vport->dev));
1853
1854 if (nla_put_s32(skb, OVS_VPORT_ATTR_NETNSID, id))
1855 goto nla_put_failure;
1856 }
1857
Jesse Grossccb13522011-10-25 19:26:31 -07001858 ovs_vport_get_stats(vport, &vport_stats);
Nicolas Dichtel66c7a5e2016-04-26 10:06:15 +02001859 if (nla_put_64bit(skb, OVS_VPORT_ATTR_STATS,
1860 sizeof(struct ovs_vport_stats), &vport_stats,
1861 OVS_VPORT_ATTR_PAD))
David S. Miller028d6a62012-03-29 23:20:48 -04001862 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001863
Alex Wang5cd667b2014-07-17 15:14:13 -07001864 if (ovs_vport_get_upcall_portids(vport, skb))
1865 goto nla_put_failure;
1866
Jesse Grossccb13522011-10-25 19:26:31 -07001867 err = ovs_vport_get_options(vport, skb);
1868 if (err == -EMSGSIZE)
1869 goto error;
1870
Johannes Berg053c0952015-01-16 22:09:00 +01001871 genlmsg_end(skb, ovs_header);
1872 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001873
1874nla_put_failure:
1875 err = -EMSGSIZE;
1876error:
1877 genlmsg_cancel(skb, ovs_header);
1878 return err;
1879}
1880
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001881static struct sk_buff *ovs_vport_cmd_alloc_info(void)
1882{
1883 return nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1884}
1885
1886/* Called with ovs_mutex, only via ovs_dp_notify_wq(). */
Jiri Benc9354d452017-11-02 17:04:37 -02001887struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, struct net *net,
1888 u32 portid, u32 seq, u8 cmd)
Jesse Grossccb13522011-10-25 19:26:31 -07001889{
1890 struct sk_buff *skb;
1891 int retval;
1892
1893 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1894 if (!skb)
1895 return ERR_PTR(-ENOMEM);
1896
Jiri Benc9354d452017-11-02 17:04:37 -02001897 retval = ovs_vport_cmd_fill_info(vport, skb, net, portid, seq, 0, cmd);
Jesse Grossa9341512013-03-26 15:48:38 -07001898 BUG_ON(retval < 0);
1899
Jesse Grossccb13522011-10-25 19:26:31 -07001900 return skb;
1901}
1902
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001903/* Called with ovs_mutex or RCU read lock. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001904static struct vport *lookup_vport(struct net *net,
Thomas Graf12eb18f2014-11-06 06:58:52 -08001905 const struct ovs_header *ovs_header,
Jesse Grossccb13522011-10-25 19:26:31 -07001906 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1907{
1908 struct datapath *dp;
1909 struct vport *vport;
1910
Jiri Benc9354d452017-11-02 17:04:37 -02001911 if (a[OVS_VPORT_ATTR_IFINDEX])
1912 return ERR_PTR(-EOPNOTSUPP);
Jesse Grossccb13522011-10-25 19:26:31 -07001913 if (a[OVS_VPORT_ATTR_NAME]) {
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001914 vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
Jesse Grossccb13522011-10-25 19:26:31 -07001915 if (!vport)
1916 return ERR_PTR(-ENODEV);
Ben Pfaff651a68e2012-03-06 15:04:04 -08001917 if (ovs_header->dp_ifindex &&
1918 ovs_header->dp_ifindex != get_dpifindex(vport->dp))
1919 return ERR_PTR(-ENODEV);
Jesse Grossccb13522011-10-25 19:26:31 -07001920 return vport;
1921 } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
1922 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1923
1924 if (port_no >= DP_MAX_PORTS)
1925 return ERR_PTR(-EFBIG);
1926
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001927 dp = get_dp(net, ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001928 if (!dp)
1929 return ERR_PTR(-ENODEV);
1930
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001931 vport = ovs_vport_ovsl_rcu(dp, port_no);
Jesse Grossccb13522011-10-25 19:26:31 -07001932 if (!vport)
Jarno Rajahalme14408db2013-01-09 14:27:35 -08001933 return ERR_PTR(-ENODEV);
Jesse Grossccb13522011-10-25 19:26:31 -07001934 return vport;
1935 } else
1936 return ERR_PTR(-EINVAL);
Jiri Benc9354d452017-11-02 17:04:37 -02001937
Jesse Grossccb13522011-10-25 19:26:31 -07001938}
1939
Paolo Abeni3a927bc2016-02-26 10:45:39 +01001940/* Called with ovs_mutex */
1941static void update_headroom(struct datapath *dp)
1942{
1943 unsigned dev_headroom, max_headroom = 0;
1944 struct net_device *dev;
1945 struct vport *vport;
1946 int i;
1947
1948 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1949 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
1950 dev = vport->dev;
1951 dev_headroom = netdev_get_fwd_headroom(dev);
1952 if (dev_headroom > max_headroom)
1953 max_headroom = dev_headroom;
1954 }
1955 }
1956
1957 dp->max_headroom = max_headroom;
1958 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1959 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node)
1960 netdev_set_rx_headroom(vport->dev, max_headroom);
1961}
1962
Jesse Grossccb13522011-10-25 19:26:31 -07001963static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
1964{
1965 struct nlattr **a = info->attrs;
1966 struct ovs_header *ovs_header = info->userhdr;
1967 struct vport_parms parms;
1968 struct sk_buff *reply;
1969 struct vport *vport;
1970 struct datapath *dp;
1971 u32 port_no;
1972 int err;
1973
Jesse Grossccb13522011-10-25 19:26:31 -07001974 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
1975 !a[OVS_VPORT_ATTR_UPCALL_PID])
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001976 return -EINVAL;
Jiri Benc9354d452017-11-02 17:04:37 -02001977 if (a[OVS_VPORT_ATTR_IFINDEX])
1978 return -EOPNOTSUPP;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001979
1980 port_no = a[OVS_VPORT_ATTR_PORT_NO]
1981 ? nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]) : 0;
1982 if (port_no >= DP_MAX_PORTS)
1983 return -EFBIG;
1984
1985 reply = ovs_vport_cmd_alloc_info();
1986 if (!reply)
1987 return -ENOMEM;
Jesse Grossccb13522011-10-25 19:26:31 -07001988
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001989 ovs_lock();
Thomas Graf62b9c8d2014-10-22 17:29:06 +02001990restart:
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001991 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001992 err = -ENODEV;
1993 if (!dp)
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001994 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001995
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001996 if (port_no) {
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001997 vport = ovs_vport_ovsl(dp, port_no);
Jesse Grossccb13522011-10-25 19:26:31 -07001998 err = -EBUSY;
1999 if (vport)
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07002000 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07002001 } else {
2002 for (port_no = 1; ; port_no++) {
2003 if (port_no >= DP_MAX_PORTS) {
2004 err = -EFBIG;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07002005 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07002006 }
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002007 vport = ovs_vport_ovsl(dp, port_no);
Jesse Grossccb13522011-10-25 19:26:31 -07002008 if (!vport)
2009 break;
2010 }
2011 }
2012
2013 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
2014 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
2015 parms.options = a[OVS_VPORT_ATTR_OPTIONS];
2016 parms.dp = dp;
2017 parms.port_no = port_no;
Alex Wang5cd667b2014-07-17 15:14:13 -07002018 parms.upcall_portids = a[OVS_VPORT_ATTR_UPCALL_PID];
Jesse Grossccb13522011-10-25 19:26:31 -07002019
2020 vport = new_vport(&parms);
2021 err = PTR_ERR(vport);
Thomas Graf62b9c8d2014-10-22 17:29:06 +02002022 if (IS_ERR(vport)) {
2023 if (err == -EAGAIN)
2024 goto restart;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07002025 goto exit_unlock_free;
Thomas Graf62b9c8d2014-10-22 17:29:06 +02002026 }
Jesse Grossccb13522011-10-25 19:26:31 -07002027
Jiri Benc9354d452017-11-02 17:04:37 -02002028 err = ovs_vport_cmd_fill_info(vport, reply, genl_info_net(info),
2029 info->snd_portid, info->snd_seq, 0,
2030 OVS_VPORT_CMD_NEW);
Paolo Abeni3a927bc2016-02-26 10:45:39 +01002031
2032 if (netdev_get_fwd_headroom(vport->dev) > dp->max_headroom)
2033 update_headroom(dp);
2034 else
2035 netdev_set_rx_headroom(vport->dev, dp->max_headroom);
2036
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07002037 BUG_ON(err < 0);
2038 ovs_unlock();
Thomas Grafed661182013-03-29 14:46:50 +01002039
Johannes Berg2a94fe42013-11-19 15:19:39 +01002040 ovs_notify(&dp_vport_genl_family, reply, info);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07002041 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -07002042
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07002043exit_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002044 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07002045 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07002046 return err;
2047}
2048
2049static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
2050{
2051 struct nlattr **a = info->attrs;
2052 struct sk_buff *reply;
2053 struct vport *vport;
2054 int err;
2055
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07002056 reply = ovs_vport_cmd_alloc_info();
2057 if (!reply)
2058 return -ENOMEM;
2059
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002060 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002061 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
Jesse Grossccb13522011-10-25 19:26:31 -07002062 err = PTR_ERR(vport);
2063 if (IS_ERR(vport))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07002064 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07002065
Jesse Grossccb13522011-10-25 19:26:31 -07002066 if (a[OVS_VPORT_ATTR_TYPE] &&
Jesse Grossf44f3402013-05-13 08:15:26 -07002067 nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
Jesse Grossccb13522011-10-25 19:26:31 -07002068 err = -EINVAL;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07002069 goto exit_unlock_free;
Jesse Grossa9341512013-03-26 15:48:38 -07002070 }
2071
Jesse Grossf44f3402013-05-13 08:15:26 -07002072 if (a[OVS_VPORT_ATTR_OPTIONS]) {
Jesse Grossccb13522011-10-25 19:26:31 -07002073 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
Jesse Grossf44f3402013-05-13 08:15:26 -07002074 if (err)
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07002075 goto exit_unlock_free;
Jesse Grossf44f3402013-05-13 08:15:26 -07002076 }
Jesse Grossa9341512013-03-26 15:48:38 -07002077
Alex Wang5cd667b2014-07-17 15:14:13 -07002078
2079 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
2080 struct nlattr *ids = a[OVS_VPORT_ATTR_UPCALL_PID];
2081
2082 err = ovs_vport_set_upcall_portids(vport, ids);
2083 if (err)
2084 goto exit_unlock_free;
2085 }
Jesse Grossccb13522011-10-25 19:26:31 -07002086
Jiri Benc9354d452017-11-02 17:04:37 -02002087 err = ovs_vport_cmd_fill_info(vport, reply, genl_info_net(info),
2088 info->snd_portid, info->snd_seq, 0,
2089 OVS_VPORT_CMD_NEW);
Jesse Grossa9341512013-03-26 15:48:38 -07002090 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07002091
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002092 ovs_unlock();
Johannes Berg2a94fe42013-11-19 15:19:39 +01002093 ovs_notify(&dp_vport_genl_family, reply, info);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002094 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -07002095
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07002096exit_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002097 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07002098 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07002099 return err;
2100}
2101
2102static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
2103{
Paolo Abeni3a927bc2016-02-26 10:45:39 +01002104 bool must_update_headroom = false;
Jesse Grossccb13522011-10-25 19:26:31 -07002105 struct nlattr **a = info->attrs;
2106 struct sk_buff *reply;
Paolo Abeni3a927bc2016-02-26 10:45:39 +01002107 struct datapath *dp;
Jesse Grossccb13522011-10-25 19:26:31 -07002108 struct vport *vport;
2109 int err;
2110
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07002111 reply = ovs_vport_cmd_alloc_info();
2112 if (!reply)
2113 return -ENOMEM;
2114
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002115 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002116 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
Jesse Grossccb13522011-10-25 19:26:31 -07002117 err = PTR_ERR(vport);
2118 if (IS_ERR(vport))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07002119 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07002120
2121 if (vport->port_no == OVSP_LOCAL) {
2122 err = -EINVAL;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07002123 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07002124 }
2125
Jiri Benc9354d452017-11-02 17:04:37 -02002126 err = ovs_vport_cmd_fill_info(vport, reply, genl_info_net(info),
2127 info->snd_portid, info->snd_seq, 0,
2128 OVS_VPORT_CMD_DEL);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07002129 BUG_ON(err < 0);
Paolo Abeni3a927bc2016-02-26 10:45:39 +01002130
2131 /* the vport deletion may trigger dp headroom update */
2132 dp = vport->dp;
2133 if (netdev_get_fwd_headroom(vport->dev) == dp->max_headroom)
2134 must_update_headroom = true;
2135 netdev_reset_rx_headroom(vport->dev);
Jesse Grossccb13522011-10-25 19:26:31 -07002136 ovs_dp_detach_port(vport);
Paolo Abeni3a927bc2016-02-26 10:45:39 +01002137
2138 if (must_update_headroom)
2139 update_headroom(dp);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07002140 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07002141
Johannes Berg2a94fe42013-11-19 15:19:39 +01002142 ovs_notify(&dp_vport_genl_family, reply, info);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07002143 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -07002144
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07002145exit_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002146 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07002147 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07002148 return err;
2149}
2150
2151static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
2152{
2153 struct nlattr **a = info->attrs;
2154 struct ovs_header *ovs_header = info->userhdr;
2155 struct sk_buff *reply;
2156 struct vport *vport;
2157 int err;
2158
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07002159 reply = ovs_vport_cmd_alloc_info();
2160 if (!reply)
2161 return -ENOMEM;
2162
Jesse Grossccb13522011-10-25 19:26:31 -07002163 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002164 vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
Jesse Grossccb13522011-10-25 19:26:31 -07002165 err = PTR_ERR(vport);
2166 if (IS_ERR(vport))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07002167 goto exit_unlock_free;
Jiri Benc9354d452017-11-02 17:04:37 -02002168 err = ovs_vport_cmd_fill_info(vport, reply, genl_info_net(info),
2169 info->snd_portid, info->snd_seq, 0,
2170 OVS_VPORT_CMD_NEW);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07002171 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07002172 rcu_read_unlock();
2173
2174 return genlmsg_reply(reply, info);
2175
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07002176exit_unlock_free:
Jesse Grossccb13522011-10-25 19:26:31 -07002177 rcu_read_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07002178 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07002179 return err;
2180}
2181
2182static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
2183{
2184 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
2185 struct datapath *dp;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07002186 int bucket = cb->args[0], skip = cb->args[1];
2187 int i, j = 0;
Jesse Grossccb13522011-10-25 19:26:31 -07002188
Jesse Grossccb13522011-10-25 19:26:31 -07002189 rcu_read_lock();
Andy Zhoucc3a5ae2014-09-08 13:14:22 -07002190 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
Jarno Rajahalme42ee19e2014-02-15 17:42:29 -08002191 if (!dp) {
2192 rcu_read_unlock();
2193 return -ENODEV;
2194 }
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07002195 for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
Jesse Grossccb13522011-10-25 19:26:31 -07002196 struct vport *vport;
2197
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07002198 j = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -08002199 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07002200 if (j >= skip &&
2201 ovs_vport_cmd_fill_info(vport, skb,
Jiri Benc9354d452017-11-02 17:04:37 -02002202 sock_net(skb->sk),
Eric W. Biederman15e47302012-09-07 20:12:54 +00002203 NETLINK_CB(cb->skb).portid,
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07002204 cb->nlh->nlmsg_seq,
2205 NLM_F_MULTI,
2206 OVS_VPORT_CMD_NEW) < 0)
2207 goto out;
Jesse Grossccb13522011-10-25 19:26:31 -07002208
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07002209 j++;
2210 }
2211 skip = 0;
Jesse Grossccb13522011-10-25 19:26:31 -07002212 }
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07002213out:
Jesse Grossccb13522011-10-25 19:26:31 -07002214 rcu_read_unlock();
2215
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07002216 cb->args[0] = i;
2217 cb->args[1] = j;
Jesse Grossccb13522011-10-25 19:26:31 -07002218
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07002219 return skb->len;
Jesse Grossccb13522011-10-25 19:26:31 -07002220}
2221
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002222static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
2223 [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
2224 [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
2225 [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
2226 [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
2227 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
2228 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
Jiri Benc9354d452017-11-02 17:04:37 -02002229 [OVS_VPORT_ATTR_IFINDEX] = { .type = NLA_U32 },
2230 [OVS_VPORT_ATTR_NETNSID] = { .type = NLA_S32 },
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002231};
2232
stephen hemminger48e48a72014-07-16 11:25:52 -07002233static const struct genl_ops dp_vport_genl_ops[] = {
Jesse Grossccb13522011-10-25 19:26:31 -07002234 { .cmd = OVS_VPORT_CMD_NEW,
Tycho Andersen4a926022016-02-05 09:20:52 -07002235 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
Jesse Grossccb13522011-10-25 19:26:31 -07002236 .policy = vport_policy,
2237 .doit = ovs_vport_cmd_new
2238 },
2239 { .cmd = OVS_VPORT_CMD_DEL,
Tycho Andersen4a926022016-02-05 09:20:52 -07002240 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
Jesse Grossccb13522011-10-25 19:26:31 -07002241 .policy = vport_policy,
2242 .doit = ovs_vport_cmd_del
2243 },
2244 { .cmd = OVS_VPORT_CMD_GET,
2245 .flags = 0, /* OK for unprivileged users. */
2246 .policy = vport_policy,
2247 .doit = ovs_vport_cmd_get,
2248 .dumpit = ovs_vport_cmd_dump
2249 },
2250 { .cmd = OVS_VPORT_CMD_SET,
Tycho Andersen4a926022016-02-05 09:20:52 -07002251 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
Jesse Grossccb13522011-10-25 19:26:31 -07002252 .policy = vport_policy,
2253 .doit = ovs_vport_cmd_set,
2254 },
2255};
2256
Johannes Berg56989f62016-10-24 14:40:05 +02002257struct genl_family dp_vport_genl_family __ro_after_init = {
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002258 .hdrsize = sizeof(struct ovs_header),
2259 .name = OVS_VPORT_FAMILY,
2260 .version = OVS_VPORT_VERSION,
2261 .maxattr = OVS_VPORT_ATTR_MAX,
2262 .netnsok = true,
2263 .parallel_ops = true,
2264 .ops = dp_vport_genl_ops,
2265 .n_ops = ARRAY_SIZE(dp_vport_genl_ops),
2266 .mcgrps = &ovs_dp_vport_multicast_group,
2267 .n_mcgrps = 1,
Johannes Berg489111e2016-10-24 14:40:03 +02002268 .module = THIS_MODULE,
Jesse Grossccb13522011-10-25 19:26:31 -07002269};
2270
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002271static struct genl_family * const dp_genl_families[] = {
2272 &dp_datapath_genl_family,
2273 &dp_vport_genl_family,
2274 &dp_flow_genl_family,
2275 &dp_packet_genl_family,
Andy Zhou96fbc132017-11-10 12:09:42 -08002276 &dp_meter_genl_family,
Jesse Grossccb13522011-10-25 19:26:31 -07002277};
2278
2279static void dp_unregister_genl(int n_families)
2280{
2281 int i;
2282
2283 for (i = 0; i < n_families; i++)
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002284 genl_unregister_family(dp_genl_families[i]);
Jesse Grossccb13522011-10-25 19:26:31 -07002285}
2286
Johannes Berg56989f62016-10-24 14:40:05 +02002287static int __init dp_register_genl(void)
Jesse Grossccb13522011-10-25 19:26:31 -07002288{
Jesse Grossccb13522011-10-25 19:26:31 -07002289 int err;
2290 int i;
2291
Jesse Grossccb13522011-10-25 19:26:31 -07002292 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
Jesse Grossccb13522011-10-25 19:26:31 -07002293
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002294 err = genl_register_family(dp_genl_families[i]);
Jesse Grossccb13522011-10-25 19:26:31 -07002295 if (err)
2296 goto error;
Jesse Grossccb13522011-10-25 19:26:31 -07002297 }
2298
2299 return 0;
2300
2301error:
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002302 dp_unregister_genl(i);
Jesse Grossccb13522011-10-25 19:26:31 -07002303 return err;
2304}
2305
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002306static int __net_init ovs_init_net(struct net *net)
2307{
2308 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2309
2310 INIT_LIST_HEAD(&ovs_net->dps);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002311 INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
Joe Stringerc2ac6672015-08-26 11:31:52 -07002312 ovs_ct_init(net);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002313 return 0;
2314}
2315
Pravin B Shelar7b4577a2015-02-17 11:23:10 -08002316static void __net_exit list_vports_from_net(struct net *net, struct net *dnet,
2317 struct list_head *head)
2318{
2319 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2320 struct datapath *dp;
2321
2322 list_for_each_entry(dp, &ovs_net->dps, list_node) {
2323 int i;
2324
2325 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
2326 struct vport *vport;
2327
2328 hlist_for_each_entry(vport, &dp->ports[i], dp_hash_node) {
Pravin B Shelar7b4577a2015-02-17 11:23:10 -08002329 if (vport->ops->type != OVS_VPORT_TYPE_INTERNAL)
2330 continue;
2331
Thomas Grafbe4ace62015-07-21 10:44:04 +02002332 if (dev_net(vport->dev) == dnet)
Pravin B Shelar7b4577a2015-02-17 11:23:10 -08002333 list_add(&vport->detach_list, head);
2334 }
2335 }
2336 }
2337}
2338
2339static void __net_exit ovs_exit_net(struct net *dnet)
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002340{
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002341 struct datapath *dp, *dp_next;
Pravin B Shelar7b4577a2015-02-17 11:23:10 -08002342 struct ovs_net *ovs_net = net_generic(dnet, ovs_net_id);
2343 struct vport *vport, *vport_next;
2344 struct net *net;
2345 LIST_HEAD(head);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002346
Joe Stringerc2ac6672015-08-26 11:31:52 -07002347 ovs_ct_exit(dnet);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002348 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002349 list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
2350 __dp_destroy(dp);
Pravin B Shelar7b4577a2015-02-17 11:23:10 -08002351
2352 rtnl_lock();
2353 for_each_net(net)
2354 list_vports_from_net(net, dnet, &head);
2355 rtnl_unlock();
2356
2357 /* Detach all vports from given namespace. */
2358 list_for_each_entry_safe(vport, vport_next, &head, detach_list) {
2359 list_del(&vport->detach_list);
2360 ovs_dp_detach_port(vport);
2361 }
2362
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002363 ovs_unlock();
2364
2365 cancel_work_sync(&ovs_net->dp_notify_work);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002366}
2367
2368static struct pernet_operations ovs_net_ops = {
2369 .init = ovs_init_net,
2370 .exit = ovs_exit_net,
2371 .id = &ovs_net_id,
2372 .size = sizeof(struct ovs_net),
2373};
2374
Jesse Grossccb13522011-10-25 19:26:31 -07002375static int __init dp_init(void)
2376{
Jesse Grossccb13522011-10-25 19:26:31 -07002377 int err;
2378
YOSHIFUJI Hideaki / 吉藤英明3523b292013-01-09 07:19:55 +00002379 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
Jesse Grossccb13522011-10-25 19:26:31 -07002380
2381 pr_info("Open vSwitch switching datapath\n");
2382
Andy Zhou971427f32014-09-15 19:37:25 -07002383 err = action_fifos_init();
Jesse Grossccb13522011-10-25 19:26:31 -07002384 if (err)
2385 goto error;
2386
Andy Zhou971427f32014-09-15 19:37:25 -07002387 err = ovs_internal_dev_rtnl_link_register();
2388 if (err)
2389 goto error_action_fifos_exit;
2390
Jiri Pirko5b9e7e12014-06-26 09:58:26 +02002391 err = ovs_flow_init();
2392 if (err)
2393 goto error_unreg_rtnl_link;
2394
Jesse Grossccb13522011-10-25 19:26:31 -07002395 err = ovs_vport_init();
2396 if (err)
2397 goto error_flow_exit;
2398
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002399 err = register_pernet_device(&ovs_net_ops);
Jesse Grossccb13522011-10-25 19:26:31 -07002400 if (err)
2401 goto error_vport_exit;
2402
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002403 err = register_netdevice_notifier(&ovs_dp_device_notifier);
2404 if (err)
2405 goto error_netns_exit;
2406
Thomas Graf62b9c8d2014-10-22 17:29:06 +02002407 err = ovs_netdev_init();
2408 if (err)
2409 goto error_unreg_notifier;
2410
Jesse Grossccb13522011-10-25 19:26:31 -07002411 err = dp_register_genl();
2412 if (err < 0)
Thomas Graf62b9c8d2014-10-22 17:29:06 +02002413 goto error_unreg_netdev;
Jesse Grossccb13522011-10-25 19:26:31 -07002414
Jesse Grossccb13522011-10-25 19:26:31 -07002415 return 0;
2416
Thomas Graf62b9c8d2014-10-22 17:29:06 +02002417error_unreg_netdev:
2418 ovs_netdev_exit();
Jesse Grossccb13522011-10-25 19:26:31 -07002419error_unreg_notifier:
2420 unregister_netdevice_notifier(&ovs_dp_device_notifier);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002421error_netns_exit:
2422 unregister_pernet_device(&ovs_net_ops);
Jesse Grossccb13522011-10-25 19:26:31 -07002423error_vport_exit:
2424 ovs_vport_exit();
2425error_flow_exit:
2426 ovs_flow_exit();
Jiri Pirko5b9e7e12014-06-26 09:58:26 +02002427error_unreg_rtnl_link:
2428 ovs_internal_dev_rtnl_link_unregister();
Andy Zhou971427f32014-09-15 19:37:25 -07002429error_action_fifos_exit:
2430 action_fifos_exit();
Jesse Grossccb13522011-10-25 19:26:31 -07002431error:
2432 return err;
2433}
2434
2435static void dp_cleanup(void)
2436{
Jesse Grossccb13522011-10-25 19:26:31 -07002437 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
Thomas Graf62b9c8d2014-10-22 17:29:06 +02002438 ovs_netdev_exit();
Jesse Grossccb13522011-10-25 19:26:31 -07002439 unregister_netdevice_notifier(&ovs_dp_device_notifier);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002440 unregister_pernet_device(&ovs_net_ops);
2441 rcu_barrier();
Jesse Grossccb13522011-10-25 19:26:31 -07002442 ovs_vport_exit();
2443 ovs_flow_exit();
Jiri Pirko5b9e7e12014-06-26 09:58:26 +02002444 ovs_internal_dev_rtnl_link_unregister();
Andy Zhou971427f32014-09-15 19:37:25 -07002445 action_fifos_exit();
Jesse Grossccb13522011-10-25 19:26:31 -07002446}
2447
2448module_init(dp_init);
2449module_exit(dp_cleanup);
2450
2451MODULE_DESCRIPTION("Open vSwitch switching datapath");
2452MODULE_LICENSE("GPL");
Thadeu Lima de Souza Cascardoed227092016-09-09 17:42:30 -03002453MODULE_ALIAS_GENL_FAMILY(OVS_DATAPATH_FAMILY);
2454MODULE_ALIAS_GENL_FAMILY(OVS_VPORT_FAMILY);
2455MODULE_ALIAS_GENL_FAMILY(OVS_FLOW_FAMILY);
2456MODULE_ALIAS_GENL_FAMILY(OVS_PACKET_FAMILY);
Andy Zhou96fbc132017-11-10 12:09:42 -08002457MODULE_ALIAS_GENL_FAMILY(OVS_METER_FAMILY);