@@ -56,45 +56,21 @@ func NewServer() *Server {
56
56
return s
57
57
}
58
58
59
- func (s * Server ) setup () {
60
- if s .bgp != nil {
61
- return
62
- }
63
-
64
- // Spawn the BGP goroutines.
65
- s .bgp = bgpServer .NewBgpServer ()
66
- go s .bgp .Serve ()
67
-
68
- // Insert any path that's already defined.
69
- if len (s .paths ) > 0 {
70
- // Reset the path list.
71
- paths := s .paths
72
- s .paths = map [string ]path {}
73
-
74
- for _ , path := range paths {
75
- err := s .addPrefix (path .prefix , path .nexthop , path .owner )
76
- logger .Warn ("Unable to add prefix to BGP server" , logger.Ctx {"prefix" : path .prefix .String (), "err" : err })
77
- }
78
- }
79
- }
80
-
81
59
// Start sets up the BGP listener.
82
- func (s * Server ) Start (address string , asn uint32 , routerID net.IP ) error {
83
- // Locking.
84
- s .mu .Lock ()
85
- defer s .mu .Unlock ()
86
-
87
- return s .start (address , asn , routerID )
88
- }
89
-
90
60
func (s * Server ) start (address string , asn uint32 , routerID net.IP ) error {
91
61
// If routerID is nil, fill with our best guess.
92
62
if routerID == nil || routerID .To4 () == nil {
93
63
return ErrBadRouterID
94
64
}
95
65
96
- // Make sure we have a BGP instance.
97
- s .setup ()
66
+ // Check if already running
67
+ if s .bgp != nil {
68
+ return fmt .Errorf ("BGP listener is already running" )
69
+ }
70
+
71
+ // Spawn the BGP goroutines.
72
+ s .bgp = bgpServer .NewBgpServer ()
73
+ go s .bgp .Serve ()
98
74
99
75
// Get the address and port.
100
76
addrHost , addrPort , err := net .SplitHostPort (address )
@@ -131,8 +107,30 @@ func (s *Server) start(address string, asn uint32, routerID net.IP) error {
131
107
return err
132
108
}
133
109
134
- // Add any existing peers.
135
- for _ , peer := range s .peers {
110
+ // Copy the path list
111
+ oldPaths := map [string ]path {}
112
+ for pathUUID , path := range s .paths {
113
+ oldPaths [pathUUID ] = path
114
+ }
115
+
116
+ // Add existing paths.
117
+ s .paths = map [string ]path {}
118
+ for _ , path := range oldPaths {
119
+ err := s .addPrefix (path .prefix , path .nexthop , path .owner )
120
+ if err != nil {
121
+ logger .Warn ("Unable to add prefix to BGP server" , logger.Ctx {"prefix" : path .prefix .String (), "err" : err })
122
+ }
123
+ }
124
+
125
+ // Copy the peer list.
126
+ oldPeers := map [string ]peer {}
127
+ for peerUUID , peer := range s .peers {
128
+ oldPeers [peerUUID ] = peer
129
+ }
130
+
131
+ // Add existing peers.
132
+ s .peers = map [string ]peer {}
133
+ for _ , peer := range oldPeers {
136
134
err := s .addPeer (peer .address , peer .asn , peer .password , peer .holdtime )
137
135
if err != nil {
138
136
return err
@@ -148,20 +146,18 @@ func (s *Server) start(address string, asn uint32, routerID net.IP) error {
148
146
}
149
147
150
148
// Stop tears down the BGP listener.
151
- func (s * Server ) Stop () error {
152
- // Locking.
153
- s .mu .Lock ()
154
- defer s .mu .Unlock ()
155
-
156
- return s .stop ()
157
- }
158
-
159
149
func (s * Server ) stop () error {
160
150
// Skip if no instance.
161
151
if s .bgp == nil {
162
152
return nil
163
153
}
164
154
155
+ // Save the peer list.
156
+ oldPeers := map [string ]peer {}
157
+ for peerUUID , peer := range s .peers {
158
+ oldPeers [peerUUID ] = peer
159
+ }
160
+
165
161
// Remove all the peers (ignore failures).
166
162
for _ , peer := range s .peers {
167
163
err := s .removePeer (peer .address )
@@ -170,37 +166,38 @@ func (s *Server) stop() error {
170
166
}
171
167
}
172
168
169
+ // Restore peer list.
170
+ s .peers = oldPeers
171
+
173
172
// Stop the listener.
174
173
err := s .bgp .StopBgp (context .Background (), & bgpAPI.StopBgpRequest {})
175
174
if err != nil {
176
175
return err
177
176
}
178
177
179
- // Unset the address
178
+ // Mark the daemon as down.
180
179
s .address = ""
181
180
s .asn = 0
182
181
s .routerID = nil
182
+ s .bgp = nil
183
+
183
184
return nil
184
185
}
185
186
186
- // Reconfigure updates the listener with a new configuration..
187
- func (s * Server ) Reconfigure (address string , asn uint32 , routerID net.IP ) error {
187
+ // Configure updates the listener with a new configuration..
188
+ func (s * Server ) Configure (address string , asn uint32 , routerID net.IP ) error {
188
189
// Locking.
189
190
s .mu .Lock ()
190
191
defer s .mu .Unlock ()
191
192
192
- return s .reconfigure (address , asn , routerID )
193
+ return s .configure (address , asn , routerID )
193
194
}
194
195
195
- func (s * Server ) reconfigure (address string , asn uint32 , routerID net.IP ) error {
196
- // Get the old address .
196
+ func (s * Server ) configure (address string , asn uint32 , routerID net.IP ) error {
197
+ // Store current configuration for reverting .
197
198
oldAddress := s .address
198
199
oldASN := s .asn
199
200
oldRouterID := s .routerID
200
- oldPeers := map [string ]peer {}
201
- for peerUUID , peer := range s .peers {
202
- oldPeers [peerUUID ] = peer
203
- }
204
201
205
202
// Setup reverter.
206
203
revert := revert .New ()
@@ -209,12 +206,9 @@ func (s *Server) reconfigure(address string, asn uint32, routerID net.IP) error
209
206
// Stop the listener.
210
207
err := s .stop ()
211
208
if err != nil {
212
- return err
209
+ return fmt . Errorf ( "Failed to stop current listener: %w" , err )
213
210
}
214
211
215
- // Restore peer list.
216
- s .peers = oldPeers
217
-
218
212
// Check if we should start.
219
213
if address != "" && asn > 0 && routerID != nil {
220
214
// Restore old address on failure.
@@ -223,7 +217,7 @@ func (s *Server) reconfigure(address string, asn uint32, routerID net.IP) error
223
217
// Start the listener with the new address.
224
218
err = s .start (address , asn , routerID )
225
219
if err != nil {
226
- return err
220
+ return fmt . Errorf ( "Failed to start new listener: %w" , err )
227
221
}
228
222
}
229
223
0 commit comments