Skip to content

Commit 6ebaf32

Browse files
author
Dorinda Bassey
committed
libkrun: Add API constants and example for vhost-user vsock
Add public API constants for vhost-user vsock devices and example usage in chroot_vm. The underlying support already exists via the generic VhostUserDevice wrapper. Example integration: - Added --vhost-user-vsock option to chroot_vm - Calls krun_disable_implicit_vsock() to avoid conflict with built-in device - Skips TSI port mapping when vhost-user-vsock is active Signed-off-by: Dorinda Bassey <dbassey@redhat.com>
1 parent 567dcc0 commit 6ebaf32

2 files changed

Lines changed: 37 additions & 1 deletion

File tree

examples/chroot_vm.c

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ static void print_help(char *const name)
4141
" --passt-socket=PATH Instead of starting passt, connect to passt socket at PATH\n"
4242
" --vhost-user-rng=PATH Use vhost-user RNG backend at socket PATH\n"
4343
" --vhost-user-snd=PATH Use vhost-user sound backend at socket PATH\n"
44+
" --vhost-user-vsock=PATH Use vhost-user vsock backend at socket PATH\n"
4445
"NET_MODE can be either TSI (default) or PASST\n"
4546
"\n"
4647
"NEWROOT: the root directory of the vm\n"
@@ -58,6 +59,7 @@ static const struct option long_options[] = {
5859
{ "passt-socket", required_argument, NULL, 'P' },
5960
{ "vhost-user-rng", required_argument, NULL, 'V' },
6061
{ "vhost-user-snd", required_argument, NULL, 'S' },
62+
{ "vhost-user-vsock", required_argument, NULL, 'K' },
6163
{ NULL, 0, NULL, 0 }
6264
};
6365

@@ -69,6 +71,7 @@ struct cmdline {
6971
char const *passt_socket_path;
7072
char const *vhost_user_rng_socket;
7173
char const *vhost_user_snd_socket;
74+
char const *vhost_user_vsock_socket;
7275
char const *new_root;
7376
char *const *guest_argv;
7477
};
@@ -97,6 +100,7 @@ bool parse_cmdline(int argc, char *const argv[], struct cmdline *cmdline)
97100
.passt_socket_path = NULL,
98101
.vhost_user_rng_socket = NULL,
99102
.vhost_user_snd_socket = NULL,
103+
.vhost_user_vsock_socket = NULL,
100104
.new_root = NULL,
101105
.guest_argv = NULL,
102106
.log_target = KRUN_LOG_TARGET_DEFAULT,
@@ -138,6 +142,9 @@ bool parse_cmdline(int argc, char *const argv[], struct cmdline *cmdline)
138142
case 'S':
139143
cmdline->vhost_user_snd_socket = optarg;
140144
break;
145+
case 'K':
146+
cmdline->vhost_user_vsock_socket = optarg;
147+
break;
141148
case '?':
142149
return false;
143150
default:
@@ -290,6 +297,26 @@ int main(int argc, char *const argv[])
290297
printf("Using vhost-user sound backend at %s\n", cmdline.vhost_user_snd_socket);
291298
}
292299

300+
// Configure vhost-user vsock if requested
301+
if (cmdline.vhost_user_vsock_socket != NULL) {
302+
// Disable the implicit vsock device to avoid conflict
303+
if (err = krun_disable_implicit_vsock(ctx_id)) {
304+
errno = -err;
305+
perror("Error disabling implicit vsock");
306+
return -1;
307+
}
308+
309+
if (err = krun_add_vhost_user_device(ctx_id, KRUN_VIRTIO_DEVICE_VSOCK,
310+
cmdline.vhost_user_vsock_socket, NULL,
311+
KRUN_VHOST_USER_VSOCK_NUM_QUEUES,
312+
KRUN_VHOST_USER_VSOCK_QUEUE_SIZES)) {
313+
errno = -err;
314+
perror("Error adding vhost-user vsock device");
315+
return -1;
316+
}
317+
printf("Using vhost-user vsock backend at %s\n", cmdline.vhost_user_vsock_socket);
318+
}
319+
293320
// Raise RLIMIT_NOFILE to the maximum allowed to create some room for virtio-fs
294321
getrlimit(RLIMIT_NOFILE, &rlim);
295322
rlim.rlim_cur = rlim.rlim_max;
@@ -310,7 +337,8 @@ int main(int argc, char *const argv[])
310337
}
311338

312339
// Map port 18000 in the host to 8000 in the guest (if networking uses TSI)
313-
if (cmdline.net_mode == NET_MODE_TSI) {
340+
// Skip port mapping when using vhost-user-vsock (TSI requires built-in vsock)
341+
if (cmdline.net_mode == NET_MODE_TSI && cmdline.vhost_user_vsock_socket == NULL) {
314342
if (err = krun_set_port_map(ctx_id, &port_map[0])) {
315343
errno = -err;
316344
perror("Error configuring port map");

include/libkrun.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -722,6 +722,7 @@ int32_t krun_set_snd_device(uint32_t ctx_id, bool enable);
722722
* These correspond to virtio device type IDs for devices.
723723
*/
724724
#define KRUN_VIRTIO_DEVICE_RNG 4
725+
#define KRUN_VIRTIO_DEVICE_VSOCK 19
725726
#define KRUN_VIRTIO_DEVICE_SND 25
726727
#define KRUN_VIRTIO_DEVICE_CAN 36
727728

@@ -739,6 +740,13 @@ int32_t krun_set_snd_device(uint32_t ctx_id, bool enable);
739740
#define KRUN_VHOST_USER_SND_NUM_QUEUES 4
740741
#define KRUN_VHOST_USER_SND_QUEUE_SIZES ((uint16_t[]){64, 64, 64, 64})
741742

743+
/**
744+
* Vhost-user vsock device default queue configuration.
745+
* Vsock device uses 3 queues: RX (idx 0), TX (idx 1), event (idx 2).
746+
*/
747+
#define KRUN_VHOST_USER_VSOCK_NUM_QUEUES 3
748+
#define KRUN_VHOST_USER_VSOCK_QUEUE_SIZES ((uint16_t[]){128, 128, 128})
749+
742750
/**
743751
* Add a vhost-user device to the VM.
744752
*

0 commit comments

Comments
 (0)