From: Yu Watanabe Date: Fri, 12 Jan 2024 02:07:46 +0000 (+0900) Subject: network/route-nexthop: split out route_nexthops_to_string() X-Git-Tag: v256-rc1~1159^2~3 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f47d38c6fcc27ea00e74e5e3211df05562e33c1a;p=thirdparty%2Fsystemd.git network/route-nexthop: split out route_nexthops_to_string() And reorder elements shown in the debugging log. No effective functionality changed, just refactoring. --- diff --git a/src/network/networkd-route-nexthop.c b/src/network/networkd-route-nexthop.c index bccb73684c2..4c46794a0f6 100644 --- a/src/network/networkd-route-nexthop.c +++ b/src/network/networkd-route-nexthop.c @@ -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; diff --git a/src/network/networkd-route-nexthop.h b/src/network/networkd-route-nexthop.h index bd3e70c6ea5..982b20551a0 100644 --- a/src/network/networkd-route-nexthop.h +++ b/src/network/networkd-route-nexthop.h @@ -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); diff --git a/src/network/networkd-route.c b/src/network/networkd-route.c index d96934c5df5..4186a74e39c 100644 --- a/src/network/networkd-route.c +++ b/src/network/networkd-route.c @@ -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; - - 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; - } + (void) route_nexthops_to_string(route, &nexthop); + 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) {