From 404d3a3a620f2ea8554d3be0cb7d315a6702de55 Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Sat, 22 Dec 2007 14:24:00 -0600 Subject: [PATCH 1/7] Fix includes. --- src/gnutls.c | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/src/gnutls.c b/src/gnutls.c index d2118a06..c8e4bde7 100644 --- a/src/gnutls.c +++ b/src/gnutls.c @@ -23,15 +23,9 @@ #ifdef GNUTLS -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include "stdinc.h" +#include "config.h" + #include #include /* for gcry_control */ From 868590746df471f97bfb59a4d26b6fd1078ee22d Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Sat, 22 Dec 2007 16:05:51 -0600 Subject: [PATCH 2/7] - add IOReadFunc and IOWriteFunc types. - add fde::read_impl, fde::write_impl. (defaults to read(2) and write(2) with raw FDs at the moment; this will be revised to act on the fde later.) --- libcharybdis/commio.c | 6 +++++- libcharybdis/commio.h | 12 ++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/libcharybdis/commio.c b/libcharybdis/commio.c index 14c9efaf..ac09126c 100644 --- a/libcharybdis/commio.c +++ b/libcharybdis/commio.c @@ -85,8 +85,12 @@ comm_add_fd(int fd) if (F != NULL) return F; - F = calloc(sizeof(fde_t), 1); + F = MyMalloc(sizeof(fde_t)); F->fd = fd; + + F->read_impl = read; + F->write_impl = write; + list = &fd_table[fd % FD_HASH_SIZE]; dlinkAdd(F, &F->node, list); diff --git a/libcharybdis/commio.h b/libcharybdis/commio.h index f7240609..3100e6f8 100644 --- a/libcharybdis/commio.h +++ b/libcharybdis/commio.h @@ -35,6 +35,10 @@ /* Callback for completed IO events */ typedef void PF(int fd, void *); +/* virtual function types for I/O --nenolod */ +typedef void IOFuncRead(int fd, void *buf, size_t count); +typedef void IOFuncWrite(int fd, const void *buf, size_t count); + /* Callback for completed connections */ /* int fd, int status, void * */ typedef void CNCB(int fd, int, void *); @@ -96,16 +100,24 @@ struct _fde fdlist_t list; /* Which list this FD should sit on */ int comm_index; /* where in the poll list we live */ char desc[FD_DESC_SZ]; + PF *read_handler; void *read_data; + PF *write_handler; void *write_data; + PF *timeout_handler; void *timeout_data; time_t timeout; + PF *flush_handler; void *flush_data; time_t flush_timeout; + + IOReadFunc *read_impl; + IOWriteFunc *write_impl; + struct DNSQuery *dns_query; struct { From 5893220fdcaec39b9574fb08c22b5a15661b77b1 Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Sat, 22 Dec 2007 16:08:34 -0600 Subject: [PATCH 3/7] wtf mercurial, tbh. --- src/gnutls.c | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/src/gnutls.c b/src/gnutls.c index d2118a06..470e0d15 100644 --- a/src/gnutls.c +++ b/src/gnutls.c @@ -23,15 +23,8 @@ #ifdef GNUTLS -#include -#include -#include -#include -#include -#include -#include -#include -#include +#include "stdinc.h" +#include "config.h" #include #include /* for gcry_control */ From d877759f5be1707344e8d00b814f0182f6d26b58 Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Sat, 22 Dec 2007 16:15:09 -0600 Subject: [PATCH 4/7] Make fde raw I/O functions act on the FDE object, not the FD directly. --- libcharybdis/commio.c | 24 ++++++++++++++++++++++-- libcharybdis/commio.h | 12 ++++++------ 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/libcharybdis/commio.c b/libcharybdis/commio.c index ac09126c..c7dfe1a4 100644 --- a/libcharybdis/commio.c +++ b/libcharybdis/commio.c @@ -58,6 +58,26 @@ static void comm_connect_dns_callback(void *vptr, struct DNSReply *reply); static PF comm_connect_tryconnect; static int comm_max_connections = 0; +static int +comm_read_raw(fde_t *F, void *buf, size_t count) +{ + s_assert(F != NULL); + s_assert(buf != NULL); + s_assert(count > 0); + + return read(F->fd, buf, count); +} + +static int +comm_write_raw(fde_t *F, const void *buf, size_t count) +{ + s_assert(F != NULL); + s_assert(buf != NULL); + s_assert(count > 0); + + return write(F->fd, buf, count); +} + inline fde_t * comm_locate_fd(int fd) { @@ -88,8 +108,8 @@ comm_add_fd(int fd) F = MyMalloc(sizeof(fde_t)); F->fd = fd; - F->read_impl = read; - F->write_impl = write; + F->read_impl = comm_read_raw; + F->write_impl = comm_write_raw; list = &fd_table[fd % FD_HASH_SIZE]; dlinkAdd(F, &F->node, list); diff --git a/libcharybdis/commio.h b/libcharybdis/commio.h index 3100e6f8..a22db1ad 100644 --- a/libcharybdis/commio.h +++ b/libcharybdis/commio.h @@ -32,12 +32,14 @@ #include "ircd_defs.h" #include "tools.h" +typedef struct _fde fde_t; + /* Callback for completed IO events */ typedef void PF(int fd, void *); /* virtual function types for I/O --nenolod */ -typedef void IOFuncRead(int fd, void *buf, size_t count); -typedef void IOFuncWrite(int fd, const void *buf, size_t count); +typedef int IOFuncRead(fde_t *, void *buf, size_t count); +typedef int IOFuncWrite(fde_t *, const void *buf, size_t count); /* Callback for completed connections */ /* int fd, int status, void * */ @@ -80,8 +82,6 @@ typedef enum fdlist_t } fdlist_t; -typedef struct _fde fde_t; - extern int highest_fd; extern int number_fd; @@ -115,8 +115,8 @@ struct _fde void *flush_data; time_t flush_timeout; - IOReadFunc *read_impl; - IOWriteFunc *write_impl; + IOFuncRead *read_impl; + IOFuncWrite *write_impl; struct DNSQuery *dns_query; struct From 5cd74a3b6c8ac2e62dcf1cd2f56e30955e0102fc Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Sat, 22 Dec 2007 16:28:05 -0600 Subject: [PATCH 5/7] linebuf_flush(): use fde_t's. --- libcharybdis/linebuf.c | 4 ++-- libcharybdis/linebuf.h | 3 ++- src/send.c | 6 +++++- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/libcharybdis/linebuf.c b/libcharybdis/linebuf.c index 8ac6a31f..a532395f 100644 --- a/libcharybdis/linebuf.c +++ b/libcharybdis/linebuf.c @@ -623,7 +623,7 @@ linebuf_putmsg(buf_head_t * bufhead, const char *format, va_list * va_args, */ int -linebuf_flush(int fd, buf_head_t * bufhead) +linebuf_flush(fde_t *fd, buf_head_t * bufhead) { buf_line_t *bufline; int retval; @@ -652,7 +652,7 @@ linebuf_flush(int fd, buf_head_t * bufhead) } /* Now, try writing data */ - retval = send(fd, bufline->buf + bufhead->writeofs, bufline->len - bufhead->writeofs, 0); + retval = fd->write_impl(fd, bufline->buf + bufhead->writeofs, bufline->len - bufhead->writeofs); if(retval <= 0) return retval; diff --git a/libcharybdis/linebuf.h b/libcharybdis/linebuf.h index 5444fe37..df84dbc4 100644 --- a/libcharybdis/linebuf.h +++ b/libcharybdis/linebuf.h @@ -28,6 +28,7 @@ #define __LINEBUF_H__ #include "tools.h" +#include "commio.h" /* How big we want a buffer - 510 data bytes, plus space for a '\0' */ #define BUF_DATA_SIZE 511 @@ -80,7 +81,7 @@ extern void linebuf_donebuf(buf_head_t *); extern int linebuf_parse(buf_head_t *, char *, int, int); extern int linebuf_get(buf_head_t *, char *, int, int, int); extern void linebuf_putmsg(buf_head_t *, const char *, va_list *, const char *, ...); -extern int linebuf_flush(int, buf_head_t *); +extern int linebuf_flush(fde_t *, buf_head_t *); extern void linebuf_attach(buf_head_t *, buf_head_t *); extern void count_linebuf_memory(size_t *, size_t *); #endif diff --git a/src/send.c b/src/send.c index bd4adea8..7c122977 100644 --- a/src/send.c +++ b/src/send.c @@ -168,6 +168,10 @@ send_queued_write(int fd, void *data) #ifdef USE_IODEBUG_HOOKS hook_data_int hd; #endif + fde_t *F = comm_lookup_fd(to->localClient->fd); + if (!F) + return; + /* cant write anything to a dead socket. */ if(IsIOError(to)) return; @@ -182,7 +186,7 @@ send_queued_write(int fd, void *data) if(linebuf_len(&to->localClient->buf_sendq)) { while ((retlen = - linebuf_flush(to->localClient->fd, &to->localClient->buf_sendq)) > 0) + linebuf_flush(F, &to->localClient->buf_sendq)) > 0) { /* We have some data written .. update counters */ #ifdef USE_IODEBUG_HOOKS From c1e5bc411703cd2ce88d537610f1cb41939a3619 Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Sat, 22 Dec 2007 16:30:06 -0600 Subject: [PATCH 6/7] comm_locate_fd(), not comm_lookup_fd(). --- src/send.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/send.c b/src/send.c index 7c122977..732efb05 100644 --- a/src/send.c +++ b/src/send.c @@ -168,7 +168,7 @@ send_queued_write(int fd, void *data) #ifdef USE_IODEBUG_HOOKS hook_data_int hd; #endif - fde_t *F = comm_lookup_fd(to->localClient->fd); + fde_t *F = comm_locate_fd(to->localClient->fd); if (!F) return; From de845813f9ad23f628502020a1bbf00b79ff399d Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Sat, 22 Dec 2007 16:38:09 -0600 Subject: [PATCH 7/7] update IDEAS for suggestions on how you can help with the I/O refactoring. --- IDEAS | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/IDEAS b/IDEAS index 9c23012c..22e7d70d 100644 --- a/IDEAS +++ b/IDEAS @@ -1,3 +1,10 @@ +Important stuff: +- our I/O operates on raw fds. we should use fde_t's everywhere so that + lookups are less necessary and so that I/O is done using the virtual functors, + IOFuncRead and IOFuncWrite. +- implement comm_get_io_direction(fde_t *) and use it to determine how to reschedule + I/O direction when needed. + Some of this may not be possible to do in 2.3... - go TS6 only? [partially done; TS6 is always enabled now]