Skip to content

Commit 22dbd7e

Browse files
authored
Merge pull request #77 from mzbenami/mbenami/field-match
add FieldMatch
2 parents b96e948 + 372902c commit 22dbd7e

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

ovs/match.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1346,3 +1346,28 @@ func (m *ipFragMatch) GoString() string {
13461346
func (m *ipFragMatch) MarshalText() ([]byte, error) {
13471347
return bprintf("%s=%s", ipFrag, m.flag), nil
13481348
}
1349+
1350+
// FieldMatch returns an fieldMatch.
1351+
func FieldMatch(field, srcOrValue string) Match {
1352+
return &fieldMatch{field: field, srcOrValue: srcOrValue}
1353+
}
1354+
1355+
// fieldMatch implements the Match interface and
1356+
// matches a given field against another a value, e.g. "0x123" or "1.2.3.4",
1357+
// or against another src field in the packet, e.g "arp_tpa" or "NXM_OF_ARP_TPA[]".
1358+
type fieldMatch struct {
1359+
field string
1360+
srcOrValue string
1361+
}
1362+
1363+
var _ Match = &fieldMatch{}
1364+
1365+
// GoString implements Match.
1366+
func (m *fieldMatch) GoString() string {
1367+
return fmt.Sprintf("ovs.FieldMatch(%v,%v)", m.field, m.srcOrValue)
1368+
}
1369+
1370+
// MarshalText implements Match.
1371+
func (m *fieldMatch) MarshalText() ([]byte, error) {
1372+
return bprintf("%s=%s", m.field, m.srcOrValue), nil
1373+
}

ovs/match_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1463,6 +1463,48 @@ func TestMatchIPFrag(t *testing.T) {
14631463
}
14641464
}
14651465

1466+
func TestMatchFieldMatch(t *testing.T) {
1467+
var tests = []struct {
1468+
desc string
1469+
field string
1470+
srcOrValue string
1471+
out string
1472+
}{
1473+
{
1474+
desc: "match on src field",
1475+
field: "nw_src",
1476+
srcOrValue: "nw_dst",
1477+
out: "nw_src=nw_dst",
1478+
},
1479+
{
1480+
desc: "match on literal value hex",
1481+
field: "dl_type",
1482+
srcOrValue: "0x0800",
1483+
out: "dl_type=0x0800",
1484+
},
1485+
{
1486+
desc: "match on literal IP address",
1487+
field: "nw_dst",
1488+
srcOrValue: "1.2.3.4",
1489+
out: "nw_dst=1.2.3.4",
1490+
},
1491+
}
1492+
1493+
for _, tt := range tests {
1494+
t.Run(tt.desc, func(t *testing.T) {
1495+
out, err := FieldMatch(tt.field, tt.srcOrValue).MarshalText()
1496+
if err != nil {
1497+
t.Fatalf("unexpected error: %v", err)
1498+
}
1499+
1500+
if want, got := tt.out, string(out); want != got {
1501+
t.Fatalf("unexpected Match output:\n- want: %q\n- got: %q",
1502+
want, got)
1503+
}
1504+
})
1505+
}
1506+
}
1507+
14661508
// mustParseMAC is a helper to parse a hardware address from a string using
14671509
// net.ParseMAC, that panic on failure.
14681510
func mustParseMAC(addr string) net.HardwareAddr {

0 commit comments

Comments
 (0)