From patchwork Tue Oct 3 13:14:02 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Akhil Goyal X-Patchwork-Id: 29536 Return-Path: X-Original-To: patchwork@dpdk.org Delivered-To: patchwork@dpdk.org Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id E342A1B389; Tue, 3 Oct 2017 15:16:58 +0200 (CEST) Received: from NAM01-BN3-obe.outbound.protection.outlook.com (mail-bn3nam01on0060.outbound.protection.outlook.com [104.47.33.60]) by dpdk.org (Postfix) with ESMTP id B5B2B1B37E for ; Tue, 3 Oct 2017 15:16:56 +0200 (CEST) Received: from CY4PR03CA0017.namprd03.prod.outlook.com (10.168.162.27) by CY4PR03MB2695.namprd03.prod.outlook.com (10.173.43.138) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P256) id 15.20.77.7; Tue, 3 Oct 2017 13:16:54 +0000 Received: from BL2FFO11FD006.protection.gbl (2a01:111:f400:7c09::101) by CY4PR03CA0017.outlook.office365.com (2603:10b6:903:33::27) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384_P256) id 15.20.56.11 via Frontend Transport; Tue, 3 Oct 2017 13:16:54 +0000 Received-SPF: Fail (protection.outlook.com: domain of nxp.com does not designate 192.88.158.2 as permitted sender) receiver=protection.outlook.com; client-ip=192.88.158.2; helo=az84smr01.freescale.net; Received: from az84smr01.freescale.net (192.88.158.2) by BL2FFO11FD006.mail.protection.outlook.com (10.173.161.2) with Microsoft SMTP Server (version=TLS1_0, cipher=TLS_RSA_WITH_AES_256_CBC_SHA) id 15.20.77.10 via Frontend Transport; Tue, 3 Oct 2017 13:16:54 +0000 Received: from netperf2.ap.freescale.net ([10.232.133.164]) by az84smr01.freescale.net (8.14.3/8.14.0) with ESMTP id v93DGfTC030592; Tue, 3 Oct 2017 06:16:49 -0700 From: Akhil Goyal To: CC: , , , , , , , , , , Date: Tue, 3 Oct 2017 18:44:02 +0530 Message-ID: <20171003131413.23846-2-akhil.goyal@nxp.com> X-Mailer: git-send-email 2.9.3 In-Reply-To: <20171003131413.23846-1-akhil.goyal@nxp.com> References: <20170914082651.26232-1-akhil.goyal@nxp.com> <20171003131413.23846-1-akhil.goyal@nxp.com> X-EOPAttributedMessage: 0 X-Matching-Connectors: 131515102143959819; (91ab9b29-cfa4-454e-5278-08d120cd25b8); () X-Forefront-Antispam-Report: CIP:192.88.158.2; IPV:NLI; CTRY:US; EFV:NLI; SFV:NSPM; SFS:(10009020)(6009001)(336005)(39860400002)(39380400002)(376002)(346002)(2980300002)(1110001)(1109001)(339900001)(199003)(189002)(50986999)(50226002)(106466001)(76176999)(81156014)(8656003)(81166006)(104016004)(97736004)(105606002)(8676002)(2351001)(53946003)(316002)(15650500001)(16586007)(2906002)(5890100001)(356003)(68736007)(36756003)(189998001)(8936002)(33646002)(53936002)(305945005)(54906003)(6916009)(5003940100001)(2950100002)(47776003)(498600001)(48376002)(4326008)(6666003)(77096006)(5660300001)(50466002)(7416002)(1076002)(69596002)(86362001); DIR:OUT; SFP:1101; SCL:1; SRVR:CY4PR03MB2695; H:az84smr01.freescale.net; FPR:; SPF:Fail; PTR:InfoDomainNonexistent; A:1; MX:1; LANG:en; X-Microsoft-Exchange-Diagnostics: 1; BL2FFO11FD006; 1:4a6k+56Dz/BFPzjbcWeKocLeifCNiSgIwjM1KaVCeCWu5PQfEMfBESXtkXTiquptUB96LGXruaUZ1vMQgxKh3sVUS7RfsoWg/kEgW/mwyXItbzWYuLruYEUdPXYu7Kmj MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: c01d54c8-1868-4c81-66a2-08d50a610445 X-Microsoft-Antispam: UriScan:; BCL:0; PCL:0; RULEID:(22001)(2017052603199)(201703131430075)(201703131517081); SRVR:CY4PR03MB2695; X-Microsoft-Exchange-Diagnostics: 1; CY4PR03MB2695; 3:4l7lU9Jnu9oQXvfoTWEohCh9C6U5AXKKuhnk/Eso+5I2kUINzZFdf7sy+fl8TgLA+PZwyAlSzjPlICoJIJk4RPlKdTM9zU1CofwcenSiT/MqAKkRRM/dZ2MKVZGN2Xb/fnipymL5Cm8FosW/Qpvbud09sHuuLuh7eH8kRH4na9/ts6bkRFoLzCwIBs7AyePO6wEudaIttrH+eatJBxwBG7vomqFNfA7BPTuV8MIODGzRgbar+w1trSaXMQfadsG3s4F5eLtpXDvrvZSrwm2DbxHwEP/eqxHY5F2V6mqE3M3z2yg/eug2dapo0VEn03qdxh7AHZmka175VfIxuvyTqg==; 25:RGXSRsOe0rcxLO1qINXfHRusmRzCN+BiTX/g3fQd0UyTDS0crUYhhKaWLAFb7WZ8gjQOAXRjOBM3pjcIk4T42+N4PuYhWWVRVSk2Slq5cnSoprqBbVAnS2nU+D+hCw6vDDKI3yjbtOy0ltfggCKhzOuMo6teADE9VaRS8/J13hj7/N58ZdbZKUXU+sZBeujZexSPXTbp2vyWF2zfI7lLLdWBsx7RQdq5WTnNKMviBFf7P4ascU5ysc+DV98+zTL0Xy22wvMYpV+Ni6npIWXUWPLbT0HFKNppfKdYUNEKApsuD2xeTF+GW7JuqIHNUwxpGh1RH5PUOz3Arw6YNgYMEg==; 31:0CKjH6zqvzL2glUUCRiLYKrlXvrJmkhPJeIMoeTctFc1eZ2FAwbtmIGsvJdz2NZG3BDXjs7IFcd/z2Bi0KvV5PmwYVx1/FGPcXbKCE/Diz4jS4Jml2IzOWcaIiWuAOUbELPMNxpFmHTMg82VFQ+/hfdkYCFOoLx6VNUFJJS2xOP5VlzxNIom3kdnWmfGQaNWU+j5JjnY22VdEEJUdPVjTDy60RJ96sELEZ48yDv8rbE= X-MS-TrafficTypeDiagnostic: CY4PR03MB2695: X-Exchange-Antispam-Report-Test: UriScan:(192374486261705)(185117386973197)(228905959029699); X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-CFA-Test: BCL:0; PCL:0; RULEID:(100000700101)(100105000095)(100000701101)(100105300095)(100000702101)(100105100095)(6095135)(2401047)(8121501046)(5005006)(93006095)(93001095)(100000703101)(100105400095)(3002001)(10201501046)(6055026)(6096035)(20161123561025)(20161123565025)(20161123563025)(20161123556025)(201703131430075)(201703131433075)(201703131448075)(201703161259150)(201703151042153)(20161123559100)(201708071742011)(100000704101)(100105200095)(100000705101)(100105500095); SRVR:CY4PR03MB2695; BCL:0; PCL:0; RULEID:(100000800101)(100110000095)(100000801101)(100110300095)(100000802101)(100110100095)(100000803101)(100110400095)(400006)(100000804101)(100110200095)(100000805101)(100110500095); SRVR:CY4PR03MB2695; X-Microsoft-Exchange-Diagnostics: 1; CY4PR03MB2695; 4:azuT4xiE7N/vPmkBRKdY4jzeIkpEm2R6O6mn5zfqzVVg7SBH3NppOMkHY8Fs7DlLl/AHC8GCIXHOIxc6ThbZIyHwqcZNlF48TNT7niZyZs/WYP/E8pKCNSskSAPzt91kVTo0giERAWOIbo8oE6kiboraHZdbfjwjfPU2d+wXb7rejMXKd6pPO2aOJXqQUa/G2lg6Br1MgLd/MGxYy7uiaQ+QzdUf8C4Ey2Pd5W+tTKr0NNX6CdBeu3IaAKjdMaU25YVfmRPAXQQeGFlvgbvZ7xFc0sZVrnqLgzC2Y+RfeLRiCZUbUoAwy9wzT0LZ40PkT+mojOqjVlSt4V9JdOikq22t1HeKaucjXR9r7hEDdC4q+pTF2efo7iXnI6VxGti/ X-Forefront-PRVS: 044968D9E1 X-Microsoft-Exchange-Diagnostics: =?us-ascii?Q?1; CY4PR03MB2695; 23:BpyHEKii1c8B1d7ZAIg95az08n1RmLAANzWIS/VRK?= /VZKTaHkXIugNoC6BxweG7zZLT0IGT2fyyIzyt4uKjA22lQQ5/XiyfO8v59djqPtUB5QWT9j5aBRSNr50h6gj8rOM5Jhx1E48/R8S1yepga9VsgJ7TbX2anwLYbOzN/PeqkS8plUGHdVTd4asdxxDnre983AZvH8SqKOuwuhKrStG34gVxTGZRlxPaRluIkCprwrd+3qja1WEA2zmmBzm4CqQ2qd9li6X1IuAh7Q1NKFYTsK36EA1+3p3s7Tf6+UCo3tjYIYoz7iZ5YhJdQT7mP9h6wwVAPNThBAhmG5PLnjHQ05HKGgWBAH3xR5P0gQpOrCEOhZgQAi4ulIxFgPcdTSu86Ftf3xFxBmzIO8e59rwOmzxMMcxDKrkiS/qEWGFmy4j7pbPixy3CoLrfcRwr3XYs/8fANoQgHw8DEKFw0c6bFI6Avi9m5N3tvKPrskDFuUwK81091UuWu7aDwmvKQIs5zc9majGYjeTP8ngTW3FFOLW7Km/Fp4C5IF9yXuls5aPoZEigN6nYTgk6u6KzfW2thDoRcE2fM4hnFmCUVxF+NxCOnH3P1hSDK06wVRvZxqeZ5cUp8an2vOn/4YVPA7Wuo+4c6A5+30hHbzMrLmTIFl7Le79vIGCQDrnSEvb6RR0YUW1wrr1eqijhtFkCzpfJSWc6pzGmLdPH2M4ZT9CDqYioPgMd2vYvhOs1XfCglJm6TsLtzXq1aMiwcHa2MzMlxudl8qI3CTBppvjOgES6RgJlafbqZqvz1ZLwhveYFuJZyGm42MKmKaWw3vqLDhYHMNvkQi2h7y+yo83h63qijQ93qu5hXYvUm7GEnBflLpIY+ZxksnNwAIZAkEvATePuQKNKl2TPvy2Khsdm2q6wTCKTr/6VsbB/ioNSlj/s0l1lMom1ZQBGC0FVqIXdRKiqtsYC5pPZjDKJkHDroB2emJORTHFKTD3a12CSV8QmzFfLXD370nR67ou/7QWfYbSplzvxIVoPyv+o9WI/jEjsnleOzBjy9+CAYq3hnKd9pgtxSWCI5WznCc3j+mXZ+Tf4EQ+v4g4L1acaluFVvRHT2tIRIP5gVSjARI+1KwjxDAD5UmRYGYEKsnkrYaffquU7fWgX0jQW+TsVo8d2cPkkWHS8F3Iw13Yj5v3lbR3EDbH2Bvhgp5xAw9DNFZ69Fhvhy7UGeXGbJ5UqITOEPgPys1DumUe08pSFqd5CWszp9vTb24vxEZJIpJDKwdRtA X-Microsoft-Exchange-Diagnostics: 1; CY4PR03MB2695; 6:cOkNtWp6JDZnFbBkpGRgk1AWnXlkgPfR51m5MJOwOvWQxQXxjwK9bC1hBa6ak5McARTy8uEYfPw+/M3mz37dk0tlGVXdxWPxTLt+gprkU4gcjZTIjKdaFLdwCIIs7Rw2Rf8ZvHjUXTkDBHpFseC+HMPOjDlEuPe/y7KQswY/IdvVdbQBYAqf2JNPhVHThPDdDLUMO3XQPxajWmAGPgLQ90CFr7iQ/P4ynEm6gbzj5Tl8PX+zyGSfG/cbccWsDQIl368daUVHh/FNYzbrjfoyGBY1MDQYOUP8rqOV+Ak0etoKXC9xMqs0477CBw8cXWOdylrZDRErP8TdpCVEPVv5Cg==; 5:QMRfCqUqK9q2d4YH1gXPQSjUD793jM29ipjgPaxvs3HCHT52hYWq4Ps/4wcsR2HnD3XeRT8hDrArqyWiXJAsymg0v+SimveUSk8T6Kip0SRHCa8X5CQgiJ93jLnWg643rFrzkEipRSnWisfL5DQXNg==; 24:6yiizoRR3PQh7zy2sS35NAtwmqfCr0B9v9E1Q8FJEzeQdlxUBf1YVAA9jVsuP6afjsEtEf4OuZPqbgHkRATesODHOpDftxNtfibbyNxaS4g=; 7:Bb0ypoam8IbkBK+j8KmlYhOsS3MQFuf6Hh98siu4k+HaS44LJOBSR0hILfrWHgukkHuA6rp4VUMa4eZM8roLSVpMnA38pJwjT7Acb2tsKn4bPK0m5aenLymLrtYK+iPACuVbpSdJF+UPyEXPX5cGCHu83PmvA22uYnq1laufyq5fMN0WxSIZyBxtZQ0uhIAotKTUeGMp1jnBpR46QKzkUjm57qyUljEAKOHCOAScdp0= SpamDiagnosticOutput: 1:99 SpamDiagnosticMetadata: NSPM X-MS-Exchange-CrossTenant-OriginalArrivalTime: 03 Oct 2017 13:16:54.0371 (UTC) X-MS-Exchange-CrossTenant-Id: 5afe0b00-7697-4969-b663-5eab37d5f47e X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=5afe0b00-7697-4969-b663-5eab37d5f47e; Ip=[192.88.158.2]; Helo=[az84smr01.freescale.net] X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: CY4PR03MB2695 Subject: [dpdk-dev] [PATCH v2 01/12] lib/rte_security: add security library 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" rte_security library provides APIs for security session create/free for protocol offload or offloaded crypto operation to ethernet device. Signed-off-by: Akhil Goyal Signed-off-by: Boris Pismenny Signed-off-by: Radu Nicolau Signed-off-by: Declan Doherty --- lib/librte_security/Makefile | 53 +++ lib/librte_security/rte_security.c | 275 +++++++++++++++ lib/librte_security/rte_security.h | 495 +++++++++++++++++++++++++++ lib/librte_security/rte_security_driver.h | 182 ++++++++++ lib/librte_security/rte_security_version.map | 13 + 5 files changed, 1018 insertions(+) create mode 100644 lib/librte_security/Makefile create mode 100644 lib/librte_security/rte_security.c create mode 100644 lib/librte_security/rte_security.h create mode 100644 lib/librte_security/rte_security_driver.h create mode 100644 lib/librte_security/rte_security_version.map diff --git a/lib/librte_security/Makefile b/lib/librte_security/Makefile new file mode 100644 index 0000000..af87bb2 --- /dev/null +++ b/lib/librte_security/Makefile @@ -0,0 +1,53 @@ +# BSD LICENSE +# +# Copyright(c) 2017 Intel Corporation. All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in +# the documentation and/or other materials provided with the +# distribution. +# * Neither the name of Intel Corporation nor the names of its +# contributors may be used to endorse or promote products derived +# from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +include $(RTE_SDK)/mk/rte.vars.mk + +# library name +LIB = librte_security.a + +# library version +LIBABIVER := 1 + +# build flags +CFLAGS += -O3 +CFLAGS += $(WERROR_FLAGS) + +# library source files +SRCS-y += rte_security.c + +# export include files +SYMLINK-y-include += rte_security.h +SYMLINK-y-include += rte_security_driver.h + +# versioning export map +EXPORT_MAP := rte_security_version.map + +include $(RTE_SDK)/mk/rte.lib.mk diff --git a/lib/librte_security/rte_security.c b/lib/librte_security/rte_security.c new file mode 100644 index 0000000..97d3857 --- /dev/null +++ b/lib/librte_security/rte_security.c @@ -0,0 +1,275 @@ +/*- + * BSD LICENSE + * + * Copyright 2017 NXP. + * Copyright(c) 2017 Intel Corporation. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * * Neither the name of NXP nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include + +#include "rte_security.h" +#include "rte_security_driver.h" + +#define RTE_SECURITY_INSTANCES_BLOCK_ALLOC_SZ (8) + +struct rte_security_ctx { + uint16_t id; + enum { + RTE_SECURITY_INSTANCE_INVALID, + RTE_SECURITY_INSTANCE_VALID + } state; + void *device; + struct rte_security_ops *ops; + uint16_t sess_cnt; +}; + +static struct rte_security_ctx *security_instances; +static uint16_t max_nb_security_instances; +static uint16_t nb_security_instances; + +static int +rte_security_is_valid_id(uint16_t id) +{ + if (id >= nb_security_instances || + (security_instances[id].state != RTE_SECURITY_INSTANCE_VALID)) + return 0; + else + return 1; +} + +/* Macros to check for valid id */ +#define RTE_SEC_VALID_ID_OR_ERR_RET(id, retval) do { \ + if (!rte_security_is_valid_id(id)) { \ + RTE_PMD_DEBUG_TRACE("Invalid sec_id=%d\n", id); \ + return retval; \ + } \ +} while (0) + +#define RTE_SEC_VALID_ID_OR_RET(id) do { \ + if (!rte_security_is_valid_id(id)) { \ + RTE_PMD_DEBUG_TRACE("Invalid sec_id=%d\n", id); \ + return; \ + } \ +} while (0) + +int +rte_security_register(uint16_t *id, void *device, + struct rte_security_ops *ops) +{ + if (max_nb_security_instances == 0) { + security_instances = rte_malloc( + "rte_security_instances_ops", + sizeof(*security_instances) * + RTE_SECURITY_INSTANCES_BLOCK_ALLOC_SZ, 0); + + if (security_instances == NULL) + return -ENOMEM; + max_nb_security_instances = + RTE_SECURITY_INSTANCES_BLOCK_ALLOC_SZ; + } else if (nb_security_instances >= max_nb_security_instances) { + uint16_t *instances = rte_realloc(security_instances, + sizeof(struct rte_security_ops *) * + (max_nb_security_instances + + RTE_SECURITY_INSTANCES_BLOCK_ALLOC_SZ), 0); + + if (instances == NULL) + return -ENOMEM; + + max_nb_security_instances += + RTE_SECURITY_INSTANCES_BLOCK_ALLOC_SZ; + } + + *id = nb_security_instances++; + + security_instances[*id].id = *id; + security_instances[*id].state = RTE_SECURITY_INSTANCE_VALID; + security_instances[*id].device = device; + security_instances[*id].ops = ops; + security_instances[*id].sess_cnt = 0; + + return 0; +} + +int +rte_security_unregister(uint16_t id) +{ + struct rte_security_ctx *instance; + + RTE_SEC_VALID_ID_OR_ERR_RET(id, -ENODEV); + instance = &security_instances[id]; + + if (instance->sess_cnt) + return -EBUSY; + + memset(instance, 0, sizeof(*instance)); + return 0; +} + +struct rte_security_session * +rte_security_session_create(uint16_t id, + struct rte_security_session_conf *conf, + struct rte_mempool *mp) +{ + struct rte_security_ctx *instance; + struct rte_security_session *sess = NULL; + + RTE_SEC_VALID_ID_OR_ERR_RET(id, NULL); + instance = &security_instances[id]; + + if (conf == NULL) + return NULL; + + RTE_FUNC_PTR_OR_ERR_RET(*instance->ops->session_create, NULL); + + if (rte_mempool_get(mp, (void *)&sess)) + return NULL; + + if (instance->ops->session_create(instance->device, conf, sess, mp)) { + rte_mempool_put(mp, (void *)sess); + return NULL; + } + instance->sess_cnt++; + + return sess; +} + +int +rte_security_session_update(uint16_t id, + struct rte_security_session *sess, + struct rte_security_session_conf *conf) +{ + struct rte_security_ctx *instance; + + RTE_SEC_VALID_ID_OR_ERR_RET(id, -ENODEV); + instance = &security_instances[id]; + + RTE_FUNC_PTR_OR_ERR_RET(*instance->ops->session_update, -ENOTSUP); + return instance->ops->session_update(instance->device, sess, conf); +} + +int +rte_security_session_stats_get(uint16_t id, + struct rte_security_session *sess, + struct rte_security_stats *stats) +{ + struct rte_security_ctx *instance; + + RTE_SEC_VALID_ID_OR_ERR_RET(id, -ENODEV); + instance = &security_instances[id]; + + RTE_FUNC_PTR_OR_ERR_RET(*instance->ops->session_stats_get, -ENOTSUP); + return instance->ops->session_stats_get(instance->device, sess, stats); +} + +int +rte_security_session_destroy(uint16_t id, struct rte_security_session *sess) +{ + int ret; + struct rte_security_ctx *instance; + struct rte_mempool *mp = rte_mempool_from_obj(sess); + + RTE_SEC_VALID_ID_OR_ERR_RET(id, -ENODEV); + instance = &security_instances[id]; + + RTE_FUNC_PTR_OR_ERR_RET(*instance->ops->session_destroy, -ENOTSUP); + + if (instance->sess_cnt) + instance->sess_cnt--; + + ret = instance->ops->session_destroy(instance->device, sess); + if (!ret) + rte_mempool_put(mp, (void *)sess); + + return ret; +} + +int +rte_security_set_pkt_metadata(uint16_t id, + struct rte_security_session *sess, + struct rte_mbuf *m, void *params) +{ + struct rte_security_ctx *instance; + + RTE_SEC_VALID_ID_OR_ERR_RET(id, -ENODEV); + instance = &security_instances[id]; + + RTE_FUNC_PTR_OR_ERR_RET(*instance->ops->set_pkt_metadata, -ENOTSUP); + return instance->ops->set_pkt_metadata(instance->device, + sess, m, params); +} + +const struct rte_security_capability * +rte_security_capabilities_get(uint16_t id) +{ + struct rte_security_ctx *instance; + + RTE_SEC_VALID_ID_OR_ERR_RET(id, NULL); + instance = &security_instances[id]; + + RTE_FUNC_PTR_OR_ERR_RET(*instance->ops->capabilities_get, NULL); + return instance->ops->capabilities_get(instance->device); +} + +const struct rte_security_capability * +rte_security_capability_get(uint16_t id, + struct rte_security_capability_idx *idx) +{ + struct rte_security_ctx *instance; + const struct rte_security_capability *capabilities; + const struct rte_security_capability *capability; + uint16_t i = 0; + + RTE_SEC_VALID_ID_OR_ERR_RET(id, NULL); + instance = &security_instances[id]; + + RTE_FUNC_PTR_OR_ERR_RET(*instance->ops->capabilities_get, NULL); + capabilities = instance->ops->capabilities_get(instance->device); + + if (capabilities == NULL) + return NULL; + + while ((capability = &capabilities[i++])->action + != RTE_SECURITY_ACTION_TYPE_NONE) { + if (capability->action == idx->action && + capability->protocol == idx->protocol) { + if (idx->protocol == RTE_SECURITY_PROTOCOL_IPSEC) { + if (capability->ipsec.proto == + idx->ipsec.proto && + capability->ipsec.mode == + idx->ipsec.mode && + capability->ipsec.direction == + idx->ipsec.direction) + return capability; + } + } + } + + return NULL; +} diff --git a/lib/librte_security/rte_security.h b/lib/librte_security/rte_security.h new file mode 100644 index 0000000..9e639fd --- /dev/null +++ b/lib/librte_security/rte_security.h @@ -0,0 +1,495 @@ +/*- + * BSD LICENSE + * + * Copyright 2017 NXP. + * Copyright(c) 2017 Intel Corporation. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * * Neither the name of NXP nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef _RTE_SECURITY_H_ +#define _RTE_SECURITY_H_ + +/** + * @file rte_security.h + * + * RTE Security Common Definitions + * + */ + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include +#include + +#include +#include +#include +#include +#include + +/** IPSec protocol mode */ +enum rte_security_ipsec_sa_mode { + RTE_SECURITY_IPSEC_SA_MODE_TRANSPORT, + /**< IPSec Transport mode */ + RTE_SECURITY_IPSEC_SA_MODE_TUNNEL, + /**< IPSec Tunnel mode */ +}; + +/** IPSec Protocol */ +enum rte_security_ipsec_sa_protocol { + RTE_SECURITY_IPSEC_SA_PROTO_AH, + /**< AH protocol */ + RTE_SECURITY_IPSEC_SA_PROTO_ESP, + /**< ESP protocol */ +}; + +/** IPSEC tunnel type */ +enum rte_security_ipsec_tunnel_type { + RTE_SECURITY_IPSEC_TUNNEL_IPV4, + /**< Outer header is IPv4 */ + RTE_SECURITY_IPSEC_TUNNEL_IPV6, + /**< Outer header is IPv6 */ +}; + +/** + * IPSEC tunnel parameters + * + * These parameters are used to build outbound tunnel headers. + */ +struct rte_security_ipsec_tunnel_param { + enum rte_security_ipsec_tunnel_type type; + /**< Tunnel type: IPv4 or IPv6 */ + RTE_STD_C11 + union { + struct { + struct in_addr src_ip; + /**< IPv4 source address */ + struct in_addr dst_ip; + /**< IPv4 destination address */ + uint8_t dscp; + /**< IPv4 Differentiated Services Code Point */ + uint8_t df; + /**< IPv4 Don't Fragment bit */ + uint8_t ttl; + /**< IPv4 Time To Live */ + } ipv4; + /**< IPv4 header parameters */ + struct { + struct in6_addr src_addr; + /**< IPv6 source address */ + struct in6_addr dst_addr; + /**< IPv6 destination address */ + uint8_t dscp; + /**< IPv6 Differentiated Services Code Point */ + uint32_t flabel; + /**< IPv6 flow label */ + uint8_t hlimit; + /**< IPv6 hop limit */ + } ipv6; + /**< IPv6 header parameters */ + }; +}; + +/** + * IPsec Security Association option flags + */ +struct rte_security_ipsec_sa_options { + /**< Extended Sequence Numbers (ESN) + * + * * 1: Use extended (64 bit) sequence numbers + * * 0: Use normal sequence numbers + */ + uint32_t esn : 1; + + /**< UDP encapsulation + * + * * 1: Do UDP encapsulation/decapsulation so that IPSEC packets can + * traverse through NAT boxes. + * * 0: No UDP encapsulation + */ + uint32_t udp_encap : 1; + + /**< Copy DSCP bits + * + * * 1: Copy IPv4 or IPv6 DSCP bits from inner IP header to + * the outer IP header in encapsulation, and vice versa in + * decapsulation. + * * 0: Use values from odp_ipsec_tunnel_param_t in encapsulation and + * do not change DSCP field in decapsulation. + */ + uint32_t copy_dscp : 1; + + /**< Copy IPv6 Flow Label + * + * * 1: Copy IPv6 flow label from inner IPv6 header to the + * outer IPv6 header. + * * 0: Use value from odp_ipsec_tunnel_param_t + */ + uint32_t copy_flabel : 1; + + /**< Copy IPv4 Don't Fragment bit + * + * * 1: Copy the DF bit from the inner IPv4 header to the outer + * IPv4 header. + * * 0: Use value from odp_ipsec_tunnel_param_t + */ + uint32_t copy_df : 1; + + /**< Decrement inner packet Time To Live (TTL) field + * + * * 1: In tunnel mode, decrement inner packet IPv4 TTL or + * IPv6 Hop Limit after tunnel decapsulation, or before tunnel + * encapsulation. + * * 0: Inner packet is not modified. + */ + uint32_t dec_ttl : 1; + + /**< HW constructs/removes trailer of packets + * + * * 1: Transmitted packets will have the trailer added to them by + * hardawre. The next protocol field will be based on the + * mbuf->inner_esp_next_proto field. + * Received packets have no trailer, the next protocol field is + * supplied in the mbuf->inner_esp_next_proto field. + * * 0: Inner packet is not modified. + */ + uint32_t no_trailer : 1; +}; + +/** IPSec security association direction */ +enum rte_security_ipsec_sa_direction { + RTE_SECURITY_IPSEC_SA_DIR_EGRESS, + /**< Encrypt and generate digest */ + RTE_SECURITY_IPSEC_SA_DIR_INGRESS, + /**< Verify digest and decrypt */ +}; + +/** + * IPsec security association configuration data. + * + * This structure contains data required to create an IPsec SA security session. + */ +struct rte_security_ipsec_xform { + uint32_t spi; + /**< SA security parameter index */ + uint32_t salt; + /**< SA salt */ + struct rte_security_ipsec_sa_options options; + /**< various SA options */ + enum rte_security_ipsec_sa_direction direction; + /**< IPSec SA Direction - Egress/Ingress */ + enum rte_security_ipsec_sa_protocol proto; + /**< IPsec SA Protocol - AH/ESP */ + enum rte_security_ipsec_sa_mode mode; + /**< IPsec SA Mode - transport/tunnel */ + struct rte_security_ipsec_tunnel_param tunnel; + /**< Tunnel parameters, NULL for transport mode */ +}; + +/** + * MACsec security session configuration + */ +struct rte_security_macsec_xform { + /** To be Filled */ +}; + +/** + * Security session action type. + */ +enum rte_security_session_action_type { + RTE_SECURITY_ACTION_TYPE_NONE, + /**< No security actions */ + RTE_SECURITY_ACTION_TYPE_INLINE_CRYPTO, + /**< Crypto processing for security protocol is processed inline + * during transmission */ + RTE_SECURITY_ACTION_TYPE_INLINE_PROTOCOL, + /**< All security protocol processing is performed inline during + * transmission */ + RTE_SECURITY_ACTION_TYPE_LOOKASIDE_PROTOCOL + /**< All security protocol processing including crypto is performed + * on a lookaside accelerator */ +}; + +/** Security session protocol definition */ +enum rte_security_session_protocol { + RTE_SECURITY_PROTOCOL_IPSEC, + /**< IPsec Protocol */ + RTE_SECURITY_PROTOCOL_MACSEC, + /**< MACSec Protocol */ +}; + +/** + * Security session configuration + */ +struct rte_security_session_conf { + enum rte_security_session_action_type action_type; + /**< Type of action to be performed on the session */ + enum rte_security_session_protocol protocol; + /**< Security protocol to be configured */ + union { + struct rte_security_ipsec_xform ipsec; + struct rte_security_macsec_xform macsec; + }; + /**< Configuration parameters for security session */ + struct rte_crypto_sym_xform *crypto_xform; + /**< Security Session Crypto Transformations */ +}; + +struct rte_security_session { + void *sess_private_data; + /**< Private session material */ +}; + +/** + * Create security session as specified by the session configuration + * + * @param id security instance identifier id + * @param conf session configuration parameters + * @param mp mempool to allocate session objects from + * @return + * - On success, pointer to session + * - On failure, NULL + */ +struct rte_security_session * +rte_security_session_create(uint16_t id, + struct rte_security_session_conf *conf, + struct rte_mempool *mp); + +/** + * Update security session as specified by the session configuration + * + * @param id security instance identifier id + * @param sess session to update parameters + * @param conf update configuration parameters + * @return + * - On success returns 0 + * - On failure return errno + */ +int +rte_security_session_update(uint16_t id, + struct rte_security_session *sess, + struct rte_security_session_conf *conf); + +/** + * Free security session header and the session private data and + * return it to its original mempool. + * + * @param id security instance identifier id + * @param sess security session to freed + * + * @return + * - 0 if successful. + * - -EINVAL if session is NULL. + * - -EBUSY if not all device private data has been freed. + */ +int +rte_security_session_destroy(uint16_t id, struct rte_security_session *sess); + +/** + * Updates the buffer with device-specific defined metadata + * + * @param id security instance identifier id + * @param sess security session + * @param m packet mbuf to set metadata on. + * @param params device-specific defined parameters required for metadata + * + * @return + * - On success, zero. + * - On failure, a negative value. + */ +int +rte_security_set_pkt_metadata(uint16_t id, + struct rte_security_session *sess, + struct rte_mbuf *mb, void *params); + +/** + * Attach a session to a symmetric crypto operation + * + * @param sym_op crypto operation + * @param sess security session + */ +static inline int +__rte_security_attach_session(struct rte_crypto_sym_op *sym_op, + struct rte_security_session *sess) +{ + sym_op->sec_session = sess; + + return 0; +} + +static inline void * +get_sec_session_private_data(const struct rte_security_session *sess) +{ + return sess->sess_private_data; +} + +static inline void +set_sec_session_private_data(struct rte_security_session *sess, + void *private_data) +{ + sess->sess_private_data = private_data; +} + +/** + * Attach a session to a crypto operation. + * This API is needed only in case of RTE_SECURITY_SESS_CRYPTO_PROTO_OFFLOAD + * For other rte_security_session_action_type, ol_flags in rte_mbuf may be + * defined to perform security operations. + * + * @param op crypto operation + * @param sess security session + */ +static inline int +rte_security_attach_session(struct rte_crypto_op *op, + struct rte_security_session *sess) +{ + if (unlikely(op->type != RTE_CRYPTO_OP_TYPE_SYMMETRIC)) + return -EINVAL; + + op->sess_type = RTE_CRYPTO_OP_SECURITY_SESSION; + + return __rte_security_attach_session(op->sym, sess); +} + +struct rte_security_macsec_stats { + uint64_t reserved; +}; + +struct rte_security_ipsec_stats { + uint64_t reserved; + +}; + +struct rte_security_stats { + enum rte_security_session_protocol protocol; + /**< Security protocol to be configured */ + + union { + struct rte_security_macsec_stats macsec; + struct rte_security_ipsec_stats ipsec; + }; +}; + +/** + * Get security session statistics + * + * @param id security instance identifier id + * @param sess security session + * @param stats statistics + * @return + * - On success return 0 + * - On failure errno + */ +int +rte_security_session_stats_get(uint16_t id, + struct rte_security_session *sess, + struct rte_security_stats *stats); + +/** + * Security capability definition + */ +struct rte_security_capability { + enum rte_security_session_action_type action; + /**< Security action type*/ + enum rte_security_session_protocol protocol; + /**< Security protocol */ + RTE_STD_C11 + union { + struct { + enum rte_security_ipsec_sa_protocol proto; + /**< IPsec SA protocol */ + enum rte_security_ipsec_sa_mode mode; + /**< IPsec SA mode */ + enum rte_security_ipsec_sa_direction direction; + /**< IPsec SA direction */ + struct rte_security_ipsec_sa_options options; + /**< IPsec SA supported options */ + } ipsec; + /**< IPsec capability */ + struct { + /* To be Filled */ + } macsec; + /**< MACsec capability */ + }; + + const struct rte_cryptodev_capabilities *crypto_capabilities; + /**< Corresponding crypto capabilities for security capability */ +}; + +/** + * Security capability index used to query a security instance for a specific + * security capability + */ +struct rte_security_capability_idx { + enum rte_security_session_action_type action; + enum rte_security_session_protocol protocol; + + union { + struct { + enum rte_security_ipsec_sa_protocol proto; + enum rte_security_ipsec_sa_mode mode; + enum rte_security_ipsec_sa_direction direction; + } ipsec; + }; +}; + +/** + * Returns array of security instance capabilities + * + * @param id Security instance identifier. + * + * @return + * - Returns array of security capabilities. + * - Return NULL if no capabilities available. + */ +const struct rte_security_capability * +rte_security_capabilities_get(uint16_t id); + +/** + * Query if a specific capability is available on security instance + * + * @param id security instance identifier. + * @param idx security capability index to match against + * + * @return + * - Returns pointer to security capability on match of capability + * index criteria. + * - Return NULL if the capability not matched on security instance. + */ +const struct rte_security_capability * +rte_security_capability_get(uint16_t id, + struct rte_security_capability_idx *idx); + +#ifdef __cplusplus +} +#endif + +#endif /* _RTE_SECURITY_H_ */ diff --git a/lib/librte_security/rte_security_driver.h b/lib/librte_security/rte_security_driver.h new file mode 100644 index 0000000..5134128 --- /dev/null +++ b/lib/librte_security/rte_security_driver.h @@ -0,0 +1,182 @@ +/*- + * BSD LICENSE + * + * Copyright(c) 2017 Intel Corporation. All rights reserved. + * Copyright 2017 NXP. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * * Neither the name of Intel Corporation nor the names of its + * contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef _RTE_SECURITY_DRIVER_H_ +#define _RTE_SECURITY_DRIVER_H_ + +/** + * @file rte_security_driver.h + * + * RTE Security Common Definitions + * + */ + +#ifdef __cplusplus +extern "C" { +#endif + +#include "rte_security.h" + +/** + * Configure a security session on a device. + * + * @param device Crypto/eth device pointer + * @param conf Security session configuration + * @param sess Pointer to Security private session structure + * @param mp Mempool where the private session is allocated + * + * @return + * - Returns 0 if private session structure have been created successfully. + * - Returns -EINVAL if input parameters are invalid. + * - Returns -ENOTSUP if crypto device does not support the crypto transform. + * - Returns -ENOMEM if the private session could not be allocated. + */ +typedef int (*security_session_create_t)(void *device, + struct rte_security_session_conf *conf, + struct rte_security_session *sess, + struct rte_mempool *mp); + +/** + * Free driver private session data. + * + * @param dev Crypto/eth device pointer + * @param sess Security session structure + */ +typedef int (*security_session_destroy_t)(void *device, + struct rte_security_session *sess); + +/** + * Update driver private session data. + * + * @param device Crypto/eth device pointer + * @param sess Pointer to Security private session structure + * @param conf Security session configuration + * + * @return + * - Returns 0 if private session structure have been updated successfully. + * - Returns -EINVAL if input parameters are invalid. + * - Returns -ENOTSUP if crypto device does not support the crypto transform. + */ +typedef int (*security_session_update_t)(void *device, + struct rte_security_session *sess, + struct rte_security_session_conf *conf); +/** + * Get stats from the PMD. + * + * @param device Crypto/eth device pointer + * @param sess Pointer to Security private session structure + * @param stats Security stats of the driver + * + * @return + * - Returns 0 if private session structure have been updated successfully. + * - Returns -EINVAL if session parameters are invalid. + */ +typedef int (*security_session_stats_get_t)(void *device, + struct rte_security_session *sess, + struct rte_security_stats *stats); + +/** + * Update the mbuf with provided metadata. + * + * @param sess Security session structure + * @param mb Packet buffer + * @param mt Metadata + * + * @return + * - Returns 0 if metadata updated successfully. + * - Returns -ve value for errors. + */ +typedef int (*security_set_pkt_metadata_t)(void *device, + struct rte_security_session *sess, struct rte_mbuf *m, + void *params); + +/** + * Get security capabilities of the device. + * + * @param device crypto/eth device pointer + * + * @return + * - Returns rte_security_capability pointer on success. + * - Returns NULL on error. + */ +typedef const struct rte_security_capability *(*security_capabilities_get_t)( + void *device); + +/** Security operations function pointer table */ +struct rte_security_ops { + security_session_create_t session_create; + /**< Configure a security session. */ + security_session_update_t session_update; + /**< Update a security session. */ + security_session_stats_get_t session_stats_get; + /**< Get security session statistics. */ + security_session_destroy_t session_destroy; + /**< Clear a security sessions private data. */ + security_set_pkt_metadata_t set_pkt_metadata; + /**< Update mbuf metadata. */ + security_capabilities_get_t capabilities_get; + /**< Get security capabilities. */ +}; + +/** + * Register a Crypto/eth device for security operations. + * + * @param id id of the crypto/eth device + * @param device crypto/eth device pointer + * @param ops Security ops to be supported by device + * + * @return + * - Returns 0 if metadata updated successfully. + * - Returns -ve value for errors. + */ +int +rte_security_register(uint16_t *id, void *device, + struct rte_security_ops *ops); + +/** + * Unregister a Crypto/eth device for security operations. + * + * @param id id of the crypto/eth device + * + * @return + * - Returns 0 if device is successfully unregistered. + * - Returns -ve value for errors. + */ +int +rte_security_unregister(uint16_t id); + +#ifdef __cplusplus +} +#endif + +#endif /* _RTE_SECURITY_DRIVER_H_ */ diff --git a/lib/librte_security/rte_security_version.map b/lib/librte_security/rte_security_version.map new file mode 100644 index 0000000..82b7921 --- /dev/null +++ b/lib/librte_security/rte_security_version.map @@ -0,0 +1,13 @@ +DPDK_17.11 { + global: + + rte_security_attach_session; + rte_security_capabilities_get; + rte_security_capability_get; + rte_security_session_create; + rte_security_session_free; + rte_security_session_query; + rte_security_session_update; + rte_security_set_pkt_metadata; + +};