xref: /freebsd/lib/libnetmap/nmctx.c (revision 22d7dd834bc5cd189810e414701e3ad1e98102e4)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (C) 2018 Universita` di Pisa
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  *   1. Redistributions of source code must retain the above copyright
12  *      notice, this list of conditions and the following disclaimer.
13  *   2. Redistributions in binary form must reproduce the above copyright
14  *      notice, this list of conditions and the following disclaimer in the
15  *      documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <sys/ioctl.h>
33 #include <sys/mman.h>
34 #include <fcntl.h>
35 #include <stdlib.h>
36 #include <stdio.h>
37 #include <stdarg.h>
38 #include <string.h>
39 #include <unistd.h>
40 #include <errno.h>
41 #include <net/netmap_user.h>
42 #define LIBNETMAP_NOTHREADSAFE
43 #include "libnetmap.h"
44 
45 static void
46 nmctx_default_error(struct nmctx *ctx, const char *errmsg)
47 {
48 	(void)ctx;
49 	fprintf(stderr, "%s\n", errmsg);
50 }
51 
52 static void *
53 nmctx_default_malloc(struct nmctx *ctx, size_t sz)
54 {
55 	(void)ctx;
56 	return malloc(sz);
57 }
58 
59 static void
60 nmctx_default_free(struct nmctx *ctx, void *p)
61 {
62 	(void)ctx;
63 	free(p);
64 }
65 
66 static struct nmctx nmctx_global = {
67 	.verbose = 1,
68 	.error = nmctx_default_error,
69 	.malloc = nmctx_default_malloc,
70 	.free = nmctx_default_free,
71 	.lock = NULL,
72 };
73 
74 static struct nmctx *nmctx_default = &nmctx_global;
75 
76 struct nmctx *
77 nmctx_get(void)
78 {
79 	return nmctx_default;
80 }
81 
82 struct nmctx *
83 nmctx_set_default(struct nmctx *ctx)
84 {
85 	struct nmctx *old = nmctx_default;
86 	nmctx_default = ctx;
87 	return old;
88 }
89 
90 #define MAXERRMSG 1000
91 void
92 nmctx_ferror(struct nmctx *ctx, const char *fmt, ...)
93 {
94 	char errmsg[MAXERRMSG];
95 	va_list ap;
96 	int rv;
97 
98 	if (!ctx->verbose)
99 		return;
100 
101 	va_start(ap, fmt);
102 	rv = vsnprintf(errmsg, MAXERRMSG, fmt, ap);
103 	va_end(ap);
104 
105 	if (rv > 0) {
106 		if (rv < MAXERRMSG) {
107 			ctx->error(ctx, errmsg);
108 		} else {
109 			ctx->error(ctx, "error message too long");
110 		}
111 	} else {
112 		ctx->error(ctx, "internal error");
113 	}
114 }
115 
116 void *
117 nmctx_malloc(struct nmctx *ctx, size_t sz)
118 {
119 	return ctx->malloc(ctx, sz);
120 }
121 
122 void
123 nmctx_free(struct nmctx *ctx, void *p)
124 {
125 	ctx->free(ctx, p);
126 }
127 
128 void
129 nmctx_lock(struct nmctx *ctx)
130 {
131 	if (ctx->lock != NULL)
132 		ctx->lock(ctx, 1);
133 }
134 
135 void
136 nmctx_unlock(struct nmctx *ctx)
137 {
138 	if (ctx->lock != NULL)
139 		ctx->lock(ctx, 0);
140 }
141