]>
Commit | Line | Data |
---|---|---|
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 | |
47 | DEFINE_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 | ||
7f5818fb | 73 | static void zfpm_iterate_rmac_table(struct hash_bucket *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 | 79 | typedef 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 | */ | |
87 | typedef 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 | */ | |
124 | typedef 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 | 155 | typedef 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 | 163 | typedef 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 | ||
279 | static zfpm_glob_t zfpm_glob_space; | |
280 | static zfpm_glob_t *zfpm_g = &zfpm_glob_space; | |
281 | ||
d62a17ae | 282 | static int zfpm_trigger_update(struct route_node *rn, const char *reason); |
4f8ea50c | 283 | |
d62a17ae | 284 | static int zfpm_read_cb(struct thread *thread); |
285 | static int zfpm_write_cb(struct thread *thread); | |
5adc2528 | 286 | |
d62a17ae | 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); | |
a780a738 | 290 | static void zfpm_mac_info_del(struct fpm_mac_info_t *fpm_mac); |
5adc2528 AS |
291 | |
292 | /* | |
293 | * zfpm_thread_should_yield | |
294 | */ | |
d62a17ae | 295 | static 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 | 303 | static 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 | 329 | static 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 | 346 | static 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 | 363 | static 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 | 392 | static 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 | 400 | static 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 | 411 | static 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 | 419 | static 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 | 427 | static 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 | 443 | static 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 | 464 | static 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 | 476 | static 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 | 488 | static 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 | 496 | static inline void zfpm_write_off(void) |
5adc2528 | 497 | { |
d62a17ae | 498 | THREAD_WRITE_OFF(zfpm_g->t_write); |
5adc2528 AS |
499 | } |
500 | ||
f0c459f0 DS |
501 | static inline void zfpm_connect_off(void) |
502 | { | |
503 | THREAD_TIMER_OFF(zfpm_g->t_connect); | |
504 | } | |
505 | ||
5adc2528 AS |
506 | /* |
507 | * zfpm_conn_up_thread_cb | |
508 | * | |
509 | * Callback for actions to be taken when the connection to the FPM | |
510 | * comes up. | |
511 | */ | |
d62a17ae | 512 | static int zfpm_conn_up_thread_cb(struct thread *thread) |
5adc2528 | 513 | { |
d62a17ae | 514 | struct route_node *rnode; |
515 | zfpm_rnodes_iter_t *iter; | |
516 | rib_dest_t *dest; | |
5adc2528 | 517 | |
d62a17ae | 518 | zfpm_g->t_conn_up = NULL; |
5adc2528 | 519 | |
d62a17ae | 520 | iter = &zfpm_g->t_conn_up_state.iter; |
5adc2528 | 521 | |
d62a17ae | 522 | if (zfpm_g->state != ZFPM_STATE_ESTABLISHED) { |
523 | zfpm_debug( | |
524 | "Connection not up anymore, conn_up thread aborting"); | |
525 | zfpm_g->stats.t_conn_up_aborts++; | |
526 | goto done; | |
527 | } | |
5adc2528 | 528 | |
fbe748e5 AD |
529 | /* Enqueue FPM updates for all the RMAC entries */ |
530 | hash_iterate(zrouter.l3vni_table, zfpm_iterate_rmac_table, NULL); | |
531 | ||
d62a17ae | 532 | while ((rnode = zfpm_rnodes_iter_next(iter))) { |
533 | dest = rib_dest_from_rnode(rnode); | |
534 | ||
535 | if (dest) { | |
536 | zfpm_g->stats.t_conn_up_dests_processed++; | |
537 | zfpm_trigger_update(rnode, NULL); | |
538 | } | |
539 | ||
540 | /* | |
541 | * Yield if need be. | |
542 | */ | |
543 | if (!zfpm_thread_should_yield(thread)) | |
544 | continue; | |
545 | ||
546 | zfpm_g->stats.t_conn_up_yields++; | |
547 | zfpm_rnodes_iter_pause(iter); | |
548 | zfpm_g->t_conn_up = NULL; | |
549 | thread_add_timer_msec(zfpm_g->master, zfpm_conn_up_thread_cb, | |
550 | NULL, 0, &zfpm_g->t_conn_up); | |
551 | return 0; | |
5adc2528 AS |
552 | } |
553 | ||
d62a17ae | 554 | zfpm_g->stats.t_conn_up_finishes++; |
555 | ||
556 | done: | |
557 | zfpm_rnodes_iter_cleanup(iter); | |
558 | return 0; | |
5adc2528 AS |
559 | } |
560 | ||
561 | /* | |
562 | * zfpm_connection_up | |
563 | * | |
564 | * Called when the connection to the FPM comes up. | |
565 | */ | |
d62a17ae | 566 | static void zfpm_connection_up(const char *detail) |
5adc2528 | 567 | { |
d62a17ae | 568 | assert(zfpm_g->sock >= 0); |
569 | zfpm_read_on(); | |
570 | zfpm_write_on(); | |
571 | zfpm_set_state(ZFPM_STATE_ESTABLISHED, detail); | |
572 | ||
573 | /* | |
574 | * Start thread to push existing routes to the FPM. | |
575 | */ | |
576 | assert(!zfpm_g->t_conn_up); | |
577 | ||
578 | zfpm_rnodes_iter_init(&zfpm_g->t_conn_up_state.iter); | |
579 | ||
580 | zfpm_debug("Starting conn_up thread"); | |
581 | zfpm_g->t_conn_up = NULL; | |
582 | thread_add_timer_msec(zfpm_g->master, zfpm_conn_up_thread_cb, NULL, 0, | |
583 | &zfpm_g->t_conn_up); | |
584 | zfpm_g->stats.t_conn_up_starts++; | |
5adc2528 AS |
585 | } |
586 | ||
587 | /* | |
588 | * zfpm_connect_check | |
589 | * | |
590 | * Check if an asynchronous connect() to the FPM is complete. | |
591 | */ | |
d62a17ae | 592 | static void zfpm_connect_check(void) |
5adc2528 | 593 | { |
d62a17ae | 594 | int status; |
595 | socklen_t slen; | |
596 | int ret; | |
597 | ||
598 | zfpm_read_off(); | |
599 | zfpm_write_off(); | |
600 | ||
601 | slen = sizeof(status); | |
602 | ret = getsockopt(zfpm_g->sock, SOL_SOCKET, SO_ERROR, (void *)&status, | |
603 | &slen); | |
604 | ||
605 | if (ret >= 0 && status == 0) { | |
606 | zfpm_connection_up("async connect complete"); | |
607 | return; | |
608 | } | |
609 | ||
610 | /* | |
611 | * getsockopt() failed or indicated an error on the socket. | |
612 | */ | |
613 | close(zfpm_g->sock); | |
614 | zfpm_g->sock = -1; | |
615 | ||
616 | zfpm_start_connect_timer("getsockopt() after async connect failed"); | |
617 | return; | |
5adc2528 AS |
618 | } |
619 | ||
620 | /* | |
621 | * zfpm_conn_down_thread_cb | |
622 | * | |
623 | * Callback that is invoked to clean up state after the TCP connection | |
624 | * to the FPM goes down. | |
625 | */ | |
d62a17ae | 626 | static int zfpm_conn_down_thread_cb(struct thread *thread) |
5adc2528 | 627 | { |
d62a17ae | 628 | struct route_node *rnode; |
629 | zfpm_rnodes_iter_t *iter; | |
630 | rib_dest_t *dest; | |
a780a738 | 631 | struct fpm_mac_info_t *mac = NULL; |
5adc2528 | 632 | |
d62a17ae | 633 | assert(zfpm_g->state == ZFPM_STATE_IDLE); |
5adc2528 | 634 | |
a780a738 AD |
635 | /* |
636 | * Delink and free all fpm_mac_info_t nodes | |
637 | * in the mac_q and fpm_mac_info_hash | |
638 | */ | |
639 | while ((mac = TAILQ_FIRST(&zfpm_g->mac_q)) != NULL) | |
640 | zfpm_mac_info_del(mac); | |
641 | ||
d62a17ae | 642 | zfpm_g->t_conn_down = NULL; |
5adc2528 | 643 | |
d62a17ae | 644 | iter = &zfpm_g->t_conn_down_state.iter; |
5adc2528 | 645 | |
d62a17ae | 646 | while ((rnode = zfpm_rnodes_iter_next(iter))) { |
647 | dest = rib_dest_from_rnode(rnode); | |
5adc2528 | 648 | |
d62a17ae | 649 | if (dest) { |
650 | if (CHECK_FLAG(dest->flags, RIB_DEST_UPDATE_FPM)) { | |
651 | TAILQ_REMOVE(&zfpm_g->dest_q, dest, | |
652 | fpm_q_entries); | |
653 | } | |
654 | ||
655 | UNSET_FLAG(dest->flags, RIB_DEST_UPDATE_FPM); | |
656 | UNSET_FLAG(dest->flags, RIB_DEST_SENT_TO_FPM); | |
5adc2528 | 657 | |
d62a17ae | 658 | zfpm_g->stats.t_conn_down_dests_processed++; |
5adc2528 | 659 | |
d62a17ae | 660 | /* |
661 | * Check if the dest should be deleted. | |
662 | */ | |
663 | rib_gc_dest(rnode); | |
664 | } | |
5adc2528 | 665 | |
d62a17ae | 666 | /* |
667 | * Yield if need be. | |
668 | */ | |
669 | if (!zfpm_thread_should_yield(thread)) | |
670 | continue; | |
671 | ||
672 | zfpm_g->stats.t_conn_down_yields++; | |
673 | zfpm_rnodes_iter_pause(iter); | |
674 | zfpm_g->t_conn_down = NULL; | |
675 | thread_add_timer_msec(zfpm_g->master, zfpm_conn_down_thread_cb, | |
676 | NULL, 0, &zfpm_g->t_conn_down); | |
677 | return 0; | |
5adc2528 AS |
678 | } |
679 | ||
d62a17ae | 680 | zfpm_g->stats.t_conn_down_finishes++; |
681 | zfpm_rnodes_iter_cleanup(iter); | |
682 | ||
683 | /* | |
684 | * Start the process of connecting to the FPM again. | |
685 | */ | |
686 | zfpm_start_connect_timer("cleanup complete"); | |
687 | return 0; | |
5adc2528 AS |
688 | } |
689 | ||
690 | /* | |
691 | * zfpm_connection_down | |
692 | * | |
693 | * Called when the connection to the FPM has gone down. | |
694 | */ | |
d62a17ae | 695 | static void zfpm_connection_down(const char *detail) |
5adc2528 | 696 | { |
d62a17ae | 697 | if (!detail) |
698 | detail = "unknown"; | |
5adc2528 | 699 | |
d62a17ae | 700 | assert(zfpm_g->state == ZFPM_STATE_ESTABLISHED); |
5adc2528 | 701 | |
d62a17ae | 702 | zlog_info("connection to the FPM has gone down: %s", detail); |
5adc2528 | 703 | |
d62a17ae | 704 | zfpm_read_off(); |
705 | zfpm_write_off(); | |
5adc2528 | 706 | |
d62a17ae | 707 | stream_reset(zfpm_g->ibuf); |
708 | stream_reset(zfpm_g->obuf); | |
5adc2528 | 709 | |
d62a17ae | 710 | if (zfpm_g->sock >= 0) { |
711 | close(zfpm_g->sock); | |
712 | zfpm_g->sock = -1; | |
713 | } | |
5adc2528 | 714 | |
d62a17ae | 715 | /* |
716 | * Start thread to clean up state after the connection goes down. | |
717 | */ | |
718 | assert(!zfpm_g->t_conn_down); | |
d62a17ae | 719 | zfpm_rnodes_iter_init(&zfpm_g->t_conn_down_state.iter); |
720 | zfpm_g->t_conn_down = NULL; | |
721 | thread_add_timer_msec(zfpm_g->master, zfpm_conn_down_thread_cb, NULL, 0, | |
722 | &zfpm_g->t_conn_down); | |
723 | zfpm_g->stats.t_conn_down_starts++; | |
724 | ||
725 | zfpm_set_state(ZFPM_STATE_IDLE, detail); | |
5adc2528 AS |
726 | } |
727 | ||
728 | /* | |
729 | * zfpm_read_cb | |
730 | */ | |
d62a17ae | 731 | static int zfpm_read_cb(struct thread *thread) |
5adc2528 | 732 | { |
d62a17ae | 733 | size_t already; |
734 | struct stream *ibuf; | |
735 | uint16_t msg_len; | |
736 | fpm_msg_hdr_t *hdr; | |
737 | ||
738 | zfpm_g->stats.read_cb_calls++; | |
d62a17ae | 739 | |
740 | /* | |
741 | * Check if async connect is now done. | |
742 | */ | |
743 | if (zfpm_g->state == ZFPM_STATE_CONNECTING) { | |
744 | zfpm_connect_check(); | |
745 | return 0; | |
5adc2528 AS |
746 | } |
747 | ||
d62a17ae | 748 | assert(zfpm_g->state == ZFPM_STATE_ESTABLISHED); |
749 | assert(zfpm_g->sock >= 0); | |
5adc2528 | 750 | |
d62a17ae | 751 | ibuf = zfpm_g->ibuf; |
5adc2528 | 752 | |
d62a17ae | 753 | already = stream_get_endp(ibuf); |
754 | if (already < FPM_MSG_HDR_LEN) { | |
755 | ssize_t nbyte; | |
5adc2528 | 756 | |
d62a17ae | 757 | nbyte = stream_read_try(ibuf, zfpm_g->sock, |
758 | FPM_MSG_HDR_LEN - already); | |
759 | if (nbyte == 0 || nbyte == -1) { | |
677f704d DS |
760 | if (nbyte == -1) { |
761 | char buffer[1024]; | |
762 | ||
763 | sprintf(buffer, "closed socket in read(%d): %s", | |
764 | errno, safe_strerror(errno)); | |
765 | zfpm_connection_down(buffer); | |
996c9314 | 766 | } else |
677f704d | 767 | zfpm_connection_down("closed socket in read"); |
d62a17ae | 768 | return 0; |
769 | } | |
5adc2528 | 770 | |
d62a17ae | 771 | if (nbyte != (ssize_t)(FPM_MSG_HDR_LEN - already)) |
772 | goto done; | |
5adc2528 | 773 | |
d62a17ae | 774 | already = FPM_MSG_HDR_LEN; |
775 | } | |
5adc2528 | 776 | |
d62a17ae | 777 | stream_set_getp(ibuf, 0); |
5adc2528 | 778 | |
d62a17ae | 779 | hdr = (fpm_msg_hdr_t *)stream_pnt(ibuf); |
5adc2528 | 780 | |
d62a17ae | 781 | if (!fpm_msg_hdr_ok(hdr)) { |
782 | zfpm_connection_down("invalid message header"); | |
783 | return 0; | |
5adc2528 AS |
784 | } |
785 | ||
d62a17ae | 786 | msg_len = fpm_msg_len(hdr); |
5adc2528 | 787 | |
d62a17ae | 788 | /* |
789 | * Read out the rest of the packet. | |
790 | */ | |
791 | if (already < msg_len) { | |
792 | ssize_t nbyte; | |
5adc2528 | 793 | |
d62a17ae | 794 | nbyte = stream_read_try(ibuf, zfpm_g->sock, msg_len - already); |
5adc2528 | 795 | |
d62a17ae | 796 | if (nbyte == 0 || nbyte == -1) { |
677f704d DS |
797 | if (nbyte == -1) { |
798 | char buffer[1024]; | |
799 | ||
800 | sprintf(buffer, "failed to read message(%d) %s", | |
801 | errno, safe_strerror(errno)); | |
802 | zfpm_connection_down(buffer); | |
996c9314 | 803 | } else |
677f704d | 804 | zfpm_connection_down("failed to read message"); |
d62a17ae | 805 | return 0; |
806 | } | |
807 | ||
808 | if (nbyte != (ssize_t)(msg_len - already)) | |
809 | goto done; | |
810 | } | |
811 | ||
d62a17ae | 812 | /* |
813 | * Just throw it away for now. | |
814 | */ | |
815 | stream_reset(ibuf); | |
816 | ||
817 | done: | |
818 | zfpm_read_on(); | |
819 | return 0; | |
5adc2528 AS |
820 | } |
821 | ||
21d814eb AD |
822 | static bool zfpm_updates_pending(void) |
823 | { | |
824 | if (!(TAILQ_EMPTY(&zfpm_g->dest_q)) || !(TAILQ_EMPTY(&zfpm_g->mac_q))) | |
825 | return true; | |
826 | ||
827 | return false; | |
828 | } | |
829 | ||
5adc2528 AS |
830 | /* |
831 | * zfpm_writes_pending | |
832 | * | |
2951a7a4 | 833 | * Returns true if we may have something to write to the FPM. |
5adc2528 | 834 | */ |
d62a17ae | 835 | static int zfpm_writes_pending(void) |
5adc2528 AS |
836 | { |
837 | ||
d62a17ae | 838 | /* |
839 | * Check if there is any data in the outbound buffer that has not | |
840 | * been written to the socket yet. | |
841 | */ | |
842 | if (stream_get_endp(zfpm_g->obuf) - stream_get_getp(zfpm_g->obuf)) | |
843 | return 1; | |
5adc2528 | 844 | |
d62a17ae | 845 | /* |
21d814eb | 846 | * Check if there are any updates scheduled on the outbound queues. |
d62a17ae | 847 | */ |
21d814eb | 848 | if (zfpm_updates_pending()) |
d62a17ae | 849 | return 1; |
5adc2528 | 850 | |
d62a17ae | 851 | return 0; |
5adc2528 AS |
852 | } |
853 | ||
854 | /* | |
855 | * zfpm_encode_route | |
856 | * | |
857 | * Encode a message to the FPM with information about the given route. | |
858 | * | |
859 | * Returns the number of bytes written to the buffer. 0 or a negative | |
860 | * value indicates an error. | |
861 | */ | |
d62a17ae | 862 | static inline int zfpm_encode_route(rib_dest_t *dest, struct route_entry *re, |
863 | char *in_buf, size_t in_buf_len, | |
864 | fpm_msg_type_e *msg_type) | |
5adc2528 | 865 | { |
d62a17ae | 866 | size_t len; |
9bf75362 | 867 | #ifdef HAVE_NETLINK |
d62a17ae | 868 | int cmd; |
9bf75362 | 869 | #endif |
d62a17ae | 870 | len = 0; |
5adc2528 | 871 | |
d62a17ae | 872 | *msg_type = FPM_MSG_TYPE_NONE; |
5adc2528 | 873 | |
d62a17ae | 874 | switch (zfpm_g->message_format) { |
5adc2528 | 875 | |
d62a17ae | 876 | case ZFPM_MSG_FORMAT_PROTOBUF: |
fb0aa886 | 877 | #ifdef HAVE_PROTOBUF |
d62a17ae | 878 | len = zfpm_protobuf_encode_route(dest, re, (uint8_t *)in_buf, |
879 | in_buf_len); | |
880 | *msg_type = FPM_MSG_TYPE_PROTOBUF; | |
fb0aa886 | 881 | #endif |
d62a17ae | 882 | break; |
5adc2528 | 883 | |
d62a17ae | 884 | case ZFPM_MSG_FORMAT_NETLINK: |
fb0aa886 | 885 | #ifdef HAVE_NETLINK |
d62a17ae | 886 | *msg_type = FPM_MSG_TYPE_NETLINK; |
887 | cmd = re ? RTM_NEWROUTE : RTM_DELROUTE; | |
888 | len = zfpm_netlink_encode_route(cmd, dest, re, in_buf, | |
889 | in_buf_len); | |
890 | assert(fpm_msg_align(len) == len); | |
891 | *msg_type = FPM_MSG_TYPE_NETLINK; | |
5adc2528 | 892 | #endif /* HAVE_NETLINK */ |
d62a17ae | 893 | break; |
fb0aa886 | 894 | |
d62a17ae | 895 | default: |
896 | break; | |
897 | } | |
fb0aa886 | 898 | |
d62a17ae | 899 | return len; |
5adc2528 AS |
900 | } |
901 | ||
902 | /* | |
903 | * zfpm_route_for_update | |
904 | * | |
f0f77c9a | 905 | * Returns the re that is to be sent to the FPM for a given dest. |
5adc2528 | 906 | */ |
d62a17ae | 907 | struct route_entry *zfpm_route_for_update(rib_dest_t *dest) |
5adc2528 | 908 | { |
5f7a4718 | 909 | return dest->selected_fib; |
5adc2528 AS |
910 | } |
911 | ||
912 | /* | |
21d814eb | 913 | * Define an enum for return codes for queue processing functions |
5adc2528 | 914 | * |
21d814eb AD |
915 | * FPM_WRITE_STOP: This return code indicates that the write buffer is full. |
916 | * Stop processing all the queues and empty the buffer by writing its content | |
917 | * to the socket. | |
918 | * | |
919 | * FPM_GOTO_NEXT_Q: This return code indicates that either this queue is | |
920 | * empty or we have processed enough updates from this queue. | |
921 | * So, move on to the next queue. | |
5adc2528 | 922 | */ |
21d814eb AD |
923 | enum { |
924 | FPM_WRITE_STOP = 0, | |
925 | FPM_GOTO_NEXT_Q = 1 | |
926 | }; | |
927 | ||
928 | #define FPM_QUEUE_PROCESS_LIMIT 10000 | |
929 | ||
930 | /* | |
931 | * zfpm_build_route_updates | |
932 | * | |
933 | * Process the dest_q queue and write FPM messages to the outbound buffer. | |
934 | */ | |
935 | static int zfpm_build_route_updates(void) | |
5adc2528 | 936 | { |
d62a17ae | 937 | struct stream *s; |
938 | rib_dest_t *dest; | |
939 | unsigned char *buf, *data, *buf_end; | |
940 | size_t msg_len; | |
941 | size_t data_len; | |
942 | fpm_msg_hdr_t *hdr; | |
943 | struct route_entry *re; | |
944 | int is_add, write_msg; | |
945 | fpm_msg_type_e msg_type; | |
21d814eb | 946 | uint16_t q_limit; |
d62a17ae | 947 | |
21d814eb AD |
948 | if (TAILQ_EMPTY(&zfpm_g->dest_q)) |
949 | return FPM_GOTO_NEXT_Q; | |
d62a17ae | 950 | |
21d814eb AD |
951 | s = zfpm_g->obuf; |
952 | q_limit = FPM_QUEUE_PROCESS_LIMIT; | |
d62a17ae | 953 | |
21d814eb | 954 | do { |
d62a17ae | 955 | /* |
956 | * Make sure there is enough space to write another message. | |
957 | */ | |
958 | if (STREAM_WRITEABLE(s) < FPM_MAX_MSG_LEN) | |
21d814eb | 959 | return FPM_WRITE_STOP; |
d62a17ae | 960 | |
961 | buf = STREAM_DATA(s) + stream_get_endp(s); | |
962 | buf_end = buf + STREAM_WRITEABLE(s); | |
963 | ||
964 | dest = TAILQ_FIRST(&zfpm_g->dest_q); | |
965 | if (!dest) | |
21d814eb | 966 | return FPM_GOTO_NEXT_Q; |
d62a17ae | 967 | |
968 | assert(CHECK_FLAG(dest->flags, RIB_DEST_UPDATE_FPM)); | |
969 | ||
970 | hdr = (fpm_msg_hdr_t *)buf; | |
971 | hdr->version = FPM_PROTO_VERSION; | |
972 | ||
973 | data = fpm_msg_data(hdr); | |
974 | ||
975 | re = zfpm_route_for_update(dest); | |
976 | is_add = re ? 1 : 0; | |
977 | ||
978 | write_msg = 1; | |
979 | ||
980 | /* | |
981 | * If this is a route deletion, and we have not sent the route | |
982 | * to | |
983 | * the FPM previously, skip it. | |
984 | */ | |
985 | if (!is_add && !CHECK_FLAG(dest->flags, RIB_DEST_SENT_TO_FPM)) { | |
986 | write_msg = 0; | |
987 | zfpm_g->stats.nop_deletes_skipped++; | |
988 | } | |
989 | ||
990 | if (write_msg) { | |
991 | data_len = zfpm_encode_route(dest, re, (char *)data, | |
992 | buf_end - data, &msg_type); | |
993 | ||
994 | assert(data_len); | |
995 | if (data_len) { | |
996 | hdr->msg_type = msg_type; | |
997 | msg_len = fpm_data_len_to_msg_len(data_len); | |
998 | hdr->msg_len = htons(msg_len); | |
999 | stream_forward_endp(s, msg_len); | |
1000 | ||
1001 | if (is_add) | |
1002 | zfpm_g->stats.route_adds++; | |
1003 | else | |
1004 | zfpm_g->stats.route_dels++; | |
1005 | } | |
1006 | } | |
1007 | ||
1008 | /* | |
1009 | * Remove the dest from the queue, and reset the flag. | |
1010 | */ | |
1011 | UNSET_FLAG(dest->flags, RIB_DEST_UPDATE_FPM); | |
1012 | TAILQ_REMOVE(&zfpm_g->dest_q, dest, fpm_q_entries); | |
1013 | ||
1014 | if (is_add) { | |
1015 | SET_FLAG(dest->flags, RIB_DEST_SENT_TO_FPM); | |
1016 | } else { | |
1017 | UNSET_FLAG(dest->flags, RIB_DEST_SENT_TO_FPM); | |
1018 | } | |
1019 | ||
1020 | /* | |
1021 | * Delete the destination if necessary. | |
1022 | */ | |
1023 | if (rib_gc_dest(dest->rnode)) | |
1024 | zfpm_g->stats.dests_del_after_update++; | |
1025 | ||
21d814eb AD |
1026 | q_limit--; |
1027 | if (q_limit == 0) { | |
1028 | /* | |
1029 | * We have processed enough updates in this queue. | |
1030 | * Now yield for other queues. | |
1031 | */ | |
1032 | return FPM_GOTO_NEXT_Q; | |
1033 | } | |
c5431822 | 1034 | } while (true); |
21d814eb AD |
1035 | } |
1036 | ||
1037 | /* | |
1038 | * zfpm_encode_mac | |
1039 | * | |
1040 | * Encode a message to FPM with information about the given MAC. | |
1041 | * | |
1042 | * Returns the number of bytes written to the buffer. | |
1043 | */ | |
1044 | static inline int zfpm_encode_mac(struct fpm_mac_info_t *mac, char *in_buf, | |
1045 | size_t in_buf_len, fpm_msg_type_e *msg_type) | |
1046 | { | |
1047 | size_t len = 0; | |
1048 | ||
1049 | *msg_type = FPM_MSG_TYPE_NONE; | |
1050 | ||
1051 | switch (zfpm_g->message_format) { | |
1052 | ||
1053 | case ZFPM_MSG_FORMAT_NONE: | |
1054 | break; | |
1055 | case ZFPM_MSG_FORMAT_NETLINK: | |
9da60d0a AD |
1056 | #ifdef HAVE_NETLINK |
1057 | len = zfpm_netlink_encode_mac(mac, in_buf, in_buf_len); | |
1058 | assert(fpm_msg_align(len) == len); | |
1059 | *msg_type = FPM_MSG_TYPE_NETLINK; | |
1060 | #endif /* HAVE_NETLINK */ | |
21d814eb AD |
1061 | break; |
1062 | case ZFPM_MSG_FORMAT_PROTOBUF: | |
1063 | break; | |
1064 | } | |
1065 | return len; | |
1066 | } | |
1067 | ||
1068 | static int zfpm_build_mac_updates(void) | |
1069 | { | |
1070 | struct stream *s; | |
1071 | struct fpm_mac_info_t *mac; | |
1072 | unsigned char *buf, *data, *buf_end; | |
1073 | fpm_msg_hdr_t *hdr; | |
1074 | size_t data_len, msg_len; | |
1075 | fpm_msg_type_e msg_type; | |
1076 | uint16_t q_limit; | |
1077 | ||
1078 | if (TAILQ_EMPTY(&zfpm_g->mac_q)) | |
1079 | return FPM_GOTO_NEXT_Q; | |
1080 | ||
1081 | s = zfpm_g->obuf; | |
1082 | q_limit = FPM_QUEUE_PROCESS_LIMIT; | |
1083 | ||
1084 | do { | |
1085 | /* Make sure there is enough space to write another message. */ | |
1086 | if (STREAM_WRITEABLE(s) < FPM_MAX_MAC_MSG_LEN) | |
1087 | return FPM_WRITE_STOP; | |
1088 | ||
1089 | buf = STREAM_DATA(s) + stream_get_endp(s); | |
1090 | buf_end = buf + STREAM_WRITEABLE(s); | |
1091 | ||
1092 | mac = TAILQ_FIRST(&zfpm_g->mac_q); | |
1093 | if (!mac) | |
1094 | return FPM_GOTO_NEXT_Q; | |
1095 | ||
1096 | /* Check for no-op */ | |
1097 | if (!CHECK_FLAG(mac->fpm_flags, ZEBRA_MAC_UPDATE_FPM)) { | |
1098 | zfpm_g->stats.nop_deletes_skipped++; | |
1099 | zfpm_mac_info_del(mac); | |
1100 | continue; | |
1101 | } | |
1102 | ||
1103 | hdr = (fpm_msg_hdr_t *)buf; | |
1104 | hdr->version = FPM_PROTO_VERSION; | |
1105 | ||
1106 | data = fpm_msg_data(hdr); | |
1107 | data_len = zfpm_encode_mac(mac, (char *)data, buf_end - data, | |
1108 | &msg_type); | |
9da60d0a | 1109 | assert(data_len); |
21d814eb AD |
1110 | |
1111 | hdr->msg_type = msg_type; | |
1112 | msg_len = fpm_data_len_to_msg_len(data_len); | |
1113 | hdr->msg_len = htons(msg_len); | |
1114 | stream_forward_endp(s, msg_len); | |
1115 | ||
1116 | /* Remove the MAC from the queue, and delete it. */ | |
1117 | zfpm_mac_info_del(mac); | |
1118 | ||
1119 | q_limit--; | |
1120 | if (q_limit == 0) { | |
1121 | /* | |
1122 | * We have processed enough updates in this queue. | |
1123 | * Now yield for other queues. | |
1124 | */ | |
1125 | return FPM_GOTO_NEXT_Q; | |
1126 | } | |
d62a17ae | 1127 | } while (1); |
5adc2528 AS |
1128 | } |
1129 | ||
21d814eb AD |
1130 | /* |
1131 | * zfpm_build_updates | |
1132 | * | |
1133 | * Process the outgoing queues and write messages to the outbound | |
1134 | * buffer. | |
1135 | */ | |
1136 | static void zfpm_build_updates(void) | |
1137 | { | |
1138 | struct stream *s; | |
1139 | ||
1140 | s = zfpm_g->obuf; | |
1141 | assert(stream_empty(s)); | |
1142 | ||
1143 | do { | |
1144 | /* | |
1145 | * Stop processing the queues if zfpm_g->obuf is full | |
1146 | * or we do not have more updates to process | |
1147 | */ | |
1148 | if (zfpm_build_mac_updates() == FPM_WRITE_STOP) | |
1149 | break; | |
1150 | if (zfpm_build_route_updates() == FPM_WRITE_STOP) | |
1151 | break; | |
1152 | } while (zfpm_updates_pending()); | |
1153 | } | |
1154 | ||
5adc2528 AS |
1155 | /* |
1156 | * zfpm_write_cb | |
1157 | */ | |
d62a17ae | 1158 | static int zfpm_write_cb(struct thread *thread) |
5adc2528 | 1159 | { |
d62a17ae | 1160 | struct stream *s; |
1161 | int num_writes; | |
1162 | ||
1163 | zfpm_g->stats.write_cb_calls++; | |
d62a17ae | 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 | 1242 | static int zfpm_connect_cb(struct thread *t) |
5adc2528 | 1243 | { |
d62a17ae | 1244 | int sock, ret; |
1245 | struct sockaddr_in serv; | |
1246 | ||
d62a17ae | 1247 | assert(zfpm_g->state == ZFPM_STATE_ACTIVE); |
1248 | ||
1249 | sock = socket(AF_INET, SOCK_STREAM, 0); | |
1250 | if (sock < 0) { | |
14a4d9d0 | 1251 | zlog_err("Failed to create socket for connect(): %s", |
d62a17ae | 1252 | strerror(errno)); |
1253 | zfpm_g->stats.connect_no_sock++; | |
1254 | return 0; | |
1255 | } | |
1256 | ||
1257 | set_nonblocking(sock); | |
1258 | ||
1259 | /* Make server socket. */ | |
1260 | memset(&serv, 0, sizeof(serv)); | |
1261 | serv.sin_family = AF_INET; | |
1262 | serv.sin_port = htons(zfpm_g->fpm_port); | |
5adc2528 | 1263 | #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN |
d62a17ae | 1264 | serv.sin_len = sizeof(struct sockaddr_in); |
5adc2528 | 1265 | #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */ |
d62a17ae | 1266 | if (!zfpm_g->fpm_server) |
1267 | serv.sin_addr.s_addr = htonl(INADDR_LOOPBACK); | |
1268 | else | |
1269 | serv.sin_addr.s_addr = (zfpm_g->fpm_server); | |
1270 | ||
1271 | /* | |
1272 | * Connect to the FPM. | |
1273 | */ | |
1274 | zfpm_g->connect_calls++; | |
1275 | zfpm_g->stats.connect_calls++; | |
1276 | zfpm_g->last_connect_call_time = monotime(NULL); | |
1277 | ||
1278 | ret = connect(sock, (struct sockaddr *)&serv, sizeof(serv)); | |
1279 | if (ret >= 0) { | |
1280 | zfpm_g->sock = sock; | |
1281 | zfpm_connection_up("connect succeeded"); | |
1282 | return 1; | |
1283 | } | |
1284 | ||
1285 | if (errno == EINPROGRESS) { | |
1286 | zfpm_g->sock = sock; | |
1287 | zfpm_read_on(); | |
1288 | zfpm_write_on(); | |
1289 | zfpm_set_state(ZFPM_STATE_CONNECTING, | |
1290 | "async connect in progress"); | |
1291 | return 0; | |
1292 | } | |
1293 | ||
1294 | zlog_info("can't connect to FPM %d: %s", sock, safe_strerror(errno)); | |
1295 | close(sock); | |
1296 | ||
1297 | /* | |
1298 | * Restart timer for retrying connection. | |
1299 | */ | |
1300 | zfpm_start_connect_timer("connect() failed"); | |
1301 | return 0; | |
5adc2528 AS |
1302 | } |
1303 | ||
1304 | /* | |
1305 | * zfpm_set_state | |
1306 | * | |
1307 | * Move state machine into the given state. | |
1308 | */ | |
d62a17ae | 1309 | static void zfpm_set_state(zfpm_state_t state, const char *reason) |
5adc2528 | 1310 | { |
d62a17ae | 1311 | zfpm_state_t cur_state = zfpm_g->state; |
1312 | ||
1313 | if (!reason) | |
1314 | reason = "Unknown"; | |
1315 | ||
1316 | if (state == cur_state) | |
1317 | return; | |
1318 | ||
1319 | zfpm_debug("beginning state transition %s -> %s. Reason: %s", | |
1320 | zfpm_state_to_str(cur_state), zfpm_state_to_str(state), | |
1321 | reason); | |
1322 | ||
1323 | switch (state) { | |
1324 | ||
1325 | case ZFPM_STATE_IDLE: | |
1326 | assert(cur_state == ZFPM_STATE_ESTABLISHED); | |
1327 | break; | |
1328 | ||
1329 | case ZFPM_STATE_ACTIVE: | |
1330 | assert(cur_state == ZFPM_STATE_IDLE | |
1331 | || cur_state == ZFPM_STATE_CONNECTING); | |
1332 | assert(zfpm_g->t_connect); | |
1333 | break; | |
1334 | ||
1335 | case ZFPM_STATE_CONNECTING: | |
1336 | assert(zfpm_g->sock); | |
1337 | assert(cur_state == ZFPM_STATE_ACTIVE); | |
1338 | assert(zfpm_g->t_read); | |
1339 | assert(zfpm_g->t_write); | |
1340 | break; | |
1341 | ||
1342 | case ZFPM_STATE_ESTABLISHED: | |
1343 | assert(cur_state == ZFPM_STATE_ACTIVE | |
1344 | || cur_state == ZFPM_STATE_CONNECTING); | |
1345 | assert(zfpm_g->sock); | |
1346 | assert(zfpm_g->t_read); | |
1347 | assert(zfpm_g->t_write); | |
1348 | break; | |
1349 | } | |
1350 | ||
1351 | zfpm_g->state = state; | |
5adc2528 AS |
1352 | } |
1353 | ||
1354 | /* | |
1355 | * zfpm_calc_connect_delay | |
1356 | * | |
1357 | * Returns the number of seconds after which we should attempt to | |
1358 | * reconnect to the FPM. | |
1359 | */ | |
d62a17ae | 1360 | static long zfpm_calc_connect_delay(void) |
5adc2528 | 1361 | { |
d62a17ae | 1362 | time_t elapsed; |
5adc2528 | 1363 | |
d62a17ae | 1364 | /* |
1365 | * Return 0 if this is our first attempt to connect. | |
1366 | */ | |
1367 | if (zfpm_g->connect_calls == 0) { | |
1368 | return 0; | |
1369 | } | |
5adc2528 | 1370 | |
d62a17ae | 1371 | elapsed = zfpm_get_elapsed_time(zfpm_g->last_connect_call_time); |
5adc2528 | 1372 | |
d62a17ae | 1373 | if (elapsed > ZFPM_CONNECT_RETRY_IVL) { |
1374 | return 0; | |
1375 | } | |
5adc2528 | 1376 | |
d62a17ae | 1377 | return ZFPM_CONNECT_RETRY_IVL - elapsed; |
5adc2528 AS |
1378 | } |
1379 | ||
1380 | /* | |
1381 | * zfpm_start_connect_timer | |
1382 | */ | |
d62a17ae | 1383 | static void zfpm_start_connect_timer(const char *reason) |
5adc2528 | 1384 | { |
d62a17ae | 1385 | long delay_secs; |
5adc2528 | 1386 | |
d62a17ae | 1387 | assert(!zfpm_g->t_connect); |
1388 | assert(zfpm_g->sock < 0); | |
5adc2528 | 1389 | |
d62a17ae | 1390 | assert(zfpm_g->state == ZFPM_STATE_IDLE |
1391 | || zfpm_g->state == ZFPM_STATE_ACTIVE | |
1392 | || zfpm_g->state == ZFPM_STATE_CONNECTING); | |
5adc2528 | 1393 | |
d62a17ae | 1394 | delay_secs = zfpm_calc_connect_delay(); |
1395 | zfpm_debug("scheduling connect in %ld seconds", delay_secs); | |
5adc2528 | 1396 | |
d62a17ae | 1397 | thread_add_timer(zfpm_g->master, zfpm_connect_cb, 0, delay_secs, |
1398 | &zfpm_g->t_connect); | |
1399 | zfpm_set_state(ZFPM_STATE_ACTIVE, reason); | |
5adc2528 AS |
1400 | } |
1401 | ||
1402 | /* | |
1403 | * zfpm_is_enabled | |
1404 | * | |
2951a7a4 | 1405 | * Returns true if the zebra FPM module has been enabled. |
5adc2528 | 1406 | */ |
d62a17ae | 1407 | static inline int zfpm_is_enabled(void) |
5adc2528 | 1408 | { |
d62a17ae | 1409 | return zfpm_g->enabled; |
5adc2528 AS |
1410 | } |
1411 | ||
1412 | /* | |
1413 | * zfpm_conn_is_up | |
1414 | * | |
2951a7a4 | 1415 | * Returns true if the connection to the FPM is up. |
5adc2528 | 1416 | */ |
d62a17ae | 1417 | static inline int zfpm_conn_is_up(void) |
5adc2528 | 1418 | { |
d62a17ae | 1419 | if (zfpm_g->state != ZFPM_STATE_ESTABLISHED) |
1420 | return 0; | |
5adc2528 | 1421 | |
d62a17ae | 1422 | assert(zfpm_g->sock >= 0); |
5adc2528 | 1423 | |
d62a17ae | 1424 | return 1; |
5adc2528 AS |
1425 | } |
1426 | ||
1427 | /* | |
1428 | * zfpm_trigger_update | |
1429 | * | |
1430 | * The zebra code invokes this function to indicate that we should | |
1431 | * send an update to the FPM about the given route_node. | |
1432 | */ | |
d62a17ae | 1433 | static int zfpm_trigger_update(struct route_node *rn, const char *reason) |
5adc2528 | 1434 | { |
d62a17ae | 1435 | rib_dest_t *dest; |
1436 | char buf[PREFIX_STRLEN]; | |
1437 | ||
1438 | /* | |
1439 | * Ignore if the connection is down. We will update the FPM about | |
1440 | * all destinations once the connection comes up. | |
1441 | */ | |
1442 | if (!zfpm_conn_is_up()) | |
1443 | return 0; | |
1444 | ||
1445 | dest = rib_dest_from_rnode(rn); | |
1446 | ||
d62a17ae | 1447 | if (CHECK_FLAG(dest->flags, RIB_DEST_UPDATE_FPM)) { |
1448 | zfpm_g->stats.redundant_triggers++; | |
1449 | return 0; | |
1450 | } | |
1451 | ||
1452 | if (reason) { | |
1453 | zfpm_debug("%s triggering update to FPM - Reason: %s", | |
1454 | prefix2str(&rn->p, buf, sizeof(buf)), reason); | |
1455 | } | |
1456 | ||
1457 | SET_FLAG(dest->flags, RIB_DEST_UPDATE_FPM); | |
1458 | TAILQ_INSERT_TAIL(&zfpm_g->dest_q, dest, fpm_q_entries); | |
1459 | zfpm_g->stats.updates_triggered++; | |
1460 | ||
1461 | /* | |
1462 | * Make sure that writes are enabled. | |
1463 | */ | |
1464 | if (zfpm_g->t_write) | |
1465 | return 0; | |
1466 | ||
1467 | zfpm_write_on(); | |
1468 | return 0; | |
5adc2528 AS |
1469 | } |
1470 | ||
e5218ec8 AD |
1471 | /* |
1472 | * Generate Key for FPM MAC info hash entry | |
e5218ec8 AD |
1473 | */ |
1474 | static unsigned int zfpm_mac_info_hash_keymake(const void *p) | |
1475 | { | |
1476 | struct fpm_mac_info_t *fpm_mac = (struct fpm_mac_info_t *)p; | |
1477 | uint32_t mac_key; | |
1478 | ||
1479 | mac_key = jhash(fpm_mac->macaddr.octet, ETH_ALEN, 0xa5a5a55a); | |
1480 | ||
1481 | return jhash_2words(mac_key, fpm_mac->vni, 0); | |
1482 | } | |
1483 | ||
1484 | /* | |
1485 | * Compare function for FPM MAC info hash lookup | |
1486 | */ | |
1487 | static bool zfpm_mac_info_cmp(const void *p1, const void *p2) | |
1488 | { | |
1489 | const struct fpm_mac_info_t *fpm_mac1 = p1; | |
1490 | const struct fpm_mac_info_t *fpm_mac2 = p2; | |
1491 | ||
1492 | if (memcmp(fpm_mac1->macaddr.octet, fpm_mac2->macaddr.octet, ETH_ALEN) | |
1493 | != 0) | |
1494 | return false; | |
e5218ec8 AD |
1495 | if (fpm_mac1->vni != fpm_mac2->vni) |
1496 | return false; | |
1497 | ||
1498 | return true; | |
1499 | } | |
1500 | ||
1501 | /* | |
1502 | * Lookup FPM MAC info hash entry. | |
1503 | */ | |
1504 | static struct fpm_mac_info_t *zfpm_mac_info_lookup(struct fpm_mac_info_t *key) | |
1505 | { | |
1506 | return hash_lookup(zfpm_g->fpm_mac_info_table, key); | |
1507 | } | |
1508 | ||
1509 | /* | |
1510 | * Callback to allocate fpm_mac_info_t structure. | |
1511 | */ | |
1512 | static void *zfpm_mac_info_alloc(void *p) | |
1513 | { | |
1514 | const struct fpm_mac_info_t *key = p; | |
1515 | struct fpm_mac_info_t *fpm_mac; | |
1516 | ||
1517 | fpm_mac = XCALLOC(MTYPE_FPM_MAC_INFO, sizeof(struct fpm_mac_info_t)); | |
1518 | ||
1519 | memcpy(&fpm_mac->macaddr, &key->macaddr, ETH_ALEN); | |
e5218ec8 AD |
1520 | fpm_mac->vni = key->vni; |
1521 | ||
1522 | return (void *)fpm_mac; | |
1523 | } | |
1524 | ||
1525 | /* | |
1526 | * Delink and free fpm_mac_info_t. | |
1527 | */ | |
1528 | static void zfpm_mac_info_del(struct fpm_mac_info_t *fpm_mac) | |
1529 | { | |
1530 | hash_release(zfpm_g->fpm_mac_info_table, fpm_mac); | |
1531 | TAILQ_REMOVE(&zfpm_g->mac_q, fpm_mac, fpm_mac_q_entries); | |
1532 | XFREE(MTYPE_FPM_MAC_INFO, fpm_mac); | |
1533 | } | |
1534 | ||
a780a738 AD |
1535 | /* |
1536 | * zfpm_trigger_rmac_update | |
1537 | * | |
1538 | * Zebra code invokes this function to indicate that we should | |
1539 | * send an update to FPM for given MAC entry. | |
1540 | * | |
1541 | * This function checks if we already have enqueued an update for this RMAC, | |
1542 | * If yes, update the same fpm_mac_info_t. Else, create and enqueue an update. | |
1543 | */ | |
1544 | static int zfpm_trigger_rmac_update(zebra_mac_t *rmac, zebra_l3vni_t *zl3vni, | |
1545 | bool delete, const char *reason) | |
1546 | { | |
1547 | char buf[ETHER_ADDR_STRLEN]; | |
1548 | struct fpm_mac_info_t *fpm_mac, key; | |
1549 | struct interface *vxlan_if, *svi_if; | |
44f7f132 | 1550 | bool mac_found = false; |
a780a738 AD |
1551 | |
1552 | /* | |
1553 | * Ignore if the connection is down. We will update the FPM about | |
1554 | * all destinations once the connection comes up. | |
1555 | */ | |
1556 | if (!zfpm_conn_is_up()) | |
1557 | return 0; | |
1558 | ||
1559 | if (reason) { | |
1560 | zfpm_debug("triggering update to FPM - Reason: %s - %s", | |
1561 | reason, | |
1562 | prefix_mac2str(&rmac->macaddr, buf, sizeof(buf))); | |
1563 | } | |
1564 | ||
1565 | vxlan_if = zl3vni_map_to_vxlan_if(zl3vni); | |
1566 | svi_if = zl3vni_map_to_svi_if(zl3vni); | |
1567 | ||
1568 | memset(&key, 0, sizeof(struct fpm_mac_info_t)); | |
1569 | ||
1570 | memcpy(&key.macaddr, &rmac->macaddr, ETH_ALEN); | |
a780a738 AD |
1571 | key.vni = zl3vni->vni; |
1572 | ||
1573 | /* Check if this MAC is already present in the queue. */ | |
1574 | fpm_mac = zfpm_mac_info_lookup(&key); | |
1575 | ||
1576 | if (fpm_mac) { | |
44f7f132 | 1577 | mac_found = true; |
a780a738 AD |
1578 | |
1579 | /* | |
44f7f132 AD |
1580 | * If the enqueued op is "add" and current op is "delete", |
1581 | * this is a noop. So, Unset ZEBRA_MAC_UPDATE_FPM flag. | |
1582 | * While processing FPM queue, we will silently delete this | |
1583 | * MAC entry without sending any update for this MAC. | |
a780a738 | 1584 | */ |
44f7f132 AD |
1585 | if (!CHECK_FLAG(fpm_mac->fpm_flags, ZEBRA_MAC_DELETE_FPM) && |
1586 | delete == 1) { | |
a780a738 AD |
1587 | SET_FLAG(fpm_mac->fpm_flags, ZEBRA_MAC_DELETE_FPM); |
1588 | UNSET_FLAG(fpm_mac->fpm_flags, ZEBRA_MAC_UPDATE_FPM); | |
44f7f132 | 1589 | return 0; |
a780a738 | 1590 | } |
44f7f132 AD |
1591 | } else { |
1592 | fpm_mac = hash_get(zfpm_g->fpm_mac_info_table, &key, | |
1593 | zfpm_mac_info_alloc); | |
1594 | if (!fpm_mac) | |
1595 | return 0; | |
a780a738 AD |
1596 | } |
1597 | ||
44f7f132 | 1598 | fpm_mac->r_vtep_ip.s_addr = rmac->fwd_info.r_vtep_ip.s_addr; |
c5431822 | 1599 | fpm_mac->zebra_flags = rmac->flags; |
a780a738 AD |
1600 | fpm_mac->vxlan_if = vxlan_if ? vxlan_if->ifindex : 0; |
1601 | fpm_mac->svi_if = svi_if ? svi_if->ifindex : 0; | |
1602 | ||
1603 | SET_FLAG(fpm_mac->fpm_flags, ZEBRA_MAC_UPDATE_FPM); | |
1604 | if (delete) | |
1605 | SET_FLAG(fpm_mac->fpm_flags, ZEBRA_MAC_DELETE_FPM); | |
44f7f132 AD |
1606 | else |
1607 | UNSET_FLAG(fpm_mac->fpm_flags, ZEBRA_MAC_DELETE_FPM); | |
a780a738 | 1608 | |
44f7f132 AD |
1609 | if (!mac_found) |
1610 | TAILQ_INSERT_TAIL(&zfpm_g->mac_q, fpm_mac, fpm_mac_q_entries); | |
a780a738 AD |
1611 | |
1612 | zfpm_g->stats.updates_triggered++; | |
1613 | ||
a780a738 AD |
1614 | /* If writes are already enabled, return. */ |
1615 | if (zfpm_g->t_write) | |
1616 | return 0; | |
1617 | ||
1618 | zfpm_write_on(); | |
1619 | return 0; | |
1620 | } | |
1621 | ||
fbe748e5 AD |
1622 | /* |
1623 | * This function is called when the FPM connections is established. | |
1624 | * Iterate over all the RMAC entries for the given L3VNI | |
1625 | * and enqueue the RMAC for FPM processing. | |
1626 | */ | |
7f5818fb | 1627 | static void zfpm_trigger_rmac_update_wrapper(struct hash_bucket *backet, |
fbe748e5 AD |
1628 | void *args) |
1629 | { | |
1630 | zebra_mac_t *zrmac = (zebra_mac_t *)backet->data; | |
1631 | zebra_l3vni_t *zl3vni = (zebra_l3vni_t *)args; | |
1632 | ||
1633 | zfpm_trigger_rmac_update(zrmac, zl3vni, false, "RMAC added"); | |
1634 | } | |
1635 | ||
1636 | /* | |
1637 | * This function is called when the FPM connections is established. | |
1638 | * This function iterates over all the L3VNIs to trigger | |
1639 | * FPM updates for RMACs currently available. | |
1640 | */ | |
7f5818fb | 1641 | static void zfpm_iterate_rmac_table(struct hash_bucket *backet, void *args) |
fbe748e5 AD |
1642 | { |
1643 | zebra_l3vni_t *zl3vni = (zebra_l3vni_t *)backet->data; | |
1644 | ||
1645 | hash_iterate(zl3vni->rmac_table, zfpm_trigger_rmac_update_wrapper, | |
1646 | (void *)zl3vni); | |
1647 | } | |
1648 | ||
5adc2528 AS |
1649 | /* |
1650 | * zfpm_stats_timer_cb | |
1651 | */ | |
d62a17ae | 1652 | static int zfpm_stats_timer_cb(struct thread *t) |
5adc2528 | 1653 | { |
d62a17ae | 1654 | zfpm_g->t_stats = NULL; |
5adc2528 | 1655 | |
d62a17ae | 1656 | /* |
1657 | * Remember the stats collected in the last interval for display | |
1658 | * purposes. | |
1659 | */ | |
1660 | zfpm_stats_copy(&zfpm_g->stats, &zfpm_g->last_ivl_stats); | |
5adc2528 | 1661 | |
d62a17ae | 1662 | /* |
1663 | * Add the current set of stats into the cumulative statistics. | |
1664 | */ | |
1665 | zfpm_stats_compose(&zfpm_g->cumulative_stats, &zfpm_g->stats, | |
1666 | &zfpm_g->cumulative_stats); | |
5adc2528 | 1667 | |
d62a17ae | 1668 | /* |
1669 | * Start collecting stats afresh over the next interval. | |
1670 | */ | |
1671 | zfpm_stats_reset(&zfpm_g->stats); | |
5adc2528 | 1672 | |
d62a17ae | 1673 | zfpm_start_stats_timer(); |
5adc2528 | 1674 | |
d62a17ae | 1675 | return 0; |
5adc2528 AS |
1676 | } |
1677 | ||
1678 | /* | |
1679 | * zfpm_stop_stats_timer | |
1680 | */ | |
d62a17ae | 1681 | static void zfpm_stop_stats_timer(void) |
5adc2528 | 1682 | { |
d62a17ae | 1683 | if (!zfpm_g->t_stats) |
1684 | return; | |
5adc2528 | 1685 | |
d62a17ae | 1686 | zfpm_debug("Stopping existing stats timer"); |
1687 | THREAD_TIMER_OFF(zfpm_g->t_stats); | |
5adc2528 AS |
1688 | } |
1689 | ||
1690 | /* | |
1691 | * zfpm_start_stats_timer | |
1692 | */ | |
d62a17ae | 1693 | void zfpm_start_stats_timer(void) |
5adc2528 | 1694 | { |
d62a17ae | 1695 | assert(!zfpm_g->t_stats); |
5adc2528 | 1696 | |
d62a17ae | 1697 | thread_add_timer(zfpm_g->master, zfpm_stats_timer_cb, 0, |
1698 | ZFPM_STATS_IVL_SECS, &zfpm_g->t_stats); | |
5adc2528 AS |
1699 | } |
1700 | ||
1701 | /* | |
1702 | * Helper macro for zfpm_show_stats() below. | |
1703 | */ | |
d62a17ae | 1704 | #define ZFPM_SHOW_STAT(counter) \ |
1705 | do { \ | |
1706 | vty_out(vty, "%-40s %10lu %16lu\n", #counter, \ | |
1707 | total_stats.counter, zfpm_g->last_ivl_stats.counter); \ | |
1708 | } while (0) | |
5adc2528 AS |
1709 | |
1710 | /* | |
1711 | * zfpm_show_stats | |
1712 | */ | |
d62a17ae | 1713 | static void zfpm_show_stats(struct vty *vty) |
5adc2528 | 1714 | { |
d62a17ae | 1715 | zfpm_stats_t total_stats; |
1716 | time_t elapsed; | |
1717 | ||
1718 | vty_out(vty, "\n%-40s %10s Last %2d secs\n\n", "Counter", "Total", | |
1719 | ZFPM_STATS_IVL_SECS); | |
1720 | ||
1721 | /* | |
1722 | * Compute the total stats up to this instant. | |
1723 | */ | |
1724 | zfpm_stats_compose(&zfpm_g->cumulative_stats, &zfpm_g->stats, | |
1725 | &total_stats); | |
1726 | ||
1727 | ZFPM_SHOW_STAT(connect_calls); | |
1728 | ZFPM_SHOW_STAT(connect_no_sock); | |
1729 | ZFPM_SHOW_STAT(read_cb_calls); | |
1730 | ZFPM_SHOW_STAT(write_cb_calls); | |
1731 | ZFPM_SHOW_STAT(write_calls); | |
1732 | ZFPM_SHOW_STAT(partial_writes); | |
1733 | ZFPM_SHOW_STAT(max_writes_hit); | |
1734 | ZFPM_SHOW_STAT(t_write_yields); | |
1735 | ZFPM_SHOW_STAT(nop_deletes_skipped); | |
1736 | ZFPM_SHOW_STAT(route_adds); | |
1737 | ZFPM_SHOW_STAT(route_dels); | |
1738 | ZFPM_SHOW_STAT(updates_triggered); | |
d62a17ae | 1739 | ZFPM_SHOW_STAT(redundant_triggers); |
1740 | ZFPM_SHOW_STAT(dests_del_after_update); | |
1741 | ZFPM_SHOW_STAT(t_conn_down_starts); | |
1742 | ZFPM_SHOW_STAT(t_conn_down_dests_processed); | |
1743 | ZFPM_SHOW_STAT(t_conn_down_yields); | |
1744 | ZFPM_SHOW_STAT(t_conn_down_finishes); | |
1745 | ZFPM_SHOW_STAT(t_conn_up_starts); | |
1746 | ZFPM_SHOW_STAT(t_conn_up_dests_processed); | |
1747 | ZFPM_SHOW_STAT(t_conn_up_yields); | |
1748 | ZFPM_SHOW_STAT(t_conn_up_aborts); | |
1749 | ZFPM_SHOW_STAT(t_conn_up_finishes); | |
1750 | ||
1751 | if (!zfpm_g->last_stats_clear_time) | |
1752 | return; | |
1753 | ||
1754 | elapsed = zfpm_get_elapsed_time(zfpm_g->last_stats_clear_time); | |
1755 | ||
1756 | vty_out(vty, "\nStats were cleared %lu seconds ago\n", | |
1757 | (unsigned long)elapsed); | |
5adc2528 AS |
1758 | } |
1759 | ||
1760 | /* | |
1761 | * zfpm_clear_stats | |
1762 | */ | |
d62a17ae | 1763 | static void zfpm_clear_stats(struct vty *vty) |
5adc2528 | 1764 | { |
d62a17ae | 1765 | if (!zfpm_is_enabled()) { |
1766 | vty_out(vty, "The FPM module is not enabled...\n"); | |
1767 | return; | |
1768 | } | |
5adc2528 | 1769 | |
d62a17ae | 1770 | zfpm_stats_reset(&zfpm_g->stats); |
1771 | zfpm_stats_reset(&zfpm_g->last_ivl_stats); | |
1772 | zfpm_stats_reset(&zfpm_g->cumulative_stats); | |
5adc2528 | 1773 | |
d62a17ae | 1774 | zfpm_stop_stats_timer(); |
1775 | zfpm_start_stats_timer(); | |
5adc2528 | 1776 | |
d62a17ae | 1777 | zfpm_g->last_stats_clear_time = monotime(NULL); |
5adc2528 | 1778 | |
d62a17ae | 1779 | vty_out(vty, "Cleared FPM stats\n"); |
5adc2528 AS |
1780 | } |
1781 | ||
1782 | /* | |
1783 | * show_zebra_fpm_stats | |
1784 | */ | |
1785 | DEFUN (show_zebra_fpm_stats, | |
1786 | show_zebra_fpm_stats_cmd, | |
1787 | "show zebra fpm stats", | |
1788 | SHOW_STR | |
41e7fb80 | 1789 | ZEBRA_STR |
5adc2528 AS |
1790 | "Forwarding Path Manager information\n" |
1791 | "Statistics\n") | |
1792 | { | |
d62a17ae | 1793 | zfpm_show_stats(vty); |
1794 | return CMD_SUCCESS; | |
5adc2528 AS |
1795 | } |
1796 | ||
1797 | /* | |
1798 | * clear_zebra_fpm_stats | |
1799 | */ | |
1800 | DEFUN (clear_zebra_fpm_stats, | |
1801 | clear_zebra_fpm_stats_cmd, | |
1802 | "clear zebra fpm stats", | |
1803 | CLEAR_STR | |
41e7fb80 | 1804 | ZEBRA_STR |
5adc2528 AS |
1805 | "Clear Forwarding Path Manager information\n" |
1806 | "Statistics\n") | |
1807 | { | |
d62a17ae | 1808 | zfpm_clear_stats(vty); |
1809 | return CMD_SUCCESS; | |
5adc2528 AS |
1810 | } |
1811 | ||
711ff0ba | 1812 | /* |
d62a17ae | 1813 | * update fpm connection information |
711ff0ba | 1814 | */ |
e52702f2 QY |
1815 | DEFUN ( fpm_remote_ip, |
1816 | fpm_remote_ip_cmd, | |
1817 | "fpm connection ip A.B.C.D port (1-65535)", | |
711ff0ba USK |
1818 | "fpm connection remote ip and port\n" |
1819 | "Remote fpm server ip A.B.C.D\n" | |
1820 | "Enter ip ") | |
1821 | { | |
1822 | ||
d62a17ae | 1823 | in_addr_t fpm_server; |
1824 | uint32_t port_no; | |
711ff0ba | 1825 | |
d62a17ae | 1826 | fpm_server = inet_addr(argv[3]->arg); |
1827 | if (fpm_server == INADDR_NONE) | |
1828 | return CMD_ERR_INCOMPLETE; | |
711ff0ba | 1829 | |
d62a17ae | 1830 | port_no = atoi(argv[5]->arg); |
1831 | if (port_no < TCP_MIN_PORT || port_no > TCP_MAX_PORT) | |
1832 | return CMD_ERR_INCOMPLETE; | |
711ff0ba | 1833 | |
d62a17ae | 1834 | zfpm_g->fpm_server = fpm_server; |
1835 | zfpm_g->fpm_port = port_no; | |
711ff0ba USK |
1836 | |
1837 | ||
d62a17ae | 1838 | return CMD_SUCCESS; |
711ff0ba USK |
1839 | } |
1840 | ||
e52702f2 QY |
1841 | DEFUN ( no_fpm_remote_ip, |
1842 | no_fpm_remote_ip_cmd, | |
1843 | "no fpm connection ip A.B.C.D port (1-65535)", | |
711ff0ba USK |
1844 | "fpm connection remote ip and port\n" |
1845 | "Connection\n" | |
1846 | "Remote fpm server ip A.B.C.D\n" | |
1847 | "Enter ip ") | |
1848 | { | |
d62a17ae | 1849 | if (zfpm_g->fpm_server != inet_addr(argv[4]->arg) |
1850 | || zfpm_g->fpm_port != atoi(argv[6]->arg)) | |
1851 | return CMD_ERR_NO_MATCH; | |
711ff0ba | 1852 | |
d62a17ae | 1853 | zfpm_g->fpm_server = FPM_DEFAULT_IP; |
1854 | zfpm_g->fpm_port = FPM_DEFAULT_PORT; | |
711ff0ba | 1855 | |
d62a17ae | 1856 | return CMD_SUCCESS; |
711ff0ba | 1857 | } |
711ff0ba | 1858 | |
fb0aa886 AS |
1859 | /* |
1860 | * zfpm_init_message_format | |
1861 | */ | |
d62a17ae | 1862 | static inline void zfpm_init_message_format(const char *format) |
fb0aa886 | 1863 | { |
d62a17ae | 1864 | int have_netlink, have_protobuf; |
fb0aa886 | 1865 | |
fb0aa886 | 1866 | #ifdef HAVE_NETLINK |
d62a17ae | 1867 | have_netlink = 1; |
4b2792b5 | 1868 | #else |
d62a17ae | 1869 | have_netlink = 0; |
fb0aa886 AS |
1870 | #endif |
1871 | ||
1872 | #ifdef HAVE_PROTOBUF | |
d62a17ae | 1873 | have_protobuf = 1; |
4b2792b5 | 1874 | #else |
d62a17ae | 1875 | have_protobuf = 0; |
fb0aa886 AS |
1876 | #endif |
1877 | ||
d62a17ae | 1878 | zfpm_g->message_format = ZFPM_MSG_FORMAT_NONE; |
fb0aa886 | 1879 | |
d62a17ae | 1880 | if (!format) { |
1881 | if (have_netlink) { | |
1882 | zfpm_g->message_format = ZFPM_MSG_FORMAT_NETLINK; | |
1883 | } else if (have_protobuf) { | |
1884 | zfpm_g->message_format = ZFPM_MSG_FORMAT_PROTOBUF; | |
1885 | } | |
1886 | return; | |
fb0aa886 | 1887 | } |
fb0aa886 | 1888 | |
d62a17ae | 1889 | if (!strcmp("netlink", format)) { |
1890 | if (!have_netlink) { | |
1c50c1c0 QY |
1891 | flog_err(EC_ZEBRA_NETLINK_NOT_AVAILABLE, |
1892 | "FPM netlink message format is not available"); | |
d62a17ae | 1893 | return; |
1894 | } | |
1895 | zfpm_g->message_format = ZFPM_MSG_FORMAT_NETLINK; | |
1896 | return; | |
fb0aa886 | 1897 | } |
fb0aa886 | 1898 | |
d62a17ae | 1899 | if (!strcmp("protobuf", format)) { |
1900 | if (!have_protobuf) { | |
af4c2728 | 1901 | flog_err( |
e914ccbe | 1902 | EC_ZEBRA_PROTOBUF_NOT_AVAILABLE, |
d62a17ae | 1903 | "FPM protobuf message format is not available"); |
1904 | return; | |
1905 | } | |
8b9cf71c DL |
1906 | flog_warn(EC_ZEBRA_PROTOBUF_NOT_AVAILABLE, |
1907 | "FPM protobuf message format is deprecated and scheduled to be removed. " | |
1908 | "Please convert to using netlink format or contact dev@lists.frrouting.org with your use case."); | |
d62a17ae | 1909 | zfpm_g->message_format = ZFPM_MSG_FORMAT_PROTOBUF; |
1910 | return; | |
fb0aa886 | 1911 | } |
fb0aa886 | 1912 | |
e914ccbe | 1913 | flog_warn(EC_ZEBRA_FPM_FORMAT_UNKNOWN, "Unknown fpm format '%s'", |
9df414fe | 1914 | format); |
fb0aa886 AS |
1915 | } |
1916 | ||
711ff0ba | 1917 | /** |
d62a17ae | 1918 | * fpm_remote_srv_write |
711ff0ba | 1919 | * |
d62a17ae | 1920 | * Module to write remote fpm connection |
711ff0ba USK |
1921 | * |
1922 | * Returns ZERO on success. | |
1923 | */ | |
1924 | ||
d62a17ae | 1925 | static int fpm_remote_srv_write(struct vty *vty) |
711ff0ba | 1926 | { |
d62a17ae | 1927 | struct in_addr in; |
711ff0ba | 1928 | |
d62a17ae | 1929 | in.s_addr = zfpm_g->fpm_server; |
711ff0ba | 1930 | |
9d1c2659 | 1931 | if ((zfpm_g->fpm_server != FPM_DEFAULT_IP |
996c9314 LB |
1932 | && zfpm_g->fpm_server != INADDR_ANY) |
1933 | || (zfpm_g->fpm_port != FPM_DEFAULT_PORT && zfpm_g->fpm_port != 0)) | |
d62a17ae | 1934 | vty_out(vty, "fpm connection ip %s port %d\n", inet_ntoa(in), |
1935 | zfpm_g->fpm_port); | |
711ff0ba | 1936 | |
d62a17ae | 1937 | return 0; |
711ff0ba USK |
1938 | } |
1939 | ||
1940 | ||
612c2c15 | 1941 | static int fpm_remote_srv_write(struct vty *vty); |
4f8ea50c | 1942 | /* Zebra node */ |
62b346ee DL |
1943 | static struct cmd_node zebra_node = { |
1944 | .node = ZEBRA_NODE, | |
1945 | .prompt = "", | |
612c2c15 | 1946 | .config_write = fpm_remote_srv_write, |
62b346ee | 1947 | }; |
4f8ea50c DL |
1948 | |
1949 | ||
5adc2528 AS |
1950 | /** |
1951 | * zfpm_init | |
1952 | * | |
1953 | * One-time initialization of the Zebra FPM module. | |
1954 | * | |
1955 | * @param[in] port port at which FPM is running. | |
2951a7a4 | 1956 | * @param[in] enable true if the zebra FPM module should be enabled |
fb0aa886 | 1957 | * @param[in] format to use to talk to the FPM. Can be 'netink' or 'protobuf'. |
5adc2528 | 1958 | * |
2951a7a4 | 1959 | * Returns true on success. |
5adc2528 | 1960 | */ |
d62a17ae | 1961 | static int zfpm_init(struct thread_master *master) |
5adc2528 | 1962 | { |
d62a17ae | 1963 | int enable = 1; |
1964 | uint16_t port = 0; | |
1965 | const char *format = THIS_MODULE->load_args; | |
5adc2528 | 1966 | |
d62a17ae | 1967 | memset(zfpm_g, 0, sizeof(*zfpm_g)); |
1968 | zfpm_g->master = master; | |
1969 | TAILQ_INIT(&zfpm_g->dest_q); | |
e5218ec8 AD |
1970 | TAILQ_INIT(&zfpm_g->mac_q); |
1971 | ||
1972 | /* Create hash table for fpm_mac_info_t enties */ | |
1973 | zfpm_g->fpm_mac_info_table = hash_create(zfpm_mac_info_hash_keymake, | |
fbe748e5 AD |
1974 | zfpm_mac_info_cmp, |
1975 | "FPM MAC info hash table"); | |
e5218ec8 | 1976 | |
d62a17ae | 1977 | zfpm_g->sock = -1; |
1978 | zfpm_g->state = ZFPM_STATE_IDLE; | |
5adc2528 | 1979 | |
d62a17ae | 1980 | zfpm_stats_init(&zfpm_g->stats); |
1981 | zfpm_stats_init(&zfpm_g->last_ivl_stats); | |
1982 | zfpm_stats_init(&zfpm_g->cumulative_stats); | |
5adc2528 | 1983 | |
612c2c15 | 1984 | install_node(&zebra_node); |
d62a17ae | 1985 | install_element(ENABLE_NODE, &show_zebra_fpm_stats_cmd); |
1986 | install_element(ENABLE_NODE, &clear_zebra_fpm_stats_cmd); | |
1987 | install_element(CONFIG_NODE, &fpm_remote_ip_cmd); | |
1988 | install_element(CONFIG_NODE, &no_fpm_remote_ip_cmd); | |
5adc2528 | 1989 | |
d62a17ae | 1990 | zfpm_init_message_format(format); |
fb0aa886 | 1991 | |
d62a17ae | 1992 | /* |
1993 | * Disable FPM interface if no suitable format is available. | |
1994 | */ | |
1995 | if (zfpm_g->message_format == ZFPM_MSG_FORMAT_NONE) | |
1996 | enable = 0; | |
fb0aa886 | 1997 | |
d62a17ae | 1998 | zfpm_g->enabled = enable; |
5adc2528 | 1999 | |
d62a17ae | 2000 | if (!zfpm_g->fpm_server) |
2001 | zfpm_g->fpm_server = FPM_DEFAULT_IP; | |
711ff0ba | 2002 | |
d62a17ae | 2003 | if (!port) |
2004 | port = FPM_DEFAULT_PORT; | |
5adc2528 | 2005 | |
d62a17ae | 2006 | zfpm_g->fpm_port = port; |
5adc2528 | 2007 | |
d62a17ae | 2008 | zfpm_g->obuf = stream_new(ZFPM_OBUF_SIZE); |
2009 | zfpm_g->ibuf = stream_new(ZFPM_IBUF_SIZE); | |
5adc2528 | 2010 | |
d62a17ae | 2011 | zfpm_start_stats_timer(); |
2012 | zfpm_start_connect_timer("initialized"); | |
2013 | return 0; | |
4f8ea50c | 2014 | } |
5adc2528 | 2015 | |
f0c459f0 DS |
2016 | static int zfpm_fini(void) |
2017 | { | |
2018 | zfpm_write_off(); | |
2019 | zfpm_read_off(); | |
2020 | zfpm_connect_off(); | |
2021 | ||
2022 | zfpm_stop_stats_timer(); | |
2023 | ||
2024 | hook_unregister(rib_update, zfpm_trigger_update); | |
2025 | return 0; | |
2026 | } | |
2027 | ||
d62a17ae | 2028 | static int zebra_fpm_module_init(void) |
4f8ea50c | 2029 | { |
d62a17ae | 2030 | hook_register(rib_update, zfpm_trigger_update); |
a780a738 | 2031 | hook_register(zebra_rmac_update, zfpm_trigger_rmac_update); |
d62a17ae | 2032 | hook_register(frr_late_init, zfpm_init); |
f0c459f0 | 2033 | hook_register(frr_early_fini, zfpm_fini); |
d62a17ae | 2034 | return 0; |
5adc2528 | 2035 | } |
4f8ea50c | 2036 | |
d62a17ae | 2037 | FRR_MODULE_SETUP(.name = "zebra_fpm", .version = FRR_VERSION, |
2038 | .description = "zebra FPM (Forwarding Plane Manager) module", | |
2039 | .init = zebra_fpm_module_init, ) |