* 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.
* 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 */
}
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 );
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 */
* 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;
}
-
- return netdev->transmit ( netdev, pkb );
+
+ 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;
}
/**
- * Add packet to receive queue
+ * Complete network transmission
*
* @v netdev Network device
* @v pkb Packet buffer
*
- * The packet is added to the network device's RX queue. This
- * function takes ownership of the packet buffer.
+ * The packet must currently be in the network device's TX queue.
*/
-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 ) );
- list_add_tail ( &pkb->list, &netdev->rx_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 );
}
/**
- * Transmit network-layer packet
+ * Complete network transmission
*
- * @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.
+ * Completes the oldest outstanding packet in the TX queue.
*/
-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 );
+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;
+ }
}
/**
- * Process received network-layer packet
+ * Add packet to receive queue
*
- * @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
+ * @v pkb Packet buffer
+ *
+ * The packet is added to the network device's RX queue. This
+ * function takes ownership of the packet buffer.
*/
-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;
+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 );
}
/**
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 ) );
}
/* 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;
}
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 )
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 );
}
/* Remove from device list */
list_del ( &netdev->list );
- DBG ( "%s unregistered\n", netdev_name ( netdev ) );
+ DBGC ( netdev, "NETDEV %p unregistered\n", netdev );
}
/**
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
*
/* 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 );
}
}