Commit ff75a257 authored by Matt Caswell's avatar Matt Caswell
Browse files

Refactor the async wait fd logic



Implementation experience has shown that the original plan for async wait
fds was too simplistic. Originally the async logic created a pipe internally
and user/engine code could then get access to it via API calls. It is more
flexible if the engine is able to create its own fd and provide it to the
async code.

Another issue is that there can be a lot of churn in the fd value within
the context of (say) a single SSL connection leading to continually adding
and removing fds from (say) epoll. It is better if we can provide some
stability of the fd value across a whole SSL connection. This is
problematic because an engine has no concept of an SSL connection.

This commit refactors things to introduce an ASYNC_WAIT_CTX which acts as a
proxy for an SSL connection down at the engine layer.

Reviewed-by: default avatarRichard Levitte <levitte@openssl.org>
parent b32166b4
Loading
Loading
Loading
Loading
+17 −5
Original line number Diff line number Diff line
@@ -2638,15 +2638,27 @@ BIO *bio_open_default_quiet(const char *filename, char mode, int format)

void wait_for_async(SSL *s)
{
    int width, fd;
    int width = 0;
    fd_set asyncfds;
    OSSL_ASYNC_FD *fds;
    size_t numfds;

    fd = SSL_get_async_wait_fd(s);
    if (fd < 0)
    if (!SSL_get_all_async_fds(s, NULL, &numfds))
        return;
    if (numfds == 0)
        return;
    fds = OPENSSL_malloc(sizeof(OSSL_ASYNC_FD) * numfds);
    if (!SSL_get_all_async_fds(s, fds, &numfds)) {
        OPENSSL_free(fds);
    }

    width = fd + 1;
    FD_ZERO(&asyncfds);
    openssl_fdset(fd, &asyncfds);
    while (numfds > 0) {
        if (width <= (int)*fds)
            width = (int)*fds + 1;
        openssl_fdset((int)*fds, &asyncfds);
        numfds--;
        fds++;
    }
    select(width, (void *)&asyncfds, NULL, NULL, NULL);
}
+2 −2
Original line number Diff line number Diff line
@@ -17,8 +17,8 @@ TEST=
APPS=

LIB=$(TOP)/libcrypto.a
LIBSRC=async.c async_err.c arch/async_posix.c arch/async_win.c arch/async_null.c
LIBOBJ=async.o async_err.o arch/async_posix.o arch/async_win.o arch/async_null.o
LIBSRC=async.c async_wait.c async_err.c arch/async_posix.c arch/async_win.c arch/async_null.c
LIBOBJ=async.o async_wait.o async_err.o arch/async_posix.o arch/async_win.o arch/async_null.o

SRC= $(LIBSRC)

+0 −20
Original line number Diff line number Diff line
@@ -55,26 +55,6 @@

#ifdef ASYNC_NULL

int async_pipe(OSSL_ASYNC_FD *pipefds)
{
    return -1;
}

int async_close_fd(OSSL_ASYNC_FD fd)
{
    return 0;
}

int async_write1(OSSL_ASYNC_FD fd, const void *buf)
{
    return -1;
}

int async_read1(OSSL_ASYNC_FD fd, void *buf)
{
    return -1;
}

int async_global_init(void)
{
    return 0;
+0 −32
Original line number Diff line number Diff line
@@ -103,36 +103,4 @@ void async_fibre_free(async_fibre *fibre)
    fibre->fibre.uc_stack.ss_sp = NULL;
}

int async_pipe(OSSL_ASYNC_FD *pipefds)
{
    if (pipe(pipefds) == 0)
        return 1;

    return 0;
}

int async_close_fd(OSSL_ASYNC_FD fd)
{
    if (close(fd) != 0)
        return 0;

    return 1;
}

int async_write1(OSSL_ASYNC_FD fd, const void *buf)
{
    if (write(fd, buf, 1) > 0)
        return 1;

    return 0;
}

int async_read1(OSSL_ASYNC_FD fd, void *buf)
{
    if (read(fd, buf, 1) > 0)
        return 1;

    return 0;
}

#endif
+0 −36
Original line number Diff line number Diff line
@@ -127,42 +127,6 @@ VOID CALLBACK async_start_func_win(PVOID unused)
    async_start_func();
}

int async_pipe(OSSL_ASYNC_FD *pipefds)
{
    if (CreatePipe(&pipefds[0], &pipefds[1], NULL, 256) == 0)
        return 0;

    return 1;
}

int async_close_fd(OSSL_ASYNC_FD fd)
{
    if (CloseHandle(fd) == 0)
        return 0;

    return 1;
}

int async_write1(OSSL_ASYNC_FD fd, const void *buf)
{
    DWORD numwritten = 0;

    if (WriteFile(fd, buf, 1, &numwritten, NULL) && numwritten == 1)
        return 1;

    return 0;
}

int async_read1(OSSL_ASYNC_FD fd, void *buf)
{
    DWORD numread = 0;

    if (ReadFile(fd, buf, 1, &numread, NULL) && numread == 1)
        return 1;

    return 0;
}

async_pool *async_get_pool(void)
{
    return (async_pool *)TlsGetValue(asyncwinpool);
Loading