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
|
// 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"
"fmt"
"github.com/osrg/gobgp/config"
"github.com/osrg/gobgp/packet"
"github.com/osrg/gobgp/table"
"gopkg.in/tomb.v2"
"net"
)
type Peer struct {
t tomb.Tomb
globalConfig config.GlobalType
peerConfig config.NeighborType
acceptedConnCh chan *net.TCPConn
incoming chan *bgp.BGPMessage
outgoing chan *bgp.BGPMessage
inEventCh chan *message
outEventCh chan *message
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
}
func NewPeer(g config.GlobalType, peer config.NeighborType, outEventCh chan *message) *Peer {
p := &Peer{
globalConfig: g,
peerConfig: peer,
acceptedConnCh: make(chan *net.TCPConn),
incoming: make(chan *bgp.BGPMessage, 4096),
outgoing: make(chan *bgp.BGPMessage, 4096),
inEventCh: make(chan *message, 4096),
outEventCh: outEventCh,
}
p.fsm = NewFSM(&g, &peer, p.acceptedConnCh, p.incoming, p.outgoing)
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)
fmt.Println(string(j))
// TODO: update state here
if m.Header.Type != bgp.BGP_MSG_UPDATE {
return
}
msg := table.NewProcessMessage(m, peer.fsm.peerInfo)
pathList := msg.ToPathList()
if len(pathList) == 0 {
return
}
peer.adjRib.UpdateIn(pathList)
peer.sendToHub("", PEER_MSG_PATH, pathList)
}
func (peer *Peer) handlePeermessage(m *message) {
switch m.event {
case PEER_MSG_PATH:
pList, wList, _ := peer.rib.ProcessPaths(m.data.([]table.Path))
// TODO: merge multiple messages
// TODO: 4bytes and 2bytes conversion.
adjPathLists := append([]table.Path(nil), pList...)
msgs := make([]*bgp.BGPMessage, 0)
for _, p := range pList {
pathAttrs := p.GetPathAttrs()
nlri := p.GetNlri().(*bgp.NLRInfo)
m := bgp.NewBGPUpdateMessage([]bgp.WithdrawnRoute{}, pathAttrs, []bgp.NLRInfo{*nlri})
msgs = append(msgs, m)
}
for _, dest := range wList {
p := dest.GetOldBestPath()
draw := p.GetNlri().(*bgp.WithdrawnRoute)
m := bgp.NewBGPUpdateMessage([]bgp.WithdrawnRoute{*draw}, []bgp.PathAttributeInterface{}, []bgp.NLRInfo{})
msgs = append(msgs, m)
adjPathLists = append(adjPathLists, p.Clone(true))
}
peer.adjRib.UpdateOut(adjPathLists)
for _, m := range msgs {
peer.outgoing <- m
}
}
}
// this goroutine handles routing table operations
func (peer *Peer) loop() error {
for {
h := NewFSMHandler(peer.fsm)
sameState := true
for sameState {
select {
case nextState := <-peer.fsm.StateChanged():
// waits for all goroutines created for the current state
h.Wait()
peer.fsm.StateChange(nextState)
sameState = false
case <-peer.t.Dying():
close(peer.acceptedConnCh)
h.Stop()
close(peer.incoming)
close(peer.outgoing)
return nil
case m := <-peer.incoming:
if m == nil {
continue
}
peer.handleBGPmessage(m)
case m := <-peer.inEventCh:
peer.handlePeermessage(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) SendMessage(msg *message) {
peer.inEventCh <- msg
}
func (peer *Peer) sendToHub(destination string, event int, data interface{}) {
peer.outEventCh <- &message{
src: peer.peerConfig.NeighborAddress.String(),
dst: destination,
event: event,
data: data,
}
}
|