Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify if prioritization and add :route_metric_fun option #332

Merged
merged 5 commits into from
Aug 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions .dialyzer_ignore.exs

This file was deleted.

1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ persistence_secret | A 16-byte secret or an MFA for getting a secret
internet_host_list | IP address/ports to try to connect to for checking Internet connectivity. Defaults to a list of large public DNS providers. E.g., `[{{1, 1, 1, 1}, 53}]`.
regulatory_domain | ISO 3166-1 alpha-2 country (`00` for global, `US`, etc.)
additional_name_servers | List of DNS servers to be used in addition to any supplied by an interface. E.g., `[{1, 1, 1, 1}, {8, 8, 8, 8}]`
route_metric_fun | Customize how network interfaces are prioritized. See `VintageNet.Route.DefaultMetric.compute_metric/2`

## Network interface configuration

Expand Down
30 changes: 30 additions & 0 deletions lib/vintage_net.ex
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,36 @@ defmodule VintageNet do
"""
@type ipv6_prefix_length :: 0..128

@typedoc """
Interface connection status

* `:disconnected` - The interface doesn't exist or it's not connected
* `:lan` - The interface is connected to the LAN, but may not be able
reach the Internet
* `:internet` - Packets going through the interface should be able to
reach the Internet
"""
@type connection_status :: :lan | :internet | :disconnected

@typedoc """
Interface type

This is a coarse characterization of a network interface that can be useful
for prioritizing interfaces.

* `:ethernet` - Wired-based networking. Generally expected to be fast.
* `:wifi` - Wireless networking. Expected to be not as fast as Ethernet,
* `:mobile` - Cellular-based networking. Expected to be metered and slower
than `:wifi` and `:ethernet`
* `:local` - Interfaces that never route to other hosts
* `:unknown` - Catch-all when the network interface can't be categorized

These are general categories that are helpful for VintageNet's default
routing prioritization. See `VintageNet.Route.DefaultMetric` for more
information on the use.
"""
@type interface_type :: :ethernet | :wifi | :mobile | :local | :unknown

@typedoc """
Valid options for `VintageNet.configure/3`

Expand Down
2 changes: 1 addition & 1 deletion lib/vintage_net/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ defmodule VintageNet.Application do
dispatcher: &VintageNet.OSEventDispatcher.dispatch/2},
VintageNet.InterfacesMonitor,
{VintageNet.NameResolver, args},
VintageNet.RouteManager,
{VintageNet.RouteManager, args},
{Registry, keys: :unique, name: VintageNet.Interface.Registry},
VintageNet.InterfacesSupervisor
]
Expand Down
6 changes: 2 additions & 4 deletions lib/vintage_net/connectivity/check_logic.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
defmodule VintageNet.Connectivity.CheckLogic do
alias VintageNet.Interface.Classification

@moduledoc """
Core logic for determining internet connectivity based on check results

Expand All @@ -14,7 +12,7 @@ defmodule VintageNet.Connectivity.CheckLogic do
@max_fails_in_a_row 3

@type state() :: %{
connectivity: Classification.connection_status(),
connectivity: VintageNet.connection_status(),
strikes: non_neg_integer(),
interval: non_neg_integer() | :infinity
}
Expand All @@ -24,7 +22,7 @@ defmodule VintageNet.Connectivity.CheckLogic do

Pass in the assumed connection status. This is a best guess to start things out.
"""
@spec init(Classification.connection_status()) :: state()
@spec init(VintageNet.connection_status()) :: state()
def init(:internet) do
# Best case, but check quickly to verify that the internet truly is reachable.
%{connectivity: :internet, strikes: 0, interval: @min_interval}
Expand Down
139 changes: 0 additions & 139 deletions lib/vintage_net/interface/classification.ex

This file was deleted.

56 changes: 56 additions & 0 deletions lib/vintage_net/interface/name_utilities.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
defmodule VintageNet.Interface.NameUtilities do
@moduledoc """
Module for classifying network interfaces
"""

@doc """
Classify a network type based on its name

Examples

iex> NameUtilities.to_type("eth0")
:ethernet

iex> NameUtilities.to_type("wlp5s0")
:wifi

iex> NameUtilities.to_type("wwan0")
:mobile

"""
@spec to_type(VintageNet.ifname()) :: VintageNet.interface_type()
def to_type("eth" <> _rest), do: :ethernet
def to_type("en" <> _rest), do: :ethernet
def to_type("wlan" <> _rest), do: :wifi
def to_type("wl" <> _rest), do: :wifi
def to_type("ra" <> _rest), do: :wifi
def to_type("ppp" <> _rest), do: :mobile
def to_type("wwan" <> _rest), do: :mobile
def to_type("lo" <> _rest), do: :local
def to_type("tap" <> _rest), do: :local
def to_type(_other), do: :unknown

@doc """
Extract a number out of an interface name

The result is the interface index for most interfaces seen
on Nerves (eth0, eth1, ...), and something quite imperfect when using predictable
interface naming (enp6s0, enp6s1).

This is currently used to order priorities when there are two
interfaces available of the same type that cannot be differentiated
by other means. It has the one property of being easy to explain.
"""
@spec get_instance(VintageNet.ifname()) :: non_neg_integer()
def get_instance(ifname) do
ifname
|> String.to_charlist()
|> Enum.reduce(0, &add_numbers/2)
end

defp add_numbers(c, sum) when c >= ?0 and c <= ?9 do
sum * 10 + c - ?0
end

defp add_numbers(_c, sum), do: sum
end
63 changes: 63 additions & 0 deletions lib/vintage_net/route.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
defmodule VintageNet.Route do
@moduledoc """
Types for handling routing tables
"""

alias VintageNet.Route.InterfaceInfo

@typedoc """
Metric (priority) for a routing table entry
"""
@type metric :: 0..32767

@typedoc """
Linux routing table index

`:main` is table 254, `:local` is table 255. `:default` is normally the same as `:main`.
"""
@type table_index :: 0..255 | :main | :local | :default

@typedoc """
An IP route rule

If the source address matches the 3rd element, then use the routing table specified by the
2nd element.
"""
@type rule :: {:rule, table_index(), :inet.ip_address()}

@typedoc """
A default route entry

The IP address is the default gateway
"""
@type default_route ::
{:default_route, VintageNet.ifname(), :inet.ip_address(), metric(), table_index()}

@typedoc """
A local route entry

This is for routing packets to the LAN
"""
@type local_route ::
{:local_route, VintageNet.ifname(), :inet.ip_address(), metric(), table_index()}

@typedoc """
A routing table entry

This can be turned into real Linux IP routing table entry.
"""
@type entry :: rule() | default_route() | local_route()

@typedoc """
A list of routing table entries
"""
@type entries :: [entry()]

@typedoc """
Compute a route metric value from information about the interface

See `VintageNet.Route.DefaultMetric.compute_metric/2` for an example. This can be
set using the `:route_metric_fun` application environment key.
"""
@type route_metric_fun() :: (VintageNet.ifname(), InterfaceInfo.t() -> metric())
end
Loading