blob: 2e1a9c24e380f12a8507dfc645a39755a1939bc9 [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)
Pravin B Shelar46df7b82012-02-22 19:58:59 -080063
Pravin B Shelar8e4e1712013-04-15 13:23:03 -070064int ovs_net_id __read_mostly;
65
Thomas Grafed6611852013-03-29 14:46:50 +010066static void ovs_notify(struct sk_buff *skb, struct genl_info *info,
67 struct genl_multicast_group *grp)
68{
69 genl_notify(skb, genl_info_net(info), info->snd_portid,
70 grp->id, info->nlhdr, GFP_KERNEL);
71}
72
Pravin B Shelar46df7b82012-02-22 19:58:59 -080073/**
Jesse Grossccb13522011-10-25 19:26:31 -070074 * DOC: Locking:
75 *
Pravin B Shelar8e4e1712013-04-15 13:23:03 -070076 * All writes e.g. Writes to device state (add/remove datapath, port, set
77 * operations on vports, etc.), Writes to other state (flow table
78 * modifications, set miscellaneous datapath parameters, etc.) are protected
79 * by ovs_lock.
Jesse Grossccb13522011-10-25 19:26:31 -070080 *
81 * Reads are protected by RCU.
82 *
83 * There are a few special cases (mostly stats) that have their own
84 * synchronization but they nest under all of above and don't interact with
85 * each other.
Pravin B Shelar8e4e1712013-04-15 13:23:03 -070086 *
87 * The RTNL lock nests inside ovs_mutex.
Jesse Grossccb13522011-10-25 19:26:31 -070088 */
89
Pravin B Shelar8e4e1712013-04-15 13:23:03 -070090static DEFINE_MUTEX(ovs_mutex);
91
92void ovs_lock(void)
93{
94 mutex_lock(&ovs_mutex);
95}
96
97void ovs_unlock(void)
98{
99 mutex_unlock(&ovs_mutex);
100}
101
102#ifdef CONFIG_LOCKDEP
103int lockdep_ovsl_is_held(void)
104{
105 if (debug_locks)
106 return lockdep_is_held(&ovs_mutex);
107 else
108 return 1;
109}
110#endif
111
Jesse Grossccb13522011-10-25 19:26:31 -0700112static struct vport *new_vport(const struct vport_parms *);
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800113static int queue_gso_packets(struct net *, int dp_ifindex, struct sk_buff *,
Jesse Grossccb13522011-10-25 19:26:31 -0700114 const struct dp_upcall_info *);
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800115static int queue_userspace_packet(struct net *, int dp_ifindex,
116 struct sk_buff *,
Jesse Grossccb13522011-10-25 19:26:31 -0700117 const struct dp_upcall_info *);
118
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700119/* Must be called with rcu_read_lock or ovs_mutex. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800120static struct datapath *get_dp(struct net *net, int dp_ifindex)
Jesse Grossccb13522011-10-25 19:26:31 -0700121{
122 struct datapath *dp = NULL;
123 struct net_device *dev;
124
125 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800126 dev = dev_get_by_index_rcu(net, dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -0700127 if (dev) {
128 struct vport *vport = ovs_internal_dev_get_vport(dev);
129 if (vport)
130 dp = vport->dp;
131 }
132 rcu_read_unlock();
133
134 return dp;
135}
136
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700137/* Must be called with rcu_read_lock or ovs_mutex. */
Jesse Grossccb13522011-10-25 19:26:31 -0700138const char *ovs_dp_name(const struct datapath *dp)
139{
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700140 struct vport *vport = ovs_vport_ovsl_rcu(dp, OVSP_LOCAL);
Jesse Grossccb13522011-10-25 19:26:31 -0700141 return vport->ops->get_name(vport);
142}
143
144static int get_dpifindex(struct datapath *dp)
145{
146 struct vport *local;
147 int ifindex;
148
149 rcu_read_lock();
150
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700151 local = ovs_vport_rcu(dp, OVSP_LOCAL);
Jesse Grossccb13522011-10-25 19:26:31 -0700152 if (local)
Thomas Grafcff63a52013-04-29 13:06:41 +0000153 ifindex = netdev_vport_priv(local)->dev->ifindex;
Jesse Grossccb13522011-10-25 19:26:31 -0700154 else
155 ifindex = 0;
156
157 rcu_read_unlock();
158
159 return ifindex;
160}
161
162static void destroy_dp_rcu(struct rcu_head *rcu)
163{
164 struct datapath *dp = container_of(rcu, struct datapath, rcu);
165
Andy Zhou03f0d912013-08-07 20:01:00 -0700166 ovs_flow_tbl_destroy((__force struct flow_table *)dp->table, false);
Jesse Grossccb13522011-10-25 19:26:31 -0700167 free_percpu(dp->stats_percpu);
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800168 release_net(ovs_dp_get_net(dp));
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700169 kfree(dp->ports);
Jesse Grossccb13522011-10-25 19:26:31 -0700170 kfree(dp);
171}
172
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700173static struct hlist_head *vport_hash_bucket(const struct datapath *dp,
174 u16 port_no)
175{
176 return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)];
177}
178
179struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no)
180{
181 struct vport *vport;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700182 struct hlist_head *head;
183
184 head = vport_hash_bucket(dp, port_no);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800185 hlist_for_each_entry_rcu(vport, head, dp_hash_node) {
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700186 if (vport->port_no == port_no)
187 return vport;
188 }
189 return NULL;
190}
191
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700192/* Called with ovs_mutex. */
Jesse Grossccb13522011-10-25 19:26:31 -0700193static struct vport *new_vport(const struct vport_parms *parms)
194{
195 struct vport *vport;
196
197 vport = ovs_vport_add(parms);
198 if (!IS_ERR(vport)) {
199 struct datapath *dp = parms->dp;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700200 struct hlist_head *head = vport_hash_bucket(dp, vport->port_no);
Jesse Grossccb13522011-10-25 19:26:31 -0700201
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700202 hlist_add_head_rcu(&vport->dp_hash_node, head);
Jesse Grossccb13522011-10-25 19:26:31 -0700203 }
Jesse Grossccb13522011-10-25 19:26:31 -0700204 return vport;
205}
206
Jesse Grossccb13522011-10-25 19:26:31 -0700207void ovs_dp_detach_port(struct vport *p)
208{
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700209 ASSERT_OVSL();
Jesse Grossccb13522011-10-25 19:26:31 -0700210
211 /* First drop references to device. */
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700212 hlist_del_rcu(&p->dp_hash_node);
Jesse Grossccb13522011-10-25 19:26:31 -0700213
214 /* Then destroy it. */
215 ovs_vport_del(p);
216}
217
218/* Must be called with rcu_read_lock. */
219void ovs_dp_process_received_packet(struct vport *p, struct sk_buff *skb)
220{
221 struct datapath *dp = p->dp;
222 struct sw_flow *flow;
223 struct dp_stats_percpu *stats;
224 struct sw_flow_key key;
225 u64 *stats_counter;
226 int error;
Jesse Grossccb13522011-10-25 19:26:31 -0700227
Shan Wei404f2f12012-11-13 09:52:25 +0800228 stats = this_cpu_ptr(dp->stats_percpu);
Jesse Grossccb13522011-10-25 19:26:31 -0700229
230 /* Extract flow from 'skb' into 'key'. */
Andy Zhou03f0d912013-08-07 20:01:00 -0700231 error = ovs_flow_extract(skb, p->port_no, &key);
Jesse Grossccb13522011-10-25 19:26:31 -0700232 if (unlikely(error)) {
233 kfree_skb(skb);
234 return;
235 }
236
237 /* Look up flow. */
Andy Zhou03f0d912013-08-07 20:01:00 -0700238 flow = ovs_flow_lookup(rcu_dereference(dp->table), &key);
Jesse Grossccb13522011-10-25 19:26:31 -0700239 if (unlikely(!flow)) {
240 struct dp_upcall_info upcall;
241
242 upcall.cmd = OVS_PACKET_CMD_MISS;
243 upcall.key = &key;
244 upcall.userdata = NULL;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000245 upcall.portid = p->upcall_portid;
Jesse Grossccb13522011-10-25 19:26:31 -0700246 ovs_dp_upcall(dp, skb, &upcall);
247 consume_skb(skb);
248 stats_counter = &stats->n_missed;
249 goto out;
250 }
251
252 OVS_CB(skb)->flow = flow;
Andy Zhou03f0d912013-08-07 20:01:00 -0700253 OVS_CB(skb)->pkt_key = &key;
Jesse Grossccb13522011-10-25 19:26:31 -0700254
255 stats_counter = &stats->n_hit;
256 ovs_flow_used(OVS_CB(skb)->flow, skb);
257 ovs_execute_actions(dp, skb);
258
259out:
260 /* Update datapath statistics. */
261 u64_stats_update_begin(&stats->sync);
262 (*stats_counter)++;
263 u64_stats_update_end(&stats->sync);
264}
265
266static struct genl_family dp_packet_genl_family = {
267 .id = GENL_ID_GENERATE,
268 .hdrsize = sizeof(struct ovs_header),
269 .name = OVS_PACKET_FAMILY,
270 .version = OVS_PACKET_VERSION,
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800271 .maxattr = OVS_PACKET_ATTR_MAX,
Pravin B Shelar3a4e0d62013-04-23 07:48:48 +0000272 .netnsok = true,
273 .parallel_ops = true,
Jesse Grossccb13522011-10-25 19:26:31 -0700274};
275
276int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800277 const struct dp_upcall_info *upcall_info)
Jesse Grossccb13522011-10-25 19:26:31 -0700278{
279 struct dp_stats_percpu *stats;
280 int dp_ifindex;
281 int err;
282
Eric W. Biederman15e47302012-09-07 20:12:54 +0000283 if (upcall_info->portid == 0) {
Jesse Grossccb13522011-10-25 19:26:31 -0700284 err = -ENOTCONN;
285 goto err;
286 }
287
288 dp_ifindex = get_dpifindex(dp);
289 if (!dp_ifindex) {
290 err = -ENODEV;
291 goto err;
292 }
293
294 if (!skb_is_gso(skb))
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800295 err = queue_userspace_packet(ovs_dp_get_net(dp), dp_ifindex, skb, upcall_info);
Jesse Grossccb13522011-10-25 19:26:31 -0700296 else
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800297 err = queue_gso_packets(ovs_dp_get_net(dp), dp_ifindex, skb, upcall_info);
Jesse Grossccb13522011-10-25 19:26:31 -0700298 if (err)
299 goto err;
300
301 return 0;
302
303err:
Shan Wei404f2f12012-11-13 09:52:25 +0800304 stats = this_cpu_ptr(dp->stats_percpu);
Jesse Grossccb13522011-10-25 19:26:31 -0700305
306 u64_stats_update_begin(&stats->sync);
307 stats->n_lost++;
308 u64_stats_update_end(&stats->sync);
309
310 return err;
311}
312
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800313static int queue_gso_packets(struct net *net, int dp_ifindex,
314 struct sk_buff *skb,
Jesse Grossccb13522011-10-25 19:26:31 -0700315 const struct dp_upcall_info *upcall_info)
316{
Ben Pfaffa1b5d0d2012-07-20 14:47:54 -0700317 unsigned short gso_type = skb_shinfo(skb)->gso_type;
Jesse Grossccb13522011-10-25 19:26:31 -0700318 struct dp_upcall_info later_info;
319 struct sw_flow_key later_key;
320 struct sk_buff *segs, *nskb;
321 int err;
322
Cong Wang12b00042013-02-05 16:36:38 +0000323 segs = __skb_gso_segment(skb, NETIF_F_SG | NETIF_F_HW_CSUM, false);
Pravin B Shelar92e5dfc2012-07-20 14:46:29 -0700324 if (IS_ERR(segs))
325 return PTR_ERR(segs);
Jesse Grossccb13522011-10-25 19:26:31 -0700326
327 /* Queue all of the segments. */
328 skb = segs;
329 do {
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800330 err = queue_userspace_packet(net, dp_ifindex, skb, upcall_info);
Jesse Grossccb13522011-10-25 19:26:31 -0700331 if (err)
332 break;
333
Ben Pfaffa1b5d0d2012-07-20 14:47:54 -0700334 if (skb == segs && gso_type & SKB_GSO_UDP) {
Jesse Grossccb13522011-10-25 19:26:31 -0700335 /* The initial flow key extracted by ovs_flow_extract()
336 * in this case is for a first fragment, so we need to
337 * properly mark later fragments.
338 */
339 later_key = *upcall_info->key;
340 later_key.ip.frag = OVS_FRAG_TYPE_LATER;
341
342 later_info = *upcall_info;
343 later_info.key = &later_key;
344 upcall_info = &later_info;
345 }
346 } while ((skb = skb->next));
347
348 /* Free all of the segments. */
349 skb = segs;
350 do {
351 nskb = skb->next;
352 if (err)
353 kfree_skb(skb);
354 else
355 consume_skb(skb);
356 } while ((skb = nskb));
357 return err;
358}
359
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100360static size_t key_attr_size(void)
361{
362 return nla_total_size(4) /* OVS_KEY_ATTR_PRIORITY */
Pravin B Shelar7d5437c2013-06-17 17:50:18 -0700363 + nla_total_size(0) /* OVS_KEY_ATTR_TUNNEL */
364 + nla_total_size(8) /* OVS_TUNNEL_KEY_ATTR_ID */
365 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_SRC */
366 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_DST */
367 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TOS */
368 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TTL */
369 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT */
370 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_CSUM */
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100371 + nla_total_size(4) /* OVS_KEY_ATTR_IN_PORT */
372 + nla_total_size(4) /* OVS_KEY_ATTR_SKB_MARK */
373 + nla_total_size(12) /* OVS_KEY_ATTR_ETHERNET */
374 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
375 + nla_total_size(4) /* OVS_KEY_ATTR_8021Q */
376 + nla_total_size(0) /* OVS_KEY_ATTR_ENCAP */
377 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
378 + nla_total_size(40) /* OVS_KEY_ATTR_IPV6 */
379 + nla_total_size(2) /* OVS_KEY_ATTR_ICMPV6 */
380 + nla_total_size(28); /* OVS_KEY_ATTR_ND */
381}
382
383static size_t upcall_msg_size(const struct sk_buff *skb,
384 const struct nlattr *userdata)
385{
386 size_t size = NLMSG_ALIGN(sizeof(struct ovs_header))
387 + nla_total_size(skb->len) /* OVS_PACKET_ATTR_PACKET */
388 + nla_total_size(key_attr_size()); /* OVS_PACKET_ATTR_KEY */
389
390 /* OVS_PACKET_ATTR_USERDATA */
391 if (userdata)
392 size += NLA_ALIGN(userdata->nla_len);
393
394 return size;
395}
396
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800397static int queue_userspace_packet(struct net *net, int dp_ifindex,
398 struct sk_buff *skb,
Jesse Grossccb13522011-10-25 19:26:31 -0700399 const struct dp_upcall_info *upcall_info)
400{
401 struct ovs_header *upcall;
402 struct sk_buff *nskb = NULL;
403 struct sk_buff *user_skb; /* to be queued to userspace */
404 struct nlattr *nla;
Jesse Grossccb13522011-10-25 19:26:31 -0700405 int err;
406
407 if (vlan_tx_tag_present(skb)) {
408 nskb = skb_clone(skb, GFP_ATOMIC);
409 if (!nskb)
410 return -ENOMEM;
411
Patrick McHardy86a9bad2013-04-19 02:04:30 +0000412 nskb = __vlan_put_tag(nskb, nskb->vlan_proto, vlan_tx_tag_get(nskb));
Dan Carpenter8aa51d62012-05-13 08:44:18 +0000413 if (!nskb)
Jesse Grossccb13522011-10-25 19:26:31 -0700414 return -ENOMEM;
415
416 nskb->vlan_tci = 0;
417 skb = nskb;
418 }
419
420 if (nla_attr_size(skb->len) > USHRT_MAX) {
421 err = -EFBIG;
422 goto out;
423 }
424
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100425 user_skb = genlmsg_new(upcall_msg_size(skb, upcall_info->userdata), GFP_ATOMIC);
Jesse Grossccb13522011-10-25 19:26:31 -0700426 if (!user_skb) {
427 err = -ENOMEM;
428 goto out;
429 }
430
431 upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
432 0, upcall_info->cmd);
433 upcall->dp_ifindex = dp_ifindex;
434
435 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
Andy Zhou03f0d912013-08-07 20:01:00 -0700436 ovs_flow_to_nlattrs(upcall_info->key, upcall_info->key, user_skb);
Jesse Grossccb13522011-10-25 19:26:31 -0700437 nla_nest_end(user_skb, nla);
438
439 if (upcall_info->userdata)
Ben Pfaff44901082013-02-15 17:29:22 -0800440 __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA,
441 nla_len(upcall_info->userdata),
442 nla_data(upcall_info->userdata));
Jesse Grossccb13522011-10-25 19:26:31 -0700443
444 nla = __nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, skb->len);
445
446 skb_copy_and_csum_dev(skb, nla_data(nla));
447
Rich Lanea15ff762013-02-15 11:07:43 -0800448 genlmsg_end(user_skb, upcall);
Eric W. Biederman15e47302012-09-07 20:12:54 +0000449 err = genlmsg_unicast(net, user_skb, upcall_info->portid);
Jesse Grossccb13522011-10-25 19:26:31 -0700450
451out:
452 kfree_skb(nskb);
453 return err;
454}
455
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700456/* Called with ovs_mutex. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800457static int flush_flows(struct datapath *dp)
Jesse Grossccb13522011-10-25 19:26:31 -0700458{
459 struct flow_table *old_table;
460 struct flow_table *new_table;
Jesse Grossccb13522011-10-25 19:26:31 -0700461
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700462 old_table = ovsl_dereference(dp->table);
Jesse Grossccb13522011-10-25 19:26:31 -0700463 new_table = ovs_flow_tbl_alloc(TBL_MIN_BUCKETS);
464 if (!new_table)
465 return -ENOMEM;
466
467 rcu_assign_pointer(dp->table, new_table);
468
Andy Zhou03f0d912013-08-07 20:01:00 -0700469 ovs_flow_tbl_destroy(old_table, true);
Jesse Grossccb13522011-10-25 19:26:31 -0700470 return 0;
471}
472
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700473static struct nlattr *reserve_sfa_size(struct sw_flow_actions **sfa, int attr_len)
474{
Jesse Grossccb13522011-10-25 19:26:31 -0700475
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700476 struct sw_flow_actions *acts;
477 int new_acts_size;
478 int req_size = NLA_ALIGN(attr_len);
479 int next_offset = offsetof(struct sw_flow_actions, actions) +
480 (*sfa)->actions_len;
481
482 if (req_size <= (ksize(*sfa) - next_offset))
483 goto out;
484
485 new_acts_size = ksize(*sfa) * 2;
486
487 if (new_acts_size > MAX_ACTIONS_BUFSIZE) {
488 if ((MAX_ACTIONS_BUFSIZE - next_offset) < req_size)
489 return ERR_PTR(-EMSGSIZE);
490 new_acts_size = MAX_ACTIONS_BUFSIZE;
491 }
492
493 acts = ovs_flow_actions_alloc(new_acts_size);
494 if (IS_ERR(acts))
495 return (void *)acts;
496
497 memcpy(acts->actions, (*sfa)->actions, (*sfa)->actions_len);
498 acts->actions_len = (*sfa)->actions_len;
499 kfree(*sfa);
500 *sfa = acts;
501
502out:
503 (*sfa)->actions_len += req_size;
504 return (struct nlattr *) ((unsigned char *)(*sfa) + next_offset);
505}
506
507static int add_action(struct sw_flow_actions **sfa, int attrtype, void *data, int len)
508{
509 struct nlattr *a;
510
511 a = reserve_sfa_size(sfa, nla_attr_size(len));
512 if (IS_ERR(a))
513 return PTR_ERR(a);
514
515 a->nla_type = attrtype;
516 a->nla_len = nla_attr_size(len);
517
518 if (data)
519 memcpy(nla_data(a), data, len);
520 memset((unsigned char *) a + a->nla_len, 0, nla_padlen(len));
521
522 return 0;
523}
524
525static inline int add_nested_action_start(struct sw_flow_actions **sfa, int attrtype)
526{
527 int used = (*sfa)->actions_len;
528 int err;
529
530 err = add_action(sfa, attrtype, NULL, 0);
531 if (err)
532 return err;
533
534 return used;
535}
536
537static inline void add_nested_action_end(struct sw_flow_actions *sfa, int st_offset)
538{
539 struct nlattr *a = (struct nlattr *) ((unsigned char *)sfa->actions + st_offset);
540
541 a->nla_len = sfa->actions_len - st_offset;
542}
543
544static int validate_and_copy_actions(const struct nlattr *attr,
545 const struct sw_flow_key *key, int depth,
546 struct sw_flow_actions **sfa);
547
548static int validate_and_copy_sample(const struct nlattr *attr,
549 const struct sw_flow_key *key, int depth,
550 struct sw_flow_actions **sfa)
Jesse Grossccb13522011-10-25 19:26:31 -0700551{
552 const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
553 const struct nlattr *probability, *actions;
554 const struct nlattr *a;
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700555 int rem, start, err, st_acts;
Jesse Grossccb13522011-10-25 19:26:31 -0700556
557 memset(attrs, 0, sizeof(attrs));
558 nla_for_each_nested(a, attr, rem) {
559 int type = nla_type(a);
560 if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
561 return -EINVAL;
562 attrs[type] = a;
563 }
564 if (rem)
565 return -EINVAL;
566
567 probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
568 if (!probability || nla_len(probability) != sizeof(u32))
569 return -EINVAL;
570
571 actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
572 if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
573 return -EINVAL;
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700574
575 /* validation done, copy sample action. */
576 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SAMPLE);
577 if (start < 0)
578 return start;
579 err = add_action(sfa, OVS_SAMPLE_ATTR_PROBABILITY, nla_data(probability), sizeof(u32));
580 if (err)
581 return err;
582 st_acts = add_nested_action_start(sfa, OVS_SAMPLE_ATTR_ACTIONS);
583 if (st_acts < 0)
584 return st_acts;
585
586 err = validate_and_copy_actions(actions, key, depth + 1, sfa);
587 if (err)
588 return err;
589
590 add_nested_action_end(*sfa, st_acts);
591 add_nested_action_end(*sfa, start);
592
593 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -0700594}
595
Pravin B Shelar072ae632012-05-07 17:21:53 -0700596static int validate_tp_port(const struct sw_flow_key *flow_key)
597{
598 if (flow_key->eth.type == htons(ETH_P_IP)) {
Jesse Gross41853922012-08-06 15:49:47 -0700599 if (flow_key->ipv4.tp.src || flow_key->ipv4.tp.dst)
Pravin B Shelar072ae632012-05-07 17:21:53 -0700600 return 0;
601 } else if (flow_key->eth.type == htons(ETH_P_IPV6)) {
Jesse Gross41853922012-08-06 15:49:47 -0700602 if (flow_key->ipv6.tp.src || flow_key->ipv6.tp.dst)
Pravin B Shelar072ae632012-05-07 17:21:53 -0700603 return 0;
604 }
605
606 return -EINVAL;
607}
608
Pravin B Shelar7d5437c2013-06-17 17:50:18 -0700609static int validate_and_copy_set_tun(const struct nlattr *attr,
610 struct sw_flow_actions **sfa)
611{
Andy Zhou03f0d912013-08-07 20:01:00 -0700612 struct sw_flow_match match;
613 struct sw_flow_key key;
Pravin B Shelar7d5437c2013-06-17 17:50:18 -0700614 int err, start;
615
Andy Zhou03f0d912013-08-07 20:01:00 -0700616 ovs_match_init(&match, &key, NULL);
617 err = ovs_ipv4_tun_from_nlattr(nla_data(attr), &match, false);
Pravin B Shelar7d5437c2013-06-17 17:50:18 -0700618 if (err)
619 return err;
620
621 start = add_nested_action_start(sfa, OVS_ACTION_ATTR_SET);
622 if (start < 0)
623 return start;
624
Andy Zhou03f0d912013-08-07 20:01:00 -0700625 err = add_action(sfa, OVS_KEY_ATTR_IPV4_TUNNEL, &match.key->tun_key,
626 sizeof(match.key->tun_key));
Pravin B Shelar7d5437c2013-06-17 17:50:18 -0700627 add_nested_action_end(*sfa, start);
628
629 return err;
630}
631
Jesse Grossccb13522011-10-25 19:26:31 -0700632static int validate_set(const struct nlattr *a,
Pravin B Shelar7d5437c2013-06-17 17:50:18 -0700633 const struct sw_flow_key *flow_key,
634 struct sw_flow_actions **sfa,
635 bool *set_tun)
Jesse Grossccb13522011-10-25 19:26:31 -0700636{
637 const struct nlattr *ovs_key = nla_data(a);
638 int key_type = nla_type(ovs_key);
639
640 /* There can be only one key in a action */
641 if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
642 return -EINVAL;
643
644 if (key_type > OVS_KEY_ATTR_MAX ||
Pravin B Shelar7d5437c2013-06-17 17:50:18 -0700645 (ovs_key_lens[key_type] != nla_len(ovs_key) &&
646 ovs_key_lens[key_type] != -1))
Jesse Grossccb13522011-10-25 19:26:31 -0700647 return -EINVAL;
648
649 switch (key_type) {
650 const struct ovs_key_ipv4 *ipv4_key;
Ansis Atteka3fdbd1c2012-11-13 15:44:14 -0800651 const struct ovs_key_ipv6 *ipv6_key;
Pravin B Shelar7d5437c2013-06-17 17:50:18 -0700652 int err;
Jesse Grossccb13522011-10-25 19:26:31 -0700653
654 case OVS_KEY_ATTR_PRIORITY:
Ansis Atteka39c7caeb2012-11-26 11:24:11 -0800655 case OVS_KEY_ATTR_SKB_MARK:
Jesse Grossccb13522011-10-25 19:26:31 -0700656 case OVS_KEY_ATTR_ETHERNET:
657 break;
658
Pravin B Shelar7d5437c2013-06-17 17:50:18 -0700659 case OVS_KEY_ATTR_TUNNEL:
660 *set_tun = true;
661 err = validate_and_copy_set_tun(a, sfa);
662 if (err)
663 return err;
664 break;
665
Jesse Grossccb13522011-10-25 19:26:31 -0700666 case OVS_KEY_ATTR_IPV4:
667 if (flow_key->eth.type != htons(ETH_P_IP))
668 return -EINVAL;
669
Jesse Gross41853922012-08-06 15:49:47 -0700670 if (!flow_key->ip.proto)
Jesse Grossccb13522011-10-25 19:26:31 -0700671 return -EINVAL;
672
673 ipv4_key = nla_data(ovs_key);
674 if (ipv4_key->ipv4_proto != flow_key->ip.proto)
675 return -EINVAL;
676
677 if (ipv4_key->ipv4_frag != flow_key->ip.frag)
678 return -EINVAL;
679
680 break;
681
Ansis Atteka3fdbd1c2012-11-13 15:44:14 -0800682 case OVS_KEY_ATTR_IPV6:
683 if (flow_key->eth.type != htons(ETH_P_IPV6))
684 return -EINVAL;
685
686 if (!flow_key->ip.proto)
687 return -EINVAL;
688
689 ipv6_key = nla_data(ovs_key);
690 if (ipv6_key->ipv6_proto != flow_key->ip.proto)
691 return -EINVAL;
692
693 if (ipv6_key->ipv6_frag != flow_key->ip.frag)
694 return -EINVAL;
695
696 if (ntohl(ipv6_key->ipv6_label) & 0xFFF00000)
697 return -EINVAL;
698
699 break;
700
Jesse Grossccb13522011-10-25 19:26:31 -0700701 case OVS_KEY_ATTR_TCP:
702 if (flow_key->ip.proto != IPPROTO_TCP)
703 return -EINVAL;
704
Pravin B Shelar072ae632012-05-07 17:21:53 -0700705 return validate_tp_port(flow_key);
Jesse Grossccb13522011-10-25 19:26:31 -0700706
707 case OVS_KEY_ATTR_UDP:
708 if (flow_key->ip.proto != IPPROTO_UDP)
709 return -EINVAL;
710
Pravin B Shelar072ae632012-05-07 17:21:53 -0700711 return validate_tp_port(flow_key);
Jesse Grossccb13522011-10-25 19:26:31 -0700712
Joe Stringera175a722013-08-22 12:30:48 -0700713 case OVS_KEY_ATTR_SCTP:
714 if (flow_key->ip.proto != IPPROTO_SCTP)
715 return -EINVAL;
716
717 return validate_tp_port(flow_key);
718
Jesse Grossccb13522011-10-25 19:26:31 -0700719 default:
720 return -EINVAL;
721 }
722
723 return 0;
724}
725
726static int validate_userspace(const struct nlattr *attr)
727{
728 static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
729 [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
Ben Pfaff44901082013-02-15 17:29:22 -0800730 [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_UNSPEC },
Jesse Grossccb13522011-10-25 19:26:31 -0700731 };
732 struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
733 int error;
734
735 error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX,
736 attr, userspace_policy);
737 if (error)
738 return error;
739
740 if (!a[OVS_USERSPACE_ATTR_PID] ||
741 !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
742 return -EINVAL;
743
744 return 0;
745}
746
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700747static int copy_action(const struct nlattr *from,
748 struct sw_flow_actions **sfa)
749{
750 int totlen = NLA_ALIGN(from->nla_len);
751 struct nlattr *to;
752
753 to = reserve_sfa_size(sfa, from->nla_len);
754 if (IS_ERR(to))
755 return PTR_ERR(to);
756
757 memcpy(to, from, totlen);
758 return 0;
759}
760
761static int validate_and_copy_actions(const struct nlattr *attr,
762 const struct sw_flow_key *key,
763 int depth,
764 struct sw_flow_actions **sfa)
Jesse Grossccb13522011-10-25 19:26:31 -0700765{
766 const struct nlattr *a;
767 int rem, err;
768
769 if (depth >= SAMPLE_ACTION_DEPTH)
770 return -EOVERFLOW;
771
772 nla_for_each_nested(a, attr, rem) {
773 /* Expected argument lengths, (u32)-1 for variable length. */
774 static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
775 [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
776 [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
777 [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
778 [OVS_ACTION_ATTR_POP_VLAN] = 0,
779 [OVS_ACTION_ATTR_SET] = (u32)-1,
780 [OVS_ACTION_ATTR_SAMPLE] = (u32)-1
781 };
782 const struct ovs_action_push_vlan *vlan;
783 int type = nla_type(a);
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700784 bool skip_copy;
Jesse Grossccb13522011-10-25 19:26:31 -0700785
786 if (type > OVS_ACTION_ATTR_MAX ||
787 (action_lens[type] != nla_len(a) &&
788 action_lens[type] != (u32)-1))
789 return -EINVAL;
790
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700791 skip_copy = false;
Jesse Grossccb13522011-10-25 19:26:31 -0700792 switch (type) {
793 case OVS_ACTION_ATTR_UNSPEC:
794 return -EINVAL;
795
796 case OVS_ACTION_ATTR_USERSPACE:
797 err = validate_userspace(a);
798 if (err)
799 return err;
800 break;
801
802 case OVS_ACTION_ATTR_OUTPUT:
803 if (nla_get_u32(a) >= DP_MAX_PORTS)
804 return -EINVAL;
805 break;
806
807
808 case OVS_ACTION_ATTR_POP_VLAN:
809 break;
810
811 case OVS_ACTION_ATTR_PUSH_VLAN:
812 vlan = nla_data(a);
813 if (vlan->vlan_tpid != htons(ETH_P_8021Q))
814 return -EINVAL;
815 if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
816 return -EINVAL;
817 break;
818
819 case OVS_ACTION_ATTR_SET:
Pravin B Shelar7d5437c2013-06-17 17:50:18 -0700820 err = validate_set(a, key, sfa, &skip_copy);
Jesse Grossccb13522011-10-25 19:26:31 -0700821 if (err)
822 return err;
823 break;
824
825 case OVS_ACTION_ATTR_SAMPLE:
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700826 err = validate_and_copy_sample(a, key, depth, sfa);
Jesse Grossccb13522011-10-25 19:26:31 -0700827 if (err)
828 return err;
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700829 skip_copy = true;
Jesse Grossccb13522011-10-25 19:26:31 -0700830 break;
831
832 default:
833 return -EINVAL;
834 }
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700835 if (!skip_copy) {
836 err = copy_action(a, sfa);
837 if (err)
838 return err;
839 }
Jesse Grossccb13522011-10-25 19:26:31 -0700840 }
841
842 if (rem > 0)
843 return -EINVAL;
844
845 return 0;
846}
847
848static void clear_stats(struct sw_flow *flow)
849{
850 flow->used = 0;
851 flow->tcp_flags = 0;
852 flow->packet_count = 0;
853 flow->byte_count = 0;
854}
855
856static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
857{
858 struct ovs_header *ovs_header = info->userhdr;
859 struct nlattr **a = info->attrs;
860 struct sw_flow_actions *acts;
861 struct sk_buff *packet;
862 struct sw_flow *flow;
863 struct datapath *dp;
864 struct ethhdr *eth;
865 int len;
866 int err;
Jesse Grossccb13522011-10-25 19:26:31 -0700867
868 err = -EINVAL;
869 if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
Thomas Grafdded45fc2013-03-29 14:46:47 +0100870 !a[OVS_PACKET_ATTR_ACTIONS])
Jesse Grossccb13522011-10-25 19:26:31 -0700871 goto err;
872
873 len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
874 packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
875 err = -ENOMEM;
876 if (!packet)
877 goto err;
878 skb_reserve(packet, NET_IP_ALIGN);
879
Thomas Graf32686a92013-03-29 14:46:48 +0100880 nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
Jesse Grossccb13522011-10-25 19:26:31 -0700881
882 skb_reset_mac_header(packet);
883 eth = eth_hdr(packet);
884
885 /* Normally, setting the skb 'protocol' field would be handled by a
886 * call to eth_type_trans(), but it assumes there's a sending
887 * device, which we may not have. */
Simon Hormane5c5d222013-03-28 13:38:25 +0900888 if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN)
Jesse Grossccb13522011-10-25 19:26:31 -0700889 packet->protocol = eth->h_proto;
890 else
891 packet->protocol = htons(ETH_P_802_2);
892
893 /* Build an sw_flow for sending this packet. */
894 flow = ovs_flow_alloc();
895 err = PTR_ERR(flow);
896 if (IS_ERR(flow))
897 goto err_kfree_skb;
898
Andy Zhou03f0d912013-08-07 20:01:00 -0700899 err = ovs_flow_extract(packet, -1, &flow->key);
Jesse Grossccb13522011-10-25 19:26:31 -0700900 if (err)
901 goto err_flow_free;
902
Andy Zhou03f0d912013-08-07 20:01:00 -0700903 err = ovs_flow_metadata_from_nlattrs(flow, a[OVS_PACKET_ATTR_KEY]);
Jesse Grossccb13522011-10-25 19:26:31 -0700904 if (err)
905 goto err_flow_free;
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700906 acts = ovs_flow_actions_alloc(nla_len(a[OVS_PACKET_ATTR_ACTIONS]));
Jesse Grossccb13522011-10-25 19:26:31 -0700907 err = PTR_ERR(acts);
908 if (IS_ERR(acts))
909 goto err_flow_free;
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700910
911 err = validate_and_copy_actions(a[OVS_PACKET_ATTR_ACTIONS], &flow->key, 0, &acts);
Jesse Grossccb13522011-10-25 19:26:31 -0700912 rcu_assign_pointer(flow->sf_acts, acts);
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700913 if (err)
914 goto err_flow_free;
Jesse Grossccb13522011-10-25 19:26:31 -0700915
916 OVS_CB(packet)->flow = flow;
Andy Zhou03f0d912013-08-07 20:01:00 -0700917 OVS_CB(packet)->pkt_key = &flow->key;
Jesse Grossccb13522011-10-25 19:26:31 -0700918 packet->priority = flow->key.phy.priority;
Ansis Atteka39c7caeb2012-11-26 11:24:11 -0800919 packet->mark = flow->key.phy.skb_mark;
Jesse Grossccb13522011-10-25 19:26:31 -0700920
921 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800922 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -0700923 err = -ENODEV;
924 if (!dp)
925 goto err_unlock;
926
927 local_bh_disable();
928 err = ovs_execute_actions(dp, packet);
929 local_bh_enable();
930 rcu_read_unlock();
931
Andy Zhou03f0d912013-08-07 20:01:00 -0700932 ovs_flow_free(flow, false);
Jesse Grossccb13522011-10-25 19:26:31 -0700933 return err;
934
935err_unlock:
936 rcu_read_unlock();
937err_flow_free:
Andy Zhou03f0d912013-08-07 20:01:00 -0700938 ovs_flow_free(flow, false);
Jesse Grossccb13522011-10-25 19:26:31 -0700939err_kfree_skb:
940 kfree_skb(packet);
941err:
942 return err;
943}
944
945static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
Thomas Grafdded45fc2013-03-29 14:46:47 +0100946 [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
Jesse Grossccb13522011-10-25 19:26:31 -0700947 [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
948 [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
949};
950
951static struct genl_ops dp_packet_genl_ops[] = {
952 { .cmd = OVS_PACKET_CMD_EXECUTE,
953 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
954 .policy = packet_policy,
955 .doit = ovs_packet_cmd_execute
956 }
957};
958
959static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats)
960{
Pravin B Shelar59a35d62013-07-30 15:42:19 -0700961 struct flow_table *table;
Jesse Grossccb13522011-10-25 19:26:31 -0700962 int i;
Jesse Grossccb13522011-10-25 19:26:31 -0700963
Pravin B Shelar59a35d62013-07-30 15:42:19 -0700964 table = rcu_dereference_check(dp->table, lockdep_ovsl_is_held());
Jesse Grossccb13522011-10-25 19:26:31 -0700965 stats->n_flows = ovs_flow_tbl_count(table);
966
967 stats->n_hit = stats->n_missed = stats->n_lost = 0;
968 for_each_possible_cpu(i) {
969 const struct dp_stats_percpu *percpu_stats;
970 struct dp_stats_percpu local_stats;
971 unsigned int start;
972
973 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
974
975 do {
976 start = u64_stats_fetch_begin_bh(&percpu_stats->sync);
977 local_stats = *percpu_stats;
978 } while (u64_stats_fetch_retry_bh(&percpu_stats->sync, start));
979
980 stats->n_hit += local_stats.n_hit;
981 stats->n_missed += local_stats.n_missed;
982 stats->n_lost += local_stats.n_lost;
983 }
984}
985
986static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
987 [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
988 [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
989 [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
990};
991
992static struct genl_family dp_flow_genl_family = {
993 .id = GENL_ID_GENERATE,
994 .hdrsize = sizeof(struct ovs_header),
995 .name = OVS_FLOW_FAMILY,
996 .version = OVS_FLOW_VERSION,
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800997 .maxattr = OVS_FLOW_ATTR_MAX,
Pravin B Shelar3a4e0d62013-04-23 07:48:48 +0000998 .netnsok = true,
999 .parallel_ops = true,
Jesse Grossccb13522011-10-25 19:26:31 -07001000};
1001
1002static struct genl_multicast_group ovs_dp_flow_multicast_group = {
1003 .name = OVS_FLOW_MCGROUP
1004};
1005
Pravin B Shelar74f84a52013-06-17 17:50:12 -07001006static int actions_to_attr(const struct nlattr *attr, int len, struct sk_buff *skb);
1007static int sample_action_to_attr(const struct nlattr *attr, struct sk_buff *skb)
1008{
1009 const struct nlattr *a;
1010 struct nlattr *start;
1011 int err = 0, rem;
1012
1013 start = nla_nest_start(skb, OVS_ACTION_ATTR_SAMPLE);
1014 if (!start)
1015 return -EMSGSIZE;
1016
1017 nla_for_each_nested(a, attr, rem) {
1018 int type = nla_type(a);
1019 struct nlattr *st_sample;
1020
1021 switch (type) {
1022 case OVS_SAMPLE_ATTR_PROBABILITY:
1023 if (nla_put(skb, OVS_SAMPLE_ATTR_PROBABILITY, sizeof(u32), nla_data(a)))
1024 return -EMSGSIZE;
1025 break;
1026 case OVS_SAMPLE_ATTR_ACTIONS:
1027 st_sample = nla_nest_start(skb, OVS_SAMPLE_ATTR_ACTIONS);
1028 if (!st_sample)
1029 return -EMSGSIZE;
1030 err = actions_to_attr(nla_data(a), nla_len(a), skb);
1031 if (err)
1032 return err;
1033 nla_nest_end(skb, st_sample);
1034 break;
1035 }
1036 }
1037
1038 nla_nest_end(skb, start);
1039 return err;
1040}
1041
Pravin B Shelar7d5437c2013-06-17 17:50:18 -07001042static int set_action_to_attr(const struct nlattr *a, struct sk_buff *skb)
1043{
1044 const struct nlattr *ovs_key = nla_data(a);
1045 int key_type = nla_type(ovs_key);
1046 struct nlattr *start;
1047 int err;
1048
1049 switch (key_type) {
1050 case OVS_KEY_ATTR_IPV4_TUNNEL:
1051 start = nla_nest_start(skb, OVS_ACTION_ATTR_SET);
1052 if (!start)
1053 return -EMSGSIZE;
1054
Andy Zhou03f0d912013-08-07 20:01:00 -07001055 err = ovs_ipv4_tun_to_nlattr(skb, nla_data(ovs_key),
1056 nla_data(ovs_key));
Pravin B Shelar7d5437c2013-06-17 17:50:18 -07001057 if (err)
1058 return err;
1059 nla_nest_end(skb, start);
1060 break;
1061 default:
1062 if (nla_put(skb, OVS_ACTION_ATTR_SET, nla_len(a), ovs_key))
1063 return -EMSGSIZE;
1064 break;
1065 }
1066
1067 return 0;
1068}
1069
Pravin B Shelar74f84a52013-06-17 17:50:12 -07001070static int actions_to_attr(const struct nlattr *attr, int len, struct sk_buff *skb)
1071{
1072 const struct nlattr *a;
1073 int rem, err;
1074
1075 nla_for_each_attr(a, attr, len, rem) {
1076 int type = nla_type(a);
1077
1078 switch (type) {
Pravin B Shelar7d5437c2013-06-17 17:50:18 -07001079 case OVS_ACTION_ATTR_SET:
1080 err = set_action_to_attr(a, skb);
1081 if (err)
1082 return err;
1083 break;
1084
Pravin B Shelar74f84a52013-06-17 17:50:12 -07001085 case OVS_ACTION_ATTR_SAMPLE:
1086 err = sample_action_to_attr(a, skb);
1087 if (err)
1088 return err;
1089 break;
1090 default:
1091 if (nla_put(skb, type, nla_len(a), nla_data(a)))
1092 return -EMSGSIZE;
1093 break;
1094 }
1095 }
1096
1097 return 0;
1098}
1099
Thomas Grafc3ff8cf2013-03-29 14:46:49 +01001100static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts)
1101{
1102 return NLMSG_ALIGN(sizeof(struct ovs_header))
1103 + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_KEY */
Andy Zhou03f0d912013-08-07 20:01:00 -07001104 + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_MASK */
Thomas Grafc3ff8cf2013-03-29 14:46:49 +01001105 + nla_total_size(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
1106 + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
1107 + nla_total_size(8) /* OVS_FLOW_ATTR_USED */
1108 + nla_total_size(acts->actions_len); /* OVS_FLOW_ATTR_ACTIONS */
1109}
1110
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001111/* Called with ovs_mutex. */
Jesse Grossccb13522011-10-25 19:26:31 -07001112static int ovs_flow_cmd_fill_info(struct sw_flow *flow, struct datapath *dp,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001113 struct sk_buff *skb, u32 portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001114 u32 seq, u32 flags, u8 cmd)
1115{
1116 const int skb_orig_len = skb->len;
Pravin B Shelar74f84a52013-06-17 17:50:12 -07001117 struct nlattr *start;
Jesse Grossccb13522011-10-25 19:26:31 -07001118 struct ovs_flow_stats stats;
1119 struct ovs_header *ovs_header;
1120 struct nlattr *nla;
1121 unsigned long used;
1122 u8 tcp_flags;
1123 int err;
1124
Eric W. Biederman15e47302012-09-07 20:12:54 +00001125 ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family, flags, cmd);
Jesse Grossccb13522011-10-25 19:26:31 -07001126 if (!ovs_header)
1127 return -EMSGSIZE;
1128
1129 ovs_header->dp_ifindex = get_dpifindex(dp);
1130
Andy Zhou03f0d912013-08-07 20:01:00 -07001131 /* Fill flow key. */
Jesse Grossccb13522011-10-25 19:26:31 -07001132 nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
1133 if (!nla)
1134 goto nla_put_failure;
Andy Zhou03f0d912013-08-07 20:01:00 -07001135
1136 err = ovs_flow_to_nlattrs(&flow->unmasked_key,
1137 &flow->unmasked_key, skb);
Jesse Grossccb13522011-10-25 19:26:31 -07001138 if (err)
1139 goto error;
1140 nla_nest_end(skb, nla);
1141
Andy Zhou03f0d912013-08-07 20:01:00 -07001142 nla = nla_nest_start(skb, OVS_FLOW_ATTR_MASK);
1143 if (!nla)
1144 goto nla_put_failure;
1145
1146 err = ovs_flow_to_nlattrs(&flow->key, &flow->mask->key, skb);
1147 if (err)
1148 goto error;
1149
1150 nla_nest_end(skb, nla);
1151
Jesse Grossccb13522011-10-25 19:26:31 -07001152 spin_lock_bh(&flow->lock);
1153 used = flow->used;
1154 stats.n_packets = flow->packet_count;
1155 stats.n_bytes = flow->byte_count;
1156 tcp_flags = flow->tcp_flags;
1157 spin_unlock_bh(&flow->lock);
1158
David S. Miller028d6a62012-03-29 23:20:48 -04001159 if (used &&
1160 nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
1161 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001162
David S. Miller028d6a62012-03-29 23:20:48 -04001163 if (stats.n_packets &&
1164 nla_put(skb, OVS_FLOW_ATTR_STATS,
1165 sizeof(struct ovs_flow_stats), &stats))
1166 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001167
David S. Miller028d6a62012-03-29 23:20:48 -04001168 if (tcp_flags &&
1169 nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, tcp_flags))
1170 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001171
1172 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
1173 * this is the first flow to be dumped into 'skb'. This is unusual for
1174 * Netlink but individual action lists can be longer than
1175 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
1176 * The userspace caller can always fetch the actions separately if it
1177 * really wants them. (Most userspace callers in fact don't care.)
1178 *
1179 * This can only fail for dump operations because the skb is always
1180 * properly sized for single flows.
1181 */
Pravin B Shelar74f84a52013-06-17 17:50:12 -07001182 start = nla_nest_start(skb, OVS_FLOW_ATTR_ACTIONS);
1183 if (start) {
Pravin B Shelard57170b2013-07-30 15:39:39 -07001184 const struct sw_flow_actions *sf_acts;
1185
1186 sf_acts = rcu_dereference_check(flow->sf_acts,
1187 lockdep_ovsl_is_held());
1188
Pravin B Shelar74f84a52013-06-17 17:50:12 -07001189 err = actions_to_attr(sf_acts->actions, sf_acts->actions_len, skb);
1190 if (!err)
1191 nla_nest_end(skb, start);
1192 else {
1193 if (skb_orig_len)
1194 goto error;
1195
1196 nla_nest_cancel(skb, start);
1197 }
1198 } else if (skb_orig_len)
1199 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001200
1201 return genlmsg_end(skb, ovs_header);
1202
1203nla_put_failure:
1204 err = -EMSGSIZE;
1205error:
1206 genlmsg_cancel(skb, ovs_header);
1207 return err;
1208}
1209
1210static struct sk_buff *ovs_flow_cmd_alloc_info(struct sw_flow *flow)
1211{
1212 const struct sw_flow_actions *sf_acts;
Jesse Grossccb13522011-10-25 19:26:31 -07001213
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001214 sf_acts = ovsl_dereference(flow->sf_acts);
Jesse Grossccb13522011-10-25 19:26:31 -07001215
Thomas Grafc3ff8cf2013-03-29 14:46:49 +01001216 return genlmsg_new(ovs_flow_cmd_msg_size(sf_acts), GFP_KERNEL);
Jesse Grossccb13522011-10-25 19:26:31 -07001217}
1218
1219static struct sk_buff *ovs_flow_cmd_build_info(struct sw_flow *flow,
1220 struct datapath *dp,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001221 u32 portid, u32 seq, u8 cmd)
Jesse Grossccb13522011-10-25 19:26:31 -07001222{
1223 struct sk_buff *skb;
1224 int retval;
1225
1226 skb = ovs_flow_cmd_alloc_info(flow);
1227 if (!skb)
1228 return ERR_PTR(-ENOMEM);
1229
Eric W. Biederman15e47302012-09-07 20:12:54 +00001230 retval = ovs_flow_cmd_fill_info(flow, dp, skb, portid, seq, 0, cmd);
Jesse Grossccb13522011-10-25 19:26:31 -07001231 BUG_ON(retval < 0);
1232 return skb;
1233}
1234
1235static int ovs_flow_cmd_new_or_set(struct sk_buff *skb, struct genl_info *info)
1236{
1237 struct nlattr **a = info->attrs;
1238 struct ovs_header *ovs_header = info->userhdr;
Andy Zhou03f0d912013-08-07 20:01:00 -07001239 struct sw_flow_key key, masked_key;
1240 struct sw_flow *flow = NULL;
1241 struct sw_flow_mask mask;
Jesse Grossccb13522011-10-25 19:26:31 -07001242 struct sk_buff *reply;
1243 struct datapath *dp;
1244 struct flow_table *table;
Pravin B Shelar74f84a52013-06-17 17:50:12 -07001245 struct sw_flow_actions *acts = NULL;
Andy Zhou03f0d912013-08-07 20:01:00 -07001246 struct sw_flow_match match;
Jesse Grossccb13522011-10-25 19:26:31 -07001247 int error;
Jesse Grossccb13522011-10-25 19:26:31 -07001248
1249 /* Extract key. */
1250 error = -EINVAL;
1251 if (!a[OVS_FLOW_ATTR_KEY])
1252 goto error;
Andy Zhou03f0d912013-08-07 20:01:00 -07001253
1254 ovs_match_init(&match, &key, &mask);
1255 error = ovs_match_from_nlattrs(&match,
1256 a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
Jesse Grossccb13522011-10-25 19:26:31 -07001257 if (error)
1258 goto error;
1259
1260 /* Validate actions. */
1261 if (a[OVS_FLOW_ATTR_ACTIONS]) {
Pravin B Shelar74f84a52013-06-17 17:50:12 -07001262 acts = ovs_flow_actions_alloc(nla_len(a[OVS_FLOW_ATTR_ACTIONS]));
1263 error = PTR_ERR(acts);
1264 if (IS_ERR(acts))
Jesse Grossccb13522011-10-25 19:26:31 -07001265 goto error;
Pravin B Shelar74f84a52013-06-17 17:50:12 -07001266
Andy Zhou03f0d912013-08-07 20:01:00 -07001267 ovs_flow_key_mask(&masked_key, &key, &mask);
1268 error = validate_and_copy_actions(a[OVS_FLOW_ATTR_ACTIONS],
1269 &masked_key, 0, &acts);
1270 if (error) {
1271 OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
Pravin B Shelar74f84a52013-06-17 17:50:12 -07001272 goto err_kfree;
Andy Zhou03f0d912013-08-07 20:01:00 -07001273 }
Jesse Grossccb13522011-10-25 19:26:31 -07001274 } else if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW) {
1275 error = -EINVAL;
1276 goto error;
1277 }
1278
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001279 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001280 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001281 error = -ENODEV;
1282 if (!dp)
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001283 goto err_unlock_ovs;
Jesse Grossccb13522011-10-25 19:26:31 -07001284
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001285 table = ovsl_dereference(dp->table);
Andy Zhou03f0d912013-08-07 20:01:00 -07001286
1287 /* Check if this is a duplicate flow */
1288 flow = ovs_flow_lookup(table, &key);
Jesse Grossccb13522011-10-25 19:26:31 -07001289 if (!flow) {
Pravin B Shelare7f13322013-09-17 09:38:23 -07001290 struct flow_table *new_table = NULL;
Andy Zhou03f0d912013-08-07 20:01:00 -07001291 struct sw_flow_mask *mask_p;
Pravin B Shelare7f13322013-09-17 09:38:23 -07001292
Jesse Grossccb13522011-10-25 19:26:31 -07001293 /* Bail out if we're not allowed to create a new flow. */
1294 error = -ENOENT;
1295 if (info->genlhdr->cmd == OVS_FLOW_CMD_SET)
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001296 goto err_unlock_ovs;
Jesse Grossccb13522011-10-25 19:26:31 -07001297
1298 /* Expand table, if necessary, to make room. */
Pravin B Shelare7f13322013-09-17 09:38:23 -07001299 if (ovs_flow_tbl_need_to_expand(table))
Jesse Grossccb13522011-10-25 19:26:31 -07001300 new_table = ovs_flow_tbl_expand(table);
Pravin B Shelare7f13322013-09-17 09:38:23 -07001301 else if (time_after(jiffies, dp->last_rehash + REHASH_FLOW_INTERVAL))
1302 new_table = ovs_flow_tbl_rehash(table);
1303
1304 if (new_table && !IS_ERR(new_table)) {
1305 rcu_assign_pointer(dp->table, new_table);
1306 ovs_flow_tbl_destroy(table, true);
1307 table = ovsl_dereference(dp->table);
1308 dp->last_rehash = jiffies;
Jesse Grossccb13522011-10-25 19:26:31 -07001309 }
1310
1311 /* Allocate flow. */
1312 flow = ovs_flow_alloc();
1313 if (IS_ERR(flow)) {
1314 error = PTR_ERR(flow);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001315 goto err_unlock_ovs;
Jesse Grossccb13522011-10-25 19:26:31 -07001316 }
Jesse Grossccb13522011-10-25 19:26:31 -07001317 clear_stats(flow);
1318
Andy Zhou03f0d912013-08-07 20:01:00 -07001319 flow->key = masked_key;
1320 flow->unmasked_key = key;
1321
1322 /* Make sure mask is unique in the system */
1323 mask_p = ovs_sw_flow_mask_find(table, &mask);
1324 if (!mask_p) {
1325 /* Allocate a new mask if none exsits. */
1326 mask_p = ovs_sw_flow_mask_alloc();
1327 if (!mask_p)
1328 goto err_flow_free;
1329 mask_p->key = mask.key;
1330 mask_p->range = mask.range;
1331 ovs_sw_flow_mask_insert(table, mask_p);
1332 }
1333
1334 ovs_sw_flow_mask_add_ref(mask_p);
1335 flow->mask = mask_p;
Jesse Grossccb13522011-10-25 19:26:31 -07001336 rcu_assign_pointer(flow->sf_acts, acts);
1337
1338 /* Put flow in bucket. */
Andy Zhou03f0d912013-08-07 20:01:00 -07001339 ovs_flow_insert(table, flow);
Jesse Grossccb13522011-10-25 19:26:31 -07001340
Eric W. Biederman15e47302012-09-07 20:12:54 +00001341 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
Andy Zhou03f0d912013-08-07 20:01:00 -07001342 info->snd_seq, OVS_FLOW_CMD_NEW);
Jesse Grossccb13522011-10-25 19:26:31 -07001343 } else {
1344 /* We found a matching flow. */
1345 struct sw_flow_actions *old_acts;
Jesse Grossccb13522011-10-25 19:26:31 -07001346
1347 /* Bail out if we're not allowed to modify an existing flow.
1348 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
1349 * because Generic Netlink treats the latter as a dump
1350 * request. We also accept NLM_F_EXCL in case that bug ever
1351 * gets fixed.
1352 */
1353 error = -EEXIST;
1354 if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW &&
1355 info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL))
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001356 goto err_unlock_ovs;
Jesse Grossccb13522011-10-25 19:26:31 -07001357
Andy Zhou03f0d912013-08-07 20:01:00 -07001358 /* The unmasked key has to be the same for flow updates. */
1359 error = -EINVAL;
1360 if (!ovs_flow_cmp_unmasked_key(flow, &key, match.range.end)) {
1361 OVS_NLERR("Flow modification message rejected, unmasked key does not match.\n");
1362 goto err_unlock_ovs;
1363 }
1364
Jesse Grossccb13522011-10-25 19:26:31 -07001365 /* Update actions. */
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001366 old_acts = ovsl_dereference(flow->sf_acts);
Pravin B Shelar74f84a52013-06-17 17:50:12 -07001367 rcu_assign_pointer(flow->sf_acts, acts);
1368 ovs_flow_deferred_free_acts(old_acts);
Jesse Grossccb13522011-10-25 19:26:31 -07001369
Eric W. Biederman15e47302012-09-07 20:12:54 +00001370 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001371 info->snd_seq, OVS_FLOW_CMD_NEW);
1372
1373 /* Clear stats. */
1374 if (a[OVS_FLOW_ATTR_CLEAR]) {
1375 spin_lock_bh(&flow->lock);
1376 clear_stats(flow);
1377 spin_unlock_bh(&flow->lock);
1378 }
1379 }
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001380 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001381
1382 if (!IS_ERR(reply))
Thomas Grafed6611852013-03-29 14:46:50 +01001383 ovs_notify(reply, info, &ovs_dp_flow_multicast_group);
Jesse Grossccb13522011-10-25 19:26:31 -07001384 else
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001385 netlink_set_err(sock_net(skb->sk)->genl_sock, 0,
Jesse Grossccb13522011-10-25 19:26:31 -07001386 ovs_dp_flow_multicast_group.id, PTR_ERR(reply));
1387 return 0;
1388
Andy Zhou03f0d912013-08-07 20:01:00 -07001389err_flow_free:
1390 ovs_flow_free(flow, false);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001391err_unlock_ovs:
1392 ovs_unlock();
Pravin B Shelar74f84a52013-06-17 17:50:12 -07001393err_kfree:
1394 kfree(acts);
Jesse Grossccb13522011-10-25 19:26:31 -07001395error:
1396 return error;
1397}
1398
1399static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
1400{
1401 struct nlattr **a = info->attrs;
1402 struct ovs_header *ovs_header = info->userhdr;
1403 struct sw_flow_key key;
1404 struct sk_buff *reply;
1405 struct sw_flow *flow;
1406 struct datapath *dp;
1407 struct flow_table *table;
Andy Zhou03f0d912013-08-07 20:01:00 -07001408 struct sw_flow_match match;
Jesse Grossccb13522011-10-25 19:26:31 -07001409 int err;
Jesse Grossccb13522011-10-25 19:26:31 -07001410
Andy Zhou03f0d912013-08-07 20:01:00 -07001411 if (!a[OVS_FLOW_ATTR_KEY]) {
1412 OVS_NLERR("Flow get message rejected, Key attribute missing.\n");
Jesse Grossccb13522011-10-25 19:26:31 -07001413 return -EINVAL;
Andy Zhou03f0d912013-08-07 20:01:00 -07001414 }
1415
1416 ovs_match_init(&match, &key, NULL);
1417 err = ovs_match_from_nlattrs(&match, a[OVS_FLOW_ATTR_KEY], NULL);
Jesse Grossccb13522011-10-25 19:26:31 -07001418 if (err)
1419 return err;
1420
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001421 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001422 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001423 if (!dp) {
1424 err = -ENODEV;
1425 goto unlock;
1426 }
Jesse Grossccb13522011-10-25 19:26:31 -07001427
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001428 table = ovsl_dereference(dp->table);
Andy Zhou03f0d912013-08-07 20:01:00 -07001429 flow = ovs_flow_lookup_unmasked_key(table, &match);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001430 if (!flow) {
1431 err = -ENOENT;
1432 goto unlock;
1433 }
Jesse Grossccb13522011-10-25 19:26:31 -07001434
Eric W. Biederman15e47302012-09-07 20:12:54 +00001435 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001436 info->snd_seq, OVS_FLOW_CMD_NEW);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001437 if (IS_ERR(reply)) {
1438 err = PTR_ERR(reply);
1439 goto unlock;
1440 }
Jesse Grossccb13522011-10-25 19:26:31 -07001441
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001442 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001443 return genlmsg_reply(reply, info);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001444unlock:
1445 ovs_unlock();
1446 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001447}
1448
1449static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
1450{
1451 struct nlattr **a = info->attrs;
1452 struct ovs_header *ovs_header = info->userhdr;
1453 struct sw_flow_key key;
1454 struct sk_buff *reply;
1455 struct sw_flow *flow;
1456 struct datapath *dp;
1457 struct flow_table *table;
Andy Zhou03f0d912013-08-07 20:01:00 -07001458 struct sw_flow_match match;
Jesse Grossccb13522011-10-25 19:26:31 -07001459 int err;
Jesse Grossccb13522011-10-25 19:26:31 -07001460
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001461 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001462 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001463 if (!dp) {
1464 err = -ENODEV;
1465 goto unlock;
1466 }
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001467
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001468 if (!a[OVS_FLOW_ATTR_KEY]) {
1469 err = flush_flows(dp);
1470 goto unlock;
1471 }
Andy Zhou03f0d912013-08-07 20:01:00 -07001472
1473 ovs_match_init(&match, &key, NULL);
1474 err = ovs_match_from_nlattrs(&match, a[OVS_FLOW_ATTR_KEY], NULL);
Jesse Grossccb13522011-10-25 19:26:31 -07001475 if (err)
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001476 goto unlock;
Jesse Grossccb13522011-10-25 19:26:31 -07001477
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001478 table = ovsl_dereference(dp->table);
Andy Zhou03f0d912013-08-07 20:01:00 -07001479 flow = ovs_flow_lookup_unmasked_key(table, &match);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001480 if (!flow) {
1481 err = -ENOENT;
1482 goto unlock;
1483 }
Jesse Grossccb13522011-10-25 19:26:31 -07001484
1485 reply = ovs_flow_cmd_alloc_info(flow);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001486 if (!reply) {
1487 err = -ENOMEM;
1488 goto unlock;
1489 }
Jesse Grossccb13522011-10-25 19:26:31 -07001490
Andy Zhou03f0d912013-08-07 20:01:00 -07001491 ovs_flow_remove(table, flow);
Jesse Grossccb13522011-10-25 19:26:31 -07001492
Eric W. Biederman15e47302012-09-07 20:12:54 +00001493 err = ovs_flow_cmd_fill_info(flow, dp, reply, info->snd_portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001494 info->snd_seq, 0, OVS_FLOW_CMD_DEL);
1495 BUG_ON(err < 0);
1496
Andy Zhou03f0d912013-08-07 20:01:00 -07001497 ovs_flow_free(flow, true);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001498 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001499
Thomas Grafed6611852013-03-29 14:46:50 +01001500 ovs_notify(reply, info, &ovs_dp_flow_multicast_group);
Jesse Grossccb13522011-10-25 19:26:31 -07001501 return 0;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001502unlock:
1503 ovs_unlock();
1504 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001505}
1506
1507static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1508{
1509 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1510 struct datapath *dp;
1511 struct flow_table *table;
1512
Pravin B Shelard57170b2013-07-30 15:39:39 -07001513 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001514 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001515 if (!dp) {
Pravin B Shelard57170b2013-07-30 15:39:39 -07001516 rcu_read_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001517 return -ENODEV;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001518 }
Jesse Grossccb13522011-10-25 19:26:31 -07001519
Pravin B Shelard57170b2013-07-30 15:39:39 -07001520 table = rcu_dereference(dp->table);
Jesse Grossccb13522011-10-25 19:26:31 -07001521 for (;;) {
1522 struct sw_flow *flow;
1523 u32 bucket, obj;
1524
1525 bucket = cb->args[0];
1526 obj = cb->args[1];
Andy Zhou03f0d912013-08-07 20:01:00 -07001527 flow = ovs_flow_dump_next(table, &bucket, &obj);
Jesse Grossccb13522011-10-25 19:26:31 -07001528 if (!flow)
1529 break;
1530
1531 if (ovs_flow_cmd_fill_info(flow, dp, skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001532 NETLINK_CB(cb->skb).portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001533 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1534 OVS_FLOW_CMD_NEW) < 0)
1535 break;
1536
1537 cb->args[0] = bucket;
1538 cb->args[1] = obj;
1539 }
Pravin B Shelard57170b2013-07-30 15:39:39 -07001540 rcu_read_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001541 return skb->len;
1542}
1543
1544static struct genl_ops dp_flow_genl_ops[] = {
1545 { .cmd = OVS_FLOW_CMD_NEW,
1546 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1547 .policy = flow_policy,
1548 .doit = ovs_flow_cmd_new_or_set
1549 },
1550 { .cmd = OVS_FLOW_CMD_DEL,
1551 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1552 .policy = flow_policy,
1553 .doit = ovs_flow_cmd_del
1554 },
1555 { .cmd = OVS_FLOW_CMD_GET,
1556 .flags = 0, /* OK for unprivileged users. */
1557 .policy = flow_policy,
1558 .doit = ovs_flow_cmd_get,
1559 .dumpit = ovs_flow_cmd_dump
1560 },
1561 { .cmd = OVS_FLOW_CMD_SET,
1562 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1563 .policy = flow_policy,
1564 .doit = ovs_flow_cmd_new_or_set,
1565 },
1566};
1567
1568static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1569 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1570 [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1571};
1572
1573static struct genl_family dp_datapath_genl_family = {
1574 .id = GENL_ID_GENERATE,
1575 .hdrsize = sizeof(struct ovs_header),
1576 .name = OVS_DATAPATH_FAMILY,
1577 .version = OVS_DATAPATH_VERSION,
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001578 .maxattr = OVS_DP_ATTR_MAX,
Pravin B Shelar3a4e0d62013-04-23 07:48:48 +00001579 .netnsok = true,
1580 .parallel_ops = true,
Jesse Grossccb13522011-10-25 19:26:31 -07001581};
1582
1583static struct genl_multicast_group ovs_dp_datapath_multicast_group = {
1584 .name = OVS_DATAPATH_MCGROUP
1585};
1586
Thomas Grafc3ff8cf2013-03-29 14:46:49 +01001587static size_t ovs_dp_cmd_msg_size(void)
1588{
1589 size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
1590
1591 msgsize += nla_total_size(IFNAMSIZ);
1592 msgsize += nla_total_size(sizeof(struct ovs_dp_stats));
1593
1594 return msgsize;
1595}
1596
Jesse Grossccb13522011-10-25 19:26:31 -07001597static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001598 u32 portid, u32 seq, u32 flags, u8 cmd)
Jesse Grossccb13522011-10-25 19:26:31 -07001599{
1600 struct ovs_header *ovs_header;
1601 struct ovs_dp_stats dp_stats;
1602 int err;
1603
Eric W. Biederman15e47302012-09-07 20:12:54 +00001604 ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
Jesse Grossccb13522011-10-25 19:26:31 -07001605 flags, cmd);
1606 if (!ovs_header)
1607 goto error;
1608
1609 ovs_header->dp_ifindex = get_dpifindex(dp);
1610
1611 rcu_read_lock();
1612 err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
1613 rcu_read_unlock();
1614 if (err)
1615 goto nla_put_failure;
1616
1617 get_dp_stats(dp, &dp_stats);
David S. Miller028d6a62012-03-29 23:20:48 -04001618 if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats), &dp_stats))
1619 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001620
1621 return genlmsg_end(skb, ovs_header);
1622
1623nla_put_failure:
1624 genlmsg_cancel(skb, ovs_header);
1625error:
1626 return -EMSGSIZE;
1627}
1628
Eric W. Biederman15e47302012-09-07 20:12:54 +00001629static struct sk_buff *ovs_dp_cmd_build_info(struct datapath *dp, u32 portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001630 u32 seq, u8 cmd)
1631{
1632 struct sk_buff *skb;
1633 int retval;
1634
Thomas Grafc3ff8cf2013-03-29 14:46:49 +01001635 skb = genlmsg_new(ovs_dp_cmd_msg_size(), GFP_KERNEL);
Jesse Grossccb13522011-10-25 19:26:31 -07001636 if (!skb)
1637 return ERR_PTR(-ENOMEM);
1638
Eric W. Biederman15e47302012-09-07 20:12:54 +00001639 retval = ovs_dp_cmd_fill_info(dp, skb, portid, seq, 0, cmd);
Jesse Grossccb13522011-10-25 19:26:31 -07001640 if (retval < 0) {
1641 kfree_skb(skb);
1642 return ERR_PTR(retval);
1643 }
1644 return skb;
1645}
1646
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001647/* Called with ovs_mutex. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001648static struct datapath *lookup_datapath(struct net *net,
1649 struct ovs_header *ovs_header,
Jesse Grossccb13522011-10-25 19:26:31 -07001650 struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1651{
1652 struct datapath *dp;
1653
1654 if (!a[OVS_DP_ATTR_NAME])
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001655 dp = get_dp(net, ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001656 else {
1657 struct vport *vport;
1658
1659 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001660 vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
Jesse Grossccb13522011-10-25 19:26:31 -07001661 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
1662 rcu_read_unlock();
1663 }
1664 return dp ? dp : ERR_PTR(-ENODEV);
1665}
1666
1667static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1668{
1669 struct nlattr **a = info->attrs;
1670 struct vport_parms parms;
1671 struct sk_buff *reply;
1672 struct datapath *dp;
1673 struct vport *vport;
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001674 struct ovs_net *ovs_net;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001675 int err, i;
Jesse Grossccb13522011-10-25 19:26:31 -07001676
1677 err = -EINVAL;
1678 if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1679 goto err;
1680
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001681 ovs_lock();
Jesse Grossccb13522011-10-25 19:26:31 -07001682
1683 err = -ENOMEM;
1684 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1685 if (dp == NULL)
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001686 goto err_unlock_ovs;
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001687
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001688 ovs_dp_set_net(dp, hold_net(sock_net(skb->sk)));
Jesse Grossccb13522011-10-25 19:26:31 -07001689
1690 /* Allocate table. */
1691 err = -ENOMEM;
1692 rcu_assign_pointer(dp->table, ovs_flow_tbl_alloc(TBL_MIN_BUCKETS));
1693 if (!dp->table)
1694 goto err_free_dp;
1695
1696 dp->stats_percpu = alloc_percpu(struct dp_stats_percpu);
1697 if (!dp->stats_percpu) {
1698 err = -ENOMEM;
1699 goto err_destroy_table;
1700 }
1701
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001702 dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
1703 GFP_KERNEL);
1704 if (!dp->ports) {
1705 err = -ENOMEM;
1706 goto err_destroy_percpu;
1707 }
1708
1709 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1710 INIT_HLIST_HEAD(&dp->ports[i]);
1711
Jesse Grossccb13522011-10-25 19:26:31 -07001712 /* Set up our datapath device. */
1713 parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1714 parms.type = OVS_VPORT_TYPE_INTERNAL;
1715 parms.options = NULL;
1716 parms.dp = dp;
1717 parms.port_no = OVSP_LOCAL;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001718 parms.upcall_portid = nla_get_u32(a[OVS_DP_ATTR_UPCALL_PID]);
Jesse Grossccb13522011-10-25 19:26:31 -07001719
1720 vport = new_vport(&parms);
1721 if (IS_ERR(vport)) {
1722 err = PTR_ERR(vport);
1723 if (err == -EBUSY)
1724 err = -EEXIST;
1725
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001726 goto err_destroy_ports_array;
Jesse Grossccb13522011-10-25 19:26:31 -07001727 }
1728
Eric W. Biederman15e47302012-09-07 20:12:54 +00001729 reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001730 info->snd_seq, OVS_DP_CMD_NEW);
1731 err = PTR_ERR(reply);
1732 if (IS_ERR(reply))
1733 goto err_destroy_local_port;
1734
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001735 ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001736 list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001737
1738 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001739
Thomas Grafed6611852013-03-29 14:46:50 +01001740 ovs_notify(reply, info, &ovs_dp_datapath_multicast_group);
Jesse Grossccb13522011-10-25 19:26:31 -07001741 return 0;
1742
1743err_destroy_local_port:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001744 ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001745err_destroy_ports_array:
1746 kfree(dp->ports);
Jesse Grossccb13522011-10-25 19:26:31 -07001747err_destroy_percpu:
1748 free_percpu(dp->stats_percpu);
1749err_destroy_table:
Andy Zhou03f0d912013-08-07 20:01:00 -07001750 ovs_flow_tbl_destroy(ovsl_dereference(dp->table), false);
Jesse Grossccb13522011-10-25 19:26:31 -07001751err_free_dp:
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001752 release_net(ovs_dp_get_net(dp));
Jesse Grossccb13522011-10-25 19:26:31 -07001753 kfree(dp);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001754err_unlock_ovs:
1755 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001756err:
1757 return err;
1758}
1759
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001760/* Called with ovs_mutex. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001761static void __dp_destroy(struct datapath *dp)
Jesse Grossccb13522011-10-25 19:26:31 -07001762{
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001763 int i;
Jesse Grossccb13522011-10-25 19:26:31 -07001764
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001765 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1766 struct vport *vport;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001767 struct hlist_node *n;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001768
Sasha Levinb67bfe02013-02-27 17:06:00 -08001769 hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001770 if (vport->port_no != OVSP_LOCAL)
1771 ovs_dp_detach_port(vport);
1772 }
Jesse Grossccb13522011-10-25 19:26:31 -07001773
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001774 list_del_rcu(&dp->list_node);
Jesse Grossccb13522011-10-25 19:26:31 -07001775
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001776 /* OVSP_LOCAL is datapath internal port. We need to make sure that
1777 * all port in datapath are destroyed first before freeing datapath.
Jesse Grossccb13522011-10-25 19:26:31 -07001778 */
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001779 ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
Jesse Grossccb13522011-10-25 19:26:31 -07001780
1781 call_rcu(&dp->rcu, destroy_dp_rcu);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001782}
1783
1784static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1785{
1786 struct sk_buff *reply;
1787 struct datapath *dp;
1788 int err;
1789
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001790 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001791 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1792 err = PTR_ERR(dp);
1793 if (IS_ERR(dp))
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001794 goto unlock;
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001795
Eric W. Biederman15e47302012-09-07 20:12:54 +00001796 reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001797 info->snd_seq, OVS_DP_CMD_DEL);
1798 err = PTR_ERR(reply);
1799 if (IS_ERR(reply))
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001800 goto unlock;
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001801
1802 __dp_destroy(dp);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001803 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001804
Thomas Grafed6611852013-03-29 14:46:50 +01001805 ovs_notify(reply, info, &ovs_dp_datapath_multicast_group);
Jesse Grossccb13522011-10-25 19:26:31 -07001806
1807 return 0;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001808unlock:
1809 ovs_unlock();
1810 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001811}
1812
1813static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1814{
1815 struct sk_buff *reply;
1816 struct datapath *dp;
1817 int err;
1818
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001819 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001820 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001821 err = PTR_ERR(dp);
Jesse Grossccb13522011-10-25 19:26:31 -07001822 if (IS_ERR(dp))
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001823 goto unlock;
Jesse Grossccb13522011-10-25 19:26:31 -07001824
Eric W. Biederman15e47302012-09-07 20:12:54 +00001825 reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001826 info->snd_seq, OVS_DP_CMD_NEW);
1827 if (IS_ERR(reply)) {
1828 err = PTR_ERR(reply);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001829 netlink_set_err(sock_net(skb->sk)->genl_sock, 0,
Jesse Grossccb13522011-10-25 19:26:31 -07001830 ovs_dp_datapath_multicast_group.id, err);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001831 err = 0;
1832 goto unlock;
Jesse Grossccb13522011-10-25 19:26:31 -07001833 }
1834
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001835 ovs_unlock();
Thomas Grafed6611852013-03-29 14:46:50 +01001836 ovs_notify(reply, info, &ovs_dp_datapath_multicast_group);
Jesse Grossccb13522011-10-25 19:26:31 -07001837
1838 return 0;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001839unlock:
1840 ovs_unlock();
1841 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001842}
1843
1844static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1845{
1846 struct sk_buff *reply;
1847 struct datapath *dp;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001848 int err;
Jesse Grossccb13522011-10-25 19:26:31 -07001849
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001850 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001851 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001852 if (IS_ERR(dp)) {
1853 err = PTR_ERR(dp);
1854 goto unlock;
1855 }
Jesse Grossccb13522011-10-25 19:26:31 -07001856
Eric W. Biederman15e47302012-09-07 20:12:54 +00001857 reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001858 info->snd_seq, OVS_DP_CMD_NEW);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001859 if (IS_ERR(reply)) {
1860 err = PTR_ERR(reply);
1861 goto unlock;
1862 }
Jesse Grossccb13522011-10-25 19:26:31 -07001863
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001864 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001865 return genlmsg_reply(reply, info);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001866
1867unlock:
1868 ovs_unlock();
1869 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001870}
1871
1872static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1873{
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001874 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
Jesse Grossccb13522011-10-25 19:26:31 -07001875 struct datapath *dp;
1876 int skip = cb->args[0];
1877 int i = 0;
1878
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001879 rcu_read_lock();
1880 list_for_each_entry_rcu(dp, &ovs_net->dps, list_node) {
Ben Pfaff77676fd2012-01-17 13:33:39 +00001881 if (i >= skip &&
Eric W. Biederman15e47302012-09-07 20:12:54 +00001882 ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001883 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1884 OVS_DP_CMD_NEW) < 0)
1885 break;
1886 i++;
1887 }
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001888 rcu_read_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001889
1890 cb->args[0] = i;
1891
1892 return skb->len;
1893}
1894
1895static struct genl_ops dp_datapath_genl_ops[] = {
1896 { .cmd = OVS_DP_CMD_NEW,
1897 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1898 .policy = datapath_policy,
1899 .doit = ovs_dp_cmd_new
1900 },
1901 { .cmd = OVS_DP_CMD_DEL,
1902 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1903 .policy = datapath_policy,
1904 .doit = ovs_dp_cmd_del
1905 },
1906 { .cmd = OVS_DP_CMD_GET,
1907 .flags = 0, /* OK for unprivileged users. */
1908 .policy = datapath_policy,
1909 .doit = ovs_dp_cmd_get,
1910 .dumpit = ovs_dp_cmd_dump
1911 },
1912 { .cmd = OVS_DP_CMD_SET,
1913 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1914 .policy = datapath_policy,
1915 .doit = ovs_dp_cmd_set,
1916 },
1917};
1918
1919static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
1920 [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1921 [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
1922 [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
1923 [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
1924 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1925 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
1926};
1927
1928static struct genl_family dp_vport_genl_family = {
1929 .id = GENL_ID_GENERATE,
1930 .hdrsize = sizeof(struct ovs_header),
1931 .name = OVS_VPORT_FAMILY,
1932 .version = OVS_VPORT_VERSION,
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001933 .maxattr = OVS_VPORT_ATTR_MAX,
Pravin B Shelar3a4e0d62013-04-23 07:48:48 +00001934 .netnsok = true,
1935 .parallel_ops = true,
Jesse Grossccb13522011-10-25 19:26:31 -07001936};
1937
1938struct genl_multicast_group ovs_dp_vport_multicast_group = {
1939 .name = OVS_VPORT_MCGROUP
1940};
1941
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001942/* Called with ovs_mutex or RCU read lock. */
Jesse Grossccb13522011-10-25 19:26:31 -07001943static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001944 u32 portid, u32 seq, u32 flags, u8 cmd)
Jesse Grossccb13522011-10-25 19:26:31 -07001945{
1946 struct ovs_header *ovs_header;
1947 struct ovs_vport_stats vport_stats;
1948 int err;
1949
Eric W. Biederman15e47302012-09-07 20:12:54 +00001950 ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
Jesse Grossccb13522011-10-25 19:26:31 -07001951 flags, cmd);
1952 if (!ovs_header)
1953 return -EMSGSIZE;
1954
1955 ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1956
David S. Miller028d6a62012-03-29 23:20:48 -04001957 if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
1958 nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
1959 nla_put_string(skb, OVS_VPORT_ATTR_NAME, vport->ops->get_name(vport)) ||
Eric W. Biederman15e47302012-09-07 20:12:54 +00001960 nla_put_u32(skb, OVS_VPORT_ATTR_UPCALL_PID, vport->upcall_portid))
David S. Miller028d6a62012-03-29 23:20:48 -04001961 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001962
1963 ovs_vport_get_stats(vport, &vport_stats);
David S. Miller028d6a62012-03-29 23:20:48 -04001964 if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
1965 &vport_stats))
1966 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001967
1968 err = ovs_vport_get_options(vport, skb);
1969 if (err == -EMSGSIZE)
1970 goto error;
1971
1972 return genlmsg_end(skb, ovs_header);
1973
1974nla_put_failure:
1975 err = -EMSGSIZE;
1976error:
1977 genlmsg_cancel(skb, ovs_header);
1978 return err;
1979}
1980
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001981/* Called with ovs_mutex or RCU read lock. */
Eric W. Biederman15e47302012-09-07 20:12:54 +00001982struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001983 u32 seq, u8 cmd)
1984{
1985 struct sk_buff *skb;
1986 int retval;
1987
1988 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1989 if (!skb)
1990 return ERR_PTR(-ENOMEM);
1991
Eric W. Biederman15e47302012-09-07 20:12:54 +00001992 retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
Jesse Grossa9341512013-03-26 15:48:38 -07001993 BUG_ON(retval < 0);
1994
Jesse Grossccb13522011-10-25 19:26:31 -07001995 return skb;
1996}
1997
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001998/* Called with ovs_mutex or RCU read lock. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001999static struct vport *lookup_vport(struct net *net,
2000 struct ovs_header *ovs_header,
Jesse Grossccb13522011-10-25 19:26:31 -07002001 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
2002{
2003 struct datapath *dp;
2004 struct vport *vport;
2005
2006 if (a[OVS_VPORT_ATTR_NAME]) {
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002007 vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
Jesse Grossccb13522011-10-25 19:26:31 -07002008 if (!vport)
2009 return ERR_PTR(-ENODEV);
Ben Pfaff651a68e2012-03-06 15:04:04 -08002010 if (ovs_header->dp_ifindex &&
2011 ovs_header->dp_ifindex != get_dpifindex(vport->dp))
2012 return ERR_PTR(-ENODEV);
Jesse Grossccb13522011-10-25 19:26:31 -07002013 return vport;
2014 } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
2015 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
2016
2017 if (port_no >= DP_MAX_PORTS)
2018 return ERR_PTR(-EFBIG);
2019
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002020 dp = get_dp(net, ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07002021 if (!dp)
2022 return ERR_PTR(-ENODEV);
2023
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002024 vport = ovs_vport_ovsl_rcu(dp, port_no);
Jesse Grossccb13522011-10-25 19:26:31 -07002025 if (!vport)
Jarno Rajahalme14408db2013-01-09 14:27:35 -08002026 return ERR_PTR(-ENODEV);
Jesse Grossccb13522011-10-25 19:26:31 -07002027 return vport;
2028 } else
2029 return ERR_PTR(-EINVAL);
2030}
2031
2032static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
2033{
2034 struct nlattr **a = info->attrs;
2035 struct ovs_header *ovs_header = info->userhdr;
2036 struct vport_parms parms;
2037 struct sk_buff *reply;
2038 struct vport *vport;
2039 struct datapath *dp;
2040 u32 port_no;
2041 int err;
2042
2043 err = -EINVAL;
2044 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
2045 !a[OVS_VPORT_ATTR_UPCALL_PID])
2046 goto exit;
2047
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002048 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002049 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07002050 err = -ENODEV;
2051 if (!dp)
2052 goto exit_unlock;
2053
2054 if (a[OVS_VPORT_ATTR_PORT_NO]) {
2055 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
2056
2057 err = -EFBIG;
2058 if (port_no >= DP_MAX_PORTS)
2059 goto exit_unlock;
2060
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002061 vport = ovs_vport_ovsl(dp, port_no);
Jesse Grossccb13522011-10-25 19:26:31 -07002062 err = -EBUSY;
2063 if (vport)
2064 goto exit_unlock;
2065 } else {
2066 for (port_no = 1; ; port_no++) {
2067 if (port_no >= DP_MAX_PORTS) {
2068 err = -EFBIG;
2069 goto exit_unlock;
2070 }
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002071 vport = ovs_vport_ovsl(dp, port_no);
Jesse Grossccb13522011-10-25 19:26:31 -07002072 if (!vport)
2073 break;
2074 }
2075 }
2076
2077 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
2078 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
2079 parms.options = a[OVS_VPORT_ATTR_OPTIONS];
2080 parms.dp = dp;
2081 parms.port_no = port_no;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002082 parms.upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
Jesse Grossccb13522011-10-25 19:26:31 -07002083
2084 vport = new_vport(&parms);
2085 err = PTR_ERR(vport);
2086 if (IS_ERR(vport))
2087 goto exit_unlock;
2088
Rich Lanecb7c5bd2013-02-08 13:18:01 -08002089 err = 0;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002090 reply = ovs_vport_cmd_build_info(vport, info->snd_portid, info->snd_seq,
Jesse Grossccb13522011-10-25 19:26:31 -07002091 OVS_VPORT_CMD_NEW);
2092 if (IS_ERR(reply)) {
2093 err = PTR_ERR(reply);
2094 ovs_dp_detach_port(vport);
2095 goto exit_unlock;
2096 }
Thomas Grafed6611852013-03-29 14:46:50 +01002097
2098 ovs_notify(reply, info, &ovs_dp_vport_multicast_group);
Jesse Grossccb13522011-10-25 19:26:31 -07002099
2100exit_unlock:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002101 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07002102exit:
2103 return err;
2104}
2105
2106static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
2107{
2108 struct nlattr **a = info->attrs;
2109 struct sk_buff *reply;
2110 struct vport *vport;
2111 int err;
2112
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002113 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002114 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
Jesse Grossccb13522011-10-25 19:26:31 -07002115 err = PTR_ERR(vport);
2116 if (IS_ERR(vport))
2117 goto exit_unlock;
2118
Jesse Grossccb13522011-10-25 19:26:31 -07002119 if (a[OVS_VPORT_ATTR_TYPE] &&
Jesse Grossf44f3402013-05-13 08:15:26 -07002120 nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
Jesse Grossccb13522011-10-25 19:26:31 -07002121 err = -EINVAL;
Jesse Grossf44f3402013-05-13 08:15:26 -07002122 goto exit_unlock;
2123 }
Jesse Grossccb13522011-10-25 19:26:31 -07002124
Jesse Grossa9341512013-03-26 15:48:38 -07002125 reply = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
2126 if (!reply) {
2127 err = -ENOMEM;
2128 goto exit_unlock;
2129 }
2130
Jesse Grossf44f3402013-05-13 08:15:26 -07002131 if (a[OVS_VPORT_ATTR_OPTIONS]) {
Jesse Grossccb13522011-10-25 19:26:31 -07002132 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
Jesse Grossf44f3402013-05-13 08:15:26 -07002133 if (err)
2134 goto exit_free;
2135 }
Jesse Grossa9341512013-03-26 15:48:38 -07002136
Ansis Atteka03fbf8b2012-04-09 12:12:12 -07002137 if (a[OVS_VPORT_ATTR_UPCALL_PID])
Eric W. Biederman15e47302012-09-07 20:12:54 +00002138 vport->upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
Jesse Grossccb13522011-10-25 19:26:31 -07002139
Jesse Grossa9341512013-03-26 15:48:38 -07002140 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
2141 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
2142 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07002143
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002144 ovs_unlock();
Thomas Grafed6611852013-03-29 14:46:50 +01002145 ovs_notify(reply, info, &ovs_dp_vport_multicast_group);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002146 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -07002147
Jesse Grossa9341512013-03-26 15:48:38 -07002148exit_free:
2149 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07002150exit_unlock:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002151 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07002152 return err;
2153}
2154
2155static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
2156{
2157 struct nlattr **a = info->attrs;
2158 struct sk_buff *reply;
2159 struct vport *vport;
2160 int err;
2161
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002162 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002163 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
Jesse Grossccb13522011-10-25 19:26:31 -07002164 err = PTR_ERR(vport);
2165 if (IS_ERR(vport))
2166 goto exit_unlock;
2167
2168 if (vport->port_no == OVSP_LOCAL) {
2169 err = -EINVAL;
2170 goto exit_unlock;
2171 }
2172
Pravin B Shelar74f84a52013-06-17 17:50:12 -07002173 reply = ovs_vport_cmd_build_info(vport, info->snd_portid,
2174 info->snd_seq, OVS_VPORT_CMD_DEL);
Jesse Grossccb13522011-10-25 19:26:31 -07002175 err = PTR_ERR(reply);
2176 if (IS_ERR(reply))
2177 goto exit_unlock;
2178
Rich Lane734907e2013-02-08 09:30:23 -08002179 err = 0;
Jesse Grossccb13522011-10-25 19:26:31 -07002180 ovs_dp_detach_port(vport);
2181
Thomas Grafed6611852013-03-29 14:46:50 +01002182 ovs_notify(reply, info, &ovs_dp_vport_multicast_group);
Jesse Grossccb13522011-10-25 19:26:31 -07002183
2184exit_unlock:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002185 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07002186 return err;
2187}
2188
2189static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
2190{
2191 struct nlattr **a = info->attrs;
2192 struct ovs_header *ovs_header = info->userhdr;
2193 struct sk_buff *reply;
2194 struct vport *vport;
2195 int err;
2196
2197 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002198 vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
Jesse Grossccb13522011-10-25 19:26:31 -07002199 err = PTR_ERR(vport);
2200 if (IS_ERR(vport))
2201 goto exit_unlock;
2202
Pravin B Shelar74f84a52013-06-17 17:50:12 -07002203 reply = ovs_vport_cmd_build_info(vport, info->snd_portid,
2204 info->snd_seq, OVS_VPORT_CMD_NEW);
Jesse Grossccb13522011-10-25 19:26:31 -07002205 err = PTR_ERR(reply);
2206 if (IS_ERR(reply))
2207 goto exit_unlock;
2208
2209 rcu_read_unlock();
2210
2211 return genlmsg_reply(reply, info);
2212
2213exit_unlock:
2214 rcu_read_unlock();
2215 return err;
2216}
2217
2218static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
2219{
2220 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
2221 struct datapath *dp;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07002222 int bucket = cb->args[0], skip = cb->args[1];
2223 int i, j = 0;
Jesse Grossccb13522011-10-25 19:26:31 -07002224
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002225 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07002226 if (!dp)
2227 return -ENODEV;
2228
2229 rcu_read_lock();
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07002230 for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
Jesse Grossccb13522011-10-25 19:26:31 -07002231 struct vport *vport;
2232
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07002233 j = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -08002234 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07002235 if (j >= skip &&
2236 ovs_vport_cmd_fill_info(vport, skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00002237 NETLINK_CB(cb->skb).portid,
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07002238 cb->nlh->nlmsg_seq,
2239 NLM_F_MULTI,
2240 OVS_VPORT_CMD_NEW) < 0)
2241 goto out;
Jesse Grossccb13522011-10-25 19:26:31 -07002242
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07002243 j++;
2244 }
2245 skip = 0;
Jesse Grossccb13522011-10-25 19:26:31 -07002246 }
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07002247out:
Jesse Grossccb13522011-10-25 19:26:31 -07002248 rcu_read_unlock();
2249
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07002250 cb->args[0] = i;
2251 cb->args[1] = j;
Jesse Grossccb13522011-10-25 19:26:31 -07002252
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07002253 return skb->len;
Jesse Grossccb13522011-10-25 19:26:31 -07002254}
2255
Jesse Grossccb13522011-10-25 19:26:31 -07002256static struct genl_ops dp_vport_genl_ops[] = {
2257 { .cmd = OVS_VPORT_CMD_NEW,
2258 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2259 .policy = vport_policy,
2260 .doit = ovs_vport_cmd_new
2261 },
2262 { .cmd = OVS_VPORT_CMD_DEL,
2263 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2264 .policy = vport_policy,
2265 .doit = ovs_vport_cmd_del
2266 },
2267 { .cmd = OVS_VPORT_CMD_GET,
2268 .flags = 0, /* OK for unprivileged users. */
2269 .policy = vport_policy,
2270 .doit = ovs_vport_cmd_get,
2271 .dumpit = ovs_vport_cmd_dump
2272 },
2273 { .cmd = OVS_VPORT_CMD_SET,
2274 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
2275 .policy = vport_policy,
2276 .doit = ovs_vport_cmd_set,
2277 },
2278};
2279
2280struct genl_family_and_ops {
2281 struct genl_family *family;
2282 struct genl_ops *ops;
2283 int n_ops;
2284 struct genl_multicast_group *group;
2285};
2286
2287static const struct genl_family_and_ops dp_genl_families[] = {
2288 { &dp_datapath_genl_family,
2289 dp_datapath_genl_ops, ARRAY_SIZE(dp_datapath_genl_ops),
2290 &ovs_dp_datapath_multicast_group },
2291 { &dp_vport_genl_family,
2292 dp_vport_genl_ops, ARRAY_SIZE(dp_vport_genl_ops),
2293 &ovs_dp_vport_multicast_group },
2294 { &dp_flow_genl_family,
2295 dp_flow_genl_ops, ARRAY_SIZE(dp_flow_genl_ops),
2296 &ovs_dp_flow_multicast_group },
2297 { &dp_packet_genl_family,
2298 dp_packet_genl_ops, ARRAY_SIZE(dp_packet_genl_ops),
2299 NULL },
2300};
2301
2302static void dp_unregister_genl(int n_families)
2303{
2304 int i;
2305
2306 for (i = 0; i < n_families; i++)
2307 genl_unregister_family(dp_genl_families[i].family);
2308}
2309
2310static int dp_register_genl(void)
2311{
2312 int n_registered;
2313 int err;
2314 int i;
2315
2316 n_registered = 0;
2317 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
2318 const struct genl_family_and_ops *f = &dp_genl_families[i];
2319
2320 err = genl_register_family_with_ops(f->family, f->ops,
2321 f->n_ops);
2322 if (err)
2323 goto error;
2324 n_registered++;
2325
2326 if (f->group) {
2327 err = genl_register_mc_group(f->family, f->group);
2328 if (err)
2329 goto error;
2330 }
2331 }
2332
2333 return 0;
2334
2335error:
2336 dp_unregister_genl(n_registered);
2337 return err;
2338}
2339
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002340static int __net_init ovs_init_net(struct net *net)
2341{
2342 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
2343
2344 INIT_LIST_HEAD(&ovs_net->dps);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002345 INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002346 return 0;
2347}
2348
2349static void __net_exit ovs_exit_net(struct net *net)
2350{
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002351 struct datapath *dp, *dp_next;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002352 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002353
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002354 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002355 list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
2356 __dp_destroy(dp);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07002357 ovs_unlock();
2358
2359 cancel_work_sync(&ovs_net->dp_notify_work);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002360}
2361
2362static struct pernet_operations ovs_net_ops = {
2363 .init = ovs_init_net,
2364 .exit = ovs_exit_net,
2365 .id = &ovs_net_id,
2366 .size = sizeof(struct ovs_net),
2367};
2368
Jesse Grossccb13522011-10-25 19:26:31 -07002369static int __init dp_init(void)
2370{
Jesse Grossccb13522011-10-25 19:26:31 -07002371 int err;
2372
YOSHIFUJI Hideaki / 吉藤英明3523b292013-01-09 07:19:55 +00002373 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
Jesse Grossccb13522011-10-25 19:26:31 -07002374
2375 pr_info("Open vSwitch switching datapath\n");
2376
2377 err = ovs_flow_init();
2378 if (err)
2379 goto error;
2380
2381 err = ovs_vport_init();
2382 if (err)
2383 goto error_flow_exit;
2384
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002385 err = register_pernet_device(&ovs_net_ops);
Jesse Grossccb13522011-10-25 19:26:31 -07002386 if (err)
2387 goto error_vport_exit;
2388
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002389 err = register_netdevice_notifier(&ovs_dp_device_notifier);
2390 if (err)
2391 goto error_netns_exit;
2392
Jesse Grossccb13522011-10-25 19:26:31 -07002393 err = dp_register_genl();
2394 if (err < 0)
2395 goto error_unreg_notifier;
2396
Jesse Grossccb13522011-10-25 19:26:31 -07002397 return 0;
2398
2399error_unreg_notifier:
2400 unregister_netdevice_notifier(&ovs_dp_device_notifier);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002401error_netns_exit:
2402 unregister_pernet_device(&ovs_net_ops);
Jesse Grossccb13522011-10-25 19:26:31 -07002403error_vport_exit:
2404 ovs_vport_exit();
2405error_flow_exit:
2406 ovs_flow_exit();
2407error:
2408 return err;
2409}
2410
2411static void dp_cleanup(void)
2412{
Jesse Grossccb13522011-10-25 19:26:31 -07002413 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
2414 unregister_netdevice_notifier(&ovs_dp_device_notifier);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08002415 unregister_pernet_device(&ovs_net_ops);
2416 rcu_barrier();
Jesse Grossccb13522011-10-25 19:26:31 -07002417 ovs_vport_exit();
2418 ovs_flow_exit();
2419}
2420
2421module_init(dp_init);
2422module_exit(dp_cleanup);
2423
2424MODULE_DESCRIPTION("Open vSwitch switching datapath");
2425MODULE_LICENSE("GPL");