david/ipxe
david
/
ipxe
Archived
1
0
Fork 0
This repository has been archived on 2020-12-06. You can view files and clone it, but cannot push or open issues or pull requests.
ipxe/src/net/netdevice.c

426 lines
11 KiB
C
Raw Normal View History

2006-04-19 14:07:46 +02: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>
#include <stdlib.h>
2007-01-19 02:13:12 +01:00
#include <stdio.h>
2006-04-19 14:07:46 +02:00
#include <byteswap.h>
#include <string.h>
2006-04-19 14:07:46 +02:00
#include <errno.h>
#include <gpxe/if_ether.h>
#include <gpxe/iobuf.h>
#include <gpxe/tables.h>
#include <gpxe/process.h>
2006-04-30 03:08:52 +02:00
#include <gpxe/init.h>
#include <gpxe/device.h>
2006-04-19 14:07:46 +02:00
#include <gpxe/netdevice.h>
/** @file
*
* Network device management
2006-04-19 14:07:46 +02:00
*
*/
/** Registered network-layer protocols */
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 );
/** List of network devices */
struct list_head net_devices = LIST_HEAD_INIT ( net_devices );
2006-04-19 14:07:46 +02:00
2006-06-16 02:19:46 +02:00
/**
* Transmit raw packet via network device
2006-06-16 02:19:46 +02:00
*
* @v netdev Network device
* @v iobuf I/O buffer
2006-06-16 02:19:46 +02:00
* @ret rc Return status code
*
* Transmits the packet via the specified network device. This
* function takes ownership of the I/O buffer.
2006-06-16 02:19:46 +02:00
*/
int netdev_tx ( struct net_device *netdev, struct io_buffer *iobuf ) {
int rc;
DBGC ( netdev, "NETDEV %p transmitting %p (%p+%zx)\n",
netdev, iobuf, iobuf->data, iob_len ( iobuf ) );
list_add_tail ( &iobuf->list, &netdev->tx_queue );
if ( ! ( netdev->state & NETDEV_OPEN ) ) {
rc = -ENETUNREACH;
goto err;
}
if ( ( rc = netdev->transmit ( netdev, iobuf ) ) != 0 )
goto err;
return 0;
err:
DBGC ( netdev, "NETDEV %p transmission %p failed: %s\n",
netdev, iobuf, strerror ( rc ) );
netdev_tx_complete ( netdev, iobuf );
return rc;
2006-06-16 02:19:46 +02:00
}
/**
* Complete network transmission
2006-06-16 02:19:46 +02:00
*
* @v netdev Network device
* @v iobuf I/O buffer
2006-06-16 02:19:46 +02:00
*
* The packet must currently be in the network device's TX queue.
2006-06-16 02:19:46 +02:00
*/
void netdev_tx_complete ( struct net_device *netdev, struct io_buffer *iobuf ) {
DBGC ( netdev, "NETDEV %p transmission %p complete\n", netdev, iobuf );
2007-01-11 06:27:02 +01:00
/* Catch data corruption as early as possible */
assert ( iobuf->list.next != NULL );
assert ( iobuf->list.prev != NULL );
2007-01-11 06:27:02 +01:00
list_del ( &iobuf->list );
free_iob ( iobuf );
2006-06-16 02:19:46 +02:00
}
/**
* Complete network transmission
*
* @v netdev Network device
*
* Completes the oldest outstanding packet in the TX queue.
*/
void netdev_tx_complete_next ( struct net_device *netdev ) {
struct io_buffer *iobuf;
list_for_each_entry ( iobuf, &netdev->tx_queue, list ) {
netdev_tx_complete ( netdev, iobuf );
return;
}
}
2006-04-19 14:07:46 +02:00
/**
* Add packet to receive queue
2006-04-19 14:07:46 +02:00
*
* @v netdev Network device
* @v iobuf I/O buffer
*
* The packet is added to the network device's RX queue. This
* function takes ownership of the I/O buffer.
2006-04-19 14:07:46 +02:00
*/
void netdev_rx ( struct net_device *netdev, struct io_buffer *iobuf ) {
DBGC ( netdev, "NETDEV %p received %p (%p+%zx)\n",
netdev, iobuf, iobuf->data, iob_len ( iobuf ) );
list_add_tail ( &iobuf->list, &netdev->rx_queue );
2006-04-19 14:07:46 +02:00
}
/**
* Poll for packet on network device
2006-04-19 14:07:46 +02:00
*
* @v netdev Network device
* @v rx_quota Maximum number of packets to receive
* @ret True There are packets present in the receive queue
* @ret False There are no packets present in the receive queue
2006-04-19 14:07:46 +02:00
*
* Polls the network device for received packets. Any received
* packets will be added to the RX packet queue via netdev_rx().
2006-04-19 14:07:46 +02:00
*/
int netdev_poll ( struct net_device *netdev, unsigned int rx_quota ) {
if ( netdev->state & NETDEV_OPEN )
netdev->poll ( netdev, rx_quota );
return ( ! list_empty ( &netdev->rx_queue ) );
2006-04-19 14:07:46 +02:00
}
/**
* Remove packet from device's receive queue
2006-04-19 14:07:46 +02:00
*
* @v netdev Network device
* @ret iobuf I/O buffer, or NULL
2006-04-19 14:07:46 +02:00
*
* Removes the first packet from the device's RX queue and returns it.
* Ownership of the packet is transferred to the caller.
2006-04-19 14:07:46 +02:00
*/
struct io_buffer * netdev_rx_dequeue ( struct net_device *netdev ) {
struct io_buffer *iobuf;
2006-04-19 14:07:46 +02:00
list_for_each_entry ( iobuf, &netdev->rx_queue, list ) {
list_del ( &iobuf->list );
return iobuf;
2006-04-19 14:07:46 +02:00
}
return NULL;
}
2006-04-19 14:07:46 +02:00
/**
* Allocate network device
*
* @v priv_size Size of private data area (net_device::priv)
* @ret netdev Network device, or NULL
*
* Allocates space for a network device and its private data area.
*/
struct net_device * alloc_netdev ( size_t priv_size ) {
struct net_device *netdev;
size_t total_len;
total_len = ( sizeof ( *netdev ) + priv_size );
netdev = malloc ( total_len );
if ( netdev ) {
memset ( netdev, 0, total_len );
INIT_LIST_HEAD ( &netdev->references );
INIT_LIST_HEAD ( &netdev->tx_queue );
INIT_LIST_HEAD ( &netdev->rx_queue );
netdev->priv = ( ( ( void * ) netdev ) + sizeof ( *netdev ) );
}
return netdev;
}
/**
* Register network device
*
* @v netdev Network device
* @ret rc Return status code
*
* Gives the network device a name and adds it to the list of network
* devices.
*/
int register_netdev ( struct net_device *netdev ) {
static unsigned int ifindex = 0;
/* Create device name */
snprintf ( netdev->name, sizeof ( netdev->name ), "net%d",
ifindex++ );
/* Add to device list */
list_add_tail ( &netdev->list, &net_devices );
DBGC ( netdev, "NETDEV %p registered as %s (phys %s hwaddr %s)\n",
netdev, netdev->name, netdev->dev->name,
netdev_hwaddr ( netdev ) );
return 0;
}
/**
* Open network device
*
* @v netdev Network device
* @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;
DBGC ( netdev, "NETDEV %p opening\n", netdev );
/* Open the device */
if ( ( rc = netdev->open ( netdev ) ) != 0 )
return rc;
/* Mark as opened */
netdev->state |= NETDEV_OPEN;
return 0;
}
/**
* Close network device
*
* @v netdev Network device
*/
void netdev_close ( struct net_device *netdev ) {
struct io_buffer *iobuf;
/* Do nothing if device is already closed */
if ( ! ( netdev->state & NETDEV_OPEN ) )
return;
DBGC ( netdev, "NETDEV %p closing\n", netdev );
/* Close the device */
netdev->close ( netdev );
/* Discard any packets in the TX queue */
while ( ! list_empty ( &netdev->tx_queue ) ) {
netdev_tx_complete_next ( netdev );
}
/* Discard any packets in the RX queue */
while ( ( iobuf = netdev_rx_dequeue ( netdev ) ) ) {
DBGC ( netdev, "NETDEV %p discarding received %p\n",
netdev, iobuf );
free_iob ( iobuf );
}
/* 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 );
/* Kill off any persistent references to this device */
forget_references ( &netdev->references );
/* Remove from device list */
list_del ( &netdev->list );
DBGC ( netdev, "NETDEV %p unregistered\n", netdev );
}
/**
* Free network device
*
* @v netdev Network device
*/
void free_netdev ( struct net_device *netdev ) {
free ( netdev );
}
/**
* 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 17:16:05 +01:00
/**
* Get network device by PCI bus:dev.fn address
*
* @v busdevfn PCI bus:dev.fn address
* @ret netdev Network device, or NULL
*/
struct net_device * find_pci_netdev ( unsigned int busdevfn ) {
struct net_device *netdev;
list_for_each_entry ( netdev, &net_devices, list ) {
if ( ( netdev->dev->desc.bus_type == BUS_TYPE_PCI ) &&
( netdev->dev->desc.location == busdevfn ) )
2007-01-10 17:16:05 +01:00
return netdev;
}
return NULL;
}
/**
* Transmit network-layer packet
*
* @v iobuf I/O 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 I/O buffer and transmits the
* packet via the specified network device. This function takes
* ownership of the I/O buffer.
*/
int net_tx ( struct io_buffer *iobuf, struct net_device *netdev,
struct net_protocol *net_protocol, const void *ll_dest ) {
return netdev->ll_protocol->tx ( iobuf, netdev, net_protocol, ll_dest );
}
/**
* Process received network-layer packet
*
* @v iobuf I/O 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 io_buffer *iobuf, 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 ( iobuf, netdev, ll_source );
}
}
free_iob ( iobuf );
return 0;
}
/**
* Single-step the network stack
*
* @v process Network stack process
*
* This polls all interfaces for received packets, and processes
* packets from the RX queue.
*/
static void net_step ( struct process *process __unused ) {
struct net_device *netdev;
struct io_buffer *iobuf;
/* Poll and process each network device */
list_for_each_entry ( netdev, &net_devices, list ) {
/* Poll for new packets */
netdev_poll ( netdev, -1U );
/* 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.
*/
if ( ( iobuf = netdev_rx_dequeue ( netdev ) ) ) {
DBGC ( netdev, "NETDEV %p processing %p\n",
netdev, iobuf );
netdev->ll_protocol->rx ( iobuf, netdev );
}
}
2006-04-19 14:07:46 +02:00
}
/** Networking stack process */
static struct process net_process = {
.step = net_step,
};
2006-04-30 03:08:52 +02:00
/** Initialise the networking stack process */
static void init_net ( void ) {
process_add ( &net_process );
}
2006-04-30 03:08:52 +02:00
INIT_FN ( INIT_PROCESS, init_net, NULL, NULL );