Skip to content

Commit

Permalink
usm: http2: Unify telemetry maps
Browse files Browse the repository at this point in the history
Each eBPF map has a minimum size in bytes. Having plaintext and tls map versions for telemetry
introduced a waste in memory cost, and redundant code duplication. Instead of having 2 maps
with a single entry each, we can have a single map with 2 entries.
  • Loading branch information
guyarb committed Nov 17, 2024
1 parent dd3da58 commit 4264aee
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 21 deletions.
9 changes: 5 additions & 4 deletions pkg/network/ebpf/c/protocols/http2/decoding.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,17 @@ static __always_inline void pktbuf_skip_preface(pktbuf_t pkt) {

// Returns the telemetry pointer from the relevant map.
static __always_inline void* get_telemetry(pktbuf_t pkt) {
const __u32 zero = 0;
const __u32 plaintext_ey = 0;
const __u32 tls_key = 1;

pktbuf_map_lookup_option_t map_lookup_telemetry_array[] = {
[PKTBUF_SKB] = {
.map = &http2_telemetry,
.key = (void*)&zero,
.key = (void*)&plaintext_ey,
},
[PKTBUF_TLS] = {
.map = &tls_http2_telemetry,
.key = (void*)&zero,
.map = &http2_telemetry,
.key = (void*)&tls_key,
},
};
return pktbuf_map_lookup(pkt, map_lookup_telemetry_array);
Expand Down
3 changes: 1 addition & 2 deletions pkg/network/ebpf/c/protocols/http2/maps-defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ BPF_PERCPU_ARRAY_MAP(http2_ctx_heap, http2_ctx_t, 1)
* only key 0 is used
* value is a http2 telemetry object
*/
BPF_ARRAY_MAP(http2_telemetry, http2_telemetry_t, 1)
BPF_ARRAY_MAP(tls_http2_telemetry, http2_telemetry_t, 1)
BPF_ARRAY_MAP(http2_telemetry, http2_telemetry_t, 2)

#endif
17 changes: 5 additions & 12 deletions pkg/network/protocols/http2/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,6 @@ const (

// TelemetryMap is the name of the map used to retrieve plaintext metrics from the kernel
TelemetryMap = "http2_telemetry"
// TLSTelemetryMap is the name of the map used to retrieve metrics from the eBPF probes for TLS
TLSTelemetryMap = "tls_http2_telemetry"

tlsFirstFrameTailCall = "uprobe__http2_tls_handle_first_frame"
tlsFilterTailCall = "uprobe__http2_tls_filter"
Expand Down Expand Up @@ -316,13 +314,8 @@ func (p *Protocol) updateKernelTelemetry(mgr *manager.Manager) {
return
}

tlsMap, err := protocols.GetMap(mgr, TLSTelemetryMap)
if err != nil {
log.Warn(err)
return
}

var zero uint32
plaintextKey := uint32(0)
tlsKey := uint32(1)
http2Telemetry := &HTTP2Telemetry{}
ticker := time.NewTicker(30 * time.Second)

Expand All @@ -332,14 +325,14 @@ func (p *Protocol) updateKernelTelemetry(mgr *manager.Manager) {
for {
select {
case <-ticker.C:
if err := mp.Lookup(unsafe.Pointer(&zero), unsafe.Pointer(http2Telemetry)); err != nil {
if err := mp.Lookup(unsafe.Pointer(&plaintextKey), unsafe.Pointer(http2Telemetry)); err != nil {
log.Errorf("unable to lookup %q map: %s", TelemetryMap, err)
return
}
p.http2Telemetry.update(http2Telemetry, false)

if err := tlsMap.Lookup(unsafe.Pointer(&zero), unsafe.Pointer(http2Telemetry)); err != nil {
log.Errorf("unable to lookup %q map: %s", TLSTelemetryMap, err)
if err := mp.Lookup(unsafe.Pointer(&tlsKey), unsafe.Pointer(http2Telemetry)); err != nil {
log.Errorf("unable to lookup %q map: %s", TelemetryMap, err)
return
}
p.http2Telemetry.update(http2Telemetry, true)
Expand Down
6 changes: 3 additions & 3 deletions pkg/network/usm/usm_http2_monitor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1924,19 +1924,19 @@ func dialHTTP2Server(t *testing.T) net.Conn {
// getHTTP2KernelTelemetry returns the HTTP2 kernel telemetry
func getHTTP2KernelTelemetry(monitor *Monitor, isTLS bool) (*usmhttp2.HTTP2Telemetry, error) {
http2Telemetry := &usmhttp2.HTTP2Telemetry{}
var zero uint32

mapName := usmhttp2.TelemetryMap
key := uint32(0)
if isTLS {
mapName = usmhttp2.TLSTelemetryMap
key = uint32(1)
}

mp, _, err := monitor.ebpfProgram.GetMap(mapName)
if err != nil {
return nil, fmt.Errorf("unable to get %q map: %s", mapName, err)
}

if err := mp.Lookup(unsafe.Pointer(&zero), unsafe.Pointer(http2Telemetry)); err != nil {
if err := mp.Lookup(unsafe.Pointer(&key), unsafe.Pointer(http2Telemetry)); err != nil {
return nil, fmt.Errorf("unable to lookup %q map: %s", mapName, err)
}
return http2Telemetry, nil
Expand Down

0 comments on commit 4264aee

Please sign in to comment.