Skip to content

Commit 117cd6d

Browse files
committed
fix: configuration parsing error mmap_log_buffer_size
1 parent 11853e1 commit 117cd6d

File tree

6 files changed

+722
-151
lines changed

6 files changed

+722
-151
lines changed

fw/access_log.c

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ static TfwMmapBufferHolder *mmap_buffer;
7979
static DEFINE_PER_CPU_ALIGNED(char[ACCESS_LOG_BUF_SIZE], access_log_buf);
8080
static DEFINE_PER_CPU_ALIGNED(u64, mmap_log_dropped);
8181
static long mmap_log_buffer_size;
82+
static char *tfw_logger_config_path = NULL;
8283

8384
/** Build string consists of chunks that belong to the header value.
8485
* If header value is empty, then it returns "-" to be nginx-like.
@@ -576,7 +577,21 @@ cfg_access_log_set(TfwCfgSpec *cs, TfwCfgEntry *ce)
576577
bool off = false;
577578

578579
TFW_CFG_CHECK_VAL_N(>, 0, cs, ce);
579-
TFW_CFG_CHECK_NO_ATTRS(cs, ce);
580+
581+
// Check for logger_config attribute
582+
const char *logger_config = tfw_cfg_get_attr(ce, "logger_config", NULL);
583+
if (logger_config) {
584+
// Save the logger config path for later use
585+
if (tfw_logger_config_path)
586+
kfree(tfw_logger_config_path);
587+
tfw_logger_config_path = kstrdup(logger_config, GFP_KERNEL);
588+
if (!tfw_logger_config_path)
589+
return -ENOMEM;
590+
591+
// Set access_log type to mmap since we're using external config
592+
access_log_type |= ACCESS_LOG_MMAP;
593+
return 0;
594+
}
580595

581596
TFW_CFG_ENTRY_FOR_EACH_VAL(ce, i, val) {
582597
if (strcasecmp(val, "off") == 0) {
@@ -596,6 +611,19 @@ cfg_access_log_set(TfwCfgSpec *cs, TfwCfgEntry *ce)
596611
return -EINVAL;
597612
}
598613

614+
// Parse other attributes if using mmap
615+
if (access_log_type & ACCESS_LOG_MMAP) {
616+
mmap_host = tfw_cfg_get_attr(ce, "mmap_host", NULL);
617+
mmap_log = tfw_cfg_get_attr(ce, "mmap_log", NULL);
618+
mmap_user = tfw_cfg_get_attr(ce, "mmap_user", NULL);
619+
mmap_password = tfw_cfg_get_attr(ce, "mmap_password", NULL);
620+
621+
if (!mmap_host || !mmap_log) {
622+
T_ERR_NL("if mmap is enabled in access log, there have to be mmap_host and mmap_log options\n");
623+
return -EINVAL;
624+
}
625+
}
626+
599627
return 0;
600628
}
601629

@@ -620,6 +648,11 @@ tfw_access_log_start(void)
620648
static void
621649
tfw_access_log_stop(void)
622650
{
651+
if (tfw_logger_config_path) {
652+
kfree(tfw_logger_config_path);
653+
tfw_logger_config_path = NULL;
654+
}
655+
623656
tfw_mmap_buffer_free(mmap_buffer);
624657
mmap_buffer = NULL;
625658
}

scripts/tempesta.sh

Lines changed: 49 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -126,19 +126,30 @@ templater()
126126
fi
127127
done < "$tfw_cfg_path"
128128

129-
opts=$(get_opts "$cfg_content" "access_log")
130-
while read -r line; do
131-
if [ $(opt_exists "$line" "mmap"; echo $?) -ne 0 ]; then
132-
tfw_logger_should_start=1
133-
mmap_log=$(get_opt_value "$line" "mmap_log")
134-
mmap_host=$(get_opt_value "$line" "mmap_host")
135-
mmap_user=$(get_opt_value "$line" "mmap_user")
136-
mmap_password=$(get_opt_value "$line" "mmap_password")
137-
138-
[[ -n "$mmap_log" && -n "$mmap_host" ]] ||
139-
error "if mmaps enabled in access log, there have to be mmap_host and mmap_log options"
140-
fi
141-
done <<< "$opts"
129+
tfw_logger_config_path=$(echo "$cfg_content" | grep -oP 'access_log\s+.*logger_config=\K[^;]*' | sed 's/"//g' | sed "s/'//g")
130+
if [ -n "$tfw_logger_config_path" ]; then
131+
tfw_logger_should_start=1
132+
else
133+
opts=$(get_opts "$cfg_content" "access_log")
134+
while read -r line; do
135+
if [ $(opt_exists "$line" "mmap"; echo $?) -ne 0 ]; then
136+
tfw_logger_should_start=1
137+
mmap_log=$(get_opt_value "$line" "mmap_log")
138+
mmap_host=$(get_opt_value "$line" "mmap_host")
139+
mmap_user=$(get_opt_value "$line" "mmap_user")
140+
mmap_password=$(get_opt_value "$line" "mmap_password")
141+
142+
[[ -n "$mmap_log" && -n "$mmap_host" ]] ||
143+
error "if mmaps enabled in access log, there have to be mmap_host and mmap_log options"
144+
fi
145+
done <<< "$opts"
146+
fi
147+
148+
# Check for buffer size
149+
buffer_size=$(echo "$cfg_content" | grep -oP 'mmap_log_buffer_size\s+\K[^;]*')
150+
if [ -n "$buffer_size" ]; then
151+
mmap_log_buffer_size=$buffer_size
152+
fi
142153

143154
cfg_content=$(remove_opts_by_mask "$cfg_content" "mmap_")
144155

@@ -311,14 +322,29 @@ start_tfw_logger()
311322
return
312323
fi
313324
314-
if [ -z "$mmap_host" ] || [ -z "$mmap_log" ]; then
315-
error "You need to specify 'mmap_host' and 'mmap_log' "`
316-
`"if access_log mmap was specified"
317-
return
318-
fi
325+
# Check if we have a JSON config path specified
326+
if [ -n "$tfw_logger_config_path" ]; then
327+
echo "...starting tfw_logger with JSON config: $tfw_logger_config_path"
328+
"$utils_path/tfw_logger" --config="$tfw_logger_config_path" || \
329+
error "cannot start tfw_logger daemon"
330+
else
331+
# Traditional approach with command line parameters
332+
if [ -z "$mmap_host" ] || [ -z "$mmap_log" ]; then
333+
error "You need to specify 'mmap_host' and 'mmap_log' if access_log mmap was specified"
334+
return
335+
fi
336+
337+
echo "...starting tfw_logger with command line arguments"
338+
cmd_args="--clickhouse-host=$mmap_host --log-path=$mmap_log"
319339
320-
"$utils_path/tfw_logger" -H "$mmap_host" -l "$mmap_log" -u "$mmap_user" -p "$mmap_password" ||
340+
# Add optional parameters
341+
[ -n "$mmap_user" ] && cmd_args="$cmd_args --clickhouse-user=$mmap_user"
342+
[ -n "$mmap_password" ] && cmd_args="$cmd_args --clickhouse-password=$mmap_password"
343+
[ -n "$mmap_log_buffer_size" ] && cmd_args="$cmd_args --buffer-size=$mmap_log_buffer_size"
344+
345+
"$utils_path/tfw_logger" $cmd_args || \
321346
error "cannot start tfw_logger daemon"
347+
fi
322348
323349
start_time=$(date +%s)
324350
while [[ ! -f "$tfw_logger_pid_path" ]]; do
@@ -329,18 +355,19 @@ start_tfw_logger()
329355
sysctl -e -w net.tempesta.state=stop
330356
unload_modules
331357
tfw_irqbalance_revert
332-
error "tfw_logger failed to start, see $mmap_log for details"
358+
error "tfw_logger failed to start, see logs for details"
333359
fi
334360

335361
sleep 0.1
336362
done
337-
338363
}
339364

340365
stop_tfw_logger()
341366
{
342367
if [ -e $tfw_logger_pid_path ]; then
343-
"$utils_path/tfw_logger" -s
368+
echo "...stopping tfw_logger"
369+
"$utils_path/tfw_logger" --stop || \
370+
echo "Warning: Failed to stop tfw_logger daemon gracefully"
344371
fi
345372
}
346373

utils/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ CLICKHOUSE_BUILD_DIR := $(CLICKHOUSE_DIR)/build
2121
CLICKHOUSE_REPO_URL := https://github.com/ClickHouse/clickhouse-cpp.git
2222

2323
CXX := g++
24-
LDFLAGS := -lfmt -lboost_program_options
24+
LDFLAGS := -lfmt -lboost_program_options -lboost_filesystem
2525
LDFLAGS += -lclickhouse-cpp-lib -L $(CLICKHOUSE_BUILD_DIR)/clickhouse
2626
LDFLAGS += -lcityhash -L $(CLICKHOUSE_BUILD_DIR)/contrib/cityhash/cityhash
2727
LDFLAGS += -lzstdstatic -L $(CLICKHOUSE_BUILD_DIR)/contrib/zstd/zstd
@@ -33,7 +33,7 @@ CXXFLAGS := -O3 -Wall -Wextra -std=c++23 -DPAGE_SIZE=$(PAGE_SIZE)
3333
CXXFLAGS += $(shell pkgconf -cflags spdlog)
3434
CXXFLAGS += -I $(CLICKHOUSE_DIR)
3535
CXXFLAGS += -I $(CLICKHOUSE_DIR)/contrib/absl
36-
SRCS := tfw_logger.cc mmap_buffer.cc clickhouse.cc
36+
SRCS := tfw_logger.cc mmap_buffer.cc clickhouse.cc tfw_logger_config.cc
3737
OBJS := $(SRCS:.cc=.o)
3838
TARGET := tfw_logger
3939

0 commit comments

Comments
 (0)