ipfix.c (15052B)
1 /* 2 * 3 * This license is set out in https://raw.githubusercontent.com/Broadcom-Network-Switching-Software/OpenBCM/master/Legal/LICENSE file. 4 * 5 * Copyright 2007-2019 Broadcom Inc. All rights reserved. 6 * 7 * File: bcmx/ipfix.c 8 * Purpose: BCMX IP Flow Information Export (IPFIX) APIs 9 */ 10 11 #include <sal/core/libc.h> 12 13 #include <bcm/types.h> 14 15 #include <bcmx/lport.h> 16 #include <bcmx/ipfix.h> 17 18 #include "bcmx_int.h" 19 20 #define BCMX_IPFIX_INIT_CHECK BCMX_READY_CHECK 21 22 #define BCMX_IPFIX_SET_ERROR_CHECK(_unit, _check, _rv) \ 23 BCMX_SET_ERROR_CHECK(_unit, _check, _rv) 24 25 #define BCMX_IPFIX_DELETE_ERROR_CHECK(_unit, _check, _rv) \ 26 BCMX_DELETE_ERROR_CHECK(_unit, _check, _rv) 27 28 #define BCMX_IPFIX_GET_IS_VALID(_unit, _rv) \ 29 BCMX_ERROR_IS_VALID(_unit, _rv) 30 31 32 #define BCMX_IPFIX_CONFIG_T_PTR_TO_BCM(_config) \ 33 ((bcm_ipfix_config_t *)(_config)) 34 35 #define BCMX_IPFIX_RATE_T_PTR_TO_BCM(_rate_info) \ 36 ((bcm_ipfix_rate_t *)(_rate_info)) 37 38 #define BCMX_IPFIX_MIRROR_CONFIG_T_PTR_TO_BCM(_config) \ 39 ((bcm_ipfix_mirror_config_t *)(_config)) 40 41 42 /* 43 * Function: 44 * bcmx_ipfix_config_t_init 45 */ 46 void 47 bcmx_ipfix_config_t_init(bcmx_ipfix_config_t *config) 48 { 49 if (config != NULL) { 50 bcm_ipfix_config_t_init(BCMX_IPFIX_CONFIG_T_PTR_TO_BCM(config)); 51 } 52 } 53 54 /* 55 * Function: 56 * bcmx_ipfix_rate_t_init 57 */ 58 void 59 bcmx_ipfix_rate_t_init(bcmx_ipfix_rate_t *rate_info) 60 { 61 if (rate_info != NULL) { 62 bcm_ipfix_rate_t_init(BCMX_IPFIX_RATE_T_PTR_TO_BCM(rate_info)); 63 } 64 } 65 66 /* 67 * Function: 68 * bcmx_ipfix_mirror_config_t_init 69 */ 70 void 71 bcmx_ipfix_mirror_config_t_init(bcmx_ipfix_mirror_config_t *config) 72 { 73 if (config != NULL) { 74 bcm_ipfix_mirror_config_t_init 75 (BCMX_IPFIX_MIRROR_CONFIG_T_PTR_TO_BCM(config)); 76 } 77 } 78 79 80 /* 81 * Function: 82 * bcmx_ipfix_config_set 83 */ 84 int bcmx_ipfix_config_set(bcm_ipfix_stage_t stage, bcmx_lport_t port, 85 bcmx_ipfix_config_t *config) 86 { 87 int bcm_unit; 88 bcm_port_t bcm_port; 89 90 BCMX_IPFIX_INIT_CHECK; 91 92 BCMX_PARAM_NULL_CHECK(config); 93 94 BCM_IF_ERROR_RETURN 95 (_bcmx_dest_to_unit_port(port, &bcm_unit, &bcm_port, 96 BCMX_DEST_CONVERT_DEFAULT)); 97 98 return bcm_ipfix_config_set(bcm_unit, stage, bcm_port, 99 BCMX_IPFIX_CONFIG_T_PTR_TO_BCM(config)); 100 } 101 102 103 /* 104 * Function: 105 * bcmx_ipfix_config_get 106 */ 107 int 108 bcmx_ipfix_config_get(bcm_ipfix_stage_t stage, bcmx_lport_t port, 109 bcmx_ipfix_config_t *config) 110 { 111 int bcm_unit; 112 bcm_port_t bcm_port; 113 114 BCMX_IPFIX_INIT_CHECK; 115 116 BCMX_PARAM_NULL_CHECK(config); 117 118 BCM_IF_ERROR_RETURN 119 (_bcmx_dest_to_unit_port(port, &bcm_unit, &bcm_port, 120 BCMX_DEST_CONVERT_DEFAULT)); 121 122 return bcm_ipfix_config_get(bcm_unit, stage, bcm_port, 123 BCMX_IPFIX_CONFIG_T_PTR_TO_BCM(config)); 124 } 125 126 127 /* 128 * Function: 129 * bcmx_ipfix_rate_create 130 * Purpose: 131 * Create an IPFIX flow rate meter entry. 132 * Parameters: 133 * rate_info - (IN/OUT) Pointer to rate information 134 * Returns: 135 * BCM_E_XXX 136 * Notes: 137 */ 138 int 139 bcmx_ipfix_rate_create(bcmx_ipfix_rate_t *rate_info) 140 { 141 int rv = BCM_E_UNAVAIL, tmp_rv; 142 int i, bcm_unit; 143 uint32 flags_orig; 144 145 BCMX_IPFIX_INIT_CHECK; 146 147 BCMX_PARAM_NULL_CHECK(rate_info); 148 149 /* Store original 'xxx_WITH_ID' flag bit */ 150 flags_orig = rate_info->flags & BCM_IPFIX_RATE_VIOLATION_WITH_ID; 151 152 BCMX_UNIT_ITER(bcm_unit, i) { 153 tmp_rv = bcm_ipfix_rate_create(bcm_unit, 154 BCMX_IPFIX_RATE_T_PTR_TO_BCM 155 (rate_info)); 156 if (BCM_FAILURE(BCMX_IPFIX_SET_ERROR_CHECK(bcm_unit, tmp_rv, &rv))) { 157 break; 158 } 159 160 /* 161 * Use the ID from first successful 'create' if rate ID 162 * is not specified. 163 */ 164 if (!(rate_info->flags & BCM_IPFIX_RATE_VIOLATION_WITH_ID)) { 165 if (BCM_SUCCESS(tmp_rv)) { 166 rate_info->flags |= BCM_IPFIX_RATE_VIOLATION_WITH_ID; 167 } 168 } 169 } 170 171 /* Restore 'xxx_WITH_ID' flag bit */ 172 rate_info->flags &= ~BCM_IPFIX_RATE_VIOLATION_WITH_ID; 173 rate_info->flags |= flags_orig; 174 175 return rv; 176 } 177 178 /* 179 * Function: 180 * bcmx_ipfix_rate_destroy 181 * Purpose: 182 * Destroy an IPFIX flow rate meter entry. 183 * Parameters: 184 * rate_id - Rate identifier 185 * Returns: 186 * BCM_E_XXX 187 * Notes: 188 */ 189 int 190 bcmx_ipfix_rate_destroy(bcm_ipfix_rate_id_t rate_id) 191 { 192 int rv = BCM_E_UNAVAIL, tmp_rv; 193 int i, bcm_unit; 194 195 BCMX_IPFIX_INIT_CHECK; 196 197 BCMX_UNIT_ITER(bcm_unit, i) { 198 tmp_rv = bcm_ipfix_rate_destroy(bcm_unit, rate_id); 199 BCM_IF_ERROR_RETURN 200 (BCMX_IPFIX_DELETE_ERROR_CHECK(bcm_unit, tmp_rv, &rv)); 201 } 202 203 return rv; 204 } 205 206 /* 207 * Function: 208 * bcmx_ipfix_rate_get 209 * Purpose: 210 * Get IPFIX flow rate meter entry for the specified id. 211 * Parameters: 212 * rate_info - (IN/OUT) Rate information 213 * Returns: 214 * BCM_E_XXX 215 * Notes: 216 */ 217 int 218 bcmx_ipfix_rate_get(bcmx_ipfix_rate_t *rate_info) 219 { 220 int rv; 221 int i, bcm_unit; 222 223 BCMX_IPFIX_INIT_CHECK; 224 225 BCMX_PARAM_NULL_CHECK(rate_info); 226 227 BCMX_UNIT_ITER(bcm_unit, i) { 228 rv = bcm_ipfix_rate_get(bcm_unit, 229 BCMX_IPFIX_RATE_T_PTR_TO_BCM(rate_info)); 230 if (BCMX_IPFIX_GET_IS_VALID(bcm_unit, rv)) { 231 return rv; 232 } 233 } 234 235 return BCM_E_UNAVAIL; 236 } 237 238 /* 239 * Function: 240 * bcmx_ipfix_rate_destroy_all 241 * Purpose: 242 * Destroy all IPFIX flow rate meter entries. 243 * Parameters: 244 * None 245 * Returns: 246 * BCM_E_XXX 247 * Notes: 248 */ 249 int 250 bcmx_ipfix_rate_destroy_all(void) 251 { 252 int rv = BCM_E_UNAVAIL, tmp_rv; 253 int i, bcm_unit; 254 255 BCMX_IPFIX_INIT_CHECK; 256 257 BCMX_UNIT_ITER(bcm_unit, i) { 258 tmp_rv = bcm_ipfix_rate_destroy_all(bcm_unit); 259 BCM_IF_ERROR_RETURN 260 (BCMX_IPFIX_DELETE_ERROR_CHECK(bcm_unit, tmp_rv, &rv)); 261 } 262 263 return rv; 264 } 265 266 /* 267 * Function: 268 * bcmx_ipfix_rate_mirror_add 269 * Purpose: 270 * Add a mirror destination to the IPFIX flow rate meter entry. 271 * Parameters: 272 * rate_id - Rate identifier 273 * mirror_dest_id - Pre-allocated mirror destination identifier 274 * Returns: 275 * BCM_E_XXX 276 * Notes: 277 */ 278 int 279 bcmx_ipfix_rate_mirror_add(bcm_ipfix_rate_id_t rate_id, 280 bcm_gport_t mirror_dest_id) 281 { 282 int rv = BCM_E_UNAVAIL, tmp_rv; 283 int i, bcm_unit; 284 285 BCMX_IPFIX_INIT_CHECK; 286 287 BCMX_UNIT_ITER(bcm_unit, i) { 288 tmp_rv = bcm_ipfix_rate_mirror_add(bcm_unit, rate_id, mirror_dest_id); 289 BCM_IF_ERROR_RETURN(BCMX_IPFIX_SET_ERROR_CHECK(bcm_unit, tmp_rv, &rv)); 290 } 291 292 return rv; 293 } 294 295 /* 296 * Function: 297 * bcmx_ipfix_rate_mirror_delete 298 * Purpose: 299 * Delete a mirror destination from the IPFIX flow rate meter entry. 300 * Parameters: 301 * rate_id - Rate identifier 302 * mirror_dest_id - Pre-allocated mirror destination identifier 303 * Returns: 304 * BCM_E_XXX 305 * Notes: 306 */ 307 int 308 bcmx_ipfix_rate_mirror_delete(bcm_ipfix_rate_id_t rate_id, 309 bcm_gport_t mirror_dest_id) 310 { 311 int rv = BCM_E_UNAVAIL, tmp_rv; 312 int i, bcm_unit; 313 314 BCMX_IPFIX_INIT_CHECK; 315 316 BCMX_UNIT_ITER(bcm_unit, i) { 317 tmp_rv = bcm_ipfix_rate_mirror_delete(bcm_unit, rate_id, 318 mirror_dest_id); 319 BCM_IF_ERROR_RETURN 320 (BCMX_IPFIX_DELETE_ERROR_CHECK(bcm_unit, tmp_rv, &rv)); 321 } 322 323 return rv; 324 } 325 326 /* 327 * Function: 328 * bcmx_ipfix_rate_mirror_delete_all 329 * Purpose: 330 * Delete all mirror destination associated with the IPFIX flow 331 * rate meter entry. 332 * Parameters: 333 * rate_id - Rate identifier 334 * Returns: 335 * BCM_E_XXX 336 * Notes: 337 */ 338 int 339 bcmx_ipfix_rate_mirror_delete_all(bcm_ipfix_rate_id_t rate_id) 340 { 341 int rv = BCM_E_UNAVAIL, tmp_rv; 342 int i, bcm_unit; 343 344 BCMX_IPFIX_INIT_CHECK; 345 346 BCMX_UNIT_ITER(bcm_unit, i) { 347 tmp_rv = bcm_ipfix_rate_mirror_delete_all(bcm_unit, rate_id); 348 BCM_IF_ERROR_RETURN 349 (BCMX_IPFIX_DELETE_ERROR_CHECK(bcm_unit, tmp_rv, &rv)); 350 } 351 352 return rv; 353 } 354 355 /* 356 * Function: 357 * bcmx_ipfix_rate_mirror_get 358 * Purpose: 359 * Get all mirror destination associated with the IPFIX flow 360 * rate meter entry. 361 * Parameters: 362 * rate_id - Rate identifier 363 * mirror_dest_size - Size of mirror_dest_id array 364 * mirror_dest_id - Array to store mirror destination identifier 365 * mirror_dest_count - Number of entries stored in the mirror_dest_id array 366 * Returns: 367 * BCM_E_XXX 368 * Notes: 369 */ 370 int 371 bcmx_ipfix_rate_mirror_get(bcm_ipfix_rate_id_t rate_id, 372 int mirror_dest_size, 373 bcm_gport_t *mirror_dest_id, 374 int *mirror_dest_count) 375 { 376 int rv; 377 int i, bcm_unit; 378 379 BCMX_IPFIX_INIT_CHECK; 380 381 BCMX_PARAM_NULL_CHECK(mirror_dest_id); 382 BCMX_PARAM_NULL_CHECK(mirror_dest_count); 383 384 BCMX_UNIT_ITER(bcm_unit, i) { 385 rv = bcm_ipfix_rate_mirror_get(bcm_unit, rate_id, mirror_dest_size, 386 mirror_dest_id, mirror_dest_count); 387 if (BCMX_IPFIX_GET_IS_VALID(bcm_unit, rv)) { 388 return rv; 389 } 390 } 391 392 return BCM_E_UNAVAIL; 393 } 394 395 /* 396 * Function: 397 * bcmx_ipfix_mirror_config_set 398 * Purpose: 399 * Set IPFIX mirror control configuration of the specified port. 400 * Parameters: 401 * stage - BCM_IPFIX_STAGE_INGRESS or BCM_IPFIX_STAGE_EGRESS 402 * port - Physical port 403 * config - Pointer to ipfix mirror configuration 404 * Returns: 405 * BCM_E_XXX 406 * Notes: 407 */ 408 int 409 bcmx_ipfix_mirror_config_set(bcm_ipfix_stage_t stage, 410 bcm_gport_t port, 411 bcmx_ipfix_mirror_config_t *config) 412 { 413 int bcm_unit; 414 bcm_port_t bcm_port; 415 416 BCMX_IPFIX_INIT_CHECK; 417 418 BCMX_PARAM_NULL_CHECK(config); 419 420 BCM_IF_ERROR_RETURN 421 (_bcmx_dest_to_unit_port(port, &bcm_unit, &bcm_port, 422 BCMX_DEST_CONVERT_DEFAULT)); 423 424 return bcm_ipfix_mirror_config_set(bcm_unit, stage, port, 425 BCMX_IPFIX_MIRROR_CONFIG_T_PTR_TO_BCM 426 (config)); 427 } 428 429 /* 430 * Function: 431 * bcmx_ipfix_mirror_config_get 432 * Purpose: 433 * Get IPFIX mirror control configuration of the specified port. 434 * Parameters: 435 * stage - BCM_IPFIX_STAGE_INGRESS or BCM_IPFIX_STAGE_EGRESS 436 * port - Physical port 437 * config - Pointer to ipfix mirror configuration 438 * Returns: 439 * BCM_E_XXX 440 * Notes: 441 */ 442 int 443 bcmx_ipfix_mirror_config_get(bcm_ipfix_stage_t stage, 444 bcm_gport_t port, 445 bcmx_ipfix_mirror_config_t *config) 446 { 447 int bcm_unit; 448 bcm_port_t bcm_port; 449 450 BCMX_IPFIX_INIT_CHECK; 451 452 BCMX_PARAM_NULL_CHECK(config); 453 454 BCM_IF_ERROR_RETURN 455 (_bcmx_dest_to_unit_port(port, &bcm_unit, &bcm_port, 456 BCMX_DEST_CONVERT_DEFAULT)); 457 458 return bcm_ipfix_mirror_config_get(bcm_unit, stage, port, 459 BCMX_IPFIX_MIRROR_CONFIG_T_PTR_TO_BCM 460 (config)); 461 } 462 463 /* 464 * Function: 465 * bcmx_ipfix_mirror_port_dest_add 466 * Purpose: 467 * Add an IPFIX mirror destination to the specified port. 468 * Parameters: 469 * stage - BCM_IPFIX_STAGE_INGRESS or BCM_IPFIX_STAGE_EGRESS 470 * port - Physical port 471 * mirror_dest_id - Pre-allocated mirror destination identifier 472 * Returns: 473 * BCM_E_XXX 474 * Notes: 475 */ 476 int 477 bcmx_ipfix_mirror_port_dest_add(bcm_ipfix_stage_t stage, 478 bcm_gport_t port, 479 bcm_gport_t mirror_dest_id) 480 { 481 int bcm_unit; 482 bcm_port_t bcm_port; 483 484 BCMX_IPFIX_INIT_CHECK; 485 486 BCM_IF_ERROR_RETURN 487 (_bcmx_dest_to_unit_port(port, &bcm_unit, &bcm_port, 488 BCMX_DEST_CONVERT_DEFAULT)); 489 490 return bcm_ipfix_mirror_port_dest_add(bcm_unit, stage, port, 491 mirror_dest_id); 492 } 493 494 /* 495 * Function: 496 * bcmx_ipfix_mirror_port_dest_delete 497 * Purpose: 498 * Delete an IPFIX mirror destination from the specified port. 499 * Parameters: 500 * stage - BCM_IPFIX_STAGE_INGRESS or BCM_IPFIX_STAGE_EGRESS 501 * port - Physical port 502 * mirror_dest_id - Pre-allocated mirror destination identifier 503 * Returns: 504 * BCM_E_XXX 505 * Notes: 506 */ 507 int 508 bcmx_ipfix_mirror_port_dest_delete(bcm_ipfix_stage_t stage, 509 bcm_gport_t port, 510 bcm_gport_t mirror_dest_id) 511 { 512 int bcm_unit; 513 bcm_port_t bcm_port; 514 515 BCMX_IPFIX_INIT_CHECK; 516 517 BCM_IF_ERROR_RETURN 518 (_bcmx_dest_to_unit_port(port, &bcm_unit, &bcm_port, 519 BCMX_DEST_CONVERT_DEFAULT)); 520 521 return bcm_ipfix_mirror_port_dest_delete(bcm_unit, stage, port, 522 mirror_dest_id); 523 } 524 525 /* 526 * Function: 527 * bcmx_ipfix_mirror_port_dest_delete_all 528 * Purpose: 529 * Delete all IPFIX mirror destinations from the specified port. 530 * Parameters: 531 * stage - BCM_IPFIX_STAGE_INGRESS or BCM_IPFIX_STAGE_EGRESS 532 * port - Physical port 533 * Returns: 534 * BCM_E_XXX 535 * Notes: 536 */ 537 int 538 bcmx_ipfix_mirror_port_dest_delete_all(bcm_ipfix_stage_t stage, 539 bcm_gport_t port) 540 { 541 int bcm_unit; 542 bcm_port_t bcm_port; 543 544 BCMX_IPFIX_INIT_CHECK; 545 546 BCM_IF_ERROR_RETURN 547 (_bcmx_dest_to_unit_port(port, &bcm_unit, &bcm_port, 548 BCMX_DEST_CONVERT_DEFAULT)); 549 550 return bcm_ipfix_mirror_port_dest_delete_all(bcm_unit, stage, port); 551 } 552 553 /* 554 * Function: 555 * bcmx_ipfix_mirror_port_dest_get 556 * Purpose: 557 * Get all IPFIX mirror destinations for the specified port. 558 * Parameters: 559 * stage - BCM_IPFIX_STAGE_INGRESS or BCM_IPFIX_STAGE_EGRESS 560 * port - Physical port 561 * mirror_dest_size - Size of mirror_dest_id array 562 * mirror_dest_id - Array to store mirror destination identifier 563 * mirror_dest_count - Number of entries stored in the mirror_dest_id array 564 * Returns: 565 * BCM_E_XXX 566 * Notes: 567 */ 568 int 569 bcmx_ipfix_mirror_port_dest_get(bcm_ipfix_stage_t stage, 570 bcm_gport_t port, 571 int mirror_dest_size, 572 bcm_gport_t *mirror_dest_id, 573 int *mirror_dest_count) 574 { 575 int bcm_unit; 576 bcm_port_t bcm_port; 577 578 BCMX_IPFIX_INIT_CHECK; 579 580 BCM_IF_ERROR_RETURN 581 (_bcmx_dest_to_unit_port(port, &bcm_unit, &bcm_port, 582 BCMX_DEST_CONVERT_DEFAULT)); 583 584 return bcm_ipfix_mirror_port_dest_get(bcm_unit, stage, port, 585 mirror_dest_size, mirror_dest_id, 586 mirror_dest_count); 587 } 588