From b7fcfe8ece36c80ddeca08aa3873063e2aefa536 Mon Sep 17 00:00:00 2001 From: Michael Brown Date: Tue, 9 Jan 2007 20:18:31 +0000 Subject: [PATCH] Added net device TX queue; this will be needed to support the PXE UNDI API (which will need us to wait for TX completions). Added debug autocolourisation to netdevice.c --- src/arch/i386/drivers/net/undinet.c | 12 +- src/drivers/net/legacy.c | 2 +- src/drivers/net/pnic.c | 2 +- src/drivers/net/rtl8139.c | 15 +-- src/include/gpxe/netdevice.h | 22 ++-- src/net/netdevice.c | 167 ++++++++++++++++++---------- 6 files changed, 135 insertions(+), 85 deletions(-) diff --git a/src/arch/i386/drivers/net/undinet.c b/src/arch/i386/drivers/net/undinet.c index dc48c975..42b0cfb7 100644 --- a/src/arch/i386/drivers/net/undinet.c +++ b/src/arch/i386/drivers/net/undinet.c @@ -349,11 +349,15 @@ static int undinet_transmit ( struct net_device *netdev, = ( ( unsigned ) & __from_data16 ( undinet_pkb ) ); /* Issue PXE API call */ - rc = undinet_call ( undinic, PXENV_UNDI_TRANSMIT, &undi_transmit, - sizeof ( undi_transmit ) ); + if ( ( rc = undinet_call ( undinic, PXENV_UNDI_TRANSMIT, + &undi_transmit, + sizeof ( undi_transmit ) ) ) != 0 ) + goto done; - /* Free packet buffer and return */ - free_pkb ( pkb ); + /* Free packet buffer */ + netdev_tx_complete ( netdev, pkb ); + + done: return rc; } diff --git a/src/drivers/net/legacy.c b/src/drivers/net/legacy.c index 0c205aed..2d952aac 100644 --- a/src/drivers/net/legacy.c +++ b/src/drivers/net/legacy.c @@ -34,7 +34,7 @@ static int legacy_transmit ( struct net_device *netdev, struct pk_buff *pkb ) { nic->nic_op->transmit ( nic, ( const char * ) ethhdr->h_dest, ntohs ( ethhdr->h_protocol ), pkb_len ( pkb ), pkb->data ); - free_pkb ( pkb ); + netdev_tx_complete ( netdev, pkb ); return 0; } diff --git a/src/drivers/net/pnic.c b/src/drivers/net/pnic.c index d8f2544e..09ac92a4 100644 --- a/src/drivers/net/pnic.c +++ b/src/drivers/net/pnic.c @@ -158,7 +158,7 @@ static int pnic_transmit ( struct net_device *netdev, struct pk_buff *pkb ) { pnic_command ( pnic, PNIC_CMD_XMIT, pkb->data, pkb_len ( pkb ), NULL, 0, NULL ); - free_pkb ( pkb ); + netdev_tx_complete ( netdev, pkb ); return 0; } diff --git a/src/drivers/net/rtl8139.c b/src/drivers/net/rtl8139.c index 4592ed7d..5e37f9c1 100644 --- a/src/drivers/net/rtl8139.c +++ b/src/drivers/net/rtl8139.c @@ -304,7 +304,7 @@ static void rtl_reset ( struct rtl8139_nic *rtl ) { /* Reset chip */ outb ( CmdReset, rtl->ioaddr + ChipCmd ); mdelay ( 10 ); - rtl->tx.next = 0; + memset ( &rtl->tx, 0, sizeof ( rtl->tx ) ); rtl->rx.offset = 0; } @@ -349,7 +349,6 @@ static int rtl_open ( struct net_device *netdev ) { */ static void rtl_close ( struct net_device *netdev ) { struct rtl8139_nic *rtl = netdev->priv; - int i; /* Reset the hardware to disable everything in one go */ rtl_reset ( rtl ); @@ -357,15 +356,6 @@ static void rtl_close ( struct net_device *netdev ) { /* Free RX ring */ free ( rtl->rx.ring ); rtl->rx.ring = NULL; - - /* Free any old TX buffers that hadn't yet completed */ - for ( i = 0 ; i < TX_RING_SIZE ; i++ ) { - if ( rtl->tx.pkb[i] ) { - free_pkb ( rtl->tx.pkb[i] ); - rtl->tx.pkb[i] = NULL; - DBG ( "TX id %d discarded\n", i ); - } - } } /** @@ -383,7 +373,6 @@ static int rtl_transmit ( struct net_device *netdev, struct pk_buff *pkb ) { /* Check for space in TX ring */ if ( rtl->tx.pkb[rtl->tx.next] != NULL ) { printf ( "TX overflow\n" ); - free_pkb ( pkb ); return -ENOBUFS; } @@ -437,7 +426,7 @@ static void rtl_poll ( struct net_device *netdev ) { for ( i = 0 ; i < TX_RING_SIZE ; i++ ) { if ( ( rtl->tx.pkb[i] != NULL ) && ( tsad & ( 1 << i ) ) ) { DBG ( "TX id %d complete\n", i ); - free_pkb ( rtl->tx.pkb[i] ); + netdev_tx_complete ( netdev, rtl->tx.pkb[i] ); rtl->tx.pkb[i] = NULL; } } diff --git a/src/include/gpxe/netdevice.h b/src/include/gpxe/netdevice.h index 20df7919..ad1f9013 100644 --- a/src/include/gpxe/netdevice.h +++ b/src/include/gpxe/netdevice.h @@ -168,9 +168,11 @@ struct net_device { * This method should cause the hardware to initiate * transmission of the packet buffer. * - * Ownership of the packet buffer is transferred to the @c - * net_device, which must eventually call free_pkb() to - * release the buffer. + * If this method returns success, the packet buffer remains + * owned by the net device's TX queue, and the net device must + * eventually call netdev_tx_complete() to free the buffer. + * If this method returns failure, the packet buffer is + * immediately released. * * This method is guaranteed to be called only when the device * is open. @@ -202,7 +204,9 @@ struct net_device { * This is the bitwise-OR of zero or more NETDEV_XXX constants. */ unsigned int state; - /** Received packet queue */ + /** TX packet queue */ + struct list_head tx_queue; + /** RX packet queue */ struct list_head rx_queue; /** Driver private data */ @@ -231,11 +235,9 @@ static inline const char * netdev_name ( struct net_device *netdev ) { } extern int netdev_tx ( struct net_device *netdev, struct pk_buff *pkb ); +void netdev_tx_complete ( struct net_device *netdev, struct pk_buff *pkb ); +void netdev_tx_complete_next ( struct net_device *netdev ); extern void netdev_rx ( struct net_device *netdev, struct pk_buff *pkb ); -extern int net_tx ( struct pk_buff *pkb, struct net_device *netdev, - struct net_protocol *net_protocol, const void *ll_dest ); -extern int net_rx ( struct pk_buff *pkb, struct net_device *netdev, - uint16_t net_proto, const void *ll_source ); extern int netdev_poll ( struct net_device *netdev ); extern struct pk_buff * netdev_rx_dequeue ( struct net_device *netdev ); extern struct net_device * alloc_netdev ( size_t priv_size ); @@ -245,5 +247,9 @@ extern void netdev_close ( struct net_device *netdev ); extern void unregister_netdev ( struct net_device *netdev ); extern void free_netdev ( struct net_device *netdev ); extern struct net_device * next_netdev ( void ); +extern int net_tx ( struct pk_buff *pkb, struct net_device *netdev, + struct net_protocol *net_protocol, const void *ll_dest ); +extern int net_rx ( struct pk_buff *pkb, struct net_device *netdev, + uint16_t net_proto, const void *ll_source ); #endif /* _GPXE_NETDEVICE_H */ diff --git a/src/net/netdevice.c b/src/net/netdevice.c index 76ff3777..6da2ddfd 100644 --- a/src/net/netdevice.c +++ b/src/net/netdevice.c @@ -52,15 +52,59 @@ static LIST_HEAD ( net_devices ); * function takes ownership of the packet buffer. */ int netdev_tx ( struct net_device *netdev, struct pk_buff *pkb ) { - DBG ( "%s transmitting %p+%zx\n", netdev_name ( netdev ), - pkb->data, pkb_len ( pkb ) ); + 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 ); if ( ! ( netdev->state & NETDEV_OPEN ) ) { - free_pkb ( pkb ); - return -ENETUNREACH; + rc = -ENETUNREACH; + goto err; + } + + 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; +} + +/** + * Complete network transmission + * + * @v netdev Network device + * @v pkb Packet buffer + * + * The packet must currently be in the network device's TX queue. + */ +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 ); +} + +/** + * 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 pk_buff *pkb; + + list_for_each_entry ( pkb, &netdev->tx_queue, list ) { + netdev_tx_complete ( netdev, pkb ); + return; } - - return netdev->transmit ( netdev, pkb ); } /** @@ -73,53 +117,11 @@ int netdev_tx ( struct net_device *netdev, struct pk_buff *pkb ) { * function takes ownership of the packet buffer. */ void netdev_rx ( struct net_device *netdev, struct pk_buff *pkb ) { - DBG ( "%s received %p+%zx\n", netdev_name ( netdev ), - pkb->data, pkb_len ( 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 ); } -/** - * 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; -} - /** * Poll for packet on network device * @@ -171,6 +173,7 @@ struct net_device * alloc_netdev ( size_t priv_size ) { netdev = calloc ( 1, sizeof ( *netdev ) + priv_size ); if ( netdev ) { INIT_LIST_HEAD ( &netdev->references ); + INIT_LIST_HEAD ( &netdev->tx_queue ); INIT_LIST_HEAD ( &netdev->rx_queue ); netdev->priv = ( ( ( void * ) netdev ) + sizeof ( *netdev ) ); } @@ -189,7 +192,8 @@ int register_netdev ( struct net_device *netdev ) { /* Add to device list */ list_add_tail ( &netdev->list, &net_devices ); - DBG ( "%s registered\n", netdev_name ( netdev ) ); + DBGC ( netdev, "NETDEV %p registered as %s\n", + netdev, netdev_name ( netdev ) ); return 0; } @@ -207,7 +211,7 @@ int netdev_open ( struct net_device *netdev ) { if ( netdev->state & NETDEV_OPEN ) return 0; - DBG ( "%s opening\n", netdev_name ( netdev ) ); + DBGC ( netdev, "NETDEV %p opening\n", netdev ); /* Open the device */ if ( ( rc = netdev->open ( netdev ) ) != 0 ) @@ -230,15 +234,20 @@ void netdev_close ( struct net_device *netdev ) { if ( ! ( netdev->state & NETDEV_OPEN ) ) return; - DBG ( "%s closing\n", netdev_name ( netdev ) ); + 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 ( ( pkb = netdev_rx_dequeue ( netdev ) ) ) { - DBG ( "%s discarding %p+%zx\n", netdev_name ( netdev ), - pkb->data, pkb_len ( pkb ) ); + DBGC ( netdev, "NETDEV %p discarding received %p\n", + netdev, pkb ); free_pkb ( pkb ); } @@ -263,7 +272,7 @@ void unregister_netdev ( struct net_device *netdev ) { /* Remove from device list */ list_del ( &netdev->list ); - DBG ( "%s unregistered\n", netdev_name ( netdev ) ); + DBGC ( netdev, "NETDEV %p unregistered\n", netdev ); } /** @@ -295,6 +304,48 @@ struct net_device * next_netdev ( void ) { return NULL; } +/** + * 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; +} + /** * Single-step the network stack * @@ -321,8 +372,8 @@ static void net_step ( struct process *process ) { /* Handle at most one received packet per poll */ if ( ( pkb = netdev_rx_dequeue ( netdev ) ) ) { - DBG ( "%s processing %p+%zx\n", netdev_name ( netdev ), - pkb->data, pkb_len ( pkb ) ); + DBGC ( netdev, "NETDEV %p processing %p\n", + netdev, pkb ); netdev->ll_protocol->rx ( pkb, netdev ); } }