Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[components][sal]add netdev ifindex feature #9418

Merged
merged 1 commit into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions components/net/netdev/include/netdev.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ struct netdev
netdev_callback_fn status_callback; /* network interface device flags change callback */
netdev_callback_fn addr_callback; /* network interface device address information change callback */

int ifindex; /* network interface device ifindex */

#ifdef RT_USING_SAL
void *sal_user_data; /* user-specific data for SAL */
#endif /* RT_USING_SAL */
Expand Down Expand Up @@ -151,6 +153,7 @@ int netdev_unregister(struct netdev *netdev);
struct netdev *netdev_get_first_by_flags(uint16_t flags);
struct netdev *netdev_get_by_ipaddr(ip_addr_t *ip_addr);
struct netdev *netdev_get_by_name(const char *name);
struct netdev *netdev_get_by_ifindex(int ifindex);
#ifdef RT_USING_SAL
struct netdev *netdev_get_by_family(int family);
int netdev_family_get(struct netdev *netdev);
Expand Down
40 changes: 40 additions & 0 deletions components/net/netdev/src/netdev.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ struct netdev *netdev_default = RT_NULL;
static netdev_callback_fn g_netdev_register_callback = RT_NULL;
static netdev_callback_fn g_netdev_default_change_callback = RT_NULL;
static RT_DEFINE_SPINLOCK(_spinlock);
static int netdev_num;

/**
* This function will register network interface device and
Expand Down Expand Up @@ -112,6 +113,9 @@ int netdev_register(struct netdev *netdev, const char *name, void *user_data)
rt_slist_append(&(netdev_list->list), &(netdev->list));
}

netdev_num++;
netdev->ifindex = netdev_num;

rt_spin_unlock(&_spinlock);

if (netdev_default == RT_NULL)
Expand Down Expand Up @@ -326,6 +330,42 @@ struct netdev *netdev_get_by_name(const char *name)
return RT_NULL;
}

/**
* This function will get network interface device
* in network interface device list by netdev ifindex.
*
* @param ifindex the ifindex of network interface device
*
* @return != NULL: network interface device object
* NULL: get failed
*/
struct netdev *netdev_get_by_ifindex(int ifindex)
{
rt_slist_t *node = RT_NULL;
struct netdev *netdev = RT_NULL;

if (netdev_list == RT_NULL)
{
return RT_NULL;
}

rt_spin_lock(&_spinlock);
zmshahaha marked this conversation as resolved.
Show resolved Hide resolved

for (node = &(netdev_list->list); node; node = rt_slist_next(node))
{
netdev = rt_slist_entry(node, struct netdev, list);
if (netdev && (netdev->ifindex == ifindex))
{
rt_spin_unlock(&_spinlock);
return netdev;
}
}

rt_spin_unlock(&_spinlock);

return RT_NULL;
}

#ifdef RT_USING_SAL
/**
* This function will get the first network interface device
Expand Down
10 changes: 10 additions & 0 deletions components/net/sal/src/sal_socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -1469,6 +1469,16 @@ int sal_ioctlsocket(int socket, long cmd, void *arg)
ifconf_tmp->ifc_ifcu.ifcu_buf = ifconf_tmp->ifc_ifcu.ifcu_buf - sizeof(struct sal_ifreq) * count_size;
return 0;
}
case SIOCGIFINDEX:
{
netdev = netdev_get_by_name(ifr->ifr_ifrn.ifrn_name);
if (netdev)
{
ifr->ifr_ifru.ifru_ivalue = netdev->ifindex;
return 0;
}
return -ENODEV;
}
default:
break;
}
Expand Down
Loading