/
types.go
562 lines (474 loc) · 15.8 KB
/
types.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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package api
import (
"encoding/json"
"fmt"
"net"
"sort"
"strconv"
"strings"
"time"
"github.com/cilium/cilium/pkg/monitor/notifications"
)
// Must be synchronized with <bpf/lib/common.h>
const (
// 0-128 are reserved for BPF datapath events
MessageTypeUnspec = iota
// MessageTypeDrop is a BPF datapath notification carrying a DropNotify
// which corresponds to drop_notify defined in bpf/lib/drop.h
MessageTypeDrop
// MessageTypeDebug is a BPF datapath notification carrying a DebugMsg
// which corresponds to debug_msg defined in bpf/lib/dbg.h
MessageTypeDebug
// MessageTypeCapture is a BPF datapath notification carrying a DebugCapture
// which corresponds to debug_capture_msg defined in bpf/lib/dbg.h
MessageTypeCapture
// MessageTypeTrace is a BPF datapath notification carrying a TraceNotify
// which corresponds to trace_notify defined in bpf/lib/trace.h
MessageTypeTrace
// MessageTypePolicyVerdict is a BPF datapath notification carrying a PolicyVerdictNotify
// which corresponds to policy_verdict_notify defined in bpf/lib/policy_log.h
MessageTypePolicyVerdict
// MessageTypeRecCapture is a BPF datapath notification carrying a RecorderCapture
// which corresponds to capture_msg defined in bpf/lib/pcap.h
MessageTypeRecCapture
// MessageTypeTraceSock is a BPF datapath notification carrying a TraceNotifySock
// which corresponds to trace_sock_notify defined in bpf/lib/trace_sock.h
MessageTypeTraceSock
// 129-255 are reserved for agent level events
// MessageTypeAccessLog contains a pkg/proxy/accesslog.LogRecord
MessageTypeAccessLog = 129
// MessageTypeAgent is an agent notification carrying a AgentNotify
MessageTypeAgent = 130
)
const (
MessageTypeNameDrop = "drop"
MessageTypeNameDebug = "debug"
MessageTypeNameCapture = "capture"
MessageTypeNameTrace = "trace"
MessageTypeNameL7 = "l7"
MessageTypeNameAgent = "agent"
MessageTypeNamePolicyVerdict = "policy-verdict"
MessageTypeNameRecCapture = "recorder"
MessageTypeNameTraceSock = "trace-sock"
)
type MessageTypeFilter []int
var (
// MessageTypeNames is a map of all type names
MessageTypeNames = map[string]int{
MessageTypeNameDrop: MessageTypeDrop,
MessageTypeNameDebug: MessageTypeDebug,
MessageTypeNameCapture: MessageTypeCapture,
MessageTypeNameTrace: MessageTypeTrace,
MessageTypeNameL7: MessageTypeAccessLog,
MessageTypeNameAgent: MessageTypeAgent,
MessageTypeNamePolicyVerdict: MessageTypePolicyVerdict,
MessageTypeNameRecCapture: MessageTypeRecCapture,
MessageTypeNameTraceSock: MessageTypeTraceSock,
}
)
// AllMessageTypeNames returns a slice of MessageTypeNames
func AllMessageTypeNames() []string {
names := make([]string, 0, len(MessageTypeNames))
for name := range MessageTypeNames {
names = append(names, name)
}
// Sort by the underlying MessageType
sort.SliceStable(names, func(i, j int) bool {
return MessageTypeNames[names[i]] < MessageTypeNames[names[j]]
})
return names
}
// MessageTypeName returns the name for a message type or the numeric value if
// the name can't be found
func MessageTypeName(typ int) string {
for name, value := range MessageTypeNames {
if value == typ {
return name
}
}
return strconv.Itoa(typ)
}
func (m *MessageTypeFilter) String() string {
pieces := make([]string, 0, len(*m))
for _, typ := range *m {
pieces = append(pieces, MessageTypeName(typ))
}
return strings.Join(pieces, ",")
}
func (m *MessageTypeFilter) Set(value string) error {
i, err := MessageTypeNames[value]
if !err {
return fmt.Errorf("Unknown type (%s). Please use one of the following ones %v",
value, MessageTypeNames)
}
*m = append(*m, i)
return nil
}
func (m *MessageTypeFilter) Type() string {
return "[]string"
}
func (m *MessageTypeFilter) Contains(typ int) bool {
for _, v := range *m {
if v == typ {
return true
}
}
return false
}
// Must be synchronized with <bpf/lib/trace.h>
const (
TraceToLxc = iota
TraceToProxy
TraceToHost
TraceToStack
TraceToOverlay
TraceFromLxc
TraceFromProxy
TraceFromHost
TraceFromStack
TraceFromOverlay
TraceFromNetwork
TraceToNetwork
)
// TraceObservationPoints is a map of all supported trace observation points
var TraceObservationPoints = map[uint8]string{
TraceToLxc: "to-endpoint",
TraceToProxy: "to-proxy",
TraceToHost: "to-host",
TraceToStack: "to-stack",
TraceToOverlay: "to-overlay",
TraceToNetwork: "to-network",
TraceFromLxc: "from-endpoint",
TraceFromProxy: "from-proxy",
TraceFromHost: "from-host",
TraceFromStack: "from-stack",
TraceFromOverlay: "from-overlay",
TraceFromNetwork: "from-network",
}
// TraceObservationPoint returns the name of a trace observation point
func TraceObservationPoint(obsPoint uint8) string {
if str, ok := TraceObservationPoints[obsPoint]; ok {
return str
}
return fmt.Sprintf("%d", obsPoint)
}
// AgentNotify is a notification from the agent. The notification is stored
// in its JSON-encoded representation
type AgentNotify struct {
Type AgentNotification
Text string
}
// AgentNotifyMessage is a notification from the agent. It is similar to
// AgentNotify, but the notification is an unencoded struct. See the *Message
// constructors in this package for possible values.
type AgentNotifyMessage struct {
Type AgentNotification
Notification interface{}
}
// ToJSON encodes a AgentNotifyMessage to its JSON-based AgentNotify representation
func (m *AgentNotifyMessage) ToJSON() (AgentNotify, error) {
repr, err := json.Marshal(m.Notification)
if err != nil {
return AgentNotify{}, err
}
return AgentNotify{
Type: m.Type,
Text: string(repr),
}, nil
}
// AgentNotification specifies the type of agent notification
type AgentNotification uint32
const (
AgentNotifyUnspec AgentNotification = iota
AgentNotifyGeneric
AgentNotifyStart
AgentNotifyEndpointRegenerateSuccess
AgentNotifyEndpointRegenerateFail
AgentNotifyPolicyUpdated
AgentNotifyPolicyDeleted
AgentNotifyEndpointCreated
AgentNotifyEndpointDeleted
AgentNotifyIPCacheUpserted
AgentNotifyIPCacheDeleted
AgentNotifyServiceUpserted
AgentNotifyServiceDeleted
)
// AgentNotifications is a map of all supported agent notification types.
var AgentNotifications = map[AgentNotification]string{
AgentNotifyUnspec: "unspecified",
AgentNotifyGeneric: "Message",
AgentNotifyStart: "Cilium agent started",
AgentNotifyEndpointRegenerateSuccess: "Endpoint regenerated",
AgentNotifyEndpointCreated: "Endpoint created",
AgentNotifyEndpointDeleted: "Endpoint deleted",
AgentNotifyEndpointRegenerateFail: "Failed endpoint regeneration",
AgentNotifyIPCacheDeleted: "IPCache entry deleted",
AgentNotifyIPCacheUpserted: "IPCache entry upserted",
AgentNotifyPolicyUpdated: "Policy updated",
AgentNotifyPolicyDeleted: "Policy deleted",
AgentNotifyServiceDeleted: "Service deleted",
AgentNotifyServiceUpserted: "Service upserted",
}
func resolveAgentType(t AgentNotification) string {
if n, ok := AgentNotifications[t]; ok {
return n
}
return fmt.Sprintf("%d", t)
}
// DumpInfo dumps an agent notification
func (n *AgentNotify) DumpInfo() {
fmt.Printf(">> %s: %s\n", resolveAgentType(n.Type), n.Text)
}
func (n *AgentNotify) getJSON() string {
return fmt.Sprintf(`{"type":"agent","subtype":"%s","message":%s}`, resolveAgentType(n.Type), n.Text)
}
// DumpJSON prints notification in json format
func (n *AgentNotify) DumpJSON() {
fmt.Println(n.getJSON())
}
// PolicyUpdateNotification structures update notification
type PolicyUpdateNotification struct {
Labels []string `json:"labels,omitempty"`
Revision uint64 `json:"revision,omitempty"`
RuleCount int `json:"rule_count"`
}
// PolicyUpdateMessage constructs an agent notification message for policy updates
func PolicyUpdateMessage(numRules int, labels []string, revision uint64) AgentNotifyMessage {
notification := PolicyUpdateNotification{
Labels: labels,
Revision: revision,
RuleCount: numRules,
}
return AgentNotifyMessage{
Type: AgentNotifyPolicyUpdated,
Notification: notification,
}
}
// PolicyDeleteMessage constructs an agent notification message for policy deletion
func PolicyDeleteMessage(deleted int, labels []string, revision uint64) AgentNotifyMessage {
notification := PolicyUpdateNotification{
Labels: labels,
Revision: revision,
RuleCount: deleted,
}
return AgentNotifyMessage{
Type: AgentNotifyPolicyDeleted,
Notification: notification,
}
}
// EndpointRegenNotification structures regeneration notification
type EndpointRegenNotification struct {
ID uint64 `json:"id,omitempty"`
Labels []string `json:"labels,omitempty"`
Error string `json:"error,omitempty"`
}
// EndpointRegenMessage constructs an agent notification message for endpoint regeneration
func EndpointRegenMessage(e notifications.RegenNotificationInfo, err error) AgentNotifyMessage {
notification := EndpointRegenNotification{
ID: e.GetID(),
Labels: e.GetOpLabels(),
}
typ := AgentNotifyEndpointRegenerateSuccess
if err != nil {
notification.Error = err.Error()
typ = AgentNotifyEndpointRegenerateFail
}
return AgentNotifyMessage{
Type: typ,
Notification: notification,
}
}
// EndpointNotification structures the endpoint create or delete notification
type EndpointNotification struct {
EndpointRegenNotification
PodName string `json:"pod-name,omitempty"`
Namespace string `json:"namespace,omitempty"`
}
// EndpointCreateMessage constructs an agent notification message for endpoint creation
func EndpointCreateMessage(e notifications.RegenNotificationInfo) AgentNotifyMessage {
notification := EndpointNotification{
EndpointRegenNotification: EndpointRegenNotification{
ID: e.GetID(),
Labels: e.GetOpLabels(),
},
PodName: e.GetK8sPodName(),
Namespace: e.GetK8sNamespace(),
}
return AgentNotifyMessage{
Type: AgentNotifyEndpointCreated,
Notification: notification,
}
}
// EndpointDeleteMessage constructs an agent notification message for endpoint deletion
func EndpointDeleteMessage(e notifications.RegenNotificationInfo) AgentNotifyMessage {
notification := EndpointNotification{
EndpointRegenNotification: EndpointRegenNotification{
ID: e.GetID(),
Labels: e.GetOpLabels(),
},
PodName: e.GetK8sPodName(),
Namespace: e.GetK8sNamespace(),
}
return AgentNotifyMessage{
Type: AgentNotifyEndpointDeleted,
Notification: notification,
}
}
// IPCacheNotification structures ipcache change notifications
type IPCacheNotification struct {
CIDR string `json:"cidr"`
Identity uint32 `json:"id"`
OldIdentity *uint32 `json:"old-id,omitempty"`
HostIP net.IP `json:"host-ip,omitempty"`
OldHostIP net.IP `json:"old-host-ip,omitempty"`
EncryptKey uint8 `json:"encrypt-key"`
Namespace string `json:"namespace,omitempty"`
PodName string `json:"pod-name,omitempty"`
}
// IPCacheUpsertedMessage constructs an agent notification message for ipcache upsertions
func IPCacheUpsertedMessage(cidr string, id uint32, oldID *uint32, hostIP net.IP, oldHostIP net.IP,
encryptKey uint8, namespace, podName string) AgentNotifyMessage {
notification := IPCacheNotification{
CIDR: cidr,
Identity: id,
OldIdentity: oldID,
HostIP: hostIP,
OldHostIP: oldHostIP,
EncryptKey: encryptKey,
Namespace: namespace,
PodName: podName,
}
return AgentNotifyMessage{
Type: AgentNotifyIPCacheUpserted,
Notification: notification,
}
}
// IPCacheDeletedMessage constructs an agent notification message for ipcache deletions
func IPCacheDeletedMessage(cidr string, id uint32, oldID *uint32, hostIP net.IP, oldHostIP net.IP,
encryptKey uint8, namespace, podName string) AgentNotifyMessage {
notification := IPCacheNotification{
CIDR: cidr,
Identity: id,
OldIdentity: oldID,
HostIP: hostIP,
OldHostIP: oldHostIP,
EncryptKey: encryptKey,
Namespace: namespace,
PodName: podName,
}
return AgentNotifyMessage{
Type: AgentNotifyIPCacheDeleted,
Notification: notification,
}
}
// TimeNotification structures agent start notification
type TimeNotification struct {
Time string `json:"time"`
}
// StartMessage constructs an agent notification message when the agent starts
func StartMessage(t time.Time) AgentNotifyMessage {
notification := TimeNotification{
Time: t.Format(time.RFC3339Nano),
}
return AgentNotifyMessage{
Type: AgentNotifyStart,
Notification: notification,
}
}
// ServiceUpsertNotificationAddr is part of ServiceUpsertNotification
type ServiceUpsertNotificationAddr struct {
IP net.IP `json:"ip"`
Port uint16 `json:"port"`
}
// ServiceUpsertNotification structures service upsert notifications
type ServiceUpsertNotification struct {
ID uint32 `json:"id"`
Frontend ServiceUpsertNotificationAddr `json:"frontend-address"`
Backends []ServiceUpsertNotificationAddr `json:"backend-addresses"`
Type string `json:"type,omitempty"`
// Deprecated: superseded by ExtTrafficPolicy.
TrafficPolicy string `json:"traffic-policy,omitempty"`
ExtTrafficPolicy string `json:"ext-traffic-policy,omitempty"`
IntTrafficPolicy string `json:"int-traffic-policy,omitempty"`
Name string `json:"name,omitempty"`
Namespace string `json:"namespace,,omitempty"`
}
// ServiceUpsertMessage constructs an agent notification message for service upserts
func ServiceUpsertMessage(
id uint32,
frontend ServiceUpsertNotificationAddr,
backends []ServiceUpsertNotificationAddr,
svcType, svcExtTrafficPolicy, svcIntTrafficPolicy, svcName, svcNamespace string,
) AgentNotifyMessage {
notification := ServiceUpsertNotification{
ID: id,
Frontend: frontend,
Backends: backends,
Type: svcType,
TrafficPolicy: svcExtTrafficPolicy,
ExtTrafficPolicy: svcExtTrafficPolicy,
IntTrafficPolicy: svcIntTrafficPolicy,
Name: svcName,
Namespace: svcNamespace,
}
return AgentNotifyMessage{
Type: AgentNotifyServiceUpserted,
Notification: notification,
}
}
// ServiceDeleteNotification structures service delete notifications
type ServiceDeleteNotification struct {
ID uint32 `json:"id"`
}
// ServiceDeleteMessage constructs an agent notification message for service deletions
func ServiceDeleteMessage(id uint32) AgentNotifyMessage {
notification := ServiceDeleteNotification{
ID: id,
}
return AgentNotifyMessage{
Type: AgentNotifyServiceDeleted,
Notification: notification,
}
}
const (
// PolicyIngress is the value of Flags&PolicyNotifyFlagDirection for ingress traffic
PolicyIngress = 1
// PolicyEgress is the value of Flags&PolicyNotifyFlagDirection for egress traffic
PolicyEgress = 2
// PolicyMatchNone is the value of MatchType indicatating no policy match
PolicyMatchNone = 0
// PolicyMatchL3Only is the value of MatchType indicating a L3-only match
PolicyMatchL3Only = 1
// PolicyMatchL3L4 is the value of MatchType indicating a L3+L4 match
PolicyMatchL3L4 = 2
// PolicyMatchL4Only is the value of MatchType indicating a L4-only match
PolicyMatchL4Only = 3
// PolicyMatchAll is the value of MatchType indicating an allow-all match
PolicyMatchAll = 4
// PolicyMatchL3Proto is the value of MatchType indicating a L3 and protocol match
PolicyMatchL3Proto = 5
// PolicyMatchProtoOnly is the value of MatchType indicating only a protocol match
PolicyMatchProtoOnly = 6
)
type PolicyMatchType int
func (m PolicyMatchType) String() string {
switch m {
case PolicyMatchL3Only:
return "L3-Only"
case PolicyMatchL3L4:
return "L3-L4"
case PolicyMatchL4Only:
return "L4-Only"
case PolicyMatchAll:
return "all"
case PolicyMatchNone:
return "none"
case PolicyMatchL3Proto:
return "L3-Proto"
case PolicyMatchProtoOnly:
return "Proto-Only"
}
return "unknown"
}