github.com/looshlee/beatles@v0.0.0-20220727174639-742810ab631c/pkg/maps/nat/ipv6.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  	"fmt"
    19  	"unsafe"
    20  
    21  	"github.com/cilium/cilium/common/types"
    22  	"github.com/cilium/cilium/pkg/byteorder"
    23  	"github.com/cilium/cilium/pkg/tuple"
    24  )
    25  
    26  // NatEntry6 represents an IPv6 entry in the NAT table.
    27  // +k8s:deepcopy-gen=true
    28  // +k8s:deepcopy-gen:interfaces=github.com/cilium/cilium/pkg/bpf.MapValue
    29  type NatEntry6 struct {
    30  	Created   uint64     `align:"created"`
    31  	HostLocal uint64     `align:"host_local"`
    32  	Pad1      uint64     `align:"pad1"`
    33  	Pad2      uint64     `align:"pad2"`
    34  	Addr      types.IPv6 `align:"to_saddr"`
    35  	Port      uint16     `align:"to_sport"`
    36  }
    37  
    38  // GetValuePtr returns the unsafe.Pointer for n.
    39  func (n *NatEntry6) GetValuePtr() unsafe.Pointer { return unsafe.Pointer(n) }
    40  
    41  // String returns the readable format.
    42  func (n *NatEntry6) String() string {
    43  	return fmt.Sprintf("Addr=%s Port=%d Created=%d HostLocal=%d\n",
    44  		n.Addr,
    45  		n.Port,
    46  		n.Created,
    47  		n.HostLocal)
    48  }
    49  
    50  // Dump dumps NAT entry to string.
    51  func (n *NatEntry6) Dump(key NatKey, start uint64) string {
    52  	var which string
    53  
    54  	if key.GetFlags()&tuple.TUPLE_F_IN != 0 {
    55  		which = "DST"
    56  	} else {
    57  		which = "SRC"
    58  	}
    59  	return fmt.Sprintf("XLATE_%s [%s]:%d Created=%s HostLocal=%d\n",
    60  		which,
    61  		n.Addr,
    62  		n.Port,
    63  		NatDumpCreated(start, n.Created),
    64  		n.HostLocal)
    65  }
    66  
    67  // ToHost converts NatEntry4 ports to host byte order.
    68  func (n *NatEntry6) ToHost() NatEntry {
    69  	x := *n
    70  	x.Port = byteorder.NetworkToHost(n.Port).(uint16)
    71  	return &x
    72  }