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
35 #include "zebra/rib.h"
36 #include "zebra/zserv.h"
37 #include "zebra/zebra_ns.h"
38 #include "zebra/zebra_vrf.h"
39 #include "zebra/zebra_errors.h"
42 #include "zebra_fpm_private.h"
43 #include "zebra/zebra_router.h"
44 #include "zebra_vxlan_private.h"
46 DEFINE_MTYPE_STATIC(ZEBRA
, FPM_MAC_INFO
, "FPM_MAC_INFO");
49 * Interval at which we attempt to connect to the FPM.
51 #define ZFPM_CONNECT_RETRY_IVL 5
54 * Sizes of outgoing and incoming stream buffers for writing/reading
57 #define ZFPM_OBUF_SIZE (2 * FPM_MAX_MSG_LEN)
58 #define ZFPM_IBUF_SIZE (FPM_MAX_MSG_LEN)
61 * The maximum number of times the FPM socket write callback can call
62 * 'write' before it yields.
64 #define ZFPM_MAX_WRITES_PER_RUN 10
67 * Interval over which we collect statistics.
69 #define ZFPM_STATS_IVL_SECS 10
70 #define FPM_MAX_MAC_MSG_LEN 512
72 static void zfpm_iterate_rmac_table(struct hash_bucket
*bucket
, void *args
);
75 * Structure that holds state for iterating over all route_node
76 * structures that are candidates for being communicated to the FPM.
78 struct zfpm_rnodes_iter
{
79 rib_tables_iter_t tables_iter
;
80 route_table_iter_t iter
;
87 unsigned long connect_calls
;
88 unsigned long connect_no_sock
;
90 unsigned long read_cb_calls
;
92 unsigned long write_cb_calls
;
93 unsigned long write_calls
;
94 unsigned long partial_writes
;
95 unsigned long max_writes_hit
;
96 unsigned long t_write_yields
;
98 unsigned long nop_deletes_skipped
;
99 unsigned long route_adds
;
100 unsigned long route_dels
;
102 unsigned long updates_triggered
;
103 unsigned long redundant_triggers
;
105 unsigned long dests_del_after_update
;
107 unsigned long t_conn_down_starts
;
108 unsigned long t_conn_down_dests_processed
;
109 unsigned long t_conn_down_yields
;
110 unsigned long t_conn_down_finishes
;
112 unsigned long t_conn_up_starts
;
113 unsigned long t_conn_up_dests_processed
;
114 unsigned long t_conn_up_yields
;
115 unsigned long t_conn_up_aborts
;
116 unsigned long t_conn_up_finishes
;
120 * States for the FPM state machine.
125 * In this state we are not yet ready to connect to the FPM. This
126 * can happen when this module is disabled, or if we're cleaning up
127 * after a connection has gone down.
132 * Ready to talk to the FPM and periodically trying to connect to
138 * In the middle of bringing up a TCP connection. Specifically,
139 * waiting for a connect() call to complete asynchronously.
141 ZFPM_STATE_CONNECTING
,
144 * TCP connection to the FPM is up.
146 ZFPM_STATE_ESTABLISHED
151 * Message format to be used to communicate with the FPM.
153 enum zfpm_msg_format
{
154 ZFPM_MSG_FORMAT_NONE
,
155 ZFPM_MSG_FORMAT_NETLINK
,
156 ZFPM_MSG_FORMAT_PROTOBUF
,
165 * True if the FPM module has been enabled.
170 * Message format to be used to communicate with the fpm.
172 enum zfpm_msg_format message_format
;
174 struct thread_master
*master
;
176 enum zfpm_state state
;
178 in_addr_t fpm_server
;
180 * Port on which the FPM is running.
185 * List of rib_dest_t structures to be processed
187 TAILQ_HEAD(zfpm_dest_q
, rib_dest_t_
) dest_q
;
190 * List of fpm_mac_info structures to be processed
192 TAILQ_HEAD(zfpm_mac_q
, fpm_mac_info_t
) mac_q
;
195 * Hash table of fpm_mac_info_t entries
197 * While adding fpm_mac_info_t for a MAC to the mac_q,
198 * it is possible that another fpm_mac_info_t node for the this MAC
199 * is already present in the queue.
200 * This is possible in the case of consecutive add->delete operations.
201 * To avoid such duplicate insertions in the mac_q,
202 * define a hash table for fpm_mac_info_t which can be looked up
203 * to see if an fpm_mac_info_t node for a MAC is already present
206 struct hash
*fpm_mac_info_table
;
209 * Stream socket to the FPM.
214 * Buffers for messages to/from the FPM.
222 struct thread
*t_connect
;
223 struct thread
*t_write
;
224 struct thread
*t_read
;
227 * Thread to clean up after the TCP connection to the FPM goes down
228 * and the state that belongs to it.
230 struct thread
*t_conn_down
;
233 struct zfpm_rnodes_iter iter
;
237 * Thread to take actions once the TCP conn to the FPM comes up, and
238 * the state that belongs to it.
240 struct thread
*t_conn_up
;
243 struct zfpm_rnodes_iter iter
;
246 unsigned long connect_calls
;
247 time_t last_connect_call_time
;
250 * Stats from the start of the current statistics interval up to
251 * now. These are the counters we typically update in the code.
253 struct zfpm_stats stats
;
256 * Statistics that were gathered in the last collection interval.
258 struct zfpm_stats last_ivl_stats
;
261 * Cumulative stats from the last clear to the start of the current
262 * statistics interval.
264 struct zfpm_stats cumulative_stats
;
267 * Stats interval timer.
269 struct thread
*t_stats
;
272 * If non-zero, the last time when statistics were cleared.
274 time_t last_stats_clear_time
;
277 * Flag to track the MAC dump status to FPM
279 bool fpm_mac_dump_done
;
282 static struct zfpm_glob zfpm_glob_space
;
283 static struct zfpm_glob
*zfpm_g
= &zfpm_glob_space
;
285 static int zfpm_trigger_update(struct route_node
*rn
, const char *reason
);
287 static int zfpm_read_cb(struct thread
*thread
);
288 static int zfpm_write_cb(struct thread
*thread
);
290 static void zfpm_set_state(enum zfpm_state state
, const char *reason
);
291 static void zfpm_start_connect_timer(const char *reason
);
292 static void zfpm_start_stats_timer(void);
293 static void zfpm_mac_info_del(struct fpm_mac_info_t
*fpm_mac
);
296 * zfpm_thread_should_yield
298 static inline int zfpm_thread_should_yield(struct thread
*t
)
300 return thread_should_yield(t
);
306 static const char *zfpm_state_to_str(enum zfpm_state state
)
310 case ZFPM_STATE_IDLE
:
313 case ZFPM_STATE_ACTIVE
:
316 case ZFPM_STATE_CONNECTING
:
319 case ZFPM_STATE_ESTABLISHED
:
320 return "established";
328 * zfpm_get_elapsed_time
330 * Returns the time elapsed (in seconds) since the given time.
332 static time_t zfpm_get_elapsed_time(time_t reference
)
336 now
= monotime(NULL
);
338 if (now
< reference
) {
343 return now
- reference
;
347 * zfpm_rnodes_iter_init
349 static inline void zfpm_rnodes_iter_init(struct zfpm_rnodes_iter
*iter
)
351 memset(iter
, 0, sizeof(*iter
));
352 rib_tables_iter_init(&iter
->tables_iter
);
355 * This is a hack, but it makes implementing 'next' easier by
356 * ensuring that route_table_iter_next() will return NULL the first
359 route_table_iter_init(&iter
->iter
, NULL
);
360 route_table_iter_cleanup(&iter
->iter
);
364 * zfpm_rnodes_iter_next
366 static inline struct route_node
*
367 zfpm_rnodes_iter_next(struct zfpm_rnodes_iter
*iter
)
369 struct route_node
*rn
;
370 struct route_table
*table
;
373 rn
= route_table_iter_next(&iter
->iter
);
378 * We've made our way through this table, go to the next one.
380 route_table_iter_cleanup(&iter
->iter
);
382 table
= rib_tables_iter_next(&iter
->tables_iter
);
387 route_table_iter_init(&iter
->iter
, table
);
394 * zfpm_rnodes_iter_pause
396 static inline void zfpm_rnodes_iter_pause(struct zfpm_rnodes_iter
*iter
)
398 route_table_iter_pause(&iter
->iter
);
402 * zfpm_rnodes_iter_cleanup
404 static inline void zfpm_rnodes_iter_cleanup(struct zfpm_rnodes_iter
*iter
)
406 route_table_iter_cleanup(&iter
->iter
);
407 rib_tables_iter_cleanup(&iter
->tables_iter
);
413 * Initialize a statistics block.
415 static inline void zfpm_stats_init(struct zfpm_stats
*stats
)
417 memset(stats
, 0, sizeof(*stats
));
423 static inline void zfpm_stats_reset(struct zfpm_stats
*stats
)
425 zfpm_stats_init(stats
);
431 static inline void zfpm_stats_copy(const struct zfpm_stats
*src
,
432 struct zfpm_stats
*dest
)
434 memcpy(dest
, src
, sizeof(*dest
));
440 * Total up the statistics in two stats structures ('s1 and 's2') and
441 * return the result in the third argument, 'result'. Note that the
442 * pointer 'result' may be the same as 's1' or 's2'.
444 * For simplicity, the implementation below assumes that the stats
445 * structure is composed entirely of counters. This can easily be
446 * changed when necessary.
448 static void zfpm_stats_compose(const struct zfpm_stats
*s1
,
449 const struct zfpm_stats
*s2
,
450 struct zfpm_stats
*result
)
452 const unsigned long *p1
, *p2
;
453 unsigned long *result_p
;
456 p1
= (const unsigned long *)s1
;
457 p2
= (const unsigned long *)s2
;
458 result_p
= (unsigned long *)result
;
460 num_counters
= (sizeof(struct zfpm_stats
) / sizeof(unsigned long));
462 for (i
= 0; i
< num_counters
; i
++) {
463 result_p
[i
] = p1
[i
] + p2
[i
];
470 static inline void zfpm_read_on(void)
472 assert(!zfpm_g
->t_read
);
473 assert(zfpm_g
->sock
>= 0);
475 thread_add_read(zfpm_g
->master
, zfpm_read_cb
, 0, zfpm_g
->sock
,
482 static inline void zfpm_write_on(void)
484 assert(!zfpm_g
->t_write
);
485 assert(zfpm_g
->sock
>= 0);
487 thread_add_write(zfpm_g
->master
, zfpm_write_cb
, 0, zfpm_g
->sock
,
494 static inline void zfpm_read_off(void)
496 thread_cancel(&zfpm_g
->t_read
);
502 static inline void zfpm_write_off(void)
504 thread_cancel(&zfpm_g
->t_write
);
507 static inline void zfpm_connect_off(void)
509 thread_cancel(&zfpm_g
->t_connect
);
513 * zfpm_conn_up_thread_cb
515 * Callback for actions to be taken when the connection to the FPM
518 static int zfpm_conn_up_thread_cb(struct thread
*thread
)
520 struct route_node
*rnode
;
521 struct zfpm_rnodes_iter
*iter
;
524 iter
= &zfpm_g
->t_conn_up_state
.iter
;
526 if (zfpm_g
->state
!= ZFPM_STATE_ESTABLISHED
) {
528 "Connection not up anymore, conn_up thread aborting");
529 zfpm_g
->stats
.t_conn_up_aborts
++;
533 if (!zfpm_g
->fpm_mac_dump_done
) {
534 /* Enqueue FPM updates for all the RMAC entries */
535 hash_iterate(zrouter
.l3vni_table
, zfpm_iterate_rmac_table
,
537 /* mark dump done so that its not repeated after yield */
538 zfpm_g
->fpm_mac_dump_done
= true;
541 while ((rnode
= zfpm_rnodes_iter_next(iter
))) {
542 dest
= rib_dest_from_rnode(rnode
);
545 zfpm_g
->stats
.t_conn_up_dests_processed
++;
546 zfpm_trigger_update(rnode
, NULL
);
552 if (!zfpm_thread_should_yield(thread
))
555 zfpm_g
->stats
.t_conn_up_yields
++;
556 zfpm_rnodes_iter_pause(iter
);
557 thread_add_timer_msec(zfpm_g
->master
, zfpm_conn_up_thread_cb
,
558 NULL
, 0, &zfpm_g
->t_conn_up
);
562 zfpm_g
->stats
.t_conn_up_finishes
++;
565 zfpm_rnodes_iter_cleanup(iter
);
572 * Called when the connection to the FPM comes up.
574 static void zfpm_connection_up(const char *detail
)
576 assert(zfpm_g
->sock
>= 0);
579 zfpm_set_state(ZFPM_STATE_ESTABLISHED
, detail
);
582 * Start thread to push existing routes to the FPM.
584 thread_cancel(&zfpm_g
->t_conn_up
);
586 zfpm_rnodes_iter_init(&zfpm_g
->t_conn_up_state
.iter
);
587 zfpm_g
->fpm_mac_dump_done
= false;
589 zfpm_debug("Starting conn_up thread");
591 thread_add_timer_msec(zfpm_g
->master
, zfpm_conn_up_thread_cb
, NULL
, 0,
593 zfpm_g
->stats
.t_conn_up_starts
++;
599 * Check if an asynchronous connect() to the FPM is complete.
601 static void zfpm_connect_check(void)
610 slen
= sizeof(status
);
611 ret
= getsockopt(zfpm_g
->sock
, SOL_SOCKET
, SO_ERROR
, (void *)&status
,
614 if (ret
>= 0 && status
== 0) {
615 zfpm_connection_up("async connect complete");
620 * getsockopt() failed or indicated an error on the socket.
625 zfpm_start_connect_timer("getsockopt() after async connect failed");
630 * zfpm_conn_down_thread_cb
632 * Callback that is invoked to clean up state after the TCP connection
633 * to the FPM goes down.
635 static int zfpm_conn_down_thread_cb(struct thread
*thread
)
637 struct route_node
*rnode
;
638 struct zfpm_rnodes_iter
*iter
;
640 struct fpm_mac_info_t
*mac
= NULL
;
642 assert(zfpm_g
->state
== ZFPM_STATE_IDLE
);
645 * Delink and free all fpm_mac_info_t nodes
646 * in the mac_q and fpm_mac_info_hash
648 while ((mac
= TAILQ_FIRST(&zfpm_g
->mac_q
)) != NULL
)
649 zfpm_mac_info_del(mac
);
651 zfpm_g
->t_conn_down
= NULL
;
653 iter
= &zfpm_g
->t_conn_down_state
.iter
;
655 while ((rnode
= zfpm_rnodes_iter_next(iter
))) {
656 dest
= rib_dest_from_rnode(rnode
);
659 if (CHECK_FLAG(dest
->flags
, RIB_DEST_UPDATE_FPM
)) {
660 TAILQ_REMOVE(&zfpm_g
->dest_q
, dest
,
664 UNSET_FLAG(dest
->flags
, RIB_DEST_UPDATE_FPM
);
665 UNSET_FLAG(dest
->flags
, RIB_DEST_SENT_TO_FPM
);
667 zfpm_g
->stats
.t_conn_down_dests_processed
++;
670 * Check if the dest should be deleted.
678 if (!zfpm_thread_should_yield(thread
))
681 zfpm_g
->stats
.t_conn_down_yields
++;
682 zfpm_rnodes_iter_pause(iter
);
683 zfpm_g
->t_conn_down
= NULL
;
684 thread_add_timer_msec(zfpm_g
->master
, zfpm_conn_down_thread_cb
,
685 NULL
, 0, &zfpm_g
->t_conn_down
);
689 zfpm_g
->stats
.t_conn_down_finishes
++;
690 zfpm_rnodes_iter_cleanup(iter
);
693 * Start the process of connecting to the FPM again.
695 zfpm_start_connect_timer("cleanup complete");
700 * zfpm_connection_down
702 * Called when the connection to the FPM has gone down.
704 static void zfpm_connection_down(const char *detail
)
709 assert(zfpm_g
->state
== ZFPM_STATE_ESTABLISHED
);
711 zlog_info("connection to the FPM has gone down: %s", detail
);
716 stream_reset(zfpm_g
->ibuf
);
717 stream_reset(zfpm_g
->obuf
);
719 if (zfpm_g
->sock
>= 0) {
725 * Start thread to clean up state after the connection goes down.
727 assert(!zfpm_g
->t_conn_down
);
728 zfpm_rnodes_iter_init(&zfpm_g
->t_conn_down_state
.iter
);
729 zfpm_g
->t_conn_down
= NULL
;
730 thread_add_timer_msec(zfpm_g
->master
, zfpm_conn_down_thread_cb
, NULL
, 0,
731 &zfpm_g
->t_conn_down
);
732 zfpm_g
->stats
.t_conn_down_starts
++;
734 zfpm_set_state(ZFPM_STATE_IDLE
, detail
);
740 static int zfpm_read_cb(struct thread
*thread
)
747 zfpm_g
->stats
.read_cb_calls
++;
750 * Check if async connect is now done.
752 if (zfpm_g
->state
== ZFPM_STATE_CONNECTING
) {
753 zfpm_connect_check();
757 assert(zfpm_g
->state
== ZFPM_STATE_ESTABLISHED
);
758 assert(zfpm_g
->sock
>= 0);
762 already
= stream_get_endp(ibuf
);
763 if (already
< FPM_MSG_HDR_LEN
) {
766 nbyte
= stream_read_try(ibuf
, zfpm_g
->sock
,
767 FPM_MSG_HDR_LEN
- already
);
768 if (nbyte
== 0 || nbyte
== -1) {
772 snprintf(buffer
, sizeof(buffer
),
773 "closed socket in read(%d): %s", errno
,
774 safe_strerror(errno
));
775 zfpm_connection_down(buffer
);
777 zfpm_connection_down("closed socket in read");
781 if (nbyte
!= (ssize_t
)(FPM_MSG_HDR_LEN
- already
))
784 already
= FPM_MSG_HDR_LEN
;
787 stream_set_getp(ibuf
, 0);
789 hdr
= (fpm_msg_hdr_t
*)stream_pnt(ibuf
);
791 if (!fpm_msg_hdr_ok(hdr
)) {
792 zfpm_connection_down("invalid message header");
796 msg_len
= fpm_msg_len(hdr
);
799 * Read out the rest of the packet.
801 if (already
< msg_len
) {
804 nbyte
= stream_read_try(ibuf
, zfpm_g
->sock
, msg_len
- already
);
806 if (nbyte
== 0 || nbyte
== -1) {
810 snprintf(buffer
, sizeof(buffer
),
811 "failed to read message(%d) %s", errno
,
812 safe_strerror(errno
));
813 zfpm_connection_down(buffer
);
815 zfpm_connection_down("failed to read message");
819 if (nbyte
!= (ssize_t
)(msg_len
- already
))
824 * Just throw it away for now.
833 static bool zfpm_updates_pending(void)
835 if (!(TAILQ_EMPTY(&zfpm_g
->dest_q
)) || !(TAILQ_EMPTY(&zfpm_g
->mac_q
)))
842 * zfpm_writes_pending
844 * Returns true if we may have something to write to the FPM.
846 static int zfpm_writes_pending(void)
850 * Check if there is any data in the outbound buffer that has not
851 * been written to the socket yet.
853 if (stream_get_endp(zfpm_g
->obuf
) - stream_get_getp(zfpm_g
->obuf
))
857 * Check if there are any updates scheduled on the outbound queues.
859 if (zfpm_updates_pending())
868 * Encode a message to the FPM with information about the given route.
870 * Returns the number of bytes written to the buffer. 0 or a negative
871 * value indicates an error.
873 static inline int zfpm_encode_route(rib_dest_t
*dest
, struct route_entry
*re
,
874 char *in_buf
, size_t in_buf_len
,
875 fpm_msg_type_e
*msg_type
)
883 *msg_type
= FPM_MSG_TYPE_NONE
;
885 switch (zfpm_g
->message_format
) {
887 case ZFPM_MSG_FORMAT_PROTOBUF
:
889 len
= zfpm_protobuf_encode_route(dest
, re
, (uint8_t *)in_buf
,
891 *msg_type
= FPM_MSG_TYPE_PROTOBUF
;
895 case ZFPM_MSG_FORMAT_NETLINK
:
897 *msg_type
= FPM_MSG_TYPE_NETLINK
;
898 cmd
= re
? RTM_NEWROUTE
: RTM_DELROUTE
;
899 len
= zfpm_netlink_encode_route(cmd
, dest
, re
, in_buf
,
901 assert(fpm_msg_align(len
) == len
);
902 *msg_type
= FPM_MSG_TYPE_NETLINK
;
903 #endif /* HAVE_NETLINK */
914 * zfpm_route_for_update
916 * Returns the re that is to be sent to the FPM for a given dest.
918 struct route_entry
*zfpm_route_for_update(rib_dest_t
*dest
)
920 return dest
->selected_fib
;
924 * Define an enum for return codes for queue processing functions
926 * FPM_WRITE_STOP: This return code indicates that the write buffer is full.
927 * Stop processing all the queues and empty the buffer by writing its content
930 * FPM_GOTO_NEXT_Q: This return code indicates that either this queue is
931 * empty or we have processed enough updates from this queue.
932 * So, move on to the next queue.
939 #define FPM_QUEUE_PROCESS_LIMIT 10000
942 * zfpm_build_route_updates
944 * Process the dest_q queue and write FPM messages to the outbound buffer.
946 static int zfpm_build_route_updates(void)
950 unsigned char *buf
, *data
, *buf_end
;
954 struct route_entry
*re
;
955 int is_add
, write_msg
;
956 fpm_msg_type_e msg_type
;
959 if (TAILQ_EMPTY(&zfpm_g
->dest_q
))
960 return FPM_GOTO_NEXT_Q
;
963 q_limit
= FPM_QUEUE_PROCESS_LIMIT
;
967 * Make sure there is enough space to write another message.
969 if (STREAM_WRITEABLE(s
) < FPM_MAX_MSG_LEN
)
970 return FPM_WRITE_STOP
;
972 buf
= STREAM_DATA(s
) + stream_get_endp(s
);
973 buf_end
= buf
+ STREAM_WRITEABLE(s
);
975 dest
= TAILQ_FIRST(&zfpm_g
->dest_q
);
977 return FPM_GOTO_NEXT_Q
;
979 assert(CHECK_FLAG(dest
->flags
, RIB_DEST_UPDATE_FPM
));
981 hdr
= (fpm_msg_hdr_t
*)buf
;
982 hdr
->version
= FPM_PROTO_VERSION
;
984 data
= fpm_msg_data(hdr
);
986 re
= zfpm_route_for_update(dest
);
992 * If this is a route deletion, and we have not sent the route
994 * the FPM previously, skip it.
996 if (!is_add
&& !CHECK_FLAG(dest
->flags
, RIB_DEST_SENT_TO_FPM
)) {
998 zfpm_g
->stats
.nop_deletes_skipped
++;
1002 data_len
= zfpm_encode_route(dest
, re
, (char *)data
,
1003 buf_end
- data
, &msg_type
);
1007 hdr
->msg_type
= msg_type
;
1008 msg_len
= fpm_data_len_to_msg_len(data_len
);
1009 hdr
->msg_len
= htons(msg_len
);
1010 stream_forward_endp(s
, msg_len
);
1013 zfpm_g
->stats
.route_adds
++;
1015 zfpm_g
->stats
.route_dels
++;
1020 * Remove the dest from the queue, and reset the flag.
1022 UNSET_FLAG(dest
->flags
, RIB_DEST_UPDATE_FPM
);
1023 TAILQ_REMOVE(&zfpm_g
->dest_q
, dest
, fpm_q_entries
);
1026 SET_FLAG(dest
->flags
, RIB_DEST_SENT_TO_FPM
);
1028 UNSET_FLAG(dest
->flags
, RIB_DEST_SENT_TO_FPM
);
1032 * Delete the destination if necessary.
1034 if (rib_gc_dest(dest
->rnode
))
1035 zfpm_g
->stats
.dests_del_after_update
++;
1040 * We have processed enough updates in this queue.
1041 * Now yield for other queues.
1043 return FPM_GOTO_NEXT_Q
;
1051 * Encode a message to FPM with information about the given MAC.
1053 * Returns the number of bytes written to the buffer.
1055 static inline int zfpm_encode_mac(struct fpm_mac_info_t
*mac
, char *in_buf
,
1056 size_t in_buf_len
, fpm_msg_type_e
*msg_type
)
1060 *msg_type
= FPM_MSG_TYPE_NONE
;
1062 switch (zfpm_g
->message_format
) {
1064 case ZFPM_MSG_FORMAT_NONE
:
1066 case ZFPM_MSG_FORMAT_NETLINK
:
1068 len
= zfpm_netlink_encode_mac(mac
, in_buf
, in_buf_len
);
1069 assert(fpm_msg_align(len
) == len
);
1070 *msg_type
= FPM_MSG_TYPE_NETLINK
;
1071 #endif /* HAVE_NETLINK */
1073 case ZFPM_MSG_FORMAT_PROTOBUF
:
1079 static int zfpm_build_mac_updates(void)
1082 struct fpm_mac_info_t
*mac
;
1083 unsigned char *buf
, *data
, *buf_end
;
1085 size_t data_len
, msg_len
;
1086 fpm_msg_type_e msg_type
;
1089 if (TAILQ_EMPTY(&zfpm_g
->mac_q
))
1090 return FPM_GOTO_NEXT_Q
;
1093 q_limit
= FPM_QUEUE_PROCESS_LIMIT
;
1096 /* Make sure there is enough space to write another message. */
1097 if (STREAM_WRITEABLE(s
) < FPM_MAX_MAC_MSG_LEN
)
1098 return FPM_WRITE_STOP
;
1100 buf
= STREAM_DATA(s
) + stream_get_endp(s
);
1101 buf_end
= buf
+ STREAM_WRITEABLE(s
);
1103 mac
= TAILQ_FIRST(&zfpm_g
->mac_q
);
1105 return FPM_GOTO_NEXT_Q
;
1107 /* Check for no-op */
1108 if (!CHECK_FLAG(mac
->fpm_flags
, ZEBRA_MAC_UPDATE_FPM
)) {
1109 zfpm_g
->stats
.nop_deletes_skipped
++;
1110 zfpm_mac_info_del(mac
);
1114 hdr
= (fpm_msg_hdr_t
*)buf
;
1115 hdr
->version
= FPM_PROTO_VERSION
;
1117 data
= fpm_msg_data(hdr
);
1118 data_len
= zfpm_encode_mac(mac
, (char *)data
, buf_end
- data
,
1122 hdr
->msg_type
= msg_type
;
1123 msg_len
= fpm_data_len_to_msg_len(data_len
);
1124 hdr
->msg_len
= htons(msg_len
);
1125 stream_forward_endp(s
, msg_len
);
1127 /* Remove the MAC from the queue, and delete it. */
1128 zfpm_mac_info_del(mac
);
1133 * We have processed enough updates in this queue.
1134 * Now yield for other queues.
1136 return FPM_GOTO_NEXT_Q
;
1142 * zfpm_build_updates
1144 * Process the outgoing queues and write messages to the outbound
1147 static void zfpm_build_updates(void)
1152 assert(stream_empty(s
));
1156 * Stop processing the queues if zfpm_g->obuf is full
1157 * or we do not have more updates to process
1159 if (zfpm_build_mac_updates() == FPM_WRITE_STOP
)
1161 if (zfpm_build_route_updates() == FPM_WRITE_STOP
)
1163 } while (zfpm_updates_pending());
1169 static int zfpm_write_cb(struct thread
*thread
)
1174 zfpm_g
->stats
.write_cb_calls
++;
1177 * Check if async connect is now done.
1179 if (zfpm_g
->state
== ZFPM_STATE_CONNECTING
) {
1180 zfpm_connect_check();
1184 assert(zfpm_g
->state
== ZFPM_STATE_ESTABLISHED
);
1185 assert(zfpm_g
->sock
>= 0);
1190 int bytes_to_write
, bytes_written
;
1195 * If the stream is empty, try fill it up with data.
1197 if (stream_empty(s
)) {
1198 zfpm_build_updates();
1201 bytes_to_write
= stream_get_endp(s
) - stream_get_getp(s
);
1202 if (!bytes_to_write
)
1206 write(zfpm_g
->sock
, stream_pnt(s
), bytes_to_write
);
1207 zfpm_g
->stats
.write_calls
++;
1210 if (bytes_written
< 0) {
1211 if (ERRNO_IO_RETRY(errno
))
1214 zfpm_connection_down("failed to write to socket");
1218 if (bytes_written
!= bytes_to_write
) {
1223 stream_forward_getp(s
, bytes_written
);
1224 zfpm_g
->stats
.partial_writes
++;
1229 * We've written out the entire contents of the stream.
1233 if (num_writes
>= ZFPM_MAX_WRITES_PER_RUN
) {
1234 zfpm_g
->stats
.max_writes_hit
++;
1238 if (zfpm_thread_should_yield(thread
)) {
1239 zfpm_g
->stats
.t_write_yields
++;
1244 if (zfpm_writes_pending())
1253 static int zfpm_connect_cb(struct thread
*t
)
1256 struct sockaddr_in serv
;
1258 assert(zfpm_g
->state
== ZFPM_STATE_ACTIVE
);
1260 sock
= socket(AF_INET
, SOCK_STREAM
, 0);
1262 zlog_err("Failed to create socket for connect(): %s",
1264 zfpm_g
->stats
.connect_no_sock
++;
1268 set_nonblocking(sock
);
1270 /* Make server socket. */
1271 memset(&serv
, 0, sizeof(serv
));
1272 serv
.sin_family
= AF_INET
;
1273 serv
.sin_port
= htons(zfpm_g
->fpm_port
);
1274 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
1275 serv
.sin_len
= sizeof(struct sockaddr_in
);
1276 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
1277 if (!zfpm_g
->fpm_server
)
1278 serv
.sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
1280 serv
.sin_addr
.s_addr
= (zfpm_g
->fpm_server
);
1283 * Connect to the FPM.
1285 zfpm_g
->connect_calls
++;
1286 zfpm_g
->stats
.connect_calls
++;
1287 zfpm_g
->last_connect_call_time
= monotime(NULL
);
1289 ret
= connect(sock
, (struct sockaddr
*)&serv
, sizeof(serv
));
1291 zfpm_g
->sock
= sock
;
1292 zfpm_connection_up("connect succeeded");
1296 if (errno
== EINPROGRESS
) {
1297 zfpm_g
->sock
= sock
;
1300 zfpm_set_state(ZFPM_STATE_CONNECTING
,
1301 "async connect in progress");
1305 zlog_info("can't connect to FPM %d: %s", sock
, safe_strerror(errno
));
1309 * Restart timer for retrying connection.
1311 zfpm_start_connect_timer("connect() failed");
1318 * Move state machine into the given state.
1320 static void zfpm_set_state(enum zfpm_state state
, const char *reason
)
1322 enum zfpm_state cur_state
= zfpm_g
->state
;
1327 if (state
== cur_state
)
1330 zfpm_debug("beginning state transition %s -> %s. Reason: %s",
1331 zfpm_state_to_str(cur_state
), zfpm_state_to_str(state
),
1336 case ZFPM_STATE_IDLE
:
1337 assert(cur_state
== ZFPM_STATE_ESTABLISHED
);
1340 case ZFPM_STATE_ACTIVE
:
1341 assert(cur_state
== ZFPM_STATE_IDLE
1342 || cur_state
== ZFPM_STATE_CONNECTING
);
1343 assert(zfpm_g
->t_connect
);
1346 case ZFPM_STATE_CONNECTING
:
1347 assert(zfpm_g
->sock
);
1348 assert(cur_state
== ZFPM_STATE_ACTIVE
);
1349 assert(zfpm_g
->t_read
);
1350 assert(zfpm_g
->t_write
);
1353 case ZFPM_STATE_ESTABLISHED
:
1354 assert(cur_state
== ZFPM_STATE_ACTIVE
1355 || cur_state
== ZFPM_STATE_CONNECTING
);
1356 assert(zfpm_g
->sock
);
1357 assert(zfpm_g
->t_read
);
1358 assert(zfpm_g
->t_write
);
1362 zfpm_g
->state
= state
;
1366 * zfpm_calc_connect_delay
1368 * Returns the number of seconds after which we should attempt to
1369 * reconnect to the FPM.
1371 static long zfpm_calc_connect_delay(void)
1376 * Return 0 if this is our first attempt to connect.
1378 if (zfpm_g
->connect_calls
== 0) {
1382 elapsed
= zfpm_get_elapsed_time(zfpm_g
->last_connect_call_time
);
1384 if (elapsed
> ZFPM_CONNECT_RETRY_IVL
) {
1388 return ZFPM_CONNECT_RETRY_IVL
- elapsed
;
1392 * zfpm_start_connect_timer
1394 static void zfpm_start_connect_timer(const char *reason
)
1398 assert(!zfpm_g
->t_connect
);
1399 assert(zfpm_g
->sock
< 0);
1401 assert(zfpm_g
->state
== ZFPM_STATE_IDLE
1402 || zfpm_g
->state
== ZFPM_STATE_ACTIVE
1403 || zfpm_g
->state
== ZFPM_STATE_CONNECTING
);
1405 delay_secs
= zfpm_calc_connect_delay();
1406 zfpm_debug("scheduling connect in %ld seconds", delay_secs
);
1408 thread_add_timer(zfpm_g
->master
, zfpm_connect_cb
, 0, delay_secs
,
1409 &zfpm_g
->t_connect
);
1410 zfpm_set_state(ZFPM_STATE_ACTIVE
, reason
);
1416 * Returns true if the zebra FPM module has been enabled.
1418 static inline int zfpm_is_enabled(void)
1420 return zfpm_g
->enabled
;
1426 * Returns true if the connection to the FPM is up.
1428 static inline int zfpm_conn_is_up(void)
1430 if (zfpm_g
->state
!= ZFPM_STATE_ESTABLISHED
)
1433 assert(zfpm_g
->sock
>= 0);
1439 * zfpm_trigger_update
1441 * The zebra code invokes this function to indicate that we should
1442 * send an update to the FPM about the given route_node.
1444 static int zfpm_trigger_update(struct route_node
*rn
, const char *reason
)
1449 * Ignore if the connection is down. We will update the FPM about
1450 * all destinations once the connection comes up.
1452 if (!zfpm_conn_is_up())
1455 dest
= rib_dest_from_rnode(rn
);
1457 if (CHECK_FLAG(dest
->flags
, RIB_DEST_UPDATE_FPM
)) {
1458 zfpm_g
->stats
.redundant_triggers
++;
1463 zfpm_debug("%pFX triggering update to FPM - Reason: %s", &rn
->p
,
1467 SET_FLAG(dest
->flags
, RIB_DEST_UPDATE_FPM
);
1468 TAILQ_INSERT_TAIL(&zfpm_g
->dest_q
, dest
, fpm_q_entries
);
1469 zfpm_g
->stats
.updates_triggered
++;
1472 * Make sure that writes are enabled.
1474 if (zfpm_g
->t_write
)
1482 * Generate Key for FPM MAC info hash entry
1484 static unsigned int zfpm_mac_info_hash_keymake(const void *p
)
1486 struct fpm_mac_info_t
*fpm_mac
= (struct fpm_mac_info_t
*)p
;
1489 mac_key
= jhash(fpm_mac
->macaddr
.octet
, ETH_ALEN
, 0xa5a5a55a);
1491 return jhash_2words(mac_key
, fpm_mac
->vni
, 0);
1495 * Compare function for FPM MAC info hash lookup
1497 static bool zfpm_mac_info_cmp(const void *p1
, const void *p2
)
1499 const struct fpm_mac_info_t
*fpm_mac1
= p1
;
1500 const struct fpm_mac_info_t
*fpm_mac2
= p2
;
1502 if (memcmp(fpm_mac1
->macaddr
.octet
, fpm_mac2
->macaddr
.octet
, ETH_ALEN
)
1505 if (fpm_mac1
->vni
!= fpm_mac2
->vni
)
1512 * Lookup FPM MAC info hash entry.
1514 static struct fpm_mac_info_t
*zfpm_mac_info_lookup(struct fpm_mac_info_t
*key
)
1516 return hash_lookup(zfpm_g
->fpm_mac_info_table
, key
);
1520 * Callback to allocate fpm_mac_info_t structure.
1522 static void *zfpm_mac_info_alloc(void *p
)
1524 const struct fpm_mac_info_t
*key
= p
;
1525 struct fpm_mac_info_t
*fpm_mac
;
1527 fpm_mac
= XCALLOC(MTYPE_FPM_MAC_INFO
, sizeof(struct fpm_mac_info_t
));
1529 memcpy(&fpm_mac
->macaddr
, &key
->macaddr
, ETH_ALEN
);
1530 fpm_mac
->vni
= key
->vni
;
1532 return (void *)fpm_mac
;
1536 * Delink and free fpm_mac_info_t.
1538 static void zfpm_mac_info_del(struct fpm_mac_info_t
*fpm_mac
)
1540 hash_release(zfpm_g
->fpm_mac_info_table
, fpm_mac
);
1541 TAILQ_REMOVE(&zfpm_g
->mac_q
, fpm_mac
, fpm_mac_q_entries
);
1542 XFREE(MTYPE_FPM_MAC_INFO
, fpm_mac
);
1546 * zfpm_trigger_rmac_update
1548 * Zebra code invokes this function to indicate that we should
1549 * send an update to FPM for given MAC entry.
1551 * This function checks if we already have enqueued an update for this RMAC,
1552 * If yes, update the same fpm_mac_info_t. Else, create and enqueue an update.
1554 static int zfpm_trigger_rmac_update(zebra_mac_t
*rmac
, zebra_l3vni_t
*zl3vni
,
1555 bool delete, const char *reason
)
1557 struct fpm_mac_info_t
*fpm_mac
, key
;
1558 struct interface
*vxlan_if
, *svi_if
;
1559 bool mac_found
= false;
1562 * Ignore if the connection is down. We will update the FPM about
1563 * all destinations once the connection comes up.
1565 if (!zfpm_conn_is_up())
1569 zfpm_debug("triggering update to FPM - Reason: %s - %pEA",
1570 reason
, &rmac
->macaddr
);
1573 vxlan_if
= zl3vni_map_to_vxlan_if(zl3vni
);
1574 svi_if
= zl3vni_map_to_svi_if(zl3vni
);
1576 memset(&key
, 0, sizeof(struct fpm_mac_info_t
));
1578 memcpy(&key
.macaddr
, &rmac
->macaddr
, ETH_ALEN
);
1579 key
.vni
= zl3vni
->vni
;
1581 /* Check if this MAC is already present in the queue. */
1582 fpm_mac
= zfpm_mac_info_lookup(&key
);
1588 * If the enqueued op is "add" and current op is "delete",
1589 * this is a noop. So, Unset ZEBRA_MAC_UPDATE_FPM flag.
1590 * While processing FPM queue, we will silently delete this
1591 * MAC entry without sending any update for this MAC.
1593 if (!CHECK_FLAG(fpm_mac
->fpm_flags
, ZEBRA_MAC_DELETE_FPM
) &&
1595 SET_FLAG(fpm_mac
->fpm_flags
, ZEBRA_MAC_DELETE_FPM
);
1596 UNSET_FLAG(fpm_mac
->fpm_flags
, ZEBRA_MAC_UPDATE_FPM
);
1600 fpm_mac
= hash_get(zfpm_g
->fpm_mac_info_table
, &key
,
1601 zfpm_mac_info_alloc
);
1606 fpm_mac
->r_vtep_ip
.s_addr
= rmac
->fwd_info
.r_vtep_ip
.s_addr
;
1607 fpm_mac
->zebra_flags
= rmac
->flags
;
1608 fpm_mac
->vxlan_if
= vxlan_if
? vxlan_if
->ifindex
: 0;
1609 fpm_mac
->svi_if
= svi_if
? svi_if
->ifindex
: 0;
1611 SET_FLAG(fpm_mac
->fpm_flags
, ZEBRA_MAC_UPDATE_FPM
);
1613 SET_FLAG(fpm_mac
->fpm_flags
, ZEBRA_MAC_DELETE_FPM
);
1615 UNSET_FLAG(fpm_mac
->fpm_flags
, ZEBRA_MAC_DELETE_FPM
);
1618 TAILQ_INSERT_TAIL(&zfpm_g
->mac_q
, fpm_mac
, fpm_mac_q_entries
);
1620 zfpm_g
->stats
.updates_triggered
++;
1622 /* If writes are already enabled, return. */
1623 if (zfpm_g
->t_write
)
1631 * This function is called when the FPM connections is established.
1632 * Iterate over all the RMAC entries for the given L3VNI
1633 * and enqueue the RMAC for FPM processing.
1635 static void zfpm_trigger_rmac_update_wrapper(struct hash_bucket
*bucket
,
1638 zebra_mac_t
*zrmac
= (zebra_mac_t
*)bucket
->data
;
1639 zebra_l3vni_t
*zl3vni
= (zebra_l3vni_t
*)args
;
1641 zfpm_trigger_rmac_update(zrmac
, zl3vni
, false, "RMAC added");
1645 * This function is called when the FPM connections is established.
1646 * This function iterates over all the L3VNIs to trigger
1647 * FPM updates for RMACs currently available.
1649 static void zfpm_iterate_rmac_table(struct hash_bucket
*bucket
, void *args
)
1651 zebra_l3vni_t
*zl3vni
= (zebra_l3vni_t
*)bucket
->data
;
1653 hash_iterate(zl3vni
->rmac_table
, zfpm_trigger_rmac_update_wrapper
,
1658 * struct zfpm_statsimer_cb
1660 static int zfpm_stats_timer_cb(struct thread
*t
)
1662 zfpm_g
->t_stats
= NULL
;
1665 * Remember the stats collected in the last interval for display
1668 zfpm_stats_copy(&zfpm_g
->stats
, &zfpm_g
->last_ivl_stats
);
1671 * Add the current set of stats into the cumulative statistics.
1673 zfpm_stats_compose(&zfpm_g
->cumulative_stats
, &zfpm_g
->stats
,
1674 &zfpm_g
->cumulative_stats
);
1677 * Start collecting stats afresh over the next interval.
1679 zfpm_stats_reset(&zfpm_g
->stats
);
1681 zfpm_start_stats_timer();
1687 * zfpm_stop_stats_timer
1689 static void zfpm_stop_stats_timer(void)
1691 if (!zfpm_g
->t_stats
)
1694 zfpm_debug("Stopping existing stats timer");
1695 thread_cancel(&zfpm_g
->t_stats
);
1699 * zfpm_start_stats_timer
1701 void zfpm_start_stats_timer(void)
1703 assert(!zfpm_g
->t_stats
);
1705 thread_add_timer(zfpm_g
->master
, zfpm_stats_timer_cb
, 0,
1706 ZFPM_STATS_IVL_SECS
, &zfpm_g
->t_stats
);
1710 * Helper macro for zfpm_show_stats() below.
1712 #define ZFPM_SHOW_STAT(counter) \
1714 vty_out(vty, "%-40s %10lu %16lu\n", #counter, \
1715 total_stats.counter, zfpm_g->last_ivl_stats.counter); \
1721 static void zfpm_show_stats(struct vty
*vty
)
1723 struct zfpm_stats total_stats
;
1726 vty_out(vty
, "\n%-40s %10s Last %2d secs\n\n", "Counter", "Total",
1727 ZFPM_STATS_IVL_SECS
);
1730 * Compute the total stats up to this instant.
1732 zfpm_stats_compose(&zfpm_g
->cumulative_stats
, &zfpm_g
->stats
,
1735 ZFPM_SHOW_STAT(connect_calls
);
1736 ZFPM_SHOW_STAT(connect_no_sock
);
1737 ZFPM_SHOW_STAT(read_cb_calls
);
1738 ZFPM_SHOW_STAT(write_cb_calls
);
1739 ZFPM_SHOW_STAT(write_calls
);
1740 ZFPM_SHOW_STAT(partial_writes
);
1741 ZFPM_SHOW_STAT(max_writes_hit
);
1742 ZFPM_SHOW_STAT(t_write_yields
);
1743 ZFPM_SHOW_STAT(nop_deletes_skipped
);
1744 ZFPM_SHOW_STAT(route_adds
);
1745 ZFPM_SHOW_STAT(route_dels
);
1746 ZFPM_SHOW_STAT(updates_triggered
);
1747 ZFPM_SHOW_STAT(redundant_triggers
);
1748 ZFPM_SHOW_STAT(dests_del_after_update
);
1749 ZFPM_SHOW_STAT(t_conn_down_starts
);
1750 ZFPM_SHOW_STAT(t_conn_down_dests_processed
);
1751 ZFPM_SHOW_STAT(t_conn_down_yields
);
1752 ZFPM_SHOW_STAT(t_conn_down_finishes
);
1753 ZFPM_SHOW_STAT(t_conn_up_starts
);
1754 ZFPM_SHOW_STAT(t_conn_up_dests_processed
);
1755 ZFPM_SHOW_STAT(t_conn_up_yields
);
1756 ZFPM_SHOW_STAT(t_conn_up_aborts
);
1757 ZFPM_SHOW_STAT(t_conn_up_finishes
);
1759 if (!zfpm_g
->last_stats_clear_time
)
1762 elapsed
= zfpm_get_elapsed_time(zfpm_g
->last_stats_clear_time
);
1764 vty_out(vty
, "\nStats were cleared %lu seconds ago\n",
1765 (unsigned long)elapsed
);
1771 static void zfpm_clear_stats(struct vty
*vty
)
1773 if (!zfpm_is_enabled()) {
1774 vty_out(vty
, "The FPM module is not enabled...\n");
1778 zfpm_stats_reset(&zfpm_g
->stats
);
1779 zfpm_stats_reset(&zfpm_g
->last_ivl_stats
);
1780 zfpm_stats_reset(&zfpm_g
->cumulative_stats
);
1782 zfpm_stop_stats_timer();
1783 zfpm_start_stats_timer();
1785 zfpm_g
->last_stats_clear_time
= monotime(NULL
);
1787 vty_out(vty
, "Cleared FPM stats\n");
1791 * show_zebra_fpm_stats
1793 DEFUN (show_zebra_fpm_stats
,
1794 show_zebra_fpm_stats_cmd
,
1795 "show zebra fpm stats",
1798 "Forwarding Path Manager information\n"
1801 zfpm_show_stats(vty
);
1806 * clear_zebra_fpm_stats
1808 DEFUN (clear_zebra_fpm_stats
,
1809 clear_zebra_fpm_stats_cmd
,
1810 "clear zebra fpm stats",
1813 "Clear Forwarding Path Manager information\n"
1816 zfpm_clear_stats(vty
);
1821 * update fpm connection information
1823 DEFUN ( fpm_remote_ip
,
1825 "fpm connection ip A.B.C.D port (1-65535)",
1826 "fpm connection remote ip and port\n"
1827 "Remote fpm server ip A.B.C.D\n"
1831 in_addr_t fpm_server
;
1834 fpm_server
= inet_addr(argv
[3]->arg
);
1835 if (fpm_server
== INADDR_NONE
)
1836 return CMD_ERR_INCOMPLETE
;
1838 port_no
= atoi(argv
[5]->arg
);
1839 if (port_no
< TCP_MIN_PORT
|| port_no
> TCP_MAX_PORT
)
1840 return CMD_ERR_INCOMPLETE
;
1842 zfpm_g
->fpm_server
= fpm_server
;
1843 zfpm_g
->fpm_port
= port_no
;
1849 DEFUN ( no_fpm_remote_ip
,
1850 no_fpm_remote_ip_cmd
,
1851 "no fpm connection ip A.B.C.D port (1-65535)",
1852 "fpm connection remote ip and port\n"
1854 "Remote fpm server ip A.B.C.D\n"
1857 if (zfpm_g
->fpm_server
!= inet_addr(argv
[4]->arg
)
1858 || zfpm_g
->fpm_port
!= atoi(argv
[6]->arg
))
1859 return CMD_ERR_NO_MATCH
;
1861 zfpm_g
->fpm_server
= FPM_DEFAULT_IP
;
1862 zfpm_g
->fpm_port
= FPM_DEFAULT_PORT
;
1868 * zfpm_init_message_format
1870 static inline void zfpm_init_message_format(const char *format
)
1872 int have_netlink
, have_protobuf
;
1880 #ifdef HAVE_PROTOBUF
1886 zfpm_g
->message_format
= ZFPM_MSG_FORMAT_NONE
;
1890 zfpm_g
->message_format
= ZFPM_MSG_FORMAT_NETLINK
;
1891 } else if (have_protobuf
) {
1892 zfpm_g
->message_format
= ZFPM_MSG_FORMAT_PROTOBUF
;
1897 if (!strcmp("netlink", format
)) {
1898 if (!have_netlink
) {
1899 flog_err(EC_ZEBRA_NETLINK_NOT_AVAILABLE
,
1900 "FPM netlink message format is not available");
1903 zfpm_g
->message_format
= ZFPM_MSG_FORMAT_NETLINK
;
1907 if (!strcmp("protobuf", format
)) {
1908 if (!have_protobuf
) {
1910 EC_ZEBRA_PROTOBUF_NOT_AVAILABLE
,
1911 "FPM protobuf message format is not available");
1914 flog_warn(EC_ZEBRA_PROTOBUF_NOT_AVAILABLE
,
1915 "FPM protobuf message format is deprecated and scheduled to be removed. Please convert to using netlink format or contact dev@lists.frrouting.org with your use case.");
1916 zfpm_g
->message_format
= ZFPM_MSG_FORMAT_PROTOBUF
;
1920 flog_warn(EC_ZEBRA_FPM_FORMAT_UNKNOWN
, "Unknown fpm format '%s'",
1925 * fpm_remote_srv_write
1927 * Module to write remote fpm connection
1929 * Returns ZERO on success.
1932 static int fpm_remote_srv_write(struct vty
*vty
)
1936 in
.s_addr
= zfpm_g
->fpm_server
;
1938 if ((zfpm_g
->fpm_server
!= FPM_DEFAULT_IP
1939 && zfpm_g
->fpm_server
!= INADDR_ANY
)
1940 || (zfpm_g
->fpm_port
!= FPM_DEFAULT_PORT
&& zfpm_g
->fpm_port
!= 0))
1941 vty_out(vty
, "fpm connection ip %pI4 port %d\n", &in
,
1948 static int fpm_remote_srv_write(struct vty
*vty
);
1950 static struct cmd_node zebra_node
= {
1953 .parent_node
= CONFIG_NODE
,
1955 .config_write
= fpm_remote_srv_write
,
1962 * One-time initialization of the Zebra FPM module.
1964 * @param[in] port port at which FPM is running.
1965 * @param[in] enable true if the zebra FPM module should be enabled
1966 * @param[in] format to use to talk to the FPM. Can be 'netink' or 'protobuf'.
1968 * Returns true on success.
1970 static int zfpm_init(struct thread_master
*master
)
1974 const char *format
= THIS_MODULE
->load_args
;
1976 memset(zfpm_g
, 0, sizeof(*zfpm_g
));
1977 zfpm_g
->master
= master
;
1978 TAILQ_INIT(&zfpm_g
->dest_q
);
1979 TAILQ_INIT(&zfpm_g
->mac_q
);
1981 /* Create hash table for fpm_mac_info_t enties */
1982 zfpm_g
->fpm_mac_info_table
= hash_create(zfpm_mac_info_hash_keymake
,
1984 "FPM MAC info hash table");
1987 zfpm_g
->state
= ZFPM_STATE_IDLE
;
1989 zfpm_stats_init(&zfpm_g
->stats
);
1990 zfpm_stats_init(&zfpm_g
->last_ivl_stats
);
1991 zfpm_stats_init(&zfpm_g
->cumulative_stats
);
1993 install_node(&zebra_node
);
1994 install_element(ENABLE_NODE
, &show_zebra_fpm_stats_cmd
);
1995 install_element(ENABLE_NODE
, &clear_zebra_fpm_stats_cmd
);
1996 install_element(CONFIG_NODE
, &fpm_remote_ip_cmd
);
1997 install_element(CONFIG_NODE
, &no_fpm_remote_ip_cmd
);
1999 zfpm_init_message_format(format
);
2002 * Disable FPM interface if no suitable format is available.
2004 if (zfpm_g
->message_format
== ZFPM_MSG_FORMAT_NONE
)
2007 zfpm_g
->enabled
= enable
;
2009 if (!zfpm_g
->fpm_server
)
2010 zfpm_g
->fpm_server
= FPM_DEFAULT_IP
;
2013 port
= FPM_DEFAULT_PORT
;
2015 zfpm_g
->fpm_port
= port
;
2017 zfpm_g
->obuf
= stream_new(ZFPM_OBUF_SIZE
);
2018 zfpm_g
->ibuf
= stream_new(ZFPM_IBUF_SIZE
);
2020 zfpm_start_stats_timer();
2021 zfpm_start_connect_timer("initialized");
2025 static int zfpm_fini(void)
2031 zfpm_stop_stats_timer();
2033 hook_unregister(rib_update
, zfpm_trigger_update
);
2037 static int zebra_fpm_module_init(void)
2039 hook_register(rib_update
, zfpm_trigger_update
);
2040 hook_register(zebra_rmac_update
, zfpm_trigger_rmac_update
);
2041 hook_register(frr_late_init
, zfpm_init
);
2042 hook_register(frr_early_fini
, zfpm_fini
);
2046 FRR_MODULE_SETUP(.name
= "zebra_fpm", .version
= FRR_VERSION
,
2047 .description
= "zebra FPM (Forwarding Plane Manager) module",
2048 .init
= zebra_fpm_module_init
,