Skip to content

Commit 6de2edd

Browse files
authored
Merge pull request #83 from btsomogyi/tunnel_params
Tunnel Parameter Support
2 parents f6d00ad + efbedcf commit 6de2edd

File tree

3 files changed

+234
-39
lines changed

3 files changed

+234
-39
lines changed

ovs/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@ err := c.OpenFlow.AddFlow("ovsbr0", &ovs.Flow{
2828
if err != nil {
2929
log.Fatalf("failed to add flow: %v", err)
3030
}
31-
```
31+
```

ovs/match.go

Lines changed: 205 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -35,44 +35,53 @@ const (
3535

3636
// Constants of full Match names.
3737
const (
38-
arpOp = "arp_op"
39-
arpSHA = "arp_sha"
40-
arpSPA = "arp_spa"
41-
arpTHA = "arp_tha"
42-
arpTPA = "arp_tpa"
43-
conjID = "conj_id"
44-
ctMark = "ct_mark"
45-
ctState = "ct_state"
46-
ctZone = "ct_zone"
47-
dlSRC = "dl_src"
48-
dlDST = "dl_dst"
49-
dlType = "dl_type"
50-
dlVLAN = "dl_vlan"
51-
dlVLANPCP = "dl_vlan_pcp"
52-
icmpType = "icmp_type"
53-
icmpCode = "icmp_code"
54-
icmp6Type = "icmpv6_type"
55-
icmp6Code = "icmpv6_code"
56-
ipv6DST = "ipv6_dst"
57-
ipv6SRC = "ipv6_src"
58-
ipv6Label = "ipv6_label"
59-
ipFrag = "ip_frag"
60-
metadata = "metadata"
61-
ndSLL = "nd_sll"
62-
ndTLL = "nd_tll"
63-
ndTarget = "nd_target"
64-
nwECN = "nw_ecn"
65-
nwDST = "nw_dst"
66-
nwProto = "nw_proto"
67-
nwTOS = "nw_tos"
68-
nwTTL = "nw_ttl"
69-
nwSRC = "nw_src"
70-
tcpFlags = "tcp_flags"
71-
tpDST = "tp_dst"
72-
tpSRC = "tp_src"
73-
tunID = "tun_id"
74-
vlanTCI = "vlan_tci"
75-
vlanTCI1 = "vlan_tci1"
38+
arpOp = "arp_op"
39+
arpSHA = "arp_sha"
40+
arpSPA = "arp_spa"
41+
arpTHA = "arp_tha"
42+
arpTPA = "arp_tpa"
43+
conjID = "conj_id"
44+
ctMark = "ct_mark"
45+
ctState = "ct_state"
46+
ctZone = "ct_zone"
47+
dlDST = "dl_dst"
48+
dlSRC = "dl_src"
49+
dlType = "dl_type"
50+
dlVLAN = "dl_vlan"
51+
dlVLANPCP = "dl_vlan_pcp"
52+
icmp6Code = "icmpv6_code"
53+
icmp6Type = "icmpv6_type"
54+
icmpCode = "icmp_code"
55+
icmpType = "icmp_type"
56+
ipFrag = "ip_frag"
57+
ipv6DST = "ipv6_dst"
58+
ipv6Label = "ipv6_label"
59+
ipv6SRC = "ipv6_src"
60+
metadata = "metadata"
61+
ndSLL = "nd_sll"
62+
ndTarget = "nd_target"
63+
ndTLL = "nd_tll"
64+
nwDST = "nw_dst"
65+
nwECN = "nw_ecn"
66+
nwProto = "nw_proto"
67+
nwSRC = "nw_src"
68+
nwTOS = "nw_tos"
69+
nwTTL = "nw_ttl"
70+
tcpFlags = "tcp_flags"
71+
tpDST = "tp_dst"
72+
tpSRC = "tp_src"
73+
tunDST = "tun_dst"
74+
tunFlags = "tun_flags"
75+
tunGbpFlags = "tun_gbp_flags"
76+
tunGbpID = "tun_gbp_id"
77+
tunID = "tun_id"
78+
tunSRC = "tun_src"
79+
tunTOS = "tun_tos"
80+
tunTTL = "tun_ttl"
81+
tunv6DST = "tun_ipv6_dst"
82+
tunv6SRC = "tun_ipv6_src"
83+
vlanTCI1 = "vlan_tci1"
84+
vlanTCI = "vlan_tci"
7685
)
7786

7887
// A Match is a type which can be marshaled into an OpenFlow packet matching
@@ -336,6 +345,78 @@ func (t *networkTOS) GoString() string {
336345
return fmt.Sprintf("ovs.NetworkTOS(%d)", t.tos)
337346
}
338347

348+
// TunnelGBP returns a new tunnelGBP
349+
func TunnelGBP(gbp int) Match {
350+
return &tunnelGBP{
351+
gbp: gbp,
352+
}
353+
}
354+
355+
var _ Match = &tunnelGBP{}
356+
357+
// tunnelGBP is a match for tunnel GBP
358+
type tunnelGBP struct {
359+
gbp int
360+
}
361+
362+
// MarshalText implements Match.
363+
func (t *tunnelGBP) MarshalText() ([]byte, error) {
364+
return bprintf("tun_gbp_id=%d", t.gbp), nil
365+
}
366+
367+
// GoString implements Match.
368+
func (t *tunnelGBP) GoString() string {
369+
return fmt.Sprintf("ovs.TunnelGBP(%d)", t.gbp)
370+
}
371+
372+
// TunnelGbpFlags returns a new tunnelFlags
373+
func TunnelGbpFlags(gbpFlags int) Match {
374+
return &tunnelGbpFlags{
375+
gbpFlags: gbpFlags,
376+
}
377+
}
378+
379+
var _ Match = &tunnelGbpFlags{}
380+
381+
// tunnelGbpFlags is a match for tunnel Flags
382+
type tunnelGbpFlags struct {
383+
gbpFlags int
384+
}
385+
386+
// MarshalText implements Match.
387+
func (t *tunnelGbpFlags) MarshalText() ([]byte, error) {
388+
return bprintf("tun_gbp_flags=%d", t.gbpFlags), nil
389+
}
390+
391+
// GoString implements Match.
392+
func (t *tunnelGbpFlags) GoString() string {
393+
return fmt.Sprintf("ovs.TunnelGbpFlags(%d)", t.gbpFlags)
394+
}
395+
396+
// TunnelFlags returns a new tunnelFlags
397+
func TunnelFlags(flags int) Match {
398+
return &tunnelFlags{
399+
flags: flags,
400+
}
401+
}
402+
403+
var _ Match = &tunnelFlags{}
404+
405+
// tunnelFlags is a match for tunnel Flags
406+
type tunnelFlags struct {
407+
flags int
408+
}
409+
410+
// MarshalText implements Match.
411+
func (t *tunnelFlags) MarshalText() ([]byte, error) {
412+
return bprintf("tun_flags=%d", t.flags), nil
413+
}
414+
415+
// GoString implements Match.
416+
func (t *tunnelFlags) GoString() string {
417+
return fmt.Sprintf("ovs.TunnelFlags(%d)", t.flags)
418+
}
419+
339420
// NetworkTTL returns a new networkTTL
340421
func NetworkTTL(ttl int) Match {
341422
return &networkTTL{
@@ -360,6 +441,30 @@ func (t *networkTTL) GoString() string {
360441
return fmt.Sprintf("ovs.NetworkTTL(%d)", t.ttl)
361442
}
362443

444+
// TunnelTTL returns a new tunnelTTL
445+
func TunnelTTL(ttl int) Match {
446+
return &tunnelTTL{
447+
ttl: ttl,
448+
}
449+
}
450+
451+
var _ Match = &tunnelTTL{}
452+
453+
// tunnelTTL is a match for a tunnel time to live
454+
type tunnelTTL struct {
455+
ttl int
456+
}
457+
458+
// MarshalText implements Match.
459+
func (t *tunnelTTL) MarshalText() ([]byte, error) {
460+
return bprintf("tun_ttl=%d", t.ttl), nil
461+
}
462+
463+
// GoString implements Match.
464+
func (t *tunnelTTL) GoString() string {
465+
return fmt.Sprintf("ovs.TunnelTTL(%d)", t.ttl)
466+
}
467+
363468
// ConjunctionID matches flows that have matched all dimension of a conjunction
364469
// inside of the openflow table.
365470
func ConjunctionID(id uint32) Match {
@@ -368,6 +473,30 @@ func ConjunctionID(id uint32) Match {
368473
}
369474
}
370475

476+
// TunnelTOS returns a new tunnelTOS
477+
func TunnelTOS(tos int) Match {
478+
return &tunnelTOS{
479+
tos: tos,
480+
}
481+
}
482+
483+
var _ Match = &tunnelTOS{}
484+
485+
// tunnelTOS is a match for a tunnel type of service
486+
type tunnelTOS struct {
487+
tos int
488+
}
489+
490+
// MarshalText implements Match.
491+
func (t *tunnelTOS) MarshalText() ([]byte, error) {
492+
return bprintf("tun_tos=%d", t.tos), nil
493+
}
494+
495+
// GoString implements Match.
496+
func (t *tunnelTOS) GoString() string {
497+
return fmt.Sprintf("ovs.TunnelTOS(%d)", t.tos)
498+
}
499+
371500
// A conjunctionIDMatch is a Match returned by ConjunctionID
372501
type conjunctionIDMatch struct {
373502
id uint32
@@ -1214,6 +1343,44 @@ func (m *tunnelIDMatch) MarshalText() ([]byte, error) {
12141343
return bprintf("%s=%#x/%#x", tunID, m.id, m.mask), nil
12151344
}
12161345

1346+
// TunnelSrc returns a Match with specified Tunnel Source.
1347+
func TunnelSrc(addr string) Match {
1348+
return &tunnelMatch{
1349+
srcdst: source,
1350+
ip: addr,
1351+
}
1352+
}
1353+
1354+
// TunnelDst returns a Match with specified Tunnel Destination.
1355+
func TunnelDst(addr string) Match {
1356+
return &tunnelMatch{
1357+
srcdst: destination,
1358+
ip: addr,
1359+
}
1360+
}
1361+
1362+
var _ Match = &tunnelMatch{}
1363+
1364+
// A tunnelMatch is a Match against a tunnel {source|destination}.
1365+
type tunnelMatch struct {
1366+
srcdst string
1367+
ip string
1368+
}
1369+
1370+
// GoString implements Match.
1371+
func (m *tunnelMatch) GoString() string {
1372+
if m.srcdst == source {
1373+
return fmt.Sprintf("ovs.TunnelSrc(%q)", m.ip)
1374+
}
1375+
1376+
return fmt.Sprintf("ovs.TunnelDst(%q)", m.ip)
1377+
}
1378+
1379+
// MarshalText implements Match.
1380+
func (m *tunnelMatch) MarshalText() ([]byte, error) {
1381+
return matchIPv4AddressOrCIDR(fmt.Sprintf("tun_%s", m.srcdst), m.ip)
1382+
}
1383+
12171384
// Metadata returns a Match that matches the given metadata value.
12181385
func Metadata(m uint64) Match {
12191386
return &metadataMatch{

ovs/matchparser.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,18 +68,36 @@ func parseMatch(key string, value string) (Match, error) {
6868
return parseIntMatch(key, value, math.MaxInt32)
6969
case nwTTL:
7070
return parseIntMatch(key, value, math.MaxInt32)
71+
case tunTTL:
72+
return parseIntMatch(key, value, math.MaxInt32)
73+
case tunTOS:
74+
return parseIntMatch(key, value, math.MaxInt32)
7175
case nwTOS:
7276
return parseIntMatch(key, value, math.MaxInt32)
77+
case tunGbpID:
78+
return parseIntMatch(key, value, math.MaxInt32)
79+
case tunGbpFlags:
80+
return parseIntMatch(key, value, math.MaxInt32)
81+
case tunFlags:
82+
return parseIntMatch(key, value, math.MaxInt32)
7383
case inPort:
7484
return parseIntMatch(key, value, math.MaxInt32)
7585
case ipv6SRC:
7686
return IPv6Source(value), nil
7787
case ipv6DST:
7888
return IPv6Destination(value), nil
89+
case tunv6SRC:
90+
return IPv6Source(value), nil
91+
case tunv6DST:
92+
return IPv6Destination(value), nil
7993
case ipv6Label:
8094
return parseIPv6Label(value)
8195
case nwSRC:
8296
return NetworkSource(value), nil
97+
case tunSRC:
98+
return NetworkSource(value), nil
99+
case tunDST:
100+
return NetworkDestination(value), nil
83101
case nwDST:
84102
return NetworkDestination(value), nil
85103
case vlanTCI1:
@@ -132,8 +150,18 @@ func parseIntMatch(key string, value string, max int) (Match, error) {
132150
return NetworkECN(int(t)), nil
133151
case nwTTL:
134152
return NetworkTTL(int(t)), nil
153+
case tunTTL:
154+
return TunnelTTL(int(t)), nil
155+
case tunTOS:
156+
return TunnelTOS(int(t)), nil
135157
case nwTOS:
136158
return NetworkTOS(int(t)), nil
159+
case tunGbpID:
160+
return TunnelGBP(int(t)), nil
161+
case tunGbpFlags:
162+
return TunnelGbpFlags(int(t)), nil
163+
case tunFlags:
164+
return TunnelFlags(int(t)), nil
137165
case nwProto:
138166
return NetworkProtocol(uint8(t)), nil
139167
case ctZone:

0 commit comments

Comments
 (0)