From patchwork Wed Feb 7 17:38:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Valerio X-Patchwork-Id: 1896250 X-Patchwork-Delegate: horms@verge.net.au Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=g21+Uu3w; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=openvswitch.org (client-ip=140.211.166.138; helo=smtp1.osuosl.org; envelope-from=ovs-dev-bounces@openvswitch.org; receiver=patchwork.ozlabs.org) Received: from smtp1.osuosl.org (smtp1.osuosl.org [140.211.166.138]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4TVS5G4PSlz23gM for ; Thu, 8 Feb 2024 04:38:26 +1100 (AEDT) Received: from localhost (localhost [127.0.0.1]) by smtp1.osuosl.org (Postfix) with ESMTP id A25DF8377F; Wed, 7 Feb 2024 17:38:22 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from smtp1.osuosl.org ([127.0.0.1]) by localhost (smtp1.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id uKQcIK2P9OhS; Wed, 7 Feb 2024 17:38:18 +0000 (UTC) X-Comment: SPF check N/A for local connections - client-ip=140.211.9.56; helo=lists.linuxfoundation.org; envelope-from=ovs-dev-bounces@openvswitch.org; receiver= DKIM-Filter: OpenDKIM Filter v2.11.0 smtp1.osuosl.org 936E483145 Authentication-Results: smtp1.osuosl.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=g21+Uu3w Received: from lists.linuxfoundation.org (lf-lists.osuosl.org [140.211.9.56]) by smtp1.osuosl.org (Postfix) with ESMTPS id 936E483145; Wed, 7 Feb 2024 17:38:18 +0000 (UTC) Received: from lf-lists.osuosl.org (localhost [127.0.0.1]) by lists.linuxfoundation.org (Postfix) with ESMTP id 80443C0072; Wed, 7 Feb 2024 17:38:18 +0000 (UTC) X-Original-To: ovs-dev@openvswitch.org Delivered-To: ovs-dev@lists.linuxfoundation.org Received: from smtp4.osuosl.org (smtp4.osuosl.org [IPv6:2605:bc80:3010::137]) by lists.linuxfoundation.org (Postfix) with ESMTP id A16FFC0037 for ; Wed, 7 Feb 2024 17:38:17 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by smtp4.osuosl.org (Postfix) with ESMTP id 7D79C4020C for ; Wed, 7 Feb 2024 17:38:17 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from smtp4.osuosl.org ([127.0.0.1]) by localhost (smtp4.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 27iVP9IjiMTe for ; Wed, 7 Feb 2024 17:38:16 +0000 (UTC) Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=170.10.129.124; helo=us-smtp-delivery-124.mimecast.com; envelope-from=pvalerio@redhat.com; receiver= DMARC-Filter: OpenDMARC Filter v1.4.2 smtp4.osuosl.org A0CD340207 Authentication-Results: smtp4.osuosl.org; dmarc=pass (p=none dis=none) header.from=redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 smtp4.osuosl.org A0CD340207 Authentication-Results: smtp4.osuosl.org; dkim=pass (1024-bit key, unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=g21+Uu3w Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by smtp4.osuosl.org (Postfix) with ESMTPS id A0CD340207 for ; Wed, 7 Feb 2024 17:38:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1707327495; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=Z32er1/iyMdnUWgdND6kNRF2yjmjGX6apWtj622fA88=; b=g21+Uu3wqR5MaKvE1HAolN9lvKnS65+qCE2/P4rc/Y0gpjbhShPfGceRjWBVS/y1l/APkh myuFtPnmwJL0w4XTzJRjHyCAy34F5bAFBR8V+F5/B//EGbwXwDyYgkylXyDr4giZOy+YuP qWc7dW/d6e/u2F3HvKjIuYtC2WVV2BI= Received: from mail-ej1-f72.google.com (mail-ej1-f72.google.com [209.85.218.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-443-PTZbjnWRNbePUClwtAb4Xg-1; Wed, 07 Feb 2024 12:38:13 -0500 X-MC-Unique: PTZbjnWRNbePUClwtAb4Xg-1 Received: by mail-ej1-f72.google.com with SMTP id a640c23a62f3a-a3571b434bfso14670366b.1 for ; Wed, 07 Feb 2024 09:38:13 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707327491; x=1707932291; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=Z32er1/iyMdnUWgdND6kNRF2yjmjGX6apWtj622fA88=; b=DqFIrRqLnFY4XzHMSahzEPMRXBleioX7CIlXbczMDlSWYt3c086dqhu+EEmn2tUaEL 5TCtCkarfE/lvrcG8W+vgIXMOwQCxSIqg606fwQTPjkeAWmyPrRinH+tQkpFmVB+H+Za AwQrwJGtprzFlLG6JNDe39m8blyS9eLhDzzvDGQLBIejXtx2qFkGnqEzAuR3KCPcKHnz uQAvrA4m7TSY/eNAkVBXo3Hwk6soxG6JivZGCZMMYiP7MurbgLpjS5tlPHpWkLi+WCds U4I2O9ymuTzf7lCiHSMW9cKs6xe1Ca0F/hbEcakoy4Y3WPI/bSqDccHjOMFTmduli4aP uZlg== X-Gm-Message-State: AOJu0YxVHV0wHmHzfvMqCtB2Ulf1uLzwfrMdAdAQLNDGU//MlxmGNE7p C1KUcTOCt7qm72FuoChCa47Z+cjXb45+gMclxDrc8ho732DWf/lacZHW88NqV2u1t18Elp4nRHF lrQXVysRh7LofypjblRfPuGsz3EUR0tgJsWKYTDkXqsaBLvYlbe2fpKeHUr4nWJpJnePkofTT80 Wc5R8btaB9JkQtzkystOezBUE7z6j82mIAg2X5SEc= X-Received: by 2002:a50:d6c6:0:b0:560:26f2:f8a7 with SMTP id l6-20020a50d6c6000000b0056026f2f8a7mr4913011edj.0.1707327491439; Wed, 07 Feb 2024 09:38:11 -0800 (PST) X-Google-Smtp-Source: AGHT+IETxB24JwAHJZyZRh6r9bYbWtg0H7RwK4NIh4liM/RfiHZi2+srVo5pwRKCNeopa7QGVoDOuA== X-Received: by 2002:a50:d6c6:0:b0:560:26f2:f8a7 with SMTP id l6-20020a50d6c6000000b0056026f2f8a7mr4912993edj.0.1707327491050; Wed, 07 Feb 2024 09:38:11 -0800 (PST) Received: from localhost ([37.183.153.57]) by smtp.gmail.com with ESMTPSA id c20-20020a056402121400b0055fe55441cbsm875024edw.40.2024.02.07.09.38.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Feb 2024 09:38:10 -0800 (PST) From: Paolo Valerio To: ovs-dev@openvswitch.org Date: Wed, 7 Feb 2024 18:38:07 +0100 Message-ID: <20240207173808.1475540-1-pvalerio@redhat.com> X-Mailer: git-send-email 2.43.0 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Subject: [ovs-dev] [PATCH v2 1/2] conntrack: Handle random selection for port ranges. X-BeenThere: ovs-dev@openvswitch.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: ovs-dev-bounces@openvswitch.org Sender: "dev" The userspace conntrack only supported hash for port selection. With the patch, both userspace and kernel datapath support the random flag. The default behavior remains the same, that is, if no flags are specified, hash is selected. Signed-off-by: Paolo Valerio Acked-by: Simon Horman --- Documentation/ref/ovs-actions.7.rst | 3 +-- NEWS | 3 +++ lib/conntrack.c | 15 ++++++++------- lib/conntrack.h | 5 +++++ lib/dpif-netdev.c | 4 +++- 5 files changed, 20 insertions(+), 10 deletions(-) diff --git a/Documentation/ref/ovs-actions.7.rst b/Documentation/ref/ovs-actions.7.rst index 36adcc5db..80acd9070 100644 --- a/Documentation/ref/ovs-actions.7.rst +++ b/Documentation/ref/ovs-actions.7.rst @@ -1551,8 +1551,7 @@ following arguments: should be selected. When a port range is specified, fallback to ephemeral ports does not happen, else, it will. The port number selection can be informed by the optional ``random`` and ``hash`` flags - described below. The userspace datapath only supports the ``hash`` - behavior. + described below. The optional *flags* are: diff --git a/NEWS b/NEWS index a6617546c..93046b963 100644 --- a/NEWS +++ b/NEWS @@ -1,5 +1,8 @@ Post-v3.3.0 -------------------- + - Userspace datapath: + * Conntrack now supports 'random' flag for selecting ports in a range + while natting. v3.3.0 - xx xxx xxxx diff --git a/lib/conntrack.c b/lib/conntrack.c index 013709bd6..e09ecdf33 100644 --- a/lib/conntrack.c +++ b/lib/conntrack.c @@ -2222,7 +2222,7 @@ nat_range_hash(const struct conn_key *key, uint32_t basis, /* Ports are stored in host byte order for convenience. */ static void set_sport_range(const struct nat_action_info_t *ni, const struct conn_key *k, - uint32_t hash, uint16_t *curr, uint16_t *min, + uint32_t off, uint16_t *curr, uint16_t *min, uint16_t *max) { if (((ni->nat_action & NAT_ACTION_SNAT_ALL) == NAT_ACTION_SRC) || @@ -2241,19 +2241,19 @@ set_sport_range(const struct nat_action_info_t *ni, const struct conn_key *k, } else { *min = ni->min_port; *max = ni->max_port; - *curr = *min + (hash % ((*max - *min) + 1)); + *curr = *min + (off % ((*max - *min) + 1)); } } static void set_dport_range(const struct nat_action_info_t *ni, const struct conn_key *k, - uint32_t hash, uint16_t *curr, uint16_t *min, + uint32_t off, uint16_t *curr, uint16_t *min, uint16_t *max) { if (ni->nat_action & NAT_ACTION_DST_PORT) { *min = ni->min_port; *max = ni->max_port; - *curr = *min + (hash % ((*max - *min) + 1)); + *curr = *min + (off % ((*max - *min) + 1)); } else { *curr = ntohs(k->dst.port); *min = *max = *curr; @@ -2388,18 +2388,19 @@ nat_get_unique_tuple(struct conntrack *ct, struct conn *conn, fwd_key->nw_proto == IPPROTO_SCTP; uint16_t min_dport, max_dport, curr_dport; uint16_t min_sport, max_sport, curr_sport; - uint32_t hash; + uint32_t hash, port_off; hash = nat_range_hash(fwd_key, ct->hash_basis, nat_info); + port_off = nat_info->nat_flags & NAT_RANGE_RANDOM ? random_uint32() : hash; min_addr = nat_info->min_addr; max_addr = nat_info->max_addr; find_addr(fwd_key, &min_addr, &max_addr, &addr, hash, (fwd_key->dl_type == htons(ETH_TYPE_IP)), nat_info); - set_sport_range(nat_info, fwd_key, hash, &curr_sport, + set_sport_range(nat_info, fwd_key, port_off, &curr_sport, &min_sport, &max_sport); - set_dport_range(nat_info, fwd_key, hash, &curr_dport, + set_dport_range(nat_info, fwd_key, port_off, &curr_dport, &min_dport, &max_dport); if (pat_proto) { diff --git a/lib/conntrack.h b/lib/conntrack.h index 0a888be45..9b0c6aa88 100644 --- a/lib/conntrack.h +++ b/lib/conntrack.h @@ -77,12 +77,17 @@ enum nat_action_e { NAT_ACTION_DST_PORT = 1 << 3, }; +enum nat_flags_e { + NAT_RANGE_RANDOM = 1 << 0, +}; + struct nat_action_info_t { union ct_addr min_addr; union ct_addr max_addr; uint16_t min_port; uint16_t max_port; uint16_t nat_action; + uint16_t nat_flags; }; struct conntrack *conntrack_init(void); diff --git a/lib/dpif-netdev.c b/lib/dpif-netdev.c index c1981137f..c3334c667 100644 --- a/lib/dpif-netdev.c +++ b/lib/dpif-netdev.c @@ -9409,9 +9409,11 @@ dp_execute_cb(void *aux_, struct dp_packet_batch *packets_, nl_attr_get_u16(b_nest); proto_num_max_specified = true; break; + case OVS_NAT_ATTR_PROTO_RANDOM: + nat_action_info.nat_flags |= NAT_RANGE_RANDOM; + break; case OVS_NAT_ATTR_PERSISTENT: case OVS_NAT_ATTR_PROTO_HASH: - case OVS_NAT_ATTR_PROTO_RANDOM: break; case OVS_NAT_ATTR_UNSPEC: case __OVS_NAT_ATTR_MAX: From patchwork Wed Feb 7 17:38:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Valerio X-Patchwork-Id: 1896249 X-Patchwork-Delegate: horms@verge.net.au Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=BxWPr8en; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=openvswitch.org (client-ip=140.211.166.137; helo=smtp4.osuosl.org; envelope-from=ovs-dev-bounces@openvswitch.org; receiver=patchwork.ozlabs.org) Received: from smtp4.osuosl.org (smtp4.osuosl.org [140.211.166.137]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4TVS5F2nX5z23gM for ; Thu, 8 Feb 2024 04:38:23 +1100 (AEDT) Received: from localhost (localhost [127.0.0.1]) by smtp4.osuosl.org (Postfix) with ESMTP id 79BFD40207; Wed, 7 Feb 2024 17:38:21 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from smtp4.osuosl.org ([127.0.0.1]) by localhost (smtp4.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id xx4tDsGWe2QP; Wed, 7 Feb 2024 17:38:20 +0000 (UTC) X-Comment: SPF check N/A for local connections - client-ip=2605:bc80:3010:104::8cd3:938; helo=lists.linuxfoundation.org; envelope-from=ovs-dev-bounces@openvswitch.org; receiver= DKIM-Filter: OpenDKIM Filter v2.11.0 smtp4.osuosl.org 611D14020C Authentication-Results: smtp4.osuosl.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=BxWPr8en Received: from lists.linuxfoundation.org (lf-lists.osuosl.org [IPv6:2605:bc80:3010:104::8cd3:938]) by smtp4.osuosl.org (Postfix) with ESMTPS id 611D14020C; Wed, 7 Feb 2024 17:38:20 +0000 (UTC) Received: from lf-lists.osuosl.org (localhost [127.0.0.1]) by lists.linuxfoundation.org (Postfix) with ESMTP id 3B53FC0072; Wed, 7 Feb 2024 17:38:20 +0000 (UTC) X-Original-To: ovs-dev@openvswitch.org Delivered-To: ovs-dev@lists.linuxfoundation.org Received: from smtp3.osuosl.org (smtp3.osuosl.org [IPv6:2605:bc80:3010::136]) by lists.linuxfoundation.org (Postfix) with ESMTP id ACFFDC007C for ; Wed, 7 Feb 2024 17:38:19 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by smtp3.osuosl.org (Postfix) with ESMTP id 88C7360DC2 for ; Wed, 7 Feb 2024 17:38:19 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from smtp3.osuosl.org ([127.0.0.1]) by localhost (smtp3.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id YKF_vcTjxOUl for ; Wed, 7 Feb 2024 17:38:18 +0000 (UTC) Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=170.10.133.124; helo=us-smtp-delivery-124.mimecast.com; envelope-from=pvalerio@redhat.com; receiver= DMARC-Filter: OpenDMARC Filter v1.4.2 smtp3.osuosl.org 3942E60B44 Authentication-Results: smtp3.osuosl.org; dmarc=pass (p=none dis=none) header.from=redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 smtp3.osuosl.org 3942E60B44 Authentication-Results: smtp3.osuosl.org; dkim=pass (1024-bit key, unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=BxWPr8en Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by smtp3.osuosl.org (Postfix) with ESMTPS id 3942E60B44 for ; Wed, 7 Feb 2024 17:38:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1707327497; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=xlEvGHYVXBaNrS8NcnpuRkCXCXKzOICWBww5QWwpy9c=; b=BxWPr8enWhnzB3SzTrnJ7I9mgfIFR4tpkj7lfOvnI+roisI4X0nxUkFaC32eEq0inlM6WY VyC4h9yqESuSvdIPNKHAmRdXLniEKDaFrkFyTjLJ5TzUKKNrPClFSHr6Miwxtqkqj75Cc/ pGr1Blt7OlIrbzs7j6cU/JuyuSJrrdY= Received: from mail-lj1-f197.google.com (mail-lj1-f197.google.com [209.85.208.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-688-EcG5KR06Oi2EQFK0XtZcvQ-1; Wed, 07 Feb 2024 12:38:16 -0500 X-MC-Unique: EcG5KR06Oi2EQFK0XtZcvQ-1 Received: by mail-lj1-f197.google.com with SMTP id 38308e7fff4ca-2d0c8552901so707401fa.1 for ; Wed, 07 Feb 2024 09:38:15 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707327493; x=1707932293; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=xlEvGHYVXBaNrS8NcnpuRkCXCXKzOICWBww5QWwpy9c=; b=DLOAVZAiSWjK4GKXeIR0O8pbXOvdIMRoruDWe0P+suoEIn0cTLyWz3tKnBmIT5DSoX uZQ0S2pWXhRkTet2lsxDeVCSNE5D69BpbMhYkeGBIVY0ZQrkENoXcCyPd3DgYoFBQHSL 7p8eY0QNWAhfXv2ZaUdcSBJxVabW8EScY3Tys9Vql8wZOpReLtajNQxw5W6ONSiZoktv 33REoUu1juNEg9jCO7AcbP72UaEhalxWAVZHSLKTJVF86TDb8+GknruG21FbQ02Z/BZA bise/to9q82hOk6mZWJTD2zxhxjZUaMsMfWw+w4Cond4y8nU1jPuokfsg1Df0Dd8wkfi 04yQ== X-Gm-Message-State: AOJu0YxG6tCIByalcaNTeCpqxKqF6edGF7eynOEScQA2ysymhHWO6ZrW C1WyTSI/stgF+9/Xhmvn7XbnwPn10ae08ES4qrtQqjdL4oCiaV9m5oNkK5+EVxQEKHLfkL6dwxX d1g3zyxs5AUtB6f8Avb6O/6gO/jFZSx7rbiBTPiOye1ltv9AG30e4f7M6K4449vz/Z+XyqJwVFX ROAy1RjR5MNrEWcqwMHrU6itwsJMXR8A0U7zt69fY= X-Received: by 2002:a2e:910d:0:b0:2cd:1ca5:282c with SMTP id m13-20020a2e910d000000b002cd1ca5282cmr4124711ljg.5.1707327493527; Wed, 07 Feb 2024 09:38:13 -0800 (PST) X-Google-Smtp-Source: AGHT+IF/Rmei1ondm9oH0+BTfLOUjzinEpE/dMHZvBwpoOMfgeLoakpYewhonGBAF+kip3Rkkx169g== X-Received: by 2002:a2e:910d:0:b0:2cd:1ca5:282c with SMTP id m13-20020a2e910d000000b002cd1ca5282cmr4124705ljg.5.1707327493150; Wed, 07 Feb 2024 09:38:13 -0800 (PST) Received: from localhost ([37.183.153.57]) by smtp.gmail.com with ESMTPSA id eh19-20020a0564020f9300b0055ffe74e39dsm871963edb.85.2024.02.07.09.38.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Feb 2024 09:38:12 -0800 (PST) From: Paolo Valerio To: ovs-dev@openvswitch.org Date: Wed, 7 Feb 2024 18:38:08 +0100 Message-ID: <20240207173808.1475540-2-pvalerio@redhat.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240207173808.1475540-1-pvalerio@redhat.com> References: <20240207173808.1475540-1-pvalerio@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Subject: [ovs-dev] [PATCH v2 2/2] conntrack: Handle persistent selection for IP addresses. X-BeenThere: ovs-dev@openvswitch.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: ovs-dev-bounces@openvswitch.org Sender: "dev" The patch, when 'persistent' flag is specified, makes the IP selection in a range persistent across reboots. Signed-off-by: Paolo Valerio Acked-by: Simon Horman --- NEWS | 3 ++- lib/conntrack.c | 27 +++++++++++++++++++++------ lib/conntrack.h | 1 + lib/dpif-netdev.c | 2 ++ 4 files changed, 26 insertions(+), 7 deletions(-) diff --git a/NEWS b/NEWS index 93046b963..0c86bba81 100644 --- a/NEWS +++ b/NEWS @@ -2,7 +2,8 @@ Post-v3.3.0 -------------------- - Userspace datapath: * Conntrack now supports 'random' flag for selecting ports in a range - while natting. + while natting and 'persistent' flag for selection of the IP address + from a range. v3.3.0 - xx xxx xxxx diff --git a/lib/conntrack.c b/lib/conntrack.c index e09ecdf33..7868a67f7 100644 --- a/lib/conntrack.c +++ b/lib/conntrack.c @@ -2202,17 +2202,21 @@ nat_range_hash(const struct conn_key *key, uint32_t basis, { uint32_t hash = basis; + if (!basis) { + hash = ct_addr_hash_add(hash, &key->src.addr); + } else { + hash = ct_endpoint_hash_add(hash, &key->src); + hash = ct_endpoint_hash_add(hash, &key->dst); + } + hash = ct_addr_hash_add(hash, &nat_info->min_addr); hash = ct_addr_hash_add(hash, &nat_info->max_addr); hash = hash_add(hash, ((uint32_t) nat_info->max_port << 16) | nat_info->min_port); - hash = ct_endpoint_hash_add(hash, &key->src); - hash = ct_endpoint_hash_add(hash, &key->dst); hash = hash_add(hash, (OVS_FORCE uint32_t) key->dl_type); hash = hash_add(hash, key->nw_proto); hash = hash_add(hash, key->zone); - /* The purpose of the second parameter is to distinguish hashes of data of * different length; our data always has the same length so there is no * value in counting. */ @@ -2386,12 +2390,23 @@ nat_get_unique_tuple(struct conntrack *ct, struct conn *conn, bool pat_proto = fwd_key->nw_proto == IPPROTO_TCP || fwd_key->nw_proto == IPPROTO_UDP || fwd_key->nw_proto == IPPROTO_SCTP; + uint32_t hash, port_off, basis = ct->hash_basis; uint16_t min_dport, max_dport, curr_dport; uint16_t min_sport, max_sport, curr_sport; - uint32_t hash, port_off; - hash = nat_range_hash(fwd_key, ct->hash_basis, nat_info); - port_off = nat_info->nat_flags & NAT_RANGE_RANDOM ? random_uint32() : hash; + if (nat_info->nat_flags & NAT_PERSISTENT) { + basis = 0; + } + + hash = nat_range_hash(fwd_key, basis, nat_info); + + if (nat_info->nat_flags & NAT_RANGE_RANDOM) { + port_off = random_uint32(); + } else { + port_off = + basis ? hash : nat_range_hash(fwd_key, ct->hash_basis, nat_info); + } + min_addr = nat_info->min_addr; max_addr = nat_info->max_addr; diff --git a/lib/conntrack.h b/lib/conntrack.h index 9b0c6aa88..ee7da099e 100644 --- a/lib/conntrack.h +++ b/lib/conntrack.h @@ -79,6 +79,7 @@ enum nat_action_e { enum nat_flags_e { NAT_RANGE_RANDOM = 1 << 0, + NAT_PERSISTENT = 1 << 1, }; struct nat_action_info_t { diff --git a/lib/dpif-netdev.c b/lib/dpif-netdev.c index c3334c667..fbf7ccabd 100644 --- a/lib/dpif-netdev.c +++ b/lib/dpif-netdev.c @@ -9413,6 +9413,8 @@ dp_execute_cb(void *aux_, struct dp_packet_batch *packets_, nat_action_info.nat_flags |= NAT_RANGE_RANDOM; break; case OVS_NAT_ATTR_PERSISTENT: + nat_action_info.nat_flags |= NAT_PERSISTENT; + break; case OVS_NAT_ATTR_PROTO_HASH: break; case OVS_NAT_ATTR_UNSPEC: