From patchwork Wed Feb 28 15:09:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bing Zhao X-Patchwork-Id: 137435 X-Patchwork-Delegate: rasland@nvidia.com Return-Path: X-Original-To: patchwork@inbox.dpdk.org Delivered-To: patchwork@inbox.dpdk.org Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 5C3C943C2E; Wed, 28 Feb 2024 16:11:09 +0100 (CET) Received: from mails.dpdk.org (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 4344D433EC; Wed, 28 Feb 2024 16:11:09 +0100 (CET) Received: from NAM10-BN7-obe.outbound.protection.outlook.com (mail-bn7nam10on2081.outbound.protection.outlook.com [40.107.92.81]) by mails.dpdk.org (Postfix) with ESMTP id 9016F4339A for ; Wed, 28 Feb 2024 16:11:07 +0100 (CET) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=HiERhGiTMOqoEFzQaxJct34L+1zsNNPMOR2+ZhaWXQzP65CdcvJrdEbVBkrBJK25lZlgjju99zM/XAQNcoQ8JQ14iNV7CLhSI+AsxEnBWsXc/3PFmSLszvbhF54PlgyvEGvd/HjpgrdpRmM6w6frpJEkQhwWWtfk+63azmaQapz0751YqraLXu0tHGuhkZoFgeis+4yW2fmqeTCglu0XjdINHiNe6Tj1RN+AYtQiYF1tOKgRhCfpxGivtJdV086xYqyN5EPxh60fvz8xwcVOvKA0IhIPCxPUhh0llHOgSpwHI9fKipR4zpp8zRLgRS0WuI5OwYCJnNYj6eah64icmQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=BA4wyGZ3iPUJfAO90NjLtM0IXcxCT+kb+Kggp63RAQM=; b=eJqfA6Mcf9f1cvj7nMZDcF536YzStvzPQdjVL3yf6zwNml77bd1AzXU5eIG2D5cfejD+BiEATfnoxwBj09zQTerKh6FDZZ/kY3crZFQUACI/0/Y0XdGeO/jLjUgC+Om/fqD7Z4+k8R6wTyxH6xBHNvRL3jcohsI2WsVHEVuy2HrTjmog1ey1lx0qrMsX4F0pYAUXHIZACrrD8xIjZh8AU3b2t01lxiU5o8nf9kde+LV25T6tTjaAGdJoIpuPIxSM0g9woyM2EU2dmYtSwZTGZprVSa5O2G8VFghRLdrW09wEskq4hlSiwm0X89pQMqWhhDF25Qjns5q/rGQJtEJI+w== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 216.228.117.161) smtp.rcpttodomain=intel.com smtp.mailfrom=nvidia.com; dmarc=pass (p=reject sp=reject pct=100) action=none header.from=nvidia.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=Nvidia.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=BA4wyGZ3iPUJfAO90NjLtM0IXcxCT+kb+Kggp63RAQM=; b=bETDneUQfOH1XLgSJt08HW45Gv2szpWo9SUWBntCdvqkOJ1KBDtpZ8kS/67UiUI5HpB8zsbL42aGjhKTFUmQq/94K7lhVkqEdPtpiSc5HcX2iOOIsw0d9uZh4uKzQXtJlnFF+cx8Xcewl0cYpg03XU160I2vGPc05kUGNDxcezu2rbdc8dZ9Qyq5E2U7jg8zFKRB6OJIlWlpxkU1wQVcjlFa4rPQO+dlT84yTCRWIDXUYggfnKn/5bhWY2ERPXDGESmPPUgkFQn7z4Z7rEIUh9Ax/tS+o6r172detOCbE3kljXj+GUIEZDIFcJFvLe2x8jY1+4Cpeet+YiFBbaWWoA== Received: from MW2PR16CA0057.namprd16.prod.outlook.com (2603:10b6:907:1::34) by PH0PR12MB5401.namprd12.prod.outlook.com (2603:10b6:510:d4::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7316.39; Wed, 28 Feb 2024 15:11:03 +0000 Received: from MWH0EPF000971E2.namprd02.prod.outlook.com (2603:10b6:907:1:cafe::fa) by MW2PR16CA0057.outlook.office365.com (2603:10b6:907:1::34) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7292.50 via Frontend Transport; Wed, 28 Feb 2024 15:11:03 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 216.228.117.161) smtp.mailfrom=nvidia.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=nvidia.com; Received-SPF: Pass (protection.outlook.com: domain of nvidia.com designates 216.228.117.161 as permitted sender) receiver=protection.outlook.com; client-ip=216.228.117.161; helo=mail.nvidia.com; pr=C Received: from mail.nvidia.com (216.228.117.161) by MWH0EPF000971E2.mail.protection.outlook.com (10.167.243.69) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7292.25 via Frontend Transport; Wed, 28 Feb 2024 15:11:03 +0000 Received: from rnnvmail201.nvidia.com (10.129.68.8) by mail.nvidia.com (10.129.200.67) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.986.41; Wed, 28 Feb 2024 07:10:40 -0800 Received: from nvidia.com (10.126.230.35) by rnnvmail201.nvidia.com (10.129.68.8) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1258.12; Wed, 28 Feb 2024 07:10:36 -0800 From: Bing Zhao To: , , , , , , , , , CC: , , Erez Shitrit Subject: [PATCH v4 1/5] net/mlx5/hws: support NAT64 action Date: Wed, 28 Feb 2024 17:09:09 +0200 Message-ID: <20240228150913.32603-2-bingz@nvidia.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20240228150913.32603-1-bingz@nvidia.com> References: <20231227090731.2569427-1-bingz@nvidia.com> <20240228150913.32603-1-bingz@nvidia.com> MIME-Version: 1.0 X-Originating-IP: [10.126.230.35] X-ClientProxiedBy: rnnvmail201.nvidia.com (10.129.68.8) To rnnvmail201.nvidia.com (10.129.68.8) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: MWH0EPF000971E2:EE_|PH0PR12MB5401:EE_ X-MS-Office365-Filtering-Correlation-Id: 22d7a380-05b9-4d57-e81c-08dc386f7aca X-LD-Processed: 43083d15-7273-40c1-b7db-39efd9ccc17a,ExtAddr X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: cueNayvJ/Z9q7VSUrd/cF3lzuyOrLfjgadcudqGwPvq2okthGmPljqn9u/06HA+8e+1zSLRPXoRYJby1j27tYiNyzSxpaMqVW+JHHbVDAw+Jjc7n0Vco5BV4v5jVYixN10ITjAzkaH59s/xQtghPoU5hmI1nof+qDGPMuQG/HJJE1AwW9jFa/KVO9S8hacfYeP2PYdU8geLCXs9DsYPSLri0AsfAWZ9/qWOCiL0hhokDCm5qKWrOAhvEFJKwGlng3Vn68G4CZHpqiDvsvchEInWWr04yRePlZkmHl5MN/w5/3Ep4AmGhnRydYkrTCy8Lqoe6MXuNb0M4fgZDD1sdF3/pXlNPFo1dxc5JSk/pkckugbcwPYKNiBXxHIBnv0JBUFeTHzWn4v49cxsShDt7tnJmKj7jAYtA4SqDI5Tu5OA1+4KS8CS1V7YYBfNznfSWUhJ5WvKc8FvxxQ1kutqNyoM8TyuR2y9EmAoIGvHCpiH8ewDr2hC+wZ+BQrJtYrPVV3g/P7yqjbckeUF8RWeyDGtLaCk94l75vjkuefxbimFEJgh71wfHenrq8Dl+HVToo/VFtXOZSt5jmaEkySbHRFZ/wY+atb+aH5GziraDTuEPYM1mSX+LhxXznRFZeiFrKi2+4Fksyjq8gwEzT6hsKzx19FDdUYRPa/EqHyNsXb0cXVHy2kpI0YoZTvWxqdkSZi1boRUmGapNYtOYYoGvQ3dKEg2y2tLquf0k/nBHaoxw61wj3f2Jn5iZL0fgkaUygV3NnhdMxclgeNscOHTdhQ== X-Forefront-Antispam-Report: CIP:216.228.117.161; CTRY:US; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:mail.nvidia.com; PTR:dc6edge2.nvidia.com; CAT:NONE; SFS:(13230031)(82310400014)(36860700004)(921011); DIR:OUT; SFP:1101; X-OriginatorOrg: Nvidia.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 28 Feb 2024 15:11:03.0692 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 22d7a380-05b9-4d57-e81c-08dc386f7aca X-MS-Exchange-CrossTenant-Id: 43083d15-7273-40c1-b7db-39efd9ccc17a X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=43083d15-7273-40c1-b7db-39efd9ccc17a; Ip=[216.228.117.161]; Helo=[mail.nvidia.com] X-MS-Exchange-CrossTenant-AuthSource: MWH0EPF000971E2.namprd02.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: PH0PR12MB5401 X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org From: Erez Shitrit Add support of new action mlx5dr_action_create_nat64. The new action allows to modify IP packets from version to version, IPV6 to IPV4 and vice versa. Signed-off-by: Erez Shitrit Signed-off-by: Bing Zhao --- drivers/net/mlx5/hws/mlx5dr.h | 29 ++ drivers/net/mlx5/hws/mlx5dr_action.c | 436 ++++++++++++++++++++++++++- drivers/net/mlx5/hws/mlx5dr_action.h | 35 +++ drivers/net/mlx5/hws/mlx5dr_debug.c | 1 + 4 files changed, 500 insertions(+), 1 deletion(-) diff --git a/drivers/net/mlx5/hws/mlx5dr.h b/drivers/net/mlx5/hws/mlx5dr.h index d612f300c6..8441ae97e9 100644 --- a/drivers/net/mlx5/hws/mlx5dr.h +++ b/drivers/net/mlx5/hws/mlx5dr.h @@ -51,6 +51,7 @@ enum mlx5dr_action_type { MLX5DR_ACTION_TYP_DEST_ARRAY, MLX5DR_ACTION_TYP_POP_IPV6_ROUTE_EXT, MLX5DR_ACTION_TYP_PUSH_IPV6_ROUTE_EXT, + MLX5DR_ACTION_TYP_NAT64, MLX5DR_ACTION_TYP_MAX, }; @@ -868,6 +869,34 @@ mlx5dr_action_create_reformat_ipv6_ext(struct mlx5dr_context *ctx, uint32_t log_bulk_size, uint32_t flags); +enum mlx5dr_action_nat64_flags { + MLX5DR_ACTION_NAT64_V4_TO_V6 = 1 << 0, + MLX5DR_ACTION_NAT64_V6_TO_V4 = 1 << 1, + /* Indicates if to backup ipv4 addresses in last two registers */ + MLX5DR_ACTION_NAT64_BACKUP_ADDR = 1 << 2, +}; + +struct mlx5dr_action_nat64_attr { + uint8_t num_of_registers; + uint8_t *registers; + enum mlx5dr_action_nat64_flags flags; +}; + +/* Create direct rule nat64 action. + * + * @param[in] ctx + * The context in which the new action will be created. + * @param[in] attr + * The relevant attribute of the NAT action. + * @param[in] flags + * Action creation flags. (enum mlx5dr_action_flags) + * @return pointer to mlx5dr_action on success NULL otherwise. + */ +struct mlx5dr_action * +mlx5dr_action_create_nat64(struct mlx5dr_context *ctx, + struct mlx5dr_action_nat64_attr *attr, + uint32_t flags); + /* Destroy direct rule action. * * @param[in] action diff --git a/drivers/net/mlx5/hws/mlx5dr_action.c b/drivers/net/mlx5/hws/mlx5dr_action.c index 631763dee0..96cad553aa 100644 --- a/drivers/net/mlx5/hws/mlx5dr_action.c +++ b/drivers/net/mlx5/hws/mlx5dr_action.c @@ -31,6 +31,7 @@ static const uint32_t action_order_arr[MLX5DR_TABLE_TYPE_MAX][MLX5DR_ACTION_TYP_ BIT(MLX5DR_ACTION_TYP_ASO_CT), BIT(MLX5DR_ACTION_TYP_PUSH_VLAN), BIT(MLX5DR_ACTION_TYP_PUSH_VLAN), + BIT(MLX5DR_ACTION_TYP_NAT64), BIT(MLX5DR_ACTION_TYP_MODIFY_HDR), BIT(MLX5DR_ACTION_TYP_INSERT_HEADER) | BIT(MLX5DR_ACTION_TYP_PUSH_IPV6_ROUTE_EXT) | @@ -52,6 +53,7 @@ static const uint32_t action_order_arr[MLX5DR_TABLE_TYPE_MAX][MLX5DR_ACTION_TYP_ BIT(MLX5DR_ACTION_TYP_ASO_CT), BIT(MLX5DR_ACTION_TYP_PUSH_VLAN), BIT(MLX5DR_ACTION_TYP_PUSH_VLAN), + BIT(MLX5DR_ACTION_TYP_NAT64), BIT(MLX5DR_ACTION_TYP_MODIFY_HDR), BIT(MLX5DR_ACTION_TYP_INSERT_HEADER) | BIT(MLX5DR_ACTION_TYP_PUSH_IPV6_ROUTE_EXT) | @@ -75,6 +77,7 @@ static const uint32_t action_order_arr[MLX5DR_TABLE_TYPE_MAX][MLX5DR_ACTION_TYP_ BIT(MLX5DR_ACTION_TYP_ASO_CT), BIT(MLX5DR_ACTION_TYP_PUSH_VLAN), BIT(MLX5DR_ACTION_TYP_PUSH_VLAN), + BIT(MLX5DR_ACTION_TYP_NAT64), BIT(MLX5DR_ACTION_TYP_MODIFY_HDR), BIT(MLX5DR_ACTION_TYP_INSERT_HEADER) | BIT(MLX5DR_ACTION_TYP_PUSH_IPV6_ROUTE_EXT) | @@ -246,6 +249,310 @@ static void mlx5dr_action_put_shared_stc(struct mlx5dr_action *action, mlx5dr_action_put_shared_stc_nic(ctx, stc_type, MLX5DR_TABLE_TYPE_FDB); } +static struct mlx5dr_action * +mlx5dr_action_create_nat64_copy_state(struct mlx5dr_context *ctx, + struct mlx5dr_action_nat64_attr *attr, + uint32_t flags) +{ + __be64 modify_action_data[MLX5DR_ACTION_NAT64_MAX_MODIFY_ACTIONS]; + struct mlx5dr_action_mh_pattern pat[2]; + struct mlx5dr_action *action; + uint32_t packet_len_field; + uint8_t *action_ptr; + uint32_t ttl_field; + uint32_t src_addr; + uint32_t dst_addr; + bool is_v4_to_v6; + + is_v4_to_v6 = attr->flags & MLX5DR_ACTION_NAT64_V4_TO_V6; + + if (is_v4_to_v6) { + packet_len_field = MLX5_MODI_OUT_IPV4_TOTAL_LEN; + ttl_field = MLX5_MODI_OUT_IPV4_TTL; + src_addr = MLX5_MODI_OUT_SIPV4; + dst_addr = MLX5_MODI_OUT_DIPV4; + } else { + packet_len_field = MLX5_MODI_OUT_IPV6_PAYLOAD_LEN; + ttl_field = MLX5_MODI_OUT_IPV6_HOPLIMIT; + src_addr = MLX5_MODI_OUT_SIPV6_31_0; + dst_addr = MLX5_MODI_OUT_DIPV6_31_0; + } + + memset(modify_action_data, 0, sizeof(modify_action_data)); + action_ptr = (uint8_t *)modify_action_data; + + if (attr->flags & MLX5DR_ACTION_NAT64_BACKUP_ADDR) { + MLX5_SET(copy_action_in, action_ptr, action_type, MLX5_MODIFICATION_TYPE_COPY); + MLX5_SET(copy_action_in, action_ptr, src_field, src_addr); + MLX5_SET(copy_action_in, action_ptr, dst_field, + attr->registers[MLX5DR_ACTION_NAT64_REG_SRC_IP]); + action_ptr += MLX5DR_ACTION_DOUBLE_SIZE; + + MLX5_SET(copy_action_in, action_ptr, action_type, MLX5_MODIFICATION_TYPE_COPY); + MLX5_SET(copy_action_in, action_ptr, src_field, dst_addr); + MLX5_SET(copy_action_in, action_ptr, dst_field, + attr->registers[MLX5DR_ACTION_NAT64_REG_DST_IP]); + action_ptr += MLX5DR_ACTION_DOUBLE_SIZE; + } + + /* | 8 bit - 8 bit - 16 bit | + * | ttl - protocol - packet-len | + */ + MLX5_SET(copy_action_in, action_ptr, action_type, MLX5_MODIFICATION_TYPE_COPY); + MLX5_SET(copy_action_in, action_ptr, src_field, packet_len_field); + MLX5_SET(copy_action_in, action_ptr, dst_field, + attr->registers[MLX5DR_ACTION_NAT64_REG_CONTROL]); + MLX5_SET(copy_action_in, action_ptr, dst_offset, 0);/* 16 bits in the lsb */ + MLX5_SET(copy_action_in, action_ptr, length, 16); + action_ptr += MLX5DR_ACTION_DOUBLE_SIZE; + + MLX5_SET(copy_action_in, action_ptr, action_type, MLX5_MODIFICATION_TYPE_NOP); + action_ptr += MLX5DR_ACTION_DOUBLE_SIZE; + + MLX5_SET(copy_action_in, action_ptr, action_type, MLX5_MODIFICATION_TYPE_COPY); + MLX5_SET(copy_action_in, action_ptr, src_field, MLX5_MODI_OUT_IP_PROTOCOL); + MLX5_SET(copy_action_in, action_ptr, dst_field, + attr->registers[MLX5DR_ACTION_NAT64_REG_CONTROL]); + MLX5_SET(copy_action_in, action_ptr, dst_offset, 16); + MLX5_SET(copy_action_in, action_ptr, length, 8); + action_ptr += MLX5DR_ACTION_DOUBLE_SIZE; + + MLX5_SET(copy_action_in, action_ptr, action_type, MLX5_MODIFICATION_TYPE_NOP); + action_ptr += MLX5DR_ACTION_DOUBLE_SIZE; + + MLX5_SET(copy_action_in, action_ptr, action_type, MLX5_MODIFICATION_TYPE_COPY); + MLX5_SET(copy_action_in, action_ptr, src_field, ttl_field); + MLX5_SET(copy_action_in, action_ptr, dst_field, + attr->registers[MLX5DR_ACTION_NAT64_REG_CONTROL]); + MLX5_SET(copy_action_in, action_ptr, dst_offset, 24); + MLX5_SET(copy_action_in, action_ptr, length, 8); + action_ptr += MLX5DR_ACTION_DOUBLE_SIZE; + + /* set sip and dip to 0, in order to have new csum */ + if (is_v4_to_v6) { + MLX5_SET(set_action_in, action_ptr, action_type, MLX5_MODIFICATION_TYPE_SET); + MLX5_SET(set_action_in, action_ptr, field, MLX5_MODI_OUT_SIPV4); + MLX5_SET(set_action_in, action_ptr, data, 0); + action_ptr += MLX5DR_ACTION_DOUBLE_SIZE; + + MLX5_SET(set_action_in, action_ptr, action_type, MLX5_MODIFICATION_TYPE_SET); + MLX5_SET(set_action_in, action_ptr, field, MLX5_MODI_OUT_DIPV4); + MLX5_SET(set_action_in, action_ptr, data, 0); + action_ptr += MLX5DR_ACTION_DOUBLE_SIZE; + } + + pat[0].data = modify_action_data; + pat[0].sz = (action_ptr - (uint8_t *)modify_action_data); + + action = mlx5dr_action_create_modify_header(ctx, 1, pat, 0, flags); + if (!action) { + DR_LOG(ERR, "Failed to create copy for NAT64: action_sz: %zu, flags: 0x%x\n", + pat[0].sz, flags); + return NULL; + } + + return action; +} + +static struct mlx5dr_action * +mlx5dr_action_create_nat64_repalce_state(struct mlx5dr_context *ctx, + struct mlx5dr_action_nat64_attr *attr, + uint32_t flags) +{ + uint32_t address_prefix[MLX5DR_ACTION_NAT64_HEADER_MINUS_ONE] = {0}; + __be64 modify_action_data[MLX5DR_ACTION_NAT64_MAX_MODIFY_ACTIONS]; + struct mlx5dr_action_mh_pattern pat[2]; + static struct mlx5dr_action *action; + uint8_t header_size_in_dw; + uint8_t *action_ptr; + uint32_t eth_type; + bool is_v4_to_v6; + uint32_t ip_ver; + int i; + + is_v4_to_v6 = attr->flags & MLX5DR_ACTION_NAT64_V4_TO_V6; + + if (is_v4_to_v6) { + uint32_t nat64_well_known_pref[] = {0x00010000, + 0x9bff6400, 0x0, 0x0, 0x0, + 0x9bff6400, 0x0, 0x0, 0x0}; + + header_size_in_dw = MLX5DR_ACTION_NAT64_IPV6_HEADER; + ip_ver = MLX5DR_ACTION_NAT64_IPV6_VER; + eth_type = RTE_ETHER_TYPE_IPV6; + memcpy(address_prefix, nat64_well_known_pref, + MLX5DR_ACTION_NAT64_HEADER_MINUS_ONE * sizeof(uint32_t)); + } else { + header_size_in_dw = MLX5DR_ACTION_NAT64_IPV4_HEADER; + ip_ver = MLX5DR_ACTION_NAT64_IPV4_VER; + eth_type = RTE_ETHER_TYPE_IPV4; + } + + memset(modify_action_data, 0, sizeof(modify_action_data)); + action_ptr = (uint8_t *)modify_action_data; + + MLX5_SET(set_action_in, action_ptr, action_type, MLX5_MODIFICATION_TYPE_SET); + MLX5_SET(set_action_in, action_ptr, field, MLX5_MODI_OUT_ETHERTYPE); + MLX5_SET(set_action_in, action_ptr, length, 16); + MLX5_SET(set_action_in, action_ptr, data, eth_type); + action_ptr += MLX5DR_ACTION_DOUBLE_SIZE; + + /* push empty header with ipv6 as version */ + MLX5_SET(stc_ste_param_insert, action_ptr, action_type, + MLX5_MODIFICATION_TYPE_INSERT); + MLX5_SET(stc_ste_param_insert, action_ptr, inline_data, 0x1); + MLX5_SET(stc_ste_param_insert, action_ptr, insert_anchor, + MLX5_HEADER_ANCHOR_IPV6_IPV4); + MLX5_SET(stc_ste_param_insert, action_ptr, insert_size, 2); + MLX5_SET(stc_ste_param_insert, action_ptr, insert_argument, ip_ver); + action_ptr += MLX5DR_ACTION_DOUBLE_SIZE; + + for (i = 0; i < header_size_in_dw - 1; i++) { + MLX5_SET(stc_ste_param_insert, action_ptr, action_type, + MLX5_MODIFICATION_TYPE_INSERT); + MLX5_SET(stc_ste_param_insert, action_ptr, inline_data, 0x1); + MLX5_SET(stc_ste_param_insert, action_ptr, insert_anchor, + MLX5_HEADER_ANCHOR_IPV6_IPV4); + MLX5_SET(stc_ste_param_insert, action_ptr, insert_size, 2); + MLX5_SET(stc_ste_param_insert, action_ptr, insert_argument, + htobe32(address_prefix[i])); + action_ptr += MLX5DR_ACTION_DOUBLE_SIZE; + } + + /* Remove orig src/dst addr (8 bytes, 4 words) */ + MLX5_SET(stc_ste_param_remove, action_ptr, action_type, + MLX5_MODIFICATION_TYPE_REMOVE); + MLX5_SET(stc_ste_param_remove, action_ptr, remove_start_anchor, + MLX5_HEADER_ANCHOR_IPV6_IPV4); + MLX5_SET(stc_ste_param_remove, action_ptr, remove_end_anchor, + MLX5_HEADER_ANCHOR_TCP_UDP); + action_ptr += MLX5DR_ACTION_DOUBLE_SIZE; + + pat[0].data = modify_action_data; + pat[0].sz = action_ptr - (uint8_t *)modify_action_data; + + action = mlx5dr_action_create_modify_header(ctx, 1, pat, 0, flags); + if (!action) { + DR_LOG(ERR, "Failed to create action: action_sz: %zu flags: 0x%x\n", + pat[0].sz, flags); + return NULL; + } + + return action; +} + +static struct mlx5dr_action * +mlx5dr_action_create_nat64_copy_back_state(struct mlx5dr_context *ctx, + struct mlx5dr_action_nat64_attr *attr, + uint32_t flags) +{ + __be64 modify_action_data[MLX5DR_ACTION_NAT64_MAX_MODIFY_ACTIONS]; + struct mlx5dr_action_mh_pattern pat[2]; + struct mlx5dr_action *action; + uint32_t packet_len_field; + uint32_t packet_len_add; + uint8_t *action_ptr; + uint32_t ttl_field; + uint32_t src_addr; + uint32_t dst_addr; + bool is_v4_to_v6; + + is_v4_to_v6 = attr->flags & MLX5DR_ACTION_NAT64_V4_TO_V6; + + if (is_v4_to_v6) { + packet_len_field = MLX5_MODI_OUT_IPV6_PAYLOAD_LEN; + /* 2' comp to 20, to get -20 in add operation */ + packet_len_add = MLX5DR_ACTION_NAT64_DEC_20; + ttl_field = MLX5_MODI_OUT_IPV6_HOPLIMIT; + src_addr = MLX5_MODI_OUT_SIPV6_31_0; + dst_addr = MLX5_MODI_OUT_DIPV6_31_0; + } else { + packet_len_field = MLX5_MODI_OUT_IPV4_TOTAL_LEN; + /* ipv4 len is including 20 bytes of the header, so add 20 over ipv6 len */ + packet_len_add = MLX5DR_ACTION_NAT64_ADD_20; + ttl_field = MLX5_MODI_OUT_IPV4_TTL; + src_addr = MLX5_MODI_OUT_SIPV4; + dst_addr = MLX5_MODI_OUT_DIPV4; + } + + memset(modify_action_data, 0, sizeof(modify_action_data)); + action_ptr = (uint8_t *)modify_action_data; + + MLX5_SET(copy_action_in, action_ptr, action_type, MLX5_MODIFICATION_TYPE_COPY); + MLX5_SET(copy_action_in, action_ptr, src_field, + attr->registers[MLX5DR_ACTION_NAT64_REG_CONTROL]); + MLX5_SET(copy_action_in, action_ptr, dst_field, + packet_len_field); + MLX5_SET(copy_action_in, action_ptr, src_offset, 32); + MLX5_SET(copy_action_in, action_ptr, length, 16); + action_ptr += MLX5DR_ACTION_DOUBLE_SIZE; + + MLX5_SET(copy_action_in, action_ptr, action_type, MLX5_MODIFICATION_TYPE_NOP); + action_ptr += MLX5DR_ACTION_DOUBLE_SIZE; + + MLX5_SET(copy_action_in, action_ptr, action_type, MLX5_MODIFICATION_TYPE_COPY); + MLX5_SET(copy_action_in, action_ptr, src_field, + attr->registers[MLX5DR_ACTION_NAT64_REG_CONTROL]); + MLX5_SET(copy_action_in, action_ptr, dst_field, + MLX5_MODI_OUT_IP_PROTOCOL); + MLX5_SET(copy_action_in, action_ptr, src_offset, 16); + MLX5_SET(copy_action_in, action_ptr, dst_offset, 0); + MLX5_SET(copy_action_in, action_ptr, length, 8); + action_ptr += MLX5DR_ACTION_DOUBLE_SIZE; + + MLX5_SET(copy_action_in, action_ptr, action_type, MLX5_MODIFICATION_TYPE_NOP); + action_ptr += MLX5DR_ACTION_DOUBLE_SIZE; + + MLX5_SET(copy_action_in, action_ptr, action_type, MLX5_MODIFICATION_TYPE_COPY); + MLX5_SET(copy_action_in, action_ptr, src_field, + attr->registers[MLX5DR_ACTION_NAT64_REG_CONTROL]); + MLX5_SET(copy_action_in, action_ptr, dst_field, ttl_field); + MLX5_SET(copy_action_in, action_ptr, src_offset, 24); + MLX5_SET(copy_action_in, action_ptr, length, 8); + action_ptr += MLX5DR_ACTION_DOUBLE_SIZE; + + MLX5_SET(copy_action_in, action_ptr, action_type, MLX5_MODIFICATION_TYPE_NOP); + action_ptr += MLX5DR_ACTION_DOUBLE_SIZE; + + /* if required Copy original addresses */ + if (attr->flags & MLX5DR_ACTION_NAT64_BACKUP_ADDR) { + MLX5_SET(copy_action_in, action_ptr, action_type, MLX5_MODIFICATION_TYPE_COPY); + MLX5_SET(copy_action_in, action_ptr, src_field, + attr->registers[MLX5DR_ACTION_NAT64_REG_SRC_IP]); + MLX5_SET(copy_action_in, action_ptr, dst_field, src_addr); + MLX5_SET(copy_action_in, action_ptr, src_offset, 0); + MLX5_SET(copy_action_in, action_ptr, length, 32); + action_ptr += MLX5DR_ACTION_DOUBLE_SIZE; + + MLX5_SET(copy_action_in, action_ptr, action_type, MLX5_MODIFICATION_TYPE_COPY); + MLX5_SET(copy_action_in, action_ptr, src_field, + attr->registers[MLX5DR_ACTION_NAT64_REG_DST_IP]); + MLX5_SET(copy_action_in, action_ptr, dst_field, dst_addr); + MLX5_SET(copy_action_in, action_ptr, src_offset, 0); + MLX5_SET(copy_action_in, action_ptr, length, 32); + action_ptr += MLX5DR_ACTION_DOUBLE_SIZE; + } + + /* take/add off 20 bytes ipv4/6 from/to the total size */ + MLX5_SET(set_action_in, action_ptr, action_type, MLX5_MODIFICATION_TYPE_ADD); + MLX5_SET(set_action_in, action_ptr, field, packet_len_field); + MLX5_SET(set_action_in, action_ptr, data, packet_len_add); + MLX5_SET(set_action_in, action_ptr, length, 16); + action_ptr += MLX5DR_ACTION_DOUBLE_SIZE; + + pat[0].data = modify_action_data; + pat[0].sz = action_ptr - (uint8_t *)modify_action_data; + + action = mlx5dr_action_create_modify_header(ctx, 1, pat, 0, flags); + if (!action) { + DR_LOG(ERR, "Failed to create action: action_sz: %zu, flags: 0x%x\n", + pat[0].sz, flags); + return NULL; + } + + return action; +} + static void mlx5dr_action_print_combo(enum mlx5dr_action_type *user_actions) { DR_LOG(ERR, "Invalid action_type sequence"); @@ -2530,6 +2837,94 @@ mlx5dr_action_create_reformat_ipv6_ext(struct mlx5dr_context *ctx, return NULL; } +static bool +mlx5dr_action_nat64_validate_param(struct mlx5dr_action_nat64_attr *attr, + uint32_t flags) +{ + if (mlx5dr_action_is_root_flags(flags)) { + DR_LOG(ERR, "Nat64 action not supported for root"); + rte_errno = ENOTSUP; + return false; + } + + if (!(flags & MLX5DR_ACTION_FLAG_SHARED)) { + DR_LOG(ERR, "Nat64 action must be with SHARED flag"); + rte_errno = EINVAL; + return false; + } + + if (attr->num_of_registers > MLX5DR_ACTION_NAT64_REG_MAX) { + DR_LOG(ERR, "Nat64 action doesn't support more than %d registers", + MLX5DR_ACTION_NAT64_REG_MAX); + rte_errno = EINVAL; + return false; + } + + if (attr->flags & MLX5DR_ACTION_NAT64_BACKUP_ADDR && + attr->num_of_registers != MLX5DR_ACTION_NAT64_REG_MAX) { + DR_LOG(ERR, "Nat64 backup addr requires %d registers", + MLX5DR_ACTION_NAT64_REG_MAX); + rte_errno = EINVAL; + return false; + } + + if (!(attr->flags & MLX5DR_ACTION_NAT64_V4_TO_V6 || + attr->flags & MLX5DR_ACTION_NAT64_V6_TO_V4)) { + DR_LOG(ERR, "Nat64 backup addr requires one mode at least"); + rte_errno = EINVAL; + return false; + } + + return true; +} + +struct mlx5dr_action * +mlx5dr_action_create_nat64(struct mlx5dr_context *ctx, + struct mlx5dr_action_nat64_attr *attr, + uint32_t flags) +{ + struct mlx5dr_action *action; + + if (!mlx5dr_action_nat64_validate_param(attr, flags)) + return NULL; + + action = mlx5dr_action_create_generic(ctx, flags, MLX5DR_ACTION_TYP_NAT64); + if (!action) + return NULL; + + action->nat64.stages[MLX5DR_ACTION_NAT64_STAGE_COPY] = + mlx5dr_action_create_nat64_copy_state(ctx, attr, flags); + if (!action->nat64.stages[MLX5DR_ACTION_NAT64_STAGE_COPY]) { + DR_LOG(ERR, "Nat64 failed creating copy state"); + goto free_action; + } + + action->nat64.stages[MLX5DR_ACTION_NAT64_STAGE_REPLACE] = + mlx5dr_action_create_nat64_repalce_state(ctx, attr, flags); + if (!action->nat64.stages[MLX5DR_ACTION_NAT64_STAGE_REPLACE]) { + DR_LOG(ERR, "Nat64 failed creating replace state"); + goto free_copy; + } + + action->nat64.stages[MLX5DR_ACTION_NAT64_STAGE_COPYBACK] = + mlx5dr_action_create_nat64_copy_back_state(ctx, attr, flags); + if (!action->nat64.stages[MLX5DR_ACTION_NAT64_STAGE_COPYBACK]) { + DR_LOG(ERR, "Nat64 failed creating copyback state"); + goto free_replace; + } + + return action; + + +free_replace: + mlx5dr_action_destroy(action->nat64.stages[MLX5DR_ACTION_NAT64_STAGE_REPLACE]); +free_copy: + mlx5dr_action_destroy(action->nat64.stages[MLX5DR_ACTION_NAT64_STAGE_COPY]); +free_action: + simple_free(action); + return NULL; +} + static void mlx5dr_action_destroy_hws(struct mlx5dr_action *action) { struct mlx5dr_devx_obj *obj = NULL; @@ -2604,6 +2999,10 @@ static void mlx5dr_action_destroy_hws(struct mlx5dr_action *action) if (action->ipv6_route_ext.action[i]) mlx5dr_action_destroy(action->ipv6_route_ext.action[i]); break; + case MLX5DR_ACTION_TYP_NAT64: + for (i = 0; i < MLX5DR_ACTION_NAT64_STAGES; i++) + mlx5dr_action_destroy(action->nat64.stages[i]); + break; } } @@ -2878,6 +3277,28 @@ mlx5dr_action_setter_modify_header(struct mlx5dr_actions_apply_data *apply, } } +static void +mlx5dr_action_setter_nat64(struct mlx5dr_actions_apply_data *apply, + struct mlx5dr_actions_wqe_setter *setter) +{ + struct mlx5dr_rule_action *rule_action; + struct mlx5dr_action *cur_stage_action; + struct mlx5dr_action *action; + uint32_t stc_idx; + + rule_action = &apply->rule_action[setter->idx_double]; + action = rule_action->action; + cur_stage_action = action->nat64.stages[setter->stage_idx]; + + stc_idx = htobe32(cur_stage_action->stc[apply->tbl_type].offset); + + apply->wqe_ctrl->stc_ix[MLX5DR_ACTION_STC_IDX_DW6] = stc_idx; + apply->wqe_ctrl->stc_ix[MLX5DR_ACTION_STC_IDX_DW7] = 0; + + apply->wqe_data[MLX5DR_ACTION_OFFSET_DW6] = 0; + apply->wqe_data[MLX5DR_ACTION_OFFSET_DW7] = 0; +} + static void mlx5dr_action_setter_insert_ptr(struct mlx5dr_actions_apply_data *apply, struct mlx5dr_actions_wqe_setter *setter) @@ -3178,7 +3599,7 @@ int mlx5dr_action_template_process(struct mlx5dr_action_template *at) struct mlx5dr_actions_wqe_setter *setter = at->setters; struct mlx5dr_actions_wqe_setter *pop_setter = NULL; struct mlx5dr_actions_wqe_setter *last_setter; - int i; + int i, j; /* Note: Given action combination must be valid */ @@ -3366,6 +3787,19 @@ int mlx5dr_action_template_process(struct mlx5dr_action_template *at) setter->idx_ctr = i; break; + case MLX5DR_ACTION_TYP_NAT64: + /* NAT64 requires 3 setters, each of them does specific modify header */ + for (j = 0; j < MLX5DR_ACTION_NAT64_STAGES; j++) { + setter = mlx5dr_action_setter_find_first(last_setter, + ASF_DOUBLE | ASF_REMOVE); + setter->flags |= ASF_DOUBLE | ASF_MODIFY; + setter->set_double = &mlx5dr_action_setter_nat64; + setter->idx_double = i; + /* The stage indicates which modify-header to push */ + setter->stage_idx = j; + } + break; + default: DR_LOG(ERR, "Unsupported action type: %d", action_type[i]); rte_errno = ENOTSUP; diff --git a/drivers/net/mlx5/hws/mlx5dr_action.h b/drivers/net/mlx5/hws/mlx5dr_action.h index 0c8e4bbb5a..064c18a90c 100644 --- a/drivers/net/mlx5/hws/mlx5dr_action.h +++ b/drivers/net/mlx5/hws/mlx5dr_action.h @@ -11,6 +11,9 @@ /* Max number of internal subactions of ipv6_ext */ #define MLX5DR_ACTION_IPV6_EXT_MAX_SA 4 +/* Number of MH in NAT64 */ +#define MLX5DR_ACTION_NAT64_STAGES 3 + enum mlx5dr_action_stc_idx { MLX5DR_ACTION_STC_IDX_CTRL = 0, MLX5DR_ACTION_STC_IDX_HIT = 1, @@ -68,6 +71,34 @@ enum mlx5dr_action_stc_reparse { MLX5DR_ACTION_STC_REPARSE_OFF, }; + /* 2' comp to 20, to get -20 in add operation */ +#define MLX5DR_ACTION_NAT64_DEC_20 0xffffffec + +enum { + MLX5DR_ACTION_NAT64_MAX_MODIFY_ACTIONS = 20, + MLX5DR_ACTION_NAT64_ADD_20 = 20, + MLX5DR_ACTION_NAT64_HEADER_MINUS_ONE = 9, + MLX5DR_ACTION_NAT64_IPV6_HEADER = 10, + MLX5DR_ACTION_NAT64_IPV4_HEADER = 5, + MLX5DR_ACTION_NAT64_IPV6_VER = 0x60000000, + MLX5DR_ACTION_NAT64_IPV4_VER = 0x45000000, +}; + +/* 3 stages for the nat64 action */ +enum mlx5dr_action_nat64_stages { + MLX5DR_ACTION_NAT64_STAGE_COPY = 0, + MLX5DR_ACTION_NAT64_STAGE_REPLACE = 1, + MLX5DR_ACTION_NAT64_STAGE_COPYBACK = 2, +}; + +/* Registers for keeping data from stage to stage */ +enum { + MLX5DR_ACTION_NAT64_REG_CONTROL = 0, + MLX5DR_ACTION_NAT64_REG_SRC_IP = 1, + MLX5DR_ACTION_NAT64_REG_DST_IP = 2, + MLX5DR_ACTION_NAT64_REG_MAX = 3, +}; + struct mlx5dr_action_default_stc { struct mlx5dr_pool_chunk nop_ctr; struct mlx5dr_pool_chunk nop_dw5; @@ -109,6 +140,7 @@ struct mlx5dr_actions_wqe_setter { uint8_t idx_double; uint8_t idx_ctr; uint8_t idx_hit; + uint8_t stage_idx; uint8_t flags; uint8_t extra_data; }; @@ -184,6 +216,9 @@ struct mlx5dr_action { uint8_t num_of_words; bool decap; } remove_header; + struct { + struct mlx5dr_action *stages[MLX5DR_ACTION_NAT64_STAGES]; + } nat64; }; }; diff --git a/drivers/net/mlx5/hws/mlx5dr_debug.c b/drivers/net/mlx5/hws/mlx5dr_debug.c index a9094cd35b..8f07c7fd66 100644 --- a/drivers/net/mlx5/hws/mlx5dr_debug.c +++ b/drivers/net/mlx5/hws/mlx5dr_debug.c @@ -28,6 +28,7 @@ const char *mlx5dr_debug_action_type_str[] = { [MLX5DR_ACTION_TYP_REMOVE_HEADER] = "REMOVE_HEADER", [MLX5DR_ACTION_TYP_POP_IPV6_ROUTE_EXT] = "POP_IPV6_ROUTE_EXT", [MLX5DR_ACTION_TYP_PUSH_IPV6_ROUTE_EXT] = "PUSH_IPV6_ROUTE_EXT", + [MLX5DR_ACTION_TYP_NAT64] = "NAT64", }; static_assert(ARRAY_SIZE(mlx5dr_debug_action_type_str) == MLX5DR_ACTION_TYP_MAX,