Skip to content

Commit

Permalink
Review comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
fruffy committed Feb 2, 2024
1 parent 2346753 commit cbc581b
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 38 deletions.
5 changes: 3 additions & 2 deletions backends/p4tools/common/control_plane/p4info_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace P4::ControlPlaneAPI {
P4InfoMaps::P4InfoMaps(const p4::config::v1::P4Info &p4Info) { buildP4InfoMaps(p4Info); }

uint64_t szudzikPairing(p4rt_id_t x, p4rt_id_t y) {
// See https://en.wikipedia.org/wiki/Pairing_function#Other_pairing_functions
// This static assert ensures that p4rt_id_t used here is a uint32_t.
// In case things change down the road.
static_assert(std::is_convertible_v<p4rt_id_t, uint32_t> &&
Expand Down Expand Up @@ -73,15 +74,15 @@ void P4InfoMaps::buildP4InfoMaps(const p4::config::v1::P4Info &p4Info) {
}
}

std::optional<uint64_t> P4InfoMaps::lookupP4RuntimeId(cstring controlPlaneName) const {
std::optional<uint64_t> P4InfoMaps::lookUpP4RuntimeId(cstring controlPlaneName) const {
auto it = nameToIdMap.find(controlPlaneName);
if (it == nameToIdMap.end()) {
return std::nullopt;
}
return it->second;
}

std::optional<cstring> P4InfoMaps::lookupControlPlaneName(uint64_t id) const {
std::optional<cstring> P4InfoMaps::lookUpControlPlaneName(uint64_t id) const {
auto it = idToNameMap.find(id);
if (it == idToNameMap.end()) {
return std::nullopt;
Expand Down
13 changes: 6 additions & 7 deletions backends/p4tools/common/control_plane/p4info_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ 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.
/// Since ids for action parameters and table keys are not unique, we use a pairing function to
/// compute a unique identifier. This pairing function uses the id of the parent object (e.g., a
/// table or action) and combines it with the id of the parameter or key element to create a unique
/// identifier.
class P4InfoMaps {
/// Type definitions for convenience.
using P4RuntimeIdToControlPlaneNameMap = std::map<uint64_t, cstring>;
Expand All @@ -47,19 +51,14 @@ class P4InfoMaps {

public:
explicit P4InfoMaps(const p4::config::v1::P4Info &p4Info);
P4InfoMaps(const P4InfoMaps &) = default;
P4InfoMaps(P4InfoMaps &&) = default;
P4InfoMaps &operator=(const P4InfoMaps &) = default;
P4InfoMaps &operator=(P4InfoMaps &&) = default;
virtual ~P4InfoMaps() = default;

/// 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;
[[nodiscard]] std::optional<uint64_t> lookUpP4RuntimeId(cstring controlPlaneName) const;

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

} // namespace P4::ControlPlaneAPI
Expand Down
58 changes: 29 additions & 29 deletions control-plane/p4infoApi.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
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.
/// and @returns the P4Runtime representation, or nullptr if none is found.
/// TODO: Should this return an optional const reference? The advantage is that the value needs to
/// be explicitly unpacked.
template <typename It>
Expand All @@ -32,7 +32,7 @@ auto findP4InfoObject(const It &first, const It &last, cstring controlPlaneName)
}

/// Generic meta function which searches an object by @id in the given range
/// and @returns the P4Runtime representation, or std::nullopt if none is found.
/// and @returns the P4Runtime representation, or nullptr if none is found.
/// TODO: Should this return an optional const reference? The advantage is that the value needs to
/// be explicitly unpacked.
template <typename It>
Expand All @@ -47,135 +47,135 @@ auto findP4InfoObject(const It &first, const It &last, p4rt_id_t id) -> const
return &*desiredObject;
}

/// Try to find the P4Info description for the given table. @return std::nullopt if the table is not
/// Try to find the P4Info description for the given table. @return nullptr if the table is not
/// is not present in the P4Info object.
const p4::config::v1::Table *findP4RuntimeTable(const p4::config::v1::P4Info &p4Info,
cstring controlPlaneName);

/// Try to find the P4Info description for the given table. @return std::nullopt if the table is not
/// Try to find the P4Info description for the given table. @return nullptr if the table is not
/// is not present in the P4Info object.
const p4::config::v1::Table *findP4RuntimeTable(const p4::config::v1::P4Info &p4Info, p4rt_id_t id);

/// Try to find the P4Info description for the given match field in a particular P4Info table.
/// @return std::nullopt if the match field is not present in the table.
/// @return nullptr if the match field is not present in the table.
const p4::config::v1::MatchField *findP4RuntimeMatchField(const p4::config::v1::Table &p4Table,
cstring controlPlaneName);

/// Try to find the P4Info description for the given match field in a particular P4Info table.
/// @return std::nullopt if the match field is not present in the table.
/// @return nullptr if the match field is not present in the table.
const p4::config::v1::MatchField *findP4RuntimeMatchField(const p4::config::v1::Table &p4Table,
p4rt_id_t id);

/// Try to find the P4Info description for the given action. @return std::nullopt if the action
/// Try to find the P4Info description for the given action. @return nullptr if the action
/// is not present in the P4Info object.
const p4::config::v1::Action *findP4RuntimeAction(const p4::config::v1::P4Info &p4Info,
cstring controlPlaneName);

/// Try to find the P4Info description for the given action. @return std::nullopt if the action
/// Try to find the P4Info description for the given action. @return nullptr if the action
/// is not present in the P4Info object.
const p4::config::v1::Action *findP4RuntimeAction(const p4::config::v1::P4Info &p4Info,
p4rt_id_t id);

/// Try to find the P4Info description for the given action profile. @return std::nullopt if the
/// Try to find the P4Info description for the given action profile. @return nullptr if the
/// action is not present in the P4Info object.
const p4::config::v1::ActionProfile *findP4RuntimeActionProfile(
const p4::config::v1::P4Info &p4Info, cstring controlPlaneName);

/// Try to find the P4Info description for the given action profile. @return std::nullopt if the
/// Try to find the P4Info description for the given action profile. @return nullptr if the
/// action is not present in the P4Info object.
const p4::config::v1::ActionProfile *findP4RuntimeActionProfile(
const p4::config::v1::P4Info &p4Info, p4rt_id_t id);

/// Try to find the P4Info description for the given counter. @return std::nullopt if the counter is
/// Try to find the P4Info description for the given counter. @return nullptr if the counter is
/// not present in the P4Info object.
const p4::config::v1::Counter *findP4RuntimeCounter(const p4::config::v1::P4Info &p4Info,
cstring controlPlaneName);

/// Try to find the P4Info description for the given counter. @return std::nullopt if the counter is
/// Try to find the P4Info description for the given counter. @return nullptr if the counter is
/// not present in the P4Info object.
const p4::config::v1::Counter *findP4RuntimeCounter(const p4::config::v1::P4Info &p4Info,
p4rt_id_t id);

/// Try to find the P4Info description for the given direct counter. @return std::nullopt if the
/// Try to find the P4Info description for the given direct counter. @return nullptr if the
/// direct counter is not present in the P4Info object.
const p4::config::v1::DirectCounter *findP4RuntimeDirectCounter(
const p4::config::v1::P4Info &p4Info, cstring controlPlaneName);

/// Try to find the P4Info description for the given direct counter. @return std::nullopt if the
/// Try to find the P4Info description for the given direct counter. @return nullptr if the
/// direct counter is not present in the P4Info object.
const p4::config::v1::DirectCounter *findP4RuntimeDirectCounter(
const p4::config::v1::P4Info &p4Info, p4rt_id_t id);

/// Try to find the P4Info description for the given meter. @return std::nullopt if the meter is not
/// Try to find the P4Info description for the given meter. @return nullptr if the meter is not
/// present in the P4Info object.
const p4::config::v1::Meter *findP4RuntimeMeter(const p4::config::v1::P4Info &p4Info,
cstring controlPlaneName);

/// Try to find the P4Info description for the given meter. @return std::nullopt if the meter is not
/// Try to find the P4Info description for the given meter. @return nullptr if the meter is not
/// present in the P4Info object.
const p4::config::v1::Meter *findP4RuntimeMeter(const p4::config::v1::P4Info &p4Info, p4rt_id_t id);

/// Try to find the P4Info description for the given direct meter. @return std::nullopt if the
/// Try to find the P4Info description for the given direct meter. @return nullptr if the
/// direct meter is not present in the P4Info object.
const p4::config::v1::DirectMeter *findP4RuntimeDirectMeter(const p4::config::v1::P4Info &p4Info,
cstring controlPlaneName);

/// Try to find the P4Info description for the given direct meter. @return std::nullopt if the
/// Try to find the P4Info description for the given direct meter. @return nullptr if the
/// direct meter is not present in the P4Info object.
const p4::config::v1::DirectMeter *findP4RuntimeDirectMeter(const p4::config::v1::P4Info &p4Info,
p4rt_id_t id);

/// Try to find the P4Info description for the given controller packet metadata. @return
/// std::nullopt if the controller packet metadata is not present in the P4Info object.
/// nullptr if the controller packet metadata is not present in the P4Info object.
const p4::config::v1::ControllerPacketMetadata *findP4RuntimeControllerPacketMetadata(
const p4::config::v1::P4Info &p4Info, cstring controlPlaneName);

/// Try to find the P4Info description for the given controller packet metadata. @return
/// std::nullopt if the controller packet metadata is not present in the P4Info object.
/// nullptr if the controller packet metadata is not present in the P4Info object.
const p4::config::v1::ControllerPacketMetadata *findP4RuntimeControllerPacketMetadata(
const p4::config::v1::P4Info &p4Info, p4rt_id_t id);

/// Try to find the P4Info description for the given value set. @return std::nullopt if the value
/// Try to find the P4Info description for the given value set. @return nullptr if the value
/// set is not present in the P4Info object.
const p4::config::v1::ValueSet *findP4RuntimeValueSet(const p4::config::v1::P4Info &p4Info,
cstring controlPlaneName);

/// Try to find the P4Info description for the given value set. @return std::nullopt if the value
/// Try to find the P4Info description for the given value set. @return nullptr if the value
/// set is not present in the P4Info object.
const p4::config::v1::ValueSet *findP4RuntimeValueSet(const p4::config::v1::P4Info &p4Info,
p4rt_id_t id);

/// Try to find the P4Info description for the given register. @return std::nullopt if the register
/// Try to find the P4Info description for the given register. @return nullptr if the register
/// is not present in the P4Info object.
const p4::config::v1::Register *findP4RuntimeRegister(const p4::config::v1::P4Info &p4Info,
cstring controlPlaneName);

/// Try to find the P4Info description for the given register. @return std::nullopt if the register
/// Try to find the P4Info description for the given register. @return nullptr if the register
/// is not present in the P4Info object.
const p4::config::v1::Register *findP4RuntimeRegister(const p4::config::v1::P4Info &p4Info,
p4rt_id_t id);

/// Try to find the P4Info description for the given digest. @return std::nullopt if the digest is
/// Try to find the P4Info description for the given digest. @return nullptr if the digest is
/// not present in the P4Info object.
const p4::config::v1::Digest *findP4RuntimeDigest(const p4::config::v1::P4Info &p4Info,
cstring controlPlaneName);

/// Try to find the P4Info description for the given digest. @return std::nullopt if the digest is
/// Try to find the P4Info description for the given digest. @return nullptr if the digest is
/// not present in the P4Info object.
const p4::config::v1::Digest *findP4RuntimeDigest(const p4::config::v1::P4Info &p4Info,
p4rt_id_t id);

/// Try to find the P4Info description for the given extern. @return std::nullopt if the extern is
/// Try to find the P4Info description for the given extern. @return nullptr if the extern is
/// not present in the P4Info object.
const p4::config::v1::Extern *findP4RuntimeExtern(const p4::config::v1::P4Info &p4Info,
cstring controlPlaneName);

/// Try to find the P4Info description for the given extern. @return std::nullopt if the extern is
/// Try to find the P4Info description for the given extern. @return nullptr if the extern is
/// not present in the P4Info object.
const p4::config::v1::Extern *findP4RuntimeExtern(const p4::config::v1::P4Info &p4Info,
p4rt_id_t id);

/// Looks up the P4Runtime id for a P4Runtime symbol. Returns std::nullopt if the object or the
/// Looks up the P4Runtime id for a P4Runtime symbol. Returns nullptr if the object or the
/// type does not exist.
std::optional<p4rt_id_t> getP4RuntimeId(const p4::config::v1::P4Info &p4Info,
const P4RuntimeSymbolType &type, cstring controlPlaneName);
Expand Down

0 comments on commit cbc581b

Please sign in to comment.