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;
262 /* fill the request */
266 nr_mode = NR_REG_ALL_NIC; /* default for no suffix */
267 nr_mem_id = r->nr_mem_id; /* if non-zero, further updates are disabled */
274 case '^': /* only SW ring */
278 case '*': /* NIC and SW */
279 nr_mode = NR_REG_NIC_SW;
280 p_state = P_RNGSFXOK;
282 case '-': /* one NIC ring pair */
283 nr_mode = NR_REG_ONE_NIC;
286 case '/': /* start of flags */
289 case '@': /* start of memid */
293 nmctx_ferror(ctx, "unknown modifier: '%c'", *scan);
307 nmctx_ferror(ctx, "unexpected character: '%c'", *scan);
313 if (!isdigit(*scan)) {
314 nmctx_ferror(ctx, "got '%s' while expecting a number", scan);
317 num = strtol(scan, (char **)&scan, 10);
318 if (num < 0 || num >= NETMAP_RING_MASK) {
319 nmctx_ferror(ctx, "'%ld' out of range [0, %d)",
320 num, NETMAP_RING_MASK);
323 nr_ringid = num & NETMAP_RING_MASK;
324 p_state = P_RNGSFXOK;
334 nr_flags |= NR_EXCLUSIVE;
337 nr_flags |= NR_ZCOPY_MON;
340 nr_flags |= NR_MONITOR_TX;
343 nr_flags |= NR_MONITOR_RX;
346 nr_flags |= NR_RX_RINGS_ONLY;
349 nr_flags |= NR_TX_RINGS_ONLY;
352 nmctx_ferror(ctx, "unrecognized flag: '%c'", *scan);
359 if (!isdigit(*scan)) {
360 scan--; /* escape to options */
363 num = strtol(scan, (char **)&scan, 10);
365 nmctx_ferror(ctx, "invalid mem_id: '%ld'", num);
368 if (nr_mem_id && nr_mem_id != num) {
369 nmctx_ferror(ctx, "invalid setting of mem_id to %ld (already set to %"PRIu16")", num, nr_mem_id);
373 p_state = P_RNGSFXOK;
376 if (!isdigit(*scan)) {
377 p_state = P_RNGSFXOK;
379 nr_mode = NR_REG_ONE_SW;
385 if (p_state == P_MEMID && !*scan) {
386 nmctx_ferror(ctx, "invalid empty mem_id");
389 if (p_state != P_START && p_state != P_RNGSFXOK &&
390 p_state != P_FLAGSOK && p_state != P_MEMID && p_state != P_ONESW) {
391 nmctx_ferror(ctx, "unexpected end of request");
395 ED("flags: %s %s %s %s %s %s",
396 (nr_flags & NR_EXCLUSIVE) ? "EXCLUSIVE" : "",
397 (nr_flags & NR_ZCOPY_MON) ? "ZCOPY_MON" : "",
398 (nr_flags & NR_MONITOR_TX) ? "MONITOR_TX" : "",
399 (nr_flags & NR_MONITOR_RX) ? "MONITOR_RX" : "",
400 (nr_flags & NR_RX_RINGS_ONLY) ? "RX_RINGS_ONLY" : "",
401 (nr_flags & NR_TX_RINGS_ONLY) ? "TX_RINGS_ONLY" : "");
402 r->nr_mode = nr_mode;
403 r->nr_ringid = nr_ringid;
404 r->nr_flags = nr_flags;
405 r->nr_mem_id = nr_mem_id;
417 nmreq_option_parsekeys(const char *prefix, char *body, struct nmreq_opt_parser *p,
418 struct nmreq_parse_ctx *pctx)
422 struct nmreq_opt_key *k;
426 while (delim1 != '\0') {
432 for ( scan++; *scan != '\0' && *scan != '=' && *scan != ','; scan++) {
439 for (k = p->keys; (k - p->keys) < NMREQ_OPT_MAXKEYS && k->key != NULL;
441 if (!strcmp(k->key, key))
445 nmctx_ferror(pctx->ctx, "unknown key: '%s'", key);
449 if (pctx->keys[k->id] != NULL) {
450 nmctx_ferror(pctx->ctx, "option '%s': duplicate key '%s', already set to '%s'",
451 prefix, key, pctx->keys[k->id]);
456 for ( ; *scan != '\0' && *scan != ','; scan++)
463 pctx->keys[k->id] = (vlen ? value : NULL);
465 if (!(k->flags & NMREQ_OPTK_ALLOWEMPTY)) {
466 nmctx_ferror(pctx->ctx, "option '%s': missing '=value' for key '%s'",
471 pctx->keys[k->id] = key;
474 /* now check that all no-default keys have been assigned */
475 for (k = p->keys; (k - p->keys) < NMREQ_OPT_MAXKEYS && k->key != NULL; k++) {
476 if ((k->flags & NMREQ_OPTK_MUSTSET) && pctx->keys[k->id] == NULL) {
477 nmctx_ferror(pctx->ctx, "option '%s': mandatory key '%s' not assigned",
488 nmreq_option_decode1(char *opt, struct nmreq_opt_parser *parsers,
489 void *token, struct nmctx *ctx)
491 struct nmreq_opt_parser *p;
495 struct nmreq_parse_ctx pctx;
499 /* find the delimiter */
500 for (scan = opt; *scan != '\0' && *scan != ':' && *scan != '='; scan++)
505 /* find the prefix */
506 for (p = parsers; p != NULL; p = p->next) {
507 if (!strcmp(prefix, p->prefix))
511 nmctx_ferror(ctx, "unknown option: '%s'", prefix);
515 if (p->flags & NMREQ_OPTF_DISABLED) {
516 nmctx_ferror(ctx, "option '%s' is not supported", prefix);
520 /* prepare the parse context */
523 for (i = 0; i < NMREQ_OPT_MAXKEYS; i++)
528 if (!(p->flags & NMREQ_OPTF_ALLOWEMPTY)) {
529 nmctx_ferror(ctx, "syntax error: missing body after '%s'",
535 case '=': /* the body goes to the default option key, if any */
536 if (p->default_key < 0 || p->default_key >= NMREQ_OPT_MAXKEYS) {
537 nmctx_ferror(ctx, "syntax error: '=' not valid after '%s'",
543 nmctx_ferror(ctx, "missing value for option '%s'", prefix);
547 pctx.keys[p->default_key] = scan;
549 case ':': /* parse 'key=value' strings */
550 if (nmreq_option_parsekeys(prefix, scan, p, &pctx) < 0)
554 return p->parse(&pctx);
558 nmreq_options_decode(const char *opt, struct nmreq_opt_parser parsers[],
559 void *token, struct nmctx *ctx)
561 const char *scan, *opt1;
567 return 0; /* empty list, OK */
570 nmctx_ferror(ctx, "option list does not start with '@'");
577 scan++; /* skip the plus */
578 opt1 = scan; /* start of option */
579 /* find the end of the option */
580 for ( ; *scan != '\0' && *scan != '@'; scan++)
584 nmctx_ferror(ctx, "invalid empty option");
588 w = nmctx_malloc(ctx, len + 1);
590 nmctx_ferror(ctx, "out of memory");
594 memcpy(w, opt1, len);
596 ret = nmreq_option_decode1(w, parsers, token, ctx);
600 } while (*scan != '\0');
605 struct nmreq_option *
606 nmreq_find_option(struct nmreq_header *h, uint32_t t)
608 struct nmreq_option *o = NULL;
610 nmreq_foreach_option(h, o) {
611 if (o->nro_reqtype == t)
618 nmreq_remove_option(struct nmreq_header *h, struct nmreq_option *o)
620 struct nmreq_option **nmo;
622 for (nmo = (struct nmreq_option **)&h->nr_options; *nmo != NULL;
623 nmo = (struct nmreq_option **)&(*nmo)->nro_next) {
625 *((uint64_t *)(*nmo)) = o->nro_next;
626 o->nro_next = (uint64_t)(uintptr_t)NULL;
633 nmreq_free_options(struct nmreq_header *h)
635 struct nmreq_option *o, *next;
638 * Note: can't use nmreq_foreach_option() here; it frees the
639 * list as it's walking and nmreq_foreach_option() isn't
642 for (o = (struct nmreq_option *)(uintptr_t)h->nr_options; o != NULL;
644 next = (struct nmreq_option *)(uintptr_t)o->nro_next;
650 nmreq_option_name(uint32_t nro_reqtype)
652 switch (nro_reqtype) {
653 case NETMAP_REQ_OPT_EXTMEM:
655 case NETMAP_REQ_OPT_SYNC_KLOOP_EVENTFDS:
656 return "sync-kloop-eventfds";
657 case NETMAP_REQ_OPT_CSB:
659 case NETMAP_REQ_OPT_SYNC_KLOOP_MODE:
660 return "sync-kloop-mode";
661 case NETMAP_REQ_OPT_OFFSETS:
669 #include <inttypes.h>
671 nmreq_dump(struct nmport_d *d)
674 printf(" nr_version: %"PRIu16"\n", d->hdr.nr_version);
675 printf(" nr_reqtype: %"PRIu16"\n", d->hdr.nr_reqtype);
676 printf(" nr_reserved: %"PRIu32"\n", d->hdr.nr_reserved);
677 printf(" nr_name: %s\n", d->hdr.nr_name);
678 printf(" nr_options: %lx\n", (unsigned long)d->hdr.nr_options);
679 printf(" nr_body: %lx\n", (unsigned long)d->hdr.nr_body);
681 printf("register (%p):\n", (void *)d->hdr.nr_body);
682 printf(" nr_mem_id: %"PRIu16"\n", d->reg.nr_mem_id);
683 printf(" nr_ringid: %"PRIu16"\n", d->reg.nr_ringid);
684 printf(" nr_mode: %lx\n", (unsigned long)d->reg.nr_mode);
685 printf(" nr_flags: %lx\n", (unsigned long)d->reg.nr_flags);
687 if (d->hdr.nr_options) {
688 struct nmreq_opt_extmem *e = (struct nmreq_opt_extmem *)d->hdr.nr_options;
689 printf("opt_extmem (%p):\n", e);
690 printf(" nro_opt.nro_next: %lx\n", (unsigned long)e->nro_opt.nro_next);
691 printf(" nro_opt.nro_reqtype: %"PRIu32"\n", e->nro_opt.nro_reqtype);
692 printf(" nro_usrptr: %lx\n", (unsigned long)e->nro_usrptr);
693 printf(" nro_info.nr_memsize %"PRIu64"\n", e->nro_info.nr_memsize);
696 printf("mem (%p):\n", d->mem);
697 printf(" refcount: %d\n", d->mem->refcount);
698 printf(" mem: %p\n", d->mem->mem);
699 printf(" size: %zu\n", d->mem->size);
702 printf(" tx: [%d, %d]\n", d->first_tx_ring, d->last_tx_ring);
703 printf(" rx: [%d, %d]\n", d->first_rx_ring, d->last_rx_ring);
706 main(int argc, char *argv[])
711 fprintf(stderr, "usage: %s netmap-expr\n", argv[0]);
715 d = nmport_open(argv[1]);