nginx/src/os/unix/ngx_freebsd_sendfile_chain.c

319 lines
9.0 KiB
C
Raw Normal View History

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
* it never sends a header with a part of the file in one packet until
* FreeBSD 5.2-STABLE. Besides over the fast ethernet connection sendfile()
* can send the partially filled packets, i.e. the 8 file pages can be sent
* as 11 full 1460-bytes packets, then one incomplete 324-bytes packet, and
* then again 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
2003-12-01 04:03:18 +08:00
* data that less than MSS so that data can 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;
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;
2004-02-09 15:46:43 +08:00
ngx_int_t eintr, eagain;
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,
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
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-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
2004-02-18 23:45:21 +08:00
for (cl = in; cl && header.nelts < IOV_MAX; cl = cl->next) {
2003-10-23 00:38:26 +08:00
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
}
2004-02-20 00:48:14 +08:00
if (file) {
/* create the tailer iovec and coalesce the neighbouring hunks */
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_hunk_special(cl->hunk)) {
continue;
}
2003-10-09 15:00:45 +08:00
2004-02-20 00:48:14 +08:00
if (!ngx_hunk_in_memory_only(cl->hunk)) {
break;
}
2003-02-27 04:21:43 +08:00
2004-02-20 00:48:14 +08:00
if (prev == cl->hunk->pos) {
iov->iov_len += cl->hunk->last - cl->hunk->pos;
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 = cl->hunk->pos;
iov->iov_len = cl->hunk->last - cl->hunk->pos;
}
2003-10-28 23:45:41 +08:00
2004-02-20 00:48:14 +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
2004-01-06 04:55:48 +08:00
if (ngx_freebsd_use_tcp_nopush && c->tcp_nopush == 0) {
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 {
c->tcp_nopush = 1;
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
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-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
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
}