nginx 0.1.11
[nginx-catap.git] / src / os / unix / ngx_freebsd_sendfile_chain.c
blob187365cb2832e88b743fb4c8a235d1e49320d6fc
2 /*
3 * Copyright (C) Igor Sysoev
4 */
7 #include <ngx_config.h>
8 #include <ngx_core.h>
9 #include <ngx_event.h>
13 * Although FreeBSD sendfile() allows to pass a header and a trailer
14 * it can not send a header with a part of the file in one packet until
15 * FreeBSD 5.3. Besides over the fast ethernet connection sendfile()
16 * may send the partially filled packets, i.e. the 8 file pages may be sent
17 * as the 11 full 1460-bytes packets, then one incomplete 324-bytes packet,
18 * and then again the 11 full 1460-bytes packets.
20 * So we use the TCP_NOPUSH option (similar to Linux's TCP_CORK)
21 * to postpone the sending - it not only sends a header and the first part
22 * of the file in one packet but also sends the file pages in the full packets.
24 * But until FreeBSD 4.5 the turning TCP_NOPUSH off does not flush a pending
25 * data that less than MSS so that data may be sent with 5 second delay.
26 * So we do not use TCP_NOPUSH on FreeBSD prior to 4.5 although it can be used
27 * for non-keepalive HTTP connections.
31 #define NGX_HEADERS 8
32 #define NGX_TRAILERS 4
35 ngx_chain_t *ngx_freebsd_sendfile_chain(ngx_connection_t *c, ngx_chain_t *in,
36 off_t limit)
38 int rc;
39 u_char *prev;
40 off_t size, send, prev_send, aligned, sent, fprev;
41 size_t header_size, file_size;
42 ngx_uint_t eintr, eagain, complete;
43 ngx_err_t err;
44 ngx_buf_t *file;
45 ngx_array_t header, trailer;
46 ngx_event_t *wev;
47 ngx_chain_t *cl;
48 struct sf_hdtr hdtr;
49 struct iovec *iov, headers[NGX_HEADERS], trailers[NGX_TRAILERS];
51 wev = c->write;
53 if (!wev->ready) {
54 return in;
57 #if (NGX_HAVE_KQUEUE)
59 if ((ngx_event_flags & NGX_USE_KQUEUE_EVENT) && wev->pending_eof) {
60 ngx_log_error(NGX_LOG_INFO, c->log, wev->kq_errno,
61 "kevent() reported about an closed connection");
63 wev->error = 1;
64 return NGX_CHAIN_ERROR;
67 #endif
69 /* the maximum limit size is the maximum size_t value - the page size */
71 if (limit == 0 || limit > MAX_SIZE_T_VALUE - ngx_pagesize) {
72 limit = MAX_SIZE_T_VALUE - ngx_pagesize;
75 send = 0;
76 eagain = 0;
78 header.elts = headers;
79 header.size = sizeof(struct iovec);
80 header.nalloc = NGX_HEADERS;
81 header.pool = c->pool;
83 trailer.elts = trailers;
84 trailer.size = sizeof(struct iovec);
85 trailer.nalloc = NGX_TRAILERS;
86 trailer.pool = c->pool;
88 for ( ;; ) {
89 file = NULL;
90 file_size = 0;
91 header_size = 0;
92 eintr = 0;
93 complete = 0;
94 prev_send = send;
96 header.nelts = 0;
97 trailer.nelts = 0;
99 /* create the header iovec and coalesce the neighbouring bufs */
101 prev = NULL;
102 iov = NULL;
104 for (cl = in;
105 cl && header.nelts < IOV_MAX && send < limit;
106 cl = cl->next)
108 if (ngx_buf_special(cl->buf)) {
109 continue;
112 if (!ngx_buf_in_memory_only(cl->buf)) {
113 break;
116 size = cl->buf->last - cl->buf->pos;
118 if (send + size > limit) {
119 size = limit - send;
122 if (prev == cl->buf->pos) {
123 iov->iov_len += (size_t) size;
125 } else {
126 if (!(iov = ngx_array_push(&header))) {
127 return NGX_CHAIN_ERROR;
130 iov->iov_base = (void *) cl->buf->pos;
131 iov->iov_len = (size_t) size;
134 prev = cl->buf->pos + (size_t) size;
135 header_size += (size_t) size;
136 send += size;
140 if (cl && cl->buf->in_file && send < limit) {
141 file = cl->buf;
143 /* coalesce the neighbouring file bufs */
145 do {
146 size = cl->buf->file_last - cl->buf->file_pos;
148 if (send + size > limit) {
149 size = limit - send;
151 aligned = (cl->buf->file_pos + size + ngx_pagesize - 1)
152 & ~(ngx_pagesize - 1);
154 if (aligned <= cl->buf->file_last) {
155 size = aligned - cl->buf->file_pos;
159 file_size += (size_t) size;
160 send += size;
161 fprev = cl->buf->file_pos + size;
162 cl = cl->next;
164 } while (cl
165 && cl->buf->in_file
166 && send < limit
167 && file->file->fd == cl->buf->file->fd
168 && fprev == cl->buf->file_pos);
172 if (file) {
174 /* create the tailer iovec and coalesce the neighbouring bufs */
176 prev = NULL;
177 iov = NULL;
179 while (cl && header.nelts < IOV_MAX && send < limit) {
181 if (ngx_buf_special(cl->buf)) {
182 cl = cl->next;
183 continue;
186 if (!ngx_buf_in_memory_only(cl->buf)) {
187 break;
190 size = cl->buf->last - cl->buf->pos;
192 if (send + size > limit) {
193 size = limit - send;
196 if (prev == cl->buf->pos) {
197 iov->iov_len += (size_t) size;
199 } else {
200 if (!(iov = ngx_array_push(&trailer))) {
201 return NGX_CHAIN_ERROR;
204 iov->iov_base = (void *) cl->buf->pos;
205 iov->iov_len = (size_t) size;
208 prev = cl->buf->pos + (size_t) size;
209 send += size;
210 cl = cl->next;
214 if (file) {
216 if (ngx_freebsd_use_tcp_nopush
217 && c->tcp_nopush == NGX_TCP_NOPUSH_UNSET)
219 if (ngx_tcp_nopush(c->fd) == NGX_ERROR) {
220 err = ngx_errno;
223 * there is a tiny chance to be interrupted, however
224 * we continue a processing without the TCP_NOPUSH
227 if (err != NGX_EINTR) {
228 wev->error = 1;
229 ngx_connection_error(c, err,
230 ngx_tcp_nopush_n " failed");
231 return NGX_CHAIN_ERROR;
234 } else {
235 c->tcp_nopush = NGX_TCP_NOPUSH_SET;
237 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, c->log, 0,
238 "tcp_nopush");
242 hdtr.headers = (struct iovec *) header.elts;
243 hdtr.hdr_cnt = header.nelts;
244 hdtr.trailers = (struct iovec *) trailer.elts;
245 hdtr.trl_cnt = trailer.nelts;
248 * the "nbytes bug" of the old sendfile() syscall:
249 * http://www.freebsd.org/cgi/query-pr.cgi?pr=33771
252 if (ngx_freebsd_sendfile_nbytes_bug == 0) {
253 header_size = 0;
256 sent = 0;
258 rc = sendfile(file->file->fd, c->fd, file->file_pos,
259 file_size + header_size, &hdtr, &sent, 0);
261 if (rc == -1) {
262 err = ngx_errno;
264 if (err == NGX_EAGAIN || err == NGX_EINTR) {
265 if (err == NGX_EINTR) {
266 eintr = 1;
268 } else {
269 eagain = 1;
272 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, err,
273 "sendfile() sent only %O bytes", sent);
275 } else {
276 wev->error = 1;
277 ngx_connection_error(c, err, "sendfile() failed");
278 return NGX_CHAIN_ERROR;
282 if (rc == 0 && sent == 0) {
285 * rc and sent are equals to zero when someone has truncated
286 * the file, so the offset became beyond the end of the file
289 ngx_log_error(NGX_LOG_ALERT, c->log, 0,
290 "sendfile() reported that \"%s\" was truncated",
291 file->file->name.data);
293 return NGX_CHAIN_ERROR;
296 ngx_log_debug4(NGX_LOG_DEBUG_EVENT, c->log, 0,
297 "sendfile: %d, @%O %O:%uz",
298 rc, file->file_pos, sent, file_size + header_size);
300 } else {
301 rc = writev(c->fd, header.elts, header.nelts);
303 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, c->log, 0,
304 "writev: %d of %uz", rc, header_size);
306 if (rc == -1) {
307 err = ngx_errno;
309 if (err == NGX_EAGAIN || err == NGX_EINTR) {
310 if (err == NGX_EINTR) {
311 eintr = 1;
314 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, c->log, err,
315 "writev() not ready");
317 } else {
318 wev->error = 1;
319 ngx_connection_error(c, err, "writev() failed");
320 return NGX_CHAIN_ERROR;
324 sent = rc > 0 ? rc : 0;
327 if (send - prev_send == sent) {
328 complete = 1;
331 c->sent += sent;
333 for (cl = in; cl; cl = cl->next) {
335 if (ngx_buf_special(cl->buf)) {
336 continue;
339 if (sent == 0) {
340 break;
343 size = ngx_buf_size(cl->buf);
345 if (sent >= size) {
346 sent -= size;
348 if (ngx_buf_in_memory(cl->buf)) {
349 cl->buf->pos = cl->buf->last;
352 if (cl->buf->in_file) {
353 cl->buf->file_pos = cl->buf->file_last;
356 continue;
359 if (ngx_buf_in_memory(cl->buf)) {
360 cl->buf->pos += (size_t) sent;
363 if (cl->buf->in_file) {
364 cl->buf->file_pos += sent;
367 break;
370 if (eagain) {
373 * sendfile() can return EAGAIN even if it has sent
374 * a whole file part but the successive sendfile() call would
375 * return EAGAIN right away and would not send anything.
376 * We use it as a hint.
379 wev->ready = 0;
380 return cl;
383 if (eintr) {
384 continue;
387 if (!complete) {
388 wev->ready = 0;
389 return cl;
392 if (send >= limit || cl == NULL) {
393 return cl;
396 in = cl;