2 * Copyright (c) 2010, 2011 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 datapath, 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) {
140 * vport_exit - shutdown vport subsystem
142 * Called at module exit time to shutdown the vport subsystem and any
143 * initialized vport types.
145 void vport_exit(void)
149 for (i = 0; i < n_vport_types; i++) {
150 if (vport_ops_list[i]->exit)
151 vport_ops_list[i]->exit();
154 kfree(vport_ops_list);
158 static struct hlist_head *hash_bucket(const char *name)
160 unsigned int hash = full_name_hash(name, strlen(name));
161 return &dev_table[hash & (VPORT_HASH_BUCKETS - 1)];
165 * vport_locate - find a port that has already been created
167 * @name: name of port to find
169 * Either RTNL or vport lock must be acquired before calling this function
170 * and held while using the found port. See the locking comments at the
173 struct vport *vport_locate(const char *name)
175 struct hlist_head *bucket = hash_bucket(name);
177 struct hlist_node *node;
179 if (unlikely(!mutex_is_locked(&vport_mutex) && !rtnl_is_locked())) {
180 pr_err("neither RTNL nor vport lock held in vport_locate\n");
186 hlist_for_each_entry(vport, node, bucket, hash_node)
187 if (!strcmp(name, vport_get_name(vport)))
197 static void register_vport(struct vport *vport)
199 hlist_add_head(&vport->hash_node, hash_bucket(vport_get_name(vport)));
202 static void unregister_vport(struct vport *vport)
204 hlist_del(&vport->hash_node);
207 static void release_vport(struct kobject *kobj)
209 struct vport *p = container_of(kobj, struct vport, kobj);
213 static struct kobj_type brport_ktype = {
215 .sysfs_ops = &brport_sysfs_ops,
217 .release = release_vport
221 * vport_alloc - allocate and initialize new vport
223 * @priv_size: Size of private data area to allocate.
224 * @ops: vport device ops
226 * Allocate and initialize a new vport defined by @ops. The vport will contain
227 * a private data area of size @priv_size that can be accessed using
228 * vport_priv(). vports that are no longer needed should be released with
231 struct vport *vport_alloc(int priv_size, const struct vport_ops *ops, const struct vport_parms *parms)
236 alloc_size = sizeof(struct vport);
238 alloc_size = ALIGN(alloc_size, VPORT_ALIGN);
239 alloc_size += priv_size;
242 vport = kzalloc(alloc_size, GFP_KERNEL);
244 return ERR_PTR(-ENOMEM);
246 vport->dp = parms->dp;
247 vport->port_no = parms->port_no;
248 atomic_set(&vport->sflow_pool, 0);
251 /* Initialize kobject for bridge. This will be added as
252 * /sys/class/net/<devname>/brport later, if sysfs is enabled. */
253 vport->kobj.kset = NULL;
254 kobject_init(&vport->kobj, &brport_ktype);
256 if (vport->ops->flags & VPORT_F_GEN_STATS) {
257 vport->percpu_stats = alloc_percpu(struct vport_percpu_stats);
258 if (!vport->percpu_stats)
259 return ERR_PTR(-ENOMEM);
261 spin_lock_init(&vport->stats_lock);
268 * vport_free - uninitialize and free vport
270 * @vport: vport to free
272 * Frees a vport allocated with vport_alloc() when it is no longer needed.
274 void vport_free(struct vport *vport)
276 if (vport->ops->flags & VPORT_F_GEN_STATS)
277 free_percpu(vport->percpu_stats);
279 kobject_put(&vport->kobj);
283 * vport_add - add vport device (for kernel callers)
285 * @parms: Information about new vport.
287 * Creates a new vport with the specified configuration (which is dependent on
288 * device type) and attaches it to a datapath. Both RTNL and vport locks must
291 struct vport *vport_add(const struct vport_parms *parms)
300 for (i = 0; i < n_vport_types; i++) {
301 if (vport_ops_list[i]->type == parms->type) {
302 vport = vport_ops_list[i]->create(parms);
304 err = PTR_ERR(vport);
308 register_vport(vport);
320 * vport_set_options - modify existing vport device (for kernel callers)
322 * @vport: vport to modify.
323 * @port: New configuration.
325 * Modifies an existing device with the specified configuration (which is
326 * dependent on device type). RTNL lock must be held.
328 int vport_set_options(struct vport *vport, struct nlattr *options)
332 if (!vport->ops->set_options)
334 return vport->ops->set_options(vport, options);
338 * vport_del - delete existing vport device (for kernel callers)
340 * @vport: vport to delete.
342 * Detaches @vport from its datapath and destroys it. It is possible to fail
343 * for reasons such as lack of memory. Both RTNL and vport locks must be held.
345 int vport_del(struct vport *vport)
350 unregister_vport(vport);
352 return vport->ops->destroy(vport);
356 * vport_set_mtu - set device MTU (for kernel callers)
358 * @vport: vport on which to set MTU.
361 * Sets the MTU of the given device. Some devices may not support setting the
362 * MTU, in which case the result will always be -EOPNOTSUPP. RTNL lock must
365 int vport_set_mtu(struct vport *vport, int mtu)
372 if (vport->ops->set_mtu) {
375 ret = vport->ops->set_mtu(vport, mtu);
377 if (!ret && !is_internal_vport(vport))
378 set_internal_devs_mtu(vport->dp);
386 * vport_set_addr - set device Ethernet address (for kernel callers)
388 * @vport: vport on which to set Ethernet address.
389 * @addr: New address.
391 * Sets the Ethernet address of the given device. Some devices may not support
392 * setting the Ethernet address, in which case the result will always be
393 * -EOPNOTSUPP. RTNL lock must be held.
395 int vport_set_addr(struct vport *vport, const unsigned char *addr)
399 if (!is_valid_ether_addr(addr))
400 return -EADDRNOTAVAIL;
402 if (vport->ops->set_addr)
403 return vport->ops->set_addr(vport, addr);
409 * vport_set_stats - sets offset device stats (for kernel callers)
411 * @vport: vport on which to set stats
412 * @stats: stats to set
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. RTNL lock must be held.
419 int vport_set_stats(struct vport *vport, struct rtnl_link_stats64 *stats)
423 if (vport->ops->flags & VPORT_F_GEN_STATS) {
424 spin_lock_bh(&vport->stats_lock);
425 vport->offset_stats = *stats;
426 spin_unlock_bh(&vport->stats_lock);
434 * vport_get_name - retrieve device name
436 * @vport: vport from which to retrieve the name.
438 * Retrieves the name of the given device. Either RTNL lock or rcu_read_lock
439 * must be held for the entire duration that the name is in use.
441 const char *vport_get_name(const struct vport *vport)
443 return vport->ops->get_name(vport);
447 * vport_get_type - retrieve device type
449 * @vport: vport from which to retrieve the type.
451 * Retrieves the type of the given device. Either RTNL lock or rcu_read_lock
454 enum odp_vport_type vport_get_type(const struct vport *vport)
456 return vport->ops->type;
460 * vport_get_addr - retrieve device Ethernet address (for kernel callers)
462 * @vport: vport from which to retrieve the Ethernet address.
464 * Retrieves the Ethernet address of the given device. Either RTNL lock or
465 * rcu_read_lock must be held for the entire duration that the Ethernet address
468 const unsigned char *vport_get_addr(const struct vport *vport)
470 return vport->ops->get_addr(vport);
474 * vport_get_kobj - retrieve associated kobj
476 * @vport: vport from which to retrieve the associated kobj
478 * Retrieves the associated kobj or null if no kobj. The returned kobj is
479 * valid for as long as the vport exists.
481 struct kobject *vport_get_kobj(const struct vport *vport)
483 if (vport->ops->get_kobj)
484 return vport->ops->get_kobj(vport);
489 static int vport_call_get_stats(struct vport *vport, struct rtnl_link_stats64 *stats)
494 err = vport->ops->get_stats(vport, stats);
501 * vport_get_stats - retrieve device stats (for kernel callers)
503 * @vport: vport from which to retrieve the stats
504 * @stats: location to store stats
506 * Retrieves transmit, receive, and error stats for the given device.
508 int vport_get_stats(struct vport *vport, struct rtnl_link_stats64 *stats)
512 if (!(vport->ops->flags & VPORT_F_GEN_STATS))
513 return vport_call_get_stats(vport, stats);
515 /* We potentially have 3 sources of stats that need to be
516 * combined: those we have collected (split into err_stats and
517 * percpu_stats), offset_stats from set_stats(), and device
518 * error stats from get_stats() (for errors that happen
519 * downstream and therefore aren't reported through our
520 * vport_record_error() function). */
522 spin_lock_bh(&vport->stats_lock);
524 *stats = vport->offset_stats;
526 stats->rx_errors += vport->err_stats.rx_errors;
527 stats->tx_errors += vport->err_stats.tx_errors;
528 stats->tx_dropped += vport->err_stats.tx_dropped;
529 stats->rx_dropped += vport->err_stats.rx_dropped;
531 spin_unlock_bh(&vport->stats_lock);
533 if (vport->ops->get_stats) {
534 struct rtnl_link_stats64 dev_stats;
537 err = vport_call_get_stats(vport, &dev_stats);
541 stats->rx_errors += dev_stats.rx_errors;
542 stats->tx_errors += dev_stats.tx_errors;
543 stats->rx_dropped += dev_stats.rx_dropped;
544 stats->tx_dropped += dev_stats.tx_dropped;
545 stats->multicast += dev_stats.multicast;
546 stats->collisions += dev_stats.collisions;
547 stats->rx_length_errors += dev_stats.rx_length_errors;
548 stats->rx_over_errors += dev_stats.rx_over_errors;
549 stats->rx_crc_errors += dev_stats.rx_crc_errors;
550 stats->rx_frame_errors += dev_stats.rx_frame_errors;
551 stats->rx_fifo_errors += dev_stats.rx_fifo_errors;
552 stats->rx_missed_errors += dev_stats.rx_missed_errors;
553 stats->tx_aborted_errors += dev_stats.tx_aborted_errors;
554 stats->tx_carrier_errors += dev_stats.tx_carrier_errors;
555 stats->tx_fifo_errors += dev_stats.tx_fifo_errors;
556 stats->tx_heartbeat_errors += dev_stats.tx_heartbeat_errors;
557 stats->tx_window_errors += dev_stats.tx_window_errors;
558 stats->rx_compressed += dev_stats.rx_compressed;
559 stats->tx_compressed += dev_stats.tx_compressed;
562 for_each_possible_cpu(i) {
563 const struct vport_percpu_stats *percpu_stats;
564 struct vport_percpu_stats local_stats;
567 percpu_stats = per_cpu_ptr(vport->percpu_stats, i);
570 seqcount = read_seqcount_begin(&percpu_stats->seqlock);
571 local_stats = *percpu_stats;
572 } while (read_seqcount_retry(&percpu_stats->seqlock, seqcount));
574 stats->rx_bytes += local_stats.rx_bytes;
575 stats->rx_packets += local_stats.rx_packets;
576 stats->tx_bytes += local_stats.tx_bytes;
577 stats->tx_packets += local_stats.tx_packets;
584 * vport_get_flags - retrieve device flags
586 * @vport: vport from which to retrieve the flags
588 * Retrieves the flags of the given device. Either RTNL lock or rcu_read_lock
591 unsigned vport_get_flags(const struct vport *vport)
593 return vport->ops->get_dev_flags(vport);
597 * vport_get_flags - check whether device is running
599 * @vport: vport on which to check status.
601 * Checks whether the given device is running. Either RTNL lock or
602 * rcu_read_lock must be held.
604 int vport_is_running(const struct vport *vport)
606 return vport->ops->is_running(vport);
610 * vport_get_flags - retrieve device operating state
612 * @vport: vport from which to check status
614 * Retrieves the RFC2863 operstate of the given device. Either RTNL lock or
615 * rcu_read_lock must be held.
617 unsigned char vport_get_operstate(const struct vport *vport)
619 return vport->ops->get_operstate(vport);
623 * vport_get_ifindex - retrieve device system interface index
625 * @vport: vport from which to retrieve index
627 * Retrieves the system interface index of the given device or 0 if
628 * the device does not have one (in the case of virtual ports).
629 * Returns a negative index on error. Either RTNL lock or
630 * rcu_read_lock must be held.
632 int vport_get_ifindex(const struct vport *vport)
634 if (vport->ops->get_ifindex)
635 return vport->ops->get_ifindex(vport);
641 * vport_get_iflink - retrieve device system link index
643 * @vport: vport from which to retrieve index
645 * Retrieves the system link index of the given device. The link is the index
646 * of the interface on which the packet will actually be sent. In most cases
647 * this is the same as the ifindex but may be different for tunnel devices.
648 * Returns a negative index on error. Either RTNL lock or rcu_read_lock must
651 int vport_get_iflink(const struct vport *vport)
653 if (vport->ops->get_iflink)
654 return vport->ops->get_iflink(vport);
656 /* If we don't have an iflink, use the ifindex. In most cases they
658 return vport_get_ifindex(vport);
662 * vport_get_mtu - retrieve device MTU (for kernel callers)
664 * @vport: vport from which to retrieve MTU
666 * Retrieves the MTU of the given device. Either RTNL lock or rcu_read_lock
669 int vport_get_mtu(const struct vport *vport)
671 return vport->ops->get_mtu(vport);
675 * vport_get_options - retrieve device options
677 * @vport: vport from which to retrieve the options.
678 * @skb: sk_buff where options should be appended.
680 * Retrieves the configuration of the given device, appending an
681 * %ODP_VPORT_ATTR_OPTIONS attribute that in turn contains nested
682 * vport-specific attributes to @skb. Either RTNL lock or rcu_read_lock must
685 * Returns 0 if successful, -EMSGSIZE if @skb has insufficient room, or another
686 * negative error code if a real error occurred.
688 int vport_get_options(const struct vport *vport, struct sk_buff *skb)
692 nla = nla_nest_start(skb, ODP_VPORT_ATTR_OPTIONS);
696 if (vport->ops->get_options) {
697 int err = vport->ops->get_options(vport, skb);
702 nla_nest_end(skb, nla);
707 * vport_receive - pass up received packet to the datapath for processing
709 * @vport: vport that received the packet
710 * @skb: skb that was received
712 * Must be called with rcu_read_lock. The packet cannot be shared and
713 * skb->data should point to the Ethernet header. The caller must have already
714 * called compute_ip_summed() to initialize the checksumming fields.
716 void vport_receive(struct vport *vport, struct sk_buff *skb)
718 if (vport->ops->flags & VPORT_F_GEN_STATS) {
719 struct vport_percpu_stats *stats;
722 stats = per_cpu_ptr(vport->percpu_stats, smp_processor_id());
724 write_seqcount_begin(&stats->seqlock);
726 stats->rx_bytes += skb->len;
727 write_seqcount_end(&stats->seqlock);
732 if (!(vport->ops->flags & VPORT_F_FLOW))
733 OVS_CB(skb)->flow = NULL;
735 if (!(vport->ops->flags & VPORT_F_TUN_ID))
736 OVS_CB(skb)->tun_id = 0;
738 dp_process_received_packet(vport, skb);
741 static inline unsigned packet_length(const struct sk_buff *skb)
743 unsigned length = skb->len - ETH_HLEN;
745 if (skb->protocol == htons(ETH_P_8021Q))
752 * vport_send - send a packet on a device
754 * @vport: vport on which to send the packet
757 * Sends the given packet and returns the length of data sent. Either RTNL
758 * lock or rcu_read_lock must be held.
760 int vport_send(struct vport *vport, struct sk_buff *skb)
765 mtu = vport_get_mtu(vport);
766 if (unlikely(packet_length(skb) > mtu && !skb_is_gso(skb))) {
768 pr_warn("%s: dropped over-mtu packet: %d > %d\n",
769 dp_name(vport->dp), packet_length(skb), mtu);
773 sent = vport->ops->send(vport, skb);
775 if (vport->ops->flags & VPORT_F_GEN_STATS && sent > 0) {
776 struct vport_percpu_stats *stats;
779 stats = per_cpu_ptr(vport->percpu_stats, smp_processor_id());
781 write_seqcount_begin(&stats->seqlock);
783 stats->tx_bytes += sent;
784 write_seqcount_end(&stats->seqlock);
793 vport_record_error(vport, VPORT_E_TX_DROPPED);
798 * vport_record_error - indicate device error to generic stats layer
800 * @vport: vport that encountered the error
801 * @err_type: one of enum vport_err_type types to indicate the error type
803 * If using the vport generic stats layer indicate that an error of the given
806 void vport_record_error(struct vport *vport, enum vport_err_type err_type)
808 if (vport->ops->flags & VPORT_F_GEN_STATS) {
810 spin_lock_bh(&vport->stats_lock);
813 case VPORT_E_RX_DROPPED:
814 vport->err_stats.rx_dropped++;
817 case VPORT_E_RX_ERROR:
818 vport->err_stats.rx_errors++;
821 case VPORT_E_TX_DROPPED:
822 vport->err_stats.tx_dropped++;
825 case VPORT_E_TX_ERROR:
826 vport->err_stats.tx_errors++;
830 spin_unlock_bh(&vport->stats_lock);