コード例 #1
0
// testBPFProgram builds a BPF program for the input server and compares the
// output of the program against the input AoE header, returning whether or
// not the header was accepted by the filter.
func testBPFProgram(t *testing.T, s *Server, h *aoe.Header) bool {
	filter, ok := bpf.Disassemble(s.mustAssembleBPF(testMTU))
	if !ok {
		t.Fatal("failed to decode all BPF instructions")
	}

	vm, err := bpf.NewVM(filter)
	if !ok {
		t.Fatalf("failed to load BPF program: %v", err)
	}

	// Fill in empty AoE header fields not relevant to this test
	h.Version = aoe.Version
	h.Command = aoe.CommandQueryConfigInformation
	h.Arg = &aoe.ConfigArg{
		Command: aoe.ConfigCommandRead,
	}

	hb, err := h.MarshalBinary()
	if err != nil {
		t.Fatalf("failed to marshal AoE header to binary: %v", err)
	}

	f := &ethernet.Frame{
		EtherType: aoe.EtherType,
		Payload:   hb,
	}
	fb, err := f.MarshalBinary()
	if err != nil {
		t.Fatalf("failed to marshal Ethernet frame to binary: %v", err)
	}

	out, err := vm.Run(fb)
	if err != nil {
		t.Fatalf("failed to run BPF program: %v", err)
	}

	return out > 0
}