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
33 /* if thread-safety is not needed, define LIBNETMAP_NOTHREADSAFE before including
37 /* NOTE: we include net/netmap_user.h without defining NETMAP_WITH_LIBS, which
38 * is deprecated. If you still need it, please define NETMAP_WITH_LIBS and
39 * include net/netmap_user.h before including this file.
41 #include <net/netmap_user.h>
48 * A port open specification (portspec for brevity) has the following syntax
49 * (square brackets delimit optional parts):
51 * subsystem:vpname[mode][options]
53 * The "subsystem" is denoted by a prefix, possibly followed by an identifier.
54 * There can be several kinds of subsystems, each one selected by a unique
55 * prefix. Currently defined subsystems are:
57 * netmap (no id allowed)
58 * the standard subsystem
60 * vale (followed by a possibly empty id)
61 * the vpname is connected to a VALE switch identified by
62 * the id (an empty id selects the default switch)
64 * The "vpname" has the following syntax:
67 * identifier1{identifier2 or
68 * identifier1}identifier2
70 * Identifiers are sequences of alphanumeric characters. The part that begins
71 * with either '{' or '}', when present, denotes a netmap pipe opened in the
72 * same memory region as the subsystem:indentifier1 port.
74 * The "mode" can be one of the following:
76 * ^ bind all host (sw) ring pairs
77 * ^NN bind individual host ring pair
78 * * bind host and NIC ring pairs
79 * -NN bind individual NIC ring pair
80 * @NN open the port in the NN memory region
81 * a suffix starting with / and the following flags,
84 * z zero copy monitor (both tx and rx)
85 * t monitor tx side (copy monitor)
86 * r monitor rx side (copy monitor)
87 * R bind only RX ring(s)
88 * T bind only TX ring(s)
90 * The "options" start at the first '@' character not followed by a number.
91 * Each option starts with '@' and has the following syntax:
93 * option (flag option)
94 * option=value (single key option)
95 * option:key1=value1,key2=value2,... (multi-key option)
97 * For multi-key options, the keys can be assigned in any order, but they
98 * cannot be assigned more than once. It is not necessary to assign all the
99 * option keys: unmentioned keys will receive default values. Some multi-key
100 * options define a default key and also accept the single-key syntax, by
101 * assigning the value to this key.
103 * NOTE: Options may be silently ignored if the port is already open by some
106 * The currently available options are (default keys, when defined, are marked
110 * open the port in the same memory region used by the
111 * given port name (the port name must be given in
112 * subsystem:vpname form)
115 * specify the rings/slots numbers (effective only on
116 * ports that are created by the open operation itself,
117 * and ignored otherwise).
121 * *rings number of tx and rx rings
122 * tx-rings number of tx rings
123 * rx-rings number of rx rings
124 * host-rings number of tx and rx host rings
125 * host-tx-rings number of host tx rings
126 * host-rx-rings number of host rx rings
127 * slots number of slots in each tx and rx
129 * tx-slots number of slots in each tx ring
130 * rx-slots number of slots in each rx ring
132 * (more specific keys override the less specific ones)
133 * All keys default to zero if not assigned, and the
134 * corresponding value will be chosen by netmap.
137 * open the port in the memory region obtained by
138 * mmap()ing the given file.
142 * *file the file to mmap
143 * if-num number of pre-allocated netmap_if's
144 * if-size size of each netmap_if
145 * ring-num number of pre-allocated netmap_ring's
146 * ring-size size of each netmap_ring
147 * buf-num number of pre-allocated buffers
148 * buf-size size of each buffer
150 * file must be assigned. The other keys default to zero,
151 * causing netmap to take the corresponding values from
152 * the priv_{if,ring,buf}_{num,size} sysctls.
157 /* nmport manipulation */
159 /* struct nmport_d - describes a netmap port */
161 /* see net/netmap.h for the definition of these fields */
162 struct nmreq_header hdr;
163 struct nmreq_register reg;
165 /* all the fields below should be considered read-only */
167 /* if the same context is used throughout the program, d1->mem ==
168 * d2->mem iff d1 and d2 are using the memory region (i.e., zero
169 * copy is possible between the two ports)
173 /* the nmctx used when this nmport_d was created */
176 int register_done; /* nmport_register() has been called */
177 int mmap_done; /* nmport_mmap() has been called */
178 /* pointer to the extmem option contained in the hdr options, if any */
179 struct nmreq_opt_extmem *extmem;
181 /* the fields below are compatible with nm_open() */
182 int fd; /* "/dev/netmap", -1 if not open */
183 struct netmap_if *nifp; /* pointer to the netmap_if */
184 uint16_t first_tx_ring;
185 uint16_t last_tx_ring;
186 uint16_t first_rx_ring;
187 uint16_t last_rx_ring;
188 uint16_t cur_tx_ring; /* used by nmport_inject */
189 uint16_t cur_rx_ring;
191 /* LIFO list of cleanup functions (used internally) */
192 struct nmport_cleanup_d *clist;
195 /* nmport_open - opens a port from a portspec
196 * @portspec the port opening specification
198 * If successful, the function returns a new nmport_d describing a netmap
199 * port, opened according to the port specification, ready to be used for rx
202 * The rings available for tx are in the [first_tx_ring, last_tx_ring]
203 * interval, and similarly for rx. One or both intervals may be empty.
205 * When done using it, the nmport_d descriptor must be closed using
208 * In case of error, NULL is returned, errno is set to some error, and an
209 * error message is sent through the error() method of the current context.
211 struct nmport_d * nmport_open(const char *portspec);
213 /* nport_close - close a netmap port
214 * @d the port we want to close
216 * Undoes the actions performed by the nmport_open that created d, then
217 * frees the descriptor.
219 void nmport_close(struct nmport_d *d);
221 /* nmport_inject - sends a packet
222 * @d the port through which we want to send
223 * @buf base address of the packet
224 * @size its size in bytes
226 * Sends a packet using the cur_tx_ring and updates the index
227 * to use all available tx rings in turn. Note: the packet is copied.
229 * Returns 0 on success an -1 on error.
231 int nmport_inject(struct nmport_d *d, const void *buf, size_t size);
234 * the functions below can be used to split the functionality of
235 * nmport_open when special features (e.g., extra buffers) are needed
237 * The relation among the functions is as follows:
240 * |nmport_prepare = |
244 * |nmport_open_desc =|
249 /* nmport_new - create a new nmport_d
251 * Creates a new nmport_d using the malloc() method of the current default
252 * context. Returns NULL on error, setting errno to an error value.
254 struct nmport_d *nmport_new(void);
256 /* nmport_parse - fills the nmport_d netmap-register request
257 * @d the nmport to be filled
258 * @portspec the port opening specification
260 * This function parses the portspec and initizalizes the @d->hdr and @d->reg
261 * fields. It may need to allocate a list of options. If an extmem option is
262 * found, it may also mmap() the corresponding file.
264 * It returns 0 on success. On failure it returns -1, sets errno to an error
265 * value and sends an error message to the error() method of the context used
266 * when @d was created. Moreover, *@d is left unchanged.
268 int nmport_parse(struct nmport_d *d, const char *portspec);
270 /* nmport_register - registers the port with netmap
271 * @d the nmport to be registered
273 * This function obtains a netmap file descriptor and registers the port with
274 * netmap. The @d->hdr and @d->reg data structures must have been previously
275 * initialized (via nmport_parse() or otherwise).
277 * It returns 0 on success. On failure it returns -1, sets errno to an error
278 * value and sends an error message to the error() method of the context used
279 * when @d was created. Moreover, *@d is left unchanged.
281 int nmport_register(struct nmport_d *);
283 /* nmport_mmap - maps the port resources into the process memory
284 * @d the nmport to be mapped
286 * The port must have been previously been registered using nmport_register.
288 * Note that if extmem is used (either via an option or by calling an
289 * nmport_extmem_* function before nmport_register()), no new mmap() is issued.
291 * It returns 0 on success. On failure it returns -1, sets errno to an error
292 * value and sends an error message to the error() method of the context used
293 * when @d was created. Moreover, *@d is left unchanged.
295 int nmport_mmap(struct nmport_d *);
297 /* the following functions undo the actions of nmport_new(), nmport_parse(),
298 * nmport_register() and nmport_mmap(), respectively.
300 void nmport_delete(struct nmport_d *);
301 void nmport_undo_parse(struct nmport_d *);
302 void nmport_undo_register(struct nmport_d *);
303 void nmport_undo_mmap(struct nmport_d *);
305 /* nmport_prepare - create a port descriptor, but do not open it
306 * @portspec the port opening specification
308 * This functions creates a new nmport_d and initializes it according to
309 * @portspec. It is equivalent to nmport_new() followed by nmport_parse().
311 * It returns 0 on success. On failure it returns -1, sets errno to an error
312 * value and sends an error message to the error() method of the context used
313 * when @d was created. Moreover, *@d is left unchanged.
315 struct nmport_d *nmport_prepare(const char *portspec);
317 /* nmport_open_desc - open an initialized port descriptor
318 * @d the descriptor we want to open
320 * Registers the port with netmap and maps the rings and buffers into the
321 * process memory. It is equivalent to nmport_register() followed by
324 * It returns 0 on success. On failure it returns -1, sets errno to an error
325 * value and sends an error message to the error() method of the context used
326 * when @d was created. Moreover, *@d is left unchanged.
328 int nmport_open_desc(struct nmport_d *d);
330 /* the following functions undo the actions of nmport_prepare()
331 * and nmport_open_desc(), respectively.
333 void nmport_undo_prepare(struct nmport_d *);
334 void nmport_undo_open_desc(struct nmport_d *);
336 /* nmport_clone - copy an nmport_d
337 * @d the nmport_d we want to copy
339 * Copying an nmport_d by hand should be avoided, since adjustments are needed
340 * and some part of the state cannot be easily duplicated. This function
341 * creates a copy of @d in a safe way. The returned nmport_d contains
342 * nmreq_header and nmreq_register structures equivalent to those contained in
343 * @d, except for the option list, which is ignored. The returned nmport_d is
344 * already nmport_prepare()d, but it must still be nmport_open_desc()ed. The
345 * new nmport_d uses the same nmctx as @d.
347 * If extmem was used for @d, then @d cannot be nmport_clone()d until it has
348 * been nmport_register()ed.
350 * In case of error, the function returns NULL, sets errno to an error value
351 * and sends an error message to the nmctx error() method.
353 struct nmport_d *nmport_clone(struct nmport_d *);
355 /* nmport_extmem - use extmem for this port
356 * @d the port we want to use the extmem for
357 * @base the base address of the extmem region
358 * @size the size in bytes of the extmem region
360 * the memory that contains the netmap ifs, rings and buffers is usually
361 * allocated by netmap and later mmap()ed by the applications. It is sometimes
362 * useful to reverse this process, by having the applications allocate some
363 * memory (through mmap() or otherwise) and then let netmap use it. The extmem
364 * option can be used to implement this latter strategy. The option can be
365 * passed through the portspec using the '@extmem:...' syntax, or
366 * programmatically by calling nmport_extmem() or nmport_extmem_from_file()
367 * between nmport_parse() and nmport_register() (or between nmport_prepare()
368 * and nmport_open_desc()).
370 * It returns 0 on success. On failure it returns -1, sets errno to an error
371 * value and sends an error message to the error() method of the context used
372 * when @d was created. Moreover, *@d is left unchanged.
374 int nmport_extmem(struct nmport_d *d, void *base, size_t size);
376 /* nmport_extmem_from_file - use the extmem obtained by mapping a file
377 * @d the port we want to use the extmem for
378 * @fname path of the file we want to map
380 * This works like nmport_extmem, but the extmem memory is obtained by
381 * mmap()ping @fname. nmport_close() will also automatically munmap() the file.
383 * It returns 0 on success. On failure it returns -1, sets errno to an error
384 * value and sends an error message to the error() method of the context used
385 * when @d was created. Moreover, *@d is left unchanged.
387 int nmport_extmem_from_file(struct nmport_d *d, const char *fname);
389 /* nmport_extmem_getinfo - opbtai a pointer to the extmem configuration
390 * @d the port we want to obtain the pointer from
392 * Returns a pointer to the nmreq_pools_info structure containing the
393 * configuration of the extmem attached to port @d, or NULL if no extmem
394 * is attached. This can be used to set the desired configuration before
395 * registering the port, or to read the actual configuration after
398 struct nmreq_pools_info* nmport_extmem_getinfo(struct nmport_d *d);
401 /* enable/disable options
403 * These functions can be used to disable options that the application cannot
404 * or doesn't want to handle, or to enable options that require special support
405 * from the application and are, therefore, disabled by default. Disabled
406 * options will cause an error if encountered during option parsing.
408 * If the option is unknown, nmport_disable_option is a NOP, while
409 * nmport_enable_option returns -1 and sets errno to EOPNOTSUPP.
411 * These functions are not threadsafe and are meant to be used at the beginning
414 void nmport_disable_option(const char *opt);
415 int nmport_enable_option(const char *opt);
417 /* nmreq manipulation
419 * nmreq_header_init - initialize an nmreq_header
420 * @hdr the nmreq_header to initialize
421 * @reqtype the kind of netmap request
422 * @body the body of the request
424 * Initialize the nr_version, nr_reqtype and nr_body fields of *@hdr.
425 * The other fields are set to zero.
427 void nmreq_header_init(struct nmreq_header *hdr, uint16_t reqtype, void *body);
430 * These functions allow for finer grained parsing of portspecs. They are used
431 * internally by nmport_parse().
434 /* nmreq_header_decode - initialize an nmreq_header
435 * @ppspec: (in/out) pointer to a pointer to the portspec
436 * @hdr: pointer to the nmreq_header to be initialized
437 * @ctx: pointer to the nmctx to use (for errors)
439 * This function fills the @hdr the nr_name field with the port name extracted
440 * from *@pifname. The other fields of *@hdr are unchanged. The @pifname is
441 * updated to point at the first char past the port name.
443 * Returns 0 on success. In case of error, -1 is returned with errno set to
444 * EINVAL, @pifname is unchanged, *@hdr is also unchanged, and an error message
445 * is sent through @ctx->error().
447 int nmreq_header_decode(const char **ppspec, struct nmreq_header *hdr,
450 /* nmreq_regiter_decode - initialize an nmreq_register
451 * @pmode: (in/out) pointer to a pointer to an opening mode
452 * @reg: pointer to the nmreq_register to be initialized
453 * @ctx: pointer to the nmctx to use (for errors)
455 * This function fills the nr_mode, nr_ringid, nr_flags and nr_mem_id fields of
456 * the structure pointed by @reg, according to the opening mode specified by
457 * *@pmode. The other fields of *@reg are unchanged. The @pmode is updated to
458 * point at the first char past the opening mode.
460 * If a '@' is encountered followed by something which is not a number, parsing
461 * stops (without error) and @pmode is left pointing at the '@' char. The
462 * nr_mode, nr_ringid and nr_flags fields are still updated, but nr_mem_id is
463 * not touched and the interpretation of the '@' field is left to the caller.
465 * Returns 0 on success. In case of error, -1 is returned with errno set to
466 * EINVAL, @pmode is unchanged, *@reg is also unchanged, and an error message
467 * is sent through @ctx->error().
469 int nmreq_register_decode(const char **pmode, struct nmreq_register *reg,
472 /* nmreq_options_decode - parse the "options" part of the portspec
473 * @opt: pointer to the option list
474 * @parsers: list of option parsers
475 * @token: token to pass to each parser
476 * @ctx: pointer to the nmctx to use (for errors and malloc/free)
478 * This function parses each option in @opt. Each option is matched (based on
479 * the "option" prefix) to a corresponding parser in @parsers. The function
480 * checks that the syntax is appropriate for the parser and it assigns all the
481 * keys mentioned in the option. It then passes control to the parser, to
482 * interpret the keys values.
484 * Returns 0 on success. In case of error, -1 is returned, errno is set to an
485 * error value and a message is sent to @ctx->error(). The effects of partially
486 * interpreted options may not be undone.
488 struct nmreq_opt_parser;
489 int nmreq_options_decode(const char *opt, struct nmreq_opt_parser *parsers,
490 void *token, struct nmctx *ctx);
492 struct nmreq_parse_ctx;
493 /* type of the option-parsers callbacks */
494 typedef int (*nmreq_opt_parser_cb)(struct nmreq_parse_ctx *);
496 #define NMREQ_OPT_MAXKEYS 16 /* max nr of recognized keys per option */
498 /* struct nmreq_opt_key - describes an option key */
499 struct nmreq_opt_key {
500 const char *key; /* the key name */
501 int id; /* its position in the parse context */
503 #define NMREQ_OPTK_ALLOWEMPTY (1U << 0) /* =value may be omitted */
504 #define NMREQ_OPTK_MUSTSET (1U << 1) /* the key is mandatory */
505 #define NMREQ_OPTK_DEFAULT (1U << 2) /* this is the default key */
508 /* struct nmreq_opt_parser - describes an option parser */
509 struct nmreq_opt_parser {
510 const char *prefix; /* matches one option prefix */
511 nmreq_opt_parser_cb parse; /* the parse callback */
512 int default_key; /* which option is the default if the
513 parser is multi-key (-1 if none) */
516 #define NMREQ_OPTF_DISABLED (1U << 0)
517 #define NMREQ_OPTF_ALLOWEMPTY (1U << 1) /* =value can be omitted */
519 struct nmreq_opt_parser *next; /* list of options */
521 /* recognized keys */
522 struct nmreq_opt_key keys[NMREQ_OPT_MAXKEYS];
523 } __attribute__((aligned(16)));
525 /* struct nmreq_parse_ctx - the parse context received by the parse callback */
526 struct nmreq_parse_ctx {
527 struct nmctx *ctx; /* the nmctx for errors and malloc/free */
528 void *token; /* the token passed to nmreq_options_parse */
530 /* the value (i.e., the part after the = sign) of each recognized key
531 * is assigned to the corresponding entry in this array, based on the
532 * key id. Unassigned keys are left at NULL.
534 const char *keys[NMREQ_OPT_MAXKEYS];
537 /* nmreq_get_mem_id - get the mem_id of the given port
538 * @portname pointer to a pointer to the portname
539 * @ctx pointer to the nmctx to use (for errors)
541 * *@portname must point to a substem:vpname porname, possibly followed by
544 * If successful, returns the mem_id of *@portname and moves @portname past the
545 * subsystem:vpname part of the input. In case of error it returns -1, sets
546 * errno to an error value and sends an error message to ctx->error().
548 int32_t nmreq_get_mem_id(const char **portname, struct nmctx *ctx);
550 /* option list manipulation */
551 void nmreq_push_option(struct nmreq_header *, struct nmreq_option *);
552 void nmreq_remove_option(struct nmreq_header *, struct nmreq_option *);
553 struct nmreq_option *nmreq_find_option(struct nmreq_header *, uint32_t);
554 void nmreq_free_options(struct nmreq_header *);
555 const char* nmreq_option_name(uint32_t);
556 #define nmreq_foreach_option(h_, o_) \
557 for ((o_) = (struct nmreq_option *)((h_)->nr_options);\
559 (o_) = (struct nmreq_option *)((o_)->nro_next))
561 /* nmctx manipulation */
563 /* the nmctx serves a few purposes:
565 * - maintain a list of all memory regions open by the program, so that two
566 * ports that are using the same region (as identified by the mem_id) will
567 * point to the same nmem_d instance.
569 * - allow the user to specify how to lock accesses to the above list, if
570 * needed (lock() callback)
572 * - allow the user to specify how error messages should be delivered (error()
575 * - select the verbosity of the library (verbose field); if verbose==0, no
576 * errors are sent to the error() callback
578 * - allow the user to override the malloc/free functions used by the library
579 * (malloc() and free() callbacks)
582 typedef void (*nmctx_error_cb)(struct nmctx *, const char *);
583 typedef void *(*nmctx_malloc_cb)(struct nmctx *,size_t);
584 typedef void (*nmctx_free_cb)(struct nmctx *,void *);
585 typedef void (*nmctx_lock_cb)(struct nmctx *, int);
589 nmctx_error_cb error;
590 nmctx_malloc_cb malloc;
594 struct nmem_d *mem_descs;
597 /* nmctx_get - obtain a pointer to the current default context */
598 struct nmctx *nmctx_get(void);
600 /* nmctx_set_default - change the default context
601 * @ctx pointer to the new context
603 * Returns a pointer to the previous default context.
605 struct nmctx *nmctx_set_default(struct nmctx *ctx);
607 /* internal functions and data structures */
609 /* struct nmem_d - describes a memory region currently used */
611 uint16_t mem_id; /* the region netmap identifier */
612 int refcount; /* how many nmport_d's point here */
613 void *mem; /* memory region base address */
614 size_t size; /* memory region size */
615 int is_extmem; /* was it obtained via extmem? */
617 /* pointers for the circular list implementation.
618 * The list head is the mem_descs filed in the nmctx
624 /* a trick to force the inclusion of libpthread only if requested. If
625 * LIBNETMAP_NOTHREADSAFE is defined, no pthread symbol is imported.
627 * There is no need to actually call this function: the ((used)) attribute is
628 * sufficient to include it in the image.
630 static __attribute__((used)) void libnetmap_init(void)
632 #ifndef LIBNETMAP_NOTHREADSAFE
633 extern int nmctx_threadsafe;
634 /* dummy assignment to link-in the nmctx-pthread.o object. The proper
635 * inizialization is performed only once in the library constructor
638 nmctx_threadsafe = 1;
639 #endif /* LIBNETMAP_NOTHREADSAFE */
642 /* nmctx_set_threadsafe - install a threadsafe default context
644 * called by the constructor in nmctx-pthread.o to initialize a lock and install
645 * the lock() callback in the default context.
647 void nmctx_set_threadsafe(void);
649 /* nmctx_ferror - format and send an error message */
650 void nmctx_ferror(struct nmctx *, const char *, ...);
651 /* nmctx_malloc - allocate memory */
652 void *nmctx_malloc(struct nmctx *, size_t);
653 /* nmctx_free - free memory allocated via nmctx_malloc */
654 void nmctx_free(struct nmctx *, void *);
655 /* nmctx_lock - lock the list of nmem_d */
656 void nmctx_lock(struct nmctx *);
657 /* nmctx_unlock - unlock the list of nmem_d */
658 void nmctx_unlock(struct nmctx *);
660 #endif /* LIBNETMAP_H_ */