]> git.proxmox.com Git - mirror_frr.git/blob - bgpd/bgp_nexthop.c
zebra: silence harmless ioctl warning when retrieving interface speed
[mirror_frr.git] / bgpd / bgp_nexthop.c
1 /* BGP nexthop scan
2 * Copyright (C) 2000 Kunihiro Ishiguro
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 along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include <zebra.h>
22
23 #include "command.h"
24 #include "thread.h"
25 #include "prefix.h"
26 #include "zclient.h"
27 #include "stream.h"
28 #include "network.h"
29 #include "log.h"
30 #include "memory.h"
31 #include "hash.h"
32 #include "jhash.h"
33 #include "nexthop.h"
34 #include "queue.h"
35 #include "filter.h"
36
37 #include "bgpd/bgpd.h"
38 #include "bgpd/bgp_table.h"
39 #include "bgpd/bgp_route.h"
40 #include "bgpd/bgp_attr.h"
41 #include "bgpd/bgp_nexthop.h"
42 #include "bgpd/bgp_nht.h"
43 #include "bgpd/bgp_debug.h"
44 #include "bgpd/bgp_damp.h"
45 #include "bgpd/bgp_fsm.h"
46 #include "bgpd/bgp_vty.h"
47
48 DEFINE_MTYPE_STATIC(BGPD, MARTIAN_STRING, "BGP Martian Address Intf String");
49
50 char *bnc_str(struct bgp_nexthop_cache *bnc, char *buf, int size)
51 {
52 prefix2str(&(bnc->node->p), buf, size);
53 return buf;
54 }
55
56 void bnc_nexthop_free(struct bgp_nexthop_cache *bnc)
57 {
58 nexthops_free(bnc->nexthop);
59 }
60
61 struct bgp_nexthop_cache *bnc_new(void)
62 {
63 struct bgp_nexthop_cache *bnc;
64
65 bnc = XCALLOC(MTYPE_BGP_NEXTHOP_CACHE,
66 sizeof(struct bgp_nexthop_cache));
67 LIST_INIT(&(bnc->paths));
68 return bnc;
69 }
70
71 void bnc_free(struct bgp_nexthop_cache *bnc)
72 {
73 bnc_nexthop_free(bnc);
74 XFREE(MTYPE_BGP_NEXTHOP_CACHE, bnc);
75 }
76
77 /* Reset and free all BGP nexthop cache. */
78 static void bgp_nexthop_cache_reset(struct bgp_table *table)
79 {
80 struct bgp_node *rn;
81 struct bgp_nexthop_cache *bnc;
82
83 for (rn = bgp_table_top(table); rn; rn = bgp_route_next(rn)) {
84 bnc = bgp_node_get_bgp_nexthop_info(rn);
85 if (!bnc)
86 continue;
87
88 while (!LIST_EMPTY(&(bnc->paths))) {
89 struct bgp_path_info *path = LIST_FIRST(&(bnc->paths));
90
91 path_nh_map(path, bnc, false);
92 }
93
94 bnc_free(bnc);
95 bgp_node_set_bgp_nexthop_info(rn, NULL);
96 bgp_unlock_node(rn);
97 }
98 }
99
100 static void *bgp_tip_hash_alloc(void *p)
101 {
102 const struct in_addr *val = (const struct in_addr *)p;
103 struct tip_addr *addr;
104
105 addr = XMALLOC(MTYPE_TIP_ADDR, sizeof(struct tip_addr));
106 addr->refcnt = 0;
107 addr->addr.s_addr = val->s_addr;
108
109 return addr;
110 }
111
112 static void bgp_tip_hash_free(void *addr)
113 {
114 XFREE(MTYPE_TIP_ADDR, addr);
115 }
116
117 static unsigned int bgp_tip_hash_key_make(void *p)
118 {
119 const struct tip_addr *addr = p;
120
121 return jhash_1word(addr->addr.s_addr, 0);
122 }
123
124 static bool bgp_tip_hash_cmp(const void *p1, const void *p2)
125 {
126 const struct tip_addr *addr1 = p1;
127 const struct tip_addr *addr2 = p2;
128
129 return addr1->addr.s_addr == addr2->addr.s_addr;
130 }
131
132 void bgp_tip_hash_init(struct bgp *bgp)
133 {
134 bgp->tip_hash = hash_create(bgp_tip_hash_key_make, bgp_tip_hash_cmp,
135 "BGP TIP hash");
136 }
137
138 void bgp_tip_hash_destroy(struct bgp *bgp)
139 {
140 if (bgp->tip_hash == NULL)
141 return;
142 hash_clean(bgp->tip_hash, bgp_tip_hash_free);
143 hash_free(bgp->tip_hash);
144 bgp->tip_hash = NULL;
145 }
146
147 void bgp_tip_add(struct bgp *bgp, struct in_addr *tip)
148 {
149 struct tip_addr tmp;
150 struct tip_addr *addr;
151
152 tmp.addr = *tip;
153
154 addr = hash_get(bgp->tip_hash, &tmp, bgp_tip_hash_alloc);
155 if (!addr)
156 return;
157
158 addr->refcnt++;
159 }
160
161 void bgp_tip_del(struct bgp *bgp, struct in_addr *tip)
162 {
163 struct tip_addr tmp;
164 struct tip_addr *addr;
165
166 tmp.addr = *tip;
167
168 addr = hash_lookup(bgp->tip_hash, &tmp);
169 /* may have been deleted earlier by bgp_interface_down() */
170 if (addr == NULL)
171 return;
172
173 addr->refcnt--;
174
175 if (addr->refcnt == 0) {
176 hash_release(bgp->tip_hash, addr);
177 XFREE(MTYPE_TIP_ADDR, addr);
178 }
179 }
180
181 /* BGP own address structure */
182 struct bgp_addr {
183 struct in_addr addr;
184 struct list *ifp_name_list;
185 };
186
187 static void show_address_entry(struct hash_backet *backet, void *args)
188 {
189 struct vty *vty = (struct vty *)args;
190 struct bgp_addr *addr = (struct bgp_addr *)backet->data;
191 char *name;
192 struct listnode *node;
193
194 vty_out(vty, "addr: %s, count: %d : ", inet_ntoa(addr->addr),
195 addr->ifp_name_list->count);
196
197 for (ALL_LIST_ELEMENTS_RO(addr->ifp_name_list, node, name)) {
198 vty_out(vty, " %s,", name);
199 }
200
201 vty_out(vty, "\n");
202 }
203
204 void bgp_nexthop_show_address_hash(struct vty *vty, struct bgp *bgp)
205 {
206 hash_iterate(bgp->address_hash,
207 (void (*)(struct hash_backet *, void *))show_address_entry,
208 vty);
209 }
210
211 static void bgp_address_hash_string_del(void *val)
212 {
213 char *data = val;
214
215 XFREE(MTYPE_MARTIAN_STRING, data);
216 }
217
218 static void *bgp_address_hash_alloc(void *p)
219 {
220 const struct in_addr *val = (const struct in_addr *)p;
221 struct bgp_addr *addr;
222
223 addr = XMALLOC(MTYPE_BGP_ADDR, sizeof(struct bgp_addr));
224 addr->addr.s_addr = val->s_addr;
225
226 addr->ifp_name_list = list_new();
227 addr->ifp_name_list->del = bgp_address_hash_string_del;
228
229 return addr;
230 }
231
232 static void bgp_address_hash_free(void *data)
233 {
234 struct bgp_addr *addr = data;
235
236 list_delete(&addr->ifp_name_list);
237 XFREE(MTYPE_BGP_ADDR, addr);
238 }
239
240 static unsigned int bgp_address_hash_key_make(void *p)
241 {
242 const struct bgp_addr *addr = p;
243
244 return jhash_1word(addr->addr.s_addr, 0);
245 }
246
247 static bool bgp_address_hash_cmp(const void *p1, const void *p2)
248 {
249 const struct bgp_addr *addr1 = p1;
250 const struct bgp_addr *addr2 = p2;
251
252 return addr1->addr.s_addr == addr2->addr.s_addr;
253 }
254
255 void bgp_address_init(struct bgp *bgp)
256 {
257 bgp->address_hash =
258 hash_create(bgp_address_hash_key_make, bgp_address_hash_cmp,
259 "BGP Address Hash");
260 }
261
262 void bgp_address_destroy(struct bgp *bgp)
263 {
264 if (bgp->address_hash == NULL)
265 return;
266 hash_clean(bgp->address_hash, bgp_address_hash_free);
267 hash_free(bgp->address_hash);
268 bgp->address_hash = NULL;
269 }
270
271 static void bgp_address_add(struct bgp *bgp, struct connected *ifc,
272 struct prefix *p)
273 {
274 struct bgp_addr tmp;
275 struct bgp_addr *addr;
276 struct listnode *node;
277 char *name;
278
279 tmp.addr = p->u.prefix4;
280
281 addr = hash_get(bgp->address_hash, &tmp, bgp_address_hash_alloc);
282
283 for (ALL_LIST_ELEMENTS_RO(addr->ifp_name_list, node, name)) {
284 if (strcmp(ifc->ifp->name, name) == 0)
285 break;
286 }
287 if (!node) {
288 name = XSTRDUP(MTYPE_MARTIAN_STRING, ifc->ifp->name);
289 listnode_add(addr->ifp_name_list, name);
290 }
291 }
292
293 static void bgp_address_del(struct bgp *bgp, struct connected *ifc,
294 struct prefix *p)
295 {
296 struct bgp_addr tmp;
297 struct bgp_addr *addr;
298 struct listnode *node;
299 char *name;
300
301 tmp.addr = p->u.prefix4;
302
303 addr = hash_lookup(bgp->address_hash, &tmp);
304 /* may have been deleted earlier by bgp_interface_down() */
305 if (addr == NULL)
306 return;
307
308 for (ALL_LIST_ELEMENTS_RO(addr->ifp_name_list, node, name)) {
309 if (strcmp(ifc->ifp->name, name) == 0)
310 break;
311 }
312
313 if (node) {
314 list_delete_node(addr->ifp_name_list, node);
315 XFREE(MTYPE_MARTIAN_STRING, name);
316 }
317
318 if (addr->ifp_name_list->count == 0) {
319 hash_release(bgp->address_hash, addr);
320 list_delete(&addr->ifp_name_list);
321 XFREE(MTYPE_BGP_ADDR, addr);
322 }
323 }
324
325
326 struct bgp_connected_ref {
327 unsigned int refcnt;
328 };
329
330 void bgp_connected_add(struct bgp *bgp, struct connected *ifc)
331 {
332 struct prefix p;
333 struct prefix *addr;
334 struct bgp_node *rn;
335 struct bgp_connected_ref *bc;
336 struct listnode *node, *nnode;
337 struct peer *peer;
338
339 addr = ifc->address;
340
341 p = *(CONNECTED_PREFIX(ifc));
342 if (addr->family == AF_INET) {
343 apply_mask_ipv4((struct prefix_ipv4 *)&p);
344
345 if (prefix_ipv4_any((struct prefix_ipv4 *)&p))
346 return;
347
348 bgp_address_add(bgp, ifc, addr);
349
350 rn = bgp_node_get(bgp->connected_table[AFI_IP],
351 (struct prefix *)&p);
352 bc = bgp_node_get_bgp_connected_ref_info(rn);
353 if (bc)
354 bc->refcnt++;
355 else {
356 bc = XCALLOC(MTYPE_BGP_CONN,
357 sizeof(struct bgp_connected_ref));
358 bc->refcnt = 1;
359 bgp_node_set_bgp_connected_ref_info(rn, bc);
360 }
361
362 for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) {
363 if (peer->conf_if
364 && (strcmp(peer->conf_if, ifc->ifp->name) == 0)
365 && peer->status != Established
366 && !CHECK_FLAG(peer->flags,
367 PEER_FLAG_IFPEER_V6ONLY)) {
368 if (peer_active(peer))
369 BGP_EVENT_ADD(peer, BGP_Stop);
370 BGP_EVENT_ADD(peer, BGP_Start);
371 }
372 }
373 } else if (addr->family == AF_INET6) {
374 apply_mask_ipv6((struct prefix_ipv6 *)&p);
375
376 if (IN6_IS_ADDR_UNSPECIFIED(&p.u.prefix6))
377 return;
378
379 if (IN6_IS_ADDR_LINKLOCAL(&p.u.prefix6))
380 return;
381
382 rn = bgp_node_get(bgp->connected_table[AFI_IP6],
383 (struct prefix *)&p);
384
385 bc = bgp_node_get_bgp_connected_ref_info(rn);
386 if (bc)
387 bc->refcnt++;
388 else {
389 bc = XCALLOC(MTYPE_BGP_CONN,
390 sizeof(struct bgp_connected_ref));
391 bc->refcnt = 1;
392 bgp_node_set_bgp_connected_ref_info(rn, bc);
393 }
394 }
395 }
396
397 void bgp_connected_delete(struct bgp *bgp, struct connected *ifc)
398 {
399 struct prefix p;
400 struct prefix *addr;
401 struct bgp_node *rn = NULL;
402 struct bgp_connected_ref *bc;
403
404 addr = ifc->address;
405
406 p = *(CONNECTED_PREFIX(ifc));
407 apply_mask(&p);
408 if (addr->family == AF_INET) {
409 if (prefix_ipv4_any((struct prefix_ipv4 *)&p))
410 return;
411
412 bgp_address_del(bgp, ifc, addr);
413
414 rn = bgp_node_lookup(bgp->connected_table[AFI_IP], &p);
415 } else if (addr->family == AF_INET6) {
416 if (IN6_IS_ADDR_UNSPECIFIED(&p.u.prefix6))
417 return;
418
419 if (IN6_IS_ADDR_LINKLOCAL(&p.u.prefix6))
420 return;
421
422 rn = bgp_node_lookup(bgp->connected_table[AFI_IP6],
423 (struct prefix *)&p);
424 }
425
426 if (!rn)
427 return;
428
429 bc = bgp_node_get_bgp_connected_ref_info(rn);
430 bc->refcnt--;
431 if (bc->refcnt == 0) {
432 XFREE(MTYPE_BGP_CONN, bc);
433 bgp_node_set_bgp_connected_ref_info(rn, NULL);
434 }
435 bgp_unlock_node(rn);
436 bgp_unlock_node(rn);
437 }
438
439 static void bgp_connected_cleanup(struct route_table *table,
440 struct route_node *rn)
441 {
442 struct bgp_connected_ref *bc;
443 struct bgp_node *bn = bgp_node_from_rnode(rn);
444
445 bc = bgp_node_get_bgp_connected_ref_info(bn);
446 if (!bc)
447 return;
448
449 bc->refcnt--;
450 if (bc->refcnt == 0) {
451 XFREE(MTYPE_BGP_CONN, bc);
452 bgp_node_set_bgp_connected_ref_info(bn, NULL);
453 }
454 }
455
456 int bgp_nexthop_self(struct bgp *bgp, struct in_addr nh_addr)
457 {
458 struct bgp_addr tmp, *addr;
459 struct tip_addr tmp_tip, *tip;
460
461 tmp.addr = nh_addr;
462
463 addr = hash_lookup(bgp->address_hash, &tmp);
464 if (addr)
465 return 1;
466
467 tmp_tip.addr = nh_addr;
468 tip = hash_lookup(bgp->tip_hash, &tmp_tip);
469 if (tip)
470 return 1;
471
472 return 0;
473 }
474
475 int bgp_multiaccess_check_v4(struct in_addr nexthop, struct peer *peer)
476 {
477 struct bgp_node *rn1;
478 struct bgp_node *rn2;
479 struct prefix p;
480 int ret;
481
482 p.family = AF_INET;
483 p.prefixlen = IPV4_MAX_BITLEN;
484 p.u.prefix4 = nexthop;
485
486 rn1 = bgp_node_match(peer->bgp->connected_table[AFI_IP], &p);
487 if (!rn1)
488 return 0;
489
490 p.family = AF_INET;
491 p.prefixlen = IPV4_MAX_BITLEN;
492 p.u.prefix4 = peer->su.sin.sin_addr;
493
494 rn2 = bgp_node_match(peer->bgp->connected_table[AFI_IP], &p);
495 if (!rn2) {
496 bgp_unlock_node(rn1);
497 return 0;
498 }
499
500 ret = (rn1 == rn2) ? 1 : 0;
501
502 bgp_unlock_node(rn1);
503 bgp_unlock_node(rn2);
504
505 return (ret);
506 }
507
508 int bgp_subgrp_multiaccess_check_v4(struct in_addr nexthop,
509 struct update_subgroup *subgrp)
510 {
511 struct bgp_node *rn1, *rn2;
512 struct peer_af *paf;
513 struct prefix p, np;
514 struct bgp *bgp;
515
516 np.family = AF_INET;
517 np.prefixlen = IPV4_MAX_BITLEN;
518 np.u.prefix4 = nexthop;
519
520 p.family = AF_INET;
521 p.prefixlen = IPV4_MAX_BITLEN;
522
523 bgp = SUBGRP_INST(subgrp);
524 rn1 = bgp_node_match(bgp->connected_table[AFI_IP], &np);
525 if (!rn1)
526 return 0;
527
528 SUBGRP_FOREACH_PEER (subgrp, paf) {
529 p.u.prefix4 = paf->peer->su.sin.sin_addr;
530
531 rn2 = bgp_node_match(bgp->connected_table[AFI_IP], &p);
532 if (rn1 == rn2) {
533 bgp_unlock_node(rn1);
534 bgp_unlock_node(rn2);
535 return 1;
536 }
537
538 if (rn2)
539 bgp_unlock_node(rn2);
540 }
541
542 bgp_unlock_node(rn1);
543 return 0;
544 }
545
546 static void bgp_show_nexthops_detail(struct vty *vty, struct bgp *bgp,
547 struct bgp_nexthop_cache *bnc)
548 {
549 char buf[PREFIX2STR_BUFFER];
550 struct nexthop *nexthop;
551
552 for (nexthop = bnc->nexthop; nexthop; nexthop = nexthop->next)
553 switch (nexthop->type) {
554 case NEXTHOP_TYPE_IPV6:
555 vty_out(vty, " gate %s\n",
556 inet_ntop(AF_INET6, &nexthop->gate.ipv6, buf,
557 sizeof(buf)));
558 break;
559 case NEXTHOP_TYPE_IPV6_IFINDEX:
560 vty_out(vty, " gate %s, if %s\n",
561 inet_ntop(AF_INET6, &nexthop->gate.ipv6, buf,
562 sizeof(buf)),
563 ifindex2ifname(nexthop->ifindex, bgp->vrf_id));
564 break;
565 case NEXTHOP_TYPE_IPV4:
566 vty_out(vty, " gate %s\n",
567 inet_ntop(AF_INET, &nexthop->gate.ipv4, buf,
568 sizeof(buf)));
569 break;
570 case NEXTHOP_TYPE_IFINDEX:
571 vty_out(vty, " if %s\n",
572 ifindex2ifname(nexthop->ifindex, bgp->vrf_id));
573 break;
574 case NEXTHOP_TYPE_IPV4_IFINDEX:
575 vty_out(vty, " gate %s, if %s\n",
576 inet_ntop(AF_INET, &nexthop->gate.ipv4, buf,
577 sizeof(buf)),
578 ifindex2ifname(nexthop->ifindex, bgp->vrf_id));
579 break;
580 case NEXTHOP_TYPE_BLACKHOLE:
581 vty_out(vty, " blackhole\n");
582 break;
583 default:
584 vty_out(vty, " invalid nexthop type %u\n",
585 nexthop->type);
586 }
587 }
588
589 static void bgp_show_nexthops(struct vty *vty, struct bgp *bgp, int detail,
590 bool import_table)
591 {
592 struct bgp_node *rn;
593 struct bgp_nexthop_cache *bnc;
594 char buf[PREFIX2STR_BUFFER];
595 time_t tbuf;
596 afi_t afi;
597 struct bgp_table **table;
598
599 if (import_table)
600 vty_out(vty, "Current BGP import check cache:\n");
601 else
602 vty_out(vty, "Current BGP nexthop cache:\n");
603 if (import_table)
604 table = bgp->import_check_table;
605 else
606 table = bgp->nexthop_cache_table;
607 for (afi = AFI_IP; afi < AFI_MAX; afi++) {
608 if (!table || !table[afi])
609 continue;
610 for (rn = bgp_table_top(table[afi]); rn;
611 rn = bgp_route_next(rn)) {
612 bnc = bgp_node_get_bgp_nexthop_info(rn);
613 if (!bnc)
614 continue;
615
616 if (CHECK_FLAG(bnc->flags, BGP_NEXTHOP_VALID)) {
617 vty_out(vty,
618 " %s valid [IGP metric %d], #paths %d\n",
619 inet_ntop(rn->p.family,
620 &rn->p.u.prefix, buf,
621 sizeof(buf)),
622 bnc->metric, bnc->path_count);
623
624 if (!detail)
625 continue;
626
627 bgp_show_nexthops_detail(vty, bgp, bnc);
628
629 } else {
630 vty_out(vty, " %s invalid\n",
631 inet_ntop(rn->p.family,
632 &rn->p.u.prefix, buf,
633 sizeof(buf)));
634 if (CHECK_FLAG(bnc->flags,
635 BGP_NEXTHOP_CONNECTED))
636 vty_out(vty, " Must be Connected\n");
637 if (!CHECK_FLAG(bnc->flags,
638 BGP_NEXTHOP_REGISTERED))
639 vty_out(vty, " Is not Registered\n");
640 }
641 tbuf = time(NULL) - (bgp_clock() - bnc->last_update);
642 vty_out(vty, " Last update: %s", ctime(&tbuf));
643 vty_out(vty, "\n");
644 }
645 }
646 }
647
648 static int show_ip_bgp_nexthop_table(struct vty *vty, const char *name,
649 int detail, bool import_table)
650 {
651 struct bgp *bgp;
652
653 if (name)
654 bgp = bgp_lookup_by_name(name);
655 else
656 bgp = bgp_get_default();
657 if (!bgp) {
658 vty_out(vty, "%% No such BGP instance exist\n");
659 return CMD_WARNING;
660 }
661
662 bgp_show_nexthops(vty, bgp, detail, import_table);
663
664 return CMD_SUCCESS;
665 }
666
667 static void bgp_show_all_instances_nexthops_vty(struct vty *vty)
668 {
669 struct listnode *node, *nnode;
670 struct bgp *bgp;
671
672 for (ALL_LIST_ELEMENTS(bm->bgp, node, nnode, bgp)) {
673 vty_out(vty, "\nInstance %s:\n",
674 (bgp->inst_type == BGP_INSTANCE_TYPE_DEFAULT)
675 ? VRF_DEFAULT_NAME
676 : bgp->name);
677 bgp_show_nexthops(vty, bgp, 0, false);
678 }
679 }
680
681 DEFUN (show_ip_bgp_nexthop,
682 show_ip_bgp_nexthop_cmd,
683 "show [ip] bgp [<view|vrf> VIEWVRFNAME] nexthop [detail]",
684 SHOW_STR
685 IP_STR
686 BGP_STR
687 BGP_INSTANCE_HELP_STR
688 "BGP nexthop table\n"
689 "Show detailed information\n")
690 {
691 int idx = 0;
692 char *vrf = NULL;
693
694 if (argv_find(argv, argc, "view", &idx)
695 || argv_find(argv, argc, "vrf", &idx))
696 vrf = argv[++idx]->arg;
697 int detail = argv_find(argv, argc, "detail", &idx) ? 1 : 0;
698
699 return show_ip_bgp_nexthop_table(vty, vrf, detail, false);
700 }
701
702 DEFUN (show_ip_bgp_import_check,
703 show_ip_bgp_import_check_cmd,
704 "show [ip] bgp [<view|vrf> VIEWVRFNAME] import-check-table [detail]",
705 SHOW_STR
706 IP_STR
707 BGP_STR
708 BGP_INSTANCE_HELP_STR
709 "BGP import check table\n"
710 "Show detailed information\n")
711 {
712 int idx = 0;
713 char *vrf = NULL;
714
715 if (argv_find(argv, argc, "view", &idx)
716 || argv_find(argv, argc, "vrf", &idx))
717 vrf = argv[++idx]->arg;
718 int detail = argv_find(argv, argc, "detail", &idx) ? 1 : 0;
719 return show_ip_bgp_nexthop_table(vty, vrf, detail, true);
720 }
721
722 DEFUN (show_ip_bgp_instance_all_nexthop,
723 show_ip_bgp_instance_all_nexthop_cmd,
724 "show [ip] bgp <view|vrf> all nexthop",
725 SHOW_STR
726 IP_STR
727 BGP_STR
728 BGP_INSTANCE_ALL_HELP_STR
729 "BGP nexthop table\n")
730 {
731 bgp_show_all_instances_nexthops_vty(vty);
732 return CMD_SUCCESS;
733 }
734
735 void bgp_scan_init(struct bgp *bgp)
736 {
737 afi_t afi;
738
739 for (afi = AFI_IP; afi < AFI_MAX; afi++) {
740 bgp->nexthop_cache_table[afi] =
741 bgp_table_init(bgp, afi, SAFI_UNICAST);
742 bgp->connected_table[afi] = bgp_table_init(bgp, afi,
743 SAFI_UNICAST);
744 bgp->import_check_table[afi] =
745 bgp_table_init(bgp, afi, SAFI_UNICAST);
746 }
747 }
748
749 void bgp_scan_vty_init(void)
750 {
751 install_element(VIEW_NODE, &show_ip_bgp_nexthop_cmd);
752 install_element(VIEW_NODE, &show_ip_bgp_import_check_cmd);
753 install_element(VIEW_NODE, &show_ip_bgp_instance_all_nexthop_cmd);
754 }
755
756 void bgp_scan_finish(struct bgp *bgp)
757 {
758 afi_t afi;
759
760 for (afi = AFI_IP; afi < AFI_MAX; afi++) {
761 /* Only the current one needs to be reset. */
762 bgp_nexthop_cache_reset(bgp->nexthop_cache_table[afi]);
763 bgp_table_unlock(bgp->nexthop_cache_table[afi]);
764 bgp->nexthop_cache_table[afi] = NULL;
765
766 bgp->connected_table[afi]->route_table->cleanup =
767 bgp_connected_cleanup;
768 bgp_table_unlock(bgp->connected_table[afi]);
769 bgp->connected_table[afi] = NULL;
770
771 bgp_table_unlock(bgp->import_check_table[afi]);
772 bgp->import_check_table[afi] = NULL;
773 }
774 }