Policy routing rules are global, and unlike routes not tied to an interface by ifindex. That means, while we take full control over all routes of an interface during a sync, we need to consider that multiple parties can contribute to the global set of rules. That might be muliple connection profiles providing the same rule, or rules that are added externally by the user. NMPRulesManager mediates for that.
This is done by NMPRulesManager "tracking" rules.
Rules that are not tracked by NMPRulesManager are completely ignored (and considered externally added).
When tracking a rule, the caller provides a track-priority. If multiple parties track a rule, then the highest (absolute value of the) priority wins.
If the highest track-priority is positive, NMPRulesManager will add the rule if it's not present.
When the highest track-priority is negative, then NMPRulesManager will remove the rule if it's present (enforce its absence).
The complicated part is, when a rule that was previously tracked becomes no longer tracked. In that case, we need to restore the previous state.
If NetworkManager added the rule earlier, then untracking the rule NMPRulesManager will remove the rule again (restore its previous absent state).
By default, if NetworkManager had a negative tracking-priority and removed the rule earlier (enforced it to be absent), then when the rule becomes no longer tracked, NetworkManager will not restore the rule. Consider: the user adds a rule externally, and then activates a profile that enforces the absence of the rule (causing NetworkManager to remove it). When deactivating the profile, by default NetworkManager will not restore such a rule! It's unclear whether that is a good idea, but it's also unclear why the rule is there and whether NetworkManager should really restore it.
Add weakly tracked rules to account for that. A tracking-priority of zero indicates such weakly tracked rules. The only difference between an untracked rule and a weakly tracked rule is, that when NetworkManager earlier removed the rule (due to a negative tracking-priority), it *will* restore weakly tracked rules when the rules becomes no longer (negatively) tracked. And it attmpts to do that only once.
Likewise, if the rule is weakly tracked and already exists when NMPRulesManager starts posively tracking the rule, then it would not remove again, when no longer positively tracking it.
f41b4cacd platform: support weakly tracked routing rules in NMPRulesManager
src/nm-netns.c | 8 ++++
src/platform/nmp-rules-manager.c | 80 ++++++++++++++++++++++++++++------------
2 files changed, 65 insertions(+), 23 deletions(-)