From 68505a0ae053953ec39bfa57646581cecdaeed7e Mon Sep 17 00:00:00 2001 From: Arnaud Chong Date: Tue, 5 Aug 2025 11:57:57 +0200 Subject: [PATCH 1/2] Fixing network detection when using multipath route Some setup could have a multipath route with a src assigned to the loopback In this situation, it fails to find a mac because there is none found on the loopback Better early return 00:00:00:00:00:00 when checking for a mac on lo Change-Id: I5ebf98914974400f1075fb1a9aae2dfc1eb2f10c Signed-off-by: Arnaud Chong --- lib/ohai/plugins/linux/network.rb | 2 ++ spec/unit/plugins/linux/network_spec.rb | 42 +++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/lib/ohai/plugins/linux/network.rb b/lib/ohai/plugins/linux/network.rb index 34d81ae7a..dbeb31b8d 100644 --- a/lib/ohai/plugins/linux/network.rb +++ b/lib/ohai/plugins/linux/network.rb @@ -578,6 +578,8 @@ def parse_ip_addr_inet6_line(cint, iface, line) # returns the macaddress for interface from a hash of interfaces (iface elsewhere in this file) def get_mac_for_interface(interfaces, interface) + return '00:00:00:00:00:00' if interfaces[interface][:flags].include?("LOOPBACK") + interfaces[interface][:addresses].find { |k, v| v["family"] == "lladdr" }.first unless interfaces[interface][:addresses].nil? || interfaces[interface][:flags].include?("NOARP") end diff --git a/spec/unit/plugins/linux/network_spec.rb b/spec/unit/plugins/linux/network_spec.rb index 810fc37fb..2ade11761 100644 --- a/spec/unit/plugins/linux/network_spec.rb +++ b/spec/unit/plugins/linux/network_spec.rb @@ -1260,6 +1260,48 @@ end end + describe "when there're multipath default routes with a loopback src" do + let(:linux_ip_route) do + <<~EOM + default nhid 18 proto bgp src 10.116.201.76 metric 20 \\ nexthop via 10.117.201.1 dev eth0 weight 1 \\ nexthop via 10.117.202.1 dev eth1 weight 1 + EOM + end + + let(:linux_ip_addr) do + <<~EOM + 1: lo: mtu 16436 qdisc noqueue state UNKNOWN + link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 + inet 127.0.0.1/8 scope host lo + inet 10.116.201.76/32 scope global lo + inet6 ::1/128 scope host + valid_lft forever preferred_lft forever + 2: eth0: mtu 1500 qdisc pfifo_fast state UP qlen 1000 + link/ether 12:31:3d:02:be:a2 brd ff:ff:ff:ff:ff:ff + inet 10.117.201.76/24 brd 10.117.201.255 scope global eth0 + inet6 fe80::1031:3dff:fe02:bea2/64 scope link + valid_lft forever preferred_lft forever + inet6 2001:44b8:4160:8f00:a00:27ff:fe13:eacd/64 scope global dynamic + valid_lft 6128sec preferred_lft 2526sec + 3: eth1: mtu 1500 qdisc noqueue state UP + link/ether 00:aa:bb:cc:dd:ee brd ff:ff:ff:ff:ff:ff + inet 10.117.202.76/24 brd 10.117.202.255 scope global eth1 + inet6 fe80::2e0:81ff:fe2b:48e7/64 scope link + inet6 1111:2222:3333:4444::2/64 scope global + valid_lft forever preferred_lft forever + inet6 1111:2222:3333:4444::3/64 scope global + valid_lft forever preferred_lft forever + EOM + end + + before do + plugin.run + end + + it 'finds the default interface' do + expect(plugin['network']['default_interface']).to eq('lo') + end + end + describe "when there's a source field in a local route entry but it isnt in the default route" do let(:linux_ip_route) do <<~EOM From d2a0441852348aba7751cf98335edec133fbbda9 Mon Sep 17 00:00:00 2001 From: Arnaud Chong Date: Wed, 13 Aug 2025 09:07:27 +0200 Subject: [PATCH 2/2] Cookstyle fixes * mostly Style/StringLiterals issues Change-Id: Iaf236f225a1d26dabc70e4e4b19c23610568fd79 Signed-off-by: Arnaud Chong --- lib/ohai/plugins/linux/network.rb | 2 +- spec/unit/plugins/linux/network_spec.rb | 48 ++++++++++++------------- 2 files changed, 25 insertions(+), 25 deletions(-) diff --git a/lib/ohai/plugins/linux/network.rb b/lib/ohai/plugins/linux/network.rb index dbeb31b8d..c6fd7c2b0 100644 --- a/lib/ohai/plugins/linux/network.rb +++ b/lib/ohai/plugins/linux/network.rb @@ -578,7 +578,7 @@ def parse_ip_addr_inet6_line(cint, iface, line) # returns the macaddress for interface from a hash of interfaces (iface elsewhere in this file) def get_mac_for_interface(interfaces, interface) - return '00:00:00:00:00:00' if interfaces[interface][:flags].include?("LOOPBACK") + return "00:00:00:00:00:00" if interfaces[interface][:flags].include?("LOOPBACK") interfaces[interface][:addresses].find { |k, v| v["family"] == "lladdr" }.first unless interfaces[interface][:addresses].nil? || interfaces[interface][:flags].include?("NOARP") end diff --git a/spec/unit/plugins/linux/network_spec.rb b/spec/unit/plugins/linux/network_spec.rb index 2ade11761..4c5d8bf8a 100644 --- a/spec/unit/plugins/linux/network_spec.rb +++ b/spec/unit/plugins/linux/network_spec.rb @@ -1263,33 +1263,33 @@ describe "when there're multipath default routes with a loopback src" do let(:linux_ip_route) do <<~EOM - default nhid 18 proto bgp src 10.116.201.76 metric 20 \\ nexthop via 10.117.201.1 dev eth0 weight 1 \\ nexthop via 10.117.202.1 dev eth1 weight 1 + default nhid 18 proto bgp src 10.116.201.76 metric 20 \\ nexthop via 10.117.201.1 dev eth0 weight 1 \\ nexthop via 10.117.202.1 dev eth1 weight 1 EOM end let(:linux_ip_addr) do <<~EOM - 1: lo: mtu 16436 qdisc noqueue state UNKNOWN - link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 - inet 127.0.0.1/8 scope host lo - inet 10.116.201.76/32 scope global lo - inet6 ::1/128 scope host - valid_lft forever preferred_lft forever - 2: eth0: mtu 1500 qdisc pfifo_fast state UP qlen 1000 - link/ether 12:31:3d:02:be:a2 brd ff:ff:ff:ff:ff:ff - inet 10.117.201.76/24 brd 10.117.201.255 scope global eth0 - inet6 fe80::1031:3dff:fe02:bea2/64 scope link - valid_lft forever preferred_lft forever - inet6 2001:44b8:4160:8f00:a00:27ff:fe13:eacd/64 scope global dynamic - valid_lft 6128sec preferred_lft 2526sec - 3: eth1: mtu 1500 qdisc noqueue state UP - link/ether 00:aa:bb:cc:dd:ee brd ff:ff:ff:ff:ff:ff - inet 10.117.202.76/24 brd 10.117.202.255 scope global eth1 - inet6 fe80::2e0:81ff:fe2b:48e7/64 scope link - inet6 1111:2222:3333:4444::2/64 scope global - valid_lft forever preferred_lft forever - inet6 1111:2222:3333:4444::3/64 scope global - valid_lft forever preferred_lft forever + 1: lo: mtu 16436 qdisc noqueue state UNKNOWN + link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 + inet 127.0.0.1/8 scope host lo + inet 10.116.201.76/32 scope global lo + inet6 ::1/128 scope host + valid_lft forever preferred_lft forever + 2: eth0: mtu 1500 qdisc pfifo_fast state UP qlen 1000 + link/ether 12:31:3d:02:be:a2 brd ff:ff:ff:ff:ff:ff + inet 10.117.201.76/24 brd 10.117.201.255 scope global eth0 + inet6 fe80::1031:3dff:fe02:bea2/64 scope link + valid_lft forever preferred_lft forever + inet6 2001:44b8:4160:8f00:a00:27ff:fe13:eacd/64 scope global dynamic + valid_lft 6128sec preferred_lft 2526sec + 3: eth1: mtu 1500 qdisc noqueue state UP + link/ether 00:aa:bb:cc:dd:ee brd ff:ff:ff:ff:ff:ff + inet 10.117.202.76/24 brd 10.117.202.255 scope global eth1 + inet6 fe80::2e0:81ff:fe2b:48e7/64 scope link + inet6 1111:2222:3333:4444::2/64 scope global + valid_lft forever preferred_lft forever + inet6 1111:2222:3333:4444::3/64 scope global + valid_lft forever preferred_lft forever EOM end @@ -1297,8 +1297,8 @@ plugin.run end - it 'finds the default interface' do - expect(plugin['network']['default_interface']).to eq('lo') + it "finds the default interface" do + expect(plugin["network"]["default_interface"]).to eq("lo") end end