]>
Commit | Line | Data |
---|---|---|
d62a17ae | 1 | /* BGP-4 Finite State Machine |
896014f4 DL |
2 | * From RFC1771 [A Border Gateway Protocol 4 (BGP-4)] |
3 | * Copyright (C) 1996, 97, 98 Kunihiro Ishiguro | |
4 | * | |
5 | * This file is part of GNU Zebra. | |
6 | * | |
7 | * GNU Zebra is free software; you can redistribute it and/or modify it | |
8 | * under the terms of the GNU General Public License as published by the | |
9 | * Free Software Foundation; either version 2, or (at your option) any | |
10 | * later version. | |
11 | * | |
12 | * GNU Zebra is distributed in the hope that it will be useful, but | |
13 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 | * General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License along | |
18 | * with this program; see the file COPYING; if not, write to the Free Software | |
19 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
20 | */ | |
718e3744 | 21 | |
22 | #include <zebra.h> | |
23 | ||
24 | #include "linklist.h" | |
25 | #include "prefix.h" | |
718e3744 | 26 | #include "sockunion.h" |
27 | #include "thread.h" | |
28 | #include "log.h" | |
29 | #include "stream.h" | |
74ffbfe6 | 30 | #include "ringbuf.h" |
718e3744 | 31 | #include "memory.h" |
32 | #include "plist.h" | |
f188f2c4 | 33 | #include "workqueue.h" |
3f9c7369 | 34 | #include "queue.h" |
039f3a34 | 35 | #include "filter.h" |
4dcadbef | 36 | #include "command.h" |
02705213 | 37 | #include "lib_errors.h" |
8c48b3b6 | 38 | #include "zclient.h" |
856ca177 | 39 | #include "lib/json.h" |
718e3744 | 40 | #include "bgpd/bgpd.h" |
41 | #include "bgpd/bgp_attr.h" | |
42 | #include "bgpd/bgp_debug.h" | |
14454c9f | 43 | #include "bgpd/bgp_errors.h" |
718e3744 | 44 | #include "bgpd/bgp_fsm.h" |
45 | #include "bgpd/bgp_packet.h" | |
46 | #include "bgpd/bgp_network.h" | |
47 | #include "bgpd/bgp_route.h" | |
48 | #include "bgpd/bgp_dump.h" | |
49 | #include "bgpd/bgp_open.h" | |
f188f2c4 | 50 | #include "bgpd/bgp_advertise.h" |
3f9c7369 | 51 | #include "bgpd/bgp_updgrp.h" |
ffd0c037 | 52 | #include "bgpd/bgp_nht.h" |
c43ed2e4 | 53 | #include "bgpd/bgp_bfd.h" |
4a1ab8e4 | 54 | #include "bgpd/bgp_memory.h" |
03014d48 | 55 | #include "bgpd/bgp_keepalives.h" |
56257a44 | 56 | #include "bgpd/bgp_io.h" |
c42eab4b | 57 | #include "bgpd/bgp_zebra.h" |
6b0655a2 | 58 | |
d62a17ae | 59 | DEFINE_HOOK(peer_backward_transition, (struct peer * peer), (peer)) |
7d8d0eab | 60 | DEFINE_HOOK(peer_status_changed, (struct peer * peer), (peer)) |
36235319 | 61 | extern const char *get_afi_safi_str(afi_t afi, safi_t safi, bool for_json); |
6403814c DS |
62 | /* Definition of display strings corresponding to FSM events. This should be |
63 | * kept consistent with the events defined in bgpd.h | |
64 | */ | |
2b64873d | 65 | static const char *const bgp_event_str[] = { |
d62a17ae | 66 | NULL, |
67 | "BGP_Start", | |
68 | "BGP_Stop", | |
69 | "TCP_connection_open", | |
70 | "TCP_connection_closed", | |
71 | "TCP_connection_open_failed", | |
72 | "TCP_fatal_error", | |
73 | "ConnectRetry_timer_expired", | |
74 | "Hold_Timer_expired", | |
75 | "KeepAlive_timer_expired", | |
76 | "Receive_OPEN_message", | |
77 | "Receive_KEEPALIVE_message", | |
78 | "Receive_UPDATE_message", | |
79 | "Receive_NOTIFICATION_message", | |
80 | "Clearing_Completed", | |
6403814c DS |
81 | }; |
82 | ||
718e3744 | 83 | /* BGP FSM (finite state machine) has three types of functions. Type |
84 | one is thread functions. Type two is event functions. Type three | |
85 | is FSM functions. Timer functions are set by bgp_timer_set | |
86 | function. */ | |
87 | ||
88 | /* BGP event function. */ | |
d62a17ae | 89 | int bgp_event(struct thread *); |
718e3744 | 90 | |
91 | /* BGP thread functions. */ | |
d62a17ae | 92 | static int bgp_start_timer(struct thread *); |
93 | static int bgp_connect_timer(struct thread *); | |
94 | static int bgp_holdtime_timer(struct thread *); | |
718e3744 | 95 | |
96 | /* BGP FSM functions. */ | |
d62a17ae | 97 | static int bgp_start(struct peer *); |
718e3744 | 98 | |
e2d3a909 | 99 | /* Register peer with NHT */ |
100 | static int bgp_peer_reg_with_nht(struct peer *peer) | |
101 | { | |
102 | int connected = 0; | |
103 | ||
c8d6f0d6 | 104 | if (peer->sort == BGP_PEER_EBGP && peer->ttl == BGP_DEFAULT_TTL |
e2d3a909 | 105 | && !CHECK_FLAG(peer->flags, PEER_FLAG_DISABLE_CONNECTED_CHECK) |
106 | && !bgp_flag_check(peer->bgp, BGP_FLAG_DISABLE_NH_CONNECTED_CHK)) | |
107 | connected = 1; | |
108 | ||
109 | return bgp_find_or_add_nexthop( | |
110 | peer->bgp, peer->bgp, family2afi(peer->su.sa.sa_family), | |
111 | NULL, peer, connected); | |
112 | } | |
113 | ||
d62a17ae | 114 | static void peer_xfer_stats(struct peer *peer_dst, struct peer *peer_src) |
1ff9a340 | 115 | { |
d62a17ae | 116 | /* Copy stats over. These are only the pre-established state stats */ |
117 | peer_dst->open_in += peer_src->open_in; | |
118 | peer_dst->open_out += peer_src->open_out; | |
119 | peer_dst->keepalive_in += peer_src->keepalive_in; | |
120 | peer_dst->keepalive_out += peer_src->keepalive_out; | |
121 | peer_dst->notify_in += peer_src->notify_in; | |
122 | peer_dst->notify_out += peer_src->notify_out; | |
123 | peer_dst->dynamic_cap_in += peer_src->dynamic_cap_in; | |
124 | peer_dst->dynamic_cap_out += peer_src->dynamic_cap_out; | |
1ff9a340 DS |
125 | } |
126 | ||
d62a17ae | 127 | static struct peer *peer_xfer_conn(struct peer *from_peer) |
1ff9a340 | 128 | { |
d62a17ae | 129 | struct peer *peer; |
130 | afi_t afi; | |
131 | safi_t safi; | |
132 | int fd; | |
133 | int status, pstatus; | |
134 | unsigned char last_evt, last_maj_evt; | |
135 | ||
136 | assert(from_peer != NULL); | |
137 | ||
138 | peer = from_peer->doppelganger; | |
139 | ||
140 | if (!peer || !CHECK_FLAG(peer->flags, PEER_FLAG_CONFIG_NODE)) | |
141 | return from_peer; | |
142 | ||
9bf904cc DS |
143 | /* |
144 | * Let's check that we are not going to loose known configuration | |
145 | * state based upon doppelganger rules. | |
146 | */ | |
147 | FOREACH_AFI_SAFI (afi, safi) { | |
148 | if (from_peer->afc[afi][safi] != peer->afc[afi][safi]) { | |
149 | flog_err( | |
150 | EC_BGP_DOPPELGANGER_CONFIG, | |
151 | "from_peer->afc[%d][%d] is not the same as what we are overwriting", | |
152 | afi, safi); | |
153 | return NULL; | |
154 | } | |
155 | } | |
156 | ||
d62a17ae | 157 | if (bgp_debug_neighbor_events(peer)) |
158 | zlog_debug("%s: peer transfer %p fd %d -> %p fd %d)", | |
159 | from_peer->host, from_peer, from_peer->fd, peer, | |
160 | peer->fd); | |
161 | ||
424ab01d QY |
162 | bgp_writes_off(peer); |
163 | bgp_reads_off(peer); | |
164 | bgp_writes_off(from_peer); | |
165 | bgp_reads_off(from_peer); | |
d62a17ae | 166 | |
74e00a55 S |
167 | /* |
168 | * Before exchanging FD remove doppelganger from | |
169 | * keepalive peer hash. It could be possible conf peer | |
170 | * fd is set to -1. If blocked on lock then keepalive | |
171 | * thread can access peer pointer with fd -1. | |
172 | */ | |
173 | bgp_keepalives_off(from_peer); | |
174 | ||
d62a17ae | 175 | BGP_TIMER_OFF(peer->t_routeadv); |
424ab01d | 176 | BGP_TIMER_OFF(peer->t_connect); |
387f984e QY |
177 | BGP_TIMER_OFF(peer->t_connect_check_r); |
178 | BGP_TIMER_OFF(peer->t_connect_check_w); | |
d62a17ae | 179 | BGP_TIMER_OFF(from_peer->t_routeadv); |
424ab01d | 180 | BGP_TIMER_OFF(from_peer->t_connect); |
387f984e QY |
181 | BGP_TIMER_OFF(from_peer->t_connect_check_r); |
182 | BGP_TIMER_OFF(from_peer->t_connect_check_w); | |
7a86aa5a | 183 | BGP_TIMER_OFF(from_peer->t_process_packet); |
d3ecc69e | 184 | |
becedef6 QY |
185 | /* |
186 | * At this point in time, it is possible that there are packets pending | |
187 | * on various buffers. Those need to be transferred or dropped, | |
188 | * otherwise we'll get spurious failures during session establishment. | |
189 | */ | |
00dffa8c | 190 | frr_with_mutex(&peer->io_mtx, &from_peer->io_mtx) { |
424ab01d QY |
191 | fd = peer->fd; |
192 | peer->fd = from_peer->fd; | |
193 | from_peer->fd = fd; | |
194 | ||
195 | stream_fifo_clean(peer->ibuf); | |
d3ecc69e | 196 | stream_fifo_clean(peer->obuf); |
d3ecc69e | 197 | |
becedef6 QY |
198 | /* |
199 | * this should never happen, since bgp_process_packet() is the | |
200 | * only task that sets and unsets the current packet and it | |
201 | * runs in our pthread. | |
202 | */ | |
424ab01d | 203 | if (peer->curr) { |
af4c2728 | 204 | flog_err( |
e50f7cfd | 205 | EC_BGP_PKT_PROCESS, |
424ab01d QY |
206 | "[%s] Dropping pending packet on connection transfer:", |
207 | peer->host); | |
584470fb DL |
208 | /* there used to be a bgp_packet_dump call here, but |
209 | * that's extremely confusing since there's no way to | |
210 | * identify the packet in MRT dumps or BMP as dropped | |
211 | * due to connection transfer. | |
212 | */ | |
424ab01d QY |
213 | stream_free(peer->curr); |
214 | peer->curr = NULL; | |
215 | } | |
216 | ||
217 | // copy each packet from old peer's output queue to new peer | |
727c4f87 QY |
218 | while (from_peer->obuf->head) |
219 | stream_fifo_push(peer->obuf, | |
220 | stream_fifo_pop(from_peer->obuf)); | |
424ab01d QY |
221 | |
222 | // copy each packet from old peer's input queue to new peer | |
223 | while (from_peer->ibuf->head) | |
224 | stream_fifo_push(peer->ibuf, | |
225 | stream_fifo_pop(from_peer->ibuf)); | |
7db44ec8 | 226 | |
74ffbfe6 QY |
227 | ringbuf_wipe(peer->ibuf_work); |
228 | ringbuf_copy(peer->ibuf_work, from_peer->ibuf_work, | |
229 | ringbuf_remain(from_peer->ibuf_work)); | |
d3ecc69e | 230 | } |
d62a17ae | 231 | |
232 | peer->as = from_peer->as; | |
233 | peer->v_holdtime = from_peer->v_holdtime; | |
234 | peer->v_keepalive = from_peer->v_keepalive; | |
d62a17ae | 235 | peer->v_routeadv = from_peer->v_routeadv; |
236 | peer->v_gr_restart = from_peer->v_gr_restart; | |
237 | peer->cap = from_peer->cap; | |
238 | status = peer->status; | |
239 | pstatus = peer->ostatus; | |
240 | last_evt = peer->last_event; | |
241 | last_maj_evt = peer->last_major_event; | |
242 | peer->status = from_peer->status; | |
243 | peer->ostatus = from_peer->ostatus; | |
244 | peer->last_event = from_peer->last_event; | |
245 | peer->last_major_event = from_peer->last_major_event; | |
246 | from_peer->status = status; | |
247 | from_peer->ostatus = pstatus; | |
248 | from_peer->last_event = last_evt; | |
249 | from_peer->last_major_event = last_maj_evt; | |
250 | peer->remote_id = from_peer->remote_id; | |
3577f1c5 | 251 | peer->last_reset = from_peer->last_reset; |
d62a17ae | 252 | |
d7b3cda6 | 253 | peer->peer_gr_present_state = from_peer->peer_gr_present_state; |
254 | peer->peer_gr_new_status_flag = from_peer->peer_gr_new_status_flag; | |
255 | bgp_peer_gr_flags_update(peer); | |
256 | ||
36235319 QY |
257 | BGP_GR_ROUTER_DETECT_AND_SEND_CAPABILITY_TO_ZEBRA(peer->bgp, |
258 | peer->bgp->peer); | |
8c48b3b6 | 259 | |
d7b3cda6 | 260 | if (bgp_peer_gr_mode_get(peer) == PEER_DISABLE) { |
261 | ||
262 | UNSET_FLAG(peer->sflags, PEER_STATUS_NSF_MODE); | |
263 | ||
36235319 | 264 | if (CHECK_FLAG(peer->sflags, PEER_STATUS_NSF_WAIT)) { |
d7b3cda6 | 265 | peer_nsf_stop(peer); |
266 | } | |
267 | } | |
268 | ||
d62a17ae | 269 | if (from_peer->hostname != NULL) { |
270 | if (peer->hostname) { | |
271 | XFREE(MTYPE_BGP_PEER_HOST, peer->hostname); | |
272 | peer->hostname = NULL; | |
273 | } | |
274 | ||
275 | peer->hostname = from_peer->hostname; | |
276 | from_peer->hostname = NULL; | |
277 | } | |
278 | ||
279 | if (from_peer->domainname != NULL) { | |
280 | if (peer->domainname) { | |
281 | XFREE(MTYPE_BGP_PEER_HOST, peer->domainname); | |
282 | peer->domainname = NULL; | |
283 | } | |
284 | ||
285 | peer->domainname = from_peer->domainname; | |
286 | from_peer->domainname = NULL; | |
287 | } | |
288 | ||
05c7a1cc QY |
289 | FOREACH_AFI_SAFI (afi, safi) { |
290 | peer->af_flags[afi][safi] = from_peer->af_flags[afi][safi]; | |
291 | peer->af_sflags[afi][safi] = from_peer->af_sflags[afi][safi]; | |
292 | peer->af_cap[afi][safi] = from_peer->af_cap[afi][safi]; | |
293 | peer->afc_nego[afi][safi] = from_peer->afc_nego[afi][safi]; | |
294 | peer->afc_adv[afi][safi] = from_peer->afc_adv[afi][safi]; | |
295 | peer->afc_recv[afi][safi] = from_peer->afc_recv[afi][safi]; | |
296 | peer->orf_plist[afi][safi] = from_peer->orf_plist[afi][safi]; | |
297 | } | |
d62a17ae | 298 | |
299 | if (bgp_getsockname(peer) < 0) { | |
af4c2728 | 300 | flog_err( |
450971aa | 301 | EC_LIB_SOCKET, |
d62a17ae | 302 | "%%bgp_getsockname() failed for %s peer %s fd %d (from_peer fd %d)", |
303 | (CHECK_FLAG(peer->sflags, PEER_STATUS_ACCEPT_PEER) | |
304 | ? "accept" | |
305 | : ""), | |
306 | peer->host, peer->fd, from_peer->fd); | |
307 | bgp_stop(peer); | |
308 | bgp_stop(from_peer); | |
309 | return NULL; | |
310 | } | |
311 | if (from_peer->status > Active) { | |
312 | if (bgp_getsockname(from_peer) < 0) { | |
af4c2728 | 313 | flog_err( |
450971aa | 314 | EC_LIB_SOCKET, |
d62a17ae | 315 | "%%bgp_getsockname() failed for %s from_peer %s fd %d (peer fd %d)", |
14454c9f | 316 | |
d62a17ae | 317 | (CHECK_FLAG(from_peer->sflags, |
318 | PEER_STATUS_ACCEPT_PEER) | |
319 | ? "accept" | |
320 | : ""), | |
321 | from_peer->host, from_peer->fd, peer->fd); | |
322 | bgp_stop(from_peer); | |
323 | from_peer = NULL; | |
324 | } | |
325 | } | |
326 | ||
0112e9e0 QY |
327 | |
328 | // Note: peer_xfer_stats() must be called with I/O turned OFF | |
329 | if (from_peer) | |
330 | peer_xfer_stats(peer, from_peer); | |
331 | ||
e2d3a909 | 332 | /* Register peer for NHT. This is to allow RAs to be enabled when |
333 | * needed, even on a passive connection. | |
334 | */ | |
335 | bgp_peer_reg_with_nht(peer); | |
336 | ||
424ab01d QY |
337 | bgp_reads_on(peer); |
338 | bgp_writes_on(peer); | |
7a86aa5a QY |
339 | thread_add_timer_msec(bm->master, bgp_process_packet, peer, 0, |
340 | &peer->t_process_packet); | |
d62a17ae | 341 | |
d62a17ae | 342 | return (peer); |
1ff9a340 DS |
343 | } |
344 | ||
718e3744 | 345 | /* Hook function called after bgp event is occered. And vty's |
346 | neighbor command invoke this function after making neighbor | |
347 | structure. */ | |
d62a17ae | 348 | void bgp_timer_set(struct peer *peer) |
718e3744 | 349 | { |
d62a17ae | 350 | switch (peer->status) { |
351 | case Idle: | |
352 | /* First entry point of peer's finite state machine. In Idle | |
353 | status start timer is on unless peer is shutdown or peer is | |
354 | inactive. All other timer must be turned off */ | |
f62abc7d | 355 | if (BGP_PEER_START_SUPPRESSED(peer) || !peer_active(peer) |
dded74d5 DS |
356 | || (peer->bgp->inst_type != BGP_INSTANCE_TYPE_VIEW && |
357 | peer->bgp->vrf_id == VRF_UNKNOWN)) { | |
d62a17ae | 358 | BGP_TIMER_OFF(peer->t_start); |
359 | } else { | |
360 | BGP_TIMER_ON(peer->t_start, bgp_start_timer, | |
361 | peer->v_start); | |
362 | } | |
363 | BGP_TIMER_OFF(peer->t_connect); | |
364 | BGP_TIMER_OFF(peer->t_holdtime); | |
b72b6f4f | 365 | bgp_keepalives_off(peer); |
d62a17ae | 366 | BGP_TIMER_OFF(peer->t_routeadv); |
367 | break; | |
368 | ||
369 | case Connect: | |
370 | /* After start timer is expired, the peer moves to Connect | |
371 | status. Make sure start timer is off and connect timer is | |
372 | on. */ | |
373 | BGP_TIMER_OFF(peer->t_start); | |
374 | BGP_TIMER_ON(peer->t_connect, bgp_connect_timer, | |
375 | peer->v_connect); | |
376 | BGP_TIMER_OFF(peer->t_holdtime); | |
b72b6f4f | 377 | bgp_keepalives_off(peer); |
d62a17ae | 378 | BGP_TIMER_OFF(peer->t_routeadv); |
379 | break; | |
380 | ||
381 | case Active: | |
382 | /* Active is waiting connection from remote peer. And if | |
383 | connect timer is expired, change status to Connect. */ | |
384 | BGP_TIMER_OFF(peer->t_start); | |
385 | /* If peer is passive mode, do not set connect timer. */ | |
386 | if (CHECK_FLAG(peer->flags, PEER_FLAG_PASSIVE) | |
387 | || CHECK_FLAG(peer->sflags, PEER_STATUS_NSF_WAIT)) { | |
388 | BGP_TIMER_OFF(peer->t_connect); | |
389 | } else { | |
390 | BGP_TIMER_ON(peer->t_connect, bgp_connect_timer, | |
391 | peer->v_connect); | |
392 | } | |
393 | BGP_TIMER_OFF(peer->t_holdtime); | |
b72b6f4f | 394 | bgp_keepalives_off(peer); |
d62a17ae | 395 | BGP_TIMER_OFF(peer->t_routeadv); |
396 | break; | |
397 | ||
398 | case OpenSent: | |
399 | /* OpenSent status. */ | |
400 | BGP_TIMER_OFF(peer->t_start); | |
401 | BGP_TIMER_OFF(peer->t_connect); | |
402 | if (peer->v_holdtime != 0) { | |
403 | BGP_TIMER_ON(peer->t_holdtime, bgp_holdtime_timer, | |
404 | peer->v_holdtime); | |
405 | } else { | |
406 | BGP_TIMER_OFF(peer->t_holdtime); | |
407 | } | |
b72b6f4f | 408 | bgp_keepalives_off(peer); |
d62a17ae | 409 | BGP_TIMER_OFF(peer->t_routeadv); |
410 | break; | |
411 | ||
412 | case OpenConfirm: | |
413 | /* OpenConfirm status. */ | |
414 | BGP_TIMER_OFF(peer->t_start); | |
415 | BGP_TIMER_OFF(peer->t_connect); | |
416 | ||
417 | /* If the negotiated Hold Time value is zero, then the Hold Time | |
418 | timer and KeepAlive timers are not started. */ | |
419 | if (peer->v_holdtime == 0) { | |
420 | BGP_TIMER_OFF(peer->t_holdtime); | |
b72b6f4f | 421 | bgp_keepalives_off(peer); |
d62a17ae | 422 | } else { |
423 | BGP_TIMER_ON(peer->t_holdtime, bgp_holdtime_timer, | |
424 | peer->v_holdtime); | |
b72b6f4f | 425 | bgp_keepalives_on(peer); |
d62a17ae | 426 | } |
427 | BGP_TIMER_OFF(peer->t_routeadv); | |
428 | break; | |
429 | ||
430 | case Established: | |
431 | /* In Established status start and connect timer is turned | |
432 | off. */ | |
433 | BGP_TIMER_OFF(peer->t_start); | |
434 | BGP_TIMER_OFF(peer->t_connect); | |
435 | ||
436 | /* Same as OpenConfirm, if holdtime is zero then both holdtime | |
437 | and keepalive must be turned off. */ | |
438 | if (peer->v_holdtime == 0) { | |
439 | BGP_TIMER_OFF(peer->t_holdtime); | |
bea01226 | 440 | bgp_keepalives_off(peer); |
d62a17ae | 441 | } else { |
442 | BGP_TIMER_ON(peer->t_holdtime, bgp_holdtime_timer, | |
443 | peer->v_holdtime); | |
bea01226 | 444 | bgp_keepalives_on(peer); |
d62a17ae | 445 | } |
446 | break; | |
447 | case Deleted: | |
448 | BGP_TIMER_OFF(peer->t_gr_restart); | |
449 | BGP_TIMER_OFF(peer->t_gr_stale); | |
450 | BGP_TIMER_OFF(peer->t_pmax_restart); | |
451 | /* fallthru */ | |
452 | case Clearing: | |
453 | BGP_TIMER_OFF(peer->t_start); | |
454 | BGP_TIMER_OFF(peer->t_connect); | |
455 | BGP_TIMER_OFF(peer->t_holdtime); | |
b72b6f4f | 456 | bgp_keepalives_off(peer); |
d62a17ae | 457 | BGP_TIMER_OFF(peer->t_routeadv); |
458 | break; | |
718e3744 | 459 | } |
718e3744 | 460 | } |
461 | ||
462 | /* BGP start timer. This function set BGP_Start event to thread value | |
463 | and process event. */ | |
d62a17ae | 464 | static int bgp_start_timer(struct thread *thread) |
718e3744 | 465 | { |
d62a17ae | 466 | struct peer *peer; |
718e3744 | 467 | |
d62a17ae | 468 | peer = THREAD_ARG(thread); |
469 | peer->t_start = NULL; | |
718e3744 | 470 | |
d62a17ae | 471 | if (bgp_debug_neighbor_events(peer)) |
472 | zlog_debug("%s [FSM] Timer (start timer expire).", peer->host); | |
718e3744 | 473 | |
d62a17ae | 474 | THREAD_VAL(thread) = BGP_Start; |
475 | bgp_event(thread); /* bgp_event unlocks peer */ | |
718e3744 | 476 | |
d62a17ae | 477 | return 0; |
718e3744 | 478 | } |
479 | ||
480 | /* BGP connect retry timer. */ | |
d62a17ae | 481 | static int bgp_connect_timer(struct thread *thread) |
718e3744 | 482 | { |
d62a17ae | 483 | struct peer *peer; |
484 | int ret; | |
485 | ||
486 | peer = THREAD_ARG(thread); | |
424ab01d QY |
487 | |
488 | assert(!peer->t_write); | |
489 | assert(!peer->t_read); | |
490 | ||
d62a17ae | 491 | peer->t_connect = NULL; |
492 | ||
493 | if (bgp_debug_neighbor_events(peer)) | |
494 | zlog_debug("%s [FSM] Timer (connect timer expire)", peer->host); | |
495 | ||
496 | if (CHECK_FLAG(peer->sflags, PEER_STATUS_ACCEPT_PEER)) { | |
497 | bgp_stop(peer); | |
498 | ret = -1; | |
499 | } else { | |
500 | THREAD_VAL(thread) = ConnectRetry_timer_expired; | |
501 | bgp_event(thread); /* bgp_event unlocks peer */ | |
502 | ret = 0; | |
503 | } | |
504 | ||
505 | return ret; | |
718e3744 | 506 | } |
507 | ||
508 | /* BGP holdtime timer. */ | |
d62a17ae | 509 | static int bgp_holdtime_timer(struct thread *thread) |
718e3744 | 510 | { |
d62a17ae | 511 | struct peer *peer; |
718e3744 | 512 | |
d62a17ae | 513 | peer = THREAD_ARG(thread); |
514 | peer->t_holdtime = NULL; | |
718e3744 | 515 | |
d62a17ae | 516 | if (bgp_debug_neighbor_events(peer)) |
517 | zlog_debug("%s [FSM] Timer (holdtime timer expire)", | |
518 | peer->host); | |
718e3744 | 519 | |
d62a17ae | 520 | THREAD_VAL(thread) = Hold_Timer_expired; |
521 | bgp_event(thread); /* bgp_event unlocks peer */ | |
718e3744 | 522 | |
d62a17ae | 523 | return 0; |
718e3744 | 524 | } |
525 | ||
d62a17ae | 526 | int bgp_routeadv_timer(struct thread *thread) |
718e3744 | 527 | { |
d62a17ae | 528 | struct peer *peer; |
718e3744 | 529 | |
d62a17ae | 530 | peer = THREAD_ARG(thread); |
531 | peer->t_routeadv = NULL; | |
718e3744 | 532 | |
d62a17ae | 533 | if (bgp_debug_neighbor_events(peer)) |
534 | zlog_debug("%s [FSM] Timer (routeadv timer expire)", | |
535 | peer->host); | |
718e3744 | 536 | |
d62a17ae | 537 | peer->synctime = bgp_clock(); |
718e3744 | 538 | |
a9794991 | 539 | thread_add_timer_msec(bm->master, bgp_generate_updgrp_packets, peer, 0, |
424ab01d QY |
540 | &peer->t_generate_updgrp_packets); |
541 | ||
d62a17ae | 542 | /* MRAI timer will be started again when FIFO is built, no need to |
543 | * do it here. | |
544 | */ | |
545 | return 0; | |
718e3744 | 546 | } |
547 | ||
e0701b79 | 548 | /* BGP Peer Down Cause */ |
2b64873d | 549 | const char *const peer_down_str[] = {"", |
d62a17ae | 550 | "Router ID changed", |
551 | "Remote AS changed", | |
552 | "Local AS change", | |
553 | "Cluster ID changed", | |
554 | "Confederation identifier changed", | |
555 | "Confederation peer changed", | |
556 | "RR client config change", | |
557 | "RS client config change", | |
558 | "Update source change", | |
559 | "Address family activated", | |
560 | "Admin. shutdown", | |
561 | "User reset", | |
562 | "BGP Notification received", | |
563 | "BGP Notification send", | |
564 | "Peer closed the session", | |
565 | "Neighbor deleted", | |
566 | "Peer-group add member", | |
567 | "Peer-group delete member", | |
568 | "Capability changed", | |
569 | "Passive config change", | |
570 | "Multihop config change", | |
571 | "NSF peer closed the session", | |
572 | "Intf peering v6only config change", | |
573 | "BFD down received", | |
574 | "Interface down", | |
05912a17 | 575 | "Neighbor address lost", |
3577f1c5 | 576 | "Waiting for NHT", |
05912a17 DD |
577 | "Waiting for Peer IPv6 LLA", |
578 | "Waiting for VRF to be initialized", | |
579 | "No AFI/SAFI activated for peer"}; | |
d62a17ae | 580 | |
581 | static int bgp_graceful_restart_timer_expire(struct thread *thread) | |
e0701b79 | 582 | { |
d62a17ae | 583 | struct peer *peer; |
584 | afi_t afi; | |
585 | safi_t safi; | |
586 | ||
587 | peer = THREAD_ARG(thread); | |
588 | peer->t_gr_restart = NULL; | |
589 | ||
590 | /* NSF delete stale route */ | |
591 | for (afi = AFI_IP; afi < AFI_MAX; afi++) | |
a08ca0a7 | 592 | for (safi = SAFI_UNICAST; safi <= SAFI_MPLS_VPN; safi++) |
d62a17ae | 593 | if (peer->nsf[afi][safi]) |
594 | bgp_clear_stale_route(peer, afi, safi); | |
595 | ||
596 | UNSET_FLAG(peer->sflags, PEER_STATUS_NSF_WAIT); | |
597 | BGP_TIMER_OFF(peer->t_gr_stale); | |
598 | ||
599 | if (bgp_debug_neighbor_events(peer)) { | |
600 | zlog_debug("%s graceful restart timer expired", peer->host); | |
601 | zlog_debug("%s graceful restart stalepath timer stopped", | |
602 | peer->host); | |
603 | } | |
93406d87 | 604 | |
d62a17ae | 605 | bgp_timer_set(peer); |
93406d87 | 606 | |
d62a17ae | 607 | return 0; |
93406d87 | 608 | } |
609 | ||
d62a17ae | 610 | static int bgp_graceful_stale_timer_expire(struct thread *thread) |
93406d87 | 611 | { |
d62a17ae | 612 | struct peer *peer; |
613 | afi_t afi; | |
614 | safi_t safi; | |
93406d87 | 615 | |
d62a17ae | 616 | peer = THREAD_ARG(thread); |
617 | peer->t_gr_stale = NULL; | |
93406d87 | 618 | |
d62a17ae | 619 | if (bgp_debug_neighbor_events(peer)) |
620 | zlog_debug("%s graceful restart stalepath timer expired", | |
621 | peer->host); | |
93406d87 | 622 | |
d62a17ae | 623 | /* NSF delete stale route */ |
624 | for (afi = AFI_IP; afi < AFI_MAX; afi++) | |
a08ca0a7 | 625 | for (safi = SAFI_UNICAST; safi <= SAFI_MPLS_VPN; safi++) |
d62a17ae | 626 | if (peer->nsf[afi][safi]) |
627 | bgp_clear_stale_route(peer, afi, safi); | |
93406d87 | 628 | |
d62a17ae | 629 | return 0; |
93406d87 | 630 | } |
631 | ||
f009ff26 | 632 | /* Selection deferral timer processing function */ |
633 | static int bgp_graceful_deferral_timer_expire(struct thread *thread) | |
634 | { | |
635 | struct afi_safi_info *info; | |
636 | afi_t afi; | |
637 | safi_t safi; | |
638 | struct bgp *bgp; | |
639 | ||
640 | info = THREAD_ARG(thread); | |
641 | afi = info->afi; | |
642 | safi = info->safi; | |
643 | bgp = info->bgp; | |
644 | ||
645 | if (BGP_DEBUG(update, UPDATE_OUT)) | |
13909c4f DS |
646 | zlog_debug( |
647 | "afi %d, safi %d : graceful restart deferral timer expired", | |
648 | afi, safi); | |
f009ff26 | 649 | |
650 | bgp->gr_info[afi][safi].t_select_deferral = NULL; | |
651 | ||
652 | bgp->gr_info[afi][safi].eor_required = 0; | |
653 | bgp->gr_info[afi][safi].eor_received = 0; | |
654 | XFREE(MTYPE_TMP, info); | |
655 | ||
656 | /* Best path selection */ | |
657 | return bgp_best_path_select_defer(bgp, afi, safi); | |
658 | } | |
659 | ||
d62a17ae | 660 | static int bgp_update_delay_applicable(struct bgp *bgp) |
f188f2c4 | 661 | { |
d62a17ae | 662 | /* update_delay_over flag should be reset (set to 0) for any new |
663 | applicability of the update-delay during BGP process lifetime. | |
664 | And it should be set after an occurence of the update-delay is | |
665 | over)*/ | |
666 | if (!bgp->update_delay_over) | |
667 | return 1; | |
668 | ||
669 | return 0; | |
f188f2c4 DS |
670 | } |
671 | ||
d62a17ae | 672 | int bgp_update_delay_active(struct bgp *bgp) |
f188f2c4 | 673 | { |
d62a17ae | 674 | if (bgp->t_update_delay) |
675 | return 1; | |
f188f2c4 | 676 | |
d62a17ae | 677 | return 0; |
f188f2c4 DS |
678 | } |
679 | ||
d62a17ae | 680 | int bgp_update_delay_configured(struct bgp *bgp) |
f188f2c4 | 681 | { |
d62a17ae | 682 | if (bgp->v_update_delay) |
683 | return 1; | |
f188f2c4 | 684 | |
d62a17ae | 685 | return 0; |
f188f2c4 DS |
686 | } |
687 | ||
688 | /* Do the post-processing needed when bgp comes out of the read-only mode | |
689 | on ending the update delay. */ | |
d62a17ae | 690 | void bgp_update_delay_end(struct bgp *bgp) |
f188f2c4 | 691 | { |
d62a17ae | 692 | THREAD_TIMER_OFF(bgp->t_update_delay); |
693 | THREAD_TIMER_OFF(bgp->t_establish_wait); | |
694 | ||
695 | /* Reset update-delay related state */ | |
696 | bgp->update_delay_over = 1; | |
697 | bgp->established = 0; | |
698 | bgp->restarted_peers = 0; | |
699 | bgp->implicit_eors = 0; | |
700 | bgp->explicit_eors = 0; | |
701 | ||
702 | quagga_timestamp(3, bgp->update_delay_end_time, | |
703 | sizeof(bgp->update_delay_end_time)); | |
704 | ||
705 | /* | |
706 | * Add an end-of-initial-update marker to the main process queues so | |
707 | * that | |
708 | * the route advertisement timer for the peers can be started. Also set | |
709 | * the zebra and peer update hold flags. These flags are used to achieve | |
710 | * three stages in the update-delay post processing: | |
711 | * 1. Finish best-path selection for all the prefixes held on the | |
712 | * queues. | |
713 | * (routes in BGP are updated, and peers sync queues are populated | |
714 | * too) | |
715 | * 2. As the eoiu mark is reached in the bgp process routine, ship all | |
716 | * the | |
717 | * routes to zebra. With that zebra should see updates from BGP | |
718 | * close | |
719 | * to each other. | |
720 | * 3. Unblock the peer update writes. With that peer update packing | |
721 | * with | |
722 | * the prefixes should be at its maximum. | |
723 | */ | |
724 | bgp_add_eoiu_mark(bgp); | |
725 | bgp->main_zebra_update_hold = 1; | |
726 | bgp->main_peers_update_hold = 1; | |
727 | ||
728 | /* Resume the queue processing. This should trigger the event that would | |
729 | take | |
730 | care of processing any work that was queued during the read-only | |
731 | mode. */ | |
732 | work_queue_unplug(bm->process_main_queue); | |
f188f2c4 DS |
733 | } |
734 | ||
cb1faec9 DS |
735 | /** |
736 | * see bgp_fsm.h | |
737 | */ | |
d62a17ae | 738 | void bgp_start_routeadv(struct bgp *bgp) |
cb1faec9 | 739 | { |
d62a17ae | 740 | struct listnode *node, *nnode; |
741 | struct peer *peer; | |
cb1faec9 | 742 | |
d62a17ae | 743 | zlog_info("bgp_start_routeadv(), update hold status %d", |
744 | bgp->main_peers_update_hold); | |
4a16ae86 | 745 | |
d62a17ae | 746 | if (bgp->main_peers_update_hold) |
747 | return; | |
4a16ae86 | 748 | |
d62a17ae | 749 | quagga_timestamp(3, bgp->update_delay_peers_resume_time, |
750 | sizeof(bgp->update_delay_peers_resume_time)); | |
4a16ae86 | 751 | |
d62a17ae | 752 | for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) { |
753 | if (peer->status != Established) | |
754 | continue; | |
755 | BGP_TIMER_OFF(peer->t_routeadv); | |
756 | BGP_TIMER_ON(peer->t_routeadv, bgp_routeadv_timer, 0); | |
757 | } | |
cb1faec9 DS |
758 | } |
759 | ||
760 | /** | |
761 | * see bgp_fsm.h | |
762 | */ | |
d62a17ae | 763 | void bgp_adjust_routeadv(struct peer *peer) |
cb1faec9 | 764 | { |
d62a17ae | 765 | time_t nowtime = bgp_clock(); |
766 | double diff; | |
767 | unsigned long remain; | |
768 | ||
769 | /* Bypass checks for special case of MRAI being 0 */ | |
770 | if (peer->v_routeadv == 0) { | |
771 | /* Stop existing timer, just in case it is running for a | |
772 | * different | |
773 | * duration and schedule write thread immediately. | |
774 | */ | |
775 | if (peer->t_routeadv) | |
776 | BGP_TIMER_OFF(peer->t_routeadv); | |
777 | ||
778 | peer->synctime = bgp_clock(); | |
a9794991 | 779 | thread_add_timer_msec(bm->master, bgp_generate_updgrp_packets, |
424ab01d QY |
780 | peer, 0, |
781 | &peer->t_generate_updgrp_packets); | |
d62a17ae | 782 | return; |
783 | } | |
784 | ||
785 | ||
786 | /* | |
787 | * CASE I: | |
788 | * If the last update was written more than MRAI back, expire the timer | |
789 | * instantly so that we can send the update out sooner. | |
790 | * | |
791 | * <------- MRAI ---------> | |
792 | * |-----------------|-----------------------| | |
793 | * <------------- m ------------> | |
794 | * ^ ^ ^ | |
795 | * | | | | |
796 | * | | current time | |
797 | * | timer start | |
798 | * last write | |
799 | * | |
800 | * m > MRAI | |
801 | */ | |
802 | diff = difftime(nowtime, peer->last_update); | |
803 | if (diff > (double)peer->v_routeadv) { | |
804 | BGP_TIMER_OFF(peer->t_routeadv); | |
805 | BGP_TIMER_ON(peer->t_routeadv, bgp_routeadv_timer, 0); | |
806 | return; | |
807 | } | |
808 | ||
809 | /* | |
810 | * CASE II: | |
811 | * - Find when to expire the MRAI timer. | |
812 | * If MRAI timer is not active, assume we can start it now. | |
813 | * | |
814 | * <------- MRAI ---------> | |
815 | * |------------|-----------------------| | |
816 | * <-------- m ----------><----- r -----> | |
817 | * ^ ^ ^ | |
818 | * | | | | |
819 | * | | current time | |
820 | * | timer start | |
821 | * last write | |
822 | * | |
823 | * (MRAI - m) < r | |
824 | */ | |
825 | if (peer->t_routeadv) | |
826 | remain = thread_timer_remain_second(peer->t_routeadv); | |
827 | else | |
828 | remain = peer->v_routeadv; | |
829 | diff = peer->v_routeadv - diff; | |
830 | if (diff <= (double)remain) { | |
831 | BGP_TIMER_OFF(peer->t_routeadv); | |
832 | BGP_TIMER_ON(peer->t_routeadv, bgp_routeadv_timer, diff); | |
833 | } | |
cb1faec9 DS |
834 | } |
835 | ||
d62a17ae | 836 | static int bgp_maxmed_onstartup_applicable(struct bgp *bgp) |
abc920f8 | 837 | { |
d62a17ae | 838 | if (!bgp->maxmed_onstartup_over) |
839 | return 1; | |
abc920f8 | 840 | |
d62a17ae | 841 | return 0; |
abc920f8 DS |
842 | } |
843 | ||
d62a17ae | 844 | int bgp_maxmed_onstartup_configured(struct bgp *bgp) |
abc920f8 | 845 | { |
d62a17ae | 846 | if (bgp->v_maxmed_onstartup != BGP_MAXMED_ONSTARTUP_UNCONFIGURED) |
847 | return 1; | |
abc920f8 | 848 | |
d62a17ae | 849 | return 0; |
abc920f8 DS |
850 | } |
851 | ||
d62a17ae | 852 | int bgp_maxmed_onstartup_active(struct bgp *bgp) |
abc920f8 | 853 | { |
d62a17ae | 854 | if (bgp->t_maxmed_onstartup) |
855 | return 1; | |
abc920f8 | 856 | |
d62a17ae | 857 | return 0; |
abc920f8 DS |
858 | } |
859 | ||
d62a17ae | 860 | void bgp_maxmed_update(struct bgp *bgp) |
abc920f8 | 861 | { |
d7c0a89a QY |
862 | uint8_t maxmed_active; |
863 | uint32_t maxmed_value; | |
d62a17ae | 864 | |
865 | if (bgp->v_maxmed_admin) { | |
866 | maxmed_active = 1; | |
867 | maxmed_value = bgp->maxmed_admin_value; | |
868 | } else if (bgp->t_maxmed_onstartup) { | |
869 | maxmed_active = 1; | |
870 | maxmed_value = bgp->maxmed_onstartup_value; | |
871 | } else { | |
872 | maxmed_active = 0; | |
873 | maxmed_value = BGP_MAXMED_VALUE_DEFAULT; | |
874 | } | |
875 | ||
876 | if (bgp->maxmed_active != maxmed_active | |
877 | || bgp->maxmed_value != maxmed_value) { | |
878 | bgp->maxmed_active = maxmed_active; | |
879 | bgp->maxmed_value = maxmed_value; | |
880 | ||
881 | update_group_announce(bgp); | |
882 | } | |
abc920f8 DS |
883 | } |
884 | ||
885 | /* The maxmed onstartup timer expiry callback. */ | |
d62a17ae | 886 | static int bgp_maxmed_onstartup_timer(struct thread *thread) |
abc920f8 | 887 | { |
d62a17ae | 888 | struct bgp *bgp; |
abc920f8 | 889 | |
d62a17ae | 890 | zlog_info("Max med on startup ended - timer expired."); |
abc920f8 | 891 | |
d62a17ae | 892 | bgp = THREAD_ARG(thread); |
893 | THREAD_TIMER_OFF(bgp->t_maxmed_onstartup); | |
894 | bgp->maxmed_onstartup_over = 1; | |
abc920f8 | 895 | |
d62a17ae | 896 | bgp_maxmed_update(bgp); |
abc920f8 | 897 | |
d62a17ae | 898 | return 0; |
abc920f8 DS |
899 | } |
900 | ||
d62a17ae | 901 | static void bgp_maxmed_onstartup_begin(struct bgp *bgp) |
abc920f8 | 902 | { |
d62a17ae | 903 | /* Applicable only once in the process lifetime on the startup */ |
904 | if (bgp->maxmed_onstartup_over) | |
905 | return; | |
abc920f8 | 906 | |
d62a17ae | 907 | zlog_info("Begin maxmed onstartup mode - timer %d seconds", |
908 | bgp->v_maxmed_onstartup); | |
abc920f8 | 909 | |
d62a17ae | 910 | thread_add_timer(bm->master, bgp_maxmed_onstartup_timer, bgp, |
911 | bgp->v_maxmed_onstartup, &bgp->t_maxmed_onstartup); | |
abc920f8 | 912 | |
d62a17ae | 913 | if (!bgp->v_maxmed_admin) { |
914 | bgp->maxmed_active = 1; | |
915 | bgp->maxmed_value = bgp->maxmed_onstartup_value; | |
916 | } | |
abc920f8 | 917 | |
d62a17ae | 918 | /* Route announce to all peers should happen after this in |
919 | * bgp_establish() */ | |
abc920f8 DS |
920 | } |
921 | ||
d62a17ae | 922 | static void bgp_maxmed_onstartup_process_status_change(struct peer *peer) |
abc920f8 | 923 | { |
d62a17ae | 924 | if (peer->status == Established && !peer->bgp->established) { |
925 | bgp_maxmed_onstartup_begin(peer->bgp); | |
926 | } | |
abc920f8 DS |
927 | } |
928 | ||
f188f2c4 | 929 | /* The update delay timer expiry callback. */ |
d62a17ae | 930 | static int bgp_update_delay_timer(struct thread *thread) |
f188f2c4 | 931 | { |
d62a17ae | 932 | struct bgp *bgp; |
f188f2c4 | 933 | |
d62a17ae | 934 | zlog_info("Update delay ended - timer expired."); |
f188f2c4 | 935 | |
d62a17ae | 936 | bgp = THREAD_ARG(thread); |
937 | THREAD_TIMER_OFF(bgp->t_update_delay); | |
938 | bgp_update_delay_end(bgp); | |
f188f2c4 | 939 | |
d62a17ae | 940 | return 0; |
f188f2c4 DS |
941 | } |
942 | ||
943 | /* The establish wait timer expiry callback. */ | |
d62a17ae | 944 | static int bgp_establish_wait_timer(struct thread *thread) |
f188f2c4 | 945 | { |
d62a17ae | 946 | struct bgp *bgp; |
f188f2c4 | 947 | |
d62a17ae | 948 | zlog_info("Establish wait - timer expired."); |
f188f2c4 | 949 | |
d62a17ae | 950 | bgp = THREAD_ARG(thread); |
951 | THREAD_TIMER_OFF(bgp->t_establish_wait); | |
952 | bgp_check_update_delay(bgp); | |
f188f2c4 | 953 | |
d62a17ae | 954 | return 0; |
f188f2c4 DS |
955 | } |
956 | ||
957 | /* Steps to begin the update delay: | |
958 | - initialize queues if needed | |
959 | - stop the queue processing | |
960 | - start the timer */ | |
d62a17ae | 961 | static void bgp_update_delay_begin(struct bgp *bgp) |
f188f2c4 | 962 | { |
d62a17ae | 963 | struct listnode *node, *nnode; |
964 | struct peer *peer; | |
f188f2c4 | 965 | |
d62a17ae | 966 | /* Stop the processing of queued work. Enqueue shall continue */ |
967 | work_queue_plug(bm->process_main_queue); | |
f188f2c4 | 968 | |
d62a17ae | 969 | for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) |
970 | peer->update_delay_over = 0; | |
f188f2c4 | 971 | |
d62a17ae | 972 | /* Start the update-delay timer */ |
973 | thread_add_timer(bm->master, bgp_update_delay_timer, bgp, | |
974 | bgp->v_update_delay, &bgp->t_update_delay); | |
f188f2c4 | 975 | |
d62a17ae | 976 | if (bgp->v_establish_wait != bgp->v_update_delay) |
977 | thread_add_timer(bm->master, bgp_establish_wait_timer, bgp, | |
978 | bgp->v_establish_wait, &bgp->t_establish_wait); | |
f188f2c4 | 979 | |
d62a17ae | 980 | quagga_timestamp(3, bgp->update_delay_begin_time, |
981 | sizeof(bgp->update_delay_begin_time)); | |
f188f2c4 DS |
982 | } |
983 | ||
d62a17ae | 984 | static void bgp_update_delay_process_status_change(struct peer *peer) |
f188f2c4 | 985 | { |
d62a17ae | 986 | if (peer->status == Established) { |
987 | if (!peer->bgp->established++) { | |
988 | bgp_update_delay_begin(peer->bgp); | |
989 | zlog_info( | |
990 | "Begin read-only mode - update-delay timer %d seconds", | |
991 | peer->bgp->v_update_delay); | |
992 | } | |
993 | if (CHECK_FLAG(peer->cap, PEER_CAP_RESTART_BIT_RCV)) | |
994 | bgp_update_restarted_peers(peer); | |
995 | } | |
996 | if (peer->ostatus == Established | |
997 | && bgp_update_delay_active(peer->bgp)) { | |
998 | /* Adjust the update-delay state to account for this flap. | |
999 | NOTE: Intentionally skipping adjusting implicit_eors or | |
1000 | explicit_eors | |
1001 | counters. Extra sanity check in bgp_check_update_delay() | |
1002 | should | |
1003 | be enough to take care of any additive discrepancy in bgp eor | |
1004 | counters */ | |
1005 | peer->bgp->established--; | |
1006 | peer->update_delay_over = 0; | |
1007 | } | |
f188f2c4 DS |
1008 | } |
1009 | ||
0437e105 | 1010 | /* Called after event occurred, this function change status and reset |
200df115 | 1011 | read/write and timer thread. */ |
d62a17ae | 1012 | void bgp_fsm_change_status(struct peer *peer, int status) |
200df115 | 1013 | { |
36dc7588 | 1014 | struct bgp *bgp; |
1015 | uint32_t peer_count; | |
1ff9a340 | 1016 | |
36dc7588 | 1017 | bgp = peer->bgp; |
1018 | peer_count = bgp->established_peers; | |
1019 | ||
1020 | if (status == Established) | |
1021 | bgp->established_peers++; | |
1022 | else if ((peer->status == Established) && (status != Established)) | |
1023 | bgp->established_peers--; | |
1024 | ||
1cfe005d DS |
1025 | if (bgp_debug_neighbor_events(peer)) { |
1026 | struct vrf *vrf = vrf_lookup_by_id(bgp->vrf_id); | |
1027 | ||
1028 | zlog_debug("%s : vrf %s(%u), Status: %s established_peers %u", __func__, | |
1029 | vrf ? vrf->name : "Unknown", bgp->vrf_id, | |
1030 | lookup_msg(bgp_status_msg, status, NULL), | |
1031 | bgp->established_peers); | |
1032 | } | |
1033 | ||
36dc7588 | 1034 | /* Set to router ID to the value provided by RIB if there are no peers |
1035 | * in the established state and peer count did not change | |
1036 | */ | |
1037 | if ((peer_count != bgp->established_peers) && | |
1038 | (bgp->established_peers == 0)) | |
1039 | bgp_router_id_zebra_bump(bgp->vrf_id, NULL); | |
1040 | ||
d62a17ae | 1041 | /* Transition into Clearing or Deleted must /always/ clear all routes.. |
1042 | * (and must do so before actually changing into Deleted.. | |
1043 | */ | |
1044 | if (status >= Clearing) { | |
1045 | bgp_clear_route_all(peer); | |
1046 | ||
1047 | /* If no route was queued for the clear-node processing, | |
1048 | * generate the | |
1049 | * completion event here. This is needed because if there are no | |
1050 | * routes | |
1051 | * to trigger the background clear-node thread, the event won't | |
1052 | * get | |
1053 | * generated and the peer would be stuck in Clearing. Note that | |
1054 | * this | |
1055 | * event is for the peer and helps the peer transition out of | |
1056 | * Clearing | |
1057 | * state; it should not be generated per (AFI,SAFI). The event | |
1058 | * is | |
1059 | * directly posted here without calling clear_node_complete() as | |
1060 | * we | |
1061 | * shouldn't do an extra unlock. This event will get processed | |
1062 | * after | |
1063 | * the state change that happens below, so peer will be in | |
1064 | * Clearing | |
1065 | * (or Deleted). | |
1066 | */ | |
1067 | if (!work_queue_is_scheduled(peer->clear_node_queue)) | |
1068 | BGP_EVENT_ADD(peer, Clearing_Completed); | |
1069 | } | |
1070 | ||
1071 | /* Preserve old status and change into new status. */ | |
1072 | peer->ostatus = peer->status; | |
1073 | peer->status = status; | |
1074 | ||
1075 | /* Save event that caused status change. */ | |
1076 | peer->last_major_event = peer->cur_event; | |
1077 | ||
7d8d0eab MKS |
1078 | /* Operations after status change */ |
1079 | hook_call(peer_status_changed, peer); | |
1080 | ||
d62a17ae | 1081 | if (status == Established) |
1082 | UNSET_FLAG(peer->sflags, PEER_STATUS_ACCEPT_PEER); | |
1083 | ||
1084 | /* If max-med processing is applicable, do the necessary. */ | |
1085 | if (status == Established) { | |
1086 | if (bgp_maxmed_onstartup_configured(peer->bgp) | |
1087 | && bgp_maxmed_onstartup_applicable(peer->bgp)) | |
1088 | bgp_maxmed_onstartup_process_status_change(peer); | |
1089 | else | |
1090 | peer->bgp->maxmed_onstartup_over = 1; | |
1091 | } | |
1092 | ||
1093 | /* If update-delay processing is applicable, do the necessary. */ | |
1094 | if (bgp_update_delay_configured(peer->bgp) | |
1095 | && bgp_update_delay_applicable(peer->bgp)) | |
1096 | bgp_update_delay_process_status_change(peer); | |
1097 | ||
1098 | if (bgp_debug_neighbor_events(peer)) | |
1099 | zlog_debug("%s went from %s to %s", peer->host, | |
1100 | lookup_msg(bgp_status_msg, peer->ostatus, NULL), | |
1101 | lookup_msg(bgp_status_msg, peer->status, NULL)); | |
200df115 | 1102 | } |
1103 | ||
3117b5c4 | 1104 | /* Flush the event queue and ensure the peer is shut down */ |
d62a17ae | 1105 | static int bgp_clearing_completed(struct peer *peer) |
3117b5c4 | 1106 | { |
d62a17ae | 1107 | int rc = bgp_stop(peer); |
1ff9a340 | 1108 | |
d62a17ae | 1109 | if (rc >= 0) |
1110 | BGP_EVENT_FLUSH(peer); | |
3117b5c4 | 1111 | |
d62a17ae | 1112 | return rc; |
3117b5c4 SH |
1113 | } |
1114 | ||
718e3744 | 1115 | /* Administrative BGP peer stop event. */ |
3117b5c4 | 1116 | /* May be called multiple times for the same peer */ |
d62a17ae | 1117 | int bgp_stop(struct peer *peer) |
718e3744 | 1118 | { |
d62a17ae | 1119 | afi_t afi; |
1120 | safi_t safi; | |
1121 | char orf_name[BUFSIZ]; | |
1122 | int ret = 0; | |
f009ff26 | 1123 | struct bgp *bgp = peer->bgp; |
1124 | struct graceful_restart_info *gr_info = NULL; | |
d62a17ae | 1125 | |
2ba1fe69 | 1126 | peer->nsf_af_count = 0; |
1127 | ||
d62a17ae | 1128 | if (peer_dynamic_neighbor(peer) |
1129 | && !(CHECK_FLAG(peer->flags, PEER_FLAG_DELETE))) { | |
1130 | if (bgp_debug_neighbor_events(peer)) | |
1131 | zlog_debug("%s (dynamic neighbor) deleted", peer->host); | |
1132 | peer_delete(peer); | |
1133 | return -1; | |
c22767d8 | 1134 | } |
848973c7 | 1135 | |
d62a17ae | 1136 | /* Can't do this in Clearing; events are used for state transitions */ |
1137 | if (peer->status != Clearing) { | |
1138 | /* Delete all existing events of the peer */ | |
1139 | BGP_EVENT_FLUSH(peer); | |
93406d87 | 1140 | } |
d62a17ae | 1141 | |
1142 | /* Increment Dropped count. */ | |
1143 | if (peer->status == Established) { | |
1144 | peer->dropped++; | |
1145 | ||
1146 | /* bgp log-neighbor-changes of neighbor Down */ | |
1147 | if (bgp_flag_check(peer->bgp, BGP_FLAG_LOG_NEIGHBOR_CHANGES)) { | |
1148 | struct vrf *vrf = vrf_lookup_by_id(peer->bgp->vrf_id); | |
2986cac2 | 1149 | |
d62a17ae | 1150 | zlog_info( |
1151 | "%%ADJCHANGE: neighbor %s(%s) in vrf %s Down %s", | |
1152 | peer->host, | |
1153 | (peer->hostname) ? peer->hostname : "Unknown", | |
5742e42b DS |
1154 | vrf ? ((vrf->vrf_id != VRF_DEFAULT) |
1155 | ? vrf->name | |
1156 | : VRF_DEFAULT_NAME) | |
d62a17ae | 1157 | : "", |
1158 | peer_down_str[(int)peer->last_reset]); | |
1159 | } | |
1160 | ||
1161 | /* graceful restart */ | |
1162 | if (peer->t_gr_stale) { | |
1163 | BGP_TIMER_OFF(peer->t_gr_stale); | |
1164 | if (bgp_debug_neighbor_events(peer)) | |
1165 | zlog_debug( | |
1166 | "%s graceful restart stalepath timer stopped", | |
1167 | peer->host); | |
1168 | } | |
1169 | if (CHECK_FLAG(peer->sflags, PEER_STATUS_NSF_WAIT)) { | |
1170 | if (bgp_debug_neighbor_events(peer)) { | |
1171 | zlog_debug( | |
1172 | "%s graceful restart timer started for %d sec", | |
1173 | peer->host, peer->v_gr_restart); | |
1174 | zlog_debug( | |
1175 | "%s graceful restart stalepath timer started for %d sec", | |
1176 | peer->host, peer->bgp->stalepath_time); | |
1177 | } | |
1178 | BGP_TIMER_ON(peer->t_gr_restart, | |
1179 | bgp_graceful_restart_timer_expire, | |
1180 | peer->v_gr_restart); | |
1181 | BGP_TIMER_ON(peer->t_gr_stale, | |
1182 | bgp_graceful_stale_timer_expire, | |
1183 | peer->bgp->stalepath_time); | |
1184 | } else { | |
1185 | UNSET_FLAG(peer->sflags, PEER_STATUS_NSF_MODE); | |
1186 | ||
1187 | for (afi = AFI_IP; afi < AFI_MAX; afi++) | |
996c9314 LB |
1188 | for (safi = SAFI_UNICAST; safi <= SAFI_MPLS_VPN; |
1189 | safi++) | |
d62a17ae | 1190 | peer->nsf[afi][safi] = 0; |
1191 | } | |
1192 | ||
f009ff26 | 1193 | /* If peer reset before receiving EOR, decrement EOR count and |
1194 | * cancel the selection deferral timer if there are no | |
1195 | * pending EOR messages to be received | |
1196 | */ | |
1197 | if (BGP_PEER_GRACEFUL_RESTART_CAPABLE(peer)) { | |
1198 | FOREACH_AFI_SAFI (afi, safi) { | |
13909c4f DS |
1199 | if (!peer->afc_nego[afi][safi] |
1200 | || CHECK_FLAG(peer->af_sflags[afi][safi], | |
1201 | PEER_STATUS_EOR_RECEIVED)) | |
1202 | continue; | |
1203 | ||
1204 | gr_info = &bgp->gr_info[afi][safi]; | |
1205 | if (!gr_info) | |
1206 | continue; | |
1207 | ||
1208 | if (gr_info->eor_required) | |
1209 | gr_info->eor_required--; | |
1210 | ||
1211 | if (BGP_DEBUG(update, UPDATE_OUT)) | |
1212 | zlog_debug("peer %s, EOR_required %d", | |
1213 | peer->host, | |
1214 | gr_info->eor_required); | |
1215 | ||
1216 | /* There is no pending EOR message */ | |
1217 | if (gr_info->eor_required == 0) { | |
1218 | BGP_TIMER_OFF( | |
f009ff26 | 1219 | gr_info->t_select_deferral); |
13909c4f | 1220 | gr_info->eor_received = 0; |
f009ff26 | 1221 | } |
1222 | } | |
1223 | } | |
1224 | ||
d62a17ae | 1225 | /* set last reset time */ |
1226 | peer->resettime = peer->uptime = bgp_clock(); | |
1227 | ||
1228 | if (BGP_DEBUG(update_groups, UPDATE_GROUPS)) | |
1229 | zlog_debug("%s remove from all update group", | |
1230 | peer->host); | |
1231 | update_group_remove_peer_afs(peer); | |
1232 | ||
1233 | hook_call(peer_backward_transition, peer); | |
1234 | ||
1235 | /* Reset peer synctime */ | |
1236 | peer->synctime = 0; | |
93406d87 | 1237 | } |
93406d87 | 1238 | |
424ab01d | 1239 | /* stop keepalives */ |
b72b6f4f | 1240 | bgp_keepalives_off(peer); |
424ab01d QY |
1241 | |
1242 | /* Stop read and write threads. */ | |
1243 | bgp_writes_off(peer); | |
1244 | bgp_reads_off(peer); | |
1245 | ||
387f984e QY |
1246 | THREAD_OFF(peer->t_connect_check_r); |
1247 | THREAD_OFF(peer->t_connect_check_w); | |
d62a17ae | 1248 | |
1249 | /* Stop all timers. */ | |
1250 | BGP_TIMER_OFF(peer->t_start); | |
1251 | BGP_TIMER_OFF(peer->t_connect); | |
1252 | BGP_TIMER_OFF(peer->t_holdtime); | |
d62a17ae | 1253 | BGP_TIMER_OFF(peer->t_routeadv); |
d62a17ae | 1254 | |
1255 | /* Clear input and output buffer. */ | |
00dffa8c | 1256 | frr_with_mutex(&peer->io_mtx) { |
424ab01d QY |
1257 | if (peer->ibuf) |
1258 | stream_fifo_clean(peer->ibuf); | |
d3ecc69e QY |
1259 | if (peer->obuf) |
1260 | stream_fifo_clean(peer->obuf); | |
424ab01d QY |
1261 | |
1262 | if (peer->ibuf_work) | |
74ffbfe6 | 1263 | ringbuf_wipe(peer->ibuf_work); |
424ab01d QY |
1264 | if (peer->obuf_work) |
1265 | stream_reset(peer->obuf_work); | |
1266 | ||
1267 | if (peer->curr) { | |
1268 | stream_free(peer->curr); | |
1269 | peer->curr = NULL; | |
1270 | } | |
d3ecc69e | 1271 | } |
d62a17ae | 1272 | |
1273 | /* Close of file descriptor. */ | |
1274 | if (peer->fd >= 0) { | |
1275 | close(peer->fd); | |
1276 | peer->fd = -1; | |
93406d87 | 1277 | } |
1278 | ||
05c7a1cc QY |
1279 | FOREACH_AFI_SAFI (afi, safi) { |
1280 | /* Reset all negotiated variables */ | |
1281 | peer->afc_nego[afi][safi] = 0; | |
1282 | peer->afc_adv[afi][safi] = 0; | |
1283 | peer->afc_recv[afi][safi] = 0; | |
1284 | ||
1285 | /* peer address family capability flags*/ | |
1286 | peer->af_cap[afi][safi] = 0; | |
1287 | ||
1288 | /* peer address family status flags*/ | |
1289 | peer->af_sflags[afi][safi] = 0; | |
1290 | ||
1291 | /* Received ORF prefix-filter */ | |
1292 | peer->orf_plist[afi][safi] = NULL; | |
1293 | ||
1294 | if ((peer->status == OpenConfirm) | |
1295 | || (peer->status == Established)) { | |
1296 | /* ORF received prefix-filter pnt */ | |
1297 | sprintf(orf_name, "%s.%d.%d", peer->host, afi, safi); | |
1298 | prefix_bgp_orf_remove_all(afi, orf_name); | |
d62a17ae | 1299 | } |
05c7a1cc | 1300 | } |
d62a17ae | 1301 | |
1302 | /* Reset keepalive and holdtime */ | |
b90a8e13 | 1303 | if (CHECK_FLAG(peer->flags, PEER_FLAG_TIMER)) { |
d62a17ae | 1304 | peer->v_keepalive = peer->keepalive; |
1305 | peer->v_holdtime = peer->holdtime; | |
1306 | } else { | |
1307 | peer->v_keepalive = peer->bgp->default_keepalive; | |
1308 | peer->v_holdtime = peer->bgp->default_holdtime; | |
1ff9a340 | 1309 | } |
d62a17ae | 1310 | |
1311 | peer->update_time = 0; | |
1312 | ||
1313 | /* Until we are sure that there is no problem about prefix count | |
1314 | this should be commented out.*/ | |
718e3744 | 1315 | #if 0 |
1316 | /* Reset prefix count */ | |
1317 | peer->pcount[AFI_IP][SAFI_UNICAST] = 0; | |
1318 | peer->pcount[AFI_IP][SAFI_MULTICAST] = 0; | |
cd1964ff | 1319 | peer->pcount[AFI_IP][SAFI_LABELED_UNICAST] = 0; |
718e3744 | 1320 | peer->pcount[AFI_IP][SAFI_MPLS_VPN] = 0; |
1321 | peer->pcount[AFI_IP6][SAFI_UNICAST] = 0; | |
1322 | peer->pcount[AFI_IP6][SAFI_MULTICAST] = 0; | |
cd1964ff | 1323 | peer->pcount[AFI_IP6][SAFI_LABELED_UNICAST] = 0; |
718e3744 | 1324 | #endif /* 0 */ |
1325 | ||
d62a17ae | 1326 | if (!CHECK_FLAG(peer->flags, PEER_FLAG_CONFIG_NODE) |
1327 | && !(CHECK_FLAG(peer->flags, PEER_FLAG_DELETE))) { | |
1328 | peer_delete(peer); | |
1329 | ret = -1; | |
1330 | } else { | |
1331 | bgp_peer_conf_if_to_su_update(peer); | |
1332 | } | |
d62a17ae | 1333 | return ret; |
718e3744 | 1334 | } |
1335 | ||
1336 | /* BGP peer is stoped by the error. */ | |
d62a17ae | 1337 | static int bgp_stop_with_error(struct peer *peer) |
718e3744 | 1338 | { |
d62a17ae | 1339 | /* Double start timer. */ |
1340 | peer->v_start *= 2; | |
1341 | ||
1342 | /* Overflow check. */ | |
1343 | if (peer->v_start >= (60 * 2)) | |
1344 | peer->v_start = (60 * 2); | |
1345 | ||
1346 | if (peer_dynamic_neighbor(peer)) { | |
1347 | if (bgp_debug_neighbor_events(peer)) | |
1348 | zlog_debug("%s (dynamic neighbor) deleted", peer->host); | |
1349 | peer_delete(peer); | |
1350 | return -1; | |
1351 | } | |
1352 | ||
1353 | return (bgp_stop(peer)); | |
718e3744 | 1354 | } |
1355 | ||
397b5bde LR |
1356 | |
1357 | /* something went wrong, send notify and tear down */ | |
d7c0a89a QY |
1358 | static int bgp_stop_with_notify(struct peer *peer, uint8_t code, |
1359 | uint8_t sub_code) | |
397b5bde | 1360 | { |
d62a17ae | 1361 | /* Send notify to remote peer */ |
1362 | bgp_notify_send(peer, code, sub_code); | |
1363 | ||
1364 | if (peer_dynamic_neighbor(peer)) { | |
1365 | if (bgp_debug_neighbor_events(peer)) | |
1366 | zlog_debug("%s (dynamic neighbor) deleted", peer->host); | |
1367 | peer_delete(peer); | |
1368 | return -1; | |
1369 | } | |
f14e6fdb | 1370 | |
d62a17ae | 1371 | /* Clear start timer value to default. */ |
1372 | peer->v_start = BGP_INIT_START_TIMER; | |
397b5bde | 1373 | |
d62a17ae | 1374 | return (bgp_stop(peer)); |
397b5bde LR |
1375 | } |
1376 | ||
07a16526 QY |
1377 | /** |
1378 | * Determines whether a TCP session has successfully established for a peer and | |
1379 | * events as appropriate. | |
1380 | * | |
1381 | * This function is called when setting up a new session. After connect() is | |
387f984e QY |
1382 | * called on the peer's socket (in bgp_start()), the fd is passed to poll() |
1383 | * to wait for connection success or failure. When poll() returns, this | |
07a16526 | 1384 | * function is called to evaluate the result. |
387f984e QY |
1385 | * |
1386 | * Due to differences in behavior of poll() on Linux and BSD - specifically, | |
1387 | * the value of .revents in the case of a closed connection - this function is | |
1388 | * scheduled both for a read and a write event. The write event is triggered | |
1389 | * when the connection is established. A read event is triggered when the | |
1390 | * connection is closed. Thus we need to cancel whichever one did not occur. | |
07a16526 QY |
1391 | */ |
1392 | static int bgp_connect_check(struct thread *thread) | |
1393 | { | |
1394 | int status; | |
1395 | socklen_t slen; | |
1396 | int ret; | |
1397 | struct peer *peer; | |
1398 | ||
1399 | peer = THREAD_ARG(thread); | |
424ab01d QY |
1400 | assert(!CHECK_FLAG(peer->thread_flags, PEER_THREAD_READS_ON)); |
1401 | assert(!CHECK_FLAG(peer->thread_flags, PEER_THREAD_WRITES_ON)); | |
1402 | assert(!peer->t_read); | |
1403 | assert(!peer->t_write); | |
07a16526 | 1404 | |
387f984e QY |
1405 | THREAD_OFF(peer->t_connect_check_r); |
1406 | THREAD_OFF(peer->t_connect_check_w); | |
dc1188bb | 1407 | |
07a16526 QY |
1408 | /* Check file descriptor. */ |
1409 | slen = sizeof(status); | |
1410 | ret = getsockopt(peer->fd, SOL_SOCKET, SO_ERROR, (void *)&status, | |
1411 | &slen); | |
1412 | ||
1413 | /* If getsockopt is fail, this is fatal error. */ | |
1414 | if (ret < 0) { | |
4cb5e18b | 1415 | zlog_err("can't get sockopt for nonblocking connect: %d(%s)", |
54ff5e9b | 1416 | errno, safe_strerror(errno)); |
07a16526 QY |
1417 | BGP_EVENT_ADD(peer, TCP_fatal_error); |
1418 | return -1; | |
1419 | } | |
1420 | ||
1421 | /* When status is 0 then TCP connection is established. */ | |
1422 | if (status == 0) { | |
1423 | BGP_EVENT_ADD(peer, TCP_connection_open); | |
1424 | return 1; | |
1425 | } else { | |
1426 | if (bgp_debug_neighbor_events(peer)) | |
54ff5e9b DS |
1427 | zlog_debug("%s [Event] Connect failed %d(%s)", |
1428 | peer->host, status, safe_strerror(status)); | |
07a16526 QY |
1429 | BGP_EVENT_ADD(peer, TCP_connection_open_failed); |
1430 | return 0; | |
1431 | } | |
1432 | } | |
397b5bde | 1433 | |
718e3744 | 1434 | /* TCP connection open. Next we send open message to remote peer. And |
1435 | add read thread for reading open message. */ | |
d62a17ae | 1436 | static int bgp_connect_success(struct peer *peer) |
718e3744 | 1437 | { |
d62a17ae | 1438 | if (peer->fd < 0) { |
e50f7cfd | 1439 | flog_err(EC_BGP_CONNECT, |
1c50c1c0 QY |
1440 | "bgp_connect_success peer's fd is negative value %d", |
1441 | peer->fd); | |
d62a17ae | 1442 | bgp_stop(peer); |
1443 | return -1; | |
1444 | } | |
1445 | ||
1446 | if (bgp_getsockname(peer) < 0) { | |
450971aa | 1447 | flog_err_sys(EC_LIB_SOCKET, |
09c866e3 QY |
1448 | "%s: bgp_getsockname(): failed for peer %s, fd %d", |
1449 | __FUNCTION__, peer->host, peer->fd); | |
0e35025e DA |
1450 | bgp_notify_send( |
1451 | peer, BGP_NOTIFY_FSM_ERR, | |
1452 | BGP_NOTIFY_SUBCODE_UNSPECIFIC); /* internal error */ | |
424ab01d | 1453 | bgp_writes_on(peer); |
d62a17ae | 1454 | return -1; |
1455 | } | |
1456 | ||
424ab01d | 1457 | bgp_reads_on(peer); |
d62a17ae | 1458 | |
1459 | if (bgp_debug_neighbor_events(peer)) { | |
1460 | char buf1[SU_ADDRSTRLEN]; | |
1461 | ||
1462 | if (!CHECK_FLAG(peer->sflags, PEER_STATUS_ACCEPT_PEER)) | |
1463 | zlog_debug("%s open active, local address %s", | |
1464 | peer->host, | |
1465 | sockunion2str(peer->su_local, buf1, | |
1466 | SU_ADDRSTRLEN)); | |
1467 | else | |
1468 | zlog_debug("%s passive open", peer->host); | |
1469 | } | |
1470 | ||
1471 | bgp_open_send(peer); | |
1472 | ||
1473 | return 0; | |
718e3744 | 1474 | } |
1475 | ||
1476 | /* TCP connect fail */ | |
d62a17ae | 1477 | static int bgp_connect_fail(struct peer *peer) |
718e3744 | 1478 | { |
d62a17ae | 1479 | if (peer_dynamic_neighbor(peer)) { |
1480 | if (bgp_debug_neighbor_events(peer)) | |
1481 | zlog_debug("%s (dynamic neighbor) deleted", peer->host); | |
1482 | peer_delete(peer); | |
1483 | return -1; | |
1484 | } | |
1485 | ||
1486 | return (bgp_stop(peer)); | |
718e3744 | 1487 | } |
1488 | ||
1489 | /* This function is the first starting point of all BGP connection. It | |
1490 | try to connect to remote peer with non-blocking IO. */ | |
d62a17ae | 1491 | int bgp_start(struct peer *peer) |
718e3744 | 1492 | { |
d62a17ae | 1493 | int status; |
d62a17ae | 1494 | |
1495 | bgp_peer_conf_if_to_su_update(peer); | |
1496 | ||
1497 | if (peer->su.sa.sa_family == AF_UNSPEC) { | |
1498 | if (bgp_debug_neighbor_events(peer)) | |
1499 | zlog_debug( | |
1500 | "%s [FSM] Unable to get neighbor's IP address, waiting...", | |
1501 | peer->host); | |
3577f1c5 | 1502 | peer->last_reset = PEER_DOWN_NBR_ADDR; |
d62a17ae | 1503 | return -1; |
1504 | } | |
1505 | ||
1506 | if (BGP_PEER_START_SUPPRESSED(peer)) { | |
1507 | if (bgp_debug_neighbor_events(peer)) | |
e50f7cfd | 1508 | flog_err(EC_BGP_FSM, |
1c50c1c0 QY |
1509 | "%s [FSM] Trying to start suppressed peer" |
1510 | " - this is never supposed to happen!", | |
1511 | peer->host); | |
d62a17ae | 1512 | return -1; |
1513 | } | |
1514 | ||
1515 | /* Scrub some information that might be left over from a previous, | |
1516 | * session | |
1517 | */ | |
1518 | /* Connection information. */ | |
1519 | if (peer->su_local) { | |
1520 | sockunion_free(peer->su_local); | |
1521 | peer->su_local = NULL; | |
1522 | } | |
1523 | ||
1524 | if (peer->su_remote) { | |
1525 | sockunion_free(peer->su_remote); | |
1526 | peer->su_remote = NULL; | |
1527 | } | |
1528 | ||
1529 | /* Clear remote router-id. */ | |
975a328e | 1530 | peer->remote_id.s_addr = INADDR_ANY; |
d62a17ae | 1531 | |
1532 | /* Clear peer capability flag. */ | |
1533 | peer->cap = 0; | |
1534 | ||
1535 | /* If the peer is passive mode, force to move to Active mode. */ | |
1536 | if (CHECK_FLAG(peer->flags, PEER_FLAG_PASSIVE)) { | |
1537 | BGP_EVENT_ADD(peer, TCP_connection_open_failed); | |
1538 | return 0; | |
1539 | } | |
1540 | ||
dded74d5 DS |
1541 | if (peer->bgp->inst_type != BGP_INSTANCE_TYPE_VIEW && |
1542 | peer->bgp->vrf_id == VRF_UNKNOWN) { | |
61cf4b37 | 1543 | if (bgp_debug_neighbor_events(peer)) |
af4c2728 | 1544 | flog_err( |
e50f7cfd | 1545 | EC_BGP_FSM, |
996c9314 LB |
1546 | "%s [FSM] In a VRF that is not initialised yet", |
1547 | peer->host); | |
3577f1c5 | 1548 | peer->last_reset = PEER_DOWN_VRF_UNINIT; |
61cf4b37 PG |
1549 | return -1; |
1550 | } | |
1551 | ||
e2d3a909 | 1552 | /* Register peer for NHT. If next hop is already resolved, proceed |
1553 | * with connection setup, else wait. | |
1554 | */ | |
1555 | if (!bgp_peer_reg_with_nht(peer)) { | |
c42eab4b DS |
1556 | if (bgp_zebra_num_connects()) { |
1557 | if (bgp_debug_neighbor_events(peer)) | |
1558 | zlog_debug("%s [FSM] Waiting for NHT", | |
1559 | peer->host); | |
3577f1c5 | 1560 | peer->last_reset = PEER_DOWN_WAITING_NHT; |
c42eab4b DS |
1561 | BGP_EVENT_ADD(peer, TCP_connection_open_failed); |
1562 | return 0; | |
1563 | } | |
718e3744 | 1564 | } |
d62a17ae | 1565 | |
424ab01d QY |
1566 | assert(!peer->t_write); |
1567 | assert(!peer->t_read); | |
1568 | assert(!CHECK_FLAG(peer->thread_flags, PEER_THREAD_WRITES_ON)); | |
1569 | assert(!CHECK_FLAG(peer->thread_flags, PEER_THREAD_READS_ON)); | |
d62a17ae | 1570 | status = bgp_connect(peer); |
1571 | ||
1572 | switch (status) { | |
1573 | case connect_error: | |
1574 | if (bgp_debug_neighbor_events(peer)) | |
1575 | zlog_debug("%s [FSM] Connect error", peer->host); | |
1576 | BGP_EVENT_ADD(peer, TCP_connection_open_failed); | |
1577 | break; | |
1578 | case connect_success: | |
1579 | if (bgp_debug_neighbor_events(peer)) | |
1580 | zlog_debug( | |
1581 | "%s [FSM] Connect immediately success, fd %d", | |
1582 | peer->host, peer->fd); | |
1583 | BGP_EVENT_ADD(peer, TCP_connection_open); | |
1584 | break; | |
1585 | case connect_in_progress: | |
1586 | /* To check nonblocking connect, we wait until socket is | |
1587 | readable or writable. */ | |
1588 | if (bgp_debug_neighbor_events(peer)) | |
1589 | zlog_debug( | |
1590 | "%s [FSM] Non blocking connect waiting result, fd %d", | |
1591 | peer->host, peer->fd); | |
1592 | if (peer->fd < 0) { | |
e50f7cfd | 1593 | flog_err(EC_BGP_FSM, |
1c50c1c0 QY |
1594 | "bgp_start peer's fd is negative value %d", |
1595 | peer->fd); | |
d62a17ae | 1596 | return -1; |
1597 | } | |
becedef6 | 1598 | /* |
387f984e QY |
1599 | * - when the socket becomes ready, poll() will signify POLLOUT |
1600 | * - if it fails to connect, poll() will signify POLLHUP | |
1601 | * - POLLHUP is handled as a 'read' event by thread.c | |
1602 | * | |
1603 | * therefore, we schedule both a read and a write event with | |
1604 | * bgp_connect_check() as the handler for each and cancel the | |
1605 | * unused event in that function. | |
becedef6 | 1606 | */ |
424ab01d | 1607 | thread_add_read(bm->master, bgp_connect_check, peer, peer->fd, |
387f984e QY |
1608 | &peer->t_connect_check_r); |
1609 | thread_add_write(bm->master, bgp_connect_check, peer, peer->fd, | |
1610 | &peer->t_connect_check_w); | |
d62a17ae | 1611 | break; |
1612 | } | |
1613 | return 0; | |
718e3744 | 1614 | } |
1615 | ||
1616 | /* Connect retry timer is expired when the peer status is Connect. */ | |
d62a17ae | 1617 | static int bgp_reconnect(struct peer *peer) |
718e3744 | 1618 | { |
d62a17ae | 1619 | if (bgp_stop(peer) < 0) |
1620 | return -1; | |
1ff9a340 | 1621 | |
8c48b3b6 | 1622 | /* Send graceful restart capabilty */ |
36235319 QY |
1623 | BGP_GR_ROUTER_DETECT_AND_SEND_CAPABILITY_TO_ZEBRA(peer->bgp, |
1624 | peer->bgp->peer); | |
8c48b3b6 | 1625 | |
d62a17ae | 1626 | bgp_start(peer); |
1627 | return 0; | |
718e3744 | 1628 | } |
1629 | ||
d62a17ae | 1630 | static int bgp_fsm_open(struct peer *peer) |
718e3744 | 1631 | { |
d62a17ae | 1632 | /* Send keepalive and make keepalive timer */ |
1633 | bgp_keepalive_send(peer); | |
718e3744 | 1634 | |
d62a17ae | 1635 | /* Reset holdtimer value. */ |
1636 | BGP_TIMER_OFF(peer->t_holdtime); | |
718e3744 | 1637 | |
d62a17ae | 1638 | return 0; |
718e3744 | 1639 | } |
1640 | ||
397b5bde LR |
1641 | /* FSM error, unexpected event. This is error of BGP connection. So cut the |
1642 | peer and change to Idle status. */ | |
d62a17ae | 1643 | static int bgp_fsm_event_error(struct peer *peer) |
397b5bde | 1644 | { |
1c50c1c0 QY |
1645 | flog_err(EC_BGP_FSM, "%s [FSM] unexpected packet received in state %s", |
1646 | peer->host, lookup_msg(bgp_status_msg, peer->status, NULL)); | |
397b5bde | 1647 | |
d62a17ae | 1648 | return bgp_stop_with_notify(peer, BGP_NOTIFY_FSM_ERR, 0); |
397b5bde LR |
1649 | } |
1650 | ||
718e3744 | 1651 | /* Hold timer expire. This is error of BGP connection. So cut the |
1652 | peer and change to Idle status. */ | |
d62a17ae | 1653 | static int bgp_fsm_holdtime_expire(struct peer *peer) |
718e3744 | 1654 | { |
d62a17ae | 1655 | if (bgp_debug_neighbor_events(peer)) |
1656 | zlog_debug("%s [FSM] Hold timer expire", peer->host); | |
718e3744 | 1657 | |
d62a17ae | 1658 | return bgp_stop_with_notify(peer, BGP_NOTIFY_HOLD_ERR, 0); |
718e3744 | 1659 | } |
1660 | ||
f009ff26 | 1661 | /* Start the selection deferral timer thread for the specified AFI, SAFI */ |
1662 | static int bgp_start_deferral_timer(struct bgp *bgp, afi_t afi, safi_t safi, | |
36235319 | 1663 | struct graceful_restart_info *gr_info) |
f009ff26 | 1664 | { |
1665 | struct afi_safi_info *thread_info; | |
1666 | ||
1667 | /* If the deferral timer is active, then increment eor count */ | |
1668 | if (gr_info->t_select_deferral) { | |
1669 | gr_info->eor_required++; | |
1670 | return 0; | |
1671 | } | |
1672 | ||
1673 | /* Start the deferral timer when the first peer enabled for the graceful | |
1674 | * restart is established | |
1675 | */ | |
1676 | if (gr_info->eor_required == 0) { | |
1677 | thread_info = XMALLOC(MTYPE_TMP, sizeof(struct afi_safi_info)); | |
f009ff26 | 1678 | |
1679 | thread_info->afi = afi; | |
1680 | thread_info->safi = safi; | |
1681 | thread_info->bgp = bgp; | |
1682 | ||
13909c4f DS |
1683 | thread_add_timer(bm->master, bgp_graceful_deferral_timer_expire, |
1684 | thread_info, bgp->select_defer_time, | |
1685 | &gr_info->t_select_deferral); | |
f009ff26 | 1686 | } |
f009ff26 | 1687 | gr_info->eor_required++; |
8c48b3b6 | 1688 | /* Send message to RIB indicating route update pending */ |
1689 | if (gr_info->af_enabled[afi][safi] == false) { | |
1690 | gr_info->af_enabled[afi][safi] = true; | |
1691 | /* Send message to RIB */ | |
1692 | bgp_zebra_update(afi, safi, bgp->vrf_id, | |
1693 | ZEBRA_CLIENT_ROUTE_UPDATE_PENDING); | |
1694 | } | |
f009ff26 | 1695 | if (BGP_DEBUG(update, UPDATE_OUT)) |
1696 | zlog_debug("Started the deferral timer for %s eor_required %d", | |
36235319 QY |
1697 | get_afi_safi_str(afi, safi, false), |
1698 | gr_info->eor_required); | |
f009ff26 | 1699 | return 0; |
1700 | } | |
1701 | ||
1702 | /* Update the graceful restart information for the specified AFI, SAFI */ | |
1703 | static int bgp_update_gr_info(struct peer *peer, afi_t afi, safi_t safi) | |
1704 | { | |
1705 | struct graceful_restart_info *gr_info; | |
1706 | struct bgp *bgp = peer->bgp; | |
1707 | int ret = 0; | |
1708 | ||
1709 | if ((afi < AFI_IP) || (afi >= AFI_MAX)) { | |
1710 | if (BGP_DEBUG(update, UPDATE_OUT)) | |
1711 | zlog_debug("%s : invalid afi %d", __func__, afi); | |
1712 | return -1; | |
1713 | } | |
1714 | ||
1715 | if ((safi < SAFI_UNICAST) || (safi > SAFI_MPLS_VPN)) { | |
1716 | if (BGP_DEBUG(update, UPDATE_OUT)) | |
1717 | zlog_debug("%s : invalid safi %d", __func__, safi); | |
1718 | return -1; | |
1719 | } | |
1720 | ||
1721 | /* Restarting router */ | |
36235319 QY |
1722 | if (BGP_PEER_GRACEFUL_RESTART_CAPABLE(peer) |
1723 | && BGP_PEER_RESTARTING_MODE(peer)) { | |
f009ff26 | 1724 | /* Check if the forwarding state is preserved */ |
1725 | if (bgp_flag_check(bgp, BGP_FLAG_GR_PRESERVE_FWD)) { | |
1726 | gr_info = &(bgp->gr_info[afi][safi]); | |
1727 | ret = bgp_start_deferral_timer(bgp, afi, safi, gr_info); | |
1728 | } | |
1729 | } | |
2ba1fe69 | 1730 | return ret; |
f009ff26 | 1731 | } |
1732 | ||
727c4f87 QY |
1733 | /** |
1734 | * Transition to Established state. | |
1735 | * | |
1736 | * Convert peer from stub to full fledged peer, set some timers, and generate | |
1737 | * initial updates. | |
1738 | */ | |
d62a17ae | 1739 | static int bgp_establish(struct peer *peer) |
718e3744 | 1740 | { |
d62a17ae | 1741 | afi_t afi; |
1742 | safi_t safi; | |
1743 | int nsf_af_count = 0; | |
1744 | int ret = 0; | |
1745 | struct peer *other; | |
f009ff26 | 1746 | int status; |
d62a17ae | 1747 | |
1748 | other = peer->doppelganger; | |
1749 | peer = peer_xfer_conn(peer); | |
1750 | if (!peer) { | |
e50f7cfd | 1751 | flog_err(EC_BGP_CONNECT, "%%Neighbor failed in xfer_conn"); |
d62a17ae | 1752 | return -1; |
93406d87 | 1753 | } |
93406d87 | 1754 | |
d62a17ae | 1755 | if (other == peer) |
996c9314 LB |
1756 | ret = 1; /* bgp_establish specific code when xfer_conn |
1757 | happens. */ | |
d62a17ae | 1758 | |
1759 | /* Reset capability open status flag. */ | |
1760 | if (!CHECK_FLAG(peer->sflags, PEER_STATUS_CAPABILITY_OPEN)) | |
1761 | SET_FLAG(peer->sflags, PEER_STATUS_CAPABILITY_OPEN); | |
1762 | ||
1763 | /* Clear start timer value to default. */ | |
1764 | peer->v_start = BGP_INIT_START_TIMER; | |
1765 | ||
1766 | /* Increment established count. */ | |
1767 | peer->established++; | |
1768 | bgp_fsm_change_status(peer, Established); | |
1769 | ||
1770 | /* bgp log-neighbor-changes of neighbor Up */ | |
1771 | if (bgp_flag_check(peer->bgp, BGP_FLAG_LOG_NEIGHBOR_CHANGES)) { | |
1772 | struct vrf *vrf = vrf_lookup_by_id(peer->bgp->vrf_id); | |
1773 | zlog_info("%%ADJCHANGE: neighbor %s(%s) in vrf %s Up", | |
1774 | peer->host, | |
1775 | (peer->hostname) ? peer->hostname : "Unknown", | |
5742e42b DS |
1776 | vrf ? ((vrf->vrf_id != VRF_DEFAULT) |
1777 | ? vrf->name | |
1778 | : VRF_DEFAULT_NAME) | |
d62a17ae | 1779 | : ""); |
1780 | } | |
1781 | /* assign update-group/subgroup */ | |
1782 | update_group_adjust_peer_afs(peer); | |
1783 | ||
1784 | /* graceful restart */ | |
1785 | UNSET_FLAG(peer->sflags, PEER_STATUS_NSF_WAIT); | |
f009ff26 | 1786 | if (bgp_debug_neighbor_events(peer)) { |
1787 | if (BGP_PEER_RESTARTING_MODE(peer)) | |
36235319 | 1788 | zlog_debug("peer %s BGP_RESTARTING_MODE", peer->host); |
f009ff26 | 1789 | else if (BGP_PEER_HELPER_MODE(peer)) |
36235319 | 1790 | zlog_debug("peer %s BGP_HELPER_MODE", peer->host); |
f009ff26 | 1791 | } |
d62a17ae | 1792 | for (afi = AFI_IP; afi < AFI_MAX; afi++) |
a08ca0a7 | 1793 | for (safi = SAFI_UNICAST; safi <= SAFI_MPLS_VPN; safi++) { |
d62a17ae | 1794 | if (peer->afc_nego[afi][safi] |
1795 | && CHECK_FLAG(peer->cap, PEER_CAP_RESTART_ADV) | |
1796 | && CHECK_FLAG(peer->af_cap[afi][safi], | |
1797 | PEER_CAP_RESTART_AF_RCV)) { | |
1798 | if (peer->nsf[afi][safi] | |
1799 | && !CHECK_FLAG( | |
1800 | peer->af_cap[afi][safi], | |
1801 | PEER_CAP_RESTART_AF_PRESERVE_RCV)) | |
1802 | bgp_clear_stale_route(peer, afi, safi); | |
1803 | ||
1804 | peer->nsf[afi][safi] = 1; | |
1805 | nsf_af_count++; | |
1806 | } else { | |
1807 | if (peer->nsf[afi][safi]) | |
1808 | bgp_clear_stale_route(peer, afi, safi); | |
1809 | peer->nsf[afi][safi] = 0; | |
1810 | } | |
f009ff26 | 1811 | /* Update the graceful restart information */ |
1812 | if (peer->afc_nego[afi][safi]) { | |
1813 | if (!BGP_SELECT_DEFER_DISABLE(peer->bgp)) { | |
1814 | status = bgp_update_gr_info(peer, afi, | |
1815 | safi); | |
1816 | if (status < 0) | |
36235319 QY |
1817 | zlog_err( |
1818 | "Error in updating graceful restart for %s", | |
1819 | get_afi_safi_str( | |
1820 | afi, safi, | |
1821 | false)); | |
9e3b51a7 | 1822 | } else { |
1823 | if (BGP_PEER_GRACEFUL_RESTART_CAPABLE( | |
36235319 QY |
1824 | peer) |
1825 | && BGP_PEER_RESTARTING_MODE(peer) | |
1826 | && bgp_flag_check( | |
1827 | peer->bgp, | |
1828 | BGP_FLAG_GR_PRESERVE_FWD)) | |
9e3b51a7 | 1829 | peer->bgp->gr_info[afi][safi] |
1830 | .eor_required++; | |
f009ff26 | 1831 | } |
1832 | } | |
d62a17ae | 1833 | } |
1834 | ||
794b37d5 | 1835 | peer->nsf_af_count = nsf_af_count; |
1836 | ||
d62a17ae | 1837 | if (nsf_af_count) |
1838 | SET_FLAG(peer->sflags, PEER_STATUS_NSF_MODE); | |
1839 | else { | |
1840 | UNSET_FLAG(peer->sflags, PEER_STATUS_NSF_MODE); | |
1841 | if (peer->t_gr_stale) { | |
1842 | BGP_TIMER_OFF(peer->t_gr_stale); | |
1843 | if (bgp_debug_neighbor_events(peer)) | |
1844 | zlog_debug( | |
1845 | "%s graceful restart stalepath timer stopped", | |
1846 | peer->host); | |
1847 | } | |
1848 | } | |
93406d87 | 1849 | |
d62a17ae | 1850 | if (peer->t_gr_restart) { |
1851 | BGP_TIMER_OFF(peer->t_gr_restart); | |
1852 | if (bgp_debug_neighbor_events(peer)) | |
1853 | zlog_debug("%s graceful restart timer stopped", | |
1854 | peer->host); | |
1855 | } | |
718e3744 | 1856 | |
9eb217ff QY |
1857 | /* Reset uptime, turn on keepalives, send current table. */ |
1858 | if (!peer->v_holdtime) | |
1859 | bgp_keepalives_on(peer); | |
1860 | ||
d62a17ae | 1861 | peer->uptime = bgp_clock(); |
1862 | ||
1863 | /* Send route-refresh when ORF is enabled */ | |
05c7a1cc QY |
1864 | FOREACH_AFI_SAFI (afi, safi) { |
1865 | if (CHECK_FLAG(peer->af_cap[afi][safi], | |
1866 | PEER_CAP_ORF_PREFIX_SM_ADV)) { | |
d62a17ae | 1867 | if (CHECK_FLAG(peer->af_cap[afi][safi], |
05c7a1cc QY |
1868 | PEER_CAP_ORF_PREFIX_RM_RCV)) |
1869 | bgp_route_refresh_send(peer, afi, safi, | |
1870 | ORF_TYPE_PREFIX, | |
1871 | REFRESH_IMMEDIATE, 0); | |
1872 | else if (CHECK_FLAG(peer->af_cap[afi][safi], | |
1873 | PEER_CAP_ORF_PREFIX_RM_OLD_RCV)) | |
1874 | bgp_route_refresh_send(peer, afi, safi, | |
1875 | ORF_TYPE_PREFIX_OLD, | |
1876 | REFRESH_IMMEDIATE, 0); | |
1877 | } | |
1878 | } | |
d62a17ae | 1879 | |
1880 | /* First update is deferred until ORF or ROUTE-REFRESH is received */ | |
05c7a1cc QY |
1881 | FOREACH_AFI_SAFI (afi, safi) { |
1882 | if (CHECK_FLAG(peer->af_cap[afi][safi], | |
1883 | PEER_CAP_ORF_PREFIX_RM_ADV)) | |
d62a17ae | 1884 | if (CHECK_FLAG(peer->af_cap[afi][safi], |
05c7a1cc QY |
1885 | PEER_CAP_ORF_PREFIX_SM_RCV) |
1886 | || CHECK_FLAG(peer->af_cap[afi][safi], | |
1887 | PEER_CAP_ORF_PREFIX_SM_OLD_RCV)) | |
1888 | SET_FLAG(peer->af_sflags[afi][safi], | |
1889 | PEER_STATUS_ORF_WAIT_REFRESH); | |
1890 | } | |
d62a17ae | 1891 | |
1892 | bgp_announce_peer(peer); | |
1893 | ||
1894 | /* Start the route advertisement timer to send updates to the peer - if | |
1895 | * BGP | |
1896 | * is not in read-only mode. If it is, the timer will be started at the | |
1897 | * end | |
1898 | * of read-only mode. | |
1899 | */ | |
1900 | if (!bgp_update_delay_active(peer->bgp)) { | |
1901 | BGP_TIMER_OFF(peer->t_routeadv); | |
1902 | BGP_TIMER_ON(peer->t_routeadv, bgp_routeadv_timer, 0); | |
1903 | } | |
718e3744 | 1904 | |
d62a17ae | 1905 | if (peer->doppelganger && (peer->doppelganger->status != Deleted)) { |
1906 | if (bgp_debug_neighbor_events(peer)) | |
1907 | zlog_debug( | |
1908 | "[Event] Deleting stub connection for peer %s", | |
1909 | peer->host); | |
1910 | ||
1911 | if (peer->doppelganger->status > Active) | |
1912 | bgp_notify_send(peer->doppelganger, BGP_NOTIFY_CEASE, | |
1913 | BGP_NOTIFY_CEASE_COLLISION_RESOLUTION); | |
1914 | else | |
1915 | peer_delete(peer->doppelganger); | |
718e3744 | 1916 | } |
1917 | ||
19bd3dff DS |
1918 | /* |
1919 | * If we are replacing the old peer for a doppelganger | |
1920 | * then switch it around in the bgp->peerhash | |
1921 | * the doppelgangers su and this peer's su are the same | |
1922 | * so the hash_release is the same for either. | |
1923 | */ | |
1924 | hash_release(peer->bgp->peerhash, peer); | |
1925 | hash_get(peer->bgp->peerhash, peer, hash_alloc_intern); | |
1926 | ||
d62a17ae | 1927 | bgp_bfd_register_peer(peer); |
1928 | return ret; | |
718e3744 | 1929 | } |
1930 | ||
1931 | /* Keepalive packet is received. */ | |
d62a17ae | 1932 | static int bgp_fsm_keepalive(struct peer *peer) |
718e3744 | 1933 | { |
d62a17ae | 1934 | BGP_TIMER_OFF(peer->t_holdtime); |
1935 | return 0; | |
718e3744 | 1936 | } |
1937 | ||
1938 | /* Update packet is received. */ | |
d62a17ae | 1939 | static int bgp_fsm_update(struct peer *peer) |
718e3744 | 1940 | { |
d62a17ae | 1941 | BGP_TIMER_OFF(peer->t_holdtime); |
1942 | return 0; | |
718e3744 | 1943 | } |
1944 | ||
1945 | /* This is empty event. */ | |
d62a17ae | 1946 | static int bgp_ignore(struct peer *peer) |
718e3744 | 1947 | { |
af4c2728 | 1948 | flog_err( |
e50f7cfd | 1949 | EC_BGP_FSM, |
d62a17ae | 1950 | "%s [FSM] Ignoring event %s in state %s, prior events %s, %s, fd %d", |
1951 | peer->host, bgp_event_str[peer->cur_event], | |
1952 | lookup_msg(bgp_status_msg, peer->status, NULL), | |
1953 | bgp_event_str[peer->last_event], | |
1954 | bgp_event_str[peer->last_major_event], peer->fd); | |
1955 | return 0; | |
718e3744 | 1956 | } |
6b0655a2 | 1957 | |
6403814c | 1958 | /* This is to handle unexpected events.. */ |
d62a17ae | 1959 | static int bgp_fsm_exeption(struct peer *peer) |
6403814c | 1960 | { |
af4c2728 | 1961 | flog_err( |
e50f7cfd | 1962 | EC_BGP_FSM, |
d62a17ae | 1963 | "%s [FSM] Unexpected event %s in state %s, prior events %s, %s, fd %d", |
1964 | peer->host, bgp_event_str[peer->cur_event], | |
1965 | lookup_msg(bgp_status_msg, peer->status, NULL), | |
1966 | bgp_event_str[peer->last_event], | |
1967 | bgp_event_str[peer->last_major_event], peer->fd); | |
1968 | return (bgp_stop(peer)); | |
6403814c DS |
1969 | } |
1970 | ||
fc04a677 | 1971 | void bgp_fsm_event_update(struct peer *peer, int valid) |
fc9a856f | 1972 | { |
d62a17ae | 1973 | if (!peer) |
1974 | return; | |
1975 | ||
1976 | switch (peer->status) { | |
1977 | case Idle: | |
1978 | if (valid) | |
1979 | BGP_EVENT_ADD(peer, BGP_Start); | |
1980 | break; | |
1981 | case Connect: | |
1982 | if (!valid) { | |
1983 | BGP_TIMER_OFF(peer->t_connect); | |
1984 | BGP_EVENT_ADD(peer, TCP_fatal_error); | |
1985 | } | |
1986 | break; | |
1987 | case Active: | |
1988 | if (valid) { | |
1989 | BGP_TIMER_OFF(peer->t_connect); | |
1990 | BGP_EVENT_ADD(peer, ConnectRetry_timer_expired); | |
1991 | } | |
1992 | break; | |
1993 | case OpenSent: | |
1994 | case OpenConfirm: | |
1995 | case Established: | |
1996 | if (!valid && (peer->gtsm_hops == 1)) | |
1997 | BGP_EVENT_ADD(peer, TCP_fatal_error); | |
1998 | case Clearing: | |
1999 | case Deleted: | |
2000 | default: | |
2001 | break; | |
fc9a856f | 2002 | } |
fc9a856f DS |
2003 | } |
2004 | ||
718e3744 | 2005 | /* Finite State Machine structure */ |
fda1d3e0 | 2006 | static const struct { |
d62a17ae | 2007 | int (*func)(struct peer *); |
2008 | int next_state; | |
2009 | } FSM[BGP_STATUS_MAX - 1][BGP_EVENTS_MAX - 1] = { | |
2010 | { | |
2011 | /* Idle state: In Idle state, all events other than BGP_Start is | |
2012 | ignored. With BGP_Start event, finite state machine calls | |
2013 | bgp_start(). */ | |
2014 | {bgp_start, Connect}, /* BGP_Start */ | |
2015 | {bgp_stop, Idle}, /* BGP_Stop */ | |
2016 | {bgp_stop, Idle}, /* TCP_connection_open */ | |
2017 | {bgp_stop, Idle}, /* TCP_connection_closed */ | |
2018 | {bgp_ignore, Idle}, /* TCP_connection_open_failed */ | |
2019 | {bgp_stop, Idle}, /* TCP_fatal_error */ | |
2020 | {bgp_ignore, Idle}, /* ConnectRetry_timer_expired */ | |
2021 | {bgp_ignore, Idle}, /* Hold_Timer_expired */ | |
2022 | {bgp_ignore, Idle}, /* KeepAlive_timer_expired */ | |
2023 | {bgp_ignore, Idle}, /* Receive_OPEN_message */ | |
2024 | {bgp_ignore, Idle}, /* Receive_KEEPALIVE_message */ | |
2025 | {bgp_ignore, Idle}, /* Receive_UPDATE_message */ | |
2026 | {bgp_ignore, Idle}, /* Receive_NOTIFICATION_message */ | |
2027 | {bgp_ignore, Idle}, /* Clearing_Completed */ | |
2028 | }, | |
2029 | { | |
2030 | /* Connect */ | |
2031 | {bgp_ignore, Connect}, /* BGP_Start */ | |
2032 | {bgp_stop, Idle}, /* BGP_Stop */ | |
2033 | {bgp_connect_success, OpenSent}, /* TCP_connection_open */ | |
2034 | {bgp_stop, Idle}, /* TCP_connection_closed */ | |
2035 | {bgp_connect_fail, Active}, /* TCP_connection_open_failed */ | |
2036 | {bgp_connect_fail, Idle}, /* TCP_fatal_error */ | |
2037 | {bgp_reconnect, Connect}, /* ConnectRetry_timer_expired */ | |
2038 | {bgp_fsm_exeption, Idle}, /* Hold_Timer_expired */ | |
2039 | {bgp_fsm_exeption, Idle}, /* KeepAlive_timer_expired */ | |
2040 | {bgp_fsm_exeption, Idle}, /* Receive_OPEN_message */ | |
2041 | {bgp_fsm_exeption, Idle}, /* Receive_KEEPALIVE_message */ | |
2042 | {bgp_fsm_exeption, Idle}, /* Receive_UPDATE_message */ | |
2043 | {bgp_stop, Idle}, /* Receive_NOTIFICATION_message */ | |
2044 | {bgp_fsm_exeption, Idle}, /* Clearing_Completed */ | |
2045 | }, | |
2046 | { | |
2047 | /* Active, */ | |
2048 | {bgp_ignore, Active}, /* BGP_Start */ | |
2049 | {bgp_stop, Idle}, /* BGP_Stop */ | |
2050 | {bgp_connect_success, OpenSent}, /* TCP_connection_open */ | |
2051 | {bgp_stop, Idle}, /* TCP_connection_closed */ | |
2052 | {bgp_ignore, Active}, /* TCP_connection_open_failed */ | |
2053 | {bgp_fsm_exeption, Idle}, /* TCP_fatal_error */ | |
2054 | {bgp_start, Connect}, /* ConnectRetry_timer_expired */ | |
2055 | {bgp_fsm_exeption, Idle}, /* Hold_Timer_expired */ | |
2056 | {bgp_fsm_exeption, Idle}, /* KeepAlive_timer_expired */ | |
2057 | {bgp_fsm_exeption, Idle}, /* Receive_OPEN_message */ | |
2058 | {bgp_fsm_exeption, Idle}, /* Receive_KEEPALIVE_message */ | |
2059 | {bgp_fsm_exeption, Idle}, /* Receive_UPDATE_message */ | |
2060 | {bgp_fsm_exeption, Idle}, /* Receive_NOTIFICATION_message */ | |
2061 | {bgp_fsm_exeption, Idle}, /* Clearing_Completed */ | |
2062 | }, | |
2063 | { | |
2064 | /* OpenSent, */ | |
2065 | {bgp_ignore, OpenSent}, /* BGP_Start */ | |
2066 | {bgp_stop, Idle}, /* BGP_Stop */ | |
2067 | {bgp_stop, Active}, /* TCP_connection_open */ | |
2068 | {bgp_stop, Active}, /* TCP_connection_closed */ | |
2069 | {bgp_stop, Active}, /* TCP_connection_open_failed */ | |
2070 | {bgp_stop, Active}, /* TCP_fatal_error */ | |
2071 | {bgp_fsm_exeption, Idle}, /* ConnectRetry_timer_expired */ | |
2072 | {bgp_fsm_holdtime_expire, Idle}, /* Hold_Timer_expired */ | |
2073 | {bgp_fsm_exeption, Idle}, /* KeepAlive_timer_expired */ | |
2074 | {bgp_fsm_open, OpenConfirm}, /* Receive_OPEN_message */ | |
2075 | {bgp_fsm_event_error, Idle}, /* Receive_KEEPALIVE_message */ | |
2076 | {bgp_fsm_event_error, Idle}, /* Receive_UPDATE_message */ | |
53b4aaec | 2077 | {bgp_fsm_event_error, Idle}, /* Receive_NOTIFICATION_message */ |
d62a17ae | 2078 | {bgp_fsm_exeption, Idle}, /* Clearing_Completed */ |
2079 | }, | |
2080 | { | |
2081 | /* OpenConfirm, */ | |
2082 | {bgp_ignore, OpenConfirm}, /* BGP_Start */ | |
2083 | {bgp_stop, Idle}, /* BGP_Stop */ | |
2084 | {bgp_stop, Idle}, /* TCP_connection_open */ | |
2085 | {bgp_stop, Idle}, /* TCP_connection_closed */ | |
2086 | {bgp_stop, Idle}, /* TCP_connection_open_failed */ | |
2087 | {bgp_stop, Idle}, /* TCP_fatal_error */ | |
2088 | {bgp_fsm_exeption, Idle}, /* ConnectRetry_timer_expired */ | |
2089 | {bgp_fsm_holdtime_expire, Idle}, /* Hold_Timer_expired */ | |
2090 | {bgp_ignore, OpenConfirm}, /* KeepAlive_timer_expired */ | |
2091 | {bgp_fsm_exeption, Idle}, /* Receive_OPEN_message */ | |
2092 | {bgp_establish, Established}, /* Receive_KEEPALIVE_message */ | |
2093 | {bgp_fsm_exeption, Idle}, /* Receive_UPDATE_message */ | |
2094 | {bgp_stop_with_error, Idle}, /* Receive_NOTIFICATION_message */ | |
2095 | {bgp_fsm_exeption, Idle}, /* Clearing_Completed */ | |
2096 | }, | |
2097 | { | |
2098 | /* Established, */ | |
2099 | {bgp_ignore, Established}, /* BGP_Start */ | |
2100 | {bgp_stop, Clearing}, /* BGP_Stop */ | |
2101 | {bgp_stop, Clearing}, /* TCP_connection_open */ | |
2102 | {bgp_stop, Clearing}, /* TCP_connection_closed */ | |
2103 | {bgp_stop, Clearing}, /* TCP_connection_open_failed */ | |
2104 | {bgp_stop, Clearing}, /* TCP_fatal_error */ | |
2105 | {bgp_stop, Clearing}, /* ConnectRetry_timer_expired */ | |
2106 | {bgp_fsm_holdtime_expire, Clearing}, /* Hold_Timer_expired */ | |
03014d48 QY |
2107 | {bgp_ignore, Established}, /* KeepAlive_timer_expired */ |
2108 | {bgp_stop, Clearing}, /* Receive_OPEN_message */ | |
d62a17ae | 2109 | {bgp_fsm_keepalive, |
2110 | Established}, /* Receive_KEEPALIVE_message */ | |
2111 | {bgp_fsm_update, Established}, /* Receive_UPDATE_message */ | |
2112 | {bgp_stop_with_error, | |
2113 | Clearing}, /* Receive_NOTIFICATION_message */ | |
2114 | {bgp_fsm_exeption, Idle}, /* Clearing_Completed */ | |
2115 | }, | |
2116 | { | |
2117 | /* Clearing, */ | |
2118 | {bgp_ignore, Clearing}, /* BGP_Start */ | |
2119 | {bgp_stop, Clearing}, /* BGP_Stop */ | |
2120 | {bgp_stop, Clearing}, /* TCP_connection_open */ | |
2121 | {bgp_stop, Clearing}, /* TCP_connection_closed */ | |
2122 | {bgp_stop, Clearing}, /* TCP_connection_open_failed */ | |
2123 | {bgp_stop, Clearing}, /* TCP_fatal_error */ | |
2124 | {bgp_stop, Clearing}, /* ConnectRetry_timer_expired */ | |
2125 | {bgp_stop, Clearing}, /* Hold_Timer_expired */ | |
2126 | {bgp_stop, Clearing}, /* KeepAlive_timer_expired */ | |
2127 | {bgp_stop, Clearing}, /* Receive_OPEN_message */ | |
2128 | {bgp_stop, Clearing}, /* Receive_KEEPALIVE_message */ | |
2129 | {bgp_stop, Clearing}, /* Receive_UPDATE_message */ | |
2130 | {bgp_stop, Clearing}, /* Receive_NOTIFICATION_message */ | |
2131 | {bgp_clearing_completed, Idle}, /* Clearing_Completed */ | |
2132 | }, | |
2133 | { | |
2134 | /* Deleted, */ | |
2135 | {bgp_ignore, Deleted}, /* BGP_Start */ | |
2136 | {bgp_ignore, Deleted}, /* BGP_Stop */ | |
2137 | {bgp_ignore, Deleted}, /* TCP_connection_open */ | |
2138 | {bgp_ignore, Deleted}, /* TCP_connection_closed */ | |
2139 | {bgp_ignore, Deleted}, /* TCP_connection_open_failed */ | |
2140 | {bgp_ignore, Deleted}, /* TCP_fatal_error */ | |
2141 | {bgp_ignore, Deleted}, /* ConnectRetry_timer_expired */ | |
2142 | {bgp_ignore, Deleted}, /* Hold_Timer_expired */ | |
2143 | {bgp_ignore, Deleted}, /* KeepAlive_timer_expired */ | |
2144 | {bgp_ignore, Deleted}, /* Receive_OPEN_message */ | |
2145 | {bgp_ignore, Deleted}, /* Receive_KEEPALIVE_message */ | |
2146 | {bgp_ignore, Deleted}, /* Receive_UPDATE_message */ | |
2147 | {bgp_ignore, Deleted}, /* Receive_NOTIFICATION_message */ | |
2148 | {bgp_ignore, Deleted}, /* Clearing_Completed */ | |
2149 | }, | |
718e3744 | 2150 | }; |
2151 | ||
718e3744 | 2152 | /* Execute event process. */ |
d62a17ae | 2153 | int bgp_event(struct thread *thread) |
718e3744 | 2154 | { |
d62a17ae | 2155 | int event; |
2156 | struct peer *peer; | |
2157 | int ret; | |
718e3744 | 2158 | |
d62a17ae | 2159 | peer = THREAD_ARG(thread); |
2160 | event = THREAD_VAL(thread); | |
718e3744 | 2161 | |
d62a17ae | 2162 | ret = bgp_event_update(peer, event); |
1ff9a340 | 2163 | |
d62a17ae | 2164 | return (ret); |
1ff9a340 DS |
2165 | } |
2166 | ||
d62a17ae | 2167 | int bgp_event_update(struct peer *peer, int event) |
1ff9a340 | 2168 | { |
d62a17ae | 2169 | int next; |
2170 | int ret = 0; | |
2171 | struct peer *other; | |
2172 | int passive_conn = 0; | |
2173 | int dyn_nbr; | |
2174 | ||
d8151687 QY |
2175 | /* default return code */ |
2176 | ret = FSM_PEER_NOOP; | |
2177 | ||
d62a17ae | 2178 | other = peer->doppelganger; |
2179 | passive_conn = | |
2180 | (CHECK_FLAG(peer->sflags, PEER_STATUS_ACCEPT_PEER)) ? 1 : 0; | |
2181 | dyn_nbr = peer_dynamic_neighbor(peer); | |
2182 | ||
2183 | /* Logging this event. */ | |
2184 | next = FSM[peer->status - 1][event - 1].next_state; | |
2185 | ||
2186 | if (bgp_debug_neighbor_events(peer) && peer->status != next) | |
2187 | zlog_debug("%s [FSM] %s (%s->%s), fd %d", peer->host, | |
2188 | bgp_event_str[event], | |
2189 | lookup_msg(bgp_status_msg, peer->status, NULL), | |
2190 | lookup_msg(bgp_status_msg, next, NULL), peer->fd); | |
2191 | ||
2192 | peer->last_event = peer->cur_event; | |
2193 | peer->cur_event = event; | |
2194 | ||
2195 | /* Call function. */ | |
2196 | if (FSM[peer->status - 1][event - 1].func) | |
2197 | ret = (*(FSM[peer->status - 1][event - 1].func))(peer); | |
2198 | ||
d62a17ae | 2199 | if (ret >= 0) { |
2200 | if (ret == 1 && next == Established) { | |
2201 | /* The case when doppelganger swap accurred in | |
2202 | bgp_establish. | |
2203 | Update the peer pointer accordingly */ | |
d8151687 | 2204 | ret = FSM_PEER_TRANSFERRED; |
d62a17ae | 2205 | peer = other; |
2206 | } | |
2207 | ||
2208 | /* If status is changed. */ | |
d8151687 | 2209 | if (next != peer->status) { |
d62a17ae | 2210 | bgp_fsm_change_status(peer, next); |
2211 | ||
becedef6 QY |
2212 | /* |
2213 | * If we're going to ESTABLISHED then we executed a | |
2214 | * peer transfer. In this case we can either return | |
2215 | * FSM_PEER_TRANSITIONED or FSM_PEER_TRANSFERRED. | |
2216 | * Opting for TRANSFERRED since transfer implies | |
2217 | * session establishment. | |
2218 | */ | |
d8151687 QY |
2219 | if (ret != FSM_PEER_TRANSFERRED) |
2220 | ret = FSM_PEER_TRANSITIONED; | |
2221 | } | |
2222 | ||
d62a17ae | 2223 | /* Make sure timer is set. */ |
2224 | bgp_timer_set(peer); | |
2225 | ||
bea01226 | 2226 | } else { |
becedef6 QY |
2227 | /* |
2228 | * If we got a return value of -1, that means there was an | |
2229 | * error, restart the FSM. Since bgp_stop() was called on the | |
2230 | * peer. only a few fields are safe to access here. In any case | |
2231 | * we need to indicate that the peer was stopped in the return | |
2232 | * code. | |
2233 | */ | |
bea01226 | 2234 | if (!dyn_nbr && !passive_conn && peer->bgp) { |
af4c2728 | 2235 | flog_err( |
e50f7cfd | 2236 | EC_BGP_FSM, |
bea01226 QY |
2237 | "%s [FSM] Failure handling event %s in state %s, " |
2238 | "prior events %s, %s, fd %d", | |
2239 | peer->host, bgp_event_str[peer->cur_event], | |
2240 | lookup_msg(bgp_status_msg, peer->status, NULL), | |
2241 | bgp_event_str[peer->last_event], | |
2242 | bgp_event_str[peer->last_major_event], | |
2243 | peer->fd); | |
2244 | bgp_stop(peer); | |
2245 | bgp_fsm_change_status(peer, Idle); | |
2246 | bgp_timer_set(peer); | |
2247 | } | |
2248 | ret = FSM_PEER_STOPPED; | |
d62a17ae | 2249 | } |
bea01226 | 2250 | |
d62a17ae | 2251 | return ret; |
718e3744 | 2252 | } |
794b37d5 | 2253 | /* BGP GR Code */ |
2254 | ||
2255 | int bgp_gr_lookup_n_update_all_peer(struct bgp *bgp, | |
36235319 QY |
2256 | enum global_mode global_new_state, |
2257 | enum global_mode global_old_state) | |
794b37d5 | 2258 | { |
2259 | struct peer *peer = {0}; | |
2260 | struct listnode *node = {0}; | |
2261 | struct listnode *nnode = {0}; | |
36235319 | 2262 | enum peer_mode peer_old_state = PEER_INVALID; |
794b37d5 | 2263 | |
2264 | for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) { | |
2265 | ||
2266 | if (BGP_DEBUG(graceful_restart, GRACEFUL_RESTART)) | |
36235319 QY |
2267 | zlog_debug("%s [BGP_GR] Peer: (%s) :", __func__, |
2268 | peer->host); | |
794b37d5 | 2269 | |
2270 | peer_old_state = bgp_peer_gr_mode_get(peer); | |
2271 | ||
2272 | if (peer_old_state == PEER_GLOBAL_INHERIT) { | |
2273 | ||
36235319 QY |
2274 | /* |
2275 | *Reset only these peers and send a | |
2276 | *new open message with the change capabilities. | |
2277 | *Considering the mode to be "global_new_state" and | |
2278 | *do all operation accordingly | |
2279 | */ | |
794b37d5 | 2280 | |
2281 | switch (global_new_state) { | |
794b37d5 | 2282 | case GLOBAL_HELPER: |
794b37d5 | 2283 | BGP_PEER_GR_HELPER_ENABLE(peer); |
2284 | break; | |
2285 | case GLOBAL_GR: | |
794b37d5 | 2286 | BGP_PEER_GR_ENABLE(peer); |
2287 | break; | |
2288 | case GLOBAL_DISABLE: | |
794b37d5 | 2289 | BGP_PEER_GR_DISABLE(peer); |
2290 | break; | |
2291 | case GLOBAL_INVALID: | |
36235319 QY |
2292 | zlog_debug("%s [BGP_GR] GLOBAL_INVALID", |
2293 | __func__); | |
794b37d5 | 2294 | return BGP_ERR_GR_OPERATION_FAILED; |
794b37d5 | 2295 | } |
2296 | } | |
2297 | } | |
2298 | ||
2299 | bgp->global_gr_present_state = global_new_state; | |
2300 | ||
794b37d5 | 2301 | return BGP_GR_SUCCESS; |
2302 | } | |
2303 | ||
2ba1fe69 | 2304 | int bgp_gr_update_all(struct bgp *bgp, int global_gr_cmd) |
794b37d5 | 2305 | { |
2306 | enum global_mode global_new_state = GLOBAL_INVALID; | |
2307 | enum global_mode global_old_state = GLOBAL_INVALID; | |
2308 | ||
2309 | if (BGP_DEBUG(graceful_restart, GRACEFUL_RESTART)) | |
36235319 QY |
2310 | zlog_debug("%s [BGP_GR]START: global_gr_cmd :%s:", __func__, |
2311 | print_global_gr_cmd(global_gr_cmd)); | |
794b37d5 | 2312 | |
2313 | global_old_state = bgp_global_gr_mode_get(bgp); | |
2314 | ||
2ba1fe69 | 2315 | if (BGP_DEBUG(graceful_restart, GRACEFUL_RESTART)) |
36235319 QY |
2316 | zlog_debug("[BGP_GR] global_old_gr_state :%s:", |
2317 | print_global_gr_mode(global_old_state)); | |
794b37d5 | 2318 | |
2ba1fe69 | 2319 | if (global_old_state != GLOBAL_INVALID) { |
794b37d5 | 2320 | global_new_state = |
2ba1fe69 | 2321 | bgp->GLOBAL_GR_FSM[global_old_state][global_gr_cmd]; |
2322 | ||
2323 | if (BGP_DEBUG(graceful_restart, GRACEFUL_RESTART)) | |
36235319 QY |
2324 | zlog_debug("[BGP_GR] global_new_gr_state :%s:", |
2325 | print_global_gr_mode(global_new_state)); | |
794b37d5 | 2326 | } else { |
36235319 QY |
2327 | zlog_err("%s [BGP_GR] global_old_state == GLOBAL_INVALID", |
2328 | __func__); | |
794b37d5 | 2329 | return BGP_ERR_GR_OPERATION_FAILED; |
2330 | } | |
2331 | ||
2332 | if (global_new_state == GLOBAL_INVALID) { | |
36235319 QY |
2333 | zlog_err("%s [BGP_GR] global_new_state == GLOBAL_INVALID", |
2334 | __func__); | |
794b37d5 | 2335 | return BGP_ERR_GR_INVALID_CMD; |
2336 | } | |
2337 | if (global_new_state == global_old_state) { | |
2338 | /* Trace msg */ | |
2ba1fe69 | 2339 | if (BGP_DEBUG(graceful_restart, GRACEFUL_RESTART)) |
2340 | zlog_debug( | |
36235319 QY |
2341 | "%s [BGP_GR] global_new_state == global_old_state :%s", |
2342 | __func__, | |
2343 | print_global_gr_mode(global_new_state)); | |
794b37d5 | 2344 | return BGP_GR_NO_OPERATION; |
2345 | } | |
2346 | ||
36235319 QY |
2347 | return bgp_gr_lookup_n_update_all_peer(bgp, global_new_state, |
2348 | global_old_state); | |
794b37d5 | 2349 | } |
2350 | ||
2ba1fe69 | 2351 | const char *print_peer_gr_mode(enum peer_mode pr_mode) |
2352 | { | |
2353 | const char *peer_gr_mode = NULL; | |
2354 | ||
2355 | switch (pr_mode) { | |
2356 | case PEER_HELPER: | |
2357 | peer_gr_mode = "PEER_HELPER"; | |
2358 | break; | |
2359 | case PEER_GR: | |
2360 | peer_gr_mode = "PEER_GR"; | |
2361 | break; | |
2362 | case PEER_DISABLE: | |
2363 | peer_gr_mode = "PEER_DISABLE"; | |
2364 | break; | |
2365 | case PEER_INVALID: | |
2366 | peer_gr_mode = "PEER_INVALID"; | |
2367 | break; | |
2368 | case PEER_GLOBAL_INHERIT: | |
2369 | peer_gr_mode = "PEER_GLOBAL_INHERIT"; | |
2370 | break; | |
2371 | } | |
2372 | ||
2373 | return peer_gr_mode; | |
2374 | } | |
2375 | ||
2376 | const char *print_peer_gr_cmd(enum peer_gr_command pr_gr_cmd) | |
2377 | { | |
2378 | const char *peer_gr_cmd = NULL; | |
2379 | ||
2380 | switch (pr_gr_cmd) { | |
2381 | case PEER_GR_CMD: | |
2382 | peer_gr_cmd = "PEER_GR_CMD"; | |
2383 | break; | |
2384 | case NO_PEER_GR_CMD: | |
2385 | peer_gr_cmd = "NO_PEER_GR_CMD"; | |
2386 | break; | |
2387 | case PEER_DISABLE_CMD: | |
2388 | peer_gr_cmd = "PEER_GR_CMD"; | |
2389 | break; | |
2390 | case NO_PEER_DISABLE_CMD: | |
2391 | peer_gr_cmd = "NO_PEER_GR_CMD"; | |
2392 | break; | |
2393 | case PEER_HELPER_CMD: | |
2394 | peer_gr_cmd = "PEER_HELPER_CMD"; | |
2395 | break; | |
2396 | case NO_PEER_HELPER_CMD: | |
2397 | peer_gr_cmd = "NO_PEER_HELPER_CMD"; | |
2398 | break; | |
2399 | } | |
2400 | ||
2401 | return peer_gr_cmd; | |
2402 | } | |
2403 | ||
2404 | const char *print_global_gr_mode(enum global_mode gl_mode) | |
2405 | { | |
2406 | const char *global_gr_mode = NULL; | |
2407 | ||
2408 | switch (gl_mode) { | |
2409 | case GLOBAL_HELPER: | |
2410 | global_gr_mode = "GLOBAL_HELPER"; | |
2411 | break; | |
2412 | case GLOBAL_GR: | |
2413 | global_gr_mode = "GLOBAL_GR"; | |
2414 | break; | |
2415 | case GLOBAL_DISABLE: | |
2416 | global_gr_mode = "GLOBAL_DISABLE"; | |
2417 | break; | |
2418 | case GLOBAL_INVALID: | |
2419 | global_gr_mode = "GLOBAL_INVALID"; | |
2420 | break; | |
2421 | } | |
2422 | ||
2423 | return global_gr_mode; | |
2424 | } | |
2425 | ||
2426 | const char *print_global_gr_cmd(enum global_gr_command gl_gr_cmd) | |
2427 | { | |
2428 | const char *global_gr_cmd = NULL; | |
2429 | ||
2430 | switch (gl_gr_cmd) { | |
2431 | case GLOBAL_GR_CMD: | |
2432 | global_gr_cmd = "GLOBAL_GR_CMD"; | |
2433 | break; | |
2434 | case NO_GLOBAL_GR_CMD: | |
2435 | global_gr_cmd = "NO_GLOBAL_GR_CMD"; | |
2436 | break; | |
2437 | case GLOBAL_DISABLE_CMD: | |
2438 | global_gr_cmd = "GLOBAL_DISABLE_CMD"; | |
2439 | break; | |
2440 | case NO_GLOBAL_DISABLE_CMD: | |
2441 | global_gr_cmd = "NO_GLOBAL_DISABLE_CMD"; | |
2442 | break; | |
2443 | } | |
2444 | ||
2445 | return global_gr_cmd; | |
2446 | } | |
2447 | ||
794b37d5 | 2448 | enum global_mode bgp_global_gr_mode_get(struct bgp *bgp) |
2449 | { | |
2450 | return bgp->global_gr_present_state; | |
2451 | } | |
2452 | ||
2453 | enum peer_mode bgp_peer_gr_mode_get(struct peer *peer) | |
2454 | { | |
2455 | return peer->peer_gr_present_state; | |
2456 | } | |
2457 | ||
36235319 | 2458 | int bgp_neighbor_graceful_restart(struct peer *peer, int peer_gr_cmd) |
794b37d5 | 2459 | { |
36235319 QY |
2460 | enum peer_mode peer_new_state = PEER_INVALID; |
2461 | enum peer_mode peer_old_state = PEER_INVALID; | |
794b37d5 | 2462 | struct bgp_peer_gr peer_state; |
2463 | int result = BGP_GR_FAILURE; | |
2464 | ||
2465 | /* | |
2466 | * fetch peer_old_state from peer structure also | |
2467 | * fetch global_old_state from bgp structure, | |
2468 | * peer had a back pointer to bgpo struct ; | |
2469 | */ | |
2470 | ||
2471 | if (BGP_DEBUG(graceful_restart, GRACEFUL_RESTART)) | |
36235319 QY |
2472 | zlog_debug("%s [BGP_GR] START:Peer: (%s) : peer_gr_cmd :%s:", |
2473 | __func__, peer->host, | |
2474 | print_peer_gr_cmd(peer_gr_cmd)); | |
794b37d5 | 2475 | |
2476 | peer_old_state = bgp_peer_gr_mode_get(peer); | |
2477 | ||
2478 | if (peer_old_state == PEER_INVALID) { | |
36235319 | 2479 | zlog_debug("[BGP_GR] peer_old_state == Invalid state !"); |
794b37d5 | 2480 | return BGP_ERR_GR_OPERATION_FAILED; |
2481 | } | |
2482 | ||
2ba1fe69 | 2483 | peer_state = peer->PEER_GR_FSM[peer_old_state][peer_gr_cmd]; |
794b37d5 | 2484 | peer_new_state = peer_state.next_state; |
2485 | ||
2486 | if (peer_new_state == PEER_INVALID) { | |
794b37d5 | 2487 | zlog_debug( |
2ba1fe69 | 2488 | "[BGP_GR] Invalid bgp graceful restart command used !"); |
794b37d5 | 2489 | return BGP_ERR_GR_INVALID_CMD; |
2490 | } | |
2491 | ||
2492 | if (peer_new_state != peer_old_state) { | |
36235319 QY |
2493 | result = peer_state.action_fun(peer, peer_old_state, |
2494 | peer_new_state); | |
794b37d5 | 2495 | } else { |
2ba1fe69 | 2496 | if (BGP_DEBUG(graceful_restart, GRACEFUL_RESTART)) |
2497 | zlog_debug( | |
36235319 | 2498 | "[BGP_GR] peer_old_state == peer_new_state !"); |
794b37d5 | 2499 | return BGP_GR_NO_OPERATION; |
2500 | } | |
2501 | ||
2502 | if (result == BGP_GR_SUCCESS) { | |
2503 | ||
2504 | /* Update the mode i.e peer_new_state into the peer structure */ | |
2505 | peer->peer_gr_present_state = peer_new_state; | |
794b37d5 | 2506 | if (BGP_DEBUG(graceful_restart, GRACEFUL_RESTART)) |
36235319 QY |
2507 | zlog_debug( |
2508 | "[BGP_GR] Succesfully change the state of the peer to : %s : !", | |
2509 | print_peer_gr_mode(peer_new_state)); | |
794b37d5 | 2510 | |
2511 | return BGP_GR_SUCCESS; | |
2512 | } | |
2513 | ||
2514 | return result; | |
2515 | } | |
2516 | ||
36235319 QY |
2517 | unsigned int bgp_peer_gr_action(struct peer *peer, int old_peer_state, |
2518 | int new_peer_state) | |
794b37d5 | 2519 | { |
2520 | if (BGP_DEBUG(graceful_restart, GRACEFUL_RESTART)) | |
2521 | zlog_debug( | |
2ba1fe69 | 2522 | "%s [BGP_GR] Move peer from old_peer_state :%s: to new_peer_state :%s: !!!!", |
2523 | __func__, print_peer_gr_mode(old_peer_state), | |
2524 | print_peer_gr_mode(new_peer_state)); | |
794b37d5 | 2525 | |
2526 | int bgp_gr_global_mode = GLOBAL_INVALID; | |
2527 | unsigned int ret = BGP_GR_FAILURE; | |
2528 | ||
2529 | if (old_peer_state == new_peer_state) { | |
36235319 QY |
2530 | /* Nothing to do over here as the present and old state is the |
2531 | * same */ | |
794b37d5 | 2532 | return BGP_GR_NO_OPERATION; |
2533 | } | |
36235319 QY |
2534 | if ((old_peer_state == PEER_INVALID) |
2535 | || (new_peer_state == PEER_INVALID)) { | |
2536 | /* something bad happend , print error message */ | |
794b37d5 | 2537 | return BGP_ERR_GR_INVALID_CMD; |
2538 | } | |
2539 | ||
2540 | bgp_gr_global_mode = bgp_global_gr_mode_get(peer->bgp); | |
2541 | ||
36235319 QY |
2542 | if ((old_peer_state == PEER_GLOBAL_INHERIT) |
2543 | && (new_peer_state != PEER_GLOBAL_INHERIT)) { | |
794b37d5 | 2544 | |
2545 | /* fetch the Mode running in the Global state machine | |
2546 | *from the bgp structure into a variable called | |
2547 | *bgp_gr_global_mode | |
2548 | */ | |
2549 | ||
2550 | /* Here we are checking if the | |
2551 | *1. peer_new_state == global_mode == helper_mode | |
2552 | *2. peer_new_state == global_mode == GR_mode | |
2553 | *3. peer_new_state == global_mode == disabled_mode | |
2554 | */ | |
2555 | ||
2556 | BGP_PEER_GR_GLOBAL_INHERIT_UNSET(peer); | |
2557 | ||
2558 | if (new_peer_state == bgp_gr_global_mode) { | |
36235319 QY |
2559 | /*This is incremental updates i.e no tear down |
2560 | *of the existing session | |
2561 | *as the peer is already working in the same mode. | |
2562 | */ | |
794b37d5 | 2563 | ret = BGP_GR_SUCCESS; |
2564 | } else { | |
2565 | if (BGP_DEBUG(graceful_restart, GRACEFUL_RESTART)) | |
2566 | zlog_debug( | |
2ba1fe69 | 2567 | "[BGP_GR] Peer state changed from :%s ", |
2568 | print_peer_gr_mode(old_peer_state)); | |
794b37d5 | 2569 | |
2570 | bgp_peer_move_to_gr_mode(peer, new_peer_state); | |
2571 | ||
2572 | ret = BGP_GR_SUCCESS; | |
2573 | } | |
2574 | } | |
2575 | /* In the case below peer is going into Global inherit mode i.e. | |
2576 | * the peer would work as the mode configured at the global level | |
2577 | */ | |
36235319 QY |
2578 | else if ((new_peer_state == PEER_GLOBAL_INHERIT) |
2579 | && (old_peer_state != PEER_GLOBAL_INHERIT)) { | |
794b37d5 | 2580 | /* Here in this case it would be destructive |
2581 | * in all the cases except one case when, | |
2582 | * Global GR is configured Disabled | |
2583 | * and present_peer_state is not disable | |
2584 | */ | |
2585 | ||
2586 | BGP_PEER_GR_GLOBAL_INHERIT_SET(peer); | |
2587 | ||
2588 | if (old_peer_state == bgp_gr_global_mode) { | |
2589 | ||
2590 | /* This is incremental updates | |
2591 | *i.e no tear down of the existing session | |
2592 | *as the peer is already working in the same mode. | |
2593 | */ | |
2594 | ret = BGP_GR_SUCCESS; | |
2595 | } else { | |
2596 | /* Destructive always */ | |
2597 | /* Tear down the old session | |
2598 | * and send the new capability | |
2599 | * as per the bgp_gr_global_mode | |
2600 | */ | |
2601 | ||
2602 | if (BGP_DEBUG(graceful_restart, GRACEFUL_RESTART)) | |
36235319 QY |
2603 | zlog_debug( |
2604 | "[BGP_GR] Peer state changed from :%s", | |
2ba1fe69 | 2605 | print_peer_gr_mode(old_peer_state)); |
794b37d5 | 2606 | |
2607 | bgp_peer_move_to_gr_mode(peer, bgp_gr_global_mode); | |
2608 | ||
2609 | ret = BGP_GR_SUCCESS; | |
2610 | } | |
2611 | } else { | |
2612 | /* | |
2613 | *This else case, it include all the cases except --> | |
2614 | *(new_peer_state != Peer_Global) && | |
2615 | *( old_peer_state != Peer_Global ) | |
2616 | */ | |
2617 | if (BGP_DEBUG(graceful_restart, GRACEFUL_RESTART)) | |
2ba1fe69 | 2618 | zlog_debug("[BGP_GR] Peer state changed from :%s", |
36235319 | 2619 | print_peer_gr_mode(old_peer_state)); |
794b37d5 | 2620 | |
2621 | bgp_peer_move_to_gr_mode(peer, new_peer_state); | |
2622 | ||
2623 | ret = BGP_GR_SUCCESS; | |
2624 | } | |
2625 | ||
2626 | return ret; | |
2627 | } | |
2628 | ||
2629 | inline void bgp_peer_move_to_gr_mode(struct peer *peer, int new_state) | |
2630 | ||
2631 | { | |
2632 | int bgp_global_gr_mode = bgp_global_gr_mode_get(peer->bgp); | |
2633 | ||
2634 | switch (new_state) { | |
794b37d5 | 2635 | case PEER_HELPER: |
2636 | BGP_PEER_GR_HELPER_ENABLE(peer); | |
2637 | break; | |
794b37d5 | 2638 | case PEER_GR: |
2639 | BGP_PEER_GR_ENABLE(peer); | |
2640 | break; | |
794b37d5 | 2641 | case PEER_DISABLE: |
2642 | BGP_PEER_GR_DISABLE(peer); | |
2643 | break; | |
794b37d5 | 2644 | case PEER_GLOBAL_INHERIT: |
2645 | BGP_PEER_GR_GLOBAL_INHERIT_SET(peer); | |
2646 | ||
2647 | if (bgp_global_gr_mode == GLOBAL_HELPER) { | |
2648 | BGP_PEER_GR_HELPER_ENABLE(peer); | |
36235319 | 2649 | } else if (bgp_global_gr_mode == GLOBAL_GR) { |
794b37d5 | 2650 | BGP_PEER_GR_ENABLE(peer); |
2651 | } else if (bgp_global_gr_mode == GLOBAL_DISABLE) { | |
2652 | BGP_PEER_GR_DISABLE(peer); | |
2653 | } else { | |
2ba1fe69 | 2654 | zlog_err( |
36235319 | 2655 | "[BGP_GR] Default switch inherit mode ::: SOMETHING IS WRONG !!!"); |
794b37d5 | 2656 | } |
2657 | break; | |
2658 | default: | |
36235319 QY |
2659 | zlog_err( |
2660 | "[BGP_GR] Default switch mode ::: SOMETHING IS WRONG !!!"); | |
794b37d5 | 2661 | break; |
2662 | } | |
2663 | if (BGP_DEBUG(graceful_restart, GRACEFUL_RESTART)) | |
2ba1fe69 | 2664 | zlog_debug("[BGP_GR] Peer state changed --to--> : %d : !", |
36235319 | 2665 | new_state); |
794b37d5 | 2666 | } |
2667 | ||
2668 | void bgp_peer_gr_flags_update(struct peer *peer) | |
2669 | { | |
2670 | if (BGP_DEBUG(graceful_restart, GRACEFUL_RESTART)) | |
36235319 | 2671 | zlog_debug("%s [BGP_GR] called !", __func__); |
794b37d5 | 2672 | if (CHECK_FLAG(peer->peer_gr_new_status_flag, |
36235319 QY |
2673 | PEER_GRACEFUL_RESTART_NEW_STATE_HELPER)) |
2674 | SET_FLAG(peer->flags, PEER_FLAG_GRACEFUL_RESTART_HELPER); | |
794b37d5 | 2675 | else |
36235319 | 2676 | UNSET_FLAG(peer->flags, PEER_FLAG_GRACEFUL_RESTART_HELPER); |
794b37d5 | 2677 | if (BGP_DEBUG(graceful_restart, GRACEFUL_RESTART)) |
2678 | zlog_debug( | |
2ba1fe69 | 2679 | "[BGP_GR] Peer %s Flag PEER_FLAG_GRACEFUL_RESTART_HELPER : %s : !", |
794b37d5 | 2680 | peer->host, |
2986cac2 | 2681 | (CHECK_FLAG(peer->flags, |
36235319 QY |
2682 | PEER_FLAG_GRACEFUL_RESTART_HELPER) |
2683 | ? "Set" | |
2684 | : "UnSet")); | |
794b37d5 | 2685 | if (CHECK_FLAG(peer->peer_gr_new_status_flag, |
36235319 QY |
2686 | PEER_GRACEFUL_RESTART_NEW_STATE_RESTART)) |
2687 | SET_FLAG(peer->flags, PEER_FLAG_GRACEFUL_RESTART); | |
794b37d5 | 2688 | else |
36235319 | 2689 | UNSET_FLAG(peer->flags, PEER_FLAG_GRACEFUL_RESTART); |
794b37d5 | 2690 | if (BGP_DEBUG(graceful_restart, GRACEFUL_RESTART)) |
2691 | zlog_debug( | |
2ba1fe69 | 2692 | "[BGP_GR] Peer %s Flag PEER_FLAG_GRACEFUL_RESTART : %s : !", |
794b37d5 | 2693 | peer->host, |
36235319 QY |
2694 | (CHECK_FLAG(peer->flags, PEER_FLAG_GRACEFUL_RESTART) |
2695 | ? "Set" | |
2696 | : "UnSet")); | |
794b37d5 | 2697 | if (CHECK_FLAG(peer->peer_gr_new_status_flag, |
36235319 | 2698 | PEER_GRACEFUL_RESTART_NEW_STATE_INHERIT)) |
2986cac2 | 2699 | SET_FLAG(peer->flags, |
36235319 | 2700 | PEER_FLAG_GRACEFUL_RESTART_GLOBAL_INHERIT); |
794b37d5 | 2701 | else |
2986cac2 | 2702 | UNSET_FLAG(peer->flags, |
36235319 | 2703 | PEER_FLAG_GRACEFUL_RESTART_GLOBAL_INHERIT); |
794b37d5 | 2704 | if (BGP_DEBUG(graceful_restart, GRACEFUL_RESTART)) |
2705 | zlog_debug( | |
36235319 | 2706 | "[BGP_GR] Peer %s Flag PEER_FLAG_GRACEFUL_RESTART_GLOBAL_INHERIT : %s : !", |
794b37d5 | 2707 | peer->host, |
2986cac2 | 2708 | (CHECK_FLAG(peer->flags, |
36235319 QY |
2709 | PEER_FLAG_GRACEFUL_RESTART_GLOBAL_INHERIT) |
2710 | ? "Set" | |
2711 | : "UnSet")); | |
d7b3cda6 | 2712 | |
36235319 QY |
2713 | if (!CHECK_FLAG(peer->flags, PEER_FLAG_GRACEFUL_RESTART) |
2714 | && !CHECK_FLAG(peer->flags, PEER_FLAG_GRACEFUL_RESTART_HELPER)) { | |
2715 | zlog_debug("[BGP_GR] Peer %s UNSET PEER_STATUS_NSF_MODE!", | |
2716 | peer->host); | |
d7b3cda6 | 2717 | |
2718 | UNSET_FLAG(peer->sflags, PEER_STATUS_NSF_MODE); | |
2719 | ||
36235319 | 2720 | if (CHECK_FLAG(peer->sflags, PEER_STATUS_NSF_WAIT)) { |
d7b3cda6 | 2721 | |
2722 | peer_nsf_stop(peer); | |
2723 | zlog_debug( | |
2ba1fe69 | 2724 | "[BGP_GR] Peer %s UNSET PEER_STATUS_NSF_WAIT!", |
d7b3cda6 | 2725 | peer->host); |
2726 | } | |
2727 | } | |
794b37d5 | 2728 | } |