From patchwork Mon Oct 8 23:27:16 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alin-Gabriel Serdean X-Patchwork-Id: 980926 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=openvswitch.org (client-ip=140.211.169.12; helo=mail.linuxfoundation.org; envelope-from=ovs-dev-bounces@openvswitch.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=ovn.org Received: from mail.linuxfoundation.org (mail.linuxfoundation.org [140.211.169.12]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 42Tc3Z2LqFz9vZs for ; Tue, 9 Oct 2018 10:27:54 +1100 (AEDT) Received: from mail.linux-foundation.org (localhost [127.0.0.1]) by mail.linuxfoundation.org (Postfix) with ESMTP id 8483A7441; Mon, 8 Oct 2018 23:27:47 +0000 (UTC) X-Original-To: dev@openvswitch.org Delivered-To: ovs-dev@mail.linuxfoundation.org Received: from smtp1.linuxfoundation.org (smtp1.linux-foundation.org [172.17.192.35]) by mail.linuxfoundation.org (Postfix) with ESMTPS id 4F906734E for ; Mon, 8 Oct 2018 23:27:27 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from relay3-d.mail.gandi.net (relay3-d.mail.gandi.net [217.70.183.195]) by smtp1.linuxfoundation.org (Postfix) with ESMTPS id 637DF784 for ; Mon, 8 Oct 2018 23:27:26 +0000 (UTC) X-Originating-IP: 89.46.161.178 Received: from localhost.localdomain (unknown [89.46.161.178]) (Authenticated sender: aserdean@ovn.org) by relay3-d.mail.gandi.net (Postfix) with ESMTPSA id 6F61260004; Mon, 8 Oct 2018 23:27:24 +0000 (UTC) From: Alin Gabriel Serdean To: dev@openvswitch.org Date: Tue, 9 Oct 2018 02:27:16 +0300 Message-Id: <20181008232716.28296-1-aserdean@ovn.org> X-Mailer: git-send-email 2.16.1.windows.1 X-Spam-Status: No, score=-2.6 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_LOW autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on smtp1.linux-foundation.org Cc: Alin Gabriel Serdean Subject: [ovs-dev] [RFC PATCH v1 3/3] windows: Allow add/delete ports via HNS API X-BeenThere: ovs-dev@openvswitch.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: ovs-dev-bounces@openvswitch.org Errors-To: ovs-dev-bounces@openvswitch.org On Windows 2016 LTSC(RTM) the Container feature and Hyper-V feature both had DCOM API to add and delete internal (management) ports on the Hyper-V switch. Starting from the 1703 release and above, enabling only the Container feature does not fulfil this requirement anymore. We need new ways to interact with the host API without the Hyper-V integration. Unfortunately, there is no C API for it, only golang and .net: https://github.com/Microsoft/hcsshim (golang) https://github.com/microsoft/dotnet-computevirtualization (.net) It is also poorly documented and reserved. Although not pretty and less performant, this will provide a way to add/delete and query the new APIs across different versions of Windows, until new API's or documentation are available. Signed-off-by: Alin Gabriel Serdean --- lib/wmi.c | 95 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 91 insertions(+), 4 deletions(-) diff --git a/lib/wmi.c b/lib/wmi.c index e6dc63cde..14451d591 100644 --- a/lib/wmi.c +++ b/lib/wmi.c @@ -203,6 +203,93 @@ wait_for_job(IWbemServices *psvc, wchar_t *job_path) return retval; } +static boolean +hns_delete(char *name) +{ + VLOG_DBG("Trying to delete port via HNS API"); + char buffer[10000]; + int count; + count = snprintf(buffer, 10000, "-NoLogo -NoProfile -NonInteractive" + "-Command \"try {Delete-OVSHNSInternalPort '%s'}" + "catch { exit 1 }; exit 0; \"", name); + if (count < 0 || count > 10000) { + VLOG_WARN("Could not allocate memory for powershell delete command"); + return false; + } + VLOG_DBG("command = %s", buffer); + DWORD res = 0; + SHELLEXECUTEINFOA ShExecInfo; + ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO); + ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS; + ShExecInfo.hwnd = NULL; + ShExecInfo.lpVerb = NULL; + ShExecInfo.lpFile = "powershell.exe"; + ShExecInfo.lpParameters = buffer; + ShExecInfo.lpDirectory = NULL; + ShExecInfo.nShow = SW_HIDE; + ShExecInfo.hInstApp = NULL; + ShellExecuteExA(&ShExecInfo); + WaitForSingleObject(ShExecInfo.hProcess, 50000); + if (GetExitCodeProcess(ShExecInfo.hProcess, &res)) { + if (res != 0) { + VLOG_ERR("Powershell delete command failed with exit code: %d", + res); + CloseHandle(ShExecInfo.hProcess); + return false; + } + } else { + VLOG_ERR("Failed to get exit code for powershell delete command." + "Last Error: %s", ovs_lasterror_to_string()); + CloseHandle(ShExecInfo.hProcess); + return false; + } + CloseHandle(ShExecInfo.hProcess); + return true; +} + +static boolean +hns_add(char *name) +{ + VLOG_WARN("Trying to add port via HNS API"); + char buffer[10000]; + int count; + count = snprintf(buffer, 10000, "-NoLogo -NoProfile -NonInteractive" + "-Command \"try {Add-OVSHNSInternalPort '%s'}" + "catch { exit 1 }; exit 0; \"", name); + if (count < 0 || count > 10000) { + VLOG_WARN("Could not allocate memory for powershell add command"); + return false; + } + VLOG_WARN("command = %s", buffer); + DWORD res = 0; + SHELLEXECUTEINFOA ShExecInfo; + ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO); + ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS; + ShExecInfo.hwnd = NULL; + ShExecInfo.lpVerb = NULL; + ShExecInfo.lpFile = "powershell.exe"; + ShExecInfo.lpParameters = buffer; + ShExecInfo.lpDirectory = NULL; + ShExecInfo.nShow = SW_HIDE; + ShExecInfo.hInstApp = NULL; + ShellExecuteExA(&ShExecInfo); + WaitForSingleObject(ShExecInfo.hProcess, 50000); + if (GetExitCodeProcess(ShExecInfo.hProcess, &res)) { + if (res != 0) { + VLOG_ERR("Powershell add command failed with exit code: %d", res); + CloseHandle(ShExecInfo.hProcess); + return false; + } + } else { + VLOG_ERR("Failed to get exit code for powershell add command." + "Last Error: %s", ovs_lasterror_to_string()); + CloseHandle(ShExecInfo.hProcess); + return false; + } + CloseHandle(ShExecInfo.hProcess); + return true; +} + /* This function will initialize DCOM retrieving the WMI locator's ploc and * the context associated to it. */ static boolean @@ -390,14 +477,14 @@ delete_wmi_port(char *name) if (!initialize_wmi(&ploc, &pcontext)) { VLOG_WARN("Could not initialize DCOM"); - retval = false; + retval = hns_delete(name); goto error; } if (!connect_set_security(ploc, pcontext, L"Root\\Virtualization\\v2", &psvc)) { VLOG_WARN("Could not connect and set security for virtualization"); - retval = false; + retval = hns_delete(name); goto error; } @@ -673,14 +760,14 @@ create_wmi_port(char *name) { if (!initialize_wmi(&ploc, &pcontext)) { VLOG_WARN("Could not initialize DCOM"); - retval = false; + retval = hns_add(name); goto error; } if (!connect_set_security(ploc, pcontext, L"Root\\Virtualization\\v2", &psvc)) { VLOG_WARN("Could not connect and set security for virtualization"); - retval = false; + retval = hns_add(name); goto error; }