Skip to content

[Nexthop][fboss2-dev] Move explicit template instantiations to individual files - #931

Closed
benoit-nexthop wants to merge 1 commit into
meta:mainfrom
nexthop-ai:fboss2-cli-prototype_part29
Closed

benoit-nexthop wants to merge 1 commit into
meta:mainfrom
nexthop-ai:fboss2-cli-prototype_part29

Conversation

@benoit-nexthop

@benoit-nexthop benoit-nexthop commented Feb 11, 2026 •

Copy link
Copy Markdown
Contributor

Pre-submission checklist

  • I've ran the linters locally and fixed lint errors related to the files I modified in this PR. You can install the linters by running pip install -r requirements-dev.txt && pre-commit install
  • pre-commit run

Summary

Move explicit template instantiations from the monolithic CmdHandlerImplConfig.cpp file to individual command .cpp files. This is the first step in refactoring the CLI command registration system to avoid creating a monolithic include file.

The non-config commands will be done in a separate PR.

Changes:

  • Created 11 new .cpp files for stub commands that were missing them
  • Added template instantiations to 20 existing command .cpp files
  • Updated cmake/CliFboss2.cmake and fboss/cli/fboss2/BUCK build files
  • Deleted fboss/cli/fboss2/CmdHandlerImplConfig.cpp

Each command .cpp file now includes CmdHandler.cpp and has its own explicit template instantiation, following the pattern:

#include "fboss/cli/fboss2/CmdHandler.cpp"

template void CmdHandler<CmdClass, CmdClassTraits>::run();

Original change by @manoharan-nexthop

Note: this change is part of a series, the previous one is #919, the next one is #935.

Test Plan

Ran all the unit tests.

@meta-cla meta-cla Bot added the CLA Signed label Feb 11, 2026
meta-codesync Bot pushed a commit that referenced this pull request Mar 26, 2026
…p class (#919)

Summary:
**Pre-submission checklist**
- [x] I've ran the linters locally and fixed lint errors related to the files I modified in this PR. You can install the linters by running `pip install -r requirements-dev.txt && pre-commit install`
- [x] `pre-commit run`

This PR consolidates the separate `CmdConfigInterfaceDescription` and `CmdConfigInterfaceMtu` commands into a unified `CmdConfigInterface` command that can set multiple interface attributes in a single invocation.
```
# Set description
config interface eth1/1/1 description "My port description"

# Set MTU
config interface eth1/1/1 mtu 9000

# Set both in one command
config interface eth1/1/1 description "My port" mtu 9000

# Apply to multiple interfaces
config interface eth1/1/1,eth1/2/1 description "Uplink" mtu 1500
```

Original change by hillol-nexthop

Note: this change is part of a series, the previous one is #900, the next one is #931.

## Implementation Details

1. **New `InterfaceConfig` class**
- Parses CLI tokens to separate port names from attribute key-value
pairs
  - Supports description and mtu attributes (case-insensitive)
  - Validates attribute names and detects missing values
  - Wraps `InterfaceList` for port/interface resolution
2. **Updated CmdConfigInterface**
  - Changed from pass-through command to functional command
  - Implements `queryClient` to process description and mtu attributes
  - Sets `port->description()` for description attribute
  - Sets `interface->mtu()` for mtu attribute with validation
3. **Updated subcommands to use `InterfaceConfig` instead of `InterfaceList`**
4. **Deleted obsolete pairs of `.cpp` / `.h`**
5. **New comprehensive test suite**
  - 23 tests covering `InterfaceConfig` validation and `CmdConfigInterface::queryClient` functionality
  - Backward Compatibility
  - Subcommands like switchport, pfc-config, and queuing-policy continue to work as before

Pull Request resolved: #919

Test Plan:
**Note:** The e2e test is already implemented in #893
Above mentioned list of new test file, deleted and updated test files.
Existing unit and end to end tests pass.

Reviewed By: KevinYakar

Differential Revision: D98340162

Pulled By: joseph5wu

fbshipit-source-id: 82d10267ae00fb6e81710f61781f0746d591f836
@joseph5wu

Copy link
Copy Markdown
Contributor

There's some concern about affect compiling time with such change.

The Core Problem: #include "CmdHandler.cpp"

Notice line 11 of both CmdHandlerImpl.cpp and CmdHandlerImplConfig.cpp:

#include "fboss/cli/fboss2/CmdHandler.cpp"

This is not a typo — it's including a .cpp file. The template method definitions (run(), runHelper(), getValidFilters(), getValidAggs(), etc.) in CmdHandler.cpp are ~620 lines of heavy template code. They must be visible at the point of explicit instantiation.

If you moved each instantiation to its respective CmdShowAcl.cpp, every one of those files would need #include "CmdHandler.cpp" too. That means:

┌───────────────────────┬───────────────────────────────────────────────────┐
│ Approach │ Times CmdHandler.cpp is compiled │
├───────────────────────┼───────────────────────────────────────────────────┤
│ Current (centralized) │ 2 (CmdHandlerImpl.cpp + CmdHandlerImplConfig.cpp) │
├───────────────────────┼───────────────────────────────────────────────────┤
│ Per-command .cpp │ ~130+ (once per command file) │
└───────────────────────┴───────────────────────────────────────────────────┘

That's a massive build time regression — each TU would independently compile all the template machinery (thrift reflection, filter/aggregate logic, thread pool executor, etc.).

Other Tradeoffs

Incremental builds: The current pattern means changing CmdHandler.cpp recompiles 2 large files. The per-command approach would recompile ~130 files, but each is smaller. In practice, changes to CmdHandler.cpp are rare — it's stable infrastructure code. Changes to individual commands (which are frequent) already only recompile that command's own .cpp.

Modularity: Yes, adding a new command requires touching CmdHandlerImpl.cpp. But it's just 1-2 lines (include + instantiation), which is a trivial cost compared to the build time tradeoff.

@benoit-nexthop
benoit-nexthop force-pushed the fboss2-cli-prototype_part29 branch from 1b76179 to 1286def Compare March 27, 2026 01:10
@benoit-nexthop
benoit-nexthop requested review from a team as code owners March 27, 2026 01:10
@benoit-nexthop
benoit-nexthop force-pushed the fboss2-cli-prototype_part29 branch from 1286def to 62cdffd Compare March 27, 2026 01:27
# Summary

Move explicit template instantiations from the monolithic
CmdHandlerImplConfig.cpp file to individual command .cpp files.
This is the first step in refactoring the CLI command registration
system to avoid creating a monolithic include file.

Changes:
- Created 11 new .cpp files for stub commands that were missing them
- Added template instantiations to 20 existing command .cpp files
- Updated cmake/CliFboss2.cmake and fboss/cli/fboss2/BUCK build files
- Deleted fboss/cli/fboss2/CmdHandlerImplConfig.cpp

Each command .cpp file now includes CmdHandler.cpp and has its own
explicit template instantiation, following the pattern:
  #include "fboss/cli/fboss2/CmdHandler.cpp"
  template void CmdHandler<CmdClass, CmdClassTraits>::run();

# Test Plan

Ran all the unit tests.
@benoit-nexthop
benoit-nexthop force-pushed the fboss2-cli-prototype_part29 branch from 62cdffd to d18515c Compare March 27, 2026 01:40
@benoit-nexthop

Copy link
Copy Markdown
Contributor Author

There's some concern about affect compiling time with such change.

Claude is wrong 😅

The Core Problem: #include "CmdHandler.cpp"

Notice line 11 of both CmdHandlerImpl.cpp and CmdHandlerImplConfig.cpp:

#include "fboss/cli/fboss2/CmdHandler.cpp"

This is not a typo — it's including a .cpp file. The template method definitions (run(), runHelper(), getValidFilters(), getValidAggs(), etc.) in CmdHandler.cpp are ~620 lines of heavy template code. They must be visible at the point of explicit instantiation.

Yes it looks weird but it's because normally the template must be in a .h file. Right now it's in a .cpp file because we're moving away from the old way of doing things. We could rename CmdHandler.cpp to CmdHandler.h when we're finished and then it would be no different than including a header in all the CLI command handlers' .cpp files.

If you moved each instantiation to its respective CmdShowAcl.cpp, every one of those files would need #include "CmdHandler.cpp" too. That means:

┌───────────────────────┬───────────────────────────────────────────────────┐ │ Approach │ Times CmdHandler.cpp is compiled │ ├───────────────────────┼───────────────────────────────────────────────────┤ │ Current (centralized) │ 2 (CmdHandlerImpl.cpp + CmdHandlerImplConfig.cpp) │ ├───────────────────────┼───────────────────────────────────────────────────┤ │ Per-command .cpp │ ~130+ (once per command file) │ └───────────────────────┴───────────────────────────────────────────────────┘

That's a massive build time regression — each TU would independently compile all the template machinery (thrift reflection, filter/aggregate logic, thread pool executor, etc.).

Claude is mistaken: this decouples the build of all the commands, so they can all be built in parallel. In practice in a heavily parallel build like what we get with BUCK / cmake, the main factors driving build time are how much can you build in parallel and what's the slowest build units. This massive .cpp file is a super-#include monster, in an incremental recompilation it needs to be rebuilt whenever ANY of the CLI commands change and it needs to rebuild something in one shot that #includes everything else. This file is also a recurring source of conflicts, and fixing conflicts takes time that is expensive.

Other Tradeoffs

Incremental builds: The current pattern means changing CmdHandler.cpp recompiles 2 large files. The per-command approach would recompile ~130 files, but each is smaller. In practice, changes to CmdHandler.cpp are rare — it's stable infrastructure code. Changes to individual commands (which are frequent) already only recompile that command's own .cpp.

Wrong, in practice what happens is that when someone works on a CLI command, if they change the header file of any of the CLI command, they trigger the recompilation of this massive .cpp file that includes everything.

Modularity: Yes, adding a new command requires touching CmdHandlerImpl.cpp. But it's just 1-2 lines (include + instantiation), which is a trivial cost compared to the build time tradeoff.

Wrong, in practice what happens is that this file is a source of conflicts, and the human time wasted solving those conflicts, no matter how trivial, is costing way more than the compilation time, but even on the compilation the argument was wrong anyway.

@meta-codesync

meta-codesync Bot commented Mar 27, 2026

Copy link
Copy Markdown
Contributor

@joseph5wu has imported this pull request. If you are a Meta employee, you can view this in D98510944.

@meta-codesync

meta-codesync Bot commented Mar 27, 2026

Copy link
Copy Markdown
Contributor

@joseph5wu merged this pull request in 675e814.

meta-codesync Bot pushed a commit that referenced this pull request Mar 28, 2026
Summary:
**Pre-submission checklist**
- [x] I've ran the linters locally and fixed lint errors related to the files I modified in this PR. You can install the linters by running `pip install -r requirements-dev.txt && pre-commit install`
- [x] `pre-commit run`

Implements a new CLI command `fboss2-dev config session clear` that removes the current config session files (`~/.fboss2/agent.conf` and `~/.fboss2/cli_metadata.json`).

This command allows users to discard any pending config session changes without committing them, providing a clean way to abandon a session and start fresh.

Original change by hillol-nexthop

Note: this change is part of a series, the previous one is #931.

## Changes

- Added `CmdConfigSessionClear.h` and `CmdConfigSessionClear.cpp` implementing the clear command
- Registered the command in `CmdListConfig.cpp` under config session subcommands
- Added unit tests in `CmdConfigSessionTest.cpp` covering:
- Clearing an existing session removes both files
- Clearing when no session exists (no-op)
- Clearing with partial session state (only config file exists)
- Added e2e test in `ConfigSessionClearTest.cpp` that creates a session and clears it
- Added unit test in `CmdConfigSessionClearTest.cpp`

Pull Request resolved: #935

Test Plan:
New E2E and unit tests.

## Sample usage

```
[admin@fboss101 .fboss2]$ fboss2-dev config session clear
Config session cleared successfully.
[admin@fboss101 .fboss2]$ fboss2-dev config session clear
No config session exists. Nothing to clear.
```

Reviewed By: AarjunC

Differential Revision: D98555036

Pulled By: joseph5wu

fbshipit-source-id: 6229f59a3d49479469fa0e6ebe5794ac988f5d82
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants