0
|
1
|
|
2 /*
|
|
3 * Copyright (C) Igor Sysoev
|
|
4 */
|
|
5
|
|
6
|
|
7 #include <ngx_config.h>
|
|
8 #include <ngx_core.h>
|
|
9 #include <ngx_event.h>
|
|
10
|
|
11
|
|
12 /*
|
28
|
13 * Although FreeBSD sendfile() allows to pass a header and a trailer,
|
640
|
14 * it cannot send a header with a part of the file in one packet until
|
28
|
15 * FreeBSD 5.3. Besides, over the fast ethernet connection sendfile()
|
0
|
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.
|
|
19 *
|
28
|
20 * Threfore 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 of
|
|
22 * the file in one packet, but also sends the file pages in the full packets.
|
0
|
23 *
|
50
|
24 * But until FreeBSD 4.5 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
|
0
|
27 * for non-keepalive HTTP connections.
|
|
28 */
|
|
29
|
|
30
|
146
|
31 #if (IOV_MAX > 64)
|
|
32 #define NGX_HEADERS 64
|
|
33 #define NGX_TRAILERS 64
|
|
34 #else
|
|
35 #define NGX_HEADERS IOV_MAX
|
|
36 #define NGX_TRAILERS IOV_MAX
|
|
37 #endif
|
0
|
38
|
|
39
|
50
|
40 ngx_chain_t *
|
|
41 ngx_freebsd_sendfile_chain(ngx_connection_t *c, ngx_chain_t *in, off_t limit)
|
0
|
42 {
|
520
|
43 int rc, flags;
|
0
|
44 u_char *prev;
|
22
|
45 off_t size, send, prev_send, aligned, sent, fprev;
|
|
46 size_t header_size, file_size;
|
0
|
47 ngx_uint_t eintr, eagain, complete;
|
|
48 ngx_err_t err;
|
|
49 ngx_buf_t *file;
|
|
50 ngx_array_t header, trailer;
|
|
51 ngx_event_t *wev;
|
|
52 ngx_chain_t *cl;
|
|
53 struct sf_hdtr hdtr;
|
|
54 struct iovec *iov, headers[NGX_HEADERS], trailers[NGX_TRAILERS];
|
|
55
|
|
56 wev = c->write;
|
|
57
|
|
58 if (!wev->ready) {
|
|
59 return in;
|
|
60 }
|
|
61
|
18
|
62 #if (NGX_HAVE_KQUEUE)
|
0
|
63
|
136
|
64 if ((ngx_event_flags & NGX_USE_KQUEUE_EVENT) && wev->pending_eof) {
|
86
|
65 (void) ngx_connection_error(c, wev->kq_errno,
|
|
66 "kevent() reported about an closed connection");
|
0
|
67 wev->error = 1;
|
|
68 return NGX_CHAIN_ERROR;
|
|
69 }
|
|
70
|
|
71 #endif
|
|
72
|
22
|
73 /* the maximum limit size is the maximum size_t value - the page size */
|
|
74
|
324
|
75 if (limit == 0 || limit > (off_t) (NGX_MAX_SIZE_T_VALUE - ngx_pagesize)) {
|
26
|
76 limit = NGX_MAX_SIZE_T_VALUE - ngx_pagesize;
|
22
|
77 }
|
|
78
|
0
|
79 send = 0;
|
|
80 eagain = 0;
|
520
|
81 flags = 0;
|
0
|
82
|
|
83 header.elts = headers;
|
|
84 header.size = sizeof(struct iovec);
|
|
85 header.nalloc = NGX_HEADERS;
|
|
86 header.pool = c->pool;
|
|
87
|
|
88 trailer.elts = trailers;
|
|
89 trailer.size = sizeof(struct iovec);
|
|
90 trailer.nalloc = NGX_TRAILERS;
|
|
91 trailer.pool = c->pool;
|
|
92
|
|
93 for ( ;; ) {
|
|
94 file = NULL;
|
22
|
95 file_size = 0;
|
|
96 header_size = 0;
|
0
|
97 eintr = 0;
|
|
98 complete = 0;
|
22
|
99 prev_send = send;
|
0
|
100
|
|
101 header.nelts = 0;
|
|
102 trailer.nelts = 0;
|
|
103
|
|
104 /* create the header iovec and coalesce the neighbouring bufs */
|
|
105
|
|
106 prev = NULL;
|
|
107 iov = NULL;
|
|
108
|
|
109 for (cl = in;
|
|
110 cl && header.nelts < IOV_MAX && send < limit;
|
|
111 cl = cl->next)
|
|
112 {
|
|
113 if (ngx_buf_special(cl->buf)) {
|
|
114 continue;
|
|
115 }
|
|
116
|
|
117 if (!ngx_buf_in_memory_only(cl->buf)) {
|
|
118 break;
|
|
119 }
|
|
120
|
|
121 size = cl->buf->last - cl->buf->pos;
|
|
122
|
|
123 if (send + size > limit) {
|
|
124 size = limit - send;
|
|
125 }
|
|
126
|
|
127 if (prev == cl->buf->pos) {
|
22
|
128 iov->iov_len += (size_t) size;
|
0
|
129
|
|
130 } else {
|
50
|
131 iov = ngx_array_push(&header);
|
|
132 if (iov == NULL) {
|
0
|
133 return NGX_CHAIN_ERROR;
|
|
134 }
|
|
135
|
|
136 iov->iov_base = (void *) cl->buf->pos;
|
22
|
137 iov->iov_len = (size_t) size;
|
0
|
138 }
|
|
139
|
22
|
140 prev = cl->buf->pos + (size_t) size;
|
|
141 header_size += (size_t) size;
|
0
|
142 send += size;
|
|
143 }
|
|
144
|
|
145
|
|
146 if (cl && cl->buf->in_file && send < limit) {
|
|
147 file = cl->buf;
|
|
148
|
|
149 /* coalesce the neighbouring file bufs */
|
|
150
|
|
151 do {
|
22
|
152 size = cl->buf->file_last - cl->buf->file_pos;
|
0
|
153
|
|
154 if (send + size > limit) {
|
|
155 size = limit - send;
|
|
156
|
|
157 aligned = (cl->buf->file_pos + size + ngx_pagesize - 1)
|
298
|
158 & ~((off_t) ngx_pagesize - 1);
|
0
|
159
|
|
160 if (aligned <= cl->buf->file_last) {
|
|
161 size = aligned - cl->buf->file_pos;
|
|
162 }
|
|
163 }
|
|
164
|
22
|
165 file_size += (size_t) size;
|
0
|
166 send += size;
|
|
167 fprev = cl->buf->file_pos + size;
|
|
168 cl = cl->next;
|
|
169
|
|
170 } while (cl
|
|
171 && cl->buf->in_file
|
|
172 && send < limit
|
|
173 && file->file->fd == cl->buf->file->fd
|
|
174 && fprev == cl->buf->file_pos);
|
|
175 }
|
|
176
|
4
|
177
|
0
|
178 if (file) {
|
4
|
179
|
0
|
180 /* create the tailer iovec and coalesce the neighbouring bufs */
|
|
181
|
|
182 prev = NULL;
|
|
183 iov = NULL;
|
|
184
|
4
|
185 while (cl && header.nelts < IOV_MAX && send < limit) {
|
|
186
|
0
|
187 if (ngx_buf_special(cl->buf)) {
|
4
|
188 cl = cl->next;
|
0
|
189 continue;
|
|
190 }
|
|
191
|
|
192 if (!ngx_buf_in_memory_only(cl->buf)) {
|
|
193 break;
|
|
194 }
|
|
195
|
|
196 size = cl->buf->last - cl->buf->pos;
|
|
197
|
|
198 if (send + size > limit) {
|
|
199 size = limit - send;
|
|
200 }
|
|
201
|
|
202 if (prev == cl->buf->pos) {
|
22
|
203 iov->iov_len += (size_t) size;
|
0
|
204
|
|
205 } else {
|
50
|
206 iov = ngx_array_push(&trailer);
|
|
207 if (iov == NULL) {
|
0
|
208 return NGX_CHAIN_ERROR;
|
|
209 }
|
|
210
|
|
211 iov->iov_base = (void *) cl->buf->pos;
|
22
|
212 iov->iov_len = (size_t) size;
|
0
|
213 }
|
|
214
|
22
|
215 prev = cl->buf->pos + (size_t) size;
|
0
|
216 send += size;
|
4
|
217 cl = cl->next;
|
0
|
218 }
|
|
219 }
|
|
220
|
|
221 if (file) {
|
|
222
|
|
223 if (ngx_freebsd_use_tcp_nopush
|
|
224 && c->tcp_nopush == NGX_TCP_NOPUSH_UNSET)
|
|
225 {
|
|
226 if (ngx_tcp_nopush(c->fd) == NGX_ERROR) {
|
|
227 err = ngx_errno;
|
|
228
|
|
229 /*
|
28
|
230 * there is a tiny chance to be interrupted, however,
|
0
|
231 * we continue a processing without the TCP_NOPUSH
|
|
232 */
|
|
233
|
|
234 if (err != NGX_EINTR) {
|
|
235 wev->error = 1;
|
86
|
236 (void) ngx_connection_error(c, err,
|
|
237 ngx_tcp_nopush_n " failed");
|
0
|
238 return NGX_CHAIN_ERROR;
|
|
239 }
|
|
240
|
|
241 } else {
|
|
242 c->tcp_nopush = NGX_TCP_NOPUSH_SET;
|
|
243
|
|
244 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, c->log, 0,
|
|
245 "tcp_nopush");
|
|
246 }
|
|
247 }
|
|
248
|
654
|
249 /*
|
|
250 * sendfile() does unneeded work if sf_hdtr's count is 0,
|
|
251 * but corresponding pointer is not NULL
|
|
252 */
|
|
253
|
|
254 hdtr.headers = header.nelts ? (struct iovec *) header.elts: NULL;
|
0
|
255 hdtr.hdr_cnt = header.nelts;
|
654
|
256 hdtr.trailers = trailer.nelts ? (struct iovec *) trailer.elts: NULL;
|
0
|
257 hdtr.trl_cnt = trailer.nelts;
|
|
258
|
|
259 /*
|
|
260 * the "nbytes bug" of the old sendfile() syscall:
|
|
261 * http://www.freebsd.org/cgi/query-pr.cgi?pr=33771
|
|
262 */
|
|
263
|
28
|
264 if (!ngx_freebsd_sendfile_nbytes_bug) {
|
22
|
265 header_size = 0;
|
0
|
266 }
|
|
267
|
|
268 sent = 0;
|
|
269
|
520
|
270 #if (NGX_HAVE_AIO_SENDFILE)
|
|
271 flags = c->aio_sendfile ? SF_NODISKIO : 0;
|
|
272 #endif
|
|
273
|
0
|
274 rc = sendfile(file->file->fd, c->fd, file->file_pos,
|
520
|
275 file_size + header_size, &hdtr, &sent, flags);
|
0
|
276
|
|
277 if (rc == -1) {
|
|
278 err = ngx_errno;
|
|
279
|
520
|
280 switch (err) {
|
|
281 case NGX_EAGAIN:
|
|
282 eagain = 1;
|
|
283 break;
|
0
|
284
|
520
|
285 case NGX_EINTR:
|
|
286 eintr = 1;
|
|
287 break;
|
0
|
288
|
520
|
289 #if (NGX_HAVE_AIO_SENDFILE)
|
|
290 case NGX_EBUSY:
|
|
291 c->busy_sendfile = file;
|
|
292 break;
|
|
293 #endif
|
0
|
294
|
520
|
295 default:
|
0
|
296 wev->error = 1;
|
86
|
297 (void) ngx_connection_error(c, err, "sendfile() failed");
|
0
|
298 return NGX_CHAIN_ERROR;
|
|
299 }
|
520
|
300
|
|
301 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, err,
|
|
302 "sendfile() sent only %O bytes", sent);
|
0
|
303
|
32
|
304 /*
|
|
305 * sendfile() in FreeBSD 3.x-4.x may return value >= 0
|
|
306 * on success, although only 0 is documented
|
|
307 */
|
|
308
|
524
|
309 } else if (rc >= 0 && sent == 0) {
|
4
|
310
|
|
311 /*
|
32
|
312 * if rc is OK and sent equal to zero, then someone
|
|
313 * has truncated the file, so the offset became beyond
|
|
314 * the end of the file
|
4
|
315 */
|
|
316
|
|
317 ngx_log_error(NGX_LOG_ALERT, c->log, 0,
|
524
|
318 "sendfile() reported that \"%s\" was truncated at %O",
|
|
319 file->file->name.data, file->file_pos);
|
4
|
320
|
|
321 return NGX_CHAIN_ERROR;
|
|
322 }
|
|
323
|
0
|
324 ngx_log_debug4(NGX_LOG_DEBUG_EVENT, c->log, 0,
|
10
|
325 "sendfile: %d, @%O %O:%uz",
|
22
|
326 rc, file->file_pos, sent, file_size + header_size);
|
0
|
327
|
|
328 } else {
|
|
329 rc = writev(c->fd, header.elts, header.nelts);
|
|
330
|
|
331 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, c->log, 0,
|
22
|
332 "writev: %d of %uz", rc, header_size);
|
0
|
333
|
|
334 if (rc == -1) {
|
|
335 err = ngx_errno;
|
|
336
|
520
|
337 switch (err) {
|
|
338 case NGX_EAGAIN:
|
|
339 break;
|
0
|
340
|
520
|
341 case NGX_EINTR:
|
|
342 eintr = 1;
|
|
343 break;
|
0
|
344
|
520
|
345 default:
|
0
|
346 wev->error = 1;
|
|
347 ngx_connection_error(c, err, "writev() failed");
|
|
348 return NGX_CHAIN_ERROR;
|
|
349 }
|
520
|
350
|
|
351 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, c->log, err,
|
|
352 "writev() not ready");
|
0
|
353 }
|
|
354
|
|
355 sent = rc > 0 ? rc : 0;
|
|
356 }
|
|
357
|
22
|
358 if (send - prev_send == sent) {
|
0
|
359 complete = 1;
|
|
360 }
|
|
361
|
|
362 c->sent += sent;
|
|
363
|
|
364 for (cl = in; cl; cl = cl->next) {
|
|
365
|
|
366 if (ngx_buf_special(cl->buf)) {
|
|
367 continue;
|
|
368 }
|
|
369
|
|
370 if (sent == 0) {
|
|
371 break;
|
|
372 }
|
|
373
|
|
374 size = ngx_buf_size(cl->buf);
|
|
375
|
|
376 if (sent >= size) {
|
|
377 sent -= size;
|
|
378
|
|
379 if (ngx_buf_in_memory(cl->buf)) {
|
|
380 cl->buf->pos = cl->buf->last;
|
|
381 }
|
|
382
|
|
383 if (cl->buf->in_file) {
|
|
384 cl->buf->file_pos = cl->buf->file_last;
|
|
385 }
|
|
386
|
|
387 continue;
|
|
388 }
|
|
389
|
|
390 if (ngx_buf_in_memory(cl->buf)) {
|
22
|
391 cl->buf->pos += (size_t) sent;
|
0
|
392 }
|
|
393
|
|
394 if (cl->buf->in_file) {
|
|
395 cl->buf->file_pos += sent;
|
|
396 }
|
|
397
|
|
398 break;
|
|
399 }
|
|
400
|
520
|
401 #if (NGX_HAVE_AIO_SENDFILE)
|
|
402 if (c->busy_sendfile) {
|
|
403 return cl;
|
|
404 }
|
|
405 #endif
|
|
406
|
0
|
407 if (eagain) {
|
|
408
|
|
409 /*
|
28
|
410 * sendfile() may return EAGAIN, even if it has sent a whole file
|
|
411 * part, it indicates that the successive sendfile() call would
|
0
|
412 * return EAGAIN right away and would not send anything.
|
|
413 * We use it as a hint.
|
|
414 */
|
|
415
|
|
416 wev->ready = 0;
|
|
417 return cl;
|
|
418 }
|
|
419
|
|
420 if (eintr) {
|
|
421 continue;
|
|
422 }
|
|
423
|
|
424 if (!complete) {
|
|
425 wev->ready = 0;
|
|
426 return cl;
|
|
427 }
|
|
428
|
|
429 if (send >= limit || cl == NULL) {
|
|
430 return cl;
|
|
431 }
|
|
432
|
|
433 in = cl;
|
|
434 }
|
|
435 }
|