-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathbgp_route.c
More file actions
15998 lines (13897 loc) · 438 KB
/
Copy pathbgp_route.c
File metadata and controls
15998 lines (13897 loc) · 438 KB
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
// SPDX-License-Identifier: GPL-2.0-or-later
/* BGP routing information
* Copyright (C) 1996, 97, 98, 99 Kunihiro Ishiguro
* Copyright (C) 2016 Job Snijders <job@instituut.net>
*/
#include <zebra.h>
#include <math.h>
#include "printfrr.h"
#include "frrstr.h"
#include "prefix.h"
#include "linklist.h"
#include "memory.h"
#include "command.h"
#include "stream.h"
#include "filter.h"
#include "log.h"
#include "routemap.h"
#include "buffer.h"
#include "sockunion.h"
#include "plist.h"
#include "frrevent.h"
#include "workqueue.h"
#include "queue.h"
#include "memory.h"
#include "srv6.h"
#include "lib/json.h"
#include "lib_errors.h"
#include "zclient.h"
#include "bgpd/bgpd.h"
#include "bgpd/bgp_table.h"
#include "bgpd/bgp_route.h"
#include "bgpd/bgp_attr.h"
#include "bgpd/bgp_debug.h"
#include "bgpd/bgp_errors.h"
#include "bgpd/bgp_aspath.h"
#include "bgpd/bgp_regex.h"
#include "bgpd/bgp_community.h"
#include "bgpd/bgp_community_alias.h"
#include "bgpd/bgp_ecommunity.h"
#include "bgpd/bgp_lcommunity.h"
#include "bgpd/bgp_clist.h"
#include "bgpd/bgp_packet.h"
#include "bgpd/bgp_filter.h"
#include "bgpd/bgp_fsm.h"
#include "bgpd/bgp_mplsvpn.h"
#include "bgpd/bgp_nexthop.h"
#include "bgpd/bgp_damp.h"
#include "bgpd/bgp_advertise.h"
#include "bgpd/bgp_zebra.h"
#include "bgpd/bgp_vty.h"
#include "bgpd/bgp_mpath.h"
#include "bgpd/bgp_nht.h"
#include "bgpd/bgp_updgrp.h"
#include "bgpd/bgp_label.h"
#include "bgpd/bgp_addpath.h"
#include "bgpd/bgp_mac.h"
#include "bgpd/bgp_network.h"
#include "bgpd/bgp_trace.h"
#include "bgpd/bgp_rpki.h"
#ifdef ENABLE_BGP_VNC
#include "bgpd/rfapi/rfapi_backend.h"
#include "bgpd/rfapi/vnc_import_bgp.h"
#include "bgpd/rfapi/vnc_export_bgp.h"
#endif
#include "bgpd/bgp_encap_types.h"
#include "bgpd/bgp_encap_tlv.h"
#include "bgpd/bgp_evpn.h"
#include "bgpd/bgp_evpn_mh.h"
#include "bgpd/bgp_evpn_vty.h"
#include "bgpd/bgp_flowspec.h"
#include "bgpd/bgp_flowspec_util.h"
#include "bgpd/bgp_pbr.h"
#include "bgpd/bgp_route_clippy.c"
DEFINE_HOOK(bgp_snmp_update_stats,
(struct bgp_dest *rn, struct bgp_path_info *pi, bool added),
(rn, pi, added));
DEFINE_HOOK(bgp_rpki_prefix_status,
(struct peer *peer, struct attr *attr,
const struct prefix *prefix),
(peer, attr, prefix));
/* Extern from bgp_dump.c */
extern const char *bgp_origin_str[];
extern const char *bgp_origin_long_str[];
/* PMSI strings. */
#define PMSI_TNLTYPE_STR_NO_INFO "No info"
#define PMSI_TNLTYPE_STR_DEFAULT PMSI_TNLTYPE_STR_NO_INFO
static const struct message bgp_pmsi_tnltype_str[] = {
{PMSI_TNLTYPE_NO_INFO, PMSI_TNLTYPE_STR_NO_INFO},
{PMSI_TNLTYPE_RSVP_TE_P2MP, "RSVP-TE P2MP"},
{PMSI_TNLTYPE_MLDP_P2MP, "mLDP P2MP"},
{PMSI_TNLTYPE_PIM_SSM, "PIM-SSM"},
{PMSI_TNLTYPE_PIM_SM, "PIM-SM"},
{PMSI_TNLTYPE_PIM_BIDIR, "PIM-BIDIR"},
{PMSI_TNLTYPE_INGR_REPL, "Ingress Replication"},
{PMSI_TNLTYPE_MLDP_MP2MP, "mLDP MP2MP"},
{0}
};
#define VRFID_NONE_STR "-"
#define SOFT_RECONFIG_TASK_MAX_PREFIX 25000
DEFINE_HOOK(bgp_process,
(struct bgp * bgp, afi_t afi, safi_t safi, struct bgp_dest *bn,
struct peer *peer, bool withdraw),
(bgp, afi, safi, bn, peer, withdraw));
/** Test if path is suppressed. */
bool bgp_path_suppressed(struct bgp_path_info *pi)
{
if (pi->extra == NULL || pi->extra->aggr_suppressors == NULL)
return false;
return listcount(pi->extra->aggr_suppressors) > 0;
}
struct bgp_dest *bgp_afi_node_get(struct bgp_table *table, afi_t afi,
safi_t safi, const struct prefix *p,
struct prefix_rd *prd)
{
struct bgp_dest *dest;
struct bgp_dest *pdest = NULL;
assert(table);
if ((safi == SAFI_MPLS_VPN) || (safi == SAFI_ENCAP)
|| (safi == SAFI_EVPN)) {
pdest = bgp_node_get(table, (struct prefix *)prd);
if (!bgp_dest_has_bgp_path_info_data(pdest))
bgp_dest_set_bgp_table_info(
pdest, bgp_table_init(table->bgp, afi, safi));
else
pdest = bgp_dest_unlock_node(pdest);
assert(pdest);
table = bgp_dest_get_bgp_table_info(pdest);
}
dest = bgp_node_get(table, p);
if ((safi == SAFI_MPLS_VPN) || (safi == SAFI_ENCAP)
|| (safi == SAFI_EVPN))
dest->pdest = pdest;
return dest;
}
struct bgp_dest *bgp_safi_node_lookup(struct bgp_table *table, safi_t safi,
const struct prefix *p,
struct prefix_rd *prd)
{
struct bgp_dest *dest;
struct bgp_dest *pdest = NULL;
if (!table)
return NULL;
if ((safi == SAFI_MPLS_VPN) || (safi == SAFI_ENCAP)
|| (safi == SAFI_EVPN)) {
pdest = bgp_node_lookup(table, (struct prefix *)prd);
if (!pdest)
return NULL;
if (!bgp_dest_has_bgp_path_info_data(pdest)) {
bgp_dest_unlock_node(pdest);
return NULL;
}
table = bgp_dest_get_bgp_table_info(pdest);
}
dest = bgp_node_lookup(table, p);
return dest;
}
/* Allocate bgp_path_info_extra */
static struct bgp_path_info_extra *bgp_path_info_extra_new(void)
{
struct bgp_path_info_extra *new;
new = XCALLOC(MTYPE_BGP_ROUTE_EXTRA,
sizeof(struct bgp_path_info_extra));
new->label[0] = MPLS_INVALID_LABEL;
new->num_labels = 0;
new->flowspec = NULL;
return new;
}
void bgp_path_info_extra_free(struct bgp_path_info_extra **extra)
{
struct bgp_path_info_extra *e;
if (!extra || !*extra)
return;
e = *extra;
if (e->damp_info)
bgp_damp_info_free(e->damp_info, 0, e->damp_info->afi,
e->damp_info->safi);
e->damp_info = NULL;
if (e->vrfleak && e->vrfleak->parent) {
struct bgp_path_info *bpi =
(struct bgp_path_info *)e->vrfleak->parent;
if (bpi->net) {
/* FIXME: since multiple e may have the same e->parent
* and e->parent->net is holding a refcount for each
* of them, we need to do some fudging here.
*
* WARNING: if bpi->net->lock drops to 0, bpi may be
* freed as well (because bpi->net was holding the
* last reference to bpi) => write after free!
*/
unsigned refcount;
bpi = bgp_path_info_lock(bpi);
refcount = bgp_dest_get_lock_count(bpi->net) - 1;
bgp_dest_unlock_node((struct bgp_dest *)bpi->net);
if (!refcount)
bpi->net = NULL;
bgp_path_info_unlock(bpi);
}
bgp_path_info_unlock(e->vrfleak->parent);
e->vrfleak->parent = NULL;
}
if (e->vrfleak && e->vrfleak->bgp_orig)
bgp_unlock(e->vrfleak->bgp_orig);
if (e->vrfleak && e->vrfleak->peer_orig)
peer_unlock(e->vrfleak->peer_orig);
if (e->aggr_suppressors)
list_delete(&e->aggr_suppressors);
if (e->evpn && e->evpn->mh_info)
bgp_evpn_path_mh_info_free(e->evpn->mh_info);
if ((*extra)->flowspec && (*extra)->flowspec->bgp_fs_iprule)
list_delete(&((*extra)->flowspec->bgp_fs_iprule));
if ((*extra)->flowspec && (*extra)->flowspec->bgp_fs_pbr)
list_delete(&((*extra)->flowspec->bgp_fs_pbr));
if (e->evpn)
XFREE(MTYPE_BGP_ROUTE_EXTRA_EVPN, e->evpn);
if (e->flowspec)
XFREE(MTYPE_BGP_ROUTE_EXTRA_FS, e->flowspec);
if (e->vrfleak)
XFREE(MTYPE_BGP_ROUTE_EXTRA_VRFLEAK, e->vrfleak);
XFREE(MTYPE_BGP_ROUTE_EXTRA, *extra);
}
/* Get bgp_path_info extra information for the given bgp_path_info, lazy
* allocated if required.
*/
struct bgp_path_info_extra *bgp_path_info_extra_get(struct bgp_path_info *pi)
{
if (!pi->extra)
pi->extra = bgp_path_info_extra_new();
if (!pi->extra->evpn && pi->net && pi->net->rn->p.family == AF_EVPN)
pi->extra->evpn =
XCALLOC(MTYPE_BGP_ROUTE_EXTRA_EVPN,
sizeof(struct bgp_path_info_extra_evpn));
return pi->extra;
}
/* Free bgp route information. */
void bgp_path_info_free_with_caller(const char *name,
struct bgp_path_info *path)
{
frrtrace(2, frr_bgp, bgp_path_info_free, path, name);
bgp_attr_unintern(&path->attr);
bgp_unlink_nexthop(path);
bgp_path_info_extra_free(&path->extra);
bgp_path_info_mpath_free(&path->mpath);
if (path->net)
bgp_addpath_free_info_data(&path->tx_addpath,
&path->net->tx_addpath);
peer_unlock(path->peer); /* bgp_path_info peer reference */
XFREE(MTYPE_BGP_ROUTE, path);
}
struct bgp_path_info *bgp_path_info_lock(struct bgp_path_info *path)
{
path->lock++;
return path;
}
struct bgp_path_info *bgp_path_info_unlock(struct bgp_path_info *path)
{
assert(path && path->lock > 0);
path->lock--;
if (path->lock == 0) {
bgp_path_info_free(path);
return NULL;
}
return path;
}
bool bgp_path_info_nexthop_changed(struct bgp_path_info *pi, struct peer *to,
afi_t afi)
{
if (pi->peer->sort == BGP_PEER_IBGP && to->sort == BGP_PEER_IBGP &&
!CHECK_FLAG(to->af_flags[afi][SAFI_MPLS_VPN],
PEER_FLAG_FORCE_NEXTHOP_SELF))
/* IBGP RR with no nexthop self force configured */
return false;
if (to->sort == BGP_PEER_IBGP &&
!CHECK_FLAG(to->af_flags[afi][SAFI_MPLS_VPN],
PEER_FLAG_NEXTHOP_SELF))
/* IBGP RR with no nexthop self configured */
return false;
if (CHECK_FLAG(to->af_flags[afi][SAFI_MPLS_VPN],
PEER_FLAG_NEXTHOP_UNCHANGED))
/* IBGP or EBGP with nexthop attribute unchanged */
return false;
return true;
}
/* This function sets flag BGP_NODE_SELECT_DEFER based on condition */
static int bgp_dest_set_defer_flag(struct bgp_dest *dest, bool delete)
{
struct peer *peer;
struct bgp_path_info *old_pi, *nextpi;
bool set_flag = false;
struct bgp *bgp = NULL;
struct bgp_table *table = NULL;
afi_t afi = 0;
safi_t safi = 0;
/* If the flag BGP_NODE_SELECT_DEFER is set and new path is added
* then the route selection is deferred
*/
if (CHECK_FLAG(dest->flags, BGP_NODE_SELECT_DEFER) && (!delete))
return 0;
if (CHECK_FLAG(dest->flags, BGP_NODE_PROCESS_SCHEDULED)) {
if (BGP_DEBUG(update, UPDATE_OUT)) {
table = bgp_dest_table(dest);
if (table)
bgp = table->bgp;
zlog_debug(
"Route %pBD(%s) is in workqueue and being processed, not deferred.",
dest, bgp ? bgp->name_pretty : "(Unknown)");
}
return 0;
}
table = bgp_dest_table(dest);
if (table) {
bgp = table->bgp;
afi = table->afi;
safi = table->safi;
}
for (old_pi = bgp_dest_get_bgp_path_info(dest);
(old_pi != NULL) && (nextpi = old_pi->next, 1); old_pi = nextpi) {
if (CHECK_FLAG(old_pi->flags, BGP_PATH_SELECTED))
continue;
/* Route selection is deferred if there is a stale path which
* which indicates peer is in restart mode
*/
if (CHECK_FLAG(old_pi->flags, BGP_PATH_STALE)
&& (old_pi->sub_type == BGP_ROUTE_NORMAL)) {
set_flag = true;
} else {
/* If the peer is graceful restart capable and peer is
* restarting mode, set the flag BGP_NODE_SELECT_DEFER
*/
peer = old_pi->peer;
if (BGP_PEER_GRACEFUL_RESTART_CAPABLE(peer)
&& BGP_PEER_RESTARTING_MODE(peer)
&& (old_pi
&& old_pi->sub_type == BGP_ROUTE_NORMAL)) {
set_flag = true;
}
}
if (set_flag)
break;
}
/* Set the flag BGP_NODE_SELECT_DEFER if route selection deferral timer
* is active
*/
if (set_flag && table) {
if (bgp && (bgp->gr_info[afi][safi].t_select_deferral)) {
if (!CHECK_FLAG(dest->flags, BGP_NODE_SELECT_DEFER))
bgp->gr_info[afi][safi].gr_deferred++;
SET_FLAG(dest->flags, BGP_NODE_SELECT_DEFER);
if (BGP_DEBUG(update, UPDATE_OUT))
zlog_debug("DEFER route %pBD(%s), dest %p",
dest, bgp->name_pretty, dest);
return 0;
}
}
return -1;
}
void bgp_path_info_add_with_caller(const char *name, struct bgp_dest *dest,
struct bgp_path_info *pi)
{
frrtrace(3, frr_bgp, bgp_path_info_add, dest, pi, name);
struct bgp_path_info *top;
top = bgp_dest_get_bgp_path_info(dest);
pi->next = top;
pi->prev = NULL;
if (top)
top->prev = pi;
bgp_dest_set_bgp_path_info(dest, pi);
bgp_path_info_lock(pi);
bgp_dest_lock_node(dest);
peer_lock(pi->peer); /* bgp_path_info peer reference */
bgp_dest_set_defer_flag(dest, false);
hook_call(bgp_snmp_update_stats, dest, pi, true);
}
/* Do the actual removal of info from RIB, for use by bgp_process
completion callback *only* */
struct bgp_dest *bgp_path_info_reap(struct bgp_dest *dest,
struct bgp_path_info *pi)
{
if (pi->next)
pi->next->prev = pi->prev;
if (pi->prev)
pi->prev->next = pi->next;
else
bgp_dest_set_bgp_path_info(dest, pi->next);
bgp_path_info_mpath_dequeue(pi);
bgp_path_info_unlock(pi);
hook_call(bgp_snmp_update_stats, dest, pi, false);
return bgp_dest_unlock_node(dest);
}
void bgp_path_info_delete(struct bgp_dest *dest, struct bgp_path_info *pi)
{
bgp_path_info_set_flag(dest, pi, BGP_PATH_REMOVED);
/* set of previous already took care of pcount */
UNSET_FLAG(pi->flags, BGP_PATH_VALID);
}
/* undo the effects of a previous call to bgp_path_info_delete; typically
called when a route is deleted and then quickly re-added before the
deletion has been processed */
void bgp_path_info_restore(struct bgp_dest *dest, struct bgp_path_info *pi)
{
bgp_path_info_unset_flag(dest, pi, BGP_PATH_REMOVED);
/* unset of previous already took care of pcount */
SET_FLAG(pi->flags, BGP_PATH_VALID);
}
/* Adjust pcount as required */
static void bgp_pcount_adjust(struct bgp_dest *dest, struct bgp_path_info *pi)
{
struct bgp_table *table;
assert(dest && bgp_dest_table(dest));
assert(pi && pi->peer && pi->peer->bgp);
table = bgp_dest_table(dest);
if (pi->peer == pi->peer->bgp->peer_self)
return;
if (!BGP_PATH_COUNTABLE(pi)
&& CHECK_FLAG(pi->flags, BGP_PATH_COUNTED)) {
UNSET_FLAG(pi->flags, BGP_PATH_COUNTED);
/* slight hack, but more robust against errors. */
if (pi->peer->pcount[table->afi][table->safi])
pi->peer->pcount[table->afi][table->safi]--;
else
flog_err(EC_LIB_DEVELOPMENT,
"Asked to decrement 0 prefix count for peer");
} else if (BGP_PATH_COUNTABLE(pi)
&& !CHECK_FLAG(pi->flags, BGP_PATH_COUNTED)) {
SET_FLAG(pi->flags, BGP_PATH_COUNTED);
pi->peer->pcount[table->afi][table->safi]++;
}
}
static int bgp_label_index_differs(struct bgp_path_info *pi1,
struct bgp_path_info *pi2)
{
return (!(pi1->attr->label_index == pi2->attr->label_index));
}
/* Set/unset bgp_path_info flags, adjusting any other state as needed.
* This is here primarily to keep prefix-count in check.
*/
void bgp_path_info_set_flag(struct bgp_dest *dest, struct bgp_path_info *pi,
uint32_t flag)
{
SET_FLAG(pi->flags, flag);
/* early bath if we know it's not a flag that changes countability state
*/
if (!CHECK_FLAG(flag,
BGP_PATH_VALID | BGP_PATH_HISTORY | BGP_PATH_REMOVED))
return;
bgp_pcount_adjust(dest, pi);
}
void bgp_path_info_unset_flag(struct bgp_dest *dest, struct bgp_path_info *pi,
uint32_t flag)
{
UNSET_FLAG(pi->flags, flag);
/* early bath if we know it's not a flag that changes countability state
*/
if (!CHECK_FLAG(flag,
BGP_PATH_VALID | BGP_PATH_HISTORY | BGP_PATH_REMOVED))
return;
bgp_pcount_adjust(dest, pi);
}
/* Get MED value. If MED value is missing and "bgp bestpath
missing-as-worst" is specified, treat it as the worst value. */
static uint32_t bgp_med_value(struct attr *attr, struct bgp *bgp)
{
if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC))
return attr->med;
else {
if (CHECK_FLAG(bgp->flags, BGP_FLAG_MED_MISSING_AS_WORST))
return BGP_MED_MAX;
else
return 0;
}
}
void bgp_path_info_path_with_addpath_rx_str(struct bgp_path_info *pi, char *buf,
size_t buf_len)
{
struct peer *peer;
if (pi->sub_type == BGP_ROUTE_IMPORTED &&
bgp_get_imported_bpi_ultimate(pi))
peer = bgp_get_imported_bpi_ultimate(pi)->peer;
else
peer = pi->peer;
if (pi->addpath_rx_id)
snprintf(buf, buf_len, "path %s (addpath rxid %d)", peer->host,
pi->addpath_rx_id);
else
snprintf(buf, buf_len, "path %s", peer->host);
}
/*
* Get the ultimate path info.
*/
struct bgp_path_info *bgp_get_imported_bpi_ultimate(struct bgp_path_info *info)
{
struct bgp_path_info *bpi_ultimate;
if (info->sub_type != BGP_ROUTE_IMPORTED)
return info;
for (bpi_ultimate = info;
bpi_ultimate->extra && bpi_ultimate->extra->vrfleak &&
bpi_ultimate->extra->vrfleak->parent;
bpi_ultimate = bpi_ultimate->extra->vrfleak->parent)
;
return bpi_ultimate;
}
/* Compare two bgp route entity. If 'new' is preferable over 'exist' return 1.
*/
int bgp_path_info_cmp(struct bgp *bgp, struct bgp_path_info *new,
struct bgp_path_info *exist, int *paths_eq,
struct bgp_maxpaths_cfg *mpath_cfg, bool debug,
char *pfx_buf, afi_t afi, safi_t safi,
enum bgp_path_selection_reason *reason)
{
const struct prefix *new_p;
struct attr *newattr, *existattr;
enum bgp_peer_sort new_sort;
enum bgp_peer_sort exist_sort;
uint32_t new_pref;
uint32_t exist_pref;
uint32_t new_med;
uint32_t exist_med;
uint32_t new_weight;
uint32_t exist_weight;
uint32_t newm, existm;
struct in_addr new_id;
struct in_addr exist_id;
int new_cluster;
int exist_cluster;
int internal_as_route;
int confed_as_route;
int ret = 0;
int igp_metric_ret = 0;
int peer_sort_ret = -1;
char new_buf[PATH_ADDPATH_STR_BUFFER];
char exist_buf[PATH_ADDPATH_STR_BUFFER];
uint32_t new_mm_seq;
uint32_t exist_mm_seq;
int nh_cmp;
esi_t *exist_esi;
esi_t *new_esi;
bool same_esi;
bool old_proxy;
bool new_proxy;
bool new_origin, exist_origin;
struct bgp_path_info *bpi_ultimate;
struct peer *peer_new, *peer_exist;
*paths_eq = 0;
/* 0. Null check. */
if (new == NULL) {
*reason = bgp_path_selection_none;
if (debug)
zlog_debug("%s: new is NULL", pfx_buf);
return 0;
}
if (debug) {
bpi_ultimate = bgp_get_imported_bpi_ultimate(new);
bgp_path_info_path_with_addpath_rx_str(bpi_ultimate, new_buf,
sizeof(new_buf));
}
if (exist == NULL) {
*reason = bgp_path_selection_first;
if (debug)
zlog_debug("%s(%s): %s is the initial bestpath",
pfx_buf, bgp->name_pretty, new_buf);
return 1;
}
if (debug) {
bpi_ultimate = bgp_get_imported_bpi_ultimate(exist);
bgp_path_info_path_with_addpath_rx_str(bpi_ultimate, exist_buf,
sizeof(exist_buf));
zlog_debug("%s(%s): Comparing %s flags 0x%x with %s flags 0x%x",
pfx_buf, bgp->name_pretty, new_buf, new->flags,
exist_buf, exist->flags);
}
newattr = new->attr;
existattr = exist->attr;
/* A BGP speaker that has advertised the "Long-lived Graceful Restart
* Capability" to a neighbor MUST perform the following upon receiving
* a route from that neighbor with the "LLGR_STALE" community, or upon
* attaching the "LLGR_STALE" community itself per Section 4.2:
*
* Treat the route as the least-preferred in route selection (see
* below). See the Risks of Depreferencing Routes section (Section 5.2)
* for a discussion of potential risks inherent in doing this.
*/
if (bgp_attr_get_community(newattr) &&
community_include(bgp_attr_get_community(newattr),
COMMUNITY_LLGR_STALE)) {
if (debug)
zlog_debug(
"%s: %s wins over %s due to LLGR_STALE community",
pfx_buf, new_buf, exist_buf);
return 0;
}
if (bgp_attr_get_community(existattr) &&
community_include(bgp_attr_get_community(existattr),
COMMUNITY_LLGR_STALE)) {
if (debug)
zlog_debug(
"%s: %s loses to %s due to LLGR_STALE community",
pfx_buf, new_buf, exist_buf);
return 1;
}
new_p = bgp_dest_get_prefix(new->net);
/* For EVPN routes, we cannot just go by local vs remote, we have to
* look at the MAC mobility sequence number, if present.
*/
if ((safi == SAFI_EVPN)
&& (new_p->u.prefix_evpn.route_type == BGP_EVPN_MAC_IP_ROUTE)) {
/* This is an error condition described in RFC 7432 Section
* 15.2. The RFC
* states that in this scenario "the PE MUST alert the operator"
* but it
* does not state what other action to take. In order to provide
* some
* consistency in this scenario we are going to prefer the path
* with the
* sticky flag.
*/
if (newattr->sticky != existattr->sticky) {
if (newattr->sticky && !existattr->sticky) {
*reason = bgp_path_selection_evpn_sticky_mac;
if (debug)
zlog_debug(
"%s: %s wins over %s due to sticky MAC flag",
pfx_buf, new_buf, exist_buf);
return 1;
}
if (!newattr->sticky && existattr->sticky) {
*reason = bgp_path_selection_evpn_sticky_mac;
if (debug)
zlog_debug(
"%s: %s loses to %s due to sticky MAC flag",
pfx_buf, new_buf, exist_buf);
return 0;
}
}
new_esi = bgp_evpn_attr_get_esi(newattr);
exist_esi = bgp_evpn_attr_get_esi(existattr);
if (bgp_evpn_is_esi_valid(new_esi) &&
!memcmp(new_esi, exist_esi, sizeof(esi_t))) {
same_esi = true;
} else {
same_esi = false;
}
/* If both paths have the same non-zero ES and
* one path is local it wins.
* PS: Note the local path wins even if the remote
* has the higher MM seq. The local path's
* MM seq will be fixed up to match the highest
* rem seq, subsequently.
*/
if (same_esi) {
char esi_buf[ESI_STR_LEN];
if (bgp_evpn_is_path_local(bgp, new)) {
*reason = bgp_path_selection_evpn_local_path;
if (debug)
zlog_debug(
"%s: %s wins over %s as ES %s is same and local",
pfx_buf, new_buf, exist_buf,
esi_to_str(new_esi, esi_buf,
sizeof(esi_buf)));
return 1;
}
if (bgp_evpn_is_path_local(bgp, exist)) {
*reason = bgp_path_selection_evpn_local_path;
if (debug)
zlog_debug(
"%s: %s loses to %s as ES %s is same and local",
pfx_buf, new_buf, exist_buf,
esi_to_str(new_esi, esi_buf,
sizeof(esi_buf)));
return 0;
}
}
new_mm_seq = mac_mobility_seqnum(newattr);
exist_mm_seq = mac_mobility_seqnum(existattr);
if (new_mm_seq > exist_mm_seq) {
*reason = bgp_path_selection_evpn_seq;
if (debug)
zlog_debug(
"%s: %s wins over %s due to MM seq %u > %u",
pfx_buf, new_buf, exist_buf, new_mm_seq,
exist_mm_seq);
return 1;
}
if (new_mm_seq < exist_mm_seq) {
*reason = bgp_path_selection_evpn_seq;
if (debug)
zlog_debug(
"%s: %s loses to %s due to MM seq %u < %u",
pfx_buf, new_buf, exist_buf, new_mm_seq,
exist_mm_seq);
return 0;
}
/* if the sequence numbers and ESI are the same and one path
* is non-proxy it wins (over proxy)
*/
new_proxy = bgp_evpn_attr_is_proxy(newattr);
old_proxy = bgp_evpn_attr_is_proxy(existattr);
if (same_esi && bgp_evpn_attr_is_local_es(newattr) &&
old_proxy != new_proxy) {
if (!new_proxy) {
*reason = bgp_path_selection_evpn_non_proxy;
if (debug)
zlog_debug(
"%s: %s wins over %s, same seq/es and non-proxy",
pfx_buf, new_buf, exist_buf);
return 1;
}
*reason = bgp_path_selection_evpn_non_proxy;
if (debug)
zlog_debug(
"%s: %s loses to %s, same seq/es and non-proxy",
pfx_buf, new_buf, exist_buf);
return 0;
}
/*
* if sequence numbers are the same path with the lowest IP
* wins
*/
nh_cmp = bgp_path_info_nexthop_cmp(new, exist);
if (nh_cmp < 0) {
*reason = bgp_path_selection_evpn_lower_ip;
if (debug)
zlog_debug(
"%s: %s wins over %s due to same MM seq %u and lower IP %pI4",
pfx_buf, new_buf, exist_buf, new_mm_seq,
&new->attr->nexthop);
return 1;
}
if (nh_cmp > 0) {
*reason = bgp_path_selection_evpn_lower_ip;
if (debug)
zlog_debug(
"%s: %s loses to %s due to same MM seq %u and higher IP %pI4",
pfx_buf, new_buf, exist_buf, new_mm_seq,
&new->attr->nexthop);
return 0;
}
}
/* 1. Weight check. */
new_weight = newattr->weight;
exist_weight = existattr->weight;
if (new_weight > exist_weight) {
*reason = bgp_path_selection_weight;
if (debug)
zlog_debug("%s: %s wins over %s due to weight %d > %d",
pfx_buf, new_buf, exist_buf, new_weight,
exist_weight);
return 1;
}
if (new_weight < exist_weight) {
*reason = bgp_path_selection_weight;
if (debug)
zlog_debug("%s: %s loses to %s due to weight %d < %d",
pfx_buf, new_buf, exist_buf, new_weight,
exist_weight);
return 0;
}
/* 2. Local preference check. */
new_pref = exist_pref = bgp->default_local_pref;
if (newattr->flag & ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF))
new_pref = newattr->local_pref;
if (existattr->flag & ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF))
exist_pref = existattr->local_pref;
if (new_pref > exist_pref) {
*reason = bgp_path_selection_local_pref;
if (debug)
zlog_debug(
"%s: %s wins over %s due to localpref %d > %d",
pfx_buf, new_buf, exist_buf, new_pref,
exist_pref);
return 1;
}
if (new_pref < exist_pref) {
*reason = bgp_path_selection_local_pref;
if (debug)
zlog_debug(
"%s: %s loses to %s due to localpref %d < %d",
pfx_buf, new_buf, exist_buf, new_pref,
exist_pref);
return 0;
}
/* If a BGP speaker supports ACCEPT_OWN and is configured for the
* extensions defined in this document, the following step is inserted
* after the LOCAL_PREF comparison step in the BGP decision process:
* When comparing a pair of routes for a BGP destination, the
* route with the ACCEPT_OWN community attached is preferred over
* the route that does not have the community.
* This extra step MUST only be invoked during the best path selection
* process of VPN-IP routes.
*/
if (safi == SAFI_MPLS_VPN &&
(CHECK_FLAG(new->peer->af_flags[afi][safi], PEER_FLAG_ACCEPT_OWN) ||
CHECK_FLAG(exist->peer->af_flags[afi][safi],
PEER_FLAG_ACCEPT_OWN))) {
bool new_accept_own = false;
bool exist_accept_own = false;
uint32_t accept_own = COMMUNITY_ACCEPT_OWN;
if (newattr->flag & ATTR_FLAG_BIT(BGP_ATTR_COMMUNITIES))
new_accept_own = community_include(
bgp_attr_get_community(newattr), accept_own);
if (existattr->flag & ATTR_FLAG_BIT(BGP_ATTR_COMMUNITIES))
exist_accept_own = community_include(
bgp_attr_get_community(existattr), accept_own);
if (new_accept_own && !exist_accept_own) {
*reason = bgp_path_selection_accept_own;
if (debug)
zlog_debug(
"%s: %s wins over %s due to accept-own",
pfx_buf, new_buf, exist_buf);
return 1;
}
if (!new_accept_own && exist_accept_own) {
*reason = bgp_path_selection_accept_own;
if (debug)
zlog_debug(
"%s: %s loses to %s due to accept-own",
pfx_buf, new_buf, exist_buf);
return 0;
}
}
/* Tie-breaker - AIGP (Metric TLV) attribute */
if (CHECK_FLAG(newattr->flag, ATTR_FLAG_BIT(BGP_ATTR_AIGP)) &&
CHECK_FLAG(existattr->flag, ATTR_FLAG_BIT(BGP_ATTR_AIGP)) &&
CHECK_FLAG(bgp->flags, BGP_FLAG_COMPARE_AIGP)) {
uint64_t new_aigp = bgp_attr_get_aigp_metric(newattr);
uint64_t exist_aigp = bgp_attr_get_aigp_metric(existattr);
if (new_aigp < exist_aigp) {
*reason = bgp_path_selection_aigp;
if (debug)
zlog_debug(
"%s: %s wins over %s due to AIGP %" PRIu64
" < %" PRIu64,
pfx_buf, new_buf, exist_buf, new_aigp,
exist_aigp);
return 1;
}
if (new_aigp > exist_aigp) {
*reason = bgp_path_selection_aigp;
if (debug)
zlog_debug(
"%s: %s loses to %s due to AIGP %" PRIu64
" > %" PRIu64,
pfx_buf, new_buf, exist_buf, new_aigp,
exist_aigp);
return 0;
}
}
/* 3. Local route check. We prefer:
* - BGP_ROUTE_STATIC
* - BGP_ROUTE_AGGREGATE
* - BGP_ROUTE_REDISTRIBUTE
*/
new_origin = !(new->sub_type == BGP_ROUTE_NORMAL ||
new->sub_type == BGP_ROUTE_IMPORTED);
exist_origin = !(exist->sub_type == BGP_ROUTE_NORMAL ||
exist->sub_type == BGP_ROUTE_IMPORTED);
if (new_origin && !exist_origin) {
*reason = bgp_path_selection_local_route;
if (debug)
zlog_debug(
"%s: %s wins over %s due to preferred BGP_ROUTE type",
pfx_buf, new_buf, exist_buf);
return 1;
}
if (!new_origin && exist_origin) {
*reason = bgp_path_selection_local_route;
if (debug)
zlog_debug(
"%s: %s loses to %s due to preferred BGP_ROUTE type",