-
Notifications
You must be signed in to change notification settings - Fork 330
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
Added Basic externs for PNA: Meter, Counter, Hash, etc #1261
Closed
rupesh-chiluka-marvell
wants to merge
3
commits into
p4lang:main
from
rupesh-chiluka-marvell:pna_dev_v1
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* Copyright 2024 Marvell Technology, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
/* | ||
* Rupesh Chiluka ([email protected]) | ||
* | ||
*/ | ||
|
||
|
||
#include "pna_counter.h" | ||
|
||
namespace bm { | ||
|
||
namespace pna { | ||
|
||
void | ||
PNA_Counter::count(const Data &index) { | ||
_counter->get_counter( | ||
index.get<size_t>()).increment_counter(get_packet()); | ||
} | ||
|
||
Counter & | ||
PNA_Counter::get_counter(size_t idx) { | ||
return _counter->get_counter(idx); | ||
} | ||
|
||
const Counter & | ||
PNA_Counter::get_counter(size_t idx) const { | ||
return _counter->get_counter(idx); | ||
} | ||
|
||
Counter::CounterErrorCode | ||
PNA_Counter::reset_counters(){ | ||
return _counter->reset_counters(); | ||
} | ||
|
||
BM_REGISTER_EXTERN_W_NAME(Counter, PNA_Counter); | ||
BM_REGISTER_EXTERN_W_NAME_METHOD(Counter, PNA_Counter, count, const Data &); | ||
|
||
} // namespace bm::pna | ||
|
||
} // namespace bm | ||
|
||
int import_counters(){ | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* Copyright 2024 Marvell Technology, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
/* | ||
* Rupesh Chiluka ([email protected]) | ||
* | ||
*/ | ||
|
||
|
||
#ifndef PNA_NIC_PNA_COUNTER_H_ | ||
#define PNA_NIC_PNA_COUNTER_H_ | ||
|
||
#include <bm/bm_sim/extern.h> | ||
#include <bm/bm_sim/counters.h> | ||
|
||
namespace bm { | ||
|
||
namespace pna { | ||
|
||
class PNA_Counter : public bm::ExternType { | ||
public: | ||
static constexpr p4object_id_t spec_id = 0xffffffff; | ||
|
||
BM_EXTERN_ATTRIBUTES { | ||
BM_EXTERN_ATTRIBUTE_ADD(n_counters); | ||
BM_EXTERN_ATTRIBUTE_ADD(type); | ||
} | ||
|
||
void init() override { | ||
_counter = std::unique_ptr<CounterArray>( | ||
new CounterArray(get_name() + ".$impl", | ||
spec_id, | ||
n_counters.get<size_t>())); | ||
} | ||
|
||
void count(const Data &index); | ||
|
||
Counter &get_counter(size_t idx); | ||
|
||
const Counter &get_counter(size_t idx) const; | ||
|
||
Counter::CounterErrorCode reset_counters(); | ||
|
||
size_t size() const { return _counter->size(); }; | ||
|
||
private: | ||
Data n_counters; | ||
Data type; | ||
std::unique_ptr<CounterArray> _counter; | ||
}; | ||
|
||
} // namespace bm::pna | ||
|
||
} // namespace bm | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* Copyright 2024 Marvell Technology, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
/* | ||
* Rupesh Chiluka ([email protected]) | ||
* | ||
*/ | ||
|
||
#include "pna_hash.h" | ||
|
||
namespace { | ||
|
||
bm::ByteContainer | ||
build_buffer(const std::vector<bm::Field> &fields) { | ||
int nbits = 0; | ||
int nbytes; | ||
for (const auto &field : fields) { | ||
nbits += field.get_nbits(); | ||
} | ||
nbytes = (nbits + 7) / 8; | ||
bm::ByteContainer buf(nbytes, '\x00'); | ||
nbits = (nbytes * 8 - nbits); // pad to the left with 0s | ||
for (const auto &field : fields) { | ||
char *ptr = buf.data() + (nbits / 8); | ||
field.deparse(ptr, nbits % 8); | ||
nbits += field.get_nbits(); | ||
} | ||
return buf; | ||
} | ||
|
||
} // namespace | ||
|
||
namespace bm { | ||
|
||
namespace pna { | ||
|
||
void | ||
PNA_Hash::init() { | ||
calc = CalculationsMap::get_instance()->get_copy(algo); | ||
} | ||
|
||
void | ||
PNA_Hash::get_hash(Field &dst, const std::vector<Field> &fields) { | ||
auto buf = build_buffer(fields); | ||
auto hash = compute(buf.data(), buf.size()); | ||
dst.set(hash); | ||
} | ||
|
||
void | ||
PNA_Hash::get_hash_mod(Field &dst, const Data &base, const std::vector<Field> &fields, const Data &max) { | ||
auto buf = build_buffer(fields); | ||
auto hash = compute(buf.data(), buf.size()); | ||
auto result = base.get<uint64_t>() + (hash % max.get<uint64_t>()); | ||
dst.set(result); | ||
} | ||
|
||
uint64_t | ||
PNA_Hash::compute(const char *buf, size_t s) { | ||
return calc.get()->output(buf, s); | ||
} | ||
|
||
BM_REGISTER_EXTERN_W_NAME(Hash, PNA_Hash); | ||
BM_REGISTER_EXTERN_W_NAME_METHOD(Hash, PNA_Hash, get_hash, Field &, const std::vector<Field>); | ||
BM_REGISTER_EXTERN_W_NAME_METHOD(Hash, PNA_Hash, get_hash_mod, Field &, const Data &, const std::vector<Field>, const Data &); | ||
|
||
} // namespace bm::pna | ||
|
||
} // namespace bm | ||
|
||
int import_hash() { | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* Copyright 2024 Marvell Technology, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
/* | ||
* Rupesh Chiluka ([email protected]) | ||
* | ||
*/ | ||
|
||
|
||
#ifndef PNA_NIC_PNA_HASH_H_ | ||
#define PNA_NIC_PNA_HASH_H_ | ||
|
||
#include <bm/bm_sim/extern.h> | ||
#include <bm/bm_sim/calculations.h> | ||
|
||
namespace bm { | ||
|
||
namespace pna { | ||
|
||
class PNA_Hash : public bm::ExternType { | ||
public: | ||
|
||
BM_EXTERN_ATTRIBUTES { | ||
BM_EXTERN_ATTRIBUTE_ADD(algo); | ||
} | ||
|
||
void init() override; | ||
|
||
void get_hash(Field &dst, const std::vector<Field> &fields); | ||
|
||
void get_hash_mod(Field &dst, const Data &base, const std::vector<Field> &fields, const Data &max); | ||
|
||
uint64_t compute(const char *buffer, size_t s); | ||
|
||
private: | ||
std::string algo; | ||
std::unique_ptr<bm::CalculationsMap::MyC> calc; | ||
|
||
}; | ||
|
||
} // namespace bm::pna | ||
|
||
} // namespace bm | ||
#endif |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: format to align indentation?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Aligned it.
tabs are used for indentation in configuration files. Everywhere else, spaces are used.