blob: 49ee37b83a2923db4dab683ca971665681800643 [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;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700161 struct hlist_head *head;
162
163 head = vport_hash_bucket(dp, port_no);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800164 hlist_for_each_entry_rcu(vport, head, dp_hash_node) {
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700165 if (vport->port_no == port_no)
166 return vport;
167 }
168 return NULL;
169}
170
Jesse Grossccb13522011-10-25 19:26:31 -0700171/* Called with RTNL lock and genl_lock. */
172static struct vport *new_vport(const struct vport_parms *parms)
173{
174 struct vport *vport;
175
176 vport = ovs_vport_add(parms);
177 if (!IS_ERR(vport)) {
178 struct datapath *dp = parms->dp;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700179 struct hlist_head *head = vport_hash_bucket(dp, vport->port_no);
Jesse Grossccb13522011-10-25 19:26:31 -0700180
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700181 hlist_add_head_rcu(&vport->dp_hash_node, head);
Jesse Grossccb13522011-10-25 19:26:31 -0700182 }
183
184 return vport;
185}
186
187/* Called with RTNL lock. */
188void ovs_dp_detach_port(struct vport *p)
189{
190 ASSERT_RTNL();
191
192 /* First drop references to device. */
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700193 hlist_del_rcu(&p->dp_hash_node);
Jesse Grossccb13522011-10-25 19:26:31 -0700194
195 /* Then destroy it. */
196 ovs_vport_del(p);
197}
198
199/* Must be called with rcu_read_lock. */
200void ovs_dp_process_received_packet(struct vport *p, struct sk_buff *skb)
201{
202 struct datapath *dp = p->dp;
203 struct sw_flow *flow;
204 struct dp_stats_percpu *stats;
205 struct sw_flow_key key;
206 u64 *stats_counter;
207 int error;
208 int key_len;
209
Shan Wei404f2f12012-11-13 09:52:25 +0800210 stats = this_cpu_ptr(dp->stats_percpu);
Jesse Grossccb13522011-10-25 19:26:31 -0700211
212 /* Extract flow from 'skb' into 'key'. */
213 error = ovs_flow_extract(skb, p->port_no, &key, &key_len);
214 if (unlikely(error)) {
215 kfree_skb(skb);
216 return;
217 }
218
219 /* Look up flow. */
220 flow = ovs_flow_tbl_lookup(rcu_dereference(dp->table), &key, key_len);
221 if (unlikely(!flow)) {
222 struct dp_upcall_info upcall;
223
224 upcall.cmd = OVS_PACKET_CMD_MISS;
225 upcall.key = &key;
226 upcall.userdata = NULL;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000227 upcall.portid = p->upcall_portid;
Jesse Grossccb13522011-10-25 19:26:31 -0700228 ovs_dp_upcall(dp, skb, &upcall);
229 consume_skb(skb);
230 stats_counter = &stats->n_missed;
231 goto out;
232 }
233
234 OVS_CB(skb)->flow = flow;
235
236 stats_counter = &stats->n_hit;
237 ovs_flow_used(OVS_CB(skb)->flow, skb);
238 ovs_execute_actions(dp, skb);
239
240out:
241 /* Update datapath statistics. */
242 u64_stats_update_begin(&stats->sync);
243 (*stats_counter)++;
244 u64_stats_update_end(&stats->sync);
245}
246
247static struct genl_family dp_packet_genl_family = {
248 .id = GENL_ID_GENERATE,
249 .hdrsize = sizeof(struct ovs_header),
250 .name = OVS_PACKET_FAMILY,
251 .version = OVS_PACKET_VERSION,
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800252 .maxattr = OVS_PACKET_ATTR_MAX,
253 .netnsok = true
Jesse Grossccb13522011-10-25 19:26:31 -0700254};
255
256int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800257 const struct dp_upcall_info *upcall_info)
Jesse Grossccb13522011-10-25 19:26:31 -0700258{
259 struct dp_stats_percpu *stats;
260 int dp_ifindex;
261 int err;
262
Eric W. Biederman15e47302012-09-07 20:12:54 +0000263 if (upcall_info->portid == 0) {
Jesse Grossccb13522011-10-25 19:26:31 -0700264 err = -ENOTCONN;
265 goto err;
266 }
267
268 dp_ifindex = get_dpifindex(dp);
269 if (!dp_ifindex) {
270 err = -ENODEV;
271 goto err;
272 }
273
274 if (!skb_is_gso(skb))
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800275 err = queue_userspace_packet(ovs_dp_get_net(dp), dp_ifindex, skb, upcall_info);
Jesse Grossccb13522011-10-25 19:26:31 -0700276 else
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800277 err = queue_gso_packets(ovs_dp_get_net(dp), dp_ifindex, skb, upcall_info);
Jesse Grossccb13522011-10-25 19:26:31 -0700278 if (err)
279 goto err;
280
281 return 0;
282
283err:
Shan Wei404f2f12012-11-13 09:52:25 +0800284 stats = this_cpu_ptr(dp->stats_percpu);
Jesse Grossccb13522011-10-25 19:26:31 -0700285
286 u64_stats_update_begin(&stats->sync);
287 stats->n_lost++;
288 u64_stats_update_end(&stats->sync);
289
290 return err;
291}
292
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800293static int queue_gso_packets(struct net *net, int dp_ifindex,
294 struct sk_buff *skb,
Jesse Grossccb13522011-10-25 19:26:31 -0700295 const struct dp_upcall_info *upcall_info)
296{
Ben Pfaffa1b5d0d2012-07-20 14:47:54 -0700297 unsigned short gso_type = skb_shinfo(skb)->gso_type;
Jesse Grossccb13522011-10-25 19:26:31 -0700298 struct dp_upcall_info later_info;
299 struct sw_flow_key later_key;
300 struct sk_buff *segs, *nskb;
301 int err;
302
Cong Wang12b00042013-02-05 16:36:38 +0000303 segs = __skb_gso_segment(skb, NETIF_F_SG | NETIF_F_HW_CSUM, false);
Pravin B Shelar92e5dfc2012-07-20 14:46:29 -0700304 if (IS_ERR(segs))
305 return PTR_ERR(segs);
Jesse Grossccb13522011-10-25 19:26:31 -0700306
307 /* Queue all of the segments. */
308 skb = segs;
309 do {
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800310 err = queue_userspace_packet(net, dp_ifindex, skb, upcall_info);
Jesse Grossccb13522011-10-25 19:26:31 -0700311 if (err)
312 break;
313
Ben Pfaffa1b5d0d2012-07-20 14:47:54 -0700314 if (skb == segs && gso_type & SKB_GSO_UDP) {
Jesse Grossccb13522011-10-25 19:26:31 -0700315 /* The initial flow key extracted by ovs_flow_extract()
316 * in this case is for a first fragment, so we need to
317 * properly mark later fragments.
318 */
319 later_key = *upcall_info->key;
320 later_key.ip.frag = OVS_FRAG_TYPE_LATER;
321
322 later_info = *upcall_info;
323 later_info.key = &later_key;
324 upcall_info = &later_info;
325 }
326 } while ((skb = skb->next));
327
328 /* Free all of the segments. */
329 skb = segs;
330 do {
331 nskb = skb->next;
332 if (err)
333 kfree_skb(skb);
334 else
335 consume_skb(skb);
336 } while ((skb = nskb));
337 return err;
338}
339
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100340static size_t key_attr_size(void)
341{
342 return nla_total_size(4) /* OVS_KEY_ATTR_PRIORITY */
343 + nla_total_size(4) /* OVS_KEY_ATTR_IN_PORT */
344 + nla_total_size(4) /* OVS_KEY_ATTR_SKB_MARK */
345 + nla_total_size(12) /* OVS_KEY_ATTR_ETHERNET */
346 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
347 + nla_total_size(4) /* OVS_KEY_ATTR_8021Q */
348 + nla_total_size(0) /* OVS_KEY_ATTR_ENCAP */
349 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
350 + nla_total_size(40) /* OVS_KEY_ATTR_IPV6 */
351 + nla_total_size(2) /* OVS_KEY_ATTR_ICMPV6 */
352 + nla_total_size(28); /* OVS_KEY_ATTR_ND */
353}
354
355static size_t upcall_msg_size(const struct sk_buff *skb,
356 const struct nlattr *userdata)
357{
358 size_t size = NLMSG_ALIGN(sizeof(struct ovs_header))
359 + nla_total_size(skb->len) /* OVS_PACKET_ATTR_PACKET */
360 + nla_total_size(key_attr_size()); /* OVS_PACKET_ATTR_KEY */
361
362 /* OVS_PACKET_ATTR_USERDATA */
363 if (userdata)
364 size += NLA_ALIGN(userdata->nla_len);
365
366 return size;
367}
368
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800369static int queue_userspace_packet(struct net *net, int dp_ifindex,
370 struct sk_buff *skb,
Jesse Grossccb13522011-10-25 19:26:31 -0700371 const struct dp_upcall_info *upcall_info)
372{
373 struct ovs_header *upcall;
374 struct sk_buff *nskb = NULL;
375 struct sk_buff *user_skb; /* to be queued to userspace */
376 struct nlattr *nla;
Jesse Grossccb13522011-10-25 19:26:31 -0700377 int err;
378
379 if (vlan_tx_tag_present(skb)) {
380 nskb = skb_clone(skb, GFP_ATOMIC);
381 if (!nskb)
382 return -ENOMEM;
383
384 nskb = __vlan_put_tag(nskb, vlan_tx_tag_get(nskb));
Dan Carpenter8aa51d62012-05-13 08:44:18 +0000385 if (!nskb)
Jesse Grossccb13522011-10-25 19:26:31 -0700386 return -ENOMEM;
387
388 nskb->vlan_tci = 0;
389 skb = nskb;
390 }
391
392 if (nla_attr_size(skb->len) > USHRT_MAX) {
393 err = -EFBIG;
394 goto out;
395 }
396
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100397 user_skb = genlmsg_new(upcall_msg_size(skb, upcall_info->userdata), GFP_ATOMIC);
Jesse Grossccb13522011-10-25 19:26:31 -0700398 if (!user_skb) {
399 err = -ENOMEM;
400 goto out;
401 }
402
403 upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
404 0, upcall_info->cmd);
405 upcall->dp_ifindex = dp_ifindex;
406
407 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
408 ovs_flow_to_nlattrs(upcall_info->key, user_skb);
409 nla_nest_end(user_skb, nla);
410
411 if (upcall_info->userdata)
Ben Pfaff44901082013-02-15 17:29:22 -0800412 __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA,
413 nla_len(upcall_info->userdata),
414 nla_data(upcall_info->userdata));
Jesse Grossccb13522011-10-25 19:26:31 -0700415
416 nla = __nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, skb->len);
417
418 skb_copy_and_csum_dev(skb, nla_data(nla));
419
Rich Lanea15ff762013-02-15 11:07:43 -0800420 genlmsg_end(user_skb, upcall);
Eric W. Biederman15e47302012-09-07 20:12:54 +0000421 err = genlmsg_unicast(net, user_skb, upcall_info->portid);
Jesse Grossccb13522011-10-25 19:26:31 -0700422
423out:
424 kfree_skb(nskb);
425 return err;
426}
427
428/* Called with genl_mutex. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800429static int flush_flows(struct datapath *dp)
Jesse Grossccb13522011-10-25 19:26:31 -0700430{
431 struct flow_table *old_table;
432 struct flow_table *new_table;
Jesse Grossccb13522011-10-25 19:26:31 -0700433
434 old_table = genl_dereference(dp->table);
435 new_table = ovs_flow_tbl_alloc(TBL_MIN_BUCKETS);
436 if (!new_table)
437 return -ENOMEM;
438
439 rcu_assign_pointer(dp->table, new_table);
440
441 ovs_flow_tbl_deferred_destroy(old_table);
442 return 0;
443}
444
445static int validate_actions(const struct nlattr *attr,
446 const struct sw_flow_key *key, int depth);
447
448static int validate_sample(const struct nlattr *attr,
449 const struct sw_flow_key *key, int depth)
450{
451 const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
452 const struct nlattr *probability, *actions;
453 const struct nlattr *a;
454 int rem;
455
456 memset(attrs, 0, sizeof(attrs));
457 nla_for_each_nested(a, attr, rem) {
458 int type = nla_type(a);
459 if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
460 return -EINVAL;
461 attrs[type] = a;
462 }
463 if (rem)
464 return -EINVAL;
465
466 probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
467 if (!probability || nla_len(probability) != sizeof(u32))
468 return -EINVAL;
469
470 actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
471 if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
472 return -EINVAL;
473 return validate_actions(actions, key, depth + 1);
474}
475
Pravin B Shelar072ae632012-05-07 17:21:53 -0700476static int validate_tp_port(const struct sw_flow_key *flow_key)
477{
478 if (flow_key->eth.type == htons(ETH_P_IP)) {
Jesse Gross41853922012-08-06 15:49:47 -0700479 if (flow_key->ipv4.tp.src || flow_key->ipv4.tp.dst)
Pravin B Shelar072ae632012-05-07 17:21:53 -0700480 return 0;
481 } else if (flow_key->eth.type == htons(ETH_P_IPV6)) {
Jesse Gross41853922012-08-06 15:49:47 -0700482 if (flow_key->ipv6.tp.src || flow_key->ipv6.tp.dst)
Pravin B Shelar072ae632012-05-07 17:21:53 -0700483 return 0;
484 }
485
486 return -EINVAL;
487}
488
Jesse Grossccb13522011-10-25 19:26:31 -0700489static int validate_set(const struct nlattr *a,
490 const struct sw_flow_key *flow_key)
491{
492 const struct nlattr *ovs_key = nla_data(a);
493 int key_type = nla_type(ovs_key);
494
495 /* There can be only one key in a action */
496 if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
497 return -EINVAL;
498
499 if (key_type > OVS_KEY_ATTR_MAX ||
500 nla_len(ovs_key) != ovs_key_lens[key_type])
501 return -EINVAL;
502
503 switch (key_type) {
504 const struct ovs_key_ipv4 *ipv4_key;
Ansis Atteka3fdbd1c2012-11-13 15:44:14 -0800505 const struct ovs_key_ipv6 *ipv6_key;
Jesse Grossccb13522011-10-25 19:26:31 -0700506
507 case OVS_KEY_ATTR_PRIORITY:
Ansis Atteka39c7caeb2012-11-26 11:24:11 -0800508 case OVS_KEY_ATTR_SKB_MARK:
Jesse Grossccb13522011-10-25 19:26:31 -0700509 case OVS_KEY_ATTR_ETHERNET:
510 break;
511
512 case OVS_KEY_ATTR_IPV4:
513 if (flow_key->eth.type != htons(ETH_P_IP))
514 return -EINVAL;
515
Jesse Gross41853922012-08-06 15:49:47 -0700516 if (!flow_key->ip.proto)
Jesse Grossccb13522011-10-25 19:26:31 -0700517 return -EINVAL;
518
519 ipv4_key = nla_data(ovs_key);
520 if (ipv4_key->ipv4_proto != flow_key->ip.proto)
521 return -EINVAL;
522
523 if (ipv4_key->ipv4_frag != flow_key->ip.frag)
524 return -EINVAL;
525
526 break;
527
Ansis Atteka3fdbd1c2012-11-13 15:44:14 -0800528 case OVS_KEY_ATTR_IPV6:
529 if (flow_key->eth.type != htons(ETH_P_IPV6))
530 return -EINVAL;
531
532 if (!flow_key->ip.proto)
533 return -EINVAL;
534
535 ipv6_key = nla_data(ovs_key);
536 if (ipv6_key->ipv6_proto != flow_key->ip.proto)
537 return -EINVAL;
538
539 if (ipv6_key->ipv6_frag != flow_key->ip.frag)
540 return -EINVAL;
541
542 if (ntohl(ipv6_key->ipv6_label) & 0xFFF00000)
543 return -EINVAL;
544
545 break;
546
Jesse Grossccb13522011-10-25 19:26:31 -0700547 case OVS_KEY_ATTR_TCP:
548 if (flow_key->ip.proto != IPPROTO_TCP)
549 return -EINVAL;
550
Pravin B Shelar072ae632012-05-07 17:21:53 -0700551 return validate_tp_port(flow_key);
Jesse Grossccb13522011-10-25 19:26:31 -0700552
553 case OVS_KEY_ATTR_UDP:
554 if (flow_key->ip.proto != IPPROTO_UDP)
555 return -EINVAL;
556
Pravin B Shelar072ae632012-05-07 17:21:53 -0700557 return validate_tp_port(flow_key);
Jesse Grossccb13522011-10-25 19:26:31 -0700558
559 default:
560 return -EINVAL;
561 }
562
563 return 0;
564}
565
566static int validate_userspace(const struct nlattr *attr)
567{
568 static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
569 [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
Ben Pfaff44901082013-02-15 17:29:22 -0800570 [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_UNSPEC },
Jesse Grossccb13522011-10-25 19:26:31 -0700571 };
572 struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
573 int error;
574
575 error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX,
576 attr, userspace_policy);
577 if (error)
578 return error;
579
580 if (!a[OVS_USERSPACE_ATTR_PID] ||
581 !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
582 return -EINVAL;
583
584 return 0;
585}
586
587static int validate_actions(const struct nlattr *attr,
588 const struct sw_flow_key *key, int depth)
589{
590 const struct nlattr *a;
591 int rem, err;
592
593 if (depth >= SAMPLE_ACTION_DEPTH)
594 return -EOVERFLOW;
595
596 nla_for_each_nested(a, attr, rem) {
597 /* Expected argument lengths, (u32)-1 for variable length. */
598 static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
599 [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
600 [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
601 [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
602 [OVS_ACTION_ATTR_POP_VLAN] = 0,
603 [OVS_ACTION_ATTR_SET] = (u32)-1,
604 [OVS_ACTION_ATTR_SAMPLE] = (u32)-1
605 };
606 const struct ovs_action_push_vlan *vlan;
607 int type = nla_type(a);
608
609 if (type > OVS_ACTION_ATTR_MAX ||
610 (action_lens[type] != nla_len(a) &&
611 action_lens[type] != (u32)-1))
612 return -EINVAL;
613
614 switch (type) {
615 case OVS_ACTION_ATTR_UNSPEC:
616 return -EINVAL;
617
618 case OVS_ACTION_ATTR_USERSPACE:
619 err = validate_userspace(a);
620 if (err)
621 return err;
622 break;
623
624 case OVS_ACTION_ATTR_OUTPUT:
625 if (nla_get_u32(a) >= DP_MAX_PORTS)
626 return -EINVAL;
627 break;
628
629
630 case OVS_ACTION_ATTR_POP_VLAN:
631 break;
632
633 case OVS_ACTION_ATTR_PUSH_VLAN:
634 vlan = nla_data(a);
635 if (vlan->vlan_tpid != htons(ETH_P_8021Q))
636 return -EINVAL;
637 if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
638 return -EINVAL;
639 break;
640
641 case OVS_ACTION_ATTR_SET:
642 err = validate_set(a, key);
643 if (err)
644 return err;
645 break;
646
647 case OVS_ACTION_ATTR_SAMPLE:
648 err = validate_sample(a, key, depth);
649 if (err)
650 return err;
651 break;
652
653 default:
654 return -EINVAL;
655 }
656 }
657
658 if (rem > 0)
659 return -EINVAL;
660
661 return 0;
662}
663
664static void clear_stats(struct sw_flow *flow)
665{
666 flow->used = 0;
667 flow->tcp_flags = 0;
668 flow->packet_count = 0;
669 flow->byte_count = 0;
670}
671
672static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
673{
674 struct ovs_header *ovs_header = info->userhdr;
675 struct nlattr **a = info->attrs;
676 struct sw_flow_actions *acts;
677 struct sk_buff *packet;
678 struct sw_flow *flow;
679 struct datapath *dp;
680 struct ethhdr *eth;
681 int len;
682 int err;
683 int key_len;
684
685 err = -EINVAL;
686 if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
Thomas Grafdded45fc2013-03-29 14:46:47 +0100687 !a[OVS_PACKET_ATTR_ACTIONS])
Jesse Grossccb13522011-10-25 19:26:31 -0700688 goto err;
689
690 len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
691 packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
692 err = -ENOMEM;
693 if (!packet)
694 goto err;
695 skb_reserve(packet, NET_IP_ALIGN);
696
Thomas Graf32686a92013-03-29 14:46:48 +0100697 nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
Jesse Grossccb13522011-10-25 19:26:31 -0700698
699 skb_reset_mac_header(packet);
700 eth = eth_hdr(packet);
701
702 /* Normally, setting the skb 'protocol' field would be handled by a
703 * call to eth_type_trans(), but it assumes there's a sending
704 * device, which we may not have. */
Simon Hormane5c5d222013-03-28 13:38:25 +0900705 if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN)
Jesse Grossccb13522011-10-25 19:26:31 -0700706 packet->protocol = eth->h_proto;
707 else
708 packet->protocol = htons(ETH_P_802_2);
709
710 /* Build an sw_flow for sending this packet. */
711 flow = ovs_flow_alloc();
712 err = PTR_ERR(flow);
713 if (IS_ERR(flow))
714 goto err_kfree_skb;
715
716 err = ovs_flow_extract(packet, -1, &flow->key, &key_len);
717 if (err)
718 goto err_flow_free;
719
720 err = ovs_flow_metadata_from_nlattrs(&flow->key.phy.priority,
Ansis Atteka39c7caeb2012-11-26 11:24:11 -0800721 &flow->key.phy.skb_mark,
Jesse Grossccb13522011-10-25 19:26:31 -0700722 &flow->key.phy.in_port,
723 a[OVS_PACKET_ATTR_KEY]);
724 if (err)
725 goto err_flow_free;
726
727 err = validate_actions(a[OVS_PACKET_ATTR_ACTIONS], &flow->key, 0);
728 if (err)
729 goto err_flow_free;
730
731 flow->hash = ovs_flow_hash(&flow->key, key_len);
732
733 acts = ovs_flow_actions_alloc(a[OVS_PACKET_ATTR_ACTIONS]);
734 err = PTR_ERR(acts);
735 if (IS_ERR(acts))
736 goto err_flow_free;
737 rcu_assign_pointer(flow->sf_acts, acts);
738
739 OVS_CB(packet)->flow = flow;
740 packet->priority = flow->key.phy.priority;
Ansis Atteka39c7caeb2012-11-26 11:24:11 -0800741 packet->mark = flow->key.phy.skb_mark;
Jesse Grossccb13522011-10-25 19:26:31 -0700742
743 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800744 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -0700745 err = -ENODEV;
746 if (!dp)
747 goto err_unlock;
748
749 local_bh_disable();
750 err = ovs_execute_actions(dp, packet);
751 local_bh_enable();
752 rcu_read_unlock();
753
754 ovs_flow_free(flow);
755 return err;
756
757err_unlock:
758 rcu_read_unlock();
759err_flow_free:
760 ovs_flow_free(flow);
761err_kfree_skb:
762 kfree_skb(packet);
763err:
764 return err;
765}
766
767static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
Thomas Grafdded45fc2013-03-29 14:46:47 +0100768 [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
Jesse Grossccb13522011-10-25 19:26:31 -0700769 [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
770 [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
771};
772
773static struct genl_ops dp_packet_genl_ops[] = {
774 { .cmd = OVS_PACKET_CMD_EXECUTE,
775 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
776 .policy = packet_policy,
777 .doit = ovs_packet_cmd_execute
778 }
779};
780
781static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats)
782{
783 int i;
784 struct flow_table *table = genl_dereference(dp->table);
785
786 stats->n_flows = ovs_flow_tbl_count(table);
787
788 stats->n_hit = stats->n_missed = stats->n_lost = 0;
789 for_each_possible_cpu(i) {
790 const struct dp_stats_percpu *percpu_stats;
791 struct dp_stats_percpu local_stats;
792 unsigned int start;
793
794 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
795
796 do {
797 start = u64_stats_fetch_begin_bh(&percpu_stats->sync);
798 local_stats = *percpu_stats;
799 } while (u64_stats_fetch_retry_bh(&percpu_stats->sync, start));
800
801 stats->n_hit += local_stats.n_hit;
802 stats->n_missed += local_stats.n_missed;
803 stats->n_lost += local_stats.n_lost;
804 }
805}
806
807static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
808 [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
809 [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
810 [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
811};
812
813static struct genl_family dp_flow_genl_family = {
814 .id = GENL_ID_GENERATE,
815 .hdrsize = sizeof(struct ovs_header),
816 .name = OVS_FLOW_FAMILY,
817 .version = OVS_FLOW_VERSION,
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800818 .maxattr = OVS_FLOW_ATTR_MAX,
819 .netnsok = true
Jesse Grossccb13522011-10-25 19:26:31 -0700820};
821
822static struct genl_multicast_group ovs_dp_flow_multicast_group = {
823 .name = OVS_FLOW_MCGROUP
824};
825
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100826static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts)
827{
828 return NLMSG_ALIGN(sizeof(struct ovs_header))
829 + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_KEY */
830 + nla_total_size(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
831 + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
832 + nla_total_size(8) /* OVS_FLOW_ATTR_USED */
833 + nla_total_size(acts->actions_len); /* OVS_FLOW_ATTR_ACTIONS */
834}
835
Jesse Grossccb13522011-10-25 19:26:31 -0700836/* Called with genl_lock. */
837static int ovs_flow_cmd_fill_info(struct sw_flow *flow, struct datapath *dp,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000838 struct sk_buff *skb, u32 portid,
Jesse Grossccb13522011-10-25 19:26:31 -0700839 u32 seq, u32 flags, u8 cmd)
840{
841 const int skb_orig_len = skb->len;
842 const struct sw_flow_actions *sf_acts;
843 struct ovs_flow_stats stats;
844 struct ovs_header *ovs_header;
845 struct nlattr *nla;
846 unsigned long used;
847 u8 tcp_flags;
848 int err;
849
850 sf_acts = rcu_dereference_protected(flow->sf_acts,
851 lockdep_genl_is_held());
852
Eric W. Biederman15e47302012-09-07 20:12:54 +0000853 ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family, flags, cmd);
Jesse Grossccb13522011-10-25 19:26:31 -0700854 if (!ovs_header)
855 return -EMSGSIZE;
856
857 ovs_header->dp_ifindex = get_dpifindex(dp);
858
859 nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
860 if (!nla)
861 goto nla_put_failure;
862 err = ovs_flow_to_nlattrs(&flow->key, skb);
863 if (err)
864 goto error;
865 nla_nest_end(skb, nla);
866
867 spin_lock_bh(&flow->lock);
868 used = flow->used;
869 stats.n_packets = flow->packet_count;
870 stats.n_bytes = flow->byte_count;
871 tcp_flags = flow->tcp_flags;
872 spin_unlock_bh(&flow->lock);
873
David S. Miller028d6a62012-03-29 23:20:48 -0400874 if (used &&
875 nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
876 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -0700877
David S. Miller028d6a62012-03-29 23:20:48 -0400878 if (stats.n_packets &&
879 nla_put(skb, OVS_FLOW_ATTR_STATS,
880 sizeof(struct ovs_flow_stats), &stats))
881 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -0700882
David S. Miller028d6a62012-03-29 23:20:48 -0400883 if (tcp_flags &&
884 nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, tcp_flags))
885 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -0700886
887 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
888 * this is the first flow to be dumped into 'skb'. This is unusual for
889 * Netlink but individual action lists can be longer than
890 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
891 * The userspace caller can always fetch the actions separately if it
892 * really wants them. (Most userspace callers in fact don't care.)
893 *
894 * This can only fail for dump operations because the skb is always
895 * properly sized for single flows.
896 */
897 err = nla_put(skb, OVS_FLOW_ATTR_ACTIONS, sf_acts->actions_len,
898 sf_acts->actions);
899 if (err < 0 && skb_orig_len)
900 goto error;
901
902 return genlmsg_end(skb, ovs_header);
903
904nla_put_failure:
905 err = -EMSGSIZE;
906error:
907 genlmsg_cancel(skb, ovs_header);
908 return err;
909}
910
911static struct sk_buff *ovs_flow_cmd_alloc_info(struct sw_flow *flow)
912{
913 const struct sw_flow_actions *sf_acts;
Jesse Grossccb13522011-10-25 19:26:31 -0700914
915 sf_acts = rcu_dereference_protected(flow->sf_acts,
916 lockdep_genl_is_held());
917
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100918 return genlmsg_new(ovs_flow_cmd_msg_size(sf_acts), GFP_KERNEL);
Jesse Grossccb13522011-10-25 19:26:31 -0700919}
920
921static struct sk_buff *ovs_flow_cmd_build_info(struct sw_flow *flow,
922 struct datapath *dp,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000923 u32 portid, u32 seq, u8 cmd)
Jesse Grossccb13522011-10-25 19:26:31 -0700924{
925 struct sk_buff *skb;
926 int retval;
927
928 skb = ovs_flow_cmd_alloc_info(flow);
929 if (!skb)
930 return ERR_PTR(-ENOMEM);
931
Eric W. Biederman15e47302012-09-07 20:12:54 +0000932 retval = ovs_flow_cmd_fill_info(flow, dp, skb, portid, seq, 0, cmd);
Jesse Grossccb13522011-10-25 19:26:31 -0700933 BUG_ON(retval < 0);
934 return skb;
935}
936
937static int ovs_flow_cmd_new_or_set(struct sk_buff *skb, struct genl_info *info)
938{
939 struct nlattr **a = info->attrs;
940 struct ovs_header *ovs_header = info->userhdr;
941 struct sw_flow_key key;
942 struct sw_flow *flow;
943 struct sk_buff *reply;
944 struct datapath *dp;
945 struct flow_table *table;
946 int error;
947 int key_len;
948
949 /* Extract key. */
950 error = -EINVAL;
951 if (!a[OVS_FLOW_ATTR_KEY])
952 goto error;
953 error = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
954 if (error)
955 goto error;
956
957 /* Validate actions. */
958 if (a[OVS_FLOW_ATTR_ACTIONS]) {
959 error = validate_actions(a[OVS_FLOW_ATTR_ACTIONS], &key, 0);
960 if (error)
961 goto error;
962 } else if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW) {
963 error = -EINVAL;
964 goto error;
965 }
966
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800967 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -0700968 error = -ENODEV;
969 if (!dp)
970 goto error;
971
972 table = genl_dereference(dp->table);
973 flow = ovs_flow_tbl_lookup(table, &key, key_len);
974 if (!flow) {
975 struct sw_flow_actions *acts;
976
977 /* Bail out if we're not allowed to create a new flow. */
978 error = -ENOENT;
979 if (info->genlhdr->cmd == OVS_FLOW_CMD_SET)
980 goto error;
981
982 /* Expand table, if necessary, to make room. */
983 if (ovs_flow_tbl_need_to_expand(table)) {
984 struct flow_table *new_table;
985
986 new_table = ovs_flow_tbl_expand(table);
987 if (!IS_ERR(new_table)) {
988 rcu_assign_pointer(dp->table, new_table);
989 ovs_flow_tbl_deferred_destroy(table);
990 table = genl_dereference(dp->table);
991 }
992 }
993
994 /* Allocate flow. */
995 flow = ovs_flow_alloc();
996 if (IS_ERR(flow)) {
997 error = PTR_ERR(flow);
998 goto error;
999 }
1000 flow->key = key;
1001 clear_stats(flow);
1002
1003 /* Obtain actions. */
1004 acts = ovs_flow_actions_alloc(a[OVS_FLOW_ATTR_ACTIONS]);
1005 error = PTR_ERR(acts);
1006 if (IS_ERR(acts))
1007 goto error_free_flow;
1008 rcu_assign_pointer(flow->sf_acts, acts);
1009
1010 /* Put flow in bucket. */
1011 flow->hash = ovs_flow_hash(&key, key_len);
1012 ovs_flow_tbl_insert(table, flow);
1013
Eric W. Biederman15e47302012-09-07 20:12:54 +00001014 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001015 info->snd_seq,
1016 OVS_FLOW_CMD_NEW);
1017 } else {
1018 /* We found a matching flow. */
1019 struct sw_flow_actions *old_acts;
1020 struct nlattr *acts_attrs;
1021
1022 /* Bail out if we're not allowed to modify an existing flow.
1023 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
1024 * because Generic Netlink treats the latter as a dump
1025 * request. We also accept NLM_F_EXCL in case that bug ever
1026 * gets fixed.
1027 */
1028 error = -EEXIST;
1029 if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW &&
1030 info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL))
1031 goto error;
1032
1033 /* Update actions. */
1034 old_acts = rcu_dereference_protected(flow->sf_acts,
1035 lockdep_genl_is_held());
1036 acts_attrs = a[OVS_FLOW_ATTR_ACTIONS];
1037 if (acts_attrs &&
1038 (old_acts->actions_len != nla_len(acts_attrs) ||
1039 memcmp(old_acts->actions, nla_data(acts_attrs),
1040 old_acts->actions_len))) {
1041 struct sw_flow_actions *new_acts;
1042
1043 new_acts = ovs_flow_actions_alloc(acts_attrs);
1044 error = PTR_ERR(new_acts);
1045 if (IS_ERR(new_acts))
1046 goto error;
1047
1048 rcu_assign_pointer(flow->sf_acts, new_acts);
1049 ovs_flow_deferred_free_acts(old_acts);
1050 }
1051
Eric W. Biederman15e47302012-09-07 20:12:54 +00001052 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001053 info->snd_seq, OVS_FLOW_CMD_NEW);
1054
1055 /* Clear stats. */
1056 if (a[OVS_FLOW_ATTR_CLEAR]) {
1057 spin_lock_bh(&flow->lock);
1058 clear_stats(flow);
1059 spin_unlock_bh(&flow->lock);
1060 }
1061 }
1062
1063 if (!IS_ERR(reply))
Eric W. Biederman15e47302012-09-07 20:12:54 +00001064 genl_notify(reply, genl_info_net(info), info->snd_portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001065 ovs_dp_flow_multicast_group.id, info->nlhdr,
1066 GFP_KERNEL);
1067 else
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001068 netlink_set_err(sock_net(skb->sk)->genl_sock, 0,
Jesse Grossccb13522011-10-25 19:26:31 -07001069 ovs_dp_flow_multicast_group.id, PTR_ERR(reply));
1070 return 0;
1071
1072error_free_flow:
1073 ovs_flow_free(flow);
1074error:
1075 return error;
1076}
1077
1078static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1079{
1080 struct nlattr **a = info->attrs;
1081 struct ovs_header *ovs_header = info->userhdr;
1082 struct sw_flow_key key;
1083 struct sk_buff *reply;
1084 struct sw_flow *flow;
1085 struct datapath *dp;
1086 struct flow_table *table;
1087 int err;
1088 int key_len;
1089
1090 if (!a[OVS_FLOW_ATTR_KEY])
1091 return -EINVAL;
1092 err = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
1093 if (err)
1094 return err;
1095
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001096 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001097 if (!dp)
1098 return -ENODEV;
1099
1100 table = genl_dereference(dp->table);
1101 flow = ovs_flow_tbl_lookup(table, &key, key_len);
1102 if (!flow)
1103 return -ENOENT;
1104
Eric W. Biederman15e47302012-09-07 20:12:54 +00001105 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001106 info->snd_seq, OVS_FLOW_CMD_NEW);
1107 if (IS_ERR(reply))
1108 return PTR_ERR(reply);
1109
1110 return genlmsg_reply(reply, info);
1111}
1112
1113static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1114{
1115 struct nlattr **a = info->attrs;
1116 struct ovs_header *ovs_header = info->userhdr;
1117 struct sw_flow_key key;
1118 struct sk_buff *reply;
1119 struct sw_flow *flow;
1120 struct datapath *dp;
1121 struct flow_table *table;
1122 int err;
1123 int key_len;
1124
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001125 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
1126 if (!dp)
1127 return -ENODEV;
1128
Jesse Grossccb13522011-10-25 19:26:31 -07001129 if (!a[OVS_FLOW_ATTR_KEY])
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001130 return flush_flows(dp);
1131
Jesse Grossccb13522011-10-25 19:26:31 -07001132 err = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
1133 if (err)
1134 return err;
1135
Jesse Grossccb13522011-10-25 19:26:31 -07001136 table = genl_dereference(dp->table);
1137 flow = ovs_flow_tbl_lookup(table, &key, key_len);
1138 if (!flow)
1139 return -ENOENT;
1140
1141 reply = ovs_flow_cmd_alloc_info(flow);
1142 if (!reply)
1143 return -ENOMEM;
1144
1145 ovs_flow_tbl_remove(table, flow);
1146
Eric W. Biederman15e47302012-09-07 20:12:54 +00001147 err = ovs_flow_cmd_fill_info(flow, dp, reply, info->snd_portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001148 info->snd_seq, 0, OVS_FLOW_CMD_DEL);
1149 BUG_ON(err < 0);
1150
1151 ovs_flow_deferred_free(flow);
1152
Eric W. Biederman15e47302012-09-07 20:12:54 +00001153 genl_notify(reply, genl_info_net(info), info->snd_portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001154 ovs_dp_flow_multicast_group.id, info->nlhdr, GFP_KERNEL);
1155 return 0;
1156}
1157
1158static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1159{
1160 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1161 struct datapath *dp;
1162 struct flow_table *table;
1163
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001164 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001165 if (!dp)
1166 return -ENODEV;
1167
1168 table = genl_dereference(dp->table);
1169
1170 for (;;) {
1171 struct sw_flow *flow;
1172 u32 bucket, obj;
1173
1174 bucket = cb->args[0];
1175 obj = cb->args[1];
1176 flow = ovs_flow_tbl_next(table, &bucket, &obj);
1177 if (!flow)
1178 break;
1179
1180 if (ovs_flow_cmd_fill_info(flow, dp, skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001181 NETLINK_CB(cb->skb).portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001182 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1183 OVS_FLOW_CMD_NEW) < 0)
1184 break;
1185
1186 cb->args[0] = bucket;
1187 cb->args[1] = obj;
1188 }
1189 return skb->len;
1190}
1191
1192static struct genl_ops dp_flow_genl_ops[] = {
1193 { .cmd = OVS_FLOW_CMD_NEW,
1194 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1195 .policy = flow_policy,
1196 .doit = ovs_flow_cmd_new_or_set
1197 },
1198 { .cmd = OVS_FLOW_CMD_DEL,
1199 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1200 .policy = flow_policy,
1201 .doit = ovs_flow_cmd_del
1202 },
1203 { .cmd = OVS_FLOW_CMD_GET,
1204 .flags = 0, /* OK for unprivileged users. */
1205 .policy = flow_policy,
1206 .doit = ovs_flow_cmd_get,
1207 .dumpit = ovs_flow_cmd_dump
1208 },
1209 { .cmd = OVS_FLOW_CMD_SET,
1210 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1211 .policy = flow_policy,
1212 .doit = ovs_flow_cmd_new_or_set,
1213 },
1214};
1215
1216static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1217 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1218 [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1219};
1220
1221static struct genl_family dp_datapath_genl_family = {
1222 .id = GENL_ID_GENERATE,
1223 .hdrsize = sizeof(struct ovs_header),
1224 .name = OVS_DATAPATH_FAMILY,
1225 .version = OVS_DATAPATH_VERSION,
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001226 .maxattr = OVS_DP_ATTR_MAX,
1227 .netnsok = true
Jesse Grossccb13522011-10-25 19:26:31 -07001228};
1229
1230static struct genl_multicast_group ovs_dp_datapath_multicast_group = {
1231 .name = OVS_DATAPATH_MCGROUP
1232};
1233
Thomas Grafc3ff8cf2013-03-29 14:46:49 +01001234static size_t ovs_dp_cmd_msg_size(void)
1235{
1236 size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
1237
1238 msgsize += nla_total_size(IFNAMSIZ);
1239 msgsize += nla_total_size(sizeof(struct ovs_dp_stats));
1240
1241 return msgsize;
1242}
1243
Jesse Grossccb13522011-10-25 19:26:31 -07001244static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001245 u32 portid, u32 seq, u32 flags, u8 cmd)
Jesse Grossccb13522011-10-25 19:26:31 -07001246{
1247 struct ovs_header *ovs_header;
1248 struct ovs_dp_stats dp_stats;
1249 int err;
1250
Eric W. Biederman15e47302012-09-07 20:12:54 +00001251 ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
Jesse Grossccb13522011-10-25 19:26:31 -07001252 flags, cmd);
1253 if (!ovs_header)
1254 goto error;
1255
1256 ovs_header->dp_ifindex = get_dpifindex(dp);
1257
1258 rcu_read_lock();
1259 err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
1260 rcu_read_unlock();
1261 if (err)
1262 goto nla_put_failure;
1263
1264 get_dp_stats(dp, &dp_stats);
David S. Miller028d6a62012-03-29 23:20:48 -04001265 if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats), &dp_stats))
1266 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001267
1268 return genlmsg_end(skb, ovs_header);
1269
1270nla_put_failure:
1271 genlmsg_cancel(skb, ovs_header);
1272error:
1273 return -EMSGSIZE;
1274}
1275
Eric W. Biederman15e47302012-09-07 20:12:54 +00001276static struct sk_buff *ovs_dp_cmd_build_info(struct datapath *dp, u32 portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001277 u32 seq, u8 cmd)
1278{
1279 struct sk_buff *skb;
1280 int retval;
1281
Thomas Grafc3ff8cf2013-03-29 14:46:49 +01001282 skb = genlmsg_new(ovs_dp_cmd_msg_size(), GFP_KERNEL);
Jesse Grossccb13522011-10-25 19:26:31 -07001283 if (!skb)
1284 return ERR_PTR(-ENOMEM);
1285
Eric W. Biederman15e47302012-09-07 20:12:54 +00001286 retval = ovs_dp_cmd_fill_info(dp, skb, portid, seq, 0, cmd);
Jesse Grossccb13522011-10-25 19:26:31 -07001287 if (retval < 0) {
1288 kfree_skb(skb);
1289 return ERR_PTR(retval);
1290 }
1291 return skb;
1292}
1293
1294/* Called with genl_mutex and optionally with RTNL lock also. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001295static struct datapath *lookup_datapath(struct net *net,
1296 struct ovs_header *ovs_header,
Jesse Grossccb13522011-10-25 19:26:31 -07001297 struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1298{
1299 struct datapath *dp;
1300
1301 if (!a[OVS_DP_ATTR_NAME])
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001302 dp = get_dp(net, ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001303 else {
1304 struct vport *vport;
1305
1306 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001307 vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
Jesse Grossccb13522011-10-25 19:26:31 -07001308 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
1309 rcu_read_unlock();
1310 }
1311 return dp ? dp : ERR_PTR(-ENODEV);
1312}
1313
1314static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1315{
1316 struct nlattr **a = info->attrs;
1317 struct vport_parms parms;
1318 struct sk_buff *reply;
1319 struct datapath *dp;
1320 struct vport *vport;
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001321 struct ovs_net *ovs_net;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001322 int err, i;
Jesse Grossccb13522011-10-25 19:26:31 -07001323
1324 err = -EINVAL;
1325 if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1326 goto err;
1327
1328 rtnl_lock();
Jesse Grossccb13522011-10-25 19:26:31 -07001329
1330 err = -ENOMEM;
1331 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1332 if (dp == NULL)
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001333 goto err_unlock_rtnl;
1334
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001335 ovs_dp_set_net(dp, hold_net(sock_net(skb->sk)));
Jesse Grossccb13522011-10-25 19:26:31 -07001336
1337 /* Allocate table. */
1338 err = -ENOMEM;
1339 rcu_assign_pointer(dp->table, ovs_flow_tbl_alloc(TBL_MIN_BUCKETS));
1340 if (!dp->table)
1341 goto err_free_dp;
1342
1343 dp->stats_percpu = alloc_percpu(struct dp_stats_percpu);
1344 if (!dp->stats_percpu) {
1345 err = -ENOMEM;
1346 goto err_destroy_table;
1347 }
1348
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001349 dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
1350 GFP_KERNEL);
1351 if (!dp->ports) {
1352 err = -ENOMEM;
1353 goto err_destroy_percpu;
1354 }
1355
1356 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1357 INIT_HLIST_HEAD(&dp->ports[i]);
1358
Jesse Grossccb13522011-10-25 19:26:31 -07001359 /* Set up our datapath device. */
1360 parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1361 parms.type = OVS_VPORT_TYPE_INTERNAL;
1362 parms.options = NULL;
1363 parms.dp = dp;
1364 parms.port_no = OVSP_LOCAL;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001365 parms.upcall_portid = nla_get_u32(a[OVS_DP_ATTR_UPCALL_PID]);
Jesse Grossccb13522011-10-25 19:26:31 -07001366
1367 vport = new_vport(&parms);
1368 if (IS_ERR(vport)) {
1369 err = PTR_ERR(vport);
1370 if (err == -EBUSY)
1371 err = -EEXIST;
1372
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001373 goto err_destroy_ports_array;
Jesse Grossccb13522011-10-25 19:26:31 -07001374 }
1375
Eric W. Biederman15e47302012-09-07 20:12:54 +00001376 reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001377 info->snd_seq, OVS_DP_CMD_NEW);
1378 err = PTR_ERR(reply);
1379 if (IS_ERR(reply))
1380 goto err_destroy_local_port;
1381
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001382 ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
1383 list_add_tail(&dp->list_node, &ovs_net->dps);
Jesse Grossccb13522011-10-25 19:26:31 -07001384 rtnl_unlock();
1385
Eric W. Biederman15e47302012-09-07 20:12:54 +00001386 genl_notify(reply, genl_info_net(info), info->snd_portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001387 ovs_dp_datapath_multicast_group.id, info->nlhdr,
1388 GFP_KERNEL);
1389 return 0;
1390
1391err_destroy_local_port:
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001392 ovs_dp_detach_port(ovs_vport_rtnl(dp, OVSP_LOCAL));
1393err_destroy_ports_array:
1394 kfree(dp->ports);
Jesse Grossccb13522011-10-25 19:26:31 -07001395err_destroy_percpu:
1396 free_percpu(dp->stats_percpu);
1397err_destroy_table:
1398 ovs_flow_tbl_destroy(genl_dereference(dp->table));
1399err_free_dp:
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001400 release_net(ovs_dp_get_net(dp));
Jesse Grossccb13522011-10-25 19:26:31 -07001401 kfree(dp);
Jesse Grossccb13522011-10-25 19:26:31 -07001402err_unlock_rtnl:
1403 rtnl_unlock();
1404err:
1405 return err;
1406}
1407
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001408/* Called with genl_mutex. */
1409static void __dp_destroy(struct datapath *dp)
Jesse Grossccb13522011-10-25 19:26:31 -07001410{
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001411 int i;
Jesse Grossccb13522011-10-25 19:26:31 -07001412
1413 rtnl_lock();
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001414
1415 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1416 struct vport *vport;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001417 struct hlist_node *n;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001418
Sasha Levinb67bfe02013-02-27 17:06:00 -08001419 hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001420 if (vport->port_no != OVSP_LOCAL)
1421 ovs_dp_detach_port(vport);
1422 }
Jesse Grossccb13522011-10-25 19:26:31 -07001423
1424 list_del(&dp->list_node);
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001425 ovs_dp_detach_port(ovs_vport_rtnl(dp, OVSP_LOCAL));
Jesse Grossccb13522011-10-25 19:26:31 -07001426
1427 /* rtnl_unlock() will wait until all the references to devices that
1428 * are pending unregistration have been dropped. We do it here to
1429 * ensure that any internal devices (which contain DP pointers) are
1430 * fully destroyed before freeing the datapath.
1431 */
1432 rtnl_unlock();
1433
1434 call_rcu(&dp->rcu, destroy_dp_rcu);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001435}
1436
1437static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1438{
1439 struct sk_buff *reply;
1440 struct datapath *dp;
1441 int err;
1442
1443 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1444 err = PTR_ERR(dp);
1445 if (IS_ERR(dp))
1446 return err;
1447
Eric W. Biederman15e47302012-09-07 20:12:54 +00001448 reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001449 info->snd_seq, OVS_DP_CMD_DEL);
1450 err = PTR_ERR(reply);
1451 if (IS_ERR(reply))
1452 return err;
1453
1454 __dp_destroy(dp);
Jesse Grossccb13522011-10-25 19:26:31 -07001455
Eric W. Biederman15e47302012-09-07 20:12:54 +00001456 genl_notify(reply, genl_info_net(info), info->snd_portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001457 ovs_dp_datapath_multicast_group.id, info->nlhdr,
1458 GFP_KERNEL);
1459
1460 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001461}
1462
1463static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1464{
1465 struct sk_buff *reply;
1466 struct datapath *dp;
1467 int err;
1468
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001469 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
Jesse Grossccb13522011-10-25 19:26:31 -07001470 if (IS_ERR(dp))
1471 return PTR_ERR(dp);
1472
Eric W. Biederman15e47302012-09-07 20:12:54 +00001473 reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001474 info->snd_seq, OVS_DP_CMD_NEW);
1475 if (IS_ERR(reply)) {
1476 err = PTR_ERR(reply);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001477 netlink_set_err(sock_net(skb->sk)->genl_sock, 0,
Jesse Grossccb13522011-10-25 19:26:31 -07001478 ovs_dp_datapath_multicast_group.id, err);
1479 return 0;
1480 }
1481
Eric W. Biederman15e47302012-09-07 20:12:54 +00001482 genl_notify(reply, genl_info_net(info), info->snd_portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001483 ovs_dp_datapath_multicast_group.id, info->nlhdr,
1484 GFP_KERNEL);
1485
1486 return 0;
1487}
1488
1489static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1490{
1491 struct sk_buff *reply;
1492 struct datapath *dp;
1493
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001494 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
Jesse Grossccb13522011-10-25 19:26:31 -07001495 if (IS_ERR(dp))
1496 return PTR_ERR(dp);
1497
Eric W. Biederman15e47302012-09-07 20:12:54 +00001498 reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001499 info->snd_seq, OVS_DP_CMD_NEW);
1500 if (IS_ERR(reply))
1501 return PTR_ERR(reply);
1502
1503 return genlmsg_reply(reply, info);
1504}
1505
1506static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1507{
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001508 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
Jesse Grossccb13522011-10-25 19:26:31 -07001509 struct datapath *dp;
1510 int skip = cb->args[0];
1511 int i = 0;
1512
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001513 list_for_each_entry(dp, &ovs_net->dps, list_node) {
Ben Pfaff77676fd2012-01-17 13:33:39 +00001514 if (i >= skip &&
Eric W. Biederman15e47302012-09-07 20:12:54 +00001515 ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001516 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1517 OVS_DP_CMD_NEW) < 0)
1518 break;
1519 i++;
1520 }
1521
1522 cb->args[0] = i;
1523
1524 return skb->len;
1525}
1526
1527static struct genl_ops dp_datapath_genl_ops[] = {
1528 { .cmd = OVS_DP_CMD_NEW,
1529 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1530 .policy = datapath_policy,
1531 .doit = ovs_dp_cmd_new
1532 },
1533 { .cmd = OVS_DP_CMD_DEL,
1534 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1535 .policy = datapath_policy,
1536 .doit = ovs_dp_cmd_del
1537 },
1538 { .cmd = OVS_DP_CMD_GET,
1539 .flags = 0, /* OK for unprivileged users. */
1540 .policy = datapath_policy,
1541 .doit = ovs_dp_cmd_get,
1542 .dumpit = ovs_dp_cmd_dump
1543 },
1544 { .cmd = OVS_DP_CMD_SET,
1545 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1546 .policy = datapath_policy,
1547 .doit = ovs_dp_cmd_set,
1548 },
1549};
1550
1551static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
1552 [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1553 [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
1554 [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
1555 [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
1556 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1557 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
1558};
1559
1560static struct genl_family dp_vport_genl_family = {
1561 .id = GENL_ID_GENERATE,
1562 .hdrsize = sizeof(struct ovs_header),
1563 .name = OVS_VPORT_FAMILY,
1564 .version = OVS_VPORT_VERSION,
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001565 .maxattr = OVS_VPORT_ATTR_MAX,
1566 .netnsok = true
Jesse Grossccb13522011-10-25 19:26:31 -07001567};
1568
1569struct genl_multicast_group ovs_dp_vport_multicast_group = {
1570 .name = OVS_VPORT_MCGROUP
1571};
1572
1573/* Called with RTNL lock or RCU read lock. */
1574static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001575 u32 portid, u32 seq, u32 flags, u8 cmd)
Jesse Grossccb13522011-10-25 19:26:31 -07001576{
1577 struct ovs_header *ovs_header;
1578 struct ovs_vport_stats vport_stats;
1579 int err;
1580
Eric W. Biederman15e47302012-09-07 20:12:54 +00001581 ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
Jesse Grossccb13522011-10-25 19:26:31 -07001582 flags, cmd);
1583 if (!ovs_header)
1584 return -EMSGSIZE;
1585
1586 ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1587
David S. Miller028d6a62012-03-29 23:20:48 -04001588 if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
1589 nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
1590 nla_put_string(skb, OVS_VPORT_ATTR_NAME, vport->ops->get_name(vport)) ||
Eric W. Biederman15e47302012-09-07 20:12:54 +00001591 nla_put_u32(skb, OVS_VPORT_ATTR_UPCALL_PID, vport->upcall_portid))
David S. Miller028d6a62012-03-29 23:20:48 -04001592 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001593
1594 ovs_vport_get_stats(vport, &vport_stats);
David S. Miller028d6a62012-03-29 23:20:48 -04001595 if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
1596 &vport_stats))
1597 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001598
1599 err = ovs_vport_get_options(vport, skb);
1600 if (err == -EMSGSIZE)
1601 goto error;
1602
1603 return genlmsg_end(skb, ovs_header);
1604
1605nla_put_failure:
1606 err = -EMSGSIZE;
1607error:
1608 genlmsg_cancel(skb, ovs_header);
1609 return err;
1610}
1611
1612/* Called with RTNL lock or RCU read lock. */
Eric W. Biederman15e47302012-09-07 20:12:54 +00001613struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001614 u32 seq, u8 cmd)
1615{
1616 struct sk_buff *skb;
1617 int retval;
1618
1619 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1620 if (!skb)
1621 return ERR_PTR(-ENOMEM);
1622
Eric W. Biederman15e47302012-09-07 20:12:54 +00001623 retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
Jesse Grossccb13522011-10-25 19:26:31 -07001624 if (retval < 0) {
1625 kfree_skb(skb);
1626 return ERR_PTR(retval);
1627 }
1628 return skb;
1629}
1630
1631/* Called with RTNL lock or RCU read lock. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001632static struct vport *lookup_vport(struct net *net,
1633 struct ovs_header *ovs_header,
Jesse Grossccb13522011-10-25 19:26:31 -07001634 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1635{
1636 struct datapath *dp;
1637 struct vport *vport;
1638
1639 if (a[OVS_VPORT_ATTR_NAME]) {
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001640 vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
Jesse Grossccb13522011-10-25 19:26:31 -07001641 if (!vport)
1642 return ERR_PTR(-ENODEV);
Ben Pfaff651a68e2012-03-06 15:04:04 -08001643 if (ovs_header->dp_ifindex &&
1644 ovs_header->dp_ifindex != get_dpifindex(vport->dp))
1645 return ERR_PTR(-ENODEV);
Jesse Grossccb13522011-10-25 19:26:31 -07001646 return vport;
1647 } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
1648 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1649
1650 if (port_no >= DP_MAX_PORTS)
1651 return ERR_PTR(-EFBIG);
1652
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001653 dp = get_dp(net, ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001654 if (!dp)
1655 return ERR_PTR(-ENODEV);
1656
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001657 vport = ovs_vport_rtnl_rcu(dp, port_no);
Jesse Grossccb13522011-10-25 19:26:31 -07001658 if (!vport)
Jarno Rajahalme14408db2013-01-09 14:27:35 -08001659 return ERR_PTR(-ENODEV);
Jesse Grossccb13522011-10-25 19:26:31 -07001660 return vport;
1661 } else
1662 return ERR_PTR(-EINVAL);
1663}
1664
1665static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
1666{
1667 struct nlattr **a = info->attrs;
1668 struct ovs_header *ovs_header = info->userhdr;
1669 struct vport_parms parms;
1670 struct sk_buff *reply;
1671 struct vport *vport;
1672 struct datapath *dp;
1673 u32 port_no;
1674 int err;
1675
1676 err = -EINVAL;
1677 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
1678 !a[OVS_VPORT_ATTR_UPCALL_PID])
1679 goto exit;
1680
1681 rtnl_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001682 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001683 err = -ENODEV;
1684 if (!dp)
1685 goto exit_unlock;
1686
1687 if (a[OVS_VPORT_ATTR_PORT_NO]) {
1688 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1689
1690 err = -EFBIG;
1691 if (port_no >= DP_MAX_PORTS)
1692 goto exit_unlock;
1693
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001694 vport = ovs_vport_rtnl_rcu(dp, port_no);
Jesse Grossccb13522011-10-25 19:26:31 -07001695 err = -EBUSY;
1696 if (vport)
1697 goto exit_unlock;
1698 } else {
1699 for (port_no = 1; ; port_no++) {
1700 if (port_no >= DP_MAX_PORTS) {
1701 err = -EFBIG;
1702 goto exit_unlock;
1703 }
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001704 vport = ovs_vport_rtnl(dp, port_no);
Jesse Grossccb13522011-10-25 19:26:31 -07001705 if (!vport)
1706 break;
1707 }
1708 }
1709
1710 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
1711 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1712 parms.options = a[OVS_VPORT_ATTR_OPTIONS];
1713 parms.dp = dp;
1714 parms.port_no = port_no;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001715 parms.upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
Jesse Grossccb13522011-10-25 19:26:31 -07001716
1717 vport = new_vport(&parms);
1718 err = PTR_ERR(vport);
1719 if (IS_ERR(vport))
1720 goto exit_unlock;
1721
Rich Lanecb7c5bd2013-02-08 13:18:01 -08001722 err = 0;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001723 reply = ovs_vport_cmd_build_info(vport, info->snd_portid, info->snd_seq,
Jesse Grossccb13522011-10-25 19:26:31 -07001724 OVS_VPORT_CMD_NEW);
1725 if (IS_ERR(reply)) {
1726 err = PTR_ERR(reply);
1727 ovs_dp_detach_port(vport);
1728 goto exit_unlock;
1729 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00001730 genl_notify(reply, genl_info_net(info), info->snd_portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001731 ovs_dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
1732
1733exit_unlock:
1734 rtnl_unlock();
1735exit:
1736 return err;
1737}
1738
1739static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
1740{
1741 struct nlattr **a = info->attrs;
1742 struct sk_buff *reply;
1743 struct vport *vport;
1744 int err;
1745
1746 rtnl_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001747 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
Jesse Grossccb13522011-10-25 19:26:31 -07001748 err = PTR_ERR(vport);
1749 if (IS_ERR(vport))
1750 goto exit_unlock;
1751
1752 err = 0;
1753 if (a[OVS_VPORT_ATTR_TYPE] &&
1754 nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type)
1755 err = -EINVAL;
1756
1757 if (!err && a[OVS_VPORT_ATTR_OPTIONS])
1758 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
Ansis Atteka03fbf8b2012-04-09 12:12:12 -07001759 if (err)
1760 goto exit_unlock;
1761 if (a[OVS_VPORT_ATTR_UPCALL_PID])
Eric W. Biederman15e47302012-09-07 20:12:54 +00001762 vport->upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
Jesse Grossccb13522011-10-25 19:26:31 -07001763
Eric W. Biederman15e47302012-09-07 20:12:54 +00001764 reply = ovs_vport_cmd_build_info(vport, info->snd_portid, info->snd_seq,
Jesse Grossccb13522011-10-25 19:26:31 -07001765 OVS_VPORT_CMD_NEW);
1766 if (IS_ERR(reply)) {
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001767 netlink_set_err(sock_net(skb->sk)->genl_sock, 0,
Ansis Atteka4cb6e112012-05-03 18:40:38 -07001768 ovs_dp_vport_multicast_group.id, PTR_ERR(reply));
1769 goto exit_unlock;
Jesse Grossccb13522011-10-25 19:26:31 -07001770 }
1771
Eric W. Biederman15e47302012-09-07 20:12:54 +00001772 genl_notify(reply, genl_info_net(info), info->snd_portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001773 ovs_dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
1774
1775exit_unlock:
1776 rtnl_unlock();
1777 return err;
1778}
1779
1780static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
1781{
1782 struct nlattr **a = info->attrs;
1783 struct sk_buff *reply;
1784 struct vport *vport;
1785 int err;
1786
1787 rtnl_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001788 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
Jesse Grossccb13522011-10-25 19:26:31 -07001789 err = PTR_ERR(vport);
1790 if (IS_ERR(vport))
1791 goto exit_unlock;
1792
1793 if (vport->port_no == OVSP_LOCAL) {
1794 err = -EINVAL;
1795 goto exit_unlock;
1796 }
1797
Eric W. Biederman15e47302012-09-07 20:12:54 +00001798 reply = ovs_vport_cmd_build_info(vport, info->snd_portid, info->snd_seq,
Jesse Grossccb13522011-10-25 19:26:31 -07001799 OVS_VPORT_CMD_DEL);
1800 err = PTR_ERR(reply);
1801 if (IS_ERR(reply))
1802 goto exit_unlock;
1803
Rich Lane734907e2013-02-08 09:30:23 -08001804 err = 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001805 ovs_dp_detach_port(vport);
1806
Eric W. Biederman15e47302012-09-07 20:12:54 +00001807 genl_notify(reply, genl_info_net(info), info->snd_portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001808 ovs_dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
1809
1810exit_unlock:
1811 rtnl_unlock();
1812 return err;
1813}
1814
1815static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
1816{
1817 struct nlattr **a = info->attrs;
1818 struct ovs_header *ovs_header = info->userhdr;
1819 struct sk_buff *reply;
1820 struct vport *vport;
1821 int err;
1822
1823 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001824 vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
Jesse Grossccb13522011-10-25 19:26:31 -07001825 err = PTR_ERR(vport);
1826 if (IS_ERR(vport))
1827 goto exit_unlock;
1828
Eric W. Biederman15e47302012-09-07 20:12:54 +00001829 reply = ovs_vport_cmd_build_info(vport, info->snd_portid, info->snd_seq,
Jesse Grossccb13522011-10-25 19:26:31 -07001830 OVS_VPORT_CMD_NEW);
1831 err = PTR_ERR(reply);
1832 if (IS_ERR(reply))
1833 goto exit_unlock;
1834
1835 rcu_read_unlock();
1836
1837 return genlmsg_reply(reply, info);
1838
1839exit_unlock:
1840 rcu_read_unlock();
1841 return err;
1842}
1843
1844static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1845{
1846 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1847 struct datapath *dp;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001848 int bucket = cb->args[0], skip = cb->args[1];
1849 int i, j = 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001850
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001851 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001852 if (!dp)
1853 return -ENODEV;
1854
1855 rcu_read_lock();
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001856 for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
Jesse Grossccb13522011-10-25 19:26:31 -07001857 struct vport *vport;
1858
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001859 j = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001860 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001861 if (j >= skip &&
1862 ovs_vport_cmd_fill_info(vport, skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001863 NETLINK_CB(cb->skb).portid,
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001864 cb->nlh->nlmsg_seq,
1865 NLM_F_MULTI,
1866 OVS_VPORT_CMD_NEW) < 0)
1867 goto out;
Jesse Grossccb13522011-10-25 19:26:31 -07001868
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001869 j++;
1870 }
1871 skip = 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001872 }
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001873out:
Jesse Grossccb13522011-10-25 19:26:31 -07001874 rcu_read_unlock();
1875
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001876 cb->args[0] = i;
1877 cb->args[1] = j;
Jesse Grossccb13522011-10-25 19:26:31 -07001878
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001879 return skb->len;
Jesse Grossccb13522011-10-25 19:26:31 -07001880}
1881
Jesse Grossccb13522011-10-25 19:26:31 -07001882static struct genl_ops dp_vport_genl_ops[] = {
1883 { .cmd = OVS_VPORT_CMD_NEW,
1884 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1885 .policy = vport_policy,
1886 .doit = ovs_vport_cmd_new
1887 },
1888 { .cmd = OVS_VPORT_CMD_DEL,
1889 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1890 .policy = vport_policy,
1891 .doit = ovs_vport_cmd_del
1892 },
1893 { .cmd = OVS_VPORT_CMD_GET,
1894 .flags = 0, /* OK for unprivileged users. */
1895 .policy = vport_policy,
1896 .doit = ovs_vport_cmd_get,
1897 .dumpit = ovs_vport_cmd_dump
1898 },
1899 { .cmd = OVS_VPORT_CMD_SET,
1900 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1901 .policy = vport_policy,
1902 .doit = ovs_vport_cmd_set,
1903 },
1904};
1905
1906struct genl_family_and_ops {
1907 struct genl_family *family;
1908 struct genl_ops *ops;
1909 int n_ops;
1910 struct genl_multicast_group *group;
1911};
1912
1913static const struct genl_family_and_ops dp_genl_families[] = {
1914 { &dp_datapath_genl_family,
1915 dp_datapath_genl_ops, ARRAY_SIZE(dp_datapath_genl_ops),
1916 &ovs_dp_datapath_multicast_group },
1917 { &dp_vport_genl_family,
1918 dp_vport_genl_ops, ARRAY_SIZE(dp_vport_genl_ops),
1919 &ovs_dp_vport_multicast_group },
1920 { &dp_flow_genl_family,
1921 dp_flow_genl_ops, ARRAY_SIZE(dp_flow_genl_ops),
1922 &ovs_dp_flow_multicast_group },
1923 { &dp_packet_genl_family,
1924 dp_packet_genl_ops, ARRAY_SIZE(dp_packet_genl_ops),
1925 NULL },
1926};
1927
1928static void dp_unregister_genl(int n_families)
1929{
1930 int i;
1931
1932 for (i = 0; i < n_families; i++)
1933 genl_unregister_family(dp_genl_families[i].family);
1934}
1935
1936static int dp_register_genl(void)
1937{
1938 int n_registered;
1939 int err;
1940 int i;
1941
1942 n_registered = 0;
1943 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
1944 const struct genl_family_and_ops *f = &dp_genl_families[i];
1945
1946 err = genl_register_family_with_ops(f->family, f->ops,
1947 f->n_ops);
1948 if (err)
1949 goto error;
1950 n_registered++;
1951
1952 if (f->group) {
1953 err = genl_register_mc_group(f->family, f->group);
1954 if (err)
1955 goto error;
1956 }
1957 }
1958
1959 return 0;
1960
1961error:
1962 dp_unregister_genl(n_registered);
1963 return err;
1964}
1965
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001966static void rehash_flow_table(struct work_struct *work)
1967{
1968 struct datapath *dp;
1969 struct net *net;
1970
1971 genl_lock();
1972 rtnl_lock();
1973 for_each_net(net) {
1974 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
1975
1976 list_for_each_entry(dp, &ovs_net->dps, list_node) {
1977 struct flow_table *old_table = genl_dereference(dp->table);
1978 struct flow_table *new_table;
1979
1980 new_table = ovs_flow_tbl_rehash(old_table);
1981 if (!IS_ERR(new_table)) {
1982 rcu_assign_pointer(dp->table, new_table);
1983 ovs_flow_tbl_deferred_destroy(old_table);
1984 }
1985 }
1986 }
1987 rtnl_unlock();
1988 genl_unlock();
1989
1990 schedule_delayed_work(&rehash_flow_wq, REHASH_FLOW_INTERVAL);
1991}
1992
1993static int __net_init ovs_init_net(struct net *net)
1994{
1995 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
1996
1997 INIT_LIST_HEAD(&ovs_net->dps);
1998 return 0;
1999}
2000
2001static void __net_exit ovs_exit_net(struct net *net)
2002{
2003 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2004 struct datapath *dp, *dp_next;
2005
2006 genl_lock();
2007 list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
2008 __dp_destroy(dp);
2009 genl_unlock();
2010}
2011
2012static struct pernet_operations ovs_net_ops = {
2013 .init = ovs_init_net,
2014 .exit = ovs_exit_net,
2015 .id = &ovs_net_id,
2016 .size = sizeof(struct ovs_net),
2017};
2018
Jesse Grossccb13522011-10-25 19:26:31 -07002019static int __init dp_init(void)
2020{
Jesse Grossccb13522011-10-25 19:26:31 -07002021 int err;
2022
YOSHIFUJI Hideaki / 吉藤英明3523b292013-01-09 07:19:55 +00002023 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
Jesse Grossccb13522011-10-25 19:26:31 -07002024
2025 pr_info("Open vSwitch switching datapath\n");
2026
2027 err = ovs_flow_init();
2028 if (err)
2029 goto error;
2030
2031 err = ovs_vport_init();
2032 if (err)
2033 goto error_flow_exit;
2034
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002035 err = register_pernet_device(&ovs_net_ops);
Jesse Grossccb13522011-10-25 19:26:31 -07002036 if (err)
2037 goto error_vport_exit;
2038
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002039 err = register_netdevice_notifier(&ovs_dp_device_notifier);
2040 if (err)
2041 goto error_netns_exit;
2042
Jesse Grossccb13522011-10-25 19:26:31 -07002043 err = dp_register_genl();
2044 if (err < 0)
2045 goto error_unreg_notifier;
2046
2047 schedule_delayed_work(&rehash_flow_wq, REHASH_FLOW_INTERVAL);
2048
2049 return 0;
2050
2051error_unreg_notifier:
2052 unregister_netdevice_notifier(&ovs_dp_device_notifier);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002053error_netns_exit:
2054 unregister_pernet_device(&ovs_net_ops);
Jesse Grossccb13522011-10-25 19:26:31 -07002055error_vport_exit:
2056 ovs_vport_exit();
2057error_flow_exit:
2058 ovs_flow_exit();
2059error:
2060 return err;
2061}
2062
2063static void dp_cleanup(void)
2064{
2065 cancel_delayed_work_sync(&rehash_flow_wq);
Jesse Grossccb13522011-10-25 19:26:31 -07002066 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
2067 unregister_netdevice_notifier(&ovs_dp_device_notifier);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002068 unregister_pernet_device(&ovs_net_ops);
2069 rcu_barrier();
Jesse Grossccb13522011-10-25 19:26:31 -07002070 ovs_vport_exit();
2071 ovs_flow_exit();
2072}
2073
2074module_init(dp_init);
2075module_exit(dp_cleanup);
2076
2077MODULE_DESCRIPTION("Open vSwitch switching datapath");
2078MODULE_LICENSE("GPL");