blob: 6a9b0cb8a1dbd33932ab0d0c2a97de66802292d6 [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 Shelar8e4e1712013-04-15 13:23:03 -070062int ovs_net_id __read_mostly;
63
Johannes Berg68eb5502013-11-19 15:19:38 +010064static void ovs_notify(struct genl_family *family,
Johannes Berg2a94fe42013-11-19 15:19:39 +010065 struct sk_buff *skb, struct genl_info *info)
Thomas Grafed661182013-03-29 14:46:50 +010066{
Johannes Berg68eb5502013-11-19 15:19:38 +010067 genl_notify(family, skb, genl_info_net(info), info->snd_portid,
Johannes Berg2a94fe42013-11-19 15:19:39 +010068 0, info->nlhdr, GFP_KERNEL);
Thomas Grafed661182013-03-29 14:46:50 +010069}
70
Pravin B Shelar46df7b82012-02-22 19:58:59 -080071/**
Jesse Grossccb13522011-10-25 19:26:31 -070072 * DOC: Locking:
73 *
Pravin B Shelar8e4e1712013-04-15 13:23:03 -070074 * All writes e.g. Writes to device state (add/remove datapath, port, set
75 * operations on vports, etc.), Writes to other state (flow table
76 * modifications, set miscellaneous datapath parameters, etc.) are protected
77 * by ovs_lock.
Jesse Grossccb13522011-10-25 19:26:31 -070078 *
79 * Reads are protected by RCU.
80 *
81 * There are a few special cases (mostly stats) that have their own
82 * synchronization but they nest under all of above and don't interact with
83 * each other.
Pravin B Shelar8e4e1712013-04-15 13:23:03 -070084 *
85 * The RTNL lock nests inside ovs_mutex.
Jesse Grossccb13522011-10-25 19:26:31 -070086 */
87
Pravin B Shelar8e4e1712013-04-15 13:23:03 -070088static DEFINE_MUTEX(ovs_mutex);
89
90void ovs_lock(void)
91{
92 mutex_lock(&ovs_mutex);
93}
94
95void ovs_unlock(void)
96{
97 mutex_unlock(&ovs_mutex);
98}
99
100#ifdef CONFIG_LOCKDEP
101int lockdep_ovsl_is_held(void)
102{
103 if (debug_locks)
104 return lockdep_is_held(&ovs_mutex);
105 else
106 return 1;
107}
108#endif
109
Jesse Grossccb13522011-10-25 19:26:31 -0700110static struct vport *new_vport(const struct vport_parms *);
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800111static int queue_gso_packets(struct net *, int dp_ifindex, struct sk_buff *,
Jesse Grossccb13522011-10-25 19:26:31 -0700112 const struct dp_upcall_info *);
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800113static int queue_userspace_packet(struct net *, int dp_ifindex,
114 struct sk_buff *,
Jesse Grossccb13522011-10-25 19:26:31 -0700115 const struct dp_upcall_info *);
116
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700117/* Must be called with rcu_read_lock or ovs_mutex. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800118static struct datapath *get_dp(struct net *net, int dp_ifindex)
Jesse Grossccb13522011-10-25 19:26:31 -0700119{
120 struct datapath *dp = NULL;
121 struct net_device *dev;
122
123 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800124 dev = dev_get_by_index_rcu(net, dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -0700125 if (dev) {
126 struct vport *vport = ovs_internal_dev_get_vport(dev);
127 if (vport)
128 dp = vport->dp;
129 }
130 rcu_read_unlock();
131
132 return dp;
133}
134
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700135/* Must be called with rcu_read_lock or ovs_mutex. */
Jesse Grossccb13522011-10-25 19:26:31 -0700136const char *ovs_dp_name(const struct datapath *dp)
137{
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700138 struct vport *vport = ovs_vport_ovsl_rcu(dp, OVSP_LOCAL);
Jesse Grossccb13522011-10-25 19:26:31 -0700139 return vport->ops->get_name(vport);
140}
141
142static int get_dpifindex(struct datapath *dp)
143{
144 struct vport *local;
145 int ifindex;
146
147 rcu_read_lock();
148
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700149 local = ovs_vport_rcu(dp, OVSP_LOCAL);
Jesse Grossccb13522011-10-25 19:26:31 -0700150 if (local)
Thomas Grafcff63a52013-04-29 13:06:41 +0000151 ifindex = netdev_vport_priv(local)->dev->ifindex;
Jesse Grossccb13522011-10-25 19:26:31 -0700152 else
153 ifindex = 0;
154
155 rcu_read_unlock();
156
157 return ifindex;
158}
159
160static void destroy_dp_rcu(struct rcu_head *rcu)
161{
162 struct datapath *dp = container_of(rcu, struct datapath, rcu);
163
Pravin B Shelar618ed0c2013-10-04 00:17:42 -0700164 ovs_flow_tbl_destroy(&dp->table);
Jesse Grossccb13522011-10-25 19:26:31 -0700165 free_percpu(dp->stats_percpu);
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800166 release_net(ovs_dp_get_net(dp));
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700167 kfree(dp->ports);
Jesse Grossccb13522011-10-25 19:26:31 -0700168 kfree(dp);
169}
170
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700171static struct hlist_head *vport_hash_bucket(const struct datapath *dp,
172 u16 port_no)
173{
174 return &dp->ports[port_no & (DP_VPORT_HASH_BUCKETS - 1)];
175}
176
177struct vport *ovs_lookup_vport(const struct datapath *dp, u16 port_no)
178{
179 struct vport *vport;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700180 struct hlist_head *head;
181
182 head = vport_hash_bucket(dp, port_no);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800183 hlist_for_each_entry_rcu(vport, head, dp_hash_node) {
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700184 if (vport->port_no == port_no)
185 return vport;
186 }
187 return NULL;
188}
189
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700190/* Called with ovs_mutex. */
Jesse Grossccb13522011-10-25 19:26:31 -0700191static struct vport *new_vport(const struct vport_parms *parms)
192{
193 struct vport *vport;
194
195 vport = ovs_vport_add(parms);
196 if (!IS_ERR(vport)) {
197 struct datapath *dp = parms->dp;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700198 struct hlist_head *head = vport_hash_bucket(dp, vport->port_no);
Jesse Grossccb13522011-10-25 19:26:31 -0700199
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700200 hlist_add_head_rcu(&vport->dp_hash_node, head);
Jesse Grossccb13522011-10-25 19:26:31 -0700201 }
Jesse Grossccb13522011-10-25 19:26:31 -0700202 return vport;
203}
204
Jesse Grossccb13522011-10-25 19:26:31 -0700205void ovs_dp_detach_port(struct vport *p)
206{
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700207 ASSERT_OVSL();
Jesse Grossccb13522011-10-25 19:26:31 -0700208
209 /* First drop references to device. */
Pravin B Shelar15eac2a2012-08-23 12:40:54 -0700210 hlist_del_rcu(&p->dp_hash_node);
Jesse Grossccb13522011-10-25 19:26:31 -0700211
212 /* Then destroy it. */
213 ovs_vport_del(p);
214}
215
216/* Must be called with rcu_read_lock. */
217void ovs_dp_process_received_packet(struct vport *p, struct sk_buff *skb)
218{
219 struct datapath *dp = p->dp;
220 struct sw_flow *flow;
221 struct dp_stats_percpu *stats;
222 struct sw_flow_key key;
223 u64 *stats_counter;
Andy Zhou1bd71162013-10-22 10:42:46 -0700224 u32 n_mask_hit;
Jesse Grossccb13522011-10-25 19:26:31 -0700225 int error;
Jesse Grossccb13522011-10-25 19:26:31 -0700226
Shan Wei404f2f12012-11-13 09:52:25 +0800227 stats = this_cpu_ptr(dp->stats_percpu);
Jesse Grossccb13522011-10-25 19:26:31 -0700228
229 /* Extract flow from 'skb' into 'key'. */
Andy Zhou03f0d912013-08-07 20:01:00 -0700230 error = ovs_flow_extract(skb, p->port_no, &key);
Jesse Grossccb13522011-10-25 19:26:31 -0700231 if (unlikely(error)) {
232 kfree_skb(skb);
233 return;
234 }
235
236 /* Look up flow. */
Andy Zhou5bb50632013-11-25 10:42:46 -0800237 flow = ovs_flow_tbl_lookup_stats(&dp->table, &key, &n_mask_hit);
Jesse Grossccb13522011-10-25 19:26:31 -0700238 if (unlikely(!flow)) {
239 struct dp_upcall_info upcall;
240
241 upcall.cmd = OVS_PACKET_CMD_MISS;
242 upcall.key = &key;
243 upcall.userdata = NULL;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000244 upcall.portid = p->upcall_portid;
Jesse Grossccb13522011-10-25 19:26:31 -0700245 ovs_dp_upcall(dp, skb, &upcall);
246 consume_skb(skb);
247 stats_counter = &stats->n_missed;
248 goto out;
249 }
250
251 OVS_CB(skb)->flow = flow;
Andy Zhou03f0d912013-08-07 20:01:00 -0700252 OVS_CB(skb)->pkt_key = &key;
Jesse Grossccb13522011-10-25 19:26:31 -0700253
Pravin B Shelare298e502013-10-29 17:22:21 -0700254 ovs_flow_stats_update(OVS_CB(skb)->flow, skb);
Jesse Grossccb13522011-10-25 19:26:31 -0700255 ovs_execute_actions(dp, skb);
Pravin B Shelare298e502013-10-29 17:22:21 -0700256 stats_counter = &stats->n_hit;
Jesse Grossccb13522011-10-25 19:26:31 -0700257
258out:
259 /* Update datapath statistics. */
260 u64_stats_update_begin(&stats->sync);
261 (*stats_counter)++;
Andy Zhou1bd71162013-10-22 10:42:46 -0700262 stats->n_mask_hit += n_mask_hit;
Jesse Grossccb13522011-10-25 19:26:31 -0700263 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;
Thomas Graf795449d2013-11-30 13:21:32 +0100405 struct genl_info info = {
406 .dst_sk = net->genl_sock,
407 .snd_portid = upcall_info->portid,
408 };
409 size_t len;
Jesse Grossccb13522011-10-25 19:26:31 -0700410 int err;
411
412 if (vlan_tx_tag_present(skb)) {
413 nskb = skb_clone(skb, GFP_ATOMIC);
414 if (!nskb)
415 return -ENOMEM;
416
Patrick McHardy86a9bad2013-04-19 02:04:30 +0000417 nskb = __vlan_put_tag(nskb, nskb->vlan_proto, vlan_tx_tag_get(nskb));
Dan Carpenter8aa51d62012-05-13 08:44:18 +0000418 if (!nskb)
Jesse Grossccb13522011-10-25 19:26:31 -0700419 return -ENOMEM;
420
421 nskb->vlan_tci = 0;
422 skb = nskb;
423 }
424
425 if (nla_attr_size(skb->len) > USHRT_MAX) {
426 err = -EFBIG;
427 goto out;
428 }
429
Thomas Graf795449d2013-11-30 13:21:32 +0100430 len = upcall_msg_size(skb, upcall_info->userdata);
431 user_skb = genlmsg_new_unicast(len, &info, GFP_ATOMIC);
Jesse Grossccb13522011-10-25 19:26:31 -0700432 if (!user_skb) {
433 err = -ENOMEM;
434 goto out;
435 }
436
437 upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
438 0, upcall_info->cmd);
439 upcall->dp_ifindex = dp_ifindex;
440
441 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
Pravin B Shelare6445712013-10-03 18:16:47 -0700442 ovs_nla_put_flow(upcall_info->key, upcall_info->key, user_skb);
Jesse Grossccb13522011-10-25 19:26:31 -0700443 nla_nest_end(user_skb, nla);
444
445 if (upcall_info->userdata)
Ben Pfaff44901082013-02-15 17:29:22 -0800446 __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA,
447 nla_len(upcall_info->userdata),
448 nla_data(upcall_info->userdata));
Jesse Grossccb13522011-10-25 19:26:31 -0700449
450 nla = __nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, skb->len);
451
452 skb_copy_and_csum_dev(skb, nla_data(nla));
453
Rich Lanea15ff762013-02-15 11:07:43 -0800454 genlmsg_end(user_skb, upcall);
Eric W. Biederman15e47302012-09-07 20:12:54 +0000455 err = genlmsg_unicast(net, user_skb, upcall_info->portid);
Jesse Grossccb13522011-10-25 19:26:31 -0700456
457out:
458 kfree_skb(nskb);
459 return err;
460}
461
Jesse Grossccb13522011-10-25 19:26:31 -0700462static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
463{
464 struct ovs_header *ovs_header = info->userhdr;
465 struct nlattr **a = info->attrs;
466 struct sw_flow_actions *acts;
467 struct sk_buff *packet;
468 struct sw_flow *flow;
469 struct datapath *dp;
470 struct ethhdr *eth;
471 int len;
472 int err;
Jesse Grossccb13522011-10-25 19:26:31 -0700473
474 err = -EINVAL;
475 if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
Thomas Grafdded45fc2013-03-29 14:46:47 +0100476 !a[OVS_PACKET_ATTR_ACTIONS])
Jesse Grossccb13522011-10-25 19:26:31 -0700477 goto err;
478
479 len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
480 packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
481 err = -ENOMEM;
482 if (!packet)
483 goto err;
484 skb_reserve(packet, NET_IP_ALIGN);
485
Thomas Graf32686a92013-03-29 14:46:48 +0100486 nla_memcpy(__skb_put(packet, len), a[OVS_PACKET_ATTR_PACKET], len);
Jesse Grossccb13522011-10-25 19:26:31 -0700487
488 skb_reset_mac_header(packet);
489 eth = eth_hdr(packet);
490
491 /* Normally, setting the skb 'protocol' field would be handled by a
492 * call to eth_type_trans(), but it assumes there's a sending
493 * device, which we may not have. */
Simon Hormane5c5d222013-03-28 13:38:25 +0900494 if (ntohs(eth->h_proto) >= ETH_P_802_3_MIN)
Jesse Grossccb13522011-10-25 19:26:31 -0700495 packet->protocol = eth->h_proto;
496 else
497 packet->protocol = htons(ETH_P_802_2);
498
499 /* Build an sw_flow for sending this packet. */
Pravin B Shelare298e502013-10-29 17:22:21 -0700500 flow = ovs_flow_alloc(false);
Jesse Grossccb13522011-10-25 19:26:31 -0700501 err = PTR_ERR(flow);
502 if (IS_ERR(flow))
503 goto err_kfree_skb;
504
Andy Zhou03f0d912013-08-07 20:01:00 -0700505 err = ovs_flow_extract(packet, -1, &flow->key);
Jesse Grossccb13522011-10-25 19:26:31 -0700506 if (err)
507 goto err_flow_free;
508
Pravin B Shelare6445712013-10-03 18:16:47 -0700509 err = ovs_nla_get_flow_metadata(flow, a[OVS_PACKET_ATTR_KEY]);
Jesse Grossccb13522011-10-25 19:26:31 -0700510 if (err)
511 goto err_flow_free;
Pravin B Shelare6445712013-10-03 18:16:47 -0700512 acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_PACKET_ATTR_ACTIONS]));
Jesse Grossccb13522011-10-25 19:26:31 -0700513 err = PTR_ERR(acts);
514 if (IS_ERR(acts))
515 goto err_flow_free;
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700516
Pravin B Shelare6445712013-10-03 18:16:47 -0700517 err = ovs_nla_copy_actions(a[OVS_PACKET_ATTR_ACTIONS],
518 &flow->key, 0, &acts);
Jesse Grossccb13522011-10-25 19:26:31 -0700519 rcu_assign_pointer(flow->sf_acts, acts);
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700520 if (err)
521 goto err_flow_free;
Jesse Grossccb13522011-10-25 19:26:31 -0700522
523 OVS_CB(packet)->flow = flow;
Andy Zhou03f0d912013-08-07 20:01:00 -0700524 OVS_CB(packet)->pkt_key = &flow->key;
Jesse Grossccb13522011-10-25 19:26:31 -0700525 packet->priority = flow->key.phy.priority;
Ansis Atteka39c7caeb2012-11-26 11:24:11 -0800526 packet->mark = flow->key.phy.skb_mark;
Jesse Grossccb13522011-10-25 19:26:31 -0700527
528 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800529 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -0700530 err = -ENODEV;
531 if (!dp)
532 goto err_unlock;
533
534 local_bh_disable();
535 err = ovs_execute_actions(dp, packet);
536 local_bh_enable();
537 rcu_read_unlock();
538
Andy Zhou03f0d912013-08-07 20:01:00 -0700539 ovs_flow_free(flow, false);
Jesse Grossccb13522011-10-25 19:26:31 -0700540 return err;
541
542err_unlock:
543 rcu_read_unlock();
544err_flow_free:
Andy Zhou03f0d912013-08-07 20:01:00 -0700545 ovs_flow_free(flow, false);
Jesse Grossccb13522011-10-25 19:26:31 -0700546err_kfree_skb:
547 kfree_skb(packet);
548err:
549 return err;
550}
551
552static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
Thomas Grafdded45fc2013-03-29 14:46:47 +0100553 [OVS_PACKET_ATTR_PACKET] = { .len = ETH_HLEN },
Jesse Grossccb13522011-10-25 19:26:31 -0700554 [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
555 [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
556};
557
Johannes Berg4534de82013-11-14 17:14:46 +0100558static const struct genl_ops dp_packet_genl_ops[] = {
Jesse Grossccb13522011-10-25 19:26:31 -0700559 { .cmd = OVS_PACKET_CMD_EXECUTE,
560 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
561 .policy = packet_policy,
562 .doit = ovs_packet_cmd_execute
563 }
564};
565
Andy Zhou1bd71162013-10-22 10:42:46 -0700566static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats,
567 struct ovs_dp_megaflow_stats *mega_stats)
Jesse Grossccb13522011-10-25 19:26:31 -0700568{
569 int i;
Jesse Grossccb13522011-10-25 19:26:31 -0700570
Andy Zhou1bd71162013-10-22 10:42:46 -0700571 memset(mega_stats, 0, sizeof(*mega_stats));
572
Pravin B Shelarb637e492013-10-04 00:14:23 -0700573 stats->n_flows = ovs_flow_tbl_count(&dp->table);
Andy Zhou1bd71162013-10-22 10:42:46 -0700574 mega_stats->n_masks = ovs_flow_tbl_num_masks(&dp->table);
Jesse Grossccb13522011-10-25 19:26:31 -0700575
576 stats->n_hit = stats->n_missed = stats->n_lost = 0;
Andy Zhou1bd71162013-10-22 10:42:46 -0700577
Jesse Grossccb13522011-10-25 19:26:31 -0700578 for_each_possible_cpu(i) {
579 const struct dp_stats_percpu *percpu_stats;
580 struct dp_stats_percpu local_stats;
581 unsigned int start;
582
583 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
584
585 do {
586 start = u64_stats_fetch_begin_bh(&percpu_stats->sync);
587 local_stats = *percpu_stats;
588 } while (u64_stats_fetch_retry_bh(&percpu_stats->sync, start));
589
590 stats->n_hit += local_stats.n_hit;
591 stats->n_missed += local_stats.n_missed;
592 stats->n_lost += local_stats.n_lost;
Andy Zhou1bd71162013-10-22 10:42:46 -0700593 mega_stats->n_mask_hit += local_stats.n_mask_hit;
Jesse Grossccb13522011-10-25 19:26:31 -0700594 }
595}
596
597static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
598 [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
599 [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
600 [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
601};
602
603static struct genl_family dp_flow_genl_family = {
604 .id = GENL_ID_GENERATE,
605 .hdrsize = sizeof(struct ovs_header),
606 .name = OVS_FLOW_FAMILY,
607 .version = OVS_FLOW_VERSION,
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800608 .maxattr = OVS_FLOW_ATTR_MAX,
Pravin B Shelar3a4e0d62013-04-23 07:48:48 +0000609 .netnsok = true,
610 .parallel_ops = true,
Jesse Grossccb13522011-10-25 19:26:31 -0700611};
612
613static struct genl_multicast_group ovs_dp_flow_multicast_group = {
614 .name = OVS_FLOW_MCGROUP
615};
616
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100617static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts)
618{
619 return NLMSG_ALIGN(sizeof(struct ovs_header))
620 + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_KEY */
Andy Zhou03f0d912013-08-07 20:01:00 -0700621 + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_MASK */
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100622 + nla_total_size(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
623 + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
624 + nla_total_size(8) /* OVS_FLOW_ATTR_USED */
625 + nla_total_size(acts->actions_len); /* OVS_FLOW_ATTR_ACTIONS */
626}
627
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700628/* Called with ovs_mutex. */
Jesse Grossccb13522011-10-25 19:26:31 -0700629static int ovs_flow_cmd_fill_info(struct sw_flow *flow, struct datapath *dp,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000630 struct sk_buff *skb, u32 portid,
Jesse Grossccb13522011-10-25 19:26:31 -0700631 u32 seq, u32 flags, u8 cmd)
632{
633 const int skb_orig_len = skb->len;
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700634 struct nlattr *start;
Jesse Grossccb13522011-10-25 19:26:31 -0700635 struct ovs_flow_stats stats;
Pravin B Shelare298e502013-10-29 17:22:21 -0700636 __be16 tcp_flags;
637 unsigned long used;
Jesse Grossccb13522011-10-25 19:26:31 -0700638 struct ovs_header *ovs_header;
639 struct nlattr *nla;
Jesse Grossccb13522011-10-25 19:26:31 -0700640 int err;
641
Eric W. Biederman15e47302012-09-07 20:12:54 +0000642 ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family, flags, cmd);
Jesse Grossccb13522011-10-25 19:26:31 -0700643 if (!ovs_header)
644 return -EMSGSIZE;
645
646 ovs_header->dp_ifindex = get_dpifindex(dp);
647
Andy Zhou03f0d912013-08-07 20:01:00 -0700648 /* Fill flow key. */
Jesse Grossccb13522011-10-25 19:26:31 -0700649 nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
650 if (!nla)
651 goto nla_put_failure;
Andy Zhou03f0d912013-08-07 20:01:00 -0700652
Pravin B Shelare6445712013-10-03 18:16:47 -0700653 err = ovs_nla_put_flow(&flow->unmasked_key, &flow->unmasked_key, skb);
Jesse Grossccb13522011-10-25 19:26:31 -0700654 if (err)
655 goto error;
656 nla_nest_end(skb, nla);
657
Andy Zhou03f0d912013-08-07 20:01:00 -0700658 nla = nla_nest_start(skb, OVS_FLOW_ATTR_MASK);
659 if (!nla)
660 goto nla_put_failure;
661
Pravin B Shelare6445712013-10-03 18:16:47 -0700662 err = ovs_nla_put_flow(&flow->key, &flow->mask->key, skb);
Andy Zhou03f0d912013-08-07 20:01:00 -0700663 if (err)
664 goto error;
665
666 nla_nest_end(skb, nla);
667
Pravin B Shelare298e502013-10-29 17:22:21 -0700668 ovs_flow_stats_get(flow, &stats, &used, &tcp_flags);
David S. Miller028d6a62012-03-29 23:20:48 -0400669 if (used &&
670 nla_put_u64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used)))
671 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -0700672
David S. Miller028d6a62012-03-29 23:20:48 -0400673 if (stats.n_packets &&
Pravin B Shelare298e502013-10-29 17:22:21 -0700674 nla_put(skb, OVS_FLOW_ATTR_STATS, sizeof(struct ovs_flow_stats), &stats))
David S. Miller028d6a62012-03-29 23:20:48 -0400675 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -0700676
Pravin B Shelare298e502013-10-29 17:22:21 -0700677 if ((u8)ntohs(tcp_flags) &&
678 nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, (u8)ntohs(tcp_flags)))
David S. Miller028d6a62012-03-29 23:20:48 -0400679 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -0700680
681 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
682 * this is the first flow to be dumped into 'skb'. This is unusual for
683 * Netlink but individual action lists can be longer than
684 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
685 * The userspace caller can always fetch the actions separately if it
686 * really wants them. (Most userspace callers in fact don't care.)
687 *
688 * This can only fail for dump operations because the skb is always
689 * properly sized for single flows.
690 */
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700691 start = nla_nest_start(skb, OVS_FLOW_ATTR_ACTIONS);
692 if (start) {
Pravin B Shelard57170b2013-07-30 15:39:39 -0700693 const struct sw_flow_actions *sf_acts;
694
Jesse Gross663efa32013-12-03 10:58:53 -0800695 sf_acts = rcu_dereference_ovsl(flow->sf_acts);
Pravin B Shelard57170b2013-07-30 15:39:39 -0700696
Pravin B Shelare6445712013-10-03 18:16:47 -0700697 err = ovs_nla_put_actions(sf_acts->actions,
698 sf_acts->actions_len, skb);
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700699 if (!err)
700 nla_nest_end(skb, start);
701 else {
702 if (skb_orig_len)
703 goto error;
704
705 nla_nest_cancel(skb, start);
706 }
707 } else if (skb_orig_len)
708 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -0700709
710 return genlmsg_end(skb, ovs_header);
711
712nla_put_failure:
713 err = -EMSGSIZE;
714error:
715 genlmsg_cancel(skb, ovs_header);
716 return err;
717}
718
Thomas Graf795449d2013-11-30 13:21:32 +0100719static struct sk_buff *ovs_flow_cmd_alloc_info(struct sw_flow *flow,
720 struct genl_info *info)
Jesse Grossccb13522011-10-25 19:26:31 -0700721{
Thomas Graf795449d2013-11-30 13:21:32 +0100722 size_t len;
Jesse Grossccb13522011-10-25 19:26:31 -0700723
Thomas Graf795449d2013-11-30 13:21:32 +0100724 len = ovs_flow_cmd_msg_size(ovsl_dereference(flow->sf_acts));
Jesse Grossccb13522011-10-25 19:26:31 -0700725
Thomas Graf795449d2013-11-30 13:21:32 +0100726 return genlmsg_new_unicast(len, info, GFP_KERNEL);
Jesse Grossccb13522011-10-25 19:26:31 -0700727}
728
729static struct sk_buff *ovs_flow_cmd_build_info(struct sw_flow *flow,
730 struct datapath *dp,
Thomas Graf795449d2013-11-30 13:21:32 +0100731 struct genl_info *info,
732 u8 cmd)
Jesse Grossccb13522011-10-25 19:26:31 -0700733{
734 struct sk_buff *skb;
735 int retval;
736
Thomas Graf795449d2013-11-30 13:21:32 +0100737 skb = ovs_flow_cmd_alloc_info(flow, info);
Jesse Grossccb13522011-10-25 19:26:31 -0700738 if (!skb)
739 return ERR_PTR(-ENOMEM);
740
Thomas Graf795449d2013-11-30 13:21:32 +0100741 retval = ovs_flow_cmd_fill_info(flow, dp, skb, info->snd_portid,
742 info->snd_seq, 0, cmd);
Jesse Grossccb13522011-10-25 19:26:31 -0700743 BUG_ON(retval < 0);
744 return skb;
745}
746
747static int ovs_flow_cmd_new_or_set(struct sk_buff *skb, struct genl_info *info)
748{
749 struct nlattr **a = info->attrs;
750 struct ovs_header *ovs_header = info->userhdr;
Andy Zhou03f0d912013-08-07 20:01:00 -0700751 struct sw_flow_key key, masked_key;
752 struct sw_flow *flow = NULL;
753 struct sw_flow_mask mask;
Jesse Grossccb13522011-10-25 19:26:31 -0700754 struct sk_buff *reply;
755 struct datapath *dp;
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700756 struct sw_flow_actions *acts = NULL;
Andy Zhou03f0d912013-08-07 20:01:00 -0700757 struct sw_flow_match match;
Pravin B Shelare298e502013-10-29 17:22:21 -0700758 bool exact_5tuple;
Jesse Grossccb13522011-10-25 19:26:31 -0700759 int error;
Jesse Grossccb13522011-10-25 19:26:31 -0700760
761 /* Extract key. */
762 error = -EINVAL;
763 if (!a[OVS_FLOW_ATTR_KEY])
764 goto error;
Andy Zhou03f0d912013-08-07 20:01:00 -0700765
766 ovs_match_init(&match, &key, &mask);
Pravin B Shelare298e502013-10-29 17:22:21 -0700767 error = ovs_nla_get_match(&match, &exact_5tuple,
Pravin B Shelare6445712013-10-03 18:16:47 -0700768 a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
Jesse Grossccb13522011-10-25 19:26:31 -0700769 if (error)
770 goto error;
771
772 /* Validate actions. */
773 if (a[OVS_FLOW_ATTR_ACTIONS]) {
Pravin B Shelare6445712013-10-03 18:16:47 -0700774 acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_FLOW_ATTR_ACTIONS]));
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700775 error = PTR_ERR(acts);
776 if (IS_ERR(acts))
Jesse Grossccb13522011-10-25 19:26:31 -0700777 goto error;
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700778
Pravin B Shelare6445712013-10-03 18:16:47 -0700779 ovs_flow_mask_key(&masked_key, &key, &mask);
780 error = ovs_nla_copy_actions(a[OVS_FLOW_ATTR_ACTIONS],
781 &masked_key, 0, &acts);
Andy Zhou03f0d912013-08-07 20:01:00 -0700782 if (error) {
783 OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700784 goto err_kfree;
Andy Zhou03f0d912013-08-07 20:01:00 -0700785 }
Jesse Grossccb13522011-10-25 19:26:31 -0700786 } else if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW) {
787 error = -EINVAL;
788 goto error;
789 }
790
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700791 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800792 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -0700793 error = -ENODEV;
794 if (!dp)
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700795 goto err_unlock_ovs;
Jesse Grossccb13522011-10-25 19:26:31 -0700796
Andy Zhou03f0d912013-08-07 20:01:00 -0700797 /* Check if this is a duplicate flow */
Andy Zhou5bb50632013-11-25 10:42:46 -0800798 flow = ovs_flow_tbl_lookup(&dp->table, &key);
Jesse Grossccb13522011-10-25 19:26:31 -0700799 if (!flow) {
Jesse Grossccb13522011-10-25 19:26:31 -0700800 /* Bail out if we're not allowed to create a new flow. */
801 error = -ENOENT;
802 if (info->genlhdr->cmd == OVS_FLOW_CMD_SET)
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700803 goto err_unlock_ovs;
Jesse Grossccb13522011-10-25 19:26:31 -0700804
Jesse Grossccb13522011-10-25 19:26:31 -0700805 /* Allocate flow. */
Pravin B Shelare298e502013-10-29 17:22:21 -0700806 flow = ovs_flow_alloc(!exact_5tuple);
Jesse Grossccb13522011-10-25 19:26:31 -0700807 if (IS_ERR(flow)) {
808 error = PTR_ERR(flow);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700809 goto err_unlock_ovs;
Jesse Grossccb13522011-10-25 19:26:31 -0700810 }
Jesse Grossccb13522011-10-25 19:26:31 -0700811
Andy Zhou03f0d912013-08-07 20:01:00 -0700812 flow->key = masked_key;
813 flow->unmasked_key = key;
Jesse Grossccb13522011-10-25 19:26:31 -0700814 rcu_assign_pointer(flow->sf_acts, acts);
815
816 /* Put flow in bucket. */
Pravin B Shelar618ed0c2013-10-04 00:17:42 -0700817 error = ovs_flow_tbl_insert(&dp->table, flow, &mask);
818 if (error) {
819 acts = NULL;
820 goto err_flow_free;
821 }
Jesse Grossccb13522011-10-25 19:26:31 -0700822
Thomas Graf795449d2013-11-30 13:21:32 +0100823 reply = ovs_flow_cmd_build_info(flow, dp, info, OVS_FLOW_CMD_NEW);
Jesse Grossccb13522011-10-25 19:26:31 -0700824 } else {
825 /* We found a matching flow. */
826 struct sw_flow_actions *old_acts;
Jesse Grossccb13522011-10-25 19:26:31 -0700827
828 /* Bail out if we're not allowed to modify an existing flow.
829 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
830 * because Generic Netlink treats the latter as a dump
831 * request. We also accept NLM_F_EXCL in case that bug ever
832 * gets fixed.
833 */
834 error = -EEXIST;
835 if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW &&
836 info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL))
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700837 goto err_unlock_ovs;
Jesse Grossccb13522011-10-25 19:26:31 -0700838
Andy Zhou03f0d912013-08-07 20:01:00 -0700839 /* The unmasked key has to be the same for flow updates. */
840 error = -EINVAL;
Pravin B Shelare6445712013-10-03 18:16:47 -0700841 if (!ovs_flow_cmp_unmasked_key(flow, &match)) {
Andy Zhou03f0d912013-08-07 20:01:00 -0700842 OVS_NLERR("Flow modification message rejected, unmasked key does not match.\n");
843 goto err_unlock_ovs;
844 }
845
Jesse Grossccb13522011-10-25 19:26:31 -0700846 /* Update actions. */
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700847 old_acts = ovsl_dereference(flow->sf_acts);
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700848 rcu_assign_pointer(flow->sf_acts, acts);
Pravin B Shelare6445712013-10-03 18:16:47 -0700849 ovs_nla_free_flow_actions(old_acts);
Jesse Grossccb13522011-10-25 19:26:31 -0700850
Thomas Graf795449d2013-11-30 13:21:32 +0100851 reply = ovs_flow_cmd_build_info(flow, dp, info, OVS_FLOW_CMD_NEW);
Jesse Grossccb13522011-10-25 19:26:31 -0700852
853 /* Clear stats. */
Pravin B Shelare298e502013-10-29 17:22:21 -0700854 if (a[OVS_FLOW_ATTR_CLEAR])
855 ovs_flow_stats_clear(flow);
Jesse Grossccb13522011-10-25 19:26:31 -0700856 }
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700857 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -0700858
859 if (!IS_ERR(reply))
Johannes Berg2a94fe42013-11-19 15:19:39 +0100860 ovs_notify(&dp_flow_genl_family, reply, info);
Jesse Grossccb13522011-10-25 19:26:31 -0700861 else
Johannes Berg68eb5502013-11-19 15:19:38 +0100862 genl_set_err(&dp_flow_genl_family, sock_net(skb->sk), 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +0100863 0, PTR_ERR(reply));
Jesse Grossccb13522011-10-25 19:26:31 -0700864 return 0;
865
Andy Zhou03f0d912013-08-07 20:01:00 -0700866err_flow_free:
867 ovs_flow_free(flow, false);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700868err_unlock_ovs:
869 ovs_unlock();
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700870err_kfree:
871 kfree(acts);
Jesse Grossccb13522011-10-25 19:26:31 -0700872error:
873 return error;
874}
875
876static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
877{
878 struct nlattr **a = info->attrs;
879 struct ovs_header *ovs_header = info->userhdr;
880 struct sw_flow_key key;
881 struct sk_buff *reply;
882 struct sw_flow *flow;
883 struct datapath *dp;
Andy Zhou03f0d912013-08-07 20:01:00 -0700884 struct sw_flow_match match;
Jesse Grossccb13522011-10-25 19:26:31 -0700885 int err;
Jesse Grossccb13522011-10-25 19:26:31 -0700886
Andy Zhou03f0d912013-08-07 20:01:00 -0700887 if (!a[OVS_FLOW_ATTR_KEY]) {
888 OVS_NLERR("Flow get message rejected, Key attribute missing.\n");
Jesse Grossccb13522011-10-25 19:26:31 -0700889 return -EINVAL;
Andy Zhou03f0d912013-08-07 20:01:00 -0700890 }
891
892 ovs_match_init(&match, &key, NULL);
Pravin B Shelare298e502013-10-29 17:22:21 -0700893 err = ovs_nla_get_match(&match, NULL, a[OVS_FLOW_ATTR_KEY], NULL);
Jesse Grossccb13522011-10-25 19:26:31 -0700894 if (err)
895 return err;
896
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700897 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800898 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700899 if (!dp) {
900 err = -ENODEV;
901 goto unlock;
902 }
Jesse Grossccb13522011-10-25 19:26:31 -0700903
Andy Zhou5bb50632013-11-25 10:42:46 -0800904 flow = ovs_flow_tbl_lookup(&dp->table, &key);
Pravin B Shelare6445712013-10-03 18:16:47 -0700905 if (!flow || !ovs_flow_cmp_unmasked_key(flow, &match)) {
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700906 err = -ENOENT;
907 goto unlock;
908 }
Jesse Grossccb13522011-10-25 19:26:31 -0700909
Thomas Graf795449d2013-11-30 13:21:32 +0100910 reply = ovs_flow_cmd_build_info(flow, dp, info, OVS_FLOW_CMD_NEW);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700911 if (IS_ERR(reply)) {
912 err = PTR_ERR(reply);
913 goto unlock;
914 }
Jesse Grossccb13522011-10-25 19:26:31 -0700915
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700916 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -0700917 return genlmsg_reply(reply, info);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700918unlock:
919 ovs_unlock();
920 return err;
Jesse Grossccb13522011-10-25 19:26:31 -0700921}
922
923static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
924{
925 struct nlattr **a = info->attrs;
926 struct ovs_header *ovs_header = info->userhdr;
927 struct sw_flow_key key;
928 struct sk_buff *reply;
929 struct sw_flow *flow;
930 struct datapath *dp;
Andy Zhou03f0d912013-08-07 20:01:00 -0700931 struct sw_flow_match match;
Jesse Grossccb13522011-10-25 19:26:31 -0700932 int err;
Jesse Grossccb13522011-10-25 19:26:31 -0700933
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700934 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800935 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700936 if (!dp) {
937 err = -ENODEV;
938 goto unlock;
939 }
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800940
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700941 if (!a[OVS_FLOW_ATTR_KEY]) {
Pravin B Shelarb637e492013-10-04 00:14:23 -0700942 err = ovs_flow_tbl_flush(&dp->table);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700943 goto unlock;
944 }
Andy Zhou03f0d912013-08-07 20:01:00 -0700945
946 ovs_match_init(&match, &key, NULL);
Pravin B Shelare298e502013-10-29 17:22:21 -0700947 err = ovs_nla_get_match(&match, NULL, a[OVS_FLOW_ATTR_KEY], NULL);
Jesse Grossccb13522011-10-25 19:26:31 -0700948 if (err)
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700949 goto unlock;
Jesse Grossccb13522011-10-25 19:26:31 -0700950
Andy Zhou5bb50632013-11-25 10:42:46 -0800951 flow = ovs_flow_tbl_lookup(&dp->table, &key);
Pravin B Shelare6445712013-10-03 18:16:47 -0700952 if (!flow || !ovs_flow_cmp_unmasked_key(flow, &match)) {
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700953 err = -ENOENT;
954 goto unlock;
955 }
Jesse Grossccb13522011-10-25 19:26:31 -0700956
Thomas Graf795449d2013-11-30 13:21:32 +0100957 reply = ovs_flow_cmd_alloc_info(flow, info);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700958 if (!reply) {
959 err = -ENOMEM;
960 goto unlock;
961 }
Jesse Grossccb13522011-10-25 19:26:31 -0700962
Pravin B Shelarb637e492013-10-04 00:14:23 -0700963 ovs_flow_tbl_remove(&dp->table, flow);
Jesse Grossccb13522011-10-25 19:26:31 -0700964
Eric W. Biederman15e47302012-09-07 20:12:54 +0000965 err = ovs_flow_cmd_fill_info(flow, dp, reply, info->snd_portid,
Jesse Grossccb13522011-10-25 19:26:31 -0700966 info->snd_seq, 0, OVS_FLOW_CMD_DEL);
967 BUG_ON(err < 0);
968
Andy Zhou03f0d912013-08-07 20:01:00 -0700969 ovs_flow_free(flow, true);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700970 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -0700971
Johannes Berg2a94fe42013-11-19 15:19:39 +0100972 ovs_notify(&dp_flow_genl_family, reply, info);
Jesse Grossccb13522011-10-25 19:26:31 -0700973 return 0;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700974unlock:
975 ovs_unlock();
976 return err;
Jesse Grossccb13522011-10-25 19:26:31 -0700977}
978
979static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
980{
981 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
Pravin B Shelarb637e492013-10-04 00:14:23 -0700982 struct table_instance *ti;
Jesse Grossccb13522011-10-25 19:26:31 -0700983 struct datapath *dp;
Jesse Grossccb13522011-10-25 19:26:31 -0700984
Pravin B Shelard57170b2013-07-30 15:39:39 -0700985 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800986 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700987 if (!dp) {
Pravin B Shelard57170b2013-07-30 15:39:39 -0700988 rcu_read_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -0700989 return -ENODEV;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700990 }
Jesse Grossccb13522011-10-25 19:26:31 -0700991
Pravin B Shelarb637e492013-10-04 00:14:23 -0700992 ti = rcu_dereference(dp->table.ti);
Jesse Grossccb13522011-10-25 19:26:31 -0700993 for (;;) {
994 struct sw_flow *flow;
995 u32 bucket, obj;
996
997 bucket = cb->args[0];
998 obj = cb->args[1];
Pravin B Shelarb637e492013-10-04 00:14:23 -0700999 flow = ovs_flow_tbl_dump_next(ti, &bucket, &obj);
Jesse Grossccb13522011-10-25 19:26:31 -07001000 if (!flow)
1001 break;
1002
1003 if (ovs_flow_cmd_fill_info(flow, dp, skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001004 NETLINK_CB(cb->skb).portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001005 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1006 OVS_FLOW_CMD_NEW) < 0)
1007 break;
1008
1009 cb->args[0] = bucket;
1010 cb->args[1] = obj;
1011 }
Pravin B Shelard57170b2013-07-30 15:39:39 -07001012 rcu_read_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001013 return skb->len;
1014}
1015
Johannes Berg4534de82013-11-14 17:14:46 +01001016static const struct genl_ops dp_flow_genl_ops[] = {
Jesse Grossccb13522011-10-25 19:26:31 -07001017 { .cmd = OVS_FLOW_CMD_NEW,
1018 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1019 .policy = flow_policy,
1020 .doit = ovs_flow_cmd_new_or_set
1021 },
1022 { .cmd = OVS_FLOW_CMD_DEL,
1023 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1024 .policy = flow_policy,
1025 .doit = ovs_flow_cmd_del
1026 },
1027 { .cmd = OVS_FLOW_CMD_GET,
1028 .flags = 0, /* OK for unprivileged users. */
1029 .policy = flow_policy,
1030 .doit = ovs_flow_cmd_get,
1031 .dumpit = ovs_flow_cmd_dump
1032 },
1033 { .cmd = OVS_FLOW_CMD_SET,
1034 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1035 .policy = flow_policy,
1036 .doit = ovs_flow_cmd_new_or_set,
1037 },
1038};
1039
1040static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1041 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1042 [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
Thomas Graf43d4be92013-12-13 15:22:18 +01001043 [OVS_DP_ATTR_USER_FEATURES] = { .type = NLA_U32 },
Jesse Grossccb13522011-10-25 19:26:31 -07001044};
1045
1046static struct genl_family dp_datapath_genl_family = {
1047 .id = GENL_ID_GENERATE,
1048 .hdrsize = sizeof(struct ovs_header),
1049 .name = OVS_DATAPATH_FAMILY,
1050 .version = OVS_DATAPATH_VERSION,
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001051 .maxattr = OVS_DP_ATTR_MAX,
Pravin B Shelar3a4e0d62013-04-23 07:48:48 +00001052 .netnsok = true,
1053 .parallel_ops = true,
Jesse Grossccb13522011-10-25 19:26:31 -07001054};
1055
1056static struct genl_multicast_group ovs_dp_datapath_multicast_group = {
1057 .name = OVS_DATAPATH_MCGROUP
1058};
1059
Thomas Grafc3ff8cf2013-03-29 14:46:49 +01001060static size_t ovs_dp_cmd_msg_size(void)
1061{
1062 size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
1063
1064 msgsize += nla_total_size(IFNAMSIZ);
1065 msgsize += nla_total_size(sizeof(struct ovs_dp_stats));
Andy Zhou1bd71162013-10-22 10:42:46 -07001066 msgsize += nla_total_size(sizeof(struct ovs_dp_megaflow_stats));
Thomas Grafc3ff8cf2013-03-29 14:46:49 +01001067
1068 return msgsize;
1069}
1070
Jesse Grossccb13522011-10-25 19:26:31 -07001071static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001072 u32 portid, u32 seq, u32 flags, u8 cmd)
Jesse Grossccb13522011-10-25 19:26:31 -07001073{
1074 struct ovs_header *ovs_header;
1075 struct ovs_dp_stats dp_stats;
Andy Zhou1bd71162013-10-22 10:42:46 -07001076 struct ovs_dp_megaflow_stats dp_megaflow_stats;
Jesse Grossccb13522011-10-25 19:26:31 -07001077 int err;
1078
Eric W. Biederman15e47302012-09-07 20:12:54 +00001079 ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
Jesse Grossccb13522011-10-25 19:26:31 -07001080 flags, cmd);
1081 if (!ovs_header)
1082 goto error;
1083
1084 ovs_header->dp_ifindex = get_dpifindex(dp);
1085
1086 rcu_read_lock();
1087 err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
1088 rcu_read_unlock();
1089 if (err)
1090 goto nla_put_failure;
1091
Andy Zhou1bd71162013-10-22 10:42:46 -07001092 get_dp_stats(dp, &dp_stats, &dp_megaflow_stats);
1093 if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats),
1094 &dp_stats))
1095 goto nla_put_failure;
1096
1097 if (nla_put(skb, OVS_DP_ATTR_MEGAFLOW_STATS,
1098 sizeof(struct ovs_dp_megaflow_stats),
1099 &dp_megaflow_stats))
David S. Miller028d6a62012-03-29 23:20:48 -04001100 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001101
Thomas Graf43d4be92013-12-13 15:22:18 +01001102 if (nla_put_u32(skb, OVS_DP_ATTR_USER_FEATURES, dp->user_features))
1103 goto nla_put_failure;
1104
Jesse Grossccb13522011-10-25 19:26:31 -07001105 return genlmsg_end(skb, ovs_header);
1106
1107nla_put_failure:
1108 genlmsg_cancel(skb, ovs_header);
1109error:
1110 return -EMSGSIZE;
1111}
1112
Thomas Graf795449d2013-11-30 13:21:32 +01001113static struct sk_buff *ovs_dp_cmd_build_info(struct datapath *dp,
1114 struct genl_info *info, u8 cmd)
Jesse Grossccb13522011-10-25 19:26:31 -07001115{
1116 struct sk_buff *skb;
1117 int retval;
1118
Thomas Graf795449d2013-11-30 13:21:32 +01001119 skb = genlmsg_new_unicast(ovs_dp_cmd_msg_size(), info, GFP_KERNEL);
Jesse Grossccb13522011-10-25 19:26:31 -07001120 if (!skb)
1121 return ERR_PTR(-ENOMEM);
1122
Thomas Graf795449d2013-11-30 13:21:32 +01001123 retval = ovs_dp_cmd_fill_info(dp, skb, info->snd_portid, info->snd_seq, 0, cmd);
Jesse Grossccb13522011-10-25 19:26:31 -07001124 if (retval < 0) {
1125 kfree_skb(skb);
1126 return ERR_PTR(retval);
1127 }
1128 return skb;
1129}
1130
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001131/* Called with ovs_mutex. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001132static struct datapath *lookup_datapath(struct net *net,
1133 struct ovs_header *ovs_header,
Jesse Grossccb13522011-10-25 19:26:31 -07001134 struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1135{
1136 struct datapath *dp;
1137
1138 if (!a[OVS_DP_ATTR_NAME])
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001139 dp = get_dp(net, ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001140 else {
1141 struct vport *vport;
1142
1143 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001144 vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
Jesse Grossccb13522011-10-25 19:26:31 -07001145 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
1146 rcu_read_unlock();
1147 }
1148 return dp ? dp : ERR_PTR(-ENODEV);
1149}
1150
Thomas Graf43d4be92013-12-13 15:22:18 +01001151static void ovs_dp_change(struct datapath *dp, struct nlattr **a)
1152{
1153 if (a[OVS_DP_ATTR_USER_FEATURES])
1154 dp->user_features = nla_get_u32(a[OVS_DP_ATTR_USER_FEATURES]);
1155}
1156
Jesse Grossccb13522011-10-25 19:26:31 -07001157static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
1158{
1159 struct nlattr **a = info->attrs;
1160 struct vport_parms parms;
1161 struct sk_buff *reply;
1162 struct datapath *dp;
1163 struct vport *vport;
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001164 struct ovs_net *ovs_net;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001165 int err, i;
Jesse Grossccb13522011-10-25 19:26:31 -07001166
1167 err = -EINVAL;
1168 if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
1169 goto err;
1170
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001171 ovs_lock();
Jesse Grossccb13522011-10-25 19:26:31 -07001172
1173 err = -ENOMEM;
1174 dp = kzalloc(sizeof(*dp), GFP_KERNEL);
1175 if (dp == NULL)
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001176 goto err_unlock_ovs;
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001177
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001178 ovs_dp_set_net(dp, hold_net(sock_net(skb->sk)));
Jesse Grossccb13522011-10-25 19:26:31 -07001179
1180 /* Allocate table. */
Pravin B Shelarb637e492013-10-04 00:14:23 -07001181 err = ovs_flow_tbl_init(&dp->table);
1182 if (err)
Jesse Grossccb13522011-10-25 19:26:31 -07001183 goto err_free_dp;
1184
1185 dp->stats_percpu = alloc_percpu(struct dp_stats_percpu);
1186 if (!dp->stats_percpu) {
1187 err = -ENOMEM;
1188 goto err_destroy_table;
1189 }
1190
John Stultz827da442013-10-07 15:51:58 -07001191 for_each_possible_cpu(i) {
1192 struct dp_stats_percpu *dpath_stats;
1193 dpath_stats = per_cpu_ptr(dp->stats_percpu, i);
1194 u64_stats_init(&dpath_stats->sync);
1195 }
1196
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001197 dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
Pravin B Shelare6445712013-10-03 18:16:47 -07001198 GFP_KERNEL);
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001199 if (!dp->ports) {
1200 err = -ENOMEM;
1201 goto err_destroy_percpu;
1202 }
1203
1204 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1205 INIT_HLIST_HEAD(&dp->ports[i]);
1206
Jesse Grossccb13522011-10-25 19:26:31 -07001207 /* Set up our datapath device. */
1208 parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1209 parms.type = OVS_VPORT_TYPE_INTERNAL;
1210 parms.options = NULL;
1211 parms.dp = dp;
1212 parms.port_no = OVSP_LOCAL;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001213 parms.upcall_portid = nla_get_u32(a[OVS_DP_ATTR_UPCALL_PID]);
Jesse Grossccb13522011-10-25 19:26:31 -07001214
Thomas Graf43d4be92013-12-13 15:22:18 +01001215 ovs_dp_change(dp, a);
1216
Jesse Grossccb13522011-10-25 19:26:31 -07001217 vport = new_vport(&parms);
1218 if (IS_ERR(vport)) {
1219 err = PTR_ERR(vport);
1220 if (err == -EBUSY)
1221 err = -EEXIST;
1222
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001223 goto err_destroy_ports_array;
Jesse Grossccb13522011-10-25 19:26:31 -07001224 }
1225
Thomas Graf795449d2013-11-30 13:21:32 +01001226 reply = ovs_dp_cmd_build_info(dp, info, OVS_DP_CMD_NEW);
Jesse Grossccb13522011-10-25 19:26:31 -07001227 err = PTR_ERR(reply);
1228 if (IS_ERR(reply))
1229 goto err_destroy_local_port;
1230
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001231 ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001232 list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001233
1234 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001235
Johannes Berg2a94fe42013-11-19 15:19:39 +01001236 ovs_notify(&dp_datapath_genl_family, reply, info);
Jesse Grossccb13522011-10-25 19:26:31 -07001237 return 0;
1238
1239err_destroy_local_port:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001240 ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001241err_destroy_ports_array:
1242 kfree(dp->ports);
Jesse Grossccb13522011-10-25 19:26:31 -07001243err_destroy_percpu:
1244 free_percpu(dp->stats_percpu);
1245err_destroy_table:
Pravin B Shelar618ed0c2013-10-04 00:17:42 -07001246 ovs_flow_tbl_destroy(&dp->table);
Jesse Grossccb13522011-10-25 19:26:31 -07001247err_free_dp:
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001248 release_net(ovs_dp_get_net(dp));
Jesse Grossccb13522011-10-25 19:26:31 -07001249 kfree(dp);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001250err_unlock_ovs:
1251 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001252err:
1253 return err;
1254}
1255
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001256/* Called with ovs_mutex. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001257static void __dp_destroy(struct datapath *dp)
Jesse Grossccb13522011-10-25 19:26:31 -07001258{
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001259 int i;
Jesse Grossccb13522011-10-25 19:26:31 -07001260
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001261 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1262 struct vport *vport;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001263 struct hlist_node *n;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001264
Sasha Levinb67bfe02013-02-27 17:06:00 -08001265 hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001266 if (vport->port_no != OVSP_LOCAL)
1267 ovs_dp_detach_port(vport);
1268 }
Jesse Grossccb13522011-10-25 19:26:31 -07001269
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001270 list_del_rcu(&dp->list_node);
Jesse Grossccb13522011-10-25 19:26:31 -07001271
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001272 /* OVSP_LOCAL is datapath internal port. We need to make sure that
1273 * all port in datapath are destroyed first before freeing datapath.
Jesse Grossccb13522011-10-25 19:26:31 -07001274 */
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001275 ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
Jesse Grossccb13522011-10-25 19:26:31 -07001276
1277 call_rcu(&dp->rcu, destroy_dp_rcu);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001278}
1279
1280static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1281{
1282 struct sk_buff *reply;
1283 struct datapath *dp;
1284 int err;
1285
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001286 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001287 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1288 err = PTR_ERR(dp);
1289 if (IS_ERR(dp))
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001290 goto unlock;
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001291
Thomas Graf795449d2013-11-30 13:21:32 +01001292 reply = ovs_dp_cmd_build_info(dp, info, OVS_DP_CMD_DEL);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001293 err = PTR_ERR(reply);
1294 if (IS_ERR(reply))
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001295 goto unlock;
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001296
1297 __dp_destroy(dp);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001298 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001299
Johannes Berg2a94fe42013-11-19 15:19:39 +01001300 ovs_notify(&dp_datapath_genl_family, reply, info);
Jesse Grossccb13522011-10-25 19:26:31 -07001301
1302 return 0;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001303unlock:
1304 ovs_unlock();
1305 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001306}
1307
1308static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1309{
1310 struct sk_buff *reply;
1311 struct datapath *dp;
1312 int err;
1313
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001314 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001315 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001316 err = PTR_ERR(dp);
Jesse Grossccb13522011-10-25 19:26:31 -07001317 if (IS_ERR(dp))
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001318 goto unlock;
Jesse Grossccb13522011-10-25 19:26:31 -07001319
Thomas Graf43d4be92013-12-13 15:22:18 +01001320 ovs_dp_change(dp, info->attrs);
1321
Thomas Graf795449d2013-11-30 13:21:32 +01001322 reply = ovs_dp_cmd_build_info(dp, info, OVS_DP_CMD_NEW);
Jesse Grossccb13522011-10-25 19:26:31 -07001323 if (IS_ERR(reply)) {
1324 err = PTR_ERR(reply);
Johannes Berg68eb5502013-11-19 15:19:38 +01001325 genl_set_err(&dp_datapath_genl_family, sock_net(skb->sk), 0,
Johannes Berg2a94fe42013-11-19 15:19:39 +01001326 0, err);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001327 err = 0;
1328 goto unlock;
Jesse Grossccb13522011-10-25 19:26:31 -07001329 }
1330
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001331 ovs_unlock();
Johannes Berg2a94fe42013-11-19 15:19:39 +01001332 ovs_notify(&dp_datapath_genl_family, reply, info);
Jesse Grossccb13522011-10-25 19:26:31 -07001333
1334 return 0;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001335unlock:
1336 ovs_unlock();
1337 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001338}
1339
1340static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1341{
1342 struct sk_buff *reply;
1343 struct datapath *dp;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001344 int err;
Jesse Grossccb13522011-10-25 19:26:31 -07001345
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001346 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001347 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001348 if (IS_ERR(dp)) {
1349 err = PTR_ERR(dp);
1350 goto unlock;
1351 }
Jesse Grossccb13522011-10-25 19:26:31 -07001352
Thomas Graf795449d2013-11-30 13:21:32 +01001353 reply = ovs_dp_cmd_build_info(dp, info, OVS_DP_CMD_NEW);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001354 if (IS_ERR(reply)) {
1355 err = PTR_ERR(reply);
1356 goto unlock;
1357 }
Jesse Grossccb13522011-10-25 19:26:31 -07001358
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001359 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001360 return genlmsg_reply(reply, info);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001361
1362unlock:
1363 ovs_unlock();
1364 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001365}
1366
1367static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1368{
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001369 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
Jesse Grossccb13522011-10-25 19:26:31 -07001370 struct datapath *dp;
1371 int skip = cb->args[0];
1372 int i = 0;
1373
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001374 rcu_read_lock();
1375 list_for_each_entry_rcu(dp, &ovs_net->dps, list_node) {
Ben Pfaff77676fd2012-01-17 13:33:39 +00001376 if (i >= skip &&
Eric W. Biederman15e47302012-09-07 20:12:54 +00001377 ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001378 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1379 OVS_DP_CMD_NEW) < 0)
1380 break;
1381 i++;
1382 }
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001383 rcu_read_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001384
1385 cb->args[0] = i;
1386
1387 return skb->len;
1388}
1389
Johannes Berg4534de82013-11-14 17:14:46 +01001390static const struct genl_ops dp_datapath_genl_ops[] = {
Jesse Grossccb13522011-10-25 19:26:31 -07001391 { .cmd = OVS_DP_CMD_NEW,
1392 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1393 .policy = datapath_policy,
1394 .doit = ovs_dp_cmd_new
1395 },
1396 { .cmd = OVS_DP_CMD_DEL,
1397 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1398 .policy = datapath_policy,
1399 .doit = ovs_dp_cmd_del
1400 },
1401 { .cmd = OVS_DP_CMD_GET,
1402 .flags = 0, /* OK for unprivileged users. */
1403 .policy = datapath_policy,
1404 .doit = ovs_dp_cmd_get,
1405 .dumpit = ovs_dp_cmd_dump
1406 },
1407 { .cmd = OVS_DP_CMD_SET,
1408 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1409 .policy = datapath_policy,
1410 .doit = ovs_dp_cmd_set,
1411 },
1412};
1413
1414static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
1415 [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1416 [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
1417 [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
1418 [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
1419 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1420 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
1421};
1422
Johannes Berg68eb5502013-11-19 15:19:38 +01001423struct genl_family dp_vport_genl_family = {
Jesse Grossccb13522011-10-25 19:26:31 -07001424 .id = GENL_ID_GENERATE,
1425 .hdrsize = sizeof(struct ovs_header),
1426 .name = OVS_VPORT_FAMILY,
1427 .version = OVS_VPORT_VERSION,
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001428 .maxattr = OVS_VPORT_ATTR_MAX,
Pravin B Shelar3a4e0d62013-04-23 07:48:48 +00001429 .netnsok = true,
1430 .parallel_ops = true,
Jesse Grossccb13522011-10-25 19:26:31 -07001431};
1432
1433struct genl_multicast_group ovs_dp_vport_multicast_group = {
1434 .name = OVS_VPORT_MCGROUP
1435};
1436
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001437/* Called with ovs_mutex or RCU read lock. */
Jesse Grossccb13522011-10-25 19:26:31 -07001438static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001439 u32 portid, u32 seq, u32 flags, u8 cmd)
Jesse Grossccb13522011-10-25 19:26:31 -07001440{
1441 struct ovs_header *ovs_header;
1442 struct ovs_vport_stats vport_stats;
1443 int err;
1444
Eric W. Biederman15e47302012-09-07 20:12:54 +00001445 ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
Jesse Grossccb13522011-10-25 19:26:31 -07001446 flags, cmd);
1447 if (!ovs_header)
1448 return -EMSGSIZE;
1449
1450 ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1451
David S. Miller028d6a62012-03-29 23:20:48 -04001452 if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
1453 nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
1454 nla_put_string(skb, OVS_VPORT_ATTR_NAME, vport->ops->get_name(vport)) ||
Eric W. Biederman15e47302012-09-07 20:12:54 +00001455 nla_put_u32(skb, OVS_VPORT_ATTR_UPCALL_PID, vport->upcall_portid))
David S. Miller028d6a62012-03-29 23:20:48 -04001456 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001457
1458 ovs_vport_get_stats(vport, &vport_stats);
David S. Miller028d6a62012-03-29 23:20:48 -04001459 if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
1460 &vport_stats))
1461 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001462
1463 err = ovs_vport_get_options(vport, skb);
1464 if (err == -EMSGSIZE)
1465 goto error;
1466
1467 return genlmsg_end(skb, ovs_header);
1468
1469nla_put_failure:
1470 err = -EMSGSIZE;
1471error:
1472 genlmsg_cancel(skb, ovs_header);
1473 return err;
1474}
1475
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001476/* Called with ovs_mutex or RCU read lock. */
Eric W. Biederman15e47302012-09-07 20:12:54 +00001477struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001478 u32 seq, u8 cmd)
1479{
1480 struct sk_buff *skb;
1481 int retval;
1482
1483 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1484 if (!skb)
1485 return ERR_PTR(-ENOMEM);
1486
Eric W. Biederman15e47302012-09-07 20:12:54 +00001487 retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
Jesse Grossa9341512013-03-26 15:48:38 -07001488 BUG_ON(retval < 0);
1489
Jesse Grossccb13522011-10-25 19:26:31 -07001490 return skb;
1491}
1492
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001493/* Called with ovs_mutex or RCU read lock. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001494static struct vport *lookup_vport(struct net *net,
1495 struct ovs_header *ovs_header,
Jesse Grossccb13522011-10-25 19:26:31 -07001496 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1497{
1498 struct datapath *dp;
1499 struct vport *vport;
1500
1501 if (a[OVS_VPORT_ATTR_NAME]) {
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001502 vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
Jesse Grossccb13522011-10-25 19:26:31 -07001503 if (!vport)
1504 return ERR_PTR(-ENODEV);
Ben Pfaff651a68e2012-03-06 15:04:04 -08001505 if (ovs_header->dp_ifindex &&
1506 ovs_header->dp_ifindex != get_dpifindex(vport->dp))
1507 return ERR_PTR(-ENODEV);
Jesse Grossccb13522011-10-25 19:26:31 -07001508 return vport;
1509 } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
1510 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1511
1512 if (port_no >= DP_MAX_PORTS)
1513 return ERR_PTR(-EFBIG);
1514
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001515 dp = get_dp(net, ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001516 if (!dp)
1517 return ERR_PTR(-ENODEV);
1518
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001519 vport = ovs_vport_ovsl_rcu(dp, port_no);
Jesse Grossccb13522011-10-25 19:26:31 -07001520 if (!vport)
Jarno Rajahalme14408db2013-01-09 14:27:35 -08001521 return ERR_PTR(-ENODEV);
Jesse Grossccb13522011-10-25 19:26:31 -07001522 return vport;
1523 } else
1524 return ERR_PTR(-EINVAL);
1525}
1526
1527static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
1528{
1529 struct nlattr **a = info->attrs;
1530 struct ovs_header *ovs_header = info->userhdr;
1531 struct vport_parms parms;
1532 struct sk_buff *reply;
1533 struct vport *vport;
1534 struct datapath *dp;
1535 u32 port_no;
1536 int err;
1537
1538 err = -EINVAL;
1539 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
1540 !a[OVS_VPORT_ATTR_UPCALL_PID])
1541 goto exit;
1542
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001543 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001544 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001545 err = -ENODEV;
1546 if (!dp)
1547 goto exit_unlock;
1548
1549 if (a[OVS_VPORT_ATTR_PORT_NO]) {
1550 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1551
1552 err = -EFBIG;
1553 if (port_no >= DP_MAX_PORTS)
1554 goto exit_unlock;
1555
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001556 vport = ovs_vport_ovsl(dp, port_no);
Jesse Grossccb13522011-10-25 19:26:31 -07001557 err = -EBUSY;
1558 if (vport)
1559 goto exit_unlock;
1560 } else {
1561 for (port_no = 1; ; port_no++) {
1562 if (port_no >= DP_MAX_PORTS) {
1563 err = -EFBIG;
1564 goto exit_unlock;
1565 }
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001566 vport = ovs_vport_ovsl(dp, port_no);
Jesse Grossccb13522011-10-25 19:26:31 -07001567 if (!vport)
1568 break;
1569 }
1570 }
1571
1572 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
1573 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1574 parms.options = a[OVS_VPORT_ATTR_OPTIONS];
1575 parms.dp = dp;
1576 parms.port_no = port_no;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001577 parms.upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
Jesse Grossccb13522011-10-25 19:26:31 -07001578
1579 vport = new_vport(&parms);
1580 err = PTR_ERR(vport);
1581 if (IS_ERR(vport))
1582 goto exit_unlock;
1583
Rich Lanecb7c5bd2013-02-08 13:18:01 -08001584 err = 0;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001585 reply = ovs_vport_cmd_build_info(vport, info->snd_portid, info->snd_seq,
Jesse Grossccb13522011-10-25 19:26:31 -07001586 OVS_VPORT_CMD_NEW);
1587 if (IS_ERR(reply)) {
1588 err = PTR_ERR(reply);
1589 ovs_dp_detach_port(vport);
1590 goto exit_unlock;
1591 }
Thomas Grafed661182013-03-29 14:46:50 +01001592
Johannes Berg2a94fe42013-11-19 15:19:39 +01001593 ovs_notify(&dp_vport_genl_family, reply, info);
Jesse Grossccb13522011-10-25 19:26:31 -07001594
1595exit_unlock:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001596 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001597exit:
1598 return err;
1599}
1600
1601static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
1602{
1603 struct nlattr **a = info->attrs;
1604 struct sk_buff *reply;
1605 struct vport *vport;
1606 int err;
1607
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001608 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001609 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
Jesse Grossccb13522011-10-25 19:26:31 -07001610 err = PTR_ERR(vport);
1611 if (IS_ERR(vport))
1612 goto exit_unlock;
1613
Jesse Grossccb13522011-10-25 19:26:31 -07001614 if (a[OVS_VPORT_ATTR_TYPE] &&
Jesse Grossf44f3402013-05-13 08:15:26 -07001615 nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
Jesse Grossccb13522011-10-25 19:26:31 -07001616 err = -EINVAL;
Jesse Grossf44f3402013-05-13 08:15:26 -07001617 goto exit_unlock;
1618 }
Jesse Grossccb13522011-10-25 19:26:31 -07001619
Jesse Grossa9341512013-03-26 15:48:38 -07001620 reply = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1621 if (!reply) {
1622 err = -ENOMEM;
1623 goto exit_unlock;
1624 }
1625
Jesse Grossf44f3402013-05-13 08:15:26 -07001626 if (a[OVS_VPORT_ATTR_OPTIONS]) {
Jesse Grossccb13522011-10-25 19:26:31 -07001627 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
Jesse Grossf44f3402013-05-13 08:15:26 -07001628 if (err)
1629 goto exit_free;
1630 }
Jesse Grossa9341512013-03-26 15:48:38 -07001631
Ansis Atteka03fbf8b2012-04-09 12:12:12 -07001632 if (a[OVS_VPORT_ATTR_UPCALL_PID])
Eric W. Biederman15e47302012-09-07 20:12:54 +00001633 vport->upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
Jesse Grossccb13522011-10-25 19:26:31 -07001634
Jesse Grossa9341512013-03-26 15:48:38 -07001635 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1636 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1637 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001638
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001639 ovs_unlock();
Johannes Berg2a94fe42013-11-19 15:19:39 +01001640 ovs_notify(&dp_vport_genl_family, reply, info);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001641 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001642
Jesse Grossa9341512013-03-26 15:48:38 -07001643exit_free:
1644 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001645exit_unlock:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001646 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001647 return err;
1648}
1649
1650static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
1651{
1652 struct nlattr **a = info->attrs;
1653 struct sk_buff *reply;
1654 struct vport *vport;
1655 int err;
1656
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001657 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001658 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
Jesse Grossccb13522011-10-25 19:26:31 -07001659 err = PTR_ERR(vport);
1660 if (IS_ERR(vport))
1661 goto exit_unlock;
1662
1663 if (vport->port_no == OVSP_LOCAL) {
1664 err = -EINVAL;
1665 goto exit_unlock;
1666 }
1667
Pravin B Shelar74f84a52013-06-17 17:50:12 -07001668 reply = ovs_vport_cmd_build_info(vport, info->snd_portid,
1669 info->snd_seq, OVS_VPORT_CMD_DEL);
Jesse Grossccb13522011-10-25 19:26:31 -07001670 err = PTR_ERR(reply);
1671 if (IS_ERR(reply))
1672 goto exit_unlock;
1673
Rich Lane734907e2013-02-08 09:30:23 -08001674 err = 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001675 ovs_dp_detach_port(vport);
1676
Johannes Berg2a94fe42013-11-19 15:19:39 +01001677 ovs_notify(&dp_vport_genl_family, reply, info);
Jesse Grossccb13522011-10-25 19:26:31 -07001678
1679exit_unlock:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001680 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001681 return err;
1682}
1683
1684static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
1685{
1686 struct nlattr **a = info->attrs;
1687 struct ovs_header *ovs_header = info->userhdr;
1688 struct sk_buff *reply;
1689 struct vport *vport;
1690 int err;
1691
1692 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001693 vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
Jesse Grossccb13522011-10-25 19:26:31 -07001694 err = PTR_ERR(vport);
1695 if (IS_ERR(vport))
1696 goto exit_unlock;
1697
Pravin B Shelar74f84a52013-06-17 17:50:12 -07001698 reply = ovs_vport_cmd_build_info(vport, info->snd_portid,
1699 info->snd_seq, OVS_VPORT_CMD_NEW);
Jesse Grossccb13522011-10-25 19:26:31 -07001700 err = PTR_ERR(reply);
1701 if (IS_ERR(reply))
1702 goto exit_unlock;
1703
1704 rcu_read_unlock();
1705
1706 return genlmsg_reply(reply, info);
1707
1708exit_unlock:
1709 rcu_read_unlock();
1710 return err;
1711}
1712
1713static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1714{
1715 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1716 struct datapath *dp;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001717 int bucket = cb->args[0], skip = cb->args[1];
1718 int i, j = 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001719
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001720 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001721 if (!dp)
1722 return -ENODEV;
1723
1724 rcu_read_lock();
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001725 for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
Jesse Grossccb13522011-10-25 19:26:31 -07001726 struct vport *vport;
1727
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001728 j = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001729 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001730 if (j >= skip &&
1731 ovs_vport_cmd_fill_info(vport, skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001732 NETLINK_CB(cb->skb).portid,
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001733 cb->nlh->nlmsg_seq,
1734 NLM_F_MULTI,
1735 OVS_VPORT_CMD_NEW) < 0)
1736 goto out;
Jesse Grossccb13522011-10-25 19:26:31 -07001737
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001738 j++;
1739 }
1740 skip = 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001741 }
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001742out:
Jesse Grossccb13522011-10-25 19:26:31 -07001743 rcu_read_unlock();
1744
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001745 cb->args[0] = i;
1746 cb->args[1] = j;
Jesse Grossccb13522011-10-25 19:26:31 -07001747
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001748 return skb->len;
Jesse Grossccb13522011-10-25 19:26:31 -07001749}
1750
Johannes Berg4534de82013-11-14 17:14:46 +01001751static const struct genl_ops dp_vport_genl_ops[] = {
Jesse Grossccb13522011-10-25 19:26:31 -07001752 { .cmd = OVS_VPORT_CMD_NEW,
1753 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1754 .policy = vport_policy,
1755 .doit = ovs_vport_cmd_new
1756 },
1757 { .cmd = OVS_VPORT_CMD_DEL,
1758 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1759 .policy = vport_policy,
1760 .doit = ovs_vport_cmd_del
1761 },
1762 { .cmd = OVS_VPORT_CMD_GET,
1763 .flags = 0, /* OK for unprivileged users. */
1764 .policy = vport_policy,
1765 .doit = ovs_vport_cmd_get,
1766 .dumpit = ovs_vport_cmd_dump
1767 },
1768 { .cmd = OVS_VPORT_CMD_SET,
1769 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1770 .policy = vport_policy,
1771 .doit = ovs_vport_cmd_set,
1772 },
1773};
1774
1775struct genl_family_and_ops {
1776 struct genl_family *family;
Johannes Berg4534de82013-11-14 17:14:46 +01001777 const struct genl_ops *ops;
Jesse Grossccb13522011-10-25 19:26:31 -07001778 int n_ops;
Johannes Berg2a94fe42013-11-19 15:19:39 +01001779 const struct genl_multicast_group *group;
Jesse Grossccb13522011-10-25 19:26:31 -07001780};
1781
1782static const struct genl_family_and_ops dp_genl_families[] = {
1783 { &dp_datapath_genl_family,
1784 dp_datapath_genl_ops, ARRAY_SIZE(dp_datapath_genl_ops),
1785 &ovs_dp_datapath_multicast_group },
1786 { &dp_vport_genl_family,
1787 dp_vport_genl_ops, ARRAY_SIZE(dp_vport_genl_ops),
1788 &ovs_dp_vport_multicast_group },
1789 { &dp_flow_genl_family,
1790 dp_flow_genl_ops, ARRAY_SIZE(dp_flow_genl_ops),
1791 &ovs_dp_flow_multicast_group },
1792 { &dp_packet_genl_family,
1793 dp_packet_genl_ops, ARRAY_SIZE(dp_packet_genl_ops),
1794 NULL },
1795};
1796
1797static void dp_unregister_genl(int n_families)
1798{
1799 int i;
1800
1801 for (i = 0; i < n_families; i++)
1802 genl_unregister_family(dp_genl_families[i].family);
1803}
1804
1805static int dp_register_genl(void)
1806{
1807 int n_registered;
1808 int err;
1809 int i;
1810
1811 n_registered = 0;
1812 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
1813 const struct genl_family_and_ops *f = &dp_genl_families[i];
1814
Johannes Bergc53ed742013-11-19 15:19:31 +01001815 f->family->ops = f->ops;
1816 f->family->n_ops = f->n_ops;
Johannes Berg2a94fe42013-11-19 15:19:39 +01001817 f->family->mcgrps = f->group;
1818 f->family->n_mcgrps = f->group ? 1 : 0;
Johannes Bergc53ed742013-11-19 15:19:31 +01001819 err = genl_register_family(f->family);
Jesse Grossccb13522011-10-25 19:26:31 -07001820 if (err)
1821 goto error;
1822 n_registered++;
Jesse Grossccb13522011-10-25 19:26:31 -07001823 }
1824
1825 return 0;
1826
1827error:
1828 dp_unregister_genl(n_registered);
1829 return err;
1830}
1831
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001832static int __net_init ovs_init_net(struct net *net)
1833{
1834 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
1835
1836 INIT_LIST_HEAD(&ovs_net->dps);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001837 INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001838 return 0;
1839}
1840
1841static void __net_exit ovs_exit_net(struct net *net)
1842{
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001843 struct datapath *dp, *dp_next;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001844 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001845
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001846 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001847 list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
1848 __dp_destroy(dp);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001849 ovs_unlock();
1850
1851 cancel_work_sync(&ovs_net->dp_notify_work);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001852}
1853
1854static struct pernet_operations ovs_net_ops = {
1855 .init = ovs_init_net,
1856 .exit = ovs_exit_net,
1857 .id = &ovs_net_id,
1858 .size = sizeof(struct ovs_net),
1859};
1860
Jesse Grossccb13522011-10-25 19:26:31 -07001861static int __init dp_init(void)
1862{
Jesse Grossccb13522011-10-25 19:26:31 -07001863 int err;
1864
YOSHIFUJI Hideaki / 吉藤英明3523b292013-01-09 07:19:55 +00001865 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
Jesse Grossccb13522011-10-25 19:26:31 -07001866
1867 pr_info("Open vSwitch switching datapath\n");
1868
1869 err = ovs_flow_init();
1870 if (err)
1871 goto error;
1872
1873 err = ovs_vport_init();
1874 if (err)
1875 goto error_flow_exit;
1876
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001877 err = register_pernet_device(&ovs_net_ops);
Jesse Grossccb13522011-10-25 19:26:31 -07001878 if (err)
1879 goto error_vport_exit;
1880
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001881 err = register_netdevice_notifier(&ovs_dp_device_notifier);
1882 if (err)
1883 goto error_netns_exit;
1884
Jesse Grossccb13522011-10-25 19:26:31 -07001885 err = dp_register_genl();
1886 if (err < 0)
1887 goto error_unreg_notifier;
1888
Jesse Grossccb13522011-10-25 19:26:31 -07001889 return 0;
1890
1891error_unreg_notifier:
1892 unregister_netdevice_notifier(&ovs_dp_device_notifier);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001893error_netns_exit:
1894 unregister_pernet_device(&ovs_net_ops);
Jesse Grossccb13522011-10-25 19:26:31 -07001895error_vport_exit:
1896 ovs_vport_exit();
1897error_flow_exit:
1898 ovs_flow_exit();
1899error:
1900 return err;
1901}
1902
1903static void dp_cleanup(void)
1904{
Jesse Grossccb13522011-10-25 19:26:31 -07001905 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
1906 unregister_netdevice_notifier(&ovs_dp_device_notifier);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001907 unregister_pernet_device(&ovs_net_ops);
1908 rcu_barrier();
Jesse Grossccb13522011-10-25 19:26:31 -07001909 ovs_vport_exit();
1910 ovs_flow_exit();
1911}
1912
1913module_init(dp_init);
1914module_exit(dp_cleanup);
1915
1916MODULE_DESCRIPTION("Open vSwitch switching datapath");
1917MODULE_LICENSE("GPL");