This repository has been archived by the owner on Aug 3, 2020. It is now read-only.
forked from ligato/vpp-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
keys.go
122 lines (103 loc) · 4.12 KB
/
keys.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Copyright (c) 2017 Cisco and/or its affiliates.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package linux_l3
import (
"net"
"strconv"
"strings"
"github.com/ligato/vpp-agent/pkg/models"
)
// ModuleName is the module name used for models.
const ModuleName = "linux.l3"
var (
ModelARPEntry = models.Register(&ARPEntry{}, models.Spec{
Module: ModuleName,
Version: "v2",
Type: "arp",
}, models.WithNameTemplate("{{.Interface}}/{{.IpAddress}}"))
ModelRoute = models.Register(&Route{}, models.Spec{
Module: ModuleName,
Version: "v2",
Type: "route",
}, models.WithNameTemplate(
`{{with ipnet .DstNetwork}}{{printf "%s/%d" .IP .MaskSize}}{{end}}/{{.OutgoingInterface}}`,
))
)
// ArpKey returns the key used in ETCD to store configuration of a particular Linux ARP entry.
func ArpKey(iface, ipAddr string) string {
return models.Key(&ARPEntry{
Interface: iface,
IpAddress: ipAddr,
})
}
// RouteKey returns the key used in ETCD to store configuration of a particular Linux route.
func RouteKey(dstNetwork, outgoingInterface string) string {
return models.Key(&Route{
DstNetwork: dstNetwork,
OutgoingInterface: outgoingInterface,
})
}
const (
/* Link-local route (derived) */
// StaticLinkLocalRouteKeyPrefix is a prefix for keys derived from link-local routes.
LinkLocalRouteKeyPrefix = "linux/link-local-route/"
// staticLinkLocalRouteKeyTemplate is a template for key derived from link-local route.
linkLocalRouteKeyTemplate = LinkLocalRouteKeyPrefix + "{out-intf}/{dest-net}/{dest-mask}"
)
/* Link-local Route (derived) */
// StaticLinkLocalRouteKey returns a derived key used to represent link-local route.
func StaticLinkLocalRouteKey(dstAddr, outgoingInterface string) string {
return RouteKeyFromTemplate(linkLocalRouteKeyTemplate, dstAddr, outgoingInterface)
}
// StaticLinkLocalRoutePrefix returns longest-common prefix of keys representing
// link-local routes that have the given outgoing Linux interface.
func StaticLinkLocalRoutePrefix(outgoingInterface string) string {
return LinkLocalRouteKeyPrefix + outgoingInterface + "/"
}
// ParseStaticLinkLocalRouteKey parses route attributes from a key derived from link-local route.
func ParseStaticLinkLocalRouteKey(key string) (dstNetAddr *net.IPNet, outgoingInterface string, isRouteKey bool) {
return parseRouteFromKeySuffix(key, LinkLocalRouteKeyPrefix, "invalid Linux link-local Route key: ")
}
/* Route helpers */
// RouteKeyFromTemplate fills key template with route attributes.
func RouteKeyFromTemplate(template, dstAddr, outgoingInterface string) string {
_, dstNet, _ := net.ParseCIDR(dstAddr)
dstNetAddr := dstNet.IP.String()
dstNetMask, _ := dstNet.Mask.Size()
key := strings.Replace(template, "{dest-net}", dstNetAddr, 1)
key = strings.Replace(key, "{dest-mask}", strconv.Itoa(dstNetMask), 1)
key = strings.Replace(key, "{out-intf}", outgoingInterface, 1)
return key
}
// parseRouteFromKeySuffix parses destination network and outgoing interface from a route key suffix.
func parseRouteFromKeySuffix(key, prefix, errPrefix string) (dstNetAddr *net.IPNet, outgoingInterface string, isRouteKey bool) {
var err error
if strings.HasPrefix(key, prefix) {
routeSuffix := strings.TrimPrefix(key, prefix)
routeComps := strings.Split(routeSuffix, "/")
// beware: interface name may contain forward slashes
if len(routeComps) < 3 {
return nil, "", false
}
lastIdx := len(routeComps) - 1
_, dstNetAddr, err = net.ParseCIDR(routeComps[lastIdx-1] + "/" + routeComps[lastIdx])
if err != nil {
return nil, "", false
}
outgoingInterface = strings.Join(routeComps[:lastIdx-1], "/")
isRouteKey = true
return
}
return nil, "", false
}