selftests/bpf: helpers: Add append_tid()

Some tests can't be run in parallel because they use same namespace
names or veth names.

Create an helper that appends the thread ID to a given string. 8
characters are used for it (7 digits + '\0')

Signed-off-by: Bastien Curutchet (eBPF Foundation) <bastien.curutchet@bootlin.com>
Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>
Link: https://patch.msgid.link/20250131-redirect-multi-v4-1-970b33678512@bootlin.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
This commit is contained in:
Bastien Curutchet (eBPF Foundation) 2025-01-31 08:21:40 +01:00 committed by Alexei Starovoitov
parent 12fdd29d5d
commit 723f1b9ce3
2 changed files with 29 additions and 0 deletions

View File

@ -446,6 +446,23 @@ char *ping_command(int family)
return "ping";
}
int append_tid(char *str, size_t sz)
{
size_t end;
if (!str)
return -1;
end = strlen(str);
if (end + 8 > sz)
return -1;
sprintf(&str[end], "%07d", gettid());
str[end + 7] = '\0';
return 0;
}
int remove_netns(const char *name)
{
char *cmd;

View File

@ -98,6 +98,18 @@ int send_recv_data(int lfd, int fd, uint32_t total_bytes);
int make_netns(const char *name);
int remove_netns(const char *name);
/**
* append_tid() - Append thread ID to the given string.
*
* @str: string to extend
* @sz: string's size
*
* 8 characters are used to append the thread ID (7 digits + '\0')
*
* Returns -1 on errors, 0 otherwise
*/
int append_tid(char *str, size_t sz);
static __u16 csum_fold(__u32 csum)
{
csum = (csum & 0xffff) + (csum >> 16);