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 #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/rcupdate.h>
18 #include <linux/rtnetlink.h>
19 #include <linux/compat.h>
20 #include <linux/version.h>
23 #include "vport-internal_dev.h"
25 /* List of statically compiled vport implementations. Don't forget to also
26 * add yours to the list at the bottom of vport.h. */
27 static const struct vport_ops *base_vport_ops_list[] = {
32 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,26)
37 static const struct vport_ops **vport_ops_list;
38 static int n_vport_types;
40 /* Protected by RCU read lock for reading, RTNL lock for writing. */
41 static struct hlist_head *dev_table;
42 #define VPORT_HASH_BUCKETS 1024
45 * vport_init - initialize vport subsystem
47 * Called at module load time to initialize the vport subsystem and any
48 * compiled in vport types.
55 dev_table = kzalloc(VPORT_HASH_BUCKETS * sizeof(struct hlist_head),
62 vport_ops_list = kmalloc(ARRAY_SIZE(base_vport_ops_list) *
63 sizeof(struct vport_ops *), GFP_KERNEL);
64 if (!vport_ops_list) {
69 for (i = 0; i < ARRAY_SIZE(base_vport_ops_list); i++) {
70 const struct vport_ops *new_ops = base_vport_ops_list[i];
73 err = new_ops->init();
78 vport_ops_list[n_vport_types++] = new_ops;
79 else if (new_ops->flags & VPORT_F_REQUIRED) {
94 * vport_exit - shutdown vport subsystem
96 * Called at module exit time to shutdown the vport subsystem and any
97 * initialized vport types.
103 for (i = 0; i < n_vport_types; i++) {
104 if (vport_ops_list[i]->exit)
105 vport_ops_list[i]->exit();
108 kfree(vport_ops_list);
112 static struct hlist_head *hash_bucket(const char *name)
114 unsigned int hash = full_name_hash(name, strlen(name));
115 return &dev_table[hash & (VPORT_HASH_BUCKETS - 1)];
119 * vport_locate - find a port that has already been created
121 * @name: name of port to find
123 * Must be called with RTNL or RCU read lock.
125 struct vport *vport_locate(const char *name)
127 struct hlist_head *bucket = hash_bucket(name);
129 struct hlist_node *node;
131 hlist_for_each_entry_rcu(vport, node, bucket, hash_node)
132 if (!strcmp(name, vport_get_name(vport)))
138 static void release_vport(struct kobject *kobj)
140 struct vport *p = container_of(kobj, struct vport, kobj);
144 static struct kobj_type brport_ktype = {
146 .sysfs_ops = &brport_sysfs_ops,
148 .release = release_vport
152 * vport_alloc - allocate and initialize new vport
154 * @priv_size: Size of private data area to allocate.
155 * @ops: vport device ops
157 * Allocate and initialize a new vport defined by @ops. The vport will contain
158 * a private data area of size @priv_size that can be accessed using
159 * vport_priv(). vports that are no longer needed should be released with
162 struct vport *vport_alloc(int priv_size, const struct vport_ops *ops, const struct vport_parms *parms)
167 alloc_size = sizeof(struct vport);
169 alloc_size = ALIGN(alloc_size, VPORT_ALIGN);
170 alloc_size += priv_size;
173 vport = kzalloc(alloc_size, GFP_KERNEL);
175 return ERR_PTR(-ENOMEM);
177 vport->dp = parms->dp;
178 vport->port_no = parms->port_no;
179 atomic_set(&vport->sflow_pool, 0);
182 /* Initialize kobject for bridge. This will be added as
183 * /sys/class/net/<devname>/brport later, if sysfs is enabled. */
184 vport->kobj.kset = NULL;
185 kobject_init(&vport->kobj, &brport_ktype);
187 if (vport->ops->flags & VPORT_F_GEN_STATS) {
188 vport->percpu_stats = alloc_percpu(struct vport_percpu_stats);
189 if (!vport->percpu_stats)
190 return ERR_PTR(-ENOMEM);
192 spin_lock_init(&vport->stats_lock);
199 * vport_free - uninitialize and free vport
201 * @vport: vport to free
203 * Frees a vport allocated with vport_alloc() when it is no longer needed.
205 * The caller must ensure that an RCU grace period has passed since the last
206 * time @vport was in a datapath.
208 void vport_free(struct vport *vport)
210 if (vport->ops->flags & VPORT_F_GEN_STATS)
211 free_percpu(vport->percpu_stats);
213 kobject_put(&vport->kobj);
217 * vport_add - add vport device (for kernel callers)
219 * @parms: Information about new vport.
221 * Creates a new vport with the specified configuration (which is dependent on
222 * device type). RTNL lock must be held.
224 struct vport *vport_add(const struct vport_parms *parms)
232 for (i = 0; i < n_vport_types; i++) {
233 if (vport_ops_list[i]->type == parms->type) {
234 vport = vport_ops_list[i]->create(parms);
236 err = PTR_ERR(vport);
240 hlist_add_head_rcu(&vport->hash_node,
241 hash_bucket(vport_get_name(vport)));
253 * vport_set_options - modify existing vport device (for kernel callers)
255 * @vport: vport to modify.
256 * @port: New configuration.
258 * Modifies an existing device with the specified configuration (which is
259 * dependent on device type). RTNL lock must be held.
261 int vport_set_options(struct vport *vport, struct nlattr *options)
265 if (!vport->ops->set_options)
267 return vport->ops->set_options(vport, options);
271 * vport_del - delete existing vport device
273 * @vport: vport to delete.
275 * Detaches @vport from its datapath and destroys it. It is possible to fail
276 * for reasons such as lack of memory. RTNL lock must be held.
278 void vport_del(struct vport *vport)
282 hlist_del_rcu(&vport->hash_node);
284 vport->ops->destroy(vport);
288 * vport_set_addr - set device Ethernet address (for kernel callers)
290 * @vport: vport on which to set Ethernet address.
291 * @addr: New address.
293 * Sets the Ethernet address of the given device. Some devices may not support
294 * setting the Ethernet address, in which case the result will always be
295 * -EOPNOTSUPP. RTNL lock must be held.
297 int vport_set_addr(struct vport *vport, const unsigned char *addr)
301 if (!is_valid_ether_addr(addr))
302 return -EADDRNOTAVAIL;
304 if (vport->ops->set_addr)
305 return vport->ops->set_addr(vport, addr);
311 * vport_set_stats - sets offset device stats
313 * @vport: vport on which to set stats
314 * @stats: stats to set
316 * Provides a set of transmit, receive, and error stats to be added as an
317 * offset to the collect data when stats are retreived. Some devices may not
318 * support setting the stats, in which case the result will always be
321 * Must be called with RTNL lock.
323 int vport_set_stats(struct vport *vport, struct rtnl_link_stats64 *stats)
327 if (vport->ops->flags & VPORT_F_GEN_STATS) {
328 spin_lock_bh(&vport->stats_lock);
329 vport->offset_stats = *stats;
330 spin_unlock_bh(&vport->stats_lock);
338 * vport_get_name - retrieve device name
340 * @vport: vport from which to retrieve the name.
342 * Retrieves the name of the given device. Either RTNL lock or rcu_read_lock
343 * must be held for the entire duration that the name is in use.
345 const char *vport_get_name(const struct vport *vport)
347 return vport->ops->get_name(vport);
351 * vport_get_type - retrieve device type
353 * @vport: vport from which to retrieve the type.
355 * Retrieves the type of the given device.
357 enum ovs_vport_type vport_get_type(const struct vport *vport)
359 return vport->ops->type;
363 * vport_get_addr - retrieve device Ethernet address (for kernel callers)
365 * @vport: vport from which to retrieve the Ethernet address.
367 * Retrieves the Ethernet address of the given device. Either RTNL lock or
368 * rcu_read_lock must be held for the entire duration that the Ethernet address
371 const unsigned char *vport_get_addr(const struct vport *vport)
373 return vport->ops->get_addr(vport);
377 * vport_get_kobj - retrieve associated kobj
379 * @vport: vport from which to retrieve the associated kobj
381 * Retrieves the associated kobj or null if no kobj. The returned kobj is
382 * valid for as long as the vport exists.
384 struct kobject *vport_get_kobj(const struct vport *vport)
386 if (vport->ops->get_kobj)
387 return vport->ops->get_kobj(vport);
392 static int vport_call_get_stats(struct vport *vport, struct rtnl_link_stats64 *stats)
397 err = vport->ops->get_stats(vport, stats);
404 * vport_get_stats - retrieve device stats
406 * @vport: vport from which to retrieve the stats
407 * @stats: location to store stats
409 * Retrieves transmit, receive, and error stats for the given device.
411 * Must be called with RTNL lock or rcu_read_lock.
413 int vport_get_stats(struct vport *vport, struct rtnl_link_stats64 *stats)
417 if (!(vport->ops->flags & VPORT_F_GEN_STATS))
418 return vport_call_get_stats(vport, stats);
420 /* We potentially have 3 sources of stats that need to be
421 * combined: those we have collected (split into err_stats and
422 * percpu_stats), offset_stats from set_stats(), and device
423 * error stats from get_stats() (for errors that happen
424 * downstream and therefore aren't reported through our
425 * vport_record_error() function). */
427 spin_lock_bh(&vport->stats_lock);
429 *stats = vport->offset_stats;
431 stats->rx_errors += vport->err_stats.rx_errors;
432 stats->tx_errors += vport->err_stats.tx_errors;
433 stats->tx_dropped += vport->err_stats.tx_dropped;
434 stats->rx_dropped += vport->err_stats.rx_dropped;
436 spin_unlock_bh(&vport->stats_lock);
438 if (vport->ops->get_stats) {
439 struct rtnl_link_stats64 dev_stats;
442 err = vport_call_get_stats(vport, &dev_stats);
446 stats->rx_errors += dev_stats.rx_errors;
447 stats->tx_errors += dev_stats.tx_errors;
448 stats->rx_dropped += dev_stats.rx_dropped;
449 stats->tx_dropped += dev_stats.tx_dropped;
450 stats->multicast += dev_stats.multicast;
451 stats->collisions += dev_stats.collisions;
452 stats->rx_length_errors += dev_stats.rx_length_errors;
453 stats->rx_over_errors += dev_stats.rx_over_errors;
454 stats->rx_crc_errors += dev_stats.rx_crc_errors;
455 stats->rx_frame_errors += dev_stats.rx_frame_errors;
456 stats->rx_fifo_errors += dev_stats.rx_fifo_errors;
457 stats->rx_missed_errors += dev_stats.rx_missed_errors;
458 stats->tx_aborted_errors += dev_stats.tx_aborted_errors;
459 stats->tx_carrier_errors += dev_stats.tx_carrier_errors;
460 stats->tx_fifo_errors += dev_stats.tx_fifo_errors;
461 stats->tx_heartbeat_errors += dev_stats.tx_heartbeat_errors;
462 stats->tx_window_errors += dev_stats.tx_window_errors;
463 stats->rx_compressed += dev_stats.rx_compressed;
464 stats->tx_compressed += dev_stats.tx_compressed;
467 for_each_possible_cpu(i) {
468 const struct vport_percpu_stats *percpu_stats;
469 struct vport_percpu_stats local_stats;
472 percpu_stats = per_cpu_ptr(vport->percpu_stats, i);
475 seqcount = read_seqcount_begin(&percpu_stats->seqlock);
476 local_stats = *percpu_stats;
477 } while (read_seqcount_retry(&percpu_stats->seqlock, seqcount));
479 stats->rx_bytes += local_stats.rx_bytes;
480 stats->rx_packets += local_stats.rx_packets;
481 stats->tx_bytes += local_stats.tx_bytes;
482 stats->tx_packets += local_stats.tx_packets;
489 * vport_get_flags - retrieve device flags
491 * @vport: vport from which to retrieve the flags
493 * Retrieves the flags of the given device.
495 * Must be called with RTNL lock or rcu_read_lock.
497 unsigned vport_get_flags(const struct vport *vport)
499 return vport->ops->get_dev_flags(vport);
503 * vport_get_flags - check whether device is running
505 * @vport: vport on which to check status.
507 * Checks whether the given device is running.
509 * Must be called with RTNL lock or rcu_read_lock.
511 int vport_is_running(const struct vport *vport)
513 return vport->ops->is_running(vport);
517 * vport_get_flags - retrieve device operating state
519 * @vport: vport from which to check status
521 * Retrieves the RFC2863 operstate of the given device.
523 * Must be called with RTNL lock or rcu_read_lock.
525 unsigned char vport_get_operstate(const struct vport *vport)
527 return vport->ops->get_operstate(vport);
531 * vport_get_ifindex - retrieve device system interface index
533 * @vport: vport from which to retrieve index
535 * Retrieves the system interface index of the given device or 0 if
536 * the device does not have one (in the case of virtual ports).
537 * Returns a negative index on error.
539 * Must be called with RTNL lock or rcu_read_lock.
541 int vport_get_ifindex(const struct vport *vport)
543 if (vport->ops->get_ifindex)
544 return vport->ops->get_ifindex(vport);
550 * vport_get_mtu - retrieve device MTU
552 * @vport: vport from which to retrieve MTU
554 * Retrieves the MTU of the given device. Returns 0 if @vport does not have an
555 * MTU (as e.g. some tunnels do not). Either RTNL lock or rcu_read_lock must
558 int vport_get_mtu(const struct vport *vport)
560 if (!vport->ops->get_mtu)
562 return vport->ops->get_mtu(vport);
566 * vport_get_options - retrieve device options
568 * @vport: vport from which to retrieve the options.
569 * @skb: sk_buff where options should be appended.
571 * Retrieves the configuration of the given device, appending an
572 * %OVS_VPORT_ATTR_OPTIONS attribute that in turn contains nested
573 * vport-specific attributes to @skb.
575 * Returns 0 if successful, -EMSGSIZE if @skb has insufficient room, or another
576 * negative error code if a real error occurred. If an error occurs, @skb is
579 * Must be called with RTNL lock or rcu_read_lock.
581 int vport_get_options(const struct vport *vport, struct sk_buff *skb)
585 nla = nla_nest_start(skb, OVS_VPORT_ATTR_OPTIONS);
589 if (vport->ops->get_options) {
590 int err = vport->ops->get_options(vport, skb);
592 nla_nest_cancel(skb, nla);
597 nla_nest_end(skb, nla);
602 * vport_receive - pass up received packet to the datapath for processing
604 * @vport: vport that received the packet
605 * @skb: skb that was received
607 * Must be called with rcu_read_lock. The packet cannot be shared and
608 * skb->data should point to the Ethernet header. The caller must have already
609 * called compute_ip_summed() to initialize the checksumming fields.
611 void vport_receive(struct vport *vport, struct sk_buff *skb)
613 if (vport->ops->flags & VPORT_F_GEN_STATS) {
614 struct vport_percpu_stats *stats;
617 stats = per_cpu_ptr(vport->percpu_stats, smp_processor_id());
619 write_seqcount_begin(&stats->seqlock);
621 stats->rx_bytes += skb->len;
622 write_seqcount_end(&stats->seqlock);
627 if (!(vport->ops->flags & VPORT_F_FLOW))
628 OVS_CB(skb)->flow = NULL;
630 if (!(vport->ops->flags & VPORT_F_TUN_ID))
631 OVS_CB(skb)->tun_id = 0;
633 dp_process_received_packet(vport, skb);
637 * vport_send - send a packet on a device
639 * @vport: vport on which to send the packet
642 * Sends the given packet and returns the length of data sent. Either RTNL
643 * lock or rcu_read_lock must be held.
645 int vport_send(struct vport *vport, struct sk_buff *skb)
647 int sent = vport->ops->send(vport, skb);
649 if (vport->ops->flags & VPORT_F_GEN_STATS && sent > 0) {
650 struct vport_percpu_stats *stats;
653 stats = per_cpu_ptr(vport->percpu_stats, smp_processor_id());
655 write_seqcount_begin(&stats->seqlock);
657 stats->tx_bytes += sent;
658 write_seqcount_end(&stats->seqlock);
667 * vport_record_error - indicate device error to generic stats layer
669 * @vport: vport that encountered the error
670 * @err_type: one of enum vport_err_type types to indicate the error type
672 * If using the vport generic stats layer indicate that an error of the given
675 void vport_record_error(struct vport *vport, enum vport_err_type err_type)
677 if (vport->ops->flags & VPORT_F_GEN_STATS) {
679 spin_lock_bh(&vport->stats_lock);
682 case VPORT_E_RX_DROPPED:
683 vport->err_stats.rx_dropped++;
686 case VPORT_E_RX_ERROR:
687 vport->err_stats.rx_errors++;
690 case VPORT_E_TX_DROPPED:
691 vport->err_stats.tx_dropped++;
694 case VPORT_E_TX_ERROR:
695 vport->err_stats.tx_errors++;
699 spin_unlock_bh(&vport->stats_lock);