2019-09-12 10:56:54 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2019, Redis Labs
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* * Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* * Neither the name of Redis nor the names of its contributors may be used
|
|
|
|
* to endorse or promote products derived from this software without
|
|
|
|
* specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "server.h"
|
|
|
|
#include "connhelpers.h"
|
|
|
|
|
|
|
|
/* The connections module provides a lean abstraction of network connections
|
|
|
|
* to avoid direct socket and async event management across the Redis code base.
|
|
|
|
*
|
|
|
|
* It does NOT provide advanced connection features commonly found in similar
|
|
|
|
* libraries such as complete in/out buffer management, throttling, etc. These
|
|
|
|
* functions remain in networking.c.
|
|
|
|
*
|
|
|
|
* The primary goal is to allow transparent handling of TCP and TLS based
|
|
|
|
* connections. To do so, connections have the following properties:
|
|
|
|
*
|
|
|
|
* 1. A connection may live before its corresponding socket exists. This
|
|
|
|
* allows various context and configuration setting to be handled before
|
|
|
|
* establishing the actual connection.
|
|
|
|
* 2. The caller may register/unregister logical read/write handlers to be
|
|
|
|
* called when the connection has data to read from/can accept writes.
|
|
|
|
* These logical handlers may or may not correspond to actual AE events,
|
|
|
|
* depending on the implementation (for TCP they are; for TLS they aren't).
|
|
|
|
*/
|
|
|
|
|
2022-07-27 10:46:31 +08:00
|
|
|
static ConnectionType CT_Socket;
|
2019-09-12 10:56:54 +03:00
|
|
|
|
|
|
|
/* When a connection is created we must know its type already, but the
|
|
|
|
* underlying socket may or may not exist:
|
|
|
|
*
|
|
|
|
* - For accepted connections, it exists as we do not model the listen/accept
|
|
|
|
* part; So caller calls connCreateSocket() followed by connAccept().
|
|
|
|
* - For outgoing connections, the socket is created by the connection module
|
|
|
|
* itself; So caller calls connCreateSocket() followed by connConnect(),
|
|
|
|
* which registers a connect callback that fires on connected/error state
|
|
|
|
* (and after any transport level handshake was done).
|
|
|
|
*
|
|
|
|
* NOTE: An earlier version relied on connections being part of other structs
|
|
|
|
* and not independently allocated. This could lead to further optimizations
|
|
|
|
* like using container_of(), etc. However it was discontinued in favor of
|
|
|
|
* this approach for these reasons:
|
|
|
|
*
|
|
|
|
* 1. In some cases conns are created/handled outside the context of the
|
|
|
|
* containing struct, in which case it gets a bit awkward to copy them.
|
|
|
|
* 2. Future implementations may wish to allocate arbitrary data for the
|
|
|
|
* connection.
|
|
|
|
* 3. The container_of() approach is anyway risky because connections may
|
|
|
|
* be embedded in different structs, not just client.
|
|
|
|
*/
|
|
|
|
|
2022-07-27 10:46:31 +08:00
|
|
|
static connection *connCreateSocket(void) {
|
2019-09-12 10:56:54 +03:00
|
|
|
connection *conn = zcalloc(sizeof(connection));
|
|
|
|
conn->type = &CT_Socket;
|
|
|
|
conn->fd = -1;
|
2023-05-28 13:35:27 +08:00
|
|
|
conn->iovcnt = IOV_MAX;
|
2019-09-12 10:56:54 +03:00
|
|
|
|
|
|
|
return conn;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create a new socket-type connection that is already associated with
|
|
|
|
* an accepted connection.
|
|
|
|
*
|
2020-07-28 11:32:47 +03:00
|
|
|
* The socket is not ready for I/O until connAccept() was called and
|
2019-09-12 10:56:54 +03:00
|
|
|
* invoked the connection-level accept handler.
|
2020-07-28 11:32:47 +03:00
|
|
|
*
|
|
|
|
* Callers should use connGetState() and verify the created connection
|
|
|
|
* is not in an error state (which is not possible for a socket connection,
|
|
|
|
* but could but possible with other protocols).
|
2019-09-12 10:56:54 +03:00
|
|
|
*/
|
2022-07-27 10:46:31 +08:00
|
|
|
static connection *connCreateAcceptedSocket(int fd, void *priv) {
|
|
|
|
UNUSED(priv);
|
2019-09-12 10:56:54 +03:00
|
|
|
connection *conn = connCreateSocket();
|
|
|
|
conn->fd = fd;
|
|
|
|
conn->state = CONN_STATE_ACCEPTING;
|
|
|
|
return conn;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int connSocketConnect(connection *conn, const char *addr, int port, const char *src_addr,
|
|
|
|
ConnectionCallbackFunc connect_handler) {
|
|
|
|
int fd = anetTcpNonBlockBestEffortBindConnect(NULL,addr,port,src_addr);
|
|
|
|
if (fd == -1) {
|
|
|
|
conn->state = CONN_STATE_ERROR;
|
|
|
|
conn->last_errno = errno;
|
|
|
|
return C_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
conn->fd = fd;
|
|
|
|
conn->state = CONN_STATE_CONNECTING;
|
|
|
|
|
|
|
|
conn->conn_handler = connect_handler;
|
|
|
|
aeCreateFileEvent(server.el, conn->fd, AE_WRITABLE,
|
|
|
|
conn->type->ae_handler, conn);
|
|
|
|
|
|
|
|
return C_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ------ Pure socket connections ------- */
|
|
|
|
|
|
|
|
/* A very incomplete list of implementation-specific calls. Much of the above shall
|
|
|
|
* move here as we implement additional connection types.
|
|
|
|
*/
|
|
|
|
|
2022-11-05 00:46:37 +08:00
|
|
|
static void connSocketShutdown(connection *conn) {
|
|
|
|
if (conn->fd == -1) return;
|
|
|
|
|
|
|
|
shutdown(conn->fd, SHUT_RDWR);
|
|
|
|
}
|
|
|
|
|
2019-09-12 10:56:54 +03:00
|
|
|
/* Close the connection and free resources. */
|
|
|
|
static void connSocketClose(connection *conn) {
|
|
|
|
if (conn->fd != -1) {
|
2020-11-26 05:37:54 +08:00
|
|
|
aeDeleteFileEvent(server.el,conn->fd, AE_READABLE | AE_WRITABLE);
|
2019-09-12 10:56:54 +03:00
|
|
|
close(conn->fd);
|
|
|
|
conn->fd = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If called from within a handler, schedule the close but
|
|
|
|
* keep the connection until the handler returns.
|
|
|
|
*/
|
2020-03-22 14:42:03 +02:00
|
|
|
if (connHasRefs(conn)) {
|
2019-09-12 10:56:54 +03:00
|
|
|
conn->flags |= CONN_FLAG_CLOSE_SCHEDULED;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
zfree(conn);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int connSocketWrite(connection *conn, const void *data, size_t data_len) {
|
|
|
|
int ret = write(conn->fd, data, data_len);
|
2019-10-15 17:20:58 +03:00
|
|
|
if (ret < 0 && errno != EAGAIN) {
|
2019-09-12 10:56:54 +03:00
|
|
|
conn->last_errno = errno;
|
2020-09-22 11:38:52 +03:00
|
|
|
|
|
|
|
/* Don't overwrite the state of a connection that is not already
|
|
|
|
* connected, not to mess with handler callbacks.
|
|
|
|
*/
|
2021-11-08 16:09:33 +02:00
|
|
|
if (errno != EINTR && conn->state == CONN_STATE_CONNECTED)
|
2020-09-22 11:38:52 +03:00
|
|
|
conn->state = CONN_STATE_ERROR;
|
2019-09-12 10:56:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
Reduce system calls of write for client->reply by introducing writev (#9934)
There are scenarios where it results in many small objects in the reply list,
such as commands heavily using deferred array replies (`addReplyDeferredLen`).
E.g. what COMMAND command and CLUSTER SLOTS used to do (see #10056, #7123),
but also in case of a transaction or a pipeline of commands that use just one differed array reply.
We used to have to run multiple loops along with multiple calls to `write()` to send data back to
peer based on the current code, but by means of `writev()`, we can gather those scattered
objects in reply list and include the static reply buffer as well, then send it by one system call,
that ought to achieve higher performance.
In the case of TLS, we simply check and concatenate buffers into one big buffer and send it
away by one call to `connTLSWrite()`, if the amount of all buffers exceeds `NET_MAX_WRITES_PER_EVENT`,
then invoke `connTLSWrite()` multiple times to avoid a huge massive of memory copies.
Note that aside of reducing system calls, this change will also reduce the amount of
small TCP packets sent.
2022-02-22 20:00:37 +08:00
|
|
|
static int connSocketWritev(connection *conn, const struct iovec *iov, int iovcnt) {
|
|
|
|
int ret = writev(conn->fd, iov, iovcnt);
|
|
|
|
if (ret < 0 && errno != EAGAIN) {
|
|
|
|
conn->last_errno = errno;
|
|
|
|
|
|
|
|
/* Don't overwrite the state of a connection that is not already
|
|
|
|
* connected, not to mess with handler callbacks.
|
|
|
|
*/
|
|
|
|
if (errno != EINTR && conn->state == CONN_STATE_CONNECTED)
|
|
|
|
conn->state = CONN_STATE_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-09-12 10:56:54 +03:00
|
|
|
static int connSocketRead(connection *conn, void *buf, size_t buf_len) {
|
|
|
|
int ret = read(conn->fd, buf, buf_len);
|
|
|
|
if (!ret) {
|
|
|
|
conn->state = CONN_STATE_CLOSED;
|
|
|
|
} else if (ret < 0 && errno != EAGAIN) {
|
|
|
|
conn->last_errno = errno;
|
2020-09-22 11:38:52 +03:00
|
|
|
|
|
|
|
/* Don't overwrite the state of a connection that is not already
|
|
|
|
* connected, not to mess with handler callbacks.
|
|
|
|
*/
|
2021-11-08 16:09:33 +02:00
|
|
|
if (errno != EINTR && conn->state == CONN_STATE_CONNECTED)
|
2020-09-22 11:38:52 +03:00
|
|
|
conn->state = CONN_STATE_ERROR;
|
2019-09-12 10:56:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int connSocketAccept(connection *conn, ConnectionCallbackFunc accept_handler) {
|
2020-03-22 14:42:03 +02:00
|
|
|
int ret = C_OK;
|
|
|
|
|
2019-09-12 10:56:54 +03:00
|
|
|
if (conn->state != CONN_STATE_ACCEPTING) return C_ERR;
|
|
|
|
conn->state = CONN_STATE_CONNECTED;
|
2020-03-22 14:42:03 +02:00
|
|
|
|
|
|
|
connIncrRefs(conn);
|
|
|
|
if (!callHandler(conn, accept_handler)) ret = C_ERR;
|
|
|
|
connDecrRefs(conn);
|
|
|
|
|
|
|
|
return ret;
|
2019-09-12 10:56:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Register a write handler, to be called when the connection is writable.
|
|
|
|
* If NULL, the existing handler is removed.
|
2019-10-15 17:21:33 +03:00
|
|
|
*
|
|
|
|
* The barrier flag indicates a write barrier is requested, resulting with
|
|
|
|
* CONN_FLAG_WRITE_BARRIER set. This will ensure that the write handler is
|
|
|
|
* always called before and not after the read handler in a single event
|
|
|
|
* loop.
|
2019-09-12 10:56:54 +03:00
|
|
|
*/
|
2019-08-19 12:18:25 +03:00
|
|
|
static int connSocketSetWriteHandler(connection *conn, ConnectionCallbackFunc func, int barrier) {
|
2019-09-12 10:56:54 +03:00
|
|
|
if (func == conn->write_handler) return C_OK;
|
|
|
|
|
|
|
|
conn->write_handler = func;
|
2019-08-19 12:18:25 +03:00
|
|
|
if (barrier)
|
|
|
|
conn->flags |= CONN_FLAG_WRITE_BARRIER;
|
|
|
|
else
|
|
|
|
conn->flags &= ~CONN_FLAG_WRITE_BARRIER;
|
2019-09-12 10:56:54 +03:00
|
|
|
if (!conn->write_handler)
|
|
|
|
aeDeleteFileEvent(server.el,conn->fd,AE_WRITABLE);
|
|
|
|
else
|
|
|
|
if (aeCreateFileEvent(server.el,conn->fd,AE_WRITABLE,
|
|
|
|
conn->type->ae_handler,conn) == AE_ERR) return C_ERR;
|
|
|
|
return C_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Register a read handler, to be called when the connection is readable.
|
|
|
|
* If NULL, the existing handler is removed.
|
|
|
|
*/
|
|
|
|
static int connSocketSetReadHandler(connection *conn, ConnectionCallbackFunc func) {
|
|
|
|
if (func == conn->read_handler) return C_OK;
|
|
|
|
|
|
|
|
conn->read_handler = func;
|
|
|
|
if (!conn->read_handler)
|
|
|
|
aeDeleteFileEvent(server.el,conn->fd,AE_READABLE);
|
|
|
|
else
|
|
|
|
if (aeCreateFileEvent(server.el,conn->fd,
|
|
|
|
AE_READABLE,conn->type->ae_handler,conn) == AE_ERR) return C_ERR;
|
|
|
|
return C_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *connSocketGetLastError(connection *conn) {
|
|
|
|
return strerror(conn->last_errno);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void connSocketEventHandler(struct aeEventLoop *el, int fd, void *clientData, int mask)
|
|
|
|
{
|
|
|
|
UNUSED(el);
|
|
|
|
UNUSED(fd);
|
|
|
|
connection *conn = clientData;
|
|
|
|
|
|
|
|
if (conn->state == CONN_STATE_CONNECTING &&
|
|
|
|
(mask & AE_WRITABLE) && conn->conn_handler) {
|
|
|
|
|
2022-07-27 09:38:25 +08:00
|
|
|
int conn_error = anetGetError(conn->fd);
|
2020-09-22 17:53:36 +08:00
|
|
|
if (conn_error) {
|
|
|
|
conn->last_errno = conn_error;
|
2019-09-12 10:56:54 +03:00
|
|
|
conn->state = CONN_STATE_ERROR;
|
|
|
|
} else {
|
|
|
|
conn->state = CONN_STATE_CONNECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!conn->write_handler) aeDeleteFileEvent(server.el,conn->fd,AE_WRITABLE);
|
|
|
|
|
|
|
|
if (!callHandler(conn, conn->conn_handler)) return;
|
|
|
|
conn->conn_handler = NULL;
|
|
|
|
}
|
|
|
|
|
2019-08-19 12:18:25 +03:00
|
|
|
/* Normally we execute the readable event first, and the writable
|
2019-10-15 17:21:33 +03:00
|
|
|
* event later. This is useful as sometimes we may be able
|
2019-08-19 12:18:25 +03:00
|
|
|
* to serve the reply of a query immediately after processing the
|
|
|
|
* query.
|
|
|
|
*
|
|
|
|
* However if WRITE_BARRIER is set in the mask, our application is
|
|
|
|
* asking us to do the reverse: never fire the writable event
|
|
|
|
* after the readable. In such a case, we invert the calls.
|
|
|
|
* This is useful when, for instance, we want to do things
|
2019-10-15 17:21:33 +03:00
|
|
|
* in the beforeSleep() hook, like fsync'ing a file to disk,
|
2019-08-19 12:18:25 +03:00
|
|
|
* before replying to a client. */
|
|
|
|
int invert = conn->flags & CONN_FLAG_WRITE_BARRIER;
|
|
|
|
|
|
|
|
int call_write = (mask & AE_WRITABLE) && conn->write_handler;
|
|
|
|
int call_read = (mask & AE_READABLE) && conn->read_handler;
|
|
|
|
|
2019-09-12 10:56:54 +03:00
|
|
|
/* Handle normal I/O flows */
|
2019-08-19 12:18:25 +03:00
|
|
|
if (!invert && call_read) {
|
2019-09-12 10:56:54 +03:00
|
|
|
if (!callHandler(conn, conn->read_handler)) return;
|
|
|
|
}
|
2019-08-19 12:18:25 +03:00
|
|
|
/* Fire the writable event. */
|
|
|
|
if (call_write) {
|
2019-09-12 10:56:54 +03:00
|
|
|
if (!callHandler(conn, conn->write_handler)) return;
|
|
|
|
}
|
2019-08-19 12:18:25 +03:00
|
|
|
/* If we have to invert the call, fire the readable event now
|
|
|
|
* after the writable one. */
|
|
|
|
if (invert && call_read) {
|
|
|
|
if (!callHandler(conn, conn->read_handler)) return;
|
|
|
|
}
|
2019-09-12 10:56:54 +03:00
|
|
|
}
|
|
|
|
|
2022-07-27 11:47:50 +08:00
|
|
|
static void connSocketAcceptHandler(aeEventLoop *el, int fd, void *privdata, int mask) {
|
|
|
|
int cport, cfd, max = MAX_ACCEPTS_PER_CALL;
|
|
|
|
char cip[NET_IP_STR_LEN];
|
|
|
|
UNUSED(el);
|
|
|
|
UNUSED(mask);
|
|
|
|
UNUSED(privdata);
|
|
|
|
|
|
|
|
while(max--) {
|
|
|
|
cfd = anetTcpAccept(server.neterr, fd, cip, sizeof(cip), &cport);
|
|
|
|
if (cfd == ANET_ERR) {
|
|
|
|
if (errno != EWOULDBLOCK)
|
|
|
|
serverLog(LL_WARNING,
|
|
|
|
"Accepting client connection: %s", server.neterr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
serverLog(LL_VERBOSE,"Accepted %s:%d", cip, cport);
|
|
|
|
acceptCommonHandler(connCreateAcceptedSocket(cfd, NULL),0,cip);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Introduce connAddr
Originally, connPeerToString is designed to get the address info from
socket only(for both TCP & TLS), and the API 'connPeerToString' is
oriented to operate a FD like:
int connPeerToString(connection *conn, char *ip, size_t ip_len, int *port) {
return anetFdToString(conn ? conn->fd : -1, ip, ip_len, port, FD_TO_PEER_NAME);
}
Introduce connAddr and implement .addr method for socket and TLS,
thus the API 'connAddr' and 'connFormatAddr' become oriented to a
connection like:
static inline int connAddr(connection *conn, char *ip, size_t ip_len, int *port, int remote) {
if (conn && conn->type->addr) {
return conn->type->addr(conn, ip, ip_len, port, remote);
}
return -1;
}
Also remove 'FD_TO_PEER_NAME' & 'FD_TO_SOCK_NAME', use a boolean type
'remote' to get local/remote address of a connection.
With these changes, it's possible to support the other connection
types which does not use socket(Ex, RDMA).
Thanks to Oran for suggestions!
Signed-off-by: zhenwei pi <pizhenwei@bytedance.com>
2022-07-27 10:08:32 +08:00
|
|
|
static int connSocketAddr(connection *conn, char *ip, size_t ip_len, int *port, int remote) {
|
|
|
|
if (anetFdToString(conn->fd, ip, ip_len, port, remote) == 0)
|
|
|
|
return C_OK;
|
|
|
|
|
|
|
|
conn->last_errno = errno;
|
|
|
|
return C_ERR;
|
|
|
|
}
|
|
|
|
|
2023-01-04 16:52:56 +08:00
|
|
|
static int connSocketIsLocal(connection *conn) {
|
|
|
|
char cip[NET_IP_STR_LEN + 1] = { 0 };
|
|
|
|
|
|
|
|
if (connSocketAddr(conn, cip, sizeof(cip) - 1, NULL, 1) == C_ERR)
|
|
|
|
return -1;
|
|
|
|
|
2023-04-04 15:45:09 +08:00
|
|
|
return !strncmp(cip, "127.", 4) || !strcmp(cip, "::1");
|
2023-01-04 16:52:56 +08:00
|
|
|
}
|
|
|
|
|
Introduce .listen into connection type
Introduce listen method into connection type, this allows no hard code
of listen logic. Originally, we initialize server during startup like
this:
if (server.port)
listenToPort(server.port,&server.ipfd);
if (server.tls_port)
listenToPort(server.port,&server.tlsfd);
if (server.unixsocket)
anetUnixServer(...server.unixsocket...);
...
if (createSocketAcceptHandler(&server.ipfd, acceptTcpHandler) != C_OK)
if (createSocketAcceptHandler(&server.tlsfd, acceptTcpHandler) != C_OK)
if (createSocketAcceptHandler(&server.sofd, acceptTcpHandler) != C_OK)
...
If a new connection type gets supported, we have to add more hard code
to setup listener.
Introduce .listen and refactor listener, and Unix socket supports this.
this allows to setup listener arguments and create listener in a loop.
What's more, '.listen' is defined in connection.h, so we should include
server.h to import 'struct socketFds', but server.h has already include
'connection.h'. To avoid including loop(also to make code reasonable),
define 'struct connListener' in connection.h instead of 'struct socketFds'
in server.h. This leads this commit to get more changes.
There are more fields in 'struct connListener', hence it's possible to
simplify changeBindAddr & applyTLSPort() & updatePort() into a single
logic: update the listener config from the server.xxx, and re-create
the listener.
Because of the new field 'priv' in struct connListener, we expect to pass
this to the accept handler(even it's not used currently), this may be used
in the future.
Signed-off-by: zhenwei pi <pizhenwei@bytedance.com>
2022-07-27 12:18:28 +08:00
|
|
|
static int connSocketListen(connListener *listener) {
|
|
|
|
return listenToPort(listener);
|
|
|
|
}
|
|
|
|
|
2019-09-12 10:56:54 +03:00
|
|
|
static int connSocketBlockingConnect(connection *conn, const char *addr, int port, long long timeout) {
|
|
|
|
int fd = anetTcpNonBlockConnect(NULL,addr,port);
|
|
|
|
if (fd == -1) {
|
|
|
|
conn->state = CONN_STATE_ERROR;
|
|
|
|
conn->last_errno = errno;
|
|
|
|
return C_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((aeWait(fd, AE_WRITABLE, timeout) & AE_WRITABLE) == 0) {
|
|
|
|
conn->state = CONN_STATE_ERROR;
|
|
|
|
conn->last_errno = ETIMEDOUT;
|
|
|
|
}
|
|
|
|
|
|
|
|
conn->fd = fd;
|
|
|
|
conn->state = CONN_STATE_CONNECTED;
|
|
|
|
return C_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Connection-based versions of syncio.c functions.
|
|
|
|
* NOTE: This should ideally be refactored out in favor of pure async work.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static ssize_t connSocketSyncWrite(connection *conn, char *ptr, ssize_t size, long long timeout) {
|
|
|
|
return syncWrite(conn->fd, ptr, size, timeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t connSocketSyncRead(connection *conn, char *ptr, ssize_t size, long long timeout) {
|
|
|
|
return syncRead(conn->fd, ptr, size, timeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
static ssize_t connSocketSyncReadLine(connection *conn, char *ptr, ssize_t size, long long timeout) {
|
|
|
|
return syncReadLine(conn->fd, ptr, size, timeout);
|
|
|
|
}
|
|
|
|
|
2022-08-22 15:15:37 +08:00
|
|
|
static const char *connSocketGetType(connection *conn) {
|
2020-08-17 17:46:54 +03:00
|
|
|
(void) conn;
|
|
|
|
|
|
|
|
return CONN_TYPE_SOCKET;
|
|
|
|
}
|
2019-09-12 10:56:54 +03:00
|
|
|
|
2022-07-27 10:46:31 +08:00
|
|
|
static ConnectionType CT_Socket = {
|
2022-07-27 09:39:37 +08:00
|
|
|
/* connection type */
|
|
|
|
.get_type = connSocketGetType,
|
|
|
|
|
Introduce connection layer framework
Use connTypeRegister() to register a connection type into redis, and
query connection by connectionByType() via type.
With this change, we can hide TLS specified methods into connection
type:
- void tlsInit(void);
- void tlsCleanup(void);
- int tlsConfigure(redisTLSContextConfig *ctx_config);
- int isTlsConfigured(void);
Merge isTlsConfigured & tlsConfigure, use an argument *reconfigure*
to distinguish:
tlsConfigure(&server.tls_ctx_config)
-> onnTypeConfigure(CONN_TYPE_TLS, &server.tls_ctx_config, 1)
isTlsConfigured() && tlsConfigure(&server.tls_ctx_config)
-> connTypeConfigure(CONN_TYPE_TLS, &server.tls_ctx_config, 0)
Finally, we can remove USE_OPENSSL from config.c. If redis is built
without TLS, and still run redis with TLS, then redis reports:
# Missing implement of connection type 1
# Failed to configure TLS. Check logs for more info.
The log can be optimised, let's leave it in the future. Maybe we can
use connection type as a string.
Although uninitialized fields of a static struct are zero, we still
set them as NULL explicitly in socket.c, let them clear to read & maintain:
.init = NULL,
.cleanup = NULL,
.configure = NULL,
Signed-off-by: zhenwei pi <pizhenwei@bytedance.com>
2022-08-22 15:09:59 +08:00
|
|
|
/* connection type initialize & finalize & configure */
|
|
|
|
.init = NULL,
|
|
|
|
.cleanup = NULL,
|
|
|
|
.configure = NULL,
|
|
|
|
|
2022-07-27 09:39:37 +08:00
|
|
|
/* ae & accept & listen & error & address handler */
|
2019-09-12 10:56:54 +03:00
|
|
|
.ae_handler = connSocketEventHandler,
|
2022-07-27 11:47:50 +08:00
|
|
|
.accept_handler = connSocketAcceptHandler,
|
Introduce connAddr
Originally, connPeerToString is designed to get the address info from
socket only(for both TCP & TLS), and the API 'connPeerToString' is
oriented to operate a FD like:
int connPeerToString(connection *conn, char *ip, size_t ip_len, int *port) {
return anetFdToString(conn ? conn->fd : -1, ip, ip_len, port, FD_TO_PEER_NAME);
}
Introduce connAddr and implement .addr method for socket and TLS,
thus the API 'connAddr' and 'connFormatAddr' become oriented to a
connection like:
static inline int connAddr(connection *conn, char *ip, size_t ip_len, int *port, int remote) {
if (conn && conn->type->addr) {
return conn->type->addr(conn, ip, ip_len, port, remote);
}
return -1;
}
Also remove 'FD_TO_PEER_NAME' & 'FD_TO_SOCK_NAME', use a boolean type
'remote' to get local/remote address of a connection.
With these changes, it's possible to support the other connection
types which does not use socket(Ex, RDMA).
Thanks to Oran for suggestions!
Signed-off-by: zhenwei pi <pizhenwei@bytedance.com>
2022-07-27 10:08:32 +08:00
|
|
|
.addr = connSocketAddr,
|
2023-01-04 16:52:56 +08:00
|
|
|
.is_local = connSocketIsLocal,
|
Introduce .listen into connection type
Introduce listen method into connection type, this allows no hard code
of listen logic. Originally, we initialize server during startup like
this:
if (server.port)
listenToPort(server.port,&server.ipfd);
if (server.tls_port)
listenToPort(server.port,&server.tlsfd);
if (server.unixsocket)
anetUnixServer(...server.unixsocket...);
...
if (createSocketAcceptHandler(&server.ipfd, acceptTcpHandler) != C_OK)
if (createSocketAcceptHandler(&server.tlsfd, acceptTcpHandler) != C_OK)
if (createSocketAcceptHandler(&server.sofd, acceptTcpHandler) != C_OK)
...
If a new connection type gets supported, we have to add more hard code
to setup listener.
Introduce .listen and refactor listener, and Unix socket supports this.
this allows to setup listener arguments and create listener in a loop.
What's more, '.listen' is defined in connection.h, so we should include
server.h to import 'struct socketFds', but server.h has already include
'connection.h'. To avoid including loop(also to make code reasonable),
define 'struct connListener' in connection.h instead of 'struct socketFds'
in server.h. This leads this commit to get more changes.
There are more fields in 'struct connListener', hence it's possible to
simplify changeBindAddr & applyTLSPort() & updatePort() into a single
logic: update the listener config from the server.xxx, and re-create
the listener.
Because of the new field 'priv' in struct connListener, we expect to pass
this to the accept handler(even it's not used currently), this may be used
in the future.
Signed-off-by: zhenwei pi <pizhenwei@bytedance.com>
2022-07-27 12:18:28 +08:00
|
|
|
.listen = connSocketListen,
|
2022-07-27 09:39:37 +08:00
|
|
|
|
2022-11-05 00:46:37 +08:00
|
|
|
/* create/shutdown/close connection */
|
2022-07-27 10:46:31 +08:00
|
|
|
.conn_create = connCreateSocket,
|
|
|
|
.conn_create_accepted = connCreateAcceptedSocket,
|
2022-11-05 00:46:37 +08:00
|
|
|
.shutdown = connSocketShutdown,
|
2019-09-12 10:56:54 +03:00
|
|
|
.close = connSocketClose,
|
2022-07-27 09:39:37 +08:00
|
|
|
|
|
|
|
/* connect & accept */
|
|
|
|
.connect = connSocketConnect,
|
|
|
|
.blocking_connect = connSocketBlockingConnect,
|
|
|
|
.accept = connSocketAccept,
|
|
|
|
|
|
|
|
/* IO */
|
2019-09-12 10:56:54 +03:00
|
|
|
.write = connSocketWrite,
|
Reduce system calls of write for client->reply by introducing writev (#9934)
There are scenarios where it results in many small objects in the reply list,
such as commands heavily using deferred array replies (`addReplyDeferredLen`).
E.g. what COMMAND command and CLUSTER SLOTS used to do (see #10056, #7123),
but also in case of a transaction or a pipeline of commands that use just one differed array reply.
We used to have to run multiple loops along with multiple calls to `write()` to send data back to
peer based on the current code, but by means of `writev()`, we can gather those scattered
objects in reply list and include the static reply buffer as well, then send it by one system call,
that ought to achieve higher performance.
In the case of TLS, we simply check and concatenate buffers into one big buffer and send it
away by one call to `connTLSWrite()`, if the amount of all buffers exceeds `NET_MAX_WRITES_PER_EVENT`,
then invoke `connTLSWrite()` multiple times to avoid a huge massive of memory copies.
Note that aside of reducing system calls, this change will also reduce the amount of
small TCP packets sent.
2022-02-22 20:00:37 +08:00
|
|
|
.writev = connSocketWritev,
|
2019-09-12 10:56:54 +03:00
|
|
|
.read = connSocketRead,
|
|
|
|
.set_write_handler = connSocketSetWriteHandler,
|
|
|
|
.set_read_handler = connSocketSetReadHandler,
|
|
|
|
.get_last_error = connSocketGetLastError,
|
|
|
|
.sync_write = connSocketSyncWrite,
|
|
|
|
.sync_read = connSocketSyncRead,
|
2020-08-17 17:46:54 +03:00
|
|
|
.sync_readline = connSocketSyncReadLine,
|
2022-07-27 10:39:49 +08:00
|
|
|
|
|
|
|
/* pending data */
|
|
|
|
.has_pending_data = NULL,
|
|
|
|
.process_pending_data = NULL,
|
2019-09-12 10:56:54 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
int connBlock(connection *conn) {
|
|
|
|
if (conn->fd == -1) return C_ERR;
|
|
|
|
return anetBlock(NULL, conn->fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
int connNonBlock(connection *conn) {
|
|
|
|
if (conn->fd == -1) return C_ERR;
|
|
|
|
return anetNonBlock(NULL, conn->fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
int connEnableTcpNoDelay(connection *conn) {
|
|
|
|
if (conn->fd == -1) return C_ERR;
|
|
|
|
return anetEnableTcpNoDelay(NULL, conn->fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
int connDisableTcpNoDelay(connection *conn) {
|
|
|
|
if (conn->fd == -1) return C_ERR;
|
|
|
|
return anetDisableTcpNoDelay(NULL, conn->fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
int connKeepAlive(connection *conn, int interval) {
|
|
|
|
if (conn->fd == -1) return C_ERR;
|
|
|
|
return anetKeepAlive(NULL, conn->fd, interval);
|
|
|
|
}
|
|
|
|
|
|
|
|
int connSendTimeout(connection *conn, long long ms) {
|
|
|
|
return anetSendTimeout(NULL, conn->fd, ms);
|
|
|
|
}
|
|
|
|
|
|
|
|
int connRecvTimeout(connection *conn, long long ms) {
|
|
|
|
return anetRecvTimeout(NULL, conn->fd, ms);
|
|
|
|
}
|
|
|
|
|
2023-05-02 17:31:32 -07:00
|
|
|
int RedisRegisterConnectionTypeSocket(void)
|
Introduce connection layer framework
Use connTypeRegister() to register a connection type into redis, and
query connection by connectionByType() via type.
With this change, we can hide TLS specified methods into connection
type:
- void tlsInit(void);
- void tlsCleanup(void);
- int tlsConfigure(redisTLSContextConfig *ctx_config);
- int isTlsConfigured(void);
Merge isTlsConfigured & tlsConfigure, use an argument *reconfigure*
to distinguish:
tlsConfigure(&server.tls_ctx_config)
-> onnTypeConfigure(CONN_TYPE_TLS, &server.tls_ctx_config, 1)
isTlsConfigured() && tlsConfigure(&server.tls_ctx_config)
-> connTypeConfigure(CONN_TYPE_TLS, &server.tls_ctx_config, 0)
Finally, we can remove USE_OPENSSL from config.c. If redis is built
without TLS, and still run redis with TLS, then redis reports:
# Missing implement of connection type 1
# Failed to configure TLS. Check logs for more info.
The log can be optimised, let's leave it in the future. Maybe we can
use connection type as a string.
Although uninitialized fields of a static struct are zero, we still
set them as NULL explicitly in socket.c, let them clear to read & maintain:
.init = NULL,
.cleanup = NULL,
.configure = NULL,
Signed-off-by: zhenwei pi <pizhenwei@bytedance.com>
2022-08-22 15:09:59 +08:00
|
|
|
{
|
|
|
|
return connTypeRegister(&CT_Socket);
|
|
|
|
}
|