Received: by 10.223.185.116 with SMTP id b49csp1618669wrg; Sun, 11 Feb 2018 16:54:10 -0800 (PST) X-Google-Smtp-Source: AH8x226JmnWb6N7H5xorI+D4RzCl4F1KhOqd6L8QEaCqWqPxFsE30DdNi4oiyjRlM9JSb/C8B+hP X-Received: by 2002:a17:902:461:: with SMTP id 88-v6mr747632ple.88.1518396850015; Sun, 11 Feb 2018 16:54:10 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1518396849; cv=none; d=google.com; s=arc-20160816; b=ArpV36wA/DzE3iiuUVgH//2uzUC94RUl/n7bYMQ0WCbR0k26PiABEG0z0XuXVCeSJk +OfswxrnVBGfoLmjT5bsrndpdTjM+AuBQ4qtFoRalBWs06iPChVnz8CQSNOF9e2rjmee G0zdsyCKX4vRjIZTPFm/ZicrRAZcK06SjjY53cSuc4ydWtOg9mx5U8C7Qe0DF8pGz8c5 smqgGCEoZJWZ9Lolx2YAXm+QufQ0fGSZ+OYa2VfLuVCt2T84K05xYb+B4LgAWLGc+342 FtZY7bdYoTgZ2AVHS+HJl9drjXsBBTDiwtoy2qJ3jHpE1H8rP2bLOqxnbOaKj2lAtUKT h3Fw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:reply-to:references:in-reply-to :message-id:date:subject:cc:to:from:arc-authentication-results; bh=R3kHaS3UHzeaKjpV5WqNVUXxku0yrrCv8xbl67XnXSc=; b=YIPGvNGozWbDfMOLwBc8V3me2oLkBV2XAh+1O0V2RWAHW9QzD0pz+NK6HBgsB9t+XC N4RM0hapDPd3hFMP2NAdHDuOo2n4EDWUsdkx8CKCo7q27HKUffv/A5jJ0+2cGkQHKNp2 w5nUfbdMEUiR+bI2uOR4e0QsG/T5yMLfoMb1qw+8pYwhiZVLbGOLXLEX5UWxUgUqnyjL +CVbQE3V4DC4s4NWwUTalk49W0ENaffOd/+8kHhzkMSlzTV4aPCk/bPHQn8UxWYyy8E7 RzqmXCqKI5FKuS+LNdQIg84Ke3YW1Jx3VymHbkFxGAdKqq8OCZAm1a8ChqTsyK9Grxi8 LM7Q== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=exchange.microsoft.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id a10si4490416pgq.154.2018.02.11.16.53.55; Sun, 11 Feb 2018 16:54:09 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=exchange.microsoft.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932445AbeBLArZ (ORCPT + 99 others); Sun, 11 Feb 2018 19:47:25 -0500 Received: from a2nlsmtp01-04.prod.iad2.secureserver.net ([198.71.225.38]:43066 "EHLO a2nlsmtp01-04.prod.iad2.secureserver.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932273AbeBLApw (ORCPT ); Sun, 11 Feb 2018 19:45:52 -0500 Received: from linuxonhyperv2.linuxonhyperv.com ([107.180.71.197]) by : HOSTING RELAY : with SMTP id l23seahoMDLOzl23seeD4E; Sun, 11 Feb 2018 17:33:36 -0700 x-originating-ip: 107.180.71.197 Received: from kys by linuxonhyperv2.linuxonhyperv.com with local (Exim 4.89_1) (envelope-from ) id 1el23s-0001ll-0m; Sun, 11 Feb 2018 17:33:36 -0700 From: kys@exchange.microsoft.com To: gregkh@linuxfoundation.org, linux-kernel@vger.kernel.org, devel@linuxdriverproject.org, olaf@aepfle.de, apw@canonical.com, vkuznets@redhat.com, jasowang@redhat.com, leann.ogasawara@canonical.com, marcelo.cerri@canonical.com, sthemmin@microsoft.com Cc: Haiyang Zhang , "K . Y . Srinivasan" Subject: [PATCH 01/12] tools/hv: Fix IP reporting by KVP daemon with SRIOV Date: Sun, 11 Feb 2018 17:33:09 -0700 Message-Id: <20180212003320.6748-1-kys@exchange.microsoft.com> X-Mailer: git-send-email 2.15.1 In-Reply-To: <20180212002958.6679-1-kys@exchange.microsoft.com> References: <20180212002958.6679-1-kys@exchange.microsoft.com> Reply-To: kys@microsoft.com X-CMAE-Envelope: MS4wfHWyA3bWQpJbWP7paxsWZ4WiegLdr/MCrqMhpnf7IdDAdEuAh6/GiOfoWsqrWthZGN1Q6iHsE4nVKVe2t61vUyTUCdWp7KO9aUtm4q3xEZB1IdJQoC0D 2oTVvnveXmhDvGMxe6D6fuzerbUchQHVpcunCNRdpeZyU4xBkwQ1/yGrHDoxlbuyOEwsTyIIYJRCUqOttaL5irOuowXp3w5eJi1t6VJgGwDat1KRPp2wXSOj He5Y63QB9a6Pr8lR3Sy/i9cJqUdSYbR81pLpAN2y2zVZMM83tQU1cbU5janAwD+DRFCx16lT6VI/dZE1mnvZ7PvDuW/EmtXy9Gfo1zY6pfPZcG+rRlL/tb6Y /bgkUkTP1iwxzxSqzx2ToE17S9yjmMy73cky3eFQZhM8kI3pNf6dFRPfSjNKmmrTz0varQUIXWUkDbyBUOh/3W4uzlqsO/cQItLP7oA/92mQ9LhfOG54jW/p FhrIfPDKsVjddr/rlVgx3YfYyFzcMuv9+zaTnIFWvFdWcr2NmfJOBG5+5ZFmNiJS8tpvTVYTPZDt0CR6IZLB99ii4WToQFZWB7ClhSVBJv8pHU1co1zntTw2 dW881tHKiPOqxGT4Q1D+i4uF Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Haiyang Zhang On Hyper-V the VF NIC has the same MAC as the related synthetic NIC. VF NIC can work under the synthetic NIC transparently, without its own IP address. The existing KVP daemon only gets IP from the first NIC matching a MAC address, and may not be able to find the IP in this case. This patch fixes the problem by searching the NIC matching the MAC, and having an IP address. So, the IP address will be found and reported to the host successfully. Signed-off-by: Haiyang Zhang Signed-off-by: K. Y. Srinivasan --- tools/hv/hv_kvp_daemon.c | 138 ++++++++++++++++++++++------------------------- 1 file changed, 65 insertions(+), 73 deletions(-) diff --git a/tools/hv/hv_kvp_daemon.c b/tools/hv/hv_kvp_daemon.c index 4c99c57736ce..dbf6e8bd98ba 100644 --- a/tools/hv/hv_kvp_daemon.c +++ b/tools/hv/hv_kvp_daemon.c @@ -634,64 +634,6 @@ static char *kvp_if_name_to_mac(char *if_name) return mac_addr; } - -/* - * Retrieve the interface name given tha MAC address. - */ - -static char *kvp_mac_to_if_name(char *mac) -{ - DIR *dir; - struct dirent *entry; - FILE *file; - char *p, *x; - char *if_name = NULL; - char buf[256]; - char dev_id[PATH_MAX]; - unsigned int i; - - dir = opendir(KVP_NET_DIR); - if (dir == NULL) - return NULL; - - while ((entry = readdir(dir)) != NULL) { - /* - * Set the state for the next pass. - */ - snprintf(dev_id, sizeof(dev_id), "%s%s/address", KVP_NET_DIR, - entry->d_name); - - file = fopen(dev_id, "r"); - if (file == NULL) - continue; - - p = fgets(buf, sizeof(buf), file); - if (p) { - x = strchr(p, '\n'); - if (x) - *x = '\0'; - - for (i = 0; i < strlen(p); i++) - p[i] = toupper(p[i]); - - if (!strcmp(p, mac)) { - /* - * Found the MAC match; return the interface - * name. The caller will free the memory. - */ - if_name = strdup(entry->d_name); - fclose(file); - break; - } - } - fclose(file); - } - - closedir(dir); - return if_name; -} - - static void kvp_process_ipconfig_file(char *cmd, char *config_buf, unsigned int len, int element_size, int offset) @@ -997,6 +939,70 @@ kvp_get_ip_info(int family, char *if_name, int op, return error; } +/* + * Retrieve the IP given the MAC address. + */ +static int kvp_mac_to_ip(struct hv_kvp_ipaddr_value *kvp_ip_val) +{ + char *mac = (char *)kvp_ip_val->adapter_id; + DIR *dir; + struct dirent *entry; + FILE *file; + char *p, *x; + char *if_name = NULL; + char buf[256]; + char dev_id[PATH_MAX]; + unsigned int i; + int error = HV_E_FAIL; + + dir = opendir(KVP_NET_DIR); + if (dir == NULL) + return HV_E_FAIL; + + while ((entry = readdir(dir)) != NULL) { + /* + * Set the state for the next pass. + */ + snprintf(dev_id, sizeof(dev_id), "%s%s/address", KVP_NET_DIR, + entry->d_name); + + file = fopen(dev_id, "r"); + if (file == NULL) + continue; + + p = fgets(buf, sizeof(buf), file); + fclose(file); + if (!p) + continue; + + x = strchr(p, '\n'); + if (x) + *x = '\0'; + + for (i = 0; i < strlen(p); i++) + p[i] = toupper(p[i]); + + if (strcmp(p, mac)) + continue; + + /* + * Found the MAC match. + * A NIC (e.g. VF) matching the MAC, but without IP, is skipped. + */ + if_name = entry->d_name; + if (!if_name) + continue; + + error = kvp_get_ip_info(0, if_name, KVP_OP_GET_IP_INFO, + kvp_ip_val, MAX_IP_ADDR_SIZE * 2); + + if (!error && strlen((char *)kvp_ip_val->ip_addr)) + break; + } + + closedir(dir); + return error; +} static int expand_ipv6(char *addr, int type) { @@ -1472,26 +1478,12 @@ int main(int argc, char *argv[]) switch (op) { case KVP_OP_GET_IP_INFO: kvp_ip_val = &hv_msg->body.kvp_ip_val; - if_name = - kvp_mac_to_if_name((char *)kvp_ip_val->adapter_id); - if (if_name == NULL) { - /* - * We could not map the mac address to an - * interface name; return error. - */ - hv_msg->error = HV_E_FAIL; - break; - } - error = kvp_get_ip_info( - 0, if_name, KVP_OP_GET_IP_INFO, - kvp_ip_val, - (MAX_IP_ADDR_SIZE * 2)); + error = kvp_mac_to_ip(kvp_ip_val); if (error) hv_msg->error = error; - free(if_name); break; case KVP_OP_SET_IP_INFO: -- 2.15.1