Getting current time in P4 code.

555 views
Skip to first unread message

VISHAL RAWAT

unread,
Jan 30, 2020, 12:51:38 PM1/30/20
to open-nfp
Hi,

So, I was looking for a way to correctly timestamp the packets and matching current time against already time-stamped packets. Is there a way in P4 or Micro-C by which I can get current time correctly?

Thanks,
Vishal Rawat.

Octavio Herrera-Ruiz

unread,
Feb 7, 2020, 11:16:35 AM2/7/20
to open-nfp
Hello Vishal,

   If you look into the different threads of this discussion forum you will find multiple entries on the subject. You can start by checking this one: https://groups.google.com/forum/?utm_source=digest&utm_medium=email#!searchin/open-nfp/timestamp%7Csort:date/open-nfp/11KugiOwoTo/pVhYdSDFAQAJ

   Let us know if you would need further assistance on the subject.

   Have a nice day!

Octavio Herrera
Netronome Support

VISHAL RAWAT

unread,
May 23, 2020, 2:23:00 AM5/23/20
to open-nfp
Hi Octavio,

   I looked at the method of getting ingress and egress timestamps from the link you shared. I have replicated David's P4 project to P4 16, I am able to get 64 bit timestamp correctly using intrinsic_metadata fields in p4 16 but I am not able to get egress timestamp. If I enable mac registers then I don't even receive the packet back i.e. the packet gets dropped while setting the egress timestamp.

I am using below command to compile the project :
/opt/netronome/p4/bin/nfp4build --output-nffw-filename ./out/ts.nffw   --sandbox-c ./plugin.c   --incl-p4-build ts.p4  include/checksum.p4                 include/definitions.p4                 include/header.p4                 include/parser.p4  --sku nfp-4xxx --platform hydrogen  -r  --nfp4c_p4_version 16  --nfp4c_p4_compiler p4c-nfp  --nfirc_mac_ingress_timestamp

I am using below command to offload firmware:
./rtecli design-load -f ~/vishal/TS_/out/ts.nffw -p ~/vishal/TS_/out/pif_design.json

I am using below commands to set mac registers:
sudo ./nfp-reg xpb:Nbi0IsldXpbMap.NbiTopXpbMap.MacGlbAdrMap.MacCsr.EgCmdPrependEn0Hi=0xffffffff
sudo ./nfp-reg xpb:Nbi0IsldXpbMap.NbiTopXpbMap.MacGlbAdrMap.MacCsr.EgCmdPrependEn0Lo=0xffffffff

The above commands are executed in the same sequence in which they are mentioned.

Please let me know if I am missing some step or doing something wrong. If it is required I can share complete project here.

Thanks and regards,
Vishal Rawat

Tejas MD

unread,
Mar 10, 2025, 6:47:09 AMMar 10
to open-nfp
Hi, 

I have been also trying to get egress mac timestamp on P4_16 and have similar problems. David's code is working but my P4_16 version is not. I have enabled mac registers and added the egress command header. I have attached the p4 file I have written, along with the commands I use to compile and load the program. 

```
/* -*- P4_16 -*- */
#include <core.p4>
#include <v1model.p4>

/*************************************************************************
*********************** H E A D E R S  ***********************************
*************************************************************************/
typedef bit<16>  egressSpec_t;
typedef bit<48> macAddr_t;

header ethernet_t {
    macAddr_t dstAddr;
    macAddr_t srcAddr;
    bit<16>   etherType;
}

header nfp_mac_eg_ts_t{
    bit<24> zero;
    bit<8> eg_off_byte;
}

struct intrinsic_metadata_t {
    bit<64> ingress_global_timestamp;
    bit<64> current_global_timestamp;
}

header nfp_ts_hdr_t {
        bit<64>ig_ts;
        bit<64>eg_ts;
}

struct metadata {
    intrinsic_metadata_t intrinsic_metadata;
}

struct headers {
    nfp_mac_eg_ts_t nfp_mac_eg_ts;
    ethernet_t   ethernet;
    nfp_ts_hdr_t nfp_ts_hdr;
}

/*************************************************************************
*********************** P A R S E R  ***********************************
*************************************************************************/
parser MyParser(packet_in packet,
                out headers hdr,
                inout metadata meta,
                inout standard_metadata_t standard_metadata) {

    state start {
        transition parse_ethernet;
    }

    state parse_ethernet {
        packet.extract(hdr.ethernet);
        transition accept;
    }

}

/*************************************************************************
************   C H E C K S U M    V E R I F I C A T I O N   *************
*************************************************************************/
control MyVerifyChecksum(inout headers hdr, inout metadata meta) {
    apply {  }
}

/*************************************************************************
**************  I N G R E S S   P R O C E S S I N G   *******************
*************************************************************************/
control MyIngress(inout headers hdr,
                  inout metadata meta,
                  inout standard_metadata_t standard_metadata) {
    action drop() {
        mark_to_drop();
    }

    action all_packets_fwd_action(egressSpec_t port) {
        standard_metadata.egress_spec = port;

    }

    table all_packets_fwd {
        key = {
            standard_metadata.ingress_port: exact;
        }
        actions = {
            all_packets_fwd_action;
            drop;
            NoAction;
        }
        size = 1024;
        default_action = NoAction;
    }


    apply {
        hdr.nfp_ts_hdr.setValid();
        hdr.nfp_ts_hdr.ig_ts = meta.intrinsic_metadata.ingress_global_timestamp;
        hdr.nfp_ts_hdr.eg_ts = 0xffffffffffffffff;  //meta.intrinsic_metadata.current_global_timestamp;
hdr.nfp_mac_eg_ts.setValid();
hdr.nfp_mac_eg_ts.zero = 0;
hdr.nfp_mac_eg_ts.eg_off_byte = 18;
        all_packets_fwd.apply();
    }
}

/*************************************************************************
****************  E G R E S S   P R O C E S S I N G   *******************
*************************************************************************/
control MyEgress(inout headers hdr,
                 inout metadata meta,
                 inout standard_metadata_t standard_metadata) {
    apply { }
}

/*************************************************************************
*************   C H E C K S U M    C O M P U T A T I O N   **************
*************************************************************************/
control MyComputeChecksum(inout headers  hdr, inout metadata meta) {
    apply { }
}

/*************************************************************************
***********************  D E P A R S E R  *******************************
*************************************************************************/
control MyDeparser(packet_out packet, in headers hdr) {
    apply {
packet.emit(hdr.nfp_mac_eg_ts);
        packet.emit(hdr.ethernet);
packet.emit(hdr.nfp_ts_hdr);

    }
}

/*************************************************************************
***********************  S W I T C H  *******************************
*************************************************************************/
V1Switch(
MyParser(),
MyVerifyChecksum(),
MyIngress(),
MyEgress(),
MyComputeChecksum(),
MyDeparser()
) main;

```

Commands to build and upload: 

```
sudo modprobe -r -v nfp && sudo modprobe nfp nfp_pf_netdev=0 nfp_dev_cpp=1
sudo /opt/netronome/p4/bin/nfp4build -o basic.nffw -p out -4 basic.p4 -l lithium --nfp4c_p4_version 16 --nfp4c_I /opt/netronome/p4/include/16/p4include/ --nfirc_mac_ingress_timestamp
sudo systemctl start nfp-sdk6-rte.service
sudo systemctl status nfp-sdk6-rte.service


sudo /opt/netronome/p4/bin/rtecli design-unload
sudo /opt/netronome/bin/nfp-nffw unload
sudo /opt/netronome/p4/bin/rtecli design-load -f basic.nffw -p out/pif_design.json
sudo /opt/netronome/p4/bin/rtecli config-reload -c user_config.json

```


user_config.json : 

```
{
    "tables": {
      "ingress::all_packets_fwd": {
        "rules": [
          {
            "action": {
              "data": {
                "port": {
                  "value": "p0"
                }
              },
              "type": "ingress::all_packets_fwd_action"
            },
            "name": "p1 to p0",
            "match": {
              "standard_metadata.ingress_port": {
                "value": "p1"
              }
            }
          },
          {
            "action": {
              "data": {
                "port": {
                  "value": "p1"
                }
              },
              "type": "ingress::all_packets_fwd_action"
            },
            "name": "p0 to p1",
            "match": {
              "standard_metadata.ingress_port": {
                "value": "p0"
              }
            }
          }
        ]
      }
    }
  }
```
Reply all
Reply to author
Forward
0 new messages