-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathzebra_fpm.c
2051 lines (1682 loc) · 44.2 KB
/
zebra_fpm.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Main implementation file for interface to Forwarding Plane Manager.
*
* Copyright (C) 2012 by Open Source Routing.
* Copyright (C) 2012 by Internet Systems Consortium, Inc. ("ISC")
*
* This file is part of GNU Zebra.
*
* GNU Zebra is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2, or (at your option) any
* later version.
*
* GNU Zebra is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; see the file COPYING; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <zebra.h>
#include "log.h"
#include "libfrr.h"
#include "stream.h"
#include "thread.h"
#include "network.h"
#include "command.h"
#include "lib/version.h"
#include "jhash.h"
#include "zebra/rib.h"
#include "zebra/zserv.h"
#include "zebra/zebra_ns.h"
#include "zebra/zebra_vrf.h"
#include "zebra/zebra_errors.h"
#include "fpm/fpm.h"
#include "zebra_fpm_private.h"
#include "zebra/zebra_router.h"
#include "zebra_vxlan_private.h"
DEFINE_MTYPE_STATIC(ZEBRA, FPM_MAC_INFO, "FPM_MAC_INFO");
/*
* Interval at which we attempt to connect to the FPM.
*/
#define ZFPM_CONNECT_RETRY_IVL 5
/*
* Sizes of outgoing and incoming stream buffers for writing/reading
* FPM messages.
*/
#define ZFPM_OBUF_SIZE (2 * FPM_MAX_MSG_LEN)
#define ZFPM_IBUF_SIZE (FPM_MAX_MSG_LEN)
/*
* The maximum number of times the FPM socket write callback can call
* 'write' before it yields.
*/
#define ZFPM_MAX_WRITES_PER_RUN 10
/*
* Interval over which we collect statistics.
*/
#define ZFPM_STATS_IVL_SECS 10
#define FPM_MAX_MAC_MSG_LEN 512
static void zfpm_iterate_rmac_table(struct hash_bucket *bucket, void *args);
/*
* Structure that holds state for iterating over all route_node
* structures that are candidates for being communicated to the FPM.
*/
struct zfpm_rnodes_iter {
rib_tables_iter_t tables_iter;
route_table_iter_t iter;
};
/*
* Statistics.
*/
struct zfpm_stats {
unsigned long connect_calls;
unsigned long connect_no_sock;
unsigned long read_cb_calls;
unsigned long write_cb_calls;
unsigned long write_calls;
unsigned long partial_writes;
unsigned long max_writes_hit;
unsigned long t_write_yields;
unsigned long nop_deletes_skipped;
unsigned long route_adds;
unsigned long route_dels;
unsigned long updates_triggered;
unsigned long redundant_triggers;
unsigned long dests_del_after_update;
unsigned long t_conn_down_starts;
unsigned long t_conn_down_dests_processed;
unsigned long t_conn_down_yields;
unsigned long t_conn_down_finishes;
unsigned long t_conn_up_starts;
unsigned long t_conn_up_dests_processed;
unsigned long t_conn_up_yields;
unsigned long t_conn_up_aborts;
unsigned long t_conn_up_finishes;
};
/*
* States for the FPM state machine.
*/
enum zfpm_state {
/*
* In this state we are not yet ready to connect to the FPM. This
* can happen when this module is disabled, or if we're cleaning up
* after a connection has gone down.
*/
ZFPM_STATE_IDLE,
/*
* Ready to talk to the FPM and periodically trying to connect to
* it.
*/
ZFPM_STATE_ACTIVE,
/*
* In the middle of bringing up a TCP connection. Specifically,
* waiting for a connect() call to complete asynchronously.
*/
ZFPM_STATE_CONNECTING,
/*
* TCP connection to the FPM is up.
*/
ZFPM_STATE_ESTABLISHED
};
/*
* Message format to be used to communicate with the FPM.
*/
enum zfpm_msg_format {
ZFPM_MSG_FORMAT_NONE,
ZFPM_MSG_FORMAT_NETLINK,
ZFPM_MSG_FORMAT_PROTOBUF,
};
/*
* Globals.
*/
struct zfpm_glob {
/*
* True if the FPM module has been enabled.
*/
int enabled;
/*
* Message format to be used to communicate with the fpm.
*/
enum zfpm_msg_format message_format;
struct thread_master *master;
enum zfpm_state state;
in_addr_t fpm_server;
/*
* Port on which the FPM is running.
*/
int fpm_port;
/*
* List of rib_dest_t structures to be processed
*/
TAILQ_HEAD(zfpm_dest_q, rib_dest_t_) dest_q;
/*
* List of fpm_mac_info structures to be processed
*/
TAILQ_HEAD(zfpm_mac_q, fpm_mac_info_t) mac_q;
/*
* Hash table of fpm_mac_info_t entries
*
* While adding fpm_mac_info_t for a MAC to the mac_q,
* it is possible that another fpm_mac_info_t node for the this MAC
* is already present in the queue.
* This is possible in the case of consecutive add->delete operations.
* To avoid such duplicate insertions in the mac_q,
* define a hash table for fpm_mac_info_t which can be looked up
* to see if an fpm_mac_info_t node for a MAC is already present
* in the mac_q.
*/
struct hash *fpm_mac_info_table;
/*
* Stream socket to the FPM.
*/
int sock;
/*
* Buffers for messages to/from the FPM.
*/
struct stream *obuf;
struct stream *ibuf;
/*
* Threads for I/O.
*/
struct thread *t_connect;
struct thread *t_write;
struct thread *t_read;
/*
* Thread to clean up after the TCP connection to the FPM goes down
* and the state that belongs to it.
*/
struct thread *t_conn_down;
struct {
struct zfpm_rnodes_iter iter;
} t_conn_down_state;
/*
* Thread to take actions once the TCP conn to the FPM comes up, and
* the state that belongs to it.
*/
struct thread *t_conn_up;
struct {
struct zfpm_rnodes_iter iter;
} t_conn_up_state;
unsigned long connect_calls;
time_t last_connect_call_time;
/*
* Stats from the start of the current statistics interval up to
* now. These are the counters we typically update in the code.
*/
struct zfpm_stats stats;
/*
* Statistics that were gathered in the last collection interval.
*/
struct zfpm_stats last_ivl_stats;
/*
* Cumulative stats from the last clear to the start of the current
* statistics interval.
*/
struct zfpm_stats cumulative_stats;
/*
* Stats interval timer.
*/
struct thread *t_stats;
/*
* If non-zero, the last time when statistics were cleared.
*/
time_t last_stats_clear_time;
/*
* Flag to track the MAC dump status to FPM
*/
bool fpm_mac_dump_done;
};
static struct zfpm_glob zfpm_glob_space;
static struct zfpm_glob *zfpm_g = &zfpm_glob_space;
static int zfpm_trigger_update(struct route_node *rn, const char *reason);
static void zfpm_read_cb(struct thread *thread);
static void zfpm_write_cb(struct thread *thread);
static void zfpm_set_state(enum zfpm_state state, const char *reason);
static void zfpm_start_connect_timer(const char *reason);
static void zfpm_start_stats_timer(void);
static void zfpm_mac_info_del(struct fpm_mac_info_t *fpm_mac);
static const char ipv4_ll_buf[16] = "169.254.0.1";
union g_addr ipv4ll_gateway;
/*
* zfpm_thread_should_yield
*/
static inline int zfpm_thread_should_yield(struct thread *t)
{
return thread_should_yield(t);
}
/*
* zfpm_state_to_str
*/
static const char *zfpm_state_to_str(enum zfpm_state state)
{
switch (state) {
case ZFPM_STATE_IDLE:
return "idle";
case ZFPM_STATE_ACTIVE:
return "active";
case ZFPM_STATE_CONNECTING:
return "connecting";
case ZFPM_STATE_ESTABLISHED:
return "established";
default:
return "unknown";
}
}
/*
* zfpm_get_elapsed_time
*
* Returns the time elapsed (in seconds) since the given time.
*/
static time_t zfpm_get_elapsed_time(time_t reference)
{
time_t now;
now = monotime(NULL);
if (now < reference) {
assert(0);
return 0;
}
return now - reference;
}
/*
* zfpm_rnodes_iter_init
*/
static inline void zfpm_rnodes_iter_init(struct zfpm_rnodes_iter *iter)
{
memset(iter, 0, sizeof(*iter));
rib_tables_iter_init(&iter->tables_iter);
/*
* This is a hack, but it makes implementing 'next' easier by
* ensuring that route_table_iter_next() will return NULL the first
* time we call it.
*/
route_table_iter_init(&iter->iter, NULL);
route_table_iter_cleanup(&iter->iter);
}
/*
* zfpm_rnodes_iter_next
*/
static inline struct route_node *
zfpm_rnodes_iter_next(struct zfpm_rnodes_iter *iter)
{
struct route_node *rn;
struct route_table *table;
while (1) {
rn = route_table_iter_next(&iter->iter);
if (rn)
return rn;
/*
* We've made our way through this table, go to the next one.
*/
route_table_iter_cleanup(&iter->iter);
table = rib_tables_iter_next(&iter->tables_iter);
if (!table)
return NULL;
route_table_iter_init(&iter->iter, table);
}
return NULL;
}
/*
* zfpm_rnodes_iter_pause
*/
static inline void zfpm_rnodes_iter_pause(struct zfpm_rnodes_iter *iter)
{
route_table_iter_pause(&iter->iter);
}
/*
* zfpm_rnodes_iter_cleanup
*/
static inline void zfpm_rnodes_iter_cleanup(struct zfpm_rnodes_iter *iter)
{
route_table_iter_cleanup(&iter->iter);
rib_tables_iter_cleanup(&iter->tables_iter);
}
/*
* zfpm_stats_init
*
* Initialize a statistics block.
*/
static inline void zfpm_stats_init(struct zfpm_stats *stats)
{
memset(stats, 0, sizeof(*stats));
}
/*
* zfpm_stats_reset
*/
static inline void zfpm_stats_reset(struct zfpm_stats *stats)
{
zfpm_stats_init(stats);
}
/*
* zfpm_stats_copy
*/
static inline void zfpm_stats_copy(const struct zfpm_stats *src,
struct zfpm_stats *dest)
{
memcpy(dest, src, sizeof(*dest));
}
/*
* zfpm_stats_compose
*
* Total up the statistics in two stats structures ('s1 and 's2') and
* return the result in the third argument, 'result'. Note that the
* pointer 'result' may be the same as 's1' or 's2'.
*
* For simplicity, the implementation below assumes that the stats
* structure is composed entirely of counters. This can easily be
* changed when necessary.
*/
static void zfpm_stats_compose(const struct zfpm_stats *s1,
const struct zfpm_stats *s2,
struct zfpm_stats *result)
{
const unsigned long *p1, *p2;
unsigned long *result_p;
int i, num_counters;
p1 = (const unsigned long *)s1;
p2 = (const unsigned long *)s2;
result_p = (unsigned long *)result;
num_counters = (sizeof(struct zfpm_stats) / sizeof(unsigned long));
for (i = 0; i < num_counters; i++) {
result_p[i] = p1[i] + p2[i];
}
}
/*
* zfpm_read_on
*/
static inline void zfpm_read_on(void)
{
assert(!zfpm_g->t_read);
assert(zfpm_g->sock >= 0);
thread_add_read(zfpm_g->master, zfpm_read_cb, 0, zfpm_g->sock,
&zfpm_g->t_read);
}
/*
* zfpm_write_on
*/
static inline void zfpm_write_on(void)
{
assert(!zfpm_g->t_write);
assert(zfpm_g->sock >= 0);
thread_add_write(zfpm_g->master, zfpm_write_cb, 0, zfpm_g->sock,
&zfpm_g->t_write);
}
/*
* zfpm_read_off
*/
static inline void zfpm_read_off(void)
{
thread_cancel(&zfpm_g->t_read);
}
/*
* zfpm_write_off
*/
static inline void zfpm_write_off(void)
{
thread_cancel(&zfpm_g->t_write);
}
static inline void zfpm_connect_off(void)
{
thread_cancel(&zfpm_g->t_connect);
}
/*
* zfpm_conn_up_thread_cb
*
* Callback for actions to be taken when the connection to the FPM
* comes up.
*/
static void zfpm_conn_up_thread_cb(struct thread *thread)
{
struct route_node *rnode;
struct zfpm_rnodes_iter *iter;
rib_dest_t *dest;
iter = &zfpm_g->t_conn_up_state.iter;
if (zfpm_g->state != ZFPM_STATE_ESTABLISHED) {
zfpm_debug(
"Connection not up anymore, conn_up thread aborting");
zfpm_g->stats.t_conn_up_aborts++;
goto done;
}
if (!zfpm_g->fpm_mac_dump_done) {
/* Enqueue FPM updates for all the RMAC entries */
hash_iterate(zrouter.l3vni_table, zfpm_iterate_rmac_table,
NULL);
/* mark dump done so that its not repeated after yield */
zfpm_g->fpm_mac_dump_done = true;
}
while ((rnode = zfpm_rnodes_iter_next(iter))) {
dest = rib_dest_from_rnode(rnode);
if (dest) {
zfpm_g->stats.t_conn_up_dests_processed++;
zfpm_trigger_update(rnode, NULL);
}
/*
* Yield if need be.
*/
if (!zfpm_thread_should_yield(thread))
continue;
zfpm_g->stats.t_conn_up_yields++;
zfpm_rnodes_iter_pause(iter);
thread_add_timer_msec(zfpm_g->master, zfpm_conn_up_thread_cb,
NULL, 0, &zfpm_g->t_conn_up);
return;
}
zfpm_g->stats.t_conn_up_finishes++;
done:
zfpm_rnodes_iter_cleanup(iter);
}
/*
* zfpm_connection_up
*
* Called when the connection to the FPM comes up.
*/
static void zfpm_connection_up(const char *detail)
{
assert(zfpm_g->sock >= 0);
zfpm_read_on();
zfpm_write_on();
zfpm_set_state(ZFPM_STATE_ESTABLISHED, detail);
/*
* Start thread to push existing routes to the FPM.
*/
thread_cancel(&zfpm_g->t_conn_up);
zfpm_rnodes_iter_init(&zfpm_g->t_conn_up_state.iter);
zfpm_g->fpm_mac_dump_done = false;
zfpm_debug("Starting conn_up thread");
thread_add_timer_msec(zfpm_g->master, zfpm_conn_up_thread_cb, NULL, 0,
&zfpm_g->t_conn_up);
zfpm_g->stats.t_conn_up_starts++;
}
/*
* zfpm_connect_check
*
* Check if an asynchronous connect() to the FPM is complete.
*/
static void zfpm_connect_check(void)
{
int status;
socklen_t slen;
int ret;
zfpm_read_off();
zfpm_write_off();
slen = sizeof(status);
ret = getsockopt(zfpm_g->sock, SOL_SOCKET, SO_ERROR, (void *)&status,
&slen);
if (ret >= 0 && status == 0) {
zfpm_connection_up("async connect complete");
return;
}
/*
* getsockopt() failed or indicated an error on the socket.
*/
close(zfpm_g->sock);
zfpm_g->sock = -1;
zfpm_start_connect_timer("getsockopt() after async connect failed");
return;
}
/*
* zfpm_conn_down_thread_cb
*
* Callback that is invoked to clean up state after the TCP connection
* to the FPM goes down.
*/
static void zfpm_conn_down_thread_cb(struct thread *thread)
{
struct route_node *rnode;
struct zfpm_rnodes_iter *iter;
rib_dest_t *dest;
struct fpm_mac_info_t *mac = NULL;
assert(zfpm_g->state == ZFPM_STATE_IDLE);
/*
* Delink and free all fpm_mac_info_t nodes
* in the mac_q and fpm_mac_info_hash
*/
while ((mac = TAILQ_FIRST(&zfpm_g->mac_q)) != NULL)
zfpm_mac_info_del(mac);
zfpm_g->t_conn_down = NULL;
iter = &zfpm_g->t_conn_down_state.iter;
while ((rnode = zfpm_rnodes_iter_next(iter))) {
dest = rib_dest_from_rnode(rnode);
if (dest) {
if (CHECK_FLAG(dest->flags, RIB_DEST_UPDATE_FPM)) {
TAILQ_REMOVE(&zfpm_g->dest_q, dest,
fpm_q_entries);
}
UNSET_FLAG(dest->flags, RIB_DEST_UPDATE_FPM);
UNSET_FLAG(dest->flags, RIB_DEST_SENT_TO_FPM);
zfpm_g->stats.t_conn_down_dests_processed++;
/*
* Check if the dest should be deleted.
*/
rib_gc_dest(rnode);
}
/*
* Yield if need be.
*/
if (!zfpm_thread_should_yield(thread))
continue;
zfpm_g->stats.t_conn_down_yields++;
zfpm_rnodes_iter_pause(iter);
zfpm_g->t_conn_down = NULL;
thread_add_timer_msec(zfpm_g->master, zfpm_conn_down_thread_cb,
NULL, 0, &zfpm_g->t_conn_down);
return;
}
zfpm_g->stats.t_conn_down_finishes++;
zfpm_rnodes_iter_cleanup(iter);
/*
* Start the process of connecting to the FPM again.
*/
zfpm_start_connect_timer("cleanup complete");
}
/*
* zfpm_connection_down
*
* Called when the connection to the FPM has gone down.
*/
static void zfpm_connection_down(const char *detail)
{
if (!detail)
detail = "unknown";
assert(zfpm_g->state == ZFPM_STATE_ESTABLISHED);
zlog_info("connection to the FPM has gone down: %s", detail);
zfpm_read_off();
zfpm_write_off();
stream_reset(zfpm_g->ibuf);
stream_reset(zfpm_g->obuf);
if (zfpm_g->sock >= 0) {
close(zfpm_g->sock);
zfpm_g->sock = -1;
}
/*
* Start thread to clean up state after the connection goes down.
*/
assert(!zfpm_g->t_conn_down);
zfpm_rnodes_iter_init(&zfpm_g->t_conn_down_state.iter);
zfpm_g->t_conn_down = NULL;
thread_add_timer_msec(zfpm_g->master, zfpm_conn_down_thread_cb, NULL, 0,
&zfpm_g->t_conn_down);
zfpm_g->stats.t_conn_down_starts++;
zfpm_set_state(ZFPM_STATE_IDLE, detail);
}
/*
* zfpm_read_cb
*/
static void zfpm_read_cb(struct thread *thread)
{
size_t already;
struct stream *ibuf;
uint16_t msg_len;
fpm_msg_hdr_t *hdr;
zfpm_g->stats.read_cb_calls++;
/*
* Check if async connect is now done.
*/
if (zfpm_g->state == ZFPM_STATE_CONNECTING) {
zfpm_connect_check();
return;
}
assert(zfpm_g->state == ZFPM_STATE_ESTABLISHED);
assert(zfpm_g->sock >= 0);
ibuf = zfpm_g->ibuf;
already = stream_get_endp(ibuf);
if (already < FPM_MSG_HDR_LEN) {
ssize_t nbyte;
nbyte = stream_read_try(ibuf, zfpm_g->sock,
FPM_MSG_HDR_LEN - already);
if (nbyte == 0 || nbyte == -1) {
if (nbyte == -1) {
char buffer[1024];
snprintf(buffer, sizeof(buffer),
"closed socket in read(%d): %s", errno,
safe_strerror(errno));
zfpm_connection_down(buffer);
} else
zfpm_connection_down("closed socket in read");
return;
}
if (nbyte != (ssize_t)(FPM_MSG_HDR_LEN - already))
goto done;
already = FPM_MSG_HDR_LEN;
}
stream_set_getp(ibuf, 0);
hdr = (fpm_msg_hdr_t *)stream_pnt(ibuf);
if (!fpm_msg_hdr_ok(hdr)) {
zfpm_connection_down("invalid message header");
return;
}
msg_len = fpm_msg_len(hdr);
/*
* Read out the rest of the packet.
*/
if (already < msg_len) {
ssize_t nbyte;
nbyte = stream_read_try(ibuf, zfpm_g->sock, msg_len - already);
if (nbyte == 0 || nbyte == -1) {
if (nbyte == -1) {
char buffer[1024];
snprintf(buffer, sizeof(buffer),
"failed to read message(%d) %s", errno,
safe_strerror(errno));
zfpm_connection_down(buffer);
} else
zfpm_connection_down("failed to read message");
return;
}
if (nbyte != (ssize_t)(msg_len - already))
goto done;
}
/*
* Just throw it away for now.
*/
stream_reset(ibuf);
done:
zfpm_read_on();
}
static bool zfpm_updates_pending(void)
{
if (!(TAILQ_EMPTY(&zfpm_g->dest_q)) || !(TAILQ_EMPTY(&zfpm_g->mac_q)))
return true;
return false;
}
/*
* zfpm_writes_pending
*
* Returns true if we may have something to write to the FPM.
*/
static int zfpm_writes_pending(void)
{
/*
* Check if there is any data in the outbound buffer that has not
* been written to the socket yet.
*/
if (stream_get_endp(zfpm_g->obuf) - stream_get_getp(zfpm_g->obuf))
return 1;
/*
* Check if there are any updates scheduled on the outbound queues.
*/
if (zfpm_updates_pending())
return 1;
return 0;
}
/*
* zfpm_encode_route
*
* Encode a message to the FPM with information about the given route.
*
* Returns the number of bytes written to the buffer. 0 or a negative
* value indicates an error.
*/
static inline int zfpm_encode_route(rib_dest_t *dest, struct route_entry *re,
char *in_buf, size_t in_buf_len,
fpm_msg_type_e *msg_type)
{
size_t len;
#ifdef HAVE_NETLINK
int cmd;
#endif
len = 0;
*msg_type = FPM_MSG_TYPE_NONE;
switch (zfpm_g->message_format) {
case ZFPM_MSG_FORMAT_PROTOBUF:
#ifdef HAVE_PROTOBUF
len = zfpm_protobuf_encode_route(dest, re, (uint8_t *)in_buf,
in_buf_len);
*msg_type = FPM_MSG_TYPE_PROTOBUF;
#endif
break;
case ZFPM_MSG_FORMAT_NETLINK:
#ifdef HAVE_NETLINK
*msg_type = FPM_MSG_TYPE_NETLINK;
cmd = re ? RTM_NEWROUTE : RTM_DELROUTE;
len = zfpm_netlink_encode_route(cmd, dest, re, in_buf,
in_buf_len);
assert(fpm_msg_align(len) == len);
*msg_type = FPM_MSG_TYPE_NETLINK;
#endif /* HAVE_NETLINK */
break;
default:
break;
}
return len;
}
/*
* zfpm_route_for_update
*
* Returns the re that is to be sent to the FPM for a given dest.
*/
struct route_entry *zfpm_route_for_update(rib_dest_t *dest)
{
return dest->selected_fib;
}
/*
* Define an enum for return codes for queue processing functions
*
* FPM_WRITE_STOP: This return code indicates that the write buffer is full.
* Stop processing all the queues and empty the buffer by writing its content
* to the socket.
*
* FPM_GOTO_NEXT_Q: This return code indicates that either this queue is
* empty or we have processed enough updates from this queue.
* So, move on to the next queue.
*/
enum {
FPM_WRITE_STOP = 0,
FPM_GOTO_NEXT_Q = 1
};
#define FPM_QUEUE_PROCESS_LIMIT 10000
/*
* zfpm_build_route_updates
*
* Process the dest_q queue and write FPM messages to the outbound buffer.
*/
static int zfpm_build_route_updates(void)
{
struct stream *s;
rib_dest_t *dest;
unsigned char *buf, *data, *buf_end;
size_t msg_len;
size_t data_len;
fpm_msg_hdr_t *hdr;
struct route_entry *re;
int is_add, write_msg;
fpm_msg_type_e msg_type;
uint16_t q_limit;
if (TAILQ_EMPTY(&zfpm_g->dest_q))
return FPM_GOTO_NEXT_Q;
s = zfpm_g->obuf;
q_limit = FPM_QUEUE_PROCESS_LIMIT;
do {
/*
* Make sure there is enough space to write another message.
*/
if (STREAM_WRITEABLE(s) < FPM_MAX_MSG_LEN)
return FPM_WRITE_STOP;
buf = STREAM_DATA(s) + stream_get_endp(s);
buf_end = buf + STREAM_WRITEABLE(s);
dest = TAILQ_FIRST(&zfpm_g->dest_q);
if (!dest)
return FPM_GOTO_NEXT_Q;
assert(CHECK_FLAG(dest->flags, RIB_DEST_UPDATE_FPM));
hdr = (fpm_msg_hdr_t *)buf;
hdr->version = FPM_PROTO_VERSION;
data = fpm_msg_data(hdr);
re = zfpm_route_for_update(dest);
is_add = re ? 1 : 0;
write_msg = 1;
/*
* If this is a route deletion, and we have not sent the route
* to
* the FPM previously, skip it.
*/
if (!is_add && !CHECK_FLAG(dest->flags, RIB_DEST_SENT_TO_FPM)) {
write_msg = 0;
zfpm_g->stats.nop_deletes_skipped++;
}