From patchwork Thu Oct 19 14:06:49 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xueming Li X-Patchwork-Id: 30595 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 BAEBD1B22F; Thu, 19 Oct 2017 16:07:34 +0200 (CEST) Received: from mellanox.co.il (mail-il-dmz.mellanox.com [193.47.165.129]) by dpdk.org (Postfix) with ESMTP id 683B81B22D for ; Thu, 19 Oct 2017 16:07:33 +0200 (CEST) Received: from Internal Mail-Server by MTLPINE1 (envelope-from xuemingl@mellanox.com) with ESMTPS (AES256-SHA encrypted); 19 Oct 2017 16:07:32 +0200 Received: from dev-r630-06.mtbc.labs.mlnx (dev-r630-06.mtbc.labs.mlnx [10.12.205.180]) by labmailer.mlnx (8.13.8/8.13.8) with ESMTP id v9JE7VKR013099; Thu, 19 Oct 2017 17:07:32 +0300 Received: from dev-r630-06.mtbc.labs.mlnx (localhost [127.0.0.1]) by dev-r630-06.mtbc.labs.mlnx (8.14.7/8.14.7) with ESMTP id v9JE7UHD026724; Thu, 19 Oct 2017 22:07:30 +0800 Received: (from xuemingl@localhost) by dev-r630-06.mtbc.labs.mlnx (8.14.7/8.14.7/Submit) id v9JE7UaR026723; Thu, 19 Oct 2017 22:07:30 +0800 From: Xueming Li To: Jingjing Wu Cc: Xueming Li , dev@dpdk.org Date: Thu, 19 Oct 2017 22:06:49 +0800 Message-Id: <20171019140649.26668-3-xuemingl@mellanox.com> X-Mailer: git-send-email 2.13.3 In-Reply-To: <20171019140649.26668-1-xuemingl@mellanox.com> References: <20171019140649.26668-1-xuemingl@mellanox.com> Subject: [dpdk-dev] [RFC PATCH 2/2] app/testpmd: add scapy command as pkt template 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" Invoke scapy to generate packet templates for each queue of specific port, command format: scapy Example: scapy 0 Ether()/IP()/GRE()/IP()/UDP(dport=(2,5))/"cool" Signed-off-by: Xueming Li --- app/test-pmd/Makefile | 5 +++ app/test-pmd/cmdline.c | 99 +++++++++++++++++++++++++++++++++++++++++++++++++- config/common_base | 2 + 3 files changed, 105 insertions(+), 1 deletion(-) diff --git a/app/test-pmd/Makefile b/app/test-pmd/Makefile index 2c50f68a9..220be17f2 100644 --- a/app/test-pmd/Makefile +++ b/app/test-pmd/Makefile @@ -91,6 +91,11 @@ endif endif +ifeq ($(CONFIG_RTE_TEST_PMD_SCAPY),y) +LDLIBS += -l$(CONFIG_RTE_PYTHON) +CFLAGS += -I/usr/include/$(CONFIG_RTE_PYTHON) +endif + CFLAGS_cmdline.o := -D_GNU_SOURCE include $(RTE_SDK)/mk/rte.app.mk diff --git a/app/test-pmd/cmdline.c b/app/test-pmd/cmdline.c index bb01e989a..b6df7ea75 100644 --- a/app/test-pmd/cmdline.c +++ b/app/test-pmd/cmdline.c @@ -33,6 +33,9 @@ */ #include +#ifdef RTE_TEST_PMD_SCAPY +#include +#endif #include #include #include @@ -48,7 +51,6 @@ #endif #endif #include - #include #include @@ -15391,6 +15393,98 @@ cmdline_parse_inst_t cmd_load_from_file = { }, }; +#ifdef RTE_TEST_PMD_SCAPY +/* Common result structure for file commands */ +struct cmd_scapy_result { + cmdline_fixed_string_t scapy; + portid_t port; + cmdline_fixed_string_t pattern; +}; + +/* Common CLI fields for file commands */ +cmdline_parse_token_string_t cmd_scapy_cmd = + TOKEN_STRING_INITIALIZER(struct cmd_scapy_result, scapy, "scapy"); +cmdline_parse_token_num_t cmd_scapy_port = + TOKEN_NUM_INITIALIZER(struct cmd_scapy_result, port, UINT16); +cmdline_parse_token_string_t cmd_scapy_pattern = + TOKEN_STRING_INITIALIZER(struct cmd_scapy_result, pattern, NULL); + +static void +cmd_scapy_parsed( + void *parsed_result, + __attribute__((unused)) struct cmdline *cl, + __attribute__((unused)) void *data) +{ + struct cmd_scapy_result *res = parsed_result; + PyObject *mod, *gdict, *ldict, *list, *item; + int i, socket; + int len = 0; + struct rte_mbuf *mbuf; + char str[sizeof(res->pattern) + 128]; + + socket = rte_eth_dev_socket_id(res->port); + /* Clean port templates */ + for (i = 0; i < RTE_MAX_QUEUES_PER_PORT; i++) + if (pkt_templ[res->port][i]) + rte_mbuf_raw_free(pkt_templ[res->port][i]); + /* Invoke scapy */ + Py_Initialize(); + if (!Py_IsInitialized()) + goto err; + if (PyRun_SimpleString("import sys;from scapy.all import *")) + goto err; + mod = PyImport_AddModule("__main__"); + if (!mod) + goto err; + gdict = PyModule_GetDict(mod); + ldict = PyDict_New(); + if (!gdict || !ldict) + goto err; + snprintf(str, sizeof(str), "p = %s;r = [str(x) for x in p];\n", + res->pattern); + PyRun_String(str, Py_file_input, gdict, ldict); + list = PyDict_GetItem(ldict, PyString_FromString("r")); + if (!list) + goto err; + for (i = 0; i < PyList_Size(list) && i < RTE_MAX_QUEUES_PER_PORT; i++) { + item = PyList_GET_ITEM(list, i); + /* allocate mbuf & copy raw pkt */ + mbuf = rte_mbuf_raw_alloc(mbuf_pool_find(socket)); + if (!mbuf) + goto end; + rte_memcpy(rte_pktmbuf_mtod_offset(mbuf, void *, 0), + PyString_AsString(item), + PyString_Size(item)); + len = PyString_Size(item); + mbuf->data_len = len > TXONLY_DEF_PACKET_LEN ? + len : TXONLY_DEF_PACKET_LEN; + pkt_templ[res->port][i] = mbuf; + } + if (len > TXONLY_DEF_PACKET_LEN) + printf("%d templates saved, size: %d\n", i, len); + else + printf("%d templates saved, size: %d -> %d\n", i, len, + TXONLY_DEF_PACKET_LEN); + goto end; +err: + printf("Error!\n"); +end: + Py_Finalize(); +} + +cmdline_parse_inst_t cmd_scapy = { + .f = cmd_scapy_parsed, + .data = NULL, + .help_str = "scapy Ether()/IP()/GRE()/IP()/UDP(dport=(2,5))/\"cool\"", + .tokens = { + (void *)&cmd_scapy_cmd, + (void *)&cmd_scapy_port, + (void *)&cmd_scapy_pattern, + NULL, + }, +}; +#endif + /* ******************************************************************************** */ /* list of instructions */ @@ -15399,6 +15493,9 @@ cmdline_parse_ctx_t main_ctx[] = { (cmdline_parse_inst_t *)&cmd_help_long, (cmdline_parse_inst_t *)&cmd_quit, (cmdline_parse_inst_t *)&cmd_load_from_file, +#ifdef RTE_TEST_PMD_SCAPY + (cmdline_parse_inst_t *)&cmd_scapy, +#endif (cmdline_parse_inst_t *)&cmd_showport, (cmdline_parse_inst_t *)&cmd_showqueue, (cmdline_parse_inst_t *)&cmd_showportall, diff --git a/config/common_base b/config/common_base index d9471e806..4abb6f2e1 100644 --- a/config/common_base +++ b/config/common_base @@ -785,6 +785,8 @@ CONFIG_RTE_APP_TEST_RESOURCE_TAR=n CONFIG_RTE_TEST_PMD=y CONFIG_RTE_TEST_PMD_RECORD_CORE_CYCLES=n CONFIG_RTE_TEST_PMD_RECORD_BURST_STATS=n +CONFIG_RTE_TEST_PMD_SCAPY=y +CONFIG_RTE_PYTHON=python2.7 # # Compile the crypto performance application