I am passing in Strings from userspace to my BPF code and was wondering if it's possible to go beyond the size limit that is possible from my char struct array. Is it possible to put in my lines one by one to a Map and bypass the stack size limit? The way I am passing in my strings through Python is here:
import ctypes
from bcc import BPF
b = BPF(src_file="TP-4091lite.c")
lookupTable = b["lookupTable"]
#add hello.csv to the lookupTable array
f = open("hello.csv","r")
contents = f.readlines()
for i in range(0,len(contents)):
string = contents[i].encode('utf-8')
print(len(string))
lookupTable[ctypes.c_int(i)] = ctypes.create_string_buffer(string, len(string))
f.close()
b.attach_kprobe(event=b.get_syscall_fnname("clone"), fn_name="TP4091")
b.trace_print()
Basically printing whatever is in my CSV line by line and putting it in my BPF Map. And my C code is here:
#include <uapi/linux/bpf.h>
#define ARRAYSIZE 64
struct data_t {
char buf[ARRAYSIZE];
};
struct char_t {
char c[ARRAYSIZE];
};
BPF_ARRAY(lookupTable, struct data_t, ARRAYSIZE);
BPF_ARRAY(characterTable, struct char_t, ARRAYSIZE);
//find substring in a string
static bool isSubstring(struct data_t stringVal)
{
char substring[] = "New York";
int M = sizeof(substring) - 1;
int N = sizeof(stringVal.buf) - 1;
/* A loop to slide pat[] one by one */
for (int i = 0; i <= N - M; i++) {
int j;
/* For current index i, check for
pattern match */
for (j = 0; j < M; j++) {
if (stringVal.buf[i + j] != substring[j]){
break;
}
}
if (j == M) {
return true;
}
}
return false;
}
int TP4091(void *ctx)
{
for (int i = 0; i < ARRAYSIZE; i++) {
char name[20];
int k = i;
struct data_t *line = lookupTable.lookup(&k);
if (line) {
// bpf_trace_printk("%s\n", key->buf);
if (isSubstring(*line) == true) {
bpf_trace_printk("%s\n", line->buf);
}
}
}
return 0;
}
There is still a size limit when putting in arbitrary values and I want to see how far I can push.