-
Notifications
You must be signed in to change notification settings - Fork 575
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement route network controllers
Route handling is very similar to addresses: * `RouteStatus` describes kernel routing table state, `RouteStatusController` reflects kernel state into resources * `RouteSpec` defines routes to be configured * `RouteConfigController` creates `RouteSpec`s based on cmdline and machine configuration * `RouteMergeController` merges different configuration layers into the final representation * `RouteSpecController` applies the specs to the kernel routing table Signed-off-by: Andrey Smirnov <smirnov.andrey@gmail.com>
- Loading branch information
Showing
39 changed files
with
2,338 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
internal/app/machined/pkg/controllers/network/cmdline.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
package network | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"sort" | ||
"strings" | ||
|
||
"github.com/talos-systems/go-procfs/procfs" | ||
"inet.af/netaddr" | ||
) | ||
|
||
// CmdlineNetworking contains parsed cmdline networking settings. | ||
type CmdlineNetworking struct { | ||
DHCP bool | ||
Address netaddr.IPPrefix | ||
Gateway netaddr.IP | ||
Hostname string | ||
LinkName string | ||
DNSAddresses []netaddr.IP | ||
NTPAddresses []netaddr.IP | ||
} | ||
|
||
// ParseCmdlineNetwork parses `ip=` kernel cmdline argument producing all the available configuration options. | ||
// | ||
//nolint:gocyclo,cyclop | ||
func ParseCmdlineNetwork(cmdline *procfs.Cmdline) (CmdlineNetworking, error) { | ||
var ( | ||
settings CmdlineNetworking | ||
err error | ||
) | ||
|
||
ipSettings := cmdline.Get("ip").First() | ||
if ipSettings == nil { | ||
return settings, nil | ||
} | ||
|
||
// https://www.kernel.org/doc/Documentation/filesystems/nfs/nfsroot.txt | ||
// ip=<client-ip>:<server-ip>:<gw-ip>:<netmask>:<hostname>:<device>:<autoconf>:<dns0-ip>:<dns1-ip>:<ntp0-ip> | ||
fields := strings.Split(*ipSettings, ":") | ||
|
||
// If dhcp is specified, we'll handle it as a normal discovered | ||
// interface | ||
if len(fields) == 1 && fields[0] == "dhcp" { | ||
settings.DHCP = true | ||
} | ||
|
||
if !settings.DHCP { | ||
for i := range fields { | ||
if fields[i] == "" { | ||
continue | ||
} | ||
|
||
switch i { | ||
case 0: | ||
settings.Address.IP, err = netaddr.ParseIP(fields[0]) | ||
if err != nil { | ||
return settings, fmt.Errorf("cmdline address parse failure: %s", err) | ||
} | ||
|
||
// default is to have complete address masked | ||
settings.Address.Bits = settings.Address.IP.BitLen() | ||
case 2: | ||
settings.Gateway, err = netaddr.ParseIP(fields[2]) | ||
if err != nil { | ||
return settings, fmt.Errorf("cmdline gateway parse failure: %s", err) | ||
} | ||
case 3: | ||
var netmask netaddr.IP | ||
|
||
netmask, err = netaddr.ParseIP(fields[3]) | ||
if err != nil { | ||
return settings, fmt.Errorf("cmdline netmask parse failure: %s", err) | ||
} | ||
|
||
ones, _ := net.IPMask(netmask.IPAddr().IP).Size() | ||
|
||
settings.Address.Bits = uint8(ones) | ||
case 4: | ||
settings.Hostname = fields[4] | ||
case 5: | ||
settings.LinkName = fields[5] | ||
case 7, 8: | ||
var dnsIP netaddr.IP | ||
|
||
dnsIP, err = netaddr.ParseIP(fields[i]) | ||
if err != nil { | ||
return settings, fmt.Errorf("error parsing DNS IP: %w", err) | ||
} | ||
|
||
settings.DNSAddresses = append(settings.DNSAddresses, dnsIP) | ||
case 9: | ||
var ntpIP netaddr.IP | ||
|
||
ntpIP, err = netaddr.ParseIP(fields[i]) | ||
if err != nil { | ||
return settings, fmt.Errorf("error parsing DNS IP: %w", err) | ||
} | ||
|
||
settings.NTPAddresses = append(settings.NTPAddresses, ntpIP) | ||
} | ||
} | ||
} | ||
|
||
// if interface name is not set, pick the first non-loopback interface | ||
if settings.LinkName == "" { | ||
ifaces, _ := net.Interfaces() //nolint:errcheck // ignoring error here as ifaces will be empty | ||
|
||
sort.Slice(ifaces, func(i, j int) bool { return ifaces[i].Name < ifaces[j].Name }) | ||
|
||
for _, iface := range ifaces { | ||
if iface.Flags&net.FlagLoopback != 0 { | ||
continue | ||
} | ||
|
||
settings.LinkName = iface.Name | ||
|
||
break | ||
} | ||
} | ||
|
||
return settings, nil | ||
} |
Oops, something went wrong.