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