-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BGP Extended Communities (RFC 4360) are a fundamental function for features like MP-BGP EVPN, VPLS or Flow-Spec. This commit adds the basic functionality for EC which is required for Flow-Spec. Signed-off-by: Tom Siewert <[email protected]>
- Loading branch information
1 parent
c567987
commit 8305ad6
Showing
10 changed files
with
506 additions
and
92 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
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
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
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
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,54 @@ | ||
package types | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/bio-routing/bio-rd/route/api" | ||
) | ||
|
||
// TODO: Add EC type mappings | ||
|
||
type ExtendedCommunities []ExtendedCommunity | ||
|
||
// ExtendedCommunity represents an extended community as | ||
// described in RFC 4360. | ||
type ExtendedCommunity struct { | ||
Type uint8 | ||
SubType uint8 | ||
Value []byte | ||
} | ||
|
||
// ToProto converts ExtendedCommunity to proto ExtendedCommunity | ||
func (ec *ExtendedCommunity) ToProto() *api.ExtendedCommunity { | ||
return &api.ExtendedCommunity{ | ||
Type: uint32(ec.Type), | ||
Subtype: uint32(ec.SubType), | ||
Value: ec.Value, | ||
} | ||
} | ||
|
||
// ExtendedCommunityFromProtoExtendedCommunity converts a proto ExtendedCommunity to ExtendedCommunity | ||
func ExtendedCommunityFromProtoExtendedCommunity(aec *api.ExtendedCommunity) ExtendedCommunity { | ||
return ExtendedCommunity{ | ||
Type: uint8(aec.Type), | ||
SubType: uint8(aec.Subtype), | ||
Value: aec.Value, | ||
} | ||
} | ||
|
||
// String transitions an extended community its (almost) human-readable representation | ||
func (ec *ExtendedCommunity) String() string { | ||
if ec == nil { | ||
return "" | ||
} | ||
|
||
var subType uint8 | ||
if ec.SubType == 0 { | ||
subType = 0xff | ||
} else { | ||
subType = ec.SubType | ||
} | ||
|
||
// TODO: translate types and value | ||
return fmt.Sprintf("Type: %d Subtype: %d Value: %s", ec.Type, subType, ec.Value) | ||
} |
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,54 @@ | ||
package types | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/bio-routing/bio-rd/route/api" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestExtendedCommunityFromProtoExtendedCommunity(t *testing.T) { | ||
input := &api.ExtendedCommunity{ | ||
Type: 128, | ||
Subtype: 6, // Flow spec traffic-rate | ||
Value: []byte{ | ||
0x00, 0x00, // 2-Octet AS = 0 | ||
0x00, 0x00, 0x00, 0x00, // Rate shaper = 0 | ||
}, | ||
} | ||
|
||
expected := ExtendedCommunity{ | ||
Type: 128, | ||
SubType: 6, | ||
Value: []byte{ | ||
0x00, 0x00, // 2-Octet AS = 0 | ||
0x00, 0x00, 0x00, 0x00, // Rate shaper = 0 | ||
}, | ||
} | ||
|
||
result := ExtendedCommunityFromProtoExtendedCommunity(input) | ||
assert.Equal(t, expected, result) | ||
} | ||
|
||
func TestExtendedCommunityToProto(t *testing.T) { | ||
input := ExtendedCommunity{ | ||
Type: 128, | ||
SubType: 6, | ||
Value: []byte{ | ||
0x00, 0x00, | ||
0x00, 0x00, 0x00, 0x00, | ||
}, | ||
} | ||
|
||
expected := &api.ExtendedCommunity{ | ||
Type: 128, | ||
Subtype: 6, | ||
Value: []byte{ | ||
0x00, 0x00, | ||
0x00, 0x00, 0x00, 0x00, | ||
}, | ||
} | ||
|
||
result := input.ToProto() | ||
assert.Equal(t, expected, result) | ||
} |
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
Oops, something went wrong.