__u16 expected_peer_port = 60000;
struct bpf_program *prog;
struct bpf_object *obj;
+ struct bpf_map *map;
+ __u16 *port_ptr;
+ size_t port_size;
const char *obj_file = v4 ? "connect_force_port4.bpf.o" : "connect_force_port6.bpf.o";
int fd, err;
__u32 duration = 0;
if (!ASSERT_OK_PTR(obj, "bpf_obj_open"))
return -1;
+ map = bpf_object__find_map_by_name(obj, ".bss");
+ if (!ASSERT_OK_PTR(map, "find bss map")) {
+ err = -EIO;
+ goto close_bpf_object;
+ }
+
+ port_ptr = bpf_map__initial_value(map, &port_size);
+ if (!ASSERT_OK_PTR(port_ptr, "get bss initial value")) {
+ err = -EIO;
+ goto close_bpf_object;
+ }
+
+ /* Auto assigns the port according to availability */
+ *port_ptr = ntohs(get_socket_local_port(server_fd));
+
err = bpf_object__load(obj);
if (!ASSERT_OK(err, "bpf_obj_load")) {
err = -EIO;
if (CHECK_FAIL(cgroup_fd < 0))
return;
- server_fd = start_server(AF_INET, SOCK_STREAM, NULL, 60123, 0);
+ server_fd = start_server(AF_INET, SOCK_STREAM, NULL, 0, 0);
if (CHECK_FAIL(server_fd < 0))
goto close_cgroup_fd;
CHECK_FAIL(run_test(cgroup_fd, server_fd, AF_INET, SOCK_STREAM));
close(server_fd);
- server_fd = start_server(AF_INET6, SOCK_STREAM, NULL, 60124, 0);
+ server_fd = start_server(AF_INET6, SOCK_STREAM, NULL, 0, 0);
if (CHECK_FAIL(server_fd < 0))
goto close_cgroup_fd;
CHECK_FAIL(run_test(cgroup_fd, server_fd, AF_INET6, SOCK_STREAM));
close(server_fd);
- server_fd = start_server(AF_INET, SOCK_DGRAM, NULL, 60123, 0);
+ server_fd = start_server(AF_INET, SOCK_DGRAM, NULL, 0, 0);
if (CHECK_FAIL(server_fd < 0))
goto close_cgroup_fd;
CHECK_FAIL(run_test(cgroup_fd, server_fd, AF_INET, SOCK_DGRAM));
close(server_fd);
- server_fd = start_server(AF_INET6, SOCK_DGRAM, NULL, 60124, 0);
+ server_fd = start_server(AF_INET6, SOCK_DGRAM, NULL, 0, 0);
if (CHECK_FAIL(server_fd < 0))
goto close_cgroup_fd;
CHECK_FAIL(run_test(cgroup_fd, server_fd, AF_INET6, SOCK_DGRAM));
char _license[] SEC("license") = "GPL";
+__u16 port = 0;
+
struct svc_addr {
__be32 addr;
__be16 port;
if (bpf_bind(ctx, (struct sockaddr *)&sa, sizeof(sa)) != 0)
return 0;
- /* Rewire service 1.2.3.4:60000 to backend 127.0.0.1:60123. */
+ /* Rewire service 1.2.3.4:60000 to backend 127.0.0.1:port. */
if (ctx->user_port == bpf_htons(60000)) {
orig = bpf_sk_storage_get(&service_mapping, ctx->sk, 0,
BPF_SK_STORAGE_GET_F_CREATE);
orig->port = ctx->user_port;
ctx->user_ip4 = bpf_htonl(0x7f000001);
- ctx->user_port = bpf_htons(60123);
+ ctx->user_port = bpf_htons(port);
}
return 1;
}
return 1;
/* Expose local server as 1.2.3.4:60000 to client. */
- if (ctx->user_port == bpf_htons(60123)) {
+ if (ctx->user_port == bpf_htons(port)) {
ctx->user_ip4 = bpf_htonl(0x01020304);
ctx->user_port = bpf_htons(60000);
}
return 1;
/* Expose service 1.2.3.4:60000 as peer instead of backend. */
- if (ctx->user_port == bpf_htons(60123)) {
+ if (ctx->user_port == bpf_htons(port)) {
orig = bpf_sk_storage_get(&service_mapping, ctx->sk, 0, 0);
if (orig) {
ctx->user_ip4 = orig->addr;
char _license[] SEC("license") = "GPL";
+__u16 port = 0;
+
struct svc_addr {
__be32 addr[4];
__be16 port;
if (bpf_bind(ctx, (struct sockaddr *)&sa, sizeof(sa)) != 0)
return 0;
- /* Rewire service [fc00::1]:60000 to backend [::1]:60124. */
+ /* Rewire service [fc00::1]:60000 to backend [::1]:port. */
if (ctx->user_port == bpf_htons(60000)) {
orig = bpf_sk_storage_get(&service_mapping, ctx->sk, 0,
BPF_SK_STORAGE_GET_F_CREATE);
ctx->user_ip6[1] = 0;
ctx->user_ip6[2] = 0;
ctx->user_ip6[3] = bpf_htonl(1);
- ctx->user_port = bpf_htons(60124);
+ ctx->user_port = bpf_htons(port);
}
return 1;
}
return 1;
/* Expose local server as [fc00::1]:60000 to client. */
- if (ctx->user_port == bpf_htons(60124)) {
+ if (ctx->user_port == bpf_htons(port)) {
ctx->user_ip6[0] = bpf_htonl(0xfc000000);
ctx->user_ip6[1] = 0;
ctx->user_ip6[2] = 0;
return 1;
/* Expose service [fc00::1]:60000 as peer instead of backend. */
- if (ctx->user_port == bpf_htons(60124)) {
+ if (ctx->user_port == bpf_htons(port)) {
orig = bpf_sk_storage_get(&service_mapping, ctx->sk, 0, 0);
if (orig) {
ctx->user_ip6[0] = orig->addr[0];