Skip to content

Commit

Permalink
[BPF] IPv6 policy tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tomastigera committed Aug 25, 2023
1 parent 6bf83e2 commit 55b82f3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 27 deletions.
6 changes: 0 additions & 6 deletions felix/bpf/state/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ package state
import (
"unsafe"

log "github.com/sirupsen/logrus"

"github.com/projectcalico/calico/felix/bpf/maps"
)

Expand Down Expand Up @@ -122,10 +120,6 @@ type State struct {
const expectedSize = 464

func (s *State) AsBytes() []byte {
size := unsafe.Sizeof(State{})
if size != expectedSize {
log.WithField("size", size).Panic("Incorrect struct size")
}
bPtr := (*[expectedSize]byte)(unsafe.Pointer(s))
bytes := make([]byte, expectedSize)
copy(bytes, bPtr[:])
Expand Down
37 changes: 16 additions & 21 deletions felix/bpf/ut/pol_prog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import (
"github.com/projectcalico/calico/felix/proto"
)

func TestLoadAllowAllProgram(t *testing.T) {
func TestPolicyLoadAllowAllProgram(t *testing.T) {
RegisterTestingT(t)

b := asm.NewBlock(false)
Expand All @@ -60,7 +60,7 @@ func TestLoadAllowAllProgram(t *testing.T) {
Expect(rc.RC).To(BeNumerically("==", -1))
}

func TestLoadProgramWithMapAccess(t *testing.T) {
func TestPolicyLoadProgramWithMapAccess(t *testing.T) {
RegisterTestingT(t)

ipsMap := ipsets.Map()
Expand Down Expand Up @@ -113,7 +113,7 @@ func makeRulesSingleTier(protoRules []*proto.Rule) polprog.Rules {
}
}

func TestLoadKitchenSinkPolicy(t *testing.T) {
func TestPolicyLoadKitchenSinkPolicy(t *testing.T) {
RegisterTestingT(t)
alloc := idalloc.New()
allocID := func(id string) string {
Expand Down Expand Up @@ -164,7 +164,7 @@ func TestLoadKitchenSinkPolicy(t *testing.T) {
Expect(fd.Close()).NotTo(HaveOccurred())
}

func TestLoadGarbageProgram(t *testing.T) {
func TestPolicyLoadGarbageProgram(t *testing.T) {
RegisterTestingT(t)

var insns asm.Insns
Expand Down Expand Up @@ -2206,32 +2206,20 @@ func wrap(p polProgramTest) polProgramTestWrapper {
return polProgramTestWrapper{p}
}

func TestPolicyPrograms(t *testing.T) {
func TestPolicyPolicyPrograms(t *testing.T) {
for i, p := range polProgramTests {
if p.ForIPv6 {
// XXX skip for now
continue
}
t.Run(fmt.Sprintf("%d:Policy=%s", i, p.PolicyName), func(t *testing.T) { runTest(t, wrap(p)) })
}
}

func TestHostPolicyPrograms(t *testing.T) {
func TestPolicyHostPolicyPrograms(t *testing.T) {
for i, p := range hostPolProgramTests {
if p.ForIPv6 {
// XXX skip for now
continue
}
t.Run(fmt.Sprintf("%d:Policy=%s", i, p.PolicyName), func(t *testing.T) { runTest(t, wrap(p)) })
}
}

func TestXDPPolicyPrograms(t *testing.T) {
func TestPolicyXDPPolicyPrograms(t *testing.T) {
for i, p := range xdpPolProgramTests {
if p.ForIPv6 {
// XXX skip for now
continue
}
t.Run(fmt.Sprintf("%d:Policy=%s", i, p.PolicyName), func(t *testing.T) { runTest(t, wrap(p)) })
}
}
Expand Down Expand Up @@ -2375,7 +2363,7 @@ func ipUintFromString(addrStr string, section int) uint32 {
return binary.LittleEndian.Uint32(addrBytes[section*4 : (section+1)*4])
}

func TestIPUintFromString(t *testing.T) {
func TestPolicyIPUintFromString(t *testing.T) {
RegisterTestingT(t)
Expect(ipUintFromString("10.0.0.1", 0)).To(Equal(uint32(0x0100000a)))
Expect(ipUintFromString("10.0.0.1", 1)).To(Equal(uint32(0)))
Expand Down Expand Up @@ -2423,8 +2411,15 @@ func runTest(t *testing.T, tp testPolicy) {
Expect(err).NotTo(HaveOccurred())

// Build the program.
allowIdx := tcdefs.ProgIndexAllowed
denyIdx := tcdefs.ProgIndexDrop
if tp.ForIPv6() {
allowIdx = tcdefs.ProgIndexV6Allowed
denyIdx = tcdefs.ProgIndexV6Drop
}

pg := polprog.NewBuilder(forceAlloc, ipsMap.MapFD(), testStateMap.MapFD(), jumpMap.MapFD(),
polprog.WithAllowDenyJumps(tcdefs.ProgIndexAllowed, tcdefs.ProgIndexDrop))
polprog.WithAllowDenyJumps(allowIdx, denyIdx))
if tp.ForIPv6() {
pg.EnableIPv6Mode()
}
Expand Down

0 comments on commit 55b82f3

Please sign in to comment.