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