Skip to content

Commit

Permalink
Sort local routes first
Browse files Browse the repository at this point in the history
Local routes are needed when setting default routes. Otherwise Linux
returns that the default route isn't reachable and fails the route.
  • Loading branch information
fhunleth committed May 15, 2019
1 parent f726935 commit ebd16cb
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 12 deletions.
27 changes: 26 additions & 1 deletion lib/vintage_net/route/calculator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,36 @@ defmodule VintageNet.Route.Calculator do
{new_table_indices, entries} =
Enum.reduce(infos, {table_indices, []}, &make_entries(&1, &2, prioritization))

sorted_entries = Enum.sort(entries)
sorted_entries = Enum.sort(entries, &sort/2)

{new_table_indices, sorted_entries}
end

# Sort order
#
# 1. Local routes
# 2. Rules
# 3. Default routes
#
# The most important part is that local routes get created before default
# routes. Linux disallows default routes that can't be supported and the
# local routes are needed for that.
defp sort({:local_route, _, _, _, _} = a, {:local_route, _, _, _, _} = b) do
a <= b
end

defp sort({:local_route, _, _, _, _}, _other) do
true
end

defp sort(_other, {:local_route, _, _, _, _}) do
false
end

defp sort(a, b) do
a <= b
end

@doc """
Utility function to trim IP address to its subnet
Expand Down
22 changes: 11 additions & 11 deletions test/vintage_net/route/calculator_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ defmodule VintageNet.Route.CalculatorTest do

assert {%{"eth0" => 100},
[
{:local_route, "eth0", {192, 168, 1, 50}, 24, 10},
{:rule, 100, {192, 168, 1, 50}},
{:default_route, "eth0", {192, 168, 1, 1}, 0, 100},
{:default_route, "eth0", {192, 168, 1, 1}, 10, :main},
{:local_route, "eth0", {192, 168, 1, 50}, 24, 10}
{:default_route, "eth0", {192, 168, 1, 1}, 10, :main}
]} == Calculator.compute(state, interfaces, prioritization)
end

Expand Down Expand Up @@ -65,7 +65,7 @@ defmodule VintageNet.Route.CalculatorTest do
}

assert {%{"eth0" => 100},
[{:rule, 100, {192, 168, 1, 50}}, {:local_route, "eth0", {192, 168, 1, 50}, 24, 50}]} ==
[{:local_route, "eth0", {192, 168, 1, 50}, 24, 50}, {:rule, 100, {192, 168, 1, 50}}]} ==
Calculator.compute(state, interfaces, prioritization)
end

Expand All @@ -90,14 +90,14 @@ defmodule VintageNet.Route.CalculatorTest do

assert {%{"eth0" => 100, "wlan0" => 101},
[
{:local_route, "eth0", {192, 168, 1, 50}, 24, 10},
{:local_route, "wlan0", {192, 168, 1, 60}, 24, 20},
{:rule, 100, {192, 168, 1, 50}},
{:rule, 101, {192, 168, 1, 60}},
{:default_route, "eth0", {192, 168, 1, 1}, 0, 100},
{:default_route, "eth0", {192, 168, 1, 1}, 10, :main},
{:default_route, "wlan0", {192, 168, 1, 1}, 0, 101},
{:default_route, "wlan0", {192, 168, 1, 1}, 20, :main},
{:local_route, "eth0", {192, 168, 1, 50}, 24, 10},
{:local_route, "wlan0", {192, 168, 1, 60}, 24, 20}
{:default_route, "wlan0", {192, 168, 1, 1}, 20, :main}
]} == Calculator.compute(state, interfaces, prioritization)
end

Expand All @@ -122,14 +122,14 @@ defmodule VintageNet.Route.CalculatorTest do

assert {%{"eth0" => 100, "wlan0" => 101},
[
{:local_route, "eth0", {192, 168, 1, 50}, 24, 50},
{:local_route, "wlan0", {192, 168, 1, 60}, 24, 20},
{:rule, 100, {192, 168, 1, 50}},
{:rule, 101, {192, 168, 1, 60}},
{:default_route, "eth0", {192, 168, 1, 1}, 0, 100},
{:default_route, "eth0", {192, 168, 1, 1}, 50, :main},
{:default_route, "wlan0", {192, 168, 1, 1}, 0, 101},
{:default_route, "wlan0", {192, 168, 1, 1}, 20, :main},
{:local_route, "eth0", {192, 168, 1, 50}, 24, 50},
{:local_route, "wlan0", {192, 168, 1, 60}, 24, 20}
{:default_route, "wlan0", {192, 168, 1, 1}, 20, :main}
]} == Calculator.compute(state, interfaces, prioritization)
end

Expand All @@ -152,12 +152,12 @@ defmodule VintageNet.Route.CalculatorTest do

assert {%{"eth0" => 100},
[
{:local_route, "eth0", {192, 168, 1, 50}, 24, 10},
{:rule, 100, {192, 168, 1, 50}},
{:rule, 100, {192, 168, 1, 51}},
{:rule, 100, {192, 168, 1, 52}},
{:default_route, "eth0", {192, 168, 1, 1}, 0, 100},
{:default_route, "eth0", {192, 168, 1, 1}, 10, :main},
{:local_route, "eth0", {192, 168, 1, 50}, 24, 10}
{:default_route, "eth0", {192, 168, 1, 1}, 10, :main}
]} == Calculator.compute(state, interfaces, prioritization)
end
end

0 comments on commit ebd16cb

Please sign in to comment.