Skip to content

Commit 2fbc32e

Browse files
authored
Merge pull request #60 from digitalocean/fhallal/arp_op
add ARP operation field to the list of fields that OVS can match on
2 parents 813765f + aef22fe commit 2fbc32e

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

ovs/match.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ const (
3535

3636
// Constants of full Match names.
3737
const (
38+
arpOp = "arp_op"
3839
arpSHA = "arp_sha"
3940
arpSPA = "arp_spa"
4041
arpTHA = "arp_tha"
@@ -433,6 +434,30 @@ func (m *neighborDiscoveryLinkLayerMatch) GoString() string {
433434
return fmt.Sprintf("ovs.NeighborDiscoveryTargetLinkLayer(%s)", syntax)
434435
}
435436

437+
// ARPOperation matches packets with the specified ARP operation matching oper.
438+
func ARPOperation(oper uint16) Match {
439+
return &arpOperationMatch{
440+
oper: oper,
441+
}
442+
}
443+
444+
var _ Match = &arpOperationMatch{}
445+
446+
// An arpOperationMatch is a Match returned by ARPOperation.
447+
type arpOperationMatch struct {
448+
oper uint16
449+
}
450+
451+
// MarshalText implements Match.
452+
func (m *arpOperationMatch) MarshalText() ([]byte, error) {
453+
return bprintf("%s=%d", arpOp, m.oper), nil
454+
}
455+
456+
// GoString implements Match.
457+
func (m *arpOperationMatch) GoString() string {
458+
return fmt.Sprintf("ovs.ARPOperation(%d)", m.oper)
459+
}
460+
436461
// ARPSourceHardwareAddress matches packets with an ARP source hardware address
437462
// (SHA) matching addr.
438463
func ARPSourceHardwareAddress(addr net.HardwareAddr) Match {

ovs/match_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1035,6 +1035,10 @@ func TestMatchGoString(t *testing.T) {
10351035
m: ConjunctionID(123),
10361036
s: `ovs.ConjunctionID(123)`,
10371037
},
1038+
{
1039+
m: ARPOperation(2),
1040+
s: `ovs.ARPOperation(2)`,
1041+
},
10381042
}
10391043

10401044
for _, tt := range tests {
@@ -1046,6 +1050,39 @@ func TestMatchGoString(t *testing.T) {
10461050
}
10471051
}
10481052

1053+
func TestMatchARPOperation(t *testing.T) {
1054+
var tests = []struct {
1055+
desc string
1056+
oper uint16
1057+
out string
1058+
}{
1059+
{
1060+
desc: "request",
1061+
oper: 1,
1062+
out: "arp_op=1",
1063+
},
1064+
{
1065+
desc: "response",
1066+
oper: 2,
1067+
out: "arp_op=2",
1068+
},
1069+
}
1070+
1071+
for _, tt := range tests {
1072+
t.Run(tt.desc, func(t *testing.T) {
1073+
out, err := ARPOperation(tt.oper).MarshalText()
1074+
if err != nil {
1075+
t.Fatalf("unexpected error: %v", err)
1076+
}
1077+
1078+
if want, got := tt.out, string(out); want != got {
1079+
t.Fatalf("unexpected Match output:\n- want: %q\n- got: %q",
1080+
want, got)
1081+
}
1082+
})
1083+
}
1084+
}
1085+
10491086
// mustParseMAC is a helper to parse a hardware address from a string using
10501087
// net.ParseMAC, that panic on failure.
10511088
func mustParseMAC(addr string) net.HardwareAddr {

0 commit comments

Comments
 (0)