blob: 74a5fe6feae3418453a490d3214e5887394abe6f [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,
Pravin B Shelar3a4e0d62013-04-23 07:48:48 +0000273 .netnsok = true,
274 .parallel_ops = true,
Jesse Grossccb13522011-10-25 19:26:31 -0700275};
276
277int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800278 const struct dp_upcall_info *upcall_info)
Jesse Grossccb13522011-10-25 19:26:31 -0700279{
280 struct dp_stats_percpu *stats;
281 int dp_ifindex;
282 int err;
283
Eric W. Biederman15e47302012-09-07 20:12:54 +0000284 if (upcall_info->portid == 0) {
Jesse Grossccb13522011-10-25 19:26:31 -0700285 err = -ENOTCONN;
286 goto err;
287 }
288
289 dp_ifindex = get_dpifindex(dp);
290 if (!dp_ifindex) {
291 err = -ENODEV;
292 goto err;
293 }
294
295 if (!skb_is_gso(skb))
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800296 err = queue_userspace_packet(ovs_dp_get_net(dp), dp_ifindex, skb, upcall_info);
Jesse Grossccb13522011-10-25 19:26:31 -0700297 else
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800298 err = queue_gso_packets(ovs_dp_get_net(dp), dp_ifindex, skb, upcall_info);
Jesse Grossccb13522011-10-25 19:26:31 -0700299 if (err)
300 goto err;
301
302 return 0;
303
304err:
Shan Wei404f2f12012-11-13 09:52:25 +0800305 stats = this_cpu_ptr(dp->stats_percpu);
Jesse Grossccb13522011-10-25 19:26:31 -0700306
307 u64_stats_update_begin(&stats->sync);
308 stats->n_lost++;
309 u64_stats_update_end(&stats->sync);
310
311 return err;
312}
313
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800314static int queue_gso_packets(struct net *net, int dp_ifindex,
315 struct sk_buff *skb,
Jesse Grossccb13522011-10-25 19:26:31 -0700316 const struct dp_upcall_info *upcall_info)
317{
Ben Pfaffa1b5d0d2012-07-20 14:47:54 -0700318 unsigned short gso_type = skb_shinfo(skb)->gso_type;
Jesse Grossccb13522011-10-25 19:26:31 -0700319 struct dp_upcall_info later_info;
320 struct sw_flow_key later_key;
321 struct sk_buff *segs, *nskb;
322 int err;
323
Cong Wang12b00042013-02-05 16:36:38 +0000324 segs = __skb_gso_segment(skb, NETIF_F_SG | NETIF_F_HW_CSUM, false);
Pravin B Shelar92e5dfc2012-07-20 14:46:29 -0700325 if (IS_ERR(segs))
326 return PTR_ERR(segs);
Jesse Grossccb13522011-10-25 19:26:31 -0700327
328 /* Queue all of the segments. */
329 skb = segs;
330 do {
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800331 err = queue_userspace_packet(net, dp_ifindex, skb, upcall_info);
Jesse Grossccb13522011-10-25 19:26:31 -0700332 if (err)
333 break;
334
Ben Pfaffa1b5d0d2012-07-20 14:47:54 -0700335 if (skb == segs && gso_type & SKB_GSO_UDP) {
Jesse Grossccb13522011-10-25 19:26:31 -0700336 /* The initial flow key extracted by ovs_flow_extract()
337 * in this case is for a first fragment, so we need to
338 * properly mark later fragments.
339 */
340 later_key = *upcall_info->key;
341 later_key.ip.frag = OVS_FRAG_TYPE_LATER;
342
343 later_info = *upcall_info;
344 later_info.key = &later_key;
345 upcall_info = &later_info;
346 }
347 } while ((skb = skb->next));
348
349 /* Free all of the segments. */
350 skb = segs;
351 do {
352 nskb = skb->next;
353 if (err)
354 kfree_skb(skb);
355 else
356 consume_skb(skb);
357 } while ((skb = nskb));
358 return err;
359}
360
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100361static size_t key_attr_size(void)
362{
363 return nla_total_size(4) /* OVS_KEY_ATTR_PRIORITY */
364 + nla_total_size(4) /* OVS_KEY_ATTR_IN_PORT */
365 + nla_total_size(4) /* OVS_KEY_ATTR_SKB_MARK */
366 + nla_total_size(12) /* OVS_KEY_ATTR_ETHERNET */
367 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
368 + nla_total_size(4) /* OVS_KEY_ATTR_8021Q */
369 + nla_total_size(0) /* OVS_KEY_ATTR_ENCAP */
370 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
371 + nla_total_size(40) /* OVS_KEY_ATTR_IPV6 */
372 + nla_total_size(2) /* OVS_KEY_ATTR_ICMPV6 */
373 + nla_total_size(28); /* OVS_KEY_ATTR_ND */
374}
375
376static size_t upcall_msg_size(const struct sk_buff *skb,
377 const struct nlattr *userdata)
378{
379 size_t size = NLMSG_ALIGN(sizeof(struct ovs_header))
380 + nla_total_size(skb->len) /* OVS_PACKET_ATTR_PACKET */
381 + nla_total_size(key_attr_size()); /* OVS_PACKET_ATTR_KEY */
382
383 /* OVS_PACKET_ATTR_USERDATA */
384 if (userdata)
385 size += NLA_ALIGN(userdata->nla_len);
386
387 return size;
388}
389
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800390static int queue_userspace_packet(struct net *net, int dp_ifindex,
391 struct sk_buff *skb,
Jesse Grossccb13522011-10-25 19:26:31 -0700392 const struct dp_upcall_info *upcall_info)
393{
394 struct ovs_header *upcall;
395 struct sk_buff *nskb = NULL;
396 struct sk_buff *user_skb; /* to be queued to userspace */
397 struct nlattr *nla;
Jesse Grossccb13522011-10-25 19:26:31 -0700398 int err;
399
400 if (vlan_tx_tag_present(skb)) {
401 nskb = skb_clone(skb, GFP_ATOMIC);
402 if (!nskb)
403 return -ENOMEM;
404
Patrick McHardy86a9bad2013-04-19 02:04:30 +0000405 nskb = __vlan_put_tag(nskb, nskb->vlan_proto, vlan_tx_tag_get(nskb));
Dan Carpenter8aa51d62012-05-13 08:44:18 +0000406 if (!nskb)
Jesse Grossccb13522011-10-25 19:26:31 -0700407 return -ENOMEM;
408
409 nskb->vlan_tci = 0;
410 skb = nskb;
411 }
412
413 if (nla_attr_size(skb->len) > USHRT_MAX) {
414 err = -EFBIG;
415 goto out;
416 }
417
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100418 user_skb = genlmsg_new(upcall_msg_size(skb, upcall_info->userdata), GFP_ATOMIC);
Jesse Grossccb13522011-10-25 19:26:31 -0700419 if (!user_skb) {
420 err = -ENOMEM;
421 goto out;
422 }
423
424 upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
425 0, upcall_info->cmd);
426 upcall->dp_ifindex = dp_ifindex;
427
428 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
429 ovs_flow_to_nlattrs(upcall_info->key, user_skb);
430 nla_nest_end(user_skb, nla);
431
432 if (upcall_info->userdata)
Ben Pfaff44901082013-02-15 17:29:22 -0800433 __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA,
434 nla_len(upcall_info->userdata),
435 nla_data(upcall_info->userdata));
Jesse Grossccb13522011-10-25 19:26:31 -0700436
437 nla = __nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, skb->len);
438
439 skb_copy_and_csum_dev(skb, nla_data(nla));
440
Rich Lanea15ff762013-02-15 11:07:43 -0800441 genlmsg_end(user_skb, upcall);
Eric W. Biederman15e47302012-09-07 20:12:54 +0000442 err = genlmsg_unicast(net, user_skb, upcall_info->portid);
Jesse Grossccb13522011-10-25 19:26:31 -0700443
444out:
445 kfree_skb(nskb);
446 return err;
447}
448
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700449/* Called with ovs_mutex. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800450static int flush_flows(struct datapath *dp)
Jesse Grossccb13522011-10-25 19:26:31 -0700451{
452 struct flow_table *old_table;
453 struct flow_table *new_table;
Jesse Grossccb13522011-10-25 19:26:31 -0700454
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700455 old_table = ovsl_dereference(dp->table);
Jesse Grossccb13522011-10-25 19:26:31 -0700456 new_table = ovs_flow_tbl_alloc(TBL_MIN_BUCKETS);
457 if (!new_table)
458 return -ENOMEM;
459
460 rcu_assign_pointer(dp->table, new_table);
461
462 ovs_flow_tbl_deferred_destroy(old_table);
463 return 0;
464}
465
466static int validate_actions(const struct nlattr *attr,
467 const struct sw_flow_key *key, int depth);
468
469static int validate_sample(const struct nlattr *attr,
470 const struct sw_flow_key *key, int depth)
471{
472 const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
473 const struct nlattr *probability, *actions;
474 const struct nlattr *a;
475 int rem;
476
477 memset(attrs, 0, sizeof(attrs));
478 nla_for_each_nested(a, attr, rem) {
479 int type = nla_type(a);
480 if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
481 return -EINVAL;
482 attrs[type] = a;
483 }
484 if (rem)
485 return -EINVAL;
486
487 probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
488 if (!probability || nla_len(probability) != sizeof(u32))
489 return -EINVAL;
490
491 actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
492 if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
493 return -EINVAL;
494 return validate_actions(actions, key, depth + 1);
495}
496
Pravin B Shelar072ae632012-05-07 17:21:53 -0700497static int validate_tp_port(const struct sw_flow_key *flow_key)
498{
499 if (flow_key->eth.type == htons(ETH_P_IP)) {
Jesse Gross41853922012-08-06 15:49:47 -0700500 if (flow_key->ipv4.tp.src || flow_key->ipv4.tp.dst)
Pravin B Shelar072ae632012-05-07 17:21:53 -0700501 return 0;
502 } else if (flow_key->eth.type == htons(ETH_P_IPV6)) {
Jesse Gross41853922012-08-06 15:49:47 -0700503 if (flow_key->ipv6.tp.src || flow_key->ipv6.tp.dst)
Pravin B Shelar072ae632012-05-07 17:21:53 -0700504 return 0;
505 }
506
507 return -EINVAL;
508}
509
Jesse Grossccb13522011-10-25 19:26:31 -0700510static int validate_set(const struct nlattr *a,
511 const struct sw_flow_key *flow_key)
512{
513 const struct nlattr *ovs_key = nla_data(a);
514 int key_type = nla_type(ovs_key);
515
516 /* There can be only one key in a action */
517 if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
518 return -EINVAL;
519
520 if (key_type > OVS_KEY_ATTR_MAX ||
521 nla_len(ovs_key) != ovs_key_lens[key_type])
522 return -EINVAL;
523
524 switch (key_type) {
525 const struct ovs_key_ipv4 *ipv4_key;
Ansis Atteka3fdbd1c2012-11-13 15:44:14 -0800526 const struct ovs_key_ipv6 *ipv6_key;
Jesse Grossccb13522011-10-25 19:26:31 -0700527
528 case OVS_KEY_ATTR_PRIORITY:
Ansis Atteka39c7caeb2012-11-26 11:24:11 -0800529 case OVS_KEY_ATTR_SKB_MARK:
Jesse Grossccb13522011-10-25 19:26:31 -0700530 case OVS_KEY_ATTR_ETHERNET:
531 break;
532
533 case OVS_KEY_ATTR_IPV4:
534 if (flow_key->eth.type != htons(ETH_P_IP))
535 return -EINVAL;
536
Jesse Gross41853922012-08-06 15:49:47 -0700537 if (!flow_key->ip.proto)
Jesse Grossccb13522011-10-25 19:26:31 -0700538 return -EINVAL;
539
540 ipv4_key = nla_data(ovs_key);
541 if (ipv4_key->ipv4_proto != flow_key->ip.proto)
542 return -EINVAL;
543
544 if (ipv4_key->ipv4_frag != flow_key->ip.frag)
545 return -EINVAL;
546
547 break;
548
Ansis Atteka3fdbd1c2012-11-13 15:44:14 -0800549 case OVS_KEY_ATTR_IPV6:
550 if (flow_key->eth.type != htons(ETH_P_IPV6))
551 return -EINVAL;
552
553 if (!flow_key->ip.proto)
554 return -EINVAL;
555
556 ipv6_key = nla_data(ovs_key);
557 if (ipv6_key->ipv6_proto != flow_key->ip.proto)
558 return -EINVAL;
559
560 if (ipv6_key->ipv6_frag != flow_key->ip.frag)
561 return -EINVAL;
562
563 if (ntohl(ipv6_key->ipv6_label) & 0xFFF00000)
564 return -EINVAL;
565
566 break;
567
Jesse Grossccb13522011-10-25 19:26:31 -0700568 case OVS_KEY_ATTR_TCP:
569 if (flow_key->ip.proto != IPPROTO_TCP)
570 return -EINVAL;
571
Pravin B Shelar072ae632012-05-07 17:21:53 -0700572 return validate_tp_port(flow_key);
Jesse Grossccb13522011-10-25 19:26:31 -0700573
574 case OVS_KEY_ATTR_UDP:
575 if (flow_key->ip.proto != IPPROTO_UDP)
576 return -EINVAL;
577
Pravin B Shelar072ae632012-05-07 17:21:53 -0700578 return validate_tp_port(flow_key);
Jesse Grossccb13522011-10-25 19:26:31 -0700579
580 default:
581 return -EINVAL;
582 }
583
584 return 0;
585}
586
587static int validate_userspace(const struct nlattr *attr)
588{
589 static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
590 [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
Ben Pfaff44901082013-02-15 17:29:22 -0800591 [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_UNSPEC },
Jesse Grossccb13522011-10-25 19:26:31 -0700592 };
593 struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
594 int error;
595
596 error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX,
597 attr, userspace_policy);
598 if (error)
599 return error;
600
601 if (!a[OVS_USERSPACE_ATTR_PID] ||
602 !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
603 return -EINVAL;
604
605 return 0;
606}
607
608static int validate_actions(const struct nlattr *attr,
609 const struct sw_flow_key *key, int depth)
610{
611 const struct nlattr *a;
612 int rem, err;
613
614 if (depth >= SAMPLE_ACTION_DEPTH)
615 return -EOVERFLOW;
616
617 nla_for_each_nested(a, attr, rem) {
618 /* Expected argument lengths, (u32)-1 for variable length. */
619 static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
620 [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
621 [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
622 [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
623 [OVS_ACTION_ATTR_POP_VLAN] = 0,
624 [OVS_ACTION_ATTR_SET] = (u32)-1,
625 [OVS_ACTION_ATTR_SAMPLE] = (u32)-1
626 };
627 const struct ovs_action_push_vlan *vlan;
628 int type = nla_type(a);
629
630 if (type > OVS_ACTION_ATTR_MAX ||
631 (action_lens[type] != nla_len(a) &&
632 action_lens[type] != (u32)-1))
633 return -EINVAL;
634
635 switch (type) {
636 case OVS_ACTION_ATTR_UNSPEC:
637 return -EINVAL;
638
639 case OVS_ACTION_ATTR_USERSPACE:
640 err = validate_userspace(a);
641 if (err)
642 return err;
643 break;
644
645 case OVS_ACTION_ATTR_OUTPUT:
646 if (nla_get_u32(a) >= DP_MAX_PORTS)
647 return -EINVAL;
648 break;
649
650
651 case OVS_ACTION_ATTR_POP_VLAN:
652 break;
653
654 case OVS_ACTION_ATTR_PUSH_VLAN:
655 vlan = nla_data(a);
656 if (vlan->vlan_tpid != htons(ETH_P_8021Q))
657 return -EINVAL;
658 if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
659 return -EINVAL;
660 break;
661
662 case OVS_ACTION_ATTR_SET:
663 err = validate_set(a, key);
664 if (err)
665 return err;
666 break;
667
668 case OVS_ACTION_ATTR_SAMPLE:
669 err = validate_sample(a, key, depth);
670 if (err)
671 return err;
672 break;
673
674 default:
675 return -EINVAL;
676 }
677 }
678
679 if (rem > 0)
680 return -EINVAL;
681
682 return 0;
683}
684
685static void clear_stats(struct sw_flow *flow)
686{
687 flow->used = 0;
688 flow->tcp_flags = 0;
689 flow->packet_count = 0;
690 flow->byte_count = 0;
691}
692
693static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
694{
695 struct ovs_header *ovs_header = info->userhdr;
696 struct nlattr **a = info->attrs;
697 struct sw_flow_actions *acts;
698 struct sk_buff *packet;
699 struct sw_flow *flow;
700 struct datapath *dp;
701 struct ethhdr *eth;
702 int len;
703 int err;
704 int key_len;
705
706 err = -EINVAL;
707 if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
Thomas Grafdded45fc2013-03-29 14:46:47 +0100708 !a[OVS_PACKET_ATTR_ACTIONS])
Jesse Grossccb13522011-10-25 19:26:31 -0700709 goto err;
710
711 len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
712 packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
713 err = -ENOMEM;
714 if (!packet)
715 goto err;
716 skb_reserve(packet, NET_IP_ALIGN);
717
Thomas Graf32686a92013-03-29 14:46:48 +0100718 nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
Jesse Grossccb13522011-10-25 19:26:31 -0700719
720 skb_reset_mac_header(packet);
721 eth = eth_hdr(packet);
722
723 /* Normally, setting the skb 'protocol' field would be handled by a
724 * call to eth_type_trans(), but it assumes there's a sending
725 * device, which we may not have. */
Simon Hormane5c5d222013-03-28 13:38:25 +0900726 if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN)
Jesse Grossccb13522011-10-25 19:26:31 -0700727 packet->protocol = eth->h_proto;
728 else
729 packet->protocol = htons(ETH_P_802_2);
730
731 /* Build an sw_flow for sending this packet. */
732 flow = ovs_flow_alloc();
733 err = PTR_ERR(flow);
734 if (IS_ERR(flow))
735 goto err_kfree_skb;
736
737 err = ovs_flow_extract(packet, -1, &flow->key, &key_len);
738 if (err)
739 goto err_flow_free;
740
741 err = ovs_flow_metadata_from_nlattrs(&flow->key.phy.priority,
Ansis Atteka39c7caeb2012-11-26 11:24:11 -0800742 &flow->key.phy.skb_mark,
Jesse Grossccb13522011-10-25 19:26:31 -0700743 &flow->key.phy.in_port,
744 a[OVS_PACKET_ATTR_KEY]);
745 if (err)
746 goto err_flow_free;
747
748 err = validate_actions(a[OVS_PACKET_ATTR_ACTIONS], &flow->key, 0);
749 if (err)
750 goto err_flow_free;
751
752 flow->hash = ovs_flow_hash(&flow->key, key_len);
753
754 acts = ovs_flow_actions_alloc(a[OVS_PACKET_ATTR_ACTIONS]);
755 err = PTR_ERR(acts);
756 if (IS_ERR(acts))
757 goto err_flow_free;
758 rcu_assign_pointer(flow->sf_acts, acts);
759
760 OVS_CB(packet)->flow = flow;
761 packet->priority = flow->key.phy.priority;
Ansis Atteka39c7caeb2012-11-26 11:24:11 -0800762 packet->mark = flow->key.phy.skb_mark;
Jesse Grossccb13522011-10-25 19:26:31 -0700763
764 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800765 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -0700766 err = -ENODEV;
767 if (!dp)
768 goto err_unlock;
769
770 local_bh_disable();
771 err = ovs_execute_actions(dp, packet);
772 local_bh_enable();
773 rcu_read_unlock();
774
775 ovs_flow_free(flow);
776 return err;
777
778err_unlock:
779 rcu_read_unlock();
780err_flow_free:
781 ovs_flow_free(flow);
782err_kfree_skb:
783 kfree_skb(packet);
784err:
785 return err;
786}
787
788static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
Thomas Grafdded45fc2013-03-29 14:46:47 +0100789 [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
Jesse Grossccb13522011-10-25 19:26:31 -0700790 [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
791 [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
792};
793
794static struct genl_ops dp_packet_genl_ops[] = {
795 { .cmd = OVS_PACKET_CMD_EXECUTE,
796 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
797 .policy = packet_policy,
798 .doit = ovs_packet_cmd_execute
799 }
800};
801
802static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats)
803{
804 int i;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700805 struct flow_table *table = ovsl_dereference(dp->table);
Jesse Grossccb13522011-10-25 19:26:31 -0700806
807 stats->n_flows = ovs_flow_tbl_count(table);
808
809 stats->n_hit = stats->n_missed = stats->n_lost = 0;
810 for_each_possible_cpu(i) {
811 const struct dp_stats_percpu *percpu_stats;
812 struct dp_stats_percpu local_stats;
813 unsigned int start;
814
815 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
816
817 do {
818 start = u64_stats_fetch_begin_bh(&percpu_stats->sync);
819 local_stats = *percpu_stats;
820 } while (u64_stats_fetch_retry_bh(&percpu_stats->sync, start));
821
822 stats->n_hit += local_stats.n_hit;
823 stats->n_missed += local_stats.n_missed;
824 stats->n_lost += local_stats.n_lost;
825 }
826}
827
828static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
829 [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
830 [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
831 [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
832};
833
834static struct genl_family dp_flow_genl_family = {
835 .id = GENL_ID_GENERATE,
836 .hdrsize = sizeof(struct ovs_header),
837 .name = OVS_FLOW_FAMILY,
838 .version = OVS_FLOW_VERSION,
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800839 .maxattr = OVS_FLOW_ATTR_MAX,
Pravin B Shelar3a4e0d62013-04-23 07:48:48 +0000840 .netnsok = true,
841 .parallel_ops = true,
Jesse Grossccb13522011-10-25 19:26:31 -0700842};
843
844static struct genl_multicast_group ovs_dp_flow_multicast_group = {
845 .name = OVS_FLOW_MCGROUP
846};
847
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100848static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts)
849{
850 return NLMSG_ALIGN(sizeof(struct ovs_header))
851 + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_KEY */
852 + nla_total_size(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
853 + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
854 + nla_total_size(8) /* OVS_FLOW_ATTR_USED */
855 + nla_total_size(acts->actions_len); /* OVS_FLOW_ATTR_ACTIONS */
856}
857
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700858/* Called with ovs_mutex. */
Jesse Grossccb13522011-10-25 19:26:31 -0700859static int ovs_flow_cmd_fill_info(struct sw_flow *flow, struct datapath *dp,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000860 struct sk_buff *skb, u32 portid,
Jesse Grossccb13522011-10-25 19:26:31 -0700861 u32 seq, u32 flags, u8 cmd)
862{
863 const int skb_orig_len = skb->len;
864 const struct sw_flow_actions *sf_acts;
865 struct ovs_flow_stats stats;
866 struct ovs_header *ovs_header;
867 struct nlattr *nla;
868 unsigned long used;
869 u8 tcp_flags;
870 int err;
871
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700872 sf_acts = ovsl_dereference(flow->sf_acts);
Jesse Grossccb13522011-10-25 19:26:31 -0700873
Eric W. Biederman15e47302012-09-07 20:12:54 +0000874 ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family, flags, cmd);
Jesse Grossccb13522011-10-25 19:26:31 -0700875 if (!ovs_header)
876 return -EMSGSIZE;
877
878 ovs_header->dp_ifindex = get_dpifindex(dp);
879
880 nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
881 if (!nla)
882 goto nla_put_failure;
883 err = ovs_flow_to_nlattrs(&flow->key, skb);
884 if (err)
885 goto error;
886 nla_nest_end(skb, nla);
887
888 spin_lock_bh(&flow->lock);
889 used = flow->used;
890 stats.n_packets = flow->packet_count;
891 stats.n_bytes = flow->byte_count;
892 tcp_flags = flow->tcp_flags;
893 spin_unlock_bh(&flow->lock);
894
David S. Miller028d6a62012-03-29 23:20:48 -0400895 if (used &&
896 nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
897 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -0700898
David S. Miller028d6a62012-03-29 23:20:48 -0400899 if (stats.n_packets &&
900 nla_put(skb, OVS_FLOW_ATTR_STATS,
901 sizeof(struct ovs_flow_stats), &stats))
902 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -0700903
David S. Miller028d6a62012-03-29 23:20:48 -0400904 if (tcp_flags &&
905 nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, tcp_flags))
906 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -0700907
908 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
909 * this is the first flow to be dumped into 'skb'. This is unusual for
910 * Netlink but individual action lists can be longer than
911 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
912 * The userspace caller can always fetch the actions separately if it
913 * really wants them. (Most userspace callers in fact don't care.)
914 *
915 * This can only fail for dump operations because the skb is always
916 * properly sized for single flows.
917 */
918 err = nla_put(skb, OVS_FLOW_ATTR_ACTIONS, sf_acts->actions_len,
919 sf_acts->actions);
920 if (err < 0 && skb_orig_len)
921 goto error;
922
923 return genlmsg_end(skb, ovs_header);
924
925nla_put_failure:
926 err = -EMSGSIZE;
927error:
928 genlmsg_cancel(skb, ovs_header);
929 return err;
930}
931
932static struct sk_buff *ovs_flow_cmd_alloc_info(struct sw_flow *flow)
933{
934 const struct sw_flow_actions *sf_acts;
Jesse Grossccb13522011-10-25 19:26:31 -0700935
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700936 sf_acts = ovsl_dereference(flow->sf_acts);
Jesse Grossccb13522011-10-25 19:26:31 -0700937
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100938 return genlmsg_new(ovs_flow_cmd_msg_size(sf_acts), GFP_KERNEL);
Jesse Grossccb13522011-10-25 19:26:31 -0700939}
940
941static struct sk_buff *ovs_flow_cmd_build_info(struct sw_flow *flow,
942 struct datapath *dp,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000943 u32 portid, u32 seq, u8 cmd)
Jesse Grossccb13522011-10-25 19:26:31 -0700944{
945 struct sk_buff *skb;
946 int retval;
947
948 skb = ovs_flow_cmd_alloc_info(flow);
949 if (!skb)
950 return ERR_PTR(-ENOMEM);
951
Eric W. Biederman15e47302012-09-07 20:12:54 +0000952 retval = ovs_flow_cmd_fill_info(flow, dp, skb, portid, seq, 0, cmd);
Jesse Grossccb13522011-10-25 19:26:31 -0700953 BUG_ON(retval < 0);
954 return skb;
955}
956
957static int ovs_flow_cmd_new_or_set(struct sk_buff *skb, struct genl_info *info)
958{
959 struct nlattr **a = info->attrs;
960 struct ovs_header *ovs_header = info->userhdr;
961 struct sw_flow_key key;
962 struct sw_flow *flow;
963 struct sk_buff *reply;
964 struct datapath *dp;
965 struct flow_table *table;
966 int error;
967 int key_len;
968
969 /* Extract key. */
970 error = -EINVAL;
971 if (!a[OVS_FLOW_ATTR_KEY])
972 goto error;
973 error = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
974 if (error)
975 goto error;
976
977 /* Validate actions. */
978 if (a[OVS_FLOW_ATTR_ACTIONS]) {
979 error = validate_actions(a[OVS_FLOW_ATTR_ACTIONS], &key, 0);
980 if (error)
981 goto error;
982 } else if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW) {
983 error = -EINVAL;
984 goto error;
985 }
986
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700987 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800988 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -0700989 error = -ENODEV;
990 if (!dp)
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700991 goto err_unlock_ovs;
Jesse Grossccb13522011-10-25 19:26:31 -0700992
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700993 table = ovsl_dereference(dp->table);
Jesse Grossccb13522011-10-25 19:26:31 -0700994 flow = ovs_flow_tbl_lookup(table, &key, key_len);
995 if (!flow) {
996 struct sw_flow_actions *acts;
997
998 /* Bail out if we're not allowed to create a new flow. */
999 error = -ENOENT;
1000 if (info->genlhdr->cmd == OVS_FLOW_CMD_SET)
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001001 goto err_unlock_ovs;
Jesse Grossccb13522011-10-25 19:26:31 -07001002
1003 /* Expand table, if necessary, to make room. */
1004 if (ovs_flow_tbl_need_to_expand(table)) {
1005 struct flow_table *new_table;
1006
1007 new_table = ovs_flow_tbl_expand(table);
1008 if (!IS_ERR(new_table)) {
1009 rcu_assign_pointer(dp->table, new_table);
1010 ovs_flow_tbl_deferred_destroy(table);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001011 table = ovsl_dereference(dp->table);
Jesse Grossccb13522011-10-25 19:26:31 -07001012 }
1013 }
1014
1015 /* Allocate flow. */
1016 flow = ovs_flow_alloc();
1017 if (IS_ERR(flow)) {
1018 error = PTR_ERR(flow);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001019 goto err_unlock_ovs;
Jesse Grossccb13522011-10-25 19:26:31 -07001020 }
1021 flow->key = key;
1022 clear_stats(flow);
1023
1024 /* Obtain actions. */
1025 acts = ovs_flow_actions_alloc(a[OVS_FLOW_ATTR_ACTIONS]);
1026 error = PTR_ERR(acts);
1027 if (IS_ERR(acts))
1028 goto error_free_flow;
1029 rcu_assign_pointer(flow->sf_acts, acts);
1030
1031 /* Put flow in bucket. */
1032 flow->hash = ovs_flow_hash(&key, key_len);
1033 ovs_flow_tbl_insert(table, flow);
1034
Eric W. Biederman15e47302012-09-07 20:12:54 +00001035 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001036 info->snd_seq,
1037 OVS_FLOW_CMD_NEW);
1038 } else {
1039 /* We found a matching flow. */
1040 struct sw_flow_actions *old_acts;
1041 struct nlattr *acts_attrs;
1042
1043 /* Bail out if we're not allowed to modify an existing flow.
1044 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
1045 * because Generic Netlink treats the latter as a dump
1046 * request. We also accept NLM_F_EXCL in case that bug ever
1047 * gets fixed.
1048 */
1049 error = -EEXIST;
1050 if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW &&
1051 info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL))
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001052 goto err_unlock_ovs;
Jesse Grossccb13522011-10-25 19:26:31 -07001053
1054 /* Update actions. */
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001055 old_acts = ovsl_dereference(flow->sf_acts);
Jesse Grossccb13522011-10-25 19:26:31 -07001056 acts_attrs = a[OVS_FLOW_ATTR_ACTIONS];
1057 if (acts_attrs &&
1058 (old_acts->actions_len != nla_len(acts_attrs) ||
1059 memcmp(old_acts->actions, nla_data(acts_attrs),
1060 old_acts->actions_len))) {
1061 struct sw_flow_actions *new_acts;
1062
1063 new_acts = ovs_flow_actions_alloc(acts_attrs);
1064 error = PTR_ERR(new_acts);
1065 if (IS_ERR(new_acts))
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001066 goto err_unlock_ovs;
Jesse Grossccb13522011-10-25 19:26:31 -07001067
1068 rcu_assign_pointer(flow->sf_acts, new_acts);
1069 ovs_flow_deferred_free_acts(old_acts);
1070 }
1071
Eric W. Biederman15e47302012-09-07 20:12:54 +00001072 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001073 info->snd_seq, OVS_FLOW_CMD_NEW);
1074
1075 /* Clear stats. */
1076 if (a[OVS_FLOW_ATTR_CLEAR]) {
1077 spin_lock_bh(&flow->lock);
1078 clear_stats(flow);
1079 spin_unlock_bh(&flow->lock);
1080 }
1081 }
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001082 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001083
1084 if (!IS_ERR(reply))
Thomas Grafed661182013-03-29 14:46:50 +01001085 ovs_notify(reply, info, &ovs_dp_flow_multicast_group);
Jesse Grossccb13522011-10-25 19:26:31 -07001086 else
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001087 netlink_set_err(sock_net(skb->sk)->genl_sock, 0,
Jesse Grossccb13522011-10-25 19:26:31 -07001088 ovs_dp_flow_multicast_group.id, PTR_ERR(reply));
1089 return 0;
1090
1091error_free_flow:
1092 ovs_flow_free(flow);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001093err_unlock_ovs:
1094 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001095error:
1096 return error;
1097}
1098
1099static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1100{
1101 struct nlattr **a = info->attrs;
1102 struct ovs_header *ovs_header = info->userhdr;
1103 struct sw_flow_key key;
1104 struct sk_buff *reply;
1105 struct sw_flow *flow;
1106 struct datapath *dp;
1107 struct flow_table *table;
1108 int err;
1109 int key_len;
1110
1111 if (!a[OVS_FLOW_ATTR_KEY])
1112 return -EINVAL;
1113 err = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
1114 if (err)
1115 return err;
1116
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001117 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001118 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001119 if (!dp) {
1120 err = -ENODEV;
1121 goto unlock;
1122 }
Jesse Grossccb13522011-10-25 19:26:31 -07001123
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001124 table = ovsl_dereference(dp->table);
Jesse Grossccb13522011-10-25 19:26:31 -07001125 flow = ovs_flow_tbl_lookup(table, &key, key_len);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001126 if (!flow) {
1127 err = -ENOENT;
1128 goto unlock;
1129 }
Jesse Grossccb13522011-10-25 19:26:31 -07001130
Eric W. Biederman15e47302012-09-07 20:12:54 +00001131 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001132 info->snd_seq, OVS_FLOW_CMD_NEW);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001133 if (IS_ERR(reply)) {
1134 err = PTR_ERR(reply);
1135 goto unlock;
1136 }
Jesse Grossccb13522011-10-25 19:26:31 -07001137
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001138 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001139 return genlmsg_reply(reply, info);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001140unlock:
1141 ovs_unlock();
1142 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001143}
1144
1145static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1146{
1147 struct nlattr **a = info->attrs;
1148 struct ovs_header *ovs_header = info->userhdr;
1149 struct sw_flow_key key;
1150 struct sk_buff *reply;
1151 struct sw_flow *flow;
1152 struct datapath *dp;
1153 struct flow_table *table;
1154 int err;
1155 int key_len;
1156
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001157 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001158 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001159 if (!dp) {
1160 err = -ENODEV;
1161 goto unlock;
1162 }
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001163
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001164 if (!a[OVS_FLOW_ATTR_KEY]) {
1165 err = flush_flows(dp);
1166 goto unlock;
1167 }
Jesse Grossccb13522011-10-25 19:26:31 -07001168 err = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
1169 if (err)
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001170 goto unlock;
Jesse Grossccb13522011-10-25 19:26:31 -07001171
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001172 table = ovsl_dereference(dp->table);
Jesse Grossccb13522011-10-25 19:26:31 -07001173 flow = ovs_flow_tbl_lookup(table, &key, key_len);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001174 if (!flow) {
1175 err = -ENOENT;
1176 goto unlock;
1177 }
Jesse Grossccb13522011-10-25 19:26:31 -07001178
1179 reply = ovs_flow_cmd_alloc_info(flow);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001180 if (!reply) {
1181 err = -ENOMEM;
1182 goto unlock;
1183 }
Jesse Grossccb13522011-10-25 19:26:31 -07001184
1185 ovs_flow_tbl_remove(table, flow);
1186
Eric W. Biederman15e47302012-09-07 20:12:54 +00001187 err = ovs_flow_cmd_fill_info(flow, dp, reply, info->snd_portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001188 info->snd_seq, 0, OVS_FLOW_CMD_DEL);
1189 BUG_ON(err < 0);
1190
1191 ovs_flow_deferred_free(flow);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001192 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001193
Thomas Grafed661182013-03-29 14:46:50 +01001194 ovs_notify(reply, info, &ovs_dp_flow_multicast_group);
Jesse Grossccb13522011-10-25 19:26:31 -07001195 return 0;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001196unlock:
1197 ovs_unlock();
1198 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001199}
1200
1201static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1202{
1203 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1204 struct datapath *dp;
1205 struct flow_table *table;
1206
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001207 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001208 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001209 if (!dp) {
1210 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001211 return -ENODEV;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001212 }
Jesse Grossccb13522011-10-25 19:26:31 -07001213
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001214 table = ovsl_dereference(dp->table);
Jesse Grossccb13522011-10-25 19:26:31 -07001215
1216 for (;;) {
1217 struct sw_flow *flow;
1218 u32 bucket, obj;
1219
1220 bucket = cb->args[0];
1221 obj = cb->args[1];
1222 flow = ovs_flow_tbl_next(table, &bucket, &obj);
1223 if (!flow)
1224 break;
1225
1226 if (ovs_flow_cmd_fill_info(flow, dp, skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001227 NETLINK_CB(cb->skb).portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001228 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1229 OVS_FLOW_CMD_NEW) < 0)
1230 break;
1231
1232 cb->args[0] = bucket;
1233 cb->args[1] = obj;
1234 }
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001235 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001236 return skb->len;
1237}
1238
1239static struct genl_ops dp_flow_genl_ops[] = {
1240 { .cmd = OVS_FLOW_CMD_NEW,
1241 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1242 .policy = flow_policy,
1243 .doit = ovs_flow_cmd_new_or_set
1244 },
1245 { .cmd = OVS_FLOW_CMD_DEL,
1246 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1247 .policy = flow_policy,
1248 .doit = ovs_flow_cmd_del
1249 },
1250 { .cmd = OVS_FLOW_CMD_GET,
1251 .flags = 0, /* OK for unprivileged users. */
1252 .policy = flow_policy,
1253 .doit = ovs_flow_cmd_get,
1254 .dumpit = ovs_flow_cmd_dump
1255 },
1256 { .cmd = OVS_FLOW_CMD_SET,
1257 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1258 .policy = flow_policy,
1259 .doit = ovs_flow_cmd_new_or_set,
1260 },
1261};
1262
1263static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1264 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1265 [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1266};
1267
1268static struct genl_family dp_datapath_genl_family = {
1269 .id = GENL_ID_GENERATE,
1270 .hdrsize = sizeof(struct ovs_header),
1271 .name = OVS_DATAPATH_FAMILY,
1272 .version = OVS_DATAPATH_VERSION,
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001273 .maxattr = OVS_DP_ATTR_MAX,
Pravin B Shelar3a4e0d62013-04-23 07:48:48 +00001274 .netnsok = true,
1275 .parallel_ops = true,
Jesse Grossccb13522011-10-25 19:26:31 -07001276};
1277
1278static struct genl_multicast_group ovs_dp_datapath_multicast_group = {
1279 .name = OVS_DATAPATH_MCGROUP
1280};
1281
Thomas Grafc3ff8cf2013-03-29 14:46:49 +01001282static size_t ovs_dp_cmd_msg_size(void)
1283{
1284 size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
1285
1286 msgsize += nla_total_size(IFNAMSIZ);
1287 msgsize += nla_total_size(sizeof(struct ovs_dp_stats));
1288
1289 return msgsize;
1290}
1291
Jesse Grossccb13522011-10-25 19:26:31 -07001292static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001293 u32 portid, u32 seq, u32 flags, u8 cmd)
Jesse Grossccb13522011-10-25 19:26:31 -07001294{
1295 struct ovs_header *ovs_header;
1296 struct ovs_dp_stats dp_stats;
1297 int err;
1298
Eric W. Biederman15e47302012-09-07 20:12:54 +00001299 ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
Jesse Grossccb13522011-10-25 19:26:31 -07001300 flags, cmd);
1301 if (!ovs_header)
1302 goto error;
1303
1304 ovs_header->dp_ifindex = get_dpifindex(dp);
1305
1306 rcu_read_lock();
1307 err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
1308 rcu_read_unlock();
1309 if (err)
1310 goto nla_put_failure;
1311
1312 get_dp_stats(dp, &dp_stats);
David S. Miller028d6a62012-03-29 23:20:48 -04001313 if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats), &dp_stats))
1314 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001315
1316 return genlmsg_end(skb, ovs_header);
1317
1318nla_put_failure:
1319 genlmsg_cancel(skb, ovs_header);
1320error:
1321 return -EMSGSIZE;
1322}
1323
Eric W. Biederman15e47302012-09-07 20:12:54 +00001324static struct sk_buff *ovs_dp_cmd_build_info(struct datapath *dp, u32 portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001325 u32 seq, u8 cmd)
1326{
1327 struct sk_buff *skb;
1328 int retval;
1329
Thomas Grafc3ff8cf2013-03-29 14:46:49 +01001330 skb = genlmsg_new(ovs_dp_cmd_msg_size(), GFP_KERNEL);
Jesse Grossccb13522011-10-25 19:26:31 -07001331 if (!skb)
1332 return ERR_PTR(-ENOMEM);
1333
Eric W. Biederman15e47302012-09-07 20:12:54 +00001334 retval = ovs_dp_cmd_fill_info(dp, skb, portid, seq, 0, cmd);
Jesse Grossccb13522011-10-25 19:26:31 -07001335 if (retval < 0) {
1336 kfree_skb(skb);
1337 return ERR_PTR(retval);
1338 }
1339 return skb;
1340}
1341
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001342/* Called with ovs_mutex. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001343static struct datapath *lookup_datapath(struct net *net,
1344 struct ovs_header *ovs_header,
Jesse Grossccb13522011-10-25 19:26:31 -07001345 struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1346{
1347 struct datapath *dp;
1348
1349 if (!a[OVS_DP_ATTR_NAME])
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001350 dp = get_dp(net, ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001351 else {
1352 struct vport *vport;
1353
1354 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001355 vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
Jesse Grossccb13522011-10-25 19:26:31 -07001356 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
1357 rcu_read_unlock();
1358 }
1359 return dp ? dp : ERR_PTR(-ENODEV);
1360}
1361
1362static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1363{
1364 struct nlattr **a = info->attrs;
1365 struct vport_parms parms;
1366 struct sk_buff *reply;
1367 struct datapath *dp;
1368 struct vport *vport;
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001369 struct ovs_net *ovs_net;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001370 int err, i;
Jesse Grossccb13522011-10-25 19:26:31 -07001371
1372 err = -EINVAL;
1373 if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1374 goto err;
1375
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001376 ovs_lock();
Jesse Grossccb13522011-10-25 19:26:31 -07001377
1378 err = -ENOMEM;
1379 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1380 if (dp == NULL)
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001381 goto err_unlock_ovs;
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001382
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001383 ovs_dp_set_net(dp, hold_net(sock_net(skb->sk)));
Jesse Grossccb13522011-10-25 19:26:31 -07001384
1385 /* Allocate table. */
1386 err = -ENOMEM;
1387 rcu_assign_pointer(dp->table, ovs_flow_tbl_alloc(TBL_MIN_BUCKETS));
1388 if (!dp->table)
1389 goto err_free_dp;
1390
1391 dp->stats_percpu = alloc_percpu(struct dp_stats_percpu);
1392 if (!dp->stats_percpu) {
1393 err = -ENOMEM;
1394 goto err_destroy_table;
1395 }
1396
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001397 dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
1398 GFP_KERNEL);
1399 if (!dp->ports) {
1400 err = -ENOMEM;
1401 goto err_destroy_percpu;
1402 }
1403
1404 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1405 INIT_HLIST_HEAD(&dp->ports[i]);
1406
Jesse Grossccb13522011-10-25 19:26:31 -07001407 /* Set up our datapath device. */
1408 parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1409 parms.type = OVS_VPORT_TYPE_INTERNAL;
1410 parms.options = NULL;
1411 parms.dp = dp;
1412 parms.port_no = OVSP_LOCAL;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001413 parms.upcall_portid = nla_get_u32(a[OVS_DP_ATTR_UPCALL_PID]);
Jesse Grossccb13522011-10-25 19:26:31 -07001414
1415 vport = new_vport(&parms);
1416 if (IS_ERR(vport)) {
1417 err = PTR_ERR(vport);
1418 if (err == -EBUSY)
1419 err = -EEXIST;
1420
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001421 goto err_destroy_ports_array;
Jesse Grossccb13522011-10-25 19:26:31 -07001422 }
1423
Eric W. Biederman15e47302012-09-07 20:12:54 +00001424 reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001425 info->snd_seq, OVS_DP_CMD_NEW);
1426 err = PTR_ERR(reply);
1427 if (IS_ERR(reply))
1428 goto err_destroy_local_port;
1429
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001430 ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
1431 list_add_tail(&dp->list_node, &ovs_net->dps);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001432
1433 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001434
Thomas Grafed661182013-03-29 14:46:50 +01001435 ovs_notify(reply, info, &ovs_dp_datapath_multicast_group);
Jesse Grossccb13522011-10-25 19:26:31 -07001436 return 0;
1437
1438err_destroy_local_port:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001439 ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001440err_destroy_ports_array:
1441 kfree(dp->ports);
Jesse Grossccb13522011-10-25 19:26:31 -07001442err_destroy_percpu:
1443 free_percpu(dp->stats_percpu);
1444err_destroy_table:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001445 ovs_flow_tbl_destroy(ovsl_dereference(dp->table));
Jesse Grossccb13522011-10-25 19:26:31 -07001446err_free_dp:
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001447 release_net(ovs_dp_get_net(dp));
Jesse Grossccb13522011-10-25 19:26:31 -07001448 kfree(dp);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001449err_unlock_ovs:
1450 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001451err:
1452 return err;
1453}
1454
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001455/* Called with ovs_mutex. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001456static void __dp_destroy(struct datapath *dp)
Jesse Grossccb13522011-10-25 19:26:31 -07001457{
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001458 int i;
Jesse Grossccb13522011-10-25 19:26:31 -07001459
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001460 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1461 struct vport *vport;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001462 struct hlist_node *n;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001463
Sasha Levinb67bfe02013-02-27 17:06:00 -08001464 hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001465 if (vport->port_no != OVSP_LOCAL)
1466 ovs_dp_detach_port(vport);
1467 }
Jesse Grossccb13522011-10-25 19:26:31 -07001468
1469 list_del(&dp->list_node);
Jesse Grossccb13522011-10-25 19:26:31 -07001470
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001471 /* OVSP_LOCAL is datapath internal port. We need to make sure that
1472 * all port in datapath are destroyed first before freeing datapath.
Jesse Grossccb13522011-10-25 19:26:31 -07001473 */
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001474 ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
Jesse Grossccb13522011-10-25 19:26:31 -07001475
1476 call_rcu(&dp->rcu, destroy_dp_rcu);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001477}
1478
1479static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1480{
1481 struct sk_buff *reply;
1482 struct datapath *dp;
1483 int err;
1484
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001485 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001486 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1487 err = PTR_ERR(dp);
1488 if (IS_ERR(dp))
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001489 goto unlock;
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001490
Eric W. Biederman15e47302012-09-07 20:12:54 +00001491 reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001492 info->snd_seq, OVS_DP_CMD_DEL);
1493 err = PTR_ERR(reply);
1494 if (IS_ERR(reply))
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001495 goto unlock;
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001496
1497 __dp_destroy(dp);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001498 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001499
Thomas Grafed661182013-03-29 14:46:50 +01001500 ovs_notify(reply, info, &ovs_dp_datapath_multicast_group);
Jesse Grossccb13522011-10-25 19:26:31 -07001501
1502 return 0;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001503unlock:
1504 ovs_unlock();
1505 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001506}
1507
1508static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1509{
1510 struct sk_buff *reply;
1511 struct datapath *dp;
1512 int err;
1513
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001514 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001515 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001516 err = PTR_ERR(dp);
Jesse Grossccb13522011-10-25 19:26:31 -07001517 if (IS_ERR(dp))
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001518 goto unlock;
Jesse Grossccb13522011-10-25 19:26:31 -07001519
Eric W. Biederman15e47302012-09-07 20:12:54 +00001520 reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001521 info->snd_seq, OVS_DP_CMD_NEW);
1522 if (IS_ERR(reply)) {
1523 err = PTR_ERR(reply);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001524 netlink_set_err(sock_net(skb->sk)->genl_sock, 0,
Jesse Grossccb13522011-10-25 19:26:31 -07001525 ovs_dp_datapath_multicast_group.id, err);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001526 err = 0;
1527 goto unlock;
Jesse Grossccb13522011-10-25 19:26:31 -07001528 }
1529
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001530 ovs_unlock();
Thomas Grafed661182013-03-29 14:46:50 +01001531 ovs_notify(reply, info, &ovs_dp_datapath_multicast_group);
Jesse Grossccb13522011-10-25 19:26:31 -07001532
1533 return 0;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001534unlock:
1535 ovs_unlock();
1536 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001537}
1538
1539static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1540{
1541 struct sk_buff *reply;
1542 struct datapath *dp;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001543 int err;
Jesse Grossccb13522011-10-25 19:26:31 -07001544
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001545 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001546 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001547 if (IS_ERR(dp)) {
1548 err = PTR_ERR(dp);
1549 goto unlock;
1550 }
Jesse Grossccb13522011-10-25 19:26:31 -07001551
Eric W. Biederman15e47302012-09-07 20:12:54 +00001552 reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001553 info->snd_seq, OVS_DP_CMD_NEW);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001554 if (IS_ERR(reply)) {
1555 err = PTR_ERR(reply);
1556 goto unlock;
1557 }
Jesse Grossccb13522011-10-25 19:26:31 -07001558
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001559 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001560 return genlmsg_reply(reply, info);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001561
1562unlock:
1563 ovs_unlock();
1564 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001565}
1566
1567static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1568{
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001569 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
Jesse Grossccb13522011-10-25 19:26:31 -07001570 struct datapath *dp;
1571 int skip = cb->args[0];
1572 int i = 0;
1573
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001574 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001575 list_for_each_entry(dp, &ovs_net->dps, list_node) {
Ben Pfaff77676fd2012-01-17 13:33:39 +00001576 if (i >= skip &&
Eric W. Biederman15e47302012-09-07 20:12:54 +00001577 ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001578 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1579 OVS_DP_CMD_NEW) < 0)
1580 break;
1581 i++;
1582 }
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001583 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001584
1585 cb->args[0] = i;
1586
1587 return skb->len;
1588}
1589
1590static struct genl_ops dp_datapath_genl_ops[] = {
1591 { .cmd = OVS_DP_CMD_NEW,
1592 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1593 .policy = datapath_policy,
1594 .doit = ovs_dp_cmd_new
1595 },
1596 { .cmd = OVS_DP_CMD_DEL,
1597 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1598 .policy = datapath_policy,
1599 .doit = ovs_dp_cmd_del
1600 },
1601 { .cmd = OVS_DP_CMD_GET,
1602 .flags = 0, /* OK for unprivileged users. */
1603 .policy = datapath_policy,
1604 .doit = ovs_dp_cmd_get,
1605 .dumpit = ovs_dp_cmd_dump
1606 },
1607 { .cmd = OVS_DP_CMD_SET,
1608 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1609 .policy = datapath_policy,
1610 .doit = ovs_dp_cmd_set,
1611 },
1612};
1613
1614static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
1615 [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1616 [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
1617 [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
1618 [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
1619 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1620 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
1621};
1622
1623static struct genl_family dp_vport_genl_family = {
1624 .id = GENL_ID_GENERATE,
1625 .hdrsize = sizeof(struct ovs_header),
1626 .name = OVS_VPORT_FAMILY,
1627 .version = OVS_VPORT_VERSION,
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001628 .maxattr = OVS_VPORT_ATTR_MAX,
Pravin B Shelar3a4e0d62013-04-23 07:48:48 +00001629 .netnsok = true,
1630 .parallel_ops = true,
Jesse Grossccb13522011-10-25 19:26:31 -07001631};
1632
1633struct genl_multicast_group ovs_dp_vport_multicast_group = {
1634 .name = OVS_VPORT_MCGROUP
1635};
1636
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001637/* Called with ovs_mutex or RCU read lock. */
Jesse Grossccb13522011-10-25 19:26:31 -07001638static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001639 u32 portid, u32 seq, u32 flags, u8 cmd)
Jesse Grossccb13522011-10-25 19:26:31 -07001640{
1641 struct ovs_header *ovs_header;
1642 struct ovs_vport_stats vport_stats;
1643 int err;
1644
Eric W. Biederman15e47302012-09-07 20:12:54 +00001645 ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
Jesse Grossccb13522011-10-25 19:26:31 -07001646 flags, cmd);
1647 if (!ovs_header)
1648 return -EMSGSIZE;
1649
1650 ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1651
David S. Miller028d6a62012-03-29 23:20:48 -04001652 if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
1653 nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
1654 nla_put_string(skb, OVS_VPORT_ATTR_NAME, vport->ops->get_name(vport)) ||
Eric W. Biederman15e47302012-09-07 20:12:54 +00001655 nla_put_u32(skb, OVS_VPORT_ATTR_UPCALL_PID, vport->upcall_portid))
David S. Miller028d6a62012-03-29 23:20:48 -04001656 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001657
1658 ovs_vport_get_stats(vport, &vport_stats);
David S. Miller028d6a62012-03-29 23:20:48 -04001659 if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
1660 &vport_stats))
1661 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001662
1663 err = ovs_vport_get_options(vport, skb);
1664 if (err == -EMSGSIZE)
1665 goto error;
1666
1667 return genlmsg_end(skb, ovs_header);
1668
1669nla_put_failure:
1670 err = -EMSGSIZE;
1671error:
1672 genlmsg_cancel(skb, ovs_header);
1673 return err;
1674}
1675
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001676/* Called with ovs_mutex or RCU read lock. */
Eric W. Biederman15e47302012-09-07 20:12:54 +00001677struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001678 u32 seq, u8 cmd)
1679{
1680 struct sk_buff *skb;
1681 int retval;
1682
1683 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1684 if (!skb)
1685 return ERR_PTR(-ENOMEM);
1686
Eric W. Biederman15e47302012-09-07 20:12:54 +00001687 retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
Jesse Grossa9341512013-03-26 15:48:38 -07001688 BUG_ON(retval < 0);
1689
Jesse Grossccb13522011-10-25 19:26:31 -07001690 return skb;
1691}
1692
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001693/* Called with ovs_mutex or RCU read lock. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001694static struct vport *lookup_vport(struct net *net,
1695 struct ovs_header *ovs_header,
Jesse Grossccb13522011-10-25 19:26:31 -07001696 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1697{
1698 struct datapath *dp;
1699 struct vport *vport;
1700
1701 if (a[OVS_VPORT_ATTR_NAME]) {
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001702 vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
Jesse Grossccb13522011-10-25 19:26:31 -07001703 if (!vport)
1704 return ERR_PTR(-ENODEV);
Ben Pfaff651a68e2012-03-06 15:04:04 -08001705 if (ovs_header->dp_ifindex &&
1706 ovs_header->dp_ifindex != get_dpifindex(vport->dp))
1707 return ERR_PTR(-ENODEV);
Jesse Grossccb13522011-10-25 19:26:31 -07001708 return vport;
1709 } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
1710 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1711
1712 if (port_no >= DP_MAX_PORTS)
1713 return ERR_PTR(-EFBIG);
1714
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001715 dp = get_dp(net, ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001716 if (!dp)
1717 return ERR_PTR(-ENODEV);
1718
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001719 vport = ovs_vport_ovsl_rcu(dp, port_no);
Jesse Grossccb13522011-10-25 19:26:31 -07001720 if (!vport)
Jarno Rajahalme14408db2013-01-09 14:27:35 -08001721 return ERR_PTR(-ENODEV);
Jesse Grossccb13522011-10-25 19:26:31 -07001722 return vport;
1723 } else
1724 return ERR_PTR(-EINVAL);
1725}
1726
1727static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
1728{
1729 struct nlattr **a = info->attrs;
1730 struct ovs_header *ovs_header = info->userhdr;
1731 struct vport_parms parms;
1732 struct sk_buff *reply;
1733 struct vport *vport;
1734 struct datapath *dp;
1735 u32 port_no;
1736 int err;
1737
1738 err = -EINVAL;
1739 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
1740 !a[OVS_VPORT_ATTR_UPCALL_PID])
1741 goto exit;
1742
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001743 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001744 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001745 err = -ENODEV;
1746 if (!dp)
1747 goto exit_unlock;
1748
1749 if (a[OVS_VPORT_ATTR_PORT_NO]) {
1750 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1751
1752 err = -EFBIG;
1753 if (port_no >= DP_MAX_PORTS)
1754 goto exit_unlock;
1755
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001756 vport = ovs_vport_ovsl(dp, port_no);
Jesse Grossccb13522011-10-25 19:26:31 -07001757 err = -EBUSY;
1758 if (vport)
1759 goto exit_unlock;
1760 } else {
1761 for (port_no = 1; ; port_no++) {
1762 if (port_no >= DP_MAX_PORTS) {
1763 err = -EFBIG;
1764 goto exit_unlock;
1765 }
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001766 vport = ovs_vport_ovsl(dp, port_no);
Jesse Grossccb13522011-10-25 19:26:31 -07001767 if (!vport)
1768 break;
1769 }
1770 }
1771
1772 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
1773 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1774 parms.options = a[OVS_VPORT_ATTR_OPTIONS];
1775 parms.dp = dp;
1776 parms.port_no = port_no;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001777 parms.upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
Jesse Grossccb13522011-10-25 19:26:31 -07001778
1779 vport = new_vport(&parms);
1780 err = PTR_ERR(vport);
1781 if (IS_ERR(vport))
1782 goto exit_unlock;
1783
Rich Lanecb7c5bd2013-02-08 13:18:01 -08001784 err = 0;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001785 reply = ovs_vport_cmd_build_info(vport, info->snd_portid, info->snd_seq,
Jesse Grossccb13522011-10-25 19:26:31 -07001786 OVS_VPORT_CMD_NEW);
1787 if (IS_ERR(reply)) {
1788 err = PTR_ERR(reply);
1789 ovs_dp_detach_port(vport);
1790 goto exit_unlock;
1791 }
Thomas Grafed661182013-03-29 14:46:50 +01001792
1793 ovs_notify(reply, info, &ovs_dp_vport_multicast_group);
Jesse Grossccb13522011-10-25 19:26:31 -07001794
1795exit_unlock:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001796 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001797exit:
1798 return err;
1799}
1800
1801static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
1802{
1803 struct nlattr **a = info->attrs;
1804 struct sk_buff *reply;
1805 struct vport *vport;
1806 int err;
1807
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001808 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001809 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
Jesse Grossccb13522011-10-25 19:26:31 -07001810 err = PTR_ERR(vport);
1811 if (IS_ERR(vport))
1812 goto exit_unlock;
1813
1814 err = 0;
1815 if (a[OVS_VPORT_ATTR_TYPE] &&
1816 nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type)
1817 err = -EINVAL;
1818
Jesse Grossa9341512013-03-26 15:48:38 -07001819 reply = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1820 if (!reply) {
1821 err = -ENOMEM;
1822 goto exit_unlock;
1823 }
1824
Jesse Grossccb13522011-10-25 19:26:31 -07001825 if (!err && a[OVS_VPORT_ATTR_OPTIONS])
1826 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
Ansis Atteka03fbf8b2012-04-09 12:12:12 -07001827 if (err)
Jesse Grossa9341512013-03-26 15:48:38 -07001828 goto exit_free;
1829
Ansis Atteka03fbf8b2012-04-09 12:12:12 -07001830 if (a[OVS_VPORT_ATTR_UPCALL_PID])
Eric W. Biederman15e47302012-09-07 20:12:54 +00001831 vport->upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
Jesse Grossccb13522011-10-25 19:26:31 -07001832
Jesse Grossa9341512013-03-26 15:48:38 -07001833 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1834 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1835 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001836
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001837 ovs_unlock();
Thomas Grafed661182013-03-29 14:46:50 +01001838 ovs_notify(reply, info, &ovs_dp_vport_multicast_group);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001839 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001840
Jesse Grossa9341512013-03-26 15:48:38 -07001841 rtnl_unlock();
1842 return 0;
1843
1844exit_free:
1845 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001846exit_unlock:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001847 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001848 return err;
1849}
1850
1851static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
1852{
1853 struct nlattr **a = info->attrs;
1854 struct sk_buff *reply;
1855 struct vport *vport;
1856 int err;
1857
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001858 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001859 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
Jesse Grossccb13522011-10-25 19:26:31 -07001860 err = PTR_ERR(vport);
1861 if (IS_ERR(vport))
1862 goto exit_unlock;
1863
1864 if (vport->port_no == OVSP_LOCAL) {
1865 err = -EINVAL;
1866 goto exit_unlock;
1867 }
1868
Eric W. Biederman15e47302012-09-07 20:12:54 +00001869 reply = ovs_vport_cmd_build_info(vport, info->snd_portid, info->snd_seq,
Jesse Grossccb13522011-10-25 19:26:31 -07001870 OVS_VPORT_CMD_DEL);
1871 err = PTR_ERR(reply);
1872 if (IS_ERR(reply))
1873 goto exit_unlock;
1874
Rich Lane734907e2013-02-08 09:30:23 -08001875 err = 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001876 ovs_dp_detach_port(vport);
1877
Thomas Grafed661182013-03-29 14:46:50 +01001878 ovs_notify(reply, info, &ovs_dp_vport_multicast_group);
Jesse Grossccb13522011-10-25 19:26:31 -07001879
1880exit_unlock:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001881 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001882 return err;
1883}
1884
1885static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
1886{
1887 struct nlattr **a = info->attrs;
1888 struct ovs_header *ovs_header = info->userhdr;
1889 struct sk_buff *reply;
1890 struct vport *vport;
1891 int err;
1892
1893 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001894 vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
Jesse Grossccb13522011-10-25 19:26:31 -07001895 err = PTR_ERR(vport);
1896 if (IS_ERR(vport))
1897 goto exit_unlock;
1898
Eric W. Biederman15e47302012-09-07 20:12:54 +00001899 reply = ovs_vport_cmd_build_info(vport, info->snd_portid, info->snd_seq,
Jesse Grossccb13522011-10-25 19:26:31 -07001900 OVS_VPORT_CMD_NEW);
1901 err = PTR_ERR(reply);
1902 if (IS_ERR(reply))
1903 goto exit_unlock;
1904
1905 rcu_read_unlock();
1906
1907 return genlmsg_reply(reply, info);
1908
1909exit_unlock:
1910 rcu_read_unlock();
1911 return err;
1912}
1913
1914static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1915{
1916 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1917 struct datapath *dp;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001918 int bucket = cb->args[0], skip = cb->args[1];
1919 int i, j = 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001920
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001921 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001922 if (!dp)
1923 return -ENODEV;
1924
1925 rcu_read_lock();
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001926 for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
Jesse Grossccb13522011-10-25 19:26:31 -07001927 struct vport *vport;
1928
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001929 j = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001930 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001931 if (j >= skip &&
1932 ovs_vport_cmd_fill_info(vport, skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001933 NETLINK_CB(cb->skb).portid,
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001934 cb->nlh->nlmsg_seq,
1935 NLM_F_MULTI,
1936 OVS_VPORT_CMD_NEW) < 0)
1937 goto out;
Jesse Grossccb13522011-10-25 19:26:31 -07001938
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001939 j++;
1940 }
1941 skip = 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001942 }
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001943out:
Jesse Grossccb13522011-10-25 19:26:31 -07001944 rcu_read_unlock();
1945
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001946 cb->args[0] = i;
1947 cb->args[1] = j;
Jesse Grossccb13522011-10-25 19:26:31 -07001948
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001949 return skb->len;
Jesse Grossccb13522011-10-25 19:26:31 -07001950}
1951
Jesse Grossccb13522011-10-25 19:26:31 -07001952static struct genl_ops dp_vport_genl_ops[] = {
1953 { .cmd = OVS_VPORT_CMD_NEW,
1954 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1955 .policy = vport_policy,
1956 .doit = ovs_vport_cmd_new
1957 },
1958 { .cmd = OVS_VPORT_CMD_DEL,
1959 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1960 .policy = vport_policy,
1961 .doit = ovs_vport_cmd_del
1962 },
1963 { .cmd = OVS_VPORT_CMD_GET,
1964 .flags = 0, /* OK for unprivileged users. */
1965 .policy = vport_policy,
1966 .doit = ovs_vport_cmd_get,
1967 .dumpit = ovs_vport_cmd_dump
1968 },
1969 { .cmd = OVS_VPORT_CMD_SET,
1970 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1971 .policy = vport_policy,
1972 .doit = ovs_vport_cmd_set,
1973 },
1974};
1975
1976struct genl_family_and_ops {
1977 struct genl_family *family;
1978 struct genl_ops *ops;
1979 int n_ops;
1980 struct genl_multicast_group *group;
1981};
1982
1983static const struct genl_family_and_ops dp_genl_families[] = {
1984 { &dp_datapath_genl_family,
1985 dp_datapath_genl_ops, ARRAY_SIZE(dp_datapath_genl_ops),
1986 &ovs_dp_datapath_multicast_group },
1987 { &dp_vport_genl_family,
1988 dp_vport_genl_ops, ARRAY_SIZE(dp_vport_genl_ops),
1989 &ovs_dp_vport_multicast_group },
1990 { &dp_flow_genl_family,
1991 dp_flow_genl_ops, ARRAY_SIZE(dp_flow_genl_ops),
1992 &ovs_dp_flow_multicast_group },
1993 { &dp_packet_genl_family,
1994 dp_packet_genl_ops, ARRAY_SIZE(dp_packet_genl_ops),
1995 NULL },
1996};
1997
1998static void dp_unregister_genl(int n_families)
1999{
2000 int i;
2001
2002 for (i = 0; i < n_families; i++)
2003 genl_unregister_family(dp_genl_families[i].family);
2004}
2005
2006static int dp_register_genl(void)
2007{
2008 int n_registered;
2009 int err;
2010 int i;
2011
2012 n_registered = 0;
2013 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
2014 const struct genl_family_and_ops *f = &dp_genl_families[i];
2015
2016 err = genl_register_family_with_ops(f->family, f->ops,
2017 f->n_ops);
2018 if (err)
2019 goto error;
2020 n_registered++;
2021
2022 if (f->group) {
2023 err = genl_register_mc_group(f->family, f->group);
2024 if (err)
2025 goto error;
2026 }
2027 }
2028
2029 return 0;
2030
2031error:
2032 dp_unregister_genl(n_registered);
2033 return err;
2034}
2035
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002036static void rehash_flow_table(struct work_struct *work)
2037{
2038 struct datapath *dp;
2039 struct net *net;
2040
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002041 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002042 rtnl_lock();
2043 for_each_net(net) {
2044 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2045
2046 list_for_each_entry(dp, &ovs_net->dps, list_node) {
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002047 struct flow_table *old_table = ovsl_dereference(dp->table);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002048 struct flow_table *new_table;
2049
2050 new_table = ovs_flow_tbl_rehash(old_table);
2051 if (!IS_ERR(new_table)) {
2052 rcu_assign_pointer(dp->table, new_table);
2053 ovs_flow_tbl_deferred_destroy(old_table);
2054 }
2055 }
2056 }
2057 rtnl_unlock();
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002058 ovs_unlock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002059 schedule_delayed_work(&rehash_flow_wq, REHASH_FLOW_INTERVAL);
2060}
2061
2062static int __net_init ovs_init_net(struct net *net)
2063{
2064 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2065
2066 INIT_LIST_HEAD(&ovs_net->dps);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002067 INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002068 return 0;
2069}
2070
2071static void __net_exit ovs_exit_net(struct net *net)
2072{
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002073 struct datapath *dp, *dp_next;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002074 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002075
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002076 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002077 list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
2078 __dp_destroy(dp);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002079 ovs_unlock();
2080
2081 cancel_work_sync(&ovs_net->dp_notify_work);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002082}
2083
2084static struct pernet_operations ovs_net_ops = {
2085 .init = ovs_init_net,
2086 .exit = ovs_exit_net,
2087 .id = &ovs_net_id,
2088 .size = sizeof(struct ovs_net),
2089};
2090
Jesse Grossccb13522011-10-25 19:26:31 -07002091static int __init dp_init(void)
2092{
Jesse Grossccb13522011-10-25 19:26:31 -07002093 int err;
2094
YOSHIFUJI Hideaki / 吉藤英明3523b292013-01-09 07:19:55 +00002095 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
Jesse Grossccb13522011-10-25 19:26:31 -07002096
2097 pr_info("Open vSwitch switching datapath\n");
2098
2099 err = ovs_flow_init();
2100 if (err)
2101 goto error;
2102
2103 err = ovs_vport_init();
2104 if (err)
2105 goto error_flow_exit;
2106
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002107 err = register_pernet_device(&ovs_net_ops);
Jesse Grossccb13522011-10-25 19:26:31 -07002108 if (err)
2109 goto error_vport_exit;
2110
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002111 err = register_netdevice_notifier(&ovs_dp_device_notifier);
2112 if (err)
2113 goto error_netns_exit;
2114
Jesse Grossccb13522011-10-25 19:26:31 -07002115 err = dp_register_genl();
2116 if (err < 0)
2117 goto error_unreg_notifier;
2118
2119 schedule_delayed_work(&rehash_flow_wq, REHASH_FLOW_INTERVAL);
2120
2121 return 0;
2122
2123error_unreg_notifier:
2124 unregister_netdevice_notifier(&ovs_dp_device_notifier);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002125error_netns_exit:
2126 unregister_pernet_device(&ovs_net_ops);
Jesse Grossccb13522011-10-25 19:26:31 -07002127error_vport_exit:
2128 ovs_vport_exit();
2129error_flow_exit:
2130 ovs_flow_exit();
2131error:
2132 return err;
2133}
2134
2135static void dp_cleanup(void)
2136{
2137 cancel_delayed_work_sync(&rehash_flow_wq);
Jesse Grossccb13522011-10-25 19:26:31 -07002138 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
2139 unregister_netdevice_notifier(&ovs_dp_device_notifier);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002140 unregister_pernet_device(&ovs_net_ops);
2141 rcu_barrier();
Jesse Grossccb13522011-10-25 19:26:31 -07002142 ovs_vport_exit();
2143 ovs_flow_exit();
2144}
2145
2146module_init(dp_init);
2147module_exit(dp_cleanup);
2148
2149MODULE_DESCRIPTION("Open vSwitch switching datapath");
2150MODULE_LICENSE("GPL");