From patchwork Tue Sep 13 03:52:08 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Gonglei (Arei)" X-Patchwork-Id: 9328347 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id B49106048F for ; Tue, 13 Sep 2016 04:03:26 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A376C29051 for ; Tue, 13 Sep 2016 04:03:26 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 983B72908A; Tue, 13 Sep 2016 04:03:26 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id A5FC829051 for ; Tue, 13 Sep 2016 04:03:25 +0000 (UTC) Received: from localhost ([::1]:46353 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bjewO-0005An-Q8 for patchwork-qemu-devel@patchwork.kernel.org; Tue, 13 Sep 2016 00:03:24 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:37615) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bjemq-0006Qj-DE for qemu-devel@nongnu.org; Mon, 12 Sep 2016 23:53:36 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bjemo-00038F-Cp for qemu-devel@nongnu.org; Mon, 12 Sep 2016 23:53:32 -0400 Received: from szxga02-in.huawei.com ([119.145.14.65]:40530) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bjemn-00036X-Fr for qemu-devel@nongnu.org; Mon, 12 Sep 2016 23:53:30 -0400 Received: from 172.24.1.60 (EHLO SZXEML424-HUB.china.huawei.com) ([172.24.1.60]) by szxrg02-dlp.huawei.com (MOS 4.3.7-GA FastPath queued) with ESMTP id DMZ70101; Tue, 13 Sep 2016 11:52:54 +0800 (CST) Received: from localhost (10.177.18.62) by SZXEML424-HUB.china.huawei.com (10.82.67.153) with Microsoft SMTP Server id 14.3.235.1; Tue, 13 Sep 2016 11:52:46 +0800 From: Gonglei To: , Date: Tue, 13 Sep 2016 11:52:08 +0800 Message-ID: <1473738741-220600-3-git-send-email-arei.gonglei@huawei.com> X-Mailer: git-send-email 2.6.3.windows.1 In-Reply-To: <1473738741-220600-1-git-send-email-arei.gonglei@huawei.com> References: <1473738741-220600-1-git-send-email-arei.gonglei@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.177.18.62] X-CFilter-Loop: Reflected X-Mirapoint-Virus-RAPID-Raw: score=unknown(0), refid=str=0001.0A020205.57D77818.003A, ss=1, re=0.000, recu=0.000, reip=0.000, cl=1, cld=1, fgs=0, ip=0.0.0.0, so=2013-06-18 04:22:30, dmn=2013-03-21 17:37:32 X-Mirapoint-Loop-Id: d5ccff27ddc4a4547b57794e2da51fe9 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4.x-2.6.x [generic] X-Received-From: 119.145.14.65 Subject: [Qemu-devel] [PATCH v2 02/15] crypto: introduce crypto queue handler X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: claudio.fontana@huawei.com, mst@redhat.com, xin.zeng@intel.com, luonengjun@huawei.com, peter.huangpeng@huawei.com, agraf@suse.de, nmorey@kalray.eu, mike.caraman@nxp.com, Gonglei , stefanha@redhat.com, pbonzini@redhat.com, vincent.jardin@6wind.com, weidong.huang@huawei.com Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP crypto queue is a gallery used for executing crypto operation, which supports both synchronization and asynchronization. The thoughts stolen from net/queue.c Signed-off-by: Gonglei --- crypto/Makefile.objs | 1 + crypto/crypto-queue.c | 206 ++++++++++++++++++++++++++++++++++++++++++ crypto/crypto.c | 28 ++++++ include/crypto/crypto-queue.h | 69 ++++++++++++++ include/crypto/crypto.h | 12 +++ 5 files changed, 316 insertions(+) create mode 100644 crypto/crypto-queue.c create mode 100644 include/crypto/crypto-queue.h diff --git a/crypto/Makefile.objs b/crypto/Makefile.objs index 2a63cb8..652b429 100644 --- a/crypto/Makefile.objs +++ b/crypto/Makefile.objs @@ -27,6 +27,7 @@ crypto-obj-y += block.o crypto-obj-y += block-qcow.o crypto-obj-y += block-luks.o crypto-obj-y += crypto.o +crypto-obj-y += crypto-queue.o # Let the userspace emulators avoid linking gnutls/etc crypto-aes-obj-y = aes.o diff --git a/crypto/crypto-queue.c b/crypto/crypto-queue.c new file mode 100644 index 0000000..3e91be9 --- /dev/null +++ b/crypto/crypto-queue.c @@ -0,0 +1,206 @@ +/* + * Queue management for crypto device (based on net/qeueu.c) + * + * Copyright (c) 2003-2008 Fabrice Bellard + * Copyright (c) 2016 HUAWEI TECHNOLOGIES CO., LTD. + * + * Authors: + * Gonglei + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "qemu/osdep.h" +#include "crypto/crypto-queue.h" +#include "crypto/crypto.h" +#include "qemu/queue.h" + + +/* The delivery handler may only return zero if it will call + * qemu_crypto_queue_flush() when it determines that it is once again able + * to deliver packets. It must also call qemu_crypto_queue_purge() in its + * cleanup path. + * + * If a sent callback is provided to send(), the caller must handle a + * zero return from the delivery handler by not sending any more packets + * until we have invoked the callback. Only in that case will we queue + * the packet. + * + * If a sent callback isn't provided, we just drop the packet to avoid + * unbounded queueing. + */ + +struct CryptoPacket { + QTAILQ_ENTRY(CryptoPacket) entry; + CryptoClientState *sender; + unsigned flags; /* algorithms' type etc. */ + CryptoPacketSent *sent_cb; /* callback after packet sent */ + void *opaque; /* header struct pointer of operation */ + uint8_t data[0]; +}; + +struct CryptoQueue { + void *opaque; + uint32_t nq_maxlen; + uint32_t nq_count; + CryptoQueueDeliverFunc *deliver; + + QTAILQ_HEAD(packets, CryptoPacket) packets; + + unsigned delivering:1; +}; + +CryptoQueue * +qemu_new_crypto_queue(CryptoQueueDeliverFunc *deliver, void *opaque) +{ + CryptoQueue *queue; + + queue = g_new0(CryptoQueue, 1); + + queue->opaque = opaque; + queue->nq_maxlen = 10000; + queue->nq_count = 0; + queue->deliver = deliver; + + QTAILQ_INIT(&queue->packets); + + queue->delivering = 0; + + return queue; +} + +void qemu_del_crypto_queue(CryptoQueue *queue) +{ + CryptoPacket *packet, *next; + + QTAILQ_FOREACH_SAFE(packet, &queue->packets, entry, next) { + QTAILQ_REMOVE(&queue->packets, packet, entry); + g_free(packet->opaque); + g_free(packet); + } + + g_free(queue); +} + +void qemu_crypto_queue_cache(CryptoQueue *queue, + unsigned flags, + CryptoClientState *sender, + void *opaque, + CryptoPacketSent *sent_cb) +{ + CryptoPacket *packet; + + if (queue->nq_count >= queue->nq_maxlen && !sent_cb) { + return; /* drop if queue full and no callback */ + } + + packet = g_malloc(sizeof(CryptoPacket)); + packet->sender = sender; + packet->sent_cb = sent_cb; + packet->flags = flags, + packet->opaque = opaque; + + queue->nq_count++; + QTAILQ_INSERT_TAIL(&queue->packets, packet, entry); +} + +static ssize_t qemu_crypto_queue_deliver(CryptoQueue *queue, + unsigned flags, + CryptoClientState *sender, + void *opaque) +{ + ssize_t ret = -1; + + queue->delivering = 1; + ret = queue->deliver(sender, flags, opaque, queue->opaque); + queue->delivering = 0; + + return ret; +} + +int qemu_crypto_queue_send(CryptoQueue *queue, + unsigned flags, + CryptoClientState *sender, + void *opaque, + CryptoPacketSent *sent_cb) +{ + int ret; + + if (queue->delivering) { + qemu_crypto_queue_cache(queue, flags, sender, + opaque, sent_cb); + return 0; + } + + ret = qemu_crypto_queue_deliver(queue, flags, sender, opaque); + if (ret == 0) { + qemu_crypto_queue_cache(queue, flags, sender, + opaque, sent_cb); + return 0; + } + + qemu_crypto_queue_flush(queue); + + return ret; +} + +void qemu_crypto_queue_purge(CryptoQueue *queue, CryptoClientState *from) +{ + CryptoPacket *packet, *next; + + QTAILQ_FOREACH_SAFE(packet, &queue->packets, entry, next) { + if (packet->sender == from) { + QTAILQ_REMOVE(&queue->packets, packet, entry); + queue->nq_count--; + if (packet->sent_cb) { + packet->sent_cb(packet->sender, 0); + } + g_free(packet->opaque); + g_free(packet); + } + } +} + +bool qemu_crypto_queue_flush(CryptoQueue *queue) +{ + while (!QTAILQ_EMPTY(&queue->packets)) { + CryptoPacket *packet; + int ret; + + packet = QTAILQ_FIRST(&queue->packets); + QTAILQ_REMOVE(&queue->packets, packet, entry); + queue->nq_count--; + + ret = qemu_crypto_queue_deliver(queue, packet->flags, + packet->sender, packet->opaque); + if (ret == 0) { + queue->nq_count++; + QTAILQ_INSERT_HEAD(&queue->packets, packet, entry); + return false; + } + + if (packet->sent_cb) { + packet->sent_cb(packet->sender, ret); + } + + g_free(packet->opaque); + g_free(packet); + } + return true; +} diff --git a/crypto/crypto.c b/crypto/crypto.c index fbc6497..a0e4a34 100644 --- a/crypto/crypto.c +++ b/crypto/crypto.c @@ -151,6 +151,8 @@ static void crypto_client_setup(CryptoClientState *cc, } QTAILQ_INSERT_TAIL(&crypto_clients, cc, next); cc->destructor = destructor; + cc->incoming_queue = + qemu_new_crypto_queue(qemu_deliver_crypto_packet, cc); } CryptoClientState *new_crypto_client(CryptoClientInfo *info, @@ -169,3 +171,29 @@ CryptoClientState *new_crypto_client(CryptoClientInfo *info, return cc; } + +int qemu_deliver_crypto_packet(CryptoClientState *sender, + unsigned flags, + void *header_opqaue, + void *opaque) +{ + return 0; +} + +int qemu_send_crypto_packet_async(CryptoClientState *sender, + unsigned flags, + void *opaque, + CryptoPacketSent *sent_cb) +{ + CryptoQueue *queue; + + if (!sender->ready) { + /* we assume that all packets are sent */ + return 1; + } + + queue = sender->peer->incoming_queue; + + return qemu_crypto_queue_send(queue, flags, sender, + opaque, sent_cb); +} diff --git a/include/crypto/crypto-queue.h b/include/crypto/crypto-queue.h new file mode 100644 index 0000000..6fba64d --- /dev/null +++ b/include/crypto/crypto-queue.h @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2003-2008 Fabrice Bellard + * Copyright (c) 2016 HUAWEI TECHNOLOGIES CO., LTD. + * + * Authors: + * Gonglei + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#ifndef QEMU_CRYPTO_QUEUE_H +#define QEMU_CRYPTO_QUEUE_H + +#include "qemu-common.h" + +typedef struct CryptoPacket CryptoPacket; +typedef struct CryptoQueue CryptoQueue; +typedef struct CryptoPacketBuf CryptoPacketBuf; + +typedef void (CryptoPacketSent) (CryptoClientState *, int); + + +/* Returns: + * >0 - success + * 0 - queue packet for future redelivery + * <0 - failure (discard packet) + */ +typedef int (CryptoQueueDeliverFunc)(CryptoClientState *sender, + unsigned flags, + void *header_opaque, + void *opaque); + +CryptoQueue * +qemu_new_crypto_queue(CryptoQueueDeliverFunc *deliver, void *opaque); + +void qemu_crypto_queue_cache(CryptoQueue *queue, + unsigned flags, + CryptoClientState *sender, + void *opaque, + CryptoPacketSent *sent_cb); + +void qemu_del_crypto_queue(CryptoQueue *queue); + +int qemu_crypto_queue_send(CryptoQueue *queue, + unsigned flags, + CryptoClientState *sender, + void *opaque, + CryptoPacketSent *sent_cb); + +void qemu_crypto_queue_purge(CryptoQueue *queue, CryptoClientState *from); +bool qemu_crypto_queue_flush(CryptoQueue *queue); + +#endif /* QEMU_CRYPTO_QUEUE_H */ diff --git a/include/crypto/crypto.h b/include/crypto/crypto.h index f93f6f9..46b3b9e 100644 --- a/include/crypto/crypto.h +++ b/include/crypto/crypto.h @@ -29,6 +29,8 @@ #include "qemu/queue.h" #include "qapi-types.h" +#include "crypto/crypto-queue.h" + typedef void (CryptoPoll)(CryptoClientState *, bool); typedef void (CryptoCleanup) (CryptoClientState *); @@ -52,6 +54,8 @@ struct CryptoClientState { char *model; char *name; char info_str[256]; + CryptoQueue *incoming_queue; + unsigned int queue_index; CryptoClientDestructor *destructor; }; @@ -62,5 +66,13 @@ CryptoClientState *new_crypto_client(CryptoClientInfo *info, CryptoClientState *peer, const char *model, const char *name); +int qemu_deliver_crypto_packet(CryptoClientState *sender, + unsigned flags, + void *header_opqaue, + void *opaque); +int qemu_send_crypto_packet_async(CryptoClientState *sender, + unsigned flags, + void *opaque, + CryptoPacketSent *sent_cb); #endif /* QCRYPTO_CRYPTO_H__ */