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