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

Add a P4Info API to the control-plane folder and P4Tools. #4381

Merged
merged 5 commits into from
Feb 2, 2024

Conversation

fruffy
Copy link
Collaborator

@fruffy fruffy commented Jan 30, 2024

This PR adds an API to look up P4Runtime objects in the P4Info file. Unlike the P4RuntimeSymbolTable, which needs to be explicitly initialized, this API only requires the corresponding P4Info file to look up objects. Additionally, it is possible to look up an object using its ID. This API was partially moved out of the p4runtime.cpp gtest. It seemed generally useful. The API is also tested using this file.

P4RuntimeMaps is a simple class to cache these pairings of ID and control-plane name. It is constructed from the provided P4Runtime file. For elements such as parameters or matches, which may depend on a parent table, we compute a unique combination of the parameter id and the table parent id using a pairing function (http://szudzik.com/ElegantPairing.pdf).

@fruffy fruffy added control-plane Topics related to the control-plane or P4Runtime. p4tools Topics related to the P4Tools back end labels Jan 30, 2024
@fruffy fruffy requested a review from smolkaj January 30, 2024 22:11
@fruffy fruffy changed the title Add a P4Runtime API to P4Tools. Add a P4Info API to the control-plane folder and P4Tools. Jan 30, 2024
Copy link
Member

@smolkaj smolkaj left a comment

Choose a reason for hiding this comment

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

Just minor nits, otherwise looks great to me!

P4InfoMaps(const P4InfoMaps &) = default;
P4InfoMaps(P4InfoMaps &&) = default;
P4InfoMaps &operator=(const P4InfoMaps &) = default;
P4InfoMaps &operator=(P4InfoMaps &&) = default;
Copy link
Member

Choose a reason for hiding this comment

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

Nit: lines 50-53 are automatically generated by the compiler, right? So at least at Google, we tend to omit them.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Can do, this code is also auotgenerated by my development environment.


/// Looks up the P4Runtime id for the given control plane name in the pre-computed P4Runtime-ID
/// map. @returns std::nullopt if the name is not in the map.
[[nodiscard]] std::optional<uint64_t> lookupP4RuntimeId(cstring controlPlaneName) const;
Copy link
Member

Choose a reason for hiding this comment

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

Nit: "lookup" -> "lookUp"?

"lookup" to me is a noun or perhaps adjective, "look up" is the verb.

Copy link
Member

Choose a reason for hiding this comment

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

Ditto once more below.

// In case things change down the road.
static_assert(std::is_convertible_v<p4rt_id_t, uint32_t> &&
std::is_same_v<p4rt_id_t, uint32_t>);
return x >= y ? static_cast<uint64_t>(x) * x + x + y : x + static_cast<uint64_t>(y) * y;
Copy link
Member

Choose a reason for hiding this comment

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

Link to wiki?

uint64_t szudzikPairing(p4rt_id_t x, p4rt_id_t y);

/// This object maps P4 control plane names to their P4Runtime IDs and vice versa.
/// It uses the P4Info object to populate the maps.
Copy link
Member

Choose a reason for hiding this comment

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

It seems like we're additionally introducing new unique IDs for certain objects that do not have unique IDs in P4Runtime / the P4info? Can we document that?

namespace P4::ControlPlaneAPI {

/// Generic meta function which searches an object by @name in the given range
/// and @returns the P4Runtime representation, or std::nullopt if none is found.
Copy link
Member

Choose a reason for hiding this comment

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

Seems to return a null pointer, not std::nullopt. Update documetnation?

Ditto one more time for the other overload

control-plane/p4infoApi.h Show resolved Hide resolved
ASSERT_TRUE(myDirectCounter1 != nullptr);
EXPECT_EQ(unsigned(P4Ids::DIRECT_COUNTER), myDirectCounter1->preamble().id() >> 24);
EXPECT_EQ(myDirectCounter1->preamble().id(), myTable1->direct_resource_ids(0));
}
// checks that myDirectCounter2 with the right ID prefix
{
auto *myTable2 = findTable(*test, "myTable2");
auto myTable2 = findP4RuntimeTable(*test->p4Info, "myTable2");
Copy link
Member

Choose a reason for hiding this comment

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

Nit: Here and always, would recommend auto* over auto for pointers, as before.

@fruffy fruffy enabled auto-merge (squash) February 2, 2024 16:42
@fruffy fruffy merged commit 4c849a6 into main Feb 2, 2024
16 checks passed
@fruffy fruffy deleted the fruffy/p4runtime_api branch February 2, 2024 17:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
control-plane Topics related to the control-plane or P4Runtime. p4tools Topics related to the P4Tools back end
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants