Add unit tests
This commit is contained in:
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