packages feed

pykinos-core-0.1.1.0: src/Pykinos/Net/SackWindow.hs

{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE DerivingStrategies #-}
-- | Module: Pykinos.Net.SackWindow
-- Description: Zero-allocation RFC 1982 TSN sliding window and ctz-based SACK gap extractor
--
-- Implements SCTP-style cumulative TSN tracking and selective-acknowledgement
-- (SACK) gap-ack block extraction over a 64-slot unboxed 'Word64' bitmask.
module Pykinos.Net.SackWindow
  ( serialLess
  , serialDiff
  , SackGap(..)
  , TsnOutcome(..)
  , SackWindow(..)
  , initSackWindow
  , recordTsn
  , extractSackGaps
  , advanceForwardTsn
  ) where

import Data.Bits
  ( complement
  , countTrailingZeros
  , setBit
  , shiftR
  , testBit
  )
import Data.Int (Int32)
import Data.Word (Word16, Word32, Word64)

-- | RFC 1982 signed modular distance @(a - b)@ in @[-2^31, 2^31 - 1]@.
{-# INLINE serialDiff #-}
serialDiff :: Word32 -> Word32 -> Int32
serialDiff !a !b = fromIntegral (a - b)

-- | RFC 1982 wrap-around comparison: 'True' iff @a@ strictly precedes @b@.
{-# INLINE serialLess #-}
serialLess :: Word32 -> Word32 -> Bool
serialLess !a !b = serialDiff a b < 0

-- | Contiguous inclusive SACK Gap Ack Block expressed as offsets above 'swCumTsn'.
data SackGap = SackGap
  { gapStart :: !Word16
  , gapEnd   :: !Word16
  } deriving stock (Eq, Ord, Show)

-- | Classification of an arriving TSN against the current sliding window.
data TsnOutcome
  = TsnAcceptedNew
  | TsnDuplicate
  | TsnOutOfWindow
  deriving stock (Eq, Ord, Show, Enum, Bounded)

-- | Compact 2-word reception window: cumulative TSN + 64-bit ahead-of-cum bitmap.
-- Bit @i@ (0-indexed) represents @swCumTsn + 1 + i@.
data SackWindow = SackWindow
  { swCumTsn :: !Word32
  , swBitmap :: !Word64
  } deriving stock (Eq, Ord, Show)

-- | Initializes a 'SackWindow' expecting @initialTsn@ as the first packet.
initSackWindow :: Word32 -> SackWindow
initSackWindow !initialTsn = SackWindow (initialTsn - 1) 0

-- | Records an arriving TSN, advancing 'swCumTsn' across all newly contiguous bits.
recordTsn :: Word32 -> SackWindow -> (TsnOutcome, SackWindow)
recordTsn !tsn sw@(SackWindow cum bm) =
  let !d = serialDiff tsn cum
  in if d <= 0
       then (TsnDuplicate, sw)
       else if d > 64
         then (TsnOutOfWindow, sw)
         else
           let !bitIdx = fromIntegral (d - 1)
           in if testBit bm bitIdx
                then (TsnDuplicate, sw)
                else
                  let !bm1    = setBit bm bitIdx
                      !adv    = countTrailingZeros (complement bm1)
                      !cum'   = cum + fromIntegral adv
                      !bm'    = if adv >= 64 then 0 else bm1 `shiftR` adv
                  in (TsnAcceptedNew, SackWindow cum' bm')

-- | Advances cumulative TSN via PR-SCTP Forward-TSN (abandoning expired gaps).
advanceForwardTsn :: Word32 -> SackWindow -> SackWindow
advanceForwardTsn !newCum sw@(SackWindow cum bm) =
  let !d = serialDiff newCum cum
  in if d <= 0
       then sw
       else if d >= 64
         then SackWindow newCum 0
         else
           let !bm1  = bm `shiftR` fromIntegral d
               !adv  = countTrailingZeros (complement bm1)
               !cum' = newCum + fromIntegral adv
               !bm'  = if adv >= 64 then 0 else bm1 `shiftR` adv
           in SackWindow cum' bm'

-- | Extracts contiguous SACK Gap Ack Blocks in O(number of gaps) using @ctz@.
extractSackGaps :: SackWindow -> [SackGap]
extractSackGaps (SackWindow _ bm0) = go 0 bm0
  where
    go :: Int -> Word64 -> [SackGap]
    go !_ 0 = []
    go !base !m =
      let !skipZeros = countTrailingZeros m
          !shifted1  = m `shiftR` skipZeros
          !runOnes   = countTrailingZeros (complement shifted1)
          !startOff  = base + skipZeros + 1
          !endOff    = startOff + runOnes - 1
          !nextBase  = base + skipZeros + runOnes
          !nextMask  = if runOnes >= 64 then 0 else shifted1 `shiftR` runOnes
      in SackGap (fromIntegral startOff) (fromIntegral endOff) : go nextBase nextMask