datapath: Change userspace vport interface to use Netlink attributes.
[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 static struct hlist_head *hash_bucket(const char *name)
159 {
160         unsigned int hash = full_name_hash(name, strlen(name));
161         return &dev_table[hash & (VPORT_HASH_BUCKETS - 1)];
162 }
163
164 /**
165  *      vport_locate - find a port that has already been created
166  *
167  * @name: name of port to find
168  *
169  * Either RTNL or vport lock must be acquired before calling this function
170  * and held while using the found port.  See the locking comments at the
171  * top of the file.
172  */
173 struct vport *vport_locate(const char *name)
174 {
175         struct hlist_head *bucket = hash_bucket(name);
176         struct vport *vport;
177         struct hlist_node *node;
178
179         if (unlikely(!mutex_is_locked(&vport_mutex) && !rtnl_is_locked())) {
180                 pr_err("neither RTNL nor vport lock held in vport_locate\n");
181                 dump_stack();
182         }
183
184         rcu_read_lock();
185
186         hlist_for_each_entry(vport, node, bucket, hash_node)
187                 if (!strcmp(name, vport_get_name(vport)))
188                         goto out;
189
190         vport = NULL;
191
192 out:
193         rcu_read_unlock();
194         return vport;
195 }
196
197 static void register_vport(struct vport *vport)
198 {
199         hlist_add_head(&vport->hash_node, hash_bucket(vport_get_name(vport)));
200 }
201
202 static void unregister_vport(struct vport *vport)
203 {
204         hlist_del(&vport->hash_node);
205 }
206
207 static void release_vport(struct kobject *kobj)
208 {
209         struct vport *p = container_of(kobj, struct vport, kobj);
210         kfree(p);
211 }
212
213 static struct kobj_type brport_ktype = {
214 #ifdef CONFIG_SYSFS
215         .sysfs_ops = &brport_sysfs_ops,
216 #endif
217         .release = release_vport
218 };
219
220 /**
221  *      vport_alloc - allocate and initialize new vport
222  *
223  * @priv_size: Size of private data area to allocate.
224  * @ops: vport device ops
225  *
226  * Allocate and initialize a new vport defined by @ops.  The vport will contain
227  * a private data area of size @priv_size that can be accessed using
228  * vport_priv().  vports that are no longer needed should be released with
229  * vport_free().
230  */
231 struct vport *vport_alloc(int priv_size, const struct vport_ops *ops, const struct vport_parms *parms)
232 {
233         struct vport *vport;
234         size_t alloc_size;
235
236         alloc_size = sizeof(struct vport);
237         if (priv_size) {
238                 alloc_size = ALIGN(alloc_size, VPORT_ALIGN);
239                 alloc_size += priv_size;
240         }
241
242         vport = kzalloc(alloc_size, GFP_KERNEL);
243         if (!vport)
244                 return ERR_PTR(-ENOMEM);
245
246         vport->dp = parms->dp;
247         vport->port_no = parms->port_no;
248         atomic_set(&vport->sflow_pool, 0);
249         vport->ops = ops;
250
251         /* Initialize kobject for bridge.  This will be added as
252          * /sys/class/net/<devname>/brport later, if sysfs is enabled. */
253         vport->kobj.kset = NULL;
254         kobject_init(&vport->kobj, &brport_ktype);
255
256         if (vport->ops->flags & VPORT_F_GEN_STATS) {
257                 vport->percpu_stats = alloc_percpu(struct vport_percpu_stats);
258                 if (!vport->percpu_stats)
259                         return ERR_PTR(-ENOMEM);
260
261                 spin_lock_init(&vport->stats_lock);
262         }
263
264         return vport;
265 }
266
267 /**
268  *      vport_free - uninitialize and free vport
269  *
270  * @vport: vport to free
271  *
272  * Frees a vport allocated with vport_alloc() when it is no longer needed.
273  */
274 void vport_free(struct vport *vport)
275 {
276         if (vport->ops->flags & VPORT_F_GEN_STATS)
277                 free_percpu(vport->percpu_stats);
278
279         kobject_put(&vport->kobj);
280 }
281
282 /**
283  *      vport_add - add vport device (for kernel callers)
284  *
285  * @parms: Information about new vport.
286  *
287  * Creates a new vport with the specified configuration (which is dependent on
288  * device type) and attaches it to a datapath.  Both RTNL and vport locks must
289  * be held.
290  */
291 struct vport *vport_add(const struct vport_parms *parms)
292 {
293         struct vport *vport;
294         int err = 0;
295         int i;
296
297         ASSERT_RTNL();
298         ASSERT_VPORT();
299
300         for (i = 0; i < n_vport_types; i++) {
301                 if (vport_ops_list[i]->type == parms->type) {
302                         vport = vport_ops_list[i]->create(parms);
303                         if (IS_ERR(vport)) {
304                                 err = PTR_ERR(vport);
305                                 goto out;
306                         }
307
308                         register_vport(vport);
309                         return vport;
310                 }
311         }
312
313         err = -EAFNOSUPPORT;
314
315 out:
316         return ERR_PTR(err);
317 }
318
319 /**
320  *      vport_set_options - modify existing vport device (for kernel callers)
321  *
322  * @vport: vport to modify.
323  * @port: New configuration.
324  *
325  * Modifies an existing device with the specified configuration (which is
326  * dependent on device type).  RTNL lock must be held.
327  */
328 int vport_set_options(struct vport *vport, struct nlattr *options)
329 {
330         ASSERT_RTNL();
331
332         if (!vport->ops->set_options)
333                 return -EOPNOTSUPP;
334         return vport->ops->set_options(vport, options);
335 }
336
337 /**
338  *      vport_del - delete existing vport device (for kernel callers)
339  *
340  * @vport: vport to delete.
341  *
342  * Detaches @vport from its datapath and destroys it.  It is possible to fail
343  * for reasons such as lack of memory.  Both RTNL and vport locks must be held.
344  */
345 int vport_del(struct vport *vport)
346 {
347         ASSERT_RTNL();
348         ASSERT_VPORT();
349
350         unregister_vport(vport);
351
352         return vport->ops->destroy(vport);
353 }
354
355 /**
356  *      vport_set_mtu - set device MTU (for kernel callers)
357  *
358  * @vport: vport on which to set MTU.
359  * @mtu: New MTU.
360  *
361  * Sets the MTU of the given device.  Some devices may not support setting the
362  * MTU, in which case the result will always be -EOPNOTSUPP.  RTNL lock must
363  * be held.
364  */
365 int vport_set_mtu(struct vport *vport, int mtu)
366 {
367         ASSERT_RTNL();
368
369         if (mtu < 68)
370                 return -EINVAL;
371
372         if (vport->ops->set_mtu) {
373                 int ret;
374
375                 ret = vport->ops->set_mtu(vport, mtu);
376
377                 if (!ret && !is_internal_vport(vport))
378                         set_internal_devs_mtu(vport->dp);
379
380                 return ret;
381         } else
382                 return -EOPNOTSUPP;
383 }
384
385 /**
386  *      vport_set_addr - set device Ethernet address (for kernel callers)
387  *
388  * @vport: vport on which to set Ethernet address.
389  * @addr: New address.
390  *
391  * Sets the Ethernet address of the given device.  Some devices may not support
392  * setting the Ethernet address, in which case the result will always be
393  * -EOPNOTSUPP.  RTNL lock must be held.
394  */
395 int vport_set_addr(struct vport *vport, const unsigned char *addr)
396 {
397         ASSERT_RTNL();
398
399         if (!is_valid_ether_addr(addr))
400                 return -EADDRNOTAVAIL;
401
402         if (vport->ops->set_addr)
403                 return vport->ops->set_addr(vport, addr);
404         else
405                 return -EOPNOTSUPP;
406 }
407
408 /**
409  *      vport_set_stats - sets offset device stats (for kernel callers)
410  *
411  * @vport: vport on which to set stats
412  * @stats: stats to set
413  *
414  * Provides a set of transmit, receive, and error stats to be added as an
415  * offset to the collect data when stats are retreived.  Some devices may not
416  * support setting the stats, in which case the result will always be
417  * -EOPNOTSUPP.  RTNL lock must be held.
418  */
419 int vport_set_stats(struct vport *vport, struct rtnl_link_stats64 *stats)
420 {
421         ASSERT_RTNL();
422
423         if (vport->ops->flags & VPORT_F_GEN_STATS) {
424                 spin_lock_bh(&vport->stats_lock);
425                 vport->offset_stats = *stats;
426                 spin_unlock_bh(&vport->stats_lock);
427
428                 return 0;
429         } else
430                 return -EOPNOTSUPP;
431 }
432
433 /**
434  *      vport_get_name - retrieve device name
435  *
436  * @vport: vport from which to retrieve the name.
437  *
438  * Retrieves the name of the given device.  Either RTNL lock or rcu_read_lock
439  * must be held for the entire duration that the name is in use.
440  */
441 const char *vport_get_name(const struct vport *vport)
442 {
443         return vport->ops->get_name(vport);
444 }
445
446 /**
447  *      vport_get_type - retrieve device type
448  *
449  * @vport: vport from which to retrieve the type.
450  *
451  * Retrieves the type of the given device.  Either RTNL lock or rcu_read_lock
452  * must be held.
453  */
454 enum odp_vport_type vport_get_type(const struct vport *vport)
455 {
456         return vport->ops->type;
457 }
458
459 /**
460  *      vport_get_addr - retrieve device Ethernet address (for kernel callers)
461  *
462  * @vport: vport from which to retrieve the Ethernet address.
463  *
464  * Retrieves the Ethernet address of the given device.  Either RTNL lock or
465  * rcu_read_lock must be held for the entire duration that the Ethernet address
466  * is in use.
467  */
468 const unsigned char *vport_get_addr(const struct vport *vport)
469 {
470         return vport->ops->get_addr(vport);
471 }
472
473 /**
474  *      vport_get_kobj - retrieve associated kobj
475  *
476  * @vport: vport from which to retrieve the associated kobj
477  *
478  * Retrieves the associated kobj or null if no kobj.  The returned kobj is
479  * valid for as long as the vport exists.
480  */
481 struct kobject *vport_get_kobj(const struct vport *vport)
482 {
483         if (vport->ops->get_kobj)
484                 return vport->ops->get_kobj(vport);
485         else
486                 return NULL;
487 }
488
489 static int vport_call_get_stats(struct vport *vport, struct rtnl_link_stats64 *stats)
490 {
491         int err;
492
493         rcu_read_lock();
494         err = vport->ops->get_stats(vport, stats);
495         rcu_read_unlock();
496
497         return err;
498 }
499
500 /**
501  *      vport_get_stats - retrieve device stats (for kernel callers)
502  *
503  * @vport: vport from which to retrieve the stats
504  * @stats: location to store stats
505  *
506  * Retrieves transmit, receive, and error stats for the given device.
507  */
508 int vport_get_stats(struct vport *vport, struct rtnl_link_stats64 *stats)
509 {
510         int i;
511
512         if (!(vport->ops->flags & VPORT_F_GEN_STATS))
513                 return vport_call_get_stats(vport, stats);
514
515         /* We potentially have 3 sources of stats that need to be
516          * combined: those we have collected (split into err_stats and
517          * percpu_stats), offset_stats from set_stats(), and device
518          * error stats from get_stats() (for errors that happen
519          * downstream and therefore aren't reported through our
520          * vport_record_error() function). */
521
522         spin_lock_bh(&vport->stats_lock);
523
524         *stats = vport->offset_stats;
525
526         stats->rx_errors        += vport->err_stats.rx_errors;
527         stats->tx_errors        += vport->err_stats.tx_errors;
528         stats->tx_dropped       += vport->err_stats.tx_dropped;
529         stats->rx_dropped       += vport->err_stats.rx_dropped;
530
531         spin_unlock_bh(&vport->stats_lock);
532
533         if (vport->ops->get_stats) {
534                 struct rtnl_link_stats64 dev_stats;
535                 int err;
536
537                 err = vport_call_get_stats(vport, &dev_stats);
538                 if (err)
539                         return err;
540
541                 stats->rx_errors           += dev_stats.rx_errors;
542                 stats->tx_errors           += dev_stats.tx_errors;
543                 stats->rx_dropped          += dev_stats.rx_dropped;
544                 stats->tx_dropped          += dev_stats.tx_dropped;
545                 stats->multicast           += dev_stats.multicast;
546                 stats->collisions          += dev_stats.collisions;
547                 stats->rx_length_errors    += dev_stats.rx_length_errors;
548                 stats->rx_over_errors      += dev_stats.rx_over_errors;
549                 stats->rx_crc_errors       += dev_stats.rx_crc_errors;
550                 stats->rx_frame_errors     += dev_stats.rx_frame_errors;
551                 stats->rx_fifo_errors      += dev_stats.rx_fifo_errors;
552                 stats->rx_missed_errors    += dev_stats.rx_missed_errors;
553                 stats->tx_aborted_errors   += dev_stats.tx_aborted_errors;
554                 stats->tx_carrier_errors   += dev_stats.tx_carrier_errors;
555                 stats->tx_fifo_errors      += dev_stats.tx_fifo_errors;
556                 stats->tx_heartbeat_errors += dev_stats.tx_heartbeat_errors;
557                 stats->tx_window_errors    += dev_stats.tx_window_errors;
558                 stats->rx_compressed       += dev_stats.rx_compressed;
559                 stats->tx_compressed       += dev_stats.tx_compressed;
560         }
561
562         for_each_possible_cpu(i) {
563                 const struct vport_percpu_stats *percpu_stats;
564                 struct vport_percpu_stats local_stats;
565                 unsigned seqcount;
566
567                 percpu_stats = per_cpu_ptr(vport->percpu_stats, i);
568
569                 do {
570                         seqcount = read_seqcount_begin(&percpu_stats->seqlock);
571                         local_stats = *percpu_stats;
572                 } while (read_seqcount_retry(&percpu_stats->seqlock, seqcount));
573
574                 stats->rx_bytes         += local_stats.rx_bytes;
575                 stats->rx_packets       += local_stats.rx_packets;
576                 stats->tx_bytes         += local_stats.tx_bytes;
577                 stats->tx_packets       += local_stats.tx_packets;
578         }
579
580         return 0;
581 }
582
583 /**
584  *      vport_get_flags - retrieve device flags
585  *
586  * @vport: vport from which to retrieve the flags
587  *
588  * Retrieves the flags of the given device.  Either RTNL lock or rcu_read_lock
589  * must be held.
590  */
591 unsigned vport_get_flags(const struct vport *vport)
592 {
593         return vport->ops->get_dev_flags(vport);
594 }
595
596 /**
597  *      vport_get_flags - check whether device is running
598  *
599  * @vport: vport on which to check status.
600  *
601  * Checks whether the given device is running.  Either RTNL lock or
602  * rcu_read_lock must be held.
603  */
604 int vport_is_running(const struct vport *vport)
605 {
606         return vport->ops->is_running(vport);
607 }
608
609 /**
610  *      vport_get_flags - retrieve device operating state
611  *
612  * @vport: vport from which to check status
613  *
614  * Retrieves the RFC2863 operstate of the given device.  Either RTNL lock or
615  * rcu_read_lock must be held.
616  */
617 unsigned char vport_get_operstate(const struct vport *vport)
618 {
619         return vport->ops->get_operstate(vport);
620 }
621
622 /**
623  *      vport_get_ifindex - retrieve device system interface index
624  *
625  * @vport: vport from which to retrieve index
626  *
627  * Retrieves the system interface index of the given device or 0 if
628  * the device does not have one (in the case of virtual ports).
629  * Returns a negative index on error. Either RTNL lock or
630  * rcu_read_lock must be held.
631  */
632 int vport_get_ifindex(const struct vport *vport)
633 {
634         if (vport->ops->get_ifindex)
635                 return vport->ops->get_ifindex(vport);
636         else
637                 return 0;
638 }
639
640 /**
641  *      vport_get_iflink - retrieve device system link index
642  *
643  * @vport: vport from which to retrieve index
644  *
645  * Retrieves the system link index of the given device.  The link is the index
646  * of the interface on which the packet will actually be sent.  In most cases
647  * this is the same as the ifindex but may be different for tunnel devices.
648  * Returns a negative index on error.  Either RTNL lock or rcu_read_lock must
649  * be held.
650  */
651 int vport_get_iflink(const struct vport *vport)
652 {
653         if (vport->ops->get_iflink)
654                 return vport->ops->get_iflink(vport);
655
656         /* If we don't have an iflink, use the ifindex.  In most cases they
657          * are the same. */
658         return vport_get_ifindex(vport);
659 }
660
661 /**
662  *      vport_get_mtu - retrieve device MTU (for kernel callers)
663  *
664  * @vport: vport from which to retrieve MTU
665  *
666  * Retrieves the MTU of the given device.  Either RTNL lock or rcu_read_lock
667  * must be held.
668  */
669 int vport_get_mtu(const struct vport *vport)
670 {
671         return vport->ops->get_mtu(vport);
672 }
673
674 /**
675  *      vport_get_options - retrieve device options
676  *
677  * @vport: vport from which to retrieve the options.
678  * @skb: sk_buff where options should be appended.
679  *
680  * Retrieves the configuration of the given device, appending an
681  * %ODP_VPORT_ATTR_OPTIONS attribute that in turn contains nested
682  * vport-specific attributes to @skb.  Either RTNL lock or rcu_read_lock must
683  * be held.
684  *
685  * Returns 0 if successful, -EMSGSIZE if @skb has insufficient room, or another
686  * negative error code if a real error occurred.
687  */
688 int vport_get_options(const struct vport *vport, struct sk_buff *skb)
689 {
690         struct nlattr *nla;
691
692         nla = nla_nest_start(skb, ODP_VPORT_ATTR_OPTIONS);
693         if (!nla)
694                 return -EMSGSIZE;
695
696         if (vport->ops->get_options) {
697                 int err = vport->ops->get_options(vport, skb);
698                 if (err)
699                         return err;
700         }
701
702         nla_nest_end(skb, nla);
703         return 0;
704 }
705
706 /**
707  *      vport_receive - pass up received packet to the datapath for processing
708  *
709  * @vport: vport that received the packet
710  * @skb: skb that was received
711  *
712  * Must be called with rcu_read_lock.  The packet cannot be shared and
713  * skb->data should point to the Ethernet header.  The caller must have already
714  * called compute_ip_summed() to initialize the checksumming fields.
715  */
716 void vport_receive(struct vport *vport, struct sk_buff *skb)
717 {
718         if (vport->ops->flags & VPORT_F_GEN_STATS) {
719                 struct vport_percpu_stats *stats;
720
721                 local_bh_disable();
722                 stats = per_cpu_ptr(vport->percpu_stats, smp_processor_id());
723
724                 write_seqcount_begin(&stats->seqlock);
725                 stats->rx_packets++;
726                 stats->rx_bytes += skb->len;
727                 write_seqcount_end(&stats->seqlock);
728
729                 local_bh_enable();
730         }
731
732         if (!(vport->ops->flags & VPORT_F_FLOW))
733                 OVS_CB(skb)->flow = NULL;
734
735         if (!(vport->ops->flags & VPORT_F_TUN_ID))
736                 OVS_CB(skb)->tun_id = 0;
737
738         dp_process_received_packet(vport, skb);
739 }
740
741 static inline unsigned packet_length(const struct sk_buff *skb)
742 {
743         unsigned length = skb->len - ETH_HLEN;
744
745         if (skb->protocol == htons(ETH_P_8021Q))
746                 length -= VLAN_HLEN;
747
748         return length;
749 }
750
751 /**
752  *      vport_send - send a packet on a device
753  *
754  * @vport: vport on which to send the packet
755  * @skb: skb to send
756  *
757  * Sends the given packet and returns the length of data sent.  Either RTNL
758  * lock or rcu_read_lock must be held.
759  */
760 int vport_send(struct vport *vport, struct sk_buff *skb)
761 {
762         int mtu;
763         int sent;
764
765         mtu = vport_get_mtu(vport);
766         if (unlikely(packet_length(skb) > mtu && !skb_is_gso(skb))) {
767                 if (net_ratelimit())
768                         pr_warn("%s: dropped over-mtu packet: %d > %d\n",
769                                 dp_name(vport->dp), packet_length(skb), mtu);
770                 goto error;
771         }
772
773         sent = vport->ops->send(vport, skb);
774
775         if (vport->ops->flags & VPORT_F_GEN_STATS && sent > 0) {
776                 struct vport_percpu_stats *stats;
777
778                 local_bh_disable();
779                 stats = per_cpu_ptr(vport->percpu_stats, smp_processor_id());
780
781                 write_seqcount_begin(&stats->seqlock);
782                 stats->tx_packets++;
783                 stats->tx_bytes += sent;
784                 write_seqcount_end(&stats->seqlock);
785
786                 local_bh_enable();
787         }
788
789         return sent;
790
791 error:
792         kfree_skb(skb);
793         vport_record_error(vport, VPORT_E_TX_DROPPED);
794         return 0;
795 }
796
797 /**
798  *      vport_record_error - indicate device error to generic stats layer
799  *
800  * @vport: vport that encountered the error
801  * @err_type: one of enum vport_err_type types to indicate the error type
802  *
803  * If using the vport generic stats layer indicate that an error of the given
804  * type has occured.
805  */
806 void vport_record_error(struct vport *vport, enum vport_err_type err_type)
807 {
808         if (vport->ops->flags & VPORT_F_GEN_STATS) {
809
810                 spin_lock_bh(&vport->stats_lock);
811
812                 switch (err_type) {
813                 case VPORT_E_RX_DROPPED:
814                         vport->err_stats.rx_dropped++;
815                         break;
816
817                 case VPORT_E_RX_ERROR:
818                         vport->err_stats.rx_errors++;
819                         break;
820
821                 case VPORT_E_TX_DROPPED:
822                         vport->err_stats.tx_dropped++;
823                         break;
824
825                 case VPORT_E_TX_ERROR:
826                         vport->err_stats.tx_errors++;
827                         break;
828                 };
829
830                 spin_unlock_bh(&vport->stats_lock);
831         }
832 }