network: merge link_foreignize_config() and link_drop_foreign_config()

When a reconfiguration of an interface is triggered, previously we
call link_foreignize_config(), which sets all static configurations as
foreign, then later call link_drop_foreign_config(), which drops
unnecessary foreign configurations.

This commit merges these two steps into one, link_drop_unmanaged_config(),
which drops unnecessary static and foreign configurations.

Also, this renames link_drop_managed_configs() to
link_drop_static_config(), as it only drops static configurations.
Note that dynamically aquired configurations are dropped by
link_stop_engines().
This commit is contained in:
Yu Watanabe
2024-11-06 02:05:00 +09:00
parent 2b07a3211b
commit dd6d53a8dc
11 changed files with 116 additions and 222 deletions
+13 -18
View File
@@ -1384,7 +1384,7 @@ int link_drop_ipv6ll_addresses(Link *link) {
return 0;
}
int link_drop_foreign_addresses(Link *link) {
int link_drop_unmanaged_addresses(Link *link) {
Address *address;
int r = 0;
@@ -1401,18 +1401,22 @@ int link_drop_foreign_addresses(Link *link) {
if (link->flags & IFF_LOOPBACK && in_addr_is_localhost_one(address->family, &address->in_addr) > 0)
continue;
/* Ignore addresses we configured. */
if (address->source != NETWORK_CONFIG_SOURCE_FOREIGN)
continue;
/* Ignore addresses not assigned yet or already removing. */
if (!address_exists(address))
continue;
/* link_address_is_dynamic() is slightly heavy. Let's call the function only when KeepConfiguration= is set. */
if (IN_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DHCP, KEEP_CONFIGURATION_STATIC) &&
link_address_is_dynamic(link, address) == (link->network->keep_configuration == KEEP_CONFIGURATION_DHCP))
continue;
if (address->source == NETWORK_CONFIG_SOURCE_FOREIGN) {
if (link->network->keep_configuration == KEEP_CONFIGURATION_YES)
continue;
/* link_address_is_dynamic() is slightly heavy. Let's call the function only when
* KeepConfiguration=dhcp or static. */
if (IN_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DHCP, KEEP_CONFIGURATION_STATIC) &&
link_address_is_dynamic(link, address) == (link->network->keep_configuration == KEEP_CONFIGURATION_DHCP))
continue;
} else if (address->source != NETWORK_CONFIG_SOURCE_STATIC)
continue; /* Ignore dynamically configurad addresses. */
address_mark(address);
}
@@ -1464,15 +1468,6 @@ int link_drop_static_addresses(Link *link) {
return r;
}
void link_foreignize_addresses(Link *link) {
Address *address;
assert(link);
SET_FOREACH(address, link->addresses)
address->source = NETWORK_CONFIG_SOURCE_FOREIGN;
}
int address_configure_handler_internal(sd_netlink *rtnl, sd_netlink_message *m, Link *link, const char *error_msg) {
int r;
+1 -2
View File
@@ -113,9 +113,8 @@ bool link_check_addresses_ready(Link *link, NetworkConfigSource source);
DEFINE_SECTION_CLEANUP_FUNCTIONS(Address, address_unref);
int link_drop_static_addresses(Link *link);
int link_drop_foreign_addresses(Link *link);
int link_drop_unmanaged_addresses(Link *link);
int link_drop_ipv6ll_addresses(Link *link);
void link_foreignize_addresses(Link *link);
bool link_address_is_dynamic(const Link *link, const Address *address);
int link_get_address_full(
+17 -45
View File
@@ -1008,7 +1008,7 @@ static int link_drop_requests(Link *link) {
/* If the request is already called, but its reply is not received, then we need to
* drop the configuration (e.g. address) here. Note, if the configuration is known,
* it will be handled later by link_drop_foreign_addresses() or so. */
* it will be handled later by link_drop_unmanaged_addresses() or so. */
if (req->waiting_reply && link->state != LINK_STATE_LINGER)
switch (req->type) {
case REQUEST_TYPE_ADDRESS: {
@@ -1086,33 +1086,23 @@ static Link *link_drop(Link *link) {
return link_unref(link);
}
static int link_drop_foreign_config(Link *link) {
static int link_drop_unmanaged_config(Link *link) {
int r;
assert(link);
assert(link->state == LINK_STATE_CONFIGURING);
assert(link->manager);
/* Drop foreign config, but ignore unmanaged, loopback, or critical interfaces. We do not want
* to remove loopback address or addresses used for root NFS. */
if (IN_SET(link->state, LINK_STATE_UNMANAGED, LINK_STATE_PENDING, LINK_STATE_INITIALIZED))
return 0;
if (FLAGS_SET(link->flags, IFF_LOOPBACK))
return 0;
if (link->network->keep_configuration == KEEP_CONFIGURATION_YES)
return 0;
r = link_drop_foreign_routes(link);
RET_GATHER(r, link_drop_foreign_nexthops(link));
RET_GATHER(r, link_drop_foreign_addresses(link));
RET_GATHER(r, link_drop_foreign_neighbors(link));
RET_GATHER(r, manager_drop_foreign_routing_policy_rules(link->manager));
r = link_drop_unmanaged_routes(link);
RET_GATHER(r, link_drop_unmanaged_nexthops(link));
RET_GATHER(r, link_drop_unmanaged_addresses(link));
RET_GATHER(r, link_drop_unmanaged_neighbors(link));
RET_GATHER(r, link_drop_unmanaged_routing_policy_rules(link));
return r;
}
static int link_drop_managed_config(Link *link) {
static int link_drop_static_config(Link *link) {
int r;
assert(link);
@@ -1127,17 +1117,6 @@ static int link_drop_managed_config(Link *link) {
return r;
}
static void link_foreignize_config(Link *link) {
assert(link);
assert(link->manager);
link_foreignize_routes(link);
link_foreignize_nexthops(link);
link_foreignize_addresses(link);
link_foreignize_neighbors(link);
link_foreignize_routing_policy_rules(link);
}
static int link_configure(Link *link) {
int r;
@@ -1252,7 +1231,7 @@ static int link_configure(Link *link) {
if (r < 0)
return r;
r = link_drop_foreign_config(link);
r = link_drop_unmanaged_config(link);
if (r < 0)
return r;
@@ -1331,7 +1310,7 @@ static void link_enter_unmanaged(Link *link) {
(void) link_stop_engines(link, /* may_keep_dhcp = */ false);
(void) link_drop_requests(link);
(void) link_drop_managed_config(link);
(void) link_drop_static_config(link);
/* The bound_to map depends on .network file, hence it needs to be freed. But, do not free the
* bound_by map. Otherwise, if a link enters unmanaged state below, then its carrier state will
@@ -1393,18 +1372,11 @@ int link_reconfigure_impl(Link *link, LinkReconfigurationFlag flags) {
if (r < 0)
return r;
if (!FLAGS_SET(flags, LINK_RECONFIGURE_UNCONDITIONALLY) && network->keep_configuration != KEEP_CONFIGURATION_YES)
/* When a new/updated .network file is assigned, first make all configs (addresses,
* routes, and so on) foreign, and then drop unnecessary configs later by
* link_drop_foreign_config() in link_configure().
* Note, when KeepConfiguration=yes, link_drop_foreign_config() does nothing. Hence,
* here we need to drop the configs such as addresses, routes, and so on configured by
* the previously assigned .network file. */
link_foreignize_config(link);
else {
/* Remove all managed configs. Note, foreign configs are removed in later by
* link_configure() -> link_drop_foreign_config() if the link is managed by us. */
r = link_drop_managed_config(link);
if (FLAGS_SET(flags, LINK_RECONFIGURE_UNCONDITIONALLY)) {
/* Remove all static configurations. Note, dynamic configurations are dropped by
* link_stop_engines(), and foreign configurations will be removed later by
* link_configure() -> link_drop_unmanaged_config(). */
r = link_drop_static_config(link);
if (r < 0)
return r;
}
@@ -1791,7 +1763,7 @@ static int link_carrier_lost_impl(Link *link) {
return ret;
RET_GATHER(ret, link_stop_engines(link, false));
RET_GATHER(ret, link_drop_managed_config(link));
RET_GATHER(ret, link_drop_static_config(link));
return ret;
}
+7 -14
View File
@@ -467,7 +467,7 @@ int neighbor_remove(Neighbor *neighbor, Link *link) {
return 0;
}
int link_drop_foreign_neighbors(Link *link) {
int link_drop_unmanaged_neighbors(Link *link) {
Neighbor *neighbor;
int r = 0;
@@ -476,14 +476,15 @@ int link_drop_foreign_neighbors(Link *link) {
/* First, mark all neighbors. */
SET_FOREACH(neighbor, link->neighbors) {
/* Do not remove neighbors we configured. */
if (neighbor->source != NETWORK_CONFIG_SOURCE_FOREIGN)
continue;
/* Ignore neighbors not assigned yet or already removing. */
if (!neighbor_exists(neighbor))
continue;
/* Ignore foreign neighbors when KeepConfiguration=yes or static. */
if (neighbor->source == NETWORK_CONFIG_SOURCE_FOREIGN &&
FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_STATIC))
continue;
neighbor_mark(neighbor);
}
@@ -495,6 +496,7 @@ int link_drop_foreign_neighbors(Link *link) {
neighbor_unmark(existing);
}
/* Finally, remove all marked neighbors. */
SET_FOREACH(neighbor, link->neighbors) {
if (!neighbor_is_marked(neighbor))
continue;
@@ -526,15 +528,6 @@ int link_drop_static_neighbors(Link *link) {
return r;
}
void link_foreignize_neighbors(Link *link) {
Neighbor *neighbor;
assert(link);
SET_FOREACH(neighbor, link->neighbors)
neighbor->source = NETWORK_CONFIG_SOURCE_FOREIGN;
}
int manager_rtnl_process_neighbor(sd_netlink *rtnl, sd_netlink_message *message, Manager *m) {
_cleanup_(neighbor_unrefp) Neighbor *tmp = NULL;
Neighbor *neighbor = NULL;
+1 -2
View File
@@ -36,8 +36,7 @@ int neighbor_remove(Neighbor *neighbor, Link *link);
int network_drop_invalid_neighbors(Network *network);
int link_drop_static_neighbors(Link *link);
int link_drop_foreign_neighbors(Link *link);
void link_foreignize_neighbors(Link *link);
int link_drop_unmanaged_neighbors(Link *link);
int link_request_static_neighbors(Link *link);
+15 -32
View File
@@ -856,9 +856,10 @@ static bool nexthop_can_update(const NextHop *assigned_nexthop, const NextHop *r
return true;
}
static void link_mark_nexthops(Link *link, bool foreign) {
int link_drop_nexthops(Link *link, bool only_static) {
NextHop *nexthop;
Link *other;
int r = 0;
assert(link);
assert(link->manager);
@@ -869,14 +870,21 @@ static void link_mark_nexthops(Link *link, bool foreign) {
if (nexthop->protocol == RTPROT_KERNEL)
continue;
/* When 'foreign' is true, mark only foreign nexthops, and vice versa. */
if (nexthop->source != (foreign ? NETWORK_CONFIG_SOURCE_FOREIGN : NETWORK_CONFIG_SOURCE_STATIC))
continue;
/* Ignore nexthops not assigned yet or already removed. */
if (!nexthop_exists(nexthop))
continue;
if (only_static) {
if (nexthop->source != NETWORK_CONFIG_SOURCE_STATIC)
continue;
} else {
/* Do not mark foreign nexthop when KeepConfiguration= is enabled. */
if (nexthop->source == NETWORK_CONFIG_SOURCE_FOREIGN &&
link->network &&
FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_STATIC))
continue;
}
/* Ignore nexthops bound to other links. */
if (nexthop->ifindex > 0 && nexthop->ifindex != link->ifindex)
continue;
@@ -886,7 +894,7 @@ static void link_mark_nexthops(Link *link, bool foreign) {
/* Then, unmark all nexthops requested by active links. */
HASHMAP_FOREACH(other, link->manager->links_by_index) {
if (!foreign && other == link)
if (only_static && other == link)
continue;
if (!IN_SET(other->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED))
@@ -905,17 +913,8 @@ static void link_mark_nexthops(Link *link, bool foreign) {
nexthop_unmark(existing);
}
}
}
int link_drop_nexthops(Link *link, bool foreign) {
NextHop *nexthop;
int r = 0;
assert(link);
assert(link->manager);
link_mark_nexthops(link, foreign);
/* Finally, remove all marked nexthops. */
HASHMAP_FOREACH(nexthop, link->manager->nexthops_by_id) {
if (!nexthop_is_marked(nexthop))
continue;
@@ -926,22 +925,6 @@ int link_drop_nexthops(Link *link, bool foreign) {
return r;
}
void link_foreignize_nexthops(Link *link) {
NextHop *nexthop;
assert(link);
assert(link->manager);
link_mark_nexthops(link, /* foreign = */ false);
HASHMAP_FOREACH(nexthop, link->manager->nexthops_by_id) {
if (!nexthop_is_marked(nexthop))
continue;
nexthop->source = NETWORK_CONFIG_SOURCE_FOREIGN;
}
}
static int nexthop_update_group(NextHop *nexthop, sd_netlink_message *message) {
_cleanup_hashmap_free_free_ Hashmap *h = NULL;
_cleanup_free_ struct nexthop_grp *group = NULL;
+4 -5
View File
@@ -53,14 +53,13 @@ int nexthop_remove(NextHop *nexthop, Manager *manager);
int network_drop_invalid_nexthops(Network *network);
int link_drop_nexthops(Link *link, bool foreign);
static inline int link_drop_foreign_nexthops(Link *link) {
return link_drop_nexthops(link, /* foreign = */ true);
int link_drop_nexthops(Link *link, bool only_static);
static inline int link_drop_unmanaged_nexthops(Link *link) {
return link_drop_nexthops(link, /* only_static = */ false);
}
static inline int link_drop_static_nexthops(Link *link) {
return link_drop_nexthops(link, /* foreign = */ false);
return link_drop_nexthops(link, /* only_static = */ true);
}
void link_foreignize_nexthops(Link *link);
int link_request_static_nexthops(Link *link, bool only_ipv4);
+21 -51
View File
@@ -1439,10 +1439,10 @@ static int link_unmark_route(Link *link, const Route *route, const RouteNextHop
return 1;
}
static int link_mark_routes(Link *link, bool foreign) {
int link_drop_routes(Link *link, bool only_static) {
Route *route;
Link *other;
int r;
int r = 0;
assert(link);
assert(link->manager);
@@ -1453,31 +1453,35 @@ static int link_mark_routes(Link *link, bool foreign) {
if (route_by_kernel(route))
continue;
/* When 'foreign' is true, mark only foreign routes, and vice versa.
* Note, do not touch dynamic routes. They will removed by when e.g. lease is lost. */
if (route->source != (foreign ? NETWORK_CONFIG_SOURCE_FOREIGN : NETWORK_CONFIG_SOURCE_STATIC))
continue;
/* Ignore routes not assigned yet or already removed. */
if (!route_exists(route))
continue;
if (link->network) {
if (route->protocol == RTPROT_STATIC &&
FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_STATIC))
if (only_static) {
if (route->source != NETWORK_CONFIG_SOURCE_STATIC)
continue;
} else {
/* Ignore dynamically assigned routes. */
if (!IN_SET(route->source, NETWORK_CONFIG_SOURCE_FOREIGN, NETWORK_CONFIG_SOURCE_STATIC))
continue;
if (route->protocol == RTPROT_DHCP &&
FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DHCP))
continue;
if (route->source == NETWORK_CONFIG_SOURCE_FOREIGN && link->network) {
if (route->protocol == RTPROT_STATIC &&
FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_STATIC))
continue;
if (IN_SET(route->protocol, RTPROT_DHCP, RTPROT_RA, RTPROT_REDIRECT) &&
FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_DHCP))
continue;
}
}
/* When we mark foreign routes, do not mark routes assigned to other interfaces.
/* When we also mark foreign routes, do not mark routes assigned to other interfaces.
* Otherwise, routes assigned to unmanaged interfaces will be dropped.
* Note, route_get_link() does not provide assigned link for routes with an unreachable type
* or IPv4 multipath routes. So, the current implementation does not support managing such
* routes by other daemon or so, unless ManageForeignRoutes=no. */
if (foreign) {
if (!only_static) {
Link *route_link;
if (route_get_link(link->manager, route, &route_link) >= 0 && route_link != link)
@@ -1489,7 +1493,7 @@ static int link_mark_routes(Link *link, bool foreign) {
/* Then, unmark all routes requested by active links. */
HASHMAP_FOREACH(other, link->manager->links_by_index) {
if (!foreign && other == link)
if (only_static && other == link)
continue;
if (!IN_SET(other->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED))
@@ -1523,20 +1527,7 @@ static int link_mark_routes(Link *link, bool foreign) {
}
}
return 0;
}
int link_drop_routes(Link *link, bool foreign) {
Route *route;
int r;
assert(link);
assert(link->manager);
r = link_mark_routes(link, foreign);
if (r < 0)
return r;
/* Finally, remove all marked routes. */
SET_FOREACH(route, link->manager->routes) {
if (!route_is_marked(route))
continue;
@@ -1547,27 +1538,6 @@ int link_drop_routes(Link *link, bool foreign) {
return r;
}
int link_foreignize_routes(Link *link) {
Route *route;
int r;
assert(link);
assert(link->manager);
r = link_mark_routes(link, /* foreign = */ false);
if (r < 0)
return r;
SET_FOREACH(route, link->manager->routes) {
if (!route_is_marked(route))
continue;
route->source = NETWORK_CONFIG_SOURCE_FOREIGN;
}
return 0;
}
int network_add_ipv4ll_route(Network *network) {
_cleanup_(route_unref_or_set_invalidp) Route *route = NULL;
unsigned section_line;
+4 -5
View File
@@ -102,14 +102,13 @@ int route_get_request(Manager *manager, const Route *route, Request **ret);
bool route_can_update(const Route *existing, const Route *requesting);
int link_drop_routes(Link *link, bool foreign);
int link_drop_routes(Link *link, bool only_static);
static inline int link_drop_static_routes(Link *link) {
return link_drop_routes(link, false);
}
static inline int link_drop_foreign_routes(Link *link) {
return link_drop_routes(link, true);
}
int link_foreignize_routes(Link *link);
static inline int link_drop_unmanaged_routes(Link *link) {
return link_drop_routes(link, false);
}
int link_request_route(
Link *link,
+28 -42
View File
@@ -800,21 +800,30 @@ static void manager_unmark_routing_policy_rule(Manager *m, const RoutingPolicyRu
routing_policy_rule_unmark(existing);
}
static void manager_mark_routing_policy_rules(Manager *m, bool foreign, const Link *except) {
int link_drop_routing_policy_rules(Link *link, bool only_static) {
RoutingPolicyRule *rule;
Link *link;
int r = 0;
assert(m);
assert(link);
assert(link->manager);
/* First, mark all existing rules. */
SET_FOREACH(rule, m->rules) {
SET_FOREACH(rule, link->manager->rules) {
/* Do not touch rules managed by kernel. */
if (rule->protocol == RTPROT_KERNEL)
continue;
/* When 'foreign' is true, mark only foreign rules, and vice versa. */
if (rule->source != (foreign ? NETWORK_CONFIG_SOURCE_FOREIGN : NETWORK_CONFIG_SOURCE_STATIC))
continue;
if (only_static) {
/* When 'only_static' is true, mark only static rules. */
if (rule->source != NETWORK_CONFIG_SOURCE_STATIC)
continue;
} else {
/* Do not mark foreign rules when KeepConfiguration= is enabled. */
if (rule->source == NETWORK_CONFIG_SOURCE_FOREIGN &&
link->network &&
FLAGS_SET(link->network->keep_configuration, KEEP_CONFIGURATION_STATIC))
continue;
}
/* Ignore rules not assigned yet or already removing. */
if (!routing_policy_rule_exists(rule))
@@ -824,57 +833,34 @@ static void manager_mark_routing_policy_rules(Manager *m, bool foreign, const Li
}
/* Then, unmark all rules requested by active links. */
HASHMAP_FOREACH(link, m->links_by_index) {
if (link == except)
Link *other;
HASHMAP_FOREACH(other, link->manager->links_by_index) {
if (only_static && other == link)
continue;
if (!IN_SET(link->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED))
if (!IN_SET(other->state, LINK_STATE_CONFIGURING, LINK_STATE_CONFIGURED))
continue;
HASHMAP_FOREACH(rule, link->network->rules_by_section) {
HASHMAP_FOREACH(rule, other->network->rules_by_section) {
if (IN_SET(rule->family, AF_INET, AF_INET6))
manager_unmark_routing_policy_rule(m, rule, rule->family);
manager_unmark_routing_policy_rule(link->manager, rule, rule->family);
else {
assert(rule->address_family == ADDRESS_FAMILY_YES);
manager_unmark_routing_policy_rule(m, rule, AF_INET);
manager_unmark_routing_policy_rule(m, rule, AF_INET6);
manager_unmark_routing_policy_rule(link->manager, rule, AF_INET);
manager_unmark_routing_policy_rule(link->manager, rule, AF_INET6);
}
}
}
}
int manager_drop_routing_policy_rules_internal(Manager *m, bool foreign, const Link *except) {
RoutingPolicyRule *rule;
int r = 0;
assert(m);
manager_mark_routing_policy_rules(m, foreign, except);
SET_FOREACH(rule, m->rules) {
if (!routing_policy_rule_is_marked(rule))
continue;
RET_GATHER(r, routing_policy_rule_remove(rule, m));
}
return r;
}
void link_foreignize_routing_policy_rules(Link *link) {
RoutingPolicyRule *rule;
assert(link);
assert(link->manager);
manager_mark_routing_policy_rules(link->manager, /* foreign = */ false, link);
/* Finally, remove all marked rules. */
SET_FOREACH(rule, link->manager->rules) {
if (!routing_policy_rule_is_marked(rule))
continue;
rule->source = NETWORK_CONFIG_SOURCE_FOREIGN;
RET_GATHER(r, routing_policy_rule_remove(rule, link->manager));
}
return r;
}
static int routing_policy_rule_process_request(Request *req, Link *link, RoutingPolicyRule *rule) {
+5 -6
View File
@@ -63,15 +63,14 @@ void network_drop_invalid_routing_policy_rules(Network *network);
int link_request_static_routing_policy_rules(Link *link);
int manager_rtnl_process_rule(sd_netlink *rtnl, sd_netlink_message *message, Manager *m);
int manager_drop_routing_policy_rules_internal(Manager *m, bool foreign, const Link *except);
static inline int manager_drop_foreign_routing_policy_rules(Manager *m) {
return manager_drop_routing_policy_rules_internal(m, true, NULL);
int link_drop_routing_policy_rules(Link *link, bool only_static);
static inline int link_drop_unmanaged_routing_policy_rules(Link *link) {
return link_drop_routing_policy_rules(link, false);
}
static inline int link_drop_static_routing_policy_rules(Link *link) {
assert(link);
return manager_drop_routing_policy_rules_internal(link->manager, false, link);
return link_drop_routing_policy_rules(link, true);
}
void link_foreignize_routing_policy_rules(Link *link);
DEFINE_NETWORK_CONFIG_STATE_FUNCTIONS(RoutingPolicyRule, routing_policy_rule);