Git Product home page Git Product logo

Comments (4)

antoninbas avatar antoninbas commented on July 20, 2024

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.

againwq avatar againwq commented on July 20, 2024

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.

antoninbas avatar antoninbas commented on July 20, 2024

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.

againwq avatar againwq commented on July 20, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.