Created
November 12, 2025 17:10
-
-
Save congwang-mk/6e5a77e3b35ed6e48a88403aef6f986d to your computer and use it in GitHub Desktop.
Multikernel Vsock Test (Server)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <string.h> | |
| #include <unistd.h> | |
| #include <errno.h> | |
| #include <sys/socket.h> | |
| #include <linux/vm_sockets.h> | |
| #define VSOCK_TRANSPORT_VM 0 | |
| #define VSOCK_TRANSPORT_MULTIKERNEL 1 | |
| #define SO_VM_SOCKETS_TRANSPORT 9 | |
| int main(int argc, char *argv[]) | |
| { | |
| int listen_fd, conn_fd; | |
| struct sockaddr_vm sa_listen, sa_client; | |
| socklen_t sa_client_len; | |
| int port = 9000; | |
| int transport_type = VSOCK_TRANSPORT_MULTIKERNEL; | |
| char buffer[1024]; | |
| ssize_t n; | |
| if (argc > 1) { | |
| port = atoi(argv[1]); | |
| } | |
| printf("Multikernel vsock server starting on port %d\n", port); | |
| /* Create vsock socket */ | |
| listen_fd = socket(AF_VSOCK, SOCK_STREAM, 0); | |
| if (listen_fd < 0) { | |
| perror("socket"); | |
| return 1; | |
| } | |
| /* Set transport to multikernel */ | |
| if (setsockopt(listen_fd, AF_VSOCK, SO_VM_SOCKETS_TRANSPORT, | |
| &transport_type, sizeof(transport_type)) < 0) { | |
| perror("setsockopt(SO_VM_SOCKETS_TRANSPORT)"); | |
| close(listen_fd); | |
| return 1; | |
| } | |
| printf("Transport set to MULTIKERNEL\n"); | |
| /* Bind to any CID, specific port */ | |
| memset(&sa_listen, 0, sizeof(sa_listen)); | |
| sa_listen.svm_family = AF_VSOCK; | |
| sa_listen.svm_cid = VMADDR_CID_ANY; | |
| sa_listen.svm_port = port; | |
| if (bind(listen_fd, (struct sockaddr *)&sa_listen, sizeof(sa_listen)) < 0) { | |
| perror("bind"); | |
| close(listen_fd); | |
| return 1; | |
| } | |
| printf("Bound to port %d\n", port); | |
| /* Listen */ | |
| if (listen(listen_fd, 5) < 0) { | |
| perror("listen"); | |
| close(listen_fd); | |
| return 1; | |
| } | |
| printf("Listening for connections...\n"); | |
| /* Accept connections */ | |
| while (1) { | |
| sa_client_len = sizeof(sa_client); | |
| conn_fd = accept(listen_fd, (struct sockaddr *)&sa_client, &sa_client_len); | |
| if (conn_fd < 0) { | |
| perror("accept"); | |
| continue; | |
| } | |
| printf("Connection from CID %u, port %u\n", | |
| sa_client.svm_cid, sa_client.svm_port); | |
| /* Echo server */ | |
| while ((n = recv(conn_fd, buffer, sizeof(buffer), 0)) > 0) { | |
| printf("Received %zd bytes: %.*s\n", n, (int)n, buffer); | |
| if (send(conn_fd, buffer, n, 0) != n) { | |
| perror("send"); | |
| break; | |
| } | |
| } | |
| if (n < 0) { | |
| perror("recv"); | |
| } | |
| printf("Connection closed\n"); | |
| close(conn_fd); | |
| } | |
| close(listen_fd); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment