Skip to content

Commit e3d54ff

Browse files
justinemariechanglan
authored andcommitted
Make BPF compatible with 32-bit deployments.
1 parent 41fd086 commit e3d54ff

2 files changed

Lines changed: 43 additions & 8 deletions

File tree

core/modules/bpf.cc

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@
3333

3434
#include "bpf.h"
3535

36-
#include <pcap.h>
3736
#include <sys/mman.h>
3837

3938
#include <cstdint>
4039
#include <cstdlib>
4140
#include <cstring>
4241

42+
#ifdef __x86_64 // JIT compilation code only works in 64-bit
4343
/*
4444
* Registers
4545
*/
@@ -1088,6 +1088,7 @@ static bpf_filter_func_t bpf_jit_compile(struct bpf_insn *prog, u_int nins,
10881088

10891089
return (reinterpret_cast<bpf_filter_func_t>(stream.ibuf));
10901090
}
1091+
#endif
10911092

10921093
/* -------------------------------------------------------------------------
10931094
* Module code begins from here
@@ -1119,7 +1120,11 @@ CommandResponse BPF::Init(const bess::pb::BPFArg &arg) {
11191120

11201121
void BPF::DeInit() {
11211122
for (int i = 0; i < n_filters_; i++) {
1123+
#ifdef __x86_64
11221124
munmap(reinterpret_cast<void *>(filters_[i].func), filters_[i].mmap_size);
1125+
#else
1126+
pcap_freecode(&(filters_[i].il_code));
1127+
#endif
11231128
free(filters_[i].exp);
11241129
}
11251130

@@ -1132,29 +1137,37 @@ CommandResponse BPF::CommandAdd(const bess::pb::BPFArg &arg) {
11321137
}
11331138

11341139
struct filter *filter = &filters_[n_filters_];
1135-
struct bpf_program il_code;
11361140

11371141
for (const auto &f : arg.filters()) {
11381142
const char *exp = f.filter().c_str();
11391143
int64_t gate = f.gate();
11401144
if (gate < 0 || gate >= MAX_GATES) {
11411145
return CommandFailure(EINVAL, "Invalid gate");
11421146
}
1147+
filter->priority = f.priority();
1148+
filter->gate = f.gate();
1149+
filter->exp = strdup(exp);
1150+
#ifdef __x86_64
1151+
struct bpf_program il_code;
11431152
if (pcap_compile_nopcap(SNAPLEN, DLT_EN10MB, // Ethernet
11441153
&il_code, exp, 1, // optimize (IL only)
11451154
PCAP_NETMASK_UNKNOWN) == -1) {
11461155
return CommandFailure(EINVAL, "BPF compilation error");
11471156
}
1148-
filter->priority = f.priority();
1149-
filter->gate = f.gate();
1150-
filter->exp = strdup(exp);
11511157
filter->func =
11521158
bpf_jit_compile(il_code.bf_insns, il_code.bf_len, &filter->mmap_size);
11531159
pcap_freecode(&il_code);
11541160
if (!filter->func) {
11551161
free(filter->exp);
11561162
return CommandFailure(ENOMEM, "BPF JIT compilation error");
11571163
}
1164+
#else
1165+
if (pcap_compile_nopcap(SNAPLEN, DLT_EN10MB, // Ethernet
1166+
&(filter->il_code), exp, 1, // optimize (IL only)
1167+
PCAP_NETMASK_UNKNOWN) == -1) {
1168+
return CommandFailure(EINVAL, "BPF compilation error");
1169+
}
1170+
#endif
11581171
n_filters_++;
11591172
qsort(filters_, n_filters_, sizeof(struct filter), &compare_filter);
11601173

@@ -1185,8 +1198,17 @@ inline void BPF::process_batch_1filter(bess::PacketBatch *batch) {
11851198
int ret;
11861199
int idx;
11871200

1201+
#ifdef __x86_64
11881202
ret = filter->func(pkt->head_data<uint8_t *>(), pkt->total_len(),
11891203
pkt->head_len());
1204+
#else
1205+
ret = bpf_filter(filter->il_code.bf_insns, pkt->head_data<uint8_t *>(),
1206+
pkt->total_len(), pkt->head_len());
1207+
1208+
if (ret != 0) {
1209+
ret = 1;
1210+
}
1211+
#endif
11901212

11911213
idx = ret & 1;
11921214
*(ptrs[idx]++) = pkt;
@@ -1227,11 +1249,19 @@ void BPF::ProcessBatch(bess::PacketBatch *batch) {
12271249
gate_idx_t gate = 0; /* default gate for unmatched pkts */
12281250

12291251
for (int j = 0; j < n_filters; j++, filter++) {
1252+
#ifdef __x86_64
12301253
if (filter->func(pkt->head_data<uint8_t *>(), pkt->total_len(),
12311254
pkt->head_len()) != 0) {
12321255
gate = filter->gate;
12331256
break;
12341257
}
1258+
#else
1259+
if (bpf_filter(filter->il_code.bf_insns, pkt->head_data<uint8_t *>(),
1260+
pkt->total_len(), pkt->head_len())) {
1261+
gate = filter->gate;
1262+
break;
1263+
}
1264+
#endif
12351265
}
12361266

12371267
out_gates[i] = gate;

core/modules/bpf.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,21 @@
33

44
#include "../module.h"
55
#include "../module_msg.pb.h"
6-
6+
#include <pcap.h>
77
#define MAX_FILTERS 128
88

9+
#ifdef __x86_64
910
typedef u_int (*bpf_filter_func_t)(u_char *, u_int, u_int);
11+
#endif
1012

1113
struct filter {
14+
#ifdef __x86_64
1215
bpf_filter_func_t func;
13-
int gate;
14-
1516
size_t mmap_size; /* needed for munmap() */
17+
#else
18+
bpf_program il_code;
19+
#endif
20+
int gate;
1621
int priority; /* higher number == higher priority */
1722
char *exp; /* original filter expression string */
1823
};

0 commit comments

Comments
 (0)