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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
|
// Copyright (C) 2014 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 (
"encoding/json"
log "github.com/Sirupsen/logrus"
"github.com/osrg/gobgp/api"
"github.com/osrg/gobgp/config"
"github.com/osrg/gobgp/packet"
"github.com/osrg/gobgp/table"
"gopkg.in/tomb.v2"
"net"
"time"
)
type peerMsgType int
const (
_ peerMsgType = iota
PEER_MSG_PATH
PEER_MSG_PEER_DOWN
)
type peerMsg struct {
msgType peerMsgType
msgData interface{}
}
type Peer struct {
t tomb.Tomb
globalConfig config.GlobalType
peerConfig config.NeighborType
acceptedConnCh chan *net.TCPConn
incoming chan *fsmMsg
outgoing chan *bgp.BGPMessage
serverMsgCh chan *serverMsg
peerMsgCh chan *peerMsg
fsm *FSM
adjRib *table.AdjRib
// peer and rib are always not one-to-one so should not be
// here but it's the simplest and works our first target.
rib *table.TableManager
// for now we support only the same afi as transport
rf bgp.RouteFamily
capMap map[bgp.BGPCapabilityCode]bgp.ParameterCapabilityInterface
peerInfo *table.PeerInfo
siblings map[string]*serverMsgDataPeer
}
func NewPeer(g config.GlobalType, peer config.NeighborType, serverMsgCh chan *serverMsg, peerMsgCh chan *peerMsg, peerList []*serverMsgDataPeer) *Peer {
p := &Peer{
globalConfig: g,
peerConfig: peer,
acceptedConnCh: make(chan *net.TCPConn),
incoming: make(chan *fsmMsg, 4096),
outgoing: make(chan *bgp.BGPMessage, 4096),
serverMsgCh: serverMsgCh,
peerMsgCh: peerMsgCh,
capMap: make(map[bgp.BGPCapabilityCode]bgp.ParameterCapabilityInterface),
}
p.siblings = make(map[string]*serverMsgDataPeer)
for _, s := range peerList {
p.siblings[s.address.String()] = s
}
p.fsm = NewFSM(&g, &peer, p.acceptedConnCh, p.incoming, p.outgoing)
peer.BgpNeighborCommonState.State = uint32(bgp.BGP_FSM_IDLE)
if peer.NeighborAddress.To4() != nil {
p.rf = bgp.RF_IPv4_UC
} else {
p.rf = bgp.RF_IPv6_UC
}
p.peerInfo = &table.PeerInfo{
AS: peer.PeerAs,
LocalID: g.RouterId,
RF: p.rf,
Address: peer.NeighborAddress,
}
p.adjRib = table.NewAdjRib()
p.rib = table.NewTableManager()
p.t.Go(p.loop)
return p
}
func (peer *Peer) handleBGPmessage(m *bgp.BGPMessage) {
j, _ := json.Marshal(m)
log.Debug(string(j))
switch m.Header.Type {
case bgp.BGP_MSG_OPEN:
body := m.Body.(*bgp.BGPOpen)
peer.peerInfo.ID = m.Body.(*bgp.BGPOpen).ID
for _, p := range body.OptParams {
paramCap, y := p.(*bgp.OptionParameterCapability)
if !y {
continue
}
for _, c := range paramCap.Capability {
peer.capMap[c.Code()] = c
}
}
case bgp.BGP_MSG_ROUTE_REFRESH:
pathList := peer.adjRib.GetOutPathList(peer.rf)
peer.sendMessages(table.CreateUpdateMsgFromPaths(pathList))
case bgp.BGP_MSG_UPDATE:
peer.peerConfig.BgpNeighborCommonState.UpdateRecvTime = time.Now()
body := m.Body.(*bgp.BGPUpdate)
table.UpdatePathAttrs4ByteAs(body)
msg := table.NewProcessMessage(m, peer.peerInfo)
pathList := msg.ToPathList()
if len(pathList) == 0 {
return
}
peer.adjRib.UpdateIn(pathList)
pm := &peerMsg{
msgType: PEER_MSG_PATH,
msgData: pathList,
}
for _, s := range peer.siblings {
if s.rf != peer.rf {
continue
}
s.peerMsgCh <- pm
}
}
}
func (peer *Peer) sendMessages(msgs []*bgp.BGPMessage) {
for _, m := range msgs {
if peer.peerConfig.BgpNeighborCommonState.State != uint32(bgp.BGP_FSM_ESTABLISHED) {
continue
}
if m.Header.Type != bgp.BGP_MSG_UPDATE {
log.Fatal("not update message ", m.Header.Type)
}
_, y := peer.capMap[bgp.BGP_CAP_FOUR_OCTET_AS_NUMBER]
if !y {
log.Debug("update BGPUpdate for 2byte AS peer, ", peer.peerConfig.NeighborAddress.String())
table.UpdatePathAttrs2ByteAs(m.Body.(*bgp.BGPUpdate))
}
peer.outgoing <- m
}
}
func (peer *Peer) handleREST(restReq *api.RestRequest) {
result := &api.RestResponse{}
j, _ := json.Marshal(peer.rib.Tables[peer.rf])
result.Data = j
restReq.ResponseCh <- result
close(restReq.ResponseCh)
}
func (peer *Peer) sendUpdateMsgFromPaths(pList []table.Path, wList []table.Path) {
pathList := append([]table.Path(nil), pList...)
pathList = append(pathList, wList...)
for _, p := range wList {
if !p.IsWithdraw() {
log.Fatal("withdraw pathlist has non withdraw path")
}
}
peer.adjRib.UpdateOut(pathList)
peer.sendMessages(table.CreateUpdateMsgFromPaths(pathList))
}
func (peer *Peer) handlePeerMsg(m *peerMsg) {
switch m.msgType {
case PEER_MSG_PATH:
pList, wList, _ := peer.rib.ProcessPaths(m.msgData.([]table.Path))
peer.sendUpdateMsgFromPaths(pList, wList)
case PEER_MSG_PEER_DOWN:
pList, wList, _ := peer.rib.DeletePathsforPeer(m.msgData.(*table.PeerInfo))
peer.sendUpdateMsgFromPaths(pList, wList)
}
}
func (peer *Peer) handleServerMsg(m *serverMsg) {
switch m.msgType {
case SRV_MSG_PEER_ADDED:
d := m.msgData.(*serverMsgDataPeer)
peer.siblings[d.address.String()] = d
pathList := peer.adjRib.GetInPathList(d.rf)
if len(pathList) == 0 {
return
}
pm := &peerMsg{
msgType: PEER_MSG_PATH,
msgData: pathList,
}
for _, s := range peer.siblings {
if s.rf != peer.rf {
continue
}
s.peerMsgCh <- pm
}
case SRV_MSG_PEER_DELETED:
d := m.msgData.(*table.PeerInfo)
_, found := peer.siblings[d.Address.String()]
if found {
delete(peer.siblings, d.Address.String())
pList, wList, _ := peer.rib.DeletePathsforPeer(d)
peer.sendUpdateMsgFromPaths(pList, wList)
} else {
log.Warning("can not find peer: ", d.Address.String())
}
case SRV_MSG_API:
peer.handleREST(m.msgData.(*api.RestRequest))
default:
log.Fatal("unknown server msg type ", m.msgType)
}
}
// this goroutine handles routing table operations
func (peer *Peer) loop() error {
for {
h := NewFSMHandler(peer.fsm)
sameState := true
for sameState {
select {
case <-peer.t.Dying():
close(peer.acceptedConnCh)
h.Stop()
close(peer.incoming)
close(peer.outgoing)
return nil
case e := <-peer.incoming:
switch e.MsgType {
case FSM_MSG_STATE_CHANGE:
nextState := e.MsgData.(bgp.FSMState)
// waits for all goroutines created for the current state
h.Wait()
oldState := bgp.FSMState(peer.peerConfig.BgpNeighborCommonState.State)
peer.peerConfig.BgpNeighborCommonState.State = uint32(nextState)
peer.fsm.StateChange(nextState)
sameState = false
if nextState == bgp.BGP_FSM_ESTABLISHED {
pathList := peer.adjRib.GetOutPathList(peer.rf)
peer.sendMessages(table.CreateUpdateMsgFromPaths(pathList))
peer.fsm.peerConfig.BgpNeighborCommonState.Uptime = time.Now()
peer.fsm.peerConfig.BgpNeighborCommonState.EstablishedCount++
}
if oldState == bgp.BGP_FSM_ESTABLISHED {
peer.fsm.peerConfig.BgpNeighborCommonState.Uptime = time.Time{}
pm := &peerMsg{
msgType: PEER_MSG_PEER_DOWN,
msgData: peer.peerInfo,
}
for _, s := range peer.siblings {
s.peerMsgCh <- pm
}
}
case FSM_MSG_BGP_MESSAGE:
peer.handleBGPmessage(e.MsgData.(*bgp.BGPMessage))
}
case m := <-peer.serverMsgCh:
peer.handleServerMsg(m)
case m := <-peer.peerMsgCh:
peer.handlePeerMsg(m)
}
}
}
}
func (peer *Peer) Stop() error {
peer.t.Kill(nil)
return peer.t.Wait()
}
func (peer *Peer) PassConn(conn *net.TCPConn) {
peer.acceptedConnCh <- conn
}
func (peer *Peer) MarshalJSON() ([]byte, error) {
f := peer.fsm
c := f.peerConfig
p := make(map[string]interface{})
capList := make([]int, 0)
for k, _ := range peer.capMap {
capList = append(capList, int(k))
}
p["conf"] = struct {
RemoteIP string `json:"remote_ip"`
Id string `json:"id"`
RemoteAS uint32 `json:"remote_as"`
CapRefresh bool `json:"cap_refresh"`
CapEnhancedRefresh bool `json:"cap_enhanced_refresh"`
RemoteCap []int
LocalCap []int
}{
RemoteIP: c.NeighborAddress.String(),
Id: peer.peerInfo.ID.To4().String(),
RemoteAS: c.PeerAs,
RemoteCap: capList,
LocalCap: []int{int(bgp.BGP_CAP_MULTIPROTOCOL), int(bgp.BGP_CAP_ROUTE_REFRESH), int(bgp.BGP_CAP_FOUR_OCTET_AS_NUMBER)},
}
s := c.BgpNeighborCommonState
uptime := float64(0)
if !s.Uptime.IsZero() {
uptime = time.Now().Sub(s.Uptime).Seconds()
}
p["info"] = struct {
BgpState string `json:"bgp_state"`
FsmEstablishedTransitions uint32 `json:"fsm_established_transitions"`
TotalMessageOut uint32 `json:"total_message_out"`
TotalMessageIn uint32 `json:"total_message_in"`
UpdateMessageOut uint32 `json:"update_message_out"`
UpdateMessageIn uint32 `json:"update_message_in"`
KeepAliveMessageOut uint32 `json:"keepalive_message_out"`
KeepAliveMessageIn uint32 `json:"keepalive_message_in"`
OpenMessageOut uint32 `json:"open_message_out"`
OpenMessageIn uint32 `json:"open_message_in"`
NotificationOut uint32 `json:"notification_out"`
NotificationIn uint32 `json:"notification_in"`
RefreshMessageOut uint32 `json:"refresh_message_out"`
RefreshMessageIn uint32 `json:"refresh_message_in"`
Uptime float64 `json:"uptime"`
LastError string `json:"last_error"`
}{
BgpState: f.state.String(),
FsmEstablishedTransitions: s.EstablishedCount,
TotalMessageOut: s.TotalOut,
TotalMessageIn: s.TotalIn,
UpdateMessageOut: s.UpdateOut,
UpdateMessageIn: s.UpdateIn,
KeepAliveMessageOut: s.KeepaliveOut,
KeepAliveMessageIn: s.KeepaliveIn,
OpenMessageOut: s.OpenOut,
OpenMessageIn: s.OpenIn,
NotificationOut: s.NotifyOut,
NotificationIn: s.NotifyIn,
RefreshMessageOut: s.RefreshOut,
RefreshMessageIn: s.RefreshIn,
Uptime: uptime,
}
return json.Marshal(p)
}
|