Add unit tests
This commit is contained in:
3704
vendor/github.com/osrg/gobgp/pkg/packet/bgp/bgp.go
generated
vendored
3704
vendor/github.com/osrg/gobgp/pkg/packet/bgp/bgp.go
generated
vendored
File diff suppressed because it is too large
Load Diff
49
vendor/github.com/osrg/gobgp/pkg/packet/bgp/bgpattrtype_string.go
generated
vendored
49
vendor/github.com/osrg/gobgp/pkg/packet/bgp/bgpattrtype_string.go
generated
vendored
@@ -1,17 +1,50 @@
|
||||
// generated by stringer -type BGPAttrType bgp.go; DO NOT EDIT
|
||||
// Code generated by "stringer -type=BGPAttrType"; DO NOT EDIT.
|
||||
|
||||
package bgp
|
||||
|
||||
import "fmt"
|
||||
import "strconv"
|
||||
|
||||
func _() {
|
||||
// An "invalid array index" compiler error signifies that the constant values have changed.
|
||||
// Re-run the stringer command to generate them again.
|
||||
var x [1]struct{}
|
||||
_ = x[BGP_ATTR_TYPE_ORIGIN-1]
|
||||
_ = x[BGP_ATTR_TYPE_AS_PATH-2]
|
||||
_ = x[BGP_ATTR_TYPE_NEXT_HOP-3]
|
||||
_ = x[BGP_ATTR_TYPE_MULTI_EXIT_DISC-4]
|
||||
_ = x[BGP_ATTR_TYPE_LOCAL_PREF-5]
|
||||
_ = x[BGP_ATTR_TYPE_ATOMIC_AGGREGATE-6]
|
||||
_ = x[BGP_ATTR_TYPE_AGGREGATOR-7]
|
||||
_ = x[BGP_ATTR_TYPE_COMMUNITIES-8]
|
||||
_ = x[BGP_ATTR_TYPE_ORIGINATOR_ID-9]
|
||||
_ = x[BGP_ATTR_TYPE_CLUSTER_LIST-10]
|
||||
_ = x[BGP_ATTR_TYPE_MP_REACH_NLRI-14]
|
||||
_ = x[BGP_ATTR_TYPE_MP_UNREACH_NLRI-15]
|
||||
_ = x[BGP_ATTR_TYPE_EXTENDED_COMMUNITIES-16]
|
||||
_ = x[BGP_ATTR_TYPE_AS4_PATH-17]
|
||||
_ = x[BGP_ATTR_TYPE_AS4_AGGREGATOR-18]
|
||||
_ = x[BGP_ATTR_TYPE_PMSI_TUNNEL-22]
|
||||
_ = x[BGP_ATTR_TYPE_TUNNEL_ENCAP-23]
|
||||
_ = x[BGP_ATTR_TYPE_IP6_EXTENDED_COMMUNITIES-25]
|
||||
_ = x[BGP_ATTR_TYPE_AIGP-26]
|
||||
_ = x[BGP_ATTR_TYPE_LS-29]
|
||||
_ = x[BGP_ATTR_TYPE_LARGE_COMMUNITY-32]
|
||||
}
|
||||
|
||||
const (
|
||||
_BGPAttrType_name_0 = "BGP_ATTR_TYPE_ORIGINBGP_ATTR_TYPE_AS_PATHBGP_ATTR_TYPE_NEXT_HOPBGP_ATTR_TYPE_MULTI_EXIT_DISCBGP_ATTR_TYPE_LOCAL_PREFBGP_ATTR_TYPE_ATOMIC_AGGREGATEBGP_ATTR_TYPE_AGGREGATORBGP_ATTR_TYPE_COMMUNITIESBGP_ATTR_TYPE_ORIGINATOR_IDBGP_ATTR_TYPE_CLUSTER_LIST"
|
||||
_BGPAttrType_name_1 = "BGP_ATTR_TYPE_MP_REACH_NLRIBGP_ATTR_TYPE_MP_UNREACH_NLRIBGP_ATTR_TYPE_EXTENDED_COMMUNITIESBGP_ATTR_TYPE_AS4_PATHBGP_ATTR_TYPE_AS4_AGGREGATOR"
|
||||
_BGPAttrType_name_2 = "BGP_ATTR_TYPE_PMSI_TUNNELBGP_ATTR_TYPE_TUNNEL_ENCAP"
|
||||
_BGPAttrType_name_3 = "BGP_ATTR_TYPE_IP6_EXTENDED_COMMUNITIESBGP_ATTR_TYPE_AIGP"
|
||||
_BGPAttrType_name_4 = "BGP_ATTR_TYPE_LS"
|
||||
_BGPAttrType_name_5 = "BGP_ATTR_TYPE_LARGE_COMMUNITY"
|
||||
)
|
||||
|
||||
var (
|
||||
_BGPAttrType_index_0 = [...]uint8{0, 20, 41, 63, 92, 116, 146, 170, 195, 222, 248}
|
||||
_BGPAttrType_index_1 = [...]uint8{0, 27, 56, 90, 112, 140}
|
||||
_BGPAttrType_index_2 = [...]uint8{0, 25, 51}
|
||||
_BGPAttrType_index_3 = [...]uint8{0, 38, 56}
|
||||
)
|
||||
|
||||
func (i BGPAttrType) String() string {
|
||||
@@ -22,7 +55,17 @@ func (i BGPAttrType) String() string {
|
||||
case 14 <= i && i <= 18:
|
||||
i -= 14
|
||||
return _BGPAttrType_name_1[_BGPAttrType_index_1[i]:_BGPAttrType_index_1[i+1]]
|
||||
case 22 <= i && i <= 23:
|
||||
i -= 22
|
||||
return _BGPAttrType_name_2[_BGPAttrType_index_2[i]:_BGPAttrType_index_2[i+1]]
|
||||
case 25 <= i && i <= 26:
|
||||
i -= 25
|
||||
return _BGPAttrType_name_3[_BGPAttrType_index_3[i]:_BGPAttrType_index_3[i+1]]
|
||||
case i == 29:
|
||||
return _BGPAttrType_name_4
|
||||
case i == 32:
|
||||
return _BGPAttrType_name_5
|
||||
default:
|
||||
return fmt.Sprintf("BGPAttrType(%d)", i)
|
||||
return "BGPAttrType(" + strconv.FormatInt(int64(i), 10) + ")"
|
||||
}
|
||||
}
|
||||
|
||||
9
vendor/github.com/osrg/gobgp/pkg/packet/bgp/constant.go
generated
vendored
9
vendor/github.com/osrg/gobgp/pkg/packet/bgp/constant.go
generated
vendored
@@ -16,7 +16,7 @@
|
||||
package bgp
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@@ -24,6 +24,7 @@ const AS_TRANS = 23456
|
||||
|
||||
const BGP_PORT = 179
|
||||
|
||||
//go:generate stringer -type=FSMState
|
||||
type FSMState int
|
||||
|
||||
const (
|
||||
@@ -73,7 +74,7 @@ var ProtocolNameMap = map[Protocol]string{
|
||||
func (p Protocol) String() string {
|
||||
name, ok := ProtocolNameMap[p]
|
||||
if !ok {
|
||||
return fmt.Sprintf("%d", p)
|
||||
return strconv.Itoa(int(p))
|
||||
}
|
||||
return name
|
||||
}
|
||||
@@ -161,7 +162,7 @@ var BitmaskFlagOpValueMap = map[string]BitmaskFlagOp{
|
||||
}
|
||||
|
||||
func (f BitmaskFlagOp) String() string {
|
||||
ops := make([]string, 0)
|
||||
ops := make([]string, 0, 3)
|
||||
if f&BITMASK_FLAG_OP_AND > 0 {
|
||||
ops = append(ops, BitmaskFlagOpNameMap[BITMASK_FLAG_OP_AND])
|
||||
} else {
|
||||
@@ -323,5 +324,5 @@ func (t EthernetType) String() string {
|
||||
if name, ok := EthernetTypeNameMap[t]; ok {
|
||||
return name
|
||||
}
|
||||
return fmt.Sprintf("%d", t)
|
||||
return strconv.Itoa(int(t))
|
||||
}
|
||||
|
||||
20
vendor/github.com/osrg/gobgp/pkg/packet/bgp/esitype_string.go
generated
vendored
20
vendor/github.com/osrg/gobgp/pkg/packet/bgp/esitype_string.go
generated
vendored
@@ -1,16 +1,28 @@
|
||||
// generated by stringer -type=ESIType bgp.go validate.go; DO NOT EDIT
|
||||
// Code generated by "stringer -type=ESIType"; DO NOT EDIT.
|
||||
|
||||
package bgp
|
||||
|
||||
import "fmt"
|
||||
import "strconv"
|
||||
|
||||
func _() {
|
||||
// An "invalid array index" compiler error signifies that the constant values have changed.
|
||||
// Re-run the stringer command to generate them again.
|
||||
var x [1]struct{}
|
||||
_ = x[ESI_ARBITRARY-0]
|
||||
_ = x[ESI_LACP-1]
|
||||
_ = x[ESI_MSTP-2]
|
||||
_ = x[ESI_MAC-3]
|
||||
_ = x[ESI_ROUTERID-4]
|
||||
_ = x[ESI_AS-5]
|
||||
}
|
||||
|
||||
const _ESIType_name = "ESI_ARBITRARYESI_LACPESI_MSTPESI_MACESI_ROUTERIDESI_AS"
|
||||
|
||||
var _ESIType_index = [...]uint8{0, 13, 21, 29, 36, 48, 54}
|
||||
|
||||
func (i ESIType) String() string {
|
||||
if i+1 >= ESIType(len(_ESIType_index)) {
|
||||
return fmt.Sprintf("ESIType(%d)", i)
|
||||
if i >= ESIType(len(_ESIType_index)-1) {
|
||||
return "ESIType(" + strconv.FormatInt(int64(i), 10) + ")"
|
||||
}
|
||||
return _ESIType_name[_ESIType_index[i]:_ESIType_index[i+1]]
|
||||
}
|
||||
|
||||
18
vendor/github.com/osrg/gobgp/pkg/packet/bgp/fsmstate_string.go
generated
vendored
18
vendor/github.com/osrg/gobgp/pkg/packet/bgp/fsmstate_string.go
generated
vendored
@@ -1,8 +1,20 @@
|
||||
// generated by stringer -type=FSMState -output=fsmstate_string.go bgp.go validate.go mrt.go rtr.go constant.go bmp.go esitype_string.go bgpattrtype_string.go; DO NOT EDIT
|
||||
// Code generated by "stringer -type=FSMState"; DO NOT EDIT.
|
||||
|
||||
package bgp
|
||||
|
||||
import "fmt"
|
||||
import "strconv"
|
||||
|
||||
func _() {
|
||||
// An "invalid array index" compiler error signifies that the constant values have changed.
|
||||
// Re-run the stringer command to generate them again.
|
||||
var x [1]struct{}
|
||||
_ = x[BGP_FSM_IDLE-0]
|
||||
_ = x[BGP_FSM_CONNECT-1]
|
||||
_ = x[BGP_FSM_ACTIVE-2]
|
||||
_ = x[BGP_FSM_OPENSENT-3]
|
||||
_ = x[BGP_FSM_OPENCONFIRM-4]
|
||||
_ = x[BGP_FSM_ESTABLISHED-5]
|
||||
}
|
||||
|
||||
const _FSMState_name = "BGP_FSM_IDLEBGP_FSM_CONNECTBGP_FSM_ACTIVEBGP_FSM_OPENSENTBGP_FSM_OPENCONFIRMBGP_FSM_ESTABLISHED"
|
||||
|
||||
@@ -10,7 +22,7 @@ var _FSMState_index = [...]uint8{0, 12, 27, 41, 57, 76, 95}
|
||||
|
||||
func (i FSMState) String() string {
|
||||
if i < 0 || i >= FSMState(len(_FSMState_index)-1) {
|
||||
return fmt.Sprintf("FSMState(%d)", i)
|
||||
return "FSMState(" + strconv.FormatInt(int64(i), 10) + ")"
|
||||
}
|
||||
return _FSMState_name[_FSMState_index[i]:_FSMState_index[i+1]]
|
||||
}
|
||||
|
||||
677
vendor/github.com/osrg/gobgp/pkg/packet/bgp/prefix_sid.go
generated
vendored
Normal file
677
vendor/github.com/osrg/gobgp/pkg/packet/bgp/prefix_sid.go
generated
vendored
Normal file
@@ -0,0 +1,677 @@
|
||||
package bgp
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net"
|
||||
|
||||
"github.com/golang/protobuf/ptypes"
|
||||
api "github.com/osrg/gobgp/api"
|
||||
)
|
||||
|
||||
const (
|
||||
prefixSIDtlvHdrLen = 4
|
||||
)
|
||||
|
||||
type TLVType uint8
|
||||
|
||||
type TLV struct {
|
||||
Type TLVType
|
||||
Length uint16
|
||||
}
|
||||
|
||||
func (s *TLV) Len() int {
|
||||
return int(s.Length) + tlvHdrLen - 1 // Extra reserved byte in the header
|
||||
}
|
||||
|
||||
func (s *TLV) Serialize(value []byte) ([]byte, error) {
|
||||
if len(value) != int(s.Length)-1 {
|
||||
return nil, malformedAttrListErr("serialization failed: Prefix SID TLV malformed")
|
||||
}
|
||||
buf := make([]byte, prefixSIDtlvHdrLen+len(value))
|
||||
p := 0
|
||||
buf[p] = byte(s.Type)
|
||||
p++
|
||||
binary.BigEndian.PutUint16(buf[p:p+2], uint16(s.Length))
|
||||
p += 2
|
||||
// Reserved byte
|
||||
p++
|
||||
copy(buf[p:], value)
|
||||
|
||||
return buf, nil
|
||||
}
|
||||
|
||||
func (s *TLV) DecodeFromBytes(data []byte) ([]byte, error) {
|
||||
if len(data) < prefixSIDtlvHdrLen {
|
||||
return nil, malformedAttrListErr("decoding failed: Prefix SID TLV malformed")
|
||||
}
|
||||
p := 0
|
||||
s.Type = TLVType(data[p])
|
||||
p++
|
||||
s.Length = binary.BigEndian.Uint16(data[p : p+2])
|
||||
|
||||
if len(data) < s.Len() {
|
||||
return nil, malformedAttrListErr("decoding failed: Prefix SID TLV malformed")
|
||||
}
|
||||
|
||||
return data[prefixSIDtlvHdrLen:s.Len()], nil
|
||||
}
|
||||
|
||||
// PrefixSIDTLVInterface defines standard set of methods to handle Prefix SID attribute's TLVs
|
||||
type PrefixSIDTLVInterface interface {
|
||||
Len() int
|
||||
DecodeFromBytes([]byte) error
|
||||
Serialize() ([]byte, error)
|
||||
String() string
|
||||
MarshalJSON() ([]byte, error)
|
||||
}
|
||||
|
||||
type PrefixSIDAttribute struct {
|
||||
TLVs []PrefixSIDTLVInterface
|
||||
}
|
||||
|
||||
type PathAttributePrefixSID struct {
|
||||
PathAttribute
|
||||
TLVs []PrefixSIDTLVInterface
|
||||
}
|
||||
|
||||
func (p *PathAttributePrefixSID) DecodeFromBytes(data []byte, options ...*MarshallingOption) error {
|
||||
tlvs, err := p.PathAttribute.DecodeFromBytes(data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for len(tlvs) >= prefixSIDtlvHdrLen {
|
||||
t := &TLV{}
|
||||
_, err := t.DecodeFromBytes(tlvs)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var tlv PrefixSIDTLVInterface
|
||||
switch t.Type {
|
||||
case 5:
|
||||
tlv = &SRv6L3ServiceAttribute{
|
||||
SubTLVs: make([]PrefixSIDTLVInterface, 0),
|
||||
}
|
||||
default:
|
||||
tlvs = tlvs[t.Len():]
|
||||
continue
|
||||
}
|
||||
|
||||
if err := tlv.DecodeFromBytes(tlvs); err != nil {
|
||||
return err
|
||||
}
|
||||
tlvs = tlvs[t.Len():]
|
||||
p.TLVs = append(p.TLVs, tlv)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *PathAttributePrefixSID) Serialize(options ...*MarshallingOption) ([]byte, error) {
|
||||
buf := make([]byte, 0)
|
||||
for _, tlv := range p.TLVs {
|
||||
s, err := tlv.Serialize()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
buf = append(buf, s...)
|
||||
}
|
||||
|
||||
return p.PathAttribute.Serialize(buf)
|
||||
}
|
||||
|
||||
func (p *PathAttributePrefixSID) String() string {
|
||||
var buf bytes.Buffer
|
||||
|
||||
for _, tlv := range p.TLVs {
|
||||
buf.WriteString(fmt.Sprintf("%s ", tlv.String()))
|
||||
}
|
||||
|
||||
return fmt.Sprintf("{Prefix SID attributes: %s}", buf.String())
|
||||
}
|
||||
|
||||
func (p *PathAttributePrefixSID) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(struct {
|
||||
Type BGPAttrType `json:"type"`
|
||||
Flags BGPAttrFlag `json:"flags"`
|
||||
PrefixSIDAttribute
|
||||
}{
|
||||
p.GetType(),
|
||||
p.GetFlags(),
|
||||
*p.Extract(),
|
||||
})
|
||||
}
|
||||
|
||||
func (p *PathAttributePrefixSID) Extract() *PrefixSIDAttribute {
|
||||
psid := &PrefixSIDAttribute{
|
||||
TLVs: make([]PrefixSIDTLVInterface, 0),
|
||||
}
|
||||
psid.TLVs = append(psid.TLVs, p.TLVs...)
|
||||
|
||||
return psid
|
||||
}
|
||||
|
||||
// SRv6L3Service defines the structure of SRv6 L3 Service object
|
||||
type SRv6L3Service struct {
|
||||
SubTLVs []PrefixSIDTLVInterface
|
||||
}
|
||||
|
||||
// SRv6L3ServiceAttribute defines the structure of SRv6 L3 Service attribute
|
||||
type SRv6L3ServiceAttribute struct {
|
||||
TLV
|
||||
SubTLVs []PrefixSIDTLVInterface
|
||||
}
|
||||
|
||||
func (s *SRv6L3ServiceAttribute) Len() int {
|
||||
return int(s.Length) + prefixSIDtlvHdrLen
|
||||
}
|
||||
|
||||
func (s *SRv6L3ServiceAttribute) Serialize() ([]byte, error) {
|
||||
buf := make([]byte, 0)
|
||||
for _, tlv := range s.SubTLVs {
|
||||
s, err := tlv.Serialize()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
buf = append(buf, s...)
|
||||
}
|
||||
return s.TLV.Serialize(buf)
|
||||
}
|
||||
|
||||
func (s *SRv6L3ServiceAttribute) DecodeFromBytes(data []byte) error {
|
||||
stlvs, err := s.TLV.DecodeFromBytes(data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for len(stlvs) >= subTLVHdrLen {
|
||||
t := &SubTLV{}
|
||||
_, err := t.DecodeFromBytes(stlvs)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var stlv PrefixSIDTLVInterface
|
||||
switch t.Type {
|
||||
case 1:
|
||||
stlv = &SRv6InformationSubTLV{
|
||||
SubSubTLVs: make([]PrefixSIDTLVInterface, 0),
|
||||
}
|
||||
default:
|
||||
data = data[t.Len():]
|
||||
continue
|
||||
}
|
||||
|
||||
if err := stlv.DecodeFromBytes(stlvs); err != nil {
|
||||
return err
|
||||
}
|
||||
stlvs = stlvs[t.Len():]
|
||||
s.SubTLVs = append(s.SubTLVs, stlv)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *SRv6L3ServiceAttribute) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(struct {
|
||||
Type TLVType `json:"type"`
|
||||
SRv6L3Service
|
||||
}{
|
||||
s.Type,
|
||||
*s.Extract(),
|
||||
})
|
||||
}
|
||||
|
||||
func (s *SRv6L3ServiceAttribute) String() string {
|
||||
var buf bytes.Buffer
|
||||
|
||||
for _, tlv := range s.SubTLVs {
|
||||
buf.WriteString(fmt.Sprintf("%s ", tlv.String()))
|
||||
}
|
||||
|
||||
return fmt.Sprintf("{SRv6 L3 Service Attribute: %s}", buf.String())
|
||||
}
|
||||
|
||||
func (s *SRv6L3ServiceAttribute) Extract() *SRv6L3Service {
|
||||
l3 := &SRv6L3Service{
|
||||
SubTLVs: make([]PrefixSIDTLVInterface, 0),
|
||||
}
|
||||
|
||||
l3.SubTLVs = append(l3.SubTLVs, s.SubTLVs...)
|
||||
|
||||
return l3
|
||||
}
|
||||
|
||||
const (
|
||||
subTLVHdrLen = 3
|
||||
)
|
||||
|
||||
type SubTLVType uint8
|
||||
|
||||
type SubTLV struct {
|
||||
Type SubTLVType
|
||||
Length uint16
|
||||
}
|
||||
|
||||
func (s *SubTLV) Len() int {
|
||||
return int(s.Length) + subTLVHdrLen
|
||||
}
|
||||
|
||||
func (s *SubTLV) Serialize(value []byte) ([]byte, error) {
|
||||
if len(value) != int(s.Length) {
|
||||
return nil, malformedAttrListErr("serialization failed: Prefix SID TLV malformed")
|
||||
}
|
||||
// Extra byte is reserved
|
||||
buf := make([]byte, subTLVHdrLen+len(value))
|
||||
buf[0] = byte(s.Type)
|
||||
binary.BigEndian.PutUint16(buf[1:4], uint16(s.Length))
|
||||
// 4th reserved byte
|
||||
copy(buf[4:], value)
|
||||
|
||||
return buf, nil
|
||||
}
|
||||
|
||||
func (s *SubTLV) DecodeFromBytes(data []byte) ([]byte, error) {
|
||||
if len(data) < subTLVHdrLen {
|
||||
return nil, malformedAttrListErr("decoding failed: Prefix SID TLV malformed")
|
||||
}
|
||||
s.Type = SubTLVType(data[0])
|
||||
s.Length = binary.BigEndian.Uint16(data[1:3])
|
||||
|
||||
if len(data) < s.Len() {
|
||||
return nil, malformedAttrListErr("decoding failed: Prefix SID TLV malformed")
|
||||
}
|
||||
|
||||
return data[subTLVHdrLen:s.Len()], nil
|
||||
}
|
||||
|
||||
type SRv6InformationSTLV struct {
|
||||
SID []byte `json:"sid"`
|
||||
Flags uint8 `json:"flags"`
|
||||
EndpointBehavior uint16 `json:"endpoint_behavior"`
|
||||
SubSubTLVs []PrefixSIDTLVInterface `json:"sub_sub_tlvs,omitempty"`
|
||||
}
|
||||
|
||||
// SRv6InformationSubTLV defines a structure of SRv6 Information Sub TLV (type 1) object
|
||||
// https://tools.ietf.org/html/draft-dawra-bess-srv6-services-02#section-2.1.1
|
||||
type SRv6InformationSubTLV struct {
|
||||
SubTLV
|
||||
SID []byte
|
||||
Flags uint8
|
||||
EndpointBehavior uint16
|
||||
SubSubTLVs []PrefixSIDTLVInterface
|
||||
}
|
||||
|
||||
func (s *SRv6InformationSubTLV) Len() int {
|
||||
return int(s.Length) + subTLVHdrLen
|
||||
}
|
||||
|
||||
func (s *SRv6InformationSubTLV) Serialize() ([]byte, error) {
|
||||
buf := make([]byte, s.Length)
|
||||
p := 0
|
||||
copy(buf[p:], s.SID)
|
||||
p += len(s.SID)
|
||||
buf[p] = byte(s.Flags)
|
||||
p++
|
||||
binary.BigEndian.PutUint16(buf[p:p+2], uint16(s.EndpointBehavior))
|
||||
p += 2
|
||||
// Reserved byte
|
||||
buf[p] = 0x0
|
||||
p++
|
||||
for _, sstlv := range s.SubSubTLVs {
|
||||
sbuf, err := sstlv.Serialize()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
copy(buf[p:], sbuf)
|
||||
p += len(sbuf)
|
||||
}
|
||||
|
||||
return s.SubTLV.Serialize(buf)
|
||||
}
|
||||
|
||||
func (s *SRv6InformationSubTLV) DecodeFromBytes(data []byte) error {
|
||||
if len(data) < subTLVHdrLen {
|
||||
return malformedAttrListErr("decoding failed: Prefix SID TLV malformed")
|
||||
}
|
||||
s.Type = SubTLVType(data[0])
|
||||
s.Length = binary.BigEndian.Uint16(data[1:3])
|
||||
// 4th reserved byte
|
||||
p := 4
|
||||
s.SID = make([]byte, 16)
|
||||
copy(s.SID, data[p:p+16])
|
||||
p += 16
|
||||
s.Flags = uint8(data[p])
|
||||
p++
|
||||
s.EndpointBehavior = binary.BigEndian.Uint16(data[p : p+2])
|
||||
p += 2
|
||||
// reserved byte
|
||||
p++
|
||||
if p+3 > len(data) {
|
||||
// There is no Sub Sub TLVs detected, returning
|
||||
return nil
|
||||
}
|
||||
stlvs := data[p:]
|
||||
for len(stlvs) >= prefixSIDtlvHdrLen {
|
||||
t := &SubSubTLV{}
|
||||
_, err := t.DecodeFromBytes(stlvs)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var sstlv PrefixSIDTLVInterface
|
||||
switch t.Type {
|
||||
case 1:
|
||||
sstlv = &SRv6SIDStructureSubSubTLV{}
|
||||
default:
|
||||
stlvs = stlvs[t.Len():]
|
||||
continue
|
||||
}
|
||||
|
||||
if err := sstlv.DecodeFromBytes(stlvs); err != nil {
|
||||
return err
|
||||
}
|
||||
stlvs = stlvs[t.Len():]
|
||||
s.SubSubTLVs = append(s.SubSubTLVs, sstlv)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *SRv6InformationSubTLV) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(struct {
|
||||
Type SubTLVType `json:"type"`
|
||||
SRv6InformationSTLV
|
||||
}{
|
||||
s.Type,
|
||||
*s.Extract(),
|
||||
})
|
||||
}
|
||||
|
||||
func (s *SRv6InformationSubTLV) String() string {
|
||||
var buf bytes.Buffer
|
||||
buf.WriteString(fmt.Sprintf("SID: %s ", net.IP(s.SID).To16().String()))
|
||||
buf.WriteString(fmt.Sprintf("Flag: %d ", s.Flags))
|
||||
buf.WriteString(fmt.Sprintf("Endpoint Behavior: %d ", s.EndpointBehavior))
|
||||
for _, tlv := range s.SubSubTLVs {
|
||||
buf.WriteString(fmt.Sprintf("%s ", tlv.String()))
|
||||
}
|
||||
|
||||
return fmt.Sprintf("{SRv6 Information Sub TLV: %s}", buf.String())
|
||||
}
|
||||
|
||||
func (s *SRv6InformationSubTLV) Extract() *SRv6InformationSTLV {
|
||||
info := &SRv6InformationSTLV{
|
||||
SID: s.SID,
|
||||
Flags: s.Flags,
|
||||
EndpointBehavior: s.EndpointBehavior,
|
||||
SubSubTLVs: make([]PrefixSIDTLVInterface, 0),
|
||||
}
|
||||
|
||||
info.SubSubTLVs = append(info.SubSubTLVs, s.SubSubTLVs...)
|
||||
|
||||
return info
|
||||
}
|
||||
|
||||
const (
|
||||
subSubTLVHdrLen = 3
|
||||
)
|
||||
|
||||
type SubSubTLVType uint8
|
||||
|
||||
type SubSubTLV struct {
|
||||
Type SubSubTLVType
|
||||
Length uint16
|
||||
}
|
||||
|
||||
func (s *SubSubTLV) Len() int {
|
||||
return int(s.Length) + subSubTLVHdrLen
|
||||
}
|
||||
|
||||
func (s *SubSubTLV) Serialize(value []byte) ([]byte, error) {
|
||||
if len(value) != int(s.Length) {
|
||||
return nil, malformedAttrListErr("serialization failed: Prefix SID TLV malformed")
|
||||
}
|
||||
// Extra byte is reserved
|
||||
buf := make([]byte, subSubTLVHdrLen+len(value))
|
||||
p := 0
|
||||
buf[p] = byte(s.Type)
|
||||
p++
|
||||
binary.BigEndian.PutUint16(buf[p:p+2], uint16(s.Length))
|
||||
p += 2
|
||||
copy(buf[p:], value)
|
||||
|
||||
return buf, nil
|
||||
}
|
||||
|
||||
func (s *SubSubTLV) DecodeFromBytes(data []byte) ([]byte, error) {
|
||||
if len(data) < prefixSIDtlvHdrLen {
|
||||
return nil, malformedAttrListErr("decoding failed: Prefix SID Sub Sub TLV malformed")
|
||||
}
|
||||
s.Type = SubSubTLVType(data[0])
|
||||
s.Length = binary.BigEndian.Uint16(data[1:3])
|
||||
|
||||
if len(data) < s.Len() {
|
||||
return nil, malformedAttrListErr("decoding failed: Prefix SID Sub Sub TLV malformed")
|
||||
}
|
||||
|
||||
return data[prefixSIDtlvHdrLen:s.Len()], nil
|
||||
}
|
||||
|
||||
// SRv6SIDStructureSubSubTLV defines a structure of SRv6 SID Structure Sub Sub TLV (type 1) object
|
||||
// https://tools.ietf.org/html/draft-dawra-bess-srv6-services-02#section-2.1.2.1
|
||||
type SRv6SIDStructureSubSubTLV struct {
|
||||
SubSubTLV
|
||||
LocalBlockLength uint8
|
||||
LocatorNodeLength uint8
|
||||
FunctionLength uint8
|
||||
ArgumentLength uint8
|
||||
TranspositionLength uint8
|
||||
TranspositionOffset uint8
|
||||
}
|
||||
|
||||
func (s *SRv6SIDStructureSubSubTLV) Len() int {
|
||||
return int(s.Length) + subSubTLVHdrLen
|
||||
}
|
||||
|
||||
func (s *SRv6SIDStructureSubSubTLV) Serialize() ([]byte, error) {
|
||||
buf := make([]byte, s.Length)
|
||||
p := 0
|
||||
buf[p] = s.LocalBlockLength
|
||||
p++
|
||||
buf[p] = s.LocatorNodeLength
|
||||
p++
|
||||
buf[p] = s.FunctionLength
|
||||
p++
|
||||
buf[p] = s.ArgumentLength
|
||||
p++
|
||||
buf[p] = s.TranspositionLength
|
||||
p++
|
||||
buf[p] = s.TranspositionOffset
|
||||
|
||||
return s.SubSubTLV.Serialize(buf)
|
||||
}
|
||||
|
||||
func (s *SRv6SIDStructureSubSubTLV) DecodeFromBytes(data []byte) error {
|
||||
if len(data) < subSubTLVHdrLen {
|
||||
return malformedAttrListErr("decoding failed: Prefix SID Sub Sub TLV malformed")
|
||||
}
|
||||
s.Type = SubSubTLVType(data[0])
|
||||
s.Length = binary.BigEndian.Uint16(data[1:3])
|
||||
|
||||
s.LocalBlockLength = data[3]
|
||||
s.LocatorNodeLength = data[4]
|
||||
s.FunctionLength = data[5]
|
||||
s.ArgumentLength = data[6]
|
||||
s.TranspositionLength = data[7]
|
||||
s.TranspositionOffset = data[8]
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *SRv6SIDStructureSubSubTLV) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(struct {
|
||||
Type SubSubTLVType `json:"type"`
|
||||
LocalBlockLength uint8 `json:"local_block_length"`
|
||||
LocatorNodeLength uint8 `json:"locator_node_length"`
|
||||
FunctionLength uint8 `json:"function_length"`
|
||||
ArgumentLength uint8 `json:"argument_length"`
|
||||
TranspositionLength uint8 `json:"transposition_length"`
|
||||
TranspositionOffset uint8 `json:"transposition_offset"`
|
||||
}{
|
||||
Type: s.Type,
|
||||
LocalBlockLength: s.LocalBlockLength,
|
||||
LocatorNodeLength: s.LocatorNodeLength,
|
||||
FunctionLength: s.FunctionLength,
|
||||
ArgumentLength: s.ArgumentLength,
|
||||
TranspositionLength: s.TranspositionLength,
|
||||
TranspositionOffset: s.TranspositionOffset,
|
||||
})
|
||||
}
|
||||
|
||||
func (s *SRv6SIDStructureSubSubTLV) String() string {
|
||||
return fmt.Sprintf("{SRv6 Structure Sub Sub TLV: [ Local Block Length: %d, Locator Node Length: %d, Function Length: %d, Argument Length: %d, Transposition Length: %d, Transposition Offset: %d] }",
|
||||
s.LocalBlockLength,
|
||||
s.LocatorNodeLength,
|
||||
s.FunctionLength,
|
||||
s.ArgumentLength,
|
||||
s.TranspositionLength,
|
||||
s.TranspositionOffset,
|
||||
)
|
||||
}
|
||||
|
||||
func NewPathAttributePrefixSID(psid *api.PrefixSID) (*PathAttributePrefixSID, error) {
|
||||
t := BGP_ATTR_TYPE_PREFIX_SID
|
||||
s := &PathAttributePrefixSID{
|
||||
PathAttribute: PathAttribute{
|
||||
Flags: PathAttrFlags[t],
|
||||
Type: t,
|
||||
},
|
||||
TLVs: make([]PrefixSIDTLVInterface, 0),
|
||||
}
|
||||
for _, raw := range psid.Tlvs {
|
||||
var tlv ptypes.DynamicAny
|
||||
if err := ptypes.UnmarshalAny(raw, &tlv); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
switch v := tlv.Message.(type) {
|
||||
case *api.SRv6L3ServiceTLV:
|
||||
tlvLength, tlvs, err := UnmarshalSubTLVs(v.SubTlvs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
o := &SRv6L3ServiceAttribute{
|
||||
TLV: TLV{
|
||||
Type: TLVType(5),
|
||||
Length: tlvLength,
|
||||
},
|
||||
}
|
||||
s.PathAttribute.Length += tlvLength
|
||||
// Storing Sub TLVs in a Service TLV
|
||||
o.SubTLVs = append(o.SubTLVs, tlvs...)
|
||||
// Adding Service TLV to Path Attribute TLV slice.
|
||||
s.TLVs = append(s.TLVs, o)
|
||||
default:
|
||||
return nil, fmt.Errorf("unknown or not implemented Prefix SID type: %+v", v)
|
||||
}
|
||||
}
|
||||
// Final Path Attribute Length is 3 bytes of the header and 1 byte Reserved1
|
||||
s.PathAttribute.Length += (3 + 1)
|
||||
return s, nil
|
||||
}
|
||||
|
||||
func UnmarshalSubTLVs(stlvs map[uint32]*api.SRv6TLV) (uint16, []PrefixSIDTLVInterface, error) {
|
||||
p := make([]PrefixSIDTLVInterface, 0, len(stlvs))
|
||||
l := uint16(0)
|
||||
// v.SubTlvs is a map by sub tlv type and the value is a slice of sub tlvs of the specific type
|
||||
for t, tlv := range stlvs {
|
||||
switch t {
|
||||
case 1:
|
||||
// Sub TLV Type 1 is SRv6 Informational Sub TLV
|
||||
for _, stlvRaw := range tlv.Tlv {
|
||||
// Instantiating Information Sub TLV
|
||||
info := &SRv6InformationSubTLV{
|
||||
SubTLV: SubTLV{
|
||||
Type: SubTLVType(1),
|
||||
},
|
||||
SubSubTLVs: make([]PrefixSIDTLVInterface, 0),
|
||||
}
|
||||
var raw ptypes.DynamicAny
|
||||
if err := ptypes.UnmarshalAny(stlvRaw, &raw); err != nil {
|
||||
return 0, nil, err
|
||||
}
|
||||
infoProto := raw.Message.(*api.SRv6InformationSubTLV)
|
||||
info.SID = make([]byte, len(infoProto.Sid))
|
||||
copy(info.SID, infoProto.Sid)
|
||||
// TODO Once RFC is published add processing of flags
|
||||
info.Flags = 0
|
||||
info.EndpointBehavior = uint16(infoProto.EndpointBehavior)
|
||||
var sstlvslength uint16
|
||||
var sstlvs []PrefixSIDTLVInterface
|
||||
if len(infoProto.SubSubTlvs) != 0 {
|
||||
// Processing Sub Sub TLVs
|
||||
var err error
|
||||
sstlvslength, sstlvs, err = UnmarshalSubSubTLVs(infoProto.SubSubTlvs)
|
||||
if err != nil {
|
||||
return 0, nil, err
|
||||
}
|
||||
info.SubSubTLVs = append(info.SubSubTLVs, sstlvs...)
|
||||
}
|
||||
// SRv6 Information Sub TLV length consists 1 byte Resrved2, 16 bytes SID, 1 byte flags, 2 bytes Endpoint Behavior
|
||||
// 1 byte Reserved3 and length of Sub Sub TLVs
|
||||
info.SubTLV.Length = 1 + 16 + 1 + 2 + 1 + sstlvslength
|
||||
// For total Srv6 Information Sub TLV length, adding 3 bytes of the Sub TLV header
|
||||
l += info.SubTLV.Length + 4
|
||||
p = append(p, info)
|
||||
}
|
||||
default:
|
||||
return 0, nil, fmt.Errorf("unknown or not implemented Prefix SID Sub TLV type: %d", t)
|
||||
}
|
||||
}
|
||||
|
||||
return l, p, nil
|
||||
}
|
||||
|
||||
func UnmarshalSubSubTLVs(stlvs map[uint32]*api.SRv6TLV) (uint16, []PrefixSIDTLVInterface, error) {
|
||||
p := make([]PrefixSIDTLVInterface, 0)
|
||||
l := uint16(0)
|
||||
// v.SubTlvs is a map by sub tlv type and the value is a slice of sub tlvs of the specific type
|
||||
for t, tlv := range stlvs {
|
||||
switch t {
|
||||
case 1:
|
||||
// Sub Sub TLV Type 1 is SRv6 Structure Sub Sub TLV
|
||||
for _, stlvRaw := range tlv.Tlv {
|
||||
// Instantiating Information Sub TLV
|
||||
structure := &SRv6SIDStructureSubSubTLV{
|
||||
SubSubTLV: SubSubTLV{
|
||||
Type: SubSubTLVType(1),
|
||||
Length: 6,
|
||||
},
|
||||
}
|
||||
var raw ptypes.DynamicAny
|
||||
if err := ptypes.UnmarshalAny(stlvRaw, &raw); err != nil {
|
||||
return 0, nil, err
|
||||
}
|
||||
structureProto := raw.Message.(*api.SRv6StructureSubSubTLV)
|
||||
structure.LocalBlockLength = uint8(structureProto.LocalBlockLength)
|
||||
structure.LocatorNodeLength = uint8(structureProto.LocalNodeLength)
|
||||
structure.FunctionLength = uint8(structureProto.FunctionLength)
|
||||
structure.ArgumentLength = uint8(structureProto.ArgumentLength)
|
||||
structure.TranspositionLength = uint8(structureProto.TranspositionLength)
|
||||
structure.TranspositionOffset = uint8(structureProto.TranspositionOffset)
|
||||
|
||||
// SRv6 Structure Sub Sub TLV length consists of header 3 bytes, 6 bytes of value
|
||||
l += (3 + 6)
|
||||
p = append(p, structure)
|
||||
}
|
||||
default:
|
||||
return 0, nil, fmt.Errorf("unknown or not implemented Prefix SID Sub TLV type: %d", t)
|
||||
}
|
||||
}
|
||||
|
||||
return l, p, nil
|
||||
}
|
||||
15
vendor/github.com/osrg/gobgp/pkg/packet/bgp/validate.go
generated
vendored
15
vendor/github.com/osrg/gobgp/pkg/packet/bgp/validate.go
generated
vendored
@@ -33,17 +33,18 @@ func ValidateUpdateMsg(m *BGPUpdate, rfs map[RouteFamily]BGPAddPathMode, isEBGP
|
||||
//check specific path attribute
|
||||
ok, err := ValidateAttribute(a, rfs, isEBGP, isConfed)
|
||||
if !ok {
|
||||
if err.(*MessageError).ErrorHandling == ERROR_HANDLING_SESSION_RESET {
|
||||
msgErr := err.(*MessageError)
|
||||
if msgErr.ErrorHandling == ERROR_HANDLING_SESSION_RESET {
|
||||
return false, err
|
||||
} else if err.(*MessageError).Stronger(strongestError) {
|
||||
} else if msgErr.Stronger(strongestError) {
|
||||
strongestError = err
|
||||
}
|
||||
}
|
||||
} else if a.GetType() == BGP_ATTR_TYPE_MP_REACH_NLRI || a.GetType() == BGP_ATTR_TYPE_MP_UNREACH_NLRI {
|
||||
eMsg := "the path attribute apears twice. Type : " + strconv.Itoa(int(a.GetType()))
|
||||
eMsg := "the path attribute appears twice. Type : " + strconv.Itoa(int(a.GetType()))
|
||||
return false, NewMessageError(eCode, eSubCodeAttrList, nil, eMsg)
|
||||
} else {
|
||||
eMsg := "the path attribute apears twice. Type : " + strconv.Itoa(int(a.GetType()))
|
||||
eMsg := "the path attribute appears twice. Type : " + strconv.Itoa(int(a.GetType()))
|
||||
e := NewMessageErrorWithErrorHandling(eCode, eSubCodeAttrList, nil, ERROR_HANDLING_ATTRIBUTE_DISCARD, nil, eMsg)
|
||||
if e.(*MessageError).Stronger(strongestError) {
|
||||
strongestError = e
|
||||
@@ -159,6 +160,10 @@ func ValidateAttribute(a PathAttributeInterface, rfs map[RouteFamily]BGPAddPathM
|
||||
}
|
||||
|
||||
isClassDorE := func(ip net.IP) bool {
|
||||
if ip.To4() == nil {
|
||||
// needs to verify ipv6 too?
|
||||
return false
|
||||
}
|
||||
res := ip[0] & 0xe0
|
||||
return res == 0xe0
|
||||
}
|
||||
@@ -197,7 +202,7 @@ func ValidateAttribute(a PathAttributeInterface, rfs map[RouteFamily]BGPAddPathM
|
||||
for _, x := range p.Values {
|
||||
found := false
|
||||
for _, y := range uniq {
|
||||
if x.String() == y.String() {
|
||||
if x.Eq(y) {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
|
||||
17
vendor/github.com/osrg/gobgp/pkg/packet/bmp/bmp.go
generated
vendored
17
vendor/github.com/osrg/gobgp/pkg/packet/bmp/bmp.go
generated
vendored
@@ -344,8 +344,21 @@ func (body *BMPStatisticsReport) ParseBody(msg *BMPMessage, data []byte) error {
|
||||
s = &BMPStatsTLV64{BMPStatsTLV: tl}
|
||||
case BMP_STAT_TYPE_PER_AFI_SAFI_ADJ_RIB_IN, BMP_STAT_TYPE_PER_AFI_SAFI_LOC_RIB:
|
||||
s = &BMPStatsTLVPerAfiSafi64{BMPStatsTLV: tl}
|
||||
default:
|
||||
case BMP_STAT_TYPE_REJECTED, BMP_STAT_TYPE_DUPLICATE_PREFIX,
|
||||
BMP_STAT_TYPE_DUPLICATE_WITHDRAW, BMP_STAT_TYPE_INV_UPDATE_DUE_TO_CLUSTER_LIST_LOOP,
|
||||
BMP_STAT_TYPE_INV_UPDATE_DUE_TO_AS_PATH_LOOP, BMP_STAT_TYPE_INV_UPDATE_DUE_TO_ORIGINATOR_ID,
|
||||
BMP_STAT_TYPE_INV_UPDATE_DUE_TO_AS_CONFED_LOOP, BMP_STAT_TYPE_WITHDRAW_UPDATE,
|
||||
BMP_STAT_TYPE_WITHDRAW_PREFIX, BMP_STAT_TYPE_DUPLICATE_UPDATE:
|
||||
s = &BMPStatsTLV32{BMPStatsTLV: tl}
|
||||
default:
|
||||
switch tl.Length {
|
||||
case 4:
|
||||
s = &BMPStatsTLV32{BMPStatsTLV: tl}
|
||||
case 8:
|
||||
s = &BMPStatsTLV64{BMPStatsTLV: tl}
|
||||
default:
|
||||
return fmt.Errorf("value length %d is not known for unknown stat type %d", tl.Length, tl.Type)
|
||||
}
|
||||
}
|
||||
if err := s.ParseValue(data); err != nil {
|
||||
return err
|
||||
@@ -371,7 +384,7 @@ func (body *BMPStatisticsReport) Serialize() ([]byte, error) {
|
||||
}
|
||||
|
||||
const (
|
||||
BMP_PEER_DOWN_REASON_UNKNOWN = iota
|
||||
BMP_peerDownByUnknownReason = iota
|
||||
BMP_PEER_DOWN_REASON_LOCAL_BGP_NOTIFICATION
|
||||
BMP_PEER_DOWN_REASON_LOCAL_NO_NOTIFICATION
|
||||
BMP_PEER_DOWN_REASON_REMOTE_BGP_NOTIFICATION
|
||||
|
||||
60
vendor/github.com/osrg/gobgp/pkg/packet/mrt/mrt.go
generated
vendored
60
vendor/github.com/osrg/gobgp/pkg/packet/mrt/mrt.go
generated
vendored
@@ -18,6 +18,7 @@ package mrt
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"fmt"
|
||||
"math"
|
||||
"net"
|
||||
@@ -198,10 +199,11 @@ type Peer struct {
|
||||
AS uint32
|
||||
}
|
||||
|
||||
var errNotAllPeerBytesAvailable = errors.New("not all Peer bytes are available")
|
||||
|
||||
func (p *Peer) DecodeFromBytes(data []byte) ([]byte, error) {
|
||||
notAllBytesAvail := fmt.Errorf("not all Peer bytes are available")
|
||||
if len(data) < 5 {
|
||||
return nil, notAllBytesAvail
|
||||
return nil, errNotAllPeerBytesAvailable
|
||||
}
|
||||
p.Type = uint8(data[0])
|
||||
p.BgpId = net.IP(data[1:5])
|
||||
@@ -209,13 +211,13 @@ func (p *Peer) DecodeFromBytes(data []byte) ([]byte, error) {
|
||||
|
||||
if p.Type&1 > 0 {
|
||||
if len(data) < 16 {
|
||||
return nil, notAllBytesAvail
|
||||
return nil, errNotAllPeerBytesAvailable
|
||||
}
|
||||
p.IpAddress = net.IP(data[:16])
|
||||
data = data[16:]
|
||||
} else {
|
||||
if len(data) < 4 {
|
||||
return nil, notAllBytesAvail
|
||||
return nil, errNotAllPeerBytesAvailable
|
||||
}
|
||||
p.IpAddress = net.IP(data[:4])
|
||||
data = data[4:]
|
||||
@@ -223,13 +225,13 @@ func (p *Peer) DecodeFromBytes(data []byte) ([]byte, error) {
|
||||
|
||||
if p.Type&(1<<1) > 0 {
|
||||
if len(data) < 4 {
|
||||
return nil, notAllBytesAvail
|
||||
return nil, errNotAllPeerBytesAvailable
|
||||
}
|
||||
p.AS = binary.BigEndian.Uint32(data[:4])
|
||||
data = data[4:]
|
||||
} else {
|
||||
if len(data) < 2 {
|
||||
return nil, notAllBytesAvail
|
||||
return nil, errNotAllPeerBytesAvailable
|
||||
}
|
||||
p.AS = uint32(binary.BigEndian.Uint16(data[:2]))
|
||||
data = data[2:]
|
||||
@@ -291,22 +293,23 @@ type PeerIndexTable struct {
|
||||
Peers []*Peer
|
||||
}
|
||||
|
||||
var errNnotAllPeerIndexBytesAvailable = errors.New("not all PeerIndexTable bytes are available")
|
||||
|
||||
func (t *PeerIndexTable) DecodeFromBytes(data []byte) error {
|
||||
notAllBytesAvail := fmt.Errorf("not all PeerIndexTable bytes are available")
|
||||
if len(data) < 6 {
|
||||
return notAllBytesAvail
|
||||
return errNnotAllPeerIndexBytesAvailable
|
||||
}
|
||||
t.CollectorBgpId = net.IP(data[:4])
|
||||
viewLen := binary.BigEndian.Uint16(data[4:6])
|
||||
if len(data) < 6+int(viewLen) {
|
||||
return notAllBytesAvail
|
||||
return errNnotAllPeerIndexBytesAvailable
|
||||
}
|
||||
t.ViewName = string(data[6 : 6+viewLen])
|
||||
|
||||
data = data[6+viewLen:]
|
||||
|
||||
if len(data) < 2 {
|
||||
return notAllBytesAvail
|
||||
return errNnotAllPeerIndexBytesAvailable
|
||||
}
|
||||
peerNum := binary.BigEndian.Uint16(data[:2])
|
||||
data = data[2:]
|
||||
@@ -360,10 +363,11 @@ type RibEntry struct {
|
||||
isAddPath bool
|
||||
}
|
||||
|
||||
var errNotAllRibEntryBytesAvailable = errors.New("not all RibEntry bytes are available")
|
||||
|
||||
func (e *RibEntry) DecodeFromBytes(data []byte) ([]byte, error) {
|
||||
notAllBytesAvail := fmt.Errorf("not all RibEntry bytes are available")
|
||||
if len(data) < 8 {
|
||||
return nil, notAllBytesAvail
|
||||
return nil, errNotAllRibEntryBytesAvailable
|
||||
}
|
||||
e.PeerIndex = binary.BigEndian.Uint16(data[:2])
|
||||
e.OriginatedTime = binary.BigEndian.Uint32(data[2:6])
|
||||
@@ -386,7 +390,7 @@ func (e *RibEntry) DecodeFromBytes(data []byte) ([]byte, error) {
|
||||
}
|
||||
attrLen -= uint16(p.Len())
|
||||
if len(data) < p.Len() {
|
||||
return nil, notAllBytesAvail
|
||||
return nil, errNotAllRibEntryBytesAvailable
|
||||
}
|
||||
data = data[p.Len():]
|
||||
e.PathAttributes = append(e.PathAttributes, p)
|
||||
@@ -417,13 +421,13 @@ func (e *RibEntry) Serialize() ([]byte, error) {
|
||||
}
|
||||
var buf []byte
|
||||
if e.isAddPath {
|
||||
buf = make([]byte, 12)
|
||||
buf = make([]byte, 12, 12+len(pbuf))
|
||||
binary.BigEndian.PutUint16(buf, e.PeerIndex)
|
||||
binary.BigEndian.PutUint32(buf[2:], e.OriginatedTime)
|
||||
binary.BigEndian.PutUint32(buf[6:], e.PathIdentifier)
|
||||
binary.BigEndian.PutUint16(buf[10:], uint16(totalLen))
|
||||
} else {
|
||||
buf = make([]byte, 8)
|
||||
buf = make([]byte, 8, 8+len(pbuf))
|
||||
binary.BigEndian.PutUint16(buf, e.PeerIndex)
|
||||
binary.BigEndian.PutUint32(buf[2:], e.OriginatedTime)
|
||||
binary.BigEndian.PutUint16(buf[6:], uint16(totalLen))
|
||||
@@ -461,7 +465,7 @@ type Rib struct {
|
||||
|
||||
func (u *Rib) DecodeFromBytes(data []byte) error {
|
||||
if len(data) < 4 {
|
||||
return fmt.Errorf("Not all RibIpv4Unicast message bytes available")
|
||||
return fmt.Errorf("not all RibIpv4Unicast message bytes available")
|
||||
}
|
||||
u.SequenceNumber = binary.BigEndian.Uint32(data[:4])
|
||||
data = data[4:]
|
||||
@@ -504,9 +508,9 @@ func (u *Rib) Serialize() ([]byte, error) {
|
||||
switch rf {
|
||||
case bgp.RF_IPv4_UC, bgp.RF_IPv4_MC, bgp.RF_IPv6_UC, bgp.RF_IPv6_MC:
|
||||
default:
|
||||
bbuf := make([]byte, 2)
|
||||
binary.BigEndian.PutUint16(bbuf, u.Prefix.AFI())
|
||||
buf = append(buf, bbuf...)
|
||||
var bbuf [2]byte
|
||||
binary.BigEndian.PutUint16(bbuf[:], u.Prefix.AFI())
|
||||
buf = append(buf, bbuf[:]...)
|
||||
buf = append(buf, u.Prefix.SAFI())
|
||||
}
|
||||
bbuf, err := u.Prefix.Serialize()
|
||||
@@ -665,9 +669,9 @@ type BGP4MPHeader struct {
|
||||
|
||||
func (m *BGP4MPHeader) decodeFromBytes(data []byte) ([]byte, error) {
|
||||
if m.isAS4 && len(data) < 8 {
|
||||
return nil, fmt.Errorf("Not all BGP4MPMessageAS4 bytes available")
|
||||
return nil, errors.New("not all BGP4MPMessageAS4 bytes available")
|
||||
} else if !m.isAS4 && len(data) < 4 {
|
||||
return nil, fmt.Errorf("Not all BGP4MPMessageAS bytes available")
|
||||
return nil, errors.New("not all BGP4MPMessageAS bytes available")
|
||||
}
|
||||
|
||||
if m.isAS4 {
|
||||
@@ -735,7 +739,7 @@ func newBGP4MPHeader(peeras, localas uint32, intfindex uint16, peerip, localip s
|
||||
if paddr != nil && laddr != nil {
|
||||
af = bgp.AFI_IP6
|
||||
} else {
|
||||
return nil, fmt.Errorf("Peer IP Address and Local IP Address must have the same address family")
|
||||
return nil, fmt.Errorf("peer IP Address and Local IP Address must have the same address family")
|
||||
}
|
||||
}
|
||||
return &BGP4MPHeader{
|
||||
@@ -761,7 +765,7 @@ func (m *BGP4MPStateChange) DecodeFromBytes(data []byte) error {
|
||||
return err
|
||||
}
|
||||
if len(rest) < 4 {
|
||||
return fmt.Errorf("Not all BGP4MPStateChange bytes available")
|
||||
return fmt.Errorf("not all BGP4MPStateChange bytes available")
|
||||
}
|
||||
m.OldState = BGPState(binary.BigEndian.Uint16(rest[:2]))
|
||||
m.NewState = BGPState(binary.BigEndian.Uint16(rest[2:4]))
|
||||
@@ -804,7 +808,7 @@ func (m *BGP4MPMessage) DecodeFromBytes(data []byte) error {
|
||||
}
|
||||
|
||||
if len(rest) < bgp.BGP_HEADER_LENGTH {
|
||||
return fmt.Errorf("Not all BGP4MPMessageAS4 bytes available")
|
||||
return fmt.Errorf("not all BGP4MPMessageAS4 bytes available")
|
||||
}
|
||||
|
||||
msg, err := bgp.ParseBGPMessage(rest)
|
||||
@@ -903,7 +907,7 @@ func SplitMrt(data []byte, atEOF bool) (advance int, token []byte, err error) {
|
||||
|
||||
func ParseMRTBody(h *MRTHeader, data []byte) (*MRTMessage, error) {
|
||||
if len(data) < int(h.Len) {
|
||||
return nil, fmt.Errorf("Not all MRT message bytes available. expected: %d, actual: %d", int(h.Len), len(data))
|
||||
return nil, fmt.Errorf("not all MRT message bytes available. expected: %d, actual: %d", int(h.Len), len(data))
|
||||
}
|
||||
msg := &MRTMessage{Header: *h}
|
||||
switch h.Type {
|
||||
@@ -940,7 +944,7 @@ func ParseMRTBody(h *MRTHeader, data []byte) (*MRTMessage, error) {
|
||||
case RIB_GENERIC_ADDPATH:
|
||||
isAddPath = true
|
||||
default:
|
||||
return nil, fmt.Errorf("unsupported table dumpv2 subtype: %v\n", subType)
|
||||
return nil, fmt.Errorf("unsupported table dumpv2 subtype: %v", subType)
|
||||
}
|
||||
|
||||
if msg.Body == nil {
|
||||
@@ -993,10 +997,10 @@ func ParseMRTBody(h *MRTHeader, data []byte) (*MRTMessage, error) {
|
||||
isAddPath: true,
|
||||
}
|
||||
default:
|
||||
return nil, fmt.Errorf("unsupported bgp4mp subtype: %v\n", subType)
|
||||
return nil, fmt.Errorf("unsupported bgp4mp subtype: %v", subType)
|
||||
}
|
||||
default:
|
||||
return nil, fmt.Errorf("unsupported type: %v\n", h.Type)
|
||||
return nil, fmt.Errorf("unsupported type: %v", h.Type)
|
||||
}
|
||||
err := msg.Body.DecodeFromBytes(data)
|
||||
if err != nil {
|
||||
|
||||
4
vendor/github.com/osrg/gobgp/pkg/packet/rtr/rtr.go
generated
vendored
4
vendor/github.com/osrg/gobgp/pkg/packet/rtr/rtr.go
generated
vendored
@@ -355,7 +355,7 @@ func SplitRTR(data []byte, atEOF bool) (advance int, token []byte, err error) {
|
||||
|
||||
totalLen := binary.BigEndian.Uint32(data[4:8])
|
||||
if totalLen < RTR_MIN_LEN {
|
||||
return 0, nil, fmt.Errorf("Invalid length: %d", totalLen)
|
||||
return 0, nil, fmt.Errorf("invalid length: %d", totalLen)
|
||||
}
|
||||
if uint32(len(data)) < totalLen {
|
||||
return 0, nil, nil
|
||||
@@ -385,7 +385,7 @@ func ParseRTR(data []byte) (RTRMessage, error) {
|
||||
case RTR_ERROR_REPORT:
|
||||
msg = &RTRErrorReport{}
|
||||
default:
|
||||
return nil, fmt.Errorf("unknown RTR message type %d:", data[1])
|
||||
return nil, fmt.Errorf("unknown RTR message type %d", data[1])
|
||||
}
|
||||
err := msg.DecodeFromBytes(data)
|
||||
return msg, err
|
||||
|
||||
92
vendor/github.com/osrg/gobgp/pkg/server/bmp.go
generated
vendored
92
vendor/github.com/osrg/gobgp/pkg/server/bmp.go
generated
vendored
@@ -16,11 +16,13 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
api "github.com/osrg/gobgp/api"
|
||||
"github.com/osrg/gobgp/internal/pkg/config"
|
||||
"github.com/osrg/gobgp/internal/pkg/table"
|
||||
"github.com/osrg/gobgp/pkg/packet/bgp"
|
||||
@@ -113,25 +115,25 @@ func (b *bmpClient) loop() {
|
||||
}
|
||||
|
||||
if func() bool {
|
||||
ops := []WatchOption{WatchPeerState(true)}
|
||||
ops := []watchOption{watchPeerState(true)}
|
||||
if b.c.RouteMonitoringPolicy == config.BMP_ROUTE_MONITORING_POLICY_TYPE_BOTH {
|
||||
log.WithFields(
|
||||
log.Fields{"Topic": "bmp"},
|
||||
).Warn("both option for route-monitoring-policy is obsoleted")
|
||||
}
|
||||
if b.c.RouteMonitoringPolicy == config.BMP_ROUTE_MONITORING_POLICY_TYPE_PRE_POLICY || b.c.RouteMonitoringPolicy == config.BMP_ROUTE_MONITORING_POLICY_TYPE_ALL {
|
||||
ops = append(ops, WatchUpdate(true))
|
||||
ops = append(ops, watchUpdate(true))
|
||||
}
|
||||
if b.c.RouteMonitoringPolicy == config.BMP_ROUTE_MONITORING_POLICY_TYPE_POST_POLICY || b.c.RouteMonitoringPolicy == config.BMP_ROUTE_MONITORING_POLICY_TYPE_ALL {
|
||||
ops = append(ops, WatchPostUpdate(true))
|
||||
ops = append(ops, watchPostUpdate(true))
|
||||
}
|
||||
if b.c.RouteMonitoringPolicy == config.BMP_ROUTE_MONITORING_POLICY_TYPE_LOCAL_RIB || b.c.RouteMonitoringPolicy == config.BMP_ROUTE_MONITORING_POLICY_TYPE_ALL {
|
||||
ops = append(ops, WatchBestPath(true))
|
||||
ops = append(ops, watchBestPath(true))
|
||||
}
|
||||
if b.c.RouteMirroringEnabled {
|
||||
ops = append(ops, WatchMessage(false))
|
||||
ops = append(ops, watchMessage(false))
|
||||
}
|
||||
w := b.s.Watch(ops...)
|
||||
w := b.s.watch(ops...)
|
||||
defer w.Stop()
|
||||
|
||||
var tickerCh <-chan time.Time
|
||||
@@ -152,7 +154,12 @@ func (b *bmpClient) loop() {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := write(bmp.NewBMPInitiation([]bmp.BMPInfoTLVInterface{})); err != nil {
|
||||
tlv := []bmp.BMPInfoTLVInterface{
|
||||
bmp.NewBMPInfoTLVString(bmp.BMP_INIT_TLV_TYPE_SYS_NAME, b.c.SysName),
|
||||
bmp.NewBMPInfoTLVString(bmp.BMP_INIT_TLV_TYPE_SYS_DESCR, b.c.SysDescr),
|
||||
}
|
||||
|
||||
if err := write(bmp.NewBMPInitiation(tlv)); err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -160,7 +167,7 @@ func (b *bmpClient) loop() {
|
||||
select {
|
||||
case ev := <-w.Event():
|
||||
switch msg := ev.(type) {
|
||||
case *WatchEventUpdate:
|
||||
case *watchEventUpdate:
|
||||
info := &table.PeerInfo{
|
||||
Address: msg.PeerAddress,
|
||||
AS: msg.PeerAS,
|
||||
@@ -180,17 +187,15 @@ func (b *bmpClient) loop() {
|
||||
for _, path := range pathList {
|
||||
for _, u := range table.CreateUpdateMsgFromPaths([]*table.Path{path}) {
|
||||
payload, _ := u.Serialize()
|
||||
if err := write(bmpPeerRoute(bmp.BMP_PEER_TYPE_GLOBAL, msg.PostPolicy, 0, true, info, msg.Timestamp.Unix(), payload)); err != nil {
|
||||
if err := write(bmpPeerRoute(bmp.BMP_PEER_TYPE_GLOBAL, msg.PostPolicy, 0, true, info, path.GetTimestamp().Unix(), payload)); err != nil {
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if err := write(bmpPeerRoute(bmp.BMP_PEER_TYPE_GLOBAL, msg.PostPolicy, 0, msg.FourBytesAs, info, msg.Timestamp.Unix(), msg.Payload)); err != nil {
|
||||
return false
|
||||
}
|
||||
} else if err := write(bmpPeerRoute(bmp.BMP_PEER_TYPE_GLOBAL, msg.PostPolicy, 0, msg.FourBytesAs, info, msg.Timestamp.Unix(), msg.Payload)); err != nil {
|
||||
return false
|
||||
}
|
||||
case *WatchEventBestPath:
|
||||
case *watchEventBestPath:
|
||||
info := &table.PeerInfo{
|
||||
Address: net.ParseIP("0.0.0.0").To4(),
|
||||
AS: b.s.bgpConfig.Global.Config.As,
|
||||
@@ -204,7 +209,7 @@ func (b *bmpClient) loop() {
|
||||
return false
|
||||
}
|
||||
}
|
||||
case *WatchEventPeerState:
|
||||
case *watchEventPeerState:
|
||||
if msg.State == bgp.BGP_FSM_ESTABLISHED {
|
||||
if err := write(bmpPeerUp(msg, bmp.BMP_PEER_TYPE_GLOBAL, false, 0)); err != nil {
|
||||
return false
|
||||
@@ -214,7 +219,7 @@ func (b *bmpClient) loop() {
|
||||
return false
|
||||
}
|
||||
}
|
||||
case *WatchEventMessage:
|
||||
case *watchEventMessage:
|
||||
info := &table.PeerInfo{
|
||||
Address: msg.PeerAddress,
|
||||
AS: msg.PeerAS,
|
||||
@@ -225,14 +230,15 @@ func (b *bmpClient) loop() {
|
||||
}
|
||||
}
|
||||
case <-tickerCh:
|
||||
neighborList := b.s.getNeighbor("", true)
|
||||
for _, n := range neighborList {
|
||||
if n.State.SessionState != config.SESSION_STATE_ESTABLISHED {
|
||||
continue
|
||||
}
|
||||
if err := write(bmpPeerStats(bmp.BMP_PEER_TYPE_GLOBAL, 0, 0, n)); err != nil {
|
||||
return false
|
||||
}
|
||||
var err error
|
||||
b.s.ListPeer(context.Background(), &api.ListPeerRequest{EnableAdvertised: true},
|
||||
func(peer *api.Peer) {
|
||||
if err == nil && peer.State.SessionState == api.PeerState_ESTABLISHED {
|
||||
err = write(bmpPeerStats(bmp.BMP_PEER_TYPE_GLOBAL, 0, time.Now().Unix(), peer))
|
||||
}
|
||||
})
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
case <-b.dead:
|
||||
term := bmp.NewBMPTermination([]bmp.BMPTermTLVInterface{
|
||||
@@ -259,7 +265,7 @@ type bmpClient struct {
|
||||
ribout ribout
|
||||
}
|
||||
|
||||
func bmpPeerUp(ev *WatchEventPeerState, t uint8, policy bool, pd uint64) *bmp.BMPMessage {
|
||||
func bmpPeerUp(ev *watchEventPeerState, t uint8, policy bool, pd uint64) *bmp.BMPMessage {
|
||||
var flags uint8 = 0
|
||||
if policy {
|
||||
flags |= bmp.BMP_PEER_FLAG_POST_POLICY
|
||||
@@ -268,13 +274,27 @@ func bmpPeerUp(ev *WatchEventPeerState, t uint8, policy bool, pd uint64) *bmp.BM
|
||||
return bmp.NewBMPPeerUpNotification(*ph, ev.LocalAddress.String(), ev.LocalPort, ev.PeerPort, ev.SentOpen, ev.RecvOpen)
|
||||
}
|
||||
|
||||
func bmpPeerDown(ev *WatchEventPeerState, t uint8, policy bool, pd uint64) *bmp.BMPMessage {
|
||||
func bmpPeerDown(ev *watchEventPeerState, t uint8, policy bool, pd uint64) *bmp.BMPMessage {
|
||||
var flags uint8 = 0
|
||||
if policy {
|
||||
flags |= bmp.BMP_PEER_FLAG_POST_POLICY
|
||||
}
|
||||
ph := bmp.NewBMPPeerHeader(t, flags, pd, ev.PeerAddress.String(), ev.PeerAS, ev.PeerID.String(), float64(ev.Timestamp.Unix()))
|
||||
return bmp.NewBMPPeerDownNotification(*ph, uint8(ev.StateReason.PeerDownReason), ev.StateReason.BGPNotification, ev.StateReason.Data)
|
||||
|
||||
reasonCode := bmp.BMP_peerDownByUnknownReason
|
||||
switch ev.StateReason.Type {
|
||||
case fsmDying, fsmInvalidMsg, fsmNotificationSent, fsmHoldTimerExpired, fsmIdleTimerExpired, fsmRestartTimerExpired:
|
||||
reasonCode = bmp.BMP_PEER_DOWN_REASON_LOCAL_BGP_NOTIFICATION
|
||||
case fsmAdminDown:
|
||||
reasonCode = bmp.BMP_PEER_DOWN_REASON_LOCAL_NO_NOTIFICATION
|
||||
case fsmNotificationRecv, fsmGracefulRestart, fsmHardReset:
|
||||
reasonCode = bmp.BMP_PEER_DOWN_REASON_REMOTE_BGP_NOTIFICATION
|
||||
case fsmReadFailed, fsmWriteFailed:
|
||||
reasonCode = bmp.BMP_PEER_DOWN_REASON_REMOTE_NO_NOTIFICATION
|
||||
case fsmDeConfigured:
|
||||
reasonCode = bmp.BMP_PEER_DOWN_REASON_PEER_DE_CONFIGURED
|
||||
}
|
||||
return bmp.NewBMPPeerDownNotification(*ph, uint8(reasonCode), ev.StateReason.BGPNotification, ev.StateReason.Data)
|
||||
}
|
||||
|
||||
func bmpPeerRoute(t uint8, policy bool, pd uint64, fourBytesAs bool, peeri *table.PeerInfo, timestamp int64, payload []byte) *bmp.BMPMessage {
|
||||
@@ -292,16 +312,22 @@ func bmpPeerRoute(t uint8, policy bool, pd uint64, fourBytesAs bool, peeri *tabl
|
||||
return m
|
||||
}
|
||||
|
||||
func bmpPeerStats(peerType uint8, peerDist uint64, timestamp int64, neighConf *config.Neighbor) *bmp.BMPMessage {
|
||||
func bmpPeerStats(peerType uint8, peerDist uint64, timestamp int64, peer *api.Peer) *bmp.BMPMessage {
|
||||
var peerFlags uint8 = 0
|
||||
ph := bmp.NewBMPPeerHeader(peerType, peerFlags, peerDist, neighConf.State.NeighborAddress, neighConf.State.PeerAs, neighConf.State.RemoteRouterId, float64(timestamp))
|
||||
ph := bmp.NewBMPPeerHeader(peerType, peerFlags, peerDist, peer.State.NeighborAddress, peer.State.PeerAs, peer.State.RouterId, float64(timestamp))
|
||||
received := uint64(0)
|
||||
accepted := uint64(0)
|
||||
for _, a := range peer.AfiSafis {
|
||||
received += a.State.Received
|
||||
accepted += a.State.Accepted
|
||||
}
|
||||
return bmp.NewBMPStatisticsReport(
|
||||
*ph,
|
||||
[]bmp.BMPStatsTLVInterface{
|
||||
bmp.NewBMPStatsTLV64(bmp.BMP_STAT_TYPE_ADJ_RIB_IN, uint64(neighConf.State.AdjTable.Accepted)),
|
||||
bmp.NewBMPStatsTLV64(bmp.BMP_STAT_TYPE_LOC_RIB, uint64(neighConf.State.AdjTable.Advertised+neighConf.State.AdjTable.Filtered)),
|
||||
bmp.NewBMPStatsTLV32(bmp.BMP_STAT_TYPE_WITHDRAW_UPDATE, neighConf.State.Messages.Received.WithdrawUpdate),
|
||||
bmp.NewBMPStatsTLV32(bmp.BMP_STAT_TYPE_WITHDRAW_PREFIX, neighConf.State.Messages.Received.WithdrawPrefix),
|
||||
bmp.NewBMPStatsTLV64(bmp.BMP_STAT_TYPE_ADJ_RIB_IN, received),
|
||||
bmp.NewBMPStatsTLV64(bmp.BMP_STAT_TYPE_LOC_RIB, accepted),
|
||||
bmp.NewBMPStatsTLV32(bmp.BMP_STAT_TYPE_WITHDRAW_UPDATE, uint32(peer.State.Messages.Received.WithdrawUpdate)),
|
||||
bmp.NewBMPStatsTLV32(bmp.BMP_STAT_TYPE_WITHDRAW_PREFIX, uint32(peer.State.Messages.Received.WithdrawPrefix)),
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
222
vendor/github.com/osrg/gobgp/pkg/server/collector.go
generated
vendored
222
vendor/github.com/osrg/gobgp/pkg/server/collector.go
generated
vendored
@@ -1,222 +0,0 @@
|
||||
// Copyright (C) 2016 Nippon Telegraph and Telephone Corporation.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
// implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package server
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/influxdata/influxdb/client/v2"
|
||||
"github.com/osrg/gobgp/internal/pkg/table"
|
||||
"github.com/osrg/gobgp/pkg/packet/bgp"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type Collector struct {
|
||||
s *BgpServer
|
||||
url string
|
||||
dbName string
|
||||
interval uint64
|
||||
client client.Client
|
||||
}
|
||||
|
||||
const (
|
||||
MEATUREMENT_UPDATE = "update"
|
||||
MEATUREMENT_PEER = "peer"
|
||||
MEATUREMENT_TABLE = "table"
|
||||
)
|
||||
|
||||
func (c *Collector) writePoints(points []*client.Point) error {
|
||||
bp, _ := client.NewBatchPoints(client.BatchPointsConfig{
|
||||
Database: c.dbName,
|
||||
Precision: "ms",
|
||||
})
|
||||
bp.AddPoints(points)
|
||||
return c.client.Write(bp)
|
||||
}
|
||||
|
||||
func (c *Collector) writePeer(msg *WatchEventPeerState) error {
|
||||
var state string
|
||||
switch msg.State {
|
||||
case bgp.BGP_FSM_ESTABLISHED:
|
||||
state = "Established"
|
||||
case bgp.BGP_FSM_IDLE:
|
||||
state = "Idle"
|
||||
default:
|
||||
return fmt.Errorf("unexpected fsm state %v", msg.State)
|
||||
}
|
||||
|
||||
tags := map[string]string{
|
||||
"PeerAddress": msg.PeerAddress.String(),
|
||||
"PeerAS": fmt.Sprintf("%v", msg.PeerAS),
|
||||
"State": state,
|
||||
}
|
||||
|
||||
fields := map[string]interface{}{
|
||||
"PeerID": msg.PeerID.String(),
|
||||
}
|
||||
|
||||
pt, err := client.NewPoint(MEATUREMENT_PEER, tags, fields, msg.Timestamp)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return c.writePoints([]*client.Point{pt})
|
||||
}
|
||||
|
||||
func path2data(path *table.Path) (map[string]interface{}, map[string]string) {
|
||||
fields := map[string]interface{}{
|
||||
"RouterID": path.GetSource().ID,
|
||||
}
|
||||
if asPath := path.GetAsPath(); asPath != nil {
|
||||
fields["ASPath"] = asPath.String()
|
||||
}
|
||||
if origin, err := path.GetOrigin(); err == nil {
|
||||
typ := "-"
|
||||
switch origin {
|
||||
case bgp.BGP_ORIGIN_ATTR_TYPE_IGP:
|
||||
typ = "i"
|
||||
case bgp.BGP_ORIGIN_ATTR_TYPE_EGP:
|
||||
typ = "e"
|
||||
case bgp.BGP_ORIGIN_ATTR_TYPE_INCOMPLETE:
|
||||
typ = "?"
|
||||
}
|
||||
fields["Origin"] = typ
|
||||
}
|
||||
if med, err := path.GetMed(); err == nil {
|
||||
fields["Med"] = med
|
||||
}
|
||||
|
||||
tags := map[string]string{
|
||||
"PeerAddress": path.GetSource().Address.String(),
|
||||
"PeerAS": fmt.Sprintf("%v", path.GetSource().AS),
|
||||
"Timestamp": path.GetTimestamp().String(),
|
||||
}
|
||||
if nexthop := path.GetNexthop(); len(nexthop) > 0 {
|
||||
fields["NextHop"] = nexthop.String()
|
||||
}
|
||||
if originAS := path.GetSourceAs(); originAS != 0 {
|
||||
fields["OriginAS"] = fmt.Sprintf("%v", originAS)
|
||||
}
|
||||
|
||||
if err := bgp.FlatUpdate(tags, path.GetNlri().Flat()); err != nil {
|
||||
log.WithFields(log.Fields{"Type": "collector", "Error": err}).Error("NLRI FlatUpdate failed")
|
||||
}
|
||||
for _, p := range path.GetPathAttrs() {
|
||||
if err := bgp.FlatUpdate(tags, p.Flat()); err != nil {
|
||||
log.WithFields(log.Fields{"Type": "collector", "Error": err}).Error("PathAttr FlatUpdate failed")
|
||||
}
|
||||
}
|
||||
return fields, tags
|
||||
}
|
||||
|
||||
func (c *Collector) writeUpdate(msg *WatchEventUpdate) error {
|
||||
if len(msg.PathList) == 0 {
|
||||
// EOR
|
||||
return nil
|
||||
}
|
||||
now := time.Now()
|
||||
points := make([]*client.Point, 0, len(msg.PathList))
|
||||
for _, path := range msg.PathList {
|
||||
fields, tags := path2data(path)
|
||||
tags["Withdraw"] = fmt.Sprintf("%v", path.IsWithdraw)
|
||||
pt, err := client.NewPoint(MEATUREMENT_UPDATE, tags, fields, now)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to write update, %v", err)
|
||||
}
|
||||
points = append(points, pt)
|
||||
}
|
||||
return c.writePoints(points)
|
||||
}
|
||||
|
||||
func (c *Collector) writeTable(msg *WatchEventAdjIn) error {
|
||||
now := time.Now()
|
||||
points := make([]*client.Point, 0, len(msg.PathList))
|
||||
for _, path := range msg.PathList {
|
||||
fields, tags := path2data(path)
|
||||
pt, err := client.NewPoint(MEATUREMENT_TABLE, tags, fields, now)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to write table, %v", err)
|
||||
}
|
||||
points = append(points, pt)
|
||||
}
|
||||
return c.writePoints(points)
|
||||
}
|
||||
|
||||
func (c *Collector) loop() {
|
||||
w := c.s.Watch(WatchPeerState(true), WatchUpdate(false))
|
||||
defer w.Stop()
|
||||
|
||||
ticker := func() *time.Ticker {
|
||||
if c.interval == 0 {
|
||||
return &time.Ticker{}
|
||||
}
|
||||
return time.NewTicker(time.Second * time.Duration(c.interval))
|
||||
}()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ticker.C:
|
||||
w.Generate(WATCH_EVENT_TYPE_PRE_UPDATE)
|
||||
case ev := <-w.Event():
|
||||
switch msg := ev.(type) {
|
||||
case *WatchEventUpdate:
|
||||
if err := c.writeUpdate(msg); err != nil {
|
||||
log.WithFields(log.Fields{"Type": "collector", "Error": err}).Error("Failed to write update event message")
|
||||
}
|
||||
case *WatchEventPeerState:
|
||||
if err := c.writePeer(msg); err != nil {
|
||||
log.WithFields(log.Fields{"Type": "collector", "Error": err}).Error("Failed to write state changed event message")
|
||||
}
|
||||
case *WatchEventAdjIn:
|
||||
if err := c.writeTable(msg); err != nil {
|
||||
log.WithFields(log.Fields{"Type": "collector", "Error": err}).Error("Failed to write Adj-In event message")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func NewCollector(s *BgpServer, url, dbName string, interval uint64) (*Collector, error) {
|
||||
c, err := client.NewHTTPClient(client.HTTPConfig{
|
||||
Addr: url,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
_, _, err = c.Ping(0)
|
||||
if err != nil {
|
||||
log.Error("can not connect to InfluxDB")
|
||||
log.WithFields(log.Fields{"Type": "collector", "Error": err}).Error("Failed to connect to InfluxDB")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
q := client.NewQuery("CREATE DATABASE "+dbName, "", "")
|
||||
if response, err := c.Query(q); err != nil || response.Error() != nil {
|
||||
log.WithFields(log.Fields{"Type": "collector", "Error": err}).Errorf("Failed to create database:%s", dbName)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
collector := &Collector{
|
||||
s: s,
|
||||
url: url,
|
||||
dbName: dbName,
|
||||
interval: interval,
|
||||
client: c,
|
||||
}
|
||||
go collector.loop()
|
||||
return collector, nil
|
||||
}
|
||||
855
vendor/github.com/osrg/gobgp/pkg/server/fsm.go
generated
vendored
855
vendor/github.com/osrg/gobgp/pkg/server/fsm.go
generated
vendored
File diff suppressed because it is too large
Load Diff
1462
vendor/github.com/osrg/gobgp/pkg/server/grpc_server.go
generated
vendored
1462
vendor/github.com/osrg/gobgp/pkg/server/grpc_server.go
generated
vendored
File diff suppressed because it is too large
Load Diff
56
vendor/github.com/osrg/gobgp/pkg/server/mrt.go
generated
vendored
56
vendor/github.com/osrg/gobgp/pkg/server/mrt.go
generated
vendored
@@ -30,8 +30,8 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
MIN_ROTATION_INTERVAL = 60
|
||||
MIN_DUMP_INTERVAL = 60
|
||||
minRotationInterval = 60
|
||||
minDumpInterval = 60
|
||||
)
|
||||
|
||||
type mrtWriter struct {
|
||||
@@ -48,16 +48,16 @@ func (m *mrtWriter) Stop() {
|
||||
}
|
||||
|
||||
func (m *mrtWriter) loop() error {
|
||||
ops := []WatchOption{}
|
||||
ops := []watchOption{}
|
||||
switch m.c.DumpType {
|
||||
case config.MRT_TYPE_UPDATES:
|
||||
ops = append(ops, WatchUpdate(false))
|
||||
ops = append(ops, watchUpdate(false))
|
||||
case config.MRT_TYPE_TABLE:
|
||||
if len(m.c.TableName) > 0 {
|
||||
ops = append(ops, WatchTableName(m.c.TableName))
|
||||
ops = append(ops, watchTableName(m.c.TableName))
|
||||
}
|
||||
}
|
||||
w := m.s.Watch(ops...)
|
||||
w := m.s.watch(ops...)
|
||||
rotator := func() *time.Ticker {
|
||||
if m.rotationInterval == 0 {
|
||||
return &time.Ticker{}
|
||||
@@ -85,10 +85,10 @@ func (m *mrtWriter) loop() error {
|
||||
}()
|
||||
|
||||
for {
|
||||
serialize := func(ev WatchEvent) []*mrt.MRTMessage {
|
||||
serialize := func(ev watchEvent) []*mrt.MRTMessage {
|
||||
msg := make([]*mrt.MRTMessage, 0, 1)
|
||||
switch e := ev.(type) {
|
||||
case *WatchEventUpdate:
|
||||
case *watchEventUpdate:
|
||||
if e.Init {
|
||||
return nil
|
||||
}
|
||||
@@ -113,7 +113,7 @@ func (m *mrtWriter) loop() error {
|
||||
} else {
|
||||
msg = append(msg, bm)
|
||||
}
|
||||
case *WatchEventTable:
|
||||
case *watchEventTable:
|
||||
t := uint32(time.Now().Unix())
|
||||
|
||||
peers := make([]*mrt.Peer, 1, len(e.Neighbor)+1)
|
||||
@@ -125,7 +125,7 @@ func (m *mrtWriter) loop() error {
|
||||
neighborMap[pconf.State.NeighborAddress] = pconf
|
||||
}
|
||||
|
||||
if bm, err := mrt.NewMRTMessage(t, mrt.TABLE_DUMPv2, mrt.PEER_INDEX_TABLE, mrt.NewPeerIndexTable(e.RouterId, "", peers)); err != nil {
|
||||
if bm, err := mrt.NewMRTMessage(t, mrt.TABLE_DUMPv2, mrt.PEER_INDEX_TABLE, mrt.NewPeerIndexTable(e.RouterID, "", peers)); err != nil {
|
||||
log.WithFields(log.Fields{
|
||||
"Topic": "mrt",
|
||||
"Data": e,
|
||||
@@ -137,12 +137,12 @@ func (m *mrtWriter) loop() error {
|
||||
}
|
||||
|
||||
idx := func(p *table.Path) uint16 {
|
||||
for i, pconf := range e.Neighbor {
|
||||
if p.GetSource().Address.String() == pconf.State.NeighborAddress {
|
||||
for i, peer := range peers {
|
||||
if peer.IpAddress.String() == p.GetSource().Address.String() {
|
||||
return uint16(i)
|
||||
}
|
||||
}
|
||||
return uint16(len(e.Neighbor))
|
||||
return uint16(len(peers))
|
||||
}
|
||||
|
||||
subtype := func(p *table.Path, isAddPath bool) mrt.MRTSubTypeTableDumpv2 {
|
||||
@@ -205,8 +205,8 @@ func (m *mrtWriter) loop() error {
|
||||
return msg
|
||||
}
|
||||
|
||||
drain := func(ev WatchEvent) {
|
||||
events := make([]WatchEvent, 0, 1+len(w.Event()))
|
||||
drain := func(ev watchEvent) {
|
||||
events := make([]watchEvent, 0, 1+len(w.Event()))
|
||||
if ev != nil {
|
||||
events = append(events, ev)
|
||||
}
|
||||
@@ -274,23 +274,23 @@ func (m *mrtWriter) loop() error {
|
||||
if m.c.DumpType == config.MRT_TYPE_UPDATES {
|
||||
rotate()
|
||||
} else {
|
||||
w.Generate(WATCH_EVENT_TYPE_TABLE)
|
||||
w.Generate(watchEventTypeTable)
|
||||
}
|
||||
case <-dump.C:
|
||||
w.Generate(WATCH_EVENT_TYPE_TABLE)
|
||||
w.Generate(watchEventTypeTable)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func mrtFileOpen(filename string, interval uint64) (*os.File, error) {
|
||||
func mrtFileOpen(filename string, rInterval uint64) (*os.File, error) {
|
||||
realname := filename
|
||||
if interval != 0 {
|
||||
if rInterval != 0 {
|
||||
realname = time.Now().Format(filename)
|
||||
}
|
||||
log.WithFields(log.Fields{
|
||||
"Topic": "mrt",
|
||||
"Filename": realname,
|
||||
"Dump Interval": interval,
|
||||
"Topic": "mrt",
|
||||
"Filename": realname,
|
||||
"RotationInterval": rInterval,
|
||||
}).Debug("Setting new MRT destination file")
|
||||
|
||||
i := len(realname)
|
||||
@@ -354,21 +354,21 @@ func (m *mrtManager) enable(c *config.MrtConfig) error {
|
||||
dInterval := c.DumpInterval
|
||||
|
||||
setRotationMin := func() {
|
||||
if rInterval < MIN_ROTATION_INTERVAL {
|
||||
if rInterval < minRotationInterval {
|
||||
log.WithFields(log.Fields{
|
||||
"Topic": "MRT",
|
||||
}).Infof("minimum mrt rotation interval is %d seconds", MIN_ROTATION_INTERVAL)
|
||||
rInterval = MIN_ROTATION_INTERVAL
|
||||
}).Infof("minimum mrt rotation interval is %d seconds", minRotationInterval)
|
||||
rInterval = minRotationInterval
|
||||
}
|
||||
}
|
||||
|
||||
if c.DumpType == config.MRT_TYPE_TABLE {
|
||||
if rInterval == 0 {
|
||||
if dInterval < MIN_DUMP_INTERVAL {
|
||||
if dInterval < minDumpInterval {
|
||||
log.WithFields(log.Fields{
|
||||
"Topic": "MRT",
|
||||
}).Infof("minimum mrt dump interval is %d seconds", MIN_DUMP_INTERVAL)
|
||||
dInterval = MIN_DUMP_INTERVAL
|
||||
}).Infof("minimum mrt dump interval is %d seconds", minDumpInterval)
|
||||
dInterval = minDumpInterval
|
||||
}
|
||||
} else if dInterval == 0 {
|
||||
setRotationMin()
|
||||
|
||||
180
vendor/github.com/osrg/gobgp/pkg/server/peer.go
generated
vendored
180
vendor/github.com/osrg/gobgp/pkg/server/peer.go
generated
vendored
@@ -24,42 +24,40 @@ import (
|
||||
"github.com/osrg/gobgp/internal/pkg/table"
|
||||
"github.com/osrg/gobgp/pkg/packet/bgp"
|
||||
|
||||
"github.com/eapache/channels"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
const (
|
||||
FLOP_THRESHOLD = time.Second * 30
|
||||
MIN_CONNECT_RETRY = 10
|
||||
flopThreshold = time.Second * 30
|
||||
)
|
||||
|
||||
type PeerGroup struct {
|
||||
type peerGroup struct {
|
||||
Conf *config.PeerGroup
|
||||
members map[string]config.Neighbor
|
||||
dynamicNeighbors map[string]*config.DynamicNeighbor
|
||||
}
|
||||
|
||||
func NewPeerGroup(c *config.PeerGroup) *PeerGroup {
|
||||
return &PeerGroup{
|
||||
func newPeerGroup(c *config.PeerGroup) *peerGroup {
|
||||
return &peerGroup{
|
||||
Conf: c,
|
||||
members: make(map[string]config.Neighbor),
|
||||
dynamicNeighbors: make(map[string]*config.DynamicNeighbor),
|
||||
}
|
||||
}
|
||||
|
||||
func (pg *PeerGroup) AddMember(c config.Neighbor) {
|
||||
func (pg *peerGroup) AddMember(c config.Neighbor) {
|
||||
pg.members[c.State.NeighborAddress] = c
|
||||
}
|
||||
|
||||
func (pg *PeerGroup) DeleteMember(c config.Neighbor) {
|
||||
func (pg *peerGroup) DeleteMember(c config.Neighbor) {
|
||||
delete(pg.members, c.State.NeighborAddress)
|
||||
}
|
||||
|
||||
func (pg *PeerGroup) AddDynamicNeighbor(c *config.DynamicNeighbor) {
|
||||
func (pg *peerGroup) AddDynamicNeighbor(c *config.DynamicNeighbor) {
|
||||
pg.dynamicNeighbors[c.Config.Prefix] = c
|
||||
}
|
||||
|
||||
func newDynamicPeer(g *config.Global, neighborAddress string, pg *config.PeerGroup, loc *table.TableManager, policy *table.RoutingPolicy) *Peer {
|
||||
func newDynamicPeer(g *config.Global, neighborAddress string, pg *config.PeerGroup, loc *table.TableManager, policy *table.RoutingPolicy) *peer {
|
||||
conf := config.Neighbor{
|
||||
Config: config.NeighborConfig{
|
||||
PeerGroup: pg.Config.PeerGroupName,
|
||||
@@ -87,30 +85,28 @@ func newDynamicPeer(g *config.Global, neighborAddress string, pg *config.PeerGro
|
||||
}).Debugf("Can't set default config: %s", err)
|
||||
return nil
|
||||
}
|
||||
peer := NewPeer(g, &conf, loc, policy)
|
||||
peer := newPeer(g, &conf, loc, policy)
|
||||
peer.fsm.lock.Lock()
|
||||
peer.fsm.state = bgp.BGP_FSM_ACTIVE
|
||||
peer.fsm.lock.Unlock()
|
||||
return peer
|
||||
}
|
||||
|
||||
type Peer struct {
|
||||
type peer struct {
|
||||
tableId string
|
||||
fsm *FSM
|
||||
fsm *fsm
|
||||
adjRibIn *table.AdjRib
|
||||
outgoing *channels.InfiniteChannel
|
||||
policy *table.RoutingPolicy
|
||||
localRib *table.TableManager
|
||||
prefixLimitWarned map[bgp.RouteFamily]bool
|
||||
llgrEndChs []chan struct{}
|
||||
}
|
||||
|
||||
func NewPeer(g *config.Global, conf *config.Neighbor, loc *table.TableManager, policy *table.RoutingPolicy) *Peer {
|
||||
peer := &Peer{
|
||||
outgoing: channels.NewInfiniteChannel(),
|
||||
func newPeer(g *config.Global, conf *config.Neighbor, loc *table.TableManager, policy *table.RoutingPolicy) *peer {
|
||||
peer := &peer{
|
||||
localRib: loc,
|
||||
policy: policy,
|
||||
fsm: NewFSM(g, conf, policy),
|
||||
fsm: newFSM(g, conf),
|
||||
prefixLimitWarned: make(map[bgp.RouteFamily]bool),
|
||||
}
|
||||
if peer.isRouteServerClient() {
|
||||
@@ -123,45 +119,53 @@ func NewPeer(g *config.Global, conf *config.Neighbor, loc *table.TableManager, p
|
||||
return peer
|
||||
}
|
||||
|
||||
func (peer *Peer) AS() uint32 {
|
||||
func (peer *peer) AS() uint32 {
|
||||
peer.fsm.lock.RLock()
|
||||
defer peer.fsm.lock.RUnlock()
|
||||
return peer.fsm.pConf.State.PeerAs
|
||||
}
|
||||
|
||||
func (peer *Peer) ID() string {
|
||||
func (peer *peer) ID() string {
|
||||
peer.fsm.lock.RLock()
|
||||
defer peer.fsm.lock.RUnlock()
|
||||
return peer.fsm.pConf.State.NeighborAddress
|
||||
}
|
||||
|
||||
func (peer *Peer) TableID() string {
|
||||
func (peer *peer) TableID() string {
|
||||
return peer.tableId
|
||||
}
|
||||
|
||||
func (peer *Peer) isIBGPPeer() bool {
|
||||
return peer.fsm.pConf.State.PeerAs == peer.fsm.gConf.Config.As
|
||||
func (peer *peer) isIBGPPeer() bool {
|
||||
peer.fsm.lock.RLock()
|
||||
defer peer.fsm.lock.RUnlock()
|
||||
return peer.fsm.pConf.State.PeerType == config.PEER_TYPE_INTERNAL
|
||||
}
|
||||
|
||||
func (peer *Peer) isRouteServerClient() bool {
|
||||
func (peer *peer) isRouteServerClient() bool {
|
||||
peer.fsm.lock.RLock()
|
||||
defer peer.fsm.lock.RUnlock()
|
||||
return peer.fsm.pConf.RouteServer.Config.RouteServerClient
|
||||
}
|
||||
|
||||
func (peer *Peer) isRouteReflectorClient() bool {
|
||||
func (peer *peer) isSecondaryRouteEnabled() bool {
|
||||
peer.fsm.lock.RLock()
|
||||
defer peer.fsm.lock.RUnlock()
|
||||
return peer.fsm.pConf.RouteServer.Config.RouteServerClient && peer.fsm.pConf.RouteServer.Config.SecondaryRoute
|
||||
}
|
||||
|
||||
func (peer *peer) isRouteReflectorClient() bool {
|
||||
peer.fsm.lock.RLock()
|
||||
defer peer.fsm.lock.RUnlock()
|
||||
return peer.fsm.pConf.RouteReflector.Config.RouteReflectorClient
|
||||
}
|
||||
|
||||
func (peer *Peer) isGracefulRestartEnabled() bool {
|
||||
func (peer *peer) isGracefulRestartEnabled() bool {
|
||||
peer.fsm.lock.RLock()
|
||||
defer peer.fsm.lock.RUnlock()
|
||||
return peer.fsm.pConf.GracefulRestart.State.Enabled
|
||||
}
|
||||
|
||||
func (peer *Peer) getAddPathMode(family bgp.RouteFamily) bgp.BGPAddPathMode {
|
||||
func (peer *peer) getAddPathMode(family bgp.RouteFamily) bgp.BGPAddPathMode {
|
||||
peer.fsm.lock.RLock()
|
||||
defer peer.fsm.lock.RUnlock()
|
||||
if mode, y := peer.fsm.rfMap[family]; y {
|
||||
@@ -170,21 +174,21 @@ func (peer *Peer) getAddPathMode(family bgp.RouteFamily) bgp.BGPAddPathMode {
|
||||
return bgp.BGP_ADD_PATH_NONE
|
||||
}
|
||||
|
||||
func (peer *Peer) isAddPathReceiveEnabled(family bgp.RouteFamily) bool {
|
||||
func (peer *peer) isAddPathReceiveEnabled(family bgp.RouteFamily) bool {
|
||||
return (peer.getAddPathMode(family) & bgp.BGP_ADD_PATH_RECEIVE) > 0
|
||||
}
|
||||
|
||||
func (peer *Peer) isAddPathSendEnabled(family bgp.RouteFamily) bool {
|
||||
func (peer *peer) isAddPathSendEnabled(family bgp.RouteFamily) bool {
|
||||
return (peer.getAddPathMode(family) & bgp.BGP_ADD_PATH_SEND) > 0
|
||||
}
|
||||
|
||||
func (peer *Peer) isDynamicNeighbor() bool {
|
||||
func (peer *peer) isDynamicNeighbor() bool {
|
||||
peer.fsm.lock.RLock()
|
||||
defer peer.fsm.lock.RUnlock()
|
||||
return peer.fsm.pConf.Config.NeighborAddress == "" && peer.fsm.pConf.Config.NeighborInterface == ""
|
||||
}
|
||||
|
||||
func (peer *Peer) recvedAllEOR() bool {
|
||||
func (peer *peer) recvedAllEOR() bool {
|
||||
peer.fsm.lock.RLock()
|
||||
defer peer.fsm.lock.RUnlock()
|
||||
for _, a := range peer.fsm.pConf.AfiSafis {
|
||||
@@ -195,14 +199,14 @@ func (peer *Peer) recvedAllEOR() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (peer *Peer) configuredRFlist() []bgp.RouteFamily {
|
||||
func (peer *peer) configuredRFlist() []bgp.RouteFamily {
|
||||
peer.fsm.lock.RLock()
|
||||
defer peer.fsm.lock.RUnlock()
|
||||
rfs, _ := config.AfiSafis(peer.fsm.pConf.AfiSafis).ToRfList()
|
||||
return rfs
|
||||
}
|
||||
|
||||
func (peer *Peer) negotiatedRFList() []bgp.RouteFamily {
|
||||
func (peer *peer) negotiatedRFList() []bgp.RouteFamily {
|
||||
peer.fsm.lock.RLock()
|
||||
defer peer.fsm.lock.RUnlock()
|
||||
l := make([]bgp.RouteFamily, 0, len(peer.fsm.rfMap))
|
||||
@@ -212,7 +216,8 @@ func (peer *Peer) negotiatedRFList() []bgp.RouteFamily {
|
||||
return l
|
||||
}
|
||||
|
||||
func (peer *Peer) toGlobalFamilies(families []bgp.RouteFamily) []bgp.RouteFamily {
|
||||
func (peer *peer) toGlobalFamilies(families []bgp.RouteFamily) []bgp.RouteFamily {
|
||||
id := peer.ID()
|
||||
peer.fsm.lock.RLock()
|
||||
defer peer.fsm.lock.RUnlock()
|
||||
if peer.fsm.pConf.Config.Vrf != "" {
|
||||
@@ -223,10 +228,14 @@ func (peer *Peer) toGlobalFamilies(families []bgp.RouteFamily) []bgp.RouteFamily
|
||||
fs = append(fs, bgp.RF_IPv4_VPN)
|
||||
case bgp.RF_IPv6_UC:
|
||||
fs = append(fs, bgp.RF_IPv6_VPN)
|
||||
case bgp.RF_FS_IPv4_UC:
|
||||
fs = append(fs, bgp.RF_FS_IPv4_VPN)
|
||||
case bgp.RF_FS_IPv6_UC:
|
||||
fs = append(fs, bgp.RF_FS_IPv6_VPN)
|
||||
default:
|
||||
log.WithFields(log.Fields{
|
||||
"Topic": "Peer",
|
||||
"Key": peer.ID(),
|
||||
"Key": id,
|
||||
"Family": f,
|
||||
"VRF": peer.fsm.pConf.Config.Vrf,
|
||||
}).Warn("invalid family configured for neighbor with vrf")
|
||||
@@ -256,7 +265,7 @@ func classifyFamilies(all, part []bgp.RouteFamily) ([]bgp.RouteFamily, []bgp.Rou
|
||||
return a, b
|
||||
}
|
||||
|
||||
func (peer *Peer) forwardingPreservedFamilies() ([]bgp.RouteFamily, []bgp.RouteFamily) {
|
||||
func (peer *peer) forwardingPreservedFamilies() ([]bgp.RouteFamily, []bgp.RouteFamily) {
|
||||
peer.fsm.lock.RLock()
|
||||
list := []bgp.RouteFamily{}
|
||||
for _, a := range peer.fsm.pConf.AfiSafis {
|
||||
@@ -268,7 +277,7 @@ func (peer *Peer) forwardingPreservedFamilies() ([]bgp.RouteFamily, []bgp.RouteF
|
||||
return classifyFamilies(peer.configuredRFlist(), list)
|
||||
}
|
||||
|
||||
func (peer *Peer) llgrFamilies() ([]bgp.RouteFamily, []bgp.RouteFamily) {
|
||||
func (peer *peer) llgrFamilies() ([]bgp.RouteFamily, []bgp.RouteFamily) {
|
||||
peer.fsm.lock.RLock()
|
||||
list := []bgp.RouteFamily{}
|
||||
for _, a := range peer.fsm.pConf.AfiSafis {
|
||||
@@ -280,7 +289,7 @@ func (peer *Peer) llgrFamilies() ([]bgp.RouteFamily, []bgp.RouteFamily) {
|
||||
return classifyFamilies(peer.configuredRFlist(), list)
|
||||
}
|
||||
|
||||
func (peer *Peer) isLLGREnabledFamily(family bgp.RouteFamily) bool {
|
||||
func (peer *peer) isLLGREnabledFamily(family bgp.RouteFamily) bool {
|
||||
peer.fsm.lock.RLock()
|
||||
llgrEnabled := peer.fsm.pConf.GracefulRestart.Config.LongLivedEnabled
|
||||
peer.fsm.lock.RUnlock()
|
||||
@@ -296,7 +305,7 @@ func (peer *Peer) isLLGREnabledFamily(family bgp.RouteFamily) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (peer *Peer) llgrRestartTime(family bgp.RouteFamily) uint32 {
|
||||
func (peer *peer) llgrRestartTime(family bgp.RouteFamily) uint32 {
|
||||
peer.fsm.lock.RLock()
|
||||
defer peer.fsm.lock.RUnlock()
|
||||
for _, a := range peer.fsm.pConf.AfiSafis {
|
||||
@@ -307,7 +316,7 @@ func (peer *Peer) llgrRestartTime(family bgp.RouteFamily) uint32 {
|
||||
return 0
|
||||
}
|
||||
|
||||
func (peer *Peer) llgrRestartTimerExpired(family bgp.RouteFamily) bool {
|
||||
func (peer *peer) llgrRestartTimerExpired(family bgp.RouteFamily) bool {
|
||||
peer.fsm.lock.RLock()
|
||||
defer peer.fsm.lock.RUnlock()
|
||||
all := true
|
||||
@@ -323,27 +332,11 @@ func (peer *Peer) llgrRestartTimerExpired(family bgp.RouteFamily) bool {
|
||||
return all
|
||||
}
|
||||
|
||||
func (peer *Peer) markLLGRStale(fs []bgp.RouteFamily) []*table.Path {
|
||||
paths := peer.adjRibIn.PathList(fs, true)
|
||||
for i, p := range paths {
|
||||
doStale := true
|
||||
for _, c := range p.GetCommunities() {
|
||||
if c == uint32(bgp.COMMUNITY_NO_LLGR) {
|
||||
doStale = false
|
||||
p = p.Clone(true)
|
||||
break
|
||||
}
|
||||
}
|
||||
if doStale {
|
||||
p = p.Clone(false)
|
||||
p.SetCommunities([]uint32{uint32(bgp.COMMUNITY_LLGR_STALE)}, false)
|
||||
}
|
||||
paths[i] = p
|
||||
}
|
||||
return paths
|
||||
func (peer *peer) markLLGRStale(fs []bgp.RouteFamily) []*table.Path {
|
||||
return peer.adjRibIn.MarkLLGRStaleOrDrop(fs)
|
||||
}
|
||||
|
||||
func (peer *Peer) stopPeerRestarting() {
|
||||
func (peer *peer) stopPeerRestarting() {
|
||||
peer.fsm.lock.Lock()
|
||||
defer peer.fsm.lock.Unlock()
|
||||
peer.fsm.pConf.GracefulRestart.State.PeerRestarting = false
|
||||
@@ -354,7 +347,7 @@ func (peer *Peer) stopPeerRestarting() {
|
||||
|
||||
}
|
||||
|
||||
func (peer *Peer) filterPathFromSourcePeer(path, old *table.Path) *table.Path {
|
||||
func (peer *peer) filterPathFromSourcePeer(path, old *table.Path) *table.Path {
|
||||
if peer.ID() != path.GetSource().Address.String() {
|
||||
return path
|
||||
}
|
||||
@@ -389,7 +382,7 @@ func (peer *Peer) filterPathFromSourcePeer(path, old *table.Path) *table.Path {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (peer *Peer) doPrefixLimit(k bgp.RouteFamily, c *config.PrefixLimitConfig) *bgp.BGPMessage {
|
||||
func (peer *peer) doPrefixLimit(k bgp.RouteFamily, c *config.PrefixLimitConfig) *bgp.BGPMessage {
|
||||
if maxPrefixes := int(c.MaxPrefixes); maxPrefixes > 0 {
|
||||
count := peer.adjRibIn.Count([]bgp.RouteFamily{k})
|
||||
pct := int(c.ShutdownThresholdPct)
|
||||
@@ -414,7 +407,7 @@ func (peer *Peer) doPrefixLimit(k bgp.RouteFamily, c *config.PrefixLimitConfig)
|
||||
|
||||
}
|
||||
|
||||
func (peer *Peer) updatePrefixLimitConfig(c []config.AfiSafi) error {
|
||||
func (peer *peer) updatePrefixLimitConfig(c []config.AfiSafi) error {
|
||||
peer.fsm.lock.RLock()
|
||||
x := peer.fsm.pConf.AfiSafis
|
||||
peer.fsm.lock.RUnlock()
|
||||
@@ -441,7 +434,7 @@ func (peer *Peer) updatePrefixLimitConfig(c []config.AfiSafi) error {
|
||||
}).Warnf("update prefix limit configuration")
|
||||
peer.prefixLimitWarned[e.State.Family] = false
|
||||
if msg := peer.doPrefixLimit(e.State.Family, &e.PrefixLimit.Config); msg != nil {
|
||||
sendFsmOutgoingMsg(peer, nil, msg, true)
|
||||
sendfsmOutgoingMsg(peer, nil, msg, true)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -451,7 +444,7 @@ func (peer *Peer) updatePrefixLimitConfig(c []config.AfiSafi) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (peer *Peer) handleUpdate(e *FsmMsg) ([]*table.Path, []bgp.RouteFamily, *bgp.BGPMessage) {
|
||||
func (peer *peer) handleUpdate(e *fsmMsg) ([]*table.Path, []bgp.RouteFamily, *bgp.BGPMessage) {
|
||||
m := e.MsgData.(*bgp.BGPMessage)
|
||||
update := m.Body.(*bgp.BGPUpdate)
|
||||
log.WithFields(log.Fields{
|
||||
@@ -488,15 +481,15 @@ func (peer *Peer) handleUpdate(e *FsmMsg) ([]*table.Path, []bgp.RouteFamily, *bg
|
||||
allowOwnAS := int(peer.fsm.pConf.AsPathOptions.Config.AllowOwnAs)
|
||||
peer.fsm.lock.RUnlock()
|
||||
if hasOwnASLoop(localAS, allowOwnAS, aspath) {
|
||||
path.SetAsLooped(true)
|
||||
path.SetRejected(true)
|
||||
continue
|
||||
}
|
||||
}
|
||||
// RFC4456 8. Avoiding Routing Information Loops
|
||||
// A router that recognizes the ORIGINATOR_ID attribute SHOULD
|
||||
// ignore a route received with its BGP Identifier as the ORIGINATOR_ID.
|
||||
peer.fsm.lock.RLock()
|
||||
isIBGPPeer := peer.isIBGPPeer()
|
||||
peer.fsm.lock.RLock()
|
||||
routerId := peer.fsm.gConf.Config.RouterId
|
||||
peer.fsm.lock.RUnlock()
|
||||
if isIBGPPeer {
|
||||
@@ -507,6 +500,7 @@ func (peer *Peer) handleUpdate(e *FsmMsg) ([]*table.Path, []bgp.RouteFamily, *bg
|
||||
"OriginatorID": id,
|
||||
"Data": path,
|
||||
}).Debug("Originator ID is mine, ignore")
|
||||
path.SetRejected(true)
|
||||
continue
|
||||
}
|
||||
}
|
||||
@@ -526,18 +520,18 @@ func (peer *Peer) handleUpdate(e *FsmMsg) ([]*table.Path, []bgp.RouteFamily, *bg
|
||||
return nil, nil, nil
|
||||
}
|
||||
|
||||
func (peer *Peer) startFSMHandler(incoming *channels.InfiniteChannel, stateCh chan *FsmMsg) {
|
||||
handler := NewFSMHandler(peer.fsm, incoming, stateCh, peer.outgoing)
|
||||
func (peer *peer) startFSMHandler() {
|
||||
handler := newFSMHandler(peer.fsm, peer.fsm.outgoingCh)
|
||||
peer.fsm.lock.Lock()
|
||||
peer.fsm.h = handler
|
||||
peer.fsm.lock.Unlock()
|
||||
}
|
||||
|
||||
func (peer *Peer) StaleAll(rfList []bgp.RouteFamily) []*table.Path {
|
||||
func (peer *peer) StaleAll(rfList []bgp.RouteFamily) []*table.Path {
|
||||
return peer.adjRibIn.StaleAll(rfList)
|
||||
}
|
||||
|
||||
func (peer *Peer) PassConn(conn *net.TCPConn) {
|
||||
func (peer *peer) PassConn(conn *net.TCPConn) {
|
||||
select {
|
||||
case peer.fsm.connCh <- conn:
|
||||
default:
|
||||
@@ -549,48 +543,6 @@ func (peer *Peer) PassConn(conn *net.TCPConn) {
|
||||
}
|
||||
}
|
||||
|
||||
func (peer *Peer) DropAll(rfList []bgp.RouteFamily) {
|
||||
peer.adjRibIn.Drop(rfList)
|
||||
}
|
||||
|
||||
func (peer *Peer) stopFSM() error {
|
||||
failed := false
|
||||
peer.fsm.lock.RLock()
|
||||
addr := peer.fsm.pConf.State.NeighborAddress
|
||||
peer.fsm.lock.RUnlock()
|
||||
t1 := time.AfterFunc(time.Minute*5, func() {
|
||||
log.WithFields(log.Fields{
|
||||
"Topic": "Peer",
|
||||
}).Warnf("Failed to free the fsm.h.t for %s", addr)
|
||||
failed = true
|
||||
})
|
||||
|
||||
peer.fsm.h.t.Kill(nil)
|
||||
peer.fsm.h.t.Wait()
|
||||
t1.Stop()
|
||||
if !failed {
|
||||
log.WithFields(log.Fields{
|
||||
"Topic": "Peer",
|
||||
"Key": addr,
|
||||
}).Debug("freed fsm.h.t")
|
||||
cleanInfiniteChannel(peer.outgoing)
|
||||
}
|
||||
failed = false
|
||||
t2 := time.AfterFunc(time.Minute*5, func() {
|
||||
log.WithFields(log.Fields{
|
||||
"Topic": "Peer",
|
||||
}).Warnf("Failed to free the fsm.t for %s", addr)
|
||||
failed = true
|
||||
})
|
||||
peer.fsm.t.Kill(nil)
|
||||
peer.fsm.t.Wait()
|
||||
t2.Stop()
|
||||
if !failed {
|
||||
log.WithFields(log.Fields{
|
||||
"Topic": "Peer",
|
||||
"Key": addr,
|
||||
}).Debug("freed fsm.t")
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("Failed to free FSM for %s", addr)
|
||||
func (peer *peer) DropAll(rfList []bgp.RouteFamily) []*table.Path {
|
||||
return peer.adjRibIn.Drop(rfList)
|
||||
}
|
||||
|
||||
384
vendor/github.com/osrg/gobgp/pkg/server/rpki.go
generated
vendored
384
vendor/github.com/osrg/gobgp/pkg/server/rpki.go
generated
vendored
@@ -20,7 +20,6 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"sort"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
@@ -29,99 +28,51 @@ import (
|
||||
"github.com/osrg/gobgp/pkg/packet/bgp"
|
||||
"github.com/osrg/gobgp/pkg/packet/rtr"
|
||||
|
||||
"github.com/armon/go-radix"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
const (
|
||||
CONNECT_RETRY_INTERVAL = 30
|
||||
connectRetryInterval = 30
|
||||
)
|
||||
|
||||
func before(a, b uint32) bool {
|
||||
return int32(a-b) < 0
|
||||
}
|
||||
|
||||
type RoaBucket struct {
|
||||
Prefix *table.IPPrefix
|
||||
entries []*table.ROA
|
||||
}
|
||||
|
||||
func (r *RoaBucket) GetEntries() []*table.ROA {
|
||||
return r.entries
|
||||
}
|
||||
|
||||
type roas []*table.ROA
|
||||
|
||||
func (r roas) Len() int {
|
||||
return len(r)
|
||||
}
|
||||
|
||||
func (r roas) Swap(i, j int) {
|
||||
r[i], r[j] = r[j], r[i]
|
||||
}
|
||||
|
||||
func (r roas) Less(i, j int) bool {
|
||||
r1 := r[i]
|
||||
r2 := r[j]
|
||||
|
||||
if r1.MaxLen < r2.MaxLen {
|
||||
return true
|
||||
} else if r1.MaxLen > r2.MaxLen {
|
||||
return false
|
||||
}
|
||||
|
||||
if r1.AS < r2.AS {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
type ROAEventType uint8
|
||||
type roaEventType uint8
|
||||
|
||||
const (
|
||||
CONNECTED ROAEventType = iota
|
||||
DISCONNECTED
|
||||
RTR
|
||||
LIFETIMEOUT
|
||||
roaConnected roaEventType = iota
|
||||
roaDisconnected
|
||||
roaRTR
|
||||
roaLifetimeout
|
||||
)
|
||||
|
||||
type ROAEvent struct {
|
||||
EventType ROAEventType
|
||||
type roaEvent struct {
|
||||
EventType roaEventType
|
||||
Src string
|
||||
Data []byte
|
||||
conn *net.TCPConn
|
||||
}
|
||||
|
||||
type roaManager struct {
|
||||
AS uint32
|
||||
Roas map[bgp.RouteFamily]*radix.Tree
|
||||
eventCh chan *ROAEvent
|
||||
eventCh chan *roaEvent
|
||||
clientMap map[string]*roaClient
|
||||
table *table.ROATable
|
||||
}
|
||||
|
||||
func NewROAManager(as uint32) (*roaManager, error) {
|
||||
func newROAManager(table *table.ROATable) *roaManager {
|
||||
m := &roaManager{
|
||||
AS: as,
|
||||
Roas: make(map[bgp.RouteFamily]*radix.Tree),
|
||||
eventCh: make(chan *roaEvent),
|
||||
clientMap: make(map[string]*roaClient),
|
||||
table: table,
|
||||
}
|
||||
m.Roas[bgp.RF_IPv4_UC] = radix.New()
|
||||
m.Roas[bgp.RF_IPv6_UC] = radix.New()
|
||||
m.eventCh = make(chan *ROAEvent)
|
||||
m.clientMap = make(map[string]*roaClient)
|
||||
return m, nil
|
||||
return m
|
||||
}
|
||||
|
||||
func (c *roaManager) enabled() bool {
|
||||
return len(c.clientMap) != 0
|
||||
}
|
||||
|
||||
func (m *roaManager) SetAS(as uint32) error {
|
||||
if m.AS != 0 {
|
||||
return fmt.Errorf("AS was already configured")
|
||||
}
|
||||
m.AS = as
|
||||
return nil
|
||||
func (m *roaManager) enabled() bool {
|
||||
return len(m.clientMap) != 0
|
||||
}
|
||||
|
||||
func (m *roaManager) AddServer(host string, lifetime int64) error {
|
||||
@@ -135,7 +86,7 @@ func (m *roaManager) AddServer(host string, lifetime int64) error {
|
||||
if _, ok := m.clientMap[host]; ok {
|
||||
return fmt.Errorf("ROA server exists %s", host)
|
||||
}
|
||||
m.clientMap[host] = NewRoaClient(address, port, m.eventCh, lifetime)
|
||||
m.clientMap[host] = newRoaClient(address, port, m.eventCh, lifetime)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -145,35 +96,11 @@ func (m *roaManager) DeleteServer(host string) error {
|
||||
return fmt.Errorf("ROA server doesn't exists %s", host)
|
||||
}
|
||||
client.stop()
|
||||
m.deleteAllROA(host)
|
||||
m.table.DeleteAll(host)
|
||||
delete(m.clientMap, host)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *roaManager) deleteAllROA(network string) {
|
||||
for _, tree := range m.Roas {
|
||||
deleteKeys := make([]string, 0, tree.Len())
|
||||
tree.Walk(func(s string, v interface{}) bool {
|
||||
b, _ := v.(*RoaBucket)
|
||||
newEntries := make([]*table.ROA, 0, len(b.entries))
|
||||
for _, r := range b.entries {
|
||||
if r.Src != network {
|
||||
newEntries = append(newEntries, r)
|
||||
}
|
||||
}
|
||||
if len(newEntries) > 0 {
|
||||
b.entries = newEntries
|
||||
} else {
|
||||
deleteKeys = append(deleteKeys, s)
|
||||
}
|
||||
return false
|
||||
})
|
||||
for _, key := range deleteKeys {
|
||||
tree.Delete(key)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (m *roaManager) Enable(address string) error {
|
||||
for network, client := range m.clientMap {
|
||||
add, _, _ := net.SplitHostPort(network)
|
||||
@@ -190,7 +117,7 @@ func (m *roaManager) Disable(address string) error {
|
||||
add, _, _ := net.SplitHostPort(network)
|
||||
if add == address {
|
||||
client.reset()
|
||||
m.deleteAllROA(add)
|
||||
m.table.DeleteAll(add)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
@@ -206,35 +133,35 @@ func (m *roaManager) SoftReset(address string) error {
|
||||
add, _, _ := net.SplitHostPort(network)
|
||||
if add == address {
|
||||
client.softReset()
|
||||
m.deleteAllROA(network)
|
||||
m.table.DeleteAll(network)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return fmt.Errorf("ROA server not found %s", address)
|
||||
}
|
||||
|
||||
func (c *roaManager) ReceiveROA() chan *ROAEvent {
|
||||
return c.eventCh
|
||||
func (m *roaManager) ReceiveROA() chan *roaEvent {
|
||||
return m.eventCh
|
||||
}
|
||||
|
||||
func (c *roaClient) lifetimeout() {
|
||||
c.eventCh <- &ROAEvent{
|
||||
EventType: LIFETIMEOUT,
|
||||
c.eventCh <- &roaEvent{
|
||||
EventType: roaLifetimeout,
|
||||
Src: c.host,
|
||||
}
|
||||
}
|
||||
|
||||
func (m *roaManager) HandleROAEvent(ev *ROAEvent) {
|
||||
func (m *roaManager) HandleROAEvent(ev *roaEvent) {
|
||||
client, y := m.clientMap[ev.Src]
|
||||
if !y {
|
||||
if ev.EventType == CONNECTED {
|
||||
if ev.EventType == roaConnected {
|
||||
ev.conn.Close()
|
||||
}
|
||||
log.WithFields(log.Fields{"Topic": "rpki"}).Errorf("Can't find %s ROA server configuration", ev.Src)
|
||||
return
|
||||
}
|
||||
switch ev.EventType {
|
||||
case DISCONNECTED:
|
||||
case roaDisconnected:
|
||||
log.WithFields(log.Fields{"Topic": "rpki"}).Infof("ROA server %s is disconnected", ev.Src)
|
||||
client.state.Downtime = time.Now().Unix()
|
||||
// clear state
|
||||
@@ -245,14 +172,14 @@ func (m *roaManager) HandleROAEvent(ev *ROAEvent) {
|
||||
go client.tryConnect()
|
||||
client.timer = time.AfterFunc(time.Duration(client.lifetime)*time.Second, client.lifetimeout)
|
||||
client.oldSessionID = client.sessionID
|
||||
case CONNECTED:
|
||||
case roaConnected:
|
||||
log.WithFields(log.Fields{"Topic": "rpki"}).Infof("ROA server %s is connected", ev.Src)
|
||||
client.conn = ev.conn
|
||||
client.state.Uptime = time.Now().Unix()
|
||||
go client.established()
|
||||
case RTR:
|
||||
case roaRTR:
|
||||
m.handleRTRMsg(client, &client.state, ev.Data)
|
||||
case LIFETIMEOUT:
|
||||
case roaLifetimeout:
|
||||
// a) already reconnected but hasn't received
|
||||
// EndOfData -> needs to delete stale ROAs
|
||||
// b) not reconnected -> needs to delete stale ROAs
|
||||
@@ -264,83 +191,17 @@ func (m *roaManager) HandleROAEvent(ev *ROAEvent) {
|
||||
log.WithFields(log.Fields{"Topic": "rpki"}).Infof("Reconnected to %s. Ignore timeout", client.host)
|
||||
} else {
|
||||
log.WithFields(log.Fields{"Topic": "rpki"}).Infof("Deleting all ROAs due to timeout with:%s", client.host)
|
||||
m.deleteAllROA(client.host)
|
||||
m.table.DeleteAll(client.host)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (m *roaManager) roa2tree(roa *table.ROA) (*radix.Tree, string) {
|
||||
tree := m.Roas[bgp.RF_IPv4_UC]
|
||||
if roa.Family == bgp.AFI_IP6 {
|
||||
tree = m.Roas[bgp.RF_IPv6_UC]
|
||||
}
|
||||
return tree, table.IpToRadixkey(roa.Prefix.Prefix, roa.Prefix.Length)
|
||||
}
|
||||
|
||||
func (m *roaManager) deleteROA(roa *table.ROA) {
|
||||
tree, key := m.roa2tree(roa)
|
||||
b, _ := tree.Get(key)
|
||||
if b != nil {
|
||||
bucket := b.(*RoaBucket)
|
||||
newEntries := make([]*table.ROA, 0, len(bucket.entries))
|
||||
for _, r := range bucket.entries {
|
||||
if !r.Equal(roa) {
|
||||
newEntries = append(newEntries, r)
|
||||
}
|
||||
}
|
||||
if len(newEntries) != len(bucket.entries) {
|
||||
bucket.entries = newEntries
|
||||
if len(newEntries) == 0 {
|
||||
tree.Delete(key)
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
log.WithFields(log.Fields{
|
||||
"Topic": "rpki",
|
||||
"Prefix": roa.Prefix.Prefix.String(),
|
||||
"Prefix Length": roa.Prefix.Length,
|
||||
"AS": roa.AS,
|
||||
"Max Length": roa.MaxLen,
|
||||
}).Info("Can't withdraw a ROA")
|
||||
}
|
||||
|
||||
func (m *roaManager) DeleteROA(roa *table.ROA) {
|
||||
m.deleteROA(roa)
|
||||
}
|
||||
|
||||
func (m *roaManager) addROA(roa *table.ROA) {
|
||||
tree, key := m.roa2tree(roa)
|
||||
b, _ := tree.Get(key)
|
||||
var bucket *RoaBucket
|
||||
if b == nil {
|
||||
bucket = &RoaBucket{
|
||||
Prefix: roa.Prefix,
|
||||
entries: make([]*table.ROA, 0),
|
||||
}
|
||||
tree.Insert(key, bucket)
|
||||
} else {
|
||||
bucket = b.(*RoaBucket)
|
||||
for _, r := range bucket.entries {
|
||||
if r.Equal(roa) {
|
||||
// we already have the same one
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
bucket.entries = append(bucket.entries, roa)
|
||||
}
|
||||
|
||||
func (m *roaManager) AddROA(roa *table.ROA) {
|
||||
m.addROA(roa)
|
||||
}
|
||||
|
||||
func (c *roaManager) handleRTRMsg(client *roaClient, state *config.RpkiServerState, buf []byte) {
|
||||
func (m *roaManager) handleRTRMsg(client *roaClient, state *config.RpkiServerState, buf []byte) {
|
||||
received := &state.RpkiMessages.RpkiReceived
|
||||
|
||||
m, err := rtr.ParseRTR(buf)
|
||||
m1, err := rtr.ParseRTR(buf)
|
||||
if err == nil {
|
||||
switch msg := m.(type) {
|
||||
switch msg := m1.(type) {
|
||||
case *rtr.RTRSerialNotify:
|
||||
if before(client.serialNumber, msg.RTRCommon.SerialNumber) {
|
||||
client.enable(client.serialNumber)
|
||||
@@ -367,19 +228,19 @@ func (c *roaManager) handleRTRMsg(client *roaClient, state *config.RpkiServerSta
|
||||
roa := table.NewROA(family, msg.Prefix, msg.PrefixLen, msg.MaxLen, msg.AS, client.host)
|
||||
if (msg.Flags & 1) == 1 {
|
||||
if client.endOfData {
|
||||
c.addROA(roa)
|
||||
m.table.Add(roa)
|
||||
} else {
|
||||
client.pendingROAs = append(client.pendingROAs, roa)
|
||||
}
|
||||
} else {
|
||||
c.deleteROA(roa)
|
||||
m.table.Delete(roa)
|
||||
}
|
||||
case *rtr.RTREndOfData:
|
||||
received.EndOfData++
|
||||
if client.sessionID != msg.RTRCommon.SessionID {
|
||||
// remove all ROAs related with the
|
||||
// previous session
|
||||
c.deleteAllROA(client.host)
|
||||
m.table.DeleteAll(client.host)
|
||||
}
|
||||
client.sessionID = msg.RTRCommon.SessionID
|
||||
client.serialNumber = msg.RTRCommon.SerialNumber
|
||||
@@ -389,7 +250,7 @@ func (c *roaManager) handleRTRMsg(client *roaClient, state *config.RpkiServerSta
|
||||
client.timer = nil
|
||||
}
|
||||
for _, roa := range client.pendingROAs {
|
||||
c.addROA(roa)
|
||||
m.table.Add(roa)
|
||||
}
|
||||
client.pendingROAs = make([]*table.ROA, 0)
|
||||
case *rtr.RTRCacheReset:
|
||||
@@ -407,34 +268,12 @@ func (c *roaManager) handleRTRMsg(client *roaClient, state *config.RpkiServerSta
|
||||
}
|
||||
}
|
||||
|
||||
func (c *roaManager) GetServers() []*config.RpkiServer {
|
||||
f := func(tree *radix.Tree) (map[string]uint32, map[string]uint32) {
|
||||
records := make(map[string]uint32)
|
||||
prefixes := make(map[string]uint32)
|
||||
func (m *roaManager) GetServers() []*config.RpkiServer {
|
||||
recordsV4, prefixesV4 := m.table.Info(bgp.RF_IPv4_UC)
|
||||
recordsV6, prefixesV6 := m.table.Info(bgp.RF_IPv6_UC)
|
||||
|
||||
tree.Walk(func(s string, v interface{}) bool {
|
||||
b, _ := v.(*RoaBucket)
|
||||
tmpRecords := make(map[string]uint32)
|
||||
for _, roa := range b.entries {
|
||||
tmpRecords[roa.Src]++
|
||||
}
|
||||
|
||||
for src, r := range tmpRecords {
|
||||
if r > 0 {
|
||||
records[src] += r
|
||||
prefixes[src]++
|
||||
}
|
||||
}
|
||||
return false
|
||||
})
|
||||
return records, prefixes
|
||||
}
|
||||
|
||||
recordsV4, prefixesV4 := f(c.Roas[bgp.RF_IPv4_UC])
|
||||
recordsV6, prefixesV6 := f(c.Roas[bgp.RF_IPv6_UC])
|
||||
|
||||
l := make([]*config.RpkiServer, 0, len(c.clientMap))
|
||||
for _, client := range c.clientMap {
|
||||
l := make([]*config.RpkiServer, 0, len(m.clientMap))
|
||||
for _, client := range m.clientMap {
|
||||
state := &client.state
|
||||
|
||||
if client.conn == nil {
|
||||
@@ -468,128 +307,11 @@ func (c *roaManager) GetServers() []*config.RpkiServer {
|
||||
return l
|
||||
}
|
||||
|
||||
func (c *roaManager) GetRoa(family bgp.RouteFamily) ([]*table.ROA, error) {
|
||||
if len(c.clientMap) == 0 {
|
||||
return []*table.ROA{}, fmt.Errorf("RPKI server isn't configured.")
|
||||
}
|
||||
var rfList []bgp.RouteFamily
|
||||
switch family {
|
||||
case bgp.RF_IPv4_UC:
|
||||
rfList = []bgp.RouteFamily{bgp.RF_IPv4_UC}
|
||||
case bgp.RF_IPv6_UC:
|
||||
rfList = []bgp.RouteFamily{bgp.RF_IPv6_UC}
|
||||
default:
|
||||
rfList = []bgp.RouteFamily{bgp.RF_IPv4_UC, bgp.RF_IPv6_UC}
|
||||
}
|
||||
l := make([]*table.ROA, 0)
|
||||
for _, rf := range rfList {
|
||||
if tree, ok := c.Roas[rf]; ok {
|
||||
tree.Walk(func(s string, v interface{}) bool {
|
||||
b, _ := v.(*RoaBucket)
|
||||
var roaList roas
|
||||
for _, r := range b.entries {
|
||||
roaList = append(roaList, r)
|
||||
}
|
||||
sort.Sort(roaList)
|
||||
for _, roa := range roaList {
|
||||
l = append(l, roa)
|
||||
}
|
||||
return false
|
||||
})
|
||||
}
|
||||
}
|
||||
return l, nil
|
||||
}
|
||||
|
||||
func ValidatePath(ownAs uint32, tree *radix.Tree, cidr string, asPath *bgp.PathAttributeAsPath) *table.Validation {
|
||||
var as uint32
|
||||
|
||||
validation := &table.Validation{
|
||||
Status: config.RPKI_VALIDATION_RESULT_TYPE_NOT_FOUND,
|
||||
Reason: table.RPKI_VALIDATION_REASON_TYPE_NONE,
|
||||
Matched: make([]*table.ROA, 0),
|
||||
UnmatchedLength: make([]*table.ROA, 0),
|
||||
UnmatchedAs: make([]*table.ROA, 0),
|
||||
}
|
||||
|
||||
if asPath == nil || len(asPath.Value) == 0 {
|
||||
as = ownAs
|
||||
} else {
|
||||
param := asPath.Value[len(asPath.Value)-1]
|
||||
switch param.GetType() {
|
||||
case bgp.BGP_ASPATH_ATTR_TYPE_SEQ:
|
||||
asList := param.GetAS()
|
||||
if len(asList) == 0 {
|
||||
as = ownAs
|
||||
} else {
|
||||
as = asList[len(asList)-1]
|
||||
}
|
||||
case bgp.BGP_ASPATH_ATTR_TYPE_CONFED_SET, bgp.BGP_ASPATH_ATTR_TYPE_CONFED_SEQ:
|
||||
as = ownAs
|
||||
default:
|
||||
return validation
|
||||
}
|
||||
}
|
||||
_, n, _ := net.ParseCIDR(cidr)
|
||||
ones, _ := n.Mask.Size()
|
||||
prefixLen := uint8(ones)
|
||||
key := table.IpToRadixkey(n.IP, prefixLen)
|
||||
_, b, _ := tree.LongestPrefix(key)
|
||||
if b == nil {
|
||||
return validation
|
||||
}
|
||||
|
||||
var bucket *RoaBucket
|
||||
fn := radix.WalkFn(func(k string, v interface{}) bool {
|
||||
bucket, _ = v.(*RoaBucket)
|
||||
for _, r := range bucket.entries {
|
||||
if prefixLen <= r.MaxLen {
|
||||
if r.AS != 0 && r.AS == as {
|
||||
validation.Matched = append(validation.Matched, r)
|
||||
} else {
|
||||
validation.UnmatchedAs = append(validation.UnmatchedAs, r)
|
||||
}
|
||||
} else {
|
||||
validation.UnmatchedLength = append(validation.UnmatchedLength, r)
|
||||
}
|
||||
}
|
||||
return false
|
||||
})
|
||||
tree.WalkPath(key, fn)
|
||||
|
||||
if len(validation.Matched) != 0 {
|
||||
validation.Status = config.RPKI_VALIDATION_RESULT_TYPE_VALID
|
||||
validation.Reason = table.RPKI_VALIDATION_REASON_TYPE_NONE
|
||||
} else if len(validation.UnmatchedAs) != 0 {
|
||||
validation.Status = config.RPKI_VALIDATION_RESULT_TYPE_INVALID
|
||||
validation.Reason = table.RPKI_VALIDATION_REASON_TYPE_AS
|
||||
} else if len(validation.UnmatchedLength) != 0 {
|
||||
validation.Status = config.RPKI_VALIDATION_RESULT_TYPE_INVALID
|
||||
validation.Reason = table.RPKI_VALIDATION_REASON_TYPE_LENGTH
|
||||
} else {
|
||||
validation.Status = config.RPKI_VALIDATION_RESULT_TYPE_NOT_FOUND
|
||||
validation.Reason = table.RPKI_VALIDATION_REASON_TYPE_NONE
|
||||
}
|
||||
|
||||
return validation
|
||||
}
|
||||
|
||||
func (c *roaManager) validate(path *table.Path) *table.Validation {
|
||||
if len(c.clientMap) == 0 || path.IsWithdraw || path.IsEOR() {
|
||||
// RPKI isn't enabled or invalid path
|
||||
return nil
|
||||
}
|
||||
if tree, ok := c.Roas[path.GetRouteFamily()]; ok {
|
||||
return ValidatePath(c.AS, tree, path.GetNlri().String(), path.GetAsPath())
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type roaClient struct {
|
||||
host string
|
||||
conn *net.TCPConn
|
||||
state config.RpkiServerState
|
||||
eventCh chan *ROAEvent
|
||||
eventCh chan *roaEvent
|
||||
sessionID uint16
|
||||
oldSessionID uint16
|
||||
serialNumber uint32
|
||||
@@ -601,7 +323,7 @@ type roaClient struct {
|
||||
ctx context.Context
|
||||
}
|
||||
|
||||
func NewRoaClient(address, port string, ch chan *ROAEvent, lifetime int64) *roaClient {
|
||||
func newRoaClient(address, port string, ch chan *roaEvent, lifetime int64) *roaClient {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
c := &roaClient{
|
||||
host: net.JoinHostPort(address, port),
|
||||
@@ -663,10 +385,10 @@ func (c *roaClient) tryConnect() {
|
||||
}
|
||||
if conn, err := net.Dial("tcp", c.host); err != nil {
|
||||
// better to use context with timeout
|
||||
time.Sleep(CONNECT_RETRY_INTERVAL * time.Second)
|
||||
time.Sleep(connectRetryInterval * time.Second)
|
||||
} else {
|
||||
c.eventCh <- &ROAEvent{
|
||||
EventType: CONNECTED,
|
||||
c.eventCh <- &roaEvent{
|
||||
EventType: roaConnected,
|
||||
Src: c.host,
|
||||
conn: conn.(*net.TCPConn),
|
||||
}
|
||||
@@ -678,8 +400,8 @@ func (c *roaClient) tryConnect() {
|
||||
func (c *roaClient) established() (err error) {
|
||||
defer func() {
|
||||
c.conn.Close()
|
||||
c.eventCh <- &ROAEvent{
|
||||
EventType: DISCONNECTED,
|
||||
c.eventCh <- &roaEvent{
|
||||
EventType: roaDisconnected,
|
||||
Src: c.host,
|
||||
}
|
||||
}()
|
||||
@@ -703,8 +425,8 @@ func (c *roaClient) established() (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
c.eventCh <- &ROAEvent{
|
||||
EventType: RTR,
|
||||
c.eventCh <- &roaEvent{
|
||||
EventType: roaRTR,
|
||||
Src: c.host,
|
||||
Data: append(header, body...),
|
||||
}
|
||||
|
||||
1854
vendor/github.com/osrg/gobgp/pkg/server/server.go
generated
vendored
1854
vendor/github.com/osrg/gobgp/pkg/server/server.go
generated
vendored
File diff suppressed because it is too large
Load Diff
54
vendor/github.com/osrg/gobgp/pkg/server/sockopt.go
generated
vendored
54
vendor/github.com/osrg/gobgp/pkg/server/sockopt.go
generated
vendored
@@ -17,74 +17,46 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"syscall"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func SetTcpMD5SigSockopt(l *net.TCPListener, address string, key string) error {
|
||||
func setTCPMD5SigSockopt(l *net.TCPListener, address string, key string) error {
|
||||
return setTcpMD5SigSockopt(l, address, key)
|
||||
}
|
||||
|
||||
func SetListenTcpTTLSockopt(l *net.TCPListener, ttl int) error {
|
||||
func setListenTCPTTLSockopt(l *net.TCPListener, ttl int) error {
|
||||
return setListenTcpTTLSockopt(l, ttl)
|
||||
}
|
||||
|
||||
func SetTcpTTLSockopt(conn *net.TCPConn, ttl int) error {
|
||||
func setTCPTTLSockopt(conn *net.TCPConn, ttl int) error {
|
||||
return setTcpTTLSockopt(conn, ttl)
|
||||
}
|
||||
|
||||
func SetTcpMinTTLSockopt(conn *net.TCPConn, ttl int) error {
|
||||
func setTCPMinTTLSockopt(conn *net.TCPConn, ttl int) error {
|
||||
return setTcpMinTTLSockopt(conn, ttl)
|
||||
}
|
||||
|
||||
type TCPDialer struct {
|
||||
net.Dialer
|
||||
|
||||
// MD5 authentication password.
|
||||
AuthPassword string
|
||||
|
||||
// The TTL value to set outgoing connection.
|
||||
Ttl uint8
|
||||
|
||||
// The minimum TTL value for incoming packets.
|
||||
TtlMin uint8
|
||||
}
|
||||
|
||||
func (d *TCPDialer) DialTCP(addr string, port int) (*net.TCPConn, error) {
|
||||
if d.AuthPassword != "" {
|
||||
func dialerControl(network, address string, c syscall.RawConn, ttl, ttlMin uint8, password string, bindInterface string) error {
|
||||
if password != "" {
|
||||
log.WithFields(log.Fields{
|
||||
"Topic": "Peer",
|
||||
"Key": addr,
|
||||
"Key": address,
|
||||
}).Warn("setting md5 for active connection is not supported")
|
||||
}
|
||||
if d.Ttl != 0 {
|
||||
if ttl != 0 {
|
||||
log.WithFields(log.Fields{
|
||||
"Topic": "Peer",
|
||||
"Key": addr,
|
||||
"Key": address,
|
||||
}).Warn("setting ttl for active connection is not supported")
|
||||
}
|
||||
if d.TtlMin != 0 {
|
||||
if ttlMin != 0 {
|
||||
log.WithFields(log.Fields{
|
||||
"Topic": "Peer",
|
||||
"Key": addr,
|
||||
"Key": address,
|
||||
}).Warn("setting min ttl for active connection is not supported")
|
||||
}
|
||||
|
||||
raddr, err := net.ResolveTCPAddr("tcp", net.JoinHostPort(addr, fmt.Sprintf("%d", port)))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid remote address: %s", err)
|
||||
}
|
||||
laddr, err := net.ResolveTCPAddr("tcp", d.LocalAddr.String())
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid local address: %s", err)
|
||||
}
|
||||
|
||||
dialer := net.Dialer{LocalAddr: laddr, Timeout: d.Timeout}
|
||||
conn, err := dialer.Dial("tcp", raddr.String())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return conn.(*net.TCPConn), nil
|
||||
return nil
|
||||
}
|
||||
|
||||
8
vendor/github.com/osrg/gobgp/pkg/server/sockopt_bsd.go
generated
vendored
8
vendor/github.com/osrg/gobgp/pkg/server/sockopt_bsd.go
generated
vendored
@@ -22,8 +22,8 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
TCP_MD5SIG = 0x10 // TCP MD5 Signature (RFC2385)
|
||||
IPV6_MINHOPCOUNT = 73 // Generalized TTL Security Mechanism (RFC5082)
|
||||
tcpMD5SIG = 0x10 // TCP MD5 Signature (RFC2385)
|
||||
ipv6MinHopCount = 73 // Generalized TTL Security Mechanism (RFC5082)
|
||||
)
|
||||
|
||||
func setTcpMD5SigSockopt(l *net.TCPListener, address string, key string) error {
|
||||
@@ -32,7 +32,7 @@ func setTcpMD5SigSockopt(l *net.TCPListener, address string, key string) error {
|
||||
return err
|
||||
}
|
||||
// always enable and assumes that the configuration is done by setkey()
|
||||
return setsockOptInt(sc, syscall.IPPROTO_TCP, TCP_MD5SIG, 1)
|
||||
return setsockOptInt(sc, syscall.IPPROTO_TCP, tcpMD5SIG, 1)
|
||||
}
|
||||
|
||||
func setListenTcpTTLSockopt(l *net.TCPListener, ttl int) error {
|
||||
@@ -63,7 +63,7 @@ func setTcpMinTTLSockopt(conn *net.TCPConn, ttl int) error {
|
||||
name := syscall.IP_MINTTL
|
||||
if family == syscall.AF_INET6 {
|
||||
level = syscall.IPPROTO_IPV6
|
||||
name = IPV6_MINHOPCOUNT
|
||||
name = ipv6MinHopCount
|
||||
}
|
||||
return setsockOptInt(sc, level, name, ttl)
|
||||
}
|
||||
|
||||
252
vendor/github.com/osrg/gobgp/pkg/server/sockopt_linux.go
generated
vendored
252
vendor/github.com/osrg/gobgp/pkg/server/sockopt_linux.go
generated
vendored
@@ -17,7 +17,6 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"os"
|
||||
"syscall"
|
||||
@@ -25,8 +24,8 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
TCP_MD5SIG = 14 // TCP MD5 Signature (RFC2385)
|
||||
IPV6_MINHOPCOUNT = 73 // Generalized TTL Security Mechanism (RFC5082)
|
||||
tcpMD5SIG = 14 // TCP MD5 Signature (RFC2385)
|
||||
ipv6MinHopCount = 73 // Generalized TTL Security Mechanism (RFC5082)
|
||||
)
|
||||
|
||||
type tcpmd5sig struct {
|
||||
@@ -40,7 +39,7 @@ type tcpmd5sig struct {
|
||||
key [80]byte
|
||||
}
|
||||
|
||||
func buildTcpMD5Sig(address string, key string) (tcpmd5sig, error) {
|
||||
func buildTcpMD5Sig(address, key string) (tcpmd5sig, error) {
|
||||
t := tcpmd5sig{}
|
||||
addr := net.ParseIP(address)
|
||||
if addr.To4() != nil {
|
||||
@@ -57,7 +56,7 @@ func buildTcpMD5Sig(address string, key string) (tcpmd5sig, error) {
|
||||
return t, nil
|
||||
}
|
||||
|
||||
func SetTcpMD5SigSockopt(l *net.TCPListener, address string, key string) error {
|
||||
func setTCPMD5SigSockopt(l *net.TCPListener, address string, key string) error {
|
||||
t, err := buildTcpMD5Sig(address, key)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -68,10 +67,10 @@ func SetTcpMD5SigSockopt(l *net.TCPListener, address string, key string) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return setsockOptString(sc, syscall.IPPROTO_TCP, TCP_MD5SIG, string(b[:]))
|
||||
return setsockOptString(sc, syscall.IPPROTO_TCP, tcpMD5SIG, string(b[:]))
|
||||
}
|
||||
|
||||
func SetListenTcpTTLSockopt(l *net.TCPListener, ttl int) error {
|
||||
func setListenTCPTTLSockopt(l *net.TCPListener, ttl int) error {
|
||||
family := extractFamilyFromTCPListener(l)
|
||||
sc, err := l.SyscallConn()
|
||||
if err != nil {
|
||||
@@ -80,7 +79,7 @@ func SetListenTcpTTLSockopt(l *net.TCPListener, ttl int) error {
|
||||
return setsockoptIpTtl(sc, family, ttl)
|
||||
}
|
||||
|
||||
func SetTcpTTLSockopt(conn *net.TCPConn, ttl int) error {
|
||||
func setTCPTTLSockopt(conn *net.TCPConn, ttl int) error {
|
||||
family := extractFamilyFromTCPConn(conn)
|
||||
sc, err := conn.SyscallConn()
|
||||
if err != nil {
|
||||
@@ -89,7 +88,7 @@ func SetTcpTTLSockopt(conn *net.TCPConn, ttl int) error {
|
||||
return setsockoptIpTtl(sc, family, ttl)
|
||||
}
|
||||
|
||||
func SetTcpMinTTLSockopt(conn *net.TCPConn, ttl int) error {
|
||||
func setTCPMinTTLSockopt(conn *net.TCPConn, ttl int) error {
|
||||
family := extractFamilyFromTCPConn(conn)
|
||||
sc, err := conn.SyscallConn()
|
||||
if err != nil {
|
||||
@@ -99,191 +98,78 @@ func SetTcpMinTTLSockopt(conn *net.TCPConn, ttl int) error {
|
||||
name := syscall.IP_MINTTL
|
||||
if family == syscall.AF_INET6 {
|
||||
level = syscall.IPPROTO_IPV6
|
||||
name = IPV6_MINHOPCOUNT
|
||||
name = ipv6MinHopCount
|
||||
}
|
||||
return setsockOptInt(sc, level, name, ttl)
|
||||
}
|
||||
|
||||
func setsockoptTcpMD5Sig(fd int, address string, key string) error {
|
||||
t, err := buildTcpMD5Sig(address, key)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
b := *(*[unsafe.Sizeof(t)]byte)(unsafe.Pointer(&t))
|
||||
return os.NewSyscallError("setsockopt", syscall.SetsockoptString(fd, syscall.IPPROTO_TCP, TCP_MD5SIG, string(b[:])))
|
||||
}
|
||||
|
||||
func setsockoptIpTtl2(fd int, family int, value int) error {
|
||||
level := syscall.IPPROTO_IP
|
||||
name := syscall.IP_TTL
|
||||
if family == syscall.AF_INET6 {
|
||||
level = syscall.IPPROTO_IPV6
|
||||
name = syscall.IPV6_UNICAST_HOPS
|
||||
}
|
||||
return os.NewSyscallError("setsockopt", syscall.SetsockoptInt(fd, level, name, value))
|
||||
}
|
||||
|
||||
func setsockoptIpMinTtl(fd int, family int, value int) error {
|
||||
level := syscall.IPPROTO_IP
|
||||
name := syscall.IP_MINTTL
|
||||
if family == syscall.AF_INET6 {
|
||||
level = syscall.IPPROTO_IPV6
|
||||
name = IPV6_MINHOPCOUNT
|
||||
}
|
||||
return os.NewSyscallError("setsockopt", syscall.SetsockoptInt(fd, level, name, value))
|
||||
}
|
||||
|
||||
type TCPDialer struct {
|
||||
net.Dialer
|
||||
|
||||
// MD5 authentication password.
|
||||
AuthPassword string
|
||||
|
||||
// The TTL value to set outgoing connection.
|
||||
Ttl uint8
|
||||
|
||||
// The minimum TTL value for incoming packets.
|
||||
TtlMin uint8
|
||||
}
|
||||
|
||||
func (d *TCPDialer) DialTCP(addr string, port int) (*net.TCPConn, error) {
|
||||
var family int
|
||||
var ra, la syscall.Sockaddr
|
||||
|
||||
raddr, err := net.ResolveTCPAddr("tcp", net.JoinHostPort(addr, fmt.Sprintf("%d", port)))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid remote address: %s", err)
|
||||
}
|
||||
laddr, err := net.ResolveTCPAddr("tcp", d.LocalAddr.String())
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid local address: %s", err)
|
||||
}
|
||||
if raddr.IP.To4() != nil {
|
||||
family = syscall.AF_INET
|
||||
rsockaddr := &syscall.SockaddrInet4{Port: port}
|
||||
copy(rsockaddr.Addr[:], raddr.IP.To4())
|
||||
ra = rsockaddr
|
||||
lsockaddr := &syscall.SockaddrInet4{}
|
||||
copy(lsockaddr.Addr[:], laddr.IP.To4())
|
||||
la = lsockaddr
|
||||
} else {
|
||||
func dialerControl(network, address string, c syscall.RawConn, ttl, minTtl uint8, password string, bindInterface string) error {
|
||||
family := syscall.AF_INET
|
||||
raddr, _ := net.ResolveTCPAddr("tcp", address)
|
||||
if raddr.IP.To4() == nil {
|
||||
family = syscall.AF_INET6
|
||||
rsockaddr := &syscall.SockaddrInet6{Port: port}
|
||||
copy(rsockaddr.Addr[:], raddr.IP.To16())
|
||||
ra = rsockaddr
|
||||
var zone uint32
|
||||
if laddr.Zone != "" {
|
||||
if intf, err := net.InterfaceByName(laddr.Zone); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
zone = uint32(intf.Index)
|
||||
}
|
||||
|
||||
var sockerr error
|
||||
if password != "" {
|
||||
addr, _, _ := net.SplitHostPort(address)
|
||||
t, err := buildTcpMD5Sig(addr, password)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
b := *(*[unsafe.Sizeof(t)]byte)(unsafe.Pointer(&t))
|
||||
if err := c.Control(func(fd uintptr) {
|
||||
sockerr = os.NewSyscallError("setsockopt", syscall.SetsockoptString(int(fd), syscall.IPPROTO_TCP, tcpMD5SIG, string(b[:])))
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
if sockerr != nil {
|
||||
return sockerr
|
||||
}
|
||||
}
|
||||
|
||||
if ttl != 0 {
|
||||
if err := c.Control(func(fd uintptr) {
|
||||
level := syscall.IPPROTO_IP
|
||||
name := syscall.IP_TTL
|
||||
if family == syscall.AF_INET6 {
|
||||
level = syscall.IPPROTO_IPV6
|
||||
name = syscall.IPV6_UNICAST_HOPS
|
||||
}
|
||||
sockerr = os.NewSyscallError("setsockopt", syscall.SetsockoptInt(int(fd), level, name, int(ttl)))
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
lsockaddr := &syscall.SockaddrInet6{ZoneId: zone}
|
||||
copy(lsockaddr.Addr[:], laddr.IP.To16())
|
||||
la = lsockaddr
|
||||
}
|
||||
|
||||
sockType := syscall.SOCK_STREAM | syscall.SOCK_CLOEXEC | syscall.SOCK_NONBLOCK
|
||||
proto := 0
|
||||
fd, err := syscall.Socket(family, sockType, proto)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
fi := os.NewFile(uintptr(fd), "")
|
||||
defer fi.Close()
|
||||
// A new socket was created so we must close it before this
|
||||
// function returns either on failure or success. On success,
|
||||
// net.FileConn() in newTCPConn() increases the refcount of
|
||||
// the socket so this fi.Close() doesn't destroy the socket.
|
||||
// The caller must call Close() with the file later.
|
||||
// Note that the above os.NewFile() doesn't play with the
|
||||
// refcount.
|
||||
|
||||
if err = syscall.SetsockoptInt(fd, syscall.SOL_SOCKET, syscall.SO_BROADCAST, 1); err != nil {
|
||||
return nil, os.NewSyscallError("setsockopt", err)
|
||||
}
|
||||
|
||||
if err = syscall.SetsockoptInt(fd, syscall.IPPROTO_TCP, syscall.TCP_NODELAY, 1); err != nil {
|
||||
return nil, os.NewSyscallError("setsockopt", err)
|
||||
}
|
||||
|
||||
if d.AuthPassword != "" {
|
||||
if err = setsockoptTcpMD5Sig(fd, addr, d.AuthPassword); err != nil {
|
||||
return nil, err
|
||||
if sockerr != nil {
|
||||
return sockerr
|
||||
}
|
||||
}
|
||||
|
||||
if d.Ttl != 0 {
|
||||
if err = setsockoptIpTtl2(fd, family, int(d.Ttl)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
if d.TtlMin != 0 {
|
||||
if err = setsockoptIpMinTtl(fd, family, int(d.Ttl)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
if err = syscall.Bind(fd, la); err != nil {
|
||||
return nil, os.NewSyscallError("bind", err)
|
||||
}
|
||||
|
||||
newTCPConn := func(fi *os.File) (*net.TCPConn, error) {
|
||||
if conn, err := net.FileConn(fi); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return conn.(*net.TCPConn), err
|
||||
}
|
||||
}
|
||||
|
||||
err = syscall.Connect(fd, ra)
|
||||
switch err {
|
||||
case syscall.EINPROGRESS, syscall.EALREADY, syscall.EINTR:
|
||||
// do timeout handling
|
||||
case nil, syscall.EISCONN:
|
||||
return newTCPConn(fi)
|
||||
default:
|
||||
return nil, os.NewSyscallError("connect", err)
|
||||
}
|
||||
|
||||
epfd, e := syscall.EpollCreate1(syscall.EPOLL_CLOEXEC)
|
||||
if e != nil {
|
||||
return nil, e
|
||||
}
|
||||
defer syscall.Close(epfd)
|
||||
|
||||
var event syscall.EpollEvent
|
||||
events := make([]syscall.EpollEvent, 1)
|
||||
|
||||
event.Events = syscall.EPOLLIN | syscall.EPOLLOUT | syscall.EPOLLPRI
|
||||
event.Fd = int32(fd)
|
||||
if e = syscall.EpollCtl(epfd, syscall.EPOLL_CTL_ADD, fd, &event); e != nil {
|
||||
return nil, e
|
||||
}
|
||||
|
||||
for {
|
||||
nevents, e := syscall.EpollWait(epfd, events, int(d.Timeout/1000000) /*msec*/)
|
||||
if e != nil {
|
||||
return nil, e
|
||||
}
|
||||
if nevents == 0 {
|
||||
return nil, fmt.Errorf("timeout")
|
||||
} else if nevents == 1 && events[0].Fd == int32(fd) {
|
||||
nerr, err := syscall.GetsockoptInt(fd, syscall.SOL_SOCKET, syscall.SO_ERROR)
|
||||
if err != nil {
|
||||
return nil, os.NewSyscallError("getsockopt", err)
|
||||
if minTtl != 0 {
|
||||
if err := c.Control(func(fd uintptr) {
|
||||
level := syscall.IPPROTO_IP
|
||||
name := syscall.IP_MINTTL
|
||||
if family == syscall.AF_INET6 {
|
||||
level = syscall.IPPROTO_IPV6
|
||||
name = ipv6MinHopCount
|
||||
}
|
||||
switch err := syscall.Errno(nerr); err {
|
||||
case syscall.EINPROGRESS, syscall.EALREADY, syscall.EINTR:
|
||||
case syscall.Errno(0), syscall.EISCONN:
|
||||
return newTCPConn(fi)
|
||||
default:
|
||||
return nil, os.NewSyscallError("getsockopt", err)
|
||||
}
|
||||
} else {
|
||||
return nil, fmt.Errorf("unexpected epoll behavior")
|
||||
sockerr = os.NewSyscallError("setsockopt", syscall.SetsockoptInt(int(fd), level, name, int(minTtl)))
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
if sockerr != nil {
|
||||
return sockerr
|
||||
}
|
||||
}
|
||||
if bindInterface != "" {
|
||||
if err := c.Control(func(fd uintptr) {
|
||||
sockerr = os.NewSyscallError("setsockopt", syscall.SetsockoptString(int(fd), syscall.SOL_SOCKET, syscall.SO_BINDTODEVICE, bindInterface))
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
if sockerr != nil {
|
||||
return sockerr
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
62
vendor/github.com/osrg/gobgp/pkg/server/sockopt_openbsd.go
generated
vendored
62
vendor/github.com/osrg/gobgp/pkg/server/sockopt_openbsd.go
generated
vendored
@@ -60,7 +60,7 @@ type sadbMsg struct {
|
||||
|
||||
func (s *sadbMsg) DecodeFromBytes(data []byte) error {
|
||||
if len(data) < SADB_MSG_SIZE {
|
||||
fmt.Errorf("too short for sadbMsg %d", len(data))
|
||||
return fmt.Errorf("too short for sadbMsg %d", len(data))
|
||||
}
|
||||
s.sadbMsgVersion = data[0]
|
||||
s.sadbMsgType = data[1]
|
||||
@@ -348,12 +348,12 @@ func saDelete(address string) error {
|
||||
}
|
||||
|
||||
const (
|
||||
TCP_MD5SIG = 0x4 // TCP MD5 Signature (RFC2385)
|
||||
IPV6_MINHOPCOUNT = 73 // Generalized TTL Security Mechanism (RFC5082)
|
||||
tcpMD5SIG = 0x4 // TCP MD5 Signature (RFC2385)
|
||||
ipv6MinHopCount = 73 // Generalized TTL Security Mechanism (RFC5082)
|
||||
)
|
||||
|
||||
func setsockoptTcpMD5Sig(sc syscall.RawConn, address string, key string) error {
|
||||
if err := setsockOptInt(sc, syscall.IPPROTO_TCP, TCP_MD5SIG, 1); err != nil {
|
||||
if err := setsockOptInt(sc, syscall.IPPROTO_TCP, tcpMD5SIG, 1); err != nil {
|
||||
return err
|
||||
}
|
||||
if len(key) > 0 {
|
||||
@@ -362,7 +362,7 @@ func setsockoptTcpMD5Sig(sc syscall.RawConn, address string, key string) error {
|
||||
return saDelete(address)
|
||||
}
|
||||
|
||||
func SetTcpMD5SigSockopt(l *net.TCPListener, address string, key string) error {
|
||||
func setTCPMD5SigSockopt(l *net.TCPListener, address string, key string) error {
|
||||
sc, err := l.SyscallConn()
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -370,7 +370,7 @@ func SetTcpMD5SigSockopt(l *net.TCPListener, address string, key string) error {
|
||||
return setsockoptTcpMD5Sig(sc, address, key)
|
||||
}
|
||||
|
||||
func SetListenTcpTTLSockopt(l *net.TCPListener, ttl int) error {
|
||||
func setListenTCPTTLSockopt(l *net.TCPListener, ttl int) error {
|
||||
family := extractFamilyFromTCPListener(l)
|
||||
sc, err := l.SyscallConn()
|
||||
if err != nil {
|
||||
@@ -379,7 +379,7 @@ func SetListenTcpTTLSockopt(l *net.TCPListener, ttl int) error {
|
||||
return setsockoptIpTtl(sc, family, ttl)
|
||||
}
|
||||
|
||||
func SetTcpTTLSockopt(conn *net.TCPConn, ttl int) error {
|
||||
func setTCPTTLSockopt(conn *net.TCPConn, ttl int) error {
|
||||
family := extractFamilyFromTCPConn(conn)
|
||||
sc, err := conn.SyscallConn()
|
||||
if err != nil {
|
||||
@@ -388,7 +388,7 @@ func SetTcpTTLSockopt(conn *net.TCPConn, ttl int) error {
|
||||
return setsockoptIpTtl(sc, family, ttl)
|
||||
}
|
||||
|
||||
func SetTcpMinTTLSockopt(conn *net.TCPConn, ttl int) error {
|
||||
func setTCPMinTTLSockopt(conn *net.TCPConn, ttl int) error {
|
||||
family := extractFamilyFromTCPConn(conn)
|
||||
sc, err := conn.SyscallConn()
|
||||
if err != nil {
|
||||
@@ -398,57 +398,29 @@ func SetTcpMinTTLSockopt(conn *net.TCPConn, ttl int) error {
|
||||
name := syscall.IP_MINTTL
|
||||
if family == syscall.AF_INET6 {
|
||||
level = syscall.IPPROTO_IPV6
|
||||
name = IPV6_MINHOPCOUNT
|
||||
name = ipv6MinHopCount
|
||||
}
|
||||
return setsockOptInt(sc, level, name, ttl)
|
||||
}
|
||||
|
||||
type TCPDialer struct {
|
||||
net.Dialer
|
||||
|
||||
// MD5 authentication password.
|
||||
AuthPassword string
|
||||
|
||||
// The TTL value to set outgoing connection.
|
||||
Ttl uint8
|
||||
|
||||
// The minimum TTL value for incoming packets.
|
||||
TtlMin uint8
|
||||
}
|
||||
|
||||
func (d *TCPDialer) DialTCP(addr string, port int) (*net.TCPConn, error) {
|
||||
if d.AuthPassword != "" {
|
||||
func dialerControl(network, address string, c syscall.RawConn, ttl, minTtl uint8, password string, bindInterface string) error {
|
||||
if password != "" {
|
||||
log.WithFields(log.Fields{
|
||||
"Topic": "Peer",
|
||||
"Key": addr,
|
||||
"Key": address,
|
||||
}).Warn("setting md5 for active connection is not supported")
|
||||
}
|
||||
if d.Ttl != 0 {
|
||||
if ttl != 0 {
|
||||
log.WithFields(log.Fields{
|
||||
"Topic": "Peer",
|
||||
"Key": addr,
|
||||
"Key": address,
|
||||
}).Warn("setting ttl for active connection is not supported")
|
||||
}
|
||||
if d.TtlMin != 0 {
|
||||
if minTtl != 0 {
|
||||
log.WithFields(log.Fields{
|
||||
"Topic": "Peer",
|
||||
"Key": addr,
|
||||
"Key": address,
|
||||
}).Warn("setting min ttl for active connection is not supported")
|
||||
}
|
||||
|
||||
raddr, err := net.ResolveTCPAddr("tcp", net.JoinHostPort(addr, fmt.Sprintf("%d", port)))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid remote address: %s", err)
|
||||
}
|
||||
laddr, err := net.ResolveTCPAddr("tcp", d.LocalAddr.String())
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid local address: %s", err)
|
||||
}
|
||||
|
||||
dialer := net.Dialer{LocalAddr: laddr, Timeout: d.Timeout}
|
||||
conn, err := dialer.Dial("tcp", raddr.String())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return conn.(*net.TCPConn), nil
|
||||
return nil
|
||||
}
|
||||
|
||||
4
vendor/github.com/osrg/gobgp/pkg/server/sockopt_stub.go
generated
vendored
4
vendor/github.com/osrg/gobgp/pkg/server/sockopt_stub.go
generated
vendored
@@ -25,10 +25,6 @@ func setTcpMD5SigSockopt(l *net.TCPListener, address string, key string) error {
|
||||
return fmt.Errorf("setting md5 is not supported")
|
||||
}
|
||||
|
||||
func setListenTcpTTLSockopt(l *net.TCPListener, ttl int) error {
|
||||
return fmt.Errorf("setting ttl is not supported")
|
||||
}
|
||||
|
||||
func setTcpTTLSockopt(conn *net.TCPConn, ttl int) error {
|
||||
return fmt.Errorf("setting ttl is not supported")
|
||||
}
|
||||
|
||||
258
vendor/github.com/osrg/gobgp/pkg/server/zclient.go
generated
vendored
258
vendor/github.com/osrg/gobgp/pkg/server/zclient.go
generated
vendored
@@ -116,7 +116,27 @@ func filterOutExternalPath(paths []*table.Path) []*table.Path {
|
||||
return filteredPaths
|
||||
}
|
||||
|
||||
func newIPRouteBody(dst []*table.Path) (body *zebra.IPRouteBody, isWithdraw bool) {
|
||||
func addLabelToNexthop(path *table.Path, z *zebraClient, msgFlags *zebra.MessageFlag, nexthop *zebra.Nexthop) {
|
||||
rf := path.GetRouteFamily()
|
||||
if rf == bgp.RF_IPv4_VPN || rf == bgp.RF_IPv6_VPN {
|
||||
z.client.SetLabelFlag(msgFlags, nexthop)
|
||||
switch rf {
|
||||
case bgp.RF_IPv4_VPN:
|
||||
for _, label := range path.GetNlri().(*bgp.LabeledVPNIPAddrPrefix).Labels.Labels {
|
||||
nexthop.LabelNum++
|
||||
nexthop.MplsLabels = append(nexthop.MplsLabels, label)
|
||||
}
|
||||
case bgp.RF_IPv6_VPN:
|
||||
for _, label := range path.GetNlri().(*bgp.LabeledVPNIPv6AddrPrefix).Labels.Labels {
|
||||
nexthop.LabelNum++
|
||||
nexthop.MplsLabels = append(nexthop.MplsLabels, label)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func newIPRouteBody(dst []*table.Path, vrfID uint32, z *zebraClient) (body *zebra.IPRouteBody, isWithdraw bool) {
|
||||
version := z.client.Version
|
||||
paths := filterOutExternalPath(dst)
|
||||
if len(paths) == 0 {
|
||||
return nil, false
|
||||
@@ -125,50 +145,53 @@ func newIPRouteBody(dst []*table.Path) (body *zebra.IPRouteBody, isWithdraw bool
|
||||
|
||||
l := strings.SplitN(path.GetNlri().String(), "/", 2)
|
||||
var prefix net.IP
|
||||
//nexthops := make([]net.IP, 0, len(paths))
|
||||
var nexthop zebra.Nexthop
|
||||
nexthops := make([]zebra.Nexthop, 0, len(paths))
|
||||
msgFlags := zebra.MessageNexthop
|
||||
switch path.GetRouteFamily() {
|
||||
case bgp.RF_IPv4_UC, bgp.RF_IPv4_VPN:
|
||||
if path.GetRouteFamily() == bgp.RF_IPv4_UC {
|
||||
prefix = path.GetNlri().(*bgp.IPAddrPrefix).IPAddrPrefixDefault.Prefix.To4()
|
||||
} else {
|
||||
prefix = path.GetNlri().(*bgp.LabeledVPNIPAddrPrefix).IPAddrPrefixDefault.Prefix.To4()
|
||||
}
|
||||
for _, p := range paths {
|
||||
nexthop.Gate = p.GetNexthop().To4()
|
||||
nexthops = append(nexthops, nexthop)
|
||||
}
|
||||
case bgp.RF_IPv6_UC, bgp.RF_IPv6_VPN:
|
||||
if path.GetRouteFamily() == bgp.RF_IPv6_UC {
|
||||
prefix = path.GetNlri().(*bgp.IPv6AddrPrefix).IPAddrPrefixDefault.Prefix.To16()
|
||||
} else {
|
||||
prefix = path.GetNlri().(*bgp.LabeledVPNIPv6AddrPrefix).IPAddrPrefixDefault.Prefix.To16()
|
||||
}
|
||||
for _, p := range paths {
|
||||
nexthop.Gate = p.GetNexthop().To16()
|
||||
nexthops = append(nexthops, nexthop)
|
||||
}
|
||||
case bgp.RF_IPv4_UC:
|
||||
prefix = path.GetNlri().(*bgp.IPAddrPrefix).IPAddrPrefixDefault.Prefix.To4()
|
||||
case bgp.RF_IPv4_VPN:
|
||||
prefix = path.GetNlri().(*bgp.LabeledVPNIPAddrPrefix).IPAddrPrefixDefault.Prefix.To4()
|
||||
case bgp.RF_IPv6_UC:
|
||||
prefix = path.GetNlri().(*bgp.IPv6AddrPrefix).IPAddrPrefixDefault.Prefix.To16()
|
||||
case bgp.RF_IPv6_VPN:
|
||||
prefix = path.GetNlri().(*bgp.LabeledVPNIPv6AddrPrefix).IPAddrPrefixDefault.Prefix.To16()
|
||||
default:
|
||||
return nil, false
|
||||
}
|
||||
msgFlags := zebra.MESSAGE_NEXTHOP
|
||||
nhVrfID := uint32(zebra.DefaultVrf)
|
||||
for vrfPath, pathVrfID := range z.pathVrfMap {
|
||||
if path.Equal(vrfPath) {
|
||||
nhVrfID = pathVrfID
|
||||
break
|
||||
} else {
|
||||
continue
|
||||
}
|
||||
}
|
||||
for _, p := range paths {
|
||||
nexthop.Gate = p.GetNexthop()
|
||||
nexthop.VrfID = nhVrfID
|
||||
if nhVrfID != vrfID {
|
||||
addLabelToNexthop(path, z, &msgFlags, &nexthop)
|
||||
}
|
||||
nexthops = append(nexthops, nexthop)
|
||||
}
|
||||
plen, _ := strconv.ParseUint(l[1], 10, 8)
|
||||
med, err := path.GetMed()
|
||||
if err == nil {
|
||||
msgFlags |= zebra.MESSAGE_METRIC
|
||||
msgFlags |= zebra.MessageMetric.ToEach(version)
|
||||
}
|
||||
var flags zebra.FLAG
|
||||
info := path.GetSource()
|
||||
if info.AS == info.LocalAS {
|
||||
flags = zebra.FLAG_IBGP | zebra.FLAG_INTERNAL
|
||||
} else if info.MultihopTtl > 0 {
|
||||
flags = zebra.FLAG_INTERNAL
|
||||
var flags zebra.Flag
|
||||
if path.IsIBGP() {
|
||||
flags = zebra.FlagIBGP.ToEach(z.client.Version, z.client.SoftwareName) | zebra.FlagAllowRecursion
|
||||
} else if path.GetSource().MultihopTtl > 0 {
|
||||
flags = zebra.FlagAllowRecursion // 0x01
|
||||
}
|
||||
return &zebra.IPRouteBody{
|
||||
Type: zebra.ROUTE_BGP,
|
||||
Type: zebra.RouteBGP,
|
||||
Flags: flags,
|
||||
SAFI: zebra.SAFI_UNICAST,
|
||||
Safi: zebra.SafiUnicast,
|
||||
Message: msgFlags,
|
||||
Prefix: zebra.Prefix{
|
||||
Prefix: prefix,
|
||||
@@ -228,11 +251,11 @@ func newNexthopUnregisterBody(family uint16, prefix net.IP) *zebra.NexthopRegist
|
||||
}
|
||||
}
|
||||
|
||||
func newPathFromIPRouteMessage(m *zebra.Message, version uint8) *table.Path {
|
||||
func newPathFromIPRouteMessage(m *zebra.Message, version uint8, software string) *table.Path {
|
||||
header := m.Header
|
||||
body := m.Body.(*zebra.IPRouteBody)
|
||||
family := body.RouteFamily(version)
|
||||
isWithdraw := body.IsWithdraw(version)
|
||||
family := body.RouteFamily(version, software)
|
||||
isWithdraw := body.IsWithdraw(version, software)
|
||||
|
||||
var nlri bgp.AddrPrefixInterface
|
||||
pattr := make([]bgp.PathAttributeInterface, 0)
|
||||
@@ -242,7 +265,7 @@ func newPathFromIPRouteMessage(m *zebra.Message, version uint8) *table.Path {
|
||||
log.WithFields(log.Fields{
|
||||
"Topic": "Zebra",
|
||||
"RouteType": body.Type.String(),
|
||||
"Flag": body.Flags.String(),
|
||||
"Flag": body.Flags.String(version, software),
|
||||
"Message": body.Message,
|
||||
"Family": body.Prefix.Family,
|
||||
"Prefix": body.Prefix.Prefix,
|
||||
@@ -282,10 +305,18 @@ func newPathFromIPRouteMessage(m *zebra.Message, version uint8) *table.Path {
|
||||
return path
|
||||
}
|
||||
|
||||
type mplsLabelParameter struct {
|
||||
rangeSize uint32
|
||||
maps map[uint64]*table.Bitmap
|
||||
unassignedVrf []*table.Vrf //Vrfs which are not assigned MPLS label
|
||||
}
|
||||
|
||||
type zebraClient struct {
|
||||
client *zebra.Client
|
||||
server *BgpServer
|
||||
nexthopCache nexthopStateCache
|
||||
pathVrfMap map[*table.Path]uint32 //vpn paths and nexthop vpn id
|
||||
mplsLabel mplsLabelParameter
|
||||
dead chan struct{}
|
||||
}
|
||||
|
||||
@@ -331,9 +362,9 @@ func (z *zebraClient) updatePathByNexthopCache(paths []*table.Path) {
|
||||
}
|
||||
|
||||
func (z *zebraClient) loop() {
|
||||
w := z.server.Watch([]WatchOption{
|
||||
WatchBestPath(true),
|
||||
WatchPostUpdate(true),
|
||||
w := z.server.watch([]watchOption{
|
||||
watchBestPath(true),
|
||||
watchPostUpdate(true),
|
||||
}...)
|
||||
defer w.Stop()
|
||||
|
||||
@@ -342,9 +373,12 @@ func (z *zebraClient) loop() {
|
||||
case <-z.dead:
|
||||
return
|
||||
case msg := <-z.client.Receive():
|
||||
if msg == nil {
|
||||
break
|
||||
}
|
||||
switch body := msg.Body.(type) {
|
||||
case *zebra.IPRouteBody:
|
||||
if path := newPathFromIPRouteMessage(msg, z.client.Version); path != nil {
|
||||
if path := newPathFromIPRouteMessage(msg, z.client.Version, z.client.SoftwareName); path != nil {
|
||||
if err := z.server.addPathList("", []*table.Path{path}); err != nil {
|
||||
log.WithFields(log.Fields{
|
||||
"Topic": "Zebra",
|
||||
@@ -361,36 +395,52 @@ func (z *zebraClient) loop() {
|
||||
if len(paths) == 0 {
|
||||
// If there is no path bound for the given nexthop, send
|
||||
// NEXTHOP_UNREGISTER message.
|
||||
z.client.SendNexthopRegister(msg.Header.VrfId, newNexthopUnregisterBody(uint16(body.Prefix.Family), body.Prefix.Prefix), true)
|
||||
z.client.SendNexthopRegister(msg.Header.VrfID, newNexthopUnregisterBody(uint16(body.Prefix.Family), body.Prefix.Prefix), true)
|
||||
delete(z.nexthopCache, body.Prefix.Prefix.String())
|
||||
}
|
||||
z.updatePathByNexthopCache(paths)
|
||||
case *zebra.GetLabelChunkBody:
|
||||
log.WithFields(log.Fields{
|
||||
"Topic": "Zebra",
|
||||
"Start": body.Start,
|
||||
"End": body.End,
|
||||
}).Debugf("zebra GetLabelChunkBody is received")
|
||||
startEnd := uint64(body.Start)<<32 | uint64(body.End)
|
||||
z.mplsLabel.maps[startEnd] = table.NewBitmap(int(body.End - body.Start + 1))
|
||||
for _, vrf := range z.mplsLabel.unassignedVrf {
|
||||
if err := z.assignAndSendVrfMplsLabel(vrf); err != nil {
|
||||
log.WithFields(log.Fields{
|
||||
"Topic": "Zebra",
|
||||
"Error": err,
|
||||
}).Error("zebra failed to assign and send vrf mpls label")
|
||||
}
|
||||
}
|
||||
z.mplsLabel.unassignedVrf = nil
|
||||
}
|
||||
case ev := <-w.Event():
|
||||
switch msg := ev.(type) {
|
||||
case *WatchEventBestPath:
|
||||
case *watchEventBestPath:
|
||||
if table.UseMultiplePaths.Enabled {
|
||||
for _, paths := range msg.MultiPathList {
|
||||
z.updatePathByNexthopCache(paths)
|
||||
if body, isWithdraw := newIPRouteBody(paths); body != nil {
|
||||
z.client.SendIPRoute(0, body, isWithdraw)
|
||||
}
|
||||
if body := newNexthopRegisterBody(paths, z.nexthopCache); body != nil {
|
||||
z.client.SendNexthopRegister(0, body, false)
|
||||
for i := range msg.Vrf {
|
||||
if body, isWithdraw := newIPRouteBody(paths, i, z); body != nil {
|
||||
z.client.SendIPRoute(i, body, isWithdraw)
|
||||
}
|
||||
if body := newNexthopRegisterBody(paths, z.nexthopCache); body != nil {
|
||||
z.client.SendNexthopRegister(i, body, false)
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
z.updatePathByNexthopCache(msg.PathList)
|
||||
for _, path := range msg.PathList {
|
||||
vrfs := []uint32{0}
|
||||
if msg.Vrf != nil {
|
||||
if v, ok := msg.Vrf[path.GetNlri().String()]; ok {
|
||||
vrfs = append(vrfs, v)
|
||||
}
|
||||
}
|
||||
for _, i := range vrfs {
|
||||
if body, isWithdraw := newIPRouteBody([]*table.Path{path}); body != nil {
|
||||
z.client.SendIPRoute(i, body, isWithdraw)
|
||||
for i := range msg.Vrf {
|
||||
if body, isWithdraw := newIPRouteBody([]*table.Path{path}, i, z); body != nil {
|
||||
err := z.client.SendIPRoute(i, body, isWithdraw)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
}
|
||||
if body := newNexthopRegisterBody([]*table.Path{path}, z.nexthopCache); body != nil {
|
||||
z.client.SendNexthopRegister(i, body, false)
|
||||
@@ -398,7 +448,7 @@ func (z *zebraClient) loop() {
|
||||
}
|
||||
}
|
||||
}
|
||||
case *WatchEventUpdate:
|
||||
case *watchEventUpdate:
|
||||
if body := newNexthopRegisterBody(msg.PathList, z.nexthopCache); body != nil {
|
||||
vrfID := uint32(0)
|
||||
for _, vrf := range z.server.listVrf() {
|
||||
@@ -413,44 +463,120 @@ func (z *zebraClient) loop() {
|
||||
}
|
||||
}
|
||||
|
||||
func newZebraClient(s *BgpServer, url string, protos []string, version uint8, nhtEnable bool, nhtDelay uint8) (*zebraClient, error) {
|
||||
func newZebraClient(s *BgpServer, url string, protos []string, version uint8, nhtEnable bool, nhtDelay uint8, mplsLabelRangeSize uint32, softwareName string) (*zebraClient, error) {
|
||||
l := strings.SplitN(url, ":", 2)
|
||||
if len(l) != 2 {
|
||||
return nil, fmt.Errorf("unsupported url: %s", url)
|
||||
}
|
||||
var cli *zebra.Client
|
||||
var err error
|
||||
for _, ver := range []uint8{version, 2, 3, 4, 5} {
|
||||
cli, err = zebra.NewClient(l[0], l[1], zebra.ROUTE_BGP, ver)
|
||||
if err == nil {
|
||||
var usingVersion uint8
|
||||
var zapivers [zebra.MaxZapiVer - zebra.MinZapiVer + 1]uint8
|
||||
zapivers[0] = version
|
||||
for elem, ver := 1, zebra.MinZapiVer; elem < len(zapivers) && ver <= zebra.MaxZapiVer; elem++ {
|
||||
if version == ver && ver < zebra.MaxZapiVer {
|
||||
ver++
|
||||
}
|
||||
zapivers[elem] = ver
|
||||
ver++
|
||||
}
|
||||
for elem, ver := range zapivers {
|
||||
cli, err = zebra.NewClient(l[0], l[1], zebra.RouteBGP, ver, softwareName, mplsLabelRangeSize)
|
||||
if cli != nil && err == nil {
|
||||
usingVersion = ver
|
||||
break
|
||||
}
|
||||
// Retry with another Zebra message version
|
||||
log.WithFields(log.Fields{
|
||||
"Topic": "Zebra",
|
||||
}).Warnf("cannot connect to Zebra with message version %d. going to retry another version...", ver)
|
||||
}).Warnf("cannot connect to Zebra with message version %d.", ver)
|
||||
if elem < len(zapivers)-1 {
|
||||
log.WithFields(log.Fields{
|
||||
"Topic": "Zebra",
|
||||
}).Warnf("going to retry another version %d.", zapivers[elem+1])
|
||||
}
|
||||
}
|
||||
if cli == nil {
|
||||
if cli == nil || err != nil {
|
||||
return nil, err
|
||||
}
|
||||
log.WithFields(log.Fields{
|
||||
"Topic": "Zebra",
|
||||
}).Infof("success to connect to Zebra with message version %d.", usingVersion)
|
||||
|
||||
// Note: HELLO/ROUTER_ID_ADD messages are automatically sent to negotiate
|
||||
// the Zebra message version in zebra.NewClient().
|
||||
// cli.SendHello()
|
||||
// cli.SendRouterIDAdd()
|
||||
cli.SendInterfaceAdd()
|
||||
for _, typ := range protos {
|
||||
t, err := zebra.RouteTypeFromString(typ, version)
|
||||
t, err := zebra.RouteTypeFromString(typ, version, softwareName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cli.SendRedistribute(t, zebra.VRF_DEFAULT)
|
||||
cli.SendRedistribute(t, zebra.DefaultVrf)
|
||||
}
|
||||
w := &zebraClient{
|
||||
client: cli,
|
||||
server: s,
|
||||
nexthopCache: make(nexthopStateCache),
|
||||
dead: make(chan struct{}),
|
||||
pathVrfMap: make(map[*table.Path]uint32),
|
||||
mplsLabel: mplsLabelParameter{
|
||||
rangeSize: mplsLabelRangeSize,
|
||||
maps: make(map[uint64]*table.Bitmap),
|
||||
},
|
||||
dead: make(chan struct{}),
|
||||
}
|
||||
go w.loop()
|
||||
if mplsLabelRangeSize > 0 && cli.SupportMpls() {
|
||||
if err = cli.SendGetLabelChunk(&zebra.GetLabelChunkBody{ChunkSize: mplsLabelRangeSize}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return w, nil
|
||||
}
|
||||
|
||||
func (z *zebraClient) assignMplsLabel() (uint32, error) {
|
||||
if z.mplsLabel.maps == nil {
|
||||
return 0, nil
|
||||
}
|
||||
var label uint32
|
||||
for startEnd, bitmap := range z.mplsLabel.maps {
|
||||
start := uint32(startEnd >> 32)
|
||||
end := uint32(startEnd & 0xffffffff)
|
||||
l, err := bitmap.FindandSetZeroBit()
|
||||
if err == nil && start+uint32(l) <= end {
|
||||
label = start + uint32(l)
|
||||
break
|
||||
}
|
||||
}
|
||||
if label == 0 {
|
||||
return 0, fmt.Errorf("failed to assign new MPLS label")
|
||||
}
|
||||
return label, nil
|
||||
}
|
||||
|
||||
func (z *zebraClient) assignAndSendVrfMplsLabel(vrf *table.Vrf) error {
|
||||
var err error
|
||||
if vrf.MplsLabel, err = z.assignMplsLabel(); vrf.MplsLabel > 0 { // success
|
||||
if err = z.client.SendVrfLabel(vrf.MplsLabel, vrf.Id); err != nil {
|
||||
return err
|
||||
}
|
||||
} else if vrf.MplsLabel == 0 { // GetLabelChunk is not performed
|
||||
z.mplsLabel.unassignedVrf = append(z.mplsLabel.unassignedVrf, vrf)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (z *zebraClient) releaseMplsLabel(label uint32) {
|
||||
if z.mplsLabel.maps == nil {
|
||||
return
|
||||
}
|
||||
for startEnd, bitmap := range z.mplsLabel.maps {
|
||||
start := uint32(startEnd >> 32)
|
||||
end := uint32(startEnd & 0xffffffff)
|
||||
if start <= label && label <= end {
|
||||
bitmap.Unflag(uint(label - start))
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user