suricata
util-dpdk.c
Go to the documentation of this file.
1/* Copyright (C) 2021 Open Information Security Foundation
2 *
3 * You can copy, redistribute or modify this Program under the terms of
4 * the GNU General Public License version 2 as published by the Free
5 * Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * version 2 along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15 * 02110-1301, USA.
16 */
17
18/**
19 * \file
20 *
21 * \author Lukas Sismis <lukas.sismis@gmail.com>
22 */
23
24#include "suricata-common.h"
25#include "suricata.h"
26#include "util-dpdk.h"
27#include "util-debug.h"
28#include "util-device-private.h"
29
31{
32#ifdef HAVE_DPDK
33 if (SCRunmodeGet() == RUNMODE_DPDK) {
34 int retval = rte_eal_cleanup();
35 if (retval != 0)
36 SCLogError("EAL cleanup failed: %s", strerror(-retval));
37 }
38#endif
39}
40
42{
43 (void)ldev; // avoid warnings of unused variable
44#ifdef HAVE_DPDK
45 if (SCRunmodeGet() == RUNMODE_DPDK) {
46 uint16_t port_id;
47 int retval = rte_eth_dev_get_port_by_name(ldev->dev, &port_id);
48 if (retval < 0) {
49 SCLogError("%s: failed get port id, error: %s", ldev->dev, rte_strerror(-retval));
50 return;
51 }
52
53 SCLogPerf("%s: closing device", ldev->dev);
54 rte_eth_dev_close(port_id);
55 }
56#endif
57}
58
60{
61 (void)ldev; // avoid warnings of unused variable
62#ifdef HAVE_DPDK
63 if (SCRunmodeGet() == RUNMODE_DPDK) {
64 SCLogDebug("%s: releasing packet mempools", ldev->dev);
65 DPDKDeviceResourcesDeinit(&ldev->dpdk_vars);
66 }
67#endif
68}
69
70/**
71 * \param port_id - queried port
72 * \param socket_id - socket ID of the queried port
73 * \return non-negative number on success, negative on failure (errno)
74 */
75int32_t DPDKDeviceSetSocketID(uint16_t port_id, int32_t *socket_id)
76{
77#ifdef HAVE_DPDK
78 rte_errno = 0;
79 int retval = rte_eth_dev_socket_id(port_id);
80 *socket_id = retval;
81
82#if RTE_VERSION >= RTE_VERSION_NUM(22, 11, 0, 0) // DPDK API changed since 22.11
83 retval = -rte_errno;
84#else
85 if (retval == SOCKET_ID_ANY)
86 retval = 0; // DPDK couldn't determine socket ID of a port
87#endif
88
89 return retval;
90#endif /* HAVE_DPDK */
91 return -ENOTSUP;
92}
93
94/**
95 * \param iface_name - name of the queried interface
96 * \param socket_id - socket ID of the queried port
97 * \return non-negative number on success, negative on failure (errno)
98 */
99int32_t DPDKDeviceNameSetSocketID(char *iface_name, int32_t *socket_id)
100{
101#ifdef HAVE_DPDK
102 uint16_t port_id = 0;
103 int r = rte_eth_dev_get_port_by_name(iface_name, &port_id);
104 if (r < 0) {
105 SCLogError("%s: interface not found: %s", iface_name, rte_strerror(-r));
106 SCReturnInt(r);
107 }
108 return DPDKDeviceSetSocketID(port_id, socket_id);
109#endif /* HAVE_DPDK */
110 return -ENOTSUP;
111}
112
113#ifdef HAVE_DPDK
114/**
115 * Retrieves name of the port from port id
116 * Not thread-safe
117 * @param pid
118 * @return static dev_name on success
119 */
120const char *DPDKGetPortNameByPortID(uint16_t pid)
121{
122 static char dev_name[RTE_ETH_NAME_MAX_LEN];
123 int32_t ret = rte_eth_dev_get_name_by_port(pid, dev_name);
124 if (ret < 0) {
125 FatalError("Port %d: Failed to obtain port name (err: %s)", pid, rte_strerror(-ret));
126 }
127 return dev_name;
128}
129
130#endif /* HAVE_DPDK */
@ RUNMODE_DPDK
Definition runmodes.h:39
SCRunMode SCRunmodeGet(void)
Get the current run mode.
Definition suricata.c:279
#define FatalError(...)
Definition util-debug.h:510
#define SCLogPerf(...)
Definition util-debug.h:234
#define SCLogDebug(...)
Definition util-debug.h:275
#define SCReturnInt(x)
Definition util-debug.h:281
#define SCLogError(...)
Macro used to log ERROR messages.
Definition util-debug.h:267
int32_t DPDKDeviceNameSetSocketID(char *iface_name, int32_t *socket_id)
Definition util-dpdk.c:99
void DPDKFreeDevice(LiveDevice *ldev)
Definition util-dpdk.c:59
int32_t DPDKDeviceSetSocketID(uint16_t port_id, int32_t *socket_id)
Definition util-dpdk.c:75
void DPDKCloseDevice(LiveDevice *ldev)
Definition util-dpdk.c:41
void DPDKCleanupEAL(void)
Definition util-dpdk.c:30