2003-05-29 21:02:09 +08:00
|
|
|
|
2004-03-01 05:03:02 +08:00
|
|
|
/*
|
2004-09-30 00:00:49 +08:00
|
|
|
* Copyright (C) Igor Sysoev
|
2004-03-01 05:03:02 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
2003-05-29 21:02:09 +08:00
|
|
|
#include <ngx_config.h>
|
|
|
|
#include <ngx_core.h>
|
2003-11-26 04:44:56 +08:00
|
|
|
#include <ngx_event.h>
|
2003-05-29 21:02:09 +08:00
|
|
|
|
|
|
|
|
2003-11-28 16:40:40 +08:00
|
|
|
/*
|
|
|
|
* On Linux up to 2.4.21 sendfile() (syscall #187) works with 32-bit
|
2004-02-09 15:46:43 +08:00
|
|
|
* offsets only and the including <sys/sendfile.h> breaks the compiling
|
|
|
|
* if off_t is 64 bit wide. So we use own sendfile() definition where offset
|
2004-11-26 00:17:31 +08:00
|
|
|
* parameter is int32_t and use sendfile() for the file parts below 2G only.
|
2003-11-28 16:40:40 +08:00
|
|
|
*
|
|
|
|
* Linux 2.4.21 has a new sendfile64() syscall #239.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2004-09-17 00:10:13 +08:00
|
|
|
#define NGX_HEADERS 8
|
|
|
|
|
|
|
|
|
2004-06-21 23:59:32 +08:00
|
|
|
ngx_chain_t *ngx_linux_sendfile_chain(ngx_connection_t *c, ngx_chain_t *in,
|
|
|
|
off_t limit)
|
2003-05-29 21:02:09 +08:00
|
|
|
{
|
2004-12-03 02:40:46 +08:00
|
|
|
int rc, tcp_nodelay;
|
2004-03-16 15:10:12 +08:00
|
|
|
u_char *prev;
|
2004-12-03 02:40:46 +08:00
|
|
|
off_t size, send, prev_send, aligned, sent, fprev;
|
|
|
|
size_t file_size;
|
2004-06-21 23:59:32 +08:00
|
|
|
ngx_uint_t eintr, complete;
|
2003-05-29 21:02:09 +08:00
|
|
|
ngx_err_t err;
|
2004-05-28 23:49:23 +08:00
|
|
|
ngx_buf_t *file;
|
2003-11-26 04:44:56 +08:00
|
|
|
ngx_array_t header;
|
|
|
|
ngx_event_t *wev;
|
2004-06-21 23:59:32 +08:00
|
|
|
ngx_chain_t *cl;
|
2004-09-17 00:10:13 +08:00
|
|
|
struct iovec *iov, headers[NGX_HEADERS];
|
2004-10-21 23:34:38 +08:00
|
|
|
#if (NGX_HAVE_SENDFILE64)
|
2003-11-28 03:01:37 +08:00
|
|
|
off_t offset;
|
|
|
|
#else
|
|
|
|
int32_t offset;
|
|
|
|
#endif
|
2003-05-29 21:02:09 +08:00
|
|
|
|
2003-11-26 04:44:56 +08:00
|
|
|
wev = c->write;
|
2003-05-29 21:02:09 +08:00
|
|
|
|
2003-11-26 04:44:56 +08:00
|
|
|
if (!wev->ready) {
|
|
|
|
return in;
|
|
|
|
}
|
|
|
|
|
2004-12-03 02:40:46 +08:00
|
|
|
|
|
|
|
/* the maximum limit size is the maximum size_t value - the page size */
|
|
|
|
|
|
|
|
if (limit == 0 || limit > MAX_SIZE_T_VALUE - ngx_pagesize) {
|
|
|
|
limit = MAX_SIZE_T_VALUE - ngx_pagesize;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-06-21 23:59:32 +08:00
|
|
|
send = 0;
|
|
|
|
|
2004-09-17 00:10:13 +08:00
|
|
|
header.elts = headers;
|
|
|
|
header.size = sizeof(struct iovec);
|
|
|
|
header.nalloc = NGX_HEADERS;
|
|
|
|
header.pool = c->pool;
|
|
|
|
|
2004-06-21 23:59:32 +08:00
|
|
|
for ( ;; ) {
|
2003-11-26 04:44:56 +08:00
|
|
|
file = NULL;
|
2004-12-03 02:40:46 +08:00
|
|
|
file_size = 0;
|
2003-11-26 04:44:56 +08:00
|
|
|
eintr = 0;
|
2004-06-21 23:59:32 +08:00
|
|
|
complete = 0;
|
2004-12-03 02:40:46 +08:00
|
|
|
prev_send = send;
|
2003-05-29 21:02:09 +08:00
|
|
|
|
2004-09-17 00:10:13 +08:00
|
|
|
header.nelts = 0;
|
2003-05-29 21:02:09 +08:00
|
|
|
|
|
|
|
prev = NULL;
|
|
|
|
iov = NULL;
|
|
|
|
|
2004-05-28 23:49:23 +08:00
|
|
|
/* create the iovec and coalesce the neighbouring bufs */
|
2003-11-26 04:44:56 +08:00
|
|
|
|
2004-06-21 23:59:32 +08:00
|
|
|
for (cl = in;
|
|
|
|
cl && header.nelts < IOV_MAX && send < limit;
|
|
|
|
cl = cl->next)
|
|
|
|
{
|
2004-06-01 14:04:46 +08:00
|
|
|
if (ngx_buf_special(cl->buf)) {
|
2003-11-26 04:44:56 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2004-06-01 14:04:46 +08:00
|
|
|
if (!ngx_buf_in_memory_only(cl->buf)) {
|
2003-11-26 04:44:56 +08:00
|
|
|
break;
|
|
|
|
}
|
2003-05-29 21:02:09 +08:00
|
|
|
|
2004-06-21 23:59:32 +08:00
|
|
|
size = cl->buf->last - cl->buf->pos;
|
|
|
|
|
|
|
|
if (send + size > limit) {
|
|
|
|
size = limit - send;
|
|
|
|
}
|
|
|
|
|
2004-06-01 14:04:46 +08:00
|
|
|
if (prev == cl->buf->pos) {
|
2004-12-03 02:40:46 +08:00
|
|
|
iov->iov_len += (size_t) size;
|
2003-05-29 21:02:09 +08:00
|
|
|
|
|
|
|
} else {
|
2004-09-17 00:10:13 +08:00
|
|
|
if (!(iov = ngx_array_push(&header))) {
|
|
|
|
return NGX_CHAIN_ERROR;
|
|
|
|
}
|
|
|
|
|
2004-06-01 14:04:46 +08:00
|
|
|
iov->iov_base = (void *) cl->buf->pos;
|
2004-12-03 02:40:46 +08:00
|
|
|
iov->iov_len = (size_t) size;
|
2003-05-29 21:02:09 +08:00
|
|
|
}
|
|
|
|
|
2004-12-03 02:40:46 +08:00
|
|
|
prev = cl->buf->pos + (size_t) size;
|
2004-06-21 23:59:32 +08:00
|
|
|
send += size;
|
2003-11-26 04:44:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* set TCP_CORK if there is a header before a file */
|
|
|
|
|
2004-06-01 14:04:46 +08:00
|
|
|
if (c->tcp_nopush == NGX_TCP_NOPUSH_UNSET
|
2003-11-26 04:44:56 +08:00
|
|
|
&& header.nelts != 0
|
|
|
|
&& cl
|
2004-06-01 14:04:46 +08:00
|
|
|
&& cl->buf->in_file)
|
2003-11-26 04:44:56 +08:00
|
|
|
{
|
|
|
|
|
2004-12-03 02:40:46 +08:00
|
|
|
/* the TCP_CORK and TCP_NODELAY are mutually exclusive */
|
2003-05-29 21:02:09 +08:00
|
|
|
|
2004-12-03 02:40:46 +08:00
|
|
|
if (c->tcp_nodelay) {
|
|
|
|
|
|
|
|
tcp_nodelay = 0;
|
|
|
|
|
|
|
|
if (setsockopt(c->fd, IPPROTO_TCP, TCP_NODELAY,
|
|
|
|
(const void *) &tcp_nodelay, sizeof(int)) == -1)
|
|
|
|
{
|
|
|
|
err = ngx_errno;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* there is a tiny chance to be interrupted, however
|
|
|
|
* we continue a processing with the TCP_NODELAY
|
|
|
|
* and without the TCP_CORK
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (err != NGX_EINTR) {
|
|
|
|
wev->error = 1;
|
|
|
|
ngx_connection_error(c, ngx_socket_errno,
|
|
|
|
"setsockopt(TCP_NODELAY) failed");
|
|
|
|
return NGX_CHAIN_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
c->tcp_nodelay = 0;
|
|
|
|
|
|
|
|
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
|
|
|
|
"no tcp_nodelay");
|
2004-02-09 15:46:43 +08:00
|
|
|
}
|
2004-12-03 02:40:46 +08:00
|
|
|
}
|
2004-02-09 15:46:43 +08:00
|
|
|
|
2004-12-03 02:40:46 +08:00
|
|
|
if (!c->tcp_nodelay) {
|
2004-06-01 14:04:46 +08:00
|
|
|
|
2004-12-03 02:40:46 +08:00
|
|
|
if (ngx_tcp_nopush(c->fd) == NGX_ERROR) {
|
|
|
|
err = ngx_errno;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* there is a tiny chance to be interrupted, however
|
|
|
|
* we continue a processing without the TCP_CORK
|
|
|
|
*/
|
|
|
|
|
|
|
|
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 = NGX_TCP_NOPUSH_SET;
|
|
|
|
|
|
|
|
ngx_log_debug0(NGX_LOG_DEBUG_EVENT, c->log, 0,
|
|
|
|
"tcp_nopush");
|
|
|
|
}
|
2003-11-26 04:44:56 +08:00
|
|
|
}
|
2003-05-29 21:02:09 +08:00
|
|
|
}
|
|
|
|
|
2004-06-21 23:59:32 +08:00
|
|
|
/* get the file buf */
|
2003-05-29 21:02:09 +08:00
|
|
|
|
2004-06-21 23:59:32 +08:00
|
|
|
if (header.nelts == 0 && cl && cl->buf->in_file && send < limit) {
|
2004-06-01 14:04:46 +08:00
|
|
|
file = cl->buf;
|
2003-05-29 21:02:09 +08:00
|
|
|
|
2004-06-01 14:04:46 +08:00
|
|
|
/* coalesce the neighbouring file bufs */
|
2003-05-29 21:02:09 +08:00
|
|
|
|
2004-06-21 23:59:32 +08:00
|
|
|
do {
|
2004-12-03 02:40:46 +08:00
|
|
|
size = cl->buf->file_last - cl->buf->file_pos;
|
2004-06-21 23:59:32 +08:00
|
|
|
|
|
|
|
if (send + size > limit) {
|
|
|
|
size = limit - send;
|
|
|
|
|
|
|
|
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-11-26 04:44:56 +08:00
|
|
|
}
|
2003-05-29 21:02:09 +08:00
|
|
|
|
2004-12-03 02:40:46 +08:00
|
|
|
file_size += (size_t) size;
|
2004-06-21 23:59:32 +08:00
|
|
|
send += size;
|
|
|
|
fprev = cl->buf->file_pos + size;
|
2003-11-26 04:44:56 +08:00
|
|
|
cl = cl->next;
|
2003-05-29 21:02:09 +08:00
|
|
|
|
2004-06-21 23:59:32 +08:00
|
|
|
} while (cl
|
|
|
|
&& cl->buf->in_file
|
|
|
|
&& send < limit
|
|
|
|
&& file->file->fd == cl->buf->file->fd
|
|
|
|
&& fprev == cl->buf->file_pos);
|
|
|
|
}
|
2003-05-29 21:02:09 +08:00
|
|
|
|
2004-06-21 23:59:32 +08:00
|
|
|
if (file) {
|
2004-10-21 23:34:38 +08:00
|
|
|
#if (NGX_HAVE_SENDFILE64)
|
2003-11-26 23:42:18 +08:00
|
|
|
offset = file->file_pos;
|
2003-11-28 03:01:37 +08:00
|
|
|
#else
|
|
|
|
offset = (int32_t) file->file_pos;
|
|
|
|
#endif
|
2004-12-03 02:40:46 +08:00
|
|
|
rc = sendfile(c->fd, file->file->fd, &offset, file_size);
|
2003-05-29 21:02:09 +08:00
|
|
|
|
2003-11-26 04:44:56 +08:00
|
|
|
if (rc == -1) {
|
|
|
|
err = ngx_errno;
|
2003-05-29 21:02:09 +08:00
|
|
|
|
2004-02-09 15:46:43 +08:00
|
|
|
if (err == NGX_EAGAIN || err == NGX_EINTR) {
|
|
|
|
if (err == NGX_EINTR) {
|
|
|
|
eintr = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ngx_log_debug0(NGX_LOG_DEBUG_EVENT, c->log, err,
|
|
|
|
"sendfile() is not ready");
|
2003-05-29 21:02:09 +08:00
|
|
|
|
2003-11-26 04:44:56 +08:00
|
|
|
} else {
|
2004-02-09 15:46:43 +08:00
|
|
|
wev->error = 1;
|
|
|
|
ngx_connection_error(c, err, "sendfile() failed");
|
2003-11-26 04:44:56 +08:00
|
|
|
return NGX_CHAIN_ERROR;
|
|
|
|
}
|
2003-05-29 21:02:09 +08:00
|
|
|
}
|
|
|
|
|
2003-11-26 04:44:56 +08:00
|
|
|
sent = rc > 0 ? rc : 0;
|
2003-05-29 21:02:09 +08:00
|
|
|
|
2004-02-09 15:46:43 +08:00
|
|
|
ngx_log_debug4(NGX_LOG_DEBUG_EVENT, c->log, 0,
|
2004-12-03 02:40:46 +08:00
|
|
|
"sendfile: %d, @%O %O:%uz",
|
|
|
|
rc, file->file_pos, sent, file_size);
|
2004-02-09 15:46:43 +08:00
|
|
|
|
2003-11-26 04:44:56 +08:00
|
|
|
} else {
|
|
|
|
rc = writev(c->fd, header.elts, header.nelts);
|
2003-05-29 21:02:09 +08:00
|
|
|
|
2003-11-26 04:44:56 +08:00
|
|
|
if (rc == -1) {
|
|
|
|
err = ngx_errno;
|
2003-05-29 21:02:09 +08:00
|
|
|
|
2004-02-09 15:46:43 +08:00
|
|
|
if (err == NGX_EAGAIN || err == NGX_EINTR) {
|
|
|
|
if (err == NGX_EINTR) {
|
|
|
|
eintr = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ngx_log_debug0(NGX_LOG_DEBUG_EVENT, c->log, err,
|
|
|
|
"writev() not ready");
|
2003-05-29 21:02:09 +08:00
|
|
|
|
2003-11-26 04:44:56 +08:00
|
|
|
} else {
|
2004-02-09 15:46:43 +08:00
|
|
|
wev->error = 1;
|
|
|
|
ngx_connection_error(c, err, "writev() failed");
|
|
|
|
return NGX_CHAIN_ERROR;
|
2003-11-26 04:44:56 +08:00
|
|
|
}
|
2003-05-29 21:02:09 +08:00
|
|
|
}
|
|
|
|
|
2003-11-26 04:44:56 +08:00
|
|
|
sent = rc > 0 ? rc : 0;
|
2003-05-29 21:02:09 +08:00
|
|
|
|
2004-12-03 02:40:46 +08:00
|
|
|
ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, "writev: %O", sent);
|
2003-11-26 04:44:56 +08:00
|
|
|
}
|
2003-05-29 21:02:09 +08:00
|
|
|
|
2004-12-03 02:40:46 +08:00
|
|
|
if (send - prev_send == sent) {
|
2004-06-21 23:59:32 +08:00
|
|
|
complete = 1;
|
|
|
|
}
|
|
|
|
|
2003-11-26 04:44:56 +08:00
|
|
|
c->sent += sent;
|
2003-05-29 21:02:09 +08:00
|
|
|
|
2003-11-26 04:44:56 +08:00
|
|
|
for (cl = in; cl; cl = cl->next) {
|
2003-05-29 21:02:09 +08:00
|
|
|
|
2004-06-01 14:04:46 +08:00
|
|
|
if (ngx_buf_special(cl->buf)) {
|
2003-11-26 04:44:56 +08:00
|
|
|
continue;
|
|
|
|
}
|
2003-05-29 21:02:09 +08:00
|
|
|
|
2003-11-26 04:44:56 +08:00
|
|
|
if (sent == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2004-06-01 14:04:46 +08:00
|
|
|
size = ngx_buf_size(cl->buf);
|
2003-11-26 04:44:56 +08:00
|
|
|
|
|
|
|
if (sent >= size) {
|
|
|
|
sent -= size;
|
2003-05-29 21:02:09 +08:00
|
|
|
|
2004-06-01 14:04:46 +08:00
|
|
|
if (ngx_buf_in_memory(cl->buf)) {
|
|
|
|
cl->buf->pos = cl->buf->last;
|
2003-11-26 04:44:56 +08:00
|
|
|
}
|
|
|
|
|
2004-06-01 14:04:46 +08:00
|
|
|
if (cl->buf->in_file) {
|
|
|
|
cl->buf->file_pos = cl->buf->file_last;
|
2003-11-26 04:44:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
continue;
|
2003-05-29 21:02:09 +08:00
|
|
|
}
|
|
|
|
|
2004-06-01 14:04:46 +08:00
|
|
|
if (ngx_buf_in_memory(cl->buf)) {
|
2004-12-03 02:40:46 +08:00
|
|
|
cl->buf->pos += (size_t) sent;
|
2003-05-29 21:02:09 +08:00
|
|
|
}
|
|
|
|
|
2004-06-01 14:04:46 +08:00
|
|
|
if (cl->buf->in_file) {
|
|
|
|
cl->buf->file_pos += sent;
|
2003-11-26 04:44:56 +08:00
|
|
|
}
|
2003-05-29 21:02:09 +08:00
|
|
|
|
2003-11-26 04:44:56 +08:00
|
|
|
break;
|
2003-05-29 21:02:09 +08:00
|
|
|
}
|
|
|
|
|
2004-06-21 23:59:32 +08:00
|
|
|
if (eintr) {
|
|
|
|
continue;
|
|
|
|
}
|
2003-05-29 21:02:09 +08:00
|
|
|
|
2004-06-21 23:59:32 +08:00
|
|
|
if (!complete) {
|
|
|
|
wev->ready = 0;
|
|
|
|
return cl;
|
|
|
|
}
|
2003-05-29 21:02:09 +08:00
|
|
|
|
2004-06-21 23:59:32 +08:00
|
|
|
if (send >= limit || cl == NULL) {
|
|
|
|
return cl;
|
|
|
|
}
|
2003-11-26 04:44:56 +08:00
|
|
|
|
2004-06-21 23:59:32 +08:00
|
|
|
in = cl;
|
2003-11-26 04:44:56 +08:00
|
|
|
}
|
2003-05-29 21:02:09 +08:00
|
|
|
}
|