github.com/looshlee/beatles@v0.0.0-20220727174639-742810ab631c/pkg/maps/ctmap/ctmap_privileged_test.go (about)

     1  // Copyright 2016-2019 Authors of Cilium
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // +build privileged_tests
    16  
    17  package ctmap
    18  
    19  import (
    20  	"testing"
    21  	"unsafe"
    22  
    23  	"github.com/cilium/cilium/common/types"
    24  	"github.com/cilium/cilium/pkg/bpf"
    25  	"github.com/cilium/cilium/pkg/tuple"
    26  	"github.com/cilium/cilium/pkg/u8proto"
    27  
    28  	. "gopkg.in/check.v1"
    29  )
    30  
    31  // Hook up gocheck into the "go test" runner.
    32  type CTMapTestSuite struct{}
    33  
    34  var _ = Suite(&CTMapTestSuite{})
    35  
    36  func Test(t *testing.T) {
    37  	TestingT(t)
    38  }
    39  
    40  func (k *CTMapTestSuite) Benchmark_MapUpdate(c *C) {
    41  	m := NewMap(MapNameTCP4Global+"_test", MapTypeIPv4TCPGlobal)
    42  	_, err := m.OpenOrCreate()
    43  	defer m.Map.Unpin()
    44  	c.Assert(err, IsNil)
    45  
    46  	key := &CtKey4{
    47  		tuple.TupleKey4{
    48  			DestAddr:   types.IPv4{0xa, 0x10, 0xc5, 0xf0},
    49  			SourceAddr: types.IPv4{0xa, 0x10, 0x9d, 0xb3},
    50  			DestPort:   0,
    51  			SourcePort: 0,
    52  			NextHeader: u8proto.TCP,
    53  			Flags:      0,
    54  		},
    55  	}
    56  	value := &CtEntry{
    57  		RxPackets:        4,
    58  		RxBytes:          216,
    59  		TxPackets:        4,
    60  		TxBytes:          216,
    61  		Lifetime:         37459,
    62  		Flags:            0x0011,
    63  		RevNAT:           0,
    64  		TxFlagsSeen:      0x02,
    65  		RxFlagsSeen:      0x14,
    66  		SourceSecurityID: 40653,
    67  		LastTxReport:     15856,
    68  		LastRxReport:     15856,
    69  	}
    70  
    71  	c.Assert(c.N < 0xFFFF*0xFFFF, Equals, true)
    72  	for i := 0; i < c.N; i++ {
    73  		key.DestPort = uint16(i % 0xFFFF)
    74  		key.SourcePort = uint16(i / 0xFFFF)
    75  		err := bpf.UpdateElement(m.Map.GetFd(), unsafe.Pointer(key), unsafe.Pointer(value), 0)
    76  		c.Assert(err, IsNil)
    77  	}
    78  
    79  	a1 := make([]tuple.TupleKey, 1)
    80  	a2 := make([]*CtEntry, 1)
    81  
    82  	// Also account the cost of casting from MapKey to TupleKey
    83  	cb := func(k bpf.MapKey, v bpf.MapValue) {
    84  		key := k.(tuple.TupleKey)
    85  		value := v.(*CtEntry)
    86  		a1[0] = key
    87  		a2[0] = value
    88  	}
    89  
    90  	c.ResetTimer()
    91  	err = m.DumpWithCallback(cb)
    92  	c.Assert(err, IsNil)
    93  	t := m.Flush()
    94  	c.Assert(t, Equals, c.N)
    95  }