2018-09-09 17:21:39 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* net/tcp/tcp_notifier.c
|
|
|
|
*
|
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-09-09 17:21:39 +02:00
|
|
|
*
|
2021-02-19 12:45:37 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2018-09-09 17:21:39 +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-09-09 17:21:39 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
2018-09-09 23:01:44 +02:00
|
|
|
#include <assert.h>
|
2019-07-27 18:26:52 +02:00
|
|
|
#include <debug.h>
|
2018-09-09 17:21:39 +02:00
|
|
|
|
2018-09-09 23:01:44 +02:00
|
|
|
#include <nuttx/wqueue.h>
|
2018-09-09 17:21:39 +02:00
|
|
|
#include <nuttx/mm/iob.h>
|
2018-09-10 01:32:10 +02:00
|
|
|
#include <nuttx/net/tcp.h>
|
2018-09-09 17:21:39 +02:00
|
|
|
|
|
|
|
#include "tcp/tcp.h"
|
|
|
|
|
2019-12-31 16:26:14 +01:00
|
|
|
#ifdef CONFIG_NET_TCP_NOTIFIER
|
2018-09-09 17:21:39 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2018-09-10 01:32:10 +02:00
|
|
|
* Name: tcp_readahead_notifier_setup
|
2018-09-09 17:21:39 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2018-09-09 23:01:44 +02:00
|
|
|
* Set up to perform a callback to the worker function when an TCP data
|
|
|
|
* is added to the read-ahead buffer. The worker function will execute
|
2018-09-11 15:22:23 +02:00
|
|
|
* on the low priority worker thread.
|
2018-09-09 19:57:25 +02:00
|
|
|
*
|
2018-09-09 17:21:39 +02:00
|
|
|
* Input Parameters:
|
2018-09-11 15:22:23 +02:00
|
|
|
* worker - The worker function to execute on the low priority work
|
2018-09-10 01:32:10 +02:00
|
|
|
* queue when data is available in the TCP read-ahead buffer.
|
2018-09-09 17:21:39 +02:00
|
|
|
* conn - The TCP connection where read-ahead data is needed.
|
2018-09-09 23:01:44 +02:00
|
|
|
* arg - A user-defined argument that will be available to the worker
|
|
|
|
* function when it runs.
|
2018-09-09 17:21:39 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
2018-09-11 15:22:23 +02:00
|
|
|
* > 0 - The notification is in place. The returned value is a key that
|
|
|
|
* may be used later in a call to tcp_notifier_teardown().
|
|
|
|
* == 0 - There is already buffered read-ahead data. No notification
|
|
|
|
* will be provided.
|
2020-02-23 09:50:23 +01:00
|
|
|
* < 0 - An unexpected error occurred and notification will occur. The
|
2018-09-09 17:21:39 +02:00
|
|
|
* returned value is a negated errno value that indicates the
|
|
|
|
* nature of the failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2018-09-10 01:32:10 +02:00
|
|
|
int tcp_readahead_notifier_setup(worker_t worker,
|
|
|
|
FAR struct tcp_conn_s *conn,
|
|
|
|
FAR void *arg)
|
2018-09-09 17:21:39 +02:00
|
|
|
{
|
2018-09-09 23:01:44 +02:00
|
|
|
struct work_notifier_s info;
|
|
|
|
|
|
|
|
DEBUGASSERT(worker != NULL);
|
|
|
|
|
2018-09-09 17:21:39 +02:00
|
|
|
/* If there is already buffered read-ahead data, then return zero without
|
|
|
|
* setting up the notification.
|
|
|
|
*/
|
|
|
|
|
2021-06-18 07:34:21 +02:00
|
|
|
if (conn->readahead != NULL)
|
2018-09-09 17:21:39 +02:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-09-09 23:01:44 +02:00
|
|
|
/* Otherwise, this is just a simple wrapper around work_notifer_setup(). */
|
|
|
|
|
|
|
|
info.evtype = WORK_TCP_READAHEAD;
|
2018-09-11 16:49:39 +02:00
|
|
|
info.qid = LPWORK;
|
2018-09-09 23:01:44 +02:00
|
|
|
info.qualifier = conn;
|
|
|
|
info.arg = arg;
|
|
|
|
info.worker = worker;
|
2018-09-09 17:21:39 +02:00
|
|
|
|
2018-09-09 23:01:44 +02:00
|
|
|
return work_notifier_setup(&info);
|
2018-09-09 17:21:39 +02:00
|
|
|
}
|
|
|
|
|
2018-09-10 01:32:10 +02:00
|
|
|
/****************************************************************************
|
2019-07-01 20:25:32 +02:00
|
|
|
* Name: tcp_writebuffer_notifier_setup
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Set up to perform a callback to the worker function when an TCP write
|
|
|
|
* buffer is emptied. The worker function will execute on the high
|
|
|
|
* priority worker thread.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* worker - The worker function to execute on the high priority work
|
|
|
|
* queue when all buffer TX data has been sent.
|
|
|
|
* conn - The TCP connection where buffer write data is pending.
|
|
|
|
* arg - A user-defined argument that will be available to the worker
|
|
|
|
* function when it runs.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* > 0 - The signal notification is in place. The returned value is a
|
|
|
|
* key that may be used later in a call to
|
|
|
|
* tcp_notifier_teardown().
|
|
|
|
* == 0 - There is already buffered read-ahead data. No signal
|
|
|
|
* notification will be provided.
|
|
|
|
* < 0 - An unexpected error occurred and no signal will be sent. The
|
|
|
|
* returned value is a negated errno value that indicates the
|
|
|
|
* nature of the failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int tcp_writebuffer_notifier_setup(worker_t worker,
|
|
|
|
FAR struct tcp_conn_s *conn,
|
|
|
|
FAR void *arg)
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_NET_TCP_WRITE_BUFFERS
|
|
|
|
struct work_notifier_s info;
|
|
|
|
|
|
|
|
DEBUGASSERT(worker != NULL);
|
|
|
|
|
|
|
|
/* If the write buffers are already empty, then return zero without
|
|
|
|
* setting up the notification.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (sq_empty(&conn->write_q) && sq_empty(&conn->unacked_q))
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Otherwise, this is just a simple wrapper around work_notifer_setup(). */
|
|
|
|
|
|
|
|
info.evtype = WORK_TCP_WRITEBUFFER;
|
|
|
|
info.qid = LPWORK;
|
|
|
|
info.qualifier = conn;
|
|
|
|
info.arg = arg;
|
|
|
|
info.worker = worker;
|
|
|
|
|
|
|
|
return work_notifier_setup(&info);
|
|
|
|
#else
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: tcp_disconnect_notifier_setup
|
2018-09-10 01:32:10 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Set up to perform a callback to the worker function if the TCP
|
|
|
|
* connection is lost.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
2018-09-11 15:22:23 +02:00
|
|
|
* worker - The worker function to execute on the low priority work
|
2018-09-10 01:32:10 +02:00
|
|
|
* queue when data is available in the TCP read-ahead buffer.
|
|
|
|
* conn - The TCP connection where read-ahead data is needed.
|
|
|
|
* arg - A user-defined argument that will be available to the worker
|
|
|
|
* function when it runs.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
2018-09-11 15:22:23 +02:00
|
|
|
* > 0 - The notification is in place. The returned value is a key that
|
|
|
|
* may be used later in a call to tcp_notifier_teardown().
|
2019-07-27 18:26:52 +02:00
|
|
|
* == 0 - No connection has been established.
|
2018-09-11 15:22:23 +02:00
|
|
|
* < 0 - An unexpected error occurred and notification will occur. The
|
2018-09-10 01:32:10 +02:00
|
|
|
* returned value is a negated errno value that indicates the
|
|
|
|
* nature of the failure.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-07-01 20:25:32 +02:00
|
|
|
int tcp_disconnect_notifier_setup(worker_t worker,
|
|
|
|
FAR struct tcp_conn_s *conn,
|
|
|
|
FAR void *arg)
|
2018-09-10 01:32:10 +02:00
|
|
|
{
|
|
|
|
struct work_notifier_s info;
|
|
|
|
|
|
|
|
DEBUGASSERT(worker != NULL);
|
|
|
|
|
|
|
|
/* If connection has not been established, then return 0. */
|
|
|
|
|
|
|
|
if (conn->tcpstateflags != TCP_ESTABLISHED)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Otherwise, this is just a simple wrapper around work_notifer_setup(). */
|
|
|
|
|
|
|
|
info.evtype = WORK_TCP_DISCONNECT;
|
2018-09-11 16:49:39 +02:00
|
|
|
info.qid = LPWORK;
|
2018-09-10 01:32:10 +02:00
|
|
|
info.qualifier = conn;
|
|
|
|
info.arg = arg;
|
|
|
|
info.worker = worker;
|
|
|
|
|
|
|
|
return work_notifier_setup(&info);
|
|
|
|
}
|
|
|
|
|
2018-09-09 17:21:39 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: tcp_notifier_teardown
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Eliminate a TCP read-ahead notification previously setup by
|
2018-09-10 01:32:10 +02:00
|
|
|
* tcp_readahead_notifier_setup(). This function should only be called
|
|
|
|
* if the notification should be aborted prior to the notification. The
|
2020-02-22 19:31:14 +01:00
|
|
|
* notification will automatically be torn down after the notification.
|
2018-09-09 17:21:39 +02:00
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* key - The key value returned from a previous call to
|
2018-09-10 01:32:10 +02:00
|
|
|
* tcp_readahead_notifier_setup().
|
2018-09-09 17:21:39 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
2022-05-13 12:26:42 +02:00
|
|
|
* None.
|
2018-09-09 17:21:39 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-05-13 12:26:42 +02:00
|
|
|
void tcp_notifier_teardown(int key)
|
2018-09-09 17:21:39 +02:00
|
|
|
{
|
2018-09-09 23:01:44 +02:00
|
|
|
/* This is just a simple wrapper around work_notifier_teardown(). */
|
2018-09-09 17:21:39 +02:00
|
|
|
|
2022-05-13 12:26:42 +02:00
|
|
|
work_notifier_teardown(key);
|
2018-09-09 17:21:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2018-09-10 01:32:10 +02:00
|
|
|
* Name: tcp_readahead_signal
|
2018-09-09 17:21:39 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Read-ahead data has been buffered. Signal all threads waiting for
|
|
|
|
* read-ahead data to become available.
|
|
|
|
*
|
2018-09-09 23:01:44 +02:00
|
|
|
* When read-ahead data becomes available, *all* of the workers waiting
|
|
|
|
* for read-ahead data will be executed. If there are multiple workers
|
|
|
|
* waiting for read-ahead data then only the first to execute will get the
|
2018-09-10 01:32:10 +02:00
|
|
|
* data. Others will need to call tcp_readahead_notifier_setup() once
|
|
|
|
* again.
|
2018-09-09 19:57:25 +02:00
|
|
|
*
|
2018-09-09 17:21:39 +02:00
|
|
|
* Input Parameters:
|
|
|
|
* conn - The TCP connection where read-ahead data was just buffered.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2018-09-10 01:32:10 +02:00
|
|
|
void tcp_readahead_signal(FAR struct tcp_conn_s *conn)
|
2018-09-09 17:21:39 +02:00
|
|
|
{
|
2018-09-09 23:01:44 +02:00
|
|
|
/* This is just a simple wrapper around work_notifier_signal(). */
|
2018-09-09 17:21:39 +02:00
|
|
|
|
2020-01-19 10:44:27 +01:00
|
|
|
work_notifier_signal(WORK_TCP_READAHEAD, conn);
|
2018-09-09 17:21:39 +02:00
|
|
|
}
|
2019-07-01 20:25:32 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: tcp_writebuffer_signal
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* All buffer Tx data has been sent. Signal all threads waiting for the
|
|
|
|
* write buffers to become empty.
|
|
|
|
*
|
|
|
|
* When write buffer becomes empty, *all* of the workers waiting
|
|
|
|
* for that event data will be executed. If there are multiple workers
|
|
|
|
* waiting for read-ahead data then only the first to execute will get the
|
|
|
|
* data. Others will need to call tcp_writebuffer_notifier_setup() once
|
|
|
|
* again.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* conn - The TCP connection where read-ahead data was just buffered.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifdef CONFIG_NET_TCP_WRITE_BUFFERS
|
|
|
|
void tcp_writebuffer_signal(FAR struct tcp_conn_s *conn)
|
|
|
|
{
|
|
|
|
/* This is just a simple wrapper around work_notifier_signal(). */
|
|
|
|
|
2020-01-19 10:44:27 +01:00
|
|
|
work_notifier_signal(WORK_TCP_WRITEBUFFER, conn);
|
2019-07-01 20:25:32 +02:00
|
|
|
}
|
|
|
|
#endif
|
2018-09-09 17:21:39 +02:00
|
|
|
|
2018-09-10 01:32:10 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: tcp_disconnect_signal
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* The TCP connection has been lost. Signal all threads monitoring TCP
|
|
|
|
* state events.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* conn - The TCP connection where read-ahead data was just buffered.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* None.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
void tcp_disconnect_signal(FAR struct tcp_conn_s *conn)
|
|
|
|
{
|
|
|
|
/* This is just a simple wrapper around work_notifier_signal(). */
|
|
|
|
|
2020-01-19 10:44:27 +01:00
|
|
|
work_notifier_signal(WORK_TCP_DISCONNECT, conn);
|
2018-09-10 01:32:10 +02:00
|
|
|
}
|
|
|
|
|
2019-12-31 16:26:14 +01:00
|
|
|
#endif /* CONFIG_NET_TCP_NOTIFIER */
|