-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbgp.go
144 lines (128 loc) · 3.57 KB
/
bgp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package main
import (
"fmt"
"io"
"sync"
"container/ring"
"github.com/Shopify/exabgp-util/types"
"github.com/Shopify/exabgp-util/util"
"github.com/Sirupsen/logrus"
)
const (
NotificationsBufSize = 10
StatesBufSize = 10
)
type BGP struct {
reader io.Reader
notifications *ring.Ring
states *ring.Ring
updates []*types.UpdateType
nMu sync.Mutex
sMu sync.Mutex
uMu sync.Mutex
}
func NewBGP(reader io.Reader) (*BGP, error) {
return &BGP{reader: reader, notifications: ring.New(NotificationsBufSize), states: ring.New(StatesBufSize), updates: []*types.UpdateType{}}, nil
}
func (bgp *BGP) handleNotificationMessage(t *types.NotificationType) {
Log.WithField("notification", fmt.Sprintf("%s", *t)).Debugf("Notification message")
if *t != types.NotificationType("shutdown") {
Log.Warnf("Unknown BGP notification message: %s", t)
}
mutexed(&bgp.nMu, func() {
bgp.notifications.Value = t
bgp.notifications = bgp.notifications.Next()
})
}
func (bgp *BGP) handleStateMessage(t *types.StateType) {
Log.WithField("state", fmt.Sprintf("%+v", *t)).Debugf("State message")
switch t.State {
case "connected":
Log.Info("BGP session connected")
case "up":
Log.Info("BGP session up")
case "down":
Log.Info("BGP session down")
default:
Log.Warnf("Unknown BGP session state: %s", t.State)
}
mutexed(&bgp.sMu, func() {
bgp.states.Value = t
bgp.states = bgp.states.Next()
})
}
func (bgp *BGP) handleUpdateMessage(t *types.UpdateType) {
Log.WithField("message", fmt.Sprintf("%+v", *t)).Debugf("Update message")
mutexed(&bgp.uMu, func() {
bgp.updates = append(bgp.updates, t)
})
}
func (bgp *BGP) ReadMessages() {
Log.Info("Starting BGP message scanner")
messageChan := util.ScanMessage(bgp.reader)
for m := range messageChan {
Log.WithFields(logrus.Fields{
"type": m.Type,
"message": fmt.Sprintf("%+v", *m),
}).Debugf("Received BGP message")
switch m.Type {
case types.NotificationMessageType:
t, err := types.UnmarshalNotificationType(m.Notification)
if err != nil {
Log.WithField("error", err).Error("Error unmarshaling BGP notification message")
continue
}
bgp.handleNotificationMessage(t)
case types.StateMessageType:
t, err := types.UnmarshalStateType(m.Neighbor)
if err != nil {
Log.WithField("error", err).Error("Error unmarshaling BGP state message")
continue
}
bgp.handleStateMessage(t)
case types.UpdateMessageType:
t, err := types.UnmarshalUpdateType(m.Neighbor)
if err != nil {
Log.WithField("error", err).Error("Error unmarshaling BGP update message")
continue
}
bgp.handleUpdateMessage(t)
default:
Log.WithField("type", m.Type).Warn("Unknown BGP message type")
}
}
Log.Info("Stopped BGP message scanner")
}
func (bgp *BGP) Notifications() []*types.NotificationType {
notifications := make([]*types.NotificationType, 0, NotificationsBufSize)
mutexed(&bgp.nMu, func() {
bgp.notifications.Do(
func(n interface{}) {
if n != nil {
notifications = append(notifications, n.(*types.NotificationType))
}
})
})
return notifications
}
func (bgp *BGP) States() []*types.StateType {
states := make([]*types.StateType, 0, StatesBufSize)
mutexed(&bgp.sMu, func() {
bgp.states.Do(
func(n interface{}) {
if n != nil {
states = append(states, n.(*types.StateType))
}
})
})
return states
}
func (bgp *BGP) Updates() []*types.UpdateType {
updates := make([]*types.UpdateType, 0, len(bgp.updates))
mutexed(&bgp.uMu, func() {
for _, u := range bgp.updates {
updates = append(updates, u)
}
})
return updates
}