Skip to content

Commit

Permalink
log: net: Convert syslog network transmitter to use sockets
Browse files Browse the repository at this point in the history
Convert the network log backend to use socket API instead of
net_context API. This allows the backend to be used also
with socket offloading network drivers.

Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
  • Loading branch information
jukkar authored and dleach02 committed Feb 1, 2024
1 parent 81a1826 commit 508a261
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 55 deletions.
3 changes: 2 additions & 1 deletion samples/net/syslog_net/prj.conf
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ CONFIG_NET_PKT_RX_COUNT=32
CONFIG_NET_PKT_TX_COUNT=32
CONFIG_NET_BUF_RX_COUNT=32
CONFIG_NET_BUF_TX_COUNT=32
CONFIG_NET_SOCKETS=y

CONFIG_NET_IF_UNICAST_IPV6_ADDR_COUNT=3
CONFIG_NET_IF_MCAST_IPV6_ADDR_COUNT=5
Expand Down Expand Up @@ -36,5 +37,5 @@ CONFIG_NET_CONFIG_PEER_IPV4_ADDR="192.0.2.2"
CONFIG_LOG_BACKEND_NET=y
CONFIG_LOG_BACKEND_NET_SERVER="[2001:db8::2]:514"

# Get newlib by default as it has proper time function support
# Get a proper libc by default in order to get working time function support
CONFIG_REQUIRES_FULL_LIBC=y
10 changes: 0 additions & 10 deletions subsys/logging/backends/Kconfig.net
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
config LOG_BACKEND_NET
bool "Networking backend"
depends on NETWORKING && NET_UDP && !LOG_MODE_IMMEDIATE
select NET_CONTEXT_NET_PKT_POOL
select LOG_OUTPUT
help
Send syslog messages to network server.
Expand All @@ -27,15 +26,6 @@ config LOG_BACKEND_NET_SERVER
[2001:db8::2]
2001:db::42

config LOG_BACKEND_NET_MAX_BUF
int "How many network buffers to allocate for sending messages"
range 3 256
default 3
help
Each syslog message should fit into a network packet that will be
sent to server. This number tells how many syslog messages can be
in transit to the server.

config LOG_BACKEND_NET_MAX_BUF_SIZE
int "Max syslog message size"
range 64 1180
Expand Down
86 changes: 42 additions & 44 deletions subsys/logging/backends/log_backend_net.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ LOG_MODULE_REGISTER(log_backend_net, CONFIG_LOG_DEFAULT_LEVEL);
#include <zephyr/logging/log_core.h>
#include <zephyr/logging/log_output.h>
#include <zephyr/logging/log_backend_net.h>
#include <zephyr/net/net_pkt.h>
#include <zephyr/net/net_context.h>
#include <zephyr/net/socket.h>

/* Set this to 1 if you want to see what is being sent to server */
#define DEBUG_PRINTING 0
Expand All @@ -37,34 +36,24 @@ struct sockaddr server_addr;
static bool panic_mode;
static uint32_t log_format_current = CONFIG_LOG_BACKEND_NET_OUTPUT_DEFAULT;

const struct log_backend *log_backend_net_get(void);

NET_PKT_SLAB_DEFINE(syslog_tx_pkts, CONFIG_LOG_BACKEND_NET_MAX_BUF);
NET_PKT_DATA_POOL_DEFINE(syslog_tx_bufs,
ROUND_UP(CONFIG_LOG_BACKEND_NET_MAX_BUF_SIZE /
CONFIG_NET_BUF_DATA_SIZE, 1) *
CONFIG_LOG_BACKEND_NET_MAX_BUF);

static struct k_mem_slab *get_tx_slab(void)
{
return &syslog_tx_pkts;
}
static struct log_backend_net_ctx {
int sock;
} ctx = {
.sock = -1,
};

struct net_buf_pool *get_data_pool(void)
{
return &syslog_tx_bufs;
}
const struct log_backend *log_backend_net_get(void);

static int line_out(uint8_t *data, size_t length, void *output_ctx)
{
struct net_context *ctx = (struct net_context *)output_ctx;
struct log_backend_net_ctx *ctx = (struct log_backend_net_ctx *)output_ctx;
int ret = -ENOMEM;

if (ctx == NULL) {
return length;
}

ret = net_context_send(ctx, data, length, NULL, K_NO_WAIT, NULL);
ret = zsock_send(ctx->sock, data, length, ZSOCK_MSG_DONTWAIT);
if (ret < 0) {
goto fail;
}
Expand All @@ -76,13 +65,12 @@ static int line_out(uint8_t *data, size_t length, void *output_ctx)

LOG_OUTPUT_DEFINE(log_output_net, line_out, output_buf, sizeof(output_buf));

static int do_net_init(void)
static int do_net_init(struct log_backend_net_ctx *ctx)
{
struct sockaddr *local_addr = NULL;
struct sockaddr_in6 local_addr6 = {0};
struct sockaddr_in local_addr4 = {0};
socklen_t server_addr_len;
struct net_context *ctx;
int ret;

if (IS_ENABLED(CONFIG_NET_IPV4) && server_addr.sa_family == AF_INET) {
Expand All @@ -104,13 +92,15 @@ static int do_net_init(void)

local_addr->sa_family = server_addr.sa_family;

ret = net_context_get(server_addr.sa_family, SOCK_DGRAM, IPPROTO_UDP,
&ctx);
ret = zsock_socket(server_addr.sa_family, SOCK_DGRAM, IPPROTO_UDP);
if (ret < 0) {
DBG("Cannot get context (%d)\n", ret);
ret = -errno;
DBG("Cannot get socket (%d)\n", ret);
return ret;
}

ctx->sock = ret;

if (IS_ENABLED(CONFIG_NET_HOSTNAME_ENABLE)) {
(void)strncpy(dev_hostname, net_hostname_get(), MAX_HOSTNAME_LEN);

Expand Down Expand Up @@ -147,30 +137,35 @@ static int do_net_init(void)

} else {
unknown:
DBG("Cannot setup local context\n");
return -EINVAL;
DBG("Cannot setup local socket\n");
ret = -EINVAL;
goto err;
}

ret = net_context_bind(ctx, local_addr, server_addr_len);
ret = zsock_bind(ctx->sock, local_addr, server_addr_len);
if (ret < 0) {
DBG("Cannot bind context (%d)\n", ret);
return ret;
ret = -errno;
DBG("Cannot bind socket (%d)\n", ret);
goto err;
}

(void)net_context_connect(ctx, &server_addr, server_addr_len,
NULL, K_NO_WAIT, NULL);

/* We do not care about return value for this UDP connect call that
* basically does nothing. Calling the connect is only useful so that
* we can see the syslog connection in net-shell.
*/

net_context_setup_pools(ctx, get_tx_slab, get_data_pool);
ret = zsock_connect(ctx->sock, &server_addr, server_addr_len);
if (ret < 0) {
ret = -errno;
DBG("Cannot connect socket (%d)\n", ret);
goto err;
}

log_output_ctx_set(&log_output_net, ctx);
log_output_hostname_set(&log_output_net, dev_hostname);

return 0;

err:
(void)zsock_close(ctx->sock);
ctx->sock = -1;

return ret;
}

static void process(const struct log_backend *const backend,
Expand All @@ -182,7 +177,7 @@ static void process(const struct log_backend *const backend,
return;
}

if (!net_init_done && do_net_init() == 0) {
if (!net_init_done && do_net_init(&ctx) == 0) {
net_init_done = true;
}

Expand All @@ -205,19 +200,23 @@ bool log_backend_net_set_addr(const char *addr)
/* Release context so it can be recreated with the specified ip address
* next time process() is called
*/
int released = net_context_put(log_output_net.control_block->ctx);
struct log_backend_net_ctx *ctx = log_output_net.control_block->ctx;
int released;

released = zsock_close(ctx->sock);
if (released < 0) {
LOG_ERR("Cannot release context (%d)", ret);
LOG_ERR("Cannot release socket (%d)", ret);
ret = false;
} else {
/* The context is successfully released so we flag it
/* The socket is successfully closed so we flag it
* to be recreated with the new ip address
*/
net_init_done = false;
ret = true;
}

ctx->sock = -1;

if (!ret) {
return ret;
}
Expand All @@ -226,7 +225,6 @@ bool log_backend_net_set_addr(const char *addr)
net_sin(&server_addr)->sin_port = htons(514);

ret = net_ipaddr_parse(addr, strlen(addr), &server_addr);

if (!ret) {
LOG_ERR("Cannot parse syslog server address");
return ret;
Expand Down

0 comments on commit 508a261

Please sign in to comment.