]> git.proxmox.com Git - mirror_frr.git/blame - eigrpd/eigrp_metric.c
Merge pull request #10590 from donaldsharp/bgp_error_codes
[mirror_frr.git] / eigrpd / eigrp_metric.c
CommitLineData
e9f1847e
DS
1/*
2 * EIGRP Metric Math Functions.
3 * Copyright (C) 2013-2016
4 * Authors:
5 * Donnie Savage
6 *
7 * This file is part of GNU Zebra.
8 *
9 * GNU Zebra is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2, or (at your option) any
12 * later version.
13 *
14 * GNU Zebra is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; see the file COPYING; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
1f8031f7
DL
24#include <zebra.h>
25
e9f1847e
DS
26#include "eigrpd/eigrp_structs.h"
27#include "eigrpd/eigrpd.h"
28#include "eigrpd/eigrp_types.h"
29#include "eigrpd/eigrp_metric.h"
30
31eigrp_scaled_t eigrp_bandwidth_to_scaled(eigrp_bandwidth_t bandwidth)
32{
33 eigrp_bandwidth_t scaled = EIGRP_BANDWIDTH_MAX;
34
35 if (bandwidth != EIGRP_BANDWIDTH_MAX) {
36 scaled = (EIGRP_CLASSIC_SCALER * EIGRP_BANDWIDTH_SCALER);
37 scaled = scaled / bandwidth;
38
39 scaled = scaled ? scaled : EIGRP_BANDWIDTH_MIN;
40 }
41
42 scaled = (scaled < EIGRP_METRIC_MAX) ? scaled : EIGRP_METRIC_MAX;
43 return (eigrp_scaled_t)scaled;
44}
45
46eigrp_bandwidth_t eigrp_scaled_to_bandwidth(eigrp_scaled_t scaled)
47{
48 eigrp_bandwidth_t bandwidth = EIGRP_BANDWIDTH_MAX;
49
50 if (scaled != EIGRP_CLASSIC_MAX) {
51 bandwidth = (EIGRP_CLASSIC_SCALER * EIGRP_BANDWIDTH_SCALER);
52 bandwidth = scaled * bandwidth;
53 bandwidth = (bandwidth < EIGRP_METRIC_MAX)
54 ? bandwidth
55 : EIGRP_BANDWIDTH_MAX;
56 }
57
58 return bandwidth;
59}
60
61eigrp_scaled_t eigrp_delay_to_scaled(eigrp_delay_t delay)
62{
63 delay = delay ? delay : EIGRP_DELAY_MIN;
64 return delay * EIGRP_CLASSIC_SCALER;
65}
66
67eigrp_delay_t eigrp_scaled_to_delay(eigrp_scaled_t scaled)
68{
69 scaled = scaled / EIGRP_CLASSIC_SCALER;
70 scaled = scaled ? scaled : EIGRP_DELAY_MIN;
71
72 return scaled;
73}
74
75eigrp_metric_t eigrp_calculate_metrics(struct eigrp *eigrp,
76 struct eigrp_metrics metric)
77{
78 eigrp_metric_t composite = 0;
79
80 if (metric.delay == EIGRP_MAX_METRIC)
81 return EIGRP_METRIC_MAX;
82
83 /*
84 * EIGRP Composite =
85 * {K1*BW+[(K2*BW)/(256-load)]+(K3*delay)}*{K5/(reliability+K4)}
86 */
87
88 if (eigrp->k_values[0])
89 composite += (eigrp->k_values[0] * metric.bandwidth);
90 if (eigrp->k_values[1])
91 composite += ((eigrp->k_values[1] * metric.bandwidth)
92 / (256 - metric.load));
93 if (eigrp->k_values[2])
94 composite += (eigrp->k_values[2] * metric.delay);
95 if (eigrp->k_values[3] && !eigrp->k_values[4])
96 composite *= eigrp->k_values[3];
97 if (!eigrp->k_values[3] && eigrp->k_values[4])
98 composite *= (eigrp->k_values[4] / metric.reliability);
99 if (eigrp->k_values[3] && eigrp->k_values[4])
100 composite *= ((eigrp->k_values[4] / metric.reliability)
101 + eigrp->k_values[3]);
102
103 composite =
104 (composite <= EIGRP_METRIC_MAX) ? composite : EIGRP_METRIC_MAX;
105
106 return composite;
107}
108
109eigrp_metric_t
110eigrp_calculate_total_metrics(struct eigrp *eigrp,
111 struct eigrp_route_descriptor *entry)
112{
113 struct eigrp_interface *ei = entry->ei;
114 eigrp_delay_t temp_delay;
115 eigrp_bandwidth_t bw;
116
117 entry->total_metric = entry->reported_metric;
118 temp_delay = entry->total_metric.delay
119 + eigrp_delay_to_scaled(ei->params.delay);
120
121 entry->total_metric.delay = temp_delay > EIGRP_METRIC_MAX_CLASSIC
122 ? EIGRP_METRIC_MAX_CLASSIC
123 : temp_delay;
124
125 bw = eigrp_bandwidth_to_scaled(ei->params.bandwidth);
126 entry->total_metric.bandwidth = entry->total_metric.bandwidth > bw
127 ? bw
128 : entry->total_metric.bandwidth;
129
130 return eigrp_calculate_metrics(eigrp, entry->total_metric);
131}
132
133bool eigrp_metrics_is_same(struct eigrp_metrics metric1,
134 struct eigrp_metrics metric2)
135{
136 if ((metric1.bandwidth == metric2.bandwidth)
137 && (metric1.delay == metric2.delay)
138 && (metric1.hop_count == metric2.hop_count)
139 && (metric1.load == metric2.load)
140 && (metric1.reliability == metric2.reliability)
141 && (metric1.mtu[0] == metric2.mtu[0])
142 && (metric1.mtu[1] == metric2.mtu[1])
143 && (metric1.mtu[2] == metric2.mtu[2])) {
144 return true;
145 }
146
147 return false; /* if different */
148}