blob: 7b1d6d2b0c1a1dde598bb9bbe5b9d302d1785359 [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
Eric W. Biederman15e47302012-09-07 20:12:54 +0000398 err = genlmsg_unicast(net, user_skb, upcall_info->portid);
Jesse Grossccb13522011-10-25 19:26:31 -0700399
400out:
401 kfree_skb(nskb);
402 return err;
403}
404
405/* Called with genl_mutex. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800406static int flush_flows(struct datapath *dp)
Jesse Grossccb13522011-10-25 19:26:31 -0700407{
408 struct flow_table *old_table;
409 struct flow_table *new_table;
Jesse Grossccb13522011-10-25 19:26:31 -0700410
411 old_table = genl_dereference(dp->table);
412 new_table = ovs_flow_tbl_alloc(TBL_MIN_BUCKETS);
413 if (!new_table)
414 return -ENOMEM;
415
416 rcu_assign_pointer(dp->table, new_table);
417
418 ovs_flow_tbl_deferred_destroy(old_table);
419 return 0;
420}
421
422static int validate_actions(const struct nlattr *attr,
423 const struct sw_flow_key *key, int depth);
424
425static int validate_sample(const struct nlattr *attr,
426 const struct sw_flow_key *key, int depth)
427{
428 const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
429 const struct nlattr *probability, *actions;
430 const struct nlattr *a;
431 int rem;
432
433 memset(attrs, 0, sizeof(attrs));
434 nla_for_each_nested(a, attr, rem) {
435 int type = nla_type(a);
436 if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
437 return -EINVAL;
438 attrs[type] = a;
439 }
440 if (rem)
441 return -EINVAL;
442
443 probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
444 if (!probability || nla_len(probability) != sizeof(u32))
445 return -EINVAL;
446
447 actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
448 if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
449 return -EINVAL;
450 return validate_actions(actions, key, depth + 1);
451}
452
Pravin B Shelar072ae632012-05-07 17:21:53 -0700453static int validate_tp_port(const struct sw_flow_key *flow_key)
454{
455 if (flow_key->eth.type == htons(ETH_P_IP)) {
Jesse Gross41853922012-08-06 15:49:47 -0700456 if (flow_key->ipv4.tp.src || flow_key->ipv4.tp.dst)
Pravin B Shelar072ae632012-05-07 17:21:53 -0700457 return 0;
458 } else if (flow_key->eth.type == htons(ETH_P_IPV6)) {
Jesse Gross41853922012-08-06 15:49:47 -0700459 if (flow_key->ipv6.tp.src || flow_key->ipv6.tp.dst)
Pravin B Shelar072ae632012-05-07 17:21:53 -0700460 return 0;
461 }
462
463 return -EINVAL;
464}
465
Jesse Grossccb13522011-10-25 19:26:31 -0700466static int validate_set(const struct nlattr *a,
467 const struct sw_flow_key *flow_key)
468{
469 const struct nlattr *ovs_key = nla_data(a);
470 int key_type = nla_type(ovs_key);
471
472 /* There can be only one key in a action */
473 if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
474 return -EINVAL;
475
476 if (key_type > OVS_KEY_ATTR_MAX ||
477 nla_len(ovs_key) != ovs_key_lens[key_type])
478 return -EINVAL;
479
480 switch (key_type) {
481 const struct ovs_key_ipv4 *ipv4_key;
Ansis Atteka3fdbd1c2012-11-13 15:44:14 -0800482 const struct ovs_key_ipv6 *ipv6_key;
Jesse Grossccb13522011-10-25 19:26:31 -0700483
484 case OVS_KEY_ATTR_PRIORITY:
485 case OVS_KEY_ATTR_ETHERNET:
486 break;
487
488 case OVS_KEY_ATTR_IPV4:
489 if (flow_key->eth.type != htons(ETH_P_IP))
490 return -EINVAL;
491
Jesse Gross41853922012-08-06 15:49:47 -0700492 if (!flow_key->ip.proto)
Jesse Grossccb13522011-10-25 19:26:31 -0700493 return -EINVAL;
494
495 ipv4_key = nla_data(ovs_key);
496 if (ipv4_key->ipv4_proto != flow_key->ip.proto)
497 return -EINVAL;
498
499 if (ipv4_key->ipv4_frag != flow_key->ip.frag)
500 return -EINVAL;
501
502 break;
503
Ansis Atteka3fdbd1c2012-11-13 15:44:14 -0800504 case OVS_KEY_ATTR_IPV6:
505 if (flow_key->eth.type != htons(ETH_P_IPV6))
506 return -EINVAL;
507
508 if (!flow_key->ip.proto)
509 return -EINVAL;
510
511 ipv6_key = nla_data(ovs_key);
512 if (ipv6_key->ipv6_proto != flow_key->ip.proto)
513 return -EINVAL;
514
515 if (ipv6_key->ipv6_frag != flow_key->ip.frag)
516 return -EINVAL;
517
518 if (ntohl(ipv6_key->ipv6_label) & 0xFFF00000)
519 return -EINVAL;
520
521 break;
522
Jesse Grossccb13522011-10-25 19:26:31 -0700523 case OVS_KEY_ATTR_TCP:
524 if (flow_key->ip.proto != IPPROTO_TCP)
525 return -EINVAL;
526
Pravin B Shelar072ae632012-05-07 17:21:53 -0700527 return validate_tp_port(flow_key);
Jesse Grossccb13522011-10-25 19:26:31 -0700528
529 case OVS_KEY_ATTR_UDP:
530 if (flow_key->ip.proto != IPPROTO_UDP)
531 return -EINVAL;
532
Pravin B Shelar072ae632012-05-07 17:21:53 -0700533 return validate_tp_port(flow_key);
Jesse Grossccb13522011-10-25 19:26:31 -0700534
535 default:
536 return -EINVAL;
537 }
538
539 return 0;
540}
541
542static int validate_userspace(const struct nlattr *attr)
543{
544 static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
545 [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
546 [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_U64 },
547 };
548 struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
549 int error;
550
551 error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX,
552 attr, userspace_policy);
553 if (error)
554 return error;
555
556 if (!a[OVS_USERSPACE_ATTR_PID] ||
557 !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
558 return -EINVAL;
559
560 return 0;
561}
562
563static int validate_actions(const struct nlattr *attr,
564 const struct sw_flow_key *key, int depth)
565{
566 const struct nlattr *a;
567 int rem, err;
568
569 if (depth >= SAMPLE_ACTION_DEPTH)
570 return -EOVERFLOW;
571
572 nla_for_each_nested(a, attr, rem) {
573 /* Expected argument lengths, (u32)-1 for variable length. */
574 static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
575 [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
576 [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
577 [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
578 [OVS_ACTION_ATTR_POP_VLAN] = 0,
579 [OVS_ACTION_ATTR_SET] = (u32)-1,
580 [OVS_ACTION_ATTR_SAMPLE] = (u32)-1
581 };
582 const struct ovs_action_push_vlan *vlan;
583 int type = nla_type(a);
584
585 if (type > OVS_ACTION_ATTR_MAX ||
586 (action_lens[type] != nla_len(a) &&
587 action_lens[type] != (u32)-1))
588 return -EINVAL;
589
590 switch (type) {
591 case OVS_ACTION_ATTR_UNSPEC:
592 return -EINVAL;
593
594 case OVS_ACTION_ATTR_USERSPACE:
595 err = validate_userspace(a);
596 if (err)
597 return err;
598 break;
599
600 case OVS_ACTION_ATTR_OUTPUT:
601 if (nla_get_u32(a) >= DP_MAX_PORTS)
602 return -EINVAL;
603 break;
604
605
606 case OVS_ACTION_ATTR_POP_VLAN:
607 break;
608
609 case OVS_ACTION_ATTR_PUSH_VLAN:
610 vlan = nla_data(a);
611 if (vlan->vlan_tpid != htons(ETH_P_8021Q))
612 return -EINVAL;
613 if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
614 return -EINVAL;
615 break;
616
617 case OVS_ACTION_ATTR_SET:
618 err = validate_set(a, key);
619 if (err)
620 return err;
621 break;
622
623 case OVS_ACTION_ATTR_SAMPLE:
624 err = validate_sample(a, key, depth);
625 if (err)
626 return err;
627 break;
628
629 default:
630 return -EINVAL;
631 }
632 }
633
634 if (rem > 0)
635 return -EINVAL;
636
637 return 0;
638}
639
640static void clear_stats(struct sw_flow *flow)
641{
642 flow->used = 0;
643 flow->tcp_flags = 0;
644 flow->packet_count = 0;
645 flow->byte_count = 0;
646}
647
648static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
649{
650 struct ovs_header *ovs_header = info->userhdr;
651 struct nlattr **a = info->attrs;
652 struct sw_flow_actions *acts;
653 struct sk_buff *packet;
654 struct sw_flow *flow;
655 struct datapath *dp;
656 struct ethhdr *eth;
657 int len;
658 int err;
659 int key_len;
660
661 err = -EINVAL;
662 if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
663 !a[OVS_PACKET_ATTR_ACTIONS] ||
664 nla_len(a[OVS_PACKET_ATTR_PACKET]) < ETH_HLEN)
665 goto err;
666
667 len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
668 packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
669 err = -ENOMEM;
670 if (!packet)
671 goto err;
672 skb_reserve(packet, NET_IP_ALIGN);
673
674 memcpy(__skb_put(packet, len), nla_data(a[OVS_PACKET_ATTR_PACKET]), len);
675
676 skb_reset_mac_header(packet);
677 eth = eth_hdr(packet);
678
679 /* Normally, setting the skb 'protocol' field would be handled by a
680 * call to eth_type_trans(), but it assumes there's a sending
681 * device, which we may not have. */
682 if (ntohs(eth->h_proto) >= 1536)
683 packet->protocol = eth->h_proto;
684 else
685 packet->protocol = htons(ETH_P_802_2);
686
687 /* Build an sw_flow for sending this packet. */
688 flow = ovs_flow_alloc();
689 err = PTR_ERR(flow);
690 if (IS_ERR(flow))
691 goto err_kfree_skb;
692
693 err = ovs_flow_extract(packet, -1, &flow->key, &key_len);
694 if (err)
695 goto err_flow_free;
696
697 err = ovs_flow_metadata_from_nlattrs(&flow->key.phy.priority,
698 &flow->key.phy.in_port,
699 a[OVS_PACKET_ATTR_KEY]);
700 if (err)
701 goto err_flow_free;
702
703 err = validate_actions(a[OVS_PACKET_ATTR_ACTIONS], &flow->key, 0);
704 if (err)
705 goto err_flow_free;
706
707 flow->hash = ovs_flow_hash(&flow->key, key_len);
708
709 acts = ovs_flow_actions_alloc(a[OVS_PACKET_ATTR_ACTIONS]);
710 err = PTR_ERR(acts);
711 if (IS_ERR(acts))
712 goto err_flow_free;
713 rcu_assign_pointer(flow->sf_acts, acts);
714
715 OVS_CB(packet)->flow = flow;
716 packet->priority = flow->key.phy.priority;
717
718 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800719 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -0700720 err = -ENODEV;
721 if (!dp)
722 goto err_unlock;
723
724 local_bh_disable();
725 err = ovs_execute_actions(dp, packet);
726 local_bh_enable();
727 rcu_read_unlock();
728
729 ovs_flow_free(flow);
730 return err;
731
732err_unlock:
733 rcu_read_unlock();
734err_flow_free:
735 ovs_flow_free(flow);
736err_kfree_skb:
737 kfree_skb(packet);
738err:
739 return err;
740}
741
742static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
743 [OVS_PACKET_ATTR_PACKET] = { .type = NLA_UNSPEC },
744 [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
745 [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
746};
747
748static struct genl_ops dp_packet_genl_ops[] = {
749 { .cmd = OVS_PACKET_CMD_EXECUTE,
750 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
751 .policy = packet_policy,
752 .doit = ovs_packet_cmd_execute
753 }
754};
755
756static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats)
757{
758 int i;
759 struct flow_table *table = genl_dereference(dp->table);
760
761 stats->n_flows = ovs_flow_tbl_count(table);
762
763 stats->n_hit = stats->n_missed = stats->n_lost = 0;
764 for_each_possible_cpu(i) {
765 const struct dp_stats_percpu *percpu_stats;
766 struct dp_stats_percpu local_stats;
767 unsigned int start;
768
769 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
770
771 do {
772 start = u64_stats_fetch_begin_bh(&percpu_stats->sync);
773 local_stats = *percpu_stats;
774 } while (u64_stats_fetch_retry_bh(&percpu_stats->sync, start));
775
776 stats->n_hit += local_stats.n_hit;
777 stats->n_missed += local_stats.n_missed;
778 stats->n_lost += local_stats.n_lost;
779 }
780}
781
782static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
783 [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
784 [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
785 [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
786};
787
788static struct genl_family dp_flow_genl_family = {
789 .id = GENL_ID_GENERATE,
790 .hdrsize = sizeof(struct ovs_header),
791 .name = OVS_FLOW_FAMILY,
792 .version = OVS_FLOW_VERSION,
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800793 .maxattr = OVS_FLOW_ATTR_MAX,
794 .netnsok = true
Jesse Grossccb13522011-10-25 19:26:31 -0700795};
796
797static struct genl_multicast_group ovs_dp_flow_multicast_group = {
798 .name = OVS_FLOW_MCGROUP
799};
800
801/* Called with genl_lock. */
802static int ovs_flow_cmd_fill_info(struct sw_flow *flow, struct datapath *dp,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000803 struct sk_buff *skb, u32 portid,
Jesse Grossccb13522011-10-25 19:26:31 -0700804 u32 seq, u32 flags, u8 cmd)
805{
806 const int skb_orig_len = skb->len;
807 const struct sw_flow_actions *sf_acts;
808 struct ovs_flow_stats stats;
809 struct ovs_header *ovs_header;
810 struct nlattr *nla;
811 unsigned long used;
812 u8 tcp_flags;
813 int err;
814
815 sf_acts = rcu_dereference_protected(flow->sf_acts,
816 lockdep_genl_is_held());
817
Eric W. Biederman15e47302012-09-07 20:12:54 +0000818 ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family, flags, cmd);
Jesse Grossccb13522011-10-25 19:26:31 -0700819 if (!ovs_header)
820 return -EMSGSIZE;
821
822 ovs_header->dp_ifindex = get_dpifindex(dp);
823
824 nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
825 if (!nla)
826 goto nla_put_failure;
827 err = ovs_flow_to_nlattrs(&flow->key, skb);
828 if (err)
829 goto error;
830 nla_nest_end(skb, nla);
831
832 spin_lock_bh(&flow->lock);
833 used = flow->used;
834 stats.n_packets = flow->packet_count;
835 stats.n_bytes = flow->byte_count;
836 tcp_flags = flow->tcp_flags;
837 spin_unlock_bh(&flow->lock);
838
David S. Miller028d6a62012-03-29 23:20:48 -0400839 if (used &&
840 nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
841 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -0700842
David S. Miller028d6a62012-03-29 23:20:48 -0400843 if (stats.n_packets &&
844 nla_put(skb, OVS_FLOW_ATTR_STATS,
845 sizeof(struct ovs_flow_stats), &stats))
846 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -0700847
David S. Miller028d6a62012-03-29 23:20:48 -0400848 if (tcp_flags &&
849 nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, tcp_flags))
850 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -0700851
852 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
853 * this is the first flow to be dumped into 'skb'. This is unusual for
854 * Netlink but individual action lists can be longer than
855 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
856 * The userspace caller can always fetch the actions separately if it
857 * really wants them. (Most userspace callers in fact don't care.)
858 *
859 * This can only fail for dump operations because the skb is always
860 * properly sized for single flows.
861 */
862 err = nla_put(skb, OVS_FLOW_ATTR_ACTIONS, sf_acts->actions_len,
863 sf_acts->actions);
864 if (err < 0 && skb_orig_len)
865 goto error;
866
867 return genlmsg_end(skb, ovs_header);
868
869nla_put_failure:
870 err = -EMSGSIZE;
871error:
872 genlmsg_cancel(skb, ovs_header);
873 return err;
874}
875
876static struct sk_buff *ovs_flow_cmd_alloc_info(struct sw_flow *flow)
877{
878 const struct sw_flow_actions *sf_acts;
879 int len;
880
881 sf_acts = rcu_dereference_protected(flow->sf_acts,
882 lockdep_genl_is_held());
883
884 /* OVS_FLOW_ATTR_KEY */
885 len = nla_total_size(FLOW_BUFSIZE);
886 /* OVS_FLOW_ATTR_ACTIONS */
887 len += nla_total_size(sf_acts->actions_len);
888 /* OVS_FLOW_ATTR_STATS */
889 len += nla_total_size(sizeof(struct ovs_flow_stats));
890 /* OVS_FLOW_ATTR_TCP_FLAGS */
891 len += nla_total_size(1);
892 /* OVS_FLOW_ATTR_USED */
893 len += nla_total_size(8);
894
895 len += NLMSG_ALIGN(sizeof(struct ovs_header));
896
897 return genlmsg_new(len, GFP_KERNEL);
898}
899
900static struct sk_buff *ovs_flow_cmd_build_info(struct sw_flow *flow,
901 struct datapath *dp,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000902 u32 portid, u32 seq, u8 cmd)
Jesse Grossccb13522011-10-25 19:26:31 -0700903{
904 struct sk_buff *skb;
905 int retval;
906
907 skb = ovs_flow_cmd_alloc_info(flow);
908 if (!skb)
909 return ERR_PTR(-ENOMEM);
910
Eric W. Biederman15e47302012-09-07 20:12:54 +0000911 retval = ovs_flow_cmd_fill_info(flow, dp, skb, portid, seq, 0, cmd);
Jesse Grossccb13522011-10-25 19:26:31 -0700912 BUG_ON(retval < 0);
913 return skb;
914}
915
916static int ovs_flow_cmd_new_or_set(struct sk_buff *skb, struct genl_info *info)
917{
918 struct nlattr **a = info->attrs;
919 struct ovs_header *ovs_header = info->userhdr;
920 struct sw_flow_key key;
921 struct sw_flow *flow;
922 struct sk_buff *reply;
923 struct datapath *dp;
924 struct flow_table *table;
925 int error;
926 int key_len;
927
928 /* Extract key. */
929 error = -EINVAL;
930 if (!a[OVS_FLOW_ATTR_KEY])
931 goto error;
932 error = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
933 if (error)
934 goto error;
935
936 /* Validate actions. */
937 if (a[OVS_FLOW_ATTR_ACTIONS]) {
938 error = validate_actions(a[OVS_FLOW_ATTR_ACTIONS], &key, 0);
939 if (error)
940 goto error;
941 } else if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW) {
942 error = -EINVAL;
943 goto error;
944 }
945
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800946 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -0700947 error = -ENODEV;
948 if (!dp)
949 goto error;
950
951 table = genl_dereference(dp->table);
952 flow = ovs_flow_tbl_lookup(table, &key, key_len);
953 if (!flow) {
954 struct sw_flow_actions *acts;
955
956 /* Bail out if we're not allowed to create a new flow. */
957 error = -ENOENT;
958 if (info->genlhdr->cmd == OVS_FLOW_CMD_SET)
959 goto error;
960
961 /* Expand table, if necessary, to make room. */
962 if (ovs_flow_tbl_need_to_expand(table)) {
963 struct flow_table *new_table;
964
965 new_table = ovs_flow_tbl_expand(table);
966 if (!IS_ERR(new_table)) {
967 rcu_assign_pointer(dp->table, new_table);
968 ovs_flow_tbl_deferred_destroy(table);
969 table = genl_dereference(dp->table);
970 }
971 }
972
973 /* Allocate flow. */
974 flow = ovs_flow_alloc();
975 if (IS_ERR(flow)) {
976 error = PTR_ERR(flow);
977 goto error;
978 }
979 flow->key = key;
980 clear_stats(flow);
981
982 /* Obtain actions. */
983 acts = ovs_flow_actions_alloc(a[OVS_FLOW_ATTR_ACTIONS]);
984 error = PTR_ERR(acts);
985 if (IS_ERR(acts))
986 goto error_free_flow;
987 rcu_assign_pointer(flow->sf_acts, acts);
988
989 /* Put flow in bucket. */
990 flow->hash = ovs_flow_hash(&key, key_len);
991 ovs_flow_tbl_insert(table, flow);
992
Eric W. Biederman15e47302012-09-07 20:12:54 +0000993 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
Jesse Grossccb13522011-10-25 19:26:31 -0700994 info->snd_seq,
995 OVS_FLOW_CMD_NEW);
996 } else {
997 /* We found a matching flow. */
998 struct sw_flow_actions *old_acts;
999 struct nlattr *acts_attrs;
1000
1001 /* Bail out if we're not allowed to modify an existing flow.
1002 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
1003 * because Generic Netlink treats the latter as a dump
1004 * request. We also accept NLM_F_EXCL in case that bug ever
1005 * gets fixed.
1006 */
1007 error = -EEXIST;
1008 if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW &&
1009 info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL))
1010 goto error;
1011
1012 /* Update actions. */
1013 old_acts = rcu_dereference_protected(flow->sf_acts,
1014 lockdep_genl_is_held());
1015 acts_attrs = a[OVS_FLOW_ATTR_ACTIONS];
1016 if (acts_attrs &&
1017 (old_acts->actions_len != nla_len(acts_attrs) ||
1018 memcmp(old_acts->actions, nla_data(acts_attrs),
1019 old_acts->actions_len))) {
1020 struct sw_flow_actions *new_acts;
1021
1022 new_acts = ovs_flow_actions_alloc(acts_attrs);
1023 error = PTR_ERR(new_acts);
1024 if (IS_ERR(new_acts))
1025 goto error;
1026
1027 rcu_assign_pointer(flow->sf_acts, new_acts);
1028 ovs_flow_deferred_free_acts(old_acts);
1029 }
1030
Eric W. Biederman15e47302012-09-07 20:12:54 +00001031 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001032 info->snd_seq, OVS_FLOW_CMD_NEW);
1033
1034 /* Clear stats. */
1035 if (a[OVS_FLOW_ATTR_CLEAR]) {
1036 spin_lock_bh(&flow->lock);
1037 clear_stats(flow);
1038 spin_unlock_bh(&flow->lock);
1039 }
1040 }
1041
1042 if (!IS_ERR(reply))
Eric W. Biederman15e47302012-09-07 20:12:54 +00001043 genl_notify(reply, genl_info_net(info), info->snd_portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001044 ovs_dp_flow_multicast_group.id, info->nlhdr,
1045 GFP_KERNEL);
1046 else
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001047 netlink_set_err(sock_net(skb->sk)->genl_sock, 0,
Jesse Grossccb13522011-10-25 19:26:31 -07001048 ovs_dp_flow_multicast_group.id, PTR_ERR(reply));
1049 return 0;
1050
1051error_free_flow:
1052 ovs_flow_free(flow);
1053error:
1054 return error;
1055}
1056
1057static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1058{
1059 struct nlattr **a = info->attrs;
1060 struct ovs_header *ovs_header = info->userhdr;
1061 struct sw_flow_key key;
1062 struct sk_buff *reply;
1063 struct sw_flow *flow;
1064 struct datapath *dp;
1065 struct flow_table *table;
1066 int err;
1067 int key_len;
1068
1069 if (!a[OVS_FLOW_ATTR_KEY])
1070 return -EINVAL;
1071 err = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
1072 if (err)
1073 return err;
1074
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001075 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001076 if (!dp)
1077 return -ENODEV;
1078
1079 table = genl_dereference(dp->table);
1080 flow = ovs_flow_tbl_lookup(table, &key, key_len);
1081 if (!flow)
1082 return -ENOENT;
1083
Eric W. Biederman15e47302012-09-07 20:12:54 +00001084 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001085 info->snd_seq, OVS_FLOW_CMD_NEW);
1086 if (IS_ERR(reply))
1087 return PTR_ERR(reply);
1088
1089 return genlmsg_reply(reply, info);
1090}
1091
1092static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1093{
1094 struct nlattr **a = info->attrs;
1095 struct ovs_header *ovs_header = info->userhdr;
1096 struct sw_flow_key key;
1097 struct sk_buff *reply;
1098 struct sw_flow *flow;
1099 struct datapath *dp;
1100 struct flow_table *table;
1101 int err;
1102 int key_len;
1103
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001104 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1105 if (!dp)
1106 return -ENODEV;
1107
Jesse Grossccb13522011-10-25 19:26:31 -07001108 if (!a[OVS_FLOW_ATTR_KEY])
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001109 return flush_flows(dp);
1110
Jesse Grossccb13522011-10-25 19:26:31 -07001111 err = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
1112 if (err)
1113 return err;
1114
Jesse Grossccb13522011-10-25 19:26:31 -07001115 table = genl_dereference(dp->table);
1116 flow = ovs_flow_tbl_lookup(table, &key, key_len);
1117 if (!flow)
1118 return -ENOENT;
1119
1120 reply = ovs_flow_cmd_alloc_info(flow);
1121 if (!reply)
1122 return -ENOMEM;
1123
1124 ovs_flow_tbl_remove(table, flow);
1125
Eric W. Biederman15e47302012-09-07 20:12:54 +00001126 err = ovs_flow_cmd_fill_info(flow, dp, reply, info->snd_portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001127 info->snd_seq, 0, OVS_FLOW_CMD_DEL);
1128 BUG_ON(err < 0);
1129
1130 ovs_flow_deferred_free(flow);
1131
Eric W. Biederman15e47302012-09-07 20:12:54 +00001132 genl_notify(reply, genl_info_net(info), info->snd_portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001133 ovs_dp_flow_multicast_group.id, info->nlhdr, GFP_KERNEL);
1134 return 0;
1135}
1136
1137static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1138{
1139 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1140 struct datapath *dp;
1141 struct flow_table *table;
1142
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001143 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001144 if (!dp)
1145 return -ENODEV;
1146
1147 table = genl_dereference(dp->table);
1148
1149 for (;;) {
1150 struct sw_flow *flow;
1151 u32 bucket, obj;
1152
1153 bucket = cb->args[0];
1154 obj = cb->args[1];
1155 flow = ovs_flow_tbl_next(table, &bucket, &obj);
1156 if (!flow)
1157 break;
1158
1159 if (ovs_flow_cmd_fill_info(flow, dp, skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001160 NETLINK_CB(cb->skb).portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001161 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1162 OVS_FLOW_CMD_NEW) < 0)
1163 break;
1164
1165 cb->args[0] = bucket;
1166 cb->args[1] = obj;
1167 }
1168 return skb->len;
1169}
1170
1171static struct genl_ops dp_flow_genl_ops[] = {
1172 { .cmd = OVS_FLOW_CMD_NEW,
1173 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1174 .policy = flow_policy,
1175 .doit = ovs_flow_cmd_new_or_set
1176 },
1177 { .cmd = OVS_FLOW_CMD_DEL,
1178 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1179 .policy = flow_policy,
1180 .doit = ovs_flow_cmd_del
1181 },
1182 { .cmd = OVS_FLOW_CMD_GET,
1183 .flags = 0, /* OK for unprivileged users. */
1184 .policy = flow_policy,
1185 .doit = ovs_flow_cmd_get,
1186 .dumpit = ovs_flow_cmd_dump
1187 },
1188 { .cmd = OVS_FLOW_CMD_SET,
1189 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1190 .policy = flow_policy,
1191 .doit = ovs_flow_cmd_new_or_set,
1192 },
1193};
1194
1195static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1196 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1197 [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1198};
1199
1200static struct genl_family dp_datapath_genl_family = {
1201 .id = GENL_ID_GENERATE,
1202 .hdrsize = sizeof(struct ovs_header),
1203 .name = OVS_DATAPATH_FAMILY,
1204 .version = OVS_DATAPATH_VERSION,
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001205 .maxattr = OVS_DP_ATTR_MAX,
1206 .netnsok = true
Jesse Grossccb13522011-10-25 19:26:31 -07001207};
1208
1209static struct genl_multicast_group ovs_dp_datapath_multicast_group = {
1210 .name = OVS_DATAPATH_MCGROUP
1211};
1212
1213static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001214 u32 portid, u32 seq, u32 flags, u8 cmd)
Jesse Grossccb13522011-10-25 19:26:31 -07001215{
1216 struct ovs_header *ovs_header;
1217 struct ovs_dp_stats dp_stats;
1218 int err;
1219
Eric W. Biederman15e47302012-09-07 20:12:54 +00001220 ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
Jesse Grossccb13522011-10-25 19:26:31 -07001221 flags, cmd);
1222 if (!ovs_header)
1223 goto error;
1224
1225 ovs_header->dp_ifindex = get_dpifindex(dp);
1226
1227 rcu_read_lock();
1228 err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
1229 rcu_read_unlock();
1230 if (err)
1231 goto nla_put_failure;
1232
1233 get_dp_stats(dp, &dp_stats);
David S. Miller028d6a62012-03-29 23:20:48 -04001234 if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats), &dp_stats))
1235 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001236
1237 return genlmsg_end(skb, ovs_header);
1238
1239nla_put_failure:
1240 genlmsg_cancel(skb, ovs_header);
1241error:
1242 return -EMSGSIZE;
1243}
1244
Eric W. Biederman15e47302012-09-07 20:12:54 +00001245static struct sk_buff *ovs_dp_cmd_build_info(struct datapath *dp, u32 portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001246 u32 seq, u8 cmd)
1247{
1248 struct sk_buff *skb;
1249 int retval;
1250
1251 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1252 if (!skb)
1253 return ERR_PTR(-ENOMEM);
1254
Eric W. Biederman15e47302012-09-07 20:12:54 +00001255 retval = ovs_dp_cmd_fill_info(dp, skb, portid, seq, 0, cmd);
Jesse Grossccb13522011-10-25 19:26:31 -07001256 if (retval < 0) {
1257 kfree_skb(skb);
1258 return ERR_PTR(retval);
1259 }
1260 return skb;
1261}
1262
1263/* Called with genl_mutex and optionally with RTNL lock also. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001264static struct datapath *lookup_datapath(struct net *net,
1265 struct ovs_header *ovs_header,
Jesse Grossccb13522011-10-25 19:26:31 -07001266 struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1267{
1268 struct datapath *dp;
1269
1270 if (!a[OVS_DP_ATTR_NAME])
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001271 dp = get_dp(net, ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001272 else {
1273 struct vport *vport;
1274
1275 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001276 vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
Jesse Grossccb13522011-10-25 19:26:31 -07001277 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
1278 rcu_read_unlock();
1279 }
1280 return dp ? dp : ERR_PTR(-ENODEV);
1281}
1282
1283static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1284{
1285 struct nlattr **a = info->attrs;
1286 struct vport_parms parms;
1287 struct sk_buff *reply;
1288 struct datapath *dp;
1289 struct vport *vport;
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001290 struct ovs_net *ovs_net;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001291 int err, i;
Jesse Grossccb13522011-10-25 19:26:31 -07001292
1293 err = -EINVAL;
1294 if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1295 goto err;
1296
1297 rtnl_lock();
Jesse Grossccb13522011-10-25 19:26:31 -07001298
1299 err = -ENOMEM;
1300 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1301 if (dp == NULL)
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001302 goto err_unlock_rtnl;
1303
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001304 ovs_dp_set_net(dp, hold_net(sock_net(skb->sk)));
Jesse Grossccb13522011-10-25 19:26:31 -07001305
1306 /* Allocate table. */
1307 err = -ENOMEM;
1308 rcu_assign_pointer(dp->table, ovs_flow_tbl_alloc(TBL_MIN_BUCKETS));
1309 if (!dp->table)
1310 goto err_free_dp;
1311
1312 dp->stats_percpu = alloc_percpu(struct dp_stats_percpu);
1313 if (!dp->stats_percpu) {
1314 err = -ENOMEM;
1315 goto err_destroy_table;
1316 }
1317
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001318 dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
1319 GFP_KERNEL);
1320 if (!dp->ports) {
1321 err = -ENOMEM;
1322 goto err_destroy_percpu;
1323 }
1324
1325 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1326 INIT_HLIST_HEAD(&dp->ports[i]);
1327
Jesse Grossccb13522011-10-25 19:26:31 -07001328 /* Set up our datapath device. */
1329 parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1330 parms.type = OVS_VPORT_TYPE_INTERNAL;
1331 parms.options = NULL;
1332 parms.dp = dp;
1333 parms.port_no = OVSP_LOCAL;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001334 parms.upcall_portid = nla_get_u32(a[OVS_DP_ATTR_UPCALL_PID]);
Jesse Grossccb13522011-10-25 19:26:31 -07001335
1336 vport = new_vport(&parms);
1337 if (IS_ERR(vport)) {
1338 err = PTR_ERR(vport);
1339 if (err == -EBUSY)
1340 err = -EEXIST;
1341
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001342 goto err_destroy_ports_array;
Jesse Grossccb13522011-10-25 19:26:31 -07001343 }
1344
Eric W. Biederman15e47302012-09-07 20:12:54 +00001345 reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001346 info->snd_seq, OVS_DP_CMD_NEW);
1347 err = PTR_ERR(reply);
1348 if (IS_ERR(reply))
1349 goto err_destroy_local_port;
1350
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001351 ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
1352 list_add_tail(&dp->list_node, &ovs_net->dps);
Jesse Grossccb13522011-10-25 19:26:31 -07001353 rtnl_unlock();
1354
Eric W. Biederman15e47302012-09-07 20:12:54 +00001355 genl_notify(reply, genl_info_net(info), info->snd_portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001356 ovs_dp_datapath_multicast_group.id, info->nlhdr,
1357 GFP_KERNEL);
1358 return 0;
1359
1360err_destroy_local_port:
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001361 ovs_dp_detach_port(ovs_vport_rtnl(dp, OVSP_LOCAL));
1362err_destroy_ports_array:
1363 kfree(dp->ports);
Jesse Grossccb13522011-10-25 19:26:31 -07001364err_destroy_percpu:
1365 free_percpu(dp->stats_percpu);
1366err_destroy_table:
1367 ovs_flow_tbl_destroy(genl_dereference(dp->table));
1368err_free_dp:
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001369 release_net(ovs_dp_get_net(dp));
Jesse Grossccb13522011-10-25 19:26:31 -07001370 kfree(dp);
Jesse Grossccb13522011-10-25 19:26:31 -07001371err_unlock_rtnl:
1372 rtnl_unlock();
1373err:
1374 return err;
1375}
1376
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001377/* Called with genl_mutex. */
1378static void __dp_destroy(struct datapath *dp)
Jesse Grossccb13522011-10-25 19:26:31 -07001379{
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001380 int i;
Jesse Grossccb13522011-10-25 19:26:31 -07001381
1382 rtnl_lock();
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001383
1384 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1385 struct vport *vport;
1386 struct hlist_node *node, *n;
1387
1388 hlist_for_each_entry_safe(vport, node, n, &dp->ports[i], dp_hash_node)
1389 if (vport->port_no != OVSP_LOCAL)
1390 ovs_dp_detach_port(vport);
1391 }
Jesse Grossccb13522011-10-25 19:26:31 -07001392
1393 list_del(&dp->list_node);
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001394 ovs_dp_detach_port(ovs_vport_rtnl(dp, OVSP_LOCAL));
Jesse Grossccb13522011-10-25 19:26:31 -07001395
1396 /* rtnl_unlock() will wait until all the references to devices that
1397 * are pending unregistration have been dropped. We do it here to
1398 * ensure that any internal devices (which contain DP pointers) are
1399 * fully destroyed before freeing the datapath.
1400 */
1401 rtnl_unlock();
1402
1403 call_rcu(&dp->rcu, destroy_dp_rcu);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001404}
1405
1406static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1407{
1408 struct sk_buff *reply;
1409 struct datapath *dp;
1410 int err;
1411
1412 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1413 err = PTR_ERR(dp);
1414 if (IS_ERR(dp))
1415 return err;
1416
Eric W. Biederman15e47302012-09-07 20:12:54 +00001417 reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001418 info->snd_seq, OVS_DP_CMD_DEL);
1419 err = PTR_ERR(reply);
1420 if (IS_ERR(reply))
1421 return err;
1422
1423 __dp_destroy(dp);
Jesse Grossccb13522011-10-25 19:26:31 -07001424
Eric W. Biederman15e47302012-09-07 20:12:54 +00001425 genl_notify(reply, genl_info_net(info), info->snd_portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001426 ovs_dp_datapath_multicast_group.id, info->nlhdr,
1427 GFP_KERNEL);
1428
1429 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001430}
1431
1432static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1433{
1434 struct sk_buff *reply;
1435 struct datapath *dp;
1436 int err;
1437
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001438 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
Jesse Grossccb13522011-10-25 19:26:31 -07001439 if (IS_ERR(dp))
1440 return PTR_ERR(dp);
1441
Eric W. Biederman15e47302012-09-07 20:12:54 +00001442 reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001443 info->snd_seq, OVS_DP_CMD_NEW);
1444 if (IS_ERR(reply)) {
1445 err = PTR_ERR(reply);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001446 netlink_set_err(sock_net(skb->sk)->genl_sock, 0,
Jesse Grossccb13522011-10-25 19:26:31 -07001447 ovs_dp_datapath_multicast_group.id, err);
1448 return 0;
1449 }
1450
Eric W. Biederman15e47302012-09-07 20:12:54 +00001451 genl_notify(reply, genl_info_net(info), info->snd_portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001452 ovs_dp_datapath_multicast_group.id, info->nlhdr,
1453 GFP_KERNEL);
1454
1455 return 0;
1456}
1457
1458static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1459{
1460 struct sk_buff *reply;
1461 struct datapath *dp;
1462
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001463 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
Jesse Grossccb13522011-10-25 19:26:31 -07001464 if (IS_ERR(dp))
1465 return PTR_ERR(dp);
1466
Eric W. Biederman15e47302012-09-07 20:12:54 +00001467 reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001468 info->snd_seq, OVS_DP_CMD_NEW);
1469 if (IS_ERR(reply))
1470 return PTR_ERR(reply);
1471
1472 return genlmsg_reply(reply, info);
1473}
1474
1475static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1476{
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001477 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
Jesse Grossccb13522011-10-25 19:26:31 -07001478 struct datapath *dp;
1479 int skip = cb->args[0];
1480 int i = 0;
1481
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001482 list_for_each_entry(dp, &ovs_net->dps, list_node) {
Ben Pfaff77676fd2012-01-17 13:33:39 +00001483 if (i >= skip &&
Eric W. Biederman15e47302012-09-07 20:12:54 +00001484 ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001485 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1486 OVS_DP_CMD_NEW) < 0)
1487 break;
1488 i++;
1489 }
1490
1491 cb->args[0] = i;
1492
1493 return skb->len;
1494}
1495
1496static struct genl_ops dp_datapath_genl_ops[] = {
1497 { .cmd = OVS_DP_CMD_NEW,
1498 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1499 .policy = datapath_policy,
1500 .doit = ovs_dp_cmd_new
1501 },
1502 { .cmd = OVS_DP_CMD_DEL,
1503 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1504 .policy = datapath_policy,
1505 .doit = ovs_dp_cmd_del
1506 },
1507 { .cmd = OVS_DP_CMD_GET,
1508 .flags = 0, /* OK for unprivileged users. */
1509 .policy = datapath_policy,
1510 .doit = ovs_dp_cmd_get,
1511 .dumpit = ovs_dp_cmd_dump
1512 },
1513 { .cmd = OVS_DP_CMD_SET,
1514 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1515 .policy = datapath_policy,
1516 .doit = ovs_dp_cmd_set,
1517 },
1518};
1519
1520static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
1521 [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1522 [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
1523 [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
1524 [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
1525 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1526 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
1527};
1528
1529static struct genl_family dp_vport_genl_family = {
1530 .id = GENL_ID_GENERATE,
1531 .hdrsize = sizeof(struct ovs_header),
1532 .name = OVS_VPORT_FAMILY,
1533 .version = OVS_VPORT_VERSION,
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001534 .maxattr = OVS_VPORT_ATTR_MAX,
1535 .netnsok = true
Jesse Grossccb13522011-10-25 19:26:31 -07001536};
1537
1538struct genl_multicast_group ovs_dp_vport_multicast_group = {
1539 .name = OVS_VPORT_MCGROUP
1540};
1541
1542/* Called with RTNL lock or RCU read lock. */
1543static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001544 u32 portid, u32 seq, u32 flags, u8 cmd)
Jesse Grossccb13522011-10-25 19:26:31 -07001545{
1546 struct ovs_header *ovs_header;
1547 struct ovs_vport_stats vport_stats;
1548 int err;
1549
Eric W. Biederman15e47302012-09-07 20:12:54 +00001550 ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
Jesse Grossccb13522011-10-25 19:26:31 -07001551 flags, cmd);
1552 if (!ovs_header)
1553 return -EMSGSIZE;
1554
1555 ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1556
David S. Miller028d6a62012-03-29 23:20:48 -04001557 if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
1558 nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
1559 nla_put_string(skb, OVS_VPORT_ATTR_NAME, vport->ops->get_name(vport)) ||
Eric W. Biederman15e47302012-09-07 20:12:54 +00001560 nla_put_u32(skb, OVS_VPORT_ATTR_UPCALL_PID, vport->upcall_portid))
David S. Miller028d6a62012-03-29 23:20:48 -04001561 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001562
1563 ovs_vport_get_stats(vport, &vport_stats);
David S. Miller028d6a62012-03-29 23:20:48 -04001564 if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
1565 &vport_stats))
1566 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001567
1568 err = ovs_vport_get_options(vport, skb);
1569 if (err == -EMSGSIZE)
1570 goto error;
1571
1572 return genlmsg_end(skb, ovs_header);
1573
1574nla_put_failure:
1575 err = -EMSGSIZE;
1576error:
1577 genlmsg_cancel(skb, ovs_header);
1578 return err;
1579}
1580
1581/* Called with RTNL lock or RCU read lock. */
Eric W. Biederman15e47302012-09-07 20:12:54 +00001582struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001583 u32 seq, u8 cmd)
1584{
1585 struct sk_buff *skb;
1586 int retval;
1587
1588 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1589 if (!skb)
1590 return ERR_PTR(-ENOMEM);
1591
Eric W. Biederman15e47302012-09-07 20:12:54 +00001592 retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
Jesse Grossccb13522011-10-25 19:26:31 -07001593 if (retval < 0) {
1594 kfree_skb(skb);
1595 return ERR_PTR(retval);
1596 }
1597 return skb;
1598}
1599
1600/* Called with RTNL lock or RCU read lock. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001601static struct vport *lookup_vport(struct net *net,
1602 struct ovs_header *ovs_header,
Jesse Grossccb13522011-10-25 19:26:31 -07001603 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1604{
1605 struct datapath *dp;
1606 struct vport *vport;
1607
1608 if (a[OVS_VPORT_ATTR_NAME]) {
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001609 vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
Jesse Grossccb13522011-10-25 19:26:31 -07001610 if (!vport)
1611 return ERR_PTR(-ENODEV);
Ben Pfaff651a68e2012-03-06 15:04:04 -08001612 if (ovs_header->dp_ifindex &&
1613 ovs_header->dp_ifindex != get_dpifindex(vport->dp))
1614 return ERR_PTR(-ENODEV);
Jesse Grossccb13522011-10-25 19:26:31 -07001615 return vport;
1616 } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
1617 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1618
1619 if (port_no >= DP_MAX_PORTS)
1620 return ERR_PTR(-EFBIG);
1621
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001622 dp = get_dp(net, ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001623 if (!dp)
1624 return ERR_PTR(-ENODEV);
1625
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001626 vport = ovs_vport_rtnl_rcu(dp, port_no);
Jesse Grossccb13522011-10-25 19:26:31 -07001627 if (!vport)
1628 return ERR_PTR(-ENOENT);
1629 return vport;
1630 } else
1631 return ERR_PTR(-EINVAL);
1632}
1633
1634static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
1635{
1636 struct nlattr **a = info->attrs;
1637 struct ovs_header *ovs_header = info->userhdr;
1638 struct vport_parms parms;
1639 struct sk_buff *reply;
1640 struct vport *vport;
1641 struct datapath *dp;
1642 u32 port_no;
1643 int err;
1644
1645 err = -EINVAL;
1646 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
1647 !a[OVS_VPORT_ATTR_UPCALL_PID])
1648 goto exit;
1649
1650 rtnl_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001651 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001652 err = -ENODEV;
1653 if (!dp)
1654 goto exit_unlock;
1655
1656 if (a[OVS_VPORT_ATTR_PORT_NO]) {
1657 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1658
1659 err = -EFBIG;
1660 if (port_no >= DP_MAX_PORTS)
1661 goto exit_unlock;
1662
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001663 vport = ovs_vport_rtnl_rcu(dp, port_no);
Jesse Grossccb13522011-10-25 19:26:31 -07001664 err = -EBUSY;
1665 if (vport)
1666 goto exit_unlock;
1667 } else {
1668 for (port_no = 1; ; port_no++) {
1669 if (port_no >= DP_MAX_PORTS) {
1670 err = -EFBIG;
1671 goto exit_unlock;
1672 }
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001673 vport = ovs_vport_rtnl(dp, port_no);
Jesse Grossccb13522011-10-25 19:26:31 -07001674 if (!vport)
1675 break;
1676 }
1677 }
1678
1679 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
1680 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1681 parms.options = a[OVS_VPORT_ATTR_OPTIONS];
1682 parms.dp = dp;
1683 parms.port_no = port_no;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001684 parms.upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
Jesse Grossccb13522011-10-25 19:26:31 -07001685
1686 vport = new_vport(&parms);
1687 err = PTR_ERR(vport);
1688 if (IS_ERR(vport))
1689 goto exit_unlock;
1690
Eric W. Biederman15e47302012-09-07 20:12:54 +00001691 reply = ovs_vport_cmd_build_info(vport, info->snd_portid, info->snd_seq,
Jesse Grossccb13522011-10-25 19:26:31 -07001692 OVS_VPORT_CMD_NEW);
1693 if (IS_ERR(reply)) {
1694 err = PTR_ERR(reply);
1695 ovs_dp_detach_port(vport);
1696 goto exit_unlock;
1697 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00001698 genl_notify(reply, genl_info_net(info), info->snd_portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001699 ovs_dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
1700
1701exit_unlock:
1702 rtnl_unlock();
1703exit:
1704 return err;
1705}
1706
1707static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
1708{
1709 struct nlattr **a = info->attrs;
1710 struct sk_buff *reply;
1711 struct vport *vport;
1712 int err;
1713
1714 rtnl_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001715 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
Jesse Grossccb13522011-10-25 19:26:31 -07001716 err = PTR_ERR(vport);
1717 if (IS_ERR(vport))
1718 goto exit_unlock;
1719
1720 err = 0;
1721 if (a[OVS_VPORT_ATTR_TYPE] &&
1722 nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type)
1723 err = -EINVAL;
1724
1725 if (!err && a[OVS_VPORT_ATTR_OPTIONS])
1726 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
Ansis Atteka03fbf8b2012-04-09 12:12:12 -07001727 if (err)
1728 goto exit_unlock;
1729 if (a[OVS_VPORT_ATTR_UPCALL_PID])
Eric W. Biederman15e47302012-09-07 20:12:54 +00001730 vport->upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
Jesse Grossccb13522011-10-25 19:26:31 -07001731
Eric W. Biederman15e47302012-09-07 20:12:54 +00001732 reply = ovs_vport_cmd_build_info(vport, info->snd_portid, info->snd_seq,
Jesse Grossccb13522011-10-25 19:26:31 -07001733 OVS_VPORT_CMD_NEW);
1734 if (IS_ERR(reply)) {
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001735 netlink_set_err(sock_net(skb->sk)->genl_sock, 0,
Ansis Atteka4cb6e112012-05-03 18:40:38 -07001736 ovs_dp_vport_multicast_group.id, PTR_ERR(reply));
1737 goto exit_unlock;
Jesse Grossccb13522011-10-25 19:26:31 -07001738 }
1739
Eric W. Biederman15e47302012-09-07 20:12:54 +00001740 genl_notify(reply, genl_info_net(info), info->snd_portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001741 ovs_dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
1742
1743exit_unlock:
1744 rtnl_unlock();
1745 return err;
1746}
1747
1748static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
1749{
1750 struct nlattr **a = info->attrs;
1751 struct sk_buff *reply;
1752 struct vport *vport;
1753 int err;
1754
1755 rtnl_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001756 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
Jesse Grossccb13522011-10-25 19:26:31 -07001757 err = PTR_ERR(vport);
1758 if (IS_ERR(vport))
1759 goto exit_unlock;
1760
1761 if (vport->port_no == OVSP_LOCAL) {
1762 err = -EINVAL;
1763 goto exit_unlock;
1764 }
1765
Eric W. Biederman15e47302012-09-07 20:12:54 +00001766 reply = ovs_vport_cmd_build_info(vport, info->snd_portid, info->snd_seq,
Jesse Grossccb13522011-10-25 19:26:31 -07001767 OVS_VPORT_CMD_DEL);
1768 err = PTR_ERR(reply);
1769 if (IS_ERR(reply))
1770 goto exit_unlock;
1771
1772 ovs_dp_detach_port(vport);
1773
Eric W. Biederman15e47302012-09-07 20:12:54 +00001774 genl_notify(reply, genl_info_net(info), info->snd_portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001775 ovs_dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
1776
1777exit_unlock:
1778 rtnl_unlock();
1779 return err;
1780}
1781
1782static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
1783{
1784 struct nlattr **a = info->attrs;
1785 struct ovs_header *ovs_header = info->userhdr;
1786 struct sk_buff *reply;
1787 struct vport *vport;
1788 int err;
1789
1790 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001791 vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
Jesse Grossccb13522011-10-25 19:26:31 -07001792 err = PTR_ERR(vport);
1793 if (IS_ERR(vport))
1794 goto exit_unlock;
1795
Eric W. Biederman15e47302012-09-07 20:12:54 +00001796 reply = ovs_vport_cmd_build_info(vport, info->snd_portid, info->snd_seq,
Jesse Grossccb13522011-10-25 19:26:31 -07001797 OVS_VPORT_CMD_NEW);
1798 err = PTR_ERR(reply);
1799 if (IS_ERR(reply))
1800 goto exit_unlock;
1801
1802 rcu_read_unlock();
1803
1804 return genlmsg_reply(reply, info);
1805
1806exit_unlock:
1807 rcu_read_unlock();
1808 return err;
1809}
1810
1811static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1812{
1813 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1814 struct datapath *dp;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001815 int bucket = cb->args[0], skip = cb->args[1];
1816 int i, j = 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001817
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001818 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001819 if (!dp)
1820 return -ENODEV;
1821
1822 rcu_read_lock();
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001823 for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
Jesse Grossccb13522011-10-25 19:26:31 -07001824 struct vport *vport;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001825 struct hlist_node *n;
Jesse Grossccb13522011-10-25 19:26:31 -07001826
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001827 j = 0;
1828 hlist_for_each_entry_rcu(vport, n, &dp->ports[i], dp_hash_node) {
1829 if (j >= skip &&
1830 ovs_vport_cmd_fill_info(vport, skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001831 NETLINK_CB(cb->skb).portid,
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001832 cb->nlh->nlmsg_seq,
1833 NLM_F_MULTI,
1834 OVS_VPORT_CMD_NEW) < 0)
1835 goto out;
Jesse Grossccb13522011-10-25 19:26:31 -07001836
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001837 j++;
1838 }
1839 skip = 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001840 }
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001841out:
Jesse Grossccb13522011-10-25 19:26:31 -07001842 rcu_read_unlock();
1843
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001844 cb->args[0] = i;
1845 cb->args[1] = j;
Jesse Grossccb13522011-10-25 19:26:31 -07001846
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001847 return skb->len;
Jesse Grossccb13522011-10-25 19:26:31 -07001848}
1849
Jesse Grossccb13522011-10-25 19:26:31 -07001850static struct genl_ops dp_vport_genl_ops[] = {
1851 { .cmd = OVS_VPORT_CMD_NEW,
1852 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1853 .policy = vport_policy,
1854 .doit = ovs_vport_cmd_new
1855 },
1856 { .cmd = OVS_VPORT_CMD_DEL,
1857 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1858 .policy = vport_policy,
1859 .doit = ovs_vport_cmd_del
1860 },
1861 { .cmd = OVS_VPORT_CMD_GET,
1862 .flags = 0, /* OK for unprivileged users. */
1863 .policy = vport_policy,
1864 .doit = ovs_vport_cmd_get,
1865 .dumpit = ovs_vport_cmd_dump
1866 },
1867 { .cmd = OVS_VPORT_CMD_SET,
1868 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1869 .policy = vport_policy,
1870 .doit = ovs_vport_cmd_set,
1871 },
1872};
1873
1874struct genl_family_and_ops {
1875 struct genl_family *family;
1876 struct genl_ops *ops;
1877 int n_ops;
1878 struct genl_multicast_group *group;
1879};
1880
1881static const struct genl_family_and_ops dp_genl_families[] = {
1882 { &dp_datapath_genl_family,
1883 dp_datapath_genl_ops, ARRAY_SIZE(dp_datapath_genl_ops),
1884 &ovs_dp_datapath_multicast_group },
1885 { &dp_vport_genl_family,
1886 dp_vport_genl_ops, ARRAY_SIZE(dp_vport_genl_ops),
1887 &ovs_dp_vport_multicast_group },
1888 { &dp_flow_genl_family,
1889 dp_flow_genl_ops, ARRAY_SIZE(dp_flow_genl_ops),
1890 &ovs_dp_flow_multicast_group },
1891 { &dp_packet_genl_family,
1892 dp_packet_genl_ops, ARRAY_SIZE(dp_packet_genl_ops),
1893 NULL },
1894};
1895
1896static void dp_unregister_genl(int n_families)
1897{
1898 int i;
1899
1900 for (i = 0; i < n_families; i++)
1901 genl_unregister_family(dp_genl_families[i].family);
1902}
1903
1904static int dp_register_genl(void)
1905{
1906 int n_registered;
1907 int err;
1908 int i;
1909
1910 n_registered = 0;
1911 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
1912 const struct genl_family_and_ops *f = &dp_genl_families[i];
1913
1914 err = genl_register_family_with_ops(f->family, f->ops,
1915 f->n_ops);
1916 if (err)
1917 goto error;
1918 n_registered++;
1919
1920 if (f->group) {
1921 err = genl_register_mc_group(f->family, f->group);
1922 if (err)
1923 goto error;
1924 }
1925 }
1926
1927 return 0;
1928
1929error:
1930 dp_unregister_genl(n_registered);
1931 return err;
1932}
1933
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001934static void rehash_flow_table(struct work_struct *work)
1935{
1936 struct datapath *dp;
1937 struct net *net;
1938
1939 genl_lock();
1940 rtnl_lock();
1941 for_each_net(net) {
1942 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
1943
1944 list_for_each_entry(dp, &ovs_net->dps, list_node) {
1945 struct flow_table *old_table = genl_dereference(dp->table);
1946 struct flow_table *new_table;
1947
1948 new_table = ovs_flow_tbl_rehash(old_table);
1949 if (!IS_ERR(new_table)) {
1950 rcu_assign_pointer(dp->table, new_table);
1951 ovs_flow_tbl_deferred_destroy(old_table);
1952 }
1953 }
1954 }
1955 rtnl_unlock();
1956 genl_unlock();
1957
1958 schedule_delayed_work(&rehash_flow_wq, REHASH_FLOW_INTERVAL);
1959}
1960
1961static int __net_init ovs_init_net(struct net *net)
1962{
1963 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
1964
1965 INIT_LIST_HEAD(&ovs_net->dps);
1966 return 0;
1967}
1968
1969static void __net_exit ovs_exit_net(struct net *net)
1970{
1971 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
1972 struct datapath *dp, *dp_next;
1973
1974 genl_lock();
1975 list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
1976 __dp_destroy(dp);
1977 genl_unlock();
1978}
1979
1980static struct pernet_operations ovs_net_ops = {
1981 .init = ovs_init_net,
1982 .exit = ovs_exit_net,
1983 .id = &ovs_net_id,
1984 .size = sizeof(struct ovs_net),
1985};
1986
Jesse Grossccb13522011-10-25 19:26:31 -07001987static int __init dp_init(void)
1988{
1989 struct sk_buff *dummy_skb;
1990 int err;
1991
1992 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > sizeof(dummy_skb->cb));
1993
1994 pr_info("Open vSwitch switching datapath\n");
1995
1996 err = ovs_flow_init();
1997 if (err)
1998 goto error;
1999
2000 err = ovs_vport_init();
2001 if (err)
2002 goto error_flow_exit;
2003
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002004 err = register_pernet_device(&ovs_net_ops);
Jesse Grossccb13522011-10-25 19:26:31 -07002005 if (err)
2006 goto error_vport_exit;
2007
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002008 err = register_netdevice_notifier(&ovs_dp_device_notifier);
2009 if (err)
2010 goto error_netns_exit;
2011
Jesse Grossccb13522011-10-25 19:26:31 -07002012 err = dp_register_genl();
2013 if (err < 0)
2014 goto error_unreg_notifier;
2015
2016 schedule_delayed_work(&rehash_flow_wq, REHASH_FLOW_INTERVAL);
2017
2018 return 0;
2019
2020error_unreg_notifier:
2021 unregister_netdevice_notifier(&ovs_dp_device_notifier);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002022error_netns_exit:
2023 unregister_pernet_device(&ovs_net_ops);
Jesse Grossccb13522011-10-25 19:26:31 -07002024error_vport_exit:
2025 ovs_vport_exit();
2026error_flow_exit:
2027 ovs_flow_exit();
2028error:
2029 return err;
2030}
2031
2032static void dp_cleanup(void)
2033{
2034 cancel_delayed_work_sync(&rehash_flow_wq);
Jesse Grossccb13522011-10-25 19:26:31 -07002035 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
2036 unregister_netdevice_notifier(&ovs_dp_device_notifier);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002037 unregister_pernet_device(&ovs_net_ops);
2038 rcu_barrier();
Jesse Grossccb13522011-10-25 19:26:31 -07002039 ovs_vport_exit();
2040 ovs_flow_exit();
2041}
2042
2043module_init(dp_init);
2044module_exit(dp_cleanup);
2045
2046MODULE_DESCRIPTION("Open vSwitch switching datapath");
2047MODULE_LICENSE("GPL");