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/rcupdate.h>
20 #include <linux/rtnetlink.h>
21 #include <linux/compat.h>
22 #include <linux/version.h>
25 #include "vport-internal_dev.h"
27 /* List of statically compiled vport implementations. Don't forget to also
28 * add yours to the list at the bottom of vport.h. */
29 static const struct vport_ops *base_vport_ops_list[] = {
34 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
39 static const struct vport_ops **vport_ops_list;
40 static int n_vport_types;
42 /* Protected by RCU read lock for reading, RTNL lock for writing. */
43 static struct hlist_head *dev_table;
44 #define VPORT_HASH_BUCKETS 1024
47 * vport_init - initialize vport subsystem
49 * Called at module load time to initialize the vport subsystem and any
50 * compiled in vport types.
57 dev_table = kzalloc(VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
64 vport_ops_list = kmalloc(ARRAY_SIZE(base_vport_ops_list) *
65 sizeof(struct vport_ops *), GFP_KERNEL);
66 if (!vport_ops_list) {
71 for (i = 0; i < ARRAY_SIZE(base_vport_ops_list); i++) {
72 const struct vport_ops *new_ops = base_vport_ops_list[i];
75 err = new_ops->init();
80 vport_ops_list[n_vport_types++] = new_ops;
81 else if (new_ops->flags & VPORT_F_REQUIRED) {
96 * vport_exit - shutdown vport subsystem
98 * Called at module exit time to shutdown the vport subsystem and any
99 * initialized vport types.
101 void vport_exit(void)
105 for (i = 0; i < n_vport_types; i++) {
106 if (vport_ops_list[i]->exit)
107 vport_ops_list[i]->exit();
110 kfree(vport_ops_list);
114 static struct hlist_head *hash_bucket(const char *name)
116 unsigned int hash = full_name_hash(name, strlen(name));
117 return &dev_table[hash & (VPORT_HASH_BUCKETS - 1)];
121 * vport_locate - find a port that has already been created
123 * @name: name of port to find
125 * Must be called with RTNL or RCU read lock.
127 struct vport *vport_locate(const char *name)
129 struct hlist_head *bucket = hash_bucket(name);
131 struct hlist_node *node;
133 hlist_for_each_entry_rcu(vport, node, bucket, hash_node)
134 if (!strcmp(name, vport_get_name(vport)))
140 static void release_vport(struct kobject *kobj)
142 struct vport *p = container_of(kobj, struct vport, kobj);
146 static struct kobj_type brport_ktype = {
148 .sysfs_ops = &brport_sysfs_ops,
150 .release = release_vport
154 * vport_alloc - allocate and initialize new vport
156 * @priv_size: Size of private data area to allocate.
157 * @ops: vport device ops
159 * Allocate and initialize a new vport defined by @ops. The vport will contain
160 * a private data area of size @priv_size that can be accessed using
161 * vport_priv(). vports that are no longer needed should be released with
164 struct vport *vport_alloc(int priv_size, const struct vport_ops *ops, const struct vport_parms *parms)
169 alloc_size = sizeof(struct vport);
171 alloc_size = ALIGN(alloc_size, VPORT_ALIGN);
172 alloc_size += priv_size;
175 vport = kzalloc(alloc_size, GFP_KERNEL);
177 return ERR_PTR(-ENOMEM);
179 vport->dp = parms->dp;
180 vport->port_no = parms->port_no;
181 atomic_set(&vport->sflow_pool, 0);
184 /* Initialize kobject for bridge. This will be added as
185 * /sys/class/net/<devname>/brport later, if sysfs is enabled. */
186 vport->kobj.kset = NULL;
187 kobject_init(&vport->kobj, &brport_ktype);
189 if (vport->ops->flags & VPORT_F_GEN_STATS) {
190 vport->percpu_stats = alloc_percpu(struct vport_percpu_stats);
191 if (!vport->percpu_stats)
192 return ERR_PTR(-ENOMEM);
194 spin_lock_init(&vport->stats_lock);
201 * vport_free - uninitialize and free vport
203 * @vport: vport to free
205 * Frees a vport allocated with vport_alloc() when it is no longer needed.
207 * The caller must ensure that an RCU grace period has passed since the last
208 * time @vport was in a datapath.
210 void vport_free(struct vport *vport)
212 if (vport->ops->flags & VPORT_F_GEN_STATS)
213 free_percpu(vport->percpu_stats);
215 kobject_put(&vport->kobj);
219 * vport_add - add vport device (for kernel callers)
221 * @parms: Information about new vport.
223 * Creates a new vport with the specified configuration (which is dependent on
224 * device type) and attaches it to a datapath. RTNL lock must be held.
226 struct vport *vport_add(const struct vport_parms *parms)
234 for (i = 0; i < n_vport_types; i++) {
235 if (vport_ops_list[i]->type == parms->type) {
236 vport = vport_ops_list[i]->create(parms);
238 err = PTR_ERR(vport);
242 hlist_add_head_rcu(&vport->hash_node,
243 hash_bucket(vport_get_name(vport)));
255 * vport_set_options - modify existing vport device (for kernel callers)
257 * @vport: vport to modify.
258 * @port: New configuration.
260 * Modifies an existing device with the specified configuration (which is
261 * dependent on device type). RTNL lock must be held.
263 int vport_set_options(struct vport *vport, struct nlattr *options)
267 if (!vport->ops->set_options)
269 return vport->ops->set_options(vport, options);
273 * vport_del - delete existing vport device (for kernel callers)
275 * @vport: vport to delete.
277 * Detaches @vport from its datapath and destroys it. It is possible to fail
278 * for reasons such as lack of memory. RTNL lock must be held.
280 int vport_del(struct vport *vport)
284 hlist_del_rcu(&vport->hash_node);
286 return vport->ops->destroy(vport);
290 * vport_set_mtu - set device MTU (for kernel callers)
292 * @vport: vport on which to set MTU.
295 * Sets the MTU of the given device. Some devices may not support setting the
296 * MTU, in which case the result will always be -EOPNOTSUPP. RTNL lock must
299 int vport_set_mtu(struct vport *vport, int mtu)
306 if (vport->ops->set_mtu) {
309 ret = vport->ops->set_mtu(vport, mtu);
311 if (!ret && !is_internal_vport(vport))
312 set_internal_devs_mtu(vport->dp);
320 * vport_set_addr - set device Ethernet address (for kernel callers)
322 * @vport: vport on which to set Ethernet address.
323 * @addr: New address.
325 * Sets the Ethernet address of the given device. Some devices may not support
326 * setting the Ethernet address, in which case the result will always be
327 * -EOPNOTSUPP. RTNL lock must be held.
329 int vport_set_addr(struct vport *vport, const unsigned char *addr)
333 if (!is_valid_ether_addr(addr))
334 return -EADDRNOTAVAIL;
336 if (vport->ops->set_addr)
337 return vport->ops->set_addr(vport, addr);
343 * vport_set_stats - sets offset device stats (for kernel callers)
345 * @vport: vport on which to set stats
346 * @stats: stats to set
348 * Provides a set of transmit, receive, and error stats to be added as an
349 * offset to the collect data when stats are retreived. Some devices may not
350 * support setting the stats, in which case the result will always be
351 * -EOPNOTSUPP. RTNL lock must be held.
353 int vport_set_stats(struct vport *vport, struct rtnl_link_stats64 *stats)
357 if (vport->ops->flags & VPORT_F_GEN_STATS) {
358 spin_lock_bh(&vport->stats_lock);
359 vport->offset_stats = *stats;
360 spin_unlock_bh(&vport->stats_lock);
368 * vport_get_name - retrieve device name
370 * @vport: vport from which to retrieve the name.
372 * Retrieves the name of the given device. Either RTNL lock or rcu_read_lock
373 * must be held for the entire duration that the name is in use.
375 const char *vport_get_name(const struct vport *vport)
377 return vport->ops->get_name(vport);
381 * vport_get_type - retrieve device type
383 * @vport: vport from which to retrieve the type.
385 * Retrieves the type of the given device. Either RTNL lock or rcu_read_lock
388 enum odp_vport_type vport_get_type(const struct vport *vport)
390 return vport->ops->type;
394 * vport_get_addr - retrieve device Ethernet address (for kernel callers)
396 * @vport: vport from which to retrieve the Ethernet address.
398 * Retrieves the Ethernet address of the given device. Either RTNL lock or
399 * rcu_read_lock must be held for the entire duration that the Ethernet address
402 const unsigned char *vport_get_addr(const struct vport *vport)
404 return vport->ops->get_addr(vport);
408 * vport_get_kobj - retrieve associated kobj
410 * @vport: vport from which to retrieve the associated kobj
412 * Retrieves the associated kobj or null if no kobj. The returned kobj is
413 * valid for as long as the vport exists.
415 struct kobject *vport_get_kobj(const struct vport *vport)
417 if (vport->ops->get_kobj)
418 return vport->ops->get_kobj(vport);
423 static int vport_call_get_stats(struct vport *vport, struct rtnl_link_stats64 *stats)
428 err = vport->ops->get_stats(vport, stats);
435 * vport_get_stats - retrieve device stats (for kernel callers)
437 * @vport: vport from which to retrieve the stats
438 * @stats: location to store stats
440 * Retrieves transmit, receive, and error stats for the given device.
442 int vport_get_stats(struct vport *vport, struct rtnl_link_stats64 *stats)
446 if (!(vport->ops->flags & VPORT_F_GEN_STATS))
447 return vport_call_get_stats(vport, stats);
449 /* We potentially have 3 sources of stats that need to be
450 * combined: those we have collected (split into err_stats and
451 * percpu_stats), offset_stats from set_stats(), and device
452 * error stats from get_stats() (for errors that happen
453 * downstream and therefore aren't reported through our
454 * vport_record_error() function). */
456 spin_lock_bh(&vport->stats_lock);
458 *stats = vport->offset_stats;
460 stats->rx_errors += vport->err_stats.rx_errors;
461 stats->tx_errors += vport->err_stats.tx_errors;
462 stats->tx_dropped += vport->err_stats.tx_dropped;
463 stats->rx_dropped += vport->err_stats.rx_dropped;
465 spin_unlock_bh(&vport->stats_lock);
467 if (vport->ops->get_stats) {
468 struct rtnl_link_stats64 dev_stats;
471 err = vport_call_get_stats(vport, &dev_stats);
475 stats->rx_errors += dev_stats.rx_errors;
476 stats->tx_errors += dev_stats.tx_errors;
477 stats->rx_dropped += dev_stats.rx_dropped;
478 stats->tx_dropped += dev_stats.tx_dropped;
479 stats->multicast += dev_stats.multicast;
480 stats->collisions += dev_stats.collisions;
481 stats->rx_length_errors += dev_stats.rx_length_errors;
482 stats->rx_over_errors += dev_stats.rx_over_errors;
483 stats->rx_crc_errors += dev_stats.rx_crc_errors;
484 stats->rx_frame_errors += dev_stats.rx_frame_errors;
485 stats->rx_fifo_errors += dev_stats.rx_fifo_errors;
486 stats->rx_missed_errors += dev_stats.rx_missed_errors;
487 stats->tx_aborted_errors += dev_stats.tx_aborted_errors;
488 stats->tx_carrier_errors += dev_stats.tx_carrier_errors;
489 stats->tx_fifo_errors += dev_stats.tx_fifo_errors;
490 stats->tx_heartbeat_errors += dev_stats.tx_heartbeat_errors;
491 stats->tx_window_errors += dev_stats.tx_window_errors;
492 stats->rx_compressed += dev_stats.rx_compressed;
493 stats->tx_compressed += dev_stats.tx_compressed;
496 for_each_possible_cpu(i) {
497 const struct vport_percpu_stats *percpu_stats;
498 struct vport_percpu_stats local_stats;
501 percpu_stats = per_cpu_ptr(vport->percpu_stats, i);
504 seqcount = read_seqcount_begin(&percpu_stats->seqlock);
505 local_stats = *percpu_stats;
506 } while (read_seqcount_retry(&percpu_stats->seqlock, seqcount));
508 stats->rx_bytes += local_stats.rx_bytes;
509 stats->rx_packets += local_stats.rx_packets;
510 stats->tx_bytes += local_stats.tx_bytes;
511 stats->tx_packets += local_stats.tx_packets;
518 * vport_get_flags - retrieve device flags
520 * @vport: vport from which to retrieve the flags
522 * Retrieves the flags of the given device. Either RTNL lock or rcu_read_lock
525 unsigned vport_get_flags(const struct vport *vport)
527 return vport->ops->get_dev_flags(vport);
531 * vport_get_flags - check whether device is running
533 * @vport: vport on which to check status.
535 * Checks whether the given device is running. Either RTNL lock or
536 * rcu_read_lock must be held.
538 int vport_is_running(const struct vport *vport)
540 return vport->ops->is_running(vport);
544 * vport_get_flags - retrieve device operating state
546 * @vport: vport from which to check status
548 * Retrieves the RFC2863 operstate of the given device. Either RTNL lock or
549 * rcu_read_lock must be held.
551 unsigned char vport_get_operstate(const struct vport *vport)
553 return vport->ops->get_operstate(vport);
557 * vport_get_ifindex - retrieve device system interface index
559 * @vport: vport from which to retrieve index
561 * Retrieves the system interface index of the given device or 0 if
562 * the device does not have one (in the case of virtual ports).
563 * Returns a negative index on error. Either RTNL lock or
564 * rcu_read_lock must be held.
566 int vport_get_ifindex(const struct vport *vport)
568 if (vport->ops->get_ifindex)
569 return vport->ops->get_ifindex(vport);
575 * vport_get_iflink - retrieve device system link index
577 * @vport: vport from which to retrieve index
579 * Retrieves the system link index of the given device. The link is the index
580 * of the interface on which the packet will actually be sent. In most cases
581 * this is the same as the ifindex but may be different for tunnel devices.
582 * Returns a negative index on error. Either RTNL lock or rcu_read_lock must
585 int vport_get_iflink(const struct vport *vport)
587 if (vport->ops->get_iflink)
588 return vport->ops->get_iflink(vport);
590 /* If we don't have an iflink, use the ifindex. In most cases they
592 return vport_get_ifindex(vport);
596 * vport_get_mtu - retrieve device MTU (for kernel callers)
598 * @vport: vport from which to retrieve MTU
600 * Retrieves the MTU of the given device. Either RTNL lock or rcu_read_lock
603 int vport_get_mtu(const struct vport *vport)
605 return vport->ops->get_mtu(vport);
609 * vport_get_options - retrieve device options
611 * @vport: vport from which to retrieve the options.
612 * @skb: sk_buff where options should be appended.
614 * Retrieves the configuration of the given device, appending an
615 * %ODP_VPORT_ATTR_OPTIONS attribute that in turn contains nested
616 * vport-specific attributes to @skb. Either RTNL lock or rcu_read_lock must
619 * Returns 0 if successful, -EMSGSIZE if @skb has insufficient room, or another
620 * negative error code if a real error occurred.
622 int vport_get_options(const struct vport *vport, struct sk_buff *skb)
626 nla = nla_nest_start(skb, ODP_VPORT_ATTR_OPTIONS);
630 if (vport->ops->get_options) {
631 int err = vport->ops->get_options(vport, skb);
636 nla_nest_end(skb, nla);
641 * vport_receive - pass up received packet to the datapath for processing
643 * @vport: vport that received the packet
644 * @skb: skb that was received
646 * Must be called with rcu_read_lock. The packet cannot be shared and
647 * skb->data should point to the Ethernet header. The caller must have already
648 * called compute_ip_summed() to initialize the checksumming fields.
650 void vport_receive(struct vport *vport, struct sk_buff *skb)
652 if (vport->ops->flags & VPORT_F_GEN_STATS) {
653 struct vport_percpu_stats *stats;
656 stats = per_cpu_ptr(vport->percpu_stats, smp_processor_id());
658 write_seqcount_begin(&stats->seqlock);
660 stats->rx_bytes += skb->len;
661 write_seqcount_end(&stats->seqlock);
666 if (!(vport->ops->flags & VPORT_F_FLOW))
667 OVS_CB(skb)->flow = NULL;
669 if (!(vport->ops->flags & VPORT_F_TUN_ID))
670 OVS_CB(skb)->tun_id = 0;
672 dp_process_received_packet(vport, skb);
675 static inline unsigned packet_length(const struct sk_buff *skb)
677 unsigned length = skb->len - ETH_HLEN;
679 if (skb->protocol == htons(ETH_P_8021Q))
686 * vport_send - send a packet on a device
688 * @vport: vport on which to send the packet
691 * Sends the given packet and returns the length of data sent. Either RTNL
692 * lock or rcu_read_lock must be held.
694 int vport_send(struct vport *vport, struct sk_buff *skb)
699 mtu = vport_get_mtu(vport);
700 if (unlikely(packet_length(skb) > mtu && !skb_is_gso(skb))) {
702 pr_warn("%s: dropped over-mtu packet: %d > %d\n",
703 dp_name(vport->dp), packet_length(skb), mtu);
707 sent = vport->ops->send(vport, skb);
709 if (vport->ops->flags & VPORT_F_GEN_STATS && sent > 0) {
710 struct vport_percpu_stats *stats;
713 stats = per_cpu_ptr(vport->percpu_stats, smp_processor_id());
715 write_seqcount_begin(&stats->seqlock);
717 stats->tx_bytes += sent;
718 write_seqcount_end(&stats->seqlock);
727 vport_record_error(vport, VPORT_E_TX_DROPPED);
732 * vport_record_error - indicate device error to generic stats layer
734 * @vport: vport that encountered the error
735 * @err_type: one of enum vport_err_type types to indicate the error type
737 * If using the vport generic stats layer indicate that an error of the given
740 void vport_record_error(struct vport *vport, enum vport_err_type err_type)
742 if (vport->ops->flags & VPORT_F_GEN_STATS) {
744 spin_lock_bh(&vport->stats_lock);
747 case VPORT_E_RX_DROPPED:
748 vport->err_stats.rx_dropped++;
751 case VPORT_E_RX_ERROR:
752 vport->err_stats.rx_errors++;
755 case VPORT_E_TX_DROPPED:
756 vport->err_stats.tx_dropped++;
759 case VPORT_E_TX_ERROR:
760 vport->err_stats.tx_errors++;
764 spin_unlock_bh(&vport->stats_lock);