* Appends one or more nexthops denoted by @wm to the nexthop group @gr_orig. * * Returns referenced nexthop group or NULL. In the latter case, @perror is * filled with an error code. * Note that function does NOT care if the next nexthops already exists * in the @gr_orig. As a result, they will be added, resulting in the * same nexthop being present multiple times in the new group. */
| 509 | * same nexthop being present multiple times in the new group. |
| 510 | */ |
| 511 | static struct nhgrp_priv * |
| 512 | append_nhops(struct nh_control *ctl, const struct nhgrp_object *gr_orig, |
| 513 | struct weightened_nhop *wn, int num_nhops, int *perror) |
| 514 | { |
| 515 | char storage[64]; |
| 516 | struct weightened_nhop *pnhops; |
| 517 | struct nhgrp_priv *nhg_priv; |
| 518 | const struct nhgrp_priv *src_priv; |
| 519 | size_t sz; |
| 520 | int curr_nhops; |
| 521 | |
| 522 | src_priv = NHGRP_PRIV_CONST(gr_orig); |
| 523 | curr_nhops = src_priv->nhg_nh_count; |
| 524 | |
| 525 | *perror = 0; |
| 526 | |
| 527 | sz = (src_priv->nhg_nh_count + num_nhops) * (sizeof(struct weightened_nhop)); |
| 528 | /* optimize for <= 4 paths, each path=16 bytes */ |
| 529 | if (sz <= sizeof(storage)) |
| 530 | pnhops = (struct weightened_nhop *)&storage[0]; |
| 531 | else { |
| 532 | pnhops = malloc(sz, M_TEMP, M_NOWAIT); |
| 533 | if (pnhops == NULL) { |
| 534 | *perror = ENOMEM; |
| 535 | return (NULL); |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | /* Copy nhops from original group first */ |
| 540 | memcpy(pnhops, src_priv->nhg_nh_weights, |
| 541 | curr_nhops * sizeof(struct weightened_nhop)); |
| 542 | memcpy(&pnhops[curr_nhops], wn, num_nhops * sizeof(struct weightened_nhop)); |
| 543 | curr_nhops += num_nhops; |
| 544 | |
| 545 | nhg_priv = get_nhgrp(ctl, pnhops, curr_nhops, perror); |
| 546 | |
| 547 | if (pnhops != (struct weightened_nhop *)&storage[0]) |
| 548 | free(pnhops, M_TEMP); |
| 549 | |
| 550 | if (nhg_priv == NULL) |
| 551 | return (NULL); |
| 552 | |
| 553 | return (nhg_priv); |
| 554 | } |
| 555 | |
| 556 | |
| 557 | /* |
no test coverage detected