From patchwork Tue May 19 08:54:42 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Muhammad Bilal X-Patchwork-Id: 70418 X-Patchwork-Delegate: thomas@monjalon.net 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 976BBA0093; Tue, 19 May 2020 10:55:36 +0200 (CEST) Received: from [92.243.14.124] (localhost [127.0.0.1]) by dpdk.org (Postfix) with ESMTP id D92791D5C7; Tue, 19 May 2020 10:55:28 +0200 (CEST) Received: from mail-wm1-f66.google.com (mail-wm1-f66.google.com [209.85.128.66]) by dpdk.org (Postfix) with ESMTP id B6EBB1D5C6 for ; Tue, 19 May 2020 10:55:26 +0200 (CEST) Received: by mail-wm1-f66.google.com with SMTP id z4so2195012wmi.2 for ; Tue, 19 May 2020 01:55: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:in-reply-to:references; bh=GIZsNI1UFeNZdhLExs1RMHkvrAHxjtWL5j1i1vKXGo8=; b=zcQHjmglbldsCu4U+1bjJuByw2PfIZHgyFN2YIcpbJ+ddJn5uo58wX6mjxM4tYkYw4 trxEBRHNHZX7HcsOQcEi3jZMhvKRT+SbtoVmgwl9gZEnWMtJpgDzvIyfMszZpzpuN38R arVd/oBWIqzWa6SJdYxNSMeiStqM4dI1DqMGDHEIiD+f9nKr9WWWPrUtLBNogR/bmpJ9 Sm5n5h/5hpEK3gwXyjZYNc51o+3SEZVPq6a7JU7edOqLw2cJq5Z/ukDJ7Af2024GQMGi +UMw0uTquwvbrIVwJ/Cc/eO588AYMolSvhxQm+JCxkix5tOehDWPgtwmeOsFryBcWres aEoQ== 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=GIZsNI1UFeNZdhLExs1RMHkvrAHxjtWL5j1i1vKXGo8=; b=fa3u5DQj4ILFOVHq9qLcHQ0RuCthA+BSgJI22r0lOxRdDPU68cz71i3rbQZfiCt9ve FLSeODl3iSv4QREhq9axvmViOzgyTaQtHRwFGPKPjQsv+tz2sl1yysEKgveS6JojhdUu YooUH+1v3L641HG8qwJwVcRahrX6NwqyjH3lbOKljqGWIRFLbuZAws3hjzuLIlN3OXyL 96PZdO3EM1CRLL9Oaz0tMJ1pJTL5i7zVbB92KDJqHZMf/uMX8NTihpKtgLz5is4GJ4/a P1mEYGk/9jVfDW0XCSj2VOA/moe91Yfj5m2rw1K6Biq7pFrR4iIcSqw+FWyykG1kRLaj kIpQ== X-Gm-Message-State: AOAM531ZVtkQ/vv61FBBTD8L2sGhZvcBKCjvBMeOHK6x2OkxxIJ/fqAp Fd2AE04uirYCJ+xz+hnNNLzHF2NGnuk= X-Google-Smtp-Source: ABdhPJzHAfIjF+rByLK6+OHoTo+WF4bu+ebIBqmX7sW6pKEBtbN+W/mSL3K6Ytmull1fVSxs37ES1g== X-Received: by 2002:a7b:cd84:: with SMTP id y4mr4129086wmj.2.1589878526471; Tue, 19 May 2020 01:55:26 -0700 (PDT) Received: from localhost.localdomain ([111.119.188.30]) by smtp.gmail.com with ESMTPSA id i4sm15528630wrv.23.2020.05.19.01.55.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 May 2020 01:55:25 -0700 (PDT) From: Muhammad Bilal To: declan.doherty@intel.com, tomasz.kantecki@intel.com, pbhagavatula@marvell.com, skori@marvell.com Cc: dev@dpdk.org, Muhammad Bilal Date: Tue, 19 May 2020 13:54:42 +0500 Message-Id: <20200519085444.4562-3-m.bilal@emumba.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20200519085444.4562-1-m.bilal@emumba.com> References: <20200519085444.4562-1-m.bilal@emumba.com> Subject: [dpdk-dev] [PATCH 3/5] examples/l2fwd-keepalive: free resources in case of error 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" Freeing the resources and call rte_eal_cleanup in case of error exit. Signed-off-by: Muhammad Bilal --- examples/l2fwd-keepalive/main.c | 97 +++++++++++++++++++++++++-------- 1 file changed, 74 insertions(+), 23 deletions(-) diff --git a/examples/l2fwd-keepalive/main.c b/examples/l2fwd-keepalive/main.c index b7585d55e..04038711f 100644 --- a/examples/l2fwd-keepalive/main.c +++ b/examples/l2fwd-keepalive/main.c @@ -526,6 +526,18 @@ relay_core_state(void *ptr_data, const int id_core, id_core, core_state, last_alive); } +static void +stop_and_close_eth_dev(uint16_t portid) +{ + RTE_ETH_FOREACH_DEV(portid) { + printf("Closing port %d...", portid); + rte_eth_dev_stop(portid); + rte_eth_dev_close(portid); + printf(" Done\n"); + } + rte_eal_cleanup(); +} + int main(int argc, char **argv) { @@ -533,7 +545,8 @@ main(int argc, char **argv) int ret; uint16_t nb_ports; uint16_t nb_ports_available = 0; - uint16_t portid, last_port; + uint16_t portid = 0; + uint16_t last_port; unsigned lcore_id, rx_lcore_id; unsigned nb_ports_in_mask = 0; unsigned int total_nb_mbufs; @@ -559,21 +572,28 @@ main(int argc, char **argv) /* parse application arguments (after the EAL ones) */ ret = l2fwd_parse_args(argc, argv); - if (ret < 0) + if (ret < 0) { + stop_and_close_eth_dev(portid); rte_exit(EXIT_FAILURE, "Invalid L2FWD arguments\n"); + } nb_ports = rte_eth_dev_count_avail(); if (nb_ports == 0) rte_exit(EXIT_FAILURE, "No Ethernet ports - bye\n"); /* create the mbuf pool */ - total_nb_mbufs = NB_MBUF_PER_PORT * nb_ports; - - l2fwd_pktmbuf_pool = rte_pktmbuf_pool_create("mbuf_pool", - total_nb_mbufs, 32, 0, RTE_MBUF_DEFAULT_BUF_SIZE, - rte_socket_id()); - if (l2fwd_pktmbuf_pool == NULL) + l2fwd_pktmbuf_pool = rte_pktmbuf_pool_create("mbuf_pool", NB_MBUF, 32, + 0, RTE_MBUF_DEFAULT_BUF_SIZE, rte_socket_id()); + if (l2fwd_pktmbuf_pool == NULL) { + stop_and_close_eth_dev(portid); rte_exit(EXIT_FAILURE, "Cannot init mbuf pool\n"); + } + + nb_ports = rte_eth_dev_count_avail(); + if (nb_ports == 0) { + stop_and_close_eth_dev(portid); + rte_exit(EXIT_FAILURE, "No Ethernet ports - bye\n"); + } /* reset l2fwd_dst_ports */ for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) @@ -615,8 +635,10 @@ main(int argc, char **argv) lcore_queue_conf[rx_lcore_id].n_rx_port == l2fwd_rx_queue_per_lcore) { rx_lcore_id++; - if (rx_lcore_id >= RTE_MAX_LCORE) + if (rx_lcore_id >= RTE_MAX_LCORE) { + stop_and_close_eth_dev(portid); rte_exit(EXIT_FAILURE, "Not enough cores\n"); + } } if (qconf != &lcore_queue_conf[rx_lcore_id]) @@ -648,33 +670,41 @@ main(int argc, char **argv) fflush(stdout); ret = rte_eth_dev_info_get(portid, &dev_info); - if (ret != 0) + if (ret != 0) { + stop_and_close_eth_dev(portid); rte_exit(EXIT_FAILURE, "Error during getting device (port %u) info: %s\n", portid, strerror(-ret)); + } if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE) local_port_conf.txmode.offloads |= DEV_TX_OFFLOAD_MBUF_FAST_FREE; ret = rte_eth_dev_configure(portid, 1, 1, &local_port_conf); - if (ret < 0) + if (ret < 0) { + stop_and_close_eth_dev(portid); rte_exit(EXIT_FAILURE, "Cannot configure device: err=%d, port=%u\n", ret, portid); + } ret = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd, &nb_txd); - if (ret < 0) + if (ret < 0) { + stop_and_close_eth_dev(portid); rte_exit(EXIT_FAILURE, "Cannot adjust number of descriptors: err=%d, port=%u\n", ret, portid); + } ret = rte_eth_macaddr_get(portid, &l2fwd_ports_eth_addr[portid]); - if (ret < 0) + if (ret < 0) { + stop_and_close_eth_dev(portid); rte_exit(EXIT_FAILURE, "Cannot mac address: err=%d, port=%u\n", ret, portid); + } /* init one RX queue */ fflush(stdout); @@ -684,10 +714,12 @@ main(int argc, char **argv) rte_eth_dev_socket_id(portid), &rxq_conf, l2fwd_pktmbuf_pool); - if (ret < 0) + if (ret < 0) { + stop_and_close_eth_dev(portid); rte_exit(EXIT_FAILURE, "rte_eth_rx_queue_setup:err=%d, port=%u\n", ret, portid); + } /* init one TX queue on each port */ fflush(stdout); @@ -696,41 +728,51 @@ main(int argc, char **argv) ret = rte_eth_tx_queue_setup(portid, 0, nb_txd, rte_eth_dev_socket_id(portid), &txq_conf); - if (ret < 0) + if (ret < 0) { + stop_and_close_eth_dev(portid); rte_exit(EXIT_FAILURE, "rte_eth_tx_queue_setup:err=%d, port=%u\n", ret, portid); + } /* Initialize TX buffers */ tx_buffer[portid] = rte_zmalloc_socket("tx_buffer", RTE_ETH_TX_BUFFER_SIZE(MAX_PKT_BURST), 0, rte_eth_dev_socket_id(portid)); - if (tx_buffer[portid] == NULL) + if (tx_buffer[portid] == NULL) { + stop_and_close_eth_dev(portid); rte_exit(EXIT_FAILURE, "Cannot allocate buffer for tx on port %u\n", portid); + } rte_eth_tx_buffer_init(tx_buffer[portid], MAX_PKT_BURST); ret = rte_eth_tx_buffer_set_err_callback(tx_buffer[portid], rte_eth_tx_buffer_count_callback, &port_statistics[portid].dropped); - if (ret < 0) + if (ret < 0) { + stop_and_close_eth_dev(portid); rte_exit(EXIT_FAILURE, "Cannot set error callback for tx buffer on port %u\n", portid); + } /* Start device */ ret = rte_eth_dev_start(portid); - if (ret < 0) + if (ret < 0) { + stop_and_close_eth_dev(portid); rte_exit(EXIT_FAILURE, "rte_eth_dev_start:err=%d, port=%u\n", ret, portid); + } ret = rte_eth_promiscuous_enable(portid); - if (ret != 0) + if (ret != 0) { + stop_and_close_eth_dev(portid); rte_exit(EXIT_FAILURE, "rte_eth_promiscuous_enable:err=%s, port=%u\n", rte_strerror(-ret), portid); + } printf("Port %u, MAC address: " "%02X:%02X:%02X:%02X:%02X:%02X\n\n", @@ -747,6 +789,7 @@ main(int argc, char **argv) } if (!nb_ports_available) { + stop_and_close_eth_dev(portid); rte_exit(EXIT_FAILURE, "All available ports are disabled. Please set portmask.\n"); } @@ -761,13 +804,17 @@ main(int argc, char **argv) ka_shm = NULL; if (check_period > 0) { ka_shm = rte_keepalive_shm_create(); - if (ka_shm == NULL) + if (ka_shm == NULL) { + stop_and_close_eth_dev(portid); rte_exit(EXIT_FAILURE, "rte_keepalive_shm_create() failed"); + } rte_global_keepalive_info = rte_keepalive_create(&dead_core, ka_shm); - if (rte_global_keepalive_info == NULL) + if (rte_global_keepalive_info == NULL) { + stop_and_close_eth_dev(portid); rte_exit(EXIT_FAILURE, "init_keep_alive() failed"); + } rte_keepalive_register_relay_callback(rte_global_keepalive_info, relay_core_state, ka_shm); rte_timer_init(&hb_timer); @@ -778,8 +825,10 @@ main(int argc, char **argv) (void(*)(struct rte_timer*, void*)) &rte_keepalive_dispatch_pings, rte_global_keepalive_info - ) != 0 ) + ) != 0 ) { + stop_and_close_eth_dev(portid); rte_exit(EXIT_FAILURE, "Keepalive setup failure.\n"); + } } if (timer_period > 0) { if (rte_timer_reset(&stats_timer, @@ -787,8 +836,10 @@ main(int argc, char **argv) PERIODICAL, rte_lcore_id(), &print_stats, NULL - ) != 0 ) + ) != 0 ) { + stop_and_close_eth_dev(portid); rte_exit(EXIT_FAILURE, "Stats setup failure.\n"); + } } /* launch per-lcore init on every slave lcore */ RTE_LCORE_FOREACH_SLAVE(lcore_id) {