|
| 1 | +/* |
| 2 | + * Copyright (c) 2004-present, Facebook, Inc. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. An additional grant |
| 7 | + * of patent rights can be found in the PATENTS file in the same directory. |
| 8 | + * |
| 9 | + */ |
| 10 | + |
| 11 | +#pragma once |
| 12 | + |
| 13 | +#include <fmt/format.h> |
| 14 | +#include <functional> |
| 15 | +#include <map> |
| 16 | +#include <string> |
| 17 | + |
| 18 | +#include "fboss/agent/gen-cpp2/switch_config_types.h" |
| 19 | +#include "fboss/cli/fboss2/session/ConfigSession.h" |
| 20 | + |
| 21 | +namespace facebook::fboss { |
| 22 | + |
| 23 | +/** |
| 24 | + * Helper function to set a buffer pool configuration field. |
| 25 | + * |
| 26 | + * This function handles the common logic for all buffer pool configuration |
| 27 | + * commands (shared-bytes, headroom-bytes, reserved-bytes): |
| 28 | + * - Gets or creates the buffer pool config map |
| 29 | + * - Gets or creates the specific buffer pool entry |
| 30 | + * - Calls the setter to update the specific field |
| 31 | + * - Saves the config |
| 32 | + * - Updates the required action level to AGENT_RESTART |
| 33 | + * |
| 34 | + * @param poolName The name of the buffer pool to configure |
| 35 | + * @param fieldName The name of the field being set (for the success message) |
| 36 | + * @param value The value to set |
| 37 | + * @param setter A lambda that sets the specific field on the BufferPoolConfig. |
| 38 | + * For new configs, it receives a reference to the new config. |
| 39 | + * For existing configs, it receives a reference to the existing |
| 40 | + * config. |
| 41 | + * @return A success message string |
| 42 | + */ |
| 43 | +template <typename SetterFn> |
| 44 | +std::string setBufferPoolConfigField( |
| 45 | + const std::string& poolName, |
| 46 | + const std::string& fieldName, |
| 47 | + int32_t value, |
| 48 | + SetterFn setter) { |
| 49 | + auto& session = ConfigSession::getInstance(); |
| 50 | + auto& agentConfig = session.getAgentConfig(); |
| 51 | + auto& switchConfig = *agentConfig.sw(); |
| 52 | + |
| 53 | + // Get or create the bufferPoolConfigs map |
| 54 | + if (!switchConfig.bufferPoolConfigs()) { |
| 55 | + switchConfig.bufferPoolConfigs() = |
| 56 | + std::map<std::string, cfg::BufferPoolConfig>{}; |
| 57 | + } |
| 58 | + |
| 59 | + auto& bufferPoolConfigs = *switchConfig.bufferPoolConfigs(); |
| 60 | + |
| 61 | + // Check if the buffer pool exists |
| 62 | + auto it = bufferPoolConfigs.find(poolName); |
| 63 | + if (it == bufferPoolConfigs.end()) { |
| 64 | + // Create a new buffer pool config |
| 65 | + // Note: sharedBytes is required, so we default it to 0 |
| 66 | + cfg::BufferPoolConfig newConfig; |
| 67 | + newConfig.sharedBytes() = 0; |
| 68 | + setter(newConfig); |
| 69 | + bufferPoolConfigs[poolName] = std::move(newConfig); |
| 70 | + } else { |
| 71 | + // Update the existing buffer pool config |
| 72 | + setter(it->second); |
| 73 | + } |
| 74 | + |
| 75 | + // Save the updated config and update the required action level |
| 76 | + // Buffer pool changes always require agent restart |
| 77 | + session.saveConfig(cli::ConfigActionLevel::AGENT_RESTART); |
| 78 | + |
| 79 | + return fmt::format( |
| 80 | + "Successfully set {} for buffer-pool '{}' to {}", |
| 81 | + fieldName, |
| 82 | + poolName, |
| 83 | + value); |
| 84 | +} |
| 85 | + |
| 86 | +} // namespace facebook::fboss |
0 commit comments