From 02a021587336a9ada3845025610ba836b173464d Mon Sep 17 00:00:00 2001 From: Michael Brown Date: Fri, 21 Nov 2008 20:31:12 +0000 Subject: [netdevice] Provide function to retrieve the most recently opened net device There are currently four places within the codebase that use a heuristic to guess the "boot network device", with varying degrees of success. Add a feature to the net device core to maintain a list of open network devices, in order of opening, and provide a function last_opened_netdev() to retrieve the most recently opened net device. This should do a better job than the current assortment of guess_boot_netdev() functions. --- src/include/gpxe/netdevice.h | 3 +++ src/net/netdevice.c | 26 ++++++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/src/include/gpxe/netdevice.h b/src/include/gpxe/netdevice.h index 01b853f3..f1585de0 100644 --- a/src/include/gpxe/netdevice.h +++ b/src/include/gpxe/netdevice.h @@ -229,6 +229,8 @@ struct net_device { struct refcnt refcnt; /** List of network devices */ struct list_head list; + /** List of open network devices */ + struct list_head open_list; /** Name of this network device */ char name[8]; /** Underlying hardware device */ @@ -424,6 +426,7 @@ extern void netdev_irq ( struct net_device *netdev, int enable ); extern struct net_device * find_netdev ( const char *name ); extern struct net_device * find_netdev_by_location ( unsigned int bus_type, unsigned int location ); +extern struct net_device * last_opened_netdev ( void ); extern int net_tx ( struct io_buffer *iobuf, struct net_device *netdev, struct net_protocol *net_protocol, const void *ll_dest ); extern int net_rx ( struct io_buffer *iobuf, struct net_device *netdev, diff --git a/src/net/netdevice.c b/src/net/netdevice.c index e8587a9d..9e142d27 100644 --- a/src/net/netdevice.c +++ b/src/net/netdevice.c @@ -45,6 +45,9 @@ static struct net_protocol net_protocols_end[0] /** List of network devices */ struct list_head net_devices = LIST_HEAD_INIT ( net_devices ); +/** List of open network devices, in reverse order of opening */ +struct list_head open_net_devices = LIST_HEAD_INIT ( open_net_devices ); + /** * Record network device statistic * @@ -368,6 +371,10 @@ int netdev_open ( struct net_device *netdev ) { /* Mark as opened */ netdev->state |= NETDEV_OPEN; + + /* Add to head of open devices list */ + list_add ( &netdev->open_list, &open_net_devices ); + return 0; } @@ -393,6 +400,9 @@ void netdev_close ( struct net_device *netdev ) { /* Mark as closed */ netdev->state &= ~NETDEV_OPEN; + + /* Remove from open devices list */ + list_del ( &netdev->open_list ); } /** @@ -462,6 +472,22 @@ struct net_device * find_netdev_by_location ( unsigned int bus_type, return NULL; } +/** + * Get most recently opened network device + * + * @ret netdev Most recently opened network device, or NULL + */ +struct net_device * last_opened_netdev ( void ) { + struct net_device *netdev; + + list_for_each_entry ( netdev, &open_net_devices, open_list ) { + assert ( netdev->state & NETDEV_OPEN ); + return netdev; + } + + return NULL; +} + /** * Transmit network-layer packet * -- cgit v1.2.3-55-g7522