This repository has been archived by the owner on Mar 20, 2024. It is now read-only.
forked from cilium/cilium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache.go
205 lines (171 loc) · 6.47 KB
/
cache.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// Copyright 2016-2019 Authors of Cilium
//
// 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 endpoint
import (
"fmt"
"github.com/cilium/cilium/common/addressing"
"github.com/cilium/cilium/pkg/identity"
"github.com/cilium/cilium/pkg/logging/logfields"
"github.com/cilium/cilium/pkg/mac"
"github.com/cilium/cilium/pkg/maps/lxcmap"
"github.com/cilium/cilium/pkg/option"
"github.com/sirupsen/logrus"
)
// epInfoCache describes the set of lxcmap entries necessary to describe an Endpoint
// in the BPF maps. It is generated while holding the Endpoint lock, then used
// after releasing that lock to push the entries into the datapath.
// Functions below implement the EndpointFrontend interface with this cached information.
type epInfoCache struct {
// revision is used by the endpoint regeneration code to determine
// whether this cache is out-of-date wrt the underlying endpoint.
revision uint64
// For lxcmap.EndpointFrontend
keys []*lxcmap.EndpointKey
value *lxcmap.EndpointInfo
// For datapath.loader.endpoint
epdir string
id uint64
ifName string
ipvlan bool
// For datapath.EndpointConfiguration
identity identity.NumericIdentity
mac mac.MAC
ipv4 addressing.CiliumIPv4
ipv6 addressing.CiliumIPv6
conntrackLocal bool
requireARPPassthrough bool
requireEgressProg bool
requireRouting bool
requireEndpointRoute bool
cidr4PrefixLengths, cidr6PrefixLengths []int
options *option.IntOptions
// endpoint is used to get the endpoint's logger.
//
// Do NOT use this for fetching endpoint data directly; this structure
// is intended as a safe cache of endpoint data that is assembled while
// holding the endpoint lock, for use beyond the holding of that lock.
// Dereferencing fields in this endpoint is not guaranteed to be safe.
endpoint *Endpoint
}
// Must be called when endpoint is still locked.
func (e *Endpoint) createEpInfoCache(epdir string) *epInfoCache {
cidr6, cidr4 := e.GetCIDRPrefixLengths()
ep := &epInfoCache{
revision: e.nextPolicyRevision,
keys: e.GetBPFKeys(),
epdir: epdir,
id: e.GetID(),
ifName: e.IfName,
ipvlan: e.HasIpvlanDataPath(),
identity: e.GetIdentity(),
mac: e.GetNodeMAC(),
ipv4: e.IPv4Address(),
ipv6: e.IPv6Address(),
conntrackLocal: e.ConntrackLocalLocked(),
requireARPPassthrough: e.RequireARPPassthrough(),
requireEgressProg: e.RequireEgressProg(),
requireRouting: e.RequireRouting(),
requireEndpointRoute: e.RequireEndpointRoute(),
cidr4PrefixLengths: cidr4,
cidr6PrefixLengths: cidr6,
options: e.Options.DeepCopy(),
endpoint: e,
}
var err error
ep.value, err = e.GetBPFValue()
if err != nil {
log.WithField(logfields.EndpointID, e.ID).WithError(err).Error("getBPFValue failed")
return nil
}
return ep
}
// InterfaceName returns the name of the link-layer interface used for
// communicating with the endpoint.
func (ep *epInfoCache) InterfaceName() string {
return ep.ifName
}
// MapPath returns tail call map path
func (ep *epInfoCache) MapPath() string {
return ep.endpoint.BPFIpvlanMapPath()
}
// GetID returns the endpoint's ID.
func (ep *epInfoCache) GetID() uint64 {
return ep.id
}
// StringID returns the endpoint's ID in a string.
func (ep *epInfoCache) StringID() string {
return fmt.Sprintf("%d", ep.id)
}
// GetIdentity returns the security identity of the endpoint.
func (ep *epInfoCache) GetIdentity() identity.NumericIdentity {
return ep.identity
}
// Logger returns the logger for the endpoint that is being cached.
func (ep *epInfoCache) Logger(subsystem string) *logrus.Entry {
return ep.endpoint.Logger(subsystem)
}
// HasIpvlanDataPath returns whether the endpoint's datapath is implemented via ipvlan.
func (ep *epInfoCache) HasIpvlanDataPath() bool {
return ep.ipvlan
}
// IPv4Address returns the cached IPv4 address for the endpoint.
func (ep *epInfoCache) IPv4Address() addressing.CiliumIPv4 {
return ep.ipv4
}
// IPv6Address returns the cached IPv6 address for the endpoint.
func (ep *epInfoCache) IPv6Address() addressing.CiliumIPv6 {
return ep.ipv6
}
// StateDir returns the directory for the endpoint's (next) state.
func (ep *epInfoCache) StateDir() string { return ep.epdir }
func (ep *epInfoCache) GetNodeMAC() mac.MAC { return ep.mac }
// GetBPFKeys returns all keys which should represent this endpoint in the BPF
// endpoints map
func (ep *epInfoCache) GetBPFKeys() []*lxcmap.EndpointKey {
return ep.keys
}
// GetBPFValue returns the value which should represent this endpoint in the
// BPF endpoints map
// Must only be called if init() succeeded.
func (ep *epInfoCache) GetBPFValue() (*lxcmap.EndpointInfo, error) {
return ep.value, nil
}
func (ep *epInfoCache) ConntrackLocalLocked() bool {
return ep.conntrackLocal
}
func (ep *epInfoCache) GetCIDRPrefixLengths() ([]int, []int) {
return ep.cidr6PrefixLengths, ep.cidr4PrefixLengths
}
func (ep *epInfoCache) GetOptions() *option.IntOptions {
return ep.options
}
// RequireARPPassthrough returns true if the datapath must implement ARP
// passthrough for this endpoint
func (ep *epInfoCache) RequireARPPassthrough() bool {
return ep.requireARPPassthrough
}
// RequireEgressProg returns true if the endpoint requires bpf_lxc with esction
// "to-container" to be attached at egress on the host facing veth pair
func (ep *epInfoCache) RequireEgressProg() bool {
return ep.requireEgressProg
}
// RequireRouting returns true if the endpoint requires BPF routing to be
// enabled, when disabled, routing is delegated to Linux routing
func (ep *epInfoCache) RequireRouting() bool {
return ep.requireRouting
}
// RequireEndpointRoute returns if the endpoint wants a per endpoint route
func (ep *epInfoCache) RequireEndpointRoute() bool {
return ep.requireEndpointRoute
}