From patchwork Wed Feb 3 03:04:37 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nikolay Aleksandrov X-Patchwork-Id: 577703 X-Patchwork-Delegate: davem@davemloft.net Return-Path: X-Original-To: patchwork-incoming@ozlabs.org Delivered-To: patchwork-incoming@ozlabs.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id EEF2E140B00 for ; Wed, 3 Feb 2016 14:05:04 +1100 (AEDT) Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=blackwall-org.20150623.gappssmtp.com header.i=@blackwall-org.20150623.gappssmtp.com header.b=LDifw0L3; dkim-atps=neutral Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932913AbcBCDEv (ORCPT ); Tue, 2 Feb 2016 22:04:51 -0500 Received: from mail-wm0-f66.google.com ([74.125.82.66]:34615 "EHLO mail-wm0-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932845AbcBCDEo (ORCPT ); Tue, 2 Feb 2016 22:04:44 -0500 Received: by mail-wm0-f66.google.com with SMTP id p63so5261900wmp.1 for ; Tue, 02 Feb 2016 19:04:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=blackwall-org.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=yPwcPaLHXwH1YbvZBsYQD1lDBegu8zqkFlkJJYb83UY=; b=LDifw0L3T4QVVWKLw9J82RKfpjWCAw+nnbXnDWPXHHqBGwziIIULrA18KFjczuEXGH glTAun8OO8KjPWRV9voXoxwZ0Sp+SXZp1dC6leLvtJw6okXRKtARJZoLFrb/KJ8/INbV bMd5epwEx0ozs/hY2q+fEFz85mZhdA3q4W7Rea3a62d9BvVqpIh2KJOAhIzrAcqtBIAV HeZ0UIx8bu18zhd/FPABm0EYVeARGOfqZAChNpbJoxDrfQGPQsp3GSrfut27wHWeg0VQ AKaTZfF5nRhtPJrRykK3rOtzf6SBsJVrYHnOxiqr/E+dK4UEgpBYAzhwS1oDr6zwyWlV B1Iw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=yPwcPaLHXwH1YbvZBsYQD1lDBegu8zqkFlkJJYb83UY=; b=ke8hWYvf0x8ZTNI/6J+SBmkLwnaUHR24It8ltFCC46cdVjavSNHe60S+I8dNLk9yD5 +2pcCn/pD3yTlKUsqF7LF0ErV72tDuHTcjFOeowLeuYvMF0nDdagYeouEOqiD+YnWPB6 Dv5+Jsyo9C/l6b2IAIcPfAmy6SeSIzY3NMEeOjbsZmQAiGS2B8Cr8c2CVWinIyK9FlS/ qF3opn344syUvMrZJmW/oICC3fx/PUW6rN5WHd8UZH4rnSrNY6ALJPtgdFV1RpXqJ/gE Alhtxs403Kq/SWvbF1XZzUg0eZSeTVoBiMJFR2AGNdJflRlCec4H+jxDthzIZmJckECi XPWQ== X-Gm-Message-State: AG10YORXaLGhRWBDM/J6qSolw5Dl6tCzbUTG2Jbk8CEf6Xavkx8SIkE5v7X3zXVXnMBPrQ== X-Received: by 10.194.114.106 with SMTP id jf10mr38526914wjb.149.1454468683293; Tue, 02 Feb 2016 19:04:43 -0800 (PST) Received: from debil.localdomain (ip4-62-4-104-109.cust.nbox.cz. [62.4.104.109]) by smtp.gmail.com with ESMTPSA id i12sm5589742wmf.10.2016.02.02.19.04.42 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 02 Feb 2016 19:04:42 -0800 (PST) From: Nikolay Aleksandrov To: netdev@vger.kernel.org Cc: mst@redhat.com, roopa@cumulusnetworks.com, davem@davemloft.net, Nikolay Aleksandrov Subject: [PATCH net-next v5 2/2] virtio_net: add ethtool support for set and get of settings Date: Wed, 3 Feb 2016 04:04:37 +0100 Message-Id: <1454468677-12280-3-git-send-email-razor@blackwall.org> X-Mailer: git-send-email 2.4.3 In-Reply-To: <1454468677-12280-1-git-send-email-razor@blackwall.org> References: <1454468677-12280-1-git-send-email-razor@blackwall.org> Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org From: Nikolay Aleksandrov This patch allows the user to set and retrieve speed and duplex of the virtio_net device via ethtool. Having this functionality is very helpful for simulating different environments and also enables the virtio_net device to participate in operations where proper speed and duplex are required (e.g. currently bonding lacp mode requires full duplex). Custom speed and duplex are not allowed, the user-supplied settings are validated before applying. Example: $ ethtool eth1 Settings for eth1: ... Speed: Unknown! Duplex: Unknown! (255) $ ethtool -s eth1 speed 1000 duplex full $ ethtool eth1 Settings for eth1: ... Speed: 1000Mb/s Duplex: Full Based on a patch by Roopa Prabhu. Signed-off-by: Nikolay Aleksandrov --- v2: use the new ethtool speed/duplex validation functions and allow half duplex to be set v3: return error if the user tries to change anything besides speed/duplex as per Michael's comment We have to zero-out advertising as it gets set automatically by ethtool if setting speed and duplex together. v4: Set port type to PORT_OTHER v5: null diff1.port because we set cmd->port now and ethtool returns it in the set request, retested all cases drivers/net/virtio_net.c | 60 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio_net.c index 767ab11a6e9f..c9fd52a8e6ec 100644 --- a/drivers/net/virtio_net.c +++ b/drivers/net/virtio_net.c @@ -146,6 +146,10 @@ struct virtnet_info { virtio_net_ctrl_ack ctrl_status; u8 ctrl_promisc; u8 ctrl_allmulti; + + /* Ethtool settings */ + u8 duplex; + u32 speed; }; struct padded_vnet_hdr { @@ -1376,6 +1380,58 @@ static void virtnet_get_channels(struct net_device *dev, channels->other_count = 0; } +/* Check if the user is trying to change anything besides speed/duplex */ +static bool virtnet_validate_ethtool_cmd(const struct ethtool_cmd *cmd) +{ + struct ethtool_cmd diff1 = *cmd; + struct ethtool_cmd diff2 = {}; + + /* advertising and cmd are usually set, ignore port because we set it */ + ethtool_cmd_speed_set(&diff1, 0); + diff1.advertising = 0; + diff1.duplex = 0; + diff1.port = 0; + diff1.cmd = 0; + + return !memcmp(&diff1, &diff2, sizeof(diff1)); +} + +static int virtnet_set_settings(struct net_device *dev, struct ethtool_cmd *cmd) +{ + struct virtnet_info *vi = netdev_priv(dev); + u32 speed; + + speed = ethtool_cmd_speed(cmd); + /* don't allow custom speed and duplex */ + if (!ethtool_validate_speed(speed) || + !ethtool_validate_duplex(cmd->duplex) || + !virtnet_validate_ethtool_cmd(cmd)) + return -EINVAL; + vi->speed = speed; + vi->duplex = cmd->duplex; + + return 0; +} + +static int virtnet_get_settings(struct net_device *dev, struct ethtool_cmd *cmd) +{ + struct virtnet_info *vi = netdev_priv(dev); + + ethtool_cmd_speed_set(cmd, vi->speed); + cmd->duplex = vi->duplex; + cmd->port = PORT_OTHER; + + return 0; +} + +static void virtnet_init_settings(struct net_device *dev) +{ + struct virtnet_info *vi = netdev_priv(dev); + + vi->speed = SPEED_UNKNOWN; + vi->duplex = DUPLEX_UNKNOWN; +} + static const struct ethtool_ops virtnet_ethtool_ops = { .get_drvinfo = virtnet_get_drvinfo, .get_link = ethtool_op_get_link, @@ -1383,6 +1439,8 @@ static const struct ethtool_ops virtnet_ethtool_ops = { .set_channels = virtnet_set_channels, .get_channels = virtnet_get_channels, .get_ts_info = ethtool_op_get_ts_info, + .get_settings = virtnet_get_settings, + .set_settings = virtnet_set_settings, }; #define MIN_MTU 68 @@ -1855,6 +1913,8 @@ static int virtnet_probe(struct virtio_device *vdev) netif_set_real_num_tx_queues(dev, vi->curr_queue_pairs); netif_set_real_num_rx_queues(dev, vi->curr_queue_pairs); + virtnet_init_settings(dev); + err = register_netdev(dev); if (err) { pr_debug("virtio_net: registering device failed\n");