From patchwork Thu Mar 29 18:31:45 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jasvinder Singh X-Patchwork-Id: 36713 Return-Path: X-Original-To: patchwork@dpdk.org Delivered-To: patchwork@dpdk.org Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id B8B39AAD6; Thu, 29 Mar 2018 20:33:41 +0200 (CEST) Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by dpdk.org (Postfix) with ESMTP id 1CD562C01 for ; Thu, 29 Mar 2018 20:32:50 +0200 (CEST) X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga005.fm.intel.com ([10.253.24.32]) by fmsmga105.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 29 Mar 2018 11:32:50 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.48,378,1517904000"; d="scan'208";a="216059069" Received: from silpixa00381635.ir.intel.com (HELO silpixa00381635.ger.corp.intel.com) ([10.237.222.149]) by fmsmga005.fm.intel.com with ESMTP; 29 Mar 2018 11:32:49 -0700 From: Jasvinder Singh To: dev@dpdk.org Cc: cristian.dumitrescu@intel.com, Kevin Laatz Date: Thu, 29 Mar 2018 19:31:45 +0100 Message-Id: <20180329183208.103844-27-jasvinder.singh@intel.com> X-Mailer: git-send-email 2.9.3 In-Reply-To: <20180329183208.103844-1-jasvinder.singh@intel.com> References: <20180316175906.159198-2-jasvinder.singh@intel.com> <20180329183208.103844-1-jasvinder.singh@intel.com> Subject: [dpdk-dev] [PATCH v4 26/49] ip_pipeline: add tap object X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 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 tap object implementation to the application Signed-off-by: Cristian Dumitrescu Signed-off-by: Jasvinder Singh Signed-off-by: Kevin Laatz --- examples/ip_pipeline/Makefile | 1 + examples/ip_pipeline/cli.c | 32 +++++++++++++ examples/ip_pipeline/main.c | 8 ++++ examples/ip_pipeline/meson.build | 1 + examples/ip_pipeline/tap.c | 97 ++++++++++++++++++++++++++++++++++++++++ examples/ip_pipeline/tap.h | 29 ++++++++++++ 6 files changed, 168 insertions(+) create mode 100644 examples/ip_pipeline/tap.c create mode 100644 examples/ip_pipeline/tap.h diff --git a/examples/ip_pipeline/Makefile b/examples/ip_pipeline/Makefile index 35e4302..0f6bb78 100644 --- a/examples/ip_pipeline/Makefile +++ b/examples/ip_pipeline/Makefile @@ -12,6 +12,7 @@ SRCS-y += main.c SRCS-y += mempool.c SRCS-y += parser.c SRCS-y += swq.c +SRCS-y += tap.c SRCS-y += tmgr.c #SRCS-y += thread.c diff --git a/examples/ip_pipeline/cli.c b/examples/ip_pipeline/cli.c index fd80ba5..bcc87f2 100644 --- a/examples/ip_pipeline/cli.c +++ b/examples/ip_pipeline/cli.c @@ -14,6 +14,7 @@ #include "mempool.h" #include "parser.h" #include "swq.h" +#include "tap.h" #include "tmgr.h" #ifndef CMD_MAX_TOKENS @@ -603,6 +604,32 @@ cmd_tmgr_subport_pipe(char **tokens, } } +/** + * tap + */ +static void +cmd_tap(char **tokens, + uint32_t n_tokens, + char *out, + size_t out_size) +{ + char *name; + struct tap *tap; + + if (n_tokens != 2) { + snprintf(out, out_size, MSG_ARG_MISMATCH, tokens[0]); + return; + } + + name = tokens[1]; + + tap = tap_create(name); + if (tap == NULL) { + snprintf(out, out_size, MSG_CMD_FAIL, tokens[0]); + return; + } +} + void cli_process(char *in, char *out, size_t out_size) { @@ -671,6 +698,11 @@ cli_process(char *in, char *out, size_t out_size) return; } + if (strcmp(tokens[0], "tap") == 0) { + cmd_tap(tokens, n_tokens, out, out_size); + return; + } + snprintf(out, out_size, MSG_CMD_UNKNOWN, tokens[0]); } diff --git a/examples/ip_pipeline/main.c b/examples/ip_pipeline/main.c index 490991f..33c3354 100644 --- a/examples/ip_pipeline/main.c +++ b/examples/ip_pipeline/main.c @@ -15,6 +15,7 @@ #include "link.h" #include "mempool.h" #include "swq.h" +#include "tap.h" #include "tmgr.h" static const char usage[] = @@ -191,6 +192,13 @@ main(int argc, char **argv) return status; } + /* TAP */ + status = tap_init(); + if (status) { + printf("Error: TAP initialization failed (%d)\n", status); + return status; + } + /* Script */ if (app.script_name) cli_script_process(app.script_name, diff --git a/examples/ip_pipeline/meson.build b/examples/ip_pipeline/meson.build index cb2154c..e875811 100644 --- a/examples/ip_pipeline/meson.build +++ b/examples/ip_pipeline/meson.build @@ -15,5 +15,6 @@ sources = files( 'mempool.c', 'parser.c', 'swq.c', + 'tap.c', 'tmgr.c' ) diff --git a/examples/ip_pipeline/tap.c b/examples/ip_pipeline/tap.c new file mode 100644 index 0000000..5b34032 --- /dev/null +++ b/examples/ip_pipeline/tap.c @@ -0,0 +1,97 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * Copyright(c) 2010-2018 Intel Corporation + */ + +#include +#ifdef RTE_EXEC_ENV_LINUXAPP +#include +#include +#endif +#include + +#include +#include +#include +#include +#include + +#include "tap.h" + +#define TAP_DEV "/dev/net/tun" + +static struct tap_list tap_list; + +int +tap_init(void) +{ + TAILQ_INIT(&tap_list); + + return 0; +} + +struct tap * +tap_find(const char *name) +{ + struct tap *tap; + + if (name == NULL) + return NULL; + + TAILQ_FOREACH(tap, &tap_list, node) + if (strcmp(tap->name, name) == 0) + return tap; + + return NULL; +} + +#ifndef RTE_EXEC_ENV_LINUXAPP + +struct tap * +tap_create(const char *name __rte_unused) +{ + return NULL; +} + +#else + +struct tap * +tap_create(const char *name) +{ + struct tap *tap; + struct ifreq ifr; + int fd, status; + + /* Check input params */ + if ((name == NULL) || + tap_find(name)) + return NULL; + + /* Resource create */ + fd = open(TAP_DEV, O_RDWR | O_NONBLOCK); + if (fd < 0) + return NULL; + + memset(&ifr, 0, sizeof(ifr)); + ifr.ifr_flags = IFF_TAP | IFF_NO_PI; /* No packet information */ + snprintf(ifr.ifr_name, IFNAMSIZ, "%s", name); + + status = ioctl(fd, TUNSETIFF, (void *) &ifr); + if (status < 0) + return NULL; + + /* Node allocation */ + tap = calloc(1, sizeof(struct tap)); + if (tap == NULL) + return NULL; + + /* Node fill in */ + strncpy(tap->name, name, sizeof(tap->name)); + tap->fd = fd; + + /* Node add to list */ + TAILQ_INSERT_TAIL(&tap_list, tap, node); + + return tap; +} + +#endif diff --git a/examples/ip_pipeline/tap.h b/examples/ip_pipeline/tap.h new file mode 100644 index 0000000..0dce72f --- /dev/null +++ b/examples/ip_pipeline/tap.h @@ -0,0 +1,29 @@ +/* SPDX-License-Identifier: BSD-3-Clause + * Copyright(c) 2010-2018 Intel Corporation + */ + +#ifndef _INCLUDE_TAP_H_ +#define _INCLUDE_TAP_H_ + +#include + +#include "common.h" + +struct tap { + TAILQ_ENTRY(tap) node; + char name[NAME_SIZE]; + int fd; +}; + +TAILQ_HEAD(tap_list, tap); + +int +tap_init(void); + +struct tap * +tap_find(const char *name); + +struct tap * +tap_create(const char *name); + +#endif /* _INCLUDE_TAP_H_ */