From ae4ec098fb2671bf299f81dd51e7a1f3d36b8ba4 Mon Sep 17 00:00:00 2001 From: Cody Wong Date: Tue, 26 Dec 2023 15:14:05 +0800 Subject: [PATCH] [tests][v9p] Add an example test for VirtIO 9p device Add a simple test to validate basic functionalities of the VirtIO 9p device driver, `dev/virtio/9p`. For now the test attaches to the first v9p device and try to read the file `LICENSE` under the shared folder. Therefore, if you set the shared folder to the git root of `lk`, it will read the `LICENSE` of the littlekernel and dump it to the console after running `v9p_tests`. For example: ``` starting internet servers starting app shell entering main console loop ] v9p_tests 0x80013c08: 2f 2a 0a 20 2a 20 43 6f 70 79 72 69 67 68 74 20 |/*. * Copyright 0x80013c18: 28 63 29 20 32 30 30 38 2d 32 30 31 35 20 54 72 |(c) 2008-2015 Tr 0x80013c28: 61 76 69 73 20 47 65 69 73 65 6c 62 72 65 63 68 |avis Geiselbrech 0x80013c38: 74 0a 20 2a 0a 20 2a 20 50 65 72 6d 69 73 73 69 |t. *. * Permissi 0x80013c48: 6f 6e 20 69 73 20 68 65 72 65 62 79 20 67 72 61 |on is hereby gra ... ``` Signed-off-by: Cody Wong --- app/tests/include/app/tests.h | 1 + app/tests/rules.mk | 4 +- app/tests/tests.c | 1 + app/tests/v9p_tests.c | 98 +++++++++++++++++++++++++++++++++++ 4 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 app/tests/v9p_tests.c diff --git a/app/tests/include/app/tests.h b/app/tests/include/app/tests.h index b93f127876..ecfe3b1b65 100644 --- a/app/tests/include/app/tests.h +++ b/app/tests/include/app/tests.h @@ -19,6 +19,7 @@ int benchmarks(int argc, const console_cmd_args *argv); int clock_tests(int argc, const console_cmd_args *argv); int printf_tests(int argc, const console_cmd_args *argv); int printf_tests_float(int argc, const console_cmd_args *argv); +int v9p_tests(int argc, const console_cmd_args *argv); #endif diff --git a/app/tests/rules.mk b/app/tests/rules.mk index 571d5e7f89..984f99e693 100644 --- a/app/tests/rules.mk +++ b/app/tests/rules.mk @@ -11,6 +11,7 @@ MODULE_SRCS := \ $(LOCAL_DIR)/tests.c \ $(LOCAL_DIR)/thread_tests.c \ $(LOCAL_DIR)/port_tests.c \ + $(LOCAL_DIR)/v9p_tests.c \ MODULE_FLOAT_SRCS := \ $(LOCAL_DIR)/benchmarks.c \ @@ -20,7 +21,8 @@ MODULE_FLOAT_SRCS := \ $(LOCAL_DIR)/printf_tests.c \ MODULE_DEPS += \ - lib/cbuf + lib/cbuf \ + dev/virtio/9p \ MODULE_COMPILEFLAGS += -fno-builtin diff --git a/app/tests/tests.c b/app/tests/tests.c index c31f958390..900330bd17 100644 --- a/app/tests/tests.c +++ b/app/tests/tests.c @@ -22,4 +22,5 @@ STATIC_COMMAND("bench", "miscellaneous benchmarks", &benchmarks) STATIC_COMMAND("fibo", "threaded fibonacci", &fibo) STATIC_COMMAND("spinner", "create a spinning thread", &spinner) STATIC_COMMAND("cbuf_tests", "test lib/cbuf", &cbuf_tests) +STATIC_COMMAND("v9p_tests", "test dev/virtio/9p", &v9p_tests) STATIC_COMMAND_END(tests); diff --git a/app/tests/v9p_tests.c b/app/tests/v9p_tests.c new file mode 100644 index 0000000000..797a8a3b92 --- /dev/null +++ b/app/tests/v9p_tests.c @@ -0,0 +1,98 @@ +/* + * Copyright (c) 2023 Cody Wong + * + * Use of this source code is governed by a MIT-style + * license that can be found in the LICENSE file or at + * https://opensource.org/licenses/MIT + */ +#include +#include +#include +#include +#include + +#define FID_ROOT 1 +#define FID_RO 2 + +#define LOGF(fmt, ...) \ + printf("[%s:%d] " fmt, __PRETTY_FUNCTION__, \ + __LINE__ __VA_OPT__(, ) __VA_ARGS__) + +int v9p_tests(int argc, const console_cmd_args *argv) { + struct virtio_device *dev = virtio_get_9p_device(0); + status_t status; + + if (dev == NULL) { + LOGF("v9p device doesn't exist\n"); + return ERR_NOT_FOUND; + } + + virtio_9p_msg_t tatt = { + .msg_type = P9_TATTACH, + .tag = P9_TAG_DEFAULT, + .msg.tattach = { + .fid = FID_ROOT, + .afid = P9_FID_NOFID, + .uname = "root", + .aname = V9P_MOUNT_ANAME, + .n_uname = P9_UNAME_NONUNAME + } + }; + virtio_9p_msg_t ratt = {}; + + status = virtio_9p_rpc(dev, &tatt, &ratt); + if (status != NO_ERROR) { + LOGF("failed to attach to the host shared folder: %d\n", status); + return status; + } + + virtio_9p_msg_t twalk = { + .msg_type = P9_TWALK, + .tag = P9_TAG_DEFAULT, + .msg.twalk = { + .fid = FID_ROOT, .newfid = FID_RO, .nwname = 1, + .wname = {"LICENSE"} + } + }; + virtio_9p_msg_t rwalk = {}; + + status = virtio_9p_rpc(dev, &twalk, &rwalk); + if (status != NO_ERROR) { + LOGF("failed to walk to the target file: %d\n", status); + return status; + } + + virtio_9p_msg_t tlopen = { + .msg_type= P9_TLOPEN, + .tag = P9_TAG_DEFAULT, + .msg.tlopen = { + .fid = FID_RO, .flags = O_RDWR, + } + }; + virtio_9p_msg_t rlopen = {}; + + status = virtio_9p_rpc(dev, &tlopen, &rlopen); + if (status != NO_ERROR) { + LOGF("failed to open the target file: %d\n", status); + return status; + } + + virtio_9p_msg_t tread = { + .msg_type= P9_TREAD, + .tag = P9_TAG_DEFAULT, + .msg.tread = { + .fid = FID_RO, .offset = 0, .count = 1024 + } + }; + virtio_9p_msg_t rread = {}; + + status = virtio_9p_rpc(dev, &tread, &rread); + if (status != NO_ERROR) { + LOGF("failed to read the target file: %d\n", status); + return status; + } + + hexdump8(rread.msg.rread.data, rread.msg.rread.count); + + return NO_ERROR; +}