From 88484dd2a15d18150a520061b6a0e7de1c80370d Mon Sep 17 00:00:00 2001 From: Matt Phillips Date: Fri, 11 Mar 2022 13:07:41 -0500 Subject: networking/linux: map src only default routes accordingly in certain configurations a default route can be set via src only / not dev, but should still be accounted for in terms of calculating the 'favored' default route / what is chosen as ipaddress for a given node. This change augments the previously added changes to account for nexthop multipath routing; but only in scenarios src address is set but only in scenarios src address is set. Signed-off-by: Matt Phillips --- lib/ohai/plugins/linux/network.rb | 13 +++++++++++- spec/unit/plugins/linux/network_spec.rb | 36 +++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/lib/ohai/plugins/linux/network.rb b/lib/ohai/plugins/linux/network.rb index 4a5ddb3c..cbecae86 100644 --- a/lib/ohai/plugins/linux/network.rb +++ b/lib/ohai/plugins/linux/network.rb @@ -103,9 +103,20 @@ Ohai.plugin(:Network) do next end route_endings.each do |route_ending| + route_int = nil if route_ending =~ /\bdev\s+([^\s]+)\b/ route_int = $1 - else + end + # does any known interface own the src address? + # we want to override the interface set via nexthop but only if possible + if line =~ /\bsrc\s+([^\s]+)\b/ && (!route_int || line.include?("nexthop")) + # only clobber previously set route_int if we find a match + if (match = iface.select { |name, intf| intf.fetch("addresses", {}).any? { |addr, _| addr == $1 } }.keys.first) + route_int = match + end + end + + unless route_int logger.trace("Plugin Network: Skipping route entry without a device: '#{line}'") next end diff --git a/spec/unit/plugins/linux/network_spec.rb b/spec/unit/plugins/linux/network_spec.rb index 48f84ffc..0bb20597 100644 --- a/spec/unit/plugins/linux/network_spec.rb +++ b/spec/unit/plugins/linux/network_spec.rb @@ -1335,6 +1335,42 @@ describe Ohai::System, "Linux Network Plugin" do end end + describe "when there's a source field in a local route entry but no dev" do + let(:linux_ip_route) do + <<~EOM + default proto bird src 172.16.19.39 metric 10 \\ nexthop via 10.1.81.1 dev eth0 weight 20 + default via 10.116.201.1 dev eth0 metric 10 src 10.116.201.1 + 10.116.201.0/24 dev eth0 proto kernel src 10.116.201.76 + 192.168.0.0/24 dev eth0 proto kernel src 192.168.0.2 + default via 10.116.201.254 dev eth0 metric 20 src 10.116.201.74 + EOM + end + let(:linux_ip_route_inet6) { "" } + + it "maps the no-dev route and sets ipaddress" do + plugin.run + expect(plugin["ipaddress"]).to eq("172.16.19.39") + end + end + + describe "when there's a source field in a local route entry but no dev with a higher priority default route" do + let(:linux_ip_route) do + <<~EOM + default proto bird src 172.16.19.39 metric 10 \\ nexthop via 10.1.81.1 dev eth0 weight 20 + default via 10.116.201.1 dev eth0 metric 10 src 10.116.201.1 + 10.116.201.0/24 dev eth0 proto kernel src 10.116.201.76 + 192.168.0.0/24 dev eth0 proto kernel src 192.168.0.2 + default via 10.116.201.254 dev eth0 metric 5 src 10.116.201.74 + EOM + end + let(:linux_ip_route_inet6) { "" } + + it "maps the no-dev route and sets ipaddress" do + plugin.run + expect(plugin["ipaddress"]).to eq("10.116.201.74") + end + end + describe "with a link level default route" do let(:linux_ip_route) do <<~EOM -- cgit v1.2.1