From patchwork Thu Jan 18 06:12:58 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hemant Agrawal X-Patchwork-Id: 124921 Delivered-To: patch@linaro.org Received: by 10.80.140.226 with SMTP id r31csp3134889edr; Wed, 17 Jan 2018 22:14:33 -0800 (PST) X-Google-Smtp-Source: ACJfBovIo6A0P97vABbD/t1U+8c6xqGBcgAyL63GEsZ59DQOcPa8PsW6xJhWON+3QJxkmMAiNhp2 X-Received: by 10.223.208.201 with SMTP id z9mr4813223wrh.194.1516256073546; Wed, 17 Jan 2018 22:14:33 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1516256073; cv=none; d=google.com; s=arc-20160816; b=AtVs5sncriMEJbAurokpqsecso41SM+8v1f4iJzyrHtL8bLymFaQbpOotU/ilX3INL yUKfymbqmzxX7R2j0WKtzLq8oUsXRbNF/2s7uGe5Lvh8weZD/wywiCsFajsRUpeMv1Mf XKfzkDjtri0g+BqYpsqYA3PwrsXfhZctMoNagi55tR7t69qa9TLskF/Z39FhkxA9vW4k wS0Fp7mkIPFIdl93lo6/CHef3nBJkwhzLYkbkfmcLNyG+b3EGZgzKxY1fAQPw0Cu7oRm dMndo4Zw0USfswvOAfS3J9UJvSUCYDfYfsdsyxPQBeybpY7bjwfQFvylosKak2RVEvOs ttXw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:subject:spamdiagnosticmetadata :spamdiagnosticoutput:mime-version:references:in-reply-to:message-id :date:cc:to:from:arc-authentication-results; bh=tWzisFevGjL3KzJMpGlBzbSPzslBsxIS0jJXVCiBGvY=; b=NlHdJR0fxD6DRSMWQl7kOrcES5Ms/dcHHmq+kXzQx1kFprrfFEmFGdHFjOcTEYaRjK RBM2jn/+YiSdFAgR1RlyOsqpKOrtvI8p5nseSmmH4OkItoCvID6us8df88VlOcPBWJRf JA15ABduWsXt8KN6LuYnqRTGsLLyPfWHuzv35VNS9W9sfE1sx8FL0sEIeQeXQ5tqd5uY maNy7J2NggYuFMmcF01V3M0tSLdDZE/uRHJ6pUig4zDySD+/4k6By1aL0c/sdLKjkkjC uoMwLHu0pYwst7vNHw6K3Ko8wS77xdUQ6y6Y9zEV0NQOY4NO1WZFf9A3m8rzCqr7WVTf 5KIw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of dev-bounces@dpdk.org designates 92.243.14.124 as permitted sender) smtp.mailfrom=dev-bounces@dpdk.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=nxp.com Return-Path: Received: from dpdk.org (dpdk.org. [92.243.14.124]) by mx.google.com with ESMTP id n188si5048387wmd.49.2018.01.17.22.14.33; Wed, 17 Jan 2018 22:14:33 -0800 (PST) Received-SPF: pass (google.com: domain of dev-bounces@dpdk.org designates 92.243.14.124 as permitted sender) client-ip=92.243.14.124; Authentication-Results: mx.google.com; spf=pass (google.com: domain of dev-bounces@dpdk.org designates 92.243.14.124 as permitted sender) smtp.mailfrom=dev-bounces@dpdk.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=nxp.com Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id C517B1B1BF; Thu, 18 Jan 2018 07:14:20 +0100 (CET) Received: from NAM01-BY2-obe.outbound.protection.outlook.com (mail-by2nam01on0086.outbound.protection.outlook.com [104.47.34.86]) by dpdk.org (Postfix) with ESMTP id 7EA981B1BF for ; Thu, 18 Jan 2018 07:14:18 +0100 (CET) Received: from BN3PR03CA0061.namprd03.prod.outlook.com (10.167.1.149) by CY1PR03MB2362.namprd03.prod.outlook.com (10.166.207.149) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P256) id 15.20.407.7; Thu, 18 Jan 2018 06:14:17 +0000 Received: from BN1AFFO11FD038.protection.gbl (2a01:111:f400:7c10::164) by BN3PR03CA0061.outlook.office365.com (2a01:111:e400:7a4d::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384) id 15.20.407.7 via Frontend Transport; Thu, 18 Jan 2018 06:14:11 +0000 Authentication-Results: spf=fail (sender IP is 192.88.168.50) smtp.mailfrom=nxp.com; intel.com; dkim=none (message not signed) header.d=none; intel.com; dmarc=fail action=none header.from=nxp.com; Received-SPF: Fail (protection.outlook.com: domain of nxp.com does not designate 192.88.168.50 as permitted sender) receiver=protection.outlook.com; client-ip=192.88.168.50; helo=tx30smr01.am.freescale.net; Received: from tx30smr01.am.freescale.net (192.88.168.50) by BN1AFFO11FD038.mail.protection.outlook.com (10.58.52.242) with Microsoft SMTP Server (version=TLS1_0, cipher=TLS_RSA_WITH_AES_256_CBC_SHA) id 15.20.345.12 via Frontend Transport; Thu, 18 Jan 2018 06:14:09 +0000 Received: from bf-netperf1.ap.freescale.net (bf-netperf1.ap.freescale.net [10.232.134.28]) by tx30smr01.am.freescale.net (8.14.3/8.14.0) with ESMTP id w0I6E6h9024428; Wed, 17 Jan 2018 23:14:07 -0700 From: Hemant Agrawal To: CC: Date: Thu, 18 Jan 2018 11:42:58 +0530 Message-ID: <1516255980-25092-1-git-send-email-hemant.agrawal@nxp.com> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1514284608-9263-1-git-send-email-hemant.agrawal@nxp.com> References: <1514284608-9263-1-git-send-email-hemant.agrawal@nxp.com> X-EOPAttributedMessage: 0 X-Matching-Connectors: 131607296502617006; (91ab9b29-cfa4-454e-5278-08d120cd25b8); () X-Forefront-Antispam-Report: CIP:192.88.168.50; IPV:NLI; CTRY:US; EFV:NLI; SFV:NSPM; SFS:(10009020)(7966004)(396003)(39380400002)(39860400002)(376002)(346002)(2980300002)(1110001)(1109001)(339900001)(3190300001)(189003)(199004)(5660300001)(26005)(2351001)(50226002)(6916009)(47776003)(8676002)(105606002)(316002)(8936002)(508600001)(81166006)(51416003)(76176011)(106466001)(356003)(16586007)(81156014)(68736007)(53936002)(6666003)(305945005)(50466002)(2906002)(2950100002)(59450400001)(97736004)(4326008)(48376002)(36756003)(85426001)(104016004)(77096007)(86362001); DIR:OUT; SFP:1101; SCL:1; SRVR:CY1PR03MB2362; H:tx30smr01.am.freescale.net; FPR:; SPF:Fail; PTR:InfoDomainNonexistent; MX:1; A:1; LANG:en; X-Microsoft-Exchange-Diagnostics: 1; BN1AFFO11FD038; 1:+3umuX3kOLwRHcuFSLs1OKNYWSsQcK7sWnmcxklX7Z+23Ohf43pe8JVrPZjOOqvNBgVp4CO1q9Y06kpP6Q1UPWxYzB1jz/Kf0eon3HXUpkTvIVTEHNB1yTEfsaz2hVRd MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 23f07744-8691-47af-53ae-08d55e3ab05a X-Microsoft-Antispam: UriScan:; BCL:0; PCL:0; RULEID:(7020095)(5600026)(4604075)(2017052603307); SRVR:CY1PR03MB2362; X-Microsoft-Exchange-Diagnostics: 1; CY1PR03MB2362; 3:wGwn0n0mZUs9e4SvOrgSIIxNwZZDteuwPqTHwGv7LDbzKKcZFEO7BwsknCtBEjDEgOp6ShRfm4SGu/H/8keBQdyDYcylateIc/7/fqCy/Zlu6W2yvO9Cr7JR9WEpytWoDxp7m23mw9Af3LkDXEphE0XsIxKQrzkajPImh85aR5LPfFz54kGu668nUtX7a7RPafshBTEBzd2nCZVPqi8hGmMOBY2FKsY4CX+Sp0MmUUKHCgAO0WzDgQ6/5QQ/G29eb2Magt3hMFNhD6QaXFMmID8n1fyw5nYRL2cL+lr/JOYdcDT0/E8OCnUpQ4XZZfMqzOI/N/GpAh959aTingK61mIDq8Wk2YY5hm7s5YewHGA=; 25:YDmtARqDECmSCFSnYvZVGBUt3w6CDxZIlNRxSWJa/VWPHvtOan3wI/RFLDSOoUKkqhk/NsDpF4ZY7RsalsZeEjdGExKapso0nr1D4WXuENYMjoEl+2vFX/hQPxgy6nhMHwIdGZaLkTYO2vot+NfisNuzTaP/8wbs+mqTh43vB4nDAVImnXzquh7m+yv4Ke6Ozbh40kjEyhLCqI3BPdQ2+x4T04gvWUL9PuXBlipAYpAM9x6Rlqd07ROQlD14X1f+GYMdK2WlHOGoHT3BQrxyiou+0oE5/KZhbYgqumqpA5o1t75HKWS/EOohpmENpo8rrLLwHrp7edglaoSunZinHQ== X-MS-TrafficTypeDiagnostic: CY1PR03MB2362: X-Microsoft-Exchange-Diagnostics: 1; CY1PR03MB2362; 31:H2RGBFDuMcPnQAG+wTdKD/8/n3jAoiZUTBhyPva5jNWn6I023AkF7Tphh3T+V3gdcIr1HS2DKzuxH74+RCD9YsBrzmikTb/91G0JQuHGmP91wlpIdiwbJptJc+aYHOXG6PjWwk0H2tvqu4+FhzSDos1XX1wIyu100N06hdse1PZehOgUFpc3mTlKf906mLAa/V0F5VgHRyEOMBx0+756WXE4FHtnr8Ap1hSvpPlUzh0=; 4:CLiZvJPKVofRji1Qqgqlk46mE6TG21V5XwinZhcseJERv6qvJkH96UtqfnKX0r8zfs4gt8CEYOxQ00soQ5lMWnWANPTz4h6W/IZquVNUL545E+0yQuoD9bP8R73/2zkM4Y8VaQAVTkSV5xkyQH6Zg/dLfyMzhxllnYIk9k8mj5mvpqHA1Ajw2eT6zQp6U27ZVKccPo6tQX1c11b1QkFNh9onermohP9rjOMFMWrmb/jr/wga76bDbvyspUAq7KGUaUssWLP5MRyNnnoQ8D/CXagBF+4rENH335gTLEdOV5Qq6r7lHF9Pu8KHZ1hz1uFj X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:(185117386973197); X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(6095135)(2401047)(5005006)(8121501046)(93006095)(93001095)(3002001)(10201501046)(3231023)(2400057)(944501161)(6055026)(6096035)(20161123565025)(20161123559100)(20161123556025)(20161123563025)(201703131430075)(201703131433075)(201703131441075)(201703131448075)(20161123561025)(201708071742011); SRVR:CY1PR03MB2362; BCL:0; PCL:0; RULEID:(100000803101)(100110400095)(400006); SRVR:CY1PR03MB2362; X-Forefront-PRVS: 05568D1FF7 X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1; CY1PR03MB2362; 23:U2exHVi6VVlodrfI4MPtWjr/LusVgpZSysLO8VOue?= Nkr+nstRM1zC9Z06/xFtsb9K+Uyu1pvwvqLKQ/Az4nnkYY6eVdBNUaaYexKUYuZgiCmu4BME1+LjVz+683IpwvJQivg0bDpmCX9aa0snM8FcG3XB3KdgeKpZX7cMPjmUy0x0OdXB+BATIucbGCoAg8IFltXMHytAW9NQ7rFfHvEFLwFKOLlFOfL/yNA4b9ym1HthJX3baiZC0CItgbWu7UgyUjgfSHsp8aNPL5fCZj9BId/eszOny+ON0pRcm4iK+nMXxjhfvBi1Kn3v0vGBdj2ASUhNw3y5QldPbtsTR5Fy7zFfOmrFkRKDxTGfXkFHWGyPB35sVnzV+qthib+lkG1iyjMHy4GmfUO+1P9WKd/0+w18FNA/NDpSIwUAxjHP4nN6HxIzA85ku/UDQcI6HQoxQnN+exPlEgglr87wX3SaeQaGrOZpsxD3TdHCvQTDLaqafA9d+kMVeKqZrwVHTYLlUrCWfCOgQs7Ug81NQQ1HQmjriooQiZg1N/x/lNe4eAviXIuwwGD4t1scw8pSaXP/MOHKKanFzbF3pGEq4su9+UU1uDbh1t0jyovXX3cffRSHUIUWM8o5KgrzAKvnYj6nGSMujNZDr3lQXiBLlRQcsoYFAqGZAtKu1aFYeB+Tkelzp04potX56cGSGDkJuEz2ZXK7vxZq099P/CR0o592xstFB9q5EKyHEX+rIRI0FpaPsF2cHEewivYSTkmSLBvmX3L0dlkTYv4eRiH4vdMgJoauLD4JZatiS/vC3/VK6nqxkvOYv2chSj/0/rV5r2IN2wLEyHwdzTcpFWUAOzb2IMjLRAa1qWPOBEmOYK0OmIpAUSR7duY9fxSAlQZ6sw50TSrrEV2XSfxb3dTWF8VN63LVBFgXlbfVVh5iTcKs3BPPe4WCICPazBRU9U9MiYfyJDYe4jRDEUa11UVnd/FFr0gBUi98h3qRUyokIa3hbRygwMsCQMhCsEgMOjhv0+rK+xjiHLtIq8wW1POMaNql2EVQ3NNKPJRYXwDw9b0tXwMR8PesSPz+F6KLU7sBj296wf76LjpvnVvt7TCh2sIjV/E1zPpGz3iJecYqXuTURo= X-Microsoft-Exchange-Diagnostics: 1; CY1PR03MB2362; 6:LwLVKvq4xiA3jJ4TDYbYL9y8s3onT3q9IsM+MBMG3zknwrfZkPan8ttNmGCz1TL4d8/gDrUiM4HRqpJJDj3qaopBV9JHSD3AC+qD0gd3xE/gTSjc+eLwO5Urlf0Xz4Pu8MCYzdrAP/7VzNcTkhye6lDLfDURCSSwLbu7TXSCOH9tRVeF8WFHhtgK6xSwm7DgRMYH8RGZxXwpGjRxnITwouv8PKt6iY2US4gnB9jOiYozdVZiHw9fXyCHogoAxyb6LIRKn5FqgO6WNxN8PqTz3wJRmBfDeQmuCh0Y8OMx4uuXKkr/VaTU5D/Ig/3PogmXYdrklr0pUAEsMOxg5WwTiWVrzHDDO/SuBsK7J9659IA=; 5:MNdyzZvUnO4VvqoYUBPd5Osw8rkqfXOjhocPHoYh8wzUoNdqVN6hYrFn9zu4StFcjKjVhwnYGrmlSfEeCHHs9jk0LLyKqMCkHugTlskUR4UkPR2VZdsZxZtXKJ21wrCpFCrnn9kHJhFnbycNJXsypLM5be6riCgAN95C0sD7mns=; 24:Dua2oQ9a/y7EziXuARLf70+6Rfk+bUtfqstNJIhZUfBxVQ2C7EnQ9FpAqubtmFlTBSyJTswRUsyXD1YFBRVbEER1FfwtzlwGZFT5/RN49h0=; 7:AAMW9yxdMyDV/NuMNvb/ZEqJ61eV+1TQLWOK02pVWWqxsJpum/gPmlw+1eZpg38yem4W6eUqBcSDO7krIXArxkAMlhl0kgYdle2uxzsL06xIfsZMI3NJ3L8c9P205muE/kGQRcR44nP5DTHzIMAeNtgR3GYZHbbmUmL1wuWZFaNqhloT0KeSkRzJXx7Y2uohSKOySVbsJfqvQch3b++SiIzKgrHxQRd1gFavFcLX2f8CLPW7MpZXLTizj70EIRv9 SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-MS-Exchange-CrossTenant-OriginalArrivalTime: 18 Jan 2018 06:14:09.4348 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 23f07744-8691-47af-53ae-08d55e3ab05a X-MS-Exchange-CrossTenant-Id: 5afe0b00-7697-4969-b663-5eab37d5f47e X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=5afe0b00-7697-4969-b663-5eab37d5f47e; Ip=[192.88.168.50]; Helo=[tx30smr01.am.freescale.net] X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY1PR03MB2362 Subject: [dpdk-dev] [PATCH v3 1/3] kni: support for MAC addr change 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" This patch adds following: 1. Option to configure the mac address during create. Generate random address only if the user has not provided any valid address. 2. Inform usespace, if mac address is being changed in linux. 3. Implement default handling of mac address change in the corresponding ethernet device. Signed-off-by: Hemant Agrawal --- v3: fix compilation err and checkpatch warnings doc/guides/sample_app_ug/kernel_nic_interface.rst | 26 +++++++++++++- examples/kni/main.c | 36 +++++++++++++++++++ .../linuxapp/eal/include/exec-env/rte_kni_common.h | 3 ++ lib/librte_eal/linuxapp/kni/kni_misc.c | 17 +++++---- lib/librte_eal/linuxapp/kni/kni_net.c | 14 +++++++- lib/librte_kni/rte_kni.c | 42 ++++++++++++++++++++-- lib/librte_kni/rte_kni.h | 5 +++ test/test/test_kni.c | 2 ++ 8 files changed, 134 insertions(+), 11 deletions(-) -- 2.7.4 Acked-by: Ferruh Yigit diff --git a/doc/guides/sample_app_ug/kernel_nic_interface.rst b/doc/guides/sample_app_ug/kernel_nic_interface.rst index 67f2140..d7989bf 100644 --- a/doc/guides/sample_app_ug/kernel_nic_interface.rst +++ b/doc/guides/sample_app_ug/kernel_nic_interface.rst @@ -209,6 +209,12 @@ Dumping the network traffic: #tcpdump -i vEth0_0 +Change the MAC address: + +.. code-block:: console + + #ifconfig vEth0_0 hw ether 0C:01:02:03:04:08 + When the DPDK userspace application is closed, all the KNI devices are deleted from Linux*. Explanation @@ -269,11 +275,15 @@ The code for allocating the kernel NIC interfaces for a specific port is as foll conf.addr = dev_info.pci_dev->addr; conf.id = dev_info.pci_dev->id; + /* Get the interface default mac address */ + rte_eth_macaddr_get(port_id, (struct ether_addr *)&conf.mac_addr); + memset(&ops, 0, sizeof(ops)); ops.port_id = port_id; ops.change_mtu = kni_change_mtu; ops.config_network_if = kni_config_network_interface; + ops.config_mac_address = kni_config_mac_address; kni = rte_kni_alloc(pktmbuf_pool, &conf, &ops); } else @@ -502,13 +512,19 @@ Callbacks for Kernel Requests To execute specific PMD operations in user space requested by some Linux* commands, callbacks must be implemented and filled in the struct rte_kni_ops structure. -Currently, setting a new MTU and configuring the network interface (up/ down) are supported. +Currently, setting a new MTU, change in mac address and configuring the network interface(up/ down) +are supported. +Default implementation for following is available in rte_kni library. Application +may choose to not implement follwoing callbacks: + ``config_mac_address`` + .. code-block:: c static struct rte_kni_ops kni_ops = { .change_mtu = kni_change_mtu, .config_network_if = kni_config_network_interface, + .config_mac_address = kni_config_mac_address, }; /* Callback for request of changing MTU */ @@ -587,3 +603,11 @@ Currently, setting a new MTU and configuring the network interface (up/ down) ar RTE_LOG(ERR, APP, "Failed to start port %d\n", port_id); return ret; } + + /* Callback for request of configuring device mac address */ + + static int + kni_config_mac_address(uint16_t port_id, uint8_t mac_addr[]) + { + ..... + } diff --git a/examples/kni/main.c b/examples/kni/main.c index 5700740..ebec433 100644 --- a/examples/kni/main.c +++ b/examples/kni/main.c @@ -131,6 +131,7 @@ static struct kni_interface_stats kni_stats[RTE_MAX_ETHPORTS]; static int kni_change_mtu(uint16_t port_id, unsigned int new_mtu); static int kni_config_network_interface(uint16_t port_id, uint8_t if_up); +static int kni_config_mac_address(uint16_t port_id, uint8_t mac_addr[]); static rte_atomic32_t kni_stop = RTE_ATOMIC32_INIT(0); @@ -767,6 +768,37 @@ kni_config_network_interface(uint16_t port_id, uint8_t if_up) return ret; } +static void +print_ethaddr(const char *name, struct ether_addr *mac_addr) +{ + char buf[ETHER_ADDR_FMT_SIZE]; + ether_format_addr(buf, ETHER_ADDR_FMT_SIZE, mac_addr); + RTE_LOG(INFO, APP, "\t%s%s\n", name, buf); +} + +/* Callback for request of configuring mac address */ +static int +kni_config_mac_address(uint16_t port_id, uint8_t mac_addr[]) +{ + int ret = 0; + + if (port_id >= rte_eth_dev_count() || port_id >= RTE_MAX_ETHPORTS) { + RTE_LOG(ERR, APP, "Invalid port id %d\n", port_id); + return -EINVAL; + } + + RTE_LOG(INFO, APP, "Configure mac address of %d\n", port_id); + print_ethaddr("Address:", (struct ether_addr *)mac_addr); + + ret = rte_eth_dev_default_mac_addr_set(port_id, + (struct ether_addr *)mac_addr); + if (ret < 0) + RTE_LOG(ERR, APP, "Failed to config mac_addr for port %d\n", + port_id); + + return ret; +} + static int kni_alloc(uint16_t port_id) { @@ -810,11 +842,15 @@ kni_alloc(uint16_t port_id) conf.addr = dev_info.pci_dev->addr; conf.id = dev_info.pci_dev->id; } + /* Get the interface default mac address */ + rte_eth_macaddr_get(port_id, + (struct ether_addr *)&conf.mac_addr); memset(&ops, 0, sizeof(ops)); ops.port_id = port_id; ops.change_mtu = kni_change_mtu; ops.config_network_if = kni_config_network_interface; + ops.config_mac_address = kni_config_mac_address; kni = rte_kni_alloc(pktmbuf_pool, &conf, &ops); } else diff --git a/lib/librte_eal/linuxapp/eal/include/exec-env/rte_kni_common.h b/lib/librte_eal/linuxapp/eal/include/exec-env/rte_kni_common.h index 61ace6f..62b4a05 100644 --- a/lib/librte_eal/linuxapp/eal/include/exec-env/rte_kni_common.h +++ b/lib/librte_eal/linuxapp/eal/include/exec-env/rte_kni_common.h @@ -28,6 +28,7 @@ enum rte_kni_req_id { RTE_KNI_REQ_UNKNOWN = 0, RTE_KNI_REQ_CHANGE_MTU, RTE_KNI_REQ_CFG_NETWORK_IF, + RTE_KNI_REQ_CHANGE_MAC_ADDR, RTE_KNI_REQ_MAX, }; @@ -40,6 +41,7 @@ struct rte_kni_request { union { uint32_t new_mtu; /**< New MTU */ uint8_t if_up; /**< 1: interface up, 0: interface down */ + uint8_t mac_addr[6]; /**< MAC address for interface */ }; int32_t result; /**< Result for processing request */ } __attribute__((__packed__)); @@ -116,6 +118,7 @@ struct rte_kni_device_info { /* mbuf size */ unsigned mbuf_size; + char mac_addr[6]; }; #define KNI_DEVICE "kni" diff --git a/lib/librte_eal/linuxapp/kni/kni_misc.c b/lib/librte_eal/linuxapp/kni/kni_misc.c index def4f1e..83649ce 100644 --- a/lib/librte_eal/linuxapp/kni/kni_misc.c +++ b/lib/librte_eal/linuxapp/kni/kni_misc.c @@ -439,12 +439,17 @@ kni_ioctl_create(struct net *net, uint32_t ioctl_num, if (kni->lad_dev) ether_addr_copy(net_dev->dev_addr, kni->lad_dev->dev_addr); - else - /* - * Generate random mac address. eth_random_addr() is the newer - * version of generating mac address in linux kernel. - */ - random_ether_addr(net_dev->dev_addr); + else { + /* if user has provided a valid mac address */ + if (is_valid_ether_addr((unsigned char *)(dev_info.mac_addr))) + memcpy(net_dev->dev_addr, dev_info.mac_addr, ETH_ALEN); + else + /* + * Generate random mac address. eth_random_addr() is the + * newer version of generating mac address in kernel. + */ + random_ether_addr(net_dev->dev_addr); + } ret = register_netdev(net_dev); if (ret) { diff --git a/lib/librte_eal/linuxapp/kni/kni_net.c b/lib/librte_eal/linuxapp/kni/kni_net.c index 07d6fdb..3e35008 100644 --- a/lib/librte_eal/linuxapp/kni/kni_net.c +++ b/lib/librte_eal/linuxapp/kni/kni_net.c @@ -649,12 +649,24 @@ kni_net_rebuild_header(struct sk_buff *skb) static int kni_net_set_mac(struct net_device *netdev, void *p) { + int ret; + struct rte_kni_request req; + struct kni_dev *kni; struct sockaddr *addr = p; + memset(&req, 0, sizeof(req)); + req.req_id = RTE_KNI_REQ_CHANGE_MAC_ADDR; + if (!is_valid_ether_addr((unsigned char *)(addr->sa_data))) return -EADDRNOTAVAIL; + + memcpy(req.mac_addr, addr->sa_data, netdev->addr_len); memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len); - return 0; + + kni = netdev_priv(netdev); + ret = kni_net_process_request(kni, &req); + + return (ret == 0 ? req.result : ret); } #ifdef HAVE_CHANGE_CARRIER_CB diff --git a/lib/librte_kni/rte_kni.c b/lib/librte_kni/rte_kni.c index f4fa994..a140dfc 100644 --- a/lib/librte_kni/rte_kni.c +++ b/lib/librte_kni/rte_kni.c @@ -327,6 +327,8 @@ rte_kni_alloc(struct rte_mempool *pktmbuf_pool, memset(ctx, 0, sizeof(struct rte_kni)); if (ops) memcpy(&ctx->ops, ops, sizeof(struct rte_kni_ops)); + else + ctx->ops.port_id = UINT16_MAX; memset(&dev_info, 0, sizeof(dev_info)); dev_info.bus = conf->addr.bus; @@ -339,6 +341,8 @@ rte_kni_alloc(struct rte_mempool *pktmbuf_pool, dev_info.group_id = conf->group_id; dev_info.mbuf_size = conf->mbuf_size; + memcpy(dev_info.mac_addr, conf->mac_addr, ETHER_ADDR_LEN); + snprintf(ctx->name, RTE_KNI_NAMESIZE, "%s", intf_name); snprintf(dev_info.name, RTE_KNI_NAMESIZE, "%s", intf_name); @@ -499,6 +503,28 @@ rte_kni_release(struct rte_kni *kni) return 0; } +/* default callback for request of configuring device mac address */ +static int +kni_config_mac_address(uint16_t port_id, uint8_t mac_addr[]) +{ + int ret = 0; + + if (port_id >= rte_eth_dev_count() || port_id >= RTE_MAX_ETHPORTS) { + RTE_LOG(ERR, KNI, "Invalid port id %d\n", port_id); + return -EINVAL; + } + + RTE_LOG(INFO, KNI, "Configure mac address of %d", port_id); + + ret = rte_eth_dev_default_mac_addr_set(port_id, + (struct ether_addr *)mac_addr); + if (ret < 0) + RTE_LOG(ERR, KNI, "Failed to config mac_addr for port %d\n", + port_id); + + return ret; +} + int rte_kni_handle_request(struct rte_kni *kni) { @@ -530,6 +556,14 @@ rte_kni_handle_request(struct rte_kni *kni) req->result = kni->ops.config_network_if(\ kni->ops.port_id, req->if_up); break; + case RTE_KNI_REQ_CHANGE_MAC_ADDR: /* Change MAC Address */ + if (kni->ops.config_mac_address) + req->result = kni->ops.config_mac_address( + kni->ops.port_id, req->mac_addr); + else if (kni->ops.port_id != UINT16_MAX) + req->result = kni_config_mac_address( + kni->ops.port_id, req->mac_addr); + break; default: RTE_LOG(ERR, KNI, "Unknown request id %u\n", req->req_id); req->result = -EINVAL; @@ -678,7 +712,9 @@ kni_check_request_register(struct rte_kni_ops *ops) if( NULL == ops ) return KNI_REQ_NO_REGISTER; - if((NULL == ops->change_mtu) && (NULL == ops->config_network_if)) + if ((ops->change_mtu == NULL) + && (ops->config_network_if == NULL) + && (ops->config_mac_address == NULL)) return KNI_REQ_NO_REGISTER; return KNI_REQ_REGISTERED; @@ -717,8 +753,8 @@ rte_kni_unregister_handlers(struct rte_kni *kni) return -1; } - kni->ops.change_mtu = NULL; - kni->ops.config_network_if = NULL; + memset(&kni->ops, 0, sizeof(struct rte_kni_ops)); + return 0; } void diff --git a/lib/librte_kni/rte_kni.h b/lib/librte_kni/rte_kni.h index c4dcfff..370b02a 100644 --- a/lib/librte_kni/rte_kni.h +++ b/lib/librte_kni/rte_kni.h @@ -20,6 +20,7 @@ #include #include #include +#include #include @@ -41,6 +42,9 @@ struct rte_kni_ops { /* Pointer to function of configuring network interface */ int (*config_network_if)(uint16_t port_id, uint8_t if_up); + + /* Pointer to function of configuring mac address */ + int (*config_mac_address)(uint16_t port_id, uint8_t mac_addr[]); }; /** @@ -61,6 +65,7 @@ struct rte_kni_conf { __extension__ uint8_t force_bind : 1; /* Flag to bind kernel thread */ + char mac_addr[ETHER_ADDR_LEN]; /* MAC address assigned to KNI */ }; /** diff --git a/test/test/test_kni.c b/test/test/test_kni.c index 539d231..4513de1 100644 --- a/test/test/test_kni.c +++ b/test/test/test_kni.c @@ -74,6 +74,7 @@ static const struct rte_eth_conf port_conf = { static struct rte_kni_ops kni_ops = { .change_mtu = NULL, .config_network_if = NULL, + .config_mac_address = NULL, }; static unsigned lcore_master, lcore_ingress, lcore_egress; @@ -231,6 +232,7 @@ test_kni_register_handler_mp(void) struct rte_kni_ops ops = { .change_mtu = kni_change_mtu, .config_network_if = NULL, + .config_mac_address = NULL, }; if (!kni) {