From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <dev-bounces@dpdk.org>
Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124])
	by inbox.dpdk.org (Postfix) with ESMTP id 99EC9A0C44;
	Fri, 16 Apr 2021 11:27:30 +0200 (CEST)
Received: from [217.70.189.124] (localhost [127.0.0.1])
	by mails.dpdk.org (Postfix) with ESMTP id 509DD141C1B;
	Fri, 16 Apr 2021 11:27:30 +0200 (CEST)
Received: from us-smtp-delivery-124.mimecast.com
 (us-smtp-delivery-124.mimecast.com [63.128.21.124])
 by mails.dpdk.org (Postfix) with ESMTP id D832E141C14
 for <dev@dpdk.org>; Fri, 16 Apr 2021 11:27:28 +0200 (CEST)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com;
 s=mimecast20190719; t=1618565248;
 h=from:from:reply-to:subject:subject:date:date:message-id:message-id:
 to:to:cc:mime-version:mime-version:content-type:content-type:
 content-transfer-encoding:content-transfer-encoding:
 in-reply-to:in-reply-to:references:references;
 bh=JSvpW2DRU0bLGDfnXO7QgRhw4zC2weaHZMu0nK6SKy8=;
 b=CzQyoOaOyo3V8Za+IPFwgIrgfCSpjeT2+RX5NRT7ugdbaCCXH6xkPjClAyka9aHefjZ/ug
 ZzrlsLkGOizdlMEofGaB00fL7TwIzcfe5hQIygLSsUMmoIScfDNm70h2/rffOAc+HzIAp1
 6dxQAlsJJKrmWlACmt4j3bFEVB2UTdQ=
Received: from mail-wr1-f71.google.com (mail-wr1-f71.google.com
 [209.85.221.71]) (Using TLS) by relay.mimecast.com with ESMTP id
 us-mta-604-f-y7xcOXPQyDmvAxfePXWw-1; Fri, 16 Apr 2021 05:27:27 -0400
X-MC-Unique: f-y7xcOXPQyDmvAxfePXWw-1
Received: by mail-wr1-f71.google.com with SMTP id
 t14-20020adff04e0000b0290103307c23e1so4167424wro.8
 for <dev@dpdk.org>; Fri, 16 Apr 2021 02:27:26 -0700 (PDT)
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=1e100.net; s=20161025;
 h=x-gm-message-state:subject:to:references:from:message-id:date
 :user-agent:mime-version:in-reply-to:content-language
 :content-transfer-encoding;
 bh=JSvpW2DRU0bLGDfnXO7QgRhw4zC2weaHZMu0nK6SKy8=;
 b=J2Z8LGW2WkPyVWO4eLBe0ab2fWUU8DFSbGUrbM+CYSDH/riZtuKVFS9Jg4MTqF97bp
 uEQ0dzAr95y20Ey+7czQukEb6fFszmdOFOhTBKAnlCcSYRy2EL5vQy71FSIPmTjIAWrC
 qI3kH/7O9a7aObWN1Rf1aLifPXQmCNqeFIFUNrwTKgsmGfQlhQUmVnbe3MfLIDvM6A9D
 P/jZdyOnTK+iSPJiQic+33dDjwm0L/NMmaGYTeK/G6LHHRm30anbfxxtkUBADpy+g950
 HsDxgoRTJhNVRPSUgIcX+P0fLFkwgfhMkLaUjMAOLF2GdvD+ekmRCGLL1UjeHJsD+5OY
 vx1A==
X-Gm-Message-State: AOAM530ZtiLPinjwSWL/qGYW2OQ1S4spHrdfvBQVCjegDmJ+ct4EpIMS
 BR3HaltYqAv1d7JngURdoRkXLwG5RLr3QO5zdCWoeyhgWMhMPgWcTUbi85kEtDKcvfSabjGPEnI
 NGXs=
X-Received: by 2002:adf:d1ce:: with SMTP id b14mr7553790wrd.159.1618565244971; 
 Fri, 16 Apr 2021 02:27:24 -0700 (PDT)
X-Google-Smtp-Source: ABdhPJwVIWLy3TR5Q/sdx7cQKnKwNSMlUSJrm/bzu2/eJrfQgBEfcHLV+2S29aJAhsB3frqkT+EkVA==
X-Received: by 2002:adf:d1ce:: with SMTP id b14mr7553773wrd.159.1618565244784; 
 Fri, 16 Apr 2021 02:27:24 -0700 (PDT)
Received: from amorenoz.users.ipa.redhat.com ([94.73.62.62])
 by smtp.gmail.com with ESMTPSA id x17sm6828884wmi.46.2021.04.16.02.27.23
 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128);
 Fri, 16 Apr 2021 02:27:23 -0700 (PDT)
To: Maxime Coquelin <maxime.coquelin@redhat.com>, dev@dpdk.org,
 chenbo.xia@intel.com, david.marchand@redhat.com
References: <20210318223526.168614-1-maxime.coquelin@redhat.com>
 <20210318223526.168614-4-maxime.coquelin@redhat.com>
From: Adrian Moreno <amorenoz@redhat.com>
Message-ID: <3997731b-29cd-5e1f-fc9f-b31b7cb5abe3@redhat.com>
Date: Fri, 16 Apr 2021 11:27:23 +0200
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101
 Thunderbird/78.8.1
MIME-Version: 1.0
In-Reply-To: <20210318223526.168614-4-maxime.coquelin@redhat.com>
Authentication-Results: relay.mimecast.com;
 auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=amorenoz@redhat.com
X-Mimecast-Spam-Score: 0
X-Mimecast-Originator: redhat.com
Content-Type: text/plain; charset=utf-8
Content-Language: en-US
Content-Transfer-Encoding: 8bit
Subject: Re: [dpdk-dev] [RFC 3/3] net/virtio: add MAC device config getter
 and setter
X-BeenThere: dev@dpdk.org
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: DPDK patches and discussions <dev.dpdk.org>
List-Unsubscribe: <https://mails.dpdk.org/options/dev>,
 <mailto:dev-request@dpdk.org?subject=unsubscribe>
List-Archive: <http://mails.dpdk.org/archives/dev/>
List-Post: <mailto:dev@dpdk.org>
List-Help: <mailto:dev-request@dpdk.org?subject=help>
List-Subscribe: <https://mails.dpdk.org/listinfo/dev>,
 <mailto:dev-request@dpdk.org?subject=subscribe>
Errors-To: dev-bounces@dpdk.org
Sender: "dev" <dev-bounces@dpdk.org>



On 3/18/21 11:35 PM, Maxime Coquelin wrote:
> This patch uses the new device config ops to get and set
> the MAC address if supported.
> 
> If a valid MAC address is passed as devarg of the
> Virtio-user PMD, the driver will try to store it in the
> device config space. Otherwise the one provided in
> the device config space will be used, if available.
> 
> Signed-off-by: Maxime Coquelin <maxime.coquelin@redhat.com>
> ---
>  .../net/virtio/virtio_user/virtio_user_dev.c  | 78 ++++++++++++++++---
>  .../net/virtio/virtio_user/virtio_user_dev.h  |  2 +
>  drivers/net/virtio/virtio_user_ethdev.c       |  7 +-
>  3 files changed, 74 insertions(+), 13 deletions(-)
> 
> diff --git a/drivers/net/virtio/virtio_user/virtio_user_dev.c b/drivers/net/virtio/virtio_user/virtio_user_dev.c
> index 8757a23f6e..61517692b3 100644
> --- a/drivers/net/virtio/virtio_user/virtio_user_dev.c
> +++ b/drivers/net/virtio/virtio_user/virtio_user_dev.c
> @@ -259,20 +259,76 @@ int virtio_user_stop_device(struct virtio_user_dev *dev)
>  	return -1;
>  }
>  
> -static inline void
> -parse_mac(struct virtio_user_dev *dev, const char *mac)
> +int
> +virtio_user_dev_set_mac(struct virtio_user_dev *dev)
>  {
> -	struct rte_ether_addr tmp;
> +	int ret = 0;
>  
> -	if (!mac)
> -		return;
> +	if (!(dev->device_features & (1ULL << VIRTIO_NET_F_MAC)))
> +		return -ENOTSUP;
> +
> +	if (!dev->ops->set_config)
> +		return -ENOTSUP;
> +
> +	ret = dev->ops->set_config(dev, dev->mac_addr,
> +			offsetof(struct virtio_net_config, mac),
> +			RTE_ETHER_ADDR_LEN);
> +	if (ret)
> +		PMD_DRV_LOG(ERR, "(%s) Failed to set MAC address in device\n", dev->path);
> +
> +	return ret;
> +}
> +
> +int
> +virtio_user_dev_get_mac(struct virtio_user_dev *dev)
> +{
> +	int ret = 0;
> +
> +	if (!(dev->device_features & (1ULL << VIRTIO_NET_F_MAC)))
> +		return -ENOTSUP;
> +
> +	if (!dev->ops->get_config)
> +		return -ENOTSUP;
> +
> +	ret = dev->ops->get_config(dev, dev->mac_addr,
> +			offsetof(struct virtio_net_config, mac),
> +			RTE_ETHER_ADDR_LEN);
> +	if (ret)
> +		PMD_DRV_LOG(ERR, "(%s) Failed to get MAC address from device\n", dev->path);
>  
> -	if (rte_ether_unformat_addr(mac, &tmp) == 0) {
> -		memcpy(dev->mac_addr, &tmp, RTE_ETHER_ADDR_LEN);
> +	return ret;
> +}
> +
> +static void
> +virtio_user_dev_init_mac(struct virtio_user_dev *dev, const char *mac)
> +{
> +	struct rte_ether_addr cmdline_mac;
> +	int ret;
> +
> +	if (mac && rte_ether_unformat_addr(mac, &cmdline_mac) == 0) {
> +		/*
> +		 * MAC address was passed from command-line, try to store
> +		 * it in the device if it supports it. Otherwise try to use
> +		 * the device one.
> +		 */
> +		memcpy(dev->mac_addr, &cmdline_mac, RTE_ETHER_ADDR_LEN);
>  		dev->mac_specified = 1;
> +
> +		/* Setting MAC may fail, continue to get the device one in this case */
> +		virtio_user_dev_set_mac(dev);
> +		ret = virtio_user_dev_get_mac(dev);
> +		if (ret == -ENOTSUP)
> +			return;
> +
> +		if (memcmp(&cmdline_mac, dev->mac_addr, RTE_ETHER_ADDR_LEN))
> +			PMD_DRV_LOG(INFO, "(%s) Device MAC update failed\n", dev->path);

Maybe log the mac that we've read if it differs from the one we tried to write?

>  	} else {
> -		/* ignore the wrong mac, use random mac */
> -		PMD_DRV_LOG(ERR, "wrong format of mac: %s", mac);
> +		ret = virtio_user_dev_get_mac(dev);
> +		if (ret)
> +			PMD_DRV_LOG(ERR, "(%s) No valid MAC in devargs or device, use random\n",
> +					dev->path);
> +		else
> +			dev->mac_specified = 1;
>  	}
>  }
>  
> @@ -508,8 +564,6 @@ virtio_user_dev_init(struct virtio_user_dev *dev, char *path, int queues,
>  	dev->unsupported_features = 0;
>  	dev->backend_type = backend_type;
>  
> -	parse_mac(dev, mac);
> -
>  	if (*ifname) {
>  		dev->ifname = *ifname;
>  		*ifname = NULL;
> @@ -537,6 +591,8 @@ virtio_user_dev_init(struct virtio_user_dev *dev, char *path, int queues,
>  		return -1;
>  	}
>  
> +	virtio_user_dev_init_mac(dev, mac);
> +
>  	if (!mrg_rxbuf)
>  		dev->unsupported_features |= (1ull << VIRTIO_NET_F_MRG_RXBUF);
>  
> diff --git a/drivers/net/virtio/virtio_user/virtio_user_dev.h b/drivers/net/virtio/virtio_user/virtio_user_dev.h
> index 8a62f7ea79..03bcf95970 100644
> --- a/drivers/net/virtio/virtio_user/virtio_user_dev.h
> +++ b/drivers/net/virtio/virtio_user/virtio_user_dev.h
> @@ -78,6 +78,8 @@ uint8_t virtio_user_handle_mq(struct virtio_user_dev *dev, uint16_t q_pairs);
>  int virtio_user_dev_set_status(struct virtio_user_dev *dev, uint8_t status);
>  int virtio_user_dev_update_status(struct virtio_user_dev *dev);
>  int virtio_user_dev_update_link_state(struct virtio_user_dev *dev);
> +int virtio_user_dev_set_mac(struct virtio_user_dev *dev);
> +int virtio_user_dev_get_mac(struct virtio_user_dev *dev);
>  void virtio_user_dev_delayed_handler(void *param);
>  int virtio_user_dev_server_reconnect(struct virtio_user_dev *dev);
>  extern const char * const virtio_user_backend_strings[];
> diff --git a/drivers/net/virtio/virtio_user_ethdev.c b/drivers/net/virtio/virtio_user_ethdev.c
> index bb36316186..34c8e7a9b5 100644
> --- a/drivers/net/virtio/virtio_user_ethdev.c
> +++ b/drivers/net/virtio/virtio_user_ethdev.c
> @@ -60,12 +60,15 @@ virtio_user_write_dev_config(struct virtio_hw *hw, size_t offset,
>  	struct virtio_user_dev *dev = virtio_user_get_dev(hw);
>  
>  	if ((offset == offsetof(struct virtio_net_config, mac)) &&
> -	    (length == RTE_ETHER_ADDR_LEN))
> +	    (length == RTE_ETHER_ADDR_LEN)) {
>  		for (i = 0; i < RTE_ETHER_ADDR_LEN; ++i)
>  			dev->mac_addr[i] = ((const uint8_t *)src)[i];
> -	else
> +		virtio_user_dev_set_mac(dev);
> +		virtio_user_dev_get_mac(dev);
> +	} else {
>  		PMD_DRV_LOG(ERR, "not supported offset=%zu, len=%d",
>  			    offset, length);
> +	}
>  }
>  
>  static void
> 

-- 
Adrián Moreno