Skip to content

Commit

Permalink
Add Basic PNA externs: Meter, Counter, Hash, etc (#1263)
Browse files Browse the repository at this point in the history
  • Loading branch information
rupesh-chiluka-marvell authored Aug 7, 2024
1 parent 1746237 commit 11c789b
Show file tree
Hide file tree
Showing 11 changed files with 744 additions and 1 deletion.
7 changes: 6 additions & 1 deletion targets/pna_nic/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 10,12 @@ noinst_LTLIBRARIES = libpnanic.la

libpnanic_la_SOURCES = \
pna_nic.cpp pna_nic.h \
primitives.cpp
primitives.cpp \
externs/pna_counter.h externs/pna_counter.cpp \
externs/pna_meter.h externs/pna_meter.cpp \
externs/pna_random.h externs/pna_random.cpp \
externs/pna_internet_checksum.h externs/pna_internet_checksum.cpp \
externs/pna_hash.h externs/pna_hash.cpp

libpnanic_la_LIBADD = \
$(top_builddir)/src/bm_sim/libbmsim.la \
Expand Down
58 changes: 58 additions & 0 deletions targets/pna_nic/externs/pna_counter.cpp
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;
}
68 changes: 68 additions & 0 deletions targets/pna_nic/externs/pna_counter.h
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
84 changes: 84 additions & 0 deletions targets/pna_nic/externs/pna_hash.cpp
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;
}
56 changes: 56 additions & 0 deletions targets/pna_nic/externs/pna_hash.h
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
Loading

0 comments on commit 11c789b

Please sign in to comment.