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>
20 extern struct vport_ops netdev_vport_ops;
21 extern struct vport_ops internal_vport_ops;
22 extern struct vport_ops gre_vport_ops;
24 static struct vport_ops *base_vport_ops_list[] = {
29 static const struct vport_ops **vport_ops_list;
30 static int n_vport_types;
32 static struct hlist_head *dev_table;
33 #define VPORT_HASH_BUCKETS 1024
35 /* Both RTNL lock and vport_mutex need to be held when updating dev_table.
37 * If you use vport_locate and then perform some operations, you need to hold
38 * one of these locks if you don't want the vport to be deleted out from under
41 * If you get a reference to a vport through a dp_port, it is protected
42 * by RCU and you need to hold rcu_read_lock instead when reading.
44 * If multiple locks are taken, the hierarchy is:
49 static DEFINE_MUTEX(vport_mutex);
52 * vport_lock - acquire vport lock
54 * Acquire global vport lock. See above comment about locking requirements
55 * and specific function definitions. May sleep.
60 mutex_lock(&vport_mutex);
64 * vport_unlock - release vport lock
66 * Release lock acquired with vport_lock.
71 mutex_unlock(&vport_mutex);
74 #define ASSERT_VPORT() do { \
75 if (unlikely(!mutex_is_locked(&vport_mutex))) { \
76 printk(KERN_ERR "openvswitch: vport lock not held at %s (%d)\n", \
77 __FILE__, __LINE__); \
83 * vport_init - initialize vport subsystem
85 * Called at module load time to initialize the vport subsystem and any
86 * 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];
111 if (new_ops->get_stats && new_ops->flags & VPORT_F_GEN_STATS) {
112 printk(KERN_INFO "openvswitch: both get_stats() and VPORT_F_GEN_STATS defined on vport %s, dropping VPORT_F_GEN_STATS\n", new_ops->type);
113 new_ops->flags &= ~VPORT_F_GEN_STATS;
117 err = new_ops->init();
122 vport_ops_list[n_vport_types++] = new_ops;
123 else if (new_ops->flags & VPORT_F_REQUIRED) {
145 for (i = 0; i < VPORT_HASH_BUCKETS; i++) {
146 struct hlist_head *bucket = &dev_table[i];
148 struct hlist_node *node, *next;
150 hlist_for_each_entry_safe(vport, node, next, bucket, hash_node)
159 * vport_exit - shutdown vport subsystem
161 * Called at module exit time to shutdown the vport subsystem and any
162 * initialized vport types.
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);
181 * vport_add - add vport device (for userspace callers)
183 * @uvport_config: New port configuration.
185 * Creates a new vport with the specified configuration (which is dependent
186 * on device type). This function is for userspace callers and assumes no
190 vport_add(const struct odp_vport_add __user *uvport_config)
192 struct odp_vport_add vport_config;
196 if (copy_from_user(&vport_config, uvport_config, sizeof(struct odp_vport_add)))
199 vport_config.port_type[VPORT_TYPE_SIZE - 1] = '\0';
200 vport_config.devname[IFNAMSIZ - 1] = '\0';
204 vport = vport_locate(vport_config.devname);
211 vport = __vport_add(vport_config.devname, vport_config.port_type,
212 vport_config.config);
216 err = PTR_ERR(vport);
224 * vport_mod - modify existing vport device (for userspace callers)
226 * @uvport_config: New configuration for vport
228 * Modifies an existing device with the specified configuration (which is
229 * dependent on device type). This function is for userspace callers and
230 * assumes no locks are held.
233 vport_mod(const struct odp_vport_mod __user *uvport_config)
235 struct odp_vport_mod vport_config;
239 if (copy_from_user(&vport_config, uvport_config, sizeof(struct odp_vport_mod)))
242 vport_config.devname[IFNAMSIZ - 1] = '\0';
246 vport = vport_locate(vport_config.devname);
253 err = __vport_mod(vport, vport_config.config);
262 * vport_del - delete existing vport device (for userspace callers)
264 * @udevname: Name of device to delete
266 * Deletes the specified device. Detaches the device from a datapath first
267 * if it is attached. Deleting the device will fail if it does not exist or it
268 * is the datapath local port. It is also possible to fail for less obvious
269 * reasons, such as lack of memory. This function is for userspace callers and
270 * assumes no locks are held.
273 vport_del(const char __user *udevname)
275 char devname[IFNAMSIZ];
277 struct dp_port *dp_port;
280 if (strncpy_from_user(devname, udevname, IFNAMSIZ - 1) < 0)
282 devname[IFNAMSIZ - 1] = '\0';
286 vport = vport_locate(devname);
292 dp_port = vport_get_dp_port(vport);
294 struct datapath *dp = dp_port->dp;
296 mutex_lock(&dp->mutex);
298 if (!strcmp(dp_name(dp), devname)) {
303 err = dp_detach_port(dp_port, 0);
306 mutex_unlock(&dp->mutex);
313 err = __vport_del(vport);
322 * vport_stats_get - retrieve device stats (for userspace callers)
324 * @ustats_req: Stats request parameters.
326 * Retrieves transmit, receive, and error stats for the given device. This
327 * function is for userspace callers and assumes no locks are held.
330 vport_stats_get(struct odp_vport_stats_req __user *ustats_req)
332 struct odp_vport_stats_req stats_req;
336 if (copy_from_user(&stats_req, ustats_req, sizeof(struct odp_vport_stats_req)))
339 stats_req.devname[IFNAMSIZ - 1] = '\0';
343 vport = vport_locate(stats_req.devname);
349 if (vport->ops->get_stats)
350 err = vport->ops->get_stats(vport, &stats_req.stats);
351 else if (vport->ops->flags & VPORT_F_GEN_STATS) {
354 memset(&stats_req.stats, 0, sizeof(struct odp_vport_stats));
356 for_each_possible_cpu(i) {
357 const struct vport_percpu_stats *percpu_stats;
359 percpu_stats = per_cpu_ptr(vport->percpu_stats, i);
360 stats_req.stats.rx_bytes += percpu_stats->rx_bytes;
361 stats_req.stats.rx_packets += percpu_stats->rx_packets;
362 stats_req.stats.tx_bytes += percpu_stats->tx_bytes;
363 stats_req.stats.tx_packets += percpu_stats->tx_packets;
366 spin_lock_bh(&vport->err_stats.lock);
368 stats_req.stats.rx_dropped = vport->err_stats.rx_dropped;
369 stats_req.stats.rx_errors = vport->err_stats.rx_errors
370 + vport->err_stats.rx_frame_err
371 + vport->err_stats.rx_over_err
372 + vport->err_stats.rx_crc_err;
373 stats_req.stats.rx_frame_err = vport->err_stats.rx_frame_err;
374 stats_req.stats.rx_over_err = vport->err_stats.rx_over_err;
375 stats_req.stats.rx_crc_err = vport->err_stats.rx_crc_err;
376 stats_req.stats.tx_dropped = vport->err_stats.tx_dropped;
377 stats_req.stats.tx_errors = vport->err_stats.tx_errors;
378 stats_req.stats.collisions = vport->err_stats.collisions;
380 spin_unlock_bh(&vport->err_stats.lock);
390 if (copy_to_user(ustats_req, &stats_req, sizeof(struct odp_vport_stats_req)))
397 * vport_ether_get - retrieve device Ethernet address (for userspace callers)
399 * @uvport_ether: Ethernet address request parameters.
401 * Retrieves the Ethernet address of the given device. This function is for
402 * userspace callers and assumes no locks are held.
405 vport_ether_get(struct odp_vport_ether __user *uvport_ether)
407 struct odp_vport_ether vport_ether;
411 if (copy_from_user(&vport_ether, uvport_ether, sizeof(struct odp_vport_ether)))
414 vport_ether.devname[IFNAMSIZ - 1] = '\0';
418 vport = vport_locate(vport_ether.devname);
424 memcpy(vport_ether.ether_addr, vport_get_addr(vport), ETH_ALEN);
430 if (copy_to_user(uvport_ether, &vport_ether, sizeof(struct odp_vport_ether)))
437 * vport_ether_set - set device Ethernet address (for userspace callers)
439 * @uvport_ether: Ethernet address request parameters.
441 * Sets the Ethernet address of the given device. Some devices may not support
442 * setting the Ethernet address, in which case the result will always be
443 * -EOPNOTSUPP. This function is for userspace callers and assumes no locks
447 vport_ether_set(struct odp_vport_ether __user *uvport_ether)
449 struct odp_vport_ether vport_ether;
453 if (copy_from_user(&vport_ether, uvport_ether, sizeof(struct odp_vport_ether)))
456 vport_ether.devname[IFNAMSIZ - 1] = '\0';
461 vport = vport_locate(vport_ether.devname);
467 err = vport_set_addr(vport, vport_ether.ether_addr);
476 * vport_mut_get - retrieve device MTU (for userspace callers)
478 * @uvport_mtu: MTU request parameters.
480 * Retrieves the MTU of the given device. This function is for userspace
481 * callers and assumes no locks are held.
484 vport_mtu_get(struct odp_vport_mtu __user *uvport_mtu)
486 struct odp_vport_mtu vport_mtu;
490 if (copy_from_user(&vport_mtu, uvport_mtu, sizeof(struct odp_vport_mtu)))
493 vport_mtu.devname[IFNAMSIZ - 1] = '\0';
497 vport = vport_locate(vport_mtu.devname);
503 vport_mtu.mtu = vport_get_mtu(vport);
509 if (copy_to_user(uvport_mtu, &vport_mtu, sizeof(struct odp_vport_mtu)))
516 * vport_mtu_set - set device MTU (for userspace callers)
518 * @uvport_mtu: MTU request parameters.
520 * Sets the MTU of the given device. Some devices may not support setting the
521 * MTU, in which case the result will always be -EOPNOTSUPP. This function is
522 * for userspace callers and assumes no locks are held.
525 vport_mtu_set(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';
539 vport = vport_locate(vport_mtu.devname);
545 err = vport_set_mtu(vport, vport_mtu.mtu);
553 static struct hlist_head *
554 hash_bucket(const char *name)
556 unsigned int hash = full_name_hash(name, strlen(name));
557 return &dev_table[hash & (VPORT_HASH_BUCKETS - 1)];
561 * vport_locate - find a port that has already been created
563 * @name: name of port to find
565 * Either RTNL or vport lock must be acquired before calling this function
566 * and held while using the found port. See the locking comments at the
570 vport_locate(const char *name)
572 struct hlist_head *bucket = hash_bucket(name);
574 struct hlist_node *node;
576 if (unlikely(!mutex_is_locked(&vport_mutex) && !rtnl_is_locked())) {
577 printk(KERN_ERR "openvswitch: neither RTNL nor vport lock held in vport_locate\n");
581 hlist_for_each_entry(vport, node, bucket, hash_node)
582 if (!strcmp(name, vport_get_name(vport)))
589 register_vport(struct vport *vport)
591 hlist_add_head(&vport->hash_node, hash_bucket(vport_get_name(vport)));
595 unregister_vport(struct vport *vport)
597 hlist_del(&vport->hash_node);
601 * vport_alloc - allocate and initialize new vport
603 * @priv_size: Size of private data area to allocate.
604 * @ops: vport device ops
606 * Allocate and initialize a new vport defined by @ops. The vport will contain
607 * a private data area of size @priv_size that can be accessed using
608 * vport_priv(). vports that are no longer needed should be released with
612 vport_alloc(int priv_size, const struct vport_ops *ops)
617 alloc_size = sizeof(struct vport);
619 alloc_size = ALIGN(alloc_size, VPORT_ALIGN);
620 alloc_size += priv_size;
623 vport = kzalloc(alloc_size, GFP_KERNEL);
625 return ERR_PTR(-ENOMEM);
629 if (vport->ops->flags & VPORT_F_GEN_STATS) {
630 vport->percpu_stats = alloc_percpu(struct vport_percpu_stats);
631 if (!vport->percpu_stats)
632 return ERR_PTR(-ENOMEM);
634 spin_lock_init(&vport->err_stats.lock);
641 * vport_free - uninitialize and free vport
643 * @vport: vport to free
645 * Frees a vport allocated with vport_alloc() when it is no longer needed.
648 vport_free(struct vport *vport)
650 if (vport->ops->flags & VPORT_F_GEN_STATS)
651 free_percpu(vport->percpu_stats);
657 * __vport_add - add vport device (for kernel callers)
659 * @name: Name of new device.
660 * @type: Type of new device (to be matched against types in registered vport
662 * @config: Device type specific configuration. Userspace pointer.
664 * Creates a new vport with the specified configuration (which is dependent
665 * on device type). Both RTNL and vport locks must be held.
668 __vport_add(const char *name, const char *type, const void __user *config)
677 for (i = 0; i < n_vport_types; i++) {
678 if (!strcmp(vport_ops_list[i]->type, type)) {
679 vport = vport_ops_list[i]->create(name, config);
681 err = PTR_ERR(vport);
685 register_vport(vport);
697 * __vport_mod - modify existing vport device (for kernel callers)
699 * @vport: vport to modify.
700 * @config: Device type specific configuration. Userspace pointer.
702 * Modifies an existing device with the specified configuration (which is
703 * dependent on device type). Both RTNL and vport locks must be held.
706 __vport_mod(struct vport *vport, const void __user *config)
711 if (vport->ops->modify)
712 return vport->ops->modify(vport, config);
718 * __vport_del - delete existing vport device (for kernel callers)
720 * @vport: vport to delete.
722 * Deletes the specified device. The device must not be currently attached to
723 * a datapath. It is possible to fail for reasons such as lack of memory.
724 * Both RTNL and vport locks must be held.
727 __vport_del(struct vport *vport)
731 BUG_ON(vport_get_dp_port(vport));
733 unregister_vport(vport);
735 return vport->ops->destroy(vport);
739 * vport_attach - attach a vport to a datapath
741 * @vport: vport to attach.
742 * @dp_port: Datapath port to attach the vport to.
744 * Attaches a vport to a specific datapath so that packets may be exchanged.
745 * Both ports must be currently unattached. @dp_port must be successfully
746 * attached to a vport before it is connected to a datapath and must not be
747 * modified while connected. RTNL lock and the appropriate DP mutex must be held.
750 vport_attach(struct vport *vport, struct dp_port *dp_port)
757 if (vport_get_dp_port(vport))
760 if (vport->ops->attach) {
763 err = vport->ops->attach(vport);
768 dp_port->vport = vport;
769 rcu_assign_pointer(vport->dp_port, dp_port);
775 * vport_detach - detach a vport from a datapath
777 * @vport: vport to detach.
779 * Detaches a vport from a datapath. May fail for a variety of reasons,
780 * including lack of memory. RTNL lock and the appropriate DP mutex must be held.
783 vport_detach(struct vport *vport)
785 struct dp_port *dp_port;
789 dp_port = vport_get_dp_port(vport);
793 dp_port->vport = NULL;
794 rcu_assign_pointer(vport->dp_port, NULL);
796 if (vport->ops->detach)
797 return vport->ops->detach(vport);
803 * vport_set_mtu - set device MTU (for kernel callers)
805 * @vport: vport on which to set MTU.
808 * Sets the MTU of the given device. Some devices may not support setting the
809 * MTU, in which case the result will always be -EOPNOTSUPP. RTNL lock must
813 vport_set_mtu(struct vport *vport, int mtu)
820 if (vport->ops->set_mtu)
821 return vport->ops->set_mtu(vport, mtu);
827 * vport_set_addr - set device Ethernet address (for kernel callers)
829 * @vport: vport on which to set Ethernet address.
830 * @addr: New address.
832 * Sets the Ethernet address of the given device. Some devices may not support
833 * setting the Ethernet address, in which case the result will always be
834 * -EOPNOTSUPP. RTNL lock must be held.
837 vport_set_addr(struct vport *vport, const unsigned char *addr)
841 if (!is_valid_ether_addr(addr))
842 return -EADDRNOTAVAIL;
844 if (vport->ops->set_addr)
845 return vport->ops->set_addr(vport, addr);
851 * vport_get_name - retrieve device name
853 * @vport: vport from which to retrieve the name.
855 * Retrieves the name of the given device. Either RTNL lock or rcu_read_lock
856 * must be held for the entire duration that the name is in use.
859 vport_get_name(const struct vport *vport)
861 return vport->ops->get_name(vport);
865 * vport_get_type - retrieve device type
867 * @vport: vport from which to retrieve the type.
869 * Retrieves the type of the given device. Either RTNL lock or rcu_read_lock
870 * must be held for the entire duration that the type is in use.
873 vport_get_type(const struct vport *vport)
875 return vport->ops->type;
879 * vport_get_addr - retrieve device Ethernet address (for kernel callers)
881 * @vport: vport from which to retrieve the Ethernet address.
883 * Retrieves the Ethernet address of the given device. Either RTNL lock or
884 * rcu_read_lock must be held for the entire duration that the Ethernet address
887 const unsigned char *
888 vport_get_addr(const struct vport *vport)
890 return vport->ops->get_addr(vport);
894 * vport_get_dp_port - retrieve attached datapath port
896 * @vport: vport from which to retrieve the datapath port.
898 * Retrieves the attached datapath port or null if not attached. Either RTNL
899 * lock or rcu_read_lock must be held for the entire duration that the datapath
900 * port is being accessed.
903 vport_get_dp_port(const struct vport *vport)
905 return rcu_dereference(vport->dp_port);
909 * vport_get_kobj - retrieve associated kobj
911 * @vport: vport from which to retrieve the associated kobj
913 * Retrieves the associated kobj or null if no kobj. The returned kobj is
914 * valid for as long as the vport exists.
917 vport_get_kobj(const struct vport *vport)
919 if (vport->ops->get_kobj)
920 return vport->ops->get_kobj(vport);
926 * vport_get_flags - retrieve device flags
928 * @vport: vport from which to retrieve the flags
930 * Retrieves the flags of the given device. Either RTNL lock or rcu_read_lock
934 vport_get_flags(const struct vport *vport)
936 return vport->ops->get_dev_flags(vport);
940 * vport_get_flags - check whether device is running
942 * @vport: vport on which to check status.
944 * Checks whether the given device is running. Either RTNL lock or
945 * rcu_read_lock must be held.
948 vport_is_running(const struct vport *vport)
950 return vport->ops->is_running(vport);
954 * vport_get_flags - retrieve device operating state
956 * @vport: vport from which to check status
958 * Retrieves the RFC2863 operstate of the given device. Either RTNL lock or
959 * rcu_read_lock must be held.
962 vport_get_operstate(const struct vport *vport)
964 return vport->ops->get_operstate(vport);
968 * vport_get_ifindex - retrieve device system interface index
970 * @vport: vport from which to retrieve index
972 * Retrieves the system interface index of the given device. Not all devices
973 * will have system indexes, in which case the index of the datapath local
974 * port is returned. Returns a negative index on error. Either RTNL lock or
975 * rcu_read_lock must be held.
978 vport_get_ifindex(const struct vport *vport)
980 const struct dp_port *dp_port;
982 if (vport->ops->get_ifindex)
983 return vport->ops->get_ifindex(vport);
985 /* If we don't actually have an ifindex, use the local port's.
986 * Userspace doesn't check it anyways. */
987 dp_port = vport_get_dp_port(vport);
991 return vport_get_ifindex(dp_port->dp->ports[ODPP_LOCAL]->vport);
995 * vport_get_iflink - retrieve device system link index
997 * @vport: vport from which to retrieve index
999 * Retrieves the system link index of the given device. The link is the index
1000 * of the interface on which the packet will actually be sent. In most cases
1001 * this is the same as the ifindex but may be different for tunnel devices.
1002 * Returns a negative index on error. Either RTNL lock or rcu_read_lock must
1006 vport_get_iflink(const struct vport *vport)
1008 if (vport->ops->get_iflink)
1009 return vport->ops->get_iflink(vport);
1011 /* If we don't have an iflink, use the ifindex. In most cases they
1013 return vport_get_ifindex(vport);
1017 * vport_get_mtu - retrieve device MTU (for kernel callers)
1019 * @vport: vport from which to retrieve MTU
1021 * Retrieves the MTU of the given device. Either RTNL lock or rcu_read_lock
1025 vport_get_mtu(const struct vport *vport)
1027 return vport->ops->get_mtu(vport);
1031 * vport_receive - pass up received packet to the datapath for processing
1033 * @vport: vport that received the packet
1034 * @skb: skb that was received
1036 * Must be called with rcu_read_lock and bottom halves disabled. The packet
1037 * cannot be shared and skb->data should point to the Ethernet header.
1040 vport_receive(struct vport *vport, struct sk_buff *skb)
1042 struct dp_port *dp_port = vport_get_dp_port(vport);
1047 if (vport->ops->flags & VPORT_F_GEN_STATS) {
1048 struct vport_percpu_stats *stats;
1052 stats = per_cpu_ptr(vport->percpu_stats, smp_processor_id());
1053 stats->rx_packets++;
1054 stats->rx_bytes += skb->len;
1059 if (!(vport->ops->flags & VPORT_F_TUN_ID))
1060 OVS_CB(skb)->tun_id = 0;
1062 dp_process_received_packet(dp_port, skb);
1066 * vport_send - send a packet on a device
1068 * @vport: vport on which to send the packet
1071 * Sends the given packet and returns the length of data sent. Either RTNL
1072 * lock or rcu_read_lock must be held.
1075 vport_send(struct vport *vport, struct sk_buff *skb)
1079 sent = vport->ops->send(vport, skb);
1081 if (vport->ops->flags & VPORT_F_GEN_STATS && sent > 0) {
1082 struct vport_percpu_stats *stats;
1086 stats = per_cpu_ptr(vport->percpu_stats, smp_processor_id());
1087 stats->tx_packets++;
1088 stats->tx_bytes += sent;
1097 * vport_record_error - indicate device error to generic stats layer
1099 * @vport: vport that encountered the error
1100 * @err_type: one of enum vport_err_type types to indicate the error type
1102 * If using the vport generic stats layer indicate that an error of the given
1106 vport_record_error(struct vport *vport, enum vport_err_type err_type)
1108 if (vport->ops->flags & VPORT_F_GEN_STATS) {
1110 spin_lock_bh(&vport->err_stats.lock);
1113 case VPORT_E_RX_DROPPED:
1114 vport->err_stats.rx_dropped++;
1117 case VPORT_E_RX_ERROR:
1118 vport->err_stats.rx_errors++;
1121 case VPORT_E_RX_FRAME:
1122 vport->err_stats.rx_frame_err++;
1125 case VPORT_E_RX_OVER:
1126 vport->err_stats.rx_over_err++;
1129 case VPORT_E_RX_CRC:
1130 vport->err_stats.rx_crc_err++;
1133 case VPORT_E_TX_DROPPED:
1134 vport->err_stats.tx_dropped++;
1137 case VPORT_E_TX_ERROR:
1138 vport->err_stats.tx_errors++;
1141 case VPORT_E_COLLISION:
1142 vport->err_stats.collisions++;
1146 spin_unlock_bh(&vport->err_stats.lock);
1151 * vport_gen_ether_addr - generate an Ethernet address
1153 * @addr: location to store generated address
1155 * Generates a random Ethernet address for use when creating a device that
1156 * has no natural address.
1159 vport_gen_ether_addr(u8 *addr)
1161 random_ether_addr(addr);
1163 /* Set the OUI to the Nicira one. */
1168 /* Set the top bit to indicate random address. */