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>
|
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>
|
|
|
|
#include <gpxe/pkbuff.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>
|
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 */
|
|
|
|
static struct net_protocol net_protocols[0] __table_start ( net_protocols );
|
|
|
|
static struct net_protocol net_protocols_end[0] __table_end ( net_protocols );
|
|
|
|
|
2006-06-17 22:36:27 +00:00
|
|
|
/** List of network devices */
|
|
|
|
static LIST_HEAD ( 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
|
2006-06-17 22:36:27 +00:00
|
|
|
* @v pkb Packet 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
|
|
|
|
* function takes ownership of the packet buffer.
|
2006-06-16 00:19:46 +00:00
|
|
|
*/
|
2006-06-17 22:36:27 +00:00
|
|
|
int netdev_tx ( struct net_device *netdev, struct pk_buff *pkb ) {
|
2007-01-09 20:18:31 +00:00
|
|
|
int rc;
|
|
|
|
|
|
|
|
DBGC ( netdev, "NETDEV %p transmitting %p (%p+%zx)\n",
|
|
|
|
netdev, pkb, pkb->data, pkb_len ( pkb ) );
|
|
|
|
|
|
|
|
list_add_tail ( &pkb->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
|
|
|
|
|
|
|
if ( ( rc = netdev->transmit ( netdev, pkb ) ) != 0 )
|
|
|
|
goto err;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
err:
|
|
|
|
DBGC ( netdev, "NETDEV %p transmission %p failed: %s\n",
|
|
|
|
netdev, pkb, strerror ( rc ) );
|
|
|
|
netdev_tx_complete ( netdev, pkb );
|
|
|
|
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
|
2006-06-17 22:36:27 +00:00
|
|
|
* @v pkb Packet buffer
|
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-01-09 20:18:31 +00:00
|
|
|
void netdev_tx_complete ( struct net_device *netdev, struct pk_buff *pkb ) {
|
|
|
|
DBGC ( netdev, "NETDEV %p transmission %p complete\n", netdev, pkb );
|
|
|
|
|
|
|
|
list_del ( &pkb->list );
|
|
|
|
free_pkb ( pkb );
|
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
|
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-01-09 20:18:31 +00:00
|
|
|
void netdev_tx_complete_next ( struct net_device *netdev ) {
|
|
|
|
struct pk_buff *pkb;
|
|
|
|
|
|
|
|
list_for_each_entry ( pkb, &netdev->tx_queue, list ) {
|
|
|
|
netdev_tx_complete ( netdev, pkb );
|
|
|
|
return;
|
|
|
|
}
|
2006-04-28 14:13:50 +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-01-09 20:18:31 +00:00
|
|
|
* @v pkb Packet buffer
|
|
|
|
*
|
|
|
|
* The packet is added to the network device's RX queue. This
|
|
|
|
* function takes ownership of the packet buffer.
|
2006-04-19 12:07:46 +00:00
|
|
|
*/
|
2007-01-09 20:18:31 +00:00
|
|
|
void netdev_rx ( struct net_device *netdev, struct pk_buff *pkb ) {
|
|
|
|
DBGC ( netdev, "NETDEV %p received %p (%p+%zx)\n",
|
|
|
|
netdev, pkb, pkb->data, pkb_len ( pkb ) );
|
|
|
|
list_add_tail ( &pkb->list, &netdev->rx_queue );
|
2006-04-19 12:07:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2006-06-17 22:36:27 +00:00
|
|
|
* Poll for packet 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-24 15:33:06 +00:00
|
|
|
* @ret True There are packets present in the receive queue
|
|
|
|
* @ret False There are no packets present in the receive queue
|
2006-04-19 12:07:46 +00:00
|
|
|
*
|
2006-06-17 22:36:27 +00:00
|
|
|
* Polls the network device for received packets. Any received
|
2006-04-24 15:33:06 +00:00
|
|
|
* packets will be added to the RX packet queue via netdev_rx().
|
2006-04-19 12:07:46 +00:00
|
|
|
*/
|
2006-06-17 22:36:27 +00:00
|
|
|
int netdev_poll ( struct net_device *netdev ) {
|
2007-01-04 04:20:08 +00:00
|
|
|
|
|
|
|
if ( netdev->state & NETDEV_OPEN )
|
|
|
|
netdev->poll ( netdev );
|
|
|
|
|
2006-06-17 22:36:27 +00:00
|
|
|
return ( ! list_empty ( &netdev->rx_queue ) );
|
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
|
2006-04-24 15:33:06 +00:00
|
|
|
* @ret pkb Packet 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
|
|
|
*/
|
2006-06-17 22:36:27 +00:00
|
|
|
struct pk_buff * netdev_rx_dequeue ( struct net_device *netdev ) {
|
2006-04-24 15:33:06 +00:00
|
|
|
struct pk_buff *pkb;
|
2006-04-19 12:07:46 +00:00
|
|
|
|
2006-06-17 22:36:27 +00:00
|
|
|
list_for_each_entry ( pkb, &netdev->rx_queue, list ) {
|
2006-04-24 15:33:06 +00:00
|
|
|
list_del ( &pkb->list );
|
|
|
|
return pkb;
|
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
|
|
|
|
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;
|
2006-04-24 15:33:06 +00:00
|
|
|
|
2006-06-17 22:36:27 +00:00
|
|
|
netdev = calloc ( 1, sizeof ( *netdev ) + priv_size );
|
|
|
|
if ( netdev ) {
|
2007-01-04 03:09:28 +00:00
|
|
|
INIT_LIST_HEAD ( &netdev->references );
|
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
|
|
|
|
*
|
|
|
|
* Adds the network device to the list of network devices.
|
|
|
|
*/
|
|
|
|
int register_netdev ( struct net_device *netdev ) {
|
2006-04-29 17:17:43 +00:00
|
|
|
|
2006-06-17 22:36:27 +00:00
|
|
|
/* Add to device list */
|
|
|
|
list_add_tail ( &netdev->list, &net_devices );
|
2007-01-09 20:18:31 +00:00
|
|
|
DBGC ( netdev, "NETDEV %p registered as %s\n",
|
|
|
|
netdev, netdev_name ( 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 */
|
|
|
|
if ( ( rc = netdev->open ( netdev ) ) != 0 )
|
|
|
|
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
|
|
|
struct pk_buff *pkb;
|
|
|
|
|
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 */
|
|
|
|
netdev->close ( netdev );
|
|
|
|
|
2007-01-09 20:18:31 +00:00
|
|
|
/* Discard any packets in the TX queue */
|
|
|
|
while ( ! list_empty ( &netdev->tx_queue ) ) {
|
|
|
|
netdev_tx_complete_next ( netdev );
|
|
|
|
}
|
|
|
|
|
2006-06-17 22:36:27 +00:00
|
|
|
/* Discard any packets in the RX queue */
|
|
|
|
while ( ( pkb = netdev_rx_dequeue ( netdev ) ) ) {
|
2007-01-09 20:18:31 +00:00
|
|
|
DBGC ( netdev, "NETDEV %p discarding received %p\n",
|
|
|
|
netdev, pkb );
|
2006-06-17 22:36:27 +00:00
|
|
|
free_pkb ( pkb );
|
|
|
|
}
|
|
|
|
|
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 );
|
|
|
|
|
2007-01-04 03:09:28 +00:00
|
|
|
/* Kill off any persistent references to this device */
|
|
|
|
forget_references ( &netdev->references );
|
|
|
|
|
2006-06-17 22:36:27 +00:00
|
|
|
/* Remove from device list */
|
|
|
|
list_del ( &netdev->list );
|
2007-01-09 20:18:31 +00:00
|
|
|
DBGC ( netdev, "NETDEV %p unregistered\n", netdev );
|
2006-06-17 22:36:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Free network device
|
|
|
|
*
|
|
|
|
* @v netdev Network device
|
|
|
|
*/
|
|
|
|
void free_netdev ( struct net_device *netdev ) {
|
|
|
|
free ( netdev );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Iterate through network devices
|
|
|
|
*
|
|
|
|
* @ret netdev Network device, or NULL
|
|
|
|
*
|
|
|
|
* This returns the registered network devices in the order of
|
|
|
|
* registration. If no network devices are registered, it will return
|
|
|
|
* NULL.
|
|
|
|
*/
|
|
|
|
struct net_device * next_netdev ( void ) {
|
|
|
|
struct net_device *netdev;
|
|
|
|
|
|
|
|
list_for_each_entry ( netdev, &net_devices, list ) {
|
|
|
|
list_del ( &netdev->list );
|
|
|
|
list_add_tail ( &netdev->list, &net_devices );
|
|
|
|
return netdev;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2007-01-09 20:18:31 +00:00
|
|
|
/**
|
|
|
|
* Transmit network-layer packet
|
|
|
|
*
|
|
|
|
* @v pkb Packet buffer
|
|
|
|
* @v netdev Network device
|
|
|
|
* @v net_protocol Network-layer protocol
|
|
|
|
* @v ll_dest Destination link-layer address
|
|
|
|
* @ret rc Return status code
|
|
|
|
*
|
|
|
|
* Prepends link-layer headers to the packet buffer and transmits the
|
|
|
|
* packet via the specified network device. This function takes
|
|
|
|
* ownership of the packet buffer.
|
|
|
|
*/
|
|
|
|
int net_tx ( struct pk_buff *pkb, struct net_device *netdev,
|
|
|
|
struct net_protocol *net_protocol, const void *ll_dest ) {
|
|
|
|
return netdev->ll_protocol->tx ( pkb, netdev, net_protocol, ll_dest );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Process received network-layer packet
|
|
|
|
*
|
|
|
|
* @v pkb Packet buffer
|
|
|
|
* @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
|
|
|
|
*/
|
|
|
|
int net_rx ( struct pk_buff *pkb, struct net_device *netdev,
|
|
|
|
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 ) {
|
|
|
|
return net_protocol->rx ( pkb, netdev, ll_source );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free_pkb ( pkb );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-04-29 17:17:43 +00:00
|
|
|
/**
|
|
|
|
* Single-step the network stack
|
|
|
|
*
|
|
|
|
* @v process Network stack process
|
|
|
|
*
|
|
|
|
* This polls all interfaces for any received packets, and processes
|
2006-05-27 13:55:36 +00:00
|
|
|
* at most one packet from the RX queue.
|
|
|
|
*
|
|
|
|
* We avoid processing all received packets, because processing the
|
|
|
|
* received packet can trigger transmission of a new packet (e.g. an
|
|
|
|
* ARP response). Since TX completions will be processed as part of
|
|
|
|
* the poll operation, it is easy to overflow small TX queues if
|
|
|
|
* multiple packets are processed per poll.
|
2006-04-29 17:17:43 +00:00
|
|
|
*/
|
|
|
|
static void net_step ( struct process *process ) {
|
2006-06-17 22:36:27 +00:00
|
|
|
struct net_device *netdev;
|
2006-04-29 17:17:43 +00:00
|
|
|
struct pk_buff *pkb;
|
|
|
|
|
2006-06-17 22:36:27 +00:00
|
|
|
/* Poll and process each network device */
|
|
|
|
list_for_each_entry ( netdev, &net_devices, list ) {
|
|
|
|
|
|
|
|
/* Poll for new packets */
|
|
|
|
netdev_poll ( netdev );
|
2006-04-28 14:13:50 +00:00
|
|
|
|
2006-06-17 22:36:27 +00:00
|
|
|
/* Handle at most one received packet per poll */
|
|
|
|
if ( ( pkb = netdev_rx_dequeue ( netdev ) ) ) {
|
2007-01-09 20:18:31 +00:00
|
|
|
DBGC ( netdev, "NETDEV %p processing %p\n",
|
|
|
|
netdev, pkb );
|
2006-06-17 22:36:27 +00:00
|
|
|
netdev->ll_protocol->rx ( pkb, netdev );
|
|
|
|
}
|
2006-04-24 15:33:06 +00:00
|
|
|
}
|
2006-04-29 17:17:43 +00:00
|
|
|
|
|
|
|
/* Re-schedule ourself */
|
|
|
|
schedule ( process );
|
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 */
|
|
|
|
static struct process net_process = {
|
|
|
|
.step = net_step,
|
|
|
|
};
|
|
|
|
|
2006-04-30 01:08:52 +00:00
|
|
|
/** Initialise the networking stack process */
|
2006-04-29 17:17:43 +00:00
|
|
|
static void init_net ( void ) {
|
|
|
|
schedule ( &net_process );
|
|
|
|
}
|
2006-04-24 15:33:06 +00:00
|
|
|
|
2006-04-30 01:08:52 +00:00
|
|
|
INIT_FN ( INIT_PROCESS, init_net, NULL, NULL );
|