network/route-nexthop: split out route_nexthops_to_string()

And reorder elements shown in the debugging log.
No effective functionality changed, just refactoring.
This commit is contained in:
Yu Watanabe
2024-01-12 11:07:46 +09:00
parent 54f9fba5e6
commit f47d38c6fc
3 changed files with 77 additions and 33 deletions

View File

@@ -12,6 +12,72 @@
#include "parse-util.h"
#include "string-util.h"
int route_nexthops_to_string(const Route *route, char **ret) {
_cleanup_free_ char *buf = NULL;
int r;
assert(route);
assert(ret);
if (route->nexthop_id != 0) {
if (asprintf(&buf, "nexthop: %"PRIu32, route->nexthop_id) < 0)
return -ENOMEM;
*ret = TAKE_PTR(buf);
return 0;
}
if (route_type_is_reject(route)) {
buf = strdup("gw: n/a");
if (!buf)
return -ENOMEM;
*ret = TAKE_PTR(buf);
return 0;
}
if (ordered_set_isempty(route->multipath_routes)) {
if (in_addr_is_set(route->gw_family, &route->gw))
buf = strjoin("gw: ", IN_ADDR_TO_STRING(route->gw_family, &route->gw));
else if (route->gateway_from_dhcp_or_ra) {
if (route->gw_family == AF_INET)
buf = strdup("gw: _dhcp4");
else if (route->gw_family == AF_INET6)
buf = strdup("gw: _ipv6ra");
else
buf = strdup("gw: _dhcp");
} else
buf = strdup("gw: n/a");
if (!buf)
return -ENOMEM;
*ret = TAKE_PTR(buf);
return 0;
}
MultipathRoute *m;
ORDERED_SET_FOREACH(m, route->multipath_routes) {
union in_addr_union a = m->gateway.address;
const char *s = in_addr_is_set(m->gateway.family, &a) ? IN_ADDR_TO_STRING(m->gateway.family, &a) : NULL;
if (m->ifindex > 0)
r = strextendf_with_separator(&buf, ",", "%s@%i:%"PRIu32, strempty(s), m->ifindex, m->weight + 1);
else if (m->ifname)
r = strextendf_with_separator(&buf, ",", "%s@%s:%"PRIu32, strempty(s), m->ifname, m->weight + 1);
else
r = strextendf_with_separator(&buf, ",", "%s:%"PRIu32, strempty(s), m->weight + 1);
if (r < 0)
return r;
}
char *p = strjoin("gw: ", strna(buf));
if (!p)
return -ENOMEM;
*ret = p;
return 0;
}
static int append_nexthop_one(Link *link, const Route *route, const MultipathRoute *m, struct rtattr **rta, size_t offset) {
struct rtnexthop *rtnh;
struct rtattr *new_rta;

View File

@@ -5,6 +5,8 @@
typedef struct Route Route;
int route_nexthops_to_string(const Route *route, char **ret);
int route_nexthops_set_netlink_message(Link *link, const Route *route, sd_netlink_message *message);
int route_nexthops_read_netlink_message(Route *route, sd_netlink_message *message);

View File

@@ -529,9 +529,9 @@ void link_mark_routes(Link *link, NetworkConfigSource source) {
}
static void log_route_debug(const Route *route, const char *str, const Link *link, const Manager *manager) {
_cleanup_free_ char *state = NULL, *gw_alloc = NULL, *prefsrc = NULL,
_cleanup_free_ char *state = NULL, *nexthop = NULL, *prefsrc = NULL,
*table = NULL, *scope = NULL, *proto = NULL, *flags = NULL;
const char *gw = NULL, *dst, *src;
const char *dst, *src;
assert(route);
assert(str);
@@ -549,32 +549,8 @@ static void log_route_debug(const Route *route, const char *str, const Link *lin
src = in_addr_is_set(route->family, &route->src) || route->src_prefixlen > 0 ?
IN_ADDR_PREFIX_TO_STRING(route->family, &route->src, route->src_prefixlen) : NULL;
if (in_addr_is_set(route->gw_family, &route->gw)) {
(void) in_addr_to_string(route->gw_family, &route->gw, &gw_alloc);
gw = gw_alloc;
} else if (route->gateway_from_dhcp_or_ra) {
if (route->gw_family == AF_INET)
gw = "_dhcp4";
else if (route->gw_family == AF_INET6)
gw = "_ipv6ra";
} else {
MultipathRoute *m;
(void) route_nexthops_to_string(route, &nexthop);
ORDERED_SET_FOREACH(m, route->multipath_routes) {
_cleanup_free_ char *buf = NULL;
union in_addr_union a = m->gateway.address;
(void) in_addr_to_string(m->gateway.family, &a, &buf);
(void) strextend_with_separator(&gw_alloc, ",", strna(buf));
if (m->ifname)
(void) strextend(&gw_alloc, "@", m->ifname);
else if (m->ifindex > 0)
(void) strextendf(&gw_alloc, "@%i", m->ifindex);
/* See comments in config_parse_multipath_route(). */
(void) strextendf(&gw_alloc, ":%"PRIu32, m->weight + 1);
}
gw = gw_alloc;
}
if (in_addr_is_set(route->family, &route->prefsrc))
(void) in_addr_to_string(route->family, &route->prefsrc, &prefsrc);
(void) route_scope_to_string_alloc(route->scope, &scope);
@@ -583,13 +559,13 @@ static void log_route_debug(const Route *route, const char *str, const Link *lin
(void) route_flags_to_string_alloc(route->flags, &flags);
log_link_debug(link,
"%s %s route (%s): dst: %s, src: %s, gw: %s, prefsrc: %s, scope: %s, table: %s, "
"proto: %s, type: %s, nexthop: %"PRIu32", priority: %"PRIu32", flags: %s",
"%s %s route (%s): dst: %s, src: %s, %s, prefsrc: %s, "
"table: %s, priority: %"PRIu32", "
"proto: %s, scope: %s, type: %s, flags: %s",
str, strna(network_config_source_to_string(route->source)), strna(state),
strna(dst), strna(src), strna(gw), strna(prefsrc),
strna(scope), strna(table), strna(proto),
strna(route_type_to_string(route->type)),
route->nexthop_id, route->priority, strna(flags));
strna(dst), strna(src), strna(nexthop), strna(prefsrc),
strna(table), route->priority,
strna(proto), strna(scope), strna(route_type_to_string(route->type)), strna(flags));
}
static int route_set_netlink_message(const Route *route, sd_netlink_message *req, Link *link) {