|
| 1 | +# SPDX-License-Identifier: BSD-2-Clause |
| 2 | + |
| 3 | +cmake_minimum_required(VERSION 3.16) |
| 4 | +project(openpenny LANGUAGES CXX) |
| 5 | + |
| 6 | +set(CMAKE_CXX_STANDARD 17) |
| 7 | +set(CMAKE_CXX_STANDARD_REQUIRED ON) |
| 8 | +set(CMAKE_POSITION_INDEPENDENT_CODE ON) |
| 9 | + |
| 10 | +list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake") |
| 11 | + |
| 12 | +# Allow user to override grpc_cpp_plugin path from the command line, e.g.: |
| 13 | +# -DGRPC_CPP_PLUGIN=/usr/bin/grpc_cpp_plugin |
| 14 | +set(GRPC_CPP_PLUGIN "" CACHE FILEPATH "Path to grpc_cpp_plugin executable") |
| 15 | + |
| 16 | +include(cmake/Dependencies.cmake) |
| 17 | + |
| 18 | +# --- Options ------------------------------------------------------------------- |
| 19 | + |
| 20 | +option(OPENPENNY_WITH_XDP "Build AF_XDP reader" ON) |
| 21 | +option(OPENPENNY_WITH_DPDK "Build DPDK reader" OFF) |
| 22 | + |
| 23 | +# --- Core library -------------------------------------------------------------- |
| 24 | + |
| 25 | +set(openpenny_sources |
| 26 | + src/agg/Stats.cpp |
| 27 | + src/config/Config.cpp |
| 28 | + src/log/Log.cpp |
| 29 | + src/app/cli/cli_helpers.cpp |
| 30 | + src/app/core/AggregatesController.cpp |
| 31 | + src/app/core/active/ActiveTestPipeline.cpp |
| 32 | + src/app/core/DropCollectorBinding.cpp |
| 33 | + src/app/core/OpenpennyPipelineDriver.cpp |
| 34 | + src/app/core/passive/PassiveTestPipeline.cpp |
| 35 | + src/app/core/utils/FlowDebug.cpp |
| 36 | + src/app/core/WorkerLauncher.cpp |
| 37 | + src/app/core/PerThreadStats.cpp |
| 38 | + src/app/core/RuntimeSetup.cpp |
| 39 | + src/penny/flow/timer/ThreadFlowEventTimer.cpp |
| 40 | + src/penny/flow/state/PennyStats.cpp |
| 41 | + src/penny/flow/engine/FlowEvaluation.cpp |
| 42 | + src/penny/flow/engine/FlowEngine.cpp |
| 43 | + src/penny/flow/manager/ThreadFlowManager.cpp |
| 44 | + src/net/PacketParser.cpp |
| 45 | + src/net/PacketSourceFactory.cpp |
| 46 | +) |
| 47 | + |
| 48 | +if(OPENPENNY_WITH_XDP) |
| 49 | + list(APPEND openpenny_sources src/sources/xdp/XdpReader.cpp) |
| 50 | +endif() |
| 51 | + |
| 52 | +if(OPENPENNY_WITH_DPDK) |
| 53 | + list(APPEND openpenny_sources src/sources/dpdk/DpdkReader.cpp) |
| 54 | +endif() |
| 55 | + |
| 56 | +add_library(openpenny ${openpenny_sources}) |
| 57 | +target_include_directories(openpenny PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include) |
| 58 | +target_compile_options(openpenny PRIVATE -O3 -march=native -Wall -Wextra -Wpedantic) |
| 59 | + |
| 60 | +target_link_libraries(openpenny |
| 61 | + PUBLIC |
| 62 | + yaml-cpp::yaml-cpp |
| 63 | + nlohmann_json::nlohmann_json |
| 64 | + nlohmann_json_schema_validator |
| 65 | +) |
| 66 | + |
| 67 | +# Link Protobuf / gRPC to the core library when available. |
| 68 | +if(OPENPENNY_PROTOBUF_TARGET AND OPENPENNY_GRPCXX_TARGET) |
| 69 | + target_link_libraries(openpenny PUBLIC ${OPENPENNY_PROTOBUF_TARGET} ${OPENPENNY_GRPCXX_TARGET}) |
| 70 | +endif() |
| 71 | + |
| 72 | +target_link_libraries(openpenny PUBLIC Boost::boost) |
| 73 | + |
| 74 | +# --- libbpf / libxdp / DPDK via pkg-config ------------------------------------ |
| 75 | + |
| 76 | +find_package(PkgConfig QUIET) |
| 77 | + |
| 78 | +if(OPENPENNY_WITH_XDP) |
| 79 | + if(PKG_CONFIG_FOUND) |
| 80 | + pkg_check_modules(LIBBPF QUIET libbpf) |
| 81 | + pkg_check_modules(LIBXDP QUIET libxdp) |
| 82 | + endif() |
| 83 | + |
| 84 | + if(LIBBPF_FOUND) |
| 85 | + target_include_directories(openpenny PUBLIC ${LIBBPF_INCLUDE_DIRS}) |
| 86 | + if(TARGET PkgConfig::LIBBPF) |
| 87 | + target_link_libraries(openpenny PUBLIC PkgConfig::LIBBPF) |
| 88 | + else() |
| 89 | + target_link_libraries(openpenny PUBLIC ${LIBBPF_LIBRARIES}) |
| 90 | + endif() |
| 91 | + target_compile_definitions(openpenny PRIVATE OPENPENNY_WITH_LIBBPF=1) |
| 92 | + else() |
| 93 | + message(WARNING "Building without libbpf; the XdpReader cannot capture packets (no synthetic fallback)") |
| 94 | + endif() |
| 95 | + |
| 96 | + if(LIBXDP_FOUND) |
| 97 | + if(TARGET PkgConfig::LIBXDP) |
| 98 | + target_link_libraries(openpenny PUBLIC PkgConfig::LIBXDP) |
| 99 | + else() |
| 100 | + target_link_libraries(openpenny PUBLIC ${LIBXDP_LIBRARIES}) |
| 101 | + endif() |
| 102 | + endif() |
| 103 | + |
| 104 | + target_compile_definitions(openpenny PRIVATE OPENPENNY_WITH_XDP=1) |
| 105 | +endif() |
| 106 | + |
| 107 | +if(OPENPENNY_WITH_DPDK) |
| 108 | + if(PKG_CONFIG_FOUND) |
| 109 | + pkg_check_modules(DPDK QUIET libdpdk) |
| 110 | + endif() |
| 111 | + |
| 112 | + if(DPDK_FOUND) |
| 113 | + target_include_directories(openpenny PUBLIC ${DPDK_INCLUDE_DIRS}) |
| 114 | + target_link_libraries(openpenny PUBLIC ${DPDK_LIBRARIES}) |
| 115 | + target_compile_definitions(openpenny PRIVATE OPENPENNY_WITH_DPDK=1) |
| 116 | + else() |
| 117 | + message(WARNING "DPDK requested but libdpdk not found; DPDK reader will be unavailable.") |
| 118 | + endif() |
| 119 | +endif() |
| 120 | + |
| 121 | +# --- CLI ----------------------------------------------------------------------- |
| 122 | + |
| 123 | +add_executable(openpenny_cli |
| 124 | + src/app/cli/penny_cli.cpp |
| 125 | + src/app/cli/source_setup.cpp |
| 126 | +) |
| 127 | +target_link_libraries(openpenny_cli PRIVATE openpenny) |
| 128 | + |
| 129 | +# --- Tests --------------------------------------------------------------------- |
| 130 | + |
| 131 | +enable_testing() |
| 132 | + |
| 133 | +add_executable(test_initial_flow_monitoring tests/test_initial_flow_monitoring.cpp) |
| 134 | +target_link_libraries(test_initial_flow_monitoring PRIVATE openpenny) |
| 135 | +add_test(NAME initial_flow_monitoring COMMAND test_initial_flow_monitoring) |
| 136 | + |
| 137 | +add_executable(test_gap_management tests/test_gap_management.cpp) |
| 138 | +target_link_libraries(test_gap_management PRIVATE openpenny) |
| 139 | +add_test(NAME gap_management COMMAND test_gap_management) |
| 140 | + |
| 141 | +add_executable(test_sequence_ordering tests/test_sequence_ordering.cpp) |
| 142 | +target_link_libraries(test_sequence_ordering PRIVATE openpenny) |
| 143 | +add_test(NAME sequence_ordering COMMAND test_sequence_ordering) |
| 144 | + |
| 145 | +add_executable(test_drop_timer tests/test_drop_timer.cpp) |
| 146 | +target_link_libraries(test_drop_timer PRIVATE openpenny) |
| 147 | +add_test(NAME drop_timer COMMAND test_drop_timer) |
| 148 | + |
| 149 | +add_executable(test_flow_thresholds tests/test_flow_thresholds.cpp) |
| 150 | +target_link_libraries(test_flow_thresholds PRIVATE openpenny) |
| 151 | +add_test(NAME flow_thresholds COMMAND test_flow_thresholds) |
| 152 | + |
| 153 | +add_executable(test_cli_options tests/test_cli_options.cpp) |
| 154 | +target_link_libraries(test_cli_options PRIVATE openpenny) |
| 155 | +add_test(NAME cli_options COMMAND test_cli_options) |
| 156 | + |
| 157 | +# --- XDP build helper ---------------------------------------------------------- |
| 158 | + |
| 159 | +add_custom_target(xdp_bpf |
| 160 | + COMMAND ${CMAKE_COMMAND} -E env "PATH=$ENV{PATH}" make -C ${CMAKE_CURRENT_SOURCE_DIR}/xdp-fw xdp_redirect_dstprefix.o |
| 161 | + BYPRODUCTS ${CMAKE_CURRENT_SOURCE_DIR}/xdp-fw/xdp_redirect_dstprefix.o |
| 162 | + COMMENT "Building XDP program (xdp-fw/xdp_redirect_dstprefix.o)" |
| 163 | +) |
| 164 | + |
| 165 | +# --- gRPC daemon / worker (only if Protobuf & gRPC really usable) -------------- |
| 166 | + |
| 167 | +if(OPENPENNY_PROTOBUF_TARGET AND OPENPENNY_GRPCXX_TARGET AND Protobuf_PROTOC_EXECUTABLE) |
| 168 | + # Locate grpc_cpp_plugin if available. |
| 169 | + set(GRPC_CPP_PLUGIN_PATH "") |
| 170 | + |
| 171 | + # 1. User override (from -DGRPC_CPP_PLUGIN=...) |
| 172 | + if(GRPC_CPP_PLUGIN) |
| 173 | + set(GRPC_CPP_PLUGIN_PATH "${GRPC_CPP_PLUGIN}") |
| 174 | + # 2. Imported CMake target, if present |
| 175 | + elseif(TARGET gRPC::grpc_cpp_plugin) |
| 176 | + get_target_property(_grpc_cpp_plugin_path gRPC::grpc_cpp_plugin IMPORTED_LOCATION) |
| 177 | + if(_grpc_cpp_plugin_path) |
| 178 | + set(GRPC_CPP_PLUGIN_PATH "${_grpc_cpp_plugin_path}") |
| 179 | + endif() |
| 180 | + # 3. Fallback: search in PATH |
| 181 | + else() |
| 182 | + find_program(_grpc_cpp_plugin_exe NAMES grpc_cpp_plugin) |
| 183 | + if(_grpc_cpp_plugin_exe) |
| 184 | + set(GRPC_CPP_PLUGIN_PATH "${_grpc_cpp_plugin_exe}") |
| 185 | + endif() |
| 186 | + endif() |
| 187 | + |
| 188 | + if(NOT GRPC_CPP_PLUGIN_PATH) |
| 189 | + message(WARNING "grpc_cpp_plugin not found; 'pennyd' daemon will not be built.") |
| 190 | + else() |
| 191 | + message(STATUS "Using grpc_cpp_plugin: ${GRPC_CPP_PLUGIN_PATH}") |
| 192 | + |
| 193 | + # Proto generation |
| 194 | + set(penny_proto ${CMAKE_CURRENT_SOURCE_DIR}/proto/penny.proto) |
| 195 | + set(penny_proto_src ${CMAKE_CURRENT_BINARY_DIR}/penny.pb.cc) |
| 196 | + set(penny_proto_hdr ${CMAKE_CURRENT_BINARY_DIR}/penny.pb.h) |
| 197 | + set(penny_grpc_src ${CMAKE_CURRENT_BINARY_DIR}/penny.grpc.pb.cc) |
| 198 | + set(penny_grpc_hdr ${CMAKE_CURRENT_BINARY_DIR}/penny.grpc.pb.h) |
| 199 | + |
| 200 | + add_custom_command( |
| 201 | + OUTPUT |
| 202 | + ${penny_proto_src} |
| 203 | + ${penny_proto_hdr} |
| 204 | + ${penny_grpc_src} |
| 205 | + ${penny_grpc_hdr} |
| 206 | + COMMAND ${Protobuf_PROTOC_EXECUTABLE} |
| 207 | + --experimental_allow_proto3_optional |
| 208 | + --grpc_out ${CMAKE_CURRENT_BINARY_DIR} |
| 209 | + --cpp_out ${CMAKE_CURRENT_BINARY_DIR} |
| 210 | + --plugin=protoc-gen-grpc=${GRPC_CPP_PLUGIN_PATH} |
| 211 | + -I ${CMAKE_CURRENT_SOURCE_DIR}/proto |
| 212 | + ${penny_proto} |
| 213 | + DEPENDS ${penny_proto} |
| 214 | + COMMENT "Generating gRPC / Protobuf sources from ${penny_proto}" |
| 215 | + ) |
| 216 | + |
| 217 | + add_library(penny_proto STATIC ${penny_proto_src} ${penny_grpc_src}) |
| 218 | + target_include_directories(penny_proto PUBLIC ${CMAKE_CURRENT_BINARY_DIR}) |
| 219 | + target_link_libraries(penny_proto PUBLIC ${OPENPENNY_PROTOBUF_TARGET} ${OPENPENNY_GRPCXX_TARGET}) |
| 220 | + |
| 221 | + target_include_directories(openpenny PUBLIC ${CMAKE_CURRENT_BINARY_DIR}) |
| 222 | + target_link_libraries(openpenny PUBLIC penny_proto) |
| 223 | + |
| 224 | + if(OPENPENNY_GRPC_C_TARGET) |
| 225 | + add_executable(pennyd |
| 226 | + src/app/grpc/pennyd.cpp |
| 227 | + src/grpc/PennyService.cpp |
| 228 | + ) |
| 229 | + target_include_directories(pennyd PRIVATE ${CMAKE_CURRENT_BINARY_DIR}) |
| 230 | + target_link_libraries(pennyd |
| 231 | + PRIVATE |
| 232 | + openpenny |
| 233 | + penny_proto |
| 234 | + ${OPENPENNY_PROTOBUF_TARGET} |
| 235 | + ${OPENPENNY_GRPCXX_TARGET} |
| 236 | + ${OPENPENNY_GRPC_C_TARGET} |
| 237 | + ) |
| 238 | + else() |
| 239 | + message(WARNING "gRPC C library target not found; 'pennyd' daemon will not be built.") |
| 240 | + endif() |
| 241 | + |
| 242 | + add_executable(penny_worker |
| 243 | + src/app/worker/penny_worker.cpp |
| 244 | + ) |
| 245 | + target_link_libraries(penny_worker PRIVATE openpenny) |
| 246 | + endif() |
| 247 | +else() |
| 248 | + message(WARNING "gRPC/Protobuf not fully available (libraries, protoc or grpc_cpp_plugin missing); penny daemon will not be built") |
| 249 | +endif() |
0 commit comments