Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[virtio][9p] Add VirtIO 9p device driver #399

Merged
merged 3 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/tests/include/app/tests.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

1 change: 1 addition & 0 deletions app/tests/rules.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
1 change: 1 addition & 0 deletions app/tests/tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
106 changes: 106 additions & 0 deletions app/tests/v9p_tests.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* 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 <app/tests.h>
#include <lk/err.h>
#include <lk/debug.h>

#define FID_ROOT 1
#define FID_RO 2

#define _LOGF(fmt, args...) \
printf("[%s:%d] " fmt, __PRETTY_FUNCTION__, __LINE__, ##args)
#define LOGF(x...) _LOGF(x)

#if WITH_DEV_VIRTIO_9P
#include <dev/virtio/9p.h>
#include <dev/virtio.h>

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;
}
#else
int v9p_tests(int argc, const console_cmd_args *argv) {
LOGF("platform didn't have dev/virtio/9p supported\n");
return ERR_NOT_SUPPORTED;
}
#endif // WITH_DEV_VIRTIO_9P
Loading