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 const 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 const 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)
182 struct vport_parms parms;
186 vport_config->port_type[VPORT_TYPE_SIZE - 1] = '\0';
187 vport_config->devname[IFNAMSIZ - 1] = '\0';
191 vport = vport_locate(vport_config->devname);
197 parms.name = vport_config->devname;
198 parms.type = vport_config->port_type;
199 parms.config = vport_config->config;
202 vport = vport_add(&parms);
206 err = PTR_ERR(vport);
214 * vport_user_add - add vport device (for userspace callers)
216 * @uvport_config: New port configuration.
218 * Creates a new vport with the specified configuration (which is dependent
219 * on device type). This function is for userspace callers and assumes no
222 int vport_user_add(const struct odp_vport_add __user *uvport_config)
224 struct odp_vport_add vport_config;
226 if (copy_from_user(&vport_config, uvport_config, sizeof(struct odp_vport_add)))
229 return do_vport_add(&vport_config);
233 int compat_vport_user_add(struct compat_odp_vport_add *ucompat)
235 struct compat_odp_vport_add compat;
236 struct odp_vport_add vport_config;
238 if (copy_from_user(&compat, ucompat, sizeof(struct compat_odp_vport_add)))
241 memcpy(vport_config.port_type, compat.port_type, VPORT_TYPE_SIZE);
242 memcpy(vport_config.devname, compat.devname, IFNAMSIZ);
243 vport_config.config = compat_ptr(compat.config);
245 return do_vport_add(&vport_config);
249 static int do_vport_mod(struct odp_vport_mod *vport_config)
254 vport_config->devname[IFNAMSIZ - 1] = '\0';
258 vport = vport_locate(vport_config->devname);
265 err = vport_mod(vport, vport_config->config);
274 * vport_user_mod - modify existing vport device (for userspace callers)
276 * @uvport_config: New configuration for vport
278 * Modifies an existing device with the specified configuration (which is
279 * dependent on device type). This function is for userspace callers and
280 * assumes no locks are held.
282 int vport_user_mod(const struct odp_vport_mod __user *uvport_config)
284 struct odp_vport_mod vport_config;
286 if (copy_from_user(&vport_config, uvport_config, sizeof(struct odp_vport_mod)))
289 return do_vport_mod(&vport_config);
293 int compat_vport_user_mod(struct compat_odp_vport_mod *ucompat)
295 struct compat_odp_vport_mod compat;
296 struct odp_vport_mod vport_config;
298 if (copy_from_user(&compat, ucompat, sizeof(struct compat_odp_vport_mod)))
301 memcpy(vport_config.devname, compat.devname, IFNAMSIZ);
302 vport_config.config = compat_ptr(compat.config);
304 return do_vport_mod(&vport_config);
309 * vport_user_del - delete existing vport device (for userspace callers)
311 * @udevname: Name of device to delete
313 * Deletes the specified device. Detaches the device from a datapath first
314 * if it is attached. Deleting the device will fail if it does not exist or it
315 * is the datapath local port. It is also possible to fail for less obvious
316 * reasons, such as lack of memory. This function is for userspace callers and
317 * assumes no locks are held.
319 int 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.
378 int vport_user_stats_get(struct odp_vport_stats_req __user *ustats_req)
380 struct odp_vport_stats_req stats_req;
384 if (copy_from_user(&stats_req, ustats_req, sizeof(struct odp_vport_stats_req)))
387 stats_req.devname[IFNAMSIZ - 1] = '\0';
391 vport = vport_locate(stats_req.devname);
397 err = vport_get_stats(vport, &stats_req.stats);
403 if (copy_to_user(ustats_req, &stats_req, sizeof(struct odp_vport_stats_req)))
410 * vport_user_stats_set - sets offset device stats (for userspace callers)
412 * @ustats_req: Stats set parameters.
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. This function is for userspace callers and assumes no locks
420 int vport_user_stats_set(struct odp_vport_stats_req __user *ustats_req)
422 struct odp_vport_stats_req stats_req;
426 if (copy_from_user(&stats_req, ustats_req, sizeof(struct odp_vport_stats_req)))
429 stats_req.devname[IFNAMSIZ - 1] = '\0';
434 vport = vport_locate(stats_req.devname);
440 err = vport_set_stats(vport, &stats_req.stats);
450 * vport_user_ether_get - retrieve device Ethernet address (for userspace callers)
452 * @uvport_ether: Ethernet address request parameters.
454 * Retrieves the Ethernet address of the given device. This function is for
455 * userspace callers and assumes no locks are held.
457 int vport_user_ether_get(struct odp_vport_ether __user *uvport_ether)
459 struct odp_vport_ether vport_ether;
463 if (copy_from_user(&vport_ether, uvport_ether, sizeof(struct odp_vport_ether)))
466 vport_ether.devname[IFNAMSIZ - 1] = '\0';
470 vport = vport_locate(vport_ether.devname);
477 memcpy(vport_ether.ether_addr, vport_get_addr(vport), ETH_ALEN);
484 if (copy_to_user(uvport_ether, &vport_ether, sizeof(struct odp_vport_ether)))
491 * vport_user_ether_set - set device Ethernet address (for userspace callers)
493 * @uvport_ether: Ethernet address request parameters.
495 * Sets the Ethernet address of the given device. Some devices may not support
496 * setting the Ethernet address, in which case the result will always be
497 * -EOPNOTSUPP. This function is for userspace callers and assumes no locks
500 int vport_user_ether_set(struct odp_vport_ether __user *uvport_ether)
502 struct odp_vport_ether vport_ether;
506 if (copy_from_user(&vport_ether, uvport_ether, sizeof(struct odp_vport_ether)))
509 vport_ether.devname[IFNAMSIZ - 1] = '\0';
514 vport = vport_locate(vport_ether.devname);
520 err = vport_set_addr(vport, vport_ether.ether_addr);
529 * vport_user_mtu_get - retrieve device MTU (for userspace callers)
531 * @uvport_mtu: MTU request parameters.
533 * Retrieves the MTU of the given device. This function is for userspace
534 * callers and assumes no locks are held.
536 int vport_user_mtu_get(struct odp_vport_mtu __user *uvport_mtu)
538 struct odp_vport_mtu vport_mtu;
542 if (copy_from_user(&vport_mtu, uvport_mtu, sizeof(struct odp_vport_mtu)))
545 vport_mtu.devname[IFNAMSIZ - 1] = '\0';
549 vport = vport_locate(vport_mtu.devname);
555 vport_mtu.mtu = vport_get_mtu(vport);
561 if (copy_to_user(uvport_mtu, &vport_mtu, sizeof(struct odp_vport_mtu)))
568 * vport_user_mtu_set - set device MTU (for userspace callers)
570 * @uvport_mtu: MTU request parameters.
572 * Sets the MTU of the given device. Some devices may not support setting the
573 * MTU, in which case the result will always be -EOPNOTSUPP. This function is
574 * for userspace callers and assumes no locks are held.
576 int vport_user_mtu_set(struct odp_vport_mtu __user *uvport_mtu)
578 struct odp_vport_mtu vport_mtu;
582 if (copy_from_user(&vport_mtu, uvport_mtu, sizeof(struct odp_vport_mtu)))
585 vport_mtu.devname[IFNAMSIZ - 1] = '\0';
590 vport = vport_locate(vport_mtu.devname);
596 err = vport_set_mtu(vport, vport_mtu.mtu);
604 static struct hlist_head *hash_bucket(const char *name)
606 unsigned int hash = full_name_hash(name, strlen(name));
607 return &dev_table[hash & (VPORT_HASH_BUCKETS - 1)];
611 * vport_locate - find a port that has already been created
613 * @name: name of port to find
615 * Either RTNL or vport lock must be acquired before calling this function
616 * and held while using the found port. See the locking comments at the
619 struct vport *vport_locate(const char *name)
621 struct hlist_head *bucket = hash_bucket(name);
623 struct hlist_node *node;
625 if (unlikely(!mutex_is_locked(&vport_mutex) && !rtnl_is_locked())) {
626 pr_err("neither RTNL nor vport lock held in vport_locate\n");
632 hlist_for_each_entry(vport, node, bucket, hash_node)
633 if (!strcmp(name, vport_get_name(vport)))
643 static void register_vport(struct vport *vport)
645 hlist_add_head(&vport->hash_node, hash_bucket(vport_get_name(vport)));
648 static void unregister_vport(struct vport *vport)
650 hlist_del(&vport->hash_node);
654 * vport_alloc - allocate and initialize new vport
656 * @priv_size: Size of private data area to allocate.
657 * @ops: vport device ops
659 * Allocate and initialize a new vport defined by @ops. The vport will contain
660 * a private data area of size @priv_size that can be accessed using
661 * vport_priv(). vports that are no longer needed should be released with
664 struct vport *vport_alloc(int priv_size, const struct vport_ops *ops)
669 alloc_size = sizeof(struct vport);
671 alloc_size = ALIGN(alloc_size, VPORT_ALIGN);
672 alloc_size += priv_size;
675 vport = kzalloc(alloc_size, GFP_KERNEL);
677 return ERR_PTR(-ENOMEM);
681 if (vport->ops->flags & VPORT_F_GEN_STATS) {
682 vport->percpu_stats = alloc_percpu(struct vport_percpu_stats);
683 if (!vport->percpu_stats)
684 return ERR_PTR(-ENOMEM);
686 spin_lock_init(&vport->stats_lock);
693 * vport_free - uninitialize and free vport
695 * @vport: vport to free
697 * Frees a vport allocated with vport_alloc() when it is no longer needed.
699 void vport_free(struct vport *vport)
701 if (vport->ops->flags & VPORT_F_GEN_STATS)
702 free_percpu(vport->percpu_stats);
708 * vport_add - add vport device (for kernel callers)
710 * @parms: Information about new vport.
712 * Creates a new vport with the specified configuration (which is dependent
713 * on device type). Both RTNL and vport locks must be held.
715 struct vport *vport_add(const struct vport_parms *parms)
724 for (i = 0; i < n_vport_types; i++) {
725 if (!strcmp(vport_ops_list[i]->type, parms->type)) {
726 vport = vport_ops_list[i]->create(parms);
728 err = PTR_ERR(vport);
732 register_vport(vport);
744 * vport_mod - modify existing vport device (for kernel callers)
746 * @vport: vport to modify.
747 * @config: Device type specific configuration. Userspace pointer.
749 * Modifies an existing device with the specified configuration (which is
750 * dependent on device type). Both RTNL and vport locks must be held.
752 int vport_mod(struct vport *vport, const void __user *config)
757 if (vport->ops->modify)
758 return vport->ops->modify(vport, config);
764 * vport_del - delete existing vport device (for kernel callers)
766 * @vport: vport to delete.
768 * Deletes the specified device. The device must not be currently attached to
769 * a datapath. It is possible to fail for reasons such as lack of memory.
770 * Both RTNL and vport locks must be held.
772 int vport_del(struct vport *vport)
776 BUG_ON(vport_get_dp_port(vport));
778 unregister_vport(vport);
780 return vport->ops->destroy(vport);
784 * vport_attach - attach a vport to a datapath
786 * @vport: vport to attach.
787 * @dp_port: Datapath port to attach the vport to.
789 * Attaches a vport to a specific datapath so that packets may be exchanged.
790 * Both ports must be currently unattached. @dp_port must be successfully
791 * attached to a vport before it is connected to a datapath and must not be
792 * modified while connected. RTNL lock and the appropriate DP mutex must be held.
794 int vport_attach(struct vport *vport, struct dp_port *dp_port)
798 if (vport_get_dp_port(vport))
801 if (vport->ops->attach) {
804 err = vport->ops->attach(vport);
809 rcu_assign_pointer(vport->dp_port, dp_port);
815 * vport_detach - detach a vport from a datapath
817 * @vport: vport to detach.
819 * Detaches a vport from a datapath. May fail for a variety of reasons,
820 * including lack of memory. RTNL lock and the appropriate DP mutex must be held.
822 int vport_detach(struct vport *vport)
824 struct dp_port *dp_port;
828 dp_port = vport_get_dp_port(vport);
832 rcu_assign_pointer(vport->dp_port, NULL);
834 if (vport->ops->detach)
835 return vport->ops->detach(vport);
841 * vport_set_mtu - set device MTU (for kernel callers)
843 * @vport: vport on which to set MTU.
846 * Sets the MTU of the given device. Some devices may not support setting the
847 * MTU, in which case the result will always be -EOPNOTSUPP. RTNL lock must
850 int vport_set_mtu(struct vport *vport, int mtu)
857 if (vport->ops->set_mtu) {
860 ret = vport->ops->set_mtu(vport, mtu);
862 if (!ret && !is_internal_vport(vport)) {
863 struct dp_port *dp_port = vport_get_dp_port(vport);
866 set_internal_devs_mtu(dp_port->dp);
875 * vport_set_addr - set device Ethernet address (for kernel callers)
877 * @vport: vport on which to set Ethernet address.
878 * @addr: New address.
880 * Sets the Ethernet address of the given device. Some devices may not support
881 * setting the Ethernet address, in which case the result will always be
882 * -EOPNOTSUPP. RTNL lock must be held.
884 int vport_set_addr(struct vport *vport, const unsigned char *addr)
888 if (!is_valid_ether_addr(addr))
889 return -EADDRNOTAVAIL;
891 if (vport->ops->set_addr)
892 return vport->ops->set_addr(vport, addr);
898 * vport_set_stats - sets offset device stats (for kernel callers)
900 * @vport: vport on which to set stats
901 * @stats: stats to set
903 * Provides a set of transmit, receive, and error stats to be added as an
904 * offset to the collect data when stats are retreived. Some devices may not
905 * support setting the stats, in which case the result will always be
906 * -EOPNOTSUPP. RTNL lock must be held.
908 int vport_set_stats(struct vport *vport, struct rtnl_link_stats64 *stats)
912 if (vport->ops->flags & VPORT_F_GEN_STATS) {
913 spin_lock_bh(&vport->stats_lock);
914 vport->offset_stats = *stats;
915 spin_unlock_bh(&vport->stats_lock);
918 } else if (vport->ops->set_stats)
919 return vport->ops->set_stats(vport, stats);
925 * vport_get_name - retrieve device name
927 * @vport: vport from which to retrieve the name.
929 * Retrieves the name of the given device. Either RTNL lock or rcu_read_lock
930 * must be held for the entire duration that the name is in use.
932 const char *vport_get_name(const struct vport *vport)
934 return vport->ops->get_name(vport);
938 * vport_get_type - retrieve device type
940 * @vport: vport from which to retrieve the type.
942 * Retrieves the type of the given device. Either RTNL lock or rcu_read_lock
943 * must be held for the entire duration that the type is in use.
945 const char *vport_get_type(const struct vport *vport)
947 return vport->ops->type;
951 * vport_get_addr - retrieve device Ethernet address (for kernel callers)
953 * @vport: vport from which to retrieve the Ethernet address.
955 * Retrieves the Ethernet address of the given device. Either RTNL lock or
956 * rcu_read_lock must be held for the entire duration that the Ethernet address
959 const unsigned char *vport_get_addr(const struct vport *vport)
961 return vport->ops->get_addr(vport);
965 * vport_get_dp_port - retrieve attached datapath port
967 * @vport: vport from which to retrieve the datapath port.
969 * Retrieves the attached datapath port or null if not attached. Either RTNL
970 * lock or rcu_read_lock must be held for the entire duration that the datapath
971 * port is being accessed.
973 struct dp_port *vport_get_dp_port(const struct vport *vport)
975 return rcu_dereference(vport->dp_port);
979 * vport_get_kobj - retrieve associated kobj
981 * @vport: vport from which to retrieve the associated kobj
983 * Retrieves the associated kobj or null if no kobj. The returned kobj is
984 * valid for as long as the vport exists.
986 struct kobject *vport_get_kobj(const struct vport *vport)
988 if (vport->ops->get_kobj)
989 return vport->ops->get_kobj(vport);
995 * vport_get_stats - retrieve device stats (for kernel callers)
997 * @vport: vport from which to retrieve the stats
998 * @stats: location to store stats
1000 * Retrieves transmit, receive, and error stats for the given device.
1002 int vport_get_stats(struct vport *vport, struct rtnl_link_stats64 *stats)
1004 struct rtnl_link_stats64 dev_stats;
1005 struct rtnl_link_stats64 *dev_statsp = NULL;
1008 if (vport->ops->get_stats) {
1009 if (vport->ops->flags & VPORT_F_GEN_STATS)
1010 dev_statsp = &dev_stats;
1015 err = vport->ops->get_stats(vport, dev_statsp);
1022 if (vport->ops->flags & VPORT_F_GEN_STATS) {
1025 /* We potentially have 3 sources of stats that need to be
1026 * combined: those we have collected (split into err_stats and
1027 * percpu_stats), offset_stats from set_stats(), and device
1028 * error stats from get_stats() (for errors that happen
1029 * downstream and therefore aren't reported through our
1030 * vport_record_error() function). */
1032 spin_lock_bh(&vport->stats_lock);
1034 *stats = vport->offset_stats;
1036 stats->rx_errors += vport->err_stats.rx_errors;
1037 stats->tx_errors += vport->err_stats.tx_errors;
1038 stats->tx_dropped += vport->err_stats.tx_dropped;
1039 stats->rx_dropped += vport->err_stats.rx_dropped;
1041 spin_unlock_bh(&vport->stats_lock);
1044 stats->rx_packets += dev_statsp->rx_packets;
1045 stats->tx_packets += dev_statsp->tx_packets;
1046 stats->rx_bytes += dev_statsp->rx_bytes;
1047 stats->tx_bytes += dev_statsp->tx_bytes;
1048 stats->rx_errors += dev_statsp->rx_errors;
1049 stats->tx_errors += dev_statsp->tx_errors;
1050 stats->rx_dropped += dev_statsp->rx_dropped;
1051 stats->tx_dropped += dev_statsp->tx_dropped;
1052 stats->multicast += dev_statsp->multicast;
1053 stats->collisions += dev_statsp->collisions;
1054 stats->rx_length_errors += dev_statsp->rx_length_errors;
1055 stats->rx_over_errors += dev_statsp->rx_over_errors;
1056 stats->rx_crc_errors += dev_statsp->rx_crc_errors;
1057 stats->rx_frame_errors += dev_statsp->rx_frame_errors;
1058 stats->rx_fifo_errors += dev_statsp->rx_fifo_errors;
1059 stats->rx_missed_errors += dev_statsp->rx_missed_errors;
1060 stats->tx_aborted_errors += dev_statsp->tx_aborted_errors;
1061 stats->tx_carrier_errors += dev_statsp->tx_carrier_errors;
1062 stats->tx_fifo_errors += dev_statsp->tx_fifo_errors;
1063 stats->tx_heartbeat_errors += dev_statsp->tx_heartbeat_errors;
1064 stats->tx_window_errors += dev_statsp->tx_window_errors;
1065 stats->rx_compressed += dev_statsp->rx_compressed;
1066 stats->tx_compressed += dev_statsp->tx_compressed;
1069 for_each_possible_cpu(i) {
1070 const struct vport_percpu_stats *percpu_stats;
1071 struct vport_percpu_stats local_stats;
1074 percpu_stats = per_cpu_ptr(vport->percpu_stats, i);
1077 seqcount = read_seqcount_begin(&percpu_stats->seqlock);
1078 local_stats = *percpu_stats;
1079 } while (read_seqcount_retry(&percpu_stats->seqlock, seqcount));
1081 stats->rx_bytes += local_stats.rx_bytes;
1082 stats->rx_packets += local_stats.rx_packets;
1083 stats->tx_bytes += local_stats.tx_bytes;
1084 stats->tx_packets += local_stats.tx_packets;
1096 * vport_get_flags - retrieve device flags
1098 * @vport: vport from which to retrieve the flags
1100 * Retrieves the flags of the given device. Either RTNL lock or rcu_read_lock
1103 unsigned vport_get_flags(const struct vport *vport)
1105 return vport->ops->get_dev_flags(vport);
1109 * vport_get_flags - check whether device is running
1111 * @vport: vport on which to check status.
1113 * Checks whether the given device is running. Either RTNL lock or
1114 * rcu_read_lock must be held.
1116 int vport_is_running(const struct vport *vport)
1118 return vport->ops->is_running(vport);
1122 * vport_get_flags - retrieve device operating state
1124 * @vport: vport from which to check status
1126 * Retrieves the RFC2863 operstate of the given device. Either RTNL lock or
1127 * rcu_read_lock must be held.
1129 unsigned char vport_get_operstate(const struct vport *vport)
1131 return vport->ops->get_operstate(vport);
1135 * vport_get_ifindex - retrieve device system interface index
1137 * @vport: vport from which to retrieve index
1139 * Retrieves the system interface index of the given device. Not all devices
1140 * will have system indexes, in which case the index of the datapath local
1141 * port is returned. Returns a negative index on error. Either RTNL lock or
1142 * rcu_read_lock must be held.
1144 int vport_get_ifindex(const struct vport *vport)
1146 const struct dp_port *dp_port;
1148 if (vport->ops->get_ifindex)
1149 return vport->ops->get_ifindex(vport);
1151 /* If we don't actually have an ifindex, use the local port's.
1152 * Userspace doesn't check it anyways. */
1153 dp_port = vport_get_dp_port(vport);
1157 return vport_get_ifindex(dp_port->dp->ports[ODPP_LOCAL]->vport);
1161 * vport_get_iflink - retrieve device system link index
1163 * @vport: vport from which to retrieve index
1165 * Retrieves the system link index of the given device. The link is the index
1166 * of the interface on which the packet will actually be sent. In most cases
1167 * this is the same as the ifindex but may be different for tunnel devices.
1168 * Returns a negative index on error. Either RTNL lock or rcu_read_lock must
1171 int vport_get_iflink(const struct vport *vport)
1173 if (vport->ops->get_iflink)
1174 return vport->ops->get_iflink(vport);
1176 /* If we don't have an iflink, use the ifindex. In most cases they
1178 return vport_get_ifindex(vport);
1182 * vport_get_mtu - retrieve device MTU (for kernel callers)
1184 * @vport: vport from which to retrieve MTU
1186 * Retrieves the MTU of the given device. Either RTNL lock or rcu_read_lock
1189 int vport_get_mtu(const struct vport *vport)
1191 return vport->ops->get_mtu(vport);
1195 * vport_receive - pass up received packet to the datapath for processing
1197 * @vport: vport that received the packet
1198 * @skb: skb that was received
1200 * Must be called with rcu_read_lock. The packet cannot be shared and
1201 * skb->data should point to the Ethernet header. The caller must have already
1202 * called compute_ip_summed() to initialize the checksumming fields.
1204 void vport_receive(struct vport *vport, struct sk_buff *skb)
1206 struct dp_port *dp_port = vport_get_dp_port(vport);
1209 vport_record_error(vport, VPORT_E_RX_DROPPED);
1215 if (vport->ops->flags & VPORT_F_GEN_STATS) {
1216 struct vport_percpu_stats *stats;
1219 stats = per_cpu_ptr(vport->percpu_stats, smp_processor_id());
1221 write_seqcount_begin(&stats->seqlock);
1222 stats->rx_packets++;
1223 stats->rx_bytes += skb->len;
1224 write_seqcount_end(&stats->seqlock);
1229 if (!(vport->ops->flags & VPORT_F_FLOW))
1230 OVS_CB(skb)->flow = NULL;
1232 if (!(vport->ops->flags & VPORT_F_TUN_ID))
1233 OVS_CB(skb)->tun_id = 0;
1235 dp_process_received_packet(dp_port, skb);
1238 static inline unsigned packet_length(const struct sk_buff *skb)
1240 unsigned length = skb->len - ETH_HLEN;
1242 if (skb->protocol == htons(ETH_P_8021Q))
1243 length -= VLAN_HLEN;
1249 * vport_send - send a packet on a device
1251 * @vport: vport on which to send the packet
1254 * Sends the given packet and returns the length of data sent. Either RTNL
1255 * lock or rcu_read_lock must be held.
1257 int vport_send(struct vport *vport, struct sk_buff *skb)
1262 mtu = vport_get_mtu(vport);
1263 if (unlikely(packet_length(skb) > mtu && !skb_is_gso(skb))) {
1264 if (net_ratelimit())
1265 pr_warn("%s: dropped over-mtu packet: %d > %d\n",
1266 dp_name(vport_get_dp_port(vport)->dp),
1267 packet_length(skb), mtu);
1271 sent = vport->ops->send(vport, skb);
1273 if (vport->ops->flags & VPORT_F_GEN_STATS && sent > 0) {
1274 struct vport_percpu_stats *stats;
1277 stats = per_cpu_ptr(vport->percpu_stats, smp_processor_id());
1279 write_seqcount_begin(&stats->seqlock);
1280 stats->tx_packets++;
1281 stats->tx_bytes += sent;
1282 write_seqcount_end(&stats->seqlock);
1291 vport_record_error(vport, VPORT_E_TX_DROPPED);
1296 * vport_record_error - indicate device error to generic stats layer
1298 * @vport: vport that encountered the error
1299 * @err_type: one of enum vport_err_type types to indicate the error type
1301 * If using the vport generic stats layer indicate that an error of the given
1304 void vport_record_error(struct vport *vport, enum vport_err_type err_type)
1306 if (vport->ops->flags & VPORT_F_GEN_STATS) {
1308 spin_lock_bh(&vport->stats_lock);
1311 case VPORT_E_RX_DROPPED:
1312 vport->err_stats.rx_dropped++;
1315 case VPORT_E_RX_ERROR:
1316 vport->err_stats.rx_errors++;
1319 case VPORT_E_TX_DROPPED:
1320 vport->err_stats.tx_dropped++;
1323 case VPORT_E_TX_ERROR:
1324 vport->err_stats.tx_errors++;
1328 spin_unlock_bh(&vport->stats_lock);