]> git.proxmox.com Git - mirror_frr.git/blame - bgpd/bgp_debug.c
Merge pull request #2775 from donaldsharp/stream_speedup
[mirror_frr.git] / bgpd / bgp_debug.c
CommitLineData
718e3744 1/* BGP-4, BGP-4+ packet debug routine
896014f4
DL
2 * Copyright (C) 1996, 97, 99 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 */
718e3744 20
21#include <zebra.h>
22
5e4fa164 23#include <lib/version.h>
718e3744 24#include "prefix.h"
25#include "linklist.h"
26#include "stream.h"
27#include "command.h"
718e3744 28#include "log.h"
29#include "sockunion.h"
16286195 30#include "memory.h"
3f9c7369 31#include "queue.h"
039f3a34 32#include "filter.h"
718e3744 33
34#include "bgpd/bgpd.h"
35#include "bgpd/bgp_aspath.h"
36#include "bgpd/bgp_route.h"
37#include "bgpd/bgp_attr.h"
38#include "bgpd/bgp_debug.h"
39#include "bgpd/bgp_community.h"
3f9c7369 40#include "bgpd/bgp_updgrp.h"
906ad49b 41#include "bgpd/bgp_mplsvpn.h"
a4638609 42#include "bgpd/bgp_ecommunity.h"
b16031a2 43#include "bgpd/bgp_label.h"
44#include "bgpd/bgp_evpn.h"
81cf0de5
CS
45#include "bgpd/bgp_evpn_private.h"
46#include "bgpd/bgp_vty.h"
268e1b9b 47#include "bgpd/bgp_flowspec.h"
718e3744 48
0b2aa3a0 49unsigned long conf_bgp_debug_as4;
16286195 50unsigned long conf_bgp_debug_neighbor_events;
718e3744 51unsigned long conf_bgp_debug_events;
52unsigned long conf_bgp_debug_packet;
53unsigned long conf_bgp_debug_filter;
54unsigned long conf_bgp_debug_keepalive;
55unsigned long conf_bgp_debug_update;
9fbdd100 56unsigned long conf_bgp_debug_bestpath;
a39275d7 57unsigned long conf_bgp_debug_zebra;
cebb7440 58unsigned long conf_bgp_debug_allow_martians;
fb018d25 59unsigned long conf_bgp_debug_nht;
3f9c7369 60unsigned long conf_bgp_debug_update_groups;
ddb5b488 61unsigned long conf_bgp_debug_vpn;
268e1b9b 62unsigned long conf_bgp_debug_flowspec;
955bfd98 63unsigned long conf_bgp_debug_labelpool;
1a80fc0f 64unsigned long conf_bgp_debug_pbr;
718e3744 65
0b2aa3a0 66unsigned long term_bgp_debug_as4;
16286195 67unsigned long term_bgp_debug_neighbor_events;
718e3744 68unsigned long term_bgp_debug_events;
69unsigned long term_bgp_debug_packet;
70unsigned long term_bgp_debug_filter;
71unsigned long term_bgp_debug_keepalive;
72unsigned long term_bgp_debug_update;
9fbdd100 73unsigned long term_bgp_debug_bestpath;
a39275d7 74unsigned long term_bgp_debug_zebra;
cebb7440 75unsigned long term_bgp_debug_allow_martians;
fb018d25 76unsigned long term_bgp_debug_nht;
3f9c7369 77unsigned long term_bgp_debug_update_groups;
ddb5b488 78unsigned long term_bgp_debug_vpn;
268e1b9b 79unsigned long term_bgp_debug_flowspec;
955bfd98 80unsigned long term_bgp_debug_labelpool;
1a80fc0f 81unsigned long term_bgp_debug_pbr;
718e3744 82
16286195
DS
83struct list *bgp_debug_neighbor_events_peers = NULL;
84struct list *bgp_debug_keepalive_peers = NULL;
85struct list *bgp_debug_update_out_peers = NULL;
86struct list *bgp_debug_update_in_peers = NULL;
87struct list *bgp_debug_update_prefixes = NULL;
9fbdd100 88struct list *bgp_debug_bestpath_prefixes = NULL;
16286195
DS
89struct list *bgp_debug_zebra_prefixes = NULL;
90
718e3744 91/* messages for BGP-4 status */
d62a17ae 92const struct message bgp_status_msg[] = {{Idle, "Idle"},
93 {Connect, "Connect"},
94 {Active, "Active"},
95 {OpenSent, "OpenSent"},
96 {OpenConfirm, "OpenConfirm"},
97 {Established, "Established"},
98 {Clearing, "Clearing"},
99 {Deleted, "Deleted"},
100 {0}};
718e3744 101
102/* BGP message type string. */
d62a17ae 103const char *bgp_type_str[] = {NULL, "OPEN", "UPDATE",
104 "NOTIFICATION", "KEEPALIVE", "ROUTE-REFRESH",
105 "CAPABILITY"};
718e3744 106
107/* message for BGP-4 Notify */
d62a17ae 108static const struct message bgp_notify_msg[] = {
109 {BGP_NOTIFY_HEADER_ERR, "Message Header Error"},
110 {BGP_NOTIFY_OPEN_ERR, "OPEN Message Error"},
111 {BGP_NOTIFY_UPDATE_ERR, "UPDATE Message Error"},
112 {BGP_NOTIFY_HOLD_ERR, "Hold Timer Expired"},
113 {BGP_NOTIFY_FSM_ERR, "Neighbor Events Error"},
114 {BGP_NOTIFY_CEASE, "Cease"},
115 {BGP_NOTIFY_CAPABILITY_ERR, "CAPABILITY Message Error"},
116 {0}};
117
118static const struct message bgp_notify_head_msg[] = {
119 {BGP_NOTIFY_HEADER_NOT_SYNC, "/Connection Not Synchronized"},
120 {BGP_NOTIFY_HEADER_BAD_MESLEN, "/Bad Message Length"},
121 {BGP_NOTIFY_HEADER_BAD_MESTYPE, "/Bad Message Type"},
122 {0}};
123
124static const struct message bgp_notify_open_msg[] = {
125 {BGP_NOTIFY_SUBCODE_UNSPECIFIC, "/Unspecific"},
126 {BGP_NOTIFY_OPEN_UNSUP_VERSION, "/Unsupported Version Number"},
127 {BGP_NOTIFY_OPEN_BAD_PEER_AS, "/Bad Peer AS"},
128 {BGP_NOTIFY_OPEN_BAD_BGP_IDENT, "/Bad BGP Identifier"},
129 {BGP_NOTIFY_OPEN_UNSUP_PARAM, "/Unsupported Optional Parameter"},
130 {BGP_NOTIFY_OPEN_AUTH_FAILURE, "/Authentication Failure"},
131 {BGP_NOTIFY_OPEN_UNACEP_HOLDTIME, "/Unacceptable Hold Time"},
132 {BGP_NOTIFY_OPEN_UNSUP_CAPBL, "/Unsupported Capability"},
133 {0}};
134
135static const struct message bgp_notify_update_msg[] = {
136 {BGP_NOTIFY_SUBCODE_UNSPECIFIC, "/Unspecific"},
137 {BGP_NOTIFY_UPDATE_MAL_ATTR, "/Malformed Attribute List"},
138 {BGP_NOTIFY_UPDATE_UNREC_ATTR, "/Unrecognized Well-known Attribute"},
139 {BGP_NOTIFY_UPDATE_MISS_ATTR, "/Missing Well-known Attribute"},
140 {BGP_NOTIFY_UPDATE_ATTR_FLAG_ERR, "/Attribute Flags Error"},
141 {BGP_NOTIFY_UPDATE_ATTR_LENG_ERR, "/Attribute Length Error"},
142 {BGP_NOTIFY_UPDATE_INVAL_ORIGIN, "/Invalid ORIGIN Attribute"},
143 {BGP_NOTIFY_UPDATE_AS_ROUTE_LOOP, "/AS Routing Loop"},
144 {BGP_NOTIFY_UPDATE_INVAL_NEXT_HOP, "/Invalid NEXT_HOP Attribute"},
145 {BGP_NOTIFY_UPDATE_OPT_ATTR_ERR, "/Optional Attribute Error"},
146 {BGP_NOTIFY_UPDATE_INVAL_NETWORK, "/Invalid Network Field"},
147 {BGP_NOTIFY_UPDATE_MAL_AS_PATH, "/Malformed AS_PATH"},
148 {0}};
149
150static const struct message bgp_notify_cease_msg[] = {
151 {BGP_NOTIFY_SUBCODE_UNSPECIFIC, "/Unspecific"},
152 {BGP_NOTIFY_CEASE_MAX_PREFIX, "/Maximum Number of Prefixes Reached"},
153 {BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN, "/Administratively Shutdown"},
154 {BGP_NOTIFY_CEASE_PEER_UNCONFIG, "/Peer Unconfigured"},
155 {BGP_NOTIFY_CEASE_ADMIN_RESET, "/Administratively Reset"},
156 {BGP_NOTIFY_CEASE_CONNECT_REJECT, "/Connection Rejected"},
157 {BGP_NOTIFY_CEASE_CONFIG_CHANGE, "/Other Configuration Change"},
158 {BGP_NOTIFY_CEASE_COLLISION_RESOLUTION,
159 "/Connection collision resolution"},
160 {BGP_NOTIFY_CEASE_OUT_OF_RESOURCE, "/Out of Resource"},
161 {0}};
162
163static const struct message bgp_notify_capability_msg[] = {
164 {BGP_NOTIFY_SUBCODE_UNSPECIFIC, "/Unspecific"},
165 {BGP_NOTIFY_CAPABILITY_INVALID_ACTION, "/Invalid Action Value"},
166 {BGP_NOTIFY_CAPABILITY_INVALID_LENGTH, "/Invalid Capability Length"},
167 {BGP_NOTIFY_CAPABILITY_MALFORMED_CODE, "/Malformed Capability Value"},
168 {0}};
718e3744 169
170/* Origin strings. */
d62a17ae 171const char *bgp_origin_str[] = {"i", "e", "?"};
172const char *bgp_origin_long_str[] = {"IGP", "EGP", "incomplete"};
718e3744 173
81cf0de5
CS
174static int bgp_debug_print_evpn_prefix(struct vty *vty, const char *desc,
175 struct prefix *p);
16286195 176/* Given a string return a pointer the corresponding peer structure */
d62a17ae 177static struct peer *bgp_find_peer(struct vty *vty, const char *peer_str)
16286195 178{
d62a17ae 179 struct bgp *bgp = VTY_GET_CONTEXT(bgp);
180 int ret;
181 union sockunion su;
182 struct peer *peer;
183
184 if (!bgp) {
185 return NULL;
186 }
187 ret = str2sockunion(peer_str, &su);
188
189 /* 'swpX' string */
190 if (ret < 0) {
191 peer = peer_lookup_by_conf_if(bgp, peer_str);
192
193 if (!peer)
194 peer = peer_lookup_by_hostname(bgp, peer_str);
195
196 return peer;
197 } else
198 return peer_lookup(bgp, &su);
16286195
DS
199}
200
d62a17ae 201static void bgp_debug_list_free(struct list *list)
16286195 202{
d62a17ae 203 struct bgp_debug_filter *filter;
204 struct listnode *node, *nnode;
16286195 205
d62a17ae 206 if (list)
207 for (ALL_LIST_ELEMENTS(list, node, nnode, filter)) {
208 listnode_delete(list, filter);
16286195 209
d62a17ae 210 if (filter->p)
211 prefix_free(filter->p);
16286195 212
d62a17ae 213 if (filter->host)
214 XFREE(MTYPE_BGP_DEBUG_STR, filter->host);
16286195 215
d62a17ae 216 XFREE(MTYPE_BGP_DEBUG_FILTER, filter);
217 }
16286195
DS
218}
219
220/* Print the desc along with a list of peers/prefixes this debug is
221 * enabled for */
d62a17ae 222static void bgp_debug_list_print(struct vty *vty, const char *desc,
81cf0de5 223 struct list *list, uint8_t evpn_dbg)
d62a17ae 224{
225 struct bgp_debug_filter *filter;
226 struct listnode *node, *nnode;
81cf0de5 227 char buf[PREFIX2STR_BUFFER];
d62a17ae 228
229 vty_out(vty, "%s", desc);
230
231 if (list && !list_isempty(list)) {
232 vty_out(vty, " for");
233 for (ALL_LIST_ELEMENTS(list, node, nnode, filter)) {
234 if (filter->host)
235 vty_out(vty, " %s", filter->host);
236
81cf0de5
CS
237 if (filter->p) {
238 if (!evpn_dbg) {
239 vty_out(vty, " %s",
240 prefix2str(filter->p, buf,
241 sizeof(buf)));
242 } else {
243 if (filter->p->family == AF_EVPN)
244 bgp_debug_print_evpn_prefix(vty,
245 "", filter->p);
246 }
247 }
d62a17ae 248 }
249 }
250
251 vty_out(vty, "\n");
16286195
DS
252}
253
91ba2c8b
DS
254/* Print the command to enable the debug for each peer/prefix this debug is
255 * enabled for
256 */
d62a17ae 257static int bgp_debug_list_conf_print(struct vty *vty, const char *desc,
81cf0de5 258 struct list *list, uint8_t evpn_dbg)
d62a17ae 259{
260 struct bgp_debug_filter *filter;
261 struct listnode *node, *nnode;
81cf0de5 262 char buf[PREFIX2STR_BUFFER];
d62a17ae 263 int write = 0;
264
265 if (list && !list_isempty(list)) {
266 for (ALL_LIST_ELEMENTS(list, node, nnode, filter)) {
267 if (filter->host) {
268 vty_out(vty, "%s %s\n", desc, filter->host);
269 write++;
270 }
271
272
273 if (filter->p) {
81cf0de5
CS
274 if (!evpn_dbg) {
275 vty_out(vty, "%s %s\n", desc,
276 prefix2str(filter->p, buf,
277 sizeof(buf)));
278 write++;
279 } else {
280 if (filter->p->family == AF_EVPN)
281 bgp_debug_print_evpn_prefix(vty,
282 desc, filter->p);
283 write++;
284 }
d62a17ae 285 }
286 }
287 }
288
289 if (!write) {
290 vty_out(vty, "%s\n", desc);
291 write++;
292 }
293
294 return write;
91ba2c8b
DS
295}
296
d62a17ae 297static void bgp_debug_list_add_entry(struct list *list, const char *host,
298 const struct prefix *p)
16286195 299{
d62a17ae 300 struct bgp_debug_filter *filter;
301
302 filter = XCALLOC(MTYPE_BGP_DEBUG_FILTER,
303 sizeof(struct bgp_debug_filter));
304
305 if (host) {
306 filter->host = XSTRDUP(MTYPE_BGP_DEBUG_STR, host);
307 filter->p = NULL;
308 } else if (p) {
309 filter->host = NULL;
310 filter->p = prefix_new();
311 prefix_copy(filter->p, p);
312 }
313
314 listnode_add(list, filter);
16286195
DS
315}
316
d62a17ae 317static int bgp_debug_list_remove_entry(struct list *list, const char *host,
318 struct prefix *p)
319{
320 struct bgp_debug_filter *filter;
321 struct listnode *node, *nnode;
322
323 for (ALL_LIST_ELEMENTS(list, node, nnode, filter)) {
324 if (host && strcmp(filter->host, host) == 0) {
325 listnode_delete(list, filter);
326 XFREE(MTYPE_BGP_DEBUG_STR, filter->host);
327 XFREE(MTYPE_BGP_DEBUG_FILTER, filter);
328 return 1;
329 } else if (p && filter->p->prefixlen == p->prefixlen
330 && prefix_match(filter->p, p)) {
331 listnode_delete(list, filter);
332 prefix_free(filter->p);
333 XFREE(MTYPE_BGP_DEBUG_FILTER, filter);
334 return 1;
335 }
336 }
337
338 return 0;
16286195
DS
339}
340
d62a17ae 341static int bgp_debug_list_has_entry(struct list *list, const char *host,
342 const struct prefix *p)
343{
344 struct bgp_debug_filter *filter;
345 struct listnode *node, *nnode;
346
347 for (ALL_LIST_ELEMENTS(list, node, nnode, filter)) {
348 if (host) {
349 if (strcmp(filter->host, host) == 0) {
350 return 1;
351 }
352 } else if (p) {
353 if (filter->p->prefixlen == p->prefixlen
354 && prefix_match(filter->p, p)) {
355 return 1;
356 }
357 }
358 }
359
360 return 0;
16286195
DS
361}
362
d62a17ae 363int bgp_debug_peer_updout_enabled(char *host)
3f9c7369 364{
d62a17ae 365 return (bgp_debug_list_has_entry(bgp_debug_update_out_peers, host,
366 NULL));
3f9c7369
DS
367}
368
718e3744 369/* Dump attribute. */
d62a17ae 370int bgp_dump_attr(struct attr *attr, char *buf, size_t size)
718e3744 371{
d62a17ae 372 char addrbuf[BUFSIZ];
718e3744 373
d62a17ae 374 if (!attr)
375 return 0;
376
377 if (CHECK_FLAG(attr->flag, ATTR_FLAG_BIT(BGP_ATTR_NEXT_HOP)))
378 snprintf(buf, size, "nexthop %s", inet_ntoa(attr->nexthop));
379
380 if (CHECK_FLAG(attr->flag, ATTR_FLAG_BIT(BGP_ATTR_ORIGIN)))
381 snprintf(buf + strlen(buf), size - strlen(buf), ", origin %s",
382 bgp_origin_str[attr->origin]);
383
384 /* Add MP case. */
385 if (attr->mp_nexthop_len == BGP_ATTR_NHLEN_IPV6_GLOBAL
386 || attr->mp_nexthop_len == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL)
387 snprintf(buf + strlen(buf), size - strlen(buf),
388 ", mp_nexthop %s",
389 inet_ntop(AF_INET6, &attr->mp_nexthop_global, addrbuf,
390 BUFSIZ));
391
392 if (attr->mp_nexthop_len == BGP_ATTR_NHLEN_IPV6_GLOBAL_AND_LL)
393 snprintf(buf + strlen(buf), size - strlen(buf), "(%s)",
394 inet_ntop(AF_INET6, &attr->mp_nexthop_local, addrbuf,
395 BUFSIZ));
396
397 if (attr->mp_nexthop_len == BGP_ATTR_NHLEN_IPV4)
398 snprintf(buf, size, "nexthop %s", inet_ntoa(attr->nexthop));
399
400 if (CHECK_FLAG(attr->flag, ATTR_FLAG_BIT(BGP_ATTR_LOCAL_PREF)))
401 snprintf(buf + strlen(buf), size - strlen(buf),
402 ", localpref %u", attr->local_pref);
403
404 if (CHECK_FLAG(attr->flag, ATTR_FLAG_BIT(BGP_ATTR_MULTI_EXIT_DISC)))
405 snprintf(buf + strlen(buf), size - strlen(buf), ", metric %u",
406 attr->med);
407
408 if (CHECK_FLAG(attr->flag, ATTR_FLAG_BIT(BGP_ATTR_COMMUNITIES)))
409 snprintf(buf + strlen(buf), size - strlen(buf),
a4d82a8a
PZ
410 ", community %s",
411 community_str(attr->community, false));
d62a17ae 412
413 if (CHECK_FLAG(attr->flag, ATTR_FLAG_BIT(BGP_ATTR_EXT_COMMUNITIES)))
414 snprintf(buf + strlen(buf), size - strlen(buf),
415 ", extcommunity %s", ecommunity_str(attr->ecommunity));
416
417 if (CHECK_FLAG(attr->flag, ATTR_FLAG_BIT(BGP_ATTR_ATOMIC_AGGREGATE)))
418 snprintf(buf + strlen(buf), size - strlen(buf),
419 ", atomic-aggregate");
420
421 if (CHECK_FLAG(attr->flag, ATTR_FLAG_BIT(BGP_ATTR_AGGREGATOR)))
422 snprintf(buf + strlen(buf), size - strlen(buf),
423 ", aggregated by %u %s", attr->aggregator_as,
424 inet_ntoa(attr->aggregator_addr));
425
426 if (CHECK_FLAG(attr->flag, ATTR_FLAG_BIT(BGP_ATTR_ORIGINATOR_ID)))
427 snprintf(buf + strlen(buf), size - strlen(buf),
428 ", originator %s", inet_ntoa(attr->originator_id));
429
430 if (CHECK_FLAG(attr->flag, ATTR_FLAG_BIT(BGP_ATTR_CLUSTER_LIST))) {
431 int i;
432
433 snprintf(buf + strlen(buf), size - strlen(buf),
434 ", clusterlist");
435 for (i = 0; i < attr->cluster->length / 4; i++)
436 snprintf(buf + strlen(buf), size - strlen(buf), " %s",
437 inet_ntoa(attr->cluster->list[i]));
438 }
439
7fd077aa 440 if (CHECK_FLAG(attr->flag, ATTR_FLAG_BIT(BGP_ATTR_PMSI_TUNNEL)))
052ea98b 441 snprintf(buf + strlen(buf), size - strlen(buf),
442 ", pmsi tnltype %u", attr->pmsi_tnl_type);
7fd077aa 443
d62a17ae 444 if (CHECK_FLAG(attr->flag, ATTR_FLAG_BIT(BGP_ATTR_AS_PATH)))
445 snprintf(buf + strlen(buf), size - strlen(buf), ", path %s",
446 aspath_print(attr->aspath));
447
448 if (CHECK_FLAG(attr->flag, ATTR_FLAG_BIT(BGP_ATTR_PREFIX_SID))) {
449 if (attr->label_index != BGP_INVALID_LABEL_INDEX)
450 snprintf(buf + strlen(buf), size - strlen(buf),
451 ", label-index %u", attr->label_index);
452 }
453
454 if (strlen(buf) > 1)
455 return 1;
456 else
457 return 0;
3a8c7ba1 458}
718e3744 459
d62a17ae 460const char *bgp_notify_code_str(char code)
3a8c7ba1 461{
d62a17ae 462 return lookup_msg(bgp_notify_msg, code, "Unrecognized Error Code");
463}
718e3744 464
d62a17ae 465const char *bgp_notify_subcode_str(char code, char subcode)
466{
467
468 switch (code) {
469 case BGP_NOTIFY_HEADER_ERR:
470 return lookup_msg(bgp_notify_head_msg, subcode,
471 "Unrecognized Error Subcode");
472 case BGP_NOTIFY_OPEN_ERR:
473 return lookup_msg(bgp_notify_open_msg, subcode,
474 "Unrecognized Error Subcode");
475 case BGP_NOTIFY_UPDATE_ERR:
476 return lookup_msg(bgp_notify_update_msg, subcode,
477 "Unrecognized Error Subcode");
478 case BGP_NOTIFY_HOLD_ERR:
479 break;
480 case BGP_NOTIFY_FSM_ERR:
481 break;
482 case BGP_NOTIFY_CEASE:
483 return lookup_msg(bgp_notify_cease_msg, subcode,
484 "Unrecognized Error Subcode");
485 case BGP_NOTIFY_CAPABILITY_ERR:
486 return lookup_msg(bgp_notify_capability_msg, subcode,
487 "Unrecognized Error Subcode");
488 }
489 return "";
3a8c7ba1
DW
490}
491
38de8d02 492/* extract notify admin reason if correctly present */
d7c0a89a 493const char *bgp_notify_admin_message(char *buf, size_t bufsz, uint8_t *data,
d62a17ae 494 size_t datalen)
38de8d02 495{
d62a17ae 496 if (!data || datalen < 1)
497 return NULL;
38de8d02 498
d7c0a89a 499 uint8_t len = data[0];
d62a17ae 500 if (len > 128 || len > datalen - 1)
501 return NULL;
38de8d02 502
d62a17ae 503 return zlog_sanitize(buf, bufsz, data + 1, len);
38de8d02
DL
504}
505
3a8c7ba1 506/* dump notify packet */
d62a17ae 507void bgp_notify_print(struct peer *peer, struct bgp_notify *bgp_notify,
508 const char *direct)
509{
510 const char *subcode_str;
511 const char *code_str;
512 const char *msg_str = NULL;
513 char msg_buf[1024];
514
515 if (BGP_DEBUG(neighbor_events, NEIGHBOR_EVENTS)
516 || bgp_flag_check(peer->bgp, BGP_FLAG_LOG_NEIGHBOR_CHANGES)) {
517 code_str = bgp_notify_code_str(bgp_notify->code);
518 subcode_str = bgp_notify_subcode_str(bgp_notify->code,
519 bgp_notify->subcode);
520
521 if (bgp_notify->code == BGP_NOTIFY_CEASE
522 && (bgp_notify->subcode == BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN
523 || bgp_notify->subcode
524 == BGP_NOTIFY_CEASE_ADMIN_RESET)) {
525 msg_str = bgp_notify_admin_message(
526 msg_buf, sizeof(msg_buf), bgp_notify->raw_data,
527 bgp_notify->length);
528 }
529
530 if (msg_str) {
531 zlog_info(
532 "%%NOTIFICATION: %s neighbor %s %d/%d (%s%s) \"%s\"",
533 strcmp(direct, "received") == 0
534 ? "received from"
535 : "sent to",
536 peer->host, bgp_notify->code,
537 bgp_notify->subcode, code_str, subcode_str,
538 msg_str);
539 } else {
540 msg_str = bgp_notify->data ? bgp_notify->data : "";
541 zlog_info(
542 "%%NOTIFICATION: %s neighbor %s %d/%d (%s%s) %d bytes %s",
543 strcmp(direct, "received") == 0
544 ? "received from"
545 : "sent to",
546 peer->host, bgp_notify->code,
547 bgp_notify->subcode, code_str, subcode_str,
548 bgp_notify->length, msg_str);
549 }
550 }
718e3744 551}
6b0655a2 552
d62a17ae 553static void bgp_debug_clear_updgrp_update_dbg(struct bgp *bgp)
3f9c7369 554{
d62a17ae 555 if (!bgp)
556 bgp = bgp_get_default();
557 update_group_walk(bgp, update_group_clear_update_dbg, NULL);
3f9c7369
DS
558}
559
81cf0de5
CS
560static int bgp_debug_print_evpn_prefix(struct vty *vty, const char *desc,
561 struct prefix *p)
562{
563 char evpn_desc[PREFIX2STR_BUFFER + INET_ADDRSTRLEN];
564 char buf[PREFIX2STR_BUFFER];
565 char buf2[ETHER_ADDR_STRLEN];
566
567 if (p->u.prefix_evpn.route_type == BGP_EVPN_MAC_IP_ROUTE) {
568 if (is_evpn_prefix_ipaddr_none((struct prefix_evpn *)p)) {
569 sprintf(evpn_desc, "l2vpn evpn type macip mac %s",
570 prefix_mac2str(
571 &p->u.prefix_evpn.macip_addr.mac,
572 buf2, sizeof(buf2)));
573 } else {
574 uint8_t family = is_evpn_prefix_ipaddr_v4(
575 (struct prefix_evpn *)p) ?
576 AF_INET : AF_INET6;
577 sprintf(evpn_desc, "l2vpn evpn type macip mac %s ip %s",
578 prefix_mac2str(
579 &p->u.prefix_evpn.macip_addr.mac,
580 buf2, sizeof(buf2)),
581 inet_ntop(family,
582 &p->u.prefix_evpn.macip_addr.ip.ip.addr,
583 buf, PREFIX2STR_BUFFER));
584 }
585 } else if (p->u.prefix_evpn.route_type == BGP_EVPN_IMET_ROUTE) {
586 sprintf(evpn_desc, "l2vpn evpn type multicast ip %s",
587 inet_ntoa(p->u.prefix_evpn.imet_addr.ip.ipaddr_v4));
588 } else if (p->u.prefix_evpn.route_type == BGP_EVPN_IP_PREFIX_ROUTE) {
589 uint8_t family = is_evpn_prefix_ipaddr_v4(
590 (struct prefix_evpn *)p) ? AF_INET
591 : AF_INET6;
592 sprintf(evpn_desc, "l2vpn evpn type prefix ip %s/%d",
593 inet_ntop(family,
594 &p->u.prefix_evpn.prefix_addr.ip.ip.addr, buf,
595 PREFIX2STR_BUFFER),
596 p->u.prefix_evpn.prefix_addr.ip_prefix_length);
597 }
598
599 vty_out(vty, "%s %s\n", desc, evpn_desc);
600
601 return 0;
602}
603
604static int bgp_debug_parse_evpn_prefix(struct vty *vty, struct cmd_token **argv,
605 int argc, struct prefix **argv_pp)
606{
607 struct prefix *argv_p;
608 struct ethaddr mac;
609 struct ipaddr ip;
610 int evpn_type;
611 int type_idx = 0;
612 int mac_idx = 0;
613 int ip_idx = 0;
614
615 argv_p = *argv_pp;
616
617 if (argv_find(argv, argc, "type", &type_idx) == 0)
618 return CMD_WARNING;
619
620 if (strncmp(argv[type_idx + 1]->arg, "ma", 2) == 0)
621 evpn_type = BGP_EVPN_MAC_IP_ROUTE;
622 else if (strncmp(argv[type_idx + 1]->arg, "mu", 2) == 0)
623 evpn_type = BGP_EVPN_IMET_ROUTE;
624 else if (strncmp(argv[type_idx + 1]->arg, "pr", 2) == 0)
625 evpn_type = BGP_EVPN_IP_PREFIX_ROUTE;
626 else
627 evpn_type = 0;
628
629 if (evpn_type == BGP_EVPN_MAC_IP_ROUTE) {
630 memset(&ip, 0, sizeof(struct ipaddr));
631 /* get the ip if specified */
632 if (argv_find(argv, argc, "ip", &ip_idx)) {
633 if (str2ipaddr(argv[ip_idx + 1]->arg, &ip) != 0) {
634 vty_out(vty, "%% Malformed IP address\n");
635 return CMD_WARNING;
636 }
637 }
638 argv_find(argv, argc, "mac", &mac_idx);
639 if (!prefix_str2mac(argv[mac_idx + 1]->arg, &mac)) {
640 vty_out(vty, "%% Malformed MAC address\n");
641 return CMD_WARNING;
642 }
643
644 build_evpn_type2_prefix((struct prefix_evpn *)argv_p,
645 &mac, &ip);
646 } else if (evpn_type == BGP_EVPN_IMET_ROUTE) {
647 memset(&ip, 0, sizeof(struct ipaddr));
648 /* get the ip if specified */
649 if (argv_find(argv, argc, "ip", &ip_idx)) {
650 if (str2ipaddr(argv[ip_idx + 1]->arg, &ip) != 0) {
651 vty_out(vty, "%% Malformed IP address\n");
652 return CMD_WARNING;
653 }
654 }
655 build_evpn_type3_prefix((struct prefix_evpn *)argv_p,
656 ip.ipaddr_v4);
657 } else if (evpn_type == BGP_EVPN_IP_PREFIX_ROUTE) {
658 struct prefix ip_prefix;
659
660 memset(&ip_prefix, 0, sizeof(struct prefix));
661 if (argv_find(argv, argc, "ip", &ip_idx)) {
662 (void)str2prefix(argv[ip_idx + 1]->arg, &ip_prefix);
663 apply_mask(&ip_prefix);
664 }
665 build_type5_prefix_from_ip_prefix(
666 (struct prefix_evpn *)argv_p,
667 &ip_prefix);
668 }
669
670 return CMD_SUCCESS;
671}
3f9c7369 672
718e3744 673/* Debug option setting interface. */
674unsigned long bgp_debug_option = 0;
675
d62a17ae 676int debug(unsigned int option)
718e3744 677{
d62a17ae 678 return bgp_debug_option & option;
718e3744 679}
680
0b2aa3a0
PJ
681DEFUN (debug_bgp_as4,
682 debug_bgp_as4_cmd,
683 "debug bgp as4",
684 DEBUG_STR
685 BGP_STR
686 "BGP AS4 actions\n")
687{
d62a17ae 688 if (vty->node == CONFIG_NODE)
689 DEBUG_ON(as4, AS4);
690 else {
691 TERM_DEBUG_ON(as4, AS4);
692 vty_out(vty, "BGP as4 debugging is on\n");
693 }
694 return CMD_SUCCESS;
0b2aa3a0
PJ
695}
696
697DEFUN (no_debug_bgp_as4,
698 no_debug_bgp_as4_cmd,
699 "no debug bgp as4",
700 NO_STR
701 DEBUG_STR
702 BGP_STR
703 "BGP AS4 actions\n")
704{
d62a17ae 705 if (vty->node == CONFIG_NODE)
706 DEBUG_OFF(as4, AS4);
707 else {
708 TERM_DEBUG_OFF(as4, AS4);
709 vty_out(vty, "BGP as4 debugging is off\n");
710 }
711 return CMD_SUCCESS;
0b2aa3a0
PJ
712}
713
0b2aa3a0
PJ
714DEFUN (debug_bgp_as4_segment,
715 debug_bgp_as4_segment_cmd,
716 "debug bgp as4 segment",
717 DEBUG_STR
718 BGP_STR
6e22b901 719 "BGP AS4 actions\n"
0b2aa3a0
PJ
720 "BGP AS4 aspath segment handling\n")
721{
d62a17ae 722 if (vty->node == CONFIG_NODE)
723 DEBUG_ON(as4, AS4_SEGMENT);
724 else {
725 TERM_DEBUG_ON(as4, AS4_SEGMENT);
726 vty_out(vty, "BGP as4 segment debugging is on\n");
727 }
728 return CMD_SUCCESS;
0b2aa3a0
PJ
729}
730
731DEFUN (no_debug_bgp_as4_segment,
732 no_debug_bgp_as4_segment_cmd,
733 "no debug bgp as4 segment",
734 NO_STR
735 DEBUG_STR
736 BGP_STR
6e22b901 737 "BGP AS4 actions\n"
0b2aa3a0
PJ
738 "BGP AS4 aspath segment handling\n")
739{
d62a17ae 740 if (vty->node == CONFIG_NODE)
741 DEBUG_OFF(as4, AS4_SEGMENT);
742 else {
743 TERM_DEBUG_OFF(as4, AS4_SEGMENT);
744 vty_out(vty, "BGP as4 segment debugging is off\n");
745 }
746 return CMD_SUCCESS;
0b2aa3a0
PJ
747}
748
16286195
DS
749/* debug bgp neighbor_events */
750DEFUN (debug_bgp_neighbor_events,
751 debug_bgp_neighbor_events_cmd,
752 "debug bgp neighbor-events",
718e3744 753 DEBUG_STR
754 BGP_STR
16286195 755 "BGP Neighbor Events\n")
718e3744 756{
d62a17ae 757 bgp_debug_list_free(bgp_debug_neighbor_events_peers);
758
759 if (vty->node == CONFIG_NODE)
760 DEBUG_ON(neighbor_events, NEIGHBOR_EVENTS);
761 else {
762 TERM_DEBUG_ON(neighbor_events, NEIGHBOR_EVENTS);
763 vty_out(vty, "BGP neighbor-events debugging is on\n");
764 }
765 return CMD_SUCCESS;
718e3744 766}
767
16286195
DS
768DEFUN (debug_bgp_neighbor_events_peer,
769 debug_bgp_neighbor_events_peer_cmd,
6147e2c6 770 "debug bgp neighbor-events <A.B.C.D|X:X::X:X|WORD>",
718e3744 771 DEBUG_STR
772 BGP_STR
16286195
DS
773 "BGP Neighbor Events\n"
774 "BGP neighbor IP address to debug\n"
775 "BGP IPv6 neighbor to debug\n"
776 "BGP neighbor on interface to debug\n")
718e3744 777{
d62a17ae 778 int idx_peer = 3;
779 const char *host = argv[idx_peer]->arg;
780
781 if (!bgp_debug_neighbor_events_peers)
782 bgp_debug_neighbor_events_peers = list_new();
783
784 if (bgp_debug_list_has_entry(bgp_debug_neighbor_events_peers, host,
785 NULL)) {
786 vty_out(vty,
787 "BGP neighbor-events debugging is already enabled for %s\n",
788 host);
789 return CMD_SUCCESS;
790 }
791
792 bgp_debug_list_add_entry(bgp_debug_neighbor_events_peers, host, NULL);
793
794 if (vty->node == CONFIG_NODE)
795 DEBUG_ON(neighbor_events, NEIGHBOR_EVENTS);
796 else {
797 TERM_DEBUG_ON(neighbor_events, NEIGHBOR_EVENTS);
798 vty_out(vty, "BGP neighbor-events debugging is on for %s\n",
799 host);
800 }
801 return CMD_SUCCESS;
718e3744 802}
803
16286195
DS
804DEFUN (no_debug_bgp_neighbor_events,
805 no_debug_bgp_neighbor_events_cmd,
806 "no debug bgp neighbor-events",
807 NO_STR
718e3744 808 DEBUG_STR
809 BGP_STR
16286195 810 "Neighbor Events\n")
718e3744 811{
d62a17ae 812 bgp_debug_list_free(bgp_debug_neighbor_events_peers);
813
814 if (vty->node == CONFIG_NODE)
815 DEBUG_OFF(neighbor_events, NEIGHBOR_EVENTS);
816 else {
817 TERM_DEBUG_OFF(neighbor_events, NEIGHBOR_EVENTS);
818 vty_out(vty, "BGP neighbor-events debugging is off\n");
819 }
820 return CMD_SUCCESS;
718e3744 821}
822
16286195
DS
823DEFUN (no_debug_bgp_neighbor_events_peer,
824 no_debug_bgp_neighbor_events_peer_cmd,
6147e2c6 825 "no debug bgp neighbor-events <A.B.C.D|X:X::X:X|WORD>",
718e3744 826 NO_STR
827 DEBUG_STR
828 BGP_STR
16286195
DS
829 "Neighbor Events\n"
830 "BGP neighbor IP address to debug\n"
831 "BGP IPv6 neighbor to debug\n"
832 "BGP neighbor on interface to debug\n")
718e3744 833{
d62a17ae 834 int idx_peer = 4;
835 int found_peer = 0;
836 const char *host = argv[idx_peer]->arg;
837
838 if (bgp_debug_neighbor_events_peers
839 && !list_isempty(bgp_debug_neighbor_events_peers)) {
840 found_peer = bgp_debug_list_remove_entry(
841 bgp_debug_neighbor_events_peers, host, NULL);
842
843 if (list_isempty(bgp_debug_neighbor_events_peers)) {
844 if (vty->node == CONFIG_NODE)
845 DEBUG_OFF(neighbor_events, NEIGHBOR_EVENTS);
846 else
847 TERM_DEBUG_OFF(neighbor_events,
848 NEIGHBOR_EVENTS);
849 }
850 }
851
852 if (found_peer)
853 vty_out(vty, "BGP neighbor-events debugging is off for %s\n",
854 host);
855 else
856 vty_out(vty,
857 "BGP neighbor-events debugging was not enabled for %s\n",
858 host);
859
860 return CMD_SUCCESS;
718e3744 861}
862
16286195 863/* debug bgp nht */
fb018d25
DS
864DEFUN (debug_bgp_nht,
865 debug_bgp_nht_cmd,
866 "debug bgp nht",
867 DEBUG_STR
868 BGP_STR
869 "BGP nexthop tracking events\n")
870{
d62a17ae 871 if (vty->node == CONFIG_NODE)
872 DEBUG_ON(nht, NHT);
873 else {
874 TERM_DEBUG_ON(nht, NHT);
875 vty_out(vty, "BGP nexthop tracking debugging is on\n");
876 }
877 return CMD_SUCCESS;
fb018d25
DS
878}
879
880DEFUN (no_debug_bgp_nht,
881 no_debug_bgp_nht_cmd,
882 "no debug bgp nht",
883 NO_STR
884 DEBUG_STR
885 BGP_STR
886 "BGP nexthop tracking events\n")
887{
d62a17ae 888 if (vty->node == CONFIG_NODE)
889 DEBUG_OFF(nht, NHT);
890 else {
891 TERM_DEBUG_OFF(nht, NHT);
892 vty_out(vty, "BGP nexthop tracking debugging is off\n");
893 }
894 return CMD_SUCCESS;
fb018d25
DS
895}
896
16286195
DS
897/* debug bgp keepalives */
898DEFUN (debug_bgp_keepalive,
899 debug_bgp_keepalive_cmd,
900 "debug bgp keepalives",
718e3744 901 DEBUG_STR
902 BGP_STR
16286195 903 "BGP keepalives\n")
718e3744 904{
d62a17ae 905 bgp_debug_list_free(bgp_debug_keepalive_peers);
906
907 if (vty->node == CONFIG_NODE)
908 DEBUG_ON(keepalive, KEEPALIVE);
909 else {
910 TERM_DEBUG_ON(keepalive, KEEPALIVE);
911 vty_out(vty, "BGP keepalives debugging is on\n");
912 }
913 return CMD_SUCCESS;
718e3744 914}
915
16286195
DS
916DEFUN (debug_bgp_keepalive_peer,
917 debug_bgp_keepalive_peer_cmd,
6147e2c6 918 "debug bgp keepalives <A.B.C.D|X:X::X:X|WORD>",
718e3744 919 DEBUG_STR
920 BGP_STR
16286195
DS
921 "BGP Neighbor Events\n"
922 "BGP neighbor IP address to debug\n"
923 "BGP IPv6 neighbor to debug\n"
924 "BGP neighbor on interface to debug\n")
718e3744 925{
d62a17ae 926 int idx_peer = 3;
927 const char *host = argv[idx_peer]->arg;
928
929 if (!bgp_debug_keepalive_peers)
930 bgp_debug_keepalive_peers = list_new();
931
932 if (bgp_debug_list_has_entry(bgp_debug_keepalive_peers, host, NULL)) {
933 vty_out(vty,
934 "BGP keepalive debugging is already enabled for %s\n",
935 host);
936 return CMD_SUCCESS;
937 }
938
939 bgp_debug_list_add_entry(bgp_debug_keepalive_peers, host, NULL);
940
941 if (vty->node == CONFIG_NODE)
942 DEBUG_ON(keepalive, KEEPALIVE);
943 else {
944 TERM_DEBUG_ON(keepalive, KEEPALIVE);
945 vty_out(vty, "BGP keepalives debugging is on for %s\n", host);
946 }
947 return CMD_SUCCESS;
718e3744 948}
949
950DEFUN (no_debug_bgp_keepalive,
951 no_debug_bgp_keepalive_cmd,
952 "no debug bgp keepalives",
953 NO_STR
954 DEBUG_STR
955 BGP_STR
956 "BGP keepalives\n")
957{
d62a17ae 958 bgp_debug_list_free(bgp_debug_keepalive_peers);
959
960 if (vty->node == CONFIG_NODE)
961 DEBUG_OFF(keepalive, KEEPALIVE);
962 else {
963 TERM_DEBUG_OFF(keepalive, KEEPALIVE);
964 vty_out(vty, "BGP keepalives debugging is off\n");
965 }
966 return CMD_SUCCESS;
718e3744 967}
968
16286195
DS
969DEFUN (no_debug_bgp_keepalive_peer,
970 no_debug_bgp_keepalive_peer_cmd,
6147e2c6 971 "no debug bgp keepalives <A.B.C.D|X:X::X:X|WORD>",
16286195
DS
972 NO_STR
973 DEBUG_STR
718e3744 974 BGP_STR
16286195
DS
975 "BGP keepalives\n"
976 "BGP neighbor IP address to debug\n"
977 "BGP IPv6 neighbor to debug\n"
978 "BGP neighbor on interface to debug\n")
979{
d62a17ae 980 int idx_peer = 4;
981 int found_peer = 0;
982 const char *host = argv[idx_peer]->arg;
983
984 if (bgp_debug_keepalive_peers
985 && !list_isempty(bgp_debug_keepalive_peers)) {
986 found_peer = bgp_debug_list_remove_entry(
987 bgp_debug_keepalive_peers, host, NULL);
988
989 if (list_isempty(bgp_debug_keepalive_peers)) {
990 if (vty->node == CONFIG_NODE)
991 DEBUG_OFF(keepalive, KEEPALIVE);
992 else
993 TERM_DEBUG_OFF(keepalive, KEEPALIVE);
994 }
995 }
996
997 if (found_peer)
998 vty_out(vty, "BGP keepalives debugging is off for %s\n", host);
999 else
1000 vty_out(vty,
1001 "BGP keepalives debugging was not enabled for %s\n",
1002 host);
1003
1004 return CMD_SUCCESS;
16286195
DS
1005}
1006
9fbdd100 1007/* debug bgp bestpath */
ffd71765 1008DEFUN (debug_bgp_bestpath_prefix,
9fbdd100 1009 debug_bgp_bestpath_prefix_cmd,
ffd71765 1010 "debug bgp bestpath <A.B.C.D/M|X:X::X:X/M>",
9fbdd100
DS
1011 DEBUG_STR
1012 BGP_STR
1013 "BGP bestpath\n"
0c7b1b01
QY
1014 "IPv4 prefix\n"
1015 "IPv6 prefix\n")
9fbdd100 1016{
0be4b77c
DW
1017 struct prefix *argv_p;
1018 int idx_ipv4_ipv6_prefixlen = 3;
1019
1020 argv_p = prefix_new();
ffd71765 1021 (void)str2prefix(argv[idx_ipv4_ipv6_prefixlen]->arg, argv_p);
0be4b77c
DW
1022 apply_mask(argv_p);
1023
d62a17ae 1024 if (!bgp_debug_bestpath_prefixes)
1025 bgp_debug_bestpath_prefixes = list_new();
1026
1027 if (bgp_debug_list_has_entry(bgp_debug_bestpath_prefixes, NULL,
ffd71765 1028 argv_p)) {
d62a17ae 1029 vty_out(vty,
1030 "BGP bestpath debugging is already enabled for %s\n",
ffd71765 1031 argv[idx_ipv4_ipv6_prefixlen]->arg);
d62a17ae 1032 return CMD_SUCCESS;
1033 }
1034
ffd71765 1035 bgp_debug_list_add_entry(bgp_debug_bestpath_prefixes, NULL, argv_p);
d62a17ae 1036
1037 if (vty->node == CONFIG_NODE) {
1038 DEBUG_ON(bestpath, BESTPATH);
1039 } else {
1040 TERM_DEBUG_ON(bestpath, BESTPATH);
1041 vty_out(vty, "BGP bestpath debugging is on for %s\n",
ffd71765 1042 argv[idx_ipv4_ipv6_prefixlen]->arg);
d62a17ae 1043 }
1044
1045 return CMD_SUCCESS;
9fbdd100
DS
1046}
1047
1048DEFUN (no_debug_bgp_bestpath_prefix,
1049 no_debug_bgp_bestpath_prefix_cmd,
6147e2c6 1050 "no debug bgp bestpath <A.B.C.D/M|X:X::X:X/M>",
9fbdd100
DS
1051 NO_STR
1052 DEBUG_STR
1053 BGP_STR
1054 "BGP bestpath\n"
0c7b1b01
QY
1055 "IPv4 prefix\n"
1056 "IPv6 prefix\n")
9fbdd100 1057{
d62a17ae 1058 int idx_ipv4_ipv6_prefixlen = 4;
1059 struct prefix *argv_p;
1060 int found_prefix = 0;
d62a17ae 1061
1062 argv_p = prefix_new();
ffd71765 1063 (void)str2prefix(argv[idx_ipv4_ipv6_prefixlen]->arg, argv_p);
0be4b77c 1064 apply_mask(argv_p);
d62a17ae 1065
1066 if (bgp_debug_bestpath_prefixes
1067 && !list_isempty(bgp_debug_bestpath_prefixes)) {
1068 found_prefix = bgp_debug_list_remove_entry(
1069 bgp_debug_bestpath_prefixes, NULL, argv_p);
1070
1071 if (list_isempty(bgp_debug_bestpath_prefixes)) {
1072 if (vty->node == CONFIG_NODE) {
1073 DEBUG_OFF(bestpath, BESTPATH);
1074 } else {
1075 TERM_DEBUG_OFF(bestpath, BESTPATH);
1076 vty_out(vty,
1077 "BGP bestpath debugging (per prefix) is off\n");
1078 }
1079 }
1080 }
1081
1082 if (found_prefix)
1083 vty_out(vty, "BGP bestpath debugging is off for %s\n",
1084 argv[idx_ipv4_ipv6_prefixlen]->arg);
1085 else
1086 vty_out(vty, "BGP bestpath debugging was not enabled for %s\n",
1087 argv[idx_ipv4_ipv6_prefixlen]->arg);
1088
1089 return CMD_SUCCESS;
9fbdd100
DS
1090}
1091
1092DEFUN (no_debug_bgp_bestpath,
1093 no_debug_bgp_bestpath_cmd,
1094 "no debug bgp bestpath",
1095 NO_STR
1096 DEBUG_STR
1097 BGP_STR
1098 "BGP bestpath\n")
1099{
d62a17ae 1100 bgp_debug_list_free(bgp_debug_bestpath_prefixes);
1101
1102 if (vty->node == CONFIG_NODE)
1103 DEBUG_OFF(bestpath, BESTPATH);
1104 else {
1105 TERM_DEBUG_OFF(bestpath, BESTPATH);
1106 vty_out(vty, "BGP bestpath debugging is off\n");
1107 }
1108 return CMD_SUCCESS;
9fbdd100
DS
1109}
1110
16286195 1111/* debug bgp updates */
718e3744 1112DEFUN (debug_bgp_update,
1113 debug_bgp_update_cmd,
1114 "debug bgp updates",
1115 DEBUG_STR
1116 BGP_STR
1117 "BGP updates\n")
1118{
d62a17ae 1119 bgp_debug_list_free(bgp_debug_update_in_peers);
1120 bgp_debug_list_free(bgp_debug_update_out_peers);
1121 bgp_debug_list_free(bgp_debug_update_prefixes);
1122
1123 if (vty->node == CONFIG_NODE) {
1124 DEBUG_ON(update, UPDATE_IN);
1125 DEBUG_ON(update, UPDATE_OUT);
1126 } else {
1127 TERM_DEBUG_ON(update, UPDATE_IN);
1128 TERM_DEBUG_ON(update, UPDATE_OUT);
1129 vty_out(vty, "BGP updates debugging is on\n");
1130 }
1131 return CMD_SUCCESS;
718e3744 1132}
1133
1134DEFUN (debug_bgp_update_direct,
1135 debug_bgp_update_direct_cmd,
6147e2c6 1136 "debug bgp updates <in|out>",
718e3744 1137 DEBUG_STR
1138 BGP_STR
1139 "BGP updates\n"
1140 "Inbound updates\n"
1141 "Outbound updates\n")
1142{
d62a17ae 1143 int idx_in_out = 3;
1144
1145 if (strncmp("i", argv[idx_in_out]->arg, 1) == 0)
1146 bgp_debug_list_free(bgp_debug_update_in_peers);
1147 else
1148 bgp_debug_list_free(bgp_debug_update_out_peers);
1149
1150 if (vty->node == CONFIG_NODE) {
1151 if (strncmp("i", argv[idx_in_out]->arg, 1) == 0)
1152 DEBUG_ON(update, UPDATE_IN);
1153 else
1154 DEBUG_ON(update, UPDATE_OUT);
1155 } else {
1156 if (strncmp("i", argv[idx_in_out]->arg, 1) == 0) {
1157 TERM_DEBUG_ON(update, UPDATE_IN);
1158 vty_out(vty, "BGP updates debugging is on (inbound)\n");
1159 } else {
1160 TERM_DEBUG_ON(update, UPDATE_OUT);
1161 vty_out(vty,
1162 "BGP updates debugging is on (outbound)\n");
1163 }
1164 }
1165 return CMD_SUCCESS;
718e3744 1166}
1167
16286195
DS
1168DEFUN (debug_bgp_update_direct_peer,
1169 debug_bgp_update_direct_peer_cmd,
6147e2c6 1170 "debug bgp updates <in|out> <A.B.C.D|X:X::X:X|WORD>",
718e3744 1171 DEBUG_STR
1172 BGP_STR
16286195
DS
1173 "BGP updates\n"
1174 "Inbound updates\n"
1175 "Outbound updates\n"
1176 "BGP neighbor IP address to debug\n"
1177 "BGP IPv6 neighbor to debug\n"
1178 "BGP neighbor on interface to debug\n")
718e3744 1179{
d62a17ae 1180 int idx_in_out = 3;
1181 int idx_peer = 4;
1182 const char *host = argv[idx_peer]->arg;
1183 int inbound;
1184
1185 if (!bgp_debug_update_in_peers)
1186 bgp_debug_update_in_peers = list_new();
1187
1188 if (!bgp_debug_update_out_peers)
1189 bgp_debug_update_out_peers = list_new();
1190
1191 if (strncmp("i", argv[idx_in_out]->arg, 1) == 0)
1192 inbound = 1;
1193 else
1194 inbound = 0;
1195
1196 if (inbound) {
1197 if (bgp_debug_list_has_entry(bgp_debug_update_in_peers, host,
1198 NULL)) {
1199 vty_out(vty,
1200 "BGP inbound update debugging is already enabled for %s\n",
1201 host);
1202 return CMD_SUCCESS;
1203 }
1204 }
1205
1206 else {
1207 if (bgp_debug_list_has_entry(bgp_debug_update_out_peers, host,
1208 NULL)) {
1209 vty_out(vty,
1210 "BGP outbound update debugging is already enabled for %s\n",
1211 host);
1212 return CMD_SUCCESS;
1213 }
1214 }
1215
1216 if (inbound)
1217 bgp_debug_list_add_entry(bgp_debug_update_in_peers, host, NULL);
1218 else {
1219 struct peer *peer;
1220 struct peer_af *paf;
1221 int afidx;
1222
1223 bgp_debug_list_add_entry(bgp_debug_update_out_peers, host,
1224 NULL);
1225 peer = bgp_find_peer(vty, host);
1226
1227 if (peer) {
1228 for (afidx = BGP_AF_START; afidx < BGP_AF_MAX;
1229 afidx++) {
1230 paf = peer->peer_af_array[afidx];
1231 if (paf != NULL) {
1232 if (PAF_SUBGRP(paf)) {
1233 UPDGRP_PEER_DBG_EN(
1234 PAF_SUBGRP(paf)
1235 ->update_group);
1236 }
1237 }
1238 }
1239 }
1240 }
1241
1242 if (vty->node == CONFIG_NODE) {
1243 if (inbound)
1244 DEBUG_ON(update, UPDATE_IN);
1245 else
1246 DEBUG_ON(update, UPDATE_OUT);
1247 } else {
1248 if (inbound) {
1249 TERM_DEBUG_ON(update, UPDATE_IN);
1250 vty_out(vty,
1251 "BGP updates debugging is on (inbound) for %s\n",
1252 argv[idx_peer]->arg);
1253 } else {
1254 TERM_DEBUG_ON(update, UPDATE_OUT);
1255 vty_out(vty,
1256 "BGP updates debugging is on (outbound) for %s\n",
1257 argv[idx_peer]->arg);
1258 }
1259 }
1260 return CMD_SUCCESS;
718e3744 1261}
1262
91ba2c8b
DS
1263DEFUN (no_debug_bgp_update_direct,
1264 no_debug_bgp_update_direct_cmd,
6147e2c6 1265 "no debug bgp updates <in|out>",
91ba2c8b
DS
1266 NO_STR
1267 DEBUG_STR
1268 BGP_STR
1269 "BGP updates\n"
1270 "Inbound updates\n"
1271 "Outbound updates\n")
1272{
d62a17ae 1273 int idx_in_out = 4;
1274 if (strncmp("i", argv[idx_in_out]->arg, 1) == 0) {
1275 bgp_debug_list_free(bgp_debug_update_in_peers);
1276
1277 if (vty->node == CONFIG_NODE) {
1278 DEBUG_OFF(update, UPDATE_IN);
1279 } else {
1280 TERM_DEBUG_OFF(update, UPDATE_IN);
1281 vty_out(vty,
1282 "BGP updates debugging is off (inbound)\n");
1283 }
1284 } else {
1285 bgp_debug_list_free(bgp_debug_update_out_peers);
1286
1287 if (vty->node == CONFIG_NODE) {
1288 DEBUG_OFF(update, UPDATE_OUT);
1289 } else {
1290 TERM_DEBUG_OFF(update, UPDATE_OUT);
1291 vty_out(vty,
1292 "BGP updates debugging is off (outbound)\n");
1293 }
1294 }
1295
1296 return CMD_SUCCESS;
91ba2c8b
DS
1297}
1298
16286195
DS
1299DEFUN (no_debug_bgp_update_direct_peer,
1300 no_debug_bgp_update_direct_peer_cmd,
6147e2c6 1301 "no debug bgp updates <in|out> <A.B.C.D|X:X::X:X|WORD>",
16286195
DS
1302 NO_STR
1303 DEBUG_STR
718e3744 1304 BGP_STR
16286195
DS
1305 "BGP updates\n"
1306 "Inbound updates\n"
1307 "Outbound updates\n"
1308 "BGP neighbor IP address to debug\n"
1309 "BGP IPv6 neighbor to debug\n"
1310 "BGP neighbor on interface to debug\n")
1311{
d62a17ae 1312 int idx_in_out = 4;
1313 int idx_peer = 5;
1314 int inbound;
1315 int found_peer = 0;
1316 const char *host = argv[idx_peer]->arg;
1317
1318 if (strncmp("i", argv[idx_in_out]->arg, 1) == 0)
1319 inbound = 1;
1320 else
1321 inbound = 0;
1322
1323 if (inbound && bgp_debug_update_in_peers
1324 && !list_isempty(bgp_debug_update_in_peers)) {
1325 found_peer = bgp_debug_list_remove_entry(
1326 bgp_debug_update_in_peers, host, NULL);
1327
1328 if (list_isempty(bgp_debug_update_in_peers)) {
1329 if (vty->node == CONFIG_NODE)
1330 DEBUG_OFF(update, UPDATE_IN);
1331 else {
1332 TERM_DEBUG_OFF(update, UPDATE_IN);
1333 vty_out(vty,
1334 "BGP updates debugging (inbound) is off\n");
1335 }
1336 }
1337 }
1338
1339 if (!inbound && bgp_debug_update_out_peers
1340 && !list_isempty(bgp_debug_update_out_peers)) {
1341 found_peer = bgp_debug_list_remove_entry(
1342 bgp_debug_update_out_peers, host, NULL);
1343
1344 if (list_isempty(bgp_debug_update_out_peers)) {
1345 if (vty->node == CONFIG_NODE)
1346 DEBUG_OFF(update, UPDATE_OUT);
1347 else {
1348 TERM_DEBUG_OFF(update, UPDATE_OUT);
1349 vty_out(vty,
1350 "BGP updates debugging (outbound) is off\n");
1351 }
1352 }
1353
1354 struct peer *peer;
1355 struct peer_af *paf;
1356 int afidx;
1357 peer = bgp_find_peer(vty, host);
1358
1359 if (peer) {
1360 for (afidx = BGP_AF_START; afidx < BGP_AF_MAX;
1361 afidx++) {
1362 paf = peer->peer_af_array[afidx];
1363 if (paf != NULL) {
1364 if (PAF_SUBGRP(paf)) {
1365 UPDGRP_PEER_DBG_DIS(
1366 PAF_SUBGRP(paf)
1367 ->update_group);
1368 }
1369 }
1370 }
1371 }
1372 }
1373
1374 if (found_peer)
1375 if (inbound)
1376 vty_out(vty,
1377 "BGP updates debugging (inbound) is off for %s\n",
1378 host);
1379 else
1380 vty_out(vty,
1381 "BGP updates debugging (outbound) is off for %s\n",
1382 host);
1383 else if (inbound)
1384 vty_out(vty,
1385 "BGP updates debugging (inbound) was not enabled for %s\n",
1386 host);
1387 else
1388 vty_out(vty,
1389 "BGP updates debugging (outbound) was not enabled for %s\n",
1390 host);
1391
1392 return CMD_SUCCESS;
16286195
DS
1393}
1394
81cf0de5
CS
1395#ifndef VTYSH_EXTRACT_PL
1396#include "bgpd/bgp_debug_clippy.c"
1397#endif
1398
1399DEFPY (debug_bgp_update_prefix_afi_safi,
1400 debug_bgp_update_prefix_afi_safi_cmd,
1401 "debug bgp updates prefix <l2vpn>$afi <evpn>$safi type <macip mac WORD [ip WORD]|multicast ip WORD |prefix ip WORD>",
1402 DEBUG_STR
1403 BGP_STR
1404 "BGP updates\n"
1405 "Specify a prefix to debug\n"
1406 "l2vpn\n"
1407 "evpn\n"
1408 "Specify EVPN Route type\n"
1409 "MAC-IP (Type-2) route\n"
1410 "MAC\n"
1411 "MAC address (e.g., 00:e0:ec:20:12:62)\n"
1412 "IP\n"
1413 "IP address (IPv4 or IPv6)\n"
1414 "Multicast (Type-3) route\n"
1415 "IP\n"
1416 "IP address (IPv4 or IPv6)\n"
1417 "Prefix (Type-5) route\n"
1418 "IP\n"
1419 "Prefix route\n")
1420{
1421 int idx_ipv4_ipv6_prefixlen = 4;
1422 struct prefix *argv_p;
1423 int ret = CMD_SUCCESS;
1424 afi_t afiz;
1425 safi_t safiz;
1426
1427 argv_p = prefix_new();
1428
1429 afiz = bgp_vty_afi_from_str(afi);
1430 safiz = safi ? bgp_vty_safi_from_str(safi) : SAFI_UNICAST;
1431
1432 /* check for evpn route type */
1433 if (afiz == AFI_L2VPN && safiz == SAFI_EVPN) {
1434 ret = bgp_debug_parse_evpn_prefix(vty, argv, argc, &argv_p);
1435 if (ret != CMD_SUCCESS)
360f9703 1436 goto cleanup;
81cf0de5
CS
1437 } else {
1438 (void)str2prefix(argv[idx_ipv4_ipv6_prefixlen]->arg, argv_p);
1439 apply_mask(argv_p);
1440 }
1441
1442 if (!bgp_debug_update_prefixes)
1443 bgp_debug_update_prefixes = list_new();
1444
1445 if (bgp_debug_list_has_entry(bgp_debug_update_prefixes, NULL, argv_p)) {
1446 vty_out(vty,
1447 "BGP updates debugging is already enabled for %s\n",
1448 argv[idx_ipv4_ipv6_prefixlen]->arg);
360f9703 1449 goto cleanup;
81cf0de5
CS
1450 }
1451
1452 bgp_debug_list_add_entry(bgp_debug_update_prefixes, NULL, argv_p);
1453
1454 if (vty->node == CONFIG_NODE) {
1455 DEBUG_ON(update, UPDATE_PREFIX);
1456 } else {
1457 TERM_DEBUG_ON(update, UPDATE_PREFIX);
1458 vty_out(vty, "BGP updates debugging is on for %s\n",
1459 argv[idx_ipv4_ipv6_prefixlen]->arg);
1460 }
1461
360f9703
A
1462cleanup:
1463 prefix_free(argv_p);
1464
1465 return ret;
81cf0de5
CS
1466}
1467
1468DEFPY (no_debug_bgp_update_prefix_afi_safi,
1469 no_debug_bgp_update_prefix_afi_safi_cmd,
1470 "no debug bgp updates prefix <l2vpn>$afi <evpn>$safi type <macip mac WORD [ip WORD]|multicast ip WORD |prefix ip WORD>",
1471 NO_STR
1472 DEBUG_STR
1473 BGP_STR
1474 "BGP updates\n"
1475 "Specify a prefix to debug\n"
1476 "l2vpn\n"
1477 "evpn\n"
1478 "Specify EVPN Route type\n"
1479 "MAC-IP (Type-2) route\n"
1480 "MAC\n"
1481 "MAC address (e.g., 00:e0:ec:20:12:62)\n"
1482 "IP\n"
1483 "IP address (IPv4 or IPv6)\n"
1484 "Multicast (Type-3) route\n"
1485 "IP\n"
1486 "IP address (IPv4 or IPv6)\n"
1487 "Prefix (Type-5) route\n"
1488 "IP\n"
1489 "Prefix route\n")
1490{
1491 int idx_ipv4_ipv6_prefixlen = 5;
1492 struct prefix *argv_p;
1493 int found_prefix = 0;
1494 int ret = CMD_SUCCESS;
1495 afi_t afiz;
1496 safi_t safiz;
1497
1498 argv_p = prefix_new();
1499
1500 afiz = bgp_vty_afi_from_str(afi);
1501 safiz = safi ? bgp_vty_safi_from_str(safi) : SAFI_UNICAST;
1502
1503 /* check for evpn route type */
1504 if (afiz == AFI_L2VPN && safiz == SAFI_EVPN) {
1505 ret = bgp_debug_parse_evpn_prefix(vty, argv, argc, &argv_p);
1506 if (ret != CMD_SUCCESS)
360f9703 1507 goto cleanup;
81cf0de5
CS
1508 } else {
1509 (void)str2prefix(argv[idx_ipv4_ipv6_prefixlen]->arg, argv_p);
1510 apply_mask(argv_p);
1511 }
1512
1513 if (bgp_debug_update_prefixes
1514 && !list_isempty(bgp_debug_update_prefixes)) {
1515 found_prefix = bgp_debug_list_remove_entry(
1516 bgp_debug_update_prefixes, NULL, argv_p);
1517
1518 if (list_isempty(bgp_debug_update_prefixes)) {
1519 if (vty->node == CONFIG_NODE) {
1520 DEBUG_OFF(update, UPDATE_PREFIX);
1521 } else {
1522 TERM_DEBUG_OFF(update, UPDATE_PREFIX);
1523 vty_out(vty,
1524 "BGP updates debugging (per prefix) is off\n");
1525 }
1526 }
1527 }
1528
1529 if (found_prefix)
1530 vty_out(vty, "BGP updates debugging is off for %s\n",
1531 argv[idx_ipv4_ipv6_prefixlen]->arg);
1532 else
1533 vty_out(vty, "BGP updates debugging was not enabled for %s\n",
1534 argv[idx_ipv4_ipv6_prefixlen]->arg);
1535
360f9703
A
1536cleanup:
1537 prefix_free(argv_p);
1538
1539 return ret;
81cf0de5
CS
1540}
1541
1542
16286195
DS
1543DEFUN (debug_bgp_update_prefix,
1544 debug_bgp_update_prefix_cmd,
6147e2c6 1545 "debug bgp updates prefix <A.B.C.D/M|X:X::X:X/M>",
718e3744 1546 DEBUG_STR
16286195
DS
1547 BGP_STR
1548 "BGP updates\n"
1549 "Specify a prefix to debug\n"
0c7b1b01
QY
1550 "IPv4 prefix\n"
1551 "IPv6 prefix\n")
718e3744 1552{
d62a17ae 1553 int idx_ipv4_ipv6_prefixlen = 4;
1554 struct prefix *argv_p;
d62a17ae 1555
1556 argv_p = prefix_new();
ffd71765 1557 (void)str2prefix(argv[idx_ipv4_ipv6_prefixlen]->arg, argv_p);
0be4b77c 1558 apply_mask(argv_p);
d62a17ae 1559
1560 if (!bgp_debug_update_prefixes)
1561 bgp_debug_update_prefixes = list_new();
1562
1563 if (bgp_debug_list_has_entry(bgp_debug_update_prefixes, NULL, argv_p)) {
1564 vty_out(vty,
1565 "BGP updates debugging is already enabled for %s\n",
1566 argv[idx_ipv4_ipv6_prefixlen]->arg);
1567 return CMD_SUCCESS;
1568 }
1569
1570 bgp_debug_list_add_entry(bgp_debug_update_prefixes, NULL, argv_p);
1571
1572 if (vty->node == CONFIG_NODE) {
1573 DEBUG_ON(update, UPDATE_PREFIX);
1574 } else {
1575 TERM_DEBUG_ON(update, UPDATE_PREFIX);
1576 vty_out(vty, "BGP updates debugging is on for %s\n",
1577 argv[idx_ipv4_ipv6_prefixlen]->arg);
1578 }
1579
1580 return CMD_SUCCESS;
718e3744 1581}
1582
16286195
DS
1583DEFUN (no_debug_bgp_update_prefix,
1584 no_debug_bgp_update_prefix_cmd,
6147e2c6 1585 "no debug bgp updates prefix <A.B.C.D/M|X:X::X:X/M>",
718e3744 1586 NO_STR
1587 DEBUG_STR
16286195
DS
1588 BGP_STR
1589 "BGP updates\n"
1590 "Specify a prefix to debug\n"
0c7b1b01
QY
1591 "IPv4 prefix\n"
1592 "IPv6 prefix\n")
16286195 1593{
d62a17ae 1594 int idx_ipv4_ipv6_prefixlen = 5;
1595 struct prefix *argv_p;
1596 int found_prefix = 0;
d62a17ae 1597
1598 argv_p = prefix_new();
ffd71765 1599 (void)str2prefix(argv[idx_ipv4_ipv6_prefixlen]->arg, argv_p);
0be4b77c 1600 apply_mask(argv_p);
d62a17ae 1601
1602 if (bgp_debug_update_prefixes
1603 && !list_isempty(bgp_debug_update_prefixes)) {
1604 found_prefix = bgp_debug_list_remove_entry(
1605 bgp_debug_update_prefixes, NULL, argv_p);
1606
1607 if (list_isempty(bgp_debug_update_prefixes)) {
1608 if (vty->node == CONFIG_NODE) {
1609 DEBUG_OFF(update, UPDATE_PREFIX);
1610 } else {
1611 TERM_DEBUG_OFF(update, UPDATE_PREFIX);
1612 vty_out(vty,
1613 "BGP updates debugging (per prefix) is off\n");
1614 }
1615 }
1616 }
1617
1618 if (found_prefix)
1619 vty_out(vty, "BGP updates debugging is off for %s\n",
1620 argv[idx_ipv4_ipv6_prefixlen]->arg);
1621 else
1622 vty_out(vty, "BGP updates debugging was not enabled for %s\n",
1623 argv[idx_ipv4_ipv6_prefixlen]->arg);
1624
1625 return CMD_SUCCESS;
16286195
DS
1626}
1627
1628DEFUN (no_debug_bgp_update,
1629 no_debug_bgp_update_cmd,
1630 "no debug bgp updates",
1631 NO_STR
1632 DEBUG_STR
1633 BGP_STR
1634 "BGP updates\n")
718e3744 1635{
87f42c2c
QY
1636 struct listnode *ln;
1637 struct bgp *bgp;
1638
d62a17ae 1639 bgp_debug_list_free(bgp_debug_update_in_peers);
1640 bgp_debug_list_free(bgp_debug_update_out_peers);
1641 bgp_debug_list_free(bgp_debug_update_prefixes);
1642
87f42c2c
QY
1643 for (ALL_LIST_ELEMENTS_RO(bm->bgp, ln, bgp))
1644 bgp_debug_clear_updgrp_update_dbg(bgp);
d62a17ae 1645
1646 if (vty->node == CONFIG_NODE) {
1647 DEBUG_OFF(update, UPDATE_IN);
1648 DEBUG_OFF(update, UPDATE_OUT);
1649 DEBUG_OFF(update, UPDATE_PREFIX);
1650 } else {
1651 TERM_DEBUG_OFF(update, UPDATE_IN);
1652 TERM_DEBUG_OFF(update, UPDATE_OUT);
1653 TERM_DEBUG_OFF(update, UPDATE_PREFIX);
1654 vty_out(vty, "BGP updates debugging is off\n");
1655 }
1656 return CMD_SUCCESS;
718e3744 1657}
1658
16286195 1659/* debug bgp zebra */
a39275d7
AS
1660DEFUN (debug_bgp_zebra,
1661 debug_bgp_zebra_cmd,
1662 "debug bgp zebra",
1663 DEBUG_STR
1664 BGP_STR
1665 "BGP Zebra messages\n")
1666{
d62a17ae 1667 if (vty->node == CONFIG_NODE)
1668 DEBUG_ON(zebra, ZEBRA);
1669 else {
1670 TERM_DEBUG_ON(zebra, ZEBRA);
1671 vty_out(vty, "BGP zebra debugging is on\n");
1672 }
1673 return CMD_SUCCESS;
a39275d7
AS
1674}
1675
16286195
DS
1676DEFUN (debug_bgp_zebra_prefix,
1677 debug_bgp_zebra_prefix_cmd,
6147e2c6 1678 "debug bgp zebra prefix <A.B.C.D/M|X:X::X:X/M>",
16286195
DS
1679 DEBUG_STR
1680 BGP_STR
1681 "BGP Zebra messages\n"
1682 "Specify a prefix to debug\n"
0c7b1b01
QY
1683 "IPv4 prefix\n"
1684 "IPv6 prefix\n")
16286195 1685{
d62a17ae 1686 int idx_ipv4_ipv6_prefixlen = 4;
1687 struct prefix *argv_p;
d62a17ae 1688
1689 argv_p = prefix_new();
ffd71765 1690 (void)str2prefix(argv[idx_ipv4_ipv6_prefixlen]->arg, argv_p);
0be4b77c 1691 apply_mask(argv_p);
d62a17ae 1692
1693 if (!bgp_debug_zebra_prefixes)
1694 bgp_debug_zebra_prefixes = list_new();
1695
1696 if (bgp_debug_list_has_entry(bgp_debug_zebra_prefixes, NULL, argv_p)) {
1697 vty_out(vty, "BGP zebra debugging is already enabled for %s\n",
1698 argv[idx_ipv4_ipv6_prefixlen]->arg);
1699 return CMD_SUCCESS;
1700 }
1701
1702 bgp_debug_list_add_entry(bgp_debug_zebra_prefixes, NULL, argv_p);
1703
1704 if (vty->node == CONFIG_NODE)
1705 DEBUG_ON(zebra, ZEBRA);
1706 else {
1707 TERM_DEBUG_ON(zebra, ZEBRA);
1708 vty_out(vty, "BGP zebra debugging is on for %s\n",
1709 argv[idx_ipv4_ipv6_prefixlen]->arg);
1710 }
1711
1712 return CMD_SUCCESS;
16286195
DS
1713}
1714
a39275d7
AS
1715DEFUN (no_debug_bgp_zebra,
1716 no_debug_bgp_zebra_cmd,
1717 "no debug bgp zebra",
1718 NO_STR
1719 DEBUG_STR
1720 BGP_STR
1721 "BGP Zebra messages\n")
1722{
d62a17ae 1723 bgp_debug_list_free(bgp_debug_zebra_prefixes);
1724
1725 if (vty->node == CONFIG_NODE)
1726 DEBUG_OFF(zebra, ZEBRA);
1727 else {
1728 TERM_DEBUG_OFF(zebra, ZEBRA);
1729 vty_out(vty, "BGP zebra debugging is off\n");
1730 }
1731 return CMD_SUCCESS;
a39275d7
AS
1732}
1733
16286195
DS
1734DEFUN (no_debug_bgp_zebra_prefix,
1735 no_debug_bgp_zebra_prefix_cmd,
6147e2c6 1736 "no debug bgp zebra prefix <A.B.C.D/M|X:X::X:X/M>",
16286195
DS
1737 NO_STR
1738 DEBUG_STR
a39275d7 1739 BGP_STR
16286195
DS
1740 "BGP Zebra messages\n"
1741 "Specify a prefix to debug\n"
0c7b1b01
QY
1742 "IPv4 prefix\n"
1743 "IPv6 prefix\n")
16286195 1744{
d62a17ae 1745 int idx_ipv4_ipv6_prefixlen = 5;
1746 struct prefix *argv_p;
1747 int found_prefix = 0;
d62a17ae 1748
1749 argv_p = prefix_new();
ffd71765 1750 (void)str2prefix(argv[idx_ipv4_ipv6_prefixlen]->arg, argv_p);
0be4b77c 1751 apply_mask(argv_p);
d62a17ae 1752
1753 if (bgp_debug_zebra_prefixes
1754 && !list_isempty(bgp_debug_zebra_prefixes)) {
1755 found_prefix = bgp_debug_list_remove_entry(
1756 bgp_debug_zebra_prefixes, NULL, argv_p);
1757
1758 if (list_isempty(bgp_debug_zebra_prefixes)) {
1759 if (vty->node == CONFIG_NODE)
1760 DEBUG_OFF(zebra, ZEBRA);
1761 else {
1762 TERM_DEBUG_OFF(zebra, ZEBRA);
1763 vty_out(vty, "BGP zebra debugging is off\n");
1764 }
1765 }
1766 }
1767
1768 if (found_prefix)
1769 vty_out(vty, "BGP zebra debugging is off for %s\n",
1770 argv[idx_ipv4_ipv6_prefixlen]->arg);
1771 else
1772 vty_out(vty, "BGP zebra debugging was not enabled for %s\n",
1773 argv[idx_ipv4_ipv6_prefixlen]->arg);
1774
1775 return CMD_SUCCESS;
16286195
DS
1776}
1777
cebb7440
LB
1778DEFUN (debug_bgp_allow_martians,
1779 debug_bgp_allow_martians_cmd,
1780 "debug bgp allow-martians",
1781 DEBUG_STR
1782 BGP_STR
1783 "BGP allow martian next hops\n")
1784{
d62a17ae 1785 if (vty->node == CONFIG_NODE)
1786 DEBUG_ON(allow_martians, ALLOW_MARTIANS);
1787 else {
1788 TERM_DEBUG_ON(allow_martians, ALLOW_MARTIANS);
1789 vty_out(vty, "BGP allow_martian next hop debugging is on\n");
1790 }
1791 return CMD_SUCCESS;
cebb7440
LB
1792}
1793
1794DEFUN (no_debug_bgp_allow_martians,
1795 no_debug_bgp_allow_martians_cmd,
1796 "no debug bgp allow-martians",
1797 NO_STR
1798 DEBUG_STR
1799 BGP_STR
1800 "BGP allow martian next hops\n")
1801{
d62a17ae 1802 if (vty->node == CONFIG_NODE)
1803 DEBUG_OFF(allow_martians, ALLOW_MARTIANS);
1804 else {
1805 TERM_DEBUG_OFF(allow_martians, ALLOW_MARTIANS);
1806 vty_out(vty, "BGP allow martian next hop debugging is off\n");
1807 }
1808 return CMD_SUCCESS;
cebb7440
LB
1809}
1810
cebb7440 1811
3f9c7369
DS
1812/* debug bgp update-groups */
1813DEFUN (debug_bgp_update_groups,
1814 debug_bgp_update_groups_cmd,
1815 "debug bgp update-groups",
1816 DEBUG_STR
1817 BGP_STR
1818 "BGP update-groups\n")
1819{
d62a17ae 1820 if (vty->node == CONFIG_NODE)
1821 DEBUG_ON(update_groups, UPDATE_GROUPS);
1822 else {
1823 TERM_DEBUG_ON(update_groups, UPDATE_GROUPS);
1824 vty_out(vty, "BGP update-groups debugging is on\n");
1825 }
1826 return CMD_SUCCESS;
3f9c7369
DS
1827}
1828
1829DEFUN (no_debug_bgp_update_groups,
1830 no_debug_bgp_update_groups_cmd,
1831 "no debug bgp update-groups",
1832 NO_STR
1833 DEBUG_STR
1834 BGP_STR
1835 "BGP update-groups\n")
1836{
d62a17ae 1837 if (vty->node == CONFIG_NODE)
1838 DEBUG_OFF(update_groups, UPDATE_GROUPS);
1839 else {
1840 TERM_DEBUG_OFF(update_groups, UPDATE_GROUPS);
1841 vty_out(vty, "BGP update-groups debugging is off\n");
1842 }
1843 return CMD_SUCCESS;
3f9c7369
DS
1844}
1845
ddb5b488
PZ
1846DEFUN (debug_bgp_vpn,
1847 debug_bgp_vpn_cmd,
1848 "debug bgp vpn <leak-from-vrf|leak-to-vrf|rmap-event|label>",
1849 DEBUG_STR
1850 BGP_STR
1851 "VPN routes\n"
1852 "leaked from vrf to vpn\n"
1853 "leaked to vrf from vpn\n"
1854 "route-map updates\n"
1855 "labels\n")
1856{
1857 int idx = 3;
1858
1859 if (argv_find(argv, argc, "leak-from-vrf", &idx)) {
1860 if (vty->node == CONFIG_NODE)
1861 DEBUG_ON(vpn, VPN_LEAK_FROM_VRF);
1862 else
1863 TERM_DEBUG_ON(vpn, VPN_LEAK_FROM_VRF);
1864 } else if (argv_find(argv, argc, "leak-to-vrf", &idx)) {
1865 if (vty->node == CONFIG_NODE)
1866 DEBUG_ON(vpn, VPN_LEAK_TO_VRF);
1867 else
1868 TERM_DEBUG_ON(vpn, VPN_LEAK_TO_VRF);
1869 } else if (argv_find(argv, argc, "rmap-event", &idx)) {
1870 if (vty->node == CONFIG_NODE)
1871 DEBUG_ON(vpn, VPN_LEAK_RMAP_EVENT);
1872 else
1873 TERM_DEBUG_ON(vpn, VPN_LEAK_RMAP_EVENT);
1874 } else if (argv_find(argv, argc, "label", &idx)) {
1875 if (vty->node == CONFIG_NODE)
1876 DEBUG_ON(vpn, VPN_LEAK_LABEL);
1877 else
1878 TERM_DEBUG_ON(vpn, VPN_LEAK_LABEL);
1879 } else {
1880 vty_out(vty, "%% unknown debug bgp vpn keyword\n");
1881 return CMD_WARNING_CONFIG_FAILED;
1882 }
1883
1884 if (vty->node != CONFIG_NODE)
1885 vty_out(vty, "enabled debug bgp vpn %s\n", argv[idx]->text);
1886
1887 return CMD_SUCCESS;
1888}
1889
1890DEFUN (no_debug_bgp_vpn,
1891 no_debug_bgp_vpn_cmd,
1892 "no debug bgp vpn <leak-from-vrf|leak-to-vrf|rmap-event|label>",
1893 NO_STR
1894 DEBUG_STR
1895 BGP_STR
1896 "VPN routes\n"
1897 "leaked from vrf to vpn\n"
1898 "leaked to vrf from vpn\n"
1899 "route-map updates\n"
1900 "labels\n")
1901{
1902 int idx = 4;
1903
1904 if (argv_find(argv, argc, "leak-from-vrf", &idx)) {
1905 if (vty->node == CONFIG_NODE)
1906 DEBUG_OFF(vpn, VPN_LEAK_FROM_VRF);
1907 else
1908 TERM_DEBUG_OFF(vpn, VPN_LEAK_FROM_VRF);
1909
1910 } else if (argv_find(argv, argc, "leak-to-vrf", &idx)) {
1911 if (vty->node == CONFIG_NODE)
1912 DEBUG_OFF(vpn, VPN_LEAK_TO_VRF);
1913 else
1914 TERM_DEBUG_OFF(vpn, VPN_LEAK_TO_VRF);
1915 } else if (argv_find(argv, argc, "rmap-event", &idx)) {
1916 if (vty->node == CONFIG_NODE)
1917 DEBUG_OFF(vpn, VPN_LEAK_RMAP_EVENT);
1918 else
1919 TERM_DEBUG_OFF(vpn, VPN_LEAK_RMAP_EVENT);
1920 } else if (argv_find(argv, argc, "label", &idx)) {
1921 if (vty->node == CONFIG_NODE)
1922 DEBUG_OFF(vpn, VPN_LEAK_LABEL);
1923 else
1924 TERM_DEBUG_OFF(vpn, VPN_LEAK_LABEL);
1925 } else {
1926 vty_out(vty, "%% unknown debug bgp vpn keyword\n");
1927 return CMD_WARNING_CONFIG_FAILED;
1928 }
1929
1930 if (vty->node != CONFIG_NODE)
1931 vty_out(vty, "disabled debug bgp vpn %s\n", argv[idx]->text);
1a80fc0f
PG
1932 return CMD_SUCCESS;
1933}
ddb5b488 1934
1a80fc0f
PG
1935/* debug bgp pbr */
1936DEFUN (debug_bgp_pbr,
1937 debug_bgp_pbr_cmd,
e9d0aa99 1938 "debug bgp pbr [error]",
1a80fc0f
PG
1939 DEBUG_STR
1940 BGP_STR
e9d0aa99
PG
1941 "BGP policy based routing\n"
1942 "BGP PBR error\n")
1a80fc0f 1943{
e9d0aa99
PG
1944 int idx = 3;
1945
1946 if (argv_find(argv, argc, "error", &idx)) {
1947 if (vty->node == CONFIG_NODE)
1948 DEBUG_ON(pbr, PBR_ERROR);
1949 else {
1950 TERM_DEBUG_ON(pbr, PBR_ERROR);
1951 vty_out(vty, "BGP policy based routing error is on\n");
1952 }
1953 return CMD_SUCCESS;
1954 }
1a80fc0f
PG
1955 if (vty->node == CONFIG_NODE)
1956 DEBUG_ON(pbr, PBR);
1957 else {
1958 TERM_DEBUG_ON(pbr, PBR);
1959 vty_out(vty, "BGP policy based routing is on\n");
1960 }
1961 return CMD_SUCCESS;
1962}
1963
1964DEFUN (no_debug_bgp_pbr,
1965 no_debug_bgp_pbr_cmd,
e9d0aa99 1966 "no debug bgp pbr [error]",
1a80fc0f
PG
1967 NO_STR
1968 DEBUG_STR
1969 BGP_STR
e9d0aa99
PG
1970 "BGP policy based routing\n"
1971 "BGP PBR Error\n")
1a80fc0f 1972{
e9d0aa99
PG
1973 int idx = 3;
1974
1975 if (argv_find(argv, argc, "error", &idx)) {
1976 if (vty->node == CONFIG_NODE)
1977 DEBUG_OFF(pbr, PBR_ERROR);
1978 else {
1979 TERM_DEBUG_OFF(pbr, PBR_ERROR);
1980 vty_out(vty, "BGP policy based routing error is off\n");
1981 }
1982 return CMD_SUCCESS;
1983 }
1a80fc0f
PG
1984 if (vty->node == CONFIG_NODE)
1985 DEBUG_OFF(pbr, PBR);
1986 else {
1987 TERM_DEBUG_OFF(pbr, PBR);
1988 vty_out(vty, "BGP policy based routing is off\n");
1989 }
ddb5b488
PZ
1990 return CMD_SUCCESS;
1991}
1992
955bfd98
PZ
1993DEFUN (debug_bgp_labelpool,
1994 debug_bgp_labelpool_cmd,
1995 "debug bgp labelpool",
1996 DEBUG_STR
1997 BGP_STR
1998 "label pool\n")
1999{
2000 if (vty->node == CONFIG_NODE)
2001 DEBUG_ON(labelpool, LABELPOOL);
2002 else
2003 TERM_DEBUG_ON(labelpool, LABELPOOL);
2004
2005 if (vty->node != CONFIG_NODE)
2006 vty_out(vty, "enabled debug bgp labelpool\n");
2007
2008 return CMD_SUCCESS;
2009}
2010
2011DEFUN (no_debug_bgp_labelpool,
2012 no_debug_bgp_labelpool_cmd,
2013 "no debug bgp labelpool",
2014 NO_STR
2015 DEBUG_STR
2016 BGP_STR
2017 "label pool\n")
2018{
2019 if (vty->node == CONFIG_NODE)
2020 DEBUG_OFF(labelpool, LABELPOOL);
2021 else
2022 TERM_DEBUG_OFF(labelpool, LABELPOOL);
2023
2024
2025 if (vty->node != CONFIG_NODE)
2026 vty_out(vty, "disabled debug bgp labelpool\n");
2027
2028 return CMD_SUCCESS;
2029}
2030
16286195
DS
2031DEFUN (no_debug_bgp,
2032 no_debug_bgp_cmd,
2033 "no debug bgp",
718e3744 2034 NO_STR
2035 DEBUG_STR
718e3744 2036 BGP_STR)
2037{
87f42c2c
QY
2038 struct bgp *bgp;
2039 struct listnode *ln;
2040
d62a17ae 2041 bgp_debug_list_free(bgp_debug_neighbor_events_peers);
2042 bgp_debug_list_free(bgp_debug_keepalive_peers);
2043 bgp_debug_list_free(bgp_debug_update_in_peers);
2044 bgp_debug_list_free(bgp_debug_update_out_peers);
2045 bgp_debug_list_free(bgp_debug_update_prefixes);
2046 bgp_debug_list_free(bgp_debug_bestpath_prefixes);
2047 bgp_debug_list_free(bgp_debug_zebra_prefixes);
2048
87f42c2c
QY
2049 for (ALL_LIST_ELEMENTS_RO(bm->bgp, ln, bgp))
2050 bgp_debug_clear_updgrp_update_dbg(bgp);
d62a17ae 2051
2052 TERM_DEBUG_OFF(keepalive, KEEPALIVE);
2053 TERM_DEBUG_OFF(update, UPDATE_IN);
2054 TERM_DEBUG_OFF(update, UPDATE_OUT);
2055 TERM_DEBUG_OFF(update, UPDATE_PREFIX);
2056 TERM_DEBUG_OFF(bestpath, BESTPATH);
2057 TERM_DEBUG_OFF(as4, AS4);
2058 TERM_DEBUG_OFF(as4, AS4_SEGMENT);
2059 TERM_DEBUG_OFF(neighbor_events, NEIGHBOR_EVENTS);
2060 TERM_DEBUG_OFF(zebra, ZEBRA);
2061 TERM_DEBUG_OFF(allow_martians, ALLOW_MARTIANS);
c33b83b3 2062 TERM_DEBUG_OFF(nht, NHT);
ddb5b488
PZ
2063 TERM_DEBUG_OFF(vpn, VPN_LEAK_FROM_VRF);
2064 TERM_DEBUG_OFF(vpn, VPN_LEAK_TO_VRF);
2065 TERM_DEBUG_OFF(vpn, VPN_LEAK_RMAP_EVENT);
2066 TERM_DEBUG_OFF(vpn, VPN_LEAK_LABEL);
268e1b9b 2067 TERM_DEBUG_OFF(flowspec, FLOWSPEC);
955bfd98 2068 TERM_DEBUG_OFF(labelpool, LABELPOOL);
1a80fc0f 2069 TERM_DEBUG_OFF(pbr, PBR);
e9d0aa99 2070 TERM_DEBUG_OFF(pbr, PBR_ERROR);
d62a17ae 2071 vty_out(vty, "All possible debugging has been turned off\n");
2072
2073 return CMD_SUCCESS;
718e3744 2074}
2075
87f6dc50
DS
2076DEFUN_NOSH (show_debugging_bgp,
2077 show_debugging_bgp_cmd,
2078 "show debugging [bgp]",
2079 SHOW_STR
2080 DEBUG_STR
2081 BGP_STR)
718e3744 2082{
d62a17ae 2083 vty_out(vty, "BGP debugging status:\n");
718e3744 2084
d62a17ae 2085 if (BGP_DEBUG(as4, AS4))
2086 vty_out(vty, " BGP as4 debugging is on\n");
16286195 2087
d62a17ae 2088 if (BGP_DEBUG(as4, AS4_SEGMENT))
2089 vty_out(vty, " BGP as4 aspath segment debugging is on\n");
16286195 2090
d62a17ae 2091 if (BGP_DEBUG(bestpath, BESTPATH))
2092 bgp_debug_list_print(vty, " BGP bestpath debugging is on",
81cf0de5 2093 bgp_debug_bestpath_prefixes, 0);
16286195 2094
d62a17ae 2095 if (BGP_DEBUG(keepalive, KEEPALIVE))
2096 bgp_debug_list_print(vty, " BGP keepalives debugging is on",
81cf0de5 2097 bgp_debug_keepalive_peers, 0);
16286195 2098
d62a17ae 2099 if (BGP_DEBUG(neighbor_events, NEIGHBOR_EVENTS))
2100 bgp_debug_list_print(vty,
2101 " BGP neighbor-events debugging is on",
81cf0de5 2102 bgp_debug_neighbor_events_peers, 0);
91ba2c8b 2103
d62a17ae 2104 if (BGP_DEBUG(nht, NHT))
2105 vty_out(vty, " BGP next-hop tracking debugging is on\n");
16286195 2106
d62a17ae 2107 if (BGP_DEBUG(update_groups, UPDATE_GROUPS))
2108 vty_out(vty, " BGP update-groups debugging is on\n");
91ba2c8b 2109
d62a17ae 2110 if (BGP_DEBUG(update, UPDATE_PREFIX))
2111 bgp_debug_list_print(vty, " BGP updates debugging is on",
81cf0de5 2112 bgp_debug_update_prefixes, 1);
16286195 2113
d62a17ae 2114 if (BGP_DEBUG(update, UPDATE_IN))
2115 bgp_debug_list_print(vty,
2116 " BGP updates debugging is on (inbound)",
81cf0de5 2117 bgp_debug_update_in_peers, 0);
16286195 2118
d62a17ae 2119 if (BGP_DEBUG(update, UPDATE_OUT))
2120 bgp_debug_list_print(vty,
2121 " BGP updates debugging is on (outbound)",
81cf0de5 2122 bgp_debug_update_out_peers, 0);
16286195 2123
d62a17ae 2124 if (BGP_DEBUG(zebra, ZEBRA))
2125 bgp_debug_list_print(vty, " BGP zebra debugging is on",
81cf0de5 2126 bgp_debug_zebra_prefixes, 0);
16286195 2127
d62a17ae 2128 if (BGP_DEBUG(allow_martians, ALLOW_MARTIANS))
2129 vty_out(vty, " BGP allow martian next hop debugging is on\n");
ddb5b488
PZ
2130
2131 if (BGP_DEBUG(vpn, VPN_LEAK_FROM_VRF))
2132 vty_out(vty,
2133 " BGP route leak from vrf to vpn debugging is on\n");
2134 if (BGP_DEBUG(vpn, VPN_LEAK_TO_VRF))
2135 vty_out(vty,
2136 " BGP route leak to vrf from vpn debugging is on\n");
2137 if (BGP_DEBUG(vpn, VPN_LEAK_RMAP_EVENT))
2138 vty_out(vty, " BGP vpn route-map event debugging is on\n");
2139 if (BGP_DEBUG(vpn, VPN_LEAK_LABEL))
2140 vty_out(vty, " BGP vpn label event debugging is on\n");
268e1b9b
PG
2141 if (BGP_DEBUG(flowspec, FLOWSPEC))
2142 vty_out(vty, " BGP flowspec debugging is on\n");
955bfd98
PZ
2143 if (BGP_DEBUG(labelpool, LABELPOOL))
2144 vty_out(vty, " BGP labelpool debugging is on\n");
ddb5b488 2145
1a80fc0f
PG
2146 if (BGP_DEBUG(pbr, PBR))
2147 vty_out(vty, " BGP policy based routing debugging is on\n");
e9d0aa99
PG
2148 if (BGP_DEBUG(pbr, PBR_ERROR))
2149 vty_out(vty, " BGP policy based routing error debugging is on\n");
1a80fc0f 2150
d62a17ae 2151 vty_out(vty, "\n");
2152 return CMD_SUCCESS;
718e3744 2153}
2154
90dcf2d7 2155/* return count of number of debug flags set */
d62a17ae 2156int bgp_debug_count(void)
90dcf2d7 2157{
d62a17ae 2158 int ret = 0;
2159 if (BGP_DEBUG(as4, AS4))
2160 ret++;
90dcf2d7 2161
d62a17ae 2162 if (BGP_DEBUG(as4, AS4_SEGMENT))
2163 ret++;
90dcf2d7 2164
d62a17ae 2165 if (BGP_DEBUG(bestpath, BESTPATH))
2166 ret++;
90dcf2d7 2167
d62a17ae 2168 if (BGP_DEBUG(keepalive, KEEPALIVE))
2169 ret++;
90dcf2d7 2170
d62a17ae 2171 if (BGP_DEBUG(neighbor_events, NEIGHBOR_EVENTS))
2172 ret++;
90dcf2d7 2173
d62a17ae 2174 if (BGP_DEBUG(nht, NHT))
2175 ret++;
90dcf2d7 2176
d62a17ae 2177 if (BGP_DEBUG(update_groups, UPDATE_GROUPS))
2178 ret++;
90dcf2d7 2179
d62a17ae 2180 if (BGP_DEBUG(update, UPDATE_PREFIX))
2181 ret++;
90dcf2d7 2182
d62a17ae 2183 if (BGP_DEBUG(update, UPDATE_IN))
2184 ret++;
90dcf2d7 2185
d62a17ae 2186 if (BGP_DEBUG(update, UPDATE_OUT))
2187 ret++;
90dcf2d7 2188
d62a17ae 2189 if (BGP_DEBUG(zebra, ZEBRA))
2190 ret++;
90dcf2d7 2191
d62a17ae 2192 if (BGP_DEBUG(allow_martians, ALLOW_MARTIANS))
2193 ret++;
90dcf2d7 2194
ddb5b488
PZ
2195 if (BGP_DEBUG(vpn, VPN_LEAK_FROM_VRF))
2196 ret++;
2197 if (BGP_DEBUG(vpn, VPN_LEAK_TO_VRF))
2198 ret++;
2199 if (BGP_DEBUG(vpn, VPN_LEAK_RMAP_EVENT))
2200 ret++;
2201 if (BGP_DEBUG(vpn, VPN_LEAK_LABEL))
2202 ret++;
268e1b9b
PG
2203 if (BGP_DEBUG(flowspec, FLOWSPEC))
2204 ret++;
955bfd98
PZ
2205 if (BGP_DEBUG(labelpool, LABELPOOL))
2206 ret++;
ddb5b488 2207
1a80fc0f
PG
2208 if (BGP_DEBUG(pbr, PBR))
2209 ret++;
e9d0aa99
PG
2210 if (BGP_DEBUG(pbr, PBR_ERROR))
2211 ret++;
1a80fc0f 2212
d62a17ae 2213 return ret;
90dcf2d7
LB
2214}
2215
d62a17ae 2216static int bgp_config_write_debug(struct vty *vty)
718e3744 2217{
d62a17ae 2218 int write = 0;
718e3744 2219
d62a17ae 2220 if (CONF_BGP_DEBUG(as4, AS4)) {
2221 vty_out(vty, "debug bgp as4\n");
2222 write++;
2223 }
718e3744 2224
d62a17ae 2225 if (CONF_BGP_DEBUG(as4, AS4_SEGMENT)) {
2226 vty_out(vty, "debug bgp as4 segment\n");
2227 write++;
2228 }
16286195 2229
d62a17ae 2230 if (CONF_BGP_DEBUG(bestpath, BESTPATH)) {
2231 write += bgp_debug_list_conf_print(vty, "debug bgp bestpath",
81cf0de5
CS
2232 bgp_debug_bestpath_prefixes,
2233 0);
d62a17ae 2234 }
2235
2236 if (CONF_BGP_DEBUG(keepalive, KEEPALIVE)) {
2237 write += bgp_debug_list_conf_print(vty, "debug bgp keepalives",
81cf0de5
CS
2238 bgp_debug_keepalive_peers,
2239 0);
d62a17ae 2240 }
2241
2242 if (CONF_BGP_DEBUG(neighbor_events, NEIGHBOR_EVENTS)) {
2243 write += bgp_debug_list_conf_print(
2244 vty, "debug bgp neighbor-events",
81cf0de5 2245 bgp_debug_neighbor_events_peers, 0);
d62a17ae 2246 }
2247
2248 if (CONF_BGP_DEBUG(nht, NHT)) {
2249 vty_out(vty, "debug bgp nht\n");
2250 write++;
2251 }
2252
2253 if (CONF_BGP_DEBUG(update_groups, UPDATE_GROUPS)) {
2254 vty_out(vty, "debug bgp update-groups\n");
2255 write++;
2256 }
2257
2258 if (CONF_BGP_DEBUG(update, UPDATE_PREFIX)) {
2259 write += bgp_debug_list_conf_print(vty,
2260 "debug bgp updates prefix",
81cf0de5
CS
2261 bgp_debug_update_prefixes,
2262 1);
d62a17ae 2263 }
2264
2265 if (CONF_BGP_DEBUG(update, UPDATE_IN)) {
2266 write += bgp_debug_list_conf_print(vty, "debug bgp updates in",
81cf0de5
CS
2267 bgp_debug_update_in_peers,
2268 0);
d62a17ae 2269 }
2270
2271 if (CONF_BGP_DEBUG(update, UPDATE_OUT)) {
2272 write += bgp_debug_list_conf_print(vty, "debug bgp updates out",
81cf0de5
CS
2273 bgp_debug_update_out_peers,
2274 0);
d62a17ae 2275 }
16286195 2276
d62a17ae 2277 if (CONF_BGP_DEBUG(zebra, ZEBRA)) {
2278 if (!bgp_debug_zebra_prefixes
2279 || list_isempty(bgp_debug_zebra_prefixes)) {
2280 vty_out(vty, "debug bgp zebra\n");
2281 write++;
2282 } else {
2283 write += bgp_debug_list_conf_print(
2284 vty, "debug bgp zebra prefix",
81cf0de5 2285 bgp_debug_zebra_prefixes, 0);
d62a17ae 2286 }
2287 }
16286195 2288
d62a17ae 2289 if (CONF_BGP_DEBUG(allow_martians, ALLOW_MARTIANS)) {
2290 vty_out(vty, "debug bgp allow-martians\n");
2291 write++;
2292 }
16286195 2293
ddb5b488
PZ
2294 if (CONF_BGP_DEBUG(vpn, VPN_LEAK_FROM_VRF)) {
2295 vty_out(vty, "debug bgp vpn leak-from-vrf\n");
2296 write++;
2297 }
2298 if (CONF_BGP_DEBUG(vpn, VPN_LEAK_TO_VRF)) {
2299 vty_out(vty, "debug bgp vpn leak-to-vrf\n");
2300 write++;
2301 }
2302 if (CONF_BGP_DEBUG(vpn, VPN_LEAK_RMAP_EVENT)) {
2303 vty_out(vty, "debug bgp vpn rmap-event\n");
2304 write++;
2305 }
2306 if (CONF_BGP_DEBUG(vpn, VPN_LEAK_LABEL)) {
2307 vty_out(vty, "debug bgp vpn label\n");
2308 write++;
2309 }
268e1b9b
PG
2310 if (CONF_BGP_DEBUG(flowspec, FLOWSPEC)) {
2311 vty_out(vty, "debug bgp flowspec\n");
2312 write++;
2313 }
955bfd98
PZ
2314 if (CONF_BGP_DEBUG(labelpool, LABELPOOL)) {
2315 vty_out(vty, "debug bgp labelpool\n");
2316 write++;
2317 }
ddb5b488 2318
1a80fc0f
PG
2319 if (CONF_BGP_DEBUG(pbr, PBR)) {
2320 vty_out(vty, "debug bgp pbr\n");
2321 write++;
e9d0aa99
PG
2322 }
2323 if (CONF_BGP_DEBUG(pbr, PBR_ERROR)) {
2324 vty_out(vty, "debug bgp pbr error\n");
2325 write++;
1a80fc0f 2326 }
d62a17ae 2327 return write;
2328}
16286195 2329
d62a17ae 2330static struct cmd_node debug_node = {DEBUG_NODE, "", 1};
2331
2332void bgp_debug_init(void)
2333{
2334 install_node(&debug_node, bgp_config_write_debug);
2335
2336 install_element(ENABLE_NODE, &show_debugging_bgp_cmd);
2337
2338 install_element(ENABLE_NODE, &debug_bgp_as4_cmd);
2339 install_element(CONFIG_NODE, &debug_bgp_as4_cmd);
2340 install_element(ENABLE_NODE, &debug_bgp_as4_segment_cmd);
2341 install_element(CONFIG_NODE, &debug_bgp_as4_segment_cmd);
2342
2343 install_element(ENABLE_NODE, &debug_bgp_neighbor_events_cmd);
2344 install_element(CONFIG_NODE, &debug_bgp_neighbor_events_cmd);
2345 install_element(ENABLE_NODE, &debug_bgp_nht_cmd);
2346 install_element(CONFIG_NODE, &debug_bgp_nht_cmd);
2347 install_element(ENABLE_NODE, &debug_bgp_keepalive_cmd);
2348 install_element(CONFIG_NODE, &debug_bgp_keepalive_cmd);
2349 install_element(ENABLE_NODE, &debug_bgp_update_cmd);
2350 install_element(CONFIG_NODE, &debug_bgp_update_cmd);
2351 install_element(ENABLE_NODE, &debug_bgp_zebra_cmd);
2352 install_element(CONFIG_NODE, &debug_bgp_zebra_cmd);
2353 install_element(ENABLE_NODE, &debug_bgp_allow_martians_cmd);
2354 install_element(CONFIG_NODE, &debug_bgp_allow_martians_cmd);
2355 install_element(ENABLE_NODE, &debug_bgp_update_groups_cmd);
2356 install_element(CONFIG_NODE, &debug_bgp_update_groups_cmd);
2357 install_element(ENABLE_NODE, &debug_bgp_bestpath_prefix_cmd);
2358 install_element(CONFIG_NODE, &debug_bgp_bestpath_prefix_cmd);
2359
2360 /* debug bgp updates (in|out) */
2361 install_element(ENABLE_NODE, &debug_bgp_update_direct_cmd);
2362 install_element(CONFIG_NODE, &debug_bgp_update_direct_cmd);
2363 install_element(ENABLE_NODE, &no_debug_bgp_update_direct_cmd);
2364 install_element(CONFIG_NODE, &no_debug_bgp_update_direct_cmd);
2365
2366 /* debug bgp updates (in|out) A.B.C.D */
2367 install_element(ENABLE_NODE, &debug_bgp_update_direct_peer_cmd);
2368 install_element(CONFIG_NODE, &debug_bgp_update_direct_peer_cmd);
2369 install_element(ENABLE_NODE, &no_debug_bgp_update_direct_peer_cmd);
2370 install_element(CONFIG_NODE, &no_debug_bgp_update_direct_peer_cmd);
2371
2372 /* debug bgp updates prefix A.B.C.D/M */
2373 install_element(ENABLE_NODE, &debug_bgp_update_prefix_cmd);
2374 install_element(CONFIG_NODE, &debug_bgp_update_prefix_cmd);
2375 install_element(ENABLE_NODE, &no_debug_bgp_update_prefix_cmd);
2376 install_element(CONFIG_NODE, &no_debug_bgp_update_prefix_cmd);
81cf0de5
CS
2377 install_element(ENABLE_NODE, &debug_bgp_update_prefix_afi_safi_cmd);
2378 install_element(CONFIG_NODE, &debug_bgp_update_prefix_afi_safi_cmd);
2379 install_element(ENABLE_NODE, &no_debug_bgp_update_prefix_afi_safi_cmd);
2380 install_element(CONFIG_NODE, &no_debug_bgp_update_prefix_afi_safi_cmd);
d62a17ae 2381
2382 /* debug bgp zebra prefix A.B.C.D/M */
2383 install_element(ENABLE_NODE, &debug_bgp_zebra_prefix_cmd);
2384 install_element(CONFIG_NODE, &debug_bgp_zebra_prefix_cmd);
2385 install_element(ENABLE_NODE, &no_debug_bgp_zebra_prefix_cmd);
2386 install_element(CONFIG_NODE, &no_debug_bgp_zebra_prefix_cmd);
2387
2388 install_element(ENABLE_NODE, &no_debug_bgp_as4_cmd);
2389 install_element(CONFIG_NODE, &no_debug_bgp_as4_cmd);
2390 install_element(ENABLE_NODE, &no_debug_bgp_as4_segment_cmd);
2391 install_element(CONFIG_NODE, &no_debug_bgp_as4_segment_cmd);
2392
2393 /* debug bgp neighbor-events A.B.C.D */
2394 install_element(ENABLE_NODE, &debug_bgp_neighbor_events_peer_cmd);
2395 install_element(CONFIG_NODE, &debug_bgp_neighbor_events_peer_cmd);
2396 install_element(ENABLE_NODE, &no_debug_bgp_neighbor_events_peer_cmd);
2397 install_element(CONFIG_NODE, &no_debug_bgp_neighbor_events_peer_cmd);
2398
2399 /* debug bgp keepalive A.B.C.D */
2400 install_element(ENABLE_NODE, &debug_bgp_keepalive_peer_cmd);
2401 install_element(CONFIG_NODE, &debug_bgp_keepalive_peer_cmd);
2402 install_element(ENABLE_NODE, &no_debug_bgp_keepalive_peer_cmd);
2403 install_element(CONFIG_NODE, &no_debug_bgp_keepalive_peer_cmd);
2404
2405 install_element(ENABLE_NODE, &no_debug_bgp_neighbor_events_cmd);
2406 install_element(CONFIG_NODE, &no_debug_bgp_neighbor_events_cmd);
2407 install_element(ENABLE_NODE, &no_debug_bgp_nht_cmd);
2408 install_element(CONFIG_NODE, &no_debug_bgp_nht_cmd);
2409 install_element(ENABLE_NODE, &no_debug_bgp_keepalive_cmd);
2410 install_element(CONFIG_NODE, &no_debug_bgp_keepalive_cmd);
2411 install_element(ENABLE_NODE, &no_debug_bgp_update_cmd);
2412 install_element(CONFIG_NODE, &no_debug_bgp_update_cmd);
2413 install_element(ENABLE_NODE, &no_debug_bgp_zebra_cmd);
2414 install_element(CONFIG_NODE, &no_debug_bgp_zebra_cmd);
2415 install_element(ENABLE_NODE, &no_debug_bgp_allow_martians_cmd);
2416 install_element(CONFIG_NODE, &no_debug_bgp_allow_martians_cmd);
2417 install_element(ENABLE_NODE, &no_debug_bgp_update_groups_cmd);
2418 install_element(CONFIG_NODE, &no_debug_bgp_update_groups_cmd);
2419 install_element(ENABLE_NODE, &no_debug_bgp_cmd);
2420 install_element(ENABLE_NODE, &no_debug_bgp_bestpath_cmd);
2421 install_element(CONFIG_NODE, &no_debug_bgp_bestpath_cmd);
2422 install_element(ENABLE_NODE, &no_debug_bgp_bestpath_prefix_cmd);
2423 install_element(CONFIG_NODE, &no_debug_bgp_bestpath_prefix_cmd);
ddb5b488
PZ
2424
2425 install_element(ENABLE_NODE, &debug_bgp_vpn_cmd);
2426 install_element(CONFIG_NODE, &debug_bgp_vpn_cmd);
2427 install_element(ENABLE_NODE, &no_debug_bgp_vpn_cmd);
2428 install_element(CONFIG_NODE, &no_debug_bgp_vpn_cmd);
955bfd98
PZ
2429
2430 install_element(ENABLE_NODE, &debug_bgp_labelpool_cmd);
2431 install_element(CONFIG_NODE, &debug_bgp_labelpool_cmd);
2432 install_element(ENABLE_NODE, &no_debug_bgp_labelpool_cmd);
2433 install_element(CONFIG_NODE, &no_debug_bgp_labelpool_cmd);
1a80fc0f
PG
2434
2435 /* debug bgp pbr */
2436 install_element(ENABLE_NODE, &debug_bgp_pbr_cmd);
2437 install_element(CONFIG_NODE, &debug_bgp_pbr_cmd);
2438 install_element(ENABLE_NODE, &no_debug_bgp_pbr_cmd);
2439 install_element(CONFIG_NODE, &no_debug_bgp_pbr_cmd);
2440
d62a17ae 2441}
16286195 2442
d62a17ae 2443/* Return true if this prefix is on the per_prefix_list of prefixes to debug
2444 * for BGP_DEBUG_TYPE
2445 */
2446static int bgp_debug_per_prefix(struct prefix *p,
2447 unsigned long term_bgp_debug_type,
2448 unsigned int BGP_DEBUG_TYPE,
2449 struct list *per_prefix_list)
2450{
2451 struct bgp_debug_filter *filter;
2452 struct listnode *node, *nnode;
2453
2454 if (term_bgp_debug_type & BGP_DEBUG_TYPE) {
2455 /* We are debugging all prefixes so return true */
2456 if (!per_prefix_list || list_isempty(per_prefix_list))
2457 return 1;
2458
2459 else {
2460 if (!p)
2461 return 0;
2462
2463 for (ALL_LIST_ELEMENTS(per_prefix_list, node, nnode,
2464 filter))
2465 if (filter->p->prefixlen == p->prefixlen
2466 && prefix_match(filter->p, p))
2467 return 1;
2468
2469 return 0;
2470 }
2471 }
2472
2473 return 0;
16286195
DS
2474}
2475
2476/* Return true if this peer is on the per_peer_list of peers to debug
2477 * for BGP_DEBUG_TYPE
2478 */
d62a17ae 2479static int bgp_debug_per_peer(char *host, unsigned long term_bgp_debug_type,
2480 unsigned int BGP_DEBUG_TYPE,
2481 struct list *per_peer_list)
16286195 2482{
d62a17ae 2483 struct bgp_debug_filter *filter;
2484 struct listnode *node, *nnode;
16286195 2485
d62a17ae 2486 if (term_bgp_debug_type & BGP_DEBUG_TYPE) {
2487 /* We are debugging all peers so return true */
2488 if (!per_peer_list || list_isempty(per_peer_list))
2489 return 1;
16286195 2490
d62a17ae 2491 else {
2492 if (!host)
2493 return 0;
16286195 2494
d62a17ae 2495 for (ALL_LIST_ELEMENTS(per_peer_list, node, nnode,
2496 filter))
2497 if (strcmp(filter->host, host) == 0)
2498 return 1;
16286195 2499
d62a17ae 2500 return 0;
2501 }
2502 }
16286195 2503
d62a17ae 2504 return 0;
16286195
DS
2505}
2506
d62a17ae 2507int bgp_debug_neighbor_events(struct peer *peer)
16286195 2508{
d62a17ae 2509 char *host = NULL;
167d390a 2510
d62a17ae 2511 if (peer)
2512 host = peer->host;
167d390a 2513
d62a17ae 2514 return bgp_debug_per_peer(host, term_bgp_debug_neighbor_events,
2515 BGP_DEBUG_NEIGHBOR_EVENTS,
2516 bgp_debug_neighbor_events_peers);
16286195
DS
2517}
2518
d62a17ae 2519int bgp_debug_keepalive(struct peer *peer)
16286195 2520{
d62a17ae 2521 char *host = NULL;
167d390a 2522
d62a17ae 2523 if (peer)
2524 host = peer->host;
167d390a 2525
d62a17ae 2526 return bgp_debug_per_peer(host, term_bgp_debug_keepalive,
2527 BGP_DEBUG_KEEPALIVE,
2528 bgp_debug_keepalive_peers);
16286195
DS
2529}
2530
d62a17ae 2531int bgp_debug_update(struct peer *peer, struct prefix *p,
2532 struct update_group *updgrp, unsigned int inbound)
16286195 2533{
d62a17ae 2534 char *host = NULL;
2535
2536 if (peer)
2537 host = peer->host;
2538
2539 if (inbound) {
2540 if (bgp_debug_per_peer(host, term_bgp_debug_update,
2541 BGP_DEBUG_UPDATE_IN,
2542 bgp_debug_update_in_peers))
2543 return 1;
2544 }
2545
2546 /* outbound */
2547 else {
2548 if (bgp_debug_per_peer(host, term_bgp_debug_update,
2549 BGP_DEBUG_UPDATE_OUT,
2550 bgp_debug_update_out_peers))
2551 return 1;
2552
2553 /* Check if update debugging implicitly enabled for the group.
2554 */
2555 if (updgrp && UPDGRP_DBG_ON(updgrp))
2556 return 1;
2557 }
2558
2559
2560 if (BGP_DEBUG(update, UPDATE_PREFIX)) {
2561 if (bgp_debug_per_prefix(p, term_bgp_debug_update,
2562 BGP_DEBUG_UPDATE_PREFIX,
2563 bgp_debug_update_prefixes))
2564 return 1;
2565 }
2566
2567 return 0;
16286195
DS
2568}
2569
d62a17ae 2570int bgp_debug_bestpath(struct prefix *p)
9fbdd100 2571{
d62a17ae 2572 if (BGP_DEBUG(bestpath, BESTPATH)) {
2573 if (bgp_debug_per_prefix(p, term_bgp_debug_bestpath,
2574 BGP_DEBUG_BESTPATH,
2575 bgp_debug_bestpath_prefixes))
2576 return 1;
2577 }
2578
2579 return 0;
9fbdd100
DS
2580}
2581
d62a17ae 2582int bgp_debug_zebra(struct prefix *p)
16286195 2583{
d62a17ae 2584 if (BGP_DEBUG(zebra, ZEBRA)) {
2585 if (bgp_debug_per_prefix(p, term_bgp_debug_zebra,
2586 BGP_DEBUG_ZEBRA,
2587 bgp_debug_zebra_prefixes))
2588 return 1;
2589 }
2590
2591 return 0;
718e3744 2592}
906ad49b 2593
d62a17ae 2594const char *bgp_debug_rdpfxpath2str(afi_t afi, safi_t safi,
2595 struct prefix_rd *prd,
2596 union prefixconstptr pu,
d7c0a89a
QY
2597 mpls_label_t *label, uint32_t num_labels,
2598 int addpath_valid, uint32_t addpath_id,
b57ba6d2 2599 char *str, int size)
d62a17ae 2600{
2601 char rd_buf[RD_ADDRSTRLEN];
2602 char pfx_buf[PREFIX_STRLEN];
2603 char tag_buf[30];
2604 /* ' with addpath ID ' 17
2605 * max strlen of uint32 + 10
2606 * +/- (just in case) + 1
2607 * null terminator + 1
2608 * ============================ 29 */
2609 char pathid_buf[30];
2610
2611 if (size < BGP_PRD_PATH_STRLEN)
2612 return NULL;
2613
2614 /* Note: Path-id is created by default, but only included in update
2615 * sometimes. */
2616 pathid_buf[0] = '\0';
2617 if (addpath_valid)
2618 snprintf(pathid_buf, sizeof(pathid_buf), " with addpath ID %u",
2619 addpath_id);
2620
2621 tag_buf[0] = '\0';
b57ba6d2 2622 if (bgp_labeled_safi(safi) && num_labels) {
d62a17ae 2623
b57ba6d2
MK
2624 if (safi == SAFI_EVPN) {
2625 char tag_buf2[20];
2626
2627 bgp_evpn_label2str(label, num_labels, tag_buf2, 20);
2628 sprintf(tag_buf, " label %s", tag_buf2);
2629 } else {
d7c0a89a 2630 uint32_t label_value;
b57ba6d2
MK
2631
2632 label_value = decode_label(label);
2633 sprintf(tag_buf, " label %u", label_value);
2634 }
d62a17ae 2635 }
2636
2637 if (prd)
e1af3f16 2638 snprintf(str, size, "RD %s %s%s%s %s %s",
d62a17ae 2639 prefix_rd2str(prd, rd_buf, sizeof(rd_buf)),
2640 prefix2str(pu, pfx_buf, sizeof(pfx_buf)), tag_buf,
e1af3f16 2641 pathid_buf, afi2str(afi), safi2str(safi));
268e1b9b
PG
2642 else if (safi == SAFI_FLOWSPEC) {
2643 char return_string[BGP_FLOWSPEC_NLRI_STRING_MAX];
2644 const struct prefix_fs *fs = pu.fs;
2645
2646 bgp_fs_nlri_get_string((unsigned char *)fs->prefix.ptr,
2647 fs->prefix.prefixlen,
2648 return_string,
d33fc23b 2649 NLRI_STRING_FORMAT_DEBUG, NULL);
268e1b9b
PG
2650 snprintf(str, size, "FS %s Match{%s}", afi2str(afi),
2651 return_string);
2652 } else
e1af3f16 2653 snprintf(str, size, "%s%s%s %s %s",
d62a17ae 2654 prefix2str(pu, pfx_buf, sizeof(pfx_buf)), tag_buf,
e1af3f16 2655 pathid_buf, afi2str(afi), safi2str(safi));
d62a17ae 2656
2657 return str;
906ad49b 2658}