Vincenzo Maffione 5c4f8d801c lib: add libnetmap
This changeset introduces the new libnetmap library for writing
netmap applications.
Before libnetmap, applications could either use the kernel API
directly (e.g. NIOCREGIF/NIOCCTRL) or the simple header-only-library
netmap_user.h (e.g. nm_open(), nm_close(), nm_mmap() etc.)

The new library offers more functionalities than netmap_user.h:
  - Support for complex netmap options, such as external memory
    allocators or per-buffer offsets. This opens the way to future
    extensions.
  - More flexibility in the netmap port bind options, such as
    non-numeric names for pipes, or the ability to specify the netmap
    allocator that must be used for a given port.
  - Automatic tracking of the netmap memory regions in use across the
    open ports.

At the moment there is no man page, but the libnetmap.h header file
has in-depth documentation.

Reviewed by:	hrs
MFC after:	2 weeks
Differential Revision:	https://reviews.freebsd.org/D26171
2020-08-28 20:03:54 +00:00

112 lines
1.7 KiB
C

/* $FreeBSD$ */
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <net/netmap_user.h>
#define LIBNETMAP_NOTHREADSAFE
#include "libnetmap.h"
static void
nmctx_default_error(struct nmctx *ctx, const char *errmsg)
{
fprintf(stderr, "%s\n", errmsg);
}
static void *
nmctx_default_malloc(struct nmctx *ctx, size_t sz)
{
(void)ctx;
return malloc(sz);
}
static void
nmctx_default_free(struct nmctx *ctx, void *p)
{
(void)ctx;
free(p);
}
static struct nmctx nmctx_global = {
.verbose = 1,
.error = nmctx_default_error,
.malloc = nmctx_default_malloc,
.free = nmctx_default_free,
.lock = NULL,
};
static struct nmctx *nmctx_default = &nmctx_global;
struct nmctx *
nmctx_get(void)
{
return nmctx_default;
}
struct nmctx *
nmctx_set_default(struct nmctx *ctx)
{
struct nmctx *old = nmctx_default;
nmctx_default = ctx;
return old;
}
#define MAXERRMSG 1000
void
nmctx_ferror(struct nmctx *ctx, const char *fmt, ...)
{
char errmsg[MAXERRMSG];
va_list ap;
int rv;
if (!ctx->verbose)
return;
va_start(ap, fmt);
rv = vsnprintf(errmsg, MAXERRMSG, fmt, ap);
va_end(ap);
if (rv > 0) {
if (rv < MAXERRMSG) {
ctx->error(ctx, errmsg);
} else {
ctx->error(ctx, "error message too long");
}
} else {
ctx->error(ctx, "internal error");
}
}
void *
nmctx_malloc(struct nmctx *ctx, size_t sz)
{
return ctx->malloc(ctx, sz);
}
void
nmctx_free(struct nmctx *ctx, void *p)
{
ctx->free(ctx, p);
}
void
nmctx_lock(struct nmctx *ctx)
{
if (ctx->lock != NULL)
ctx->lock(ctx, 1);
}
void
nmctx_unlock(struct nmctx *ctx)
{
if (ctx->lock != NULL)
ctx->lock(ctx, 0);
}