tests/9p: add Tread / Rread test client functions
Add v9fs_tread() and v9fs_rread() functions to the 9P test client for reading files from 9pfs server in test cases. Link: https://lore.kernel.org/qemu-devel/049bdd1e66416f5200fb3d59d2da5e8ec149926d.1781361555.git.qemu_oss@crudebyte.com Signed-off-by: Christian Schoenebeck <qemu_oss@crudebyte.com>
Christian Schoenebeck committed
Jun 13, 2026 at 16:55 UTC
54b8f64c422d0ed6d992387f0de419420cdb9fea
3 files changed
+79
tests/qtest/libqos/virtio-9p-client.c
+45
@@ -241,6 +241,7 @@ static const char *rmessage_name(uint8_t id)
241
id == P9_RUNLINKAT ? "RUNLINKAT" :
242
id == P9_RFLUSH ? "RFLUSH" :
243
id == P9_RREADDIR ? "RREADDIR" :
244
+ id == P9_RREAD ? "RREAD" :
245
"<unknown>";
246
}
247
@@ -1103,3 +1104,47 @@ void v9fs_runlinkat(P9Req *req)
1104
v9fs_req_recv(req, P9_RUNLINKAT);
1105
v9fs_req_free(req);
1106
}
1107
+
1108
+/* size[4] Tread tag[2] fid[4] offset[8] count[4] */
1109
+TReadRes v9fs_tread(TReadOpt opt)
1110
+{
1111
+ P9Req *req;
1112
+ uint32_t err;
1113
+
1114
+ g_assert(opt.client);
1115
+
1116
+ uint32_t body_size = 4 + 8 + 4;
1117
+
1118
+ req = v9fs_req_init(opt.client, body_size, P9_TREAD, opt.tag);
1119
+ v9fs_uint32_write(req, opt.fid);
1120
+ v9fs_uint64_write(req, opt.offset);
1121
+ v9fs_uint32_write(req, opt.count);
1122
+ v9fs_req_send(req);
1123
+
1124
+ if (!opt.requestOnly) {
1125
+ v9fs_req_wait_for_reply(req, NULL);
1126
+ if (opt.expectErr) {
1127
+ v9fs_rlerror(req, &err);
1128
+ g_assert_cmpint(err, ==, opt.expectErr);
1129
+ } else {
1130
+ v9fs_rread(req, opt.rread.count, opt.rread.data);
1131
+ }
1132
+ req = NULL; /* request was freed */
1133
+ }
1134
+
1135
+ return (TReadRes) {
1136
+ .req = req,
1137
+ .count = opt.rread.count ? *opt.rread.count : 0
1138
+ };
1139
+}
1140
+
1141
+/* size[4] Rread tag[2] count[4] data[count] */
1142
+void v9fs_rread(P9Req *req, uint32_t *count, void *data)
1143
+{
1144
+ v9fs_req_recv(req, P9_RREAD);
1145
+ v9fs_uint32_read(req, count);
1146
+ if (data && *count > 0) {
1147
+ v9fs_memread(req, data, *count);
1148
+ }
1149
+ v9fs_req_free(req);
1150
+}
tests/qtest/libqos/virtio-9p-client.h
+33
@@ -473,6 +473,37 @@ typedef struct TunlinkatRes {
473
P9Req *req;
474
} TunlinkatRes;
475
476
+/* options for 'Tread' 9p request */
477
+typedef struct TReadOpt {
478
+ /* 9P client being used (mandatory) */
479
+ QVirtio9P *client;
480
+ /* user supplied tag number being returned with response (optional) */
481
+ uint16_t tag;
482
+ /* file ID of file to read from (required) */
483
+ uint32_t fid;
484
+ /* start position of read from beginning of file (optional) */
485
+ uint64_t offset;
486
+ /* how many bytes to read (required) */
487
+ uint32_t count;
488
+ /* data being received from 9p server as 'Rread' response (optional) */
489
+ struct {
490
+ uint32_t *count;
491
+ void *data;
492
+ } rread;
493
+ /* only send Tread request but not wait for a reply? (optional) */
494
+ bool requestOnly;
495
+ /* do we expect an Rlerror response, if yes which error code? (optional) */
496
+ uint32_t expectErr;
497
+} TReadOpt;
498
+
499
+/* result of 'Tread' 9p request */
500
+typedef struct TReadRes {
501
+ /* if requestOnly was set: request object for further processing */
502
+ P9Req *req;
503
+ /* amount of bytes read */
504
+ uint32_t count;
505
+} TReadRes;
506
+
507
void v9fs_set_allocator(QGuestAllocator *t_alloc);
508
void v9fs_memwrite(P9Req *req, const void *addr, size_t len);
509
void v9fs_memskip(P9Req *req, size_t len);
@@ -524,5 +555,7 @@ TlinkRes v9fs_tlink(TlinkOpt);
555
void v9fs_rlink(P9Req *req);
556
TunlinkatRes v9fs_tunlinkat(TunlinkatOpt);
557
void v9fs_runlinkat(P9Req *req);
558
+TReadRes v9fs_tread(TReadOpt opt);
559
+void v9fs_rread(P9Req *req, uint32_t *count, void *data);
560
561
#endif
tests/qtest/virtio-9p-test.c
+1
@@ -31,6 +31,7 @@
31
#define tsymlink(...) v9fs_tsymlink((TsymlinkOpt) __VA_ARGS__)
32
#define tlink(...) v9fs_tlink((TlinkOpt) __VA_ARGS__)
33
#define tunlinkat(...) v9fs_tunlinkat((TunlinkatOpt) __VA_ARGS__)
34
+#define tread(...) v9fs_tread((TReadOpt) __VA_ARGS__)
35
36
static void pci_config(void *obj, void *data, QGuestAllocator *t_alloc)
37
{