]> git.proxmox.com Git - mirror_frr.git/blame - zebra/zebra_fpm.c
lib: Add a couple functions to nexthop_group.c (#4323)
[mirror_frr.git] / zebra / zebra_fpm.c
CommitLineData
5adc2528
AS
1/*
2 * Main implementation file for interface to Forwarding Plane Manager.
3 *
4 * Copyright (C) 2012 by Open Source Routing.
5 * Copyright (C) 2012 by Internet Systems Consortium, Inc. ("ISC")
6 *
7 * This file is part of GNU Zebra.
8 *
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
12 * later version.
13 *
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.
18 *
896014f4
DL
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
5adc2528
AS
22 */
23
24#include <zebra.h>
25
26#include "log.h"
4f8ea50c 27#include "libfrr.h"
5adc2528
AS
28#include "stream.h"
29#include "thread.h"
30#include "network.h"
31#include "command.h"
4f8ea50c 32#include "version.h"
e5218ec8 33#include "jhash.h"
5adc2528
AS
34
35#include "zebra/rib.h"
7c551956
DS
36#include "zebra/zserv.h"
37#include "zebra/zebra_ns.h"
38#include "zebra/zebra_vrf.h"
67aeb554 39#include "zebra/zebra_errors.h"
e5218ec8 40#include "zebra/zebra_memory.h"
5adc2528
AS
41
42#include "fpm/fpm.h"
5adc2528 43#include "zebra_fpm_private.h"
e5218ec8 44#include "zebra/zebra_router.h"
a780a738 45#include "zebra_vxlan_private.h"
e5218ec8
AD
46
47DEFINE_MTYPE_STATIC(ZEBRA, FPM_MAC_INFO, "FPM_MAC_INFO");
5adc2528
AS
48
49/*
50 * Interval at which we attempt to connect to the FPM.
51 */
52#define ZFPM_CONNECT_RETRY_IVL 5
53
54/*
55 * Sizes of outgoing and incoming stream buffers for writing/reading
56 * FPM messages.
57 */
58#define ZFPM_OBUF_SIZE (2 * FPM_MAX_MSG_LEN)
59#define ZFPM_IBUF_SIZE (FPM_MAX_MSG_LEN)
60
61/*
62 * The maximum number of times the FPM socket write callback can call
63 * 'write' before it yields.
64 */
65#define ZFPM_MAX_WRITES_PER_RUN 10
66
67/*
68 * Interval over which we collect statistics.
69 */
70#define ZFPM_STATS_IVL_SECS 10
fbe748e5
AD
71#define FPM_MAX_MAC_MSG_LEN 512
72
73static void zfpm_iterate_rmac_table(struct hash_backet *backet, void *args);
5adc2528
AS
74
75/*
76 * Structure that holds state for iterating over all route_node
77 * structures that are candidates for being communicated to the FPM.
78 */
d62a17ae 79typedef struct zfpm_rnodes_iter_t_ {
80 rib_tables_iter_t tables_iter;
81 route_table_iter_t iter;
5adc2528
AS
82} zfpm_rnodes_iter_t;
83
84/*
85 * Statistics.
86 */
87typedef struct zfpm_stats_t_ {
d62a17ae 88 unsigned long connect_calls;
89 unsigned long connect_no_sock;
5adc2528 90
d62a17ae 91 unsigned long read_cb_calls;
5adc2528 92
d62a17ae 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;
5adc2528 98
d62a17ae 99 unsigned long nop_deletes_skipped;
100 unsigned long route_adds;
101 unsigned long route_dels;
5adc2528 102
d62a17ae 103 unsigned long updates_triggered;
104 unsigned long redundant_triggers;
5adc2528 105
d62a17ae 106 unsigned long dests_del_after_update;
5adc2528 107
d62a17ae 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;
5adc2528 112
d62a17ae 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;
5adc2528
AS
118
119} zfpm_stats_t;
120
121/*
122 * States for the FPM state machine.
123 */
124typedef enum {
125
d62a17ae 126 /*
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.
130 */
131 ZFPM_STATE_IDLE,
132
133 /*
134 * Ready to talk to the FPM and periodically trying to connect to
135 * it.
136 */
137 ZFPM_STATE_ACTIVE,
138
139 /*
140 * In the middle of bringing up a TCP connection. Specifically,
141 * waiting for a connect() call to complete asynchronously.
142 */
143 ZFPM_STATE_CONNECTING,
144
145 /*
146 * TCP connection to the FPM is up.
147 */
148 ZFPM_STATE_ESTABLISHED
5adc2528
AS
149
150} zfpm_state_t;
151
fb0aa886
AS
152/*
153 * Message format to be used to communicate with the FPM.
154 */
d62a17ae 155typedef enum {
156 ZFPM_MSG_FORMAT_NONE,
157 ZFPM_MSG_FORMAT_NETLINK,
158 ZFPM_MSG_FORMAT_PROTOBUF,
fb0aa886 159} zfpm_msg_format_e;
5adc2528
AS
160/*
161 * Globals.
162 */
d62a17ae 163typedef struct zfpm_glob_t_ {
164
165 /*
166 * True if the FPM module has been enabled.
167 */
168 int enabled;
169
170 /*
171 * Message format to be used to communicate with the fpm.
172 */
173 zfpm_msg_format_e message_format;
174
175 struct thread_master *master;
176
177 zfpm_state_t state;
178
179 in_addr_t fpm_server;
180 /*
181 * Port on which the FPM is running.
182 */
183 int fpm_port;
184
185 /*
186 * List of rib_dest_t structures to be processed
187 */
188 TAILQ_HEAD(zfpm_dest_q, rib_dest_t_) dest_q;
189
e5218ec8
AD
190 /*
191 * List of fpm_mac_info structures to be processed
192 */
193 TAILQ_HEAD(zfpm_mac_q, fpm_mac_info_t) mac_q;
194
195 /*
196 * Hash table of fpm_mac_info_t entries
197 *
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
205 * in the mac_q.
206 */
207 struct hash *fpm_mac_info_table;
208
d62a17ae 209 /*
210 * Stream socket to the FPM.
211 */
212 int sock;
213
214 /*
215 * Buffers for messages to/from the FPM.
216 */
217 struct stream *obuf;
218 struct stream *ibuf;
219
220 /*
221 * Threads for I/O.
222 */
223 struct thread *t_connect;
224 struct thread *t_write;
225 struct thread *t_read;
226
227 /*
228 * Thread to clean up after the TCP connection to the FPM goes down
229 * and the state that belongs to it.
230 */
231 struct thread *t_conn_down;
232
233 struct {
234 zfpm_rnodes_iter_t iter;
235 } t_conn_down_state;
236
237 /*
238 * Thread to take actions once the TCP conn to the FPM comes up, and
239 * the state that belongs to it.
240 */
241 struct thread *t_conn_up;
242
243 struct {
244 zfpm_rnodes_iter_t iter;
245 } t_conn_up_state;
246
247 unsigned long connect_calls;
248 time_t last_connect_call_time;
249
250 /*
251 * Stats from the start of the current statistics interval up to
252 * now. These are the counters we typically update in the code.
253 */
254 zfpm_stats_t stats;
255
256 /*
257 * Statistics that were gathered in the last collection interval.
258 */
259 zfpm_stats_t last_ivl_stats;
260
261 /*
262 * Cumulative stats from the last clear to the start of the current
263 * statistics interval.
264 */
265 zfpm_stats_t cumulative_stats;
266
267 /*
268 * Stats interval timer.
269 */
270 struct thread *t_stats;
271
272 /*
273 * If non-zero, the last time when statistics were cleared.
274 */
275 time_t last_stats_clear_time;
5adc2528
AS
276
277} zfpm_glob_t;
278
279static zfpm_glob_t zfpm_glob_space;
280static zfpm_glob_t *zfpm_g = &zfpm_glob_space;
281
d62a17ae 282static int zfpm_trigger_update(struct route_node *rn, const char *reason);
4f8ea50c 283
d62a17ae 284static int zfpm_read_cb(struct thread *thread);
285static int zfpm_write_cb(struct thread *thread);
5adc2528 286
d62a17ae 287static void zfpm_set_state(zfpm_state_t state, const char *reason);
288static void zfpm_start_connect_timer(const char *reason);
289static void zfpm_start_stats_timer(void);
a780a738 290static void zfpm_mac_info_del(struct fpm_mac_info_t *fpm_mac);
5adc2528
AS
291
292/*
293 * zfpm_thread_should_yield
294 */
d62a17ae 295static inline int zfpm_thread_should_yield(struct thread *t)
5adc2528 296{
d62a17ae 297 return thread_should_yield(t);
5adc2528
AS
298}
299
300/*
301 * zfpm_state_to_str
302 */
d62a17ae 303static const char *zfpm_state_to_str(zfpm_state_t state)
5adc2528 304{
d62a17ae 305 switch (state) {
5adc2528 306
d62a17ae 307 case ZFPM_STATE_IDLE:
308 return "idle";
5adc2528 309
d62a17ae 310 case ZFPM_STATE_ACTIVE:
311 return "active";
5adc2528 312
d62a17ae 313 case ZFPM_STATE_CONNECTING:
314 return "connecting";
5adc2528 315
d62a17ae 316 case ZFPM_STATE_ESTABLISHED:
317 return "established";
5adc2528 318
d62a17ae 319 default:
320 return "unknown";
321 }
5adc2528
AS
322}
323
5adc2528
AS
324/*
325 * zfpm_get_elapsed_time
326 *
327 * Returns the time elapsed (in seconds) since the given time.
328 */
d62a17ae 329static time_t zfpm_get_elapsed_time(time_t reference)
5adc2528 330{
d62a17ae 331 time_t now;
5adc2528 332
d62a17ae 333 now = monotime(NULL);
5adc2528 334
d62a17ae 335 if (now < reference) {
336 assert(0);
337 return 0;
338 }
5adc2528 339
d62a17ae 340 return now - reference;
5adc2528
AS
341}
342
5adc2528
AS
343/*
344 * zfpm_rnodes_iter_init
345 */
d62a17ae 346static inline void zfpm_rnodes_iter_init(zfpm_rnodes_iter_t *iter)
5adc2528 347{
d62a17ae 348 memset(iter, 0, sizeof(*iter));
349 rib_tables_iter_init(&iter->tables_iter);
350
351 /*
352 * This is a hack, but it makes implementing 'next' easier by
353 * ensuring that route_table_iter_next() will return NULL the first
354 * time we call it.
355 */
356 route_table_iter_init(&iter->iter, NULL);
357 route_table_iter_cleanup(&iter->iter);
5adc2528
AS
358}
359
360/*
361 * zfpm_rnodes_iter_next
362 */
d62a17ae 363static inline struct route_node *zfpm_rnodes_iter_next(zfpm_rnodes_iter_t *iter)
5adc2528 364{
d62a17ae 365 struct route_node *rn;
366 struct route_table *table;
5adc2528 367
d62a17ae 368 while (1) {
369 rn = route_table_iter_next(&iter->iter);
370 if (rn)
371 return rn;
5adc2528 372
d62a17ae 373 /*
374 * We've made our way through this table, go to the next one.
375 */
376 route_table_iter_cleanup(&iter->iter);
5adc2528 377
c6bbea17 378 table = rib_tables_iter_next(&iter->tables_iter);
5adc2528 379
d62a17ae 380 if (!table)
381 return NULL;
5adc2528 382
d62a17ae 383 route_table_iter_init(&iter->iter, table);
384 }
5adc2528 385
d62a17ae 386 return NULL;
5adc2528
AS
387}
388
389/*
390 * zfpm_rnodes_iter_pause
391 */
d62a17ae 392static inline void zfpm_rnodes_iter_pause(zfpm_rnodes_iter_t *iter)
5adc2528 393{
d62a17ae 394 route_table_iter_pause(&iter->iter);
5adc2528
AS
395}
396
397/*
398 * zfpm_rnodes_iter_cleanup
399 */
d62a17ae 400static inline void zfpm_rnodes_iter_cleanup(zfpm_rnodes_iter_t *iter)
5adc2528 401{
d62a17ae 402 route_table_iter_cleanup(&iter->iter);
403 rib_tables_iter_cleanup(&iter->tables_iter);
5adc2528
AS
404}
405
406/*
407 * zfpm_stats_init
408 *
409 * Initialize a statistics block.
410 */
d62a17ae 411static inline void zfpm_stats_init(zfpm_stats_t *stats)
5adc2528 412{
d62a17ae 413 memset(stats, 0, sizeof(*stats));
5adc2528
AS
414}
415
416/*
417 * zfpm_stats_reset
418 */
d62a17ae 419static inline void zfpm_stats_reset(zfpm_stats_t *stats)
5adc2528 420{
d62a17ae 421 zfpm_stats_init(stats);
5adc2528
AS
422}
423
424/*
425 * zfpm_stats_copy
426 */
d62a17ae 427static inline void zfpm_stats_copy(const zfpm_stats_t *src, zfpm_stats_t *dest)
5adc2528 428{
d62a17ae 429 memcpy(dest, src, sizeof(*dest));
5adc2528
AS
430}
431
432/*
433 * zfpm_stats_compose
434 *
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'.
438 *
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.
442 */
d62a17ae 443static void zfpm_stats_compose(const zfpm_stats_t *s1, const zfpm_stats_t *s2,
444 zfpm_stats_t *result)
5adc2528 445{
d62a17ae 446 const unsigned long *p1, *p2;
447 unsigned long *result_p;
448 int i, num_counters;
5adc2528 449
d62a17ae 450 p1 = (const unsigned long *)s1;
451 p2 = (const unsigned long *)s2;
452 result_p = (unsigned long *)result;
5adc2528 453
d62a17ae 454 num_counters = (sizeof(zfpm_stats_t) / sizeof(unsigned long));
5adc2528 455
d62a17ae 456 for (i = 0; i < num_counters; i++) {
457 result_p[i] = p1[i] + p2[i];
458 }
5adc2528
AS
459}
460
461/*
462 * zfpm_read_on
463 */
d62a17ae 464static inline void zfpm_read_on(void)
5adc2528 465{
d62a17ae 466 assert(!zfpm_g->t_read);
467 assert(zfpm_g->sock >= 0);
5adc2528 468
d62a17ae 469 thread_add_read(zfpm_g->master, zfpm_read_cb, 0, zfpm_g->sock,
470 &zfpm_g->t_read);
5adc2528
AS
471}
472
473/*
474 * zfpm_write_on
475 */
d62a17ae 476static inline void zfpm_write_on(void)
5adc2528 477{
d62a17ae 478 assert(!zfpm_g->t_write);
479 assert(zfpm_g->sock >= 0);
5adc2528 480
d62a17ae 481 thread_add_write(zfpm_g->master, zfpm_write_cb, 0, zfpm_g->sock,
482 &zfpm_g->t_write);
5adc2528
AS
483}
484
485/*
486 * zfpm_read_off
487 */
d62a17ae 488static inline void zfpm_read_off(void)
5adc2528 489{
d62a17ae 490 THREAD_READ_OFF(zfpm_g->t_read);
5adc2528
AS
491}
492
493/*
494 * zfpm_write_off
495 */
d62a17ae 496static inline void zfpm_write_off(void)
5adc2528 497{
d62a17ae 498 THREAD_WRITE_OFF(zfpm_g->t_write);
5adc2528
AS
499}
500
501/*
502 * zfpm_conn_up_thread_cb
503 *
504 * Callback for actions to be taken when the connection to the FPM
505 * comes up.
506 */
d62a17ae 507static int zfpm_conn_up_thread_cb(struct thread *thread)
5adc2528 508{
d62a17ae 509 struct route_node *rnode;
510 zfpm_rnodes_iter_t *iter;
511 rib_dest_t *dest;
5adc2528 512
d62a17ae 513 zfpm_g->t_conn_up = NULL;
5adc2528 514
d62a17ae 515 iter = &zfpm_g->t_conn_up_state.iter;
5adc2528 516
d62a17ae 517 if (zfpm_g->state != ZFPM_STATE_ESTABLISHED) {
518 zfpm_debug(
519 "Connection not up anymore, conn_up thread aborting");
520 zfpm_g->stats.t_conn_up_aborts++;
521 goto done;
522 }
5adc2528 523
fbe748e5
AD
524 /* Enqueue FPM updates for all the RMAC entries */
525 hash_iterate(zrouter.l3vni_table, zfpm_iterate_rmac_table, NULL);
526
d62a17ae 527 while ((rnode = zfpm_rnodes_iter_next(iter))) {
528 dest = rib_dest_from_rnode(rnode);
529
530 if (dest) {
531 zfpm_g->stats.t_conn_up_dests_processed++;
532 zfpm_trigger_update(rnode, NULL);
533 }
534
535 /*
536 * Yield if need be.
537 */
538 if (!zfpm_thread_should_yield(thread))
539 continue;
540
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);
546 return 0;
5adc2528
AS
547 }
548
d62a17ae 549 zfpm_g->stats.t_conn_up_finishes++;
550
551done:
552 zfpm_rnodes_iter_cleanup(iter);
553 return 0;
5adc2528
AS
554}
555
556/*
557 * zfpm_connection_up
558 *
559 * Called when the connection to the FPM comes up.
560 */
d62a17ae 561static void zfpm_connection_up(const char *detail)
5adc2528 562{
d62a17ae 563 assert(zfpm_g->sock >= 0);
564 zfpm_read_on();
565 zfpm_write_on();
566 zfpm_set_state(ZFPM_STATE_ESTABLISHED, detail);
567
568 /*
569 * Start thread to push existing routes to the FPM.
570 */
571 assert(!zfpm_g->t_conn_up);
572
573 zfpm_rnodes_iter_init(&zfpm_g->t_conn_up_state.iter);
574
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,
578 &zfpm_g->t_conn_up);
579 zfpm_g->stats.t_conn_up_starts++;
5adc2528
AS
580}
581
582/*
583 * zfpm_connect_check
584 *
585 * Check if an asynchronous connect() to the FPM is complete.
586 */
d62a17ae 587static void zfpm_connect_check(void)
5adc2528 588{
d62a17ae 589 int status;
590 socklen_t slen;
591 int ret;
592
593 zfpm_read_off();
594 zfpm_write_off();
595
596 slen = sizeof(status);
597 ret = getsockopt(zfpm_g->sock, SOL_SOCKET, SO_ERROR, (void *)&status,
598 &slen);
599
600 if (ret >= 0 && status == 0) {
601 zfpm_connection_up("async connect complete");
602 return;
603 }
604
605 /*
606 * getsockopt() failed or indicated an error on the socket.
607 */
608 close(zfpm_g->sock);
609 zfpm_g->sock = -1;
610
611 zfpm_start_connect_timer("getsockopt() after async connect failed");
612 return;
5adc2528
AS
613}
614
615/*
616 * zfpm_conn_down_thread_cb
617 *
618 * Callback that is invoked to clean up state after the TCP connection
619 * to the FPM goes down.
620 */
d62a17ae 621static int zfpm_conn_down_thread_cb(struct thread *thread)
5adc2528 622{
d62a17ae 623 struct route_node *rnode;
624 zfpm_rnodes_iter_t *iter;
625 rib_dest_t *dest;
a780a738 626 struct fpm_mac_info_t *mac = NULL;
5adc2528 627
d62a17ae 628 assert(zfpm_g->state == ZFPM_STATE_IDLE);
5adc2528 629
a780a738
AD
630 /*
631 * Delink and free all fpm_mac_info_t nodes
632 * in the mac_q and fpm_mac_info_hash
633 */
634 while ((mac = TAILQ_FIRST(&zfpm_g->mac_q)) != NULL)
635 zfpm_mac_info_del(mac);
636
d62a17ae 637 zfpm_g->t_conn_down = NULL;
5adc2528 638
d62a17ae 639 iter = &zfpm_g->t_conn_down_state.iter;
5adc2528 640
d62a17ae 641 while ((rnode = zfpm_rnodes_iter_next(iter))) {
642 dest = rib_dest_from_rnode(rnode);
5adc2528 643
d62a17ae 644 if (dest) {
645 if (CHECK_FLAG(dest->flags, RIB_DEST_UPDATE_FPM)) {
646 TAILQ_REMOVE(&zfpm_g->dest_q, dest,
647 fpm_q_entries);
648 }
649
650 UNSET_FLAG(dest->flags, RIB_DEST_UPDATE_FPM);
651 UNSET_FLAG(dest->flags, RIB_DEST_SENT_TO_FPM);
5adc2528 652
d62a17ae 653 zfpm_g->stats.t_conn_down_dests_processed++;
5adc2528 654
d62a17ae 655 /*
656 * Check if the dest should be deleted.
657 */
658 rib_gc_dest(rnode);
659 }
5adc2528 660
d62a17ae 661 /*
662 * Yield if need be.
663 */
664 if (!zfpm_thread_should_yield(thread))
665 continue;
666
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);
672 return 0;
5adc2528
AS
673 }
674
d62a17ae 675 zfpm_g->stats.t_conn_down_finishes++;
676 zfpm_rnodes_iter_cleanup(iter);
677
678 /*
679 * Start the process of connecting to the FPM again.
680 */
681 zfpm_start_connect_timer("cleanup complete");
682 return 0;
5adc2528
AS
683}
684
685/*
686 * zfpm_connection_down
687 *
688 * Called when the connection to the FPM has gone down.
689 */
d62a17ae 690static void zfpm_connection_down(const char *detail)
5adc2528 691{
d62a17ae 692 if (!detail)
693 detail = "unknown";
5adc2528 694
d62a17ae 695 assert(zfpm_g->state == ZFPM_STATE_ESTABLISHED);
5adc2528 696
d62a17ae 697 zlog_info("connection to the FPM has gone down: %s", detail);
5adc2528 698
d62a17ae 699 zfpm_read_off();
700 zfpm_write_off();
5adc2528 701
d62a17ae 702 stream_reset(zfpm_g->ibuf);
703 stream_reset(zfpm_g->obuf);
5adc2528 704
d62a17ae 705 if (zfpm_g->sock >= 0) {
706 close(zfpm_g->sock);
707 zfpm_g->sock = -1;
708 }
5adc2528 709
d62a17ae 710 /*
711 * Start thread to clean up state after the connection goes down.
712 */
713 assert(!zfpm_g->t_conn_down);
714 zfpm_debug("Starting conn_down thread");
715 zfpm_rnodes_iter_init(&zfpm_g->t_conn_down_state.iter);
716 zfpm_g->t_conn_down = NULL;
717 thread_add_timer_msec(zfpm_g->master, zfpm_conn_down_thread_cb, NULL, 0,
718 &zfpm_g->t_conn_down);
719 zfpm_g->stats.t_conn_down_starts++;
720
721 zfpm_set_state(ZFPM_STATE_IDLE, detail);
5adc2528
AS
722}
723
724/*
725 * zfpm_read_cb
726 */
d62a17ae 727static int zfpm_read_cb(struct thread *thread)
5adc2528 728{
d62a17ae 729 size_t already;
730 struct stream *ibuf;
731 uint16_t msg_len;
732 fpm_msg_hdr_t *hdr;
733
734 zfpm_g->stats.read_cb_calls++;
735 zfpm_g->t_read = NULL;
736
737 /*
738 * Check if async connect is now done.
739 */
740 if (zfpm_g->state == ZFPM_STATE_CONNECTING) {
741 zfpm_connect_check();
742 return 0;
5adc2528
AS
743 }
744
d62a17ae 745 assert(zfpm_g->state == ZFPM_STATE_ESTABLISHED);
746 assert(zfpm_g->sock >= 0);
5adc2528 747
d62a17ae 748 ibuf = zfpm_g->ibuf;
5adc2528 749
d62a17ae 750 already = stream_get_endp(ibuf);
751 if (already < FPM_MSG_HDR_LEN) {
752 ssize_t nbyte;
5adc2528 753
d62a17ae 754 nbyte = stream_read_try(ibuf, zfpm_g->sock,
755 FPM_MSG_HDR_LEN - already);
756 if (nbyte == 0 || nbyte == -1) {
677f704d
DS
757 if (nbyte == -1) {
758 char buffer[1024];
759
760 sprintf(buffer, "closed socket in read(%d): %s",
761 errno, safe_strerror(errno));
762 zfpm_connection_down(buffer);
996c9314 763 } else
677f704d 764 zfpm_connection_down("closed socket in read");
d62a17ae 765 return 0;
766 }
5adc2528 767
d62a17ae 768 if (nbyte != (ssize_t)(FPM_MSG_HDR_LEN - already))
769 goto done;
5adc2528 770
d62a17ae 771 already = FPM_MSG_HDR_LEN;
772 }
5adc2528 773
d62a17ae 774 stream_set_getp(ibuf, 0);
5adc2528 775
d62a17ae 776 hdr = (fpm_msg_hdr_t *)stream_pnt(ibuf);
5adc2528 777
d62a17ae 778 if (!fpm_msg_hdr_ok(hdr)) {
779 zfpm_connection_down("invalid message header");
780 return 0;
5adc2528
AS
781 }
782
d62a17ae 783 msg_len = fpm_msg_len(hdr);
5adc2528 784
d62a17ae 785 /*
786 * Read out the rest of the packet.
787 */
788 if (already < msg_len) {
789 ssize_t nbyte;
5adc2528 790
d62a17ae 791 nbyte = stream_read_try(ibuf, zfpm_g->sock, msg_len - already);
5adc2528 792
d62a17ae 793 if (nbyte == 0 || nbyte == -1) {
677f704d
DS
794 if (nbyte == -1) {
795 char buffer[1024];
796
797 sprintf(buffer, "failed to read message(%d) %s",
798 errno, safe_strerror(errno));
799 zfpm_connection_down(buffer);
996c9314 800 } else
677f704d 801 zfpm_connection_down("failed to read message");
d62a17ae 802 return 0;
803 }
804
805 if (nbyte != (ssize_t)(msg_len - already))
806 goto done;
807 }
808
809 zfpm_debug("Read out a full fpm message");
810
811 /*
812 * Just throw it away for now.
813 */
814 stream_reset(ibuf);
815
816done:
817 zfpm_read_on();
818 return 0;
5adc2528
AS
819}
820
21d814eb
AD
821static bool zfpm_updates_pending(void)
822{
823 if (!(TAILQ_EMPTY(&zfpm_g->dest_q)) || !(TAILQ_EMPTY(&zfpm_g->mac_q)))
824 return true;
825
826 return false;
827}
828
5adc2528
AS
829/*
830 * zfpm_writes_pending
831 *
2951a7a4 832 * Returns true if we may have something to write to the FPM.
5adc2528 833 */
d62a17ae 834static int zfpm_writes_pending(void)
5adc2528
AS
835{
836
d62a17ae 837 /*
838 * Check if there is any data in the outbound buffer that has not
839 * been written to the socket yet.
840 */
841 if (stream_get_endp(zfpm_g->obuf) - stream_get_getp(zfpm_g->obuf))
842 return 1;
5adc2528 843
d62a17ae 844 /*
21d814eb 845 * Check if there are any updates scheduled on the outbound queues.
d62a17ae 846 */
21d814eb 847 if (zfpm_updates_pending())
d62a17ae 848 return 1;
5adc2528 849
d62a17ae 850 return 0;
5adc2528
AS
851}
852
853/*
854 * zfpm_encode_route
855 *
856 * Encode a message to the FPM with information about the given route.
857 *
858 * Returns the number of bytes written to the buffer. 0 or a negative
859 * value indicates an error.
860 */
d62a17ae 861static inline int zfpm_encode_route(rib_dest_t *dest, struct route_entry *re,
862 char *in_buf, size_t in_buf_len,
863 fpm_msg_type_e *msg_type)
5adc2528 864{
d62a17ae 865 size_t len;
9bf75362 866#ifdef HAVE_NETLINK
d62a17ae 867 int cmd;
9bf75362 868#endif
d62a17ae 869 len = 0;
5adc2528 870
d62a17ae 871 *msg_type = FPM_MSG_TYPE_NONE;
5adc2528 872
d62a17ae 873 switch (zfpm_g->message_format) {
5adc2528 874
d62a17ae 875 case ZFPM_MSG_FORMAT_PROTOBUF:
fb0aa886 876#ifdef HAVE_PROTOBUF
d62a17ae 877 len = zfpm_protobuf_encode_route(dest, re, (uint8_t *)in_buf,
878 in_buf_len);
879 *msg_type = FPM_MSG_TYPE_PROTOBUF;
fb0aa886 880#endif
d62a17ae 881 break;
5adc2528 882
d62a17ae 883 case ZFPM_MSG_FORMAT_NETLINK:
fb0aa886 884#ifdef HAVE_NETLINK
d62a17ae 885 *msg_type = FPM_MSG_TYPE_NETLINK;
886 cmd = re ? RTM_NEWROUTE : RTM_DELROUTE;
887 len = zfpm_netlink_encode_route(cmd, dest, re, in_buf,
888 in_buf_len);
889 assert(fpm_msg_align(len) == len);
890 *msg_type = FPM_MSG_TYPE_NETLINK;
5adc2528 891#endif /* HAVE_NETLINK */
d62a17ae 892 break;
fb0aa886 893
d62a17ae 894 default:
895 break;
896 }
fb0aa886 897
d62a17ae 898 return len;
5adc2528
AS
899}
900
901/*
902 * zfpm_route_for_update
903 *
f0f77c9a 904 * Returns the re that is to be sent to the FPM for a given dest.
5adc2528 905 */
d62a17ae 906struct route_entry *zfpm_route_for_update(rib_dest_t *dest)
5adc2528 907{
5f7a4718 908 return dest->selected_fib;
5adc2528
AS
909}
910
911/*
21d814eb 912 * Define an enum for return codes for queue processing functions
5adc2528 913 *
21d814eb
AD
914 * FPM_WRITE_STOP: This return code indicates that the write buffer is full.
915 * Stop processing all the queues and empty the buffer by writing its content
916 * to the socket.
917 *
918 * FPM_GOTO_NEXT_Q: This return code indicates that either this queue is
919 * empty or we have processed enough updates from this queue.
920 * So, move on to the next queue.
5adc2528 921 */
21d814eb
AD
922enum {
923 FPM_WRITE_STOP = 0,
924 FPM_GOTO_NEXT_Q = 1
925};
926
927#define FPM_QUEUE_PROCESS_LIMIT 10000
928
929/*
930 * zfpm_build_route_updates
931 *
932 * Process the dest_q queue and write FPM messages to the outbound buffer.
933 */
934static int zfpm_build_route_updates(void)
5adc2528 935{
d62a17ae 936 struct stream *s;
937 rib_dest_t *dest;
938 unsigned char *buf, *data, *buf_end;
939 size_t msg_len;
940 size_t data_len;
941 fpm_msg_hdr_t *hdr;
942 struct route_entry *re;
943 int is_add, write_msg;
944 fpm_msg_type_e msg_type;
21d814eb 945 uint16_t q_limit;
d62a17ae 946
21d814eb
AD
947 if (TAILQ_EMPTY(&zfpm_g->dest_q))
948 return FPM_GOTO_NEXT_Q;
d62a17ae 949
21d814eb
AD
950 s = zfpm_g->obuf;
951 q_limit = FPM_QUEUE_PROCESS_LIMIT;
d62a17ae 952
21d814eb 953 do {
d62a17ae 954 /*
955 * Make sure there is enough space to write another message.
956 */
957 if (STREAM_WRITEABLE(s) < FPM_MAX_MSG_LEN)
21d814eb 958 return FPM_WRITE_STOP;
d62a17ae 959
960 buf = STREAM_DATA(s) + stream_get_endp(s);
961 buf_end = buf + STREAM_WRITEABLE(s);
962
963 dest = TAILQ_FIRST(&zfpm_g->dest_q);
964 if (!dest)
21d814eb 965 return FPM_GOTO_NEXT_Q;
d62a17ae 966
967 assert(CHECK_FLAG(dest->flags, RIB_DEST_UPDATE_FPM));
968
969 hdr = (fpm_msg_hdr_t *)buf;
970 hdr->version = FPM_PROTO_VERSION;
971
972 data = fpm_msg_data(hdr);
973
974 re = zfpm_route_for_update(dest);
975 is_add = re ? 1 : 0;
976
977 write_msg = 1;
978
979 /*
980 * If this is a route deletion, and we have not sent the route
981 * to
982 * the FPM previously, skip it.
983 */
984 if (!is_add && !CHECK_FLAG(dest->flags, RIB_DEST_SENT_TO_FPM)) {
985 write_msg = 0;
986 zfpm_g->stats.nop_deletes_skipped++;
987 }
988
989 if (write_msg) {
990 data_len = zfpm_encode_route(dest, re, (char *)data,
991 buf_end - data, &msg_type);
992
993 assert(data_len);
994 if (data_len) {
995 hdr->msg_type = msg_type;
996 msg_len = fpm_data_len_to_msg_len(data_len);
997 hdr->msg_len = htons(msg_len);
998 stream_forward_endp(s, msg_len);
999
1000 if (is_add)
1001 zfpm_g->stats.route_adds++;
1002 else
1003 zfpm_g->stats.route_dels++;
1004 }
1005 }
1006
1007 /*
1008 * Remove the dest from the queue, and reset the flag.
1009 */
1010 UNSET_FLAG(dest->flags, RIB_DEST_UPDATE_FPM);
1011 TAILQ_REMOVE(&zfpm_g->dest_q, dest, fpm_q_entries);
1012
1013 if (is_add) {
1014 SET_FLAG(dest->flags, RIB_DEST_SENT_TO_FPM);
1015 } else {
1016 UNSET_FLAG(dest->flags, RIB_DEST_SENT_TO_FPM);
1017 }
1018
1019 /*
1020 * Delete the destination if necessary.
1021 */
1022 if (rib_gc_dest(dest->rnode))
1023 zfpm_g->stats.dests_del_after_update++;
1024
21d814eb
AD
1025 q_limit--;
1026 if (q_limit == 0) {
1027 /*
1028 * We have processed enough updates in this queue.
1029 * Now yield for other queues.
1030 */
1031 return FPM_GOTO_NEXT_Q;
1032 }
c5431822 1033 } while (true);
21d814eb
AD
1034}
1035
1036/*
1037 * zfpm_encode_mac
1038 *
1039 * Encode a message to FPM with information about the given MAC.
1040 *
1041 * Returns the number of bytes written to the buffer.
1042 */
1043static inline int zfpm_encode_mac(struct fpm_mac_info_t *mac, char *in_buf,
1044 size_t in_buf_len, fpm_msg_type_e *msg_type)
1045{
1046 size_t len = 0;
1047
1048 *msg_type = FPM_MSG_TYPE_NONE;
1049
1050 switch (zfpm_g->message_format) {
1051
1052 case ZFPM_MSG_FORMAT_NONE:
1053 break;
1054 case ZFPM_MSG_FORMAT_NETLINK:
9da60d0a
AD
1055#ifdef HAVE_NETLINK
1056 len = zfpm_netlink_encode_mac(mac, in_buf, in_buf_len);
1057 assert(fpm_msg_align(len) == len);
1058 *msg_type = FPM_MSG_TYPE_NETLINK;
1059#endif /* HAVE_NETLINK */
21d814eb
AD
1060 break;
1061 case ZFPM_MSG_FORMAT_PROTOBUF:
1062 break;
1063 }
1064 return len;
1065}
1066
1067static int zfpm_build_mac_updates(void)
1068{
1069 struct stream *s;
1070 struct fpm_mac_info_t *mac;
1071 unsigned char *buf, *data, *buf_end;
1072 fpm_msg_hdr_t *hdr;
1073 size_t data_len, msg_len;
1074 fpm_msg_type_e msg_type;
1075 uint16_t q_limit;
1076
1077 if (TAILQ_EMPTY(&zfpm_g->mac_q))
1078 return FPM_GOTO_NEXT_Q;
1079
1080 s = zfpm_g->obuf;
1081 q_limit = FPM_QUEUE_PROCESS_LIMIT;
1082
1083 do {
1084 /* Make sure there is enough space to write another message. */
1085 if (STREAM_WRITEABLE(s) < FPM_MAX_MAC_MSG_LEN)
1086 return FPM_WRITE_STOP;
1087
1088 buf = STREAM_DATA(s) + stream_get_endp(s);
1089 buf_end = buf + STREAM_WRITEABLE(s);
1090
1091 mac = TAILQ_FIRST(&zfpm_g->mac_q);
1092 if (!mac)
1093 return FPM_GOTO_NEXT_Q;
1094
1095 /* Check for no-op */
1096 if (!CHECK_FLAG(mac->fpm_flags, ZEBRA_MAC_UPDATE_FPM)) {
1097 zfpm_g->stats.nop_deletes_skipped++;
1098 zfpm_mac_info_del(mac);
1099 continue;
1100 }
1101
1102 hdr = (fpm_msg_hdr_t *)buf;
1103 hdr->version = FPM_PROTO_VERSION;
1104
1105 data = fpm_msg_data(hdr);
1106 data_len = zfpm_encode_mac(mac, (char *)data, buf_end - data,
1107 &msg_type);
9da60d0a 1108 assert(data_len);
21d814eb
AD
1109
1110 hdr->msg_type = msg_type;
1111 msg_len = fpm_data_len_to_msg_len(data_len);
1112 hdr->msg_len = htons(msg_len);
1113 stream_forward_endp(s, msg_len);
1114
1115 /* Remove the MAC from the queue, and delete it. */
1116 zfpm_mac_info_del(mac);
1117
1118 q_limit--;
1119 if (q_limit == 0) {
1120 /*
1121 * We have processed enough updates in this queue.
1122 * Now yield for other queues.
1123 */
1124 return FPM_GOTO_NEXT_Q;
1125 }
d62a17ae 1126 } while (1);
5adc2528
AS
1127}
1128
21d814eb
AD
1129/*
1130 * zfpm_build_updates
1131 *
1132 * Process the outgoing queues and write messages to the outbound
1133 * buffer.
1134 */
1135static void zfpm_build_updates(void)
1136{
1137 struct stream *s;
1138
1139 s = zfpm_g->obuf;
1140 assert(stream_empty(s));
1141
1142 do {
1143 /*
1144 * Stop processing the queues if zfpm_g->obuf is full
1145 * or we do not have more updates to process
1146 */
1147 if (zfpm_build_mac_updates() == FPM_WRITE_STOP)
1148 break;
1149 if (zfpm_build_route_updates() == FPM_WRITE_STOP)
1150 break;
1151 } while (zfpm_updates_pending());
1152}
1153
5adc2528
AS
1154/*
1155 * zfpm_write_cb
1156 */
d62a17ae 1157static int zfpm_write_cb(struct thread *thread)
5adc2528 1158{
d62a17ae 1159 struct stream *s;
1160 int num_writes;
1161
1162 zfpm_g->stats.write_cb_calls++;
1163 zfpm_g->t_write = NULL;
1164
1165 /*
1166 * Check if async connect is now done.
1167 */
1168 if (zfpm_g->state == ZFPM_STATE_CONNECTING) {
1169 zfpm_connect_check();
1170 return 0;
1171 }
5adc2528 1172
d62a17ae 1173 assert(zfpm_g->state == ZFPM_STATE_ESTABLISHED);
1174 assert(zfpm_g->sock >= 0);
5adc2528 1175
d62a17ae 1176 num_writes = 0;
5adc2528 1177
d62a17ae 1178 do {
1179 int bytes_to_write, bytes_written;
5adc2528 1180
d62a17ae 1181 s = zfpm_g->obuf;
5adc2528 1182
d62a17ae 1183 /*
1184 * If the stream is empty, try fill it up with data.
1185 */
1186 if (stream_empty(s)) {
1187 zfpm_build_updates();
1188 }
5adc2528 1189
d62a17ae 1190 bytes_to_write = stream_get_endp(s) - stream_get_getp(s);
1191 if (!bytes_to_write)
1192 break;
5adc2528 1193
d62a17ae 1194 bytes_written =
2d34fb80 1195 write(zfpm_g->sock, stream_pnt(s), bytes_to_write);
d62a17ae 1196 zfpm_g->stats.write_calls++;
1197 num_writes++;
5adc2528 1198
d62a17ae 1199 if (bytes_written < 0) {
1200 if (ERRNO_IO_RETRY(errno))
1201 break;
5adc2528 1202
d62a17ae 1203 zfpm_connection_down("failed to write to socket");
1204 return 0;
1205 }
5adc2528 1206
d62a17ae 1207 if (bytes_written != bytes_to_write) {
5adc2528 1208
d62a17ae 1209 /*
1210 * Partial write.
1211 */
1212 stream_forward_getp(s, bytes_written);
1213 zfpm_g->stats.partial_writes++;
1214 break;
1215 }
5adc2528 1216
d62a17ae 1217 /*
1218 * We've written out the entire contents of the stream.
1219 */
1220 stream_reset(s);
5adc2528 1221
d62a17ae 1222 if (num_writes >= ZFPM_MAX_WRITES_PER_RUN) {
1223 zfpm_g->stats.max_writes_hit++;
1224 break;
1225 }
5adc2528 1226
d62a17ae 1227 if (zfpm_thread_should_yield(thread)) {
1228 zfpm_g->stats.t_write_yields++;
1229 break;
1230 }
1231 } while (1);
5adc2528 1232
d62a17ae 1233 if (zfpm_writes_pending())
1234 zfpm_write_on();
5adc2528 1235
d62a17ae 1236 return 0;
5adc2528
AS
1237}
1238
1239/*
1240 * zfpm_connect_cb
1241 */
d62a17ae 1242static int zfpm_connect_cb(struct thread *t)
5adc2528 1243{
d62a17ae 1244 int sock, ret;
1245 struct sockaddr_in serv;
1246
1247 zfpm_g->t_connect = NULL;
1248 assert(zfpm_g->state == ZFPM_STATE_ACTIVE);
1249
1250 sock = socket(AF_INET, SOCK_STREAM, 0);
1251 if (sock < 0) {
1252 zfpm_debug("Failed to create socket for connect(): %s",
1253 strerror(errno));
1254 zfpm_g->stats.connect_no_sock++;
1255 return 0;
1256 }
1257
1258 set_nonblocking(sock);
1259
1260 /* Make server socket. */
1261 memset(&serv, 0, sizeof(serv));
1262 serv.sin_family = AF_INET;
1263 serv.sin_port = htons(zfpm_g->fpm_port);
5adc2528 1264#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
d62a17ae 1265 serv.sin_len = sizeof(struct sockaddr_in);
5adc2528 1266#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
d62a17ae 1267 if (!zfpm_g->fpm_server)
1268 serv.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
1269 else
1270 serv.sin_addr.s_addr = (zfpm_g->fpm_server);
1271
1272 /*
1273 * Connect to the FPM.
1274 */
1275 zfpm_g->connect_calls++;
1276 zfpm_g->stats.connect_calls++;
1277 zfpm_g->last_connect_call_time = monotime(NULL);
1278
1279 ret = connect(sock, (struct sockaddr *)&serv, sizeof(serv));
1280 if (ret >= 0) {
1281 zfpm_g->sock = sock;
1282 zfpm_connection_up("connect succeeded");
1283 return 1;
1284 }
1285
1286 if (errno == EINPROGRESS) {
1287 zfpm_g->sock = sock;
1288 zfpm_read_on();
1289 zfpm_write_on();
1290 zfpm_set_state(ZFPM_STATE_CONNECTING,
1291 "async connect in progress");
1292 return 0;
1293 }
1294
1295 zlog_info("can't connect to FPM %d: %s", sock, safe_strerror(errno));
1296 close(sock);
1297
1298 /*
1299 * Restart timer for retrying connection.
1300 */
1301 zfpm_start_connect_timer("connect() failed");
1302 return 0;
5adc2528
AS
1303}
1304
1305/*
1306 * zfpm_set_state
1307 *
1308 * Move state machine into the given state.
1309 */
d62a17ae 1310static void zfpm_set_state(zfpm_state_t state, const char *reason)
5adc2528 1311{
d62a17ae 1312 zfpm_state_t cur_state = zfpm_g->state;
1313
1314 if (!reason)
1315 reason = "Unknown";
1316
1317 if (state == cur_state)
1318 return;
1319
1320 zfpm_debug("beginning state transition %s -> %s. Reason: %s",
1321 zfpm_state_to_str(cur_state), zfpm_state_to_str(state),
1322 reason);
1323
1324 switch (state) {
1325
1326 case ZFPM_STATE_IDLE:
1327 assert(cur_state == ZFPM_STATE_ESTABLISHED);
1328 break;
1329
1330 case ZFPM_STATE_ACTIVE:
1331 assert(cur_state == ZFPM_STATE_IDLE
1332 || cur_state == ZFPM_STATE_CONNECTING);
1333 assert(zfpm_g->t_connect);
1334 break;
1335
1336 case ZFPM_STATE_CONNECTING:
1337 assert(zfpm_g->sock);
1338 assert(cur_state == ZFPM_STATE_ACTIVE);
1339 assert(zfpm_g->t_read);
1340 assert(zfpm_g->t_write);
1341 break;
1342
1343 case ZFPM_STATE_ESTABLISHED:
1344 assert(cur_state == ZFPM_STATE_ACTIVE
1345 || cur_state == ZFPM_STATE_CONNECTING);
1346 assert(zfpm_g->sock);
1347 assert(zfpm_g->t_read);
1348 assert(zfpm_g->t_write);
1349 break;
1350 }
1351
1352 zfpm_g->state = state;
5adc2528
AS
1353}
1354
1355/*
1356 * zfpm_calc_connect_delay
1357 *
1358 * Returns the number of seconds after which we should attempt to
1359 * reconnect to the FPM.
1360 */
d62a17ae 1361static long zfpm_calc_connect_delay(void)
5adc2528 1362{
d62a17ae 1363 time_t elapsed;
5adc2528 1364
d62a17ae 1365 /*
1366 * Return 0 if this is our first attempt to connect.
1367 */
1368 if (zfpm_g->connect_calls == 0) {
1369 return 0;
1370 }
5adc2528 1371
d62a17ae 1372 elapsed = zfpm_get_elapsed_time(zfpm_g->last_connect_call_time);
5adc2528 1373
d62a17ae 1374 if (elapsed > ZFPM_CONNECT_RETRY_IVL) {
1375 return 0;
1376 }
5adc2528 1377
d62a17ae 1378 return ZFPM_CONNECT_RETRY_IVL - elapsed;
5adc2528
AS
1379}
1380
1381/*
1382 * zfpm_start_connect_timer
1383 */
d62a17ae 1384static void zfpm_start_connect_timer(const char *reason)
5adc2528 1385{
d62a17ae 1386 long delay_secs;
5adc2528 1387
d62a17ae 1388 assert(!zfpm_g->t_connect);
1389 assert(zfpm_g->sock < 0);
5adc2528 1390
d62a17ae 1391 assert(zfpm_g->state == ZFPM_STATE_IDLE
1392 || zfpm_g->state == ZFPM_STATE_ACTIVE
1393 || zfpm_g->state == ZFPM_STATE_CONNECTING);
5adc2528 1394
d62a17ae 1395 delay_secs = zfpm_calc_connect_delay();
1396 zfpm_debug("scheduling connect in %ld seconds", delay_secs);
5adc2528 1397
d62a17ae 1398 thread_add_timer(zfpm_g->master, zfpm_connect_cb, 0, delay_secs,
1399 &zfpm_g->t_connect);
1400 zfpm_set_state(ZFPM_STATE_ACTIVE, reason);
5adc2528
AS
1401}
1402
1403/*
1404 * zfpm_is_enabled
1405 *
2951a7a4 1406 * Returns true if the zebra FPM module has been enabled.
5adc2528 1407 */
d62a17ae 1408static inline int zfpm_is_enabled(void)
5adc2528 1409{
d62a17ae 1410 return zfpm_g->enabled;
5adc2528
AS
1411}
1412
1413/*
1414 * zfpm_conn_is_up
1415 *
2951a7a4 1416 * Returns true if the connection to the FPM is up.
5adc2528 1417 */
d62a17ae 1418static inline int zfpm_conn_is_up(void)
5adc2528 1419{
d62a17ae 1420 if (zfpm_g->state != ZFPM_STATE_ESTABLISHED)
1421 return 0;
5adc2528 1422
d62a17ae 1423 assert(zfpm_g->sock >= 0);
5adc2528 1424
d62a17ae 1425 return 1;
5adc2528
AS
1426}
1427
1428/*
1429 * zfpm_trigger_update
1430 *
1431 * The zebra code invokes this function to indicate that we should
1432 * send an update to the FPM about the given route_node.
1433 */
d62a17ae 1434static int zfpm_trigger_update(struct route_node *rn, const char *reason)
5adc2528 1435{
d62a17ae 1436 rib_dest_t *dest;
1437 char buf[PREFIX_STRLEN];
1438
1439 /*
1440 * Ignore if the connection is down. We will update the FPM about
1441 * all destinations once the connection comes up.
1442 */
1443 if (!zfpm_conn_is_up())
1444 return 0;
1445
1446 dest = rib_dest_from_rnode(rn);
1447
d62a17ae 1448 if (CHECK_FLAG(dest->flags, RIB_DEST_UPDATE_FPM)) {
1449 zfpm_g->stats.redundant_triggers++;
1450 return 0;
1451 }
1452
1453 if (reason) {
1454 zfpm_debug("%s triggering update to FPM - Reason: %s",
1455 prefix2str(&rn->p, buf, sizeof(buf)), reason);
1456 }
1457
1458 SET_FLAG(dest->flags, RIB_DEST_UPDATE_FPM);
1459 TAILQ_INSERT_TAIL(&zfpm_g->dest_q, dest, fpm_q_entries);
1460 zfpm_g->stats.updates_triggered++;
1461
1462 /*
1463 * Make sure that writes are enabled.
1464 */
1465 if (zfpm_g->t_write)
1466 return 0;
1467
1468 zfpm_write_on();
1469 return 0;
5adc2528
AS
1470}
1471
e5218ec8
AD
1472/*
1473 * Generate Key for FPM MAC info hash entry
1474 * Key is generated using MAC address and VNI id which should be sufficient
1475 * to provide uniqueness
1476 */
1477static unsigned int zfpm_mac_info_hash_keymake(const void *p)
1478{
1479 struct fpm_mac_info_t *fpm_mac = (struct fpm_mac_info_t *)p;
1480 uint32_t mac_key;
1481
1482 mac_key = jhash(fpm_mac->macaddr.octet, ETH_ALEN, 0xa5a5a55a);
1483
1484 return jhash_2words(mac_key, fpm_mac->vni, 0);
1485}
1486
1487/*
1488 * Compare function for FPM MAC info hash lookup
1489 */
1490static bool zfpm_mac_info_cmp(const void *p1, const void *p2)
1491{
1492 const struct fpm_mac_info_t *fpm_mac1 = p1;
1493 const struct fpm_mac_info_t *fpm_mac2 = p2;
1494
1495 if (memcmp(fpm_mac1->macaddr.octet, fpm_mac2->macaddr.octet, ETH_ALEN)
1496 != 0)
1497 return false;
1498 if (fpm_mac1->r_vtep_ip.s_addr != fpm_mac2->r_vtep_ip.s_addr)
1499 return false;
1500 if (fpm_mac1->vni != fpm_mac2->vni)
1501 return false;
1502
1503 return true;
1504}
1505
1506/*
1507 * Lookup FPM MAC info hash entry.
1508 */
1509static struct fpm_mac_info_t *zfpm_mac_info_lookup(struct fpm_mac_info_t *key)
1510{
1511 return hash_lookup(zfpm_g->fpm_mac_info_table, key);
1512}
1513
1514/*
1515 * Callback to allocate fpm_mac_info_t structure.
1516 */
1517static void *zfpm_mac_info_alloc(void *p)
1518{
1519 const struct fpm_mac_info_t *key = p;
1520 struct fpm_mac_info_t *fpm_mac;
1521
1522 fpm_mac = XCALLOC(MTYPE_FPM_MAC_INFO, sizeof(struct fpm_mac_info_t));
1523
1524 memcpy(&fpm_mac->macaddr, &key->macaddr, ETH_ALEN);
1525 memcpy(&fpm_mac->r_vtep_ip, &key->r_vtep_ip, sizeof(struct in_addr));
1526 fpm_mac->vni = key->vni;
1527
1528 return (void *)fpm_mac;
1529}
1530
1531/*
1532 * Delink and free fpm_mac_info_t.
1533 */
1534static void zfpm_mac_info_del(struct fpm_mac_info_t *fpm_mac)
1535{
1536 hash_release(zfpm_g->fpm_mac_info_table, fpm_mac);
1537 TAILQ_REMOVE(&zfpm_g->mac_q, fpm_mac, fpm_mac_q_entries);
1538 XFREE(MTYPE_FPM_MAC_INFO, fpm_mac);
1539}
1540
a780a738
AD
1541/*
1542 * zfpm_trigger_rmac_update
1543 *
1544 * Zebra code invokes this function to indicate that we should
1545 * send an update to FPM for given MAC entry.
1546 *
1547 * This function checks if we already have enqueued an update for this RMAC,
1548 * If yes, update the same fpm_mac_info_t. Else, create and enqueue an update.
1549 */
1550static int zfpm_trigger_rmac_update(zebra_mac_t *rmac, zebra_l3vni_t *zl3vni,
1551 bool delete, const char *reason)
1552{
1553 char buf[ETHER_ADDR_STRLEN];
1554 struct fpm_mac_info_t *fpm_mac, key;
1555 struct interface *vxlan_if, *svi_if;
1556
1557 /*
1558 * Ignore if the connection is down. We will update the FPM about
1559 * all destinations once the connection comes up.
1560 */
1561 if (!zfpm_conn_is_up())
1562 return 0;
1563
1564 if (reason) {
1565 zfpm_debug("triggering update to FPM - Reason: %s - %s",
1566 reason,
1567 prefix_mac2str(&rmac->macaddr, buf, sizeof(buf)));
1568 }
1569
1570 vxlan_if = zl3vni_map_to_vxlan_if(zl3vni);
1571 svi_if = zl3vni_map_to_svi_if(zl3vni);
1572
1573 memset(&key, 0, sizeof(struct fpm_mac_info_t));
1574
1575 memcpy(&key.macaddr, &rmac->macaddr, ETH_ALEN);
1576 key.r_vtep_ip.s_addr = rmac->fwd_info.r_vtep_ip.s_addr;
1577 key.vni = zl3vni->vni;
1578
1579 /* Check if this MAC is already present in the queue. */
1580 fpm_mac = zfpm_mac_info_lookup(&key);
1581
1582 if (fpm_mac) {
1583 if (!!CHECK_FLAG(fpm_mac->fpm_flags, ZEBRA_MAC_DELETE_FPM)
1584 == delete) {
1585 /*
1586 * MAC is already present in the queue
1587 * with the same op as this one. Do nothing
1588 */
1589 zfpm_g->stats.redundant_triggers++;
1590 return 0;
1591 }
1592
1593 /*
1594 * A new op for an already existing fpm_mac_info_t node.
1595 * Update the existing node for the new op.
1596 */
1597 if (!delete) {
1598 /*
1599 * New op is "add". Previous op is "delete".
1600 * Update the fpm_mac_info_t for the new add.
1601 */
1602 fpm_mac->zebra_flags = rmac->flags;
1603
1604 fpm_mac->vxlan_if = vxlan_if ? vxlan_if->ifindex : 0;
1605 fpm_mac->svi_if = svi_if ? svi_if->ifindex : 0;
1606
1607 UNSET_FLAG(fpm_mac->fpm_flags, ZEBRA_MAC_DELETE_FPM);
1608 SET_FLAG(fpm_mac->fpm_flags, ZEBRA_MAC_UPDATE_FPM);
1609 } else {
1610 /*
1611 * New op is "delete". Previous op is "add".
1612 * Thus, no-op. Unset ZEBRA_MAC_UPDATE_FPM flag.
1613 */
1614 SET_FLAG(fpm_mac->fpm_flags, ZEBRA_MAC_DELETE_FPM);
1615 UNSET_FLAG(fpm_mac->fpm_flags, ZEBRA_MAC_UPDATE_FPM);
1616 }
1617
1618 return 0;
1619 }
1620
1621 fpm_mac = hash_get(zfpm_g->fpm_mac_info_table, &key,
1622 zfpm_mac_info_alloc);
1623 if (!fpm_mac)
1624 return 0;
1625
c5431822 1626 fpm_mac->zebra_flags = rmac->flags;
a780a738
AD
1627 fpm_mac->vxlan_if = vxlan_if ? vxlan_if->ifindex : 0;
1628 fpm_mac->svi_if = svi_if ? svi_if->ifindex : 0;
1629
1630 SET_FLAG(fpm_mac->fpm_flags, ZEBRA_MAC_UPDATE_FPM);
1631 if (delete)
1632 SET_FLAG(fpm_mac->fpm_flags, ZEBRA_MAC_DELETE_FPM);
1633
1634 TAILQ_INSERT_TAIL(&zfpm_g->mac_q, fpm_mac, fpm_mac_q_entries);
1635
1636 zfpm_g->stats.updates_triggered++;
1637
a780a738
AD
1638 /* If writes are already enabled, return. */
1639 if (zfpm_g->t_write)
1640 return 0;
1641
1642 zfpm_write_on();
1643 return 0;
1644}
1645
fbe748e5
AD
1646/*
1647 * This function is called when the FPM connections is established.
1648 * Iterate over all the RMAC entries for the given L3VNI
1649 * and enqueue the RMAC for FPM processing.
1650 */
1651static void zfpm_trigger_rmac_update_wrapper(struct hash_backet *backet,
1652 void *args)
1653{
1654 zebra_mac_t *zrmac = (zebra_mac_t *)backet->data;
1655 zebra_l3vni_t *zl3vni = (zebra_l3vni_t *)args;
1656
1657 zfpm_trigger_rmac_update(zrmac, zl3vni, false, "RMAC added");
1658}
1659
1660/*
1661 * This function is called when the FPM connections is established.
1662 * This function iterates over all the L3VNIs to trigger
1663 * FPM updates for RMACs currently available.
1664 */
1665static void zfpm_iterate_rmac_table(struct hash_backet *backet, void *args)
1666{
1667 zebra_l3vni_t *zl3vni = (zebra_l3vni_t *)backet->data;
1668
1669 hash_iterate(zl3vni->rmac_table, zfpm_trigger_rmac_update_wrapper,
1670 (void *)zl3vni);
1671}
1672
5adc2528
AS
1673/*
1674 * zfpm_stats_timer_cb
1675 */
d62a17ae 1676static int zfpm_stats_timer_cb(struct thread *t)
5adc2528 1677{
d62a17ae 1678 zfpm_g->t_stats = NULL;
5adc2528 1679
d62a17ae 1680 /*
1681 * Remember the stats collected in the last interval for display
1682 * purposes.
1683 */
1684 zfpm_stats_copy(&zfpm_g->stats, &zfpm_g->last_ivl_stats);
5adc2528 1685
d62a17ae 1686 /*
1687 * Add the current set of stats into the cumulative statistics.
1688 */
1689 zfpm_stats_compose(&zfpm_g->cumulative_stats, &zfpm_g->stats,
1690 &zfpm_g->cumulative_stats);
5adc2528 1691
d62a17ae 1692 /*
1693 * Start collecting stats afresh over the next interval.
1694 */
1695 zfpm_stats_reset(&zfpm_g->stats);
5adc2528 1696
d62a17ae 1697 zfpm_start_stats_timer();
5adc2528 1698
d62a17ae 1699 return 0;
5adc2528
AS
1700}
1701
1702/*
1703 * zfpm_stop_stats_timer
1704 */
d62a17ae 1705static void zfpm_stop_stats_timer(void)
5adc2528 1706{
d62a17ae 1707 if (!zfpm_g->t_stats)
1708 return;
5adc2528 1709
d62a17ae 1710 zfpm_debug("Stopping existing stats timer");
1711 THREAD_TIMER_OFF(zfpm_g->t_stats);
5adc2528
AS
1712}
1713
1714/*
1715 * zfpm_start_stats_timer
1716 */
d62a17ae 1717void zfpm_start_stats_timer(void)
5adc2528 1718{
d62a17ae 1719 assert(!zfpm_g->t_stats);
5adc2528 1720
d62a17ae 1721 thread_add_timer(zfpm_g->master, zfpm_stats_timer_cb, 0,
1722 ZFPM_STATS_IVL_SECS, &zfpm_g->t_stats);
5adc2528
AS
1723}
1724
1725/*
1726 * Helper macro for zfpm_show_stats() below.
1727 */
d62a17ae 1728#define ZFPM_SHOW_STAT(counter) \
1729 do { \
1730 vty_out(vty, "%-40s %10lu %16lu\n", #counter, \
1731 total_stats.counter, zfpm_g->last_ivl_stats.counter); \
1732 } while (0)
5adc2528
AS
1733
1734/*
1735 * zfpm_show_stats
1736 */
d62a17ae 1737static void zfpm_show_stats(struct vty *vty)
5adc2528 1738{
d62a17ae 1739 zfpm_stats_t total_stats;
1740 time_t elapsed;
1741
1742 vty_out(vty, "\n%-40s %10s Last %2d secs\n\n", "Counter", "Total",
1743 ZFPM_STATS_IVL_SECS);
1744
1745 /*
1746 * Compute the total stats up to this instant.
1747 */
1748 zfpm_stats_compose(&zfpm_g->cumulative_stats, &zfpm_g->stats,
1749 &total_stats);
1750
1751 ZFPM_SHOW_STAT(connect_calls);
1752 ZFPM_SHOW_STAT(connect_no_sock);
1753 ZFPM_SHOW_STAT(read_cb_calls);
1754 ZFPM_SHOW_STAT(write_cb_calls);
1755 ZFPM_SHOW_STAT(write_calls);
1756 ZFPM_SHOW_STAT(partial_writes);
1757 ZFPM_SHOW_STAT(max_writes_hit);
1758 ZFPM_SHOW_STAT(t_write_yields);
1759 ZFPM_SHOW_STAT(nop_deletes_skipped);
1760 ZFPM_SHOW_STAT(route_adds);
1761 ZFPM_SHOW_STAT(route_dels);
1762 ZFPM_SHOW_STAT(updates_triggered);
d62a17ae 1763 ZFPM_SHOW_STAT(redundant_triggers);
1764 ZFPM_SHOW_STAT(dests_del_after_update);
1765 ZFPM_SHOW_STAT(t_conn_down_starts);
1766 ZFPM_SHOW_STAT(t_conn_down_dests_processed);
1767 ZFPM_SHOW_STAT(t_conn_down_yields);
1768 ZFPM_SHOW_STAT(t_conn_down_finishes);
1769 ZFPM_SHOW_STAT(t_conn_up_starts);
1770 ZFPM_SHOW_STAT(t_conn_up_dests_processed);
1771 ZFPM_SHOW_STAT(t_conn_up_yields);
1772 ZFPM_SHOW_STAT(t_conn_up_aborts);
1773 ZFPM_SHOW_STAT(t_conn_up_finishes);
1774
1775 if (!zfpm_g->last_stats_clear_time)
1776 return;
1777
1778 elapsed = zfpm_get_elapsed_time(zfpm_g->last_stats_clear_time);
1779
1780 vty_out(vty, "\nStats were cleared %lu seconds ago\n",
1781 (unsigned long)elapsed);
5adc2528
AS
1782}
1783
1784/*
1785 * zfpm_clear_stats
1786 */
d62a17ae 1787static void zfpm_clear_stats(struct vty *vty)
5adc2528 1788{
d62a17ae 1789 if (!zfpm_is_enabled()) {
1790 vty_out(vty, "The FPM module is not enabled...\n");
1791 return;
1792 }
5adc2528 1793
d62a17ae 1794 zfpm_stats_reset(&zfpm_g->stats);
1795 zfpm_stats_reset(&zfpm_g->last_ivl_stats);
1796 zfpm_stats_reset(&zfpm_g->cumulative_stats);
5adc2528 1797
d62a17ae 1798 zfpm_stop_stats_timer();
1799 zfpm_start_stats_timer();
5adc2528 1800
d62a17ae 1801 zfpm_g->last_stats_clear_time = monotime(NULL);
5adc2528 1802
d62a17ae 1803 vty_out(vty, "Cleared FPM stats\n");
5adc2528
AS
1804}
1805
1806/*
1807 * show_zebra_fpm_stats
1808 */
1809DEFUN (show_zebra_fpm_stats,
1810 show_zebra_fpm_stats_cmd,
1811 "show zebra fpm stats",
1812 SHOW_STR
41e7fb80 1813 ZEBRA_STR
5adc2528
AS
1814 "Forwarding Path Manager information\n"
1815 "Statistics\n")
1816{
d62a17ae 1817 zfpm_show_stats(vty);
1818 return CMD_SUCCESS;
5adc2528
AS
1819}
1820
1821/*
1822 * clear_zebra_fpm_stats
1823 */
1824DEFUN (clear_zebra_fpm_stats,
1825 clear_zebra_fpm_stats_cmd,
1826 "clear zebra fpm stats",
1827 CLEAR_STR
41e7fb80 1828 ZEBRA_STR
5adc2528
AS
1829 "Clear Forwarding Path Manager information\n"
1830 "Statistics\n")
1831{
d62a17ae 1832 zfpm_clear_stats(vty);
1833 return CMD_SUCCESS;
5adc2528
AS
1834}
1835
711ff0ba 1836/*
d62a17ae 1837 * update fpm connection information
711ff0ba 1838 */
e52702f2
QY
1839DEFUN ( fpm_remote_ip,
1840 fpm_remote_ip_cmd,
1841 "fpm connection ip A.B.C.D port (1-65535)",
711ff0ba
USK
1842 "fpm connection remote ip and port\n"
1843 "Remote fpm server ip A.B.C.D\n"
1844 "Enter ip ")
1845{
1846
d62a17ae 1847 in_addr_t fpm_server;
1848 uint32_t port_no;
711ff0ba 1849
d62a17ae 1850 fpm_server = inet_addr(argv[3]->arg);
1851 if (fpm_server == INADDR_NONE)
1852 return CMD_ERR_INCOMPLETE;
711ff0ba 1853
d62a17ae 1854 port_no = atoi(argv[5]->arg);
1855 if (port_no < TCP_MIN_PORT || port_no > TCP_MAX_PORT)
1856 return CMD_ERR_INCOMPLETE;
711ff0ba 1857
d62a17ae 1858 zfpm_g->fpm_server = fpm_server;
1859 zfpm_g->fpm_port = port_no;
711ff0ba
USK
1860
1861
d62a17ae 1862 return CMD_SUCCESS;
711ff0ba
USK
1863}
1864
e52702f2
QY
1865DEFUN ( no_fpm_remote_ip,
1866 no_fpm_remote_ip_cmd,
1867 "no fpm connection ip A.B.C.D port (1-65535)",
711ff0ba
USK
1868 "fpm connection remote ip and port\n"
1869 "Connection\n"
1870 "Remote fpm server ip A.B.C.D\n"
1871 "Enter ip ")
1872{
d62a17ae 1873 if (zfpm_g->fpm_server != inet_addr(argv[4]->arg)
1874 || zfpm_g->fpm_port != atoi(argv[6]->arg))
1875 return CMD_ERR_NO_MATCH;
711ff0ba 1876
d62a17ae 1877 zfpm_g->fpm_server = FPM_DEFAULT_IP;
1878 zfpm_g->fpm_port = FPM_DEFAULT_PORT;
711ff0ba 1879
d62a17ae 1880 return CMD_SUCCESS;
711ff0ba 1881}
711ff0ba 1882
fb0aa886
AS
1883/*
1884 * zfpm_init_message_format
1885 */
d62a17ae 1886static inline void zfpm_init_message_format(const char *format)
fb0aa886 1887{
d62a17ae 1888 int have_netlink, have_protobuf;
fb0aa886 1889
fb0aa886 1890#ifdef HAVE_NETLINK
d62a17ae 1891 have_netlink = 1;
4b2792b5 1892#else
d62a17ae 1893 have_netlink = 0;
fb0aa886
AS
1894#endif
1895
1896#ifdef HAVE_PROTOBUF
d62a17ae 1897 have_protobuf = 1;
4b2792b5 1898#else
d62a17ae 1899 have_protobuf = 0;
fb0aa886
AS
1900#endif
1901
d62a17ae 1902 zfpm_g->message_format = ZFPM_MSG_FORMAT_NONE;
fb0aa886 1903
d62a17ae 1904 if (!format) {
1905 if (have_netlink) {
1906 zfpm_g->message_format = ZFPM_MSG_FORMAT_NETLINK;
1907 } else if (have_protobuf) {
1908 zfpm_g->message_format = ZFPM_MSG_FORMAT_PROTOBUF;
1909 }
1910 return;
fb0aa886 1911 }
fb0aa886 1912
d62a17ae 1913 if (!strcmp("netlink", format)) {
1914 if (!have_netlink) {
1c50c1c0
QY
1915 flog_err(EC_ZEBRA_NETLINK_NOT_AVAILABLE,
1916 "FPM netlink message format is not available");
d62a17ae 1917 return;
1918 }
1919 zfpm_g->message_format = ZFPM_MSG_FORMAT_NETLINK;
1920 return;
fb0aa886 1921 }
fb0aa886 1922
d62a17ae 1923 if (!strcmp("protobuf", format)) {
1924 if (!have_protobuf) {
af4c2728 1925 flog_err(
e914ccbe 1926 EC_ZEBRA_PROTOBUF_NOT_AVAILABLE,
d62a17ae 1927 "FPM protobuf message format is not available");
1928 return;
1929 }
1930 zfpm_g->message_format = ZFPM_MSG_FORMAT_PROTOBUF;
1931 return;
fb0aa886 1932 }
fb0aa886 1933
e914ccbe 1934 flog_warn(EC_ZEBRA_FPM_FORMAT_UNKNOWN, "Unknown fpm format '%s'",
9df414fe 1935 format);
fb0aa886
AS
1936}
1937
711ff0ba 1938/**
d62a17ae 1939 * fpm_remote_srv_write
711ff0ba 1940 *
d62a17ae 1941 * Module to write remote fpm connection
711ff0ba
USK
1942 *
1943 * Returns ZERO on success.
1944 */
1945
d62a17ae 1946static int fpm_remote_srv_write(struct vty *vty)
711ff0ba 1947{
d62a17ae 1948 struct in_addr in;
711ff0ba 1949
d62a17ae 1950 in.s_addr = zfpm_g->fpm_server;
711ff0ba 1951
9d1c2659 1952 if ((zfpm_g->fpm_server != FPM_DEFAULT_IP
996c9314
LB
1953 && zfpm_g->fpm_server != INADDR_ANY)
1954 || (zfpm_g->fpm_port != FPM_DEFAULT_PORT && zfpm_g->fpm_port != 0))
d62a17ae 1955 vty_out(vty, "fpm connection ip %s port %d\n", inet_ntoa(in),
1956 zfpm_g->fpm_port);
711ff0ba 1957
d62a17ae 1958 return 0;
711ff0ba
USK
1959}
1960
1961
4f8ea50c 1962/* Zebra node */
d62a17ae 1963static struct cmd_node zebra_node = {ZEBRA_NODE, "", 1};
4f8ea50c
DL
1964
1965
5adc2528
AS
1966/**
1967 * zfpm_init
1968 *
1969 * One-time initialization of the Zebra FPM module.
1970 *
1971 * @param[in] port port at which FPM is running.
2951a7a4 1972 * @param[in] enable true if the zebra FPM module should be enabled
fb0aa886 1973 * @param[in] format to use to talk to the FPM. Can be 'netink' or 'protobuf'.
5adc2528 1974 *
2951a7a4 1975 * Returns true on success.
5adc2528 1976 */
d62a17ae 1977static int zfpm_init(struct thread_master *master)
5adc2528 1978{
d62a17ae 1979 int enable = 1;
1980 uint16_t port = 0;
1981 const char *format = THIS_MODULE->load_args;
5adc2528 1982
d62a17ae 1983 memset(zfpm_g, 0, sizeof(*zfpm_g));
1984 zfpm_g->master = master;
1985 TAILQ_INIT(&zfpm_g->dest_q);
e5218ec8
AD
1986 TAILQ_INIT(&zfpm_g->mac_q);
1987
1988 /* Create hash table for fpm_mac_info_t enties */
1989 zfpm_g->fpm_mac_info_table = hash_create(zfpm_mac_info_hash_keymake,
fbe748e5
AD
1990 zfpm_mac_info_cmp,
1991 "FPM MAC info hash table");
e5218ec8 1992
d62a17ae 1993 zfpm_g->sock = -1;
1994 zfpm_g->state = ZFPM_STATE_IDLE;
5adc2528 1995
d62a17ae 1996 zfpm_stats_init(&zfpm_g->stats);
1997 zfpm_stats_init(&zfpm_g->last_ivl_stats);
1998 zfpm_stats_init(&zfpm_g->cumulative_stats);
5adc2528 1999
d62a17ae 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);
5adc2528 2005
d62a17ae 2006 zfpm_init_message_format(format);
fb0aa886 2007
d62a17ae 2008 /*
2009 * Disable FPM interface if no suitable format is available.
2010 */
2011 if (zfpm_g->message_format == ZFPM_MSG_FORMAT_NONE)
2012 enable = 0;
fb0aa886 2013
d62a17ae 2014 zfpm_g->enabled = enable;
5adc2528 2015
d62a17ae 2016 if (!zfpm_g->fpm_server)
2017 zfpm_g->fpm_server = FPM_DEFAULT_IP;
711ff0ba 2018
d62a17ae 2019 if (!port)
2020 port = FPM_DEFAULT_PORT;
5adc2528 2021
d62a17ae 2022 zfpm_g->fpm_port = port;
5adc2528 2023
d62a17ae 2024 zfpm_g->obuf = stream_new(ZFPM_OBUF_SIZE);
2025 zfpm_g->ibuf = stream_new(ZFPM_IBUF_SIZE);
5adc2528 2026
d62a17ae 2027 zfpm_start_stats_timer();
2028 zfpm_start_connect_timer("initialized");
2029 return 0;
4f8ea50c 2030}
5adc2528 2031
d62a17ae 2032static int zebra_fpm_module_init(void)
4f8ea50c 2033{
d62a17ae 2034 hook_register(rib_update, zfpm_trigger_update);
a780a738 2035 hook_register(zebra_rmac_update, zfpm_trigger_rmac_update);
d62a17ae 2036 hook_register(frr_late_init, zfpm_init);
2037 return 0;
5adc2528 2038}
4f8ea50c 2039
d62a17ae 2040FRR_MODULE_SETUP(.name = "zebra_fpm", .version = FRR_VERSION,
2041 .description = "zebra FPM (Forwarding Plane Manager) module",
2042 .init = zebra_fpm_module_init, )