blob: 72e68743c6435402ec1a02715214546f606ee979 [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"
Pravin B Shelare6445712013-10-03 18:16:47 -070058#include "flow_netlink.h"
Jesse Grossccb13522011-10-25 19:26:31 -070059#include "vport-internal_dev.h"
Thomas Grafcff63a52013-04-29 13:06:41 +000060#include "vport-netdev.h"
Jesse Grossccb13522011-10-25 19:26:31 -070061
Pravin B Shelar46df7b82012-02-22 19:58:59 -080062#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. */
Pravin B Shelare6445712013-10-03 18:16:47 -0700238 flow = ovs_flow_tbl_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);
Pravin B Shelare6445712013-10-03 18:16:47 -0700436 ovs_nla_put_flow(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
Jesse Grossccb13522011-10-25 19:26:31 -0700473static void clear_stats(struct sw_flow *flow)
474{
475 flow->used = 0;
476 flow->tcp_flags = 0;
477 flow->packet_count = 0;
478 flow->byte_count = 0;
479}
480
481static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
482{
483 struct ovs_header *ovs_header = info->userhdr;
484 struct nlattr **a = info->attrs;
485 struct sw_flow_actions *acts;
486 struct sk_buff *packet;
487 struct sw_flow *flow;
488 struct datapath *dp;
489 struct ethhdr *eth;
490 int len;
491 int err;
Jesse Grossccb13522011-10-25 19:26:31 -0700492
493 err = -EINVAL;
494 if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
Thomas Grafdded45fc2013-03-29 14:46:47 +0100495 !a[OVS_PACKET_ATTR_ACTIONS])
Jesse Grossccb13522011-10-25 19:26:31 -0700496 goto err;
497
498 len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
499 packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
500 err = -ENOMEM;
501 if (!packet)
502 goto err;
503 skb_reserve(packet, NET_IP_ALIGN);
504
Thomas Graf32686a92013-03-29 14:46:48 +0100505 nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
Jesse Grossccb13522011-10-25 19:26:31 -0700506
507 skb_reset_mac_header(packet);
508 eth = eth_hdr(packet);
509
510 /* Normally, setting the skb 'protocol' field would be handled by a
511 * call to eth_type_trans(), but it assumes there's a sending
512 * device, which we may not have. */
Simon Hormane5c5d222013-03-28 13:38:25 +0900513 if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN)
Jesse Grossccb13522011-10-25 19:26:31 -0700514 packet->protocol = eth->h_proto;
515 else
516 packet->protocol = htons(ETH_P_802_2);
517
518 /* Build an sw_flow for sending this packet. */
519 flow = ovs_flow_alloc();
520 err = PTR_ERR(flow);
521 if (IS_ERR(flow))
522 goto err_kfree_skb;
523
Andy Zhou03f0d912013-08-07 20:01:00 -0700524 err = ovs_flow_extract(packet, -1, &flow->key);
Jesse Grossccb13522011-10-25 19:26:31 -0700525 if (err)
526 goto err_flow_free;
527
Pravin B Shelare6445712013-10-03 18:16:47 -0700528 err = ovs_nla_get_flow_metadata(flow, a[OVS_PACKET_ATTR_KEY]);
Jesse Grossccb13522011-10-25 19:26:31 -0700529 if (err)
530 goto err_flow_free;
Pravin B Shelare6445712013-10-03 18:16:47 -0700531 acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_PACKET_ATTR_ACTIONS]));
Jesse Grossccb13522011-10-25 19:26:31 -0700532 err = PTR_ERR(acts);
533 if (IS_ERR(acts))
534 goto err_flow_free;
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700535
Pravin B Shelare6445712013-10-03 18:16:47 -0700536 err = ovs_nla_copy_actions(a[OVS_PACKET_ATTR_ACTIONS],
537 &flow->key, 0, &acts);
Jesse Grossccb13522011-10-25 19:26:31 -0700538 rcu_assign_pointer(flow->sf_acts, acts);
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700539 if (err)
540 goto err_flow_free;
Jesse Grossccb13522011-10-25 19:26:31 -0700541
542 OVS_CB(packet)->flow = flow;
Andy Zhou03f0d912013-08-07 20:01:00 -0700543 OVS_CB(packet)->pkt_key = &flow->key;
Jesse Grossccb13522011-10-25 19:26:31 -0700544 packet->priority = flow->key.phy.priority;
Ansis Atteka39c7caeb2012-11-26 11:24:11 -0800545 packet->mark = flow->key.phy.skb_mark;
Jesse Grossccb13522011-10-25 19:26:31 -0700546
547 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800548 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -0700549 err = -ENODEV;
550 if (!dp)
551 goto err_unlock;
552
553 local_bh_disable();
554 err = ovs_execute_actions(dp, packet);
555 local_bh_enable();
556 rcu_read_unlock();
557
Andy Zhou03f0d912013-08-07 20:01:00 -0700558 ovs_flow_free(flow, false);
Jesse Grossccb13522011-10-25 19:26:31 -0700559 return err;
560
561err_unlock:
562 rcu_read_unlock();
563err_flow_free:
Andy Zhou03f0d912013-08-07 20:01:00 -0700564 ovs_flow_free(flow, false);
Jesse Grossccb13522011-10-25 19:26:31 -0700565err_kfree_skb:
566 kfree_skb(packet);
567err:
568 return err;
569}
570
571static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
Thomas Grafdded45fc2013-03-29 14:46:47 +0100572 [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
Jesse Grossccb13522011-10-25 19:26:31 -0700573 [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
574 [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
575};
576
577static struct genl_ops dp_packet_genl_ops[] = {
578 { .cmd = OVS_PACKET_CMD_EXECUTE,
579 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
580 .policy = packet_policy,
581 .doit = ovs_packet_cmd_execute
582 }
583};
584
585static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats)
586{
Pravin B Shelar59a35d62013-07-30 15:42:19 -0700587 struct flow_table *table;
Jesse Grossccb13522011-10-25 19:26:31 -0700588 int i;
Jesse Grossccb13522011-10-25 19:26:31 -0700589
Pravin B Shelar59a35d62013-07-30 15:42:19 -0700590 table = rcu_dereference_check(dp->table, lockdep_ovsl_is_held());
Jesse Grossccb13522011-10-25 19:26:31 -0700591 stats->n_flows = ovs_flow_tbl_count(table);
592
593 stats->n_hit = stats->n_missed = stats->n_lost = 0;
594 for_each_possible_cpu(i) {
595 const struct dp_stats_percpu *percpu_stats;
596 struct dp_stats_percpu local_stats;
597 unsigned int start;
598
599 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
600
601 do {
602 start = u64_stats_fetch_begin_bh(&percpu_stats->sync);
603 local_stats = *percpu_stats;
604 } while (u64_stats_fetch_retry_bh(&percpu_stats->sync, start));
605
606 stats->n_hit += local_stats.n_hit;
607 stats->n_missed += local_stats.n_missed;
608 stats->n_lost += local_stats.n_lost;
609 }
610}
611
612static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
613 [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
614 [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
615 [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
616};
617
618static struct genl_family dp_flow_genl_family = {
619 .id = GENL_ID_GENERATE,
620 .hdrsize = sizeof(struct ovs_header),
621 .name = OVS_FLOW_FAMILY,
622 .version = OVS_FLOW_VERSION,
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800623 .maxattr = OVS_FLOW_ATTR_MAX,
Pravin B Shelar3a4e0d62013-04-23 07:48:48 +0000624 .netnsok = true,
625 .parallel_ops = true,
Jesse Grossccb13522011-10-25 19:26:31 -0700626};
627
628static struct genl_multicast_group ovs_dp_flow_multicast_group = {
629 .name = OVS_FLOW_MCGROUP
630};
631
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100632static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts)
633{
634 return NLMSG_ALIGN(sizeof(struct ovs_header))
635 + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_KEY */
Andy Zhou03f0d912013-08-07 20:01:00 -0700636 + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_MASK */
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100637 + nla_total_size(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
638 + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
639 + nla_total_size(8) /* OVS_FLOW_ATTR_USED */
640 + nla_total_size(acts->actions_len); /* OVS_FLOW_ATTR_ACTIONS */
641}
642
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700643/* Called with ovs_mutex. */
Jesse Grossccb13522011-10-25 19:26:31 -0700644static int ovs_flow_cmd_fill_info(struct sw_flow *flow, struct datapath *dp,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000645 struct sk_buff *skb, u32 portid,
Jesse Grossccb13522011-10-25 19:26:31 -0700646 u32 seq, u32 flags, u8 cmd)
647{
648 const int skb_orig_len = skb->len;
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700649 struct nlattr *start;
Jesse Grossccb13522011-10-25 19:26:31 -0700650 struct ovs_flow_stats stats;
651 struct ovs_header *ovs_header;
652 struct nlattr *nla;
653 unsigned long used;
654 u8 tcp_flags;
655 int err;
656
Eric W. Biederman15e47302012-09-07 20:12:54 +0000657 ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family, flags, cmd);
Jesse Grossccb13522011-10-25 19:26:31 -0700658 if (!ovs_header)
659 return -EMSGSIZE;
660
661 ovs_header->dp_ifindex = get_dpifindex(dp);
662
Andy Zhou03f0d912013-08-07 20:01:00 -0700663 /* Fill flow key. */
Jesse Grossccb13522011-10-25 19:26:31 -0700664 nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
665 if (!nla)
666 goto nla_put_failure;
Andy Zhou03f0d912013-08-07 20:01:00 -0700667
Pravin B Shelare6445712013-10-03 18:16:47 -0700668 err = ovs_nla_put_flow(&flow->unmasked_key, &flow->unmasked_key, skb);
Jesse Grossccb13522011-10-25 19:26:31 -0700669 if (err)
670 goto error;
671 nla_nest_end(skb, nla);
672
Andy Zhou03f0d912013-08-07 20:01:00 -0700673 nla = nla_nest_start(skb, OVS_FLOW_ATTR_MASK);
674 if (!nla)
675 goto nla_put_failure;
676
Pravin B Shelare6445712013-10-03 18:16:47 -0700677 err = ovs_nla_put_flow(&flow->key, &flow->mask->key, skb);
Andy Zhou03f0d912013-08-07 20:01:00 -0700678 if (err)
679 goto error;
680
681 nla_nest_end(skb, nla);
682
Jesse Grossccb13522011-10-25 19:26:31 -0700683 spin_lock_bh(&flow->lock);
684 used = flow->used;
685 stats.n_packets = flow->packet_count;
686 stats.n_bytes = flow->byte_count;
687 tcp_flags = flow->tcp_flags;
688 spin_unlock_bh(&flow->lock);
689
David S. Miller028d6a62012-03-29 23:20:48 -0400690 if (used &&
691 nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
692 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -0700693
David S. Miller028d6a62012-03-29 23:20:48 -0400694 if (stats.n_packets &&
695 nla_put(skb, OVS_FLOW_ATTR_STATS,
696 sizeof(struct ovs_flow_stats), &stats))
697 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -0700698
David S. Miller028d6a62012-03-29 23:20:48 -0400699 if (tcp_flags &&
700 nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, tcp_flags))
701 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -0700702
703 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
704 * this is the first flow to be dumped into 'skb'. This is unusual for
705 * Netlink but individual action lists can be longer than
706 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
707 * The userspace caller can always fetch the actions separately if it
708 * really wants them. (Most userspace callers in fact don't care.)
709 *
710 * This can only fail for dump operations because the skb is always
711 * properly sized for single flows.
712 */
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700713 start = nla_nest_start(skb, OVS_FLOW_ATTR_ACTIONS);
714 if (start) {
Pravin B Shelard57170b2013-07-30 15:39:39 -0700715 const struct sw_flow_actions *sf_acts;
716
717 sf_acts = rcu_dereference_check(flow->sf_acts,
718 lockdep_ovsl_is_held());
719
Pravin B Shelare6445712013-10-03 18:16:47 -0700720 err = ovs_nla_put_actions(sf_acts->actions,
721 sf_acts->actions_len, skb);
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700722 if (!err)
723 nla_nest_end(skb, start);
724 else {
725 if (skb_orig_len)
726 goto error;
727
728 nla_nest_cancel(skb, start);
729 }
730 } else if (skb_orig_len)
731 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -0700732
733 return genlmsg_end(skb, ovs_header);
734
735nla_put_failure:
736 err = -EMSGSIZE;
737error:
738 genlmsg_cancel(skb, ovs_header);
739 return err;
740}
741
742static struct sk_buff *ovs_flow_cmd_alloc_info(struct sw_flow *flow)
743{
744 const struct sw_flow_actions *sf_acts;
Jesse Grossccb13522011-10-25 19:26:31 -0700745
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700746 sf_acts = ovsl_dereference(flow->sf_acts);
Jesse Grossccb13522011-10-25 19:26:31 -0700747
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100748 return genlmsg_new(ovs_flow_cmd_msg_size(sf_acts), GFP_KERNEL);
Jesse Grossccb13522011-10-25 19:26:31 -0700749}
750
751static struct sk_buff *ovs_flow_cmd_build_info(struct sw_flow *flow,
752 struct datapath *dp,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000753 u32 portid, u32 seq, u8 cmd)
Jesse Grossccb13522011-10-25 19:26:31 -0700754{
755 struct sk_buff *skb;
756 int retval;
757
758 skb = ovs_flow_cmd_alloc_info(flow);
759 if (!skb)
760 return ERR_PTR(-ENOMEM);
761
Eric W. Biederman15e47302012-09-07 20:12:54 +0000762 retval = ovs_flow_cmd_fill_info(flow, dp, skb, portid, seq, 0, cmd);
Jesse Grossccb13522011-10-25 19:26:31 -0700763 BUG_ON(retval < 0);
764 return skb;
765}
766
767static int ovs_flow_cmd_new_or_set(struct sk_buff *skb, struct genl_info *info)
768{
769 struct nlattr **a = info->attrs;
770 struct ovs_header *ovs_header = info->userhdr;
Andy Zhou03f0d912013-08-07 20:01:00 -0700771 struct sw_flow_key key, masked_key;
772 struct sw_flow *flow = NULL;
773 struct sw_flow_mask mask;
Jesse Grossccb13522011-10-25 19:26:31 -0700774 struct sk_buff *reply;
775 struct datapath *dp;
776 struct flow_table *table;
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700777 struct sw_flow_actions *acts = NULL;
Andy Zhou03f0d912013-08-07 20:01:00 -0700778 struct sw_flow_match match;
Jesse Grossccb13522011-10-25 19:26:31 -0700779 int error;
Jesse Grossccb13522011-10-25 19:26:31 -0700780
781 /* Extract key. */
782 error = -EINVAL;
783 if (!a[OVS_FLOW_ATTR_KEY])
784 goto error;
Andy Zhou03f0d912013-08-07 20:01:00 -0700785
786 ovs_match_init(&match, &key, &mask);
Pravin B Shelare6445712013-10-03 18:16:47 -0700787 error = ovs_nla_get_match(&match,
788 a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
Jesse Grossccb13522011-10-25 19:26:31 -0700789 if (error)
790 goto error;
791
792 /* Validate actions. */
793 if (a[OVS_FLOW_ATTR_ACTIONS]) {
Pravin B Shelare6445712013-10-03 18:16:47 -0700794 acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_FLOW_ATTR_ACTIONS]));
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700795 error = PTR_ERR(acts);
796 if (IS_ERR(acts))
Jesse Grossccb13522011-10-25 19:26:31 -0700797 goto error;
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700798
Pravin B Shelare6445712013-10-03 18:16:47 -0700799 ovs_flow_mask_key(&masked_key, &key, &mask);
800 error = ovs_nla_copy_actions(a[OVS_FLOW_ATTR_ACTIONS],
801 &masked_key, 0, &acts);
Andy Zhou03f0d912013-08-07 20:01:00 -0700802 if (error) {
803 OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700804 goto err_kfree;
Andy Zhou03f0d912013-08-07 20:01:00 -0700805 }
Jesse Grossccb13522011-10-25 19:26:31 -0700806 } else if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW) {
807 error = -EINVAL;
808 goto error;
809 }
810
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700811 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800812 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -0700813 error = -ENODEV;
814 if (!dp)
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700815 goto err_unlock_ovs;
Jesse Grossccb13522011-10-25 19:26:31 -0700816
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700817 table = ovsl_dereference(dp->table);
Andy Zhou03f0d912013-08-07 20:01:00 -0700818
819 /* Check if this is a duplicate flow */
Pravin B Shelare6445712013-10-03 18:16:47 -0700820 flow = ovs_flow_tbl_lookup(table, &key);
Jesse Grossccb13522011-10-25 19:26:31 -0700821 if (!flow) {
Pravin B Shelare7f13322013-09-17 09:38:23 -0700822 struct flow_table *new_table = NULL;
Andy Zhou03f0d912013-08-07 20:01:00 -0700823 struct sw_flow_mask *mask_p;
Pravin B Shelare7f13322013-09-17 09:38:23 -0700824
Jesse Grossccb13522011-10-25 19:26:31 -0700825 /* Bail out if we're not allowed to create a new flow. */
826 error = -ENOENT;
827 if (info->genlhdr->cmd == OVS_FLOW_CMD_SET)
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700828 goto err_unlock_ovs;
Jesse Grossccb13522011-10-25 19:26:31 -0700829
830 /* Expand table, if necessary, to make room. */
Pravin B Shelare7f13322013-09-17 09:38:23 -0700831 if (ovs_flow_tbl_need_to_expand(table))
Jesse Grossccb13522011-10-25 19:26:31 -0700832 new_table = ovs_flow_tbl_expand(table);
Pravin B Shelare7f13322013-09-17 09:38:23 -0700833 else if (time_after(jiffies, dp->last_rehash + REHASH_FLOW_INTERVAL))
834 new_table = ovs_flow_tbl_rehash(table);
835
836 if (new_table && !IS_ERR(new_table)) {
837 rcu_assign_pointer(dp->table, new_table);
838 ovs_flow_tbl_destroy(table, true);
839 table = ovsl_dereference(dp->table);
840 dp->last_rehash = jiffies;
Jesse Grossccb13522011-10-25 19:26:31 -0700841 }
842
843 /* Allocate flow. */
844 flow = ovs_flow_alloc();
845 if (IS_ERR(flow)) {
846 error = PTR_ERR(flow);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700847 goto err_unlock_ovs;
Jesse Grossccb13522011-10-25 19:26:31 -0700848 }
Jesse Grossccb13522011-10-25 19:26:31 -0700849 clear_stats(flow);
850
Andy Zhou03f0d912013-08-07 20:01:00 -0700851 flow->key = masked_key;
852 flow->unmasked_key = key;
853
854 /* Make sure mask is unique in the system */
855 mask_p = ovs_sw_flow_mask_find(table, &mask);
856 if (!mask_p) {
857 /* Allocate a new mask if none exsits. */
858 mask_p = ovs_sw_flow_mask_alloc();
859 if (!mask_p)
860 goto err_flow_free;
861 mask_p->key = mask.key;
862 mask_p->range = mask.range;
863 ovs_sw_flow_mask_insert(table, mask_p);
864 }
865
866 ovs_sw_flow_mask_add_ref(mask_p);
867 flow->mask = mask_p;
Jesse Grossccb13522011-10-25 19:26:31 -0700868 rcu_assign_pointer(flow->sf_acts, acts);
869
870 /* Put flow in bucket. */
Pravin B Shelare6445712013-10-03 18:16:47 -0700871 ovs_flow_tbl_insert(table, flow);
Jesse Grossccb13522011-10-25 19:26:31 -0700872
Eric W. Biederman15e47302012-09-07 20:12:54 +0000873 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
Andy Zhou03f0d912013-08-07 20:01:00 -0700874 info->snd_seq, OVS_FLOW_CMD_NEW);
Jesse Grossccb13522011-10-25 19:26:31 -0700875 } else {
876 /* We found a matching flow. */
877 struct sw_flow_actions *old_acts;
Jesse Grossccb13522011-10-25 19:26:31 -0700878
879 /* Bail out if we're not allowed to modify an existing flow.
880 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
881 * because Generic Netlink treats the latter as a dump
882 * request. We also accept NLM_F_EXCL in case that bug ever
883 * gets fixed.
884 */
885 error = -EEXIST;
886 if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW &&
887 info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL))
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700888 goto err_unlock_ovs;
Jesse Grossccb13522011-10-25 19:26:31 -0700889
Andy Zhou03f0d912013-08-07 20:01:00 -0700890 /* The unmasked key has to be the same for flow updates. */
891 error = -EINVAL;
Pravin B Shelare6445712013-10-03 18:16:47 -0700892 if (!ovs_flow_cmp_unmasked_key(flow, &match)) {
Andy Zhou03f0d912013-08-07 20:01:00 -0700893 OVS_NLERR("Flow modification message rejected, unmasked key does not match.\n");
894 goto err_unlock_ovs;
895 }
896
Jesse Grossccb13522011-10-25 19:26:31 -0700897 /* Update actions. */
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700898 old_acts = ovsl_dereference(flow->sf_acts);
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700899 rcu_assign_pointer(flow->sf_acts, acts);
Pravin B Shelare6445712013-10-03 18:16:47 -0700900 ovs_nla_free_flow_actions(old_acts);
Jesse Grossccb13522011-10-25 19:26:31 -0700901
Eric W. Biederman15e47302012-09-07 20:12:54 +0000902 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
Jesse Grossccb13522011-10-25 19:26:31 -0700903 info->snd_seq, OVS_FLOW_CMD_NEW);
904
905 /* Clear stats. */
906 if (a[OVS_FLOW_ATTR_CLEAR]) {
907 spin_lock_bh(&flow->lock);
908 clear_stats(flow);
909 spin_unlock_bh(&flow->lock);
910 }
911 }
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700912 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -0700913
914 if (!IS_ERR(reply))
Thomas Grafed6611852013-03-29 14:46:50 +0100915 ovs_notify(reply, info, &ovs_dp_flow_multicast_group);
Jesse Grossccb13522011-10-25 19:26:31 -0700916 else
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800917 netlink_set_err(sock_net(skb->sk)->genl_sock, 0,
Jesse Grossccb13522011-10-25 19:26:31 -0700918 ovs_dp_flow_multicast_group.id, PTR_ERR(reply));
919 return 0;
920
Andy Zhou03f0d912013-08-07 20:01:00 -0700921err_flow_free:
922 ovs_flow_free(flow, false);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700923err_unlock_ovs:
924 ovs_unlock();
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700925err_kfree:
926 kfree(acts);
Jesse Grossccb13522011-10-25 19:26:31 -0700927error:
928 return error;
929}
930
931static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
932{
933 struct nlattr **a = info->attrs;
934 struct ovs_header *ovs_header = info->userhdr;
935 struct sw_flow_key key;
936 struct sk_buff *reply;
937 struct sw_flow *flow;
938 struct datapath *dp;
939 struct flow_table *table;
Andy Zhou03f0d912013-08-07 20:01:00 -0700940 struct sw_flow_match match;
Jesse Grossccb13522011-10-25 19:26:31 -0700941 int err;
Jesse Grossccb13522011-10-25 19:26:31 -0700942
Andy Zhou03f0d912013-08-07 20:01:00 -0700943 if (!a[OVS_FLOW_ATTR_KEY]) {
944 OVS_NLERR("Flow get message rejected, Key attribute missing.\n");
Jesse Grossccb13522011-10-25 19:26:31 -0700945 return -EINVAL;
Andy Zhou03f0d912013-08-07 20:01:00 -0700946 }
947
948 ovs_match_init(&match, &key, NULL);
Pravin B Shelare6445712013-10-03 18:16:47 -0700949 err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL);
Jesse Grossccb13522011-10-25 19:26:31 -0700950 if (err)
951 return err;
952
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700953 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800954 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700955 if (!dp) {
956 err = -ENODEV;
957 goto unlock;
958 }
Jesse Grossccb13522011-10-25 19:26:31 -0700959
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700960 table = ovsl_dereference(dp->table);
Pravin B Shelare6445712013-10-03 18:16:47 -0700961 flow = ovs_flow_tbl_lookup(table, &key);
962 if (!flow || !ovs_flow_cmp_unmasked_key(flow, &match)) {
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700963 err = -ENOENT;
964 goto unlock;
965 }
Jesse Grossccb13522011-10-25 19:26:31 -0700966
Eric W. Biederman15e47302012-09-07 20:12:54 +0000967 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
Jesse Grossccb13522011-10-25 19:26:31 -0700968 info->snd_seq, OVS_FLOW_CMD_NEW);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700969 if (IS_ERR(reply)) {
970 err = PTR_ERR(reply);
971 goto unlock;
972 }
Jesse Grossccb13522011-10-25 19:26:31 -0700973
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700974 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -0700975 return genlmsg_reply(reply, info);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700976unlock:
977 ovs_unlock();
978 return err;
Jesse Grossccb13522011-10-25 19:26:31 -0700979}
980
981static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
982{
983 struct nlattr **a = info->attrs;
984 struct ovs_header *ovs_header = info->userhdr;
985 struct sw_flow_key key;
986 struct sk_buff *reply;
987 struct sw_flow *flow;
988 struct datapath *dp;
989 struct flow_table *table;
Andy Zhou03f0d912013-08-07 20:01:00 -0700990 struct sw_flow_match match;
Jesse Grossccb13522011-10-25 19:26:31 -0700991 int err;
Jesse Grossccb13522011-10-25 19:26:31 -0700992
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700993 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800994 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700995 if (!dp) {
996 err = -ENODEV;
997 goto unlock;
998 }
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800999
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001000 if (!a[OVS_FLOW_ATTR_KEY]) {
1001 err = flush_flows(dp);
1002 goto unlock;
1003 }
Andy Zhou03f0d912013-08-07 20:01:00 -07001004
1005 ovs_match_init(&match, &key, NULL);
Pravin B Shelare6445712013-10-03 18:16:47 -07001006 err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL);
Jesse Grossccb13522011-10-25 19:26:31 -07001007 if (err)
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001008 goto unlock;
Jesse Grossccb13522011-10-25 19:26:31 -07001009
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001010 table = ovsl_dereference(dp->table);
Pravin B Shelare6445712013-10-03 18:16:47 -07001011 flow = ovs_flow_tbl_lookup(table, &key);
1012 if (!flow || !ovs_flow_cmp_unmasked_key(flow, &match)) {
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001013 err = -ENOENT;
1014 goto unlock;
1015 }
Jesse Grossccb13522011-10-25 19:26:31 -07001016
1017 reply = ovs_flow_cmd_alloc_info(flow);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001018 if (!reply) {
1019 err = -ENOMEM;
1020 goto unlock;
1021 }
Jesse Grossccb13522011-10-25 19:26:31 -07001022
Pravin B Shelare6445712013-10-03 18:16:47 -07001023 ovs_flow_tbl_remove(table, flow);
Jesse Grossccb13522011-10-25 19:26:31 -07001024
Eric W. Biederman15e47302012-09-07 20:12:54 +00001025 err = ovs_flow_cmd_fill_info(flow, dp, reply, info->snd_portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001026 info->snd_seq, 0, OVS_FLOW_CMD_DEL);
1027 BUG_ON(err < 0);
1028
Andy Zhou03f0d912013-08-07 20:01:00 -07001029 ovs_flow_free(flow, true);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001030 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001031
Thomas Grafed6611852013-03-29 14:46:50 +01001032 ovs_notify(reply, info, &ovs_dp_flow_multicast_group);
Jesse Grossccb13522011-10-25 19:26:31 -07001033 return 0;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001034unlock:
1035 ovs_unlock();
1036 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001037}
1038
1039static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1040{
1041 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1042 struct datapath *dp;
1043 struct flow_table *table;
1044
Pravin B Shelard57170b2013-07-30 15:39:39 -07001045 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001046 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001047 if (!dp) {
Pravin B Shelard57170b2013-07-30 15:39:39 -07001048 rcu_read_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001049 return -ENODEV;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001050 }
Jesse Grossccb13522011-10-25 19:26:31 -07001051
Pravin B Shelard57170b2013-07-30 15:39:39 -07001052 table = rcu_dereference(dp->table);
Jesse Grossccb13522011-10-25 19:26:31 -07001053 for (;;) {
1054 struct sw_flow *flow;
1055 u32 bucket, obj;
1056
1057 bucket = cb->args[0];
1058 obj = cb->args[1];
Pravin B Shelare6445712013-10-03 18:16:47 -07001059 flow = ovs_flow_tbl_dump_next(table, &bucket, &obj);
Jesse Grossccb13522011-10-25 19:26:31 -07001060 if (!flow)
1061 break;
1062
1063 if (ovs_flow_cmd_fill_info(flow, dp, skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001064 NETLINK_CB(cb->skb).portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001065 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1066 OVS_FLOW_CMD_NEW) < 0)
1067 break;
1068
1069 cb->args[0] = bucket;
1070 cb->args[1] = obj;
1071 }
Pravin B Shelard57170b2013-07-30 15:39:39 -07001072 rcu_read_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001073 return skb->len;
1074}
1075
1076static struct genl_ops dp_flow_genl_ops[] = {
1077 { .cmd = OVS_FLOW_CMD_NEW,
1078 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1079 .policy = flow_policy,
1080 .doit = ovs_flow_cmd_new_or_set
1081 },
1082 { .cmd = OVS_FLOW_CMD_DEL,
1083 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1084 .policy = flow_policy,
1085 .doit = ovs_flow_cmd_del
1086 },
1087 { .cmd = OVS_FLOW_CMD_GET,
1088 .flags = 0, /* OK for unprivileged users. */
1089 .policy = flow_policy,
1090 .doit = ovs_flow_cmd_get,
1091 .dumpit = ovs_flow_cmd_dump
1092 },
1093 { .cmd = OVS_FLOW_CMD_SET,
1094 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1095 .policy = flow_policy,
1096 .doit = ovs_flow_cmd_new_or_set,
1097 },
1098};
1099
1100static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1101 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1102 [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1103};
1104
1105static struct genl_family dp_datapath_genl_family = {
1106 .id = GENL_ID_GENERATE,
1107 .hdrsize = sizeof(struct ovs_header),
1108 .name = OVS_DATAPATH_FAMILY,
1109 .version = OVS_DATAPATH_VERSION,
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001110 .maxattr = OVS_DP_ATTR_MAX,
Pravin B Shelar3a4e0d62013-04-23 07:48:48 +00001111 .netnsok = true,
1112 .parallel_ops = true,
Jesse Grossccb13522011-10-25 19:26:31 -07001113};
1114
1115static struct genl_multicast_group ovs_dp_datapath_multicast_group = {
1116 .name = OVS_DATAPATH_MCGROUP
1117};
1118
Thomas Grafc3ff8cf2013-03-29 14:46:49 +01001119static size_t ovs_dp_cmd_msg_size(void)
1120{
1121 size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
1122
1123 msgsize += nla_total_size(IFNAMSIZ);
1124 msgsize += nla_total_size(sizeof(struct ovs_dp_stats));
1125
1126 return msgsize;
1127}
1128
Jesse Grossccb13522011-10-25 19:26:31 -07001129static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001130 u32 portid, u32 seq, u32 flags, u8 cmd)
Jesse Grossccb13522011-10-25 19:26:31 -07001131{
1132 struct ovs_header *ovs_header;
1133 struct ovs_dp_stats dp_stats;
1134 int err;
1135
Eric W. Biederman15e47302012-09-07 20:12:54 +00001136 ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
Jesse Grossccb13522011-10-25 19:26:31 -07001137 flags, cmd);
1138 if (!ovs_header)
1139 goto error;
1140
1141 ovs_header->dp_ifindex = get_dpifindex(dp);
1142
1143 rcu_read_lock();
1144 err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
1145 rcu_read_unlock();
1146 if (err)
1147 goto nla_put_failure;
1148
1149 get_dp_stats(dp, &dp_stats);
David S. Miller028d6a62012-03-29 23:20:48 -04001150 if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats), &dp_stats))
1151 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001152
1153 return genlmsg_end(skb, ovs_header);
1154
1155nla_put_failure:
1156 genlmsg_cancel(skb, ovs_header);
1157error:
1158 return -EMSGSIZE;
1159}
1160
Eric W. Biederman15e47302012-09-07 20:12:54 +00001161static struct sk_buff *ovs_dp_cmd_build_info(struct datapath *dp, u32 portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001162 u32 seq, u8 cmd)
1163{
1164 struct sk_buff *skb;
1165 int retval;
1166
Thomas Grafc3ff8cf2013-03-29 14:46:49 +01001167 skb = genlmsg_new(ovs_dp_cmd_msg_size(), GFP_KERNEL);
Jesse Grossccb13522011-10-25 19:26:31 -07001168 if (!skb)
1169 return ERR_PTR(-ENOMEM);
1170
Eric W. Biederman15e47302012-09-07 20:12:54 +00001171 retval = ovs_dp_cmd_fill_info(dp, skb, portid, seq, 0, cmd);
Jesse Grossccb13522011-10-25 19:26:31 -07001172 if (retval < 0) {
1173 kfree_skb(skb);
1174 return ERR_PTR(retval);
1175 }
1176 return skb;
1177}
1178
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001179/* Called with ovs_mutex. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001180static struct datapath *lookup_datapath(struct net *net,
1181 struct ovs_header *ovs_header,
Jesse Grossccb13522011-10-25 19:26:31 -07001182 struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1183{
1184 struct datapath *dp;
1185
1186 if (!a[OVS_DP_ATTR_NAME])
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001187 dp = get_dp(net, ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001188 else {
1189 struct vport *vport;
1190
1191 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001192 vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
Jesse Grossccb13522011-10-25 19:26:31 -07001193 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
1194 rcu_read_unlock();
1195 }
1196 return dp ? dp : ERR_PTR(-ENODEV);
1197}
1198
1199static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1200{
1201 struct nlattr **a = info->attrs;
1202 struct vport_parms parms;
1203 struct sk_buff *reply;
1204 struct datapath *dp;
1205 struct vport *vport;
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001206 struct ovs_net *ovs_net;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001207 int err, i;
Jesse Grossccb13522011-10-25 19:26:31 -07001208
1209 err = -EINVAL;
1210 if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1211 goto err;
1212
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001213 ovs_lock();
Jesse Grossccb13522011-10-25 19:26:31 -07001214
1215 err = -ENOMEM;
1216 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1217 if (dp == NULL)
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001218 goto err_unlock_ovs;
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001219
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001220 ovs_dp_set_net(dp, hold_net(sock_net(skb->sk)));
Jesse Grossccb13522011-10-25 19:26:31 -07001221
1222 /* Allocate table. */
1223 err = -ENOMEM;
1224 rcu_assign_pointer(dp->table, ovs_flow_tbl_alloc(TBL_MIN_BUCKETS));
1225 if (!dp->table)
1226 goto err_free_dp;
1227
1228 dp->stats_percpu = alloc_percpu(struct dp_stats_percpu);
1229 if (!dp->stats_percpu) {
1230 err = -ENOMEM;
1231 goto err_destroy_table;
1232 }
1233
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001234 dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
Pravin B Shelare6445712013-10-03 18:16:47 -07001235 GFP_KERNEL);
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001236 if (!dp->ports) {
1237 err = -ENOMEM;
1238 goto err_destroy_percpu;
1239 }
1240
1241 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1242 INIT_HLIST_HEAD(&dp->ports[i]);
1243
Jesse Grossccb13522011-10-25 19:26:31 -07001244 /* Set up our datapath device. */
1245 parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1246 parms.type = OVS_VPORT_TYPE_INTERNAL;
1247 parms.options = NULL;
1248 parms.dp = dp;
1249 parms.port_no = OVSP_LOCAL;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001250 parms.upcall_portid = nla_get_u32(a[OVS_DP_ATTR_UPCALL_PID]);
Jesse Grossccb13522011-10-25 19:26:31 -07001251
1252 vport = new_vport(&parms);
1253 if (IS_ERR(vport)) {
1254 err = PTR_ERR(vport);
1255 if (err == -EBUSY)
1256 err = -EEXIST;
1257
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001258 goto err_destroy_ports_array;
Jesse Grossccb13522011-10-25 19:26:31 -07001259 }
1260
Eric W. Biederman15e47302012-09-07 20:12:54 +00001261 reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001262 info->snd_seq, OVS_DP_CMD_NEW);
1263 err = PTR_ERR(reply);
1264 if (IS_ERR(reply))
1265 goto err_destroy_local_port;
1266
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001267 ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001268 list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001269
1270 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001271
Thomas Grafed6611852013-03-29 14:46:50 +01001272 ovs_notify(reply, info, &ovs_dp_datapath_multicast_group);
Jesse Grossccb13522011-10-25 19:26:31 -07001273 return 0;
1274
1275err_destroy_local_port:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001276 ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001277err_destroy_ports_array:
1278 kfree(dp->ports);
Jesse Grossccb13522011-10-25 19:26:31 -07001279err_destroy_percpu:
1280 free_percpu(dp->stats_percpu);
1281err_destroy_table:
Andy Zhou03f0d912013-08-07 20:01:00 -07001282 ovs_flow_tbl_destroy(ovsl_dereference(dp->table), false);
Jesse Grossccb13522011-10-25 19:26:31 -07001283err_free_dp:
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001284 release_net(ovs_dp_get_net(dp));
Jesse Grossccb13522011-10-25 19:26:31 -07001285 kfree(dp);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001286err_unlock_ovs:
1287 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001288err:
1289 return err;
1290}
1291
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001292/* Called with ovs_mutex. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001293static void __dp_destroy(struct datapath *dp)
Jesse Grossccb13522011-10-25 19:26:31 -07001294{
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001295 int i;
Jesse Grossccb13522011-10-25 19:26:31 -07001296
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001297 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1298 struct vport *vport;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001299 struct hlist_node *n;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001300
Sasha Levinb67bfe02013-02-27 17:06:00 -08001301 hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001302 if (vport->port_no != OVSP_LOCAL)
1303 ovs_dp_detach_port(vport);
1304 }
Jesse Grossccb13522011-10-25 19:26:31 -07001305
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001306 list_del_rcu(&dp->list_node);
Jesse Grossccb13522011-10-25 19:26:31 -07001307
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001308 /* OVSP_LOCAL is datapath internal port. We need to make sure that
1309 * all port in datapath are destroyed first before freeing datapath.
Jesse Grossccb13522011-10-25 19:26:31 -07001310 */
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001311 ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
Jesse Grossccb13522011-10-25 19:26:31 -07001312
1313 call_rcu(&dp->rcu, destroy_dp_rcu);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001314}
1315
1316static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1317{
1318 struct sk_buff *reply;
1319 struct datapath *dp;
1320 int err;
1321
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001322 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001323 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1324 err = PTR_ERR(dp);
1325 if (IS_ERR(dp))
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001326 goto unlock;
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001327
Eric W. Biederman15e47302012-09-07 20:12:54 +00001328 reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001329 info->snd_seq, OVS_DP_CMD_DEL);
1330 err = PTR_ERR(reply);
1331 if (IS_ERR(reply))
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001332 goto unlock;
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001333
1334 __dp_destroy(dp);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001335 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001336
Thomas Grafed6611852013-03-29 14:46:50 +01001337 ovs_notify(reply, info, &ovs_dp_datapath_multicast_group);
Jesse Grossccb13522011-10-25 19:26:31 -07001338
1339 return 0;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001340unlock:
1341 ovs_unlock();
1342 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001343}
1344
1345static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1346{
1347 struct sk_buff *reply;
1348 struct datapath *dp;
1349 int err;
1350
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001351 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001352 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001353 err = PTR_ERR(dp);
Jesse Grossccb13522011-10-25 19:26:31 -07001354 if (IS_ERR(dp))
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001355 goto unlock;
Jesse Grossccb13522011-10-25 19:26:31 -07001356
Eric W. Biederman15e47302012-09-07 20:12:54 +00001357 reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001358 info->snd_seq, OVS_DP_CMD_NEW);
1359 if (IS_ERR(reply)) {
1360 err = PTR_ERR(reply);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001361 netlink_set_err(sock_net(skb->sk)->genl_sock, 0,
Jesse Grossccb13522011-10-25 19:26:31 -07001362 ovs_dp_datapath_multicast_group.id, err);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001363 err = 0;
1364 goto unlock;
Jesse Grossccb13522011-10-25 19:26:31 -07001365 }
1366
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001367 ovs_unlock();
Thomas Grafed6611852013-03-29 14:46:50 +01001368 ovs_notify(reply, info, &ovs_dp_datapath_multicast_group);
Jesse Grossccb13522011-10-25 19:26:31 -07001369
1370 return 0;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001371unlock:
1372 ovs_unlock();
1373 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001374}
1375
1376static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1377{
1378 struct sk_buff *reply;
1379 struct datapath *dp;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001380 int err;
Jesse Grossccb13522011-10-25 19:26:31 -07001381
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001382 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001383 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001384 if (IS_ERR(dp)) {
1385 err = PTR_ERR(dp);
1386 goto unlock;
1387 }
Jesse Grossccb13522011-10-25 19:26:31 -07001388
Eric W. Biederman15e47302012-09-07 20:12:54 +00001389 reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001390 info->snd_seq, OVS_DP_CMD_NEW);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001391 if (IS_ERR(reply)) {
1392 err = PTR_ERR(reply);
1393 goto unlock;
1394 }
Jesse Grossccb13522011-10-25 19:26:31 -07001395
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001396 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001397 return genlmsg_reply(reply, info);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001398
1399unlock:
1400 ovs_unlock();
1401 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001402}
1403
1404static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1405{
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001406 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
Jesse Grossccb13522011-10-25 19:26:31 -07001407 struct datapath *dp;
1408 int skip = cb->args[0];
1409 int i = 0;
1410
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001411 rcu_read_lock();
1412 list_for_each_entry_rcu(dp, &ovs_net->dps, list_node) {
Ben Pfaff77676fd2012-01-17 13:33:39 +00001413 if (i >= skip &&
Eric W. Biederman15e47302012-09-07 20:12:54 +00001414 ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001415 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1416 OVS_DP_CMD_NEW) < 0)
1417 break;
1418 i++;
1419 }
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001420 rcu_read_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001421
1422 cb->args[0] = i;
1423
1424 return skb->len;
1425}
1426
1427static struct genl_ops dp_datapath_genl_ops[] = {
1428 { .cmd = OVS_DP_CMD_NEW,
1429 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1430 .policy = datapath_policy,
1431 .doit = ovs_dp_cmd_new
1432 },
1433 { .cmd = OVS_DP_CMD_DEL,
1434 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1435 .policy = datapath_policy,
1436 .doit = ovs_dp_cmd_del
1437 },
1438 { .cmd = OVS_DP_CMD_GET,
1439 .flags = 0, /* OK for unprivileged users. */
1440 .policy = datapath_policy,
1441 .doit = ovs_dp_cmd_get,
1442 .dumpit = ovs_dp_cmd_dump
1443 },
1444 { .cmd = OVS_DP_CMD_SET,
1445 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1446 .policy = datapath_policy,
1447 .doit = ovs_dp_cmd_set,
1448 },
1449};
1450
1451static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
1452 [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1453 [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
1454 [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
1455 [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
1456 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1457 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
1458};
1459
1460static struct genl_family dp_vport_genl_family = {
1461 .id = GENL_ID_GENERATE,
1462 .hdrsize = sizeof(struct ovs_header),
1463 .name = OVS_VPORT_FAMILY,
1464 .version = OVS_VPORT_VERSION,
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001465 .maxattr = OVS_VPORT_ATTR_MAX,
Pravin B Shelar3a4e0d62013-04-23 07:48:48 +00001466 .netnsok = true,
1467 .parallel_ops = true,
Jesse Grossccb13522011-10-25 19:26:31 -07001468};
1469
1470struct genl_multicast_group ovs_dp_vport_multicast_group = {
1471 .name = OVS_VPORT_MCGROUP
1472};
1473
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001474/* Called with ovs_mutex or RCU read lock. */
Jesse Grossccb13522011-10-25 19:26:31 -07001475static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001476 u32 portid, u32 seq, u32 flags, u8 cmd)
Jesse Grossccb13522011-10-25 19:26:31 -07001477{
1478 struct ovs_header *ovs_header;
1479 struct ovs_vport_stats vport_stats;
1480 int err;
1481
Eric W. Biederman15e47302012-09-07 20:12:54 +00001482 ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
Jesse Grossccb13522011-10-25 19:26:31 -07001483 flags, cmd);
1484 if (!ovs_header)
1485 return -EMSGSIZE;
1486
1487 ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1488
David S. Miller028d6a62012-03-29 23:20:48 -04001489 if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
1490 nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
1491 nla_put_string(skb, OVS_VPORT_ATTR_NAME, vport->ops->get_name(vport)) ||
Eric W. Biederman15e47302012-09-07 20:12:54 +00001492 nla_put_u32(skb, OVS_VPORT_ATTR_UPCALL_PID, vport->upcall_portid))
David S. Miller028d6a62012-03-29 23:20:48 -04001493 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001494
1495 ovs_vport_get_stats(vport, &vport_stats);
David S. Miller028d6a62012-03-29 23:20:48 -04001496 if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
1497 &vport_stats))
1498 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001499
1500 err = ovs_vport_get_options(vport, skb);
1501 if (err == -EMSGSIZE)
1502 goto error;
1503
1504 return genlmsg_end(skb, ovs_header);
1505
1506nla_put_failure:
1507 err = -EMSGSIZE;
1508error:
1509 genlmsg_cancel(skb, ovs_header);
1510 return err;
1511}
1512
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001513/* Called with ovs_mutex or RCU read lock. */
Eric W. Biederman15e47302012-09-07 20:12:54 +00001514struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001515 u32 seq, u8 cmd)
1516{
1517 struct sk_buff *skb;
1518 int retval;
1519
1520 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1521 if (!skb)
1522 return ERR_PTR(-ENOMEM);
1523
Eric W. Biederman15e47302012-09-07 20:12:54 +00001524 retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
Jesse Grossa9341512013-03-26 15:48:38 -07001525 BUG_ON(retval < 0);
1526
Jesse Grossccb13522011-10-25 19:26:31 -07001527 return skb;
1528}
1529
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001530/* Called with ovs_mutex or RCU read lock. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001531static struct vport *lookup_vport(struct net *net,
1532 struct ovs_header *ovs_header,
Jesse Grossccb13522011-10-25 19:26:31 -07001533 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1534{
1535 struct datapath *dp;
1536 struct vport *vport;
1537
1538 if (a[OVS_VPORT_ATTR_NAME]) {
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001539 vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
Jesse Grossccb13522011-10-25 19:26:31 -07001540 if (!vport)
1541 return ERR_PTR(-ENODEV);
Ben Pfaff651a68e2012-03-06 15:04:04 -08001542 if (ovs_header->dp_ifindex &&
1543 ovs_header->dp_ifindex != get_dpifindex(vport->dp))
1544 return ERR_PTR(-ENODEV);
Jesse Grossccb13522011-10-25 19:26:31 -07001545 return vport;
1546 } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
1547 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1548
1549 if (port_no >= DP_MAX_PORTS)
1550 return ERR_PTR(-EFBIG);
1551
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001552 dp = get_dp(net, ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001553 if (!dp)
1554 return ERR_PTR(-ENODEV);
1555
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001556 vport = ovs_vport_ovsl_rcu(dp, port_no);
Jesse Grossccb13522011-10-25 19:26:31 -07001557 if (!vport)
Jarno Rajahalme14408db2013-01-09 14:27:35 -08001558 return ERR_PTR(-ENODEV);
Jesse Grossccb13522011-10-25 19:26:31 -07001559 return vport;
1560 } else
1561 return ERR_PTR(-EINVAL);
1562}
1563
1564static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
1565{
1566 struct nlattr **a = info->attrs;
1567 struct ovs_header *ovs_header = info->userhdr;
1568 struct vport_parms parms;
1569 struct sk_buff *reply;
1570 struct vport *vport;
1571 struct datapath *dp;
1572 u32 port_no;
1573 int err;
1574
1575 err = -EINVAL;
1576 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
1577 !a[OVS_VPORT_ATTR_UPCALL_PID])
1578 goto exit;
1579
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001580 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001581 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001582 err = -ENODEV;
1583 if (!dp)
1584 goto exit_unlock;
1585
1586 if (a[OVS_VPORT_ATTR_PORT_NO]) {
1587 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1588
1589 err = -EFBIG;
1590 if (port_no >= DP_MAX_PORTS)
1591 goto exit_unlock;
1592
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001593 vport = ovs_vport_ovsl(dp, port_no);
Jesse Grossccb13522011-10-25 19:26:31 -07001594 err = -EBUSY;
1595 if (vport)
1596 goto exit_unlock;
1597 } else {
1598 for (port_no = 1; ; port_no++) {
1599 if (port_no >= DP_MAX_PORTS) {
1600 err = -EFBIG;
1601 goto exit_unlock;
1602 }
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001603 vport = ovs_vport_ovsl(dp, port_no);
Jesse Grossccb13522011-10-25 19:26:31 -07001604 if (!vport)
1605 break;
1606 }
1607 }
1608
1609 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
1610 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1611 parms.options = a[OVS_VPORT_ATTR_OPTIONS];
1612 parms.dp = dp;
1613 parms.port_no = port_no;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001614 parms.upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
Jesse Grossccb13522011-10-25 19:26:31 -07001615
1616 vport = new_vport(&parms);
1617 err = PTR_ERR(vport);
1618 if (IS_ERR(vport))
1619 goto exit_unlock;
1620
Rich Lanecb7c5bd2013-02-08 13:18:01 -08001621 err = 0;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001622 reply = ovs_vport_cmd_build_info(vport, info->snd_portid, info->snd_seq,
Jesse Grossccb13522011-10-25 19:26:31 -07001623 OVS_VPORT_CMD_NEW);
1624 if (IS_ERR(reply)) {
1625 err = PTR_ERR(reply);
1626 ovs_dp_detach_port(vport);
1627 goto exit_unlock;
1628 }
Thomas Grafed6611852013-03-29 14:46:50 +01001629
1630 ovs_notify(reply, info, &ovs_dp_vport_multicast_group);
Jesse Grossccb13522011-10-25 19:26:31 -07001631
1632exit_unlock:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001633 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001634exit:
1635 return err;
1636}
1637
1638static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
1639{
1640 struct nlattr **a = info->attrs;
1641 struct sk_buff *reply;
1642 struct vport *vport;
1643 int err;
1644
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001645 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001646 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
Jesse Grossccb13522011-10-25 19:26:31 -07001647 err = PTR_ERR(vport);
1648 if (IS_ERR(vport))
1649 goto exit_unlock;
1650
Jesse Grossccb13522011-10-25 19:26:31 -07001651 if (a[OVS_VPORT_ATTR_TYPE] &&
Jesse Grossf44f3402013-05-13 08:15:26 -07001652 nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
Jesse Grossccb13522011-10-25 19:26:31 -07001653 err = -EINVAL;
Jesse Grossf44f3402013-05-13 08:15:26 -07001654 goto exit_unlock;
1655 }
Jesse Grossccb13522011-10-25 19:26:31 -07001656
Jesse Grossa9341512013-03-26 15:48:38 -07001657 reply = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1658 if (!reply) {
1659 err = -ENOMEM;
1660 goto exit_unlock;
1661 }
1662
Jesse Grossf44f3402013-05-13 08:15:26 -07001663 if (a[OVS_VPORT_ATTR_OPTIONS]) {
Jesse Grossccb13522011-10-25 19:26:31 -07001664 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
Jesse Grossf44f3402013-05-13 08:15:26 -07001665 if (err)
1666 goto exit_free;
1667 }
Jesse Grossa9341512013-03-26 15:48:38 -07001668
Ansis Atteka03fbf8b2012-04-09 12:12:12 -07001669 if (a[OVS_VPORT_ATTR_UPCALL_PID])
Eric W. Biederman15e47302012-09-07 20:12:54 +00001670 vport->upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
Jesse Grossccb13522011-10-25 19:26:31 -07001671
Jesse Grossa9341512013-03-26 15:48:38 -07001672 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1673 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1674 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001675
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001676 ovs_unlock();
Thomas Grafed6611852013-03-29 14:46:50 +01001677 ovs_notify(reply, info, &ovs_dp_vport_multicast_group);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001678 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001679
Jesse Grossa9341512013-03-26 15:48:38 -07001680exit_free:
1681 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001682exit_unlock:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001683 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001684 return err;
1685}
1686
1687static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
1688{
1689 struct nlattr **a = info->attrs;
1690 struct sk_buff *reply;
1691 struct vport *vport;
1692 int err;
1693
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001694 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001695 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
Jesse Grossccb13522011-10-25 19:26:31 -07001696 err = PTR_ERR(vport);
1697 if (IS_ERR(vport))
1698 goto exit_unlock;
1699
1700 if (vport->port_no == OVSP_LOCAL) {
1701 err = -EINVAL;
1702 goto exit_unlock;
1703 }
1704
Pravin B Shelar74f84a52013-06-17 17:50:12 -07001705 reply = ovs_vport_cmd_build_info(vport, info->snd_portid,
1706 info->snd_seq, OVS_VPORT_CMD_DEL);
Jesse Grossccb13522011-10-25 19:26:31 -07001707 err = PTR_ERR(reply);
1708 if (IS_ERR(reply))
1709 goto exit_unlock;
1710
Rich Lane734907e2013-02-08 09:30:23 -08001711 err = 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001712 ovs_dp_detach_port(vport);
1713
Thomas Grafed6611852013-03-29 14:46:50 +01001714 ovs_notify(reply, info, &ovs_dp_vport_multicast_group);
Jesse Grossccb13522011-10-25 19:26:31 -07001715
1716exit_unlock:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001717 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001718 return err;
1719}
1720
1721static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
1722{
1723 struct nlattr **a = info->attrs;
1724 struct ovs_header *ovs_header = info->userhdr;
1725 struct sk_buff *reply;
1726 struct vport *vport;
1727 int err;
1728
1729 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001730 vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
Jesse Grossccb13522011-10-25 19:26:31 -07001731 err = PTR_ERR(vport);
1732 if (IS_ERR(vport))
1733 goto exit_unlock;
1734
Pravin B Shelar74f84a52013-06-17 17:50:12 -07001735 reply = ovs_vport_cmd_build_info(vport, info->snd_portid,
1736 info->snd_seq, OVS_VPORT_CMD_NEW);
Jesse Grossccb13522011-10-25 19:26:31 -07001737 err = PTR_ERR(reply);
1738 if (IS_ERR(reply))
1739 goto exit_unlock;
1740
1741 rcu_read_unlock();
1742
1743 return genlmsg_reply(reply, info);
1744
1745exit_unlock:
1746 rcu_read_unlock();
1747 return err;
1748}
1749
1750static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1751{
1752 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1753 struct datapath *dp;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001754 int bucket = cb->args[0], skip = cb->args[1];
1755 int i, j = 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001756
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001757 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001758 if (!dp)
1759 return -ENODEV;
1760
1761 rcu_read_lock();
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001762 for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
Jesse Grossccb13522011-10-25 19:26:31 -07001763 struct vport *vport;
1764
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001765 j = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001766 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001767 if (j >= skip &&
1768 ovs_vport_cmd_fill_info(vport, skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001769 NETLINK_CB(cb->skb).portid,
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001770 cb->nlh->nlmsg_seq,
1771 NLM_F_MULTI,
1772 OVS_VPORT_CMD_NEW) < 0)
1773 goto out;
Jesse Grossccb13522011-10-25 19:26:31 -07001774
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001775 j++;
1776 }
1777 skip = 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001778 }
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001779out:
Jesse Grossccb13522011-10-25 19:26:31 -07001780 rcu_read_unlock();
1781
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001782 cb->args[0] = i;
1783 cb->args[1] = j;
Jesse Grossccb13522011-10-25 19:26:31 -07001784
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001785 return skb->len;
Jesse Grossccb13522011-10-25 19:26:31 -07001786}
1787
Jesse Grossccb13522011-10-25 19:26:31 -07001788static struct genl_ops dp_vport_genl_ops[] = {
1789 { .cmd = OVS_VPORT_CMD_NEW,
1790 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1791 .policy = vport_policy,
1792 .doit = ovs_vport_cmd_new
1793 },
1794 { .cmd = OVS_VPORT_CMD_DEL,
1795 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1796 .policy = vport_policy,
1797 .doit = ovs_vport_cmd_del
1798 },
1799 { .cmd = OVS_VPORT_CMD_GET,
1800 .flags = 0, /* OK for unprivileged users. */
1801 .policy = vport_policy,
1802 .doit = ovs_vport_cmd_get,
1803 .dumpit = ovs_vport_cmd_dump
1804 },
1805 { .cmd = OVS_VPORT_CMD_SET,
1806 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1807 .policy = vport_policy,
1808 .doit = ovs_vport_cmd_set,
1809 },
1810};
1811
1812struct genl_family_and_ops {
1813 struct genl_family *family;
1814 struct genl_ops *ops;
1815 int n_ops;
1816 struct genl_multicast_group *group;
1817};
1818
1819static const struct genl_family_and_ops dp_genl_families[] = {
1820 { &dp_datapath_genl_family,
1821 dp_datapath_genl_ops, ARRAY_SIZE(dp_datapath_genl_ops),
1822 &ovs_dp_datapath_multicast_group },
1823 { &dp_vport_genl_family,
1824 dp_vport_genl_ops, ARRAY_SIZE(dp_vport_genl_ops),
1825 &ovs_dp_vport_multicast_group },
1826 { &dp_flow_genl_family,
1827 dp_flow_genl_ops, ARRAY_SIZE(dp_flow_genl_ops),
1828 &ovs_dp_flow_multicast_group },
1829 { &dp_packet_genl_family,
1830 dp_packet_genl_ops, ARRAY_SIZE(dp_packet_genl_ops),
1831 NULL },
1832};
1833
1834static void dp_unregister_genl(int n_families)
1835{
1836 int i;
1837
1838 for (i = 0; i < n_families; i++)
1839 genl_unregister_family(dp_genl_families[i].family);
1840}
1841
1842static int dp_register_genl(void)
1843{
1844 int n_registered;
1845 int err;
1846 int i;
1847
1848 n_registered = 0;
1849 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
1850 const struct genl_family_and_ops *f = &dp_genl_families[i];
1851
1852 err = genl_register_family_with_ops(f->family, f->ops,
1853 f->n_ops);
1854 if (err)
1855 goto error;
1856 n_registered++;
1857
1858 if (f->group) {
1859 err = genl_register_mc_group(f->family, f->group);
1860 if (err)
1861 goto error;
1862 }
1863 }
1864
1865 return 0;
1866
1867error:
1868 dp_unregister_genl(n_registered);
1869 return err;
1870}
1871
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001872static int __net_init ovs_init_net(struct net *net)
1873{
1874 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
1875
1876 INIT_LIST_HEAD(&ovs_net->dps);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001877 INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001878 return 0;
1879}
1880
1881static void __net_exit ovs_exit_net(struct net *net)
1882{
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001883 struct datapath *dp, *dp_next;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001884 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001885
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001886 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001887 list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
1888 __dp_destroy(dp);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001889 ovs_unlock();
1890
1891 cancel_work_sync(&ovs_net->dp_notify_work);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001892}
1893
1894static struct pernet_operations ovs_net_ops = {
1895 .init = ovs_init_net,
1896 .exit = ovs_exit_net,
1897 .id = &ovs_net_id,
1898 .size = sizeof(struct ovs_net),
1899};
1900
Jesse Grossccb13522011-10-25 19:26:31 -07001901static int __init dp_init(void)
1902{
Jesse Grossccb13522011-10-25 19:26:31 -07001903 int err;
1904
YOSHIFUJI Hideaki / 吉藤英明3523b292013-01-09 07:19:55 +00001905 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
Jesse Grossccb13522011-10-25 19:26:31 -07001906
1907 pr_info("Open vSwitch switching datapath\n");
1908
1909 err = ovs_flow_init();
1910 if (err)
1911 goto error;
1912
1913 err = ovs_vport_init();
1914 if (err)
1915 goto error_flow_exit;
1916
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001917 err = register_pernet_device(&ovs_net_ops);
Jesse Grossccb13522011-10-25 19:26:31 -07001918 if (err)
1919 goto error_vport_exit;
1920
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001921 err = register_netdevice_notifier(&ovs_dp_device_notifier);
1922 if (err)
1923 goto error_netns_exit;
1924
Jesse Grossccb13522011-10-25 19:26:31 -07001925 err = dp_register_genl();
1926 if (err < 0)
1927 goto error_unreg_notifier;
1928
Jesse Grossccb13522011-10-25 19:26:31 -07001929 return 0;
1930
1931error_unreg_notifier:
1932 unregister_netdevice_notifier(&ovs_dp_device_notifier);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001933error_netns_exit:
1934 unregister_pernet_device(&ovs_net_ops);
Jesse Grossccb13522011-10-25 19:26:31 -07001935error_vport_exit:
1936 ovs_vport_exit();
1937error_flow_exit:
1938 ovs_flow_exit();
1939error:
1940 return err;
1941}
1942
1943static void dp_cleanup(void)
1944{
Jesse Grossccb13522011-10-25 19:26:31 -07001945 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
1946 unregister_netdevice_notifier(&ovs_dp_device_notifier);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001947 unregister_pernet_device(&ovs_net_ops);
1948 rcu_barrier();
Jesse Grossccb13522011-10-25 19:26:31 -07001949 ovs_vport_exit();
1950 ovs_flow_exit();
1951}
1952
1953module_init(dp_init);
1954module_exit(dp_cleanup);
1955
1956MODULE_DESCRIPTION("Open vSwitch switching datapath");
1957MODULE_LICENSE("GPL");