2018-08-03 21:22:36 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* net/netlink/netlink_conn.c
|
|
|
|
*
|
2024-09-11 14:39:39 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
2021-02-19 12:45:37 +01:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2018-08-03 21:22:36 +02:00
|
|
|
*
|
2021-02-19 12:45:37 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2018-08-03 21:22:36 +02:00
|
|
|
*
|
2021-02-19 12:45:37 +01:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2018-08-03 21:22:36 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <arch/irq.h>
|
|
|
|
|
2019-11-04 16:02:14 +01:00
|
|
|
#include <nuttx/kmalloc.h>
|
2022-09-25 17:08:38 +02:00
|
|
|
#include <nuttx/queue.h>
|
2022-09-06 08:18:45 +02:00
|
|
|
#include <nuttx/mutex.h>
|
2018-08-03 21:22:36 +02:00
|
|
|
#include <nuttx/semaphore.h>
|
|
|
|
#include <nuttx/net/netconfig.h>
|
|
|
|
#include <nuttx/net/net.h>
|
2019-11-19 04:12:50 +01:00
|
|
|
#include <nuttx/net/netlink.h>
|
2018-08-03 21:22:36 +02:00
|
|
|
|
2019-11-18 16:48:14 +01:00
|
|
|
#include "utils/utils.h"
|
2018-08-03 21:22:36 +02:00
|
|
|
#include "netlink/netlink.h"
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_NETLINK
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-11-04 21:06:07 +01:00
|
|
|
/* The array containing all NetLink connections. */
|
2018-08-03 21:22:36 +02:00
|
|
|
|
2023-02-13 15:57:17 +01:00
|
|
|
#if CONFIG_NETLINK_PREALLOC_CONNS > 0
|
|
|
|
static struct netlink_conn_s
|
|
|
|
g_netlink_connections[CONFIG_NETLINK_PREALLOC_CONNS];
|
2022-01-04 04:53:55 +01:00
|
|
|
#endif
|
2018-08-03 21:22:36 +02:00
|
|
|
|
2019-11-04 21:06:07 +01:00
|
|
|
/* A list of all free NetLink connections */
|
2018-08-03 21:22:36 +02:00
|
|
|
|
|
|
|
static dq_queue_t g_free_netlink_connections;
|
2022-09-06 08:18:45 +02:00
|
|
|
static mutex_t g_free_lock = NXMUTEX_INITIALIZER;
|
2018-08-03 21:22:36 +02:00
|
|
|
|
2019-11-04 21:06:07 +01:00
|
|
|
/* A list of all allocated NetLink connections */
|
2018-08-03 21:22:36 +02:00
|
|
|
|
|
|
|
static dq_queue_t g_active_netlink_connections;
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-11-18 16:48:14 +01:00
|
|
|
/****************************************************************************
|
2019-11-28 21:20:40 +01:00
|
|
|
* Name: netlink_response_available
|
2019-11-18 16:48:14 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2019-11-28 21:20:40 +01:00
|
|
|
* Handle a Netlink response available notification.
|
2019-11-18 16:48:14 +01:00
|
|
|
*
|
2019-11-28 21:20:40 +01:00
|
|
|
* Input Parameters:
|
|
|
|
* Standard work handler parameters
|
2019-11-18 16:48:14 +01:00
|
|
|
*
|
2019-11-28 21:20:40 +01:00
|
|
|
* Returned Value:
|
|
|
|
* None
|
2019-11-18 16:48:14 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-11-28 21:20:40 +01:00
|
|
|
static void netlink_response_available(FAR void *arg)
|
2019-11-18 16:48:14 +01:00
|
|
|
{
|
2020-01-19 10:44:27 +01:00
|
|
|
DEBUGASSERT(arg != NULL);
|
2019-11-27 01:52:22 +01:00
|
|
|
|
2019-11-28 21:20:40 +01:00
|
|
|
/* wakeup the waiter */
|
2019-11-27 01:52:22 +01:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxsem_post(arg);
|
2019-11-27 01:52:22 +01:00
|
|
|
}
|
|
|
|
|
2024-03-21 05:14:29 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: netlink_get_terminator
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Generate one NLMSG_DONE response.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static FAR struct netlink_response_s *
|
|
|
|
netlink_get_terminator(FAR const struct nlmsghdr *req)
|
|
|
|
{
|
|
|
|
FAR struct netlink_response_s *resp;
|
|
|
|
FAR struct nlmsghdr *hdr;
|
|
|
|
|
|
|
|
/* Allocate the list terminator */
|
|
|
|
|
|
|
|
resp = kmm_zalloc(sizeof(struct netlink_response_s));
|
|
|
|
if (resp == NULL)
|
|
|
|
{
|
|
|
|
nerr("ERROR: Failed to allocate response terminator.\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize and send the list terminator */
|
|
|
|
|
|
|
|
hdr = &resp->msg;
|
|
|
|
hdr->nlmsg_len = sizeof(struct nlmsghdr);
|
|
|
|
hdr->nlmsg_type = NLMSG_DONE;
|
|
|
|
hdr->nlmsg_flags = req ? req->nlmsg_flags : 0;
|
|
|
|
hdr->nlmsg_seq = req ? req->nlmsg_seq : 0;
|
|
|
|
hdr->nlmsg_pid = req ? req->nlmsg_pid : 0;
|
|
|
|
|
|
|
|
/* Finally, return the response */
|
|
|
|
|
|
|
|
return resp;
|
|
|
|
}
|
|
|
|
|
2018-08-03 21:22:36 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: netlink_initialize()
|
|
|
|
*
|
|
|
|
* Description:
|
2020-04-07 19:44:23 +02:00
|
|
|
* Initialize the NetLink connection structures. Called once and only
|
2018-08-03 21:22:36 +02:00
|
|
|
* from the networking layer.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void netlink_initialize(void)
|
|
|
|
{
|
2023-02-13 15:57:17 +01:00
|
|
|
#if CONFIG_NETLINK_PREALLOC_CONNS > 0
|
2018-08-03 21:22:36 +02:00
|
|
|
int i;
|
|
|
|
|
2023-02-13 15:57:17 +01:00
|
|
|
for (i = 0; i < CONFIG_NETLINK_PREALLOC_CONNS; i++)
|
2018-08-03 21:22:36 +02:00
|
|
|
{
|
|
|
|
/* Mark the connection closed and move it to the free list */
|
|
|
|
|
2022-02-07 04:17:44 +01:00
|
|
|
dq_addlast(&g_netlink_connections[i].sconn.node,
|
2022-01-04 04:53:55 +01:00
|
|
|
&g_free_netlink_connections);
|
2018-08-03 21:22:36 +02:00
|
|
|
}
|
2022-01-04 04:53:55 +01:00
|
|
|
#endif
|
2018-08-03 21:22:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: netlink_alloc()
|
|
|
|
*
|
|
|
|
* Description:
|
2019-11-04 21:06:07 +01:00
|
|
|
* Allocate a new, uninitialized NetLink connection structure. This is
|
2018-08-03 21:22:36 +02:00
|
|
|
* normally something done by the implementation of the socket() API
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
FAR struct netlink_conn_s *netlink_alloc(void)
|
|
|
|
{
|
|
|
|
FAR struct netlink_conn_s *conn;
|
2023-02-13 15:57:17 +01:00
|
|
|
#if CONFIG_NETLINK_ALLOC_CONNS > 0
|
2022-01-04 04:53:55 +01:00
|
|
|
int i;
|
|
|
|
#endif
|
2018-08-03 21:22:36 +02:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
/* The free list is protected by a mutex. */
|
2018-08-03 21:22:36 +02:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_lock(&g_free_lock);
|
2023-02-13 15:57:17 +01:00
|
|
|
#if CONFIG_NETLINK_ALLOC_CONNS > 0
|
2022-01-04 04:53:55 +01:00
|
|
|
if (dq_peek(&g_free_netlink_connections) == NULL)
|
|
|
|
{
|
2023-02-13 15:57:17 +01:00
|
|
|
#if CONFIG_NETLINK_MAX_CONNS > 0
|
|
|
|
if (dq_count(&g_active_netlink_connections) +
|
2024-04-06 15:07:06 +02:00
|
|
|
CONFIG_NETLINK_ALLOC_CONNS > CONFIG_NETLINK_MAX_CONNS)
|
2023-02-13 15:57:17 +01:00
|
|
|
{
|
|
|
|
nxmutex_unlock(&g_free_lock);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
conn = kmm_zalloc(sizeof(*conn) * CONFIG_NETLINK_ALLOC_CONNS);
|
2022-01-04 04:53:55 +01:00
|
|
|
if (conn != NULL)
|
|
|
|
{
|
2023-02-13 15:57:17 +01:00
|
|
|
for (i = 0; i < CONFIG_NETLINK_ALLOC_CONNS; i++)
|
2022-01-04 04:53:55 +01:00
|
|
|
{
|
2022-02-07 04:17:44 +01:00
|
|
|
dq_addlast(&conn[i].sconn.node, &g_free_netlink_connections);
|
2022-01-04 04:53:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-04-10 15:03:34 +02:00
|
|
|
conn = (FAR struct netlink_conn_s *)
|
|
|
|
dq_remfirst(&g_free_netlink_connections);
|
2019-11-18 16:48:14 +01:00
|
|
|
if (conn != NULL)
|
2018-08-03 21:22:36 +02:00
|
|
|
{
|
|
|
|
/* Enqueue the connection into the active list */
|
|
|
|
|
2022-02-07 04:17:44 +01:00
|
|
|
dq_addlast(&conn->sconn.node, &g_active_netlink_connections);
|
2018-08-03 21:22:36 +02:00
|
|
|
}
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&g_free_lock);
|
2018-08-03 21:22:36 +02:00
|
|
|
return conn;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: netlink_free()
|
|
|
|
*
|
|
|
|
* Description:
|
2020-04-10 15:03:34 +02:00
|
|
|
* Free a NetLink connection structure that is no longer in use. This
|
|
|
|
* should be done by the implementation of close().
|
2018-08-03 21:22:36 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void netlink_free(FAR struct netlink_conn_s *conn)
|
|
|
|
{
|
2019-11-04 16:02:14 +01:00
|
|
|
FAR sq_entry_t *resp;
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
/* The free list is protected by a mutex. */
|
2018-08-03 21:22:36 +02:00
|
|
|
|
|
|
|
DEBUGASSERT(conn->crefs == 0);
|
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_lock(&g_free_lock);
|
2018-08-03 21:22:36 +02:00
|
|
|
|
|
|
|
/* Remove the connection from the active list */
|
|
|
|
|
2022-02-07 04:17:44 +01:00
|
|
|
dq_rem(&conn->sconn.node, &g_active_netlink_connections);
|
2018-08-03 21:22:36 +02:00
|
|
|
|
2019-11-04 16:02:14 +01:00
|
|
|
/* Free any unclaimed responses */
|
|
|
|
|
|
|
|
while ((resp = sq_remfirst(&conn->resplist)) != NULL)
|
|
|
|
{
|
|
|
|
kmm_free(resp);
|
|
|
|
}
|
|
|
|
|
2023-02-13 15:57:17 +01:00
|
|
|
/* If this is a preallocated or a batch allocated connection store it in
|
|
|
|
* the free connections list. Else free it.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if CONFIG_NETLINK_ALLOC_CONNS == 1
|
|
|
|
if (conn < g_netlink_connections || conn >= (g_netlink_connections +
|
|
|
|
CONFIG_NETLINK_PREALLOC_CONNS))
|
|
|
|
{
|
|
|
|
kmm_free(conn);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
2023-02-28 11:31:21 +01:00
|
|
|
memset(conn, 0, sizeof(*conn));
|
2023-02-13 15:57:17 +01:00
|
|
|
dq_addlast(&conn->sconn.node, &g_free_netlink_connections);
|
|
|
|
}
|
2018-08-03 21:22:36 +02:00
|
|
|
|
2022-09-06 08:18:45 +02:00
|
|
|
nxmutex_unlock(&g_free_lock);
|
2018-08-03 21:22:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: netlink_nextconn()
|
|
|
|
*
|
|
|
|
* Description:
|
2019-11-04 21:06:07 +01:00
|
|
|
* Traverse the list of allocated NetLink connections
|
2018-08-03 21:22:36 +02:00
|
|
|
*
|
|
|
|
* Assumptions:
|
2019-11-04 21:06:07 +01:00
|
|
|
* This function is called from NetLink device logic.
|
2018-08-03 21:22:36 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
FAR struct netlink_conn_s *netlink_nextconn(FAR struct netlink_conn_s *conn)
|
|
|
|
{
|
|
|
|
if (conn == NULL)
|
|
|
|
{
|
|
|
|
return (FAR struct netlink_conn_s *)g_active_netlink_connections.head;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-02-07 04:17:44 +01:00
|
|
|
return (FAR struct netlink_conn_s *)conn->sconn.node.flink;
|
2018-08-03 21:22:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-03 20:59:42 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: netlink_add_response
|
|
|
|
*
|
|
|
|
* Description:
|
2019-11-04 16:02:14 +01:00
|
|
|
* Add response data at the tail of the pending response list.
|
2019-11-03 20:59:42 +01:00
|
|
|
*
|
2019-11-18 16:48:14 +01:00
|
|
|
* Note: The network will be momentarily locked to support exclusive
|
|
|
|
* access to the pending response list.
|
2019-11-03 20:59:42 +01:00
|
|
|
*
|
2019-11-20 03:46:43 +01:00
|
|
|
* Input Parameters:
|
|
|
|
* handle - The handle previously provided to the sendto() implementation
|
|
|
|
* for the protocol. This is an opaque reference to the Netlink
|
|
|
|
* socket state structure.
|
|
|
|
* resp - The response to the request. The memory referenced by 'resp'
|
|
|
|
* must have been allocated via kmm_malloc(). It will be freed
|
|
|
|
* using kmm_free() after it has been consumed.
|
|
|
|
*
|
2019-11-03 20:59:42 +01:00
|
|
|
****************************************************************************/
|
|
|
|
|
2019-11-19 04:12:50 +01:00
|
|
|
void netlink_add_response(NETLINK_HANDLE handle,
|
2019-11-03 20:59:42 +01:00
|
|
|
FAR struct netlink_response_s *resp)
|
|
|
|
{
|
|
|
|
FAR struct netlink_conn_s *conn;
|
|
|
|
|
2020-04-13 14:49:52 +02:00
|
|
|
conn = handle;
|
|
|
|
DEBUGASSERT(conn != NULL && resp != NULL);
|
2019-11-18 16:48:14 +01:00
|
|
|
|
|
|
|
/* Add the response to the end of the FIFO list */
|
|
|
|
|
|
|
|
net_lock();
|
2019-11-04 16:02:14 +01:00
|
|
|
sq_addlast(&resp->flink, &conn->resplist);
|
2019-11-18 16:48:14 +01:00
|
|
|
|
|
|
|
/* Notify any waiters that a response is available */
|
|
|
|
|
2019-11-28 21:20:40 +01:00
|
|
|
netlink_notifier_signal(conn);
|
2019-11-18 16:48:14 +01:00
|
|
|
net_unlock();
|
2019-11-03 20:59:42 +01:00
|
|
|
}
|
|
|
|
|
2024-03-21 05:14:29 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: netlink_add_terminator
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Add one NLMSG_DONE response to handle.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* handle - The handle previously provided to the sendto() implementation
|
|
|
|
* for the protocol. This is an opaque reference to the Netlink
|
|
|
|
* socket state structure.
|
|
|
|
* req - The request message header.
|
|
|
|
* group - The broadcast group index, 0 for normal response.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero (OK) is returned if the terminator was successfully added to the
|
|
|
|
* response list.
|
|
|
|
* A negated error value is returned if an unexpected error occurred.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int netlink_add_terminator(NETLINK_HANDLE handle,
|
|
|
|
FAR const struct nlmsghdr *req, int group)
|
|
|
|
{
|
|
|
|
FAR struct netlink_response_s *resp;
|
|
|
|
|
|
|
|
resp = netlink_get_terminator(req);
|
|
|
|
if (resp == NULL)
|
|
|
|
{
|
|
|
|
return -ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (group > 0)
|
|
|
|
{
|
|
|
|
netlink_add_broadcast(group, resp);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
netlink_add_response(handle, resp);
|
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
2020-04-16 07:50:20 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: netlink_add_broadcast
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Add broadcast data to all interested netlink connections.
|
|
|
|
*
|
|
|
|
* Note: The network will be momentarily locked to support exclusive
|
|
|
|
* access to the pending response list.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* group - The broadcast group index.
|
|
|
|
* data - The broadcast data. The memory referenced by 'data'
|
|
|
|
* must have been allocated via kmm_malloc(). It will be freed
|
|
|
|
* using kmm_free() after it has been consumed.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void netlink_add_broadcast(int group, FAR struct netlink_response_s *data)
|
|
|
|
{
|
|
|
|
FAR struct netlink_conn_s *conn = NULL;
|
|
|
|
int first = 1;
|
|
|
|
|
|
|
|
DEBUGASSERT(data != NULL);
|
|
|
|
|
|
|
|
net_lock();
|
|
|
|
|
|
|
|
while ((conn = netlink_nextconn(conn)) != NULL)
|
|
|
|
{
|
2020-04-16 07:50:20 +02:00
|
|
|
if ((conn->groups & (1 << (group - 1))) == 0)
|
2020-04-16 07:50:20 +02:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Duplicate the package except the first loop */
|
|
|
|
|
|
|
|
if (!first)
|
|
|
|
{
|
|
|
|
FAR struct netlink_response_s *tmp;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
len = sizeof(sq_entry_t) + data->msg.nlmsg_len;
|
|
|
|
tmp = kmm_malloc(len);
|
|
|
|
if (tmp == NULL)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(tmp, data, len);
|
|
|
|
data = tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
first = 0;
|
|
|
|
|
|
|
|
/* Add the response to the end of the FIFO list */
|
|
|
|
|
|
|
|
sq_addlast(&data->flink, &conn->resplist);
|
|
|
|
|
|
|
|
/* Notify any waiters that a response is available */
|
|
|
|
|
|
|
|
netlink_notifier_signal(conn);
|
|
|
|
}
|
|
|
|
|
|
|
|
net_unlock();
|
|
|
|
|
|
|
|
/* Drop the package if nobody is interested in */
|
|
|
|
|
|
|
|
if (first)
|
|
|
|
{
|
|
|
|
kmm_free(data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-03 20:59:42 +01:00
|
|
|
/****************************************************************************
|
2019-11-18 16:48:14 +01:00
|
|
|
* Name: netlink_tryget_response
|
2019-11-03 20:59:42 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2019-11-04 16:02:14 +01:00
|
|
|
* Return the next response from the head of the pending response list.
|
|
|
|
* Responses are returned one-at-a-time in FIFO order.
|
2019-11-03 20:59:42 +01:00
|
|
|
*
|
2019-11-18 16:48:14 +01:00
|
|
|
* Note: The network will be momentarily locked to support exclusive
|
|
|
|
* access to the pending response list.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* The next response from the head of the pending response list is
|
|
|
|
* returned. NULL will be returned if the pending response list is
|
|
|
|
* empty
|
2019-11-03 20:59:42 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-11-18 16:48:14 +01:00
|
|
|
FAR struct netlink_response_s *
|
2020-04-13 14:49:52 +02:00
|
|
|
netlink_tryget_response(FAR struct netlink_conn_s *conn)
|
2019-11-03 20:59:42 +01:00
|
|
|
{
|
2019-11-18 16:48:14 +01:00
|
|
|
FAR struct netlink_response_s *resp;
|
2019-11-03 20:59:42 +01:00
|
|
|
|
2020-04-13 14:49:52 +02:00
|
|
|
DEBUGASSERT(conn != NULL);
|
2019-11-03 20:59:42 +01:00
|
|
|
|
2019-11-04 16:02:14 +01:00
|
|
|
/* Return the response at the head of the pending response list (may be
|
|
|
|
* NULL).
|
2019-11-03 20:59:42 +01:00
|
|
|
*/
|
|
|
|
|
2019-11-18 16:48:14 +01:00
|
|
|
net_lock();
|
|
|
|
resp = (FAR struct netlink_response_s *)sq_remfirst(&conn->resplist);
|
|
|
|
net_unlock();
|
|
|
|
|
|
|
|
return resp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: netlink_get_response
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Return the next response from the head of the pending response list.
|
|
|
|
* Responses are returned one-at-a-time in FIFO order.
|
|
|
|
*
|
|
|
|
* Note: The network will be momentarily locked to support exclusive
|
|
|
|
* access to the pending response list.
|
|
|
|
*
|
2023-12-19 11:38:44 +01:00
|
|
|
* Input Parameters:
|
|
|
|
* conn - The Netlink connection
|
|
|
|
* response - The next response from the head of the pending response list
|
|
|
|
* is returned. This function will block until a response is
|
|
|
|
* received if the pending response list is empty. NULL will be
|
|
|
|
* returned only in the event of a failure.
|
|
|
|
*
|
2019-11-18 16:48:14 +01:00
|
|
|
* Returned Value:
|
2023-12-19 11:38:44 +01:00
|
|
|
* Zero (OK) is returned if the notification was successfully set up.
|
|
|
|
* A negated error value is returned if an unexpected error occurred
|
2019-11-18 16:48:14 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2023-12-19 11:38:44 +01:00
|
|
|
int netlink_get_response(FAR struct netlink_conn_s *conn,
|
|
|
|
FAR struct netlink_response_s **response)
|
2019-11-18 16:48:14 +01:00
|
|
|
{
|
2023-12-19 11:38:44 +01:00
|
|
|
int ret = OK;
|
2019-11-18 16:48:14 +01:00
|
|
|
|
2020-04-13 14:49:52 +02:00
|
|
|
DEBUGASSERT(conn != NULL);
|
2019-11-18 16:48:14 +01:00
|
|
|
|
|
|
|
/* Loop, until a response is received. A loop is used because in the case
|
|
|
|
* of multiple waiters, all waiters will be awakened, but only the highest
|
|
|
|
* priority waiter will get the response.
|
|
|
|
*/
|
|
|
|
|
|
|
|
net_lock();
|
2023-12-19 11:38:44 +01:00
|
|
|
while ((*response = netlink_tryget_response(conn)) == NULL)
|
2019-11-18 16:48:14 +01:00
|
|
|
{
|
2019-11-28 21:20:40 +01:00
|
|
|
sem_t waitsem;
|
2019-11-18 16:48:14 +01:00
|
|
|
|
2019-11-28 21:20:40 +01:00
|
|
|
/* Set up a semaphore to notify us when a response is queued. */
|
2019-11-18 16:48:14 +01:00
|
|
|
|
2020-05-02 16:30:58 +02:00
|
|
|
nxsem_init(&waitsem, 0, 0);
|
2019-11-18 16:48:14 +01:00
|
|
|
|
2019-11-28 21:20:40 +01:00
|
|
|
/* Set up a notifier to post the semaphore when a response is
|
|
|
|
* received.
|
|
|
|
*/
|
|
|
|
|
|
|
|
ret = netlink_notifier_setup(netlink_response_available, conn,
|
|
|
|
&waitsem);
|
2019-11-18 16:48:14 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2019-11-28 21:20:40 +01:00
|
|
|
nerr("ERROR: netlink_notifier_setup() failed: %d\n", ret);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-11-28 23:14:59 +01:00
|
|
|
/* Wait for a response to be queued */
|
|
|
|
|
2023-12-19 11:38:44 +01:00
|
|
|
ret = net_sem_wait(&waitsem);
|
2019-11-18 16:48:14 +01:00
|
|
|
}
|
|
|
|
|
2019-11-28 21:20:40 +01:00
|
|
|
/* Clean-up the semaphore */
|
|
|
|
|
2020-05-02 16:30:58 +02:00
|
|
|
nxsem_destroy(&waitsem);
|
2019-11-28 21:20:40 +01:00
|
|
|
netlink_notifier_teardown(conn);
|
|
|
|
|
|
|
|
/* Check for any failures */
|
2019-11-18 16:48:14 +01:00
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2019-11-28 21:20:40 +01:00
|
|
|
break;
|
2019-11-18 16:48:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-28 21:20:40 +01:00
|
|
|
net_unlock();
|
2023-12-19 11:38:44 +01:00
|
|
|
return ret;
|
2019-11-03 20:59:42 +01:00
|
|
|
}
|
|
|
|
|
2019-11-27 01:52:22 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: netlink_check_response
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Return true is a response is pending now.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* True: A response is available; False; No response is available.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2020-04-13 14:49:52 +02:00
|
|
|
bool netlink_check_response(FAR struct netlink_conn_s *conn)
|
2019-11-27 01:52:22 +01:00
|
|
|
{
|
2020-04-13 14:49:52 +02:00
|
|
|
DEBUGASSERT(conn != NULL);
|
2019-11-27 01:52:22 +01:00
|
|
|
|
|
|
|
/* Check if the response is available. It is not necessary to lock the
|
|
|
|
* network because the sq_peek() is an atomic operation.
|
|
|
|
*/
|
|
|
|
|
|
|
|
return (sq_peek(&conn->resplist) != NULL);
|
|
|
|
}
|
|
|
|
|
2018-08-03 21:22:36 +02:00
|
|
|
#endif /* CONFIG_NET_NETLINK */
|