flow, tcp: Consolidate flow pointer<->index helpers
Both tcp.c and tcp_splice.c define CONN_IDX() variants to find the index of their connection structures in the connection table, now become the unified flow table. We can easily combine these into a common helper. While we're there, add some trickery for some additional type safety. They also define their own CONN() versions, which aren't so easily combined since they need to return different types, but we can have them use a common helper. In the process, we standardise on always using an unsigned type to store the connection / flow index, which makes more sense. tcp.c's conn_at_idx() remains for now, but we change its parameter to unsigned to match. That in turn means we can remove a check for negative values from it. Signed-off-by: David Gibson <david@gibson.dropbear.id.au> Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
This commit is contained in:
parent
f08ce92a13
commit
e2e8219f13
4 changed files with 69 additions and 46 deletions
25
flow_table.h
25
flow_table.h
|
@ -22,4 +22,29 @@ union flow {
|
|||
/* Global Flow Table */
|
||||
extern union flow flowtab[];
|
||||
|
||||
|
||||
/** flow_idx - Index of flow from common structure
|
||||
* @f: Common flow fields pointer
|
||||
*
|
||||
* Return: index of @f in the flow table
|
||||
*/
|
||||
static inline unsigned flow_idx(const struct flow_common *f)
|
||||
{
|
||||
return (union flow *)f - flowtab;
|
||||
}
|
||||
|
||||
/** FLOW_IDX - Find the index of a flow
|
||||
* @f_: Flow pointer, either union flow * or protocol specific
|
||||
*
|
||||
* Return: index of @f in the flow table
|
||||
*/
|
||||
#define FLOW_IDX(f_) (flow_idx(&(f_)->f))
|
||||
|
||||
/** FLOW - Flow entry at a given index
|
||||
* @idx: Flow index
|
||||
*
|
||||
* Return: pointer to entry @idx in the flow table
|
||||
*/
|
||||
#define FLOW(idx) (&flowtab[(idx)])
|
||||
|
||||
#endif /* FLOW_TABLE_H */
|
||||
|
|
67
tcp.c
67
tcp.c
|
@ -570,17 +570,16 @@ tcp6_l2_flags_buf[TCP_FRAMES_MEM];
|
|||
|
||||
static unsigned int tcp6_l2_flags_buf_used;
|
||||
|
||||
#define CONN(idx) (&flowtab[(idx)].tcp)
|
||||
#define CONN_IDX(conn) ((union flow *)(conn) - flowtab)
|
||||
#define CONN(idx) (&(FLOW(idx)->tcp))
|
||||
|
||||
/** conn_at_idx() - Find a connection by index, if present
|
||||
* @idx: Index of connection to lookup
|
||||
*
|
||||
* Return: pointer to connection, or NULL if @idx is out of bounds
|
||||
*/
|
||||
static inline struct tcp_tap_conn *conn_at_idx(int idx)
|
||||
static inline struct tcp_tap_conn *conn_at_idx(unsigned idx)
|
||||
{
|
||||
if ((idx < 0) || (idx >= FLOW_MAX))
|
||||
if (idx >= FLOW_MAX)
|
||||
return NULL;
|
||||
ASSERT(CONN(idx)->f.type == FLOW_TCP);
|
||||
return CONN(idx);
|
||||
|
@ -640,7 +639,7 @@ static int tcp_epoll_ctl(const struct ctx *c, struct tcp_tap_conn *conn)
|
|||
{
|
||||
int m = conn->in_epoll ? EPOLL_CTL_MOD : EPOLL_CTL_ADD;
|
||||
union epoll_ref ref = { .type = EPOLL_TYPE_TCP, .fd = conn->sock,
|
||||
.tcp.index = CONN_IDX(conn) };
|
||||
.tcp.index = FLOW_IDX(conn) };
|
||||
struct epoll_event ev = { .data.u64 = ref.u64 };
|
||||
|
||||
if (conn->events == CLOSED) {
|
||||
|
@ -661,7 +660,7 @@ static int tcp_epoll_ctl(const struct ctx *c, struct tcp_tap_conn *conn)
|
|||
if (conn->timer != -1) {
|
||||
union epoll_ref ref_t = { .type = EPOLL_TYPE_TCP_TIMER,
|
||||
.fd = conn->sock,
|
||||
.tcp.index = CONN_IDX(conn) };
|
||||
.tcp.index = FLOW_IDX(conn) };
|
||||
struct epoll_event ev_t = { .data.u64 = ref_t.u64,
|
||||
.events = EPOLLIN | EPOLLET };
|
||||
|
||||
|
@ -689,7 +688,7 @@ static void tcp_timer_ctl(const struct ctx *c, struct tcp_tap_conn *conn)
|
|||
if (conn->timer == -1) {
|
||||
union epoll_ref ref = { .type = EPOLL_TYPE_TCP_TIMER,
|
||||
.fd = conn->sock,
|
||||
.tcp.index = CONN_IDX(conn) };
|
||||
.tcp.index = FLOW_IDX(conn) };
|
||||
struct epoll_event ev = { .data.u64 = ref.u64,
|
||||
.events = EPOLLIN | EPOLLET };
|
||||
int fd;
|
||||
|
@ -725,7 +724,7 @@ static void tcp_timer_ctl(const struct ctx *c, struct tcp_tap_conn *conn)
|
|||
it.it_value.tv_sec = ACT_TIMEOUT;
|
||||
}
|
||||
|
||||
debug("TCP: index %li, timer expires in %lu.%03lus", CONN_IDX(conn),
|
||||
debug("TCP: index %u, timer expires in %lu.%03lus", FLOW_IDX(conn),
|
||||
it.it_value.tv_sec, it.it_value.tv_nsec / 1000 / 1000);
|
||||
|
||||
timerfd_settime(conn->timer, 0, &it, NULL);
|
||||
|
@ -748,7 +747,7 @@ static void conn_flag_do(const struct ctx *c, struct tcp_tap_conn *conn,
|
|||
|
||||
conn->flags &= flag;
|
||||
if (flag_index >= 0) {
|
||||
debug("TCP: index %li: %s dropped", CONN_IDX(conn),
|
||||
debug("TCP: index %u: %s dropped", FLOW_IDX(conn),
|
||||
tcp_flag_str[flag_index]);
|
||||
}
|
||||
} else {
|
||||
|
@ -769,7 +768,7 @@ static void conn_flag_do(const struct ctx *c, struct tcp_tap_conn *conn,
|
|||
|
||||
conn->flags |= flag;
|
||||
if (flag_index >= 0) {
|
||||
debug("TCP: index %li: %s", CONN_IDX(conn),
|
||||
debug("TCP: index %u: %s", FLOW_IDX(conn),
|
||||
tcp_flag_str[flag_index]);
|
||||
}
|
||||
}
|
||||
|
@ -819,12 +818,12 @@ static void conn_event_do(const struct ctx *c, struct tcp_tap_conn *conn,
|
|||
new += 5;
|
||||
|
||||
if (prev != new) {
|
||||
debug("TCP: index %li, %s: %s -> %s", CONN_IDX(conn),
|
||||
debug("TCP: index %u, %s: %s -> %s", FLOW_IDX(conn),
|
||||
num == -1 ? "CLOSED" : tcp_event_str[num],
|
||||
prev == -1 ? "CLOSED" : tcp_state_str[prev],
|
||||
(new == -1 || num == -1) ? "CLOSED" : tcp_state_str[new]);
|
||||
} else {
|
||||
debug("TCP: index %li, %s", CONN_IDX(conn),
|
||||
debug("TCP: index %u, %s", FLOW_IDX(conn),
|
||||
num == -1 ? "CLOSED" : tcp_event_str[num]);
|
||||
}
|
||||
|
||||
|
@ -1204,11 +1203,11 @@ static void tcp_hash_insert(const struct ctx *c, struct tcp_tap_conn *conn)
|
|||
int b;
|
||||
|
||||
b = tcp_hash(c, &conn->faddr, conn->eport, conn->fport);
|
||||
conn->next_index = tc_hash[b] ? CONN_IDX(tc_hash[b]) : -1;
|
||||
conn->next_index = tc_hash[b] ? FLOW_IDX(tc_hash[b]) : -1U;
|
||||
tc_hash[b] = conn;
|
||||
|
||||
debug("TCP: hash table insert: index %li, sock %i, bucket: %i, next: "
|
||||
"%p", CONN_IDX(conn), conn->sock, b,
|
||||
debug("TCP: hash table insert: index %u, sock %i, bucket: %i, next: "
|
||||
"%p", FLOW_IDX(conn), conn->sock, b,
|
||||
(void *)conn_at_idx(conn->next_index));
|
||||
}
|
||||
|
||||
|
@ -1234,8 +1233,8 @@ static void tcp_hash_remove(const struct ctx *c,
|
|||
}
|
||||
}
|
||||
|
||||
debug("TCP: hash table remove: index %li, sock %i, bucket: %i, new: %p",
|
||||
CONN_IDX(conn), conn->sock, b,
|
||||
debug("TCP: hash table remove: index %u, sock %i, bucket: %i, new: %p",
|
||||
FLOW_IDX(conn), conn->sock, b,
|
||||
(void *)(prev ? conn_at_idx(prev->next_index) : tc_hash[b]));
|
||||
}
|
||||
|
||||
|
@ -1255,16 +1254,16 @@ static void tcp_tap_conn_update(const struct ctx *c, struct tcp_tap_conn *old,
|
|||
prev = entry, entry = conn_at_idx(entry->next_index)) {
|
||||
if (entry == old) {
|
||||
if (prev)
|
||||
prev->next_index = CONN_IDX(new);
|
||||
prev->next_index = FLOW_IDX(new);
|
||||
else
|
||||
tc_hash[b] = new;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
debug("TCP: hash table update: old index %li, new index %li, sock %i, "
|
||||
debug("TCP: hash table update: old index %u, new index %u, sock %i, "
|
||||
"bucket: %i, old: %p, new: %p",
|
||||
CONN_IDX(old), CONN_IDX(new), new->sock, b,
|
||||
FLOW_IDX(old), FLOW_IDX(new), new->sock, b,
|
||||
(void *)old, (void *)new);
|
||||
|
||||
tcp_epoll_ctl(c, new);
|
||||
|
@ -1307,9 +1306,9 @@ void tcp_table_compact(struct ctx *c, union flow *hole)
|
|||
{
|
||||
union flow *from;
|
||||
|
||||
if (CONN_IDX(hole) == --c->flow_count) {
|
||||
debug("TCP: table compaction: maximum index was %li (%p)",
|
||||
CONN_IDX(hole), (void *)hole);
|
||||
if (FLOW_IDX(hole) == --c->flow_count) {
|
||||
debug("TCP: table compaction: maximum index was %u (%p)",
|
||||
FLOW_IDX(hole), (void *)hole);
|
||||
memset(hole, 0, sizeof(*hole));
|
||||
return;
|
||||
}
|
||||
|
@ -1329,9 +1328,9 @@ void tcp_table_compact(struct ctx *c, union flow *hole)
|
|||
FLOW_TYPE(&from->f));
|
||||
}
|
||||
|
||||
debug("TCP: table compaction (%s): old index %li, new index %li, "
|
||||
debug("TCP: table compaction (%s): old index %u, new index %u, "
|
||||
"from: %p, to: %p",
|
||||
FLOW_TYPE(&from->f), CONN_IDX(from), CONN_IDX(hole),
|
||||
FLOW_TYPE(&from->f), FLOW_IDX(from), FLOW_IDX(hole),
|
||||
(void *)from, (void *)hole);
|
||||
|
||||
memset(from, 0, sizeof(*from));
|
||||
|
@ -1357,7 +1356,7 @@ static void tcp_conn_destroy(struct ctx *c, union flow *flow)
|
|||
static void tcp_rst_do(struct ctx *c, struct tcp_tap_conn *conn);
|
||||
#define tcp_rst(c, conn) \
|
||||
do { \
|
||||
debug("TCP: index %li, reset at %s:%i", CONN_IDX(conn), \
|
||||
debug("TCP: index %u, reset at %s:%i", FLOW_IDX(conn), \
|
||||
__func__, __LINE__); \
|
||||
tcp_rst_do(c, conn); \
|
||||
} while (0)
|
||||
|
@ -2580,8 +2579,8 @@ int tcp_tap_handler(struct ctx *c, uint8_t pif, int af,
|
|||
return 1;
|
||||
}
|
||||
|
||||
trace("TCP: packet length %zu from tap for index %lu",
|
||||
len, CONN_IDX(conn));
|
||||
trace("TCP: packet length %zu from tap for index %u",
|
||||
len, FLOW_IDX(conn));
|
||||
|
||||
if (th->rst) {
|
||||
conn_event(c, conn, CLOSED);
|
||||
|
@ -2821,17 +2820,17 @@ void tcp_timer_handler(struct ctx *c, union epoll_ref ref)
|
|||
tcp_timer_ctl(c, conn);
|
||||
} else if (conn->flags & ACK_FROM_TAP_DUE) {
|
||||
if (!(conn->events & ESTABLISHED)) {
|
||||
debug("TCP: index %li, handshake timeout", CONN_IDX(conn));
|
||||
debug("TCP: index %u, handshake timeout", FLOW_IDX(conn));
|
||||
tcp_rst(c, conn);
|
||||
} else if (CONN_HAS(conn, SOCK_FIN_SENT | TAP_FIN_ACKED)) {
|
||||
debug("TCP: index %li, FIN timeout", CONN_IDX(conn));
|
||||
debug("TCP: index %u, FIN timeout", FLOW_IDX(conn));
|
||||
tcp_rst(c, conn);
|
||||
} else if (conn->retrans == TCP_MAX_RETRANS) {
|
||||
debug("TCP: index %li, retransmissions count exceeded",
|
||||
CONN_IDX(conn));
|
||||
debug("TCP: index %u, retransmissions count exceeded",
|
||||
FLOW_IDX(conn));
|
||||
tcp_rst(c, conn);
|
||||
} else {
|
||||
debug("TCP: index %li, ACK timeout, retry", CONN_IDX(conn));
|
||||
debug("TCP: index %u, ACK timeout, retry", FLOW_IDX(conn));
|
||||
conn->retrans++;
|
||||
conn->seq_to_tap = conn->seq_ack_from_tap;
|
||||
tcp_data_from_sock(c, conn);
|
||||
|
@ -2849,7 +2848,7 @@ void tcp_timer_handler(struct ctx *c, union epoll_ref ref)
|
|||
*/
|
||||
timerfd_settime(conn->timer, 0, &new, &old);
|
||||
if (old.it_value.tv_sec == ACT_TIMEOUT) {
|
||||
debug("TCP: index %li, activity timeout", CONN_IDX(conn));
|
||||
debug("TCP: index %u, activity timeout", FLOW_IDX(conn));
|
||||
tcp_rst(c, conn);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -40,7 +40,7 @@ struct tcp_tap_conn {
|
|||
struct flow_common f;
|
||||
|
||||
bool in_epoll :1;
|
||||
int next_index :FLOW_INDEX_BITS + 2;
|
||||
unsigned next_index :FLOW_INDEX_BITS + 2;
|
||||
|
||||
#define TCP_RETRANS_BITS 3
|
||||
unsigned int retrans :TCP_RETRANS_BITS;
|
||||
|
|
21
tcp_splice.c
21
tcp_splice.c
|
@ -76,8 +76,7 @@ static int splice_pipe_pool [TCP_SPLICE_PIPE_POOL_SIZE][2];
|
|||
#define CONN_V6(x) (x->flags & SPLICE_V6)
|
||||
#define CONN_V4(x) (!CONN_V6(x))
|
||||
#define CONN_HAS(conn, set) ((conn->events & (set)) == (set))
|
||||
#define CONN(idx) (&tc[(idx)].splice)
|
||||
#define CONN_IDX(conn) ((union flow *)(conn) - flowtab)
|
||||
#define CONN(idx) (&FLOW(idx)->tcp_splice)
|
||||
|
||||
/* Display strings for connection events */
|
||||
static const char *tcp_splice_event_str[] __attribute((__unused__)) = {
|
||||
|
@ -129,8 +128,8 @@ static int tcp_splice_epoll_ctl(const struct ctx *c,
|
|||
{
|
||||
int m = conn->in_epoll ? EPOLL_CTL_MOD : EPOLL_CTL_ADD;
|
||||
union epoll_ref ref[SIDES] = {
|
||||
{ .type = EPOLL_TYPE_TCP, .fd = conn->s[0], .tcp.index = CONN_IDX(conn) },
|
||||
{ .type = EPOLL_TYPE_TCP, .fd = conn->s[1], .tcp.index = CONN_IDX(conn) }
|
||||
{ .type = EPOLL_TYPE_TCP, .fd = conn->s[0], .tcp.index = FLOW_IDX(conn) },
|
||||
{ .type = EPOLL_TYPE_TCP, .fd = conn->s[1], .tcp.index = FLOW_IDX(conn) }
|
||||
};
|
||||
struct epoll_event ev[SIDES] = { { .data.u64 = ref[0].u64 },
|
||||
{ .data.u64 = ref[1].u64 } };
|
||||
|
@ -140,8 +139,8 @@ static int tcp_splice_epoll_ctl(const struct ctx *c,
|
|||
if (epoll_ctl(c->epollfd, m, conn->s[0], &ev[0]) ||
|
||||
epoll_ctl(c->epollfd, m, conn->s[1], &ev[1])) {
|
||||
int ret = -errno;
|
||||
err("TCP (spliced): index %li, ERROR on epoll_ctl(): %s",
|
||||
CONN_IDX(conn), strerror(errno));
|
||||
err("TCP (spliced): index %u, ERROR on epoll_ctl(): %s",
|
||||
FLOW_IDX(conn), strerror(errno));
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
@ -167,7 +166,7 @@ static void conn_flag_do(const struct ctx *c, struct tcp_splice_conn *conn,
|
|||
|
||||
conn->flags &= flag;
|
||||
if (flag_index >= 0) {
|
||||
debug("TCP (spliced): index %li: %s dropped", CONN_IDX(conn),
|
||||
debug("TCP (spliced): index %u: %s dropped", FLOW_IDX(conn),
|
||||
tcp_splice_flag_str[flag_index]);
|
||||
}
|
||||
} else {
|
||||
|
@ -178,7 +177,7 @@ static void conn_flag_do(const struct ctx *c, struct tcp_splice_conn *conn,
|
|||
|
||||
conn->flags |= flag;
|
||||
if (flag_index >= 0) {
|
||||
debug("TCP (spliced): index %li: %s", CONN_IDX(conn),
|
||||
debug("TCP (spliced): index %u: %s", FLOW_IDX(conn),
|
||||
tcp_splice_flag_str[flag_index]);
|
||||
}
|
||||
}
|
||||
|
@ -213,7 +212,7 @@ static void conn_event_do(const struct ctx *c, struct tcp_splice_conn *conn,
|
|||
|
||||
conn->events &= event;
|
||||
if (flag_index >= 0) {
|
||||
debug("TCP (spliced): index %li, ~%s", CONN_IDX(conn),
|
||||
debug("TCP (spliced): index %u, ~%s", FLOW_IDX(conn),
|
||||
tcp_splice_event_str[flag_index]);
|
||||
}
|
||||
} else {
|
||||
|
@ -224,7 +223,7 @@ static void conn_event_do(const struct ctx *c, struct tcp_splice_conn *conn,
|
|||
|
||||
conn->events |= event;
|
||||
if (flag_index >= 0) {
|
||||
debug("TCP (spliced): index %li, %s", CONN_IDX(conn),
|
||||
debug("TCP (spliced): index %u, %s", FLOW_IDX(conn),
|
||||
tcp_splice_event_str[flag_index]);
|
||||
}
|
||||
}
|
||||
|
@ -282,7 +281,7 @@ void tcp_splice_destroy(struct ctx *c, union flow *flow)
|
|||
|
||||
conn->events = SPLICE_CLOSED;
|
||||
conn->flags = 0;
|
||||
debug("TCP (spliced): index %li, CLOSED", CONN_IDX(conn));
|
||||
debug("TCP (spliced): index %u, CLOSED", FLOW_IDX(conn));
|
||||
|
||||
tcp_table_compact(c, flow);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue