-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit b4915bd
Showing
6 changed files
with
312 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
language: go | ||
go: | ||
- 1.5 | ||
- tip | ||
before_install: | ||
- go get github.com/axw/gocov/gocov | ||
- go get github.com/mattn/goveralls | ||
- go get golang.org/x/tools/cmd/cover | ||
before_script: | ||
- go get -d ./... | ||
script: | ||
- go test -v ./... | ||
- if ! $HOME/gopath/bin/goveralls -service=travis-ci -repotoken $COVERALLS_TOKEN; then echo "Coveralls not available."; fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
MIT License | ||
=========== | ||
|
||
Copyright (C) 2016 Matt Layher | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
vxlan [![Build Status](https://travis-ci.org/mdlayher/vxlan.svg?branch=master)](https://travis-ci.org/mdlayher/vxlan) [![Coverage Status](https://coveralls.io/repos/mdlayher/vxlan/badge.svg?branch=master)](https://coveralls.io/r/mdlayher/vxlan?branch=master) [![GoDoc](http://godoc.org/github.com/mdlayher/vxlan?status.svg)](http://godoc.org/github.com/mdlayher/vxlan) [![Report Card](http://goreportcard.com/badge/mdlayher/vxlan)](http://goreportcard.com/report/mdlayher/vxlan) | ||
===== | ||
|
||
Package `vxlan` implements marshaling and unmarshaling of Virtual eXtensible | ||
Local Area Network (VXLAN) frames, as described in RFC 7348. MIT Licensed. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// +build gofuzz | ||
|
||
package vxlan | ||
|
||
func Fuzz(data []byte) int { | ||
f := new(Frame) | ||
if err := f.UnmarshalBinary(data); err != nil { | ||
return 0 | ||
} | ||
|
||
if _, err := f.MarshalBinary(); err != nil { | ||
panic(err) | ||
} | ||
|
||
return 1 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
// Package vxlan implements marshaling and unmarshaling of Virtual eXtensible | ||
// Local Area Network (VXLAN) frames, as described in RFC 7348. | ||
package vxlan | ||
|
||
import ( | ||
"encoding/binary" | ||
"errors" | ||
"io" | ||
|
||
"github.com/mdlayher/ethernet" | ||
) | ||
|
||
const ( | ||
// MaxVNI is the maximum possible value for a VNI: the maximum value | ||
// of a 24-bit integer. | ||
MaxVNI = (1 << 24) - 1 | ||
) | ||
|
||
var ( | ||
// ErrInvalidFrame is returned when the reserved I bit is not set in | ||
// a byte slice when Frame.UnmarshalBinary is called. | ||
ErrInvalidFrame = errors.New("invalid Frame") | ||
|
||
// ErrInvalidVNI is returned when a Frame contains an invalid VNI, | ||
// and Frame.MarshalBinary is called. | ||
ErrInvalidVNI = errors.New("invalid VNI") | ||
) | ||
|
||
// A VNI is a 24-bit Virtual Network Identifier. It is used to designate a | ||
// VXLAN overlay network. Use its Valid method to determine if a VNI contains | ||
// a valid value. | ||
type VNI uint32 | ||
|
||
// Valid determines if a VNI is a valid, 24-bit integer. | ||
func (v VNI) Valid() bool { | ||
return v <= MaxVNI | ||
} | ||
|
||
// A Frame is an Virtual eXtensible Local Area Network (VXLAN) frame, as | ||
// described in RFC 7348, Section 5. | ||
// | ||
// It contains a VNI used to designate an overlay network, and an embedded | ||
// Ethernet frame which transports an arbitrary payload within the overlay | ||
// network. | ||
type Frame struct { | ||
VNI VNI | ||
Ethernet *ethernet.Frame | ||
} | ||
|
||
// MarshalBinary allocates a byte slice and marshals a Frame into binary form. | ||
// | ||
// If a VNI value is invalid, ErrInvalidVNI will be returned. | ||
func (f *Frame) MarshalBinary() ([]byte, error) { | ||
if !f.VNI.Valid() { | ||
return nil, ErrInvalidVNI | ||
} | ||
|
||
efb, err := f.Ethernet.MarshalFCS() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
b := make([]byte, 8) | ||
|
||
// I flag is always set to 1, all others are reserved | ||
b[0] |= 1 << 3 | ||
|
||
binary.BigEndian.PutUint32(b[3:], uint32(f.VNI)) | ||
|
||
// Ethernet frame bytes accompany VXLAN frame | ||
return append(b, efb...), nil | ||
} | ||
|
||
// UnmarshalBinary allocates a byte slice and marshals a Frame into binary form. | ||
// | ||
// If a VNI value is invalid, ErrInvalidVNI will be returned. | ||
func (f *Frame) UnmarshalBinary(b []byte) error { | ||
// Need at least VXLAN frame and empty Ethernet frame | ||
if len(b) < 18 { | ||
return io.ErrUnexpectedEOF | ||
} | ||
|
||
// I flag must be set to 1. | ||
if (b[0] >> 3) != 1 { | ||
return ErrInvalidFrame | ||
} | ||
|
||
f.VNI = VNI(binary.BigEndian.Uint32(b[3:])) | ||
|
||
ef := new(ethernet.Frame) | ||
if err := ef.UnmarshalFCS(b[8:]); err != nil { | ||
return err | ||
} | ||
f.Ethernet = ef | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
package vxlan | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"testing" | ||
|
||
"github.com/mdlayher/ethernet" | ||
) | ||
|
||
func TestFrameMarshalBinary(t *testing.T) { | ||
var tests = []struct { | ||
desc string | ||
f *Frame | ||
b []byte | ||
err error | ||
}{ | ||
{ | ||
desc: "Frame with VNI value too large", | ||
f: &Frame{ | ||
VNI: MaxVNI + 1, | ||
}, | ||
err: ErrInvalidVNI, | ||
}, | ||
{ | ||
desc: "Frame with Ethernet frame with VLAN too large", | ||
f: &Frame{ | ||
Ethernet: ðernet.Frame{ | ||
VLAN: []*ethernet.VLAN{{ | ||
ID: ethernet.VLANMax + 1, | ||
}}, | ||
}, | ||
}, | ||
err: ethernet.ErrInvalidVLAN, | ||
}, | ||
{ | ||
desc: "Frame with VNI 1", | ||
f: &Frame{ | ||
VNI: 1, | ||
Ethernet: ðernet.Frame{}, | ||
}, | ||
b: append([]byte{ | ||
0x08, 0x00, 0x00, 0x00, | ||
0x00, 0x00, 0x01, 0x00, | ||
}, ethernetFrame(t)...), | ||
}, | ||
{ | ||
desc: "Frame with VNI Max", | ||
f: &Frame{ | ||
VNI: MaxVNI, | ||
Ethernet: ðernet.Frame{}, | ||
}, | ||
b: append([]byte{ | ||
0x08, 0x00, 0x00, 0x00, | ||
0xff, 0xff, 0xff, 0x00, | ||
}, ethernetFrame(t)...), | ||
}, | ||
} | ||
|
||
for i, tt := range tests { | ||
t.Logf("[%02d] test %q", i, tt.desc) | ||
|
||
b, err := tt.f.MarshalBinary() | ||
if err != nil { | ||
if want, got := tt.err, err; want != got { | ||
t.Fatalf("unexpected error: %v != %v", | ||
want, got) | ||
} | ||
|
||
continue | ||
} | ||
|
||
if want, got := tt.b, b; !bytes.Equal(want, got) { | ||
t.Fatalf("unexpected Frame bytes:\n- want: %v\n- got: %v", | ||
want, got) | ||
} | ||
} | ||
} | ||
|
||
func TestFrameUnmarshalBinary(t *testing.T) { | ||
var tests = []struct { | ||
desc string | ||
b []byte | ||
f *Frame | ||
err error | ||
}{ | ||
{ | ||
desc: "Frame too short", | ||
b: []byte{0x00}, | ||
err: io.ErrUnexpectedEOF, | ||
}, | ||
{ | ||
desc: "Frame with I flag not set", | ||
b: append([]byte{ | ||
0x00, 0x00, 0x00, 0x00, | ||
0x00, 0x00, 0x01, 0x00, | ||
}, ethernetFrame(t)...), | ||
err: ErrInvalidFrame, | ||
}, | ||
{ | ||
desc: "Frame with invalid Ethernet frame check sequence", | ||
b: func() []byte { | ||
b := append([]byte{ | ||
0x08, 0x00, 0x00, 0x00, | ||
0x00, 0x00, 0x01, 0x00, | ||
}, ethernetFrame(t)...) | ||
|
||
// Break FCS | ||
b[len(b)-1] = 0x00 | ||
return b | ||
}(), | ||
err: ethernet.ErrInvalidFCS, | ||
}, | ||
{ | ||
desc: "Frame with VNI 1", | ||
b: append([]byte{ | ||
0x08, 0x00, 0x00, 0x00, | ||
0x00, 0x00, 0x01, 0x00, | ||
}, ethernetFrame(t)...), | ||
f: &Frame{ | ||
VNI: 1, | ||
Ethernet: ðernet.Frame{}, | ||
}, | ||
}, | ||
{ | ||
desc: "Frame with VNI Max", | ||
b: append([]byte{ | ||
0x08, 0x00, 0x00, 0x00, | ||
0xff, 0xff, 0xff, 0x00, | ||
}, ethernetFrame(t)...), | ||
f: &Frame{ | ||
VNI: MaxVNI, | ||
Ethernet: ðernet.Frame{}, | ||
}, | ||
}, | ||
} | ||
|
||
for i, tt := range tests { | ||
t.Logf("[%02d] test %q", i, tt.desc) | ||
|
||
f := new(Frame) | ||
if err := f.UnmarshalBinary(tt.b); err != nil { | ||
if want, got := tt.err, err; want != got { | ||
t.Fatalf("unexpected error: %v != %v", | ||
want, got) | ||
} | ||
|
||
continue | ||
} | ||
|
||
fb, err := f.MarshalBinary() | ||
if err != nil { | ||
t.Fatalf("unexpected error: %v", err) | ||
} | ||
|
||
if want, got := tt.b, fb; !bytes.Equal(want, got) { | ||
t.Fatalf("unexpected Frame bytes:\n- want: %v\n- got: %v", | ||
want, got) | ||
} | ||
} | ||
} | ||
|
||
func ethernetFrame(t *testing.T) []byte { | ||
f := new(ethernet.Frame) | ||
fb, err := f.MarshalFCS() | ||
if err != nil { | ||
t.Fatalf("failed to marshal Ethernet frame: %v", err) | ||
} | ||
|
||
return fb | ||
} |