Squashed commit of the following:

net/netdev:  Add implementation of if_nametoindex() and if indextoname().
    net/pkt:  Raw AF_PACKET sockets now depend on CONFIG_NETDEV_IFINDEX.
    net/procfs:  Tweak to handle traversal of interfaces if CONFIG_NETDEV_IFINDEX is not defined.
    net/netdev.h:  Update netdev_findbyaddr() to use the assigned device index.
    Trivial typo fix
    net/netdev:  Add support for assigned an interface index to a device when it is regisgtered.
This commit is contained in:
Gregory Nutt
2018-06-25 12:08:10 -06:00
parent 68418262a5
commit 7c1394d814
12 changed files with 396 additions and 78 deletions
+12
View File
@@ -24,4 +24,16 @@ config NETDEV_WIRELESS_IOCTL
---help---
Enable support for wireless device ioctl() commands
config NETDEV_IFINDEX
bool "Enable IF index support"
default n
---help---
Enable support for references devices by an interface index.
This feature is automatically enabled when raw, PACKET sockets
are enabled.
When enabled, these option also enables the user interfaces:
if_nametoindex() and if_indextoname().
endmenu # Network Device Operations
+4
View File
@@ -41,6 +41,10 @@ NETDEV_CSRCS += netdev_count.c netdev_ifconf.c netdev_foreach.c
NETDEV_CSRCS += netdev_unregister.c netdev_carrier.c netdev_default.c
NETDEV_CSRCS += netdev_verify.c netdev_lladdrsize.c
ifeq ($(CONFIG_NETDEV_IFINDEX),y)
NETDEV_CSRCS += netdev_indextoname.c netdev_nametoindex.c
endif
# Include netdev build support
DEPPATH += --dep-path netdev
+88 -39
View File
@@ -47,6 +47,16 @@
#include <nuttx/net/ip.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/* If CONFIG_NETDEV_IFINDEX is enabled then there is limit to the number of
* devices that can be registered due to the nature of some static data.
*/
#define MAX_IFINDEX 32
/****************************************************************************
* Public Data
****************************************************************************/
@@ -70,6 +80,17 @@ extern "C"
EXTERN struct net_driver_s *g_netdevices;
#endif
#ifdef CONFIG_NETDEV_IFINDEX
/* The set of network devices that have been registered. This is used to
* assign a unique device index to the newly registered device.
*
* REVISIT: The width of g_nassigned limits the number of registered
* devices to 32 (MAX_IFINDEX).
*/
EXTERN uint32_t g_devset;
#endif
/****************************************************************************
* Public Types
****************************************************************************/
@@ -120,9 +141,6 @@ bool netdev_verify(FAR struct net_driver_s *dev);
* Returned Value:
* Pointer to driver on success; null on failure
*
* Assumptions:
* Called from normal user mode
*
****************************************************************************/
#if CONFIG_NSOCKET_DESCRIPTORS > 0
@@ -144,9 +162,6 @@ FAR struct net_driver_s *netdev_findbyname(FAR const char *ifname);
* Returned Value:
* 0:Enumeration completed 1:Enumeration terminated early by callback
*
* Assumptions:
* Called from normal user mode
*
****************************************************************************/
int netdev_foreach(netdev_callback_t callback, FAR void *arg);
@@ -165,9 +180,6 @@ int netdev_foreach(netdev_callback_t callback, FAR void *arg);
* Returned Value:
* Pointer to driver on success; null on failure
*
* Assumptions:
* Called from normal user mode
*
****************************************************************************/
#if CONFIG_NSOCKET_DESCRIPTORS > 0
@@ -190,9 +202,6 @@ FAR struct net_driver_s *netdev_findby_ipv4addr(in_addr_t lipaddr,
* Returned Value:
* Pointer to driver on success; null on failure
*
* Assumptions:
* Called from normal user mode
*
****************************************************************************/
#ifdef CONFIG_NET_IPv6
@@ -205,25 +214,80 @@ FAR struct net_driver_s *netdev_findby_ipv6addr(const net_ipv6addr_t lipaddr,
* Name: netdev_findbyindex
*
* Description:
* Find a previously registered network device by its position in the
* list of registered devices. NOTE that this function is not a safe way
* to enumerate network devices: There could be changes to the list of
* registered device causing a given index to be meaningless (unless, of
* course, the caller keeps the network locked).
* Find a previously registered network device by assigned interface index.
*
* Input Parameters:
* index - the index of the interface to file
* ifindex - The interface index
*
* Returned Value:
* Pointer to driver on success; NULL on failure. This function can only
* fail if there are fewer registered interfaces than could be indexed.
*
* Assumptions:
* Called from normal user mode
* Pointer to driver on success; NULL on failure. This function will return
* NULL only if there is no device corresponding to the provided index.
*
****************************************************************************/
FAR struct net_driver_s *netdev_findbyindex(int index);
FAR struct net_driver_s *netdev_findbyindex(int ifindex);
/****************************************************************************
* Name: netdev_nextindex
*
* Description:
* Return the interface index to the next valid device.
*
* Input Parameters:
* ifindex - The first interface index to check. Usually in a traversal
* this would be the previous inteface index plus 1.
*
* Returned Value:
* The interface index for the next network driver. -ENODEV is returned if
* there are no further devices with assigned interface indices.
*
****************************************************************************/
#ifdef CONFIG_NETDEV_IFINDEX
int netdev_nextindex(int ifindex);
#endif
/****************************************************************************
* Name: netdev_indextoname
*
* Description:
* The if_indextoname() function maps an interface index to its
* corresponding name.
*
* Input Parameters:
* ifname - Points to a buffer of at least IF_NAMESIZE bytes.
* if_indextoname() will place in this buffer the name of the
* interface with index ifindex.
*
* Returned Value:
* If ifindex is an interface index, then the function will return zero
* (OK). Otherwise, the function returns a negated errno value;
*
****************************************************************************/
#ifdef CONFIG_NETDEV_IFINDEX
int netdev_indextoname(unsigned int ifindex, FAR char *ifname);
#endif
/****************************************************************************
* Name: netdev_nametoindex
*
* Description:
* The if_nametoindex() function returns the interface index corresponding
* to name ifname.
*
* Input Parameters:
* ifname - The interface name
*
* Returned Value:
* The corresponding index if ifname is the name of an interface;
* otherwise, a negated errno value is returned.
*
****************************************************************************/
#ifdef CONFIG_NETDEV_IFINDEX
unsigned int netdev_nametoindex(FAR const char *ifname);
#endif
/****************************************************************************
* Name: netdev_default
@@ -244,9 +308,6 @@ FAR struct net_driver_s *netdev_findbyindex(int index);
* Returned Value:
* Pointer to default network driver on success; null on failure
*
* Assumptions:
* Called from normal user mode
*
****************************************************************************/
#if CONFIG_NSOCKET_DESCRIPTORS > 0
@@ -267,9 +328,6 @@ FAR struct net_driver_s *netdev_default(void);
* Returned Value:
* None
*
* Assumptions:
* Called from normal user mode
*
****************************************************************************/
#if CONFIG_NSOCKET_DESCRIPTORS > 0
@@ -291,9 +349,6 @@ void netdev_ipv4_txnotify(in_addr_t lipaddr, in_addr_t ripaddr);
* Returned Value:
* None
*
* Assumptions:
* Called from normal user mode
*
****************************************************************************/
#ifdef CONFIG_NET_IPv6
@@ -316,9 +371,6 @@ void netdev_ipv6_txnotify(FAR const net_ipv6addr_t lipaddr,
* Returned Value:
* None
*
* Assumptions:
* Called from normal user mode
*
****************************************************************************/
void netdev_txnotify_dev(FAR struct net_driver_s *dev);
@@ -335,9 +387,6 @@ void netdev_txnotify_dev(FAR struct net_driver_s *dev);
* Returned Value:
* The number of network devices
*
* Assumptions:
* Called from normal user mode
*
****************************************************************************/
#if CONFIG_NSOCKET_DESCRIPTORS > 0
+99 -16
View File
@@ -1,7 +1,7 @@
/****************************************************************************
* net/netdev/netdev_findbyindex.c
*
* Copyright (C) 2015 Gregory Nutt. All rights reserved.
* Copyright (C) 2015, 2018 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
@@ -38,9 +38,9 @@
****************************************************************************/
#include <nuttx/config.h>
#if defined(CONFIG_NET) && CONFIG_NSOCKET_DESCRIPTORS > 0
#include <string.h>
#include <assert.h>
#include <errno.h>
#include <nuttx/net/netdev.h>
@@ -48,6 +48,8 @@
#include "utils/utils.h"
#include "netdev/netdev.h"
#if CONFIG_NSOCKET_DESCRIPTORS > 0
/****************************************************************************
* Public Functions
****************************************************************************/
@@ -56,33 +58,65 @@
* Name: netdev_findbyindex
*
* Description:
* Find a previously registered network device by its position in the
* list of registered devices. NOTE that this function is not a safe way
* to enumerate network devices: There could be changes to the list of
* registered device causing a given index to be meaningless (unless, of
* course, the caller keeps the network locked).
* Find a previously registered network device by assigned interface index.
*
* Input Parameters:
* index - the index of the interface to file
* ifindex - The interface index
*
* Returned Value:
* Pointer to driver on success; NULL on failure. This function can only
* fail if there are fewer registered interfaces than could be indexed.
*
* Assumptions:
* Called from normal user mode
* Pointer to driver on success; NULL on failure. This function will return
* NULL only if there is no device corresponding to the provided index.
*
****************************************************************************/
FAR struct net_driver_s *netdev_findbyindex(int index)
FAR struct net_driver_s *netdev_findbyindex(int ifindex)
{
FAR struct net_driver_s *dev;
int i;
#ifdef CONFIG_NETDEV_IFINDEX
/* The bit index is the interface index minus one. Zero is reserved in
* POSIX to mean no interface index.
*/
DEBUGASSERT(ifindex > 0 && ifindex <= MAX_IFINDEX);
ifindex--;
if (ifindex < 0 || ifindex >= MAX_IFINDEX)
{
return NULL;
}
#endif
net_lock();
#ifdef CONFIG_NETDEV_IFINDEX
/* Check if this index has been assigned */
if ((g_devset & (1L << ifindex)) == 0)
{
/* This index has not been assigned */
net_unlock();
return NULL;
}
#endif
for (i = 0, dev = g_netdevices; dev; i++, dev = dev->flink)
{
if (i == index)
#ifdef CONFIG_NETDEV_IFINDEX
/* Check if the index matches the index assigned when the device was
* registered.
*/
if (dev->d_ifindex == ifindex)
#else
/* NOTE that this option is not a safe way to enumerate network
* devices: There could be changes to the list of registered device
* causing a given index to be meaningless (unless, of course, the
* caller keeps the network locked).
*/
if (i == ifindex)
#endif
{
net_unlock();
return dev;
@@ -93,4 +127,53 @@ FAR struct net_driver_s *netdev_findbyindex(int index)
return NULL;
}
#endif /* CONFIG_NET && CONFIG_NSOCKET_DESCRIPTORS */
/****************************************************************************
* Name: netdev_nextindex
*
* Description:
* Return the interface index to the next valid device.
*
* Input Parameters:
* ifindex - The first interface index to check. Usually in a traversal
* this would be the previous inteface index plus 1.
*
* Returned Value:
* The interface index for the next network driver. -ENODEV is returned if
* there are no further devices with assigned interface indices.
*
****************************************************************************/
#ifdef CONFIG_NETDEV_IFINDEX
int netdev_nextindex(int ifindex)
{
/* The bit index is the interface index minus one. Zero is reserved in
* POSIX to mean no interface index.
*/
DEBUGASSERT(ifindex > 0 && ifindex <= MAX_IFINDEX);
ifindex--;
if (ifindex >= 0 && ifindex < MAX_IFINDEX)
{
net_lock();
for (; ifindex < MAX_IFINDEX; ifindex++)
{
if ((g_devset & (1L << ifindex)) != 0)
{
/* NOTE that the index + 1 is returned. Zero is reserved to
* mean no-index in the POSIX standards.
*/
net_unlock();
return ifindex + 1;
}
}
net_unlock();
}
return -ENODEV;
}
#endif
#endif /* CONFIG_NSOCKET_DESCRIPTORS */
+72 -2
View File
@@ -95,6 +95,17 @@
struct net_driver_s *g_netdevices = NULL;
#ifdef CONFIG_NETDEV_IFINDEX
/* The set of network devices that have been registered. This is used to
* assign a unique device index to the newly registered device.
*
* REVISIT: The width of g_nassigned limits the number of registered
* devices to 32 (MAX_IFINDEX).
*/
uint32_t g_devset;
#endif
/****************************************************************************
* Private Functions
****************************************************************************/
@@ -146,6 +157,50 @@ static int find_devnum(FAR const char *devfmt)
return result;
}
/****************************************************************************
* Name: get_ifindex
*
* Description:
* Assign a unique interface index to the device.
*
* Input Parameters:
* None
*
* Returned Value:
* The interface index assigned to the device. -ENOSPC is returned if
* more the MAX_IFINDEX names have been assigned.
*
****************************************************************************/
#ifdef CONFIG_NETDEV_IFINDEX
static int get_ifindex(void)
{
int ndx;
/* Search for an unused index */
for (ndx = 0; ndx < MAX_IFINDEX; ndx++)
{
uint32_t bit = 1L << ndx;
if ((g_devset & bit) == 0)
{
/* Indicate that this index is in use */
g_devset |= bit;
/* NOTE that the index + 1 is returned. Zero is reserved to
* mean no-index in the POSIX standards.
*/
net_unlock();
return ndx + 1;
}
}
return -ENOSPC;
}
#endif
/****************************************************************************
* Public Functions
****************************************************************************/
@@ -177,6 +232,9 @@ int netdev_register(FAR struct net_driver_s *dev, enum net_lltype_e lltype)
FAR const char devfmt_str[IFNAMSIZ];
#endif
int devnum;
#ifdef CONFIG_NETDEV_IFINDEX
int ifindex;
#endif
if (dev != NULL)
{
@@ -285,12 +343,24 @@ int netdev_register(FAR struct net_driver_s *dev, enum net_lltype_e lltype)
dev->d_conncb = NULL;
dev->d_devcb = NULL;
/* We need exclusive access for the following operations */
net_lock();
#ifdef CONFIG_NETDEV_IFINDEX
ifindex = get_ifindex();
if (ifindex < 0)
{
return ifindex;
}
dev->d_ifindex = (uint8_t)ifindex;
#endif
/* Get the next available device number and assign a device name to
* the interface
*/
net_lock();
#ifdef CONFIG_NET_LOOPBACK
/* The local loopback device is a special case: There can be only one
* local loopback device so it is unnumbered.
+42 -2
View File
@@ -1,7 +1,7 @@
/****************************************************************************
* net/netdev/netdev_unregister.c
*
* Copyright (C) 2011, 2015 Gregory Nutt. All rights reserved.
* Copyright (C) 2011, 2015, 2018 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
@@ -65,6 +65,43 @@
# define NETDEV_FORMAT "eth%d"
#endif
/****************************************************************************
* Name: free_ifindex
*
* Description:
* Free a interface index to the assigned to device.
*
* Input Parameters:
* dev - Instance of device structure for the unregistered device.
*
* Returned Value:
* None.
*
****************************************************************************/
#ifdef CONFIG_NETDEV_IFINDEX
static inline void free_ifindex(int ndx)
{
uint32_t bit;
/* The bit index is the interface index minus one. Zero is reserved in
* POSIX to mean no interface index.
*/
DEBUGASSERT(ndx > 0 && ndx <= MAX_IFINDEX);
ndx--;
/* Set the bit in g_devset corresponding to the zero based index */
bit = 1 << ndx;
/* The bit should be in the set state */
DEBUGASSERT((g_devset & bit) != 0);
g_devset &= ~bit;
}
#endif
/****************************************************************************
* Public Functions
****************************************************************************/
@@ -124,6 +161,9 @@ int netdev_unregister(FAR struct net_driver_s *dev)
curr->flink = NULL;
}
#ifdef CONFIG_NETDEV_IFINDEX
free_ifindex(dev->d_ifindex);
#endif
net_unlock();
#ifdef CONFIG_NET_ETHERNET
@@ -135,7 +175,7 @@ int netdev_unregister(FAR struct net_driver_s *dev)
dev->d_mac.ether.ether_addr_octet[4],
dev->d_mac.ether.ether_addr_octet[5], dev->d_ifname);
#else
ninfo("Registered dev: %s\n", dev->d_ifname);
ninfo("Unregistered dev: %s\n", dev->d_ifname);
#endif
return OK;
}