Remove the connection_t.outbuf_flushlen field

This was once used for rate-limiting, but now it's only for
accounting.  It hasn't served a useful purpose in a long time.

Closes ticket 33097.
This commit is contained in:
Nick Mathewson 2020-07-21 15:08:00 -04:00 committed by Alexander Færøy
parent e8497bfaa7
commit 3cb9a9b8ce
14 changed files with 61 additions and 127 deletions

4
changes/bug33097 Normal file
View File

@ -0,0 +1,4 @@
o Code simplification and refactoring:
- Remove the now-redundant 'outbuf_flushlen' field from our connection
type. It was previously used for an older version of our rate-limiting
logic. Closes ticket 33097.

View File

@ -1033,11 +1033,11 @@ connection_close_immediate(connection_t *conn)
tor_fragile_assert(); tor_fragile_assert();
return; return;
} }
if (conn->outbuf_flushlen) { if (connection_get_outbuf_len(conn)) {
log_info(LD_NET,"fd %d, type %s, state %s, %d bytes on outbuf.", log_info(LD_NET,"fd %d, type %s, state %s, %"TOR_PRIuSZ" bytes on outbuf.",
(int)conn->s, conn_type_to_string(conn->type), (int)conn->s, conn_type_to_string(conn->type),
conn_state_to_string(conn->type, conn->state), conn_state_to_string(conn->type, conn->state),
(int)conn->outbuf_flushlen); buf_datalen(conn->outbuf));
} }
connection_unregister_events(conn); connection_unregister_events(conn);
@ -1053,7 +1053,6 @@ connection_close_immediate(connection_t *conn)
conn->linked_conn_is_closed = 1; conn->linked_conn_is_closed = 1;
if (conn->outbuf) if (conn->outbuf)
buf_clear(conn->outbuf); buf_clear(conn->outbuf);
conn->outbuf_flushlen = 0;
} }
/** Mark <b>conn</b> to be closed next time we loop through /** Mark <b>conn</b> to be closed next time we loop through
@ -3421,12 +3420,12 @@ connection_bucket_write_limit(connection_t *conn, time_t now)
{ {
int base = RELAY_PAYLOAD_SIZE; int base = RELAY_PAYLOAD_SIZE;
int priority = conn->type != CONN_TYPE_DIR; int priority = conn->type != CONN_TYPE_DIR;
size_t conn_bucket = conn->outbuf_flushlen; size_t conn_bucket = buf_datalen(conn->outbuf);
size_t global_bucket_val = token_bucket_rw_get_write(&global_bucket); size_t global_bucket_val = token_bucket_rw_get_write(&global_bucket);
if (!connection_is_rate_limited(conn)) { if (!connection_is_rate_limited(conn)) {
/* be willing to write to local conns even if our buckets are empty */ /* be willing to write to local conns even if our buckets are empty */
return conn->outbuf_flushlen; return conn_bucket;
} }
if (connection_speaks_cells(conn)) { if (connection_speaks_cells(conn)) {
@ -4079,12 +4078,7 @@ connection_buf_read_from_socket(connection_t *conn, ssize_t *max_to_read,
result, (long)n_read, (long)n_written); result, (long)n_read, (long)n_written);
} else if (conn->linked) { } else if (conn->linked) {
if (conn->linked_conn) { if (conn->linked_conn) {
result = buf_move_to_buf(conn->inbuf, conn->linked_conn->outbuf, result = (int) buf_move_all(conn->inbuf, conn->linked_conn->outbuf);
&conn->linked_conn->outbuf_flushlen);
if (BUG(result<0)) {
log_warn(LD_BUG, "reading from linked connection buffer failed.");
return -1;
}
} else { } else {
result = 0; result = 0;
} }
@ -4188,12 +4182,11 @@ connection_fetch_from_buf_http(connection_t *conn,
body_out, body_used, max_bodylen, force_complete); body_out, body_used, max_bodylen, force_complete);
} }
/** Return conn-\>outbuf_flushlen: how many bytes conn wants to flush /** Return true if this connection has data to flush. */
* from its outbuf. */
int int
connection_wants_to_flush(connection_t *conn) connection_wants_to_flush(connection_t *conn)
{ {
return conn->outbuf_flushlen > 0; return connection_get_outbuf_len(conn) > 0;
} }
/** Are there too many bytes on edge connection <b>conn</b>'s outbuf to /** Are there too many bytes on edge connection <b>conn</b>'s outbuf to
@ -4203,7 +4196,7 @@ connection_wants_to_flush(connection_t *conn)
int int
connection_outbuf_too_full(connection_t *conn) connection_outbuf_too_full(connection_t *conn)
{ {
return (conn->outbuf_flushlen > 10*CELL_PAYLOAD_SIZE); return connection_get_outbuf_len(conn) > 10*CELL_PAYLOAD_SIZE;
} }
/** /**
@ -4329,7 +4322,7 @@ connection_handle_write_impl(connection_t *conn, int force)
return -1; return -1;
} }
max_to_write = force ? (ssize_t)conn->outbuf_flushlen max_to_write = force ? (ssize_t)buf_datalen(conn->outbuf)
: connection_bucket_write_limit(conn, now); : connection_bucket_write_limit(conn, now);
if (connection_speaks_cells(conn) && if (connection_speaks_cells(conn) &&
@ -4361,7 +4354,7 @@ connection_handle_write_impl(connection_t *conn, int force)
/* else open, or closing */ /* else open, or closing */
initial_size = buf_datalen(conn->outbuf); initial_size = buf_datalen(conn->outbuf);
result = buf_flush_to_tls(conn->outbuf, or_conn->tls, result = buf_flush_to_tls(conn->outbuf, or_conn->tls,
max_to_write, &conn->outbuf_flushlen); max_to_write);
if (result >= 0) if (result >= 0)
update_send_buffer_size(conn->s); update_send_buffer_size(conn->s);
@ -4427,7 +4420,7 @@ connection_handle_write_impl(connection_t *conn, int force)
} else { } else {
CONN_LOG_PROTECT(conn, CONN_LOG_PROTECT(conn,
result = buf_flush_to_socket(conn->outbuf, conn->s, result = buf_flush_to_socket(conn->outbuf, conn->s,
max_to_write, &conn->outbuf_flushlen)); max_to_write));
if (result < 0) { if (result < 0) {
if (CONN_IS_EDGE(conn)) if (CONN_IS_EDGE(conn))
connection_edge_end_errno(TO_EDGE_CONN(conn)); connection_edge_end_errno(TO_EDGE_CONN(conn));
@ -4583,10 +4576,10 @@ connection_write_to_buf_failed(connection_t *conn)
/** Helper for connection_write_to_buf_impl and connection_write_buf_to_buf: /** Helper for connection_write_to_buf_impl and connection_write_buf_to_buf:
* *
* Called when an attempt to add bytes on <b>conn</b>'s outbuf has succeeded: * Called when an attempt to add bytes on <b>conn</b>'s outbuf has succeeded:
* record the number of bytes added. * start writing if appropriate.
*/ */
static void static void
connection_write_to_buf_commit(connection_t *conn, size_t len) connection_write_to_buf_commit(connection_t *conn)
{ {
/* If we receive optimistic data in the EXIT_CONN_STATE_RESOLVING /* If we receive optimistic data in the EXIT_CONN_STATE_RESOLVING
* state, we don't want to try to write it right away, since * state, we don't want to try to write it right away, since
@ -4595,7 +4588,6 @@ connection_write_to_buf_commit(connection_t *conn, size_t len)
if (conn->write_event) { if (conn->write_event) {
connection_start_writing(conn); connection_start_writing(conn);
} }
conn->outbuf_flushlen += len;
} }
/** Append <b>len</b> bytes of <b>string</b> onto <b>conn</b>'s /** Append <b>len</b> bytes of <b>string</b> onto <b>conn</b>'s
@ -4618,25 +4610,20 @@ connection_write_to_buf_impl_,(const char *string, size_t len,
if (!connection_may_write_to_buf(conn)) if (!connection_may_write_to_buf(conn))
return; return;
size_t written;
if (zlib) { if (zlib) {
size_t old_datalen = buf_datalen(conn->outbuf);
dir_connection_t *dir_conn = TO_DIR_CONN(conn); dir_connection_t *dir_conn = TO_DIR_CONN(conn);
int done = zlib < 0; int done = zlib < 0;
CONN_LOG_PROTECT(conn, r = buf_add_compress(conn->outbuf, CONN_LOG_PROTECT(conn, r = buf_add_compress(conn->outbuf,
dir_conn->compress_state, dir_conn->compress_state,
string, len, done)); string, len, done));
written = buf_datalen(conn->outbuf) - old_datalen;
} else { } else {
CONN_LOG_PROTECT(conn, r = buf_add(conn->outbuf, string, len)); CONN_LOG_PROTECT(conn, r = buf_add(conn->outbuf, string, len));
written = len;
} }
if (r < 0) { if (r < 0) {
connection_write_to_buf_failed(conn); connection_write_to_buf_failed(conn);
return; return;
} }
connection_write_to_buf_commit(conn, written); connection_write_to_buf_commit(conn);
} }
/** /**
@ -4681,7 +4668,7 @@ connection_buf_add_buf(connection_t *conn, buf_t *buf)
return; return;
buf_move_all(conn->outbuf, buf); buf_move_all(conn->outbuf, buf);
connection_write_to_buf_commit(conn, len); connection_write_to_buf_commit(conn);
} }
#define CONN_GET_ALL_TEMPLATE(var, test) \ #define CONN_GET_ALL_TEMPLATE(var, test) \
@ -5569,18 +5556,6 @@ assert_connection_ok(connection_t *conn, time_t now)
if (conn->linked) if (conn->linked)
tor_assert(!SOCKET_OK(conn->s)); tor_assert(!SOCKET_OK(conn->s));
if (conn->outbuf_flushlen > 0) {
/* With optimistic data, we may have queued data in
* EXIT_CONN_STATE_RESOLVING while the conn is not yet marked to writing.
* */
tor_assert((conn->type == CONN_TYPE_EXIT &&
conn->state == EXIT_CONN_STATE_RESOLVING) ||
connection_is_writing(conn) ||
conn->write_blocked_on_bw ||
(CONN_IS_EDGE(conn) &&
TO_EDGE_CONN(conn)->edge_blocked_on_circ));
}
if (conn->hold_open_until_flushed) if (conn->hold_open_until_flushed)
tor_assert(conn->marked_for_close); tor_assert(conn->marked_for_close);

View File

@ -985,33 +985,29 @@ conn_close_if_marked(int i)
if (!conn->hold_open_until_flushed) if (!conn->hold_open_until_flushed)
log_info(LD_NET, log_info(LD_NET,
"Conn (addr %s, fd %d, type %s, state %d) marked, but wants " "Conn (addr %s, fd %d, type %s, state %d) marked, but wants "
"to flush %d bytes. (Marked at %s:%d)", "to flush %"TOR_PRIuSZ" bytes. (Marked at %s:%d)",
escaped_safe_str_client(conn->address), escaped_safe_str_client(conn->address),
(int)conn->s, conn_type_to_string(conn->type), conn->state, (int)conn->s, conn_type_to_string(conn->type), conn->state,
(int)conn->outbuf_flushlen, connection_get_outbuf_len(conn),
conn->marked_for_close_file, conn->marked_for_close); conn->marked_for_close_file, conn->marked_for_close);
if (conn->linked_conn) { if (conn->linked_conn) {
retval = buf_move_to_buf(conn->linked_conn->inbuf, conn->outbuf, retval = (int) buf_move_all(conn->linked_conn->inbuf, conn->outbuf);
&conn->outbuf_flushlen);
if (retval >= 0) { if (retval >= 0) {
/* The linked conn will notice that it has data when it notices that /* The linked conn will notice that it has data when it notices that
* we're gone. */ * we're gone. */
connection_start_reading_from_linked_conn(conn->linked_conn); connection_start_reading_from_linked_conn(conn->linked_conn);
} }
log_debug(LD_GENERAL, "Flushed last %d bytes from a linked conn; " log_debug(LD_GENERAL, "Flushed last %d bytes from a linked conn; "
"%d left; flushlen %d; wants-to-flush==%d", retval, "%d left; wants-to-flush==%d", retval,
(int)connection_get_outbuf_len(conn), (int)connection_get_outbuf_len(conn),
(int)conn->outbuf_flushlen,
connection_wants_to_flush(conn)); connection_wants_to_flush(conn));
} else if (connection_speaks_cells(conn)) { } else if (connection_speaks_cells(conn)) {
if (conn->state == OR_CONN_STATE_OPEN) { if (conn->state == OR_CONN_STATE_OPEN) {
retval = buf_flush_to_tls(conn->outbuf, TO_OR_CONN(conn)->tls, sz, retval = buf_flush_to_tls(conn->outbuf, TO_OR_CONN(conn)->tls, sz);
&conn->outbuf_flushlen);
} else } else
retval = -1; /* never flush non-open broken tls connections */ retval = -1; /* never flush non-open broken tls connections */
} else { } else {
retval = buf_flush_to_socket(conn->outbuf, conn->s, sz, retval = buf_flush_to_socket(conn->outbuf, conn->s, sz);
&conn->outbuf_flushlen);
} }
if (retval >= 0 && /* Technically, we could survive things like if (retval >= 0 && /* Technically, we could survive things like
TLS_WANT_WRITE here. But don't bother for now. */ TLS_WANT_WRITE here. But don't bother for now. */

View File

@ -2437,7 +2437,6 @@ single_conn_free_bytes(connection_t *conn)
if (conn->outbuf) { if (conn->outbuf) {
result += buf_allocation(conn->outbuf); result += buf_allocation(conn->outbuf);
buf_clear(conn->outbuf); buf_clear(conn->outbuf);
conn->outbuf_flushlen = 0;
} }
if (conn->type == CONN_TYPE_DIR) { if (conn->type == CONN_TYPE_DIR) {
dir_connection_t *dir_conn = TO_DIR_CONN(conn); dir_connection_t *dir_conn = TO_DIR_CONN(conn);

View File

@ -98,8 +98,6 @@ struct connection_t {
struct buf_t *inbuf; /**< Buffer holding data read over this connection. */ struct buf_t *inbuf; /**< Buffer holding data read over this connection. */
struct buf_t *outbuf; /**< Buffer holding data to write over this struct buf_t *outbuf; /**< Buffer holding data to write over this
* connection. */ * connection. */
size_t outbuf_flushlen; /**< How much data should we try to flush from the
* outbuf? */
time_t timestamp_last_read_allowed; /**< When was the last time libevent said time_t timestamp_last_read_allowed; /**< When was the last time libevent said
* we could read? */ * we could read? */
time_t timestamp_last_write_allowed; /**< When was the last time libevent time_t timestamp_last_write_allowed; /**< When was the last time libevent

View File

@ -394,7 +394,7 @@ sendme_connection_edge_consider_sending(edge_connection_t *conn)
while (conn->deliver_window <= while (conn->deliver_window <=
(STREAMWINDOW_START - STREAMWINDOW_INCREMENT)) { (STREAMWINDOW_START - STREAMWINDOW_INCREMENT)) {
log_debug(log_domain, "Outbuf %" TOR_PRIuSZ ", queuing stream SENDME.", log_debug(log_domain, "Outbuf %" TOR_PRIuSZ ", queuing stream SENDME.",
TO_CONN(conn)->outbuf_flushlen); buf_datalen(TO_CONN(conn)->outbuf));
conn->deliver_window += STREAMWINDOW_INCREMENT; conn->deliver_window += STREAMWINDOW_INCREMENT;
if (connection_edge_send_command(conn, RELAY_COMMAND_SENDME, if (connection_edge_send_command(conn, RELAY_COMMAND_SENDME,
NULL, 0) < 0) { NULL, 0) < 0) {

View File

@ -685,17 +685,20 @@ buf_move_to_buf(buf_t *buf_out, buf_t *buf_in, size_t *buf_flushlen)
} }
/** Moves all data from <b>buf_in</b> to <b>buf_out</b>, without copying. /** Moves all data from <b>buf_in</b> to <b>buf_out</b>, without copying.
* Return the number of bytes that were moved.
*/ */
void size_t
buf_move_all(buf_t *buf_out, buf_t *buf_in) buf_move_all(buf_t *buf_out, buf_t *buf_in)
{ {
tor_assert(buf_out); tor_assert(buf_out);
if (!buf_in) if (!buf_in)
return; return 0;
if (BUG(buf_out->datalen > BUF_MAX_LEN || buf_in->datalen > BUF_MAX_LEN)) if (BUG(buf_out->datalen > BUF_MAX_LEN || buf_in->datalen > BUF_MAX_LEN))
return; return 0;
if (BUG(buf_out->datalen > BUF_MAX_LEN - buf_in->datalen)) if (BUG(buf_out->datalen > BUF_MAX_LEN - buf_in->datalen))
return; return 0;
size_t n_bytes_moved = buf_in->datalen;
if (buf_out->head == NULL) { if (buf_out->head == NULL) {
buf_out->head = buf_in->head; buf_out->head = buf_in->head;
@ -708,6 +711,8 @@ buf_move_all(buf_t *buf_out, buf_t *buf_in)
buf_out->datalen += buf_in->datalen; buf_out->datalen += buf_in->datalen;
buf_in->head = buf_in->tail = NULL; buf_in->head = buf_in->tail = NULL;
buf_in->datalen = 0; buf_in->datalen = 0;
return n_bytes_moved;
} }
/** Internal structure: represents a position in a buffer. */ /** Internal structure: represents a position in a buffer. */

View File

@ -46,7 +46,7 @@ void buf_add_printf(buf_t *buf, const char *format, ...)
void buf_add_vprintf(buf_t *buf, const char *format, va_list args) void buf_add_vprintf(buf_t *buf, const char *format, va_list args)
CHECK_PRINTF(2, 0); CHECK_PRINTF(2, 0);
int buf_move_to_buf(buf_t *buf_out, buf_t *buf_in, size_t *buf_flushlen); int buf_move_to_buf(buf_t *buf_out, buf_t *buf_in, size_t *buf_flushlen);
void buf_move_all(buf_t *buf_out, buf_t *buf_in); size_t buf_move_all(buf_t *buf_out, buf_t *buf_in);
void buf_peek(const buf_t *buf, char *string, size_t string_len); void buf_peek(const buf_t *buf, char *string, size_t string_len);
void buf_drain(buf_t *buf, size_t n); void buf_drain(buf_t *buf, size_t n);
int buf_get_bytes(buf_t *buf, char *string, size_t string_len); int buf_get_bytes(buf_t *buf, char *string, size_t string_len);

View File

@ -137,13 +137,12 @@ buf_read_from_fd(buf_t *buf, int fd, size_t at_most,
} }
/** Helper for buf_flush_to_socket(): try to write <b>sz</b> bytes from chunk /** Helper for buf_flush_to_socket(): try to write <b>sz</b> bytes from chunk
* <b>chunk</b> of buffer <b>buf</b> onto file descriptor <b>fd</b>. On * <b>chunk</b> of buffer <b>buf</b> onto file descriptor <b>fd</b>. Return
* success, deduct the bytes written from *<b>buf_flushlen</b>. Return the * the number of bytes written on success, 0 on blocking, -1 on failure.
* number of bytes written on success, 0 on blocking, -1 on failure.
*/ */
static inline int static inline int
flush_chunk(tor_socket_t fd, buf_t *buf, chunk_t *chunk, size_t sz, flush_chunk(tor_socket_t fd, buf_t *buf, chunk_t *chunk, size_t sz,
size_t *buf_flushlen, bool is_socket) bool is_socket)
{ {
ssize_t write_result; ssize_t write_result;
@ -168,7 +167,6 @@ flush_chunk(tor_socket_t fd, buf_t *buf, chunk_t *chunk, size_t sz,
log_debug(LD_NET,"write() would block, returning."); log_debug(LD_NET,"write() would block, returning.");
return 0; return 0;
} else { } else {
*buf_flushlen -= write_result;
buf_drain(buf, write_result); buf_drain(buf, write_result);
tor_assert(write_result <= BUF_MAX_LEN); tor_assert(write_result <= BUF_MAX_LEN);
return (int)write_result; return (int)write_result;
@ -176,27 +174,22 @@ flush_chunk(tor_socket_t fd, buf_t *buf, chunk_t *chunk, size_t sz,
} }
/** Write data from <b>buf</b> to the file descriptor <b>fd</b>. Write at most /** Write data from <b>buf</b> to the file descriptor <b>fd</b>. Write at most
* <b>sz</b> bytes, decrement *<b>buf_flushlen</b> by * <b>sz</b> bytes, and remove the written bytes
* the number of bytes actually written, and remove the written bytes
* from the buffer. Return the number of bytes written on success, * from the buffer. Return the number of bytes written on success,
* -1 on failure. Return 0 if write() would block. * -1 on failure. Return 0 if write() would block.
*/ */
static int static int
buf_flush_to_fd(buf_t *buf, int fd, size_t sz, buf_flush_to_fd(buf_t *buf, int fd, size_t sz,
size_t *buf_flushlen, bool is_socket) bool is_socket)
{ {
/* XXXX It's stupid to overload the return values for these functions: /* XXXX It's stupid to overload the return values for these functions:
* "error status" and "number of bytes flushed" are not mutually exclusive. * "error status" and "number of bytes flushed" are not mutually exclusive.
*/ */
int r; int r;
size_t flushed = 0; size_t flushed = 0;
tor_assert(buf_flushlen);
tor_assert(SOCKET_OK(fd)); tor_assert(SOCKET_OK(fd));
if (BUG(*buf_flushlen > buf->datalen)) { if (BUG(sz > buf->datalen)) {
*buf_flushlen = buf->datalen; sz = buf->datalen;
}
if (BUG(sz > *buf_flushlen)) {
sz = *buf_flushlen;
} }
check(); check();
@ -208,7 +201,7 @@ buf_flush_to_fd(buf_t *buf, int fd, size_t sz,
else else
flushlen0 = buf->head->datalen; flushlen0 = buf->head->datalen;
r = flush_chunk(fd, buf, buf->head, flushlen0, buf_flushlen, is_socket); r = flush_chunk(fd, buf, buf->head, flushlen0, is_socket);
check(); check();
if (r < 0) if (r < 0)
return r; return r;
@ -228,10 +221,9 @@ buf_flush_to_fd(buf_t *buf, int fd, size_t sz,
* -1 on failure. Return 0 if write() would block. * -1 on failure. Return 0 if write() would block.
*/ */
int int
buf_flush_to_socket(buf_t *buf, tor_socket_t s, size_t sz, buf_flush_to_socket(buf_t *buf, tor_socket_t s, size_t sz)
size_t *buf_flushlen)
{ {
return buf_flush_to_fd(buf, s, sz, buf_flushlen, true); return buf_flush_to_fd(buf, s, sz, true);
} }
/** Read from socket <b>s</b>, writing onto end of <b>buf</b>. Read at most /** Read from socket <b>s</b>, writing onto end of <b>buf</b>. Read at most
@ -254,10 +246,9 @@ buf_read_from_socket(buf_t *buf, tor_socket_t s, size_t at_most,
* -1 on failure. Return 0 if write() would block. * -1 on failure. Return 0 if write() would block.
*/ */
int int
buf_flush_to_pipe(buf_t *buf, int fd, size_t sz, buf_flush_to_pipe(buf_t *buf, int fd, size_t sz)
size_t *buf_flushlen)
{ {
return buf_flush_to_fd(buf, fd, sz, buf_flushlen, false); return buf_flush_to_fd(buf, fd, sz, false);
} }
/** Read from pipe <b>fd</b>, writing onto end of <b>buf</b>. Read at most /** Read from pipe <b>fd</b>, writing onto end of <b>buf</b>. Read at most

View File

@ -21,14 +21,12 @@ int buf_read_from_socket(struct buf_t *buf, tor_socket_t s, size_t at_most,
int *reached_eof, int *reached_eof,
int *socket_error); int *socket_error);
int buf_flush_to_socket(struct buf_t *buf, tor_socket_t s, size_t sz, int buf_flush_to_socket(struct buf_t *buf, tor_socket_t s, size_t sz);
size_t *buf_flushlen);
int buf_read_from_pipe(struct buf_t *buf, int fd, size_t at_most, int buf_read_from_pipe(struct buf_t *buf, int fd, size_t at_most,
int *reached_eof, int *reached_eof,
int *socket_error); int *socket_error);
int buf_flush_to_pipe(struct buf_t *buf, int fd, size_t sz, int buf_flush_to_pipe(struct buf_t *buf, int fd, size_t sz);
size_t *buf_flushlen);
#endif /* !defined(TOR_BUFFERS_NET_H) */ #endif /* !defined(TOR_BUFFERS_NET_H) */

View File

@ -418,7 +418,7 @@ process_unix_write(process_t *process, buf_t *buffer)
/* We have data to write and the kernel have told us to write it. */ /* We have data to write and the kernel have told us to write it. */
return buf_flush_to_pipe(buffer, return buf_flush_to_pipe(buffer,
process_get_unix_process(process)->stdin_handle.fd, process_get_unix_process(process)->stdin_handle.fd,
max_to_write, &buffer_flush_len); max_to_write);
} }
/** Read data from the given process's standard output and put it into /** Read data from the given process's standard output and put it into

View File

@ -106,8 +106,7 @@ buf_read_from_tls(buf_t *buf, tor_tls_t *tls, size_t at_most)
* written on success, and a TOR_TLS error code on failure or blocking. * written on success, and a TOR_TLS error code on failure or blocking.
*/ */
static inline int static inline int
flush_chunk_tls(tor_tls_t *tls, buf_t *buf, chunk_t *chunk, flush_chunk_tls(tor_tls_t *tls, buf_t *buf, chunk_t *chunk, size_t sz)
size_t sz, size_t *buf_flushlen)
{ {
int r; int r;
size_t forced; size_t forced;
@ -126,13 +125,9 @@ flush_chunk_tls(tor_tls_t *tls, buf_t *buf, chunk_t *chunk,
r = tor_tls_write(tls, data, sz); r = tor_tls_write(tls, data, sz);
if (r < 0) if (r < 0)
return r; return r;
if (*buf_flushlen > (size_t)r)
*buf_flushlen -= r;
else
*buf_flushlen = 0;
buf_drain(buf, r); buf_drain(buf, r);
log_debug(LD_NET,"flushed %d bytes, %d ready to flush, %d remain.", log_debug(LD_NET,"flushed %d bytes, %d remain.",
r,(int)*buf_flushlen,(int)buf->datalen); r,(int)buf->datalen);
return r; return r;
} }
@ -140,18 +135,13 @@ flush_chunk_tls(tor_tls_t *tls, buf_t *buf, chunk_t *chunk,
* more than <b>flushlen</b> bytes. * more than <b>flushlen</b> bytes.
*/ */
int int
buf_flush_to_tls(buf_t *buf, tor_tls_t *tls, size_t flushlen, buf_flush_to_tls(buf_t *buf, tor_tls_t *tls, size_t flushlen)
size_t *buf_flushlen)
{ {
int r; int r;
size_t flushed = 0; size_t flushed = 0;
ssize_t sz; ssize_t sz;
tor_assert(buf_flushlen); IF_BUG_ONCE(flushlen > buf->datalen) {
IF_BUG_ONCE(*buf_flushlen > buf->datalen) { flushlen = buf->datalen;
*buf_flushlen = buf->datalen;
}
IF_BUG_ONCE(flushlen > *buf_flushlen) {
flushlen = *buf_flushlen;
} }
sz = (ssize_t) flushlen; sz = (ssize_t) flushlen;
@ -170,7 +160,7 @@ buf_flush_to_tls(buf_t *buf, tor_tls_t *tls, size_t flushlen,
flushlen0 = 0; flushlen0 = 0;
} }
r = flush_chunk_tls(tls, buf, buf->head, flushlen0, buf_flushlen); r = flush_chunk_tls(tls, buf, buf->head, flushlen0);
if (r < 0) if (r < 0)
return r; return r;
flushed += r; flushed += r;

View File

@ -18,6 +18,6 @@ struct tor_tls_t;
int buf_read_from_tls(struct buf_t *buf, int buf_read_from_tls(struct buf_t *buf,
struct tor_tls_t *tls, size_t at_most); struct tor_tls_t *tls, size_t at_most);
int buf_flush_to_tls(struct buf_t *buf, struct tor_tls_t *tls, int buf_flush_to_tls(struct buf_t *buf, struct tor_tls_t *tls,
size_t sz, size_t *buf_flushlen); size_t sz);
#endif /* !defined(TOR_BUFFERS_TLS_H) */ #endif /* !defined(TOR_BUFFERS_TLS_H) */

View File

@ -220,7 +220,6 @@ subtest_circbw_halfclosed(origin_circuit_t *circ, streamid_t init_id)
int sendme_cells = (STREAMWINDOW_START-edgeconn->package_window) int sendme_cells = (STREAMWINDOW_START-edgeconn->package_window)
/STREAMWINDOW_INCREMENT; /STREAMWINDOW_INCREMENT;
ENTRY_TO_CONN(entryconn2)->marked_for_close = 0; ENTRY_TO_CONN(entryconn2)->marked_for_close = 0;
ENTRY_TO_CONN(entryconn2)->outbuf_flushlen = 0;
connection_edge_reached_eof(edgeconn); connection_edge_reached_eof(edgeconn);
/* Data cell not in the half-opened list */ /* Data cell not in the half-opened list */
@ -272,7 +271,6 @@ subtest_circbw_halfclosed(origin_circuit_t *circ, streamid_t init_id)
/* DATA cells up to limit */ /* DATA cells up to limit */
while (data_cells > 0) { while (data_cells > 0) {
ENTRY_TO_CONN(entryconn2)->marked_for_close = 0; ENTRY_TO_CONN(entryconn2)->marked_for_close = 0;
ENTRY_TO_CONN(entryconn2)->outbuf_flushlen = 0;
PACK_CELL(edgeconn->stream_id, RELAY_COMMAND_DATA, "Data1234"); PACK_CELL(edgeconn->stream_id, RELAY_COMMAND_DATA, "Data1234");
if (circ->base_.purpose == CIRCUIT_PURPOSE_PATH_BIAS_TESTING) if (circ->base_.purpose == CIRCUIT_PURPOSE_PATH_BIAS_TESTING)
pathbias_count_valid_cells(TO_CIRCUIT(circ), &cell); pathbias_count_valid_cells(TO_CIRCUIT(circ), &cell);
@ -283,7 +281,6 @@ subtest_circbw_halfclosed(origin_circuit_t *circ, streamid_t init_id)
data_cells--; data_cells--;
} }
ENTRY_TO_CONN(entryconn2)->marked_for_close = 0; ENTRY_TO_CONN(entryconn2)->marked_for_close = 0;
ENTRY_TO_CONN(entryconn2)->outbuf_flushlen = 0;
PACK_CELL(edgeconn->stream_id, RELAY_COMMAND_DATA, "Data1234"); PACK_CELL(edgeconn->stream_id, RELAY_COMMAND_DATA, "Data1234");
if (circ->base_.purpose == CIRCUIT_PURPOSE_PATH_BIAS_TESTING) if (circ->base_.purpose == CIRCUIT_PURPOSE_PATH_BIAS_TESTING)
pathbias_count_valid_cells(TO_CIRCUIT(circ), &cell); pathbias_count_valid_cells(TO_CIRCUIT(circ), &cell);
@ -295,7 +292,6 @@ subtest_circbw_halfclosed(origin_circuit_t *circ, streamid_t init_id)
/* SENDME cells up to limit */ /* SENDME cells up to limit */
while (sendme_cells > 0) { while (sendme_cells > 0) {
ENTRY_TO_CONN(entryconn2)->marked_for_close = 0; ENTRY_TO_CONN(entryconn2)->marked_for_close = 0;
ENTRY_TO_CONN(entryconn2)->outbuf_flushlen = 0;
PACK_CELL(edgeconn->stream_id, RELAY_COMMAND_SENDME, "Data1234"); PACK_CELL(edgeconn->stream_id, RELAY_COMMAND_SENDME, "Data1234");
if (circ->base_.purpose == CIRCUIT_PURPOSE_PATH_BIAS_TESTING) if (circ->base_.purpose == CIRCUIT_PURPOSE_PATH_BIAS_TESTING)
pathbias_count_valid_cells(TO_CIRCUIT(circ), &cell); pathbias_count_valid_cells(TO_CIRCUIT(circ), &cell);
@ -306,7 +302,6 @@ subtest_circbw_halfclosed(origin_circuit_t *circ, streamid_t init_id)
sendme_cells--; sendme_cells--;
} }
ENTRY_TO_CONN(entryconn2)->marked_for_close = 0; ENTRY_TO_CONN(entryconn2)->marked_for_close = 0;
ENTRY_TO_CONN(entryconn2)->outbuf_flushlen = 0;
PACK_CELL(edgeconn->stream_id, RELAY_COMMAND_SENDME, "Data1234"); PACK_CELL(edgeconn->stream_id, RELAY_COMMAND_SENDME, "Data1234");
if (circ->base_.purpose == CIRCUIT_PURPOSE_PATH_BIAS_TESTING) if (circ->base_.purpose == CIRCUIT_PURPOSE_PATH_BIAS_TESTING)
pathbias_count_valid_cells(TO_CIRCUIT(circ), &cell); pathbias_count_valid_cells(TO_CIRCUIT(circ), &cell);
@ -317,7 +312,6 @@ subtest_circbw_halfclosed(origin_circuit_t *circ, streamid_t init_id)
/* Only one END cell */ /* Only one END cell */
ENTRY_TO_CONN(entryconn2)->marked_for_close = 0; ENTRY_TO_CONN(entryconn2)->marked_for_close = 0;
ENTRY_TO_CONN(entryconn2)->outbuf_flushlen = 0;
PACK_CELL(edgeconn->stream_id, RELAY_COMMAND_END, "Data1234"); PACK_CELL(edgeconn->stream_id, RELAY_COMMAND_END, "Data1234");
if (circ->base_.purpose == CIRCUIT_PURPOSE_PATH_BIAS_TESTING) if (circ->base_.purpose == CIRCUIT_PURPOSE_PATH_BIAS_TESTING)
pathbias_count_valid_cells(TO_CIRCUIT(circ), &cell); pathbias_count_valid_cells(TO_CIRCUIT(circ), &cell);
@ -327,7 +321,6 @@ subtest_circbw_halfclosed(origin_circuit_t *circ, streamid_t init_id)
ASSERT_COUNTED_BW(); ASSERT_COUNTED_BW();
ENTRY_TO_CONN(entryconn2)->marked_for_close = 0; ENTRY_TO_CONN(entryconn2)->marked_for_close = 0;
ENTRY_TO_CONN(entryconn2)->outbuf_flushlen = 0;
PACK_CELL(edgeconn->stream_id, RELAY_COMMAND_END, "Data1234"); PACK_CELL(edgeconn->stream_id, RELAY_COMMAND_END, "Data1234");
if (circ->base_.purpose == CIRCUIT_PURPOSE_PATH_BIAS_TESTING) if (circ->base_.purpose == CIRCUIT_PURPOSE_PATH_BIAS_TESTING)
pathbias_count_valid_cells(TO_CIRCUIT(circ), &cell); pathbias_count_valid_cells(TO_CIRCUIT(circ), &cell);
@ -339,7 +332,6 @@ subtest_circbw_halfclosed(origin_circuit_t *circ, streamid_t init_id)
edgeconn = ENTRY_TO_EDGE_CONN(entryconn3); edgeconn = ENTRY_TO_EDGE_CONN(entryconn3);
edgeconn->base_.state = AP_CONN_STATE_OPEN; edgeconn->base_.state = AP_CONN_STATE_OPEN;
ENTRY_TO_CONN(entryconn3)->marked_for_close = 0; ENTRY_TO_CONN(entryconn3)->marked_for_close = 0;
ENTRY_TO_CONN(entryconn3)->outbuf_flushlen = 0;
/* sendme cell on open entryconn with full window */ /* sendme cell on open entryconn with full window */
PACK_CELL(edgeconn->stream_id, RELAY_COMMAND_SENDME, "Data1234"); PACK_CELL(edgeconn->stream_id, RELAY_COMMAND_SENDME, "Data1234");
int ret = int ret =
@ -350,7 +342,6 @@ subtest_circbw_halfclosed(origin_circuit_t *circ, streamid_t init_id)
/* connected cell on a after EOF */ /* connected cell on a after EOF */
ENTRY_TO_CONN(entryconn3)->marked_for_close = 0; ENTRY_TO_CONN(entryconn3)->marked_for_close = 0;
ENTRY_TO_CONN(entryconn3)->outbuf_flushlen = 0;
edgeconn->base_.state = AP_CONN_STATE_CONNECT_WAIT; edgeconn->base_.state = AP_CONN_STATE_CONNECT_WAIT;
connection_edge_reached_eof(edgeconn); connection_edge_reached_eof(edgeconn);
PACK_CELL(edgeconn->stream_id, RELAY_COMMAND_CONNECTED, "Data1234"); PACK_CELL(edgeconn->stream_id, RELAY_COMMAND_CONNECTED, "Data1234");
@ -362,7 +353,6 @@ subtest_circbw_halfclosed(origin_circuit_t *circ, streamid_t init_id)
ASSERT_COUNTED_BW(); ASSERT_COUNTED_BW();
ENTRY_TO_CONN(entryconn3)->marked_for_close = 0; ENTRY_TO_CONN(entryconn3)->marked_for_close = 0;
ENTRY_TO_CONN(entryconn3)->outbuf_flushlen = 0;
PACK_CELL(edgeconn->stream_id, RELAY_COMMAND_CONNECTED, "Data1234"); PACK_CELL(edgeconn->stream_id, RELAY_COMMAND_CONNECTED, "Data1234");
if (circ->base_.purpose == CIRCUIT_PURPOSE_PATH_BIAS_TESTING) if (circ->base_.purpose == CIRCUIT_PURPOSE_PATH_BIAS_TESTING)
pathbias_count_valid_cells(TO_CIRCUIT(circ), &cell); pathbias_count_valid_cells(TO_CIRCUIT(circ), &cell);
@ -373,7 +363,6 @@ subtest_circbw_halfclosed(origin_circuit_t *circ, streamid_t init_id)
/* DATA and SENDME after END cell */ /* DATA and SENDME after END cell */
ENTRY_TO_CONN(entryconn3)->marked_for_close = 0; ENTRY_TO_CONN(entryconn3)->marked_for_close = 0;
ENTRY_TO_CONN(entryconn3)->outbuf_flushlen = 0;
PACK_CELL(edgeconn->stream_id, RELAY_COMMAND_END, "Data1234"); PACK_CELL(edgeconn->stream_id, RELAY_COMMAND_END, "Data1234");
if (circ->base_.purpose == CIRCUIT_PURPOSE_PATH_BIAS_TESTING) if (circ->base_.purpose == CIRCUIT_PURPOSE_PATH_BIAS_TESTING)
pathbias_count_valid_cells(TO_CIRCUIT(circ), &cell); pathbias_count_valid_cells(TO_CIRCUIT(circ), &cell);
@ -383,7 +372,6 @@ subtest_circbw_halfclosed(origin_circuit_t *circ, streamid_t init_id)
ASSERT_COUNTED_BW(); ASSERT_COUNTED_BW();
ENTRY_TO_CONN(entryconn3)->marked_for_close = 0; ENTRY_TO_CONN(entryconn3)->marked_for_close = 0;
ENTRY_TO_CONN(entryconn3)->outbuf_flushlen = 0;
PACK_CELL(edgeconn->stream_id, RELAY_COMMAND_SENDME, "Data1234"); PACK_CELL(edgeconn->stream_id, RELAY_COMMAND_SENDME, "Data1234");
ret = ret =
connection_edge_process_relay_cell(&cell, TO_CIRCUIT(circ), NULL, connection_edge_process_relay_cell(&cell, TO_CIRCUIT(circ), NULL,
@ -392,7 +380,6 @@ subtest_circbw_halfclosed(origin_circuit_t *circ, streamid_t init_id)
ASSERT_UNCOUNTED_BW(); ASSERT_UNCOUNTED_BW();
ENTRY_TO_CONN(entryconn3)->marked_for_close = 0; ENTRY_TO_CONN(entryconn3)->marked_for_close = 0;
ENTRY_TO_CONN(entryconn3)->outbuf_flushlen = 0;
PACK_CELL(edgeconn->stream_id, RELAY_COMMAND_DATA, "Data1234"); PACK_CELL(edgeconn->stream_id, RELAY_COMMAND_DATA, "Data1234");
if (circ->base_.purpose == CIRCUIT_PURPOSE_PATH_BIAS_TESTING) if (circ->base_.purpose == CIRCUIT_PURPOSE_PATH_BIAS_TESTING)
pathbias_count_valid_cells(TO_CIRCUIT(circ), &cell); pathbias_count_valid_cells(TO_CIRCUIT(circ), &cell);
@ -407,11 +394,9 @@ subtest_circbw_halfclosed(origin_circuit_t *circ, streamid_t init_id)
edgeconn->base_.state = AP_CONN_STATE_RESOLVE_WAIT; edgeconn->base_.state = AP_CONN_STATE_RESOLVE_WAIT;
edgeconn->on_circuit = TO_CIRCUIT(circ); edgeconn->on_circuit = TO_CIRCUIT(circ);
ENTRY_TO_CONN(entryconn4)->marked_for_close = 0; ENTRY_TO_CONN(entryconn4)->marked_for_close = 0;
ENTRY_TO_CONN(entryconn4)->outbuf_flushlen = 0;
connection_edge_reached_eof(edgeconn); connection_edge_reached_eof(edgeconn);
ENTRY_TO_CONN(entryconn4)->marked_for_close = 0; ENTRY_TO_CONN(entryconn4)->marked_for_close = 0;
ENTRY_TO_CONN(entryconn4)->outbuf_flushlen = 0;
PACK_CELL(edgeconn->stream_id, RELAY_COMMAND_RESOLVED, PACK_CELL(edgeconn->stream_id, RELAY_COMMAND_RESOLVED,
"\x04\x04\x12\x00\x00\x01\x00\x00\x02\x00"); "\x04\x04\x12\x00\x00\x01\x00\x00\x02\x00");
if (circ->base_.purpose == CIRCUIT_PURPOSE_PATH_BIAS_TESTING) if (circ->base_.purpose == CIRCUIT_PURPOSE_PATH_BIAS_TESTING)
@ -422,7 +407,6 @@ subtest_circbw_halfclosed(origin_circuit_t *circ, streamid_t init_id)
ASSERT_COUNTED_BW(); ASSERT_COUNTED_BW();
ENTRY_TO_CONN(entryconn4)->marked_for_close = 0; ENTRY_TO_CONN(entryconn4)->marked_for_close = 0;
ENTRY_TO_CONN(entryconn4)->outbuf_flushlen = 0;
PACK_CELL(edgeconn->stream_id, RELAY_COMMAND_RESOLVED, PACK_CELL(edgeconn->stream_id, RELAY_COMMAND_RESOLVED,
"\x04\x04\x12\x00\x00\x01\x00\x00\x02\x00"); "\x04\x04\x12\x00\x00\x01\x00\x00\x02\x00");
connection_edge_process_relay_cell(&cell, TO_CIRCUIT(circ), NULL, connection_edge_process_relay_cell(&cell, TO_CIRCUIT(circ), NULL,
@ -431,7 +415,6 @@ subtest_circbw_halfclosed(origin_circuit_t *circ, streamid_t init_id)
/* Data not counted after resolved */ /* Data not counted after resolved */
ENTRY_TO_CONN(entryconn4)->marked_for_close = 0; ENTRY_TO_CONN(entryconn4)->marked_for_close = 0;
ENTRY_TO_CONN(entryconn4)->outbuf_flushlen = 0;
PACK_CELL(edgeconn->stream_id, RELAY_COMMAND_DATA, "Data1234"); PACK_CELL(edgeconn->stream_id, RELAY_COMMAND_DATA, "Data1234");
if (circ->base_.purpose == CIRCUIT_PURPOSE_PATH_BIAS_TESTING) if (circ->base_.purpose == CIRCUIT_PURPOSE_PATH_BIAS_TESTING)
pathbias_count_valid_cells(TO_CIRCUIT(circ), &cell); pathbias_count_valid_cells(TO_CIRCUIT(circ), &cell);
@ -442,7 +425,6 @@ subtest_circbw_halfclosed(origin_circuit_t *circ, streamid_t init_id)
/* End not counted after resolved */ /* End not counted after resolved */
ENTRY_TO_CONN(entryconn4)->marked_for_close = 0; ENTRY_TO_CONN(entryconn4)->marked_for_close = 0;
ENTRY_TO_CONN(entryconn4)->outbuf_flushlen = 0;
PACK_CELL(edgeconn->stream_id, RELAY_COMMAND_END, "Data1234"); PACK_CELL(edgeconn->stream_id, RELAY_COMMAND_END, "Data1234");
if (circ->base_.purpose == CIRCUIT_PURPOSE_PATH_BIAS_TESTING) if (circ->base_.purpose == CIRCUIT_PURPOSE_PATH_BIAS_TESTING)
pathbias_count_valid_cells(TO_CIRCUIT(circ), &cell); pathbias_count_valid_cells(TO_CIRCUIT(circ), &cell);
@ -660,7 +642,6 @@ test_halfstream_wrap(void *arg)
/* Insert an opened stream on the circ with that id */ /* Insert an opened stream on the circ with that id */
ENTRY_TO_CONN(entryconn)->marked_for_close = 0; ENTRY_TO_CONN(entryconn)->marked_for_close = 0;
ENTRY_TO_CONN(entryconn)->outbuf_flushlen = 0;
edgeconn->base_.state = AP_CONN_STATE_CONNECT_WAIT; edgeconn->base_.state = AP_CONN_STATE_CONNECT_WAIT;
circ->p_streams = edgeconn; circ->p_streams = edgeconn;
@ -784,14 +765,12 @@ test_circbw_relay(void *arg)
/* Sendme on valid stream: counted */ /* Sendme on valid stream: counted */
edgeconn->package_window -= STREAMWINDOW_INCREMENT; edgeconn->package_window -= STREAMWINDOW_INCREMENT;
ENTRY_TO_CONN(entryconn1)->outbuf_flushlen = 0;
PACK_CELL(1, RELAY_COMMAND_SENDME, "Data1234"); PACK_CELL(1, RELAY_COMMAND_SENDME, "Data1234");
connection_edge_process_relay_cell(&cell, TO_CIRCUIT(circ), edgeconn, connection_edge_process_relay_cell(&cell, TO_CIRCUIT(circ), edgeconn,
circ->cpath); circ->cpath);
ASSERT_COUNTED_BW(); ASSERT_COUNTED_BW();
/* Sendme on valid stream with full window: not counted */ /* Sendme on valid stream with full window: not counted */
ENTRY_TO_CONN(entryconn1)->outbuf_flushlen = 0;
PACK_CELL(1, RELAY_COMMAND_SENDME, "Data1234"); PACK_CELL(1, RELAY_COMMAND_SENDME, "Data1234");
edgeconn->package_window = STREAMWINDOW_START; edgeconn->package_window = STREAMWINDOW_START;
connection_edge_process_relay_cell(&cell, TO_CIRCUIT(circ), edgeconn, connection_edge_process_relay_cell(&cell, TO_CIRCUIT(circ), edgeconn,
@ -799,7 +778,6 @@ test_circbw_relay(void *arg)
ASSERT_UNCOUNTED_BW(); ASSERT_UNCOUNTED_BW();
/* Sendme on unknown stream: not counted */ /* Sendme on unknown stream: not counted */
ENTRY_TO_CONN(entryconn1)->outbuf_flushlen = 0;
PACK_CELL(1, RELAY_COMMAND_SENDME, "Data1234"); PACK_CELL(1, RELAY_COMMAND_SENDME, "Data1234");
connection_edge_process_relay_cell(&cell, TO_CIRCUIT(circ), NULL, connection_edge_process_relay_cell(&cell, TO_CIRCUIT(circ), NULL,
circ->cpath); circ->cpath);