blob: 235acaeaedc7979ad8371c74e2766b94e3e811dc [file] [log] [blame]
Jesse Grossccb13522011-10-25 19:26:31 -07001/*
Andy Zhou03f0d912013-08-07 20:01:00 -07002 * Copyright (c) 2007-2013 Nicira, Inc.
Jesse Grossccb13522011-10-25 19:26:31 -07003 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of version 2 of the GNU General Public
6 * License as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 * 02110-1301, USA
17 */
18
19#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20
21#include <linux/init.h>
22#include <linux/module.h>
23#include <linux/if_arp.h>
24#include <linux/if_vlan.h>
25#include <linux/in.h>
26#include <linux/ip.h>
27#include <linux/jhash.h>
28#include <linux/delay.h>
29#include <linux/time.h>
30#include <linux/etherdevice.h>
31#include <linux/genetlink.h>
32#include <linux/kernel.h>
33#include <linux/kthread.h>
34#include <linux/mutex.h>
35#include <linux/percpu.h>
36#include <linux/rcupdate.h>
37#include <linux/tcp.h>
38#include <linux/udp.h>
Jesse Grossccb13522011-10-25 19:26:31 -070039#include <linux/ethtool.h>
40#include <linux/wait.h>
Jesse Grossccb13522011-10-25 19:26:31 -070041#include <asm/div64.h>
42#include <linux/highmem.h>
43#include <linux/netfilter_bridge.h>
44#include <linux/netfilter_ipv4.h>
45#include <linux/inetdevice.h>
46#include <linux/list.h>
Pravin B Shelar8e4e1712013-04-15 13:23:03 -070047#include <linux/lockdep.h>
Jesse Grossccb13522011-10-25 19:26:31 -070048#include <linux/openvswitch.h>
49#include <linux/rculist.h>
50#include <linux/dmi.h>
51#include <linux/workqueue.h>
52#include <net/genetlink.h>
Pravin B Shelar46df7b82012-02-22 19:58:59 -080053#include <net/net_namespace.h>
54#include <net/netns/generic.h>
Jesse Grossccb13522011-10-25 19:26:31 -070055
56#include "datapath.h"
57#include "flow.h"
Pravin B Shelare6445712013-10-03 18:16:47 -070058#include "flow_netlink.h"
Jesse Grossccb13522011-10-25 19:26:31 -070059#include "vport-internal_dev.h"
Thomas Grafcff63a52013-04-29 13:06:41 +000060#include "vport-netdev.h"
Jesse Grossccb13522011-10-25 19:26:31 -070061
Pravin B Shelar8e4e1712013-04-15 13:23:03 -070062int ovs_net_id __read_mostly;
63
Johannes Berg68eb5502013-11-19 15:19:38 +010064static void ovs_notify(struct genl_family *family,
Johannes Berg2a94fe42013-11-19 15:19:39 +010065 struct sk_buff *skb, struct genl_info *info)
Thomas Grafed6611852013-03-29 14:46:50 +010066{
Johannes Berg68eb5502013-11-19 15:19:38 +010067 genl_notify(family, skb, genl_info_net(info), info->snd_portid,
Johannes Berg2a94fe42013-11-19 15:19:39 +010068 0, info->nlhdr, GFP_KERNEL);
Thomas Grafed6611852013-03-29 14:46:50 +010069}
70
Pravin B Shelar46df7b82012-02-22 19:58:59 -080071/**
Jesse Grossccb13522011-10-25 19:26:31 -070072 * DOC: Locking:
73 *
Pravin B Shelar8e4e1712013-04-15 13:23:03 -070074 * All writes e.g. Writes to device state (add/remove datapath, port, set
75 * operations on vports, etc.), Writes to other state (flow table
76 * modifications, set miscellaneous datapath parameters, etc.) are protected
77 * by ovs_lock.
Jesse Grossccb13522011-10-25 19:26:31 -070078 *
79 * Reads are protected by RCU.
80 *
81 * There are a few special cases (mostly stats) that have their own
82 * synchronization but they nest under all of above and don't interact with
83 * each other.
Pravin B Shelar8e4e1712013-04-15 13:23:03 -070084 *
85 * The RTNL lock nests inside ovs_mutex.
Jesse Grossccb13522011-10-25 19:26:31 -070086 */
87
Pravin B Shelar8e4e1712013-04-15 13:23:03 -070088static DEFINE_MUTEX(ovs_mutex);
89
90void ovs_lock(void)
91{
92 mutex_lock(&ovs_mutex);
93}
94
95void ovs_unlock(void)
96{
97 mutex_unlock(&ovs_mutex);
98}
99
100#ifdef CONFIG_LOCKDEP
101int lockdep_ovsl_is_held(void)
102{
103 if (debug_locks)
104 return lockdep_is_held(&ovs_mutex);
105 else
106 return 1;
107}
108#endif
109
Jesse Grossccb13522011-10-25 19:26:31 -0700110static struct vport *new_vport(const struct vport_parms *);
Thomas Graf8055a892013-12-13 15:22:20 +0100111static int queue_gso_packets(struct datapath *dp, struct sk_buff *,
Jesse Grossccb13522011-10-25 19:26:31 -0700112 const struct dp_upcall_info *);
Thomas Graf8055a892013-12-13 15:22:20 +0100113static int queue_userspace_packet(struct datapath *dp, struct sk_buff *,
Jesse Grossccb13522011-10-25 19:26:31 -0700114 const struct dp_upcall_info *);
115
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700116/* Must be called with rcu_read_lock or ovs_mutex. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800117static struct datapath *get_dp(struct net *net, int dp_ifindex)
Jesse Grossccb13522011-10-25 19:26:31 -0700118{
119 struct datapath *dp = NULL;
120 struct net_device *dev;
121
122 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800123 dev = dev_get_by_index_rcu(net, dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -0700124 if (dev) {
125 struct vport *vport = ovs_internal_dev_get_vport(dev);
126 if (vport)
127 dp = vport->dp;
128 }
129 rcu_read_unlock();
130
131 return dp;
132}
133
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700134/* Must be called with rcu_read_lock or ovs_mutex. */
Jesse Grossccb13522011-10-25 19:26:31 -0700135const char *ovs_dp_name(const struct datapath *dp)
136{
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700137 struct vport *vport = ovs_vport_ovsl_rcu(dp, OVSP_LOCAL);
Jesse Grossccb13522011-10-25 19:26:31 -0700138 return vport->ops->get_name(vport);
139}
140
141static int get_dpifindex(struct datapath *dp)
142{
143 struct vport *local;
144 int ifindex;
145
146 rcu_read_lock();
147
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700148 local = ovs_vport_rcu(dp, OVSP_LOCAL);
Jesse Grossccb13522011-10-25 19:26:31 -0700149 if (local)
Thomas Grafcff63a52013-04-29 13:06:41 +0000150 ifindex = netdev_vport_priv(local)->dev->ifindex;
Jesse Grossccb13522011-10-25 19:26:31 -0700151 else
152 ifindex = 0;
153
154 rcu_read_unlock();
155
156 return ifindex;
157}
158
159static void destroy_dp_rcu(struct rcu_head *rcu)
160{
161 struct datapath *dp = container_of(rcu, struct datapath, rcu);
162
Pravin B Shelar618ed0c2013-10-04 00:17:42 -0700163 ovs_flow_tbl_destroy(&dp->table);
Jesse Grossccb13522011-10-25 19:26:31 -0700164 free_percpu(dp->stats_percpu);
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800165 release_net(ovs_dp_get_net(dp));
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700166 kfree(dp->ports);
Jesse Grossccb13522011-10-25 19:26:31 -0700167 kfree(dp);
168}
169
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700170static struct hlist_head *vport_hash_bucket(const struct datapath *dp,
171 u16 port_no)
172{
173 return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)];
174}
175
176struct 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. */
216void ovs_dp_process_received_packet(struct vport *p, struct sk_buff *skb)
217{
218 struct datapath *dp = p->dp;
219 struct sw_flow *flow;
220 struct dp_stats_percpu *stats;
221 struct sw_flow_key key;
222 u64 *stats_counter;
Andy Zhou1bd71162013-10-22 10:42:46 -0700223 u32 n_mask_hit;
Jesse Grossccb13522011-10-25 19:26:31 -0700224 int error;
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
228 /* Extract flow from 'skb' into 'key'. */
Andy Zhou03f0d912013-08-07 20:01:00 -0700229 error = ovs_flow_extract(skb, p->port_no, &key);
Jesse Grossccb13522011-10-25 19:26:31 -0700230 if (unlikely(error)) {
231 kfree_skb(skb);
232 return;
233 }
234
235 /* Look up flow. */
Andy Zhou5bb50632013-11-25 10:42:46 -0800236 flow = ovs_flow_tbl_lookup_stats(&dp->table, &key, &n_mask_hit);
Jesse Grossccb13522011-10-25 19:26:31 -0700237 if (unlikely(!flow)) {
238 struct dp_upcall_info upcall;
239
240 upcall.cmd = OVS_PACKET_CMD_MISS;
241 upcall.key = &key;
242 upcall.userdata = NULL;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000243 upcall.portid = p->upcall_portid;
Jesse Grossccb13522011-10-25 19:26:31 -0700244 ovs_dp_upcall(dp, skb, &upcall);
245 consume_skb(skb);
246 stats_counter = &stats->n_missed;
247 goto out;
248 }
249
250 OVS_CB(skb)->flow = flow;
Andy Zhou03f0d912013-08-07 20:01:00 -0700251 OVS_CB(skb)->pkt_key = &key;
Jesse Grossccb13522011-10-25 19:26:31 -0700252
Pravin B Shelare298e502013-10-29 17:22:21 -0700253 ovs_flow_stats_update(OVS_CB(skb)->flow, skb);
Jesse Grossccb13522011-10-25 19:26:31 -0700254 ovs_execute_actions(dp, skb);
Pravin B Shelare298e502013-10-29 17:22:21 -0700255 stats_counter = &stats->n_hit;
Jesse Grossccb13522011-10-25 19:26:31 -0700256
257out:
258 /* Update datapath statistics. */
259 u64_stats_update_begin(&stats->sync);
260 (*stats_counter)++;
Andy Zhou1bd71162013-10-22 10:42:46 -0700261 stats->n_mask_hit += n_mask_hit;
Jesse Grossccb13522011-10-25 19:26:31 -0700262 u64_stats_update_end(&stats->sync);
263}
264
265static struct genl_family dp_packet_genl_family = {
266 .id = GENL_ID_GENERATE,
267 .hdrsize = sizeof(struct ovs_header),
268 .name = OVS_PACKET_FAMILY,
269 .version = OVS_PACKET_VERSION,
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800270 .maxattr = OVS_PACKET_ATTR_MAX,
Pravin B Shelar3a4e0d62013-04-23 07:48:48 +0000271 .netnsok = true,
272 .parallel_ops = true,
Jesse Grossccb13522011-10-25 19:26:31 -0700273};
274
275int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800276 const struct dp_upcall_info *upcall_info)
Jesse Grossccb13522011-10-25 19:26:31 -0700277{
278 struct dp_stats_percpu *stats;
Jesse Grossccb13522011-10-25 19:26:31 -0700279 int err;
280
Eric W. Biederman15e47302012-09-07 20:12:54 +0000281 if (upcall_info->portid == 0) {
Jesse Grossccb13522011-10-25 19:26:31 -0700282 err = -ENOTCONN;
283 goto err;
284 }
285
Jesse Grossccb13522011-10-25 19:26:31 -0700286 if (!skb_is_gso(skb))
Thomas Graf8055a892013-12-13 15:22:20 +0100287 err = queue_userspace_packet(dp, skb, upcall_info);
Jesse Grossccb13522011-10-25 19:26:31 -0700288 else
Thomas Graf8055a892013-12-13 15:22:20 +0100289 err = queue_gso_packets(dp, skb, upcall_info);
Jesse Grossccb13522011-10-25 19:26:31 -0700290 if (err)
291 goto err;
292
293 return 0;
294
295err:
Shan Wei404f2f12012-11-13 09:52:25 +0800296 stats = this_cpu_ptr(dp->stats_percpu);
Jesse Grossccb13522011-10-25 19:26:31 -0700297
298 u64_stats_update_begin(&stats->sync);
299 stats->n_lost++;
300 u64_stats_update_end(&stats->sync);
301
302 return err;
303}
304
Thomas Graf8055a892013-12-13 15:22:20 +0100305static int queue_gso_packets(struct datapath *dp, struct sk_buff *skb,
Jesse Grossccb13522011-10-25 19:26:31 -0700306 const struct dp_upcall_info *upcall_info)
307{
Ben Pfaffa1b5d0d2012-07-20 14:47:54 -0700308 unsigned short gso_type = skb_shinfo(skb)->gso_type;
Jesse Grossccb13522011-10-25 19:26:31 -0700309 struct dp_upcall_info later_info;
310 struct sw_flow_key later_key;
311 struct sk_buff *segs, *nskb;
312 int err;
313
Cong Wang12b00042013-02-05 16:36:38 +0000314 segs = __skb_gso_segment(skb, NETIF_F_SG | NETIF_F_HW_CSUM, false);
Pravin B Shelar92e5dfc2012-07-20 14:46:29 -0700315 if (IS_ERR(segs))
316 return PTR_ERR(segs);
Jesse Grossccb13522011-10-25 19:26:31 -0700317
318 /* Queue all of the segments. */
319 skb = segs;
320 do {
Thomas Graf8055a892013-12-13 15:22:20 +0100321 err = queue_userspace_packet(dp, skb, upcall_info);
Jesse Grossccb13522011-10-25 19:26:31 -0700322 if (err)
323 break;
324
Ben Pfaffa1b5d0d2012-07-20 14:47:54 -0700325 if (skb == segs && gso_type & SKB_GSO_UDP) {
Jesse Grossccb13522011-10-25 19:26:31 -0700326 /* The initial flow key extracted by ovs_flow_extract()
327 * in this case is for a first fragment, so we need to
328 * properly mark later fragments.
329 */
330 later_key = *upcall_info->key;
331 later_key.ip.frag = OVS_FRAG_TYPE_LATER;
332
333 later_info = *upcall_info;
334 later_info.key = &later_key;
335 upcall_info = &later_info;
336 }
337 } while ((skb = skb->next));
338
339 /* Free all of the segments. */
340 skb = segs;
341 do {
342 nskb = skb->next;
343 if (err)
344 kfree_skb(skb);
345 else
346 consume_skb(skb);
347 } while ((skb = nskb));
348 return err;
349}
350
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100351static size_t key_attr_size(void)
352{
353 return nla_total_size(4) /* OVS_KEY_ATTR_PRIORITY */
Pravin B Shelar7d5437c2013-06-17 17:50:18 -0700354 + nla_total_size(0) /* OVS_KEY_ATTR_TUNNEL */
355 + nla_total_size(8) /* OVS_TUNNEL_KEY_ATTR_ID */
356 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_SRC */
357 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_DST */
358 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TOS */
359 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TTL */
360 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT */
361 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_CSUM */
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100362 + nla_total_size(4) /* OVS_KEY_ATTR_IN_PORT */
363 + nla_total_size(4) /* OVS_KEY_ATTR_SKB_MARK */
364 + nla_total_size(12) /* OVS_KEY_ATTR_ETHERNET */
365 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
366 + nla_total_size(4) /* OVS_KEY_ATTR_8021Q */
367 + nla_total_size(0) /* OVS_KEY_ATTR_ENCAP */
368 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
369 + nla_total_size(40) /* OVS_KEY_ATTR_IPV6 */
370 + nla_total_size(2) /* OVS_KEY_ATTR_ICMPV6 */
371 + nla_total_size(28); /* OVS_KEY_ATTR_ND */
372}
373
374static size_t upcall_msg_size(const struct sk_buff *skb,
375 const struct nlattr *userdata)
376{
377 size_t size = NLMSG_ALIGN(sizeof(struct ovs_header))
378 + nla_total_size(skb->len) /* OVS_PACKET_ATTR_PACKET */
379 + nla_total_size(key_attr_size()); /* OVS_PACKET_ATTR_KEY */
380
381 /* OVS_PACKET_ATTR_USERDATA */
382 if (userdata)
383 size += NLA_ALIGN(userdata->nla_len);
384
385 return size;
386}
387
Thomas Graf8055a892013-12-13 15:22:20 +0100388static int queue_userspace_packet(struct datapath *dp, struct sk_buff *skb,
Jesse Grossccb13522011-10-25 19:26:31 -0700389 const struct dp_upcall_info *upcall_info)
390{
391 struct ovs_header *upcall;
392 struct sk_buff *nskb = NULL;
393 struct sk_buff *user_skb; /* to be queued to userspace */
394 struct nlattr *nla;
Thomas Graf795449d2013-11-30 13:21:32 +0100395 struct genl_info info = {
Thomas Graf8055a892013-12-13 15:22:20 +0100396 .dst_sk = ovs_dp_get_net(dp)->genl_sock,
Thomas Graf795449d2013-11-30 13:21:32 +0100397 .snd_portid = upcall_info->portid,
398 };
399 size_t len;
Thomas Graf8055a892013-12-13 15:22:20 +0100400 int err, dp_ifindex;
401
402 dp_ifindex = get_dpifindex(dp);
403 if (!dp_ifindex)
404 return -ENODEV;
Jesse Grossccb13522011-10-25 19:26:31 -0700405
406 if (vlan_tx_tag_present(skb)) {
407 nskb = skb_clone(skb, GFP_ATOMIC);
408 if (!nskb)
409 return -ENOMEM;
410
Patrick McHardy86a9bad2013-04-19 02:04:30 +0000411 nskb = __vlan_put_tag(nskb, nskb->vlan_proto, vlan_tx_tag_get(nskb));
Dan Carpenter8aa51d62012-05-13 08:44:18 +0000412 if (!nskb)
Jesse Grossccb13522011-10-25 19:26:31 -0700413 return -ENOMEM;
414
415 nskb->vlan_tci = 0;
416 skb = nskb;
417 }
418
419 if (nla_attr_size(skb->len) > USHRT_MAX) {
420 err = -EFBIG;
421 goto out;
422 }
423
Thomas Graf795449d2013-11-30 13:21:32 +0100424 len = upcall_msg_size(skb, upcall_info->userdata);
425 user_skb = genlmsg_new_unicast(len, &info, GFP_ATOMIC);
Jesse Grossccb13522011-10-25 19:26:31 -0700426 if (!user_skb) {
427 err = -ENOMEM;
428 goto out;
429 }
430
431 upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
432 0, upcall_info->cmd);
433 upcall->dp_ifindex = dp_ifindex;
434
435 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
Pravin B Shelare6445712013-10-03 18:16:47 -0700436 ovs_nla_put_flow(upcall_info->key, upcall_info->key, user_skb);
Jesse Grossccb13522011-10-25 19:26:31 -0700437 nla_nest_end(user_skb, nla);
438
439 if (upcall_info->userdata)
Ben Pfaff44901082013-02-15 17:29:22 -0800440 __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA,
441 nla_len(upcall_info->userdata),
442 nla_data(upcall_info->userdata));
Jesse Grossccb13522011-10-25 19:26:31 -0700443
444 nla = __nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, skb->len);
445
446 skb_copy_and_csum_dev(skb, nla_data(nla));
447
Rich Lanea15ff762013-02-15 11:07:43 -0800448 genlmsg_end(user_skb, upcall);
Thomas Graf8055a892013-12-13 15:22:20 +0100449 err = genlmsg_unicast(ovs_dp_get_net(dp), user_skb, upcall_info->portid);
Jesse Grossccb13522011-10-25 19:26:31 -0700450
451out:
452 kfree_skb(nskb);
453 return err;
454}
455
Jesse Grossccb13522011-10-25 19:26:31 -0700456static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
457{
458 struct ovs_header *ovs_header = info->userhdr;
459 struct nlattr **a = info->attrs;
460 struct sw_flow_actions *acts;
461 struct sk_buff *packet;
462 struct sw_flow *flow;
463 struct datapath *dp;
464 struct ethhdr *eth;
465 int len;
466 int err;
Jesse Grossccb13522011-10-25 19:26:31 -0700467
468 err = -EINVAL;
469 if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
Thomas Grafdded45fc2013-03-29 14:46:47 +0100470 !a[OVS_PACKET_ATTR_ACTIONS])
Jesse Grossccb13522011-10-25 19:26:31 -0700471 goto err;
472
473 len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
474 packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
475 err = -ENOMEM;
476 if (!packet)
477 goto err;
478 skb_reserve(packet, NET_IP_ALIGN);
479
Thomas Graf32686a92013-03-29 14:46:48 +0100480 nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
Jesse Grossccb13522011-10-25 19:26:31 -0700481
482 skb_reset_mac_header(packet);
483 eth = eth_hdr(packet);
484
485 /* Normally, setting the skb 'protocol' field would be handled by a
486 * call to eth_type_trans(), but it assumes there's a sending
487 * device, which we may not have. */
Simon Hormane5c5d222013-03-28 13:38:25 +0900488 if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN)
Jesse Grossccb13522011-10-25 19:26:31 -0700489 packet->protocol = eth->h_proto;
490 else
491 packet->protocol = htons(ETH_P_802_2);
492
493 /* Build an sw_flow for sending this packet. */
Pravin B Shelare298e502013-10-29 17:22:21 -0700494 flow = ovs_flow_alloc(false);
Jesse Grossccb13522011-10-25 19:26:31 -0700495 err = PTR_ERR(flow);
496 if (IS_ERR(flow))
497 goto err_kfree_skb;
498
Andy Zhou03f0d912013-08-07 20:01:00 -0700499 err = ovs_flow_extract(packet, -1, &flow->key);
Jesse Grossccb13522011-10-25 19:26:31 -0700500 if (err)
501 goto err_flow_free;
502
Pravin B Shelare6445712013-10-03 18:16:47 -0700503 err = ovs_nla_get_flow_metadata(flow, a[OVS_PACKET_ATTR_KEY]);
Jesse Grossccb13522011-10-25 19:26:31 -0700504 if (err)
505 goto err_flow_free;
Pravin B Shelare6445712013-10-03 18:16:47 -0700506 acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_PACKET_ATTR_ACTIONS]));
Jesse Grossccb13522011-10-25 19:26:31 -0700507 err = PTR_ERR(acts);
508 if (IS_ERR(acts))
509 goto err_flow_free;
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700510
Pravin B Shelare6445712013-10-03 18:16:47 -0700511 err = ovs_nla_copy_actions(a[OVS_PACKET_ATTR_ACTIONS],
512 &flow->key, 0, &acts);
Jesse Grossccb13522011-10-25 19:26:31 -0700513 rcu_assign_pointer(flow->sf_acts, acts);
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700514 if (err)
515 goto err_flow_free;
Jesse Grossccb13522011-10-25 19:26:31 -0700516
517 OVS_CB(packet)->flow = flow;
Andy Zhou03f0d912013-08-07 20:01:00 -0700518 OVS_CB(packet)->pkt_key = &flow->key;
Jesse Grossccb13522011-10-25 19:26:31 -0700519 packet->priority = flow->key.phy.priority;
Ansis Atteka39c7caeb2012-11-26 11:24:11 -0800520 packet->mark = flow->key.phy.skb_mark;
Jesse Grossccb13522011-10-25 19:26:31 -0700521
522 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800523 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -0700524 err = -ENODEV;
525 if (!dp)
526 goto err_unlock;
527
528 local_bh_disable();
529 err = ovs_execute_actions(dp, packet);
530 local_bh_enable();
531 rcu_read_unlock();
532
Andy Zhou03f0d912013-08-07 20:01:00 -0700533 ovs_flow_free(flow, false);
Jesse Grossccb13522011-10-25 19:26:31 -0700534 return err;
535
536err_unlock:
537 rcu_read_unlock();
538err_flow_free:
Andy Zhou03f0d912013-08-07 20:01:00 -0700539 ovs_flow_free(flow, false);
Jesse Grossccb13522011-10-25 19:26:31 -0700540err_kfree_skb:
541 kfree_skb(packet);
542err:
543 return err;
544}
545
546static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
Thomas Grafdded45fc2013-03-29 14:46:47 +0100547 [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
Jesse Grossccb13522011-10-25 19:26:31 -0700548 [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
549 [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
550};
551
Johannes Berg4534de82013-11-14 17:14:46 +0100552static const struct genl_ops dp_packet_genl_ops[] = {
Jesse Grossccb13522011-10-25 19:26:31 -0700553 { .cmd = OVS_PACKET_CMD_EXECUTE,
554 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
555 .policy = packet_policy,
556 .doit = ovs_packet_cmd_execute
557 }
558};
559
Andy Zhou1bd71162013-10-22 10:42:46 -0700560static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats,
561 struct ovs_dp_megaflow_stats *mega_stats)
Jesse Grossccb13522011-10-25 19:26:31 -0700562{
563 int i;
Jesse Grossccb13522011-10-25 19:26:31 -0700564
Andy Zhou1bd71162013-10-22 10:42:46 -0700565 memset(mega_stats, 0, sizeof(*mega_stats));
566
Pravin B Shelarb637e492013-10-04 00:14:23 -0700567 stats->n_flows = ovs_flow_tbl_count(&dp->table);
Andy Zhou1bd71162013-10-22 10:42:46 -0700568 mega_stats->n_masks = ovs_flow_tbl_num_masks(&dp->table);
Jesse Grossccb13522011-10-25 19:26:31 -0700569
570 stats->n_hit = stats->n_missed = stats->n_lost = 0;
Andy Zhou1bd71162013-10-22 10:42:46 -0700571
Jesse Grossccb13522011-10-25 19:26:31 -0700572 for_each_possible_cpu(i) {
573 const struct dp_stats_percpu *percpu_stats;
574 struct dp_stats_percpu local_stats;
575 unsigned int start;
576
577 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
578
579 do {
580 start = u64_stats_fetch_begin_bh(&percpu_stats->sync);
581 local_stats = *percpu_stats;
582 } while (u64_stats_fetch_retry_bh(&percpu_stats->sync, start));
583
584 stats->n_hit += local_stats.n_hit;
585 stats->n_missed += local_stats.n_missed;
586 stats->n_lost += local_stats.n_lost;
Andy Zhou1bd71162013-10-22 10:42:46 -0700587 mega_stats->n_mask_hit += local_stats.n_mask_hit;
Jesse Grossccb13522011-10-25 19:26:31 -0700588 }
589}
590
591static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
592 [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
593 [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
594 [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
595};
596
597static struct genl_family dp_flow_genl_family = {
598 .id = GENL_ID_GENERATE,
599 .hdrsize = sizeof(struct ovs_header),
600 .name = OVS_FLOW_FAMILY,
601 .version = OVS_FLOW_VERSION,
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800602 .maxattr = OVS_FLOW_ATTR_MAX,
Pravin B Shelar3a4e0d62013-04-23 07:48:48 +0000603 .netnsok = true,
604 .parallel_ops = true,
Jesse Grossccb13522011-10-25 19:26:31 -0700605};
606
607static struct genl_multicast_group ovs_dp_flow_multicast_group = {
608 .name = OVS_FLOW_MCGROUP
609};
610
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100611static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts)
612{
613 return NLMSG_ALIGN(sizeof(struct ovs_header))
614 + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_KEY */
Andy Zhou03f0d912013-08-07 20:01:00 -0700615 + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_MASK */
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100616 + nla_total_size(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
617 + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
618 + nla_total_size(8) /* OVS_FLOW_ATTR_USED */
619 + nla_total_size(acts->actions_len); /* OVS_FLOW_ATTR_ACTIONS */
620}
621
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700622/* Called with ovs_mutex. */
Jesse Grossccb13522011-10-25 19:26:31 -0700623static int ovs_flow_cmd_fill_info(struct sw_flow *flow, struct datapath *dp,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000624 struct sk_buff *skb, u32 portid,
Jesse Grossccb13522011-10-25 19:26:31 -0700625 u32 seq, u32 flags, u8 cmd)
626{
627 const int skb_orig_len = skb->len;
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700628 struct nlattr *start;
Jesse Grossccb13522011-10-25 19:26:31 -0700629 struct ovs_flow_stats stats;
Pravin B Shelare298e502013-10-29 17:22:21 -0700630 __be16 tcp_flags;
631 unsigned long used;
Jesse Grossccb13522011-10-25 19:26:31 -0700632 struct ovs_header *ovs_header;
633 struct nlattr *nla;
Jesse Grossccb13522011-10-25 19:26:31 -0700634 int err;
635
Eric W. Biederman15e47302012-09-07 20:12:54 +0000636 ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family, flags, cmd);
Jesse Grossccb13522011-10-25 19:26:31 -0700637 if (!ovs_header)
638 return -EMSGSIZE;
639
640 ovs_header->dp_ifindex = get_dpifindex(dp);
641
Andy Zhou03f0d912013-08-07 20:01:00 -0700642 /* Fill flow key. */
Jesse Grossccb13522011-10-25 19:26:31 -0700643 nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
644 if (!nla)
645 goto nla_put_failure;
Andy Zhou03f0d912013-08-07 20:01:00 -0700646
Pravin B Shelare6445712013-10-03 18:16:47 -0700647 err = ovs_nla_put_flow(&flow->unmasked_key, &flow->unmasked_key, skb);
Jesse Grossccb13522011-10-25 19:26:31 -0700648 if (err)
649 goto error;
650 nla_nest_end(skb, nla);
651
Andy Zhou03f0d912013-08-07 20:01:00 -0700652 nla = nla_nest_start(skb, OVS_FLOW_ATTR_MASK);
653 if (!nla)
654 goto nla_put_failure;
655
Pravin B Shelare6445712013-10-03 18:16:47 -0700656 err = ovs_nla_put_flow(&flow->key, &flow->mask->key, skb);
Andy Zhou03f0d912013-08-07 20:01:00 -0700657 if (err)
658 goto error;
659
660 nla_nest_end(skb, nla);
661
Pravin B Shelare298e502013-10-29 17:22:21 -0700662 ovs_flow_stats_get(flow, &stats, &used, &tcp_flags);
David S. Miller028d6a62012-03-29 23:20:48 -0400663 if (used &&
664 nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
665 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -0700666
David S. Miller028d6a62012-03-29 23:20:48 -0400667 if (stats.n_packets &&
Pravin B Shelare298e502013-10-29 17:22:21 -0700668 nla_put(skb, OVS_FLOW_ATTR_STATS, sizeof(struct ovs_flow_stats), &stats))
David S. Miller028d6a62012-03-29 23:20:48 -0400669 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -0700670
Pravin B Shelare298e502013-10-29 17:22:21 -0700671 if ((u8)ntohs(tcp_flags) &&
672 nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, (u8)ntohs(tcp_flags)))
David S. Miller028d6a62012-03-29 23:20:48 -0400673 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -0700674
675 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
676 * this is the first flow to be dumped into 'skb'. This is unusual for
677 * Netlink but individual action lists can be longer than
678 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
679 * The userspace caller can always fetch the actions separately if it
680 * really wants them. (Most userspace callers in fact don't care.)
681 *
682 * This can only fail for dump operations because the skb is always
683 * properly sized for single flows.
684 */
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700685 start = nla_nest_start(skb, OVS_FLOW_ATTR_ACTIONS);
686 if (start) {
Pravin B Shelard57170b2013-07-30 15:39:39 -0700687 const struct sw_flow_actions *sf_acts;
688
Jesse Gross663efa32013-12-03 10:58:53 -0800689 sf_acts = rcu_dereference_ovsl(flow->sf_acts);
Pravin B Shelard57170b2013-07-30 15:39:39 -0700690
Pravin B Shelare6445712013-10-03 18:16:47 -0700691 err = ovs_nla_put_actions(sf_acts->actions,
692 sf_acts->actions_len, skb);
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700693 if (!err)
694 nla_nest_end(skb, start);
695 else {
696 if (skb_orig_len)
697 goto error;
698
699 nla_nest_cancel(skb, start);
700 }
701 } else if (skb_orig_len)
702 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -0700703
704 return genlmsg_end(skb, ovs_header);
705
706nla_put_failure:
707 err = -EMSGSIZE;
708error:
709 genlmsg_cancel(skb, ovs_header);
710 return err;
711}
712
Thomas Graf795449d2013-11-30 13:21:32 +0100713static struct sk_buff *ovs_flow_cmd_alloc_info(struct sw_flow *flow,
714 struct genl_info *info)
Jesse Grossccb13522011-10-25 19:26:31 -0700715{
Thomas Graf795449d2013-11-30 13:21:32 +0100716 size_t len;
Jesse Grossccb13522011-10-25 19:26:31 -0700717
Thomas Graf795449d2013-11-30 13:21:32 +0100718 len = ovs_flow_cmd_msg_size(ovsl_dereference(flow->sf_acts));
Jesse Grossccb13522011-10-25 19:26:31 -0700719
Thomas Graf795449d2013-11-30 13:21:32 +0100720 return genlmsg_new_unicast(len, info, GFP_KERNEL);
Jesse Grossccb13522011-10-25 19:26:31 -0700721}
722
723static struct sk_buff *ovs_flow_cmd_build_info(struct sw_flow *flow,
724 struct datapath *dp,
Thomas Graf795449d2013-11-30 13:21:32 +0100725 struct genl_info *info,
726 u8 cmd)
Jesse Grossccb13522011-10-25 19:26:31 -0700727{
728 struct sk_buff *skb;
729 int retval;
730
Thomas Graf795449d2013-11-30 13:21:32 +0100731 skb = ovs_flow_cmd_alloc_info(flow, info);
Jesse Grossccb13522011-10-25 19:26:31 -0700732 if (!skb)
733 return ERR_PTR(-ENOMEM);
734
Thomas Graf795449d2013-11-30 13:21:32 +0100735 retval = ovs_flow_cmd_fill_info(flow, dp, skb, info->snd_portid,
736 info->snd_seq, 0, cmd);
Jesse Grossccb13522011-10-25 19:26:31 -0700737 BUG_ON(retval < 0);
738 return skb;
739}
740
741static int ovs_flow_cmd_new_or_set(struct sk_buff *skb, struct genl_info *info)
742{
743 struct nlattr **a = info->attrs;
744 struct ovs_header *ovs_header = info->userhdr;
Andy Zhou03f0d912013-08-07 20:01:00 -0700745 struct sw_flow_key key, masked_key;
746 struct sw_flow *flow = NULL;
747 struct sw_flow_mask mask;
Jesse Grossccb13522011-10-25 19:26:31 -0700748 struct sk_buff *reply;
749 struct datapath *dp;
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700750 struct sw_flow_actions *acts = NULL;
Andy Zhou03f0d912013-08-07 20:01:00 -0700751 struct sw_flow_match match;
Pravin B Shelare298e502013-10-29 17:22:21 -0700752 bool exact_5tuple;
Jesse Grossccb13522011-10-25 19:26:31 -0700753 int error;
Jesse Grossccb13522011-10-25 19:26:31 -0700754
755 /* Extract key. */
756 error = -EINVAL;
757 if (!a[OVS_FLOW_ATTR_KEY])
758 goto error;
Andy Zhou03f0d912013-08-07 20:01:00 -0700759
760 ovs_match_init(&match, &key, &mask);
Pravin B Shelare298e502013-10-29 17:22:21 -0700761 error = ovs_nla_get_match(&match, &exact_5tuple,
Pravin B Shelare6445712013-10-03 18:16:47 -0700762 a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
Jesse Grossccb13522011-10-25 19:26:31 -0700763 if (error)
764 goto error;
765
766 /* Validate actions. */
767 if (a[OVS_FLOW_ATTR_ACTIONS]) {
Pravin B Shelare6445712013-10-03 18:16:47 -0700768 acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_FLOW_ATTR_ACTIONS]));
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700769 error = PTR_ERR(acts);
770 if (IS_ERR(acts))
Jesse Grossccb13522011-10-25 19:26:31 -0700771 goto error;
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700772
Pravin B Shelare6445712013-10-03 18:16:47 -0700773 ovs_flow_mask_key(&masked_key, &key, &mask);
774 error = ovs_nla_copy_actions(a[OVS_FLOW_ATTR_ACTIONS],
775 &masked_key, 0, &acts);
Andy Zhou03f0d912013-08-07 20:01:00 -0700776 if (error) {
777 OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700778 goto err_kfree;
Andy Zhou03f0d912013-08-07 20:01:00 -0700779 }
Jesse Grossccb13522011-10-25 19:26:31 -0700780 } else if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW) {
781 error = -EINVAL;
782 goto error;
783 }
784
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700785 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800786 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -0700787 error = -ENODEV;
788 if (!dp)
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700789 goto err_unlock_ovs;
Jesse Grossccb13522011-10-25 19:26:31 -0700790
Andy Zhou03f0d912013-08-07 20:01:00 -0700791 /* Check if this is a duplicate flow */
Andy Zhou5bb50632013-11-25 10:42:46 -0800792 flow = ovs_flow_tbl_lookup(&dp->table, &key);
Jesse Grossccb13522011-10-25 19:26:31 -0700793 if (!flow) {
Jesse Grossccb13522011-10-25 19:26:31 -0700794 /* Bail out if we're not allowed to create a new flow. */
795 error = -ENOENT;
796 if (info->genlhdr->cmd == OVS_FLOW_CMD_SET)
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700797 goto err_unlock_ovs;
Jesse Grossccb13522011-10-25 19:26:31 -0700798
Jesse Grossccb13522011-10-25 19:26:31 -0700799 /* Allocate flow. */
Pravin B Shelare298e502013-10-29 17:22:21 -0700800 flow = ovs_flow_alloc(!exact_5tuple);
Jesse Grossccb13522011-10-25 19:26:31 -0700801 if (IS_ERR(flow)) {
802 error = PTR_ERR(flow);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700803 goto err_unlock_ovs;
Jesse Grossccb13522011-10-25 19:26:31 -0700804 }
Jesse Grossccb13522011-10-25 19:26:31 -0700805
Andy Zhou03f0d912013-08-07 20:01:00 -0700806 flow->key = masked_key;
807 flow->unmasked_key = key;
Jesse Grossccb13522011-10-25 19:26:31 -0700808 rcu_assign_pointer(flow->sf_acts, acts);
809
810 /* Put flow in bucket. */
Pravin B Shelar618ed0c2013-10-04 00:17:42 -0700811 error = ovs_flow_tbl_insert(&dp->table, flow, &mask);
812 if (error) {
813 acts = NULL;
814 goto err_flow_free;
815 }
Jesse Grossccb13522011-10-25 19:26:31 -0700816
Thomas Graf795449d2013-11-30 13:21:32 +0100817 reply = ovs_flow_cmd_build_info(flow, dp, info, OVS_FLOW_CMD_NEW);
Jesse Grossccb13522011-10-25 19:26:31 -0700818 } else {
819 /* We found a matching flow. */
820 struct sw_flow_actions *old_acts;
Jesse Grossccb13522011-10-25 19:26:31 -0700821
822 /* Bail out if we're not allowed to modify an existing flow.
823 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
824 * because Generic Netlink treats the latter as a dump
825 * request. We also accept NLM_F_EXCL in case that bug ever
826 * gets fixed.
827 */
828 error = -EEXIST;
829 if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW &&
830 info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL))
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700831 goto err_unlock_ovs;
Jesse Grossccb13522011-10-25 19:26:31 -0700832
Andy Zhou03f0d912013-08-07 20:01:00 -0700833 /* The unmasked key has to be the same for flow updates. */
834 error = -EINVAL;
Pravin B Shelare6445712013-10-03 18:16:47 -0700835 if (!ovs_flow_cmp_unmasked_key(flow, &match)) {
Andy Zhou03f0d912013-08-07 20:01:00 -0700836 OVS_NLERR("Flow modification message rejected, unmasked key does not match.\n");
837 goto err_unlock_ovs;
838 }
839
Jesse Grossccb13522011-10-25 19:26:31 -0700840 /* Update actions. */
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700841 old_acts = ovsl_dereference(flow->sf_acts);
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700842 rcu_assign_pointer(flow->sf_acts, acts);
Pravin B Shelare6445712013-10-03 18:16:47 -0700843 ovs_nla_free_flow_actions(old_acts);
Jesse Grossccb13522011-10-25 19:26:31 -0700844
Thomas Graf795449d2013-11-30 13:21:32 +0100845 reply = ovs_flow_cmd_build_info(flow, dp, info, OVS_FLOW_CMD_NEW);
Jesse Grossccb13522011-10-25 19:26:31 -0700846
847 /* Clear stats. */
Pravin B Shelare298e502013-10-29 17:22:21 -0700848 if (a[OVS_FLOW_ATTR_CLEAR])
849 ovs_flow_stats_clear(flow);
Jesse Grossccb13522011-10-25 19:26:31 -0700850 }
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700851 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -0700852
853 if (!IS_ERR(reply))
Johannes Berg2a94fe42013-11-19 15:19:39 +0100854 ovs_notify(&dp_flow_genl_family, reply, info);
Jesse Grossccb13522011-10-25 19:26:31 -0700855 else
Johannes Berg68eb5502013-11-19 15:19:38 +0100856 genl_set_err(&dp_flow_genl_family, sock_net(skb->sk), 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +0100857 0, PTR_ERR(reply));
Jesse Grossccb13522011-10-25 19:26:31 -0700858 return 0;
859
Andy Zhou03f0d912013-08-07 20:01:00 -0700860err_flow_free:
861 ovs_flow_free(flow, false);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700862err_unlock_ovs:
863 ovs_unlock();
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700864err_kfree:
865 kfree(acts);
Jesse Grossccb13522011-10-25 19:26:31 -0700866error:
867 return error;
868}
869
870static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
871{
872 struct nlattr **a = info->attrs;
873 struct ovs_header *ovs_header = info->userhdr;
874 struct sw_flow_key key;
875 struct sk_buff *reply;
876 struct sw_flow *flow;
877 struct datapath *dp;
Andy Zhou03f0d912013-08-07 20:01:00 -0700878 struct sw_flow_match match;
Jesse Grossccb13522011-10-25 19:26:31 -0700879 int err;
Jesse Grossccb13522011-10-25 19:26:31 -0700880
Andy Zhou03f0d912013-08-07 20:01:00 -0700881 if (!a[OVS_FLOW_ATTR_KEY]) {
882 OVS_NLERR("Flow get message rejected, Key attribute missing.\n");
Jesse Grossccb13522011-10-25 19:26:31 -0700883 return -EINVAL;
Andy Zhou03f0d912013-08-07 20:01:00 -0700884 }
885
886 ovs_match_init(&match, &key, NULL);
Pravin B Shelare298e502013-10-29 17:22:21 -0700887 err = ovs_nla_get_match(&match, NULL, a[OVS_FLOW_ATTR_KEY], NULL);
Jesse Grossccb13522011-10-25 19:26:31 -0700888 if (err)
889 return err;
890
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700891 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800892 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700893 if (!dp) {
894 err = -ENODEV;
895 goto unlock;
896 }
Jesse Grossccb13522011-10-25 19:26:31 -0700897
Andy Zhou5bb50632013-11-25 10:42:46 -0800898 flow = ovs_flow_tbl_lookup(&dp->table, &key);
Pravin B Shelare6445712013-10-03 18:16:47 -0700899 if (!flow || !ovs_flow_cmp_unmasked_key(flow, &match)) {
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700900 err = -ENOENT;
901 goto unlock;
902 }
Jesse Grossccb13522011-10-25 19:26:31 -0700903
Thomas Graf795449d2013-11-30 13:21:32 +0100904 reply = ovs_flow_cmd_build_info(flow, dp, info, OVS_FLOW_CMD_NEW);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700905 if (IS_ERR(reply)) {
906 err = PTR_ERR(reply);
907 goto unlock;
908 }
Jesse Grossccb13522011-10-25 19:26:31 -0700909
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700910 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -0700911 return genlmsg_reply(reply, info);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700912unlock:
913 ovs_unlock();
914 return err;
Jesse Grossccb13522011-10-25 19:26:31 -0700915}
916
917static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
918{
919 struct nlattr **a = info->attrs;
920 struct ovs_header *ovs_header = info->userhdr;
921 struct sw_flow_key key;
922 struct sk_buff *reply;
923 struct sw_flow *flow;
924 struct datapath *dp;
Andy Zhou03f0d912013-08-07 20:01:00 -0700925 struct sw_flow_match match;
Jesse Grossccb13522011-10-25 19:26:31 -0700926 int err;
Jesse Grossccb13522011-10-25 19:26:31 -0700927
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700928 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800929 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700930 if (!dp) {
931 err = -ENODEV;
932 goto unlock;
933 }
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800934
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700935 if (!a[OVS_FLOW_ATTR_KEY]) {
Pravin B Shelarb637e492013-10-04 00:14:23 -0700936 err = ovs_flow_tbl_flush(&dp->table);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700937 goto unlock;
938 }
Andy Zhou03f0d912013-08-07 20:01:00 -0700939
940 ovs_match_init(&match, &key, NULL);
Pravin B Shelare298e502013-10-29 17:22:21 -0700941 err = ovs_nla_get_match(&match, NULL, a[OVS_FLOW_ATTR_KEY], NULL);
Jesse Grossccb13522011-10-25 19:26:31 -0700942 if (err)
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700943 goto unlock;
Jesse Grossccb13522011-10-25 19:26:31 -0700944
Andy Zhou5bb50632013-11-25 10:42:46 -0800945 flow = ovs_flow_tbl_lookup(&dp->table, &key);
Pravin B Shelare6445712013-10-03 18:16:47 -0700946 if (!flow || !ovs_flow_cmp_unmasked_key(flow, &match)) {
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700947 err = -ENOENT;
948 goto unlock;
949 }
Jesse Grossccb13522011-10-25 19:26:31 -0700950
Thomas Graf795449d2013-11-30 13:21:32 +0100951 reply = ovs_flow_cmd_alloc_info(flow, info);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700952 if (!reply) {
953 err = -ENOMEM;
954 goto unlock;
955 }
Jesse Grossccb13522011-10-25 19:26:31 -0700956
Pravin B Shelarb637e492013-10-04 00:14:23 -0700957 ovs_flow_tbl_remove(&dp->table, flow);
Jesse Grossccb13522011-10-25 19:26:31 -0700958
Eric W. Biederman15e47302012-09-07 20:12:54 +0000959 err = ovs_flow_cmd_fill_info(flow, dp, reply, info->snd_portid,
Jesse Grossccb13522011-10-25 19:26:31 -0700960 info->snd_seq, 0, OVS_FLOW_CMD_DEL);
961 BUG_ON(err < 0);
962
Andy Zhou03f0d912013-08-07 20:01:00 -0700963 ovs_flow_free(flow, true);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700964 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -0700965
Johannes Berg2a94fe42013-11-19 15:19:39 +0100966 ovs_notify(&dp_flow_genl_family, reply, info);
Jesse Grossccb13522011-10-25 19:26:31 -0700967 return 0;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700968unlock:
969 ovs_unlock();
970 return err;
Jesse Grossccb13522011-10-25 19:26:31 -0700971}
972
973static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
974{
975 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
Pravin B Shelarb637e492013-10-04 00:14:23 -0700976 struct table_instance *ti;
Jesse Grossccb13522011-10-25 19:26:31 -0700977 struct datapath *dp;
Jesse Grossccb13522011-10-25 19:26:31 -0700978
Pravin B Shelard57170b2013-07-30 15:39:39 -0700979 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800980 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700981 if (!dp) {
Pravin B Shelard57170b2013-07-30 15:39:39 -0700982 rcu_read_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -0700983 return -ENODEV;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700984 }
Jesse Grossccb13522011-10-25 19:26:31 -0700985
Pravin B Shelarb637e492013-10-04 00:14:23 -0700986 ti = rcu_dereference(dp->table.ti);
Jesse Grossccb13522011-10-25 19:26:31 -0700987 for (;;) {
988 struct sw_flow *flow;
989 u32 bucket, obj;
990
991 bucket = cb->args[0];
992 obj = cb->args[1];
Pravin B Shelarb637e492013-10-04 00:14:23 -0700993 flow = ovs_flow_tbl_dump_next(ti, &bucket, &obj);
Jesse Grossccb13522011-10-25 19:26:31 -0700994 if (!flow)
995 break;
996
997 if (ovs_flow_cmd_fill_info(flow, dp, skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000998 NETLINK_CB(cb->skb).portid,
Jesse Grossccb13522011-10-25 19:26:31 -0700999 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1000 OVS_FLOW_CMD_NEW) < 0)
1001 break;
1002
1003 cb->args[0] = bucket;
1004 cb->args[1] = obj;
1005 }
Pravin B Shelard57170b2013-07-30 15:39:39 -07001006 rcu_read_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001007 return skb->len;
1008}
1009
Johannes Berg4534de82013-11-14 17:14:46 +01001010static const struct genl_ops dp_flow_genl_ops[] = {
Jesse Grossccb13522011-10-25 19:26:31 -07001011 { .cmd = OVS_FLOW_CMD_NEW,
1012 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1013 .policy = flow_policy,
1014 .doit = ovs_flow_cmd_new_or_set
1015 },
1016 { .cmd = OVS_FLOW_CMD_DEL,
1017 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1018 .policy = flow_policy,
1019 .doit = ovs_flow_cmd_del
1020 },
1021 { .cmd = OVS_FLOW_CMD_GET,
1022 .flags = 0, /* OK for unprivileged users. */
1023 .policy = flow_policy,
1024 .doit = ovs_flow_cmd_get,
1025 .dumpit = ovs_flow_cmd_dump
1026 },
1027 { .cmd = OVS_FLOW_CMD_SET,
1028 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1029 .policy = flow_policy,
1030 .doit = ovs_flow_cmd_new_or_set,
1031 },
1032};
1033
1034static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1035 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1036 [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
Thomas Graf43d4be92013-12-13 15:22:18 +01001037 [OVS_DP_ATTR_USER_FEATURES] = { .type = NLA_U32 },
Jesse Grossccb13522011-10-25 19:26:31 -07001038};
1039
1040static struct genl_family dp_datapath_genl_family = {
1041 .id = GENL_ID_GENERATE,
1042 .hdrsize = sizeof(struct ovs_header),
1043 .name = OVS_DATAPATH_FAMILY,
1044 .version = OVS_DATAPATH_VERSION,
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001045 .maxattr = OVS_DP_ATTR_MAX,
Pravin B Shelar3a4e0d62013-04-23 07:48:48 +00001046 .netnsok = true,
1047 .parallel_ops = true,
Jesse Grossccb13522011-10-25 19:26:31 -07001048};
1049
1050static struct genl_multicast_group ovs_dp_datapath_multicast_group = {
1051 .name = OVS_DATAPATH_MCGROUP
1052};
1053
Thomas Grafc3ff8cf2013-03-29 14:46:49 +01001054static size_t ovs_dp_cmd_msg_size(void)
1055{
1056 size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
1057
1058 msgsize += nla_total_size(IFNAMSIZ);
1059 msgsize += nla_total_size(sizeof(struct ovs_dp_stats));
Andy Zhou1bd71162013-10-22 10:42:46 -07001060 msgsize += nla_total_size(sizeof(struct ovs_dp_megaflow_stats));
Thomas Grafc3ff8cf2013-03-29 14:46:49 +01001061
1062 return msgsize;
1063}
1064
Jesse Grossccb13522011-10-25 19:26:31 -07001065static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001066 u32 portid, u32 seq, u32 flags, u8 cmd)
Jesse Grossccb13522011-10-25 19:26:31 -07001067{
1068 struct ovs_header *ovs_header;
1069 struct ovs_dp_stats dp_stats;
Andy Zhou1bd71162013-10-22 10:42:46 -07001070 struct ovs_dp_megaflow_stats dp_megaflow_stats;
Jesse Grossccb13522011-10-25 19:26:31 -07001071 int err;
1072
Eric W. Biederman15e47302012-09-07 20:12:54 +00001073 ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
Jesse Grossccb13522011-10-25 19:26:31 -07001074 flags, cmd);
1075 if (!ovs_header)
1076 goto error;
1077
1078 ovs_header->dp_ifindex = get_dpifindex(dp);
1079
1080 rcu_read_lock();
1081 err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
1082 rcu_read_unlock();
1083 if (err)
1084 goto nla_put_failure;
1085
Andy Zhou1bd71162013-10-22 10:42:46 -07001086 get_dp_stats(dp, &dp_stats, &dp_megaflow_stats);
1087 if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats),
1088 &dp_stats))
1089 goto nla_put_failure;
1090
1091 if (nla_put(skb, OVS_DP_ATTR_MEGAFLOW_STATS,
1092 sizeof(struct ovs_dp_megaflow_stats),
1093 &dp_megaflow_stats))
David S. Miller028d6a62012-03-29 23:20:48 -04001094 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001095
Thomas Graf43d4be92013-12-13 15:22:18 +01001096 if (nla_put_u32(skb, OVS_DP_ATTR_USER_FEATURES, dp->user_features))
1097 goto nla_put_failure;
1098
Jesse Grossccb13522011-10-25 19:26:31 -07001099 return genlmsg_end(skb, ovs_header);
1100
1101nla_put_failure:
1102 genlmsg_cancel(skb, ovs_header);
1103error:
1104 return -EMSGSIZE;
1105}
1106
Thomas Graf795449d2013-11-30 13:21:32 +01001107static struct sk_buff *ovs_dp_cmd_build_info(struct datapath *dp,
1108 struct genl_info *info, u8 cmd)
Jesse Grossccb13522011-10-25 19:26:31 -07001109{
1110 struct sk_buff *skb;
1111 int retval;
1112
Thomas Graf795449d2013-11-30 13:21:32 +01001113 skb = genlmsg_new_unicast(ovs_dp_cmd_msg_size(), info, GFP_KERNEL);
Jesse Grossccb13522011-10-25 19:26:31 -07001114 if (!skb)
1115 return ERR_PTR(-ENOMEM);
1116
Thomas Graf795449d2013-11-30 13:21:32 +01001117 retval = ovs_dp_cmd_fill_info(dp, skb, info->snd_portid, info->snd_seq, 0, cmd);
Jesse Grossccb13522011-10-25 19:26:31 -07001118 if (retval < 0) {
1119 kfree_skb(skb);
1120 return ERR_PTR(retval);
1121 }
1122 return skb;
1123}
1124
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001125/* Called with ovs_mutex. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001126static struct datapath *lookup_datapath(struct net *net,
1127 struct ovs_header *ovs_header,
Jesse Grossccb13522011-10-25 19:26:31 -07001128 struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1129{
1130 struct datapath *dp;
1131
1132 if (!a[OVS_DP_ATTR_NAME])
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001133 dp = get_dp(net, ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001134 else {
1135 struct vport *vport;
1136
1137 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001138 vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
Jesse Grossccb13522011-10-25 19:26:31 -07001139 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
1140 rcu_read_unlock();
1141 }
1142 return dp ? dp : ERR_PTR(-ENODEV);
1143}
1144
Thomas Graf44da5ae2013-12-13 15:22:19 +01001145static void ovs_dp_reset_user_features(struct sk_buff *skb, struct genl_info *info)
1146{
1147 struct datapath *dp;
1148
1149 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1150 if (!dp)
1151 return;
1152
1153 WARN(dp->user_features, "Dropping previously announced user features\n");
1154 dp->user_features = 0;
1155}
1156
Thomas Graf43d4be92013-12-13 15:22:18 +01001157static void ovs_dp_change(struct datapath *dp, struct nlattr **a)
1158{
1159 if (a[OVS_DP_ATTR_USER_FEATURES])
1160 dp->user_features = nla_get_u32(a[OVS_DP_ATTR_USER_FEATURES]);
1161}
1162
Jesse Grossccb13522011-10-25 19:26:31 -07001163static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1164{
1165 struct nlattr **a = info->attrs;
1166 struct vport_parms parms;
1167 struct sk_buff *reply;
1168 struct datapath *dp;
1169 struct vport *vport;
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001170 struct ovs_net *ovs_net;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001171 int err, i;
Jesse Grossccb13522011-10-25 19:26:31 -07001172
1173 err = -EINVAL;
1174 if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1175 goto err;
1176
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001177 ovs_lock();
Jesse Grossccb13522011-10-25 19:26:31 -07001178
1179 err = -ENOMEM;
1180 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1181 if (dp == NULL)
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001182 goto err_unlock_ovs;
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001183
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001184 ovs_dp_set_net(dp, hold_net(sock_net(skb->sk)));
Jesse Grossccb13522011-10-25 19:26:31 -07001185
1186 /* Allocate table. */
Pravin B Shelarb637e492013-10-04 00:14:23 -07001187 err = ovs_flow_tbl_init(&dp->table);
1188 if (err)
Jesse Grossccb13522011-10-25 19:26:31 -07001189 goto err_free_dp;
1190
1191 dp->stats_percpu = alloc_percpu(struct dp_stats_percpu);
1192 if (!dp->stats_percpu) {
1193 err = -ENOMEM;
1194 goto err_destroy_table;
1195 }
1196
John Stultz827da442013-10-07 15:51:58 -07001197 for_each_possible_cpu(i) {
1198 struct dp_stats_percpu *dpath_stats;
1199 dpath_stats = per_cpu_ptr(dp->stats_percpu, i);
1200 u64_stats_init(&dpath_stats->sync);
1201 }
1202
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001203 dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
Pravin B Shelare6445712013-10-03 18:16:47 -07001204 GFP_KERNEL);
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001205 if (!dp->ports) {
1206 err = -ENOMEM;
1207 goto err_destroy_percpu;
1208 }
1209
1210 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1211 INIT_HLIST_HEAD(&dp->ports[i]);
1212
Jesse Grossccb13522011-10-25 19:26:31 -07001213 /* Set up our datapath device. */
1214 parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1215 parms.type = OVS_VPORT_TYPE_INTERNAL;
1216 parms.options = NULL;
1217 parms.dp = dp;
1218 parms.port_no = OVSP_LOCAL;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001219 parms.upcall_portid = nla_get_u32(a[OVS_DP_ATTR_UPCALL_PID]);
Jesse Grossccb13522011-10-25 19:26:31 -07001220
Thomas Graf43d4be92013-12-13 15:22:18 +01001221 ovs_dp_change(dp, a);
1222
Jesse Grossccb13522011-10-25 19:26:31 -07001223 vport = new_vport(&parms);
1224 if (IS_ERR(vport)) {
1225 err = PTR_ERR(vport);
1226 if (err == -EBUSY)
1227 err = -EEXIST;
1228
Thomas Graf44da5ae2013-12-13 15:22:19 +01001229 if (err == -EEXIST) {
1230 /* An outdated user space instance that does not understand
1231 * the concept of user_features has attempted to create a new
1232 * datapath and is likely to reuse it. Drop all user features.
1233 */
1234 if (info->genlhdr->version < OVS_DP_VER_FEATURES)
1235 ovs_dp_reset_user_features(skb, info);
1236 }
1237
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001238 goto err_destroy_ports_array;
Jesse Grossccb13522011-10-25 19:26:31 -07001239 }
1240
Thomas Graf795449d2013-11-30 13:21:32 +01001241 reply = ovs_dp_cmd_build_info(dp, info, OVS_DP_CMD_NEW);
Jesse Grossccb13522011-10-25 19:26:31 -07001242 err = PTR_ERR(reply);
1243 if (IS_ERR(reply))
1244 goto err_destroy_local_port;
1245
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001246 ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001247 list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001248
1249 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001250
Johannes Berg2a94fe42013-11-19 15:19:39 +01001251 ovs_notify(&dp_datapath_genl_family, reply, info);
Jesse Grossccb13522011-10-25 19:26:31 -07001252 return 0;
1253
1254err_destroy_local_port:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001255 ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001256err_destroy_ports_array:
1257 kfree(dp->ports);
Jesse Grossccb13522011-10-25 19:26:31 -07001258err_destroy_percpu:
1259 free_percpu(dp->stats_percpu);
1260err_destroy_table:
Pravin B Shelar618ed0c2013-10-04 00:17:42 -07001261 ovs_flow_tbl_destroy(&dp->table);
Jesse Grossccb13522011-10-25 19:26:31 -07001262err_free_dp:
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001263 release_net(ovs_dp_get_net(dp));
Jesse Grossccb13522011-10-25 19:26:31 -07001264 kfree(dp);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001265err_unlock_ovs:
1266 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001267err:
1268 return err;
1269}
1270
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001271/* Called with ovs_mutex. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001272static void __dp_destroy(struct datapath *dp)
Jesse Grossccb13522011-10-25 19:26:31 -07001273{
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001274 int i;
Jesse Grossccb13522011-10-25 19:26:31 -07001275
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001276 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1277 struct vport *vport;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001278 struct hlist_node *n;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001279
Sasha Levinb67bfe02013-02-27 17:06:00 -08001280 hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001281 if (vport->port_no != OVSP_LOCAL)
1282 ovs_dp_detach_port(vport);
1283 }
Jesse Grossccb13522011-10-25 19:26:31 -07001284
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001285 list_del_rcu(&dp->list_node);
Jesse Grossccb13522011-10-25 19:26:31 -07001286
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001287 /* OVSP_LOCAL is datapath internal port. We need to make sure that
1288 * all port in datapath are destroyed first before freeing datapath.
Jesse Grossccb13522011-10-25 19:26:31 -07001289 */
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001290 ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
Jesse Grossccb13522011-10-25 19:26:31 -07001291
1292 call_rcu(&dp->rcu, destroy_dp_rcu);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001293}
1294
1295static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1296{
1297 struct sk_buff *reply;
1298 struct datapath *dp;
1299 int err;
1300
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001301 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001302 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1303 err = PTR_ERR(dp);
1304 if (IS_ERR(dp))
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001305 goto unlock;
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001306
Thomas Graf795449d2013-11-30 13:21:32 +01001307 reply = ovs_dp_cmd_build_info(dp, info, OVS_DP_CMD_DEL);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001308 err = PTR_ERR(reply);
1309 if (IS_ERR(reply))
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001310 goto unlock;
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001311
1312 __dp_destroy(dp);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001313 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001314
Johannes Berg2a94fe42013-11-19 15:19:39 +01001315 ovs_notify(&dp_datapath_genl_family, reply, info);
Jesse Grossccb13522011-10-25 19:26:31 -07001316
1317 return 0;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001318unlock:
1319 ovs_unlock();
1320 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001321}
1322
1323static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1324{
1325 struct sk_buff *reply;
1326 struct datapath *dp;
1327 int err;
1328
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001329 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001330 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001331 err = PTR_ERR(dp);
Jesse Grossccb13522011-10-25 19:26:31 -07001332 if (IS_ERR(dp))
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001333 goto unlock;
Jesse Grossccb13522011-10-25 19:26:31 -07001334
Thomas Graf43d4be92013-12-13 15:22:18 +01001335 ovs_dp_change(dp, info->attrs);
1336
Thomas Graf795449d2013-11-30 13:21:32 +01001337 reply = ovs_dp_cmd_build_info(dp, info, OVS_DP_CMD_NEW);
Jesse Grossccb13522011-10-25 19:26:31 -07001338 if (IS_ERR(reply)) {
1339 err = PTR_ERR(reply);
Johannes Berg68eb5502013-11-19 15:19:38 +01001340 genl_set_err(&dp_datapath_genl_family, sock_net(skb->sk), 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +01001341 0, err);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001342 err = 0;
1343 goto unlock;
Jesse Grossccb13522011-10-25 19:26:31 -07001344 }
1345
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001346 ovs_unlock();
Johannes Berg2a94fe42013-11-19 15:19:39 +01001347 ovs_notify(&dp_datapath_genl_family, reply, info);
Jesse Grossccb13522011-10-25 19:26:31 -07001348
1349 return 0;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001350unlock:
1351 ovs_unlock();
1352 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001353}
1354
1355static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1356{
1357 struct sk_buff *reply;
1358 struct datapath *dp;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001359 int err;
Jesse Grossccb13522011-10-25 19:26:31 -07001360
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001361 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001362 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001363 if (IS_ERR(dp)) {
1364 err = PTR_ERR(dp);
1365 goto unlock;
1366 }
Jesse Grossccb13522011-10-25 19:26:31 -07001367
Thomas Graf795449d2013-11-30 13:21:32 +01001368 reply = ovs_dp_cmd_build_info(dp, info, OVS_DP_CMD_NEW);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001369 if (IS_ERR(reply)) {
1370 err = PTR_ERR(reply);
1371 goto unlock;
1372 }
Jesse Grossccb13522011-10-25 19:26:31 -07001373
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001374 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001375 return genlmsg_reply(reply, info);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001376
1377unlock:
1378 ovs_unlock();
1379 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001380}
1381
1382static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1383{
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001384 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
Jesse Grossccb13522011-10-25 19:26:31 -07001385 struct datapath *dp;
1386 int skip = cb->args[0];
1387 int i = 0;
1388
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001389 rcu_read_lock();
1390 list_for_each_entry_rcu(dp, &ovs_net->dps, list_node) {
Ben Pfaff77676fd2012-01-17 13:33:39 +00001391 if (i >= skip &&
Eric W. Biederman15e47302012-09-07 20:12:54 +00001392 ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001393 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1394 OVS_DP_CMD_NEW) < 0)
1395 break;
1396 i++;
1397 }
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001398 rcu_read_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001399
1400 cb->args[0] = i;
1401
1402 return skb->len;
1403}
1404
Johannes Berg4534de82013-11-14 17:14:46 +01001405static const struct genl_ops dp_datapath_genl_ops[] = {
Jesse Grossccb13522011-10-25 19:26:31 -07001406 { .cmd = OVS_DP_CMD_NEW,
1407 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1408 .policy = datapath_policy,
1409 .doit = ovs_dp_cmd_new
1410 },
1411 { .cmd = OVS_DP_CMD_DEL,
1412 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1413 .policy = datapath_policy,
1414 .doit = ovs_dp_cmd_del
1415 },
1416 { .cmd = OVS_DP_CMD_GET,
1417 .flags = 0, /* OK for unprivileged users. */
1418 .policy = datapath_policy,
1419 .doit = ovs_dp_cmd_get,
1420 .dumpit = ovs_dp_cmd_dump
1421 },
1422 { .cmd = OVS_DP_CMD_SET,
1423 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1424 .policy = datapath_policy,
1425 .doit = ovs_dp_cmd_set,
1426 },
1427};
1428
1429static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
1430 [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1431 [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
1432 [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
1433 [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
1434 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1435 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
1436};
1437
Johannes Berg68eb5502013-11-19 15:19:38 +01001438struct genl_family dp_vport_genl_family = {
Jesse Grossccb13522011-10-25 19:26:31 -07001439 .id = GENL_ID_GENERATE,
1440 .hdrsize = sizeof(struct ovs_header),
1441 .name = OVS_VPORT_FAMILY,
1442 .version = OVS_VPORT_VERSION,
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001443 .maxattr = OVS_VPORT_ATTR_MAX,
Pravin B Shelar3a4e0d62013-04-23 07:48:48 +00001444 .netnsok = true,
1445 .parallel_ops = true,
Jesse Grossccb13522011-10-25 19:26:31 -07001446};
1447
1448struct genl_multicast_group ovs_dp_vport_multicast_group = {
1449 .name = OVS_VPORT_MCGROUP
1450};
1451
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001452/* Called with ovs_mutex or RCU read lock. */
Jesse Grossccb13522011-10-25 19:26:31 -07001453static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001454 u32 portid, u32 seq, u32 flags, u8 cmd)
Jesse Grossccb13522011-10-25 19:26:31 -07001455{
1456 struct ovs_header *ovs_header;
1457 struct ovs_vport_stats vport_stats;
1458 int err;
1459
Eric W. Biederman15e47302012-09-07 20:12:54 +00001460 ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
Jesse Grossccb13522011-10-25 19:26:31 -07001461 flags, cmd);
1462 if (!ovs_header)
1463 return -EMSGSIZE;
1464
1465 ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1466
David S. Miller028d6a62012-03-29 23:20:48 -04001467 if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
1468 nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
1469 nla_put_string(skb, OVS_VPORT_ATTR_NAME, vport->ops->get_name(vport)) ||
Eric W. Biederman15e47302012-09-07 20:12:54 +00001470 nla_put_u32(skb, OVS_VPORT_ATTR_UPCALL_PID, vport->upcall_portid))
David S. Miller028d6a62012-03-29 23:20:48 -04001471 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001472
1473 ovs_vport_get_stats(vport, &vport_stats);
David S. Miller028d6a62012-03-29 23:20:48 -04001474 if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
1475 &vport_stats))
1476 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001477
1478 err = ovs_vport_get_options(vport, skb);
1479 if (err == -EMSGSIZE)
1480 goto error;
1481
1482 return genlmsg_end(skb, ovs_header);
1483
1484nla_put_failure:
1485 err = -EMSGSIZE;
1486error:
1487 genlmsg_cancel(skb, ovs_header);
1488 return err;
1489}
1490
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001491/* Called with ovs_mutex or RCU read lock. */
Eric W. Biederman15e47302012-09-07 20:12:54 +00001492struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001493 u32 seq, u8 cmd)
1494{
1495 struct sk_buff *skb;
1496 int retval;
1497
1498 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1499 if (!skb)
1500 return ERR_PTR(-ENOMEM);
1501
Eric W. Biederman15e47302012-09-07 20:12:54 +00001502 retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
Jesse Grossa9341512013-03-26 15:48:38 -07001503 BUG_ON(retval < 0);
1504
Jesse Grossccb13522011-10-25 19:26:31 -07001505 return skb;
1506}
1507
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001508/* Called with ovs_mutex or RCU read lock. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001509static struct vport *lookup_vport(struct net *net,
1510 struct ovs_header *ovs_header,
Jesse Grossccb13522011-10-25 19:26:31 -07001511 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1512{
1513 struct datapath *dp;
1514 struct vport *vport;
1515
1516 if (a[OVS_VPORT_ATTR_NAME]) {
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001517 vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
Jesse Grossccb13522011-10-25 19:26:31 -07001518 if (!vport)
1519 return ERR_PTR(-ENODEV);
Ben Pfaff651a68e2012-03-06 15:04:04 -08001520 if (ovs_header->dp_ifindex &&
1521 ovs_header->dp_ifindex != get_dpifindex(vport->dp))
1522 return ERR_PTR(-ENODEV);
Jesse Grossccb13522011-10-25 19:26:31 -07001523 return vport;
1524 } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
1525 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1526
1527 if (port_no >= DP_MAX_PORTS)
1528 return ERR_PTR(-EFBIG);
1529
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001530 dp = get_dp(net, ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001531 if (!dp)
1532 return ERR_PTR(-ENODEV);
1533
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001534 vport = ovs_vport_ovsl_rcu(dp, port_no);
Jesse Grossccb13522011-10-25 19:26:31 -07001535 if (!vport)
Jarno Rajahalme14408db2013-01-09 14:27:35 -08001536 return ERR_PTR(-ENODEV);
Jesse Grossccb13522011-10-25 19:26:31 -07001537 return vport;
1538 } else
1539 return ERR_PTR(-EINVAL);
1540}
1541
1542static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
1543{
1544 struct nlattr **a = info->attrs;
1545 struct ovs_header *ovs_header = info->userhdr;
1546 struct vport_parms parms;
1547 struct sk_buff *reply;
1548 struct vport *vport;
1549 struct datapath *dp;
1550 u32 port_no;
1551 int err;
1552
1553 err = -EINVAL;
1554 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
1555 !a[OVS_VPORT_ATTR_UPCALL_PID])
1556 goto exit;
1557
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001558 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001559 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001560 err = -ENODEV;
1561 if (!dp)
1562 goto exit_unlock;
1563
1564 if (a[OVS_VPORT_ATTR_PORT_NO]) {
1565 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1566
1567 err = -EFBIG;
1568 if (port_no >= DP_MAX_PORTS)
1569 goto exit_unlock;
1570
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001571 vport = ovs_vport_ovsl(dp, port_no);
Jesse Grossccb13522011-10-25 19:26:31 -07001572 err = -EBUSY;
1573 if (vport)
1574 goto exit_unlock;
1575 } else {
1576 for (port_no = 1; ; port_no++) {
1577 if (port_no >= DP_MAX_PORTS) {
1578 err = -EFBIG;
1579 goto exit_unlock;
1580 }
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001581 vport = ovs_vport_ovsl(dp, port_no);
Jesse Grossccb13522011-10-25 19:26:31 -07001582 if (!vport)
1583 break;
1584 }
1585 }
1586
1587 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
1588 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1589 parms.options = a[OVS_VPORT_ATTR_OPTIONS];
1590 parms.dp = dp;
1591 parms.port_no = port_no;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001592 parms.upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
Jesse Grossccb13522011-10-25 19:26:31 -07001593
1594 vport = new_vport(&parms);
1595 err = PTR_ERR(vport);
1596 if (IS_ERR(vport))
1597 goto exit_unlock;
1598
Rich Lanecb7c5bd2013-02-08 13:18:01 -08001599 err = 0;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001600 reply = ovs_vport_cmd_build_info(vport, info->snd_portid, info->snd_seq,
Jesse Grossccb13522011-10-25 19:26:31 -07001601 OVS_VPORT_CMD_NEW);
1602 if (IS_ERR(reply)) {
1603 err = PTR_ERR(reply);
1604 ovs_dp_detach_port(vport);
1605 goto exit_unlock;
1606 }
Thomas Grafed6611852013-03-29 14:46:50 +01001607
Johannes Berg2a94fe42013-11-19 15:19:39 +01001608 ovs_notify(&dp_vport_genl_family, reply, info);
Jesse Grossccb13522011-10-25 19:26:31 -07001609
1610exit_unlock:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001611 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001612exit:
1613 return err;
1614}
1615
1616static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
1617{
1618 struct nlattr **a = info->attrs;
1619 struct sk_buff *reply;
1620 struct vport *vport;
1621 int err;
1622
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001623 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001624 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
Jesse Grossccb13522011-10-25 19:26:31 -07001625 err = PTR_ERR(vport);
1626 if (IS_ERR(vport))
1627 goto exit_unlock;
1628
Jesse Grossccb13522011-10-25 19:26:31 -07001629 if (a[OVS_VPORT_ATTR_TYPE] &&
Jesse Grossf44f3402013-05-13 08:15:26 -07001630 nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
Jesse Grossccb13522011-10-25 19:26:31 -07001631 err = -EINVAL;
Jesse Grossf44f3402013-05-13 08:15:26 -07001632 goto exit_unlock;
1633 }
Jesse Grossccb13522011-10-25 19:26:31 -07001634
Jesse Grossa9341512013-03-26 15:48:38 -07001635 reply = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1636 if (!reply) {
1637 err = -ENOMEM;
1638 goto exit_unlock;
1639 }
1640
Jesse Grossf44f3402013-05-13 08:15:26 -07001641 if (a[OVS_VPORT_ATTR_OPTIONS]) {
Jesse Grossccb13522011-10-25 19:26:31 -07001642 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
Jesse Grossf44f3402013-05-13 08:15:26 -07001643 if (err)
1644 goto exit_free;
1645 }
Jesse Grossa9341512013-03-26 15:48:38 -07001646
Ansis Atteka03fbf8b2012-04-09 12:12:12 -07001647 if (a[OVS_VPORT_ATTR_UPCALL_PID])
Eric W. Biederman15e47302012-09-07 20:12:54 +00001648 vport->upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
Jesse Grossccb13522011-10-25 19:26:31 -07001649
Jesse Grossa9341512013-03-26 15:48:38 -07001650 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1651 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1652 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001653
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001654 ovs_unlock();
Johannes Berg2a94fe42013-11-19 15:19:39 +01001655 ovs_notify(&dp_vport_genl_family, reply, info);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001656 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001657
Jesse Grossa9341512013-03-26 15:48:38 -07001658exit_free:
1659 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001660exit_unlock:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001661 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001662 return err;
1663}
1664
1665static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
1666{
1667 struct nlattr **a = info->attrs;
1668 struct sk_buff *reply;
1669 struct vport *vport;
1670 int err;
1671
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001672 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001673 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
Jesse Grossccb13522011-10-25 19:26:31 -07001674 err = PTR_ERR(vport);
1675 if (IS_ERR(vport))
1676 goto exit_unlock;
1677
1678 if (vport->port_no == OVSP_LOCAL) {
1679 err = -EINVAL;
1680 goto exit_unlock;
1681 }
1682
Pravin B Shelar74f84a52013-06-17 17:50:12 -07001683 reply = ovs_vport_cmd_build_info(vport, info->snd_portid,
1684 info->snd_seq, OVS_VPORT_CMD_DEL);
Jesse Grossccb13522011-10-25 19:26:31 -07001685 err = PTR_ERR(reply);
1686 if (IS_ERR(reply))
1687 goto exit_unlock;
1688
Rich Lane734907e2013-02-08 09:30:23 -08001689 err = 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001690 ovs_dp_detach_port(vport);
1691
Johannes Berg2a94fe42013-11-19 15:19:39 +01001692 ovs_notify(&dp_vport_genl_family, reply, info);
Jesse Grossccb13522011-10-25 19:26:31 -07001693
1694exit_unlock:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001695 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001696 return err;
1697}
1698
1699static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
1700{
1701 struct nlattr **a = info->attrs;
1702 struct ovs_header *ovs_header = info->userhdr;
1703 struct sk_buff *reply;
1704 struct vport *vport;
1705 int err;
1706
1707 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001708 vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
Jesse Grossccb13522011-10-25 19:26:31 -07001709 err = PTR_ERR(vport);
1710 if (IS_ERR(vport))
1711 goto exit_unlock;
1712
Pravin B Shelar74f84a52013-06-17 17:50:12 -07001713 reply = ovs_vport_cmd_build_info(vport, info->snd_portid,
1714 info->snd_seq, OVS_VPORT_CMD_NEW);
Jesse Grossccb13522011-10-25 19:26:31 -07001715 err = PTR_ERR(reply);
1716 if (IS_ERR(reply))
1717 goto exit_unlock;
1718
1719 rcu_read_unlock();
1720
1721 return genlmsg_reply(reply, info);
1722
1723exit_unlock:
1724 rcu_read_unlock();
1725 return err;
1726}
1727
1728static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1729{
1730 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1731 struct datapath *dp;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001732 int bucket = cb->args[0], skip = cb->args[1];
1733 int i, j = 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001734
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001735 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001736 if (!dp)
1737 return -ENODEV;
1738
1739 rcu_read_lock();
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001740 for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
Jesse Grossccb13522011-10-25 19:26:31 -07001741 struct vport *vport;
1742
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001743 j = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001744 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001745 if (j >= skip &&
1746 ovs_vport_cmd_fill_info(vport, skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001747 NETLINK_CB(cb->skb).portid,
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001748 cb->nlh->nlmsg_seq,
1749 NLM_F_MULTI,
1750 OVS_VPORT_CMD_NEW) < 0)
1751 goto out;
Jesse Grossccb13522011-10-25 19:26:31 -07001752
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001753 j++;
1754 }
1755 skip = 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001756 }
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001757out:
Jesse Grossccb13522011-10-25 19:26:31 -07001758 rcu_read_unlock();
1759
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001760 cb->args[0] = i;
1761 cb->args[1] = j;
Jesse Grossccb13522011-10-25 19:26:31 -07001762
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001763 return skb->len;
Jesse Grossccb13522011-10-25 19:26:31 -07001764}
1765
Johannes Berg4534de82013-11-14 17:14:46 +01001766static const struct genl_ops dp_vport_genl_ops[] = {
Jesse Grossccb13522011-10-25 19:26:31 -07001767 { .cmd = OVS_VPORT_CMD_NEW,
1768 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1769 .policy = vport_policy,
1770 .doit = ovs_vport_cmd_new
1771 },
1772 { .cmd = OVS_VPORT_CMD_DEL,
1773 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1774 .policy = vport_policy,
1775 .doit = ovs_vport_cmd_del
1776 },
1777 { .cmd = OVS_VPORT_CMD_GET,
1778 .flags = 0, /* OK for unprivileged users. */
1779 .policy = vport_policy,
1780 .doit = ovs_vport_cmd_get,
1781 .dumpit = ovs_vport_cmd_dump
1782 },
1783 { .cmd = OVS_VPORT_CMD_SET,
1784 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1785 .policy = vport_policy,
1786 .doit = ovs_vport_cmd_set,
1787 },
1788};
1789
1790struct genl_family_and_ops {
1791 struct genl_family *family;
Johannes Berg4534de82013-11-14 17:14:46 +01001792 const struct genl_ops *ops;
Jesse Grossccb13522011-10-25 19:26:31 -07001793 int n_ops;
Johannes Berg2a94fe42013-11-19 15:19:39 +01001794 const struct genl_multicast_group *group;
Jesse Grossccb13522011-10-25 19:26:31 -07001795};
1796
1797static const struct genl_family_and_ops dp_genl_families[] = {
1798 { &dp_datapath_genl_family,
1799 dp_datapath_genl_ops, ARRAY_SIZE(dp_datapath_genl_ops),
1800 &ovs_dp_datapath_multicast_group },
1801 { &dp_vport_genl_family,
1802 dp_vport_genl_ops, ARRAY_SIZE(dp_vport_genl_ops),
1803 &ovs_dp_vport_multicast_group },
1804 { &dp_flow_genl_family,
1805 dp_flow_genl_ops, ARRAY_SIZE(dp_flow_genl_ops),
1806 &ovs_dp_flow_multicast_group },
1807 { &dp_packet_genl_family,
1808 dp_packet_genl_ops, ARRAY_SIZE(dp_packet_genl_ops),
1809 NULL },
1810};
1811
1812static void dp_unregister_genl(int n_families)
1813{
1814 int i;
1815
1816 for (i = 0; i < n_families; i++)
1817 genl_unregister_family(dp_genl_families[i].family);
1818}
1819
1820static int dp_register_genl(void)
1821{
1822 int n_registered;
1823 int err;
1824 int i;
1825
1826 n_registered = 0;
1827 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
1828 const struct genl_family_and_ops *f = &dp_genl_families[i];
1829
Johannes Bergc53ed742013-11-19 15:19:31 +01001830 f->family->ops = f->ops;
1831 f->family->n_ops = f->n_ops;
Johannes Berg2a94fe42013-11-19 15:19:39 +01001832 f->family->mcgrps = f->group;
1833 f->family->n_mcgrps = f->group ? 1 : 0;
Johannes Bergc53ed742013-11-19 15:19:31 +01001834 err = genl_register_family(f->family);
Jesse Grossccb13522011-10-25 19:26:31 -07001835 if (err)
1836 goto error;
1837 n_registered++;
Jesse Grossccb13522011-10-25 19:26:31 -07001838 }
1839
1840 return 0;
1841
1842error:
1843 dp_unregister_genl(n_registered);
1844 return err;
1845}
1846
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001847static int __net_init ovs_init_net(struct net *net)
1848{
1849 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
1850
1851 INIT_LIST_HEAD(&ovs_net->dps);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001852 INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001853 return 0;
1854}
1855
1856static void __net_exit ovs_exit_net(struct net *net)
1857{
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001858 struct datapath *dp, *dp_next;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001859 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001860
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001861 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001862 list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
1863 __dp_destroy(dp);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001864 ovs_unlock();
1865
1866 cancel_work_sync(&ovs_net->dp_notify_work);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001867}
1868
1869static struct pernet_operations ovs_net_ops = {
1870 .init = ovs_init_net,
1871 .exit = ovs_exit_net,
1872 .id = &ovs_net_id,
1873 .size = sizeof(struct ovs_net),
1874};
1875
Jesse Grossccb13522011-10-25 19:26:31 -07001876static int __init dp_init(void)
1877{
Jesse Grossccb13522011-10-25 19:26:31 -07001878 int err;
1879
YOSHIFUJI Hideaki / 吉藤英明3523b292013-01-09 07:19:55 +00001880 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
Jesse Grossccb13522011-10-25 19:26:31 -07001881
1882 pr_info("Open vSwitch switching datapath\n");
1883
1884 err = ovs_flow_init();
1885 if (err)
1886 goto error;
1887
1888 err = ovs_vport_init();
1889 if (err)
1890 goto error_flow_exit;
1891
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001892 err = register_pernet_device(&ovs_net_ops);
Jesse Grossccb13522011-10-25 19:26:31 -07001893 if (err)
1894 goto error_vport_exit;
1895
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001896 err = register_netdevice_notifier(&ovs_dp_device_notifier);
1897 if (err)
1898 goto error_netns_exit;
1899
Jesse Grossccb13522011-10-25 19:26:31 -07001900 err = dp_register_genl();
1901 if (err < 0)
1902 goto error_unreg_notifier;
1903
Jesse Grossccb13522011-10-25 19:26:31 -07001904 return 0;
1905
1906error_unreg_notifier:
1907 unregister_netdevice_notifier(&ovs_dp_device_notifier);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001908error_netns_exit:
1909 unregister_pernet_device(&ovs_net_ops);
Jesse Grossccb13522011-10-25 19:26:31 -07001910error_vport_exit:
1911 ovs_vport_exit();
1912error_flow_exit:
1913 ovs_flow_exit();
1914error:
1915 return err;
1916}
1917
1918static void dp_cleanup(void)
1919{
Jesse Grossccb13522011-10-25 19:26:31 -07001920 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
1921 unregister_netdevice_notifier(&ovs_dp_device_notifier);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001922 unregister_pernet_device(&ovs_net_ops);
1923 rcu_barrier();
Jesse Grossccb13522011-10-25 19:26:31 -07001924 ovs_vport_exit();
1925 ovs_flow_exit();
1926}
1927
1928module_init(dp_init);
1929module_exit(dp_cleanup);
1930
1931MODULE_DESCRIPTION("Open vSwitch switching datapath");
1932MODULE_LICENSE("GPL");