net/netlink: replace net_lock with netlink_lock

remove the use of net_lock in the netlink module and decouple it from
other network modules.

Signed-off-by: zhanghongyu <zhanghongyu@xiaomi.com>
This commit is contained in:
zhanghongyu 2025-11-07 17:01:04 +08:00 committed by Xiang Xiao
parent 3778834527
commit eb5c8f4e9c
4 changed files with 78 additions and 36 deletions

View file

@ -631,6 +631,26 @@ void netlink_conntrack_notify(uint8_t type, uint8_t domain,
#endif /* CONFIG_NETLINK_NETFILTER */
/****************************************************************************
* Name: netlink_lock
*
* Description:
* Take the global netlink lock
*
****************************************************************************/
void netlink_lock(void);
/****************************************************************************
* Name: netlink_unlock
*
* Description:
* Release the global netlink lock
*
****************************************************************************/
void netlink_unlock(void);
#undef EXTERN
#ifdef __cplusplus
}

View file

@ -70,6 +70,10 @@ NET_BUFPOOL_DECLARE(g_netlink_connections, sizeof(struct netlink_conn_s),
static dq_queue_t g_active_netlink_connections;
/* Global protection lock for netlink */
static rmutex_t g_netlink_lock = NXRMUTEX_INITIALIZER;
/****************************************************************************
* Private Functions
****************************************************************************/
@ -256,13 +260,13 @@ void netlink_add_response(NETLINK_HANDLE handle,
/* Add the response to the end of the FIFO list */
net_lock();
netlink_lock();
sq_addlast(&resp->flink, &conn->resplist);
/* Notify any waiters that a response is available */
netlink_notifier_signal(conn);
net_unlock();
netlink_unlock();
}
/****************************************************************************
@ -332,7 +336,7 @@ void netlink_add_broadcast(int group, FAR struct netlink_response_s *data)
DEBUGASSERT(data != NULL);
net_lock();
netlink_lock();
while ((conn = netlink_nextconn(conn)) != NULL)
{
@ -370,7 +374,7 @@ void netlink_add_broadcast(int group, FAR struct netlink_response_s *data)
netlink_notifier_signal(conn);
}
net_unlock();
netlink_unlock();
/* Drop the package if nobody is interested in */
@ -408,9 +412,9 @@ netlink_tryget_response(FAR struct netlink_conn_s *conn)
* NULL).
*/
net_lock();
netlink_lock();
resp = (FAR struct netlink_response_s *)sq_remfirst(&conn->resplist);
net_unlock();
netlink_unlock();
return resp;
}
@ -450,7 +454,7 @@ int netlink_get_response(FAR struct netlink_conn_s *conn,
* priority waiter will get the response.
*/
net_lock();
netlink_lock();
while ((*response = netlink_tryget_response(conn)) == NULL)
{
sem_t waitsem;
@ -471,10 +475,14 @@ int netlink_get_response(FAR struct netlink_conn_s *conn,
}
else
{
unsigned int count;
/* Wait for a response to be queued */
tls_cleanup_push(tls_get_info(), netlink_notifier_teardown, conn);
nxrmutex_breaklock(&g_netlink_lock, &count);
ret = net_sem_wait(&waitsem);
nxrmutex_restorelock(&g_netlink_lock, count);
tls_cleanup_pop(tls_get_info(), 0);
}
@ -491,7 +499,7 @@ int netlink_get_response(FAR struct netlink_conn_s *conn,
}
}
net_unlock();
netlink_unlock();
return ret;
}
@ -517,4 +525,30 @@ bool netlink_check_response(FAR struct netlink_conn_s *conn)
return (sq_peek(&conn->resplist) != NULL);
}
/****************************************************************************
* Name: netlink_lock
*
* Description:
* Take the global netlink lock
*
****************************************************************************/
void netlink_lock(void)
{
nxrmutex_lock(&g_netlink_lock);
}
/****************************************************************************
* Name: netlink_unlock
*
* Description:
* Release the global netlink lock
*
****************************************************************************/
void netlink_unlock(void)
{
nxrmutex_unlock(&g_netlink_lock);
}
#endif /* CONFIG_NET_NETLINK */

View file

@ -505,9 +505,7 @@ static int netlink_get_devlist(NETLINK_HANDLE handle,
info.handle = handle;
info.req = req;
net_lock();
ret = netdev_foreach(netlink_device_callback, &info);
net_unlock();
if (ret < 0)
{
return ret;
@ -538,10 +536,8 @@ static size_t netlink_fill_arptable(
* the ARP table into the allocated memory.
*/
net_lock();
ncopied = arp_snapshot((FAR struct arpreq *)(*entry)->payload.data,
CONFIG_NET_ARPTAB_SIZE);
net_unlock();
/* Now we have the real number of valid entries in the ARP table and
* we can trim the allocation.
@ -591,11 +587,9 @@ static size_t netlink_fill_nbtable(
* copy the Neighbor table into the allocated memory.
*/
net_lock();
ncopied = neighbor_snapshot(
(FAR struct neighbor_entry_s *)(*entry)->payload.data,
CONFIG_NET_IPv6_NCONF_ENTRIES);
net_unlock();
/* Now we have the real number of valid entries in the Neighbor table
* and we can trim the allocation.
@ -1016,21 +1010,20 @@ static int netlink_new_ipv4addr(NETLINK_HANDLE handle,
return -EINVAL;
}
net_lock();
dev = netdev_findbyindex(ifm->ifa_index);
if (dev == NULL)
{
net_unlock();
return -ENODEV;
}
netdev_lock(dev);
dev->d_ipaddr = nla_get_in_addr(tb[IFA_LOCAL]);
dev->d_netmask = make_mask(ifm->ifa_prefixlen);
netlink_device_notify_ipaddr(dev, RTM_NEWADDR, AF_INET, &dev->d_ipaddr,
ifm->ifa_prefixlen);
net_unlock();
netdev_unlock(dev);
return OK;
}
@ -1066,15 +1059,14 @@ static int netlink_new_ipv6addr(NETLINK_HANDLE handle,
return -EINVAL;
}
net_lock();
dev = netdev_findbyindex(ifm->ifa_index);
if (dev == NULL)
{
net_unlock();
return -ENODEV;
}
netdev_lock(dev);
ret = netdev_ipv6_add(dev, nla_data(tb[IFA_LOCAL]), ifm->ifa_prefixlen);
if (ret == OK)
{
@ -1082,7 +1074,7 @@ static int netlink_new_ipv6addr(NETLINK_HANDLE handle,
nla_data(tb[IFA_LOCAL]), ifm->ifa_prefixlen);
}
net_unlock();
netdev_unlock(dev);
return ret;
}
@ -1113,18 +1105,17 @@ static int netlink_del_ipv4addr(NETLINK_HANDLE handle,
return ret;
}
net_lock();
dev = netdev_findbyindex(ifm->ifa_index);
if (dev == NULL)
{
net_unlock();
return -ENODEV;
}
netdev_lock(dev);
if (tb[IFA_LOCAL] && dev->d_ipaddr != nla_get_in_addr(tb[IFA_LOCAL]))
{
net_unlock();
netdev_unlock(dev);
return -EADDRNOTAVAIL;
}
@ -1132,7 +1123,7 @@ static int netlink_del_ipv4addr(NETLINK_HANDLE handle,
net_ipv4_mask2pref(dev->d_netmask));
dev->d_ipaddr = 0;
net_unlock();
netdev_unlock(dev);
return OK;
}
@ -1168,18 +1159,17 @@ static int netlink_del_ipv6addr(NETLINK_HANDLE handle,
return -EINVAL;
}
net_lock();
dev = netdev_findbyindex(ifm->ifa_index);
if (dev == NULL)
{
net_unlock();
return -ENODEV;
}
netdev_lock(dev);
if (!NETDEV_IS_MY_V6ADDR(dev, nla_data(tb[IFA_LOCAL])))
{
net_unlock();
netdev_unlock(dev);
return -EADDRNOTAVAIL;
}
@ -1190,7 +1180,7 @@ static int netlink_del_ipv6addr(NETLINK_HANDLE handle,
nla_data(tb[IFA_LOCAL]), ifm->ifa_prefixlen);
}
net_unlock();
netdev_unlock(dev);
return ret;
}
@ -1266,9 +1256,7 @@ static int netlink_get_addr(NETLINK_HANDLE handle,
info.handle = handle;
info.req = req;
net_lock();
ret = netdev_foreach(netlink_addr_callback, &info);
net_unlock();
if (ret < 0)
{
return ret;

View file

@ -428,7 +428,7 @@ static void netlink_response_available(FAR void *arg)
* condition?
*/
net_lock();
netlink_lock();
if (conn->fds != NULL)
{
@ -445,7 +445,7 @@ static void netlink_response_available(FAR void *arg)
conn->fds = NULL;
net_unlock();
netlink_unlock();
}
/****************************************************************************
@ -491,7 +491,7 @@ static int netlink_poll(FAR struct socket *psock, FAR struct pollfd *fds,
* immediately (maybe).
*/
net_lock();
netlink_lock();
if (netlink_check_response(conn))
{
revents |= POLLIN;
@ -504,7 +504,7 @@ static int netlink_poll(FAR struct socket *psock, FAR struct pollfd *fds,
poll_notify(&fds, 1, revents);
if (fds->revents != 0)
{
net_unlock();
netlink_unlock();
return OK;
}
@ -521,7 +521,7 @@ static int netlink_poll(FAR struct socket *psock, FAR struct pollfd *fds,
if (conn->fds != NULL)
{
nerr("ERROR: Multiple polls() on socket not supported.\n");
net_unlock();
netlink_unlock();
return -EBUSY;
}
@ -538,7 +538,7 @@ static int netlink_poll(FAR struct socket *psock, FAR struct pollfd *fds,
}
}
net_unlock();
netlink_unlock();
}
else
{