Strange behaviour when reading a register

I defined a global register as follows:
register<bit<32>>(MAX_PORTS) byte_cnt_reg;
Then, in the apply method in MyIngress, I want to increment the value of the register using the following code:

        bit<32> byte_cnt;
        byte_cnt_reg.read(byte_cnt, 1);
        byte_cnt = byte_cnt + 1;
        byte_cnt_reg.write(1, byte_cnt);

It works as it should without any problem.
However, if I put the above code in an if condition like:

    if (hdr.ipv4.isValid()) {
        bit<32> byte_cnt;
        byte_cnt_reg.read(byte_cnt, 1);
        byte_cnt = byte_cnt + 1;
        byte_cnt_reg.write(1, byte_cnt);
        ....

Two problems happen! First, the value of the register is a strange large integer instead of zero. Second, at some points in my code, some commands will not be executed. Also, in the log file of the respective switch for those codes, I see: “Primitive (no source info)”.
Please help me with this strange behaviour!

If you are able to share a complete P4 program, e.g. a link to a copy of it in some public Github/Gitlab/etc. repository, ideally a program that has been cut down to something close to the smallest it can be that still exhibits the problem, and any details about table entries you installed and/or packets you are sending (if those make a difference), what output you saw, and what you expected, I know that can be tedious to do, but it may make it much quicker for someone to reproduce the behavior you are seeing and diagnose it. From your description alone, it is not obvious to me why the behavior would differ. Oh, also whether you are using the open source p4c compiler with the v1model architecture (I am guessing from your source code snippets that you are probably using those), or something else.

1 Like