Is if statement in action supported in bmv2

Hello,
I am using p4_16, bmv2, and simple_switch. I am using registers and after writing to each register, I want to check the size that is used in this register, and if it is higher than a threshold I want to send a notification to the controller. Therefore, I need to use an “if” statement in the action part of my code to see if the used part of the register is higher than a specific threshold. Is it possible?

So I agree that this might seem unnatural and not obvious at all, but most P4 targets support if statements inside the ‘apply’ block of your control (e.g. inside of ingress or egress controls) much better than they do inside of an if statement.

Feel free to share your current code with a link to a public web site, e.g. Github, if you are interested in more specific advice for your program, but if the general advice here leads you to a solution, then great.

Instead of trying to do an if statement inside of an action, from your description it sounds like you should be able to instead assign the current value stored in the register to some metadata variable inside of the action, with no if statement inside of the action, e.g. current_size = size_read_from_the_register;

Then, soon after apply’ing the table in the control where this action is executed, do something like if (current_size > threshold) { send_notification_code_here; }

1 Like

Thanks a lot for your reply. Yes, I think if statement in the apply block can also be used in this problem.