Skip to content

Commit ad1cd75

Browse files
committed
ovs: add conj_id matcher
1 parent 12e0112 commit ad1cd75

File tree

4 files changed

+73
-0
lines changed

4 files changed

+73
-0
lines changed

ovs/match.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ const (
3939
arpSPA = "arp_spa"
4040
arpTHA = "arp_tha"
4141
arpTPA = "arp_tpa"
42+
conjID = "conj_id"
4243
ctMark = "ct_mark"
4344
ctState = "ct_state"
4445
ctZone = "ct_zone"
@@ -247,6 +248,29 @@ func (m *networkMatch) GoString() string {
247248
return fmt.Sprintf("ovs.NetworkDestination(%q)", m.ip)
248249
}
249250

251+
// ConjunctionID matches flows that have matched all dimension of a conjunction
252+
// inside of the openflow table.
253+
func ConjunctionID(id uint32) Match {
254+
return &conjunctionIDMatch{
255+
id: id,
256+
}
257+
}
258+
259+
// A conjunctionIDMatch is a Match returned by ConjunctionID
260+
type conjunctionIDMatch struct {
261+
id uint32
262+
}
263+
264+
// MarshalText implements Match.
265+
func (m *conjunctionIDMatch) MarshalText() ([]byte, error) {
266+
return bprintf("conj_id=%v", m.id), nil
267+
}
268+
269+
// GoString implements Match.
270+
func (m *conjunctionIDMatch) GoString() string {
271+
return fmt.Sprintf("ovs.ConjunctionID(%v)", m.id)
272+
}
273+
250274
// NetworkProtocol matches packets with the specified IP or IPv6 protocol
251275
// number matching num. For example, specifying 1 when a Flow's Protocol
252276
// is IPv4 matches ICMP packets, or 58 when Protocol is IPv6 matches ICMPv6

ovs/match_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,39 @@ func TestMatchNetworkProtocol(t *testing.T) {
486486
}
487487
}
488488

489+
func TestMatchConjunctionID(t *testing.T) {
490+
var tests = []struct {
491+
desc string
492+
num uint32
493+
out string
494+
}{
495+
{
496+
desc: "ID 1",
497+
num: 1,
498+
out: "conj_id=1",
499+
},
500+
{
501+
desc: "ID 11111",
502+
num: 11111,
503+
out: "conj_id=11111",
504+
},
505+
}
506+
507+
for _, tt := range tests {
508+
t.Run(tt.desc, func(t *testing.T) {
509+
out, err := ConjunctionID(tt.num).MarshalText()
510+
if err != nil {
511+
t.Fatalf("unexpected error: %v", err)
512+
}
513+
514+
if want, got := tt.out, string(out); want != got {
515+
t.Fatalf("unexpected Match output:\n- want: %q\n- got: %q",
516+
want, got)
517+
}
518+
})
519+
}
520+
}
521+
489522
func TestMatchConnectionTrackingState(t *testing.T) {
490523
var tests = []struct {
491524
desc string
@@ -998,6 +1031,10 @@ func TestMatchGoString(t *testing.T) {
9981031
m: TunnelIDWithMask(0xa, 0x0f),
9991032
s: `ovs.TunnelIDWithMask(0xa, 0xf)`,
10001033
},
1034+
{
1035+
m: ConjunctionID(123),
1036+
s: `ovs.ConjunctionID(123)`,
1037+
},
10011038
}
10021039

10031040
for _, tt := range tests {

ovs/matchparser.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ func parseMatch(key string, value string) (Match, error) {
3333
return parseIntMatch(key, value, math.MaxUint8)
3434
case tpSRC, tpDST, ctZone:
3535
return parseIntMatch(key, value, math.MaxUint16)
36+
case conjID:
37+
return parseIntMatch(key, value, math.MaxUint32)
3638
case arpSPA:
3739
return ARPSourceProtocolAddress(value), nil
3840
case arpTPA:
@@ -108,6 +110,8 @@ func parseIntMatch(key string, value string, max int) (Match, error) {
108110
return TransportDestinationPort(uint16(t)), nil
109111
case ctZone:
110112
return ConnectionTrackingZone(uint16(t)), nil
113+
case conjID:
114+
return ConjunctionID(uint32(t)), nil
111115
}
112116

113117
return nil, fmt.Errorf("no action matched for %s=%s", key, value)

ovs/matchparser_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,14 @@ func Test_parseMatch(t *testing.T) {
287287
final: "tun_id=0xa/0x2",
288288
m: TunnelIDWithMask(10, 2),
289289
},
290+
{
291+
s: "conj_id=123",
292+
m: ConjunctionID(123),
293+
},
294+
{
295+
s: "conj_id=nope",
296+
invalid: true,
297+
},
290298
}
291299

292300
for _, tt := range tests {

0 commit comments

Comments
 (0)