nginx/src/os/unix/ngx_freebsd_sendfile_chain.c

411 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
/*
* 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
*
* Threfore 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 turning TCP_NOPUSH off does not flush a pending
* data that less than MSS, so that data may be sent with 5 second delay.
* 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
#if (IOV_MAX > 64)
#define NGX_HEADERS 64
#define NGX_TRAILERS 64
#else
#define NGX_HEADERS IOV_MAX
#define NGX_TRAILERS IOV_MAX
#endif
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;
off_t size, send, prev_send, aligned, sent, fprev;
size_t header_size, file_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;
}
#if (NGX_HAVE_KQUEUE)
2003-11-03 06:56:18 +08:00
if ((ngx_event_flags & NGX_USE_KQUEUE_EVENT) && wev->pending_eof) {
(void) ngx_connection_error(c, wev->kq_errno,
"kevent() reported about an closed connection");
2003-11-03 06:56:18 +08:00
wev->error = 1;
return NGX_CHAIN_ERROR;
}
#endif
/* the maximum limit size is the maximum size_t value - the page size */
if (limit == 0 || limit > NGX_MAX_SIZE_T_VALUE - ngx_pagesize) {
limit = NGX_MAX_SIZE_T_VALUE - ngx_pagesize;
}
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;
file_size = 0;
header_size = 0;
2003-05-27 20:18:54 +08:00
eintr = 0;
2004-06-21 23:59:32 +08:00
complete = 0;
prev_send = 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) {
iov->iov_len += (size_t) size;
2003-02-27 04:21:43 +08:00
2003-10-10 23:10:50 +08:00
} else {
iov = ngx_array_push(&header);
if (iov == NULL) {
return NGX_CHAIN_ERROR;
}
iov->iov_base = (void *) cl->buf->pos;
iov->iov_len = (size_t) size;
2003-05-15 01:13:13 +08:00
}
2003-10-10 23:10:50 +08:00
prev = cl->buf->pos + (size_t) size;
header_size += (size_t) size;
2004-06-21 03:54:15 +08:00
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 = cl->buf->file_last - cl->buf->file_pos;
2004-06-21 03:54:15 +08:00
if (send + size > limit) {
size = limit - send;
2004-06-21 23:59:32 +08:00
aligned = (cl->buf->file_pos + size + ngx_pagesize - 1)
& ~((off_t) ngx_pagesize - 1);
2004-06-21 23:59:32 +08:00
if (aligned <= cl->buf->file_last) {
size = aligned - cl->buf->file_pos;
}
2003-10-21 15:47:21 +08:00
}
file_size += (size_t) size;
2004-06-21 03:54:15 +08:00
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) {
iov->iov_len += (size_t) size;
2003-02-27 04:21:43 +08:00
2004-02-20 00:48:14 +08:00
} else {
iov = ngx_array_push(&trailer);
if (iov == NULL) {
return NGX_CHAIN_ERROR;
}
iov->iov_base = (void *) cl->buf->pos;
iov->iov_len = (size_t) size;
2004-02-20 00:48:14 +08:00
}
2003-10-28 23:45:41 +08:00
prev = cl->buf->pos + (size_t) size;
2004-06-21 03:54:15 +08:00
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,
2004-02-09 15:46:43 +08:00
* we continue a processing without the TCP_NOPUSH
*/
if (err != NGX_EINTR) {
wev->error = 1;
(void) ngx_connection_error(c, err,
ngx_tcp_nopush_n " failed");
2004-02-09 15:46:43 +08:00
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
*/
if (!ngx_freebsd_sendfile_nbytes_bug) {
header_size = 0;
2003-05-27 20:18:54 +08:00
}
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,
file_size + header_size, &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 %O bytes", sent);
2003-05-27 20:18:54 +08:00
} else {
2003-11-03 06:56:18 +08:00
wev->error = 1;
(void) 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
}
/*
* sendfile() in FreeBSD 3.x-4.x may return value >= 0
* on success, although only 0 is documented
*/
if (rc >= 0 && sent == 0) {
/*
* if rc is OK and sent equal to zero, then 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, @%O %O:%uz",
rc, file->file_pos, sent, file_size + header_size);
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 %uz", rc, header_size);
2004-03-30 01:43:58 +08:00
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
if (send - prev_send == sent) {
2004-06-21 23:59:32 +08:00
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 += (size_t) 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() may return EAGAIN, even if it has sent a whole file
* part, it indicates that the successive sendfile() call would
2004-02-09 15:46:43 +08:00
* 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
}