[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH 1/2] Write current NetCfgInstanceId before upgrade


  • To: <win-pv-devel@xxxxxxxxxxxxxxxxxxxx>
  • From: Owen Smith <owen.smith@xxxxxxxxxx>
  • Date: Thu, 22 Sep 2022 07:58:05 +0100
  • Authentication-results: esa4.hc3370-68.iphmx.com; dkim=none (message not signed) header.i=none
  • Cc: Owen Smith <owen.smith@xxxxxxxxxx>
  • Delivery-date: Thu, 22 Sep 2022 06:58:32 +0000
  • Ironport-data: A9a23:okf276vwtku5myUFE6qI++Q/b+fnVBVeMUV32f8akzHdYApBsoF/q tZmKWGBaK3ZZGamed0iPdmyoBgC65eAn4NkGwtv+Cs3FHlD+JbJXdiXEBz9bniYRiHhoOOLz Cm8hv3odp1coqr0/0/1WlTZhSAgk/vOHtIQMcacUghpXwhoVSw9vhxqnu89k+ZAjMOwRgiAo rsemeWGULOe82MyYzl8B56r8ks15qyj4mlA5zTSWNgQ1LPgvyhNZH4gDfnZw0vQGuF8AuO8T uDf+7C1lkuxE8AFU47Nfh7TKyXmc5aKVeS8oiM+t5uK23CukhcawKcjXMfwXG8M49m/c3Kd/ /0W3XC4YV9B0qQhA43xWTEAe811FfUuFLMqvRFTGCFcpqHLWyKE/hlgMK05FdQ0wrxwJk98y fIRATQoSx2KpO6L3L3uH4GAhux7RCXqFIYWu3UmxjDFF/c2B5vERs0m5/cBgm123JoXW6+DO YxJMlKDbzyZC/FLElAIBZR4h+KpmnnXeDxEslOF46Ew5gA/ySQhi+a0aIqNILRmQ+1WumbJu l6a0l/GDwgVGf++9T6CzjGF07qncSTTB9tJSezQGuRRqEKSwCkLFlgaWEW2pdG9i1WiQJRPJ koM4C0soKMuskuxQbHAswaQ+SDe+ERGApwJTrN8uFrlJrfoDxixX0Q8FDlmRu4ausYfQRkP2 1aqk+71Cmk62FGKck5x5ot4vBvrZ3ZOczNePnRYJecWy4K9+d9u13ojWv4mSffo1YOtRFkc1 hjQ9EADa6MvYdnnPklR1XTOmHqSq5fAVWbZDS2HDzv+vmuViGNIDrFECGQ3Dt4adu51tnHb4 BA5dzG2tYji962lmi2XW/kqF7q0/fuDOzC0qQcxQcJ+p2n2piH7INk4DNRCyKBBba45lcLBO heP6Wu9GrcKVJdVUUOHS93oUJl7pUQRPd/kSurVfrJzX3SFTyfepXkGWKJl9zqy+KTauf1gZ MzznAfFJSpyNJmLOxLtGLhFgO5ynXpnrY4RLLiipymaPXOlTCb9Yd843JGmN4jVMIvsTN3pz uti
  • Ironport-hdrordr: A9a23:aZUWsKqXb4gXFfkdtQhKig8aV5oTeYIsimQD101hICG8cqSj+f xG+85rsyMc6QxhIE3I9urhBEDtex/hHNtOkOws1NSZLW7bUQmTXeJfBOLZqlWKcUDDH6xmpM NdmsBFeaTN5DNB7PoSjjPWLz9Z+qjkzJyV
  • List-id: Developer list for the Windows PV Drivers subproject <win-pv-devel.lists.xenproject.org>

Write the current NetCfgInstanceId and NetLuid before the upgrade replaces
the driver.
A later patch will change the xenvif!SettingsSave and xenvif!SettingsRestore
to copy the current data, and not cache a copy during xenvif!SettingsSave.
This will pick up any modifications to the network settings made before the
upgrade.

Signed-off-by: Owen Smith <owen.smith@xxxxxxxxxx>
---
 src/coinst/coinst.c | 341 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 341 insertions(+)

diff --git a/src/coinst/coinst.c b/src/coinst/coinst.c
index e749506..dbc21b3 100644
--- a/src/coinst/coinst.c
+++ b/src/coinst/coinst.c
@@ -36,6 +36,7 @@
 #include <strsafe.h>
 #include <malloc.h>
 #include <assert.h>
+#include <iphlpapi.h>
 
 #include <version.h>
 #include <revision.h>
@@ -56,6 +57,9 @@ __user_code;
 #define UNPLUG_KEY \
         SERVICE_KEY(XEN) ## "\\Unplug"
 
+#define SETTINGS_KEY \
+        SERVICE_KEY(XENVIF) ## "\\Settings"
+
 #define CONTROL_KEY "SYSTEM\\CurrentControlSet\\Control"
 
 #define CLASS_KEY   \
@@ -63,6 +67,9 @@ __user_code;
 
 #define ENUM_KEY    "SYSTEM\\CurrentControlSet\\Enum"
 
+#define NETWORK_CLASS_GUID \
+        "{4d36e972-e325-11ce-bfc1-08002be10318}"
+
 static VOID
 #pragma prefast(suppress:6262) // Function uses '1036' bytes of stack: exceeds 
/analyze:stacksize'1024'
 __Log(
@@ -1025,6 +1032,333 @@ fail1:
     return FALSE;
 }
 
+static BOOLEAN
+CreateSettingsKey(
+    IN  PTCHAR  KeyName,
+    OUT PHKEY   Key
+    )
+{
+    HKEY        SettingsKey;
+    HRESULT     Error;
+
+    Error = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
+                         SETTINGS_KEY,
+                         0,
+                         KEY_ALL_ACCESS,
+                         &SettingsKey);
+    if (Error != ERROR_SUCCESS) {
+        SetLastError(Error);
+        goto fail1;
+    }
+
+    Error = RegCreateKeyEx(SettingsKey,
+                           KeyName,
+                           0,
+                           NULL,
+                           REG_OPTION_NON_VOLATILE,
+                           KEY_ALL_ACCESS,
+                           NULL,
+                           Key,
+                           NULL);
+    if (Error != ERROR_SUCCESS) {
+        SetLastError(Error);
+        goto fail2;
+    }
+
+    RegCloseKey(SettingsKey);
+
+    return TRUE;
+
+fail2:
+    Log("fail2");
+
+    RegCloseKey(SettingsKey);
+
+fail1:
+    Error = GetLastError();
+
+    {
+        PTCHAR  Message;
+
+        Message = GetErrorMessage(Error);
+        Log("fail1 (%s)", Message);
+        LocalFree(Message);
+    }
+
+    return FALSE;
+}
+
+static BOOLEAN
+CopyNetworkID(
+    IN  PTCHAR  VifIndex,
+    IN  HKEY    DriverKey
+    )
+{
+    BOOLEAN     Success;
+    HRESULT     Error;
+    TCHAR       NetCfgInstanceId[MAX_PATH];
+    DWORD       NetCfgInstanceIdLength;
+    DWORD       Value;
+    DWORD       ValueLength;
+    NET_LUID    NetLuid;
+    HKEY        SettingsKey;
+
+    NetCfgInstanceIdLength = sizeof(NetCfgInstanceId);
+    Error = RegQueryValueEx(DriverKey,
+                            "NetCfgInstanceId",
+                            NULL,
+                            NULL,
+                            (LPBYTE)NetCfgInstanceId,
+                            &NetCfgInstanceIdLength);
+    if (Error != ERROR_SUCCESS) {
+        SetLastError(Error);
+        goto fail1;
+    }
+
+    ValueLength = sizeof(DWORD);
+    Error = RegQueryValueEx(DriverKey,
+                            "NetLuidIndex",
+                            NULL,
+                            NULL,
+                            (LPBYTE)&Value,
+                            &ValueLength);
+    if (Error != ERROR_SUCCESS) {
+        SetLastError(Error);
+        goto fail2;
+    }
+
+    NetLuid.Info.NetLuidIndex = Value;
+
+    ValueLength = sizeof(DWORD);
+    Error = RegQueryValueEx(DriverKey,
+                            "*IfType",
+                            NULL,
+                            NULL,
+                            (LPBYTE)&Value,
+                            &ValueLength);
+    if (Error != ERROR_SUCCESS) {
+        SetLastError(Error);
+        goto fail3;
+    }
+
+    NetLuid.Info.IfType = Value;
+
+    Log("VIF/%s was %s", VifIndex, NetCfgInstanceId);
+
+    Success = CreateSettingsKey(VifIndex, &SettingsKey);
+    if (!Success)
+        goto fail4;
+
+    Error = RegSetValueEx(SettingsKey,
+                          "NetCfgInstanceId",
+                          0,
+                          REG_SZ,
+                          (const BYTE*)NetCfgInstanceId,
+                          NetCfgInstanceIdLength);
+    if (Error != ERROR_SUCCESS) {
+        SetLastError(Error);
+        goto fail5;
+    }
+
+    Error = RegSetValueEx(SettingsKey,
+                          "NetLuid",
+                          0,
+                          REG_BINARY,
+                          (const BYTE*)&NetLuid,
+                          sizeof(NET_LUID));
+    if (Error != ERROR_SUCCESS) {
+        SetLastError(Error);
+        goto fail6;
+    }
+
+    Value = 1;
+    Error = RegSetValueEx(SettingsKey,
+                          "HasSettings",
+                          0,
+                          REG_DWORD,
+                          (const BYTE*)&Value,
+                          sizeof(DWORD));
+    if (Error != ERROR_SUCCESS) {
+        SetLastError(Error);
+        goto fail7;
+    }
+
+    RegCloseKey(SettingsKey);
+
+    return TRUE;
+
+fail7:
+    Log("fail7");
+
+fail6:
+    Log("fail6");
+
+fail5:
+    Log("fail5");
+
+    RegCloseKey(SettingsKey);
+
+fail4:
+    Log("fail4");
+
+fail3:
+    Log("fail3");
+
+fail2:
+    Log("fail2");
+
+fail1:
+    Error = GetLastError();
+
+    {
+        PTCHAR  Message;
+
+        Message = GetErrorMessage(Error);
+        Log("fail1 (%s)", Message);
+        LocalFree(Message);
+    }
+
+    return FALSE;
+}
+
+static BOOLEAN
+CopyNetworkIDs(
+    VOID
+    )
+{
+    BOOLEAN     Success;
+    HRESULT     Error;
+    HKEY        ClassKey;
+    DWORD       SubKeys;
+    DWORD       MaxSubKeyLength;
+    DWORD       Index;
+    DWORD       SubKeyLength;
+    PTCHAR      SubKeyName;
+
+    Log("====>");
+
+    Success = OpenDriverKey(NETWORK_CLASS_GUID, &ClassKey);
+    if (!Success)
+        goto fail1;
+
+    Error = RegQueryInfoKey(ClassKey,
+                            NULL,
+                            NULL,
+                            NULL,
+                            &SubKeys,
+                            &MaxSubKeyLength,
+                            NULL,
+                            NULL,
+                            NULL,
+                            NULL,
+                            NULL,
+                            NULL);
+    if (Error != ERROR_SUCCESS) {
+        SetLastError(Error);
+        goto fail2;
+    }
+
+    SubKeyLength = MaxSubKeyLength + sizeof (TCHAR);
+
+    SubKeyName = malloc(SubKeyLength);
+    if (SubKeyName == NULL)
+        goto fail3;
+
+    for (Index = 0; Index < SubKeys; ++Index) {
+        HKEY        DriverKey;
+        TCHAR       DeviceInstanceID[MAX_PATH];
+        PTCHAR      VifIndex;
+        DWORD       Length;
+
+        SubKeyLength = MaxSubKeyLength + sizeof (TCHAR);
+        memset(SubKeyName, 0, SubKeyLength);
+
+        Error = RegEnumKeyEx(ClassKey,
+                             Index,
+                             (LPTSTR)SubKeyName,
+                             &SubKeyLength,
+                             NULL,
+                             NULL,
+                             NULL,
+                             NULL);
+        if (Error != ERROR_SUCCESS) {
+            SetLastError(Error);
+            goto fail4;
+        }
+
+        Error = RegOpenKeyEx(ClassKey,
+                             SubKeyName,
+                             0,
+                             KEY_READ,
+                             &DriverKey);
+        if (Error != ERROR_SUCCESS) {
+            SetLastError(Error);
+            continue;
+        }
+
+        Length = sizeof(DeviceInstanceID);
+        Error = RegQueryValueEx(DriverKey,
+                                "DeviceInstanceID",
+                                NULL,
+                                NULL,
+                                (LPBYTE)DeviceInstanceID,
+                                &Length);
+        if (Error != ERROR_SUCCESS) {
+            SetLastError(Error);
+            goto loop1;
+        }
+
+        if (_strnicmp("XENVIF", DeviceInstanceID, strlen("XENVIF")) != 0)
+            goto loop2;
+
+        VifIndex = strrchr(DeviceInstanceID, '\\');
+        if (VifIndex == NULL)
+            goto loop3;
+
+        ++VifIndex;
+
+        CopyNetworkID(VifIndex, DriverKey);
+ 
+    loop3:
+    loop2:
+    loop1:
+        RegCloseKey(DriverKey);
+    }
+
+    free(SubKeyName);
+    RegCloseKey(ClassKey);
+
+    Log("<====");
+
+    return TRUE;
+
+fail4:
+    Log("fail4");
+
+    free(SubKeyName);
+
+fail3:
+    Log("fail3");
+
+fail2:
+    Log("fail2");
+
+    RegCloseKey(ClassKey);
+
+fail1:
+    Error = GetLastError();
+
+    {
+        PTCHAR  Message;
+
+        Message = GetErrorMessage(Error);
+        Log("fail1 (%s)", Message);
+        LocalFree(Message);
+    }
+
+    return FALSE;
+}
+
 static HRESULT
 DifInstallPreProcess(
     IN  HDEVINFO                    DeviceInfoSet,
@@ -1055,10 +1389,17 @@ DifInstallPreProcess(
     if (!Success)
         goto fail3;
 
+    Success = CopyNetworkIDs();
+    if (!Success)
+        goto fail4;
+
     Log("<====");
 
     return NO_ERROR;
 
+fail4:
+    Log("fail4");
+
 fail3:
     Log("fail3");
 
-- 
2.32.0.windows.1




 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.