diff options
author | Henning Brauer <henning@cvs.openbsd.org> | 2003-12-28 14:34:31 +0000 |
---|---|---|
committer | Henning Brauer <henning@cvs.openbsd.org> | 2003-12-28 14:34:31 +0000 |
commit | 61f8bc1c82d9aab917529611effaad633782e457 (patch) | |
tree | e5468112fc21c6cfed03619bd0f8cb8caea1b3cd | |
parent | a7bfdd02fe4106044791878771234737d19b306f (diff) |
redo the imsg readers to use bigger buffers and less read(2)s. should increase
performance even further.
gets rif od struct imsg_readbuf; rename peer_read_buf to read_buf as that
is more appropriate now.
-rw-r--r-- | usr.sbin/bgpd/bgpd.h | 18 | ||||
-rw-r--r-- | usr.sbin/bgpd/imsg.c | 97 | ||||
-rw-r--r-- | usr.sbin/bgpd/session.c | 4 |
3 files changed, 47 insertions, 72 deletions
diff --git a/usr.sbin/bgpd/bgpd.h b/usr.sbin/bgpd/bgpd.h index 6cc04e25505..0c3a48dada9 100644 --- a/usr.sbin/bgpd/bgpd.h +++ b/usr.sbin/bgpd/bgpd.h @@ -1,4 +1,4 @@ -/* $OpenBSD: bgpd.h,v 1.43 2003/12/27 14:24:42 henning Exp $ */ +/* $OpenBSD: bgpd.h,v 1.44 2003/12/28 14:34:30 henning Exp $ */ /* * Copyright (c) 2003 Henning Brauer <henning@openbsd.org> @@ -111,7 +111,7 @@ struct bgpd_config { struct peer *peers; }; -struct peer_buf_read { +struct buf_read { u_char buf[READ_BUF_SIZE]; u_char *rptr; ssize_t wpos; @@ -143,7 +143,7 @@ struct peer { int sock; int events; struct msgbuf wbuf; - struct peer_buf_read *rbuf; + struct buf_read *rbuf; struct peer *next; }; @@ -187,19 +187,9 @@ struct mrtdump_config { #define IMSG_HEADER_SIZE sizeof(struct imsg_hdr) #define MAX_IMSGSIZE 8192 -struct imsg_readbuf { - u_char buf[MAX_IMSGSIZE]; - ssize_t read_len; - u_int32_t peerid; - u_int16_t pkt_len; - u_int8_t type; - u_char *wptr; - u_int8_t seen_hdr; -}; - struct imsgbuf { int sock; - struct imsg_readbuf r; + struct buf_read r; struct msgbuf w; }; diff --git a/usr.sbin/bgpd/imsg.c b/usr.sbin/bgpd/imsg.c index 7c740bc0a83..fb92c6d6003 100644 --- a/usr.sbin/bgpd/imsg.c +++ b/usr.sbin/bgpd/imsg.c @@ -1,4 +1,4 @@ -/* $OpenBSD: imsg.c,v 1.11 2003/12/26 18:33:11 henning Exp $ */ +/* $OpenBSD: imsg.c,v 1.12 2003/12/28 14:34:30 henning Exp $ */ /* * Copyright (c) 2003 Henning Brauer <henning@openbsd.org> @@ -25,21 +25,11 @@ #include "bgpd.h" -void imsg_init_readbuf(struct imsgbuf *); - -void -imsg_init_readbuf(struct imsgbuf *ibuf) -{ - bzero(&ibuf->r, sizeof(ibuf->r)); - ibuf->r.wptr = ibuf->r.buf; - ibuf->r.pkt_len = IMSG_HEADER_SIZE; -} - void imsg_init(struct imsgbuf *ibuf, int sock) { - imsg_init_readbuf(ibuf); msgbuf_init(&ibuf->w); + bzero(&ibuf->r, sizeof(ibuf->r)); ibuf->sock = sock; ibuf->w.sock = sock; } @@ -47,53 +37,48 @@ imsg_init(struct imsgbuf *ibuf, int sock) int imsg_get(struct imsgbuf *ibuf, struct imsg *imsg) { - struct imsg_hdr *hdr; - ssize_t n, read_total = 0, datalen = 0; - u_char *rptr; + ssize_t n, datalen = 0; + size_t av, left; - do { - if ((n = read(ibuf->sock, ibuf->r.wptr, - ibuf->r.pkt_len - ibuf->r.read_len)) == -1) { - if (errno != EAGAIN && errno != EINTR) { - log_err("imsg_get pipe read error"); - return (-1); - } - return (0); - } - read_total += n; - ibuf->r.wptr += n; - ibuf->r.read_len += n; - if (ibuf->r.read_len == ibuf->r.pkt_len) { - if (!ibuf->r.seen_hdr) { /* got header */ - hdr = (struct imsg_hdr *)&ibuf->r.buf; - ibuf->r.type = hdr->type; - ibuf->r.pkt_len = hdr->len; - ibuf->r.peerid = hdr->peerid; - if (hdr->len < IMSG_HEADER_SIZE || - hdr->len > MAX_IMSGSIZE) { - logit(LOG_CRIT, "wrong imsg hdr len"); - return (-1); - } - ibuf->r.seen_hdr = 1; - } else { /* we got the full packet */ - imsg->hdr.type = ibuf->r.type; - imsg->hdr.len = ibuf->r.pkt_len; - imsg->hdr.peerid = ibuf->r.peerid; - datalen = ibuf->r.pkt_len - IMSG_HEADER_SIZE; - rptr = ibuf->r.buf + IMSG_HEADER_SIZE; - if ((imsg->data = malloc(datalen)) == NULL) { - log_err("imsg_get"); - return (-1); - } - memcpy(imsg->data, rptr, datalen); - n = 0; /* give others a chance */ - imsg_init_readbuf(ibuf); - } + if ((n = read(ibuf->sock, ibuf->r.buf + ibuf->r.wpos, + sizeof(ibuf->r.buf) - ibuf->r.wpos)) == -1) { + if (errno != EINTR && errno != EAGAIN) { + log_err("imsg_get: pipe read error"); + return (-1); } - } while (n > 0); - - if (read_total == 0) /* connection closed */ return (0); + } + if (n == 0) { /* connection closed */ + logit(LOG_CRIT, "imsg_get: pipe close"); + return (-1); + } + av = ibuf->r.wpos + n; + + if (IMSG_HEADER_SIZE > av) + return (-1); + + memcpy(&imsg->hdr, ibuf->r.buf, sizeof(imsg->hdr)); + if (imsg->hdr.len < IMSG_HEADER_SIZE || + imsg->hdr.len > MAX_IMSGSIZE) { + logit(LOG_CRIT, "wrong imsg hdr len"); + return (-1); + } + if (imsg->hdr.len > av) + return (-1); + datalen = imsg->hdr.len - IMSG_HEADER_SIZE; + ibuf->r.rptr = ibuf->r.buf + IMSG_HEADER_SIZE; + if ((imsg->data = malloc(datalen)) == NULL) { + log_err("imsg_get"); + return (-1); + } + memcpy(imsg->data, ibuf->r.rptr, datalen); + + if (imsg->hdr.len < av) { + left = av - imsg->hdr.len; + memcpy(&ibuf->r.buf, ibuf->r.buf + imsg->hdr.len, left); + ibuf->r.wpos = left; + } else + ibuf->r.wpos = 0; return (datalen + IMSG_HEADER_SIZE); } diff --git a/usr.sbin/bgpd/session.c b/usr.sbin/bgpd/session.c index 8ae763b5b6b..b98e6015a0d 100644 --- a/usr.sbin/bgpd/session.c +++ b/usr.sbin/bgpd/session.c @@ -1,4 +1,4 @@ -/* $OpenBSD: session.c,v 1.47 2003/12/27 14:28:41 henning Exp $ */ +/* $OpenBSD: session.c,v 1.48 2003/12/28 14:34:30 henning Exp $ */ /* * Copyright (c) 2003 Henning Brauer <henning@openbsd.org> @@ -327,7 +327,7 @@ bgp_fsm(struct peer *peer, enum session_events event) peer->StartTimer = 0; /* allocate read buffer */ - peer->rbuf = calloc(1, sizeof(struct peer_buf_read)); + peer->rbuf = calloc(1, sizeof(struct buf_read)); if (peer->rbuf == NULL) fatal(NULL); peer->rbuf->wpos = 0; |