From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from dpdk.org (dpdk.org [92.243.14.124]) by dpdk.space (Postfix) with ESMTP id 9C0E9A0096 for ; Wed, 5 Jun 2019 20:10:15 +0200 (CEST) Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id 3A3DB1B9B7; Wed, 5 Jun 2019 20:09:58 +0200 (CEST) Received: from mail-pg1-f174.google.com (mail-pg1-f174.google.com [209.85.215.174]) by dpdk.org (Postfix) with ESMTP id D0A1B1B9A6 for ; Wed, 5 Jun 2019 20:09:54 +0200 (CEST) Received: by mail-pg1-f174.google.com with SMTP id 196so12838157pgc.6 for ; Wed, 05 Jun 2019 11:09:54 -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=HT7fgvqo3DxGVdfkCgxmfvm3m5YXMqttimfnKXT3DRU=; b=x6ZwWYoLjDiPQPEVrplfB5l7oW3Llcav5aaoc29oLZCwVrTs1ubY/m2gF/mY2SAKTD 1yhuhw8cmkWdz299HE1vpKm2Cc7/ZgtJEG1iqh/e3LgzAvsF3yn+iRqAjv9N6RgrvQle 8crs5KJbnHfRu9YeUW5AjqPc5bOQQWK619doBWHm6046dYmn0Qm/EySk7rQyljbmLV0F wdSl0Qx07UspiaSAiG4WeRK8VX273UTf8pVYfdvcbNBzR48+l0Nmj2NH9SmIIS6oIodV v+pfXI01JxmqKPchC1ZewDo/zEukeFggMW7QIZ7e2TjZL79H99sU4i0oSBHrPDpC3uGs xbKA== 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=HT7fgvqo3DxGVdfkCgxmfvm3m5YXMqttimfnKXT3DRU=; b=DYGyQYaXX6rNx8LEmaHFOKYvJwqvMBbF5Vo+C2GSIT9QdzVK0IhLHJGvA5gaW4L1N1 wwopAXaJEZ0lIFY9roFQkLjhYWlZmndz5YV06pLGVBKv5mUCOfj9JIgP8OHszC4ktSD2 e8vZlztmOX9bO5ZssOUx7skT6DCkvkLHnmSNLLhk5H0LCxfA833a427/njPaPPdkQ09X GjvMHveKl3ePL+RD7HxrkPHEIDyNoyYv4eqo1Q7Ln0cdPggX6WwmERnlFFcb/dFfJ8JS Ksm1ZgLu0tFaNz0Re0qF5Oy+y1sTVZ5peST5EMas9NBqSPdnOZZQjYyEYcmczRAqsI4h 7Vyg== X-Gm-Message-State: APjAAAWh/9Nsji991Ir0ulvT+x/zNKtOLF9EBzt6/vZuoWjQ4DufwI15 dmLd2vOXQF1FWgUVAokjYGoNUK6jquk= X-Google-Smtp-Source: APXvYqwfA0SvedLftN6uwBYhcbV2ZpCW8odASMdQ771zdE5q0e8WvQN/2UR6zcfHnGrhUd4ra/CF7g== X-Received: by 2002:a62:e511:: with SMTP id n17mr41663512pff.181.1559758193717; Wed, 05 Jun 2019 11:09:53 -0700 (PDT) Received: from hermes.lan (204-195-22-127.wavecable.com. [204.195.22.127]) by smtp.gmail.com with ESMTPSA id a7sm19081888pgj.42.2019.06.05.11.09.52 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 05 Jun 2019 11:09:52 -0700 (PDT) From: Stephen Hemminger To: dev@dpdk.org Cc: Stephen Hemminger , Andrew Rybchenko Date: Wed, 5 Jun 2019 11:09:42 -0700 Message-Id: <20190605180948.22414-3-stephen@networkplumber.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190605180948.22414-1-stephen@networkplumber.org> References: <20190516180427.17270-1-stephen@networkplumber.org> <20190605180948.22414-1-stephen@networkplumber.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [dpdk-dev] [PATCH v4 2/8] net/ether: add rte_eth_unformat_addr 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 | 47 ++++++++++++++++++++++++++++++ lib/librte_net/rte_ether.h | 14 +++++++++ lib/librte_net/rte_net_version.map | 1 + 3 files changed, 62 insertions(+) diff --git a/lib/librte_net/rte_ether.c b/lib/librte_net/rte_ether.c index 974fe815b335..acc8a0e938c5 100644 --- a/lib/librte_net/rte_ether.c +++ b/lib/librte_net/rte_ether.c @@ -27,3 +27,50 @@ 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) { + if (o0 > UINT8_MAX || o1 > UINT8_MAX || o2 > UINT8_MAX || + o3 > UINT8_MAX || o4 > UINT8_MAX || o5 > UINT8_MAX) + 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; + + return 0; + } + + /* Support the format XXXX:XXXX:XXXX */ + n = sscanf(s, "%x:%x:%x", &o0, &o1, &o2); + if (n == 3) { + if (o0 > UINT16_MAX || o1 > UINT16_MAX || o2 > UINT16_MAX) + 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; + return 0; + } + /* unknown format */ + + return -1; +} diff --git a/lib/librte_net/rte_ether.h b/lib/librte_net/rte_ether.h index 3caae0d98f6d..8edc7e217b25 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; }; -- 2.20.1