Use the new inline max() function instead of MAX() macro function in sslproxy

Do not pass pxy_thr_print_children() or bufferevent_getfd() to MAX() or
util_max() macro functions as params, or else they are called twice.
Since MAX() macro call duplicates params, do not call it nested either,
or else we get very long macro expansions.
pull/48/head
Soner Tari 4 years ago
parent e63d6dd3aa
commit be80523036

@ -54,6 +54,7 @@
#define MALLOC __attribute__((malloc)) WUNRES
#define NONNULL(...) __attribute__((nonnull(__VA_ARGS__)))
#define PURE __attribute__((pure))
#define INLINE __attribute__((always_inline))
/*
* Branch prediction macros.

@ -41,6 +41,7 @@
#include "log.h"
#include "attrib.h"
#include "proc.h"
#include "util.h"
#include <string.h>
#include <arpa/inet.h>
@ -178,7 +179,7 @@ pxy_conn_attach_child(pxy_conn_child_ctx_t *ctx)
// @attention Child connections use the parent's event bases, otherwise we would get multithreading issues
// Always keep thr load and conns list in sync
ctx->conn->thr->load++;
ctx->conn->thr->max_load = MAX(ctx->conn->thr->max_load, ctx->conn->thr->load);
ctx->conn->thr->max_load = max(ctx->conn->thr->max_load, ctx->conn->thr->load);
// Prepend child to the children list of parent
ctx->next = ctx->conn->children;
@ -1004,7 +1005,7 @@ pxy_listener_acceptcb_child(UNUSED struct evconnlistener *listener, evutil_socke
}
// @attention fd (child_ctx->fd) is different from child event listener fd (ctx->child_fd)
ctx->thr->max_fd = MAX(ctx->thr->max_fd, child_ctx->fd);
ctx->thr->max_fd = max(ctx->thr->max_fd, child_ctx->fd);
ctx->child_src_fd = child_ctx->fd;
/* create server-side socket and eventbuffer */
@ -1036,7 +1037,7 @@ pxy_listener_acceptcb_child(UNUSED struct evconnlistener *listener, evutil_socke
child_ctx->dst_fd = bufferevent_getfd(child_ctx->dst.bev);
ctx->child_dst_fd = child_ctx->dst_fd;
ctx->thr->max_fd = MAX(ctx->thr->max_fd, child_ctx->dst_fd);
ctx->thr->max_fd = max(ctx->thr->max_fd, child_ctx->dst_fd);
// Do not return here, but continue and check term/enomem flags below
out:
// @attention Do not use child_ctx->conn here, child_ctx may be uninitialized
@ -1104,7 +1105,7 @@ pxy_setup_child_listener(pxy_conn_ctx_t *ctx)
pxy_conn_term(ctx, 1);
return -1;
}
ctx->thr->max_fd = MAX(ctx->thr->max_fd, ctx->child_fd);
ctx->thr->max_fd = max(ctx->thr->max_fd, ctx->child_fd);
// @attention Do not pass NULL as user-supplied pointer
struct evconnlistener *child_evcl = evconnlistener_new(ctx->thr->evbase, pxy_listener_acceptcb_child, ctx, LEV_OPT_CLOSE_ON_FREE, 1024, ctx->child_fd);
@ -1421,17 +1422,17 @@ pxy_bev_eventcb_postexec_logging_and_stats(struct bufferevent *bev, short events
}
if (bev == ctx->srvdst.bev) {
ctx->thr->max_load = MAX(ctx->thr->max_load, ctx->thr->load);
ctx->thr->max_fd = MAX(ctx->thr->max_fd, ctx->fd);
ctx->thr->max_load = max(ctx->thr->max_load, ctx->thr->load);
ctx->thr->max_fd = max(ctx->thr->max_fd, ctx->fd);
// src and other fd stats are collected in acceptcb functions
ctx->srvdst_fd = bufferevent_getfd(ctx->srvdst.bev);
ctx->thr->max_fd = MAX(ctx->thr->max_fd, ctx->srvdst_fd);
ctx->thr->max_fd = max(ctx->thr->max_fd, ctx->srvdst_fd);
// Passthrough proto may have a NULL dst.bev
if (ctx->dst.bev) {
ctx->dst_fd = bufferevent_getfd(ctx->dst.bev);
ctx->thr->max_fd = MAX(ctx->thr->max_fd, ctx->dst_fd);
ctx->thr->max_fd = max(ctx->thr->max_fd, ctx->dst_fd);
}
}
}
@ -1469,7 +1470,7 @@ void
pxy_bev_eventcb_postexec_stats_child(short events, pxy_conn_child_ctx_t *ctx)
{
if (events & BEV_EVENT_CONNECTED) {
ctx->conn->thr->max_fd = MAX(ctx->conn->thr->max_fd, MAX(bufferevent_getfd(ctx->src.bev), bufferevent_getfd(ctx->dst.bev)));
ctx->conn->thr->max_fd = max(ctx->conn->thr->max_fd, max(bufferevent_getfd(ctx->src.bev), bufferevent_getfd(ctx->dst.bev)));
}
}

@ -30,9 +30,9 @@
#include "log.h"
#include "pxyconn.h"
#include "util.h"
#include <assert.h>
#include <sys/param.h>
/*
* Attach a connection to its thread.
@ -161,7 +161,7 @@ pxy_thr_print_children(pxy_conn_child_ctx_t *ctx)
// No need to log child stats
log_finest_main_va("CHILD CONN: thr=%d, id=%llu, cid=%d, src=%d, dst=%d, c=%d-%d",
ctx->conn->thr->id, ctx->conn->id, ctx->conn->child_count, ctx->fd, ctx->dst_fd, ctx->src.closed, ctx->dst.closed);
max_fd = MAX(max_fd, MAX(ctx->fd, ctx->dst_fd));
max_fd = max(max_fd, max(ctx->fd, ctx->dst_fd));
ctx = ctx->next;
}
return max_fd;
@ -211,12 +211,14 @@ pxy_thr_print_info(pxy_thr_ctx_t *tctx)
// child_src_fd and child_dst_fd fields are mostly for debugging purposes, used in debug printing parent conns.
// However, while an ssl child is closing, the children list may be empty, but child's ssl fd may be still open,
// hence we include those fields in this max comparisons too
max_fd = MAX(max_fd, MAX(ctx->fd, MAX(ctx->dst_fd, MAX(ctx->srvdst_fd, MAX(ctx->child_fd, MAX(ctx->child_src_fd, ctx->child_dst_fd))))));
max_atime = MAX(max_atime, atime);
max_ctime = MAX(max_ctime, ctime);
max_fd = max(max_fd, max(ctx->fd, max(ctx->dst_fd, max(ctx->srvdst_fd, max(ctx->child_fd, max(ctx->child_src_fd, ctx->child_dst_fd))))));
max_atime = util_max(max_atime, atime);
max_ctime = util_max(max_ctime, ctime);
if (ctx->children) {
max_fd = MAX(max_fd, pxy_thr_print_children(ctx->children));
// @attention Do not pass pxy_thr_print_children() to MAX() or util_max() macro functions as param, or else it is called twice
// Use the inline max() function instead
max_fd = max(max_fd, pxy_thr_print_children(ctx->children));
}
ctx = ctx->next;
}

@ -35,6 +35,12 @@ char * util_skipws(const char *) NONNULL(1) PURE;
#define util_max(a,b) ((a) > (b) ? (a) : (b))
inline int INLINE
max(int a, int b)
{
return a > b ? a : b;
}
#endif /* !UTIL_H */
/* vim: set noet ft=c: */

@ -103,7 +103,8 @@ pxy_thr_print_thr_info(pxy_thr_ctx_t *tctx)
tctx->id, ctx->id, ctx->fd, ctx->dst_fd, ctx->src.closed, ctx->dst.closed, (long long)atime, (long long)ctime,
STRORDASH(ctx->srchost_str), STRORDASH(ctx->srcport_str), STRORDASH(ctx->dsthost_str), STRORDASH(ctx->dstport_str));
max_fd = MAX(max_fd, MAX(ctx->fd, ctx->dst_fd));
max_fd = MAX(max_fd, ctx->fd);
max_fd = MAX(max_fd, ctx->dst_fd);
max_atime = MAX(max_atime, atime);
max_ctime = MAX(max_ctime, ctime);

Loading…
Cancel
Save