github.com/cilium/cilium@v1.16.2/pkg/maps/lbmap/maglev_test.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright Authors of Cilium
     3  
     4  package lbmap
     5  
     6  import (
     7  	"net"
     8  	"testing"
     9  
    10  	"github.com/cilium/ebpf/rlimit"
    11  	"github.com/stretchr/testify/require"
    12  
    13  	datapathTypes "github.com/cilium/cilium/pkg/datapath/types"
    14  	"github.com/cilium/cilium/pkg/loadbalancer"
    15  	"github.com/cilium/cilium/pkg/option"
    16  	"github.com/cilium/cilium/pkg/testutils"
    17  )
    18  
    19  type MaglevSuite struct {
    20  	prevMaglevTableSize int
    21  	prevNodePortAlg     string
    22  }
    23  
    24  func setupMaglevSuite(tb testing.TB) *MaglevSuite {
    25  	testutils.PrivilegedTest(tb)
    26  
    27  	s := &MaglevSuite{}
    28  
    29  	s.prevMaglevTableSize = option.Config.MaglevTableSize
    30  	s.prevNodePortAlg = option.Config.NodePortAlg
    31  
    32  	// Otherwise opening the map might fail with EPERM
    33  	err := rlimit.RemoveMemlock()
    34  	require.NoError(tb, err)
    35  
    36  	option.Config.LBMapEntries = DefaultMaxEntries
    37  	option.Config.NodePortAlg = option.NodePortAlgMaglev
    38  
    39  	Init(InitParams{
    40  		IPv4: option.Config.EnableIPv4,
    41  		IPv6: option.Config.EnableIPv6,
    42  
    43  		ServiceMapMaxEntries: option.Config.LBMapEntries,
    44  		RevNatMapMaxEntries:  option.Config.LBMapEntries,
    45  		MaglevMapMaxEntries:  option.Config.LBMapEntries,
    46  	})
    47  
    48  	tb.Cleanup(func() {
    49  		option.Config.MaglevTableSize = s.prevMaglevTableSize
    50  		option.Config.NodePortAlg = s.prevNodePortAlg
    51  	})
    52  
    53  	return s
    54  }
    55  
    56  func TestInitMaps(t *testing.T) {
    57  	setupMaglevSuite(t)
    58  
    59  	option.Config.MaglevTableSize = 251
    60  	err := InitMaglevMaps(true, false, uint32(option.Config.MaglevTableSize))
    61  	require.NoError(t, err)
    62  
    63  	option.Config.MaglevTableSize = 509
    64  	// M mismatch, so the map should be removed
    65  	deleted, err := deleteMapIfMNotMatch(MaglevOuter4MapName, uint32(option.Config.MaglevTableSize))
    66  	require.NoError(t, err)
    67  	require.True(t, deleted)
    68  
    69  	// M is the same, but no entries, so the map should be removed too
    70  	err = InitMaglevMaps(true, false, uint32(option.Config.MaglevTableSize))
    71  	require.NoError(t, err)
    72  	deleted, err = deleteMapIfMNotMatch(MaglevOuter4MapName, uint32(option.Config.MaglevTableSize))
    73  	require.NoError(t, err)
    74  	require.True(t, deleted)
    75  
    76  	// Now insert the entry, so that the map should not be removed
    77  	err = InitMaglevMaps(true, false, uint32(option.Config.MaglevTableSize))
    78  	require.NoError(t, err)
    79  	lbm := New()
    80  	params := &datapathTypes.UpsertServiceParams{
    81  		ID:   1,
    82  		IP:   net.ParseIP("1.1.1.1"),
    83  		Port: 8080,
    84  		ActiveBackends: map[string]*loadbalancer.Backend{"backend-1": {
    85  			ID:     1,
    86  			Weight: 1,
    87  		}},
    88  		Type:      loadbalancer.SVCTypeNodePort,
    89  		UseMaglev: true,
    90  	}
    91  	err = lbm.UpsertService(params)
    92  	require.NoError(t, err)
    93  	deleted, err = deleteMapIfMNotMatch(MaglevOuter4MapName, uint32(option.Config.MaglevTableSize))
    94  	require.NoError(t, err)
    95  	require.Equal(t, false, deleted)
    96  }