github.com/looshlee/beatles@v0.0.0-20220727174639-742810ab631c/pkg/maps/nat/types.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 nat
    16  
    17  import (
    18  	"bytes"
    19  	"unsafe"
    20  
    21  	"github.com/cilium/cilium/pkg/bpf"
    22  	"github.com/cilium/cilium/pkg/tuple"
    23  )
    24  
    25  type NatKey interface {
    26  	bpf.MapKey
    27  
    28  	// ToNetwork converts fields to network byte order.
    29  	ToNetwork() NatKey
    30  
    31  	// ToHost converts fields to host byte order.
    32  	ToHost() NatKey
    33  
    34  	// Dump contents of key to buffer. Returns true if successful.
    35  	Dump(buffer *bytes.Buffer, reverse bool) bool
    36  
    37  	// GetFlags flags containing the direction of the TupleKey.
    38  	GetFlags() uint8
    39  }
    40  
    41  // NatKey4 is needed to provide NatEntry type to Lookup values
    42  // +k8s:deepcopy-gen=true
    43  // +k8s:deepcopy-gen:interfaces=github.com/cilium/cilium/pkg/bpf.MapKey
    44  type NatKey4 struct {
    45  	tuple.TupleKey4Global
    46  }
    47  
    48  // NewValue creates a new bpf.MapValue.
    49  func (k *NatKey4) NewValue() bpf.MapValue { return &NatEntry4{} }
    50  
    51  // ToNetwork converts ports to network byte order.
    52  //
    53  // This is necessary to prevent callers from implicitly converting
    54  // the NatKey4 type here into a local key type in the nested
    55  // TupleKey4Global field.
    56  func (k *NatKey4) ToNetwork() NatKey {
    57  	return &NatKey4{
    58  		TupleKey4Global: *k.TupleKey4Global.ToNetwork().(*tuple.TupleKey4Global),
    59  	}
    60  }
    61  
    62  // ToHost converts ports to host byte order.
    63  //
    64  // This is necessary to prevent callers from implicitly converting
    65  // the NatKey4 type here into a local key type in the nested
    66  // TupleKey4Global field.
    67  func (k *NatKey4) ToHost() NatKey {
    68  	return &NatKey4{
    69  		TupleKey4Global: *k.TupleKey4Global.ToHost().(*tuple.TupleKey4Global),
    70  	}
    71  }
    72  
    73  // GetKeyPtr returns the unsafe.Pointer for k.
    74  func (k *NatKey4) GetKeyPtr() unsafe.Pointer { return unsafe.Pointer(k) }
    75  
    76  // NatKey6 is needed to provide NatEntry type to Lookup values
    77  // +k8s:deepcopy-gen=true
    78  // +k8s:deepcopy-gen:interfaces=github.com/cilium/cilium/pkg/bpf.MapKey
    79  type NatKey6 struct {
    80  	tuple.TupleKey6Global
    81  }
    82  
    83  // NewValue creates a new bpf.MapValue.
    84  func (k *NatKey6) NewValue() bpf.MapValue { return &NatEntry6{} }
    85  
    86  // ToNetwork converts ports to network byte order.
    87  //
    88  // This is necessary to prevent callers from implicitly converting
    89  // the NatKey6 type here into a local key type in the nested
    90  // TupleKey6Global field.
    91  func (k *NatKey6) ToNetwork() NatKey {
    92  	return &NatKey6{
    93  		TupleKey6Global: *k.TupleKey6Global.ToNetwork().(*tuple.TupleKey6Global),
    94  	}
    95  }
    96  
    97  // ToHost converts ports to host byte order.
    98  //
    99  // This is necessary to prevent callers from implicitly converting
   100  // the NatKey6 type here into a local key type in the nested
   101  // TupleKey6Global field.
   102  func (k *NatKey6) ToHost() NatKey {
   103  	return &NatKey6{
   104  		TupleKey6Global: *k.TupleKey6Global.ToHost().(*tuple.TupleKey6Global),
   105  	}
   106  }
   107  
   108  // GetKeyPtr returns the unsafe.Pointer for k.
   109  func (k *NatKey6) GetKeyPtr() unsafe.Pointer { return unsafe.Pointer(k) }