From: Vladislav Shpilevoy <v.shpilevoy@tarantool.org> To: tarantool-patches@freelists.org Cc: kostja@tarantool.org, vdavydov.dev@gmail.com Subject: [PATCH v4 11/12] [RAW] swim: introduce broadcast tasks Date: Thu, 31 Jan 2019 00:28:29 +0300 [thread overview] Message-ID: <f4de859791eada6c32ed44b3cc2cd0fff6708d3f.1548883137.git.v.shpilevoy@tarantool.org> (raw) In-Reply-To: <cover.1548883137.git.v.shpilevoy@tarantool.org> In-Reply-To: <cover.1548883137.git.v.shpilevoy@tarantool.org> When a cluster is just created, no one knows anyone. Broadcast helps to establish some initial relationships between members. Part of #3234 --- src/lib/swim/swim_io.c | 97 ++++++++++++++++++++++++++++++++++++++++++ src/lib/swim/swim_io.h | 22 ++++++++++ 2 files changed, 119 insertions(+) diff --git a/src/lib/swim/swim_io.c b/src/lib/swim/swim_io.c index e62b7126f..7bdb4bee0 100644 --- a/src/lib/swim/swim_io.c +++ b/src/lib/swim/swim_io.c @@ -32,6 +32,8 @@ #include "swim_proto.h" #include "fiber.h" #include "sio.h" +#include <ifaddrs.h> +#include <net/if.h> /** * Allocate memory for meta. The same as mere alloc, but moves @@ -147,6 +149,101 @@ swim_task_send(struct swim_task *task, const struct sockaddr_in *dst, swim_task_schedule(task, scheduler); } +/** Delete a broadcast task. */ +static void +swim_bcast_task_delete(struct swim_bcast_task *task) +{ + freeifaddrs(task->addrs); + swim_task_destroy(&task->base); + free(task); +} + +/** Delete broadcast task on its cancelation. */ +static void +swim_bcast_task_delete_cb(struct swim_task *task, + struct swim_scheduler *scheduler, int rc) +{ + (void) scheduler; + (void) rc; + swim_bcast_task_delete((struct swim_bcast_task *) task); +} + +/** + * Write down a next available broadcast address into the task + * destination field. + * @param task Broadcast task to update. + * @retval 0 Success. 'dst' field is updated. + * @retval -1 No more addresses. + */ +static int +swim_bcast_task_next_addr(struct swim_bcast_task *task) +{ + for (struct ifaddrs *i = task->i; i != NULL; i = i->ifa_next) { + int flags = i->ifa_flags; + if ((flags & IFF_UP) == 0) + continue; + + if ((flags & IFF_BROADCAST) != 0 && + (i->ifa_broadaddr->sa_family == AF_INET)) + task->base.dst = + *(struct sockaddr_in *) i->ifa_broadaddr; + else if (i->ifa_addr != NULL && + i->ifa_addr->sa_family == AF_INET) + task->base.dst = *(struct sockaddr_in *) i->ifa_addr; + else + continue; + task->base.dst.sin_port = task->port; + task->i = task->i->ifa_next; + return 0; + } + return -1; +} + +/** + * Callback on a send completion. If there are more broadcast + * addresses to use, then the task is rescheduled. Else deleted. + */ +static void +swim_bcast_task_complete(struct swim_task *base_task, + struct swim_scheduler *scheduler, int rc) +{ + (void) rc; + struct swim_bcast_task *task = (struct swim_bcast_task *) base_task; + if (swim_bcast_task_next_addr(task) != 0) + swim_bcast_task_delete(task); + else + swim_task_schedule(base_task, scheduler); +} + +struct swim_bcast_task * +swim_bcast_task_new(int port) +{ + struct swim_bcast_task *task = + (struct swim_bcast_task *) malloc(sizeof(*task)); + if (task == NULL) { + diag_set(OutOfMemory, sizeof(*task), "malloc", "task"); + return NULL; + } + struct ifaddrs *addrs; + if (getifaddrs(&addrs) != 0) { + diag_set(SystemError, "error in getifaddrs"); + free(task); + return NULL; + } + task->port = port; + task->addrs = addrs; + task->i = addrs; + swim_task_create(&task->base, swim_bcast_task_complete, + swim_bcast_task_delete_cb); + if (swim_bcast_task_next_addr(task) != 0) { + diag_set(SwimError, "broadcast has failed - no available "\ + "interfaces"); + swim_bcast_task_delete(task); + return NULL; + } + return task; +} + /** * Dispatch a next output event. Build packet meta and send the * packet. diff --git a/src/lib/swim/swim_io.h b/src/lib/swim/swim_io.h index c13b5d14f..0341d8e3c 100644 --- a/src/lib/swim/swim_io.h +++ b/src/lib/swim/swim_io.h @@ -43,6 +43,7 @@ */ struct swim_task; +struct ifaddrs; struct swim_scheduler; enum { @@ -257,4 +258,25 @@ swim_task_destroy(struct swim_task *task) rlist_del_entry(task, in_queue_output); } +/** + * Broadcast task. Besides usual task fields, stores a list of + * interfaces available for broadcast packets. The task works + * multiple times, each time sending a packet to one interface. + * After completion it is self-deleted. + */ +struct swim_bcast_task { + /** Base structure. */ + struct swim_task base; + /** Port to use for broadcast. */ + int port; + /** A list of interfaces. */ + struct ifaddrs *addrs; + /** A next interface to send to. */ + struct ifaddrs *i; +}; + +/** Create a new broadcast task with a specified port. */ +struct swim_bcast_task * +swim_bcast_task_new(int port); + #endif /* TARANTOOL_SWIM_IO_H_INCLUDED */ \ No newline at end of file -- 2.17.2 (Apple Git-113)
next prev parent reply other threads:[~2019-01-30 21:28 UTC|newest] Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top 2019-01-30 21:28 [PATCH v4 00/12] SWIM draft Vladislav Shpilevoy 2019-01-30 21:28 ` [PATCH v4 01/12] sio: introduce sio_uri_to_addr Vladislav Shpilevoy 2019-02-15 13:21 ` [tarantool-patches] " Konstantin Osipov 2019-02-15 21:22 ` [tarantool-patches] " Vladislav Shpilevoy 2019-01-30 21:28 ` [PATCH v4 10/12] [RAW] swim: introduce 'quit' message Vladislav Shpilevoy 2019-02-21 12:13 ` [tarantool-patches] " Vladislav Shpilevoy 2019-01-30 21:28 ` Vladislav Shpilevoy [this message] 2019-01-30 21:28 ` [PATCH v4 12/12] [RAW] swim: allow to use broadcast tasks to send pings Vladislav Shpilevoy 2019-01-30 21:28 ` [PATCH v4 02/12] evio: expose evio_setsockopt_server function Vladislav Shpilevoy 2019-02-15 13:21 ` [tarantool-patches] " Konstantin Osipov 2019-02-15 21:22 ` [tarantool-patches] " Vladislav Shpilevoy 2019-01-30 21:28 ` [PATCH v4 03/12] rlist: introduce rlist_add_tail_entry_sorted Vladislav Shpilevoy 2019-02-15 13:26 ` [tarantool-patches] " Konstantin Osipov 2019-02-15 13:34 ` [tarantool-patches] " Vladislav Shpilevoy 2019-02-15 18:07 ` Konstantin Osipov 2019-01-30 21:28 ` [PATCH v4 04/12] [RAW] swim: introduce SWIM's anti-entropy component Vladislav Shpilevoy 2019-02-21 18:35 ` [tarantool-patches] " Konstantin Osipov 2019-02-26 18:28 ` [tarantool-patches] " Vladislav Shpilevoy 2019-01-30 21:28 ` [PATCH v4 05/12] [RAW] swim: introduce failure detection component Vladislav Shpilevoy 2019-01-30 21:28 ` [PATCH v4 06/12] [RAW] swim: introduce dissemination component Vladislav Shpilevoy 2019-01-30 21:28 ` [PATCH v4 07/12] [RAW] swim: keep encoded round message cached Vladislav Shpilevoy 2019-01-30 21:28 ` [PATCH v4 08/12] [RAW] swim: introduce payload Vladislav Shpilevoy 2019-01-30 21:28 ` [PATCH v4 09/12] [RAW] swim: introduce routing Vladislav Shpilevoy
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=f4de859791eada6c32ed44b3cc2cd0fff6708d3f.1548883137.git.v.shpilevoy@tarantool.org \ --to=v.shpilevoy@tarantool.org \ --cc=kostja@tarantool.org \ --cc=tarantool-patches@freelists.org \ --cc=vdavydov.dev@gmail.com \ --subject='Re: [PATCH v4 11/12] [RAW] swim: introduce broadcast tasks' \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: link
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox