p4_programming_slides_for_computer_networks

ShikharShiromani1 17 views 34 slides Sep 28, 2024
Slide 1
Slide 1 of 42
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42

About This Presentation

p4 programming


Slide Content

CS 6250/4251
Computer Networks
Alberto Dainotti, Thomas Holterbach
11/09/2023

reflect_packet
inout bit<48> src
inout bit<48> dst
in bit<9> inPort
out bit<9> outPort
src
dst
inPort
outPort
credit: https://adv-net.ethz.ch

Actions parameters resulting from a table lookup do not take
a direction as they come from the control plane
Parameter

without direction
I.e., The parameter's value is provided by the control plane
software when an entry is added to a table that uses that
action.
credit: https://adv-net.ethz.ch

Tables
Actions
Control flow
match a key and return an action
similar to functions in C
similar to C but without loops
Control
credit: https://adv-net.ethz.ch

Interacting with tables
from the control flow
Applying a table
(Applying a table and) Checking if there
was a hit
Check which action was executed
ipv4_lpm.apply()
if (ipv4_lpm.apply().hit) {...}
else {...}
switch (ipv4_lpm.apply().action_run) {
ipv4_forward: { ... }
}
credit: https://adv-net.ethz.ch

Validating and computing checksums
extern void verify_checksum<T, O>( in bool condition,
in T data,
inout O checksum,
HashAlgorithm algo
);
extern void update_checksum<T, O>( in bool condition,
in T data,
inout O checksum,
HashAlgorithm algo
);
v1model.p4
credit: https://adv-net.ethz.ch

Re-computing checksums

(e.g. after modifying the IP header)
credit: https://adv-net.ethz.ch

cloning packets
sending packets

to control plane
create a clone of a packet
using dedicated Ethernet port,
or target-specific mechanisms
(e.g. digests)
Control flows contain more advanced concepts
check them out!
https://p4.org/p4-spec/docs/P4-16-v1.0.0-spec.htmlmore info
recirculating send packet through pipeline multiple
times
credit: https://adv-net.ethz.ch

Example: L3 forwarding with multiple tables
IP Packet
ipv4_lpm forward
1.1.1.0 1
2.2.2.0 1
3.3.3.0 2
4.4.4.0 3
1 10
2 12
3 30
Map a prefix to 

a next hop index
Map a next hop index

to an egress port
3.3.3.0/24
4.4.4.0/24
1.1.1.0/24 2.2.2.0/24
12
30
10
credit: https://adv-net.ethz.ch

Example: L3 forwarding with multiple tables
table ipv4_lpm {
key = {
hdr.ipv4.dstAddr: lpm;
}
actions = {
set_nhop_index;
drop;
NoAction;
}
size = 1024;
default_action = NoAction();
}
table forward {
key = {
meta.nhop_index: exact;
}
actions = {
_forward;
NoAction;
}
size = 64;
default_action = NoAction();
}
action set_nhop_index(bit<8> index) {
meta.nhop_index = index;
}
credit: https://adv-net.ethz.ch

Applying multiple tables in sequence

and checking whether there was a hit
credit: https://adv-net.ethz.ch

Language
Constructs: State
12
https://p4.org/p4-spec/docs/P4-16-v-1.2.3.html

P4
environment
P4
language
stateful
objects
How do we maintain
state in P4?
credit: https://adv-net.ethz.ch

Variables have local scope and their values is
not maintained across subsequent invocations
variables cannot be used to maintain state
between different network packets
important
instead
to maintain state
you can only use two stateful constructs
tables
extern objects
modified by control plane
modified by control plane &
data plane
credit: https://adv-net.ethz.ch

15
https://opennetworking.org/wp-content/uploads/2021/05/2021-P4-WS-Vladimir-Gurevich-Slides.pdf
registers are per stage - the compiler will decide

Stateful objects in P4
Table
Register
Counter
managed by the control plane
store arbitrary data
count events
Meter

rate-limiting

externs in v1model
16
credit: https://adv-net.ethz.ch

Stateful objects in P4
Table
Register
Counter
managed by the control plane
store arbitrary data
count events
Meter

rate-limiting

externs in v1model
17
credit: https://adv-net.ethz.ch

Registers are useful for storing 

(small amounts of) arbitrary data
<Type>
r.write(…)
r.read(…)
18
credit: https://adv-net.ethz.ch

Registers are assigned in arrays
N
0
1
2
3
4
5
6
7
<Type>
r.write(0,value)
r.read(result,0)
register<Type>(N) r;
19
credit: https://adv-net.ethz.ch

Example: Calculating inter packet gap
register<bit<48>>(16384) last_seen;
action get_inter_packet_gap(out bit<48> interval, bit<32> flow_id)
{
bit<48> last_pkt_ts;
/* Get the time the previous packet was seen */
last_seen.read(last_pkt_ts, flow_id);
/* Calculate the time interval */
interval = standard_metadata.ingress_global_timestamp – last_pkt_ts;
/* Update the register with the new timestamp */
last_seen.write(flow_id, standard_metadata.ingress_global_timestamp);
...
}
20
credit: https://adv-net.ethz.ch

Example: Stateful firewall
Only allow incoming packets
if they belong to an
established connection
21
credit: https://adv-net.ethz.ch

Example: Stateful firewall
TCP

Packet
from
internal?
SYN flag?
add flow

to register
forward
flow in
register?
drop
yes yes
no
no
yes
no
22
credit: https://adv-net.ethz.ch

Example: Stateful firewall
credit: https://adv-net.ethz.ch

Stateful objects in P4
Table
Register
Counter
managed by the control plane
store arbitrary data
count events
Meter

rate-limiting

externs in v1model
24
credit: https://adv-net.ethz.ch

Counters are useful for… counting
Type
c.count(…)
c.read(…)
only from the 

control plane
25
credit: https://adv-net.ethz.ch

Counters can be of three different types
Type
c.count(…)
packets
bytes
packets_and_bytes
26
credit: https://adv-net.ethz.ch

Like registers, counters are assigned in arrays
N
0
1
2
3
4
5
6
7
Type
c.count(0)
counter(N,Type) c;
27
credit: https://adv-net.ethz.ch

use the ingress port as counter index
Example: Counting packets and bytes 

arriving at each port
control MyIngress(...) {
counter(512, CounterType.packets_and_bytes) port_counter;
apply {
port_counter.count((bit<32>)standard_metadata.ingress_port);
}
}
28
credit: https://adv-net.ethz.ch

use the ingress port as counter index
Example: Reading the counter values

from the control plane
control MyIngress(...) {
counter(512, CounterType.packets_and_bytes) port_counter;
apply {
port_counter.count((bit<32>)standard_metadata.ingress_port);
}
}
Control Plane
RuntimeCmd: counter_read MyIngress.port_counter 1
MyIngress.port_counter[1]= BmCounterValue(packets=13, bytes=1150)
29
credit: https://adv-net.ethz.ch

Direct counters are a special kind of counters 

that are attached to tables
Match Action
Key ID Data
Default
Counter
Each entry has a counter cell
that counts when the entry
matches
30
credit: https://adv-net.ethz.ch

Example: Counting packets and bytes 

arriving at each port using a direct counter
attach counter to table
control MyIngress(...) {
direct_counter(CounterType.packets_and_bytes) direct_port_counter;
table count_table {
key = {
standard_metadata.ingress_port: exact;
}
actions = {
NoAction;
}
default_action = NoAction;
counters = direct_port_counter;
size = 512;
}
apply {
count_table.apply();
}
31
credit: https://adv-net.ethz.ch

Stateful objects in P4
Table
Register
Counter
managed by the control plane
store arbitrary data
count events
Meter

rate-limiting

externs in v1model
32
credit: https://adv-net.ethz.ch

Meters
Stream of packets Meter Stream of colored packets
33
credit: https://adv-net.ethz.ch

Meters
Exceeds the PIR
Does not exceed PIR
but exceeds CIR
Does not exceed
PIR and CIR
Stream of packets
PIRPeak Information Rate
CIRCommitted Information Rate
[bytes/s] or [packets/s]
[bytes/s] or [packets/s]
Parameters:
https://tools.ietf.org/html/rfc2698more info
Meter
34
credit: https://adv-net.ethz.ch

Like registers and counters, 

meters are assigned in arrays
N
0
1
2
3
4
5
6
7
Type
m.execute(0, tag)
meter(N,Type) m;
35
credit: https://adv-net.ethz.ch

Example: Using a meter for rate-limiting
max. 1 packet/s
36
credit: https://adv-net.ethz.ch

Example: Using a meter for rate-limiting
IP Packet
m_read m_filter
11:11:… 0
22:22:… 1
33:33:… 2
44:44:… 3
NoAction
drop
drop
Map the sender
(source MAC address)
to a meter index
Map the meter tag

to a policy
37
credit: https://adv-net.ethz.ch

Example: Using a meter for rate-limiting
handle packets

depending on

meter tag
packet meter
execute meter
control MyIngress(...) {
meter(32w16384, MeterType.packets) my_meter;

action m_action(bit<32> meter_index) {
my_meter.execute_meter<bit<32>>(meter_index, meta.meter_tag);
}
table m_read {
key = { hdr.ethernet.srcAddr: exact; }
actions = { m_action; NoAction; }
...
}
table m_filter {
key = { meta.meter_tag: exact; }
actions = { drop; NoAction; }
...
}
apply {
m_read.apply();
m_filter.apply();
}
}
38
credit: https://adv-net.ethz.ch

Direct meters are a special kind of meters 

that are attached to tables
Match Action
Key ID Data
Default
Meter
Each entry has a meter cell
that is executed 

when the entry matches
39
credit: https://adv-net.ethz.ch

Example: Using a meter for rate-limiting
direct meter
read meter
attach meter to table
control MyIngress(...) {
direct_meter<bit<32>>(MeterType.packets) my_meter;

action m_action(bit<32> meter_index) {
my_meter.read(meta.meter_tag);
}
table m_read {
key = { hdr.ethernet.srcAddr: exact; }
actions = { m_action; NoAction; }
meters = my_meter;
...
}
table m_filter { ... }
apply {
m_read.apply();
m_filter.apply();
}
}
40
credit: https://adv-net.ethz.ch

Summary
Object
Table
Register
Counter
Meter
read
apply()
read()

execute()
modify/write

write()
count()
read
yes
yes
yes
configuration only
modify/write
yes
yes
reset
Data plane interface Control plane interface
41
credit: https://adv-net.ethz.ch

42
Questions?