From patchwork Tue Jul 28 17:54:29 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Stuart Haslam X-Patchwork-Id: 51605 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-lb0-f200.google.com (mail-lb0-f200.google.com [209.85.217.200]) by patches.linaro.org (Postfix) with ESMTPS id 20367214B7 for ; Tue, 28 Jul 2015 17:55:21 +0000 (UTC) Received: by lbcjf8 with SMTP id jf8sf40361342lbc.0 for ; Tue, 28 Jul 2015 10:55:19 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:delivered-to:delivered-to:from:to:date :message-id:subject:precedence:list-id:list-unsubscribe:list-archive :list-post:list-help:list-subscribe:mime-version:content-type :content-transfer-encoding:errors-to:sender:x-original-sender :x-original-authentication-results:mailing-list; bh=DqaEK8oySD6/F9FRpXICazt27e9y27cK3RzXC0MoMIs=; b=DmDqvpCOxWBRhA1TPlUM/sNWjHgokdIDgACpM44CNoRZM3qolOgfKwwDnALY6j0ZWl GfZt1qv2QtIsHkMuKrZbw2cF+VSap1HgZbWMk6MHYmwfW2BXoUI1jteLCPYOXROhkoP/ cuLc8bqKvhCRC9WQ7jxoOcn02KP7SR42k19REJ6L51S3QyGgKE9XJVfzQiYXVaRlryF4 JQh+kMxlVEuZ0uuiUQPb+LaHbyHAAYniPAIZBWZTHw9Uh7HEWsgt+fbQR5TerRNEN4cu cICvn26wrbYm/nxTSDyfh+lkwU4bCuUJyT01jsTGMFH2LqhN8zf7672vp0OD2J+QBcsL p4CQ== X-Gm-Message-State: ALoCoQkOSyYJfCJbWzqTLNvX1CMfWdWiYIfI5NQPvOhGcZA3WagBGIukIMBaHK0KPP6X1sUoehzc X-Received: by 10.152.27.130 with SMTP id t2mr14439833lag.2.1438106119735; Tue, 28 Jul 2015 10:55:19 -0700 (PDT) X-BeenThere: patchwork-forward@linaro.org Received: by 10.152.87.228 with SMTP id bb4ls764140lab.88.gmail; Tue, 28 Jul 2015 10:55:19 -0700 (PDT) X-Received: by 10.152.245.43 with SMTP id xl11mr31453830lac.26.1438106119201; Tue, 28 Jul 2015 10:55:19 -0700 (PDT) Received: from mail-la0-f51.google.com (mail-la0-f51.google.com. [209.85.215.51]) by mx.google.com with ESMTPS id to6si18993113lbb.131.2015.07.28.10.55.19 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 28 Jul 2015 10:55:19 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.215.51 as permitted sender) client-ip=209.85.215.51; Received: by lafd3 with SMTP id d3so63063056laf.1 for ; Tue, 28 Jul 2015 10:55:19 -0700 (PDT) X-Received: by 10.112.220.7 with SMTP id ps7mr33733786lbc.72.1438106118916; Tue, 28 Jul 2015 10:55:18 -0700 (PDT) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patch@linaro.org Received: by 10.112.7.198 with SMTP id l6csp2279927lba; Tue, 28 Jul 2015 10:55:17 -0700 (PDT) X-Received: by 10.55.48.11 with SMTP id w11mr50642528qkw.61.1438106117265; Tue, 28 Jul 2015 10:55:17 -0700 (PDT) Received: from lists.linaro.org (lists.linaro.org. [54.225.227.206]) by mx.google.com with ESMTP id 91si26296078qgo.20.2015.07.28.10.55.15; Tue, 28 Jul 2015 10:55:17 -0700 (PDT) Received-SPF: pass (google.com: domain of lng-odp-bounces@lists.linaro.org designates 54.225.227.206 as permitted sender) client-ip=54.225.227.206; Received: by lists.linaro.org (Postfix, from userid 109) id D9C9D61D91; Tue, 28 Jul 2015 17:55:15 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on ip-10-142-244-252.ec2.internal X-Spam-Level: X-Spam-Status: No, score=-2.6 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, URIBL_BLOCKED autolearn=disabled version=3.4.0 Received: from ip-10-142-244-252.ec2.internal (localhost [127.0.0.1]) by lists.linaro.org (Postfix) with ESMTP id 5958561D55; Tue, 28 Jul 2015 17:55:08 +0000 (UTC) X-Original-To: lng-odp@lists.linaro.org Delivered-To: lng-odp@lists.linaro.org Received: by lists.linaro.org (Postfix, from userid 109) id C1AF961D55; Tue, 28 Jul 2015 17:55:02 +0000 (UTC) Received: from mail-wi0-f173.google.com (mail-wi0-f173.google.com [209.85.212.173]) by lists.linaro.org (Postfix) with ESMTPS id CC27761CB5 for ; Tue, 28 Jul 2015 17:54:59 +0000 (UTC) Received: by wicgb10 with SMTP id gb10so167546859wic.1 for ; Tue, 28 Jul 2015 10:54:59 -0700 (PDT) X-Received: by 10.194.90.171 with SMTP id bx11mr65205807wjb.129.1438106099066; Tue, 28 Jul 2015 10:54:59 -0700 (PDT) Received: from e106441.cambridge.arm.com ([2001:41d0:a:3cb4::abcd]) by smtp.gmail.com with ESMTPSA id k2sm20146872wif.4.2015.07.28.10.54.57 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 28 Jul 2015 10:54:58 -0700 (PDT) From: Stuart Haslam To: lng-odp@lists.linaro.org Date: Tue, 28 Jul 2015 18:54:29 +0100 Message-Id: <1438106069-14958-1-git-send-email-stuart.haslam@linaro.org> X-Mailer: git-send-email 2.1.1 X-Topics: patch Subject: [lng-odp] [RFC PATCH] linux-generc: pktio: add pcap pktio type X-BeenThere: lng-odp@lists.linaro.org X-Mailman-Version: 2.1.16 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: , List-Help: , List-Subscribe: , MIME-Version: 1.0 Errors-To: lng-odp-bounces@lists.linaro.org Sender: "lng-odp" X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: stuart.haslam@linaro.org X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.215.51 as permitted sender) smtp.mail=patch+caf_=patchwork-forward=linaro.org@linaro.org Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org X-Google-Group-Id: 836684582541 Create a new pktio type that allows for reading from and writing to a pcap capture file. This is intended to be used as a simple way of injecting test packets into an application for functional testing and can be used as it is with some of the existing example applications. To open a pcap file the name passed to the odp_pktio_open() call needs to be of the format "pcap:". By default all packets transmitted to a pcap pktio are simply freed, but if the environment variable ODP_PKTIO_PCAP_DUMP is set, transmitted packets will instead be saved to a separate pcap dump file, the name of which is generated from the input filename (test.pcap -> test_out.pcap). MTU, MAC and promiscuous mode APIs aren't implemented. Signed-off-by: Stuart Haslam Reviewed-by: Bill Fischofer --- This is pretty handy for testing, for example with the classifier app; sudo ./odp_classifier -ipcap:test.pcap -p "ODP_PMR_SIP_ADDR:192.168.111.2:FFFFFFFF:queue1" -t 5 configure.ac | 8 + platform/linux-generic/Makefile.am | 1 + .../linux-generic/include/odp_packet_io_internal.h | 12 +- platform/linux-generic/odp_packet_io.c | 8 +- platform/linux-generic/pktio/io_ops.c | 1 + platform/linux-generic/pktio/pcap.c | 191 +++++++++++++++++++++ 6 files changed, 216 insertions(+), 5 deletions(-) create mode 100644 platform/linux-generic/pktio/pcap.c diff --git a/configure.ac b/configure.ac index 2ea1368..2a58ba9 100644 --- a/configure.ac +++ b/configure.ac @@ -265,6 +265,14 @@ AC_CHECK_HEADERS([openssl/des.h openssl/rand.h openssl/hmac.h openssl/evp.h], [] [AC_MSG_ERROR([OpenSSL headers required])]) ########################################################################## +# Check for libpcap availability +########################################################################## +AC_CHECK_LIB([pcap], [pcap_open_offline], [], + [AC_MSG_FAILURE([libpcap libraries required])]) +AC_CHECK_HEADERS([pcap/pcap.h pcap/bpf.h], [], + [AC_MSG_ERROR([pcap headers required])]) + +########################################################################## # Restore old saved variables ########################################################################## LDFLAGS=$OLD_LDFLAGS diff --git a/platform/linux-generic/Makefile.am b/platform/linux-generic/Makefile.am index 4ee781c..4e702af 100644 --- a/platform/linux-generic/Makefile.am +++ b/platform/linux-generic/Makefile.am @@ -142,6 +142,7 @@ __LIB__libodp_la_SOURCES = \ odp_packet_io.c \ pktio/io_ops.c \ pktio/loop.c \ + pktio/pcap.c \ pktio/socket.c \ pktio/socket_mmap.c \ odp_pool.c \ diff --git a/platform/linux-generic/include/odp_packet_io_internal.h b/platform/linux-generic/include/odp_packet_io_internal.h index f230936..5339606 100644 --- a/platform/linux-generic/include/odp_packet_io_internal.h +++ b/platform/linux-generic/include/odp_packet_io_internal.h @@ -28,6 +28,8 @@ extern "C" { #include #include +#define PKTIO_NAME_LEN 64 + /* Forward declaration */ struct pktio_if_ops; @@ -36,6 +38,12 @@ typedef struct { odp_bool_t promisc; /**< promiscuous mode state */ } pkt_loop_t; +typedef struct { + odp_pool_t pool; + void *rx; + void *tx_dump; +} pkt_pcap_t; + struct pktio_entry { const struct pktio_if_ops *ops; /**< Implementation specific methods */ odp_spinlock_t lock; /**< entry spinlock */ @@ -49,9 +57,10 @@ struct pktio_entry { pkt_sock_t pkt_sock; /**< using socket API for IO */ pkt_sock_mmap_t pkt_sock_mmap; /**< using socket mmap * API for IO */ + pkt_pcap_t pkt_pcap; /**< Using pcap for IO */ }; classifier_t cls; /**< classifier linked with this pktio*/ - char name[IF_NAMESIZE]; /**< name of pktio provided to + char name[PKTIO_NAME_LEN]; /**< name of pktio provided to pktio_open() */ }; @@ -106,6 +115,7 @@ extern const pktio_if_ops_t sock_basic_pktio_ops; extern const pktio_if_ops_t sock_mmsg_pktio_ops; extern const pktio_if_ops_t sock_mmap_pktio_ops; extern const pktio_if_ops_t loopback_pktio_ops; +extern const pktio_if_ops_t pcap_pktio_ops; extern const pktio_if_ops_t * const pktio_if_ops[]; #ifdef __cplusplus diff --git a/platform/linux-generic/odp_packet_io.c b/platform/linux-generic/odp_packet_io.c index c159baf..7eeadd5 100644 --- a/platform/linux-generic/odp_packet_io.c +++ b/platform/linux-generic/odp_packet_io.c @@ -187,10 +187,10 @@ static odp_pktio_t setup_pktio_entry(const char *dev, odp_pool_t pool) int ret = -1; int pktio_if; - if (strlen(dev) >= IF_NAMESIZE) { + if (strlen(dev) >= PKTIO_NAME_LEN) { /* ioctl names limitation */ ODP_ERR("pktio name %s is too big, limit is %d bytes\n", - dev, IF_NAMESIZE); + dev, PKTIO_NAME_LEN); return ODP_PKTIO_INVALID; } @@ -220,7 +220,7 @@ static odp_pktio_t setup_pktio_entry(const char *dev, odp_pool_t pool) id = ODP_PKTIO_INVALID; ODP_ERR("Unable to init any I/O type.\n"); } else { - snprintf(pktio_entry->s.name, IF_NAMESIZE, "%s", dev); + snprintf(pktio_entry->s.name, PKTIO_NAME_LEN, "%s", dev); unlock_entry_classifier(pktio_entry); } @@ -285,7 +285,7 @@ odp_pktio_t odp_pktio_lookup(const char *dev) lock_entry(entry); if (!is_free(entry) && - strncmp(entry->s.name, dev, IF_NAMESIZE) == 0) + strncmp(entry->s.name, dev, PKTIO_NAME_LEN) == 0) id = _odp_cast_scalar(odp_pktio_t, i); unlock_entry(entry); diff --git a/platform/linux-generic/pktio/io_ops.c b/platform/linux-generic/pktio/io_ops.c index 6cd3d00..ddcf39e 100644 --- a/platform/linux-generic/pktio/io_ops.c +++ b/platform/linux-generic/pktio/io_ops.c @@ -15,5 +15,6 @@ const pktio_if_ops_t * const pktio_if_ops[] = { &sock_mmap_pktio_ops, &sock_mmsg_pktio_ops, &sock_basic_pktio_ops, + &pcap_pktio_ops, NULL }; diff --git a/platform/linux-generic/pktio/pcap.c b/platform/linux-generic/pktio/pcap.c new file mode 100644 index 0000000..58dcd6f --- /dev/null +++ b/platform/linux-generic/pktio/pcap.c @@ -0,0 +1,191 @@ +/* Copyright (c) 2015, Linaro Limited + * All rights reserved. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +#ifndef _GNU_SOURCE +#define _GNU_SOURCE +#endif + +#include +#include +#include + +#include +#include + +#define PCAP_DUMPFILE_SUFFIX "_out" + +static int _pcapif_output_fname(char *out, const char *in, size_t len) +{ + char tmp[len]; + int ret, i; + + strncpy(tmp, in, sizeof(tmp)); + + i = strlen(tmp); + while (i && tmp[i] != '.') + i--; + + if (i) { + tmp[i] = '\0'; + ret = snprintf(out, len, "%s%s.%s", tmp, + PCAP_DUMPFILE_SUFFIX, tmp + i + 1); + } else { + ret = snprintf(out, len, "%s%s", tmp, PCAP_DUMPFILE_SUFFIX); + } + + return ret; +} + +static int pcapif_init(odp_pktio_t id ODP_UNUSED, pktio_entry_t *pktio_entry, + const char *devname, odp_pool_t pool) +{ + pkt_pcap_t *pkt_pcap = &pktio_entry->s.pkt_pcap; + const char *fname_rx; + char errbuf[PCAP_ERRBUF_SIZE]; + int linktype; + + if (strncmp(devname, "pcap:", 5)) + return -1; + + fname_rx = devname + 5; + + pkt_pcap->rx = pcap_open_offline(fname_rx, errbuf); + if (!pkt_pcap->rx) { + ODP_ERR("PCAP open failure: %s\n", errbuf); + return -1; + } + + pkt_pcap->pool = pool; + + linktype = pcap_datalink(pkt_pcap->rx); + if (linktype != DLT_EN10MB) { + ODP_ERR("Datalink type not supported: %d\n", linktype); + return -1; + } + + /* By default all packets sent to the pktio will just be freed, but if + * the ODP_PKTIO_PCAP_DUMP environment variable is set sent packets will + * instead be saved to a separate output pcap file. */ + if (getenv("ODP_PKTIO_PCAP_DUMP")) { + char fname_tx[PKTIO_NAME_LEN + sizeof(PCAP_DUMPFILE_SUFFIX)]; + + _pcapif_output_fname(fname_tx, fname_rx, sizeof(fname_tx)); + + pkt_pcap->tx_dump = pcap_dump_open(pkt_pcap->rx, fname_tx); + if (!pkt_pcap->tx_dump) { + ODP_ERR("pcap_dump_open failed\n"); + return -1; + } + } + + return 0; +} + +static int pcapif_close(pktio_entry_t *pktio_entry) +{ + pkt_pcap_t *pkt_pcap = &pktio_entry->s.pkt_pcap; + + if (pkt_pcap->rx) + pcap_close(pkt_pcap->rx); + + if (pkt_pcap->tx_dump) + pcap_dump_close(pkt_pcap->tx_dump); + + return 0; +} + +static int pcapif_recv_pkt(pktio_entry_t *pktio_entry, odp_packet_t pkts[], + unsigned len) +{ + unsigned i; + struct pcap_pkthdr *pkt_hdr; + const u_char *pkt_data; + pkt_pcap_t *pkt_pcap = &pktio_entry->s.pkt_pcap; + int nbr = 0; + + for (i = 0; i < len; ++i) { + if (pcap_next_ex(pkt_pcap->rx, &pkt_hdr, &pkt_data) != 1) + break; + + pkts[i] = odp_packet_alloc(pkt_pcap->pool, pkt_hdr->caplen); + if (odp_unlikely(pkts[i] == ODP_PACKET_INVALID)) + break; + + odp_packet_copydata_in(pkts[i], 0, pkt_hdr->caplen, pkt_data); + _odp_packet_reset_parse(pkts[i]); + + nbr++; + } + + return nbr; +} + +static void pcapif_dump_pkt(pkt_pcap_t *pkt_pcap, odp_packet_t pkt) +{ + unsigned char *buf; + struct pcap_pkthdr hdr; + + if (!pkt_pcap->tx_dump) + return; + + hdr.caplen = odp_packet_len(pkt); + hdr.len = hdr.caplen; + gettimeofday(&hdr.ts, NULL); + + buf = malloc(hdr.len); + + if (odp_packet_copydata_out(pkt, 0, hdr.len, buf) == 0) + pcap_dump(pkt_pcap->tx_dump, &hdr, buf); + + free(buf); +} + +static int pcapif_send_pkt(pktio_entry_t *pktio_entry, odp_packet_t pkts[], + unsigned len) +{ + pkt_pcap_t *pkt_pcap = &pktio_entry->s.pkt_pcap; + unsigned i; + + for (i = 0; i < len; ++i) { + pcapif_dump_pkt(pkt_pcap, pkts[i]); + odp_packet_free(pkts[i]); + } + + return len; +} + +static int pcapif_mtu_get(pktio_entry_t *pktio_entry ODP_UNUSED) +{ + return 0; +} + +static int pcapif_mac_addr_get(pktio_entry_t *pktio_entry ODP_UNUSED, + void *mac_addr ODP_UNUSED) +{ + return 0; +} + +static int pcapif_promisc_mode_set(pktio_entry_t *pktio_entry ODP_UNUSED, + odp_bool_t enable ODP_UNUSED) +{ + return 0; +} + +static int pcapif_promisc_mode_get(pktio_entry_t *pktio_entry ODP_UNUSED) +{ + return 0; +} + +const pktio_if_ops_t pcap_pktio_ops = { + .open = pcapif_init, + .close = pcapif_close, + .recv = pcapif_recv_pkt, + .send = pcapif_send_pkt, + .mtu_get = pcapif_mtu_get, + .promisc_mode_set = pcapif_promisc_mode_set, + .promisc_mode_get = pcapif_promisc_mode_get, + .mac_get = pcapif_mac_addr_get +};