From patchwork Mon Jun 8 11:10:01 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Balasubramanian Manoharan X-Patchwork-Id: 49610 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-wg0-f69.google.com (mail-wg0-f69.google.com [74.125.82.69]) by ip-10-151-82-157.ec2.internal (Postfix) with ESMTPS id 9813B21419 for ; Mon, 8 Jun 2015 11:13:10 +0000 (UTC) Received: by wgme6 with SMTP id e6sf31867362wgm.3 for ; Mon, 08 Jun 2015 04:13:09 -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=PhBL5dfuE8+CKy6jbRy/1xHLUXa0ggyCaYL3IKka+EI=; b=KHs/52OYX42Hj4rNzmZ1Sd9QDti8s6g8xBYOFQcpGnyapHDheYOzZv3X664irdTdxw k7itjLeHPq/L4/0tTUcmUC4dLMjuJ8cW60GGFvrrsavuvxTNqWNFUcPiFhbQS285zIDG OabzeX+BA8gx03yZhT6aQFqg/+qAWzzXvfGs0j0XpHw5w/lnupCZ1kDe+02UAFGYy/rg f0B4xzZXwhM9QJ4XQZ+7Avl10wDATAaeANidMgP96nu9cEydn48D7hE5DwxPbyr6XIYH cldFlQgu1n5X7J0mXDQeYmI62K60o/sc1vfVNktH6TnDBzqmYVnK0HUJPwYVyHLj+q0m OKvQ== X-Gm-Message-State: ALoCoQmXU4kgJeiMqO2krZYijSiQxgxw60l5VDgJtXWiWq+1juvGUyjVPEixl4us3XTvivCJE0dw X-Received: by 10.180.90.106 with SMTP id bv10mr12056928wib.6.1433761989889; Mon, 08 Jun 2015 04:13:09 -0700 (PDT) X-BeenThere: patchwork-forward@linaro.org Received: by 10.152.27.10 with SMTP id p10ls679124lag.29.gmail; Mon, 08 Jun 2015 04:13:09 -0700 (PDT) X-Received: by 10.152.206.10 with SMTP id lk10mr13250219lac.90.1433761989698; Mon, 08 Jun 2015 04:13:09 -0700 (PDT) Received: from mail-la0-f46.google.com (mail-la0-f46.google.com. [209.85.215.46]) by mx.google.com with ESMTPS id ws3si2457174lbb.70.2015.06.08.04.13.09 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 08 Jun 2015 04:13:09 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.215.46 as permitted sender) client-ip=209.85.215.46; Received: by labpy14 with SMTP id py14so94433310lab.0 for ; Mon, 08 Jun 2015 04:13:09 -0700 (PDT) X-Received: by 10.112.140.231 with SMTP id rj7mr16130306lbb.76.1433761989507; Mon, 08 Jun 2015 04:13:09 -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.108.230 with SMTP id hn6csp1782927lbb; Mon, 8 Jun 2015 04:13:07 -0700 (PDT) X-Received: by 10.229.16.69 with SMTP id n5mr19570907qca.25.1433761987212; Mon, 08 Jun 2015 04:13:07 -0700 (PDT) Received: from lists.linaro.org (lists.linaro.org. [54.225.227.206]) by mx.google.com with ESMTP id p76si2182565qkp.60.2015.06.08.04.13.06; Mon, 08 Jun 2015 04:13:07 -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 4ADDE61E34; Mon, 8 Jun 2015 11:13:06 +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 B34DE6183A; Mon, 8 Jun 2015 11:13:02 +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 7C09361CEA; Mon, 8 Jun 2015 11:12:58 +0000 (UTC) Received: from mail-pa0-f50.google.com (mail-pa0-f50.google.com [209.85.220.50]) by lists.linaro.org (Postfix) with ESMTPS id A2CBF61815 for ; Mon, 8 Jun 2015 11:10:12 +0000 (UTC) Received: by padev16 with SMTP id ev16so38049099pad.0 for ; Mon, 08 Jun 2015 04:10:11 -0700 (PDT) X-Received: by 10.70.61.167 with SMTP id q7mr28284627pdr.41.1433761811850; Mon, 08 Jun 2015 04:10:11 -0700 (PDT) Received: from manoharan-Vostro-2520.domain.name ([117.207.78.186]) by mx.google.com with ESMTPSA id gy3sm2271683pbb.42.2015.06.08.04.10.09 (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Mon, 08 Jun 2015 04:10:11 -0700 (PDT) From: bala.manoharan@linaro.org To: lng-odp@lists.linaro.org Date: Mon, 8 Jun 2015 16:40:01 +0530 Message-Id: <1433761801-7130-1-git-send-email-bala.manoharan@linaro.org> X-Mailer: git-send-email 2.0.1.472.g6f92e5f Subject: [lng-odp] [ODP/RFC 2/2] example: egress classifier example 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: bala.manoharan@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.46 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 From: Balasubramanian Manoharan This is a vlan priority based L2 loopback application which creates multiple packet output queues for each vlan priority value and attaches them to the given pktio interface. The packets are enqueued into different packet output queues based on their vlan priority values and the user can configure the CIR, PIR, CBS and PBS values for each of the packet output queues. The user can also set a port level rate limitting value. Signed-off-by: Balasubramanian Manoharan --- Makefile.am | 2 +- configure.ac | 1 + example/Makefile.am | 2 +- example/egress/Makefile.am | 10 + example/egress/odp_egress.c | 702 ++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 715 insertions(+), 2 deletions(-) create mode 100644 example/egress/Makefile.am create mode 100644 example/egress/odp_egress.c diff --git a/Makefile.am b/Makefile.am index b9b2517..72be426 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,4 +1,4 @@ -ACLOCAL_AMFLAGS=-I m4 +ACLOCAL_AMFLAGS=-I m4 -g AUTOMAKE_OPTIONS = foreign SUBDIRS = doc platform example test helper scripts diff --git a/configure.ac b/configure.ac index 89b0846..ce70ca0 100644 --- a/configure.ac +++ b/configure.ac @@ -287,6 +287,7 @@ AC_CONFIG_FILES([Makefile doc/Makefile example/Makefile example/classifier/Makefile + example/egress/Makefile example/generator/Makefile example/ipsec/Makefile example/packet/Makefile diff --git a/example/Makefile.am b/example/Makefile.am index 353f397..6ea51ab 100644 --- a/example/Makefile.am +++ b/example/Makefile.am @@ -1 +1 @@ -SUBDIRS = classifier generator ipsec packet timer +SUBDIRS = classifier egress generator ipsec packet timer diff --git a/example/egress/Makefile.am b/example/egress/Makefile.am new file mode 100644 index 0000000..1d62daa --- /dev/null +++ b/example/egress/Makefile.am @@ -0,0 +1,10 @@ +include $(top_srcdir)/example/Makefile.inc + +bin_PROGRAMS = odp_egress +odp_egress_LDFLAGS = $(AM_LDFLAGS) -static +odp_egress_CFLAGS = $(AM_CFLAGS) -I${top_srcdir}/example + +noinst_HEADERS = \ + $(top_srcdir)/example/example_debug.h + +dist_odp_egress_SOURCES = odp_egress.c diff --git a/example/egress/odp_egress.c b/example/egress/odp_egress.c new file mode 100644 index 0000000..969b1e3 --- /dev/null +++ b/example/egress/odp_egress.c @@ -0,0 +1,702 @@ +/* Copyright (c) 2015, Linaro Limited + * All rights reserved. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +#define _POSIX_C_SOURCE 200112L +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +/** @def MAX_WORKERS + * @brief Maximum number of worker threads + */ +#define MAX_WORKERS 32 + +/** @def SHM_PKT_POOL_SIZE + * @brief Size of the shared memory block + */ +#define SHM_PKT_POOL_SIZE (1024 * 2048) + +/** @def SHM_PKT_POOL_BUF_SIZE + * @brief Buffer size of the packet pool buffer + */ +#define SHM_PKT_POOL_BUF_SIZE 1856 + +/** @def MAX_VLAN_PRIO + * @brief Maximum vlan priority value + */ +#define MAX_VLAN_PRIO 8 + +/** Get rid of path in filename - only for unix-type paths using '/' */ +#define NO_PATH(file_name) (strrchr((file_name), '/') ? \ + strrchr((file_name), '/') + 1 : (file_name)) + +typedef struct { + int appl_mode; /**< application mode */ + int cpu_count; /**< Number of CPUs to use */ + uint32_t time; /**< Number of seconds to run */ + char *if_name; /**< pointer to interface names */ + odp_atomic_u64_t total_packets; /**< total received packets */ + odp_atomic_u64_t drop_packets; /**< packets dropped */ + odp_pktout_queue_t outq[MAX_VLAN_PRIO]; + uint32_t cir_kbps[MAX_VLAN_PRIO]; + uint32_t pir_kbps[MAX_VLAN_PRIO]; + uint32_t cbs_bytes[MAX_VLAN_PRIO]; + uint32_t pbs_bytes[MAX_VLAN_PRIO]; + uint32_t rate_kbps; + uint32_t burst_byte; +} appl_args_t; + +/* helper funcs */ +static int drop_err_pkts(odp_packet_t pkt_tbl[], unsigned len); +static void swap_pkt_addrs(odp_packet_t pkt_tbl[], unsigned len); +static void parse_args(int argc, char *argv[], appl_args_t *appl_args); +static void print_info(char *progname, appl_args_t *appl_args); +static void usage(char *progname); +static int parse_vlan_conf(appl_args_t *appl_args, char *argv[], char *optarg); + +static inline +void statistics(appl_args_t *args) +{ + int i; + uint32_t timeout; + int infinite = 0; + struct timespec t; + + t.tv_sec = 0; + t.tv_nsec = 10 * 1000 * 1000; /* 10 milli second */ + + printf("\n"); + for (i = 0; i < 80; i++) + printf("-"); + printf("\n"); + /* print statistics */ + printf("EGRESS EXAMPLE STATISTICS\n"); + for (i = 0; i < 80; i++) + printf("-"); + printf("\n"); + + printf("PKTOUT_QUEUE\t"); + for (i = 0; i < MAX_VLAN_PRIO; i++) + printf("| Q%d\t", i); + printf("Port\t"); + printf("\n"); + + for (i = 0; i < 80; i++) + printf("-"); + printf("\n"); + printf("VLAN_PRIO\t"); + for (i = 0; i < MAX_VLAN_PRIO; i++) + printf("| %d\t", i); + printf("\n"); + + printf("CIR\t\t"); + for (i = 0; i < MAX_VLAN_PRIO; i++) + printf("| %d\t", args->cir_kbps[i]); + printf("| %d\t", args->rate_kbps); + printf("\n"); + + printf("CBS\t\t"); + for (i = 0; i < MAX_VLAN_PRIO; i++) + printf("| %d\t", args->cbs_bytes[i]); + printf("| %d\t", args->burst_byte); + printf("\n"); + + printf("PIR\t\t"); + for (i = 0; i < MAX_VLAN_PRIO; i++) + printf("| %d\t", args->pir_kbps[i]); + printf("| %d\t", args->rate_kbps); + printf("\n"); + + printf("PBS\t\t"); + for (i = 0; i < MAX_VLAN_PRIO; i++) + printf("| %d\t", args->pbs_bytes[i]); + printf("| %d\t", args->burst_byte); + printf("\n"); + + timeout = args->time; + timeout *= 100; /* nanosleep is for 10 millisecond */ + + /* Incase if default value is given for timeout + run the loop infinitely */ + if (timeout == 0) + infinite = 1; + + for (; timeout > 0 || infinite; timeout--) { + printf("DEPTH\t\t"); + for (i = 0; i < MAX_VLAN_PRIO; i++) + printf("| %04d\t", odp_pktout_queue_packets(args-> + outq[i])); + + printf("TOTAL:\t%" PRIu64 "\t", odp_atomic_load_u64(&args-> + total_packets)); + printf("DROP:\t%" PRIu64 "\t", odp_atomic_load_u64(&args-> + drop_packets)); + + nanosleep(&t, NULL); + + printf("\r"); + fflush(stdout); + } +} + +/** + * Create a pktio handle, optionally associating a default input queue. + * + * @param dev Device name + * @param pool Associated Packet Pool + * + * @return The handle of the created pktio object. + * @retval ODP_PKTIO_INVALID if the create fails. + */ +static odp_pktio_t create_pktio(const char *dev, odp_pool_t pool) +{ + odp_pktio_t pktio; + odp_queue_t inq_def; + odp_queue_param_t qparam; + char inq_name[ODP_QUEUE_NAME_LEN]; + int ret; + + /* Open a packet IO instance */ + pktio = odp_pktio_open(dev, pool); + if (pktio == ODP_PKTIO_INVALID) { + if (odp_errno() == EPERM) + EXAMPLE_ERR("Root level permission required\n"); + + EXAMPLE_ERR("pktio create failed for %s\n", dev); + exit(EXIT_FAILURE); + } + + qparam.sched.prio = ODP_SCHED_PRIO_DEFAULT; + qparam.sched.sync = ODP_SCHED_SYNC_ATOMIC; + qparam.sched.group = ODP_SCHED_GROUP_DEFAULT; + snprintf(inq_name, sizeof(inq_name), "%" PRIu64 "-pktio_inq_def", + odp_pktio_to_u64(pktio)); + inq_name[ODP_QUEUE_NAME_LEN - 1] = '\0'; + + inq_def = odp_queue_create(inq_name, ODP_QUEUE_TYPE_PKTIN, &qparam); + if (inq_def == ODP_QUEUE_INVALID) { + EXAMPLE_ERR("pktio inq create failed for %s\n", dev); + exit(EXIT_FAILURE); + } + + ret = odp_pktio_inq_setdef(pktio, inq_def); + if (ret != 0) { + EXAMPLE_ERR("default input-Q setup for %s\n", dev); + exit(EXIT_FAILURE); + } + + printf(" created pktio:%02" PRIu64 + ", dev:%s, queue mode (ATOMIC queues)\n" + " \tdefault pktio%02" PRIu64 + "-INPUT queue:%" PRIu64 "\n", + odp_pktio_to_u64(pktio), dev, + odp_pktio_to_u64(pktio), odp_queue_to_u64(inq_def)); + + return pktio; +} + +/** + * Worker threads to receive the packet + * + */ +static void *pktio_receive_thread(void *arg) +{ + odp_packet_t pkt; + odp_event_t ev; + unsigned long err_cnt = 0; + odp_queue_t queue; + appl_args_t *appl; + odph_ethhdr_t *eth; + odph_vlanhdr_t *vlan; + int qos; + int ret; + + appl = (appl_args_t *)arg; + /* Init this thread */ + if (odp_init_local()) { + EXAMPLE_ERR("ODP thread local init failed.\n"); + exit(EXIT_FAILURE); + } + + /* Loop packets */ + for (;;) { + /* Use schedule to get buf from any input queue */ + ev = odp_schedule(&queue, ODP_SCHED_WAIT); + + /* Loop back to receive packets incase of invalid event */ + if (odp_unlikely(ev == ODP_EVENT_INVALID)) + continue; + + pkt = odp_packet_from_event(ev); + + /* Total packets received */ + odp_atomic_inc_u64(&appl->total_packets); + + /* Drop packets with errors */ + if (odp_unlikely(drop_err_pkts(&pkt, 1) == 0)) { + EXAMPLE_ERR("Drop frame - err_cnt:%lu\n", ++err_cnt); + continue; + } + + /* Swap Eth MACs and possibly IP-addrs before sending back */ + swap_pkt_addrs(&pkt, 1); + + if (odp_packet_has_vlan(pkt)) { + eth = (odph_ethhdr_t *)odp_packet_l2_ptr(pkt, NULL); + vlan = (odph_vlanhdr_t *)(ð->type); + qos = odp_be_to_cpu_16(vlan->tci); + qos = ((qos >> 13) & 0x07); + qos = 7 - qos; + ret = odp_pktout_enq(appl->outq[qos], pkt); + if (ret < 0) { + /* Packet drop count */ + odp_atomic_inc_u64(&appl->drop_packets); + odp_packet_free(pkt); + } + } else { + /** enqueue into default priority queue */ + ret = odp_pktout_enq(appl->outq[7], pkt); + if (ret < 0) { + /* Packet drop count */ + odp_atomic_inc_u64(&appl->drop_packets); + odp_packet_free(pkt); + } + } + } + + return NULL; +} + +/** + * ODP Egress example main function + */ +int main(int argc, char *argv[]) +{ + odph_linux_pthread_t thread_tbl[MAX_WORKERS]; + odp_pool_t pool; + int num_workers; + int i; + int cpu; + int priority; + odp_cpumask_t cpumask; + char cpumaskstr[ODP_CPUMASK_STR_SIZE]; + odp_pool_param_t params; + odp_pktio_t pktio; + appl_args_t *args; + odp_shm_t shm; + odp_pktout_queue_param_t param; + int num; + odp_pktout_queue_t queue[MAX_PKTOUT_QUEUE]; + + /* Init ODP before calling anything else */ + if (odp_init_global(NULL, NULL)) { + EXAMPLE_ERR("Error: ODP global init failed.\n"); + exit(EXIT_FAILURE); + } + + /* Init this thread */ + if (odp_init_local()) { + EXAMPLE_ERR("Error: ODP local init failed.\n"); + exit(EXIT_FAILURE); + } + + /* Reserve memory for args from shared mem */ + shm = odp_shm_reserve("cls_shm_args", sizeof(appl_args_t), + ODP_CACHE_LINE_SIZE, 0); + + if (shm == ODP_SHM_INVALID) { + EXAMPLE_ERR("Error: shared mem reserve failed.\n"); + exit(EXIT_FAILURE); + } + + args = odp_shm_addr(shm); + + if (!args) { + EXAMPLE_ERR("Error: shared mem alloc failed.\n"); + exit(EXIT_FAILURE); + } + + memset(args, 0, sizeof(*args)); + /* Parse and store the application arguments */ + parse_args(argc, argv, args); + + /* Print both system and application information */ + print_info(NO_PATH(argv[0]), args); + + /* Default to system CPU count unless user specified */ + num_workers = MAX_WORKERS; + if (args->cpu_count) + num_workers = args->cpu_count; + + /* + * By default CPU #0 runs Linux kernel background tasks. + * Start mapping thread from CPU #1 + */ + num_workers = odph_linux_cpumask_default(&cpumask, num_workers); + (void)odp_cpumask_to_str(&cpumask, cpumaskstr, sizeof(cpumaskstr)); + + printf("num worker threads: %i\n", num_workers); + printf("first CPU: %i\n", odp_cpumask_first(&cpumask)); + printf("cpu mask: %s\n", cpumaskstr); + + /* Create packet pool */ + memset(¶ms, 0, sizeof(params)); + params.pkt.seg_len = SHM_PKT_POOL_BUF_SIZE; + params.pkt.len = SHM_PKT_POOL_BUF_SIZE; + params.pkt.num = SHM_PKT_POOL_SIZE / SHM_PKT_POOL_BUF_SIZE; + params.type = ODP_POOL_PACKET; + + pool = odp_pool_create("packet_pool", ODP_SHM_NULL, ¶ms); + + if (pool == ODP_POOL_INVALID) { + EXAMPLE_ERR("Error: packet pool create failed.\n"); + exit(EXIT_FAILURE); + } + + odp_atomic_init_u64(&args->total_packets, 0); + odp_atomic_init_u64(&args->drop_packets, 0); + + /* create pktio per interface */ + pktio = create_pktio(args->if_name, pool); + + /* Create and init worker threads */ + memset(thread_tbl, 0, sizeof(thread_tbl)); + + /* Create packet output queue */ + + param.num = MAX_VLAN_PRIO; + for (i = 0; i < MAX_VLAN_PRIO; i++) + param.prio[i] = i; + + /* todo: Since the current API version only supports only one hierarchy + level, this function creates packet output queues which are + attached at the root level. + Once the hierarchy APIs are finalized in the sprint the same will + be modified */ + num = odp_pktout_queue_create(pktio, param, queue); + + if (num < 0) { + EXAMPLE_ERR("Error: Packet output queue createion failed\n"); + exit(EXIT_FAILURE); + } + + for (i = 0; i < num; i++) { + priority = odp_pktout_queue_priority(queue[i]); + args->outq[priority] = queue[i]; + } + + odp_pktio_rate_set(pktio, args->rate_kbps, args->burst_byte); + + for (i = 0; i < MAX_VLAN_PRIO; i++) { + odp_pktout_cir_set(args->outq[i], args->cir_kbps[i], + args->cbs_bytes[i]); + odp_pktout_pir_set(args->outq[i], args->pir_kbps[i], + args->pbs_bytes[i]); + } + + cpu = odp_cpumask_first(&cpumask); + for (i = 0; i < num_workers; ++i) { + odp_cpumask_t thd_mask; + /* + * Calls odp_thread_create(cpu) for each thread + */ + odp_cpumask_zero(&thd_mask); + odp_cpumask_set(&thd_mask, cpu); + odph_linux_pthread_create(&thread_tbl[i], &thd_mask, + pktio_receive_thread, + args); + cpu = odp_cpumask_next(&cpumask, cpu); + } + + statistics(args); + + free(args->if_name); + odp_shm_free(shm); + printf("Exit\n\n"); + + return 0; +} + +/** + * Drop packets which input parsing marked as containing errors. + * + * Frees packets with error and modifies pkt_tbl[] to only contain packets with + * no detected errors. + * + * @param pkt_tbl Array of packet + * @param len Length of pkt_tbl[] + * + * @return Number of packets with no detected error + */ +static int drop_err_pkts(odp_packet_t pkt_tbl[], unsigned len) +{ + odp_packet_t pkt; + unsigned pkt_cnt = len; + unsigned i, j; + + for (i = 0, j = 0; i < len; ++i) { + pkt = pkt_tbl[i]; + + if (odp_unlikely(odp_packet_has_error(pkt))) { + odp_packet_free(pkt); /* Drop */ + pkt_cnt--; + } else if (odp_unlikely(i != j++)) { + pkt_tbl[j - 1] = pkt; + } + } + + return pkt_cnt; +} + +/** + * Swap eth src<->dst and IP src<->dst addresses + * + * @param pkt_tbl Array of packets + * @param len Length of pkt_tbl[] + */ +static void swap_pkt_addrs(odp_packet_t pkt_tbl[], unsigned len) +{ + odp_packet_t pkt; + odph_ethhdr_t *eth; + odph_ethaddr_t tmp_addr; + odph_ipv4hdr_t *ip; + uint32be_t ip_tmp_addr; /* tmp ip addr */ + unsigned i; + + for (i = 0; i < len; ++i) { + pkt = pkt_tbl[i]; + if (odp_packet_has_eth(pkt)) { + eth = (odph_ethhdr_t *)odp_packet_l2_ptr(pkt, NULL); + + tmp_addr = eth->dst; + eth->dst = eth->src; + eth->src = tmp_addr; + + if (odp_packet_has_ipv4(pkt)) { + /* IPv4 */ + ip = (odph_ipv4hdr_t *) + odp_packet_l3_ptr(pkt, NULL); + + ip_tmp_addr = ip->src_addr; + ip->src_addr = ip->dst_addr; + ip->dst_addr = ip_tmp_addr; + } + } + } +} + +static int parse_vlan_conf(appl_args_t *appl_args, char *argv[], char *optarg) +{ + char *token; + size_t len; + char *vlan_str; + int prio; + + len = strlen(optarg); + len++; + vlan_str = malloc(len); + strcpy(vlan_str, optarg); + + /* vlan priority */ + token = strtok(vlan_str, ":"); + prio = atoi(token); + if (prio > 7) { + EXAMPLE_ERR("Invalid vlan priority"); + usage(argv[0]); + exit(EXIT_FAILURE); + } + /* CIR kbps */ + token = strtok(NULL, ":"); + appl_args->cir_kbps[prio] = atoi(token); + + /* Committed Burst size */ + token = strtok(NULL, ":"); + appl_args->cbs_bytes[prio] = atoi(token); + + /* PIR kbps */ + token = strtok(NULL, ":"); + appl_args->pir_kbps[prio] = atoi(token); + + token = strtok(NULL, ":"); + appl_args->pbs_bytes[prio] = atoi(token); + + free(vlan_str); + return 0; +} + +/** + * Parse and store the command line arguments + * + * @param argc argument count + * @param argv[] argument vector + * @param appl_args Store application arguments here + */ +static void parse_args(int argc, char *argv[], appl_args_t *appl_args) +{ + int opt; + int long_index; + size_t len; + int interface = 0; + int vlan = 0; + int port = 0; + + static struct option longopts[] = { + {"count", required_argument, NULL, 'c'}, + {"interface", required_argument, NULL, 'i'}, /* return 'i' */ + {"vlan", required_argument, NULL, 'v'}, /* return 'v' */ + {"time", required_argument, NULL, 't'}, /* return 't' */ + {"port", required_argument, NULL, 'p'}, /* return 't' */ + {"help", no_argument, NULL, 'h'}, /* return 'h' */ + {NULL, 0, NULL, 0} + }; + + while (1) { + opt = getopt_long(argc, argv, "+c:t:i:v:p:t:h", + longopts, &long_index); + + if (opt == -1) + break; /* No more options */ + + switch (opt) { + case 'c': + appl_args->cpu_count = atoi(optarg); + break; + case 'v': + if (0 > parse_vlan_conf(appl_args, argv, optarg)) + continue; + vlan = 1; + break; + case 'p': + len = strlen(optarg); + char *str = malloc(len); + char *token; + + strcpy(str, optarg); + token = strtok(str, ":"); + appl_args->rate_kbps = atoi(token); + token = strtok(NULL, ":"); + appl_args->burst_byte = atoi(token); + free(str); + port = 1; + break; + case 't': + appl_args->time = atoi(optarg); + break; + case 'i': + len = strlen(optarg); + if (len == 0) { + usage(argv[0]); + exit(EXIT_FAILURE); + } + len += 1; /* add room for '\0' */ + + appl_args->if_name = malloc(len); + if (!appl_args->if_name) { + usage(argv[0]); + exit(EXIT_FAILURE); + } + + strcpy(appl_args->if_name, optarg); + interface = 1; + break; + + case 'h': + usage(argv[0]); + exit(EXIT_SUCCESS); + break; + default: + break; + } + } + + if (!interface || !vlan || !port) { + usage(argv[0]); + exit(EXIT_FAILURE); + } + if (!appl_args->if_name) { + usage(argv[0]); + exit(EXIT_FAILURE); + } + + optind = 1; /* reset 'extern optind' from the getopt lib */ +} + +/** + * Print system and application info + */ +static void print_info(char *progname, appl_args_t *appl_args) +{ + printf("\n" + "ODP system info\n" + "---------------\n" + "ODP API version: %s\n" + "CPU model: %s\n" + "CPU freq (hz): %" PRIu64 "\n" + "Cache line size: %i\n" + "CPU count: %i\n" + "\n", + odp_version_api_str(), odp_sys_cpu_model_str(), + odp_sys_cpu_hz(), odp_sys_cache_line_size(), + odp_cpu_count()); + + printf("Running ODP appl: \"%s\"\n" + "-----------------\n" + "Using IF:%s ", + progname, appl_args->if_name); + printf("\n\n"); + fflush(NULL); +} + +/** + * Prinf usage information + */ +static void usage(char *progname) +{ + printf("\n" + "OpenDataPlane Egress example.\n" + "Usage: %s OPTIONS\n" + " E.g. %s -i eth1 -m 0 -v \"0:10:100:20:200\" \\\n" + "\t\t\t-v \"1:10:100:20:200\" \\\n" + "\t\t\t-v \"2:20:100:40:200\"\\\n" + "\t\t\t-p \"100:2000\"\n" + "\n" + "For the above example configuration the following will be the packet distribution\n" + "\n" + "Mandatory OPTIONS:\n" + " -i, --interface Eth interface\n" + " -v, --vlan ::::\n\n" + "\t \tvlan pcp value. Correct values are from 0 to 7\n" + "\t Committed Information Rate in kbps\n" + "\t \tCommitted Burst Size in bytes\n" + "\t Peak Information Rate in kbps\n" + "\t \tPeak Burst Size in bytes\n\n" + " -p, --port :\n" + "\t \tPort rate in kbps\n" + "\t Port burst size in bytes\n" + "\n" + "Optional OPTIONS\n" + " -c, --count CPU count.\n" + " default: CPU core count.\n" + "\n" + " -t, --timeout !0: Time for which the classifier will be run in seconds\n" + " 0: Runs in infinite loop\n" + " default: Runs in infinite loop\n" + "\n" + " -h, --help Display help and exit.\n" + "\n", NO_PATH(progname), NO_PATH(progname) + ); +}