Skip to content

Commit 36a5e45

Browse files
committed
modify err code
1 parent b6a6425 commit 36a5e45

File tree

6 files changed

+45
-48
lines changed

6 files changed

+45
-48
lines changed

Diff for: core/ebpf/driver/FileFilter.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ int CreateFileFilterForCallname(std::shared_ptr<logtail::ebpf::BPFWrapper<securi
4848

4949
int callNameIdx = GetCallNameIdx(callName);
5050
if (callNameIdx == ERR_UNKNOWN_CALLNAME) {
51-
return ERR_DRIVER_INVALID_PARAM;
51+
return kErrDriverInvalidParam;
5252
}
5353

5454
auto filter = std::get_if<SecurityFileFilter>(&config);
@@ -63,7 +63,7 @@ int CreateFileFilterForCallname(std::shared_ptr<logtail::ebpf::BPFWrapper<securi
6363
ebpf_log(logtail::ebpf::eBPFLogType::NAMI_LOG_TYPE_WARN,
6464
"[CreateFilterForCallname][IDAllocator] Failed to get next id, reach max %d\n",
6565
IdAllocator::GetInstance()->GetMaxId<StringPrefixMap>());
66-
return ERR_DRIVER_INVALID_PARAM;
66+
return kErrDriverInvalidParam;
6767
}
6868
ebpf_log(logtail::ebpf::eBPFLogType::NAMI_LOG_TYPE_WARN,
6969
"[CreateFilterForCallname] Get index %d for %s\n",
@@ -122,7 +122,7 @@ int DeleteFileFilterForCallname(std::shared_ptr<logtail::ebpf::BPFWrapper<securi
122122
ebpf_log(logtail::ebpf::eBPFLogType::NAMI_LOG_TYPE_WARN, "DeleteFilterForCallname %s\n", callName.c_str());
123123
int callNameIdx = GetCallNameIdx(callName);
124124
if (callNameIdx == ERR_UNKNOWN_CALLNAME) {
125-
return ERR_DRIVER_INVALID_PARAM;
125+
return kErrDriverInvalidParam;
126126
}
127127
int ret = 0;
128128
// step1: detach callname

Diff for: core/ebpf/driver/NetworkFilter.cpp

+10-10
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ int SetCommonAddrFilter(std::shared_ptr<BPFWrapper<security_bpf>>& wrapper,
165165
logtail::ebpf::eBPFLogType::NAMI_LOG_TYPE_WARN,
166166
"[CreateNetFilterForCallname][IDAllocator] Failed to get next id for addr4 map, reach max %d\n",
167167
IdAllocator::GetInstance()->GetMaxId<Addr4Map>());
168-
return ERR_DRIVER_INVALID_PARAM;
168+
return kErrDriverInvalidParam;
169169
}
170170
kFilter.map_idx[0] = ipv4Idx;
171171
for (auto arg4 : addr4Tries) {
@@ -203,7 +203,7 @@ int SetCommonAddrFilter(std::shared_ptr<BPFWrapper<security_bpf>>& wrapper,
203203
logtail::ebpf::eBPFLogType::NAMI_LOG_TYPE_WARN,
204204
"[CreateNetFilterForCallname][IDAllocator] Failed to get next id for addr6 map, reach max %d\n",
205205
IdAllocator::GetInstance()->GetMaxId<Addr6Map>());
206-
return ERR_DRIVER_INVALID_PARAM;
206+
return kErrDriverInvalidParam;
207207
}
208208
kFilter.map_idx[1] = ipv6Idx;
209209
for (auto arg6 : addr6Tries) {
@@ -249,7 +249,7 @@ int SetCommonPortFilter(std::shared_ptr<BPFWrapper<security_bpf>>& wrapper,
249249
ebpf_log(logtail::ebpf::eBPFLogType::NAMI_LOG_TYPE_WARN,
250250
"[CreateNetFilterForCallname][IDAllocator] Failed to get next id for port map, reach max %d\n",
251251
IdAllocator::GetInstance()->GetMaxId<PortMap>());
252-
return ERR_DRIVER_INVALID_PARAM;
252+
return kErrDriverInvalidParam;
253253
}
254254
selector_filter kFilter{};
255255
kFilter.filter_type = filterType;
@@ -258,7 +258,7 @@ int SetCommonPortFilter(std::shared_ptr<BPFWrapper<security_bpf>>& wrapper,
258258

259259
if (filters.filter_count >= MAX_FILTER_FOR_PER_CALLNAME) {
260260
ebpf_log(eBPFLogType::NAMI_LOG_TYPE_WARN, "filters count exceeded! max: %d\n", MAX_FILTER_FOR_PER_CALLNAME);
261-
return ERR_DRIVER_INVALID_PARAM;
261+
return kErrDriverInvalidParam;
262262
}
263263
filters.filters[filters.filter_count++] = kFilter;
264264
for (uint32_t port : portList) {
@@ -361,22 +361,22 @@ int CreateNetworkFilterForCallname(
361361
int ret = 0;
362362
int callNameIdx = GetCallNameIdx(callName);
363363
if (callNameIdx == ERR_UNKNOWN_CALLNAME) {
364-
return ERR_DRIVER_INVALID_PARAM;
364+
return kErrDriverInvalidParam;
365365
}
366366
const auto* filter = std::get_if<SecurityNetworkFilter>(&newConfig);
367367

368368
if (filter) {
369369
if (filter->mDestAddrBlackList.size() && filter->mDestAddrList.size()) {
370-
return ERR_DRIVER_INVALID_PARAM;
370+
return kErrDriverInvalidParam;
371371
}
372372
if (filter->mSourceAddrBlackList.size() && filter->mSourceAddrList.size()) {
373-
return ERR_DRIVER_INVALID_PARAM;
373+
return kErrDriverInvalidParam;
374374
}
375375
if (filter->mDestPortList.size() && filter->mDestPortBlackList.size()) {
376-
return ERR_DRIVER_INVALID_PARAM;
376+
return kErrDriverInvalidParam;
377377
}
378378
if (filter->mSourcePortList.size() && filter->mSourcePortBlackList.size()) {
379-
return ERR_DRIVER_INVALID_PARAM;
379+
return kErrDriverInvalidParam;
380380
}
381381
selector_filters kernelFilters{};
382382
ebpf_log(eBPFLogType::NAMI_LOG_TYPE_INFO, "filter not empty!\n");
@@ -416,7 +416,7 @@ int DeleteNetworkFilterForCallname(std::shared_ptr<BPFWrapper<security_bpf>>& wr
416416
ebpf_log(eBPFLogType::NAMI_LOG_TYPE_INFO, "DisableCallName %s\n", callName.c_str());
417417
int callNameIdx = GetCallNameIdx(callName);
418418
if (callNameIdx == ERR_UNKNOWN_CALLNAME) {
419-
return ERR_DRIVER_INVALID_PARAM;
419+
return kErrDriverInvalidParam;
420420
}
421421
int ret = 0;
422422

Diff for: core/ebpf/driver/eBPFDriver.cpp

+10-10
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ int SetupPerfBuffers(logtail::ebpf::PluginConfig* arg) {
105105
}
106106
case logtail::ebpf::PluginType::NETWORK_OBSERVE:
107107
default:
108-
return ERR_DRIVER_INTERNAL;
108+
return kErrDriverInternal;
109109
}
110110
auto config = arg->mConfig;
111111
// create pb and set perf buffer meta
@@ -124,7 +124,7 @@ int SetupPerfBuffers(logtail::ebpf::PluginConfig* arg) {
124124
magic_enum::enum_name(arg->mPluginType).data(),
125125
spec.mName.c_str(),
126126
spec.mSize);
127-
return ERR_DRIVER_INTERNAL;
127+
return kErrDriverInternal;
128128
}
129129
pbs.push_back(pb);
130130
}
@@ -220,7 +220,7 @@ int start_plugin(logtail::ebpf::PluginConfig* arg) {
220220
ebpf_log(logtail::ebpf::eBPFLogType::NAMI_LOG_TYPE_WARN,
221221
"[start_plugin] Failed to create filter for callname %s\n",
222222
cn.c_str());
223-
return ERR_DRIVER_INTERNAL;
223+
return kErrDriverInternal;
224224
}
225225
}
226226
}
@@ -229,7 +229,7 @@ int start_plugin(logtail::ebpf::PluginConfig* arg) {
229229
if (ret) {
230230
ebpf_log(logtail::ebpf::eBPFLogType::NAMI_LOG_TYPE_WARN,
231231
"file security: DynamicAttachBPFObject fail\n");
232-
return ERR_DRIVER_INTERNAL;
232+
return kErrDriverInternal;
233233
}
234234
ebpf_log(logtail::ebpf::eBPFLogType::NAMI_LOG_TYPE_DEBUG,
235235
"file security: DynamicAttachBPFObject success\n");
@@ -268,7 +268,7 @@ int start_plugin(logtail::ebpf::PluginConfig* arg) {
268268
ebpf_log(logtail::ebpf::eBPFLogType::NAMI_LOG_TYPE_WARN,
269269
"[start_plugin] Failed to create filter for callname %s\n",
270270
cn.c_str());
271-
return ERR_DRIVER_INTERNAL;
271+
return kErrDriverInternal;
272272
}
273273
}
274274
}
@@ -277,7 +277,7 @@ int start_plugin(logtail::ebpf::PluginConfig* arg) {
277277
if (ret) {
278278
ebpf_log(logtail::ebpf::eBPFLogType::NAMI_LOG_TYPE_WARN,
279279
"network security: DynamicAttachBPFObject fail\n");
280-
return ERR_DRIVER_INTERNAL;
280+
return kErrDriverInternal;
281281
}
282282
ebpf_log(logtail::ebpf::eBPFLogType::NAMI_LOG_TYPE_DEBUG,
283283
"network security: DynamicAttachBPFObject success\n");
@@ -331,7 +331,7 @@ int start_plugin(logtail::ebpf::PluginConfig* arg) {
331331
ebpf_log(logtail::ebpf::eBPFLogType::NAMI_LOG_TYPE_WARN,
332332
"process security: DynamicAttachBPFObject fail ret:%d\n",
333333
ret);
334-
return ERR_DRIVER_INTERNAL;
334+
return kErrDriverInternal;
335335
}
336336
ebpf_log(logtail::ebpf::eBPFLogType::NAMI_LOG_TYPE_DEBUG,
337337
"process security: DynamicAttachBPFObject success\n");
@@ -396,7 +396,7 @@ int resume_plugin(logtail::ebpf::PluginConfig* arg) {
396396
if (ret) {
397397
ebpf_log(logtail::ebpf::eBPFLogType::NAMI_LOG_TYPE_WARN,
398398
"file security: DynamicAttachBPFObject fail\n");
399-
return ERR_DRIVER_INTERNAL;
399+
return kErrDriverInternal;
400400
}
401401
ebpf_log(logtail::ebpf::eBPFLogType::NAMI_LOG_TYPE_DEBUG,
402402
"file security: DynamicAttachBPFObject success\n");
@@ -418,7 +418,7 @@ int resume_plugin(logtail::ebpf::PluginConfig* arg) {
418418
if (ret) {
419419
ebpf_log(logtail::ebpf::eBPFLogType::NAMI_LOG_TYPE_WARN,
420420
"network security: DynamicAttachBPFObject fail\n");
421-
return ERR_DRIVER_INTERNAL;
421+
return kErrDriverInternal;
422422
}
423423
ebpf_log(logtail::ebpf::eBPFLogType::NAMI_LOG_TYPE_DEBUG,
424424
"network security: DynamicAttachBPFObject success\n");
@@ -452,7 +452,7 @@ int update_plugin(logtail::ebpf::PluginConfig* arg) {
452452
ebpf_log(logtail::ebpf::eBPFLogType::NAMI_LOG_TYPE_WARN,
453453
"[update plugin] network security delete filter for callname %s failed.\n",
454454
cn.c_str());
455-
return ERR_DRIVER_INTERNAL;
455+
return kErrDriverInternal;
456456
}
457457

458458
ret = logtail::ebpf::CreateNetworkFilterForCallname(gWrapper, cn, opt.mFilter);

Diff for: core/ebpf/driver/eBPFWrapper.h

+16-17
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,9 @@ class BPFWrapperBase {
5555
virtual ~BPFWrapperBase() = default;
5656
};
5757

58-
#define ERR_INIT_SKEL 1
59-
#define ERR_INVALID_PARAM 2
60-
#define ERR_LIBBPF 3
61-
#define ERR_NOT_FOUND -1
58+
inline constexpr int kErrInitSkel = 1;
59+
inline constexpr int kErrLibbpf = 2;
60+
inline constexpr int kErrNotFound = -1;
6261

6362
template <typename T>
6463
class BPFWrapper : public BPFWrapperBase {
@@ -77,7 +76,7 @@ class BPFWrapper : public BPFWrapperBase {
7776
mSkel = T::open_and_load();
7877
mFlag = true;
7978
if (!mSkel) {
80-
return ERR_INIT_SKEL;
79+
return kErrInitSkel;
8180
}
8281
bpf_map* map = nullptr;
8382
bpf_object__for_each_map(map, mSkel->obj) {
@@ -146,7 +145,7 @@ class BPFWrapper : public BPFWrapperBase {
146145
int SetTailCall(const std::string& mapName, const std::vector<std::string>& functions) {
147146
int mapFd = SearchMapFd(mapName);
148147
if (mapFd < 0) {
149-
return ERR_NOT_FOUND;
148+
return kErrNotFound;
150149
}
151150

152151
for (int i = 0; i < (int)functions.size(); i++) {
@@ -168,7 +167,7 @@ class BPFWrapper : public BPFWrapperBase {
168167
int DeleteInnerMap(const std::string& outterMapName, void* outterKey) {
169168
int mapFd = SearchMapFd(outterMapName);
170169
if (mapFd < 0) {
171-
return ERR_NOT_FOUND;
170+
return kErrNotFound;
172171
}
173172

174173
// delete bpf map
@@ -194,7 +193,7 @@ class BPFWrapper : public BPFWrapperBase {
194193
int DeleteInnerMapElem(const std::string& outterMapName, void* outterKey, void* innerKey) {
195194
int mapFd = SearchMapFd(outterMapName);
196195
if (mapFd < 0) {
197-
return ERR_NOT_FOUND;
196+
return kErrNotFound;
198197
}
199198
int innerMapFd = -1;
200199
uint32_t innerMapId = 0;
@@ -205,7 +204,7 @@ class BPFWrapper : public BPFWrapperBase {
205204

206205
innerMapFd = bpf_map_get_fd_by_id(innerMapId);
207206
if (innerMapFd < 0) {
208-
return ERR_NOT_FOUND;
207+
return kErrNotFound;
209208
}
210209

211210
ret = bpf_map_delete_elem(innerMapFd, innerKey);
@@ -223,7 +222,7 @@ class BPFWrapper : public BPFWrapperBase {
223222
ebpf_log(logtail::ebpf::eBPFLogType::NAMI_LOG_TYPE_WARN,
224223
"[BPFWrapper][UpdateInnerMapElem] find outter hash map failed for: %s \n",
225224
outterMapName.c_str());
226-
return ERR_NOT_FOUND;
225+
return kErrNotFound;
227226
}
228227
int innerMapFd = -1;
229228
uint32_t innerMapId = 0;
@@ -252,7 +251,7 @@ class BPFWrapper : public BPFWrapperBase {
252251
ebpf_log(logtail::ebpf::eBPFLogType::NAMI_LOG_TYPE_WARN,
253252
"[BPFWrapper][UpdateInnerMapElem] failed to create inner map for outter map: %s \n",
254253
outterMapName.c_str());
255-
return ERR_LIBBPF;
254+
return kErrLibbpf;
256255
}
257256

258257
int* key = static_cast<int*>(outterKey);
@@ -275,7 +274,7 @@ class BPFWrapper : public BPFWrapperBase {
275274
ebpf_log(logtail::ebpf::eBPFLogType::NAMI_LOG_TYPE_WARN,
276275
"[BPFWrapper][UpdateInnerMapElem] failed to find inner map fd by id for outter map: %s \n",
277276
outterMapName.c_str());
278-
return ERR_NOT_FOUND;
277+
return kErrNotFound;
279278
}
280279

281280
ret = bpf_map_update_elem(innerMapFd, innerKey, innerValue, flag);
@@ -297,7 +296,7 @@ class BPFWrapper : public BPFWrapperBase {
297296
ebpf_log(logtail::ebpf::eBPFLogType::NAMI_LOG_TYPE_DEBUG,
298297
"[BPFWrapper][UpdateBPFHashMap] find hash map failed for: %s \n",
299298
mapName.c_str());
300-
return ERR_NOT_FOUND;
299+
return kErrNotFound;
301300
}
302301
return bpf_map_update_elem(mapFd, key, value, flag);
303302
}
@@ -315,7 +314,7 @@ class BPFWrapper : public BPFWrapperBase {
315314
ebpf_log(logtail::ebpf::eBPFLogType::NAMI_LOG_TYPE_DEBUG,
316315
"[BPFWrapper][LookupBPFHashMap] find hash map failed for: %s \n",
317316
mapName.c_str());
318-
return ERR_NOT_FOUND;
317+
return kErrNotFound;
319318
}
320319
return bpf_map_lookup_elem(mapFd, key, value);
321320
}
@@ -333,7 +332,7 @@ class BPFWrapper : public BPFWrapperBase {
333332
ebpf_log(logtail::ebpf::eBPFLogType::NAMI_LOG_TYPE_DEBUG,
334333
"[BPFWrapper][RemoveBPFHashMap] find hash map failed for: %s \n",
335334
mapName.c_str());
336-
return ERR_NOT_FOUND;
335+
return kErrNotFound;
337336
}
338337
bpf_map_delete_elem(mapFd, key);
339338
return 0;
@@ -415,7 +414,7 @@ class BPFWrapper : public BPFWrapperBase {
415414
int SearchProgFd(const std::string& name) {
416415
auto it = mBpfProgs.find(name);
417416
if (it == mBpfProgs.end()) {
418-
return ERR_NOT_FOUND;
417+
return kErrNotFound;
419418
}
420419

421420
return bpf_program__fd(it->second);
@@ -424,7 +423,7 @@ class BPFWrapper : public BPFWrapperBase {
424423
int SearchMapFd(const std::string& name) {
425424
auto it = mBpfMaps.find(name);
426425
if (it == mBpfMaps.end()) {
427-
return ERR_NOT_FOUND;
426+
return kErrNotFound;
428427
}
429428

430429
return bpf_map__fd(it->second);

Diff for: core/ebpf/include/export.h

+5-7
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ extern "C" {
1212
#include <coolbpf/net.h>
1313
}
1414

15-
#define ERR_DRIVER_INTERNAL 1
16-
#define ERR_DRIVER_INVALID_PARAM 2
1715

18-
namespace logtail {
19-
namespace ebpf {
16+
inline constexpr int kErrDriverInternal = 1;
17+
inline constexpr int kErrDriverInvalidParam = 2;
18+
19+
namespace logtail::ebpf {
2020

2121
using PerfBufferSampleHandler = void (*)(void* ctx, int cpu, void* data, uint32_t size);
2222
using PerfBufferLostHandler = void (*)(void* ctx, int cpu, unsigned long long cnt);
@@ -151,6 +151,4 @@ struct PluginConfig {
151151
std::variant<NetworkObserveConfig, ProcessConfig, NetworkSecurityConfig, FileSecurityConfig> mConfig;
152152
};
153153

154-
} // namespace ebpf
155-
156-
} // namespace logtail
154+
} // namespace logtail::ebpf

Diff for: core/ebpf/plugin/network_observer/NetworkObserverManager.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1314,7 +1314,7 @@ int NetworkObserverManager::Init(const std::variant<SecurityOptions*, ObserverNe
13141314
config.mEnableCidFilter = true;
13151315
std::shared_ptr<ScheduleConfig> config
13161316
= std::make_shared<NetworkObserverScheduleConfig>(std::chrono::seconds(5), JobType::HOST_META_UPDATE);
1317-
ScheduleNext(std::chrono::steady_clock::now(), config);
1317+
ScheduleNext(now, config);
13181318
}
13191319

13201320
pc->mConfig = config;

0 commit comments

Comments
 (0)