Message ID | 20230418153148.2231644-7-aditi.ghag@isovalent.com (mailing list archive) |
---|---|
State | Changes Requested |
Delegated to: | BPF |
Headers | show |
Series | bpf: Add socket destroy capability | expand |
On 04/18, Aditi Ghag wrote: > The helper will be used to programmatically retrieve, > and pass ports in userspace and kernel selftest programs. > > Suggested-by: Stanislav Fomichev <sdf@google.com> > Signed-off-by: Aditi Ghag <aditi.ghag@isovalent.com> > --- > tools/testing/selftests/bpf/network_helpers.c | 28 +++++++++++++++++++ > tools/testing/selftests/bpf/network_helpers.h | 1 + > 2 files changed, 29 insertions(+) > > diff --git a/tools/testing/selftests/bpf/network_helpers.c b/tools/testing/selftests/bpf/network_helpers.c > index 596caa176582..7217cac762f0 100644 > --- a/tools/testing/selftests/bpf/network_helpers.c > +++ b/tools/testing/selftests/bpf/network_helpers.c > @@ -427,3 +427,31 @@ void close_netns(struct nstoken *token) > close(token->orig_netns_fd); > free(token); > } > + > +int get_socket_local_port(int family, int sock_fd, __u16 *out_port) > +{ > + socklen_t addr_len; > + int err; Sorry for keeping bikeshedding this part, but if you're going to do another respin, we can also drop the family argument: int get_socket_local_port(int sock_fd, __be16 *out_port) /* ^^ maybe also do be16? */ { struct sockaddr_storage addr; socklen_t addrlen; addrlen = sizeof(addr); getsockname(sock_fd, (struct sockaddr *)&addr, &addrlen); if (addr.ss_family == AF_INET) { } else if () { } } > + > + if (family == AF_INET) { > + struct sockaddr_in addr = {}; > + > + addr_len = sizeof(addr); > + err = getsockname(sock_fd, (struct sockaddr *)&addr, &addr_len); > + if (err < 0) > + return err; > + *out_port = addr.sin_port; > + return 0; > + } else if (family == AF_INET6) { > + struct sockaddr_in6 addr = {}; > + > + addr_len = sizeof(addr); > + err = getsockname(sock_fd, (struct sockaddr *)&addr, &addr_len); > + if (err < 0) > + return err; > + *out_port = addr.sin6_port; > + return 0; > + } > + > + return -1; > +} > diff --git a/tools/testing/selftests/bpf/network_helpers.h b/tools/testing/selftests/bpf/network_helpers.h > index f882c691b790..ca4a147b58b8 100644 > --- a/tools/testing/selftests/bpf/network_helpers.h > +++ b/tools/testing/selftests/bpf/network_helpers.h > @@ -56,6 +56,7 @@ int fastopen_connect(int server_fd, const char *data, unsigned int data_len, > int make_sockaddr(int family, const char *addr_str, __u16 port, > struct sockaddr_storage *addr, socklen_t *len); > char *ping_command(int family); > +int get_socket_local_port(int family, int sock_fd, __u16 *out_port); > > struct nstoken; > /** > -- > 2.34.1 >
On 4/18/23 11:45 AM, Stanislav Fomichev wrote: >> + >> +int get_socket_local_port(int family, int sock_fd, __u16 *out_port) >> +{ >> + socklen_t addr_len; >> + int err; > > Sorry for keeping bikeshedding this part, but if you're going to do > another respin, we can also drop the family argument: > > int get_socket_local_port(int sock_fd, __be16 *out_port) > /* ^^ maybe also do be16? */ I would also just return the port as the return value instead of having another arg for this. The int is more than enough. > { > struct sockaddr_storage addr; > socklen_t addrlen; > > addrlen = sizeof(addr); > getsockname(sock_fd, (struct sockaddr *)&addr, &addrlen); > > if (addr.ss_family == AF_INET) { > } else if () { > } > } > >> + >> + if (family == AF_INET) { >> + struct sockaddr_in addr = {}; >> + >> + addr_len = sizeof(addr); >> + err = getsockname(sock_fd, (struct sockaddr *)&addr, &addr_len); >> + if (err < 0) >> + return err; >> + *out_port = addr.sin_port; >> + return 0; >> + } else if (family == AF_INET6) { >> + struct sockaddr_in6 addr = {}; >> + >> + addr_len = sizeof(addr); >> + err = getsockname(sock_fd, (struct sockaddr *)&addr, &addr_len); >> + if (err < 0) >> + return err; >> + *out_port = addr.sin6_port; >> + return 0; >> + } >> + >> + return -1; >> +}
diff --git a/tools/testing/selftests/bpf/network_helpers.c b/tools/testing/selftests/bpf/network_helpers.c index 596caa176582..7217cac762f0 100644 --- a/tools/testing/selftests/bpf/network_helpers.c +++ b/tools/testing/selftests/bpf/network_helpers.c @@ -427,3 +427,31 @@ void close_netns(struct nstoken *token) close(token->orig_netns_fd); free(token); } + +int get_socket_local_port(int family, int sock_fd, __u16 *out_port) +{ + socklen_t addr_len; + int err; + + if (family == AF_INET) { + struct sockaddr_in addr = {}; + + addr_len = sizeof(addr); + err = getsockname(sock_fd, (struct sockaddr *)&addr, &addr_len); + if (err < 0) + return err; + *out_port = addr.sin_port; + return 0; + } else if (family == AF_INET6) { + struct sockaddr_in6 addr = {}; + + addr_len = sizeof(addr); + err = getsockname(sock_fd, (struct sockaddr *)&addr, &addr_len); + if (err < 0) + return err; + *out_port = addr.sin6_port; + return 0; + } + + return -1; +} diff --git a/tools/testing/selftests/bpf/network_helpers.h b/tools/testing/selftests/bpf/network_helpers.h index f882c691b790..ca4a147b58b8 100644 --- a/tools/testing/selftests/bpf/network_helpers.h +++ b/tools/testing/selftests/bpf/network_helpers.h @@ -56,6 +56,7 @@ int fastopen_connect(int server_fd, const char *data, unsigned int data_len, int make_sockaddr(int family, const char *addr_str, __u16 port, struct sockaddr_storage *addr, socklen_t *len); char *ping_command(int family); +int get_socket_local_port(int family, int sock_fd, __u16 *out_port); struct nstoken; /**
The helper will be used to programmatically retrieve, and pass ports in userspace and kernel selftest programs. Suggested-by: Stanislav Fomichev <sdf@google.com> Signed-off-by: Aditi Ghag <aditi.ghag@isovalent.com> --- tools/testing/selftests/bpf/network_helpers.c | 28 +++++++++++++++++++ tools/testing/selftests/bpf/network_helpers.h | 1 + 2 files changed, 29 insertions(+)