github.com/noironetworks/cilium-net@v1.6.12/pkg/probe/probe.go (about)

     1  // Copyright 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  package probe
    16  
    17  import (
    18  	"fmt"
    19  	"unsafe"
    20  
    21  	"github.com/cilium/cilium/pkg/bpf"
    22  )
    23  
    24  type probeKey struct {
    25  	Prefixlen uint32
    26  	Key       uint32
    27  }
    28  
    29  type probeValue struct {
    30  	Value uint32
    31  }
    32  
    33  func (p *probeKey) String() string             { return fmt.Sprintf("key=%d", p.Key) }
    34  func (p *probeKey) GetKeyPtr() unsafe.Pointer  { return unsafe.Pointer(p) }
    35  func (p *probeKey) NewValue() bpf.MapValue     { return &probeValue{} }
    36  func (p *probeKey) DeepCopyMapKey() bpf.MapKey { return &probeKey{p.Prefixlen, p.Key} }
    37  
    38  func (p *probeValue) String() string                 { return fmt.Sprintf("value=%d", p.Value) }
    39  func (p *probeValue) GetValuePtr() unsafe.Pointer    { return unsafe.Pointer(p) }
    40  func (p *probeValue) DeepCopyMapValue() bpf.MapValue { return &probeValue{p.Value} }
    41  
    42  // HaveFullLPM tests whether kernel supports fully functioning BPF LPM map
    43  // with proper bpf.GetNextKey() traversal. Needs 4.16 or higher.
    44  func HaveFullLPM() bool {
    45  	m := bpf.NewMap("cilium_test", bpf.MapTypeLPMTrie,
    46  		&probeKey{}, int(unsafe.Sizeof(probeKey{})),
    47  		&probeValue{}, int(unsafe.Sizeof(probeValue{})),
    48  		1, bpf.BPF_F_NO_PREALLOC, 0, bpf.ConvertKeyValue).WithCache()
    49  	_, err := m.OpenOrCreateUnpinned()
    50  	defer m.Close()
    51  	if err != nil {
    52  		return false
    53  	}
    54  	err = bpf.UpdateElement(m.GetFd(), unsafe.Pointer(&probeKey{}),
    55  		unsafe.Pointer(&probeValue{}), bpf.BPF_ANY)
    56  	if err != nil {
    57  		return false
    58  	}
    59  	err = bpf.GetNextKey(m.GetFd(), nil, unsafe.Pointer(&probeKey{}))
    60  	if err != nil {
    61  		return false
    62  	}
    63  	return true
    64  }