From 4e44b1101dc1c2a763bb37c0343e48ead1780e3c Mon Sep 17 00:00:00 2001 From: Benjamin Milde Date: Wed, 4 Aug 2021 15:18:03 +0200 Subject: [PATCH] Set weights in route manager --- lib/vintage_net/route_manager.ex | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/lib/vintage_net/route_manager.ex b/lib/vintage_net/route_manager.ex index efbccf67..554b2d6d 100644 --- a/lib/vintage_net/route_manager.ex +++ b/lib/vintage_net/route_manager.ex @@ -114,6 +114,14 @@ defmodule VintageNet.RouteManager do GenServer.call(__MODULE__, {:set_prioritization, priorities}) end + @doc """ + Set the weights to interfaces to prioritize with the same type + """ + @spec set_weights(%{VintageNet.ifname() => Classification.weight()}) :: :ok + def set_weights(weights) do + GenServer.call(__MODULE__, {:set_weights, weights}) + end + ## GenServer @impl GenServer @@ -192,6 +200,23 @@ defmodule VintageNet.RouteManager do {:reply, :ok, new_state} end + @impl GenServer + def handle_call({:set_weights, weights}, _from, state) do + new_state = + Enum.reduce(weights, state, fn {ifname, weight}, state when weight in 0..9 -> + case Map.fetch(state.interfaces, ifname) do + {:ok, %InterfaceInfo{}} -> + update_in(state.interfaces[ifname], &struct!(&1, weight: weight)) + + _ -> + Logger.warn("RouteManager: set_weights to #{weight} on unknown ifname: #{ifname}") + end + end) + |> update_route_tables() + + {:reply, :ok, new_state} + end + defp interface_info_changed?(state, ifname, ip_subnets, default_gateway) do case Map.fetch(state.interfaces, ifname) do {:ok,