github.com/zhyoulun/cilium@v1.6.12/pkg/policy/proxyid.go (about)

     1  // Copyright 2016-2018 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 policy
    16  
    17  import (
    18  	"fmt"
    19  	"strconv"
    20  	"strings"
    21  
    22  	"github.com/cilium/cilium/pkg/policy/trafficdirection"
    23  	"github.com/cilium/cilium/pkg/u8proto"
    24  )
    25  
    26  // ProxyID returns a unique string to identify a proxy mapping.
    27  func ProxyID(endpointID uint16, ingress bool, protocol string, port uint16) string {
    28  	direction := "egress"
    29  	if ingress {
    30  		direction = "ingress"
    31  	}
    32  	return fmt.Sprintf("%d:%s:%s:%d", endpointID, direction, protocol, port)
    33  }
    34  
    35  // ProxyIDFromKey returns a unique string to identify a proxy mapping.
    36  func ProxyIDFromKey(endpointID uint16, key Key) string {
    37  	return ProxyID(endpointID, key.TrafficDirection == trafficdirection.Ingress.Uint8(), u8proto.U8proto(key.Nexthdr).String(), key.DestPort)
    38  }
    39  
    40  // ProxyIDFromFilter returns a unique string to identify a proxy mapping.
    41  func ProxyIDFromFilter(endpointID uint16, l4 *L4Filter) string {
    42  	return ProxyID(endpointID, l4.Ingress, string(l4.Protocol), uint16(l4.Port))
    43  }
    44  
    45  // ParseProxyID parses a proxy ID returned by ProxyID and returns its components.
    46  func ParseProxyID(proxyID string) (endpointID uint16, ingress bool, protocol string, port uint16, err error) {
    47  	comps := strings.Split(proxyID, ":")
    48  	if len(comps) != 4 {
    49  		err = fmt.Errorf("invalid proxy ID structure: %s", proxyID)
    50  		return
    51  	}
    52  	epID, err := strconv.ParseUint(comps[0], 10, 16)
    53  	if err != nil {
    54  		return
    55  	}
    56  	endpointID = uint16(epID)
    57  	ingress = comps[1] == "ingress"
    58  	protocol = comps[2]
    59  	l4port, err := strconv.ParseUint(comps[3], 10, 16)
    60  	if err != nil {
    61  		return
    62  	}
    63  	port = uint16(l4port)
    64  	return
    65  }