Comments (4)
You can define your own extern function or object, and implement this functionality in C++. There is an example here: https://github.com/p4lang/behavioral-model/tree/main/examples/custom_extern
from behavioral-model.
This example just show that how to manipulate basic data. If I want to do som operation in p4 register, how do I declaremy extern
p4 function define my c++ function
from behavioral-model.
From a bmv2 standpoint, you could write the following extern function:
void register_find(bm::Data &index, const bm::RegisterArray &ra, const bm::Data &v) {
// look for value "v" in "ra", and set "index" appropriately
}
BM_REGISTER_EXTERN_FUNCTION(register_find, bm::Data &, const bm::RegisterArray &, const bm::Data &);
I am not sure what the P4 declaration would look like, and I am not sure if there is full compiler support (p4c with bmv2 backend) for this. But it is worth a try. Maybe something like this:
extern void register_find<T>(inout bit<32> index, in register<T> ra, in T v);
If this doesn't work, you will need to define your own register extern type (instead of using the standard v1model one), with the extra method that you need. This should be a good example: https://github.com/p4lang/behavioral-model/blob/main/targets/psa_switch/externs/psa_counter.h. You will need to do a similar thing, but with RegisterArray
instead of CounterArray
. An important thing to keep in mind is that bmv2 only supports registers which store binary strings (bit<X>
/ int<X>
in P4). It doesn't support registers with an arbitrary value type (e.g. struct).
from behavioral-model.
Thanks for your reply. I've tried the first method, but the p4c compiler throws an error like this:
p4c-bm2-ss --emit-externs -o example.json example.p4
extern_lib/declaration.p4(19): [--Werror=type-error] error: ra: a parameter with type register cannot have a direction
extern void register_find<T>(inout bit<32> index, in register<T> ra, in T v);
after I change the declaration of register_find
to extern void register_find<T>(inout bit<32> index, register<T> ra, in T v);
. it throw an error example.p4(71): [--Werror=target-error] error: testRegister: Could not generate code for expression register_find(find_index, testRegister, 32w56);
. This is most likely p4c does not support this;
But https://github.com/p4lang/behavioral-model/blob/main/targets/psa_switch/externs/psa_counter.h this example gives me a lot of help. I can define my own register and extend its function like this:
class MyRegister : public bm::ExternType{
public:
static constexpr bm::p4object_id_t spec_id = 0xffffffff;
BM_EXTERN_ATTRIBUTES{
BM_EXTERN_ATTRIBUTE_ADD(num);
BM_EXTERN_ATTRIBUTE_ADD(width);
}
void init() override {
_array = std::unique_ptr<bm::RegisterArray>(
new bm::RegisterArray(get_name() + ".$impl",
spec_id,
num.get<size_t>(), width.get_int()));
}
void read(const bm::Data &index, bm::Data &value){
// read array[index] to valeue
}
void write(const bm::Data &index, const bm::Data &value){
// write value to array[index]
}
void find_index(const bm::Data &value, bm::Data &index){
// find the index of value in array
}
private:
bm::Data width;
bm::Data num;
std::unique_ptr<bm::RegisterArray> _array;
};
BM_REGISTER_EXTERN(MyRegister);
BM_REGISTER_EXTERN_METHOD(MyRegister, read, const bm::Data &, bm::Data &);
BM_REGISTER_EXTERN_METHOD(MyRegister, write, const bm::Data &,const bm::Data &);
BM_REGISTER_EXTERN_METHOD(MyRegister, find_index, const bm::Data &,bm::Data &);
and declare it in p4:
extern MyRegister<T>{
MyRegister(T num, T width);
void read(in T index, inout T value);
void write(in T index, in T value);
void find_index(in T value, inout T index);
}
Thank you again for your reply, it helped me a lot
from behavioral-model.
Related Issues (20)
- Trying to run make HOT 5
- --use-files should create files for outputs even if the interface is not provided HOT 4
- Does multicast replication preserve local metadata in v1model/simple switch? HOT 7
- Is it possible to interact from the BMv2 mininet host to Ganache (Ethereum) server? HOT 2
- Behavior of tables with empty keys HOT 6
- Installation error with thrift HOT 6
- Known issues with using `pip install nnpy` instead of particular commit SHA of nnpy? HOT 2
- Bringing a bmv2 (simple_switch) interface down freezes the switch program in Ubuntu 22.04 HOT 6
- What is the size of PHV? HOT 4
- how to achieve a timer in bmv2 HOT 3
- Can BMv2 interfaces be bound to virtual network cards of two virtual machines? How should this be done specifically? HOT 2
- import thrift
- TCP packets seem not to be processed correctly by bmv2 HOT 2
- Problem using multicast in runtime HOT 1
- Empty action-parameter vector is always accessed in ActionFnEntry::execute HOT 3
- How to build behavioral-model on the MacOS(Apple M1 Pro(13.5)) HOT 1
- How to track fileds of metadata with p4dbg? HOT 2
- Approximate conversion of bmv2 p4 performance to hardware performance HOT 3
- Applying the same table twice HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from behavioral-model.