From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id C64D142674; Fri, 29 Sep 2023 18:36:24 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 8A10740273; Fri, 29 Sep 2023 18:36:24 +0200 (CEST) Received: from mail-pf1-f178.google.com (mail-pf1-f178.google.com [209.85.210.178]) by mails.dpdk.org (Postfix) with ESMTP id 38DDB4003C for ; Fri, 29 Sep 2023 18:36:23 +0200 (CEST) Received: by mail-pf1-f178.google.com with SMTP id d2e1a72fcca58-692b2bdfce9so11321216b3a.3 for ; Fri, 29 Sep 2023 09:36:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=networkplumber-org.20230601.gappssmtp.com; s=20230601; t=1696005382; x=1696610182; darn=dpdk.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=568u6rBRPGN7dbiNxpWASug5G2uBIp9VMOevfRSc6WI=; b=0HEaYY5FR4pAZdXKz3lWWc/Mxh9+qu7sC59k1Lw12lpMN89KJR86xHr7feARKhvtWU RUOGKGBZLcRHjk7nWGonYLM+SKipTjHgnvj5N/kO4vSJBIsDw1hWPomkbn8A3VanQNxf 2wnlzD+pS6UW7hZJcP3OEGQc+g1ZxUAs5bYY6Ij89Q8fm1CuT7e+Iyoxx+Fsx+C7PXI/ BGmPYcAf9cgftBIPKz6SvpIM1Aq2sFIaQG7J8D4uqEh9ywtdVLkoahPl7DKCb6F9PatH /OPOImysEcgPzrxAWIiJJApDoF4A3W4izKE9hIunj5LXDirWQHEAtqemCP7fbNnu29Aa ul6A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696005382; x=1696610182; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=568u6rBRPGN7dbiNxpWASug5G2uBIp9VMOevfRSc6WI=; b=hZh7kWH6x7l4bpoHBtmzY7hRdr0m0xAn07ZkDIuG+AlrW1wlvTlYlJ44TEwxUvMNfS dYbbzEjEUXdbnqLMYt+eMos7v1DGyHvSjl/9iOLS3DeVh6D7K1kDcJIt744u3eSgPV6m G2Esl8jh/MWCnC3v144Lb8v4yWmwr15W8MHshWkwW+HNspYJJi4oDgtKQHEeO2sZAR8s kpjS5A07I/EZwk72Ywmlco0QJ7aYLQuWykaEKyU7dKF62wLT00y30qGgHTm1ASNUrpTM 8MDHAhhM9onAJIYJE+Wvl9PtaWe6W7LMf+4aFJJK6n/3hvOtWmY16Pa8UXa0mphi6ESG ocIg== X-Gm-Message-State: AOJu0Yy8O5lXwkjZ6BKnaB/ZZmMMxnOTgCTIPjiufXHqLEiG0WGxgU7c NLtfPGQp6HQJeTtqOJvmdckRzcyytmOEFCYoAFg= X-Google-Smtp-Source: AGHT+IGVly+unVPQWEfqXvNtLm7vqGv/84oh2Q1yCti8HPU9PsJefzb9Ppu439l4Jc3DMxWsZTON9Q== X-Received: by 2002:a05:6a20:258a:b0:153:56b1:8417 with SMTP id k10-20020a056a20258a00b0015356b18417mr5530815pzd.21.1696005381929; Fri, 29 Sep 2023 09:36:21 -0700 (PDT) Received: from hermes.local (204-195-126-68.wavecable.com. [204.195.126.68]) by smtp.gmail.com with ESMTPSA id s22-20020a62e716000000b0069023d80e63sm15088435pfh.25.2023.09.29.09.36.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 29 Sep 2023 09:36:21 -0700 (PDT) From: Stephen Hemminger To: dev@dpdk.org Cc: Stephen Hemminger , Olivier Matz Subject: [RFC] rte_ether_unformat: accept more inputs Date: Fri, 29 Sep 2023 09:36:11 -0700 Message-Id: <20230929163611.62691-1-stephen@networkplumber.org> X-Mailer: git-send-email 2.39.2 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org This updates rte_ether_addr_unformat() to accept more types of input. There have been requests to handle Windows and other formats. Signed-off-by: Stephen Hemminger --- Marking this as RFC until unit tests are added. lib/net/rte_ether.c | 78 +++++++++++++++++++++++++++++++++++---------- lib/net/rte_ether.h | 6 ++-- 2 files changed, 66 insertions(+), 18 deletions(-) diff --git a/lib/net/rte_ether.c b/lib/net/rte_ether.c index 66d9a9d0699a..5250353eb162 100644 --- a/lib/net/rte_ether.c +++ b/lib/net/rte_ether.c @@ -38,7 +38,8 @@ static int8_t get_xdigit(char ch) } /* Convert 00:11:22:33:44:55 to ethernet address */ -static bool get_ether_addr6(const char *s0, struct rte_ether_addr *ea) +static bool get_ether_addr6(const char *s0, struct rte_ether_addr *ea, + const char sep) { const char *s = s0; int i; @@ -50,14 +51,17 @@ static bool get_ether_addr6(const char *s0, struct rte_ether_addr *ea) if (x < 0) return false; - ea->addr_bytes[i] = x << 4; - x = get_xdigit(*s++); - if (x < 0) - return false; - ea->addr_bytes[i] |= x; + ea->addr_bytes[i] = x; + if (*s != sep) { + x = get_xdigit(*s++); + if (x < 0) + return false; + ea->addr_bytes[i] <<= 4; + ea->addr_bytes[i] |= x; + } if (i < RTE_ETHER_ADDR_LEN - 1 && - *s++ != ':') + *s++ != sep) return false; } @@ -66,7 +70,8 @@ static bool get_ether_addr6(const char *s0, struct rte_ether_addr *ea) } /* Convert 0011:2233:4455 to ethernet address */ -static bool get_ether_addr3(const char *s, struct rte_ether_addr *ea) +static bool get_ether_addr3(const char *s, struct rte_ether_addr *ea, + const char sep) { int i, j; @@ -80,12 +85,14 @@ static bool get_ether_addr3(const char *s, struct rte_ether_addr *ea) if (x < 0) return false; w = (w << 4) | x; + if (*s == sep) + break; } ea->addr_bytes[i] = w >> 8; ea->addr_bytes[i + 1] = w & 0xff; if (i < RTE_ETHER_ADDR_LEN - 2 && - *s++ != ':') + *s++ != sep) return false; } @@ -93,17 +100,56 @@ static bool get_ether_addr3(const char *s, struct rte_ether_addr *ea) } /* - * Like ether_aton_r but can handle either - * XX:XX:XX:XX:XX:XX or XXXX:XXXX:XXXX - * and is more restrictive. + * Scan input to see if seperated by dash, colon or period + * Returns seperator and number of matches + * If seperators are mixed will return + */ +static unsigned int get_ether_sep(const char *s, char *sep) +{ + const char seperators[] = "-:."; + unsigned int count = 0; + const char *cp; + + cp = strpbrk(s, seperators); + if (cp == NULL) + return 0; + + *sep = *cp; + do { + ++count; + /* find next instance of seperator */ + cp = strchr(cp + 1, *sep); + } while (cp != NULL); + + return count; +} + +/* + * Be libreal in accepting a wide variety of notational formats + * for MAC address including: + * - Linux format six groups of hexadecimal digits seperated by colon + * - Windows format six groups seperated by hyphen + * - two groups hexadecimal digits */ int rte_ether_unformat_addr(const char *s, struct rte_ether_addr *ea) { - if (get_ether_addr6(s, ea)) - return 0; - if (get_ether_addr3(s, ea)) - return 0; + unsigned int count; + char sep = '\0'; + + count = get_ether_sep(s, &sep); + switch (count) { + case 5: /* i.e 01:23:45:67:89:AB */ + if (get_ether_addr6(s, ea, sep)) + return 0; + break; + case 2: /* i.e 0123.4567.89AB */ + if (get_ether_addr3(s, ea, sep)) + return 0; + break; + default: + break; + } rte_errno = EINVAL; return -1; diff --git a/lib/net/rte_ether.h b/lib/net/rte_ether.h index b35c72c7b0e0..e9a4ba9b5860 100644 --- a/lib/net/rte_ether.h +++ b/lib/net/rte_ether.h @@ -254,8 +254,10 @@ rte_ether_format_addr(char *buf, uint16_t size, * * @param str * A pointer to buffer contains the formatted MAC address. - * The supported formats are: - * XX:XX:XX:XX:XX:XX or XXXX:XXXX:XXXX + * The example formats are: + * XX:XX:XX:XX:XX:XX - Canonical form + * XX-XX-XX-XX-XX-XX - Windows and IEEE 802 + * XXXX:XXXX:XXXX - original DPDK * where XX is a hex digit: 0-9, a-f, or A-F. * @param eth_addr * A pointer to a ether_addr structure. -- 2.39.2