blob: b7d0b7c3fe2c35234966b499a8e0d31229f3b1e6 [file] [log] [blame]
Jesse Grossccb13522011-10-25 19:26:31 -07001/*
Raju Subramaniancaf2ee12012-05-03 18:55:23 -07002 * Copyright (c) 2007-2012 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"
58#include "vport-internal_dev.h"
59
Pravin B Shelar46df7b82012-02-22 19:58:59 -080060
61#define REHASH_FLOW_INTERVAL (10 * 60 * HZ)
62static void rehash_flow_table(struct work_struct *work);
63static DECLARE_DELAYED_WORK(rehash_flow_wq, rehash_flow_table);
64
Pravin B Shelar8e4e1712013-04-15 13:23:03 -070065int ovs_net_id __read_mostly;
66
Thomas Grafed661182013-03-29 14:46:50 +010067static void ovs_notify(struct sk_buff *skb, struct genl_info *info,
68 struct genl_multicast_group *grp)
69{
70 genl_notify(skb, genl_info_net(info), info->snd_portid,
71 grp->id, info->nlhdr, GFP_KERNEL);
72}
73
Pravin B Shelar46df7b82012-02-22 19:58:59 -080074/**
Jesse Grossccb13522011-10-25 19:26:31 -070075 * DOC: Locking:
76 *
Pravin B Shelar8e4e1712013-04-15 13:23:03 -070077 * All writes e.g. Writes to device state (add/remove datapath, port, set
78 * operations on vports, etc.), Writes to other state (flow table
79 * modifications, set miscellaneous datapath parameters, etc.) are protected
80 * by ovs_lock.
Jesse Grossccb13522011-10-25 19:26:31 -070081 *
82 * Reads are protected by RCU.
83 *
84 * There are a few special cases (mostly stats) that have their own
85 * synchronization but they nest under all of above and don't interact with
86 * each other.
Pravin B Shelar8e4e1712013-04-15 13:23:03 -070087 *
88 * The RTNL lock nests inside ovs_mutex.
Jesse Grossccb13522011-10-25 19:26:31 -070089 */
90
Pravin B Shelar8e4e1712013-04-15 13:23:03 -070091static DEFINE_MUTEX(ovs_mutex);
92
93void ovs_lock(void)
94{
95 mutex_lock(&ovs_mutex);
96}
97
98void ovs_unlock(void)
99{
100 mutex_unlock(&ovs_mutex);
101}
102
103#ifdef CONFIG_LOCKDEP
104int lockdep_ovsl_is_held(void)
105{
106 if (debug_locks)
107 return lockdep_is_held(&ovs_mutex);
108 else
109 return 1;
110}
111#endif
112
Jesse Grossccb13522011-10-25 19:26:31 -0700113static struct vport *new_vport(const struct vport_parms *);
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800114static int queue_gso_packets(struct net *, int dp_ifindex, struct sk_buff *,
Jesse Grossccb13522011-10-25 19:26:31 -0700115 const struct dp_upcall_info *);
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800116static int queue_userspace_packet(struct net *, int dp_ifindex,
117 struct sk_buff *,
Jesse Grossccb13522011-10-25 19:26:31 -0700118 const struct dp_upcall_info *);
119
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700120/* Must be called with rcu_read_lock or ovs_mutex. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800121static struct datapath *get_dp(struct net *net, int dp_ifindex)
Jesse Grossccb13522011-10-25 19:26:31 -0700122{
123 struct datapath *dp = NULL;
124 struct net_device *dev;
125
126 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800127 dev = dev_get_by_index_rcu(net, dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -0700128 if (dev) {
129 struct vport *vport = ovs_internal_dev_get_vport(dev);
130 if (vport)
131 dp = vport->dp;
132 }
133 rcu_read_unlock();
134
135 return dp;
136}
137
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700138/* Must be called with rcu_read_lock or ovs_mutex. */
Jesse Grossccb13522011-10-25 19:26:31 -0700139const char *ovs_dp_name(const struct datapath *dp)
140{
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700141 struct vport *vport = ovs_vport_ovsl_rcu(dp, OVSP_LOCAL);
Jesse Grossccb13522011-10-25 19:26:31 -0700142 return vport->ops->get_name(vport);
143}
144
145static int get_dpifindex(struct datapath *dp)
146{
147 struct vport *local;
148 int ifindex;
149
150 rcu_read_lock();
151
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700152 local = ovs_vport_rcu(dp, OVSP_LOCAL);
Jesse Grossccb13522011-10-25 19:26:31 -0700153 if (local)
154 ifindex = local->ops->get_ifindex(local);
155 else
156 ifindex = 0;
157
158 rcu_read_unlock();
159
160 return ifindex;
161}
162
163static void destroy_dp_rcu(struct rcu_head *rcu)
164{
165 struct datapath *dp = container_of(rcu, struct datapath, rcu);
166
167 ovs_flow_tbl_destroy((__force struct flow_table *)dp->table);
168 free_percpu(dp->stats_percpu);
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800169 release_net(ovs_dp_get_net(dp));
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700170 kfree(dp->ports);
Jesse Grossccb13522011-10-25 19:26:31 -0700171 kfree(dp);
172}
173
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700174static struct hlist_head *vport_hash_bucket(const struct datapath *dp,
175 u16 port_no)
176{
177 return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)];
178}
179
180struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no)
181{
182 struct vport *vport;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700183 struct hlist_head *head;
184
185 head = vport_hash_bucket(dp, port_no);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800186 hlist_for_each_entry_rcu(vport, head, dp_hash_node) {
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700187 if (vport->port_no == port_no)
188 return vport;
189 }
190 return NULL;
191}
192
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700193/* Called with ovs_mutex. */
Jesse Grossccb13522011-10-25 19:26:31 -0700194static struct vport *new_vport(const struct vport_parms *parms)
195{
196 struct vport *vport;
197
198 vport = ovs_vport_add(parms);
199 if (!IS_ERR(vport)) {
200 struct datapath *dp = parms->dp;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700201 struct hlist_head *head = vport_hash_bucket(dp, vport->port_no);
Jesse Grossccb13522011-10-25 19:26:31 -0700202
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700203 hlist_add_head_rcu(&vport->dp_hash_node, head);
Jesse Grossccb13522011-10-25 19:26:31 -0700204 }
Jesse Grossccb13522011-10-25 19:26:31 -0700205 return vport;
206}
207
Jesse Grossccb13522011-10-25 19:26:31 -0700208void ovs_dp_detach_port(struct vport *p)
209{
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700210 ASSERT_OVSL();
Jesse Grossccb13522011-10-25 19:26:31 -0700211
212 /* First drop references to device. */
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700213 hlist_del_rcu(&p->dp_hash_node);
Jesse Grossccb13522011-10-25 19:26:31 -0700214
215 /* Then destroy it. */
216 ovs_vport_del(p);
217}
218
219/* Must be called with rcu_read_lock. */
220void ovs_dp_process_received_packet(struct vport *p, struct sk_buff *skb)
221{
222 struct datapath *dp = p->dp;
223 struct sw_flow *flow;
224 struct dp_stats_percpu *stats;
225 struct sw_flow_key key;
226 u64 *stats_counter;
227 int error;
228 int key_len;
229
Shan Wei404f2f12012-11-13 09:52:25 +0800230 stats = this_cpu_ptr(dp->stats_percpu);
Jesse Grossccb13522011-10-25 19:26:31 -0700231
232 /* Extract flow from 'skb' into 'key'. */
233 error = ovs_flow_extract(skb, p->port_no, &key, &key_len);
234 if (unlikely(error)) {
235 kfree_skb(skb);
236 return;
237 }
238
239 /* Look up flow. */
240 flow = ovs_flow_tbl_lookup(rcu_dereference(dp->table), &key, key_len);
241 if (unlikely(!flow)) {
242 struct dp_upcall_info upcall;
243
244 upcall.cmd = OVS_PACKET_CMD_MISS;
245 upcall.key = &key;
246 upcall.userdata = NULL;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000247 upcall.portid = p->upcall_portid;
Jesse Grossccb13522011-10-25 19:26:31 -0700248 ovs_dp_upcall(dp, skb, &upcall);
249 consume_skb(skb);
250 stats_counter = &stats->n_missed;
251 goto out;
252 }
253
254 OVS_CB(skb)->flow = flow;
255
256 stats_counter = &stats->n_hit;
257 ovs_flow_used(OVS_CB(skb)->flow, skb);
258 ovs_execute_actions(dp, skb);
259
260out:
261 /* Update datapath statistics. */
262 u64_stats_update_begin(&stats->sync);
263 (*stats_counter)++;
264 u64_stats_update_end(&stats->sync);
265}
266
267static struct genl_family dp_packet_genl_family = {
268 .id = GENL_ID_GENERATE,
269 .hdrsize = sizeof(struct ovs_header),
270 .name = OVS_PACKET_FAMILY,
271 .version = OVS_PACKET_VERSION,
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800272 .maxattr = OVS_PACKET_ATTR_MAX,
273 .netnsok = true
Jesse Grossccb13522011-10-25 19:26:31 -0700274};
275
276int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800277 const struct dp_upcall_info *upcall_info)
Jesse Grossccb13522011-10-25 19:26:31 -0700278{
279 struct dp_stats_percpu *stats;
280 int dp_ifindex;
281 int err;
282
Eric W. Biederman15e47302012-09-07 20:12:54 +0000283 if (upcall_info->portid == 0) {
Jesse Grossccb13522011-10-25 19:26:31 -0700284 err = -ENOTCONN;
285 goto err;
286 }
287
288 dp_ifindex = get_dpifindex(dp);
289 if (!dp_ifindex) {
290 err = -ENODEV;
291 goto err;
292 }
293
294 if (!skb_is_gso(skb))
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800295 err = queue_userspace_packet(ovs_dp_get_net(dp), dp_ifindex, skb, upcall_info);
Jesse Grossccb13522011-10-25 19:26:31 -0700296 else
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800297 err = queue_gso_packets(ovs_dp_get_net(dp), dp_ifindex, skb, upcall_info);
Jesse Grossccb13522011-10-25 19:26:31 -0700298 if (err)
299 goto err;
300
301 return 0;
302
303err:
Shan Wei404f2f12012-11-13 09:52:25 +0800304 stats = this_cpu_ptr(dp->stats_percpu);
Jesse Grossccb13522011-10-25 19:26:31 -0700305
306 u64_stats_update_begin(&stats->sync);
307 stats->n_lost++;
308 u64_stats_update_end(&stats->sync);
309
310 return err;
311}
312
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800313static int queue_gso_packets(struct net *net, int dp_ifindex,
314 struct sk_buff *skb,
Jesse Grossccb13522011-10-25 19:26:31 -0700315 const struct dp_upcall_info *upcall_info)
316{
Ben Pfaffa1b5d0d2012-07-20 14:47:54 -0700317 unsigned short gso_type = skb_shinfo(skb)->gso_type;
Jesse Grossccb13522011-10-25 19:26:31 -0700318 struct dp_upcall_info later_info;
319 struct sw_flow_key later_key;
320 struct sk_buff *segs, *nskb;
321 int err;
322
Cong Wang12b00042013-02-05 16:36:38 +0000323 segs = __skb_gso_segment(skb, NETIF_F_SG | NETIF_F_HW_CSUM, false);
Pravin B Shelar92e5dfc2012-07-20 14:46:29 -0700324 if (IS_ERR(segs))
325 return PTR_ERR(segs);
Jesse Grossccb13522011-10-25 19:26:31 -0700326
327 /* Queue all of the segments. */
328 skb = segs;
329 do {
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800330 err = queue_userspace_packet(net, dp_ifindex, skb, upcall_info);
Jesse Grossccb13522011-10-25 19:26:31 -0700331 if (err)
332 break;
333
Ben Pfaffa1b5d0d2012-07-20 14:47:54 -0700334 if (skb == segs && gso_type & SKB_GSO_UDP) {
Jesse Grossccb13522011-10-25 19:26:31 -0700335 /* The initial flow key extracted by ovs_flow_extract()
336 * in this case is for a first fragment, so we need to
337 * properly mark later fragments.
338 */
339 later_key = *upcall_info->key;
340 later_key.ip.frag = OVS_FRAG_TYPE_LATER;
341
342 later_info = *upcall_info;
343 later_info.key = &later_key;
344 upcall_info = &later_info;
345 }
346 } while ((skb = skb->next));
347
348 /* Free all of the segments. */
349 skb = segs;
350 do {
351 nskb = skb->next;
352 if (err)
353 kfree_skb(skb);
354 else
355 consume_skb(skb);
356 } while ((skb = nskb));
357 return err;
358}
359
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100360static size_t key_attr_size(void)
361{
362 return nla_total_size(4) /* OVS_KEY_ATTR_PRIORITY */
363 + nla_total_size(4) /* OVS_KEY_ATTR_IN_PORT */
364 + nla_total_size(4) /* OVS_KEY_ATTR_SKB_MARK */
365 + nla_total_size(12) /* OVS_KEY_ATTR_ETHERNET */
366 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
367 + nla_total_size(4) /* OVS_KEY_ATTR_8021Q */
368 + nla_total_size(0) /* OVS_KEY_ATTR_ENCAP */
369 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
370 + nla_total_size(40) /* OVS_KEY_ATTR_IPV6 */
371 + nla_total_size(2) /* OVS_KEY_ATTR_ICMPV6 */
372 + nla_total_size(28); /* OVS_KEY_ATTR_ND */
373}
374
375static size_t upcall_msg_size(const struct sk_buff *skb,
376 const struct nlattr *userdata)
377{
378 size_t size = NLMSG_ALIGN(sizeof(struct ovs_header))
379 + nla_total_size(skb->len) /* OVS_PACKET_ATTR_PACKET */
380 + nla_total_size(key_attr_size()); /* OVS_PACKET_ATTR_KEY */
381
382 /* OVS_PACKET_ATTR_USERDATA */
383 if (userdata)
384 size += NLA_ALIGN(userdata->nla_len);
385
386 return size;
387}
388
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800389static int queue_userspace_packet(struct net *net, int dp_ifindex,
390 struct sk_buff *skb,
Jesse Grossccb13522011-10-25 19:26:31 -0700391 const struct dp_upcall_info *upcall_info)
392{
393 struct ovs_header *upcall;
394 struct sk_buff *nskb = NULL;
395 struct sk_buff *user_skb; /* to be queued to userspace */
396 struct nlattr *nla;
Jesse Grossccb13522011-10-25 19:26:31 -0700397 int err;
398
399 if (vlan_tx_tag_present(skb)) {
400 nskb = skb_clone(skb, GFP_ATOMIC);
401 if (!nskb)
402 return -ENOMEM;
403
404 nskb = __vlan_put_tag(nskb, vlan_tx_tag_get(nskb));
Dan Carpenter8aa51d62012-05-13 08:44:18 +0000405 if (!nskb)
Jesse Grossccb13522011-10-25 19:26:31 -0700406 return -ENOMEM;
407
408 nskb->vlan_tci = 0;
409 skb = nskb;
410 }
411
412 if (nla_attr_size(skb->len) > USHRT_MAX) {
413 err = -EFBIG;
414 goto out;
415 }
416
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100417 user_skb = genlmsg_new(upcall_msg_size(skb, upcall_info->userdata), GFP_ATOMIC);
Jesse Grossccb13522011-10-25 19:26:31 -0700418 if (!user_skb) {
419 err = -ENOMEM;
420 goto out;
421 }
422
423 upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
424 0, upcall_info->cmd);
425 upcall->dp_ifindex = dp_ifindex;
426
427 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
428 ovs_flow_to_nlattrs(upcall_info->key, user_skb);
429 nla_nest_end(user_skb, nla);
430
431 if (upcall_info->userdata)
Ben Pfaff44901082013-02-15 17:29:22 -0800432 __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA,
433 nla_len(upcall_info->userdata),
434 nla_data(upcall_info->userdata));
Jesse Grossccb13522011-10-25 19:26:31 -0700435
436 nla = __nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, skb->len);
437
438 skb_copy_and_csum_dev(skb, nla_data(nla));
439
Rich Lanea15ff762013-02-15 11:07:43 -0800440 genlmsg_end(user_skb, upcall);
Eric W. Biederman15e47302012-09-07 20:12:54 +0000441 err = genlmsg_unicast(net, user_skb, upcall_info->portid);
Jesse Grossccb13522011-10-25 19:26:31 -0700442
443out:
444 kfree_skb(nskb);
445 return err;
446}
447
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700448/* Called with ovs_mutex. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800449static int flush_flows(struct datapath *dp)
Jesse Grossccb13522011-10-25 19:26:31 -0700450{
451 struct flow_table *old_table;
452 struct flow_table *new_table;
Jesse Grossccb13522011-10-25 19:26:31 -0700453
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700454 old_table = ovsl_dereference(dp->table);
Jesse Grossccb13522011-10-25 19:26:31 -0700455 new_table = ovs_flow_tbl_alloc(TBL_MIN_BUCKETS);
456 if (!new_table)
457 return -ENOMEM;
458
459 rcu_assign_pointer(dp->table, new_table);
460
461 ovs_flow_tbl_deferred_destroy(old_table);
462 return 0;
463}
464
465static int validate_actions(const struct nlattr *attr,
466 const struct sw_flow_key *key, int depth);
467
468static int validate_sample(const struct nlattr *attr,
469 const struct sw_flow_key *key, int depth)
470{
471 const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
472 const struct nlattr *probability, *actions;
473 const struct nlattr *a;
474 int rem;
475
476 memset(attrs, 0, sizeof(attrs));
477 nla_for_each_nested(a, attr, rem) {
478 int type = nla_type(a);
479 if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
480 return -EINVAL;
481 attrs[type] = a;
482 }
483 if (rem)
484 return -EINVAL;
485
486 probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
487 if (!probability || nla_len(probability) != sizeof(u32))
488 return -EINVAL;
489
490 actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
491 if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
492 return -EINVAL;
493 return validate_actions(actions, key, depth + 1);
494}
495
Pravin B Shelar072ae632012-05-07 17:21:53 -0700496static int validate_tp_port(const struct sw_flow_key *flow_key)
497{
498 if (flow_key->eth.type == htons(ETH_P_IP)) {
Jesse Gross41853922012-08-06 15:49:47 -0700499 if (flow_key->ipv4.tp.src || flow_key->ipv4.tp.dst)
Pravin B Shelar072ae632012-05-07 17:21:53 -0700500 return 0;
501 } else if (flow_key->eth.type == htons(ETH_P_IPV6)) {
Jesse Gross41853922012-08-06 15:49:47 -0700502 if (flow_key->ipv6.tp.src || flow_key->ipv6.tp.dst)
Pravin B Shelar072ae632012-05-07 17:21:53 -0700503 return 0;
504 }
505
506 return -EINVAL;
507}
508
Jesse Grossccb13522011-10-25 19:26:31 -0700509static int validate_set(const struct nlattr *a,
510 const struct sw_flow_key *flow_key)
511{
512 const struct nlattr *ovs_key = nla_data(a);
513 int key_type = nla_type(ovs_key);
514
515 /* There can be only one key in a action */
516 if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
517 return -EINVAL;
518
519 if (key_type > OVS_KEY_ATTR_MAX ||
520 nla_len(ovs_key) != ovs_key_lens[key_type])
521 return -EINVAL;
522
523 switch (key_type) {
524 const struct ovs_key_ipv4 *ipv4_key;
Ansis Atteka3fdbd1c2012-11-13 15:44:14 -0800525 const struct ovs_key_ipv6 *ipv6_key;
Jesse Grossccb13522011-10-25 19:26:31 -0700526
527 case OVS_KEY_ATTR_PRIORITY:
Ansis Atteka39c7caeb2012-11-26 11:24:11 -0800528 case OVS_KEY_ATTR_SKB_MARK:
Jesse Grossccb13522011-10-25 19:26:31 -0700529 case OVS_KEY_ATTR_ETHERNET:
530 break;
531
532 case OVS_KEY_ATTR_IPV4:
533 if (flow_key->eth.type != htons(ETH_P_IP))
534 return -EINVAL;
535
Jesse Gross41853922012-08-06 15:49:47 -0700536 if (!flow_key->ip.proto)
Jesse Grossccb13522011-10-25 19:26:31 -0700537 return -EINVAL;
538
539 ipv4_key = nla_data(ovs_key);
540 if (ipv4_key->ipv4_proto != flow_key->ip.proto)
541 return -EINVAL;
542
543 if (ipv4_key->ipv4_frag != flow_key->ip.frag)
544 return -EINVAL;
545
546 break;
547
Ansis Atteka3fdbd1c2012-11-13 15:44:14 -0800548 case OVS_KEY_ATTR_IPV6:
549 if (flow_key->eth.type != htons(ETH_P_IPV6))
550 return -EINVAL;
551
552 if (!flow_key->ip.proto)
553 return -EINVAL;
554
555 ipv6_key = nla_data(ovs_key);
556 if (ipv6_key->ipv6_proto != flow_key->ip.proto)
557 return -EINVAL;
558
559 if (ipv6_key->ipv6_frag != flow_key->ip.frag)
560 return -EINVAL;
561
562 if (ntohl(ipv6_key->ipv6_label) & 0xFFF00000)
563 return -EINVAL;
564
565 break;
566
Jesse Grossccb13522011-10-25 19:26:31 -0700567 case OVS_KEY_ATTR_TCP:
568 if (flow_key->ip.proto != IPPROTO_TCP)
569 return -EINVAL;
570
Pravin B Shelar072ae632012-05-07 17:21:53 -0700571 return validate_tp_port(flow_key);
Jesse Grossccb13522011-10-25 19:26:31 -0700572
573 case OVS_KEY_ATTR_UDP:
574 if (flow_key->ip.proto != IPPROTO_UDP)
575 return -EINVAL;
576
Pravin B Shelar072ae632012-05-07 17:21:53 -0700577 return validate_tp_port(flow_key);
Jesse Grossccb13522011-10-25 19:26:31 -0700578
579 default:
580 return -EINVAL;
581 }
582
583 return 0;
584}
585
586static int validate_userspace(const struct nlattr *attr)
587{
588 static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
589 [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
Ben Pfaff44901082013-02-15 17:29:22 -0800590 [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_UNSPEC },
Jesse Grossccb13522011-10-25 19:26:31 -0700591 };
592 struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
593 int error;
594
595 error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX,
596 attr, userspace_policy);
597 if (error)
598 return error;
599
600 if (!a[OVS_USERSPACE_ATTR_PID] ||
601 !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
602 return -EINVAL;
603
604 return 0;
605}
606
607static int validate_actions(const struct nlattr *attr,
608 const struct sw_flow_key *key, int depth)
609{
610 const struct nlattr *a;
611 int rem, err;
612
613 if (depth >= SAMPLE_ACTION_DEPTH)
614 return -EOVERFLOW;
615
616 nla_for_each_nested(a, attr, rem) {
617 /* Expected argument lengths, (u32)-1 for variable length. */
618 static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
619 [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
620 [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
621 [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
622 [OVS_ACTION_ATTR_POP_VLAN] = 0,
623 [OVS_ACTION_ATTR_SET] = (u32)-1,
624 [OVS_ACTION_ATTR_SAMPLE] = (u32)-1
625 };
626 const struct ovs_action_push_vlan *vlan;
627 int type = nla_type(a);
628
629 if (type > OVS_ACTION_ATTR_MAX ||
630 (action_lens[type] != nla_len(a) &&
631 action_lens[type] != (u32)-1))
632 return -EINVAL;
633
634 switch (type) {
635 case OVS_ACTION_ATTR_UNSPEC:
636 return -EINVAL;
637
638 case OVS_ACTION_ATTR_USERSPACE:
639 err = validate_userspace(a);
640 if (err)
641 return err;
642 break;
643
644 case OVS_ACTION_ATTR_OUTPUT:
645 if (nla_get_u32(a) >= DP_MAX_PORTS)
646 return -EINVAL;
647 break;
648
649
650 case OVS_ACTION_ATTR_POP_VLAN:
651 break;
652
653 case OVS_ACTION_ATTR_PUSH_VLAN:
654 vlan = nla_data(a);
655 if (vlan->vlan_tpid != htons(ETH_P_8021Q))
656 return -EINVAL;
657 if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
658 return -EINVAL;
659 break;
660
661 case OVS_ACTION_ATTR_SET:
662 err = validate_set(a, key);
663 if (err)
664 return err;
665 break;
666
667 case OVS_ACTION_ATTR_SAMPLE:
668 err = validate_sample(a, key, depth);
669 if (err)
670 return err;
671 break;
672
673 default:
674 return -EINVAL;
675 }
676 }
677
678 if (rem > 0)
679 return -EINVAL;
680
681 return 0;
682}
683
684static void clear_stats(struct sw_flow *flow)
685{
686 flow->used = 0;
687 flow->tcp_flags = 0;
688 flow->packet_count = 0;
689 flow->byte_count = 0;
690}
691
692static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
693{
694 struct ovs_header *ovs_header = info->userhdr;
695 struct nlattr **a = info->attrs;
696 struct sw_flow_actions *acts;
697 struct sk_buff *packet;
698 struct sw_flow *flow;
699 struct datapath *dp;
700 struct ethhdr *eth;
701 int len;
702 int err;
703 int key_len;
704
705 err = -EINVAL;
706 if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
Thomas Grafdded45fc2013-03-29 14:46:47 +0100707 !a[OVS_PACKET_ATTR_ACTIONS])
Jesse Grossccb13522011-10-25 19:26:31 -0700708 goto err;
709
710 len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
711 packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
712 err = -ENOMEM;
713 if (!packet)
714 goto err;
715 skb_reserve(packet, NET_IP_ALIGN);
716
Thomas Graf32686a92013-03-29 14:46:48 +0100717 nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
Jesse Grossccb13522011-10-25 19:26:31 -0700718
719 skb_reset_mac_header(packet);
720 eth = eth_hdr(packet);
721
722 /* Normally, setting the skb 'protocol' field would be handled by a
723 * call to eth_type_trans(), but it assumes there's a sending
724 * device, which we may not have. */
Simon Hormane5c5d222013-03-28 13:38:25 +0900725 if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN)
Jesse Grossccb13522011-10-25 19:26:31 -0700726 packet->protocol = eth->h_proto;
727 else
728 packet->protocol = htons(ETH_P_802_2);
729
730 /* Build an sw_flow for sending this packet. */
731 flow = ovs_flow_alloc();
732 err = PTR_ERR(flow);
733 if (IS_ERR(flow))
734 goto err_kfree_skb;
735
736 err = ovs_flow_extract(packet, -1, &flow->key, &key_len);
737 if (err)
738 goto err_flow_free;
739
740 err = ovs_flow_metadata_from_nlattrs(&flow->key.phy.priority,
Ansis Atteka39c7caeb2012-11-26 11:24:11 -0800741 &flow->key.phy.skb_mark,
Jesse Grossccb13522011-10-25 19:26:31 -0700742 &flow->key.phy.in_port,
743 a[OVS_PACKET_ATTR_KEY]);
744 if (err)
745 goto err_flow_free;
746
747 err = validate_actions(a[OVS_PACKET_ATTR_ACTIONS], &flow->key, 0);
748 if (err)
749 goto err_flow_free;
750
751 flow->hash = ovs_flow_hash(&flow->key, key_len);
752
753 acts = ovs_flow_actions_alloc(a[OVS_PACKET_ATTR_ACTIONS]);
754 err = PTR_ERR(acts);
755 if (IS_ERR(acts))
756 goto err_flow_free;
757 rcu_assign_pointer(flow->sf_acts, acts);
758
759 OVS_CB(packet)->flow = flow;
760 packet->priority = flow->key.phy.priority;
Ansis Atteka39c7caeb2012-11-26 11:24:11 -0800761 packet->mark = flow->key.phy.skb_mark;
Jesse Grossccb13522011-10-25 19:26:31 -0700762
763 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800764 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -0700765 err = -ENODEV;
766 if (!dp)
767 goto err_unlock;
768
769 local_bh_disable();
770 err = ovs_execute_actions(dp, packet);
771 local_bh_enable();
772 rcu_read_unlock();
773
774 ovs_flow_free(flow);
775 return err;
776
777err_unlock:
778 rcu_read_unlock();
779err_flow_free:
780 ovs_flow_free(flow);
781err_kfree_skb:
782 kfree_skb(packet);
783err:
784 return err;
785}
786
787static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
Thomas Grafdded45fc2013-03-29 14:46:47 +0100788 [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
Jesse Grossccb13522011-10-25 19:26:31 -0700789 [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
790 [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
791};
792
793static struct genl_ops dp_packet_genl_ops[] = {
794 { .cmd = OVS_PACKET_CMD_EXECUTE,
795 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
796 .policy = packet_policy,
797 .doit = ovs_packet_cmd_execute
798 }
799};
800
801static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats)
802{
803 int i;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700804 struct flow_table *table = ovsl_dereference(dp->table);
Jesse Grossccb13522011-10-25 19:26:31 -0700805
806 stats->n_flows = ovs_flow_tbl_count(table);
807
808 stats->n_hit = stats->n_missed = stats->n_lost = 0;
809 for_each_possible_cpu(i) {
810 const struct dp_stats_percpu *percpu_stats;
811 struct dp_stats_percpu local_stats;
812 unsigned int start;
813
814 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
815
816 do {
817 start = u64_stats_fetch_begin_bh(&percpu_stats->sync);
818 local_stats = *percpu_stats;
819 } while (u64_stats_fetch_retry_bh(&percpu_stats->sync, start));
820
821 stats->n_hit += local_stats.n_hit;
822 stats->n_missed += local_stats.n_missed;
823 stats->n_lost += local_stats.n_lost;
824 }
825}
826
827static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
828 [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
829 [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
830 [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
831};
832
833static struct genl_family dp_flow_genl_family = {
834 .id = GENL_ID_GENERATE,
835 .hdrsize = sizeof(struct ovs_header),
836 .name = OVS_FLOW_FAMILY,
837 .version = OVS_FLOW_VERSION,
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800838 .maxattr = OVS_FLOW_ATTR_MAX,
839 .netnsok = true
Jesse Grossccb13522011-10-25 19:26:31 -0700840};
841
842static struct genl_multicast_group ovs_dp_flow_multicast_group = {
843 .name = OVS_FLOW_MCGROUP
844};
845
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100846static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts)
847{
848 return NLMSG_ALIGN(sizeof(struct ovs_header))
849 + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_KEY */
850 + nla_total_size(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
851 + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
852 + nla_total_size(8) /* OVS_FLOW_ATTR_USED */
853 + nla_total_size(acts->actions_len); /* OVS_FLOW_ATTR_ACTIONS */
854}
855
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700856/* Called with ovs_mutex. */
Jesse Grossccb13522011-10-25 19:26:31 -0700857static int ovs_flow_cmd_fill_info(struct sw_flow *flow, struct datapath *dp,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000858 struct sk_buff *skb, u32 portid,
Jesse Grossccb13522011-10-25 19:26:31 -0700859 u32 seq, u32 flags, u8 cmd)
860{
861 const int skb_orig_len = skb->len;
862 const struct sw_flow_actions *sf_acts;
863 struct ovs_flow_stats stats;
864 struct ovs_header *ovs_header;
865 struct nlattr *nla;
866 unsigned long used;
867 u8 tcp_flags;
868 int err;
869
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700870 sf_acts = ovsl_dereference(flow->sf_acts);
Jesse Grossccb13522011-10-25 19:26:31 -0700871
Eric W. Biederman15e47302012-09-07 20:12:54 +0000872 ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family, flags, cmd);
Jesse Grossccb13522011-10-25 19:26:31 -0700873 if (!ovs_header)
874 return -EMSGSIZE;
875
876 ovs_header->dp_ifindex = get_dpifindex(dp);
877
878 nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
879 if (!nla)
880 goto nla_put_failure;
881 err = ovs_flow_to_nlattrs(&flow->key, skb);
882 if (err)
883 goto error;
884 nla_nest_end(skb, nla);
885
886 spin_lock_bh(&flow->lock);
887 used = flow->used;
888 stats.n_packets = flow->packet_count;
889 stats.n_bytes = flow->byte_count;
890 tcp_flags = flow->tcp_flags;
891 spin_unlock_bh(&flow->lock);
892
David S. Miller028d6a62012-03-29 23:20:48 -0400893 if (used &&
894 nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
895 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -0700896
David S. Miller028d6a62012-03-29 23:20:48 -0400897 if (stats.n_packets &&
898 nla_put(skb, OVS_FLOW_ATTR_STATS,
899 sizeof(struct ovs_flow_stats), &stats))
900 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -0700901
David S. Miller028d6a62012-03-29 23:20:48 -0400902 if (tcp_flags &&
903 nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, tcp_flags))
904 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -0700905
906 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
907 * this is the first flow to be dumped into 'skb'. This is unusual for
908 * Netlink but individual action lists can be longer than
909 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
910 * The userspace caller can always fetch the actions separately if it
911 * really wants them. (Most userspace callers in fact don't care.)
912 *
913 * This can only fail for dump operations because the skb is always
914 * properly sized for single flows.
915 */
916 err = nla_put(skb, OVS_FLOW_ATTR_ACTIONS, sf_acts->actions_len,
917 sf_acts->actions);
918 if (err < 0 && skb_orig_len)
919 goto error;
920
921 return genlmsg_end(skb, ovs_header);
922
923nla_put_failure:
924 err = -EMSGSIZE;
925error:
926 genlmsg_cancel(skb, ovs_header);
927 return err;
928}
929
930static struct sk_buff *ovs_flow_cmd_alloc_info(struct sw_flow *flow)
931{
932 const struct sw_flow_actions *sf_acts;
Jesse Grossccb13522011-10-25 19:26:31 -0700933
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700934 sf_acts = ovsl_dereference(flow->sf_acts);
Jesse Grossccb13522011-10-25 19:26:31 -0700935
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100936 return genlmsg_new(ovs_flow_cmd_msg_size(sf_acts), GFP_KERNEL);
Jesse Grossccb13522011-10-25 19:26:31 -0700937}
938
939static struct sk_buff *ovs_flow_cmd_build_info(struct sw_flow *flow,
940 struct datapath *dp,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000941 u32 portid, u32 seq, u8 cmd)
Jesse Grossccb13522011-10-25 19:26:31 -0700942{
943 struct sk_buff *skb;
944 int retval;
945
946 skb = ovs_flow_cmd_alloc_info(flow);
947 if (!skb)
948 return ERR_PTR(-ENOMEM);
949
Eric W. Biederman15e47302012-09-07 20:12:54 +0000950 retval = ovs_flow_cmd_fill_info(flow, dp, skb, portid, seq, 0, cmd);
Jesse Grossccb13522011-10-25 19:26:31 -0700951 BUG_ON(retval < 0);
952 return skb;
953}
954
955static int ovs_flow_cmd_new_or_set(struct sk_buff *skb, struct genl_info *info)
956{
957 struct nlattr **a = info->attrs;
958 struct ovs_header *ovs_header = info->userhdr;
959 struct sw_flow_key key;
960 struct sw_flow *flow;
961 struct sk_buff *reply;
962 struct datapath *dp;
963 struct flow_table *table;
964 int error;
965 int key_len;
966
967 /* Extract key. */
968 error = -EINVAL;
969 if (!a[OVS_FLOW_ATTR_KEY])
970 goto error;
971 error = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
972 if (error)
973 goto error;
974
975 /* Validate actions. */
976 if (a[OVS_FLOW_ATTR_ACTIONS]) {
977 error = validate_actions(a[OVS_FLOW_ATTR_ACTIONS], &key, 0);
978 if (error)
979 goto error;
980 } else if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW) {
981 error = -EINVAL;
982 goto error;
983 }
984
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700985 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800986 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -0700987 error = -ENODEV;
988 if (!dp)
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700989 goto err_unlock_ovs;
Jesse Grossccb13522011-10-25 19:26:31 -0700990
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700991 table = ovsl_dereference(dp->table);
Jesse Grossccb13522011-10-25 19:26:31 -0700992 flow = ovs_flow_tbl_lookup(table, &key, key_len);
993 if (!flow) {
994 struct sw_flow_actions *acts;
995
996 /* Bail out if we're not allowed to create a new flow. */
997 error = -ENOENT;
998 if (info->genlhdr->cmd == OVS_FLOW_CMD_SET)
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700999 goto err_unlock_ovs;
Jesse Grossccb13522011-10-25 19:26:31 -07001000
1001 /* Expand table, if necessary, to make room. */
1002 if (ovs_flow_tbl_need_to_expand(table)) {
1003 struct flow_table *new_table;
1004
1005 new_table = ovs_flow_tbl_expand(table);
1006 if (!IS_ERR(new_table)) {
1007 rcu_assign_pointer(dp->table, new_table);
1008 ovs_flow_tbl_deferred_destroy(table);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001009 table = ovsl_dereference(dp->table);
Jesse Grossccb13522011-10-25 19:26:31 -07001010 }
1011 }
1012
1013 /* Allocate flow. */
1014 flow = ovs_flow_alloc();
1015 if (IS_ERR(flow)) {
1016 error = PTR_ERR(flow);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001017 goto err_unlock_ovs;
Jesse Grossccb13522011-10-25 19:26:31 -07001018 }
1019 flow->key = key;
1020 clear_stats(flow);
1021
1022 /* Obtain actions. */
1023 acts = ovs_flow_actions_alloc(a[OVS_FLOW_ATTR_ACTIONS]);
1024 error = PTR_ERR(acts);
1025 if (IS_ERR(acts))
1026 goto error_free_flow;
1027 rcu_assign_pointer(flow->sf_acts, acts);
1028
1029 /* Put flow in bucket. */
1030 flow->hash = ovs_flow_hash(&key, key_len);
1031 ovs_flow_tbl_insert(table, flow);
1032
Eric W. Biederman15e47302012-09-07 20:12:54 +00001033 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001034 info->snd_seq,
1035 OVS_FLOW_CMD_NEW);
1036 } else {
1037 /* We found a matching flow. */
1038 struct sw_flow_actions *old_acts;
1039 struct nlattr *acts_attrs;
1040
1041 /* Bail out if we're not allowed to modify an existing flow.
1042 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
1043 * because Generic Netlink treats the latter as a dump
1044 * request. We also accept NLM_F_EXCL in case that bug ever
1045 * gets fixed.
1046 */
1047 error = -EEXIST;
1048 if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW &&
1049 info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL))
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001050 goto err_unlock_ovs;
Jesse Grossccb13522011-10-25 19:26:31 -07001051
1052 /* Update actions. */
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001053 old_acts = ovsl_dereference(flow->sf_acts);
Jesse Grossccb13522011-10-25 19:26:31 -07001054 acts_attrs = a[OVS_FLOW_ATTR_ACTIONS];
1055 if (acts_attrs &&
1056 (old_acts->actions_len != nla_len(acts_attrs) ||
1057 memcmp(old_acts->actions, nla_data(acts_attrs),
1058 old_acts->actions_len))) {
1059 struct sw_flow_actions *new_acts;
1060
1061 new_acts = ovs_flow_actions_alloc(acts_attrs);
1062 error = PTR_ERR(new_acts);
1063 if (IS_ERR(new_acts))
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001064 goto err_unlock_ovs;
Jesse Grossccb13522011-10-25 19:26:31 -07001065
1066 rcu_assign_pointer(flow->sf_acts, new_acts);
1067 ovs_flow_deferred_free_acts(old_acts);
1068 }
1069
Eric W. Biederman15e47302012-09-07 20:12:54 +00001070 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001071 info->snd_seq, OVS_FLOW_CMD_NEW);
1072
1073 /* Clear stats. */
1074 if (a[OVS_FLOW_ATTR_CLEAR]) {
1075 spin_lock_bh(&flow->lock);
1076 clear_stats(flow);
1077 spin_unlock_bh(&flow->lock);
1078 }
1079 }
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001080 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001081
1082 if (!IS_ERR(reply))
Thomas Grafed661182013-03-29 14:46:50 +01001083 ovs_notify(reply, info, &ovs_dp_flow_multicast_group);
Jesse Grossccb13522011-10-25 19:26:31 -07001084 else
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001085 netlink_set_err(sock_net(skb->sk)->genl_sock, 0,
Jesse Grossccb13522011-10-25 19:26:31 -07001086 ovs_dp_flow_multicast_group.id, PTR_ERR(reply));
1087 return 0;
1088
1089error_free_flow:
1090 ovs_flow_free(flow);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001091err_unlock_ovs:
1092 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001093error:
1094 return error;
1095}
1096
1097static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1098{
1099 struct nlattr **a = info->attrs;
1100 struct ovs_header *ovs_header = info->userhdr;
1101 struct sw_flow_key key;
1102 struct sk_buff *reply;
1103 struct sw_flow *flow;
1104 struct datapath *dp;
1105 struct flow_table *table;
1106 int err;
1107 int key_len;
1108
1109 if (!a[OVS_FLOW_ATTR_KEY])
1110 return -EINVAL;
1111 err = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
1112 if (err)
1113 return err;
1114
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001115 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001116 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001117 if (!dp) {
1118 err = -ENODEV;
1119 goto unlock;
1120 }
Jesse Grossccb13522011-10-25 19:26:31 -07001121
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001122 table = ovsl_dereference(dp->table);
Jesse Grossccb13522011-10-25 19:26:31 -07001123 flow = ovs_flow_tbl_lookup(table, &key, key_len);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001124 if (!flow) {
1125 err = -ENOENT;
1126 goto unlock;
1127 }
Jesse Grossccb13522011-10-25 19:26:31 -07001128
Eric W. Biederman15e47302012-09-07 20:12:54 +00001129 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001130 info->snd_seq, OVS_FLOW_CMD_NEW);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001131 if (IS_ERR(reply)) {
1132 err = PTR_ERR(reply);
1133 goto unlock;
1134 }
Jesse Grossccb13522011-10-25 19:26:31 -07001135
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001136 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001137 return genlmsg_reply(reply, info);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001138unlock:
1139 ovs_unlock();
1140 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001141}
1142
1143static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1144{
1145 struct nlattr **a = info->attrs;
1146 struct ovs_header *ovs_header = info->userhdr;
1147 struct sw_flow_key key;
1148 struct sk_buff *reply;
1149 struct sw_flow *flow;
1150 struct datapath *dp;
1151 struct flow_table *table;
1152 int err;
1153 int key_len;
1154
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001155 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001156 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001157 if (!dp) {
1158 err = -ENODEV;
1159 goto unlock;
1160 }
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001161
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001162 if (!a[OVS_FLOW_ATTR_KEY]) {
1163 err = flush_flows(dp);
1164 goto unlock;
1165 }
Jesse Grossccb13522011-10-25 19:26:31 -07001166 err = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
1167 if (err)
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001168 goto unlock;
Jesse Grossccb13522011-10-25 19:26:31 -07001169
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001170 table = ovsl_dereference(dp->table);
Jesse Grossccb13522011-10-25 19:26:31 -07001171 flow = ovs_flow_tbl_lookup(table, &key, key_len);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001172 if (!flow) {
1173 err = -ENOENT;
1174 goto unlock;
1175 }
Jesse Grossccb13522011-10-25 19:26:31 -07001176
1177 reply = ovs_flow_cmd_alloc_info(flow);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001178 if (!reply) {
1179 err = -ENOMEM;
1180 goto unlock;
1181 }
Jesse Grossccb13522011-10-25 19:26:31 -07001182
1183 ovs_flow_tbl_remove(table, flow);
1184
Eric W. Biederman15e47302012-09-07 20:12:54 +00001185 err = ovs_flow_cmd_fill_info(flow, dp, reply, info->snd_portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001186 info->snd_seq, 0, OVS_FLOW_CMD_DEL);
1187 BUG_ON(err < 0);
1188
1189 ovs_flow_deferred_free(flow);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001190 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001191
Thomas Grafed661182013-03-29 14:46:50 +01001192 ovs_notify(reply, info, &ovs_dp_flow_multicast_group);
Jesse Grossccb13522011-10-25 19:26:31 -07001193 return 0;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001194unlock:
1195 ovs_unlock();
1196 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001197}
1198
1199static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1200{
1201 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1202 struct datapath *dp;
1203 struct flow_table *table;
1204
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001205 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001206 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001207 if (!dp) {
1208 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001209 return -ENODEV;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001210 }
Jesse Grossccb13522011-10-25 19:26:31 -07001211
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001212 table = ovsl_dereference(dp->table);
Jesse Grossccb13522011-10-25 19:26:31 -07001213
1214 for (;;) {
1215 struct sw_flow *flow;
1216 u32 bucket, obj;
1217
1218 bucket = cb->args[0];
1219 obj = cb->args[1];
1220 flow = ovs_flow_tbl_next(table, &bucket, &obj);
1221 if (!flow)
1222 break;
1223
1224 if (ovs_flow_cmd_fill_info(flow, dp, skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001225 NETLINK_CB(cb->skb).portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001226 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1227 OVS_FLOW_CMD_NEW) < 0)
1228 break;
1229
1230 cb->args[0] = bucket;
1231 cb->args[1] = obj;
1232 }
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001233 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001234 return skb->len;
1235}
1236
1237static struct genl_ops dp_flow_genl_ops[] = {
1238 { .cmd = OVS_FLOW_CMD_NEW,
1239 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1240 .policy = flow_policy,
1241 .doit = ovs_flow_cmd_new_or_set
1242 },
1243 { .cmd = OVS_FLOW_CMD_DEL,
1244 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1245 .policy = flow_policy,
1246 .doit = ovs_flow_cmd_del
1247 },
1248 { .cmd = OVS_FLOW_CMD_GET,
1249 .flags = 0, /* OK for unprivileged users. */
1250 .policy = flow_policy,
1251 .doit = ovs_flow_cmd_get,
1252 .dumpit = ovs_flow_cmd_dump
1253 },
1254 { .cmd = OVS_FLOW_CMD_SET,
1255 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1256 .policy = flow_policy,
1257 .doit = ovs_flow_cmd_new_or_set,
1258 },
1259};
1260
1261static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1262 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1263 [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1264};
1265
1266static struct genl_family dp_datapath_genl_family = {
1267 .id = GENL_ID_GENERATE,
1268 .hdrsize = sizeof(struct ovs_header),
1269 .name = OVS_DATAPATH_FAMILY,
1270 .version = OVS_DATAPATH_VERSION,
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001271 .maxattr = OVS_DP_ATTR_MAX,
1272 .netnsok = true
Jesse Grossccb13522011-10-25 19:26:31 -07001273};
1274
1275static struct genl_multicast_group ovs_dp_datapath_multicast_group = {
1276 .name = OVS_DATAPATH_MCGROUP
1277};
1278
Thomas Grafc3ff8cf2013-03-29 14:46:49 +01001279static size_t ovs_dp_cmd_msg_size(void)
1280{
1281 size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
1282
1283 msgsize += nla_total_size(IFNAMSIZ);
1284 msgsize += nla_total_size(sizeof(struct ovs_dp_stats));
1285
1286 return msgsize;
1287}
1288
Jesse Grossccb13522011-10-25 19:26:31 -07001289static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001290 u32 portid, u32 seq, u32 flags, u8 cmd)
Jesse Grossccb13522011-10-25 19:26:31 -07001291{
1292 struct ovs_header *ovs_header;
1293 struct ovs_dp_stats dp_stats;
1294 int err;
1295
Eric W. Biederman15e47302012-09-07 20:12:54 +00001296 ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
Jesse Grossccb13522011-10-25 19:26:31 -07001297 flags, cmd);
1298 if (!ovs_header)
1299 goto error;
1300
1301 ovs_header->dp_ifindex = get_dpifindex(dp);
1302
1303 rcu_read_lock();
1304 err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
1305 rcu_read_unlock();
1306 if (err)
1307 goto nla_put_failure;
1308
1309 get_dp_stats(dp, &dp_stats);
David S. Miller028d6a62012-03-29 23:20:48 -04001310 if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats), &dp_stats))
1311 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001312
1313 return genlmsg_end(skb, ovs_header);
1314
1315nla_put_failure:
1316 genlmsg_cancel(skb, ovs_header);
1317error:
1318 return -EMSGSIZE;
1319}
1320
Eric W. Biederman15e47302012-09-07 20:12:54 +00001321static struct sk_buff *ovs_dp_cmd_build_info(struct datapath *dp, u32 portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001322 u32 seq, u8 cmd)
1323{
1324 struct sk_buff *skb;
1325 int retval;
1326
Thomas Grafc3ff8cf2013-03-29 14:46:49 +01001327 skb = genlmsg_new(ovs_dp_cmd_msg_size(), GFP_KERNEL);
Jesse Grossccb13522011-10-25 19:26:31 -07001328 if (!skb)
1329 return ERR_PTR(-ENOMEM);
1330
Eric W. Biederman15e47302012-09-07 20:12:54 +00001331 retval = ovs_dp_cmd_fill_info(dp, skb, portid, seq, 0, cmd);
Jesse Grossccb13522011-10-25 19:26:31 -07001332 if (retval < 0) {
1333 kfree_skb(skb);
1334 return ERR_PTR(retval);
1335 }
1336 return skb;
1337}
1338
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001339/* Called with ovs_mutex. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001340static struct datapath *lookup_datapath(struct net *net,
1341 struct ovs_header *ovs_header,
Jesse Grossccb13522011-10-25 19:26:31 -07001342 struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1343{
1344 struct datapath *dp;
1345
1346 if (!a[OVS_DP_ATTR_NAME])
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001347 dp = get_dp(net, ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001348 else {
1349 struct vport *vport;
1350
1351 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001352 vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
Jesse Grossccb13522011-10-25 19:26:31 -07001353 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
1354 rcu_read_unlock();
1355 }
1356 return dp ? dp : ERR_PTR(-ENODEV);
1357}
1358
1359static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1360{
1361 struct nlattr **a = info->attrs;
1362 struct vport_parms parms;
1363 struct sk_buff *reply;
1364 struct datapath *dp;
1365 struct vport *vport;
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001366 struct ovs_net *ovs_net;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001367 int err, i;
Jesse Grossccb13522011-10-25 19:26:31 -07001368
1369 err = -EINVAL;
1370 if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1371 goto err;
1372
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001373 ovs_lock();
Jesse Grossccb13522011-10-25 19:26:31 -07001374
1375 err = -ENOMEM;
1376 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1377 if (dp == NULL)
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001378 goto err_unlock_ovs;
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001379
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001380 ovs_dp_set_net(dp, hold_net(sock_net(skb->sk)));
Jesse Grossccb13522011-10-25 19:26:31 -07001381
1382 /* Allocate table. */
1383 err = -ENOMEM;
1384 rcu_assign_pointer(dp->table, ovs_flow_tbl_alloc(TBL_MIN_BUCKETS));
1385 if (!dp->table)
1386 goto err_free_dp;
1387
1388 dp->stats_percpu = alloc_percpu(struct dp_stats_percpu);
1389 if (!dp->stats_percpu) {
1390 err = -ENOMEM;
1391 goto err_destroy_table;
1392 }
1393
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001394 dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
1395 GFP_KERNEL);
1396 if (!dp->ports) {
1397 err = -ENOMEM;
1398 goto err_destroy_percpu;
1399 }
1400
1401 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1402 INIT_HLIST_HEAD(&dp->ports[i]);
1403
Jesse Grossccb13522011-10-25 19:26:31 -07001404 /* Set up our datapath device. */
1405 parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1406 parms.type = OVS_VPORT_TYPE_INTERNAL;
1407 parms.options = NULL;
1408 parms.dp = dp;
1409 parms.port_no = OVSP_LOCAL;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001410 parms.upcall_portid = nla_get_u32(a[OVS_DP_ATTR_UPCALL_PID]);
Jesse Grossccb13522011-10-25 19:26:31 -07001411
1412 vport = new_vport(&parms);
1413 if (IS_ERR(vport)) {
1414 err = PTR_ERR(vport);
1415 if (err == -EBUSY)
1416 err = -EEXIST;
1417
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001418 goto err_destroy_ports_array;
Jesse Grossccb13522011-10-25 19:26:31 -07001419 }
1420
Eric W. Biederman15e47302012-09-07 20:12:54 +00001421 reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001422 info->snd_seq, OVS_DP_CMD_NEW);
1423 err = PTR_ERR(reply);
1424 if (IS_ERR(reply))
1425 goto err_destroy_local_port;
1426
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001427 ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
1428 list_add_tail(&dp->list_node, &ovs_net->dps);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001429
1430 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001431
Thomas Grafed661182013-03-29 14:46:50 +01001432 ovs_notify(reply, info, &ovs_dp_datapath_multicast_group);
Jesse Grossccb13522011-10-25 19:26:31 -07001433 return 0;
1434
1435err_destroy_local_port:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001436 ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001437err_destroy_ports_array:
1438 kfree(dp->ports);
Jesse Grossccb13522011-10-25 19:26:31 -07001439err_destroy_percpu:
1440 free_percpu(dp->stats_percpu);
1441err_destroy_table:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001442 ovs_flow_tbl_destroy(ovsl_dereference(dp->table));
Jesse Grossccb13522011-10-25 19:26:31 -07001443err_free_dp:
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001444 release_net(ovs_dp_get_net(dp));
Jesse Grossccb13522011-10-25 19:26:31 -07001445 kfree(dp);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001446err_unlock_ovs:
1447 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001448err:
1449 return err;
1450}
1451
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001452/* Called with ovs_mutex. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001453static void __dp_destroy(struct datapath *dp)
Jesse Grossccb13522011-10-25 19:26:31 -07001454{
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001455 int i;
Jesse Grossccb13522011-10-25 19:26:31 -07001456
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001457 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1458 struct vport *vport;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001459 struct hlist_node *n;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001460
Sasha Levinb67bfe02013-02-27 17:06:00 -08001461 hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001462 if (vport->port_no != OVSP_LOCAL)
1463 ovs_dp_detach_port(vport);
1464 }
Jesse Grossccb13522011-10-25 19:26:31 -07001465
1466 list_del(&dp->list_node);
Jesse Grossccb13522011-10-25 19:26:31 -07001467
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001468 /* OVSP_LOCAL is datapath internal port. We need to make sure that
1469 * all port in datapath are destroyed first before freeing datapath.
Jesse Grossccb13522011-10-25 19:26:31 -07001470 */
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001471 ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
Jesse Grossccb13522011-10-25 19:26:31 -07001472
1473 call_rcu(&dp->rcu, destroy_dp_rcu);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001474}
1475
1476static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1477{
1478 struct sk_buff *reply;
1479 struct datapath *dp;
1480 int err;
1481
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001482 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001483 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1484 err = PTR_ERR(dp);
1485 if (IS_ERR(dp))
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001486 goto unlock;
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001487
Eric W. Biederman15e47302012-09-07 20:12:54 +00001488 reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001489 info->snd_seq, OVS_DP_CMD_DEL);
1490 err = PTR_ERR(reply);
1491 if (IS_ERR(reply))
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001492 goto unlock;
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001493
1494 __dp_destroy(dp);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001495 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001496
Thomas Grafed661182013-03-29 14:46:50 +01001497 ovs_notify(reply, info, &ovs_dp_datapath_multicast_group);
Jesse Grossccb13522011-10-25 19:26:31 -07001498
1499 return 0;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001500unlock:
1501 ovs_unlock();
1502 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001503}
1504
1505static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1506{
1507 struct sk_buff *reply;
1508 struct datapath *dp;
1509 int err;
1510
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001511 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001512 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001513 err = PTR_ERR(dp);
Jesse Grossccb13522011-10-25 19:26:31 -07001514 if (IS_ERR(dp))
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001515 goto unlock;
Jesse Grossccb13522011-10-25 19:26:31 -07001516
Eric W. Biederman15e47302012-09-07 20:12:54 +00001517 reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001518 info->snd_seq, OVS_DP_CMD_NEW);
1519 if (IS_ERR(reply)) {
1520 err = PTR_ERR(reply);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001521 netlink_set_err(sock_net(skb->sk)->genl_sock, 0,
Jesse Grossccb13522011-10-25 19:26:31 -07001522 ovs_dp_datapath_multicast_group.id, err);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001523 err = 0;
1524 goto unlock;
Jesse Grossccb13522011-10-25 19:26:31 -07001525 }
1526
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001527 ovs_unlock();
Thomas Grafed661182013-03-29 14:46:50 +01001528 ovs_notify(reply, info, &ovs_dp_datapath_multicast_group);
Jesse Grossccb13522011-10-25 19:26:31 -07001529
1530 return 0;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001531unlock:
1532 ovs_unlock();
1533 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001534}
1535
1536static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1537{
1538 struct sk_buff *reply;
1539 struct datapath *dp;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001540 int err;
Jesse Grossccb13522011-10-25 19:26:31 -07001541
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001542 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001543 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001544 if (IS_ERR(dp)) {
1545 err = PTR_ERR(dp);
1546 goto unlock;
1547 }
Jesse Grossccb13522011-10-25 19:26:31 -07001548
Eric W. Biederman15e47302012-09-07 20:12:54 +00001549 reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001550 info->snd_seq, OVS_DP_CMD_NEW);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001551 if (IS_ERR(reply)) {
1552 err = PTR_ERR(reply);
1553 goto unlock;
1554 }
Jesse Grossccb13522011-10-25 19:26:31 -07001555
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001556 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001557 return genlmsg_reply(reply, info);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001558
1559unlock:
1560 ovs_unlock();
1561 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001562}
1563
1564static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1565{
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001566 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
Jesse Grossccb13522011-10-25 19:26:31 -07001567 struct datapath *dp;
1568 int skip = cb->args[0];
1569 int i = 0;
1570
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001571 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001572 list_for_each_entry(dp, &ovs_net->dps, list_node) {
Ben Pfaff77676fd2012-01-17 13:33:39 +00001573 if (i >= skip &&
Eric W. Biederman15e47302012-09-07 20:12:54 +00001574 ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001575 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1576 OVS_DP_CMD_NEW) < 0)
1577 break;
1578 i++;
1579 }
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001580 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001581
1582 cb->args[0] = i;
1583
1584 return skb->len;
1585}
1586
1587static struct genl_ops dp_datapath_genl_ops[] = {
1588 { .cmd = OVS_DP_CMD_NEW,
1589 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1590 .policy = datapath_policy,
1591 .doit = ovs_dp_cmd_new
1592 },
1593 { .cmd = OVS_DP_CMD_DEL,
1594 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1595 .policy = datapath_policy,
1596 .doit = ovs_dp_cmd_del
1597 },
1598 { .cmd = OVS_DP_CMD_GET,
1599 .flags = 0, /* OK for unprivileged users. */
1600 .policy = datapath_policy,
1601 .doit = ovs_dp_cmd_get,
1602 .dumpit = ovs_dp_cmd_dump
1603 },
1604 { .cmd = OVS_DP_CMD_SET,
1605 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1606 .policy = datapath_policy,
1607 .doit = ovs_dp_cmd_set,
1608 },
1609};
1610
1611static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
1612 [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1613 [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
1614 [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
1615 [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
1616 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1617 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
1618};
1619
1620static struct genl_family dp_vport_genl_family = {
1621 .id = GENL_ID_GENERATE,
1622 .hdrsize = sizeof(struct ovs_header),
1623 .name = OVS_VPORT_FAMILY,
1624 .version = OVS_VPORT_VERSION,
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001625 .maxattr = OVS_VPORT_ATTR_MAX,
1626 .netnsok = true
Jesse Grossccb13522011-10-25 19:26:31 -07001627};
1628
1629struct genl_multicast_group ovs_dp_vport_multicast_group = {
1630 .name = OVS_VPORT_MCGROUP
1631};
1632
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001633/* Called with ovs_mutex or RCU read lock. */
Jesse Grossccb13522011-10-25 19:26:31 -07001634static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001635 u32 portid, u32 seq, u32 flags, u8 cmd)
Jesse Grossccb13522011-10-25 19:26:31 -07001636{
1637 struct ovs_header *ovs_header;
1638 struct ovs_vport_stats vport_stats;
1639 int err;
1640
Eric W. Biederman15e47302012-09-07 20:12:54 +00001641 ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
Jesse Grossccb13522011-10-25 19:26:31 -07001642 flags, cmd);
1643 if (!ovs_header)
1644 return -EMSGSIZE;
1645
1646 ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1647
David S. Miller028d6a62012-03-29 23:20:48 -04001648 if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
1649 nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
1650 nla_put_string(skb, OVS_VPORT_ATTR_NAME, vport->ops->get_name(vport)) ||
Eric W. Biederman15e47302012-09-07 20:12:54 +00001651 nla_put_u32(skb, OVS_VPORT_ATTR_UPCALL_PID, vport->upcall_portid))
David S. Miller028d6a62012-03-29 23:20:48 -04001652 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001653
1654 ovs_vport_get_stats(vport, &vport_stats);
David S. Miller028d6a62012-03-29 23:20:48 -04001655 if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
1656 &vport_stats))
1657 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001658
1659 err = ovs_vport_get_options(vport, skb);
1660 if (err == -EMSGSIZE)
1661 goto error;
1662
1663 return genlmsg_end(skb, ovs_header);
1664
1665nla_put_failure:
1666 err = -EMSGSIZE;
1667error:
1668 genlmsg_cancel(skb, ovs_header);
1669 return err;
1670}
1671
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001672/* Called with ovs_mutex or RCU read lock. */
Eric W. Biederman15e47302012-09-07 20:12:54 +00001673struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001674 u32 seq, u8 cmd)
1675{
1676 struct sk_buff *skb;
1677 int retval;
1678
1679 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1680 if (!skb)
1681 return ERR_PTR(-ENOMEM);
1682
Eric W. Biederman15e47302012-09-07 20:12:54 +00001683 retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
Jesse Grossccb13522011-10-25 19:26:31 -07001684 if (retval < 0) {
1685 kfree_skb(skb);
1686 return ERR_PTR(retval);
1687 }
1688 return skb;
1689}
1690
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001691/* Called with ovs_mutex or RCU read lock. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001692static struct vport *lookup_vport(struct net *net,
1693 struct ovs_header *ovs_header,
Jesse Grossccb13522011-10-25 19:26:31 -07001694 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1695{
1696 struct datapath *dp;
1697 struct vport *vport;
1698
1699 if (a[OVS_VPORT_ATTR_NAME]) {
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001700 vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
Jesse Grossccb13522011-10-25 19:26:31 -07001701 if (!vport)
1702 return ERR_PTR(-ENODEV);
Ben Pfaff651a68e2012-03-06 15:04:04 -08001703 if (ovs_header->dp_ifindex &&
1704 ovs_header->dp_ifindex != get_dpifindex(vport->dp))
1705 return ERR_PTR(-ENODEV);
Jesse Grossccb13522011-10-25 19:26:31 -07001706 return vport;
1707 } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
1708 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1709
1710 if (port_no >= DP_MAX_PORTS)
1711 return ERR_PTR(-EFBIG);
1712
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001713 dp = get_dp(net, ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001714 if (!dp)
1715 return ERR_PTR(-ENODEV);
1716
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001717 vport = ovs_vport_ovsl_rcu(dp, port_no);
Jesse Grossccb13522011-10-25 19:26:31 -07001718 if (!vport)
Jarno Rajahalme14408db2013-01-09 14:27:35 -08001719 return ERR_PTR(-ENODEV);
Jesse Grossccb13522011-10-25 19:26:31 -07001720 return vport;
1721 } else
1722 return ERR_PTR(-EINVAL);
1723}
1724
1725static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
1726{
1727 struct nlattr **a = info->attrs;
1728 struct ovs_header *ovs_header = info->userhdr;
1729 struct vport_parms parms;
1730 struct sk_buff *reply;
1731 struct vport *vport;
1732 struct datapath *dp;
1733 u32 port_no;
1734 int err;
1735
1736 err = -EINVAL;
1737 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
1738 !a[OVS_VPORT_ATTR_UPCALL_PID])
1739 goto exit;
1740
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001741 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001742 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001743 err = -ENODEV;
1744 if (!dp)
1745 goto exit_unlock;
1746
1747 if (a[OVS_VPORT_ATTR_PORT_NO]) {
1748 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1749
1750 err = -EFBIG;
1751 if (port_no >= DP_MAX_PORTS)
1752 goto exit_unlock;
1753
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001754 vport = ovs_vport_ovsl(dp, port_no);
Jesse Grossccb13522011-10-25 19:26:31 -07001755 err = -EBUSY;
1756 if (vport)
1757 goto exit_unlock;
1758 } else {
1759 for (port_no = 1; ; port_no++) {
1760 if (port_no >= DP_MAX_PORTS) {
1761 err = -EFBIG;
1762 goto exit_unlock;
1763 }
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001764 vport = ovs_vport_ovsl(dp, port_no);
Jesse Grossccb13522011-10-25 19:26:31 -07001765 if (!vport)
1766 break;
1767 }
1768 }
1769
1770 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
1771 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1772 parms.options = a[OVS_VPORT_ATTR_OPTIONS];
1773 parms.dp = dp;
1774 parms.port_no = port_no;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001775 parms.upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
Jesse Grossccb13522011-10-25 19:26:31 -07001776
1777 vport = new_vport(&parms);
1778 err = PTR_ERR(vport);
1779 if (IS_ERR(vport))
1780 goto exit_unlock;
1781
Rich Lanecb7c5bd2013-02-08 13:18:01 -08001782 err = 0;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001783 reply = ovs_vport_cmd_build_info(vport, info->snd_portid, info->snd_seq,
Jesse Grossccb13522011-10-25 19:26:31 -07001784 OVS_VPORT_CMD_NEW);
1785 if (IS_ERR(reply)) {
1786 err = PTR_ERR(reply);
1787 ovs_dp_detach_port(vport);
1788 goto exit_unlock;
1789 }
Thomas Grafed661182013-03-29 14:46:50 +01001790
1791 ovs_notify(reply, info, &ovs_dp_vport_multicast_group);
Jesse Grossccb13522011-10-25 19:26:31 -07001792
1793exit_unlock:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001794 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001795exit:
1796 return err;
1797}
1798
1799static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
1800{
1801 struct nlattr **a = info->attrs;
1802 struct sk_buff *reply;
1803 struct vport *vport;
1804 int err;
1805
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001806 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001807 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
Jesse Grossccb13522011-10-25 19:26:31 -07001808 err = PTR_ERR(vport);
1809 if (IS_ERR(vport))
1810 goto exit_unlock;
1811
1812 err = 0;
1813 if (a[OVS_VPORT_ATTR_TYPE] &&
1814 nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type)
1815 err = -EINVAL;
1816
1817 if (!err && a[OVS_VPORT_ATTR_OPTIONS])
1818 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
Ansis Atteka03fbf8b2012-04-09 12:12:12 -07001819 if (err)
1820 goto exit_unlock;
1821 if (a[OVS_VPORT_ATTR_UPCALL_PID])
Eric W. Biederman15e47302012-09-07 20:12:54 +00001822 vport->upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
Jesse Grossccb13522011-10-25 19:26:31 -07001823
Eric W. Biederman15e47302012-09-07 20:12:54 +00001824 reply = ovs_vport_cmd_build_info(vport, info->snd_portid, info->snd_seq,
Jesse Grossccb13522011-10-25 19:26:31 -07001825 OVS_VPORT_CMD_NEW);
1826 if (IS_ERR(reply)) {
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001827 netlink_set_err(sock_net(skb->sk)->genl_sock, 0,
Ansis Atteka4cb6e112012-05-03 18:40:38 -07001828 ovs_dp_vport_multicast_group.id, PTR_ERR(reply));
1829 goto exit_unlock;
Jesse Grossccb13522011-10-25 19:26:31 -07001830 }
1831
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001832 ovs_unlock();
Thomas Grafed661182013-03-29 14:46:50 +01001833 ovs_notify(reply, info, &ovs_dp_vport_multicast_group);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001834 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001835
1836exit_unlock:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001837 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001838 return err;
1839}
1840
1841static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
1842{
1843 struct nlattr **a = info->attrs;
1844 struct sk_buff *reply;
1845 struct vport *vport;
1846 int err;
1847
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001848 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001849 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
Jesse Grossccb13522011-10-25 19:26:31 -07001850 err = PTR_ERR(vport);
1851 if (IS_ERR(vport))
1852 goto exit_unlock;
1853
1854 if (vport->port_no == OVSP_LOCAL) {
1855 err = -EINVAL;
1856 goto exit_unlock;
1857 }
1858
Eric W. Biederman15e47302012-09-07 20:12:54 +00001859 reply = ovs_vport_cmd_build_info(vport, info->snd_portid, info->snd_seq,
Jesse Grossccb13522011-10-25 19:26:31 -07001860 OVS_VPORT_CMD_DEL);
1861 err = PTR_ERR(reply);
1862 if (IS_ERR(reply))
1863 goto exit_unlock;
1864
Rich Lane734907e2013-02-08 09:30:23 -08001865 err = 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001866 ovs_dp_detach_port(vport);
1867
Thomas Grafed661182013-03-29 14:46:50 +01001868 ovs_notify(reply, info, &ovs_dp_vport_multicast_group);
Jesse Grossccb13522011-10-25 19:26:31 -07001869
1870exit_unlock:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001871 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001872 return err;
1873}
1874
1875static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
1876{
1877 struct nlattr **a = info->attrs;
1878 struct ovs_header *ovs_header = info->userhdr;
1879 struct sk_buff *reply;
1880 struct vport *vport;
1881 int err;
1882
1883 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001884 vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
Jesse Grossccb13522011-10-25 19:26:31 -07001885 err = PTR_ERR(vport);
1886 if (IS_ERR(vport))
1887 goto exit_unlock;
1888
Eric W. Biederman15e47302012-09-07 20:12:54 +00001889 reply = ovs_vport_cmd_build_info(vport, info->snd_portid, info->snd_seq,
Jesse Grossccb13522011-10-25 19:26:31 -07001890 OVS_VPORT_CMD_NEW);
1891 err = PTR_ERR(reply);
1892 if (IS_ERR(reply))
1893 goto exit_unlock;
1894
1895 rcu_read_unlock();
1896
1897 return genlmsg_reply(reply, info);
1898
1899exit_unlock:
1900 rcu_read_unlock();
1901 return err;
1902}
1903
1904static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1905{
1906 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1907 struct datapath *dp;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001908 int bucket = cb->args[0], skip = cb->args[1];
1909 int i, j = 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001910
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001911 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001912 if (!dp)
1913 return -ENODEV;
1914
1915 rcu_read_lock();
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001916 for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
Jesse Grossccb13522011-10-25 19:26:31 -07001917 struct vport *vport;
1918
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001919 j = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001920 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001921 if (j >= skip &&
1922 ovs_vport_cmd_fill_info(vport, skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001923 NETLINK_CB(cb->skb).portid,
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001924 cb->nlh->nlmsg_seq,
1925 NLM_F_MULTI,
1926 OVS_VPORT_CMD_NEW) < 0)
1927 goto out;
Jesse Grossccb13522011-10-25 19:26:31 -07001928
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001929 j++;
1930 }
1931 skip = 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001932 }
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001933out:
Jesse Grossccb13522011-10-25 19:26:31 -07001934 rcu_read_unlock();
1935
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001936 cb->args[0] = i;
1937 cb->args[1] = j;
Jesse Grossccb13522011-10-25 19:26:31 -07001938
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001939 return skb->len;
Jesse Grossccb13522011-10-25 19:26:31 -07001940}
1941
Jesse Grossccb13522011-10-25 19:26:31 -07001942static struct genl_ops dp_vport_genl_ops[] = {
1943 { .cmd = OVS_VPORT_CMD_NEW,
1944 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1945 .policy = vport_policy,
1946 .doit = ovs_vport_cmd_new
1947 },
1948 { .cmd = OVS_VPORT_CMD_DEL,
1949 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1950 .policy = vport_policy,
1951 .doit = ovs_vport_cmd_del
1952 },
1953 { .cmd = OVS_VPORT_CMD_GET,
1954 .flags = 0, /* OK for unprivileged users. */
1955 .policy = vport_policy,
1956 .doit = ovs_vport_cmd_get,
1957 .dumpit = ovs_vport_cmd_dump
1958 },
1959 { .cmd = OVS_VPORT_CMD_SET,
1960 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1961 .policy = vport_policy,
1962 .doit = ovs_vport_cmd_set,
1963 },
1964};
1965
1966struct genl_family_and_ops {
1967 struct genl_family *family;
1968 struct genl_ops *ops;
1969 int n_ops;
1970 struct genl_multicast_group *group;
1971};
1972
1973static const struct genl_family_and_ops dp_genl_families[] = {
1974 { &dp_datapath_genl_family,
1975 dp_datapath_genl_ops, ARRAY_SIZE(dp_datapath_genl_ops),
1976 &ovs_dp_datapath_multicast_group },
1977 { &dp_vport_genl_family,
1978 dp_vport_genl_ops, ARRAY_SIZE(dp_vport_genl_ops),
1979 &ovs_dp_vport_multicast_group },
1980 { &dp_flow_genl_family,
1981 dp_flow_genl_ops, ARRAY_SIZE(dp_flow_genl_ops),
1982 &ovs_dp_flow_multicast_group },
1983 { &dp_packet_genl_family,
1984 dp_packet_genl_ops, ARRAY_SIZE(dp_packet_genl_ops),
1985 NULL },
1986};
1987
1988static void dp_unregister_genl(int n_families)
1989{
1990 int i;
1991
1992 for (i = 0; i < n_families; i++)
1993 genl_unregister_family(dp_genl_families[i].family);
1994}
1995
1996static int dp_register_genl(void)
1997{
1998 int n_registered;
1999 int err;
2000 int i;
2001
2002 n_registered = 0;
2003 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
2004 const struct genl_family_and_ops *f = &dp_genl_families[i];
2005
2006 err = genl_register_family_with_ops(f->family, f->ops,
2007 f->n_ops);
2008 if (err)
2009 goto error;
2010 n_registered++;
2011
2012 if (f->group) {
2013 err = genl_register_mc_group(f->family, f->group);
2014 if (err)
2015 goto error;
2016 }
2017 }
2018
2019 return 0;
2020
2021error:
2022 dp_unregister_genl(n_registered);
2023 return err;
2024}
2025
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002026static void rehash_flow_table(struct work_struct *work)
2027{
2028 struct datapath *dp;
2029 struct net *net;
2030
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002031 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002032 rtnl_lock();
2033 for_each_net(net) {
2034 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2035
2036 list_for_each_entry(dp, &ovs_net->dps, list_node) {
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002037 struct flow_table *old_table = ovsl_dereference(dp->table);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002038 struct flow_table *new_table;
2039
2040 new_table = ovs_flow_tbl_rehash(old_table);
2041 if (!IS_ERR(new_table)) {
2042 rcu_assign_pointer(dp->table, new_table);
2043 ovs_flow_tbl_deferred_destroy(old_table);
2044 }
2045 }
2046 }
2047 rtnl_unlock();
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002048 ovs_unlock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002049 schedule_delayed_work(&rehash_flow_wq, REHASH_FLOW_INTERVAL);
2050}
2051
2052static int __net_init ovs_init_net(struct net *net)
2053{
2054 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2055
2056 INIT_LIST_HEAD(&ovs_net->dps);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002057 INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002058 return 0;
2059}
2060
2061static void __net_exit ovs_exit_net(struct net *net)
2062{
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002063 struct datapath *dp, *dp_next;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002064 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002065
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002066 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002067 list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
2068 __dp_destroy(dp);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002069 ovs_unlock();
2070
2071 cancel_work_sync(&ovs_net->dp_notify_work);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002072}
2073
2074static struct pernet_operations ovs_net_ops = {
2075 .init = ovs_init_net,
2076 .exit = ovs_exit_net,
2077 .id = &ovs_net_id,
2078 .size = sizeof(struct ovs_net),
2079};
2080
Jesse Grossccb13522011-10-25 19:26:31 -07002081static int __init dp_init(void)
2082{
Jesse Grossccb13522011-10-25 19:26:31 -07002083 int err;
2084
YOSHIFUJI Hideaki / 吉藤英明3523b292013-01-09 07:19:55 +00002085 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
Jesse Grossccb13522011-10-25 19:26:31 -07002086
2087 pr_info("Open vSwitch switching datapath\n");
2088
2089 err = ovs_flow_init();
2090 if (err)
2091 goto error;
2092
2093 err = ovs_vport_init();
2094 if (err)
2095 goto error_flow_exit;
2096
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002097 err = register_pernet_device(&ovs_net_ops);
Jesse Grossccb13522011-10-25 19:26:31 -07002098 if (err)
2099 goto error_vport_exit;
2100
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002101 err = register_netdevice_notifier(&ovs_dp_device_notifier);
2102 if (err)
2103 goto error_netns_exit;
2104
Jesse Grossccb13522011-10-25 19:26:31 -07002105 err = dp_register_genl();
2106 if (err < 0)
2107 goto error_unreg_notifier;
2108
2109 schedule_delayed_work(&rehash_flow_wq, REHASH_FLOW_INTERVAL);
2110
2111 return 0;
2112
2113error_unreg_notifier:
2114 unregister_netdevice_notifier(&ovs_dp_device_notifier);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002115error_netns_exit:
2116 unregister_pernet_device(&ovs_net_ops);
Jesse Grossccb13522011-10-25 19:26:31 -07002117error_vport_exit:
2118 ovs_vport_exit();
2119error_flow_exit:
2120 ovs_flow_exit();
2121error:
2122 return err;
2123}
2124
2125static void dp_cleanup(void)
2126{
2127 cancel_delayed_work_sync(&rehash_flow_wq);
Jesse Grossccb13522011-10-25 19:26:31 -07002128 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
2129 unregister_netdevice_notifier(&ovs_dp_device_notifier);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002130 unregister_pernet_device(&ovs_net_ops);
2131 rcu_barrier();
Jesse Grossccb13522011-10-25 19:26:31 -07002132 ovs_vport_exit();
2133 ovs_flow_exit();
2134}
2135
2136module_init(dp_init);
2137module_exit(dp_cleanup);
2138
2139MODULE_DESCRIPTION("Open vSwitch switching datapath");
2140MODULE_LICENSE("GPL");