blob: d29cd9aa4a678cf57deb14afcd148d339c12b55a [file] [log] [blame]
Jesse Grossccb13522011-10-25 19:26:31 -07001/*
Andy Zhou03f0d912013-08-07 20:01:00 -07002 * Copyright (c) 2007-2013 Nicira, Inc.
Jesse Grossccb13522011-10-25 19:26:31 -07003 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of version 2 of the GNU General Public
6 * License as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 * 02110-1301, USA
17 */
18
19#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20
21#include <linux/init.h>
22#include <linux/module.h>
23#include <linux/if_arp.h>
24#include <linux/if_vlan.h>
25#include <linux/in.h>
26#include <linux/ip.h>
27#include <linux/jhash.h>
28#include <linux/delay.h>
29#include <linux/time.h>
30#include <linux/etherdevice.h>
31#include <linux/genetlink.h>
32#include <linux/kernel.h>
33#include <linux/kthread.h>
34#include <linux/mutex.h>
35#include <linux/percpu.h>
36#include <linux/rcupdate.h>
37#include <linux/tcp.h>
38#include <linux/udp.h>
Jesse Grossccb13522011-10-25 19:26:31 -070039#include <linux/ethtool.h>
40#include <linux/wait.h>
Jesse Grossccb13522011-10-25 19:26:31 -070041#include <asm/div64.h>
42#include <linux/highmem.h>
43#include <linux/netfilter_bridge.h>
44#include <linux/netfilter_ipv4.h>
45#include <linux/inetdevice.h>
46#include <linux/list.h>
Pravin B Shelar8e4e1712013-04-15 13:23:03 -070047#include <linux/lockdep.h>
Jesse Grossccb13522011-10-25 19:26:31 -070048#include <linux/openvswitch.h>
49#include <linux/rculist.h>
50#include <linux/dmi.h>
51#include <linux/workqueue.h>
52#include <net/genetlink.h>
Pravin B Shelar46df7b82012-02-22 19:58:59 -080053#include <net/net_namespace.h>
54#include <net/netns/generic.h>
Jesse Grossccb13522011-10-25 19:26:31 -070055
56#include "datapath.h"
57#include "flow.h"
58#include "vport-internal_dev.h"
Thomas Grafcff63a52013-04-29 13:06:41 +000059#include "vport-netdev.h"
Jesse Grossccb13522011-10-25 19:26:31 -070060
Pravin B Shelar46df7b82012-02-22 19:58:59 -080061
62#define REHASH_FLOW_INTERVAL (10 * 60 * HZ)
63static void rehash_flow_table(struct work_struct *work);
64static DECLARE_DELAYED_WORK(rehash_flow_wq, rehash_flow_table);
65
Pravin B Shelar8e4e1712013-04-15 13:23:03 -070066int ovs_net_id __read_mostly;
67
Thomas Grafed661182013-03-29 14:46:50 +010068static void ovs_notify(struct sk_buff *skb, struct genl_info *info,
69 struct genl_multicast_group *grp)
70{
71 genl_notify(skb, genl_info_net(info), info->snd_portid,
72 grp->id, info->nlhdr, GFP_KERNEL);
73}
74
Pravin B Shelar46df7b82012-02-22 19:58:59 -080075/**
Jesse Grossccb13522011-10-25 19:26:31 -070076 * DOC: Locking:
77 *
Pravin B Shelar8e4e1712013-04-15 13:23:03 -070078 * All writes e.g. Writes to device state (add/remove datapath, port, set
79 * operations on vports, etc.), Writes to other state (flow table
80 * modifications, set miscellaneous datapath parameters, etc.) are protected
81 * by ovs_lock.
Jesse Grossccb13522011-10-25 19:26:31 -070082 *
83 * Reads are protected by RCU.
84 *
85 * There are a few special cases (mostly stats) that have their own
86 * synchronization but they nest under all of above and don't interact with
87 * each other.
Pravin B Shelar8e4e1712013-04-15 13:23:03 -070088 *
89 * The RTNL lock nests inside ovs_mutex.
Jesse Grossccb13522011-10-25 19:26:31 -070090 */
91
Pravin B Shelar8e4e1712013-04-15 13:23:03 -070092static DEFINE_MUTEX(ovs_mutex);
93
94void ovs_lock(void)
95{
96 mutex_lock(&ovs_mutex);
97}
98
99void ovs_unlock(void)
100{
101 mutex_unlock(&ovs_mutex);
102}
103
104#ifdef CONFIG_LOCKDEP
105int lockdep_ovsl_is_held(void)
106{
107 if (debug_locks)
108 return lockdep_is_held(&ovs_mutex);
109 else
110 return 1;
111}
112#endif
113
Jesse Grossccb13522011-10-25 19:26:31 -0700114static struct vport *new_vport(const struct vport_parms *);
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800115static int queue_gso_packets(struct net *, int dp_ifindex, struct sk_buff *,
Jesse Grossccb13522011-10-25 19:26:31 -0700116 const struct dp_upcall_info *);
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800117static int queue_userspace_packet(struct net *, int dp_ifindex,
118 struct sk_buff *,
Jesse Grossccb13522011-10-25 19:26:31 -0700119 const struct dp_upcall_info *);
120
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700121/* Must be called with rcu_read_lock or ovs_mutex. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800122static struct datapath *get_dp(struct net *net, int dp_ifindex)
Jesse Grossccb13522011-10-25 19:26:31 -0700123{
124 struct datapath *dp = NULL;
125 struct net_device *dev;
126
127 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800128 dev = dev_get_by_index_rcu(net, dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -0700129 if (dev) {
130 struct vport *vport = ovs_internal_dev_get_vport(dev);
131 if (vport)
132 dp = vport->dp;
133 }
134 rcu_read_unlock();
135
136 return dp;
137}
138
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700139/* Must be called with rcu_read_lock or ovs_mutex. */
Jesse Grossccb13522011-10-25 19:26:31 -0700140const char *ovs_dp_name(const struct datapath *dp)
141{
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700142 struct vport *vport = ovs_vport_ovsl_rcu(dp, OVSP_LOCAL);
Jesse Grossccb13522011-10-25 19:26:31 -0700143 return vport->ops->get_name(vport);
144}
145
146static int get_dpifindex(struct datapath *dp)
147{
148 struct vport *local;
149 int ifindex;
150
151 rcu_read_lock();
152
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700153 local = ovs_vport_rcu(dp, OVSP_LOCAL);
Jesse Grossccb13522011-10-25 19:26:31 -0700154 if (local)
Thomas Grafcff63a52013-04-29 13:06:41 +0000155 ifindex = netdev_vport_priv(local)->dev->ifindex;
Jesse Grossccb13522011-10-25 19:26:31 -0700156 else
157 ifindex = 0;
158
159 rcu_read_unlock();
160
161 return ifindex;
162}
163
164static void destroy_dp_rcu(struct rcu_head *rcu)
165{
166 struct datapath *dp = container_of(rcu, struct datapath, rcu);
167
Andy Zhou03f0d912013-08-07 20:01:00 -0700168 ovs_flow_tbl_destroy((__force struct flow_table *)dp->table, false);
Jesse Grossccb13522011-10-25 19:26:31 -0700169 free_percpu(dp->stats_percpu);
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800170 release_net(ovs_dp_get_net(dp));
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700171 kfree(dp->ports);
Jesse Grossccb13522011-10-25 19:26:31 -0700172 kfree(dp);
173}
174
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700175static struct hlist_head *vport_hash_bucket(const struct datapath *dp,
176 u16 port_no)
177{
178 return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)];
179}
180
181struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no)
182{
183 struct vport *vport;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700184 struct hlist_head *head;
185
186 head = vport_hash_bucket(dp, port_no);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800187 hlist_for_each_entry_rcu(vport, head, dp_hash_node) {
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700188 if (vport->port_no == port_no)
189 return vport;
190 }
191 return NULL;
192}
193
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700194/* Called with ovs_mutex. */
Jesse Grossccb13522011-10-25 19:26:31 -0700195static struct vport *new_vport(const struct vport_parms *parms)
196{
197 struct vport *vport;
198
199 vport = ovs_vport_add(parms);
200 if (!IS_ERR(vport)) {
201 struct datapath *dp = parms->dp;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700202 struct hlist_head *head = vport_hash_bucket(dp, vport->port_no);
Jesse Grossccb13522011-10-25 19:26:31 -0700203
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700204 hlist_add_head_rcu(&vport->dp_hash_node, head);
Jesse Grossccb13522011-10-25 19:26:31 -0700205 }
Jesse Grossccb13522011-10-25 19:26:31 -0700206 return vport;
207}
208
Jesse Grossccb13522011-10-25 19:26:31 -0700209void ovs_dp_detach_port(struct vport *p)
210{
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700211 ASSERT_OVSL();
Jesse Grossccb13522011-10-25 19:26:31 -0700212
213 /* First drop references to device. */
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700214 hlist_del_rcu(&p->dp_hash_node);
Jesse Grossccb13522011-10-25 19:26:31 -0700215
216 /* Then destroy it. */
217 ovs_vport_del(p);
218}
219
220/* Must be called with rcu_read_lock. */
221void ovs_dp_process_received_packet(struct vport *p, struct sk_buff *skb)
222{
223 struct datapath *dp = p->dp;
224 struct sw_flow *flow;
225 struct dp_stats_percpu *stats;
226 struct sw_flow_key key;
227 u64 *stats_counter;
228 int error;
Jesse Grossccb13522011-10-25 19:26:31 -0700229
Shan Wei404f2f12012-11-13 09:52:25 +0800230 stats = this_cpu_ptr(dp->stats_percpu);
Jesse Grossccb13522011-10-25 19:26:31 -0700231
232 /* Extract flow from 'skb' into 'key'. */
Andy Zhou03f0d912013-08-07 20:01:00 -0700233 error = ovs_flow_extract(skb, p->port_no, &key);
Jesse Grossccb13522011-10-25 19:26:31 -0700234 if (unlikely(error)) {
235 kfree_skb(skb);
236 return;
237 }
238
239 /* Look up flow. */
Andy Zhou03f0d912013-08-07 20:01:00 -0700240 flow = ovs_flow_lookup(rcu_dereference(dp->table), &key);
Jesse Grossccb13522011-10-25 19:26:31 -0700241 if (unlikely(!flow)) {
242 struct dp_upcall_info upcall;
243
244 upcall.cmd = OVS_PACKET_CMD_MISS;
245 upcall.key = &key;
246 upcall.userdata = NULL;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000247 upcall.portid = p->upcall_portid;
Jesse Grossccb13522011-10-25 19:26:31 -0700248 ovs_dp_upcall(dp, skb, &upcall);
249 consume_skb(skb);
250 stats_counter = &stats->n_missed;
251 goto out;
252 }
253
254 OVS_CB(skb)->flow = flow;
Andy Zhou03f0d912013-08-07 20:01:00 -0700255 OVS_CB(skb)->pkt_key = &key;
Jesse Grossccb13522011-10-25 19:26:31 -0700256
257 stats_counter = &stats->n_hit;
258 ovs_flow_used(OVS_CB(skb)->flow, skb);
259 ovs_execute_actions(dp, skb);
260
261out:
262 /* Update datapath statistics. */
263 u64_stats_update_begin(&stats->sync);
264 (*stats_counter)++;
265 u64_stats_update_end(&stats->sync);
266}
267
268static struct genl_family dp_packet_genl_family = {
269 .id = GENL_ID_GENERATE,
270 .hdrsize = sizeof(struct ovs_header),
271 .name = OVS_PACKET_FAMILY,
272 .version = OVS_PACKET_VERSION,
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800273 .maxattr = OVS_PACKET_ATTR_MAX,
Pravin B Shelar3a4e0d62013-04-23 07:48:48 +0000274 .netnsok = true,
275 .parallel_ops = true,
Jesse Grossccb13522011-10-25 19:26:31 -0700276};
277
278int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800279 const struct dp_upcall_info *upcall_info)
Jesse Grossccb13522011-10-25 19:26:31 -0700280{
281 struct dp_stats_percpu *stats;
282 int dp_ifindex;
283 int err;
284
Eric W. Biederman15e47302012-09-07 20:12:54 +0000285 if (upcall_info->portid == 0) {
Jesse Grossccb13522011-10-25 19:26:31 -0700286 err = -ENOTCONN;
287 goto err;
288 }
289
290 dp_ifindex = get_dpifindex(dp);
291 if (!dp_ifindex) {
292 err = -ENODEV;
293 goto err;
294 }
295
296 if (!skb_is_gso(skb))
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800297 err = queue_userspace_packet(ovs_dp_get_net(dp), dp_ifindex, skb, upcall_info);
Jesse Grossccb13522011-10-25 19:26:31 -0700298 else
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800299 err = queue_gso_packets(ovs_dp_get_net(dp), dp_ifindex, skb, upcall_info);
Jesse Grossccb13522011-10-25 19:26:31 -0700300 if (err)
301 goto err;
302
303 return 0;
304
305err:
Shan Wei404f2f12012-11-13 09:52:25 +0800306 stats = this_cpu_ptr(dp->stats_percpu);
Jesse Grossccb13522011-10-25 19:26:31 -0700307
308 u64_stats_update_begin(&stats->sync);
309 stats->n_lost++;
310 u64_stats_update_end(&stats->sync);
311
312 return err;
313}
314
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800315static int queue_gso_packets(struct net *net, int dp_ifindex,
316 struct sk_buff *skb,
Jesse Grossccb13522011-10-25 19:26:31 -0700317 const struct dp_upcall_info *upcall_info)
318{
Ben Pfaffa1b5d0d2012-07-20 14:47:54 -0700319 unsigned short gso_type = skb_shinfo(skb)->gso_type;
Jesse Grossccb13522011-10-25 19:26:31 -0700320 struct dp_upcall_info later_info;
321 struct sw_flow_key later_key;
322 struct sk_buff *segs, *nskb;
323 int err;
324
Cong Wang12b00042013-02-05 16:36:38 +0000325 segs = __skb_gso_segment(skb, NETIF_F_SG | NETIF_F_HW_CSUM, false);
Pravin B Shelar92e5dfc2012-07-20 14:46:29 -0700326 if (IS_ERR(segs))
327 return PTR_ERR(segs);
Jesse Grossccb13522011-10-25 19:26:31 -0700328
329 /* Queue all of the segments. */
330 skb = segs;
331 do {
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800332 err = queue_userspace_packet(net, dp_ifindex, skb, upcall_info);
Jesse Grossccb13522011-10-25 19:26:31 -0700333 if (err)
334 break;
335
Ben Pfaffa1b5d0d2012-07-20 14:47:54 -0700336 if (skb == segs && gso_type & SKB_GSO_UDP) {
Jesse Grossccb13522011-10-25 19:26:31 -0700337 /* The initial flow key extracted by ovs_flow_extract()
338 * in this case is for a first fragment, so we need to
339 * properly mark later fragments.
340 */
341 later_key = *upcall_info->key;
342 later_key.ip.frag = OVS_FRAG_TYPE_LATER;
343
344 later_info = *upcall_info;
345 later_info.key = &later_key;
346 upcall_info = &later_info;
347 }
348 } while ((skb = skb->next));
349
350 /* Free all of the segments. */
351 skb = segs;
352 do {
353 nskb = skb->next;
354 if (err)
355 kfree_skb(skb);
356 else
357 consume_skb(skb);
358 } while ((skb = nskb));
359 return err;
360}
361
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100362static size_t key_attr_size(void)
363{
364 return nla_total_size(4) /* OVS_KEY_ATTR_PRIORITY */
Pravin B Shelar7d5437c2013-06-17 17:50:18 -0700365 + nla_total_size(0) /* OVS_KEY_ATTR_TUNNEL */
366 + nla_total_size(8) /* OVS_TUNNEL_KEY_ATTR_ID */
367 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_SRC */
368 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_DST */
369 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TOS */
370 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TTL */
371 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT */
372 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_CSUM */
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100373 + nla_total_size(4) /* OVS_KEY_ATTR_IN_PORT */
374 + nla_total_size(4) /* OVS_KEY_ATTR_SKB_MARK */
375 + nla_total_size(12) /* OVS_KEY_ATTR_ETHERNET */
376 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
377 + nla_total_size(4) /* OVS_KEY_ATTR_8021Q */
378 + nla_total_size(0) /* OVS_KEY_ATTR_ENCAP */
379 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
380 + nla_total_size(40) /* OVS_KEY_ATTR_IPV6 */
381 + nla_total_size(2) /* OVS_KEY_ATTR_ICMPV6 */
382 + nla_total_size(28); /* OVS_KEY_ATTR_ND */
383}
384
385static size_t upcall_msg_size(const struct sk_buff *skb,
386 const struct nlattr *userdata)
387{
388 size_t size = NLMSG_ALIGN(sizeof(struct ovs_header))
389 + nla_total_size(skb->len) /* OVS_PACKET_ATTR_PACKET */
390 + nla_total_size(key_attr_size()); /* OVS_PACKET_ATTR_KEY */
391
392 /* OVS_PACKET_ATTR_USERDATA */
393 if (userdata)
394 size += NLA_ALIGN(userdata->nla_len);
395
396 return size;
397}
398
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800399static int queue_userspace_packet(struct net *net, int dp_ifindex,
400 struct sk_buff *skb,
Jesse Grossccb13522011-10-25 19:26:31 -0700401 const struct dp_upcall_info *upcall_info)
402{
403 struct ovs_header *upcall;
404 struct sk_buff *nskb = NULL;
405 struct sk_buff *user_skb; /* to be queued to userspace */
406 struct nlattr *nla;
Jesse Grossccb13522011-10-25 19:26:31 -0700407 int err;
408
409 if (vlan_tx_tag_present(skb)) {
410 nskb = skb_clone(skb, GFP_ATOMIC);
411 if (!nskb)
412 return -ENOMEM;
413
Patrick McHardy86a9bad2013-04-19 02:04:30 +0000414 nskb = __vlan_put_tag(nskb, nskb->vlan_proto, vlan_tx_tag_get(nskb));
Dan Carpenter8aa51d62012-05-13 08:44:18 +0000415 if (!nskb)
Jesse Grossccb13522011-10-25 19:26:31 -0700416 return -ENOMEM;
417
418 nskb->vlan_tci = 0;
419 skb = nskb;
420 }
421
422 if (nla_attr_size(skb->len) > USHRT_MAX) {
423 err = -EFBIG;
424 goto out;
425 }
426
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100427 user_skb = genlmsg_new(upcall_msg_size(skb, upcall_info->userdata), GFP_ATOMIC);
Jesse Grossccb13522011-10-25 19:26:31 -0700428 if (!user_skb) {
429 err = -ENOMEM;
430 goto out;
431 }
432
433 upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
434 0, upcall_info->cmd);
435 upcall->dp_ifindex = dp_ifindex;
436
437 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
Andy Zhou03f0d912013-08-07 20:01:00 -0700438 ovs_flow_to_nlattrs(upcall_info->key, upcall_info->key, user_skb);
Jesse Grossccb13522011-10-25 19:26:31 -0700439 nla_nest_end(user_skb, nla);
440
441 if (upcall_info->userdata)
Ben Pfaff44901082013-02-15 17:29:22 -0800442 __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA,
443 nla_len(upcall_info->userdata),
444 nla_data(upcall_info->userdata));
Jesse Grossccb13522011-10-25 19:26:31 -0700445
446 nla = __nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, skb->len);
447
448 skb_copy_and_csum_dev(skb, nla_data(nla));
449
Rich Lanea15ff762013-02-15 11:07:43 -0800450 genlmsg_end(user_skb, upcall);
Eric W. Biederman15e47302012-09-07 20:12:54 +0000451 err = genlmsg_unicast(net, user_skb, upcall_info->portid);
Jesse Grossccb13522011-10-25 19:26:31 -0700452
453out:
454 kfree_skb(nskb);
455 return err;
456}
457
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700458/* Called with ovs_mutex. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800459static int flush_flows(struct datapath *dp)
Jesse Grossccb13522011-10-25 19:26:31 -0700460{
461 struct flow_table *old_table;
462 struct flow_table *new_table;
Jesse Grossccb13522011-10-25 19:26:31 -0700463
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700464 old_table = ovsl_dereference(dp->table);
Jesse Grossccb13522011-10-25 19:26:31 -0700465 new_table = ovs_flow_tbl_alloc(TBL_MIN_BUCKETS);
466 if (!new_table)
467 return -ENOMEM;
468
469 rcu_assign_pointer(dp->table, new_table);
470
Andy Zhou03f0d912013-08-07 20:01:00 -0700471 ovs_flow_tbl_destroy(old_table, true);
Jesse Grossccb13522011-10-25 19:26:31 -0700472 return 0;
473}
474
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700475static struct nlattr *reserve_sfa_size(struct sw_flow_actions **sfa, int attr_len)
476{
Jesse Grossccb13522011-10-25 19:26:31 -0700477
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700478 struct sw_flow_actions *acts;
479 int new_acts_size;
480 int req_size = NLA_ALIGN(attr_len);
481 int next_offset = offsetof(struct sw_flow_actions, actions) +
482 (*sfa)->actions_len;
483
484 if (req_size <= (ksize(*sfa) - next_offset))
485 goto out;
486
487 new_acts_size = ksize(*sfa) * 2;
488
489 if (new_acts_size > MAX_ACTIONS_BUFSIZE) {
490 if ((MAX_ACTIONS_BUFSIZE - next_offset) < req_size)
491 return ERR_PTR(-EMSGSIZE);
492 new_acts_size = MAX_ACTIONS_BUFSIZE;
493 }
494
495 acts = ovs_flow_actions_alloc(new_acts_size);
496 if (IS_ERR(acts))
497 return (void *)acts;
498
499 memcpy(acts->actions, (*sfa)->actions, (*sfa)->actions_len);
500 acts->actions_len = (*sfa)->actions_len;
501 kfree(*sfa);
502 *sfa = acts;
503
504out:
505 (*sfa)->actions_len += req_size;
506 return (struct nlattr *) ((unsigned char *)(*sfa) + next_offset);
507}
508
509static int add_action(struct sw_flow_actions **sfa, int attrtype, void *data, int len)
510{
511 struct nlattr *a;
512
513 a = reserve_sfa_size(sfa, nla_attr_size(len));
514 if (IS_ERR(a))
515 return PTR_ERR(a);
516
517 a->nla_type = attrtype;
518 a->nla_len = nla_attr_size(len);
519
520 if (data)
521 memcpy(nla_data(a), data, len);
522 memset((unsigned char *) a + a->nla_len, 0, nla_padlen(len));
523
524 return 0;
525}
526
527static inline int add_nested_action_start(struct sw_flow_actions **sfa, int attrtype)
528{
529 int used = (*sfa)->actions_len;
530 int err;
531
532 err = add_action(sfa, attrtype, NULL, 0);
533 if (err)
534 return err;
535
536 return used;
537}
538
539static inline void add_nested_action_end(struct sw_flow_actions *sfa, int st_offset)
540{
541 struct nlattr *a = (struct nlattr *) ((unsigned char *)sfa->actions + st_offset);
542
543 a->nla_len = sfa->actions_len - st_offset;
544}
545
546static int validate_and_copy_actions(const struct nlattr *attr,
547 const struct sw_flow_key *key, int depth,
548 struct sw_flow_actions **sfa);
549
550static int validate_and_copy_sample(const struct nlattr *attr,
551 const struct sw_flow_key *key, int depth,
552 struct sw_flow_actions **sfa)
Jesse Grossccb13522011-10-25 19:26:31 -0700553{
554 const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
555 const struct nlattr *probability, *actions;
556 const struct nlattr *a;
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700557 int rem, start, err, st_acts;
Jesse Grossccb13522011-10-25 19:26:31 -0700558
559 memset(attrs, 0, sizeof(attrs));
560 nla_for_each_nested(a, attr, rem) {
561 int type = nla_type(a);
562 if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
563 return -EINVAL;
564 attrs[type] = a;
565 }
566 if (rem)
567 return -EINVAL;
568
569 probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
570 if (!probability || nla_len(probability) != sizeof(u32))
571 return -EINVAL;
572
573 actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
574 if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
575 return -EINVAL;
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700576
577 /* validation done, copy sample action. */
578 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SAMPLE);
579 if (start < 0)
580 return start;
581 err = add_action(sfa, OVS_SAMPLE_ATTR_PROBABILITY, nla_data(probability), sizeof(u32));
582 if (err)
583 return err;
584 st_acts = add_nested_action_start(sfa, OVS_SAMPLE_ATTR_ACTIONS);
585 if (st_acts < 0)
586 return st_acts;
587
588 err = validate_and_copy_actions(actions, key, depth + 1, sfa);
589 if (err)
590 return err;
591
592 add_nested_action_end(*sfa, st_acts);
593 add_nested_action_end(*sfa, start);
594
595 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -0700596}
597
Pravin B Shelar072ae632012-05-07 17:21:53 -0700598static int validate_tp_port(const struct sw_flow_key *flow_key)
599{
600 if (flow_key->eth.type == htons(ETH_P_IP)) {
Jesse Gross41853922012-08-06 15:49:47 -0700601 if (flow_key->ipv4.tp.src || flow_key->ipv4.tp.dst)
Pravin B Shelar072ae632012-05-07 17:21:53 -0700602 return 0;
603 } else if (flow_key->eth.type == htons(ETH_P_IPV6)) {
Jesse Gross41853922012-08-06 15:49:47 -0700604 if (flow_key->ipv6.tp.src || flow_key->ipv6.tp.dst)
Pravin B Shelar072ae632012-05-07 17:21:53 -0700605 return 0;
606 }
607
608 return -EINVAL;
609}
610
Pravin B Shelar7d5437c2013-06-17 17:50:18 -0700611static int validate_and_copy_set_tun(const struct nlattr *attr,
612 struct sw_flow_actions **sfa)
613{
Andy Zhou03f0d912013-08-07 20:01:00 -0700614 struct sw_flow_match match;
615 struct sw_flow_key key;
Pravin B Shelar7d5437c2013-06-17 17:50:18 -0700616 int err, start;
617
Andy Zhou03f0d912013-08-07 20:01:00 -0700618 ovs_match_init(&match, &key, NULL);
619 err = ovs_ipv4_tun_from_nlattr(nla_data(attr), &match, false);
Pravin B Shelar7d5437c2013-06-17 17:50:18 -0700620 if (err)
621 return err;
622
623 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SET);
624 if (start < 0)
625 return start;
626
Andy Zhou03f0d912013-08-07 20:01:00 -0700627 err = add_action(sfa, OVS_KEY_ATTR_IPV4_TUNNEL, &match.key->tun_key,
628 sizeof(match.key->tun_key));
Pravin B Shelar7d5437c2013-06-17 17:50:18 -0700629 add_nested_action_end(*sfa, start);
630
631 return err;
632}
633
Jesse Grossccb13522011-10-25 19:26:31 -0700634static int validate_set(const struct nlattr *a,
Pravin B Shelar7d5437c2013-06-17 17:50:18 -0700635 const struct sw_flow_key *flow_key,
636 struct sw_flow_actions **sfa,
637 bool *set_tun)
Jesse Grossccb13522011-10-25 19:26:31 -0700638{
639 const struct nlattr *ovs_key = nla_data(a);
640 int key_type = nla_type(ovs_key);
641
642 /* There can be only one key in a action */
643 if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
644 return -EINVAL;
645
646 if (key_type > OVS_KEY_ATTR_MAX ||
Pravin B Shelar7d5437c2013-06-17 17:50:18 -0700647 (ovs_key_lens[key_type] != nla_len(ovs_key) &&
648 ovs_key_lens[key_type] != -1))
Jesse Grossccb13522011-10-25 19:26:31 -0700649 return -EINVAL;
650
651 switch (key_type) {
652 const struct ovs_key_ipv4 *ipv4_key;
Ansis Atteka3fdbd1c2012-11-13 15:44:14 -0800653 const struct ovs_key_ipv6 *ipv6_key;
Pravin B Shelar7d5437c2013-06-17 17:50:18 -0700654 int err;
Jesse Grossccb13522011-10-25 19:26:31 -0700655
656 case OVS_KEY_ATTR_PRIORITY:
Ansis Atteka39c7caeb2012-11-26 11:24:11 -0800657 case OVS_KEY_ATTR_SKB_MARK:
Jesse Grossccb13522011-10-25 19:26:31 -0700658 case OVS_KEY_ATTR_ETHERNET:
659 break;
660
Pravin B Shelar7d5437c2013-06-17 17:50:18 -0700661 case OVS_KEY_ATTR_TUNNEL:
662 *set_tun = true;
663 err = validate_and_copy_set_tun(a, sfa);
664 if (err)
665 return err;
666 break;
667
Jesse Grossccb13522011-10-25 19:26:31 -0700668 case OVS_KEY_ATTR_IPV4:
669 if (flow_key->eth.type != htons(ETH_P_IP))
670 return -EINVAL;
671
Jesse Gross41853922012-08-06 15:49:47 -0700672 if (!flow_key->ip.proto)
Jesse Grossccb13522011-10-25 19:26:31 -0700673 return -EINVAL;
674
675 ipv4_key = nla_data(ovs_key);
676 if (ipv4_key->ipv4_proto != flow_key->ip.proto)
677 return -EINVAL;
678
679 if (ipv4_key->ipv4_frag != flow_key->ip.frag)
680 return -EINVAL;
681
682 break;
683
Ansis Atteka3fdbd1c2012-11-13 15:44:14 -0800684 case OVS_KEY_ATTR_IPV6:
685 if (flow_key->eth.type != htons(ETH_P_IPV6))
686 return -EINVAL;
687
688 if (!flow_key->ip.proto)
689 return -EINVAL;
690
691 ipv6_key = nla_data(ovs_key);
692 if (ipv6_key->ipv6_proto != flow_key->ip.proto)
693 return -EINVAL;
694
695 if (ipv6_key->ipv6_frag != flow_key->ip.frag)
696 return -EINVAL;
697
698 if (ntohl(ipv6_key->ipv6_label) & 0xFFF00000)
699 return -EINVAL;
700
701 break;
702
Jesse Grossccb13522011-10-25 19:26:31 -0700703 case OVS_KEY_ATTR_TCP:
704 if (flow_key->ip.proto != IPPROTO_TCP)
705 return -EINVAL;
706
Pravin B Shelar072ae632012-05-07 17:21:53 -0700707 return validate_tp_port(flow_key);
Jesse Grossccb13522011-10-25 19:26:31 -0700708
709 case OVS_KEY_ATTR_UDP:
710 if (flow_key->ip.proto != IPPROTO_UDP)
711 return -EINVAL;
712
Pravin B Shelar072ae632012-05-07 17:21:53 -0700713 return validate_tp_port(flow_key);
Jesse Grossccb13522011-10-25 19:26:31 -0700714
715 default:
716 return -EINVAL;
717 }
718
719 return 0;
720}
721
722static int validate_userspace(const struct nlattr *attr)
723{
724 static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
725 [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
Ben Pfaff44901082013-02-15 17:29:22 -0800726 [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_UNSPEC },
Jesse Grossccb13522011-10-25 19:26:31 -0700727 };
728 struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
729 int error;
730
731 error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX,
732 attr, userspace_policy);
733 if (error)
734 return error;
735
736 if (!a[OVS_USERSPACE_ATTR_PID] ||
737 !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
738 return -EINVAL;
739
740 return 0;
741}
742
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700743static int copy_action(const struct nlattr *from,
744 struct sw_flow_actions **sfa)
745{
746 int totlen = NLA_ALIGN(from->nla_len);
747 struct nlattr *to;
748
749 to = reserve_sfa_size(sfa, from->nla_len);
750 if (IS_ERR(to))
751 return PTR_ERR(to);
752
753 memcpy(to, from, totlen);
754 return 0;
755}
756
757static int validate_and_copy_actions(const struct nlattr *attr,
758 const struct sw_flow_key *key,
759 int depth,
760 struct sw_flow_actions **sfa)
Jesse Grossccb13522011-10-25 19:26:31 -0700761{
762 const struct nlattr *a;
763 int rem, err;
764
765 if (depth >= SAMPLE_ACTION_DEPTH)
766 return -EOVERFLOW;
767
768 nla_for_each_nested(a, attr, rem) {
769 /* Expected argument lengths, (u32)-1 for variable length. */
770 static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
771 [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
772 [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
773 [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
774 [OVS_ACTION_ATTR_POP_VLAN] = 0,
775 [OVS_ACTION_ATTR_SET] = (u32)-1,
776 [OVS_ACTION_ATTR_SAMPLE] = (u32)-1
777 };
778 const struct ovs_action_push_vlan *vlan;
779 int type = nla_type(a);
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700780 bool skip_copy;
Jesse Grossccb13522011-10-25 19:26:31 -0700781
782 if (type > OVS_ACTION_ATTR_MAX ||
783 (action_lens[type] != nla_len(a) &&
784 action_lens[type] != (u32)-1))
785 return -EINVAL;
786
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700787 skip_copy = false;
Jesse Grossccb13522011-10-25 19:26:31 -0700788 switch (type) {
789 case OVS_ACTION_ATTR_UNSPEC:
790 return -EINVAL;
791
792 case OVS_ACTION_ATTR_USERSPACE:
793 err = validate_userspace(a);
794 if (err)
795 return err;
796 break;
797
798 case OVS_ACTION_ATTR_OUTPUT:
799 if (nla_get_u32(a) >= DP_MAX_PORTS)
800 return -EINVAL;
801 break;
802
803
804 case OVS_ACTION_ATTR_POP_VLAN:
805 break;
806
807 case OVS_ACTION_ATTR_PUSH_VLAN:
808 vlan = nla_data(a);
809 if (vlan->vlan_tpid != htons(ETH_P_8021Q))
810 return -EINVAL;
811 if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
812 return -EINVAL;
813 break;
814
815 case OVS_ACTION_ATTR_SET:
Pravin B Shelar7d5437c2013-06-17 17:50:18 -0700816 err = validate_set(a, key, sfa, &skip_copy);
Jesse Grossccb13522011-10-25 19:26:31 -0700817 if (err)
818 return err;
819 break;
820
821 case OVS_ACTION_ATTR_SAMPLE:
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700822 err = validate_and_copy_sample(a, key, depth, sfa);
Jesse Grossccb13522011-10-25 19:26:31 -0700823 if (err)
824 return err;
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700825 skip_copy = true;
Jesse Grossccb13522011-10-25 19:26:31 -0700826 break;
827
828 default:
829 return -EINVAL;
830 }
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700831 if (!skip_copy) {
832 err = copy_action(a, sfa);
833 if (err)
834 return err;
835 }
Jesse Grossccb13522011-10-25 19:26:31 -0700836 }
837
838 if (rem > 0)
839 return -EINVAL;
840
841 return 0;
842}
843
844static void clear_stats(struct sw_flow *flow)
845{
846 flow->used = 0;
847 flow->tcp_flags = 0;
848 flow->packet_count = 0;
849 flow->byte_count = 0;
850}
851
852static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
853{
854 struct ovs_header *ovs_header = info->userhdr;
855 struct nlattr **a = info->attrs;
856 struct sw_flow_actions *acts;
857 struct sk_buff *packet;
858 struct sw_flow *flow;
859 struct datapath *dp;
860 struct ethhdr *eth;
861 int len;
862 int err;
Jesse Grossccb13522011-10-25 19:26:31 -0700863
864 err = -EINVAL;
865 if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
Thomas Grafdded45fc2013-03-29 14:46:47 +0100866 !a[OVS_PACKET_ATTR_ACTIONS])
Jesse Grossccb13522011-10-25 19:26:31 -0700867 goto err;
868
869 len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
870 packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
871 err = -ENOMEM;
872 if (!packet)
873 goto err;
874 skb_reserve(packet, NET_IP_ALIGN);
875
Thomas Graf32686a92013-03-29 14:46:48 +0100876 nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
Jesse Grossccb13522011-10-25 19:26:31 -0700877
878 skb_reset_mac_header(packet);
879 eth = eth_hdr(packet);
880
881 /* Normally, setting the skb 'protocol' field would be handled by a
882 * call to eth_type_trans(), but it assumes there's a sending
883 * device, which we may not have. */
Simon Hormane5c5d222013-03-28 13:38:25 +0900884 if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN)
Jesse Grossccb13522011-10-25 19:26:31 -0700885 packet->protocol = eth->h_proto;
886 else
887 packet->protocol = htons(ETH_P_802_2);
888
889 /* Build an sw_flow for sending this packet. */
890 flow = ovs_flow_alloc();
891 err = PTR_ERR(flow);
892 if (IS_ERR(flow))
893 goto err_kfree_skb;
894
Andy Zhou03f0d912013-08-07 20:01:00 -0700895 err = ovs_flow_extract(packet, -1, &flow->key);
Jesse Grossccb13522011-10-25 19:26:31 -0700896 if (err)
897 goto err_flow_free;
898
Andy Zhou03f0d912013-08-07 20:01:00 -0700899 err = ovs_flow_metadata_from_nlattrs(flow, a[OVS_PACKET_ATTR_KEY]);
Jesse Grossccb13522011-10-25 19:26:31 -0700900 if (err)
901 goto err_flow_free;
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700902 acts = ovs_flow_actions_alloc(nla_len(a[OVS_PACKET_ATTR_ACTIONS]));
Jesse Grossccb13522011-10-25 19:26:31 -0700903 err = PTR_ERR(acts);
904 if (IS_ERR(acts))
905 goto err_flow_free;
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700906
907 err = validate_and_copy_actions(a[OVS_PACKET_ATTR_ACTIONS], &flow->key, 0, &acts);
Jesse Grossccb13522011-10-25 19:26:31 -0700908 rcu_assign_pointer(flow->sf_acts, acts);
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700909 if (err)
910 goto err_flow_free;
Jesse Grossccb13522011-10-25 19:26:31 -0700911
912 OVS_CB(packet)->flow = flow;
Andy Zhou03f0d912013-08-07 20:01:00 -0700913 OVS_CB(packet)->pkt_key = &flow->key;
Jesse Grossccb13522011-10-25 19:26:31 -0700914 packet->priority = flow->key.phy.priority;
Ansis Atteka39c7caeb2012-11-26 11:24:11 -0800915 packet->mark = flow->key.phy.skb_mark;
Jesse Grossccb13522011-10-25 19:26:31 -0700916
917 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800918 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -0700919 err = -ENODEV;
920 if (!dp)
921 goto err_unlock;
922
923 local_bh_disable();
924 err = ovs_execute_actions(dp, packet);
925 local_bh_enable();
926 rcu_read_unlock();
927
Andy Zhou03f0d912013-08-07 20:01:00 -0700928 ovs_flow_free(flow, false);
Jesse Grossccb13522011-10-25 19:26:31 -0700929 return err;
930
931err_unlock:
932 rcu_read_unlock();
933err_flow_free:
Andy Zhou03f0d912013-08-07 20:01:00 -0700934 ovs_flow_free(flow, false);
Jesse Grossccb13522011-10-25 19:26:31 -0700935err_kfree_skb:
936 kfree_skb(packet);
937err:
938 return err;
939}
940
941static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
Thomas Grafdded45fc2013-03-29 14:46:47 +0100942 [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
Jesse Grossccb13522011-10-25 19:26:31 -0700943 [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
944 [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
945};
946
947static struct genl_ops dp_packet_genl_ops[] = {
948 { .cmd = OVS_PACKET_CMD_EXECUTE,
949 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
950 .policy = packet_policy,
951 .doit = ovs_packet_cmd_execute
952 }
953};
954
955static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats)
956{
Pravin B Shelar59a35d62013-07-30 15:42:19 -0700957 struct flow_table *table;
Jesse Grossccb13522011-10-25 19:26:31 -0700958 int i;
Jesse Grossccb13522011-10-25 19:26:31 -0700959
Pravin B Shelar59a35d62013-07-30 15:42:19 -0700960 table = rcu_dereference_check(dp->table, lockdep_ovsl_is_held());
Jesse Grossccb13522011-10-25 19:26:31 -0700961 stats->n_flows = ovs_flow_tbl_count(table);
962
963 stats->n_hit = stats->n_missed = stats->n_lost = 0;
964 for_each_possible_cpu(i) {
965 const struct dp_stats_percpu *percpu_stats;
966 struct dp_stats_percpu local_stats;
967 unsigned int start;
968
969 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
970
971 do {
972 start = u64_stats_fetch_begin_bh(&percpu_stats->sync);
973 local_stats = *percpu_stats;
974 } while (u64_stats_fetch_retry_bh(&percpu_stats->sync, start));
975
976 stats->n_hit += local_stats.n_hit;
977 stats->n_missed += local_stats.n_missed;
978 stats->n_lost += local_stats.n_lost;
979 }
980}
981
982static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
983 [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
984 [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
985 [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
986};
987
988static struct genl_family dp_flow_genl_family = {
989 .id = GENL_ID_GENERATE,
990 .hdrsize = sizeof(struct ovs_header),
991 .name = OVS_FLOW_FAMILY,
992 .version = OVS_FLOW_VERSION,
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800993 .maxattr = OVS_FLOW_ATTR_MAX,
Pravin B Shelar3a4e0d62013-04-23 07:48:48 +0000994 .netnsok = true,
995 .parallel_ops = true,
Jesse Grossccb13522011-10-25 19:26:31 -0700996};
997
998static struct genl_multicast_group ovs_dp_flow_multicast_group = {
999 .name = OVS_FLOW_MCGROUP
1000};
1001
Pravin B Shelar74f84a52013-06-17 17:50:12 -07001002static int actions_to_attr(const struct nlattr *attr, int len, struct sk_buff *skb);
1003static int sample_action_to_attr(const struct nlattr *attr, struct sk_buff *skb)
1004{
1005 const struct nlattr *a;
1006 struct nlattr *start;
1007 int err = 0, rem;
1008
1009 start = nla_nest_start(skb, OVS_ACTION_ATTR_SAMPLE);
1010 if (!start)
1011 return -EMSGSIZE;
1012
1013 nla_for_each_nested(a, attr, rem) {
1014 int type = nla_type(a);
1015 struct nlattr *st_sample;
1016
1017 switch (type) {
1018 case OVS_SAMPLE_ATTR_PROBABILITY:
1019 if (nla_put(skb, OVS_SAMPLE_ATTR_PROBABILITY, sizeof(u32), nla_data(a)))
1020 return -EMSGSIZE;
1021 break;
1022 case OVS_SAMPLE_ATTR_ACTIONS:
1023 st_sample = nla_nest_start(skb, OVS_SAMPLE_ATTR_ACTIONS);
1024 if (!st_sample)
1025 return -EMSGSIZE;
1026 err = actions_to_attr(nla_data(a), nla_len(a), skb);
1027 if (err)
1028 return err;
1029 nla_nest_end(skb, st_sample);
1030 break;
1031 }
1032 }
1033
1034 nla_nest_end(skb, start);
1035 return err;
1036}
1037
Pravin B Shelar7d5437c2013-06-17 17:50:18 -07001038static int set_action_to_attr(const struct nlattr *a, struct sk_buff *skb)
1039{
1040 const struct nlattr *ovs_key = nla_data(a);
1041 int key_type = nla_type(ovs_key);
1042 struct nlattr *start;
1043 int err;
1044
1045 switch (key_type) {
1046 case OVS_KEY_ATTR_IPV4_TUNNEL:
1047 start = nla_nest_start(skb, OVS_ACTION_ATTR_SET);
1048 if (!start)
1049 return -EMSGSIZE;
1050
Andy Zhou03f0d912013-08-07 20:01:00 -07001051 err = ovs_ipv4_tun_to_nlattr(skb, nla_data(ovs_key),
1052 nla_data(ovs_key));
Pravin B Shelar7d5437c2013-06-17 17:50:18 -07001053 if (err)
1054 return err;
1055 nla_nest_end(skb, start);
1056 break;
1057 default:
1058 if (nla_put(skb, OVS_ACTION_ATTR_SET, nla_len(a), ovs_key))
1059 return -EMSGSIZE;
1060 break;
1061 }
1062
1063 return 0;
1064}
1065
Pravin B Shelar74f84a52013-06-17 17:50:12 -07001066static int actions_to_attr(const struct nlattr *attr, int len, struct sk_buff *skb)
1067{
1068 const struct nlattr *a;
1069 int rem, err;
1070
1071 nla_for_each_attr(a, attr, len, rem) {
1072 int type = nla_type(a);
1073
1074 switch (type) {
Pravin B Shelar7d5437c2013-06-17 17:50:18 -07001075 case OVS_ACTION_ATTR_SET:
1076 err = set_action_to_attr(a, skb);
1077 if (err)
1078 return err;
1079 break;
1080
Pravin B Shelar74f84a52013-06-17 17:50:12 -07001081 case OVS_ACTION_ATTR_SAMPLE:
1082 err = sample_action_to_attr(a, skb);
1083 if (err)
1084 return err;
1085 break;
1086 default:
1087 if (nla_put(skb, type, nla_len(a), nla_data(a)))
1088 return -EMSGSIZE;
1089 break;
1090 }
1091 }
1092
1093 return 0;
1094}
1095
Thomas Grafc3ff8cf2013-03-29 14:46:49 +01001096static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts)
1097{
1098 return NLMSG_ALIGN(sizeof(struct ovs_header))
1099 + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_KEY */
Andy Zhou03f0d912013-08-07 20:01:00 -07001100 + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_MASK */
Thomas Grafc3ff8cf2013-03-29 14:46:49 +01001101 + nla_total_size(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
1102 + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
1103 + nla_total_size(8) /* OVS_FLOW_ATTR_USED */
1104 + nla_total_size(acts->actions_len); /* OVS_FLOW_ATTR_ACTIONS */
1105}
1106
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001107/* Called with ovs_mutex. */
Jesse Grossccb13522011-10-25 19:26:31 -07001108static int ovs_flow_cmd_fill_info(struct sw_flow *flow, struct datapath *dp,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001109 struct sk_buff *skb, u32 portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001110 u32 seq, u32 flags, u8 cmd)
1111{
1112 const int skb_orig_len = skb->len;
Pravin B Shelar74f84a52013-06-17 17:50:12 -07001113 struct nlattr *start;
Jesse Grossccb13522011-10-25 19:26:31 -07001114 struct ovs_flow_stats stats;
1115 struct ovs_header *ovs_header;
1116 struct nlattr *nla;
1117 unsigned long used;
1118 u8 tcp_flags;
1119 int err;
1120
Eric W. Biederman15e47302012-09-07 20:12:54 +00001121 ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family, flags, cmd);
Jesse Grossccb13522011-10-25 19:26:31 -07001122 if (!ovs_header)
1123 return -EMSGSIZE;
1124
1125 ovs_header->dp_ifindex = get_dpifindex(dp);
1126
Andy Zhou03f0d912013-08-07 20:01:00 -07001127 /* Fill flow key. */
Jesse Grossccb13522011-10-25 19:26:31 -07001128 nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
1129 if (!nla)
1130 goto nla_put_failure;
Andy Zhou03f0d912013-08-07 20:01:00 -07001131
1132 err = ovs_flow_to_nlattrs(&flow->unmasked_key,
1133 &flow->unmasked_key, skb);
Jesse Grossccb13522011-10-25 19:26:31 -07001134 if (err)
1135 goto error;
1136 nla_nest_end(skb, nla);
1137
Andy Zhou03f0d912013-08-07 20:01:00 -07001138 nla = nla_nest_start(skb, OVS_FLOW_ATTR_MASK);
1139 if (!nla)
1140 goto nla_put_failure;
1141
1142 err = ovs_flow_to_nlattrs(&flow->key, &flow->mask->key, skb);
1143 if (err)
1144 goto error;
1145
1146 nla_nest_end(skb, nla);
1147
Jesse Grossccb13522011-10-25 19:26:31 -07001148 spin_lock_bh(&flow->lock);
1149 used = flow->used;
1150 stats.n_packets = flow->packet_count;
1151 stats.n_bytes = flow->byte_count;
1152 tcp_flags = flow->tcp_flags;
1153 spin_unlock_bh(&flow->lock);
1154
David S. Miller028d6a62012-03-29 23:20:48 -04001155 if (used &&
1156 nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
1157 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001158
David S. Miller028d6a62012-03-29 23:20:48 -04001159 if (stats.n_packets &&
1160 nla_put(skb, OVS_FLOW_ATTR_STATS,
1161 sizeof(struct ovs_flow_stats), &stats))
1162 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001163
David S. Miller028d6a62012-03-29 23:20:48 -04001164 if (tcp_flags &&
1165 nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, tcp_flags))
1166 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001167
1168 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
1169 * this is the first flow to be dumped into 'skb'. This is unusual for
1170 * Netlink but individual action lists can be longer than
1171 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
1172 * The userspace caller can always fetch the actions separately if it
1173 * really wants them. (Most userspace callers in fact don't care.)
1174 *
1175 * This can only fail for dump operations because the skb is always
1176 * properly sized for single flows.
1177 */
Pravin B Shelar74f84a52013-06-17 17:50:12 -07001178 start = nla_nest_start(skb, OVS_FLOW_ATTR_ACTIONS);
1179 if (start) {
Pravin B Shelard57170b2013-07-30 15:39:39 -07001180 const struct sw_flow_actions *sf_acts;
1181
1182 sf_acts = rcu_dereference_check(flow->sf_acts,
1183 lockdep_ovsl_is_held());
1184
Pravin B Shelar74f84a52013-06-17 17:50:12 -07001185 err = actions_to_attr(sf_acts->actions, sf_acts->actions_len, skb);
1186 if (!err)
1187 nla_nest_end(skb, start);
1188 else {
1189 if (skb_orig_len)
1190 goto error;
1191
1192 nla_nest_cancel(skb, start);
1193 }
1194 } else if (skb_orig_len)
1195 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001196
1197 return genlmsg_end(skb, ovs_header);
1198
1199nla_put_failure:
1200 err = -EMSGSIZE;
1201error:
1202 genlmsg_cancel(skb, ovs_header);
1203 return err;
1204}
1205
1206static struct sk_buff *ovs_flow_cmd_alloc_info(struct sw_flow *flow)
1207{
1208 const struct sw_flow_actions *sf_acts;
Jesse Grossccb13522011-10-25 19:26:31 -07001209
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001210 sf_acts = ovsl_dereference(flow->sf_acts);
Jesse Grossccb13522011-10-25 19:26:31 -07001211
Thomas Grafc3ff8cf2013-03-29 14:46:49 +01001212 return genlmsg_new(ovs_flow_cmd_msg_size(sf_acts), GFP_KERNEL);
Jesse Grossccb13522011-10-25 19:26:31 -07001213}
1214
1215static struct sk_buff *ovs_flow_cmd_build_info(struct sw_flow *flow,
1216 struct datapath *dp,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001217 u32 portid, u32 seq, u8 cmd)
Jesse Grossccb13522011-10-25 19:26:31 -07001218{
1219 struct sk_buff *skb;
1220 int retval;
1221
1222 skb = ovs_flow_cmd_alloc_info(flow);
1223 if (!skb)
1224 return ERR_PTR(-ENOMEM);
1225
Eric W. Biederman15e47302012-09-07 20:12:54 +00001226 retval = ovs_flow_cmd_fill_info(flow, dp, skb, portid, seq, 0, cmd);
Jesse Grossccb13522011-10-25 19:26:31 -07001227 BUG_ON(retval < 0);
1228 return skb;
1229}
1230
1231static int ovs_flow_cmd_new_or_set(struct sk_buff *skb, struct genl_info *info)
1232{
1233 struct nlattr **a = info->attrs;
1234 struct ovs_header *ovs_header = info->userhdr;
Andy Zhou03f0d912013-08-07 20:01:00 -07001235 struct sw_flow_key key, masked_key;
1236 struct sw_flow *flow = NULL;
1237 struct sw_flow_mask mask;
Jesse Grossccb13522011-10-25 19:26:31 -07001238 struct sk_buff *reply;
1239 struct datapath *dp;
1240 struct flow_table *table;
Pravin B Shelar74f84a52013-06-17 17:50:12 -07001241 struct sw_flow_actions *acts = NULL;
Andy Zhou03f0d912013-08-07 20:01:00 -07001242 struct sw_flow_match match;
Jesse Grossccb13522011-10-25 19:26:31 -07001243 int error;
Jesse Grossccb13522011-10-25 19:26:31 -07001244
1245 /* Extract key. */
1246 error = -EINVAL;
1247 if (!a[OVS_FLOW_ATTR_KEY])
1248 goto error;
Andy Zhou03f0d912013-08-07 20:01:00 -07001249
1250 ovs_match_init(&match, &key, &mask);
1251 error = ovs_match_from_nlattrs(&match,
1252 a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
Jesse Grossccb13522011-10-25 19:26:31 -07001253 if (error)
1254 goto error;
1255
1256 /* Validate actions. */
1257 if (a[OVS_FLOW_ATTR_ACTIONS]) {
Pravin B Shelar74f84a52013-06-17 17:50:12 -07001258 acts = ovs_flow_actions_alloc(nla_len(a[OVS_FLOW_ATTR_ACTIONS]));
1259 error = PTR_ERR(acts);
1260 if (IS_ERR(acts))
Jesse Grossccb13522011-10-25 19:26:31 -07001261 goto error;
Pravin B Shelar74f84a52013-06-17 17:50:12 -07001262
Andy Zhou03f0d912013-08-07 20:01:00 -07001263 ovs_flow_key_mask(&masked_key, &key, &mask);
1264 error = validate_and_copy_actions(a[OVS_FLOW_ATTR_ACTIONS],
1265 &masked_key, 0, &acts);
1266 if (error) {
1267 OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
Pravin B Shelar74f84a52013-06-17 17:50:12 -07001268 goto err_kfree;
Andy Zhou03f0d912013-08-07 20:01:00 -07001269 }
Jesse Grossccb13522011-10-25 19:26:31 -07001270 } else if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW) {
1271 error = -EINVAL;
1272 goto error;
1273 }
1274
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001275 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001276 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001277 error = -ENODEV;
1278 if (!dp)
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001279 goto err_unlock_ovs;
Jesse Grossccb13522011-10-25 19:26:31 -07001280
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001281 table = ovsl_dereference(dp->table);
Andy Zhou03f0d912013-08-07 20:01:00 -07001282
1283 /* Check if this is a duplicate flow */
1284 flow = ovs_flow_lookup(table, &key);
Jesse Grossccb13522011-10-25 19:26:31 -07001285 if (!flow) {
Andy Zhou03f0d912013-08-07 20:01:00 -07001286 struct sw_flow_mask *mask_p;
Jesse Grossccb13522011-10-25 19:26:31 -07001287 /* Bail out if we're not allowed to create a new flow. */
1288 error = -ENOENT;
1289 if (info->genlhdr->cmd == OVS_FLOW_CMD_SET)
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001290 goto err_unlock_ovs;
Jesse Grossccb13522011-10-25 19:26:31 -07001291
1292 /* Expand table, if necessary, to make room. */
1293 if (ovs_flow_tbl_need_to_expand(table)) {
1294 struct flow_table *new_table;
1295
1296 new_table = ovs_flow_tbl_expand(table);
1297 if (!IS_ERR(new_table)) {
1298 rcu_assign_pointer(dp->table, new_table);
Andy Zhou03f0d912013-08-07 20:01:00 -07001299 ovs_flow_tbl_destroy(table, true);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001300 table = ovsl_dereference(dp->table);
Jesse Grossccb13522011-10-25 19:26:31 -07001301 }
1302 }
1303
1304 /* Allocate flow. */
1305 flow = ovs_flow_alloc();
1306 if (IS_ERR(flow)) {
1307 error = PTR_ERR(flow);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001308 goto err_unlock_ovs;
Jesse Grossccb13522011-10-25 19:26:31 -07001309 }
Jesse Grossccb13522011-10-25 19:26:31 -07001310 clear_stats(flow);
1311
Andy Zhou03f0d912013-08-07 20:01:00 -07001312 flow->key = masked_key;
1313 flow->unmasked_key = key;
1314
1315 /* Make sure mask is unique in the system */
1316 mask_p = ovs_sw_flow_mask_find(table, &mask);
1317 if (!mask_p) {
1318 /* Allocate a new mask if none exsits. */
1319 mask_p = ovs_sw_flow_mask_alloc();
1320 if (!mask_p)
1321 goto err_flow_free;
1322 mask_p->key = mask.key;
1323 mask_p->range = mask.range;
1324 ovs_sw_flow_mask_insert(table, mask_p);
1325 }
1326
1327 ovs_sw_flow_mask_add_ref(mask_p);
1328 flow->mask = mask_p;
Jesse Grossccb13522011-10-25 19:26:31 -07001329 rcu_assign_pointer(flow->sf_acts, acts);
1330
1331 /* Put flow in bucket. */
Andy Zhou03f0d912013-08-07 20:01:00 -07001332 ovs_flow_insert(table, flow);
Jesse Grossccb13522011-10-25 19:26:31 -07001333
Eric W. Biederman15e47302012-09-07 20:12:54 +00001334 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
Andy Zhou03f0d912013-08-07 20:01:00 -07001335 info->snd_seq, OVS_FLOW_CMD_NEW);
Jesse Grossccb13522011-10-25 19:26:31 -07001336 } else {
1337 /* We found a matching flow. */
1338 struct sw_flow_actions *old_acts;
Jesse Grossccb13522011-10-25 19:26:31 -07001339
1340 /* Bail out if we're not allowed to modify an existing flow.
1341 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
1342 * because Generic Netlink treats the latter as a dump
1343 * request. We also accept NLM_F_EXCL in case that bug ever
1344 * gets fixed.
1345 */
1346 error = -EEXIST;
1347 if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW &&
1348 info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL))
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001349 goto err_unlock_ovs;
Jesse Grossccb13522011-10-25 19:26:31 -07001350
Andy Zhou03f0d912013-08-07 20:01:00 -07001351 /* The unmasked key has to be the same for flow updates. */
1352 error = -EINVAL;
1353 if (!ovs_flow_cmp_unmasked_key(flow, &key, match.range.end)) {
1354 OVS_NLERR("Flow modification message rejected, unmasked key does not match.\n");
1355 goto err_unlock_ovs;
1356 }
1357
Jesse Grossccb13522011-10-25 19:26:31 -07001358 /* Update actions. */
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001359 old_acts = ovsl_dereference(flow->sf_acts);
Pravin B Shelar74f84a52013-06-17 17:50:12 -07001360 rcu_assign_pointer(flow->sf_acts, acts);
1361 ovs_flow_deferred_free_acts(old_acts);
Jesse Grossccb13522011-10-25 19:26:31 -07001362
Eric W. Biederman15e47302012-09-07 20:12:54 +00001363 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001364 info->snd_seq, OVS_FLOW_CMD_NEW);
1365
1366 /* Clear stats. */
1367 if (a[OVS_FLOW_ATTR_CLEAR]) {
1368 spin_lock_bh(&flow->lock);
1369 clear_stats(flow);
1370 spin_unlock_bh(&flow->lock);
1371 }
1372 }
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001373 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001374
1375 if (!IS_ERR(reply))
Thomas Grafed661182013-03-29 14:46:50 +01001376 ovs_notify(reply, info, &ovs_dp_flow_multicast_group);
Jesse Grossccb13522011-10-25 19:26:31 -07001377 else
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001378 netlink_set_err(sock_net(skb->sk)->genl_sock, 0,
Jesse Grossccb13522011-10-25 19:26:31 -07001379 ovs_dp_flow_multicast_group.id, PTR_ERR(reply));
1380 return 0;
1381
Andy Zhou03f0d912013-08-07 20:01:00 -07001382err_flow_free:
1383 ovs_flow_free(flow, false);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001384err_unlock_ovs:
1385 ovs_unlock();
Pravin B Shelar74f84a52013-06-17 17:50:12 -07001386err_kfree:
1387 kfree(acts);
Jesse Grossccb13522011-10-25 19:26:31 -07001388error:
1389 return error;
1390}
1391
1392static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1393{
1394 struct nlattr **a = info->attrs;
1395 struct ovs_header *ovs_header = info->userhdr;
1396 struct sw_flow_key key;
1397 struct sk_buff *reply;
1398 struct sw_flow *flow;
1399 struct datapath *dp;
1400 struct flow_table *table;
Andy Zhou03f0d912013-08-07 20:01:00 -07001401 struct sw_flow_match match;
Jesse Grossccb13522011-10-25 19:26:31 -07001402 int err;
Jesse Grossccb13522011-10-25 19:26:31 -07001403
Andy Zhou03f0d912013-08-07 20:01:00 -07001404 if (!a[OVS_FLOW_ATTR_KEY]) {
1405 OVS_NLERR("Flow get message rejected, Key attribute missing.\n");
Jesse Grossccb13522011-10-25 19:26:31 -07001406 return -EINVAL;
Andy Zhou03f0d912013-08-07 20:01:00 -07001407 }
1408
1409 ovs_match_init(&match, &key, NULL);
1410 err = ovs_match_from_nlattrs(&match, a[OVS_FLOW_ATTR_KEY], NULL);
Jesse Grossccb13522011-10-25 19:26:31 -07001411 if (err)
1412 return err;
1413
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001414 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001415 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001416 if (!dp) {
1417 err = -ENODEV;
1418 goto unlock;
1419 }
Jesse Grossccb13522011-10-25 19:26:31 -07001420
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001421 table = ovsl_dereference(dp->table);
Andy Zhou03f0d912013-08-07 20:01:00 -07001422 flow = ovs_flow_lookup_unmasked_key(table, &match);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001423 if (!flow) {
1424 err = -ENOENT;
1425 goto unlock;
1426 }
Jesse Grossccb13522011-10-25 19:26:31 -07001427
Eric W. Biederman15e47302012-09-07 20:12:54 +00001428 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001429 info->snd_seq, OVS_FLOW_CMD_NEW);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001430 if (IS_ERR(reply)) {
1431 err = PTR_ERR(reply);
1432 goto unlock;
1433 }
Jesse Grossccb13522011-10-25 19:26:31 -07001434
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001435 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001436 return genlmsg_reply(reply, info);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001437unlock:
1438 ovs_unlock();
1439 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001440}
1441
1442static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1443{
1444 struct nlattr **a = info->attrs;
1445 struct ovs_header *ovs_header = info->userhdr;
1446 struct sw_flow_key key;
1447 struct sk_buff *reply;
1448 struct sw_flow *flow;
1449 struct datapath *dp;
1450 struct flow_table *table;
Andy Zhou03f0d912013-08-07 20:01:00 -07001451 struct sw_flow_match match;
Jesse Grossccb13522011-10-25 19:26:31 -07001452 int err;
Jesse Grossccb13522011-10-25 19:26:31 -07001453
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001454 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001455 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001456 if (!dp) {
1457 err = -ENODEV;
1458 goto unlock;
1459 }
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001460
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001461 if (!a[OVS_FLOW_ATTR_KEY]) {
1462 err = flush_flows(dp);
1463 goto unlock;
1464 }
Andy Zhou03f0d912013-08-07 20:01:00 -07001465
1466 ovs_match_init(&match, &key, NULL);
1467 err = ovs_match_from_nlattrs(&match, a[OVS_FLOW_ATTR_KEY], NULL);
Jesse Grossccb13522011-10-25 19:26:31 -07001468 if (err)
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001469 goto unlock;
Jesse Grossccb13522011-10-25 19:26:31 -07001470
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001471 table = ovsl_dereference(dp->table);
Andy Zhou03f0d912013-08-07 20:01:00 -07001472 flow = ovs_flow_lookup_unmasked_key(table, &match);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001473 if (!flow) {
1474 err = -ENOENT;
1475 goto unlock;
1476 }
Jesse Grossccb13522011-10-25 19:26:31 -07001477
1478 reply = ovs_flow_cmd_alloc_info(flow);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001479 if (!reply) {
1480 err = -ENOMEM;
1481 goto unlock;
1482 }
Jesse Grossccb13522011-10-25 19:26:31 -07001483
Andy Zhou03f0d912013-08-07 20:01:00 -07001484 ovs_flow_remove(table, flow);
Jesse Grossccb13522011-10-25 19:26:31 -07001485
Eric W. Biederman15e47302012-09-07 20:12:54 +00001486 err = ovs_flow_cmd_fill_info(flow, dp, reply, info->snd_portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001487 info->snd_seq, 0, OVS_FLOW_CMD_DEL);
1488 BUG_ON(err < 0);
1489
Andy Zhou03f0d912013-08-07 20:01:00 -07001490 ovs_flow_free(flow, true);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001491 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001492
Thomas Grafed661182013-03-29 14:46:50 +01001493 ovs_notify(reply, info, &ovs_dp_flow_multicast_group);
Jesse Grossccb13522011-10-25 19:26:31 -07001494 return 0;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001495unlock:
1496 ovs_unlock();
1497 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001498}
1499
1500static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1501{
1502 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1503 struct datapath *dp;
1504 struct flow_table *table;
1505
Pravin B Shelard57170b2013-07-30 15:39:39 -07001506 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001507 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001508 if (!dp) {
Pravin B Shelard57170b2013-07-30 15:39:39 -07001509 rcu_read_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001510 return -ENODEV;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001511 }
Jesse Grossccb13522011-10-25 19:26:31 -07001512
Pravin B Shelard57170b2013-07-30 15:39:39 -07001513 table = rcu_dereference(dp->table);
Jesse Grossccb13522011-10-25 19:26:31 -07001514 for (;;) {
1515 struct sw_flow *flow;
1516 u32 bucket, obj;
1517
1518 bucket = cb->args[0];
1519 obj = cb->args[1];
Andy Zhou03f0d912013-08-07 20:01:00 -07001520 flow = ovs_flow_dump_next(table, &bucket, &obj);
Jesse Grossccb13522011-10-25 19:26:31 -07001521 if (!flow)
1522 break;
1523
1524 if (ovs_flow_cmd_fill_info(flow, dp, skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001525 NETLINK_CB(cb->skb).portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001526 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1527 OVS_FLOW_CMD_NEW) < 0)
1528 break;
1529
1530 cb->args[0] = bucket;
1531 cb->args[1] = obj;
1532 }
Pravin B Shelard57170b2013-07-30 15:39:39 -07001533 rcu_read_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001534 return skb->len;
1535}
1536
1537static struct genl_ops dp_flow_genl_ops[] = {
1538 { .cmd = OVS_FLOW_CMD_NEW,
1539 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1540 .policy = flow_policy,
1541 .doit = ovs_flow_cmd_new_or_set
1542 },
1543 { .cmd = OVS_FLOW_CMD_DEL,
1544 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1545 .policy = flow_policy,
1546 .doit = ovs_flow_cmd_del
1547 },
1548 { .cmd = OVS_FLOW_CMD_GET,
1549 .flags = 0, /* OK for unprivileged users. */
1550 .policy = flow_policy,
1551 .doit = ovs_flow_cmd_get,
1552 .dumpit = ovs_flow_cmd_dump
1553 },
1554 { .cmd = OVS_FLOW_CMD_SET,
1555 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1556 .policy = flow_policy,
1557 .doit = ovs_flow_cmd_new_or_set,
1558 },
1559};
1560
1561static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1562 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1563 [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1564};
1565
1566static struct genl_family dp_datapath_genl_family = {
1567 .id = GENL_ID_GENERATE,
1568 .hdrsize = sizeof(struct ovs_header),
1569 .name = OVS_DATAPATH_FAMILY,
1570 .version = OVS_DATAPATH_VERSION,
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001571 .maxattr = OVS_DP_ATTR_MAX,
Pravin B Shelar3a4e0d62013-04-23 07:48:48 +00001572 .netnsok = true,
1573 .parallel_ops = true,
Jesse Grossccb13522011-10-25 19:26:31 -07001574};
1575
1576static struct genl_multicast_group ovs_dp_datapath_multicast_group = {
1577 .name = OVS_DATAPATH_MCGROUP
1578};
1579
Thomas Grafc3ff8cf2013-03-29 14:46:49 +01001580static size_t ovs_dp_cmd_msg_size(void)
1581{
1582 size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
1583
1584 msgsize += nla_total_size(IFNAMSIZ);
1585 msgsize += nla_total_size(sizeof(struct ovs_dp_stats));
1586
1587 return msgsize;
1588}
1589
Jesse Grossccb13522011-10-25 19:26:31 -07001590static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001591 u32 portid, u32 seq, u32 flags, u8 cmd)
Jesse Grossccb13522011-10-25 19:26:31 -07001592{
1593 struct ovs_header *ovs_header;
1594 struct ovs_dp_stats dp_stats;
1595 int err;
1596
Eric W. Biederman15e47302012-09-07 20:12:54 +00001597 ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
Jesse Grossccb13522011-10-25 19:26:31 -07001598 flags, cmd);
1599 if (!ovs_header)
1600 goto error;
1601
1602 ovs_header->dp_ifindex = get_dpifindex(dp);
1603
1604 rcu_read_lock();
1605 err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
1606 rcu_read_unlock();
1607 if (err)
1608 goto nla_put_failure;
1609
1610 get_dp_stats(dp, &dp_stats);
David S. Miller028d6a62012-03-29 23:20:48 -04001611 if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats), &dp_stats))
1612 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001613
1614 return genlmsg_end(skb, ovs_header);
1615
1616nla_put_failure:
1617 genlmsg_cancel(skb, ovs_header);
1618error:
1619 return -EMSGSIZE;
1620}
1621
Eric W. Biederman15e47302012-09-07 20:12:54 +00001622static struct sk_buff *ovs_dp_cmd_build_info(struct datapath *dp, u32 portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001623 u32 seq, u8 cmd)
1624{
1625 struct sk_buff *skb;
1626 int retval;
1627
Thomas Grafc3ff8cf2013-03-29 14:46:49 +01001628 skb = genlmsg_new(ovs_dp_cmd_msg_size(), GFP_KERNEL);
Jesse Grossccb13522011-10-25 19:26:31 -07001629 if (!skb)
1630 return ERR_PTR(-ENOMEM);
1631
Eric W. Biederman15e47302012-09-07 20:12:54 +00001632 retval = ovs_dp_cmd_fill_info(dp, skb, portid, seq, 0, cmd);
Jesse Grossccb13522011-10-25 19:26:31 -07001633 if (retval < 0) {
1634 kfree_skb(skb);
1635 return ERR_PTR(retval);
1636 }
1637 return skb;
1638}
1639
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001640/* Called with ovs_mutex. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001641static struct datapath *lookup_datapath(struct net *net,
1642 struct ovs_header *ovs_header,
Jesse Grossccb13522011-10-25 19:26:31 -07001643 struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1644{
1645 struct datapath *dp;
1646
1647 if (!a[OVS_DP_ATTR_NAME])
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001648 dp = get_dp(net, ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001649 else {
1650 struct vport *vport;
1651
1652 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001653 vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
Jesse Grossccb13522011-10-25 19:26:31 -07001654 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
1655 rcu_read_unlock();
1656 }
1657 return dp ? dp : ERR_PTR(-ENODEV);
1658}
1659
1660static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1661{
1662 struct nlattr **a = info->attrs;
1663 struct vport_parms parms;
1664 struct sk_buff *reply;
1665 struct datapath *dp;
1666 struct vport *vport;
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001667 struct ovs_net *ovs_net;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001668 int err, i;
Jesse Grossccb13522011-10-25 19:26:31 -07001669
1670 err = -EINVAL;
1671 if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1672 goto err;
1673
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001674 ovs_lock();
Jesse Grossccb13522011-10-25 19:26:31 -07001675
1676 err = -ENOMEM;
1677 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1678 if (dp == NULL)
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001679 goto err_unlock_ovs;
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001680
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001681 ovs_dp_set_net(dp, hold_net(sock_net(skb->sk)));
Jesse Grossccb13522011-10-25 19:26:31 -07001682
1683 /* Allocate table. */
1684 err = -ENOMEM;
1685 rcu_assign_pointer(dp->table, ovs_flow_tbl_alloc(TBL_MIN_BUCKETS));
1686 if (!dp->table)
1687 goto err_free_dp;
1688
1689 dp->stats_percpu = alloc_percpu(struct dp_stats_percpu);
1690 if (!dp->stats_percpu) {
1691 err = -ENOMEM;
1692 goto err_destroy_table;
1693 }
1694
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001695 dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
1696 GFP_KERNEL);
1697 if (!dp->ports) {
1698 err = -ENOMEM;
1699 goto err_destroy_percpu;
1700 }
1701
1702 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1703 INIT_HLIST_HEAD(&dp->ports[i]);
1704
Jesse Grossccb13522011-10-25 19:26:31 -07001705 /* Set up our datapath device. */
1706 parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1707 parms.type = OVS_VPORT_TYPE_INTERNAL;
1708 parms.options = NULL;
1709 parms.dp = dp;
1710 parms.port_no = OVSP_LOCAL;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001711 parms.upcall_portid = nla_get_u32(a[OVS_DP_ATTR_UPCALL_PID]);
Jesse Grossccb13522011-10-25 19:26:31 -07001712
1713 vport = new_vport(&parms);
1714 if (IS_ERR(vport)) {
1715 err = PTR_ERR(vport);
1716 if (err == -EBUSY)
1717 err = -EEXIST;
1718
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001719 goto err_destroy_ports_array;
Jesse Grossccb13522011-10-25 19:26:31 -07001720 }
1721
Eric W. Biederman15e47302012-09-07 20:12:54 +00001722 reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001723 info->snd_seq, OVS_DP_CMD_NEW);
1724 err = PTR_ERR(reply);
1725 if (IS_ERR(reply))
1726 goto err_destroy_local_port;
1727
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001728 ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001729 list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001730
1731 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001732
Thomas Grafed661182013-03-29 14:46:50 +01001733 ovs_notify(reply, info, &ovs_dp_datapath_multicast_group);
Jesse Grossccb13522011-10-25 19:26:31 -07001734 return 0;
1735
1736err_destroy_local_port:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001737 ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001738err_destroy_ports_array:
1739 kfree(dp->ports);
Jesse Grossccb13522011-10-25 19:26:31 -07001740err_destroy_percpu:
1741 free_percpu(dp->stats_percpu);
1742err_destroy_table:
Andy Zhou03f0d912013-08-07 20:01:00 -07001743 ovs_flow_tbl_destroy(ovsl_dereference(dp->table), false);
Jesse Grossccb13522011-10-25 19:26:31 -07001744err_free_dp:
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001745 release_net(ovs_dp_get_net(dp));
Jesse Grossccb13522011-10-25 19:26:31 -07001746 kfree(dp);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001747err_unlock_ovs:
1748 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001749err:
1750 return err;
1751}
1752
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001753/* Called with ovs_mutex. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001754static void __dp_destroy(struct datapath *dp)
Jesse Grossccb13522011-10-25 19:26:31 -07001755{
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001756 int i;
Jesse Grossccb13522011-10-25 19:26:31 -07001757
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001758 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1759 struct vport *vport;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001760 struct hlist_node *n;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001761
Sasha Levinb67bfe02013-02-27 17:06:00 -08001762 hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001763 if (vport->port_no != OVSP_LOCAL)
1764 ovs_dp_detach_port(vport);
1765 }
Jesse Grossccb13522011-10-25 19:26:31 -07001766
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001767 list_del_rcu(&dp->list_node);
Jesse Grossccb13522011-10-25 19:26:31 -07001768
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001769 /* OVSP_LOCAL is datapath internal port. We need to make sure that
1770 * all port in datapath are destroyed first before freeing datapath.
Jesse Grossccb13522011-10-25 19:26:31 -07001771 */
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001772 ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
Jesse Grossccb13522011-10-25 19:26:31 -07001773
1774 call_rcu(&dp->rcu, destroy_dp_rcu);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001775}
1776
1777static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1778{
1779 struct sk_buff *reply;
1780 struct datapath *dp;
1781 int err;
1782
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001783 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001784 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1785 err = PTR_ERR(dp);
1786 if (IS_ERR(dp))
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001787 goto unlock;
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001788
Eric W. Biederman15e47302012-09-07 20:12:54 +00001789 reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001790 info->snd_seq, OVS_DP_CMD_DEL);
1791 err = PTR_ERR(reply);
1792 if (IS_ERR(reply))
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001793 goto unlock;
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001794
1795 __dp_destroy(dp);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001796 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001797
Thomas Grafed661182013-03-29 14:46:50 +01001798 ovs_notify(reply, info, &ovs_dp_datapath_multicast_group);
Jesse Grossccb13522011-10-25 19:26:31 -07001799
1800 return 0;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001801unlock:
1802 ovs_unlock();
1803 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001804}
1805
1806static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1807{
1808 struct sk_buff *reply;
1809 struct datapath *dp;
1810 int err;
1811
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001812 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001813 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001814 err = PTR_ERR(dp);
Jesse Grossccb13522011-10-25 19:26:31 -07001815 if (IS_ERR(dp))
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001816 goto unlock;
Jesse Grossccb13522011-10-25 19:26:31 -07001817
Eric W. Biederman15e47302012-09-07 20:12:54 +00001818 reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001819 info->snd_seq, OVS_DP_CMD_NEW);
1820 if (IS_ERR(reply)) {
1821 err = PTR_ERR(reply);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001822 netlink_set_err(sock_net(skb->sk)->genl_sock, 0,
Jesse Grossccb13522011-10-25 19:26:31 -07001823 ovs_dp_datapath_multicast_group.id, err);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001824 err = 0;
1825 goto unlock;
Jesse Grossccb13522011-10-25 19:26:31 -07001826 }
1827
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001828 ovs_unlock();
Thomas Grafed661182013-03-29 14:46:50 +01001829 ovs_notify(reply, info, &ovs_dp_datapath_multicast_group);
Jesse Grossccb13522011-10-25 19:26:31 -07001830
1831 return 0;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001832unlock:
1833 ovs_unlock();
1834 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001835}
1836
1837static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1838{
1839 struct sk_buff *reply;
1840 struct datapath *dp;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001841 int err;
Jesse Grossccb13522011-10-25 19:26:31 -07001842
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001843 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001844 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001845 if (IS_ERR(dp)) {
1846 err = PTR_ERR(dp);
1847 goto unlock;
1848 }
Jesse Grossccb13522011-10-25 19:26:31 -07001849
Eric W. Biederman15e47302012-09-07 20:12:54 +00001850 reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001851 info->snd_seq, OVS_DP_CMD_NEW);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001852 if (IS_ERR(reply)) {
1853 err = PTR_ERR(reply);
1854 goto unlock;
1855 }
Jesse Grossccb13522011-10-25 19:26:31 -07001856
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001857 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001858 return genlmsg_reply(reply, info);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001859
1860unlock:
1861 ovs_unlock();
1862 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001863}
1864
1865static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1866{
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001867 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
Jesse Grossccb13522011-10-25 19:26:31 -07001868 struct datapath *dp;
1869 int skip = cb->args[0];
1870 int i = 0;
1871
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001872 rcu_read_lock();
1873 list_for_each_entry_rcu(dp, &ovs_net->dps, list_node) {
Ben Pfaff77676fd2012-01-17 13:33:39 +00001874 if (i >= skip &&
Eric W. Biederman15e47302012-09-07 20:12:54 +00001875 ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001876 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1877 OVS_DP_CMD_NEW) < 0)
1878 break;
1879 i++;
1880 }
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001881 rcu_read_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001882
1883 cb->args[0] = i;
1884
1885 return skb->len;
1886}
1887
1888static struct genl_ops dp_datapath_genl_ops[] = {
1889 { .cmd = OVS_DP_CMD_NEW,
1890 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1891 .policy = datapath_policy,
1892 .doit = ovs_dp_cmd_new
1893 },
1894 { .cmd = OVS_DP_CMD_DEL,
1895 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1896 .policy = datapath_policy,
1897 .doit = ovs_dp_cmd_del
1898 },
1899 { .cmd = OVS_DP_CMD_GET,
1900 .flags = 0, /* OK for unprivileged users. */
1901 .policy = datapath_policy,
1902 .doit = ovs_dp_cmd_get,
1903 .dumpit = ovs_dp_cmd_dump
1904 },
1905 { .cmd = OVS_DP_CMD_SET,
1906 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1907 .policy = datapath_policy,
1908 .doit = ovs_dp_cmd_set,
1909 },
1910};
1911
1912static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
1913 [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1914 [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
1915 [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
1916 [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
1917 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1918 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
1919};
1920
1921static struct genl_family dp_vport_genl_family = {
1922 .id = GENL_ID_GENERATE,
1923 .hdrsize = sizeof(struct ovs_header),
1924 .name = OVS_VPORT_FAMILY,
1925 .version = OVS_VPORT_VERSION,
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001926 .maxattr = OVS_VPORT_ATTR_MAX,
Pravin B Shelar3a4e0d62013-04-23 07:48:48 +00001927 .netnsok = true,
1928 .parallel_ops = true,
Jesse Grossccb13522011-10-25 19:26:31 -07001929};
1930
1931struct genl_multicast_group ovs_dp_vport_multicast_group = {
1932 .name = OVS_VPORT_MCGROUP
1933};
1934
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001935/* Called with ovs_mutex or RCU read lock. */
Jesse Grossccb13522011-10-25 19:26:31 -07001936static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001937 u32 portid, u32 seq, u32 flags, u8 cmd)
Jesse Grossccb13522011-10-25 19:26:31 -07001938{
1939 struct ovs_header *ovs_header;
1940 struct ovs_vport_stats vport_stats;
1941 int err;
1942
Eric W. Biederman15e47302012-09-07 20:12:54 +00001943 ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
Jesse Grossccb13522011-10-25 19:26:31 -07001944 flags, cmd);
1945 if (!ovs_header)
1946 return -EMSGSIZE;
1947
1948 ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1949
David S. Miller028d6a62012-03-29 23:20:48 -04001950 if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
1951 nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
1952 nla_put_string(skb, OVS_VPORT_ATTR_NAME, vport->ops->get_name(vport)) ||
Eric W. Biederman15e47302012-09-07 20:12:54 +00001953 nla_put_u32(skb, OVS_VPORT_ATTR_UPCALL_PID, vport->upcall_portid))
David S. Miller028d6a62012-03-29 23:20:48 -04001954 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001955
1956 ovs_vport_get_stats(vport, &vport_stats);
David S. Miller028d6a62012-03-29 23:20:48 -04001957 if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
1958 &vport_stats))
1959 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001960
1961 err = ovs_vport_get_options(vport, skb);
1962 if (err == -EMSGSIZE)
1963 goto error;
1964
1965 return genlmsg_end(skb, ovs_header);
1966
1967nla_put_failure:
1968 err = -EMSGSIZE;
1969error:
1970 genlmsg_cancel(skb, ovs_header);
1971 return err;
1972}
1973
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001974/* Called with ovs_mutex or RCU read lock. */
Eric W. Biederman15e47302012-09-07 20:12:54 +00001975struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001976 u32 seq, u8 cmd)
1977{
1978 struct sk_buff *skb;
1979 int retval;
1980
1981 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1982 if (!skb)
1983 return ERR_PTR(-ENOMEM);
1984
Eric W. Biederman15e47302012-09-07 20:12:54 +00001985 retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
Jesse Grossa9341512013-03-26 15:48:38 -07001986 BUG_ON(retval < 0);
1987
Jesse Grossccb13522011-10-25 19:26:31 -07001988 return skb;
1989}
1990
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001991/* Called with ovs_mutex or RCU read lock. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001992static struct vport *lookup_vport(struct net *net,
1993 struct ovs_header *ovs_header,
Jesse Grossccb13522011-10-25 19:26:31 -07001994 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1995{
1996 struct datapath *dp;
1997 struct vport *vport;
1998
1999 if (a[OVS_VPORT_ATTR_NAME]) {
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002000 vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
Jesse Grossccb13522011-10-25 19:26:31 -07002001 if (!vport)
2002 return ERR_PTR(-ENODEV);
Ben Pfaff651a68e2012-03-06 15:04:04 -08002003 if (ovs_header->dp_ifindex &&
2004 ovs_header->dp_ifindex != get_dpifindex(vport->dp))
2005 return ERR_PTR(-ENODEV);
Jesse Grossccb13522011-10-25 19:26:31 -07002006 return vport;
2007 } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
2008 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
2009
2010 if (port_no >= DP_MAX_PORTS)
2011 return ERR_PTR(-EFBIG);
2012
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002013 dp = get_dp(net, ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07002014 if (!dp)
2015 return ERR_PTR(-ENODEV);
2016
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002017 vport = ovs_vport_ovsl_rcu(dp, port_no);
Jesse Grossccb13522011-10-25 19:26:31 -07002018 if (!vport)
Jarno Rajahalme14408db2013-01-09 14:27:35 -08002019 return ERR_PTR(-ENODEV);
Jesse Grossccb13522011-10-25 19:26:31 -07002020 return vport;
2021 } else
2022 return ERR_PTR(-EINVAL);
2023}
2024
2025static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
2026{
2027 struct nlattr **a = info->attrs;
2028 struct ovs_header *ovs_header = info->userhdr;
2029 struct vport_parms parms;
2030 struct sk_buff *reply;
2031 struct vport *vport;
2032 struct datapath *dp;
2033 u32 port_no;
2034 int err;
2035
2036 err = -EINVAL;
2037 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
2038 !a[OVS_VPORT_ATTR_UPCALL_PID])
2039 goto exit;
2040
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002041 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002042 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07002043 err = -ENODEV;
2044 if (!dp)
2045 goto exit_unlock;
2046
2047 if (a[OVS_VPORT_ATTR_PORT_NO]) {
2048 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
2049
2050 err = -EFBIG;
2051 if (port_no >= DP_MAX_PORTS)
2052 goto exit_unlock;
2053
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002054 vport = ovs_vport_ovsl(dp, port_no);
Jesse Grossccb13522011-10-25 19:26:31 -07002055 err = -EBUSY;
2056 if (vport)
2057 goto exit_unlock;
2058 } else {
2059 for (port_no = 1; ; port_no++) {
2060 if (port_no >= DP_MAX_PORTS) {
2061 err = -EFBIG;
2062 goto exit_unlock;
2063 }
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002064 vport = ovs_vport_ovsl(dp, port_no);
Jesse Grossccb13522011-10-25 19:26:31 -07002065 if (!vport)
2066 break;
2067 }
2068 }
2069
2070 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
2071 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
2072 parms.options = a[OVS_VPORT_ATTR_OPTIONS];
2073 parms.dp = dp;
2074 parms.port_no = port_no;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002075 parms.upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
Jesse Grossccb13522011-10-25 19:26:31 -07002076
2077 vport = new_vport(&parms);
2078 err = PTR_ERR(vport);
2079 if (IS_ERR(vport))
2080 goto exit_unlock;
2081
Rich Lanecb7c5bd2013-02-08 13:18:01 -08002082 err = 0;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002083 reply = ovs_vport_cmd_build_info(vport, info->snd_portid, info->snd_seq,
Jesse Grossccb13522011-10-25 19:26:31 -07002084 OVS_VPORT_CMD_NEW);
2085 if (IS_ERR(reply)) {
2086 err = PTR_ERR(reply);
2087 ovs_dp_detach_port(vport);
2088 goto exit_unlock;
2089 }
Thomas Grafed661182013-03-29 14:46:50 +01002090
2091 ovs_notify(reply, info, &ovs_dp_vport_multicast_group);
Jesse Grossccb13522011-10-25 19:26:31 -07002092
2093exit_unlock:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002094 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07002095exit:
2096 return err;
2097}
2098
2099static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
2100{
2101 struct nlattr **a = info->attrs;
2102 struct sk_buff *reply;
2103 struct vport *vport;
2104 int err;
2105
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002106 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002107 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
Jesse Grossccb13522011-10-25 19:26:31 -07002108 err = PTR_ERR(vport);
2109 if (IS_ERR(vport))
2110 goto exit_unlock;
2111
Jesse Grossccb13522011-10-25 19:26:31 -07002112 if (a[OVS_VPORT_ATTR_TYPE] &&
Jesse Grossf44f3402013-05-13 08:15:26 -07002113 nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
Jesse Grossccb13522011-10-25 19:26:31 -07002114 err = -EINVAL;
Jesse Grossf44f3402013-05-13 08:15:26 -07002115 goto exit_unlock;
2116 }
Jesse Grossccb13522011-10-25 19:26:31 -07002117
Jesse Grossa9341512013-03-26 15:48:38 -07002118 reply = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2119 if (!reply) {
2120 err = -ENOMEM;
2121 goto exit_unlock;
2122 }
2123
Jesse Grossf44f3402013-05-13 08:15:26 -07002124 if (a[OVS_VPORT_ATTR_OPTIONS]) {
Jesse Grossccb13522011-10-25 19:26:31 -07002125 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
Jesse Grossf44f3402013-05-13 08:15:26 -07002126 if (err)
2127 goto exit_free;
2128 }
Jesse Grossa9341512013-03-26 15:48:38 -07002129
Ansis Atteka03fbf8b2012-04-09 12:12:12 -07002130 if (a[OVS_VPORT_ATTR_UPCALL_PID])
Eric W. Biederman15e47302012-09-07 20:12:54 +00002131 vport->upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
Jesse Grossccb13522011-10-25 19:26:31 -07002132
Jesse Grossa9341512013-03-26 15:48:38 -07002133 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
2134 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
2135 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07002136
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002137 ovs_unlock();
Thomas Grafed661182013-03-29 14:46:50 +01002138 ovs_notify(reply, info, &ovs_dp_vport_multicast_group);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002139 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -07002140
Jesse Grossa9341512013-03-26 15:48:38 -07002141exit_free:
2142 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07002143exit_unlock:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002144 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07002145 return err;
2146}
2147
2148static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
2149{
2150 struct nlattr **a = info->attrs;
2151 struct sk_buff *reply;
2152 struct vport *vport;
2153 int err;
2154
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002155 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002156 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
Jesse Grossccb13522011-10-25 19:26:31 -07002157 err = PTR_ERR(vport);
2158 if (IS_ERR(vport))
2159 goto exit_unlock;
2160
2161 if (vport->port_no == OVSP_LOCAL) {
2162 err = -EINVAL;
2163 goto exit_unlock;
2164 }
2165
Pravin B Shelar74f84a52013-06-17 17:50:12 -07002166 reply = ovs_vport_cmd_build_info(vport, info->snd_portid,
2167 info->snd_seq, OVS_VPORT_CMD_DEL);
Jesse Grossccb13522011-10-25 19:26:31 -07002168 err = PTR_ERR(reply);
2169 if (IS_ERR(reply))
2170 goto exit_unlock;
2171
Rich Lane734907e2013-02-08 09:30:23 -08002172 err = 0;
Jesse Grossccb13522011-10-25 19:26:31 -07002173 ovs_dp_detach_port(vport);
2174
Thomas Grafed661182013-03-29 14:46:50 +01002175 ovs_notify(reply, info, &ovs_dp_vport_multicast_group);
Jesse Grossccb13522011-10-25 19:26:31 -07002176
2177exit_unlock:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002178 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07002179 return err;
2180}
2181
2182static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
2183{
2184 struct nlattr **a = info->attrs;
2185 struct ovs_header *ovs_header = info->userhdr;
2186 struct sk_buff *reply;
2187 struct vport *vport;
2188 int err;
2189
2190 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002191 vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
Jesse Grossccb13522011-10-25 19:26:31 -07002192 err = PTR_ERR(vport);
2193 if (IS_ERR(vport))
2194 goto exit_unlock;
2195
Pravin B Shelar74f84a52013-06-17 17:50:12 -07002196 reply = ovs_vport_cmd_build_info(vport, info->snd_portid,
2197 info->snd_seq, OVS_VPORT_CMD_NEW);
Jesse Grossccb13522011-10-25 19:26:31 -07002198 err = PTR_ERR(reply);
2199 if (IS_ERR(reply))
2200 goto exit_unlock;
2201
2202 rcu_read_unlock();
2203
2204 return genlmsg_reply(reply, info);
2205
2206exit_unlock:
2207 rcu_read_unlock();
2208 return err;
2209}
2210
2211static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
2212{
2213 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
2214 struct datapath *dp;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07002215 int bucket = cb->args[0], skip = cb->args[1];
2216 int i, j = 0;
Jesse Grossccb13522011-10-25 19:26:31 -07002217
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002218 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07002219 if (!dp)
2220 return -ENODEV;
2221
2222 rcu_read_lock();
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07002223 for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
Jesse Grossccb13522011-10-25 19:26:31 -07002224 struct vport *vport;
2225
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07002226 j = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -08002227 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07002228 if (j >= skip &&
2229 ovs_vport_cmd_fill_info(vport, skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00002230 NETLINK_CB(cb->skb).portid,
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07002231 cb->nlh->nlmsg_seq,
2232 NLM_F_MULTI,
2233 OVS_VPORT_CMD_NEW) < 0)
2234 goto out;
Jesse Grossccb13522011-10-25 19:26:31 -07002235
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07002236 j++;
2237 }
2238 skip = 0;
Jesse Grossccb13522011-10-25 19:26:31 -07002239 }
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07002240out:
Jesse Grossccb13522011-10-25 19:26:31 -07002241 rcu_read_unlock();
2242
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07002243 cb->args[0] = i;
2244 cb->args[1] = j;
Jesse Grossccb13522011-10-25 19:26:31 -07002245
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07002246 return skb->len;
Jesse Grossccb13522011-10-25 19:26:31 -07002247}
2248
Jesse Grossccb13522011-10-25 19:26:31 -07002249static struct genl_ops dp_vport_genl_ops[] = {
2250 { .cmd = OVS_VPORT_CMD_NEW,
2251 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2252 .policy = vport_policy,
2253 .doit = ovs_vport_cmd_new
2254 },
2255 { .cmd = OVS_VPORT_CMD_DEL,
2256 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2257 .policy = vport_policy,
2258 .doit = ovs_vport_cmd_del
2259 },
2260 { .cmd = OVS_VPORT_CMD_GET,
2261 .flags = 0, /* OK for unprivileged users. */
2262 .policy = vport_policy,
2263 .doit = ovs_vport_cmd_get,
2264 .dumpit = ovs_vport_cmd_dump
2265 },
2266 { .cmd = OVS_VPORT_CMD_SET,
2267 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2268 .policy = vport_policy,
2269 .doit = ovs_vport_cmd_set,
2270 },
2271};
2272
2273struct genl_family_and_ops {
2274 struct genl_family *family;
2275 struct genl_ops *ops;
2276 int n_ops;
2277 struct genl_multicast_group *group;
2278};
2279
2280static const struct genl_family_and_ops dp_genl_families[] = {
2281 { &dp_datapath_genl_family,
2282 dp_datapath_genl_ops, ARRAY_SIZE(dp_datapath_genl_ops),
2283 &ovs_dp_datapath_multicast_group },
2284 { &dp_vport_genl_family,
2285 dp_vport_genl_ops, ARRAY_SIZE(dp_vport_genl_ops),
2286 &ovs_dp_vport_multicast_group },
2287 { &dp_flow_genl_family,
2288 dp_flow_genl_ops, ARRAY_SIZE(dp_flow_genl_ops),
2289 &ovs_dp_flow_multicast_group },
2290 { &dp_packet_genl_family,
2291 dp_packet_genl_ops, ARRAY_SIZE(dp_packet_genl_ops),
2292 NULL },
2293};
2294
2295static void dp_unregister_genl(int n_families)
2296{
2297 int i;
2298
2299 for (i = 0; i < n_families; i++)
2300 genl_unregister_family(dp_genl_families[i].family);
2301}
2302
2303static int dp_register_genl(void)
2304{
2305 int n_registered;
2306 int err;
2307 int i;
2308
2309 n_registered = 0;
2310 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
2311 const struct genl_family_and_ops *f = &dp_genl_families[i];
2312
2313 err = genl_register_family_with_ops(f->family, f->ops,
2314 f->n_ops);
2315 if (err)
2316 goto error;
2317 n_registered++;
2318
2319 if (f->group) {
2320 err = genl_register_mc_group(f->family, f->group);
2321 if (err)
2322 goto error;
2323 }
2324 }
2325
2326 return 0;
2327
2328error:
2329 dp_unregister_genl(n_registered);
2330 return err;
2331}
2332
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002333static void rehash_flow_table(struct work_struct *work)
2334{
2335 struct datapath *dp;
2336 struct net *net;
2337
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002338 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002339 rtnl_lock();
2340 for_each_net(net) {
2341 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2342
2343 list_for_each_entry(dp, &ovs_net->dps, list_node) {
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002344 struct flow_table *old_table = ovsl_dereference(dp->table);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002345 struct flow_table *new_table;
2346
2347 new_table = ovs_flow_tbl_rehash(old_table);
2348 if (!IS_ERR(new_table)) {
2349 rcu_assign_pointer(dp->table, new_table);
Andy Zhou03f0d912013-08-07 20:01:00 -07002350 ovs_flow_tbl_destroy(old_table, true);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002351 }
2352 }
2353 }
2354 rtnl_unlock();
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002355 ovs_unlock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002356 schedule_delayed_work(&rehash_flow_wq, REHASH_FLOW_INTERVAL);
2357}
2358
2359static int __net_init ovs_init_net(struct net *net)
2360{
2361 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2362
2363 INIT_LIST_HEAD(&ovs_net->dps);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002364 INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002365 return 0;
2366}
2367
2368static void __net_exit ovs_exit_net(struct net *net)
2369{
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002370 struct datapath *dp, *dp_next;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002371 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002372
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002373 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002374 list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
2375 __dp_destroy(dp);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002376 ovs_unlock();
2377
2378 cancel_work_sync(&ovs_net->dp_notify_work);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002379}
2380
2381static struct pernet_operations ovs_net_ops = {
2382 .init = ovs_init_net,
2383 .exit = ovs_exit_net,
2384 .id = &ovs_net_id,
2385 .size = sizeof(struct ovs_net),
2386};
2387
Jesse Grossccb13522011-10-25 19:26:31 -07002388static int __init dp_init(void)
2389{
Jesse Grossccb13522011-10-25 19:26:31 -07002390 int err;
2391
YOSHIFUJI Hideaki / 吉藤英明3523b292013-01-09 07:19:55 +00002392 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
Jesse Grossccb13522011-10-25 19:26:31 -07002393
2394 pr_info("Open vSwitch switching datapath\n");
2395
2396 err = ovs_flow_init();
2397 if (err)
2398 goto error;
2399
2400 err = ovs_vport_init();
2401 if (err)
2402 goto error_flow_exit;
2403
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002404 err = register_pernet_device(&ovs_net_ops);
Jesse Grossccb13522011-10-25 19:26:31 -07002405 if (err)
2406 goto error_vport_exit;
2407
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002408 err = register_netdevice_notifier(&ovs_dp_device_notifier);
2409 if (err)
2410 goto error_netns_exit;
2411
Jesse Grossccb13522011-10-25 19:26:31 -07002412 err = dp_register_genl();
2413 if (err < 0)
2414 goto error_unreg_notifier;
2415
2416 schedule_delayed_work(&rehash_flow_wq, REHASH_FLOW_INTERVAL);
2417
2418 return 0;
2419
2420error_unreg_notifier:
2421 unregister_netdevice_notifier(&ovs_dp_device_notifier);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002422error_netns_exit:
2423 unregister_pernet_device(&ovs_net_ops);
Jesse Grossccb13522011-10-25 19:26:31 -07002424error_vport_exit:
2425 ovs_vport_exit();
2426error_flow_exit:
2427 ovs_flow_exit();
2428error:
2429 return err;
2430}
2431
2432static void dp_cleanup(void)
2433{
2434 cancel_delayed_work_sync(&rehash_flow_wq);
Jesse Grossccb13522011-10-25 19:26:31 -07002435 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
2436 unregister_netdevice_notifier(&ovs_dp_device_notifier);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002437 unregister_pernet_device(&ovs_net_ops);
2438 rcu_barrier();
Jesse Grossccb13522011-10-25 19:26:31 -07002439 ovs_vport_exit();
2440 ovs_flow_exit();
2441}
2442
2443module_init(dp_init);
2444module_exit(dp_cleanup);
2445
2446MODULE_DESCRIPTION("Open vSwitch switching datapath");
2447MODULE_LICENSE("GPL");