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