Skip to content

Commit 6b5dd30

Browse files
borkmannaboch
authored andcommitted
geneve: Support setting/getting source port range
Add support for geneve feature to specify source port range, see kernel commits: - e1f95b1992b8 ("geneve: Allow users to specify source port range") - 5a41a00cd5d5 ("geneve, specs: Add port range to rt_link specification") This is exactly equivalent on what is done in case of vxlan today. Signed-off-by: Daniel Borkmann <[email protected]>
1 parent d85a66b commit 6b5dd30

File tree

4 files changed

+32
-0
lines changed

4 files changed

+32
-0
lines changed

link.go

+2
Original file line numberDiff line numberDiff line change
@@ -1059,6 +1059,8 @@ type Geneve struct {
10591059
FlowBased bool
10601060
InnerProtoInherit bool
10611061
Df GeneveDf
1062+
PortLow int
1063+
PortHigh int
10621064
}
10631065

10641066
func (geneve *Geneve) Attrs() *LinkAttrs {

link_linux.go

+20
Original file line numberDiff line numberDiff line change
@@ -3100,6 +3100,10 @@ func linkFlags(rawFlags uint32) net.Flags {
31003100
return f
31013101
}
31023102

3103+
type genevePortRange struct {
3104+
Lo, Hi uint16
3105+
}
3106+
31033107
func addGeneveAttrs(geneve *Geneve, linkInfo *nl.RtAttr) {
31043108
data := linkInfo.AddRtAttr(nl.IFLA_INFO_DATA, nil)
31053109

@@ -3136,6 +3140,15 @@ func addGeneveAttrs(geneve *Geneve, linkInfo *nl.RtAttr) {
31363140
data.AddRtAttr(nl.IFLA_GENEVE_TOS, nl.Uint8Attr(geneve.Tos))
31373141
}
31383142

3143+
if geneve.PortLow > 0 || geneve.PortHigh > 0 {
3144+
pr := genevePortRange{uint16(geneve.PortLow), uint16(geneve.PortHigh)}
3145+
3146+
buf := new(bytes.Buffer)
3147+
binary.Write(buf, binary.BigEndian, &pr)
3148+
3149+
data.AddRtAttr(nl.IFLA_GENEVE_PORT_RANGE, buf.Bytes())
3150+
}
3151+
31393152
data.AddRtAttr(nl.IFLA_GENEVE_DF, nl.Uint8Attr(uint8(geneve.Df)))
31403153
}
31413154

@@ -3157,6 +3170,13 @@ func parseGeneveData(link Link, data []syscall.NetlinkRouteAttr) {
31573170
geneve.FlowBased = true
31583171
case nl.IFLA_GENEVE_INNER_PROTO_INHERIT:
31593172
geneve.InnerProtoInherit = true
3173+
case nl.IFLA_GENEVE_PORT_RANGE:
3174+
buf := bytes.NewBuffer(datum.Value[0:4])
3175+
var pr genevePortRange
3176+
if binary.Read(buf, binary.BigEndian, &pr) == nil {
3177+
geneve.PortLow = int(pr.Lo)
3178+
geneve.PortHigh = int(pr.Hi)
3179+
}
31603180
}
31613181
}
31623182
}

link_test.go

+9
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,15 @@ func compareGeneve(t *testing.T, expected, actual *Geneve) {
438438
t.Fatal("Geneve.InnerProtoInherit doesn't match")
439439
}
440440

441+
if expected.PortLow > 0 || expected.PortHigh > 0 {
442+
if actual.PortLow != expected.PortLow {
443+
t.Fatal("Geneve.PortLow doesn't match")
444+
}
445+
if actual.PortHigh != expected.PortHigh {
446+
t.Fatal("Geneve.PortHigh doesn't match")
447+
}
448+
}
449+
441450
// TODO: we should implement the rest of the geneve methods
442451
}
443452

nl/link_linux.go

+1
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ const (
234234
IFLA_GENEVE_TTL_INHERIT
235235
IFLA_GENEVE_DF
236236
IFLA_GENEVE_INNER_PROTO_INHERIT
237+
IFLA_GENEVE_PORT_RANGE
237238
IFLA_GENEVE_MAX = IFLA_GENEVE_INNER_PROTO_INHERIT
238239
)
239240

0 commit comments

Comments
 (0)