2 * Main implementation file for interface to Forwarding Plane Manager.
4 * Copyright (C) 2012 by Open Source Routing.
5 * Copyright (C) 2012 by Internet Systems Consortium, Inc. ("ISC")
7 * This file is part of GNU Zebra.
9 * GNU Zebra is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2, or (at your option) any
14 * GNU Zebra is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; see the file COPYING; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
34 #include "zebra/rib.h"
35 #include "zebra/zserv.h"
36 #include "zebra/zebra_ns.h"
37 #include "zebra/zebra_vrf.h"
38 #include "zebra/zebra_errors.h"
41 #include "zebra_fpm_private.h"
44 * Interval at which we attempt to connect to the FPM.
46 #define ZFPM_CONNECT_RETRY_IVL 5
49 * Sizes of outgoing and incoming stream buffers for writing/reading
52 #define ZFPM_OBUF_SIZE (2 * FPM_MAX_MSG_LEN)
53 #define ZFPM_IBUF_SIZE (FPM_MAX_MSG_LEN)
56 * The maximum number of times the FPM socket write callback can call
57 * 'write' before it yields.
59 #define ZFPM_MAX_WRITES_PER_RUN 10
62 * Interval over which we collect statistics.
64 #define ZFPM_STATS_IVL_SECS 10
67 * Structure that holds state for iterating over all route_node
68 * structures that are candidates for being communicated to the FPM.
70 typedef struct zfpm_rnodes_iter_t_
{
71 rib_tables_iter_t tables_iter
;
72 route_table_iter_t iter
;
78 typedef struct zfpm_stats_t_
{
79 unsigned long connect_calls
;
80 unsigned long connect_no_sock
;
82 unsigned long read_cb_calls
;
84 unsigned long write_cb_calls
;
85 unsigned long write_calls
;
86 unsigned long partial_writes
;
87 unsigned long max_writes_hit
;
88 unsigned long t_write_yields
;
90 unsigned long nop_deletes_skipped
;
91 unsigned long route_adds
;
92 unsigned long route_dels
;
94 unsigned long updates_triggered
;
95 unsigned long redundant_triggers
;
97 unsigned long dests_del_after_update
;
99 unsigned long t_conn_down_starts
;
100 unsigned long t_conn_down_dests_processed
;
101 unsigned long t_conn_down_yields
;
102 unsigned long t_conn_down_finishes
;
104 unsigned long t_conn_up_starts
;
105 unsigned long t_conn_up_dests_processed
;
106 unsigned long t_conn_up_yields
;
107 unsigned long t_conn_up_aborts
;
108 unsigned long t_conn_up_finishes
;
113 * States for the FPM state machine.
118 * In this state we are not yet ready to connect to the FPM. This
119 * can happen when this module is disabled, or if we're cleaning up
120 * after a connection has gone down.
125 * Ready to talk to the FPM and periodically trying to connect to
131 * In the middle of bringing up a TCP connection. Specifically,
132 * waiting for a connect() call to complete asynchronously.
134 ZFPM_STATE_CONNECTING
,
137 * TCP connection to the FPM is up.
139 ZFPM_STATE_ESTABLISHED
144 * Message format to be used to communicate with the FPM.
147 ZFPM_MSG_FORMAT_NONE
,
148 ZFPM_MSG_FORMAT_NETLINK
,
149 ZFPM_MSG_FORMAT_PROTOBUF
,
154 typedef struct zfpm_glob_t_
{
157 * True if the FPM module has been enabled.
162 * Message format to be used to communicate with the fpm.
164 zfpm_msg_format_e message_format
;
166 struct thread_master
*master
;
170 in_addr_t fpm_server
;
172 * Port on which the FPM is running.
177 * List of rib_dest_t structures to be processed
179 TAILQ_HEAD(zfpm_dest_q
, rib_dest_t_
) dest_q
;
182 * Stream socket to the FPM.
187 * Buffers for messages to/from the FPM.
195 struct thread
*t_connect
;
196 struct thread
*t_write
;
197 struct thread
*t_read
;
200 * Thread to clean up after the TCP connection to the FPM goes down
201 * and the state that belongs to it.
203 struct thread
*t_conn_down
;
206 zfpm_rnodes_iter_t iter
;
210 * Thread to take actions once the TCP conn to the FPM comes up, and
211 * the state that belongs to it.
213 struct thread
*t_conn_up
;
216 zfpm_rnodes_iter_t iter
;
219 unsigned long connect_calls
;
220 time_t last_connect_call_time
;
223 * Stats from the start of the current statistics interval up to
224 * now. These are the counters we typically update in the code.
229 * Statistics that were gathered in the last collection interval.
231 zfpm_stats_t last_ivl_stats
;
234 * Cumulative stats from the last clear to the start of the current
235 * statistics interval.
237 zfpm_stats_t cumulative_stats
;
240 * Stats interval timer.
242 struct thread
*t_stats
;
245 * If non-zero, the last time when statistics were cleared.
247 time_t last_stats_clear_time
;
251 static zfpm_glob_t zfpm_glob_space
;
252 static zfpm_glob_t
*zfpm_g
= &zfpm_glob_space
;
254 static int zfpm_trigger_update(struct route_node
*rn
, const char *reason
);
256 static int zfpm_read_cb(struct thread
*thread
);
257 static int zfpm_write_cb(struct thread
*thread
);
259 static void zfpm_set_state(zfpm_state_t state
, const char *reason
);
260 static void zfpm_start_connect_timer(const char *reason
);
261 static void zfpm_start_stats_timer(void);
264 * zfpm_thread_should_yield
266 static inline int zfpm_thread_should_yield(struct thread
*t
)
268 return thread_should_yield(t
);
274 static const char *zfpm_state_to_str(zfpm_state_t state
)
278 case ZFPM_STATE_IDLE
:
281 case ZFPM_STATE_ACTIVE
:
284 case ZFPM_STATE_CONNECTING
:
287 case ZFPM_STATE_ESTABLISHED
:
288 return "established";
296 * zfpm_get_elapsed_time
298 * Returns the time elapsed (in seconds) since the given time.
300 static time_t zfpm_get_elapsed_time(time_t reference
)
304 now
= monotime(NULL
);
306 if (now
< reference
) {
311 return now
- reference
;
315 * zfpm_rnodes_iter_init
317 static inline void zfpm_rnodes_iter_init(zfpm_rnodes_iter_t
*iter
)
319 memset(iter
, 0, sizeof(*iter
));
320 rib_tables_iter_init(&iter
->tables_iter
);
323 * This is a hack, but it makes implementing 'next' easier by
324 * ensuring that route_table_iter_next() will return NULL the first
327 route_table_iter_init(&iter
->iter
, NULL
);
328 route_table_iter_cleanup(&iter
->iter
);
332 * zfpm_rnodes_iter_next
334 static inline struct route_node
*zfpm_rnodes_iter_next(zfpm_rnodes_iter_t
*iter
)
336 struct route_node
*rn
;
337 struct route_table
*table
;
340 rn
= route_table_iter_next(&iter
->iter
);
345 * We've made our way through this table, go to the next one.
347 route_table_iter_cleanup(&iter
->iter
);
349 table
= rib_tables_iter_next(&iter
->tables_iter
);
354 route_table_iter_init(&iter
->iter
, table
);
361 * zfpm_rnodes_iter_pause
363 static inline void zfpm_rnodes_iter_pause(zfpm_rnodes_iter_t
*iter
)
365 route_table_iter_pause(&iter
->iter
);
369 * zfpm_rnodes_iter_cleanup
371 static inline void zfpm_rnodes_iter_cleanup(zfpm_rnodes_iter_t
*iter
)
373 route_table_iter_cleanup(&iter
->iter
);
374 rib_tables_iter_cleanup(&iter
->tables_iter
);
380 * Initialize a statistics block.
382 static inline void zfpm_stats_init(zfpm_stats_t
*stats
)
384 memset(stats
, 0, sizeof(*stats
));
390 static inline void zfpm_stats_reset(zfpm_stats_t
*stats
)
392 zfpm_stats_init(stats
);
398 static inline void zfpm_stats_copy(const zfpm_stats_t
*src
, zfpm_stats_t
*dest
)
400 memcpy(dest
, src
, sizeof(*dest
));
406 * Total up the statistics in two stats structures ('s1 and 's2') and
407 * return the result in the third argument, 'result'. Note that the
408 * pointer 'result' may be the same as 's1' or 's2'.
410 * For simplicity, the implementation below assumes that the stats
411 * structure is composed entirely of counters. This can easily be
412 * changed when necessary.
414 static void zfpm_stats_compose(const zfpm_stats_t
*s1
, const zfpm_stats_t
*s2
,
415 zfpm_stats_t
*result
)
417 const unsigned long *p1
, *p2
;
418 unsigned long *result_p
;
421 p1
= (const unsigned long *)s1
;
422 p2
= (const unsigned long *)s2
;
423 result_p
= (unsigned long *)result
;
425 num_counters
= (sizeof(zfpm_stats_t
) / sizeof(unsigned long));
427 for (i
= 0; i
< num_counters
; i
++) {
428 result_p
[i
] = p1
[i
] + p2
[i
];
435 static inline void zfpm_read_on(void)
437 assert(!zfpm_g
->t_read
);
438 assert(zfpm_g
->sock
>= 0);
440 thread_add_read(zfpm_g
->master
, zfpm_read_cb
, 0, zfpm_g
->sock
,
447 static inline void zfpm_write_on(void)
449 assert(!zfpm_g
->t_write
);
450 assert(zfpm_g
->sock
>= 0);
452 thread_add_write(zfpm_g
->master
, zfpm_write_cb
, 0, zfpm_g
->sock
,
459 static inline void zfpm_read_off(void)
461 THREAD_READ_OFF(zfpm_g
->t_read
);
467 static inline void zfpm_write_off(void)
469 THREAD_WRITE_OFF(zfpm_g
->t_write
);
473 * zfpm_conn_up_thread_cb
475 * Callback for actions to be taken when the connection to the FPM
478 static int zfpm_conn_up_thread_cb(struct thread
*thread
)
480 struct route_node
*rnode
;
481 zfpm_rnodes_iter_t
*iter
;
484 zfpm_g
->t_conn_up
= NULL
;
486 iter
= &zfpm_g
->t_conn_up_state
.iter
;
488 if (zfpm_g
->state
!= ZFPM_STATE_ESTABLISHED
) {
490 "Connection not up anymore, conn_up thread aborting");
491 zfpm_g
->stats
.t_conn_up_aborts
++;
495 while ((rnode
= zfpm_rnodes_iter_next(iter
))) {
496 dest
= rib_dest_from_rnode(rnode
);
499 zfpm_g
->stats
.t_conn_up_dests_processed
++;
500 zfpm_trigger_update(rnode
, NULL
);
506 if (!zfpm_thread_should_yield(thread
))
509 zfpm_g
->stats
.t_conn_up_yields
++;
510 zfpm_rnodes_iter_pause(iter
);
511 zfpm_g
->t_conn_up
= NULL
;
512 thread_add_timer_msec(zfpm_g
->master
, zfpm_conn_up_thread_cb
,
513 NULL
, 0, &zfpm_g
->t_conn_up
);
517 zfpm_g
->stats
.t_conn_up_finishes
++;
520 zfpm_rnodes_iter_cleanup(iter
);
527 * Called when the connection to the FPM comes up.
529 static void zfpm_connection_up(const char *detail
)
531 assert(zfpm_g
->sock
>= 0);
534 zfpm_set_state(ZFPM_STATE_ESTABLISHED
, detail
);
537 * Start thread to push existing routes to the FPM.
539 assert(!zfpm_g
->t_conn_up
);
541 zfpm_rnodes_iter_init(&zfpm_g
->t_conn_up_state
.iter
);
543 zfpm_debug("Starting conn_up thread");
544 zfpm_g
->t_conn_up
= NULL
;
545 thread_add_timer_msec(zfpm_g
->master
, zfpm_conn_up_thread_cb
, NULL
, 0,
547 zfpm_g
->stats
.t_conn_up_starts
++;
553 * Check if an asynchronous connect() to the FPM is complete.
555 static void zfpm_connect_check(void)
564 slen
= sizeof(status
);
565 ret
= getsockopt(zfpm_g
->sock
, SOL_SOCKET
, SO_ERROR
, (void *)&status
,
568 if (ret
>= 0 && status
== 0) {
569 zfpm_connection_up("async connect complete");
574 * getsockopt() failed or indicated an error on the socket.
579 zfpm_start_connect_timer("getsockopt() after async connect failed");
584 * zfpm_conn_down_thread_cb
586 * Callback that is invoked to clean up state after the TCP connection
587 * to the FPM goes down.
589 static int zfpm_conn_down_thread_cb(struct thread
*thread
)
591 struct route_node
*rnode
;
592 zfpm_rnodes_iter_t
*iter
;
595 assert(zfpm_g
->state
== ZFPM_STATE_IDLE
);
597 zfpm_g
->t_conn_down
= NULL
;
599 iter
= &zfpm_g
->t_conn_down_state
.iter
;
601 while ((rnode
= zfpm_rnodes_iter_next(iter
))) {
602 dest
= rib_dest_from_rnode(rnode
);
605 if (CHECK_FLAG(dest
->flags
, RIB_DEST_UPDATE_FPM
)) {
606 TAILQ_REMOVE(&zfpm_g
->dest_q
, dest
,
610 UNSET_FLAG(dest
->flags
, RIB_DEST_UPDATE_FPM
);
611 UNSET_FLAG(dest
->flags
, RIB_DEST_SENT_TO_FPM
);
613 zfpm_g
->stats
.t_conn_down_dests_processed
++;
616 * Check if the dest should be deleted.
624 if (!zfpm_thread_should_yield(thread
))
627 zfpm_g
->stats
.t_conn_down_yields
++;
628 zfpm_rnodes_iter_pause(iter
);
629 zfpm_g
->t_conn_down
= NULL
;
630 thread_add_timer_msec(zfpm_g
->master
, zfpm_conn_down_thread_cb
,
631 NULL
, 0, &zfpm_g
->t_conn_down
);
635 zfpm_g
->stats
.t_conn_down_finishes
++;
636 zfpm_rnodes_iter_cleanup(iter
);
639 * Start the process of connecting to the FPM again.
641 zfpm_start_connect_timer("cleanup complete");
646 * zfpm_connection_down
648 * Called when the connection to the FPM has gone down.
650 static void zfpm_connection_down(const char *detail
)
655 assert(zfpm_g
->state
== ZFPM_STATE_ESTABLISHED
);
657 zlog_info("connection to the FPM has gone down: %s", detail
);
662 stream_reset(zfpm_g
->ibuf
);
663 stream_reset(zfpm_g
->obuf
);
665 if (zfpm_g
->sock
>= 0) {
671 * Start thread to clean up state after the connection goes down.
673 assert(!zfpm_g
->t_conn_down
);
674 zfpm_debug("Starting conn_down thread");
675 zfpm_rnodes_iter_init(&zfpm_g
->t_conn_down_state
.iter
);
676 zfpm_g
->t_conn_down
= NULL
;
677 thread_add_timer_msec(zfpm_g
->master
, zfpm_conn_down_thread_cb
, NULL
, 0,
678 &zfpm_g
->t_conn_down
);
679 zfpm_g
->stats
.t_conn_down_starts
++;
681 zfpm_set_state(ZFPM_STATE_IDLE
, detail
);
687 static int zfpm_read_cb(struct thread
*thread
)
694 zfpm_g
->stats
.read_cb_calls
++;
695 zfpm_g
->t_read
= NULL
;
698 * Check if async connect is now done.
700 if (zfpm_g
->state
== ZFPM_STATE_CONNECTING
) {
701 zfpm_connect_check();
705 assert(zfpm_g
->state
== ZFPM_STATE_ESTABLISHED
);
706 assert(zfpm_g
->sock
>= 0);
710 already
= stream_get_endp(ibuf
);
711 if (already
< FPM_MSG_HDR_LEN
) {
714 nbyte
= stream_read_try(ibuf
, zfpm_g
->sock
,
715 FPM_MSG_HDR_LEN
- already
);
716 if (nbyte
== 0 || nbyte
== -1) {
720 sprintf(buffer
, "closed socket in read(%d): %s",
721 errno
, safe_strerror(errno
));
722 zfpm_connection_down(buffer
);
724 zfpm_connection_down("closed socket in read");
728 if (nbyte
!= (ssize_t
)(FPM_MSG_HDR_LEN
- already
))
731 already
= FPM_MSG_HDR_LEN
;
734 stream_set_getp(ibuf
, 0);
736 hdr
= (fpm_msg_hdr_t
*)stream_pnt(ibuf
);
738 if (!fpm_msg_hdr_ok(hdr
)) {
739 zfpm_connection_down("invalid message header");
743 msg_len
= fpm_msg_len(hdr
);
746 * Read out the rest of the packet.
748 if (already
< msg_len
) {
751 nbyte
= stream_read_try(ibuf
, zfpm_g
->sock
, msg_len
- already
);
753 if (nbyte
== 0 || nbyte
== -1) {
757 sprintf(buffer
, "failed to read message(%d) %s",
758 errno
, safe_strerror(errno
));
759 zfpm_connection_down(buffer
);
761 zfpm_connection_down("failed to read message");
765 if (nbyte
!= (ssize_t
)(msg_len
- already
))
769 zfpm_debug("Read out a full fpm message");
772 * Just throw it away for now.
782 * zfpm_writes_pending
784 * Returns TRUE if we may have something to write to the FPM.
786 static int zfpm_writes_pending(void)
790 * Check if there is any data in the outbound buffer that has not
791 * been written to the socket yet.
793 if (stream_get_endp(zfpm_g
->obuf
) - stream_get_getp(zfpm_g
->obuf
))
797 * Check if there are any prefixes on the outbound queue.
799 if (!TAILQ_EMPTY(&zfpm_g
->dest_q
))
808 * Encode a message to the FPM with information about the given route.
810 * Returns the number of bytes written to the buffer. 0 or a negative
811 * value indicates an error.
813 static inline int zfpm_encode_route(rib_dest_t
*dest
, struct route_entry
*re
,
814 char *in_buf
, size_t in_buf_len
,
815 fpm_msg_type_e
*msg_type
)
823 *msg_type
= FPM_MSG_TYPE_NONE
;
825 switch (zfpm_g
->message_format
) {
827 case ZFPM_MSG_FORMAT_PROTOBUF
:
829 len
= zfpm_protobuf_encode_route(dest
, re
, (uint8_t *)in_buf
,
831 *msg_type
= FPM_MSG_TYPE_PROTOBUF
;
835 case ZFPM_MSG_FORMAT_NETLINK
:
837 *msg_type
= FPM_MSG_TYPE_NETLINK
;
838 cmd
= re
? RTM_NEWROUTE
: RTM_DELROUTE
;
839 len
= zfpm_netlink_encode_route(cmd
, dest
, re
, in_buf
,
841 assert(fpm_msg_align(len
) == len
);
842 *msg_type
= FPM_MSG_TYPE_NETLINK
;
843 #endif /* HAVE_NETLINK */
854 * zfpm_route_for_update
856 * Returns the re that is to be sent to the FPM for a given dest.
858 struct route_entry
*zfpm_route_for_update(rib_dest_t
*dest
)
860 return dest
->selected_fib
;
866 * Process the outgoing queue and write messages to the outbound
869 static void zfpm_build_updates(void)
873 unsigned char *buf
, *data
, *buf_end
;
877 struct route_entry
*re
;
878 int is_add
, write_msg
;
879 fpm_msg_type_e msg_type
;
883 assert(stream_empty(s
));
888 * Make sure there is enough space to write another message.
890 if (STREAM_WRITEABLE(s
) < FPM_MAX_MSG_LEN
)
893 buf
= STREAM_DATA(s
) + stream_get_endp(s
);
894 buf_end
= buf
+ STREAM_WRITEABLE(s
);
896 dest
= TAILQ_FIRST(&zfpm_g
->dest_q
);
900 assert(CHECK_FLAG(dest
->flags
, RIB_DEST_UPDATE_FPM
));
902 hdr
= (fpm_msg_hdr_t
*)buf
;
903 hdr
->version
= FPM_PROTO_VERSION
;
905 data
= fpm_msg_data(hdr
);
907 re
= zfpm_route_for_update(dest
);
913 * If this is a route deletion, and we have not sent the route
915 * the FPM previously, skip it.
917 if (!is_add
&& !CHECK_FLAG(dest
->flags
, RIB_DEST_SENT_TO_FPM
)) {
919 zfpm_g
->stats
.nop_deletes_skipped
++;
923 data_len
= zfpm_encode_route(dest
, re
, (char *)data
,
924 buf_end
- data
, &msg_type
);
928 hdr
->msg_type
= msg_type
;
929 msg_len
= fpm_data_len_to_msg_len(data_len
);
930 hdr
->msg_len
= htons(msg_len
);
931 stream_forward_endp(s
, msg_len
);
934 zfpm_g
->stats
.route_adds
++;
936 zfpm_g
->stats
.route_dels
++;
941 * Remove the dest from the queue, and reset the flag.
943 UNSET_FLAG(dest
->flags
, RIB_DEST_UPDATE_FPM
);
944 TAILQ_REMOVE(&zfpm_g
->dest_q
, dest
, fpm_q_entries
);
947 SET_FLAG(dest
->flags
, RIB_DEST_SENT_TO_FPM
);
949 UNSET_FLAG(dest
->flags
, RIB_DEST_SENT_TO_FPM
);
953 * Delete the destination if necessary.
955 if (rib_gc_dest(dest
->rnode
))
956 zfpm_g
->stats
.dests_del_after_update
++;
964 static int zfpm_write_cb(struct thread
*thread
)
969 zfpm_g
->stats
.write_cb_calls
++;
970 zfpm_g
->t_write
= NULL
;
973 * Check if async connect is now done.
975 if (zfpm_g
->state
== ZFPM_STATE_CONNECTING
) {
976 zfpm_connect_check();
980 assert(zfpm_g
->state
== ZFPM_STATE_ESTABLISHED
);
981 assert(zfpm_g
->sock
>= 0);
986 int bytes_to_write
, bytes_written
;
991 * If the stream is empty, try fill it up with data.
993 if (stream_empty(s
)) {
994 zfpm_build_updates();
997 bytes_to_write
= stream_get_endp(s
) - stream_get_getp(s
);
1002 write(zfpm_g
->sock
, stream_pnt(s
), bytes_to_write
);
1003 zfpm_g
->stats
.write_calls
++;
1006 if (bytes_written
< 0) {
1007 if (ERRNO_IO_RETRY(errno
))
1010 zfpm_connection_down("failed to write to socket");
1014 if (bytes_written
!= bytes_to_write
) {
1019 stream_forward_getp(s
, bytes_written
);
1020 zfpm_g
->stats
.partial_writes
++;
1025 * We've written out the entire contents of the stream.
1029 if (num_writes
>= ZFPM_MAX_WRITES_PER_RUN
) {
1030 zfpm_g
->stats
.max_writes_hit
++;
1034 if (zfpm_thread_should_yield(thread
)) {
1035 zfpm_g
->stats
.t_write_yields
++;
1040 if (zfpm_writes_pending())
1049 static int zfpm_connect_cb(struct thread
*t
)
1052 struct sockaddr_in serv
;
1054 zfpm_g
->t_connect
= NULL
;
1055 assert(zfpm_g
->state
== ZFPM_STATE_ACTIVE
);
1057 sock
= socket(AF_INET
, SOCK_STREAM
, 0);
1059 zfpm_debug("Failed to create socket for connect(): %s",
1061 zfpm_g
->stats
.connect_no_sock
++;
1065 set_nonblocking(sock
);
1067 /* Make server socket. */
1068 memset(&serv
, 0, sizeof(serv
));
1069 serv
.sin_family
= AF_INET
;
1070 serv
.sin_port
= htons(zfpm_g
->fpm_port
);
1071 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
1072 serv
.sin_len
= sizeof(struct sockaddr_in
);
1073 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
1074 if (!zfpm_g
->fpm_server
)
1075 serv
.sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
1077 serv
.sin_addr
.s_addr
= (zfpm_g
->fpm_server
);
1080 * Connect to the FPM.
1082 zfpm_g
->connect_calls
++;
1083 zfpm_g
->stats
.connect_calls
++;
1084 zfpm_g
->last_connect_call_time
= monotime(NULL
);
1086 ret
= connect(sock
, (struct sockaddr
*)&serv
, sizeof(serv
));
1088 zfpm_g
->sock
= sock
;
1089 zfpm_connection_up("connect succeeded");
1093 if (errno
== EINPROGRESS
) {
1094 zfpm_g
->sock
= sock
;
1097 zfpm_set_state(ZFPM_STATE_CONNECTING
,
1098 "async connect in progress");
1102 zlog_info("can't connect to FPM %d: %s", sock
, safe_strerror(errno
));
1106 * Restart timer for retrying connection.
1108 zfpm_start_connect_timer("connect() failed");
1115 * Move state machine into the given state.
1117 static void zfpm_set_state(zfpm_state_t state
, const char *reason
)
1119 zfpm_state_t cur_state
= zfpm_g
->state
;
1124 if (state
== cur_state
)
1127 zfpm_debug("beginning state transition %s -> %s. Reason: %s",
1128 zfpm_state_to_str(cur_state
), zfpm_state_to_str(state
),
1133 case ZFPM_STATE_IDLE
:
1134 assert(cur_state
== ZFPM_STATE_ESTABLISHED
);
1137 case ZFPM_STATE_ACTIVE
:
1138 assert(cur_state
== ZFPM_STATE_IDLE
1139 || cur_state
== ZFPM_STATE_CONNECTING
);
1140 assert(zfpm_g
->t_connect
);
1143 case ZFPM_STATE_CONNECTING
:
1144 assert(zfpm_g
->sock
);
1145 assert(cur_state
== ZFPM_STATE_ACTIVE
);
1146 assert(zfpm_g
->t_read
);
1147 assert(zfpm_g
->t_write
);
1150 case ZFPM_STATE_ESTABLISHED
:
1151 assert(cur_state
== ZFPM_STATE_ACTIVE
1152 || cur_state
== ZFPM_STATE_CONNECTING
);
1153 assert(zfpm_g
->sock
);
1154 assert(zfpm_g
->t_read
);
1155 assert(zfpm_g
->t_write
);
1159 zfpm_g
->state
= state
;
1163 * zfpm_calc_connect_delay
1165 * Returns the number of seconds after which we should attempt to
1166 * reconnect to the FPM.
1168 static long zfpm_calc_connect_delay(void)
1173 * Return 0 if this is our first attempt to connect.
1175 if (zfpm_g
->connect_calls
== 0) {
1179 elapsed
= zfpm_get_elapsed_time(zfpm_g
->last_connect_call_time
);
1181 if (elapsed
> ZFPM_CONNECT_RETRY_IVL
) {
1185 return ZFPM_CONNECT_RETRY_IVL
- elapsed
;
1189 * zfpm_start_connect_timer
1191 static void zfpm_start_connect_timer(const char *reason
)
1195 assert(!zfpm_g
->t_connect
);
1196 assert(zfpm_g
->sock
< 0);
1198 assert(zfpm_g
->state
== ZFPM_STATE_IDLE
1199 || zfpm_g
->state
== ZFPM_STATE_ACTIVE
1200 || zfpm_g
->state
== ZFPM_STATE_CONNECTING
);
1202 delay_secs
= zfpm_calc_connect_delay();
1203 zfpm_debug("scheduling connect in %ld seconds", delay_secs
);
1205 thread_add_timer(zfpm_g
->master
, zfpm_connect_cb
, 0, delay_secs
,
1206 &zfpm_g
->t_connect
);
1207 zfpm_set_state(ZFPM_STATE_ACTIVE
, reason
);
1213 * Returns TRUE if the zebra FPM module has been enabled.
1215 static inline int zfpm_is_enabled(void)
1217 return zfpm_g
->enabled
;
1223 * Returns TRUE if the connection to the FPM is up.
1225 static inline int zfpm_conn_is_up(void)
1227 if (zfpm_g
->state
!= ZFPM_STATE_ESTABLISHED
)
1230 assert(zfpm_g
->sock
>= 0);
1236 * zfpm_trigger_update
1238 * The zebra code invokes this function to indicate that we should
1239 * send an update to the FPM about the given route_node.
1241 static int zfpm_trigger_update(struct route_node
*rn
, const char *reason
)
1244 char buf
[PREFIX_STRLEN
];
1247 * Ignore if the connection is down. We will update the FPM about
1248 * all destinations once the connection comes up.
1250 if (!zfpm_conn_is_up())
1253 dest
= rib_dest_from_rnode(rn
);
1255 if (CHECK_FLAG(dest
->flags
, RIB_DEST_UPDATE_FPM
)) {
1256 zfpm_g
->stats
.redundant_triggers
++;
1261 zfpm_debug("%s triggering update to FPM - Reason: %s",
1262 prefix2str(&rn
->p
, buf
, sizeof(buf
)), reason
);
1265 SET_FLAG(dest
->flags
, RIB_DEST_UPDATE_FPM
);
1266 TAILQ_INSERT_TAIL(&zfpm_g
->dest_q
, dest
, fpm_q_entries
);
1267 zfpm_g
->stats
.updates_triggered
++;
1270 * Make sure that writes are enabled.
1272 if (zfpm_g
->t_write
)
1280 * zfpm_stats_timer_cb
1282 static int zfpm_stats_timer_cb(struct thread
*t
)
1284 zfpm_g
->t_stats
= NULL
;
1287 * Remember the stats collected in the last interval for display
1290 zfpm_stats_copy(&zfpm_g
->stats
, &zfpm_g
->last_ivl_stats
);
1293 * Add the current set of stats into the cumulative statistics.
1295 zfpm_stats_compose(&zfpm_g
->cumulative_stats
, &zfpm_g
->stats
,
1296 &zfpm_g
->cumulative_stats
);
1299 * Start collecting stats afresh over the next interval.
1301 zfpm_stats_reset(&zfpm_g
->stats
);
1303 zfpm_start_stats_timer();
1309 * zfpm_stop_stats_timer
1311 static void zfpm_stop_stats_timer(void)
1313 if (!zfpm_g
->t_stats
)
1316 zfpm_debug("Stopping existing stats timer");
1317 THREAD_TIMER_OFF(zfpm_g
->t_stats
);
1321 * zfpm_start_stats_timer
1323 void zfpm_start_stats_timer(void)
1325 assert(!zfpm_g
->t_stats
);
1327 thread_add_timer(zfpm_g
->master
, zfpm_stats_timer_cb
, 0,
1328 ZFPM_STATS_IVL_SECS
, &zfpm_g
->t_stats
);
1332 * Helper macro for zfpm_show_stats() below.
1334 #define ZFPM_SHOW_STAT(counter) \
1336 vty_out(vty, "%-40s %10lu %16lu\n", #counter, \
1337 total_stats.counter, zfpm_g->last_ivl_stats.counter); \
1343 static void zfpm_show_stats(struct vty
*vty
)
1345 zfpm_stats_t total_stats
;
1348 vty_out(vty
, "\n%-40s %10s Last %2d secs\n\n", "Counter", "Total",
1349 ZFPM_STATS_IVL_SECS
);
1352 * Compute the total stats up to this instant.
1354 zfpm_stats_compose(&zfpm_g
->cumulative_stats
, &zfpm_g
->stats
,
1357 ZFPM_SHOW_STAT(connect_calls
);
1358 ZFPM_SHOW_STAT(connect_no_sock
);
1359 ZFPM_SHOW_STAT(read_cb_calls
);
1360 ZFPM_SHOW_STAT(write_cb_calls
);
1361 ZFPM_SHOW_STAT(write_calls
);
1362 ZFPM_SHOW_STAT(partial_writes
);
1363 ZFPM_SHOW_STAT(max_writes_hit
);
1364 ZFPM_SHOW_STAT(t_write_yields
);
1365 ZFPM_SHOW_STAT(nop_deletes_skipped
);
1366 ZFPM_SHOW_STAT(route_adds
);
1367 ZFPM_SHOW_STAT(route_dels
);
1368 ZFPM_SHOW_STAT(updates_triggered
);
1369 ZFPM_SHOW_STAT(redundant_triggers
);
1370 ZFPM_SHOW_STAT(dests_del_after_update
);
1371 ZFPM_SHOW_STAT(t_conn_down_starts
);
1372 ZFPM_SHOW_STAT(t_conn_down_dests_processed
);
1373 ZFPM_SHOW_STAT(t_conn_down_yields
);
1374 ZFPM_SHOW_STAT(t_conn_down_finishes
);
1375 ZFPM_SHOW_STAT(t_conn_up_starts
);
1376 ZFPM_SHOW_STAT(t_conn_up_dests_processed
);
1377 ZFPM_SHOW_STAT(t_conn_up_yields
);
1378 ZFPM_SHOW_STAT(t_conn_up_aborts
);
1379 ZFPM_SHOW_STAT(t_conn_up_finishes
);
1381 if (!zfpm_g
->last_stats_clear_time
)
1384 elapsed
= zfpm_get_elapsed_time(zfpm_g
->last_stats_clear_time
);
1386 vty_out(vty
, "\nStats were cleared %lu seconds ago\n",
1387 (unsigned long)elapsed
);
1393 static void zfpm_clear_stats(struct vty
*vty
)
1395 if (!zfpm_is_enabled()) {
1396 vty_out(vty
, "The FPM module is not enabled...\n");
1400 zfpm_stats_reset(&zfpm_g
->stats
);
1401 zfpm_stats_reset(&zfpm_g
->last_ivl_stats
);
1402 zfpm_stats_reset(&zfpm_g
->cumulative_stats
);
1404 zfpm_stop_stats_timer();
1405 zfpm_start_stats_timer();
1407 zfpm_g
->last_stats_clear_time
= monotime(NULL
);
1409 vty_out(vty
, "Cleared FPM stats\n");
1413 * show_zebra_fpm_stats
1415 DEFUN (show_zebra_fpm_stats
,
1416 show_zebra_fpm_stats_cmd
,
1417 "show zebra fpm stats",
1420 "Forwarding Path Manager information\n"
1423 zfpm_show_stats(vty
);
1428 * clear_zebra_fpm_stats
1430 DEFUN (clear_zebra_fpm_stats
,
1431 clear_zebra_fpm_stats_cmd
,
1432 "clear zebra fpm stats",
1435 "Clear Forwarding Path Manager information\n"
1438 zfpm_clear_stats(vty
);
1443 * update fpm connection information
1445 DEFUN ( fpm_remote_ip
,
1447 "fpm connection ip A.B.C.D port (1-65535)",
1448 "fpm connection remote ip and port\n"
1449 "Remote fpm server ip A.B.C.D\n"
1453 in_addr_t fpm_server
;
1456 fpm_server
= inet_addr(argv
[3]->arg
);
1457 if (fpm_server
== INADDR_NONE
)
1458 return CMD_ERR_INCOMPLETE
;
1460 port_no
= atoi(argv
[5]->arg
);
1461 if (port_no
< TCP_MIN_PORT
|| port_no
> TCP_MAX_PORT
)
1462 return CMD_ERR_INCOMPLETE
;
1464 zfpm_g
->fpm_server
= fpm_server
;
1465 zfpm_g
->fpm_port
= port_no
;
1471 DEFUN ( no_fpm_remote_ip
,
1472 no_fpm_remote_ip_cmd
,
1473 "no fpm connection ip A.B.C.D port (1-65535)",
1474 "fpm connection remote ip and port\n"
1476 "Remote fpm server ip A.B.C.D\n"
1479 if (zfpm_g
->fpm_server
!= inet_addr(argv
[4]->arg
)
1480 || zfpm_g
->fpm_port
!= atoi(argv
[6]->arg
))
1481 return CMD_ERR_NO_MATCH
;
1483 zfpm_g
->fpm_server
= FPM_DEFAULT_IP
;
1484 zfpm_g
->fpm_port
= FPM_DEFAULT_PORT
;
1490 * zfpm_init_message_format
1492 static inline void zfpm_init_message_format(const char *format
)
1494 int have_netlink
, have_protobuf
;
1502 #ifdef HAVE_PROTOBUF
1508 zfpm_g
->message_format
= ZFPM_MSG_FORMAT_NONE
;
1512 zfpm_g
->message_format
= ZFPM_MSG_FORMAT_NETLINK
;
1513 } else if (have_protobuf
) {
1514 zfpm_g
->message_format
= ZFPM_MSG_FORMAT_PROTOBUF
;
1519 if (!strcmp("netlink", format
)) {
1520 if (!have_netlink
) {
1522 ZEBRA_ERR_NETLINK_NOT_AVAILABLE
,
1523 "FPM netlink message format is not available");
1526 zfpm_g
->message_format
= ZFPM_MSG_FORMAT_NETLINK
;
1530 if (!strcmp("protobuf", format
)) {
1531 if (!have_protobuf
) {
1533 ZEBRA_ERR_PROTOBUF_NOT_AVAILABLE
,
1534 "FPM protobuf message format is not available");
1537 zfpm_g
->message_format
= ZFPM_MSG_FORMAT_PROTOBUF
;
1541 zlog_warn("Unknown fpm format '%s'", format
);
1545 * fpm_remote_srv_write
1547 * Module to write remote fpm connection
1549 * Returns ZERO on success.
1552 static int fpm_remote_srv_write(struct vty
*vty
)
1556 in
.s_addr
= zfpm_g
->fpm_server
;
1558 if ((zfpm_g
->fpm_server
!= FPM_DEFAULT_IP
1559 && zfpm_g
->fpm_server
!= INADDR_ANY
)
1560 || (zfpm_g
->fpm_port
!= FPM_DEFAULT_PORT
&& zfpm_g
->fpm_port
!= 0))
1561 vty_out(vty
, "fpm connection ip %s port %d\n", inet_ntoa(in
),
1569 static struct cmd_node zebra_node
= {ZEBRA_NODE
, "", 1};
1575 * One-time initialization of the Zebra FPM module.
1577 * @param[in] port port at which FPM is running.
1578 * @param[in] enable TRUE if the zebra FPM module should be enabled
1579 * @param[in] format to use to talk to the FPM. Can be 'netink' or 'protobuf'.
1581 * Returns TRUE on success.
1583 static int zfpm_init(struct thread_master
*master
)
1587 const char *format
= THIS_MODULE
->load_args
;
1589 memset(zfpm_g
, 0, sizeof(*zfpm_g
));
1590 zfpm_g
->master
= master
;
1591 TAILQ_INIT(&zfpm_g
->dest_q
);
1593 zfpm_g
->state
= ZFPM_STATE_IDLE
;
1595 zfpm_stats_init(&zfpm_g
->stats
);
1596 zfpm_stats_init(&zfpm_g
->last_ivl_stats
);
1597 zfpm_stats_init(&zfpm_g
->cumulative_stats
);
1599 install_node(&zebra_node
, fpm_remote_srv_write
);
1600 install_element(ENABLE_NODE
, &show_zebra_fpm_stats_cmd
);
1601 install_element(ENABLE_NODE
, &clear_zebra_fpm_stats_cmd
);
1602 install_element(CONFIG_NODE
, &fpm_remote_ip_cmd
);
1603 install_element(CONFIG_NODE
, &no_fpm_remote_ip_cmd
);
1605 zfpm_init_message_format(format
);
1608 * Disable FPM interface if no suitable format is available.
1610 if (zfpm_g
->message_format
== ZFPM_MSG_FORMAT_NONE
)
1613 zfpm_g
->enabled
= enable
;
1615 if (!zfpm_g
->fpm_server
)
1616 zfpm_g
->fpm_server
= FPM_DEFAULT_IP
;
1619 port
= FPM_DEFAULT_PORT
;
1621 zfpm_g
->fpm_port
= port
;
1623 zfpm_g
->obuf
= stream_new(ZFPM_OBUF_SIZE
);
1624 zfpm_g
->ibuf
= stream_new(ZFPM_IBUF_SIZE
);
1626 zfpm_start_stats_timer();
1627 zfpm_start_connect_timer("initialized");
1631 static int zebra_fpm_module_init(void)
1633 hook_register(rib_update
, zfpm_trigger_update
);
1634 hook_register(frr_late_init
, zfpm_init
);
1638 FRR_MODULE_SETUP(.name
= "zebra_fpm", .version
= FRR_VERSION
,
1639 .description
= "zebra FPM (Forwarding Plane Manager) module",
1640 .init
= zebra_fpm_module_init
, )