From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtpng1.m.smailru.net (smtpng1.m.smailru.net [94.100.181.251]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id C907845C304 for ; Tue, 1 Dec 2020 02:56:20 +0300 (MSK) From: Vladislav Shpilevoy Date: Tue, 1 Dec 2020 00:56:09 +0100 Message-Id: <5625dfe8e8a67e907561d715c26b87fe46d28a8d.1606780408.git.v.shpilevoy@tarantool.org> In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [Tarantool-patches] [PATCH 01/10] test: stop using swim_transport.addr as in-param List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: tarantool-patches@dev.tarantool.org, sergepetrenko@tarantool.org SWIM unit tests contain special libraries for emulating event loop and network: swim_test_ev and swim_test_transport. They provide API similar to libev and to network part of libc, which internally is implemented entirely in user-space and allows to simulate all kinds of errors, any time durations, etc. These test libraries are going to be re-used for Raft unit tests. But for that it is necessary to detach them from all SWIM dependencies. -- One of the dependencies - swim_transport.addr, which was used in swim_transport_send() as an input parameter. swim_transport_send() simulates sendto(), and it can't be generalized while it depends on source address being passed explicitly. This patch makes swim_transport_send() deduct the source address by the file descriptor number. There is a couple of new functions for that: swim_test_sockaddr_in_to_fd and swim_test_fd_to_sockaddr_in. They were inlined earlier, but it seems the fd <-> source address translation is used often enough to extract these functions. Part of #5303 --- test/unit/swim_test_transport.c | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/test/unit/swim_test_transport.c b/test/unit/swim_test_transport.c index 334ac926e..9fad351b5 100644 --- a/test/unit/swim_test_transport.c +++ b/test/unit/swim_test_transport.c @@ -51,6 +51,25 @@ enum { FAKE_FD_NUMBER = 1000, }; +static inline int +swim_test_sockaddr_in_to_fd(const struct sockaddr_in *addr) +{ + assert(addr->sin_family == AF_INET); + return ntohs(addr->sin_port) + FAKE_FD_BASE; +} + +static inline void +swim_test_fd_to_sockaddr_in(int fd, struct sockaddr_in *addr) +{ + *addr = (struct sockaddr_in){ + .sin_family = AF_INET, + .sin_port = htons(fd) - FAKE_FD_BASE, + .sin_addr = { + .s_addr = htonl(INADDR_LOOPBACK), + }, + }; +} + /** UDP packet wrapper. It is stored in send/recv queues. */ struct swim_test_packet { /** Source address. */ @@ -289,8 +308,10 @@ swim_transport_send(struct swim_transport *transport, const void *data, */ (void) addr_size; assert(addr->sa_family == AF_INET); + struct sockaddr_in src_addr; + swim_test_fd_to_sockaddr_in(transport->fd, &src_addr); struct swim_test_packet *p = - swim_test_packet_new(data, size, &transport->addr, + swim_test_packet_new(data, size, &src_addr, (const struct sockaddr_in *) addr); struct swim_fd *src = &swim_fd[transport->fd - FAKE_FD_BASE]; assert(src->is_opened); @@ -328,7 +349,7 @@ swim_transport_bind(struct swim_transport *transport, { assert(addr->sa_family == AF_INET); const struct sockaddr_in *new_addr = (const struct sockaddr_in *) addr; - int new_fd = ntohs(new_addr->sin_port) + FAKE_FD_BASE; + int new_fd = swim_test_sockaddr_in_to_fd(new_addr); int old_fd = transport->fd; if (old_fd == new_fd) { transport->addr = *new_addr; @@ -406,7 +427,8 @@ swim_fd_send_packet(struct swim_fd *fd) } swim_test_packet_delete(p); } else { - dst = &swim_fd[ntohs(p->dst.sin_port)]; + int fdnum = swim_test_sockaddr_in_to_fd(&p->dst); + dst = &swim_fd[fdnum - FAKE_FD_BASE]; swim_move_packet(fd, dst, p); } } -- 2.24.3 (Apple Git-128)