forked from FRRouting/frr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbgp_route.c
12332 lines (10745 loc) · 329 KB
/
bgp_route.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
/* BGP routing information
* Copyright (C) 1996, 97, 98, 99 Kunihiro Ishiguro
* Copyright (C) 2016 Job Snijders <job@instituut.net>
*
* 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 <math.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 "thread.h"
#include "workqueue.h"
#include "queue.h"
#include "memory.h"
#include "lib/json.h"
#include "lib_errors.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_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"
#if 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_vty.h"
#include "bgpd/bgp_flowspec.h"
#include "bgpd/bgp_flowspec_util.h"
#include "bgpd/bgp_pbr.h"
#ifndef VTYSH_EXTRACT_PL
#include "bgpd/bgp_route_clippy.c"
#endif
/* 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 "-"
struct bgp_node *bgp_afi_node_get(struct bgp_table *table, afi_t afi,
safi_t safi, struct prefix *p,
struct prefix_rd *prd)
{
struct bgp_node *rn;
struct bgp_node *prn = NULL;
assert(table);
if (!table)
return NULL;
if ((safi == SAFI_MPLS_VPN) || (safi == SAFI_ENCAP)
|| (safi == SAFI_EVPN)) {
prn = bgp_node_get(table, (struct prefix *)prd);
if (!bgp_node_has_bgp_path_info_data(prn))
bgp_node_set_bgp_table_info(
prn, bgp_table_init(table->bgp, afi, safi));
else
bgp_unlock_node(prn);
table = bgp_node_get_bgp_table_info(prn);
}
rn = bgp_node_get(table, p);
if ((safi == SAFI_MPLS_VPN) || (safi == SAFI_ENCAP)
|| (safi == SAFI_EVPN))
rn->prn = prn;
return rn;
}
struct bgp_node *bgp_afi_node_lookup(struct bgp_table *table, afi_t afi,
safi_t safi, struct prefix *p,
struct prefix_rd *prd)
{
struct bgp_node *rn;
struct bgp_node *prn = NULL;
if (!table)
return NULL;
if ((safi == SAFI_MPLS_VPN) || (safi == SAFI_ENCAP)
|| (safi == SAFI_EVPN)) {
prn = bgp_node_lookup(table, (struct prefix *)prd);
if (!prn)
return NULL;
if (!bgp_node_has_bgp_path_info_data(prn)) {
bgp_unlock_node(prn);
return NULL;
}
table = bgp_node_get_bgp_table_info(prn);
}
rn = bgp_node_lookup(table, p);
return rn;
}
/* 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->bgp_fs_pbr = list_new();
new->bgp_fs_iprule = list_new();
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 = NULL;
if (e->parent) {
struct bgp_path_info *bpi = (struct bgp_path_info *)e->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 = bpi->net->lock - 1;
bgp_unlock_node((struct bgp_node *)bpi->net);
if (!refcount)
bpi->net = NULL;
bgp_path_info_unlock(bpi);
}
bgp_path_info_unlock(e->parent);
e->parent = NULL;
}
if (e->bgp_orig)
bgp_unlock(e->bgp_orig);
if ((*extra)->bgp_fs_iprule)
list_delete(&((*extra)->bgp_fs_iprule));
if ((*extra)->bgp_fs_pbr)
list_delete(&((*extra)->bgp_fs_pbr));
XFREE(MTYPE_BGP_ROUTE_EXTRA, *extra);
*extra = NULL;
}
/* 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();
return pi->extra;
}
/* Allocate new bgp info structure. */
struct bgp_path_info *bgp_path_info_new(void)
{
return XCALLOC(MTYPE_BGP_ROUTE, sizeof(struct bgp_path_info));
}
/* Free bgp route information. */
static void bgp_path_info_free(struct bgp_path_info *path)
{
if (path->attr)
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) {
#if 0
zlog_debug ("%s: unlocked and freeing", __func__);
zlog_backtrace (LOG_DEBUG);
#endif
bgp_path_info_free(path);
return NULL;
}
#if 0
if (path->lock == 1)
{
zlog_debug ("%s: unlocked to 1", __func__);
zlog_backtrace (LOG_DEBUG);
}
#endif
return path;
}
void bgp_path_info_add(struct bgp_node *rn, struct bgp_path_info *pi)
{
struct bgp_path_info *top;
top = bgp_node_get_bgp_path_info(rn);
pi->next = top;
pi->prev = NULL;
if (top)
top->prev = pi;
bgp_node_set_bgp_path_info(rn, pi);
bgp_path_info_lock(pi);
bgp_lock_node(rn);
peer_lock(pi->peer); /* bgp_path_info peer reference */
}
/* Do the actual removal of info from RIB, for use by bgp_process
completion callback *only* */
void bgp_path_info_reap(struct bgp_node *rn, struct bgp_path_info *pi)
{
if (pi->next)
pi->next->prev = pi->prev;
if (pi->prev)
pi->prev->next = pi->next;
else
bgp_node_set_bgp_path_info(rn, pi->next);
bgp_path_info_mpath_dequeue(pi);
bgp_path_info_unlock(pi);
bgp_unlock_node(rn);
}
void bgp_path_info_delete(struct bgp_node *rn, struct bgp_path_info *pi)
{
bgp_path_info_set_flag(rn, 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_node *rn, struct bgp_path_info *pi)
{
bgp_path_info_unset_flag(rn, 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_node *rn, struct bgp_path_info *pi)
{
struct bgp_table *table;
assert(rn && bgp_node_table(rn));
assert(pi && pi->peer && pi->peer->bgp);
table = bgp_node_table(rn);
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_node *rn, 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(rn, pi);
}
void bgp_path_info_unset_flag(struct bgp_node *rn, 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(rn, 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 (bgp_flag_check(bgp, 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)
{
if (pi->addpath_rx_id)
sprintf(buf, "path %s (addpath rxid %d)", pi->peer->host,
pi->addpath_rx_id);
else
sprintf(buf, "path %s", pi->peer->host);
}
/* Compare two bgp route entity. If 'new' is preferable over 'exist' return 1.
*/
static 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, int debug,
char *pfx_buf, afi_t afi, safi_t safi)
{
struct attr *newattr, *existattr;
bgp_peer_sort_t new_sort;
bgp_peer_sort_t 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;
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;
*paths_eq = 0;
/* 0. Null check. */
if (new == NULL) {
if (debug)
zlog_debug("%s: new is NULL", pfx_buf);
return 0;
}
if (debug)
bgp_path_info_path_with_addpath_rx_str(new, new_buf);
if (exist == NULL) {
if (debug)
zlog_debug("%s: %s is the initial bestpath", pfx_buf,
new_buf);
return 1;
}
if (debug) {
bgp_path_info_path_with_addpath_rx_str(exist, exist_buf);
zlog_debug("%s: Comparing %s flags 0x%x with %s flags 0x%x",
pfx_buf, new_buf, new->flags, exist_buf,
exist->flags);
}
newattr = new->attr;
existattr = exist->attr;
/* 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) {
/* 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 (!debug) {
prefix2str(&new->net->p, pfx_buf,
sizeof(*pfx_buf)
* PREFIX2STR_BUFFER);
bgp_path_info_path_with_addpath_rx_str(new,
new_buf);
bgp_path_info_path_with_addpath_rx_str(
exist, exist_buf);
}
if (newattr->sticky && !existattr->sticky) {
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) {
if (debug)
zlog_debug(
"%s: %s loses to %s due to sticky MAC flag",
pfx_buf, new_buf, exist_buf);
return 0;
}
}
new_mm_seq = mac_mobility_seqnum(newattr);
exist_mm_seq = mac_mobility_seqnum(existattr);
if (new_mm_seq > exist_mm_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) {
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 sequence numbers are the same path with the lowest IP
* wins
*/
nh_cmp = bgp_path_info_nexthop_cmp(new, exist);
if (nh_cmp < 0) {
if (debug)
zlog_debug(
"%s: %s wins over %s due to same MM seq %u and lower IP %s",
pfx_buf, new_buf, exist_buf, new_mm_seq,
inet_ntoa(new->attr->nexthop));
return 1;
}
if (nh_cmp > 0) {
if (debug)
zlog_debug(
"%s: %s loses to %s due to same MM seq %u and higher IP %s",
pfx_buf, new_buf, exist_buf, new_mm_seq,
inet_ntoa(new->attr->nexthop));
return 0;
}
}
/* 1. Weight check. */
new_weight = newattr->weight;
exist_weight = existattr->weight;
if (new_weight > exist_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) {
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) {
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) {
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;
}
/* 3. Local route check. We prefer:
* - BGP_ROUTE_STATIC
* - BGP_ROUTE_AGGREGATE
* - BGP_ROUTE_REDISTRIBUTE
*/
if (!(new->sub_type == BGP_ROUTE_NORMAL ||
new->sub_type == BGP_ROUTE_IMPORTED)) {
if (debug)
zlog_debug(
"%s: %s wins over %s due to preferred BGP_ROUTE type",
pfx_buf, new_buf, exist_buf);
return 1;
}
if (!(exist->sub_type == BGP_ROUTE_NORMAL ||
exist->sub_type == BGP_ROUTE_IMPORTED)) {
if (debug)
zlog_debug(
"%s: %s loses to %s due to preferred BGP_ROUTE type",
pfx_buf, new_buf, exist_buf);
return 0;
}
/* 4. AS path length check. */
if (!bgp_flag_check(bgp, BGP_FLAG_ASPATH_IGNORE)) {
int exist_hops = aspath_count_hops(existattr->aspath);
int exist_confeds = aspath_count_confeds(existattr->aspath);
if (bgp_flag_check(bgp, BGP_FLAG_ASPATH_CONFED)) {
int aspath_hops;
aspath_hops = aspath_count_hops(newattr->aspath);
aspath_hops += aspath_count_confeds(newattr->aspath);
if (aspath_hops < (exist_hops + exist_confeds)) {
if (debug)
zlog_debug(
"%s: %s wins over %s due to aspath (with confeds) hopcount %d < %d",
pfx_buf, new_buf, exist_buf,
aspath_hops,
(exist_hops + exist_confeds));
return 1;
}
if (aspath_hops > (exist_hops + exist_confeds)) {
if (debug)
zlog_debug(
"%s: %s loses to %s due to aspath (with confeds) hopcount %d > %d",
pfx_buf, new_buf, exist_buf,
aspath_hops,
(exist_hops + exist_confeds));
return 0;
}
} else {
int newhops = aspath_count_hops(newattr->aspath);
if (newhops < exist_hops) {
if (debug)
zlog_debug(
"%s: %s wins over %s due to aspath hopcount %d < %d",
pfx_buf, new_buf, exist_buf,
newhops, exist_hops);
return 1;
}
if (newhops > exist_hops) {
if (debug)
zlog_debug(
"%s: %s loses to %s due to aspath hopcount %d > %d",
pfx_buf, new_buf, exist_buf,
newhops, exist_hops);
return 0;
}
}
}
/* 5. Origin check. */
if (newattr->origin < existattr->origin) {
if (debug)
zlog_debug("%s: %s wins over %s due to ORIGIN %s < %s",
pfx_buf, new_buf, exist_buf,
bgp_origin_long_str[newattr->origin],
bgp_origin_long_str[existattr->origin]);
return 1;
}
if (newattr->origin > existattr->origin) {
if (debug)
zlog_debug("%s: %s loses to %s due to ORIGIN %s > %s",
pfx_buf, new_buf, exist_buf,
bgp_origin_long_str[newattr->origin],
bgp_origin_long_str[existattr->origin]);
return 0;
}
/* 6. MED check. */
internal_as_route = (aspath_count_hops(newattr->aspath) == 0
&& aspath_count_hops(existattr->aspath) == 0);
confed_as_route = (aspath_count_confeds(newattr->aspath) > 0
&& aspath_count_confeds(existattr->aspath) > 0
&& aspath_count_hops(newattr->aspath) == 0
&& aspath_count_hops(existattr->aspath) == 0);
if (bgp_flag_check(bgp, BGP_FLAG_ALWAYS_COMPARE_MED)
|| (bgp_flag_check(bgp, BGP_FLAG_MED_CONFED) && confed_as_route)
|| aspath_cmp_left(newattr->aspath, existattr->aspath)
|| aspath_cmp_left_confed(newattr->aspath, existattr->aspath)
|| internal_as_route) {
new_med = bgp_med_value(new->attr, bgp);
exist_med = bgp_med_value(exist->attr, bgp);
if (new_med < exist_med) {
if (debug)
zlog_debug(
"%s: %s wins over %s due to MED %d < %d",
pfx_buf, new_buf, exist_buf, new_med,
exist_med);
return 1;
}
if (new_med > exist_med) {
if (debug)
zlog_debug(
"%s: %s loses to %s due to MED %d > %d",
pfx_buf, new_buf, exist_buf, new_med,
exist_med);
return 0;
}
}
/* 7. Peer type check. */
new_sort = new->peer->sort;
exist_sort = exist->peer->sort;
if (new_sort == BGP_PEER_EBGP
&& (exist_sort == BGP_PEER_IBGP || exist_sort == BGP_PEER_CONFED)) {
if (debug)
zlog_debug(
"%s: %s wins over %s due to eBGP peer > iBGP peer",
pfx_buf, new_buf, exist_buf);
return 1;
}
if (exist_sort == BGP_PEER_EBGP
&& (new_sort == BGP_PEER_IBGP || new_sort == BGP_PEER_CONFED)) {
if (debug)
zlog_debug(
"%s: %s loses to %s due to iBGP peer < eBGP peer",
pfx_buf, new_buf, exist_buf);
return 0;
}
/* 8. IGP metric check. */
newm = existm = 0;
if (new->extra)
newm = new->extra->igpmetric;
if (exist->extra)
existm = exist->extra->igpmetric;
if (newm < existm) {
if (debug)
zlog_debug(
"%s: %s wins over %s due to IGP metric %d < %d",
pfx_buf, new_buf, exist_buf, newm, existm);
ret = 1;
}
if (newm > existm) {
if (debug)
zlog_debug(
"%s: %s loses to %s due to IGP metric %d > %d",
pfx_buf, new_buf, exist_buf, newm, existm);
ret = 0;
}
/* 9. Same IGP metric. Compare the cluster list length as
representative of IGP hops metric. Rewrite the metric value
pair (newm, existm) with the cluster list length. Prefer the
path with smaller cluster list length. */
if (newm == existm) {
if (peer_sort(new->peer) == BGP_PEER_IBGP
&& peer_sort(exist->peer) == BGP_PEER_IBGP
&& (mpath_cfg == NULL
|| CHECK_FLAG(
mpath_cfg->ibgp_flags,
BGP_FLAG_IBGP_MULTIPATH_SAME_CLUSTERLEN))) {
newm = BGP_CLUSTER_LIST_LENGTH(new->attr);
existm = BGP_CLUSTER_LIST_LENGTH(exist->attr);
if (newm < existm) {
if (debug)
zlog_debug(
"%s: %s wins over %s due to CLUSTER_LIST length %d < %d",
pfx_buf, new_buf, exist_buf,
newm, existm);
ret = 1;
}
if (newm > existm) {
if (debug)
zlog_debug(
"%s: %s loses to %s due to CLUSTER_LIST length %d > %d",
pfx_buf, new_buf, exist_buf,
newm, existm);
ret = 0;
}
}
}
/* 10. confed-external vs. confed-internal */
if (CHECK_FLAG(bgp->config, BGP_CONFIG_CONFEDERATION)) {
if (new_sort == BGP_PEER_CONFED
&& exist_sort == BGP_PEER_IBGP) {
if (debug)
zlog_debug(
"%s: %s wins over %s due to confed-external peer > confed-internal peer",
pfx_buf, new_buf, exist_buf);
return 1;
}
if (exist_sort == BGP_PEER_CONFED
&& new_sort == BGP_PEER_IBGP) {
if (debug)
zlog_debug(
"%s: %s loses to %s due to confed-internal peer < confed-external peer",
pfx_buf, new_buf, exist_buf);
return 0;
}
}
/* 11. Maximum path check. */
if (newm == existm) {
/* If one path has a label but the other does not, do not treat
* them as equals for multipath
*/
if ((new->extra &&bgp_is_valid_label(&new->extra->label[0]))
!= (exist->extra
&& bgp_is_valid_label(&exist->extra->label[0]))) {
if (debug)
zlog_debug(
"%s: %s and %s cannot be multipath, one has a label while the other does not",
pfx_buf, new_buf, exist_buf);
} else if (bgp_flag_check(bgp,
BGP_FLAG_ASPATH_MULTIPATH_RELAX)) {
/*
* For the two paths, all comparison steps till IGP
* metric
* have succeeded - including AS_PATH hop count. Since
* 'bgp
* bestpath as-path multipath-relax' knob is on, we
* don't need
* an exact match of AS_PATH. Thus, mark the paths are
* equal.
* That will trigger both these paths to get into the
* multipath
* array.
*/
*paths_eq = 1;
if (debug)
zlog_debug(
"%s: %s and %s are equal via multipath-relax",
pfx_buf, new_buf, exist_buf);
} else if (new->peer->sort == BGP_PEER_IBGP) {
if (aspath_cmp(new->attr->aspath,
exist->attr->aspath)) {
*paths_eq = 1;
if (debug)
zlog_debug(
"%s: %s and %s are equal via matching aspaths",
pfx_buf, new_buf, exist_buf);
}
} else if (new->peer->as == exist->peer->as) {
*paths_eq = 1;
if (debug)
zlog_debug(
"%s: %s and %s are equal via same remote-as",
pfx_buf, new_buf, exist_buf);
}
} else {
/*
* TODO: If unequal cost ibgp multipath is enabled we can
* mark the paths as equal here instead of returning
*/
if (debug) {
if (ret == 1)
zlog_debug(
"%s: %s wins over %s after IGP metric comparison",
pfx_buf, new_buf, exist_buf);
else
zlog_debug(
"%s: %s loses to %s after IGP metric comparison",
pfx_buf, new_buf, exist_buf);
}
return ret;
}
/* 12. If both paths are external, prefer the path that was received
first (the oldest one). This step minimizes route-flap, since a
newer path won't displace an older one, even if it was the
preferred route based on the additional decision criteria below. */
if (!bgp_flag_check(bgp, BGP_FLAG_COMPARE_ROUTER_ID)
&& new_sort == BGP_PEER_EBGP && exist_sort == BGP_PEER_EBGP) {
if (CHECK_FLAG(new->flags, BGP_PATH_SELECTED)) {
if (debug)
zlog_debug(
"%s: %s wins over %s due to oldest external",
pfx_buf, new_buf, exist_buf);
return 1;
}
if (CHECK_FLAG(exist->flags, BGP_PATH_SELECTED)) {
if (debug)
zlog_debug(
"%s: %s loses to %s due to oldest external",
pfx_buf, new_buf, exist_buf);
return 0;
}
}
/* 13. Router-ID comparision. */
/* If one of the paths is "stale", the corresponding peer router-id will
* be 0 and would always win over the other path. If originator id is
* used for the comparision, it will decide which path is better.
*/
if (newattr->flag & ATTR_FLAG_BIT(BGP_ATTR_ORIGINATOR_ID))
new_id.s_addr = newattr->originator_id.s_addr;
else
new_id.s_addr = new->peer->remote_id.s_addr;
if (existattr->flag & ATTR_FLAG_BIT(BGP_ATTR_ORIGINATOR_ID))
exist_id.s_addr = existattr->originator_id.s_addr;
else
exist_id.s_addr = exist->peer->remote_id.s_addr;
if (ntohl(new_id.s_addr) < ntohl(exist_id.s_addr)) {
if (debug)
zlog_debug(
"%s: %s wins over %s due to Router-ID comparison",
pfx_buf, new_buf, exist_buf);
return 1;
}
if (ntohl(new_id.s_addr) > ntohl(exist_id.s_addr)) {
if (debug)
zlog_debug(
"%s: %s loses to %s due to Router-ID comparison",
pfx_buf, new_buf, exist_buf);
return 0;
}
/* 14. Cluster length comparision. */
new_cluster = BGP_CLUSTER_LIST_LENGTH(new->attr);
exist_cluster = BGP_CLUSTER_LIST_LENGTH(exist->attr);
if (new_cluster < exist_cluster) {
if (debug)
zlog_debug(
"%s: %s wins over %s due to CLUSTER_LIST length %d < %d",
pfx_buf, new_buf, exist_buf, new_cluster,
exist_cluster);
return 1;
}
if (new_cluster > exist_cluster) {
if (debug)
zlog_debug(
"%s: %s loses to %s due to CLUSTER_LIST length %d > %d",
pfx_buf, new_buf, exist_buf, new_cluster,