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