forked from cilium/cilium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy.go
377 lines (305 loc) · 10.7 KB
/
proxy.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
// Copyright 2016-2018 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 proxy
import (
"context"
"fmt"
"math/rand"
"sync"
"time"
"github.com/cilium/cilium/api/v1/models"
"github.com/cilium/cilium/pkg/completion"
"github.com/cilium/cilium/pkg/envoy"
"github.com/cilium/cilium/pkg/lock"
"github.com/cilium/cilium/pkg/logging"
"github.com/cilium/cilium/pkg/logging/logfields"
"github.com/cilium/cilium/pkg/maps/proxymap"
"github.com/cilium/cilium/pkg/metrics"
"github.com/cilium/cilium/pkg/node"
"github.com/cilium/cilium/pkg/policy"
"github.com/cilium/cilium/pkg/proxy/logger"
"github.com/cilium/cilium/pkg/revert"
"github.com/sirupsen/logrus"
)
var (
log = logging.DefaultLogger.WithField(logfields.LogSubsys, "proxy")
)
// field names used while logging
const (
fieldMarker = "marker"
fieldSocket = "socket"
fieldFd = "fd"
fieldProxyRedirectID = "id"
// portReuseDelay is the delay until a port is being reused
portReuseDelay = 5 * time.Minute
// redirectCreationAttempts is the number of attempts to create a redirect
redirectCreationAttempts = 5
)
// Proxy maintains state about redirects
type Proxy struct {
*envoy.XDSServer
// stateDir is the path of the directory where the state of L7 proxies is
// stored.
stateDir string
// mutex is the lock required when modifying any proxy datastructure
mutex lock.RWMutex
// rangeMin is the minimum port used for proxy port allocation
rangeMin uint16
// rangeMax is the maximum port used for proxy port allocation.
// If port is unspecified, the proxy will automatically allocate
// ports out of the rangeMin-rangeMax range.
rangeMax uint16
// allocatedPorts is the map of all allocated proxy ports
allocatedPorts map[uint16]struct{}
// redirects is the map of all redirect configurations indexed by
// the redirect identifier. Redirects may be implemented by different
// proxies.
redirects map[string]*Redirect
}
// StartProxySupport starts the servers to support L7 proxies: xDS GRPC server
// and access log server.
func StartProxySupport(minPort uint16, maxPort uint16, stateDir string,
accessLogFile string, accessLogNotifier logger.LogRecordNotifier, accessLogMetadata []string) *Proxy {
xdsServer := envoy.StartXDSServer(stateDir)
if accessLogFile != "" {
if err := logger.OpenLogfile(accessLogFile); err != nil {
log.WithError(err).WithField(logfields.Path, accessLogFile).
Warn("Cannot open L7 access log")
}
}
if accessLogNotifier != nil {
logger.SetNotifier(accessLogNotifier)
}
if len(accessLogMetadata) > 0 {
logger.SetMetadata(accessLogMetadata)
}
envoy.StartAccessLogServer(stateDir, xdsServer, DefaultEndpointInfoRegistry)
return &Proxy{
XDSServer: xdsServer,
stateDir: stateDir,
rangeMin: minPort,
rangeMax: maxPort,
redirects: make(map[string]*Redirect),
allocatedPorts: make(map[uint16]struct{}),
}
}
var (
portRandomizer = rand.New(rand.NewSource(time.Now().UnixNano()))
portRandomizerMutex lock.Mutex
)
func (p *Proxy) allocatePort() (uint16, error) {
// Get a snapshot of the TCP and UDP ports already open locally.
openLocalPorts := readOpenLocalPorts(append(procNetTCPFiles, procNetUDPFiles...))
portRandomizerMutex.Lock()
defer portRandomizerMutex.Unlock()
for _, r := range portRandomizer.Perm(int(p.rangeMax - p.rangeMin + 1)) {
resPort := uint16(r) + p.rangeMin
if _, ok := p.allocatedPorts[resPort]; !ok {
if _, alreadyOpen := openLocalPorts[resPort]; !alreadyOpen {
return resPort, nil
}
}
}
return 0, fmt.Errorf("no available proxy ports")
}
var gcOnce sync.Once
// CreateOrUpdateRedirect creates or updates a L4 redirect with corresponding
// proxy configuration. This will allocate a proxy port as required and launch
// a proxy instance. If the redirect is already in place, only the rules will be
// updated.
func (p *Proxy) CreateOrUpdateRedirect(l4 *policy.L4Filter, id string, localEndpoint logger.EndpointUpdater,
wg *completion.WaitGroup) (redir *Redirect, err error, finalizeFunc revert.FinalizeFunc, revertFunc revert.RevertFunc) {
gcOnce.Do(func() {
go func() {
for {
time.Sleep(10 * time.Second)
if deleted := proxymap.GC(); deleted > 0 {
log.WithField("count", deleted).
Debug("Evicted entries from proxy table")
}
}
}()
})
p.mutex.Lock()
defer func() {
p.UpdateRedirectMetrics()
p.mutex.Unlock()
}()
scopedLog := log.WithField(fieldProxyRedirectID, id)
var revertStack revert.RevertStack
revertFunc = revertStack.Revert
var ok bool
if redir, ok = p.redirects[id]; ok {
redir.mutex.Lock()
if redir.parserType == l4.L7Parser {
updateRevertFunc := redir.updateRules(l4)
revertStack.Push(updateRevertFunc)
var implUpdateRevertFunc revert.RevertFunc
implUpdateRevertFunc, err = redir.implementation.UpdateRules(wg, l4)
if err != nil {
err = fmt.Errorf("unable to update existing redirect: %s", err)
return
}
revertStack.Push(implUpdateRevertFunc)
redir.lastUpdated = time.Now()
scopedLog.WithField(logfields.Object, logfields.Repr(redir)).
Debug("updated existing ", l4.L7Parser, " proxy instance")
redir.mutex.Unlock()
return
}
var removeRevertFunc revert.RevertFunc
err, finalizeFunc, removeRevertFunc = p.removeRedirect(id, wg)
redir.mutex.Unlock()
if err != nil {
err = fmt.Errorf("unable to remove old redirect: %s", err)
return
}
revertStack.Push(removeRevertFunc)
}
redir = newRedirect(localEndpoint, id)
redir.endpointID = localEndpoint.GetID()
redir.ingress = l4.Ingress
redir.parserType = l4.L7Parser
redir.updateRules(l4)
// Rely on create*Redirect to update rules, unlike the update case above.
for nRetry := 0; ; nRetry++ {
var to uint16
to, err = p.allocatePort()
if err != nil {
revertFunc() // Ignore errors while reverting. This is best-effort.
return
}
redir.ProxyPort = to
switch l4.L7Parser {
case policy.ParserTypeDNS:
redir.implementation, err = createDNSRedirect(redir, dnsConfiguration{}, DefaultEndpointInfoRegistry)
// DNS uses a fixed port. Release the allocated ProxyPort since normal
// cleanup will not (.ProxyPort is set in createDNSRedirect). This must
// be deferred because it is set below.
defer delete(p.allocatedPorts, to)
case policy.ParserTypeKafka:
redir.implementation, err = createKafkaRedirect(redir, kafkaConfiguration{}, DefaultEndpointInfoRegistry)
case policy.ParserTypeHTTP:
redir.implementation, err = createEnvoyRedirect(redir, p.stateDir, p.XDSServer, wg)
default:
redir.implementation, err = createEnvoyRedirect(redir, p.stateDir, p.XDSServer, wg)
}
switch {
case err == nil:
scopedLog.WithField(logfields.Object, logfields.Repr(redir)).
Debug("Created new ", l4.L7Parser, " proxy instance")
p.allocatedPorts[to] = struct{}{}
p.redirects[id] = redir
revertStack.Push(func() error {
completionCtx, cancel := context.WithCancel(context.Background())
proxyWaitGroup := completion.NewWaitGroup(completionCtx)
err, finalize, _ := p.RemoveRedirect(id, proxyWaitGroup)
// Don't wait for an ACK. This is best-effort. Just clean up the completions.
cancel()
proxyWaitGroup.Wait() // Ignore the returned error.
if err == nil && finalize != nil {
finalize()
}
return err
})
return
// an error occurred, and we have no more retries
case nRetry >= redirectCreationAttempts:
scopedLog.WithError(err).Error("Unable to create ", l4.L7Parser, " proxy")
revertFunc() // Ignore errors while reverting. This is best-effort.
return
// an error occurred and we can retry
default:
scopedLog.WithError(err).Warning("Unable to create ", l4.L7Parser, " proxy, will retry")
}
}
}
// RemoveRedirect removes an existing redirect.
func (p *Proxy) RemoveRedirect(id string, wg *completion.WaitGroup) (error, revert.FinalizeFunc, revert.RevertFunc) {
p.mutex.Lock()
defer func() {
p.UpdateRedirectMetrics()
p.mutex.Unlock()
}()
return p.removeRedirect(id, wg)
}
// removeRedirect removes an existing redirect. p.mutex must be held
func (p *Proxy) removeRedirect(id string, wg *completion.WaitGroup) (err error, finalizeFunc revert.FinalizeFunc, revertFunc revert.RevertFunc) {
log.WithField(fieldProxyRedirectID, id).
Debug("Removing proxy redirect")
r, ok := p.redirects[id]
if !ok {
return fmt.Errorf("unable to find redirect %s", id), nil, nil
}
delete(p.redirects, id)
implFinalizeFunc, implRevertFunc := r.implementation.Close(wg)
// Delay the release and reuse of the port number so it is guaranteed to be
// safe to listen on the port again. This can't be reverted, so do it in a
// FinalizeFunc.
proxyPort := r.ProxyPort
finalizeFunc = func() {
// break GC loop (implementation may point back to 'r')
r.implementation = nil
if implFinalizeFunc != nil {
implFinalizeFunc()
}
go func() {
time.Sleep(portReuseDelay)
// The cleanup of the proxymap is delayed a bit to ensure that
// the datapath has implemented the redirect change and we
// cleanup the map before we release the port and allow reuse
proxymap.CleanupOnRedirectClose(proxyPort)
p.mutex.Lock()
delete(p.allocatedPorts, proxyPort)
p.mutex.Unlock()
log.WithField(fieldProxyRedirectID, id).Debugf("Delayed release of proxy port %d", proxyPort)
}()
}
revertFunc = func() error {
if implRevertFunc != nil {
return implRevertFunc()
}
p.mutex.Lock()
p.redirects[id] = r
p.mutex.Unlock()
return nil
}
return
}
// ChangeLogLevel changes proxy log level to correspond to the logrus log level 'level'.
func ChangeLogLevel(level logrus.Level) {
if envoyProxy != nil {
envoyProxy.ChangeLogLevel(level)
}
}
// GetStatusModel returns the proxy status as API model
func (p *Proxy) GetStatusModel() *models.ProxyStatus {
p.mutex.RLock()
defer p.mutex.RUnlock()
return &models.ProxyStatus{
IP: node.GetInternalIPv4().String(),
PortRange: fmt.Sprintf("%d-%d", p.rangeMin, p.rangeMax),
}
}
// UpdateRedirectMetrics updates the redirect metrics per application protocol
// in Prometheus. Lock needs to be held to call this function.
func (p *Proxy) UpdateRedirectMetrics() {
result := map[string]int{}
for _, redirect := range p.redirects {
result[string(redirect.parserType)]++
}
for proto, count := range result {
metrics.ProxyRedirects.WithLabelValues(proto).Set(float64(count))
}
}