Skip to content

Commit

Permalink
refactor(bpf): split even more bpf_parse_readv_writev_bufs
Browse files Browse the repository at this point in the history
Signed-off-by: therealbobo <[email protected]>
  • Loading branch information
therealbobo committed May 9, 2024
1 parent c2a04b0 commit f561834
Showing 1 changed file with 39 additions and 39 deletions.
78 changes: 39 additions & 39 deletions driver/bpf/fillers.h
Original file line number Diff line number Diff line change
Expand Up @@ -575,12 +575,12 @@ static __always_inline int _bpf_parse_readv_writev_bufs(struct filler_data *data
const void __user *iovsrc,
unsigned long iovcnt,
long retval,
int flags)
int flags,
long *size)
{
const struct iovec *iov;
int res = PPM_SUCCESS;
unsigned int copylen;
long size = 0;
int j;

copylen = iovcnt * sizeof(struct iovec);
Expand Down Expand Up @@ -608,25 +608,20 @@ static __always_inline int _bpf_parse_readv_writev_bufs(struct filler_data *data
if (j == iovcnt)
break;
// BPF seems to require a hard limit to avoid overflows
if (size == LONG_MAX)
if (*size == LONG_MAX)
break;

size += iov[j].iov_len;
*size += iov[j].iov_len;
}

if ((flags & PRB_FLAG_IS_WRITE) == 0)
if (size > retval)
size = retval;

if (flags & PRB_FLAG_PUSH_SIZE) {
res = bpf_push_u32_to_ring(data, (uint32_t)size);
CHECK_RES(res);
}
if (*size > retval)
*size = retval;

if (flags & PRB_FLAG_PUSH_DATA) {
if (size > 0) {
if (*size > 0) {
unsigned long off = _READ(data->state->tail_ctx.curoff);
unsigned long remaining = size;
unsigned long remaining = *size;
int j;

#pragma unroll
Expand Down Expand Up @@ -664,12 +659,9 @@ static __always_inline int _bpf_parse_readv_writev_bufs(struct filler_data *data
off += to_read;
}
} else {
size = 0;
*size = 0;
}

data->fd = bpf_syscall_get_argument(data, 0);
data->curarg_already_on_frame = true;
return __bpf_val_to_ring(data, 0, size, PT_BYTEBUF, -1, true, KERNEL);
return PPM_SUCCESS;
}

return res;
Expand All @@ -680,12 +672,12 @@ static __always_inline int _bpf_parse_readv_writev_bufs_ia32(struct filler_data
const void __user *iovsrc,
unsigned long iovcnt,
long retval,
int flags)
int flags,
long *size)
{
const struct compat_iovec *compat_iov;
int res = PPM_SUCCESS;
unsigned int copylen;
long size = 0;
int j;

copylen = iovcnt * sizeof(struct compat_iovec);
Expand Down Expand Up @@ -713,29 +705,26 @@ static __always_inline int _bpf_parse_readv_writev_bufs_ia32(struct filler_data
if (j == iovcnt)
break;
// BPF seems to require a hard limit to avoid overflows
if (size == LONG_MAX)
if (*size == LONG_MAX)
break;

size += compat_iov[j].iov_len;
*size += compat_iov[j].iov_len;
}

if ((flags & PRB_FLAG_IS_WRITE) == 0)
if (size > retval)
size = retval;

if (flags & PRB_FLAG_PUSH_SIZE) {
res = bpf_push_u32_to_ring(data, (uint32_t)size);
CHECK_RES(res);
}
if (*size > retval)
*size = retval;

if (flags & PRB_FLAG_PUSH_DATA) {
if (size > 0) {
if (*size > 0) {
unsigned long off = _READ(data->state->tail_ctx.curoff);
unsigned long remaining = size;
unsigned long remaining = *size;
int j;

#pragma unroll
for (j = 0; j < MAX_IOVCNT; ++j) {
// The 14 iovec count limit is due to old kernels verifiers
// complaining.
for (j = 0; j < 14; ++j) {
volatile unsigned int to_read;

if (j == iovcnt)
Expand Down Expand Up @@ -769,14 +758,11 @@ static __always_inline int _bpf_parse_readv_writev_bufs_ia32(struct filler_data
off += to_read;
}
} else {
size = 0;
*size = 0;
}

data->fd = bpf_syscall_get_argument(data, 0);
data->curarg_already_on_frame = true;
return __bpf_val_to_ring(data, 0, size, PT_BYTEBUF, -1, true, KERNEL);
return PPM_SUCCESS;
}

return res;
}
#endif
Expand All @@ -787,18 +773,32 @@ static __always_inline int bpf_parse_readv_writev_bufs(struct filler_data *data,
long retval,
int flags)
{
long size = 0;
int res = PPM_SUCCESS;
#if defined(CONFIG_X86_64)
if (!bpf_in_ia32_syscall())
{
#endif
return _bpf_parse_readv_writev_bufs(data, iovsrc, iovcnt, retval, flags);
res = _bpf_parse_readv_writev_bufs(data, iovsrc, iovcnt, retval, flags, &size);
#if defined(CONFIG_X86_64)
}
else
{
return _bpf_parse_readv_writev_bufs_ia32(data, iovsrc, iovcnt, retval, flags);
res = _bpf_parse_readv_writev_bufs_ia32(data, iovsrc, iovcnt, retval, flags, &size);
}
#endif
if (flags & PRB_FLAG_PUSH_SIZE && res == PPM_SUCCESS) {
res = bpf_push_u32_to_ring(data, (uint32_t)size);
CHECK_RES(res);
}

if(flags & PRB_FLAG_PUSH_DATA && res == PPM_SUCCESS)
{
data->fd = bpf_syscall_get_argument(data, 0);
data->curarg_already_on_frame = true;
return __bpf_val_to_ring(data, 0, size, PT_BYTEBUF, -1, true, KERNEL);
}
return res;
}

FILLER(sys_readv_e, true)
Expand Down

0 comments on commit f561834

Please sign in to comment.