-
Notifications
You must be signed in to change notification settings - Fork 548
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: shorten VLAN link names to fit into the limit of 15 characters
Fixes #7679 This should be no-op if the link name is <= 10 chars, but with predictable interface names based on MAC addresses, they have to be shortened to make some space for VLAN ID. Signed-off-by: Andrey Smirnov <[email protected]>
- Loading branch information
Showing
8 changed files
with
112 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// 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 nethelpers | ||
|
||
import ( | ||
"crypto/sha256" | ||
"fmt" | ||
) | ||
|
||
const maxLinkNameLength = 15 | ||
|
||
// VLANLinkName builds a VLAN link name out of the base device name and VLAN ID. | ||
// | ||
// The function takes care of the maximum length of the link name. | ||
func VLANLinkName(base string, vlanID uint16) string { | ||
// VLAN ID is actually 12-bit, so the allowed values are 0-4095. | ||
// In ".%d" format, vlanID can be up to 5 characters long. | ||
if len(base)+5 <= maxLinkNameLength { | ||
return fmt.Sprintf("%s.%d", base, vlanID) | ||
} | ||
|
||
// If the base name is too long, we need to truncate it, but simply | ||
// truncating might lead to ambiguous link name, so take some hash of the original | ||
// name. | ||
prefix := base[:4] | ||
|
||
hash := sha256.Sum256([]byte(base)) | ||
|
||
return fmt.Sprintf("%s%x.%d", prefix, hash[:(maxLinkNameLength-len(prefix)-5)/2], vlanID) | ||
} |
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,70 @@ | ||
// 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 nethelpers_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/siderolabs/talos/pkg/machinery/nethelpers" | ||
) | ||
|
||
func TestVLANLinkName(t *testing.T) { | ||
t.Parallel() | ||
|
||
for _, test := range []struct { | ||
base string | ||
vlanID uint16 | ||
|
||
expected string | ||
}{ | ||
{ | ||
base: "eth0", | ||
vlanID: 1, | ||
|
||
expected: "eth0.1", | ||
}, | ||
{ | ||
base: "en9s0", | ||
vlanID: 4095, | ||
|
||
expected: "en9s0.4095", | ||
}, | ||
{ | ||
base: "0123456789", | ||
vlanID: 4095, | ||
|
||
expected: "0123456789.4095", | ||
}, | ||
{ | ||
base: "enx12545f8c99cd", | ||
vlanID: 25, | ||
|
||
expected: "enx1ee6413.25", | ||
}, | ||
{ | ||
base: "enx12545f8c99cd", | ||
vlanID: 4095, | ||
|
||
expected: "enx1ee6413.4095", | ||
}, | ||
{ | ||
base: "enx12545f8c99ce", | ||
vlanID: 4095, | ||
|
||
expected: "enx1ef972f.4095", | ||
}, | ||
} { | ||
test := test | ||
|
||
t.Run(fmt.Sprintf("%s.%d", test.base, test.vlanID), func(t *testing.T) { | ||
t.Parallel() | ||
|
||
assert.Equal(t, test.expected, nethelpers.VLANLinkName(test.base, test.vlanID)) | ||
}) | ||
} | ||
} |