suricata
util-dpdk-bonding.c
Go to the documentation of this file.
1 /* Copyright (C) 2023 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 "util-dpdk-bonding.h"
26 
27 #ifdef HAVE_DPDK
28 
29 #include "util-dpdk.h"
30 #include "util-debug.h"
31 
32 /**
33  * Determines if the port is Bond or not by evaluating device driver name
34  * @param pid port ID
35  * @return 0 - the device si Bond PMD, 1 - regular device, <0 error
36  */
37 int32_t BondingIsBond(uint16_t pid)
38 {
39  struct rte_eth_dev_info di;
40  int32_t ret = rte_eth_dev_info_get(pid, &di);
41  if (ret < 0) {
42  SCLogError("%s: unable to get device info (err: %s)", DPDKGetPortNameByPortID(pid),
43  rte_strerror(-ret));
44  return ret;
45  }
46 
47  return strcmp(di.driver_name, "net_bonding") == 0 ? 0 : 1;
48 }
49 
50 uint16_t BondingMemberDevicesGet(
51  uint16_t bond_pid, uint16_t bonded_devs[], uint16_t bonded_devs_length)
52 {
53 #ifdef HAVE_DPDK_BOND
54 #if RTE_VERSION >= RTE_VERSION_NUM(23, 11, 0, 0)
55 
56 #if RTE_VERSION < RTE_VERSION_NUM(24, 11, 0, 0) // DPDK 23.11 - 24.07
57 #pragma GCC diagnostic push
58 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
59 #endif /* RTE_VERSION < RTE_VERSION_NUM(24, 11, 0, 0) */
60 
61  int32_t len = rte_eth_bond_members_get(bond_pid, bonded_devs, bonded_devs_length);
62 
63 #if RTE_VERSION < RTE_VERSION_NUM(24, 11, 0, 0)
64 #pragma GCC diagnostic pop
65 #endif /* RTE_VERSION < RTE_VERSION_NUM(24, 11, 0, 0) */
66 
67 #else
68  int32_t len = rte_eth_bond_slaves_get(bond_pid, bonded_devs, bonded_devs_length);
69 #endif /* RTE_VERSION >= RTE_VERSION_NUM(23, 11, 0, 0) */
70 
71  if (len == 0)
72  FatalError("%s: no bonded devices found", DPDKGetPortNameByPortID(bond_pid));
73  else if (len < 0)
74  FatalError("%s: unable to get bonded devices (err: %s)", DPDKGetPortNameByPortID(bond_pid),
75  rte_strerror(-len));
76 
77  return len;
78 #else
79  FatalError(
80  "%s: bond port not supported in DPDK installation", DPDKGetPortNameByPortID(bond_pid));
81 #endif
82 }
83 
84 int32_t BondingAllDevicesSameDriver(uint16_t bond_pid)
85 {
86  uint16_t bonded_devs[RTE_MAX_ETHPORTS] = { 0 };
87  uint16_t len = BondingMemberDevicesGet(bond_pid, bonded_devs, RTE_MAX_ETHPORTS);
88 
89  const char *driver_name = NULL, *first_driver_name = NULL;
90  struct rte_eth_dev_info di = { 0 };
91 
92  for (uint16_t i = 0; i < len; i++) {
93  int32_t ret = rte_eth_dev_info_get(bonded_devs[i], &di);
94  if (ret < 0)
95  FatalError("%s: unable to get device info (err: %s)",
96  DPDKGetPortNameByPortID(bonded_devs[i]), rte_strerror(-ret));
97 
98  if (i == 0) {
99  first_driver_name = di.driver_name;
100  } else {
101  driver_name = di.driver_name;
102  if (strncmp(first_driver_name, driver_name,
103  MIN(strlen(first_driver_name), strlen(driver_name))) != 0) {
104  return -EINVAL; // inconsistent drivers
105  }
106  }
107  }
108 
109  return 0;
110 }
111 
112 /**
113  * Translates to the driver that is actually used by the bonded ports
114  * \param bond_pid
115  * \return driver name, FatalError otherwise
116  */
117 const char *BondingDeviceDriverGet(uint16_t bond_pid)
118 {
119  uint16_t bonded_devs[RTE_MAX_ETHPORTS] = { 0 };
120  BondingMemberDevicesGet(bond_pid, bonded_devs, RTE_MAX_ETHPORTS);
121 
122  struct rte_eth_dev_info di = { 0 };
123  int32_t ret = rte_eth_dev_info_get(bonded_devs[0], &di);
124  if (ret < 0)
125  FatalError("%s: unable to get device info (err: %s)",
126  DPDKGetPortNameByPortID(bonded_devs[0]), rte_strerror(-ret));
127 
128  return di.driver_name;
129 }
130 
131 #endif /* HAVE_DPDK */
len
uint8_t len
Definition: app-layer-dnp3.h:2
MIN
#define MIN(x, y)
Definition: suricata-common.h:408
util-debug.h
util-dpdk.h
suricata-common.h
FatalError
#define FatalError(...)
Definition: util-debug.h:514
SCLogError
#define SCLogError(...)
Macro used to log ERROR messages.
Definition: util-debug.h:271
util-dpdk-bonding.h