Skip to content

Commit

Permalink
Squashed commit of the following:
Browse files Browse the repository at this point in the history
    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.
  • Loading branch information
gregory-nutt committed Jun 25, 2018
1 parent 6841826 commit 7c1394d
Show file tree
Hide file tree
Showing 12 changed files with 395 additions and 77 deletions.
35 changes: 35 additions & 0 deletions include/net/if.h
Original file line number Diff line number Diff line change
Expand Up @@ -247,4 +247,39 @@ struct ifconf
* Public Function Prototypes
*******************************************************************************************/

/*******************************************************************************************
* Name: if_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, zero.
*
*******************************************************************************************/

unsigned int if_nametoindex(FAR const char *ifname);

/*******************************************************************************************
* Name: if_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 the value supplied by
* ifname. Otherwise, the function returns a NULL pointer and sets errno to indicate the
* error.
*
*******************************************************************************************/

FAR char *if_indextoname(unsigned int ifindex, FAR char *ifname);

#endif /* __INCLUDE_NET_IF_H */
7 changes: 6 additions & 1 deletion include/nuttx/net/netdev.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Defines architecture-specific device driver interfaces to the NuttX
* network.
*
* Copyright (C) 2007, 2009, 2011-2017 Gregory Nutt. All rights reserved.
* Copyright (C) 2007, 2009, 2011-2018 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Derived largely from portions of uIP with has a similar BSD-styple license:
Expand Down Expand Up @@ -241,6 +241,11 @@ struct net_driver_s

uint8_t d_lltype; /* See enum net_lltype_e */
uint8_t d_llhdrlen; /* Link layer header size */

#ifdef CONFIG_NETDEV_IFINDEX
uint8_t d_ifindex; /* Device index */
#endif

uint16_t d_mtu; /* Maximum packet size */
#ifdef CONFIG_NET_TCP
uint16_t d_recvwndo; /* TCP receive window size */
Expand Down
12 changes: 12 additions & 0 deletions net/netdev/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 4 additions & 0 deletions net/netdev/Make.defs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
125 changes: 87 additions & 38 deletions net/netdev/netdev.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
****************************************************************************/
Expand All @@ -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
****************************************************************************/
Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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.
* Pointer to driver on success; NULL on failure. This function will return
* NULL only if there is no device corresponding to the provided index.
*
* Assumptions:
* Called from normal user mode
****************************************************************************/

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.
*
****************************************************************************/

FAR struct net_driver_s *netdev_findbyindex(int index);
#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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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);
Expand All @@ -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
Expand Down
Loading

0 comments on commit 7c1394d

Please sign in to comment.