Skip to content

Commit c06b927

Browse files
authored
Merge pull request #72 from mzbenami/mbenami/metadata
add metadata match
2 parents 4b28a97 + 79d6b93 commit c06b927

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

ovs/match.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ const (
5656
ipv6DST = "ipv6_dst"
5757
ipv6SRC = "ipv6_src"
5858
ipv6Label = "ipv6_label"
59+
metadata = "metadata"
5960
ndSLL = "nd_sll"
6061
ndTLL = "nd_tll"
6162
ndTarget = "nd_target"
@@ -1212,6 +1213,30 @@ func (m *tunnelIDMatch) MarshalText() ([]byte, error) {
12121213
return bprintf("%s=%#x/%#x", tunID, m.id, m.mask), nil
12131214
}
12141215

1216+
// Metadata returns a Match that matches the given metadata value.
1217+
func Metadata(m uint64) Match {
1218+
return &metadataMatch{
1219+
m: m,
1220+
}
1221+
}
1222+
1223+
var _ Match = &metadataMatch{}
1224+
1225+
// A metadataMatch is a Match against a metadata value.
1226+
type metadataMatch struct {
1227+
m uint64
1228+
}
1229+
1230+
// GoString implements Match.
1231+
func (m *metadataMatch) GoString() string {
1232+
return fmt.Sprintf("ovs.Metadata(%#x)", m.m)
1233+
}
1234+
1235+
// MarshalText implements Match.
1236+
func (m *metadataMatch) MarshalText() ([]byte, error) {
1237+
return bprintf("%s=%#x", metadata, m.m), nil
1238+
}
1239+
12151240
// matchIPv4AddressOrCIDR attempts to create a Match using the specified key
12161241
// and input string, which could be interpreted as an IPv4 address or IPv4
12171242
// CIDR block.

ovs/match_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1187,6 +1187,39 @@ func TestMatchTunnelID(t *testing.T) {
11871187
}
11881188
}
11891189

1190+
func TestMatchMetadata(t *testing.T) {
1191+
var tests = []struct {
1192+
desc string
1193+
m Match
1194+
out string
1195+
}{
1196+
{
1197+
desc: "metadata 0xa",
1198+
m: Metadata(0xa),
1199+
out: "metadata=0xa",
1200+
},
1201+
{
1202+
desc: "metadata max 64 bit",
1203+
m: Metadata(0xffffffffffffffff),
1204+
out: "metadata=0xffffffffffffffff",
1205+
},
1206+
}
1207+
1208+
for _, tt := range tests {
1209+
t.Run(tt.desc, func(t *testing.T) {
1210+
out, err := tt.m.MarshalText()
1211+
if err != nil {
1212+
t.Fatalf("unexpected error: %v", err)
1213+
}
1214+
1215+
if want, got := tt.out, string(out); want != got {
1216+
t.Fatalf("unexpected Match output:\n- want: %q\n- got: %q",
1217+
want, got)
1218+
}
1219+
})
1220+
}
1221+
}
1222+
11901223
func TestMatchGoString(t *testing.T) {
11911224
var tests = []struct {
11921225
m Match

0 commit comments

Comments
 (0)