mirror of
https://passt.top/passt
synced 2025-08-19 04:43:13 +02:00
packet: Don't pass start and offset separately to packet_check_range()
Fundamentally what packet_check_range() does is to check whether a given memory range is within the allowed / expected memory set aside for packets from a particular pool. That range could represent a whole packet (from packet_add_do()) or part of a packet (from packet_get_do()), but it doesn't really matter which. However, we pass the start of the range as two parameters: @start which is the start of the packet, and @offset which is the offset within the packet of the range we're interested in. We never use these separately, only as (start + offset). Simplify the interface of packet_check_range() and vu_packet_check_range() to directly take the start of the relevant range. This will allow some additional future improvements. Signed-off-by: David Gibson <david@gibson.dropbear.id.au> Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
This commit is contained in:
parent
0a51060f7a
commit
354bc0bab1
3 changed files with 24 additions and 26 deletions
36
packet.c
36
packet.c
|
@ -23,23 +23,22 @@
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* packet_check_range() - Check if a packet memory range is valid
|
* packet_check_range() - Check if a memory range is valid for a pool
|
||||||
* @p: Packet pool
|
* @p: Packet pool
|
||||||
* @offset: Offset of data range in packet descriptor
|
* @ptr: Start of desired data range
|
||||||
* @len: Length of desired data range
|
* @len: Length of desired data range
|
||||||
* @start: Start of the packet descriptor
|
|
||||||
* @func: For tracing: name of calling function
|
* @func: For tracing: name of calling function
|
||||||
* @line: For tracing: caller line of function call
|
* @line: For tracing: caller line of function call
|
||||||
*
|
*
|
||||||
* Return: 0 if the range is valid, -1 otherwise
|
* Return: 0 if the range is valid, -1 otherwise
|
||||||
*/
|
*/
|
||||||
static int packet_check_range(const struct pool *p, size_t offset, size_t len,
|
static int packet_check_range(const struct pool *p, const char *ptr, size_t len,
|
||||||
const char *start, const char *func, int line)
|
const char *func, int line)
|
||||||
{
|
{
|
||||||
if (p->buf_size == 0) {
|
if (p->buf_size == 0) {
|
||||||
int ret;
|
int ret;
|
||||||
|
|
||||||
ret = vu_packet_check_range((void *)p->buf, offset, len, start);
|
ret = vu_packet_check_range((void *)p->buf, ptr, len);
|
||||||
|
|
||||||
if (ret == -1)
|
if (ret == -1)
|
||||||
trace("cannot find region, %s:%i", func, line);
|
trace("cannot find region, %s:%i", func, line);
|
||||||
|
@ -47,16 +46,16 @@ static int packet_check_range(const struct pool *p, size_t offset, size_t len,
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (start < p->buf) {
|
if (ptr < p->buf) {
|
||||||
trace("packet start %p before buffer start %p, "
|
trace("packet range start %p before buffer start %p, %s:%i",
|
||||||
"%s:%i", (void *)start, (void *)p->buf, func, line);
|
(void *)ptr, (void *)p->buf, func, line);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (start + len + offset > p->buf + p->buf_size) {
|
if (ptr + len > p->buf + p->buf_size) {
|
||||||
trace("packet offset plus length %zu from size %zu, "
|
trace("packet range end %p after buffer end %p, %s:%i",
|
||||||
"%s:%i", start - p->buf + len + offset,
|
(void *)(ptr + len), (void *)(p->buf + p->buf_size),
|
||||||
p->buf_size, func, line);
|
func, line);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -81,7 +80,7 @@ void packet_add_do(struct pool *p, size_t len, const char *start,
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (packet_check_range(p, 0, len, start, func, line))
|
if (packet_check_range(p, start, len, func, line))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (len > UINT16_MAX) {
|
if (len > UINT16_MAX) {
|
||||||
|
@ -110,6 +109,8 @@ void packet_add_do(struct pool *p, size_t len, const char *start,
|
||||||
void *packet_get_do(const struct pool *p, size_t idx, size_t offset,
|
void *packet_get_do(const struct pool *p, size_t idx, size_t offset,
|
||||||
size_t len, size_t *left, const char *func, int line)
|
size_t len, size_t *left, const char *func, int line)
|
||||||
{
|
{
|
||||||
|
char *ptr;
|
||||||
|
|
||||||
if (idx >= p->size || idx >= p->count) {
|
if (idx >= p->size || idx >= p->count) {
|
||||||
if (func) {
|
if (func) {
|
||||||
trace("packet %zu from pool size: %zu, count: %zu, "
|
trace("packet %zu from pool size: %zu, count: %zu, "
|
||||||
|
@ -135,14 +136,15 @@ void *packet_get_do(const struct pool *p, size_t idx, size_t offset,
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (packet_check_range(p, offset, len, p->pkt[idx].iov_base,
|
ptr = (char *)p->pkt[idx].iov_base + offset;
|
||||||
func, line))
|
|
||||||
|
if (packet_check_range(p, ptr, len, func, line))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
if (left)
|
if (left)
|
||||||
*left = p->pkt[idx].iov_len - offset - len;
|
*left = p->pkt[idx].iov_len - offset - len;
|
||||||
|
|
||||||
return (char *)p->pkt[idx].iov_base + offset;
|
return ptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
3
packet.h
3
packet.h
|
@ -24,8 +24,7 @@ struct pool {
|
||||||
struct iovec pkt[];
|
struct iovec pkt[];
|
||||||
};
|
};
|
||||||
|
|
||||||
int vu_packet_check_range(void *buf, size_t offset, size_t len,
|
int vu_packet_check_range(void *buf, const char *ptr, size_t len);
|
||||||
const char *start);
|
|
||||||
void packet_add_do(struct pool *p, size_t len, const char *start,
|
void packet_add_do(struct pool *p, size_t len, const char *start,
|
||||||
const char *func, int line);
|
const char *func, int line);
|
||||||
void *packet_get_do(const struct pool *p, const size_t idx,
|
void *packet_get_do(const struct pool *p, const size_t idx,
|
||||||
|
|
11
vu_common.c
11
vu_common.c
|
@ -26,14 +26,12 @@
|
||||||
* vu_packet_check_range() - Check if a given memory zone is contained in
|
* vu_packet_check_range() - Check if a given memory zone is contained in
|
||||||
* a mapped guest memory region
|
* a mapped guest memory region
|
||||||
* @buf: Array of the available memory regions
|
* @buf: Array of the available memory regions
|
||||||
* @offset: Offset of data range in packet descriptor
|
* @ptr: Start of desired data range
|
||||||
* @size: Length of desired data range
|
* @size: Length of desired data range
|
||||||
* @start: Start of the packet descriptor
|
|
||||||
*
|
*
|
||||||
* Return: 0 if the zone is in a mapped memory region, -1 otherwise
|
* Return: 0 if the zone is in a mapped memory region, -1 otherwise
|
||||||
*/
|
*/
|
||||||
int vu_packet_check_range(void *buf, size_t offset, size_t len,
|
int vu_packet_check_range(void *buf, const char *ptr, size_t len)
|
||||||
const char *start)
|
|
||||||
{
|
{
|
||||||
struct vu_dev_region *dev_region;
|
struct vu_dev_region *dev_region;
|
||||||
|
|
||||||
|
@ -41,9 +39,8 @@ int vu_packet_check_range(void *buf, size_t offset, size_t len,
|
||||||
/* NOLINTNEXTLINE(performance-no-int-to-ptr) */
|
/* NOLINTNEXTLINE(performance-no-int-to-ptr) */
|
||||||
char *m = (char *)(uintptr_t)dev_region->mmap_addr;
|
char *m = (char *)(uintptr_t)dev_region->mmap_addr;
|
||||||
|
|
||||||
if (m <= start &&
|
if (m <= ptr &&
|
||||||
start + offset + len <= m + dev_region->mmap_offset +
|
ptr + len <= m + dev_region->mmap_offset + dev_region->size)
|
||||||
dev_region->size)
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue