Skip to content

Commit a21e929

Browse files
Dorinda Basseyslp
authored andcommitted
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 4bac857 commit a21e929

2 files changed

Lines changed: 35 additions & 1 deletion

File tree

examples/chroot_vm.c

Lines changed: 27 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"
@@ -68,6 +69,7 @@ static const struct option long_options[] = {
6869
{ "passt-socket", required_argument, NULL, 'P' },
6970
{ "vhost-user-rng", required_argument, NULL, 'V' },
7071
{ "vhost-user-snd", required_argument, NULL, 'S' },
72+
{ "vhost-user-vsock", required_argument, NULL, 'K' },
7173
{ NULL, 0, NULL, 0 }
7274
};
7375

@@ -79,6 +81,7 @@ struct cmdline {
7981
char const *passt_socket_path;
8082
char const *vhost_user_rng_socket;
8183
char const *vhost_user_snd_socket;
84+
char const *vhost_user_vsock_socket;
8285
char const *new_root;
8386
char *const *guest_argv;
8487
};
@@ -107,6 +110,7 @@ bool parse_cmdline(int argc, char *const argv[], struct cmdline *cmdline)
107110
.passt_socket_path = NULL,
108111
.vhost_user_rng_socket = NULL,
109112
.vhost_user_snd_socket = NULL,
113+
.vhost_user_vsock_socket = NULL,
110114
.new_root = NULL,
111115
.guest_argv = NULL,
112116
.log_target = KRUN_LOG_TARGET_DEFAULT,
@@ -148,6 +152,9 @@ bool parse_cmdline(int argc, char *const argv[], struct cmdline *cmdline)
148152
case 'S':
149153
cmdline->vhost_user_snd_socket = optarg;
150154
break;
155+
case 'K':
156+
cmdline->vhost_user_vsock_socket = optarg;
157+
break;
151158
case '?':
152159
return false;
153160
default:
@@ -298,6 +305,24 @@ int main(int argc, char *const argv[])
298305
printf("Using vhost-user sound backend at %s\n", cmdline.vhost_user_snd_socket);
299306
}
300307

308+
// Configure vhost-user vsock if requested
309+
if (cmdline.vhost_user_vsock_socket != NULL) {
310+
// Disable the implicit vsock device to avoid conflict
311+
if (!check_krun_error(krun_disable_implicit_vsock(ctx_id),
312+
"Error disabling implicit vsock")) {
313+
return -1;
314+
}
315+
316+
if (!check_krun_error(krun_add_vhost_user_device(ctx_id, KRUN_VIRTIO_DEVICE_VSOCK,
317+
cmdline.vhost_user_vsock_socket, NULL,
318+
KRUN_VHOST_USER_VSOCK_NUM_QUEUES,
319+
KRUN_VHOST_USER_VSOCK_QUEUE_SIZES),
320+
"Error adding vhost-user vsock device")) {
321+
return -1;
322+
}
323+
printf("Using vhost-user vsock backend at %s\n", cmdline.vhost_user_vsock_socket);
324+
}
325+
301326
// Raise RLIMIT_NOFILE to the maximum allowed to create some room for virtio-fs
302327
getrlimit(RLIMIT_NOFILE, &rlim);
303328
rlim.rlim_cur = rlim.rlim_max;
@@ -318,7 +343,8 @@ int main(int argc, char *const argv[])
318343
}
319344

320345
// Map port 18000 in the host to 8000 in the guest (if networking uses TSI)
321-
if (cmdline.net_mode == NET_MODE_TSI) {
346+
// Skip port mapping when using vhost-user-vsock (TSI requires built-in vsock)
347+
if (cmdline.net_mode == NET_MODE_TSI && cmdline.vhost_user_vsock_socket == NULL) {
322348
if (err = krun_set_port_map(ctx_id, &port_map[0])) {
323349
errno = -err;
324350
perror("Error configuring port map");

include/libkrun.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -752,6 +752,7 @@ int32_t krun_set_snd_device(uint32_t ctx_id, bool enable);
752752
* These correspond to virtio device type IDs for devices.
753753
*/
754754
#define KRUN_VIRTIO_DEVICE_RNG 4
755+
#define KRUN_VIRTIO_DEVICE_VSOCK 19
755756
#define KRUN_VIRTIO_DEVICE_SND 25
756757
#define KRUN_VIRTIO_DEVICE_CAN 36
757758

@@ -769,6 +770,13 @@ int32_t krun_set_snd_device(uint32_t ctx_id, bool enable);
769770
#define KRUN_VHOST_USER_SND_NUM_QUEUES 4
770771
#define KRUN_VHOST_USER_SND_QUEUE_SIZES ((uint16_t[]){64, 64, 64, 64})
771772

773+
/**
774+
* Vhost-user vsock device default queue configuration.
775+
* Vsock device uses 3 queues: RX (idx 0), TX (idx 1), event (idx 2).
776+
*/
777+
#define KRUN_VHOST_USER_VSOCK_NUM_QUEUES 3
778+
#define KRUN_VHOST_USER_VSOCK_QUEUE_SIZES ((uint16_t[]){128, 128, 128})
779+
772780
/**
773781
* Add a vhost-user device to the VM.
774782
*

0 commit comments

Comments
 (0)