network/route: use generic [Route] section parser more

This commit is contained in:
Yu Watanabe
2024-09-23 06:20:05 +09:00
parent 571bb24123
commit a768478738
3 changed files with 56 additions and 152 deletions

View File

@@ -196,17 +196,17 @@ RoutingPolicyRule.SuppressInterfaceGroup, config_parse_routing_policy_rule,
RoutingPolicyRule.SuppressPrefixLength, config_parse_routing_policy_rule, ROUTING_POLICY_RULE_SUPPRESS_PREFIXLEN, 0
RoutingPolicyRule.Type, config_parse_routing_policy_rule, ROUTING_POLICY_RULE_ACTION, 0
Route.Gateway, config_parse_route_section, ROUTE_GATEWAY, 0
Route.Destination, config_parse_destination, 0, 0
Route.Source, config_parse_destination, 0, 0
Route.Metric, config_parse_route_priority, 0, 0
Route.Scope, config_parse_route_scope, 0, 0
Route.PreferredSource, config_parse_preferred_src, 0, 0
Route.Table, config_parse_route_table, 0, 0
Route.Destination, config_parse_route_section, ROUTE_DESTINATION, 0
Route.Source, config_parse_route_section, ROUTE_DESTINATION, 0
Route.Metric, config_parse_route_section, ROUTE_PRIORITY, 0
Route.Scope, config_parse_route_section, ROUTE_SCOPE, 0
Route.PreferredSource, config_parse_route_section, ROUTE_PREFERRED_SOURCE, 0
Route.Table, config_parse_route_section, ROUTE_TABLE, 0
Route.GatewayOnLink, config_parse_route_section, ROUTE_GATEWAY_ONLINK, 0
Route.GatewayOnlink, config_parse_route_section, ROUTE_GATEWAY_ONLINK, 0
Route.IPv6Preference, config_parse_ipv6_route_preference, 0, 0
Route.Protocol, config_parse_route_protocol, 0, 0
Route.Type, config_parse_route_type, 0, 0
Route.IPv6Preference, config_parse_route_section, ROUTE_PREFERENCE, 0
Route.Protocol, config_parse_route_section, ROUTE_PROTOCOL, 0
Route.Type, config_parse_route_section, ROUTE_TYPE, 0
Route.MultiPathRoute, config_parse_route_section, ROUTE_MULTIPATH, 0
Route.NextHop, config_parse_route_section, ROUTE_NEXTHOP, 0
Route.MTUBytes, config_parse_route_section, ROUTE_METRIC_MTU, 0

View File

@@ -1628,7 +1628,7 @@ int network_add_default_route_on_device(Network *network) {
return 0;
}
int config_parse_preferred_src(
static int config_parse_preferred_src(
const char *unit,
const char *filename,
unsigned line,
@@ -1640,24 +1640,10 @@ int config_parse_preferred_src(
void *data,
void *userdata) {
Network *network = userdata;
_cleanup_(route_unref_or_set_invalidp) Route *route = NULL;
Route *route = ASSERT_PTR(userdata);
int r;
assert(filename);
assert(section);
assert(lvalue);
assert(rvalue);
assert(data);
r = route_new_static(network, filename, section_line, &route);
if (r == -ENOMEM)
return log_oom();
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r,
"Failed to allocate route, ignoring assignment: %m");
return 0;
}
if (route->family == AF_UNSPEC)
r = in_addr_from_string_auto(rvalue, &route->family, &route->prefsrc);
@@ -1666,11 +1652,10 @@ int config_parse_preferred_src(
if (r < 0)
return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
TAKE_PTR(route);
return 0;
return 1;
}
int config_parse_destination(
static int config_parse_route_destination(
const char *unit,
const char *filename,
unsigned line,
@@ -1682,26 +1667,13 @@ int config_parse_destination(
void *data,
void *userdata) {
Network *network = userdata;
_cleanup_(route_unref_or_set_invalidp) Route *route = NULL;
Route *route = ASSERT_PTR(userdata);
union in_addr_union *buffer;
unsigned char *prefixlen;
int r;
assert(filename);
assert(section);
assert(lvalue);
assert(rvalue);
assert(data);
r = route_new_static(network, filename, section_line, &route);
if (r == -ENOMEM)
return log_oom();
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r,
"Failed to allocate route, ignoring assignment: %m");
return 0;
}
if (streq(lvalue, "Destination")) {
buffer = &route->dst;
@@ -1720,12 +1692,10 @@ int config_parse_destination(
return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
(void) in_addr_mask(route->family, buffer, *prefixlen);
TAKE_PTR(route);
return 0;
return 1;
}
int config_parse_route_priority(
static int config_parse_route_priority(
const char *unit,
const char *filename,
unsigned line,
@@ -1737,35 +1707,20 @@ int config_parse_route_priority(
void *data,
void *userdata) {
Network *network = userdata;
_cleanup_(route_unref_or_set_invalidp) Route *route = NULL;
Route *route = ASSERT_PTR(userdata);
int r;
assert(filename);
assert(section);
assert(lvalue);
assert(rvalue);
assert(data);
r = route_new_static(network, filename, section_line, &route);
if (r == -ENOMEM)
return log_oom();
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r,
"Failed to allocate route, ignoring assignment: %m");
return 0;
}
r = safe_atou32(rvalue, &route->priority);
if (r < 0)
return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
route->priority_set = true;
TAKE_PTR(route);
return 0;
return 1;
}
int config_parse_route_scope(
static int config_parse_route_scope(
const char *unit,
const char *filename,
unsigned line,
@@ -1777,24 +1732,10 @@ int config_parse_route_scope(
void *data,
void *userdata) {
Network *network = userdata;
_cleanup_(route_unref_or_set_invalidp) Route *route = NULL;
Route *route = ASSERT_PTR(userdata);
int r;
assert(filename);
assert(section);
assert(lvalue);
assert(rvalue);
assert(data);
r = route_new_static(network, filename, section_line, &route);
if (r == -ENOMEM)
return log_oom();
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r,
"Failed to allocate route, ignoring assignment: %m");
return 0;
}
r = route_scope_from_string(rvalue);
if (r < 0)
@@ -1802,11 +1743,10 @@ int config_parse_route_scope(
route->scope = r;
route->scope_set = true;
TAKE_PTR(route);
return 0;
return 1;
}
int config_parse_route_table(
static int config_parse_route_table(
const char *unit,
const char *filename,
unsigned line,
@@ -1818,35 +1758,21 @@ int config_parse_route_table(
void *data,
void *userdata) {
_cleanup_(route_unref_or_set_invalidp) Route *route = NULL;
Network *network = userdata;
Route *route = ASSERT_PTR(userdata);
Manager *manager = ASSERT_PTR(ASSERT_PTR(route->network)->manager);
int r;
assert(filename);
assert(section);
assert(lvalue);
assert(rvalue);
assert(data);
r = route_new_static(network, filename, section_line, &route);
if (r == -ENOMEM)
return log_oom();
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r,
"Failed to allocate route, ignoring assignment: %m");
return 0;
}
r = manager_get_route_table_from_string(network->manager, rvalue, &route->table);
r = manager_get_route_table_from_string(manager, rvalue, &route->table);
if (r < 0)
return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
route->table_set = true;
TAKE_PTR(route);
return 0;
return 1;
}
int config_parse_ipv6_route_preference(
static int config_parse_route_preference(
const char *unit,
const char *filename,
unsigned line,
@@ -1858,18 +1784,9 @@ int config_parse_ipv6_route_preference(
void *data,
void *userdata) {
Network *network = userdata;
_cleanup_(route_unref_or_set_invalidp) Route *route = NULL;
int r;
Route *route = ASSERT_PTR(userdata);
r = route_new_static(network, filename, section_line, &route);
if (r == -ENOMEM)
return log_oom();
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r,
"Failed to allocate route, ignoring assignment: %m");
return 0;
}
assert(rvalue);
if (streq(rvalue, "low"))
route->pref = SD_NDISC_PREFERENCE_LOW;
@@ -1881,11 +1798,10 @@ int config_parse_ipv6_route_preference(
return log_syntax_parse_error(unit, filename, line, 0, lvalue, rvalue);
route->pref_set = true;
TAKE_PTR(route);
return 0;
return 1;
}
int config_parse_route_protocol(
static int config_parse_route_protocol(
const char *unit,
const char *filename,
unsigned line,
@@ -1897,30 +1813,20 @@ int config_parse_route_protocol(
void *data,
void *userdata) {
Network *network = userdata;
_cleanup_(route_unref_or_set_invalidp) Route *route = NULL;
unsigned char *p = ASSERT_PTR(data);
int r;
r = route_new_static(network, filename, section_line, &route);
if (r == -ENOMEM)
return log_oom();
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r,
"Failed to allocate route, ignoring assignment: %m");
return 0;
}
assert(rvalue);
r = route_protocol_from_string(rvalue);
if (r < 0)
return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
route->protocol = r;
TAKE_PTR(route);
return 0;
*p = (unsigned char) r;
return 1;
}
int config_parse_route_type(
static int config_parse_route_type(
const char *unit,
const char *filename,
unsigned line,
@@ -1932,27 +1838,17 @@ int config_parse_route_type(
void *data,
void *userdata) {
Network *network = userdata;
_cleanup_(route_unref_or_set_invalidp) Route *route = NULL;
unsigned char *p = ASSERT_PTR(data);
int r;
r = route_new_static(network, filename, section_line, &route);
if (r == -ENOMEM)
return log_oom();
if (r < 0) {
log_syntax(unit, LOG_WARNING, filename, line, r,
"Failed to allocate route, ignoring assignment: %m");
return 0;
}
assert(rvalue);
r = route_type_from_string(rvalue);
if (r < 0)
return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
route->type = (unsigned char) r;
TAKE_PTR(route);
return 0;
*p = (unsigned char) r;
return 1;
}
int config_parse_route_section(
@@ -1968,6 +1864,14 @@ int config_parse_route_section(
void *userdata) {
static const ConfigSectionParser table[_ROUTE_CONF_PARSER_MAX] = {
[ROUTE_DESTINATION] = { .parser = config_parse_route_destination, .ltype = 0, .offset = 0, },
[ROUTE_PREFERRED_SOURCE] = { .parser = config_parse_preferred_src, .ltype = 0, .offset = 0, },
[ROUTE_PRIORITY] = { .parser = config_parse_route_priority, .ltype = 0, .offset = 0, },
[ROUTE_SCOPE] = { .parser = config_parse_route_scope, .ltype = 0, .offset = 0, },
[ROUTE_TABLE] = { .parser = config_parse_route_table, .ltype = 0, .offset = 0, },
[ROUTE_PREFERENCE] = { .parser = config_parse_route_preference, .ltype = 0, .offset = 0, },
[ROUTE_PROTOCOL] = { .parser = config_parse_route_protocol, .ltype = 0, .offset = offsetof(Route, protocol), },
[ROUTE_TYPE] = { .parser = config_parse_route_type, .ltype = 0, .offset = offsetof(Route, type), },
[ROUTE_GATEWAY_NETWORK] = { .parser = config_parse_gateway, .ltype = 0, .offset = 0, },
[ROUTE_GATEWAY] = { .parser = config_parse_gateway, .ltype = 1, .offset = 0, },
[ROUTE_GATEWAY_ONLINK] = { .parser = config_parse_tristate, .ltype = 0, .offset = offsetof(Route, gateway_onlink), },

View File

@@ -127,6 +127,14 @@ DEFINE_NETWORK_CONFIG_STATE_FUNCTIONS(Route, route);
void manager_mark_routes(Manager *manager, Link *link, NetworkConfigSource source);
typedef enum RouteConfParserType {
ROUTE_DESTINATION,
ROUTE_PREFERRED_SOURCE,
ROUTE_PRIORITY,
ROUTE_SCOPE,
ROUTE_TABLE,
ROUTE_PREFERENCE,
ROUTE_PROTOCOL,
ROUTE_TYPE,
ROUTE_GATEWAY_NETWORK,
ROUTE_GATEWAY,
ROUTE_GATEWAY_ONLINK,
@@ -146,11 +154,3 @@ typedef enum RouteConfParserType {
} RouteConfParserType;
CONFIG_PARSER_PROTOTYPE(config_parse_route_section);
CONFIG_PARSER_PROTOTYPE(config_parse_preferred_src);
CONFIG_PARSER_PROTOTYPE(config_parse_destination);
CONFIG_PARSER_PROTOTYPE(config_parse_route_priority);
CONFIG_PARSER_PROTOTYPE(config_parse_route_scope);
CONFIG_PARSER_PROTOTYPE(config_parse_route_table);
CONFIG_PARSER_PROTOTYPE(config_parse_ipv6_route_preference);
CONFIG_PARSER_PROTOTYPE(config_parse_route_protocol);
CONFIG_PARSER_PROTOTYPE(config_parse_route_type);