Skip to content
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
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,7 @@ AC_CONFIG_FILES([Makefile
targets/psa_switch/Makefile
targets/psa_switch/tests/Makefile
targets/pna_nic/Makefile
targets/pna_nic/tests/Makefile
Copy link
Member

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?

Copy link
Contributor Author

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.

tests/Makefile
tests/stress_tests/Makefile
tools/Makefile
Expand Down
13 changes: 12 additions & 1 deletion targets/pna_nic/Makefile.am
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
if COND_NANOMSG
MAYBE_TESTS = tests
endif

SUBDIRS = . $(MAYBE_TESTS)

THRIFT_IDL = $(srcdir)/thrift/pna_nic.thrift

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