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

Fix kernel interface name generation #1603

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
16 changes: 9 additions & 7 deletions pkg/networkservice/common/mechanisms/kernel/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,16 @@ func NewServer(opts ...Option) networkservice.NetworkServiceServer {
func (m *kernelMechanismServer) Request(ctx context.Context, request *networkservice.NetworkServiceRequest) (*networkservice.Connection, error) {
if mechanism := kernelmech.ToMechanism(request.GetConnection().GetMechanism()); mechanism != nil {
mechanism.SetNetNSURL(netNSURL)
if m.interfaceName != "" {
mechanism.SetInterfaceName(m.interfaceName)
} else {
ifname, err := generateInterfaceName(m.interfaceNameGenerator)
if err != nil {
return nil, errors.Wrap(err, "Failed to generate kernel interface name")
if mechanism.GetInterfaceName() == "" {
if m.interfaceName != "" {
mechanism.SetInterfaceName(m.interfaceName)
} else {
ifname, err := generateInterfaceName(m.interfaceNameGenerator)
if err != nil {
return nil, errors.Wrap(err, "Failed to generate kernel interface name")
}
mechanism.SetInterfaceName(ifname)
}
mechanism.SetInterfaceName(ifname)
}
}
return next.Server(ctx).Request(ctx, request)
Expand Down
63 changes: 45 additions & 18 deletions pkg/networkservice/common/mechanisms/kernel/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,50 +37,77 @@ func TestKernelMechanismServer_ShouldSetInterfaceName(t *testing.T) {
expectedIfaceName += "a"
}

c := kernel.NewClient(kernel.WithInterfaceName(expectedIfaceName + "long-suffix"))
s := kernel.NewServer(kernel.WithInterfaceName(expectedIfaceName + "long-suffix"))

req := &networkservice.NetworkServiceRequest{}
_, err := c.Request(context.Background(), req)
req := &networkservice.NetworkServiceRequest{
Connection: &networkservice.Connection{
Mechanism: kernelmech.New(""),
},
}
conn, err := s.Request(context.Background(), req)
require.NoError(t, err)
require.Equal(t, expectedIfaceName, conn.Mechanism.Parameters[kernelmech.InterfaceNameKey])

require.Len(t, req.MechanismPreferences, 1)
require.Equal(t, expectedIfaceName, req.MechanismPreferences[0].Parameters[kernelmech.InterfaceNameKey])
// Refresh
req.Connection = conn.Clone()
conn, err = s.Request(context.Background(), req)
require.NoError(t, err)
require.Equal(t, expectedIfaceName, conn.Mechanism.Parameters[kernelmech.InterfaceNameKey])
}

func TestKernelMechanismServer_ShouldSetValidNetNSURL(t *testing.T) {
c := kernel.NewClient()
s := kernel.NewServer()

req := &networkservice.NetworkServiceRequest{
MechanismPreferences: []*networkservice.Mechanism{
kernelmech.New("invalid-url"),
Connection: &networkservice.Connection{
Mechanism: kernelmech.New("invalid-url"),
},
}

_, err := c.Request(context.Background(), req)
conn, err := s.Request(context.Background(), req)
require.NoError(t, err)
require.Equal(t, netNSURL, req.MechanismPreferences[0].Parameters[kernelmech.NetNSURL])
require.Equal(t, netNSURL, conn.Mechanism.Parameters[kernelmech.NetNSURL])

// Refresh
req.Connection = conn.Clone()
conn, err = s.Request(context.Background(), req)
require.NoError(t, err)
require.Equal(t, netNSURL, conn.Mechanism.Parameters[kernelmech.NetNSURL])
}

func TestKernelMechanismServer_ShouldSetRandomInteraceName(t *testing.T) {
c := kernel.NewClient()
req := &networkservice.NetworkServiceRequest{}
s := kernel.NewServer()
req := &networkservice.NetworkServiceRequest{
Connection: &networkservice.Connection{
Mechanism: kernelmech.New(""),
},
}

_, err := c.Request(context.Background(), req)
conn, err := s.Request(context.Background(), req)
require.NoError(t, err)

ifname := req.MechanismPreferences[0].Parameters[kernelmech.InterfaceNameKey]
ifname := conn.Mechanism.Parameters[kernelmech.InterfaceNameKey]

require.Len(t, ifname, kernelmech.LinuxIfMaxLength)
require.True(t, strings.HasPrefix(ifname, "nsm"))
for i := 0; i < kernelmech.LinuxIfMaxLength; i++ {
require.Contains(t, nanoid.DefaultAlphabet, string(ifname[i]))
}

// Refresh
req.Connection = conn.Clone()
conn, err = s.Request(context.Background(), req)
require.NoError(t, err)
require.Equal(t, ifname, conn.Mechanism.Parameters[kernelmech.InterfaceNameKey])
}

func TestKernelMechanismServer_FailedToGenerateRandomName(t *testing.T) {
c := kernel.NewClient(kernel.WithInterfaceNameGenerator(nanoid.New(nanoid.WithRandomByteGenerator(&brokenByteGenerator{}))))
req := &networkservice.NetworkServiceRequest{}
s := kernel.NewServer(kernel.WithInterfaceNameGenerator(nanoid.New(nanoid.WithRandomByteGenerator(&brokenByteGenerator{}))))
req := &networkservice.NetworkServiceRequest{
Connection: &networkservice.Connection{
Mechanism: kernelmech.New(""),
},
}

_, err := c.Request(context.Background(), req)
_, err := s.Request(context.Background(), req)
require.Error(t, err)
}
Loading