Windows 网络设备驱动程序:从驱动程序中设置 link UP/DOWN

Windows Network Device Driver: Set link UP/DOWN from within the driver

我正在为 Windows 编写网络驱动程序。我想做类似下面的伪代码的事情:

Init_interface_link_status = disconnected (Equivalent to DOWN in Linux)
Repeat using delayed workitem:
  if (condition is true)
    interface_link_status = connected (UP)
    break
  else
    interface_link_status = disconnected (DOWN)

所有这些都发生在驱动程序代码中。

我正在使用 Windows 驱动程序示例作为参考。我发现了一些看起来很有希望的东西:https://github.com/microsoft/Windows-driver-samples/blob/master/network/ndis/netvmini/6x/adapter.c#L353 AdapterGeneral.MediaConnectState = HWGetMediaConnectStatus(Adapter); 我可以在此处将此 MediaConnectSate 设置为 MediaConnectStateDisconnected,驱动程序会在断开连接状态下初始化,这正是我想要的。 但是我找不到在驱动程序初始化后在其他地方更改此状态的方法。

从专有网络驱动程序代码中获得灵感。 这个函数转接口 on/off:


VOID NSUChangeAdapterLinkState(
    _In_ PMP_ADAPTER Adapter,
    _In_ BOOLEAN TurnInterfaceUP)
/*++
Routine Description:
    Change Adapter Link's state. This is equivalent to doing ifup/ifdown on Linux.
Arguments:
    Adapter              - Pointer to our adapter
    TurnInterfaceUP      - Pass TRUE to turn interface UP, FALSE to turn DOWN
Return Value:
    None
--*/
{
    NDIS_LINK_STATE LinkState;
    NDIS_STATUS_INDICATION StatusIndication;

    RtlZeroMemory(&LinkState, sizeof(LinkState));
    LinkState.Header.Revision = NDIS_LINK_STATE_REVISION_1;
    LinkState.Header.Type = NDIS_OBJECT_TYPE_DEFAULT;
    LinkState.Header.Size = NDIS_SIZEOF_LINK_STATE_REVISION_1;

    if (TurnInterfaceUP)
    {
        LinkState.MediaConnectState = MediaConnectStateConnected;
        MP_CLEAR_FLAG(Adapter, fMP_DISCONNECTED);
    } else
    {
        LinkState.MediaConnectState = MediaConnectStateDisconnected;
        MP_SET_FLAG(Adapter, fMP_DISCONNECTED);
    }

    LinkState.RcvLinkSpeed = Adapter->ulLinkRecvSpeed;
    LinkState.XmitLinkSpeed = Adapter->ulLinkSendSpeed; 
    LinkState.MediaDuplexState = MediaDuplexStateFull;

    RtlZeroMemory(&StatusIndication, sizeof(StatusIndication));
    StatusIndication.Header.Type = NDIS_OBJECT_TYPE_STATUS_INDICATION;
    StatusIndication.Header.Revision = NDIS_STATUS_INDICATION_REVISION_1;
    StatusIndication.Header.Size = NDIS_SIZEOF_STATUS_INDICATION_REVISION_1;
    StatusIndication.SourceHandle = Adapter->AdapterHandle;
    StatusIndication.StatusCode = NDIS_STATUS_LINK_STATE;
    StatusIndication.StatusBuffer = &LinkState;
    StatusIndication.StatusBufferSize = sizeof(NDIS_LINK_STATE);

    NdisMIndicateStatusEx(Adapter->AdapterHandle, &StatusIndication);
}