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 73C97A0032; Fri, 1 Oct 2021 18:31:47 +0200 (CEST) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id 3495041206; Fri, 1 Oct 2021 18:30:53 +0200 (CEST) Received: from mail-pl1-f179.google.com (mail-pl1-f179.google.com [209.85.214.179]) by mails.dpdk.org (Postfix) with ESMTP id 6649B411C6 for ; Fri, 1 Oct 2021 18:30:50 +0200 (CEST) Received: by mail-pl1-f179.google.com with SMTP id l6so6642274plh.9 for ; Fri, 01 Oct 2021 09:30:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=networkplumber-org.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=pcgId2+2sqML7Vrj5tT2iNlvprP4pU6cOK6bBqJGcow=; b=rYJgEgf+nrug4ZePp6p6NiZZfLqw26YQFs0QH7qOOnu0zU/2Solx+pA6KnM5WGOj+B ail/4NGUwtNSJx3bLkYbEXtUUMSK7ubu9RclK9uTE/e71DpmlCnq63Ppf6mlYN7T9IYn dW8aovUx8sWUztX6nZ4mn3fMJ4/OLF5cypVTZXJv9b0uOmihmDHzQt2oagT34rgKTFYz bAw4AFI2ZhKS2vXzttOkMlpLGuO3X1THWnbzVFf5rfuLQDAO/qY+naCZXAf4yuKdmtzE eRPo0hB/6eELZDThWsXIXKJAz0a6Co2PhDJ3YaTghnYZYivVkz0b4jDO7wFbkwC7GkEZ u1nQ== 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=pcgId2+2sqML7Vrj5tT2iNlvprP4pU6cOK6bBqJGcow=; b=nysO4Lb9/+QhHuboiZWP37gMdvOEPEn3xiUntJCfBgAgRS+sltOBfsfk+PeQTCvHYz 1dzGWN0/5EHoccdKOQOcC4dUC1/cQ/DBnBtO1qHGT6PU+hq/byU95GTFhKe5ZgkVgyyc nYRj1u+aj4ebm5RIGgH0x/Hh8rpIKDAJJbc0tf7r0+3T2+7Aiw+n1FaYO/ODQR5YSqCP S5+wnLnlYwGiuBll2QqJh5osEPNc0RWKQPLDlDX3I6DVCy7qgAvK9fVdLkLJjHXWm7bD B+nR7lc8YS6RMtlb5ct++pW4ViccZG+hjSHoKQCbNZc5XV/oC7GOp73Icn4Zp5VB8Kkc TP+w== X-Gm-Message-State: AOAM532gJfUmsUHx0nJkN/pCCp/lBke5hca5dFrd9wbwd97UWFQI48LW rIDMsvd7Mg3iF9PMOgozYLNuB1bE+ZPDFy7M X-Google-Smtp-Source: ABdhPJxukKy4OGo5cph8A6K/2tNfCqjTD20+U73z1NDcr88hCaQkd0q6FKtx2NeGusmbS8TwNRIwMQ== X-Received: by 2002:a17:90a:30b:: with SMTP id 11mr14163565pje.136.1633105849111; Fri, 01 Oct 2021 09:30:49 -0700 (PDT) Received: from hermes.local (204-195-33-123.wavecable.com. [204.195.33.123]) by smtp.gmail.com with ESMTPSA id u2sm1450432pfi.120.2021.10.01.09.30.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Oct 2021 09:30:48 -0700 (PDT) From: Stephen Hemminger To: dev@dpdk.org Cc: Stephen Hemminger Date: Fri, 1 Oct 2021 09:27:01 -0700 Message-Id: <20211001162705.442298-9-stephen@networkplumber.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20211001162705.442298-1-stephen@networkplumber.org> References: <20210903004732.109023-1-stephen@networkplumber.org> <20211001162705.442298-1-stephen@networkplumber.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [dpdk-dev] [PATCH v12 08/12] 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 | 200 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 200 insertions(+) diff --git a/app/test/test_bpf.c b/app/test/test_bpf.c index 527c06b80708..543a5fd615b2 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,202 @@ 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, + struct rte_mbuf *mb) +{ + struct bpf_program fcode; + struct rte_bpf_prm *prm = NULL; + struct rte_bpf *bpf = NULL; + 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; + } + + rc = rte_bpf_exec(bpf, mb); + /* The return code from bpf capture filter is non-zero if matched */ + ret = (rc == 0); +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) +{ + const uint32_t plen = 100; + struct rte_mbuf mb, *m; + uint8_t tbuf[RTE_MBUF_DEFAULT_BUF_SIZE]; + struct { + struct rte_ether_hdr eth_hdr; + struct rte_ipv4_hdr ip_hdr; + } *hdr; + + dummy_mbuf_prep(&mb, tbuf, sizeof(tbuf), plen); + m = &mb; + + hdr = rte_pktmbuf_mtod(m, typeof(hdr)); + hdr->eth_hdr = (struct rte_ether_hdr) { + .d_addr.addr_bytes = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, + .ether_type = rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4), + }; + hdr->ip_hdr = (struct rte_ipv4_hdr) { + .version_ihl = RTE_IPV4_VHL_DEF, + .total_length = rte_cpu_to_be_16(plen), + .time_to_live = IPDEFTTL, + .next_proto_id = IPPROTO_RAW, + .src_addr = rte_cpu_to_be_32(RTE_IPV4_LOOPBACK), + .dst_addr = rte_cpu_to_be_32(RTE_IPV4_BROADCAST), + }; + + if (test_bpf_match(pcap, "ip", m) != 0) { + printf("%s@%d: filter \"ip\" doesn't match test data\n", + __func__, __LINE__); + return -1; + } + if (test_bpf_match(pcap, "not ip", m) == 0) { + printf("%s@%d: filter \"not ip\" does match test data\n", + __func__, __LINE__); + return -1; + } + + return 0; +} + +/* + * Some sample pcap filter strings from + * https://wiki.wireshark.org/CaptureFilters + */ +static const char * const sample_filters[] = { + "host 172.18.5.4", + "net 192.168.0.0/24", + "src net 192.168.0.0/24", + "src net 192.168.0.0 mask 255.255.255.0", + "dst net 192.168.0.0/24", + "dst net 192.168.0.0 mask 255.255.255.0", + "port 53", + "host dpdk.org and not (port 80 or port 25)", + "host dpdk.org and not port 80 and not port 25", + "port not 53 and not arp", + "(tcp[0:2] > 1500 and tcp[0:2] < 1550) or (tcp[2:2] > 1500 and tcp[2:2] < 1550)", + "ether proto 0x888e", + "ether[0] & 1 = 0 and ip[16] >= 224", + "icmp[icmptype] != icmp-echo and icmp[icmptype] != icmp-echoreply", + "tcp[tcpflags] & (tcp-syn|tcp-fin) != 0 and not src and dst net 127.0.0.1", + "not ether dst 01:80:c2:00:00:0e", + "not broadcast and not multicast", + "dst host ff02::1", + "port 80 and tcp[((tcp[12:1] & 0xf0) >> 2):4] = 0x47455420", + /* Worms */ + "dst port 135 and tcp port 135 and ip[2:2]==48", + "icmp[icmptype]==icmp-echo and ip[2:2]==92 and icmp[8:4]==0xAAAAAAAA", + "dst port 135 or dst port 445 or dst port 1433" + " and tcp[tcpflags] & (tcp-syn) != 0" + " and tcp[tcpflags] & (tcp-ack) = 0 and src net 192.168.0.0/24", + "tcp src port 443 and (tcp[((tcp[12] & 0xF0) >> 4 ) * 4] = 0x18)" + " and (tcp[((tcp[12] & 0xF0) >> 4 ) * 4 + 1] = 0x03)" + " and (tcp[((tcp[12] & 0xF0) >> 4 ) * 4 + 2] < 0x04)" + " and ((ip[2:2] - 4 * (ip[0] & 0x0F) - 4 * ((tcp[12] & 0xF0) >> 4) > 69))", + /* Other */ + "len = 128", +}; + +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