Skip to content

Commit

Permalink
init default value
Browse files Browse the repository at this point in the history
Signed-off-by: Ze Gan <[email protected]>
  • Loading branch information
Pterosaur committed Jul 12, 2024
1 parent 9220af1 commit 7ecb1e7
Show file tree
Hide file tree
Showing 7 changed files with 169 additions and 0 deletions.
25 changes: 25 additions & 0 deletions dash-pipeline/SAI/src/default_value.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include "default_value.h"

#include <map>

std::vector<sai_attribute_t> merge_default_values(
const std::vector<sai_attribute_t>& attrs,
const std::vector<sai_attribute_t>& default_values) {

std::vector<sai_attribute_t> merged_attrs;
std::map<sai_attr_id_t, sai_attribute_t> attrs_map;

for (const auto &attr : default_values) {
attrs_map[attr.id] = attr;
}

for (const auto &attr : attrs) {
attrs_map[attr.id] = attr;
}

for (const auto &pair : attrs_map) {
merged_attrs.push_back(pair.second);
}

return merged_attrs;
}
13 changes: 13 additions & 0 deletions dash-pipeline/SAI/src/default_value.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#pragma once

#include <vector>

extern "C"
{
#include "sai.h"
}


std::vector<sai_attribute_t> merge_default_values(
const std::vector<sai_attribute_t>& attrs,
const std::vector<sai_attribute_t>& default_values);
77 changes: 77 additions & 0 deletions dash-pipeline/SAI/templates/imp/default_value.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Set default value block
{
{% for sai_attr in table.sai_attributes %}

/**
* Set default value for SAI_{{ table.name | upper }}_ATTR_{{ sai_attr.name | upper }}
*/
do {

// attribute already exists on current attribute list
if (std::find_if(attrs.begin(), attrs.end(), [](const sai_attribute_t &attr) {
return attr.id == SAI_{{ table.name | upper }}_ATTR_{{ sai_attr.name | upper }};
}) != attrs.end())
{
break;
}

// conditional attributes should be populated by user

{% if sai_attr.isreadonly == 'true' %}
// can't be read only
break;
{% endif %}

// can't be mandatory on create

// there is no default value

// is valid only
{% if table.actions | length > 1 %}
{% if sai_attr.param_actions | length == 0 %}
// always set default attribute in this case, even if condition is not set
// (see github discussion https://github.com/sonic-net/DASH/pull/547)
{% else %}
bool match_action = false;
auto action_itr = find_if(attrs.begin(), attrs.end(), [](const sai_attribute_t &attr) {
return attr.id == SAI_{{ table.name | upper }}_ATTR_ACTION;
});
sai_int32_t action_value = SAI_{{ table.name | upper }}_ACTION_{{ table.actions[0].name | upper }};
if (action_itr != attrs.end())
{
action_value = action_itr->value.s32;
}
{% for action in sai_attr.param_actions %}
match_action |= action_value == SAI_{{ table.name | upper }}_ACTION_{{ action | upper }};
{% endfor %}
if (!match_action)
{
break;
}
{% endif %}
{% endif %}

// set default value
{% if sai_attr.default is defined %}
sai_attribute_t a = {};
a.id = SAI_{{ table.name | upper }}_ATTR_{{ sai_attr.name | upper }};
{% if sai_attr.default and sai_attr.default != "empty" %}
{% if sai_attr.type == "sai_ip_address_t" %}
sai_deserialize_ip_address("{{ sai_attr.default }}", &a.value.ipaddr);
{% elif sai_attr.type == "sai_uint32_t" %}
a.value.u32 = {{ sai_attr.default }};
{% elif sai_attr.default == "vendor" %}
// Hardcode vendor to zero
{% else %}
a.value.s32 = {{ sai_attr.default }};
{% endif %}
{% endif %}
DASH_LOG_NOTICE("adding SAI_{{ table.name | upper }}_ATTR_{{ sai_attr.name | upper }} with default value");
attrs.push_back(a);
break;
{% endif %}

} while(0);

{% endfor %}
}
21 changes: 21 additions & 0 deletions dash-pipeline/SAI/templates/imp/default_value_define.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

// SAI_{{ table.name | upper }}_ACTION_{{ action.name | upper }}

static std::vector<sai_attribute_t> set_default_{{ table.name }}_{{ action.name }}_attributes() {
std::vector<sai_attribute_t> attrs;

{% for sai_attr in table.sai_attributes %}
{% if sai_attr.param_actions | length == 0 or action.name in sai_attr.param_actions %}
{
{% include 'templates/imp/default_value_set.j2' %}
}
{% endif %}
{% endfor %}

return attrs;
}

static const std::vector<sai_attribute_t>& get_default_{{ table.name }}_{{ action.name }}_attributes() {
static const std::vector<sai_attribute_t> attrs = set_default_{{ table.name }}_{{ action.name }}_attributes();
return attrs;
}
4 changes: 4 additions & 0 deletions dash-pipeline/SAI/templates/imp/default_value_merge.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{

attrs = merge_default_values(attrs, get_default_{{ table.name }}_{{ action.name }}_attributes());
}
18 changes: 18 additions & 0 deletions dash-pipeline/SAI/templates/imp/default_value_set.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// set default value
{% if sai_attr.default is defined %}
sai_attribute_t a = {};
a.id = SAI_{{ table.name | upper }}_ATTR_{{ sai_attr.name | upper }};
{% if sai_attr.default and sai_attr.default != "empty" %}
{% if sai_attr.type == "sai_ip_address_t" %}
sai_deserialize_ip_address("{{ sai_attr.default }}", &a.value.ipaddr);
{% elif sai_attr.type == "sai_uint32_t" %}
a.value.u32 = {{ sai_attr.default }};
{% elif sai_attr.default == "vendor" %}
// Hardcode vendor to zero
{% else %}
a.value.s32 = {{ sai_attr.default }};
{% endif %}
{% endif %}
DASH_LOG_NOTICE("adding SAI_{{ table.name | upper }}_ATTR_{{ sai_attr.name | upper }} with default value");
attrs.push_back(a);
{% endif %}
11 changes: 11 additions & 0 deletions dash-pipeline/SAI/templates/saiapi.cpp.j2
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
#include "utils.h"
#include "saiimpl.h"
#include "default_value.h"

extern "C" {
#include "saimetadata.h"
}

using namespace dash::utils;

{% for table in tables %}
{% for action in table.actions %}
{% include 'templates/imp/default_value_define.j2' %}
{% endfor %}
{% endfor %}

{% set registered_group = [] %}
{% for table in tables %}
{% if table.name in registered_group %}{% continue %}{% endif %}
Expand Down Expand Up @@ -162,6 +169,7 @@ static sai_status_t dash_sai_create_{{ table.name }}(
{% for action in table.actions %}
actionId = {{action.id}}; // SAI_{{ table.name | upper }}_ACTION_{{ action.name | upper }}
//expectedParams = {{ action.params|length }};
{% include 'templates/imp/default_value_merge.j2' %}
{% endfor %}
{% else %}
// Search the action
Expand All @@ -176,6 +184,7 @@ static sai_status_t dash_sai_create_{{ table.name }}(
{
actionId = {{action.id}};
//expectedParams = {{ action.params|length }};
{% include 'templates/imp/default_value_merge.j2' %}
break;
}
{% endfor %}
Expand Down Expand Up @@ -462,6 +471,7 @@ static sai_status_t dash_sai_create_{{ table.name }}(
{% for action in table.actions %}
actionId = {{action.id}}; // SAI_{{ table.name | upper }}_ACTION_{{ action.name | upper }}
//expectedParams = {{ action.params|length }};
{% include 'templates/imp/default_value_merge.j2' %}
{% endfor %}
{% else %}
// Search the action
Expand All @@ -476,6 +486,7 @@ static sai_status_t dash_sai_create_{{ table.name }}(
{
actionId = {{action.id}};
//expectedParams = {{ action.params|length }};
{% include 'templates/imp/default_value_merge.j2' %}
break;
}
{% endfor %}
Expand Down

0 comments on commit 7ecb1e7

Please sign in to comment.