2 * Copyright (c) 2010 Nicira Networks.
3 * Distributed under the terms of the GNU GPL version 2.
5 * Significant portions of this file may be copied from parts of the Linux
6 * kernel, by Linus Torvalds and others.
9 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11 #include <linux/dcache.h>
12 #include <linux/etherdevice.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>
24 #include "vport-internal_dev.h"
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 struct vport_ops *base_vport_ops_list[] = {
33 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
38 static const struct vport_ops **vport_ops_list;
39 static int n_vport_types;
41 static struct hlist_head *dev_table;
42 #define VPORT_HASH_BUCKETS 1024
44 /* Both RTNL lock and vport_mutex need to be held when updating dev_table.
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
50 * If you get a reference to a vport through a dp_port, it is protected
51 * by RCU and you need to hold rcu_read_lock instead when reading.
53 * If multiple locks are taken, the hierarchy is:
58 static DEFINE_MUTEX(vport_mutex);
61 * vport_lock - acquire vport lock
63 * Acquire global vport lock. See above comment about locking requirements
64 * and specific function definitions. May sleep.
68 mutex_lock(&vport_mutex);
72 * vport_unlock - release vport lock
74 * Release lock acquired with vport_lock.
76 void vport_unlock(void)
78 mutex_unlock(&vport_mutex);
81 #define ASSERT_VPORT() \
83 if (unlikely(!mutex_is_locked(&vport_mutex))) { \
84 pr_err("vport lock not held at %s (%d)\n", \
85 __FILE__, __LINE__); \
91 * vport_init - initialize vport subsystem
93 * Called at module load time to initialize the vport subsystem and any
94 * compiled in vport types.
101 dev_table = kzalloc(VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
108 vport_ops_list = kmalloc(ARRAY_SIZE(base_vport_ops_list) *
109 sizeof(struct vport_ops *), GFP_KERNEL);
110 if (!vport_ops_list) {
112 goto error_dev_table;
115 for (i = 0; i < ARRAY_SIZE(base_vport_ops_list); i++) {
116 struct vport_ops *new_ops = base_vport_ops_list[i];
119 err = new_ops->init();
124 vport_ops_list[n_vport_types++] = new_ops;
125 else if (new_ops->flags & VPORT_F_REQUIRED) {
139 static void vport_del_all(void)
146 for (i = 0; i < VPORT_HASH_BUCKETS; i++) {
147 struct hlist_head *bucket = &dev_table[i];
149 struct hlist_node *node, *next;
151 hlist_for_each_entry_safe(vport, node, next, bucket, hash_node)
160 * vport_exit - shutdown vport subsystem
162 * Called at module exit time to shutdown the vport subsystem and any
163 * initialized vport types.
165 void vport_exit(void)
171 for (i = 0; i < n_vport_types; i++) {
172 if (vport_ops_list[i]->exit)
173 vport_ops_list[i]->exit();
176 kfree(vport_ops_list);
180 static int do_vport_add(struct odp_vport_add *vport_config)
185 vport_config->port_type[VPORT_TYPE_SIZE - 1] = '\0';
186 vport_config->devname[IFNAMSIZ - 1] = '\0';
190 vport = vport_locate(vport_config->devname);
197 vport = vport_add(vport_config->devname, vport_config->port_type,
198 vport_config->config);
202 err = PTR_ERR(vport);
210 * vport_user_add - add vport device (for userspace callers)
212 * @uvport_config: New port configuration.
214 * Creates a new vport with the specified configuration (which is dependent
215 * on device type). This function is for userspace callers and assumes no
218 int vport_user_add(const struct odp_vport_add __user *uvport_config)
220 struct odp_vport_add vport_config;
222 if (copy_from_user(&vport_config, uvport_config, sizeof(struct odp_vport_add)))
225 return do_vport_add(&vport_config);
229 int compat_vport_user_add(struct compat_odp_vport_add *ucompat)
231 struct compat_odp_vport_add compat;
232 struct odp_vport_add vport_config;
234 if (copy_from_user(&compat, ucompat, sizeof(struct compat_odp_vport_add)))
237 memcpy(vport_config.port_type, compat.port_type, VPORT_TYPE_SIZE);
238 memcpy(vport_config.devname, compat.devname, IFNAMSIZ);
239 vport_config.config = compat_ptr(compat.config);
241 return do_vport_add(&vport_config);
245 static int do_vport_mod(struct odp_vport_mod *vport_config)
250 vport_config->devname[IFNAMSIZ - 1] = '\0';
254 vport = vport_locate(vport_config->devname);
261 err = vport_mod(vport, vport_config->config);
270 * vport_user_mod - modify existing vport device (for userspace callers)
272 * @uvport_config: New configuration for vport
274 * Modifies an existing device with the specified configuration (which is
275 * dependent on device type). This function is for userspace callers and
276 * assumes no locks are held.
278 int vport_user_mod(const struct odp_vport_mod __user *uvport_config)
280 struct odp_vport_mod vport_config;
282 if (copy_from_user(&vport_config, uvport_config, sizeof(struct odp_vport_mod)))
285 return do_vport_mod(&vport_config);
289 int compat_vport_user_mod(struct compat_odp_vport_mod *ucompat)
291 struct compat_odp_vport_mod compat;
292 struct odp_vport_mod vport_config;
294 if (copy_from_user(&compat, ucompat, sizeof(struct compat_odp_vport_mod)))
297 memcpy(vport_config.devname, compat.devname, IFNAMSIZ);
298 vport_config.config = compat_ptr(compat.config);
300 return do_vport_mod(&vport_config);
305 * vport_user_del - delete existing vport device (for userspace callers)
307 * @udevname: Name of device to delete
309 * Deletes the specified device. Detaches the device from a datapath first
310 * if it is attached. Deleting the device will fail if it does not exist or it
311 * is the datapath local port. It is also possible to fail for less obvious
312 * reasons, such as lack of memory. This function is for userspace callers and
313 * assumes no locks are held.
315 int vport_user_del(const char __user *udevname)
317 char devname[IFNAMSIZ];
319 struct dp_port *dp_port;
323 retval = strncpy_from_user(devname, udevname, IFNAMSIZ);
326 else if (retval >= IFNAMSIZ)
327 return -ENAMETOOLONG;
331 vport = vport_locate(devname);
337 dp_port = vport_get_dp_port(vport);
339 struct datapath *dp = dp_port->dp;
341 mutex_lock(&dp->mutex);
343 if (!strcmp(dp_name(dp), devname)) {
348 err = dp_detach_port(dp_port, 0);
351 mutex_unlock(&dp->mutex);
358 err = vport_del(vport);
367 * vport_user_stats_get - retrieve device stats (for userspace callers)
369 * @ustats_req: Stats request parameters.
371 * Retrieves transmit, receive, and error stats for the given device. This
372 * function is for userspace callers and assumes no locks are held.
374 int vport_user_stats_get(struct odp_vport_stats_req __user *ustats_req)
376 struct odp_vport_stats_req stats_req;
380 if (copy_from_user(&stats_req, ustats_req, sizeof(struct odp_vport_stats_req)))
383 stats_req.devname[IFNAMSIZ - 1] = '\0';
387 vport = vport_locate(stats_req.devname);
393 err = vport_get_stats(vport, &stats_req.stats);
399 if (copy_to_user(ustats_req, &stats_req, sizeof(struct odp_vport_stats_req)))
406 * vport_user_stats_set - sets offset device stats (for userspace callers)
408 * @ustats_req: Stats set parameters.
410 * Provides a set of transmit, receive, and error stats to be added as an
411 * offset to the collect data when stats are retreived. Some devices may not
412 * support setting the stats, in which case the result will always be
413 * -EOPNOTSUPP. This function is for userspace callers and assumes no locks
416 int vport_user_stats_set(struct odp_vport_stats_req __user *ustats_req)
418 struct odp_vport_stats_req stats_req;
422 if (copy_from_user(&stats_req, ustats_req, sizeof(struct odp_vport_stats_req)))
425 stats_req.devname[IFNAMSIZ - 1] = '\0';
430 vport = vport_locate(stats_req.devname);
436 err = vport_set_stats(vport, &stats_req.stats);
446 * vport_user_ether_get - retrieve device Ethernet address (for userspace callers)
448 * @uvport_ether: Ethernet address request parameters.
450 * Retrieves the Ethernet address of the given device. This function is for
451 * userspace callers and assumes no locks are held.
453 int vport_user_ether_get(struct odp_vport_ether __user *uvport_ether)
455 struct odp_vport_ether vport_ether;
459 if (copy_from_user(&vport_ether, uvport_ether, sizeof(struct odp_vport_ether)))
462 vport_ether.devname[IFNAMSIZ - 1] = '\0';
466 vport = vport_locate(vport_ether.devname);
473 memcpy(vport_ether.ether_addr, vport_get_addr(vport), ETH_ALEN);
480 if (copy_to_user(uvport_ether, &vport_ether, sizeof(struct odp_vport_ether)))
487 * vport_user_ether_set - set device Ethernet address (for userspace callers)
489 * @uvport_ether: Ethernet address request parameters.
491 * Sets the Ethernet address of the given device. Some devices may not support
492 * setting the Ethernet address, in which case the result will always be
493 * -EOPNOTSUPP. This function is for userspace callers and assumes no locks
496 int vport_user_ether_set(struct odp_vport_ether __user *uvport_ether)
498 struct odp_vport_ether vport_ether;
502 if (copy_from_user(&vport_ether, uvport_ether, sizeof(struct odp_vport_ether)))
505 vport_ether.devname[IFNAMSIZ - 1] = '\0';
510 vport = vport_locate(vport_ether.devname);
516 err = vport_set_addr(vport, vport_ether.ether_addr);
525 * vport_user_mtu_get - retrieve device MTU (for userspace callers)
527 * @uvport_mtu: MTU request parameters.
529 * Retrieves the MTU of the given device. This function is for userspace
530 * callers and assumes no locks are held.
532 int vport_user_mtu_get(struct odp_vport_mtu __user *uvport_mtu)
534 struct odp_vport_mtu vport_mtu;
538 if (copy_from_user(&vport_mtu, uvport_mtu, sizeof(struct odp_vport_mtu)))
541 vport_mtu.devname[IFNAMSIZ - 1] = '\0';
545 vport = vport_locate(vport_mtu.devname);
551 vport_mtu.mtu = vport_get_mtu(vport);
557 if (copy_to_user(uvport_mtu, &vport_mtu, sizeof(struct odp_vport_mtu)))
564 * vport_user_mtu_set - set device MTU (for userspace callers)
566 * @uvport_mtu: MTU request parameters.
568 * Sets the MTU of the given device. Some devices may not support setting the
569 * MTU, in which case the result will always be -EOPNOTSUPP. This function is
570 * for userspace callers and assumes no locks are held.
572 int vport_user_mtu_set(struct odp_vport_mtu __user *uvport_mtu)
574 struct odp_vport_mtu vport_mtu;
578 if (copy_from_user(&vport_mtu, uvport_mtu, sizeof(struct odp_vport_mtu)))
581 vport_mtu.devname[IFNAMSIZ - 1] = '\0';
586 vport = vport_locate(vport_mtu.devname);
592 err = vport_set_mtu(vport, vport_mtu.mtu);
600 static struct hlist_head *hash_bucket(const char *name)
602 unsigned int hash = full_name_hash(name, strlen(name));
603 return &dev_table[hash & (VPORT_HASH_BUCKETS - 1)];
607 * vport_locate - find a port that has already been created
609 * @name: name of port to find
611 * Either RTNL or vport lock must be acquired before calling this function
612 * and held while using the found port. See the locking comments at the
615 struct vport *vport_locate(const char *name)
617 struct hlist_head *bucket = hash_bucket(name);
619 struct hlist_node *node;
621 if (unlikely(!mutex_is_locked(&vport_mutex) && !rtnl_is_locked())) {
622 pr_err("neither RTNL nor vport lock held in vport_locate\n");
628 hlist_for_each_entry(vport, node, bucket, hash_node)
629 if (!strcmp(name, vport_get_name(vport)))
639 static void register_vport(struct vport *vport)
641 hlist_add_head(&vport->hash_node, hash_bucket(vport_get_name(vport)));
644 static void unregister_vport(struct vport *vport)
646 hlist_del(&vport->hash_node);
650 * vport_alloc - allocate and initialize new vport
652 * @priv_size: Size of private data area to allocate.
653 * @ops: vport device ops
655 * Allocate and initialize a new vport defined by @ops. The vport will contain
656 * a private data area of size @priv_size that can be accessed using
657 * vport_priv(). vports that are no longer needed should be released with
660 struct vport *vport_alloc(int priv_size, const struct vport_ops *ops)
665 alloc_size = sizeof(struct vport);
667 alloc_size = ALIGN(alloc_size, VPORT_ALIGN);
668 alloc_size += priv_size;
671 vport = kzalloc(alloc_size, GFP_KERNEL);
673 return ERR_PTR(-ENOMEM);
677 if (vport->ops->flags & VPORT_F_GEN_STATS) {
678 vport->percpu_stats = alloc_percpu(struct vport_percpu_stats);
679 if (!vport->percpu_stats)
680 return ERR_PTR(-ENOMEM);
682 spin_lock_init(&vport->stats_lock);
689 * vport_free - uninitialize and free vport
691 * @vport: vport to free
693 * Frees a vport allocated with vport_alloc() when it is no longer needed.
695 void vport_free(struct vport *vport)
697 if (vport->ops->flags & VPORT_F_GEN_STATS)
698 free_percpu(vport->percpu_stats);
704 * vport_add - add vport device (for kernel callers)
706 * @name: Name of new device.
707 * @type: Type of new device (to be matched against types in registered vport
709 * @config: Device type specific configuration. Userspace pointer.
711 * Creates a new vport with the specified configuration (which is dependent
712 * on device type). Both RTNL and vport locks must be held.
714 struct vport *vport_add(const char *name, const char *type, const void __user *config)
723 for (i = 0; i < n_vport_types; i++) {
724 if (!strcmp(vport_ops_list[i]->type, type)) {
725 vport = vport_ops_list[i]->create(name, config);
727 err = PTR_ERR(vport);
731 register_vport(vport);
743 * vport_mod - modify existing vport device (for kernel callers)
745 * @vport: vport to modify.
746 * @config: Device type specific configuration. Userspace pointer.
748 * Modifies an existing device with the specified configuration (which is
749 * dependent on device type). Both RTNL and vport locks must be held.
751 int vport_mod(struct vport *vport, const void __user *config)
756 if (vport->ops->modify)
757 return vport->ops->modify(vport, config);
763 * vport_del - delete existing vport device (for kernel callers)
765 * @vport: vport to delete.
767 * Deletes the specified device. The device must not be currently attached to
768 * a datapath. It is possible to fail for reasons such as lack of memory.
769 * Both RTNL and vport locks must be held.
771 int vport_del(struct vport *vport)
775 BUG_ON(vport_get_dp_port(vport));
777 unregister_vport(vport);
779 return vport->ops->destroy(vport);
783 * vport_attach - attach a vport to a datapath
785 * @vport: vport to attach.
786 * @dp_port: Datapath port to attach the vport to.
788 * Attaches a vport to a specific datapath so that packets may be exchanged.
789 * Both ports must be currently unattached. @dp_port must be successfully
790 * attached to a vport before it is connected to a datapath and must not be
791 * modified while connected. RTNL lock and the appropriate DP mutex must be held.
793 int vport_attach(struct vport *vport, struct dp_port *dp_port)
797 if (vport_get_dp_port(vport))
800 if (vport->ops->attach) {
803 err = vport->ops->attach(vport);
808 rcu_assign_pointer(vport->dp_port, dp_port);
814 * vport_detach - detach a vport from a datapath
816 * @vport: vport to detach.
818 * Detaches a vport from a datapath. May fail for a variety of reasons,
819 * including lack of memory. RTNL lock and the appropriate DP mutex must be held.
821 int vport_detach(struct vport *vport)
823 struct dp_port *dp_port;
827 dp_port = vport_get_dp_port(vport);
831 rcu_assign_pointer(vport->dp_port, NULL);
833 if (vport->ops->detach)
834 return vport->ops->detach(vport);
840 * vport_set_mtu - set device MTU (for kernel callers)
842 * @vport: vport on which to set MTU.
845 * Sets the MTU of the given device. Some devices may not support setting the
846 * MTU, in which case the result will always be -EOPNOTSUPP. RTNL lock must
849 int vport_set_mtu(struct vport *vport, int mtu)
856 if (vport->ops->set_mtu) {
859 ret = vport->ops->set_mtu(vport, mtu);
861 if (!ret && !is_internal_vport(vport)) {
862 struct dp_port *dp_port = vport_get_dp_port(vport);
865 set_internal_devs_mtu(dp_port->dp);
874 * vport_set_addr - set device Ethernet address (for kernel callers)
876 * @vport: vport on which to set Ethernet address.
877 * @addr: New address.
879 * Sets the Ethernet address of the given device. Some devices may not support
880 * setting the Ethernet address, in which case the result will always be
881 * -EOPNOTSUPP. RTNL lock must be held.
883 int vport_set_addr(struct vport *vport, const unsigned char *addr)
887 if (!is_valid_ether_addr(addr))
888 return -EADDRNOTAVAIL;
890 if (vport->ops->set_addr)
891 return vport->ops->set_addr(vport, addr);
897 * vport_set_stats - sets offset device stats (for kernel callers)
899 * @vport: vport on which to set stats
900 * @stats: stats to set
902 * Provides a set of transmit, receive, and error stats to be added as an
903 * offset to the collect data when stats are retreived. Some devices may not
904 * support setting the stats, in which case the result will always be
905 * -EOPNOTSUPP. RTNL lock must be held.
907 int vport_set_stats(struct vport *vport, struct odp_vport_stats *stats)
911 if (vport->ops->flags & VPORT_F_GEN_STATS) {
912 spin_lock_bh(&vport->stats_lock);
913 memcpy(&vport->offset_stats, stats, sizeof(struct odp_vport_stats));
914 spin_unlock_bh(&vport->stats_lock);
917 } else if (vport->ops->set_stats)
918 return vport->ops->set_stats(vport, stats);
924 * vport_get_name - retrieve device name
926 * @vport: vport from which to retrieve the name.
928 * Retrieves the name of the given device. Either RTNL lock or rcu_read_lock
929 * must be held for the entire duration that the name is in use.
931 const char *vport_get_name(const struct vport *vport)
933 return vport->ops->get_name(vport);
937 * vport_get_type - retrieve device type
939 * @vport: vport from which to retrieve the type.
941 * Retrieves the type of the given device. Either RTNL lock or rcu_read_lock
942 * must be held for the entire duration that the type is in use.
944 const char *vport_get_type(const struct vport *vport)
946 return vport->ops->type;
950 * vport_get_addr - retrieve device Ethernet address (for kernel callers)
952 * @vport: vport from which to retrieve the Ethernet address.
954 * Retrieves the Ethernet address of the given device. Either RTNL lock or
955 * rcu_read_lock must be held for the entire duration that the Ethernet address
958 const unsigned char *vport_get_addr(const struct vport *vport)
960 return vport->ops->get_addr(vport);
964 * vport_get_dp_port - retrieve attached datapath port
966 * @vport: vport from which to retrieve the datapath port.
968 * Retrieves the attached datapath port or null if not attached. Either RTNL
969 * lock or rcu_read_lock must be held for the entire duration that the datapath
970 * port is being accessed.
972 struct dp_port *vport_get_dp_port(const struct vport *vport)
974 return rcu_dereference(vport->dp_port);
978 * vport_get_kobj - retrieve associated kobj
980 * @vport: vport from which to retrieve the associated kobj
982 * Retrieves the associated kobj or null if no kobj. The returned kobj is
983 * valid for as long as the vport exists.
985 struct kobject *vport_get_kobj(const struct vport *vport)
987 if (vport->ops->get_kobj)
988 return vport->ops->get_kobj(vport);
994 * vport_get_stats - retrieve device stats (for kernel callers)
996 * @vport: vport from which to retrieve the stats
997 * @stats: location to store stats
999 * Retrieves transmit, receive, and error stats for the given device.
1001 int vport_get_stats(struct vport *vport, struct odp_vport_stats *stats)
1003 struct odp_vport_stats dev_stats;
1004 struct odp_vport_stats *dev_statsp = NULL;
1007 if (vport->ops->get_stats) {
1008 if (vport->ops->flags & VPORT_F_GEN_STATS)
1009 dev_statsp = &dev_stats;
1014 err = vport->ops->get_stats(vport, dev_statsp);
1021 if (vport->ops->flags & VPORT_F_GEN_STATS) {
1024 /* We potentially have 3 sources of stats that need to be
1025 * combined: those we have collected (split into err_stats and
1026 * percpu_stats), offset_stats from set_stats(), and device
1027 * error stats from get_stats() (for errors that happen
1028 * downstream and therefore aren't reported through our
1029 * vport_record_error() function). */
1031 spin_lock_bh(&vport->stats_lock);
1033 memcpy(stats, &vport->offset_stats, sizeof(struct odp_vport_stats));
1035 stats->rx_errors += vport->err_stats.rx_errors
1036 + vport->err_stats.rx_frame_err
1037 + vport->err_stats.rx_over_err
1038 + vport->err_stats.rx_crc_err;
1039 stats->tx_errors += vport->err_stats.tx_errors;
1040 stats->tx_dropped += vport->err_stats.tx_dropped;
1041 stats->rx_dropped += vport->err_stats.rx_dropped;
1042 stats->rx_over_err += vport->err_stats.rx_over_err;
1043 stats->rx_crc_err += vport->err_stats.rx_crc_err;
1044 stats->rx_frame_err += vport->err_stats.rx_frame_err;
1045 stats->collisions += vport->err_stats.collisions;
1047 spin_unlock_bh(&vport->stats_lock);
1050 stats->rx_errors += dev_statsp->rx_errors;
1051 stats->tx_errors += dev_statsp->tx_errors;
1052 stats->rx_dropped += dev_statsp->rx_dropped;
1053 stats->tx_dropped += dev_statsp->tx_dropped;
1054 stats->rx_over_err += dev_statsp->rx_over_err;
1055 stats->rx_crc_err += dev_statsp->rx_crc_err;
1056 stats->rx_frame_err += dev_statsp->rx_frame_err;
1057 stats->collisions += dev_statsp->collisions;
1060 for_each_possible_cpu(i) {
1061 const struct vport_percpu_stats *percpu_stats;
1062 struct vport_percpu_stats local_stats;
1065 percpu_stats = per_cpu_ptr(vport->percpu_stats, i);
1068 seqcount = read_seqcount_begin(&percpu_stats->seqlock);
1069 local_stats = *percpu_stats;
1070 } while (read_seqcount_retry(&percpu_stats->seqlock, seqcount));
1072 stats->rx_bytes += local_stats.rx_bytes;
1073 stats->rx_packets += local_stats.rx_packets;
1074 stats->tx_bytes += local_stats.tx_bytes;
1075 stats->tx_packets += local_stats.tx_packets;
1087 * vport_get_flags - retrieve device flags
1089 * @vport: vport from which to retrieve the flags
1091 * Retrieves the flags of the given device. Either RTNL lock or rcu_read_lock
1094 unsigned vport_get_flags(const struct vport *vport)
1096 return vport->ops->get_dev_flags(vport);
1100 * vport_get_flags - check whether device is running
1102 * @vport: vport on which to check status.
1104 * Checks whether the given device is running. Either RTNL lock or
1105 * rcu_read_lock must be held.
1107 int vport_is_running(const struct vport *vport)
1109 return vport->ops->is_running(vport);
1113 * vport_get_flags - retrieve device operating state
1115 * @vport: vport from which to check status
1117 * Retrieves the RFC2863 operstate of the given device. Either RTNL lock or
1118 * rcu_read_lock must be held.
1120 unsigned char vport_get_operstate(const struct vport *vport)
1122 return vport->ops->get_operstate(vport);
1126 * vport_get_ifindex - retrieve device system interface index
1128 * @vport: vport from which to retrieve index
1130 * Retrieves the system interface index of the given device. Not all devices
1131 * will have system indexes, in which case the index of the datapath local
1132 * port is returned. Returns a negative index on error. Either RTNL lock or
1133 * rcu_read_lock must be held.
1135 int vport_get_ifindex(const struct vport *vport)
1137 const struct dp_port *dp_port;
1139 if (vport->ops->get_ifindex)
1140 return vport->ops->get_ifindex(vport);
1142 /* If we don't actually have an ifindex, use the local port's.
1143 * Userspace doesn't check it anyways. */
1144 dp_port = vport_get_dp_port(vport);
1148 return vport_get_ifindex(dp_port->dp->ports[ODPP_LOCAL]->vport);
1152 * vport_get_iflink - retrieve device system link index
1154 * @vport: vport from which to retrieve index
1156 * Retrieves the system link index of the given device. The link is the index
1157 * of the interface on which the packet will actually be sent. In most cases
1158 * this is the same as the ifindex but may be different for tunnel devices.
1159 * Returns a negative index on error. Either RTNL lock or rcu_read_lock must
1162 int vport_get_iflink(const struct vport *vport)
1164 if (vport->ops->get_iflink)
1165 return vport->ops->get_iflink(vport);
1167 /* If we don't have an iflink, use the ifindex. In most cases they
1169 return vport_get_ifindex(vport);
1173 * vport_get_mtu - retrieve device MTU (for kernel callers)
1175 * @vport: vport from which to retrieve MTU
1177 * Retrieves the MTU of the given device. Either RTNL lock or rcu_read_lock
1180 int vport_get_mtu(const struct vport *vport)
1182 return vport->ops->get_mtu(vport);
1186 * vport_receive - pass up received packet to the datapath for processing
1188 * @vport: vport that received the packet
1189 * @skb: skb that was received
1191 * Must be called with rcu_read_lock. The packet cannot be shared and
1192 * skb->data should point to the Ethernet header. The caller must have already
1193 * called compute_ip_summed() to initialize the checksumming fields.
1195 void vport_receive(struct vport *vport, struct sk_buff *skb)
1197 struct dp_port *dp_port = vport_get_dp_port(vport);
1200 vport_record_error(vport, VPORT_E_RX_DROPPED);
1206 if (vport->ops->flags & VPORT_F_GEN_STATS) {
1207 struct vport_percpu_stats *stats;
1210 stats = per_cpu_ptr(vport->percpu_stats, smp_processor_id());
1212 write_seqcount_begin(&stats->seqlock);
1213 stats->rx_packets++;
1214 stats->rx_bytes += skb->len;
1215 write_seqcount_end(&stats->seqlock);
1220 if (!(vport->ops->flags & VPORT_F_TUN_ID))
1221 OVS_CB(skb)->tun_id = 0;
1223 dp_process_received_packet(dp_port, skb);
1226 static inline unsigned packet_length(const struct sk_buff *skb)
1228 unsigned length = skb->len - ETH_HLEN;
1230 if (skb->protocol == htons(ETH_P_8021Q))
1231 length -= VLAN_HLEN;
1237 * vport_send - send a packet on a device
1239 * @vport: vport on which to send the packet
1242 * Sends the given packet and returns the length of data sent. Either RTNL
1243 * lock or rcu_read_lock must be held.
1245 int vport_send(struct vport *vport, struct sk_buff *skb)
1250 mtu = vport_get_mtu(vport);
1251 if (unlikely(packet_length(skb) > mtu && !skb_is_gso(skb))) {
1252 if (net_ratelimit())
1253 pr_warn("%s: dropped over-mtu packet: %d > %d\n",
1254 dp_name(vport_get_dp_port(vport)->dp),
1255 packet_length(skb), mtu);
1259 sent = vport->ops->send(vport, skb);
1261 if (vport->ops->flags & VPORT_F_GEN_STATS && sent > 0) {
1262 struct vport_percpu_stats *stats;
1265 stats = per_cpu_ptr(vport->percpu_stats, smp_processor_id());
1267 write_seqcount_begin(&stats->seqlock);
1268 stats->tx_packets++;
1269 stats->tx_bytes += sent;
1270 write_seqcount_end(&stats->seqlock);
1279 vport_record_error(vport, VPORT_E_TX_DROPPED);
1284 * vport_record_error - indicate device error to generic stats layer
1286 * @vport: vport that encountered the error
1287 * @err_type: one of enum vport_err_type types to indicate the error type
1289 * If using the vport generic stats layer indicate that an error of the given
1292 void vport_record_error(struct vport *vport, enum vport_err_type err_type)
1294 if (vport->ops->flags & VPORT_F_GEN_STATS) {
1296 spin_lock_bh(&vport->stats_lock);
1299 case VPORT_E_RX_DROPPED:
1300 vport->err_stats.rx_dropped++;
1303 case VPORT_E_RX_ERROR:
1304 vport->err_stats.rx_errors++;
1307 case VPORT_E_RX_FRAME:
1308 vport->err_stats.rx_frame_err++;
1311 case VPORT_E_RX_OVER:
1312 vport->err_stats.rx_over_err++;
1315 case VPORT_E_RX_CRC:
1316 vport->err_stats.rx_crc_err++;
1319 case VPORT_E_TX_DROPPED:
1320 vport->err_stats.tx_dropped++;
1323 case VPORT_E_TX_ERROR:
1324 vport->err_stats.tx_errors++;
1327 case VPORT_E_COLLISION:
1328 vport->err_stats.collisions++;
1332 spin_unlock_bh(&vport->stats_lock);