Browse Source

[netdevice] Reset network device index when last device is unregistered

When functioning as an EFI driver, drivers can be disconnected and
reconnected multiple times (e.g. via the EFI shell "connect" command,
or by running an executable such as ipxe.efi which will temporarily
disconnect existing drivers).

Minimise surprise by resetting the network device index to zero
whenever the last device is unregistered.  This is not foolproof, but
it does handle the common case of having all devices unregistered and
then reregistered in the original order.

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 10 years ago
parent
commit
c4af977271
1 changed files with 8 additions and 2 deletions
  1. 8
    2
      src/net/netdevice.c

+ 8
- 2
src/net/netdevice.c View File

50
 /** List of open network devices, in reverse order of opening */
50
 /** List of open network devices, in reverse order of opening */
51
 static struct list_head open_net_devices = LIST_HEAD_INIT ( open_net_devices );
51
 static struct list_head open_net_devices = LIST_HEAD_INIT ( open_net_devices );
52
 
52
 
53
+/** Network device index */
54
+static unsigned int netdev_index = 0;
55
+
53
 /** Network polling profiler */
56
 /** Network polling profiler */
54
 static struct profiler net_poll_profiler __profiler = { .name = "net.poll" };
57
 static struct profiler net_poll_profiler __profiler = { .name = "net.poll" };
55
 
58
 
597
  * devices.
600
  * devices.
598
  */
601
  */
599
 int register_netdev ( struct net_device *netdev ) {
602
 int register_netdev ( struct net_device *netdev ) {
600
-	static unsigned int ifindex = 0;
601
 	struct ll_protocol *ll_protocol = netdev->ll_protocol;
603
 	struct ll_protocol *ll_protocol = netdev->ll_protocol;
602
 	struct net_driver *driver;
604
 	struct net_driver *driver;
603
 	uint32_t seed;
605
 	uint32_t seed;
604
 	int rc;
606
 	int rc;
605
 
607
 
606
 	/* Record device index and create device name */
608
 	/* Record device index and create device name */
607
-	netdev->index = ifindex++;
609
+	netdev->index = netdev_index++;
608
 	if ( netdev->name[0] == '\0' ) {
610
 	if ( netdev->name[0] == '\0' ) {
609
 		snprintf ( netdev->name, sizeof ( netdev->name ), "net%d",
611
 		snprintf ( netdev->name, sizeof ( netdev->name ), "net%d",
610
 			   netdev->index );
612
 			   netdev->index );
764
 	DBGC ( netdev, "NETDEV %s unregistered\n", netdev->name );
766
 	DBGC ( netdev, "NETDEV %s unregistered\n", netdev->name );
765
 	list_del ( &netdev->list );
767
 	list_del ( &netdev->list );
766
 	netdev_put ( netdev );
768
 	netdev_put ( netdev );
769
+
770
+	/* Reset network device index if no devices remain */
771
+	if ( list_empty ( &net_devices ) )
772
+		netdev_index = 0;
767
 }
773
 }
768
 
774
 
769
 /** Enable or disable interrupts
775
 /** Enable or disable interrupts

Loading…
Cancel
Save