Skip to content

Instantly share code, notes, and snippets.

@pothos
Created August 5, 2018 17:26
Show Gist options
  • Save pothos/998131170b0107f31ad3ce5aba34dd2f to your computer and use it in GitHub Desktop.
Save pothos/998131170b0107f31ad3ce5aba34dd2f to your computer and use it in GitHub Desktop.
Netmap pipe bug test
#define NETMAP_WITH_LIBS
#include <net/netmap_user.h>
#include <sys/poll.h>
void sender(int tx_sync) {
struct nm_desc *d;
struct pollfd fds;
printf("Using %s\n", tx_sync ? "TXSYNC ioctl" : "TX_POLL POLLIN poll");
d = nm_open("netmap:pipe}0", NULL, 0, 0);
fds.fd = NETMAP_FD(d);
fds.events = POLLIN;
while (1) {
int wrote_something = 0;
int ret, i;
for (i = d->first_tx_ring; i <= d->last_tx_ring; i++) {
unsigned int cur, next;
struct netmap_slot *slot;
struct netmap_ring *txring;
char *p;
txring = NETMAP_TXRING(d->nifp, i);
while (!nm_ring_empty(txring)) {
cur = txring->cur;
slot = &txring->slot[cur];
p = NETMAP_BUF(txring, slot->buf_idx);
p[0] = 1; p[79] = 1;
slot->len = 80;
next = nm_ring_next(txring, cur);
txring->head = next;
txring->cur = next;
wrote_something = 1;
}
}
if (!wrote_something && !tx_sync) {
printf("pipe is still full\n");
}
// filled pipe
if (tx_sync) {
ret = ioctl(NETMAP_FD(d), NIOCTXSYNC, NULL);
if (ret < 0) {
printf("ioctl err\n");
break;
}
} else {
ret = poll(&fds, 1, 1000); // sometimes it works after first the TXSYNC version was used
if (ret == 0) {
printf("timeout\n");
} else if (ret == -1) {
printf("poll err\n");
break;
}
}
}
nm_close(d);
}
void main(int argc, char** argv) {
printf("To be started after ./pkt-gen -i pipe{0 -f rx\n");
if (argc != 2) {
printf("Usage: %s p|t\n p: use TX_POLL POLLIN poll for tx\n t: use TXSYNC ioctl\n", argv[0]);
} else {
sender(*argv[1] == 't');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment