From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from NAM04-BN3-obe.outbound.protection.outlook.com (mail-eopbgr680080.outbound.protection.outlook.com [40.107.68.80]) by dpdk.org (Postfix) with ESMTP id 9C3511B149 for ; Thu, 27 Sep 2018 12:49:12 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=CAVIUMNETWORKS.onmicrosoft.com; s=selector1-cavium-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=hVC3AhYWk6tP+oL3Oh+hwT5zM95qg7nyBYq11KX0l2A=; b=cq4kW3ZJyk0XBevWouXwd65bE9nIQRApI8UVERfzzuLd8RFRw7fcb2W1wAonphyrvZ6nr5XjWmDM5FzbHu5n/qxlb2D+gUe1sSjrCtq4sQocrBxobWS2lkLb5ZsZdM3uoo4PZzaqOLOYy5gu/0G8Bh497Yel7G6Rjeyfh4z3/uQ= Received: from BYAPR07MB4950.namprd07.prod.outlook.com (52.135.238.203) by BYAPR07MB4151.namprd07.prod.outlook.com (52.135.222.140) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.1164.25; Thu, 27 Sep 2018 10:49:10 +0000 Received: from BYAPR07MB4950.namprd07.prod.outlook.com ([fe80::588d:e152:14fa:fe14]) by BYAPR07MB4950.namprd07.prod.outlook.com ([fe80::588d:e152:14fa:fe14%2]) with mapi id 15.20.1164.024; Thu, 27 Sep 2018 10:49:10 +0000 From: Kiran Kumar To: "ferruh.yigit@intel.com" CC: "dev@dpdk.org" , "anatoly.burakov@intel.com" , "Jacob, Jerin" , "hemant.agrawal@nxp.com" , "jianfeng.tan@intel.com" , "Kokkilagadda, Kiran" Thread-Topic: [dpdk-dev] [PATCH] kni: add IOVA va support for kni Thread-Index: AQHUVk+4ittzQMwGpEqVEVCzHdKaAg== Date: Thu, 27 Sep 2018 10:49:10 +0000 Message-ID: <20180927104846.16356-1-kkokkilagadda@caviumnetworks.com> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-clientproxiedby: SN6PR0102CA0031.prod.exchangelabs.com (2603:10b6:805:1::44) To BYAPR07MB4950.namprd07.prod.outlook.com (2603:10b6:a03:5b::11) x-ms-exchange-messagesentrepresentingtype: 1 x-originating-ip: [111.93.218.67] x-ms-publictraffictype: Email x-microsoft-exchange-diagnostics: 1; BYAPR07MB4151; 6:BxnEmY49OGJclvj48dZxtAjYY/cgqvffC0dcX44Cg4PyMQDbGf4MkLNNjO2U1QktjOIM1u6KQFObfSKchozFpT9AYbeAAmZawWjtLUxEGmCTnr6TMrvwTmSoVO6Uy/EL4Bf67lsrtToEADl4QWZk0vC7kQFi+e/Z5cqtqKF1A6DaU+AigHhivHgjJ4Se6EjIGH2CsMXln84BbYgzw4+ID83EbqKgzS89CXt5Y0I5i6vII10sAG4R+0W9K29bc2TZmU5Mm1StO+2Aqqbe43HqKqQHrf4nPjG3MGkceyqZZswpmACEBYeQUi7WHhHNuZrJMtaU0B0vrAaOnAkDNUaSjMpv9vf0SLxAbOsCvevvX83RAlzu49OiqWhm3bHa3NSFA2ZCQtF8dkucEjBJo9a/92caYT0QqoBWWek1S0z8mHDdLB1+qXrndglH27V1884xbRS2X3UsZ1IZLlfTRrHQkg==; 5:OivM3cQfWJPUBnfRj3uX26vlofqr4AwNoPh4kcNlXJ45KeCrIZwmaEkJmtDtB5c2M63KlMvMmCn9iNTVAjWEYyyjFQOUyRe3fiWAKT3xnW6T5OZFWREhqUwd5jNtPdBlqa4u6RLbMwzIDNeF37/IsJADgoGp1A18uZDzlFt+Uf0=; 7:ayPvw/c4GAzl3wU1pgDQSF2l2TowrZTrJIFTuMMuC1U2nGbyuzQQ++YfhM/mw9XdbCgQEeLil5i7bqCRKfU7bO4JoLZH1j5EIlCotrqD8AvodanMdfJxW8MbOGiPjlaRR2xhGCOSxhZ2SIQs+TLkfuTtYdhAO5B5IFLKataBCo3PnpyXM1/C6cYTGZdU2bu9yXs6wjARIazUyRvDTN1vXOnsIGluJWXtX8eWqH+8KZapRf3qbDNOmCwSrdR3dWuN x-ms-office365-filtering-correlation-id: ef5dc2b2-1e48-4ab5-e17c-08d62466dac3 x-microsoft-antispam: BCL:0; PCL:0; RULEID:(7020095)(4652040)(8989299)(4534165)(4627221)(201703031133081)(201702281549075)(8990200)(5600074)(711020)(2017052603328)(7153060)(7193020); SRVR:BYAPR07MB4151; x-ms-traffictypediagnostic: BYAPR07MB4151: authentication-results: spf=none (sender IP is ) smtp.mailfrom=Kiran.Kokkilagadda@cavium.com; x-microsoft-antispam-prvs: x-exchange-antispam-report-test: UriScan:(278428928389397); x-ms-exchange-senderadcheck: 1 x-exchange-antispam-report-cfa-test: BCL:0; PCL:0; RULEID:(8211001083)(6040522)(2401047)(5005006)(8121501046)(3002001)(93006095)(10201501046)(3231355)(944501410)(52105095)(149066)(150057)(6041310)(20161123558120)(201703131423095)(201702281528075)(20161123555045)(201703061421075)(201703061406153)(20161123562045)(20161123564045)(20161123560045)(201708071742011)(7699051)(76991041); SRVR:BYAPR07MB4151; BCL:0; PCL:0; RULEID:; SRVR:BYAPR07MB4151; x-forefront-prvs: 0808323E97 x-forefront-antispam-report: SFV:NSPM; SFS:(10009020)(396003)(39860400002)(136003)(376002)(366004)(346002)(189003)(199004)(99286004)(8676002)(476003)(81166006)(66066001)(6512007)(53946003)(52116002)(6486002)(54906003)(14444005)(53936002)(386003)(6506007)(2616005)(256004)(5250100002)(102836004)(305945005)(7736002)(34290500001)(1857600001)(186003)(26005)(97736004)(81156014)(42882007)(2351001)(5660300001)(105586002)(3846002)(6916009)(8936002)(478600001)(2906002)(106356001)(72206003)(68736007)(2900100001)(6436002)(6116002)(316002)(5640700003)(2501003)(107886003)(36756003)(486006)(14454004)(25786009)(1076002)(4326008)(71190400001)(71200400001); DIR:OUT; SFP:1101; SCL:1; SRVR:BYAPR07MB4151; H:BYAPR07MB4950.namprd07.prod.outlook.com; FPR:; SPF:None; LANG:en; PTR:InfoNoRecords; MX:1; A:1; received-spf: None (protection.outlook.com: cavium.com does not designate permitted sender hosts) x-microsoft-antispam-message-info: F8j23tArlgG9Tz0pYcoT23RO7awtq8cbfTvy04qthl8H9PZ94pIIM6cU9QYoyMVtuLiDs+zUTArJt5f1jMFzVYRxCb8DAGAKRVyr9u/qoPbhuEf5YuMx7wCT5SZTdZ6eKnhZce19OxgovLD22/s/XMzHNg2KQ5wWmN31GDcuXpYb+vXEg495AinXjrtr7G0IpZbYh4PVhGCLCEhYD15hFIwXDPJuY0A+51yEd5bWOOz79eGlIGPtBG4jJfApNauiPJ/kOHyJlwuCQ094kgs8TTDjAGGlatYZ2YvS9+gzeQIrLU163zrjeT/L0+Jr32Wi6mAAupo8emH0CkAUKkejSxvskEo2c4jygMu0J+cmJws= spamdiagnosticoutput: 1:99 spamdiagnosticmetadata: NSPM Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-OriginatorOrg: caviumnetworks.com X-MS-Exchange-CrossTenant-Network-Message-Id: ef5dc2b2-1e48-4ab5-e17c-08d62466dac3 X-MS-Exchange-CrossTenant-originalarrivaltime: 27 Sep 2018 10:49:10.3163 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 711e4ccf-2e9b-4bcf-a551-4094005b6194 X-MS-Exchange-Transport-CrossTenantHeadersStamped: BYAPR07MB4151 Subject: [dpdk-dev] [PATCH] kni: add IOVA va support for kni 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: , X-List-Received-Date: Thu, 27 Sep 2018 10:49:13 -0000 With current KNI implementation kernel module will work only in IOVA=3DPA mode. This patch will add support for kernel module to work with IOVA=3DVA mode. The idea is to maintain a mapping in KNI module between user pages and kernel pages and in fast path perform a lookup in this table and get the kernel virtual address for corresponding user virtual address. In IOVA=3DVA mode, the memory allocated to the pool is physically and virtually contiguous. We will take advantage of this and create a mapping in the kernel.In kernel we need mapping for queues (tx_q, rx_q,... slow path) and mbuf memory (fast path). At the KNI init time, in slow path we will create a mapping for the queues and mbuf using get_user_pages similar to af_xdp. Using pool memory base address, we will create a page map table for the mbuf, which we will use in the fast path for kernel page translation. At KNI init time, we will pass the base address of the pool and size of the pool to kernel. In kernel, using get_user_pages API, we will get the pages with size PAGE_SIZE and store the mapping and start address of user space in a table. In fast path for any user address perform PAGE_SHIFT (user_addr >> PAGE_SHIFT) and subtract the start address from this value, we will get the index of the kernel page with in the page map table. Adding offset to this kernel page address, we will get the kernel address for this user virtual address. For example user pool base address is X, and size is S that we passed to kernel. In kernel we will create a mapping for this using get_user_pages. Our page map table will look like [Y, Y+PAGE_SIZE, Y+(PAGE_SIZE*2) ....] and user start page will be U (we will get it from X >> PAGE_SHIFT). For any user address Z we will get the index of the page map table using ((Z >> PAGE_SHIFT) - U). Adding offset (Z & (PAGE_SIZE - 1)) to this address will give kernel virtual address. Signed-off-by: Kiran Kumar --- kernel/linux/kni/kni_dev.h | 37 +++ kernel/linux/kni/kni_misc.c | 211 +++++++++++++++++- kernel/linux/kni/kni_net.c | 112 ++++++++-- lib/librte_eal/linuxapp/eal/eal.c | 9 - .../eal/include/exec-env/rte_kni_common.h | 8 + lib/librte_kni/rte_kni.c | 21 ++ 6 files changed, 363 insertions(+), 35 deletions(-) diff --git a/kernel/linux/kni/kni_dev.h b/kernel/linux/kni/kni_dev.h index 6275ef27f..6a92da497 100644 --- a/kernel/linux/kni/kni_dev.h +++ b/kernel/linux/kni/kni_dev.h @@ -29,10 +29,47 @@ =20 #define MBUF_BURST_SZ 32 =20 +struct iova_page_info { + /* User to kernel page table map, used for + * fast path lookup + */ + struct mbuf_page { + void *addr; + } *page_map; + + /* Page mask */ + u64 page_mask; + + /* Start page for user address */ + u64 start_page; + + struct page_info { + /* Physical pages returned by get_user_pages */ + struct page **pgs; + + /* Number of Pages returned by get_user_pages */ + u32 npgs; + } page_info; + + /* Queue info */ + struct page_info tx_q; + struct page_info rx_q; + struct page_info alloc_q; + struct page_info free_q; + struct page_info req_q; + struct page_info resp_q; + struct page_info sync_va; +}; + /** * A structure describing the private information for a kni device. */ struct kni_dev { + /* Page info for IOVA=3DVA mode */ + struct iova_page_info va_info; + /* IOVA mode 0 =3D PA, 1 =3D VA */ + uint8_t iova_mode; + /* kni list */ struct list_head list; =20 diff --git a/kernel/linux/kni/kni_misc.c b/kernel/linux/kni/kni_misc.c index fa69f8e63..2627c1f69 100644 --- a/kernel/linux/kni/kni_misc.c +++ b/kernel/linux/kni/kni_misc.c @@ -197,6 +197,117 @@ kni_dev_remove(struct kni_dev *dev) return 0; } =20 +static void +kni_unpin_pages(struct page_info *mem) +{ + u32 i; + + /* Set the user pages as dirty, so that these pages will not be + * allocated to other applications until we release them. + */ + for (i =3D 0; i < mem->npgs; i++) { + struct page *page =3D mem->pgs[i]; + + set_page_dirty_lock(page); + put_page(page); + } + + kfree(mem->pgs); + mem->pgs =3D NULL; +} + +static void +kni_clean_queue(struct page_info *mem) +{ + if (mem->pgs) { + set_page_dirty_lock(mem->pgs[0]); + put_page(mem->pgs[0]); + kfree(mem->pgs); + mem->pgs =3D NULL; + } +} + +static void +kni_cleanup_iova(struct iova_page_info *mem) +{ + kni_unpin_pages(&mem->page_info); + kfree(mem->page_map); + mem->page_map =3D NULL; + + kni_clean_queue(&mem->tx_q); + kni_clean_queue(&mem->rx_q); + kni_clean_queue(&mem->alloc_q); + kni_clean_queue(&mem->free_q); + kni_clean_queue(&mem->req_q); + kni_clean_queue(&mem->resp_q); + kni_clean_queue(&mem->sync_va); +} + +int +kni_pin_pages(void *address, size_t size, struct page_info *mem) +{ + unsigned int gup_flags =3D FOLL_WRITE; + long npgs; + int err; + + /* Get at least one page */ + if (size < PAGE_SIZE) + size =3D PAGE_SIZE; + + /* Compute number of user pages based on page size */ + mem->npgs =3D (size + PAGE_SIZE - 1) / PAGE_SIZE; + + /* Allocate memory for the pages */ + mem->pgs =3D kcalloc(mem->npgs, sizeof(*mem->pgs), + GFP_KERNEL | __GFP_NOWARN); + if (!mem->pgs) { + pr_err("%s: -ENOMEM\n", __func__); + return -ENOMEM; + } + + down_write(¤t->mm->mmap_sem); + + /* Get the user pages from the user address*/ + npgs =3D get_user_pages((u64)address, mem->npgs, + gup_flags, &mem->pgs[0], NULL); + up_write(¤t->mm->mmap_sem); + + /* We didn't get all the requested pages, throw error */ + if (npgs !=3D mem->npgs) { + if (npgs >=3D 0) { + mem->npgs =3D npgs; + err =3D -ENOMEM; + pr_err("%s: -ENOMEM\n", __func__); + goto out_pin; + } + err =3D npgs; + goto out_pgs; + } + return 0; + +out_pin: + kni_unpin_pages(mem); +out_pgs: + kfree(mem->pgs); + mem->pgs =3D NULL; + return err; +} + +static void* +kni_map_queue(struct kni_dev *kni, u64 addr, + struct page_info *mm) +{ + /* Map atleast 1 page */ + if (kni_pin_pages((void *)addr, PAGE_SIZE, + mm) !=3D 0) { + pr_err("Unable to pin pages\n"); + return NULL; + } + + return (page_address(mm->pgs[0]) + + (addr & kni->va_info.page_mask)); +} + static int kni_release(struct inode *inode, struct file *file) { @@ -224,6 +335,11 @@ kni_release(struct inode *inode, struct file *file) } =20 kni_dev_remove(dev); + + /* IOVA=3DVA mode, unpin pages */ + if (likely(dev->iova_mode =3D=3D 1)) + kni_cleanup_iova(&dev->va_info); + list_del(&dev->list); } up_write(&knet->kni_list_lock); @@ -364,18 +480,94 @@ kni_ioctl_create(struct net *net, uint32_t ioctl_num, strncpy(kni->name, dev_info.name, RTE_KNI_NAMESIZE); =20 /* Translate user space info into kernel space info */ - kni->tx_q =3D phys_to_virt(dev_info.tx_phys); - kni->rx_q =3D phys_to_virt(dev_info.rx_phys); - kni->alloc_q =3D phys_to_virt(dev_info.alloc_phys); - kni->free_q =3D phys_to_virt(dev_info.free_phys); + kni->iova_mode =3D dev_info.iova_mode; =20 - kni->req_q =3D phys_to_virt(dev_info.req_phys); - kni->resp_q =3D phys_to_virt(dev_info.resp_phys); - kni->sync_va =3D dev_info.sync_va; - kni->sync_kva =3D phys_to_virt(dev_info.sync_phys); + if (kni->iova_mode) { + u64 mbuf_addr; + int i; + + /* map userspace memory info */ + mbuf_addr =3D (u64)dev_info.mbuf_va; + + /* Pre compute page mask, used in fast path */ + kni->va_info.page_mask =3D (u64)(PAGE_SIZE - 1); =20 + /* Store start page address, This is the reference + * for all the user virtual address + */ + kni->va_info.start_page =3D (mbuf_addr >> PAGE_SHIFT); + + /* Get and pin the user pages */ + if (kni_pin_pages(dev_info.mbuf_va, dev_info.mbuf_pool_size, + &kni->va_info.page_info) !=3D 0) { + pr_err("Unable to pin pages\n"); + return -1; + } + + /* Page map table between user and kernel pages */ + kni->va_info.page_map =3D kcalloc(kni->va_info.page_info.npgs, + sizeof(struct mbuf_page), + GFP_KERNEL); + if (kni->va_info.page_map =3D=3D NULL) { + pr_err("Out of memory\n"); + return -ENOMEM; + } + + /* Conver the user pages to kernel pages */ + for (i =3D 0; i < kni->va_info.page_info.npgs; i++) { + kni->va_info.page_map[i].addr =3D + page_address(kni->va_info.page_info.pgs[i]); + } + + /* map queues */ + kni->tx_q =3D kni_map_queue(kni, dev_info.tx_phys, + &kni->va_info.tx_q); + if (kni->tx_q =3D=3D NULL) + goto iova_err; + + kni->rx_q =3D kni_map_queue(kni, dev_info.rx_phys, + &kni->va_info.rx_q); + if (kni->rx_q =3D=3D NULL) + goto iova_err; + + kni->alloc_q =3D kni_map_queue(kni, dev_info.alloc_phys, + &kni->va_info.alloc_q); + if (kni->alloc_q =3D=3D NULL) + goto iova_err; + + kni->free_q =3D kni_map_queue(kni, dev_info.free_phys, + &kni->va_info.free_q); + if (kni->free_q =3D=3D NULL) + goto iova_err; + + kni->req_q =3D kni_map_queue(kni, dev_info.req_phys, + &kni->va_info.req_q); + if (kni->req_q =3D=3D NULL) + goto iova_err; + + kni->resp_q =3D kni_map_queue(kni, dev_info.resp_phys, + &kni->va_info.resp_q); + if (kni->resp_q =3D=3D NULL) + goto iova_err; + + kni->sync_kva =3D kni_map_queue(kni, dev_info.sync_phys, + &kni->va_info.sync_va); + if (kni->sync_kva =3D=3D NULL) + goto iova_err; + } else { + /* Address tranlation for IOVA=3DPA mode */ + kni->tx_q =3D phys_to_virt(dev_info.tx_phys); + kni->rx_q =3D phys_to_virt(dev_info.rx_phys); + kni->alloc_q =3D phys_to_virt(dev_info.alloc_phys); + kni->free_q =3D phys_to_virt(dev_info.free_phys); + kni->req_q =3D phys_to_virt(dev_info.req_phys); + kni->resp_q =3D phys_to_virt(dev_info.resp_phys); + kni->sync_kva =3D phys_to_virt(dev_info.sync_phys); + } + kni->sync_va =3D dev_info.sync_va; kni->mbuf_size =3D dev_info.mbuf_size; =20 + pr_debug("tx_phys: 0x%016llx, tx_q addr: 0x%p\n", (unsigned long long) dev_info.tx_phys, kni->tx_q); pr_debug("rx_phys: 0x%016llx, rx_q addr: 0x%p\n", @@ -475,6 +667,9 @@ kni_ioctl_create(struct net *net, uint32_t ioctl_num, up_write(&knet->kni_list_lock); =20 return 0; +iova_err: + kni_cleanup_iova(&kni->va_info); + return -1; } =20 static int diff --git a/kernel/linux/kni/kni_net.c b/kernel/linux/kni/kni_net.c index 7fcfa106c..d6d76a32f 100644 --- a/kernel/linux/kni/kni_net.c +++ b/kernel/linux/kni/kni_net.c @@ -35,6 +35,25 @@ static void kni_net_rx_normal(struct kni_dev *kni); /* kni rx function pointer, with default to normal rx */ static kni_net_rx_t kni_net_rx_func =3D kni_net_rx_normal; =20 + +/* Get the kernel address from the user address using + * page map table. Will be used only in IOVA=3DVA mode + */ +static inline void* +get_kva(uint64_t usr_addr, struct kni_dev *kni) +{ + uint32_t index; + /* User page - start user page will give the index + * with in the page map table + */ + index =3D (usr_addr >> PAGE_SHIFT) - kni->va_info.start_page; + + /* Add the offset to the page address */ + return (kni->va_info.page_map[index].addr + + (usr_addr & kni->va_info.page_mask)); + +} + /* physical address to kernel virtual address */ static void * pa2kva(void *pa) @@ -181,7 +200,10 @@ kni_fifo_trans_pa2va(struct kni_dev *kni, return; =20 for (i =3D 0; i < num_rx; i++) { - kva =3D pa2kva(kni->pa[i]); + if (likely(kni->iova_mode =3D=3D 1)) + kva =3D get_kva((u64)(kni->pa[i]), kni); + else + kva =3D pa2kva(kni->pa[i]); kni->va[i] =3D pa2va(kni->pa[i], kva); } =20 @@ -258,8 +280,15 @@ kni_net_tx(struct sk_buff *skb, struct net_device *dev= ) if (likely(ret =3D=3D 1)) { void *data_kva; =20 - pkt_kva =3D pa2kva(pkt_pa); - data_kva =3D kva2data_kva(pkt_kva); + if (likely(kni->iova_mode =3D=3D 1)) { + pkt_kva =3D get_kva((u64)pkt_pa, kni); + data_kva =3D (uint8_t *)pkt_kva + + (sizeof(struct rte_kni_mbuf) + + pkt_kva->data_off); + } else { + pkt_kva =3D pa2kva(pkt_pa); + data_kva =3D kva2data_kva(pkt_kva); + } pkt_va =3D pa2va(pkt_pa, pkt_kva); =20 len =3D skb->len; @@ -330,9 +359,15 @@ kni_net_rx_normal(struct kni_dev *kni) =20 /* Transfer received packets to netif */ for (i =3D 0; i < num_rx; i++) { - kva =3D pa2kva(kni->pa[i]); + if (likely(kni->iova_mode =3D=3D 1)) { + kva =3D get_kva((u64)kni->pa[i], kni); + data_kva =3D (uint8_t *)kva + + (sizeof(struct rte_kni_mbuf) + kva->data_off); + } else { + kva =3D pa2kva(kni->pa[i]); + data_kva =3D kva2data_kva(kva); + } len =3D kva->pkt_len; - data_kva =3D kva2data_kva(kva); kni->va[i] =3D pa2va(kni->pa[i], kva); =20 skb =3D dev_alloc_skb(len + 2); @@ -358,8 +393,17 @@ kni_net_rx_normal(struct kni_dev *kni) if (!kva->next) break; =20 - kva =3D pa2kva(va2pa(kva->next, kva)); - data_kva =3D kva2data_kva(kva); + if (likely(kni->iova_mode =3D=3D 1)) { + kva =3D get_kva( + (u64)va2pa(kva->next, kva), + kni); + data_kva =3D (uint8_t *)kva + + (sizeof(struct rte_kni_mbuf) + + kva->data_off); + } else { + kva =3D pa2kva(va2pa(kva->next, kva)); + data_kva =3D kva2data_kva(kva); + } } } =20 @@ -429,14 +473,31 @@ kni_net_rx_lo_fifo(struct kni_dev *kni) num =3D ret; /* Copy mbufs */ for (i =3D 0; i < num; i++) { - kva =3D pa2kva(kni->pa[i]); - len =3D kva->pkt_len; - data_kva =3D kva2data_kva(kva); - kni->va[i] =3D pa2va(kni->pa[i], kva); + if (likely(kni->iova_mode =3D=3D 1)) { + kva =3D get_kva((u64)(kni->pa[i]), kni); + len =3D kva->pkt_len; + data_kva =3D (uint8_t *)kva + + (sizeof(struct rte_kni_mbuf) + + kva->data_off); + kni->va[i] =3D pa2va(kni->pa[i], kva); + alloc_kva =3D get_kva((u64)(kni->alloc_pa[i]), + kni); + alloc_data_kva =3D (uint8_t *)alloc_kva + + (sizeof(struct rte_kni_mbuf) + + alloc_kva->data_off); + kni->alloc_va[i] =3D pa2va(kni->alloc_pa[i], + alloc_kva); + } else { + kva =3D pa2kva(kni->pa[i]); + len =3D kva->pkt_len; + data_kva =3D kva2data_kva(kva); + kni->va[i] =3D pa2va(kni->pa[i], kva); =20 - alloc_kva =3D pa2kva(kni->alloc_pa[i]); - alloc_data_kva =3D kva2data_kva(alloc_kva); - kni->alloc_va[i] =3D pa2va(kni->alloc_pa[i], alloc_kva); + alloc_kva =3D pa2kva(kni->alloc_pa[i]); + alloc_data_kva =3D kva2data_kva(alloc_kva); + kni->alloc_va[i] =3D pa2va(kni->alloc_pa[i], + alloc_kva); + } =20 memcpy(alloc_data_kva, data_kva, len); alloc_kva->pkt_len =3D len; @@ -502,9 +563,15 @@ kni_net_rx_lo_fifo_skb(struct kni_dev *kni) =20 /* Copy mbufs to sk buffer and then call tx interface */ for (i =3D 0; i < num; i++) { - kva =3D pa2kva(kni->pa[i]); + if (likely(kni->iova_mode =3D=3D 1)) { + kva =3D get_kva((u64)(kni->pa[i]), kni); + data_kva =3D (uint8_t *)kva + + (sizeof(struct rte_kni_mbuf) + kva->data_off); + } else { + kva =3D pa2kva(kni->pa[i]); + data_kva =3D kva2data_kva(kva); + } len =3D kva->pkt_len; - data_kva =3D kva2data_kva(kva); kni->va[i] =3D pa2va(kni->pa[i], kva); =20 skb =3D dev_alloc_skb(len + 2); @@ -540,8 +607,17 @@ kni_net_rx_lo_fifo_skb(struct kni_dev *kni) if (!kva->next) break; =20 - kva =3D pa2kva(va2pa(kva->next, kva)); - data_kva =3D kva2data_kva(kva); + if (likely(kni->iova_mode =3D=3D 1)) { + kva =3D get_kva( + (u64)(va2pa(kva->next, kva)), + kni); + data_kva =3D (uint8_t *)kva + + (sizeof(struct rte_kni_mbuf) + + kva->data_off); + } else { + kva =3D pa2kva(va2pa(kva->next, kva)); + data_kva =3D kva2data_kva(kva); + } } } =20 diff --git a/lib/librte_eal/linuxapp/eal/eal.c b/lib/librte_eal/linuxapp/ea= l/eal.c index e59ac6577..7d5b2ebfa 100644 --- a/lib/librte_eal/linuxapp/eal/eal.c +++ b/lib/librte_eal/linuxapp/eal/eal.c @@ -875,15 +875,6 @@ rte_eal_init(int argc, char **argv) /* autodetect the iova mapping mode (default is iova_pa) */ rte_eal_get_configuration()->iova_mode =3D rte_bus_get_iommu_class(); =20 - /* Workaround for KNI which requires physical address to work */ - if (rte_eal_get_configuration()->iova_mode =3D=3D RTE_IOVA_VA && - rte_eal_check_module("rte_kni") =3D=3D 1) { - rte_eal_get_configuration()->iova_mode =3D RTE_IOVA_PA; - RTE_LOG(WARNING, EAL, - "Some devices want IOVA as VA but PA will be used because.. " - "KNI module inserted\n"); - } - if (internal_config.no_hugetlbfs =3D=3D 0) { /* rte_config isn't initialized yet */ ret =3D internal_config.process_type =3D=3D RTE_PROC_PRIMARY ? 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 cfa9448bd..706756e6c 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 @@ -122,6 +122,14 @@ struct rte_kni_device_info { unsigned mbuf_size; unsigned int mtu; char mac_addr[6]; + + /* IOVA mode. 1 =3D VA, 0 =3D PA */ + uint8_t iova_mode; + + /* Pool size, will be used in kernel to map the + * user pages + */ + uint64_t mbuf_pool_size; }; =20 #define KNI_DEVICE "kni" diff --git a/lib/librte_kni/rte_kni.c b/lib/librte_kni/rte_kni.c index 65f6a2b03..1e79e68f1 100644 --- a/lib/librte_kni/rte_kni.c +++ b/lib/librte_kni/rte_kni.c @@ -397,6 +397,27 @@ rte_kni_alloc(struct rte_mempool *pktmbuf_pool, ctx->slot_id =3D slot->id; ctx->mbuf_size =3D conf->mbuf_size; =20 + dev_info.iova_mode =3D (rte_eal_iova_mode() =3D=3D RTE_IOVA_VA) ? 1 : 0; + if (dev_info.iova_mode) { + struct rte_mempool_memhdr *hdr; + uint64_t pool_size =3D 0; + + /* In each pool header chunk, we will maintain the + * base address of the pool. This chunk is physically and + * virtually contiguous. + * This approach will work, only if the allocated pool + * memory is contiguous, else it won't work + */ + hdr =3D STAILQ_FIRST(&pktmbuf_pool->mem_list); + dev_info.mbuf_va =3D (void *)(hdr->addr); + + /* Traverse the list and get the total size of the pool */ + STAILQ_FOREACH(hdr, &pktmbuf_pool->mem_list, next) { + pool_size +=3D hdr->len; + } + dev_info.mbuf_pool_size =3D pool_size + + pktmbuf_pool->mz->len; + } ret =3D ioctl(kni_fd, RTE_KNI_IOCTL_CREATE, &dev_info); KNI_MEM_CHECK(ret < 0); =20 --=20 2.17.1