blob: 60b9be3b947779b89cc69ebf5a3bd1c3538cf54e [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
Thomas Grafed661182013-03-29 14:46:50 +010064static void ovs_notify(struct sk_buff *skb, struct genl_info *info,
65 struct genl_multicast_group *grp)
66{
67 genl_notify(skb, genl_info_net(info), info->snd_portid,
68 grp->id, info->nlhdr, GFP_KERNEL);
69}
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 Shelarb637e492013-10-04 00:14:23 -0700164 ovs_flow_tbl_destroy(&dp->table, false);
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;
224 int error;
Jesse Grossccb13522011-10-25 19:26:31 -0700225
Shan Wei404f2f12012-11-13 09:52:25 +0800226 stats = this_cpu_ptr(dp->stats_percpu);
Jesse Grossccb13522011-10-25 19:26:31 -0700227
228 /* Extract flow from 'skb' into 'key'. */
Andy Zhou03f0d912013-08-07 20:01:00 -0700229 error = ovs_flow_extract(skb, p->port_no, &key);
Jesse Grossccb13522011-10-25 19:26:31 -0700230 if (unlikely(error)) {
231 kfree_skb(skb);
232 return;
233 }
234
235 /* Look up flow. */
Pravin B Shelarb637e492013-10-04 00:14:23 -0700236 flow = ovs_flow_tbl_lookup(&dp->table, &key);
Jesse Grossccb13522011-10-25 19:26:31 -0700237 if (unlikely(!flow)) {
238 struct dp_upcall_info upcall;
239
240 upcall.cmd = OVS_PACKET_CMD_MISS;
241 upcall.key = &key;
242 upcall.userdata = NULL;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000243 upcall.portid = p->upcall_portid;
Jesse Grossccb13522011-10-25 19:26:31 -0700244 ovs_dp_upcall(dp, skb, &upcall);
245 consume_skb(skb);
246 stats_counter = &stats->n_missed;
247 goto out;
248 }
249
250 OVS_CB(skb)->flow = flow;
Andy Zhou03f0d912013-08-07 20:01:00 -0700251 OVS_CB(skb)->pkt_key = &key;
Jesse Grossccb13522011-10-25 19:26:31 -0700252
253 stats_counter = &stats->n_hit;
254 ovs_flow_used(OVS_CB(skb)->flow, skb);
255 ovs_execute_actions(dp, skb);
256
257out:
258 /* Update datapath statistics. */
259 u64_stats_update_begin(&stats->sync);
260 (*stats_counter)++;
261 u64_stats_update_end(&stats->sync);
262}
263
264static struct genl_family dp_packet_genl_family = {
265 .id = GENL_ID_GENERATE,
266 .hdrsize = sizeof(struct ovs_header),
267 .name = OVS_PACKET_FAMILY,
268 .version = OVS_PACKET_VERSION,
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800269 .maxattr = OVS_PACKET_ATTR_MAX,
Pravin B Shelar3a4e0d62013-04-23 07:48:48 +0000270 .netnsok = true,
271 .parallel_ops = true,
Jesse Grossccb13522011-10-25 19:26:31 -0700272};
273
274int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800275 const struct dp_upcall_info *upcall_info)
Jesse Grossccb13522011-10-25 19:26:31 -0700276{
277 struct dp_stats_percpu *stats;
278 int dp_ifindex;
279 int err;
280
Eric W. Biederman15e47302012-09-07 20:12:54 +0000281 if (upcall_info->portid == 0) {
Jesse Grossccb13522011-10-25 19:26:31 -0700282 err = -ENOTCONN;
283 goto err;
284 }
285
286 dp_ifindex = get_dpifindex(dp);
287 if (!dp_ifindex) {
288 err = -ENODEV;
289 goto err;
290 }
291
292 if (!skb_is_gso(skb))
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800293 err = queue_userspace_packet(ovs_dp_get_net(dp), dp_ifindex, skb, upcall_info);
Jesse Grossccb13522011-10-25 19:26:31 -0700294 else
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800295 err = queue_gso_packets(ovs_dp_get_net(dp), dp_ifindex, skb, upcall_info);
Jesse Grossccb13522011-10-25 19:26:31 -0700296 if (err)
297 goto err;
298
299 return 0;
300
301err:
Shan Wei404f2f12012-11-13 09:52:25 +0800302 stats = this_cpu_ptr(dp->stats_percpu);
Jesse Grossccb13522011-10-25 19:26:31 -0700303
304 u64_stats_update_begin(&stats->sync);
305 stats->n_lost++;
306 u64_stats_update_end(&stats->sync);
307
308 return err;
309}
310
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800311static int queue_gso_packets(struct net *net, int dp_ifindex,
312 struct sk_buff *skb,
Jesse Grossccb13522011-10-25 19:26:31 -0700313 const struct dp_upcall_info *upcall_info)
314{
Ben Pfaffa1b5d0d2012-07-20 14:47:54 -0700315 unsigned short gso_type = skb_shinfo(skb)->gso_type;
Jesse Grossccb13522011-10-25 19:26:31 -0700316 struct dp_upcall_info later_info;
317 struct sw_flow_key later_key;
318 struct sk_buff *segs, *nskb;
319 int err;
320
Cong Wang12b00042013-02-05 16:36:38 +0000321 segs = __skb_gso_segment(skb, NETIF_F_SG | NETIF_F_HW_CSUM, false);
Pravin B Shelar92e5dfc2012-07-20 14:46:29 -0700322 if (IS_ERR(segs))
323 return PTR_ERR(segs);
Jesse Grossccb13522011-10-25 19:26:31 -0700324
325 /* Queue all of the segments. */
326 skb = segs;
327 do {
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800328 err = queue_userspace_packet(net, dp_ifindex, skb, upcall_info);
Jesse Grossccb13522011-10-25 19:26:31 -0700329 if (err)
330 break;
331
Ben Pfaffa1b5d0d2012-07-20 14:47:54 -0700332 if (skb == segs && gso_type & SKB_GSO_UDP) {
Jesse Grossccb13522011-10-25 19:26:31 -0700333 /* The initial flow key extracted by ovs_flow_extract()
334 * in this case is for a first fragment, so we need to
335 * properly mark later fragments.
336 */
337 later_key = *upcall_info->key;
338 later_key.ip.frag = OVS_FRAG_TYPE_LATER;
339
340 later_info = *upcall_info;
341 later_info.key = &later_key;
342 upcall_info = &later_info;
343 }
344 } while ((skb = skb->next));
345
346 /* Free all of the segments. */
347 skb = segs;
348 do {
349 nskb = skb->next;
350 if (err)
351 kfree_skb(skb);
352 else
353 consume_skb(skb);
354 } while ((skb = nskb));
355 return err;
356}
357
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100358static size_t key_attr_size(void)
359{
360 return nla_total_size(4) /* OVS_KEY_ATTR_PRIORITY */
Pravin B Shelar7d5437c2013-06-17 17:50:18 -0700361 + nla_total_size(0) /* OVS_KEY_ATTR_TUNNEL */
362 + nla_total_size(8) /* OVS_TUNNEL_KEY_ATTR_ID */
363 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_SRC */
364 + nla_total_size(4) /* OVS_TUNNEL_KEY_ATTR_IPV4_DST */
365 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TOS */
366 + nla_total_size(1) /* OVS_TUNNEL_KEY_ATTR_TTL */
367 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_DONT_FRAGMENT */
368 + nla_total_size(0) /* OVS_TUNNEL_KEY_ATTR_CSUM */
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100369 + nla_total_size(4) /* OVS_KEY_ATTR_IN_PORT */
370 + nla_total_size(4) /* OVS_KEY_ATTR_SKB_MARK */
371 + nla_total_size(12) /* OVS_KEY_ATTR_ETHERNET */
372 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
373 + nla_total_size(4) /* OVS_KEY_ATTR_8021Q */
374 + nla_total_size(0) /* OVS_KEY_ATTR_ENCAP */
375 + nla_total_size(2) /* OVS_KEY_ATTR_ETHERTYPE */
376 + nla_total_size(40) /* OVS_KEY_ATTR_IPV6 */
377 + nla_total_size(2) /* OVS_KEY_ATTR_ICMPV6 */
378 + nla_total_size(28); /* OVS_KEY_ATTR_ND */
379}
380
381static size_t upcall_msg_size(const struct sk_buff *skb,
382 const struct nlattr *userdata)
383{
384 size_t size = NLMSG_ALIGN(sizeof(struct ovs_header))
385 + nla_total_size(skb->len) /* OVS_PACKET_ATTR_PACKET */
386 + nla_total_size(key_attr_size()); /* OVS_PACKET_ATTR_KEY */
387
388 /* OVS_PACKET_ATTR_USERDATA */
389 if (userdata)
390 size += NLA_ALIGN(userdata->nla_len);
391
392 return size;
393}
394
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800395static int queue_userspace_packet(struct net *net, int dp_ifindex,
396 struct sk_buff *skb,
Jesse Grossccb13522011-10-25 19:26:31 -0700397 const struct dp_upcall_info *upcall_info)
398{
399 struct ovs_header *upcall;
400 struct sk_buff *nskb = NULL;
401 struct sk_buff *user_skb; /* to be queued to userspace */
402 struct nlattr *nla;
Jesse Grossccb13522011-10-25 19:26:31 -0700403 int err;
404
405 if (vlan_tx_tag_present(skb)) {
406 nskb = skb_clone(skb, GFP_ATOMIC);
407 if (!nskb)
408 return -ENOMEM;
409
Patrick McHardy86a9bad2013-04-19 02:04:30 +0000410 nskb = __vlan_put_tag(nskb, nskb->vlan_proto, vlan_tx_tag_get(nskb));
Dan Carpenter8aa51d62012-05-13 08:44:18 +0000411 if (!nskb)
Jesse Grossccb13522011-10-25 19:26:31 -0700412 return -ENOMEM;
413
414 nskb->vlan_tci = 0;
415 skb = nskb;
416 }
417
418 if (nla_attr_size(skb->len) > USHRT_MAX) {
419 err = -EFBIG;
420 goto out;
421 }
422
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100423 user_skb = genlmsg_new(upcall_msg_size(skb, upcall_info->userdata), GFP_ATOMIC);
Jesse Grossccb13522011-10-25 19:26:31 -0700424 if (!user_skb) {
425 err = -ENOMEM;
426 goto out;
427 }
428
429 upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
430 0, upcall_info->cmd);
431 upcall->dp_ifindex = dp_ifindex;
432
433 nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
Pravin B Shelare6445712013-10-03 18:16:47 -0700434 ovs_nla_put_flow(upcall_info->key, upcall_info->key, user_skb);
Jesse Grossccb13522011-10-25 19:26:31 -0700435 nla_nest_end(user_skb, nla);
436
437 if (upcall_info->userdata)
Ben Pfaff44901082013-02-15 17:29:22 -0800438 __nla_put(user_skb, OVS_PACKET_ATTR_USERDATA,
439 nla_len(upcall_info->userdata),
440 nla_data(upcall_info->userdata));
Jesse Grossccb13522011-10-25 19:26:31 -0700441
442 nla = __nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, skb->len);
443
444 skb_copy_and_csum_dev(skb, nla_data(nla));
445
Rich Lanea15ff762013-02-15 11:07:43 -0800446 genlmsg_end(user_skb, upcall);
Eric W. Biederman15e47302012-09-07 20:12:54 +0000447 err = genlmsg_unicast(net, user_skb, upcall_info->portid);
Jesse Grossccb13522011-10-25 19:26:31 -0700448
449out:
450 kfree_skb(nskb);
451 return err;
452}
453
Jesse Grossccb13522011-10-25 19:26:31 -0700454static void clear_stats(struct sw_flow *flow)
455{
456 flow->used = 0;
457 flow->tcp_flags = 0;
458 flow->packet_count = 0;
459 flow->byte_count = 0;
460}
461
462static 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. */
500 flow = ovs_flow_alloc();
501 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
558static struct genl_ops dp_packet_genl_ops[] = {
559 { .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
566static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats)
567{
568 int i;
Jesse Grossccb13522011-10-25 19:26:31 -0700569
Pravin B Shelarb637e492013-10-04 00:14:23 -0700570 stats->n_flows = ovs_flow_tbl_count(&dp->table);
Jesse Grossccb13522011-10-25 19:26:31 -0700571
572 stats->n_hit = stats->n_missed = stats->n_lost = 0;
573 for_each_possible_cpu(i) {
574 const struct dp_stats_percpu *percpu_stats;
575 struct dp_stats_percpu local_stats;
576 unsigned int start;
577
578 percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
579
580 do {
581 start = u64_stats_fetch_begin_bh(&percpu_stats->sync);
582 local_stats = *percpu_stats;
583 } while (u64_stats_fetch_retry_bh(&percpu_stats->sync, start));
584
585 stats->n_hit += local_stats.n_hit;
586 stats->n_missed += local_stats.n_missed;
587 stats->n_lost += local_stats.n_lost;
588 }
589}
590
591static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
592 [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
593 [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
594 [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
595};
596
597static struct genl_family dp_flow_genl_family = {
598 .id = GENL_ID_GENERATE,
599 .hdrsize = sizeof(struct ovs_header),
600 .name = OVS_FLOW_FAMILY,
601 .version = OVS_FLOW_VERSION,
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800602 .maxattr = OVS_FLOW_ATTR_MAX,
Pravin B Shelar3a4e0d62013-04-23 07:48:48 +0000603 .netnsok = true,
604 .parallel_ops = true,
Jesse Grossccb13522011-10-25 19:26:31 -0700605};
606
607static struct genl_multicast_group ovs_dp_flow_multicast_group = {
608 .name = OVS_FLOW_MCGROUP
609};
610
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100611static size_t ovs_flow_cmd_msg_size(const struct sw_flow_actions *acts)
612{
613 return NLMSG_ALIGN(sizeof(struct ovs_header))
614 + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_KEY */
Andy Zhou03f0d912013-08-07 20:01:00 -0700615 + nla_total_size(key_attr_size()) /* OVS_FLOW_ATTR_MASK */
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100616 + nla_total_size(sizeof(struct ovs_flow_stats)) /* OVS_FLOW_ATTR_STATS */
617 + nla_total_size(1) /* OVS_FLOW_ATTR_TCP_FLAGS */
618 + nla_total_size(8) /* OVS_FLOW_ATTR_USED */
619 + nla_total_size(acts->actions_len); /* OVS_FLOW_ATTR_ACTIONS */
620}
621
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700622/* Called with ovs_mutex. */
Jesse Grossccb13522011-10-25 19:26:31 -0700623static int ovs_flow_cmd_fill_info(struct sw_flow *flow, struct datapath *dp,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000624 struct sk_buff *skb, u32 portid,
Jesse Grossccb13522011-10-25 19:26:31 -0700625 u32 seq, u32 flags, u8 cmd)
626{
627 const int skb_orig_len = skb->len;
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700628 struct nlattr *start;
Jesse Grossccb13522011-10-25 19:26:31 -0700629 struct ovs_flow_stats stats;
630 struct ovs_header *ovs_header;
631 struct nlattr *nla;
632 unsigned long used;
633 u8 tcp_flags;
634 int err;
635
Eric W. Biederman15e47302012-09-07 20:12:54 +0000636 ovs_header = genlmsg_put(skb, portid, seq, &dp_flow_genl_family, flags, cmd);
Jesse Grossccb13522011-10-25 19:26:31 -0700637 if (!ovs_header)
638 return -EMSGSIZE;
639
640 ovs_header->dp_ifindex = get_dpifindex(dp);
641
Andy Zhou03f0d912013-08-07 20:01:00 -0700642 /* Fill flow key. */
Jesse Grossccb13522011-10-25 19:26:31 -0700643 nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
644 if (!nla)
645 goto nla_put_failure;
Andy Zhou03f0d912013-08-07 20:01:00 -0700646
Pravin B Shelare6445712013-10-03 18:16:47 -0700647 err = ovs_nla_put_flow(&flow->unmasked_key, &flow->unmasked_key, skb);
Jesse Grossccb13522011-10-25 19:26:31 -0700648 if (err)
649 goto error;
650 nla_nest_end(skb, nla);
651
Andy Zhou03f0d912013-08-07 20:01:00 -0700652 nla = nla_nest_start(skb, OVS_FLOW_ATTR_MASK);
653 if (!nla)
654 goto nla_put_failure;
655
Pravin B Shelare6445712013-10-03 18:16:47 -0700656 err = ovs_nla_put_flow(&flow->key, &flow->mask->key, skb);
Andy Zhou03f0d912013-08-07 20:01:00 -0700657 if (err)
658 goto error;
659
660 nla_nest_end(skb, nla);
661
Jesse Grossccb13522011-10-25 19:26:31 -0700662 spin_lock_bh(&flow->lock);
663 used = flow->used;
664 stats.n_packets = flow->packet_count;
665 stats.n_bytes = flow->byte_count;
666 tcp_flags = flow->tcp_flags;
667 spin_unlock_bh(&flow->lock);
668
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 &&
674 nla_put(skb, OVS_FLOW_ATTR_STATS,
675 sizeof(struct ovs_flow_stats), &stats))
676 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -0700677
David S. Miller028d6a62012-03-29 23:20:48 -0400678 if (tcp_flags &&
679 nla_put_u8(skb, OVS_FLOW_ATTR_TCP_FLAGS, tcp_flags))
680 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -0700681
682 /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
683 * this is the first flow to be dumped into 'skb'. This is unusual for
684 * Netlink but individual action lists can be longer than
685 * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
686 * The userspace caller can always fetch the actions separately if it
687 * really wants them. (Most userspace callers in fact don't care.)
688 *
689 * This can only fail for dump operations because the skb is always
690 * properly sized for single flows.
691 */
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700692 start = nla_nest_start(skb, OVS_FLOW_ATTR_ACTIONS);
693 if (start) {
Pravin B Shelard57170b2013-07-30 15:39:39 -0700694 const struct sw_flow_actions *sf_acts;
695
696 sf_acts = rcu_dereference_check(flow->sf_acts,
697 lockdep_ovsl_is_held());
698
Pravin B Shelare6445712013-10-03 18:16:47 -0700699 err = ovs_nla_put_actions(sf_acts->actions,
700 sf_acts->actions_len, skb);
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700701 if (!err)
702 nla_nest_end(skb, start);
703 else {
704 if (skb_orig_len)
705 goto error;
706
707 nla_nest_cancel(skb, start);
708 }
709 } else if (skb_orig_len)
710 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -0700711
712 return genlmsg_end(skb, ovs_header);
713
714nla_put_failure:
715 err = -EMSGSIZE;
716error:
717 genlmsg_cancel(skb, ovs_header);
718 return err;
719}
720
721static struct sk_buff *ovs_flow_cmd_alloc_info(struct sw_flow *flow)
722{
723 const struct sw_flow_actions *sf_acts;
Jesse Grossccb13522011-10-25 19:26:31 -0700724
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700725 sf_acts = ovsl_dereference(flow->sf_acts);
Jesse Grossccb13522011-10-25 19:26:31 -0700726
Thomas Grafc3ff8cf2013-03-29 14:46:49 +0100727 return genlmsg_new(ovs_flow_cmd_msg_size(sf_acts), GFP_KERNEL);
Jesse Grossccb13522011-10-25 19:26:31 -0700728}
729
730static struct sk_buff *ovs_flow_cmd_build_info(struct sw_flow *flow,
731 struct datapath *dp,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000732 u32 portid, u32 seq, u8 cmd)
Jesse Grossccb13522011-10-25 19:26:31 -0700733{
734 struct sk_buff *skb;
735 int retval;
736
737 skb = ovs_flow_cmd_alloc_info(flow);
738 if (!skb)
739 return ERR_PTR(-ENOMEM);
740
Eric W. Biederman15e47302012-09-07 20:12:54 +0000741 retval = ovs_flow_cmd_fill_info(flow, dp, skb, portid, seq, 0, cmd);
Jesse Grossccb13522011-10-25 19:26:31 -0700742 BUG_ON(retval < 0);
743 return skb;
744}
745
746static int ovs_flow_cmd_new_or_set(struct sk_buff *skb, struct genl_info *info)
747{
748 struct nlattr **a = info->attrs;
749 struct ovs_header *ovs_header = info->userhdr;
Andy Zhou03f0d912013-08-07 20:01:00 -0700750 struct sw_flow_key key, masked_key;
751 struct sw_flow *flow = NULL;
752 struct sw_flow_mask mask;
Jesse Grossccb13522011-10-25 19:26:31 -0700753 struct sk_buff *reply;
754 struct datapath *dp;
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700755 struct sw_flow_actions *acts = NULL;
Andy Zhou03f0d912013-08-07 20:01:00 -0700756 struct sw_flow_match match;
Jesse Grossccb13522011-10-25 19:26:31 -0700757 int error;
Jesse Grossccb13522011-10-25 19:26:31 -0700758
759 /* Extract key. */
760 error = -EINVAL;
761 if (!a[OVS_FLOW_ATTR_KEY])
762 goto error;
Andy Zhou03f0d912013-08-07 20:01:00 -0700763
764 ovs_match_init(&match, &key, &mask);
Pravin B Shelare6445712013-10-03 18:16:47 -0700765 error = ovs_nla_get_match(&match,
766 a[OVS_FLOW_ATTR_KEY], a[OVS_FLOW_ATTR_MASK]);
Jesse Grossccb13522011-10-25 19:26:31 -0700767 if (error)
768 goto error;
769
770 /* Validate actions. */
771 if (a[OVS_FLOW_ATTR_ACTIONS]) {
Pravin B Shelare6445712013-10-03 18:16:47 -0700772 acts = ovs_nla_alloc_flow_actions(nla_len(a[OVS_FLOW_ATTR_ACTIONS]));
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700773 error = PTR_ERR(acts);
774 if (IS_ERR(acts))
Jesse Grossccb13522011-10-25 19:26:31 -0700775 goto error;
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700776
Pravin B Shelare6445712013-10-03 18:16:47 -0700777 ovs_flow_mask_key(&masked_key, &key, &mask);
778 error = ovs_nla_copy_actions(a[OVS_FLOW_ATTR_ACTIONS],
779 &masked_key, 0, &acts);
Andy Zhou03f0d912013-08-07 20:01:00 -0700780 if (error) {
781 OVS_NLERR("Flow actions may not be safe on all matching packets.\n");
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700782 goto err_kfree;
Andy Zhou03f0d912013-08-07 20:01:00 -0700783 }
Jesse Grossccb13522011-10-25 19:26:31 -0700784 } else if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW) {
785 error = -EINVAL;
786 goto error;
787 }
788
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700789 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800790 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -0700791 error = -ENODEV;
792 if (!dp)
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700793 goto err_unlock_ovs;
Jesse Grossccb13522011-10-25 19:26:31 -0700794
Andy Zhou03f0d912013-08-07 20:01:00 -0700795 /* Check if this is a duplicate flow */
Pravin B Shelarb637e492013-10-04 00:14:23 -0700796 flow = ovs_flow_tbl_lookup(&dp->table, &key);
Jesse Grossccb13522011-10-25 19:26:31 -0700797 if (!flow) {
Andy Zhou03f0d912013-08-07 20:01:00 -0700798 struct sw_flow_mask *mask_p;
Pravin B Shelare7f13322013-09-17 09:38:23 -0700799
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. */
806 flow = ovs_flow_alloc();
807 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 clear_stats(flow);
812
Andy Zhou03f0d912013-08-07 20:01:00 -0700813 flow->key = masked_key;
814 flow->unmasked_key = key;
815
816 /* Make sure mask is unique in the system */
Pravin B Shelarb637e492013-10-04 00:14:23 -0700817 mask_p = ovs_sw_flow_mask_find(&dp->table, &mask);
Andy Zhou03f0d912013-08-07 20:01:00 -0700818 if (!mask_p) {
819 /* Allocate a new mask if none exsits. */
820 mask_p = ovs_sw_flow_mask_alloc();
821 if (!mask_p)
822 goto err_flow_free;
823 mask_p->key = mask.key;
824 mask_p->range = mask.range;
Pravin B Shelarb637e492013-10-04 00:14:23 -0700825 ovs_sw_flow_mask_insert(&dp->table, mask_p);
Andy Zhou03f0d912013-08-07 20:01:00 -0700826 }
827
828 ovs_sw_flow_mask_add_ref(mask_p);
829 flow->mask = mask_p;
Jesse Grossccb13522011-10-25 19:26:31 -0700830 rcu_assign_pointer(flow->sf_acts, acts);
831
832 /* Put flow in bucket. */
Pravin B Shelarb637e492013-10-04 00:14:23 -0700833 ovs_flow_tbl_insert(&dp->table, flow);
Jesse Grossccb13522011-10-25 19:26:31 -0700834
Eric W. Biederman15e47302012-09-07 20:12:54 +0000835 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
Andy Zhou03f0d912013-08-07 20:01:00 -0700836 info->snd_seq, OVS_FLOW_CMD_NEW);
Jesse Grossccb13522011-10-25 19:26:31 -0700837 } else {
838 /* We found a matching flow. */
839 struct sw_flow_actions *old_acts;
Jesse Grossccb13522011-10-25 19:26:31 -0700840
841 /* Bail out if we're not allowed to modify an existing flow.
842 * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
843 * because Generic Netlink treats the latter as a dump
844 * request. We also accept NLM_F_EXCL in case that bug ever
845 * gets fixed.
846 */
847 error = -EEXIST;
848 if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW &&
849 info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL))
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700850 goto err_unlock_ovs;
Jesse Grossccb13522011-10-25 19:26:31 -0700851
Andy Zhou03f0d912013-08-07 20:01:00 -0700852 /* The unmasked key has to be the same for flow updates. */
853 error = -EINVAL;
Pravin B Shelare6445712013-10-03 18:16:47 -0700854 if (!ovs_flow_cmp_unmasked_key(flow, &match)) {
Andy Zhou03f0d912013-08-07 20:01:00 -0700855 OVS_NLERR("Flow modification message rejected, unmasked key does not match.\n");
856 goto err_unlock_ovs;
857 }
858
Jesse Grossccb13522011-10-25 19:26:31 -0700859 /* Update actions. */
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700860 old_acts = ovsl_dereference(flow->sf_acts);
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700861 rcu_assign_pointer(flow->sf_acts, acts);
Pravin B Shelare6445712013-10-03 18:16:47 -0700862 ovs_nla_free_flow_actions(old_acts);
Jesse Grossccb13522011-10-25 19:26:31 -0700863
Eric W. Biederman15e47302012-09-07 20:12:54 +0000864 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
Jesse Grossccb13522011-10-25 19:26:31 -0700865 info->snd_seq, OVS_FLOW_CMD_NEW);
866
867 /* Clear stats. */
868 if (a[OVS_FLOW_ATTR_CLEAR]) {
869 spin_lock_bh(&flow->lock);
870 clear_stats(flow);
871 spin_unlock_bh(&flow->lock);
872 }
873 }
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700874 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -0700875
876 if (!IS_ERR(reply))
Thomas Grafed661182013-03-29 14:46:50 +0100877 ovs_notify(reply, info, &ovs_dp_flow_multicast_group);
Jesse Grossccb13522011-10-25 19:26:31 -0700878 else
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800879 netlink_set_err(sock_net(skb->sk)->genl_sock, 0,
Jesse Grossccb13522011-10-25 19:26:31 -0700880 ovs_dp_flow_multicast_group.id, PTR_ERR(reply));
881 return 0;
882
Andy Zhou03f0d912013-08-07 20:01:00 -0700883err_flow_free:
884 ovs_flow_free(flow, false);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700885err_unlock_ovs:
886 ovs_unlock();
Pravin B Shelar74f84a52013-06-17 17:50:12 -0700887err_kfree:
888 kfree(acts);
Jesse Grossccb13522011-10-25 19:26:31 -0700889error:
890 return error;
891}
892
893static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
894{
895 struct nlattr **a = info->attrs;
896 struct ovs_header *ovs_header = info->userhdr;
897 struct sw_flow_key key;
898 struct sk_buff *reply;
899 struct sw_flow *flow;
900 struct datapath *dp;
Andy Zhou03f0d912013-08-07 20:01:00 -0700901 struct sw_flow_match match;
Jesse Grossccb13522011-10-25 19:26:31 -0700902 int err;
Jesse Grossccb13522011-10-25 19:26:31 -0700903
Andy Zhou03f0d912013-08-07 20:01:00 -0700904 if (!a[OVS_FLOW_ATTR_KEY]) {
905 OVS_NLERR("Flow get message rejected, Key attribute missing.\n");
Jesse Grossccb13522011-10-25 19:26:31 -0700906 return -EINVAL;
Andy Zhou03f0d912013-08-07 20:01:00 -0700907 }
908
909 ovs_match_init(&match, &key, NULL);
Pravin B Shelare6445712013-10-03 18:16:47 -0700910 err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL);
Jesse Grossccb13522011-10-25 19:26:31 -0700911 if (err)
912 return err;
913
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700914 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800915 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700916 if (!dp) {
917 err = -ENODEV;
918 goto unlock;
919 }
Jesse Grossccb13522011-10-25 19:26:31 -0700920
Pravin B Shelarb637e492013-10-04 00:14:23 -0700921 flow = ovs_flow_tbl_lookup(&dp->table, &key);
Pravin B Shelare6445712013-10-03 18:16:47 -0700922 if (!flow || !ovs_flow_cmp_unmasked_key(flow, &match)) {
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700923 err = -ENOENT;
924 goto unlock;
925 }
Jesse Grossccb13522011-10-25 19:26:31 -0700926
Eric W. Biederman15e47302012-09-07 20:12:54 +0000927 reply = ovs_flow_cmd_build_info(flow, dp, info->snd_portid,
Jesse Grossccb13522011-10-25 19:26:31 -0700928 info->snd_seq, OVS_FLOW_CMD_NEW);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700929 if (IS_ERR(reply)) {
930 err = PTR_ERR(reply);
931 goto unlock;
932 }
Jesse Grossccb13522011-10-25 19:26:31 -0700933
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700934 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -0700935 return genlmsg_reply(reply, info);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700936unlock:
937 ovs_unlock();
938 return err;
Jesse Grossccb13522011-10-25 19:26:31 -0700939}
940
941static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
942{
943 struct nlattr **a = info->attrs;
944 struct ovs_header *ovs_header = info->userhdr;
945 struct sw_flow_key key;
946 struct sk_buff *reply;
947 struct sw_flow *flow;
948 struct datapath *dp;
Andy Zhou03f0d912013-08-07 20:01:00 -0700949 struct sw_flow_match match;
Jesse Grossccb13522011-10-25 19:26:31 -0700950 int err;
Jesse Grossccb13522011-10-25 19:26:31 -0700951
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700952 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800953 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700954 if (!dp) {
955 err = -ENODEV;
956 goto unlock;
957 }
Pravin B Shelar46df7b82012-02-22 19:58:59 -0800958
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700959 if (!a[OVS_FLOW_ATTR_KEY]) {
Pravin B Shelarb637e492013-10-04 00:14:23 -0700960 err = ovs_flow_tbl_flush(&dp->table);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700961 goto unlock;
962 }
Andy Zhou03f0d912013-08-07 20:01:00 -0700963
964 ovs_match_init(&match, &key, NULL);
Pravin B Shelare6445712013-10-03 18:16:47 -0700965 err = ovs_nla_get_match(&match, a[OVS_FLOW_ATTR_KEY], NULL);
Jesse Grossccb13522011-10-25 19:26:31 -0700966 if (err)
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700967 goto unlock;
Jesse Grossccb13522011-10-25 19:26:31 -0700968
Pravin B Shelarb637e492013-10-04 00:14:23 -0700969 flow = ovs_flow_tbl_lookup(&dp->table, &key);
Pravin B Shelare6445712013-10-03 18:16:47 -0700970 if (!flow || !ovs_flow_cmp_unmasked_key(flow, &match)) {
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700971 err = -ENOENT;
972 goto unlock;
973 }
Jesse Grossccb13522011-10-25 19:26:31 -0700974
975 reply = ovs_flow_cmd_alloc_info(flow);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700976 if (!reply) {
977 err = -ENOMEM;
978 goto unlock;
979 }
Jesse Grossccb13522011-10-25 19:26:31 -0700980
Pravin B Shelarb637e492013-10-04 00:14:23 -0700981 ovs_flow_tbl_remove(&dp->table, flow);
Jesse Grossccb13522011-10-25 19:26:31 -0700982
Eric W. Biederman15e47302012-09-07 20:12:54 +0000983 err = ovs_flow_cmd_fill_info(flow, dp, reply, info->snd_portid,
Jesse Grossccb13522011-10-25 19:26:31 -0700984 info->snd_seq, 0, OVS_FLOW_CMD_DEL);
985 BUG_ON(err < 0);
986
Andy Zhou03f0d912013-08-07 20:01:00 -0700987 ovs_flow_free(flow, true);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700988 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -0700989
Thomas Grafed661182013-03-29 14:46:50 +0100990 ovs_notify(reply, info, &ovs_dp_flow_multicast_group);
Jesse Grossccb13522011-10-25 19:26:31 -0700991 return 0;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -0700992unlock:
993 ovs_unlock();
994 return err;
Jesse Grossccb13522011-10-25 19:26:31 -0700995}
996
997static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
998{
999 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
Pravin B Shelarb637e492013-10-04 00:14:23 -07001000 struct table_instance *ti;
Jesse Grossccb13522011-10-25 19:26:31 -07001001 struct datapath *dp;
Jesse Grossccb13522011-10-25 19:26:31 -07001002
Pravin B Shelard57170b2013-07-30 15:39:39 -07001003 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001004 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001005 if (!dp) {
Pravin B Shelard57170b2013-07-30 15:39:39 -07001006 rcu_read_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001007 return -ENODEV;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001008 }
Jesse Grossccb13522011-10-25 19:26:31 -07001009
Pravin B Shelarb637e492013-10-04 00:14:23 -07001010 ti = rcu_dereference(dp->table.ti);
Jesse Grossccb13522011-10-25 19:26:31 -07001011 for (;;) {
1012 struct sw_flow *flow;
1013 u32 bucket, obj;
1014
1015 bucket = cb->args[0];
1016 obj = cb->args[1];
Pravin B Shelarb637e492013-10-04 00:14:23 -07001017 flow = ovs_flow_tbl_dump_next(ti, &bucket, &obj);
Jesse Grossccb13522011-10-25 19:26:31 -07001018 if (!flow)
1019 break;
1020
1021 if (ovs_flow_cmd_fill_info(flow, dp, skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001022 NETLINK_CB(cb->skb).portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001023 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1024 OVS_FLOW_CMD_NEW) < 0)
1025 break;
1026
1027 cb->args[0] = bucket;
1028 cb->args[1] = obj;
1029 }
Pravin B Shelard57170b2013-07-30 15:39:39 -07001030 rcu_read_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001031 return skb->len;
1032}
1033
1034static struct genl_ops dp_flow_genl_ops[] = {
1035 { .cmd = OVS_FLOW_CMD_NEW,
1036 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1037 .policy = flow_policy,
1038 .doit = ovs_flow_cmd_new_or_set
1039 },
1040 { .cmd = OVS_FLOW_CMD_DEL,
1041 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1042 .policy = flow_policy,
1043 .doit = ovs_flow_cmd_del
1044 },
1045 { .cmd = OVS_FLOW_CMD_GET,
1046 .flags = 0, /* OK for unprivileged users. */
1047 .policy = flow_policy,
1048 .doit = ovs_flow_cmd_get,
1049 .dumpit = ovs_flow_cmd_dump
1050 },
1051 { .cmd = OVS_FLOW_CMD_SET,
1052 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1053 .policy = flow_policy,
1054 .doit = ovs_flow_cmd_new_or_set,
1055 },
1056};
1057
1058static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
1059 [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1060 [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1061};
1062
1063static struct genl_family dp_datapath_genl_family = {
1064 .id = GENL_ID_GENERATE,
1065 .hdrsize = sizeof(struct ovs_header),
1066 .name = OVS_DATAPATH_FAMILY,
1067 .version = OVS_DATAPATH_VERSION,
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001068 .maxattr = OVS_DP_ATTR_MAX,
Pravin B Shelar3a4e0d62013-04-23 07:48:48 +00001069 .netnsok = true,
1070 .parallel_ops = true,
Jesse Grossccb13522011-10-25 19:26:31 -07001071};
1072
1073static struct genl_multicast_group ovs_dp_datapath_multicast_group = {
1074 .name = OVS_DATAPATH_MCGROUP
1075};
1076
Thomas Grafc3ff8cf2013-03-29 14:46:49 +01001077static size_t ovs_dp_cmd_msg_size(void)
1078{
1079 size_t msgsize = NLMSG_ALIGN(sizeof(struct ovs_header));
1080
1081 msgsize += nla_total_size(IFNAMSIZ);
1082 msgsize += nla_total_size(sizeof(struct ovs_dp_stats));
1083
1084 return msgsize;
1085}
1086
Jesse Grossccb13522011-10-25 19:26:31 -07001087static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001088 u32 portid, u32 seq, u32 flags, u8 cmd)
Jesse Grossccb13522011-10-25 19:26:31 -07001089{
1090 struct ovs_header *ovs_header;
1091 struct ovs_dp_stats dp_stats;
1092 int err;
1093
Eric W. Biederman15e47302012-09-07 20:12:54 +00001094 ovs_header = genlmsg_put(skb, portid, seq, &dp_datapath_genl_family,
Jesse Grossccb13522011-10-25 19:26:31 -07001095 flags, cmd);
1096 if (!ovs_header)
1097 goto error;
1098
1099 ovs_header->dp_ifindex = get_dpifindex(dp);
1100
1101 rcu_read_lock();
1102 err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
1103 rcu_read_unlock();
1104 if (err)
1105 goto nla_put_failure;
1106
1107 get_dp_stats(dp, &dp_stats);
David S. Miller028d6a62012-03-29 23:20:48 -04001108 if (nla_put(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats), &dp_stats))
1109 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001110
1111 return genlmsg_end(skb, ovs_header);
1112
1113nla_put_failure:
1114 genlmsg_cancel(skb, ovs_header);
1115error:
1116 return -EMSGSIZE;
1117}
1118
Eric W. Biederman15e47302012-09-07 20:12:54 +00001119static struct sk_buff *ovs_dp_cmd_build_info(struct datapath *dp, u32 portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001120 u32 seq, u8 cmd)
1121{
1122 struct sk_buff *skb;
1123 int retval;
1124
Thomas Grafc3ff8cf2013-03-29 14:46:49 +01001125 skb = genlmsg_new(ovs_dp_cmd_msg_size(), GFP_KERNEL);
Jesse Grossccb13522011-10-25 19:26:31 -07001126 if (!skb)
1127 return ERR_PTR(-ENOMEM);
1128
Eric W. Biederman15e47302012-09-07 20:12:54 +00001129 retval = ovs_dp_cmd_fill_info(dp, skb, portid, seq, 0, cmd);
Jesse Grossccb13522011-10-25 19:26:31 -07001130 if (retval < 0) {
1131 kfree_skb(skb);
1132 return ERR_PTR(retval);
1133 }
1134 return skb;
1135}
1136
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001137/* Called with ovs_mutex. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001138static struct datapath *lookup_datapath(struct net *net,
1139 struct ovs_header *ovs_header,
Jesse Grossccb13522011-10-25 19:26:31 -07001140 struct nlattr *a[OVS_DP_ATTR_MAX + 1])
1141{
1142 struct datapath *dp;
1143
1144 if (!a[OVS_DP_ATTR_NAME])
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001145 dp = get_dp(net, ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001146 else {
1147 struct vport *vport;
1148
1149 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001150 vport = ovs_vport_locate(net, nla_data(a[OVS_DP_ATTR_NAME]));
Jesse Grossccb13522011-10-25 19:26:31 -07001151 dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
1152 rcu_read_unlock();
1153 }
1154 return dp ? dp : ERR_PTR(-ENODEV);
1155}
1156
1157static 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
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001191 dp->ports = kmalloc(DP_VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
Pravin B Shelare6445712013-10-03 18:16:47 -07001192 GFP_KERNEL);
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001193 if (!dp->ports) {
1194 err = -ENOMEM;
1195 goto err_destroy_percpu;
1196 }
1197
1198 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++)
1199 INIT_HLIST_HEAD(&dp->ports[i]);
1200
Jesse Grossccb13522011-10-25 19:26:31 -07001201 /* Set up our datapath device. */
1202 parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
1203 parms.type = OVS_VPORT_TYPE_INTERNAL;
1204 parms.options = NULL;
1205 parms.dp = dp;
1206 parms.port_no = OVSP_LOCAL;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001207 parms.upcall_portid = nla_get_u32(a[OVS_DP_ATTR_UPCALL_PID]);
Jesse Grossccb13522011-10-25 19:26:31 -07001208
1209 vport = new_vport(&parms);
1210 if (IS_ERR(vport)) {
1211 err = PTR_ERR(vport);
1212 if (err == -EBUSY)
1213 err = -EEXIST;
1214
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001215 goto err_destroy_ports_array;
Jesse Grossccb13522011-10-25 19:26:31 -07001216 }
1217
Eric W. Biederman15e47302012-09-07 20:12:54 +00001218 reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001219 info->snd_seq, OVS_DP_CMD_NEW);
1220 err = PTR_ERR(reply);
1221 if (IS_ERR(reply))
1222 goto err_destroy_local_port;
1223
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001224 ovs_net = net_generic(ovs_dp_get_net(dp), ovs_net_id);
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001225 list_add_tail_rcu(&dp->list_node, &ovs_net->dps);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001226
1227 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001228
Thomas Grafed661182013-03-29 14:46:50 +01001229 ovs_notify(reply, info, &ovs_dp_datapath_multicast_group);
Jesse Grossccb13522011-10-25 19:26:31 -07001230 return 0;
1231
1232err_destroy_local_port:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001233 ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001234err_destroy_ports_array:
1235 kfree(dp->ports);
Jesse Grossccb13522011-10-25 19:26:31 -07001236err_destroy_percpu:
1237 free_percpu(dp->stats_percpu);
1238err_destroy_table:
Pravin B Shelarb637e492013-10-04 00:14:23 -07001239 ovs_flow_tbl_destroy(&dp->table, false);
Jesse Grossccb13522011-10-25 19:26:31 -07001240err_free_dp:
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001241 release_net(ovs_dp_get_net(dp));
Jesse Grossccb13522011-10-25 19:26:31 -07001242 kfree(dp);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001243err_unlock_ovs:
1244 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001245err:
1246 return err;
1247}
1248
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001249/* Called with ovs_mutex. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001250static void __dp_destroy(struct datapath *dp)
Jesse Grossccb13522011-10-25 19:26:31 -07001251{
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001252 int i;
Jesse Grossccb13522011-10-25 19:26:31 -07001253
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001254 for (i = 0; i < DP_VPORT_HASH_BUCKETS; i++) {
1255 struct vport *vport;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001256 struct hlist_node *n;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001257
Sasha Levinb67bfe02013-02-27 17:06:00 -08001258 hlist_for_each_entry_safe(vport, n, &dp->ports[i], dp_hash_node)
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001259 if (vport->port_no != OVSP_LOCAL)
1260 ovs_dp_detach_port(vport);
1261 }
Jesse Grossccb13522011-10-25 19:26:31 -07001262
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001263 list_del_rcu(&dp->list_node);
Jesse Grossccb13522011-10-25 19:26:31 -07001264
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001265 /* OVSP_LOCAL is datapath internal port. We need to make sure that
1266 * all port in datapath are destroyed first before freeing datapath.
Jesse Grossccb13522011-10-25 19:26:31 -07001267 */
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001268 ovs_dp_detach_port(ovs_vport_ovsl(dp, OVSP_LOCAL));
Jesse Grossccb13522011-10-25 19:26:31 -07001269
1270 call_rcu(&dp->rcu, destroy_dp_rcu);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001271}
1272
1273static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
1274{
1275 struct sk_buff *reply;
1276 struct datapath *dp;
1277 int err;
1278
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001279 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001280 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
1281 err = PTR_ERR(dp);
1282 if (IS_ERR(dp))
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001283 goto unlock;
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001284
Eric W. Biederman15e47302012-09-07 20:12:54 +00001285 reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001286 info->snd_seq, OVS_DP_CMD_DEL);
1287 err = PTR_ERR(reply);
1288 if (IS_ERR(reply))
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001289 goto unlock;
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001290
1291 __dp_destroy(dp);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001292 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001293
Thomas Grafed661182013-03-29 14:46:50 +01001294 ovs_notify(reply, info, &ovs_dp_datapath_multicast_group);
Jesse Grossccb13522011-10-25 19:26:31 -07001295
1296 return 0;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001297unlock:
1298 ovs_unlock();
1299 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001300}
1301
1302static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
1303{
1304 struct sk_buff *reply;
1305 struct datapath *dp;
1306 int err;
1307
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001308 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001309 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001310 err = PTR_ERR(dp);
Jesse Grossccb13522011-10-25 19:26:31 -07001311 if (IS_ERR(dp))
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001312 goto unlock;
Jesse Grossccb13522011-10-25 19:26:31 -07001313
Eric W. Biederman15e47302012-09-07 20:12:54 +00001314 reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001315 info->snd_seq, OVS_DP_CMD_NEW);
1316 if (IS_ERR(reply)) {
1317 err = PTR_ERR(reply);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001318 netlink_set_err(sock_net(skb->sk)->genl_sock, 0,
Jesse Grossccb13522011-10-25 19:26:31 -07001319 ovs_dp_datapath_multicast_group.id, err);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001320 err = 0;
1321 goto unlock;
Jesse Grossccb13522011-10-25 19:26:31 -07001322 }
1323
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001324 ovs_unlock();
Thomas Grafed661182013-03-29 14:46:50 +01001325 ovs_notify(reply, info, &ovs_dp_datapath_multicast_group);
Jesse Grossccb13522011-10-25 19:26:31 -07001326
1327 return 0;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001328unlock:
1329 ovs_unlock();
1330 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001331}
1332
1333static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
1334{
1335 struct sk_buff *reply;
1336 struct datapath *dp;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001337 int err;
Jesse Grossccb13522011-10-25 19:26:31 -07001338
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001339 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001340 dp = lookup_datapath(sock_net(skb->sk), info->userhdr, info->attrs);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001341 if (IS_ERR(dp)) {
1342 err = PTR_ERR(dp);
1343 goto unlock;
1344 }
Jesse Grossccb13522011-10-25 19:26:31 -07001345
Eric W. Biederman15e47302012-09-07 20:12:54 +00001346 reply = ovs_dp_cmd_build_info(dp, info->snd_portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001347 info->snd_seq, OVS_DP_CMD_NEW);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001348 if (IS_ERR(reply)) {
1349 err = PTR_ERR(reply);
1350 goto unlock;
1351 }
Jesse Grossccb13522011-10-25 19:26:31 -07001352
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001353 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001354 return genlmsg_reply(reply, info);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001355
1356unlock:
1357 ovs_unlock();
1358 return err;
Jesse Grossccb13522011-10-25 19:26:31 -07001359}
1360
1361static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1362{
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001363 struct ovs_net *ovs_net = net_generic(sock_net(skb->sk), ovs_net_id);
Jesse Grossccb13522011-10-25 19:26:31 -07001364 struct datapath *dp;
1365 int skip = cb->args[0];
1366 int i = 0;
1367
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001368 rcu_read_lock();
1369 list_for_each_entry_rcu(dp, &ovs_net->dps, list_node) {
Ben Pfaff77676fd2012-01-17 13:33:39 +00001370 if (i >= skip &&
Eric W. Biederman15e47302012-09-07 20:12:54 +00001371 ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001372 cb->nlh->nlmsg_seq, NLM_F_MULTI,
1373 OVS_DP_CMD_NEW) < 0)
1374 break;
1375 i++;
1376 }
Pravin B Shelar59a35d62013-07-30 15:42:19 -07001377 rcu_read_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001378
1379 cb->args[0] = i;
1380
1381 return skb->len;
1382}
1383
1384static struct genl_ops dp_datapath_genl_ops[] = {
1385 { .cmd = OVS_DP_CMD_NEW,
1386 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1387 .policy = datapath_policy,
1388 .doit = ovs_dp_cmd_new
1389 },
1390 { .cmd = OVS_DP_CMD_DEL,
1391 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1392 .policy = datapath_policy,
1393 .doit = ovs_dp_cmd_del
1394 },
1395 { .cmd = OVS_DP_CMD_GET,
1396 .flags = 0, /* OK for unprivileged users. */
1397 .policy = datapath_policy,
1398 .doit = ovs_dp_cmd_get,
1399 .dumpit = ovs_dp_cmd_dump
1400 },
1401 { .cmd = OVS_DP_CMD_SET,
1402 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1403 .policy = datapath_policy,
1404 .doit = ovs_dp_cmd_set,
1405 },
1406};
1407
1408static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
1409 [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
1410 [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
1411 [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
1412 [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
1413 [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
1414 [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
1415};
1416
1417static struct genl_family dp_vport_genl_family = {
1418 .id = GENL_ID_GENERATE,
1419 .hdrsize = sizeof(struct ovs_header),
1420 .name = OVS_VPORT_FAMILY,
1421 .version = OVS_VPORT_VERSION,
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001422 .maxattr = OVS_VPORT_ATTR_MAX,
Pravin B Shelar3a4e0d62013-04-23 07:48:48 +00001423 .netnsok = true,
1424 .parallel_ops = true,
Jesse Grossccb13522011-10-25 19:26:31 -07001425};
1426
1427struct genl_multicast_group ovs_dp_vport_multicast_group = {
1428 .name = OVS_VPORT_MCGROUP
1429};
1430
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001431/* Called with ovs_mutex or RCU read lock. */
Jesse Grossccb13522011-10-25 19:26:31 -07001432static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001433 u32 portid, u32 seq, u32 flags, u8 cmd)
Jesse Grossccb13522011-10-25 19:26:31 -07001434{
1435 struct ovs_header *ovs_header;
1436 struct ovs_vport_stats vport_stats;
1437 int err;
1438
Eric W. Biederman15e47302012-09-07 20:12:54 +00001439 ovs_header = genlmsg_put(skb, portid, seq, &dp_vport_genl_family,
Jesse Grossccb13522011-10-25 19:26:31 -07001440 flags, cmd);
1441 if (!ovs_header)
1442 return -EMSGSIZE;
1443
1444 ovs_header->dp_ifindex = get_dpifindex(vport->dp);
1445
David S. Miller028d6a62012-03-29 23:20:48 -04001446 if (nla_put_u32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no) ||
1447 nla_put_u32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type) ||
1448 nla_put_string(skb, OVS_VPORT_ATTR_NAME, vport->ops->get_name(vport)) ||
Eric W. Biederman15e47302012-09-07 20:12:54 +00001449 nla_put_u32(skb, OVS_VPORT_ATTR_UPCALL_PID, vport->upcall_portid))
David S. Miller028d6a62012-03-29 23:20:48 -04001450 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001451
1452 ovs_vport_get_stats(vport, &vport_stats);
David S. Miller028d6a62012-03-29 23:20:48 -04001453 if (nla_put(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
1454 &vport_stats))
1455 goto nla_put_failure;
Jesse Grossccb13522011-10-25 19:26:31 -07001456
1457 err = ovs_vport_get_options(vport, skb);
1458 if (err == -EMSGSIZE)
1459 goto error;
1460
1461 return genlmsg_end(skb, ovs_header);
1462
1463nla_put_failure:
1464 err = -EMSGSIZE;
1465error:
1466 genlmsg_cancel(skb, ovs_header);
1467 return err;
1468}
1469
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001470/* Called with ovs_mutex or RCU read lock. */
Eric W. Biederman15e47302012-09-07 20:12:54 +00001471struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 portid,
Jesse Grossccb13522011-10-25 19:26:31 -07001472 u32 seq, u8 cmd)
1473{
1474 struct sk_buff *skb;
1475 int retval;
1476
1477 skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
1478 if (!skb)
1479 return ERR_PTR(-ENOMEM);
1480
Eric W. Biederman15e47302012-09-07 20:12:54 +00001481 retval = ovs_vport_cmd_fill_info(vport, skb, portid, seq, 0, cmd);
Jesse Grossa9341512013-03-26 15:48:38 -07001482 BUG_ON(retval < 0);
1483
Jesse Grossccb13522011-10-25 19:26:31 -07001484 return skb;
1485}
1486
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001487/* Called with ovs_mutex or RCU read lock. */
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001488static struct vport *lookup_vport(struct net *net,
1489 struct ovs_header *ovs_header,
Jesse Grossccb13522011-10-25 19:26:31 -07001490 struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
1491{
1492 struct datapath *dp;
1493 struct vport *vport;
1494
1495 if (a[OVS_VPORT_ATTR_NAME]) {
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001496 vport = ovs_vport_locate(net, nla_data(a[OVS_VPORT_ATTR_NAME]));
Jesse Grossccb13522011-10-25 19:26:31 -07001497 if (!vport)
1498 return ERR_PTR(-ENODEV);
Ben Pfaff651a68e2012-03-06 15:04:04 -08001499 if (ovs_header->dp_ifindex &&
1500 ovs_header->dp_ifindex != get_dpifindex(vport->dp))
1501 return ERR_PTR(-ENODEV);
Jesse Grossccb13522011-10-25 19:26:31 -07001502 return vport;
1503 } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
1504 u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1505
1506 if (port_no >= DP_MAX_PORTS)
1507 return ERR_PTR(-EFBIG);
1508
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001509 dp = get_dp(net, ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001510 if (!dp)
1511 return ERR_PTR(-ENODEV);
1512
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001513 vport = ovs_vport_ovsl_rcu(dp, port_no);
Jesse Grossccb13522011-10-25 19:26:31 -07001514 if (!vport)
Jarno Rajahalme14408db2013-01-09 14:27:35 -08001515 return ERR_PTR(-ENODEV);
Jesse Grossccb13522011-10-25 19:26:31 -07001516 return vport;
1517 } else
1518 return ERR_PTR(-EINVAL);
1519}
1520
1521static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
1522{
1523 struct nlattr **a = info->attrs;
1524 struct ovs_header *ovs_header = info->userhdr;
1525 struct vport_parms parms;
1526 struct sk_buff *reply;
1527 struct vport *vport;
1528 struct datapath *dp;
1529 u32 port_no;
1530 int err;
1531
1532 err = -EINVAL;
1533 if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
1534 !a[OVS_VPORT_ATTR_UPCALL_PID])
1535 goto exit;
1536
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001537 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001538 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001539 err = -ENODEV;
1540 if (!dp)
1541 goto exit_unlock;
1542
1543 if (a[OVS_VPORT_ATTR_PORT_NO]) {
1544 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
1545
1546 err = -EFBIG;
1547 if (port_no >= DP_MAX_PORTS)
1548 goto exit_unlock;
1549
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001550 vport = ovs_vport_ovsl(dp, port_no);
Jesse Grossccb13522011-10-25 19:26:31 -07001551 err = -EBUSY;
1552 if (vport)
1553 goto exit_unlock;
1554 } else {
1555 for (port_no = 1; ; port_no++) {
1556 if (port_no >= DP_MAX_PORTS) {
1557 err = -EFBIG;
1558 goto exit_unlock;
1559 }
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001560 vport = ovs_vport_ovsl(dp, port_no);
Jesse Grossccb13522011-10-25 19:26:31 -07001561 if (!vport)
1562 break;
1563 }
1564 }
1565
1566 parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
1567 parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
1568 parms.options = a[OVS_VPORT_ATTR_OPTIONS];
1569 parms.dp = dp;
1570 parms.port_no = port_no;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001571 parms.upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
Jesse Grossccb13522011-10-25 19:26:31 -07001572
1573 vport = new_vport(&parms);
1574 err = PTR_ERR(vport);
1575 if (IS_ERR(vport))
1576 goto exit_unlock;
1577
Rich Lanecb7c5bd2013-02-08 13:18:01 -08001578 err = 0;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001579 reply = ovs_vport_cmd_build_info(vport, info->snd_portid, info->snd_seq,
Jesse Grossccb13522011-10-25 19:26:31 -07001580 OVS_VPORT_CMD_NEW);
1581 if (IS_ERR(reply)) {
1582 err = PTR_ERR(reply);
1583 ovs_dp_detach_port(vport);
1584 goto exit_unlock;
1585 }
Thomas Grafed661182013-03-29 14:46:50 +01001586
1587 ovs_notify(reply, info, &ovs_dp_vport_multicast_group);
Jesse Grossccb13522011-10-25 19:26:31 -07001588
1589exit_unlock:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001590 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001591exit:
1592 return err;
1593}
1594
1595static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
1596{
1597 struct nlattr **a = info->attrs;
1598 struct sk_buff *reply;
1599 struct vport *vport;
1600 int err;
1601
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001602 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001603 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
Jesse Grossccb13522011-10-25 19:26:31 -07001604 err = PTR_ERR(vport);
1605 if (IS_ERR(vport))
1606 goto exit_unlock;
1607
Jesse Grossccb13522011-10-25 19:26:31 -07001608 if (a[OVS_VPORT_ATTR_TYPE] &&
Jesse Grossf44f3402013-05-13 08:15:26 -07001609 nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type) {
Jesse Grossccb13522011-10-25 19:26:31 -07001610 err = -EINVAL;
Jesse Grossf44f3402013-05-13 08:15:26 -07001611 goto exit_unlock;
1612 }
Jesse Grossccb13522011-10-25 19:26:31 -07001613
Jesse Grossa9341512013-03-26 15:48:38 -07001614 reply = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
1615 if (!reply) {
1616 err = -ENOMEM;
1617 goto exit_unlock;
1618 }
1619
Jesse Grossf44f3402013-05-13 08:15:26 -07001620 if (a[OVS_VPORT_ATTR_OPTIONS]) {
Jesse Grossccb13522011-10-25 19:26:31 -07001621 err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
Jesse Grossf44f3402013-05-13 08:15:26 -07001622 if (err)
1623 goto exit_free;
1624 }
Jesse Grossa9341512013-03-26 15:48:38 -07001625
Ansis Atteka03fbf8b2012-04-09 12:12:12 -07001626 if (a[OVS_VPORT_ATTR_UPCALL_PID])
Eric W. Biederman15e47302012-09-07 20:12:54 +00001627 vport->upcall_portid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
Jesse Grossccb13522011-10-25 19:26:31 -07001628
Jesse Grossa9341512013-03-26 15:48:38 -07001629 err = ovs_vport_cmd_fill_info(vport, reply, info->snd_portid,
1630 info->snd_seq, 0, OVS_VPORT_CMD_NEW);
1631 BUG_ON(err < 0);
Jesse Grossccb13522011-10-25 19:26:31 -07001632
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001633 ovs_unlock();
Thomas Grafed661182013-03-29 14:46:50 +01001634 ovs_notify(reply, info, &ovs_dp_vport_multicast_group);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001635 return 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001636
Jesse Grossa9341512013-03-26 15:48:38 -07001637exit_free:
1638 kfree_skb(reply);
Jesse Grossccb13522011-10-25 19:26:31 -07001639exit_unlock:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001640 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001641 return err;
1642}
1643
1644static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
1645{
1646 struct nlattr **a = info->attrs;
1647 struct sk_buff *reply;
1648 struct vport *vport;
1649 int err;
1650
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001651 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001652 vport = lookup_vport(sock_net(skb->sk), info->userhdr, a);
Jesse Grossccb13522011-10-25 19:26:31 -07001653 err = PTR_ERR(vport);
1654 if (IS_ERR(vport))
1655 goto exit_unlock;
1656
1657 if (vport->port_no == OVSP_LOCAL) {
1658 err = -EINVAL;
1659 goto exit_unlock;
1660 }
1661
Pravin B Shelar74f84a52013-06-17 17:50:12 -07001662 reply = ovs_vport_cmd_build_info(vport, info->snd_portid,
1663 info->snd_seq, OVS_VPORT_CMD_DEL);
Jesse Grossccb13522011-10-25 19:26:31 -07001664 err = PTR_ERR(reply);
1665 if (IS_ERR(reply))
1666 goto exit_unlock;
1667
Rich Lane734907e2013-02-08 09:30:23 -08001668 err = 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001669 ovs_dp_detach_port(vport);
1670
Thomas Grafed661182013-03-29 14:46:50 +01001671 ovs_notify(reply, info, &ovs_dp_vport_multicast_group);
Jesse Grossccb13522011-10-25 19:26:31 -07001672
1673exit_unlock:
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001674 ovs_unlock();
Jesse Grossccb13522011-10-25 19:26:31 -07001675 return err;
1676}
1677
1678static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
1679{
1680 struct nlattr **a = info->attrs;
1681 struct ovs_header *ovs_header = info->userhdr;
1682 struct sk_buff *reply;
1683 struct vport *vport;
1684 int err;
1685
1686 rcu_read_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001687 vport = lookup_vport(sock_net(skb->sk), ovs_header, a);
Jesse Grossccb13522011-10-25 19:26:31 -07001688 err = PTR_ERR(vport);
1689 if (IS_ERR(vport))
1690 goto exit_unlock;
1691
Pravin B Shelar74f84a52013-06-17 17:50:12 -07001692 reply = ovs_vport_cmd_build_info(vport, info->snd_portid,
1693 info->snd_seq, OVS_VPORT_CMD_NEW);
Jesse Grossccb13522011-10-25 19:26:31 -07001694 err = PTR_ERR(reply);
1695 if (IS_ERR(reply))
1696 goto exit_unlock;
1697
1698 rcu_read_unlock();
1699
1700 return genlmsg_reply(reply, info);
1701
1702exit_unlock:
1703 rcu_read_unlock();
1704 return err;
1705}
1706
1707static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
1708{
1709 struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
1710 struct datapath *dp;
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001711 int bucket = cb->args[0], skip = cb->args[1];
1712 int i, j = 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001713
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001714 dp = get_dp(sock_net(skb->sk), ovs_header->dp_ifindex);
Jesse Grossccb13522011-10-25 19:26:31 -07001715 if (!dp)
1716 return -ENODEV;
1717
1718 rcu_read_lock();
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001719 for (i = bucket; i < DP_VPORT_HASH_BUCKETS; i++) {
Jesse Grossccb13522011-10-25 19:26:31 -07001720 struct vport *vport;
1721
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001722 j = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001723 hlist_for_each_entry_rcu(vport, &dp->ports[i], dp_hash_node) {
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001724 if (j >= skip &&
1725 ovs_vport_cmd_fill_info(vport, skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001726 NETLINK_CB(cb->skb).portid,
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001727 cb->nlh->nlmsg_seq,
1728 NLM_F_MULTI,
1729 OVS_VPORT_CMD_NEW) < 0)
1730 goto out;
Jesse Grossccb13522011-10-25 19:26:31 -07001731
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001732 j++;
1733 }
1734 skip = 0;
Jesse Grossccb13522011-10-25 19:26:31 -07001735 }
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001736out:
Jesse Grossccb13522011-10-25 19:26:31 -07001737 rcu_read_unlock();
1738
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001739 cb->args[0] = i;
1740 cb->args[1] = j;
Jesse Grossccb13522011-10-25 19:26:31 -07001741
Pravin B Shelar15eac2a2012-08-23 12:40:54 -07001742 return skb->len;
Jesse Grossccb13522011-10-25 19:26:31 -07001743}
1744
Jesse Grossccb13522011-10-25 19:26:31 -07001745static struct genl_ops dp_vport_genl_ops[] = {
1746 { .cmd = OVS_VPORT_CMD_NEW,
1747 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1748 .policy = vport_policy,
1749 .doit = ovs_vport_cmd_new
1750 },
1751 { .cmd = OVS_VPORT_CMD_DEL,
1752 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1753 .policy = vport_policy,
1754 .doit = ovs_vport_cmd_del
1755 },
1756 { .cmd = OVS_VPORT_CMD_GET,
1757 .flags = 0, /* OK for unprivileged users. */
1758 .policy = vport_policy,
1759 .doit = ovs_vport_cmd_get,
1760 .dumpit = ovs_vport_cmd_dump
1761 },
1762 { .cmd = OVS_VPORT_CMD_SET,
1763 .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
1764 .policy = vport_policy,
1765 .doit = ovs_vport_cmd_set,
1766 },
1767};
1768
1769struct genl_family_and_ops {
1770 struct genl_family *family;
1771 struct genl_ops *ops;
1772 int n_ops;
1773 struct genl_multicast_group *group;
1774};
1775
1776static const struct genl_family_and_ops dp_genl_families[] = {
1777 { &dp_datapath_genl_family,
1778 dp_datapath_genl_ops, ARRAY_SIZE(dp_datapath_genl_ops),
1779 &ovs_dp_datapath_multicast_group },
1780 { &dp_vport_genl_family,
1781 dp_vport_genl_ops, ARRAY_SIZE(dp_vport_genl_ops),
1782 &ovs_dp_vport_multicast_group },
1783 { &dp_flow_genl_family,
1784 dp_flow_genl_ops, ARRAY_SIZE(dp_flow_genl_ops),
1785 &ovs_dp_flow_multicast_group },
1786 { &dp_packet_genl_family,
1787 dp_packet_genl_ops, ARRAY_SIZE(dp_packet_genl_ops),
1788 NULL },
1789};
1790
1791static void dp_unregister_genl(int n_families)
1792{
1793 int i;
1794
1795 for (i = 0; i < n_families; i++)
1796 genl_unregister_family(dp_genl_families[i].family);
1797}
1798
1799static int dp_register_genl(void)
1800{
1801 int n_registered;
1802 int err;
1803 int i;
1804
1805 n_registered = 0;
1806 for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
1807 const struct genl_family_and_ops *f = &dp_genl_families[i];
1808
1809 err = genl_register_family_with_ops(f->family, f->ops,
1810 f->n_ops);
1811 if (err)
1812 goto error;
1813 n_registered++;
1814
1815 if (f->group) {
1816 err = genl_register_mc_group(f->family, f->group);
1817 if (err)
1818 goto error;
1819 }
1820 }
1821
1822 return 0;
1823
1824error:
1825 dp_unregister_genl(n_registered);
1826 return err;
1827}
1828
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001829static int __net_init ovs_init_net(struct net *net)
1830{
1831 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
1832
1833 INIT_LIST_HEAD(&ovs_net->dps);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001834 INIT_WORK(&ovs_net->dp_notify_work, ovs_dp_notify_wq);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001835 return 0;
1836}
1837
1838static void __net_exit ovs_exit_net(struct net *net)
1839{
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001840 struct datapath *dp, *dp_next;
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001841 struct ovs_net *ovs_net = net_generic(net, ovs_net_id);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001842
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001843 ovs_lock();
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001844 list_for_each_entry_safe(dp, dp_next, &ovs_net->dps, list_node)
1845 __dp_destroy(dp);
Pravin B Shelar8e4e1712013-04-15 13:23:03 -07001846 ovs_unlock();
1847
1848 cancel_work_sync(&ovs_net->dp_notify_work);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001849}
1850
1851static struct pernet_operations ovs_net_ops = {
1852 .init = ovs_init_net,
1853 .exit = ovs_exit_net,
1854 .id = &ovs_net_id,
1855 .size = sizeof(struct ovs_net),
1856};
1857
Jesse Grossccb13522011-10-25 19:26:31 -07001858static int __init dp_init(void)
1859{
Jesse Grossccb13522011-10-25 19:26:31 -07001860 int err;
1861
YOSHIFUJI Hideaki / 吉藤英明3523b292013-01-09 07:19:55 +00001862 BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > FIELD_SIZEOF(struct sk_buff, cb));
Jesse Grossccb13522011-10-25 19:26:31 -07001863
1864 pr_info("Open vSwitch switching datapath\n");
1865
1866 err = ovs_flow_init();
1867 if (err)
1868 goto error;
1869
1870 err = ovs_vport_init();
1871 if (err)
1872 goto error_flow_exit;
1873
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001874 err = register_pernet_device(&ovs_net_ops);
Jesse Grossccb13522011-10-25 19:26:31 -07001875 if (err)
1876 goto error_vport_exit;
1877
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001878 err = register_netdevice_notifier(&ovs_dp_device_notifier);
1879 if (err)
1880 goto error_netns_exit;
1881
Jesse Grossccb13522011-10-25 19:26:31 -07001882 err = dp_register_genl();
1883 if (err < 0)
1884 goto error_unreg_notifier;
1885
Jesse Grossccb13522011-10-25 19:26:31 -07001886 return 0;
1887
1888error_unreg_notifier:
1889 unregister_netdevice_notifier(&ovs_dp_device_notifier);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001890error_netns_exit:
1891 unregister_pernet_device(&ovs_net_ops);
Jesse Grossccb13522011-10-25 19:26:31 -07001892error_vport_exit:
1893 ovs_vport_exit();
1894error_flow_exit:
1895 ovs_flow_exit();
1896error:
1897 return err;
1898}
1899
1900static void dp_cleanup(void)
1901{
Jesse Grossccb13522011-10-25 19:26:31 -07001902 dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
1903 unregister_netdevice_notifier(&ovs_dp_device_notifier);
Pravin B Shelar46df7b82012-02-22 19:58:59 -08001904 unregister_pernet_device(&ovs_net_ops);
1905 rcu_barrier();
Jesse Grossccb13522011-10-25 19:26:31 -07001906 ovs_vport_exit();
1907 ovs_flow_exit();
1908}
1909
1910module_init(dp_init);
1911module_exit(dp_cleanup);
1912
1913MODULE_DESCRIPTION("Open vSwitch switching datapath");
1914MODULE_LICENSE("GPL");