From owner-dev-commits-src-all@freebsd.org Wed Jan 20 14:44:55 2021 Return-Path: Delivered-To: dev-commits-src-all@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 56DC94F8A0C; Wed, 20 Jan 2021 14:44:55 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4DLSwl22gGz4qnQ; Wed, 20 Jan 2021 14:44:55 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 38F171ADC9; Wed, 20 Jan 2021 14:44:55 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 10KEitZt019336; Wed, 20 Jan 2021 14:44:55 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 10KEit82019335; Wed, 20 Jan 2021 14:44:55 GMT (envelope-from git) Date: Wed, 20 Jan 2021 14:44:55 GMT Message-Id: <202101201444.10KEit82019335@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Kristof Provost Subject: git: b55dd986a489 - stable/12 - pf: Use counter_u64 in pf_src_node MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: kp X-Git-Repository: src X-Git-Refname: refs/heads/stable/12 X-Git-Reftype: branch X-Git-Commit: b55dd986a4894e0705be24118bcedc1b60120d82 Auto-Submitted: auto-generated X-BeenThere: dev-commits-src-all@freebsd.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Commit messages for all branches of the src repository List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 20 Jan 2021 14:44:55 -0000 The branch stable/12 has been updated by kp: URL: https://cgit.FreeBSD.org/src/commit/?id=b55dd986a4894e0705be24118bcedc1b60120d82 commit b55dd986a4894e0705be24118bcedc1b60120d82 Author: Kristof Provost AuthorDate: 2020-11-13 19:31:51 +0000 Commit: Kristof Provost CommitDate: 2021-01-20 14:16:04 +0000 pf: Use counter_u64 in pf_src_node Reviewd by: philip MFC after: 2 weeks Sponsored by: Orange Business Services Differential Revision: https://reviews.freebsd.org/D27756 (cherry picked from commit fbbf270eef271806a0a106e45356d91f5b5e1f55) --- sys/net/pfvar.h | 4 ++-- sys/netpfil/pf/pf.c | 50 ++++++++++++++++++++++++++++++++++++++--------- sys/netpfil/pf/pf_ioctl.c | 7 +++++-- 3 files changed, 48 insertions(+), 13 deletions(-) diff --git a/sys/net/pfvar.h b/sys/net/pfvar.h index 5ff47c99b457..dd85ac5f7b9f 100644 --- a/sys/net/pfvar.h +++ b/sys/net/pfvar.h @@ -616,8 +616,8 @@ struct pf_ksrc_node { struct pf_addr raddr; union pf_rule_ptr rule; struct pfi_kif *kif; - u_int64_t bytes[2]; - u_int64_t packets[2]; + counter_u64_t bytes[2]; + counter_u64_t packets[2]; u_int32_t states; u_int32_t conn; struct pf_threshold conn_rate; diff --git a/sys/netpfil/pf/pf.c b/sys/netpfil/pf/pf.c index 7b2128da7985..89236817e3e9 100644 --- a/sys/netpfil/pf/pf.c +++ b/sys/netpfil/pf/pf.c @@ -702,6 +702,19 @@ pf_find_src_node(struct pf_addr *src, struct pf_rule *rule, sa_family_t af, return (n); } +static void +pf_free_src_node(struct pf_ksrc_node *sn) +{ + + for (int i = 0; i < 2; i++) { + if (sn->bytes[i]) + counter_u64_free(sn->bytes[i]); + if (sn->packets[i]) + counter_u64_free(sn->packets[i]); + } + uma_zfree(V_pf_sources_z, sn); +} + static int pf_insert_src_node(struct pf_ksrc_node **sn, struct pf_rule *rule, struct pf_addr *src, sa_family_t af) @@ -730,6 +743,17 @@ pf_insert_src_node(struct pf_ksrc_node **sn, struct pf_rule *rule, return (-1); } + for (int i = 0; i < 2; i++) { + (*sn)->bytes[i] = counter_u64_alloc(M_NOWAIT); + (*sn)->packets[i] = counter_u64_alloc(M_NOWAIT); + + if ((*sn)->bytes[i] == NULL || (*sn)->packets[i] == NULL) { + pf_free_src_node(*sn); + PF_HASHROW_UNLOCK(sh); + return (-1); + } + } + pf_init_threshold(&(*sn)->conn_rate, rule->max_src_conn_rate.limit, rule->max_src_conn_rate.seconds); @@ -773,7 +797,7 @@ pf_free_src_nodes(struct pf_ksrc_node_list *head) u_int count = 0; LIST_FOREACH_SAFE(sn, head, entry, tmp) { - uma_zfree(V_pf_sources_z, sn); + pf_free_src_node(sn); count++; } @@ -6322,12 +6346,16 @@ done: s->nat_rule.ptr->bytes[dirndx] += pd.tot_len; } if (s->src_node != NULL) { - s->src_node->packets[dirndx]++; - s->src_node->bytes[dirndx] += pd.tot_len; + counter_u64_add(s->src_node->packets[dirndx], + 1); + counter_u64_add(s->src_node->bytes[dirndx], + pd.tot_len); } if (s->nat_src_node != NULL) { - s->nat_src_node->packets[dirndx]++; - s->nat_src_node->bytes[dirndx] += pd.tot_len; + counter_u64_add(s->nat_src_node->packets[dirndx], + 1); + counter_u64_add(s->nat_src_node->bytes[dirndx], + pd.tot_len); } dirndx = (dir == s->direction) ? 0 : 1; counter_u64_add(s->packets[dirndx], 1); @@ -6721,12 +6749,16 @@ done: s->nat_rule.ptr->bytes[dirndx] += pd.tot_len; } if (s->src_node != NULL) { - s->src_node->packets[dirndx]++; - s->src_node->bytes[dirndx] += pd.tot_len; + counter_u64_add(s->src_node->packets[dirndx], + 1); + counter_u64_add(s->src_node->bytes[dirndx], + pd.tot_len); } if (s->nat_src_node != NULL) { - s->nat_src_node->packets[dirndx]++; - s->nat_src_node->bytes[dirndx] += pd.tot_len; + counter_u64_add(s->nat_src_node->packets[dirndx], + 1); + counter_u64_add(s->nat_src_node->bytes[dirndx], + pd.tot_len); } dirndx = (dir == s->direction) ? 0 : 1; counter_u64_add(s->packets[dirndx], 1); diff --git a/sys/netpfil/pf/pf_ioctl.c b/sys/netpfil/pf/pf_ioctl.c index cb503f7f3e1e..4767cfd48afd 100644 --- a/sys/netpfil/pf/pf_ioctl.c +++ b/sys/netpfil/pf/pf_ioctl.c @@ -1163,8 +1163,11 @@ pf_src_node_copy(const struct pf_ksrc_node *in, struct pf_src_node *out) if (in->rule.ptr != NULL) out->rule.nr = in->rule.ptr->nr; - bcopy(&in->bytes, &out->bytes, sizeof(u_int64_t) * 2); - bcopy(&in->packets, &out->packets, sizeof(u_int64_t) * 2); + for (int i = 0; i < 2; i++) { + out->bytes[i] = counter_u64_fetch(in->bytes[i]); + out->packets[i] = counter_u64_fetch(in->packets[i]); + } + out->states = in->states; out->conn = in->conn; out->af = in->af;