ospf6_spf.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917
  1. /*
  2. * Copyright (C) 2003 Yasuhiro Ohara
  3. *
  4. * This file is part of GNU Zebra.
  5. *
  6. * GNU Zebra is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation; either version 2, or (at your option) any
  9. * later version.
  10. *
  11. * GNU Zebra is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with GNU Zebra; see the file COPYING. If not, write to the
  18. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  19. * Boston, MA 02111-1307, USA.
  20. */
  21. /* Shortest Path First calculation for OSPFv3 */
  22. #include <zebra.h>
  23. #include "log.h"
  24. #include "memory.h"
  25. #include "command.h"
  26. #include "vty.h"
  27. #include "prefix.h"
  28. #include "pqueue.h"
  29. #include "linklist.h"
  30. #include "thread.h"
  31. #include "ospf6_lsa.h"
  32. #include "ospf6_lsdb.h"
  33. #include "ospf6_route.h"
  34. #include "ospf6_area.h"
  35. #include "ospf6_spf.h"
  36. #include "ospf6_intra.h"
  37. #include "ospf6_interface.h"
  38. #include "ospf6d.h"
  39. #include "ospf6_abr.h"
  40. unsigned char conf_debug_ospf6_spf = 0;
  41. static int
  42. ospf6_vertex_cmp (void *a, void *b)
  43. {
  44. struct ospf6_vertex *va = (struct ospf6_vertex *) a;
  45. struct ospf6_vertex *vb = (struct ospf6_vertex *) b;
  46. /* ascending order */
  47. if (va->cost != vb->cost)
  48. return (va->cost - vb->cost);
  49. return (va->hops - vb->hops);
  50. }
  51. static int
  52. ospf6_vertex_id_cmp (void *a, void *b)
  53. {
  54. struct ospf6_vertex *va = (struct ospf6_vertex *) a;
  55. struct ospf6_vertex *vb = (struct ospf6_vertex *) b;
  56. int ret = 0;
  57. ret = ntohl (ospf6_linkstate_prefix_adv_router (&va->vertex_id)) -
  58. ntohl (ospf6_linkstate_prefix_adv_router (&vb->vertex_id));
  59. if (ret)
  60. return ret;
  61. ret = ntohl (ospf6_linkstate_prefix_id (&va->vertex_id)) -
  62. ntohl (ospf6_linkstate_prefix_id (&vb->vertex_id));
  63. return ret;
  64. }
  65. static struct ospf6_vertex *
  66. ospf6_vertex_create (struct ospf6_lsa *lsa)
  67. {
  68. struct ospf6_vertex *v;
  69. int i;
  70. v = (struct ospf6_vertex *)
  71. XMALLOC (MTYPE_OSPF6_VERTEX, sizeof (struct ospf6_vertex));
  72. /* type */
  73. if (ntohs (lsa->header->type) == OSPF6_LSTYPE_ROUTER)
  74. v->type = OSPF6_VERTEX_TYPE_ROUTER;
  75. else if (ntohs (lsa->header->type) == OSPF6_LSTYPE_NETWORK)
  76. v->type = OSPF6_VERTEX_TYPE_NETWORK;
  77. else
  78. assert (0);
  79. /* vertex_id */
  80. ospf6_linkstate_prefix (lsa->header->adv_router, lsa->header->id,
  81. &v->vertex_id);
  82. /* name */
  83. ospf6_linkstate_prefix2str (&v->vertex_id, v->name, sizeof (v->name));
  84. /* Associated LSA */
  85. v->lsa = lsa;
  86. /* capability bits + options */
  87. v->capability = *(u_char *)(OSPF6_LSA_HEADER_END (lsa->header));
  88. v->options[0] = *(u_char *)(OSPF6_LSA_HEADER_END (lsa->header) + 1);
  89. v->options[1] = *(u_char *)(OSPF6_LSA_HEADER_END (lsa->header) + 2);
  90. v->options[2] = *(u_char *)(OSPF6_LSA_HEADER_END (lsa->header) + 3);
  91. for (i = 0; i < OSPF6_MULTI_PATH_LIMIT; i++)
  92. ospf6_nexthop_clear (&v->nexthop[i]);
  93. v->parent = NULL;
  94. v->child_list = list_new ();
  95. v->child_list->cmp = ospf6_vertex_id_cmp;
  96. return v;
  97. }
  98. static void
  99. ospf6_vertex_delete (struct ospf6_vertex *v)
  100. {
  101. list_delete (v->child_list);
  102. XFREE (MTYPE_OSPF6_VERTEX, v);
  103. }
  104. static struct ospf6_lsa *
  105. ospf6_lsdesc_lsa (caddr_t lsdesc, struct ospf6_vertex *v)
  106. {
  107. struct ospf6_lsa *lsa;
  108. u_int16_t type = 0;
  109. u_int32_t id = 0, adv_router = 0;
  110. if (VERTEX_IS_TYPE (NETWORK, v))
  111. {
  112. type = htons (OSPF6_LSTYPE_ROUTER);
  113. id = htonl (0);
  114. adv_router = NETWORK_LSDESC_GET_NBR_ROUTERID (lsdesc);
  115. }
  116. else
  117. {
  118. if (ROUTER_LSDESC_IS_TYPE (POINTTOPOINT, lsdesc))
  119. {
  120. type = htons (OSPF6_LSTYPE_ROUTER);
  121. id = htonl (0);
  122. adv_router = ROUTER_LSDESC_GET_NBR_ROUTERID (lsdesc);
  123. }
  124. else if (ROUTER_LSDESC_IS_TYPE (TRANSIT_NETWORK, lsdesc))
  125. {
  126. type = htons (OSPF6_LSTYPE_NETWORK);
  127. id = htonl (ROUTER_LSDESC_GET_NBR_IFID (lsdesc));
  128. adv_router = ROUTER_LSDESC_GET_NBR_ROUTERID (lsdesc);
  129. }
  130. }
  131. lsa = ospf6_lsdb_lookup (type, id, adv_router, v->area->lsdb);
  132. if (IS_OSPF6_DEBUG_SPF (PROCESS))
  133. {
  134. char ibuf[16], abuf[16];
  135. inet_ntop (AF_INET, &id, ibuf, sizeof (ibuf));
  136. inet_ntop (AF_INET, &adv_router, abuf, sizeof (abuf));
  137. if (lsa)
  138. zlog_debug (" Link to: %s", lsa->name);
  139. else
  140. zlog_debug (" Link to: [%s Id:%s Adv:%s] No LSA",
  141. ospf6_lstype_name (type), ibuf, abuf);
  142. }
  143. return lsa;
  144. }
  145. static char *
  146. ospf6_lsdesc_backlink (struct ospf6_lsa *lsa,
  147. caddr_t lsdesc, struct ospf6_vertex *v)
  148. {
  149. caddr_t backlink, found = NULL;
  150. int size;
  151. size = (OSPF6_LSA_IS_TYPE (ROUTER, lsa) ?
  152. sizeof (struct ospf6_router_lsdesc) :
  153. sizeof (struct ospf6_network_lsdesc));
  154. for (backlink = OSPF6_LSA_HEADER_END (lsa->header) + 4;
  155. backlink + size <= OSPF6_LSA_END (lsa->header); backlink += size)
  156. {
  157. assert (! (OSPF6_LSA_IS_TYPE (NETWORK, lsa) &&
  158. VERTEX_IS_TYPE (NETWORK, v)));
  159. if (OSPF6_LSA_IS_TYPE (NETWORK, lsa) &&
  160. NETWORK_LSDESC_GET_NBR_ROUTERID (backlink)
  161. == v->lsa->header->adv_router)
  162. found = backlink;
  163. else if (VERTEX_IS_TYPE (NETWORK, v) &&
  164. ROUTER_LSDESC_IS_TYPE (TRANSIT_NETWORK, backlink) &&
  165. ROUTER_LSDESC_GET_NBR_ROUTERID (backlink)
  166. == v->lsa->header->adv_router &&
  167. ROUTER_LSDESC_GET_NBR_IFID (backlink)
  168. == ntohl (v->lsa->header->id))
  169. found = backlink;
  170. else
  171. {
  172. if (! ROUTER_LSDESC_IS_TYPE (POINTTOPOINT, backlink) ||
  173. ! ROUTER_LSDESC_IS_TYPE (POINTTOPOINT, lsdesc))
  174. continue;
  175. if (ROUTER_LSDESC_GET_NBR_IFID (backlink) !=
  176. ROUTER_LSDESC_GET_IFID (lsdesc) ||
  177. ROUTER_LSDESC_GET_NBR_IFID (lsdesc) !=
  178. ROUTER_LSDESC_GET_IFID (backlink))
  179. continue;
  180. if (ROUTER_LSDESC_GET_NBR_ROUTERID (backlink) !=
  181. v->lsa->header->adv_router ||
  182. ROUTER_LSDESC_GET_NBR_ROUTERID (lsdesc) !=
  183. lsa->header->adv_router)
  184. continue;
  185. found = backlink;
  186. }
  187. }
  188. if (IS_OSPF6_DEBUG_SPF (PROCESS))
  189. zlog_debug (" Backlink %s", (found ? "OK" : "FAIL"));
  190. return found;
  191. }
  192. static void
  193. ospf6_nexthop_calc (struct ospf6_vertex *w, struct ospf6_vertex *v,
  194. caddr_t lsdesc)
  195. {
  196. int i;
  197. ifindex_t ifindex;
  198. struct ospf6_interface *oi;
  199. u_int16_t type;
  200. u_int32_t adv_router;
  201. struct ospf6_lsa *lsa;
  202. struct ospf6_link_lsa *link_lsa;
  203. char buf[64];
  204. assert (VERTEX_IS_TYPE (ROUTER, w));
  205. ifindex = (VERTEX_IS_TYPE (NETWORK, v) ? v->nexthop[0].ifindex :
  206. /* v is the local router & the interface_id is a local ifindex */
  207. (ifindex_t) ROUTER_LSDESC_GET_IFID (lsdesc));
  208. assert (ifindex >= 0);
  209. oi = ospf6_interface_lookup_by_ifindex (ifindex);
  210. if (oi == NULL)
  211. {
  212. if (IS_OSPF6_DEBUG_SPF (PROCESS))
  213. zlog_debug ("Can't find interface in SPF: ifindex %d", ifindex);
  214. return;
  215. }
  216. type = htons (OSPF6_LSTYPE_LINK);
  217. adv_router = (VERTEX_IS_TYPE (NETWORK, v) ?
  218. NETWORK_LSDESC_GET_NBR_ROUTERID (lsdesc) :
  219. ROUTER_LSDESC_GET_NBR_ROUTERID (lsdesc));
  220. i = 0;
  221. for (lsa = ospf6_lsdb_type_router_head (type, adv_router, oi->lsdb); lsa;
  222. lsa = ospf6_lsdb_type_router_next (type, adv_router, lsa))
  223. {
  224. if (VERTEX_IS_TYPE (ROUTER, v) &&
  225. htonl (ROUTER_LSDESC_GET_NBR_IFID (lsdesc)) != lsa->header->id)
  226. continue;
  227. link_lsa = (struct ospf6_link_lsa *) OSPF6_LSA_HEADER_END (lsa->header);
  228. if (IS_OSPF6_DEBUG_SPF (PROCESS))
  229. {
  230. inet_ntop (AF_INET6, &link_lsa->linklocal_addr, buf, sizeof (buf));
  231. zlog_debug (" nexthop %s from %s", buf, lsa->name);
  232. }
  233. if (i < OSPF6_MULTI_PATH_LIMIT)
  234. {
  235. memcpy (&w->nexthop[i].address, &link_lsa->linklocal_addr,
  236. sizeof (struct in6_addr));
  237. w->nexthop[i].ifindex = ifindex;
  238. i++;
  239. }
  240. }
  241. if (i == 0 && IS_OSPF6_DEBUG_SPF (PROCESS))
  242. zlog_debug ("No nexthop for %s found", w->name);
  243. }
  244. static int
  245. ospf6_spf_install (struct ospf6_vertex *v,
  246. struct ospf6_route_table *result_table)
  247. {
  248. struct ospf6_route *route;
  249. int i, j;
  250. struct ospf6_vertex *prev;
  251. if (IS_OSPF6_DEBUG_SPF (PROCESS))
  252. zlog_debug ("SPF install %s hops %d cost %d",
  253. v->name, v->hops, v->cost);
  254. route = ospf6_route_lookup (&v->vertex_id, result_table);
  255. if (route && route->path.cost < v->cost)
  256. {
  257. if (IS_OSPF6_DEBUG_SPF (PROCESS))
  258. zlog_debug (" already installed with lower cost (%d), ignore",
  259. route->path.cost);
  260. ospf6_vertex_delete (v);
  261. return -1;
  262. }
  263. else if (route && route->path.cost == v->cost)
  264. {
  265. if (IS_OSPF6_DEBUG_SPF (PROCESS))
  266. zlog_debug (" another path found, merge");
  267. for (i = 0; ospf6_nexthop_is_set (&v->nexthop[i]) &&
  268. i < OSPF6_MULTI_PATH_LIMIT; i++)
  269. {
  270. for (j = 0; j < OSPF6_MULTI_PATH_LIMIT; j++)
  271. {
  272. if (ospf6_nexthop_is_set (&route->nexthop[j]))
  273. {
  274. if (ospf6_nexthop_is_same (&route->nexthop[j],
  275. &v->nexthop[i]))
  276. break;
  277. else
  278. continue;
  279. }
  280. ospf6_nexthop_copy (&route->nexthop[j], &v->nexthop[i]);
  281. break;
  282. }
  283. }
  284. prev = (struct ospf6_vertex *) route->route_option;
  285. assert (prev->hops <= v->hops);
  286. ospf6_vertex_delete (v);
  287. return -1;
  288. }
  289. /* There should be no case where candidate being installed (variable
  290. "v") is closer than the one in the SPF tree (variable "route").
  291. In the case something has gone wrong with the behavior of
  292. Priority-Queue. */
  293. /* the case where the route exists already is handled and returned
  294. up to here. */
  295. assert (route == NULL);
  296. route = ospf6_route_create ();
  297. memcpy (&route->prefix, &v->vertex_id, sizeof (struct prefix));
  298. route->type = OSPF6_DEST_TYPE_LINKSTATE;
  299. route->path.type = OSPF6_PATH_TYPE_INTRA;
  300. route->path.origin.type = v->lsa->header->type;
  301. route->path.origin.id = v->lsa->header->id;
  302. route->path.origin.adv_router = v->lsa->header->adv_router;
  303. route->path.metric_type = 1;
  304. route->path.cost = v->cost;
  305. route->path.cost_e2 = v->hops;
  306. route->path.router_bits = v->capability;
  307. route->path.options[0] = v->options[0];
  308. route->path.options[1] = v->options[1];
  309. route->path.options[2] = v->options[2];
  310. for (i = 0; ospf6_nexthop_is_set (&v->nexthop[i]) &&
  311. i < OSPF6_MULTI_PATH_LIMIT; i++)
  312. ospf6_nexthop_copy (&route->nexthop[i], &v->nexthop[i]);
  313. if (v->parent)
  314. listnode_add_sort (v->parent->child_list, v);
  315. route->route_option = v;
  316. ospf6_route_add (route, result_table);
  317. return 0;
  318. }
  319. void
  320. ospf6_spf_table_finish (struct ospf6_route_table *result_table)
  321. {
  322. struct ospf6_route *route;
  323. struct ospf6_vertex *v;
  324. for (route = ospf6_route_head (result_table); route;
  325. route = ospf6_route_next (route))
  326. {
  327. v = (struct ospf6_vertex *) route->route_option;
  328. ospf6_vertex_delete (v);
  329. ospf6_route_remove (route, result_table);
  330. }
  331. }
  332. static const char *ospf6_spf_reason_str[] =
  333. {
  334. "R+",
  335. "R-",
  336. "N+",
  337. "N-",
  338. "L+",
  339. "L-",
  340. "R*",
  341. "N*",
  342. };
  343. void ospf6_spf_reason_string (unsigned int reason, char *buf, int size)
  344. {
  345. size_t bit;
  346. int len = 0;
  347. if (!buf)
  348. return;
  349. for (bit = 0; bit <= (sizeof(ospf6_spf_reason_str) / sizeof(char *)); bit++)
  350. {
  351. if ((reason & (1 << bit)) && (len < size))
  352. {
  353. len += snprintf((buf + len), (size - len), "%s%s",
  354. (len > 0) ? ", " : "", ospf6_spf_reason_str[bit]);
  355. }
  356. }
  357. }
  358. /* RFC2328 16.1. Calculating the shortest-path tree for an area */
  359. /* RFC2740 3.8.1. Calculating the shortest path tree for an area */
  360. void
  361. ospf6_spf_calculation (u_int32_t router_id,
  362. struct ospf6_route_table *result_table,
  363. struct ospf6_area *oa)
  364. {
  365. struct pqueue *candidate_list;
  366. struct ospf6_vertex *root, *v, *w;
  367. int i;
  368. int size;
  369. caddr_t lsdesc;
  370. struct ospf6_lsa *lsa;
  371. ospf6_spf_table_finish (result_table);
  372. /* Install the calculating router itself as the root of the SPF tree */
  373. /* construct root vertex */
  374. lsa = ospf6_lsdb_lookup (htons (OSPF6_LSTYPE_ROUTER), htonl (0),
  375. router_id, oa->lsdb);
  376. if (lsa == NULL)
  377. return;
  378. /* initialize */
  379. candidate_list = pqueue_create ();
  380. candidate_list->cmp = ospf6_vertex_cmp;
  381. root = ospf6_vertex_create (lsa);
  382. root->area = oa;
  383. root->cost = 0;
  384. root->hops = 0;
  385. root->nexthop[0].ifindex = 0; /* loopbak I/F is better ... */
  386. inet_pton (AF_INET6, "::1", &root->nexthop[0].address);
  387. /* Actually insert root to the candidate-list as the only candidate */
  388. pqueue_enqueue (root, candidate_list);
  389. /* Iterate until candidate-list becomes empty */
  390. while (candidate_list->size)
  391. {
  392. /* get closest candidate from priority queue */
  393. v = pqueue_dequeue (candidate_list);
  394. /* installing may result in merging or rejecting of the vertex */
  395. if (ospf6_spf_install (v, result_table) < 0)
  396. continue;
  397. /* Skip overloaded routers */
  398. if ((OSPF6_LSA_IS_TYPE (ROUTER, v->lsa) &&
  399. ospf6_router_is_stub_router (v->lsa)))
  400. continue;
  401. /* For each LS description in the just-added vertex V's LSA */
  402. size = (VERTEX_IS_TYPE (ROUTER, v) ?
  403. sizeof (struct ospf6_router_lsdesc) :
  404. sizeof (struct ospf6_network_lsdesc));
  405. for (lsdesc = OSPF6_LSA_HEADER_END (v->lsa->header) + 4;
  406. lsdesc + size <= OSPF6_LSA_END (v->lsa->header); lsdesc += size)
  407. {
  408. lsa = ospf6_lsdesc_lsa (lsdesc, v);
  409. if (lsa == NULL)
  410. continue;
  411. if (! ospf6_lsdesc_backlink (lsa, lsdesc, v))
  412. continue;
  413. w = ospf6_vertex_create (lsa);
  414. w->area = oa;
  415. w->parent = v;
  416. if (VERTEX_IS_TYPE (ROUTER, v))
  417. {
  418. w->cost = v->cost + ROUTER_LSDESC_GET_METRIC (lsdesc);
  419. w->hops = v->hops + (VERTEX_IS_TYPE (NETWORK, w) ? 0 : 1);
  420. }
  421. else /* NETWORK */
  422. {
  423. w->cost = v->cost;
  424. w->hops = v->hops + 1;
  425. }
  426. /* nexthop calculation */
  427. if (w->hops == 0)
  428. w->nexthop[0].ifindex = ROUTER_LSDESC_GET_IFID (lsdesc);
  429. else if (w->hops == 1 && v->hops == 0)
  430. ospf6_nexthop_calc (w, v, lsdesc);
  431. else
  432. {
  433. for (i = 0; ospf6_nexthop_is_set (&v->nexthop[i]) &&
  434. i < OSPF6_MULTI_PATH_LIMIT; i++)
  435. ospf6_nexthop_copy (&w->nexthop[i], &v->nexthop[i]);
  436. }
  437. /* add new candidate to the candidate_list */
  438. if (IS_OSPF6_DEBUG_SPF (PROCESS))
  439. zlog_debug (" New candidate: %s hops %d cost %d",
  440. w->name, w->hops, w->cost);
  441. pqueue_enqueue (w, candidate_list);
  442. }
  443. }
  444. pqueue_delete (candidate_list);
  445. oa->spf_calculation++;
  446. }
  447. static void
  448. ospf6_spf_log_database (struct ospf6_area *oa)
  449. {
  450. char *p, *end, buffer[256];
  451. struct listnode *node;
  452. struct ospf6_interface *oi;
  453. p = buffer;
  454. end = buffer + sizeof (buffer);
  455. snprintf (p, end - p, "SPF on DB (#LSAs):");
  456. p = (buffer + strlen (buffer) < end ? buffer + strlen (buffer) : end);
  457. snprintf (p, end - p, " Area %s: %d", oa->name, oa->lsdb->count);
  458. p = (buffer + strlen (buffer) < end ? buffer + strlen (buffer) : end);
  459. for (ALL_LIST_ELEMENTS_RO (oa->if_list, node, oi))
  460. {
  461. snprintf (p, end - p, " I/F %s: %d",
  462. oi->interface->name, oi->lsdb->count);
  463. p = (buffer + strlen (buffer) < end ? buffer + strlen (buffer) : end);
  464. }
  465. zlog_debug ("%s", buffer);
  466. }
  467. static int
  468. ospf6_spf_calculation_thread (struct thread *t)
  469. {
  470. struct ospf6_area *oa;
  471. struct ospf6 *ospf6;
  472. struct timeval start, end, runtime;
  473. struct listnode *node;
  474. struct ospf6_route *route;
  475. int areas_processed = 0;
  476. char rbuf[32];
  477. ospf6 = (struct ospf6 *)THREAD_ARG (t);
  478. ospf6->t_spf_calc = NULL;
  479. /* execute SPF calculation */
  480. quagga_gettime (QUAGGA_CLK_MONOTONIC, &start);
  481. for (ALL_LIST_ELEMENTS_RO(ospf6->area_list, node, oa))
  482. {
  483. if (oa == ospf6->backbone)
  484. continue;
  485. if (IS_OSPF6_DEBUG_SPF (PROCESS))
  486. zlog_debug ("SPF calculation for Area %s", oa->name);
  487. if (IS_OSPF6_DEBUG_SPF (DATABASE))
  488. ospf6_spf_log_database (oa);
  489. ospf6_spf_calculation (ospf6->router_id, oa->spf_table, oa);
  490. ospf6_intra_route_calculation (oa);
  491. ospf6_intra_brouter_calculation (oa);
  492. areas_processed++;
  493. }
  494. if (ospf6->backbone)
  495. {
  496. if (IS_OSPF6_DEBUG_SPF (PROCESS))
  497. zlog_debug ("SPF calculation for Backbone area %s",
  498. ospf6->backbone->name);
  499. if (IS_OSPF6_DEBUG_SPF (DATABASE))
  500. ospf6_spf_log_database(ospf6->backbone);
  501. ospf6_spf_calculation(ospf6->router_id, ospf6->backbone->spf_table,
  502. ospf6->backbone);
  503. ospf6_intra_route_calculation(ospf6->backbone);
  504. ospf6_intra_brouter_calculation(ospf6->backbone);
  505. areas_processed++;
  506. }
  507. /* Redo summaries if required */
  508. for (route = ospf6_route_head (ospf6->route_table); route;
  509. route = ospf6_route_next (route))
  510. ospf6_abr_originate_summary(route);
  511. quagga_gettime (QUAGGA_CLK_MONOTONIC, &end);
  512. timersub (&end, &start, &runtime);
  513. ospf6->ts_spf_duration = runtime;
  514. ospf6_spf_reason_string(ospf6->spf_reason, rbuf, sizeof(rbuf));
  515. if (IS_OSPF6_DEBUG_SPF (PROCESS) || IS_OSPF6_DEBUG_SPF (TIME))
  516. zlog_debug ("SPF runtime: %lld sec %lld usec",
  517. (long long)runtime.tv_sec, (long long)runtime.tv_usec);
  518. zlog_info("SPF processing: # Areas: %d, SPF runtime: %lld sec %lld usec, "
  519. "Reason: %s\n", areas_processed,
  520. (long long)runtime.tv_sec, (long long)runtime.tv_usec,
  521. rbuf);
  522. ospf6->last_spf_reason = ospf6->spf_reason;
  523. ospf6_reset_spf_reason(ospf6);
  524. return 0;
  525. }
  526. /* Add schedule for SPF calculation. To avoid frequenst SPF calc, we
  527. set timer for SPF calc. */
  528. void
  529. ospf6_spf_schedule (struct ospf6 *ospf6, unsigned int reason)
  530. {
  531. unsigned long delay, elapsed, ht;
  532. struct timeval now, result;
  533. ospf6_set_spf_reason(ospf6, reason);
  534. if (IS_OSPF6_DEBUG_SPF(PROCESS) || IS_OSPF6_DEBUG_SPF (TIME))
  535. {
  536. char rbuf[32];
  537. ospf6_spf_reason_string(reason, rbuf, sizeof(rbuf));
  538. zlog_debug ("SPF: calculation timer scheduled (reason %s)", rbuf);
  539. }
  540. /* OSPF instance does not exist. */
  541. if (ospf6 == NULL)
  542. return;
  543. /* SPF calculation timer is already scheduled. */
  544. if (ospf6->t_spf_calc)
  545. {
  546. if (IS_OSPF6_DEBUG_SPF(PROCESS) || IS_OSPF6_DEBUG_SPF (TIME))
  547. zlog_debug ("SPF: calculation timer is already scheduled: %p",
  548. (void *)ospf6->t_spf_calc);
  549. return;
  550. }
  551. /* XXX Monotic timers: we only care about relative time here. */
  552. now = recent_relative_time ();
  553. timersub (&now, &ospf6->ts_spf, &result);
  554. elapsed = (result.tv_sec * 1000) + (result.tv_usec / 1000);
  555. ht = ospf6->spf_holdtime * ospf6->spf_hold_multiplier;
  556. if (ht > ospf6->spf_max_holdtime)
  557. ht = ospf6->spf_max_holdtime;
  558. /* Get SPF calculation delay time. */
  559. if (elapsed < ht)
  560. {
  561. /* Got an event within the hold time of last SPF. We need to
  562. * increase the hold_multiplier, if it's not already at/past
  563. * maximum value, and wasn't already increased..
  564. */
  565. if (ht < ospf6->spf_max_holdtime)
  566. ospf6->spf_hold_multiplier++;
  567. /* always honour the SPF initial delay */
  568. if ( (ht - elapsed) < ospf6->spf_delay)
  569. delay = ospf6->spf_delay;
  570. else
  571. delay = ht - elapsed;
  572. }
  573. else
  574. {
  575. /* Event is past required hold-time of last SPF */
  576. delay = ospf6->spf_delay;
  577. ospf6->spf_hold_multiplier = 1;
  578. }
  579. if (IS_OSPF6_DEBUG_SPF(PROCESS) || IS_OSPF6_DEBUG_SPF (TIME))
  580. zlog_debug ("SPF: calculation timer delay = %ld", delay);
  581. zlog_info ("SPF: Scheduled in %ld msec", delay);
  582. ospf6->t_spf_calc =
  583. thread_add_timer_msec (master, ospf6_spf_calculation_thread, ospf6, delay);
  584. }
  585. void
  586. ospf6_spf_display_subtree (struct vty *vty, const char *prefix, int rest,
  587. struct ospf6_vertex *v)
  588. {
  589. struct listnode *node, *nnode;
  590. struct ospf6_vertex *c;
  591. char *next_prefix;
  592. int len;
  593. int restnum;
  594. /* "prefix" is the space prefix of the display line */
  595. vty_out (vty, "%s+-%s [%d]%s", prefix, v->name, v->cost, VNL);
  596. len = strlen (prefix) + 4;
  597. next_prefix = (char *) malloc (len);
  598. if (next_prefix == NULL)
  599. {
  600. vty_out (vty, "malloc failed%s", VNL);
  601. return;
  602. }
  603. snprintf (next_prefix, len, "%s%s", prefix, (rest ? "| " : " "));
  604. restnum = listcount (v->child_list);
  605. for (ALL_LIST_ELEMENTS (v->child_list, node, nnode, c))
  606. {
  607. restnum--;
  608. ospf6_spf_display_subtree (vty, next_prefix, restnum, c);
  609. }
  610. free (next_prefix);
  611. }
  612. DEFUN (debug_ospf6_spf_process,
  613. debug_ospf6_spf_process_cmd,
  614. "debug ospf6 spf process",
  615. DEBUG_STR
  616. OSPF6_STR
  617. "Debug SPF Calculation\n"
  618. "Debug Detailed SPF Process\n"
  619. )
  620. {
  621. unsigned char level = 0;
  622. level = OSPF6_DEBUG_SPF_PROCESS;
  623. OSPF6_DEBUG_SPF_ON (level);
  624. return CMD_SUCCESS;
  625. }
  626. DEFUN (debug_ospf6_spf_time,
  627. debug_ospf6_spf_time_cmd,
  628. "debug ospf6 spf time",
  629. DEBUG_STR
  630. OSPF6_STR
  631. "Debug SPF Calculation\n"
  632. "Measure time taken by SPF Calculation\n"
  633. )
  634. {
  635. unsigned char level = 0;
  636. level = OSPF6_DEBUG_SPF_TIME;
  637. OSPF6_DEBUG_SPF_ON (level);
  638. return CMD_SUCCESS;
  639. }
  640. DEFUN (debug_ospf6_spf_database,
  641. debug_ospf6_spf_database_cmd,
  642. "debug ospf6 spf database",
  643. DEBUG_STR
  644. OSPF6_STR
  645. "Debug SPF Calculation\n"
  646. "Log number of LSAs at SPF Calculation time\n"
  647. )
  648. {
  649. unsigned char level = 0;
  650. level = OSPF6_DEBUG_SPF_DATABASE;
  651. OSPF6_DEBUG_SPF_ON (level);
  652. return CMD_SUCCESS;
  653. }
  654. DEFUN (no_debug_ospf6_spf_process,
  655. no_debug_ospf6_spf_process_cmd,
  656. "no debug ospf6 spf process",
  657. NO_STR
  658. DEBUG_STR
  659. OSPF6_STR
  660. "Quit Debugging SPF Calculation\n"
  661. "Quit Debugging Detailed SPF Process\n"
  662. )
  663. {
  664. unsigned char level = 0;
  665. level = OSPF6_DEBUG_SPF_PROCESS;
  666. OSPF6_DEBUG_SPF_OFF (level);
  667. return CMD_SUCCESS;
  668. }
  669. DEFUN (no_debug_ospf6_spf_time,
  670. no_debug_ospf6_spf_time_cmd,
  671. "no debug ospf6 spf time",
  672. NO_STR
  673. DEBUG_STR
  674. OSPF6_STR
  675. "Quit Debugging SPF Calculation\n"
  676. "Quit Measuring time taken by SPF Calculation\n"
  677. )
  678. {
  679. unsigned char level = 0;
  680. level = OSPF6_DEBUG_SPF_TIME;
  681. OSPF6_DEBUG_SPF_OFF (level);
  682. return CMD_SUCCESS;
  683. }
  684. DEFUN (no_debug_ospf6_spf_database,
  685. no_debug_ospf6_spf_database_cmd,
  686. "no debug ospf6 spf database",
  687. NO_STR
  688. DEBUG_STR
  689. OSPF6_STR
  690. "Debug SPF Calculation\n"
  691. "Quit Logging number of LSAs at SPF Calculation time\n"
  692. )
  693. {
  694. unsigned char level = 0;
  695. level = OSPF6_DEBUG_SPF_DATABASE;
  696. OSPF6_DEBUG_SPF_OFF (level);
  697. return CMD_SUCCESS;
  698. }
  699. static int
  700. ospf6_timers_spf_set (struct vty *vty, unsigned int delay,
  701. unsigned int hold,
  702. unsigned int max)
  703. {
  704. struct ospf6 *ospf = vty->index;
  705. ospf->spf_delay = delay;
  706. ospf->spf_holdtime = hold;
  707. ospf->spf_max_holdtime = max;
  708. return CMD_SUCCESS;
  709. }
  710. DEFUN (ospf6_timers_throttle_spf,
  711. ospf6_timers_throttle_spf_cmd,
  712. "timers throttle spf <0-600000> <0-600000> <0-600000>",
  713. "Adjust routing timers\n"
  714. "Throttling adaptive timer\n"
  715. "OSPF6 SPF timers\n"
  716. "Delay (msec) from first change received till SPF calculation\n"
  717. "Initial hold time (msec) between consecutive SPF calculations\n"
  718. "Maximum hold time (msec)\n")
  719. {
  720. unsigned int delay, hold, max;
  721. if (argc != 3)
  722. {
  723. vty_out (vty, "Insufficient arguments%s", VTY_NEWLINE);
  724. return CMD_WARNING;
  725. }
  726. VTY_GET_INTEGER_RANGE ("SPF delay timer", delay, argv[0], 0, 600000);
  727. VTY_GET_INTEGER_RANGE ("SPF hold timer", hold, argv[1], 0, 600000);
  728. VTY_GET_INTEGER_RANGE ("SPF max-hold timer", max, argv[2], 0, 600000);
  729. return ospf6_timers_spf_set (vty, delay, hold, max);
  730. }
  731. DEFUN (no_ospf6_timers_throttle_spf,
  732. no_ospf6_timers_throttle_spf_cmd,
  733. "no timers throttle spf",
  734. NO_STR
  735. "Adjust routing timers\n"
  736. "Throttling adaptive timer\n"
  737. "OSPF6 SPF timers\n")
  738. {
  739. return ospf6_timers_spf_set (vty,
  740. OSPF_SPF_DELAY_DEFAULT,
  741. OSPF_SPF_HOLDTIME_DEFAULT,
  742. OSPF_SPF_MAX_HOLDTIME_DEFAULT);
  743. }
  744. int
  745. config_write_ospf6_debug_spf (struct vty *vty)
  746. {
  747. if (IS_OSPF6_DEBUG_SPF (PROCESS))
  748. vty_out (vty, "debug ospf6 spf process%s", VNL);
  749. if (IS_OSPF6_DEBUG_SPF (TIME))
  750. vty_out (vty, "debug ospf6 spf time%s", VNL);
  751. if (IS_OSPF6_DEBUG_SPF (DATABASE))
  752. vty_out (vty, "debug ospf6 spf database%s", VNL);
  753. return 0;
  754. }
  755. void
  756. ospf6_spf_config_write (struct vty *vty)
  757. {
  758. if (ospf6->spf_delay != OSPF_SPF_DELAY_DEFAULT ||
  759. ospf6->spf_holdtime != OSPF_SPF_HOLDTIME_DEFAULT ||
  760. ospf6->spf_max_holdtime != OSPF_SPF_MAX_HOLDTIME_DEFAULT)
  761. vty_out (vty, " timers throttle spf %d %d %d%s",
  762. ospf6->spf_delay, ospf6->spf_holdtime,
  763. ospf6->spf_max_holdtime, VTY_NEWLINE);
  764. }
  765. void
  766. install_element_ospf6_debug_spf (void)
  767. {
  768. install_element (ENABLE_NODE, &debug_ospf6_spf_process_cmd);
  769. install_element (ENABLE_NODE, &debug_ospf6_spf_time_cmd);
  770. install_element (ENABLE_NODE, &debug_ospf6_spf_database_cmd);
  771. install_element (ENABLE_NODE, &no_debug_ospf6_spf_process_cmd);
  772. install_element (ENABLE_NODE, &no_debug_ospf6_spf_time_cmd);
  773. install_element (ENABLE_NODE, &no_debug_ospf6_spf_database_cmd);
  774. install_element (CONFIG_NODE, &debug_ospf6_spf_process_cmd);
  775. install_element (CONFIG_NODE, &debug_ospf6_spf_time_cmd);
  776. install_element (CONFIG_NODE, &debug_ospf6_spf_database_cmd);
  777. install_element (CONFIG_NODE, &no_debug_ospf6_spf_process_cmd);
  778. install_element (CONFIG_NODE, &no_debug_ospf6_spf_time_cmd);
  779. install_element (CONFIG_NODE, &no_debug_ospf6_spf_database_cmd);
  780. }
  781. void
  782. ospf6_spf_init (void)
  783. {
  784. install_element (OSPF6_NODE, &ospf6_timers_throttle_spf_cmd);
  785. install_element (OSPF6_NODE, &no_ospf6_timers_throttle_spf_cmd);
  786. }