diff options
-rw-r--r-- | cmd/sbc_harness/hw/rp2040_hwspi.c | 2 | ||||
-rw-r--r-- | cmd/sbc_harness/hw/rp2040_hwspi.h | 2 | ||||
-rw-r--r-- | cmd/sbc_harness/hw/spi.h | 12 | ||||
-rw-r--r-- | cmd/sbc_harness/hw/w5500.c | 2 | ||||
-rw-r--r-- | cmd/sbc_harness/hw/w5500.h | 4 | ||||
-rw-r--r-- | cmd/srv9p/gnet.c | 12 | ||||
-rw-r--r-- | cmd/srv9p/static9p.h | 2 | ||||
-rw-r--r-- | lib9p/include/lib9p/srv.h | 81 | ||||
-rw-r--r-- | lib9p/srv.c | 24 | ||||
-rw-r--r-- | libmisc/include/libmisc/net.h | 49 |
10 files changed, 98 insertions, 92 deletions
diff --git a/cmd/sbc_harness/hw/rp2040_hwspi.c b/cmd/sbc_harness/hw/rp2040_hwspi.c index dd9a577..6742c60 100644 --- a/cmd/sbc_harness/hw/rp2040_hwspi.c +++ b/cmd/sbc_harness/hw/rp2040_hwspi.c @@ -1,4 +1,4 @@ -/* hw/rp2040_hwspi.c - `struct spi` implementation for the RP2040's +/* hw/rp2040_hwspi.c - `implements_spi` implementation for the RP2040's * ARM Primecell SSP (PL022) (implementation file) * * Copyright (C) 2024 Luke T. Shumaker <lukeshu@lukeshu.com> diff --git a/cmd/sbc_harness/hw/rp2040_hwspi.h b/cmd/sbc_harness/hw/rp2040_hwspi.h index 8bf783f..8cf8aff 100644 --- a/cmd/sbc_harness/hw/rp2040_hwspi.h +++ b/cmd/sbc_harness/hw/rp2040_hwspi.h @@ -1,4 +1,4 @@ -/* hw/rp2040_hwspi.h - `struct spi` implementation for the RP2040's +/* hw/rp2040_hwspi.h - `implements_spi` implementation for the RP2040's * ARM Primecell SSP (PL022) (header file) * * Copyright (C) 2024 Luke T. Shumaker <lukeshu@lukeshu.com> diff --git a/cmd/sbc_harness/hw/spi.h b/cmd/sbc_harness/hw/spi.h index 653e80f..da588de 100644 --- a/cmd/sbc_harness/hw/spi.h +++ b/cmd/sbc_harness/hw/spi.h @@ -22,7 +22,11 @@ struct bidi_iovec { size_t iov_len; }; -struct spi; +struct spi_vtable; + +typedef struct { + struct spi_vtable *vtable; +} implements_spi; /* This API assumes that an SPI frame is a multiple of 8-bits. * @@ -37,11 +41,7 @@ struct spi; * non-multiple-of-8 number of bits. */ struct spi_vtable { - void (*readwritev)(struct spi *, const struct bidi_iovec *iov, int iovcnt); + void (*readwritev)(implements_spi *, const struct bidi_iovec *iov, int iovcnt); }; -typedef struct spi { - struct spi_vtable *vtable; -} implements_spi; - #endif /* _HW_SPI_H_ */ diff --git a/cmd/sbc_harness/hw/w5500.c b/cmd/sbc_harness/hw/w5500.c index a6cdc8b..5e36bcf 100644 --- a/cmd/sbc_harness/hw/w5500.c +++ b/cmd/sbc_harness/hw/w5500.c @@ -170,7 +170,7 @@ static void w5500_intrhandler(uint gpio, uint32_t UNUSED(event_mask)) { } void _w5500_init(struct w5500 *chip, - struct spi* spi, uint pin_intr, uint pin_reset, + implements_spi *spi, uint pin_intr, uint pin_reset, struct net_eth_addr addr) { assert(chip); assert(spi); diff --git a/cmd/sbc_harness/hw/w5500.h b/cmd/sbc_harness/hw/w5500.h index 1b485dd..2ff04df 100644 --- a/cmd/sbc_harness/hw/w5500.h +++ b/cmd/sbc_harness/hw/w5500.h @@ -35,7 +35,7 @@ struct _w5500_tcp_listener { struct w5500 { /* const-after-init */ - struct spi *spidev; + implements_spi *spidev; uint pin_intr; uint pin_reset; struct net_eth_addr hwaddr; @@ -66,7 +66,7 @@ struct w5500 { _w5500_init(self, spi, pin_intr, pin_reset, eth_addr); \ } while (0) void _w5500_init(struct w5500 *self, - struct spi* spi, uint pin_intr, uint pin_reset, + implements_spi *spi, uint pin_intr, uint pin_reset, struct net_eth_addr addr); /** diff --git a/cmd/srv9p/gnet.c b/cmd/srv9p/gnet.c index 699deaf..f1ea709 100644 --- a/cmd/srv9p/gnet.c +++ b/cmd/srv9p/gnet.c @@ -280,11 +280,11 @@ static int gnet_tcp_close(implements_net_stream_conn *_conn, bool rd, bool wr) { /* UDP init() *****************************************************************/ -static ssize_t gnet_udp_sendto(struct net_packet_conn *self, void *buf, size_t len, +static ssize_t gnet_udp_sendto(implements_net_packet_conn *self, void *buf, size_t len, struct net_ip4_addr addr, uint16_t port); -static ssize_t gnet_udp_recvfrom(struct net_packet_conn *self, void *buf, size_t len, +static ssize_t gnet_udp_recvfrom(implements_net_packet_conn *self, void *buf, size_t len, struct net_ip4_addr *ret_addr, uint16_t *ret_port); -static int gnet_udp_close(struct net_packet_conn *self); +static int gnet_udp_close(implements_net_packet_conn *self); static struct net_packet_conn_vtable gnet_udp_conn_vtable = { .sendto = gnet_udp_sendto, @@ -351,7 +351,7 @@ static void *gnet_pthread_sendto(void *_args) { return NULL; } -static ssize_t gnet_udp_sendto(struct net_packet_conn *_conn, void *buf, size_t count, +static ssize_t gnet_udp_sendto(implements_net_packet_conn *_conn, void *buf, size_t count, struct net_ip4_addr node, uint16_t port) { struct gnet_udp_conn *conn = VCALL_SELF(struct gnet_udp_conn, implements_net_packet_conn, _conn); @@ -418,7 +418,7 @@ static void *gnet_pthread_recvfrom(void *_args) { return NULL; } -static ssize_t gnet_udp_recvfrom(struct net_packet_conn *_conn, void *buf, size_t count, +static ssize_t gnet_udp_recvfrom(implements_net_packet_conn *_conn, void *buf, size_t count, struct net_ip4_addr *ret_node, uint16_t *ret_port) { struct gnet_udp_conn *conn = VCALL_SELF(struct gnet_udp_conn, implements_net_packet_conn, _conn); @@ -444,7 +444,7 @@ static ssize_t gnet_udp_recvfrom(struct net_packet_conn *_conn, void *buf, size_ /* UDP close() ****************************************************************/ -static int gnet_udp_close(struct net_packet_conn *_conn) { +static int gnet_udp_close(implements_net_packet_conn *_conn) { struct gnet_udp_conn *conn = VCALL_SELF(struct gnet_udp_conn, implements_net_packet_conn, _conn); assert(conn); diff --git a/cmd/srv9p/static9p.h b/cmd/srv9p/static9p.h index 98b85a2..e03a480 100644 --- a/cmd/srv9p/static9p.h +++ b/cmd/srv9p/static9p.h @@ -29,7 +29,7 @@ struct static_dir { _static_common; /* NULL-terminated */ - struct lib9p_srv_file *members[]; + implements_lib9p_srv_file *members[]; }; diff --git a/lib9p/include/lib9p/srv.h b/lib9p/include/lib9p/srv.h index b9669d5..072925f 100644 --- a/lib9p/include/lib9p/srv.h +++ b/lib9p/include/lib9p/srv.h @@ -41,65 +41,66 @@ static inline int lib9p_srv_acknowledge_flush(struct lib9p_srv_ctx *ctx) { /* interface definitions ******************************************************/ -struct lib9p_srv_file; +struct lib9p_srv_file_vtable; + +struct __lib9p_srv_file; +typedef struct __lib9p_srv_file { + struct lib9p_srv_file_vtable *vtable; + + /* Managed by srv.c, but should be cloned by ->vtable->clone(). */ + struct __lib9p_srv_file *_parent_dir; /* clone this + + /* Managed by srv.c, but should be initialized to 0 by ->vtable->clone(). */ + /* ref type 1: an entry in fidmap + * ref type 2: ->_parent_dir of another file */ + unsigned int _refcount; +} implements_lib9p_srv_file; struct lib9p_srv_file_vtable { /* all - resource management */ - struct lib9p_srv_file *(*clone )(struct lib9p_srv_file *, struct lib9p_srv_ctx *); - void (*free )(struct lib9p_srv_file *, struct lib9p_srv_ctx *); + implements_lib9p_srv_file *(*clone )(implements_lib9p_srv_file *, struct lib9p_srv_ctx *); + void (*free )(implements_lib9p_srv_file *, struct lib9p_srv_ctx *); /* all - syscalls */ - uint32_t (*io )(struct lib9p_srv_file *, struct lib9p_srv_ctx *, - lib9p_o_t flags); - struct lib9p_stat (*stat )(struct lib9p_srv_file *, struct lib9p_srv_ctx *); - void (*wstat )(struct lib9p_srv_file *, struct lib9p_srv_ctx *, - struct lib9p_stat new); - void (*remove )(struct lib9p_srv_file *, struct lib9p_srv_ctx *); + uint32_t (*io )(implements_lib9p_srv_file *, struct lib9p_srv_ctx *, + lib9p_o_t flags); + struct lib9p_stat (*stat )(implements_lib9p_srv_file *, struct lib9p_srv_ctx *); + void (*wstat )(implements_lib9p_srv_file *, struct lib9p_srv_ctx *, + struct lib9p_stat new); + void (*remove )(implements_lib9p_srv_file *, struct lib9p_srv_ctx *); /* directories - base */ - struct lib9p_srv_file *(*dopen )(struct lib9p_srv_file *, struct lib9p_srv_ctx *, - char *childname); - struct lib9p_srv_file *(*dcreate)(struct lib9p_srv_file *, struct lib9p_srv_ctx *, - char *childname, - lib9p_dm_t perm, lib9p_o_t flags); + implements_lib9p_srv_file *(*dopen )(implements_lib9p_srv_file *, struct lib9p_srv_ctx *, + char *childname); + implements_lib9p_srv_file *(*dcreate)(implements_lib9p_srv_file *, struct lib9p_srv_ctx *, + char *childname, + lib9p_dm_t perm, lib9p_o_t flags); /* directories - once opened */ - size_t /* <- obj cnt */(*dread )(struct lib9p_srv_file *, struct lib9p_srv_ctx *, - uint8_t *buf, - uint32_t byte_count, /* <- num bytes */ - size_t obj_offset); /* <- starting at this object */ + size_t /* <- obj cnt */ (*dread )(implements_lib9p_srv_file *, struct lib9p_srv_ctx *, + uint8_t *buf, + uint32_t byte_count, /* <- num bytes */ + size_t obj_offset); /* <- starting at this object */ /* non-directories - once opened */ - uint32_t (*pread )(struct lib9p_srv_file *, struct lib9p_srv_ctx *, - void *buf, - uint32_t byte_count, - uint64_t byte_offset); - uint32_t (*pwrite )(struct lib9p_srv_file *, struct lib9p_srv_ctx *, - void *buf, - uint32_t byte_count, - uint64_t byte_offset); + uint32_t (*pread )(implements_lib9p_srv_file *, struct lib9p_srv_ctx *, + void *buf, + uint32_t byte_count, + uint64_t byte_offset); + uint32_t (*pwrite )(implements_lib9p_srv_file *, struct lib9p_srv_ctx *, + void *buf, + uint32_t byte_count, + uint64_t byte_offset); }; -typedef struct lib9p_srv_file { - struct lib9p_srv_file_vtable *vtable; - - /* Managed by srv.c, but should be cloned by ->vtable->clone(). */ - struct lib9p_srv_file *_parent_dir; /* clone this - - /* Managed by srv.c, but should be initialized to 0 by ->vtable->clone(). */ - /* ref type 1: an entry in fidmap - * ref type 2: ->_parent_dir of another file */ - unsigned int _refcount; -} implements_lib9p_srv_file; - /* main server entrypoints ****************************************************/ CR_RPC_DECLARE(_lib9p_srv_reqch, struct _lib9p_srv_req *, bool) struct lib9p_srv { /* Things you provide */ - void /*TODO*/ (*auth )(struct lib9p_srv_ctx *, char *treename); /* optional */ - struct lib9p_srv_file *(*rootdir)(struct lib9p_srv_ctx *, char *treename); + void /*TODO*/ (*auth )(struct lib9p_srv_ctx *, char *treename); /* optional */ + implements_lib9p_srv_file *(*rootdir)(struct lib9p_srv_ctx *, char *treename); /* For internal use */ _lib9p_srv_reqch_t _reqch; diff --git a/lib9p/srv.c b/lib9p/srv.c index 83a6555..43676a8 100644 --- a/lib9p/srv.c +++ b/lib9p/srv.c @@ -28,10 +28,10 @@ #define FIDFLAG_OPEN (FIDFLAG_OPEN_R|FIDFLAG_OPEN_W) struct _srv_fidinfo { - struct lib9p_srv_file *file; - uint8_t flags; - size_t dir_idx; - uint32_t dir_off; + implements_lib9p_srv_file *file; + uint8_t flags; + size_t dir_idx; + uint32_t dir_off; }; #define NAME fidmap @@ -65,7 +65,7 @@ struct _srv_conn { struct _srv_sess { /* immutable */ - struct _srv_conn *parent_conn; + struct _srv_conn *parent_conn; enum lib9p_version version; uint32_t max_msg_size; uint32_t rerror_overhead; @@ -407,7 +407,7 @@ static inline bool util_check_perm(struct lib9p_srv_ctx *ctx, struct lib9p_stat return mode & action; } -static inline bool util_release(struct lib9p_srv_ctx *ctx, struct lib9p_srv_file *file) { +static inline bool util_release(struct lib9p_srv_ctx *ctx, implements_lib9p_srv_file *file) { assert(file); file->_refcount--; if (file->_refcount == 0) { @@ -564,7 +564,7 @@ static void handle_Tattach(struct _lib9p_srv_req *ctx, return; } - struct lib9p_srv_file *rootdir = srv->rootdir(&ctx->ctx, req->aname.utf8); + implements_lib9p_srv_file *rootdir = srv->rootdir(&ctx->ctx, req->aname.utf8); assert((rootdir == NULL) == lib9p_ctx_has_error(&ctx->ctx.basectx)); if (lib9p_ctx_has_error(&ctx->ctx.basectx)) return; @@ -623,7 +623,7 @@ static void handle_Twalk(struct _lib9p_srv_req *ctx, return; } - struct lib9p_srv_file *dir = fidinfo->file; + implements_lib9p_srv_file *dir = fidinfo->file; if (req->newfid != req->fid) { dir = VCALL(dir, clone, &ctx->ctx); assert((dir == NULL) == lib9p_ctx_has_error(&ctx->ctx.basectx)); @@ -635,7 +635,7 @@ static void handle_Twalk(struct _lib9p_srv_req *ctx, resp->wqid = (struct lib9p_qid *)(&resp[1]); for (resp->nwqid = 0; resp->nwqid < req->nwname; resp->nwqid++) { - struct lib9p_srv_file *member; + implements_lib9p_srv_file *member; if (strcmp(req->wname[resp->nwqid].utf8, "..") == 0) { member = dir->_parent_dir; } else { @@ -727,7 +727,7 @@ static void handle_Topen(struct _lib9p_srv_req *ctx, /* Variables. */ lib9p_o_t reqmode = req->mode; uint8_t fidflags = fidinfo->flags; - struct lib9p_srv_file *file = fidinfo->file; + implements_lib9p_srv_file *file = fidinfo->file; /* Check permissions. */ if (reqmode & LIB9P_O_RCLOSE) { @@ -811,7 +811,7 @@ static void handle_Tread(struct _lib9p_srv_req *ctx, } /* Variables. */ - struct lib9p_srv_file *file = fidinfo->file; + implements_lib9p_srv_file *file = fidinfo->file; resp->data.dat = (char *)(&resp[1]); /* Do it. */ @@ -864,7 +864,7 @@ static void handle_Twrite(struct _lib9p_srv_req *ctx, } /* Variables. */ - struct lib9p_srv_file *file = fidinfo->file; + implements_lib9p_srv_file *file = fidinfo->file; /* Do it. */ resp->count = VCALL(file, pwrite, &ctx->ctx, req->data.dat, req->data.len, req->offset); diff --git a/libmisc/include/libmisc/net.h b/libmisc/include/libmisc/net.h index 43c4a48..057e875 100644 --- a/libmisc/include/libmisc/net.h +++ b/libmisc/include/libmisc/net.h @@ -33,15 +33,23 @@ struct net_eth_addr { /* Streams (e.g. TCP) *********************************************************/ -struct net_stream_listener; -struct net_stream_conn; +struct net_stream_listener_vtable; +struct net_stream_conn_btable; + +typedef struct { + struct net_stream_listener_vtable *vtable; +} implements_net_stream_listener; + +typedef struct { + struct net_stream_conn_vtable *vtable; +} implements_net_stream_conn; struct net_stream_listener_vtable { /** * It is invalid to accept() a new connection if an existing * connection is still open. */ - struct net_stream_conn *(*accept)(struct net_stream_listener *self); + implements_net_stream_conn *(*accept)(implements_net_stream_listener *self); }; struct net_stream_conn_vtable { @@ -49,7 +57,8 @@ struct net_stream_conn_vtable { * Return bytes-read on success, 0 on EOF, -errno on error; a * short read is *not* an error. */ - ssize_t (*read)(struct net_stream_conn *self, void *buf, size_t count); + ssize_t (*read)(implements_net_stream_conn *self, + void *buf, size_t count); /** * Return `count` on success, -errno on error; a short write *is* an @@ -59,36 +68,32 @@ struct net_stream_conn_vtable { * expensive to implement), so if you have concurrent writers then you * should arrange for a mutex to protect the connection. */ - ssize_t (*write)(struct net_stream_conn *self, void *buf, size_t count); + ssize_t (*write)(implements_net_stream_conn *self, + void *buf, size_t count); /** * Return 0 on success, -errno on error. */ - int (*close)(struct net_stream_conn *self, bool rd, bool wr); + int (*close)(implements_net_stream_conn *self, + bool rd, bool wr); }; -typedef struct net_stream_listener { - struct net_stream_listener_vtable *vtable; -} implements_net_stream_listener; - -typedef struct net_stream_conn { - struct net_stream_conn_vtable *vtable; -} implements_net_stream_conn; - /* Packets (e.g. UDP) *********************************************************/ -struct net_packet_conn; +struct net_packet_conn_vtable; + +typedef struct { + struct net_packet_conn_vtable *vtable; +} implements_net_packet_conn; struct net_packet_conn_vtable { - ssize_t (*sendto )(struct net_packet_conn *self, void *buf, size_t len, + ssize_t (*sendto )(implements_net_packet_conn *self, + void *buf, size_t len, struct net_ip4_addr node, uint16_t port); - ssize_t (*recvfrom)(struct net_packet_conn *self, void *buf, size_t len, + ssize_t (*recvfrom)(implements_net_packet_conn *self, + void *buf, size_t len, struct net_ip4_addr *ret_node, uint16_t *ret_port); - int (*close )(struct net_packet_conn *self); + int (*close )(implements_net_packet_conn *self); }; -typedef struct net_packet_conn { - struct net_packet_conn_vtable *vtable; -} implements_net_packet_conn; - #endif /* _LIBMISC_NET_H_ */ |