summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorMatt Phillips <mphillips81@bloomberg.net>2022-03-23 11:52:47 -0400
committerMatt Phillips <mphillips81@bloomberg.net>2022-05-16 17:03:51 -0400
commitc431f946585ea7ffddae2c7a1450667a0ffdfda8 (patch)
tree88495b15458148835d95e655123826487a1ff159 /lib
parent88484dd2a15d18150a520061b6a0e7de1c80370d (diff)
downloadohai-c431f946585ea7ffddae2c7a1450667a0ffdfda8.tar.gz
prefer route that explicitly define a device
in theory I don't this should occur in real life, as I'm pretty sure /usr/sbin/ip should always print the dev routes above non dev routes if the metric is the same, but it is a good case to cover in code. also removed the default metric 10 gateway route as it was spurious/irrelevant to the test. Signed-off-by: Matt Phillips <mphillips81@bloomberg.net>
Diffstat (limited to 'lib')
-rw-r--r--lib/ohai/plugins/linux/network.rb8
1 files changed, 6 insertions, 2 deletions
diff --git a/lib/ohai/plugins/linux/network.rb b/lib/ohai/plugins/linux/network.rb
index cbecae86..eb83843c 100644
--- a/lib/ohai/plugins/linux/network.rb
+++ b/lib/ohai/plugins/linux/network.rb
@@ -103,16 +103,20 @@ Ohai.plugin(:Network) do
next
end
route_endings.each do |route_ending|
+ route_entry = Mash.new(destination: route_dest,
+ family: family[:name])
route_int = nil
if route_ending =~ /\bdev\s+([^\s]+)\b/
route_int = $1
end
# does any known interface own the src address?
+ # we try to infer the interface/device from its address if it isn't specified
# 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
+ route_entry[:inferred] = true
end
end
@@ -127,8 +131,6 @@ Ohai.plugin(:Network) do
next
end
- route_entry = Mash.new(destination: route_dest,
- family: family[:name])
%w{via scope metric proto src}.each do |k|
# http://rubular.com/r/pwTNp65VFf
route_entry[k] = $1 if route_ending =~ /\b#{k}\s+([^\s]+)/
@@ -656,10 +658,12 @@ Ohai.plugin(:Network) do
# sorting the selected routes:
# - getting default routes first
# - then sort by metric
+ # - then sort by if the device was inferred or not (preferring explicit to inferred)
# - then by prefixlen
[
r[:destination] == "default" ? 0 : 1,
r[:metric].nil? ? 0 : r[:metric].to_i,
+ r[:inferred] ? 1 : 0,
# for some reason IPAddress doesn't accept "::/0", it doesn't like prefix==0
# just a quick workaround: use 0 if IPAddress fails
begin