-
Notifications
You must be signed in to change notification settings - Fork 330
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Basic PNA externs: Meter, Counter, Hash, etc (#1263)
- Loading branch information
1 parent
1746237
commit 11c789b
Showing
11 changed files
with
744 additions
and
1 deletion.
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
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.