From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id 29094A0547; Fri, 10 Sep 2021 20:19:40 +0200 (CEST) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 55FC54116B; Fri, 10 Sep 2021 20:19:01 +0200 (CEST) Received: from mail-pj1-f44.google.com (mail-pj1-f44.google.com [209.85.216.44]) by mails.dpdk.org (Postfix) with ESMTP id 2C16140DFD for ; Fri, 10 Sep 2021 20:18:55 +0200 (CEST) Received: by mail-pj1-f44.google.com with SMTP id j10-20020a17090a94ca00b00181f17b7ef7so2103045pjw.2 for ; Fri, 10 Sep 2021 11:18:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=networkplumber-org.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=PDdY4oBU1gcR2+3EtAxIcQ34wYOai6D2reU0A2iPtss=; b=FTjPY1yYjUvJzNalYCkyKPU+ruZf6bBhxZeKwGPOl+7aIGXp8sJitlYIer0lHfMLiz 9QmaCIamt7UcdXde+9vz3PTP/zCei99G8X0NvL3Zu+sner4CqhKtrAKVnzdDdBgc9B6I eA0/175rEUdTNNawqg0AUHbsbuKp6/9ut/PWlnIop7+jUmtA1/+/7jNWwbfuwDm4EnIW NgSMD+pWnCXzwwAqeIkXYS0lShc7/Ozs1RfdQgbDdMOR5HhIZsUewzKdjVHxIbPlm163 oveYGe/We6kuZpVql72o300dZd981RF+zfqismXMXh12wb+YuV5S3sisMkbDDGJXTEgw EkfA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=PDdY4oBU1gcR2+3EtAxIcQ34wYOai6D2reU0A2iPtss=; b=yEMAT1ra25kYn1LjojvIm/w8HA/nsvwZTMklxsoaIlShCeBSf6KwLTaiEnwQ2DaJE7 tZlD32U6rbNBun0Vi7XhUwdi3PkzZ9eeOoZqoey9inTdqhxpcPygyKYjHR9DVZ+C5PGd N83FdFHGIvkFivmcu05kqkIoFByTnYzmO1q6OSLMOTJNApd2g9KeMWL9wkm0t+dT2xMs ogQwtAGEXhEwklPr67vUmdLjzLi98bRIWO3ePDRpGSMxdon6qnWy6ZlQ0407W8GFqLlE tQSVR9xYiEQaVIyWc5evynj0anGwOdUEfCo54OYhGjPELN9RX0nUjCXC4lNdxdjIvmaL cWmA== X-Gm-Message-State: AOAM5308CRFl4QhzQm2w98N+kLBZ79Ixn50JaB0lVXMzw+MB+eKrYqrn 4y51hvYAwRMZCIyWmVKcl2DtBC7yL0S9DA== X-Google-Smtp-Source: ABdhPJz1xcJRQ6tX9Kgg3G0FezIzq70SK962onCsSzVRaJHVuZCfwGi8Cn94qk63wpMJPSCjpkA6rg== X-Received: by 2002:a17:902:780f:b0:13a:3919:e365 with SMTP id p15-20020a170902780f00b0013a3919e365mr8683713pll.63.1631297933933; Fri, 10 Sep 2021 11:18:53 -0700 (PDT) Received: from hermes.local (204-195-33-123.wavecable.com. [204.195.33.123]) by smtp.gmail.com with ESMTPSA id p13sm5652857pjo.9.2021.09.10.11.18.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 10 Sep 2021 11:18:53 -0700 (PDT) From: Stephen Hemminger To: dev@dpdk.org Cc: Stephen Hemminger Date: Fri, 10 Sep 2021 11:18:38 -0700 Message-Id: <20210910181841.530280-9-stephen@networkplumber.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210910181841.530280-1-stephen@networkplumber.org> References: <20210903004732.109023-1-stephen@networkplumber.org> <20210910181841.530280-1-stephen@networkplumber.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [dpdk-dev] [PATCH v7 08/11] test: add test for bpf_convert 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 Sender: "dev" Add some functional tests for the Classic BPF to DPDK BPF converter. Signed-off-by: Stephen Hemminger --- app/test/test_bpf.c | 148 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 148 insertions(+) diff --git a/app/test/test_bpf.c b/app/test/test_bpf.c index 527c06b80708..565b19653a77 100644 --- a/app/test/test_bpf.c +++ b/app/test/test_bpf.c @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -3233,3 +3234,150 @@ test_bpf(void) } REGISTER_TEST_COMMAND(bpf_autotest, test_bpf); + +#ifdef RTE_PORT_PCAP +#include + +static void +test_bpf_dump(struct bpf_program *cbf, const struct rte_bpf_prm *prm) +{ + printf("cBPF program (%u insns)\n", cbf->bf_len); + bpf_dump(cbf, 1); + + printf("\neBPF program (%u insns)\n", prm->nb_ins); + rte_bpf_dump(stdout, prm->ins, prm->nb_ins); +} + +static int +test_bpf_match(pcap_t *pcap, const char *str, bool expected) +{ + struct bpf_program fcode; + struct rte_bpf_prm *prm = NULL; + struct rte_bpf *bpf = NULL; + uint8_t tbuf[sizeof(struct dummy_mbuf)]; + int ret = -1; + uint64_t rc; + + if (pcap_compile(pcap, &fcode, str, 1, PCAP_NETMASK_UNKNOWN)) { + printf("%s@%d: pcap_compile(\"%s\") failed: %s;\n", + __func__, __LINE__, str, pcap_geterr(pcap)); + return -1; + } + + prm = rte_bpf_convert(&fcode); + if (prm == NULL) { + printf("%s@%d: bpf_convert('%s') failed,, error=%d(%s);\n", + __func__, __LINE__, str, rte_errno, strerror(rte_errno)); + goto error; + } + + bpf = rte_bpf_load(prm); + if (bpf == NULL) { + printf("%s@%d: failed to load bpf code, error=%d(%s);\n", + __func__, __LINE__, rte_errno, strerror(rte_errno)); + goto error; + } + + test_ld_mbuf1_prepare(tbuf); + rc = rte_bpf_exec(bpf, tbuf); + if ((rc == 0) == expected) + ret = 0; + else + printf("%s@%d: failed match: expect %s 0 got %"PRIu64"\n", + __func__, __LINE__, expected ? "==" : "<>", rc); +error: + if (bpf) + rte_bpf_destroy(bpf); + rte_free(prm); + pcap_freecode(&fcode); + return ret; +} + +/* Basic sanity test can we match a IP packet */ +static int +test_bpf_filter_sanity(pcap_t *pcap) +{ + int ret; + + ret = test_bpf_match(pcap, "ip", true); + ret |= test_bpf_match(pcap, "not ip", false); + + return ret; +} + +/* Some sample pcap filter strings from tcpdump man page */ +static const char * const sample_filters[] = { + "host 192.168.1.100", + "src net 10", + "not stp", + "len = 128", + "ip host 1.1.1.1 and not 1.1.1.2", + "ip and not net 127.0.0.1", + "tcp[tcpflags] & (tcp-syn|tcp-fin) != 0 and not src and dst net 127.0.0.1", + "ether[0] & 1 = 0 and ip[16] >= 224", + "icmp[icmptype] != icmp-echo and icmp[icmptype] != icmp-echoreply", +}; + +static int +test_bpf_filter(pcap_t *pcap, const char *s) +{ + struct bpf_program fcode; + struct rte_bpf_prm *prm = NULL; + struct rte_bpf *bpf = NULL; + + if (pcap_compile(pcap, &fcode, s, 1, PCAP_NETMASK_UNKNOWN)) { + printf("%s@%d: pcap_compile('%s') failed: %s;\n", + __func__, __LINE__, s, pcap_geterr(pcap)); + return -1; + } + + prm = rte_bpf_convert(&fcode); + if (prm == NULL) { + printf("%s@%d: bpf_convert('%s') failed,, error=%d(%s);\n", + __func__, __LINE__, s, rte_errno, strerror(rte_errno)); + goto error; + } + + bpf = rte_bpf_load(prm); + if (bpf == NULL) { + printf("%s@%d: failed to load bpf code, error=%d(%s);\n", + __func__, __LINE__, rte_errno, strerror(rte_errno)); + goto error; + } + +error: + if (bpf) + rte_bpf_destroy(bpf); + else { + printf("%s \"%s\"\n", __func__, s); + test_bpf_dump(&fcode, prm); + } + + rte_free(prm); + pcap_freecode(&fcode); + return (bpf == NULL) ? -1 : 0; +} + +static int +test_bpf_convert(void) +{ + unsigned int i; + pcap_t *pcap; + int rc; + + pcap = pcap_open_dead(DLT_EN10MB, 262144); + if (!pcap) { + printf("pcap_open_dead failed\n"); + return -1; + } + + rc = test_bpf_filter_sanity(pcap); + for (i = 0; i < RTE_DIM(sample_filters); i++) + rc |= test_bpf_filter(pcap, sample_filters[i]); + + pcap_close(pcap); + return rc; +} + +REGISTER_TEST_COMMAND(bpf_convert_autotest, test_bpf_convert); +#endif /* RTE_PORT_PCAP */ -- 2.30.2