2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (C) 2018 Universita` di Pisa
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
32 #include <sys/types.h>
34 #include <sys/ioctl.h>
48 #define NETMAP_WITH_LIBS
49 #define ED(...) D(__VA_ARGS__)
52 /* an identifier is a possibly empty sequence of alphanum characters and
56 nm_is_identifier(const char *s, const char *e)
59 if (!isalnum(*s) && *s != '_') {
66 #endif /* NMREQ_DEBUG */
68 #include <net/netmap_user.h>
69 #define LIBNETMAP_NOTHREADSAFE
70 #include "libnetmap.h"
73 nmreq_push_option(struct nmreq_header *h, struct nmreq_option *o)
75 o->nro_next = h->nr_options;
76 h->nr_options = (uintptr_t)o;
80 const char *prefix; /* the constant part of the prefix */
81 size_t len; /* its strlen() */
83 #define NR_P_ID (1U << 0) /* whether an identifier is needed */
84 #define NR_P_SKIP (1U << 1) /* whether the scope must be passed to netmap */
85 #define NR_P_EMPTYID (1U << 2) /* whether an empty identifier is allowed */
88 #define declprefix(prefix, flags) { (prefix), (sizeof(prefix) - 1), (flags) }
90 static struct nmreq_prefix nmreq_prefixes[] = {
91 declprefix("netmap", NR_P_SKIP),
92 declprefix(NM_BDG_NAME, NR_P_ID|NR_P_EMPTYID),
93 { NULL } /* terminate the list */
97 nmreq_header_init(struct nmreq_header *h, uint16_t reqtype, void *body)
99 memset(h, 0, sizeof(*h));
100 h->nr_version = NETMAP_API;
101 h->nr_reqtype = reqtype;
102 h->nr_body = (uintptr_t)body;
106 nmreq_header_decode(const char **pifname, struct nmreq_header *h, struct nmctx *ctx)
108 const char *scan = NULL;
109 const char *vpname = NULL;
110 const char *pipesep = NULL;
112 const char *ifname = *pifname;
113 struct nmreq_prefix *p;
116 for (p = nmreq_prefixes; p->prefix != NULL; p++) {
117 if (!strncmp(scan, p->prefix, p->len))
120 if (p->prefix == NULL) {
121 nmctx_ferror(ctx, "%s: invalid request, prefix unknown or missing", *pifname);
126 vpname = index(scan, ':');
127 if (vpname == NULL) {
128 nmctx_ferror(ctx, "%s: missing ':'", ifname);
131 if (vpname != scan) {
132 /* there is an identifier, can we accept it? */
133 if (!(p->flags & NR_P_ID)) {
134 nmctx_ferror(ctx, "%s: no identifier allowed between '%s' and ':'", *pifname, p->prefix);
138 if (!nm_is_identifier(scan, vpname)) {
139 nmctx_ferror(ctx, "%s: invalid identifier '%.*s'", *pifname, vpname - scan, scan);
143 if ((p->flags & NR_P_ID) && !(p->flags & NR_P_EMPTYID)) {
144 nmctx_ferror(ctx, "%s: identifier is missing between '%s' and ':'", *pifname, p->prefix);
148 ++vpname; /* skip the colon */
149 if (p->flags & NR_P_SKIP)
153 /* scan for a separator */
154 for (; *scan && !index("-*^/@", *scan); scan++)
157 /* search for possible pipe indicators */
158 for (pipesep = vpname; pipesep != scan && !index("{}", *pipesep); pipesep++)
161 if (!nm_is_identifier(vpname, pipesep)) {
162 nmctx_ferror(ctx, "%s: invalid port name '%.*s'", *pifname,
163 pipesep - vpname, vpname);
166 if (pipesep != scan) {
168 if (*pipesep == '\0') {
169 nmctx_ferror(ctx, "%s: invalid empty pipe name", *pifname);
172 if (!nm_is_identifier(pipesep, scan)) {
173 nmctx_ferror(ctx, "%s: invalid pipe name '%.*s'", *pifname, scan - pipesep, pipesep);
178 namelen = scan - ifname;
179 if (namelen >= sizeof(h->nr_name)) {
180 nmctx_ferror(ctx, "name '%.*s' too long", namelen, ifname);
184 nmctx_ferror(ctx, "%s: invalid empty port name", *pifname);
188 /* fill the header */
189 memcpy(h->nr_name, ifname, namelen);
190 h->nr_name[namelen] = '\0';
191 ED("name %s", h->nr_name);
208 nmreq_get_mem_id(const char **pifname, struct nmctx *ctx)
211 struct nmreq_header gh;
212 struct nmreq_port_info_get gb;
221 /* try to look for a netmap port with this name */
222 fd = open("/dev/netmap", O_RDWR);
224 nmctx_ferror(ctx, "cannot open /dev/netmap: %s", strerror(errno));
227 nmreq_header_init(&gh, NETMAP_REQ_PORT_INFO_GET, &gb);
228 if (nmreq_header_decode(&ifname, &gh, ctx) < 0) {
231 memset(&gb, 0, sizeof(gb));
232 if (ioctl(fd, NIOCCTRL, &gh) < 0) {
233 nmctx_ferror(ctx, "cannot get info for '%s': %s", *pifname, strerror(errno));
250 nmreq_register_decode(const char **pifname, struct nmreq_register *r, struct nmctx *ctx)
252 enum { P_START, P_RNGSFXOK, P_GETNUM, P_FLAGS, P_FLAGSOK, P_MEMID, P_ONESW } p_state;
254 const char *scan = *pifname;
260 /* fill the request */
264 nr_mode = NR_REG_ALL_NIC; /* default for no suffix */
265 nr_mem_id = r->nr_mem_id; /* if non-zero, further updates are disabled */
272 case '^': /* only SW ring */
276 case '*': /* NIC and SW */
277 nr_mode = NR_REG_NIC_SW;
278 p_state = P_RNGSFXOK;
280 case '-': /* one NIC ring pair */
281 nr_mode = NR_REG_ONE_NIC;
284 case '/': /* start of flags */
287 case '@': /* start of memid */
291 nmctx_ferror(ctx, "unknown modifier: '%c'", *scan);
305 nmctx_ferror(ctx, "unexpected character: '%c'", *scan);
311 if (!isdigit(*scan)) {
312 nmctx_ferror(ctx, "got '%s' while expecting a number", scan);
315 num = strtol(scan, (char **)&scan, 10);
316 if (num < 0 || num >= NETMAP_RING_MASK) {
317 nmctx_ferror(ctx, "'%ld' out of range [0, %d)",
318 num, NETMAP_RING_MASK);
321 nr_ringid = num & NETMAP_RING_MASK;
322 p_state = P_RNGSFXOK;
332 nr_flags |= NR_EXCLUSIVE;
335 nr_flags |= NR_ZCOPY_MON;
338 nr_flags |= NR_MONITOR_TX;
341 nr_flags |= NR_MONITOR_RX;
344 nr_flags |= NR_RX_RINGS_ONLY;
347 nr_flags |= NR_TX_RINGS_ONLY;
350 nmctx_ferror(ctx, "unrecognized flag: '%c'", *scan);
357 if (!isdigit(*scan)) {
358 scan--; /* escape to options */
361 num = strtol(scan, (char **)&scan, 10);
363 nmctx_ferror(ctx, "invalid mem_id: '%ld'", num);
366 if (nr_mem_id && nr_mem_id != num) {
367 nmctx_ferror(ctx, "invalid setting of mem_id to %ld (already set to %"PRIu16")", num, nr_mem_id);
371 p_state = P_RNGSFXOK;
374 if (!isdigit(*scan)) {
375 p_state = P_RNGSFXOK;
377 nr_mode = NR_REG_ONE_SW;
383 if (p_state == P_MEMID && !*scan) {
384 nmctx_ferror(ctx, "invalid empty mem_id");
387 if (p_state != P_START && p_state != P_RNGSFXOK &&
388 p_state != P_FLAGSOK && p_state != P_MEMID && p_state != P_ONESW) {
389 nmctx_ferror(ctx, "unexpected end of request");
393 ED("flags: %s %s %s %s %s %s",
394 (nr_flags & NR_EXCLUSIVE) ? "EXCLUSIVE" : "",
395 (nr_flags & NR_ZCOPY_MON) ? "ZCOPY_MON" : "",
396 (nr_flags & NR_MONITOR_TX) ? "MONITOR_TX" : "",
397 (nr_flags & NR_MONITOR_RX) ? "MONITOR_RX" : "",
398 (nr_flags & NR_RX_RINGS_ONLY) ? "RX_RINGS_ONLY" : "",
399 (nr_flags & NR_TX_RINGS_ONLY) ? "TX_RINGS_ONLY" : "");
400 r->nr_mode = nr_mode;
401 r->nr_ringid = nr_ringid;
402 r->nr_flags = nr_flags;
403 r->nr_mem_id = nr_mem_id;
415 nmreq_option_parsekeys(const char *prefix, char *body, struct nmreq_opt_parser *p,
416 struct nmreq_parse_ctx *pctx)
420 struct nmreq_opt_key *k;
424 while (delim1 != '\0') {
430 for ( scan++; *scan != '\0' && *scan != '=' && *scan != ','; scan++) {
437 for (k = p->keys; (k - p->keys) < NMREQ_OPT_MAXKEYS && k->key != NULL;
439 if (!strcmp(k->key, key))
443 nmctx_ferror(pctx->ctx, "unknown key: '%s'", key);
447 if (pctx->keys[k->id] != NULL) {
448 nmctx_ferror(pctx->ctx, "option '%s': duplicate key '%s', already set to '%s'",
449 prefix, key, pctx->keys[k->id]);
454 for ( ; *scan != '\0' && *scan != ','; scan++)
461 pctx->keys[k->id] = (vlen ? value : NULL);
463 if (!(k->flags & NMREQ_OPTK_ALLOWEMPTY)) {
464 nmctx_ferror(pctx->ctx, "option '%s': missing '=value' for key '%s'",
469 pctx->keys[k->id] = key;
472 /* now check that all no-default keys have been assigned */
473 for (k = p->keys; (k - p->keys) < NMREQ_OPT_MAXKEYS && k->key != NULL; k++) {
474 if ((k->flags & NMREQ_OPTK_MUSTSET) && pctx->keys[k->id] == NULL) {
475 nmctx_ferror(pctx->ctx, "option '%s': mandatory key '%s' not assigned",
486 nmreq_option_decode1(char *opt, struct nmreq_opt_parser *parsers,
487 void *token, struct nmctx *ctx)
489 struct nmreq_opt_parser *p;
493 struct nmreq_parse_ctx pctx;
497 /* find the delimiter */
498 for (scan = opt; *scan != '\0' && *scan != ':' && *scan != '='; scan++)
503 /* find the prefix */
504 for (p = parsers; p != NULL; p = p->next) {
505 if (!strcmp(prefix, p->prefix))
509 nmctx_ferror(ctx, "unknown option: '%s'", prefix);
513 if (p->flags & NMREQ_OPTF_DISABLED) {
514 nmctx_ferror(ctx, "option '%s' is not supported", prefix);
518 /* prepare the parse context */
521 for (i = 0; i < NMREQ_OPT_MAXKEYS; i++)
526 if (!(p->flags & NMREQ_OPTF_ALLOWEMPTY)) {
527 nmctx_ferror(ctx, "syntax error: missing body after '%s'",
533 case '=': /* the body goes to the default option key, if any */
534 if (p->default_key < 0 || p->default_key >= NMREQ_OPT_MAXKEYS) {
535 nmctx_ferror(ctx, "syntax error: '=' not valid after '%s'",
541 nmctx_ferror(ctx, "missing value for option '%s'", prefix);
545 pctx.keys[p->default_key] = scan;
547 case ':': /* parse 'key=value' strings */
548 if (nmreq_option_parsekeys(prefix, scan, p, &pctx) < 0)
552 return p->parse(&pctx);
556 nmreq_options_decode(const char *opt, struct nmreq_opt_parser parsers[],
557 void *token, struct nmctx *ctx)
559 const char *scan, *opt1;
565 return 0; /* empty list, OK */
568 nmctx_ferror(ctx, "option list does not start with '@'");
575 scan++; /* skip the plus */
576 opt1 = scan; /* start of option */
577 /* find the end of the option */
578 for ( ; *scan != '\0' && *scan != '@'; scan++)
582 nmctx_ferror(ctx, "invalid empty option");
586 w = nmctx_malloc(ctx, len + 1);
588 nmctx_ferror(ctx, "out of memory");
592 memcpy(w, opt1, len);
594 ret = nmreq_option_decode1(w, parsers, token, ctx);
598 } while (*scan != '\0');
603 struct nmreq_option *
604 nmreq_find_option(struct nmreq_header *h, uint32_t t)
606 struct nmreq_option *o = NULL;
608 nmreq_foreach_option(h, o) {
609 if (o->nro_reqtype == t)
616 nmreq_remove_option(struct nmreq_header *h, struct nmreq_option *o)
618 struct nmreq_option **nmo;
620 for (nmo = (struct nmreq_option **)&h->nr_options; *nmo != NULL;
621 nmo = (struct nmreq_option **)&(*nmo)->nro_next) {
623 *((uint64_t *)(*nmo)) = o->nro_next;
624 o->nro_next = (uint64_t)(uintptr_t)NULL;
631 nmreq_free_options(struct nmreq_header *h)
633 struct nmreq_option *o, *next;
636 * Note: can't use nmreq_foreach_option() here; it frees the
637 * list as it's walking and nmreq_foreach_option() isn't
640 for (o = (struct nmreq_option *)(uintptr_t)h->nr_options; o != NULL;
642 next = (struct nmreq_option *)(uintptr_t)o->nro_next;
648 nmreq_option_name(uint32_t nro_reqtype)
650 switch (nro_reqtype) {
651 case NETMAP_REQ_OPT_EXTMEM:
653 case NETMAP_REQ_OPT_SYNC_KLOOP_EVENTFDS:
654 return "sync-kloop-eventfds";
655 case NETMAP_REQ_OPT_CSB:
657 case NETMAP_REQ_OPT_SYNC_KLOOP_MODE:
658 return "sync-kloop-mode";
665 #include <inttypes.h>
667 nmreq_dump(struct nmport_d *d)
670 printf(" nr_version: %"PRIu16"\n", d->hdr.nr_version);
671 printf(" nr_reqtype: %"PRIu16"\n", d->hdr.nr_reqtype);
672 printf(" nr_reserved: %"PRIu32"\n", d->hdr.nr_reserved);
673 printf(" nr_name: %s\n", d->hdr.nr_name);
674 printf(" nr_options: %lx\n", (unsigned long)d->hdr.nr_options);
675 printf(" nr_body: %lx\n", (unsigned long)d->hdr.nr_body);
677 printf("register (%p):\n", (void *)d->hdr.nr_body);
678 printf(" nr_mem_id: %"PRIu16"\n", d->reg.nr_mem_id);
679 printf(" nr_ringid: %"PRIu16"\n", d->reg.nr_ringid);
680 printf(" nr_mode: %lx\n", (unsigned long)d->reg.nr_mode);
681 printf(" nr_flags: %lx\n", (unsigned long)d->reg.nr_flags);
683 if (d->hdr.nr_options) {
684 struct nmreq_opt_extmem *e = (struct nmreq_opt_extmem *)d->hdr.nr_options;
685 printf("opt_extmem (%p):\n", e);
686 printf(" nro_opt.nro_next: %lx\n", (unsigned long)e->nro_opt.nro_next);
687 printf(" nro_opt.nro_reqtype: %"PRIu32"\n", e->nro_opt.nro_reqtype);
688 printf(" nro_usrptr: %lx\n", (unsigned long)e->nro_usrptr);
689 printf(" nro_info.nr_memsize %"PRIu64"\n", e->nro_info.nr_memsize);
692 printf("mem (%p):\n", d->mem);
693 printf(" refcount: %d\n", d->mem->refcount);
694 printf(" mem: %p\n", d->mem->mem);
695 printf(" size: %zu\n", d->mem->size);
698 printf(" tx: [%d, %d]\n", d->first_tx_ring, d->last_tx_ring);
699 printf(" rx: [%d, %d]\n", d->first_rx_ring, d->last_rx_ring);
702 main(int argc, char *argv[])
707 fprintf(stderr, "usage: %s netmap-expr\n", argv[0]);
711 d = nmport_open(argv[1]);