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 /**
85  * \brief Callback for rte_kvargs_process that increments a counter.
86  */
87 static int BondingMemberCountCb(
88  const char *key __rte_unused, const char *value __rte_unused, void *opaque)
89 {
90  uint16_t *cnt = opaque;
91  (*cnt)++;
92  return 0;
93 }
94 
95 /**
96  * \brief Count bonding member devices from the device's devargs.
97  *
98  * Bonding members are only attached when rte_eth_dev_configure() is called
99  * (inside bond_ethdev_configure), so rte_eth_bond_members_get() returns 0
100  * during early config. Instead, parse the devargs stored during device probe
101  * to count member/slave entries.
102  *
103  * \param dev_info device info (must be non-NULL)
104  * \return number of member devices found, 0 on any failure
105  */
106 static uint16_t BondingMemberDevCountFromDevargs(const struct rte_eth_dev_info *dev_info)
107 {
108  if (dev_info->device == NULL) {
109  return 0;
110  }
111 
112 #if RTE_VERSION >= RTE_VERSION_NUM(22, 11, 0, 0)
113  const struct rte_devargs *devargs = rte_dev_devargs(dev_info->device);
114 #else
115  const struct rte_devargs *devargs = dev_info->device->devargs;
116 #endif
117  if (devargs == NULL || devargs->args == NULL) {
118  return 0;
119  }
120 
121  struct rte_kvargs *kvargs = rte_kvargs_parse(devargs->args, NULL);
122  if (kvargs == NULL) {
123  return 0;
124  }
125 
126  uint16_t count = 0;
127  int ret;
128 #if RTE_VERSION >= RTE_VERSION_NUM(23, 11, 0, 0)
129  ret = rte_kvargs_process(kvargs, "member", BondingMemberCountCb, &count);
130 #else
131  ret = rte_kvargs_process(kvargs, "slave", BondingMemberCountCb, &count);
132 #endif
133 
134  rte_kvargs_free(kvargs);
135  return ret == 0 ? count : 0;
136 }
137 
138 uint32_t BondingMempoolSizeCalculate(
139  uint16_t bond_pid, const struct rte_eth_dev_info *dev_info, uint32_t curr_mempool_size)
140 {
141  if (curr_mempool_size == 0) {
142  return 0;
143  }
144 
145  uint16_t cnt = BondingMemberDevCountFromDevargs(dev_info);
146  if (cnt == 0) {
147  // don't adjust if unable to determine the number of bonded devices
148  return curr_mempool_size;
149  } else if (curr_mempool_size > UINT32_MAX / cnt) {
150  FatalError("%s: mempool size too large to adjust for %u bonded devices",
151  DPDKGetPortNameByPortID(bond_pid), cnt);
152  }
153 
154  return curr_mempool_size * cnt;
155 }
156 
157 int32_t BondingAllDevicesSameDriver(uint16_t bond_pid)
158 {
159  uint16_t bonded_devs[RTE_MAX_ETHPORTS] = { 0 };
160  uint16_t len = BondingMemberDevicesGet(bond_pid, bonded_devs, RTE_MAX_ETHPORTS);
161 
162  const char *driver_name = NULL, *first_driver_name = NULL;
163  struct rte_eth_dev_info di = { 0 };
164 
165  for (uint16_t i = 0; i < len; i++) {
166  int32_t ret = rte_eth_dev_info_get(bonded_devs[i], &di);
167  if (ret < 0)
168  FatalError("%s: unable to get device info (err: %s)",
169  DPDKGetPortNameByPortID(bonded_devs[i]), rte_strerror(-ret));
170 
171  if (i == 0) {
172  first_driver_name = di.driver_name;
173  } else {
174  driver_name = di.driver_name;
175  if (strncmp(first_driver_name, driver_name,
176  MIN(strlen(first_driver_name), strlen(driver_name))) != 0) {
177  return -EINVAL; // inconsistent drivers
178  }
179  }
180  }
181 
182  return 0;
183 }
184 
185 /**
186  * Translates to the driver that is actually used by the bonded ports
187  * \param bond_pid
188  * \return driver name, FatalError otherwise
189  */
190 const char *BondingDeviceDriverGet(uint16_t bond_pid)
191 {
192  uint16_t bonded_devs[RTE_MAX_ETHPORTS] = { 0 };
193  BondingMemberDevicesGet(bond_pid, bonded_devs, RTE_MAX_ETHPORTS);
194 
195  struct rte_eth_dev_info di = { 0 };
196  int32_t ret = rte_eth_dev_info_get(bonded_devs[0], &di);
197  if (ret < 0)
198  FatalError("%s: unable to get device info (err: %s)",
199  DPDKGetPortNameByPortID(bonded_devs[0]), rte_strerror(-ret));
200 
201  return di.driver_name;
202 }
203 
204 #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
cnt
uint32_t cnt
Definition: tmqh-packetpool.h:7
util-dpdk.h
suricata-common.h
FatalError
#define FatalError(...)
Definition: util-debug.h:517
SCLogError
#define SCLogError(...)
Macro used to log ERROR messages.
Definition: util-debug.h:274
util-dpdk-bonding.h