nsg-ethz / p4-utils

Extension to Mininet that makes P4 networks easier to build
GNU General Public License v2.0
175 stars 65 forks source link

Read and write to registers #61

Open MohamadAlAdraa opened 9 months ago

MohamadAlAdraa commented 9 months ago

Hello,

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

Thank you.

edgar-costa commented 9 months ago

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 commented 9 months ago

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 commented 9 months ago

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 commented 8 months ago

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 commented 7 months ago

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 commented 7 months ago

@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```