From patchwork Mon Jul 8 18:26:05 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stephen Hemminger X-Patchwork-Id: 56246 X-Patchwork-Delegate: ferruh.yigit@amd.com Return-Path: X-Original-To: patchwork@dpdk.org Delivered-To: patchwork@dpdk.org Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id CBD011B9B2; Mon, 8 Jul 2019 20:26:29 +0200 (CEST) Received: from mail-pf1-f174.google.com (mail-pf1-f174.google.com [209.85.210.174]) by dpdk.org (Postfix) with ESMTP id 375C7160 for ; Mon, 8 Jul 2019 20:26:25 +0200 (CEST) Received: by mail-pf1-f174.google.com with SMTP id c73so3336191pfb.13 for ; Mon, 08 Jul 2019 11:26:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=networkplumber-org.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=MeK+yID5ssi0NPFn+Lh/3/mX9Sd3TlIiRDH9K1yPHnY=; b=QB/gRl78fRwFrh1C7CxqU/119z60jpU1boYPDIjZnWX6vKR2JFeS4c0MqJkZ0Jp2hF qIGgbaRx5xrioIqYqg51COHM9lbYfQQYitDYYd9xNj9IiblxHZfTgQiAPzZPrKQTcuXb 0XFaGAFXcQSFPaTltBoZ9EWmxCFgYcq+vG2laxhOii5T7a+5zrDqeMJ/2yBjLicNbIWl USxs7EsUsmOU/WtabglmT/1Lz420EWH6icRFjKALGPM9dEl+rGrLCo7TmJv2cv+Wvw+4 NklBUmK8dVllriXhWMshb7XiB9tcsFc7wl0fJFsC9XFhh7Zzr3nJZLXiCReTzXkWmwoi Dw1w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=MeK+yID5ssi0NPFn+Lh/3/mX9Sd3TlIiRDH9K1yPHnY=; b=Wkc/J830QIzVweEIvQa3LZiLhdtTPVxif/17uPXjwh2Z97yfgGjZ6BPjQZFH9tD/Wu 9j9BSEExMgTnivZTnSoQpKvHD1dEmmqBXXfKIzkAwkQrUX/sMjI9PGiyWtrw3CMRnGoC frCbzaMgF1G60QWPmQxx3BZIc74hTZ9xWLwnkzBNUe9tVzIGZfBDVUMWzjBwTCuyUkpX P9Ya7jShv870cvaV8fP4x73w9zYsFIP2bRB8mQLZpR76zwbkGfI0/4mjJFlT79D/KTxY 2MfWz9Elgk5UPs4IJYnUUdID5sb5q02sKOS7R6KMyPm3ik4hT2eHg1c9i7oSmy+zoSU2 EaOQ== X-Gm-Message-State: APjAAAUeVavS8nag63CDvHKVF4LReOzw/I3LcnhuO1KvCOrTO+lC6L9D AtGyz9kgDGLteBWaC1hkaFB5p+rujME= X-Google-Smtp-Source: APXvYqwjpxg5gsxzGNXvxkup7ZSHeBQykqsy/LHU5Q2wPaKXvV2fXBSmUAX3NrUUZyfcdPAPeMJ/AQ== X-Received: by 2002:a17:90a:2023:: with SMTP id n32mr26416932pjc.3.1562610384131; Mon, 08 Jul 2019 11:26:24 -0700 (PDT) Received: from hermes.lan (204-195-22-127.wavecable.com. [204.195.22.127]) by smtp.gmail.com with ESMTPSA id o15sm18769295pgj.18.2019.07.08.11.26.22 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Mon, 08 Jul 2019 11:26:23 -0700 (PDT) From: Stephen Hemminger To: dev@dpdk.org Cc: Stephen Hemminger , Andrew Rybchenko Date: Mon, 8 Jul 2019 11:26:05 -0700 Message-Id: <20190708182614.1677-3-stephen@networkplumber.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190708182614.1677-1-stephen@networkplumber.org> References: <20190516180427.17270-1-stephen@networkplumber.org> <20190708182614.1677-1-stephen@networkplumber.org> MIME-Version: 1.0 Subject: [dpdk-dev] [PATCH v9 02/11] net/ether: add function to convert string to ethernet address X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" Make a function that can be used in place of eth_aton_r to convert a string to rte_ether_addr. This function allows both byte (xx:xx:xx:xx:xx:xx) and word (XXXX:XXXX:XXXX) format and has the same lack of error handling as the original. This also allows ethdev to no longer have a hard dependency on the cmdline library. Signed-off-by: Stephen Hemminger Reviewed-by: Andrew Rybchenko --- lib/librte_net/rte_ether.c | 49 ++++++++++++++++++++++++++++++ lib/librte_net/rte_ether.h | 14 +++++++++ lib/librte_net/rte_net_version.map | 1 + 3 files changed, 64 insertions(+) diff --git a/lib/librte_net/rte_ether.c b/lib/librte_net/rte_ether.c index 974fe815b335..8d040173cfc6 100644 --- a/lib/librte_net/rte_ether.c +++ b/lib/librte_net/rte_ether.c @@ -3,6 +3,7 @@ */ #include +#include void rte_eth_random_addr(uint8_t *addr) @@ -27,3 +28,51 @@ rte_ether_format_addr(char *buf, uint16_t size, eth_addr->addr_bytes[4], eth_addr->addr_bytes[5]); } + +/* + * Like ether_aton_r but can handle either + * XX:XX:XX:XX:XX:XX or XXXX:XXXX:XXXX + */ +int +rte_ether_unformat_addr(const char *s, struct rte_ether_addr *ea) +{ + unsigned int o0, o1, o2, o3, o4, o5; + int n; + + n = sscanf(s, "%x:%x:%x:%x:%x:%x", + &o0, &o1, &o2, &o3, &o4, &o5); + + if (n == 6) { + /* Standard format XX:XX:XX:XX:XX:XX */ + if (o0 > UINT8_MAX || o1 > UINT8_MAX || o2 > UINT8_MAX || + o3 > UINT8_MAX || o4 > UINT8_MAX || o5 > UINT8_MAX) { + rte_errno = ERANGE; + return -1; + } + + ea->addr_bytes[0] = o0; + ea->addr_bytes[1] = o1; + ea->addr_bytes[2] = o2; + ea->addr_bytes[3] = o3; + ea->addr_bytes[4] = o4; + ea->addr_bytes[5] = o5; + } else if (n == 3) { + /* Support the format XXXX:XXXX:XXXX */ + if (o0 > UINT16_MAX || o1 > UINT16_MAX || o2 > UINT16_MAX) { + rte_errno = ERANGE; + return -1; + } + + ea->addr_bytes[0] = o0 >> 8; + ea->addr_bytes[1] = o0 & 0xff; + ea->addr_bytes[2] = o1 >> 8; + ea->addr_bytes[3] = o1 & 0xff; + ea->addr_bytes[4] = o2 >> 8; + ea->addr_bytes[5] = o2 & 0xff; + } else { + /* unknown format */ + rte_errno = EINVAL; + return -1; + } + return 0; +} diff --git a/lib/librte_net/rte_ether.h b/lib/librte_net/rte_ether.h index df1a51462a37..77b4569b1ad4 100644 --- a/lib/librte_net/rte_ether.h +++ b/lib/librte_net/rte_ether.h @@ -250,6 +250,20 @@ static inline void rte_ether_addr_copy(const struct rte_ether_addr *ea_from, void rte_ether_format_addr(char *buf, uint16_t size, const struct rte_ether_addr *eth_addr); +/** + * Convert string with Ethernet address to an ether_addr. + * + * @param str + * A pointer to buffer contains the formatted MAC address. + * @param eth_addr + * A pointer to a ether_addr structure. + * @return + * 0 if successful + * -1 and sets rte_errno if invalid string + */ +int __rte_experimental +rte_ether_unformat_addr(const char *str, struct rte_ether_addr *eth_addr); + /** * Ethernet header: Contains the destination address, source address * and frame type. diff --git a/lib/librte_net/rte_net_version.map b/lib/librte_net/rte_net_version.map index f1e1b84ab491..f4c915069d57 100644 --- a/lib/librte_net/rte_net_version.map +++ b/lib/librte_net/rte_net_version.map @@ -25,4 +25,5 @@ EXPERIMENTAL { rte_net_make_rarp_packet; rte_net_skip_ip6_ext; + rte_ether_unformat_addr; };