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

Read and write to registers #61

Open
MohamadAlAdraa opened this issue Oct 2, 2023 · 6 comments
Open

Read and write to registers #61

MohamadAlAdraa opened this issue Oct 2, 2023 · 6 comments

Comments

@MohamadAlAdraa
Copy link

Hello,

I just want to ask if P4-UTILS supports reading and writing to registers. I highly appreciate your help.

Thank you.

@edgar-costa
Copy link
Collaborator

Yes it does.

You have to use the thrift API: https://github.com/nsg-ethz/p4-utils/blob/master/p4utils/utils/thrift_API.py#L1976

You can find examples in the p4-learning repo:

https://github.com/nsg-ethz/p4-learning/blob/master/examples/recirculate_and_add_header/fill_register.py
https://github.com/nsg-ethz/p4-learning/blob/master/exercises/11-Packet-Loss-Detection/solution/packet-loss-controller.py (here you can find reads and writes from the controller)

@MohamadAlAdraa
Copy link
Author

Hi @edgar-costa,

Thank you so much for your answer, I was able to get it to work.
I have another question if you can help I would appreciate that.

I need for each incoming packet to have the queue size of each egress port. Indeed, I know that in the ingress using the standard metadata we can have access to the queue size of the port that the packet enq and deq in it.

However, what I need is for each incoming packet to check the current queue size of each egress port so I wanna send on the least congested port. Is there any solution or tricks or resources that might help in this?

Again thank you so much for your help.

@edgar-costa
Copy link
Collaborator

That is something related to specific P4 architectures not P4-utils.

Using the simple_switch you can not know the queue depth at the ingress, you could know it once you are at the egress and write it into a register, since you can access registers globally you can then read it with a bit of delay for the next packet.

Otherwise, you can not know it beforehand.

With a real hardware architecture you will need them to support this. There is something similar in tofino2 I think.

By the way, this paper: https://dl.acm.org/doi/10.1145/3098822.3098839 is doing what you want to do, but its a pre-p4 paper.

@Tyler-ytr
Copy link

Hello,

I just want to ask if P4-UTILS supports reading and writing to registers. I highly appreciate your help.

Thank you.

You can do so like:

register<int<32>,bit<8>>(32) test_reg;
bit<8> index1=0;
int<32> result;
conv_reg.read(result,index1);

@yqcc-p4
Copy link

yqcc-p4 commented Dec 5, 2023

I am writing the source ip address and destination ip address of the header to the register, why is the data read by the controller a set of numbers, how to translate the bit IP address

@edgar-costa
Copy link
Collaborator

@huyongqingandczw That has nothing to do with P4. Addresses are 32-bit integers. When you read them, you need to translate them yourself. This code will do that

import socket
import struct

def int_to_ip(int_ip):
    # Convert the integer to a binary string
    packed_ip = struct.pack("!I", int_ip)
    # Unpack the binary string into a readable IPv4 address
    readable_ip = socket.inet_ntoa(packed_ip)
    return readable_ip```

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

4 participants