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"
40 #include "zebra/zebra_memory.h"
43 #include "zebra_fpm_private.h"
44 #include "zebra/zebra_router.h"
45 #include "zebra_vxlan_private.h"
47 DEFINE_MTYPE_STATIC(ZEBRA
, FPM_MAC_INFO
, "FPM_MAC_INFO");
50 * Interval at which we attempt to connect to the FPM.
52 #define ZFPM_CONNECT_RETRY_IVL 5
55 * Sizes of outgoing and incoming stream buffers for writing/reading
58 #define ZFPM_OBUF_SIZE (2 * FPM_MAX_MSG_LEN)
59 #define ZFPM_IBUF_SIZE (FPM_MAX_MSG_LEN)
62 * The maximum number of times the FPM socket write callback can call
63 * 'write' before it yields.
65 #define ZFPM_MAX_WRITES_PER_RUN 10
68 * Interval over which we collect statistics.
70 #define ZFPM_STATS_IVL_SECS 10
71 #define FPM_MAX_MAC_MSG_LEN 512
73 static void zfpm_iterate_rmac_table(struct hash_backet
*backet
, void *args
);
76 * Structure that holds state for iterating over all route_node
77 * structures that are candidates for being communicated to the FPM.
79 typedef struct zfpm_rnodes_iter_t_
{
80 rib_tables_iter_t tables_iter
;
81 route_table_iter_t iter
;
87 typedef struct zfpm_stats_t_
{
88 unsigned long connect_calls
;
89 unsigned long connect_no_sock
;
91 unsigned long read_cb_calls
;
93 unsigned long write_cb_calls
;
94 unsigned long write_calls
;
95 unsigned long partial_writes
;
96 unsigned long max_writes_hit
;
97 unsigned long t_write_yields
;
99 unsigned long nop_deletes_skipped
;
100 unsigned long route_adds
;
101 unsigned long route_dels
;
103 unsigned long updates_triggered
;
104 unsigned long redundant_triggers
;
106 unsigned long dests_del_after_update
;
108 unsigned long t_conn_down_starts
;
109 unsigned long t_conn_down_dests_processed
;
110 unsigned long t_conn_down_yields
;
111 unsigned long t_conn_down_finishes
;
113 unsigned long t_conn_up_starts
;
114 unsigned long t_conn_up_dests_processed
;
115 unsigned long t_conn_up_yields
;
116 unsigned long t_conn_up_aborts
;
117 unsigned long t_conn_up_finishes
;
122 * States for the FPM state machine.
127 * In this state we are not yet ready to connect to the FPM. This
128 * can happen when this module is disabled, or if we're cleaning up
129 * after a connection has gone down.
134 * Ready to talk to the FPM and periodically trying to connect to
140 * In the middle of bringing up a TCP connection. Specifically,
141 * waiting for a connect() call to complete asynchronously.
143 ZFPM_STATE_CONNECTING
,
146 * TCP connection to the FPM is up.
148 ZFPM_STATE_ESTABLISHED
153 * Message format to be used to communicate with the FPM.
156 ZFPM_MSG_FORMAT_NONE
,
157 ZFPM_MSG_FORMAT_NETLINK
,
158 ZFPM_MSG_FORMAT_PROTOBUF
,
163 typedef struct zfpm_glob_t_
{
166 * True if the FPM module has been enabled.
171 * Message format to be used to communicate with the fpm.
173 zfpm_msg_format_e message_format
;
175 struct thread_master
*master
;
179 in_addr_t fpm_server
;
181 * Port on which the FPM is running.
186 * List of rib_dest_t structures to be processed
188 TAILQ_HEAD(zfpm_dest_q
, rib_dest_t_
) dest_q
;
191 * List of fpm_mac_info structures to be processed
193 TAILQ_HEAD(zfpm_mac_q
, fpm_mac_info_t
) mac_q
;
196 * Hash table of fpm_mac_info_t entries
198 * While adding fpm_mac_info_t for a MAC to the mac_q,
199 * it is possible that another fpm_mac_info_t node for the this MAC
200 * is already present in the queue.
201 * This is possible in the case of consecutive add->delete operations.
202 * To avoid such duplicate insertions in the mac_q,
203 * define a hash table for fpm_mac_info_t which can be looked up
204 * to see if an fpm_mac_info_t node for a MAC is already present
207 struct hash
*fpm_mac_info_table
;
210 * Stream socket to the FPM.
215 * Buffers for messages to/from the FPM.
223 struct thread
*t_connect
;
224 struct thread
*t_write
;
225 struct thread
*t_read
;
228 * Thread to clean up after the TCP connection to the FPM goes down
229 * and the state that belongs to it.
231 struct thread
*t_conn_down
;
234 zfpm_rnodes_iter_t iter
;
238 * Thread to take actions once the TCP conn to the FPM comes up, and
239 * the state that belongs to it.
241 struct thread
*t_conn_up
;
244 zfpm_rnodes_iter_t iter
;
247 unsigned long connect_calls
;
248 time_t last_connect_call_time
;
251 * Stats from the start of the current statistics interval up to
252 * now. These are the counters we typically update in the code.
257 * Statistics that were gathered in the last collection interval.
259 zfpm_stats_t last_ivl_stats
;
262 * Cumulative stats from the last clear to the start of the current
263 * statistics interval.
265 zfpm_stats_t cumulative_stats
;
268 * Stats interval timer.
270 struct thread
*t_stats
;
273 * If non-zero, the last time when statistics were cleared.
275 time_t last_stats_clear_time
;
279 static zfpm_glob_t zfpm_glob_space
;
280 static zfpm_glob_t
*zfpm_g
= &zfpm_glob_space
;
282 static int zfpm_trigger_update(struct route_node
*rn
, const char *reason
);
284 static int zfpm_read_cb(struct thread
*thread
);
285 static int zfpm_write_cb(struct thread
*thread
);
287 static void zfpm_set_state(zfpm_state_t state
, const char *reason
);
288 static void zfpm_start_connect_timer(const char *reason
);
289 static void zfpm_start_stats_timer(void);
290 static void zfpm_mac_info_del(struct fpm_mac_info_t
*fpm_mac
);
293 * zfpm_thread_should_yield
295 static inline int zfpm_thread_should_yield(struct thread
*t
)
297 return thread_should_yield(t
);
303 static const char *zfpm_state_to_str(zfpm_state_t state
)
307 case ZFPM_STATE_IDLE
:
310 case ZFPM_STATE_ACTIVE
:
313 case ZFPM_STATE_CONNECTING
:
316 case ZFPM_STATE_ESTABLISHED
:
317 return "established";
325 * zfpm_get_elapsed_time
327 * Returns the time elapsed (in seconds) since the given time.
329 static time_t zfpm_get_elapsed_time(time_t reference
)
333 now
= monotime(NULL
);
335 if (now
< reference
) {
340 return now
- reference
;
344 * zfpm_rnodes_iter_init
346 static inline void zfpm_rnodes_iter_init(zfpm_rnodes_iter_t
*iter
)
348 memset(iter
, 0, sizeof(*iter
));
349 rib_tables_iter_init(&iter
->tables_iter
);
352 * This is a hack, but it makes implementing 'next' easier by
353 * ensuring that route_table_iter_next() will return NULL the first
356 route_table_iter_init(&iter
->iter
, NULL
);
357 route_table_iter_cleanup(&iter
->iter
);
361 * zfpm_rnodes_iter_next
363 static inline struct route_node
*zfpm_rnodes_iter_next(zfpm_rnodes_iter_t
*iter
)
365 struct route_node
*rn
;
366 struct route_table
*table
;
369 rn
= route_table_iter_next(&iter
->iter
);
374 * We've made our way through this table, go to the next one.
376 route_table_iter_cleanup(&iter
->iter
);
378 table
= rib_tables_iter_next(&iter
->tables_iter
);
383 route_table_iter_init(&iter
->iter
, table
);
390 * zfpm_rnodes_iter_pause
392 static inline void zfpm_rnodes_iter_pause(zfpm_rnodes_iter_t
*iter
)
394 route_table_iter_pause(&iter
->iter
);
398 * zfpm_rnodes_iter_cleanup
400 static inline void zfpm_rnodes_iter_cleanup(zfpm_rnodes_iter_t
*iter
)
402 route_table_iter_cleanup(&iter
->iter
);
403 rib_tables_iter_cleanup(&iter
->tables_iter
);
409 * Initialize a statistics block.
411 static inline void zfpm_stats_init(zfpm_stats_t
*stats
)
413 memset(stats
, 0, sizeof(*stats
));
419 static inline void zfpm_stats_reset(zfpm_stats_t
*stats
)
421 zfpm_stats_init(stats
);
427 static inline void zfpm_stats_copy(const zfpm_stats_t
*src
, zfpm_stats_t
*dest
)
429 memcpy(dest
, src
, sizeof(*dest
));
435 * Total up the statistics in two stats structures ('s1 and 's2') and
436 * return the result in the third argument, 'result'. Note that the
437 * pointer 'result' may be the same as 's1' or 's2'.
439 * For simplicity, the implementation below assumes that the stats
440 * structure is composed entirely of counters. This can easily be
441 * changed when necessary.
443 static void zfpm_stats_compose(const zfpm_stats_t
*s1
, const zfpm_stats_t
*s2
,
444 zfpm_stats_t
*result
)
446 const unsigned long *p1
, *p2
;
447 unsigned long *result_p
;
450 p1
= (const unsigned long *)s1
;
451 p2
= (const unsigned long *)s2
;
452 result_p
= (unsigned long *)result
;
454 num_counters
= (sizeof(zfpm_stats_t
) / sizeof(unsigned long));
456 for (i
= 0; i
< num_counters
; i
++) {
457 result_p
[i
] = p1
[i
] + p2
[i
];
464 static inline void zfpm_read_on(void)
466 assert(!zfpm_g
->t_read
);
467 assert(zfpm_g
->sock
>= 0);
469 thread_add_read(zfpm_g
->master
, zfpm_read_cb
, 0, zfpm_g
->sock
,
476 static inline void zfpm_write_on(void)
478 assert(!zfpm_g
->t_write
);
479 assert(zfpm_g
->sock
>= 0);
481 thread_add_write(zfpm_g
->master
, zfpm_write_cb
, 0, zfpm_g
->sock
,
488 static inline void zfpm_read_off(void)
490 THREAD_READ_OFF(zfpm_g
->t_read
);
496 static inline void zfpm_write_off(void)
498 THREAD_WRITE_OFF(zfpm_g
->t_write
);
502 * zfpm_conn_up_thread_cb
504 * Callback for actions to be taken when the connection to the FPM
507 static int zfpm_conn_up_thread_cb(struct thread
*thread
)
509 struct route_node
*rnode
;
510 zfpm_rnodes_iter_t
*iter
;
513 zfpm_g
->t_conn_up
= NULL
;
515 iter
= &zfpm_g
->t_conn_up_state
.iter
;
517 if (zfpm_g
->state
!= ZFPM_STATE_ESTABLISHED
) {
519 "Connection not up anymore, conn_up thread aborting");
520 zfpm_g
->stats
.t_conn_up_aborts
++;
524 /* Enqueue FPM updates for all the RMAC entries */
525 hash_iterate(zrouter
.l3vni_table
, zfpm_iterate_rmac_table
, NULL
);
527 while ((rnode
= zfpm_rnodes_iter_next(iter
))) {
528 dest
= rib_dest_from_rnode(rnode
);
531 zfpm_g
->stats
.t_conn_up_dests_processed
++;
532 zfpm_trigger_update(rnode
, NULL
);
538 if (!zfpm_thread_should_yield(thread
))
541 zfpm_g
->stats
.t_conn_up_yields
++;
542 zfpm_rnodes_iter_pause(iter
);
543 zfpm_g
->t_conn_up
= NULL
;
544 thread_add_timer_msec(zfpm_g
->master
, zfpm_conn_up_thread_cb
,
545 NULL
, 0, &zfpm_g
->t_conn_up
);
549 zfpm_g
->stats
.t_conn_up_finishes
++;
552 zfpm_rnodes_iter_cleanup(iter
);
559 * Called when the connection to the FPM comes up.
561 static void zfpm_connection_up(const char *detail
)
563 assert(zfpm_g
->sock
>= 0);
566 zfpm_set_state(ZFPM_STATE_ESTABLISHED
, detail
);
569 * Start thread to push existing routes to the FPM.
571 assert(!zfpm_g
->t_conn_up
);
573 zfpm_rnodes_iter_init(&zfpm_g
->t_conn_up_state
.iter
);
575 zfpm_debug("Starting conn_up thread");
576 zfpm_g
->t_conn_up
= NULL
;
577 thread_add_timer_msec(zfpm_g
->master
, zfpm_conn_up_thread_cb
, NULL
, 0,
579 zfpm_g
->stats
.t_conn_up_starts
++;
585 * Check if an asynchronous connect() to the FPM is complete.
587 static void zfpm_connect_check(void)
596 slen
= sizeof(status
);
597 ret
= getsockopt(zfpm_g
->sock
, SOL_SOCKET
, SO_ERROR
, (void *)&status
,
600 if (ret
>= 0 && status
== 0) {
601 zfpm_connection_up("async connect complete");
606 * getsockopt() failed or indicated an error on the socket.
611 zfpm_start_connect_timer("getsockopt() after async connect failed");
616 * zfpm_conn_down_thread_cb
618 * Callback that is invoked to clean up state after the TCP connection
619 * to the FPM goes down.
621 static int zfpm_conn_down_thread_cb(struct thread
*thread
)
623 struct route_node
*rnode
;
624 zfpm_rnodes_iter_t
*iter
;
626 struct fpm_mac_info_t
*mac
= NULL
;
628 assert(zfpm_g
->state
== ZFPM_STATE_IDLE
);
631 * Delink and free all fpm_mac_info_t nodes
632 * in the mac_q and fpm_mac_info_hash
634 while ((mac
= TAILQ_FIRST(&zfpm_g
->mac_q
)) != NULL
)
635 zfpm_mac_info_del(mac
);
637 zfpm_g
->t_conn_down
= NULL
;
639 iter
= &zfpm_g
->t_conn_down_state
.iter
;
641 while ((rnode
= zfpm_rnodes_iter_next(iter
))) {
642 dest
= rib_dest_from_rnode(rnode
);
645 if (CHECK_FLAG(dest
->flags
, RIB_DEST_UPDATE_FPM
)) {
646 TAILQ_REMOVE(&zfpm_g
->dest_q
, dest
,
650 UNSET_FLAG(dest
->flags
, RIB_DEST_UPDATE_FPM
);
651 UNSET_FLAG(dest
->flags
, RIB_DEST_SENT_TO_FPM
);
653 zfpm_g
->stats
.t_conn_down_dests_processed
++;
656 * Check if the dest should be deleted.
664 if (!zfpm_thread_should_yield(thread
))
667 zfpm_g
->stats
.t_conn_down_yields
++;
668 zfpm_rnodes_iter_pause(iter
);
669 zfpm_g
->t_conn_down
= NULL
;
670 thread_add_timer_msec(zfpm_g
->master
, zfpm_conn_down_thread_cb
,
671 NULL
, 0, &zfpm_g
->t_conn_down
);
675 zfpm_g
->stats
.t_conn_down_finishes
++;
676 zfpm_rnodes_iter_cleanup(iter
);
679 * Start the process of connecting to the FPM again.
681 zfpm_start_connect_timer("cleanup complete");
686 * zfpm_connection_down
688 * Called when the connection to the FPM has gone down.
690 static void zfpm_connection_down(const char *detail
)
695 assert(zfpm_g
->state
== ZFPM_STATE_ESTABLISHED
);
697 zlog_info("connection to the FPM has gone down: %s", detail
);
702 stream_reset(zfpm_g
->ibuf
);
703 stream_reset(zfpm_g
->obuf
);
705 if (zfpm_g
->sock
>= 0) {
711 * Start thread to clean up state after the connection goes down.
713 assert(!zfpm_g
->t_conn_down
);
714 zfpm_rnodes_iter_init(&zfpm_g
->t_conn_down_state
.iter
);
715 zfpm_g
->t_conn_down
= NULL
;
716 thread_add_timer_msec(zfpm_g
->master
, zfpm_conn_down_thread_cb
, NULL
, 0,
717 &zfpm_g
->t_conn_down
);
718 zfpm_g
->stats
.t_conn_down_starts
++;
720 zfpm_set_state(ZFPM_STATE_IDLE
, detail
);
726 static int zfpm_read_cb(struct thread
*thread
)
733 zfpm_g
->stats
.read_cb_calls
++;
734 zfpm_g
->t_read
= NULL
;
737 * Check if async connect is now done.
739 if (zfpm_g
->state
== ZFPM_STATE_CONNECTING
) {
740 zfpm_connect_check();
744 assert(zfpm_g
->state
== ZFPM_STATE_ESTABLISHED
);
745 assert(zfpm_g
->sock
>= 0);
749 already
= stream_get_endp(ibuf
);
750 if (already
< FPM_MSG_HDR_LEN
) {
753 nbyte
= stream_read_try(ibuf
, zfpm_g
->sock
,
754 FPM_MSG_HDR_LEN
- already
);
755 if (nbyte
== 0 || nbyte
== -1) {
759 sprintf(buffer
, "closed socket in read(%d): %s",
760 errno
, safe_strerror(errno
));
761 zfpm_connection_down(buffer
);
763 zfpm_connection_down("closed socket in read");
767 if (nbyte
!= (ssize_t
)(FPM_MSG_HDR_LEN
- already
))
770 already
= FPM_MSG_HDR_LEN
;
773 stream_set_getp(ibuf
, 0);
775 hdr
= (fpm_msg_hdr_t
*)stream_pnt(ibuf
);
777 if (!fpm_msg_hdr_ok(hdr
)) {
778 zfpm_connection_down("invalid message header");
782 msg_len
= fpm_msg_len(hdr
);
785 * Read out the rest of the packet.
787 if (already
< msg_len
) {
790 nbyte
= stream_read_try(ibuf
, zfpm_g
->sock
, msg_len
- already
);
792 if (nbyte
== 0 || nbyte
== -1) {
796 sprintf(buffer
, "failed to read message(%d) %s",
797 errno
, safe_strerror(errno
));
798 zfpm_connection_down(buffer
);
800 zfpm_connection_down("failed to read message");
804 if (nbyte
!= (ssize_t
)(msg_len
- already
))
809 * Just throw it away for now.
818 static bool zfpm_updates_pending(void)
820 if (!(TAILQ_EMPTY(&zfpm_g
->dest_q
)) || !(TAILQ_EMPTY(&zfpm_g
->mac_q
)))
827 * zfpm_writes_pending
829 * Returns true if we may have something to write to the FPM.
831 static int zfpm_writes_pending(void)
835 * Check if there is any data in the outbound buffer that has not
836 * been written to the socket yet.
838 if (stream_get_endp(zfpm_g
->obuf
) - stream_get_getp(zfpm_g
->obuf
))
842 * Check if there are any updates scheduled on the outbound queues.
844 if (zfpm_updates_pending())
853 * Encode a message to the FPM with information about the given route.
855 * Returns the number of bytes written to the buffer. 0 or a negative
856 * value indicates an error.
858 static inline int zfpm_encode_route(rib_dest_t
*dest
, struct route_entry
*re
,
859 char *in_buf
, size_t in_buf_len
,
860 fpm_msg_type_e
*msg_type
)
868 *msg_type
= FPM_MSG_TYPE_NONE
;
870 switch (zfpm_g
->message_format
) {
872 case ZFPM_MSG_FORMAT_PROTOBUF
:
874 len
= zfpm_protobuf_encode_route(dest
, re
, (uint8_t *)in_buf
,
876 *msg_type
= FPM_MSG_TYPE_PROTOBUF
;
880 case ZFPM_MSG_FORMAT_NETLINK
:
882 *msg_type
= FPM_MSG_TYPE_NETLINK
;
883 cmd
= re
? RTM_NEWROUTE
: RTM_DELROUTE
;
884 len
= zfpm_netlink_encode_route(cmd
, dest
, re
, in_buf
,
886 assert(fpm_msg_align(len
) == len
);
887 *msg_type
= FPM_MSG_TYPE_NETLINK
;
888 #endif /* HAVE_NETLINK */
899 * zfpm_route_for_update
901 * Returns the re that is to be sent to the FPM for a given dest.
903 struct route_entry
*zfpm_route_for_update(rib_dest_t
*dest
)
905 return dest
->selected_fib
;
909 * Define an enum for return codes for queue processing functions
911 * FPM_WRITE_STOP: This return code indicates that the write buffer is full.
912 * Stop processing all the queues and empty the buffer by writing its content
915 * FPM_GOTO_NEXT_Q: This return code indicates that either this queue is
916 * empty or we have processed enough updates from this queue.
917 * So, move on to the next queue.
924 #define FPM_QUEUE_PROCESS_LIMIT 10000
927 * zfpm_build_route_updates
929 * Process the dest_q queue and write FPM messages to the outbound buffer.
931 static int zfpm_build_route_updates(void)
935 unsigned char *buf
, *data
, *buf_end
;
939 struct route_entry
*re
;
940 int is_add
, write_msg
;
941 fpm_msg_type_e msg_type
;
944 if (TAILQ_EMPTY(&zfpm_g
->dest_q
))
945 return FPM_GOTO_NEXT_Q
;
948 q_limit
= FPM_QUEUE_PROCESS_LIMIT
;
952 * Make sure there is enough space to write another message.
954 if (STREAM_WRITEABLE(s
) < FPM_MAX_MSG_LEN
)
955 return FPM_WRITE_STOP
;
957 buf
= STREAM_DATA(s
) + stream_get_endp(s
);
958 buf_end
= buf
+ STREAM_WRITEABLE(s
);
960 dest
= TAILQ_FIRST(&zfpm_g
->dest_q
);
962 return FPM_GOTO_NEXT_Q
;
964 assert(CHECK_FLAG(dest
->flags
, RIB_DEST_UPDATE_FPM
));
966 hdr
= (fpm_msg_hdr_t
*)buf
;
967 hdr
->version
= FPM_PROTO_VERSION
;
969 data
= fpm_msg_data(hdr
);
971 re
= zfpm_route_for_update(dest
);
977 * If this is a route deletion, and we have not sent the route
979 * the FPM previously, skip it.
981 if (!is_add
&& !CHECK_FLAG(dest
->flags
, RIB_DEST_SENT_TO_FPM
)) {
983 zfpm_g
->stats
.nop_deletes_skipped
++;
987 data_len
= zfpm_encode_route(dest
, re
, (char *)data
,
988 buf_end
- data
, &msg_type
);
992 hdr
->msg_type
= msg_type
;
993 msg_len
= fpm_data_len_to_msg_len(data_len
);
994 hdr
->msg_len
= htons(msg_len
);
995 stream_forward_endp(s
, msg_len
);
998 zfpm_g
->stats
.route_adds
++;
1000 zfpm_g
->stats
.route_dels
++;
1005 * Remove the dest from the queue, and reset the flag.
1007 UNSET_FLAG(dest
->flags
, RIB_DEST_UPDATE_FPM
);
1008 TAILQ_REMOVE(&zfpm_g
->dest_q
, dest
, fpm_q_entries
);
1011 SET_FLAG(dest
->flags
, RIB_DEST_SENT_TO_FPM
);
1013 UNSET_FLAG(dest
->flags
, RIB_DEST_SENT_TO_FPM
);
1017 * Delete the destination if necessary.
1019 if (rib_gc_dest(dest
->rnode
))
1020 zfpm_g
->stats
.dests_del_after_update
++;
1025 * We have processed enough updates in this queue.
1026 * Now yield for other queues.
1028 return FPM_GOTO_NEXT_Q
;
1036 * Encode a message to FPM with information about the given MAC.
1038 * Returns the number of bytes written to the buffer.
1040 static inline int zfpm_encode_mac(struct fpm_mac_info_t
*mac
, char *in_buf
,
1041 size_t in_buf_len
, fpm_msg_type_e
*msg_type
)
1045 *msg_type
= FPM_MSG_TYPE_NONE
;
1047 switch (zfpm_g
->message_format
) {
1049 case ZFPM_MSG_FORMAT_NONE
:
1051 case ZFPM_MSG_FORMAT_NETLINK
:
1053 len
= zfpm_netlink_encode_mac(mac
, in_buf
, in_buf_len
);
1054 assert(fpm_msg_align(len
) == len
);
1055 *msg_type
= FPM_MSG_TYPE_NETLINK
;
1056 #endif /* HAVE_NETLINK */
1058 case ZFPM_MSG_FORMAT_PROTOBUF
:
1064 static int zfpm_build_mac_updates(void)
1067 struct fpm_mac_info_t
*mac
;
1068 unsigned char *buf
, *data
, *buf_end
;
1070 size_t data_len
, msg_len
;
1071 fpm_msg_type_e msg_type
;
1074 if (TAILQ_EMPTY(&zfpm_g
->mac_q
))
1075 return FPM_GOTO_NEXT_Q
;
1078 q_limit
= FPM_QUEUE_PROCESS_LIMIT
;
1081 /* Make sure there is enough space to write another message. */
1082 if (STREAM_WRITEABLE(s
) < FPM_MAX_MAC_MSG_LEN
)
1083 return FPM_WRITE_STOP
;
1085 buf
= STREAM_DATA(s
) + stream_get_endp(s
);
1086 buf_end
= buf
+ STREAM_WRITEABLE(s
);
1088 mac
= TAILQ_FIRST(&zfpm_g
->mac_q
);
1090 return FPM_GOTO_NEXT_Q
;
1092 /* Check for no-op */
1093 if (!CHECK_FLAG(mac
->fpm_flags
, ZEBRA_MAC_UPDATE_FPM
)) {
1094 zfpm_g
->stats
.nop_deletes_skipped
++;
1095 zfpm_mac_info_del(mac
);
1099 hdr
= (fpm_msg_hdr_t
*)buf
;
1100 hdr
->version
= FPM_PROTO_VERSION
;
1102 data
= fpm_msg_data(hdr
);
1103 data_len
= zfpm_encode_mac(mac
, (char *)data
, buf_end
- data
,
1107 hdr
->msg_type
= msg_type
;
1108 msg_len
= fpm_data_len_to_msg_len(data_len
);
1109 hdr
->msg_len
= htons(msg_len
);
1110 stream_forward_endp(s
, msg_len
);
1112 /* Remove the MAC from the queue, and delete it. */
1113 zfpm_mac_info_del(mac
);
1118 * We have processed enough updates in this queue.
1119 * Now yield for other queues.
1121 return FPM_GOTO_NEXT_Q
;
1127 * zfpm_build_updates
1129 * Process the outgoing queues and write messages to the outbound
1132 static void zfpm_build_updates(void)
1137 assert(stream_empty(s
));
1141 * Stop processing the queues if zfpm_g->obuf is full
1142 * or we do not have more updates to process
1144 if (zfpm_build_mac_updates() == FPM_WRITE_STOP
)
1146 if (zfpm_build_route_updates() == FPM_WRITE_STOP
)
1148 } while (zfpm_updates_pending());
1154 static int zfpm_write_cb(struct thread
*thread
)
1159 zfpm_g
->stats
.write_cb_calls
++;
1160 zfpm_g
->t_write
= NULL
;
1163 * Check if async connect is now done.
1165 if (zfpm_g
->state
== ZFPM_STATE_CONNECTING
) {
1166 zfpm_connect_check();
1170 assert(zfpm_g
->state
== ZFPM_STATE_ESTABLISHED
);
1171 assert(zfpm_g
->sock
>= 0);
1176 int bytes_to_write
, bytes_written
;
1181 * If the stream is empty, try fill it up with data.
1183 if (stream_empty(s
)) {
1184 zfpm_build_updates();
1187 bytes_to_write
= stream_get_endp(s
) - stream_get_getp(s
);
1188 if (!bytes_to_write
)
1192 write(zfpm_g
->sock
, stream_pnt(s
), bytes_to_write
);
1193 zfpm_g
->stats
.write_calls
++;
1196 if (bytes_written
< 0) {
1197 if (ERRNO_IO_RETRY(errno
))
1200 zfpm_connection_down("failed to write to socket");
1204 if (bytes_written
!= bytes_to_write
) {
1209 stream_forward_getp(s
, bytes_written
);
1210 zfpm_g
->stats
.partial_writes
++;
1215 * We've written out the entire contents of the stream.
1219 if (num_writes
>= ZFPM_MAX_WRITES_PER_RUN
) {
1220 zfpm_g
->stats
.max_writes_hit
++;
1224 if (zfpm_thread_should_yield(thread
)) {
1225 zfpm_g
->stats
.t_write_yields
++;
1230 if (zfpm_writes_pending())
1239 static int zfpm_connect_cb(struct thread
*t
)
1242 struct sockaddr_in serv
;
1244 zfpm_g
->t_connect
= NULL
;
1245 assert(zfpm_g
->state
== ZFPM_STATE_ACTIVE
);
1247 sock
= socket(AF_INET
, SOCK_STREAM
, 0);
1249 zlog_err("Failed to create socket for connect(): %s",
1251 zfpm_g
->stats
.connect_no_sock
++;
1255 set_nonblocking(sock
);
1257 /* Make server socket. */
1258 memset(&serv
, 0, sizeof(serv
));
1259 serv
.sin_family
= AF_INET
;
1260 serv
.sin_port
= htons(zfpm_g
->fpm_port
);
1261 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
1262 serv
.sin_len
= sizeof(struct sockaddr_in
);
1263 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
1264 if (!zfpm_g
->fpm_server
)
1265 serv
.sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
1267 serv
.sin_addr
.s_addr
= (zfpm_g
->fpm_server
);
1270 * Connect to the FPM.
1272 zfpm_g
->connect_calls
++;
1273 zfpm_g
->stats
.connect_calls
++;
1274 zfpm_g
->last_connect_call_time
= monotime(NULL
);
1276 ret
= connect(sock
, (struct sockaddr
*)&serv
, sizeof(serv
));
1278 zfpm_g
->sock
= sock
;
1279 zfpm_connection_up("connect succeeded");
1283 if (errno
== EINPROGRESS
) {
1284 zfpm_g
->sock
= sock
;
1287 zfpm_set_state(ZFPM_STATE_CONNECTING
,
1288 "async connect in progress");
1292 zlog_info("can't connect to FPM %d: %s", sock
, safe_strerror(errno
));
1296 * Restart timer for retrying connection.
1298 zfpm_start_connect_timer("connect() failed");
1305 * Move state machine into the given state.
1307 static void zfpm_set_state(zfpm_state_t state
, const char *reason
)
1309 zfpm_state_t cur_state
= zfpm_g
->state
;
1314 if (state
== cur_state
)
1317 zfpm_debug("beginning state transition %s -> %s. Reason: %s",
1318 zfpm_state_to_str(cur_state
), zfpm_state_to_str(state
),
1323 case ZFPM_STATE_IDLE
:
1324 assert(cur_state
== ZFPM_STATE_ESTABLISHED
);
1327 case ZFPM_STATE_ACTIVE
:
1328 assert(cur_state
== ZFPM_STATE_IDLE
1329 || cur_state
== ZFPM_STATE_CONNECTING
);
1330 assert(zfpm_g
->t_connect
);
1333 case ZFPM_STATE_CONNECTING
:
1334 assert(zfpm_g
->sock
);
1335 assert(cur_state
== ZFPM_STATE_ACTIVE
);
1336 assert(zfpm_g
->t_read
);
1337 assert(zfpm_g
->t_write
);
1340 case ZFPM_STATE_ESTABLISHED
:
1341 assert(cur_state
== ZFPM_STATE_ACTIVE
1342 || cur_state
== ZFPM_STATE_CONNECTING
);
1343 assert(zfpm_g
->sock
);
1344 assert(zfpm_g
->t_read
);
1345 assert(zfpm_g
->t_write
);
1349 zfpm_g
->state
= state
;
1353 * zfpm_calc_connect_delay
1355 * Returns the number of seconds after which we should attempt to
1356 * reconnect to the FPM.
1358 static long zfpm_calc_connect_delay(void)
1363 * Return 0 if this is our first attempt to connect.
1365 if (zfpm_g
->connect_calls
== 0) {
1369 elapsed
= zfpm_get_elapsed_time(zfpm_g
->last_connect_call_time
);
1371 if (elapsed
> ZFPM_CONNECT_RETRY_IVL
) {
1375 return ZFPM_CONNECT_RETRY_IVL
- elapsed
;
1379 * zfpm_start_connect_timer
1381 static void zfpm_start_connect_timer(const char *reason
)
1385 assert(!zfpm_g
->t_connect
);
1386 assert(zfpm_g
->sock
< 0);
1388 assert(zfpm_g
->state
== ZFPM_STATE_IDLE
1389 || zfpm_g
->state
== ZFPM_STATE_ACTIVE
1390 || zfpm_g
->state
== ZFPM_STATE_CONNECTING
);
1392 delay_secs
= zfpm_calc_connect_delay();
1393 zfpm_debug("scheduling connect in %ld seconds", delay_secs
);
1395 thread_add_timer(zfpm_g
->master
, zfpm_connect_cb
, 0, delay_secs
,
1396 &zfpm_g
->t_connect
);
1397 zfpm_set_state(ZFPM_STATE_ACTIVE
, reason
);
1403 * Returns true if the zebra FPM module has been enabled.
1405 static inline int zfpm_is_enabled(void)
1407 return zfpm_g
->enabled
;
1413 * Returns true if the connection to the FPM is up.
1415 static inline int zfpm_conn_is_up(void)
1417 if (zfpm_g
->state
!= ZFPM_STATE_ESTABLISHED
)
1420 assert(zfpm_g
->sock
>= 0);
1426 * zfpm_trigger_update
1428 * The zebra code invokes this function to indicate that we should
1429 * send an update to the FPM about the given route_node.
1431 static int zfpm_trigger_update(struct route_node
*rn
, const char *reason
)
1434 char buf
[PREFIX_STRLEN
];
1437 * Ignore if the connection is down. We will update the FPM about
1438 * all destinations once the connection comes up.
1440 if (!zfpm_conn_is_up())
1443 dest
= rib_dest_from_rnode(rn
);
1445 if (CHECK_FLAG(dest
->flags
, RIB_DEST_UPDATE_FPM
)) {
1446 zfpm_g
->stats
.redundant_triggers
++;
1451 zfpm_debug("%s triggering update to FPM - Reason: %s",
1452 prefix2str(&rn
->p
, buf
, sizeof(buf
)), reason
);
1455 SET_FLAG(dest
->flags
, RIB_DEST_UPDATE_FPM
);
1456 TAILQ_INSERT_TAIL(&zfpm_g
->dest_q
, dest
, fpm_q_entries
);
1457 zfpm_g
->stats
.updates_triggered
++;
1460 * Make sure that writes are enabled.
1462 if (zfpm_g
->t_write
)
1470 * Generate Key for FPM MAC info hash entry
1471 * Key is generated using MAC address and VNI id which should be sufficient
1472 * to provide uniqueness
1474 static unsigned int zfpm_mac_info_hash_keymake(const void *p
)
1476 struct fpm_mac_info_t
*fpm_mac
= (struct fpm_mac_info_t
*)p
;
1479 mac_key
= jhash(fpm_mac
->macaddr
.octet
, ETH_ALEN
, 0xa5a5a55a);
1481 return jhash_2words(mac_key
, fpm_mac
->vni
, 0);
1485 * Compare function for FPM MAC info hash lookup
1487 static bool zfpm_mac_info_cmp(const void *p1
, const void *p2
)
1489 const struct fpm_mac_info_t
*fpm_mac1
= p1
;
1490 const struct fpm_mac_info_t
*fpm_mac2
= p2
;
1492 if (memcmp(fpm_mac1
->macaddr
.octet
, fpm_mac2
->macaddr
.octet
, ETH_ALEN
)
1495 if (fpm_mac1
->r_vtep_ip
.s_addr
!= fpm_mac2
->r_vtep_ip
.s_addr
)
1497 if (fpm_mac1
->vni
!= fpm_mac2
->vni
)
1504 * Lookup FPM MAC info hash entry.
1506 static struct fpm_mac_info_t
*zfpm_mac_info_lookup(struct fpm_mac_info_t
*key
)
1508 return hash_lookup(zfpm_g
->fpm_mac_info_table
, key
);
1512 * Callback to allocate fpm_mac_info_t structure.
1514 static void *zfpm_mac_info_alloc(void *p
)
1516 const struct fpm_mac_info_t
*key
= p
;
1517 struct fpm_mac_info_t
*fpm_mac
;
1519 fpm_mac
= XCALLOC(MTYPE_FPM_MAC_INFO
, sizeof(struct fpm_mac_info_t
));
1521 memcpy(&fpm_mac
->macaddr
, &key
->macaddr
, ETH_ALEN
);
1522 memcpy(&fpm_mac
->r_vtep_ip
, &key
->r_vtep_ip
, sizeof(struct in_addr
));
1523 fpm_mac
->vni
= key
->vni
;
1525 return (void *)fpm_mac
;
1529 * Delink and free fpm_mac_info_t.
1531 static void zfpm_mac_info_del(struct fpm_mac_info_t
*fpm_mac
)
1533 hash_release(zfpm_g
->fpm_mac_info_table
, fpm_mac
);
1534 TAILQ_REMOVE(&zfpm_g
->mac_q
, fpm_mac
, fpm_mac_q_entries
);
1535 XFREE(MTYPE_FPM_MAC_INFO
, fpm_mac
);
1539 * zfpm_trigger_rmac_update
1541 * Zebra code invokes this function to indicate that we should
1542 * send an update to FPM for given MAC entry.
1544 * This function checks if we already have enqueued an update for this RMAC,
1545 * If yes, update the same fpm_mac_info_t. Else, create and enqueue an update.
1547 static int zfpm_trigger_rmac_update(zebra_mac_t
*rmac
, zebra_l3vni_t
*zl3vni
,
1548 bool delete, const char *reason
)
1550 char buf
[ETHER_ADDR_STRLEN
];
1551 struct fpm_mac_info_t
*fpm_mac
, key
;
1552 struct interface
*vxlan_if
, *svi_if
;
1555 * Ignore if the connection is down. We will update the FPM about
1556 * all destinations once the connection comes up.
1558 if (!zfpm_conn_is_up())
1562 zfpm_debug("triggering update to FPM - Reason: %s - %s",
1564 prefix_mac2str(&rmac
->macaddr
, buf
, sizeof(buf
)));
1567 vxlan_if
= zl3vni_map_to_vxlan_if(zl3vni
);
1568 svi_if
= zl3vni_map_to_svi_if(zl3vni
);
1570 memset(&key
, 0, sizeof(struct fpm_mac_info_t
));
1572 memcpy(&key
.macaddr
, &rmac
->macaddr
, ETH_ALEN
);
1573 key
.r_vtep_ip
.s_addr
= rmac
->fwd_info
.r_vtep_ip
.s_addr
;
1574 key
.vni
= zl3vni
->vni
;
1576 /* Check if this MAC is already present in the queue. */
1577 fpm_mac
= zfpm_mac_info_lookup(&key
);
1580 if (!!CHECK_FLAG(fpm_mac
->fpm_flags
, ZEBRA_MAC_DELETE_FPM
)
1583 * MAC is already present in the queue
1584 * with the same op as this one. Do nothing
1586 zfpm_g
->stats
.redundant_triggers
++;
1591 * A new op for an already existing fpm_mac_info_t node.
1592 * Update the existing node for the new op.
1596 * New op is "add". Previous op is "delete".
1597 * Update the fpm_mac_info_t for the new add.
1599 fpm_mac
->zebra_flags
= rmac
->flags
;
1601 fpm_mac
->vxlan_if
= vxlan_if
? vxlan_if
->ifindex
: 0;
1602 fpm_mac
->svi_if
= svi_if
? svi_if
->ifindex
: 0;
1604 UNSET_FLAG(fpm_mac
->fpm_flags
, ZEBRA_MAC_DELETE_FPM
);
1605 SET_FLAG(fpm_mac
->fpm_flags
, ZEBRA_MAC_UPDATE_FPM
);
1608 * New op is "delete". Previous op is "add".
1609 * Thus, no-op. Unset ZEBRA_MAC_UPDATE_FPM flag.
1611 SET_FLAG(fpm_mac
->fpm_flags
, ZEBRA_MAC_DELETE_FPM
);
1612 UNSET_FLAG(fpm_mac
->fpm_flags
, ZEBRA_MAC_UPDATE_FPM
);
1618 fpm_mac
= hash_get(zfpm_g
->fpm_mac_info_table
, &key
,
1619 zfpm_mac_info_alloc
);
1623 fpm_mac
->zebra_flags
= rmac
->flags
;
1624 fpm_mac
->vxlan_if
= vxlan_if
? vxlan_if
->ifindex
: 0;
1625 fpm_mac
->svi_if
= svi_if
? svi_if
->ifindex
: 0;
1627 SET_FLAG(fpm_mac
->fpm_flags
, ZEBRA_MAC_UPDATE_FPM
);
1629 SET_FLAG(fpm_mac
->fpm_flags
, ZEBRA_MAC_DELETE_FPM
);
1631 TAILQ_INSERT_TAIL(&zfpm_g
->mac_q
, fpm_mac
, fpm_mac_q_entries
);
1633 zfpm_g
->stats
.updates_triggered
++;
1635 /* If writes are already enabled, return. */
1636 if (zfpm_g
->t_write
)
1644 * This function is called when the FPM connections is established.
1645 * Iterate over all the RMAC entries for the given L3VNI
1646 * and enqueue the RMAC for FPM processing.
1648 static void zfpm_trigger_rmac_update_wrapper(struct hash_backet
*backet
,
1651 zebra_mac_t
*zrmac
= (zebra_mac_t
*)backet
->data
;
1652 zebra_l3vni_t
*zl3vni
= (zebra_l3vni_t
*)args
;
1654 zfpm_trigger_rmac_update(zrmac
, zl3vni
, false, "RMAC added");
1658 * This function is called when the FPM connections is established.
1659 * This function iterates over all the L3VNIs to trigger
1660 * FPM updates for RMACs currently available.
1662 static void zfpm_iterate_rmac_table(struct hash_backet
*backet
, void *args
)
1664 zebra_l3vni_t
*zl3vni
= (zebra_l3vni_t
*)backet
->data
;
1666 hash_iterate(zl3vni
->rmac_table
, zfpm_trigger_rmac_update_wrapper
,
1671 * zfpm_stats_timer_cb
1673 static int zfpm_stats_timer_cb(struct thread
*t
)
1675 zfpm_g
->t_stats
= NULL
;
1678 * Remember the stats collected in the last interval for display
1681 zfpm_stats_copy(&zfpm_g
->stats
, &zfpm_g
->last_ivl_stats
);
1684 * Add the current set of stats into the cumulative statistics.
1686 zfpm_stats_compose(&zfpm_g
->cumulative_stats
, &zfpm_g
->stats
,
1687 &zfpm_g
->cumulative_stats
);
1690 * Start collecting stats afresh over the next interval.
1692 zfpm_stats_reset(&zfpm_g
->stats
);
1694 zfpm_start_stats_timer();
1700 * zfpm_stop_stats_timer
1702 static void zfpm_stop_stats_timer(void)
1704 if (!zfpm_g
->t_stats
)
1707 zfpm_debug("Stopping existing stats timer");
1708 THREAD_TIMER_OFF(zfpm_g
->t_stats
);
1712 * zfpm_start_stats_timer
1714 void zfpm_start_stats_timer(void)
1716 assert(!zfpm_g
->t_stats
);
1718 thread_add_timer(zfpm_g
->master
, zfpm_stats_timer_cb
, 0,
1719 ZFPM_STATS_IVL_SECS
, &zfpm_g
->t_stats
);
1723 * Helper macro for zfpm_show_stats() below.
1725 #define ZFPM_SHOW_STAT(counter) \
1727 vty_out(vty, "%-40s %10lu %16lu\n", #counter, \
1728 total_stats.counter, zfpm_g->last_ivl_stats.counter); \
1734 static void zfpm_show_stats(struct vty
*vty
)
1736 zfpm_stats_t total_stats
;
1739 vty_out(vty
, "\n%-40s %10s Last %2d secs\n\n", "Counter", "Total",
1740 ZFPM_STATS_IVL_SECS
);
1743 * Compute the total stats up to this instant.
1745 zfpm_stats_compose(&zfpm_g
->cumulative_stats
, &zfpm_g
->stats
,
1748 ZFPM_SHOW_STAT(connect_calls
);
1749 ZFPM_SHOW_STAT(connect_no_sock
);
1750 ZFPM_SHOW_STAT(read_cb_calls
);
1751 ZFPM_SHOW_STAT(write_cb_calls
);
1752 ZFPM_SHOW_STAT(write_calls
);
1753 ZFPM_SHOW_STAT(partial_writes
);
1754 ZFPM_SHOW_STAT(max_writes_hit
);
1755 ZFPM_SHOW_STAT(t_write_yields
);
1756 ZFPM_SHOW_STAT(nop_deletes_skipped
);
1757 ZFPM_SHOW_STAT(route_adds
);
1758 ZFPM_SHOW_STAT(route_dels
);
1759 ZFPM_SHOW_STAT(updates_triggered
);
1760 ZFPM_SHOW_STAT(redundant_triggers
);
1761 ZFPM_SHOW_STAT(dests_del_after_update
);
1762 ZFPM_SHOW_STAT(t_conn_down_starts
);
1763 ZFPM_SHOW_STAT(t_conn_down_dests_processed
);
1764 ZFPM_SHOW_STAT(t_conn_down_yields
);
1765 ZFPM_SHOW_STAT(t_conn_down_finishes
);
1766 ZFPM_SHOW_STAT(t_conn_up_starts
);
1767 ZFPM_SHOW_STAT(t_conn_up_dests_processed
);
1768 ZFPM_SHOW_STAT(t_conn_up_yields
);
1769 ZFPM_SHOW_STAT(t_conn_up_aborts
);
1770 ZFPM_SHOW_STAT(t_conn_up_finishes
);
1772 if (!zfpm_g
->last_stats_clear_time
)
1775 elapsed
= zfpm_get_elapsed_time(zfpm_g
->last_stats_clear_time
);
1777 vty_out(vty
, "\nStats were cleared %lu seconds ago\n",
1778 (unsigned long)elapsed
);
1784 static void zfpm_clear_stats(struct vty
*vty
)
1786 if (!zfpm_is_enabled()) {
1787 vty_out(vty
, "The FPM module is not enabled...\n");
1791 zfpm_stats_reset(&zfpm_g
->stats
);
1792 zfpm_stats_reset(&zfpm_g
->last_ivl_stats
);
1793 zfpm_stats_reset(&zfpm_g
->cumulative_stats
);
1795 zfpm_stop_stats_timer();
1796 zfpm_start_stats_timer();
1798 zfpm_g
->last_stats_clear_time
= monotime(NULL
);
1800 vty_out(vty
, "Cleared FPM stats\n");
1804 * show_zebra_fpm_stats
1806 DEFUN (show_zebra_fpm_stats
,
1807 show_zebra_fpm_stats_cmd
,
1808 "show zebra fpm stats",
1811 "Forwarding Path Manager information\n"
1814 zfpm_show_stats(vty
);
1819 * clear_zebra_fpm_stats
1821 DEFUN (clear_zebra_fpm_stats
,
1822 clear_zebra_fpm_stats_cmd
,
1823 "clear zebra fpm stats",
1826 "Clear Forwarding Path Manager information\n"
1829 zfpm_clear_stats(vty
);
1834 * update fpm connection information
1836 DEFUN ( fpm_remote_ip
,
1838 "fpm connection ip A.B.C.D port (1-65535)",
1839 "fpm connection remote ip and port\n"
1840 "Remote fpm server ip A.B.C.D\n"
1844 in_addr_t fpm_server
;
1847 fpm_server
= inet_addr(argv
[3]->arg
);
1848 if (fpm_server
== INADDR_NONE
)
1849 return CMD_ERR_INCOMPLETE
;
1851 port_no
= atoi(argv
[5]->arg
);
1852 if (port_no
< TCP_MIN_PORT
|| port_no
> TCP_MAX_PORT
)
1853 return CMD_ERR_INCOMPLETE
;
1855 zfpm_g
->fpm_server
= fpm_server
;
1856 zfpm_g
->fpm_port
= port_no
;
1862 DEFUN ( no_fpm_remote_ip
,
1863 no_fpm_remote_ip_cmd
,
1864 "no fpm connection ip A.B.C.D port (1-65535)",
1865 "fpm connection remote ip and port\n"
1867 "Remote fpm server ip A.B.C.D\n"
1870 if (zfpm_g
->fpm_server
!= inet_addr(argv
[4]->arg
)
1871 || zfpm_g
->fpm_port
!= atoi(argv
[6]->arg
))
1872 return CMD_ERR_NO_MATCH
;
1874 zfpm_g
->fpm_server
= FPM_DEFAULT_IP
;
1875 zfpm_g
->fpm_port
= FPM_DEFAULT_PORT
;
1881 * zfpm_init_message_format
1883 static inline void zfpm_init_message_format(const char *format
)
1885 int have_netlink
, have_protobuf
;
1893 #ifdef HAVE_PROTOBUF
1899 zfpm_g
->message_format
= ZFPM_MSG_FORMAT_NONE
;
1903 zfpm_g
->message_format
= ZFPM_MSG_FORMAT_NETLINK
;
1904 } else if (have_protobuf
) {
1905 zfpm_g
->message_format
= ZFPM_MSG_FORMAT_PROTOBUF
;
1910 if (!strcmp("netlink", format
)) {
1911 if (!have_netlink
) {
1912 flog_err(EC_ZEBRA_NETLINK_NOT_AVAILABLE
,
1913 "FPM netlink message format is not available");
1916 zfpm_g
->message_format
= ZFPM_MSG_FORMAT_NETLINK
;
1920 if (!strcmp("protobuf", format
)) {
1921 if (!have_protobuf
) {
1923 EC_ZEBRA_PROTOBUF_NOT_AVAILABLE
,
1924 "FPM protobuf message format is not available");
1927 flog_warn(EC_ZEBRA_PROTOBUF_NOT_AVAILABLE
,
1928 "FPM protobuf message format is deprecated and scheduled to be removed. "
1929 "Please convert to using netlink format or contact dev@lists.frrouting.org with your use case.");
1930 zfpm_g
->message_format
= ZFPM_MSG_FORMAT_PROTOBUF
;
1934 flog_warn(EC_ZEBRA_FPM_FORMAT_UNKNOWN
, "Unknown fpm format '%s'",
1939 * fpm_remote_srv_write
1941 * Module to write remote fpm connection
1943 * Returns ZERO on success.
1946 static int fpm_remote_srv_write(struct vty
*vty
)
1950 in
.s_addr
= zfpm_g
->fpm_server
;
1952 if ((zfpm_g
->fpm_server
!= FPM_DEFAULT_IP
1953 && zfpm_g
->fpm_server
!= INADDR_ANY
)
1954 || (zfpm_g
->fpm_port
!= FPM_DEFAULT_PORT
&& zfpm_g
->fpm_port
!= 0))
1955 vty_out(vty
, "fpm connection ip %s port %d\n", inet_ntoa(in
),
1963 static struct cmd_node zebra_node
= {ZEBRA_NODE
, "", 1};
1969 * One-time initialization of the Zebra FPM module.
1971 * @param[in] port port at which FPM is running.
1972 * @param[in] enable true if the zebra FPM module should be enabled
1973 * @param[in] format to use to talk to the FPM. Can be 'netink' or 'protobuf'.
1975 * Returns true on success.
1977 static int zfpm_init(struct thread_master
*master
)
1981 const char *format
= THIS_MODULE
->load_args
;
1983 memset(zfpm_g
, 0, sizeof(*zfpm_g
));
1984 zfpm_g
->master
= master
;
1985 TAILQ_INIT(&zfpm_g
->dest_q
);
1986 TAILQ_INIT(&zfpm_g
->mac_q
);
1988 /* Create hash table for fpm_mac_info_t enties */
1989 zfpm_g
->fpm_mac_info_table
= hash_create(zfpm_mac_info_hash_keymake
,
1991 "FPM MAC info hash table");
1994 zfpm_g
->state
= ZFPM_STATE_IDLE
;
1996 zfpm_stats_init(&zfpm_g
->stats
);
1997 zfpm_stats_init(&zfpm_g
->last_ivl_stats
);
1998 zfpm_stats_init(&zfpm_g
->cumulative_stats
);
2000 install_node(&zebra_node
, fpm_remote_srv_write
);
2001 install_element(ENABLE_NODE
, &show_zebra_fpm_stats_cmd
);
2002 install_element(ENABLE_NODE
, &clear_zebra_fpm_stats_cmd
);
2003 install_element(CONFIG_NODE
, &fpm_remote_ip_cmd
);
2004 install_element(CONFIG_NODE
, &no_fpm_remote_ip_cmd
);
2006 zfpm_init_message_format(format
);
2009 * Disable FPM interface if no suitable format is available.
2011 if (zfpm_g
->message_format
== ZFPM_MSG_FORMAT_NONE
)
2014 zfpm_g
->enabled
= enable
;
2016 if (!zfpm_g
->fpm_server
)
2017 zfpm_g
->fpm_server
= FPM_DEFAULT_IP
;
2020 port
= FPM_DEFAULT_PORT
;
2022 zfpm_g
->fpm_port
= port
;
2024 zfpm_g
->obuf
= stream_new(ZFPM_OBUF_SIZE
);
2025 zfpm_g
->ibuf
= stream_new(ZFPM_IBUF_SIZE
);
2027 zfpm_start_stats_timer();
2028 zfpm_start_connect_timer("initialized");
2032 static int zebra_fpm_module_init(void)
2034 hook_register(rib_update
, zfpm_trigger_update
);
2035 hook_register(zebra_rmac_update
, zfpm_trigger_rmac_update
);
2036 hook_register(frr_late_init
, zfpm_init
);
2040 FRR_MODULE_SETUP(.name
= "zebra_fpm", .version
= FRR_VERSION
,
2041 .description
= "zebra FPM (Forwarding Plane Manager) module",
2042 .init
= zebra_fpm_module_init
, )