/
events.go
286 lines (251 loc) · 7.89 KB
/
events.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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Cilium
package bpf
import (
"context"
"fmt"
"sync"
"sync/atomic"
"github.com/cilium/cilium/pkg/container"
"github.com/cilium/cilium/pkg/controller"
"github.com/cilium/cilium/pkg/lock"
"github.com/cilium/cilium/pkg/time"
)
// Action describes an action for map buffer events.
type Action uint8
const (
// MapUpdate describes a map.Update event.
MapUpdate Action = iota
// MapDelete describes a map.Delete event.
MapDelete
// MapDeleteAll describes a map.DeleteAll event which is aggregated into a single event
// to minimize memory and subscription buffer usage.
MapDeleteAll
)
var bpfEventBufferGCControllerGroup = controller.NewGroup("bpf-event-buffer-gc")
// String returns a string representation of an Action.
func (e Action) String() string {
switch e {
case MapUpdate:
return "update"
case MapDelete:
return "delete"
case MapDeleteAll:
return "delete-all"
default:
return "unknown"
}
}
// Event contains data about a bpf operation event.
type Event struct {
Timestamp time.Time
action Action
cacheEntry
}
// GetAction returns the event action string.
func (e *Event) GetAction() string {
return e.action.String()
}
// GetKey returns the string representation of a event key.
func (e Event) GetKey() string {
if e.cacheEntry.Key == nil {
return "<nil>"
}
return e.cacheEntry.Key.String()
}
// GetValue returns the string representation of a event value.
// Nil values (such as with deletes) are returned as a canonical
// string representation.
func (e Event) GetValue() string {
if e.cacheEntry.Value == nil {
return "<nil>"
}
return e.cacheEntry.Value.String()
}
// GetLastError returns the last error for an event.
func (e Event) GetLastError() error {
return e.cacheEntry.LastError
}
// GetDesiredAction returns the desired action enum for an event.
func (e Event) GetDesiredAction() DesiredAction {
return e.cacheEntry.DesiredAction
}
func (m *Map) initEventsBuffer(maxSize int, eventsTTL time.Duration) {
b := &eventsBuffer{
buffer: container.NewRingBuffer(maxSize),
eventTTL: eventsTTL,
}
if b.eventTTL > 0 {
m.scopedLogger().Debug("starting bpf map event buffer GC controller")
mapControllers.UpdateController(
fmt.Sprintf("bpf-event-buffer-gc-%s", m.name),
controller.ControllerParams{
Group: bpfEventBufferGCControllerGroup,
DoFunc: func(_ context.Context) error {
m.scopedLogger().Debugf("clearing bpf map events older than %s", b.eventTTL)
b.buffer.Compact(func(e interface{}) bool {
event, ok := e.(*Event)
if !ok {
log.WithError(wrongObjTypeErr(e)).Error("Failed to compact the event buffer")
return false
}
return time.Since(event.Timestamp) < b.eventTTL
})
return nil
},
RunInterval: b.eventTTL,
},
)
}
m.events = b
}
// eventsBuffer stores a buffer of events for auditing and debugging
// purposes.
type eventsBuffer struct {
buffer *container.RingBuffer
eventTTL time.Duration
subsLock lock.RWMutex
subscriptions []*Handle
}
// Handle allows for handling event streams safely outside of this package.
// The key design consideration for event streaming is that it is non-blocking.
// The eventsBuffer takes care of closing handles when their consumer is not reading
// off the buffer (or is not reading off it fast enough).
type Handle struct {
c chan *Event
closed atomic.Bool
closer *sync.Once
err error
}
// Returns read only channel for Handle subscription events. Channel should be closed with
// handle.Close() function.
func (h *Handle) C() <-chan *Event {
return h.c // return read only channel to prevent closing outside of Close(...).
}
// Close allows for safaley closing of a handle.
func (h *Handle) Close() {
h.close(nil)
}
func (h *Handle) close(err error) {
h.closer.Do(func() {
close(h.c)
h.err = err
h.closed.Store(true)
})
}
func (h *Handle) isClosed() bool {
return h.closed.Load()
}
func (h *Handle) isFull() bool {
return len(h.c) >= cap(h.c)
}
// This configures how big buffers are for channels used for streaming events from
// eventsBuffer.
//
// To prevent blocking bpf.Map operations, subscribed events are buffered per client handle.
// How fast subscribers will need to proceess events will depend on the event throughput.
// In this case, our throughput will be expected to be not above 100 events a second.
// Therefore the consumer will have 10ms to process each event. The channel is also
// given a constant buffer size in the case where events arrive at once (i.e. all 100 events
// arriving at the top of the second).
//
// NOTE: Although using timers/timed-contexts seems like an obvious choice for this use case,
// the timer.After implementation actually uses a large amount of memory. To reduce memory spikes
// in high throughput cases, we instead just use a sufficiently buffered channel.
const (
eventSubChanBufferSize = 32
maxConcurrentEventSubs = 32
)
func (eb *eventsBuffer) hasSubCapacity() bool {
eb.subsLock.RLock()
defer eb.subsLock.RUnlock()
return len(eb.subscriptions) <= maxConcurrentEventSubs
}
func (eb *eventsBuffer) dumpAndSubscribe(callback EventCallbackFunc, follow bool) (*Handle, error) {
if follow && !eb.hasSubCapacity() {
return nil, fmt.Errorf("exceeded max number of concurrent map event subscriptions %d", maxConcurrentEventSubs)
}
if callback != nil {
eb.dumpWithCallback(callback)
}
if !follow {
return nil, nil
}
h := &Handle{
c: make(chan *Event, eventSubChanBufferSize),
closer: &sync.Once{},
}
eb.subsLock.Lock()
defer eb.subsLock.Unlock()
eb.subscriptions = append(eb.subscriptions, h)
return h, nil
}
// DumpAndSubscribe dumps existing buffer, if callback is not nil. Followed by creating a
// subscription to the maps events buffer and returning the handle.
// These actions are done together so as to prevent possible missed events between the handoff
// of the callback and sub handle creation.
func (m *Map) DumpAndSubscribe(callback EventCallbackFunc, follow bool) (*Handle, error) {
// note: we have to hold rlock for the duration of this to prevent missed events between dump and sub.
// dumpAndSubscribe maintains its own write-lock for updating subscribers.
m.lock.RLock()
defer m.lock.RUnlock()
if !m.eventsBufferEnabled {
return nil, fmt.Errorf("map events not enabled for map %q", m.name)
}
return m.events.dumpAndSubscribe(callback, follow)
}
func (m *Map) IsEventsEnabled() bool {
return m.eventsBufferEnabled
}
func (eb *eventsBuffer) add(e *Event) {
eb.buffer.Add(e)
var activeSubs []*Handle
activeSubsLock := &lock.Mutex{}
wg := &sync.WaitGroup{}
for i, sub := range eb.subscriptions {
if sub.isClosed() { // sub will be removed.
continue
}
wg.Add(1)
go func(sub *Handle, i int) {
defer wg.Done()
if sub.isFull() {
err := fmt.Errorf("timed out waiting to send sub map event")
log.WithError(err).Warnf("subscription channel buffer %d was full, closing subscription", i)
sub.close(err)
} else {
sub.c <- e
activeSubsLock.Lock()
activeSubs = append(activeSubs, sub)
activeSubsLock.Unlock()
}
}(sub, i)
}
wg.Wait()
eb.subsLock.Lock()
defer eb.subsLock.Unlock()
eb.subscriptions = activeSubs
}
func wrongObjTypeErr(i any) error {
return fmt.Errorf("BUG: wrong object type in event ring buffer: %T", i)
}
func (eb *eventsBuffer) eventIsValid(e interface{}) bool {
event, ok := e.(*Event)
if !ok {
log.WithError(wrongObjTypeErr(e)).Error("Could not dump contents of events buffer")
return false
}
return eb.eventTTL == 0 || time.Since(event.Timestamp) <= eb.eventTTL
}
// EventCallbackFunc is used to dump events from a event buffer.
type EventCallbackFunc func(*Event)
func (eb *eventsBuffer) dumpWithCallback(callback EventCallbackFunc) {
eb.buffer.IterateValid(eb.eventIsValid, func(e interface{}) {
event, ok := e.(*Event)
if !ok {
log.WithError(wrongObjTypeErr(e)).Error("Could not dump contents of events buffer")
return
}
callback(event)
})
}