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/if_vlan.h>
13 #include <linux/kernel.h>
14 #include <linux/list.h>
15 #include <linux/mutex.h>
16 #include <linux/percpu.h>
17 #include <linux/rtnetlink.h>
18 #include <linux/compat.h>
19 #include <linux/version.h>
22 #include "vport-internal_dev.h"
24 /* List of statically compiled vport implementations. Don't forget to also
25 * add yours to the list at the bottom of vport.h. */
26 static struct vport_ops *base_vport_ops_list[] = {
31 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
36 static const struct vport_ops **vport_ops_list;
37 static int n_vport_types;
39 static struct hlist_head *dev_table;
40 #define VPORT_HASH_BUCKETS 1024
42 /* Both RTNL lock and vport_mutex need to be held when updating dev_table.
44 * If you use vport_locate and then perform some operations, you need to hold
45 * one of these locks if you don't want the vport to be deleted out from under
48 * If you get a reference to a vport through a dp_port, it is protected
49 * by RCU and you need to hold rcu_read_lock instead when reading.
51 * If multiple locks are taken, the hierarchy is:
56 static DEFINE_MUTEX(vport_mutex);
59 * vport_lock - acquire vport lock
61 * Acquire global vport lock. See above comment about locking requirements
62 * and specific function definitions. May sleep.
66 mutex_lock(&vport_mutex);
70 * vport_unlock - release vport lock
72 * Release lock acquired with vport_lock.
74 void vport_unlock(void)
76 mutex_unlock(&vport_mutex);
79 #define ASSERT_VPORT() do { \
80 if (unlikely(!mutex_is_locked(&vport_mutex))) { \
81 printk(KERN_ERR "openvswitch: vport lock not held at %s (%d)\n", \
82 __FILE__, __LINE__); \
88 * vport_init - initialize vport subsystem
90 * Called at module load time to initialize the vport subsystem and any
91 * compiled in vport types.
98 dev_table = kzalloc(VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
105 vport_ops_list = kmalloc(ARRAY_SIZE(base_vport_ops_list) *
106 sizeof(struct vport_ops *), GFP_KERNEL);
107 if (!vport_ops_list) {
109 goto error_dev_table;
112 for (i = 0; i < ARRAY_SIZE(base_vport_ops_list); i++) {
113 struct vport_ops *new_ops = base_vport_ops_list[i];
116 err = new_ops->init();
121 vport_ops_list[n_vport_types++] = new_ops;
122 else if (new_ops->flags & VPORT_F_REQUIRED) {
136 static void vport_del_all(void)
143 for (i = 0; i < VPORT_HASH_BUCKETS; i++) {
144 struct hlist_head *bucket = &dev_table[i];
146 struct hlist_node *node, *next;
148 hlist_for_each_entry_safe(vport, node, next, bucket, hash_node)
157 * vport_exit - shutdown vport subsystem
159 * Called at module exit time to shutdown the vport subsystem and any
160 * initialized vport types.
162 void vport_exit(void)
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);
177 static int do_vport_add(struct odp_vport_add *vport_config)
182 vport_config->port_type[VPORT_TYPE_SIZE - 1] = '\0';
183 vport_config->devname[IFNAMSIZ - 1] = '\0';
187 vport = vport_locate(vport_config->devname);
194 vport = vport_add(vport_config->devname, vport_config->port_type,
195 vport_config->config);
199 err = PTR_ERR(vport);
207 * vport_user_add - add vport device (for userspace callers)
209 * @uvport_config: New port configuration.
211 * Creates a new vport with the specified configuration (which is dependent
212 * on device type). This function is for userspace callers and assumes no
215 int vport_user_add(const struct odp_vport_add __user *uvport_config)
217 struct odp_vport_add vport_config;
219 if (copy_from_user(&vport_config, uvport_config, sizeof(struct odp_vport_add)))
222 return do_vport_add(&vport_config);
226 int compat_vport_user_add(struct compat_odp_vport_add *ucompat)
228 struct compat_odp_vport_add compat;
229 struct odp_vport_add vport_config;
231 if (copy_from_user(&compat, ucompat, sizeof(struct compat_odp_vport_add)))
234 memcpy(vport_config.port_type, compat.port_type, VPORT_TYPE_SIZE);
235 memcpy(vport_config.devname, compat.devname, IFNAMSIZ);
236 vport_config.config = compat_ptr(compat.config);
238 return do_vport_add(&vport_config);
242 static int do_vport_mod(struct odp_vport_mod *vport_config)
247 vport_config->devname[IFNAMSIZ - 1] = '\0';
251 vport = vport_locate(vport_config->devname);
258 err = vport_mod(vport, vport_config->config);
267 * vport_user_mod - modify existing vport device (for userspace callers)
269 * @uvport_config: New configuration for vport
271 * Modifies an existing device with the specified configuration (which is
272 * dependent on device type). This function is for userspace callers and
273 * assumes no locks are held.
275 int vport_user_mod(const struct odp_vport_mod __user *uvport_config)
277 struct odp_vport_mod vport_config;
279 if (copy_from_user(&vport_config, uvport_config, sizeof(struct odp_vport_mod)))
282 return do_vport_mod(&vport_config);
286 int compat_vport_user_mod(struct compat_odp_vport_mod *ucompat)
288 struct compat_odp_vport_mod compat;
289 struct odp_vport_mod vport_config;
291 if (copy_from_user(&compat, ucompat, sizeof(struct compat_odp_vport_mod)))
294 memcpy(vport_config.devname, compat.devname, IFNAMSIZ);
295 vport_config.config = compat_ptr(compat.config);
297 return do_vport_mod(&vport_config);
302 * vport_user_del - delete existing vport device (for userspace callers)
304 * @udevname: Name of device to delete
306 * Deletes the specified device. Detaches the device from a datapath first
307 * if it is attached. Deleting the device will fail if it does not exist or it
308 * is the datapath local port. It is also possible to fail for less obvious
309 * reasons, such as lack of memory. This function is for userspace callers and
310 * assumes no locks are held.
312 int vport_user_del(const char __user *udevname)
314 char devname[IFNAMSIZ];
316 struct dp_port *dp_port;
320 retval = strncpy_from_user(devname, udevname, IFNAMSIZ);
323 else if (retval >= IFNAMSIZ)
324 return -ENAMETOOLONG;
328 vport = vport_locate(devname);
334 dp_port = vport_get_dp_port(vport);
336 struct datapath *dp = dp_port->dp;
338 mutex_lock(&dp->mutex);
340 if (!strcmp(dp_name(dp), devname)) {
345 err = dp_detach_port(dp_port, 0);
348 mutex_unlock(&dp->mutex);
355 err = vport_del(vport);
364 * vport_user_stats_get - retrieve device stats (for userspace callers)
366 * @ustats_req: Stats request parameters.
368 * Retrieves transmit, receive, and error stats for the given device. This
369 * function is for userspace callers and assumes no locks are held.
371 int vport_user_stats_get(struct odp_vport_stats_req __user *ustats_req)
373 struct odp_vport_stats_req stats_req;
377 if (copy_from_user(&stats_req, ustats_req, sizeof(struct odp_vport_stats_req)))
380 stats_req.devname[IFNAMSIZ - 1] = '\0';
384 vport = vport_locate(stats_req.devname);
390 err = vport_get_stats(vport, &stats_req.stats);
396 if (copy_to_user(ustats_req, &stats_req, sizeof(struct odp_vport_stats_req)))
403 * vport_user_stats_set - sets offset device stats (for userspace callers)
405 * @ustats_req: Stats set parameters.
407 * Provides a set of transmit, receive, and error stats to be added as an
408 * offset to the collect data when stats are retreived. Some devices may not
409 * support setting the stats, in which case the result will always be
410 * -EOPNOTSUPP. This function is for userspace callers and assumes no locks
413 int vport_user_stats_set(struct odp_vport_stats_req __user *ustats_req)
415 struct odp_vport_stats_req stats_req;
419 if (copy_from_user(&stats_req, ustats_req, sizeof(struct odp_vport_stats_req)))
422 stats_req.devname[IFNAMSIZ - 1] = '\0';
427 vport = vport_locate(stats_req.devname);
433 err = vport_set_stats(vport, &stats_req.stats);
443 * vport_user_ether_get - retrieve device Ethernet address (for userspace callers)
445 * @uvport_ether: Ethernet address request parameters.
447 * Retrieves the Ethernet address of the given device. This function is for
448 * userspace callers and assumes no locks are held.
450 int vport_user_ether_get(struct odp_vport_ether __user *uvport_ether)
452 struct odp_vport_ether vport_ether;
456 if (copy_from_user(&vport_ether, uvport_ether, sizeof(struct odp_vport_ether)))
459 vport_ether.devname[IFNAMSIZ - 1] = '\0';
463 vport = vport_locate(vport_ether.devname);
470 memcpy(vport_ether.ether_addr, vport_get_addr(vport), ETH_ALEN);
477 if (copy_to_user(uvport_ether, &vport_ether, sizeof(struct odp_vport_ether)))
484 * vport_user_ether_set - set device Ethernet address (for userspace callers)
486 * @uvport_ether: Ethernet address request parameters.
488 * Sets the Ethernet address of the given device. Some devices may not support
489 * setting the Ethernet address, in which case the result will always be
490 * -EOPNOTSUPP. This function is for userspace callers and assumes no locks
493 int vport_user_ether_set(struct odp_vport_ether __user *uvport_ether)
495 struct odp_vport_ether vport_ether;
499 if (copy_from_user(&vport_ether, uvport_ether, sizeof(struct odp_vport_ether)))
502 vport_ether.devname[IFNAMSIZ - 1] = '\0';
507 vport = vport_locate(vport_ether.devname);
513 err = vport_set_addr(vport, vport_ether.ether_addr);
522 * vport_user_mtu_get - retrieve device MTU (for userspace callers)
524 * @uvport_mtu: MTU request parameters.
526 * Retrieves the MTU of the given device. This function is for userspace
527 * callers and assumes no locks are held.
529 int vport_user_mtu_get(struct odp_vport_mtu __user *uvport_mtu)
531 struct odp_vport_mtu vport_mtu;
535 if (copy_from_user(&vport_mtu, uvport_mtu, sizeof(struct odp_vport_mtu)))
538 vport_mtu.devname[IFNAMSIZ - 1] = '\0';
542 vport = vport_locate(vport_mtu.devname);
548 vport_mtu.mtu = vport_get_mtu(vport);
554 if (copy_to_user(uvport_mtu, &vport_mtu, sizeof(struct odp_vport_mtu)))
561 * vport_user_mtu_set - set device MTU (for userspace callers)
563 * @uvport_mtu: MTU request parameters.
565 * Sets the MTU of the given device. Some devices may not support setting the
566 * MTU, in which case the result will always be -EOPNOTSUPP. This function is
567 * for userspace callers and assumes no locks are held.
569 int vport_user_mtu_set(struct odp_vport_mtu __user *uvport_mtu)
571 struct odp_vport_mtu vport_mtu;
575 if (copy_from_user(&vport_mtu, uvport_mtu, sizeof(struct odp_vport_mtu)))
578 vport_mtu.devname[IFNAMSIZ - 1] = '\0';
583 vport = vport_locate(vport_mtu.devname);
589 err = vport_set_mtu(vport, vport_mtu.mtu);
597 static struct hlist_head *hash_bucket(const char *name)
599 unsigned int hash = full_name_hash(name, strlen(name));
600 return &dev_table[hash & (VPORT_HASH_BUCKETS - 1)];
604 * vport_locate - find a port that has already been created
606 * @name: name of port to find
608 * Either RTNL or vport lock must be acquired before calling this function
609 * and held while using the found port. See the locking comments at the
612 struct vport *vport_locate(const char *name)
614 struct hlist_head *bucket = hash_bucket(name);
616 struct hlist_node *node;
618 if (unlikely(!mutex_is_locked(&vport_mutex) && !rtnl_is_locked())) {
619 printk(KERN_ERR "openvswitch: neither RTNL nor vport lock held in vport_locate\n");
625 hlist_for_each_entry(vport, node, bucket, hash_node)
626 if (!strcmp(name, vport_get_name(vport)))
636 static void register_vport(struct vport *vport)
638 hlist_add_head(&vport->hash_node, hash_bucket(vport_get_name(vport)));
641 static void unregister_vport(struct vport *vport)
643 hlist_del(&vport->hash_node);
647 * vport_alloc - allocate and initialize new vport
649 * @priv_size: Size of private data area to allocate.
650 * @ops: vport device ops
652 * Allocate and initialize a new vport defined by @ops. The vport will contain
653 * a private data area of size @priv_size that can be accessed using
654 * vport_priv(). vports that are no longer needed should be released with
657 struct vport *vport_alloc(int priv_size, const struct vport_ops *ops)
662 alloc_size = sizeof(struct vport);
664 alloc_size = ALIGN(alloc_size, VPORT_ALIGN);
665 alloc_size += priv_size;
668 vport = kzalloc(alloc_size, GFP_KERNEL);
670 return ERR_PTR(-ENOMEM);
674 if (vport->ops->flags & VPORT_F_GEN_STATS) {
675 vport->percpu_stats = alloc_percpu(struct vport_percpu_stats);
676 if (!vport->percpu_stats)
677 return ERR_PTR(-ENOMEM);
679 spin_lock_init(&vport->stats_lock);
686 * vport_free - uninitialize and free vport
688 * @vport: vport to free
690 * Frees a vport allocated with vport_alloc() when it is no longer needed.
692 void vport_free(struct vport *vport)
694 if (vport->ops->flags & VPORT_F_GEN_STATS)
695 free_percpu(vport->percpu_stats);
701 * vport_add - add vport device (for kernel callers)
703 * @name: Name of new device.
704 * @type: Type of new device (to be matched against types in registered vport
706 * @config: Device type specific configuration. Userspace pointer.
708 * Creates a new vport with the specified configuration (which is dependent
709 * on device type). Both RTNL and vport locks must be held.
711 struct vport *vport_add(const char *name, const char *type, const void __user *config)
720 for (i = 0; i < n_vport_types; i++) {
721 if (!strcmp(vport_ops_list[i]->type, type)) {
722 vport = vport_ops_list[i]->create(name, config);
724 err = PTR_ERR(vport);
728 register_vport(vport);
740 * vport_mod - modify existing vport device (for kernel callers)
742 * @vport: vport to modify.
743 * @config: Device type specific configuration. Userspace pointer.
745 * Modifies an existing device with the specified configuration (which is
746 * dependent on device type). Both RTNL and vport locks must be held.
748 int vport_mod(struct vport *vport, const void __user *config)
753 if (vport->ops->modify)
754 return vport->ops->modify(vport, config);
760 * vport_del - delete existing vport device (for kernel callers)
762 * @vport: vport to delete.
764 * Deletes the specified device. The device must not be currently attached to
765 * a datapath. It is possible to fail for reasons such as lack of memory.
766 * Both RTNL and vport locks must be held.
768 int vport_del(struct vport *vport)
772 BUG_ON(vport_get_dp_port(vport));
774 unregister_vport(vport);
776 return vport->ops->destroy(vport);
780 * vport_attach - attach a vport to a datapath
782 * @vport: vport to attach.
783 * @dp_port: Datapath port to attach the vport to.
785 * Attaches a vport to a specific datapath so that packets may be exchanged.
786 * Both ports must be currently unattached. @dp_port must be successfully
787 * attached to a vport before it is connected to a datapath and must not be
788 * modified while connected. RTNL lock and the appropriate DP mutex must be held.
790 int vport_attach(struct vport *vport, struct dp_port *dp_port)
794 if (vport_get_dp_port(vport))
797 if (vport->ops->attach) {
800 err = vport->ops->attach(vport);
805 rcu_assign_pointer(vport->dp_port, dp_port);
811 * vport_detach - detach a vport from a datapath
813 * @vport: vport to detach.
815 * Detaches a vport from a datapath. May fail for a variety of reasons,
816 * including lack of memory. RTNL lock and the appropriate DP mutex must be held.
818 int vport_detach(struct vport *vport)
820 struct dp_port *dp_port;
824 dp_port = vport_get_dp_port(vport);
828 rcu_assign_pointer(vport->dp_port, NULL);
830 if (vport->ops->detach)
831 return vport->ops->detach(vport);
837 * vport_set_mtu - set device MTU (for kernel callers)
839 * @vport: vport on which to set MTU.
842 * Sets the MTU of the given device. Some devices may not support setting the
843 * MTU, in which case the result will always be -EOPNOTSUPP. RTNL lock must
846 int vport_set_mtu(struct vport *vport, int mtu)
853 if (vport->ops->set_mtu) {
856 ret = vport->ops->set_mtu(vport, mtu);
858 if (!ret && !is_internal_vport(vport)) {
859 struct dp_port *dp_port = vport_get_dp_port(vport);
862 set_internal_devs_mtu(dp_port->dp);
871 * vport_set_addr - set device Ethernet address (for kernel callers)
873 * @vport: vport on which to set Ethernet address.
874 * @addr: New address.
876 * Sets the Ethernet address of the given device. Some devices may not support
877 * setting the Ethernet address, in which case the result will always be
878 * -EOPNOTSUPP. RTNL lock must be held.
880 int vport_set_addr(struct vport *vport, const unsigned char *addr)
884 if (!is_valid_ether_addr(addr))
885 return -EADDRNOTAVAIL;
887 if (vport->ops->set_addr)
888 return vport->ops->set_addr(vport, addr);
894 * vport_set_stats - sets offset device stats (for kernel callers)
896 * @vport: vport on which to set stats
897 * @stats: stats to set
899 * Provides a set of transmit, receive, and error stats to be added as an
900 * offset to the collect data when stats are retreived. Some devices may not
901 * support setting the stats, in which case the result will always be
902 * -EOPNOTSUPP. RTNL lock must be held.
904 int vport_set_stats(struct vport *vport, struct odp_vport_stats *stats)
908 if (vport->ops->flags & VPORT_F_GEN_STATS) {
909 spin_lock_bh(&vport->stats_lock);
910 memcpy(&vport->offset_stats, stats, sizeof(struct odp_vport_stats));
911 spin_unlock_bh(&vport->stats_lock);
914 } else if (vport->ops->set_stats)
915 return vport->ops->set_stats(vport, stats);
921 * vport_get_name - retrieve device name
923 * @vport: vport from which to retrieve the name.
925 * Retrieves the name of the given device. Either RTNL lock or rcu_read_lock
926 * must be held for the entire duration that the name is in use.
928 const char *vport_get_name(const struct vport *vport)
930 return vport->ops->get_name(vport);
934 * vport_get_type - retrieve device type
936 * @vport: vport from which to retrieve the type.
938 * Retrieves the type of the given device. Either RTNL lock or rcu_read_lock
939 * must be held for the entire duration that the type is in use.
941 const char *vport_get_type(const struct vport *vport)
943 return vport->ops->type;
947 * vport_get_addr - retrieve device Ethernet address (for kernel callers)
949 * @vport: vport from which to retrieve the Ethernet address.
951 * Retrieves the Ethernet address of the given device. Either RTNL lock or
952 * rcu_read_lock must be held for the entire duration that the Ethernet address
955 const unsigned char *vport_get_addr(const struct vport *vport)
957 return vport->ops->get_addr(vport);
961 * vport_get_dp_port - retrieve attached datapath port
963 * @vport: vport from which to retrieve the datapath port.
965 * Retrieves the attached datapath port or null if not attached. Either RTNL
966 * lock or rcu_read_lock must be held for the entire duration that the datapath
967 * port is being accessed.
969 struct dp_port *vport_get_dp_port(const struct vport *vport)
971 return rcu_dereference(vport->dp_port);
975 * vport_get_kobj - retrieve associated kobj
977 * @vport: vport from which to retrieve the associated kobj
979 * Retrieves the associated kobj or null if no kobj. The returned kobj is
980 * valid for as long as the vport exists.
982 struct kobject *vport_get_kobj(const struct vport *vport)
984 if (vport->ops->get_kobj)
985 return vport->ops->get_kobj(vport);
991 * vport_get_stats - retrieve device stats (for kernel callers)
993 * @vport: vport from which to retrieve the stats
994 * @stats: location to store stats
996 * Retrieves transmit, receive, and error stats for the given device.
998 int vport_get_stats(struct vport *vport, struct odp_vport_stats *stats)
1000 struct odp_vport_stats dev_stats;
1001 struct odp_vport_stats *dev_statsp = NULL;
1004 if (vport->ops->get_stats) {
1005 if (vport->ops->flags & VPORT_F_GEN_STATS)
1006 dev_statsp = &dev_stats;
1011 err = vport->ops->get_stats(vport, dev_statsp);
1018 if (vport->ops->flags & VPORT_F_GEN_STATS) {
1021 /* We potentially have 3 sources of stats that need to be
1022 * combined: those we have collected (split into err_stats and
1023 * percpu_stats), offset_stats from set_stats(), and device
1024 * error stats from get_stats() (for errors that happen
1025 * downstream and therefore aren't reported through our
1026 * vport_record_error() function). */
1028 spin_lock_bh(&vport->stats_lock);
1030 memcpy(stats, &vport->offset_stats, sizeof(struct odp_vport_stats));
1032 stats->rx_errors += vport->err_stats.rx_errors
1033 + vport->err_stats.rx_frame_err
1034 + vport->err_stats.rx_over_err
1035 + vport->err_stats.rx_crc_err;
1036 stats->tx_errors += vport->err_stats.tx_errors;
1037 stats->tx_dropped += vport->err_stats.tx_dropped;
1038 stats->rx_dropped += vport->err_stats.rx_dropped;
1039 stats->rx_over_err += vport->err_stats.rx_over_err;
1040 stats->rx_crc_err += vport->err_stats.rx_crc_err;
1041 stats->rx_frame_err += vport->err_stats.rx_frame_err;
1042 stats->collisions += vport->err_stats.collisions;
1044 spin_unlock_bh(&vport->stats_lock);
1047 stats->rx_errors += dev_statsp->rx_errors;
1048 stats->tx_errors += dev_statsp->tx_errors;
1049 stats->rx_dropped += dev_statsp->rx_dropped;
1050 stats->tx_dropped += dev_statsp->tx_dropped;
1051 stats->rx_over_err += dev_statsp->rx_over_err;
1052 stats->rx_crc_err += dev_statsp->rx_crc_err;
1053 stats->rx_frame_err += dev_statsp->rx_frame_err;
1054 stats->collisions += dev_statsp->collisions;
1057 for_each_possible_cpu(i) {
1058 const struct vport_percpu_stats *percpu_stats;
1059 struct vport_percpu_stats local_stats;
1062 percpu_stats = per_cpu_ptr(vport->percpu_stats, i);
1065 seqcount = read_seqcount_begin(&percpu_stats->seqlock);
1066 local_stats = *percpu_stats;
1067 } while (read_seqcount_retry(&percpu_stats->seqlock, seqcount));
1069 stats->rx_bytes += local_stats.rx_bytes;
1070 stats->rx_packets += local_stats.rx_packets;
1071 stats->tx_bytes += local_stats.tx_bytes;
1072 stats->tx_packets += local_stats.tx_packets;
1084 * vport_get_flags - retrieve device flags
1086 * @vport: vport from which to retrieve the flags
1088 * Retrieves the flags of the given device. Either RTNL lock or rcu_read_lock
1091 unsigned vport_get_flags(const struct vport *vport)
1093 return vport->ops->get_dev_flags(vport);
1097 * vport_get_flags - check whether device is running
1099 * @vport: vport on which to check status.
1101 * Checks whether the given device is running. Either RTNL lock or
1102 * rcu_read_lock must be held.
1104 int vport_is_running(const struct vport *vport)
1106 return vport->ops->is_running(vport);
1110 * vport_get_flags - retrieve device operating state
1112 * @vport: vport from which to check status
1114 * Retrieves the RFC2863 operstate of the given device. Either RTNL lock or
1115 * rcu_read_lock must be held.
1117 unsigned char vport_get_operstate(const struct vport *vport)
1119 return vport->ops->get_operstate(vport);
1123 * vport_get_ifindex - retrieve device system interface index
1125 * @vport: vport from which to retrieve index
1127 * Retrieves the system interface index of the given device. Not all devices
1128 * will have system indexes, in which case the index of the datapath local
1129 * port is returned. Returns a negative index on error. Either RTNL lock or
1130 * rcu_read_lock must be held.
1132 int vport_get_ifindex(const struct vport *vport)
1134 const struct dp_port *dp_port;
1136 if (vport->ops->get_ifindex)
1137 return vport->ops->get_ifindex(vport);
1139 /* If we don't actually have an ifindex, use the local port's.
1140 * Userspace doesn't check it anyways. */
1141 dp_port = vport_get_dp_port(vport);
1145 return vport_get_ifindex(dp_port->dp->ports[ODPP_LOCAL]->vport);
1149 * vport_get_iflink - retrieve device system link index
1151 * @vport: vport from which to retrieve index
1153 * Retrieves the system link index of the given device. The link is the index
1154 * of the interface on which the packet will actually be sent. In most cases
1155 * this is the same as the ifindex but may be different for tunnel devices.
1156 * Returns a negative index on error. Either RTNL lock or rcu_read_lock must
1159 int vport_get_iflink(const struct vport *vport)
1161 if (vport->ops->get_iflink)
1162 return vport->ops->get_iflink(vport);
1164 /* If we don't have an iflink, use the ifindex. In most cases they
1166 return vport_get_ifindex(vport);
1170 * vport_get_mtu - retrieve device MTU (for kernel callers)
1172 * @vport: vport from which to retrieve MTU
1174 * Retrieves the MTU of the given device. Either RTNL lock or rcu_read_lock
1177 int vport_get_mtu(const struct vport *vport)
1179 return vport->ops->get_mtu(vport);
1183 * vport_receive - pass up received packet to the datapath for processing
1185 * @vport: vport that received the packet
1186 * @skb: skb that was received
1188 * Must be called with rcu_read_lock. The packet cannot be shared and
1189 * skb->data should point to the Ethernet header. The caller must have already
1190 * called compute_ip_summed() to initialize the checksumming fields.
1192 void vport_receive(struct vport *vport, struct sk_buff *skb)
1194 struct dp_port *dp_port = vport_get_dp_port(vport);
1197 vport_record_error(vport, VPORT_E_RX_DROPPED);
1203 if (vport->ops->flags & VPORT_F_GEN_STATS) {
1204 struct vport_percpu_stats *stats;
1207 stats = per_cpu_ptr(vport->percpu_stats, smp_processor_id());
1209 write_seqcount_begin(&stats->seqlock);
1210 stats->rx_packets++;
1211 stats->rx_bytes += skb->len;
1212 write_seqcount_end(&stats->seqlock);
1217 if (!(vport->ops->flags & VPORT_F_TUN_ID))
1218 OVS_CB(skb)->tun_id = 0;
1220 dp_process_received_packet(dp_port, skb);
1223 static inline unsigned packet_length(const struct sk_buff *skb)
1225 unsigned length = skb->len - ETH_HLEN;
1227 if (skb->protocol == htons(ETH_P_8021Q))
1228 length -= VLAN_HLEN;
1234 * vport_send - send a packet on a device
1236 * @vport: vport on which to send the packet
1239 * Sends the given packet and returns the length of data sent. Either RTNL
1240 * lock or rcu_read_lock must be held.
1242 int vport_send(struct vport *vport, struct sk_buff *skb)
1247 mtu = vport_get_mtu(vport);
1248 if (unlikely(packet_length(skb) > mtu && !skb_is_gso(skb))) {
1249 if (net_ratelimit())
1250 printk(KERN_WARNING "%s: dropped over-mtu packet: %d > %d\n",
1251 dp_name(vport_get_dp_port(vport)->dp), packet_length(skb), mtu);
1255 sent = vport->ops->send(vport, skb);
1257 if (vport->ops->flags & VPORT_F_GEN_STATS && sent > 0) {
1258 struct vport_percpu_stats *stats;
1261 stats = per_cpu_ptr(vport->percpu_stats, smp_processor_id());
1263 write_seqcount_begin(&stats->seqlock);
1264 stats->tx_packets++;
1265 stats->tx_bytes += sent;
1266 write_seqcount_end(&stats->seqlock);
1275 vport_record_error(vport, VPORT_E_TX_DROPPED);
1280 * vport_record_error - indicate device error to generic stats layer
1282 * @vport: vport that encountered the error
1283 * @err_type: one of enum vport_err_type types to indicate the error type
1285 * If using the vport generic stats layer indicate that an error of the given
1288 void vport_record_error(struct vport *vport, enum vport_err_type err_type)
1290 if (vport->ops->flags & VPORT_F_GEN_STATS) {
1292 spin_lock_bh(&vport->stats_lock);
1295 case VPORT_E_RX_DROPPED:
1296 vport->err_stats.rx_dropped++;
1299 case VPORT_E_RX_ERROR:
1300 vport->err_stats.rx_errors++;
1303 case VPORT_E_RX_FRAME:
1304 vport->err_stats.rx_frame_err++;
1307 case VPORT_E_RX_OVER:
1308 vport->err_stats.rx_over_err++;
1311 case VPORT_E_RX_CRC:
1312 vport->err_stats.rx_crc_err++;
1315 case VPORT_E_TX_DROPPED:
1316 vport->err_stats.tx_dropped++;
1319 case VPORT_E_TX_ERROR:
1320 vport->err_stats.tx_errors++;
1323 case VPORT_E_COLLISION:
1324 vport->err_stats.collisions++;
1328 spin_unlock_bh(&vport->stats_lock);