首页 > 解决方案 > Windows 网络设备驱动程序:从驱动程序中设置链接 UP/DOWN

问题描述

我正在为 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这里和驱动程序在断开连接状态下初始化,这是我想要的。但是在初始化驱动程序后,我找不到在其他地方更改此状态的方法。

标签: windowsdriverndisminiport

解决方案


从专有网络驱动程序代码中找到灵感。此函数打开/关闭接口:


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);
}

推荐阅读