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
|
Index: ospfd/ospf_spf.c
===================================================================
RCS file: /cvsroot/zebra/ospfd/ospf_spf.c,v
retrieving revision 1.124
diff -u -r1.124 ospf_spf.c
--- ospfd/ospf_spf.c 28 Mar 2003 19:55:28 -0000 1.124
+++ ospfd/ospf_spf.c 22 Feb 2006 03:03:26 -0000
@@ -29,6 +29,7 @@
#include "table.h"
#include "log.h"
#include "sockunion.h" /* for inet_ntop () */
+#include "pqueue.h"
#include "ospfd/ospfd.h"
#include "ospfd/ospf_interface.h"
@@ -114,7 +115,7 @@
}
void
-ospf_vertex_add_parent (struct vertex *v)
+ospf_vertex_connect_to_parent (struct vertex *v)
{
struct vertex_nexthop *nh;
listnode node;
@@ -458,48 +459,36 @@
}
}
-void
-ospf_install_candidate (list candidate, struct vertex *w)
+int
+ospf_vertex_cmp (void *a, void *b)
{
- listnode node;
- struct vertex *cw;
-
- if (list_isempty (candidate))
- {
- listnode_add (candidate, w);
- return;
- }
+ struct vertex *va = (struct vertex *) a;
+ struct vertex *vb = (struct vertex *) b;
+ /* ascending order */
+ return (va->distance - vb->distance);
+}
- /* Install vertex with sorting by distance. */
- for (node = listhead (candidate); node; nextnode (node))
- {
- cw = (struct vertex *) getdata (node);
- if (cw->distance > w->distance)
- {
- list_add_node_prev (candidate, node, w);
- break;
- }
- else if (node->next == NULL)
- {
- list_add_node_next (candidate, node, w);
- break;
- }
- }
+void
+ospf_install_candidate (struct pqueue *candidate_list, struct vertex *w)
+{
+ if (IS_DEBUG_OSPF_EVENT)
+ zlog_info ("candidate: type: %d id: %s p: %p distance: %lu",
+ w->type, inet_ntoa (w->id), w, (u_long)w->distance);
+ pqueue_enqueue (w, candidate_list);
}
/* RFC2328 Section 16.1 (2). */
void
ospf_spf_next (struct vertex *v, struct ospf_area *area,
- list candidate, struct route_table *rv,
+ struct pqueue *candidate_list, struct route_table *rv,
struct route_table *nv)
{
struct ospf_lsa *w_lsa = NULL;
- struct vertex *w, *cw;
+ struct vertex *w;
u_char *p;
u_char *lim;
struct router_lsa_link *l = NULL;
struct in_addr *r;
- listnode node;
int type = 0;
/* If this is a router-LSA, and bit V of the router-LSA (see Section
@@ -618,58 +607,22 @@
else
w->distance = v->distance;
- /* Is there already vertex W in candidate list? */
- node = ospf_vertex_lookup (candidate, w->id, w->type);
- if (node == NULL)
- {
- /* Calculate nexthop to W. */
- ospf_nexthop_calculation (area, v, w);
-
- ospf_install_candidate (candidate, w);
- }
- else
- {
- cw = (struct vertex *) getdata (node);
-
- /* if D is greater than. */
- if (cw->distance < w->distance)
- {
- ospf_vertex_free (w);
- continue;
- }
- /* equal to. */
- else if (cw->distance == w->distance)
- {
- /* Calculate nexthop to W. */
- ospf_nexthop_calculation (area, v, w);
- ospf_nexthop_merge (cw->nexthop, w->nexthop);
- list_delete_all_node (w->nexthop);
- ospf_vertex_free (w);
- }
- /* less than. */
- else
- {
- /* Calculate nexthop. */
- ospf_nexthop_calculation (area, v, w);
-
- /* Remove old vertex from candidate list. */
- ospf_vertex_free (cw);
- listnode_delete (candidate, cw);
+ /* calculate nexthop */
+ ospf_nexthop_calculation (area, v, w);
- /* Install new to candidate. */
- ospf_install_candidate (candidate, w);
- }
- }
+ /* install candidate */
+ ospf_install_candidate (candidate_list, w);
}
}
/* Add vertex V to SPF tree. */
-void
+struct vertex *
ospf_spf_register (struct vertex *v, struct route_table *rv,
struct route_table *nv)
{
struct prefix p;
struct route_node *rn;
+ struct vertex *cv;
p.family = AF_INET;
p.prefixlen = IPV4_MAX_BITLEN;
@@ -680,7 +633,39 @@
else
rn = route_node_get (nv, &p);
- rn->info = v;
+ cv = rn->info;
+
+ /* if the route does not exist yet, just install and return */
+ if (! cv)
+ {
+ rn->info = v;
+ return v;
+ }
+
+ /* if D is greater than. */
+ if (cv->distance < v->distance)
+ ospf_vertex_free (v);
+
+ /* equal to. */
+ else if (cv->distance == v->distance)
+ {
+ /* Merge nexthop to D. */
+ ospf_nexthop_merge (cv->nexthop, v->nexthop);
+ list_delete_all_node (v->nexthop);
+ ospf_vertex_free (v);
+ }
+
+ /* less than. */
+ else
+ {
+ /* As long as sorting in the candidate_list works,
+ cv->distance never become more than v->distance
+ (i.e. (cv->distance > v->distance) should not happen). */
+ assert (0);
+ }
+
+ /* the vertex is dropped anyway */
+ return NULL;
}
void
@@ -709,6 +694,7 @@
listnode cnode;
listnode nnode;
struct vertex_nexthop *nexthop;
+ struct vertex *w;
if (v->type == OSPF_VERTEX_ROUTER)
{
@@ -734,8 +720,8 @@
for (cnode = listhead (v->child); cnode; nextnode (cnode))
{
- v = getdata (cnode);
- ospf_spf_dump (v, i);
+ w = getdata (cnode);
+ ospf_spf_dump (w, i);
}
}
@@ -895,8 +881,7 @@
ospf_spf_calculate (struct ospf_area *area, struct route_table *new_table,
struct route_table *new_rtrs)
{
- list candidate;
- listnode node;
+ struct pqueue *candidate_list;
struct vertex *v;
struct route_table *rv;
struct route_table *nv;
@@ -925,7 +910,8 @@
nv = route_table_init ();
/* Clear the list of candidate vertices. */
- candidate = list_new ();
+ candidate_list = pqueue_create ();
+ candidate_list->cmp = ospf_vertex_cmp;
/* Initialize the shortest-path tree to only the root (which is the
router doing the calculation). */
@@ -939,29 +925,37 @@
for (;;)
{
- /* RFC2328 16.1. (2). */
- ospf_spf_next (v, area, candidate, rv, nv);
+ if (v != NULL)
+ {
+ if (IS_DEBUG_OSPF_EVENT)
+ zlog_info ("determined: type: %d id: %s p: %p distance: %lu",
+ v->type, inet_ntoa (v->id), v, (u_long)v->distance);
+
+ /* RFC2328 16.1. (2). */
+ ospf_spf_next (v, area, candidate_list, rv, nv);
+ }
/* RFC2328 16.1. (3). */
/* If at this step the candidate list is empty, the shortest-
path tree (of transit vertices) has been completely built and
this stage of the procedure terminates. */
- if (listcount (candidate) == 0)
+ if (candidate_list->size == 0)
break;
/* Otherwise, choose the vertex belonging to the candidate list
that is closest to the root, and add it to the shortest-path
tree (removing it from the candidate list in the
process). */
- node = listhead (candidate);
- v = getdata (node);
- ospf_vertex_add_parent (v);
-
- /* Reveve from the candidate list. */
- listnode_delete (candidate, v);
+ v = pqueue_dequeue (candidate_list);
/* Add to SPF tree. */
- ospf_spf_register (v, rv, nv);
+ v = ospf_spf_register (v, rv, nv);
+
+ /* Skip rest if the vertex is rejected to be installed in SPF tree */
+ if (v == NULL)
+ continue;
+
+ ospf_vertex_connect_to_parent (v);
/* Note that when there is a choice of vertices closest to the
root, network vertices must be chosen before router vertices
@@ -993,7 +987,7 @@
ospf_spf_route_free (nv);
/* Free candidate list */
- list_free (candidate);
+ pqueue_delete (candidate_list);
/* Increment SPF Calculation Counter. */
area->spf_calculation++;
|