jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 1 | /* |
| 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> |
| 26 | #include <net/ethernet.h> |
| 27 | |
| 28 | #include "log.h" |
| 29 | #include "hash.h" |
| 30 | #include "thread.h" |
| 31 | #include "linklist.h" |
| 32 | #include "vty.h" |
| 33 | #include "stream.h" |
| 34 | #include "if.h" |
| 35 | |
| 36 | #include "isisd/dict.h" |
| 37 | #include "isisd/isis_constants.h" |
| 38 | #include "isisd/isis_common.h" |
| 39 | #include "isisd/isis_misc.h" |
| 40 | #include "isisd/isis_flags.h" |
| 41 | #include "isisd/isis_circuit.h" |
| 42 | #include "isisd/isisd.h" |
| 43 | #include "isisd/isis_adjacency.h" |
| 44 | #include "isisd/isis_constants.h" |
| 45 | #include "isisd/isis_pdu.h" |
| 46 | #include "isisd/isis_tlv.h" |
| 47 | #include "isisd/isis_lsp.h" |
| 48 | #include "isisd/isis_dr.h" |
| 49 | #include "isisd/isis_events.h" |
| 50 | |
| 51 | extern struct isis *isis; |
| 52 | extern struct thread_master *master; |
| 53 | |
| 54 | char * |
| 55 | isis_disflag2string (int disflag) { |
| 56 | |
| 57 | switch (disflag) { |
| 58 | case ISIS_IS_NOT_DIS: |
| 59 | return "is not DIS"; |
| 60 | case ISIS_IS_DIS: |
| 61 | return "is DIS"; |
| 62 | case ISIS_WAS_DIS: |
| 63 | return "was DIS"; |
| 64 | default: |
| 65 | return "unknown DIS state"; |
| 66 | } |
| 67 | return NULL; /* not reached */ |
| 68 | } |
| 69 | |
| 70 | |
| 71 | |
| 72 | int |
| 73 | isis_run_dr_l1 (struct thread *thread) |
| 74 | { |
| 75 | struct isis_circuit *circuit; |
| 76 | |
| 77 | circuit = THREAD_ARG (thread); |
| 78 | assert (circuit); |
| 79 | |
| 80 | if (circuit->u.bc.run_dr_elect[0]) |
| 81 | zlog_warn ("isis_run_dr(): run_dr_elect already set for l1"); |
| 82 | |
hasso | efc1e72 | 2003-12-31 20:33:23 +0000 | [diff] [blame^] | 83 | circuit->u.bc.t_run_dr[0] = NULL; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 84 | circuit->u.bc.run_dr_elect[0] = 1; |
| 85 | |
| 86 | return ISIS_OK; |
| 87 | } |
| 88 | |
| 89 | int |
| 90 | isis_run_dr_l2 (struct thread *thread) |
| 91 | { |
| 92 | struct isis_circuit *circuit; |
| 93 | |
| 94 | circuit = THREAD_ARG (thread); |
| 95 | assert (circuit); |
| 96 | |
| 97 | if (circuit->u.bc.run_dr_elect[1]) |
| 98 | zlog_warn ("isis_run_dr(): run_dr_elect already set for l2"); |
| 99 | |
| 100 | |
hasso | efc1e72 | 2003-12-31 20:33:23 +0000 | [diff] [blame^] | 101 | circuit->u.bc.t_run_dr[1] = NULL; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 102 | circuit->u.bc.run_dr_elect[1] = 1; |
| 103 | |
| 104 | return ISIS_OK; |
| 105 | } |
| 106 | |
| 107 | int |
| 108 | isis_check_dr_change (struct isis_adjacency *adj, int level) |
| 109 | { |
| 110 | int i; |
| 111 | |
| 112 | if ( adj->dis_record[level-1].dis != |
| 113 | adj->dis_record[(1*ISIS_LEVELS) + level - 1].dis) |
| 114 | /* was there a DIS state transition ? */ |
| 115 | { |
| 116 | adj->dischanges[level-1]++; |
| 117 | /* ok rotate the history list through */ |
| 118 | for (i = DIS_RECORDS - 1; i > 0; i--) |
| 119 | { |
| 120 | adj->dis_record[(i * ISIS_LEVELS) + level - 1].dis = |
| 121 | adj->dis_record[((i-1) * ISIS_LEVELS) + level - 1].dis; |
| 122 | adj->dis_record[(i * ISIS_LEVELS) + level - 1].last_dis_change = |
| 123 | adj->dis_record[((i-1) * ISIS_LEVELS) + level - 1].last_dis_change; |
| 124 | } |
| 125 | } |
| 126 | return ISIS_OK; |
| 127 | } |
| 128 | |
| 129 | int |
| 130 | isis_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; |
| 139 | |
| 140 | own_prio = circuit->u.bc.priority[level - 1]; |
| 141 | adjdb = circuit->u.bc.adjdb[level - 1]; |
| 142 | |
| 143 | if (!adjdb) { |
| 144 | zlog_warn ("isis_dr_elect() adjdb == NULL"); |
| 145 | retval = ISIS_WARNING; |
| 146 | list_delete (list); |
| 147 | goto out; |
| 148 | } |
| 149 | isis_adj_build_up_list (adjdb, list); |
| 150 | |
| 151 | /* |
| 152 | * Loop the adjacencies and find the one with the biggest priority |
| 153 | */ |
| 154 | for (node = listhead (list); node; nextnode (node)) { |
| 155 | adj = getdata (node); |
| 156 | /* clear flag for show output */ |
| 157 | adj->dis_record[level-1].dis = ISIS_IS_NOT_DIS; |
| 158 | adj->dis_record[level-1].last_dis_change = time (NULL); |
| 159 | |
| 160 | if (adj->prio[level-1] > biggest_prio) { |
| 161 | biggest_prio = adj->prio[level-1]; |
| 162 | adj_dr = adj; |
| 163 | } else if (adj->prio[level-1] == biggest_prio) { |
| 164 | /* |
| 165 | * Comparison of MACs breaks a tie |
| 166 | */ |
| 167 | if (adj_dr) { |
| 168 | cmp_res = memcmp (adj_dr->snpa, adj->snpa, ETH_ALEN); |
| 169 | if (cmp_res < 0) { |
| 170 | adj_dr = adj; |
| 171 | } |
| 172 | if (cmp_res == 0) |
| 173 | zlog_warn ("isis_dr_elect(): multiple adjacencies with same SNPA"); |
| 174 | } else { |
| 175 | adj_dr = adj; |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | if (!adj_dr) { |
| 181 | /* |
| 182 | * Could not find the DR - means we are alone and thus the DR |
| 183 | */ |
| 184 | if ( !circuit->u.bc.is_dr[level - 1]) { |
| 185 | list_delete (list); |
| 186 | list = NULL; |
| 187 | return isis_dr_commence (circuit, level); |
| 188 | } |
| 189 | goto out; |
| 190 | } |
| 191 | |
| 192 | /* |
| 193 | * Now we have the DR adjacency, compare it to self |
| 194 | */ |
| 195 | if (adj_dr->prio[level-1] < own_prio || (adj_dr->prio[level-1] == own_prio && |
| 196 | memcmp (adj_dr->snpa, circuit->u.bc.snpa, |
| 197 | ETH_ALEN) < 0)) { |
| 198 | if (!circuit->u.bc.is_dr[level - 1]) { |
| 199 | /* |
| 200 | * We are the DR -> commence |
| 201 | */ |
| 202 | list_delete (list); |
| 203 | return isis_dr_commence (circuit, level); |
| 204 | } |
| 205 | } else { |
| 206 | |
| 207 | /* ok we have found the DIS - lets mark the adjacency */ |
| 208 | /* set flag for show output */ |
| 209 | adj_dr->dis_record[level - 1].dis = ISIS_IS_DIS; |
| 210 | adj_dr->dis_record[level - 1].last_dis_change = time(NULL); |
| 211 | |
| 212 | /* now loop through a second time to check if there has been a DIS change |
| 213 | * if yes rotate the history log |
| 214 | */ |
| 215 | |
| 216 | for (node = listhead (list); node; nextnode (node)) { |
| 217 | adj = getdata (node); |
| 218 | isis_check_dr_change(adj, level); |
| 219 | } |
| 220 | |
| 221 | /* |
| 222 | * We are not DR - if we were -> resign |
| 223 | */ |
| 224 | |
| 225 | if (circuit->u.bc.is_dr[level - 1]) { |
| 226 | list_delete (list); |
| 227 | return isis_dr_resign (circuit, level); |
| 228 | } |
| 229 | } |
| 230 | out: |
| 231 | if (list) |
| 232 | list_delete (list); |
| 233 | return retval; |
| 234 | } |
| 235 | |
| 236 | int |
| 237 | isis_dr_resign (struct isis_circuit *circuit, int level) |
| 238 | { |
| 239 | u_char id[ISIS_SYS_ID_LEN + 2]; |
| 240 | |
| 241 | zlog_info ("isis_dr_resign l%d", level); |
| 242 | |
| 243 | circuit->u.bc.is_dr[level - 1] = 0; |
| 244 | circuit->u.bc.run_dr_elect[level - 1] = 0; |
| 245 | if (circuit->u.bc.t_run_dr[level - 1]) { |
| 246 | thread_cancel (circuit->u.bc.t_run_dr[level - 1]); |
| 247 | circuit->u.bc.t_run_dr[level - 1] = NULL; |
| 248 | } |
| 249 | if (circuit->u.bc.t_refresh_pseudo_lsp[level - 1]) { |
| 250 | thread_cancel (circuit->u.bc.t_refresh_pseudo_lsp[level - 1]); |
| 251 | circuit->u.bc.t_refresh_pseudo_lsp[level - 1] = NULL; |
| 252 | } |
| 253 | |
| 254 | memcpy (id, isis->sysid, ISIS_SYS_ID_LEN); |
| 255 | LSP_PSEUDO_ID(id) = circuit->circuit_id; |
| 256 | LSP_FRAGMENT(id) = 0; |
| 257 | lsp_purge_dr (id, circuit, level); |
| 258 | |
| 259 | if (level == 1) { |
| 260 | memset (circuit->u.bc.l1_desig_is, 0, ISIS_SYS_ID_LEN + 1); |
| 261 | |
| 262 | if (circuit->t_send_csnp[0]) |
| 263 | thread_cancel (circuit->t_send_csnp[0]); |
| 264 | |
| 265 | circuit->u.bc.t_run_dr[0] = |
| 266 | thread_add_timer (master, isis_run_dr_l1, circuit, |
| 267 | 2 * circuit->hello_interval[1]); |
| 268 | |
| 269 | circuit->t_send_psnp[0] = |
| 270 | thread_add_timer (master, |
| 271 | send_l1_psnp, |
| 272 | circuit, |
| 273 | isis_jitter (circuit->psnp_interval[level - 1], |
| 274 | PSNP_JITTER)); |
| 275 | } else { |
| 276 | memset (circuit->u.bc.l2_desig_is, 0, ISIS_SYS_ID_LEN + 1); |
| 277 | |
hasso | efc1e72 | 2003-12-31 20:33:23 +0000 | [diff] [blame^] | 278 | if (circuit->t_send_csnp[1]) |
| 279 | thread_cancel (circuit->t_send_csnp[1]); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 280 | |
| 281 | circuit->u.bc.t_run_dr[1] = |
| 282 | thread_add_timer (master, isis_run_dr_l2, circuit, |
| 283 | 2 * circuit->hello_interval[1]); |
| 284 | circuit->t_send_psnp[1] = |
| 285 | thread_add_timer (master, |
| 286 | send_l2_psnp, |
| 287 | circuit, |
| 288 | isis_jitter (circuit->psnp_interval[level - 1], |
| 289 | PSNP_JITTER)); |
| 290 | } |
| 291 | |
| 292 | thread_add_event (master, isis_event_dis_status_change, circuit, 0); |
| 293 | |
| 294 | return ISIS_OK; |
| 295 | } |
| 296 | |
| 297 | int |
| 298 | isis_dr_commence (struct isis_circuit *circuit, int level) |
| 299 | { |
| 300 | u_char old_dr[ISIS_SYS_ID_LEN + 2]; |
| 301 | |
| 302 | zlog_info ("isis_dr_commence l%d", level); |
| 303 | |
| 304 | /* Lets keep a pause in DR election */ |
| 305 | circuit->u.bc.run_dr_elect[level - 1] = 0; |
| 306 | if (level == 1) |
| 307 | circuit->u.bc.t_run_dr[0] = |
| 308 | thread_add_timer (master, isis_run_dr_l1, circuit, |
| 309 | 2 * circuit->hello_multiplier[0] * |
| 310 | circuit->hello_interval[0]); |
| 311 | else |
| 312 | circuit->u.bc.t_run_dr[1] = |
| 313 | thread_add_timer (master, isis_run_dr_l2, circuit, |
| 314 | 2 * circuit->hello_multiplier[1] * |
| 315 | circuit->hello_interval[1]); |
| 316 | circuit->u.bc.is_dr[level - 1] = 1; |
| 317 | |
| 318 | if (level == 1) { |
| 319 | memcpy (old_dr, circuit->u.bc.l1_desig_is, ISIS_SYS_ID_LEN + 1); |
| 320 | LSP_FRAGMENT (old_dr) = 0; |
| 321 | if (LSP_PSEUDO_ID(old_dr)) { |
| 322 | /* there was a dr elected, purge its LSPs from the db */ |
| 323 | lsp_purge_dr (old_dr, circuit, level); |
| 324 | } |
| 325 | memcpy (circuit->u.bc.l1_desig_is, isis->sysid, ISIS_SYS_ID_LEN); |
| 326 | *(circuit->u.bc.l1_desig_is + ISIS_SYS_ID_LEN) = circuit->circuit_id; |
| 327 | |
| 328 | assert (circuit->circuit_id); /* must be non-zero */ |
| 329 | /* if (circuit->t_send_l1_psnp) |
| 330 | thread_cancel (circuit->t_send_l1_psnp); */ |
| 331 | lsp_l1_pseudo_generate (circuit); |
| 332 | |
| 333 | circuit->u.bc.t_run_dr[0] = |
| 334 | thread_add_timer (master, isis_run_dr_l1, circuit, |
| 335 | 2 * circuit->hello_interval[0]); |
| 336 | |
| 337 | circuit->t_send_csnp[0] = thread_add_timer (master, |
| 338 | send_l1_csnp, |
| 339 | circuit, |
| 340 | isis_jitter |
| 341 | (circuit->csnp_interval[level-1], |
| 342 | CSNP_JITTER)); |
| 343 | } else { |
| 344 | memcpy (old_dr, circuit->u.bc.l2_desig_is, ISIS_SYS_ID_LEN + 1); |
| 345 | LSP_FRAGMENT (old_dr) = 0; |
| 346 | if (LSP_PSEUDO_ID(old_dr)) { |
| 347 | /* there was a dr elected, purge its LSPs from the db */ |
| 348 | lsp_purge_dr (old_dr, circuit, level); |
| 349 | } |
| 350 | memcpy (circuit->u.bc.l2_desig_is, isis->sysid, ISIS_SYS_ID_LEN); |
| 351 | *(circuit->u.bc.l2_desig_is + ISIS_SYS_ID_LEN) = circuit->circuit_id; |
| 352 | |
| 353 | assert (circuit->circuit_id); /* must be non-zero */ |
| 354 | /* if (circuit->t_send_l1_psnp) |
| 355 | thread_cancel (circuit->t_send_l1_psnp); */ |
| 356 | lsp_l2_pseudo_generate (circuit); |
| 357 | |
| 358 | circuit->u.bc.t_run_dr[1] = |
| 359 | thread_add_timer (master, isis_run_dr_l2, circuit, |
| 360 | 2 * circuit->hello_interval[1]); |
| 361 | |
| 362 | circuit->t_send_csnp[1] = |
| 363 | thread_add_timer (master, |
| 364 | send_l2_csnp, |
| 365 | circuit, |
| 366 | isis_jitter (circuit->csnp_interval[level-1], |
| 367 | CSNP_JITTER)); |
| 368 | |
| 369 | } |
| 370 | |
| 371 | thread_add_event (master, isis_event_dis_status_change, circuit, 0); |
| 372 | |
| 373 | return ISIS_OK; |
| 374 | } |
| 375 | |