forked from FRRouting/frr
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpath_pcep_pcc.c
1837 lines (1600 loc) · 52.4 KB
/
path_pcep_pcc.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
/*
* Copyright (C) 2020 NetDEF, Inc.
*
* This program 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 of the License, or (at your option)
* any later version.
*
* This program 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
*/
/* TODOS AND KNOWN ISSUES:
- Delete mapping from NB keys to PLSPID when an LSP is deleted either
by the PCE or by NB.
- Revert the hacks to work around ODL requiring a report with
operational status DOWN when an LSP is activated.
- Enforce only the PCE a policy has been delegated to can update it.
- If the router-id is used because the PCC IP is not specified
(either IPv4 or IPv6), the connection to the PCE is not reset
when the router-id changes.
*/
#include <zebra.h>
#include "log.h"
#include "command.h"
#include "libfrr.h"
#include "printfrr.h"
#include "lib/version.h"
#include "northbound.h"
#include "frr_pthread.h"
#include "jhash.h"
#include "pathd/pathd.h"
#include "pathd/path_zebra.h"
#include "pathd/path_errors.h"
#include "pathd/path_pcep.h"
#include "pathd/path_pcep_controller.h"
#include "pathd/path_pcep_lib.h"
#include "pathd/path_pcep_config.h"
#include "pathd/path_pcep_debug.h"
/* The number of time we will skip connecting if we are missing the PCC
* address for an inet family different from the selected transport one*/
#define OTHER_FAMILY_MAX_RETRIES 4
#define MAX_ERROR_MSG_SIZE 256
#define MAX_COMPREQ_TRIES 3
pthread_mutex_t g_pcc_info_mtx = PTHREAD_MUTEX_INITIALIZER;
/* PCEP Event Handler */
static void handle_pcep_open(struct ctrl_state *ctrl_state,
struct pcc_state *pcc_state,
struct pcep_message *msg);
static void handle_pcep_message(struct ctrl_state *ctrl_state,
struct pcc_state *pcc_state,
struct pcep_message *msg);
static void handle_pcep_lsp_initiate(struct ctrl_state *ctrl_state,
struct pcc_state *pcc_state,
struct pcep_message *msg);
static void handle_pcep_lsp_update(struct ctrl_state *ctrl_state,
struct pcc_state *pcc_state,
struct pcep_message *msg);
static void continue_pcep_lsp_update(struct ctrl_state *ctrl_state,
struct pcc_state *pcc_state,
struct path *path, void *payload);
static void handle_pcep_comp_reply(struct ctrl_state *ctrl_state,
struct pcc_state *pcc_state,
struct pcep_message *msg);
/* Internal Functions */
static const char *ipaddr_type_name(struct ipaddr *addr);
static bool filter_path(struct pcc_state *pcc_state, struct path *path);
static void select_pcc_addresses(struct pcc_state *pcc_state);
static void select_transport_address(struct pcc_state *pcc_state);
static void update_tag(struct pcc_state *pcc_state);
static void update_originator(struct pcc_state *pcc_state);
static void schedule_reconnect(struct ctrl_state *ctrl_state,
struct pcc_state *pcc_state);
static void schedule_session_timeout(struct ctrl_state *ctrl_state,
struct pcc_state *pcc_state);
static void cancel_session_timeout(struct ctrl_state *ctrl_state,
struct pcc_state *pcc_state);
static void send_pcep_message(struct pcc_state *pcc_state,
struct pcep_message *msg);
static void send_pcep_error(struct pcc_state *pcc_state,
enum pcep_error_type error_type,
enum pcep_error_value error_value);
static void send_report(struct pcc_state *pcc_state, struct path *path);
static void send_comp_request(struct ctrl_state *ctrl_state,
struct pcc_state *pcc_state,
struct req_entry *req);
static void cancel_comp_requests(struct ctrl_state *ctrl_state,
struct pcc_state *pcc_state);
static void cancel_comp_request(struct ctrl_state *ctrl_state,
struct pcc_state *pcc_state,
struct req_entry *req);
static void specialize_outgoing_path(struct pcc_state *pcc_state,
struct path *path);
static void specialize_incoming_path(struct pcc_state *pcc_state,
struct path *path);
static bool validate_incoming_path(struct pcc_state *pcc_state,
struct path *path, char *errbuff,
size_t buffsize);
static void set_pcc_address(struct pcc_state *pcc_state,
struct lsp_nb_key *nbkey, struct ipaddr *addr);
static int compare_pcc_opts(struct pcc_opts *lhs, struct pcc_opts *rhs);
static int compare_pce_opts(struct pce_opts *lhs, struct pce_opts *rhs);
static int get_previous_best_pce(struct pcc_state **pcc);
static int get_best_pce(struct pcc_state **pcc);
static int get_pce_count_connected(struct pcc_state **pcc);
static bool update_best_pce(struct pcc_state **pcc, int best);
/* Data Structure Helper Functions */
static void lookup_plspid(struct pcc_state *pcc_state, struct path *path);
static void lookup_nbkey(struct pcc_state *pcc_state, struct path *path);
static void free_req_entry(struct req_entry *req);
static struct req_entry *push_new_req(struct pcc_state *pcc_state,
struct path *path);
static void repush_req(struct pcc_state *pcc_state, struct req_entry *req);
static struct req_entry *pop_req(struct pcc_state *pcc_state, uint32_t reqid);
static bool add_reqid_mapping(struct pcc_state *pcc_state, struct path *path);
static void remove_reqid_mapping(struct pcc_state *pcc_state,
struct path *path);
static uint32_t lookup_reqid(struct pcc_state *pcc_state, struct path *path);
static bool has_pending_req_for(struct pcc_state *pcc_state, struct path *path);
/* Data Structure Callbacks */
static int plspid_map_cmp(const struct plspid_map_data *a,
const struct plspid_map_data *b);
static uint32_t plspid_map_hash(const struct plspid_map_data *e);
static int nbkey_map_cmp(const struct nbkey_map_data *a,
const struct nbkey_map_data *b);
static uint32_t nbkey_map_hash(const struct nbkey_map_data *e);
static int req_map_cmp(const struct req_map_data *a,
const struct req_map_data *b);
static uint32_t req_map_hash(const struct req_map_data *e);
/* Data Structure Declarations */
DECLARE_HASH(plspid_map, struct plspid_map_data, mi, plspid_map_cmp,
plspid_map_hash);
DECLARE_HASH(nbkey_map, struct nbkey_map_data, mi, nbkey_map_cmp,
nbkey_map_hash);
DECLARE_HASH(req_map, struct req_map_data, mi, req_map_cmp, req_map_hash);
static inline int req_entry_compare(const struct req_entry *a,
const struct req_entry *b)
{
return a->path->req_id - b->path->req_id;
}
RB_GENERATE(req_entry_head, req_entry, entry, req_entry_compare)
/* ------------ API Functions ------------ */
struct pcc_state *pcep_pcc_initialize(struct ctrl_state *ctrl_state, int index)
{
struct pcc_state *pcc_state = XCALLOC(MTYPE_PCEP, sizeof(*pcc_state));
pcc_state->id = index;
pcc_state->status = PCEP_PCC_DISCONNECTED;
pcc_state->next_reqid = 1;
pcc_state->next_plspid = 1;
RB_INIT(req_entry_head, &pcc_state->requests);
update_tag(pcc_state);
update_originator(pcc_state);
PCEP_DEBUG("%s PCC initialized", pcc_state->tag);
return pcc_state;
}
void pcep_pcc_finalize(struct ctrl_state *ctrl_state,
struct pcc_state *pcc_state)
{
PCEP_DEBUG("%s PCC finalizing...", pcc_state->tag);
pcep_pcc_disable(ctrl_state, pcc_state);
if (pcc_state->pcc_opts != NULL) {
XFREE(MTYPE_PCEP, pcc_state->pcc_opts);
pcc_state->pcc_opts = NULL;
}
if (pcc_state->pce_opts != NULL) {
XFREE(MTYPE_PCEP, pcc_state->pce_opts);
pcc_state->pce_opts = NULL;
}
if (pcc_state->originator != NULL) {
XFREE(MTYPE_PCEP, pcc_state->originator);
pcc_state->originator = NULL;
}
if (pcc_state->t_reconnect != NULL) {
thread_cancel(&pcc_state->t_reconnect);
pcc_state->t_reconnect = NULL;
}
if (pcc_state->t_update_best != NULL) {
thread_cancel(&pcc_state->t_update_best);
pcc_state->t_update_best = NULL;
}
if (pcc_state->t_session_timeout != NULL) {
thread_cancel(&pcc_state->t_session_timeout);
pcc_state->t_session_timeout = NULL;
}
XFREE(MTYPE_PCEP, pcc_state);
}
int compare_pcc_opts(struct pcc_opts *lhs, struct pcc_opts *rhs)
{
int retval;
if (lhs == NULL) {
return 1;
}
if (rhs == NULL) {
return -1;
}
retval = lhs->port - rhs->port;
if (retval != 0) {
return retval;
}
retval = lhs->msd - rhs->msd;
if (retval != 0) {
return retval;
}
if (IS_IPADDR_V4(&lhs->addr)) {
retval = memcmp(&lhs->addr.ipaddr_v4, &rhs->addr.ipaddr_v4,
sizeof(lhs->addr.ipaddr_v4));
if (retval != 0) {
return retval;
}
} else if (IS_IPADDR_V6(&lhs->addr)) {
retval = memcmp(&lhs->addr.ipaddr_v6, &rhs->addr.ipaddr_v6,
sizeof(lhs->addr.ipaddr_v6));
if (retval != 0) {
return retval;
}
}
return 0;
}
int compare_pce_opts(struct pce_opts *lhs, struct pce_opts *rhs)
{
if (lhs == NULL) {
return 1;
}
if (rhs == NULL) {
return -1;
}
int retval = lhs->port - rhs->port;
if (retval != 0) {
return retval;
}
retval = strcmp(lhs->pce_name, rhs->pce_name);
if (retval != 0) {
return retval;
}
retval = lhs->precedence - rhs->precedence;
if (retval != 0) {
return retval;
}
retval = memcmp(&lhs->addr, &rhs->addr, sizeof(lhs->addr));
if (retval != 0) {
return retval;
}
return 0;
}
int pcep_pcc_update(struct ctrl_state *ctrl_state, struct pcc_state *pcc_state,
struct pcc_opts *pcc_opts, struct pce_opts *pce_opts)
{
int ret = 0;
// If the options did not change, then there is nothing to do
if ((compare_pce_opts(pce_opts, pcc_state->pce_opts) == 0)
&& (compare_pcc_opts(pcc_opts, pcc_state->pcc_opts) == 0)) {
return ret;
}
if ((ret = pcep_pcc_disable(ctrl_state, pcc_state))) {
XFREE(MTYPE_PCEP, pcc_opts);
XFREE(MTYPE_PCEP, pce_opts);
return ret;
}
if (pcc_state->pcc_opts != NULL) {
XFREE(MTYPE_PCEP, pcc_state->pcc_opts);
}
if (pcc_state->pce_opts != NULL) {
XFREE(MTYPE_PCEP, pcc_state->pce_opts);
}
pcc_state->pcc_opts = pcc_opts;
pcc_state->pce_opts = pce_opts;
if (IS_IPADDR_V4(&pcc_opts->addr)) {
pcc_state->pcc_addr_v4 = pcc_opts->addr.ipaddr_v4;
SET_FLAG(pcc_state->flags, F_PCC_STATE_HAS_IPV4);
} else {
UNSET_FLAG(pcc_state->flags, F_PCC_STATE_HAS_IPV4);
}
if (IS_IPADDR_V6(&pcc_opts->addr)) {
memcpy(&pcc_state->pcc_addr_v6, &pcc_opts->addr.ipaddr_v6,
sizeof(struct in6_addr));
SET_FLAG(pcc_state->flags, F_PCC_STATE_HAS_IPV6);
} else {
UNSET_FLAG(pcc_state->flags, F_PCC_STATE_HAS_IPV6);
}
update_tag(pcc_state);
update_originator(pcc_state);
return pcep_pcc_enable(ctrl_state, pcc_state);
}
void pcep_pcc_reconnect(struct ctrl_state *ctrl_state,
struct pcc_state *pcc_state)
{
if (pcc_state->status == PCEP_PCC_DISCONNECTED)
pcep_pcc_enable(ctrl_state, pcc_state);
}
int pcep_pcc_enable(struct ctrl_state *ctrl_state, struct pcc_state *pcc_state)
{
assert(pcc_state->status == PCEP_PCC_DISCONNECTED);
assert(pcc_state->sess == NULL);
if (pcc_state->t_reconnect != NULL) {
thread_cancel(&pcc_state->t_reconnect);
pcc_state->t_reconnect = NULL;
}
select_transport_address(pcc_state);
/* Even though we are connecting using IPv6. we want to have an IPv4
* address so we can handle candidate path with IPv4 endpoints */
if (!CHECK_FLAG(pcc_state->flags, F_PCC_STATE_HAS_IPV4)) {
if (pcc_state->retry_count < OTHER_FAMILY_MAX_RETRIES) {
flog_warn(EC_PATH_PCEP_MISSING_SOURCE_ADDRESS,
"skipping connection to PCE %pIA:%d due to missing PCC IPv4 address",
&pcc_state->pce_opts->addr,
pcc_state->pce_opts->port);
schedule_reconnect(ctrl_state, pcc_state);
return 0;
} else {
flog_warn(EC_PATH_PCEP_MISSING_SOURCE_ADDRESS,
"missing IPv4 PCC address, IPv4 candidate paths will be ignored");
}
}
/* Even though we are connecting using IPv4. we want to have an IPv6
* address so we can handle candidate path with IPv6 endpoints */
if (!CHECK_FLAG(pcc_state->flags, F_PCC_STATE_HAS_IPV6)) {
if (pcc_state->retry_count < OTHER_FAMILY_MAX_RETRIES) {
flog_warn(EC_PATH_PCEP_MISSING_SOURCE_ADDRESS,
"skipping connection to PCE %pIA:%d due to missing PCC IPv6 address",
&pcc_state->pce_opts->addr,
pcc_state->pce_opts->port);
schedule_reconnect(ctrl_state, pcc_state);
return 0;
} else {
flog_warn(EC_PATH_PCEP_MISSING_SOURCE_ADDRESS,
"missing IPv6 PCC address, IPv6 candidate paths will be ignored");
}
}
/* Even if the maximum retries to try to have all the familly addresses
* have been spent, we still need the one for the transport familly */
if (pcc_state->pcc_addr_tr.ipa_type == IPADDR_NONE) {
flog_warn(EC_PATH_PCEP_MISSING_SOURCE_ADDRESS,
"skipping connection to PCE %pIA:%d due to missing PCC address",
&pcc_state->pce_opts->addr,
pcc_state->pce_opts->port);
schedule_reconnect(ctrl_state, pcc_state);
return 0;
}
PCEP_DEBUG("%s PCC connecting", pcc_state->tag);
pcc_state->sess = pcep_lib_connect(
&pcc_state->pcc_addr_tr, pcc_state->pcc_opts->port,
&pcc_state->pce_opts->addr, pcc_state->pce_opts->port,
pcc_state->pcc_opts->msd, &pcc_state->pce_opts->config_opts);
if (pcc_state->sess == NULL) {
flog_warn(EC_PATH_PCEP_LIB_CONNECT,
"failed to connect to PCE %pIA:%d from %pIA:%d",
&pcc_state->pce_opts->addr,
pcc_state->pce_opts->port,
&pcc_state->pcc_addr_tr,
pcc_state->pcc_opts->port);
schedule_reconnect(ctrl_state, pcc_state);
return 0;
}
// In case some best pce alternative were waiting to activate
if (pcc_state->t_update_best != NULL) {
thread_cancel(&pcc_state->t_update_best);
pcc_state->t_update_best = NULL;
}
pcc_state->status = PCEP_PCC_CONNECTING;
return 0;
}
int pcep_pcc_disable(struct ctrl_state *ctrl_state, struct pcc_state *pcc_state)
{
switch (pcc_state->status) {
case PCEP_PCC_DISCONNECTED:
return 0;
case PCEP_PCC_CONNECTING:
case PCEP_PCC_SYNCHRONIZING:
case PCEP_PCC_OPERATING:
PCEP_DEBUG("%s Disconnecting PCC...", pcc_state->tag);
cancel_comp_requests(ctrl_state, pcc_state);
pcep_lib_disconnect(pcc_state->sess);
/* No need to remove if any PCEs is connected */
if (get_pce_count_connected(ctrl_state->pcc) == 0) {
pcep_thread_remove_candidate_path_segments(ctrl_state,
pcc_state);
}
pcc_state->sess = NULL;
pcc_state->status = PCEP_PCC_DISCONNECTED;
return 0;
default:
return 1;
}
}
void pcep_pcc_sync_path(struct ctrl_state *ctrl_state,
struct pcc_state *pcc_state, struct path *path)
{
if (pcc_state->status == PCEP_PCC_SYNCHRONIZING) {
path->is_synching = true;
} else if (pcc_state->status == PCEP_PCC_OPERATING)
path->is_synching = false;
else
return;
path->go_active = true;
/* Accumulate the dynamic paths without any LSP so computation
* requests can be performed after synchronization */
if ((path->type == SRTE_CANDIDATE_TYPE_DYNAMIC)
&& (path->first_hop == NULL)
&& !has_pending_req_for(pcc_state, path)) {
PCEP_DEBUG("%s Scheduling computation request for path %s",
pcc_state->tag, path->name);
push_new_req(pcc_state, path);
return;
}
/* Synchronize the path if the PCE supports LSP updates and the
* endpoint address familly is supported */
if (pcc_state->caps.is_stateful) {
if (filter_path(pcc_state, path)) {
PCEP_DEBUG("%s Synchronizing path %s", pcc_state->tag,
path->name);
send_report(pcc_state, path);
} else {
PCEP_DEBUG(
"%s Skipping %s candidate path %s synchronization",
pcc_state->tag,
ipaddr_type_name(&path->nbkey.endpoint),
path->name);
}
}
}
void pcep_pcc_sync_done(struct ctrl_state *ctrl_state,
struct pcc_state *pcc_state)
{
struct req_entry *req;
if (pcc_state->status != PCEP_PCC_SYNCHRONIZING
&& pcc_state->status != PCEP_PCC_OPERATING)
return;
if (pcc_state->caps.is_stateful
&& pcc_state->status == PCEP_PCC_SYNCHRONIZING) {
struct path *path = pcep_new_path();
*path = (struct path){.name = NULL,
.srp_id = 0,
.plsp_id = 0,
.status = PCEP_LSP_OPERATIONAL_DOWN,
.do_remove = false,
.go_active = false,
.was_created = false,
.was_removed = false,
.is_synching = false,
.is_delegated = false,
.first_hop = NULL,
.first_metric = NULL};
send_report(pcc_state, path);
pcep_free_path(path);
}
pcc_state->synchronized = true;
pcc_state->status = PCEP_PCC_OPERATING;
PCEP_DEBUG("%s Synchronization done", pcc_state->tag);
/* Start the computation request accumulated during synchronization */
RB_FOREACH (req, req_entry_head, &pcc_state->requests) {
send_comp_request(ctrl_state, pcc_state, req);
}
}
void pcep_pcc_send_report(struct ctrl_state *ctrl_state,
struct pcc_state *pcc_state, struct path *path,
bool is_stable)
{
if ((pcc_state->status != PCEP_PCC_OPERATING)
|| (!pcc_state->caps.is_stateful)) {
pcep_free_path(path);
return;
}
PCEP_DEBUG("%s Send report for candidate path %s", pcc_state->tag,
path->name);
/* ODL and Cisco requires the first reported
* LSP to have a DOWN status, the later status changes
* will be comunicated through hook calls.
*/
enum pcep_lsp_operational_status real_status = path->status;
path->status = PCEP_LSP_OPERATIONAL_DOWN;
send_report(pcc_state, path);
/* If no update is expected and the real status wasn't down, we need to
* send a second report with the real status */
if (is_stable && (real_status != PCEP_LSP_OPERATIONAL_DOWN)) {
path->srp_id = 0;
path->status = real_status;
send_report(pcc_state, path);
}
pcep_free_path(path);
}
/* ------------ Timeout handler ------------ */
void pcep_pcc_timeout_handler(struct ctrl_state *ctrl_state,
struct pcc_state *pcc_state,
enum pcep_ctrl_timeout_type type, void *param)
{
struct req_entry *req;
switch (type) {
case TO_COMPUTATION_REQUEST:
assert(param != NULL);
req = (struct req_entry *)param;
pop_req(pcc_state, req->path->req_id);
flog_warn(EC_PATH_PCEP_COMPUTATION_REQUEST_TIMEOUT,
"Computation request %d timeout", req->path->req_id);
cancel_comp_request(ctrl_state, pcc_state, req);
if (req->retry_count++ < MAX_COMPREQ_TRIES) {
repush_req(pcc_state, req);
send_comp_request(ctrl_state, pcc_state, req);
return;
}
if (pcc_state->caps.is_stateful) {
struct path *path;
PCEP_DEBUG(
"%s Delegating undefined dynamic path %s to PCE %s",
pcc_state->tag, req->path->name,
pcc_state->originator);
path = pcep_copy_path(req->path);
path->is_delegated = true;
send_report(pcc_state, path);
free_req_entry(req);
}
break;
default:
break;
}
}
/* ------------ Pathd event handler ------------ */
void pcep_pcc_pathd_event_handler(struct ctrl_state *ctrl_state,
struct pcc_state *pcc_state,
enum pcep_pathd_event_type type,
struct path *path)
{
struct req_entry *req;
if (pcc_state->status != PCEP_PCC_OPERATING)
return;
/* Skipping candidate path with endpoint that do not match the
* configured or deduced PCC IP version */
if (!filter_path(pcc_state, path)) {
PCEP_DEBUG("%s Skipping %s candidate path %s event",
pcc_state->tag,
ipaddr_type_name(&path->nbkey.endpoint), path->name);
return;
}
switch (type) {
case PCEP_PATH_CREATED:
if (has_pending_req_for(pcc_state, path)) {
PCEP_DEBUG(
"%s Candidate path %s created, computation request already sent",
pcc_state->tag, path->name);
return;
}
PCEP_DEBUG("%s Candidate path %s created", pcc_state->tag,
path->name);
if ((path->first_hop == NULL)
&& (path->type == SRTE_CANDIDATE_TYPE_DYNAMIC)) {
req = push_new_req(pcc_state, path);
send_comp_request(ctrl_state, pcc_state, req);
} else if (pcc_state->caps.is_stateful)
send_report(pcc_state, path);
return;
case PCEP_PATH_UPDATED:
PCEP_DEBUG("%s Candidate path %s updated", pcc_state->tag,
path->name);
if (pcc_state->caps.is_stateful)
send_report(pcc_state, path);
return;
case PCEP_PATH_REMOVED:
PCEP_DEBUG("%s Candidate path %s removed", pcc_state->tag,
path->name);
path->was_removed = true;
if (pcc_state->caps.is_stateful)
send_report(pcc_state, path);
return;
default:
flog_warn(EC_PATH_PCEP_RECOVERABLE_INTERNAL_ERROR,
"Unexpected pathd event received by pcc %s: %u",
pcc_state->tag, type);
return;
}
}
/* ------------ PCEP event handler ------------ */
void pcep_pcc_pcep_event_handler(struct ctrl_state *ctrl_state,
struct pcc_state *pcc_state, pcep_event *event)
{
PCEP_DEBUG("%s Received PCEP event: %s", pcc_state->tag,
pcep_event_type_name(event->event_type));
switch (event->event_type) {
case PCC_CONNECTED_TO_PCE:
assert(PCEP_PCC_CONNECTING == pcc_state->status);
PCEP_DEBUG("%s Connection established", pcc_state->tag);
pcc_state->status = PCEP_PCC_SYNCHRONIZING;
pcc_state->retry_count = 0;
pcc_state->synchronized = false;
PCEP_DEBUG("%s Starting PCE synchronization", pcc_state->tag);
cancel_session_timeout(ctrl_state, pcc_state);
pcep_pcc_calculate_best_pce(ctrl_state->pcc);
pcep_thread_start_sync(ctrl_state, pcc_state->id);
break;
case PCC_SENT_INVALID_OPEN:
PCEP_DEBUG("%s Sent invalid OPEN message", pcc_state->tag);
PCEP_DEBUG(
"%s Reconciling values: keep alive (%d) dead timer (%d) seconds ",
pcc_state->tag,
pcc_state->sess->pcc_config
.keep_alive_pce_negotiated_timer_seconds,
pcc_state->sess->pcc_config
.dead_timer_pce_negotiated_seconds);
pcc_state->pce_opts->config_opts.keep_alive_seconds =
pcc_state->sess->pcc_config
.keep_alive_pce_negotiated_timer_seconds;
pcc_state->pce_opts->config_opts.dead_timer_seconds =
pcc_state->sess->pcc_config
.dead_timer_pce_negotiated_seconds;
break;
case PCC_RCVD_INVALID_OPEN:
PCEP_DEBUG("%s Received invalid OPEN message", pcc_state->tag);
PCEP_DEBUG_PCEP("%s PCEP message: %s", pcc_state->tag,
format_pcep_message(event->message));
break;
case PCE_DEAD_TIMER_EXPIRED:
case PCE_CLOSED_SOCKET:
case PCE_SENT_PCEP_CLOSE:
case PCE_OPEN_KEEP_WAIT_TIMER_EXPIRED:
case PCC_PCEP_SESSION_CLOSED:
case PCC_RCVD_MAX_INVALID_MSGS:
case PCC_RCVD_MAX_UNKOWN_MSGS:
pcep_pcc_disable(ctrl_state, pcc_state);
schedule_reconnect(ctrl_state, pcc_state);
schedule_session_timeout(ctrl_state, pcc_state);
break;
case MESSAGE_RECEIVED:
PCEP_DEBUG_PCEP("%s Received PCEP message: %s", pcc_state->tag,
format_pcep_message(event->message));
if (pcc_state->status == PCEP_PCC_CONNECTING) {
if (event->message->msg_header->type == PCEP_TYPE_OPEN)
handle_pcep_open(ctrl_state, pcc_state,
event->message);
break;
}
assert(pcc_state->status == PCEP_PCC_SYNCHRONIZING
|| pcc_state->status == PCEP_PCC_OPERATING);
handle_pcep_message(ctrl_state, pcc_state, event->message);
break;
default:
flog_warn(EC_PATH_PCEP_UNEXPECTED_PCEPLIB_EVENT,
"Unexpected event from pceplib: %s",
format_pcep_event(event));
break;
}
}
/*------------------ Multi-PCE --------------------- */
/* Internal util function, returns true if sync is necessary, false otherwise */
bool update_best_pce(struct pcc_state **pcc, int best)
{
PCEP_DEBUG(" recalculating pce precedence ");
if (best) {
struct pcc_state *best_pcc_state =
pcep_pcc_get_pcc_by_id(pcc, best);
if (best_pcc_state->previous_best != best_pcc_state->is_best) {
PCEP_DEBUG(" %s Resynch best (%i) previous best (%i)",
best_pcc_state->tag, best_pcc_state->id,
best_pcc_state->previous_best);
return true;
} else {
PCEP_DEBUG(
" %s No Resynch best (%i) previous best (%i)",
best_pcc_state->tag, best_pcc_state->id,
best_pcc_state->previous_best);
}
} else {
PCEP_DEBUG(" No best pce available, all pce seem disconnected");
}
return false;
}
int get_best_pce(struct pcc_state **pcc)
{
for (int i = 0; i < MAX_PCC; i++) {
if (pcc[i] && pcc[i]->pce_opts) {
if (pcc[i]->is_best == true) {
return pcc[i]->id;
}
}
}
return 0;
}
int get_pce_count_connected(struct pcc_state **pcc)
{
int count = 0;
for (int i = 0; i < MAX_PCC; i++) {
if (pcc[i] && pcc[i]->pce_opts
&& pcc[i]->status != PCEP_PCC_DISCONNECTED) {
count++;
}
}
return count;
}
int get_previous_best_pce(struct pcc_state **pcc)
{
int previous_best_pce = -1;
for (int i = 0; i < MAX_PCC; i++) {
if (pcc[i] && pcc[i]->pce_opts && pcc[i]->previous_best == true
&& pcc[i]->status != PCEP_PCC_DISCONNECTED) {
previous_best_pce = i;
break;
}
}
return previous_best_pce != -1 ? pcc[previous_best_pce]->id : 0;
}
/* Called by path_pcep_controller EV_REMOVE_PCC
* Event handler when a PCC is removed. */
int pcep_pcc_multi_pce_remove_pcc(struct ctrl_state *ctrl_state,
struct pcc_state **pcc)
{
int new_best_pcc_id = -1;
new_best_pcc_id = pcep_pcc_calculate_best_pce(pcc);
if (new_best_pcc_id) {
if (update_best_pce(ctrl_state->pcc, new_best_pcc_id) == true) {
pcep_thread_start_sync(ctrl_state, new_best_pcc_id);
}
}
return 0;
}
/* Called by path_pcep_controller EV_SYNC_PATH
* Event handler when a path is sync'd. */
int pcep_pcc_multi_pce_sync_path(struct ctrl_state *ctrl_state, int pcc_id,
struct pcc_state **pcc)
{
int previous_best_pcc_id = -1;
if (pcc_id == get_best_pce(pcc)) {
previous_best_pcc_id = get_previous_best_pce(pcc);
if (previous_best_pcc_id != 0) {
/* while adding new pce, path has to resync to the
* previous best. pcep_thread_start_sync() will be
* called by the calling function */
if (update_best_pce(ctrl_state->pcc,
previous_best_pcc_id)
== true) {
cancel_comp_requests(
ctrl_state,
pcep_pcc_get_pcc_by_id(
pcc, previous_best_pcc_id));
pcep_thread_start_sync(ctrl_state,
previous_best_pcc_id);
}
}
}
return 0;
}
/* Called by path_pcep_controller when the TM_CALCULATE_BEST_PCE
* timer expires */
int pcep_pcc_timer_update_best_pce(struct ctrl_state *ctrl_state, int pcc_id)
{
int ret = 0;
/* resync whatever was the new best */
int prev_best = get_best_pce(ctrl_state->pcc);
int best_id = pcep_pcc_calculate_best_pce(ctrl_state->pcc);
if (best_id && prev_best != best_id) { // Avoid Multiple call
struct pcc_state *pcc_state =
pcep_pcc_get_pcc_by_id(ctrl_state->pcc, best_id);
if (update_best_pce(ctrl_state->pcc, pcc_state->id) == true) {
pcep_thread_start_sync(ctrl_state, pcc_state->id);
}
}
return ret;
}
/* Called by path_pcep_controller::pcep_thread_event_update_pce_options()
* Returns the best PCE id */
int pcep_pcc_calculate_best_pce(struct pcc_state **pcc)
{
int best_precedence = 255; // DEFAULT_PCE_PRECEDENCE;
int best_pce = -1;
int one_connected_pce = -1;
int previous_best_pce = -1;
int step_0_best = -1;
int step_0_previous = -1;
int pcc_count = 0;
// Get state
for (int i = 0; i < MAX_PCC; i++) {
if (pcc[i] && pcc[i]->pce_opts) {
zlog_debug(
"multi-pce: calculate all : i (%i) is_best (%i) previous_best (%i) ",
i, pcc[i]->is_best, pcc[i]->previous_best);
pcc_count++;
if (pcc[i]->is_best == true) {
step_0_best = i;
}
if (pcc[i]->previous_best == true) {
step_0_previous = i;
}
}
}
if (!pcc_count) {
return 0;
}
// Calculate best
for (int i = 0; i < MAX_PCC; i++) {
if (pcc[i] && pcc[i]->pce_opts
&& pcc[i]->status != PCEP_PCC_DISCONNECTED) {
one_connected_pce = i; // In case none better
if (pcc[i]->pce_opts->precedence <= best_precedence) {
if (best_pce != -1
&& pcc[best_pce]->pce_opts->precedence
== pcc[i]->pce_opts
->precedence) {
if (ipaddr_cmp(
&pcc[i]->pce_opts->addr,
&pcc[best_pce]
->pce_opts->addr)
> 0)
// collide of precedences so
// compare ip
best_pce = i;
} else {
if (!pcc[i]->previous_best) {
best_precedence =
pcc[i]->pce_opts
->precedence;
best_pce = i;
}
}
}
}
}
zlog_debug(
"multi-pce: calculate data : sb (%i) sp (%i) oc (%i) b (%i) ",
step_0_best, step_0_previous, one_connected_pce, best_pce);
// Changed of state so ...
if (step_0_best != best_pce) {
pthread_mutex_lock(&g_pcc_info_mtx);
// Calculate previous
previous_best_pce = step_0_best;
// Clean state
if (step_0_best != -1) {
pcc[step_0_best]->is_best = false;
}
if (step_0_previous != -1) {
pcc[step_0_previous]->previous_best = false;
}
// Set previous
if (previous_best_pce != -1
&& pcc[previous_best_pce]->status
== PCEP_PCC_DISCONNECTED) {
pcc[previous_best_pce]->previous_best = true;
zlog_debug("multi-pce: previous best pce (%i) ",
previous_best_pce + 1);
}
// Set best
if (best_pce != -1) {
pcc[best_pce]->is_best = true;
zlog_debug("multi-pce: best pce (%i) ", best_pce + 1);
} else {
if (one_connected_pce != -1) {
best_pce = one_connected_pce;
pcc[one_connected_pce]->is_best = true;
zlog_debug(
"multi-pce: one connected best pce (default) (%i) ",
one_connected_pce + 1);
} else {
for (int i = 0; i < MAX_PCC; i++) {
if (pcc[i] && pcc[i]->pce_opts) {
best_pce = i;
pcc[i]->is_best = true;
zlog_debug(
"(disconnected) best pce (default) (%i) ",
i + 1);
break;
}
}
}
}
pthread_mutex_unlock(&g_pcc_info_mtx);
}
return ((best_pce == -1) ? 0 : pcc[best_pce]->id);
}
int pcep_pcc_get_pcc_id_by_ip_port(struct pcc_state **pcc,
struct pce_opts *pce_opts)
{
if (pcc == NULL) {
return 0;
}
for (int idx = 0; idx < MAX_PCC; idx++) {
if (pcc[idx]) {
if ((ipaddr_cmp((const struct ipaddr *)&pcc[idx]
->pce_opts->addr,
(const struct ipaddr *)&pce_opts->addr)