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>
21 #include "vport-internal_dev.h"
23 /* List of statically compiled vport implementations. Don't forget to also
24 * add yours to the list at the bottom of vport.h. */
25 static struct vport_ops *base_vport_ops_list[] = {
32 static const struct vport_ops **vport_ops_list;
33 static int n_vport_types;
35 static struct hlist_head *dev_table;
36 #define VPORT_HASH_BUCKETS 1024
38 /* Both RTNL lock and vport_mutex need to be held when updating dev_table.
40 * If you use vport_locate and then perform some operations, you need to hold
41 * one of these locks if you don't want the vport to be deleted out from under
44 * If you get a reference to a vport through a dp_port, it is protected
45 * by RCU and you need to hold rcu_read_lock instead when reading.
47 * If multiple locks are taken, the hierarchy is:
52 static DEFINE_MUTEX(vport_mutex);
55 * vport_lock - acquire vport lock
57 * Acquire global vport lock. See above comment about locking requirements
58 * and specific function definitions. May sleep.
62 mutex_lock(&vport_mutex);
66 * vport_unlock - release vport lock
68 * Release lock acquired with vport_lock.
70 void vport_unlock(void)
72 mutex_unlock(&vport_mutex);
75 #define ASSERT_VPORT() do { \
76 if (unlikely(!mutex_is_locked(&vport_mutex))) { \
77 printk(KERN_ERR "openvswitch: vport lock not held at %s (%d)\n", \
78 __FILE__, __LINE__); \
84 * vport_init - initialize vport subsystem
86 * Called at module load time to initialize the vport subsystem and any
87 * compiled in vport types.
94 dev_table = kzalloc(VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
101 vport_ops_list = kmalloc(ARRAY_SIZE(base_vport_ops_list) *
102 sizeof(struct vport_ops *), GFP_KERNEL);
103 if (!vport_ops_list) {
105 goto error_dev_table;
108 for (i = 0; i < ARRAY_SIZE(base_vport_ops_list); i++) {
109 struct vport_ops *new_ops = base_vport_ops_list[i];
112 err = new_ops->init();
117 vport_ops_list[n_vport_types++] = new_ops;
118 else if (new_ops->flags & VPORT_F_REQUIRED) {
132 static void vport_del_all(void)
139 for (i = 0; i < VPORT_HASH_BUCKETS; i++) {
140 struct hlist_head *bucket = &dev_table[i];
142 struct hlist_node *node, *next;
144 hlist_for_each_entry_safe(vport, node, next, bucket, hash_node)
153 * vport_exit - shutdown vport subsystem
155 * Called at module exit time to shutdown the vport subsystem and any
156 * initialized vport types.
158 void vport_exit(void)
164 for (i = 0; i < n_vport_types; i++) {
165 if (vport_ops_list[i]->exit)
166 vport_ops_list[i]->exit();
169 kfree(vport_ops_list);
173 static int do_vport_add(struct odp_vport_add *vport_config)
178 vport_config->port_type[VPORT_TYPE_SIZE - 1] = '\0';
179 vport_config->devname[IFNAMSIZ - 1] = '\0';
183 vport = vport_locate(vport_config->devname);
190 vport = vport_add(vport_config->devname, vport_config->port_type,
191 vport_config->config);
195 err = PTR_ERR(vport);
203 * vport_user_add - add vport device (for userspace callers)
205 * @uvport_config: New port configuration.
207 * Creates a new vport with the specified configuration (which is dependent
208 * on device type). This function is for userspace callers and assumes no
211 int vport_user_add(const struct odp_vport_add __user *uvport_config)
213 struct odp_vport_add vport_config;
215 if (copy_from_user(&vport_config, uvport_config, sizeof(struct odp_vport_add)))
218 return do_vport_add(&vport_config);
222 int compat_vport_user_add(struct compat_odp_vport_add *ucompat)
224 struct compat_odp_vport_add compat;
225 struct odp_vport_add vport_config;
227 if (copy_from_user(&compat, ucompat, sizeof(struct compat_odp_vport_add)))
230 memcpy(vport_config.port_type, compat.port_type, VPORT_TYPE_SIZE);
231 memcpy(vport_config.devname, compat.devname, IFNAMSIZ);
232 vport_config.config = compat_ptr(compat.config);
234 return do_vport_add(&vport_config);
238 static int do_vport_mod(struct odp_vport_mod *vport_config)
243 vport_config->devname[IFNAMSIZ - 1] = '\0';
247 vport = vport_locate(vport_config->devname);
254 err = vport_mod(vport, vport_config->config);
263 * vport_user_mod - modify existing vport device (for userspace callers)
265 * @uvport_config: New configuration for vport
267 * Modifies an existing device with the specified configuration (which is
268 * dependent on device type). This function is for userspace callers and
269 * assumes no locks are held.
271 int vport_user_mod(const struct odp_vport_mod __user *uvport_config)
273 struct odp_vport_mod vport_config;
275 if (copy_from_user(&vport_config, uvport_config, sizeof(struct odp_vport_mod)))
278 return do_vport_mod(&vport_config);
282 int compat_vport_user_mod(struct compat_odp_vport_mod *ucompat)
284 struct compat_odp_vport_mod compat;
285 struct odp_vport_mod vport_config;
287 if (copy_from_user(&compat, ucompat, sizeof(struct compat_odp_vport_mod)))
290 memcpy(vport_config.devname, compat.devname, IFNAMSIZ);
291 vport_config.config = compat_ptr(compat.config);
293 return do_vport_mod(&vport_config);
298 * vport_user_del - delete existing vport device (for userspace callers)
300 * @udevname: Name of device to delete
302 * Deletes the specified device. Detaches the device from a datapath first
303 * if it is attached. Deleting the device will fail if it does not exist or it
304 * is the datapath local port. It is also possible to fail for less obvious
305 * reasons, such as lack of memory. This function is for userspace callers and
306 * assumes no locks are held.
308 int vport_user_del(const char __user *udevname)
310 char devname[IFNAMSIZ];
312 struct dp_port *dp_port;
316 retval = strncpy_from_user(devname, udevname, IFNAMSIZ);
319 else if (retval >= IFNAMSIZ)
320 return -ENAMETOOLONG;
324 vport = vport_locate(devname);
330 dp_port = vport_get_dp_port(vport);
332 struct datapath *dp = dp_port->dp;
334 mutex_lock(&dp->mutex);
336 if (!strcmp(dp_name(dp), devname)) {
341 err = dp_detach_port(dp_port, 0);
344 mutex_unlock(&dp->mutex);
351 err = vport_del(vport);
360 * vport_user_stats_get - retrieve device stats (for userspace callers)
362 * @ustats_req: Stats request parameters.
364 * Retrieves transmit, receive, and error stats for the given device. This
365 * function is for userspace callers and assumes no locks are held.
367 int vport_user_stats_get(struct odp_vport_stats_req __user *ustats_req)
369 struct odp_vport_stats_req stats_req;
373 if (copy_from_user(&stats_req, ustats_req, sizeof(struct odp_vport_stats_req)))
376 stats_req.devname[IFNAMSIZ - 1] = '\0';
380 vport = vport_locate(stats_req.devname);
386 err = vport_get_stats(vport, &stats_req.stats);
392 if (copy_to_user(ustats_req, &stats_req, sizeof(struct odp_vport_stats_req)))
399 * vport_user_stats_set - sets offset device stats (for userspace callers)
401 * @ustats_req: Stats set parameters.
403 * Provides a set of transmit, receive, and error stats to be added as an
404 * offset to the collect data when stats are retreived. Some devices may not
405 * support setting the stats, in which case the result will always be
406 * -EOPNOTSUPP. This function is for userspace callers and assumes no locks
409 int vport_user_stats_set(struct odp_vport_stats_req __user *ustats_req)
411 struct odp_vport_stats_req stats_req;
415 if (copy_from_user(&stats_req, ustats_req, sizeof(struct odp_vport_stats_req)))
418 stats_req.devname[IFNAMSIZ - 1] = '\0';
423 vport = vport_locate(stats_req.devname);
429 err = vport_set_stats(vport, &stats_req.stats);
439 * vport_user_ether_get - retrieve device Ethernet address (for userspace callers)
441 * @uvport_ether: Ethernet address request parameters.
443 * Retrieves the Ethernet address of the given device. This function is for
444 * userspace callers and assumes no locks are held.
446 int vport_user_ether_get(struct odp_vport_ether __user *uvport_ether)
448 struct odp_vport_ether vport_ether;
452 if (copy_from_user(&vport_ether, uvport_ether, sizeof(struct odp_vport_ether)))
455 vport_ether.devname[IFNAMSIZ - 1] = '\0';
459 vport = vport_locate(vport_ether.devname);
466 memcpy(vport_ether.ether_addr, vport_get_addr(vport), ETH_ALEN);
473 if (copy_to_user(uvport_ether, &vport_ether, sizeof(struct odp_vport_ether)))
480 * vport_user_ether_set - set device Ethernet address (for userspace callers)
482 * @uvport_ether: Ethernet address request parameters.
484 * Sets the Ethernet address of the given device. Some devices may not support
485 * setting the Ethernet address, in which case the result will always be
486 * -EOPNOTSUPP. This function is for userspace callers and assumes no locks
489 int vport_user_ether_set(struct odp_vport_ether __user *uvport_ether)
491 struct odp_vport_ether vport_ether;
495 if (copy_from_user(&vport_ether, uvport_ether, sizeof(struct odp_vport_ether)))
498 vport_ether.devname[IFNAMSIZ - 1] = '\0';
503 vport = vport_locate(vport_ether.devname);
509 err = vport_set_addr(vport, vport_ether.ether_addr);
518 * vport_user_mtu_get - retrieve device MTU (for userspace callers)
520 * @uvport_mtu: MTU request parameters.
522 * Retrieves the MTU of the given device. This function is for userspace
523 * callers and assumes no locks are held.
525 int vport_user_mtu_get(struct odp_vport_mtu __user *uvport_mtu)
527 struct odp_vport_mtu vport_mtu;
531 if (copy_from_user(&vport_mtu, uvport_mtu, sizeof(struct odp_vport_mtu)))
534 vport_mtu.devname[IFNAMSIZ - 1] = '\0';
538 vport = vport_locate(vport_mtu.devname);
544 vport_mtu.mtu = vport_get_mtu(vport);
550 if (copy_to_user(uvport_mtu, &vport_mtu, sizeof(struct odp_vport_mtu)))
557 * vport_user_mtu_set - set device MTU (for userspace callers)
559 * @uvport_mtu: MTU request parameters.
561 * Sets the MTU of the given device. Some devices may not support setting the
562 * MTU, in which case the result will always be -EOPNOTSUPP. This function is
563 * for userspace callers and assumes no locks are held.
565 int vport_user_mtu_set(struct odp_vport_mtu __user *uvport_mtu)
567 struct odp_vport_mtu vport_mtu;
571 if (copy_from_user(&vport_mtu, uvport_mtu, sizeof(struct odp_vport_mtu)))
574 vport_mtu.devname[IFNAMSIZ - 1] = '\0';
579 vport = vport_locate(vport_mtu.devname);
585 err = vport_set_mtu(vport, vport_mtu.mtu);
593 static struct hlist_head *hash_bucket(const char *name)
595 unsigned int hash = full_name_hash(name, strlen(name));
596 return &dev_table[hash & (VPORT_HASH_BUCKETS - 1)];
600 * vport_locate - find a port that has already been created
602 * @name: name of port to find
604 * Either RTNL or vport lock must be acquired before calling this function
605 * and held while using the found port. See the locking comments at the
608 struct vport *vport_locate(const char *name)
610 struct hlist_head *bucket = hash_bucket(name);
612 struct hlist_node *node;
614 if (unlikely(!mutex_is_locked(&vport_mutex) && !rtnl_is_locked())) {
615 printk(KERN_ERR "openvswitch: neither RTNL nor vport lock held in vport_locate\n");
621 hlist_for_each_entry(vport, node, bucket, hash_node)
622 if (!strcmp(name, vport_get_name(vport)))
632 static void register_vport(struct vport *vport)
634 hlist_add_head(&vport->hash_node, hash_bucket(vport_get_name(vport)));
637 static void unregister_vport(struct vport *vport)
639 hlist_del(&vport->hash_node);
643 * vport_alloc - allocate and initialize new vport
645 * @priv_size: Size of private data area to allocate.
646 * @ops: vport device ops
648 * Allocate and initialize a new vport defined by @ops. The vport will contain
649 * a private data area of size @priv_size that can be accessed using
650 * vport_priv(). vports that are no longer needed should be released with
653 struct vport *vport_alloc(int priv_size, const struct vport_ops *ops)
658 alloc_size = sizeof(struct vport);
660 alloc_size = ALIGN(alloc_size, VPORT_ALIGN);
661 alloc_size += priv_size;
664 vport = kzalloc(alloc_size, GFP_KERNEL);
666 return ERR_PTR(-ENOMEM);
670 if (vport->ops->flags & VPORT_F_GEN_STATS) {
671 vport->percpu_stats = alloc_percpu(struct vport_percpu_stats);
672 if (!vport->percpu_stats)
673 return ERR_PTR(-ENOMEM);
675 spin_lock_init(&vport->stats_lock);
682 * vport_free - uninitialize and free vport
684 * @vport: vport to free
686 * Frees a vport allocated with vport_alloc() when it is no longer needed.
688 void vport_free(struct vport *vport)
690 if (vport->ops->flags & VPORT_F_GEN_STATS)
691 free_percpu(vport->percpu_stats);
697 * vport_add - add vport device (for kernel callers)
699 * @name: Name of new device.
700 * @type: Type of new device (to be matched against types in registered vport
702 * @config: Device type specific configuration. Userspace pointer.
704 * Creates a new vport with the specified configuration (which is dependent
705 * on device type). Both RTNL and vport locks must be held.
707 struct vport *vport_add(const char *name, const char *type, const void __user *config)
716 for (i = 0; i < n_vport_types; i++) {
717 if (!strcmp(vport_ops_list[i]->type, type)) {
718 vport = vport_ops_list[i]->create(name, config);
720 err = PTR_ERR(vport);
724 register_vport(vport);
736 * vport_mod - modify existing vport device (for kernel callers)
738 * @vport: vport to modify.
739 * @config: Device type specific configuration. Userspace pointer.
741 * Modifies an existing device with the specified configuration (which is
742 * dependent on device type). Both RTNL and vport locks must be held.
744 int vport_mod(struct vport *vport, const void __user *config)
749 if (vport->ops->modify)
750 return vport->ops->modify(vport, config);
756 * vport_del - delete existing vport device (for kernel callers)
758 * @vport: vport to delete.
760 * Deletes the specified device. The device must not be currently attached to
761 * a datapath. It is possible to fail for reasons such as lack of memory.
762 * Both RTNL and vport locks must be held.
764 int vport_del(struct vport *vport)
768 BUG_ON(vport_get_dp_port(vport));
770 unregister_vport(vport);
772 return vport->ops->destroy(vport);
776 * vport_attach - attach a vport to a datapath
778 * @vport: vport to attach.
779 * @dp_port: Datapath port to attach the vport to.
781 * Attaches a vport to a specific datapath so that packets may be exchanged.
782 * Both ports must be currently unattached. @dp_port must be successfully
783 * attached to a vport before it is connected to a datapath and must not be
784 * modified while connected. RTNL lock and the appropriate DP mutex must be held.
786 int vport_attach(struct vport *vport, struct dp_port *dp_port)
790 if (vport_get_dp_port(vport))
793 if (vport->ops->attach) {
796 err = vport->ops->attach(vport);
801 rcu_assign_pointer(vport->dp_port, dp_port);
807 * vport_detach - detach a vport from a datapath
809 * @vport: vport to detach.
811 * Detaches a vport from a datapath. May fail for a variety of reasons,
812 * including lack of memory. RTNL lock and the appropriate DP mutex must be held.
814 int vport_detach(struct vport *vport)
816 struct dp_port *dp_port;
820 dp_port = vport_get_dp_port(vport);
824 rcu_assign_pointer(vport->dp_port, NULL);
826 if (vport->ops->detach)
827 return vport->ops->detach(vport);
833 * vport_set_mtu - set device MTU (for kernel callers)
835 * @vport: vport on which to set MTU.
838 * Sets the MTU of the given device. Some devices may not support setting the
839 * MTU, in which case the result will always be -EOPNOTSUPP. RTNL lock must
842 int vport_set_mtu(struct vport *vport, int mtu)
849 if (vport->ops->set_mtu) {
852 ret = vport->ops->set_mtu(vport, mtu);
854 if (!ret && !is_internal_vport(vport)) {
855 struct dp_port *dp_port = vport_get_dp_port(vport);
858 set_internal_devs_mtu(dp_port->dp);
867 * vport_set_addr - set device Ethernet address (for kernel callers)
869 * @vport: vport on which to set Ethernet address.
870 * @addr: New address.
872 * Sets the Ethernet address of the given device. Some devices may not support
873 * setting the Ethernet address, in which case the result will always be
874 * -EOPNOTSUPP. RTNL lock must be held.
876 int vport_set_addr(struct vport *vport, const unsigned char *addr)
880 if (!is_valid_ether_addr(addr))
881 return -EADDRNOTAVAIL;
883 if (vport->ops->set_addr)
884 return vport->ops->set_addr(vport, addr);
890 * vport_set_stats - sets offset device stats (for kernel callers)
892 * @vport: vport on which to set stats
893 * @stats: stats to set
895 * Provides a set of transmit, receive, and error stats to be added as an
896 * offset to the collect data when stats are retreived. Some devices may not
897 * support setting the stats, in which case the result will always be
898 * -EOPNOTSUPP. RTNL lock must be held.
900 int vport_set_stats(struct vport *vport, struct odp_vport_stats *stats)
904 if (vport->ops->flags & VPORT_F_GEN_STATS) {
905 spin_lock_bh(&vport->stats_lock);
906 memcpy(&vport->offset_stats, stats, sizeof(struct odp_vport_stats));
907 spin_unlock_bh(&vport->stats_lock);
910 } else if (vport->ops->set_stats)
911 return vport->ops->set_stats(vport, stats);
917 * vport_get_name - retrieve device name
919 * @vport: vport from which to retrieve the name.
921 * Retrieves the name of the given device. Either RTNL lock or rcu_read_lock
922 * must be held for the entire duration that the name is in use.
924 const char *vport_get_name(const struct vport *vport)
926 return vport->ops->get_name(vport);
930 * vport_get_type - retrieve device type
932 * @vport: vport from which to retrieve the type.
934 * Retrieves the type of the given device. Either RTNL lock or rcu_read_lock
935 * must be held for the entire duration that the type is in use.
937 const char *vport_get_type(const struct vport *vport)
939 return vport->ops->type;
943 * vport_get_addr - retrieve device Ethernet address (for kernel callers)
945 * @vport: vport from which to retrieve the Ethernet address.
947 * Retrieves the Ethernet address of the given device. Either RTNL lock or
948 * rcu_read_lock must be held for the entire duration that the Ethernet address
951 const unsigned char *vport_get_addr(const struct vport *vport)
953 return vport->ops->get_addr(vport);
957 * vport_get_dp_port - retrieve attached datapath port
959 * @vport: vport from which to retrieve the datapath port.
961 * Retrieves the attached datapath port or null if not attached. Either RTNL
962 * lock or rcu_read_lock must be held for the entire duration that the datapath
963 * port is being accessed.
965 struct dp_port *vport_get_dp_port(const struct vport *vport)
967 return rcu_dereference(vport->dp_port);
971 * vport_get_kobj - retrieve associated kobj
973 * @vport: vport from which to retrieve the associated kobj
975 * Retrieves the associated kobj or null if no kobj. The returned kobj is
976 * valid for as long as the vport exists.
978 struct kobject *vport_get_kobj(const struct vport *vport)
980 if (vport->ops->get_kobj)
981 return vport->ops->get_kobj(vport);
987 * vport_get_stats - retrieve device stats (for kernel callers)
989 * @vport: vport from which to retrieve the stats
990 * @stats: location to store stats
992 * Retrieves transmit, receive, and error stats for the given device.
994 int vport_get_stats(struct vport *vport, struct odp_vport_stats *stats)
996 struct odp_vport_stats dev_stats;
997 struct odp_vport_stats *dev_statsp = NULL;
1000 if (vport->ops->get_stats) {
1001 if (vport->ops->flags & VPORT_F_GEN_STATS)
1002 dev_statsp = &dev_stats;
1007 err = vport->ops->get_stats(vport, dev_statsp);
1014 if (vport->ops->flags & VPORT_F_GEN_STATS) {
1017 /* We potentially have 3 sources of stats that need to be
1018 * combined: those we have collected (split into err_stats and
1019 * percpu_stats), offset_stats from set_stats(), and device
1020 * error stats from get_stats() (for errors that happen
1021 * downstream and therefore aren't reported through our
1022 * vport_record_error() function). */
1024 spin_lock_bh(&vport->stats_lock);
1026 memcpy(stats, &vport->offset_stats, sizeof(struct odp_vport_stats));
1028 stats->rx_errors += vport->err_stats.rx_errors
1029 + vport->err_stats.rx_frame_err
1030 + vport->err_stats.rx_over_err
1031 + vport->err_stats.rx_crc_err;
1032 stats->tx_errors += vport->err_stats.tx_errors;
1033 stats->tx_dropped += vport->err_stats.tx_dropped;
1034 stats->rx_dropped += vport->err_stats.rx_dropped;
1035 stats->rx_over_err += vport->err_stats.rx_over_err;
1036 stats->rx_crc_err += vport->err_stats.rx_crc_err;
1037 stats->rx_frame_err += vport->err_stats.rx_frame_err;
1038 stats->collisions += vport->err_stats.collisions;
1040 spin_unlock_bh(&vport->stats_lock);
1043 stats->rx_errors += dev_statsp->rx_errors;
1044 stats->tx_errors += dev_statsp->tx_errors;
1045 stats->rx_dropped += dev_statsp->rx_dropped;
1046 stats->tx_dropped += dev_statsp->tx_dropped;
1047 stats->rx_over_err += dev_statsp->rx_over_err;
1048 stats->rx_crc_err += dev_statsp->rx_crc_err;
1049 stats->rx_frame_err += dev_statsp->rx_frame_err;
1050 stats->collisions += dev_statsp->collisions;
1053 for_each_possible_cpu(i) {
1054 const struct vport_percpu_stats *percpu_stats;
1056 percpu_stats = per_cpu_ptr(vport->percpu_stats, i);
1057 stats->rx_bytes += percpu_stats->rx_bytes;
1058 stats->rx_packets += percpu_stats->rx_packets;
1059 stats->tx_bytes += percpu_stats->tx_bytes;
1060 stats->tx_packets += percpu_stats->tx_packets;
1072 * vport_get_flags - retrieve device flags
1074 * @vport: vport from which to retrieve the flags
1076 * Retrieves the flags of the given device. Either RTNL lock or rcu_read_lock
1079 unsigned vport_get_flags(const struct vport *vport)
1081 return vport->ops->get_dev_flags(vport);
1085 * vport_get_flags - check whether device is running
1087 * @vport: vport on which to check status.
1089 * Checks whether the given device is running. Either RTNL lock or
1090 * rcu_read_lock must be held.
1092 int vport_is_running(const struct vport *vport)
1094 return vport->ops->is_running(vport);
1098 * vport_get_flags - retrieve device operating state
1100 * @vport: vport from which to check status
1102 * Retrieves the RFC2863 operstate of the given device. Either RTNL lock or
1103 * rcu_read_lock must be held.
1105 unsigned char vport_get_operstate(const struct vport *vport)
1107 return vport->ops->get_operstate(vport);
1111 * vport_get_ifindex - retrieve device system interface index
1113 * @vport: vport from which to retrieve index
1115 * Retrieves the system interface index of the given device. Not all devices
1116 * will have system indexes, in which case the index of the datapath local
1117 * port is returned. Returns a negative index on error. Either RTNL lock or
1118 * rcu_read_lock must be held.
1120 int vport_get_ifindex(const struct vport *vport)
1122 const struct dp_port *dp_port;
1124 if (vport->ops->get_ifindex)
1125 return vport->ops->get_ifindex(vport);
1127 /* If we don't actually have an ifindex, use the local port's.
1128 * Userspace doesn't check it anyways. */
1129 dp_port = vport_get_dp_port(vport);
1133 return vport_get_ifindex(dp_port->dp->ports[ODPP_LOCAL]->vport);
1137 * vport_get_iflink - retrieve device system link index
1139 * @vport: vport from which to retrieve index
1141 * Retrieves the system link index of the given device. The link is the index
1142 * of the interface on which the packet will actually be sent. In most cases
1143 * this is the same as the ifindex but may be different for tunnel devices.
1144 * Returns a negative index on error. Either RTNL lock or rcu_read_lock must
1147 int vport_get_iflink(const struct vport *vport)
1149 if (vport->ops->get_iflink)
1150 return vport->ops->get_iflink(vport);
1152 /* If we don't have an iflink, use the ifindex. In most cases they
1154 return vport_get_ifindex(vport);
1158 * vport_get_mtu - retrieve device MTU (for kernel callers)
1160 * @vport: vport from which to retrieve MTU
1162 * Retrieves the MTU of the given device. Either RTNL lock or rcu_read_lock
1165 int vport_get_mtu(const struct vport *vport)
1167 return vport->ops->get_mtu(vport);
1171 * vport_receive - pass up received packet to the datapath for processing
1173 * @vport: vport that received the packet
1174 * @skb: skb that was received
1176 * Must be called with rcu_read_lock. The packet cannot be shared and
1177 * skb->data should point to the Ethernet header. The caller must have already
1178 * called compute_ip_summed() to initialize the checksumming fields.
1180 void vport_receive(struct vport *vport, struct sk_buff *skb)
1182 struct dp_port *dp_port = vport_get_dp_port(vport);
1185 vport_record_error(vport, VPORT_E_RX_DROPPED);
1191 if (vport->ops->flags & VPORT_F_GEN_STATS) {
1192 struct vport_percpu_stats *stats;
1196 stats = per_cpu_ptr(vport->percpu_stats, smp_processor_id());
1197 stats->rx_packets++;
1198 stats->rx_bytes += skb->len;
1203 if (!(vport->ops->flags & VPORT_F_TUN_ID))
1204 OVS_CB(skb)->tun_id = 0;
1206 dp_process_received_packet(dp_port, skb);
1209 static inline unsigned packet_length(const struct sk_buff *skb)
1211 unsigned length = skb->len - ETH_HLEN;
1213 if (skb->protocol == htons(ETH_P_8021Q))
1214 length -= VLAN_HLEN;
1220 * vport_send - send a packet on a device
1222 * @vport: vport on which to send the packet
1225 * Sends the given packet and returns the length of data sent. Either RTNL
1226 * lock or rcu_read_lock must be held.
1228 int vport_send(struct vport *vport, struct sk_buff *skb)
1233 mtu = vport_get_mtu(vport);
1234 if (unlikely(packet_length(skb) > mtu && !skb_is_gso(skb))) {
1235 if (net_ratelimit())
1236 printk(KERN_WARNING "%s: dropped over-mtu packet: %d > %d\n",
1237 dp_name(vport_get_dp_port(vport)->dp), packet_length(skb), mtu);
1241 sent = vport->ops->send(vport, skb);
1243 if (vport->ops->flags & VPORT_F_GEN_STATS && sent > 0) {
1244 struct vport_percpu_stats *stats;
1248 stats = per_cpu_ptr(vport->percpu_stats, smp_processor_id());
1249 stats->tx_packets++;
1250 stats->tx_bytes += sent;
1259 vport_record_error(vport, VPORT_E_TX_DROPPED);
1264 * vport_record_error - indicate device error to generic stats layer
1266 * @vport: vport that encountered the error
1267 * @err_type: one of enum vport_err_type types to indicate the error type
1269 * If using the vport generic stats layer indicate that an error of the given
1272 void vport_record_error(struct vport *vport, enum vport_err_type err_type)
1274 if (vport->ops->flags & VPORT_F_GEN_STATS) {
1276 spin_lock_bh(&vport->stats_lock);
1279 case VPORT_E_RX_DROPPED:
1280 vport->err_stats.rx_dropped++;
1283 case VPORT_E_RX_ERROR:
1284 vport->err_stats.rx_errors++;
1287 case VPORT_E_RX_FRAME:
1288 vport->err_stats.rx_frame_err++;
1291 case VPORT_E_RX_OVER:
1292 vport->err_stats.rx_over_err++;
1295 case VPORT_E_RX_CRC:
1296 vport->err_stats.rx_crc_err++;
1299 case VPORT_E_TX_DROPPED:
1300 vport->err_stats.tx_dropped++;
1303 case VPORT_E_TX_ERROR:
1304 vport->err_stats.tx_errors++;
1307 case VPORT_E_COLLISION:
1308 vport->err_stats.collisions++;
1312 spin_unlock_bh(&vport->stats_lock);