2017-03-31 16:58:14 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* net/usrsock/usrsock_poll.c
|
|
|
|
*
|
2024-09-11 14:39:39 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
2021-11-15 07:53:26 +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
|
2017-03-31 16:58:14 +02:00
|
|
|
*
|
2021-11-15 07:53:26 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2017-03-31 16:58:14 +02:00
|
|
|
*
|
2021-11-15 07:53:26 +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.
|
2017-03-31 16:58:14 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
2019-05-22 02:57:54 +02:00
|
|
|
#if defined(CONFIG_NET) && defined(CONFIG_NET_USRSOCK)
|
2017-03-31 16:58:14 +02:00
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <poll.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
#include <arch/irq.h>
|
|
|
|
|
|
|
|
#include <sys/socket.h>
|
2017-10-25 15:31:14 +02:00
|
|
|
#include <nuttx/semaphore.h>
|
2017-03-31 16:58:14 +02:00
|
|
|
#include <nuttx/net/net.h>
|
|
|
|
#include <nuttx/net/usrsock.h>
|
|
|
|
|
|
|
|
#include "usrsock/usrsock.h"
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-08-25 15:17:57 +02:00
|
|
|
static uint16_t poll_event(FAR struct net_driver_s *dev,
|
2017-03-31 16:58:14 +02:00
|
|
|
FAR void *pvpriv, uint16_t flags)
|
|
|
|
{
|
2022-08-25 15:17:57 +02:00
|
|
|
FAR struct usrsock_poll_s *info = pvpriv;
|
2022-08-25 14:15:56 +02:00
|
|
|
FAR struct usrsock_conn_s *conn = info->conn;
|
2017-03-31 16:58:14 +02:00
|
|
|
pollevent_t eventset = 0;
|
|
|
|
|
2022-08-25 14:15:56 +02:00
|
|
|
DEBUGASSERT(!info || info->fds);
|
2017-03-31 16:58:14 +02:00
|
|
|
|
2019-07-03 02:02:23 +02:00
|
|
|
if (info == NULL)
|
|
|
|
{
|
|
|
|
return flags;
|
|
|
|
}
|
2017-03-31 16:58:14 +02:00
|
|
|
|
|
|
|
if (flags & USRSOCK_EVENT_ABORT)
|
|
|
|
{
|
|
|
|
ninfo("socket aborted.\n");
|
|
|
|
|
|
|
|
/* Socket forcefully terminated. */
|
|
|
|
|
|
|
|
eventset |= (POLLERR | POLLHUP);
|
|
|
|
}
|
2017-07-31 15:38:24 +02:00
|
|
|
else if ((flags & USRSOCK_EVENT_CONNECT_READY) && !conn->connected)
|
2017-03-31 16:58:14 +02:00
|
|
|
{
|
|
|
|
ninfo("socket connect failed.\n");
|
|
|
|
|
|
|
|
/* Non-blocking connect failed. */
|
|
|
|
|
|
|
|
eventset |= (POLLERR | POLLHUP);
|
|
|
|
}
|
|
|
|
else if (flags & USRSOCK_EVENT_REMOTE_CLOSED)
|
|
|
|
{
|
|
|
|
ninfo("remote closed.\n");
|
|
|
|
|
|
|
|
/* Remote closed. */
|
|
|
|
|
|
|
|
eventset |= (POLLHUP | POLLIN);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Check data events. */
|
|
|
|
|
|
|
|
if (flags & USRSOCK_EVENT_RECVFROM_AVAIL)
|
|
|
|
{
|
|
|
|
ninfo("socket recv avail.\n");
|
|
|
|
|
|
|
|
eventset |= POLLIN;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & USRSOCK_EVENT_SENDTO_READY)
|
|
|
|
{
|
|
|
|
ninfo("socket send ready.\n");
|
|
|
|
|
|
|
|
eventset |= POLLOUT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-19 05:08:57 +02:00
|
|
|
poll_notify(&info->fds, 1, eventset);
|
2017-03-31 16:58:14 +02:00
|
|
|
|
|
|
|
return flags;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: usrsock_poll
|
2017-03-31 16:58:14 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Setup to monitor events on an usrsock socket
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* psock - An instance of the internal socket structure.
|
|
|
|
* fds - The structure describing the events to be monitored.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* 0: Success; Negated errno on failure
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-04-02 07:28:21 +02:00
|
|
|
static int usrsock_pollsetup(FAR struct socket *psock,
|
|
|
|
FAR struct pollfd *fds)
|
2017-03-31 16:58:14 +02:00
|
|
|
{
|
|
|
|
FAR struct usrsock_conn_s *conn = psock->s_conn;
|
|
|
|
FAR struct usrsock_poll_s *info;
|
|
|
|
FAR struct devif_callback_s *cb;
|
2022-09-19 05:08:57 +02:00
|
|
|
pollevent_t eventset = 0;
|
2017-03-31 16:58:14 +02:00
|
|
|
int ret = OK;
|
|
|
|
|
|
|
|
/* Sanity check */
|
|
|
|
|
|
|
|
#ifdef CONFIG_DEBUG
|
|
|
|
if (!conn || !fds)
|
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-12-31 16:26:14 +01:00
|
|
|
net_lock();
|
2017-03-31 16:58:14 +02:00
|
|
|
|
2019-12-31 16:26:14 +01:00
|
|
|
/* Find a container to hold the poll information */
|
|
|
|
|
|
|
|
info = conn->pollinfo;
|
2022-08-25 14:15:56 +02:00
|
|
|
while (info->conn != NULL)
|
2017-03-31 16:58:14 +02:00
|
|
|
{
|
2019-12-31 16:26:14 +01:00
|
|
|
if (++info >= &conn->pollinfo[CONFIG_NET_USRSOCK_NPOLLWAITERS])
|
|
|
|
{
|
|
|
|
ret = -ENOMEM;
|
|
|
|
goto errout_unlock;
|
|
|
|
}
|
2017-03-31 16:58:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate a usrsock callback structure */
|
|
|
|
|
2022-02-07 04:24:38 +01:00
|
|
|
cb = devif_callback_alloc(NULL, &conn->sconn.list, &conn->sconn.list_tail);
|
2017-10-19 19:55:51 +02:00
|
|
|
if (cb == NULL)
|
2017-03-31 16:58:14 +02:00
|
|
|
{
|
|
|
|
ret = -EBUSY;
|
|
|
|
goto errout_unlock;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize the poll info container */
|
|
|
|
|
2022-09-08 09:28:46 +02:00
|
|
|
info->conn = conn;
|
|
|
|
info->fds = fds;
|
|
|
|
info->cb = cb;
|
2017-03-31 16:58:14 +02:00
|
|
|
|
|
|
|
/* Initialize the callback structure. Save the reference to the info
|
|
|
|
* structure as callback private data so that it will be available during
|
|
|
|
* callback processing.
|
|
|
|
*/
|
|
|
|
|
2023-11-19 12:19:53 +01:00
|
|
|
cb->flags = USRSOCK_EVENT_ABORT | USRSOCK_EVENT_CONNECT_READY |
|
|
|
|
USRSOCK_EVENT_SENDTO_READY | USRSOCK_EVENT_RECVFROM_AVAIL |
|
|
|
|
USRSOCK_EVENT_REMOTE_CLOSED;
|
|
|
|
cb->priv = info;
|
|
|
|
cb->event = poll_event;
|
2017-03-31 16:58:14 +02:00
|
|
|
|
|
|
|
/* Save the reference in the poll info structure as fds private as well
|
|
|
|
* for use during poll teardown as well.
|
|
|
|
*/
|
|
|
|
|
2023-11-19 12:19:53 +01:00
|
|
|
fds->priv = info;
|
2017-03-31 16:58:14 +02:00
|
|
|
|
|
|
|
/* Check if socket is in error state */
|
|
|
|
|
|
|
|
if (conn->state == USRSOCK_CONN_STATE_UNINITIALIZED ||
|
|
|
|
conn->state == USRSOCK_CONN_STATE_ABORTED)
|
|
|
|
{
|
|
|
|
ninfo("socket %s.\n",
|
|
|
|
conn->state == USRSOCK_CONN_STATE_UNINITIALIZED ?
|
|
|
|
"uninitialized" : "aborted");
|
|
|
|
|
2022-09-19 05:08:57 +02:00
|
|
|
eventset |= (POLLERR | POLLHUP);
|
2017-03-31 16:58:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Stream sockets need to be connected or connecting (or listening). */
|
|
|
|
|
2022-11-23 14:44:11 +01:00
|
|
|
else if ((psock->s_type == SOCK_STREAM ||
|
|
|
|
psock->s_type == SOCK_SEQPACKET) &&
|
2021-04-02 07:28:21 +02:00
|
|
|
!(conn->connected || conn->state ==
|
|
|
|
USRSOCK_CONN_STATE_CONNECTING))
|
2017-03-31 16:58:14 +02:00
|
|
|
{
|
|
|
|
ninfo("stream socket not connected and not connecting.\n");
|
|
|
|
|
2022-09-19 05:08:57 +02:00
|
|
|
eventset |= (POLLOUT | POLLIN | POLLHUP);
|
2017-03-31 16:58:14 +02:00
|
|
|
}
|
|
|
|
else if (conn->flags & USRSOCK_EVENT_REMOTE_CLOSED)
|
|
|
|
{
|
|
|
|
ninfo("socket remote closed.\n");
|
|
|
|
|
|
|
|
/* Remote closed. */
|
|
|
|
|
2022-09-19 05:08:57 +02:00
|
|
|
eventset |= (POLLHUP | POLLIN);
|
2017-03-31 16:58:14 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Check if daemon has room for send data or has data to receive. */
|
|
|
|
|
|
|
|
if (conn->flags & USRSOCK_EVENT_SENDTO_READY)
|
|
|
|
{
|
|
|
|
ninfo("socket send ready.\n");
|
|
|
|
|
2022-09-19 05:08:57 +02:00
|
|
|
eventset |= POLLOUT;
|
2017-03-31 16:58:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (conn->flags & USRSOCK_EVENT_RECVFROM_AVAIL)
|
|
|
|
{
|
|
|
|
ninfo("socket recv avail.\n");
|
|
|
|
|
2022-09-19 05:08:57 +02:00
|
|
|
eventset |= POLLIN;
|
2017-03-31 16:58:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if any requested events are already in effect */
|
|
|
|
|
2022-09-19 05:08:57 +02:00
|
|
|
poll_notify(&fds, 1, eventset);
|
2017-03-31 16:58:14 +02:00
|
|
|
|
|
|
|
errout_unlock:
|
|
|
|
net_unlock();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: usrsock_pollteardown
|
2017-03-31 16:58:14 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Teardown monitoring of events on an usrsock socket
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* psock - An instance of the internal socket structure.
|
2019-03-11 19:48:17 +01:00
|
|
|
* fds - The structure describing the events to be stopped being
|
|
|
|
* monitored.
|
2017-03-31 16:58:14 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* 0: Success; Negated errno on failure
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
static int usrsock_pollteardown(FAR struct socket *psock,
|
|
|
|
FAR struct pollfd *fds)
|
|
|
|
{
|
|
|
|
FAR struct usrsock_conn_s *conn = psock->s_conn;
|
|
|
|
FAR struct usrsock_poll_s *info;
|
|
|
|
|
|
|
|
/* Sanity check */
|
|
|
|
|
|
|
|
#ifdef CONFIG_DEBUG
|
|
|
|
if (!conn || !fds->priv)
|
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Recover the socket descriptor poll state info from the poll structure */
|
|
|
|
|
|
|
|
info = (FAR struct usrsock_poll_s *)fds->priv;
|
|
|
|
DEBUGASSERT(info && info->fds && info->cb);
|
|
|
|
if (info)
|
|
|
|
{
|
|
|
|
/* Release the callback */
|
|
|
|
|
net/devif/devif_callback.c: corrected the connection event list to work as FIFO instead of LIFO.
In case of enabled packet forwarding mode, packets were forwarded in a reverse order
because of LIFO behavior of the connection event list.
The issue exposed only during high network traffic. Thus the event list started to grow
that resulted in changing the order of packets inside of groups of several packets
like the following: 3, 2, 1, 6, 5, 4, 8, 7 etc.
Remarks concerning the connection event list implementation:
* Now the queue (list) is FIFO as it should be.
* The list is singly linked.
* The list has a head pointer (inside of outer net_driver_s structure),
and a tail pointer is added into outer net_driver_s structure.
* The list item is devif_callback_s structure.
It still has two pointers to two different list chains (*nxtconn and *nxtdev).
* As before the first argument (*dev) of the list functions can be NULL,
while the other argument (*list) is effective (not NULL).
* An extra (*tail) argument is added to devif_callback_alloc()
and devif_conn_callback_free() functions.
* devif_callback_alloc() time complexity is O(1) (i.e. O(n) to fill the whole list).
* devif_callback_free() time complexity is O(n) (i.e. O(n^2) to empty the whole list).
* devif_conn_event() time complexity is O(n).
2021-08-29 22:57:26 +02:00
|
|
|
devif_conn_callback_free(NULL,
|
|
|
|
info->cb,
|
2022-02-07 04:24:38 +01:00
|
|
|
&conn->sconn.list,
|
|
|
|
&conn->sconn.list_tail);
|
2017-03-31 16:58:14 +02:00
|
|
|
|
|
|
|
/* Release the poll/select data slot */
|
|
|
|
|
|
|
|
info->fds->priv = NULL;
|
|
|
|
|
|
|
|
/* Then free the poll info container */
|
|
|
|
|
2022-08-25 14:15:56 +02:00
|
|
|
info->conn = NULL;
|
2017-03-31 16:58:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2017-04-22 00:33:14 +02:00
|
|
|
* Name: usrsock_poll
|
2017-03-31 16:58:14 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* The standard poll() operation redirects operations on socket descriptors
|
|
|
|
* to this function.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* psock - An instance of the internal socket structure.
|
|
|
|
* fds - The structure describing the events to be monitored.
|
|
|
|
* setup - true: Setup up the poll; false: Teardown the poll
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* 0: Success; Negated errno on failure
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-04-02 07:28:21 +02:00
|
|
|
int usrsock_poll(FAR struct socket *psock,
|
|
|
|
FAR struct pollfd *fds, bool setup)
|
2017-03-31 16:58:14 +02:00
|
|
|
{
|
|
|
|
if (setup)
|
|
|
|
{
|
|
|
|
return usrsock_pollsetup(psock, fds);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return usrsock_pollteardown(psock, fds);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-22 02:57:54 +02:00
|
|
|
#endif /* CONFIG_NET && CONFIG_NET_USRSOCK */
|