Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ovsp4rt] Implement GetTxAccVsiPort() #737

Closed
wants to merge 42 commits into from
Closed

Conversation

ffoulkes
Copy link
Contributor

@ffoulkes ffoulkes commented Jan 12, 2025

  • Extracted code from ConfigFdbEntry() and ConfigSrcPortEntry() to a new GetTxAccVsiPort() function. This reduces the complexity of the calling functions, which don't need to know how the port is obtained, and it removes duplicate code.

See later comments in the PR for AI analysis of this refactoring.

This PR is ES2K-specific. GetTxiAccVsiPort is only called by ES2K functions.

Note

The purpose of this PR is to refactor. I propose to make the mutability changes noted in the comments in a second PR, and any other changes in subsequent PRs.

ffoulkes and others added 30 commits October 2, 2024 09:13
Document that summarizes apparent encoding issues in ovsp4rt.

Signed-off-by: Derek Foster <[email protected]>
- Encode and validate IPv6 addresses as arrays of uint16_t.
  This allows us to provide better feedback in case of a
  mismatch.

- Make a couple of minor changes to encode_addr_test.

Signed-off-by: Derek Foster <[email protected]>
- Enabled CMAKE_EXPORT_COMPILE_COMMANDS. This option instructs CMake
  to generate a `compile_commands.json` file in the build directory,
  for use by other tools (such as `clang-tidy`)

- Expanded CMAKE_INSTALL_PREFIX to an absolute path, which is what
  happens when you set `CMAKE_INSTALL_PREFIX` on the CMake command
  line.

Signed-off-by: Derek Foster <[email protected]>
- Moved the set_property() for the STRINGS property on P4OVS_MODE
  after its definition. This fixes a CMake configuration error
  that cropped up in the Tofino build.

Signed-off-by: Derek Foster <[email protected]>
Bumps [actions/setup-go](https://github.com/actions/setup-go) from 5.0.2 to 5.1.0.
- [Release notes](https://github.com/actions/setup-go/releases)
- [Commits](actions/setup-go@v5.0.2...v5.1.0)

---
updated-dependencies:
- dependency-name: actions/setup-go
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
- Implemented an `install-path-presets` preset that defines
  DEPEND_INSTALL_DIR and SDE_INSTALL_DIR from the DEPEND_INSTALL
  and SDE_INSTALL environment variables.

Signed-off-by: Derek Foster <[email protected]>
- Added sample cmake pre-load scripts and a user presets file,
  and updated the buildsystem to install them in the `share`
  directory tree.

- Added /dpdk.cmake and /es2k.cmake to .gitignore.

Signed-off-by: Derek Foster <[email protected]>
- Sanitized a couple of file paths.

- Changed "pre-load" to "preload". The CMake documentation uses
  the hyphenated form, but I find it distracting.

Signed-off-by: Derek Foster <[email protected]>
- Removed the experimental p4cpconfig script. It has been
  superseded by cmake presets.

Signed-off-by: Derek Foster <[email protected]>
Signed-off-by: Derek Foster <[email protected]>
- Marked the BUILD_JOURNAL and BUILD_SPIES variables as advanced.
  They are internal options, used to enable features that are
  still under development, and should normally be hidden.

Signed-off-by: Derek Foster <[email protected]>
- This is an internal variable intended for use by developers.

Signed-off-by: Derek Foster <[email protected]>
- Added logic to generate an uninstall script and defined a
  target to run the script.

Signed-off-by: Derek Foster <[email protected]>
- Created an experimental CMakePresets.json file.

- Defined property strings for the P4OVS_MODE variable.

Signed-off-by: Derek Foster <[email protected]>
- Updated the stratum and krnlmon submodule references.

Signed-off-by: Derek Foster <[email protected]>
- Implemented the `Client` `ClientInterface`, and `ClientMock`
  classes, to abstract the interface to the P4Runtime server.

Signed-off-by: Derek Foster <[email protected]>
- Renamed the Journal "schema" element to "version".

- Added `const` qualifier to a couple of parameters.

Signed-off-by: Derek Foster <[email protected]>
Bumps [actions/setup-go](https://github.com/actions/setup-go) from 5.1.0 to 5.2.0.
- [Release notes](https://github.com/actions/setup-go/releases)
- [Commits](actions/setup-go@v5.1.0...v5.2.0)

---
updated-dependencies:
- dependency-name: actions/setup-go
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
The purpose of the `Client` class is to replace the discrete logic
that interacts with the P4Runtime server with an object that
implements an abstract interface.

- `ClientInterface` is an abstract class that defines the interface.
- `Client` is an implementation of `ClientInterface` that replaces
  the current discrete logic.
- `ClientMock` is a mock implementation of `ClientInterface`.

For this to work, we need to hide the session object from the user.

Signed-off-by: Derek Foster <[email protected]>
- Modified CMakeLists.txt files to fix compile and link
  errors when building with BUILD_CLIENT enabled.

Signed-off-by: Derek Foster <[email protected]>
- Changed `ubuntu-latest` to `ubuntu-22.04` in the workflows,
  in preparation for GitHub bumping ubuntu-latest to 24.04.

Signed-off-by: Derek Foster <[email protected]>
* [ovsp4rt] Implement journaling api and client

- Created JournalClient, a subclass of Client that is
  instrumented to record interactions between OVS and
  the P4Runtime server.

Signed-off-by: Derek Foster <[email protected]>

* [ovsp4rt] Rough in new Journal methods

- Added methods to the Journal class to record P4Runtime server
  requests and responses.

Signed-off-by: Derek Foster <[email protected]>

---------

Signed-off-by: Derek Foster <[email protected]>
- Moved the C API wrapper functions to a separate source
  file. This makes it possible to substitute a different
  implementation of the API, for testing.

  Extracted from PR #674. Works with an updated version
  of ovsp4rt.cc that has not been committed. Currently
  excluded from the build.

Signed-off-by: Derek Foster <[email protected]>
- Created a variant of the public API that uses
  JournalClient objects.

  Extracted from PR #674. Works with several files that
  have not yet been committed, or that are being committed
  separately. Currently excluded from the build.

Signed-off-by: Derek Foster <[email protected]>
* Update to stratum-deps v1.3.4
* Update stratum submodule to v3.3.0.0

Signed-off-by: Derek Foster <[email protected]>
- Corrected ES2K to DPDK in the DPDK version of the file.

Signed-off-by: Derek Foster <[email protected]>
Signed-off-by: Derek Foster <[email protected]>
- Updated cmake listfile to use `newovsp4rt.cc` instead of
  `ovsp4rt.cc` when the BUILD_CLIENT option is enabled.

- Modified newovsp4rt.cc to use a `Client` object instead of an
  `OvsP4rtSession` object to communicate with the P4Runtime server.

- Made each public C API function a wrapper around a C++ function
  that accepts a `Client` object as a parameter. This allows a
  test to replace the object with a test double (e.g. a mock).

- Moved the C API functions to another file
  (`ovsp4rt_standard_api.cc`), separating the user interface from
  the implementation.

- Created a second implementation of the C API
  (`ovsp4rt_journal_api.cc`) that enables journaling.

- Added an `ovsp4rt_build_and_test` job to the pipeline, to verify
  that ovsp4rt builds when BUILD_CLIENT and BUILD_JOURNAL are enabled.

Signed-off-by: Derek Foster <[email protected]>
- Moved ovsp4rt_str_to_tunnel_type() to a separate source
  file for C api functions that are not wrappers.

Signed-off-by: Derek Foster <[email protected]>
- Renamed the functions in the internal C++ API from
  ConfigEntry to DoConfigEntry, to keep them from being
  confused with ConfigTableEntry functions.

The conventions are:

- A PrepareTableEntry function populates the fields of a
  TableEntry.

- A ConfigTableEntry function initializes a WriteRequest,
  prepares the TableEntry, and sends the request to the
  P4Runtime server.

- A GetTableEntry function initializes a ReadRequest,
  prepares the TableEntry, sends the request to the
  P4Runtime server, and returns the response.

- A DoConfigEntry function implements the core logic of
  an ovsp4rt_table_entry C API. It may make one or more
  ConfigTableEntry or GetTableEntry calls.

Signed-off-by: Derek Foster <[email protected]>
- Modified the functions in the internal C++ API to return
  `absl::Status`, allowing a test program to verify whether
  the function encountered an error.

- Updated the C API wrappers to ignore the returned Status
  explicitly. The public APIs currently have a `void` return
  type.

These changes only apply when BUILD_CLIENT is enabled.

Signed-off-by: Derek Foster <[email protected]>
- Extracted the GetTxiAccVsiHostSrcPort() function from
  ConfigFdbEntry() and ConfigSrcPortEntry().

  This change is ES2K-specific.

Signed-off-by: Derek Foster <[email protected]>
@ffoulkes ffoulkes changed the title [ovsp4rt] Implement GetTxiAccVsiHostSrcPort() [ovsp4rt] Implement GetTxiAccVsiPort() Jan 15, 2025
@ffoulkes
Copy link
Contributor Author

ffoulkes commented Jan 23, 2025

Here is ChatGPT's assessment of the first refactoring:

Let's compare the original and refactored code and identify key differences to determine if they are functionally equivalent:

Key Changes:

  1. Refactoring of TxAccVsi Port Logic:

    • Original Code: The logic to obtain the src_port was handled directly within DoConfigFdbEntry. It involves calling GetTxAccVsiTableEntry, processing the ReadResponse, and extracting the src_port.
    • Refactored Code: This logic has been extracted into a new function, GetTxAccVsiPort, which encapsulates the same functionality. The refactored DoConfigFdbEntry then calls this function and updates learn_info.src_port accordingly.
  2. GetTxAccVsiPort:

    In the refactored version, the code that fetches the src_port from the TxAccVsiTableEntry and processes the response has been moved to a separate function (GetTxAccVsiPort). This makes the DoConfigFdbEntry function cleaner and easier to follow. The GetTxAccVsiPort function itself returns the processed src_port, which is then used in the DoConfigFdbEntry function.

  3. host_sp Handling:

    In the original code, the host_sp variable is calculated inline within the DoConfigFdbEntry function.
    In the refactored code, host_sp is calculated in the GetTxAccVsiPort function and then returned to DoConfigFdbEntry.

  4. Error Handling:

    Both versions handle errors similarly, but the error handling is encapsulated in the GetTxAccVsiPort function in the refactored version. It checks if the result is an error (!status.ok()) before continuing.

Functional Equivalence:

  • The core logic of connecting to the client, fetching P4Info, checking tunnel entries, and configuring the FDB entries remains unchanged in both versions.
  • The refactoring does not alter the behavior of the system. It simply moves the logic of obtaining the src_port into a separate function (GetTxAccVsiPort), which is then invoked in DoConfigFdbEntry. This refactor improves the modularity and readability of the code without changing its overall functionality.
  • The handling of the learn_info.src_port and the update logic is preserved in the refactored code, so the flow of the program remains the same.

Conclusion:

The original and refactored code are functionally equivalent. The refactoring only reorganizes the code to improve clarity and modularity, without altering the core behavior.

@ffoulkes
Copy link
Contributor Author

ffoulkes commented Jan 23, 2025

Its conclusions on the second refactoring were as follows:

Functional Equivalence:

  • Core Logic: The core logic remains the same as in the original code. The refactored version still follows the same steps—connect to the client, fetch P4Info, get the src_port, and configure the entry. The main difference is that fetching and processing the src_port has been abstracted into the helper function GetTxAccVsiPort.
  • Error Handling: The error handling around the src_port retrieval and the flow of control in case of failure are consistent with the original version.

Conclusion:

The refactored code is functionally equivalent to the original code. The change primarily improves the structure of the function by extracting the logic for obtaining and processing the src_port into the GetTxAccVsiPort function. This refactor makes the DoConfigSrcPortEntry function cleaner and more maintainable, while maintaining the same behavior as the original code.

@ffoulkes ffoulkes changed the title [ovsp4rt] Implement GetTxiAccVsiPort() [ovsp4rt] Implement GetTxAccVsiPort() Jan 23, 2025
}
// TODO(derek): This break exits to the outermost for loop,
// advancing to the next table entry. Is this correct?
// Would `return host_sp` be better?
Copy link
Contributor Author

@ffoulkes ffoulkes Jan 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we replace break with return host_sp, we can (1) define and initialize host_sp within the if statement, and (2) change line 2153 to return 0.

I also propose to rename the loop index from param_bytes to i. The long name is misleading and adds unnecessary complexity.

@ffoulkes ffoulkes closed this Jan 24, 2025
@ffoulkes ffoulkes deleted the get-txi-src-port branch January 24, 2025 20:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant