diff mbox series

[bpf-next,10/15] selftests/bpf: AF_VSOCK support for make_sockaddr

Message ID cb705cbc3a0ba5d8b4d92b4cf30187075894e81b.1721771340.git.tanggeliang@kylinos.cn (mailing list archive)
State New
Headers show
Series use network helpers, part 11 | expand

Commit Message

Geliang Tang July 23, 2024, 10:22 p.m. UTC
From: Geliang Tang <tanggeliang@kylinos.cn>

This patch adds AF_VSOCK support for make_sockaddr(), uses atoi()
to convert the given addr_str string to an integer id and sets it to
"svm_cid". Set "svm_port" as VMADDR_PORT_ANY if the value of port is
zero.

Signed-off-by: Geliang Tang <tanggeliang@kylinos.cn>
---
 tools/testing/selftests/bpf/network_helpers.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
diff mbox series

Patch

diff --git a/tools/testing/selftests/bpf/network_helpers.c b/tools/testing/selftests/bpf/network_helpers.c
index f140a76d3d7f..8b06b0bf66ae 100644
--- a/tools/testing/selftests/bpf/network_helpers.c
+++ b/tools/testing/selftests/bpf/network_helpers.c
@@ -18,6 +18,7 @@ 
 #include <linux/in6.h>
 #include <linux/limits.h>
 #include <linux/if_alg.h>
+#include <linux/vm_sockets.h>
 
 #include "bpf_util.h"
 #include "network_helpers.h"
@@ -486,6 +487,16 @@  int make_sockaddr(int family, const char *addr_str, __u16 port,
 		if (len)
 			*len = sizeof(*salg);
 		return 0;
+	} else if (family == AF_VSOCK) {
+		struct sockaddr_vm *svm = (void *)addr;
+
+		memset(addr, 0, sizeof(*svm));
+		svm->svm_family = family;
+		svm->svm_port = port ?: VMADDR_PORT_ANY;
+		svm->svm_cid = addr_str ? atoi(addr_str) : 0;
+		if (len)
+			*len = sizeof(*svm);
+		return 0;
 	}
 	return -1;
 }