From a890fa2ffaf16e0bdc8afc16c6ef5d7e7280b8f3 Mon Sep 17 00:00:00 2001 From: Henning Brauer Date: Sat, 10 Jan 2004 23:25:45 +0000 Subject: use writev(2) to get as many messages out at once as possible. should gives us a little exra performance gain ok claudio@ --- usr.sbin/bgpd/buffer.c | 44 ++++++++++++++++++++++++++++++++++++-------- 1 file changed, 36 insertions(+), 8 deletions(-) (limited to 'usr.sbin') diff --git a/usr.sbin/bgpd/buffer.c b/usr.sbin/bgpd/buffer.c index f7026dec044..7eff2add24c 100644 --- a/usr.sbin/bgpd/buffer.c +++ b/usr.sbin/bgpd/buffer.c @@ -1,4 +1,4 @@ -/* $OpenBSD: buffer.c,v 1.13 2004/01/10 21:02:54 henning Exp $ */ +/* $OpenBSD: buffer.c,v 1.14 2004/01/10 23:25:44 henning Exp $ */ /* * Copyright (c) 2003, 2004 Henning Brauer @@ -17,6 +17,7 @@ */ #include +#include #include #include @@ -140,15 +141,42 @@ msgbuf_write(struct msgbuf *msgbuf) * we MUST return and NOT try to write out stuff from later buffers - * the socket might have become writeable again */ - struct buf *buf; - int n; + struct iovec iov[IOV_MAX]; + struct buf *buf, *next; + int i = 0; + ssize_t n; + + TAILQ_FOREACH(buf, &msgbuf->bufs, entries) { + if (i >= IOV_MAX) + break; + iov[i].iov_base = buf->buf + buf->rpos; + iov[i].iov_len = buf->size - buf->rpos; + i++; + } - buf = TAILQ_FIRST(&msgbuf->bufs); - if ((n = buf_write(msgbuf->sock, buf)) < 0) - return (n); + if ((n = writev(msgbuf->sock, iov, i)) == -1) { + if (errno == EAGAIN) /* cannot write immediately */ + return (0); + else + return (-1); + } - if (n == 1) /* everything written out */ - buf_dequeue(msgbuf, buf); + if (n == 0) { /* connection closed */ + errno = 0; + return (-2); + } + + for (buf = TAILQ_FIRST(&msgbuf->bufs); buf != NULL && n > 0; + buf = next) { + next = TAILQ_NEXT(buf, entries); + if (n >= buf->size - buf->rpos) { + n -= buf->size - buf->rpos; + buf_dequeue(msgbuf, buf); + } else { + buf->rpos += n; + n = 0; + } + } return (0); } -- cgit v1.2.3