diff options
author | Luke T. Shumaker <lukeshu@lukeshu.com> | 2024-10-17 14:31:03 -0600 |
---|---|---|
committer | Luke T. Shumaker <lukeshu@lukeshu.com> | 2024-10-17 14:31:03 -0600 |
commit | f132dab76a07473d41e14f5f4fb1857a3229ec6a (patch) | |
tree | 621a8f1ae6cb15d360cd47c0bccd08a1c2226f4e /cmd | |
parent | a1fb6a6103cc7d38d54270bcdb9779982d329c9e (diff) |
libmisc
Diffstat (limited to 'cmd')
-rw-r--r-- | cmd/sbc_harness/CMakeLists.txt | 2 | ||||
-rw-r--r-- | cmd/sbc_harness/hw/rp2040_hwspi.c | 8 | ||||
-rw-r--r-- | cmd/sbc_harness/hw/rp2040_hwspi.h | 2 | ||||
-rw-r--r-- | cmd/sbc_harness/hw/spi.h | 7 | ||||
-rw-r--r-- | cmd/sbc_harness/hw/w5500.c | 40 | ||||
-rw-r--r-- | cmd/sbc_harness/hw/w5500.h | 18 | ||||
-rw-r--r-- | cmd/sbc_harness/main.c | 4 | ||||
-rw-r--r-- | cmd/srv9p/CMakeLists.txt | 2 | ||||
-rw-r--r-- | cmd/srv9p/gnet.c | 33 | ||||
-rw-r--r-- | cmd/srv9p/gnet.h | 8 | ||||
-rw-r--r-- | cmd/srv9p/main.c | 30 | ||||
-rw-r--r-- | cmd/srv9p/static9p.c | 156 | ||||
-rw-r--r-- | cmd/srv9p/static9p.h | 10 |
13 files changed, 163 insertions, 157 deletions
diff --git a/cmd/sbc_harness/CMakeLists.txt b/cmd/sbc_harness/CMakeLists.txt index 04f231d..c42fecb 100644 --- a/cmd/sbc_harness/CMakeLists.txt +++ b/cmd/sbc_harness/CMakeLists.txt @@ -18,8 +18,8 @@ target_link_libraries(sbc_harness hardware_spi hardware_gpio + libmisc libusb - libnet ) pico_enable_stdio_usb(sbc_harness 0) diff --git a/cmd/sbc_harness/hw/rp2040_hwspi.c b/cmd/sbc_harness/hw/rp2040_hwspi.c index e6e4566..dd9a577 100644 --- a/cmd/sbc_harness/hw/rp2040_hwspi.c +++ b/cmd/sbc_harness/hw/rp2040_hwspi.c @@ -10,9 +10,11 @@ #include <hardware/spi.h> /* pico-sdk:hardware_spi */ #include <hardware/gpio.h> /* pico-sdk:hardware_gpio5 */ +#include <libmisc/vcall.h> + #include "hw/rp2040_hwspi.h" -static void rp2040_hwspi_readwritev(struct spi *_self, const struct bidi_iovec *iov, int iovcnt); +static void rp2040_hwspi_readwritev(implements_spi *, const struct bidi_iovec *iov, int iovcnt); struct spi_vtable rp2040_hwspi_vtable = { .readwritev = rp2040_hwspi_readwritev, @@ -96,8 +98,8 @@ void _rp2040_hwspi_init(struct rp2040_hwspi *self, self->pin_cs = pin_cs; } -static void rp2040_hwspi_readwritev(struct spi *_self, const struct bidi_iovec *iov, int iovcnt) { - struct rp2040_hwspi *self = (struct rp2040_hwspi *)_self; +static void rp2040_hwspi_readwritev(implements_spi *_self, const struct bidi_iovec *iov, int iovcnt) { + struct rp2040_hwspi *self = VCALL_SELF(struct rp2040_hwspi, implements_spi, _self); assert(self); spi_inst_t *inst = self->inst; diff --git a/cmd/sbc_harness/hw/rp2040_hwspi.h b/cmd/sbc_harness/hw/rp2040_hwspi.h index 8793fd6..8bf783f 100644 --- a/cmd/sbc_harness/hw/rp2040_hwspi.h +++ b/cmd/sbc_harness/hw/rp2040_hwspi.h @@ -18,7 +18,7 @@ enum rp2040_hwspi_instance { }; struct rp2040_hwspi { - struct spi_vtable *vtable; + implements_spi; void /*spi_inst_t*/ *inst; uint pin_cs; diff --git a/cmd/sbc_harness/hw/spi.h b/cmd/sbc_harness/hw/spi.h index 86b831e..653e80f 100644 --- a/cmd/sbc_harness/hw/spi.h +++ b/cmd/sbc_harness/hw/spi.h @@ -40,11 +40,8 @@ struct spi_vtable { void (*readwritev)(struct spi *, const struct bidi_iovec *iov, int iovcnt); }; -struct spi { +typedef struct spi { struct spi_vtable *vtable; - - /* This is where your implementation data goes. */ - char data[0]; -}; +} implements_spi; #endif /* _HW_SPI_H_ */ diff --git a/cmd/sbc_harness/hw/w5500.c b/cmd/sbc_harness/hw/w5500.c index e32c589..8e3dab0 100644 --- a/cmd/sbc_harness/hw/w5500.c +++ b/cmd/sbc_harness/hw/w5500.c @@ -1,4 +1,4 @@ -/* hw/w5500.c - libnet implementation for the WIZnet W5500 chip +/* hw/w5500.c - libmisc/net.h implementation for the WIZnet W5500 chip * * Copyright (C) 2024 Luke T. Shumaker <lukeshu@lukeshu.com> * SPDX-Licence-Identifier: AGPL-3.0-or-later @@ -42,6 +42,8 @@ #include <hardware/gpio.h> /* pico-sdk:hardware_gpio5 */ #include <libcr/coroutine.h> +#include <libmisc/vcall.h> + #include "hw/w5500.h" #include "config.h" @@ -110,7 +112,7 @@ void w5500_spiframe_write(struct spi *spidev, uint16_t addr, uint8_t block, void {.iov_read_dst = NULL, .iov_write_src = header, .iov_len = sizeof(header)}, {.iov_read_dst = NULL, .iov_write_src = data, .iov_len = data_len}, }; - spidev->vtable->readwritev(spidev, iov, 2); + VCALL(spidev, readwritev, iov, 2); } void w5500_spiframe_read(struct spi *spidev, uint16_t addr, uint8_t block, void *data, size_t data_len) { @@ -128,7 +130,7 @@ void w5500_spiframe_read(struct spi *spidev, uint16_t addr, uint8_t block, void {.iov_read_dst = NULL, .iov_write_src = header, .iov_len = sizeof(header)}, {.iov_read_dst = data, .iov_write_src = NULL, .iov_len = data_len}, }; - spidev->vtable->readwritev(spidev, iov, 2); + VCALL(spidev, readwritev, iov, 2); } /* Offsets and sizes for use with that protocol. *****************************/ @@ -357,16 +359,16 @@ static_assert(sizeof(struct w5500_block_sock_reg) == 0x30); /* init() *********************************************************************/ -static struct libnet_conn *w5500_accept(struct libnet_listener *_listener); -static ssize_t w5500_read(struct libnet_conn *conn, void *buf, size_t count); -static ssize_t w5500_write(struct libnet_conn *conn, void *buf, size_t count); -static int w5500_close(struct libnet_conn *conn, bool rd, bool wr); +static implements_net_conn *w5500_accept(implements_net_listener *_listener); +static ssize_t w5500_read(implements_net_conn *conn, void *buf, size_t count); +static ssize_t w5500_write(implements_net_conn *conn, void *buf, size_t count); +static int w5500_close(implements_net_conn *conn, bool rd, bool wr); -static struct libnet_listener_vtable w5500_listener_vtable = { +static struct net_listener_vtable w5500_listener_vtable = { .accept = w5500_accept, }; -static struct libnet_conn_vtable w5500_conn_vtable = { +static struct net_conn_vtable w5500_conn_vtable = { .read = w5500_read, .write = w5500_write, .close = w5500_close, @@ -381,7 +383,7 @@ uint16_t w5500_get_local_port(struct w5500 *self) { void _w5500_init(struct w5500 *chip, struct spi* spi, uint pin_intr, uint pin_reset, - struct eth_addr addr) { + struct net_eth_addr addr) { chip->spidev = spi; chip->pin_reset = pin_reset; for (uint8_t i = 0; i < 8; i++) { @@ -418,12 +420,12 @@ void w5500_netcfg(struct w5500 *chip, struct w5500_netcfg cfg) { /* listen() *******************************************************************/ -struct libnet_listener *w5500_listen(struct w5500 *chip, uint8_t socknum, uint16_t port) { +implements_net_listener *w5500_listen(struct w5500 *chip, uint8_t socknum, uint16_t port) { assert(chip); assert(socknum < 8); assert(port); - w5500_close((struct libnet_conn *)&chip->_listeners[socknum].active_conn, true, true); + w5500_close(&chip->_listeners[socknum].active_conn, true, true); REGWRITE_SOCK(chip->spidev, socknum, mode, MODE_TCP); REGWRITE_SOCK(chip->spidev, socknum, local_port, ((uint8_t[2]){port>>8, port})); REGWRITE_SOCK(chip->spidev, socknum, command, CMD_OPEN); @@ -432,13 +434,13 @@ struct libnet_listener *w5500_listen(struct w5500 *chip, uint8_t socknum, uint16 while (REGREAD_SOCK(chip->spidev, socknum, state, uint8_t) != STATE_TCP_INIT) cr_yield(); - return (struct libnet_listener *)&chip->_listeners[socknum]; + return &chip->_listeners[socknum]; } /* accept() *******************************************************************/ #define ASSERT_LISTENER() \ - struct _w5500_listener *self = (struct _w5500_listener *)_self; \ + struct _w5500_listener *self = VCALL_SELF(struct _w5500_listener, implements_net_listener, _self); \ assert(self); \ \ struct w5500 *chip = self->chip; \ @@ -446,7 +448,7 @@ struct libnet_listener *w5500_listen(struct w5500 *chip, uint8_t socknum, uint16 assert(chip); \ assert(socknum < 8) -static struct libnet_conn *w5500_accept(struct libnet_listener *_self) { +static implements_net_conn *w5500_accept(implements_net_listener *_self) { ASSERT_LISTENER(); REGWRITE_SOCK(chip->spidev, socknum, command, CMD_LISTEN); @@ -460,7 +462,7 @@ static struct libnet_conn *w5500_accept(struct libnet_listener *_self) { cr_yield(); break; default: - return (struct libnet_conn *)&self->active_conn; + return &self->active_conn; } } } @@ -468,7 +470,7 @@ static struct libnet_conn *w5500_accept(struct libnet_listener *_self) { /* write() ********************************************************************/ #define ASSERT_CONN() \ - struct _w5500_conn *self = (struct _w5500_conn *)_self; \ + struct _w5500_conn *self = VCALL_SELF(struct _w5500_conn, implements_net_conn, _self); \ assert(self); \ \ struct w5500 *chip = self->parent_listener->chip; \ @@ -476,7 +478,7 @@ static struct libnet_conn *w5500_accept(struct libnet_listener *_self) { assert(chip); \ assert(socknum < 8) -static ssize_t w5500_write(struct libnet_conn *_self, void *buf, size_t count) { +static ssize_t w5500_write(implements_net_conn *_self, void *buf, size_t count) { ASSERT_CONN(); assert(buf); assert(count); @@ -484,7 +486,7 @@ static ssize_t w5500_write(struct libnet_conn *_self, void *buf, size_t count) { // TODO } -static int w5500_close(struct libnet_conn *_self, bool rd, bool wr) { +static int w5500_close(implements_net_conn *_self, bool rd, bool wr) { ASSERT_CONN(); REGWRITE_SOCK(chip->spidev, socknum, command, CMD_CLOSE); diff --git a/cmd/sbc_harness/hw/w5500.h b/cmd/sbc_harness/hw/w5500.h index bb629c5..4ec6edd 100644 --- a/cmd/sbc_harness/hw/w5500.h +++ b/cmd/sbc_harness/hw/w5500.h @@ -1,4 +1,4 @@ -/* hw/w5500.h - libnet implementation for the WIZnet W5500 chip +/* hw/w5500.h - libmisc/net.h implementation for the WIZnet W5500 chip * * Copyright (C) 2024 Luke T. Shumaker <lukeshu@lukeshu.com> * SPDX-Licence-Identifier: AGPL-3.0-or-later @@ -7,20 +7,20 @@ #ifndef _HW_W5500_H_ #define _HW_W5500_H_ -#include <libnet/libnet.h> +#include <libmisc/net.h> #include "hw/spi.h" struct _w5500_listener; struct _w5500_conn { - struct libnet_conn_vtable *vtable; + implements_net_conn; struct _w5500_listener *parent_listener; }; struct _w5500_listener { - struct libnet_listener_vtable *vtable; + implements_net_listener; struct w5500 *chip; uint8_t socknum; @@ -56,7 +56,7 @@ struct w5500 { } while (0) void _w5500_init(struct w5500 *self, struct spi* spi, uint pin_intr, uint pin_reset, - struct eth_addr addr); + struct net_eth_addr addr); /** * TODO. @@ -64,9 +64,9 @@ void _w5500_init(struct w5500 *self, void w5500_reset(struct w5500 *self); struct w5500_netcfg { - struct ip4_addr gateway_addr; - struct ip4_addr subnet_mask; - struct ip4_addr addr; + struct net_ip4_addr gateway_addr; + struct net_ip4_addr subnet_mask; + struct net_ip4_addr addr; }; /** @@ -74,6 +74,6 @@ struct w5500_netcfg { */ void w5500_netcfg(struct w5500 *self, struct w5500_netcfg cfg); -struct libnet_listener *w5500_listen(struct w5500 *self, uint8_t socknum, uint16_t port); +implements_net_listener *w5500_listen(struct w5500 *self, uint8_t socknum, uint16_t port); #endif /* _HW_W5500_H_ */ diff --git a/cmd/sbc_harness/main.c b/cmd/sbc_harness/main.c index 9564665..c6df8f5 100644 --- a/cmd/sbc_harness/main.c +++ b/cmd/sbc_harness/main.c @@ -44,10 +44,10 @@ int main() { 19, /* PIN_MOSI */ 18, /* PIN_CLK */ 17); /* PIN_CS */ - w5500_init(&dev_w5500, "W5500", (struct spi *)&dev_spi, + w5500_init(&dev_w5500, "W5500", &dev_spi, 21, /* PIN_INTR */ 20, /* PIN_RESET */ - ((struct eth_addr){{0xDE,0xAD,0xBE,0xEF,0x01,0x02}})); + ((struct net_eth_addr){{0xDE,0xAD,0xBE,0xEF,0x01,0x02}})); usb_common_earlyinit(); usb_keyboard_init(); diff --git a/cmd/srv9p/CMakeLists.txt b/cmd/srv9p/CMakeLists.txt index 61bd5c2..52e5171 100644 --- a/cmd/srv9p/CMakeLists.txt +++ b/cmd/srv9p/CMakeLists.txt @@ -18,7 +18,7 @@ target_include_directories(srv9p PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/config) target_link_libraries(srv9p libcr libcr_ipc - libnet + libmisc lib9p ) diff --git a/cmd/srv9p/gnet.c b/cmd/srv9p/gnet.c index 92e74c7..d7e8717 100644 --- a/cmd/srv9p/gnet.c +++ b/cmd/srv9p/gnet.c @@ -1,4 +1,4 @@ -/* srv9p/gnet.c - libnet implementation for libcr + GNU libc +/* srv9p/gnet.c - libmisc/net.h implementation for libcr + GNU libc * * Copyright (C) 2024 Luke T. Shumaker <lukeshu@lukeshu.com> * SPDX-Licence-Identifier: AGPL-3.0-or-later @@ -20,6 +20,7 @@ #include <signal.h> /* for siginfo_t, struct sigaction, enum sigval, sigaction(), SIGRTMIN, SIGRTMAX, SA_SIGINFO */ #include <libcr/coroutine.h> +#include <libmisc/vcall.h> #include "gnet.h" @@ -71,16 +72,16 @@ static inline bool RUN_PTHREAD(void *(*fn)(void *), void *args) { /* init() ( AKA socket(3) + listen(3) )****************************************/ -static struct libnet_conn *gnet_accept(struct libnet_listener *_listener); -static ssize_t gnet_read(struct libnet_conn *conn, void *buf, size_t count); -static ssize_t gnet_write(struct libnet_conn *conn, void *buf, size_t count); -static int gnet_close(struct libnet_conn *conn, bool rd, bool wr); +static implements_net_conn *gnet_accept(implements_net_listener *_listener); +static ssize_t gnet_read(implements_net_conn *conn, void *buf, size_t count); +static ssize_t gnet_write(implements_net_conn *conn, void *buf, size_t count); +static int gnet_close(implements_net_conn *conn, bool rd, bool wr); -static struct libnet_listener_vtable gnet_listener_vtable = { +static struct net_listener_vtable gnet_listener_vtable = { .accept = gnet_accept, }; -static struct libnet_conn_vtable gnet_conn_vtable = { +static struct net_conn_vtable gnet_conn_vtable = { .read = gnet_read, .write = gnet_write, .close = gnet_close, @@ -131,8 +132,8 @@ static void *_pthread_accept(void *_args) { return NULL; }; -static struct libnet_conn *gnet_accept(struct libnet_listener *_listener) { - struct gnet_listener *listener = (struct gnet_listener *)_listener; +static implements_net_conn *gnet_accept(implements_net_listener *_listener) { + struct gnet_listener *listener = VCALL_SELF(struct gnet_listener, implements_net_listener, _listener); assert(listener); int ret_connfd; @@ -147,7 +148,7 @@ static struct libnet_conn *gnet_accept(struct libnet_listener *_listener) { listener->active_conn.vtable = &gnet_conn_vtable; listener->active_conn.fd = ret_connfd; - return (struct libnet_conn *)&listener->active_conn; + return &listener->active_conn; } /* read() *********************************************************************/ @@ -172,8 +173,8 @@ static void *_pthread_read(void *_args) { return NULL; }; -static ssize_t gnet_read(struct libnet_conn *_conn, void *buf, size_t count) { - struct _gnet_conn *conn = (struct _gnet_conn *)_conn; +static ssize_t gnet_read(implements_net_conn *_conn, void *buf, size_t count) { + struct _gnet_conn *conn = VCALL_SELF(struct _gnet_conn, implements_net_conn, _conn); assert(conn); ssize_t ret; @@ -222,8 +223,8 @@ static void *_pthread_write(void *_args) { return NULL; }; -static ssize_t gnet_write(struct libnet_conn *_conn, void *buf, size_t count) { - struct _gnet_conn *conn = (struct _gnet_conn *)_conn; +static ssize_t gnet_write(implements_net_conn *_conn, void *buf, size_t count) { + struct _gnet_conn *conn = VCALL_SELF(struct _gnet_conn, implements_net_conn, _conn); assert(conn); ssize_t ret; @@ -244,8 +245,8 @@ static ssize_t gnet_write(struct libnet_conn *_conn, void *buf, size_t count) { /* close() ********************************************************************/ -static int gnet_close(struct libnet_conn *_conn, bool rd, bool wr) { - struct _gnet_conn *conn = (struct _gnet_conn *)_conn; +static int gnet_close(implements_net_conn *_conn, bool rd, bool wr) { + struct _gnet_conn *conn = VCALL_SELF(struct _gnet_conn, implements_net_conn, _conn); assert(conn); int how; diff --git a/cmd/srv9p/gnet.h b/cmd/srv9p/gnet.h index d43874f..01f2d40 100644 --- a/cmd/srv9p/gnet.h +++ b/cmd/srv9p/gnet.h @@ -1,4 +1,4 @@ -/* srv9p/gnet.h - libnet implementation for libcr + GNU libc +/* srv9p/gnet.h - libmisc/net.h implementation for libcr + GNU libc * * Copyright (C) 2024 Luke T. Shumaker <lukeshu@lukeshu.com> * SPDX-Licence-Identifier: AGPL-3.0-or-later @@ -9,16 +9,16 @@ #include <stdint.h> /* for uint16_6 */ -#include <libnet/libnet.h> +#include <libmisc/net.h> struct _gnet_conn { - struct libnet_conn_vtable *vtable; + implements_net_conn; int fd; }; struct gnet_listener { - struct libnet_listener_vtable *vtable; + implements_net_listener; int fd; struct _gnet_conn active_conn; diff --git a/cmd/srv9p/main.c b/cmd/srv9p/main.c index ae0e34e..d83fac4 100644 --- a/cmd/srv9p/main.c +++ b/cmd/srv9p/main.c @@ -8,7 +8,7 @@ #include <stdio.h> #include <libcr/coroutine.h> -#include <libnet/libnet.h> +#include <libmisc/net.h> #include <lib9p/srv.h> #include "static9p.h" @@ -29,8 +29,8 @@ /* file tree ******************************************************************/ -#define FILE_METADATA(NAME) { \ - .header = { .vtable = &static_file_vtable }, \ +#define FILE_COMMON(NAME) { \ + .vtable = &static_file_vtable, \ \ .u_name = "root", .u_num = 0, /* owner user */ \ .g_name = "root", .g_num = 0, /* owner group */ \ @@ -43,8 +43,8 @@ .mtime = 1728337904, \ } -#define DIR_METADATA(NAME) { \ - .header = { .vtable = &static_dir_vtable }, \ +#define DIR_COMMON(NAME) { \ + .vtable = &static_dir_vtable, \ \ .u_name = "root", .u_num = 0, /* owner user */ \ .g_name = "root", .g_num = 0, /* owner group */ \ @@ -59,28 +59,28 @@ #define STATIC_FILE(STRNAME, SYMNAME) \ &((static struct static_file){ \ - .metadata = FILE_METADATA(STRNAME), \ - .data_start = _binary_static_##SYMNAME##_start, \ - .data_end = _binary_static_##SYMNAME##_end, \ - }).metadata.header + ._static_common = FILE_COMMON(STRNAME), \ + .data_start = _binary_static_##SYMNAME##_start, \ + .data_end = _binary_static_##SYMNAME##_end, \ + }) static struct static_dir root = { - .metadata = DIR_METADATA(""), + ._static_common = DIR_COMMON(""), .members = { &((static struct static_dir){ - .metadata = DIR_METADATA("Documentation"), + ._static_common = DIR_COMMON("Documentation"), .members = { STATIC_FILE("x", Documentation_x), NULL }, - }).metadata.header, + }), STATIC_FILE("README.md", README_md), NULL, }, }; -static struct lib9p_srv_file *get_root(struct lib9p_srv_ctx *UNUSED(ctx), char *UNUSED(treename)) { - return &root.metadata.header; +static implements_lib9p_srv_file *get_root(struct lib9p_srv_ctx *UNUSED(ctx), char *UNUSED(treename)) { + return &root; } /* main ***********************************************************************/ @@ -94,7 +94,7 @@ static COROUTINE read_cr(void *_srv) { struct gnet_listener listener; gnet_listener_init(&listener, 9000); - lib9p_srv_read_cr(srv, (struct libnet_listener *)&listener); + lib9p_srv_read_cr(srv, &listener); cr_end(); } diff --git a/cmd/srv9p/static9p.c b/cmd/srv9p/static9p.c index f508ff1..47c07be 100644 --- a/cmd/srv9p/static9p.c +++ b/cmd/srv9p/static9p.c @@ -6,61 +6,63 @@ #include <assert.h> +#include <libmisc/vcall.h> + #include "static9p.h" #define UNUSED(name) /* name __attribute__((unused)) */ #define p9_str(cstr) ((struct lib9p_s){ .len = strlen(cstr), .utf8 = cstr }) #define p9_nulstr ((struct lib9p_s){ .len = 0, .utf8 = NULL }) -/* common metadata ************************************************************/ +/* common *********************************************************************/ -static struct lib9p_srv_file *static_metadata_clone(struct lib9p_srv_ctx *ctx, struct lib9p_srv_file *_file) { +static implements_lib9p_srv_file *static_common_clone(implements_lib9p_srv_file *_self, struct lib9p_srv_ctx *ctx) { + _static_common *self = VCALL_SELF(_static_common, implements_lib9p_srv_file, _self); + assert(self); assert(ctx); - struct static_metadata *file = (struct static_metadata *)_file; - assert(file); - return &file->header; + return self; } -static void static_metadata_free(struct lib9p_srv_ctx *ctx, struct lib9p_srv_file *_file) { +static void static_common_free(implements_lib9p_srv_file *_self, struct lib9p_srv_ctx *ctx) { + _static_common *self = VCALL_SELF(_static_common, implements_lib9p_srv_file, _self); + assert(self); assert(ctx); - struct static_metadata *file = (struct static_metadata *)_file; - assert(file); /* do nothing */ } -static uint32_t static_metadata_io(struct lib9p_srv_ctx *ctx, struct lib9p_srv_file *_file, lib9p_o_t UNUSED(flags)) { +static uint32_t static_common_io(implements_lib9p_srv_file *_self, struct lib9p_srv_ctx *ctx, lib9p_o_t UNUSED(flags)) { + _static_common *self = VCALL_SELF(_static_common, implements_lib9p_srv_file, _self); + assert(self); assert(ctx); - struct static_metadata *file = (struct static_metadata *)_file; - assert(file); return 0; } -static void static_metadata_wstat(struct lib9p_srv_ctx *ctx, struct lib9p_srv_file *_file, +static void static_common_wstat(implements_lib9p_srv_file *_self, struct lib9p_srv_ctx *ctx, struct lib9p_stat UNUSED(new)) { + _static_common *self = VCALL_SELF(_static_common, implements_lib9p_srv_file, _self); + assert(self); assert(ctx); - struct static_metadata *file = (struct static_metadata *)_file; - assert(file); lib9p_error(&ctx->basectx, LINUX_EROFS, "read-only part of filesystem"); } -static void static_metadata_remove(struct lib9p_srv_ctx *ctx, struct lib9p_srv_file *_file) { +static void static_common_remove(implements_lib9p_srv_file *_self, struct lib9p_srv_ctx *ctx) { + _static_common *self = VCALL_SELF(_static_common, implements_lib9p_srv_file, _self); + assert(self); assert(ctx); - struct static_metadata *file = (struct static_metadata *)_file; - assert(file); lib9p_error(&ctx->basectx, LINUX_EROFS, "read-only part of filesystem"); } /* dir ************************************************************************/ -static struct lib9p_stat static_dir_stat(struct lib9p_srv_ctx *ctx, struct lib9p_srv_file *_file) { +static struct lib9p_stat static_dir_stat(implements_lib9p_srv_file *_self, struct lib9p_srv_ctx *ctx) { + struct static_dir *self = VCALL_SELF(struct static_dir, implements_lib9p_srv_file, _self); + assert(self); assert(ctx); - struct static_dir *file = (struct static_dir *)_file; - assert(file); return (struct lib9p_stat){ .kern_type = 0, @@ -68,32 +70,32 @@ static struct lib9p_stat static_dir_stat(struct lib9p_srv_ctx *ctx, struct lib9p .file_qid = { .type = LIB9P_QT_DIR, .vers = 1, - .path = file->metadata.pathnum, + .path = self->pathnum, }, - .file_mode = LIB9P_DM_DIR | (file->metadata.perm & 0555), - .file_atime = file->metadata.atime, - .file_mtime = file->metadata.mtime, + .file_mode = LIB9P_DM_DIR | (self->perm & 0555), + .file_atime = self->atime, + .file_mtime = self->mtime, .file_size = 0, - .file_name = p9_str(file->metadata.name), - .file_owner_uid = p9_str(file->metadata.u_name), - .file_owner_gid = p9_str(file->metadata.g_name), - .file_last_modified_uid = p9_str(file->metadata.m_name), + .file_name = p9_str(self->name), + .file_owner_uid = p9_str(self->u_name), + .file_owner_gid = p9_str(self->g_name), + .file_last_modified_uid = p9_str(self->m_name), .file_extension = p9_nulstr, - .file_owner_n_uid = file->metadata.u_num, - .file_owner_n_gid = file->metadata.g_num, - .file_last_modified_n_uid = file->metadata.m_num, + .file_owner_n_uid = self->u_num, + .file_owner_n_gid = self->g_num, + .file_last_modified_n_uid = self->m_num, }; } -static struct lib9p_srv_file *static_dir_dopen(struct lib9p_srv_ctx *ctx, struct lib9p_srv_file *_dir, +static implements_lib9p_srv_file *static_dir_dopen(implements_lib9p_srv_file *_self, struct lib9p_srv_ctx *ctx, char *childname) { + struct static_dir *self = VCALL_SELF(struct static_dir, implements_lib9p_srv_file, _self); + assert(self); assert(ctx); - struct static_dir *dir = (struct static_dir *)_dir; - assert(dir); - for (size_t i = 0; dir->members[i]; i++) { - struct lib9p_srv_file *filep = dir->members[i]; - struct lib9p_stat stat = filep->vtable->stat(ctx, filep); + for (size_t i = 0; self->members[i]; i++) { + implements_lib9p_srv_file *filep = self->members[i]; + struct lib9p_stat stat = VCALL(filep, stat, ctx); if (lib9p_ctx_has_error(&ctx->basectx)) break; lib9p_assert_stat(stat); @@ -105,30 +107,30 @@ static struct lib9p_srv_file *static_dir_dopen(struct lib9p_srv_ctx *ctx, struct return NULL; } -static struct lib9p_srv_file *static_dir_dcreate(struct lib9p_srv_ctx *ctx, struct lib9p_srv_file *_dir, +static implements_lib9p_srv_file *static_dir_dcreate(implements_lib9p_srv_file *_self, struct lib9p_srv_ctx *ctx, char *UNUSED(childname), lib9p_dm_t UNUSED(perm), lib9p_o_t UNUSED(flags)) { + struct static_dir *self = VCALL_SELF(struct static_dir, implements_lib9p_srv_file, _self); + assert(self); assert(ctx); - struct static_dir *dir = (struct static_dir *)_dir; - assert(dir); lib9p_error(&ctx->basectx, LINUX_EROFS, "read-only part of filesystem"); return NULL; } -static size_t static_dir_dread(struct lib9p_srv_ctx *ctx, struct lib9p_srv_file *_dir, +static size_t static_dir_dread(implements_lib9p_srv_file *_self, struct lib9p_srv_ctx *ctx, uint8_t *buf, uint32_t byte_count, size_t _obj_offset) { + struct static_dir *self = VCALL_SELF(struct static_dir, implements_lib9p_srv_file, _self); + assert(self); assert(ctx); - struct static_dir *dir = (struct static_dir *)_dir; - assert(dir); uint32_t byte_offset = 0; size_t obj_offset = _obj_offset; - while (dir->members[obj_offset]) { - struct lib9p_srv_file *filep = dir->members[obj_offset]; - struct lib9p_stat stat = filep->vtable->stat(ctx, filep); + while (self->members[obj_offset]) { + implements_lib9p_srv_file *filep = self->members[obj_offset]; + struct lib9p_stat stat = VCALL(filep, stat, ctx); if (lib9p_ctx_has_error(&ctx->basectx)) break; lib9p_assert_stat(stat); @@ -147,13 +149,13 @@ static size_t static_dir_dread(struct lib9p_srv_ctx *ctx, struct lib9p_srv_file } struct lib9p_srv_file_vtable static_dir_vtable = { - .clone = static_metadata_clone, - .free = static_metadata_free, + .clone = static_common_clone, + .free = static_common_free, - .io = static_metadata_io, + .io = static_common_io, .stat = static_dir_stat, - .wstat = static_metadata_wstat, - .remove = static_metadata_remove, + .wstat = static_common_wstat, + .remove = static_common_remove, .dopen = static_dir_dopen, .dcreate = static_dir_dcreate, @@ -166,16 +168,18 @@ struct lib9p_srv_file_vtable static_dir_vtable = { static inline size_t static_file_size(struct static_file *file) { assert(file); +#if __x86_64__ assert(file->data_start); +#endif if (!file->data_end) return file->data_size; return (size_t)((uintptr_t)file->data_end - (uintptr_t)file->data_start); } -static struct lib9p_stat static_file_stat(struct lib9p_srv_ctx *ctx, struct lib9p_srv_file *_file) { +static struct lib9p_stat static_file_stat(implements_lib9p_srv_file *_self, struct lib9p_srv_ctx *ctx) { + struct static_file *self = VCALL_SELF(struct static_file, implements_lib9p_srv_file, _self); + assert(self); assert(ctx); - struct static_file *file = (struct static_file *)_file; - assert(file); return (struct lib9p_stat){ .kern_type = 0, @@ -183,32 +187,32 @@ static struct lib9p_stat static_file_stat(struct lib9p_srv_ctx *ctx, struct lib9 .file_qid = { .type = LIB9P_QT_FILE, .vers = 1, - .path = file->metadata.pathnum, + .path = self->pathnum, }, - .file_mode = file->metadata.perm & 0444, - .file_atime = file->metadata.atime, - .file_mtime = file->metadata.mtime, - .file_size = (uint64_t)static_file_size(file), - .file_name = p9_str(file->metadata.name), - .file_owner_uid = p9_str(file->metadata.u_name), - .file_owner_gid = p9_str(file->metadata.g_name), - .file_last_modified_uid = p9_str(file->metadata.m_name), + .file_mode = self->perm & 0444, + .file_atime = self->atime, + .file_mtime = self->mtime, + .file_size = (uint64_t)static_file_size(self), + .file_name = p9_str(self->name), + .file_owner_uid = p9_str(self->u_name), + .file_owner_gid = p9_str(self->g_name), + .file_last_modified_uid = p9_str(self->m_name), .file_extension = p9_nulstr, - .file_owner_n_uid = file->metadata.u_num, - .file_owner_n_gid = file->metadata.g_num, - .file_last_modified_n_uid = file->metadata.m_num, + .file_owner_n_uid = self->u_num, + .file_owner_n_gid = self->g_num, + .file_last_modified_n_uid = self->m_num, }; } -static uint32_t static_file_pread(struct lib9p_srv_ctx *ctx, struct lib9p_srv_file *_file, +static uint32_t static_file_pread(implements_lib9p_srv_file *_self, struct lib9p_srv_ctx *ctx, void *buf, uint32_t byte_count, uint64_t byte_offset) { + struct static_file *self = VCALL_SELF(struct static_file, implements_lib9p_srv_file, _self); + assert(self); assert(ctx); - struct static_file *file = (struct static_file *)_file; - assert(file); - size_t data_size = static_file_size(file); + size_t data_size = static_file_size(self); if (byte_offset > (uint64_t)data_size) { lib9p_error(&ctx->basectx, @@ -220,18 +224,18 @@ static uint32_t static_file_pread(struct lib9p_srv_ctx *ctx, struct lib9p_srv_fi size_t end_off = beg_off + (size_t)byte_count; if (end_off > data_size) end_off = data_size; - memcpy(buf, &file->data_start[beg_off], end_off-beg_off); + memcpy(buf, &self->data_start[beg_off], end_off-beg_off); return (uint32_t)(end_off-beg_off); } struct lib9p_srv_file_vtable static_file_vtable = { - .clone = static_metadata_clone, - .free = static_metadata_free, + .clone = static_common_clone, + .free = static_common_free, - .io = static_metadata_io, + .io = static_common_io, .stat = static_file_stat, - .wstat = static_metadata_wstat, - .remove = static_metadata_remove, + .wstat = static_common_wstat, + .remove = static_common_remove, .pread = static_file_pread, .pwrite = NULL, diff --git a/cmd/srv9p/static9p.h b/cmd/srv9p/static9p.h index 92f5909..98b85a2 100644 --- a/cmd/srv9p/static9p.h +++ b/cmd/srv9p/static9p.h @@ -9,8 +9,8 @@ #include <lib9p/srv.h> -struct static_metadata { - struct lib9p_srv_file header; +typedef struct { + implements_lib9p_srv_file; char *u_name; uint32_t u_num; @@ -23,10 +23,10 @@ struct static_metadata { char *name; lib9p_dm_t perm; uint32_t atime, mtime; -}; +} _static_common; struct static_dir { - struct static_metadata metadata; + _static_common; /* NULL-terminated */ struct lib9p_srv_file *members[]; @@ -34,7 +34,7 @@ struct static_dir { struct static_file { - struct static_metadata metadata; + _static_common; char *data_start; /* must not be NULL */ char *data_end; /* may be NULL, in which case data_size is used */ |