blob: cae1062f94ba28111e8b17ec2356e4dd0839efda [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>
47#include <linux/openvswitch.h>
48#include <linux/rculist.h>
49#include <linux/dmi.h>
50#include <linux/workqueue.h>
51#include <net/genetlink.h>
Pravin B Shelar46df7b82012-02-22 19:58:59 -080052#include <net/net_namespace.h>
53#include <net/netns/generic.h>
Jesse Grossccb13522011-10-25 19:26:31 -070054
55#include "datapath.h"
56#include "flow.h"
57#include "vport-internal_dev.h"
58
59/**
Pravin B Shelar46df7b82012-02-22 19:58:59 -080060 * struct ovs_net - Per net-namespace data for ovs.
61 * @dps: List of datapaths to enable dumping them all out.
62 * Protected by genl_mutex.
63 */
64struct ovs_net {
65 struct list_head dps;
66};
67
68static int ovs_net_id __read_mostly;
69
70#define REHASH_FLOW_INTERVAL (10 * 60 * HZ)
71static void rehash_flow_table(struct work_struct *work);
72static DECLARE_DELAYED_WORK(rehash_flow_wq, rehash_flow_table);
73
74/**
Jesse Grossccb13522011-10-25 19:26:31 -070075 * DOC: Locking:
76 *
77 * Writes to device state (add/remove datapath, port, set operations on vports,
78 * etc.) are protected by RTNL.
79 *
80 * Writes to other state (flow table modifications, set miscellaneous datapath
81 * parameters, etc.) are protected by genl_mutex. The RTNL lock nests inside
82 * genl_mutex.
83 *
84 * Reads are protected by RCU.
85 *
86 * There are a few special cases (mostly stats) that have their own
87 * synchronization but they nest under all of above and don't interact with
88 * each other.
89 */
90
Jesse Grossccb13522011-10-25 19:26:31 -070091static struct vport *new_vport(const struct vport_parms *);
Pravin B Shelar46df7b82012-02-22 19:58:59 -080092static int queue_gso_packets(struct net *, int dp_ifindex, struct sk_buff *,
Jesse Grossccb13522011-10-25 19:26:31 -070093 const struct dp_upcall_info *);
Pravin B Shelar46df7b82012-02-22 19:58:59 -080094static int queue_userspace_packet(struct net *, int dp_ifindex,
95 struct sk_buff *,
Jesse Grossccb13522011-10-25 19:26:31 -070096 const struct dp_upcall_info *);
97
98/* Must be called with rcu_read_lock, genl_mutex, or RTNL lock. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -080099static struct datapath *get_dp(struct net *net, int dp_ifindex)
Jesse Grossccb13522011-10-25 19:26:31 -0700100{
101 struct datapath *dp = NULL;
102 struct net_device *dev;
103
104 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800105 dev = dev_get_by_index_rcu(net, dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -0700106 if (dev) {
107 struct vport *vport = ovs_internal_dev_get_vport(dev);
108 if (vport)
109 dp = vport->dp;
110 }
111 rcu_read_unlock();
112
113 return dp;
114}
115
116/* Must be called with rcu_read_lock or RTNL lock. */
117const char *ovs_dp_name(const struct datapath *dp)
118{
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700119 struct vport *vport = ovs_vport_rtnl_rcu(dp, OVSP_LOCAL);
Jesse Grossccb13522011-10-25 19:26:31 -0700120 return vport->ops->get_name(vport);
121}
122
123static int get_dpifindex(struct datapath *dp)
124{
125 struct vport *local;
126 int ifindex;
127
128 rcu_read_lock();
129
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700130 local = ovs_vport_rcu(dp, OVSP_LOCAL);
Jesse Grossccb13522011-10-25 19:26:31 -0700131 if (local)
132 ifindex = local->ops->get_ifindex(local);
133 else
134 ifindex = 0;
135
136 rcu_read_unlock();
137
138 return ifindex;
139}
140
141static void destroy_dp_rcu(struct rcu_head *rcu)
142{
143 struct datapath *dp = container_of(rcu, struct datapath, rcu);
144
145 ovs_flow_tbl_destroy((__force struct flow_table *)dp->table);
146 free_percpu(dp->stats_percpu);
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800147 release_net(ovs_dp_get_net(dp));
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700148 kfree(dp->ports);
Jesse Grossccb13522011-10-25 19:26:31 -0700149 kfree(dp);
150}
151
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700152static struct hlist_head *vport_hash_bucket(const struct datapath *dp,
153 u16 port_no)
154{
155 return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)];
156}
157
158struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no)
159{
160 struct vport *vport;
161 struct hlist_node *n;
162 struct hlist_head *head;
163
164 head = vport_hash_bucket(dp, port_no);
165 hlist_for_each_entry_rcu(vport, n, head, dp_hash_node) {
166 if (vport->port_no == port_no)
167 return vport;
168 }
169 return NULL;
170}
171
Jesse Grossccb13522011-10-25 19:26:31 -0700172/* Called with RTNL lock and genl_lock. */
173static struct vport *new_vport(const struct vport_parms *parms)
174{
175 struct vport *vport;
176
177 vport = ovs_vport_add(parms);
178 if (!IS_ERR(vport)) {
179 struct datapath *dp = parms->dp;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700180 struct hlist_head *head = vport_hash_bucket(dp, vport->port_no);
Jesse Grossccb13522011-10-25 19:26:31 -0700181
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700182 hlist_add_head_rcu(&vport->dp_hash_node, head);
Jesse Grossccb13522011-10-25 19:26:31 -0700183 }
184
185 return vport;
186}
187
188/* Called with RTNL lock. */
189void ovs_dp_detach_port(struct vport *p)
190{
191 ASSERT_RTNL();
192
193 /* First drop references to device. */
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700194 hlist_del_rcu(&p->dp_hash_node);
Jesse Grossccb13522011-10-25 19:26:31 -0700195
196 /* Then destroy it. */
197 ovs_vport_del(p);
198}
199
200/* Must be called with rcu_read_lock. */
201void ovs_dp_process_received_packet(struct vport *p, struct sk_buff *skb)
202{
203 struct datapath *dp = p->dp;
204 struct sw_flow *flow;
205 struct dp_stats_percpu *stats;
206 struct sw_flow_key key;
207 u64 *stats_counter;
208 int error;
209 int key_len;
210
Shan Wei404f2f12012-11-13 09:52:25 +0800211 stats = this_cpu_ptr(dp->stats_percpu);
Jesse Grossccb13522011-10-25 19:26:31 -0700212
213 /* Extract flow from 'skb' into 'key'. */
214 error = ovs_flow_extract(skb, p->port_no, &key, &key_len);
215 if (unlikely(error)) {
216 kfree_skb(skb);
217 return;
218 }
219
220 /* Look up flow. */
221 flow = ovs_flow_tbl_lookup(rcu_dereference(dp->table), &key, key_len);
222 if (unlikely(!flow)) {
223 struct dp_upcall_info upcall;
224
225 upcall.cmd = OVS_PACKET_CMD_MISS;
226 upcall.key = &key;
227 upcall.userdata = NULL;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000228 upcall.portid = p->upcall_portid;
Jesse Grossccb13522011-10-25 19:26:31 -0700229 ovs_dp_upcall(dp, skb, &upcall);
230 consume_skb(skb);
231 stats_counter = &stats->n_missed;
232 goto out;
233 }
234
235 OVS_CB(skb)->flow = flow;
236
237 stats_counter = &stats->n_hit;
238 ovs_flow_used(OVS_CB(skb)->flow, skb);
239 ovs_execute_actions(dp, skb);
240
241out:
242 /* Update datapath statistics. */
243 u64_stats_update_begin(&stats->sync);
244 (*stats_counter)++;
245 u64_stats_update_end(&stats->sync);
246}
247
248static struct genl_family dp_packet_genl_family = {
249 .id = GENL_ID_GENERATE,
250 .hdrsize = sizeof(struct ovs_header),
251 .name = OVS_PACKET_FAMILY,
252 .version = OVS_PACKET_VERSION,
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800253 .maxattr = OVS_PACKET_ATTR_MAX,
254 .netnsok = true
Jesse Grossccb13522011-10-25 19:26:31 -0700255};
256
257int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800258 const struct dp_upcall_info *upcall_info)
Jesse Grossccb13522011-10-25 19:26:31 -0700259{
260 struct dp_stats_percpu *stats;
261 int dp_ifindex;
262 int err;
263
Eric W. Biederman15e47302012-09-07 20:12:54 +0000264 if (upcall_info->portid == 0) {
Jesse Grossccb13522011-10-25 19:26:31 -0700265 err = -ENOTCONN;
266 goto err;
267 }
268
269 dp_ifindex = get_dpifindex(dp);
270 if (!dp_ifindex) {
271 err = -ENODEV;
272 goto err;
273 }
274
275 if (!skb_is_gso(skb))
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800276 err = queue_userspace_packet(ovs_dp_get_net(dp), dp_ifindex, skb, upcall_info);
Jesse Grossccb13522011-10-25 19:26:31 -0700277 else
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800278 err = queue_gso_packets(ovs_dp_get_net(dp), dp_ifindex, skb, upcall_info);
Jesse Grossccb13522011-10-25 19:26:31 -0700279 if (err)
280 goto err;
281
282 return 0;
283
284err:
Shan Wei404f2f12012-11-13 09:52:25 +0800285 stats = this_cpu_ptr(dp->stats_percpu);
Jesse Grossccb13522011-10-25 19:26:31 -0700286
287 u64_stats_update_begin(&stats->sync);
288 stats->n_lost++;
289 u64_stats_update_end(&stats->sync);
290
291 return err;
292}
293
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800294static int queue_gso_packets(struct net *net, int dp_ifindex,
295 struct sk_buff *skb,
Jesse Grossccb13522011-10-25 19:26:31 -0700296 const struct dp_upcall_info *upcall_info)
297{
Ben Pfaffa1b5d0d2012-07-20 14:47:54 -0700298 unsigned short gso_type = skb_shinfo(skb)->gso_type;
Jesse Grossccb13522011-10-25 19:26:31 -0700299 struct dp_upcall_info later_info;
300 struct sw_flow_key later_key;
301 struct sk_buff *segs, *nskb;
302 int err;
303
304 segs = skb_gso_segment(skb, NETIF_F_SG | NETIF_F_HW_CSUM);
Pravin B Shelar92e5dfc2012-07-20 14:46:29 -0700305 if (IS_ERR(segs))
306 return PTR_ERR(segs);
Jesse Grossccb13522011-10-25 19:26:31 -0700307
308 /* Queue all of the segments. */
309 skb = segs;
310 do {
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800311 err = queue_userspace_packet(net, dp_ifindex, skb, upcall_info);
Jesse Grossccb13522011-10-25 19:26:31 -0700312 if (err)
313 break;
314
Ben Pfaffa1b5d0d2012-07-20 14:47:54 -0700315 if (skb == segs && gso_type & SKB_GSO_UDP) {
Jesse Grossccb13522011-10-25 19:26:31 -0700316 /* The initial flow key extracted by ovs_flow_extract()
317 * in this case is for a first fragment, so we need to
318 * properly mark later fragments.
319 */
320 later_key = *upcall_info->key;
321 later_key.ip.frag = OVS_FRAG_TYPE_LATER;
322
323 later_info = *upcall_info;
324 later_info.key = &later_key;
325 upcall_info = &later_info;
326 }
327 } while ((skb = skb->next));
328
329 /* Free all of the segments. */
330 skb = segs;
331 do {
332 nskb = skb->next;
333 if (err)
334 kfree_skb(skb);
335 else
336 consume_skb(skb);
337 } while ((skb = nskb));
338 return err;
339}
340
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800341static int queue_userspace_packet(struct net *net, int dp_ifindex,
342 struct sk_buff *skb,
Jesse Grossccb13522011-10-25 19:26:31 -0700343 const struct dp_upcall_info *upcall_info)
344{
345 struct ovs_header *upcall;
346 struct sk_buff *nskb = NULL;
347 struct sk_buff *user_skb; /* to be queued to userspace */
348 struct nlattr *nla;
349 unsigned int len;
350 int err;
351
352 if (vlan_tx_tag_present(skb)) {
353 nskb = skb_clone(skb, GFP_ATOMIC);
354 if (!nskb)
355 return -ENOMEM;
356
357 nskb = __vlan_put_tag(nskb, vlan_tx_tag_get(nskb));
Dan Carpenter8aa51d62012-05-13 08:44:18 +0000358 if (!nskb)
Jesse Grossccb13522011-10-25 19:26:31 -0700359 return -ENOMEM;
360
361 nskb->vlan_tci = 0;
362 skb = nskb;
363 }
364
365 if (nla_attr_size(skb->len) > USHRT_MAX) {
366 err = -EFBIG;
367 goto out;
368 }
369
370 len = sizeof(struct ovs_header);
371 len += nla_total_size(skb->len);
372 len += nla_total_size(FLOW_BUFSIZE);
373 if (upcall_info->cmd == OVS_PACKET_CMD_ACTION)
374 len += nla_total_size(8);
375
376 user_skb = genlmsg_new(len, GFP_ATOMIC);
377 if (!user_skb) {
378 err = -ENOMEM;
379 goto out;
380 }
381
382 upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
383 0, upcall_info->cmd);
384 upcall->dp_ifindex = dp_ifindex;
385
386 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
387 ovs_flow_to_nlattrs(upcall_info->key, user_skb);
388 nla_nest_end(user_skb, nla);
389
390 if (upcall_info->userdata)
391 nla_put_u64(user_skb, OVS_PACKET_ATTR_USERDATA,
392 nla_get_u64(upcall_info->userdata));
393
394 nla = __nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, skb->len);
395
396 skb_copy_and_csum_dev(skb, nla_data(nla));
397
Rich Lanea15ff762013-02-15 11:07:43 -0800398 genlmsg_end(user_skb, upcall);
Eric W. Biederman15e47302012-09-07 20:12:54 +0000399 err = genlmsg_unicast(net, user_skb, upcall_info->portid);
Jesse Grossccb13522011-10-25 19:26:31 -0700400
401out:
402 kfree_skb(nskb);
403 return err;
404}
405
406/* Called with genl_mutex. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800407static int flush_flows(struct datapath *dp)
Jesse Grossccb13522011-10-25 19:26:31 -0700408{
409 struct flow_table *old_table;
410 struct flow_table *new_table;
Jesse Grossccb13522011-10-25 19:26:31 -0700411
412 old_table = genl_dereference(dp->table);
413 new_table = ovs_flow_tbl_alloc(TBL_MIN_BUCKETS);
414 if (!new_table)
415 return -ENOMEM;
416
417 rcu_assign_pointer(dp->table, new_table);
418
419 ovs_flow_tbl_deferred_destroy(old_table);
420 return 0;
421}
422
423static int validate_actions(const struct nlattr *attr,
424 const struct sw_flow_key *key, int depth);
425
426static int validate_sample(const struct nlattr *attr,
427 const struct sw_flow_key *key, int depth)
428{
429 const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
430 const struct nlattr *probability, *actions;
431 const struct nlattr *a;
432 int rem;
433
434 memset(attrs, 0, sizeof(attrs));
435 nla_for_each_nested(a, attr, rem) {
436 int type = nla_type(a);
437 if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
438 return -EINVAL;
439 attrs[type] = a;
440 }
441 if (rem)
442 return -EINVAL;
443
444 probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
445 if (!probability || nla_len(probability) != sizeof(u32))
446 return -EINVAL;
447
448 actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
449 if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
450 return -EINVAL;
451 return validate_actions(actions, key, depth + 1);
452}
453
Pravin B Shelar072ae632012-05-07 17:21:53 -0700454static int validate_tp_port(const struct sw_flow_key *flow_key)
455{
456 if (flow_key->eth.type == htons(ETH_P_IP)) {
Jesse Gross41853922012-08-06 15:49:47 -0700457 if (flow_key->ipv4.tp.src || flow_key->ipv4.tp.dst)
Pravin B Shelar072ae632012-05-07 17:21:53 -0700458 return 0;
459 } else if (flow_key->eth.type == htons(ETH_P_IPV6)) {
Jesse Gross41853922012-08-06 15:49:47 -0700460 if (flow_key->ipv6.tp.src || flow_key->ipv6.tp.dst)
Pravin B Shelar072ae632012-05-07 17:21:53 -0700461 return 0;
462 }
463
464 return -EINVAL;
465}
466
Jesse Grossccb13522011-10-25 19:26:31 -0700467static int validate_set(const struct nlattr *a,
468 const struct sw_flow_key *flow_key)
469{
470 const struct nlattr *ovs_key = nla_data(a);
471 int key_type = nla_type(ovs_key);
472
473 /* There can be only one key in a action */
474 if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
475 return -EINVAL;
476
477 if (key_type > OVS_KEY_ATTR_MAX ||
478 nla_len(ovs_key) != ovs_key_lens[key_type])
479 return -EINVAL;
480
481 switch (key_type) {
482 const struct ovs_key_ipv4 *ipv4_key;
Ansis Atteka3fdbd1c2012-11-13 15:44:14 -0800483 const struct ovs_key_ipv6 *ipv6_key;
Jesse Grossccb13522011-10-25 19:26:31 -0700484
485 case OVS_KEY_ATTR_PRIORITY:
Ansis Atteka39c7caeb2012-11-26 11:24:11 -0800486 case OVS_KEY_ATTR_SKB_MARK:
Jesse Grossccb13522011-10-25 19:26:31 -0700487 case OVS_KEY_ATTR_ETHERNET:
488 break;
489
490 case OVS_KEY_ATTR_IPV4:
491 if (flow_key->eth.type != htons(ETH_P_IP))
492 return -EINVAL;
493
Jesse Gross41853922012-08-06 15:49:47 -0700494 if (!flow_key->ip.proto)
Jesse Grossccb13522011-10-25 19:26:31 -0700495 return -EINVAL;
496
497 ipv4_key = nla_data(ovs_key);
498 if (ipv4_key->ipv4_proto != flow_key->ip.proto)
499 return -EINVAL;
500
501 if (ipv4_key->ipv4_frag != flow_key->ip.frag)
502 return -EINVAL;
503
504 break;
505
Ansis Atteka3fdbd1c2012-11-13 15:44:14 -0800506 case OVS_KEY_ATTR_IPV6:
507 if (flow_key->eth.type != htons(ETH_P_IPV6))
508 return -EINVAL;
509
510 if (!flow_key->ip.proto)
511 return -EINVAL;
512
513 ipv6_key = nla_data(ovs_key);
514 if (ipv6_key->ipv6_proto != flow_key->ip.proto)
515 return -EINVAL;
516
517 if (ipv6_key->ipv6_frag != flow_key->ip.frag)
518 return -EINVAL;
519
520 if (ntohl(ipv6_key->ipv6_label) & 0xFFF00000)
521 return -EINVAL;
522
523 break;
524
Jesse Grossccb13522011-10-25 19:26:31 -0700525 case OVS_KEY_ATTR_TCP:
526 if (flow_key->ip.proto != IPPROTO_TCP)
527 return -EINVAL;
528
Pravin B Shelar072ae632012-05-07 17:21:53 -0700529 return validate_tp_port(flow_key);
Jesse Grossccb13522011-10-25 19:26:31 -0700530
531 case OVS_KEY_ATTR_UDP:
532 if (flow_key->ip.proto != IPPROTO_UDP)
533 return -EINVAL;
534
Pravin B Shelar072ae632012-05-07 17:21:53 -0700535 return validate_tp_port(flow_key);
Jesse Grossccb13522011-10-25 19:26:31 -0700536
537 default:
538 return -EINVAL;
539 }
540
541 return 0;
542}
543
544static int validate_userspace(const struct nlattr *attr)
545{
546 static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
547 [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
548 [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_U64 },
549 };
550 struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
551 int error;
552
553 error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX,
554 attr, userspace_policy);
555 if (error)
556 return error;
557
558 if (!a[OVS_USERSPACE_ATTR_PID] ||
559 !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
560 return -EINVAL;
561
562 return 0;
563}
564
565static int validate_actions(const struct nlattr *attr,
566 const struct sw_flow_key *key, int depth)
567{
568 const struct nlattr *a;
569 int rem, err;
570
571 if (depth >= SAMPLE_ACTION_DEPTH)
572 return -EOVERFLOW;
573
574 nla_for_each_nested(a, attr, rem) {
575 /* Expected argument lengths, (u32)-1 for variable length. */
576 static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
577 [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
578 [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
579 [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
580 [OVS_ACTION_ATTR_POP_VLAN] = 0,
581 [OVS_ACTION_ATTR_SET] = (u32)-1,
582 [OVS_ACTION_ATTR_SAMPLE] = (u32)-1
583 };
584 const struct ovs_action_push_vlan *vlan;
585 int type = nla_type(a);
586
587 if (type > OVS_ACTION_ATTR_MAX ||
588 (action_lens[type] != nla_len(a) &&
589 action_lens[type] != (u32)-1))
590 return -EINVAL;
591
592 switch (type) {
593 case OVS_ACTION_ATTR_UNSPEC:
594 return -EINVAL;
595
596 case OVS_ACTION_ATTR_USERSPACE:
597 err = validate_userspace(a);
598 if (err)
599 return err;
600 break;
601
602 case OVS_ACTION_ATTR_OUTPUT:
603 if (nla_get_u32(a) >= DP_MAX_PORTS)
604 return -EINVAL;
605 break;
606
607
608 case OVS_ACTION_ATTR_POP_VLAN:
609 break;
610
611 case OVS_ACTION_ATTR_PUSH_VLAN:
612 vlan = nla_data(a);
613 if (vlan->vlan_tpid != htons(ETH_P_8021Q))
614 return -EINVAL;
615 if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
616 return -EINVAL;
617 break;
618
619 case OVS_ACTION_ATTR_SET:
620 err = validate_set(a, key);
621 if (err)
622 return err;
623 break;
624
625 case OVS_ACTION_ATTR_SAMPLE:
626 err = validate_sample(a, key, depth);
627 if (err)
628 return err;
629 break;
630
631 default:
632 return -EINVAL;
633 }
634 }
635
636 if (rem > 0)
637 return -EINVAL;
638
639 return 0;
640}
641
642static void clear_stats(struct sw_flow *flow)
643{
644 flow->used = 0;
645 flow->tcp_flags = 0;
646 flow->packet_count = 0;
647 flow->byte_count = 0;
648}
649
650static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
651{
652 struct ovs_header *ovs_header = info->userhdr;
653 struct nlattr **a = info->attrs;
654 struct sw_flow_actions *acts;
655 struct sk_buff *packet;
656 struct sw_flow *flow;
657 struct datapath *dp;
658 struct ethhdr *eth;
659 int len;
660 int err;
661 int key_len;
662
663 err = -EINVAL;
664 if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
665 !a[OVS_PACKET_ATTR_ACTIONS] ||
666 nla_len(a[OVS_PACKET_ATTR_PACKET]) < ETH_HLEN)
667 goto err;
668
669 len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
670 packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
671 err = -ENOMEM;
672 if (!packet)
673 goto err;
674 skb_reserve(packet, NET_IP_ALIGN);
675
676 memcpy(__skb_put(packet, len), nla_data(a[OVS_PACKET_ATTR_PACKET]), len);
677
678 skb_reset_mac_header(packet);
679 eth = eth_hdr(packet);
680
681 /* Normally, setting the skb 'protocol' field would be handled by a
682 * call to eth_type_trans(), but it assumes there's a sending
683 * device, which we may not have. */
684 if (ntohs(eth->h_proto) >= 1536)
685 packet->protocol = eth->h_proto;
686 else
687 packet->protocol = htons(ETH_P_802_2);
688
689 /* Build an sw_flow for sending this packet. */
690 flow = ovs_flow_alloc();
691 err = PTR_ERR(flow);
692 if (IS_ERR(flow))
693 goto err_kfree_skb;
694
695 err = ovs_flow_extract(packet, -1, &flow->key, &key_len);
696 if (err)
697 goto err_flow_free;
698
699 err = ovs_flow_metadata_from_nlattrs(&flow->key.phy.priority,
Ansis Atteka39c7caeb2012-11-26 11:24:11 -0800700 &flow->key.phy.skb_mark,
Jesse Grossccb13522011-10-25 19:26:31 -0700701 &flow->key.phy.in_port,
702 a[OVS_PACKET_ATTR_KEY]);
703 if (err)
704 goto err_flow_free;
705
706 err = validate_actions(a[OVS_PACKET_ATTR_ACTIONS], &flow->key, 0);
707 if (err)
708 goto err_flow_free;
709
710 flow->hash = ovs_flow_hash(&flow->key, key_len);
711
712 acts = ovs_flow_actions_alloc(a[OVS_PACKET_ATTR_ACTIONS]);
713 err = PTR_ERR(acts);
714 if (IS_ERR(acts))
715 goto err_flow_free;
716 rcu_assign_pointer(flow->sf_acts, acts);
717
718 OVS_CB(packet)->flow = flow;
719 packet->priority = flow->key.phy.priority;
Ansis Atteka39c7caeb2012-11-26 11:24:11 -0800720 packet->mark = flow->key.phy.skb_mark;
Jesse Grossccb13522011-10-25 19:26:31 -0700721
722 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800723 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -0700724 err = -ENODEV;
725 if (!dp)
726 goto err_unlock;
727
728 local_bh_disable();
729 err = ovs_execute_actions(dp, packet);
730 local_bh_enable();
731 rcu_read_unlock();
732
733 ovs_flow_free(flow);
734 return err;
735
736err_unlock:
737 rcu_read_unlock();
738err_flow_free:
739 ovs_flow_free(flow);
740err_kfree_skb:
741 kfree_skb(packet);
742err:
743 return err;
744}
745
746static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
747 [OVS_PACKET_ATTR_PACKET] = { .type = NLA_UNSPEC },
748 [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
749 [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
750};
751
752static struct genl_ops dp_packet_genl_ops[] = {
753 { .cmd = OVS_PACKET_CMD_EXECUTE,
754 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
755 .policy = packet_policy,
756 .doit = ovs_packet_cmd_execute
757 }
758};
759
760static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats)
761{
762 int i;
763 struct flow_table *table = genl_dereference(dp->table);
764
765 stats->n_flows = ovs_flow_tbl_count(table);
766
767 stats->n_hit = stats->n_missed = stats->n_lost = 0;
768 for_each_possible_cpu(i) {
769 const struct dp_stats_percpu *percpu_stats;
770 struct dp_stats_percpu local_stats;
771 unsigned int start;
772
773 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
774
775 do {
776 start = u64_stats_fetch_begin_bh(&percpu_stats->sync);
777 local_stats = *percpu_stats;
778 } while (u64_stats_fetch_retry_bh(&percpu_stats->sync, start));
779
780 stats->n_hit += local_stats.n_hit;
781 stats->n_missed += local_stats.n_missed;
782 stats->n_lost += local_stats.n_lost;
783 }
784}
785
786static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
787 [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
788 [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
789 [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
790};
791
792static struct genl_family dp_flow_genl_family = {
793 .id = GENL_ID_GENERATE,
794 .hdrsize = sizeof(struct ovs_header),
795 .name = OVS_FLOW_FAMILY,
796 .version = OVS_FLOW_VERSION,
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800797 .maxattr = OVS_FLOW_ATTR_MAX,
798 .netnsok = true
Jesse Grossccb13522011-10-25 19:26:31 -0700799};
800
801static struct genl_multicast_group ovs_dp_flow_multicast_group = {
802 .name = OVS_FLOW_MCGROUP
803};
804
805/* Called with genl_lock. */
806static int ovs_flow_cmd_fill_info(struct sw_flow *flow, struct datapath *dp,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000807 struct sk_buff *skb, u32 portid,
Jesse Grossccb13522011-10-25 19:26:31 -0700808 u32 seq, u32 flags, u8 cmd)
809{
810 const int skb_orig_len = skb->len;
811 const struct sw_flow_actions *sf_acts;
812 struct ovs_flow_stats stats;
813 struct ovs_header *ovs_header;
814 struct nlattr *nla;
815 unsigned long used;
816 u8 tcp_flags;
817 int err;
818
819 sf_acts = rcu_dereference_protected(flow->sf_acts,
820 lockdep_genl_is_held());
821
Eric W. Biederman15e47302012-09-07 20:12:54 +0000822 ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family, flags, cmd);
Jesse Grossccb13522011-10-25 19:26:31 -0700823 if (!ovs_header)
824 return -EMSGSIZE;
825
826 ovs_header->dp_ifindex = get_dpifindex(dp);
827
828 nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
829 if (!nla)
830 goto nla_put_failure;
831 err = ovs_flow_to_nlattrs(&flow->key, skb);
832 if (err)
833 goto error;
834 nla_nest_end(skb, nla);
835
836 spin_lock_bh(&flow->lock);
837 used = flow->used;
838 stats.n_packets = flow->packet_count;
839 stats.n_bytes = flow->byte_count;
840 tcp_flags = flow->tcp_flags;
841 spin_unlock_bh(&flow->lock);
842
David S. Miller028d6a62012-03-29 23:20:48 -0400843 if (used &&
844 nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
845 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -0700846
David S. Miller028d6a62012-03-29 23:20:48 -0400847 if (stats.n_packets &&
848 nla_put(skb, OVS_FLOW_ATTR_STATS,
849 sizeof(struct ovs_flow_stats), &stats))
850 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -0700851
David S. Miller028d6a62012-03-29 23:20:48 -0400852 if (tcp_flags &&
853 nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, tcp_flags))
854 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -0700855
856 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
857 * this is the first flow to be dumped into 'skb'. This is unusual for
858 * Netlink but individual action lists can be longer than
859 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
860 * The userspace caller can always fetch the actions separately if it
861 * really wants them. (Most userspace callers in fact don't care.)
862 *
863 * This can only fail for dump operations because the skb is always
864 * properly sized for single flows.
865 */
866 err = nla_put(skb, OVS_FLOW_ATTR_ACTIONS, sf_acts->actions_len,
867 sf_acts->actions);
868 if (err < 0 && skb_orig_len)
869 goto error;
870
871 return genlmsg_end(skb, ovs_header);
872
873nla_put_failure:
874 err = -EMSGSIZE;
875error:
876 genlmsg_cancel(skb, ovs_header);
877 return err;
878}
879
880static struct sk_buff *ovs_flow_cmd_alloc_info(struct sw_flow *flow)
881{
882 const struct sw_flow_actions *sf_acts;
883 int len;
884
885 sf_acts = rcu_dereference_protected(flow->sf_acts,
886 lockdep_genl_is_held());
887
888 /* OVS_FLOW_ATTR_KEY */
889 len = nla_total_size(FLOW_BUFSIZE);
890 /* OVS_FLOW_ATTR_ACTIONS */
891 len += nla_total_size(sf_acts->actions_len);
892 /* OVS_FLOW_ATTR_STATS */
893 len += nla_total_size(sizeof(struct ovs_flow_stats));
894 /* OVS_FLOW_ATTR_TCP_FLAGS */
895 len += nla_total_size(1);
896 /* OVS_FLOW_ATTR_USED */
897 len += nla_total_size(8);
898
899 len += NLMSG_ALIGN(sizeof(struct ovs_header));
900
901 return genlmsg_new(len, GFP_KERNEL);
902}
903
904static struct sk_buff *ovs_flow_cmd_build_info(struct sw_flow *flow,
905 struct datapath *dp,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000906 u32 portid, u32 seq, u8 cmd)
Jesse Grossccb13522011-10-25 19:26:31 -0700907{
908 struct sk_buff *skb;
909 int retval;
910
911 skb = ovs_flow_cmd_alloc_info(flow);
912 if (!skb)
913 return ERR_PTR(-ENOMEM);
914
Eric W. Biederman15e47302012-09-07 20:12:54 +0000915 retval = ovs_flow_cmd_fill_info(flow, dp, skb, portid, seq, 0, cmd);
Jesse Grossccb13522011-10-25 19:26:31 -0700916 BUG_ON(retval < 0);
917 return skb;
918}
919
920static int ovs_flow_cmd_new_or_set(struct sk_buff *skb, struct genl_info *info)
921{
922 struct nlattr **a = info->attrs;
923 struct ovs_header *ovs_header = info->userhdr;
924 struct sw_flow_key key;
925 struct sw_flow *flow;
926 struct sk_buff *reply;
927 struct datapath *dp;
928 struct flow_table *table;
929 int error;
930 int key_len;
931
932 /* Extract key. */
933 error = -EINVAL;
934 if (!a[OVS_FLOW_ATTR_KEY])
935 goto error;
936 error = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
937 if (error)
938 goto error;
939
940 /* Validate actions. */
941 if (a[OVS_FLOW_ATTR_ACTIONS]) {
942 error = validate_actions(a[OVS_FLOW_ATTR_ACTIONS], &key, 0);
943 if (error)
944 goto error;
945 } else if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW) {
946 error = -EINVAL;
947 goto error;
948 }
949
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800950 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -0700951 error = -ENODEV;
952 if (!dp)
953 goto error;
954
955 table = genl_dereference(dp->table);
956 flow = ovs_flow_tbl_lookup(table, &key, key_len);
957 if (!flow) {
958 struct sw_flow_actions *acts;
959
960 /* Bail out if we're not allowed to create a new flow. */
961 error = -ENOENT;
962 if (info->genlhdr->cmd == OVS_FLOW_CMD_SET)
963 goto error;
964
965 /* Expand table, if necessary, to make room. */
966 if (ovs_flow_tbl_need_to_expand(table)) {
967 struct flow_table *new_table;
968
969 new_table = ovs_flow_tbl_expand(table);
970 if (!IS_ERR(new_table)) {
971 rcu_assign_pointer(dp->table, new_table);
972 ovs_flow_tbl_deferred_destroy(table);
973 table = genl_dereference(dp->table);
974 }
975 }
976
977 /* Allocate flow. */
978 flow = ovs_flow_alloc();
979 if (IS_ERR(flow)) {
980 error = PTR_ERR(flow);
981 goto error;
982 }
983 flow->key = key;
984 clear_stats(flow);
985
986 /* Obtain actions. */
987 acts = ovs_flow_actions_alloc(a[OVS_FLOW_ATTR_ACTIONS]);
988 error = PTR_ERR(acts);
989 if (IS_ERR(acts))
990 goto error_free_flow;
991 rcu_assign_pointer(flow->sf_acts, acts);
992
993 /* Put flow in bucket. */
994 flow->hash = ovs_flow_hash(&key, key_len);
995 ovs_flow_tbl_insert(table, flow);
996
Eric W. Biederman15e47302012-09-07 20:12:54 +0000997 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
Jesse Grossccb13522011-10-25 19:26:31 -0700998 info->snd_seq,
999 OVS_FLOW_CMD_NEW);
1000 } else {
1001 /* We found a matching flow. */
1002 struct sw_flow_actions *old_acts;
1003 struct nlattr *acts_attrs;
1004
1005 /* Bail out if we're not allowed to modify an existing flow.
1006 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
1007 * because Generic Netlink treats the latter as a dump
1008 * request. We also accept NLM_F_EXCL in case that bug ever
1009 * gets fixed.
1010 */
1011 error = -EEXIST;
1012 if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW &&
1013 info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL))
1014 goto error;
1015
1016 /* Update actions. */
1017 old_acts = rcu_dereference_protected(flow->sf_acts,
1018 lockdep_genl_is_held());
1019 acts_attrs = a[OVS_FLOW_ATTR_ACTIONS];
1020 if (acts_attrs &&
1021 (old_acts->actions_len != nla_len(acts_attrs) ||
1022 memcmp(old_acts->actions, nla_data(acts_attrs),
1023 old_acts->actions_len))) {
1024 struct sw_flow_actions *new_acts;
1025
1026 new_acts = ovs_flow_actions_alloc(acts_attrs);
1027 error = PTR_ERR(new_acts);
1028 if (IS_ERR(new_acts))
1029 goto error;
1030
1031 rcu_assign_pointer(flow->sf_acts, new_acts);
1032 ovs_flow_deferred_free_acts(old_acts);
1033 }
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, OVS_FLOW_CMD_NEW);
1037
1038 /* Clear stats. */
1039 if (a[OVS_FLOW_ATTR_CLEAR]) {
1040 spin_lock_bh(&flow->lock);
1041 clear_stats(flow);
1042 spin_unlock_bh(&flow->lock);
1043 }
1044 }
1045
1046 if (!IS_ERR(reply))
Eric W. Biederman15e47302012-09-07 20:12:54 +00001047 genl_notify(reply, genl_info_net(info), info->snd_portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001048 ovs_dp_flow_multicast_group.id, info->nlhdr,
1049 GFP_KERNEL);
1050 else
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001051 netlink_set_err(sock_net(skb->sk)->genl_sock, 0,
Jesse Grossccb13522011-10-25 19:26:31 -07001052 ovs_dp_flow_multicast_group.id, PTR_ERR(reply));
1053 return 0;
1054
1055error_free_flow:
1056 ovs_flow_free(flow);
1057error:
1058 return error;
1059}
1060
1061static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1062{
1063 struct nlattr **a = info->attrs;
1064 struct ovs_header *ovs_header = info->userhdr;
1065 struct sw_flow_key key;
1066 struct sk_buff *reply;
1067 struct sw_flow *flow;
1068 struct datapath *dp;
1069 struct flow_table *table;
1070 int err;
1071 int key_len;
1072
1073 if (!a[OVS_FLOW_ATTR_KEY])
1074 return -EINVAL;
1075 err = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
1076 if (err)
1077 return err;
1078
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001079 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001080 if (!dp)
1081 return -ENODEV;
1082
1083 table = genl_dereference(dp->table);
1084 flow = ovs_flow_tbl_lookup(table, &key, key_len);
1085 if (!flow)
1086 return -ENOENT;
1087
Eric W. Biederman15e47302012-09-07 20:12:54 +00001088 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001089 info->snd_seq, OVS_FLOW_CMD_NEW);
1090 if (IS_ERR(reply))
1091 return PTR_ERR(reply);
1092
1093 return genlmsg_reply(reply, info);
1094}
1095
1096static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1097{
1098 struct nlattr **a = info->attrs;
1099 struct ovs_header *ovs_header = info->userhdr;
1100 struct sw_flow_key key;
1101 struct sk_buff *reply;
1102 struct sw_flow *flow;
1103 struct datapath *dp;
1104 struct flow_table *table;
1105 int err;
1106 int key_len;
1107
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001108 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1109 if (!dp)
1110 return -ENODEV;
1111
Jesse Grossccb13522011-10-25 19:26:31 -07001112 if (!a[OVS_FLOW_ATTR_KEY])
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001113 return flush_flows(dp);
1114
Jesse Grossccb13522011-10-25 19:26:31 -07001115 err = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
1116 if (err)
1117 return err;
1118
Jesse Grossccb13522011-10-25 19:26:31 -07001119 table = genl_dereference(dp->table);
1120 flow = ovs_flow_tbl_lookup(table, &key, key_len);
1121 if (!flow)
1122 return -ENOENT;
1123
1124 reply = ovs_flow_cmd_alloc_info(flow);
1125 if (!reply)
1126 return -ENOMEM;
1127
1128 ovs_flow_tbl_remove(table, flow);
1129
Eric W. Biederman15e47302012-09-07 20:12:54 +00001130 err = ovs_flow_cmd_fill_info(flow, dp, reply, info->snd_portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001131 info->snd_seq, 0, OVS_FLOW_CMD_DEL);
1132 BUG_ON(err < 0);
1133
1134 ovs_flow_deferred_free(flow);
1135
Eric W. Biederman15e47302012-09-07 20:12:54 +00001136 genl_notify(reply, genl_info_net(info), info->snd_portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001137 ovs_dp_flow_multicast_group.id, info->nlhdr, GFP_KERNEL);
1138 return 0;
1139}
1140
1141static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1142{
1143 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1144 struct datapath *dp;
1145 struct flow_table *table;
1146
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001147 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001148 if (!dp)
1149 return -ENODEV;
1150
1151 table = genl_dereference(dp->table);
1152
1153 for (;;) {
1154 struct sw_flow *flow;
1155 u32 bucket, obj;
1156
1157 bucket = cb->args[0];
1158 obj = cb->args[1];
1159 flow = ovs_flow_tbl_next(table, &bucket, &obj);
1160 if (!flow)
1161 break;
1162
1163 if (ovs_flow_cmd_fill_info(flow, dp, skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001164 NETLINK_CB(cb->skb).portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001165 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1166 OVS_FLOW_CMD_NEW) < 0)
1167 break;
1168
1169 cb->args[0] = bucket;
1170 cb->args[1] = obj;
1171 }
1172 return skb->len;
1173}
1174
1175static struct genl_ops dp_flow_genl_ops[] = {
1176 { .cmd = OVS_FLOW_CMD_NEW,
1177 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1178 .policy = flow_policy,
1179 .doit = ovs_flow_cmd_new_or_set
1180 },
1181 { .cmd = OVS_FLOW_CMD_DEL,
1182 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1183 .policy = flow_policy,
1184 .doit = ovs_flow_cmd_del
1185 },
1186 { .cmd = OVS_FLOW_CMD_GET,
1187 .flags = 0, /* OK for unprivileged users. */
1188 .policy = flow_policy,
1189 .doit = ovs_flow_cmd_get,
1190 .dumpit = ovs_flow_cmd_dump
1191 },
1192 { .cmd = OVS_FLOW_CMD_SET,
1193 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1194 .policy = flow_policy,
1195 .doit = ovs_flow_cmd_new_or_set,
1196 },
1197};
1198
1199static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1200 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1201 [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1202};
1203
1204static struct genl_family dp_datapath_genl_family = {
1205 .id = GENL_ID_GENERATE,
1206 .hdrsize = sizeof(struct ovs_header),
1207 .name = OVS_DATAPATH_FAMILY,
1208 .version = OVS_DATAPATH_VERSION,
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001209 .maxattr = OVS_DP_ATTR_MAX,
1210 .netnsok = true
Jesse Grossccb13522011-10-25 19:26:31 -07001211};
1212
1213static struct genl_multicast_group ovs_dp_datapath_multicast_group = {
1214 .name = OVS_DATAPATH_MCGROUP
1215};
1216
1217static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001218 u32 portid, u32 seq, u32 flags, u8 cmd)
Jesse Grossccb13522011-10-25 19:26:31 -07001219{
1220 struct ovs_header *ovs_header;
1221 struct ovs_dp_stats dp_stats;
1222 int err;
1223
Eric W. Biederman15e47302012-09-07 20:12:54 +00001224 ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
Jesse Grossccb13522011-10-25 19:26:31 -07001225 flags, cmd);
1226 if (!ovs_header)
1227 goto error;
1228
1229 ovs_header->dp_ifindex = get_dpifindex(dp);
1230
1231 rcu_read_lock();
1232 err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
1233 rcu_read_unlock();
1234 if (err)
1235 goto nla_put_failure;
1236
1237 get_dp_stats(dp, &dp_stats);
David S. Miller028d6a62012-03-29 23:20:48 -04001238 if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats), &dp_stats))
1239 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001240
1241 return genlmsg_end(skb, ovs_header);
1242
1243nla_put_failure:
1244 genlmsg_cancel(skb, ovs_header);
1245error:
1246 return -EMSGSIZE;
1247}
1248
Eric W. Biederman15e47302012-09-07 20:12:54 +00001249static struct sk_buff *ovs_dp_cmd_build_info(struct datapath *dp, u32 portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001250 u32 seq, u8 cmd)
1251{
1252 struct sk_buff *skb;
1253 int retval;
1254
1255 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1256 if (!skb)
1257 return ERR_PTR(-ENOMEM);
1258
Eric W. Biederman15e47302012-09-07 20:12:54 +00001259 retval = ovs_dp_cmd_fill_info(dp, skb, portid, seq, 0, cmd);
Jesse Grossccb13522011-10-25 19:26:31 -07001260 if (retval < 0) {
1261 kfree_skb(skb);
1262 return ERR_PTR(retval);
1263 }
1264 return skb;
1265}
1266
1267/* Called with genl_mutex and optionally with RTNL lock also. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001268static struct datapath *lookup_datapath(struct net *net,
1269 struct ovs_header *ovs_header,
Jesse Grossccb13522011-10-25 19:26:31 -07001270 struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1271{
1272 struct datapath *dp;
1273
1274 if (!a[OVS_DP_ATTR_NAME])
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001275 dp = get_dp(net, ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001276 else {
1277 struct vport *vport;
1278
1279 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001280 vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
Jesse Grossccb13522011-10-25 19:26:31 -07001281 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
1282 rcu_read_unlock();
1283 }
1284 return dp ? dp : ERR_PTR(-ENODEV);
1285}
1286
1287static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1288{
1289 struct nlattr **a = info->attrs;
1290 struct vport_parms parms;
1291 struct sk_buff *reply;
1292 struct datapath *dp;
1293 struct vport *vport;
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001294 struct ovs_net *ovs_net;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001295 int err, i;
Jesse Grossccb13522011-10-25 19:26:31 -07001296
1297 err = -EINVAL;
1298 if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1299 goto err;
1300
1301 rtnl_lock();
Jesse Grossccb13522011-10-25 19:26:31 -07001302
1303 err = -ENOMEM;
1304 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1305 if (dp == NULL)
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001306 goto err_unlock_rtnl;
1307
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001308 ovs_dp_set_net(dp, hold_net(sock_net(skb->sk)));
Jesse Grossccb13522011-10-25 19:26:31 -07001309
1310 /* Allocate table. */
1311 err = -ENOMEM;
1312 rcu_assign_pointer(dp->table, ovs_flow_tbl_alloc(TBL_MIN_BUCKETS));
1313 if (!dp->table)
1314 goto err_free_dp;
1315
1316 dp->stats_percpu = alloc_percpu(struct dp_stats_percpu);
1317 if (!dp->stats_percpu) {
1318 err = -ENOMEM;
1319 goto err_destroy_table;
1320 }
1321
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001322 dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
1323 GFP_KERNEL);
1324 if (!dp->ports) {
1325 err = -ENOMEM;
1326 goto err_destroy_percpu;
1327 }
1328
1329 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1330 INIT_HLIST_HEAD(&dp->ports[i]);
1331
Jesse Grossccb13522011-10-25 19:26:31 -07001332 /* Set up our datapath device. */
1333 parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1334 parms.type = OVS_VPORT_TYPE_INTERNAL;
1335 parms.options = NULL;
1336 parms.dp = dp;
1337 parms.port_no = OVSP_LOCAL;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001338 parms.upcall_portid = nla_get_u32(a[OVS_DP_ATTR_UPCALL_PID]);
Jesse Grossccb13522011-10-25 19:26:31 -07001339
1340 vport = new_vport(&parms);
1341 if (IS_ERR(vport)) {
1342 err = PTR_ERR(vport);
1343 if (err == -EBUSY)
1344 err = -EEXIST;
1345
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001346 goto err_destroy_ports_array;
Jesse Grossccb13522011-10-25 19:26:31 -07001347 }
1348
Eric W. Biederman15e47302012-09-07 20:12:54 +00001349 reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001350 info->snd_seq, OVS_DP_CMD_NEW);
1351 err = PTR_ERR(reply);
1352 if (IS_ERR(reply))
1353 goto err_destroy_local_port;
1354
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001355 ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
1356 list_add_tail(&dp->list_node, &ovs_net->dps);
Jesse Grossccb13522011-10-25 19:26:31 -07001357 rtnl_unlock();
1358
Eric W. Biederman15e47302012-09-07 20:12:54 +00001359 genl_notify(reply, genl_info_net(info), info->snd_portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001360 ovs_dp_datapath_multicast_group.id, info->nlhdr,
1361 GFP_KERNEL);
1362 return 0;
1363
1364err_destroy_local_port:
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001365 ovs_dp_detach_port(ovs_vport_rtnl(dp, OVSP_LOCAL));
1366err_destroy_ports_array:
1367 kfree(dp->ports);
Jesse Grossccb13522011-10-25 19:26:31 -07001368err_destroy_percpu:
1369 free_percpu(dp->stats_percpu);
1370err_destroy_table:
1371 ovs_flow_tbl_destroy(genl_dereference(dp->table));
1372err_free_dp:
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001373 release_net(ovs_dp_get_net(dp));
Jesse Grossccb13522011-10-25 19:26:31 -07001374 kfree(dp);
Jesse Grossccb13522011-10-25 19:26:31 -07001375err_unlock_rtnl:
1376 rtnl_unlock();
1377err:
1378 return err;
1379}
1380
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001381/* Called with genl_mutex. */
1382static void __dp_destroy(struct datapath *dp)
Jesse Grossccb13522011-10-25 19:26:31 -07001383{
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001384 int i;
Jesse Grossccb13522011-10-25 19:26:31 -07001385
1386 rtnl_lock();
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001387
1388 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1389 struct vport *vport;
1390 struct hlist_node *node, *n;
1391
1392 hlist_for_each_entry_safe(vport, node, n, &dp->ports[i], dp_hash_node)
1393 if (vport->port_no != OVSP_LOCAL)
1394 ovs_dp_detach_port(vport);
1395 }
Jesse Grossccb13522011-10-25 19:26:31 -07001396
1397 list_del(&dp->list_node);
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001398 ovs_dp_detach_port(ovs_vport_rtnl(dp, OVSP_LOCAL));
Jesse Grossccb13522011-10-25 19:26:31 -07001399
1400 /* rtnl_unlock() will wait until all the references to devices that
1401 * are pending unregistration have been dropped. We do it here to
1402 * ensure that any internal devices (which contain DP pointers) are
1403 * fully destroyed before freeing the datapath.
1404 */
1405 rtnl_unlock();
1406
1407 call_rcu(&dp->rcu, destroy_dp_rcu);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001408}
1409
1410static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1411{
1412 struct sk_buff *reply;
1413 struct datapath *dp;
1414 int err;
1415
1416 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1417 err = PTR_ERR(dp);
1418 if (IS_ERR(dp))
1419 return err;
1420
Eric W. Biederman15e47302012-09-07 20:12:54 +00001421 reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001422 info->snd_seq, OVS_DP_CMD_DEL);
1423 err = PTR_ERR(reply);
1424 if (IS_ERR(reply))
1425 return err;
1426
1427 __dp_destroy(dp);
Jesse Grossccb13522011-10-25 19:26:31 -07001428
Eric W. Biederman15e47302012-09-07 20:12:54 +00001429 genl_notify(reply, genl_info_net(info), info->snd_portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001430 ovs_dp_datapath_multicast_group.id, info->nlhdr,
1431 GFP_KERNEL);
1432
1433 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001434}
1435
1436static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1437{
1438 struct sk_buff *reply;
1439 struct datapath *dp;
1440 int err;
1441
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001442 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
Jesse Grossccb13522011-10-25 19:26:31 -07001443 if (IS_ERR(dp))
1444 return PTR_ERR(dp);
1445
Eric W. Biederman15e47302012-09-07 20:12:54 +00001446 reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001447 info->snd_seq, OVS_DP_CMD_NEW);
1448 if (IS_ERR(reply)) {
1449 err = PTR_ERR(reply);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001450 netlink_set_err(sock_net(skb->sk)->genl_sock, 0,
Jesse Grossccb13522011-10-25 19:26:31 -07001451 ovs_dp_datapath_multicast_group.id, err);
1452 return 0;
1453 }
1454
Eric W. Biederman15e47302012-09-07 20:12:54 +00001455 genl_notify(reply, genl_info_net(info), info->snd_portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001456 ovs_dp_datapath_multicast_group.id, info->nlhdr,
1457 GFP_KERNEL);
1458
1459 return 0;
1460}
1461
1462static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1463{
1464 struct sk_buff *reply;
1465 struct datapath *dp;
1466
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001467 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
Jesse Grossccb13522011-10-25 19:26:31 -07001468 if (IS_ERR(dp))
1469 return PTR_ERR(dp);
1470
Eric W. Biederman15e47302012-09-07 20:12:54 +00001471 reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001472 info->snd_seq, OVS_DP_CMD_NEW);
1473 if (IS_ERR(reply))
1474 return PTR_ERR(reply);
1475
1476 return genlmsg_reply(reply, info);
1477}
1478
1479static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1480{
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001481 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
Jesse Grossccb13522011-10-25 19:26:31 -07001482 struct datapath *dp;
1483 int skip = cb->args[0];
1484 int i = 0;
1485
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001486 list_for_each_entry(dp, &ovs_net->dps, list_node) {
Ben Pfaff77676fd2012-01-17 13:33:39 +00001487 if (i >= skip &&
Eric W. Biederman15e47302012-09-07 20:12:54 +00001488 ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001489 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1490 OVS_DP_CMD_NEW) < 0)
1491 break;
1492 i++;
1493 }
1494
1495 cb->args[0] = i;
1496
1497 return skb->len;
1498}
1499
1500static struct genl_ops dp_datapath_genl_ops[] = {
1501 { .cmd = OVS_DP_CMD_NEW,
1502 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1503 .policy = datapath_policy,
1504 .doit = ovs_dp_cmd_new
1505 },
1506 { .cmd = OVS_DP_CMD_DEL,
1507 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1508 .policy = datapath_policy,
1509 .doit = ovs_dp_cmd_del
1510 },
1511 { .cmd = OVS_DP_CMD_GET,
1512 .flags = 0, /* OK for unprivileged users. */
1513 .policy = datapath_policy,
1514 .doit = ovs_dp_cmd_get,
1515 .dumpit = ovs_dp_cmd_dump
1516 },
1517 { .cmd = OVS_DP_CMD_SET,
1518 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1519 .policy = datapath_policy,
1520 .doit = ovs_dp_cmd_set,
1521 },
1522};
1523
1524static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
1525 [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1526 [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
1527 [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
1528 [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
1529 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1530 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
1531};
1532
1533static struct genl_family dp_vport_genl_family = {
1534 .id = GENL_ID_GENERATE,
1535 .hdrsize = sizeof(struct ovs_header),
1536 .name = OVS_VPORT_FAMILY,
1537 .version = OVS_VPORT_VERSION,
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001538 .maxattr = OVS_VPORT_ATTR_MAX,
1539 .netnsok = true
Jesse Grossccb13522011-10-25 19:26:31 -07001540};
1541
1542struct genl_multicast_group ovs_dp_vport_multicast_group = {
1543 .name = OVS_VPORT_MCGROUP
1544};
1545
1546/* Called with RTNL lock or RCU read lock. */
1547static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001548 u32 portid, u32 seq, u32 flags, u8 cmd)
Jesse Grossccb13522011-10-25 19:26:31 -07001549{
1550 struct ovs_header *ovs_header;
1551 struct ovs_vport_stats vport_stats;
1552 int err;
1553
Eric W. Biederman15e47302012-09-07 20:12:54 +00001554 ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
Jesse Grossccb13522011-10-25 19:26:31 -07001555 flags, cmd);
1556 if (!ovs_header)
1557 return -EMSGSIZE;
1558
1559 ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1560
David S. Miller028d6a62012-03-29 23:20:48 -04001561 if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
1562 nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
1563 nla_put_string(skb, OVS_VPORT_ATTR_NAME, vport->ops->get_name(vport)) ||
Eric W. Biederman15e47302012-09-07 20:12:54 +00001564 nla_put_u32(skb, OVS_VPORT_ATTR_UPCALL_PID, vport->upcall_portid))
David S. Miller028d6a62012-03-29 23:20:48 -04001565 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001566
1567 ovs_vport_get_stats(vport, &vport_stats);
David S. Miller028d6a62012-03-29 23:20:48 -04001568 if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
1569 &vport_stats))
1570 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001571
1572 err = ovs_vport_get_options(vport, skb);
1573 if (err == -EMSGSIZE)
1574 goto error;
1575
1576 return genlmsg_end(skb, ovs_header);
1577
1578nla_put_failure:
1579 err = -EMSGSIZE;
1580error:
1581 genlmsg_cancel(skb, ovs_header);
1582 return err;
1583}
1584
1585/* Called with RTNL lock or RCU read lock. */
Eric W. Biederman15e47302012-09-07 20:12:54 +00001586struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001587 u32 seq, u8 cmd)
1588{
1589 struct sk_buff *skb;
1590 int retval;
1591
1592 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1593 if (!skb)
1594 return ERR_PTR(-ENOMEM);
1595
Eric W. Biederman15e47302012-09-07 20:12:54 +00001596 retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
Jesse Grossccb13522011-10-25 19:26:31 -07001597 if (retval < 0) {
1598 kfree_skb(skb);
1599 return ERR_PTR(retval);
1600 }
1601 return skb;
1602}
1603
1604/* Called with RTNL lock or RCU read lock. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001605static struct vport *lookup_vport(struct net *net,
1606 struct ovs_header *ovs_header,
Jesse Grossccb13522011-10-25 19:26:31 -07001607 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1608{
1609 struct datapath *dp;
1610 struct vport *vport;
1611
1612 if (a[OVS_VPORT_ATTR_NAME]) {
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001613 vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
Jesse Grossccb13522011-10-25 19:26:31 -07001614 if (!vport)
1615 return ERR_PTR(-ENODEV);
Ben Pfaff651a68e2012-03-06 15:04:04 -08001616 if (ovs_header->dp_ifindex &&
1617 ovs_header->dp_ifindex != get_dpifindex(vport->dp))
1618 return ERR_PTR(-ENODEV);
Jesse Grossccb13522011-10-25 19:26:31 -07001619 return vport;
1620 } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
1621 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1622
1623 if (port_no >= DP_MAX_PORTS)
1624 return ERR_PTR(-EFBIG);
1625
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001626 dp = get_dp(net, ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001627 if (!dp)
1628 return ERR_PTR(-ENODEV);
1629
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001630 vport = ovs_vport_rtnl_rcu(dp, port_no);
Jesse Grossccb13522011-10-25 19:26:31 -07001631 if (!vport)
1632 return ERR_PTR(-ENOENT);
1633 return vport;
1634 } else
1635 return ERR_PTR(-EINVAL);
1636}
1637
1638static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
1639{
1640 struct nlattr **a = info->attrs;
1641 struct ovs_header *ovs_header = info->userhdr;
1642 struct vport_parms parms;
1643 struct sk_buff *reply;
1644 struct vport *vport;
1645 struct datapath *dp;
1646 u32 port_no;
1647 int err;
1648
1649 err = -EINVAL;
1650 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
1651 !a[OVS_VPORT_ATTR_UPCALL_PID])
1652 goto exit;
1653
1654 rtnl_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001655 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001656 err = -ENODEV;
1657 if (!dp)
1658 goto exit_unlock;
1659
1660 if (a[OVS_VPORT_ATTR_PORT_NO]) {
1661 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1662
1663 err = -EFBIG;
1664 if (port_no >= DP_MAX_PORTS)
1665 goto exit_unlock;
1666
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001667 vport = ovs_vport_rtnl_rcu(dp, port_no);
Jesse Grossccb13522011-10-25 19:26:31 -07001668 err = -EBUSY;
1669 if (vport)
1670 goto exit_unlock;
1671 } else {
1672 for (port_no = 1; ; port_no++) {
1673 if (port_no >= DP_MAX_PORTS) {
1674 err = -EFBIG;
1675 goto exit_unlock;
1676 }
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001677 vport = ovs_vport_rtnl(dp, port_no);
Jesse Grossccb13522011-10-25 19:26:31 -07001678 if (!vport)
1679 break;
1680 }
1681 }
1682
1683 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
1684 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1685 parms.options = a[OVS_VPORT_ATTR_OPTIONS];
1686 parms.dp = dp;
1687 parms.port_no = port_no;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001688 parms.upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
Jesse Grossccb13522011-10-25 19:26:31 -07001689
1690 vport = new_vport(&parms);
1691 err = PTR_ERR(vport);
1692 if (IS_ERR(vport))
1693 goto exit_unlock;
1694
Rich Lanecb7c5bd2013-02-08 13:18:01 -08001695 err = 0;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001696 reply = ovs_vport_cmd_build_info(vport, info->snd_portid, info->snd_seq,
Jesse Grossccb13522011-10-25 19:26:31 -07001697 OVS_VPORT_CMD_NEW);
1698 if (IS_ERR(reply)) {
1699 err = PTR_ERR(reply);
1700 ovs_dp_detach_port(vport);
1701 goto exit_unlock;
1702 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00001703 genl_notify(reply, genl_info_net(info), info->snd_portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001704 ovs_dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
1705
1706exit_unlock:
1707 rtnl_unlock();
1708exit:
1709 return err;
1710}
1711
1712static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
1713{
1714 struct nlattr **a = info->attrs;
1715 struct sk_buff *reply;
1716 struct vport *vport;
1717 int err;
1718
1719 rtnl_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001720 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
Jesse Grossccb13522011-10-25 19:26:31 -07001721 err = PTR_ERR(vport);
1722 if (IS_ERR(vport))
1723 goto exit_unlock;
1724
1725 err = 0;
1726 if (a[OVS_VPORT_ATTR_TYPE] &&
1727 nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type)
1728 err = -EINVAL;
1729
1730 if (!err && a[OVS_VPORT_ATTR_OPTIONS])
1731 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
Ansis Atteka03fbf8b2012-04-09 12:12:12 -07001732 if (err)
1733 goto exit_unlock;
1734 if (a[OVS_VPORT_ATTR_UPCALL_PID])
Eric W. Biederman15e47302012-09-07 20:12:54 +00001735 vport->upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
Jesse Grossccb13522011-10-25 19:26:31 -07001736
Eric W. Biederman15e47302012-09-07 20:12:54 +00001737 reply = ovs_vport_cmd_build_info(vport, info->snd_portid, info->snd_seq,
Jesse Grossccb13522011-10-25 19:26:31 -07001738 OVS_VPORT_CMD_NEW);
1739 if (IS_ERR(reply)) {
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001740 netlink_set_err(sock_net(skb->sk)->genl_sock, 0,
Ansis Atteka4cb6e112012-05-03 18:40:38 -07001741 ovs_dp_vport_multicast_group.id, PTR_ERR(reply));
1742 goto exit_unlock;
Jesse Grossccb13522011-10-25 19:26:31 -07001743 }
1744
Eric W. Biederman15e47302012-09-07 20:12:54 +00001745 genl_notify(reply, genl_info_net(info), info->snd_portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001746 ovs_dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
1747
1748exit_unlock:
1749 rtnl_unlock();
1750 return err;
1751}
1752
1753static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
1754{
1755 struct nlattr **a = info->attrs;
1756 struct sk_buff *reply;
1757 struct vport *vport;
1758 int err;
1759
1760 rtnl_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001761 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
Jesse Grossccb13522011-10-25 19:26:31 -07001762 err = PTR_ERR(vport);
1763 if (IS_ERR(vport))
1764 goto exit_unlock;
1765
1766 if (vport->port_no == OVSP_LOCAL) {
1767 err = -EINVAL;
1768 goto exit_unlock;
1769 }
1770
Eric W. Biederman15e47302012-09-07 20:12:54 +00001771 reply = ovs_vport_cmd_build_info(vport, info->snd_portid, info->snd_seq,
Jesse Grossccb13522011-10-25 19:26:31 -07001772 OVS_VPORT_CMD_DEL);
1773 err = PTR_ERR(reply);
1774 if (IS_ERR(reply))
1775 goto exit_unlock;
1776
Rich Lane734907e2013-02-08 09:30:23 -08001777 err = 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001778 ovs_dp_detach_port(vport);
1779
Eric W. Biederman15e47302012-09-07 20:12:54 +00001780 genl_notify(reply, genl_info_net(info), info->snd_portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001781 ovs_dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
1782
1783exit_unlock:
1784 rtnl_unlock();
1785 return err;
1786}
1787
1788static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
1789{
1790 struct nlattr **a = info->attrs;
1791 struct ovs_header *ovs_header = info->userhdr;
1792 struct sk_buff *reply;
1793 struct vport *vport;
1794 int err;
1795
1796 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001797 vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
Jesse Grossccb13522011-10-25 19:26:31 -07001798 err = PTR_ERR(vport);
1799 if (IS_ERR(vport))
1800 goto exit_unlock;
1801
Eric W. Biederman15e47302012-09-07 20:12:54 +00001802 reply = ovs_vport_cmd_build_info(vport, info->snd_portid, info->snd_seq,
Jesse Grossccb13522011-10-25 19:26:31 -07001803 OVS_VPORT_CMD_NEW);
1804 err = PTR_ERR(reply);
1805 if (IS_ERR(reply))
1806 goto exit_unlock;
1807
1808 rcu_read_unlock();
1809
1810 return genlmsg_reply(reply, info);
1811
1812exit_unlock:
1813 rcu_read_unlock();
1814 return err;
1815}
1816
1817static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1818{
1819 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1820 struct datapath *dp;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001821 int bucket = cb->args[0], skip = cb->args[1];
1822 int i, j = 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001823
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001824 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001825 if (!dp)
1826 return -ENODEV;
1827
1828 rcu_read_lock();
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001829 for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
Jesse Grossccb13522011-10-25 19:26:31 -07001830 struct vport *vport;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001831 struct hlist_node *n;
Jesse Grossccb13522011-10-25 19:26:31 -07001832
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001833 j = 0;
1834 hlist_for_each_entry_rcu(vport, n, &dp->ports[i], dp_hash_node) {
1835 if (j >= skip &&
1836 ovs_vport_cmd_fill_info(vport, skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001837 NETLINK_CB(cb->skb).portid,
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001838 cb->nlh->nlmsg_seq,
1839 NLM_F_MULTI,
1840 OVS_VPORT_CMD_NEW) < 0)
1841 goto out;
Jesse Grossccb13522011-10-25 19:26:31 -07001842
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001843 j++;
1844 }
1845 skip = 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001846 }
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001847out:
Jesse Grossccb13522011-10-25 19:26:31 -07001848 rcu_read_unlock();
1849
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001850 cb->args[0] = i;
1851 cb->args[1] = j;
Jesse Grossccb13522011-10-25 19:26:31 -07001852
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001853 return skb->len;
Jesse Grossccb13522011-10-25 19:26:31 -07001854}
1855
Jesse Grossccb13522011-10-25 19:26:31 -07001856static struct genl_ops dp_vport_genl_ops[] = {
1857 { .cmd = OVS_VPORT_CMD_NEW,
1858 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1859 .policy = vport_policy,
1860 .doit = ovs_vport_cmd_new
1861 },
1862 { .cmd = OVS_VPORT_CMD_DEL,
1863 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1864 .policy = vport_policy,
1865 .doit = ovs_vport_cmd_del
1866 },
1867 { .cmd = OVS_VPORT_CMD_GET,
1868 .flags = 0, /* OK for unprivileged users. */
1869 .policy = vport_policy,
1870 .doit = ovs_vport_cmd_get,
1871 .dumpit = ovs_vport_cmd_dump
1872 },
1873 { .cmd = OVS_VPORT_CMD_SET,
1874 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1875 .policy = vport_policy,
1876 .doit = ovs_vport_cmd_set,
1877 },
1878};
1879
1880struct genl_family_and_ops {
1881 struct genl_family *family;
1882 struct genl_ops *ops;
1883 int n_ops;
1884 struct genl_multicast_group *group;
1885};
1886
1887static const struct genl_family_and_ops dp_genl_families[] = {
1888 { &dp_datapath_genl_family,
1889 dp_datapath_genl_ops, ARRAY_SIZE(dp_datapath_genl_ops),
1890 &ovs_dp_datapath_multicast_group },
1891 { &dp_vport_genl_family,
1892 dp_vport_genl_ops, ARRAY_SIZE(dp_vport_genl_ops),
1893 &ovs_dp_vport_multicast_group },
1894 { &dp_flow_genl_family,
1895 dp_flow_genl_ops, ARRAY_SIZE(dp_flow_genl_ops),
1896 &ovs_dp_flow_multicast_group },
1897 { &dp_packet_genl_family,
1898 dp_packet_genl_ops, ARRAY_SIZE(dp_packet_genl_ops),
1899 NULL },
1900};
1901
1902static void dp_unregister_genl(int n_families)
1903{
1904 int i;
1905
1906 for (i = 0; i < n_families; i++)
1907 genl_unregister_family(dp_genl_families[i].family);
1908}
1909
1910static int dp_register_genl(void)
1911{
1912 int n_registered;
1913 int err;
1914 int i;
1915
1916 n_registered = 0;
1917 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
1918 const struct genl_family_and_ops *f = &dp_genl_families[i];
1919
1920 err = genl_register_family_with_ops(f->family, f->ops,
1921 f->n_ops);
1922 if (err)
1923 goto error;
1924 n_registered++;
1925
1926 if (f->group) {
1927 err = genl_register_mc_group(f->family, f->group);
1928 if (err)
1929 goto error;
1930 }
1931 }
1932
1933 return 0;
1934
1935error:
1936 dp_unregister_genl(n_registered);
1937 return err;
1938}
1939
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001940static void rehash_flow_table(struct work_struct *work)
1941{
1942 struct datapath *dp;
1943 struct net *net;
1944
1945 genl_lock();
1946 rtnl_lock();
1947 for_each_net(net) {
1948 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
1949
1950 list_for_each_entry(dp, &ovs_net->dps, list_node) {
1951 struct flow_table *old_table = genl_dereference(dp->table);
1952 struct flow_table *new_table;
1953
1954 new_table = ovs_flow_tbl_rehash(old_table);
1955 if (!IS_ERR(new_table)) {
1956 rcu_assign_pointer(dp->table, new_table);
1957 ovs_flow_tbl_deferred_destroy(old_table);
1958 }
1959 }
1960 }
1961 rtnl_unlock();
1962 genl_unlock();
1963
1964 schedule_delayed_work(&rehash_flow_wq, REHASH_FLOW_INTERVAL);
1965}
1966
1967static int __net_init ovs_init_net(struct net *net)
1968{
1969 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
1970
1971 INIT_LIST_HEAD(&ovs_net->dps);
1972 return 0;
1973}
1974
1975static void __net_exit ovs_exit_net(struct net *net)
1976{
1977 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
1978 struct datapath *dp, *dp_next;
1979
1980 genl_lock();
1981 list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
1982 __dp_destroy(dp);
1983 genl_unlock();
1984}
1985
1986static struct pernet_operations ovs_net_ops = {
1987 .init = ovs_init_net,
1988 .exit = ovs_exit_net,
1989 .id = &ovs_net_id,
1990 .size = sizeof(struct ovs_net),
1991};
1992
Jesse Grossccb13522011-10-25 19:26:31 -07001993static int __init dp_init(void)
1994{
1995 struct sk_buff *dummy_skb;
1996 int err;
1997
1998 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > sizeof(dummy_skb->cb));
1999
2000 pr_info("Open vSwitch switching datapath\n");
2001
2002 err = ovs_flow_init();
2003 if (err)
2004 goto error;
2005
2006 err = ovs_vport_init();
2007 if (err)
2008 goto error_flow_exit;
2009
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002010 err = register_pernet_device(&ovs_net_ops);
Jesse Grossccb13522011-10-25 19:26:31 -07002011 if (err)
2012 goto error_vport_exit;
2013
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002014 err = register_netdevice_notifier(&ovs_dp_device_notifier);
2015 if (err)
2016 goto error_netns_exit;
2017
Jesse Grossccb13522011-10-25 19:26:31 -07002018 err = dp_register_genl();
2019 if (err < 0)
2020 goto error_unreg_notifier;
2021
2022 schedule_delayed_work(&rehash_flow_wq, REHASH_FLOW_INTERVAL);
2023
2024 return 0;
2025
2026error_unreg_notifier:
2027 unregister_netdevice_notifier(&ovs_dp_device_notifier);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002028error_netns_exit:
2029 unregister_pernet_device(&ovs_net_ops);
Jesse Grossccb13522011-10-25 19:26:31 -07002030error_vport_exit:
2031 ovs_vport_exit();
2032error_flow_exit:
2033 ovs_flow_exit();
2034error:
2035 return err;
2036}
2037
2038static void dp_cleanup(void)
2039{
2040 cancel_delayed_work_sync(&rehash_flow_wq);
Jesse Grossccb13522011-10-25 19:26:31 -07002041 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
2042 unregister_netdevice_notifier(&ovs_dp_device_notifier);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002043 unregister_pernet_device(&ovs_net_ops);
2044 rcu_barrier();
Jesse Grossccb13522011-10-25 19:26:31 -07002045 ovs_vport_exit();
2046 ovs_flow_exit();
2047}
2048
2049module_init(dp_init);
2050module_exit(dp_cleanup);
2051
2052MODULE_DESCRIPTION("Open vSwitch switching datapath");
2053MODULE_LICENSE("GPL");