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 6E5B4426A5; Mon, 2 Oct 2023 20:37:55 +0200 (CEST) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 59392402E6; Mon, 2 Oct 2023 20:37:46 +0200 (CEST) Received: from mail-pl1-f172.google.com (mail-pl1-f172.google.com [209.85.214.172]) by mails.dpdk.org (Postfix) with ESMTP id 0BC5E40262 for ; Mon, 2 Oct 2023 20:37:44 +0200 (CEST) Received: by mail-pl1-f172.google.com with SMTP id d9443c01a7336-1c60a514f3aso821125ad.3 for ; Mon, 02 Oct 2023 11:37:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=networkplumber-org.20230601.gappssmtp.com; s=20230601; t=1696271863; x=1696876663; darn=dpdk.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=QgzAUgb634wFXn1aAuxin4P9Z47Vd+GEEggy/N4IhWE=; b=jfbgTg/iA4FPfJVNi+fP1OQi7E1drPCFWfYIquDvclM78NX2hG0o+v/ICcJy6mE+04 oxbMLXK8bPmSIxJ+hQpMucp42A2F+2sJw4Y/tWgzRs6P/C91KO/RbdSGsgvvqeJz22Xo JzUCIF6dvSIOVGu+pP+ZLxuXmIMegMit85G9/oOcxkWJ/DX0qR7US2gopkF9AftvdSF7 oo8o6iEApn91tNb/NdVjcozYj3QlbH3/BFwUfGRd7Ogq6OX4RCuEi4JbiCbfOhPN5cL7 w7Nw7CmAsCd3tpmvRUEAaeNyEQPWoOeccdd2HaLhoS3SF8zV58D2bxLCFSMEB0q7bJEd Xs9A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696271863; x=1696876663; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=QgzAUgb634wFXn1aAuxin4P9Z47Vd+GEEggy/N4IhWE=; b=sNw+iA0Jnf+QvDsmF1Y0MShNReVzdzlpL8+mOstdjYcshQTIGOO4bW1gPMMxHfjaLE aYTXqNJm5tQEejh23BeWWFp6DssvLToY55LbeJAy0zxtIB49LHugI8CmUjvrn5vgDkF3 fAKJE4+0vbbXL3DKDf8qKsffhHV6CbFrEXaO96AvG0lVX9O6sz4cq890H+q+MjRJTkjR x8BvgHjpWKXMUvwWW6VJA3+bRjPKTY8waFcQmHfgxQ0m/gIbMU0kxF15BWD8SNMs8/AA PHXXckSGzV03lKIPB5pxBracMxoAATvXsV7Y/LiT895hbnD66Hf3B/4Q3gBxKDgwhgfo Nbiw== X-Gm-Message-State: AOJu0YwqH2HSyAz0cGPIqoevbyQdW+TFcLruThkXXYXSA1DlEwB3o/P9 oHHBoe9rwQvVJ+TIxTZQ/hSMAGgas1UeFloiUFs= X-Google-Smtp-Source: AGHT+IEe6+4Ol3g56LCp7oxLah5PIpUS2/7gEd0hQODi7K1v1EORy1CQMGAow5mREcFudMC+JI2Srw== X-Received: by 2002:a17:903:26c6:b0:1b8:66f6:87a3 with SMTP id jg6-20020a17090326c600b001b866f687a3mr11146073plb.52.1696271862881; Mon, 02 Oct 2023 11:37:42 -0700 (PDT) Received: from hermes.local (204-195-126-68.wavecable.com. [204.195.126.68]) by smtp.gmail.com with ESMTPSA id iz7-20020a170902ef8700b001b9f7bc3e77sm12546595plb.189.2023.10.02.11.37.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 Oct 2023 11:37:42 -0700 (PDT) From: Stephen Hemminger To: dev@dpdk.org Cc: Stephen Hemminger Subject: [PATCH v2 2/3] rte_ether_unformat: accept more inputs Date: Mon, 2 Oct 2023 11:37:29 -0700 Message-Id: <20231002183730.301163-3-stephen@networkplumber.org> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20231002183730.301163-1-stephen@networkplumber.org> References: <20230929163611.62691-1-stephen@networkplumber.org> <20231002183730.301163-1-stephen@networkplumber.org> 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 formats for MAC address. It allows IEEE, IETF and Cisco formats with leading zeros as well. Signed-off-by: Stephen Hemminger --- lib/net/rte_ether.c | 87 ++++++++++++++++++++++++++++++++++----------- lib/net/rte_ether.h | 9 +++-- 2 files changed, 74 insertions(+), 22 deletions(-) diff --git a/lib/net/rte_ether.c b/lib/net/rte_ether.c index 66d9a9d0699a..de3f456207e8 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; @@ -48,25 +49,29 @@ static bool get_ether_addr6(const char *s0, struct rte_ether_addr *ea) x = get_xdigit(*s++); if (x < 0) - return false; + return false; /* not a hex digit */ - 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 && *s != '\0') { + x = get_xdigit(*s++); + if (x < 0) + return false; /* not a hex digit */ + ea->addr_bytes[i] <<= 4; + ea->addr_bytes[i] |= x; + } if (i < RTE_ETHER_ADDR_LEN - 1 && - *s++ != ':') - return false; + *s++ != sep) + return false; /* premature end of string */ } - /* return true if at end of string */ + /* return true if no trailing characters */ return *s == '\0'; } /* 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; @@ -78,14 +83,17 @@ static bool get_ether_addr3(const char *s, struct rte_ether_addr *ea) x = get_xdigit(*s++); if (x < 0) - return false; + return false; /* not a hex digit */ w = (w << 4) | x; + if (*s == sep || *s == '\0') + 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 +101,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 separated by dash, colon or period + * Returns separator and number of matches + * If separators are mixed will return + */ +static unsigned int get_ether_sep(const char *s, char *sep) +{ + static const char separators[] = "-:."; + unsigned int count = 0; + const char *cp; + + cp = strpbrk(s, separators); + if (cp == NULL) + return 0; /* no separator found */ + + *sep = *cp; /* return the separator */ + do { + ++count; + /* find next instance of separator */ + cp = strchr(cp + 1, *sep); + } while (cp != NULL); + + return count; +} + +/* + * Be liberal in accepting a wide variety of notational formats + * for MAC address including: + * - Linux format six groups of hexadecimal digits separated by colon + * - Windows format six groups separated 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..097aedcf21eb 100644 --- a/lib/net/rte_ether.h +++ b/lib/net/rte_ether.h @@ -254,8 +254,13 @@ 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 + * Accepts either byte or word format separated by colon, + * hyphen or period. + * + * 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 - Cisco * 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