blob: 892287d06c1768d3f16b029859f344c560984ff1 [file] [log] [blame]
Thomas Gleixnerc9422992019-05-29 07:12:43 -07001// SPDX-License-Identifier: GPL-2.0-only
Jesse Grossccb13522011-10-25 19:26:31 -07002/*
Ben Pfaffad552002014-05-06 16:48:38 -07003 * Copyright (c) 2007-2014 Nicira, Inc.
Jesse Grossccb13522011-10-25 19:26:31 -07004 */
5
6#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7
8#include <linux/init.h>
9#include <linux/module.h>
10#include <linux/if_arp.h>
11#include <linux/if_vlan.h>
12#include <linux/in.h>
13#include <linux/ip.h>
14#include <linux/jhash.h>
15#include <linux/delay.h>
16#include <linux/time.h>
17#include <linux/etherdevice.h>
18#include <linux/genetlink.h>
19#include <linux/kernel.h>
20#include <linux/kthread.h>
21#include <linux/mutex.h>
22#include <linux/percpu.h>
23#include <linux/rcupdate.h>
24#include <linux/tcp.h>
25#include <linux/udp.h>
Jesse Grossccb13522011-10-25 19:26:31 -070026#include <linux/ethtool.h>
27#include <linux/wait.h>
Jesse Grossccb13522011-10-25 19:26:31 -070028#include <asm/div64.h>
29#include <linux/highmem.h>
30#include <linux/netfilter_bridge.h>
31#include <linux/netfilter_ipv4.h>
32#include <linux/inetdevice.h>
33#include <linux/list.h>
34#include <linux/openvswitch.h>
35#include <linux/rculist.h>
36#include <linux/dmi.h>
Jesse Grossccb13522011-10-25 19:26:31 -070037#include <net/genetlink.h>
Pravin B Shelar46df7b82012-02-22 19:58:59 -080038#include <net/net_namespace.h>
39#include <net/netns/generic.h>
Jesse Grossccb13522011-10-25 19:26:31 -070040
41#include "datapath.h"
42#include "flow.h"
Andy Zhoue80857c2014-01-21 09:31:04 -080043#include "flow_table.h"
Pravin B Shelare6445712013-10-03 18:16:47 -070044#include "flow_netlink.h"
Andy Zhou96fbc132017-11-10 12:09:42 -080045#include "meter.h"
Jesse Grossccb13522011-10-25 19:26:31 -070046#include "vport-internal_dev.h"
Thomas Grafcff63a52013-04-29 13:06:41 +000047#include "vport-netdev.h"
Jesse Grossccb13522011-10-25 19:26:31 -070048
Alexey Dobriyanc7d03a02016-11-17 04:58:21 +030049unsigned int ovs_net_id __read_mostly;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -070050
Pravin B Shelar0c200ef2014-05-06 16:44:50 -070051static struct genl_family dp_packet_genl_family;
52static struct genl_family dp_flow_genl_family;
53static struct genl_family dp_datapath_genl_family;
54
Joe Stringer74ed7ab2015-01-21 16:42:52 -080055static const struct nla_policy flow_policy[];
56
stephen hemminger48e48a72014-07-16 11:25:52 -070057static const struct genl_multicast_group ovs_dp_flow_multicast_group = {
58 .name = OVS_FLOW_MCGROUP,
Pravin B Shelar0c200ef2014-05-06 16:44:50 -070059};
60
stephen hemminger48e48a72014-07-16 11:25:52 -070061static const struct genl_multicast_group ovs_dp_datapath_multicast_group = {
62 .name = OVS_DATAPATH_MCGROUP,
Pravin B Shelar0c200ef2014-05-06 16:44:50 -070063};
64
stephen hemminger48e48a72014-07-16 11:25:52 -070065static const struct genl_multicast_group ovs_dp_vport_multicast_group = {
66 .name = OVS_VPORT_MCGROUP,
Pravin B Shelar0c200ef2014-05-06 16:44:50 -070067};
68
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -070069/* Check if need to build a reply message.
70 * OVS userspace sets the NLM_F_ECHO flag if it needs the reply. */
Samuel Gauthier9b67aa42014-09-18 10:31:04 +020071static bool ovs_must_notify(struct genl_family *family, struct genl_info *info,
72 unsigned int group)
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -070073{
74 return info->nlhdr->nlmsg_flags & NLM_F_ECHO ||
Johannes Bergf8403a22014-12-22 18:56:36 +010075 genl_has_listeners(family, genl_info_net(info), group);
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -070076}
77
Johannes Berg68eb5502013-11-19 15:19:38 +010078static void ovs_notify(struct genl_family *family,
Johannes Berg2a94fe42013-11-19 15:19:39 +010079 struct sk_buff *skb, struct genl_info *info)
Thomas Grafed661182013-03-29 14:46:50 +010080{
Jiri Benc92c14d92015-09-22 18:56:43 +020081 genl_notify(family, skb, info, 0, GFP_KERNEL);
Thomas Grafed661182013-03-29 14:46:50 +010082}
83
Pravin B Shelar46df7b82012-02-22 19:58:59 -080084/**
Jesse Grossccb13522011-10-25 19:26:31 -070085 * DOC: Locking:
86 *
Pravin B Shelar8e4e1712013-04-15 13:23:03 -070087 * All writes e.g. Writes to device state (add/remove datapath, port, set
88 * operations on vports, etc.), Writes to other state (flow table
89 * modifications, set miscellaneous datapath parameters, etc.) are protected
90 * by ovs_lock.
Jesse Grossccb13522011-10-25 19:26:31 -070091 *
92 * Reads are protected by RCU.
93 *
94 * There are a few special cases (mostly stats) that have their own
95 * synchronization but they nest under all of above and don't interact with
96 * each other.
Pravin B Shelar8e4e1712013-04-15 13:23:03 -070097 *
98 * The RTNL lock nests inside ovs_mutex.
Jesse Grossccb13522011-10-25 19:26:31 -070099 */
100
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700101static DEFINE_MUTEX(ovs_mutex);
102
103void ovs_lock(void)
104{
105 mutex_lock(&ovs_mutex);
106}
107
108void ovs_unlock(void)
109{
110 mutex_unlock(&ovs_mutex);
111}
112
113#ifdef CONFIG_LOCKDEP
114int lockdep_ovsl_is_held(void)
115{
116 if (debug_locks)
117 return lockdep_is_held(&ovs_mutex);
118 else
119 return 1;
120}
121#endif
122
Jesse Grossccb13522011-10-25 19:26:31 -0700123static struct vport *new_vport(const struct vport_parms *);
Thomas Graf8055a892013-12-13 15:22:20 +0100124static int queue_gso_packets(struct datapath *dp, struct sk_buff *,
Pravin B Shelare8eedb82014-11-06 06:57:27 -0800125 const struct sw_flow_key *,
William Tuf2a4d082016-06-10 11:49:33 -0700126 const struct dp_upcall_info *,
127 uint32_t cutlen);
Thomas Graf8055a892013-12-13 15:22:20 +0100128static int queue_userspace_packet(struct datapath *dp, struct sk_buff *,
Pravin B Shelare8eedb82014-11-06 06:57:27 -0800129 const struct sw_flow_key *,
William Tuf2a4d082016-06-10 11:49:33 -0700130 const struct dp_upcall_info *,
131 uint32_t cutlen);
Jesse Grossccb13522011-10-25 19:26:31 -0700132
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700133/* Must be called with rcu_read_lock or ovs_mutex. */
Andy Zhou971427f32014-09-15 19:37:25 -0700134const char *ovs_dp_name(const struct datapath *dp)
Jesse Grossccb13522011-10-25 19:26:31 -0700135{
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700136 struct vport *vport = ovs_vport_ovsl_rcu(dp, OVSP_LOCAL);
Thomas Grafc9db9652015-07-21 10:44:05 +0200137 return ovs_vport_name(vport);
Jesse Grossccb13522011-10-25 19:26:31 -0700138}
139
Thomas Graf12eb18f2014-11-06 06:58:52 -0800140static int get_dpifindex(const struct datapath *dp)
Jesse Grossccb13522011-10-25 19:26:31 -0700141{
142 struct vport *local;
143 int ifindex;
144
145 rcu_read_lock();
146
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700147 local = ovs_vport_rcu(dp, OVSP_LOCAL);
Jesse Grossccb13522011-10-25 19:26:31 -0700148 if (local)
Thomas Grafbe4ace62015-07-21 10:44:04 +0200149 ifindex = local->dev->ifindex;
Jesse Grossccb13522011-10-25 19:26:31 -0700150 else
151 ifindex = 0;
152
153 rcu_read_unlock();
154
155 return ifindex;
156}
157
158static void destroy_dp_rcu(struct rcu_head *rcu)
159{
160 struct datapath *dp = container_of(rcu, struct datapath, rcu);
161
Pravin B Shelar9b996e52014-05-06 18:41:20 -0700162 ovs_flow_tbl_destroy(&dp->table);
Jesse Grossccb13522011-10-25 19:26:31 -0700163 free_percpu(dp->stats_percpu);
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700164 kfree(dp->ports);
Andy Zhou96fbc132017-11-10 12:09:42 -0800165 ovs_meters_exit(dp);
Jesse Grossccb13522011-10-25 19:26:31 -0700166 kfree(dp);
167}
168
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700169static struct hlist_head *vport_hash_bucket(const struct datapath *dp,
170 u16 port_no)
171{
172 return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)];
173}
174
Jarno Rajahalmebb6f9a72014-05-05 11:32:17 -0700175/* Called with ovs_mutex or RCU read lock. */
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700176struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no)
177{
178 struct vport *vport;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700179 struct hlist_head *head;
180
181 head = vport_hash_bucket(dp, port_no);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800182 hlist_for_each_entry_rcu(vport, head, dp_hash_node) {
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700183 if (vport->port_no == port_no)
184 return vport;
185 }
186 return NULL;
187}
188
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700189/* Called with ovs_mutex. */
Jesse Grossccb13522011-10-25 19:26:31 -0700190static struct vport *new_vport(const struct vport_parms *parms)
191{
192 struct vport *vport;
193
194 vport = ovs_vport_add(parms);
195 if (!IS_ERR(vport)) {
196 struct datapath *dp = parms->dp;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700197 struct hlist_head *head = vport_hash_bucket(dp, vport->port_no);
Jesse Grossccb13522011-10-25 19:26:31 -0700198
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700199 hlist_add_head_rcu(&vport->dp_hash_node, head);
Jesse Grossccb13522011-10-25 19:26:31 -0700200 }
Jesse Grossccb13522011-10-25 19:26:31 -0700201 return vport;
202}
203
Jesse Grossccb13522011-10-25 19:26:31 -0700204void ovs_dp_detach_port(struct vport *p)
205{
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700206 ASSERT_OVSL();
Jesse Grossccb13522011-10-25 19:26:31 -0700207
208 /* First drop references to device. */
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700209 hlist_del_rcu(&p->dp_hash_node);
Jesse Grossccb13522011-10-25 19:26:31 -0700210
211 /* Then destroy it. */
212 ovs_vport_del(p);
213}
214
215/* Must be called with rcu_read_lock. */
Pravin B Shelar8c8b1b82014-09-15 19:28:44 -0700216void ovs_dp_process_packet(struct sk_buff *skb, struct sw_flow_key *key)
Jesse Grossccb13522011-10-25 19:26:31 -0700217{
Pravin B Shelar83c8df22014-09-15 19:20:31 -0700218 const struct vport *p = OVS_CB(skb)->input_vport;
Jesse Grossccb13522011-10-25 19:26:31 -0700219 struct datapath *dp = p->dp;
220 struct sw_flow *flow;
Lorand Jakabd98612b2014-10-06 05:45:32 -0700221 struct sw_flow_actions *sf_acts;
Jesse Grossccb13522011-10-25 19:26:31 -0700222 struct dp_stats_percpu *stats;
Jesse Grossccb13522011-10-25 19:26:31 -0700223 u64 *stats_counter;
Andy Zhou1bd71162013-10-22 10:42:46 -0700224 u32 n_mask_hit;
Jesse Grossccb13522011-10-25 19:26:31 -0700225
Shan Wei404f2f12012-11-13 09:52:25 +0800226 stats = this_cpu_ptr(dp->stats_percpu);
Jesse Grossccb13522011-10-25 19:26:31 -0700227
Jesse Grossccb13522011-10-25 19:26:31 -0700228 /* Look up flow. */
Pravin B Shelar8c8b1b82014-09-15 19:28:44 -0700229 flow = ovs_flow_tbl_lookup_stats(&dp->table, key, &n_mask_hit);
Jesse Grossccb13522011-10-25 19:26:31 -0700230 if (unlikely(!flow)) {
231 struct dp_upcall_info upcall;
Pravin B Shelar8c8b1b82014-09-15 19:28:44 -0700232 int error;
Jesse Grossccb13522011-10-25 19:26:31 -0700233
Neil McKeeccea7442015-05-26 20:59:43 -0700234 memset(&upcall, 0, sizeof(upcall));
Jesse Grossccb13522011-10-25 19:26:31 -0700235 upcall.cmd = OVS_PACKET_CMD_MISS;
Alex Wang5cd667b2014-07-17 15:14:13 -0700236 upcall.portid = ovs_vport_find_upcall_portid(p, skb);
Joe Stringer7f8a4362015-08-26 11:31:48 -0700237 upcall.mru = OVS_CB(skb)->mru;
William Tuf2a4d082016-06-10 11:49:33 -0700238 error = ovs_dp_upcall(dp, skb, key, &upcall, 0);
Li RongQingc5eba0b2014-09-03 17:43:45 +0800239 if (unlikely(error))
240 kfree_skb(skb);
241 else
242 consume_skb(skb);
Jesse Grossccb13522011-10-25 19:26:31 -0700243 stats_counter = &stats->n_missed;
244 goto out;
245 }
246
Lorand Jakabd98612b2014-10-06 05:45:32 -0700247 ovs_flow_stats_update(flow, key->tp.flags, skb);
248 sf_acts = rcu_dereference(flow->sf_acts);
249 ovs_execute_actions(dp, skb, sf_acts, key);
Jesse Grossccb13522011-10-25 19:26:31 -0700250
Pravin B Shelare298e502013-10-29 17:22:21 -0700251 stats_counter = &stats->n_hit;
Jesse Grossccb13522011-10-25 19:26:31 -0700252
253out:
254 /* Update datapath statistics. */
WANG Congdf9d9fd2014-02-14 15:10:46 -0800255 u64_stats_update_begin(&stats->syncp);
Jesse Grossccb13522011-10-25 19:26:31 -0700256 (*stats_counter)++;
Andy Zhou1bd71162013-10-22 10:42:46 -0700257 stats->n_mask_hit += n_mask_hit;
WANG Congdf9d9fd2014-02-14 15:10:46 -0800258 u64_stats_update_end(&stats->syncp);
Jesse Grossccb13522011-10-25 19:26:31 -0700259}
260
Jesse Grossccb13522011-10-25 19:26:31 -0700261int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
Pravin B Shelare8eedb82014-11-06 06:57:27 -0800262 const struct sw_flow_key *key,
William Tuf2a4d082016-06-10 11:49:33 -0700263 const struct dp_upcall_info *upcall_info,
264 uint32_t cutlen)
Jesse Grossccb13522011-10-25 19:26:31 -0700265{
266 struct dp_stats_percpu *stats;
Jesse Grossccb13522011-10-25 19:26:31 -0700267 int err;
268
Eric W. Biederman15e47302012-09-07 20:12:54 +0000269 if (upcall_info->portid == 0) {
Jesse Grossccb13522011-10-25 19:26:31 -0700270 err = -ENOTCONN;
271 goto err;
272 }
273
Jesse Grossccb13522011-10-25 19:26:31 -0700274 if (!skb_is_gso(skb))
William Tuf2a4d082016-06-10 11:49:33 -0700275 err = queue_userspace_packet(dp, skb, key, upcall_info, cutlen);
Jesse Grossccb13522011-10-25 19:26:31 -0700276 else
William Tuf2a4d082016-06-10 11:49:33 -0700277 err = queue_gso_packets(dp, skb, key, upcall_info, cutlen);
Jesse Grossccb13522011-10-25 19:26:31 -0700278 if (err)
279 goto err;
280
281 return 0;
282
283err:
Shan Wei404f2f12012-11-13 09:52:25 +0800284 stats = this_cpu_ptr(dp->stats_percpu);
Jesse Grossccb13522011-10-25 19:26:31 -0700285
WANG Congdf9d9fd2014-02-14 15:10:46 -0800286 u64_stats_update_begin(&stats->syncp);
Jesse Grossccb13522011-10-25 19:26:31 -0700287 stats->n_lost++;
WANG Congdf9d9fd2014-02-14 15:10:46 -0800288 u64_stats_update_end(&stats->syncp);
Jesse Grossccb13522011-10-25 19:26:31 -0700289
290 return err;
291}
292
Thomas Graf8055a892013-12-13 15:22:20 +0100293static int queue_gso_packets(struct datapath *dp, struct sk_buff *skb,
Pravin B Shelare8eedb82014-11-06 06:57:27 -0800294 const struct sw_flow_key *key,
William Tuf2a4d082016-06-10 11:49:33 -0700295 const struct dp_upcall_info *upcall_info,
296 uint32_t cutlen)
Jesse Grossccb13522011-10-25 19:26:31 -0700297{
Gustavo A. R. Silva27341662017-11-25 13:14:40 -0600298 unsigned int gso_type = skb_shinfo(skb)->gso_type;
Willem de Bruijn0c19f8462017-11-21 10:22:25 -0500299 struct sw_flow_key later_key;
Jesse Grossccb13522011-10-25 19:26:31 -0700300 struct sk_buff *segs, *nskb;
301 int err;
302
Konstantin Khlebnikov9207f9d2016-01-08 15:21:46 +0300303 BUILD_BUG_ON(sizeof(*OVS_CB(skb)) > SKB_SGO_CB_OFFSET);
Thomas Graf09c5e602013-12-13 15:22:22 +0100304 segs = __skb_gso_segment(skb, NETIF_F_SG, false);
Pravin B Shelar92e5dfc2012-07-20 14:46:29 -0700305 if (IS_ERR(segs))
306 return PTR_ERR(segs);
Florian Westphal330966e2014-10-20 13:49:17 +0200307 if (segs == NULL)
308 return -EINVAL;
Jesse Grossccb13522011-10-25 19:26:31 -0700309
Willem de Bruijn0c19f8462017-11-21 10:22:25 -0500310 if (gso_type & SKB_GSO_UDP) {
311 /* The initial flow key extracted by ovs_flow_key_extract()
312 * in this case is for a first fragment, so we need to
313 * properly mark later fragments.
314 */
315 later_key = *key;
316 later_key.ip.frag = OVS_FRAG_TYPE_LATER;
317 }
318
Jesse Grossccb13522011-10-25 19:26:31 -0700319 /* Queue all of the segments. */
320 skb = segs;
321 do {
Willem de Bruijn0c19f8462017-11-21 10:22:25 -0500322 if (gso_type & SKB_GSO_UDP && skb != segs)
323 key = &later_key;
324
William Tuf2a4d082016-06-10 11:49:33 -0700325 err = queue_userspace_packet(dp, skb, key, upcall_info, cutlen);
Jesse Grossccb13522011-10-25 19:26:31 -0700326 if (err)
327 break;
328
Jesse Grossccb13522011-10-25 19:26:31 -0700329 } while ((skb = skb->next));
330
331 /* Free all of the segments. */
332 skb = segs;
333 do {
334 nskb = skb->next;
335 if (err)
336 kfree_skb(skb);
337 else
338 consume_skb(skb);
339 } while ((skb = nskb));
340 return err;
341}
342
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800343static size_t upcall_msg_size(const struct dp_upcall_info *upcall_info,
Liping Zhang494bea32017-08-16 13:30:07 +0800344 unsigned int hdrlen, int actions_attrlen)
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100345{
346 size_t size = NLMSG_ALIGN(sizeof(struct ovs_header))
Thomas Grafbda56f12013-12-13 15:22:21 +0100347 + nla_total_size(hdrlen) /* OVS_PACKET_ATTR_PACKET */
William Tub95e5922016-06-20 07:26:17 -0700348 + nla_total_size(ovs_key_attr_size()) /* OVS_PACKET_ATTR_KEY */
349 + nla_total_size(sizeof(unsigned int)); /* OVS_PACKET_ATTR_LEN */
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100350
351 /* OVS_PACKET_ATTR_USERDATA */
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800352 if (upcall_info->userdata)
353 size += NLA_ALIGN(upcall_info->userdata->nla_len);
354
355 /* OVS_PACKET_ATTR_EGRESS_TUN_KEY */
356 if (upcall_info->egress_tun_info)
357 size += nla_total_size(ovs_tun_key_attr_size());
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100358
Neil McKeeccea7442015-05-26 20:59:43 -0700359 /* OVS_PACKET_ATTR_ACTIONS */
360 if (upcall_info->actions_len)
Liping Zhang494bea32017-08-16 13:30:07 +0800361 size += nla_total_size(actions_attrlen);
Neil McKeeccea7442015-05-26 20:59:43 -0700362
Joe Stringer7f8a4362015-08-26 11:31:48 -0700363 /* OVS_PACKET_ATTR_MRU */
364 if (upcall_info->mru)
365 size += nla_total_size(sizeof(upcall_info->mru));
366
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100367 return size;
368}
369
Joe Stringer7f8a4362015-08-26 11:31:48 -0700370static void pad_packet(struct datapath *dp, struct sk_buff *skb)
371{
372 if (!(dp->user_features & OVS_DP_F_UNALIGNED)) {
373 size_t plen = NLA_ALIGN(skb->len) - skb->len;
374
375 if (plen > 0)
Johannes Bergb080db52017-06-16 14:29:19 +0200376 skb_put_zero(skb, plen);
Joe Stringer7f8a4362015-08-26 11:31:48 -0700377 }
378}
379
Thomas Graf8055a892013-12-13 15:22:20 +0100380static int queue_userspace_packet(struct datapath *dp, struct sk_buff *skb,
Pravin B Shelare8eedb82014-11-06 06:57:27 -0800381 const struct sw_flow_key *key,
William Tuf2a4d082016-06-10 11:49:33 -0700382 const struct dp_upcall_info *upcall_info,
383 uint32_t cutlen)
Jesse Grossccb13522011-10-25 19:26:31 -0700384{
385 struct ovs_header *upcall;
386 struct sk_buff *nskb = NULL;
Li RongQing4ee45ea2014-09-02 20:52:28 +0800387 struct sk_buff *user_skb = NULL; /* to be queued to userspace */
Jesse Grossccb13522011-10-25 19:26:31 -0700388 struct nlattr *nla;
Thomas Graf795449d2013-11-30 13:21:32 +0100389 size_t len;
Thomas Grafbda56f12013-12-13 15:22:21 +0100390 unsigned int hlen;
Thomas Graf8055a892013-12-13 15:22:20 +0100391 int err, dp_ifindex;
392
393 dp_ifindex = get_dpifindex(dp);
394 if (!dp_ifindex)
395 return -ENODEV;
Jesse Grossccb13522011-10-25 19:26:31 -0700396
Jiri Pirkodf8a39d2015-01-13 17:13:44 +0100397 if (skb_vlan_tag_present(skb)) {
Jesse Grossccb13522011-10-25 19:26:31 -0700398 nskb = skb_clone(skb, GFP_ATOMIC);
399 if (!nskb)
400 return -ENOMEM;
401
Jiri Pirko59682502014-11-19 14:04:59 +0100402 nskb = __vlan_hwaccel_push_inside(nskb);
Dan Carpenter8aa51d62012-05-13 08:44:18 +0000403 if (!nskb)
Jesse Grossccb13522011-10-25 19:26:31 -0700404 return -ENOMEM;
405
Jesse Grossccb13522011-10-25 19:26:31 -0700406 skb = nskb;
407 }
408
409 if (nla_attr_size(skb->len) > USHRT_MAX) {
410 err = -EFBIG;
411 goto out;
412 }
413
Thomas Grafbda56f12013-12-13 15:22:21 +0100414 /* Complete checksum if needed */
415 if (skb->ip_summed == CHECKSUM_PARTIAL &&
Davide Caratti75293902017-05-18 15:44:42 +0200416 (err = skb_csum_hwoffload_help(skb, 0)))
Thomas Grafbda56f12013-12-13 15:22:21 +0100417 goto out;
418
419 /* Older versions of OVS user space enforce alignment of the last
420 * Netlink attribute to NLA_ALIGNTO which would require extensive
421 * padding logic. Only perform zerocopy if padding is not required.
422 */
423 if (dp->user_features & OVS_DP_F_UNALIGNED)
424 hlen = skb_zerocopy_headlen(skb);
425 else
426 hlen = skb->len;
427
Liping Zhang494bea32017-08-16 13:30:07 +0800428 len = upcall_msg_size(upcall_info, hlen - cutlen,
429 OVS_CB(skb)->acts_origlen);
Florian Westphal551ddc02016-02-18 15:03:25 +0100430 user_skb = genlmsg_new(len, GFP_ATOMIC);
Jesse Grossccb13522011-10-25 19:26:31 -0700431 if (!user_skb) {
432 err = -ENOMEM;
433 goto out;
434 }
435
436 upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
437 0, upcall_info->cmd);
Kangjie Lu6f198932019-03-14 23:20:16 -0500438 if (!upcall) {
439 err = -EINVAL;
440 goto out;
441 }
Jesse Grossccb13522011-10-25 19:26:31 -0700442 upcall->dp_ifindex = dp_ifindex;
443
Joe Stringer5b4237b2015-01-21 16:42:48 -0800444 err = ovs_nla_put_key(key, key, OVS_PACKET_ATTR_KEY, false, user_skb);
Eelco Chaudrona734d1f2019-05-02 16:12:38 -0400445 if (err)
446 goto out;
Jesse Grossccb13522011-10-25 19:26:31 -0700447
448 if (upcall_info->userdata)
Ben Pfaff44901082013-02-15 17:29:22 -0800449 __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA,
450 nla_len(upcall_info->userdata),
451 nla_data(upcall_info->userdata));
Jesse Grossccb13522011-10-25 19:26:31 -0700452
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800453 if (upcall_info->egress_tun_info) {
Michal Kubecekae0be8d2019-04-26 11:13:06 +0200454 nla = nla_nest_start_noflag(user_skb,
455 OVS_PACKET_ATTR_EGRESS_TUN_KEY);
Kangjie Lu0fff9bd2019-03-15 01:11:22 -0500456 if (!nla) {
457 err = -EMSGSIZE;
458 goto out;
459 }
Pravin B Shelarfc4099f2015-10-22 18:17:16 -0700460 err = ovs_nla_put_tunnel_info(user_skb,
461 upcall_info->egress_tun_info);
Eelco Chaudrona734d1f2019-05-02 16:12:38 -0400462 if (err)
463 goto out;
464
Wenyu Zhang8f0aad62014-11-06 06:51:24 -0800465 nla_nest_end(user_skb, nla);
466 }
467
Neil McKeeccea7442015-05-26 20:59:43 -0700468 if (upcall_info->actions_len) {
Michal Kubecekae0be8d2019-04-26 11:13:06 +0200469 nla = nla_nest_start_noflag(user_skb, OVS_PACKET_ATTR_ACTIONS);
Kangjie Lu0fff9bd2019-03-15 01:11:22 -0500470 if (!nla) {
471 err = -EMSGSIZE;
472 goto out;
473 }
Neil McKeeccea7442015-05-26 20:59:43 -0700474 err = ovs_nla_put_actions(upcall_info->actions,
475 upcall_info->actions_len,
476 user_skb);
477 if (!err)
478 nla_nest_end(user_skb, nla);
479 else
480 nla_nest_cancel(user_skb, nla);
481 }
482
Joe Stringer7f8a4362015-08-26 11:31:48 -0700483 /* Add OVS_PACKET_ATTR_MRU */
484 if (upcall_info->mru) {
485 if (nla_put_u16(user_skb, OVS_PACKET_ATTR_MRU,
486 upcall_info->mru)) {
487 err = -ENOBUFS;
488 goto out;
489 }
490 pad_packet(dp, user_skb);
491 }
492
William Tub95e5922016-06-20 07:26:17 -0700493 /* Add OVS_PACKET_ATTR_LEN when packet is truncated */
494 if (cutlen > 0) {
495 if (nla_put_u32(user_skb, OVS_PACKET_ATTR_LEN,
496 skb->len)) {
497 err = -ENOBUFS;
498 goto out;
499 }
500 pad_packet(dp, user_skb);
501 }
502
Thomas Grafbda56f12013-12-13 15:22:21 +0100503 /* Only reserve room for attribute header, packet data is added
504 * in skb_zerocopy() */
505 if (!(nla = nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, 0))) {
506 err = -ENOBUFS;
507 goto out;
508 }
William Tuf2a4d082016-06-10 11:49:33 -0700509 nla->nla_len = nla_attr_size(skb->len - cutlen);
Jesse Grossccb13522011-10-25 19:26:31 -0700510
William Tuf2a4d082016-06-10 11:49:33 -0700511 err = skb_zerocopy(user_skb, skb, skb->len - cutlen, hlen);
Zoltan Kiss36d5fe62014-03-26 22:37:45 +0000512 if (err)
513 goto out;
Jesse Grossccb13522011-10-25 19:26:31 -0700514
Thomas Grafaea0bb42014-01-14 16:27:49 +0000515 /* Pad OVS_PACKET_ATTR_PACKET if linear copy was performed */
Joe Stringer7f8a4362015-08-26 11:31:48 -0700516 pad_packet(dp, user_skb);
Thomas Grafaea0bb42014-01-14 16:27:49 +0000517
Thomas Grafbda56f12013-12-13 15:22:21 +0100518 ((struct nlmsghdr *) user_skb->data)->nlmsg_len = user_skb->len;
519
Thomas Graf8055a892013-12-13 15:22:20 +0100520 err = genlmsg_unicast(ovs_dp_get_net(dp), user_skb, upcall_info->portid);
Li RongQing4ee45ea2014-09-02 20:52:28 +0800521 user_skb = NULL;
Jesse Grossccb13522011-10-25 19:26:31 -0700522out:
Zoltan Kiss36d5fe62014-03-26 22:37:45 +0000523 if (err)
524 skb_tx_error(skb);
Li RongQing4ee45ea2014-09-02 20:52:28 +0800525 kfree_skb(user_skb);
Jesse Grossccb13522011-10-25 19:26:31 -0700526 kfree_skb(nskb);
527 return err;
528}
529
Jesse Grossccb13522011-10-25 19:26:31 -0700530static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
531{
532 struct ovs_header *ovs_header = info->userhdr;
Joe Stringer7f8a4362015-08-26 11:31:48 -0700533 struct net *net = sock_net(skb->sk);
Jesse Grossccb13522011-10-25 19:26:31 -0700534 struct nlattr **a = info->attrs;
535 struct sw_flow_actions *acts;
536 struct sk_buff *packet;
537 struct sw_flow *flow;
Lorand Jakabd98612b2014-10-06 05:45:32 -0700538 struct sw_flow_actions *sf_acts;
Jesse Grossccb13522011-10-25 19:26:31 -0700539 struct datapath *dp;
Pravin B Shelar83c8df22014-09-15 19:20:31 -0700540 struct vport *input_vport;
Joe Stringer7f8a4362015-08-26 11:31:48 -0700541 u16 mru = 0;
Jesse Grossccb13522011-10-25 19:26:31 -0700542 int len;
543 int err;
Thomas Graf1ba39802015-01-14 13:56:19 +0000544 bool log = !a[OVS_PACKET_ATTR_PROBE];
Jesse Grossccb13522011-10-25 19:26:31 -0700545
546 err = -EINVAL;
547 if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
Thomas Grafdded45fc2013-03-29 14:46:47 +0100548 !a[OVS_PACKET_ATTR_ACTIONS])
Jesse Grossccb13522011-10-25 19:26:31 -0700549 goto err;
550
551 len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
552 packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
553 err = -ENOMEM;
554 if (!packet)
555 goto err;
556 skb_reserve(packet, NET_IP_ALIGN);
557
Thomas Graf32686a92013-03-29 14:46:48 +0100558 nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
Jesse Grossccb13522011-10-25 19:26:31 -0700559
Joe Stringer7f8a4362015-08-26 11:31:48 -0700560 /* Set packet's mru */
561 if (a[OVS_PACKET_ATTR_MRU]) {
562 mru = nla_get_u16(a[OVS_PACKET_ATTR_MRU]);
563 packet->ignore_df = 1;
564 }
565 OVS_CB(packet)->mru = mru;
566
Jesse Grossccb13522011-10-25 19:26:31 -0700567 /* Build an sw_flow for sending this packet. */
Jarno Rajahalme23dabf82014-03-27 12:35:23 -0700568 flow = ovs_flow_alloc();
Jesse Grossccb13522011-10-25 19:26:31 -0700569 err = PTR_ERR(flow);
570 if (IS_ERR(flow))
571 goto err_kfree_skb;
572
Joe Stringerc2ac6672015-08-26 11:31:52 -0700573 err = ovs_flow_key_extract_userspace(net, a[OVS_PACKET_ATTR_KEY],
574 packet, &flow->key, log);
Jesse Grossccb13522011-10-25 19:26:31 -0700575 if (err)
576 goto err_flow_free;
577
Joe Stringer7f8a4362015-08-26 11:31:48 -0700578 err = ovs_nla_copy_actions(net, a[OVS_PACKET_ATTR_ACTIONS],
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800579 &flow->key, &acts, log);
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700580 if (err)
581 goto err_flow_free;
Jesse Grossccb13522011-10-25 19:26:31 -0700582
Jesse Grossf5796682014-10-03 15:35:33 -0700583 rcu_assign_pointer(flow->sf_acts, acts);
Jesse Grossccb13522011-10-25 19:26:31 -0700584 packet->priority = flow->key.phy.priority;
Ansis Atteka39c7caeb2012-11-26 11:24:11 -0800585 packet->mark = flow->key.phy.skb_mark;
Jesse Grossccb13522011-10-25 19:26:31 -0700586
587 rcu_read_lock();
Joe Stringer7f8a4362015-08-26 11:31:48 -0700588 dp = get_dp_rcu(net, ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -0700589 err = -ENODEV;
590 if (!dp)
591 goto err_unlock;
592
Pravin B Shelar83c8df22014-09-15 19:20:31 -0700593 input_vport = ovs_vport_rcu(dp, flow->key.phy.in_port);
594 if (!input_vport)
595 input_vport = ovs_vport_rcu(dp, OVSP_LOCAL);
596
597 if (!input_vport)
598 goto err_unlock;
599
Joe Stringer7f8a4362015-08-26 11:31:48 -0700600 packet->dev = input_vport->dev;
Pravin B Shelar83c8df22014-09-15 19:20:31 -0700601 OVS_CB(packet)->input_vport = input_vport;
Lorand Jakabd98612b2014-10-06 05:45:32 -0700602 sf_acts = rcu_dereference(flow->sf_acts);
Pravin B Shelar83c8df22014-09-15 19:20:31 -0700603
Jesse Grossccb13522011-10-25 19:26:31 -0700604 local_bh_disable();
Lorand Jakabd98612b2014-10-06 05:45:32 -0700605 err = ovs_execute_actions(dp, packet, sf_acts, &flow->key);
Jesse Grossccb13522011-10-25 19:26:31 -0700606 local_bh_enable();
607 rcu_read_unlock();
608
Andy Zhou03f0d912013-08-07 20:01:00 -0700609 ovs_flow_free(flow, false);
Jesse Grossccb13522011-10-25 19:26:31 -0700610 return err;
611
612err_unlock:
613 rcu_read_unlock();
614err_flow_free:
Andy Zhou03f0d912013-08-07 20:01:00 -0700615 ovs_flow_free(flow, false);
Jesse Grossccb13522011-10-25 19:26:31 -0700616err_kfree_skb:
617 kfree_skb(packet);
618err:
619 return err;
620}
621
622static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
Thomas Grafdded45fc2013-03-29 14:46:47 +0100623 [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
Jesse Grossccb13522011-10-25 19:26:31 -0700624 [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
625 [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
Thomas Graf1ba39802015-01-14 13:56:19 +0000626 [OVS_PACKET_ATTR_PROBE] = { .type = NLA_FLAG },
Joe Stringer7f8a4362015-08-26 11:31:48 -0700627 [OVS_PACKET_ATTR_MRU] = { .type = NLA_U16 },
Jesse Grossccb13522011-10-25 19:26:31 -0700628};
629
Johannes Berg4534de82013-11-14 17:14:46 +0100630static const struct genl_ops dp_packet_genl_ops[] = {
Jesse Grossccb13522011-10-25 19:26:31 -0700631 { .cmd = OVS_PACKET_CMD_EXECUTE,
Johannes Bergef6243a2019-04-26 14:07:31 +0200632 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Tycho Andersen4a926022016-02-05 09:20:52 -0700633 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
Jesse Grossccb13522011-10-25 19:26:31 -0700634 .doit = ovs_packet_cmd_execute
635 }
636};
637
Johannes Berg56989f62016-10-24 14:40:05 +0200638static struct genl_family dp_packet_genl_family __ro_after_init = {
Pravin B Shelar0c200ef2014-05-06 16:44:50 -0700639 .hdrsize = sizeof(struct ovs_header),
640 .name = OVS_PACKET_FAMILY,
641 .version = OVS_PACKET_VERSION,
642 .maxattr = OVS_PACKET_ATTR_MAX,
Johannes Berg3b0f31f2019-03-21 22:51:02 +0100643 .policy = packet_policy,
Pravin B Shelar0c200ef2014-05-06 16:44:50 -0700644 .netnsok = true,
645 .parallel_ops = true,
646 .ops = dp_packet_genl_ops,
647 .n_ops = ARRAY_SIZE(dp_packet_genl_ops),
Johannes Berg489111e2016-10-24 14:40:03 +0200648 .module = THIS_MODULE,
Pravin B Shelar0c200ef2014-05-06 16:44:50 -0700649};
650
Thomas Graf12eb18f2014-11-06 06:58:52 -0800651static void get_dp_stats(const struct datapath *dp, struct ovs_dp_stats *stats,
Andy Zhou1bd71162013-10-22 10:42:46 -0700652 struct ovs_dp_megaflow_stats *mega_stats)
Jesse Grossccb13522011-10-25 19:26:31 -0700653{
654 int i;
Jesse Grossccb13522011-10-25 19:26:31 -0700655
Andy Zhou1bd71162013-10-22 10:42:46 -0700656 memset(mega_stats, 0, sizeof(*mega_stats));
657
Pravin B Shelarb637e492013-10-04 00:14:23 -0700658 stats->n_flows = ovs_flow_tbl_count(&dp->table);
Andy Zhou1bd71162013-10-22 10:42:46 -0700659 mega_stats->n_masks = ovs_flow_tbl_num_masks(&dp->table);
Jesse Grossccb13522011-10-25 19:26:31 -0700660
661 stats->n_hit = stats->n_missed = stats->n_lost = 0;
Andy Zhou1bd71162013-10-22 10:42:46 -0700662
Jesse Grossccb13522011-10-25 19:26:31 -0700663 for_each_possible_cpu(i) {
664 const struct dp_stats_percpu *percpu_stats;
665 struct dp_stats_percpu local_stats;
666 unsigned int start;
667
668 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
669
670 do {
Eric W. Biederman57a77442014-03-13 21:26:42 -0700671 start = u64_stats_fetch_begin_irq(&percpu_stats->syncp);
Jesse Grossccb13522011-10-25 19:26:31 -0700672 local_stats = *percpu_stats;
Eric W. Biederman57a77442014-03-13 21:26:42 -0700673 } while (u64_stats_fetch_retry_irq(&percpu_stats->syncp, start));
Jesse Grossccb13522011-10-25 19:26:31 -0700674
675 stats->n_hit += local_stats.n_hit;
676 stats->n_missed += local_stats.n_missed;
677 stats->n_lost += local_stats.n_lost;
Andy Zhou1bd71162013-10-22 10:42:46 -0700678 mega_stats->n_mask_hit += local_stats.n_mask_hit;
Jesse Grossccb13522011-10-25 19:26:31 -0700679 }
680}
681
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800682static bool should_fill_key(const struct sw_flow_id *sfid, uint32_t ufid_flags)
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100683{
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800684 return ovs_identifier_is_ufid(sfid) &&
685 !(ufid_flags & OVS_UFID_F_OMIT_KEY);
686}
687
688static bool should_fill_mask(uint32_t ufid_flags)
689{
690 return !(ufid_flags & OVS_UFID_F_OMIT_MASK);
691}
692
693static bool should_fill_actions(uint32_t ufid_flags)
694{
695 return !(ufid_flags & OVS_UFID_F_OMIT_ACTIONS);
696}
697
698static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts,
699 const struct sw_flow_id *sfid,
700 uint32_t ufid_flags)
701{
702 size_t len = NLMSG_ALIGN(sizeof(struct ovs_header));
703
704 /* OVS_FLOW_ATTR_UFID */
705 if (sfid && ovs_identifier_is_ufid(sfid))
706 len += nla_total_size(sfid->ufid_len);
707
708 /* OVS_FLOW_ATTR_KEY */
709 if (!sfid || should_fill_key(sfid, ufid_flags))
710 len += nla_total_size(ovs_key_attr_size());
711
712 /* OVS_FLOW_ATTR_MASK */
713 if (should_fill_mask(ufid_flags))
714 len += nla_total_size(ovs_key_attr_size());
715
716 /* OVS_FLOW_ATTR_ACTIONS */
717 if (should_fill_actions(ufid_flags))
Joe Stringer8e2fed12015-08-26 11:31:44 -0700718 len += nla_total_size(acts->orig_len);
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800719
720 return len
Nicolas Dichtel66c7a5e2016-04-26 10:06:15 +0200721 + nla_total_size_64bit(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100722 + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
Nicolas Dichtel66c7a5e2016-04-26 10:06:15 +0200723 + nla_total_size_64bit(8); /* OVS_FLOW_ATTR_USED */
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100724}
725
Jarno Rajahalmebb6f9a72014-05-05 11:32:17 -0700726/* Called with ovs_mutex or RCU read lock. */
Joe Stringerca7105f2014-09-08 13:09:37 -0700727static int ovs_flow_cmd_fill_stats(const struct sw_flow *flow,
728 struct sk_buff *skb)
729{
730 struct ovs_flow_stats stats;
731 __be16 tcp_flags;
732 unsigned long used;
Andy Zhou03f0d912013-08-07 20:01:00 -0700733
Pravin B Shelare298e502013-10-29 17:22:21 -0700734 ovs_flow_stats_get(flow, &stats, &used, &tcp_flags);
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700735
David S. Miller028d6a62012-03-29 23:20:48 -0400736 if (used &&
Nicolas Dichtel0238b722016-04-25 10:25:17 +0200737 nla_put_u64_64bit(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used),
738 OVS_FLOW_ATTR_PAD))
Joe Stringerca7105f2014-09-08 13:09:37 -0700739 return -EMSGSIZE;
Jesse Grossccb13522011-10-25 19:26:31 -0700740
David S. Miller028d6a62012-03-29 23:20:48 -0400741 if (stats.n_packets &&
Nicolas Dichtel66c7a5e2016-04-26 10:06:15 +0200742 nla_put_64bit(skb, OVS_FLOW_ATTR_STATS,
743 sizeof(struct ovs_flow_stats), &stats,
744 OVS_FLOW_ATTR_PAD))
Joe Stringerca7105f2014-09-08 13:09:37 -0700745 return -EMSGSIZE;
Jesse Grossccb13522011-10-25 19:26:31 -0700746
Pravin B Shelare298e502013-10-29 17:22:21 -0700747 if ((u8)ntohs(tcp_flags) &&
748 nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, (u8)ntohs(tcp_flags)))
Joe Stringerca7105f2014-09-08 13:09:37 -0700749 return -EMSGSIZE;
750
751 return 0;
752}
753
754/* Called with ovs_mutex or RCU read lock. */
755static int ovs_flow_cmd_fill_actions(const struct sw_flow *flow,
756 struct sk_buff *skb, int skb_orig_len)
757{
758 struct nlattr *start;
759 int err;
Jesse Grossccb13522011-10-25 19:26:31 -0700760
761 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
762 * this is the first flow to be dumped into 'skb'. This is unusual for
763 * Netlink but individual action lists can be longer than
764 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
765 * The userspace caller can always fetch the actions separately if it
766 * really wants them. (Most userspace callers in fact don't care.)
767 *
768 * This can only fail for dump operations because the skb is always
769 * properly sized for single flows.
770 */
Michal Kubecekae0be8d2019-04-26 11:13:06 +0200771 start = nla_nest_start_noflag(skb, OVS_FLOW_ATTR_ACTIONS);
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700772 if (start) {
Pravin B Shelard57170b2013-07-30 15:39:39 -0700773 const struct sw_flow_actions *sf_acts;
774
Jesse Gross663efa32013-12-03 10:58:53 -0800775 sf_acts = rcu_dereference_ovsl(flow->sf_acts);
Pravin B Shelare6445712013-10-03 18:16:47 -0700776 err = ovs_nla_put_actions(sf_acts->actions,
777 sf_acts->actions_len, skb);
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700778
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700779 if (!err)
780 nla_nest_end(skb, start);
781 else {
782 if (skb_orig_len)
Joe Stringerca7105f2014-09-08 13:09:37 -0700783 return err;
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700784
785 nla_nest_cancel(skb, start);
786 }
Joe Stringerca7105f2014-09-08 13:09:37 -0700787 } else if (skb_orig_len) {
788 return -EMSGSIZE;
789 }
790
791 return 0;
792}
793
794/* Called with ovs_mutex or RCU read lock. */
795static int ovs_flow_cmd_fill_info(const struct sw_flow *flow, int dp_ifindex,
796 struct sk_buff *skb, u32 portid,
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800797 u32 seq, u32 flags, u8 cmd, u32 ufid_flags)
Joe Stringerca7105f2014-09-08 13:09:37 -0700798{
799 const int skb_orig_len = skb->len;
800 struct ovs_header *ovs_header;
801 int err;
802
803 ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family,
804 flags, cmd);
805 if (!ovs_header)
806 return -EMSGSIZE;
807
808 ovs_header->dp_ifindex = dp_ifindex;
809
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800810 err = ovs_nla_put_identifier(flow, skb);
Joe Stringer5b4237b2015-01-21 16:42:48 -0800811 if (err)
812 goto error;
813
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800814 if (should_fill_key(&flow->id, ufid_flags)) {
815 err = ovs_nla_put_masked_key(flow, skb);
816 if (err)
817 goto error;
818 }
819
820 if (should_fill_mask(ufid_flags)) {
821 err = ovs_nla_put_mask(flow, skb);
822 if (err)
823 goto error;
824 }
Joe Stringerca7105f2014-09-08 13:09:37 -0700825
826 err = ovs_flow_cmd_fill_stats(flow, skb);
827 if (err)
828 goto error;
829
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800830 if (should_fill_actions(ufid_flags)) {
831 err = ovs_flow_cmd_fill_actions(flow, skb, skb_orig_len);
832 if (err)
833 goto error;
834 }
Jesse Grossccb13522011-10-25 19:26:31 -0700835
Johannes Berg053c0952015-01-16 22:09:00 +0100836 genlmsg_end(skb, ovs_header);
837 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -0700838
Jesse Grossccb13522011-10-25 19:26:31 -0700839error:
840 genlmsg_cancel(skb, ovs_header);
841 return err;
842}
843
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700844/* May not be called with RCU read lock. */
845static struct sk_buff *ovs_flow_cmd_alloc_info(const struct sw_flow_actions *acts,
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800846 const struct sw_flow_id *sfid,
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700847 struct genl_info *info,
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800848 bool always,
849 uint32_t ufid_flags)
Jesse Grossccb13522011-10-25 19:26:31 -0700850{
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700851 struct sk_buff *skb;
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800852 size_t len;
Jesse Grossccb13522011-10-25 19:26:31 -0700853
Samuel Gauthier9b67aa42014-09-18 10:31:04 +0200854 if (!always && !ovs_must_notify(&dp_flow_genl_family, info, 0))
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700855 return NULL;
856
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800857 len = ovs_flow_cmd_msg_size(acts, sfid, ufid_flags);
Florian Westphal551ddc02016-02-18 15:03:25 +0100858 skb = genlmsg_new(len, GFP_KERNEL);
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700859 if (!skb)
860 return ERR_PTR(-ENOMEM);
861
862 return skb;
Jesse Grossccb13522011-10-25 19:26:31 -0700863}
864
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700865/* Called with ovs_mutex. */
866static struct sk_buff *ovs_flow_cmd_build_info(const struct sw_flow *flow,
867 int dp_ifindex,
868 struct genl_info *info, u8 cmd,
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800869 bool always, u32 ufid_flags)
Jesse Grossccb13522011-10-25 19:26:31 -0700870{
871 struct sk_buff *skb;
872 int retval;
873
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800874 skb = ovs_flow_cmd_alloc_info(ovsl_dereference(flow->sf_acts),
875 &flow->id, info, always, ufid_flags);
Himangi Saraogid0e992a2014-07-27 12:37:46 +0530876 if (IS_ERR_OR_NULL(skb))
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -0700877 return skb;
Jesse Grossccb13522011-10-25 19:26:31 -0700878
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -0700879 retval = ovs_flow_cmd_fill_info(flow, dp_ifindex, skb,
880 info->snd_portid, info->snd_seq, 0,
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800881 cmd, ufid_flags);
Jesse Grossccb13522011-10-25 19:26:31 -0700882 BUG_ON(retval < 0);
883 return skb;
884}
885
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700886static int ovs_flow_cmd_new(struct sk_buff *skb, struct genl_info *info)
Jesse Grossccb13522011-10-25 19:26:31 -0700887{
Joe Stringer7f8a4362015-08-26 11:31:48 -0700888 struct net *net = sock_net(skb->sk);
Jesse Grossccb13522011-10-25 19:26:31 -0700889 struct nlattr **a = info->attrs;
890 struct ovs_header *ovs_header = info->userhdr;
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800891 struct sw_flow *flow = NULL, *new_flow;
Andy Zhou03f0d912013-08-07 20:01:00 -0700892 struct sw_flow_mask mask;
Jesse Grossccb13522011-10-25 19:26:31 -0700893 struct sk_buff *reply;
894 struct datapath *dp;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700895 struct sw_flow_actions *acts;
896 struct sw_flow_match match;
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800897 u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700898 int error;
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800899 bool log = !a[OVS_FLOW_ATTR_PROBE];
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700900
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700901 /* Must have key and actions. */
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700902 error = -EINVAL;
Jesse Gross426cda52014-10-06 05:08:38 -0700903 if (!a[OVS_FLOW_ATTR_KEY]) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800904 OVS_NLERR(log, "Flow key attr not present in new flow.");
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700905 goto error;
Jesse Gross426cda52014-10-06 05:08:38 -0700906 }
907 if (!a[OVS_FLOW_ATTR_ACTIONS]) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800908 OVS_NLERR(log, "Flow actions attr not present in new flow.");
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700909 goto error;
Jesse Gross426cda52014-10-06 05:08:38 -0700910 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700911
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700912 /* Most of the time we need to allocate a new flow, do it before
913 * locking.
914 */
915 new_flow = ovs_flow_alloc();
916 if (IS_ERR(new_flow)) {
917 error = PTR_ERR(new_flow);
918 goto error;
919 }
920
921 /* Extract key. */
pravin shelar22799942016-09-19 13:51:00 -0700922 ovs_match_init(&match, &new_flow->key, false, &mask);
Joe Stringerc2ac6672015-08-26 11:31:52 -0700923 error = ovs_nla_get_match(net, &match, a[OVS_FLOW_ATTR_KEY],
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800924 a[OVS_FLOW_ATTR_MASK], log);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700925 if (error)
926 goto err_kfree_flow;
927
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800928 /* Extract flow identifier. */
929 error = ovs_nla_get_identifier(&new_flow->id, a[OVS_FLOW_ATTR_UFID],
pravin shelar190aa3e72016-09-19 13:50:59 -0700930 &new_flow->key, log);
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800931 if (error)
932 goto err_kfree_flow;
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700933
pravin shelar190aa3e72016-09-19 13:50:59 -0700934 /* unmasked key is needed to match when ufid is not used. */
935 if (ovs_identifier_is_key(&new_flow->id))
936 match.key = new_flow->id.unmasked_key;
937
938 ovs_flow_mask_key(&new_flow->key, &new_flow->key, true, &mask);
939
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700940 /* Validate actions. */
Joe Stringer7f8a4362015-08-26 11:31:48 -0700941 error = ovs_nla_copy_actions(net, a[OVS_FLOW_ATTR_ACTIONS],
942 &new_flow->key, &acts, log);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700943 if (error) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -0800944 OVS_NLERR(log, "Flow actions may not be safe on all matching packets.");
Pravin B Shelar2fdb9572014-10-19 11:19:51 -0700945 goto err_kfree_flow;
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700946 }
947
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800948 reply = ovs_flow_cmd_alloc_info(acts, &new_flow->id, info, false,
949 ufid_flags);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700950 if (IS_ERR(reply)) {
951 error = PTR_ERR(reply);
952 goto err_kfree_acts;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700953 }
954
955 ovs_lock();
Joe Stringer7f8a4362015-08-26 11:31:48 -0700956 dp = get_dp(net, ovs_header->dp_ifindex);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700957 if (unlikely(!dp)) {
958 error = -ENODEV;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700959 goto err_unlock_ovs;
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700960 }
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800961
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700962 /* Check if this is a duplicate flow */
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800963 if (ovs_identifier_is_ufid(&new_flow->id))
964 flow = ovs_flow_tbl_lookup_ufid(&dp->table, &new_flow->id);
965 if (!flow)
pravin shelar190aa3e72016-09-19 13:50:59 -0700966 flow = ovs_flow_tbl_lookup(&dp->table, &new_flow->key);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700967 if (likely(!flow)) {
968 rcu_assign_pointer(new_flow->sf_acts, acts);
969
970 /* Put flow in bucket. */
971 error = ovs_flow_tbl_insert(&dp->table, new_flow, &mask);
972 if (unlikely(error)) {
973 acts = NULL;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700974 goto err_unlock_ovs;
975 }
976
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700977 if (unlikely(reply)) {
978 error = ovs_flow_cmd_fill_info(new_flow,
979 ovs_header->dp_ifindex,
980 reply, info->snd_portid,
981 info->snd_seq, 0,
Joe Stringer74ed7ab2015-01-21 16:42:52 -0800982 OVS_FLOW_CMD_NEW,
983 ufid_flags);
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700984 BUG_ON(error < 0);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700985 }
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700986 ovs_unlock();
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700987 } else {
988 struct sw_flow_actions *old_acts;
989
990 /* Bail out if we're not allowed to modify an existing flow.
991 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
992 * because Generic Netlink treats the latter as a dump
993 * request. We also accept NLM_F_EXCL in case that bug ever
994 * gets fixed.
995 */
Jarno Rajahalme893f1392014-05-05 15:22:25 -0700996 if (unlikely(info->nlhdr->nlmsg_flags & (NLM_F_CREATE
997 | NLM_F_EXCL))) {
998 error = -EEXIST;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -0700999 goto err_unlock_ovs;
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001000 }
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001001 /* The flow identifier has to be the same for flow updates.
1002 * Look for any overlapping flow.
1003 */
1004 if (unlikely(!ovs_flow_cmp(flow, &match))) {
1005 if (ovs_identifier_is_key(&flow->id))
1006 flow = ovs_flow_tbl_lookup_exact(&dp->table,
1007 &match);
1008 else /* UFID matches but key is different */
1009 flow = NULL;
Alex Wang4a46b242014-06-30 20:30:29 -07001010 if (!flow) {
1011 error = -ENOENT;
1012 goto err_unlock_ovs;
1013 }
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001014 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001015 /* Update actions. */
1016 old_acts = ovsl_dereference(flow->sf_acts);
1017 rcu_assign_pointer(flow->sf_acts, acts);
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001018
1019 if (unlikely(reply)) {
1020 error = ovs_flow_cmd_fill_info(flow,
1021 ovs_header->dp_ifindex,
1022 reply, info->snd_portid,
1023 info->snd_seq, 0,
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001024 OVS_FLOW_CMD_NEW,
1025 ufid_flags);
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001026 BUG_ON(error < 0);
1027 }
1028 ovs_unlock();
1029
Thomas Graf34ae9322015-07-21 10:44:03 +02001030 ovs_nla_free_flow_actions_rcu(old_acts);
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001031 ovs_flow_free(new_flow, false);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001032 }
1033
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001034 if (reply)
1035 ovs_notify(&dp_flow_genl_family, reply, info);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001036 return 0;
1037
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001038err_unlock_ovs:
1039 ovs_unlock();
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001040 kfree_skb(reply);
1041err_kfree_acts:
Thomas Graf34ae9322015-07-21 10:44:03 +02001042 ovs_nla_free_flow_actions(acts);
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001043err_kfree_flow:
1044 ovs_flow_free(new_flow, false);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001045error:
1046 return error;
1047}
1048
Pravin B Shelar2fdb9572014-10-19 11:19:51 -07001049/* Factor out action copy to avoid "Wframe-larger-than=1024" warning. */
Joe Stringer7f8a4362015-08-26 11:31:48 -07001050static struct sw_flow_actions *get_flow_actions(struct net *net,
1051 const struct nlattr *a,
Jesse Gross6b205b22014-10-03 15:35:32 -07001052 const struct sw_flow_key *key,
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001053 const struct sw_flow_mask *mask,
1054 bool log)
Jesse Gross6b205b22014-10-03 15:35:32 -07001055{
1056 struct sw_flow_actions *acts;
1057 struct sw_flow_key masked_key;
1058 int error;
1059
Jesse Grossae5f2fb2015-09-21 20:21:20 -07001060 ovs_flow_mask_key(&masked_key, key, true, mask);
Joe Stringer7f8a4362015-08-26 11:31:48 -07001061 error = ovs_nla_copy_actions(net, a, &masked_key, &acts, log);
Jesse Gross6b205b22014-10-03 15:35:32 -07001062 if (error) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001063 OVS_NLERR(log,
1064 "Actions may not be safe on all matching packets");
Jesse Gross6b205b22014-10-03 15:35:32 -07001065 return ERR_PTR(error);
1066 }
1067
1068 return acts;
1069}
1070
Tonghao Zhang9cc9a5c2017-06-29 17:27:44 -07001071/* Factor out match-init and action-copy to avoid
1072 * "Wframe-larger-than=1024" warning. Because mask is only
1073 * used to get actions, we new a function to save some
1074 * stack space.
1075 *
1076 * If there are not key and action attrs, we return 0
1077 * directly. In the case, the caller will also not use the
1078 * match as before. If there is action attr, we try to get
1079 * actions and save them to *acts. Before returning from
1080 * the function, we reset the match->mask pointer. Because
1081 * we should not to return match object with dangling reference
1082 * to mask.
1083 * */
1084static int ovs_nla_init_match_and_action(struct net *net,
1085 struct sw_flow_match *match,
1086 struct sw_flow_key *key,
1087 struct nlattr **a,
1088 struct sw_flow_actions **acts,
1089 bool log)
1090{
1091 struct sw_flow_mask mask;
1092 int error = 0;
1093
1094 if (a[OVS_FLOW_ATTR_KEY]) {
1095 ovs_match_init(match, key, true, &mask);
1096 error = ovs_nla_get_match(net, match, a[OVS_FLOW_ATTR_KEY],
1097 a[OVS_FLOW_ATTR_MASK], log);
1098 if (error)
1099 goto error;
1100 }
1101
1102 if (a[OVS_FLOW_ATTR_ACTIONS]) {
1103 if (!a[OVS_FLOW_ATTR_KEY]) {
1104 OVS_NLERR(log,
1105 "Flow key attribute not present in set flow.");
Christophe JAILLET5829e622017-09-11 21:56:20 +02001106 error = -EINVAL;
1107 goto error;
Tonghao Zhang9cc9a5c2017-06-29 17:27:44 -07001108 }
1109
1110 *acts = get_flow_actions(net, a[OVS_FLOW_ATTR_ACTIONS], key,
1111 &mask, log);
1112 if (IS_ERR(*acts)) {
1113 error = PTR_ERR(*acts);
1114 goto error;
1115 }
1116 }
1117
1118 /* On success, error is 0. */
1119error:
1120 match->mask = NULL;
1121 return error;
1122}
1123
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001124static int ovs_flow_cmd_set(struct sk_buff *skb, struct genl_info *info)
1125{
Joe Stringer7f8a4362015-08-26 11:31:48 -07001126 struct net *net = sock_net(skb->sk);
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001127 struct nlattr **a = info->attrs;
1128 struct ovs_header *ovs_header = info->userhdr;
Jesse Gross6b205b22014-10-03 15:35:32 -07001129 struct sw_flow_key key;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001130 struct sw_flow *flow;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001131 struct sk_buff *reply = NULL;
1132 struct datapath *dp;
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001133 struct sw_flow_actions *old_acts = NULL, *acts = NULL;
Andy Zhou03f0d912013-08-07 20:01:00 -07001134 struct sw_flow_match match;
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001135 struct sw_flow_id sfid;
1136 u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
Samuel Gauthier6f15cdb2016-03-10 17:14:59 +01001137 int error = 0;
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001138 bool log = !a[OVS_FLOW_ATTR_PROBE];
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001139 bool ufid_present;
Jesse Grossccb13522011-10-25 19:26:31 -07001140
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001141 ufid_present = ovs_nla_get_ufid(&sfid, a[OVS_FLOW_ATTR_UFID], log);
Tonghao Zhang9cc9a5c2017-06-29 17:27:44 -07001142 if (!a[OVS_FLOW_ATTR_KEY] && !ufid_present) {
Samuel Gauthier6f15cdb2016-03-10 17:14:59 +01001143 OVS_NLERR(log,
1144 "Flow set message rejected, Key attribute missing.");
Tonghao Zhang9cc9a5c2017-06-29 17:27:44 -07001145 return -EINVAL;
Samuel Gauthier6f15cdb2016-03-10 17:14:59 +01001146 }
Tonghao Zhang9cc9a5c2017-06-29 17:27:44 -07001147
1148 error = ovs_nla_init_match_and_action(net, &match, &key, a,
1149 &acts, log);
Jesse Grossccb13522011-10-25 19:26:31 -07001150 if (error)
1151 goto error;
1152
Tonghao Zhang9cc9a5c2017-06-29 17:27:44 -07001153 if (acts) {
Pravin B Shelar2fdb9572014-10-19 11:19:51 -07001154 /* Can allocate before locking if have acts. */
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001155 reply = ovs_flow_cmd_alloc_info(acts, &sfid, info, false,
1156 ufid_flags);
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001157 if (IS_ERR(reply)) {
1158 error = PTR_ERR(reply);
1159 goto err_kfree_acts;
Andy Zhou03f0d912013-08-07 20:01:00 -07001160 }
Jesse Grossccb13522011-10-25 19:26:31 -07001161 }
1162
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001163 ovs_lock();
Joe Stringer7f8a4362015-08-26 11:31:48 -07001164 dp = get_dp(net, ovs_header->dp_ifindex);
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001165 if (unlikely(!dp)) {
1166 error = -ENODEV;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001167 goto err_unlock_ovs;
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001168 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001169 /* Check that the flow exists. */
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001170 if (ufid_present)
1171 flow = ovs_flow_tbl_lookup_ufid(&dp->table, &sfid);
1172 else
1173 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001174 if (unlikely(!flow)) {
1175 error = -ENOENT;
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001176 goto err_unlock_ovs;
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001177 }
Alex Wang4a46b242014-06-30 20:30:29 -07001178
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001179 /* Update actions, if present. */
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001180 if (likely(acts)) {
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001181 old_acts = ovsl_dereference(flow->sf_acts);
Jesse Grossccb13522011-10-25 19:26:31 -07001182 rcu_assign_pointer(flow->sf_acts, acts);
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001183
1184 if (unlikely(reply)) {
1185 error = ovs_flow_cmd_fill_info(flow,
1186 ovs_header->dp_ifindex,
1187 reply, info->snd_portid,
1188 info->snd_seq, 0,
Yifeng Sun804fe102018-09-26 11:40:14 -07001189 OVS_FLOW_CMD_SET,
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001190 ufid_flags);
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001191 BUG_ON(error < 0);
1192 }
1193 } else {
1194 /* Could not alloc without acts before locking. */
1195 reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex,
Yifeng Sun804fe102018-09-26 11:40:14 -07001196 info, OVS_FLOW_CMD_SET, false,
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001197 ufid_flags);
1198
Viresh Kumarb5ffe632015-08-12 15:59:47 +05301199 if (IS_ERR(reply)) {
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001200 error = PTR_ERR(reply);
1201 goto err_unlock_ovs;
1202 }
Jesse Grossccb13522011-10-25 19:26:31 -07001203 }
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001204
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001205 /* Clear stats. */
1206 if (a[OVS_FLOW_ATTR_CLEAR])
1207 ovs_flow_stats_clear(flow);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001208 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001209
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001210 if (reply)
1211 ovs_notify(&dp_flow_genl_family, reply, info);
1212 if (old_acts)
Thomas Graf34ae9322015-07-21 10:44:03 +02001213 ovs_nla_free_flow_actions_rcu(old_acts);
Jarno Rajahalmefb5d1e92014-05-05 13:13:14 -07001214
Jesse Grossccb13522011-10-25 19:26:31 -07001215 return 0;
1216
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001217err_unlock_ovs:
1218 ovs_unlock();
Jarno Rajahalme893f1392014-05-05 15:22:25 -07001219 kfree_skb(reply);
1220err_kfree_acts:
Thomas Graf34ae9322015-07-21 10:44:03 +02001221 ovs_nla_free_flow_actions(acts);
Jesse Grossccb13522011-10-25 19:26:31 -07001222error:
1223 return error;
1224}
1225
1226static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1227{
1228 struct nlattr **a = info->attrs;
1229 struct ovs_header *ovs_header = info->userhdr;
Joe Stringerc2ac6672015-08-26 11:31:52 -07001230 struct net *net = sock_net(skb->sk);
Jesse Grossccb13522011-10-25 19:26:31 -07001231 struct sw_flow_key key;
1232 struct sk_buff *reply;
1233 struct sw_flow *flow;
1234 struct datapath *dp;
Andy Zhou03f0d912013-08-07 20:01:00 -07001235 struct sw_flow_match match;
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001236 struct sw_flow_id ufid;
1237 u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
1238 int err = 0;
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001239 bool log = !a[OVS_FLOW_ATTR_PROBE];
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001240 bool ufid_present;
Jesse Grossccb13522011-10-25 19:26:31 -07001241
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001242 ufid_present = ovs_nla_get_ufid(&ufid, a[OVS_FLOW_ATTR_UFID], log);
1243 if (a[OVS_FLOW_ATTR_KEY]) {
pravin shelar22799942016-09-19 13:51:00 -07001244 ovs_match_init(&match, &key, true, NULL);
Joe Stringerc2ac6672015-08-26 11:31:52 -07001245 err = ovs_nla_get_match(net, &match, a[OVS_FLOW_ATTR_KEY], NULL,
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001246 log);
1247 } else if (!ufid_present) {
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001248 OVS_NLERR(log,
1249 "Flow get message rejected, Key attribute missing.");
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001250 err = -EINVAL;
Andy Zhou03f0d912013-08-07 20:01:00 -07001251 }
Jesse Grossccb13522011-10-25 19:26:31 -07001252 if (err)
1253 return err;
1254
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001255 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001256 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001257 if (!dp) {
1258 err = -ENODEV;
1259 goto unlock;
1260 }
Jesse Grossccb13522011-10-25 19:26:31 -07001261
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001262 if (ufid_present)
1263 flow = ovs_flow_tbl_lookup_ufid(&dp->table, &ufid);
1264 else
1265 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
Alex Wang4a46b242014-06-30 20:30:29 -07001266 if (!flow) {
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001267 err = -ENOENT;
1268 goto unlock;
1269 }
Jesse Grossccb13522011-10-25 19:26:31 -07001270
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -07001271 reply = ovs_flow_cmd_build_info(flow, ovs_header->dp_ifindex, info,
Yifeng Sun804fe102018-09-26 11:40:14 -07001272 OVS_FLOW_CMD_GET, true, ufid_flags);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001273 if (IS_ERR(reply)) {
1274 err = PTR_ERR(reply);
1275 goto unlock;
1276 }
Jesse Grossccb13522011-10-25 19:26:31 -07001277
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001278 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001279 return genlmsg_reply(reply, info);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001280unlock:
1281 ovs_unlock();
1282 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001283}
1284
1285static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1286{
1287 struct nlattr **a = info->attrs;
1288 struct ovs_header *ovs_header = info->userhdr;
Joe Stringerc2ac6672015-08-26 11:31:52 -07001289 struct net *net = sock_net(skb->sk);
Jesse Grossccb13522011-10-25 19:26:31 -07001290 struct sw_flow_key key;
1291 struct sk_buff *reply;
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001292 struct sw_flow *flow = NULL;
Jesse Grossccb13522011-10-25 19:26:31 -07001293 struct datapath *dp;
Andy Zhou03f0d912013-08-07 20:01:00 -07001294 struct sw_flow_match match;
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001295 struct sw_flow_id ufid;
1296 u32 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
Jesse Grossccb13522011-10-25 19:26:31 -07001297 int err;
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001298 bool log = !a[OVS_FLOW_ATTR_PROBE];
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001299 bool ufid_present;
Jesse Grossccb13522011-10-25 19:26:31 -07001300
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001301 ufid_present = ovs_nla_get_ufid(&ufid, a[OVS_FLOW_ATTR_UFID], log);
1302 if (a[OVS_FLOW_ATTR_KEY]) {
pravin shelar22799942016-09-19 13:51:00 -07001303 ovs_match_init(&match, &key, true, NULL);
Joe Stringerc2ac6672015-08-26 11:31:52 -07001304 err = ovs_nla_get_match(net, &match, a[OVS_FLOW_ATTR_KEY],
1305 NULL, log);
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001306 if (unlikely(err))
1307 return err;
1308 }
1309
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001310 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001311 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001312 if (unlikely(!dp)) {
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001313 err = -ENODEV;
1314 goto unlock;
1315 }
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001316
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001317 if (unlikely(!a[OVS_FLOW_ATTR_KEY] && !ufid_present)) {
Pravin B Shelarb637e492013-10-04 00:14:23 -07001318 err = ovs_flow_tbl_flush(&dp->table);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001319 goto unlock;
1320 }
Andy Zhou03f0d912013-08-07 20:01:00 -07001321
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001322 if (ufid_present)
1323 flow = ovs_flow_tbl_lookup_ufid(&dp->table, &ufid);
1324 else
1325 flow = ovs_flow_tbl_lookup_exact(&dp->table, &match);
Alex Wang4a46b242014-06-30 20:30:29 -07001326 if (unlikely(!flow)) {
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001327 err = -ENOENT;
1328 goto unlock;
1329 }
Jesse Grossccb13522011-10-25 19:26:31 -07001330
Pravin B Shelarb637e492013-10-04 00:14:23 -07001331 ovs_flow_tbl_remove(&dp->table, flow);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001332 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001333
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001334 reply = ovs_flow_cmd_alloc_info((const struct sw_flow_actions __force *) flow->sf_acts,
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001335 &flow->id, info, false, ufid_flags);
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001336 if (likely(reply)) {
Enrico Weigeltb90f5aa2019-06-05 23:06:40 +02001337 if (!IS_ERR(reply)) {
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001338 rcu_read_lock(); /*To keep RCU checker happy. */
1339 err = ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex,
1340 reply, info->snd_portid,
1341 info->snd_seq, 0,
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001342 OVS_FLOW_CMD_DEL,
1343 ufid_flags);
Jarno Rajahalmeaed06772014-05-05 14:40:13 -07001344 rcu_read_unlock();
1345 BUG_ON(err < 0);
1346
1347 ovs_notify(&dp_flow_genl_family, reply, info);
1348 } else {
1349 netlink_set_err(sock_net(skb->sk)->genl_sock, 0, 0, PTR_ERR(reply));
1350 }
1351 }
1352
1353 ovs_flow_free(flow, true);
Jesse Grossccb13522011-10-25 19:26:31 -07001354 return 0;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001355unlock:
1356 ovs_unlock();
1357 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001358}
1359
1360static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1361{
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001362 struct nlattr *a[__OVS_FLOW_ATTR_MAX];
Jesse Grossccb13522011-10-25 19:26:31 -07001363 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
Pravin B Shelarb637e492013-10-04 00:14:23 -07001364 struct table_instance *ti;
Jesse Grossccb13522011-10-25 19:26:31 -07001365 struct datapath *dp;
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001366 u32 ufid_flags;
1367 int err;
1368
Johannes Berg8cb08172019-04-26 14:07:28 +02001369 err = genlmsg_parse_deprecated(cb->nlh, &dp_flow_genl_family, a,
1370 OVS_FLOW_ATTR_MAX, flow_policy, NULL);
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001371 if (err)
1372 return err;
1373 ufid_flags = ovs_nla_get_ufid_flags(a[OVS_FLOW_ATTR_UFID_FLAGS]);
Jesse Grossccb13522011-10-25 19:26:31 -07001374
Pravin B Shelard57170b2013-07-30 15:39:39 -07001375 rcu_read_lock();
Andy Zhoucc3a5ae2014-09-08 13:14:22 -07001376 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001377 if (!dp) {
Pravin B Shelard57170b2013-07-30 15:39:39 -07001378 rcu_read_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001379 return -ENODEV;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001380 }
Jesse Grossccb13522011-10-25 19:26:31 -07001381
Pravin B Shelarb637e492013-10-04 00:14:23 -07001382 ti = rcu_dereference(dp->table.ti);
Jesse Grossccb13522011-10-25 19:26:31 -07001383 for (;;) {
1384 struct sw_flow *flow;
1385 u32 bucket, obj;
1386
1387 bucket = cb->args[0];
1388 obj = cb->args[1];
Pravin B Shelarb637e492013-10-04 00:14:23 -07001389 flow = ovs_flow_tbl_dump_next(ti, &bucket, &obj);
Jesse Grossccb13522011-10-25 19:26:31 -07001390 if (!flow)
1391 break;
1392
Jarno Rajahalme0e9796b2014-05-05 14:28:07 -07001393 if (ovs_flow_cmd_fill_info(flow, ovs_header->dp_ifindex, skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001394 NETLINK_CB(cb->skb).portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001395 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Yifeng Sun804fe102018-09-26 11:40:14 -07001396 OVS_FLOW_CMD_GET, ufid_flags) < 0)
Jesse Grossccb13522011-10-25 19:26:31 -07001397 break;
1398
1399 cb->args[0] = bucket;
1400 cb->args[1] = obj;
1401 }
Pravin B Shelard57170b2013-07-30 15:39:39 -07001402 rcu_read_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001403 return skb->len;
1404}
1405
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001406static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
1407 [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001408 [OVS_FLOW_ATTR_MASK] = { .type = NLA_NESTED },
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001409 [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
1410 [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
Jarno Rajahalme05da5892014-11-06 07:03:05 -08001411 [OVS_FLOW_ATTR_PROBE] = { .type = NLA_FLAG },
Joe Stringer74ed7ab2015-01-21 16:42:52 -08001412 [OVS_FLOW_ATTR_UFID] = { .type = NLA_UNSPEC, .len = 1 },
1413 [OVS_FLOW_ATTR_UFID_FLAGS] = { .type = NLA_U32 },
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001414};
1415
stephen hemminger48e48a72014-07-16 11:25:52 -07001416static const struct genl_ops dp_flow_genl_ops[] = {
Jesse Grossccb13522011-10-25 19:26:31 -07001417 { .cmd = OVS_FLOW_CMD_NEW,
Johannes Bergef6243a2019-04-26 14:07:31 +02001418 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Tycho Andersen4a926022016-02-05 09:20:52 -07001419 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001420 .doit = ovs_flow_cmd_new
Jesse Grossccb13522011-10-25 19:26:31 -07001421 },
1422 { .cmd = OVS_FLOW_CMD_DEL,
Johannes Bergef6243a2019-04-26 14:07:31 +02001423 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Tycho Andersen4a926022016-02-05 09:20:52 -07001424 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
Jesse Grossccb13522011-10-25 19:26:31 -07001425 .doit = ovs_flow_cmd_del
1426 },
1427 { .cmd = OVS_FLOW_CMD_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +02001428 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jesse Grossccb13522011-10-25 19:26:31 -07001429 .flags = 0, /* OK for unprivileged users. */
Jesse Grossccb13522011-10-25 19:26:31 -07001430 .doit = ovs_flow_cmd_get,
1431 .dumpit = ovs_flow_cmd_dump
1432 },
1433 { .cmd = OVS_FLOW_CMD_SET,
Johannes Bergef6243a2019-04-26 14:07:31 +02001434 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Tycho Andersen4a926022016-02-05 09:20:52 -07001435 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
Jarno Rajahalme37bdc872014-05-05 14:53:51 -07001436 .doit = ovs_flow_cmd_set,
Jesse Grossccb13522011-10-25 19:26:31 -07001437 },
1438};
1439
Johannes Berg56989f62016-10-24 14:40:05 +02001440static struct genl_family dp_flow_genl_family __ro_after_init = {
Jesse Grossccb13522011-10-25 19:26:31 -07001441 .hdrsize = sizeof(struct ovs_header),
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001442 .name = OVS_FLOW_FAMILY,
1443 .version = OVS_FLOW_VERSION,
1444 .maxattr = OVS_FLOW_ATTR_MAX,
Johannes Berg3b0f31f2019-03-21 22:51:02 +01001445 .policy = flow_policy,
Pravin B Shelar3a4e0d62013-04-23 07:48:48 +00001446 .netnsok = true,
1447 .parallel_ops = true,
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001448 .ops = dp_flow_genl_ops,
1449 .n_ops = ARRAY_SIZE(dp_flow_genl_ops),
1450 .mcgrps = &ovs_dp_flow_multicast_group,
1451 .n_mcgrps = 1,
Johannes Berg489111e2016-10-24 14:40:03 +02001452 .module = THIS_MODULE,
Jesse Grossccb13522011-10-25 19:26:31 -07001453};
1454
Thomas Grafc3ff8cf2013-03-29 14:46:49 +01001455static size_t ovs_dp_cmd_msg_size(void)
1456{
1457 size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
1458
1459 msgsize += nla_total_size(IFNAMSIZ);
Nicolas Dichtel66c7a5e2016-04-26 10:06:15 +02001460 msgsize += nla_total_size_64bit(sizeof(struct ovs_dp_stats));
1461 msgsize += nla_total_size_64bit(sizeof(struct ovs_dp_megaflow_stats));
Daniele Di Proietto45fb9c32014-01-23 10:47:35 -08001462 msgsize += nla_total_size(sizeof(u32)); /* OVS_DP_ATTR_USER_FEATURES */
Thomas Grafc3ff8cf2013-03-29 14:46:49 +01001463
1464 return msgsize;
1465}
1466
Pravin B Shelar8ec609d2014-11-11 15:55:16 -08001467/* Called with ovs_mutex. */
Jesse Grossccb13522011-10-25 19:26:31 -07001468static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001469 u32 portid, u32 seq, u32 flags, u8 cmd)
Jesse Grossccb13522011-10-25 19:26:31 -07001470{
1471 struct ovs_header *ovs_header;
1472 struct ovs_dp_stats dp_stats;
Andy Zhou1bd71162013-10-22 10:42:46 -07001473 struct ovs_dp_megaflow_stats dp_megaflow_stats;
Jesse Grossccb13522011-10-25 19:26:31 -07001474 int err;
1475
Eric W. Biederman15e47302012-09-07 20:12:54 +00001476 ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
Jesse Grossccb13522011-10-25 19:26:31 -07001477 flags, cmd);
1478 if (!ovs_header)
1479 goto error;
1480
1481 ovs_header->dp_ifindex = get_dpifindex(dp);
1482
Jesse Grossccb13522011-10-25 19:26:31 -07001483 err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
Jesse Grossccb13522011-10-25 19:26:31 -07001484 if (err)
1485 goto nla_put_failure;
1486
Andy Zhou1bd71162013-10-22 10:42:46 -07001487 get_dp_stats(dp, &dp_stats, &dp_megaflow_stats);
Nicolas Dichtel66c7a5e2016-04-26 10:06:15 +02001488 if (nla_put_64bit(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats),
1489 &dp_stats, OVS_DP_ATTR_PAD))
Andy Zhou1bd71162013-10-22 10:42:46 -07001490 goto nla_put_failure;
1491
Nicolas Dichtel66c7a5e2016-04-26 10:06:15 +02001492 if (nla_put_64bit(skb, OVS_DP_ATTR_MEGAFLOW_STATS,
1493 sizeof(struct ovs_dp_megaflow_stats),
1494 &dp_megaflow_stats, OVS_DP_ATTR_PAD))
David S. Miller028d6a62012-03-29 23:20:48 -04001495 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001496
Thomas Graf43d4be92013-12-13 15:22:18 +01001497 if (nla_put_u32(skb, OVS_DP_ATTR_USER_FEATURES, dp->user_features))
1498 goto nla_put_failure;
1499
Johannes Berg053c0952015-01-16 22:09:00 +01001500 genlmsg_end(skb, ovs_header);
1501 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001502
1503nla_put_failure:
1504 genlmsg_cancel(skb, ovs_header);
1505error:
1506 return -EMSGSIZE;
1507}
1508
Florian Westphal263ea092016-02-18 15:03:26 +01001509static struct sk_buff *ovs_dp_cmd_alloc_info(void)
Jesse Grossccb13522011-10-25 19:26:31 -07001510{
Florian Westphal551ddc02016-02-18 15:03:25 +01001511 return genlmsg_new(ovs_dp_cmd_msg_size(), GFP_KERNEL);
Jesse Grossccb13522011-10-25 19:26:31 -07001512}
1513
Jarno Rajahalmebb6f9a72014-05-05 11:32:17 -07001514/* Called with rcu_read_lock or ovs_mutex. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001515static struct datapath *lookup_datapath(struct net *net,
Thomas Graf12eb18f2014-11-06 06:58:52 -08001516 const struct ovs_header *ovs_header,
Jesse Grossccb13522011-10-25 19:26:31 -07001517 struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1518{
1519 struct datapath *dp;
1520
1521 if (!a[OVS_DP_ATTR_NAME])
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001522 dp = get_dp(net, ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001523 else {
1524 struct vport *vport;
1525
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001526 vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
Jesse Grossccb13522011-10-25 19:26:31 -07001527 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
Jesse Grossccb13522011-10-25 19:26:31 -07001528 }
1529 return dp ? dp : ERR_PTR(-ENODEV);
1530}
1531
Thomas Graf44da5ae2013-12-13 15:22:19 +01001532static void ovs_dp_reset_user_features(struct sk_buff *skb, struct genl_info *info)
1533{
1534 struct datapath *dp;
1535
1536 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
Jiri Pirko3c7eacf2014-02-14 11:42:36 +01001537 if (IS_ERR(dp))
Thomas Graf44da5ae2013-12-13 15:22:19 +01001538 return;
1539
1540 WARN(dp->user_features, "Dropping previously announced user features\n");
1541 dp->user_features = 0;
1542}
1543
Thomas Graf12eb18f2014-11-06 06:58:52 -08001544static void ovs_dp_change(struct datapath *dp, struct nlattr *a[])
Thomas Graf43d4be92013-12-13 15:22:18 +01001545{
1546 if (a[OVS_DP_ATTR_USER_FEATURES])
1547 dp->user_features = nla_get_u32(a[OVS_DP_ATTR_USER_FEATURES]);
1548}
1549
Jesse Grossccb13522011-10-25 19:26:31 -07001550static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1551{
1552 struct nlattr **a = info->attrs;
1553 struct vport_parms parms;
1554 struct sk_buff *reply;
1555 struct datapath *dp;
1556 struct vport *vport;
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001557 struct ovs_net *ovs_net;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001558 int err, i;
Jesse Grossccb13522011-10-25 19:26:31 -07001559
1560 err = -EINVAL;
1561 if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1562 goto err;
1563
Florian Westphal263ea092016-02-18 15:03:26 +01001564 reply = ovs_dp_cmd_alloc_info();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001565 if (!reply)
1566 return -ENOMEM;
Jesse Grossccb13522011-10-25 19:26:31 -07001567
1568 err = -ENOMEM;
1569 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1570 if (dp == NULL)
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001571 goto err_free_reply;
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001572
Eric W. Biedermanefd7ef12015-03-11 23:04:08 -05001573 ovs_dp_set_net(dp, sock_net(skb->sk));
Jesse Grossccb13522011-10-25 19:26:31 -07001574
1575 /* Allocate table. */
Pravin B Shelarb637e492013-10-04 00:14:23 -07001576 err = ovs_flow_tbl_init(&dp->table);
1577 if (err)
Jesse Grossccb13522011-10-25 19:26:31 -07001578 goto err_free_dp;
1579
WANG Cong1c213bd2014-02-13 11:46:28 -08001580 dp->stats_percpu = netdev_alloc_pcpu_stats(struct dp_stats_percpu);
Jesse Grossccb13522011-10-25 19:26:31 -07001581 if (!dp->stats_percpu) {
1582 err = -ENOMEM;
1583 goto err_destroy_table;
1584 }
1585
Kees Cook6da2ec52018-06-12 13:55:00 -07001586 dp->ports = kmalloc_array(DP_VPORT_HASH_BUCKETS,
1587 sizeof(struct hlist_head),
1588 GFP_KERNEL);
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001589 if (!dp->ports) {
1590 err = -ENOMEM;
1591 goto err_destroy_percpu;
1592 }
1593
1594 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1595 INIT_HLIST_HEAD(&dp->ports[i]);
1596
Andy Zhou96fbc132017-11-10 12:09:42 -08001597 err = ovs_meters_init(dp);
1598 if (err)
1599 goto err_destroy_ports_array;
1600
Jesse Grossccb13522011-10-25 19:26:31 -07001601 /* Set up our datapath device. */
1602 parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1603 parms.type = OVS_VPORT_TYPE_INTERNAL;
1604 parms.options = NULL;
1605 parms.dp = dp;
1606 parms.port_no = OVSP_LOCAL;
Alex Wang5cd667b2014-07-17 15:14:13 -07001607 parms.upcall_portids = a[OVS_DP_ATTR_UPCALL_PID];
Jesse Grossccb13522011-10-25 19:26:31 -07001608
Thomas Graf43d4be92013-12-13 15:22:18 +01001609 ovs_dp_change(dp, a);
1610
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001611 /* So far only local changes have been made, now need the lock. */
1612 ovs_lock();
1613
Jesse Grossccb13522011-10-25 19:26:31 -07001614 vport = new_vport(&parms);
1615 if (IS_ERR(vport)) {
1616 err = PTR_ERR(vport);
1617 if (err == -EBUSY)
1618 err = -EEXIST;
1619
Thomas Graf44da5ae2013-12-13 15:22:19 +01001620 if (err == -EEXIST) {
1621 /* An outdated user space instance that does not understand
1622 * the concept of user_features has attempted to create a new
1623 * datapath and is likely to reuse it. Drop all user features.
1624 */
1625 if (info->genlhdr->version < OVS_DP_VER_FEATURES)
1626 ovs_dp_reset_user_features(skb, info);
1627 }
1628
Andy Zhou96fbc132017-11-10 12:09:42 -08001629 goto err_destroy_meters;
Jesse Grossccb13522011-10-25 19:26:31 -07001630 }
1631
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001632 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1633 info->snd_seq, 0, OVS_DP_CMD_NEW);
1634 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001635
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001636 ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001637 list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001638
1639 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001640
Johannes Berg2a94fe42013-11-19 15:19:39 +01001641 ovs_notify(&dp_datapath_genl_family, reply, info);
Jesse Grossccb13522011-10-25 19:26:31 -07001642 return 0;
1643
Andy Zhou96fbc132017-11-10 12:09:42 -08001644err_destroy_meters:
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001645 ovs_unlock();
Andy Zhou96fbc132017-11-10 12:09:42 -08001646 ovs_meters_exit(dp);
1647err_destroy_ports_array:
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001648 kfree(dp->ports);
Jesse Grossccb13522011-10-25 19:26:31 -07001649err_destroy_percpu:
1650 free_percpu(dp->stats_percpu);
1651err_destroy_table:
Pravin B Shelar9b996e52014-05-06 18:41:20 -07001652 ovs_flow_tbl_destroy(&dp->table);
Jesse Grossccb13522011-10-25 19:26:31 -07001653err_free_dp:
1654 kfree(dp);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001655err_free_reply:
1656 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001657err:
1658 return err;
1659}
1660
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001661/* Called with ovs_mutex. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001662static void __dp_destroy(struct datapath *dp)
Jesse Grossccb13522011-10-25 19:26:31 -07001663{
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001664 int i;
Jesse Grossccb13522011-10-25 19:26:31 -07001665
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001666 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1667 struct vport *vport;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001668 struct hlist_node *n;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001669
Sasha Levinb67bfe02013-02-27 17:06:00 -08001670 hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001671 if (vport->port_no != OVSP_LOCAL)
1672 ovs_dp_detach_port(vport);
1673 }
Jesse Grossccb13522011-10-25 19:26:31 -07001674
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001675 list_del_rcu(&dp->list_node);
Jesse Grossccb13522011-10-25 19:26:31 -07001676
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001677 /* OVSP_LOCAL is datapath internal port. We need to make sure that
Andy Zhoue80857c2014-01-21 09:31:04 -08001678 * all ports in datapath are destroyed first before freeing datapath.
Jesse Grossccb13522011-10-25 19:26:31 -07001679 */
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001680 ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
Jesse Grossccb13522011-10-25 19:26:31 -07001681
Andy Zhoue80857c2014-01-21 09:31:04 -08001682 /* RCU destroy the flow table */
Jesse Grossccb13522011-10-25 19:26:31 -07001683 call_rcu(&dp->rcu, destroy_dp_rcu);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001684}
1685
1686static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1687{
1688 struct sk_buff *reply;
1689 struct datapath *dp;
1690 int err;
1691
Florian Westphal263ea092016-02-18 15:03:26 +01001692 reply = ovs_dp_cmd_alloc_info();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001693 if (!reply)
1694 return -ENOMEM;
1695
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001696 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001697 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1698 err = PTR_ERR(dp);
1699 if (IS_ERR(dp))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001700 goto err_unlock_free;
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001701
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001702 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
1703 info->snd_seq, 0, OVS_DP_CMD_DEL);
1704 BUG_ON(err < 0);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001705
1706 __dp_destroy(dp);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001707 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001708
Johannes Berg2a94fe42013-11-19 15:19:39 +01001709 ovs_notify(&dp_datapath_genl_family, reply, info);
Jesse Grossccb13522011-10-25 19:26:31 -07001710
1711 return 0;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001712
1713err_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001714 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001715 kfree_skb(reply);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001716 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001717}
1718
1719static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1720{
1721 struct sk_buff *reply;
1722 struct datapath *dp;
1723 int err;
1724
Florian Westphal263ea092016-02-18 15:03:26 +01001725 reply = ovs_dp_cmd_alloc_info();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001726 if (!reply)
1727 return -ENOMEM;
1728
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001729 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001730 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001731 err = PTR_ERR(dp);
Jesse Grossccb13522011-10-25 19:26:31 -07001732 if (IS_ERR(dp))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001733 goto err_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07001734
Thomas Graf43d4be92013-12-13 15:22:18 +01001735 ovs_dp_change(dp, info->attrs);
1736
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001737 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
Yifeng Sun804fe102018-09-26 11:40:14 -07001738 info->snd_seq, 0, OVS_DP_CMD_SET);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001739 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001740
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001741 ovs_unlock();
Johannes Berg2a94fe42013-11-19 15:19:39 +01001742 ovs_notify(&dp_datapath_genl_family, reply, info);
Jesse Grossccb13522011-10-25 19:26:31 -07001743
1744 return 0;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001745
1746err_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001747 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001748 kfree_skb(reply);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001749 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001750}
1751
1752static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1753{
1754 struct sk_buff *reply;
1755 struct datapath *dp;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001756 int err;
Jesse Grossccb13522011-10-25 19:26:31 -07001757
Florian Westphal263ea092016-02-18 15:03:26 +01001758 reply = ovs_dp_cmd_alloc_info();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001759 if (!reply)
1760 return -ENOMEM;
1761
Pravin B Shelar8ec609d2014-11-11 15:55:16 -08001762 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001763 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001764 if (IS_ERR(dp)) {
1765 err = PTR_ERR(dp);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001766 goto err_unlock_free;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001767 }
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001768 err = ovs_dp_cmd_fill_info(dp, reply, info->snd_portid,
Yifeng Sun804fe102018-09-26 11:40:14 -07001769 info->snd_seq, 0, OVS_DP_CMD_GET);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001770 BUG_ON(err < 0);
Pravin B Shelar8ec609d2014-11-11 15:55:16 -08001771 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001772
Jesse Grossccb13522011-10-25 19:26:31 -07001773 return genlmsg_reply(reply, info);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001774
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001775err_unlock_free:
Pravin B Shelar8ec609d2014-11-11 15:55:16 -08001776 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001777 kfree_skb(reply);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001778 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001779}
1780
1781static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1782{
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001783 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
Jesse Grossccb13522011-10-25 19:26:31 -07001784 struct datapath *dp;
1785 int skip = cb->args[0];
1786 int i = 0;
1787
Pravin B Shelar8ec609d2014-11-11 15:55:16 -08001788 ovs_lock();
1789 list_for_each_entry(dp, &ovs_net->dps, list_node) {
Ben Pfaff77676fd2012-01-17 13:33:39 +00001790 if (i >= skip &&
Eric W. Biederman15e47302012-09-07 20:12:54 +00001791 ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001792 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Yifeng Sun804fe102018-09-26 11:40:14 -07001793 OVS_DP_CMD_GET) < 0)
Jesse Grossccb13522011-10-25 19:26:31 -07001794 break;
1795 i++;
1796 }
Pravin B Shelar8ec609d2014-11-11 15:55:16 -08001797 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001798
1799 cb->args[0] = i;
1800
1801 return skb->len;
1802}
1803
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001804static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1805 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1806 [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1807 [OVS_DP_ATTR_USER_FEATURES] = { .type = NLA_U32 },
1808};
1809
stephen hemminger48e48a72014-07-16 11:25:52 -07001810static const struct genl_ops dp_datapath_genl_ops[] = {
Jesse Grossccb13522011-10-25 19:26:31 -07001811 { .cmd = OVS_DP_CMD_NEW,
Johannes Bergef6243a2019-04-26 14:07:31 +02001812 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Tycho Andersen4a926022016-02-05 09:20:52 -07001813 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
Jesse Grossccb13522011-10-25 19:26:31 -07001814 .doit = ovs_dp_cmd_new
1815 },
1816 { .cmd = OVS_DP_CMD_DEL,
Johannes Bergef6243a2019-04-26 14:07:31 +02001817 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Tycho Andersen4a926022016-02-05 09:20:52 -07001818 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
Jesse Grossccb13522011-10-25 19:26:31 -07001819 .doit = ovs_dp_cmd_del
1820 },
1821 { .cmd = OVS_DP_CMD_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +02001822 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jesse Grossccb13522011-10-25 19:26:31 -07001823 .flags = 0, /* OK for unprivileged users. */
Jesse Grossccb13522011-10-25 19:26:31 -07001824 .doit = ovs_dp_cmd_get,
1825 .dumpit = ovs_dp_cmd_dump
1826 },
1827 { .cmd = OVS_DP_CMD_SET,
Johannes Bergef6243a2019-04-26 14:07:31 +02001828 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Tycho Andersen4a926022016-02-05 09:20:52 -07001829 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
Jesse Grossccb13522011-10-25 19:26:31 -07001830 .doit = ovs_dp_cmd_set,
1831 },
1832};
1833
Johannes Berg56989f62016-10-24 14:40:05 +02001834static struct genl_family dp_datapath_genl_family __ro_after_init = {
Jesse Grossccb13522011-10-25 19:26:31 -07001835 .hdrsize = sizeof(struct ovs_header),
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001836 .name = OVS_DATAPATH_FAMILY,
1837 .version = OVS_DATAPATH_VERSION,
1838 .maxattr = OVS_DP_ATTR_MAX,
Johannes Berg3b0f31f2019-03-21 22:51:02 +01001839 .policy = datapath_policy,
Pravin B Shelar3a4e0d62013-04-23 07:48:48 +00001840 .netnsok = true,
1841 .parallel_ops = true,
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07001842 .ops = dp_datapath_genl_ops,
1843 .n_ops = ARRAY_SIZE(dp_datapath_genl_ops),
1844 .mcgrps = &ovs_dp_datapath_multicast_group,
1845 .n_mcgrps = 1,
Johannes Berg489111e2016-10-24 14:40:03 +02001846 .module = THIS_MODULE,
Jesse Grossccb13522011-10-25 19:26:31 -07001847};
1848
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001849/* Called with ovs_mutex or RCU read lock. */
Jesse Grossccb13522011-10-25 19:26:31 -07001850static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
Jiri Benc9354d452017-11-02 17:04:37 -02001851 struct net *net, u32 portid, u32 seq,
1852 u32 flags, u8 cmd)
Jesse Grossccb13522011-10-25 19:26:31 -07001853{
1854 struct ovs_header *ovs_header;
1855 struct ovs_vport_stats vport_stats;
1856 int err;
1857
Eric W. Biederman15e47302012-09-07 20:12:54 +00001858 ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
Jesse Grossccb13522011-10-25 19:26:31 -07001859 flags, cmd);
1860 if (!ovs_header)
1861 return -EMSGSIZE;
1862
1863 ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1864
David S. Miller028d6a62012-03-29 23:20:48 -04001865 if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
1866 nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
Alex Wang5cd667b2014-07-17 15:14:13 -07001867 nla_put_string(skb, OVS_VPORT_ATTR_NAME,
Jiri Benc9354d452017-11-02 17:04:37 -02001868 ovs_vport_name(vport)) ||
1869 nla_put_u32(skb, OVS_VPORT_ATTR_IFINDEX, vport->dev->ifindex))
David S. Miller028d6a62012-03-29 23:20:48 -04001870 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001871
Jiri Benc9354d452017-11-02 17:04:37 -02001872 if (!net_eq(net, dev_net(vport->dev))) {
1873 int id = peernet2id_alloc(net, dev_net(vport->dev));
1874
1875 if (nla_put_s32(skb, OVS_VPORT_ATTR_NETNSID, id))
1876 goto nla_put_failure;
1877 }
1878
Jesse Grossccb13522011-10-25 19:26:31 -07001879 ovs_vport_get_stats(vport, &vport_stats);
Nicolas Dichtel66c7a5e2016-04-26 10:06:15 +02001880 if (nla_put_64bit(skb, OVS_VPORT_ATTR_STATS,
1881 sizeof(struct ovs_vport_stats), &vport_stats,
1882 OVS_VPORT_ATTR_PAD))
David S. Miller028d6a62012-03-29 23:20:48 -04001883 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001884
Alex Wang5cd667b2014-07-17 15:14:13 -07001885 if (ovs_vport_get_upcall_portids(vport, skb))
1886 goto nla_put_failure;
1887
Jesse Grossccb13522011-10-25 19:26:31 -07001888 err = ovs_vport_get_options(vport, skb);
1889 if (err == -EMSGSIZE)
1890 goto error;
1891
Johannes Berg053c0952015-01-16 22:09:00 +01001892 genlmsg_end(skb, ovs_header);
1893 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001894
1895nla_put_failure:
1896 err = -EMSGSIZE;
1897error:
1898 genlmsg_cancel(skb, ovs_header);
1899 return err;
1900}
1901
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07001902static struct sk_buff *ovs_vport_cmd_alloc_info(void)
1903{
1904 return nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1905}
1906
1907/* Called with ovs_mutex, only via ovs_dp_notify_wq(). */
Jiri Benc9354d452017-11-02 17:04:37 -02001908struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, struct net *net,
1909 u32 portid, u32 seq, u8 cmd)
Jesse Grossccb13522011-10-25 19:26:31 -07001910{
1911 struct sk_buff *skb;
1912 int retval;
1913
1914 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1915 if (!skb)
1916 return ERR_PTR(-ENOMEM);
1917
Jiri Benc9354d452017-11-02 17:04:37 -02001918 retval = ovs_vport_cmd_fill_info(vport, skb, net, portid, seq, 0, cmd);
Jesse Grossa9341512013-03-26 15:48:38 -07001919 BUG_ON(retval < 0);
1920
Jesse Grossccb13522011-10-25 19:26:31 -07001921 return skb;
1922}
1923
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001924/* Called with ovs_mutex or RCU read lock. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001925static struct vport *lookup_vport(struct net *net,
Thomas Graf12eb18f2014-11-06 06:58:52 -08001926 const struct ovs_header *ovs_header,
Jesse Grossccb13522011-10-25 19:26:31 -07001927 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1928{
1929 struct datapath *dp;
1930 struct vport *vport;
1931
Jiri Benc9354d452017-11-02 17:04:37 -02001932 if (a[OVS_VPORT_ATTR_IFINDEX])
1933 return ERR_PTR(-EOPNOTSUPP);
Jesse Grossccb13522011-10-25 19:26:31 -07001934 if (a[OVS_VPORT_ATTR_NAME]) {
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001935 vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
Jesse Grossccb13522011-10-25 19:26:31 -07001936 if (!vport)
1937 return ERR_PTR(-ENODEV);
Ben Pfaff651a68e2012-03-06 15:04:04 -08001938 if (ovs_header->dp_ifindex &&
1939 ovs_header->dp_ifindex != get_dpifindex(vport->dp))
1940 return ERR_PTR(-ENODEV);
Jesse Grossccb13522011-10-25 19:26:31 -07001941 return vport;
1942 } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
1943 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1944
1945 if (port_no >= DP_MAX_PORTS)
1946 return ERR_PTR(-EFBIG);
1947
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001948 dp = get_dp(net, ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001949 if (!dp)
1950 return ERR_PTR(-ENODEV);
1951
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001952 vport = ovs_vport_ovsl_rcu(dp, port_no);
Jesse Grossccb13522011-10-25 19:26:31 -07001953 if (!vport)
Jarno Rajahalme14408db2013-01-09 14:27:35 -08001954 return ERR_PTR(-ENODEV);
Jesse Grossccb13522011-10-25 19:26:31 -07001955 return vport;
1956 } else
1957 return ERR_PTR(-EINVAL);
Jiri Benc9354d452017-11-02 17:04:37 -02001958
Jesse Grossccb13522011-10-25 19:26:31 -07001959}
1960
Taehee Yoo6b660c42019-07-06 01:08:09 +09001961static unsigned int ovs_get_max_headroom(struct datapath *dp)
Paolo Abeni3a927bc2016-02-26 10:45:39 +01001962{
Taehee Yoo6b660c42019-07-06 01:08:09 +09001963 unsigned int dev_headroom, max_headroom = 0;
Paolo Abeni3a927bc2016-02-26 10:45:39 +01001964 struct net_device *dev;
1965 struct vport *vport;
1966 int i;
1967
1968 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1969 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
1970 dev = vport->dev;
1971 dev_headroom = netdev_get_fwd_headroom(dev);
1972 if (dev_headroom > max_headroom)
1973 max_headroom = dev_headroom;
1974 }
1975 }
1976
Taehee Yoo6b660c42019-07-06 01:08:09 +09001977 return max_headroom;
1978}
1979
1980/* Called with ovs_mutex */
1981static void ovs_update_headroom(struct datapath *dp, unsigned int new_headroom)
1982{
1983 struct vport *vport;
1984 int i;
1985
1986 dp->max_headroom = new_headroom;
Paolo Abeni3a927bc2016-02-26 10:45:39 +01001987 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1988 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node)
Taehee Yoo6b660c42019-07-06 01:08:09 +09001989 netdev_set_rx_headroom(vport->dev, new_headroom);
Paolo Abeni3a927bc2016-02-26 10:45:39 +01001990}
1991
Jesse Grossccb13522011-10-25 19:26:31 -07001992static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
1993{
1994 struct nlattr **a = info->attrs;
1995 struct ovs_header *ovs_header = info->userhdr;
1996 struct vport_parms parms;
1997 struct sk_buff *reply;
1998 struct vport *vport;
1999 struct datapath *dp;
Taehee Yoo6b660c42019-07-06 01:08:09 +09002000 unsigned int new_headroom;
Jesse Grossccb13522011-10-25 19:26:31 -07002001 u32 port_no;
2002 int err;
2003
Jesse Grossccb13522011-10-25 19:26:31 -07002004 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
2005 !a[OVS_VPORT_ATTR_UPCALL_PID])
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07002006 return -EINVAL;
Jiri Benc9354d452017-11-02 17:04:37 -02002007 if (a[OVS_VPORT_ATTR_IFINDEX])
2008 return -EOPNOTSUPP;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07002009
2010 port_no = a[OVS_VPORT_ATTR_PORT_NO]
2011 ? nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]) : 0;
2012 if (port_no >= DP_MAX_PORTS)
2013 return -EFBIG;
2014
2015 reply = ovs_vport_cmd_alloc_info();
2016 if (!reply)
2017 return -ENOMEM;
Jesse Grossccb13522011-10-25 19:26:31 -07002018
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002019 ovs_lock();
Thomas Graf62b9c8d2014-10-22 17:29:06 +02002020restart:
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002021 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07002022 err = -ENODEV;
2023 if (!dp)
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07002024 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07002025
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07002026 if (port_no) {
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002027 vport = ovs_vport_ovsl(dp, port_no);
Jesse Grossccb13522011-10-25 19:26:31 -07002028 err = -EBUSY;
2029 if (vport)
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07002030 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07002031 } else {
2032 for (port_no = 1; ; port_no++) {
2033 if (port_no >= DP_MAX_PORTS) {
2034 err = -EFBIG;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07002035 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07002036 }
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002037 vport = ovs_vport_ovsl(dp, port_no);
Jesse Grossccb13522011-10-25 19:26:31 -07002038 if (!vport)
2039 break;
2040 }
2041 }
2042
2043 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
2044 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
2045 parms.options = a[OVS_VPORT_ATTR_OPTIONS];
2046 parms.dp = dp;
2047 parms.port_no = port_no;
Alex Wang5cd667b2014-07-17 15:14:13 -07002048 parms.upcall_portids = a[OVS_VPORT_ATTR_UPCALL_PID];
Jesse Grossccb13522011-10-25 19:26:31 -07002049
2050 vport = new_vport(&parms);
2051 err = PTR_ERR(vport);
Thomas Graf62b9c8d2014-10-22 17:29:06 +02002052 if (IS_ERR(vport)) {
2053 if (err == -EAGAIN)
2054 goto restart;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07002055 goto exit_unlock_free;
Thomas Graf62b9c8d2014-10-22 17:29:06 +02002056 }
Jesse Grossccb13522011-10-25 19:26:31 -07002057
Jiri Benc9354d452017-11-02 17:04:37 -02002058 err = ovs_vport_cmd_fill_info(vport, reply, genl_info_net(info),
2059 info->snd_portid, info->snd_seq, 0,
2060 OVS_VPORT_CMD_NEW);
Paolo Abeni3a927bc2016-02-26 10:45:39 +01002061
Taehee Yoo6b660c42019-07-06 01:08:09 +09002062 new_headroom = netdev_get_fwd_headroom(vport->dev);
2063
2064 if (new_headroom > dp->max_headroom)
2065 ovs_update_headroom(dp, new_headroom);
Paolo Abeni3a927bc2016-02-26 10:45:39 +01002066 else
2067 netdev_set_rx_headroom(vport->dev, dp->max_headroom);
2068
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07002069 BUG_ON(err < 0);
2070 ovs_unlock();
Thomas Grafed661182013-03-29 14:46:50 +01002071
Johannes Berg2a94fe42013-11-19 15:19:39 +01002072 ovs_notify(&dp_vport_genl_family, reply, info);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07002073 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -07002074
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07002075exit_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002076 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07002077 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07002078 return err;
2079}
2080
2081static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
2082{
2083 struct nlattr **a = info->attrs;
2084 struct sk_buff *reply;
2085 struct vport *vport;
2086 int err;
2087
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07002088 reply = ovs_vport_cmd_alloc_info();
2089 if (!reply)
2090 return -ENOMEM;
2091
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002092 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002093 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
Jesse Grossccb13522011-10-25 19:26:31 -07002094 err = PTR_ERR(vport);
2095 if (IS_ERR(vport))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07002096 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07002097
Jesse Grossccb13522011-10-25 19:26:31 -07002098 if (a[OVS_VPORT_ATTR_TYPE] &&
Jesse Grossf44f3402013-05-13 08:15:26 -07002099 nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
Jesse Grossccb13522011-10-25 19:26:31 -07002100 err = -EINVAL;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07002101 goto exit_unlock_free;
Jesse Grossa9341512013-03-26 15:48:38 -07002102 }
2103
Jesse Grossf44f3402013-05-13 08:15:26 -07002104 if (a[OVS_VPORT_ATTR_OPTIONS]) {
Jesse Grossccb13522011-10-25 19:26:31 -07002105 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
Jesse Grossf44f3402013-05-13 08:15:26 -07002106 if (err)
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07002107 goto exit_unlock_free;
Jesse Grossf44f3402013-05-13 08:15:26 -07002108 }
Jesse Grossa9341512013-03-26 15:48:38 -07002109
Alex Wang5cd667b2014-07-17 15:14:13 -07002110
2111 if (a[OVS_VPORT_ATTR_UPCALL_PID]) {
2112 struct nlattr *ids = a[OVS_VPORT_ATTR_UPCALL_PID];
2113
2114 err = ovs_vport_set_upcall_portids(vport, ids);
2115 if (err)
2116 goto exit_unlock_free;
2117 }
Jesse Grossccb13522011-10-25 19:26:31 -07002118
Jiri Benc9354d452017-11-02 17:04:37 -02002119 err = ovs_vport_cmd_fill_info(vport, reply, genl_info_net(info),
2120 info->snd_portid, info->snd_seq, 0,
Yifeng Sun804fe102018-09-26 11:40:14 -07002121 OVS_VPORT_CMD_SET);
Jesse Grossa9341512013-03-26 15:48:38 -07002122 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07002123
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002124 ovs_unlock();
Johannes Berg2a94fe42013-11-19 15:19:39 +01002125 ovs_notify(&dp_vport_genl_family, reply, info);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002126 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -07002127
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07002128exit_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002129 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07002130 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07002131 return err;
2132}
2133
2134static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
2135{
Taehee Yoo6b660c42019-07-06 01:08:09 +09002136 bool update_headroom = false;
Jesse Grossccb13522011-10-25 19:26:31 -07002137 struct nlattr **a = info->attrs;
2138 struct sk_buff *reply;
Paolo Abeni3a927bc2016-02-26 10:45:39 +01002139 struct datapath *dp;
Jesse Grossccb13522011-10-25 19:26:31 -07002140 struct vport *vport;
Taehee Yoo6b660c42019-07-06 01:08:09 +09002141 unsigned int new_headroom;
Jesse Grossccb13522011-10-25 19:26:31 -07002142 int err;
2143
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07002144 reply = ovs_vport_cmd_alloc_info();
2145 if (!reply)
2146 return -ENOMEM;
2147
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002148 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002149 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
Jesse Grossccb13522011-10-25 19:26:31 -07002150 err = PTR_ERR(vport);
2151 if (IS_ERR(vport))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07002152 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07002153
2154 if (vport->port_no == OVSP_LOCAL) {
2155 err = -EINVAL;
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07002156 goto exit_unlock_free;
Jesse Grossccb13522011-10-25 19:26:31 -07002157 }
2158
Jiri Benc9354d452017-11-02 17:04:37 -02002159 err = ovs_vport_cmd_fill_info(vport, reply, genl_info_net(info),
2160 info->snd_portid, info->snd_seq, 0,
2161 OVS_VPORT_CMD_DEL);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07002162 BUG_ON(err < 0);
Paolo Abeni3a927bc2016-02-26 10:45:39 +01002163
2164 /* the vport deletion may trigger dp headroom update */
2165 dp = vport->dp;
2166 if (netdev_get_fwd_headroom(vport->dev) == dp->max_headroom)
Taehee Yoo6b660c42019-07-06 01:08:09 +09002167 update_headroom = true;
2168
Paolo Abeni3a927bc2016-02-26 10:45:39 +01002169 netdev_reset_rx_headroom(vport->dev);
Jesse Grossccb13522011-10-25 19:26:31 -07002170 ovs_dp_detach_port(vport);
Paolo Abeni3a927bc2016-02-26 10:45:39 +01002171
Taehee Yoo6b660c42019-07-06 01:08:09 +09002172 if (update_headroom) {
2173 new_headroom = ovs_get_max_headroom(dp);
2174
2175 if (new_headroom < dp->max_headroom)
2176 ovs_update_headroom(dp, new_headroom);
2177 }
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07002178 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07002179
Johannes Berg2a94fe42013-11-19 15:19:39 +01002180 ovs_notify(&dp_vport_genl_family, reply, info);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07002181 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -07002182
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07002183exit_unlock_free:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002184 ovs_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07002185 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07002186 return err;
2187}
2188
2189static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
2190{
2191 struct nlattr **a = info->attrs;
2192 struct ovs_header *ovs_header = info->userhdr;
2193 struct sk_buff *reply;
2194 struct vport *vport;
2195 int err;
2196
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07002197 reply = ovs_vport_cmd_alloc_info();
2198 if (!reply)
2199 return -ENOMEM;
2200
Jesse Grossccb13522011-10-25 19:26:31 -07002201 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002202 vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
Jesse Grossccb13522011-10-25 19:26:31 -07002203 err = PTR_ERR(vport);
2204 if (IS_ERR(vport))
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07002205 goto exit_unlock_free;
Jiri Benc9354d452017-11-02 17:04:37 -02002206 err = ovs_vport_cmd_fill_info(vport, reply, genl_info_net(info),
2207 info->snd_portid, info->snd_seq, 0,
Yifeng Sun804fe102018-09-26 11:40:14 -07002208 OVS_VPORT_CMD_GET);
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07002209 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07002210 rcu_read_unlock();
2211
2212 return genlmsg_reply(reply, info);
2213
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07002214exit_unlock_free:
Jesse Grossccb13522011-10-25 19:26:31 -07002215 rcu_read_unlock();
Jarno Rajahalme6093ae92014-05-05 14:13:32 -07002216 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07002217 return err;
2218}
2219
2220static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
2221{
2222 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
2223 struct datapath *dp;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07002224 int bucket = cb->args[0], skip = cb->args[1];
2225 int i, j = 0;
Jesse Grossccb13522011-10-25 19:26:31 -07002226
Jesse Grossccb13522011-10-25 19:26:31 -07002227 rcu_read_lock();
Andy Zhoucc3a5ae2014-09-08 13:14:22 -07002228 dp = get_dp_rcu(sock_net(skb->sk), ovs_header->dp_ifindex);
Jarno Rajahalme42ee19e2014-02-15 17:42:29 -08002229 if (!dp) {
2230 rcu_read_unlock();
2231 return -ENODEV;
2232 }
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07002233 for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
Jesse Grossccb13522011-10-25 19:26:31 -07002234 struct vport *vport;
2235
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07002236 j = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -08002237 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07002238 if (j >= skip &&
2239 ovs_vport_cmd_fill_info(vport, skb,
Jiri Benc9354d452017-11-02 17:04:37 -02002240 sock_net(skb->sk),
Eric W. Biederman15e47302012-09-07 20:12:54 +00002241 NETLINK_CB(cb->skb).portid,
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07002242 cb->nlh->nlmsg_seq,
2243 NLM_F_MULTI,
Yifeng Sun804fe102018-09-26 11:40:14 -07002244 OVS_VPORT_CMD_GET) < 0)
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07002245 goto out;
Jesse Grossccb13522011-10-25 19:26:31 -07002246
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07002247 j++;
2248 }
2249 skip = 0;
Jesse Grossccb13522011-10-25 19:26:31 -07002250 }
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07002251out:
Jesse Grossccb13522011-10-25 19:26:31 -07002252 rcu_read_unlock();
2253
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07002254 cb->args[0] = i;
2255 cb->args[1] = j;
Jesse Grossccb13522011-10-25 19:26:31 -07002256
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07002257 return skb->len;
Jesse Grossccb13522011-10-25 19:26:31 -07002258}
2259
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002260static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
2261 [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
2262 [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
2263 [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
2264 [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
2265 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
2266 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
Jiri Benc9354d452017-11-02 17:04:37 -02002267 [OVS_VPORT_ATTR_IFINDEX] = { .type = NLA_U32 },
2268 [OVS_VPORT_ATTR_NETNSID] = { .type = NLA_S32 },
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002269};
2270
stephen hemminger48e48a72014-07-16 11:25:52 -07002271static const struct genl_ops dp_vport_genl_ops[] = {
Jesse Grossccb13522011-10-25 19:26:31 -07002272 { .cmd = OVS_VPORT_CMD_NEW,
Johannes Bergef6243a2019-04-26 14:07:31 +02002273 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Tycho Andersen4a926022016-02-05 09:20:52 -07002274 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
Jesse Grossccb13522011-10-25 19:26:31 -07002275 .doit = ovs_vport_cmd_new
2276 },
2277 { .cmd = OVS_VPORT_CMD_DEL,
Johannes Bergef6243a2019-04-26 14:07:31 +02002278 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Tycho Andersen4a926022016-02-05 09:20:52 -07002279 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
Jesse Grossccb13522011-10-25 19:26:31 -07002280 .doit = ovs_vport_cmd_del
2281 },
2282 { .cmd = OVS_VPORT_CMD_GET,
Johannes Bergef6243a2019-04-26 14:07:31 +02002283 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Jesse Grossccb13522011-10-25 19:26:31 -07002284 .flags = 0, /* OK for unprivileged users. */
Jesse Grossccb13522011-10-25 19:26:31 -07002285 .doit = ovs_vport_cmd_get,
2286 .dumpit = ovs_vport_cmd_dump
2287 },
2288 { .cmd = OVS_VPORT_CMD_SET,
Johannes Bergef6243a2019-04-26 14:07:31 +02002289 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
Tycho Andersen4a926022016-02-05 09:20:52 -07002290 .flags = GENL_UNS_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
Jesse Grossccb13522011-10-25 19:26:31 -07002291 .doit = ovs_vport_cmd_set,
2292 },
2293};
2294
Johannes Berg56989f62016-10-24 14:40:05 +02002295struct genl_family dp_vport_genl_family __ro_after_init = {
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002296 .hdrsize = sizeof(struct ovs_header),
2297 .name = OVS_VPORT_FAMILY,
2298 .version = OVS_VPORT_VERSION,
2299 .maxattr = OVS_VPORT_ATTR_MAX,
Johannes Berg3b0f31f2019-03-21 22:51:02 +01002300 .policy = vport_policy,
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002301 .netnsok = true,
2302 .parallel_ops = true,
2303 .ops = dp_vport_genl_ops,
2304 .n_ops = ARRAY_SIZE(dp_vport_genl_ops),
2305 .mcgrps = &ovs_dp_vport_multicast_group,
2306 .n_mcgrps = 1,
Johannes Berg489111e2016-10-24 14:40:03 +02002307 .module = THIS_MODULE,
Jesse Grossccb13522011-10-25 19:26:31 -07002308};
2309
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002310static struct genl_family * const dp_genl_families[] = {
2311 &dp_datapath_genl_family,
2312 &dp_vport_genl_family,
2313 &dp_flow_genl_family,
2314 &dp_packet_genl_family,
Andy Zhou96fbc132017-11-10 12:09:42 -08002315 &dp_meter_genl_family,
Yi-Hung Wei11efd5c2018-05-24 17:56:43 -07002316#if IS_ENABLED(CONFIG_NETFILTER_CONNCOUNT)
2317 &dp_ct_limit_genl_family,
2318#endif
Jesse Grossccb13522011-10-25 19:26:31 -07002319};
2320
2321static void dp_unregister_genl(int n_families)
2322{
2323 int i;
2324
2325 for (i = 0; i < n_families; i++)
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002326 genl_unregister_family(dp_genl_families[i]);
Jesse Grossccb13522011-10-25 19:26:31 -07002327}
2328
Johannes Berg56989f62016-10-24 14:40:05 +02002329static int __init dp_register_genl(void)
Jesse Grossccb13522011-10-25 19:26:31 -07002330{
Jesse Grossccb13522011-10-25 19:26:31 -07002331 int err;
2332 int i;
2333
Jesse Grossccb13522011-10-25 19:26:31 -07002334 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
Jesse Grossccb13522011-10-25 19:26:31 -07002335
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002336 err = genl_register_family(dp_genl_families[i]);
Jesse Grossccb13522011-10-25 19:26:31 -07002337 if (err)
2338 goto error;
Jesse Grossccb13522011-10-25 19:26:31 -07002339 }
2340
2341 return 0;
2342
2343error:
Pravin B Shelar0c200ef2014-05-06 16:44:50 -07002344 dp_unregister_genl(i);
Jesse Grossccb13522011-10-25 19:26:31 -07002345 return err;
2346}
2347
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002348static int __net_init ovs_init_net(struct net *net)
2349{
2350 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2351
2352 INIT_LIST_HEAD(&ovs_net->dps);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002353 INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
Yi-Hung Wei11efd5c2018-05-24 17:56:43 -07002354 return ovs_ct_init(net);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002355}
2356
Pravin B Shelar7b4577a2015-02-17 11:23:10 -08002357static void __net_exit list_vports_from_net(struct net *net, struct net *dnet,
2358 struct list_head *head)
2359{
2360 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2361 struct datapath *dp;
2362
2363 list_for_each_entry(dp, &ovs_net->dps, list_node) {
2364 int i;
2365
2366 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
2367 struct vport *vport;
2368
2369 hlist_for_each_entry(vport, &dp->ports[i], dp_hash_node) {
Pravin B Shelar7b4577a2015-02-17 11:23:10 -08002370 if (vport->ops->type != OVS_VPORT_TYPE_INTERNAL)
2371 continue;
2372
Thomas Grafbe4ace62015-07-21 10:44:04 +02002373 if (dev_net(vport->dev) == dnet)
Pravin B Shelar7b4577a2015-02-17 11:23:10 -08002374 list_add(&vport->detach_list, head);
2375 }
2376 }
2377 }
2378}
2379
2380static void __net_exit ovs_exit_net(struct net *dnet)
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002381{
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002382 struct datapath *dp, *dp_next;
Pravin B Shelar7b4577a2015-02-17 11:23:10 -08002383 struct ovs_net *ovs_net = net_generic(dnet, ovs_net_id);
2384 struct vport *vport, *vport_next;
2385 struct net *net;
2386 LIST_HEAD(head);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002387
Joe Stringerc2ac6672015-08-26 11:31:52 -07002388 ovs_ct_exit(dnet);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002389 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002390 list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
2391 __dp_destroy(dp);
Pravin B Shelar7b4577a2015-02-17 11:23:10 -08002392
Kirill Tkhaif0b07bb12018-03-29 19:20:32 +03002393 down_read(&net_rwsem);
Pravin B Shelar7b4577a2015-02-17 11:23:10 -08002394 for_each_net(net)
2395 list_vports_from_net(net, dnet, &head);
Kirill Tkhaif0b07bb12018-03-29 19:20:32 +03002396 up_read(&net_rwsem);
Pravin B Shelar7b4577a2015-02-17 11:23:10 -08002397
2398 /* Detach all vports from given namespace. */
2399 list_for_each_entry_safe(vport, vport_next, &head, detach_list) {
2400 list_del(&vport->detach_list);
2401 ovs_dp_detach_port(vport);
2402 }
2403
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002404 ovs_unlock();
2405
2406 cancel_work_sync(&ovs_net->dp_notify_work);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002407}
2408
2409static struct pernet_operations ovs_net_ops = {
2410 .init = ovs_init_net,
2411 .exit = ovs_exit_net,
2412 .id = &ovs_net_id,
2413 .size = sizeof(struct ovs_net),
2414};
2415
Jesse Grossccb13522011-10-25 19:26:31 -07002416static int __init dp_init(void)
2417{
Jesse Grossccb13522011-10-25 19:26:31 -07002418 int err;
2419
YOSHIFUJI Hideaki / 吉藤英明3523b292013-01-09 07:19:55 +00002420 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
Jesse Grossccb13522011-10-25 19:26:31 -07002421
2422 pr_info("Open vSwitch switching datapath\n");
2423
Andy Zhou971427f32014-09-15 19:37:25 -07002424 err = action_fifos_init();
Jesse Grossccb13522011-10-25 19:26:31 -07002425 if (err)
2426 goto error;
2427
Andy Zhou971427f32014-09-15 19:37:25 -07002428 err = ovs_internal_dev_rtnl_link_register();
2429 if (err)
2430 goto error_action_fifos_exit;
2431
Jiri Pirko5b9e7e12014-06-26 09:58:26 +02002432 err = ovs_flow_init();
2433 if (err)
2434 goto error_unreg_rtnl_link;
2435
Jesse Grossccb13522011-10-25 19:26:31 -07002436 err = ovs_vport_init();
2437 if (err)
2438 goto error_flow_exit;
2439
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002440 err = register_pernet_device(&ovs_net_ops);
Jesse Grossccb13522011-10-25 19:26:31 -07002441 if (err)
2442 goto error_vport_exit;
2443
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002444 err = register_netdevice_notifier(&ovs_dp_device_notifier);
2445 if (err)
2446 goto error_netns_exit;
2447
Thomas Graf62b9c8d2014-10-22 17:29:06 +02002448 err = ovs_netdev_init();
2449 if (err)
2450 goto error_unreg_notifier;
2451
Jesse Grossccb13522011-10-25 19:26:31 -07002452 err = dp_register_genl();
2453 if (err < 0)
Thomas Graf62b9c8d2014-10-22 17:29:06 +02002454 goto error_unreg_netdev;
Jesse Grossccb13522011-10-25 19:26:31 -07002455
Jesse Grossccb13522011-10-25 19:26:31 -07002456 return 0;
2457
Thomas Graf62b9c8d2014-10-22 17:29:06 +02002458error_unreg_netdev:
2459 ovs_netdev_exit();
Jesse Grossccb13522011-10-25 19:26:31 -07002460error_unreg_notifier:
2461 unregister_netdevice_notifier(&ovs_dp_device_notifier);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002462error_netns_exit:
2463 unregister_pernet_device(&ovs_net_ops);
Jesse Grossccb13522011-10-25 19:26:31 -07002464error_vport_exit:
2465 ovs_vport_exit();
2466error_flow_exit:
2467 ovs_flow_exit();
Jiri Pirko5b9e7e12014-06-26 09:58:26 +02002468error_unreg_rtnl_link:
2469 ovs_internal_dev_rtnl_link_unregister();
Andy Zhou971427f32014-09-15 19:37:25 -07002470error_action_fifos_exit:
2471 action_fifos_exit();
Jesse Grossccb13522011-10-25 19:26:31 -07002472error:
2473 return err;
2474}
2475
2476static void dp_cleanup(void)
2477{
Jesse Grossccb13522011-10-25 19:26:31 -07002478 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
Thomas Graf62b9c8d2014-10-22 17:29:06 +02002479 ovs_netdev_exit();
Jesse Grossccb13522011-10-25 19:26:31 -07002480 unregister_netdevice_notifier(&ovs_dp_device_notifier);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002481 unregister_pernet_device(&ovs_net_ops);
2482 rcu_barrier();
Jesse Grossccb13522011-10-25 19:26:31 -07002483 ovs_vport_exit();
2484 ovs_flow_exit();
Jiri Pirko5b9e7e12014-06-26 09:58:26 +02002485 ovs_internal_dev_rtnl_link_unregister();
Andy Zhou971427f32014-09-15 19:37:25 -07002486 action_fifos_exit();
Jesse Grossccb13522011-10-25 19:26:31 -07002487}
2488
2489module_init(dp_init);
2490module_exit(dp_cleanup);
2491
2492MODULE_DESCRIPTION("Open vSwitch switching datapath");
2493MODULE_LICENSE("GPL");
Thadeu Lima de Souza Cascardoed227092016-09-09 17:42:30 -03002494MODULE_ALIAS_GENL_FAMILY(OVS_DATAPATH_FAMILY);
2495MODULE_ALIAS_GENL_FAMILY(OVS_VPORT_FAMILY);
2496MODULE_ALIAS_GENL_FAMILY(OVS_FLOW_FAMILY);
2497MODULE_ALIAS_GENL_FAMILY(OVS_PACKET_FAMILY);
Andy Zhou96fbc132017-11-10 12:09:42 -08002498MODULE_ALIAS_GENL_FAMILY(OVS_METER_FAMILY);
Yi-Hung Wei11efd5c2018-05-24 17:56:43 -07002499MODULE_ALIAS_GENL_FAMILY(OVS_CT_LIMIT_FAMILY);