Skip to content

Commit fc0ff76

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 96a5695 commit fc0ff76

2 files changed

Lines changed: 26 additions & 0 deletions

File tree

examples/chroot_vm.c

Lines changed: 19 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"
@@ -70,6 +71,7 @@ static const struct option long_options[] = {
7071
{ "vhost-user-rng", required_argument, NULL, 'V' },
7172
{ "vhost-user-snd", required_argument, NULL, 'S' },
7273
{ "vhost-user-vsock", required_argument, NULL, 'K' },
74+
{ "vhost-user-can", required_argument, NULL, 'A' },
7375
{ NULL, 0, NULL, 0 }
7476
};
7577

@@ -82,6 +84,7 @@ struct cmdline {
8284
char const *vhost_user_rng_socket;
8385
char const *vhost_user_snd_socket;
8486
char const *vhost_user_vsock_socket;
87+
char const *vhost_user_can_socket;
8588
char const *new_root;
8689
char *const *guest_argv;
8790
};
@@ -111,6 +114,7 @@ bool parse_cmdline(int argc, char *const argv[], struct cmdline *cmdline)
111114
.vhost_user_rng_socket = NULL,
112115
.vhost_user_snd_socket = NULL,
113116
.vhost_user_vsock_socket = NULL,
117+
.vhost_user_can_socket = NULL,
114118
.new_root = NULL,
115119
.guest_argv = NULL,
116120
.log_target = KRUN_LOG_TARGET_DEFAULT,
@@ -155,6 +159,9 @@ bool parse_cmdline(int argc, char *const argv[], struct cmdline *cmdline)
155159
case 'K':
156160
cmdline->vhost_user_vsock_socket = optarg;
157161
break;
162+
case 'A':
163+
cmdline->vhost_user_can_socket = optarg;
164+
break;
158165
case '?':
159166
return false;
160167
default:
@@ -323,6 +330,18 @@ int main(int argc, char *const argv[])
323330
printf("Using vhost-user vsock backend at %s\n", cmdline.vhost_user_vsock_socket);
324331
}
325332

333+
// Configure vhost-user CAN if requested
334+
if (cmdline.vhost_user_can_socket != NULL) {
335+
if (!check_krun_error(krun_add_vhost_user_device(ctx_id, KRUN_VIRTIO_DEVICE_CAN,
336+
cmdline.vhost_user_can_socket, NULL,
337+
KRUN_VHOST_USER_CAN_NUM_QUEUES,
338+
KRUN_VHOST_USER_CAN_QUEUE_SIZES),
339+
"Error adding vhost-user CAN device")) {
340+
return -1;
341+
}
342+
printf("Using vhost-user CAN backend at %s\n", cmdline.vhost_user_can_socket);
343+
}
344+
326345
// Raise RLIMIT_NOFILE to the maximum allowed to create some room for virtio-fs
327346
getrlimit(RLIMIT_NOFILE, &rlim);
328347
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)