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
|
|
|
/*
|
2003-12-01 04:03:18 +08:00
|
|
|
* FreeBSD's sendfile() often sends 4K pages over ethernet in 3 packets: 2x1460
|
|
|
|
* and 1176 or in 6 packets: 5x1460 and 892. Besides although sendfile()
|
|
|
|
* allows to pass the header and the trailer it never sends the header or
|
|
|
|
* the trailer with the part of the file in one packet. So we use TCP_NOPUSH
|
|
|
|
* (similar to Linux's TCP_CORK) to postpone the sending - it not only sends
|
|
|
|
* the header and the first part of the file in one packet but also sends
|
|
|
|
* 4K pages in the full packets.
|
2003-10-09 15:00:45 +08:00
|
|
|
*
|
2003-12-01 04:03:18 +08:00
|
|
|
* Until FreeBSD 4.5 the turning TCP_NOPUSH off does not flush a pending
|
|
|
|
* data that less than MSS so that data can be sent with 5 second delay.
|
2003-10-10 23:10:50 +08:00
|
|
|
* We do not use TCP_NOPUSH on FreeBSD prior to 4.5 although it can be used
|
|
|
|
* 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;
|
2003-02-27 04:21:43 +08:00
|
|
|
char *prev;
|
2003-10-21 15:47:21 +08:00
|
|
|
off_t sent, fprev;
|
2003-11-21 14:30:49 +08:00
|
|
|
size_t hsize, fsize, size;
|
2003-12-26 04:26:58 +08:00
|
|
|
ngx_int_t eintr, eagain, level;
|
2003-02-27 04:21:43 +08:00
|
|
|
struct iovec *iov;
|
|
|
|
struct sf_hdtr hdtr;
|
|
|
|
ngx_err_t err;
|
2003-11-26 04:44:56 +08:00
|
|
|
ngx_hunk_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,
|
2003-11-03 06:56:18 +08:00
|
|
|
"kevent() reported about closed connection");
|
|
|
|
|
|
|
|
wev->error = 1;
|
|
|
|
return NGX_CHAIN_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
#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;
|
|
|
|
eintr = 0;
|
2003-10-21 15:47:21 +08:00
|
|
|
eagain = 0;
|
2003-12-26 04:26:58 +08:00
|
|
|
level = NGX_LOG_CRIT;
|
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
|
|
|
|
2003-10-23 00:38:26 +08:00
|
|
|
/* create the header iovec and coalesce the neighbouring hunks */
|
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
|
|
|
|
2003-10-23 00:38:26 +08:00
|
|
|
for (cl = in; cl; cl = cl->next) {
|
|
|
|
if (ngx_hunk_special(cl->hunk)) {
|
2003-10-10 23:10:50 +08:00
|
|
|
continue;
|
|
|
|
}
|
2003-02-27 04:21:43 +08:00
|
|
|
|
2003-10-23 00:38:26 +08:00
|
|
|
if (!ngx_hunk_in_memory_only(cl->hunk)) {
|
2003-10-10 23:10:50 +08:00
|
|
|
break;
|
|
|
|
}
|
2003-05-27 20:18:54 +08:00
|
|
|
|
2003-10-23 00:38:26 +08:00
|
|
|
if (prev == cl->hunk->pos) {
|
|
|
|
iov->iov_len += cl->hunk->last - cl->hunk->pos;
|
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);
|
2003-10-23 00:38:26 +08:00
|
|
|
iov->iov_base = cl->hunk->pos;
|
|
|
|
iov->iov_len = cl->hunk->last - cl->hunk->pos;
|
2003-05-15 01:13:13 +08:00
|
|
|
}
|
2003-10-10 23:10:50 +08:00
|
|
|
|
2003-10-28 23:45:41 +08:00
|
|
|
prev = cl->hunk->last;
|
2003-10-23 00:38:26 +08:00
|
|
|
hsize += cl->hunk->last - cl->hunk->pos;
|
2003-05-27 20:18:54 +08:00
|
|
|
}
|
2003-05-15 23:42:53 +08:00
|
|
|
|
2003-10-21 15:47:21 +08:00
|
|
|
/* get the file hunk */
|
2003-10-09 15:00:45 +08:00
|
|
|
|
2003-10-23 00:38:26 +08:00
|
|
|
if (cl && (cl->hunk->type & NGX_HUNK_FILE)) {
|
|
|
|
file = cl->hunk;
|
2003-10-21 15:47:21 +08:00
|
|
|
fsize = (size_t) (file->file_last - file->file_pos);
|
|
|
|
fprev = file->file_last;
|
2003-10-23 00:38:26 +08:00
|
|
|
cl = cl->next;
|
2003-10-21 15:47:21 +08:00
|
|
|
|
|
|
|
/* coalesce the neighbouring file hunks */
|
|
|
|
|
2003-10-23 00:38:26 +08:00
|
|
|
while (cl && (cl->hunk->type & NGX_HUNK_FILE)) {
|
|
|
|
if (file->file->fd != cl->hunk->file->fd
|
|
|
|
|| fprev != cl->hunk->file_pos)
|
2003-10-21 15:47:21 +08:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2003-10-23 00:38:26 +08:00
|
|
|
fsize += (size_t) (cl->hunk->file_last - cl->hunk->file_pos);
|
|
|
|
fprev = cl->hunk->file_last;
|
|
|
|
cl = cl->next;
|
2003-10-21 15:47:21 +08:00
|
|
|
}
|
2003-02-27 04:21:43 +08:00
|
|
|
}
|
|
|
|
|
2003-10-23 00:38:26 +08:00
|
|
|
/* create the tailer iovec and coalesce the neighbouring hunks */
|
2003-02-27 04:21:43 +08:00
|
|
|
|
2003-10-10 23:10:50 +08:00
|
|
|
prev = NULL;
|
|
|
|
iov = NULL;
|
2003-10-09 15:00:45 +08:00
|
|
|
|
2003-10-23 00:38:26 +08:00
|
|
|
for ( /* void */; cl; cl = cl->next) {
|
|
|
|
if (ngx_hunk_special(cl->hunk)) {
|
2003-10-10 23:10:50 +08:00
|
|
|
continue;
|
|
|
|
}
|
2003-10-09 15:00:45 +08:00
|
|
|
|
2003-10-23 00:38:26 +08:00
|
|
|
if (!ngx_hunk_in_memory_only(cl->hunk)) {
|
2003-10-10 23:10:50 +08:00
|
|
|
break;
|
|
|
|
}
|
2003-02-27 04:21:43 +08:00
|
|
|
|
2003-10-23 00:38:26 +08:00
|
|
|
if (prev == cl->hunk->pos) {
|
|
|
|
iov->iov_len += cl->hunk->last - cl->hunk->pos;
|
2003-02-27 04:21:43 +08:00
|
|
|
|
2003-10-10 23:10:50 +08:00
|
|
|
} else {
|
|
|
|
ngx_test_null(iov, ngx_push_array(&trailer), NGX_CHAIN_ERROR);
|
2003-10-23 00:38:26 +08:00
|
|
|
iov->iov_base = cl->hunk->pos;
|
|
|
|
iov->iov_len = cl->hunk->last - cl->hunk->pos;
|
2003-02-27 04:21:43 +08:00
|
|
|
}
|
2003-10-28 23:45:41 +08:00
|
|
|
|
|
|
|
prev = cl->hunk->last;
|
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 exceeded
|
|
|
|
* 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
|
|
|
|
2003-10-10 23:10:50 +08:00
|
|
|
if (ngx_freebsd_use_tcp_nopush && !c->tcp_nopush) {
|
2003-06-02 23:24:30 +08:00
|
|
|
c->tcp_nopush = 1;
|
2003-10-10 23:10:50 +08:00
|
|
|
|
2003-12-22 17:40:48 +08:00
|
|
|
ngx_log_debug0(NGX_LOG_DEBUG_EVENT, c->log, 0, "tcp_nopush");
|
2003-10-10 23:10:50 +08:00
|
|
|
|
2003-06-05 01:28:33 +08:00
|
|
|
if (ngx_tcp_nopush(c->fd) == NGX_ERROR) {
|
2003-11-26 04:44:56 +08:00
|
|
|
ngx_log_error(NGX_LOG_CRIT, c->log, ngx_errno,
|
2003-06-05 01:28:33 +08:00
|
|
|
ngx_tcp_nopush_n " failed");
|
2003-05-27 20:18:54 +08:00
|
|
|
return NGX_CHAIN_ERROR;
|
|
|
|
}
|
|
|
|
}
|
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;
|
|
|
|
|
|
|
|
if (err == NGX_EINTR) {
|
|
|
|
eintr = 1;
|
|
|
|
|
2003-12-26 04:26:58 +08:00
|
|
|
} else if (err == NGX_EAGAIN) {
|
2003-10-21 15:47:21 +08:00
|
|
|
eagain = 1;
|
2003-12-26 04:26:58 +08:00
|
|
|
|
|
|
|
} else if (err == NGX_EPIPE) {
|
|
|
|
level = NGX_LOG_INFO;
|
2003-10-21 15:47:21 +08:00
|
|
|
}
|
|
|
|
|
2003-05-27 20:18:54 +08:00
|
|
|
if (err == NGX_EAGAIN || err == NGX_EINTR) {
|
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;
|
2003-12-26 04:26:58 +08:00
|
|
|
ngx_log_error(level, c->log, err,
|
2003-05-27 20:18:54 +08:00
|
|
|
"sendfile() failed");
|
|
|
|
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
|
|
|
|
|
|
|
if (rc == -1) {
|
|
|
|
err = ngx_errno;
|
|
|
|
|
2003-12-22 17:40:48 +08:00
|
|
|
if (err == NGX_EINTR) {
|
2003-10-10 23:10:50 +08:00
|
|
|
eintr = 1;
|
2003-12-26 04:26:58 +08:00
|
|
|
|
|
|
|
} else if (err == NGX_EPIPE) {
|
|
|
|
level = NGX_LOG_INFO;
|
2003-12-22 17:40:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (err == NGX_EAGAIN || err == NGX_EINTR) {
|
|
|
|
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;
|
2003-12-26 04:26:58 +08:00
|
|
|
ngx_log_error(level, c->log, 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-02-27 04:21:43 +08:00
|
|
|
|
2003-12-22 17:40:48 +08:00
|
|
|
ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0,
|
|
|
|
"writev: " OFF_T_FMT, sent);
|
2003-05-27 20:18:54 +08:00
|
|
|
}
|
2003-02-27 04:21:43 +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
|
|
|
|
2003-10-23 00:38:26 +08:00
|
|
|
if (ngx_hunk_special(cl->hunk)) {
|
2003-10-21 15:47:21 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sent == 0) {
|
|
|
|
break;
|
2003-05-27 20:18:54 +08:00
|
|
|
}
|
|
|
|
|
2003-10-23 00:38:26 +08:00
|
|
|
size = ngx_hunk_size(cl->hunk);
|
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
|
|
|
|
2003-10-23 00:38:26 +08:00
|
|
|
if (cl->hunk->type & NGX_HUNK_IN_MEMORY) {
|
|
|
|
cl->hunk->pos = cl->hunk->last;
|
2003-05-27 20:18:54 +08:00
|
|
|
}
|
|
|
|
|
2003-10-23 00:38:26 +08:00
|
|
|
if (cl->hunk->type & NGX_HUNK_FILE) {
|
|
|
|
cl->hunk->file_pos = cl->hunk->file_last;
|
2003-05-27 20:18:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
2003-03-12 04:38:13 +08:00
|
|
|
|
2003-10-23 00:38:26 +08:00
|
|
|
if (cl->hunk->type & NGX_HUNK_IN_MEMORY) {
|
|
|
|
cl->hunk->pos += sent;
|
2003-03-12 04:38:13 +08:00
|
|
|
}
|
|
|
|
|
2003-10-23 00:38:26 +08:00
|
|
|
if (cl->hunk->type & NGX_HUNK_FILE) {
|
|
|
|
cl->hunk->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-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
|
|
|
|
* a whole file part and successive sendfile() would
|
2003-10-23 00:38:26 +08:00
|
|
|
* return EAGAIN right away and would not send anything
|
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
|
|
|
}
|