2 * Implements packet I/O in a pthread.
3 * Copyright (C) 2017 Cumulus Networks
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; see the file COPYING; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
22 /* clang-format off */
24 #include <pthread.h> // for pthread_mutex_unlock, pthread_mutex_lock
26 #include "frr_pthread.h" // for frr_pthread_get, frr_pthread
27 #include "linklist.h" // for list_delete, list_delete_all_node, lis...
28 #include "log.h" // for zlog_debug, safe_strerror, zlog_err
29 #include "memory.h" // for MTYPE_TMP, XCALLOC, XFREE
30 #include "network.h" // for ERRNO_IO_RETRY
31 #include "stream.h" // for stream_get_endp, stream_getw_from, str...
32 #include "ringbuf.h" // for ringbuf_remain, ringbuf_peek, ringbuf_...
33 #include "thread.h" // for THREAD_OFF, THREAD_ARG, thread, thread...
34 #include "zassert.h" // for assert
36 #include "bgpd/bgp_io.h"
37 #include "bgpd/bgp_debug.h" // for bgp_debug_neighbor_events, bgp_type_str
38 #include "bgpd/bgp_fsm.h" // for BGP_EVENT_ADD, bgp_event
39 #include "bgpd/bgp_packet.h" // for bgp_notify_send_with_data, bgp_notify...
40 #include "bgpd/bgpd.h" // for peer, BGP_MARKER_SIZE, bgp_master, bm
43 /* forward declarations */
44 static uint16_t bgp_write(struct peer
*);
45 static uint16_t bgp_read(struct peer
*);
46 static int bgp_process_writes(struct thread
*);
47 static int bgp_process_reads(struct thread
*);
48 static bool validate_header(struct peer
*);
50 /* generic i/o status codes */
51 #define BGP_IO_TRANS_ERR (1 << 0) // EAGAIN or similar occurred
52 #define BGP_IO_FATAL_ERR (1 << 1) // some kind of fatal TCP error
54 /* Thread external API ----------------------------------------------------- */
56 void bgp_writes_on(struct peer
*peer
)
58 struct frr_pthread
*fpt
= frr_pthread_get(PTHREAD_IO
);
61 assert(peer
->status
!= Deleted
);
64 assert(peer
->ibuf_work
);
65 assert(!peer
->t_connect_check_r
);
66 assert(!peer
->t_connect_check_w
);
69 thread_add_write(fpt
->master
, bgp_process_writes
, peer
, peer
->fd
,
71 SET_FLAG(peer
->thread_flags
, PEER_THREAD_WRITES_ON
);
74 void bgp_writes_off(struct peer
*peer
)
76 struct frr_pthread
*fpt
= frr_pthread_get(PTHREAD_IO
);
79 thread_cancel_async(fpt
->master
, &peer
->t_write
, NULL
);
80 THREAD_OFF(peer
->t_generate_updgrp_packets
);
82 UNSET_FLAG(peer
->thread_flags
, PEER_THREAD_WRITES_ON
);
85 void bgp_reads_on(struct peer
*peer
)
87 struct frr_pthread
*fpt
= frr_pthread_get(PTHREAD_IO
);
90 assert(peer
->status
!= Deleted
);
93 assert(peer
->ibuf_work
);
95 assert(!peer
->t_connect_check_r
);
96 assert(!peer
->t_connect_check_w
);
99 thread_add_read(fpt
->master
, bgp_process_reads
, peer
, peer
->fd
,
102 SET_FLAG(peer
->thread_flags
, PEER_THREAD_READS_ON
);
105 void bgp_reads_off(struct peer
*peer
)
107 struct frr_pthread
*fpt
= frr_pthread_get(PTHREAD_IO
);
108 assert(fpt
->running
);
110 thread_cancel_async(fpt
->master
, &peer
->t_read
, NULL
);
111 THREAD_OFF(peer
->t_process_packet
);
113 UNSET_FLAG(peer
->thread_flags
, PEER_THREAD_READS_ON
);
116 /* Thread internal functions ----------------------------------------------- */
119 * Called from I/O pthread when a file descriptor has become ready for writing.
121 static int bgp_process_writes(struct thread
*thread
)
123 static struct peer
*peer
;
124 peer
= THREAD_ARG(thread
);
132 struct frr_pthread
*fpt
= frr_pthread_get(PTHREAD_IO
);
134 pthread_mutex_lock(&peer
->io_mtx
);
136 status
= bgp_write(peer
);
137 reschedule
= (stream_fifo_head(peer
->obuf
) != NULL
);
139 pthread_mutex_unlock(&peer
->io_mtx
);
142 if (CHECK_FLAG(status
, BGP_IO_TRANS_ERR
)) {
146 if (CHECK_FLAG(status
, BGP_IO_FATAL_ERR
)) {
152 thread_add_write(fpt
->master
, bgp_process_writes
, peer
,
153 peer
->fd
, &peer
->t_write
);
155 BGP_TIMER_ON(peer
->t_generate_updgrp_packets
,
156 bgp_generate_updgrp_packets
, 0);
163 * Called from I/O pthread when a file descriptor has become ready for reading,
166 * We read as much data as possible, process as many packets as we can and
167 * place them on peer->ibuf for secondary processing by the main thread.
169 static int bgp_process_reads(struct thread
*thread
)
171 /* clang-format off */
172 static struct peer
*peer
; // peer to read from
173 uint16_t status
; // bgp_read status code
174 bool more
= true; // whether we got more data
175 bool fatal
= false; // whether fatal error occurred
176 bool added_pkt
= false; // whether we pushed onto ->ibuf
177 /* clang-format on */
179 peer
= THREAD_ARG(thread
);
181 if (peer
->fd
< 0 || bm
->terminating
)
184 struct frr_pthread
*fpt
= frr_pthread_get(PTHREAD_IO
);
186 pthread_mutex_lock(&peer
->io_mtx
);
188 status
= bgp_read(peer
);
190 pthread_mutex_unlock(&peer
->io_mtx
);
192 /* error checking phase */
193 if (CHECK_FLAG(status
, BGP_IO_TRANS_ERR
)) {
194 /* no problem; just don't process packets */
198 if (CHECK_FLAG(status
, BGP_IO_FATAL_ERR
)) {
199 /* problem; tear down session */
205 /* static buffer for transferring packets */
206 static unsigned char pktbuf
[BGP_MAX_PACKET_SIZE
];
207 /* shorter alias to peer's input buffer */
208 struct ringbuf
*ibw
= peer
->ibuf_work
;
209 /* packet size as given by header */
210 uint16_t pktsize
= 0;
212 /* check that we have enough data for a header */
213 if (ringbuf_remain(ibw
) < BGP_HEADER_SIZE
)
216 /* check that header is valid */
217 if (!validate_header(peer
)) {
222 /* header is valid; retrieve packet size */
223 ringbuf_peek(ibw
, BGP_MARKER_SIZE
, &pktsize
, sizeof(pktsize
));
225 pktsize
= ntohs(pktsize
);
227 /* if this fails we are seriously screwed */
228 assert(pktsize
<= BGP_MAX_PACKET_SIZE
);
231 * If we have that much data, chuck it into its own
232 * stream and append to input queue for processing.
234 if (ringbuf_remain(ibw
) >= pktsize
) {
235 struct stream
*pkt
= stream_new(pktsize
);
236 assert(ringbuf_get(ibw
, pktbuf
, pktsize
) == pktsize
);
237 stream_put(pkt
, pktbuf
, pktsize
);
239 pthread_mutex_lock(&peer
->io_mtx
);
241 stream_fifo_push(peer
->ibuf
, pkt
);
243 pthread_mutex_unlock(&peer
->io_mtx
);
250 assert(ringbuf_space(peer
->ibuf_work
) >= BGP_MAX_PACKET_SIZE
);
252 /* handle invalid header */
254 /* wipe buffer just in case someone screwed up */
255 ringbuf_wipe(peer
->ibuf_work
);
257 thread_add_read(fpt
->master
, bgp_process_reads
, peer
, peer
->fd
,
260 thread_add_timer_msec(bm
->master
, bgp_process_packet
,
261 peer
, 0, &peer
->t_process_packet
);
268 * Flush peer output buffer.
270 * This function pops packets off of peer->obuf and writes them to peer->fd.
271 * The amount of packets written is equal to the minimum of peer->wpkt_quanta
272 * and the number of packets on the output buffer, unless an error occurs.
274 * If write() returns an error, the appropriate FSM event is generated.
276 * The return value is equal to the number of packets written
277 * (which may be zero).
279 static uint16_t bgp_write(struct peer
*peer
)
284 int update_last_write
= 0;
285 unsigned int count
= 0;
288 uint32_t wpkt_quanta_old
;
290 wpkt_quanta_old
= atomic_load_explicit(&peer
->bgp
->wpkt_quanta
,
291 memory_order_relaxed
);
293 while (count
< wpkt_quanta_old
&& (s
= stream_fifo_head(peer
->obuf
))) {
296 writenum
= stream_get_endp(s
) - stream_get_getp(s
);
297 num
= write(peer
->fd
, STREAM_PNT(s
), writenum
);
300 if (!ERRNO_IO_RETRY(errno
)) {
301 BGP_EVENT_ADD(peer
, TCP_fatal_error
);
302 SET_FLAG(status
, BGP_IO_FATAL_ERR
);
304 SET_FLAG(status
, BGP_IO_TRANS_ERR
);
308 } else if (num
!= writenum
)
309 stream_forward_getp(s
, num
);
311 } while (num
!= writenum
);
313 /* Retrieve BGP packet type. */
314 stream_set_getp(s
, BGP_MARKER_SIZE
+ 2);
315 type
= stream_getc(s
);
319 atomic_fetch_add_explicit(&peer
->open_out
, 1,
320 memory_order_relaxed
);
323 atomic_fetch_add_explicit(&peer
->update_out
, 1,
324 memory_order_relaxed
);
328 atomic_fetch_add_explicit(&peer
->notify_out
, 1,
329 memory_order_relaxed
);
330 /* Double start timer. */
333 /* Overflow check. */
334 if (peer
->v_start
>= (60 * 2))
335 peer
->v_start
= (60 * 2);
338 * Handle Graceful Restart case where the state changes
339 * to Connect instead of Idle.
341 BGP_EVENT_ADD(peer
, BGP_Stop
);
344 case BGP_MSG_KEEPALIVE
:
345 atomic_fetch_add_explicit(&peer
->keepalive_out
, 1,
346 memory_order_relaxed
);
348 case BGP_MSG_ROUTE_REFRESH_NEW
:
349 case BGP_MSG_ROUTE_REFRESH_OLD
:
350 atomic_fetch_add_explicit(&peer
->refresh_out
, 1,
351 memory_order_relaxed
);
353 case BGP_MSG_CAPABILITY
:
354 atomic_fetch_add_explicit(&peer
->dynamic_cap_out
, 1,
355 memory_order_relaxed
);
361 stream_free(stream_fifo_pop(peer
->obuf
));
362 update_last_write
= 1;
367 * Update last_update if UPDATEs were written.
368 * Note: that these are only updated at end,
369 * not per message (i.e., per loop)
372 atomic_store_explicit(&peer
->last_update
, bgp_clock(),
373 memory_order_relaxed
);
375 /* If we TXed any flavor of packet */
376 if (update_last_write
)
377 atomic_store_explicit(&peer
->last_write
, bgp_clock(),
378 memory_order_relaxed
);
385 * Reads a chunk of data from peer->fd into peer->ibuf_work.
387 * @return status flag (see top-of-file)
389 static uint16_t bgp_read(struct peer
*peer
)
391 size_t readsize
; // how many bytes we want to read
392 ssize_t nbytes
; // how many bytes we actually read
394 static uint8_t ibw
[BGP_MAX_PACKET_SIZE
* BGP_READ_PACKET_MAX
];
396 readsize
= MIN(ringbuf_space(peer
->ibuf_work
), sizeof(ibw
));
397 nbytes
= read(peer
->fd
, ibw
, readsize
);
399 /* EAGAIN or EWOULDBLOCK; come back later */
400 if (nbytes
< 0 && ERRNO_IO_RETRY(errno
)) {
401 SET_FLAG(status
, BGP_IO_TRANS_ERR
);
402 /* Fatal error; tear down session */
403 } else if (nbytes
< 0) {
404 zlog_err("%s [Error] bgp_read_packet error: %s", peer
->host
,
405 safe_strerror(errno
));
407 if (peer
->status
== Established
) {
408 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_NSF_MODE
)) {
409 peer
->last_reset
= PEER_DOWN_NSF_CLOSE_SESSION
;
410 SET_FLAG(peer
->sflags
, PEER_STATUS_NSF_WAIT
);
412 peer
->last_reset
= PEER_DOWN_CLOSE_SESSION
;
415 BGP_EVENT_ADD(peer
, TCP_fatal_error
);
416 SET_FLAG(status
, BGP_IO_FATAL_ERR
);
417 /* Received EOF / TCP session closed */
418 } else if (nbytes
== 0) {
419 if (bgp_debug_neighbor_events(peer
))
420 zlog_debug("%s [Event] BGP connection closed fd %d",
421 peer
->host
, peer
->fd
);
423 if (peer
->status
== Established
) {
424 if (CHECK_FLAG(peer
->sflags
, PEER_STATUS_NSF_MODE
)) {
425 peer
->last_reset
= PEER_DOWN_NSF_CLOSE_SESSION
;
426 SET_FLAG(peer
->sflags
, PEER_STATUS_NSF_WAIT
);
428 peer
->last_reset
= PEER_DOWN_CLOSE_SESSION
;
431 BGP_EVENT_ADD(peer
, TCP_connection_closed
);
432 SET_FLAG(status
, BGP_IO_FATAL_ERR
);
434 assert(ringbuf_put(peer
->ibuf_work
, ibw
, nbytes
)
442 * Called after we have read a BGP packet header. Validates marker, message
443 * type and packet length. If any of these aren't correct, sends a notify.
445 * Assumes that there are at least BGP_HEADER_SIZE readable bytes in the input
448 static bool validate_header(struct peer
*peer
)
452 struct ringbuf
*pkt
= peer
->ibuf_work
;
454 static uint8_t m_correct
[BGP_MARKER_SIZE
] = {
455 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
456 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
457 uint8_t m_rx
[BGP_MARKER_SIZE
] = {0x00};
459 if (ringbuf_peek(pkt
, 0, m_rx
, BGP_MARKER_SIZE
) != BGP_MARKER_SIZE
)
462 if (memcmp(m_correct
, m_rx
, BGP_MARKER_SIZE
) != 0) {
463 bgp_notify_send(peer
, BGP_NOTIFY_HEADER_ERR
,
464 BGP_NOTIFY_HEADER_NOT_SYNC
);
468 /* Get size and type in network byte order. */
469 ringbuf_peek(pkt
, BGP_MARKER_SIZE
, &size
, sizeof(size
));
470 ringbuf_peek(pkt
, BGP_MARKER_SIZE
+ 2, &type
, sizeof(type
));
474 /* BGP type check. */
475 if (type
!= BGP_MSG_OPEN
&& type
!= BGP_MSG_UPDATE
476 && type
!= BGP_MSG_NOTIFY
&& type
!= BGP_MSG_KEEPALIVE
477 && type
!= BGP_MSG_ROUTE_REFRESH_NEW
478 && type
!= BGP_MSG_ROUTE_REFRESH_OLD
479 && type
!= BGP_MSG_CAPABILITY
) {
480 if (bgp_debug_neighbor_events(peer
))
481 zlog_debug("%s unknown message type 0x%02x", peer
->host
,
484 bgp_notify_send_with_data(peer
, BGP_NOTIFY_HEADER_ERR
,
485 BGP_NOTIFY_HEADER_BAD_MESTYPE
, &type
,
490 /* Minimum packet length check. */
491 if ((size
< BGP_HEADER_SIZE
) || (size
> BGP_MAX_PACKET_SIZE
)
492 || (type
== BGP_MSG_OPEN
&& size
< BGP_MSG_OPEN_MIN_SIZE
)
493 || (type
== BGP_MSG_UPDATE
&& size
< BGP_MSG_UPDATE_MIN_SIZE
)
494 || (type
== BGP_MSG_NOTIFY
&& size
< BGP_MSG_NOTIFY_MIN_SIZE
)
495 || (type
== BGP_MSG_KEEPALIVE
&& size
!= BGP_MSG_KEEPALIVE_MIN_SIZE
)
496 || (type
== BGP_MSG_ROUTE_REFRESH_NEW
497 && size
< BGP_MSG_ROUTE_REFRESH_MIN_SIZE
)
498 || (type
== BGP_MSG_ROUTE_REFRESH_OLD
499 && size
< BGP_MSG_ROUTE_REFRESH_MIN_SIZE
)
500 || (type
== BGP_MSG_CAPABILITY
501 && size
< BGP_MSG_CAPABILITY_MIN_SIZE
)) {
502 if (bgp_debug_neighbor_events(peer
)) {
503 zlog_debug("%s bad message length - %d for %s",
505 type
== 128 ? "ROUTE-REFRESH"
506 : bgp_type_str
[(int)type
]);
509 uint16_t nsize
= htons(size
);
511 bgp_notify_send_with_data(peer
, BGP_NOTIFY_HEADER_ERR
,
512 BGP_NOTIFY_HEADER_BAD_MESLEN
,
513 (unsigned char *)&nsize
, 2);