]> git.proxmox.com Git - mirror_frr.git/blame - isisd/isis_adjacency.c
isisd: Replace isis_event_adjacency_state_change with a hook
[mirror_frr.git] / isisd / isis_adjacency.c
CommitLineData
eb5d44eb 1/*
d62a17ae 2 * IS-IS Rout(e)ing protocol - isis_adjacency.c
eb5d44eb 3 * handling of IS-IS adjacencies
4 *
5 * Copyright (C) 2001,2002 Sampo Saaristo
d62a17ae 6 * Tampere University of Technology
eb5d44eb 7 * Institute of Communications Engineering
8 *
d62a17ae 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)
eb5d44eb 12 * any later version.
13 *
d62a17ae 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
eb5d44eb 17 * more details.
896014f4
DL
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
eb5d44eb 22 */
23
eb5d44eb 24#include <zebra.h>
eb5d44eb 25
26#include "log.h"
27#include "memory.h"
28#include "hash.h"
29#include "vty.h"
30#include "linklist.h"
31#include "thread.h"
32#include "if.h"
33#include "stream.h"
34
35#include "isisd/dict.h"
eb5d44eb 36#include "isisd/isis_constants.h"
37#include "isisd/isis_common.h"
3f045a08 38#include "isisd/isis_flags.h"
eb5d44eb 39#include "isisd/isisd.h"
40#include "isisd/isis_circuit.h"
41#include "isisd/isis_adjacency.h"
42#include "isisd/isis_misc.h"
43#include "isisd/isis_dr.h"
44#include "isisd/isis_dynhn.h"
45#include "isisd/isis_pdu.h"
3f045a08
JB
46#include "isisd/isis_lsp.h"
47#include "isisd/isis_spf.h"
48#include "isisd/isis_events.h"
206f4aae 49#include "isisd/isis_mt.h"
42fe2621 50#include "isisd/isis_tlvs.h"
8e6fb83b 51#include "isisd/fabricd.h"
eb5d44eb 52
eb5d44eb 53extern struct isis *isis;
54
d7c0a89a 55static struct isis_adjacency *adj_alloc(const uint8_t *id)
eb5d44eb 56{
d62a17ae 57 struct isis_adjacency *adj;
eb5d44eb 58
d62a17ae 59 adj = XCALLOC(MTYPE_ISIS_ADJACENCY, sizeof(struct isis_adjacency));
60 memcpy(adj->sysid, id, ISIS_SYS_ID_LEN);
f390d2c7 61
d62a17ae 62 return adj;
eb5d44eb 63}
64
d7c0a89a 65struct isis_adjacency *isis_new_adj(const uint8_t *id, const uint8_t *snpa,
d62a17ae 66 int level, struct isis_circuit *circuit)
eb5d44eb 67{
d62a17ae 68 struct isis_adjacency *adj;
69 int i;
70
71 adj = adj_alloc(id); /* P2P kludge */
72
d62a17ae 73 if (snpa) {
74 memcpy(adj->snpa, snpa, ETH_ALEN);
75 } else {
76 memset(adj->snpa, ' ', ETH_ALEN);
eb5d44eb 77 }
eb5d44eb 78
d62a17ae 79 adj->circuit = circuit;
80 adj->level = level;
81 adj->flaps = 0;
82 adj->last_flap = time(NULL);
42fe2621 83 adj->threeway_state = ISIS_THREEWAY_DOWN;
d62a17ae 84 if (circuit->circ_type == CIRCUIT_T_BROADCAST) {
85 listnode_add(circuit->u.bc.adjdb[level - 1], adj);
86 adj->dischanges[level - 1] = 0;
87 for (i = 0; i < DIS_RECORDS;
88 i++) /* clear N DIS state change records */
89 {
90 adj->dis_record[(i * ISIS_LEVELS) + level - 1].dis =
91 ISIS_UNKNOWN_DIS;
92 adj->dis_record[(i * ISIS_LEVELS) + level - 1]
93 .last_dis_change = time(NULL);
94 }
95 }
96
97 return adj;
eb5d44eb 98}
99
d7c0a89a 100struct isis_adjacency *isis_adj_lookup(const uint8_t *sysid, struct list *adjdb)
eb5d44eb 101{
d62a17ae 102 struct isis_adjacency *adj;
103 struct listnode *node;
eb5d44eb 104
d62a17ae 105 for (ALL_LIST_ELEMENTS_RO(adjdb, node, adj))
106 if (memcmp(adj->sysid, sysid, ISIS_SYS_ID_LEN) == 0)
107 return adj;
f390d2c7 108
d62a17ae 109 return NULL;
eb5d44eb 110}
111
d7c0a89a 112struct isis_adjacency *isis_adj_lookup_snpa(const uint8_t *ssnpa,
d62a17ae 113 struct list *adjdb)
eb5d44eb 114{
d62a17ae 115 struct listnode *node;
116 struct isis_adjacency *adj;
eb5d44eb 117
d62a17ae 118 for (ALL_LIST_ELEMENTS_RO(adjdb, node, adj))
119 if (memcmp(adj->snpa, ssnpa, ETH_ALEN) == 0)
120 return adj;
f390d2c7 121
d62a17ae 122 return NULL;
eb5d44eb 123}
124
a5b5e946
CF
125DEFINE_HOOK(isis_adj_state_change_hook, (struct isis_adjacency *adj), (adj))
126
d62a17ae 127void isis_delete_adj(void *arg)
eb5d44eb 128{
d62a17ae 129 struct isis_adjacency *adj = arg;
3f045a08 130
d62a17ae 131 if (!adj)
132 return;
f390d2c7 133
d62a17ae 134 THREAD_TIMER_OFF(adj->t_expire);
a5b5e946
CF
135 if (adj->adj_state != ISIS_ADJ_DOWN) {
136 adj->adj_state = ISIS_ADJ_DOWN;
137 hook_call(isis_adj_state_change_hook, adj);
138 }
3f045a08 139
d62a17ae 140 /* remove from SPF trees */
141 spftree_area_adj_del(adj->circuit->area, adj);
13fb40ac 142
0c1bd758
CF
143 if (adj->area_addresses)
144 XFREE(MTYPE_ISIS_ADJACENCY_INFO, adj->area_addresses);
145 if (adj->ipv4_addresses)
146 XFREE(MTYPE_ISIS_ADJACENCY_INFO, adj->ipv4_addresses);
147 if (adj->ipv6_addresses)
148 XFREE(MTYPE_ISIS_ADJACENCY_INFO, adj->ipv6_addresses);
3f045a08 149
d62a17ae 150 adj_mt_finish(adj);
d8fba7d9 151
d62a17ae 152 XFREE(MTYPE_ISIS_ADJACENCY, adj);
153 return;
eb5d44eb 154}
155
d62a17ae 156static const char *adj_state2string(int state)
3f045a08
JB
157{
158
d62a17ae 159 switch (state) {
160 case ISIS_ADJ_INITIALIZING:
161 return "Initializing";
162 case ISIS_ADJ_UP:
163 return "Up";
164 case ISIS_ADJ_DOWN:
165 return "Down";
166 default:
167 return "Unknown";
168 }
169
170 return NULL; /* not reached */
3f045a08
JB
171}
172
42fe2621
CF
173void isis_adj_process_threeway(struct isis_adjacency *adj,
174 struct isis_threeway_adj *tw_adj,
175 enum isis_adj_usage adj_usage)
176{
177 enum isis_threeway_state next_tw_state = ISIS_THREEWAY_DOWN;
178
58e5d748 179 if (tw_adj && !adj->circuit->disable_threeway_adj) {
42fe2621
CF
180 if (tw_adj->state == ISIS_THREEWAY_DOWN) {
181 next_tw_state = ISIS_THREEWAY_INITIALIZING;
182 } else if (tw_adj->state == ISIS_THREEWAY_INITIALIZING) {
183 next_tw_state = ISIS_THREEWAY_UP;
184 } else if (tw_adj->state == ISIS_THREEWAY_UP) {
185 if (adj->threeway_state == ISIS_THREEWAY_DOWN)
186 next_tw_state = ISIS_THREEWAY_DOWN;
187 else
188 next_tw_state = ISIS_THREEWAY_UP;
189 }
190 } else {
191 next_tw_state = ISIS_THREEWAY_UP;
192 }
193
194 if (next_tw_state != adj->threeway_state) {
195 if (isis->debugs & DEBUG_ADJ_PACKETS) {
196 zlog_info("ISIS-Adj (%s): Threeway state change %s to %s",
197 adj->circuit->area->area_tag,
198 isis_threeway_state_name(adj->threeway_state),
199 isis_threeway_state_name(next_tw_state));
200 }
201 }
202
8e6fb83b
CF
203 if (next_tw_state != ISIS_THREEWAY_DOWN)
204 fabricd_initial_sync_hello(adj->circuit);
205
42fe2621
CF
206 if (next_tw_state == ISIS_THREEWAY_DOWN) {
207 isis_adj_state_change(adj, ISIS_ADJ_DOWN, "Neighbor restarted");
208 return;
209 }
210
211 if (next_tw_state == ISIS_THREEWAY_UP) {
212 if (adj->adj_state != ISIS_ADJ_UP) {
213 isis_adj_state_change(adj, ISIS_ADJ_UP, NULL);
214 adj->adj_usage = adj_usage;
215 }
216 }
217
218 adj->threeway_state = next_tw_state;
219}
220
d62a17ae 221void isis_adj_state_change(struct isis_adjacency *adj,
222 enum isis_adj_state new_state, const char *reason)
eb5d44eb 223{
d62a17ae 224 int old_state;
225 int level;
226 struct isis_circuit *circuit;
227 bool del;
228
229 old_state = adj->adj_state;
230 adj->adj_state = new_state;
231
232 circuit = adj->circuit;
233
234 if (isis->debugs & DEBUG_ADJ_PACKETS) {
235 zlog_debug("ISIS-Adj (%s): Adjacency state change %d->%d: %s",
236 circuit->area->area_tag, old_state, new_state,
237 reason ? reason : "unspecified");
238 }
239
240 if (circuit->area->log_adj_changes) {
241 const char *adj_name;
242 struct isis_dynhn *dyn;
243
244 dyn = dynhn_find_by_id(adj->sysid);
245 if (dyn)
af8ac8f9 246 adj_name = dyn->hostname;
d62a17ae 247 else
248 adj_name = sysid_print(adj->sysid);
249
250 zlog_info(
251 "%%ADJCHANGE: Adjacency to %s (%s) changed from %s to %s, %s",
252 adj_name, adj->circuit->interface->name,
253 adj_state2string(old_state),
254 adj_state2string(new_state),
255 reason ? reason : "unspecified");
256 }
257
258 if (circuit->circ_type == CIRCUIT_T_BROADCAST) {
259 del = false;
260 for (level = IS_LEVEL_1; level <= IS_LEVEL_2; level++) {
261 if ((adj->level & level) == 0)
262 continue;
263 if (new_state == ISIS_ADJ_UP) {
264 circuit->upadjcount[level - 1]++;
a5b5e946 265 hook_call(isis_adj_state_change_hook, adj);
d62a17ae 266 /* update counter & timers for debugging
267 * purposes */
268 adj->last_flap = time(NULL);
269 adj->flaps++;
270 } else if (new_state == ISIS_ADJ_DOWN) {
271 listnode_delete(circuit->u.bc.adjdb[level - 1],
272 adj);
58e16237 273
d62a17ae 274 circuit->upadjcount[level - 1]--;
58e16237 275 if (circuit->upadjcount[level - 1] == 0)
9b39405f 276 isis_tx_queue_clean(circuit->tx_queue);
58e16237 277
a5b5e946 278 hook_call(isis_adj_state_change_hook, adj);
d62a17ae 279 del = true;
280 }
281
282 if (circuit->u.bc.lan_neighs[level - 1]) {
283 list_delete_all_node(
284 circuit->u.bc.lan_neighs[level - 1]);
285 isis_adj_build_neigh_list(
286 circuit->u.bc.adjdb[level - 1],
287 circuit->u.bc.lan_neighs[level - 1]);
288 }
289
290 /* On adjacency state change send new pseudo LSP if we
291 * are the DR */
292 if (circuit->u.bc.is_dr[level - 1])
293 lsp_regenerate_schedule_pseudo(circuit, level);
294 }
295
296 if (del)
297 isis_delete_adj(adj);
298
d62a17ae 299 } else if (circuit->circ_type == CIRCUIT_T_P2P) {
300 del = false;
301 for (level = IS_LEVEL_1; level <= IS_LEVEL_2; level++) {
302 if ((adj->level & level) == 0)
303 continue;
304 if (new_state == ISIS_ADJ_UP) {
305 circuit->upadjcount[level - 1]++;
a5b5e946 306 hook_call(isis_adj_state_change_hook, adj);
d62a17ae 307
308 if (adj->sys_type == ISIS_SYSTYPE_UNKNOWN)
309 send_hello(circuit, level);
310
311 /* update counter & timers for debugging
312 * purposes */
313 adj->last_flap = time(NULL);
314 adj->flaps++;
315
8e6fb83b
CF
316 if (level == IS_LEVEL_1) {
317 thread_add_timer(master, send_l1_csnp,
318 circuit, 0,
319 &circuit->t_send_csnp[0]);
320 } else {
321 thread_add_timer(master, send_l2_csnp,
322 circuit, 0,
323 &circuit->t_send_csnp[1]);
324 }
d62a17ae 325 } else if (new_state == ISIS_ADJ_DOWN) {
326 if (adj->circuit->u.p2p.neighbor == adj)
327 adj->circuit->u.p2p.neighbor = NULL;
328 circuit->upadjcount[level - 1]--;
58e16237 329 if (circuit->upadjcount[level - 1] == 0)
9b39405f 330 isis_tx_queue_clean(circuit->tx_queue);
58e16237 331
a5b5e946 332 hook_call(isis_adj_state_change_hook, adj);
d62a17ae 333 del = true;
334 }
335 }
336
337 if (del)
338 isis_delete_adj(adj);
d62a17ae 339 }
340
341 return;
eb5d44eb 342}
343
344
d62a17ae 345void isis_adj_print(struct isis_adjacency *adj)
eb5d44eb 346{
d62a17ae 347 struct isis_dynhn *dyn;
d62a17ae 348
349 if (!adj)
350 return;
351 dyn = dynhn_find_by_id(adj->sysid);
352 if (dyn)
af8ac8f9 353 zlog_debug("%s", dyn->hostname);
d62a17ae 354
355 zlog_debug("SystemId %20s SNPA %s, level %d\nHolding Time %d",
356 sysid_print(adj->sysid), snpa_print(adj->snpa), adj->level,
357 adj->hold_time);
0c1bd758 358 if (adj->ipv4_address_count) {
d62a17ae 359 zlog_debug("IPv4 Address(es):");
0c1bd758
CF
360 for (unsigned int i = 0; i < adj->ipv4_address_count; i++)
361 zlog_debug("%s", inet_ntoa(adj->ipv4_addresses[i]));
d62a17ae 362 }
363
0c1bd758 364 if (adj->ipv6_address_count) {
d62a17ae 365 zlog_debug("IPv6 Address(es):");
0c1bd758
CF
366 for (unsigned int i = 0; i < adj->ipv6_address_count; i++) {
367 char buf[INET6_ADDRSTRLEN];
368 inet_ntop(AF_INET6, &adj->ipv6_addresses[i], buf,
369 sizeof(buf));
370 zlog_debug("%s", buf);
d62a17ae 371 }
f390d2c7 372 }
d62a17ae 373 zlog_debug("Speaks: %s", nlpid2string(&adj->nlpids));
eb5d44eb 374
d62a17ae 375 return;
eb5d44eb 376}
377
d62a17ae 378int isis_adj_expire(struct thread *thread)
eb5d44eb 379{
d62a17ae 380 struct isis_adjacency *adj;
eb5d44eb 381
d62a17ae 382 /*
383 * Get the adjacency
384 */
385 adj = THREAD_ARG(thread);
386 assert(adj);
387 adj->t_expire = NULL;
eb5d44eb 388
d62a17ae 389 /* trigger the adj expire event */
390 isis_adj_state_change(adj, ISIS_ADJ_DOWN, "holding time expired");
eb5d44eb 391
d62a17ae 392 return 0;
eb5d44eb 393}
394
eb5d44eb 395/*
3f045a08 396 * show isis neighbor [detail]
eb5d44eb 397 */
d62a17ae 398void isis_adj_print_vty(struct isis_adjacency *adj, struct vty *vty,
399 char detail)
eb5d44eb 400{
d62a17ae 401 time_t now;
402 struct isis_dynhn *dyn;
403 int level;
d62a17ae 404
405 dyn = dynhn_find_by_id(adj->sysid);
406 if (dyn)
af8ac8f9 407 vty_out(vty, " %-20s", dyn->hostname);
d62a17ae 408 else
409 vty_out(vty, " %-20s", sysid_print(adj->sysid));
410
411 if (detail == ISIS_UI_LEVEL_BRIEF) {
412 if (adj->circuit)
413 vty_out(vty, "%-12s", adj->circuit->interface->name);
414 else
415 vty_out(vty, "NULL circuit!");
416 vty_out(vty, "%-3u", adj->level); /* level */
417 vty_out(vty, "%-13s", adj_state2string(adj->adj_state));
418 now = time(NULL);
419 if (adj->last_upd)
420 vty_out(vty, "%-9llu",
421 (unsigned long long)adj->last_upd
422 + adj->hold_time - now);
423 else
424 vty_out(vty, "- ");
425 vty_out(vty, "%-10s", snpa_print(adj->snpa));
426 vty_out(vty, "\n");
f390d2c7 427 }
d62a17ae 428
429 if (detail == ISIS_UI_LEVEL_DETAIL) {
430 level = adj->level;
431 vty_out(vty, "\n");
432 if (adj->circuit)
433 vty_out(vty, " Interface: %s",
434 adj->circuit->interface->name);
435 else
436 vty_out(vty, " Interface: NULL circuit");
437 vty_out(vty, ", Level: %u", adj->level); /* level */
438 vty_out(vty, ", State: %s", adj_state2string(adj->adj_state));
439 now = time(NULL);
440 if (adj->last_upd)
441 vty_out(vty, ", Expires in %s",
442 time2string(adj->last_upd + adj->hold_time
443 - now));
444 else
445 vty_out(vty, ", Expires in %s",
446 time2string(adj->hold_time));
447 vty_out(vty, "\n");
448 vty_out(vty, " Adjacency flaps: %u", adj->flaps);
449 vty_out(vty, ", Last: %s ago",
450 time2string(now - adj->last_flap));
451 vty_out(vty, "\n");
452 vty_out(vty, " Circuit type: %s",
453 circuit_t2string(adj->circuit_t));
454 vty_out(vty, ", Speaks: %s", nlpid2string(&adj->nlpids));
455 vty_out(vty, "\n");
456 if (adj->mt_count != 1
457 || adj->mt_set[0] != ISIS_MT_IPV4_UNICAST) {
458 vty_out(vty, " Topologies:\n");
459 for (unsigned int i = 0; i < adj->mt_count; i++)
460 vty_out(vty, " %s\n",
461 isis_mtid2str(adj->mt_set[i]));
462 }
463 vty_out(vty, " SNPA: %s", snpa_print(adj->snpa));
464 if (adj->circuit
465 && (adj->circuit->circ_type == CIRCUIT_T_BROADCAST)) {
466 dyn = dynhn_find_by_id(adj->lanid);
467 if (dyn)
af8ac8f9 468 vty_out(vty, ", LAN id: %s.%02x", dyn->hostname,
d62a17ae 469 adj->lanid[ISIS_SYS_ID_LEN]);
470 else
471 vty_out(vty, ", LAN id: %s.%02x",
472 sysid_print(adj->lanid),
473 adj->lanid[ISIS_SYS_ID_LEN]);
474
475 vty_out(vty, "\n");
476 vty_out(vty, " LAN Priority: %u",
477 adj->prio[adj->level - 1]);
478
479 vty_out(vty, ", %s, DIS flaps: %u, Last: %s ago",
480 isis_disflag2string(
481 adj->dis_record[ISIS_LEVELS + level - 1]
482 .dis),
483 adj->dischanges[level - 1],
9d303b37
DL
484 time2string(now - (adj->dis_record[ISIS_LEVELS
485 + level - 1]
486 .last_dis_change)));
d62a17ae 487 }
488 vty_out(vty, "\n");
489
0c1bd758 490 if (adj->area_address_count) {
d62a17ae 491 vty_out(vty, " Area Address(es):\n");
0c1bd758
CF
492 for (unsigned int i = 0; i < adj->area_address_count;
493 i++) {
d62a17ae 494 vty_out(vty, " %s\n",
996c9314
LB
495 isonet_print(adj->area_addresses[i]
496 .area_addr,
497 adj->area_addresses[i]
498 .addr_len));
0c1bd758 499 }
d62a17ae 500 }
0c1bd758 501 if (adj->ipv4_address_count) {
d62a17ae 502 vty_out(vty, " IPv4 Address(es):\n");
0c1bd758
CF
503 for (unsigned int i = 0; i < adj->ipv4_address_count;
504 i++)
505 vty_out(vty, " %s\n",
996c9314 506 inet_ntoa(adj->ipv4_addresses[i]));
d62a17ae 507 }
0c1bd758 508 if (adj->ipv6_address_count) {
d62a17ae 509 vty_out(vty, " IPv6 Address(es):\n");
0c1bd758
CF
510 for (unsigned int i = 0; i < adj->ipv6_address_count;
511 i++) {
512 char buf[INET6_ADDRSTRLEN];
513 inet_ntop(AF_INET6, &adj->ipv6_addresses[i],
514 buf, sizeof(buf));
515 vty_out(vty, " %s\n", buf);
d62a17ae 516 }
517 }
518 vty_out(vty, "\n");
f390d2c7 519 }
d62a17ae 520 return;
eb5d44eb 521}
522
d62a17ae 523void isis_adj_build_neigh_list(struct list *adjdb, struct list *list)
eb5d44eb 524{
d62a17ae 525 struct isis_adjacency *adj;
526 struct listnode *node;
527
528 if (!list) {
529 zlog_warn("isis_adj_build_neigh_list(): NULL list");
530 return;
f390d2c7 531 }
532
d62a17ae 533 for (ALL_LIST_ELEMENTS_RO(adjdb, node, adj)) {
534 if (!adj) {
535 zlog_warn("isis_adj_build_neigh_list(): NULL adj");
536 return;
537 }
538
539 if ((adj->adj_state == ISIS_ADJ_UP
540 || adj->adj_state == ISIS_ADJ_INITIALIZING))
541 listnode_add(list, adj->snpa);
542 }
543 return;
eb5d44eb 544}
545
d62a17ae 546void isis_adj_build_up_list(struct list *adjdb, struct list *list)
eb5d44eb 547{
d62a17ae 548 struct isis_adjacency *adj;
549 struct listnode *node;
550
551 if (adjdb == NULL) {
552 zlog_warn("isis_adj_build_up_list(): adjacency DB is empty");
553 return;
554 }
555
556 if (!list) {
557 zlog_warn("isis_adj_build_up_list(): NULL list");
558 return;
f390d2c7 559 }
560
d62a17ae 561 for (ALL_LIST_ELEMENTS_RO(adjdb, node, adj)) {
562 if (!adj) {
563 zlog_warn("isis_adj_build_up_list(): NULL adj");
564 return;
565 }
f390d2c7 566
d62a17ae 567 if (adj->adj_state == ISIS_ADJ_UP)
568 listnode_add(list, adj);
569 }
570
571 return;
eb5d44eb 572}
d8fba7d9 573
d62a17ae 574int isis_adj_usage2levels(enum isis_adj_usage usage)
d8fba7d9 575{
d62a17ae 576 switch (usage) {
577 case ISIS_ADJ_LEVEL1:
578 return IS_LEVEL_1;
579 case ISIS_ADJ_LEVEL2:
580 return IS_LEVEL_2;
581 case ISIS_ADJ_LEVEL1AND2:
582 return IS_LEVEL_1 | IS_LEVEL_2;
583 default:
584 break;
585 }
586 return 0;
d8fba7d9 587}