23
24
#include <common/config.h>
24
25
#include <common/debug.h>
25
26
#include <common/standard.h>
27
#include <common/ticks.h>
26
28
#include <common/time.h>
28
#include <types/buffers.h>
29
#include <types/global.h>
30
#include <types/polling.h>
30
#include <proto/buffers.h>
32
31
#include <proto/client.h>
33
32
#include <proto/fd.h>
33
#include <proto/pipe.h>
34
34
#include <proto/stream_sock.h>
35
35
#include <proto/task.h>
37
#include <types/global.h>
39
/* On recent Linux kernels, the splice() syscall may be used for faster data copy.
40
* But it's not always defined on some OS versions, and it even happens that some
41
* definitions are wrong with some glibc due to an offset bug in syscall().
44
#if defined(CONFIG_HAP_LINUX_SPLICE)
46
#include <sys/syscall.h>
49
#define SPLICE_F_MOVE 0x1
52
#ifndef SPLICE_F_NONBLOCK
53
#define SPLICE_F_NONBLOCK 0x2
57
#define SPLICE_F_MORE 0x4
61
#if defined(__powerpc__) || defined(__powerpc64__)
62
#define __NR_splice 283
63
#elif defined(__sparc__) || defined(__sparc64__)
64
#define __NR_splice 232
65
#elif defined(__x86_64__)
66
#define __NR_splice 275
67
#elif defined(__alpha__)
68
#define __NR_splice 468
69
#elif defined (__i386__)
70
#define __NR_splice 313
72
#warning unsupported architecture, guessing __NR_splice=313 like x86...
73
#define __NR_splice 313
76
_syscall6(int, splice, int, fdin, loff_t *, off_in, int, fdout, loff_t *, off_out, size_t, len, unsigned long, flags)
78
#endif /* __NR_splice */
80
/* A pipe contains 16 segments max, and it's common to see segments of 1448 bytes
81
* because of timestamps. Use this as a hint for not looping on splice().
83
#define SPLICE_FULL_HINT 16*1448
86
* -1 if splice is not possible or not possible anymore and we must switch to
87
* user-land copy (eg: to_forward reached)
88
* 0 when we know that polling is required to get more data (EAGAIN)
89
* 1 for all other cases (we can safely try again, or if an activity has been
90
* detected (DATA/NULL/ERR))
94
* BF_WRITE_PARTIAL (during copy)
95
* BF_EMPTY (during copy)
100
* This function automatically allocates a pipe from the pipe pool. It also
101
* carefully ensures to clear b->pipe whenever it leaves the pipe empty.
103
static int stream_sock_splice_in(struct buffer *b, struct stream_interface *si)
106
int ret, max, total = 0;
112
if (!(b->flags & BF_KERN_SPLICING))
116
/* We're embarrassed, there are already data pending in
117
* the buffer and we don't want to have them at two
118
* locations at a time. Let's indicate we need some
119
* place and ask the consumer to hurry.
121
si->flags |= SI_FL_WAIT_ROOM;
122
EV_FD_CLR(fd, DIR_RD);
123
b->rex = TICK_ETERNITY;
124
b->cons->chk_snd(b->cons);
128
if (unlikely(b->pipe == NULL)) {
129
if (pipes_used >= global.maxpipes || !(b->pipe = get_pipe())) {
130
b->flags &= ~BF_KERN_SPLICING;
135
/* At this point, b->pipe is valid */
140
/* It looks like the buffer + the pipe already contain
141
* the maximum amount of data to be transferred. Try to
142
* send those data immediately on the other side if it
143
* is currently waiting.
145
retval = -1; /* end of forwarding */
149
ret = splice(fd, NULL, b->pipe->prod, NULL, max,
150
SPLICE_F_MOVE|SPLICE_F_NONBLOCK);
154
/* connection closed. This is only detected by
155
* recent kernels (>= 2.6.27.13).
157
b->flags |= BF_READ_NULL;
158
retval = 1; /* no need for further polling */
162
if (errno == EAGAIN) {
163
/* there are two reasons for EAGAIN :
164
* - nothing in the socket buffer (standard)
166
* - the connection is closed (kernel < 2.6.27.13)
167
* Since we don't know if pipe is full, we'll
168
* stop if the pipe is not empty. Anyway, we
169
* will almost always fill/empty the pipe.
173
si->flags |= SI_FL_WAIT_ROOM;
178
/* We don't know if the connection was closed.
179
* But if we're called upon POLLIN with an empty
180
* pipe and get EAGAIN, it is suspect enought to
181
* try to fall back to the normal recv scheme
182
* which will be able to deal with the situation.
187
/* here we have another error */
188
si->flags |= SI_FL_ERR;
193
b->to_forward -= ret;
196
b->pipe->data += ret;
197
b->flags |= BF_READ_PARTIAL;
198
b->flags &= ~BF_EMPTY; /* to prevent shutdowns */
200
if (b->pipe->data >= SPLICE_FULL_HINT ||
201
ret >= global.tune.recv_enough) {
202
/* We've read enough of it for this time. */
208
if (unlikely(!b->pipe->data)) {
216
#endif /* CONFIG_HAP_LINUX_SPLICE */
39
220
* this function is called on a read event from a stream socket.
168
449
} /* while (1) */
171
* The only way to get out of this loop is to have stopped reading
172
* without any error nor close, either by limiting the number of
173
* loops, or because of an EAGAIN. We only rearm the timer if we
174
* have at least read something.
452
/* We might have some data the consumer is waiting for */
453
if ((b->send_max || b->pipe) && (b->cons->flags & SI_FL_WAIT_DATA)) {
454
int last_len = b->pipe ? b->pipe->data : 0;
456
b->cons->chk_snd(b->cons);
458
/* check if the consumer has freed some space */
459
if (!(b->flags & BF_FULL) &&
460
(!last_len || !b->pipe || b->pipe->data < last_len))
461
si->flags &= ~SI_FL_WAIT_ROOM;
464
if (si->flags & SI_FL_WAIT_ROOM) {
465
EV_FD_CLR(fd, DIR_RD);
466
b->rex = TICK_ETERNITY;
468
else if ((b->flags & (BF_SHUTR|BF_READ_PARTIAL|BF_FULL|BF_READ_NOEXP)) == BF_READ_PARTIAL)
469
b->rex = tick_add_ifset(now_ms, b->rto);
471
/* we have to wake up if there is a special event or if we don't have
472
* any more data to forward.
177
if (b->flags & BF_PARTIAL_READ && tv_isset(&b->rex)) {
178
if (tv_add_ifset(&b->rex, &now, &b->rto))
181
tv_eternity(&b->rex);
185
if (b->flags & BF_READ_STATUS)
186
task_wakeup(fdtab[fd].owner);
474
if ((b->flags & (BF_READ_NULL|BF_READ_ERROR|BF_SHUTR|BF_READ_DONTWAIT)) ||
476
si->state != SI_ST_EST ||
477
b->cons->state != SI_ST_EST ||
478
(si->flags & SI_FL_ERR))
479
task_wakeup(si->owner, TASK_WOKEN_IO);
481
b->flags &= ~BF_READ_DONTWAIT;
187
482
fdtab[fd].ev &= ~FD_POLL_IN;
486
/* we received a shutdown */
191
487
fdtab[fd].ev &= ~FD_POLL_HUP;
192
488
b->flags |= BF_READ_NULL;
489
stream_sock_shutr(si);
196
/* There was an error. we must wakeup the task. No need to clear
197
* the events, the task will do it.
493
/* Read error on the file descriptor. We mark the FD as STERROR so
494
* that we don't use it anymore. The error is reported to the stream
495
* interface which will take proper action. We must not perturbate the
496
* buffer because the stream interface wants to ensure transparent
497
* connection retries.
199
500
fdtab[fd].state = FD_STERROR;
200
501
fdtab[fd].ev &= ~FD_POLL_STICKY;
201
b->flags |= BF_READ_ERROR;
503
si->flags |= SI_FL_ERR;
207
* this function is called on a write event from a stream socket.
208
* It returns 0 if we have a high confidence that we will not be
209
* able to write more data without polling first. Returns non-zero
510
* This function is called to send buffer data to a stream socket.
511
* It returns -1 in case of unrecoverable error, 0 if the caller needs to poll
512
* before calling it again, otherwise 1. If a pipe was associated with the
513
* buffer and it empties it, it releases it as well.
212
int stream_sock_write(int fd) {
213
__label__ out_eternity, out_wakeup, out_error;
214
struct buffer *b = fdtab[fd].cb[DIR_WR].b;
215
int ret, max, retval;
515
static int stream_sock_write_loop(struct stream_interface *si, struct buffer *b)
216
517
int write_poll = MAX_WRITE_POLL_LOOPS;
219
fprintf(stderr,"stream_sock_write : fd=%d, owner=%p\n", fd, fdtab[fd].owner);
521
#if defined(CONFIG_HAP_LINUX_SPLICE)
523
ret = splice(b->pipe->cons, NULL, si->fd, NULL, b->pipe->data,
524
SPLICE_F_MOVE|SPLICE_F_NONBLOCK);
526
if (ret == 0 || errno == EAGAIN) {
530
/* here we have another error */
535
b->flags |= BF_WRITE_PARTIAL;
536
b->pipe->data -= ret;
538
if (!b->pipe->data) {
544
if (--write_poll <= 0)
548
/* At this point, the pipe is empty, but we may still have data pending
549
* in the normal buffer.
552
b->flags |= BF_EMPTY;
223
if (fdtab[fd].state == FD_STERROR || (fdtab[fd].ev & FD_POLL_ERR))
559
/* when we're in this loop, we already know that there is no spliced
560
* data left, and that there are sendable buffered data.
227
if (b->l == 0) { /* let's realign the buffer to optimize I/O */
228
b->r = b->w = b->lr = b->data;
231
else if (b->r > b->w) {
232
564
max = b->r - b->w;
235
566
max = b->data + BUFSIZE - b->w;
239
/* may be we have received a connection acknowledgement in TCP mode without data */
240
if (likely(fdtab[fd].state == FD_STCONN)) {
241
/* We have no data to send to check the connection, and
242
* getsockopt() will not inform us whether the connection
243
* is still pending. So we'll reuse connect() to check the
244
* state of the socket. This has the advantage of givig us
245
* the following info :
247
* - connecting (EALREADY, EINPROGRESS)
248
* - connected (EISCONN, 0)
250
if ((connect(fd, fdtab[fd].peeraddr, fdtab[fd].peerlen) == 0))
253
if (errno == EALREADY || errno == EINPROGRESS) {
258
if (errno && errno != EISCONN)
261
/* OK we just need to indicate that we got a connection
262
* and that we wrote nothing.
264
b->flags |= BF_WRITE_NULL;
265
fdtab[fd].state = FD_STREADY;
268
/* Funny, we were called to write something but there wasn't
269
* anything. Theorically we cannot get there, but just in case,
270
* let's disable the write event and pretend we never came there.
272
EV_FD_CLR(fd, DIR_WR);
568
/* limit the amount of outgoing data if required */
569
if (max > b->send_max)
276
572
#ifndef MSG_NOSIGNAL
279
575
socklen_t lskerr = sizeof(skerr);
281
ret = getsockopt(fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr);
577
ret = getsockopt(si->fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr);
282
578
if (ret == -1 || skerr)
285
ret = send(fd, b->w, max, MSG_DONTWAIT);
581
ret = send(si->fd, b->w, max, MSG_DONTWAIT);
288
ret = send(fd, b->w, max, MSG_DONTWAIT | MSG_NOSIGNAL);
584
ret = send(si->fd, b->w, max, MSG_DONTWAIT | MSG_NOSIGNAL);
588
if (fdtab[si->fd].state == FD_STCONN)
589
fdtab[si->fd].state = FD_STREADY;
591
b->flags |= BF_WRITE_PARTIAL;
295
b->flags |= BF_PARTIAL_WRITE;
297
if (b->w == b->data + BUFSIZE) {
594
if (b->w == b->data + BUFSIZE)
298
595
b->w = b->data; /* wrap around the buffer */
302
EV_FD_CLR(fd, DIR_WR);
598
if (likely(b->l < b->max_len))
599
b->flags &= ~BF_FULL;
602
/* optimize data alignment in the buffer */
603
b->r = b->w = b->lr = b->data;
604
if (likely(!b->pipe))
605
b->flags |= BF_EMPTY;
609
if (!b->send_max || !b->l)
306
612
/* if the system buffer is full, don't insist */
313
619
else if (ret == 0 || errno == EAGAIN) {
314
/* nothing written, just pretend we were never called
315
* and wait for the socket to be ready. But we may have
316
* done some work justifying to notify the task.
620
/* nothing written, we need to poll for write first */
625
/* bad, we got an error */
636
* This function is called on a write event from a stream socket.
637
* It returns 0 if the caller needs to poll before calling it again, otherwise
640
int stream_sock_write(int fd)
642
struct stream_interface *si = fdtab[fd].owner;
643
struct buffer *b = si->ob;
647
fprintf(stderr,"stream_sock_write : fd=%d, owner=%p\n", fd, fdtab[fd].owner);
651
if (fdtab[fd].state == FD_STERROR || (fdtab[fd].ev & FD_POLL_ERR))
654
/* we might have been called just after an asynchronous shutw */
655
if (b->flags & BF_SHUTW)
658
if (likely(!(b->flags & BF_EMPTY))) {
659
/* OK there are data waiting to be sent */
660
retval = stream_sock_write_loop(si, b);
665
/* may be we have received a connection acknowledgement in TCP mode without data */
666
if (likely(fdtab[fd].state == FD_STCONN)) {
667
/* We have no data to send to check the connection, and
668
* getsockopt() will not inform us whether the connection
669
* is still pending. So we'll reuse connect() to check the
670
* state of the socket. This has the advantage of givig us
671
* the following info :
673
* - connecting (EALREADY, EINPROGRESS)
674
* - connected (EISCONN, 0)
676
if ((connect(fd, fdtab[fd].peeraddr, fdtab[fd].peerlen) == 0))
679
if (errno == EALREADY || errno == EINPROGRESS) {
684
if (errno && errno != EISCONN)
687
/* OK we just need to indicate that we got a connection
688
* and that we wrote nothing.
690
b->flags |= BF_WRITE_NULL;
691
fdtab[fd].state = FD_STREADY;
327
* The only way to get out of this loop is to have stopped writing
328
* without any error, either by limiting the number of loops, or
329
* because of an EAGAIN. We only rearm the timer if we have at least
333
if (b->flags & BF_PARTIAL_WRITE && tv_isset(&b->wex)) {
334
if (tv_add_ifset(&b->wex, &now, &b->wto)) {
335
/* FIXME: to prevent the client from expiring read timeouts during writes,
336
* we refresh it. A solution would be to merge read+write timeouts into a
337
* unique one, although that needs some study particularly on full-duplex
338
* TCP connections. */
339
if (!(b->flags & BF_SHUTR_STATUS) && tv_isset(&b->rex))
694
/* Funny, we were called to write something but there wasn't
695
* anything. We can get there, for example if we were woken up
696
* on a write event to finish the splice, but the send_max is 0
697
* so we cannot write anything from the buffer. Let's disable
698
* the write event and pretend we never came there.
702
if (!b->pipe && !b->send_max) {
703
/* the connection is established but we can't write. Either the
704
* buffer is empty, or we just refrain from sending because the
705
* send_max limit was reached. Maybe we just wrote the last
706
* chunk and need to close.
708
if (((b->flags & (BF_SHUTW|BF_EMPTY|BF_HIJACK|BF_WRITE_ENA|BF_SHUTR)) ==
709
(BF_EMPTY|BF_WRITE_ENA|BF_SHUTR)) &&
710
(si->state == SI_ST_EST)) {
711
stream_sock_shutw(si);
344
tv_eternity(&b->wex);
348
if (b->flags & BF_WRITE_STATUS)
349
task_wakeup(fdtab[fd].owner);
715
if ((b->flags & (BF_EMPTY|BF_SHUTW)) == BF_EMPTY)
716
si->flags |= SI_FL_WAIT_DATA;
718
EV_FD_CLR(fd, DIR_WR);
719
b->wex = TICK_ETERNITY;
723
if (b->flags & BF_WRITE_ACTIVITY) {
724
/* update timeout if we have written something */
725
if ((b->send_max || b->pipe) &&
726
(b->flags & (BF_SHUTW|BF_WRITE_PARTIAL)) == BF_WRITE_PARTIAL)
727
b->wex = tick_add_ifset(now_ms, b->wto);
730
if (tick_isset(si->ib->rex)) {
731
/* Note: to prevent the client from expiring read timeouts
732
* during writes, we refresh it. A better solution would be
733
* to merge read+write timeouts into a unique one, although
734
* that needs some study particularly on full-duplex TCP
737
si->ib->rex = tick_add_ifset(now_ms, si->ib->rto);
740
/* the producer might be waiting for more room to store data */
741
if (likely((b->flags & (BF_SHUTW|BF_WRITE_PARTIAL|BF_FULL)) == BF_WRITE_PARTIAL &&
742
(b->prod->flags & SI_FL_WAIT_ROOM)))
743
b->prod->chk_rcv(b->prod);
745
/* we have to wake up if there is a special event or if we don't have
746
* any more data to forward and it's not planned to send any more.
748
if (likely((b->flags & (BF_WRITE_NULL|BF_WRITE_ERROR|BF_SHUTW)) ||
749
(!b->to_forward && !b->send_max && !b->pipe) ||
750
si->state != SI_ST_EST ||
751
b->prod->state != SI_ST_EST))
752
task_wakeup(si->owner, TASK_WOKEN_IO);
350
755
fdtab[fd].ev &= ~FD_POLL_OUT;
354
/* There was an error. we must wakeup the task. No need to clear
355
* the events, the task will do it.
759
/* Write error on the file descriptor. We mark the FD as STERROR so
760
* that we don't use it anymore. The error is reported to the stream
761
* interface which will take proper action. We must not perturbate the
762
* buffer because the stream interface wants to ensure transparent
763
* connection retries.
357
766
fdtab[fd].state = FD_STERROR;
358
767
fdtab[fd].ev &= ~FD_POLL_STICKY;
359
b->flags |= BF_WRITE_ERROR;
769
si->flags |= SI_FL_ERR;
770
task_wakeup(si->owner, TASK_WOKEN_IO);
775
* This function performs a shutdown-write on a stream interface in a connected or
776
* init state (it does nothing for other states). It either shuts the write side
777
* or closes the file descriptor and marks itself as closed. The buffer flags are
778
* updated to reflect the new state.
780
void stream_sock_shutw(struct stream_interface *si)
782
if (si->ob->flags & BF_SHUTW)
784
si->ob->flags |= BF_SHUTW;
785
si->ob->wex = TICK_ETERNITY;
786
si->flags &= ~SI_FL_WAIT_DATA;
790
if (!(si->ib->flags & BF_SHUTR)) {
791
EV_FD_CLR(si->fd, DIR_WR);
792
shutdown(si->fd, SHUT_WR);
797
/* we may have to close a pending connection, and mark the
798
* response buffer as shutr
803
si->state = SI_ST_DIS;
805
si->flags &= ~SI_FL_WAIT_ROOM;
806
si->ib->flags |= BF_SHUTR;
807
si->ib->rex = TICK_ETERNITY;
808
si->exp = TICK_ETERNITY;
814
* This function performs a shutdown-read on a stream interface in a connected or
815
* init state (it does nothing for other states). It either shuts the read side
816
* or closes the file descriptor and marks itself as closed. The buffer flags are
817
* updated to reflect the new state.
819
void stream_sock_shutr(struct stream_interface *si)
821
if (si->ib->flags & BF_SHUTR)
823
si->ib->flags |= BF_SHUTR;
824
si->ib->rex = TICK_ETERNITY;
825
si->flags &= ~SI_FL_WAIT_ROOM;
827
if (si->state != SI_ST_EST && si->state != SI_ST_CON)
830
if (si->ob->flags & BF_SHUTW) {
832
si->state = SI_ST_DIS;
833
si->exp = TICK_ETERNITY;
836
EV_FD_CLR(si->fd, DIR_RD);
841
* Updates a connected stream_sock file descriptor status and timeouts
842
* according to the buffers' flags. It should only be called once after the
843
* buffer flags have settled down, and before they are cleared. It doesn't
844
* harm to call it as often as desired (it just slightly hurts performance).
846
void stream_sock_data_finish(struct stream_interface *si)
848
struct buffer *ib = si->ib;
849
struct buffer *ob = si->ob;
852
DPRINTF(stderr,"[%u] %s: fd=%d owner=%p ib=%p, ob=%p, exp(r,w)=%u,%u ibf=%08x obf=%08x ibl=%d obl=%d si=%d\n",
853
now_ms, __FUNCTION__,
857
ib->flags, ob->flags,
858
ib->l, ob->l, si->state);
860
/* Check if we need to close the read side */
861
if (!(ib->flags & BF_SHUTR)) {
862
/* Read not closed, update FD status and timeout for reads */
863
if (ib->flags & (BF_FULL|BF_HIJACK)) {
865
if ((ib->flags & (BF_FULL|BF_HIJACK)) == BF_FULL)
866
si->flags |= SI_FL_WAIT_ROOM;
867
EV_FD_COND_C(fd, DIR_RD);
868
ib->rex = TICK_ETERNITY;
871
/* (re)start reading and update timeout. Note: we don't recompute the timeout
872
* everytime we get here, otherwise it would risk never to expire. We only
873
* update it if is was not yet set, or if we already got some read status.
875
si->flags &= ~SI_FL_WAIT_ROOM;
876
EV_FD_COND_S(fd, DIR_RD);
877
if (!(ib->flags & BF_READ_NOEXP) &&
878
(!tick_isset(ib->rex) || ib->flags & BF_READ_ACTIVITY))
879
ib->rex = tick_add_ifset(now_ms, ib->rto);
883
/* Check if we need to close the write side */
884
if (!(ob->flags & BF_SHUTW)) {
885
/* Write not closed, update FD status and timeout for writes */
886
if ((ob->send_max == 0 && !ob->pipe) ||
887
(ob->flags & BF_EMPTY) ||
888
(ob->flags & (BF_HIJACK|BF_WRITE_ENA)) == 0) {
890
if ((ob->flags & (BF_EMPTY|BF_HIJACK|BF_WRITE_ENA)) == (BF_EMPTY|BF_WRITE_ENA))
891
si->flags |= SI_FL_WAIT_DATA;
892
EV_FD_COND_C(fd, DIR_WR);
893
ob->wex = TICK_ETERNITY;
896
/* (re)start writing and update timeout. Note: we don't recompute the timeout
897
* everytime we get here, otherwise it would risk never to expire. We only
898
* update it if is was not yet set, or if we already got some write status.
900
si->flags &= ~SI_FL_WAIT_DATA;
901
EV_FD_COND_S(fd, DIR_WR);
902
if (!tick_isset(ob->wex) || ob->flags & BF_WRITE_ACTIVITY) {
903
ob->wex = tick_add_ifset(now_ms, ob->wto);
904
if (tick_isset(ib->rex)) {
905
/* Note: depending on the protocol, we don't know if we're waiting
906
* for incoming data or not. So in order to prevent the socket from
907
* expiring read timeouts during writes, we refresh the read timeout,
908
* except if it was already infinite.
910
ib->rex = tick_add_ifset(now_ms, ib->rto);
917
/* This function is used for inter-stream-interface calls. It is called by the
918
* consumer to inform the producer side that it may be interested in checking
919
* for free space in the buffer. Note that it intentionally does not update
920
* timeouts, so that we can still check them later at wake-up.
922
void stream_sock_chk_rcv(struct stream_interface *si)
924
struct buffer *ib = si->ib;
926
DPRINTF(stderr,"[%u] %s: fd=%d owner=%p ib=%p, ob=%p, exp(r,w)=%u,%u ibf=%08x obf=%08x ibl=%d obl=%d si=%d\n",
927
now_ms, __FUNCTION__,
928
si->fd, fdtab[si->fd].owner,
930
ib->rex, si->ob->wex,
931
ib->flags, si->ob->flags,
932
ib->l, si->ob->l, si->state);
934
if (unlikely(si->state != SI_ST_EST || (ib->flags & BF_SHUTR)))
937
if (ib->flags & (BF_FULL|BF_HIJACK)) {
939
if ((ib->flags & (BF_FULL|BF_HIJACK)) == BF_FULL)
940
si->flags |= SI_FL_WAIT_ROOM;
941
EV_FD_COND_C(si->fd, DIR_RD);
944
/* (re)start reading */
945
si->flags &= ~SI_FL_WAIT_ROOM;
946
EV_FD_COND_S(si->fd, DIR_RD);
951
/* This function is used for inter-stream-interface calls. It is called by the
952
* producer to inform the consumer side that it may be interested in checking
953
* for data in the buffer. Note that it intentionally does not update timeouts,
954
* so that we can still check them later at wake-up.
956
void stream_sock_chk_snd(struct stream_interface *si)
958
struct buffer *ob = si->ob;
961
DPRINTF(stderr,"[%u] %s: fd=%d owner=%p ib=%p, ob=%p, exp(r,w)=%u,%u ibf=%08x obf=%08x ibl=%d obl=%d si=%d\n",
962
now_ms, __FUNCTION__,
963
si->fd, fdtab[si->fd].owner,
965
si->ib->rex, ob->wex,
966
si->ib->flags, ob->flags,
967
si->ib->l, ob->l, si->state);
969
if (unlikely(si->state != SI_ST_EST || (ob->flags & BF_SHUTW)))
972
if (!(si->flags & SI_FL_WAIT_DATA) || /* not waiting for data */
973
(fdtab[si->fd].ev & FD_POLL_OUT) || /* we'll be called anyway */
974
!(ob->send_max || ob->pipe) || /* called with nothing to send ! */
975
!(ob->flags & (BF_HIJACK|BF_WRITE_ENA))) /* we may not write */
978
retval = stream_sock_write_loop(si, ob);
980
/* Write error on the file descriptor. We mark the FD as STERROR so
981
* that we don't use it anymore and we notify the task.
983
fdtab[si->fd].state = FD_STERROR;
984
fdtab[si->fd].ev &= ~FD_POLL_STICKY;
986
si->flags |= SI_FL_ERR;
990
if (retval > 0 || (ob->send_max == 0 && !ob->pipe)) {
991
/* the connection is established but we can't write. Either the
992
* buffer is empty, or we just refrain from sending because the
993
* send_max limit was reached. Maybe we just wrote the last
994
* chunk and need to close.
996
if (((ob->flags & (BF_SHUTW|BF_EMPTY|BF_HIJACK|BF_WRITE_ENA|BF_SHUTR)) ==
997
(BF_EMPTY|BF_WRITE_ENA|BF_SHUTR)) &&
998
(si->state == SI_ST_EST)) {
999
stream_sock_shutw(si);
1003
if ((ob->flags & (BF_SHUTW|BF_EMPTY|BF_HIJACK|BF_WRITE_ENA)) == (BF_EMPTY|BF_WRITE_ENA))
1004
si->flags |= SI_FL_WAIT_DATA;
1005
ob->wex = TICK_ETERNITY;
1008
/* (re)start writing. */
1009
si->flags &= ~SI_FL_WAIT_DATA;
1010
EV_FD_COND_S(si->fd, DIR_WR);
1013
if (likely(ob->flags & BF_WRITE_ACTIVITY)) {
1014
/* update timeout if we have written something */
1015
if ((ob->send_max || ob->pipe) &&
1016
(ob->flags & (BF_SHUTW|BF_WRITE_PARTIAL)) == BF_WRITE_PARTIAL)
1017
ob->wex = tick_add_ifset(now_ms, ob->wto);
1019
if (tick_isset(si->ib->rex)) {
1020
/* Note: to prevent the client from expiring read timeouts
1021
* during writes, we refresh it. A better solution would be
1022
* to merge read+write timeouts into a unique one, although
1023
* that needs some study particularly on full-duplex TCP
1026
si->ib->rex = tick_add_ifset(now_ms, si->ib->rto);
1030
/* in case of special condition (error, shutdown, end of write...), we
1031
* have to notify the task.
1033
if (likely((ob->flags & (BF_WRITE_NULL|BF_WRITE_ERROR|BF_SHUTW)) ||
1034
(!ob->to_forward && !ob->send_max && !ob->pipe) ||
1035
si->state != SI_ST_EST)) {
1037
task_wakeup(si->owner, TASK_WOKEN_IO);