/*
 * Copyright (c) 2019 NITK Surathkal
 *
 * SPDX-License-Identifier: GPL-2.0-only
 *
 * Author: Apoorva Bhargava <apoorvabhargava13@gmail.com>
 *         Mohit P. Tahiliani <tahiliani@nitk.edu.in>
 *
 */

#include "tcp-linux-reno.h"

#include "ns3/log.h"
#include "ns3/simulator.h"

namespace ns3
{

NS_LOG_COMPONENT_DEFINE("TcpLinuxReno");
NS_OBJECT_ENSURE_REGISTERED(TcpLinuxReno);

TypeId
TcpLinuxReno::GetTypeId()
{
    static TypeId tid = TypeId("ns3::TcpLinuxReno")
                            .SetParent<TcpCongestionOps>()
                            .SetGroupName("Internet")
                            .AddConstructor<TcpLinuxReno>()
                            .AddAttribute("BetaLoss",
                                          "Beta for multiplicative decrease",
                                          DoubleValue(0.5),
                                          MakeDoubleAccessor(&TcpLinuxReno::m_betaLoss),
                                          MakeDoubleChecker<double>(0.0, 1.0))
                            .AddAttribute("BetaEcn",
                                          "Beta for multiplicative decrease for ABE",
                                          DoubleValue(0.7), // According to RFC 8511 (ABE)
                                          MakeDoubleAccessor(&TcpLinuxReno::m_betaEcn),
                                          MakeDoubleChecker<double>(0.0, 1.0));

    return tid;
}

TcpLinuxReno::TcpLinuxReno()
    : TcpCongestionOps()
{
    NS_LOG_FUNCTION(this);
}

TcpLinuxReno::TcpLinuxReno(const TcpLinuxReno& sock)
    : TcpCongestionOps(sock)
{
    NS_LOG_FUNCTION(this);
}

TcpLinuxReno::~TcpLinuxReno()
{
}

uint32_t
TcpLinuxReno::SlowStart(Ptr<TcpSocketState> tcb, uint32_t segmentsAcked)
{
    NS_LOG_FUNCTION(this << tcb << segmentsAcked);

    if (segmentsAcked >= 1)
    {
        uint32_t sndCwnd = tcb->m_cWnd;
        tcb->m_cWnd =
            std::min((sndCwnd + (segmentsAcked * tcb->m_segmentSize)), (uint32_t)tcb->m_ssThresh);
        NS_LOG_INFO("In SlowStart, updated to cwnd " << tcb->m_cWnd << " ssthresh "
                                                     << tcb->m_ssThresh);
        return segmentsAcked - ((tcb->m_cWnd - sndCwnd) / tcb->m_segmentSize);
    }

    return 0;
}

void
TcpLinuxReno::CongestionAvoidance(Ptr<TcpSocketState> tcb, uint32_t segmentsAcked)
{
    NS_LOG_FUNCTION(this << tcb << segmentsAcked);

    if (m_suppressIncreaseIfCwndLimited && !tcb->m_isCwndLimited)
    {
        NS_LOG_DEBUG("No increase because current cwnd " << tcb->m_cWnd
                                                         << " is not limiting the flow");
        return;
    }

    uint32_t w = tcb->m_cWnd / tcb->m_segmentSize;

    // Floor w to 1 if w == 0
    if (w == 0)
    {
        w = 1;
    }

    NS_LOG_DEBUG("w in segments " << w << " m_cWndCnt " << m_cWndCnt << " segments acked "
                                  << segmentsAcked);
    if (m_cWndCnt >= w)
    {
        m_cWndCnt = 0;
        tcb->m_cWnd += tcb->m_segmentSize;
        NS_LOG_DEBUG("Adding 1 segment to m_cWnd");
    }

    m_cWndCnt += segmentsAcked;
    NS_LOG_DEBUG("Adding 1 segment to m_cWndCnt");
    if (m_cWndCnt >= w)
    {
        uint32_t delta = m_cWndCnt / w;

        m_cWndCnt -= delta * w;
        tcb->m_cWnd += delta * tcb->m_segmentSize;
        NS_LOG_DEBUG("Subtracting delta * w from m_cWndCnt " << delta * w);
    }
    NS_LOG_DEBUG("At end of CongestionAvoidance(), m_cWnd: " << tcb->m_cWnd
                                                             << " m_cWndCnt: " << m_cWndCnt);
}

void
TcpLinuxReno::IncreaseWindow(Ptr<TcpSocketState> tcb, uint32_t segmentsAcked)
{
    NS_LOG_FUNCTION(this << tcb << segmentsAcked);

    // Linux tcp_in_slow_start() condition
    if (tcb->m_cWnd < tcb->m_ssThresh)
    {
        NS_LOG_DEBUG("In slow start, m_cWnd " << tcb->m_cWnd << " m_ssThresh " << tcb->m_ssThresh);
        segmentsAcked = SlowStart(tcb, segmentsAcked);
    }
    else
    {
        NS_LOG_DEBUG("In cong. avoidance, m_cWnd " << tcb->m_cWnd << " m_ssThresh "
                                                   << tcb->m_ssThresh);
        CongestionAvoidance(tcb, segmentsAcked);
    }
}

std::string
TcpLinuxReno::GetName() const
{
    return "TcpLinuxReno";
}

uint32_t
TcpLinuxReno::GetSsThresh(Ptr<const TcpSocketState> state, uint32_t bytesInFlight)
{
    NS_LOG_FUNCTION(this << state << bytesInFlight);

    if (state->m_abeEnabled && state->m_ecnState == TcpSocketState::ECN_ECE_RCVD)
    {
        return std::max<uint32_t>(2 * state->m_segmentSize,
                                  state->m_cWnd * m_betaEcn); // According to RFC 8511 (ABE)
    }

    return std::max<uint32_t>(2 * state->m_segmentSize, state->m_cWnd * m_betaLoss);
}

Ptr<TcpCongestionOps>
TcpLinuxReno::Fork()
{
    return CopyObject<TcpLinuxReno>(this);
}

void
TcpLinuxReno::SetSuppressIncreaseIfCwndLimited(bool value)
{
    NS_LOG_FUNCTION(this << value);
    m_suppressIncreaseIfCwndLimited = value;
}

} // namespace ns3
