Skip to content

Commit

Permalink
Refactor APIs create/remove
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmyzhai committed Dec 19, 2024
1 parent 5149bf6 commit d7f18a9
Show file tree
Hide file tree
Showing 6 changed files with 173 additions and 254 deletions.
138 changes: 137 additions & 1 deletion dash-pipeline/SAI/src/dashsai.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -640,10 +640,12 @@ grpc::StatusCode DashSai::readTableEntry(
entity = rep.mutable_entities(0);
entry->CopyFrom(entity->table_entry());
}
else {
entity->release_table_entry();
}

auto status = client_reader->Finish();

exit:
if (status.ok()) {
DASH_LOG_NOTICE("GRPC call Read OK %s", entry->ShortDebugString().c_str());
}
Expand Down Expand Up @@ -1043,6 +1045,140 @@ sai_status_t DashSai::bulk_remove_objects(
return agg_status;
}

sai_status_t DashSai::create(
_In_ const P4MetaTable &meta_table,
_In_ sai_object_type_t objectType,
_Out_ sai_object_id_t* objectId,
_In_ sai_object_id_t switchId,
_In_ uint32_t attr_count,
_In_ const sai_attribute_t *attr_list)
{
DASH_LOG_ENTER();
DASH_CHECK_API_INITIALIZED();

auto attrs = DashSai::populateDefaultAttributes(objectType, attr_count, attr_list);
attr_count = (uint32_t)attrs.size();
attr_list = attrs.data();

std::shared_ptr<p4::v1::TableEntry> matchActionEntry;
matchActionEntry = std::make_shared<p4::v1::TableEntry>();
matchActionEntry->set_table_id(meta_table.id);

sai_object_id_t objId = getNextObjectId(objectType);
if (objId == SAI_NULL_OBJECT_ID)
{
DASH_LOG_ERROR("getNextObjectId failed for OBJECT_TYPE %u", objectType);
return SAI_STATUS_FAILURE;
}

auto meta_object_key = meta_table.get_meta_object_key();
assert(meta_object_key);
auto key_mf = matchActionEntry->add_match();
auto key_mf_exact = key_mf->mutable_exact();
key_mf->set_field_id(meta_object_key->id);
u16SetVal((uint16_t)objId, key_mf_exact, 16);

pi_p4_id_t action_id = meta_table.find_action_id(attr_count, attr_list);
if (!action_id) {
DASH_LOG_ERROR("Not find p4 table action");
return SAI_STATUS_FAILURE;
}

auto action = matchActionEntry->mutable_action()->mutable_action();
action->set_action_id(action_id);

for (uint32_t i = 0; i < attr_count; i++) {
if (auto meta_param = meta_table.get_meta_action_param(action_id, attr_list[i].id)) {
// attr in table action params
set_attr_to_p4_action(meta_param, &attr_list[i], action);
}
else if (auto meta_key = meta_table.get_meta_key(attr_list[i].id)) {
// attr in table keys
set_attr_to_p4_match(meta_key, &attr_list[i], matchActionEntry);
}
else {
// FIXME: check extra fields
}
}

if (insertInTable(matchActionEntry, objId)) {
*objectId = objId;
return SAI_STATUS_SUCCESS;
}

return SAI_STATUS_FAILURE;
}

sai_status_t DashSai::create(
_In_ const P4MetaTable &meta_table,
_In_ sai_object_type_t objectType,
_Inout_ std::shared_ptr<p4::v1::TableEntry> matchActionEntry,
_In_ uint32_t attr_count,
_In_ const sai_attribute_t *attr_list)
{
DASH_LOG_ENTER();
DASH_CHECK_API_INITIALIZED();

auto attrs = DashSai::populateDefaultAttributes(objectType, attr_count, attr_list);
attr_count = (uint32_t)attrs.size();
attr_list = attrs.data();

matchActionEntry->set_table_id(meta_table.id);

pi_p4_id_t action_id = meta_table.find_action_id(attr_count, attr_list);
if (!action_id) {
DASH_LOG_ERROR("Not find p4 table action");
return SAI_STATUS_FAILURE;
}
auto action = matchActionEntry->mutable_action()->mutable_action();
action->set_action_id(action_id);

for (uint32_t i = 0; i < attr_count; i++) {
if (auto meta_param = meta_table.get_meta_action_param(action_id, attr_list[i].id)) {
// attr in table action params
set_attr_to_p4_action(meta_param, &attr_list[i], action);
}
else {
// FIXME: check extra fields
}
}

auto ret = mutateTableEntry(matchActionEntry, p4::v1::Update_Type_INSERT);
if (grpc::StatusCode::OK == ret) {
return SAI_STATUS_SUCCESS;
}

return SAI_STATUS_FAILURE;
}

sai_status_t DashSai::remove(
_In_ sai_object_id_t objectId)
{
DASH_LOG_ENTER();
DASH_CHECK_API_INITIALIZED();

if (removeFromTable(objectId)) {
return SAI_STATUS_SUCCESS;
}

return SAI_STATUS_FAILURE;
}

sai_status_t DashSai::remove(
_Inout_ std::shared_ptr<p4::v1::TableEntry> matchActionEntry)
{
DASH_LOG_ENTER();
DASH_CHECK_API_INITIALIZED();

auto ret = mutateTableEntry(matchActionEntry, p4::v1::Update_Type_DELETE);

if (grpc::StatusCode::OK == ret) {
return SAI_STATUS_SUCCESS;
}

return SAI_STATUS_FAILURE;
}

sai_status_t DashSai::set(
_In_ const P4MetaTable &meta_table,
_In_ sai_object_id_t objectId,
Expand Down
21 changes: 21 additions & 0 deletions dash-pipeline/SAI/src/dashsai.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,27 @@ namespace dash
_Inout_ sai_attribute_t *attr_list);

// QUAD api implementation, using p4 meta table
sai_status_t create(
_In_ const P4MetaTable &meta_table,
_In_ sai_object_type_t objectType,
_Out_ sai_object_id_t* objectId,
_In_ sai_object_id_t switchId,
_In_ uint32_t attr_count,
_In_ const sai_attribute_t *attr_list);

sai_status_t create(
_In_ const P4MetaTable &meta_table,
_In_ sai_object_type_t objectType,
_Inout_ std::shared_ptr<p4::v1::TableEntry> matchActionEntry,
_In_ uint32_t attr_count,
_In_ const sai_attribute_t *attr_list);

sai_status_t remove(
_In_ sai_object_id_t objectId);

sai_status_t remove(
_Inout_ std::shared_ptr<p4::v1::TableEntry> matchActionEntry);

sai_status_t set(
_In_ const P4MetaTable &meta_table,
_In_ sai_object_id_t objectId,
Expand Down
6 changes: 6 additions & 0 deletions dash-pipeline/SAI/src/p4meta.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ namespace dash
return u8SetVal(value, p4_key_or_param, bitwidth);
if (field == "u16")
return u16SetVal(value, p4_key_or_param, bitwidth);
if (field == "s32")
return s32SetVal(value, p4_key_or_param, bitwidth);
if (field == "u32")
return u32SetVal(value, p4_key_or_param, bitwidth);
if (field == "u64")
Expand Down Expand Up @@ -121,6 +123,10 @@ namespace dash
uint16_t val = *(const uint16_t*)v;
value.u16 = ntohs(val);
}
else if (field == "s32") {
int32_t val = *(const int32_t*)v;
value.s32 = ntohl(val) >> (32 - bitwidth);
}
else if (field == "u32") {
uint32_t val = *(const uint32_t*)v;
value.u32 = ntohl(val) >> (32 - bitwidth);
Expand Down
73 changes: 0 additions & 73 deletions dash-pipeline/SAI/templates/impls/p4_table_action.cpp.j2

This file was deleted.

66 changes: 0 additions & 66 deletions dash-pipeline/SAI/templates/impls/p4_table_object_match.cpp.j2

This file was deleted.

Loading

0 comments on commit d7f18a9

Please sign in to comment.