Date created: Saturday, April 30, 2016 4:30:34 PM. Last modified: Friday, January 3, 2020 3:34:58 PM

Netmap Notes

Ubuntu 14 install:

uname -a
Linux ubuntu-test 3.13.0-55-generic #94-Ubuntu SMP Thu Jun 18 00:28:41 UTC 2015 i686 i686 i686 GNU/Linux

sudo apt-get install linux-headers-3.13.0-55
sudo apt-get install linux-source-3.13.0

$ ./configure --no-drivers
kernel directory            /lib/modules/3.13.0-55-generic/build
linux version               30d0b  [3.13.11]
module file                 netmap.ko
subsystems                  generic monitor pipe vale
no-drivers

make
make apps
sudo make install

sudo insmod ./netmap.ko
lsmod | grep netmap.ko

Hello World, dump packets as hex to the terminal:

// Basic Netmap Receiver
// James Bensley 2016-05

#define NETMAP_WITH_LIBS
#include <net/netmap_user.h>
#include <sys/poll.h>

int main()
{

	struct	nm_desc	*nm_desc;
	struct netmap_ring *ring;
	struct	pollfd fds;
	char *nm_pkt_buf;
	uint8_t nm_ring_index, nm_pack_len;

	nm_desc = nm_open("netmap:eth0", NULL, 0, 0);

	while (1) {  

	    	fds.fd	= NETMAP_FD(nm_desc);
		fds.events = POLLIN;
		poll(&fds, 1, -1);

		ring = NETMAP_RXRING(nm_desc->nifp, 0);

		while (!nm_ring_empty(ring)) {
			nm_ring_index = ring->cur;
		        nm_pkt_buf = NETMAP_BUF(ring, ring->slot[nm_ring_index].buf_idx);
		        nm_pack_len = ring->slot[nm_ring_index].len;
	        
			printf("len: %d\n", nm_pack_len);
			for(int x = 0; x<nm_pack_len; x++) {
				printf("%02x ", (unsigned int)nm_pkt_buf[x]);
			}
			printf ("\n");

	        	ring->head = ring->cur = nm_ring_next(ring, nm_ring_index);
		}
	
	}

	return 0;
}

Basic Hello World style traffic generator:

// Basic Netmap Sender
// James Bensley 2016-05

#define NETMAP_WITH_LIBS
#include <net/netmap_user.h>
#include <sys/poll.h>

int main()
{

    struct nm_desc *nm_desc;
    struct netmap_ring *ring;
    struct pollfd fds;
    char *nm_pkt_buf;
    uint8_t nm_ring_index, nm_pack_len;

    nm_desc = nm_open("netmap:eth0", NULL, 0, 0);
    ring = NETMAP_TXRING(nm_desc->nifp, 0);

    fds.fd = NETMAP_FD(nm_desc);
    fds.events = POLLOUT;

    char payload[64] = {0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x12, 0x34, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40};

    while (1) {  

        poll(&fds, 1, -1);
        
        while (!nm_ring_empty(ring)) {

            nm_ring_index = ring->cur;
            nm_pkt_buf = NETMAP_BUF(ring, ring->slot[nm_ring_index].buf_idx);

            memcpy((void*)nm_pkt_buf, (void*)&payload, sizeof(payload));
            ring->slot[nm_ring_index].len = sizeof(payload);
            ring->head = ring->cur = nm_ring_next(ring, nm_ring_index);

        }
        
    }

 return 0;

}

Previous page: Linux Debug Levels
Next page: Sizeof Reference