p4-dev@lists.p4.org

list for questions/discussion of p4 programs and tools

View all threads

Difficulties implementing Switch statement for ingress_port

FM
Francisco Machado
Sun, May 2, 2021 7:10 PM

Hi,

I am working on a pretty simple program that only has one table which
simply forwards packets.
In the apply of the ingress block, when there is a miss, the program
invokes two different actions. And these actions depend on the ingress port
the packet was received. (I consider that there are only 2 possible ports)

How can I implement this using the switch statement? I would like to run
something like this:

apply {
if (!forward.apply().hit){
switch(standard_metadata.ingress_port){
1: register_flow(); //packet received in ingress port 1
2: drop(); //packet received in ingress port 2
}
}
}

Is it possible? I'm not sure I understand the usage of the
switch statement, I only find examples of this being used with
"table.apply().action_run".

Thanks in advance

Hi, I am working on a pretty simple program that only has one table which simply forwards packets. In the apply of the ingress block, when there is a miss, the program invokes two different actions. And these actions depend on the ingress port the packet was received. (I consider that there are only 2 possible ports) How can I implement this using the switch statement? I would like to run something like this: apply { if (!forward.apply().hit){ switch(standard_metadata.ingress_port){ 1: register_flow(); //packet received in ingress port 1 2: drop(); //packet received in ingress port 2 } } } Is it possible? I'm not sure I understand the usage of the switch statement, I only find examples of this being used with "table.apply().action_run". Thanks in advance
MB
Mihai Budiu
Sun, May 2, 2021 8:03 PM

Usually this is what tables are for, use the port as a key and initialize the table at compilation time.


From: Francisco Machado franciscocmachado@gmail.com
Sent: Sunday, May 2, 2021 12:10:11 PM
To: p4-dev@lists.p4.org p4-dev@lists.p4.org
Subject: [P4-dev] Difficulties implementing Switch statement for ingress_port

Hi,

I am working on a pretty simple program that only has one table which simply forwards packets.
In the apply of the ingress block, when there is a miss, the program invokes two different actions. And these actions depend on the ingress port the packet was received. (I consider that there are only 2 possible ports)

How can I implement this using the switch statement? I would like to run something like this:

apply {
if (!forward.apply().hit){
switch(standard_metadata.ingress_port){
1: register_flow(); //packet received in ingress port 1
2: drop(); //packet received in ingress port 2
}
}
}

Is it possible? I'm not sure I understand the usage of the switch statement, I only find examples of this being used with "table.apply().action_run".

Thanks in advance

Usually this is what tables are for, use the port as a key and initialize the table at compilation time. ________________________________ From: Francisco Machado <franciscocmachado@gmail.com> Sent: Sunday, May 2, 2021 12:10:11 PM To: p4-dev@lists.p4.org <p4-dev@lists.p4.org> Subject: [P4-dev] Difficulties implementing Switch statement for ingress_port Hi, I am working on a pretty simple program that only has one table which simply forwards packets. In the apply of the ingress block, when there is a miss, the program invokes two different actions. And these actions depend on the ingress port the packet was received. (I consider that there are only 2 possible ports) How can I implement this using the switch statement? I would like to run something like this: apply { if (!forward.apply().hit){ switch(standard_metadata.ingress_port){ 1: register_flow(); //packet received in ingress port 1 2: drop(); //packet received in ingress port 2 } } } Is it possible? I'm not sure I understand the usage of the switch statement, I only find examples of this being used with "table.apply().action_run". Thanks in advance
AF
Andy Fingerhut
Sun, May 2, 2021 9:02 PM

The use of switch statement that you show has relatively recently had
support added for it to the open source p4c compiler, and should work if
you build a recent version from source code (or someone else did for you).

Since there are only two cases, an if/then/else statement would work just
as well there.

And as Mihai mentioned, you could also create a P4 table with
standard_metadata.ingress_port as a key and const entries.

All of those techniques should work reasonably for what you are trying to
do.  Only the first approach requires a recent version of p4c.  The others
have been supported for a long time.

Andy

On Sun, May 2, 2021 at 3:11 PM Francisco Machado <
franciscocmachado@gmail.com> wrote:

Hi,

I am working on a pretty simple program that only has one table which
simply forwards packets.
In the apply of the ingress block, when there is a miss, the program
invokes two different actions. And these actions depend on the ingress port
the packet was received. (I consider that there are only 2 possible ports)

How can I implement this using the switch statement? I would like to run
something like this:

apply {
if (!forward.apply().hit){
switch(standard_metadata.ingress_port){
1: register_flow(); //packet received in ingress port 1
2: drop(); //packet received in ingress port 2
}
}
}

Is it possible? I'm not sure I understand the usage of the
switch statement, I only find examples of this being used with
"table.apply().action_run".

Thanks in advance


P4-dev mailing list -- p4-dev@lists.p4.org
To unsubscribe send an email to p4-dev-leave@lists.p4.org
%(web_page_url)slistinfo%(cgiext)s/%(_internal_name)s

The use of switch statement that you show has relatively recently had support added for it to the open source p4c compiler, and should work if you build a recent version from source code (or someone else did for you). Since there are only two cases, an if/then/else statement would work just as well there. And as Mihai mentioned, you could also create a P4 table with `standard_metadata.ingress_port` as a key and `const entries`. All of those techniques should work reasonably for what you are trying to do. Only the first approach requires a recent version of p4c. The others have been supported for a long time. Andy On Sun, May 2, 2021 at 3:11 PM Francisco Machado < franciscocmachado@gmail.com> wrote: > Hi, > > I am working on a pretty simple program that only has one table which > simply forwards packets. > In the apply of the ingress block, when there is a miss, the program > invokes two different actions. And these actions depend on the ingress port > the packet was received. (I consider that there are only 2 possible ports) > > How can I implement this using the switch statement? I would like to run > something like this: > > apply { > if (!forward.apply().hit){ > switch(standard_metadata.ingress_port){ > 1: register_flow(); //packet received in ingress port 1 > 2: drop(); //packet received in ingress port 2 > } > } > } > > Is it possible? I'm not sure I understand the usage of the > switch statement, I only find examples of this being used with > "table.apply().action_run". > > Thanks in advance > _______________________________________________ > P4-dev mailing list -- p4-dev@lists.p4.org > To unsubscribe send an email to p4-dev-leave@lists.p4.org > %(web_page_url)slistinfo%(cgiext)s/%(_internal_name)s
FM
Francisco Machado
Mon, May 3, 2021 10:30 AM

Thank you for all the help.
I didn't implement it with a table because those two actions are not the
ones I pretend to attribute to the matching packets.
Those actions are the default action for when a packet does not match any
entry, which differs according to the ingress port. (I should have written
this in the first post, sorry)

Andy Fingerhut andy.fingerhut@gmail.com escreveu no dia domingo,
2/05/2021 à(s) 22:03:

The use of switch statement that you show has relatively recently had
support added for it to the open source p4c compiler, and should work if
you build a recent version from source code (or someone else did for you).

Since there are only two cases, an if/then/else statement would work just
as well there.

And as Mihai mentioned, you could also create a P4 table with
standard_metadata.ingress_port as a key and const entries.

All of those techniques should work reasonably for what you are trying to
do.  Only the first approach requires a recent version of p4c.  The others
have been supported for a long time.

Andy

On Sun, May 2, 2021 at 3:11 PM Francisco Machado <
franciscocmachado@gmail.com> wrote:

Hi,

I am working on a pretty simple program that only has one table which
simply forwards packets.
In the apply of the ingress block, when there is a miss, the program
invokes two different actions. And these actions depend on the ingress port
the packet was received. (I consider that there are only 2 possible ports)

How can I implement this using the switch statement? I would like to run
something like this:

apply {
if (!forward.apply().hit){
switch(standard_metadata.ingress_port){
1: register_flow(); //packet received in ingress port 1
2: drop(); //packet received in ingress port 2
}
}
}

Is it possible? I'm not sure I understand the usage of the
switch statement, I only find examples of this being used with
"table.apply().action_run".

Thanks in advance


P4-dev mailing list -- p4-dev@lists.p4.org
To unsubscribe send an email to p4-dev-leave@lists.p4.org
%(web_page_url)slistinfo%(cgiext)s/%(_internal_name)s

Thank you for all the help. I didn't implement it with a table because those two actions are not the ones I pretend to attribute to the matching packets. Those actions are the default action for when a packet does not match any entry, which differs according to the ingress port. (I should have written this in the first post, sorry) Andy Fingerhut <andy.fingerhut@gmail.com> escreveu no dia domingo, 2/05/2021 à(s) 22:03: > The use of switch statement that you show has relatively recently had > support added for it to the open source p4c compiler, and should work if > you build a recent version from source code (or someone else did for you). > > Since there are only two cases, an if/then/else statement would work just > as well there. > > And as Mihai mentioned, you could also create a P4 table with > `standard_metadata.ingress_port` as a key and `const entries`. > > All of those techniques should work reasonably for what you are trying to > do. Only the first approach requires a recent version of p4c. The others > have been supported for a long time. > > Andy > > On Sun, May 2, 2021 at 3:11 PM Francisco Machado < > franciscocmachado@gmail.com> wrote: > >> Hi, >> >> I am working on a pretty simple program that only has one table which >> simply forwards packets. >> In the apply of the ingress block, when there is a miss, the program >> invokes two different actions. And these actions depend on the ingress port >> the packet was received. (I consider that there are only 2 possible ports) >> >> How can I implement this using the switch statement? I would like to run >> something like this: >> >> apply { >> if (!forward.apply().hit){ >> switch(standard_metadata.ingress_port){ >> 1: register_flow(); //packet received in ingress port 1 >> 2: drop(); //packet received in ingress port 2 >> } >> } >> } >> >> Is it possible? I'm not sure I understand the usage of the >> switch statement, I only find examples of this being used with >> "table.apply().action_run". >> >> Thanks in advance >> _______________________________________________ >> P4-dev mailing list -- p4-dev@lists.p4.org >> To unsubscribe send an email to p4-dev-leave@lists.p4.org >> %(web_page_url)slistinfo%(cgiext)s/%(_internal_name)s > >