⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 maintbuf.c

📁 Vitual Ring Routing 管你知不知道
💻 C
📖 第 1 页 / 共 3 页
字号:
// -*- mode: C++; tab-width: 4; indent-tabs-mode: nil -*- (for GNU Emacs)
//
// (c) Microsoft Corporation. All rights reserved. 
//
// This file is part of the Microsoft Virtual Ring Routing distribution.
// You should have received a copy of the Microsoft Research Shared Source
// license agreement (MSR-SSLA) for this software; see the file "license.txt".
// If not, please see http://research.microsoft.com/vrr/license.htm,
// or write to Microsoft Research, One Microsoft Way, Redmond, WA 98052-6399.
//
// This file is derived from the Microsoft Research Mesh Connectivity Layer,
// available under the MSR-SSLA license, and downloadable from
// http://research.microsoft.com/mesh/.
//

#include "headers.h"

//* MaintBufAckExpected
//
//  Returns TRUE if we expect to receive an ack, that is,
//  we have any unacknowledged ack requests outstanding.
//
__inline boolint
MaintBufAckExpected(MaintBufNode *MBN)
{
    return (VRRAckId)(MBN->LastAckNum + 1) != MBN->NextAckNum;
}

//* MaintBufValidAck
//
//  Returns TRUE if AckNum is a valid sequence number, that is,
//  corresponds to an unacknowledged ack request.
//
__inline boolint
MaintBufValidAck(
    MaintBufNode *MBN,
    VRRAckId AckNum)
{
    //
    // LastAckNum is the last acknowledged sequence number,
    // and NextAckNum is the next sequence number that will be used.
    // NB: The sequence numbers can wrap.
    //
    return ((0 < (VRRAckId)(AckNum - MBN->LastAckNum)) &&
            ((VRRAckId)(AckNum - MBN->LastAckNum) <
             (VRRAckId)(MBN->NextAckNum - MBN->LastAckNum)));
}

//* MaintBufAckNum
//
//  Returns the most-recently used sequence number.
//
__inline VRRAckId
MaintBufAckNum(MaintBufNode *MBN)
{
    return (VRRAckId)(MBN->NextAckNum - 1);
}

//* MaintBufNew
//
//  Allocates a new maintenance buffer.
//
MaintBuf *
MaintBufNew(void)
{
    MaintBuf *MB;

    MB = ExAllocatePool(NonPagedPool, sizeof *MB);
    if (MB == NULL)
        return NULL;

    KeInitializeSpinLock(&MB->Lock);
    MB->MBN = NULL;

    MB->NumPackets = 0;
    MB->HighWater = 0;

    return MB;
}

//* MaintBufFree
//
//  Frees an existing maintenance buffer.
//
void
MaintBufFree(MiniportAdapter *VA)
{
    MaintBuf *MB = VA->MaintBuf;
    MaintBufNode *MBN;
    MaintBufPacket *MBP;

    //
    // Free the node structures.
    // There can be internally-generated packets waiting to be sent.
    //
    while ((MBN = MB->MBN) != NULL) {
        MB->MBN = MBN->Next;
        VrrKdPrint("Free MBN(s)",MBN->Address,NULL);

        while ((MBP = MBN->MBP) != NULL) {
            SRPacket *SRP = MBP->srp;

            MBN->MBP = MBP->Next;
            VrrKdPrint("MaintBufFree: complete pkt on MBN(s)",MBN->Address,NULL);

            (*SRP->TransmitComplete)(VA, SRP, NDIS_STATUS_SUCCESS);
            ExFreePool(MBP);
        }

        ExFreePool(MBN);
    }

    ExFreePool(MB);
}

//* MaintBufFindNode
//
//  Find a MaintBufNode with specified address and interfaces.
//  If one doesn't exist, creates it.
//
//  Called with the MaintBuf locked.
//
MaintBufNode *
MaintBufFindNode(MaintBuf *MB,
                 const VirtualAddress Address,
                 VRRIf InIf, VRRIf OutIf)  // gregos ToDo: rename InIf=>LocIF OutIf=>RemIF
{
    MaintBufNode *MBN;

    //
    // Search for an existing MaintBufNode.
    //
    for (MBN = MB->MBN; MBN != NULL; MBN = MBN->Next) {
        if (VirtualAddressEqual(MBN->Address, Address) &&
            (MBN->InIf == InIf) &&
            (MBN->OutIf == OutIf))
            return MBN;
    }

    //
    // Create a new MaintBufNode for this node.
    //
    MBN = ExAllocatePool(NonPagedPool, sizeof *MBN);
    if (MBN == NULL)
        return NULL;

    RtlZeroMemory(MBN, sizeof *MBN);
    RtlCopyMemory(MBN->Address, Address, SR_ADDR_LEN);
    MBN->OutIf = OutIf;
    MBN->InIf = InIf;
    MBN->LastAckNum = (VRRAckId)-1;

    //
    // Add the new node to the Maintenance Buffer.
    //
    MBN->Next = MB->MBN;
    MB->MBN = MBN;

    return MBN;
}

//* MaintBufPacketRelease
//
//  Releases a reference for a MaintBufPacket.
//
void
MaintBufPacketRelease(
    MiniportAdapter *VA,
    MaintBufPacket *MBP)
{
    if (InterlockedDecrement((PLONG)&MBP->RefCnt) == 0) {
        SRPacket *srp = MBP->srp;
        //
        // There is no outstanding use of this MaintBufPacket,
        // so deallocate it and complete the packet.
        //
        (*srp->TransmitComplete)(VA, srp, NDIS_STATUS_SUCCESS);
        ExFreePool(MBP);
    }
}

//* MaintBufTransmitComplete
//
//  Called when ProtocolTransmit finishes transmitting a packet that is
//  in the maintenance buffer. Releases a reference to the MBP.
//
void
MaintBufTransmitComplete(
    MiniportAdapter *VA,
    NDIS_PACKET *Packet,
    NDIS_STATUS Status)
{
    MaintBufPacket *MBP = PC(Packet)->MBP;

    UNREFERENCED_PARAMETER(Status);

    NdisFreePacketClone(Packet);

    MaintBufPacketRelease(VA, MBP);
}

//* MaintBufAddPacket
//
//  Adds a MaintBufPacket to a MaintBufNode,
//  returning an NDIS_PACKET to transmit.
//  Adds a reference to the MaintBufPacket
//  when returning non-NULL.
//
//  Our caller must update MBN->FirstAckReq, MBN->LackAckReq,
//  and MBN->NumAckReqs.
//
//  Called with the MaintBuf locked.
//
static NDIS_PACKET *
MaintBufAddPacket(
    MiniportAdapter *VA,
    MaintBufNode *MBN,
    MaintBufPacket *MBP)
{
    MaintBuf *MB = VA->MaintBuf;
    SRPacket *srp = MBP->srp;
    NDIS_PACKET *Packet;
    NDIS_STATUS Status;

    //
    // Get the Ack identifier for this packet.
    //
    MBP->AckNum = srp->ackreq->opt.identification = MBN->NextAckNum++;
    VrrTrace(VA,3,"MB:AR=2Pkt",srp->ackreq->opt.Source,srp->Token.NextVAddress,srp->ackreq->opt.Dest,"SeqNo",srp->ackreq->opt.identification,NULL,0);

    //
    // Add the MaintBufPacket to the MaintBufNode.
    //
    MBP->Next = MBN->MBP;
    MBN->MBP = MBP;

    if (++MBN->NumPackets > MBN->HighWater)
        MBN->HighWater = MBN->NumPackets;
    if (++MB->NumPackets > MB->HighWater)
        MB->HighWater = MB->NumPackets;

    Status = SRPacketToPkt(VA, srp, &Packet);

    if (Status != NDIS_STATUS_SUCCESS) {
        //
        // We do not add a reference to MBP.
        //
        return NULL;
    }
    else {
        //
        // Add a reference to MBP for MaintBufTransmitComplete.
        //
        InterlockedIncrement((PLONG)&MBP->RefCnt);
        return Packet;
    }
}

//* MaintBufTransmit
//
//  If Packet is non-NULL, our caller provides a reference
//  for the MaintBufPacket that we pass through
//  to MaintBufTransmitComplete.
//
//  Called with the MaintBuf locked.
//
static void
MaintBufTransmit(
    MiniportAdapter *VA,
    MaintBufPacket *MBP,
    NDIS_PACKET *Packet)
{
    if (Packet == NULL) {
        //
        // MaintBufAddPacket did not give us a ref for MBP.
        // MaintBufTimer will retransmit.
        //
        return;
    }

#if DBG
    if (MBP->srp == NULL)
        VrrTrace(VA,3,"MB:MP=Tx__",NULL,NULL,NULL,NULL,0,NULL,0);
    else
        VrrTrace(VA,3,"MB:MP=Tx__",MBP->srp->Dest,MBP->srp->Token.NextVAddress,MBP->srp->Token.NextVAddress,
               "FrameSeqNo",MBP->srp->FrameSeqNo,NULL,0);
#endif

    //
    // MaintBufTransmitComplete needs these fields.
    // We have a reference for MBP from MaintBufAddPacket
    // that we pass on to MaintBufTransmitComplete.
    //
    PC(Packet)->TransmitComplete = MaintBufTransmitComplete;
    PC(Packet)->MBP = MBP;

    if (PC(Packet)->PA == NULL) {
        //
        // This means the source route is trying to use a physical adapter
        // that no longer exists. 
        // REVIEW - We know this packet will never transmit successfully,
        // so we could immediately try salvaging instead of letting
        // MaintBufTimer get there eventually. But this is a rare situation.
        //
        VrrKdPrint("MaintBufTransmit PC(Packet)->PA == NULL",MBP->srp->Source, MBP->srp->Dest);
        MaintBufTransmitComplete(VA, Packet, NDIS_STATUS_FAILURE);
        return;
    }
    ProtocolTransmit(PC(Packet)->PA, Packet);
}

//* TxQueueIsFull
//
//  Checks if sending this packet would overflow
//  the transmit queue of the outgoing interface.
//  Returns FALSE to indicate the packet should 
//  be dropped.
//
boolint
TxQueueIsFull(
    MiniportAdapter *VA,
    SRPacket *SRP)
{
    boolint QueueFull;
    //
    // We only want to check for packets that are carrying real payload.
    //
    if (SRP->PacketLength != SRP->PayloadOffset)
        QueueFull = ProtocolQueueFull(VA, SRP->Token.LocIF);
    else
        QueueFull = FALSE;

    return !QueueFull;

}

//* MaintBufCreateAckRequest
//
//  Creates an ack request packet.
//
//  Called with the MaintBuf locked.
//
static SRPacket *
MaintBufCreateAckRequest(
    MiniportAdapter *VA,
    MaintBufNode *MBN)
{
    SRPacket *SRP;
    InternalAcknowledgementRequest *AR;

    //
    // Allocate a packet with which to send the ack request.
    //

    SRP = ExAllocatePool(NonPagedPool, sizeof *SRP);
    if (SRP == NULL)
        return NULL;

    AR = ExAllocatePool(NonPagedPool, sizeof *AR);
    if (AR == NULL) {
        ExFreePool(SRP);
        return NULL;
    }

    //
    // Initialize the packet.
    //
    RtlZeroMemory(SRP, sizeof *SRP);
    RtlCopyMemory(SRP->Dest, MBN->Address, sizeof(VirtualAddress));
    RtlCopyMemory(SRP->Source, VA->Address, sizeof(VirtualAddress));
    SRP->ackreq = AR;

    //
    // Initialize TxToken in the new SRP, taking the IF indices

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -