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"
40 #include "zebra_fpm_private.h"
43 * Interval at which we attempt to connect to the FPM.
45 #define ZFPM_CONNECT_RETRY_IVL 5
48 * Sizes of outgoing and incoming stream buffers for writing/reading
51 #define ZFPM_OBUF_SIZE (2 * FPM_MAX_MSG_LEN)
52 #define ZFPM_IBUF_SIZE (FPM_MAX_MSG_LEN)
55 * The maximum number of times the FPM socket write callback can call
56 * 'write' before it yields.
58 #define ZFPM_MAX_WRITES_PER_RUN 10
61 * Interval over which we collect statistics.
63 #define ZFPM_STATS_IVL_SECS 10
66 * Structure that holds state for iterating over all route_node
67 * structures that are candidates for being communicated to the FPM.
69 typedef struct zfpm_rnodes_iter_t_
{
70 rib_tables_iter_t tables_iter
;
71 route_table_iter_t iter
;
77 typedef struct zfpm_stats_t_
{
78 unsigned long connect_calls
;
79 unsigned long connect_no_sock
;
81 unsigned long read_cb_calls
;
83 unsigned long write_cb_calls
;
84 unsigned long write_calls
;
85 unsigned long partial_writes
;
86 unsigned long max_writes_hit
;
87 unsigned long t_write_yields
;
89 unsigned long nop_deletes_skipped
;
90 unsigned long route_adds
;
91 unsigned long route_dels
;
93 unsigned long updates_triggered
;
94 unsigned long redundant_triggers
;
96 unsigned long dests_del_after_update
;
98 unsigned long t_conn_down_starts
;
99 unsigned long t_conn_down_dests_processed
;
100 unsigned long t_conn_down_yields
;
101 unsigned long t_conn_down_finishes
;
103 unsigned long t_conn_up_starts
;
104 unsigned long t_conn_up_dests_processed
;
105 unsigned long t_conn_up_yields
;
106 unsigned long t_conn_up_aborts
;
107 unsigned long t_conn_up_finishes
;
112 * States for the FPM state machine.
117 * In this state we are not yet ready to connect to the FPM. This
118 * can happen when this module is disabled, or if we're cleaning up
119 * after a connection has gone down.
124 * Ready to talk to the FPM and periodically trying to connect to
130 * In the middle of bringing up a TCP connection. Specifically,
131 * waiting for a connect() call to complete asynchronously.
133 ZFPM_STATE_CONNECTING
,
136 * TCP connection to the FPM is up.
138 ZFPM_STATE_ESTABLISHED
143 * Message format to be used to communicate with the FPM.
146 ZFPM_MSG_FORMAT_NONE
,
147 ZFPM_MSG_FORMAT_NETLINK
,
148 ZFPM_MSG_FORMAT_PROTOBUF
,
153 typedef struct zfpm_glob_t_
{
156 * True if the FPM module has been enabled.
161 * Message format to be used to communicate with the fpm.
163 zfpm_msg_format_e message_format
;
165 struct thread_master
*master
;
169 in_addr_t fpm_server
;
171 * Port on which the FPM is running.
176 * List of rib_dest_t structures to be processed
178 TAILQ_HEAD(zfpm_dest_q
, rib_dest_t_
) dest_q
;
181 * Stream socket to the FPM.
186 * Buffers for messages to/from the FPM.
194 struct thread
*t_connect
;
195 struct thread
*t_write
;
196 struct thread
*t_read
;
199 * Thread to clean up after the TCP connection to the FPM goes down
200 * and the state that belongs to it.
202 struct thread
*t_conn_down
;
205 zfpm_rnodes_iter_t iter
;
209 * Thread to take actions once the TCP conn to the FPM comes up, and
210 * the state that belongs to it.
212 struct thread
*t_conn_up
;
215 zfpm_rnodes_iter_t iter
;
218 unsigned long connect_calls
;
219 time_t last_connect_call_time
;
222 * Stats from the start of the current statistics interval up to
223 * now. These are the counters we typically update in the code.
228 * Statistics that were gathered in the last collection interval.
230 zfpm_stats_t last_ivl_stats
;
233 * Cumulative stats from the last clear to the start of the current
234 * statistics interval.
236 zfpm_stats_t cumulative_stats
;
239 * Stats interval timer.
241 struct thread
*t_stats
;
244 * If non-zero, the last time when statistics were cleared.
246 time_t last_stats_clear_time
;
250 static zfpm_glob_t zfpm_glob_space
;
251 static zfpm_glob_t
*zfpm_g
= &zfpm_glob_space
;
253 static int zfpm_trigger_update(struct route_node
*rn
, const char *reason
);
255 static int zfpm_read_cb(struct thread
*thread
);
256 static int zfpm_write_cb(struct thread
*thread
);
258 static void zfpm_set_state(zfpm_state_t state
, const char *reason
);
259 static void zfpm_start_connect_timer(const char *reason
);
260 static void zfpm_start_stats_timer(void);
263 * zfpm_thread_should_yield
265 static inline int zfpm_thread_should_yield(struct thread
*t
)
267 return thread_should_yield(t
);
273 static const char *zfpm_state_to_str(zfpm_state_t state
)
277 case ZFPM_STATE_IDLE
:
280 case ZFPM_STATE_ACTIVE
:
283 case ZFPM_STATE_CONNECTING
:
286 case ZFPM_STATE_ESTABLISHED
:
287 return "established";
295 * zfpm_get_elapsed_time
297 * Returns the time elapsed (in seconds) since the given time.
299 static time_t zfpm_get_elapsed_time(time_t reference
)
303 now
= monotime(NULL
);
305 if (now
< reference
) {
310 return now
- reference
;
314 * zfpm_rnodes_iter_init
316 static inline void zfpm_rnodes_iter_init(zfpm_rnodes_iter_t
*iter
)
318 memset(iter
, 0, sizeof(*iter
));
319 rib_tables_iter_init(&iter
->tables_iter
);
322 * This is a hack, but it makes implementing 'next' easier by
323 * ensuring that route_table_iter_next() will return NULL the first
326 route_table_iter_init(&iter
->iter
, NULL
);
327 route_table_iter_cleanup(&iter
->iter
);
331 * zfpm_rnodes_iter_next
333 static inline struct route_node
*zfpm_rnodes_iter_next(zfpm_rnodes_iter_t
*iter
)
335 struct route_node
*rn
;
336 struct route_table
*table
;
339 rn
= route_table_iter_next(&iter
->iter
);
344 * We've made our way through this table, go to the next one.
346 route_table_iter_cleanup(&iter
->iter
);
348 table
= rib_tables_iter_next(&iter
->tables_iter
);
353 route_table_iter_init(&iter
->iter
, table
);
360 * zfpm_rnodes_iter_pause
362 static inline void zfpm_rnodes_iter_pause(zfpm_rnodes_iter_t
*iter
)
364 route_table_iter_pause(&iter
->iter
);
368 * zfpm_rnodes_iter_cleanup
370 static inline void zfpm_rnodes_iter_cleanup(zfpm_rnodes_iter_t
*iter
)
372 route_table_iter_cleanup(&iter
->iter
);
373 rib_tables_iter_cleanup(&iter
->tables_iter
);
379 * Initialize a statistics block.
381 static inline void zfpm_stats_init(zfpm_stats_t
*stats
)
383 memset(stats
, 0, sizeof(*stats
));
389 static inline void zfpm_stats_reset(zfpm_stats_t
*stats
)
391 zfpm_stats_init(stats
);
397 static inline void zfpm_stats_copy(const zfpm_stats_t
*src
, zfpm_stats_t
*dest
)
399 memcpy(dest
, src
, sizeof(*dest
));
405 * Total up the statistics in two stats structures ('s1 and 's2') and
406 * return the result in the third argument, 'result'. Note that the
407 * pointer 'result' may be the same as 's1' or 's2'.
409 * For simplicity, the implementation below assumes that the stats
410 * structure is composed entirely of counters. This can easily be
411 * changed when necessary.
413 static void zfpm_stats_compose(const zfpm_stats_t
*s1
, const zfpm_stats_t
*s2
,
414 zfpm_stats_t
*result
)
416 const unsigned long *p1
, *p2
;
417 unsigned long *result_p
;
420 p1
= (const unsigned long *)s1
;
421 p2
= (const unsigned long *)s2
;
422 result_p
= (unsigned long *)result
;
424 num_counters
= (sizeof(zfpm_stats_t
) / sizeof(unsigned long));
426 for (i
= 0; i
< num_counters
; i
++) {
427 result_p
[i
] = p1
[i
] + p2
[i
];
434 static inline void zfpm_read_on(void)
436 assert(!zfpm_g
->t_read
);
437 assert(zfpm_g
->sock
>= 0);
439 thread_add_read(zfpm_g
->master
, zfpm_read_cb
, 0, zfpm_g
->sock
,
446 static inline void zfpm_write_on(void)
448 assert(!zfpm_g
->t_write
);
449 assert(zfpm_g
->sock
>= 0);
451 thread_add_write(zfpm_g
->master
, zfpm_write_cb
, 0, zfpm_g
->sock
,
458 static inline void zfpm_read_off(void)
460 THREAD_READ_OFF(zfpm_g
->t_read
);
466 static inline void zfpm_write_off(void)
468 THREAD_WRITE_OFF(zfpm_g
->t_write
);
472 * zfpm_conn_up_thread_cb
474 * Callback for actions to be taken when the connection to the FPM
477 static int zfpm_conn_up_thread_cb(struct thread
*thread
)
479 struct route_node
*rnode
;
480 zfpm_rnodes_iter_t
*iter
;
483 zfpm_g
->t_conn_up
= NULL
;
485 iter
= &zfpm_g
->t_conn_up_state
.iter
;
487 if (zfpm_g
->state
!= ZFPM_STATE_ESTABLISHED
) {
489 "Connection not up anymore, conn_up thread aborting");
490 zfpm_g
->stats
.t_conn_up_aborts
++;
494 while ((rnode
= zfpm_rnodes_iter_next(iter
))) {
495 dest
= rib_dest_from_rnode(rnode
);
498 zfpm_g
->stats
.t_conn_up_dests_processed
++;
499 zfpm_trigger_update(rnode
, NULL
);
505 if (!zfpm_thread_should_yield(thread
))
508 zfpm_g
->stats
.t_conn_up_yields
++;
509 zfpm_rnodes_iter_pause(iter
);
510 zfpm_g
->t_conn_up
= NULL
;
511 thread_add_timer_msec(zfpm_g
->master
, zfpm_conn_up_thread_cb
,
512 NULL
, 0, &zfpm_g
->t_conn_up
);
516 zfpm_g
->stats
.t_conn_up_finishes
++;
519 zfpm_rnodes_iter_cleanup(iter
);
526 * Called when the connection to the FPM comes up.
528 static void zfpm_connection_up(const char *detail
)
530 assert(zfpm_g
->sock
>= 0);
533 zfpm_set_state(ZFPM_STATE_ESTABLISHED
, detail
);
536 * Start thread to push existing routes to the FPM.
538 assert(!zfpm_g
->t_conn_up
);
540 zfpm_rnodes_iter_init(&zfpm_g
->t_conn_up_state
.iter
);
542 zfpm_debug("Starting conn_up thread");
543 zfpm_g
->t_conn_up
= NULL
;
544 thread_add_timer_msec(zfpm_g
->master
, zfpm_conn_up_thread_cb
, NULL
, 0,
546 zfpm_g
->stats
.t_conn_up_starts
++;
552 * Check if an asynchronous connect() to the FPM is complete.
554 static void zfpm_connect_check(void)
563 slen
= sizeof(status
);
564 ret
= getsockopt(zfpm_g
->sock
, SOL_SOCKET
, SO_ERROR
, (void *)&status
,
567 if (ret
>= 0 && status
== 0) {
568 zfpm_connection_up("async connect complete");
573 * getsockopt() failed or indicated an error on the socket.
578 zfpm_start_connect_timer("getsockopt() after async connect failed");
583 * zfpm_conn_down_thread_cb
585 * Callback that is invoked to clean up state after the TCP connection
586 * to the FPM goes down.
588 static int zfpm_conn_down_thread_cb(struct thread
*thread
)
590 struct route_node
*rnode
;
591 zfpm_rnodes_iter_t
*iter
;
594 assert(zfpm_g
->state
== ZFPM_STATE_IDLE
);
596 zfpm_g
->t_conn_down
= NULL
;
598 iter
= &zfpm_g
->t_conn_down_state
.iter
;
600 while ((rnode
= zfpm_rnodes_iter_next(iter
))) {
601 dest
= rib_dest_from_rnode(rnode
);
604 if (CHECK_FLAG(dest
->flags
, RIB_DEST_UPDATE_FPM
)) {
605 TAILQ_REMOVE(&zfpm_g
->dest_q
, dest
,
609 UNSET_FLAG(dest
->flags
, RIB_DEST_UPDATE_FPM
);
610 UNSET_FLAG(dest
->flags
, RIB_DEST_SENT_TO_FPM
);
612 zfpm_g
->stats
.t_conn_down_dests_processed
++;
615 * Check if the dest should be deleted.
623 if (!zfpm_thread_should_yield(thread
))
626 zfpm_g
->stats
.t_conn_down_yields
++;
627 zfpm_rnodes_iter_pause(iter
);
628 zfpm_g
->t_conn_down
= NULL
;
629 thread_add_timer_msec(zfpm_g
->master
, zfpm_conn_down_thread_cb
,
630 NULL
, 0, &zfpm_g
->t_conn_down
);
634 zfpm_g
->stats
.t_conn_down_finishes
++;
635 zfpm_rnodes_iter_cleanup(iter
);
638 * Start the process of connecting to the FPM again.
640 zfpm_start_connect_timer("cleanup complete");
645 * zfpm_connection_down
647 * Called when the connection to the FPM has gone down.
649 static void zfpm_connection_down(const char *detail
)
654 assert(zfpm_g
->state
== ZFPM_STATE_ESTABLISHED
);
656 zlog_info("connection to the FPM has gone down: %s", detail
);
661 stream_reset(zfpm_g
->ibuf
);
662 stream_reset(zfpm_g
->obuf
);
664 if (zfpm_g
->sock
>= 0) {
670 * Start thread to clean up state after the connection goes down.
672 assert(!zfpm_g
->t_conn_down
);
673 zfpm_debug("Starting conn_down thread");
674 zfpm_rnodes_iter_init(&zfpm_g
->t_conn_down_state
.iter
);
675 zfpm_g
->t_conn_down
= NULL
;
676 thread_add_timer_msec(zfpm_g
->master
, zfpm_conn_down_thread_cb
, NULL
, 0,
677 &zfpm_g
->t_conn_down
);
678 zfpm_g
->stats
.t_conn_down_starts
++;
680 zfpm_set_state(ZFPM_STATE_IDLE
, detail
);
686 static int zfpm_read_cb(struct thread
*thread
)
693 zfpm_g
->stats
.read_cb_calls
++;
694 zfpm_g
->t_read
= NULL
;
697 * Check if async connect is now done.
699 if (zfpm_g
->state
== ZFPM_STATE_CONNECTING
) {
700 zfpm_connect_check();
704 assert(zfpm_g
->state
== ZFPM_STATE_ESTABLISHED
);
705 assert(zfpm_g
->sock
>= 0);
709 already
= stream_get_endp(ibuf
);
710 if (already
< FPM_MSG_HDR_LEN
) {
713 nbyte
= stream_read_try(ibuf
, zfpm_g
->sock
,
714 FPM_MSG_HDR_LEN
- already
);
715 if (nbyte
== 0 || nbyte
== -1) {
719 sprintf(buffer
, "closed socket in read(%d): %s",
720 errno
, safe_strerror(errno
));
721 zfpm_connection_down(buffer
);
723 zfpm_connection_down("closed socket in read");
727 if (nbyte
!= (ssize_t
)(FPM_MSG_HDR_LEN
- already
))
730 already
= FPM_MSG_HDR_LEN
;
733 stream_set_getp(ibuf
, 0);
735 hdr
= (fpm_msg_hdr_t
*)stream_pnt(ibuf
);
737 if (!fpm_msg_hdr_ok(hdr
)) {
738 zfpm_connection_down("invalid message header");
742 msg_len
= fpm_msg_len(hdr
);
745 * Read out the rest of the packet.
747 if (already
< msg_len
) {
750 nbyte
= stream_read_try(ibuf
, zfpm_g
->sock
, msg_len
- already
);
752 if (nbyte
== 0 || nbyte
== -1) {
756 sprintf(buffer
, "failed to read message(%d) %s",
757 errno
, safe_strerror(errno
));
758 zfpm_connection_down(buffer
);
760 zfpm_connection_down("failed to read message");
764 if (nbyte
!= (ssize_t
)(msg_len
- already
))
768 zfpm_debug("Read out a full fpm message");
771 * Just throw it away for now.
781 * zfpm_writes_pending
783 * Returns TRUE if we may have something to write to the FPM.
785 static int zfpm_writes_pending(void)
789 * Check if there is any data in the outbound buffer that has not
790 * been written to the socket yet.
792 if (stream_get_endp(zfpm_g
->obuf
) - stream_get_getp(zfpm_g
->obuf
))
796 * Check if there are any prefixes on the outbound queue.
798 if (!TAILQ_EMPTY(&zfpm_g
->dest_q
))
807 * Encode a message to the FPM with information about the given route.
809 * Returns the number of bytes written to the buffer. 0 or a negative
810 * value indicates an error.
812 static inline int zfpm_encode_route(rib_dest_t
*dest
, struct route_entry
*re
,
813 char *in_buf
, size_t in_buf_len
,
814 fpm_msg_type_e
*msg_type
)
822 *msg_type
= FPM_MSG_TYPE_NONE
;
824 switch (zfpm_g
->message_format
) {
826 case ZFPM_MSG_FORMAT_PROTOBUF
:
828 len
= zfpm_protobuf_encode_route(dest
, re
, (uint8_t *)in_buf
,
830 *msg_type
= FPM_MSG_TYPE_PROTOBUF
;
834 case ZFPM_MSG_FORMAT_NETLINK
:
836 *msg_type
= FPM_MSG_TYPE_NETLINK
;
837 cmd
= re
? RTM_NEWROUTE
: RTM_DELROUTE
;
838 len
= zfpm_netlink_encode_route(cmd
, dest
, re
, in_buf
,
840 assert(fpm_msg_align(len
) == len
);
841 *msg_type
= FPM_MSG_TYPE_NETLINK
;
842 #endif /* HAVE_NETLINK */
853 * zfpm_route_for_update
855 * Returns the re that is to be sent to the FPM for a given dest.
857 struct route_entry
*zfpm_route_for_update(rib_dest_t
*dest
)
859 return dest
->selected_fib
;
865 * Process the outgoing queue and write messages to the outbound
868 static void zfpm_build_updates(void)
872 unsigned char *buf
, *data
, *buf_end
;
876 struct route_entry
*re
;
877 int is_add
, write_msg
;
878 fpm_msg_type_e msg_type
;
882 assert(stream_empty(s
));
887 * Make sure there is enough space to write another message.
889 if (STREAM_WRITEABLE(s
) < FPM_MAX_MSG_LEN
)
892 buf
= STREAM_DATA(s
) + stream_get_endp(s
);
893 buf_end
= buf
+ STREAM_WRITEABLE(s
);
895 dest
= TAILQ_FIRST(&zfpm_g
->dest_q
);
899 assert(CHECK_FLAG(dest
->flags
, RIB_DEST_UPDATE_FPM
));
901 hdr
= (fpm_msg_hdr_t
*)buf
;
902 hdr
->version
= FPM_PROTO_VERSION
;
904 data
= fpm_msg_data(hdr
);
906 re
= zfpm_route_for_update(dest
);
912 * If this is a route deletion, and we have not sent the route
914 * the FPM previously, skip it.
916 if (!is_add
&& !CHECK_FLAG(dest
->flags
, RIB_DEST_SENT_TO_FPM
)) {
918 zfpm_g
->stats
.nop_deletes_skipped
++;
922 data_len
= zfpm_encode_route(dest
, re
, (char *)data
,
923 buf_end
- data
, &msg_type
);
927 hdr
->msg_type
= msg_type
;
928 msg_len
= fpm_data_len_to_msg_len(data_len
);
929 hdr
->msg_len
= htons(msg_len
);
930 stream_forward_endp(s
, msg_len
);
933 zfpm_g
->stats
.route_adds
++;
935 zfpm_g
->stats
.route_dels
++;
940 * Remove the dest from the queue, and reset the flag.
942 UNSET_FLAG(dest
->flags
, RIB_DEST_UPDATE_FPM
);
943 TAILQ_REMOVE(&zfpm_g
->dest_q
, dest
, fpm_q_entries
);
946 SET_FLAG(dest
->flags
, RIB_DEST_SENT_TO_FPM
);
948 UNSET_FLAG(dest
->flags
, RIB_DEST_SENT_TO_FPM
);
952 * Delete the destination if necessary.
954 if (rib_gc_dest(dest
->rnode
))
955 zfpm_g
->stats
.dests_del_after_update
++;
963 static int zfpm_write_cb(struct thread
*thread
)
968 zfpm_g
->stats
.write_cb_calls
++;
969 zfpm_g
->t_write
= NULL
;
972 * Check if async connect is now done.
974 if (zfpm_g
->state
== ZFPM_STATE_CONNECTING
) {
975 zfpm_connect_check();
979 assert(zfpm_g
->state
== ZFPM_STATE_ESTABLISHED
);
980 assert(zfpm_g
->sock
>= 0);
985 int bytes_to_write
, bytes_written
;
990 * If the stream is empty, try fill it up with data.
992 if (stream_empty(s
)) {
993 zfpm_build_updates();
996 bytes_to_write
= stream_get_endp(s
) - stream_get_getp(s
);
1001 write(zfpm_g
->sock
, stream_pnt(s
), bytes_to_write
);
1002 zfpm_g
->stats
.write_calls
++;
1005 if (bytes_written
< 0) {
1006 if (ERRNO_IO_RETRY(errno
))
1009 zfpm_connection_down("failed to write to socket");
1013 if (bytes_written
!= bytes_to_write
) {
1018 stream_forward_getp(s
, bytes_written
);
1019 zfpm_g
->stats
.partial_writes
++;
1024 * We've written out the entire contents of the stream.
1028 if (num_writes
>= ZFPM_MAX_WRITES_PER_RUN
) {
1029 zfpm_g
->stats
.max_writes_hit
++;
1033 if (zfpm_thread_should_yield(thread
)) {
1034 zfpm_g
->stats
.t_write_yields
++;
1039 if (zfpm_writes_pending())
1048 static int zfpm_connect_cb(struct thread
*t
)
1051 struct sockaddr_in serv
;
1053 zfpm_g
->t_connect
= NULL
;
1054 assert(zfpm_g
->state
== ZFPM_STATE_ACTIVE
);
1056 sock
= socket(AF_INET
, SOCK_STREAM
, 0);
1058 zfpm_debug("Failed to create socket for connect(): %s",
1060 zfpm_g
->stats
.connect_no_sock
++;
1064 set_nonblocking(sock
);
1066 /* Make server socket. */
1067 memset(&serv
, 0, sizeof(serv
));
1068 serv
.sin_family
= AF_INET
;
1069 serv
.sin_port
= htons(zfpm_g
->fpm_port
);
1070 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
1071 serv
.sin_len
= sizeof(struct sockaddr_in
);
1072 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
1073 if (!zfpm_g
->fpm_server
)
1074 serv
.sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
1076 serv
.sin_addr
.s_addr
= (zfpm_g
->fpm_server
);
1079 * Connect to the FPM.
1081 zfpm_g
->connect_calls
++;
1082 zfpm_g
->stats
.connect_calls
++;
1083 zfpm_g
->last_connect_call_time
= monotime(NULL
);
1085 ret
= connect(sock
, (struct sockaddr
*)&serv
, sizeof(serv
));
1087 zfpm_g
->sock
= sock
;
1088 zfpm_connection_up("connect succeeded");
1092 if (errno
== EINPROGRESS
) {
1093 zfpm_g
->sock
= sock
;
1096 zfpm_set_state(ZFPM_STATE_CONNECTING
,
1097 "async connect in progress");
1101 zlog_info("can't connect to FPM %d: %s", sock
, safe_strerror(errno
));
1105 * Restart timer for retrying connection.
1107 zfpm_start_connect_timer("connect() failed");
1114 * Move state machine into the given state.
1116 static void zfpm_set_state(zfpm_state_t state
, const char *reason
)
1118 zfpm_state_t cur_state
= zfpm_g
->state
;
1123 if (state
== cur_state
)
1126 zfpm_debug("beginning state transition %s -> %s. Reason: %s",
1127 zfpm_state_to_str(cur_state
), zfpm_state_to_str(state
),
1132 case ZFPM_STATE_IDLE
:
1133 assert(cur_state
== ZFPM_STATE_ESTABLISHED
);
1136 case ZFPM_STATE_ACTIVE
:
1137 assert(cur_state
== ZFPM_STATE_IDLE
1138 || cur_state
== ZFPM_STATE_CONNECTING
);
1139 assert(zfpm_g
->t_connect
);
1142 case ZFPM_STATE_CONNECTING
:
1143 assert(zfpm_g
->sock
);
1144 assert(cur_state
== ZFPM_STATE_ACTIVE
);
1145 assert(zfpm_g
->t_read
);
1146 assert(zfpm_g
->t_write
);
1149 case ZFPM_STATE_ESTABLISHED
:
1150 assert(cur_state
== ZFPM_STATE_ACTIVE
1151 || cur_state
== ZFPM_STATE_CONNECTING
);
1152 assert(zfpm_g
->sock
);
1153 assert(zfpm_g
->t_read
);
1154 assert(zfpm_g
->t_write
);
1158 zfpm_g
->state
= state
;
1162 * zfpm_calc_connect_delay
1164 * Returns the number of seconds after which we should attempt to
1165 * reconnect to the FPM.
1167 static long zfpm_calc_connect_delay(void)
1172 * Return 0 if this is our first attempt to connect.
1174 if (zfpm_g
->connect_calls
== 0) {
1178 elapsed
= zfpm_get_elapsed_time(zfpm_g
->last_connect_call_time
);
1180 if (elapsed
> ZFPM_CONNECT_RETRY_IVL
) {
1184 return ZFPM_CONNECT_RETRY_IVL
- elapsed
;
1188 * zfpm_start_connect_timer
1190 static void zfpm_start_connect_timer(const char *reason
)
1194 assert(!zfpm_g
->t_connect
);
1195 assert(zfpm_g
->sock
< 0);
1197 assert(zfpm_g
->state
== ZFPM_STATE_IDLE
1198 || zfpm_g
->state
== ZFPM_STATE_ACTIVE
1199 || zfpm_g
->state
== ZFPM_STATE_CONNECTING
);
1201 delay_secs
= zfpm_calc_connect_delay();
1202 zfpm_debug("scheduling connect in %ld seconds", delay_secs
);
1204 thread_add_timer(zfpm_g
->master
, zfpm_connect_cb
, 0, delay_secs
,
1205 &zfpm_g
->t_connect
);
1206 zfpm_set_state(ZFPM_STATE_ACTIVE
, reason
);
1212 * Returns TRUE if the zebra FPM module has been enabled.
1214 static inline int zfpm_is_enabled(void)
1216 return zfpm_g
->enabled
;
1222 * Returns TRUE if the connection to the FPM is up.
1224 static inline int zfpm_conn_is_up(void)
1226 if (zfpm_g
->state
!= ZFPM_STATE_ESTABLISHED
)
1229 assert(zfpm_g
->sock
>= 0);
1235 * zfpm_trigger_update
1237 * The zebra code invokes this function to indicate that we should
1238 * send an update to the FPM about the given route_node.
1240 static int zfpm_trigger_update(struct route_node
*rn
, const char *reason
)
1243 char buf
[PREFIX_STRLEN
];
1246 * Ignore if the connection is down. We will update the FPM about
1247 * all destinations once the connection comes up.
1249 if (!zfpm_conn_is_up())
1252 dest
= rib_dest_from_rnode(rn
);
1254 if (CHECK_FLAG(dest
->flags
, RIB_DEST_UPDATE_FPM
)) {
1255 zfpm_g
->stats
.redundant_triggers
++;
1260 zfpm_debug("%s triggering update to FPM - Reason: %s",
1261 prefix2str(&rn
->p
, buf
, sizeof(buf
)), reason
);
1264 SET_FLAG(dest
->flags
, RIB_DEST_UPDATE_FPM
);
1265 TAILQ_INSERT_TAIL(&zfpm_g
->dest_q
, dest
, fpm_q_entries
);
1266 zfpm_g
->stats
.updates_triggered
++;
1269 * Make sure that writes are enabled.
1271 if (zfpm_g
->t_write
)
1279 * zfpm_stats_timer_cb
1281 static int zfpm_stats_timer_cb(struct thread
*t
)
1283 zfpm_g
->t_stats
= NULL
;
1286 * Remember the stats collected in the last interval for display
1289 zfpm_stats_copy(&zfpm_g
->stats
, &zfpm_g
->last_ivl_stats
);
1292 * Add the current set of stats into the cumulative statistics.
1294 zfpm_stats_compose(&zfpm_g
->cumulative_stats
, &zfpm_g
->stats
,
1295 &zfpm_g
->cumulative_stats
);
1298 * Start collecting stats afresh over the next interval.
1300 zfpm_stats_reset(&zfpm_g
->stats
);
1302 zfpm_start_stats_timer();
1308 * zfpm_stop_stats_timer
1310 static void zfpm_stop_stats_timer(void)
1312 if (!zfpm_g
->t_stats
)
1315 zfpm_debug("Stopping existing stats timer");
1316 THREAD_TIMER_OFF(zfpm_g
->t_stats
);
1320 * zfpm_start_stats_timer
1322 void zfpm_start_stats_timer(void)
1324 assert(!zfpm_g
->t_stats
);
1326 thread_add_timer(zfpm_g
->master
, zfpm_stats_timer_cb
, 0,
1327 ZFPM_STATS_IVL_SECS
, &zfpm_g
->t_stats
);
1331 * Helper macro for zfpm_show_stats() below.
1333 #define ZFPM_SHOW_STAT(counter) \
1335 vty_out(vty, "%-40s %10lu %16lu\n", #counter, \
1336 total_stats.counter, zfpm_g->last_ivl_stats.counter); \
1342 static void zfpm_show_stats(struct vty
*vty
)
1344 zfpm_stats_t total_stats
;
1347 vty_out(vty
, "\n%-40s %10s Last %2d secs\n\n", "Counter", "Total",
1348 ZFPM_STATS_IVL_SECS
);
1351 * Compute the total stats up to this instant.
1353 zfpm_stats_compose(&zfpm_g
->cumulative_stats
, &zfpm_g
->stats
,
1356 ZFPM_SHOW_STAT(connect_calls
);
1357 ZFPM_SHOW_STAT(connect_no_sock
);
1358 ZFPM_SHOW_STAT(read_cb_calls
);
1359 ZFPM_SHOW_STAT(write_cb_calls
);
1360 ZFPM_SHOW_STAT(write_calls
);
1361 ZFPM_SHOW_STAT(partial_writes
);
1362 ZFPM_SHOW_STAT(max_writes_hit
);
1363 ZFPM_SHOW_STAT(t_write_yields
);
1364 ZFPM_SHOW_STAT(nop_deletes_skipped
);
1365 ZFPM_SHOW_STAT(route_adds
);
1366 ZFPM_SHOW_STAT(route_dels
);
1367 ZFPM_SHOW_STAT(updates_triggered
);
1368 ZFPM_SHOW_STAT(redundant_triggers
);
1369 ZFPM_SHOW_STAT(dests_del_after_update
);
1370 ZFPM_SHOW_STAT(t_conn_down_starts
);
1371 ZFPM_SHOW_STAT(t_conn_down_dests_processed
);
1372 ZFPM_SHOW_STAT(t_conn_down_yields
);
1373 ZFPM_SHOW_STAT(t_conn_down_finishes
);
1374 ZFPM_SHOW_STAT(t_conn_up_starts
);
1375 ZFPM_SHOW_STAT(t_conn_up_dests_processed
);
1376 ZFPM_SHOW_STAT(t_conn_up_yields
);
1377 ZFPM_SHOW_STAT(t_conn_up_aborts
);
1378 ZFPM_SHOW_STAT(t_conn_up_finishes
);
1380 if (!zfpm_g
->last_stats_clear_time
)
1383 elapsed
= zfpm_get_elapsed_time(zfpm_g
->last_stats_clear_time
);
1385 vty_out(vty
, "\nStats were cleared %lu seconds ago\n",
1386 (unsigned long)elapsed
);
1392 static void zfpm_clear_stats(struct vty
*vty
)
1394 if (!zfpm_is_enabled()) {
1395 vty_out(vty
, "The FPM module is not enabled...\n");
1399 zfpm_stats_reset(&zfpm_g
->stats
);
1400 zfpm_stats_reset(&zfpm_g
->last_ivl_stats
);
1401 zfpm_stats_reset(&zfpm_g
->cumulative_stats
);
1403 zfpm_stop_stats_timer();
1404 zfpm_start_stats_timer();
1406 zfpm_g
->last_stats_clear_time
= monotime(NULL
);
1408 vty_out(vty
, "Cleared FPM stats\n");
1412 * show_zebra_fpm_stats
1414 DEFUN (show_zebra_fpm_stats
,
1415 show_zebra_fpm_stats_cmd
,
1416 "show zebra fpm stats",
1419 "Forwarding Path Manager information\n"
1422 zfpm_show_stats(vty
);
1427 * clear_zebra_fpm_stats
1429 DEFUN (clear_zebra_fpm_stats
,
1430 clear_zebra_fpm_stats_cmd
,
1431 "clear zebra fpm stats",
1434 "Clear Forwarding Path Manager information\n"
1437 zfpm_clear_stats(vty
);
1442 * update fpm connection information
1444 DEFUN ( fpm_remote_ip
,
1446 "fpm connection ip A.B.C.D port (1-65535)",
1447 "fpm connection remote ip and port\n"
1448 "Remote fpm server ip A.B.C.D\n"
1452 in_addr_t fpm_server
;
1455 fpm_server
= inet_addr(argv
[3]->arg
);
1456 if (fpm_server
== INADDR_NONE
)
1457 return CMD_ERR_INCOMPLETE
;
1459 port_no
= atoi(argv
[5]->arg
);
1460 if (port_no
< TCP_MIN_PORT
|| port_no
> TCP_MAX_PORT
)
1461 return CMD_ERR_INCOMPLETE
;
1463 zfpm_g
->fpm_server
= fpm_server
;
1464 zfpm_g
->fpm_port
= port_no
;
1470 DEFUN ( no_fpm_remote_ip
,
1471 no_fpm_remote_ip_cmd
,
1472 "no fpm connection ip A.B.C.D port (1-65535)",
1473 "fpm connection remote ip and port\n"
1475 "Remote fpm server ip A.B.C.D\n"
1478 if (zfpm_g
->fpm_server
!= inet_addr(argv
[4]->arg
)
1479 || zfpm_g
->fpm_port
!= atoi(argv
[6]->arg
))
1480 return CMD_ERR_NO_MATCH
;
1482 zfpm_g
->fpm_server
= FPM_DEFAULT_IP
;
1483 zfpm_g
->fpm_port
= FPM_DEFAULT_PORT
;
1489 * zfpm_init_message_format
1491 static inline void zfpm_init_message_format(const char *format
)
1493 int have_netlink
, have_protobuf
;
1501 #ifdef HAVE_PROTOBUF
1507 zfpm_g
->message_format
= ZFPM_MSG_FORMAT_NONE
;
1511 zfpm_g
->message_format
= ZFPM_MSG_FORMAT_NETLINK
;
1512 } else if (have_protobuf
) {
1513 zfpm_g
->message_format
= ZFPM_MSG_FORMAT_PROTOBUF
;
1518 if (!strcmp("netlink", format
)) {
1519 if (!have_netlink
) {
1520 zlog_err("FPM netlink message format is not available");
1523 zfpm_g
->message_format
= ZFPM_MSG_FORMAT_NETLINK
;
1527 if (!strcmp("protobuf", format
)) {
1528 if (!have_protobuf
) {
1530 "FPM protobuf message format is not available");
1533 zfpm_g
->message_format
= ZFPM_MSG_FORMAT_PROTOBUF
;
1537 zlog_warn("Unknown fpm format '%s'", format
);
1541 * fpm_remote_srv_write
1543 * Module to write remote fpm connection
1545 * Returns ZERO on success.
1548 static int fpm_remote_srv_write(struct vty
*vty
)
1552 in
.s_addr
= zfpm_g
->fpm_server
;
1554 if ((zfpm_g
->fpm_server
!= FPM_DEFAULT_IP
1555 && zfpm_g
->fpm_server
!= INADDR_ANY
)
1556 || (zfpm_g
->fpm_port
!= FPM_DEFAULT_PORT
&& zfpm_g
->fpm_port
!= 0))
1557 vty_out(vty
, "fpm connection ip %s port %d\n", inet_ntoa(in
),
1565 static struct cmd_node zebra_node
= {ZEBRA_NODE
, "", 1};
1571 * One-time initialization of the Zebra FPM module.
1573 * @param[in] port port at which FPM is running.
1574 * @param[in] enable TRUE if the zebra FPM module should be enabled
1575 * @param[in] format to use to talk to the FPM. Can be 'netink' or 'protobuf'.
1577 * Returns TRUE on success.
1579 static int zfpm_init(struct thread_master
*master
)
1583 const char *format
= THIS_MODULE
->load_args
;
1585 memset(zfpm_g
, 0, sizeof(*zfpm_g
));
1586 zfpm_g
->master
= master
;
1587 TAILQ_INIT(&zfpm_g
->dest_q
);
1589 zfpm_g
->state
= ZFPM_STATE_IDLE
;
1591 zfpm_stats_init(&zfpm_g
->stats
);
1592 zfpm_stats_init(&zfpm_g
->last_ivl_stats
);
1593 zfpm_stats_init(&zfpm_g
->cumulative_stats
);
1595 install_node(&zebra_node
, fpm_remote_srv_write
);
1596 install_element(ENABLE_NODE
, &show_zebra_fpm_stats_cmd
);
1597 install_element(ENABLE_NODE
, &clear_zebra_fpm_stats_cmd
);
1598 install_element(CONFIG_NODE
, &fpm_remote_ip_cmd
);
1599 install_element(CONFIG_NODE
, &no_fpm_remote_ip_cmd
);
1601 zfpm_init_message_format(format
);
1604 * Disable FPM interface if no suitable format is available.
1606 if (zfpm_g
->message_format
== ZFPM_MSG_FORMAT_NONE
)
1609 zfpm_g
->enabled
= enable
;
1611 if (!zfpm_g
->fpm_server
)
1612 zfpm_g
->fpm_server
= FPM_DEFAULT_IP
;
1615 port
= FPM_DEFAULT_PORT
;
1617 zfpm_g
->fpm_port
= port
;
1619 zfpm_g
->obuf
= stream_new(ZFPM_OBUF_SIZE
);
1620 zfpm_g
->ibuf
= stream_new(ZFPM_IBUF_SIZE
);
1622 zfpm_start_stats_timer();
1623 zfpm_start_connect_timer("initialized");
1627 static int zebra_fpm_module_init(void)
1629 hook_register(rib_update
, zfpm_trigger_update
);
1630 hook_register(frr_late_init
, zfpm_init
);
1634 FRR_MODULE_SETUP(.name
= "zebra_fpm", .version
= FRR_VERSION
,
1635 .description
= "zebra FPM (Forwarding Plane Manager) module",
1636 .init
= zebra_fpm_module_init
, )