Skip to content

Commit

Permalink
Merge pull request projectcalico#9258 from tomastigera/tomas-bpf-debu…
Browse files Browse the repository at this point in the history
…g-ipsets

[BPF] ipsets debug enhancements
  • Loading branch information
tomastigera authored Sep 20, 2024
2 parents 4191e20 + 5ee8c74 commit 7d3b780
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 1 deletion.
3 changes: 2 additions & 1 deletion felix/bpf-gpl/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ enum cali_globals_flags {
CALI_GLOBALS_RESERVED7 = 0x00000040,
CALI_GLOBALS_NO_DSR_CIDRS = 0x00000080,
CALI_GLOBALS_LO_UDP_ONLY = 0x00000100,
CALI_GLOBALS_REDIRECT_PEER = 0x00000200,
CALI_GLOBALS_RESERVED10 = 0x00000200,
CALI_GLOBALS_REDIRECT_PEER = 0x00000400,
};

struct cali_ctlb_globals {
Expand Down
28 changes: 28 additions & 0 deletions felix/bpf/ipsets/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package ipsets

import (
"encoding/binary"
"fmt"
"net"
"strconv"
"strings"
Expand Down Expand Up @@ -95,6 +96,10 @@ func (e IPSetEntry) Port() uint16 {
return binary.LittleEndian.Uint16(e[16:18])
}

func (e IPSetEntry) String() string {
return fmt.Sprintf("0x%08x %11s prefix %d port %d proto %d", e.SetID(), e.Addr(), e.PrefixLen(), e.Port(), e.Protocol())
}

func IPSetEntryFromBytes(b []byte) IPSetEntryInterface {
var e IPSetEntry
copy(e[:], b)
Expand Down Expand Up @@ -153,3 +158,26 @@ func ProtoIPSetMemberToBPFEntry(id uint64, member string) IPSetEntryInterface {
entry := MakeBPFIPSetEntry(id, cidr, port, protocol)
return entry
}

type MapMem map[IPSetEntry]struct{}

func MapMemIter(m MapMem) func(k, v []byte) {
ks := len(IPSetEntry{})

return func(k, v []byte) {
var key IPSetEntry
copy(key[:ks], k[:ks])

m[key] = struct{}{}
}
}

func (m MapMem) String() string {
var out string

for k := range m {
out += k.String() + "\n"
}

return out
}
28 changes: 28 additions & 0 deletions felix/bpf/ipsets/map6.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package ipsets

import (
"encoding/binary"
"fmt"
"net"
"strconv"
"strings"
Expand Down Expand Up @@ -81,6 +82,10 @@ func (e IPSetEntryV6) Port() uint16 {
return binary.LittleEndian.Uint16(e[28:30])
}

func (e IPSetEntryV6) String() string {
return fmt.Sprintf("0x%08x %20s prefix %d port %d proto %d", e.SetID(), e.Addr(), e.PrefixLen(), e.Port(), e.Protocol())
}

func IPSetEntryV6FromBytes(b []byte) IPSetEntryInterface {
var e IPSetEntryV6
copy(e[:], b)
Expand Down Expand Up @@ -138,3 +143,26 @@ func ProtoIPSetMemberToBPFEntryV6(id uint64, member string) IPSetEntryInterface
entry := MakeBPFIPSetEntryV6(id, cidr, port, protocol)
return entry
}

type MapMemV6 map[IPSetEntryV6]struct{}

func MapMemV6Iter(m MapMemV6) func(k, v []byte) {
ks := len(IPSetEntryV6{})

return func(k, v []byte) {
var key IPSetEntryV6
copy(key[:ks], k[:ks])

m[key] = struct{}{}
}
}

func (m MapMemV6) String() string {
var out string

for k := range m {
out += k.String() + "\n"
}

return out
}
15 changes: 15 additions & 0 deletions felix/fv/bpf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import (
"github.com/projectcalico/calico/felix/bpf"
"github.com/projectcalico/calico/felix/bpf/conntrack"
"github.com/projectcalico/calico/felix/bpf/ifstate"
"github.com/projectcalico/calico/felix/bpf/ipsets"
"github.com/projectcalico/calico/felix/bpf/maps"
"github.com/projectcalico/calico/felix/bpf/nat"
"github.com/projectcalico/calico/felix/bpf/proxy"
Expand Down Expand Up @@ -5258,6 +5259,20 @@ func dumpIfStateMap(felix *infrastructure.Felix) ifstate.MapMem {
return m
}

func dumpIPSetsMap(felix *infrastructure.Felix) ipsets.MapMem {
im := ipsets.Map()
m := make(ipsets.MapMem)
dumpBPFMap(felix, im, ipsets.MapMemIter(m))
return m
}

func dumpIPSets6Map(felix *infrastructure.Felix) ipsets.MapMemV6 {
im := ipsets.MapV6()
m := make(ipsets.MapMemV6)
dumpBPFMap(felix, im, ipsets.MapMemV6Iter(m))
return m
}

func ensureAllNodesBPFProgramsAttached(felixes []*infrastructure.Felix, ifacesExtra ...string) {
for _, felix := range felixes {
ensureBPFProgramsAttachedOffset(2, felix, ifacesExtra...)
Expand Down

0 comments on commit 7d3b780

Please sign in to comment.