Willy Tarreau | 1122d9c | 2012-02-27 19:31:50 +0100 | [diff] [blame] | 1 | 2012/02/27 - Operations on haproxy buffers - w@1wt.eu |
| 2 | |
| 3 | |
| 4 | 1) Definitions |
| 5 | -------------- |
| 6 | |
| 7 | A buffer is a unidirectional storage between two stream interfaces which are |
| 8 | most often composed of a socket file descriptor. This storage is fixed sized |
| 9 | and circular, which means that once data reach the end of the buffer, it loops |
| 10 | back at the beginning of the buffer : |
| 11 | |
| 12 | |
| 13 | Representation of a non-wrapping buffer |
| 14 | --------------------------------------- |
| 15 | |
| 16 | |
| 17 | beginning end |
| 18 | | -------- length --------> | |
| 19 | V V |
| 20 | +-------------------------------------------+ |
| 21 | | <--------------- size ----------------> | |
| 22 | +-------------------------------------------+ |
| 23 | |
| 24 | |
| 25 | Representation of a wrapping buffer |
| 26 | ----------------------------------- |
| 27 | |
| 28 | end beginning |
| 29 | +------> | | -------------+ |
| 30 | | V V | |
| 31 | | +-------------------------------------------+ | |
| 32 | | | <--------------- size ----------------> | | |
| 33 | | +-------------------------------------------+ | |
| 34 | | | |
| 35 | +--------------------- length -----------------------+ |
| 36 | |
| 37 | |
| 38 | Buffers are read by two entities : |
| 39 | - stream interfaces |
| 40 | - analysers |
| 41 | |
| 42 | Buffers are filled by two entities : |
| 43 | - stream interfaces |
| 44 | - hijackers |
| 45 | |
| 46 | A stream interface writes at the input of a buffer and reads at its output. An |
| 47 | analyser has to parse incoming buffer contents, so it reads the input. It does |
| 48 | not really write the output though it may change the buffer's contents at the |
| 49 | input, possibly causing data moves. A hijacker it able to write at the output |
| 50 | of a buffer. Hijackers are not used anymore at the moment though error outputs |
| 51 | still work the same way. |
| 52 | |
| 53 | Buffers are referenced in the session. Each session has two buffers which |
| 54 | interconnect the two stream interfaces. One buffer is called the request |
| 55 | buffer, it sees traffic flowing from the client to the server. The other buffer |
| 56 | is the response buffer, it sees traffic flowing from the server to the client. |
| 57 | |
| 58 | By convention, sessions are represented as 2 buffers one on top of the other, |
| 59 | and with 2 stream interfaces connected to the two buffers. The client connects |
| 60 | to the left stream interface (which then acts as a server), and the right |
| 61 | stream interface (which acts as a client) connects to the server. The data |
| 62 | circulate clockwise, so the upper buffer is the request buffer and the lower |
| 63 | buffer is the response buffer : |
| 64 | |
| 65 | ,------------------------. |
| 66 | ,-----> | request buffer | ------. |
| 67 | from ,--./ `------------------------' \,--. to |
| 68 | client ( ) ( ) server |
| 69 | `--' ,------------------------. /`--' |
| 70 | ^------- | response buffer | <-----' |
| 71 | `------------------------' |
| 72 | |
| 73 | 2) Operations |
| 74 | ------------- |
| 75 | |
| 76 | Socket-based stream interfaces write to buffers directly from the I/O layer |
| 77 | without relying on any specific function. |
| 78 | |
| 79 | Function-based stream interfaces do use a number of non-uniform functions to |
| 80 | read from the buffer's output and to write to the buffer's input. More suited |
| 81 | names could be : |
| 82 | |
| 83 | int buffer_output_peek_at(buf, ofs, ptr, size); |
| 84 | int buffer_output_peek(buf, ptr, size); |
| 85 | int buffer_output_read(buf, ptr, size); |
| 86 | int buffer_output_skip(buf, size); |
| 87 | int buffer_input_write(buf, ptr, size); |
| 88 | |
| 89 | Right now some stream interfaces use the following functions which also happen |
| 90 | to automatically schedule the response for automatic forward : |
| 91 | |
| 92 | buffer_put_block() [peers] |
| 93 | buffer_put_chunk() -> buffer_put_block() |
| 94 | buffer_feed_chunk() -> buffer_put_chunk() -> buffer_put_block() [dumpstats] |
| 95 | buffer_feed() -> buffer_put_string() -> buffer_put_block() [dumpstats] |
| 96 | |
| 97 | |
| 98 | The following stream-interface oriented functions are not used : |
| 99 | |
| 100 | buffer_get_char() |
| 101 | buffer_write_chunk() |
| 102 | |
| 103 | |
| 104 | Analysers read data from the buffers' input, and may sometimes write data |
| 105 | there too (or trim data). More suited names could be : |
| 106 | |
| 107 | int buffer_input_peek_at(buf, ofs, ptr, size); |
| 108 | int buffer_input_truncate_at(buf, ofs); |
| 109 | int buffer_input_peek(buf, ptr, size); |
| 110 | int buffer_input_read(buf, ptr, size); |
| 111 | int buffer_input_skip(buf, size); |
| 112 | int buffer_input_cut(buf, size); |
| 113 | int buffer_input_truncate(buf); |
| 114 | |
| 115 | |
| 116 | Functions that are available and need to be renamed : |
| 117 | - buffer_skip : buffer_output_skip |
| 118 | - buffer_ignore : buffer_input_skip ? => not exactly, more like |
| 119 | buffer_output_skip() without affecting sendmax ! |
| 120 | - buffer_cut_tail : deletes all pending data after sendmax. |
| 121 | -> buffer_input_truncate(). Used by si_retnclose() only. |
| 122 | - buffer_contig_data : buffer_output_contig_data |
| 123 | - buffer_pending : buffer_input_pending_data |
| 124 | - buffer_contig_space : buffer_input_contig_space |
| 125 | |
| 126 | |
| 127 | It looks like buf->lr could be removed and be stored in the HTTP message struct |
| 128 | since it's only used at the HTTP level. |