From 13875b1598906bbc29dedfc76f7a5d61641088d6 Mon Sep 17 00:00:00 2001 From: Piotr Sawicki Date: Thu, 14 Sep 2023 01:08:47 -0700 Subject: [PATCH] Add initialize method to InternetChecksum --- .../psa/examples/psa-example-incremental-checksum.p4 | 10 ++-------- p4-16/psa/psa.p4 | 11 +++++++++++ 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/p4-16/psa/examples/psa-example-incremental-checksum.p4 b/p4-16/psa/examples/psa-example-incremental-checksum.p4 index 55360f544a..4a34ebea1b 100644 --- a/p4-16/psa/examples/psa-example-incremental-checksum.p4 +++ b/p4-16/psa/examples/psa-example-incremental-checksum.p4 @@ -208,14 +208,8 @@ control EgressDeparserImpl(packet_out packet, }); hdr.ipv4.hdrChecksum = ck.get(); // Update TCP checksum - // This clear() call is necessary for correct behavior, since - // the same instance 'ck' is reused from above for the same - // packet. If a second InternetChecksum instance other than - // 'ck' were used below instead, this clear() call would be - // unnecessary. - ck.clear(); - // Subtract the original TCP checksum - ck.subtract(hdr.tcp.checksum); + // Initialize with the original TCP checksum + ck.initialize(hdr.tcp.checksum); // Subtract the effect of the original IPv4 source address, // which is part of the TCP 'pseudo-header' for the purposes // of TCP checksum calculation (see RFC 793), then add the diff --git a/p4-16/psa/psa.p4 b/p4-16/psa/psa.p4 index 2e573cdedc..c66a8ffb90 100644 --- a/p4-16/psa/psa.p4 +++ b/p4-16/psa/psa.p4 @@ -570,6 +570,17 @@ extern InternetChecksum { /// 16 bits long. void subtract(in T data); + /// Initialize the InternetChecksum extern object. + /// This method prepares the InternetChecksum object to compute an + /// incremental checksum (e.g. TCP checksum). + /// The primary use case for this method is to initialize the checksum + /// computation with the value extracted from the received packet header. + /// Calling ck.initizalize(chksum) is algorithmically equivalent to calling + /// ck.clear() followed by ck.substract(chksum) + /// @param chksum : The initial checksum value, typically extracted from + /// the packet header. + void initialize(in bit<16> chksum); + /// Get checksum for data added (and not removed) since last clear @noSideEffects bit<16> get();