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 #include <linux/dcache.h>
10 #include <linux/etherdevice.h>
12 #include <linux/kernel.h>
13 #include <linux/list.h>
14 #include <linux/mutex.h>
15 #include <linux/percpu.h>
16 #include <linux/rtnetlink.h>
17 #include <linux/compat.h>
20 #include "vport-internal_dev.h"
22 /* List of statically compiled vport implementations. Don't forget to also
23 * add yours to the list at the bottom of vport.h. */
24 static struct vport_ops *base_vport_ops_list[] = {
31 static const struct vport_ops **vport_ops_list;
32 static int n_vport_types;
34 static struct hlist_head *dev_table;
35 #define VPORT_HASH_BUCKETS 1024
37 /* Both RTNL lock and vport_mutex need to be held when updating dev_table.
39 * If you use vport_locate and then perform some operations, you need to hold
40 * one of these locks if you don't want the vport to be deleted out from under
43 * If you get a reference to a vport through a dp_port, it is protected
44 * by RCU and you need to hold rcu_read_lock instead when reading.
46 * If multiple locks are taken, the hierarchy is:
51 static DEFINE_MUTEX(vport_mutex);
54 * vport_lock - acquire vport lock
56 * Acquire global vport lock. See above comment about locking requirements
57 * and specific function definitions. May sleep.
62 mutex_lock(&vport_mutex);
66 * vport_unlock - release vport lock
68 * Release lock acquired with vport_lock.
73 mutex_unlock(&vport_mutex);
76 #define ASSERT_VPORT() do { \
77 if (unlikely(!mutex_is_locked(&vport_mutex))) { \
78 printk(KERN_ERR "openvswitch: vport lock not held at %s (%d)\n", \
79 __FILE__, __LINE__); \
85 * vport_init - initialize vport subsystem
87 * Called at module load time to initialize the vport subsystem and any
88 * compiled in vport types.
96 dev_table = kzalloc(VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
103 vport_ops_list = kmalloc(ARRAY_SIZE(base_vport_ops_list) *
104 sizeof(struct vport_ops *), GFP_KERNEL);
105 if (!vport_ops_list) {
107 goto error_dev_table;
110 for (i = 0; i < ARRAY_SIZE(base_vport_ops_list); i++) {
111 struct vport_ops *new_ops = base_vport_ops_list[i];
114 err = new_ops->init();
119 vport_ops_list[n_vport_types++] = new_ops;
120 else if (new_ops->flags & VPORT_F_REQUIRED) {
142 for (i = 0; i < VPORT_HASH_BUCKETS; i++) {
143 struct hlist_head *bucket = &dev_table[i];
145 struct hlist_node *node, *next;
147 hlist_for_each_entry_safe(vport, node, next, bucket, hash_node)
156 * vport_exit - shutdown vport subsystem
158 * Called at module exit time to shutdown the vport subsystem and any
159 * initialized vport types.
168 for (i = 0; i < n_vport_types; i++) {
169 if (vport_ops_list[i]->exit)
170 vport_ops_list[i]->exit();
173 kfree(vport_ops_list);
178 do_vport_add(struct odp_vport_add *vport_config)
183 vport_config->port_type[VPORT_TYPE_SIZE - 1] = '\0';
184 vport_config->devname[IFNAMSIZ - 1] = '\0';
188 vport = vport_locate(vport_config->devname);
195 vport = vport_add(vport_config->devname, vport_config->port_type,
196 vport_config->config);
200 err = PTR_ERR(vport);
208 * vport_user_add - add vport device (for userspace callers)
210 * @uvport_config: New port configuration.
212 * Creates a new vport with the specified configuration (which is dependent
213 * on device type). This function is for userspace callers and assumes no
217 vport_user_add(const struct odp_vport_add __user *uvport_config)
219 struct odp_vport_add vport_config;
221 if (copy_from_user(&vport_config, uvport_config, sizeof(struct odp_vport_add)))
224 return do_vport_add(&vport_config);
229 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);
246 do_vport_mod(struct odp_vport_mod *vport_config)
251 vport_config->devname[IFNAMSIZ - 1] = '\0';
255 vport = vport_locate(vport_config->devname);
262 err = vport_mod(vport, vport_config->config);
271 * vport_user_mod - modify existing vport device (for userspace callers)
273 * @uvport_config: New configuration for vport
275 * Modifies an existing device with the specified configuration (which is
276 * dependent on device type). This function is for userspace callers and
277 * assumes no locks are held.
280 vport_user_mod(const struct odp_vport_mod __user *uvport_config)
282 struct odp_vport_mod vport_config;
284 if (copy_from_user(&vport_config, uvport_config, sizeof(struct odp_vport_mod)))
287 return do_vport_mod(&vport_config);
292 compat_vport_user_mod(struct compat_odp_vport_mod *ucompat)
294 struct compat_odp_vport_mod compat;
295 struct odp_vport_mod vport_config;
297 if (copy_from_user(&compat, ucompat, sizeof(struct compat_odp_vport_mod)))
300 memcpy(vport_config.devname, compat.devname, IFNAMSIZ);
301 vport_config.config = compat_ptr(compat.config);
303 return do_vport_mod(&vport_config);
308 * vport_user_del - delete existing vport device (for userspace callers)
310 * @udevname: Name of device to delete
312 * Deletes the specified device. Detaches the device from a datapath first
313 * if it is attached. Deleting the device will fail if it does not exist or it
314 * is the datapath local port. It is also possible to fail for less obvious
315 * reasons, such as lack of memory. This function is for userspace callers and
316 * assumes no locks are held.
319 vport_user_del(const char __user *udevname)
321 char devname[IFNAMSIZ];
323 struct dp_port *dp_port;
327 retval = strncpy_from_user(devname, udevname, IFNAMSIZ);
330 else if (retval >= IFNAMSIZ)
331 return -ENAMETOOLONG;
335 vport = vport_locate(devname);
341 dp_port = vport_get_dp_port(vport);
343 struct datapath *dp = dp_port->dp;
345 mutex_lock(&dp->mutex);
347 if (!strcmp(dp_name(dp), devname)) {
352 err = dp_detach_port(dp_port, 0);
355 mutex_unlock(&dp->mutex);
362 err = vport_del(vport);
371 * vport_user_stats_get - retrieve device stats (for userspace callers)
373 * @ustats_req: Stats request parameters.
375 * Retrieves transmit, receive, and error stats for the given device. This
376 * function is for userspace callers and assumes no locks are held.
379 vport_user_stats_get(struct odp_vport_stats_req __user *ustats_req)
381 struct odp_vport_stats_req stats_req;
385 if (copy_from_user(&stats_req, ustats_req, sizeof(struct odp_vport_stats_req)))
388 stats_req.devname[IFNAMSIZ - 1] = '\0';
392 vport = vport_locate(stats_req.devname);
398 err = vport_get_stats(vport, &stats_req.stats);
404 if (copy_to_user(ustats_req, &stats_req, sizeof(struct odp_vport_stats_req)))
411 * vport_user_stats_set - sets offset device stats (for userspace callers)
413 * @ustats_req: Stats set parameters.
415 * Provides a set of transmit, receive, and error stats to be added as an
416 * offset to the collect data when stats are retreived. Some devices may not
417 * support setting the stats, in which case the result will always be
418 * -EOPNOTSUPP. This function is for userspace callers and assumes no locks
422 vport_user_stats_set(struct odp_vport_stats_req __user *ustats_req)
424 struct odp_vport_stats_req stats_req;
428 if (copy_from_user(&stats_req, ustats_req, sizeof(struct odp_vport_stats_req)))
431 stats_req.devname[IFNAMSIZ - 1] = '\0';
436 vport = vport_locate(stats_req.devname);
442 err = vport_set_stats(vport, &stats_req.stats);
452 * vport_user_ether_get - retrieve device Ethernet address (for userspace callers)
454 * @uvport_ether: Ethernet address request parameters.
456 * Retrieves the Ethernet address of the given device. This function is for
457 * userspace callers and assumes no locks are held.
460 vport_user_ether_get(struct odp_vport_ether __user *uvport_ether)
462 struct odp_vport_ether vport_ether;
466 if (copy_from_user(&vport_ether, uvport_ether, sizeof(struct odp_vport_ether)))
469 vport_ether.devname[IFNAMSIZ - 1] = '\0';
473 vport = vport_locate(vport_ether.devname);
480 memcpy(vport_ether.ether_addr, vport_get_addr(vport), ETH_ALEN);
487 if (copy_to_user(uvport_ether, &vport_ether, sizeof(struct odp_vport_ether)))
494 * vport_user_ether_set - set device Ethernet address (for userspace callers)
496 * @uvport_ether: Ethernet address request parameters.
498 * Sets the Ethernet address of the given device. Some devices may not support
499 * setting the Ethernet address, in which case the result will always be
500 * -EOPNOTSUPP. This function is for userspace callers and assumes no locks
504 vport_user_ether_set(struct odp_vport_ether __user *uvport_ether)
506 struct odp_vport_ether vport_ether;
510 if (copy_from_user(&vport_ether, uvport_ether, sizeof(struct odp_vport_ether)))
513 vport_ether.devname[IFNAMSIZ - 1] = '\0';
518 vport = vport_locate(vport_ether.devname);
524 err = vport_set_addr(vport, vport_ether.ether_addr);
533 * vport_user_mtu_get - retrieve device MTU (for userspace callers)
535 * @uvport_mtu: MTU request parameters.
537 * Retrieves the MTU of the given device. This function is for userspace
538 * callers and assumes no locks are held.
541 vport_user_mtu_get(struct odp_vport_mtu __user *uvport_mtu)
543 struct odp_vport_mtu vport_mtu;
547 if (copy_from_user(&vport_mtu, uvport_mtu, sizeof(struct odp_vport_mtu)))
550 vport_mtu.devname[IFNAMSIZ - 1] = '\0';
554 vport = vport_locate(vport_mtu.devname);
560 vport_mtu.mtu = vport_get_mtu(vport);
566 if (copy_to_user(uvport_mtu, &vport_mtu, sizeof(struct odp_vport_mtu)))
573 * vport_user_mtu_set - set device MTU (for userspace callers)
575 * @uvport_mtu: MTU request parameters.
577 * Sets the MTU of the given device. Some devices may not support setting the
578 * MTU, in which case the result will always be -EOPNOTSUPP. This function is
579 * for userspace callers and assumes no locks are held.
582 vport_user_mtu_set(struct odp_vport_mtu __user *uvport_mtu)
584 struct odp_vport_mtu vport_mtu;
588 if (copy_from_user(&vport_mtu, uvport_mtu, sizeof(struct odp_vport_mtu)))
591 vport_mtu.devname[IFNAMSIZ - 1] = '\0';
596 vport = vport_locate(vport_mtu.devname);
602 err = vport_set_mtu(vport, vport_mtu.mtu);
610 static struct hlist_head *
611 hash_bucket(const char *name)
613 unsigned int hash = full_name_hash(name, strlen(name));
614 return &dev_table[hash & (VPORT_HASH_BUCKETS - 1)];
618 * vport_locate - find a port that has already been created
620 * @name: name of port to find
622 * Either RTNL or vport lock must be acquired before calling this function
623 * and held while using the found port. See the locking comments at the
627 vport_locate(const char *name)
629 struct hlist_head *bucket = hash_bucket(name);
631 struct hlist_node *node;
633 if (unlikely(!mutex_is_locked(&vport_mutex) && !rtnl_is_locked())) {
634 printk(KERN_ERR "openvswitch: neither RTNL nor vport lock held in vport_locate\n");
640 hlist_for_each_entry(vport, node, bucket, hash_node)
641 if (!strcmp(name, vport_get_name(vport)))
652 register_vport(struct vport *vport)
654 hlist_add_head(&vport->hash_node, hash_bucket(vport_get_name(vport)));
658 unregister_vport(struct vport *vport)
660 hlist_del(&vport->hash_node);
664 * vport_alloc - allocate and initialize new vport
666 * @priv_size: Size of private data area to allocate.
667 * @ops: vport device ops
669 * Allocate and initialize a new vport defined by @ops. The vport will contain
670 * a private data area of size @priv_size that can be accessed using
671 * vport_priv(). vports that are no longer needed should be released with
675 vport_alloc(int priv_size, const struct vport_ops *ops)
680 alloc_size = sizeof(struct vport);
682 alloc_size = ALIGN(alloc_size, VPORT_ALIGN);
683 alloc_size += priv_size;
686 vport = kzalloc(alloc_size, GFP_KERNEL);
688 return ERR_PTR(-ENOMEM);
692 if (vport->ops->flags & VPORT_F_GEN_STATS) {
693 vport->percpu_stats = alloc_percpu(struct vport_percpu_stats);
694 if (!vport->percpu_stats)
695 return ERR_PTR(-ENOMEM);
697 spin_lock_init(&vport->stats_lock);
704 * vport_free - uninitialize and free vport
706 * @vport: vport to free
708 * Frees a vport allocated with vport_alloc() when it is no longer needed.
711 vport_free(struct vport *vport)
713 if (vport->ops->flags & VPORT_F_GEN_STATS)
714 free_percpu(vport->percpu_stats);
720 * vport_add - add vport device (for kernel callers)
722 * @name: Name of new device.
723 * @type: Type of new device (to be matched against types in registered vport
725 * @config: Device type specific configuration. Userspace pointer.
727 * Creates a new vport with the specified configuration (which is dependent
728 * on device type). Both RTNL and vport locks must be held.
731 vport_add(const char *name, const char *type, const void __user *config)
740 for (i = 0; i < n_vport_types; i++) {
741 if (!strcmp(vport_ops_list[i]->type, type)) {
742 vport = vport_ops_list[i]->create(name, config);
744 err = PTR_ERR(vport);
748 register_vport(vport);
760 * vport_mod - modify existing vport device (for kernel callers)
762 * @vport: vport to modify.
763 * @config: Device type specific configuration. Userspace pointer.
765 * Modifies an existing device with the specified configuration (which is
766 * dependent on device type). Both RTNL and vport locks must be held.
769 vport_mod(struct vport *vport, const void __user *config)
774 if (vport->ops->modify)
775 return vport->ops->modify(vport, config);
781 * vport_del - delete existing vport device (for kernel callers)
783 * @vport: vport to delete.
785 * Deletes the specified device. The device must not be currently attached to
786 * a datapath. It is possible to fail for reasons such as lack of memory.
787 * Both RTNL and vport locks must be held.
790 vport_del(struct vport *vport)
794 BUG_ON(vport_get_dp_port(vport));
796 unregister_vport(vport);
798 return vport->ops->destroy(vport);
802 * vport_attach - attach a vport to a datapath
804 * @vport: vport to attach.
805 * @dp_port: Datapath port to attach the vport to.
807 * Attaches a vport to a specific datapath so that packets may be exchanged.
808 * Both ports must be currently unattached. @dp_port must be successfully
809 * attached to a vport before it is connected to a datapath and must not be
810 * modified while connected. RTNL lock and the appropriate DP mutex must be held.
813 vport_attach(struct vport *vport, struct dp_port *dp_port)
820 if (vport_get_dp_port(vport))
823 if (vport->ops->attach) {
826 err = vport->ops->attach(vport);
831 dp_port->vport = vport;
832 rcu_assign_pointer(vport->dp_port, dp_port);
838 * vport_detach - detach a vport from a datapath
840 * @vport: vport to detach.
842 * Detaches a vport from a datapath. May fail for a variety of reasons,
843 * including lack of memory. RTNL lock and the appropriate DP mutex must be held.
846 vport_detach(struct vport *vport)
848 struct dp_port *dp_port;
852 dp_port = vport_get_dp_port(vport);
856 dp_port->vport = NULL;
857 rcu_assign_pointer(vport->dp_port, NULL);
859 if (vport->ops->detach)
860 return vport->ops->detach(vport);
866 * vport_set_mtu - set device MTU (for kernel callers)
868 * @vport: vport on which to set MTU.
871 * Sets the MTU of the given device. Some devices may not support setting the
872 * MTU, in which case the result will always be -EOPNOTSUPP. RTNL lock must
876 vport_set_mtu(struct vport *vport, int mtu)
883 if (vport->ops->set_mtu) {
886 ret = vport->ops->set_mtu(vport, mtu);
888 if (!ret && !is_internal_vport(vport)) {
889 struct dp_port *dp_port = vport_get_dp_port(vport);
892 set_internal_devs_mtu(dp_port->dp);
901 * vport_set_addr - set device Ethernet address (for kernel callers)
903 * @vport: vport on which to set Ethernet address.
904 * @addr: New address.
906 * Sets the Ethernet address of the given device. Some devices may not support
907 * setting the Ethernet address, in which case the result will always be
908 * -EOPNOTSUPP. RTNL lock must be held.
911 vport_set_addr(struct vport *vport, const unsigned char *addr)
915 if (!is_valid_ether_addr(addr))
916 return -EADDRNOTAVAIL;
918 if (vport->ops->set_addr)
919 return vport->ops->set_addr(vport, addr);
925 * vport_set_stats - sets offset device stats (for kernel callers)
927 * @vport: vport on which to set stats
928 * @stats: stats to set
930 * Provides a set of transmit, receive, and error stats to be added as an
931 * offset to the collect data when stats are retreived. Some devices may not
932 * support setting the stats, in which case the result will always be
933 * -EOPNOTSUPP. RTNL lock must be held.
936 vport_set_stats(struct vport *vport, struct odp_vport_stats *stats)
940 if (vport->ops->flags & VPORT_F_GEN_STATS) {
941 spin_lock_bh(&vport->stats_lock);
942 memcpy(&vport->offset_stats, stats, sizeof(struct odp_vport_stats));
943 spin_unlock_bh(&vport->stats_lock);
946 } else if (vport->ops->set_stats)
947 return vport->ops->set_stats(vport, stats);
953 * vport_get_name - retrieve device name
955 * @vport: vport from which to retrieve the name.
957 * Retrieves the name of the given device. Either RTNL lock or rcu_read_lock
958 * must be held for the entire duration that the name is in use.
961 vport_get_name(const struct vport *vport)
963 return vport->ops->get_name(vport);
967 * vport_get_type - retrieve device type
969 * @vport: vport from which to retrieve the type.
971 * Retrieves the type of the given device. Either RTNL lock or rcu_read_lock
972 * must be held for the entire duration that the type is in use.
975 vport_get_type(const struct vport *vport)
977 return vport->ops->type;
981 * vport_get_addr - retrieve device Ethernet address (for kernel callers)
983 * @vport: vport from which to retrieve the Ethernet address.
985 * Retrieves the Ethernet address of the given device. Either RTNL lock or
986 * rcu_read_lock must be held for the entire duration that the Ethernet address
989 const unsigned char *
990 vport_get_addr(const struct vport *vport)
992 return vport->ops->get_addr(vport);
996 * vport_get_dp_port - retrieve attached datapath port
998 * @vport: vport from which to retrieve the datapath port.
1000 * Retrieves the attached datapath port or null if not attached. Either RTNL
1001 * lock or rcu_read_lock must be held for the entire duration that the datapath
1002 * port is being accessed.
1005 vport_get_dp_port(const struct vport *vport)
1007 return rcu_dereference(vport->dp_port);
1011 * vport_get_kobj - retrieve associated kobj
1013 * @vport: vport from which to retrieve the associated kobj
1015 * Retrieves the associated kobj or null if no kobj. The returned kobj is
1016 * valid for as long as the vport exists.
1019 vport_get_kobj(const struct vport *vport)
1021 if (vport->ops->get_kobj)
1022 return vport->ops->get_kobj(vport);
1028 * vport_get_stats - retrieve device stats (for kernel callers)
1030 * @vport: vport from which to retrieve the stats
1031 * @stats: location to store stats
1033 * Retrieves transmit, receive, and error stats for the given device.
1036 vport_get_stats(struct vport *vport, struct odp_vport_stats *stats)
1038 struct odp_vport_stats dev_stats;
1039 struct odp_vport_stats *dev_statsp = NULL;
1042 if (vport->ops->get_stats) {
1043 if (vport->ops->flags & VPORT_F_GEN_STATS)
1044 dev_statsp = &dev_stats;
1049 err = vport->ops->get_stats(vport, dev_statsp);
1056 if (vport->ops->flags & VPORT_F_GEN_STATS) {
1059 /* We potentially have 3 sources of stats that need to be
1060 * combined: those we have collected (split into err_stats and
1061 * percpu_stats), offset_stats from set_stats(), and device
1062 * error stats from get_stats() (for errors that happen
1063 * downstream and therefore aren't reported through our
1064 * vport_record_error() function). */
1066 spin_lock_bh(&vport->stats_lock);
1068 memcpy(stats, &vport->offset_stats, sizeof(struct odp_vport_stats));
1070 stats->rx_errors += vport->err_stats.rx_errors
1071 + vport->err_stats.rx_frame_err
1072 + vport->err_stats.rx_over_err
1073 + vport->err_stats.rx_crc_err;
1074 stats->tx_errors += vport->err_stats.tx_errors;
1075 stats->tx_dropped += vport->err_stats.tx_dropped;
1076 stats->rx_dropped += vport->err_stats.rx_dropped;
1077 stats->rx_over_err += vport->err_stats.rx_over_err;
1078 stats->rx_crc_err += vport->err_stats.rx_crc_err;
1079 stats->rx_frame_err += vport->err_stats.rx_frame_err;
1080 stats->collisions += vport->err_stats.collisions;
1082 spin_unlock_bh(&vport->stats_lock);
1085 stats->rx_errors += dev_statsp->rx_errors;
1086 stats->tx_errors += dev_statsp->tx_errors;
1087 stats->rx_dropped += dev_statsp->rx_dropped;
1088 stats->tx_dropped += dev_statsp->tx_dropped;
1089 stats->rx_over_err += dev_statsp->rx_over_err;
1090 stats->rx_crc_err += dev_statsp->rx_crc_err;
1091 stats->rx_frame_err += dev_statsp->rx_frame_err;
1092 stats->collisions += dev_statsp->collisions;
1095 for_each_possible_cpu(i) {
1096 const struct vport_percpu_stats *percpu_stats;
1098 percpu_stats = per_cpu_ptr(vport->percpu_stats, i);
1099 stats->rx_bytes += percpu_stats->rx_bytes;
1100 stats->rx_packets += percpu_stats->rx_packets;
1101 stats->tx_bytes += percpu_stats->tx_bytes;
1102 stats->tx_packets += percpu_stats->tx_packets;
1114 * vport_get_flags - retrieve device flags
1116 * @vport: vport from which to retrieve the flags
1118 * Retrieves the flags of the given device. Either RTNL lock or rcu_read_lock
1122 vport_get_flags(const struct vport *vport)
1124 return vport->ops->get_dev_flags(vport);
1128 * vport_get_flags - check whether device is running
1130 * @vport: vport on which to check status.
1132 * Checks whether the given device is running. Either RTNL lock or
1133 * rcu_read_lock must be held.
1136 vport_is_running(const struct vport *vport)
1138 return vport->ops->is_running(vport);
1142 * vport_get_flags - retrieve device operating state
1144 * @vport: vport from which to check status
1146 * Retrieves the RFC2863 operstate of the given device. Either RTNL lock or
1147 * rcu_read_lock must be held.
1150 vport_get_operstate(const struct vport *vport)
1152 return vport->ops->get_operstate(vport);
1156 * vport_get_ifindex - retrieve device system interface index
1158 * @vport: vport from which to retrieve index
1160 * Retrieves the system interface index of the given device. Not all devices
1161 * will have system indexes, in which case the index of the datapath local
1162 * port is returned. Returns a negative index on error. Either RTNL lock or
1163 * rcu_read_lock must be held.
1166 vport_get_ifindex(const struct vport *vport)
1168 const struct dp_port *dp_port;
1170 if (vport->ops->get_ifindex)
1171 return vport->ops->get_ifindex(vport);
1173 /* If we don't actually have an ifindex, use the local port's.
1174 * Userspace doesn't check it anyways. */
1175 dp_port = vport_get_dp_port(vport);
1179 return vport_get_ifindex(dp_port->dp->ports[ODPP_LOCAL]->vport);
1183 * vport_get_iflink - retrieve device system link index
1185 * @vport: vport from which to retrieve index
1187 * Retrieves the system link index of the given device. The link is the index
1188 * of the interface on which the packet will actually be sent. In most cases
1189 * this is the same as the ifindex but may be different for tunnel devices.
1190 * Returns a negative index on error. Either RTNL lock or rcu_read_lock must
1194 vport_get_iflink(const struct vport *vport)
1196 if (vport->ops->get_iflink)
1197 return vport->ops->get_iflink(vport);
1199 /* If we don't have an iflink, use the ifindex. In most cases they
1201 return vport_get_ifindex(vport);
1205 * vport_get_mtu - retrieve device MTU (for kernel callers)
1207 * @vport: vport from which to retrieve MTU
1209 * Retrieves the MTU of the given device. Either RTNL lock or rcu_read_lock
1213 vport_get_mtu(const struct vport *vport)
1215 return vport->ops->get_mtu(vport);
1219 * vport_receive - pass up received packet to the datapath for processing
1221 * @vport: vport that received the packet
1222 * @skb: skb that was received
1224 * Must be called with rcu_read_lock. The packet cannot be shared and
1225 * skb->data should point to the Ethernet header. The caller must have already
1226 * called compute_ip_summed() to initialize the checksumming fields.
1229 vport_receive(struct vport *vport, struct sk_buff *skb)
1231 struct dp_port *dp_port = vport_get_dp_port(vport);
1234 vport_record_error(vport, VPORT_E_RX_DROPPED);
1240 if (vport->ops->flags & VPORT_F_GEN_STATS) {
1241 struct vport_percpu_stats *stats;
1245 stats = per_cpu_ptr(vport->percpu_stats, smp_processor_id());
1246 stats->rx_packets++;
1247 stats->rx_bytes += skb->len;
1252 if (!(vport->ops->flags & VPORT_F_TUN_ID))
1253 OVS_CB(skb)->tun_id = 0;
1255 dp_process_received_packet(dp_port, skb);
1259 * vport_send - send a packet on a device
1261 * @vport: vport on which to send the packet
1264 * Sends the given packet and returns the length of data sent. Either RTNL
1265 * lock or rcu_read_lock must be held.
1268 vport_send(struct vport *vport, struct sk_buff *skb)
1272 sent = vport->ops->send(vport, skb);
1274 if (vport->ops->flags & VPORT_F_GEN_STATS && sent > 0) {
1275 struct vport_percpu_stats *stats;
1279 stats = per_cpu_ptr(vport->percpu_stats, smp_processor_id());
1280 stats->tx_packets++;
1281 stats->tx_bytes += sent;
1290 * vport_record_error - indicate device error to generic stats layer
1292 * @vport: vport that encountered the error
1293 * @err_type: one of enum vport_err_type types to indicate the error type
1295 * If using the vport generic stats layer indicate that an error of the given
1299 vport_record_error(struct vport *vport, enum vport_err_type err_type)
1301 if (vport->ops->flags & VPORT_F_GEN_STATS) {
1303 spin_lock_bh(&vport->stats_lock);
1306 case VPORT_E_RX_DROPPED:
1307 vport->err_stats.rx_dropped++;
1310 case VPORT_E_RX_ERROR:
1311 vport->err_stats.rx_errors++;
1314 case VPORT_E_RX_FRAME:
1315 vport->err_stats.rx_frame_err++;
1318 case VPORT_E_RX_OVER:
1319 vport->err_stats.rx_over_err++;
1322 case VPORT_E_RX_CRC:
1323 vport->err_stats.rx_crc_err++;
1326 case VPORT_E_TX_DROPPED:
1327 vport->err_stats.tx_dropped++;
1330 case VPORT_E_TX_ERROR:
1331 vport->err_stats.tx_errors++;
1334 case VPORT_E_COLLISION:
1335 vport->err_stats.collisions++;
1339 spin_unlock_bh(&vport->stats_lock);