Skip to content

Commit 44c9c19

Browse files
author
Dorinda Bassey
committed
libkrun: Add API constants and example for vhost-user rtc
Add public API constants for vhost-user rtc devices and example usage in chroot_vm. The underlying support already exists via the generic VhostUserDevice wrapper. RTC devices require 2 queues (requestq, alarmq). Example integration: - Added --vhost-user-rtc option to chroot_vm Example usage: On host, start vhost-device-rtc backend vhost-device-rtc --socket-path /tmp/rtc.sock Run libkrun with RTC device ./chroot_vm --vhost-user-rtc=/tmp/rtc.sock / /bin/sh The RTC device provides: - Accurate time synchronization without NTP - Multiple clock types (UTC, TAI, monotonic) - PTP clocks for precision time protocol - Alarm support for wake-from-suspend Signed-off-by: Dorinda Bassey <dbassey@redhat.com>
1 parent 30231ab commit 44c9c19

2 files changed

Lines changed: 27 additions & 0 deletions

File tree

examples/chroot_vm.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ static void print_help(char *const name)
4040
" --net=NET_MODE Set network mode\n"
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"
43+
" --vhost-user-rtc=PATH Use vhost-user RTC backend at socket PATH\n"
4344
" --vhost-user-input=PATH Use vhost-user input backend at socket PATH\n"
4445
" --vhost-user-snd=PATH Use vhost-user sound backend at socket PATH\n"
4546
" --vhost-user-vsock=PATH Use vhost-user vsock backend at socket PATH\n"
@@ -71,6 +72,7 @@ static const struct option long_options[] = {
7172
{ "net_mode", required_argument, NULL, 'N' },
7273
{ "passt-socket", required_argument, NULL, 'P' },
7374
{ "vhost-user-rng", required_argument, NULL, 'V' },
75+
{ "vhost-user-rtc", required_argument, NULL, 'R' },
7476
{ "vhost-user-input", required_argument, NULL, 'I' },
7577
{ "vhost-user-snd", required_argument, NULL, 'S' },
7678
{ "vhost-user-vsock", required_argument, NULL, 'K' },
@@ -86,6 +88,7 @@ struct cmdline {
8688
enum net_mode net_mode;
8789
char const *passt_socket_path;
8890
char const *vhost_user_rng_socket;
91+
char const *vhost_user_rtc_socket;
8992
char const *vhost_user_input_socket;
9093
char const *vhost_user_snd_socket;
9194
char const *vhost_user_vsock_socket;
@@ -118,6 +121,7 @@ bool parse_cmdline(int argc, char *const argv[], struct cmdline *cmdline)
118121
.net_mode = NET_MODE_TSI,
119122
.passt_socket_path = NULL,
120123
.vhost_user_rng_socket = NULL,
124+
.vhost_user_rtc_socket = NULL,
121125
.vhost_user_input_socket = NULL,
122126
.vhost_user_snd_socket = NULL,
123127
.vhost_user_vsock_socket = NULL,
@@ -161,6 +165,9 @@ bool parse_cmdline(int argc, char *const argv[], struct cmdline *cmdline)
161165
case 'V':
162166
cmdline->vhost_user_rng_socket = optarg;
163167
break;
168+
case 'R':
169+
cmdline->vhost_user_rtc_socket = optarg;
170+
break;
164171
case 'I':
165172
cmdline->vhost_user_input_socket = optarg;
166173
break;
@@ -314,6 +321,18 @@ int main(int argc, char *const argv[])
314321
printf("Using vhost-user RNG backend at %s (custom queue size: 512)\n", cmdline.vhost_user_rng_socket);
315322
}
316323

324+
// Configure vhost-user RTC if requested
325+
if (cmdline.vhost_user_rtc_socket != NULL) {
326+
if (!check_krun_error(krun_add_vhost_user_device(ctx_id, KRUN_VIRTIO_DEVICE_RTC,
327+
cmdline.vhost_user_rtc_socket, NULL,
328+
KRUN_VHOST_USER_RTC_NUM_QUEUES,
329+
KRUN_VHOST_USER_RTC_QUEUE_SIZES),
330+
"Error adding vhost-user RTC device")) {
331+
return -1;
332+
}
333+
printf("Using vhost-user RTC backend at %s (available as /dev/ptp* and /dev/rtc* in guest)\n", cmdline.vhost_user_rtc_socket);
334+
}
335+
317336
// Configure vhost-user input if requested
318337
if (cmdline.vhost_user_input_socket != NULL) {
319338
if (!check_krun_error(krun_add_vhost_user_device(ctx_id, KRUN_VIRTIO_DEVICE_INPUT,

include/libkrun.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -753,6 +753,7 @@ int32_t krun_set_snd_device(uint32_t ctx_id, bool enable);
753753
*/
754754
#define KRUN_VIRTIO_DEVICE_CONSOLE 3
755755
#define KRUN_VIRTIO_DEVICE_RNG 4
756+
#define KRUN_VIRTIO_DEVICE_RTC 17
756757
#define KRUN_VIRTIO_DEVICE_INPUT 18
757758
#define KRUN_VIRTIO_DEVICE_VSOCK 19
758759
#define KRUN_VIRTIO_DEVICE_SND 25
@@ -773,6 +774,13 @@ int32_t krun_set_snd_device(uint32_t ctx_id, bool enable);
773774
#define KRUN_VHOST_USER_RNG_NUM_QUEUES 1
774775
#define KRUN_VHOST_USER_RNG_QUEUE_SIZES ((uint16_t[]){256})
775776

777+
/**
778+
* Vhost-user RTC device default queue configuration.
779+
* RTC device uses 2 queues: requestq (idx 0), alarmq (idx 1).
780+
*/
781+
#define KRUN_VHOST_USER_RTC_NUM_QUEUES 2
782+
#define KRUN_VHOST_USER_RTC_QUEUE_SIZES ((uint16_t[]){1024, 1024})
783+
776784
/**
777785
* Vhost-user input device default queue configuration.
778786
* Input device uses 2 queues: eventq (idx 0), statusq (idx 1).

0 commit comments

Comments
 (0)