Skip to content

Commit

Permalink
drivers: ethernet: Fix eth_ivshmem shared memory mapping
Browse files Browse the repository at this point in the history
This driver assumed the ivshmem-v2 output sections would be mapped
contiguously, which is no longer true.

Modify eth_ivshmem to treat each output section independently

Signed-off-by: Grant Ramsay <[email protected]>
(cherry picked from commit 82644a3)
  • Loading branch information
gramsay0 authored and github-actions[bot] committed Nov 27, 2023
1 parent af314ff commit a46a7b9
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 24 deletions.
11 changes: 6 additions & 5 deletions drivers/ethernet/eth_ivshmem.c
Original file line number Diff line number Diff line change
Expand Up @@ -297,14 +297,15 @@ int eth_ivshmem_initialize(const struct device *dev)
}
dev_data->peer_id = (id == 0) ? 1 : 0;

bool tx_buffer_first = id == 0;
uintptr_t output_section_addr;
uintptr_t output_sections[2];
size_t output_section_size = ivshmem_get_output_mem_section(
cfg_data->ivshmem, 0, &output_section_addr);
cfg_data->ivshmem, 0, &output_sections[0]);
ivshmem_get_output_mem_section(
cfg_data->ivshmem, 1, &output_sections[1]);

res = eth_ivshmem_queue_init(
&dev_data->ivshmem_queue, output_section_addr,
output_section_size, tx_buffer_first);
&dev_data->ivshmem_queue, output_sections[id],
output_sections[dev_data->peer_id], output_section_size);
if (res != 0) {
LOG_ERR("Failed to init ivshmem queue");
return res;
Expand Down
4 changes: 2 additions & 2 deletions drivers/ethernet/eth_ivshmem_priv.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ struct eth_ivshmem_queue {
};

int eth_ivshmem_queue_init(
struct eth_ivshmem_queue *q, uintptr_t shmem,
size_t shmem_section_size, bool tx_buffer_first);
struct eth_ivshmem_queue *q, uintptr_t tx_shmem,
uintptr_t rx_shmem, size_t shmem_section_size);
void eth_ivshmem_queue_reset(struct eth_ivshmem_queue *q);
int eth_ivshmem_queue_tx_get_buff(struct eth_ivshmem_queue *q, void **data, size_t len);
int eth_ivshmem_queue_tx_commit_buff(struct eth_ivshmem_queue *q);
Expand Down
14 changes: 4 additions & 10 deletions drivers/ethernet/eth_ivshmem_queue.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ static int tx_clean_used(struct eth_ivshmem_queue *q);
static int get_rx_avail_desc_idx(struct eth_ivshmem_queue *q, uint16_t *avail_desc_idx);

int eth_ivshmem_queue_init(
struct eth_ivshmem_queue *q, uintptr_t shmem,
size_t shmem_section_size, bool tx_buffer_first)
struct eth_ivshmem_queue *q, uintptr_t tx_shmem,
uintptr_t rx_shmem, size_t shmem_section_size)
{
memset(q, 0, sizeof(*q));

Expand All @@ -44,14 +44,8 @@ int eth_ivshmem_queue_init(
q->desc_max_len = vring_desc_len;
q->vring_data_max_len = shmem_section_size - vring_header_size;
q->vring_header_size = vring_header_size;

if (tx_buffer_first) {
q->tx.shmem = (void *)shmem;
q->rx.shmem = (void *)(shmem + shmem_section_size);
} else {
q->rx.shmem = (void *)shmem;
q->tx.shmem = (void *)(shmem + shmem_section_size);
}
q->tx.shmem = (void *)tx_shmem;
q->rx.shmem = (void *)rx_shmem;

/* Init vrings */
vring_init(&q->tx.vring, vring_desc_len, q->tx.shmem, ETH_IVSHMEM_VRING_ALIGNMENT);
Expand Down
19 changes: 12 additions & 7 deletions tests/drivers/ethernet/eth_ivshmem_queue/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,21 @@
#define VRING_DATA_MAX_LEN 2304

static struct eth_ivshmem_queue q1, q2;
static uint8_t shmem_buff[SHMEM_SECTION_SIZE * 2];
static uint8_t shmem_buff[2][SHMEM_SECTION_SIZE] __aligned(KB(4));
static const void *rx_message;
static size_t rx_len;

static void test_init_queues(void)
{
int res = eth_ivshmem_queue_init(&q1, (uintptr_t)shmem_buff, SHMEM_SECTION_SIZE, true);
int res;

res = eth_ivshmem_queue_init(
&q1, (uintptr_t)shmem_buff[0],
(uintptr_t)shmem_buff[1], SHMEM_SECTION_SIZE);
zassert_ok(res);
res = eth_ivshmem_queue_init(&q2, (uintptr_t)shmem_buff, SHMEM_SECTION_SIZE, false);
res = eth_ivshmem_queue_init(
&q2, (uintptr_t)shmem_buff[1],
(uintptr_t)shmem_buff[0], SHMEM_SECTION_SIZE);
zassert_ok(res);
}

Expand Down Expand Up @@ -55,10 +60,10 @@ ZTEST(eth_ivshmem_queue_tests, test_init)
zassert_equal(q1.desc_max_len, VRING_DESC_LEN);
zassert_equal(q1.vring_header_size, VRING_HEADER_SIZE);
zassert_equal(q1.vring_data_max_len, VRING_DATA_MAX_LEN);
zassert_equal_ptr(q1.tx.shmem, shmem_buff);
zassert_equal_ptr(q1.rx.shmem, shmem_buff + SHMEM_SECTION_SIZE);
zassert_equal_ptr(q2.tx.shmem, shmem_buff + SHMEM_SECTION_SIZE);
zassert_equal_ptr(q2.rx.shmem, shmem_buff);
zassert_equal_ptr(q1.tx.shmem, shmem_buff[0]);
zassert_equal_ptr(q1.rx.shmem, shmem_buff[1]);
zassert_equal_ptr(q2.tx.shmem, shmem_buff[1]);
zassert_equal_ptr(q2.rx.shmem, shmem_buff[0]);
}

ZTEST(eth_ivshmem_queue_tests, test_simple_send_receive)
Expand Down

0 comments on commit a46a7b9

Please sign in to comment.