blob: 0ee173e0711a1f6ef42a6637ad9a05e8bad597c0 [file] [log] [blame]
jardineb5d44e2003-12-23 08:09:43 +00001/*
2 * IS-IS Rout(e)ing protocol - isis_dr.c
3 * IS-IS designated router related routines
4 *
5 * Copyright (C) 2001,2002 Sampo Saaristo
6 * Tampere University of Technology
7 * Institute of Communications Engineering
8 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public Licenseas published by the Free
11 * Software Foundation; either version 2 of the License, or (at your option)
12 * any later version.
13 *
14 * This program is distributed in the hope that it will be useful,but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 * more details.
18
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 */
23
24
25#include <zebra.h>
jardineb5d44e2003-12-23 08:09:43 +000026
27#include "log.h"
28#include "hash.h"
29#include "thread.h"
30#include "linklist.h"
31#include "vty.h"
32#include "stream.h"
33#include "if.h"
34
35#include "isisd/dict.h"
36#include "isisd/isis_constants.h"
37#include "isisd/isis_common.h"
38#include "isisd/isis_misc.h"
39#include "isisd/isis_flags.h"
40#include "isisd/isis_circuit.h"
41#include "isisd/isisd.h"
42#include "isisd/isis_adjacency.h"
43#include "isisd/isis_constants.h"
44#include "isisd/isis_pdu.h"
45#include "isisd/isis_tlv.h"
46#include "isisd/isis_lsp.h"
47#include "isisd/isis_dr.h"
48#include "isisd/isis_events.h"
49
50extern struct isis *isis;
51extern struct thread_master *master;
52
hasso1cd80842004-10-07 20:07:40 +000053const char *
hassof390d2c2004-09-10 20:48:21 +000054isis_disflag2string (int disflag)
55{
jardineb5d44e2003-12-23 08:09:43 +000056
hassof390d2c2004-09-10 20:48:21 +000057 switch (disflag)
58 {
jardineb5d44e2003-12-23 08:09:43 +000059 case ISIS_IS_NOT_DIS:
60 return "is not DIS";
61 case ISIS_IS_DIS:
62 return "is DIS";
63 case ISIS_WAS_DIS:
64 return "was DIS";
hassof390d2c2004-09-10 20:48:21 +000065 default:
66 return "unknown DIS state";
67 }
68 return NULL; /* not reached */
jardineb5d44e2003-12-23 08:09:43 +000069}
70
jardineb5d44e2003-12-23 08:09:43 +000071int
72isis_run_dr_l1 (struct thread *thread)
73{
74 struct isis_circuit *circuit;
hassof390d2c2004-09-10 20:48:21 +000075
jardineb5d44e2003-12-23 08:09:43 +000076 circuit = THREAD_ARG (thread);
77 assert (circuit);
78
79 if (circuit->u.bc.run_dr_elect[0])
80 zlog_warn ("isis_run_dr(): run_dr_elect already set for l1");
hassof390d2c2004-09-10 20:48:21 +000081
hassoefc1e722003-12-31 20:33:23 +000082 circuit->u.bc.t_run_dr[0] = NULL;
jardineb5d44e2003-12-23 08:09:43 +000083 circuit->u.bc.run_dr_elect[0] = 1;
hassof390d2c2004-09-10 20:48:21 +000084
jardineb5d44e2003-12-23 08:09:43 +000085 return ISIS_OK;
86}
87
88int
89isis_run_dr_l2 (struct thread *thread)
90{
91 struct isis_circuit *circuit;
hassof390d2c2004-09-10 20:48:21 +000092
jardineb5d44e2003-12-23 08:09:43 +000093 circuit = THREAD_ARG (thread);
94 assert (circuit);
95
96 if (circuit->u.bc.run_dr_elect[1])
97 zlog_warn ("isis_run_dr(): run_dr_elect already set for l2");
hassof390d2c2004-09-10 20:48:21 +000098
99
100 circuit->u.bc.t_run_dr[1] = NULL;
jardineb5d44e2003-12-23 08:09:43 +0000101 circuit->u.bc.run_dr_elect[1] = 1;
hassof390d2c2004-09-10 20:48:21 +0000102
jardineb5d44e2003-12-23 08:09:43 +0000103 return ISIS_OK;
104}
105
106int
107isis_check_dr_change (struct isis_adjacency *adj, int level)
108{
109 int i;
110
hassof390d2c2004-09-10 20:48:21 +0000111 if (adj->dis_record[level - 1].dis !=
112 adj->dis_record[(1 * ISIS_LEVELS) + level - 1].dis)
113 /* was there a DIS state transition ? */
jardineb5d44e2003-12-23 08:09:43 +0000114 {
hassof390d2c2004-09-10 20:48:21 +0000115 adj->dischanges[level - 1]++;
jardineb5d44e2003-12-23 08:09:43 +0000116 /* ok rotate the history list through */
hassof390d2c2004-09-10 20:48:21 +0000117 for (i = DIS_RECORDS - 1; i > 0; i--)
jardineb5d44e2003-12-23 08:09:43 +0000118 {
hassof390d2c2004-09-10 20:48:21 +0000119 adj->dis_record[(i * ISIS_LEVELS) + level - 1].dis =
120 adj->dis_record[((i - 1) * ISIS_LEVELS) + level - 1].dis;
121 adj->dis_record[(i * ISIS_LEVELS) + level - 1].last_dis_change =
122 adj->dis_record[((i - 1) * ISIS_LEVELS) + level -
123 1].last_dis_change;
jardineb5d44e2003-12-23 08:09:43 +0000124 }
125 }
126 return ISIS_OK;
127}
128
hassof390d2c2004-09-10 20:48:21 +0000129int
jardineb5d44e2003-12-23 08:09:43 +0000130isis_dr_elect (struct isis_circuit *circuit, int level)
131{
132 struct list *adjdb;
133 struct listnode *node;
134 struct isis_adjacency *adj, *adj_dr = NULL;
135 struct list *list = list_new ();
136 u_char own_prio;
137 int biggest_prio = -1;
138 int cmp_res, retval = ISIS_OK;
hassof390d2c2004-09-10 20:48:21 +0000139
jardineb5d44e2003-12-23 08:09:43 +0000140 own_prio = circuit->u.bc.priority[level - 1];
141 adjdb = circuit->u.bc.adjdb[level - 1];
142
hassof390d2c2004-09-10 20:48:21 +0000143 if (!adjdb)
144 {
145 zlog_warn ("isis_dr_elect() adjdb == NULL");
146 retval = ISIS_WARNING;
147 list_delete (list);
148 goto out;
149 }
jardineb5d44e2003-12-23 08:09:43 +0000150 isis_adj_build_up_list (adjdb, list);
151
152 /*
153 * Loop the adjacencies and find the one with the biggest priority
154 */
hassof390d2c2004-09-10 20:48:21 +0000155 for (node = listhead (list); node; nextnode (node))
156 {
157 adj = getdata (node);
158 /* clear flag for show output */
159 adj->dis_record[level - 1].dis = ISIS_IS_NOT_DIS;
160 adj->dis_record[level - 1].last_dis_change = time (NULL);
jardineb5d44e2003-12-23 08:09:43 +0000161
hassof390d2c2004-09-10 20:48:21 +0000162 if (adj->prio[level - 1] > biggest_prio)
163 {
164 biggest_prio = adj->prio[level - 1];
165 adj_dr = adj;
jardineb5d44e2003-12-23 08:09:43 +0000166 }
hassof390d2c2004-09-10 20:48:21 +0000167 else if (adj->prio[level - 1] == biggest_prio)
168 {
169 /*
170 * Comparison of MACs breaks a tie
171 */
172 if (adj_dr)
173 {
174 cmp_res = memcmp (adj_dr->snpa, adj->snpa, ETH_ALEN);
175 if (cmp_res < 0)
176 {
177 adj_dr = adj;
178 }
179 if (cmp_res == 0)
180 zlog_warn
181 ("isis_dr_elect(): multiple adjacencies with same SNPA");
182 }
183 else
184 {
185 adj_dr = adj;
186 }
187 }
jardineb5d44e2003-12-23 08:09:43 +0000188 }
hassof390d2c2004-09-10 20:48:21 +0000189
190 if (!adj_dr)
191 {
192 /*
193 * Could not find the DR - means we are alone and thus the DR
194 */
195 if (!circuit->u.bc.is_dr[level - 1])
196 {
197 list_delete (list);
198 list = NULL;
199 return isis_dr_commence (circuit, level);
200 }
201 goto out;
jardineb5d44e2003-12-23 08:09:43 +0000202 }
jardineb5d44e2003-12-23 08:09:43 +0000203
204 /*
205 * Now we have the DR adjacency, compare it to self
206 */
hassof390d2c2004-09-10 20:48:21 +0000207 if (adj_dr->prio[level - 1] < own_prio
208 || (adj_dr->prio[level - 1] == own_prio
209 && memcmp (adj_dr->snpa, circuit->u.bc.snpa, ETH_ALEN) < 0))
210 {
211 if (!circuit->u.bc.is_dr[level - 1])
212 {
213 /*
hassod9c427b2004-09-21 14:23:01 +0000214 * We are the DR
hassof390d2c2004-09-10 20:48:21 +0000215 */
hassod9c427b2004-09-21 14:23:01 +0000216
217 /* rotate the history log */
218 for (node = listhead (list); node; nextnode (node))
219 {
220 adj = getdata (node);
221 isis_check_dr_change (adj, level);
222 }
223
224 /* commence */
hassof390d2c2004-09-10 20:48:21 +0000225 list_delete (list);
226 return isis_dr_commence (circuit, level);
227 }
228 }
229 else
230 {
231
232 /* ok we have found the DIS - lets mark the adjacency */
233 /* set flag for show output */
234 adj_dr->dis_record[level - 1].dis = ISIS_IS_DIS;
235 adj_dr->dis_record[level - 1].last_dis_change = time (NULL);
236
237 /* now loop through a second time to check if there has been a DIS change
238 * if yes rotate the history log
jardineb5d44e2003-12-23 08:09:43 +0000239 */
hassof390d2c2004-09-10 20:48:21 +0000240
241 for (node = listhead (list); node; nextnode (node))
242 {
243 adj = getdata (node);
244 isis_check_dr_change (adj, level);
245 }
246
247 /*
248 * We are not DR - if we were -> resign
249 */
250
251 if (circuit->u.bc.is_dr[level - 1])
252 {
253 list_delete (list);
254 return isis_dr_resign (circuit, level);
255 }
jardineb5d44e2003-12-23 08:09:43 +0000256 }
hassof390d2c2004-09-10 20:48:21 +0000257out:
jardineb5d44e2003-12-23 08:09:43 +0000258 if (list)
259 list_delete (list);
260 return retval;
261}
262
hassof390d2c2004-09-10 20:48:21 +0000263int
jardineb5d44e2003-12-23 08:09:43 +0000264isis_dr_resign (struct isis_circuit *circuit, int level)
265{
266 u_char id[ISIS_SYS_ID_LEN + 2];
hassof390d2c2004-09-10 20:48:21 +0000267
jardineb5d44e2003-12-23 08:09:43 +0000268 zlog_info ("isis_dr_resign l%d", level);
269
270 circuit->u.bc.is_dr[level - 1] = 0;
hassof390d2c2004-09-10 20:48:21 +0000271 circuit->u.bc.run_dr_elect[level - 1] = 0;
272 THREAD_TIMER_OFF (circuit->u.bc.t_run_dr[level - 1]);
273 THREAD_TIMER_OFF (circuit->u.bc.t_refresh_pseudo_lsp[level - 1]);
274
jardineb5d44e2003-12-23 08:09:43 +0000275 memcpy (id, isis->sysid, ISIS_SYS_ID_LEN);
hassof390d2c2004-09-10 20:48:21 +0000276 LSP_PSEUDO_ID (id) = circuit->circuit_id;
277 LSP_FRAGMENT (id) = 0;
jardineb5d44e2003-12-23 08:09:43 +0000278 lsp_purge_dr (id, circuit, level);
279
hassof390d2c2004-09-10 20:48:21 +0000280 if (level == 1)
281 {
282 memset (circuit->u.bc.l1_desig_is, 0, ISIS_SYS_ID_LEN + 1);
jardineb5d44e2003-12-23 08:09:43 +0000283
hassof390d2c2004-09-10 20:48:21 +0000284 THREAD_TIMER_OFF (circuit->t_send_csnp[0]);
jardineb5d44e2003-12-23 08:09:43 +0000285
hassof390d2c2004-09-10 20:48:21 +0000286 THREAD_TIMER_ON (master, circuit->u.bc.t_run_dr[0], isis_run_dr_l1,
287 circuit, 2 * circuit->hello_interval[1]);
288
289 THREAD_TIMER_ON (master, circuit->t_send_psnp[0], send_l1_psnp, circuit,
290 isis_jitter (circuit->psnp_interval[level - 1],
291 PSNP_JITTER));
292 }
293 else
294 {
295 memset (circuit->u.bc.l2_desig_is, 0, ISIS_SYS_ID_LEN + 1);
296
297 THREAD_TIMER_OFF (circuit->t_send_csnp[1]);
298
299 THREAD_TIMER_ON (master, circuit->u.bc.t_run_dr[1], isis_run_dr_l2,
300 circuit, 2 * circuit->hello_interval[1]);
301
302 THREAD_TIMER_ON (master, circuit->t_send_psnp[1], send_l2_psnp, circuit,
303 isis_jitter (circuit->psnp_interval[level - 1],
304 PSNP_JITTER));
305 }
306
jardineb5d44e2003-12-23 08:09:43 +0000307 thread_add_event (master, isis_event_dis_status_change, circuit, 0);
308
309 return ISIS_OK;
310}
311
hassof390d2c2004-09-10 20:48:21 +0000312int
jardineb5d44e2003-12-23 08:09:43 +0000313isis_dr_commence (struct isis_circuit *circuit, int level)
314{
315 u_char old_dr[ISIS_SYS_ID_LEN + 2];
hassof390d2c2004-09-10 20:48:21 +0000316
jardineb5d44e2003-12-23 08:09:43 +0000317 zlog_info ("isis_dr_commence l%d", level);
318
319 /* Lets keep a pause in DR election */
320 circuit->u.bc.run_dr_elect[level - 1] = 0;
hassof390d2c2004-09-10 20:48:21 +0000321 if (level == 1)
322 THREAD_TIMER_ON (master, circuit->u.bc.t_run_dr[0], isis_run_dr_l1,
hassoa211d652004-09-20 14:55:29 +0000323 circuit, 2 * circuit->hello_interval[0]);
hassof390d2c2004-09-10 20:48:21 +0000324 else
325 THREAD_TIMER_ON (master, circuit->u.bc.t_run_dr[1], isis_run_dr_l2,
hassoa211d652004-09-20 14:55:29 +0000326 circuit, 2 * circuit->hello_interval[1]);
jardineb5d44e2003-12-23 08:09:43 +0000327 circuit->u.bc.is_dr[level - 1] = 1;
328
hassof390d2c2004-09-10 20:48:21 +0000329 if (level == 1)
330 {
331 memcpy (old_dr, circuit->u.bc.l1_desig_is, ISIS_SYS_ID_LEN + 1);
332 LSP_FRAGMENT (old_dr) = 0;
333 if (LSP_PSEUDO_ID (old_dr))
334 {
335 /* there was a dr elected, purge its LSPs from the db */
336 lsp_purge_dr (old_dr, circuit, level);
337 }
338 memcpy (circuit->u.bc.l1_desig_is, isis->sysid, ISIS_SYS_ID_LEN);
339 *(circuit->u.bc.l1_desig_is + ISIS_SYS_ID_LEN) = circuit->circuit_id;
jardineb5d44e2003-12-23 08:09:43 +0000340
hassof390d2c2004-09-10 20:48:21 +0000341 assert (circuit->circuit_id); /* must be non-zero */
342 /* if (circuit->t_send_l1_psnp)
343 thread_cancel (circuit->t_send_l1_psnp); */
344 lsp_l1_pseudo_generate (circuit);
jardineb5d44e2003-12-23 08:09:43 +0000345
hassof390d2c2004-09-10 20:48:21 +0000346 THREAD_TIMER_OFF (circuit->u.bc.t_run_dr[0]);
347 THREAD_TIMER_ON (master, circuit->u.bc.t_run_dr[0], isis_run_dr_l1,
348 circuit, 2 * circuit->hello_interval[0]);
349
350 THREAD_TIMER_ON (master, circuit->t_send_csnp[0], send_l1_csnp, circuit,
351 isis_jitter (circuit->csnp_interval[level - 1],
352 CSNP_JITTER));
353
354 }
355 else
356 {
357 memcpy (old_dr, circuit->u.bc.l2_desig_is, ISIS_SYS_ID_LEN + 1);
358 LSP_FRAGMENT (old_dr) = 0;
359 if (LSP_PSEUDO_ID (old_dr))
360 {
361 /* there was a dr elected, purge its LSPs from the db */
362 lsp_purge_dr (old_dr, circuit, level);
363 }
364 memcpy (circuit->u.bc.l2_desig_is, isis->sysid, ISIS_SYS_ID_LEN);
365 *(circuit->u.bc.l2_desig_is + ISIS_SYS_ID_LEN) = circuit->circuit_id;
366
367 assert (circuit->circuit_id); /* must be non-zero */
368 /* if (circuit->t_send_l1_psnp)
369 thread_cancel (circuit->t_send_l1_psnp); */
370 lsp_l2_pseudo_generate (circuit);
371
372 THREAD_TIMER_OFF (circuit->u.bc.t_run_dr[1]);
373 THREAD_TIMER_ON (master, circuit->u.bc.t_run_dr[1], isis_run_dr_l2,
374 circuit, 2 * circuit->hello_interval[1]);
375
376 THREAD_TIMER_ON (master, circuit->t_send_csnp[1], send_l2_csnp, circuit,
377 isis_jitter (circuit->csnp_interval[level - 1],
378 CSNP_JITTER));
379 }
jardineb5d44e2003-12-23 08:09:43 +0000380
381 thread_add_event (master, isis_event_dis_status_change, circuit, 0);
hassof390d2c2004-09-10 20:48:21 +0000382
jardineb5d44e2003-12-23 08:09:43 +0000383 return ISIS_OK;
384}