Browse Source

[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.
tags/v0.9.6
Michael Brown 15 years ago
parent
commit
02a0215873
2 changed files with 29 additions and 0 deletions
  1. 3
    0
      src/include/gpxe/netdevice.h
  2. 26
    0
      src/net/netdevice.c

+ 3
- 0
src/include/gpxe/netdevice.h View File

@@ -229,6 +229,8 @@ struct net_device {
229 229
 	struct refcnt refcnt;
230 230
 	/** List of network devices */
231 231
 	struct list_head list;
232
+	/** List of open network devices */
233
+	struct list_head open_list;
232 234
 	/** Name of this network device */
233 235
 	char name[8];
234 236
 	/** Underlying hardware device */
@@ -424,6 +426,7 @@ extern void netdev_irq ( struct net_device *netdev, int enable );
424 426
 extern struct net_device * find_netdev ( const char *name );
425 427
 extern struct net_device * find_netdev_by_location ( unsigned int bus_type,
426 428
 						     unsigned int location );
429
+extern struct net_device * last_opened_netdev ( void );
427 430
 extern int net_tx ( struct io_buffer *iobuf, struct net_device *netdev,
428 431
 		    struct net_protocol *net_protocol, const void *ll_dest );
429 432
 extern int net_rx ( struct io_buffer *iobuf, struct net_device *netdev,

+ 26
- 0
src/net/netdevice.c View File

@@ -45,6 +45,9 @@ static struct net_protocol net_protocols_end[0]
45 45
 /** List of network devices */
46 46
 struct list_head net_devices = LIST_HEAD_INIT ( net_devices );
47 47
 
48
+/** List of open network devices, in reverse order of opening */
49
+struct list_head open_net_devices = LIST_HEAD_INIT ( open_net_devices );
50
+
48 51
 /**
49 52
  * Record network device statistic
50 53
  *
@@ -368,6 +371,10 @@ int netdev_open ( struct net_device *netdev ) {
368 371
 
369 372
 	/* Mark as opened */
370 373
 	netdev->state |= NETDEV_OPEN;
374
+
375
+	/* Add to head of open devices list */
376
+	list_add ( &netdev->open_list, &open_net_devices );
377
+
371 378
 	return 0;
372 379
 }
373 380
 
@@ -393,6 +400,9 @@ void netdev_close ( struct net_device *netdev ) {
393 400
 
394 401
 	/* Mark as closed */
395 402
 	netdev->state &= ~NETDEV_OPEN;
403
+
404
+	/* Remove from open devices list */
405
+	list_del ( &netdev->open_list );
396 406
 }
397 407
 
398 408
 /**
@@ -462,6 +472,22 @@ struct net_device * find_netdev_by_location ( unsigned int bus_type,
462 472
 	return NULL;	
463 473
 }
464 474
 
475
+/**
476
+ * Get most recently opened network device
477
+ *
478
+ * @ret netdev		Most recently opened network device, or NULL
479
+ */
480
+struct net_device * last_opened_netdev ( void ) {
481
+	struct net_device *netdev;
482
+
483
+	list_for_each_entry ( netdev, &open_net_devices, open_list ) {
484
+		assert ( netdev->state & NETDEV_OPEN );
485
+		return netdev;
486
+	}
487
+
488
+	return NULL;
489
+}
490
+
465 491
 /**
466 492
  * Transmit network-layer packet
467 493
  *

Loading…
Cancel
Save