From patchwork Mon Jun 15 08:41:30 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sarosh Arif X-Patchwork-Id: 71592 Return-Path: X-Original-To: patchwork@inbox.dpdk.org Delivered-To: patchwork@inbox.dpdk.org Received: from dpdk.org (dpdk.org [92.243.14.124]) by inbox.dpdk.org (Postfix) with ESMTP id 9743BA0093; Mon, 15 Jun 2020 10:44:28 +0200 (CEST) Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id 1F9804C89; Mon, 15 Jun 2020 10:44:28 +0200 (CEST) Received: from mail-wm1-f65.google.com (mail-wm1-f65.google.com [209.85.128.65]) by dpdk.org (Postfix) with ESMTP id 7B4D72B87 for ; Mon, 15 Jun 2020 10:44:26 +0200 (CEST) Received: by mail-wm1-f65.google.com with SMTP id j198so13589488wmj.0 for ; Mon, 15 Jun 2020 01:44:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=emumba-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id; bh=rDIiZ/9/E+RK+q2L74vX/KqE7ibGWiWvc5dcWl5BiwI=; b=PSkFbz+meBkl7nwrIxSLEFiIc+f5jGBTBzAMs7y4mbkdzxH9HAWWEVOJOW4edmDlto aGc9B7ibcthvR7iMRibwmQY1GpYF3ZkxGDFUUrOH3d5dUnEude+k7s+ATV+n8dZOCw6W XuUJ3p7Tlg2Lv3KQXbxc4AAW8tVq/cYarTDXLTaxU7Q/CIlBccFDDCMKlkSwQUi8R6K8 XiLs0dYvn1Bj5GPf4xU6q9Y/4Zc/9qBKZ8KOtAma/RJPtBVf2iXrIKLhZUZdRCJN+zAa Iv9dAlRricvKqupTmN8yjW4YLnS0EmfXC/3vF4K8EcOAN0Cllmo4BrBqmDiRnR8fNJdv 8t8w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=rDIiZ/9/E+RK+q2L74vX/KqE7ibGWiWvc5dcWl5BiwI=; b=dldl4jAi+aOpDPFnuCmMlweCLQDc2MQq0V718A+zZMG7APN7XVSLGU9rJ0Fsg95gMP OX3gH4w8BwCzvAM27Wqn1GHOiqJHOJ1RQxkIRhiRwodhNk0Co8n2gTI5dXuaWXBVK8lD 1NV80AOq1XKdFn7JEBeGLXRjZAkF/MpP56Ds+DCiSEcmkF0G8aC8Qpsj7Q6LZ7iR513m Tz08vwqZ7sTRFw0rLamC/nmfFhZOOB9yAuNvlbhumTIcxFddIQ3UJQ7lqQ+wvXk5uJcn vK7faDE1Qw0pSZx1Yt6peX0xkHjTYv9v/OZwzdfxEvmw/MOxuTODK3MjdTcsUCdayesX X4Xw== X-Gm-Message-State: AOAM531eieBrNIzKm3R/ELpnwsKaXQsT8cwYnmMG8SV+Ew1Uf0JUIIdH 0QBi3BQ5V6OoAcpndWcrtfUaFh3n0LTqi/Nn X-Google-Smtp-Source: ABdhPJwyfs4ZQuLv7VuK0ZjHKPRhXYuM2mwcFIYjfPuQOfsU3pGa9WNRgh6eOh6nNfqlb98uNZecaQ== X-Received: by 2002:a05:600c:2259:: with SMTP id a25mr12456516wmm.32.1592210665924; Mon, 15 Jun 2020 01:44:25 -0700 (PDT) Received: from localhost.localdomain ([39.40.31.151]) by smtp.gmail.com with ESMTPSA id 67sm24266075wrk.49.2020.06.15.01.44.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 15 Jun 2020 01:44:25 -0700 (PDT) From: Sarosh Arif To: dev@dpdk.org, bruce.richardson@intel.com, john.mcnamara@intel.com, reshma.pattan@intel.com, vipin.varghese@intel.com Cc: stable@dpdk.org, Sarosh Arif Date: Mon, 15 Jun 2020 13:41:30 +0500 Message-Id: <20200615084132.9553-1-sarosh.arif@emumba.com> X-Mailer: git-send-email 2.17.1 Subject: [dpdk-dev] [PATCH 1/3] examples/packet_ordering: free resources on exit 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" Resources should be cleared while exiting the application. Bugzilla ID: 437 Signed-off-by: Sarosh Arif --- examples/packet_ordering/main.c | 51 ++++++++++++++++++++++++++------- 1 file changed, 41 insertions(+), 10 deletions(-) diff --git a/examples/packet_ordering/main.c b/examples/packet_ordering/main.c index b5fc6c54b..49326d015 100644 --- a/examples/packet_ordering/main.c +++ b/examples/packet_ordering/main.c @@ -653,6 +653,17 @@ tx_thread(struct rte_ring *ring_in) return 0; } +static void +stop_and_close_eth_dev(uint16_t port_id) +{ + RTE_ETH_FOREACH_DEV(port_id) { + printf("Closing port %d...", port_id); + rte_eth_dev_stop(port_id); + rte_eth_dev_close(port_id); + printf(" Done\n"); + } +} + int main(int argc, char **argv) { @@ -683,25 +694,31 @@ main(int argc, char **argv) rte_exit(EXIT_FAILURE, "Invalid packet_ordering arguments\n"); /* Check if we have enought cores */ - if (rte_lcore_count() < 3) + if (rte_lcore_count() < 3) { + stop_and_close_eth_dev(port_id); rte_exit(EXIT_FAILURE, "Error, This application needs at " "least 3 logical cores to run:\n" "1 lcore for packet RX\n" "1 lcore for packet TX\n" "and at least 1 lcore for worker threads\n"); + } nb_ports = rte_eth_dev_count_avail(); if (nb_ports == 0) - rte_exit(EXIT_FAILURE, "Error: no ethernet ports detected\n"); - if (nb_ports != 1 && (nb_ports & 1)) + rte_exit(EXIT_FAILURE, "Error: no ethernet ports detected\n"); + if (nb_ports != 1 && (nb_ports & 1)) { + stop_and_close_eth_dev(port_id); rte_exit(EXIT_FAILURE, "Error: number of ports must be even, except " "when using a single port\n"); + } mbuf_pool = rte_pktmbuf_pool_create("mbuf_pool", MBUF_PER_POOL, MBUF_POOL_CACHE_SIZE, 0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id()); - if (mbuf_pool == NULL) + if (mbuf_pool == NULL) { + stop_and_close_eth_dev(port_id); rte_exit(EXIT_FAILURE, "%s\n", rte_strerror(rte_errno)); + } nb_ports_available = nb_ports; @@ -716,12 +733,15 @@ main(int argc, char **argv) /* init port */ printf("Initializing port %u... done\n", port_id); - if (configure_eth_port(port_id) != 0) + if (configure_eth_port(port_id) != 0) { + stop_and_close_eth_dev(port_id); rte_exit(EXIT_FAILURE, "Cannot initialize port %"PRIu8"\n", port_id); + } } if (!nb_ports_available) { + stop_and_close_eth_dev(port_id); rte_exit(EXIT_FAILURE, "All available ports are disabled. Please set portmask.\n"); } @@ -729,19 +749,25 @@ main(int argc, char **argv) /* Create rings for inter core communication */ rx_to_workers = rte_ring_create("rx_to_workers", RING_SIZE, rte_socket_id(), RING_F_SP_ENQ); - if (rx_to_workers == NULL) + if (rx_to_workers == NULL) { + stop_and_close_eth_dev(port_id); rte_exit(EXIT_FAILURE, "%s\n", rte_strerror(rte_errno)); + } workers_to_tx = rte_ring_create("workers_to_tx", RING_SIZE, rte_socket_id(), RING_F_SC_DEQ); - if (workers_to_tx == NULL) + if (workers_to_tx == NULL) { + stop_and_close_eth_dev(port_id); rte_exit(EXIT_FAILURE, "%s\n", rte_strerror(rte_errno)); + } if (!disable_reorder) { send_args.buffer = rte_reorder_create("PKT_RO", rte_socket_id(), REORDER_BUFFER_SIZE); - if (send_args.buffer == NULL) + if (send_args.buffer == NULL) { + stop_and_close_eth_dev(port_id); rte_exit(EXIT_FAILURE, "%s\n", rte_strerror(rte_errno)); + } } last_lcore_id = get_last_lcore_id(); @@ -769,12 +795,17 @@ main(int argc, char **argv) /* Start rx_thread() on the master core */ rx_thread(rx_to_workers); - + RTE_LCORE_FOREACH_SLAVE(lcore_id) { - if (rte_eal_wait_lcore(lcore_id) < 0) + if (rte_eal_wait_lcore(lcore_id) < 0) { + stop_and_close_eth_dev(port_id); + rte_eal_cleanup(); return -1; + } } print_stats(); + stop_and_close_eth_dev(port_id); + rte_eal_cleanup(); return 0; } From patchwork Mon Jun 15 08:41:31 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sarosh Arif X-Patchwork-Id: 71593 Return-Path: X-Original-To: patchwork@inbox.dpdk.org Delivered-To: patchwork@inbox.dpdk.org Received: from dpdk.org (dpdk.org [92.243.14.124]) by inbox.dpdk.org (Postfix) with ESMTP id E951AA0093; Mon, 15 Jun 2020 10:44:42 +0200 (CEST) Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id C9E8E4F9C; Mon, 15 Jun 2020 10:44:42 +0200 (CEST) Received: from mail-wm1-f67.google.com (mail-wm1-f67.google.com [209.85.128.67]) by dpdk.org (Postfix) with ESMTP id 3D27749E0 for ; Mon, 15 Jun 2020 10:44:42 +0200 (CEST) Received: by mail-wm1-f67.google.com with SMTP id u26so13572994wmn.1 for ; Mon, 15 Jun 2020 01:44:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=emumba-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=ZLMAJiHpQtugHKODH9IswC8E0vE/qCqGaf29TJYSfNg=; b=FsfJxoyD3IVMq0Q9tg11ObT9DHtR3CCD2avoQmaaDZXws5JlmFRgaL00uQYFxTDdf9 NJwFAtbvjeAjxKbP3UKaWKbdMlhwp9G4MrJdtSTp3wOFLl3miAKsQAhCkEnrHDZpMZjH yT9udgI9Wt9DA4D22NetpB+rSd5Tvnt4Ov73UXGusjAze9S2/nfIpzqR+otsU3VvfRuV BwNi9YY/4xPzcSuo0RXw23Afjq8O3bRQafCfW9WTh/uEwbtoSH15GtPZybwl0Ppibo7Q Aa9jJ/AD4rLxrZQJKQMe2V0r10H5UVqsMWiu6DUjrLvuxL7Jz5mMZPGUI0HtBzGDiKBP 3BMQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=ZLMAJiHpQtugHKODH9IswC8E0vE/qCqGaf29TJYSfNg=; b=CpMT4Lhl2mVh4b5MEm1XWW46vuQ05kagdsjbWJvvdkls6jZZo11/MXU5A5jT3JJ/ji jCOtaQUZBOaaEck+ymqOEXzo6Ci8VuI2IBU98pRyHw9U3r5EhdKNsTQh5nDFZ5o/4jbv sInjeY9STmgjwe2qBGRhsa416on9ZVtJoiK4LtKSO3QjZdaABwCzp0svV3JKxhzamWVz f/FxmGnGpXDU1Dh02dDJtIUIyNlFzgAMjeJoSfkr4Rv/CA96I5ec3xhDY3A8l7I9lIJ2 TZLFGt5UCTMkHQhYxy8vhhmtNHNO8CyhKsA8CGE4a+dMIh/wkzLoKlwpvjBRTZ/TTzxw M17w== X-Gm-Message-State: AOAM530xvwAZlCtamkX1+XSh/NQHE4ebhNhZj4cQMlIdStrUNel13Dz3 Hm4IJPep0GaEuQzADnTzveuiXYVNy2Iaieds X-Google-Smtp-Source: ABdhPJz3afn6jCv3iK0j04VHgr8mRPecnUXSqdrah339miHEdoQoiVL/6Bo2yxPwIW2d0x5i+3Dp2Q== X-Received: by 2002:a1c:1f4d:: with SMTP id f74mr11156228wmf.12.1592210681629; Mon, 15 Jun 2020 01:44:41 -0700 (PDT) Received: from localhost.localdomain ([39.40.31.151]) by smtp.gmail.com with ESMTPSA id 67sm24266075wrk.49.2020.06.15.01.44.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 15 Jun 2020 01:44:41 -0700 (PDT) From: Sarosh Arif To: dev@dpdk.org, bruce.richardson@intel.com, john.mcnamara@intel.com, reshma.pattan@intel.com, vipin.varghese@intel.com Cc: stable@dpdk.org, Sarosh Arif Date: Mon, 15 Jun 2020 13:41:31 +0500 Message-Id: <20200615084132.9553-2-sarosh.arif@emumba.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200615084132.9553-1-sarosh.arif@emumba.com> References: <20200615084132.9553-1-sarosh.arif@emumba.com> Subject: [dpdk-dev] [PATCH 2/3] examples/rxtx_callbacks: free resources on exit 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" Resources should be cleared while exiting the application. Bugzilla ID: 437 Signed-off-by: Sarosh Arif --- examples/rxtx_callbacks/main.c | 45 ++++++++++++++++++++++++++++------ 1 file changed, 38 insertions(+), 7 deletions(-) diff --git a/examples/rxtx_callbacks/main.c b/examples/rxtx_callbacks/main.c index 54d124b00..bc594dd20 100644 --- a/examples/rxtx_callbacks/main.c +++ b/examples/rxtx_callbacks/main.c @@ -1,7 +1,7 @@ /* SPDX-License-Identifier: BSD-3-Clause * Copyright(c) 2010-2015 Intel Corporation */ - +#include #include #include #include @@ -34,6 +34,7 @@ static struct { } latency_numbers; int hw_timestamping; +static volatile bool quit_signal; #define TICKS_PER_CYCLE_SHIFT 16 static uint64_t ticks_per_cycle_mult; @@ -215,7 +216,7 @@ port_init(uint16_t port, struct rte_mempool *mbuf_pool) * Main thread that does the work, reading from INPUT_PORT * and writing to OUTPUT_PORT */ -static __rte_noreturn void +static void lcore_main(void) { uint16_t port; @@ -230,7 +231,7 @@ lcore_main(void) printf("\nCore %u forwarding packets. [Ctrl+C to quit]\n", rte_lcore_id()); - for (;;) { + while (!quit_signal) { RTE_ETH_FOREACH_DEV(port) { struct rte_mbuf *bufs[BURST_SIZE]; const uint16_t nb_rx = rte_eth_rx_burst(port, 0, @@ -249,6 +250,24 @@ lcore_main(void) } } +static void +stop_and_close_eth_dev(uint16_t port_id) +{ + RTE_ETH_FOREACH_DEV(port_id) { + printf("Closing port %d...", port_id); + rte_eth_dev_stop(port_id); + rte_eth_dev_close(port_id); + printf(" Done\n"); + } +} + +static void +int_handler(int sig_num) +{ + printf("Exiting on signal %d\n", sig_num); + quit_signal = true; +} + /* Main function, does initialisation and calls the per-lcore functions */ int main(int argc, char *argv[]) @@ -260,7 +279,9 @@ main(int argc, char *argv[]) { NULL, 0, 0, 0 } }; int opt, option_index; - + quit_signal = false; + /* catch ctrl-c so we can clear resources on exit */ + signal(SIGINT, int_handler); /* init EAL */ int ret = rte_eal_init(argc, argv); @@ -278,25 +299,33 @@ main(int argc, char *argv[]) break; default: printf(usage, argv[0]); + stop_and_close_eth_dev(portid); + rte_eal_cleanup(); return -1; } optind = 1; /* reset getopt lib */ nb_ports = rte_eth_dev_count_avail(); - if (nb_ports < 2 || (nb_ports & 1)) + if (nb_ports < 2 || (nb_ports & 1)) { + stop_and_close_eth_dev(portid); rte_exit(EXIT_FAILURE, "Error: number of ports must be even\n"); + } mbuf_pool = rte_pktmbuf_pool_create("MBUF_POOL", NUM_MBUFS * nb_ports, MBUF_CACHE_SIZE, 0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id()); - if (mbuf_pool == NULL) + if (mbuf_pool == NULL) { + stop_and_close_eth_dev(portid); rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n"); + } /* initialize all ports */ RTE_ETH_FOREACH_DEV(portid) - if (port_init(portid, mbuf_pool) != 0) + if (port_init(portid, mbuf_pool) != 0) { + stop_and_close_eth_dev(portid); rte_exit(EXIT_FAILURE, "Cannot init port %"PRIu8"\n", portid); + } if (rte_lcore_count() > 1) printf("\nWARNING: Too much enabled lcores - " @@ -304,5 +333,7 @@ main(int argc, char *argv[]) /* call lcore_main on master core only */ lcore_main(); + stop_and_close_eth_dev(portid); + rte_eal_cleanup(); return 0; } From patchwork Mon Jun 15 08:41:32 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sarosh Arif X-Patchwork-Id: 71594 Return-Path: X-Original-To: patchwork@inbox.dpdk.org Delivered-To: patchwork@inbox.dpdk.org Received: from dpdk.org (dpdk.org [92.243.14.124]) by inbox.dpdk.org (Postfix) with ESMTP id 75E7CA0093; Mon, 15 Jun 2020 10:44:54 +0200 (CEST) Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id 5388C5B3A; Mon, 15 Jun 2020 10:44:54 +0200 (CEST) Received: from mail-wr1-f68.google.com (mail-wr1-f68.google.com [209.85.221.68]) by dpdk.org (Postfix) with ESMTP id 1723A49E0 for ; Mon, 15 Jun 2020 10:44:53 +0200 (CEST) Received: by mail-wr1-f68.google.com with SMTP id l10so16153947wrr.10 for ; Mon, 15 Jun 2020 01:44:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=emumba-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=oV/yICtqN7FzbtSIeLc2BbIi3k+hz5z1xX0T/vS27pc=; b=hlsvMuHnhbWHBy1cHHjxUpaoAYv9cbg8mwFqv71Cs46JbBiofIswD/fetIVGnyAYpi na310wy9WWT9raEKcZjvTisRasLAoJ1XCjVQgzjnj7b22DdDOn/Lbb++9u8zabjq7Kba 1CXfpJf9XMbaz4ja+yeop4UQGO0G3kYwxQbjA/3X7w3Nb05P6KW/g46wASUNg9QxuS5j D6os09aRHobUgdcQEIfzxNwO7EYGcaTnQM4szq9WLklHhn6XXm0cwLLhJFdmqu1FG8xF k9buXvcsNGCfT3/MkZcZRbfo5/mgM4FX2YuZKAxVmNpIwXP0YD7hi8wu9KsazGmk5uJX r3fA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=oV/yICtqN7FzbtSIeLc2BbIi3k+hz5z1xX0T/vS27pc=; b=McnL026hnuKKppAcYi22LF/xzr2UOTbtGEiU4FHZufrfmCVgtbKubWun9cL3sxJqd6 8wIN2Mm89ql/AJL/mXR1Fjm48HKpOPgiUo0Ek1OhZxuZQuIhHcIOBtXv0h1soNsNnac/ ZF0awNy5P9j62tN+erxRhGg95aj5pH1/OFygAE+/h+RZtb59eHvdp8l9/Ptat4xAwJwQ yFSXLouoF6fBXLic1cmg2Kwp8X3AUqh4jLWeDc50p/TJ3SdzcJYO2C/Jy/mrwh1+EjBj JT5FfLtmxk5mxe+s1uITZvE8ZufSalo3E/UjFnlYKq8Zkqu/jqZowZf4QbPcqqHVQmJ3 M4Zg== X-Gm-Message-State: AOAM532kK+4VhQXA+9ezZjk76mVmEleHYxOkLleC9hVdHYN4trp+93CE t11HkJ0/jvwD3Y706PixrwaviIg6ER0p7Iwn X-Google-Smtp-Source: ABdhPJwoCXV21SwkFNMcUiSSc+djN7RdVP6aYLgbtu6hkSj+tSkdhUch4s70uFr7k60sSAvbl09tLg== X-Received: by 2002:adf:f208:: with SMTP id p8mr25057656wro.388.1592210692475; Mon, 15 Jun 2020 01:44:52 -0700 (PDT) Received: from localhost.localdomain ([39.40.31.151]) by smtp.gmail.com with ESMTPSA id 67sm24266075wrk.49.2020.06.15.01.44.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 15 Jun 2020 01:44:52 -0700 (PDT) From: Sarosh Arif To: dev@dpdk.org, bruce.richardson@intel.com, john.mcnamara@intel.com, reshma.pattan@intel.com, vipin.varghese@intel.com Cc: stable@dpdk.org, Sarosh Arif Date: Mon, 15 Jun 2020 13:41:32 +0500 Message-Id: <20200615084132.9553-3-sarosh.arif@emumba.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200615084132.9553-1-sarosh.arif@emumba.com> References: <20200615084132.9553-1-sarosh.arif@emumba.com> Subject: [dpdk-dev] [PATCH 3/3] examples/skeleton: free resources on exit 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" Resources should be cleared while exiting the application. Bugzilla ID: 437 Signed-off-by: Sarosh Arif --- examples/skeleton/basicfwd.c | 39 +++++++++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/examples/skeleton/basicfwd.c b/examples/skeleton/basicfwd.c index 72ba85fa1..3b40e04d7 100644 --- a/examples/skeleton/basicfwd.c +++ b/examples/skeleton/basicfwd.c @@ -9,6 +9,7 @@ #include #include #include +#include #define RX_RING_SIZE 1024 #define TX_RING_SIZE 1024 @@ -17,6 +18,7 @@ #define MBUF_CACHE_SIZE 250 #define BURST_SIZE 32 +static volatile bool force_quit; static const struct rte_eth_conf port_conf_default = { .rxmode = { .max_rx_pkt_len = RTE_ETHER_MAX_LEN, @@ -162,6 +164,26 @@ lcore_main(void) } } +static void +stop_and_close_eth_dev(uint16_t port_id) +{ + RTE_ETH_FOREACH_DEV(port_id) { + printf("Closing port %d...", port_id); + rte_eth_dev_stop(port_id); + rte_eth_dev_close(port_id); + printf(" Done\n"); + } +} + +static void +int_handler(int signum) +{ + printf("\n\nSignal %d received, preparing to exit...\n", + signum); + force_quit = true; + +} + /* * The main function, which does initialization and calls the per-lcore * functions. @@ -173,6 +195,9 @@ main(int argc, char *argv[]) unsigned nb_ports; uint16_t portid; + force_quit = false; + signal(SIGINT, int_handler); + /* Initialize the Environment Abstraction Layer (EAL). */ int ret = rte_eal_init(argc, argv); if (ret < 0) @@ -183,21 +208,27 @@ main(int argc, char *argv[]) /* Check that there is an even number of ports to send/receive on. */ nb_ports = rte_eth_dev_count_avail(); - if (nb_ports < 2 || (nb_ports & 1)) + if (nb_ports < 2 || (nb_ports & 1)) { + stop_and_close_eth_dev(portid); rte_exit(EXIT_FAILURE, "Error: number of ports must be even\n"); + } /* Creates a new mempool in memory to hold the mbufs. */ mbuf_pool = rte_pktmbuf_pool_create("MBUF_POOL", NUM_MBUFS * nb_ports, MBUF_CACHE_SIZE, 0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id()); - if (mbuf_pool == NULL) + if (mbuf_pool == NULL) { + stop_and_close_eth_dev(portid); rte_exit(EXIT_FAILURE, "Cannot create mbuf pool\n"); + } /* Initialize all ports. */ RTE_ETH_FOREACH_DEV(portid) - if (port_init(portid, mbuf_pool) != 0) + if (port_init(portid, mbuf_pool) != 0) { + stop_and_close_eth_dev(portid); rte_exit(EXIT_FAILURE, "Cannot init port %"PRIu16 "\n", portid); + } if (rte_lcore_count() > 1) printf("\nWARNING: Too many lcores enabled. Only 1 used.\n"); @@ -205,5 +236,7 @@ main(int argc, char *argv[]) /* Call lcore_main on the master core only. */ lcore_main(); + stop_and_close_eth_dev(portid); + rte_eal_cleanup(); return 0; }