paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1 | /* BGP-4 dump routine |
| 2 | Copyright (C) 1999 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 |
| 17 | along with GNU Zebra; see the file COPYING. If not, write to the Free |
| 18 | Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
| 19 | 02111-1307, USA. */ |
| 20 | |
| 21 | #include <zebra.h> |
| 22 | |
| 23 | #include "log.h" |
| 24 | #include "stream.h" |
| 25 | #include "sockunion.h" |
| 26 | #include "command.h" |
| 27 | #include "prefix.h" |
| 28 | #include "thread.h" |
| 29 | #include "bgpd/bgp_table.h" |
| 30 | |
| 31 | #include "bgpd/bgpd.h" |
| 32 | #include "bgpd/bgp_route.h" |
| 33 | #include "bgpd/bgp_attr.h" |
| 34 | #include "bgpd/bgp_dump.h" |
| 35 | |
| 36 | enum bgp_dump_type |
| 37 | { |
| 38 | BGP_DUMP_ALL, |
| 39 | BGP_DUMP_UPDATES, |
| 40 | BGP_DUMP_ROUTES |
| 41 | }; |
| 42 | |
| 43 | enum MRT_MSG_TYPES { |
| 44 | MSG_NULL, |
| 45 | MSG_START, /* sender is starting up */ |
| 46 | MSG_DIE, /* receiver should shut down */ |
| 47 | MSG_I_AM_DEAD, /* sender is shutting down */ |
| 48 | MSG_PEER_DOWN, /* sender's peer is down */ |
| 49 | MSG_PROTOCOL_BGP, /* msg is a BGP packet */ |
| 50 | MSG_PROTOCOL_RIP, /* msg is a RIP packet */ |
| 51 | MSG_PROTOCOL_IDRP, /* msg is an IDRP packet */ |
| 52 | MSG_PROTOCOL_RIPNG, /* msg is a RIPNG packet */ |
| 53 | MSG_PROTOCOL_BGP4PLUS, /* msg is a BGP4+ packet */ |
| 54 | MSG_PROTOCOL_BGP4PLUS_01, /* msg is a BGP4+ (draft 01) packet */ |
| 55 | MSG_PROTOCOL_OSPF, /* msg is an OSPF packet */ |
| 56 | MSG_TABLE_DUMP /* routing table dump */ |
| 57 | }; |
| 58 | |
| 59 | struct bgp_dump |
| 60 | { |
| 61 | enum bgp_dump_type type; |
| 62 | |
| 63 | char *filename; |
| 64 | |
| 65 | FILE *fp; |
| 66 | |
| 67 | unsigned int interval; |
| 68 | |
| 69 | char *interval_str; |
| 70 | |
| 71 | struct thread *t_interval; |
| 72 | }; |
| 73 | |
| 74 | /* BGP packet dump output buffer. */ |
| 75 | struct stream *bgp_dump_obuf; |
| 76 | |
| 77 | /* BGP dump strucuture for 'dump bgp all' */ |
| 78 | struct bgp_dump bgp_dump_all; |
| 79 | |
| 80 | /* BGP dump structure for 'dump bgp updates' */ |
| 81 | struct bgp_dump bgp_dump_updates; |
| 82 | |
| 83 | /* BGP dump structure for 'dump bgp routes' */ |
| 84 | struct bgp_dump bgp_dump_routes; |
| 85 | |
| 86 | /* Dump whole BGP table is very heavy process. */ |
| 87 | struct thread *t_bgp_dump_routes; |
| 88 | |
| 89 | /* Some define for BGP packet dump. */ |
| 90 | FILE * |
| 91 | bgp_dump_open_file (struct bgp_dump *bgp_dump) |
| 92 | { |
| 93 | int ret; |
| 94 | time_t clock; |
| 95 | struct tm *tm; |
| 96 | char fullpath[MAXPATHLEN]; |
| 97 | char realpath[MAXPATHLEN]; |
| 98 | |
| 99 | time (&clock); |
| 100 | tm = localtime (&clock); |
| 101 | |
| 102 | if (bgp_dump->filename[0] != DIRECTORY_SEP) |
| 103 | { |
| 104 | sprintf (fullpath, "%s/%s", vty_get_cwd (), bgp_dump->filename); |
| 105 | ret = strftime (realpath, MAXPATHLEN, fullpath, tm); |
| 106 | } |
| 107 | else |
| 108 | ret = strftime (realpath, MAXPATHLEN, bgp_dump->filename, tm); |
| 109 | |
| 110 | if (ret == 0) |
| 111 | { |
| 112 | zlog_warn ("bgp_dump_open_file: strftime error"); |
| 113 | return NULL; |
| 114 | } |
| 115 | |
| 116 | if (bgp_dump->fp) |
| 117 | fclose (bgp_dump->fp); |
| 118 | |
| 119 | |
| 120 | bgp_dump->fp = fopen (realpath, "w"); |
| 121 | |
| 122 | if (bgp_dump->fp == NULL) |
| 123 | return NULL; |
| 124 | |
| 125 | return bgp_dump->fp; |
| 126 | } |
| 127 | |
| 128 | int |
| 129 | bgp_dump_interval_add (struct bgp_dump *bgp_dump, int interval) |
| 130 | { |
| 131 | int bgp_dump_interval_func (struct thread *); |
paul | 9834cd0 | 2003-10-18 01:01:19 +0000 | [diff] [blame^] | 132 | int interval2, secs_into_day; |
| 133 | time_t t; |
| 134 | struct tm *tm; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 135 | |
paul | fba3d22 | 2003-05-10 18:33:28 +0000 | [diff] [blame] | 136 | if (interval > 0 ) |
paul | 9834cd0 | 2003-10-18 01:01:19 +0000 | [diff] [blame^] | 137 | { |
| 138 | if ((interval < 86400) && ((86400 % interval) == 0)) |
| 139 | { |
| 140 | (void) time(&t); |
| 141 | tm = localtime(&t); |
| 142 | secs_into_day = tm->tm_sec + 60*tm->tm_min + 60*60*tm->tm_hour; |
| 143 | interval2 = interval - secs_into_day % interval; |
| 144 | if(interval2 == 0) interval2 = interval; |
| 145 | } |
| 146 | else |
| 147 | { |
| 148 | interval2 = interval; |
| 149 | } |
| 150 | bgp_dump->t_interval = thread_add_timer (master, bgp_dump_interval_func, |
| 151 | bgp_dump, interval2); |
| 152 | } |
paul | fba3d22 | 2003-05-10 18:33:28 +0000 | [diff] [blame] | 153 | else |
paul | 9834cd0 | 2003-10-18 01:01:19 +0000 | [diff] [blame^] | 154 | { |
| 155 | bgp_dump->t_interval = thread_add_event (master, bgp_dump_interval_func, |
| 156 | bgp_dump, 0); |
| 157 | } |
paul | fba3d22 | 2003-05-10 18:33:28 +0000 | [diff] [blame] | 158 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 159 | return 0; |
| 160 | } |
| 161 | |
| 162 | /* Dump common header. */ |
| 163 | void |
| 164 | bgp_dump_header (struct stream *obuf, int type, int subtype) |
| 165 | { |
| 166 | time_t now; |
| 167 | |
| 168 | /* Set header. */ |
| 169 | time (&now); |
| 170 | |
| 171 | /* Put dump packet header. */ |
| 172 | stream_putl (obuf, now); |
| 173 | stream_putw (obuf, type); |
| 174 | stream_putw (obuf, subtype); |
| 175 | |
| 176 | stream_putl (obuf, 0); /* len */ |
| 177 | } |
| 178 | |
| 179 | void |
| 180 | bgp_dump_set_size (struct stream *s, int type) |
| 181 | { |
| 182 | stream_putl_at (s, 8, stream_get_putp (s) - BGP_DUMP_HEADER_SIZE); |
| 183 | } |
| 184 | |
| 185 | void |
| 186 | bgp_dump_routes_entry (struct prefix *p, struct bgp_info *info, int afi, |
| 187 | int type, unsigned int seq) |
| 188 | { |
| 189 | struct stream *obuf; |
| 190 | struct attr *attr; |
| 191 | struct peer *peer; |
| 192 | int plen; |
| 193 | int safi = 0; |
| 194 | |
| 195 | /* Make dump stream. */ |
| 196 | obuf = bgp_dump_obuf; |
| 197 | stream_reset (obuf); |
| 198 | |
| 199 | attr = info->attr; |
| 200 | peer = info->peer; |
| 201 | |
| 202 | /* We support MRT's old format. */ |
| 203 | if (type == MSG_TABLE_DUMP) |
| 204 | { |
| 205 | bgp_dump_header (obuf, MSG_TABLE_DUMP, afi); |
| 206 | stream_putw (obuf, 0); /* View # */ |
| 207 | stream_putw (obuf, seq); /* Sequence number. */ |
| 208 | } |
| 209 | else |
| 210 | { |
| 211 | bgp_dump_header (obuf, MSG_PROTOCOL_BGP4MP, BGP4MP_ENTRY); |
| 212 | |
| 213 | stream_putl (obuf, info->uptime); /* Time Last Change */ |
| 214 | stream_putw (obuf, afi); /* Address Family */ |
| 215 | stream_putc (obuf, safi); /* SAFI */ |
| 216 | } |
| 217 | |
| 218 | if (afi == AFI_IP) |
| 219 | { |
| 220 | if (type == MSG_TABLE_DUMP) |
| 221 | { |
| 222 | /* Prefix */ |
| 223 | stream_put_in_addr (obuf, &p->u.prefix4); |
| 224 | stream_putc (obuf, p->prefixlen); |
| 225 | |
| 226 | /* Status */ |
| 227 | stream_putc (obuf, 1); |
| 228 | |
| 229 | /* Originated */ |
| 230 | stream_putl (obuf, info->uptime); |
| 231 | |
| 232 | /* Peer's IP address */ |
| 233 | stream_put_in_addr (obuf, &peer->su.sin.sin_addr); |
| 234 | |
| 235 | /* Peer's AS number. */ |
| 236 | stream_putw (obuf, peer->as); |
| 237 | |
| 238 | /* Dump attribute. */ |
| 239 | bgp_dump_routes_attr (obuf, attr); |
| 240 | } |
| 241 | else |
| 242 | { |
| 243 | /* Next-Hop-Len */ |
| 244 | stream_putc (obuf, IPV4_MAX_BYTELEN); |
| 245 | stream_put_in_addr (obuf, &attr->nexthop); |
| 246 | stream_putc (obuf, p->prefixlen); |
| 247 | plen = PSIZE (p->prefixlen); |
| 248 | stream_put (obuf, &p->u.prefix4, plen); |
| 249 | bgp_dump_routes_attr (obuf, attr); |
| 250 | } |
| 251 | } |
| 252 | #ifdef HAVE_IPV6 |
| 253 | else if (afi == AFI_IP6) |
| 254 | { |
| 255 | if (type == MSG_TABLE_DUMP) |
| 256 | { |
| 257 | /* Prefix */ |
| 258 | stream_write (obuf, (u_char *)&p->u.prefix6, IPV6_MAX_BYTELEN); |
| 259 | stream_putc (obuf, p->prefixlen); |
| 260 | |
| 261 | /* Status */ |
| 262 | stream_putc (obuf, 1); |
| 263 | |
| 264 | /* Originated */ |
| 265 | stream_putl (obuf, info->uptime); |
| 266 | |
| 267 | /* Peer's IP address */ |
| 268 | stream_write (obuf, (u_char *)&peer->su.sin6.sin6_addr, |
| 269 | IPV6_MAX_BYTELEN); |
| 270 | |
| 271 | /* Peer's AS number. */ |
| 272 | stream_putw (obuf, peer->as); |
| 273 | |
| 274 | /* Dump attribute. */ |
| 275 | bgp_dump_routes_attr (obuf, attr); |
| 276 | } |
| 277 | else |
| 278 | { |
| 279 | ; |
| 280 | } |
| 281 | } |
| 282 | #endif /* HAVE_IPV6 */ |
| 283 | |
| 284 | /* Set length. */ |
| 285 | bgp_dump_set_size (obuf, type); |
| 286 | |
| 287 | fwrite (STREAM_DATA (obuf), stream_get_putp (obuf), 1, bgp_dump_routes.fp); |
| 288 | fflush (bgp_dump_routes.fp); |
| 289 | } |
| 290 | |
| 291 | /* Runs under child process. */ |
| 292 | void |
| 293 | bgp_dump_routes_func (int afi) |
| 294 | { |
| 295 | struct stream *obuf; |
| 296 | struct bgp_node *rn; |
| 297 | struct bgp_info *info; |
| 298 | struct bgp *bgp; |
| 299 | struct bgp_table *table; |
| 300 | unsigned int seq = 0; |
| 301 | |
| 302 | obuf = bgp_dump_obuf; |
| 303 | |
| 304 | bgp = bgp_get_default (); |
| 305 | if (!bgp) |
| 306 | return; |
| 307 | |
| 308 | if (bgp_dump_routes.fp == NULL) |
| 309 | return; |
| 310 | |
| 311 | /* Walk down each BGP route. */ |
| 312 | table = bgp->rib[afi][SAFI_UNICAST]; |
| 313 | |
| 314 | for (rn = bgp_table_top (table); rn; rn = bgp_route_next (rn)) |
| 315 | for (info = rn->info; info; info = info->next) |
| 316 | bgp_dump_routes_entry (&rn->p, info, afi, MSG_TABLE_DUMP, seq++); |
| 317 | } |
| 318 | |
| 319 | int |
| 320 | bgp_dump_interval_func (struct thread *t) |
| 321 | { |
| 322 | struct bgp_dump *bgp_dump; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 323 | bgp_dump = THREAD_ARG (t); |
| 324 | bgp_dump->t_interval = NULL; |
| 325 | |
paul | 9834cd0 | 2003-10-18 01:01:19 +0000 | [diff] [blame^] | 326 | /* Reschedule dump even if file couldn't be opened this time... */ |
| 327 | if (bgp_dump_open_file (bgp_dump) != NULL) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 328 | { |
paul | 9834cd0 | 2003-10-18 01:01:19 +0000 | [diff] [blame^] | 329 | /* In case of bgp_dump_routes, we need special route dump function. */ |
| 330 | if (bgp_dump->type == BGP_DUMP_ROUTES) |
| 331 | { |
| 332 | bgp_dump_routes_func (AFI_IP); |
| 333 | bgp_dump_routes_func (AFI_IP6); |
| 334 | /* Close the file now. For a RIB dump there's no point in leaving |
| 335 | * it open until the next scheduled dump starts. */ |
| 336 | fclose(bgp_dump->fp); bgp_dump->fp = NULL; |
| 337 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 338 | } |
| 339 | |
paul | fba3d22 | 2003-05-10 18:33:28 +0000 | [diff] [blame] | 340 | /* if interval is set reschedule */ |
| 341 | if (bgp_dump->interval > 0) |
| 342 | bgp_dump_interval_add (bgp_dump, bgp_dump->interval); |
| 343 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 344 | return 0; |
| 345 | } |
| 346 | |
| 347 | /* Dump common information. */ |
| 348 | void |
| 349 | bgp_dump_common (struct stream *obuf, struct peer *peer) |
| 350 | { |
| 351 | char empty[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; |
| 352 | |
| 353 | /* Source AS number and Destination AS number. */ |
| 354 | stream_putw (obuf, peer->as); |
| 355 | stream_putw (obuf, peer->local_as); |
| 356 | |
| 357 | if (peer->afc[AFI_IP][SAFI_UNICAST]) |
| 358 | { |
| 359 | stream_putw (obuf, peer->ifindex); |
| 360 | stream_putw (obuf, AFI_IP); |
| 361 | |
| 362 | stream_put (obuf, &peer->su.sin.sin_addr, IPV4_MAX_BYTELEN); |
| 363 | |
| 364 | if (peer->su_local) |
| 365 | stream_put (obuf, &peer->su_local->sin.sin_addr, IPV4_MAX_BYTELEN); |
| 366 | else |
| 367 | stream_put (obuf, empty, IPV4_MAX_BYTELEN); |
| 368 | } |
| 369 | #ifdef HAVE_IPV6 |
| 370 | else if (peer->afc[AFI_IP6][SAFI_UNICAST]) |
| 371 | { |
| 372 | /* Interface Index and Address family. */ |
| 373 | stream_putw (obuf, peer->ifindex); |
| 374 | stream_putw (obuf, AFI_IP6); |
| 375 | |
| 376 | /* Source IP Address and Destination IP Address. */ |
| 377 | stream_put (obuf, &peer->su.sin6.sin6_addr, IPV6_MAX_BYTELEN); |
| 378 | |
| 379 | if (peer->su_local) |
| 380 | stream_put (obuf, &peer->su_local->sin6.sin6_addr, IPV6_MAX_BYTELEN); |
| 381 | else |
| 382 | stream_put (obuf, empty, IPV6_MAX_BYTELEN); |
| 383 | } |
| 384 | #endif /* HAVE_IPV6 */ |
| 385 | } |
| 386 | |
| 387 | /* Dump BGP status change. */ |
| 388 | void |
| 389 | bgp_dump_state (struct peer *peer, int status_old, int status_new) |
| 390 | { |
| 391 | struct stream *obuf; |
| 392 | |
| 393 | /* If dump file pointer is disabled return immediately. */ |
| 394 | if (bgp_dump_all.fp == NULL) |
| 395 | return; |
| 396 | |
| 397 | /* Make dump stream. */ |
| 398 | obuf = bgp_dump_obuf; |
| 399 | stream_reset (obuf); |
| 400 | |
| 401 | bgp_dump_header (obuf, MSG_PROTOCOL_BGP4MP, BGP4MP_STATE_CHANGE); |
| 402 | bgp_dump_common (obuf, peer); |
| 403 | |
| 404 | stream_putw (obuf, status_old); |
| 405 | stream_putw (obuf, status_new); |
| 406 | |
| 407 | /* Set length. */ |
| 408 | bgp_dump_set_size (obuf, MSG_PROTOCOL_BGP4MP); |
| 409 | |
| 410 | /* Write to the stream. */ |
| 411 | fwrite (STREAM_DATA (obuf), stream_get_putp (obuf), 1, bgp_dump_all.fp); |
| 412 | fflush (bgp_dump_all.fp); |
| 413 | } |
| 414 | |
| 415 | void |
| 416 | bgp_dump_packet_func (struct bgp_dump *bgp_dump, struct peer *peer, |
| 417 | struct stream *packet) |
| 418 | { |
| 419 | struct stream *obuf; |
| 420 | |
| 421 | /* If dump file pointer is disabled return immediately. */ |
| 422 | if (bgp_dump->fp == NULL) |
| 423 | return; |
| 424 | |
| 425 | /* Make dump stream. */ |
| 426 | obuf = bgp_dump_obuf; |
| 427 | stream_reset (obuf); |
| 428 | |
| 429 | /* Dump header and common part. */ |
| 430 | bgp_dump_header (obuf, MSG_PROTOCOL_BGP4MP, BGP4MP_MESSAGE); |
| 431 | bgp_dump_common (obuf, peer); |
| 432 | |
| 433 | /* Packet contents. */ |
| 434 | stream_put (obuf, STREAM_DATA (packet), stream_get_endp (packet)); |
| 435 | |
| 436 | /* Set length. */ |
| 437 | bgp_dump_set_size (obuf, MSG_PROTOCOL_BGP4MP); |
| 438 | |
| 439 | /* Write to the stream. */ |
| 440 | fwrite (STREAM_DATA (obuf), stream_get_putp (obuf), 1, bgp_dump->fp); |
| 441 | fflush (bgp_dump->fp); |
| 442 | } |
| 443 | |
| 444 | /* Called from bgp_packet.c when BGP packet is received. */ |
| 445 | void |
| 446 | bgp_dump_packet (struct peer *peer, int type, struct stream *packet) |
| 447 | { |
| 448 | /* bgp_dump_all. */ |
| 449 | bgp_dump_packet_func (&bgp_dump_all, peer, packet); |
| 450 | |
| 451 | /* bgp_dump_updates. */ |
| 452 | if (type == BGP_MSG_UPDATE) |
| 453 | bgp_dump_packet_func (&bgp_dump_updates, peer, packet); |
| 454 | } |
| 455 | |
| 456 | unsigned int |
| 457 | bgp_dump_parse_time (char *str) |
| 458 | { |
| 459 | int i; |
| 460 | int len; |
| 461 | int seen_h; |
| 462 | int seen_m; |
| 463 | int time; |
| 464 | unsigned int total; |
| 465 | |
| 466 | time = 0; |
| 467 | total = 0; |
| 468 | seen_h = 0; |
| 469 | seen_m = 0; |
| 470 | len = strlen (str); |
| 471 | |
| 472 | for (i = 0; i < len; i++) |
| 473 | { |
| 474 | if (isdigit ((int) str[i])) |
| 475 | { |
| 476 | time *= 10; |
| 477 | time += str[i] - '0'; |
| 478 | } |
| 479 | else if (str[i] == 'H' || str[i] == 'h') |
| 480 | { |
| 481 | if (seen_h) |
| 482 | return 0; |
| 483 | if (seen_m) |
| 484 | return 0; |
| 485 | total += time * 60 *60; |
| 486 | time = 0; |
| 487 | seen_h = 1; |
| 488 | } |
| 489 | else if (str[i] == 'M' || str[i] == 'm') |
| 490 | { |
| 491 | if (seen_m) |
| 492 | return 0; |
| 493 | total += time * 60; |
| 494 | time = 0; |
| 495 | seen_h = 1; |
| 496 | } |
| 497 | else |
| 498 | return 0; |
| 499 | } |
| 500 | return total + time; |
| 501 | } |
| 502 | |
| 503 | int |
| 504 | bgp_dump_set (struct vty *vty, struct bgp_dump *bgp_dump, int type, |
| 505 | char *path, char *interval_str) |
| 506 | { |
paul | fba3d22 | 2003-05-10 18:33:28 +0000 | [diff] [blame] | 507 | unsigned int interval; |
| 508 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 509 | if (interval_str) |
| 510 | { |
paul | fba3d22 | 2003-05-10 18:33:28 +0000 | [diff] [blame] | 511 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 512 | /* Check interval string. */ |
| 513 | interval = bgp_dump_parse_time (interval_str); |
| 514 | if (interval == 0) |
| 515 | { |
| 516 | vty_out (vty, "Malformed interval string%s", VTY_NEWLINE); |
| 517 | return CMD_WARNING; |
| 518 | } |
| 519 | /* Set interval. */ |
| 520 | bgp_dump->interval = interval; |
| 521 | if (bgp_dump->interval_str) |
| 522 | free (bgp_dump->interval_str); |
| 523 | bgp_dump->interval_str = strdup (interval_str); |
paul | fba3d22 | 2003-05-10 18:33:28 +0000 | [diff] [blame] | 524 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 525 | } |
paul | fba3d22 | 2003-05-10 18:33:28 +0000 | [diff] [blame] | 526 | else |
| 527 | { |
| 528 | interval = 0; |
| 529 | } |
| 530 | |
| 531 | /* Create interval thread. */ |
| 532 | bgp_dump_interval_add (bgp_dump, interval); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 533 | |
| 534 | /* Set type. */ |
| 535 | bgp_dump->type = type; |
| 536 | |
| 537 | /* Set file name. */ |
| 538 | if (bgp_dump->filename) |
| 539 | free (bgp_dump->filename); |
| 540 | bgp_dump->filename = strdup (path); |
| 541 | |
| 542 | /* This should be called when interval is expired. */ |
| 543 | bgp_dump_open_file (bgp_dump); |
| 544 | |
| 545 | return CMD_SUCCESS; |
| 546 | } |
| 547 | |
| 548 | int |
| 549 | bgp_dump_unset (struct vty *vty, struct bgp_dump *bgp_dump) |
| 550 | { |
| 551 | /* Set file name. */ |
| 552 | if (bgp_dump->filename) |
| 553 | { |
| 554 | free (bgp_dump->filename); |
| 555 | bgp_dump->filename = NULL; |
| 556 | } |
| 557 | |
| 558 | /* This should be called when interval is expired. */ |
| 559 | if (bgp_dump->fp) |
| 560 | { |
| 561 | fclose (bgp_dump->fp); |
| 562 | bgp_dump->fp = NULL; |
| 563 | } |
| 564 | |
| 565 | /* Create interval thread. */ |
| 566 | if (bgp_dump->t_interval) |
| 567 | { |
| 568 | thread_cancel (bgp_dump->t_interval); |
| 569 | bgp_dump->t_interval = NULL; |
| 570 | } |
| 571 | |
| 572 | bgp_dump->interval = 0; |
| 573 | |
| 574 | if (bgp_dump->interval_str) |
| 575 | { |
| 576 | free (bgp_dump->interval_str); |
| 577 | bgp_dump->interval_str = NULL; |
| 578 | } |
| 579 | |
| 580 | |
| 581 | return CMD_SUCCESS; |
| 582 | } |
| 583 | |
| 584 | DEFUN (dump_bgp_all, |
| 585 | dump_bgp_all_cmd, |
| 586 | "dump bgp all PATH", |
| 587 | "Dump packet\n" |
| 588 | "BGP packet dump\n" |
| 589 | "Dump all BGP packets\n" |
| 590 | "Output filename\n") |
| 591 | { |
| 592 | return bgp_dump_set (vty, &bgp_dump_all, BGP_DUMP_ALL, argv[0], NULL); |
| 593 | } |
| 594 | |
| 595 | DEFUN (dump_bgp_all_interval, |
| 596 | dump_bgp_all_interval_cmd, |
| 597 | "dump bgp all PATH INTERVAL", |
| 598 | "Dump packet\n" |
| 599 | "BGP packet dump\n" |
| 600 | "Dump all BGP packets\n" |
| 601 | "Output filename\n" |
| 602 | "Interval of output\n") |
| 603 | { |
| 604 | return bgp_dump_set (vty, &bgp_dump_all, BGP_DUMP_ALL, argv[0], argv[1]); |
| 605 | } |
| 606 | |
| 607 | DEFUN (no_dump_bgp_all, |
| 608 | no_dump_bgp_all_cmd, |
| 609 | "no dump bgp all [PATH] [INTERVAL]", |
| 610 | NO_STR |
| 611 | "Dump packet\n" |
| 612 | "BGP packet dump\n" |
| 613 | "Dump all BGP packets\n") |
| 614 | { |
| 615 | return bgp_dump_unset (vty, &bgp_dump_all); |
| 616 | } |
| 617 | |
| 618 | DEFUN (dump_bgp_updates, |
| 619 | dump_bgp_updates_cmd, |
| 620 | "dump bgp updates PATH", |
| 621 | "Dump packet\n" |
| 622 | "BGP packet dump\n" |
| 623 | "Dump BGP updates only\n" |
| 624 | "Output filename\n") |
| 625 | { |
| 626 | return bgp_dump_set (vty, &bgp_dump_updates, BGP_DUMP_UPDATES, argv[0], NULL); |
| 627 | } |
| 628 | |
| 629 | DEFUN (dump_bgp_updates_interval, |
| 630 | dump_bgp_updates_interval_cmd, |
| 631 | "dump bgp updates PATH INTERVAL", |
| 632 | "Dump packet\n" |
| 633 | "BGP packet dump\n" |
| 634 | "Dump BGP updates only\n" |
| 635 | "Output filename\n" |
| 636 | "Interval of output\n") |
| 637 | { |
| 638 | return bgp_dump_set (vty, &bgp_dump_updates, BGP_DUMP_UPDATES, argv[0], argv[1]); |
| 639 | } |
| 640 | |
| 641 | DEFUN (no_dump_bgp_updates, |
| 642 | no_dump_bgp_updates_cmd, |
| 643 | "no dump bgp updates [PATH] [INTERVAL]", |
| 644 | NO_STR |
| 645 | "Dump packet\n" |
| 646 | "BGP packet dump\n" |
| 647 | "Dump BGP updates only\n") |
| 648 | { |
| 649 | return bgp_dump_unset (vty, &bgp_dump_updates); |
| 650 | } |
| 651 | |
| 652 | DEFUN (dump_bgp_routes, |
| 653 | dump_bgp_routes_cmd, |
| 654 | "dump bgp routes-mrt PATH", |
| 655 | "Dump packet\n" |
| 656 | "BGP packet dump\n" |
| 657 | "Dump whole BGP routing table\n" |
| 658 | "Output filename\n") |
| 659 | { |
| 660 | return bgp_dump_set (vty, &bgp_dump_routes, BGP_DUMP_ROUTES, argv[0], NULL); |
| 661 | } |
| 662 | |
| 663 | DEFUN (dump_bgp_routes_interval, |
| 664 | dump_bgp_routes_interval_cmd, |
| 665 | "dump bgp routes-mrt PATH INTERVAL", |
| 666 | "Dump packet\n" |
| 667 | "BGP packet dump\n" |
| 668 | "Dump whole BGP routing table\n" |
| 669 | "Output filename\n" |
| 670 | "Interval of output\n") |
| 671 | { |
| 672 | return bgp_dump_set (vty, &bgp_dump_routes, BGP_DUMP_ROUTES, argv[0], argv[1]); |
| 673 | } |
| 674 | |
| 675 | DEFUN (no_dump_bgp_routes, |
| 676 | no_dump_bgp_routes_cmd, |
| 677 | "no dump bgp routes-mrt [PATH] [INTERVAL]", |
| 678 | NO_STR |
| 679 | "Dump packet\n" |
| 680 | "BGP packet dump\n" |
| 681 | "Dump whole BGP routing table\n") |
| 682 | { |
| 683 | return bgp_dump_unset (vty, &bgp_dump_routes); |
| 684 | } |
| 685 | |
| 686 | /* BGP node structure. */ |
| 687 | struct cmd_node bgp_dump_node = |
| 688 | { |
| 689 | DUMP_NODE, |
| 690 | "", |
| 691 | }; |
| 692 | |
| 693 | #if 0 |
| 694 | char * |
| 695 | config_time2str (unsigned int interval) |
| 696 | { |
| 697 | static char buf[BUFSIZ]; |
| 698 | |
| 699 | buf[0] = '\0'; |
| 700 | |
| 701 | if (interval / 3600) |
| 702 | { |
| 703 | sprintf (buf, "%dh", interval / 3600); |
| 704 | interval %= 3600; |
| 705 | } |
| 706 | if (interval / 60) |
| 707 | { |
| 708 | sprintf (buf + strlen (buf), "%dm", interval /60); |
| 709 | interval %= 60; |
| 710 | } |
| 711 | if (interval) |
| 712 | { |
| 713 | sprintf (buf + strlen (buf), "%d", interval); |
| 714 | } |
| 715 | return buf; |
| 716 | } |
| 717 | #endif |
| 718 | |
| 719 | int |
| 720 | config_write_bgp_dump (struct vty *vty) |
| 721 | { |
| 722 | if (bgp_dump_all.filename) |
| 723 | { |
| 724 | if (bgp_dump_all.interval_str) |
| 725 | vty_out (vty, "dump bgp all %s %s%s", |
| 726 | bgp_dump_all.filename, bgp_dump_all.interval_str, |
| 727 | VTY_NEWLINE); |
| 728 | else |
| 729 | vty_out (vty, "dump bgp all %s%s", |
| 730 | bgp_dump_all.filename, VTY_NEWLINE); |
| 731 | } |
| 732 | if (bgp_dump_updates.filename) |
| 733 | { |
| 734 | if (bgp_dump_updates.interval_str) |
| 735 | vty_out (vty, "dump bgp updates %s %s%s", |
| 736 | bgp_dump_updates.filename, bgp_dump_updates.interval_str, |
| 737 | VTY_NEWLINE); |
| 738 | else |
| 739 | vty_out (vty, "dump bgp updates %s%s", |
| 740 | bgp_dump_updates.filename, VTY_NEWLINE); |
| 741 | } |
| 742 | if (bgp_dump_routes.filename) |
| 743 | { |
| 744 | if (bgp_dump_routes.interval_str) |
| 745 | vty_out (vty, "dump bgp routes-mrt %s %s%s", |
| 746 | bgp_dump_routes.filename, bgp_dump_routes.interval_str, |
| 747 | VTY_NEWLINE); |
| 748 | else |
| 749 | vty_out (vty, "dump bgp routes-mrt %s%s", |
| 750 | bgp_dump_routes.filename, VTY_NEWLINE); |
| 751 | } |
| 752 | return 0; |
| 753 | } |
| 754 | |
| 755 | /* Initialize BGP packet dump functionality. */ |
| 756 | void |
| 757 | bgp_dump_init () |
| 758 | { |
| 759 | memset (&bgp_dump_all, 0, sizeof (struct bgp_dump)); |
| 760 | memset (&bgp_dump_updates, 0, sizeof (struct bgp_dump)); |
| 761 | memset (&bgp_dump_routes, 0, sizeof (struct bgp_dump)); |
| 762 | |
paul | 9834cd0 | 2003-10-18 01:01:19 +0000 | [diff] [blame^] | 763 | bgp_dump_obuf = stream_new (BGP_MAX_PACKET_SIZE + BGP_DUMP_MSG_HEADER |
| 764 | + BGP_DUMP_HEADER_SIZE); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 765 | |
| 766 | install_node (&bgp_dump_node, config_write_bgp_dump); |
| 767 | |
| 768 | install_element (CONFIG_NODE, &dump_bgp_all_cmd); |
| 769 | install_element (CONFIG_NODE, &dump_bgp_all_interval_cmd); |
| 770 | install_element (CONFIG_NODE, &no_dump_bgp_all_cmd); |
| 771 | install_element (CONFIG_NODE, &dump_bgp_updates_cmd); |
| 772 | install_element (CONFIG_NODE, &dump_bgp_updates_interval_cmd); |
| 773 | install_element (CONFIG_NODE, &no_dump_bgp_updates_cmd); |
| 774 | install_element (CONFIG_NODE, &dump_bgp_routes_cmd); |
| 775 | install_element (CONFIG_NODE, &dump_bgp_routes_interval_cmd); |
| 776 | install_element (CONFIG_NODE, &no_dump_bgp_routes_cmd); |
| 777 | } |