go.ligato.io/vpp-agent/v3@v3.5.0/proto/ligato/linux/l3/models.go (about)

     1  // Copyright (c) 2017 Cisco and/or its affiliates.
     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 linux_l3
    16  
    17  import (
    18  	"strings"
    19  
    20  	"go.ligato.io/vpp-agent/v3/pkg/models"
    21  )
    22  
    23  // ModuleName is the module name used for models.
    24  const ModuleName = "linux.l3"
    25  
    26  var (
    27  	ModelARPEntry = models.Register(&ARPEntry{}, models.Spec{
    28  		Module:  ModuleName,
    29  		Version: "v2",
    30  		Type:    "arp",
    31  	}, models.WithNameTemplate("{{.Interface}}/{{.IpAddress}}"))
    32  
    33  	ModelRoute = models.Register(&Route{}, models.Spec{
    34  		Module:  ModuleName,
    35  		Version: "v2",
    36  		Type:    "route",
    37  	}, models.WithNameTemplate(
    38  		`{{with ipnet .DstNetwork}}{{printf "%s/%d" .IP .MaskSize}}`+
    39  			`{{else}}{{.DstNetwork}}{{end}}/{{.OutgoingInterface}}`,
    40  	))
    41  )
    42  
    43  // ArpKey returns the key used in ETCD to store configuration of a particular Linux ARP entry.
    44  func ArpKey(iface, ipAddr string) string {
    45  	return models.Key(&ARPEntry{
    46  		Interface: iface,
    47  		IpAddress: ipAddr,
    48  	})
    49  }
    50  
    51  // RouteKey returns the key used in ETCD to store configuration of a particular Linux route.
    52  func RouteKey(dstNetwork, outgoingInterface string) string {
    53  	return models.Key(&Route{
    54  		DstNetwork:        dstNetwork,
    55  		OutgoingInterface: outgoingInterface,
    56  	})
    57  }
    58  
    59  const (
    60  	/* Link-local route (derived) */
    61  
    62  	// StaticLinkLocalRouteKeyPrefix is a prefix for keys derived from link-local routes.
    63  	LinkLocalRouteKeyPrefix = "linux/link-local-route/"
    64  
    65  	// staticLinkLocalRouteKeyTemplate is a template for key derived from link-local route.
    66  	linkLocalRouteKeyTemplate = LinkLocalRouteKeyPrefix + "{out-iface}/dest-address/{dest-address}"
    67  )
    68  
    69  /* Link-local Route (derived) */
    70  
    71  // StaticLinkLocalRouteKey returns a derived key used to represent link-local route.
    72  func StaticLinkLocalRouteKey(dstAddr, outgoingInterface string) string {
    73  	key := strings.Replace(linkLocalRouteKeyTemplate, "{dest-address}", dstAddr, 1)
    74  	key = strings.Replace(key, "{out-iface}", outgoingInterface, 1)
    75  	return key
    76  }
    77  
    78  // StaticLinkLocalRoutePrefix returns longest-common prefix of keys representing
    79  // link-local routes that have the given outgoing Linux interface.
    80  func StaticLinkLocalRoutePrefix(outgoingInterface string) string {
    81  	return LinkLocalRouteKeyPrefix + outgoingInterface + "/"
    82  }
    83  
    84  // ParseStaticLinkLocalRouteKey parses route attributes from a key derived from link-local route.
    85  func ParseStaticLinkLocalRouteKey(key string) (dstAddr string, outgoingInterface string, isRouteKey bool) {
    86  	if strings.HasPrefix(key, LinkLocalRouteKeyPrefix) {
    87  		routeSuffix := strings.TrimPrefix(key, LinkLocalRouteKeyPrefix)
    88  		parts := strings.Split(routeSuffix, "/dest-address/")
    89  
    90  		if len(parts) != 2 {
    91  			return "", "", false
    92  		}
    93  		outgoingInterface = parts[0]
    94  		dstAddr = parts[1]
    95  		isRouteKey = true
    96  		return
    97  	}
    98  	return "", "", false
    99  }