Skip to content

Commit 54707e6

Browse files
author
Dorinda Bassey
committed
libkrun: Add API constants and example for vhost-user CAN
Add public API constants for vhost-user CAN devices and example usage in chroot_vm. The underlying support already exists via the generic VhostUserDevice wrapper. Example integration: - Added --vhost-user-can option to chroot_vm - Uses 3 queues (TX, RX, control) with 64-entry queue sizes - Follows same pattern as other vhost-user device integrations Signed-off-by: Dorinda Bassey <dbassey@redhat.com>
1 parent 6ebaf32 commit 54707e6

2 files changed

Lines changed: 27 additions & 0 deletions

File tree

examples/chroot_vm.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ static void print_help(char *const name)
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"
4444
" --vhost-user-vsock=PATH Use vhost-user vsock backend at socket PATH\n"
45+
" --vhost-user-can=PATH Use vhost-user CAN backend at socket PATH\n"
4546
"NET_MODE can be either TSI (default) or PASST\n"
4647
"\n"
4748
"NEWROOT: the root directory of the vm\n"
@@ -60,6 +61,7 @@ static const struct option long_options[] = {
6061
{ "vhost-user-rng", required_argument, NULL, 'V' },
6162
{ "vhost-user-snd", required_argument, NULL, 'S' },
6263
{ "vhost-user-vsock", required_argument, NULL, 'K' },
64+
{ "vhost-user-can", required_argument, NULL, 'A' },
6365
{ NULL, 0, NULL, 0 }
6466
};
6567

@@ -72,6 +74,7 @@ struct cmdline {
7274
char const *vhost_user_rng_socket;
7375
char const *vhost_user_snd_socket;
7476
char const *vhost_user_vsock_socket;
77+
char const *vhost_user_can_socket;
7578
char const *new_root;
7679
char *const *guest_argv;
7780
};
@@ -101,6 +104,7 @@ bool parse_cmdline(int argc, char *const argv[], struct cmdline *cmdline)
101104
.vhost_user_rng_socket = NULL,
102105
.vhost_user_snd_socket = NULL,
103106
.vhost_user_vsock_socket = NULL,
107+
.vhost_user_can_socket = NULL,
104108
.new_root = NULL,
105109
.guest_argv = NULL,
106110
.log_target = KRUN_LOG_TARGET_DEFAULT,
@@ -145,6 +149,9 @@ bool parse_cmdline(int argc, char *const argv[], struct cmdline *cmdline)
145149
case 'K':
146150
cmdline->vhost_user_vsock_socket = optarg;
147151
break;
152+
case 'A':
153+
cmdline->vhost_user_can_socket = optarg;
154+
break;
148155
case '?':
149156
return false;
150157
default:
@@ -317,6 +324,19 @@ int main(int argc, char *const argv[])
317324
printf("Using vhost-user vsock backend at %s\n", cmdline.vhost_user_vsock_socket);
318325
}
319326

327+
// Configure vhost-user CAN if requested
328+
if (cmdline.vhost_user_can_socket != NULL) {
329+
if (err = krun_add_vhost_user_device(ctx_id, KRUN_VIRTIO_DEVICE_CAN,
330+
cmdline.vhost_user_can_socket, NULL,
331+
KRUN_VHOST_USER_CAN_NUM_QUEUES,
332+
KRUN_VHOST_USER_CAN_QUEUE_SIZES)) {
333+
errno = -err;
334+
perror("Error adding vhost-user CAN device");
335+
return -1;
336+
}
337+
printf("Using vhost-user CAN backend at %s\n", cmdline.vhost_user_can_socket);
338+
}
339+
320340
// Raise RLIMIT_NOFILE to the maximum allowed to create some room for virtio-fs
321341
getrlimit(RLIMIT_NOFILE, &rlim);
322342
rlim.rlim_cur = rlim.rlim_max;

include/libkrun.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,13 @@ int32_t krun_set_snd_device(uint32_t ctx_id, bool enable);
747747
#define KRUN_VHOST_USER_VSOCK_NUM_QUEUES 3
748748
#define KRUN_VHOST_USER_VSOCK_QUEUE_SIZES ((uint16_t[]){128, 128, 128})
749749

750+
/**
751+
* Vhost-user CAN device default queue configuration.
752+
* CAN device uses 3 queues: TX (idx 0), RX (idx 1), control (idx 2).
753+
*/
754+
#define KRUN_VHOST_USER_CAN_NUM_QUEUES 3
755+
#define KRUN_VHOST_USER_CAN_QUEUE_SIZES ((uint16_t[]){64, 64, 64})
756+
750757
/**
751758
* Add a vhost-user device to the VM.
752759
*

0 commit comments

Comments
 (0)