d945e4e6de9328b08f6befffbf3794e82fab5083
[sliver-openvswitch.git] / datapath / brcompat.c
1 /*
2  * Copyright (c) 2009 Nicira Networks.
3  * Distributed under the terms of the GNU GPL version 2.
4  *
5  * Significant portions of this file may be copied from parts of the Linux
6  * kernel, by Linus Torvalds and others.
7  */
8
9 #include <linux/kernel.h>
10 #include <asm/uaccess.h>
11 #include <linux/completion.h>
12 #include <linux/delay.h>
13 #include <linux/etherdevice.h>
14 #include <linux/if_bridge.h>
15 #include <linux/rculist.h>
16 #include <linux/netdevice.h>
17 #include <linux/rtnetlink.h>
18 #include <net/genetlink.h>
19
20 #include "compat.h"
21 #include "openvswitch/brcompat-netlink.h"
22 #include "brc_procfs.h"
23 #include "brc_sysfs.h"
24 #include "datapath.h"
25 #include "dp_dev.h"
26
27 static struct genl_family brc_genl_family;
28 static struct genl_multicast_group brc_mc_group;
29
30 /* Time to wait for ovs-vswitchd to respond to a datapath action, in
31  * jiffies. */
32 #define BRC_TIMEOUT (HZ * 5)
33
34 /* Mutex to serialize ovs-brcompatd callbacks.  (Some callbacks naturally hold
35  * br_ioctl_mutex, others hold rtnl_lock, but we can't take the former
36  * ourselves and we don't want to hold the latter over a potentially long
37  * period of time.) */
38 static DEFINE_MUTEX(brc_serial);
39
40 /* Userspace communication. */
41 static DEFINE_SPINLOCK(brc_lock);    /* Ensure atomic access to these vars. */
42 static DECLARE_COMPLETION(brc_done); /* Userspace signaled operation done? */
43 static struct sk_buff *brc_reply;    /* Reply from userspace. */
44 static u32 brc_seq;                  /* Sequence number for current op. */
45
46 static struct sk_buff *brc_send_command(struct sk_buff *, struct nlattr **attrs);
47 static int brc_send_simple_command(struct sk_buff *);
48
49 static int
50 get_dp_ifindices(int *indices, int num)
51 {
52         int i, index = 0;
53
54         rcu_read_lock();
55         for (i=0; i < ODP_MAX && index < num; i++) {
56                 struct datapath *dp = get_dp(i);
57                 if (!dp)
58                         continue;
59                 indices[index++] = dp->ports[ODPP_LOCAL]->dev->ifindex;
60         }
61         rcu_read_unlock();
62
63         return index;
64 }
65
66 static void
67 get_port_ifindices(struct datapath *dp, int *ifindices, int num)
68 {
69         struct net_bridge_port *p;
70
71         rcu_read_lock();
72         list_for_each_entry_rcu (p, &dp->port_list, node) {
73                 if (p->port_no < num)
74                         ifindices[p->port_no] = p->dev->ifindex;
75         }
76         rcu_read_unlock();
77 }
78
79 static struct sk_buff *
80 brc_make_request(int op, const char *bridge, const char *port)
81 {
82         struct sk_buff *skb = genlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
83         if (!skb)
84                 goto error;
85
86         genlmsg_put(skb, 0, 0, &brc_genl_family, 0, op);
87         NLA_PUT_STRING(skb, BRC_GENL_A_DP_NAME, bridge);
88         if (port)
89                 NLA_PUT_STRING(skb, BRC_GENL_A_PORT_NAME, port);
90         return skb;
91
92 nla_put_failure:
93         kfree_skb(skb);
94 error:
95         return NULL;
96 }
97
98 static int brc_send_simple_command(struct sk_buff *request)
99 {
100         struct nlattr *attrs[BRC_GENL_A_MAX + 1];
101         struct sk_buff *reply;
102         int error;
103
104         reply = brc_send_command(request, attrs);
105         if (IS_ERR(reply))
106                 return PTR_ERR(reply);
107
108         error = nla_get_u32(attrs[BRC_GENL_A_ERR_CODE]);
109         kfree_skb(reply);
110         return -error;
111 }
112
113 static int brc_add_del_bridge(char __user *uname, int add)
114 {
115         struct sk_buff *request;
116         char name[IFNAMSIZ];
117
118         if (copy_from_user(name, uname, IFNAMSIZ))
119                 return -EFAULT;
120
121         name[IFNAMSIZ - 1] = 0;
122         request = brc_make_request(add ? BRC_GENL_C_DP_ADD : BRC_GENL_C_DP_DEL,
123                                    name, NULL);
124         if (!request)
125                 return -ENOMEM;
126
127         return brc_send_simple_command(request);
128 }
129
130 static int brc_get_bridges(int __user *uindices, int n)
131 {
132         int *indices;
133         int ret;
134
135         if (n >= 2048)
136                 return -ENOMEM;
137
138         indices = kcalloc(n, sizeof(int), GFP_KERNEL);
139         if (indices == NULL)
140                 return -ENOMEM;
141
142         n = get_dp_ifindices(indices, n);
143
144         ret = copy_to_user(uindices, indices, n * sizeof(int)) ? -EFAULT : n;
145
146         kfree(indices);
147         return ret;
148 }
149
150 /* Legacy deviceless bridge ioctl's.  Called with br_ioctl_mutex. */
151 static int
152 old_deviceless(void __user *uarg)
153 {
154         unsigned long args[3];
155
156         if (copy_from_user(args, uarg, sizeof(args)))
157                 return -EFAULT;
158
159         switch (args[0]) {
160         case BRCTL_GET_BRIDGES:
161                 return brc_get_bridges((int __user *)args[1], args[2]);
162
163         case BRCTL_ADD_BRIDGE:
164                 return brc_add_del_bridge((void __user *)args[1], 1);
165         case BRCTL_DEL_BRIDGE:
166                 return brc_add_del_bridge((void __user *)args[1], 0);
167         }
168
169         return -EOPNOTSUPP;
170 }
171
172 /* Called with the br_ioctl_mutex. */
173 static int
174 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
175 brc_ioctl_deviceless_stub(unsigned int cmd, void __user *uarg)
176 #else
177 brc_ioctl_deviceless_stub(struct net *net, unsigned int cmd, void __user *uarg)
178 #endif
179 {
180         switch (cmd) {
181         case SIOCGIFBR:
182         case SIOCSIFBR:
183                 return old_deviceless(uarg);
184
185         case SIOCBRADDBR:
186                 return brc_add_del_bridge(uarg, 1);
187         case SIOCBRDELBR:
188                 return brc_add_del_bridge(uarg, 0);
189         }
190
191         return -EOPNOTSUPP;
192 }
193
194 static int
195 brc_add_del_port(struct net_device *dev, int port_ifindex, int add)
196 {
197         struct sk_buff *request;
198         struct net_device *port;
199         int err;
200
201         port = __dev_get_by_index(&init_net, port_ifindex);
202         if (!port)
203                 return -EINVAL;
204
205         /* Save name of dev and port because there's a race between the
206          * rtnl_unlock() and the brc_send_simple_command(). */
207         request = brc_make_request(add ? BRC_GENL_C_PORT_ADD : BRC_GENL_C_PORT_DEL,
208                                    dev->name, port->name);
209         if (!request)
210                 return -ENOMEM;
211
212         rtnl_unlock();
213         err = brc_send_simple_command(request);
214         rtnl_lock();
215
216         return err;
217 }
218
219 static int
220 brc_get_bridge_info(struct net_device *dev, struct __bridge_info __user *ub)
221 {
222         struct __bridge_info b;
223         u64 id = 0;
224         int i;
225
226         memset(&b, 0, sizeof(struct __bridge_info));
227
228         for (i=0; i<ETH_ALEN; i++)
229                 id |= (u64)dev->dev_addr[i] << (8*(ETH_ALEN-1 - i));
230         b.bridge_id = cpu_to_be64(id);
231         b.stp_enabled = 0;
232
233         if (copy_to_user(ub, &b, sizeof(struct __bridge_info)))
234                 return -EFAULT;
235
236         return 0;
237 }
238
239 static int
240 brc_get_port_list(struct net_device *dev, int __user *uindices, int num)
241 {
242         struct dp_dev *dp_dev = netdev_priv(dev);
243         struct datapath *dp = dp_dev->dp;
244         int *indices;
245
246         if (num < 0)
247                 return -EINVAL;
248         if (num == 0)
249                 num = 256;
250         if (num > DP_MAX_PORTS)
251                 num = DP_MAX_PORTS;
252
253         indices = kcalloc(num, sizeof(int), GFP_KERNEL);
254         if (indices == NULL)
255                 return -ENOMEM;
256
257         get_port_ifindices(dp, indices, num);
258         if (copy_to_user(uindices, indices, num * sizeof(int)))
259                 num = -EFAULT;
260         kfree(indices);
261         return num;
262 }
263
264 /* Legacy ioctl's through SIOCDEVPRIVATE.  Called with rtnl_lock. */
265 static int
266 old_dev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
267 {
268         unsigned long args[4];
269
270         if (copy_from_user(args, rq->ifr_data, sizeof(args)))
271                 return -EFAULT;
272
273         switch (args[0]) {
274         case BRCTL_ADD_IF:
275                 return brc_add_del_port(dev, args[1], 1);
276         case BRCTL_DEL_IF:
277                 return brc_add_del_port(dev, args[1], 0);
278
279         case BRCTL_GET_BRIDGE_INFO:
280                 return brc_get_bridge_info(dev, (struct __bridge_info __user *)args[1]);
281
282         case BRCTL_GET_PORT_LIST:
283                 return brc_get_port_list(dev, (int __user *)args[1], args[2]);
284         }
285
286         return -EOPNOTSUPP;
287 }
288
289 /* Called with the rtnl_lock. */
290 static int
291 brc_dev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
292 {
293         int err;
294
295         switch (cmd) {
296                 case SIOCDEVPRIVATE:
297                         err = old_dev_ioctl(dev, rq, cmd);
298                         break;
299
300                 case SIOCBRADDIF:
301                         return brc_add_del_port(dev, rq->ifr_ifindex, 1);
302                 case SIOCBRDELIF:
303                         return brc_add_del_port(dev, rq->ifr_ifindex, 0);
304
305                 default:
306                         err = -EOPNOTSUPP;
307                         break;
308         }
309
310         return err;
311 }
312
313
314 static struct genl_family brc_genl_family = {
315         .id = GENL_ID_GENERATE,
316         .hdrsize = 0,
317         .name = BRC_GENL_FAMILY_NAME,
318         .version = 1,
319         .maxattr = BRC_GENL_A_MAX,
320 };
321
322 static int brc_genl_query(struct sk_buff *skb, struct genl_info *info)
323 {
324         int err = -EINVAL;
325         struct sk_buff *ans_skb;
326         void *data;
327
328         ans_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
329         if (!ans_skb) 
330                 return -ENOMEM;
331
332         data = genlmsg_put_reply(ans_skb, info, &brc_genl_family,
333                                  0, BRC_GENL_C_QUERY_MC);
334         if (data == NULL) {
335                 err = -ENOMEM;
336                 goto err;
337         }
338         NLA_PUT_U32(ans_skb, BRC_GENL_A_MC_GROUP, brc_mc_group.id);
339
340         genlmsg_end(ans_skb, data);
341         return genlmsg_reply(ans_skb, info);
342
343 err:
344 nla_put_failure:
345         kfree_skb(ans_skb);
346         return err;
347 }
348
349 static struct genl_ops brc_genl_ops_query_dp = {
350         .cmd = BRC_GENL_C_QUERY_MC,
351         .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privelege. */
352         .policy = NULL,
353         .doit = brc_genl_query,
354         .dumpit = NULL
355 };
356
357 /* Attribute policy: what each attribute may contain.  */
358 static struct nla_policy brc_genl_policy[BRC_GENL_A_MAX + 1] = {
359         [BRC_GENL_A_ERR_CODE] = { .type = NLA_U32 },
360         [BRC_GENL_A_PROC_DIR] = { .type = NLA_NUL_STRING },
361         [BRC_GENL_A_PROC_NAME] = { .type = NLA_NUL_STRING },
362         [BRC_GENL_A_PROC_DATA] = { .type = NLA_NUL_STRING },
363 };
364
365 static int
366 brc_genl_dp_result(struct sk_buff *skb, struct genl_info *info)
367 {
368         unsigned long int flags;
369         int err;
370
371         if (!info->attrs[BRC_GENL_A_ERR_CODE])
372                 return -EINVAL;
373
374         skb = skb_clone(skb, GFP_KERNEL);
375         if (!skb)
376                 return -ENOMEM;
377
378         spin_lock_irqsave(&brc_lock, flags);
379         if (brc_seq == info->snd_seq) {
380                 brc_seq++;
381
382                 if (brc_reply)
383                         kfree_skb(brc_reply);
384                 brc_reply = skb;
385
386                 complete(&brc_done);
387                 err = 0;
388         } else {
389                 kfree_skb(skb);
390                 err = -ESTALE;
391         }
392         spin_unlock_irqrestore(&brc_lock, flags);
393
394         return err;
395 }
396
397 static struct genl_ops brc_genl_ops_dp_result = {
398         .cmd = BRC_GENL_C_DP_RESULT,
399         .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privelege. */
400         .policy = brc_genl_policy,
401         .doit = brc_genl_dp_result,
402         .dumpit = NULL
403 };
404
405 static struct genl_ops brc_genl_ops_set_proc = {
406         .cmd = BRC_GENL_C_SET_PROC,
407         .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privelege. */
408         .policy = brc_genl_policy,
409         .doit = brc_genl_set_proc,
410         .dumpit = NULL
411 };
412
413 static struct sk_buff *brc_send_command(struct sk_buff *request, struct nlattr **attrs)
414 {
415         unsigned long int flags;
416         struct sk_buff *reply;
417         int error;
418
419         mutex_lock(&brc_serial);
420
421         /* Increment sequence number first, so that we ignore any replies
422          * to stale requests. */
423         spin_lock_irqsave(&brc_lock, flags);
424         nlmsg_hdr(request)->nlmsg_seq = ++brc_seq;
425         INIT_COMPLETION(brc_done);
426         spin_unlock_irqrestore(&brc_lock, flags);
427
428         nlmsg_end(request, nlmsg_hdr(request));
429
430         /* Send message. */
431         error = genlmsg_multicast(request, 0, brc_mc_group.id, GFP_KERNEL);
432         if (error < 0)
433                 goto error;
434
435         /* Wait for reply. */
436         error = -ETIMEDOUT;
437         if (!wait_for_completion_timeout(&brc_done, BRC_TIMEOUT))
438                 goto error;
439
440         /* Grab reply. */
441         spin_lock_irqsave(&brc_lock, flags);
442         reply = brc_reply;
443         brc_reply = NULL;
444         spin_unlock_irqrestore(&brc_lock, flags);
445
446         mutex_unlock(&brc_serial);
447
448         /* Re-parse message.  Can't fail, since it parsed correctly once
449          * already. */
450         error = nlmsg_parse(nlmsg_hdr(reply), GENL_HDRLEN,
451                             attrs, BRC_GENL_A_MAX, brc_genl_policy);
452         WARN_ON(error);
453
454         return reply;
455
456 error:
457         mutex_unlock(&brc_serial);
458         return ERR_PTR(error);
459 }
460
461 int brc_add_dp(struct datapath *dp)
462 {
463         if (!try_module_get(THIS_MODULE))
464                 return -ENODEV;
465 #ifdef SUPPORT_SYSFS
466         brc_sysfs_add_dp(dp);
467 #endif
468
469         return 0;
470 }
471
472 int brc_del_dp(struct datapath *dp) 
473 {
474 #ifdef SUPPORT_SYSFS
475         brc_sysfs_del_dp(dp);
476 #endif
477         module_put(THIS_MODULE);
478
479         return 0;
480 }
481
482 static int 
483 __init brc_init(void)
484 {
485         int i;
486         int err;
487
488         printk("Open vSwitch Bridge Compatibility, built "__DATE__" "__TIME__"\n");
489
490         rcu_read_lock();
491         for (i=0; i<ODP_MAX; i++) {
492                 if (get_dp(i)) {
493                         rcu_read_unlock();
494                         printk(KERN_EMERG "brcompat: no datapaths may exist!\n");
495                         return -EEXIST;
496                 }
497         }
498         rcu_read_unlock();
499
500         /* Set the bridge ioctl handler */
501         brioctl_set(brc_ioctl_deviceless_stub);
502
503         /* Set the openvswitch_mod device ioctl handler */
504         dp_ioctl_hook = brc_dev_ioctl;
505
506         /* Register hooks for datapath adds and deletes */
507         dp_add_dp_hook = brc_add_dp;
508         dp_del_dp_hook = brc_del_dp;
509
510         /* Register hooks for interface adds and deletes */
511 #ifdef SUPPORT_SYSFS
512         dp_add_if_hook = brc_sysfs_add_if;
513         dp_del_if_hook = brc_sysfs_del_if;
514 #endif
515
516         /* Randomize the initial sequence number.  This is not a security
517          * feature; it only helps avoid crossed wires between userspace and
518          * the kernel when the module is unloaded and reloaded. */
519         brc_seq = net_random();
520
521         /* Register generic netlink family to communicate changes to
522          * userspace. */
523         err = genl_register_family(&brc_genl_family);
524         if (err)
525                 goto error;
526
527         err = genl_register_ops(&brc_genl_family, &brc_genl_ops_query_dp);
528         if (err != 0) 
529                 goto err_unregister;
530
531         err = genl_register_ops(&brc_genl_family, &brc_genl_ops_dp_result);
532         if (err != 0) 
533                 goto err_unregister;
534
535         err = genl_register_ops(&brc_genl_family, &brc_genl_ops_set_proc);
536         if (err != 0) 
537                 goto err_unregister;
538
539         strcpy(brc_mc_group.name, "brcompat");
540         err = genl_register_mc_group(&brc_genl_family, &brc_mc_group);
541         if (err < 0)
542                 goto err_unregister;
543
544         return 0;
545
546 err_unregister:
547         genl_unregister_family(&brc_genl_family);
548 error:
549         printk(KERN_EMERG "brcompat: failed to install!");
550         return err;
551 }
552
553 static void 
554 brc_cleanup(void)
555 {
556         /* Unregister hooks for datapath adds and deletes */
557         dp_add_dp_hook = NULL;
558         dp_del_dp_hook = NULL;
559         
560         /* Unregister hooks for interface adds and deletes */
561         dp_add_if_hook = NULL;
562         dp_del_if_hook = NULL;
563
564         /* Unregister ioctl hooks */
565         dp_ioctl_hook = NULL;
566         brioctl_set(NULL);
567
568         genl_unregister_family(&brc_genl_family);
569         brc_procfs_exit();
570 }
571
572 module_init(brc_init);
573 module_exit(brc_cleanup);
574
575 MODULE_DESCRIPTION("Open vSwitch bridge compatibility");
576 MODULE_AUTHOR("Nicira Networks");
577 MODULE_LICENSE("GPL");