[netdevice] Separate concept of scope ID from network device name index

The network device index currently serves two purposes: acting as a
sequential index for network device names ("net0", "net1", etc), and
acting as an opaque unique integer identifier used in socket address
scope IDs.

There is no particular need for these usages to be linked, and it can
lead to situations in which devices are named unexpectedly.  For
example: if a system has two network devices "net0" and "net1", a VLAN
is created as "net1-42", and then a USB NIC is connected, then the USB
NIC will be named "net3" rather than the expected "net2" since the
VLAN device "net1-42" will have consumed an index.

Separate the usages: rename the "index" field to "scope_id" (matching
its one and only use case), and assign the name without reference to
the scope ID by finding the first unused name.  For consistency,
assign the scope ID by similarly finding the first unused scope ID.

Signed-off-by: Michael Brown <mcb30@ipxe.org>
This commit is contained in:
Michael Brown
2023-01-14 00:09:20 +00:00
parent ab19546386
commit 47af48012e
10 changed files with 36 additions and 33 deletions
+3 -3
View File
@@ -356,8 +356,8 @@ struct net_device {
struct list_head list;
/** List of open network devices */
struct list_head open_list;
/** Index of this network device */
unsigned int index;
/** Scope ID */
unsigned int scope_id;
/** Name of this network device */
char name[NETDEV_NAME_LEN];
/** Underlying hardware device */
@@ -726,7 +726,7 @@ extern void netdev_close ( struct net_device *netdev );
extern void unregister_netdev ( struct net_device *netdev );
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_index ( unsigned int index );
extern struct net_device * find_netdev_by_scope_id ( unsigned int scope_id );
extern struct net_device * find_netdev_by_location ( unsigned int bus_type,
unsigned int location );
extern struct net_device *