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