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

How to implement getting data from multiple registers with potentially fewer packets #635

Open
kobe-S9 opened this issue Sep 23, 2024 · 6 comments

Comments

@kobe-S9
Copy link

kobe-S9 commented Sep 23, 2024

I want to read the data of 4 registers many times when a specific condition is reached in a packet processing, these data are just different indexes, but because my customized header data has only 4 items, I can only read these 4 registers once, and to read n times, I need 4*n items or n packets, how can I achieve this function while minimizing the traffic on the network link? How can I do this while minimizing the traffic on the network link?
ps: I checked the clone feature and found that cloned packets can only be processed in the egress stage, while my table and other features are in the ingress stage, is there any potential problem with this? If I define 4 global registers, I would have to write multiple lines of similar p4runtime rules code, which would make the code redundant.
Hope there is a better solution

@jafingerhut
Copy link
Collaborator

Most network devices have the ability to recirculate packets, giving you another chance to do whatever you want to the packet. This includes the v1model architecture, which will start over at ingress processing again for packets after they are recirculated.

I am not sure what you plan to do with these 4*n register read results, but for example, if you want to take the n values from one register and add them up, you can also consider maintaining one register with the individual N data items, and a separate register with their sum, and keep the one containing the sum up to date whenever you modify an item in the register with N items. Then you do not need to read all N to get the sum.

That technique only works if it is straightforward to keep the value you want updated easily, e.g. if you want the max of N elements, it is not easy to update it when the largest of the N values decreases, without reading all of the N-1 others to find out if one of them became the new maximum value.

@kobe-S9
Copy link
Author

kobe-S9 commented Sep 25, 2024

Most network devices have the ability to recirculate packets, giving you another chance to do whatever you want to the packet. This includes the v1model architecture, which will start over at ingress processing again for packets after they are recirculated.

I am not sure what you plan to do with these 4*n register read results, but for example, if you want to take the n values from one register and add them up, you can also consider maintaining one register with the individual N data items, and a separate register with their sum, and keep the one containing the sum up to date whenever you modify an item in the register with N items. Then you do not need to read all N to get the sum.

That technique only works if it is straightforward to keep the value you want updated easily, e.g. if you want the max of N elements, it is not easy to update it when the largest of the N values decreases, without reading all of the N-1 others to find out if one of them became the new maximum value.

Ok, thank you, I just want to read these 4 registers n times in as few packets as possible to get the result because these 4 registers are storing my 4 sum values, after listening to your suggestion I feel that recirculate packets is a good option, but do you think that modifying the customized header to satisfy the one time packet processing to read it n times in a specific condition, this is not a better option?

@jafingerhut
Copy link
Collaborator

It is not clear to me what the customized header format has to do with the number of times you read a register in a single pass of processing.

@kobe-S9
Copy link
Author

kobe-S9 commented Sep 25, 2024

It is not clear to me what the customized header format has to do with the number of times you read a register in a single pass of processing.

Thank you for your reply, because I use a custom header to store the data, the number of times the data is read varies and determines the length of the header, this number of times n up to 4 times, so I want to adapt the number of times the register data is read by modifying the entries of the custom header to give me enough entries to store the read results

@jafingerhut
Copy link
Collaborator

If n is the number of times you want to access a register, or in general repeat any block of code that does anything at all, you can do it with P4 code like this:

control do_something_n_times (in bit<8> n, /* other parameters go here */) {
    apply {
        i = 0;
        if (n <= i) return;
        // repetitive code for i=0 here
        i = 1;
        if (n <= i) return;
        // repetitive code for i=1 here
        i = 2;
        if (n <= i) return;
        // repetitive code for i=2 here

        // ... etc., up to maximum value of i that you might want to repeat for
    }
}

If that is too repetitive to type, you can write a little program that prints out the P4 source code of interest for you, e.g. like in an example shown here that uses a Python program to print P4 source code: https://github.com/jafingerhut/p4-guide/blob/master/code-generation/README.md

@kobe-S9
Copy link
Author

kobe-S9 commented Sep 26, 2024

If n is the number of times you want to access a register, or in general repeat any block of code that does anything at all, you can do it with P4 code like this:

control do_something_n_times (in bit<8> n, /* other parameters go here */) {
    apply {
        i = 0;
        if (n <= i) return;
        // repetitive code for i=0 here
        i = 1;
        if (n <= i) return;
        // repetitive code for i=1 here
        i = 2;
        if (n <= i) return;
        // repetitive code for i=2 here

        // ... etc., up to maximum value of i that you might want to repeat for
    }
}

If that is too repetitive to type, you can write a little program that prints out the P4 source code of interest for you, e.g. like in an example shown here that uses a Python program to print P4 source code: https://github.com/jafingerhut/p4-guide/blob/master/code-generation/README.md

ok,Thank you very much.I think I can finish my work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants