staticint get_seen_count(int fd, struct sock_count counts[], int n)
{
__u64 cookie = socket_cookie(fd); int count = 0; int i = 0;
for (; cookie && !count && i < n; i++) if (cookie == counts[i].cookie)
count = counts[i].count;
return count;
}
staticvoid check_n_were_seen_once(int *fds, int fds_len, int n, struct sock_count counts[], int counts_len)
{ int seen_once = 0; int seen_cnt; int i = 0;
for (; i < fds_len; i++) { /* Skip any sockets that were closed or that weren't seen * exactly once.
*/ if (fds[i] < 0) continue;
seen_cnt = get_seen_count(fds[i], counts, counts_len); if (seen_cnt && ASSERT_EQ(seen_cnt, 1, "seen_cnt"))
seen_once++;
}
ASSERT_EQ(seen_once, n, "seen_once");
}
staticint accept_from_one(struct pollfd *server_poll_fds, int server_poll_fds_len)
{ staticconstint poll_timeout_ms = 5000; /* 5s */ int ret; int i;
ret = poll(server_poll_fds, server_poll_fds_len, poll_timeout_ms); if (!ASSERT_EQ(ret, 1, "poll")) return -1;
for (i = 0; i < server_poll_fds_len; i++) if (server_poll_fds[i].revents & POLLIN) return accept(server_poll_fds[i].fd, NULL, NULL);
return -1;
}
staticint *connect_to_server(int family, int sock_type, constchar *addr,
__u16 port, int nr_connects, int *server_fds, int server_fds_len)
{ struct pollfd *server_poll_fds = NULL; int *established_socks = NULL; int i;
server_poll_fds = calloc(server_fds_len, sizeof(*server_poll_fds)); if (!ASSERT_OK_PTR(server_poll_fds, "server_poll_fds")) return NULL;
for (i = 0; i < server_fds_len; i++) {
server_poll_fds[i].fd = server_fds[i];
server_poll_fds[i].events = POLLIN;
}
i = 0;
established_socks = malloc(sizeof(*established_socks) * nr_connects*2); if (!ASSERT_OK_PTR(established_socks, "established_socks")) goto error;
while (nr_connects--) {
established_socks[i] = connect_to_addr_str(family, sock_type,
addr, port, NULL); if (!ASSERT_OK_FD(established_socks[i], "connect_to_addr_str")) goto error;
i++;
established_socks[i] = accept_from_one(server_poll_fds,
server_fds_len); if (!ASSERT_OK_FD(established_socks[i], "accept_from_one")) goto error;
i++;
}
staticvoid remove_seen(int family, int sock_type, constchar *addr, __u16 port, int *socks, int socks_len, int *established_socks, int established_socks_len, struct sock_count *counts, int counts_len, struct bpf_link *link, int iter_fd)
{ int close_idx;
/* Iterate through the first socks_len - 1 sockets. */
read_n(iter_fd, socks_len - 1, counts, counts_len);
/* Make sure we saw socks_len - 1 sockets exactly once. */
check_n_were_seen_once(socks, socks_len, socks_len - 1, counts,
counts_len);
/* Close a socket we've already seen to remove it from the bucket. */
close_idx = get_seen_socket(socks, counts, counts_len); if (!ASSERT_GE(close_idx, 0, "close_idx")) return;
close(socks[close_idx]);
socks[close_idx] = -1;
/* Iterate through the rest of the sockets. */
read_n(iter_fd, -1, counts, counts_len);
/* Make sure the last socket wasn't skipped and that there were no * repeats.
*/
check_n_were_seen_once(socks, socks_len, socks_len - 1, counts,
counts_len);
}
staticvoid remove_seen_established(int family, int sock_type, constchar *addr,
__u16 port, int *listen_socks, int listen_socks_len, int *established_socks, int established_socks_len, struct sock_count *counts, int counts_len, struct bpf_link *link, int iter_fd)
{ int close_idx;
/* Iterate through all listening sockets. */
read_n(iter_fd, listen_socks_len, counts, counts_len);
/* Make sure we saw all listening sockets exactly once. */
check_n_were_seen_once(listen_socks, listen_socks_len, listen_socks_len,
counts, counts_len);
/* Leave one established socket. */
read_n(iter_fd, established_socks_len - 1, counts, counts_len);
/* Close a socket we've already seen to remove it from the bucket. */
close_idx = get_nth_socket(established_socks, established_socks_len,
link, listen_socks_len + 1); if (!ASSERT_GE(close_idx, 0, "close_idx")) return;
destroy(established_socks[close_idx]);
established_socks[close_idx] = -1;
/* Iterate through the rest of the sockets. */
read_n(iter_fd, -1, counts, counts_len);
/* Make sure the last socket wasn't skipped and that there were no * repeats.
*/
check_n_were_seen_once(established_socks, established_socks_len,
established_socks_len - 1, counts, counts_len);
}
staticvoid remove_unseen(int family, int sock_type, constchar *addr,
__u16 port, int *socks, int socks_len, int *established_socks, int established_socks_len, struct sock_count *counts, int counts_len, struct bpf_link *link, int iter_fd)
{ int close_idx;
/* Iterate through the first socket. */
read_n(iter_fd, 1, counts, counts_len);
/* Make sure we saw a socket from fds. */
check_n_were_seen_once(socks, socks_len, 1, counts, counts_len);
/* Close what would be the next socket in the bucket to exercise the * condition where we need to skip past the first cookie we remembered.
*/
close_idx = get_nth_socket(socks, socks_len, link, 1); if (!ASSERT_GE(close_idx, 0, "close_idx")) return;
close(socks[close_idx]);
socks[close_idx] = -1;
/* Iterate through the rest of the sockets. */
read_n(iter_fd, -1, counts, counts_len);
/* Make sure the remaining sockets were seen exactly once and that we * didn't repeat the socket that was already seen.
*/
check_n_were_seen_once(socks, socks_len, socks_len - 1, counts,
counts_len);
}
staticvoid remove_unseen_established(int family, int sock_type, constchar *addr, __u16 port, int *listen_socks, int listen_socks_len, int *established_socks, int established_socks_len, struct sock_count *counts, int counts_len, struct bpf_link *link, int iter_fd)
{ int close_idx;
/* Iterate through all listening sockets. */
read_n(iter_fd, listen_socks_len, counts, counts_len);
/* Make sure we saw all listening sockets exactly once. */
check_n_were_seen_once(listen_socks, listen_socks_len, listen_socks_len,
counts, counts_len);
/* Iterate through the first established socket. */
read_n(iter_fd, 1, counts, counts_len);
/* Make sure we saw one established socks. */
check_n_were_seen_once(established_socks, established_socks_len, 1,
counts, counts_len);
/* Close what would be the next socket in the bucket to exercise the * condition where we need to skip past the first cookie we remembered.
*/
close_idx = get_nth_socket(established_socks, established_socks_len,
link, listen_socks_len + 1); if (!ASSERT_GE(close_idx, 0, "close_idx")) return;
/* Iterate through the rest of the sockets. */
read_n(iter_fd, -1, counts, counts_len);
/* Make sure the remaining sockets were seen exactly once and that we * didn't repeat the socket that was already seen.
*/
check_n_were_seen_once(established_socks, established_socks_len,
established_socks_len - 1, counts, counts_len);
}
staticvoid remove_all(int family, int sock_type, constchar *addr,
__u16 port, int *socks, int socks_len, int *established_socks, int established_socks_len, struct sock_count *counts, int counts_len, struct bpf_link *link, int iter_fd)
{ int close_idx, i;
/* Iterate through the first socket. */
read_n(iter_fd, 1, counts, counts_len);
/* Make sure we saw a socket from fds. */
check_n_were_seen_once(socks, socks_len, 1, counts, counts_len);
/* Close all remaining sockets to exhaust the list of saved cookies and * exit without putting any sockets into the batch on the next read.
*/ for (i = 0; i < socks_len - 1; i++) {
close_idx = get_nth_socket(socks, socks_len, link, 1); if (!ASSERT_GE(close_idx, 0, "close_idx")) return;
close(socks[close_idx]);
socks[close_idx] = -1;
}
/* Make sure there are no more sockets returned */
ASSERT_EQ(read_n(iter_fd, -1, counts, counts_len), 0, "read_n");
}
staticvoid remove_all_established(int family, int sock_type, constchar *addr,
__u16 port, int *listen_socks, int listen_socks_len, int *established_socks, int established_socks_len, struct sock_count *counts, int counts_len, struct bpf_link *link, int iter_fd)
{ int *close_idx = NULL; int i;
/* Iterate through all listening sockets. */
read_n(iter_fd, listen_socks_len, counts, counts_len);
/* Make sure we saw all listening sockets exactly once. */
check_n_were_seen_once(listen_socks, listen_socks_len, listen_socks_len,
counts, counts_len);
/* Iterate through the first established socket. */
read_n(iter_fd, 1, counts, counts_len);
/* Make sure we saw one established socks. */
check_n_were_seen_once(established_socks, established_socks_len, 1,
counts, counts_len);
/* Close all remaining sockets to exhaust the list of saved cookies and * exit without putting any sockets into the batch on the next read.
*/
close_idx = malloc(sizeof(int) * (established_socks_len - 1)); if (!ASSERT_OK_PTR(close_idx, "close_idx malloc")) return; for (i = 0; i < established_socks_len - 1; i++) {
close_idx[i] = get_nth_socket(established_socks,
established_socks_len, link,
listen_socks_len + i); if (!ASSERT_GE(close_idx[i], 0, "close_idx")) return;
}
for (i = 0; i < established_socks_len - 1; i++) {
destroy(established_socks[close_idx[i]]);
established_socks[close_idx[i]] = -1;
}
/* Make sure there are no more sockets returned */
ASSERT_EQ(read_n(iter_fd, -1, counts, counts_len), 0, "read_n");
free(close_idx);
}
staticvoid add_some(int family, int sock_type, constchar *addr, __u16 port, int *socks, int socks_len, int *established_socks, int established_socks_len, struct sock_count *counts, int counts_len, struct bpf_link *link, int iter_fd)
{ int *new_socks = NULL;
/* Iterate through the first socks_len - 1 sockets. */
read_n(iter_fd, socks_len - 1, counts, counts_len);
/* Make sure we saw socks_len - 1 sockets exactly once. */
check_n_were_seen_once(socks, socks_len, socks_len - 1, counts,
counts_len);
/* Double the number of sockets in the bucket. */
new_socks = start_reuseport_server(family, sock_type, addr, port, 0,
socks_len); if (!ASSERT_OK_PTR(new_socks, "start_reuseport_server")) goto done;
/* Iterate through the rest of the sockets. */
read_n(iter_fd, -1, counts, counts_len);
/* Make sure each of the original sockets was seen exactly once. */
check_n_were_seen_once(socks, socks_len, socks_len, counts,
counts_len);
done:
free_fds(new_socks, socks_len);
}
staticvoid add_some_established(int family, int sock_type, constchar *addr,
__u16 port, int *listen_socks, int listen_socks_len, int *established_socks, int established_socks_len, struct sock_count *counts, int counts_len, struct bpf_link *link, int iter_fd)
{ int *new_socks = NULL;
/* Iterate through all listening sockets. */
read_n(iter_fd, listen_socks_len, counts, counts_len);
/* Make sure we saw all listening sockets exactly once. */
check_n_were_seen_once(listen_socks, listen_socks_len, listen_socks_len,
counts, counts_len);
/* Iterate through the first established_socks_len - 1 sockets. */
read_n(iter_fd, established_socks_len - 1, counts, counts_len);
/* Make sure we saw established_socks_len - 1 sockets exactly once. */
check_n_were_seen_once(established_socks, established_socks_len,
established_socks_len - 1, counts, counts_len);
/* Double the number of established sockets in the bucket. */
new_socks = connect_to_server(family, sock_type, addr, port,
established_socks_len / 2, listen_socks,
listen_socks_len); if (!ASSERT_OK_PTR(new_socks, "connect_to_server")) goto done;
/* Iterate through the rest of the sockets. */
read_n(iter_fd, -1, counts, counts_len);
/* Make sure each of the original sockets was seen exactly once. */
check_n_were_seen_once(listen_socks, listen_socks_len, listen_socks_len,
counts, counts_len);
check_n_were_seen_once(established_socks, established_socks_len,
established_socks_len, counts, counts_len);
done:
free_fds(new_socks, established_socks_len);
}
staticvoid force_realloc(int family, int sock_type, constchar *addr,
__u16 port, int *socks, int socks_len, int *established_socks, int established_socks_len, struct sock_count *counts, int counts_len, struct bpf_link *link, int iter_fd)
{ int *new_socks = NULL;
/* Iterate through the first socket just to initialize the batch. */
read_n(iter_fd, 1, counts, counts_len);
/* Double the number of sockets in the bucket to force a realloc on the * next read.
*/
new_socks = start_reuseport_server(family, sock_type, addr, port, 0,
socks_len); if (!ASSERT_OK_PTR(new_socks, "start_reuseport_server")) goto done;
/* Iterate through the rest of the sockets. */
read_n(iter_fd, -1, counts, counts_len);
/* Make sure each socket from the first set was seen exactly once. */
check_n_were_seen_once(socks, socks_len, socks_len, counts,
counts_len);
done:
free_fds(new_socks, socks_len);
}
staticvoid force_realloc_established(int family, int sock_type, constchar *addr, __u16 port, int *listen_socks, int listen_socks_len, int *established_socks, int established_socks_len, struct sock_count *counts, int counts_len, struct bpf_link *link, int iter_fd)
{ /* Iterate through all sockets to trigger a realloc. */
read_n(iter_fd, -1, counts, counts_len);
/* Make sure each socket was seen exactly once. */
check_n_were_seen_once(listen_socks, listen_socks_len, listen_socks_len,
counts, counts_len);
check_n_were_seen_once(established_socks, established_socks_len,
established_socks_len, counts, counts_len);
}
struct test_case { void (*test)(int family, int sock_type, constchar *addr, __u16 port, int *socks, int socks_len, int *established_socks, int established_socks_len, struct sock_count *counts, int counts_len, struct bpf_link *link, int iter_fd); constchar *description; int ehash_buckets; int connections; int init_socks; int max_socks; int sock_type; int family;
};
staticstruct test_case resume_tests[] = {
{
.description = "udp: resume after removing a seen socket",
.init_socks = nr_soreuse,
.max_socks = nr_soreuse,
.sock_type = SOCK_DGRAM,
.family = AF_INET6,
.test = remove_seen,
},
{
.description = "udp: resume after removing one unseen socket",
.init_socks = nr_soreuse,
.max_socks = nr_soreuse,
.sock_type = SOCK_DGRAM,
.family = AF_INET6,
.test = remove_unseen,
},
{
.description = "udp: resume after removing all unseen sockets",
.init_socks = nr_soreuse,
.max_socks = nr_soreuse,
.sock_type = SOCK_DGRAM,
.family = AF_INET6,
.test = remove_all,
},
{
.description = "udp: resume after adding a few sockets",
.init_socks = nr_soreuse,
.max_socks = nr_soreuse,
.sock_type = SOCK_DGRAM, /* Use AF_INET so that new sockets are added to the head of the * bucket's list.
*/
.family = AF_INET,
.test = add_some,
},
{
.description = "udp: force a realloc to occur",
.init_socks = init_batch_size,
.max_socks = init_batch_size * 2,
.sock_type = SOCK_DGRAM, /* Use AF_INET6 so that new sockets are added to the tail of the * bucket's list, needing to be added to the next batch to force * a realloc.
*/
.family = AF_INET6,
.test = force_realloc,
},
{
.description = "tcp: resume after removing a seen socket (listening)",
.init_socks = nr_soreuse,
.max_socks = nr_soreuse,
.sock_type = SOCK_STREAM,
.family = AF_INET6,
.test = remove_seen,
},
{
.description = "tcp: resume after removing one unseen socket (listening)",
.init_socks = nr_soreuse,
.max_socks = nr_soreuse,
.sock_type = SOCK_STREAM,
.family = AF_INET6,
.test = remove_unseen,
},
{
.description = "tcp: resume after removing all unseen sockets (listening)",
.init_socks = nr_soreuse,
.max_socks = nr_soreuse,
.sock_type = SOCK_STREAM,
.family = AF_INET6,
.test = remove_all,
},
{
.description = "tcp: resume after adding a few sockets (listening)",
.init_socks = nr_soreuse,
.max_socks = nr_soreuse,
.sock_type = SOCK_STREAM, /* Use AF_INET so that new sockets are added to the head of the * bucket's list.
*/
.family = AF_INET,
.test = add_some,
},
{
.description = "tcp: force a realloc to occur (listening)",
.init_socks = init_batch_size,
.max_socks = init_batch_size * 2,
.sock_type = SOCK_STREAM, /* Use AF_INET6 so that new sockets are added to the tail of the * bucket's list, needing to be added to the next batch to force * a realloc.
*/
.family = AF_INET6,
.test = force_realloc,
},
{
.description = "tcp: resume after removing a seen socket (established)", /* Force all established sockets into one bucket */
.ehash_buckets = 1,
.connections = nr_soreuse,
.init_socks = nr_soreuse, /* Room for connect()ed and accept()ed sockets */
.max_socks = nr_soreuse * 3,
.sock_type = SOCK_STREAM,
.family = AF_INET6,
.test = remove_seen_established,
},
{
.description = "tcp: resume after removing one unseen socket (established)", /* Force all established sockets into one bucket */
.ehash_buckets = 1,
.connections = nr_soreuse,
.init_socks = nr_soreuse, /* Room for connect()ed and accept()ed sockets */
.max_socks = nr_soreuse * 3,
.sock_type = SOCK_STREAM,
.family = AF_INET6,
.test = remove_unseen_established,
},
{
.description = "tcp: resume after removing all unseen sockets (established)", /* Force all established sockets into one bucket */
.ehash_buckets = 1,
.connections = nr_soreuse,
.init_socks = nr_soreuse, /* Room for connect()ed and accept()ed sockets */
.max_socks = nr_soreuse * 3,
.sock_type = SOCK_STREAM,
.family = AF_INET6,
.test = remove_all_established,
},
{
.description = "tcp: resume after adding a few sockets (established)", /* Force all established sockets into one bucket */
.ehash_buckets = 1,
.connections = nr_soreuse,
.init_socks = nr_soreuse, /* Room for connect()ed and accept()ed sockets */
.max_socks = nr_soreuse * 3,
.sock_type = SOCK_STREAM,
.family = AF_INET6,
.test = add_some_established,
},
{
.description = "tcp: force a realloc to occur (established)", /* Force all established sockets into one bucket */
.ehash_buckets = 1, /* Bucket size will need to double when going from listening to * established sockets.
*/
.connections = init_batch_size,
.init_socks = nr_soreuse, /* Room for connect()ed and accept()ed sockets */
.max_socks = nr_soreuse + (init_batch_size * 2),
.sock_type = SOCK_STREAM,
.family = AF_INET6,
.test = force_realloc_established,
},
};
err = sock_iter_batch__load(skel); if (!ASSERT_OK(err, "sock_iter_batch__load")) goto done;
link = bpf_program__attach_iter(sock_type == SOCK_STREAM ?
skel->progs.iter_tcp_soreuse :
skel->progs.iter_udp_soreuse,
NULL); if (!ASSERT_OK_PTR(link, "bpf_program__attach_iter")) goto done;
iter_fd = bpf_iter_create(bpf_link__fd(link)); if (!ASSERT_GE(iter_fd, 0, "bpf_iter_create")) goto done;
/* Test reading a bucket (either from fds[0] or fds[1]). * Only read "nr_soreuse - 1" number of sockets * from a bucket and leave one socket out from * that bucket on purpose.
*/
to_read = (nr_soreuse - 1) * sizeof(*outputs);
total_read = 0;
first_idx = -1; do {
nread = read(iter_fd, outputs, onebyone ? sizeof(*outputs) : to_read); if (nread <= 0 || nread % sizeof(*outputs)) break;
total_read += nread;
if (first_idx == -1)
first_idx = outputs[0].idx; for (i = 0; i < nread / sizeof(*outputs); i++)
ASSERT_EQ(outputs[i].idx, first_idx, "first_idx");
} while (total_read < to_read);
ASSERT_EQ(nread, onebyone ? sizeof(*outputs) : to_read, "nread");
ASSERT_EQ(total_read, to_read, "total_read");
for (i = 0; i < nread / sizeof(*outputs); i++)
ASSERT_EQ(outputs[i].idx, second_idx, "second_idx");
} while (total_read <= to_read);
ASSERT_EQ(nread, 0, "nread"); /* Both so_reuseport ports should be in different buckets, so * total_read must equal to the expected to_read. * * For a very unlikely case, both ports collide at the same bucket, * the bucket offset (i.e. 3) will be skipped and it cannot * expect the to_read number of bytes.
*/ if (skel->bss->bucket[0] != skel->bss->bucket[1])
ASSERT_EQ(total_read, to_read, "total_read");
done: for (i = 0; i < ARRAY_SIZE(fds); i++)
free_fds(fds[i], nr_soreuse); if (iter_fd < 0)
close(iter_fd);
bpf_link__destroy(link);
sock_iter_batch__destroy(skel);
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.