f-41: netif: HostMAC et MaxVNI partagés (RMAC, MAC de gateway, borne des VNI) #41
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
parent
87e97c8be1
commit
387a082c02
10 changed files with 99 additions and 91 deletions
21
internal/netif/mac.go
Normal file
21
internal/netif/mac.go
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
package netif
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
)
|
||||
|
||||
const (
|
||||
locallyAdministered byte = 0x02
|
||||
|
||||
RoleRouter byte = 0x02
|
||||
RoleHostGateway byte = 0x03
|
||||
)
|
||||
|
||||
func HostMAC(role byte, vtep net.IP) (net.HardwareAddr, error) {
|
||||
ip := vtep.To4()
|
||||
if ip == nil {
|
||||
return nil, fmt.Errorf("vtep %v is not an IPv4 address", vtep)
|
||||
}
|
||||
return net.HardwareAddr{locallyAdministered, role, ip[0], ip[1], ip[2], ip[3]}, nil
|
||||
}
|
||||
60
internal/netif/mac_test.go
Normal file
60
internal/netif/mac_test.go
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
package netif
|
||||
|
||||
import (
|
||||
"net"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestHostMAC(t *testing.T) {
|
||||
cases := []struct {
|
||||
role byte
|
||||
vtep string
|
||||
want string
|
||||
}{
|
||||
{RoleHostGateway, "192.168.14.11", "02:03:c0:a8:0e:0b"},
|
||||
{RoleHostGateway, "192.168.14.12", "02:03:c0:a8:0e:0c"},
|
||||
{RoleHostGateway, "10.0.0.1", "02:03:0a:00:00:01"},
|
||||
{RoleRouter, "192.168.14.11", "02:02:c0:a8:0e:0b"},
|
||||
}
|
||||
for _, c := range cases {
|
||||
got, err := HostMAC(c.role, net.ParseIP(c.vtep))
|
||||
if err != nil {
|
||||
t.Fatalf("rôle %#x, VTEP %s : erreur inattendue %v", c.role, c.vtep, err)
|
||||
}
|
||||
if got.String() != c.want {
|
||||
t.Errorf("rôle %#x, VTEP %s : %s, %s attendu", c.role, c.vtep, got, c.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestHostMACIsLocalUnicast(t *testing.T) {
|
||||
for _, role := range []byte{RoleRouter, RoleHostGateway} {
|
||||
got, err := HostMAC(role, net.ParseIP("192.168.14.11"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got[0]&0x02 == 0 {
|
||||
t.Errorf("%s : bit « administrée localement » absent", got)
|
||||
}
|
||||
if got[0]&0x01 != 0 {
|
||||
t.Errorf("%s : MAC de groupe (multicast)", got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestHostMACRolesDifferOnTheSameHost(t *testing.T) {
|
||||
if RoleRouter == RoleHostGateway {
|
||||
t.Fatalf("rôles identiques : %#x", RoleRouter)
|
||||
}
|
||||
if RoleRouter != 0x02 || RoleHostGateway != 0x03 {
|
||||
t.Errorf("rôles %#x / %#x, 0x02 / 0x03 attendus (valeurs publiées dans la doc)", RoleRouter, RoleHostGateway)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHostMACRejectsANonIPv4VTEP(t *testing.T) {
|
||||
for _, vtep := range []net.IP{nil, net.ParseIP("fd00::1")} {
|
||||
if mac, err := HostMAC(RoleHostGateway, vtep); err == nil {
|
||||
t.Errorf("VTEP %v accepté : %s", vtep, mac)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -8,6 +8,8 @@ import (
|
|||
"github.com/vishvananda/netlink"
|
||||
)
|
||||
|
||||
const MaxVNI = 1<<24 - 1
|
||||
|
||||
const (
|
||||
scopeUniverse = 0
|
||||
addressSecondary = 0x01
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue