2006-04-19 12:07:46 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2 of the
|
|
|
|
* License, or any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdint.h>
|
2006-12-19 23:42:46 +00:00
|
|
|
#include <stdlib.h>
|
2007-01-19 01:13:12 +00:00
|
|
|
#include <stdio.h>
|
2006-04-19 12:07:46 +00:00
|
|
|
#include <byteswap.h>
|
2006-04-24 15:33:06 +00:00
|
|
|
#include <string.h>
|
2006-04-19 12:07:46 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <gpxe/if_ether.h>
|
2007-05-19 18:39:40 +00:00
|
|
|
#include <gpxe/iobuf.h>
|
2006-04-24 15:33:06 +00:00
|
|
|
#include <gpxe/tables.h>
|
2006-04-29 17:17:43 +00:00
|
|
|
#include <gpxe/process.h>
|
2006-04-30 01:08:52 +00:00
|
|
|
#include <gpxe/init.h>
|
2007-01-10 04:22:09 +00:00
|
|
|
#include <gpxe/device.h>
|
2006-04-19 12:07:46 +00:00
|
|
|
#include <gpxe/netdevice.h>
|
|
|
|
|
|
|
|
/** @file
|
|
|
|
*
|
2006-04-24 15:33:06 +00:00
|
|
|
* Network device management
|
2006-04-19 12:07:46 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2006-04-24 15:33:06 +00:00
|
|
|
/** Registered network-layer protocols */
|
2007-01-10 04:22:09 +00:00
|
|
|
static struct net_protocol net_protocols[0]
|
|
|
|
__table_start ( struct net_protocol, net_protocols );
|
|
|
|
static struct net_protocol net_protocols_end[0]
|
|
|
|
__table_end ( struct net_protocol, net_protocols );
|
2006-04-24 15:33:06 +00:00
|
|
|
|
2006-06-17 22:36:27 +00:00
|
|
|
/** List of network devices */
|
2007-01-10 01:55:07 +00:00
|
|
|
struct list_head net_devices = LIST_HEAD_INIT ( net_devices );
|
2006-04-19 12:07:46 +00:00
|
|
|
|
2006-06-16 00:19:46 +00:00
|
|
|
/**
|
2006-06-17 22:36:27 +00:00
|
|
|
* Transmit raw packet via network device
|
2006-06-16 00:19:46 +00:00
|
|
|
*
|
|
|
|
* @v netdev Network device
|
2007-05-19 18:39:40 +00:00
|
|
|
* @v iobuf I/O buffer
|
2006-06-16 00:19:46 +00:00
|
|
|
* @ret rc Return status code
|
|
|
|
*
|
2006-06-17 22:36:27 +00:00
|
|
|
* Transmits the packet via the specified network device. This
|
2007-05-19 18:39:40 +00:00
|
|
|
* function takes ownership of the I/O buffer.
|
2006-06-16 00:19:46 +00:00
|
|
|
*/
|
2007-05-19 18:39:40 +00:00
|
|
|
int netdev_tx ( struct net_device *netdev, struct io_buffer *iobuf ) {
|
2007-01-09 20:18:31 +00:00
|
|
|
int rc;
|
|
|
|
|
|
|
|
DBGC ( netdev, "NETDEV %p transmitting %p (%p+%zx)\n",
|
2007-05-19 18:39:40 +00:00
|
|
|
netdev, iobuf, iobuf->data, iob_len ( iobuf ) );
|
2007-01-09 20:18:31 +00:00
|
|
|
|
2007-05-19 18:39:40 +00:00
|
|
|
list_add_tail ( &iobuf->list, &netdev->tx_queue );
|
2007-01-04 04:20:08 +00:00
|
|
|
|
|
|
|
if ( ! ( netdev->state & NETDEV_OPEN ) ) {
|
2007-01-09 20:18:31 +00:00
|
|
|
rc = -ENETUNREACH;
|
|
|
|
goto err;
|
2007-01-04 04:20:08 +00:00
|
|
|
}
|
2007-01-09 20:18:31 +00:00
|
|
|
|
2007-07-07 15:43:39 +00:00
|
|
|
if ( ( rc = netdev->op->transmit ( netdev, iobuf ) ) != 0 )
|
2007-01-09 20:18:31 +00:00
|
|
|
goto err;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
err:
|
2007-07-05 16:18:27 +00:00
|
|
|
netdev_tx_complete_err ( netdev, iobuf, rc );
|
2007-01-09 20:18:31 +00:00
|
|
|
return rc;
|
2006-06-16 00:19:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2007-01-09 20:18:31 +00:00
|
|
|
* Complete network transmission
|
2006-06-16 00:19:46 +00:00
|
|
|
*
|
|
|
|
* @v netdev Network device
|
2007-05-19 18:39:40 +00:00
|
|
|
* @v iobuf I/O buffer
|
2007-07-05 16:18:27 +00:00
|
|
|
* @v rc Packet status code
|
2006-06-16 00:19:46 +00:00
|
|
|
*
|
2007-01-09 20:18:31 +00:00
|
|
|
* The packet must currently be in the network device's TX queue.
|
2006-06-16 00:19:46 +00:00
|
|
|
*/
|
2007-07-05 16:18:27 +00:00
|
|
|
void netdev_tx_complete_err ( struct net_device *netdev,
|
|
|
|
struct io_buffer *iobuf, int rc ) {
|
|
|
|
|
|
|
|
/* Update statistics counter */
|
|
|
|
if ( rc == 0 ) {
|
|
|
|
netdev->stats.tx_ok++;
|
|
|
|
DBGC ( netdev, "NETDEV %p transmission %p complete\n",
|
|
|
|
netdev, iobuf );
|
|
|
|
} else {
|
|
|
|
netdev->stats.tx_err++;
|
|
|
|
DBGC ( netdev, "NETDEV %p transmission %p failed: %s\n",
|
|
|
|
netdev, iobuf, strerror ( rc ) );
|
|
|
|
}
|
2007-01-09 20:18:31 +00:00
|
|
|
|
2007-01-11 05:27:02 +00:00
|
|
|
/* Catch data corruption as early as possible */
|
2007-05-19 18:39:40 +00:00
|
|
|
assert ( iobuf->list.next != NULL );
|
|
|
|
assert ( iobuf->list.prev != NULL );
|
2007-01-11 05:27:02 +00:00
|
|
|
|
2007-07-02 23:15:53 +00:00
|
|
|
/* Dequeue and free I/O buffer */
|
2007-05-19 18:39:40 +00:00
|
|
|
list_del ( &iobuf->list );
|
|
|
|
free_iob ( iobuf );
|
2006-06-16 00:19:46 +00:00
|
|
|
}
|
|
|
|
|
2006-04-28 14:13:50 +00:00
|
|
|
/**
|
2007-01-09 20:18:31 +00:00
|
|
|
* Complete network transmission
|
2006-04-28 14:13:50 +00:00
|
|
|
*
|
2006-06-17 22:36:27 +00:00
|
|
|
* @v netdev Network device
|
2007-07-05 16:18:27 +00:00
|
|
|
* @v rc Packet status code
|
2006-04-28 14:13:50 +00:00
|
|
|
*
|
2007-01-09 20:18:31 +00:00
|
|
|
* Completes the oldest outstanding packet in the TX queue.
|
2006-04-28 14:13:50 +00:00
|
|
|
*/
|
2007-07-05 16:18:27 +00:00
|
|
|
void netdev_tx_complete_next_err ( struct net_device *netdev, int rc ) {
|
2007-05-19 18:39:40 +00:00
|
|
|
struct io_buffer *iobuf;
|
2007-01-09 20:18:31 +00:00
|
|
|
|
2007-05-19 18:39:40 +00:00
|
|
|
list_for_each_entry ( iobuf, &netdev->tx_queue, list ) {
|
2007-07-05 16:18:27 +00:00
|
|
|
netdev_tx_complete_err ( netdev, iobuf, rc );
|
2007-01-09 20:18:31 +00:00
|
|
|
return;
|
|
|
|
}
|
2006-04-28 14:13:50 +00:00
|
|
|
}
|
|
|
|
|
2007-06-27 13:48:31 +00:00
|
|
|
/**
|
|
|
|
* Flush device's transmit queue
|
|
|
|
*
|
|
|
|
* @v netdev Network device
|
|
|
|
*/
|
|
|
|
static void netdev_tx_flush ( struct net_device *netdev ) {
|
|
|
|
|
|
|
|
/* Discard any packets in the TX queue */
|
|
|
|
while ( ! list_empty ( &netdev->tx_queue ) ) {
|
2007-07-05 16:18:27 +00:00
|
|
|
netdev_tx_complete_next_err ( netdev, -ECANCELED );
|
2007-06-27 13:48:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-04-19 12:07:46 +00:00
|
|
|
/**
|
2007-01-09 20:18:31 +00:00
|
|
|
* Add packet to receive queue
|
2006-04-19 12:07:46 +00:00
|
|
|
*
|
2006-06-17 22:36:27 +00:00
|
|
|
* @v netdev Network device
|
2007-07-05 16:18:27 +00:00
|
|
|
* @v iobuf I/O buffer, or NULL
|
2007-01-09 20:18:31 +00:00
|
|
|
*
|
|
|
|
* The packet is added to the network device's RX queue. This
|
2007-05-19 18:39:40 +00:00
|
|
|
* function takes ownership of the I/O buffer.
|
2006-04-19 12:07:46 +00:00
|
|
|
*/
|
2007-05-19 18:39:40 +00:00
|
|
|
void netdev_rx ( struct net_device *netdev, struct io_buffer *iobuf ) {
|
2007-07-05 16:18:27 +00:00
|
|
|
|
2007-01-09 20:18:31 +00:00
|
|
|
DBGC ( netdev, "NETDEV %p received %p (%p+%zx)\n",
|
2007-05-19 18:39:40 +00:00
|
|
|
netdev, iobuf, iobuf->data, iob_len ( iobuf ) );
|
2007-07-02 23:15:53 +00:00
|
|
|
|
|
|
|
/* Enqueue packet */
|
2007-05-19 18:39:40 +00:00
|
|
|
list_add_tail ( &iobuf->list, &netdev->rx_queue );
|
2007-07-02 23:15:53 +00:00
|
|
|
|
|
|
|
/* Update statistics counter */
|
2007-07-05 16:18:27 +00:00
|
|
|
netdev->stats.rx_ok++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Discard received packet
|
|
|
|
*
|
|
|
|
* @v netdev Network device
|
|
|
|
* @v iobuf I/O buffer, or NULL
|
|
|
|
* @v rc Packet status code
|
|
|
|
*
|
|
|
|
* The packet is discarded and an RX error is recorded. This function
|
|
|
|
* takes ownership of the I/O buffer. @c iobuf may be NULL if, for
|
|
|
|
* example, the net device wishes to report an error due to being
|
|
|
|
* unable to allocate an I/O buffer.
|
|
|
|
*/
|
|
|
|
void netdev_rx_err ( struct net_device *netdev,
|
|
|
|
struct io_buffer *iobuf, int rc ) {
|
|
|
|
|
|
|
|
DBGC ( netdev, "NETDEV %p failed to receive %p: %s\n",
|
|
|
|
netdev, iobuf, strerror ( rc ) );
|
|
|
|
|
|
|
|
/* Discard packet */
|
|
|
|
free_iob ( iobuf );
|
|
|
|
|
|
|
|
/* Update statistics counter */
|
|
|
|
netdev->stats.rx_err++;
|
2006-04-19 12:07:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2007-07-07 15:43:39 +00:00
|
|
|
* Poll for completed and received packets on network device
|
2006-04-19 12:07:46 +00:00
|
|
|
*
|
2006-06-17 22:36:27 +00:00
|
|
|
* @v netdev Network device
|
2006-04-19 12:07:46 +00:00
|
|
|
*
|
2007-07-07 15:43:39 +00:00
|
|
|
* Polls the network device for completed transmissions and received
|
|
|
|
* packets. Any received packets will be added to the RX packet queue
|
|
|
|
* via netdev_rx().
|
2006-04-19 12:07:46 +00:00
|
|
|
*/
|
2007-07-07 15:43:39 +00:00
|
|
|
void netdev_poll ( struct net_device *netdev ) {
|
2007-01-04 04:20:08 +00:00
|
|
|
|
|
|
|
if ( netdev->state & NETDEV_OPEN )
|
2007-07-07 15:43:39 +00:00
|
|
|
netdev->op->poll ( netdev );
|
2006-04-19 12:07:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2006-06-17 22:36:27 +00:00
|
|
|
* Remove packet from device's receive queue
|
2006-04-19 12:07:46 +00:00
|
|
|
*
|
2006-06-17 22:36:27 +00:00
|
|
|
* @v netdev Network device
|
2007-05-19 18:39:40 +00:00
|
|
|
* @ret iobuf I/O buffer, or NULL
|
2006-04-19 12:07:46 +00:00
|
|
|
*
|
2006-06-17 22:36:27 +00:00
|
|
|
* Removes the first packet from the device's RX queue and returns it.
|
2006-04-24 15:33:06 +00:00
|
|
|
* Ownership of the packet is transferred to the caller.
|
2006-04-19 12:07:46 +00:00
|
|
|
*/
|
2007-05-19 18:39:40 +00:00
|
|
|
struct io_buffer * netdev_rx_dequeue ( struct net_device *netdev ) {
|
|
|
|
struct io_buffer *iobuf;
|
2006-04-19 12:07:46 +00:00
|
|
|
|
2007-05-19 18:39:40 +00:00
|
|
|
list_for_each_entry ( iobuf, &netdev->rx_queue, list ) {
|
|
|
|
list_del ( &iobuf->list );
|
|
|
|
return iobuf;
|
2006-04-19 12:07:46 +00:00
|
|
|
}
|
2006-04-24 15:33:06 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2006-04-19 12:07:46 +00:00
|
|
|
|
2007-06-27 13:48:31 +00:00
|
|
|
/**
|
|
|
|
* Flush device's receive queue
|
|
|
|
*
|
|
|
|
* @v netdev Network device
|
|
|
|
*/
|
|
|
|
static void netdev_rx_flush ( struct net_device *netdev ) {
|
|
|
|
struct io_buffer *iobuf;
|
|
|
|
|
|
|
|
/* Discard any packets in the RX queue */
|
|
|
|
while ( ( iobuf = netdev_rx_dequeue ( netdev ) ) ) {
|
2007-07-05 16:18:27 +00:00
|
|
|
netdev_rx_err ( netdev, iobuf, -ECANCELED );
|
2007-06-27 13:48:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Free network device
|
|
|
|
*
|
|
|
|
* @v refcnt Network device reference counter
|
|
|
|
*/
|
|
|
|
static void free_netdev ( struct refcnt *refcnt ) {
|
|
|
|
struct net_device *netdev =
|
|
|
|
container_of ( refcnt, struct net_device, refcnt );
|
|
|
|
|
|
|
|
netdev_tx_flush ( netdev );
|
|
|
|
netdev_rx_flush ( netdev );
|
|
|
|
free ( netdev );
|
|
|
|
}
|
|
|
|
|
2006-04-29 17:17:43 +00:00
|
|
|
/**
|
2006-06-17 22:36:27 +00:00
|
|
|
* Allocate network device
|
2006-04-29 17:17:43 +00:00
|
|
|
*
|
2006-06-17 22:36:27 +00:00
|
|
|
* @v priv_size Size of private data area (net_device::priv)
|
|
|
|
* @ret netdev Network device, or NULL
|
2006-04-29 17:17:43 +00:00
|
|
|
*
|
2006-06-17 22:36:27 +00:00
|
|
|
* Allocates space for a network device and its private data area.
|
2006-04-29 17:17:43 +00:00
|
|
|
*/
|
2006-06-17 22:36:27 +00:00
|
|
|
struct net_device * alloc_netdev ( size_t priv_size ) {
|
|
|
|
struct net_device *netdev;
|
2007-01-18 12:45:58 +00:00
|
|
|
size_t total_len;
|
2006-04-24 15:33:06 +00:00
|
|
|
|
2007-01-18 12:45:58 +00:00
|
|
|
total_len = ( sizeof ( *netdev ) + priv_size );
|
2007-07-06 19:08:29 +00:00
|
|
|
netdev = zalloc ( total_len );
|
2006-06-17 22:36:27 +00:00
|
|
|
if ( netdev ) {
|
2007-06-27 13:48:31 +00:00
|
|
|
netdev->refcnt.free = free_netdev;
|
2007-01-09 20:18:31 +00:00
|
|
|
INIT_LIST_HEAD ( &netdev->tx_queue );
|
2006-06-17 22:36:27 +00:00
|
|
|
INIT_LIST_HEAD ( &netdev->rx_queue );
|
|
|
|
netdev->priv = ( ( ( void * ) netdev ) + sizeof ( *netdev ) );
|
2006-04-29 17:17:43 +00:00
|
|
|
}
|
2006-06-17 22:36:27 +00:00
|
|
|
return netdev;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register network device
|
|
|
|
*
|
|
|
|
* @v netdev Network device
|
|
|
|
* @ret rc Return status code
|
|
|
|
*
|
2007-01-09 23:48:18 +00:00
|
|
|
* Gives the network device a name and adds it to the list of network
|
|
|
|
* devices.
|
2006-06-17 22:36:27 +00:00
|
|
|
*/
|
|
|
|
int register_netdev ( struct net_device *netdev ) {
|
2007-01-09 23:48:18 +00:00
|
|
|
static unsigned int ifindex = 0;
|
|
|
|
|
|
|
|
/* Create device name */
|
|
|
|
snprintf ( netdev->name, sizeof ( netdev->name ), "net%d",
|
|
|
|
ifindex++ );
|
|
|
|
|
2006-06-17 22:36:27 +00:00
|
|
|
/* Add to device list */
|
2007-06-27 13:48:31 +00:00
|
|
|
netdev_get ( netdev );
|
2006-06-17 22:36:27 +00:00
|
|
|
list_add_tail ( &netdev->list, &net_devices );
|
2007-01-10 04:22:09 +00:00
|
|
|
DBGC ( netdev, "NETDEV %p registered as %s (phys %s hwaddr %s)\n",
|
|
|
|
netdev, netdev->name, netdev->dev->name,
|
|
|
|
netdev_hwaddr ( netdev ) );
|
2006-06-17 22:36:27 +00:00
|
|
|
|
2006-04-29 17:17:43 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2006-04-24 15:33:06 +00:00
|
|
|
|
2006-06-17 22:36:27 +00:00
|
|
|
/**
|
2007-01-04 04:20:08 +00:00
|
|
|
* Open network device
|
2006-06-17 22:36:27 +00:00
|
|
|
*
|
|
|
|
* @v netdev Network device
|
2007-01-04 04:20:08 +00:00
|
|
|
* @ret rc Return status code
|
|
|
|
*/
|
|
|
|
int netdev_open ( struct net_device *netdev ) {
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
/* Do nothing if device is already open */
|
|
|
|
if ( netdev->state & NETDEV_OPEN )
|
|
|
|
return 0;
|
|
|
|
|
2007-01-09 20:18:31 +00:00
|
|
|
DBGC ( netdev, "NETDEV %p opening\n", netdev );
|
2007-01-04 04:20:08 +00:00
|
|
|
|
|
|
|
/* Open the device */
|
2007-07-07 15:43:39 +00:00
|
|
|
if ( ( rc = netdev->op->open ( netdev ) ) != 0 )
|
2007-01-04 04:20:08 +00:00
|
|
|
return rc;
|
|
|
|
|
|
|
|
/* Mark as opened */
|
|
|
|
netdev->state |= NETDEV_OPEN;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Close network device
|
2006-06-17 22:36:27 +00:00
|
|
|
*
|
2007-01-04 04:20:08 +00:00
|
|
|
* @v netdev Network device
|
2006-06-17 22:36:27 +00:00
|
|
|
*/
|
2007-01-04 04:20:08 +00:00
|
|
|
void netdev_close ( struct net_device *netdev ) {
|
2006-06-17 22:36:27 +00:00
|
|
|
|
2007-01-04 04:20:08 +00:00
|
|
|
/* Do nothing if device is already closed */
|
|
|
|
if ( ! ( netdev->state & NETDEV_OPEN ) )
|
|
|
|
return;
|
|
|
|
|
2007-01-09 20:18:31 +00:00
|
|
|
DBGC ( netdev, "NETDEV %p closing\n", netdev );
|
2007-01-04 04:20:08 +00:00
|
|
|
|
|
|
|
/* Close the device */
|
2007-07-07 15:43:39 +00:00
|
|
|
netdev->op->close ( netdev );
|
2007-01-04 04:20:08 +00:00
|
|
|
|
2007-06-27 13:48:31 +00:00
|
|
|
/* Flush TX and RX queues */
|
|
|
|
netdev_tx_flush ( netdev );
|
|
|
|
netdev_rx_flush ( netdev );
|
2006-06-17 22:36:27 +00:00
|
|
|
|
2007-01-04 04:20:08 +00:00
|
|
|
/* Mark as closed */
|
|
|
|
netdev->state &= ~NETDEV_OPEN;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unregister network device
|
|
|
|
*
|
|
|
|
* @v netdev Network device
|
|
|
|
*
|
|
|
|
* Removes the network device from the list of network devices.
|
|
|
|
*/
|
|
|
|
void unregister_netdev ( struct net_device *netdev ) {
|
|
|
|
|
|
|
|
/* Ensure device is closed */
|
|
|
|
netdev_close ( netdev );
|
|
|
|
|
2006-06-17 22:36:27 +00:00
|
|
|
/* Remove from device list */
|
|
|
|
list_del ( &netdev->list );
|
2007-06-27 13:48:31 +00:00
|
|
|
netdev_put ( netdev );
|
2007-01-09 20:18:31 +00:00
|
|
|
DBGC ( netdev, "NETDEV %p unregistered\n", netdev );
|
2006-06-17 22:36:27 +00:00
|
|
|
}
|
|
|
|
|
2007-07-07 15:43:39 +00:00
|
|
|
/** Enable or disable interrupts
|
|
|
|
*
|
|
|
|
* @v netdev Network device
|
|
|
|
* @v enable Interrupts should be enabled
|
|
|
|
*/
|
|
|
|
void netdev_irq ( struct net_device *netdev, int enable ) {
|
|
|
|
netdev->op->irq ( netdev, enable );
|
|
|
|
}
|
|
|
|
|
2007-01-09 23:48:18 +00:00
|
|
|
/**
|
|
|
|
* Get network device by name
|
|
|
|
*
|
|
|
|
* @v name Network device name
|
|
|
|
* @ret netdev Network device, or NULL
|
|
|
|
*/
|
|
|
|
struct net_device * find_netdev ( const char *name ) {
|
|
|
|
struct net_device *netdev;
|
|
|
|
|
|
|
|
list_for_each_entry ( netdev, &net_devices, list ) {
|
|
|
|
if ( strcmp ( netdev->name, name ) == 0 )
|
|
|
|
return netdev;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2007-01-10 16:16:05 +00:00
|
|
|
/**
|
|
|
|
* Get network device by PCI bus:dev.fn address
|
|
|
|
*
|
2007-07-03 17:17:14 +00:00
|
|
|
* @v bus_type Bus type
|
|
|
|
* @v location Bus location
|
2007-01-10 16:16:05 +00:00
|
|
|
* @ret netdev Network device, or NULL
|
|
|
|
*/
|
2007-07-03 17:17:14 +00:00
|
|
|
struct net_device * find_netdev_by_location ( unsigned int bus_type,
|
|
|
|
unsigned int location ) {
|
2007-01-10 16:16:05 +00:00
|
|
|
struct net_device *netdev;
|
|
|
|
|
|
|
|
list_for_each_entry ( netdev, &net_devices, list ) {
|
2007-07-03 17:17:14 +00:00
|
|
|
if ( ( netdev->dev->desc.bus_type == bus_type ) &&
|
|
|
|
( netdev->dev->desc.location == location ) )
|
2007-01-10 16:16:05 +00:00
|
|
|
return netdev;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2007-01-09 20:18:31 +00:00
|
|
|
/**
|
|
|
|
* Transmit network-layer packet
|
|
|
|
*
|
2007-05-19 18:39:40 +00:00
|
|
|
* @v iobuf I/O buffer
|
2007-01-09 20:18:31 +00:00
|
|
|
* @v netdev Network device
|
|
|
|
* @v net_protocol Network-layer protocol
|
|
|
|
* @v ll_dest Destination link-layer address
|
|
|
|
* @ret rc Return status code
|
|
|
|
*
|
2007-05-19 18:39:40 +00:00
|
|
|
* Prepends link-layer headers to the I/O buffer and transmits the
|
2007-01-09 20:18:31 +00:00
|
|
|
* packet via the specified network device. This function takes
|
2007-05-19 18:39:40 +00:00
|
|
|
* ownership of the I/O buffer.
|
2007-01-09 20:18:31 +00:00
|
|
|
*/
|
2007-05-19 18:39:40 +00:00
|
|
|
int net_tx ( struct io_buffer *iobuf, struct net_device *netdev,
|
2007-01-09 20:18:31 +00:00
|
|
|
struct net_protocol *net_protocol, const void *ll_dest ) {
|
2007-05-19 18:39:40 +00:00
|
|
|
return netdev->ll_protocol->tx ( iobuf, netdev, net_protocol, ll_dest );
|
2007-01-09 20:18:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Process received network-layer packet
|
|
|
|
*
|
2007-05-19 18:39:40 +00:00
|
|
|
* @v iobuf I/O buffer
|
2007-01-09 20:18:31 +00:00
|
|
|
* @v netdev Network device
|
|
|
|
* @v net_proto Network-layer protocol, in network-byte order
|
|
|
|
* @v ll_source Source link-layer address
|
|
|
|
* @ret rc Return status code
|
|
|
|
*/
|
2007-05-19 18:39:40 +00:00
|
|
|
int net_rx ( struct io_buffer *iobuf, struct net_device *netdev,
|
2007-01-09 20:18:31 +00:00
|
|
|
uint16_t net_proto, const void *ll_source ) {
|
|
|
|
struct net_protocol *net_protocol;
|
|
|
|
|
|
|
|
/* Hand off to network-layer protocol, if any */
|
|
|
|
for ( net_protocol = net_protocols ; net_protocol < net_protocols_end ;
|
|
|
|
net_protocol++ ) {
|
|
|
|
if ( net_protocol->net_proto == net_proto ) {
|
2007-05-19 18:39:40 +00:00
|
|
|
return net_protocol->rx ( iobuf, netdev, ll_source );
|
2007-01-09 20:18:31 +00:00
|
|
|
}
|
|
|
|
}
|
2007-05-19 18:39:40 +00:00
|
|
|
free_iob ( iobuf );
|
2007-01-09 20:18:31 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-04-29 17:17:43 +00:00
|
|
|
/**
|
|
|
|
* Single-step the network stack
|
|
|
|
*
|
|
|
|
* @v process Network stack process
|
|
|
|
*
|
2007-01-09 21:47:01 +00:00
|
|
|
* This polls all interfaces for received packets, and processes
|
|
|
|
* packets from the RX queue.
|
2006-04-29 17:17:43 +00:00
|
|
|
*/
|
2007-05-26 15:00:56 +00:00
|
|
|
static void net_step ( struct process *process __unused ) {
|
2006-06-17 22:36:27 +00:00
|
|
|
struct net_device *netdev;
|
2007-05-19 18:39:40 +00:00
|
|
|
struct io_buffer *iobuf;
|
2006-04-29 17:17:43 +00:00
|
|
|
|
2006-06-17 22:36:27 +00:00
|
|
|
/* Poll and process each network device */
|
|
|
|
list_for_each_entry ( netdev, &net_devices, list ) {
|
|
|
|
|
2007-01-18 16:50:35 +00:00
|
|
|
/* Poll for new packets */
|
2007-07-07 15:43:39 +00:00
|
|
|
netdev_poll ( netdev );
|
2007-01-18 16:50:35 +00:00
|
|
|
|
2007-01-18 20:39:17 +00:00
|
|
|
/* Process at most one received packet. Give priority
|
|
|
|
* to getting packets out of the NIC over processing
|
|
|
|
* the received packets, because we advertise a window
|
|
|
|
* that assumes that we can receive packets from the
|
|
|
|
* NIC faster than they arrive.
|
|
|
|
*/
|
2007-05-19 18:39:40 +00:00
|
|
|
if ( ( iobuf = netdev_rx_dequeue ( netdev ) ) ) {
|
2007-01-09 20:18:31 +00:00
|
|
|
DBGC ( netdev, "NETDEV %p processing %p\n",
|
2007-05-19 18:39:40 +00:00
|
|
|
netdev, iobuf );
|
|
|
|
netdev->ll_protocol->rx ( iobuf, netdev );
|
2006-06-17 22:36:27 +00:00
|
|
|
}
|
2006-04-24 15:33:06 +00:00
|
|
|
}
|
2006-04-19 12:07:46 +00:00
|
|
|
}
|
2006-04-24 15:33:06 +00:00
|
|
|
|
2006-04-29 17:17:43 +00:00
|
|
|
/** Networking stack process */
|
2007-07-03 19:09:14 +00:00
|
|
|
struct process net_process __permanent_process = {
|
2006-04-29 17:17:43 +00:00
|
|
|
.step = net_step,
|
|
|
|
};
|