nginx/src/os/unix/ngx_freebsd_sendfile_chain.c

395 lines
11 KiB
C
Raw Normal View History

2003-02-27 04:21:43 +08:00
2004-03-01 05:03:02 +08:00
/*
* Copyright (C) Igor Sysoev
2004-03-01 05:03:02 +08:00
*/
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
* FreeBSD 5.3. 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 the file pages in the full packets.
2004-02-09 15:46:43 +08:00
*
* 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
#define NGX_HEADERS 8
#define NGX_TRAILERS 4
2004-06-21 23:59:32 +08:00
ngx_chain_t *ngx_freebsd_sendfile_chain(ngx_connection_t *c, ngx_chain_t *in,
off_t limit)
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 23:59:32 +08:00
off_t fprev, sent, send, sprev, aligned;
2004-02-21 00:48:59 +08:00
size_t hsize, fsize;
ssize_t size;
2004-06-21 23:59:32 +08:00
ngx_uint_t eintr, eagain, complete;
2003-02-27 04:21:43 +08:00
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;
2004-06-21 23:59:32 +08:00
ngx_chain_t *cl;
struct sf_hdtr hdtr;
struct iovec *iov, headers[NGX_HEADERS], trailers[NGX_TRAILERS];
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_USE_KQUEUE_EVENT) && wev->pending_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 23:59:32 +08:00
send = 0;
eagain = 0;
2004-06-21 03:54:15 +08:00
header.elts = headers;
header.size = sizeof(struct iovec);
header.nalloc = NGX_HEADERS;
header.pool = c->pool;
trailer.elts = trailers;
trailer.size = sizeof(struct iovec);
trailer.nalloc = NGX_TRAILERS;
trailer.pool = c->pool;
2004-06-21 23:59:32 +08:00
for ( ;; ) {
2003-05-27 20:18:54 +08:00
file = NULL;
2003-10-21 15:47:21 +08:00
fsize = 0;
2003-05-27 20:18:54 +08:00
hsize = 0;
eintr = 0;
2004-06-21 23:59:32 +08:00
complete = 0;
sprev = send;
2003-02-27 04:21:43 +08:00
header.nelts = 0;
trailer.nelts = 0;
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-06-21 23:59:32 +08:00
for (cl = in;
cl && header.nelts < IOV_MAX && send < limit;
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 {
if (!(iov = ngx_array_push(&header))) {
return 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
2003-10-09 15:00:45 +08:00
2004-06-21 23:59:32 +08:00
if (cl && cl->buf->in_file && send < limit) {
file = cl->buf;
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;
2004-06-21 23:59:32 +08:00
aligned = (cl->buf->file_pos + size + ngx_pagesize - 1)
& ~(ngx_pagesize - 1);
if (aligned <= cl->buf->file_last) {
size = aligned - cl->buf->file_pos;
}
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
2004-06-21 23:59:32 +08:00
&& send < limit
2004-06-21 03:54:15 +08:00
&& 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
while (cl && header.nelts < IOV_MAX && send < limit) {
if (ngx_buf_special(cl->buf)) {
cl = cl->next;
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 {
if (!(iov = ngx_array_push(&trailer))) {
return 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;
cl = cl->next;
2004-02-20 00:48:14 +08:00
}
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-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
}
if (rc == 0 && sent == 0) {
/*
* rc and sent are equals to zero when someone has truncated
* the file, so the offset became beyond the end of the file
*/
ngx_log_error(NGX_LOG_ALERT, c->log, 0,
"sendfile() reported that \"%s\" was truncated",
file->file->name.data);
return NGX_CHAIN_ERROR;
}
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 23:59:32 +08:00
if (send - sprev == sent) {
complete = 1;
2004-06-21 03:54:15 +08:00
}
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
}
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;
2004-06-21 23:59:32 +08:00
return cl;
2003-10-21 15:47:21 +08:00
}
2004-06-21 23:59:32 +08:00
if (eintr) {
continue;
}
2003-10-21 15:47:21 +08:00
2004-06-21 23:59:32 +08:00
if (!complete) {
wev->ready = 0;
return cl;
}
2003-02-27 04:21:43 +08:00
2004-06-21 23:59:32 +08:00
if (send >= limit || cl == NULL) {
return cl;
}
2003-10-09 15:00:45 +08:00
2004-06-21 23:59:32 +08:00
in = cl;
}
2003-02-27 04:21:43 +08:00
}