#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "main.h" #include "utils.h" #include "uring.h" static unsigned total_malloc_count = 0; static int malloc_count = 0; LIST_HEAD(malloc_list); struct allocation { const char *allocfn; const char *callerfn; int line; void *ptr; size_t size; struct list_head list; }; static void add_allocation(const char *allocfn, const char *callerfn, int line, void *ptr, size_t size) { struct allocation *a = malloc(sizeof(*a)); debug(DBG_MALLOC, "called from %s:%i - %s(%zu) = %p\n", callerfn, line, allocfn, size, ptr); a->allocfn = allocfn; a->callerfn = callerfn; a->line = line; a->ptr = ptr; a->size = size; list_add(&a->list, &malloc_list); total_malloc_count++; malloc_count++; } void * __zmalloc(const char *fn, int line, size_t size) { void *ptr; ptr = calloc(1, size); if (ptr) add_allocation("zmalloc", fn, line, ptr, size); else perrordie("zmalloc"); return ptr; } char * __xstrdup(const char *fn, int line, const char *s) { char *ptr; ptr = strdup(s); if (ptr) add_allocation("xstrdup", fn, line, ptr, strlen(s) + 1); else perrordie("strdup"); return ptr; } char * __xstrndup(const char *fn, int line, const char *s, size_t n) { char *ptr; ptr = strndup(s, n); if (ptr) add_allocation("xstrndup", fn, line, ptr, n); else perrordie("strndup"); return ptr; } void __xfree(const char *fn, int line, void *ptr) { struct allocation *a, *tmp; unsigned delete_count = 0; if (!ptr) return; free(ptr); malloc_count--; debug(DBG_MALLOC, "called from %s:%i - %p\n", fn, line, ptr); list_for_each_entry_safe(a, tmp, &malloc_list, list) { if (a->ptr == ptr) { list_del(&a->list); free(a); delete_count++; } } if (delete_count != 1) { error("Delete count is %u for ptr 0x%p\n", delete_count, ptr); exit(EXIT_FAILURE); } } void debug_resource_usage() { struct allocation *a; DIR *dir; struct dirent *dent; char buf[4096]; ssize_t r; unsigned file_count = 0; debug(DBG_MALLOC, "Still malloced %i (total %u)\n", malloc_count, total_malloc_count); list_for_each_entry(a, &malloc_list, list) { debug(DBG_MALLOC, "* Lost allocation - %s:%i - ptr: %p, size: %zu\n", a->callerfn, a->line, a->ptr, a->size); } dir = opendir("/proc/self/fd"); if (!dir) { error("failed to open fd dir\n"); return; } debug(DBG_MALLOC, "Open files:\n"); while ((dent = readdir(dir)) != NULL) { if (!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, "..")) continue; r = readlinkat(dirfd(dir), dent->d_name, buf, sizeof(buf)); if (r < 0) { debug(DBG_MALLOC, "Failed to readlink %s\n", dent->d_name); continue; } buf[r] = '\0'; debug(DBG_MALLOC, " * %s -> %s\n", dent->d_name, buf); file_count++; } closedir(dir); if (file_count > 4) debug(DBG_MALLOC, "Lost file descriptor(s)\n"); debug(DBG_MALLOC, "CQEs used: %" PRIu64 ", SQEs used: %" PRIu64 "\n", cqe_count, sqe_count); } void socket_set_low_latency(struct cfg *cfg, int sfd) { int option; if (sfd <= 0) return; /* FIXME: could make this configurable */ option = true; if (setsockopt(sfd, SOL_SOCKET, SO_KEEPALIVE, &option, sizeof(option)) < 0) error("setsockopt: %m"); /* Doubtful if it has much effect, but can't hurt */ option = IPTOS_LOWDELAY; if (setsockopt(sfd, IPPROTO_IP, IP_TOS, &option, sizeof(option)) < 0) error("setsockopt: %m"); /* Nagle's algorithm is a poor fit for gaming */ option = true; if (setsockopt(sfd, IPPROTO_TCP, TCP_NODELAY, &option, sizeof(option)) < 0) error("setsockopt: %m"); } void connection_set_local(struct cfg *cfg, struct connection *conn, int fd) { if (fd < 0 || getsockname(fd, (struct sockaddr *)&conn->local.storage, &conn->local.addrlen) < 0) sprintf(conn->localstr, ""); else sockaddr_to_str(&conn->local, conn->localstr, sizeof(conn->localstr)); } void connection_set_remote(struct cfg *cfg, struct connection *conn, struct sockaddr_in46 *remote) { conn->remote = *remote; sockaddr_to_str(&conn->remote, conn->remotestr, sizeof(conn->remotestr)); } static void connect_next(struct cfg *cfg, struct uring_task *task, struct connection *conn); static void connect_cb(struct cfg *cfg, struct uring_task *task, int res) { struct connection *conn = task->priv; if (res < 0) { debug(DBG_UR, "%s: connection to %s failed\n", task->name, conn->remotestr); uring_task_close_fd(cfg, task); connect_next(cfg, task, conn); return; } connection_set_local(cfg, conn, task->fd); debug(DBG_UR, "%s: connection established %s -> %s\n", task->name, conn->localstr, conn->remotestr); conn->callback(cfg, conn, true); } static void connect_next(struct cfg *cfg, struct uring_task *task, struct connection *conn) { struct sockaddr_in46 *remote, *tmp; int sfd; unsigned i; again: i = 0; remote = NULL; list_for_each_entry(tmp, conn->addrs, list) { if (i == conn->next_addr) { remote = tmp; break; } i++; } if (!remote) { debug(DBG_UR, "%s: no more remote addresses to attempt\n", task->name); conn->callback(cfg, conn, false); return; } conn->next_addr++; connection_set_remote(cfg, conn, remote); debug(DBG_MALLOC, "%s: attempting to connect to %s\n", task->name, conn->remotestr); sfd = socket(conn->remote.storage.ss_family, SOCK_STREAM | SOCK_CLOEXEC, 0); if (sfd < 0) { perror("socket"); goto again; } socket_set_low_latency(cfg, sfd); task->priv = conn; uring_task_set_fd(task, sfd); uring_connect(cfg, task, &conn->remote, connect_cb); } void connect_any(struct cfg *cfg, struct uring_task *task, struct list_head *addrs, struct connection *conn, void (*callback)(struct cfg *, struct connection *, bool res)) { if (!cfg || !task || !addrs || !conn || !callback) { error("invalid arguments\n"); return; } conn->next_addr = 0; conn->addrs = addrs; conn->callback = callback; connect_next(cfg, task, conn); } uint16_t sockaddr_port(struct sockaddr_in46 *addr) { switch (addr->storage.ss_family) { case AF_INET: return ntohs(addr->in4.sin_port); case AF_INET6: return ntohs(addr->in6.sin6_port); default: return 0; } } char * sockaddr_addr(struct sockaddr_in46 *addr, char *buf, size_t len) { switch (addr->storage.ss_family) { case AF_INET: if (inet_ntop(addr->in4.sin_family, &addr->in4.sin_addr, buf, len)) return buf; break; case AF_INET6: if (inet_ntop(addr->in6.sin6_family, &addr->in6.sin6_addr, buf, len)) return buf; break; default: break; } snprintf(buf, len, ""); return buf; } char * sockaddr_to_str(struct sockaddr_in46 *addr, char *buf, size_t len) { char abuf[ADDRSTRLEN]; switch (addr->storage.ss_family) { case AF_INET: snprintf(buf, len, "AF_INET4 %s %" PRIu16, sockaddr_addr(addr, abuf, sizeof(abuf)), sockaddr_port(addr)); break; case AF_INET6: snprintf(buf, len, "AF_INET6 %s %" PRIu16, sockaddr_addr(addr, abuf, sizeof(abuf)), sockaddr_port(addr)); break; default: snprintf(buf, len, "AF_UNKNOWN"); break; } return buf; } int strtou16_strict(const char *str, uint16_t *result) { char *end; long val; if (!str) return -EINVAL; errno = 0; val = strtol(str, &end, 10); if (errno == ERANGE && (val == LONG_MAX || val == LONG_MIN)) return -EINVAL; if (errno != 0 && val == 0) return -EINVAL; if (end == str) return -EINVAL; if (*end != '\0') return -EINVAL; if (val < 1 || val > UINT16_MAX) return -EINVAL; if (result) *result = val; return 0; }