nginx/src/os/unix/ngx_freebsd_sendfile_chain.c

361 lines
9.6 KiB
C
Raw Normal View History

2003-02-27 04:21:43 +08:00
2004-03-01 05:03:02 +08:00
/*
* Copyright (C) 2002-2004 Igor Sysoev, http://sysoev.ru/en/
*/
2003-02-27 04:21:43 +08:00
#include <ngx_config.h>
#include <ngx_core.h>
2003-10-09 15:00:45 +08:00
#include <ngx_event.h>
2003-02-27 04:21:43 +08:00
2003-05-27 20:18:54 +08:00
/*
2004-02-09 15:46:43 +08:00
* Although FreeBSD sendfile() allows to pass a header and a trailer
2004-06-21 03:54:15 +08:00
* it can not send a header with a part of the file in one packet until
2004-02-09 15:46:43 +08:00
* FreeBSD 5.2-STABLE. Besides over the fast ethernet connection sendfile()
2004-06-21 03:54:15 +08:00
* may send the partially filled packets, i.e. the 8 file pages may be sent
2004-03-16 15:10:12 +08:00
* as the 11 full 1460-bytes packets, then one incomplete 324-bytes packet,
* and then again the 11 full 1460-bytes packets.
2003-10-09 15:00:45 +08:00
*
2004-02-09 15:46:43 +08:00
* So we use the TCP_NOPUSH option (similar to Linux's TCP_CORK)
* to postpone the sending - it not only sends a header and the first part
* of the file in one packet but also sends file pages in the full packets.
*
* But until FreeBSD 4.5 the turning TCP_NOPUSH off does not flush a pending
2004-06-21 03:54:15 +08:00
* data that less than MSS so that data may be sent with 5 second delay.
2004-02-09 15:46:43 +08:00
* So we do not use TCP_NOPUSH on FreeBSD prior to 4.5 although it can be used
2003-10-10 23:10:50 +08:00
* for non-keepalive HTTP connections.
2003-10-09 15:00:45 +08:00
*/
2003-05-27 20:18:54 +08:00
2003-05-22 23:23:47 +08:00
ngx_chain_t *ngx_freebsd_sendfile_chain(ngx_connection_t *c, ngx_chain_t *in)
2003-02-27 04:21:43 +08:00
{
2003-11-26 04:44:56 +08:00
int rc;
2004-03-16 15:10:12 +08:00
u_char *prev;
2004-06-21 03:54:15 +08:00
off_t sent, fprev, send, limit;
2004-02-21 00:48:59 +08:00
size_t hsize, fsize;
ssize_t size;
2004-06-21 03:54:15 +08:00
ngx_uint_t eintr, eagain, ready;
2003-02-27 04:21:43 +08:00
struct iovec *iov;
struct sf_hdtr hdtr;
ngx_err_t err;
ngx_buf_t *file;
2003-02-27 04:21:43 +08:00
ngx_array_t header, trailer;
2003-11-03 06:56:18 +08:00
ngx_event_t *wev;
2003-10-23 00:38:26 +08:00
ngx_chain_t *cl, *tail;
2003-02-27 04:21:43 +08:00
2003-11-03 06:56:18 +08:00
wev = c->write;
if (!wev->ready) {
2003-10-13 00:49:16 +08:00
return in;
}
2003-11-03 06:56:18 +08:00
#if (HAVE_KQUEUE)
if ((ngx_event_flags & NGX_HAVE_KQUEUE_EVENT) && wev->kq_eof) {
2003-12-22 17:40:48 +08:00
ngx_log_error(NGX_LOG_INFO, c->log, wev->kq_errno,
2004-02-09 15:46:43 +08:00
"kevent() reported about an closed connection");
2003-11-03 06:56:18 +08:00
wev->error = 1;
return NGX_CHAIN_ERROR;
}
#endif
2004-06-21 03:54:15 +08:00
#if 1
limit = 4096;
#else
limit = OFF_T_MAX_VALUE;
#endif
2003-05-27 20:18:54 +08:00
do {
file = NULL;
2003-10-21 15:47:21 +08:00
fsize = 0;
2003-05-27 20:18:54 +08:00
hsize = 0;
2004-06-21 03:54:15 +08:00
send = 0;
2003-05-27 20:18:54 +08:00
eintr = 0;
2003-10-21 15:47:21 +08:00
eagain = 0;
2004-06-21 03:54:15 +08:00
ready = 0;
2003-02-27 04:21:43 +08:00
2003-05-27 20:18:54 +08:00
ngx_init_array(header, c->pool, 10, sizeof(struct iovec),
NGX_CHAIN_ERROR);
ngx_init_array(trailer, c->pool, 10, sizeof(struct iovec),
NGX_CHAIN_ERROR);
2003-02-27 04:21:43 +08:00
/* create the header iovec and coalesce the neighbouring bufs */
2003-03-12 04:38:13 +08:00
2003-10-10 23:10:50 +08:00
prev = NULL;
iov = NULL;
2003-10-09 15:00:45 +08:00
2004-02-18 23:45:21 +08:00
for (cl = in; cl && header.nelts < IOV_MAX; cl = cl->next) {
if (ngx_buf_special(cl->buf)) {
2003-10-10 23:10:50 +08:00
continue;
}
2003-02-27 04:21:43 +08:00
if (!ngx_buf_in_memory_only(cl->buf)) {
2003-10-10 23:10:50 +08:00
break;
}
2003-05-27 20:18:54 +08:00
2004-06-21 03:54:15 +08:00
size = cl->buf->last - cl->buf->pos;
if (send + size > limit) {
size = limit - send;
}
if (prev == cl->buf->pos) {
2004-06-21 03:54:15 +08:00
iov->iov_len += size;
2003-02-27 04:21:43 +08:00
2003-10-10 23:10:50 +08:00
} else {
ngx_test_null(iov, ngx_push_array(&header), NGX_CHAIN_ERROR);
iov->iov_base = (void *) cl->buf->pos;
2004-06-21 03:54:15 +08:00
iov->iov_len = size;
2003-05-15 01:13:13 +08:00
}
2003-10-10 23:10:50 +08:00
2004-06-21 03:54:15 +08:00
prev = cl->buf->pos + size;
hsize += size;
send += size;
2003-05-27 20:18:54 +08:00
}
2003-05-15 23:42:53 +08:00
/* get the file buf */
2003-10-09 15:00:45 +08:00
if (cl && cl->buf->in_file) {
file = cl->buf;
2004-06-21 03:54:15 +08:00
fsize = 0;
2003-10-21 15:47:21 +08:00
/* coalesce the neighbouring file bufs */
2003-10-21 15:47:21 +08:00
2004-06-21 03:54:15 +08:00
do {
size = (size_t) (cl->buf->file_last - cl->buf->file_pos);
if (send + size > limit) {
size = limit - send;
2003-10-21 15:47:21 +08:00
}
2004-06-21 03:54:15 +08:00
fsize += size;
send += size;
fprev = cl->buf->file_pos + size;
2003-10-23 00:38:26 +08:00
cl = cl->next;
2004-06-21 03:54:15 +08:00
} while (cl
&& cl->buf->in_file
&& file->file->fd == cl->buf->file->fd
&& fprev == cl->buf->file_pos);
2003-02-27 04:21:43 +08:00
}
2004-02-20 00:48:14 +08:00
if (file) {
/* create the tailer iovec and coalesce the neighbouring bufs */
2003-02-27 04:21:43 +08:00
2004-02-20 00:48:14 +08:00
prev = NULL;
iov = NULL;
2003-10-09 15:00:45 +08:00
2004-02-20 00:48:14 +08:00
for ( /* void */; cl && trailer.nelts < IOV_MAX; cl = cl->next) {
if (ngx_buf_special(cl->buf)) {
2004-02-20 00:48:14 +08:00
continue;
}
2003-10-09 15:00:45 +08:00
if (!ngx_buf_in_memory_only(cl->buf)) {
2004-02-20 00:48:14 +08:00
break;
}
2003-02-27 04:21:43 +08:00
2004-06-21 03:54:15 +08:00
size = cl->buf->last - cl->buf->pos;
if (send + size > limit) {
size = limit - send;
}
if (prev == cl->buf->pos) {
2004-06-21 03:54:15 +08:00
iov->iov_len += size;
2003-02-27 04:21:43 +08:00
2004-02-20 00:48:14 +08:00
} else {
ngx_test_null(iov, ngx_push_array(&trailer),
NGX_CHAIN_ERROR);
iov->iov_base = (void *) cl->buf->pos;
2004-06-21 03:54:15 +08:00
iov->iov_len = size;
2004-02-20 00:48:14 +08:00
}
2003-10-28 23:45:41 +08:00
2004-06-21 03:54:15 +08:00
prev = cl->buf->pos + size;
send += size;
2004-02-20 00:48:14 +08:00
}
2003-02-27 04:21:43 +08:00
}
2003-10-21 15:47:21 +08:00
/*
* the tail is the rest of the chain that exceedes
2003-10-21 15:47:21 +08:00
* a single sendfile() capability
*/
2003-10-23 00:38:26 +08:00
tail = cl;
2003-02-27 04:21:43 +08:00
2003-05-27 20:18:54 +08:00
if (file) {
2003-02-27 04:21:43 +08:00
2004-06-01 14:04:46 +08:00
if (ngx_freebsd_use_tcp_nopush
&& c->tcp_nopush == NGX_TCP_NOPUSH_UNSET)
{
2003-10-10 23:10:50 +08:00
2003-06-05 01:28:33 +08:00
if (ngx_tcp_nopush(c->fd) == NGX_ERROR) {
2004-02-09 15:46:43 +08:00
err = ngx_errno;
/*
* there is a tiny chance to be interrupted, however
* we continue a processing without the TCP_NOPUSH
*/
if (err != NGX_EINTR) {
wev->error = 1;
ngx_connection_error(c, err,
ngx_tcp_nopush_n " failed");
return NGX_CHAIN_ERROR;
}
} else {
2004-06-01 14:04:46 +08:00
c->tcp_nopush = NGX_TCP_NOPUSH_SET;
2004-02-09 15:46:43 +08:00
ngx_log_debug0(NGX_LOG_DEBUG_EVENT, c->log, 0,
"tcp_nopush");
2003-05-27 20:18:54 +08:00
}
}
2003-02-27 04:21:43 +08:00
2003-05-27 20:18:54 +08:00
hdtr.headers = (struct iovec *) header.elts;
hdtr.hdr_cnt = header.nelts;
hdtr.trailers = (struct iovec *) trailer.elts;
hdtr.trl_cnt = trailer.nelts;
2003-10-22 00:49:56 +08:00
/*
2003-10-23 00:38:26 +08:00
* the "nbytes bug" of the old sendfile() syscall:
2003-10-22 00:49:56 +08:00
* http://www.freebsd.org/cgi/query-pr.cgi?pr=33771
*/
2003-05-27 20:18:54 +08:00
if (ngx_freebsd_sendfile_nbytes_bug == 0) {
hsize = 0;
}
2003-12-02 13:47:29 +08:00
sent = 0;
2003-05-27 20:18:54 +08:00
rc = sendfile(file->file->fd, c->fd, file->file_pos,
2003-10-21 15:47:21 +08:00
fsize + hsize, &hdtr, &sent, 0);
2003-05-27 20:18:54 +08:00
if (rc == -1) {
err = ngx_errno;
2004-02-09 15:46:43 +08:00
if (err == NGX_EAGAIN || err == NGX_EINTR) {
if (err == NGX_EINTR) {
eintr = 1;
2003-12-26 04:26:58 +08:00
2004-02-09 15:46:43 +08:00
} else {
eagain = 1;
}
2003-10-21 15:47:21 +08:00
2003-12-22 17:40:48 +08:00
ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, err,
"sendfile() sent only " OFF_T_FMT " bytes",
sent);
2003-05-27 20:18:54 +08:00
} else {
2003-11-03 06:56:18 +08:00
wev->error = 1;
2004-02-09 15:46:43 +08:00
ngx_connection_error(c, err, "sendfile() failed");
2003-05-27 20:18:54 +08:00
return NGX_CHAIN_ERROR;
}
2003-02-27 04:21:43 +08:00
}
2003-12-22 17:40:48 +08:00
ngx_log_debug4(NGX_LOG_DEBUG_EVENT, c->log, 0,
"sendfile: %d, @" OFF_T_FMT " " OFF_T_FMT ":%d",
rc, file->file_pos, sent, fsize + hsize);
2003-02-27 04:21:43 +08:00
2003-05-27 20:18:54 +08:00
} else {
2003-11-03 06:56:18 +08:00
rc = writev(c->fd, header.elts, header.nelts);
2003-10-10 23:10:50 +08:00
2004-03-30 01:43:58 +08:00
ngx_log_debug2(NGX_LOG_DEBUG_EVENT, c->log, 0,
"writev: %d of " SIZE_T_FMT, rc, hsize);
2003-10-10 23:10:50 +08:00
if (rc == -1) {
err = ngx_errno;
2003-12-22 17:40:48 +08:00
if (err == NGX_EAGAIN || err == NGX_EINTR) {
2004-02-09 15:46:43 +08:00
if (err == NGX_EINTR) {
eintr = 1;
}
2003-12-22 17:40:48 +08:00
ngx_log_debug0(NGX_LOG_DEBUG_EVENT, c->log, err,
"writev() not ready");
2003-10-10 23:10:50 +08:00
} else {
2003-11-03 06:56:18 +08:00
wev->error = 1;
2004-02-09 15:46:43 +08:00
ngx_connection_error(c, err, "writev() failed");
2003-10-10 23:10:50 +08:00
return NGX_CHAIN_ERROR;
2003-05-27 20:18:54 +08:00
}
2003-10-10 23:10:50 +08:00
}
2003-02-27 04:21:43 +08:00
2003-10-10 23:10:50 +08:00
sent = rc > 0 ? rc : 0;
2003-05-27 20:18:54 +08:00
}
2003-02-27 04:21:43 +08:00
2004-06-21 03:54:15 +08:00
if (send == sent) {
ready = 1;
}
2003-05-27 20:18:54 +08:00
c->sent += sent;
2003-02-27 04:21:43 +08:00
2003-10-23 00:38:26 +08:00
for (cl = in; cl; cl = cl->next) {
2003-02-27 04:21:43 +08:00
if (ngx_buf_special(cl->buf)) {
2003-10-21 15:47:21 +08:00
continue;
}
if (sent == 0) {
break;
2003-05-27 20:18:54 +08:00
}
size = ngx_buf_size(cl->buf);
2003-10-21 15:47:21 +08:00
2003-05-27 20:18:54 +08:00
if (sent >= size) {
sent -= size;
2003-02-27 04:21:43 +08:00
if (ngx_buf_in_memory(cl->buf)) {
cl->buf->pos = cl->buf->last;
2003-05-27 20:18:54 +08:00
}
if (cl->buf->in_file) {
cl->buf->file_pos = cl->buf->file_last;
2003-05-27 20:18:54 +08:00
}
continue;
}
2003-03-12 04:38:13 +08:00
if (ngx_buf_in_memory(cl->buf)) {
cl->buf->pos += sent;
2003-03-12 04:38:13 +08:00
}
if (cl->buf->in_file) {
cl->buf->file_pos += sent;
2003-03-12 04:38:13 +08:00
}
2003-02-27 04:21:43 +08:00
2003-05-27 20:18:54 +08:00
break;
2003-02-27 04:21:43 +08:00
}
2004-06-21 03:54:15 +08:00
if (ready) {
return cl;
}
2003-10-23 00:38:26 +08:00
in = cl;
2003-02-27 04:21:43 +08:00
2003-10-21 15:47:21 +08:00
if (eagain) {
2003-10-23 00:38:26 +08:00
2003-10-22 00:49:56 +08:00
/*
* sendfile() can return EAGAIN even if it has sent
2004-02-09 15:46:43 +08:00
* a whole file part but the successive sendfile() call would
* return EAGAIN right away and would not send anything.
* We use it as a hint.
2003-10-22 00:49:56 +08:00
*/
2003-10-23 00:38:26 +08:00
2003-11-03 06:56:18 +08:00
wev->ready = 0;
2003-10-21 15:47:21 +08:00
break;
}
/* "tail == in" means that a single sendfile() is complete */
} while ((tail && tail == in) || eintr);
2003-02-27 04:21:43 +08:00
2003-10-21 15:47:21 +08:00
if (in) {
2003-11-03 06:56:18 +08:00
wev->ready = 0;
2003-10-09 15:00:45 +08:00
}
2003-10-21 15:47:21 +08:00
return in;
2003-02-27 04:21:43 +08:00
}