github.com/cilium/cilium@v1.16.2/pkg/maps/nat/ipv6.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright Authors of Cilium
     3  
     4  package nat
     5  
     6  import (
     7  	"fmt"
     8  	"unsafe"
     9  
    10  	"github.com/cilium/cilium/pkg/bpf"
    11  	"github.com/cilium/cilium/pkg/byteorder"
    12  	"github.com/cilium/cilium/pkg/tuple"
    13  	"github.com/cilium/cilium/pkg/types"
    14  )
    15  
    16  // NatEntry6 represents an IPv6 entry in the NAT table.
    17  type NatEntry6 struct {
    18  	Created uint64     `align:"created"`
    19  	NeedsCT uint64     `align:"needs_ct"`
    20  	Pad1    uint64     `align:"pad1"`
    21  	Pad2    uint64     `align:"pad2"`
    22  	Addr    types.IPv6 `align:"to_saddr"`
    23  	Port    uint16     `align:"to_sport"`
    24  	_       [6]byte
    25  }
    26  
    27  // SizeofNatEntry6 is the size of the NatEntry6 type in bytes.
    28  const SizeofNatEntry6 = int(unsafe.Sizeof(NatEntry6{}))
    29  
    30  // String returns the readable format.
    31  func (n *NatEntry6) String() string {
    32  	return fmt.Sprintf("Addr=%s Port=%d Created=%d NeedsCT=%d\n",
    33  		n.Addr,
    34  		n.Port,
    35  		n.Created,
    36  		n.NeedsCT)
    37  }
    38  
    39  // Dump dumps NAT entry to string.
    40  func (n *NatEntry6) Dump(key NatKey, toDeltaSecs func(uint64) string) string {
    41  	var which string
    42  
    43  	if key.GetFlags()&tuple.TUPLE_F_IN != 0 {
    44  		which = "DST"
    45  	} else {
    46  		which = "SRC"
    47  	}
    48  	return fmt.Sprintf("XLATE_%s [%s]:%d Created=%s NeedsCT=%d\n",
    49  		which,
    50  		n.Addr,
    51  		n.Port,
    52  		toDeltaSecs(n.Created),
    53  		n.NeedsCT)
    54  }
    55  
    56  // ToHost converts NatEntry4 ports to host byte order.
    57  func (n *NatEntry6) ToHost() NatEntry {
    58  	x := *n
    59  	x.Port = byteorder.NetworkToHost16(n.Port)
    60  	return &x
    61  }
    62  
    63  func (n *NatEntry6) New() bpf.MapValue { return &NatEntry6{} }