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