datapath: Add loop checking.
[sliver-openvswitch.git] / datapath / vport.c
1 /*
2  * Copyright (c) 2010 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/dcache.h>
10 #include <linux/etherdevice.h>
11 #include <linux/if.h>
12 #include <linux/kernel.h>
13 #include <linux/list.h>
14 #include <linux/mutex.h>
15 #include <linux/percpu.h>
16 #include <linux/rtnetlink.h>
17 #include <linux/compat.h>
18
19 #include "vport.h"
20 #include "vport-internal_dev.h"
21
22 /* List of statically compiled vport implementations.  Don't forget to also
23  * add yours to the list at the bottom of vport.h. */
24 static struct vport_ops *base_vport_ops_list[] = {
25         &netdev_vport_ops,
26         &internal_vport_ops,
27         &patch_vport_ops,
28         &gre_vport_ops,
29 };
30
31 static const struct vport_ops **vport_ops_list;
32 static int n_vport_types;
33
34 static struct hlist_head *dev_table;
35 #define VPORT_HASH_BUCKETS 1024
36
37 /* We limit the number of times that we pass through vport_send() to
38  * avoid blowing out the stack in the event that we have a loop. There is
39  * a separate counter for each CPU for both interrupt and non-interrupt
40  * context in order to keep the limit deterministic for a given packet. */
41 struct percpu_loop_counter {
42         int count[2];
43 };
44
45 static struct percpu_loop_counter *vport_loop_counter;
46 #define VPORT_MAX_LOOPS 5
47
48 /* Both RTNL lock and vport_mutex need to be held when updating dev_table.
49  *
50  * If you use vport_locate and then perform some operations, you need to hold
51  * one of these locks if you don't want the vport to be deleted out from under
52  * you.
53  *
54  * If you get a reference to a vport through a dp_port, it is protected
55  * by RCU and you need to hold rcu_read_lock instead when reading.
56  *
57  * If multiple locks are taken, the hierarchy is:
58  * 1. RTNL
59  * 2. DP
60  * 3. vport
61  */
62 static DEFINE_MUTEX(vport_mutex);
63
64 /**
65  *      vport_lock - acquire vport lock
66  *
67  * Acquire global vport lock.  See above comment about locking requirements
68  * and specific function definitions.  May sleep.
69  */
70 void
71 vport_lock(void)
72 {
73         mutex_lock(&vport_mutex);
74 }
75
76 /**
77  *      vport_unlock - release vport lock
78  *
79  * Release lock acquired with vport_lock.
80  */
81 void
82 vport_unlock(void)
83 {
84         mutex_unlock(&vport_mutex);
85 }
86
87 #define ASSERT_VPORT() do { \
88         if (unlikely(!mutex_is_locked(&vport_mutex))) { \
89                 printk(KERN_ERR "openvswitch: vport lock not held at %s (%d)\n", \
90                         __FILE__, __LINE__); \
91                 dump_stack(); \
92         } \
93 } while(0)
94
95 /**
96  *      vport_init - initialize vport subsystem
97  *
98  * Called at module load time to initialize the vport subsystem and any
99  * compiled in vport types.
100  */
101 int
102 vport_init(void)
103 {
104         int err;
105         int i;
106
107         dev_table = kzalloc(VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
108                             GFP_KERNEL);
109         if (!dev_table) {
110                 err = -ENOMEM;
111                 goto error;
112         }
113
114         vport_ops_list = kmalloc(ARRAY_SIZE(base_vport_ops_list) *
115                                  sizeof(struct vport_ops *), GFP_KERNEL);
116         if (!vport_ops_list) {
117                 err = -ENOMEM;
118                 goto error_dev_table;
119         }
120
121         vport_loop_counter = alloc_percpu(struct percpu_loop_counter);
122         if (!vport_loop_counter) {
123                 err = -ENOMEM;
124                 goto error_ops_list;
125         }
126
127         for (i = 0; i < ARRAY_SIZE(base_vport_ops_list); i++) {
128                 struct vport_ops *new_ops = base_vport_ops_list[i];
129
130                 if (new_ops->init)
131                         err = new_ops->init();
132                 else
133                         err = 0;
134
135                 if (!err)
136                         vport_ops_list[n_vport_types++] = new_ops;
137                 else if (new_ops->flags & VPORT_F_REQUIRED) {
138                         vport_exit();
139                         goto error;
140                 }
141         }
142
143         return 0;
144
145 error_ops_list:
146         kfree(vport_ops_list);
147 error_dev_table:
148         kfree(dev_table);
149 error:
150         return err;
151 }
152
153 static void
154 vport_del_all(void)
155 {
156         int i;
157
158         rtnl_lock();
159         vport_lock();
160
161         for (i = 0; i < VPORT_HASH_BUCKETS; i++) {
162                 struct hlist_head *bucket = &dev_table[i];
163                 struct vport *vport;
164                 struct hlist_node *node, *next;
165
166                 hlist_for_each_entry_safe(vport, node, next, bucket, hash_node)
167                         vport_del(vport);
168         }
169
170         vport_unlock();
171         rtnl_unlock();
172 }
173
174 /**
175  *      vport_exit - shutdown vport subsystem
176  *
177  * Called at module exit time to shutdown the vport subsystem and any
178  * initialized vport types.
179  */
180 void
181 vport_exit(void)
182 {
183         int i;
184
185         vport_del_all();
186
187         for (i = 0; i < n_vport_types; i++) {
188                 if (vport_ops_list[i]->exit)
189                         vport_ops_list[i]->exit();
190         }
191
192         free_percpu(vport_loop_counter);
193         kfree(vport_ops_list);
194         kfree(dev_table);
195 }
196
197 static int
198 do_vport_add(struct odp_vport_add *vport_config)
199 {
200         struct vport *vport;
201         int err = 0;
202
203         vport_config->port_type[VPORT_TYPE_SIZE - 1] = '\0';
204         vport_config->devname[IFNAMSIZ - 1] = '\0';
205
206         rtnl_lock();
207
208         vport = vport_locate(vport_config->devname);
209         if (vport) {
210                 err = -EEXIST;
211                 goto out;
212         }
213
214         vport_lock();
215         vport = vport_add(vport_config->devname, vport_config->port_type,
216                           vport_config->config);
217         vport_unlock();
218
219         if (IS_ERR(vport))
220                 err = PTR_ERR(vport);
221
222 out:
223         rtnl_unlock();
224         return err;
225 }
226
227 /**
228  *      vport_user_add - add vport device (for userspace callers)
229  *
230  * @uvport_config: New port configuration.
231  *
232  * Creates a new vport with the specified configuration (which is dependent
233  * on device type).  This function is for userspace callers and assumes no
234  * locks are held.
235  */
236 int
237 vport_user_add(const struct odp_vport_add __user *uvport_config)
238 {
239         struct odp_vport_add vport_config;
240
241         if (copy_from_user(&vport_config, uvport_config, sizeof(struct odp_vport_add)))
242                 return -EFAULT;
243
244         return do_vport_add(&vport_config);
245 }
246
247 #ifdef CONFIG_COMPAT
248 int
249 compat_vport_user_add(struct compat_odp_vport_add *ucompat)
250 {
251         struct compat_odp_vport_add compat;
252         struct odp_vport_add vport_config;
253
254         if (copy_from_user(&compat, ucompat, sizeof(struct compat_odp_vport_add)))
255                 return -EFAULT;
256
257         memcpy(vport_config.port_type, compat.port_type, VPORT_TYPE_SIZE);
258         memcpy(vport_config.devname, compat.devname, IFNAMSIZ);
259         vport_config.config = compat_ptr(compat.config);
260
261         return do_vport_add(&vport_config);
262 }
263 #endif
264
265 static int
266 do_vport_mod(struct odp_vport_mod *vport_config)
267 {
268         struct vport *vport;
269         int err;
270
271         vport_config->devname[IFNAMSIZ - 1] = '\0';
272
273         rtnl_lock();
274
275         vport = vport_locate(vport_config->devname);
276         if (!vport) {
277                 err = -ENODEV;
278                 goto out;
279         }
280
281         vport_lock();
282         err = vport_mod(vport, vport_config->config);
283         vport_unlock();
284
285 out:
286         rtnl_unlock();
287         return err;
288 }
289
290 /**
291  *      vport_user_mod - modify existing vport device (for userspace callers)
292  *
293  * @uvport_config: New configuration for vport
294  *
295  * Modifies an existing device with the specified configuration (which is
296  * dependent on device type).  This function is for userspace callers and
297  * assumes no locks are held.
298  */
299 int
300 vport_user_mod(const struct odp_vport_mod __user *uvport_config)
301 {
302         struct odp_vport_mod vport_config;
303
304         if (copy_from_user(&vport_config, uvport_config, sizeof(struct odp_vport_mod)))
305                 return -EFAULT;
306
307         return do_vport_mod(&vport_config);
308 }
309
310 #ifdef CONFIG_COMPAT
311 int
312 compat_vport_user_mod(struct compat_odp_vport_mod *ucompat)
313 {
314         struct compat_odp_vport_mod compat;
315         struct odp_vport_mod vport_config;
316
317         if (copy_from_user(&compat, ucompat, sizeof(struct compat_odp_vport_mod)))
318                 return -EFAULT;
319
320         memcpy(vport_config.devname, compat.devname, IFNAMSIZ);
321         vport_config.config = compat_ptr(compat.config);
322
323         return do_vport_mod(&vport_config);
324 }
325 #endif
326
327 /**
328  *      vport_user_del - delete existing vport device (for userspace callers)
329  *
330  * @udevname: Name of device to delete
331  *
332  * Deletes the specified device.  Detaches the device from a datapath first
333  * if it is attached.  Deleting the device will fail if it does not exist or it
334  * is the datapath local port.  It is also possible to fail for less obvious
335  * reasons, such as lack of memory.  This function is for userspace callers and
336  * assumes no locks are held.
337  */
338 int
339 vport_user_del(const char __user *udevname)
340 {
341         char devname[IFNAMSIZ];
342         struct vport *vport;
343         struct dp_port *dp_port;
344         int err = 0;
345         int retval;
346
347         retval = strncpy_from_user(devname, udevname, IFNAMSIZ);
348         if (retval < 0)
349                 return -EFAULT;
350         else if (retval >= IFNAMSIZ)
351                 return -ENAMETOOLONG;
352
353         rtnl_lock();
354
355         vport = vport_locate(devname);
356         if (!vport) {
357                 err = -ENODEV;
358                 goto out;
359         }
360
361         dp_port = vport_get_dp_port(vport);
362         if (dp_port) {
363                 struct datapath *dp = dp_port->dp;
364
365                 mutex_lock(&dp->mutex);
366
367                 if (!strcmp(dp_name(dp), devname)) {
368                         err = -EINVAL;
369                         goto dp_port_out;
370                 }
371
372                 err = dp_detach_port(dp_port, 0);
373
374 dp_port_out:
375                 mutex_unlock(&dp->mutex);
376
377                 if (err)
378                         goto out;
379         }
380
381         vport_lock();
382         err = vport_del(vport);
383         vport_unlock();
384
385 out:
386         rtnl_unlock();
387         return err;
388 }
389
390 /**
391  *      vport_user_stats_get - retrieve device stats (for userspace callers)
392  *
393  * @ustats_req: Stats request parameters.
394  *
395  * Retrieves transmit, receive, and error stats for the given device.  This
396  * function is for userspace callers and assumes no locks are held.
397  */
398 int
399 vport_user_stats_get(struct odp_vport_stats_req __user *ustats_req)
400 {
401         struct odp_vport_stats_req stats_req;
402         struct vport *vport;
403         int err;
404
405         if (copy_from_user(&stats_req, ustats_req, sizeof(struct odp_vport_stats_req)))
406                 return -EFAULT;
407
408         stats_req.devname[IFNAMSIZ - 1] = '\0';
409
410         vport_lock();
411
412         vport = vport_locate(stats_req.devname);
413         if (!vport) {
414                 err = -ENODEV;
415                 goto out;
416         }
417
418         err = vport_get_stats(vport, &stats_req.stats);
419
420 out:
421         vport_unlock();
422
423         if (!err)
424                 if (copy_to_user(ustats_req, &stats_req, sizeof(struct odp_vport_stats_req)))
425                         err = -EFAULT;
426
427         return err;
428 }
429
430 /**
431  *      vport_user_stats_set - sets offset device stats (for userspace callers)
432  *
433  * @ustats_req: Stats set parameters.
434  *
435  * Provides a set of transmit, receive, and error stats to be added as an
436  * offset to the collect data when stats are retreived.  Some devices may not
437  * support setting the stats, in which case the result will always be
438  * -EOPNOTSUPP.  This function is for userspace callers and assumes no locks
439  * are held.
440  */
441 int
442 vport_user_stats_set(struct odp_vport_stats_req __user *ustats_req)
443 {
444         struct odp_vport_stats_req stats_req;
445         struct vport *vport;
446         int err;
447
448         if (copy_from_user(&stats_req, ustats_req, sizeof(struct odp_vport_stats_req)))
449                 return -EFAULT;
450
451         stats_req.devname[IFNAMSIZ - 1] = '\0';
452
453         rtnl_lock();
454         vport_lock();
455
456         vport = vport_locate(stats_req.devname);
457         if (!vport) {
458                 err = -ENODEV;
459                 goto out;
460         }
461
462         err = vport_set_stats(vport, &stats_req.stats);
463
464 out:
465         vport_unlock();
466         rtnl_unlock();
467         return err;
468 }
469
470
471 /**
472  *      vport_user_ether_get - retrieve device Ethernet address (for userspace callers)
473  *
474  * @uvport_ether: Ethernet address request parameters.
475  *
476  * Retrieves the Ethernet address of the given device.  This function is for
477  * userspace callers and assumes no locks are held.
478  */
479 int
480 vport_user_ether_get(struct odp_vport_ether __user *uvport_ether)
481 {
482         struct odp_vport_ether vport_ether;
483         struct vport *vport;
484         int err = 0;
485
486         if (copy_from_user(&vport_ether, uvport_ether, sizeof(struct odp_vport_ether)))
487                 return -EFAULT;
488
489         vport_ether.devname[IFNAMSIZ - 1] = '\0';
490
491         vport_lock();
492
493         vport = vport_locate(vport_ether.devname);
494         if (!vport) {
495                 err = -ENODEV;
496                 goto out;
497         }
498
499         rcu_read_lock();
500         memcpy(vport_ether.ether_addr, vport_get_addr(vport), ETH_ALEN);
501         rcu_read_unlock();
502
503 out:
504         vport_unlock();
505
506         if (!err)
507                 if (copy_to_user(uvport_ether, &vport_ether, sizeof(struct odp_vport_ether)))
508                         err = -EFAULT;
509
510         return err;
511 }
512
513 /**
514  *      vport_user_ether_set - set device Ethernet address (for userspace callers)
515  *
516  * @uvport_ether: Ethernet address request parameters.
517  *
518  * Sets the Ethernet address of the given device.  Some devices may not support
519  * setting the Ethernet address, in which case the result will always be
520  * -EOPNOTSUPP.  This function is for userspace callers and assumes no locks
521  * are held.
522  */
523 int
524 vport_user_ether_set(struct odp_vport_ether __user *uvport_ether)
525 {
526         struct odp_vport_ether vport_ether;
527         struct vport *vport;
528         int err;
529
530         if (copy_from_user(&vport_ether, uvport_ether, sizeof(struct odp_vport_ether)))
531                 return -EFAULT;
532
533         vport_ether.devname[IFNAMSIZ - 1] = '\0';
534
535         rtnl_lock();
536         vport_lock();
537
538         vport = vport_locate(vport_ether.devname);
539         if (!vport) {
540                 err = -ENODEV;
541                 goto out;
542         }
543
544         err = vport_set_addr(vport, vport_ether.ether_addr);
545
546 out:
547         vport_unlock();
548         rtnl_unlock();
549         return err;
550 }
551
552 /**
553  *      vport_user_mtu_get - retrieve device MTU (for userspace callers)
554  *
555  * @uvport_mtu: MTU request parameters.
556  *
557  * Retrieves the MTU of the given device.  This function is for userspace
558  * callers and assumes no locks are held.
559  */
560 int
561 vport_user_mtu_get(struct odp_vport_mtu __user *uvport_mtu)
562 {
563         struct odp_vport_mtu vport_mtu;
564         struct vport *vport;
565         int err = 0;
566
567         if (copy_from_user(&vport_mtu, uvport_mtu, sizeof(struct odp_vport_mtu)))
568                 return -EFAULT;
569
570         vport_mtu.devname[IFNAMSIZ - 1] = '\0';
571
572         vport_lock();
573
574         vport = vport_locate(vport_mtu.devname);
575         if (!vport) {
576                 err = -ENODEV;
577                 goto out;
578         }
579
580         vport_mtu.mtu = vport_get_mtu(vport);
581
582 out:
583         vport_unlock();
584
585         if (!err)
586                 if (copy_to_user(uvport_mtu, &vport_mtu, sizeof(struct odp_vport_mtu)))
587                         err = -EFAULT;
588
589         return err;
590 }
591
592 /**
593  *      vport_user_mtu_set - set device MTU (for userspace callers)
594  *
595  * @uvport_mtu: MTU request parameters.
596  *
597  * Sets the MTU of the given device.  Some devices may not support setting the
598  * MTU, in which case the result will always be -EOPNOTSUPP.  This function is
599  * for userspace callers and assumes no locks are held.
600  */
601 int
602 vport_user_mtu_set(struct odp_vport_mtu __user *uvport_mtu)
603 {
604         struct odp_vport_mtu vport_mtu;
605         struct vport *vport;
606         int err;
607
608         if (copy_from_user(&vport_mtu, uvport_mtu, sizeof(struct odp_vport_mtu)))
609                 return -EFAULT;
610
611         vport_mtu.devname[IFNAMSIZ - 1] = '\0';
612
613         rtnl_lock();
614         vport_lock();
615
616         vport = vport_locate(vport_mtu.devname);
617         if (!vport) {
618                 err = -ENODEV;
619                 goto out;
620         }
621
622         err = vport_set_mtu(vport, vport_mtu.mtu);
623
624 out:
625         vport_unlock();
626         rtnl_unlock();
627         return err;
628 }
629
630 static struct hlist_head *
631 hash_bucket(const char *name)
632 {
633         unsigned int hash = full_name_hash(name, strlen(name));
634         return &dev_table[hash & (VPORT_HASH_BUCKETS - 1)];
635 }
636
637 /**
638  *      vport_locate - find a port that has already been created
639  *
640  * @name: name of port to find
641  *
642  * Either RTNL or vport lock must be acquired before calling this function
643  * and held while using the found port.  See the locking comments at the
644  * top of the file.
645  */
646 struct vport *
647 vport_locate(const char *name)
648 {
649         struct hlist_head *bucket = hash_bucket(name);
650         struct vport *vport;
651         struct hlist_node *node;
652
653         if (unlikely(!mutex_is_locked(&vport_mutex) && !rtnl_is_locked())) {
654                 printk(KERN_ERR "openvswitch: neither RTNL nor vport lock held in vport_locate\n");
655                 dump_stack();
656         }
657
658         rcu_read_lock();
659
660         hlist_for_each_entry(vport, node, bucket, hash_node)
661                 if (!strcmp(name, vport_get_name(vport)))
662                         goto out;
663
664         vport = NULL;
665
666 out:
667         rcu_read_unlock();
668         return vport;
669 }
670
671 static void
672 register_vport(struct vport *vport)
673 {
674         hlist_add_head(&vport->hash_node, hash_bucket(vport_get_name(vport)));
675 }
676
677 static void
678 unregister_vport(struct vport *vport)
679 {
680         hlist_del(&vport->hash_node);
681 }
682
683 /**
684  *      vport_alloc - allocate and initialize new vport
685  *
686  * @priv_size: Size of private data area to allocate.
687  * @ops: vport device ops
688  *
689  * Allocate and initialize a new vport defined by @ops.  The vport will contain
690  * a private data area of size @priv_size that can be accessed using
691  * vport_priv().  vports that are no longer needed should be released with
692  * vport_free().
693  */
694 struct vport *
695 vport_alloc(int priv_size, const struct vport_ops *ops)
696 {
697         struct vport *vport;
698         size_t alloc_size;
699
700         alloc_size = sizeof(struct vport);
701         if (priv_size) {
702                 alloc_size = ALIGN(alloc_size, VPORT_ALIGN);
703                 alloc_size += priv_size;
704         }
705
706         vport = kzalloc(alloc_size, GFP_KERNEL);
707         if (!vport)
708                 return ERR_PTR(-ENOMEM);
709
710         vport->ops = ops;
711
712         if (vport->ops->flags & VPORT_F_GEN_STATS) {
713                 vport->percpu_stats = alloc_percpu(struct vport_percpu_stats);
714                 if (!vport->percpu_stats)
715                         return ERR_PTR(-ENOMEM);
716
717                 spin_lock_init(&vport->stats_lock);
718         }
719
720         return vport;
721 }
722
723 /**
724  *      vport_free - uninitialize and free vport
725  *
726  * @vport: vport to free
727  *
728  * Frees a vport allocated with vport_alloc() when it is no longer needed.
729  */
730 void
731 vport_free(struct vport *vport)
732 {
733         if (vport->ops->flags & VPORT_F_GEN_STATS)
734                 free_percpu(vport->percpu_stats);
735
736         kfree(vport);
737 }
738
739 /**
740  *      vport_add - add vport device (for kernel callers)
741  *
742  * @name: Name of new device.
743  * @type: Type of new device (to be matched against types in registered vport
744  * ops).
745  * @config: Device type specific configuration.  Userspace pointer.
746  *
747  * Creates a new vport with the specified configuration (which is dependent
748  * on device type).  Both RTNL and vport locks must be held.
749  */
750 struct vport *
751 vport_add(const char *name, const char *type, const void __user *config)
752 {
753         struct vport *vport;
754         int err = 0;
755         int i;
756
757         ASSERT_RTNL();
758         ASSERT_VPORT();
759
760         for (i = 0; i < n_vport_types; i++) {
761                 if (!strcmp(vport_ops_list[i]->type, type)) {
762                         vport = vport_ops_list[i]->create(name, config);
763                         if (IS_ERR(vport)) {
764                                 err = PTR_ERR(vport);
765                                 goto out;
766                         }
767
768                         register_vport(vport);
769                         return vport;
770                 }
771         }
772
773         err = -EAFNOSUPPORT;
774
775 out:
776         return ERR_PTR(err);
777 }
778
779 /**
780  *      vport_mod - modify existing vport device (for kernel callers)
781  *
782  * @vport: vport to modify.
783  * @config: Device type specific configuration.  Userspace pointer.
784  *
785  * Modifies an existing device with the specified configuration (which is
786  * dependent on device type).  Both RTNL and vport locks must be held.
787  */
788 int
789 vport_mod(struct vport *vport, const void __user *config)
790 {
791         ASSERT_RTNL();
792         ASSERT_VPORT();
793
794         if (vport->ops->modify)
795                 return vport->ops->modify(vport, config);
796         else
797                 return -EOPNOTSUPP;
798 }
799
800 /**
801  *      vport_del - delete existing vport device (for kernel callers)
802  *
803  * @vport: vport to delete.
804  *
805  * Deletes the specified device.  The device must not be currently attached to
806  * a datapath.  It is possible to fail for reasons such as lack of memory.
807  * Both RTNL and vport locks must be held.
808  */
809 int
810 vport_del(struct vport *vport)
811 {
812         ASSERT_RTNL();
813         ASSERT_VPORT();
814         BUG_ON(vport_get_dp_port(vport));
815
816         unregister_vport(vport);
817
818         return vport->ops->destroy(vport);
819 }
820
821 /**
822  *      vport_attach - attach a vport to a datapath
823  *
824  * @vport: vport to attach.
825  * @dp_port: Datapath port to attach the vport to.
826  *
827  * Attaches a vport to a specific datapath so that packets may be exchanged.
828  * Both ports must be currently unattached.  @dp_port must be successfully
829  * attached to a vport before it is connected to a datapath and must not be
830  * modified while connected.  RTNL lock and the appropriate DP mutex must be held.
831  */
832 int
833 vport_attach(struct vport *vport, struct dp_port *dp_port)
834 {
835         ASSERT_RTNL();
836
837         if (dp_port->vport)
838                 return -EBUSY;
839
840         if (vport_get_dp_port(vport))
841                 return -EBUSY;
842
843         if (vport->ops->attach) {
844                 int err;
845
846                 err = vport->ops->attach(vport);
847                 if (err)
848                         return err;
849         }
850
851         dp_port->vport = vport;
852         rcu_assign_pointer(vport->dp_port, dp_port);
853
854         return 0;
855 }
856
857 /**
858  *      vport_detach - detach a vport from a datapath
859  *
860  * @vport: vport to detach.
861  *
862  * Detaches a vport from a datapath.  May fail for a variety of reasons,
863  * including lack of memory.  RTNL lock and the appropriate DP mutex must be held.
864  */
865 int
866 vport_detach(struct vport *vport)
867 {
868         struct dp_port *dp_port;
869
870         ASSERT_RTNL();
871
872         dp_port = vport_get_dp_port(vport);
873         if (!dp_port)
874                 return -EINVAL;
875
876         dp_port->vport = NULL;
877         rcu_assign_pointer(vport->dp_port, NULL);
878
879         if (vport->ops->detach)
880                 return vport->ops->detach(vport);
881         else
882                 return 0;
883 }
884
885 /**
886  *      vport_set_mtu - set device MTU (for kernel callers)
887  *
888  * @vport: vport on which to set MTU.
889  * @mtu: New MTU.
890  *
891  * Sets the MTU of the given device.  Some devices may not support setting the
892  * MTU, in which case the result will always be -EOPNOTSUPP.  RTNL lock must
893  * be held.
894  */
895 int
896 vport_set_mtu(struct vport *vport, int mtu)
897 {
898         ASSERT_RTNL();
899
900         if (mtu < 68)
901                 return -EINVAL;
902
903         if (vport->ops->set_mtu) {
904                 int ret;
905
906                 ret = vport->ops->set_mtu(vport, mtu);
907
908                 if (!ret && !is_internal_vport(vport)) {
909                         struct dp_port *dp_port = vport_get_dp_port(vport);
910
911                         if (dp_port)
912                                 set_internal_devs_mtu(dp_port->dp);
913                 }
914
915                 return ret;
916         } else
917                 return -EOPNOTSUPP;
918 }
919
920 /**
921  *      vport_set_addr - set device Ethernet address (for kernel callers)
922  *
923  * @vport: vport on which to set Ethernet address.
924  * @addr: New address.
925  *
926  * Sets the Ethernet address of the given device.  Some devices may not support
927  * setting the Ethernet address, in which case the result will always be
928  * -EOPNOTSUPP.  RTNL lock must be held.
929  */
930 int
931 vport_set_addr(struct vport *vport, const unsigned char *addr)
932 {
933         ASSERT_RTNL();
934
935         if (!is_valid_ether_addr(addr))
936                 return -EADDRNOTAVAIL;
937
938         if (vport->ops->set_addr)
939                 return vport->ops->set_addr(vport, addr);
940         else
941                 return -EOPNOTSUPP;
942 }
943
944 /**
945  *      vport_set_stats - sets offset device stats (for kernel callers)
946  *
947  * @vport: vport on which to set stats
948  * @stats: stats to set
949  *
950  * Provides a set of transmit, receive, and error stats to be added as an
951  * offset to the collect data when stats are retreived.  Some devices may not
952  * support setting the stats, in which case the result will always be
953  * -EOPNOTSUPP.  RTNL lock must be held.
954  */
955 int
956 vport_set_stats(struct vport *vport, struct odp_vport_stats *stats)
957 {
958         ASSERT_RTNL();
959
960         if (vport->ops->flags & VPORT_F_GEN_STATS) {
961                 spin_lock_bh(&vport->stats_lock);
962                 memcpy(&vport->offset_stats, stats, sizeof(struct odp_vport_stats));
963                 spin_unlock_bh(&vport->stats_lock);
964
965                 return 0;
966         } else if (vport->ops->set_stats)
967                 return vport->ops->set_stats(vport, stats);
968         else
969                 return -EOPNOTSUPP;
970 }
971
972 /**
973  *      vport_get_name - retrieve device name
974  *
975  * @vport: vport from which to retrieve the name.
976  *
977  * Retrieves the name of the given device.  Either RTNL lock or rcu_read_lock
978  * must be held for the entire duration that the name is in use.
979  */
980 const char *
981 vport_get_name(const struct vport *vport)
982 {
983         return vport->ops->get_name(vport);
984 }
985
986 /**
987  *      vport_get_type - retrieve device type
988  *
989  * @vport: vport from which to retrieve the type.
990  *
991  * Retrieves the type of the given device.  Either RTNL lock or rcu_read_lock
992  * must be held for the entire duration that the type is in use.
993  */
994 const char *
995 vport_get_type(const struct vport *vport)
996 {
997         return vport->ops->type;
998 }
999
1000 /**
1001  *      vport_get_addr - retrieve device Ethernet address (for kernel callers)
1002  *
1003  * @vport: vport from which to retrieve the Ethernet address.
1004  *
1005  * Retrieves the Ethernet address of the given device.  Either RTNL lock or
1006  * rcu_read_lock must be held for the entire duration that the Ethernet address
1007  * is in use.
1008  */
1009 const unsigned char *
1010 vport_get_addr(const struct vport *vport)
1011 {
1012         return vport->ops->get_addr(vport);
1013 }
1014
1015 /**
1016  *      vport_get_dp_port - retrieve attached datapath port
1017  *
1018  * @vport: vport from which to retrieve the datapath port.
1019  *
1020  * Retrieves the attached datapath port or null if not attached.  Either RTNL
1021  * lock or rcu_read_lock must be held for the entire duration that the datapath
1022  * port is being accessed.
1023  */
1024 struct dp_port *
1025 vport_get_dp_port(const struct vport *vport)
1026 {
1027         return rcu_dereference(vport->dp_port);
1028 }
1029
1030 /**
1031  *      vport_get_kobj - retrieve associated kobj
1032  *
1033  * @vport: vport from which to retrieve the associated kobj
1034  *
1035  * Retrieves the associated kobj or null if no kobj.  The returned kobj is
1036  * valid for as long as the vport exists.
1037  */
1038 struct kobject *
1039 vport_get_kobj(const struct vport *vport)
1040 {
1041         if (vport->ops->get_kobj)
1042                 return vport->ops->get_kobj(vport);
1043         else
1044                 return NULL;
1045 }
1046
1047 /**
1048  *      vport_get_stats - retrieve device stats (for kernel callers)
1049  *
1050  * @vport: vport from which to retrieve the stats
1051  * @stats: location to store stats
1052  *
1053  * Retrieves transmit, receive, and error stats for the given device.
1054  */
1055 int
1056 vport_get_stats(struct vport *vport, struct odp_vport_stats *stats)
1057 {
1058         struct odp_vport_stats dev_stats;
1059         struct odp_vport_stats *dev_statsp = NULL;
1060         int err;
1061
1062         if (vport->ops->get_stats) {
1063                 if (vport->ops->flags & VPORT_F_GEN_STATS)
1064                         dev_statsp = &dev_stats;
1065                 else
1066                         dev_statsp = stats;
1067
1068                 rcu_read_lock();
1069                 err = vport->ops->get_stats(vport, dev_statsp);
1070                 rcu_read_unlock();
1071
1072                 if (err)
1073                         goto out;
1074         }
1075
1076         if (vport->ops->flags & VPORT_F_GEN_STATS) {
1077                 int i;
1078
1079                 /* We potentially have 3 sources of stats that need to be
1080                  * combined: those we have collected (split into err_stats and
1081                  * percpu_stats), offset_stats from set_stats(), and device
1082                  * error stats from get_stats() (for errors that happen
1083                  * downstream and therefore aren't reported through our
1084                  * vport_record_error() function). */
1085
1086                 spin_lock_bh(&vport->stats_lock);
1087
1088                 memcpy(stats, &vport->offset_stats, sizeof(struct odp_vport_stats));
1089
1090                 stats->rx_errors        += vport->err_stats.rx_errors
1091                                                 + vport->err_stats.rx_frame_err
1092                                                 + vport->err_stats.rx_over_err
1093                                                 + vport->err_stats.rx_crc_err;
1094                 stats->tx_errors        += vport->err_stats.tx_errors;
1095                 stats->tx_dropped       += vport->err_stats.tx_dropped;
1096                 stats->rx_dropped       += vport->err_stats.rx_dropped;
1097                 stats->rx_over_err      += vport->err_stats.rx_over_err;
1098                 stats->rx_crc_err       += vport->err_stats.rx_crc_err;
1099                 stats->rx_frame_err     += vport->err_stats.rx_frame_err;
1100                 stats->collisions       += vport->err_stats.collisions;
1101
1102                 spin_unlock_bh(&vport->stats_lock);
1103
1104                 if (dev_statsp) {
1105                         stats->rx_errors        += dev_statsp->rx_errors;
1106                         stats->tx_errors        += dev_statsp->tx_errors;
1107                         stats->rx_dropped       += dev_statsp->rx_dropped;
1108                         stats->tx_dropped       += dev_statsp->tx_dropped;
1109                         stats->rx_over_err      += dev_statsp->rx_over_err;
1110                         stats->rx_crc_err       += dev_statsp->rx_crc_err;
1111                         stats->rx_frame_err     += dev_statsp->rx_frame_err;
1112                         stats->collisions       += dev_statsp->collisions;
1113                 }
1114
1115                 for_each_possible_cpu(i) {
1116                         const struct vport_percpu_stats *percpu_stats;
1117
1118                         percpu_stats = per_cpu_ptr(vport->percpu_stats, i);
1119                         stats->rx_bytes         += percpu_stats->rx_bytes;
1120                         stats->rx_packets       += percpu_stats->rx_packets;
1121                         stats->tx_bytes         += percpu_stats->tx_bytes;
1122                         stats->tx_packets       += percpu_stats->tx_packets;
1123                 }
1124
1125                 err = 0;
1126         } else
1127                 err = -EOPNOTSUPP;
1128
1129 out:
1130         return err;
1131 }
1132
1133 /**
1134  *      vport_get_flags - retrieve device flags
1135  *
1136  * @vport: vport from which to retrieve the flags
1137  *
1138  * Retrieves the flags of the given device.  Either RTNL lock or rcu_read_lock
1139  * must be held.
1140  */
1141 unsigned
1142 vport_get_flags(const struct vport *vport)
1143 {
1144         return vport->ops->get_dev_flags(vport);
1145 }
1146
1147 /**
1148  *      vport_get_flags - check whether device is running
1149  *
1150  * @vport: vport on which to check status.
1151  *
1152  * Checks whether the given device is running.  Either RTNL lock or
1153  * rcu_read_lock must be held.
1154  */
1155 int
1156 vport_is_running(const struct vport *vport)
1157 {
1158         return vport->ops->is_running(vport);
1159 }
1160
1161 /**
1162  *      vport_get_flags - retrieve device operating state
1163  *
1164  * @vport: vport from which to check status
1165  *
1166  * Retrieves the RFC2863 operstate of the given device.  Either RTNL lock or
1167  * rcu_read_lock must be held.
1168  */
1169 unsigned char
1170 vport_get_operstate(const struct vport *vport)
1171 {
1172         return vport->ops->get_operstate(vport);
1173 }
1174
1175 /**
1176  *      vport_get_ifindex - retrieve device system interface index
1177  *
1178  * @vport: vport from which to retrieve index
1179  *
1180  * Retrieves the system interface index of the given device.  Not all devices
1181  * will have system indexes, in which case the index of the datapath local
1182  * port is returned.  Returns a negative index on error.  Either RTNL lock or
1183  * rcu_read_lock must be held.
1184  */
1185 int
1186 vport_get_ifindex(const struct vport *vport)
1187 {
1188         const struct dp_port *dp_port;
1189
1190         if (vport->ops->get_ifindex)
1191                 return vport->ops->get_ifindex(vport);
1192
1193         /* If we don't actually have an ifindex, use the local port's.
1194          * Userspace doesn't check it anyways. */
1195         dp_port = vport_get_dp_port(vport);
1196         if (!dp_port)
1197                 return -EAGAIN;
1198
1199         return vport_get_ifindex(dp_port->dp->ports[ODPP_LOCAL]->vport);
1200 }
1201
1202 /**
1203  *      vport_get_iflink - retrieve device system link index
1204  *
1205  * @vport: vport from which to retrieve index
1206  *
1207  * Retrieves the system link index of the given device.  The link is the index
1208  * of the interface on which the packet will actually be sent.  In most cases
1209  * this is the same as the ifindex but may be different for tunnel devices.
1210  * Returns a negative index on error.  Either RTNL lock or rcu_read_lock must
1211  * be held.
1212  */
1213 int
1214 vport_get_iflink(const struct vport *vport)
1215 {
1216         if (vport->ops->get_iflink)
1217                 return vport->ops->get_iflink(vport);
1218
1219         /* If we don't have an iflink, use the ifindex.  In most cases they
1220          * are the same. */
1221         return vport_get_ifindex(vport);
1222 }
1223
1224 /**
1225  *      vport_get_mtu - retrieve device MTU (for kernel callers)
1226  *
1227  * @vport: vport from which to retrieve MTU
1228  *
1229  * Retrieves the MTU of the given device.  Either RTNL lock or rcu_read_lock
1230  * must be held.
1231  */
1232 int
1233 vport_get_mtu(const struct vport *vport)
1234 {
1235         return vport->ops->get_mtu(vport);
1236 }
1237
1238 /**
1239  *      vport_receive - pass up received packet to the datapath for processing
1240  *
1241  * @vport: vport that received the packet
1242  * @skb: skb that was received
1243  *
1244  * Must be called with rcu_read_lock.  The packet cannot be shared and
1245  * skb->data should point to the Ethernet header.  The caller must have already
1246  * called compute_ip_summed() to initialize the checksumming fields.
1247  */
1248 void
1249 vport_receive(struct vport *vport, struct sk_buff *skb)
1250 {
1251         struct dp_port *dp_port = vport_get_dp_port(vport);
1252
1253         if (!dp_port) {
1254                 vport_record_error(vport, VPORT_E_RX_DROPPED);
1255                 kfree_skb(skb);
1256
1257                 return;
1258         }
1259
1260         if (vport->ops->flags & VPORT_F_GEN_STATS) {
1261                 struct vport_percpu_stats *stats;
1262
1263                 local_bh_disable();
1264
1265                 stats = per_cpu_ptr(vport->percpu_stats, smp_processor_id());
1266                 stats->rx_packets++;
1267                 stats->rx_bytes += skb->len;
1268
1269                 local_bh_enable();
1270         }
1271
1272         if (!(vport->ops->flags & VPORT_F_TUN_ID))
1273                 OVS_CB(skb)->tun_id = 0;
1274
1275         dp_process_received_packet(dp_port, skb);
1276 }
1277
1278 /**
1279  *      vport_send - send a packet on a device
1280  *
1281  * @vport: vport on which to send the packet
1282  * @skb: skb to send
1283  *
1284  * Sends the given packet and returns the length of data sent.  Either RTNL
1285  * lock or rcu_read_lock must be held.
1286  */
1287 int
1288 vport_send(struct vport *vport, struct sk_buff *skb)
1289 {
1290         int *loop_count;
1291         int sent;
1292
1293         loop_count = &per_cpu_ptr(vport_loop_counter, get_cpu())->count[!!in_interrupt()];
1294         (*loop_count)++;
1295
1296         if (likely(*loop_count <= VPORT_MAX_LOOPS)) {
1297                 sent = vport->ops->send(vport, skb);
1298         } else {
1299                 if (net_ratelimit())
1300                         printk(KERN_WARNING "%s: dropping packet that has looped more than %d times\n",
1301                                dp_name(vport_get_dp_port(vport)->dp), VPORT_MAX_LOOPS);
1302
1303                 sent = 0;
1304                 kfree_skb(skb);
1305                 vport_record_error(vport, VPORT_E_TX_DROPPED);
1306         }
1307
1308         (*loop_count)--;
1309         put_cpu();
1310
1311         if (vport->ops->flags & VPORT_F_GEN_STATS && sent > 0) {
1312                 struct vport_percpu_stats *stats;
1313
1314                 local_bh_disable();
1315
1316                 stats = per_cpu_ptr(vport->percpu_stats, smp_processor_id());
1317                 stats->tx_packets++;
1318                 stats->tx_bytes += sent;
1319
1320                 local_bh_enable();
1321         }
1322
1323         return sent;
1324 }
1325
1326 /**
1327  *      vport_record_error - indicate device error to generic stats layer
1328  *
1329  * @vport: vport that encountered the error
1330  * @err_type: one of enum vport_err_type types to indicate the error type
1331  *
1332  * If using the vport generic stats layer indicate that an error of the given
1333  * type has occured.
1334  */
1335 void
1336 vport_record_error(struct vport *vport, enum vport_err_type err_type)
1337 {
1338         if (vport->ops->flags & VPORT_F_GEN_STATS) {
1339
1340                 spin_lock_bh(&vport->stats_lock);
1341
1342                 switch (err_type) {
1343                 case VPORT_E_RX_DROPPED:
1344                         vport->err_stats.rx_dropped++;
1345                         break;
1346
1347                 case VPORT_E_RX_ERROR:
1348                         vport->err_stats.rx_errors++;
1349                         break;
1350
1351                 case VPORT_E_RX_FRAME:
1352                         vport->err_stats.rx_frame_err++;
1353                         break;
1354
1355                 case VPORT_E_RX_OVER:
1356                         vport->err_stats.rx_over_err++;
1357                         break;
1358
1359                 case VPORT_E_RX_CRC:
1360                         vport->err_stats.rx_crc_err++;
1361                         break;
1362
1363                 case VPORT_E_TX_DROPPED:
1364                         vport->err_stats.tx_dropped++;
1365                         break;
1366
1367                 case VPORT_E_TX_ERROR:
1368                         vport->err_stats.tx_errors++;
1369                         break;
1370
1371                 case VPORT_E_COLLISION:
1372                         vport->err_stats.collisions++;
1373                         break;
1374                 };
1375
1376                 spin_unlock_bh(&vport->stats_lock);
1377         }
1378 }