Skip to content

Commit

Permalink
usm: change http2_in_flight from LRU map to HASH map (#20393)
Browse files Browse the repository at this point in the history
* [usm] change http2_in_flight from LRU map to HASH map
  • Loading branch information
amitslavin authored Nov 8, 2023
1 parent 4fe56c9 commit 4181d38
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pkg/network/ebpf/c/protocols/http2/maps-defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ BPF_LRU_MAP(http2_dynamic_table, dynamic_table_index_t, dynamic_table_entry_t, 0
BPF_HASH_MAP(http2_dynamic_counter_table, conn_tuple_t, u64, 0)

/* This map is used to keep track of in-flight HTTP2 transactions for each TCP connection */
BPF_LRU_MAP(http2_in_flight, http2_stream_key_t, http2_stream_t, 0)
BPF_HASH_MAP(http2_in_flight, http2_stream_key_t, http2_stream_t, 0)

/* This map serves the purpose of maintaining the current state of tail calls for each frame,
identified by a tuple consisting of con_tup and skb_info.
Expand Down
45 changes: 42 additions & 3 deletions pkg/network/protocols/http2/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ import (

manager "github.com/DataDog/ebpf-manager"

ddebpf "github.com/DataDog/datadog-agent/pkg/ebpf"
"github.com/DataDog/datadog-agent/pkg/network/config"
"github.com/DataDog/datadog-agent/pkg/network/ebpf/probes"
"github.com/DataDog/datadog-agent/pkg/network/protocols"
"github.com/DataDog/datadog-agent/pkg/network/protocols/events"
"github.com/DataDog/datadog-agent/pkg/network/protocols/http"
"github.com/DataDog/datadog-agent/pkg/network/usm/buildmode"
"github.com/DataDog/datadog-agent/pkg/network/usm/utils"
"github.com/DataDog/datadog-agent/pkg/util/log"
)

type protocol struct {
Expand All @@ -33,6 +35,7 @@ type protocol struct {
telemetry *http.Telemetry
// TODO: Do we need to duplicate?
statkeeper *http.StatKeeper
mapCleaner *ddebpf.MapCleaner
eventsConsumer *events.Consumer
}

Expand All @@ -49,7 +52,7 @@ const (
)

var Spec = &protocols.ProtocolSpec{
Factory: newHttpProtocol,
Factory: newHTTP2Protocol,
Maps: []*manager.Map{
{
Name: inFlightMap,
Expand Down Expand Up @@ -104,7 +107,7 @@ var Spec = &protocols.ProtocolSpec{
},
}

func newHttpProtocol(cfg *config.Config) (protocols.Protocol, error) {
func newHTTP2Protocol(cfg *config.Config) (protocols.Protocol, error) {
if !cfg.EnableHTTP2Monitoring {
return nil, nil
}
Expand Down Expand Up @@ -178,11 +181,17 @@ func (p *protocol) PreStart(mgr *manager.Manager) (err error) {
return
}

func (p *protocol) PostStart(_ *manager.Manager) error {
func (p *protocol) PostStart(mgr *manager.Manager) error {
// Setup map cleaner after manager start.
p.setupMapCleaner(mgr)

return nil
}

func (p *protocol) Stop(_ *manager.Manager) {
// mapCleaner handles nil pointer receivers
p.mapCleaner.Stop()

if p.eventsConsumer != nil {
p.eventsConsumer.Stop()
}
Expand Down Expand Up @@ -218,6 +227,36 @@ func (p *protocol) processHTTP2(data []byte) {
p.statkeeper.Process(tx)
}

func (p *protocol) setupMapCleaner(mgr *manager.Manager) {
http2Map, _, err := mgr.GetMap(inFlightMap)
if err != nil {
log.Errorf("error getting %q map: %s", inFlightMap, err)
return
}
mapCleaner, err := ddebpf.NewMapCleaner(http2Map, new(http2StreamKey), new(EbpfTx))
if err != nil {
log.Errorf("error creating map cleaner: %s", err)
return
}

ttl := p.cfg.HTTPIdleConnectionTTL.Nanoseconds()
mapCleaner.Clean(p.cfg.HTTPMapCleanerInterval, func(now int64, key, val interface{}) bool {
http2Txn, ok := val.(*EbpfTx)
if !ok {
return false
}

if updated := int64(http2Txn.Response_last_seen); updated > 0 {
return (now - updated) > ttl
}

started := int64(http2Txn.Request_started)
return started > 0 && (now-started) > ttl
})

p.mapCleaner = mapCleaner
}

// GetStats returns a map of HTTP2 stats stored in the following format:
// [source, dest tuple, request path] -> RequestStats object
func (p *protocol) GetStats() *protocols.ProtocolStats {
Expand Down

0 comments on commit 4181d38

Please sign in to comment.