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. */ |
paul | 1302bdc | 2003-11-10 23:50:54 +0000 | [diff] [blame] | 239 | bgp_dump_routes_attr (obuf, attr, p); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 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); |
paul | 1302bdc | 2003-11-10 23:50:54 +0000 | [diff] [blame] | 249 | bgp_dump_routes_attr (obuf, attr, p); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 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. */ |
paul | a384592 | 2003-10-18 01:30:50 +0000 | [diff] [blame] | 275 | bgp_dump_routes_attr (obuf, attr, p); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 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); |
paul | a384592 | 2003-10-18 01:30:50 +0000 | [diff] [blame] | 333 | #ifdef HAVE_IPV6 |
paul | 9834cd0 | 2003-10-18 01:01:19 +0000 | [diff] [blame] | 334 | bgp_dump_routes_func (AFI_IP6); |
paul | a384592 | 2003-10-18 01:30:50 +0000 | [diff] [blame] | 335 | #endif /* HAVE_IPV6 */ |
paul | 9834cd0 | 2003-10-18 01:01:19 +0000 | [diff] [blame] | 336 | /* Close the file now. For a RIB dump there's no point in leaving |
| 337 | * it open until the next scheduled dump starts. */ |
| 338 | fclose(bgp_dump->fp); bgp_dump->fp = NULL; |
| 339 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 340 | } |
| 341 | |
paul | fba3d22 | 2003-05-10 18:33:28 +0000 | [diff] [blame] | 342 | /* if interval is set reschedule */ |
| 343 | if (bgp_dump->interval > 0) |
| 344 | bgp_dump_interval_add (bgp_dump, bgp_dump->interval); |
| 345 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 346 | return 0; |
| 347 | } |
| 348 | |
| 349 | /* Dump common information. */ |
| 350 | void |
| 351 | bgp_dump_common (struct stream *obuf, struct peer *peer) |
| 352 | { |
| 353 | char empty[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; |
| 354 | |
| 355 | /* Source AS number and Destination AS number. */ |
| 356 | stream_putw (obuf, peer->as); |
| 357 | stream_putw (obuf, peer->local_as); |
| 358 | |
paul | a384592 | 2003-10-18 01:30:50 +0000 | [diff] [blame] | 359 | if (peer->su.sa.sa_family == AF_INET) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 360 | { |
| 361 | stream_putw (obuf, peer->ifindex); |
| 362 | stream_putw (obuf, AFI_IP); |
| 363 | |
| 364 | stream_put (obuf, &peer->su.sin.sin_addr, IPV4_MAX_BYTELEN); |
| 365 | |
| 366 | if (peer->su_local) |
| 367 | stream_put (obuf, &peer->su_local->sin.sin_addr, IPV4_MAX_BYTELEN); |
| 368 | else |
| 369 | stream_put (obuf, empty, IPV4_MAX_BYTELEN); |
| 370 | } |
| 371 | #ifdef HAVE_IPV6 |
paul | a384592 | 2003-10-18 01:30:50 +0000 | [diff] [blame] | 372 | else if (peer->su.sa.sa_family == AF_INET6) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 373 | { |
| 374 | /* Interface Index and Address family. */ |
| 375 | stream_putw (obuf, peer->ifindex); |
| 376 | stream_putw (obuf, AFI_IP6); |
| 377 | |
| 378 | /* Source IP Address and Destination IP Address. */ |
| 379 | stream_put (obuf, &peer->su.sin6.sin6_addr, IPV6_MAX_BYTELEN); |
| 380 | |
| 381 | if (peer->su_local) |
| 382 | stream_put (obuf, &peer->su_local->sin6.sin6_addr, IPV6_MAX_BYTELEN); |
| 383 | else |
| 384 | stream_put (obuf, empty, IPV6_MAX_BYTELEN); |
| 385 | } |
| 386 | #endif /* HAVE_IPV6 */ |
| 387 | } |
| 388 | |
| 389 | /* Dump BGP status change. */ |
| 390 | void |
| 391 | bgp_dump_state (struct peer *peer, int status_old, int status_new) |
| 392 | { |
| 393 | struct stream *obuf; |
| 394 | |
| 395 | /* If dump file pointer is disabled return immediately. */ |
| 396 | if (bgp_dump_all.fp == NULL) |
| 397 | return; |
| 398 | |
| 399 | /* Make dump stream. */ |
| 400 | obuf = bgp_dump_obuf; |
| 401 | stream_reset (obuf); |
| 402 | |
| 403 | bgp_dump_header (obuf, MSG_PROTOCOL_BGP4MP, BGP4MP_STATE_CHANGE); |
| 404 | bgp_dump_common (obuf, peer); |
| 405 | |
| 406 | stream_putw (obuf, status_old); |
| 407 | stream_putw (obuf, status_new); |
| 408 | |
| 409 | /* Set length. */ |
| 410 | bgp_dump_set_size (obuf, MSG_PROTOCOL_BGP4MP); |
| 411 | |
| 412 | /* Write to the stream. */ |
| 413 | fwrite (STREAM_DATA (obuf), stream_get_putp (obuf), 1, bgp_dump_all.fp); |
| 414 | fflush (bgp_dump_all.fp); |
| 415 | } |
| 416 | |
| 417 | void |
| 418 | bgp_dump_packet_func (struct bgp_dump *bgp_dump, struct peer *peer, |
| 419 | struct stream *packet) |
| 420 | { |
| 421 | struct stream *obuf; |
| 422 | |
| 423 | /* If dump file pointer is disabled return immediately. */ |
| 424 | if (bgp_dump->fp == NULL) |
| 425 | return; |
| 426 | |
| 427 | /* Make dump stream. */ |
| 428 | obuf = bgp_dump_obuf; |
| 429 | stream_reset (obuf); |
| 430 | |
| 431 | /* Dump header and common part. */ |
| 432 | bgp_dump_header (obuf, MSG_PROTOCOL_BGP4MP, BGP4MP_MESSAGE); |
| 433 | bgp_dump_common (obuf, peer); |
| 434 | |
| 435 | /* Packet contents. */ |
| 436 | stream_put (obuf, STREAM_DATA (packet), stream_get_endp (packet)); |
| 437 | |
| 438 | /* Set length. */ |
| 439 | bgp_dump_set_size (obuf, MSG_PROTOCOL_BGP4MP); |
| 440 | |
| 441 | /* Write to the stream. */ |
| 442 | fwrite (STREAM_DATA (obuf), stream_get_putp (obuf), 1, bgp_dump->fp); |
| 443 | fflush (bgp_dump->fp); |
| 444 | } |
| 445 | |
| 446 | /* Called from bgp_packet.c when BGP packet is received. */ |
| 447 | void |
| 448 | bgp_dump_packet (struct peer *peer, int type, struct stream *packet) |
| 449 | { |
| 450 | /* bgp_dump_all. */ |
| 451 | bgp_dump_packet_func (&bgp_dump_all, peer, packet); |
| 452 | |
| 453 | /* bgp_dump_updates. */ |
| 454 | if (type == BGP_MSG_UPDATE) |
| 455 | bgp_dump_packet_func (&bgp_dump_updates, peer, packet); |
| 456 | } |
| 457 | |
| 458 | unsigned int |
| 459 | bgp_dump_parse_time (char *str) |
| 460 | { |
| 461 | int i; |
| 462 | int len; |
| 463 | int seen_h; |
| 464 | int seen_m; |
| 465 | int time; |
| 466 | unsigned int total; |
| 467 | |
| 468 | time = 0; |
| 469 | total = 0; |
| 470 | seen_h = 0; |
| 471 | seen_m = 0; |
| 472 | len = strlen (str); |
| 473 | |
| 474 | for (i = 0; i < len; i++) |
| 475 | { |
| 476 | if (isdigit ((int) str[i])) |
| 477 | { |
| 478 | time *= 10; |
| 479 | time += str[i] - '0'; |
| 480 | } |
| 481 | else if (str[i] == 'H' || str[i] == 'h') |
| 482 | { |
| 483 | if (seen_h) |
| 484 | return 0; |
| 485 | if (seen_m) |
| 486 | return 0; |
| 487 | total += time * 60 *60; |
| 488 | time = 0; |
| 489 | seen_h = 1; |
| 490 | } |
| 491 | else if (str[i] == 'M' || str[i] == 'm') |
| 492 | { |
| 493 | if (seen_m) |
| 494 | return 0; |
| 495 | total += time * 60; |
| 496 | time = 0; |
| 497 | seen_h = 1; |
| 498 | } |
| 499 | else |
| 500 | return 0; |
| 501 | } |
| 502 | return total + time; |
| 503 | } |
| 504 | |
| 505 | int |
| 506 | bgp_dump_set (struct vty *vty, struct bgp_dump *bgp_dump, int type, |
| 507 | char *path, char *interval_str) |
| 508 | { |
paul | fba3d22 | 2003-05-10 18:33:28 +0000 | [diff] [blame] | 509 | unsigned int interval; |
| 510 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 511 | if (interval_str) |
| 512 | { |
paul | fba3d22 | 2003-05-10 18:33:28 +0000 | [diff] [blame] | 513 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 514 | /* Check interval string. */ |
| 515 | interval = bgp_dump_parse_time (interval_str); |
| 516 | if (interval == 0) |
| 517 | { |
| 518 | vty_out (vty, "Malformed interval string%s", VTY_NEWLINE); |
| 519 | return CMD_WARNING; |
| 520 | } |
| 521 | /* Set interval. */ |
| 522 | bgp_dump->interval = interval; |
| 523 | if (bgp_dump->interval_str) |
| 524 | free (bgp_dump->interval_str); |
| 525 | bgp_dump->interval_str = strdup (interval_str); |
paul | fba3d22 | 2003-05-10 18:33:28 +0000 | [diff] [blame] | 526 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 527 | } |
paul | fba3d22 | 2003-05-10 18:33:28 +0000 | [diff] [blame] | 528 | else |
| 529 | { |
| 530 | interval = 0; |
| 531 | } |
| 532 | |
| 533 | /* Create interval thread. */ |
| 534 | bgp_dump_interval_add (bgp_dump, interval); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 535 | |
| 536 | /* Set type. */ |
| 537 | bgp_dump->type = type; |
| 538 | |
| 539 | /* Set file name. */ |
| 540 | if (bgp_dump->filename) |
| 541 | free (bgp_dump->filename); |
| 542 | bgp_dump->filename = strdup (path); |
| 543 | |
| 544 | /* This should be called when interval is expired. */ |
| 545 | bgp_dump_open_file (bgp_dump); |
| 546 | |
| 547 | return CMD_SUCCESS; |
| 548 | } |
| 549 | |
| 550 | int |
| 551 | bgp_dump_unset (struct vty *vty, struct bgp_dump *bgp_dump) |
| 552 | { |
| 553 | /* Set file name. */ |
| 554 | if (bgp_dump->filename) |
| 555 | { |
| 556 | free (bgp_dump->filename); |
| 557 | bgp_dump->filename = NULL; |
| 558 | } |
| 559 | |
| 560 | /* This should be called when interval is expired. */ |
| 561 | if (bgp_dump->fp) |
| 562 | { |
| 563 | fclose (bgp_dump->fp); |
| 564 | bgp_dump->fp = NULL; |
| 565 | } |
| 566 | |
| 567 | /* Create interval thread. */ |
| 568 | if (bgp_dump->t_interval) |
| 569 | { |
| 570 | thread_cancel (bgp_dump->t_interval); |
| 571 | bgp_dump->t_interval = NULL; |
| 572 | } |
| 573 | |
| 574 | bgp_dump->interval = 0; |
| 575 | |
| 576 | if (bgp_dump->interval_str) |
| 577 | { |
| 578 | free (bgp_dump->interval_str); |
| 579 | bgp_dump->interval_str = NULL; |
| 580 | } |
| 581 | |
| 582 | |
| 583 | return CMD_SUCCESS; |
| 584 | } |
| 585 | |
| 586 | DEFUN (dump_bgp_all, |
| 587 | dump_bgp_all_cmd, |
| 588 | "dump bgp all PATH", |
| 589 | "Dump packet\n" |
| 590 | "BGP packet dump\n" |
| 591 | "Dump all BGP packets\n" |
| 592 | "Output filename\n") |
| 593 | { |
| 594 | return bgp_dump_set (vty, &bgp_dump_all, BGP_DUMP_ALL, argv[0], NULL); |
| 595 | } |
| 596 | |
| 597 | DEFUN (dump_bgp_all_interval, |
| 598 | dump_bgp_all_interval_cmd, |
| 599 | "dump bgp all PATH INTERVAL", |
| 600 | "Dump packet\n" |
| 601 | "BGP packet dump\n" |
| 602 | "Dump all BGP packets\n" |
| 603 | "Output filename\n" |
| 604 | "Interval of output\n") |
| 605 | { |
| 606 | return bgp_dump_set (vty, &bgp_dump_all, BGP_DUMP_ALL, argv[0], argv[1]); |
| 607 | } |
| 608 | |
| 609 | DEFUN (no_dump_bgp_all, |
| 610 | no_dump_bgp_all_cmd, |
| 611 | "no dump bgp all [PATH] [INTERVAL]", |
| 612 | NO_STR |
| 613 | "Dump packet\n" |
| 614 | "BGP packet dump\n" |
| 615 | "Dump all BGP packets\n") |
| 616 | { |
| 617 | return bgp_dump_unset (vty, &bgp_dump_all); |
| 618 | } |
| 619 | |
| 620 | DEFUN (dump_bgp_updates, |
| 621 | dump_bgp_updates_cmd, |
| 622 | "dump bgp updates PATH", |
| 623 | "Dump packet\n" |
| 624 | "BGP packet dump\n" |
| 625 | "Dump BGP updates only\n" |
| 626 | "Output filename\n") |
| 627 | { |
| 628 | return bgp_dump_set (vty, &bgp_dump_updates, BGP_DUMP_UPDATES, argv[0], NULL); |
| 629 | } |
| 630 | |
| 631 | DEFUN (dump_bgp_updates_interval, |
| 632 | dump_bgp_updates_interval_cmd, |
| 633 | "dump bgp updates PATH INTERVAL", |
| 634 | "Dump packet\n" |
| 635 | "BGP packet dump\n" |
| 636 | "Dump BGP updates only\n" |
| 637 | "Output filename\n" |
| 638 | "Interval of output\n") |
| 639 | { |
| 640 | return bgp_dump_set (vty, &bgp_dump_updates, BGP_DUMP_UPDATES, argv[0], argv[1]); |
| 641 | } |
| 642 | |
| 643 | DEFUN (no_dump_bgp_updates, |
| 644 | no_dump_bgp_updates_cmd, |
| 645 | "no dump bgp updates [PATH] [INTERVAL]", |
| 646 | NO_STR |
| 647 | "Dump packet\n" |
| 648 | "BGP packet dump\n" |
| 649 | "Dump BGP updates only\n") |
| 650 | { |
| 651 | return bgp_dump_unset (vty, &bgp_dump_updates); |
| 652 | } |
| 653 | |
| 654 | DEFUN (dump_bgp_routes, |
| 655 | dump_bgp_routes_cmd, |
| 656 | "dump bgp routes-mrt PATH", |
| 657 | "Dump packet\n" |
| 658 | "BGP packet dump\n" |
| 659 | "Dump whole BGP routing table\n" |
| 660 | "Output filename\n") |
| 661 | { |
| 662 | return bgp_dump_set (vty, &bgp_dump_routes, BGP_DUMP_ROUTES, argv[0], NULL); |
| 663 | } |
| 664 | |
| 665 | DEFUN (dump_bgp_routes_interval, |
| 666 | dump_bgp_routes_interval_cmd, |
| 667 | "dump bgp routes-mrt PATH INTERVAL", |
| 668 | "Dump packet\n" |
| 669 | "BGP packet dump\n" |
| 670 | "Dump whole BGP routing table\n" |
| 671 | "Output filename\n" |
| 672 | "Interval of output\n") |
| 673 | { |
| 674 | return bgp_dump_set (vty, &bgp_dump_routes, BGP_DUMP_ROUTES, argv[0], argv[1]); |
| 675 | } |
| 676 | |
| 677 | DEFUN (no_dump_bgp_routes, |
| 678 | no_dump_bgp_routes_cmd, |
| 679 | "no dump bgp routes-mrt [PATH] [INTERVAL]", |
| 680 | NO_STR |
| 681 | "Dump packet\n" |
| 682 | "BGP packet dump\n" |
| 683 | "Dump whole BGP routing table\n") |
| 684 | { |
| 685 | return bgp_dump_unset (vty, &bgp_dump_routes); |
| 686 | } |
| 687 | |
| 688 | /* BGP node structure. */ |
| 689 | struct cmd_node bgp_dump_node = |
| 690 | { |
| 691 | DUMP_NODE, |
| 692 | "", |
| 693 | }; |
| 694 | |
| 695 | #if 0 |
| 696 | char * |
| 697 | config_time2str (unsigned int interval) |
| 698 | { |
| 699 | static char buf[BUFSIZ]; |
| 700 | |
| 701 | buf[0] = '\0'; |
| 702 | |
| 703 | if (interval / 3600) |
| 704 | { |
| 705 | sprintf (buf, "%dh", interval / 3600); |
| 706 | interval %= 3600; |
| 707 | } |
| 708 | if (interval / 60) |
| 709 | { |
| 710 | sprintf (buf + strlen (buf), "%dm", interval /60); |
| 711 | interval %= 60; |
| 712 | } |
| 713 | if (interval) |
| 714 | { |
| 715 | sprintf (buf + strlen (buf), "%d", interval); |
| 716 | } |
| 717 | return buf; |
| 718 | } |
| 719 | #endif |
| 720 | |
| 721 | int |
| 722 | config_write_bgp_dump (struct vty *vty) |
| 723 | { |
| 724 | if (bgp_dump_all.filename) |
| 725 | { |
| 726 | if (bgp_dump_all.interval_str) |
| 727 | vty_out (vty, "dump bgp all %s %s%s", |
| 728 | bgp_dump_all.filename, bgp_dump_all.interval_str, |
| 729 | VTY_NEWLINE); |
| 730 | else |
| 731 | vty_out (vty, "dump bgp all %s%s", |
| 732 | bgp_dump_all.filename, VTY_NEWLINE); |
| 733 | } |
| 734 | if (bgp_dump_updates.filename) |
| 735 | { |
| 736 | if (bgp_dump_updates.interval_str) |
| 737 | vty_out (vty, "dump bgp updates %s %s%s", |
| 738 | bgp_dump_updates.filename, bgp_dump_updates.interval_str, |
| 739 | VTY_NEWLINE); |
| 740 | else |
| 741 | vty_out (vty, "dump bgp updates %s%s", |
| 742 | bgp_dump_updates.filename, VTY_NEWLINE); |
| 743 | } |
| 744 | if (bgp_dump_routes.filename) |
| 745 | { |
| 746 | if (bgp_dump_routes.interval_str) |
| 747 | vty_out (vty, "dump bgp routes-mrt %s %s%s", |
| 748 | bgp_dump_routes.filename, bgp_dump_routes.interval_str, |
| 749 | VTY_NEWLINE); |
| 750 | else |
| 751 | vty_out (vty, "dump bgp routes-mrt %s%s", |
| 752 | bgp_dump_routes.filename, VTY_NEWLINE); |
| 753 | } |
| 754 | return 0; |
| 755 | } |
| 756 | |
| 757 | /* Initialize BGP packet dump functionality. */ |
| 758 | void |
| 759 | bgp_dump_init () |
| 760 | { |
| 761 | memset (&bgp_dump_all, 0, sizeof (struct bgp_dump)); |
| 762 | memset (&bgp_dump_updates, 0, sizeof (struct bgp_dump)); |
| 763 | memset (&bgp_dump_routes, 0, sizeof (struct bgp_dump)); |
| 764 | |
paul | 9834cd0 | 2003-10-18 01:01:19 +0000 | [diff] [blame] | 765 | bgp_dump_obuf = stream_new (BGP_MAX_PACKET_SIZE + BGP_DUMP_MSG_HEADER |
| 766 | + BGP_DUMP_HEADER_SIZE); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 767 | |
| 768 | install_node (&bgp_dump_node, config_write_bgp_dump); |
| 769 | |
| 770 | install_element (CONFIG_NODE, &dump_bgp_all_cmd); |
| 771 | install_element (CONFIG_NODE, &dump_bgp_all_interval_cmd); |
| 772 | install_element (CONFIG_NODE, &no_dump_bgp_all_cmd); |
| 773 | install_element (CONFIG_NODE, &dump_bgp_updates_cmd); |
| 774 | install_element (CONFIG_NODE, &dump_bgp_updates_interval_cmd); |
| 775 | install_element (CONFIG_NODE, &no_dump_bgp_updates_cmd); |
| 776 | install_element (CONFIG_NODE, &dump_bgp_routes_cmd); |
| 777 | install_element (CONFIG_NODE, &dump_bgp_routes_interval_cmd); |
| 778 | install_element (CONFIG_NODE, &no_dump_bgp_routes_cmd); |
| 779 | } |