]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - share/man/man4/netmap.4
MFstable/10 r233422:
[FreeBSD/stable/9.git] / share / man / man4 / netmap.4
1 .\" Copyright (c) 2011-2014 Matteo Landi, Luigi Rizzo, Universita` di Pisa
2 .\" All rights reserved.
3 .\"
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
6 .\" are met:
7 .\" 1. Redistributions of source code must retain the above copyright
8 .\"    notice, this list of conditions and the following disclaimer.
9 .\" 2. Redistributions in binary form must reproduce the above copyright
10 .\"    notice, this list of conditions and the following disclaimer in the
11 .\"    documentation and/or other materials provided with the distribution.
12 .\"
13 .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 .\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23 .\" SUCH DAMAGE.
24 .\"
25 .\" This document is derived in part from the enet man page (enet.4)
26 .\" distributed with 4.3BSD Unix.
27 .\"
28 .\" $FreeBSD$
29 .\"
30 .Dd February 13, 2014
31 .Dt NETMAP 4
32 .Os
33 .Sh NAME
34 .Nm netmap
35 .Nd a framework for fast packet I/O
36 .br
37 .Nm VALE
38 .Nd a fast VirtuAl Local Ethernet using the netmap API
39 .br
40 .Nm netmap pipes
41 .Nd a shared memory packet transport channel
42 .Sh SYNOPSIS
43 .Cd device netmap
44 .Sh DESCRIPTION
45 .Nm
46 is a framework for extremely fast and efficient packet I/O
47 for both userspace and kernel clients.
48 It runs on FreeBSD and Linux,
49 and includes
50 .Nm VALE ,
51 a very fast and modular in-kernel software switch/dataplane,
52 and
53 .Nm netmap pipes ,
54 a shared memory packet transport channel.
55 All these are accessed interchangeably with the same API.
56 .Pp
57 .Nm , VALE
58 and
59 .Nm netmap pipes
60 are at least one order of magnitude faster than
61 standard OS mechanisms
62 (sockets, bpf, tun/tap interfaces, native switches, pipes),
63 reaching 14.88 million packets per second (Mpps)
64 with much less than one core on a 10 Gbit NIC,
65 about 20 Mpps per core for VALE ports,
66 and over 100 Mpps for netmap pipes.
67 .Pp
68 Userspace clients can dynamically switch NICs into
69 .Nm
70 mode and send and receive raw packets through
71 memory mapped buffers.
72 Similarly,
73 .Nm VALE
74 switch instances and ports, and
75 .Nm netmap pipes
76 can be created dynamically,
77 providing high speed packet I/O between processes,
78 virtual machines, NICs and the host stack.
79 .Pp
80 .Nm
81 suports both non-blocking I/O through
82 .Xr ioctls() ,
83 synchronization and blocking I/O through a file descriptor
84 and standard OS mechanisms such as
85 .Xr select 2 ,
86 .Xr poll 2 ,
87 .Xr epoll 2 ,
88 .Xr kqueue 2 .
89 .Nm VALE
90 and
91 .Nm netmap pipes
92 are implemented by a single kernel module, which also emulates the
93 .Nm
94 API over standard drivers for devices without native
95 .Nm
96 support.
97 For best performance,
98 .Nm
99 requires explicit support in device drivers.
100 .Pp
101 In the rest of this (long) manual page we document
102 various aspects of the
103 .Nm
104 and
105 .Nm VALE
106 architecture, features and usage.
107 .Pp
108 .Sh ARCHITECTURE
109 .Nm
110 supports raw packet I/O through a
111 .Em port ,
112 which can be connected to a physical interface
113 .Em ( NIC ) ,
114 to the host stack,
115 or to a
116 .Nm VALE
117 switch).
118 Ports use preallocated circular queues of buffers
119 .Em ( rings )
120 residing in an mmapped region.
121 There is one ring for each transmit/receive queue of a
122 NIC or virtual port.
123 An additional ring pair connects to the host stack.
124 .Pp
125 After binding a file descriptor to a port, a
126 .Nm
127 client can send or receive packets in batches through
128 the rings, and possibly implement zero-copy forwarding
129 between ports.
130 .Pp
131 All NICs operating in
132 .Nm
133 mode use the same memory region,
134 accessible to all processes who own
135 .Nm /dev/netmap
136 file descriptors bound to NICs.
137 Independent
138 .Nm VALE
139 and
140 .Nm netmap pipe
141 ports
142 by default use separate memory regions,
143 but can be independently configured to share memory.
144 .Pp
145 .Sh ENTERING AND EXITING NETMAP MODE
146 The following section describes the system calls to create
147 and control
148 .Nm netmap 
149 ports (including
150 .Nm VALE
151 and
152 .Nm netmap pipe
153 ports).
154 Simpler, higher level functions are described in section
155 .Xr LIBRARIES .
156 .Pp
157 Ports and rings are created and controlled through a file descriptor,
158 created by opening a special device
159 .Dl fd = open("/dev/netmap");
160 and then bound to a specific port with an
161 .Dl ioctl(fd, NIOCREGIF, (struct nmreq *)arg);
162 .Pp
163 .Nm
164 has multiple modes of operation controlled by the
165 .Vt struct nmreq
166 argument.
167 .Va arg.nr_name
168 specifies the port name, as follows:
169 .Bl -tag -width XXXX
170 .It Dv OS network interface name (e.g. 'em0', 'eth1', ... )
171 the data path of the NIC is disconnected from the host stack,
172 and the file descriptor is bound to the NIC (one or all queues),
173 or to the host stack;
174 .It Dv valeXXX:YYY (arbitrary XXX and YYY)
175 the file descriptor is bound to port YYY of a VALE switch called XXX,
176 both dynamically created if necessary.
177 The string cannot exceed IFNAMSIZ characters, and YYY cannot
178 be the name of any existing OS network interface.
179 .El
180 .Pp
181 On return,
182 .Va arg
183 indicates the size of the shared memory region,
184 and the number, size and location of all the
185 .Nm
186 data structures, which can be accessed by mmapping the memory
187 .Dl char *mem = mmap(0, arg.nr_memsize, fd);
188 .Pp
189 Non blocking I/O is done with special
190 .Xr ioctl 2
191 .Xr select 2
192 and
193 .Xr poll 2
194 on the file descriptor permit blocking I/O.
195 .Xr epoll 2
196 and
197 .Xr kqueue 2
198 are not supported on
199 .Nm
200 file descriptors.
201 .Pp
202 While a NIC is in
203 .Nm
204 mode, the OS will still believe the interface is up and running.
205 OS-generated packets for that NIC end up into a
206 .Nm
207 ring, and another ring is used to send packets into the OS network stack.
208 A
209 .Xr close 2
210 on the file descriptor removes the binding,
211 and returns the NIC to normal mode (reconnecting the data path
212 to the host stack), or destroys the virtual port.
213 .Sh DATA STRUCTURES
214 The data structures in the mmapped memory region are detailed in
215 .Xr sys/net/netmap.h ,
216 which is the ultimate reference for the
217 .Nm
218 API. The main structures and fields are indicated below:
219 .Bl -tag -width XXX
220 .It Dv struct netmap_if (one per interface)
221 .Bd -literal
222 struct netmap_if {
223     ...
224     const uint32_t   ni_flags;      /* properties              */
225     ...
226     const uint32_t   ni_tx_rings;   /* NIC tx rings            */
227     const uint32_t   ni_rx_rings;   /* NIC rx rings            */
228     uint32_t         ni_bufs_head;  /* head of extra bufs list */
229     ...
230 };
231 .Ed
232 .Pp
233 Indicates the number of available rings
234 .Pa ( struct netmap_rings )
235 and their position in the mmapped region.
236 The number of tx and rx rings
237 .Pa ( ni_tx_rings , ni_rx_rings )
238 normally depends on the hardware.
239 NICs also have an extra tx/rx ring pair connected to the host stack.
240 .Em NIOCREGIF
241 can also request additional unbound buffers in the same memory space,
242 to be used as temporary storage for packets.
243 .Pa ni_bufs_head
244 contains the index of the first of these free rings,
245 which are connected in a list (the first uint32_t of each
246 buffer being the index of the next buffer in the list).
247 A 0 indicates the end of the list.
248 .Pp
249 .It Dv struct netmap_ring (one per ring)
250 .Bd -literal
251 struct netmap_ring {
252     ...
253     const uint32_t num_slots;   /* slots in each ring            */
254     const uint32_t nr_buf_size; /* size of each buffer           */
255     ...
256     uint32_t       head;        /* (u) first buf owned by user   */
257     uint32_t       cur;         /* (u) wakeup position           */
258     const uint32_t tail;        /* (k) first buf owned by kernel */
259     ...
260     uint32_t       flags;
261     struct timeval ts;          /* (k) time of last rxsync()     */
262     ...
263     struct netmap_slot slot[0]; /* array of slots                */
264 }
265 .Ed
266 .Pp
267 Implements transmit and receive rings, with read/write
268 pointers, metadata and and an array of
269 .Pa slots
270 describing the buffers.
271 .Pp
272 .It Dv struct netmap_slot (one per buffer)
273 .Bd -literal
274 struct netmap_slot {
275     uint32_t buf_idx;           /* buffer index                 */
276     uint16_t len;               /* packet length                */
277     uint16_t flags;             /* buf changed, etc.            */
278     uint64_t ptr;               /* address for indirect buffers */
279 };
280 .Ed
281 .Pp
282 Describes a packet buffer, which normally is identified by
283 an index and resides in the mmapped region.
284 .It Dv packet buffers
285 Fixed size (normally 2 KB) packet buffers allocated by the kernel.
286 .El
287 .Pp
288 The offset of the
289 .Pa struct netmap_if
290 in the mmapped region is indicated by the
291 .Pa nr_offset
292 field in the structure returned by
293 .Pa NIOCREGIF .
294 From there, all other objects are reachable through
295 relative references (offsets or indexes).
296 Macros and functions in <net/netmap_user.h>
297 help converting them into actual pointers:
298 .Pp
299 .Dl struct netmap_if  *nifp = NETMAP_IF(mem, arg.nr_offset);
300 .Dl struct netmap_ring *txr = NETMAP_TXRING(nifp, ring_index);
301 .Dl struct netmap_ring *rxr = NETMAP_RXRING(nifp, ring_index);
302 .Pp
303 .Dl char *buf = NETMAP_BUF(ring, buffer_index);
304 .Sh RINGS, BUFFERS AND DATA I/O
305 .Va Rings
306 are circular queues of packets with three indexes/pointers
307 .Va ( head , cur , tail ) ;
308 one slot is always kept empty.
309 The ring size
310 .Va ( num_slots )
311 should not be assumed to be a power of two.
312 .br
313 (NOTE: older versions of netmap used head/count format to indicate
314 the content of a ring).
315 .Pp
316 .Va head
317 is the first slot available to userspace;
318 .br
319 .Va cur
320 is the wakeup point:
321 select/poll will unblock when
322 .Va tail
323 passes
324 .Va cur ;
325 .br
326 .Va tail
327 is the first slot reserved to the kernel.
328 .Pp
329 Slot indexes MUST only move forward;
330 for convenience, the function
331 .Dl nm_ring_next(ring, index)
332 returns the next index modulo the ring size.
333 .Pp
334 .Va head
335 and
336 .Va cur
337 are only modified by the user program;
338 .Va tail
339 is only modified by the kernel.
340 The kernel only reads/writes the
341 .Vt struct netmap_ring
342 slots and buffers
343 during the execution of a netmap-related system call.
344 The only exception are slots (and buffers) in the range
345 .Va tail\  . . . head-1 ,
346 that are explicitly assigned to the kernel.
347 .Pp
348 .Ss TRANSMIT RINGS
349 On transmit rings, after a
350 .Nm
351 system call, slots in the range
352 .Va head\  . . . tail-1
353 are available for transmission.
354 User code should fill the slots sequentially
355 and advance
356 .Va head
357 and
358 .Va cur
359 past slots ready to transmit.
360 .Va cur
361 may be moved further ahead if the user code needs
362 more slots before further transmissions (see
363 .Sx SCATTER GATHER I/O ) .
364 .Pp
365 At the next NIOCTXSYNC/select()/poll(),
366 slots up to
367 .Va head-1
368 are pushed to the port, and
369 .Va tail
370 may advance if further slots have become available.
371 Below is an example of the evolution of a TX ring:
372 .Pp
373 .Bd -literal
374     after the syscall, slots between cur and tail are (a)vailable
375               head=cur   tail
376                |          |
377                v          v
378      TX  [.....aaaaaaaaaaa.............]
379
380     user creates new packets to (T)ransmit
381                 head=cur tail
382                     |     |
383                     v     v
384      TX  [.....TTTTTaaaaaa.............]
385
386     NIOCTXSYNC/poll()/select() sends packets and reports new slots
387                 head=cur      tail
388                     |          |
389                     v          v
390      TX  [..........aaaaaaaaaaa........]
391 .Ed
392 select() and poll() wlll block if there is no space in the ring, i.e.
393 .Dl ring->cur == ring->tail
394 and return when new slots have become available.
395 .Pp
396 High speed applications may want to amortize the cost of system calls
397 by preparing as many packets as possible before issuing them.
398 .Pp
399 A transmit ring with pending transmissions has
400 .Dl ring->head != ring->tail + 1 (modulo the ring size).
401 The function
402 .Va int nm_tx_pending(ring)
403 implements this test.
404 .Pp
405 .Ss RECEIVE RINGS
406 On receive rings, after a
407 .Nm
408 system call, the slots in the range
409 .Va head\& . . . tail-1
410 contain received packets.
411 User code should process them and advance
412 .Va head
413 and
414 .Va cur
415 past slots it wants to return to the kernel.
416 .Va cur
417 may be moved further ahead if the user code wants to
418 wait for more packets
419 without returning all the previous slots to the kernel.
420 .Pp
421 At the next NIOCRXSYNC/select()/poll(),
422 slots up to
423 .Va head-1
424 are returned to the kernel for further receives, and
425 .Va tail
426 may advance to report new incoming packets.
427 .br
428 Below is an example of the evolution of an RX ring:
429 .Bd -literal
430     after the syscall, there are some (h)eld and some (R)eceived slots
431            head  cur     tail
432             |     |       |
433             v     v       v
434      RX  [..hhhhhhRRRRRRRR..........]
435
436     user advances head and cur, releasing some slots and holding others
437                head cur  tail
438                  |  |     |
439                  v  v     v
440      RX  [..*****hhhRRRRRR...........]
441
442     NICRXSYNC/poll()/select() recovers slots and reports new packets
443                head cur        tail
444                  |  |           |
445                  v  v           v
446      RX  [.......hhhRRRRRRRRRRRR....]
447 .Ed
448 .Pp
449 .Sh SLOTS AND PACKET BUFFERS
450 Normally, packets should be stored in the netmap-allocated buffers
451 assigned to slots when ports are bound to a file descriptor.
452 One packet is fully contained in a single buffer.
453 .Pp
454 The following flags affect slot and buffer processing:
455 .Bl -tag -width XXX
456 .It NS_BUF_CHANGED
457 it MUST be used when the buf_idx in the slot is changed.
458 This can be used to implement
459 zero-copy forwarding, see
460 .Sx ZERO-COPY FORWARDING .
461 .Pp
462 .It NS_REPORT
463 reports when this buffer has been transmitted.
464 Normally,
465 .Nm
466 notifies transmit completions in batches, hence signals
467 can be delayed indefinitely. This flag helps detecting
468 when packets have been send and a file descriptor can be closed.
469 .It NS_FORWARD
470 When a ring is in 'transparent' mode (see
471 .Sx TRANSPARENT MODE ) ,
472 packets marked with this flags are forwarded to the other endpoint
473 at the next system call, thus restoring (in a selective way)
474 the connection between a NIC and the host stack.
475 .It NS_NO_LEARN
476 tells the forwarding code that the SRC MAC address for this
477 packet must not be used in the learning bridge code.
478 .It NS_INDIRECT
479 indicates that the packet's payload is in a user-supplied buffer,
480 whose user virtual address is in the 'ptr' field of the slot.
481 The size can reach 65535 bytes.
482 .br
483 This is only supported on the transmit ring of
484 .Nm VALE
485 ports, and it helps reducing data copies in the interconnection
486 of virtual machines.
487 .It NS_MOREFRAG
488 indicates that the packet continues with subsequent buffers;
489 the last buffer in a packet must have the flag clear.
490 .El
491 .Sh SCATTER GATHER I/O
492 Packets can span multiple slots if the
493 .Va NS_MOREFRAG
494 flag is set in all but the last slot.
495 The maximum length of a chain is 64 buffers.
496 This is normally used with
497 .Nm VALE
498 ports when connecting virtual machines, as they generate large
499 TSO segments that are not split unless they reach a physical device.
500 .Pp
501 NOTE: The length field always refers to the individual
502 fragment; there is no place with the total length of a packet.
503 .Pp
504 On receive rings the macro
505 .Va NS_RFRAGS(slot)
506 indicates the remaining number of slots for this packet,
507 including the current one.
508 Slots with a value greater than 1 also have NS_MOREFRAG set.
509 .Sh IOCTLS
510 .Nm
511 uses two ioctls (NIOCTXSYNC, NIOCRXSYNC)
512 for non-blocking I/O. They take no argument.
513 Two more ioctls (NIOCGINFO, NIOCREGIF) are used
514 to query and configure ports, with the following argument:
515 .Bd -literal
516 struct nmreq {
517     char      nr_name[IFNAMSIZ]; /* (i) port name                  */
518     uint32_t  nr_version;        /* (i) API version                */
519     uint32_t  nr_offset;         /* (o) nifp offset in mmap region */
520     uint32_t  nr_memsize;        /* (o) size of the mmap region    */
521     uint32_t  nr_tx_slots;       /* (i/o) slots in tx rings        */
522     uint32_t  nr_rx_slots;       /* (i/o) slots in rx rings        */
523     uint16_t  nr_tx_rings;       /* (i/o) number of tx rings       */
524     uint16_t  nr_rx_rings;       /* (i/o) number of tx rings       */
525     uint16_t  nr_ringid;         /* (i/o) ring(s) we care about    */
526     uint16_t  nr_cmd;            /* (i) special command            */
527     uint16_t  nr_arg1;           /* (i/o) extra arguments          */
528     uint16_t  nr_arg2;           /* (i/o) extra arguments          */
529     uint32_t  nr_arg3;           /* (i/o) extra arguments          */
530     uint32_t  nr_flags           /* (i/o) open mode                */
531     ...
532 };
533 .Ed
534 .Pp
535 A file descriptor obtained through
536 .Pa /dev/netmap
537 also supports the ioctl supported by network devices, see
538 .Xr netintro 4 .
539 .Pp
540 .Bl -tag -width XXXX
541 .It Dv NIOCGINFO
542 returns EINVAL if the named port does not support netmap.
543 Otherwise, it returns 0 and (advisory) information
544 about the port.
545 Note that all the information below can change before the
546 interface is actually put in netmap mode.
547 .Pp
548 .Bl -tag -width XX
549 .It Pa nr_memsize
550 indicates the size of the
551 .Nm
552 memory region. NICs in
553 .Nm
554 mode all share the same memory region,
555 whereas
556 .Nm VALE
557 ports have independent regions for each port.
558 .It Pa nr_tx_slots , nr_rx_slots
559 indicate the size of transmit and receive rings.
560 .It Pa nr_tx_rings , nr_rx_rings
561 indicate the number of transmit
562 and receive rings.
563 Both ring number and sizes may be configured at runtime
564 using interface-specific functions (e.g.
565 .Xr ethtool
566 ).
567 .El
568 .It Dv NIOCREGIF
569 binds the port named in
570 .Va nr_name
571 to the file descriptor. For a physical device this also switches it into
572 .Nm
573 mode, disconnecting
574 it from the host stack.
575 Multiple file descriptors can be bound to the same port,
576 with proper synchronization left to the user.
577 .Pp
578 .Dv NIOCREGIF can also bind a file descriptor to one endpoint of a
579 .Em netmap pipe ,
580 consisting of two netmap ports with a crossover connection.
581 A netmap pipe share the same memory space of the parent port,
582 and is meant to enable configuration where a master process acts
583 as a dispatcher towards slave processes.
584 .Pp
585 To enable this function, the
586 .Pa nr_arg1
587 field of the structure can be used as a hint to the kernel to
588 indicate how many pipes we expect to use, and reserve extra space
589 in the memory region.
590 .Pp
591 On return, it gives the same info as NIOCGINFO,
592 with
593 .Pa nr_ringid
594 and
595 .Pa nr_flags
596 indicating the identity of the rings controlled through the file
597 descriptor.
598 .Pp
599 .Va nr_flags
600 .Va nr_ringid
601 selects which rings are controlled through this file descriptor.
602 Possible values of
603 .Pa nr_flags
604 are indicated below, together with the naming schemes
605 that application libraries (such as the
606 .Nm nm_open
607 indicated below) can use to indicate the specific set of rings.
608 In the example below, "netmap:foo" is any valid netmap port name.
609 .Pp
610 .Bl -tag -width XXXXX
611 .It NR_REG_ALL_NIC                         "netmap:foo"
612 (default) all hardware ring pairs
613 .It NR_REG_SW_NIC           "netmap:foo^"
614 the ``host rings'', connecting to the host stack.
615 .It NR_RING_NIC_SW        "netmap:foo+
616 all hardware rings and the host rings
617 .It NR_REG_ONE_NIC       "netmap:foo-i"
618 only the i-th hardware ring pair, where the number is in
619 .Pa nr_ringid ;
620 .It NR_REG_PIPE_MASTER  "netmap:foo{i"
621 the master side of the netmap pipe whose identifier (i) is in
622 .Pa nr_ringid ;
623 .It NR_REG_PIPE_SLAVE   "netmap:foo}i"
624 the slave side of the netmap pipe whose identifier (i) is in
625 .Pa nr_ringid .
626 .Pp
627 The identifier of a pipe must be thought as part of the pipe name,
628 and does not need to be sequential. On return the pipe
629 will only have a single ring pair with index 0,
630 irrespective of the value of i.
631 .El
632 .Pp
633 By default, a
634 .Xr poll 2
635 or
636 .Xr select 2
637 call pushes out any pending packets on the transmit ring, even if
638 no write events are specified.
639 The feature can be disabled by or-ing
640 .Va NETMAP_NO_TX_SYNC
641 to the value written to
642 .Va nr_ringid.
643 When this feature is used,
644 packets are transmitted only on
645 .Va ioctl(NIOCTXSYNC)
646 or select()/poll() are called with a write event (POLLOUT/wfdset) or a full ring.
647 .Pp
648 When registering a virtual interface that is dynamically created to a
649 .Xr vale 4
650 switch, we can specify the desired number of rings (1 by default,
651 and currently up to 16) on it using nr_tx_rings and nr_rx_rings fields.
652 .It Dv NIOCTXSYNC
653 tells the hardware of new packets to transmit, and updates the
654 number of slots available for transmission.
655 .It Dv NIOCRXSYNC
656 tells the hardware of consumed packets, and asks for newly available
657 packets.
658 .El
659 .Sh SELECT, POLL, EPOLL, KQUEUE.
660 .Xr select 2
661 and
662 .Xr poll 2
663 on a
664 .Nm
665 file descriptor process rings as indicated in
666 .Sx TRANSMIT RINGS
667 and
668 .Sx RECEIVE RINGS ,
669 respectively when write (POLLOUT) and read (POLLIN) events are requested.
670 Both block if no slots are available in the ring
671 .Va ( ring->cur == ring->tail ) .
672 Depending on the platform,
673 .Xr epoll 2
674 and
675 .Xr kqueue 2
676 are supported too.
677 .Pp
678 Packets in transmit rings are normally pushed out
679 (and buffers reclaimed) even without
680 requesting write events. Passing the NETMAP_NO_TX_SYNC flag to
681 .Em NIOCREGIF
682 disables this feature.
683 By default, receive rings are processed only if read
684 events are requested. Passing the NETMAP_DO_RX_SYNC flag to
685 .Em NIOCREGIF updates receive rings even without read events.
686 Note that on epoll and kqueue, NETMAP_NO_TX_SYNC and NETMAP_DO_RX_SYNC
687 only have an effect when some event is posted for the file descriptor.
688 .Sh LIBRARIES
689 The
690 .Nm
691 API is supposed to be used directly, both because of its simplicity and
692 for efficient integration with applications.
693 .Pp
694 For conveniency, the
695 .Va <net/netmap_user.h>
696 header provides a few macros and functions to ease creating
697 a file descriptor and doing I/O with a
698 .Nm
699 port. These are loosely modeled after the
700 .Xr pcap 3
701 API, to ease porting of libpcap-based applications to
702 .Nm .
703 To use these extra functions, programs should
704 .Dl #define NETMAP_WITH_LIBS
705 before
706 .Dl #include <net/netmap_user.h>
707 .Pp
708 The following functions are available:
709 .Bl -tag -width XXXXX
710 .It Va  struct nm_desc * nm_open(const char *ifname, const struct nmreq *req, uint64_t flags, const struct nm_desc *arg)
711 similar to
712 .Xr pcap_open ,
713 binds a file descriptor to a port.
714 .Bl -tag -width XX
715 .It Va ifname
716 is a port name, in the form "netmap:XXX" for a NIC and "valeXXX:YYY" for a
717 .Nm VALE
718 port.
719 .It Va req
720 provides the initial values for the argument to the NIOCREGIF ioctl.
721 The nm_flags and nm_ringid values are overwritten by parsing
722 ifname and flags, and other fields can be overridden through
723 the other two arguments.
724 .It Va arg
725 points to a struct nm_desc containing arguments (e.g. from a previously
726 open file descriptor) that should override the defaults.
727 The fields are used as described below
728 .It Va flags
729 can be set to a combination of the following flags:
730 .Va NETMAP_NO_TX_POLL ,
731 .Va NETMAP_DO_RX_POLL
732 (copied into nr_ringid);
733 .Va NM_OPEN_NO_MMAP (if arg points to the same memory region,
734 avoids the mmap and uses the values from it);
735 .Va NM_OPEN_IFNAME (ignores ifname and uses the values in arg);
736 .Va NM_OPEN_ARG1 ,
737 .Va NM_OPEN_ARG2 ,
738 .Va NM_OPEN_ARG3 (uses the fields from arg);
739 .Va NM_OPEN_RING_CFG (uses the ring number and sizes from arg).
740 .El
741 .It Va int nm_close(struct nm_desc *d)
742 closes the file descriptor, unmaps memory, frees resources.
743 .It Va int nm_inject(struct nm_desc *d, const void *buf, size_t size)
744 similar to pcap_inject(), pushes a packet to a ring, returns the size
745 of the packet is successful, or 0 on error;
746 .It Va int nm_dispatch(struct nm_desc *d, int cnt, nm_cb_t cb, u_char *arg)
747 similar to pcap_dispatch(), applies a callback to incoming packets
748 .It Va u_char * nm_nextpkt(struct nm_desc *d, struct nm_pkthdr *hdr)
749 similar to pcap_next(), fetches the next packet
750 .Pp
751 .El
752 .Sh SUPPORTED DEVICES
753 .Nm
754 natively supports the following devices:
755 .Pp
756 On FreeBSD:
757 .Xr em 4 ,
758 .Xr igb 4 ,
759 .Xr ixgbe 4 ,
760 .Xr lem 4 ,
761 .Xr re 4 .
762 .Pp
763 On Linux
764 .Xr e1000 4 ,
765 .Xr e1000e 4 ,
766 .Xr igb 4 ,
767 .Xr ixgbe 4 ,
768 .Xr mlx4 4 ,
769 .Xr forcedeth 4 ,
770 .Xr r8169 4 .
771 .Pp
772 NICs without native support can still be used in
773 .Nm
774 mode through emulation. Performance is inferior to native netmap
775 mode but still significantly higher than sockets, and approaching
776 that of in-kernel solutions such as Linux's
777 .Xr pktgen .
778 .Pp
779 Emulation is also available for devices with native netmap support,
780 which can be used for testing or performance comparison.
781 The sysctl variable
782 .Va dev.netmap.admode
783 globally controls how netmap mode is implemented.
784 .Sh SYSCTL VARIABLES AND MODULE PARAMETERS
785 Some aspect of the operation of
786 .Nm
787 are controlled through sysctl variables on FreeBSD
788 .Em ( dev.netmap.* )
789 and module parameters on Linux
790 .Em ( /sys/module/netmap_lin/parameters/* ) :
791 .Pp
792 .Bl -tag -width indent
793 .It Va dev.netmap.admode: 0
794 Controls the use of native or emulated adapter mode.
795 0 uses the best available option, 1 forces native and
796 fails if not available, 2 forces emulated hence never fails.
797 .It Va dev.netmap.generic_ringsize: 1024
798 Ring size used for emulated netmap mode
799 .It Va dev.netmap.generic_mit: 100000
800 Controls interrupt moderation for emulated mode
801 .It Va dev.netmap.mmap_unreg: 0
802 .It Va dev.netmap.fwd: 0
803 Forces NS_FORWARD mode
804 .It Va dev.netmap.flags: 0
805 .It Va dev.netmap.txsync_retry: 2
806 .It Va dev.netmap.no_pendintr: 1
807 Forces recovery of transmit buffers on system calls
808 .It Va dev.netmap.mitigate: 1
809 Propagates interrupt mitigation to user processes
810 .It Va dev.netmap.no_timestamp: 0
811 Disables the update of the timestamp in the netmap ring
812 .It Va dev.netmap.verbose: 0
813 Verbose kernel messages
814 .It Va dev.netmap.buf_num: 163840
815 .It Va dev.netmap.buf_size: 2048
816 .It Va dev.netmap.ring_num: 200
817 .It Va dev.netmap.ring_size: 36864
818 .It Va dev.netmap.if_num: 100
819 .It Va dev.netmap.if_size: 1024
820 Sizes and number of objects (netmap_if, netmap_ring, buffers)
821 for the global memory region. The only parameter worth modifying is
822 .Va dev.netmap.buf_num
823 as it impacts the total amount of memory used by netmap.
824 .It Va dev.netmap.buf_curr_num: 0
825 .It Va dev.netmap.buf_curr_size: 0
826 .It Va dev.netmap.ring_curr_num: 0
827 .It Va dev.netmap.ring_curr_size: 0
828 .It Va dev.netmap.if_curr_num: 0
829 .It Va dev.netmap.if_curr_size: 0
830 Actual values in use.
831 .It Va dev.netmap.bridge_batch: 1024
832 Batch size used when moving packets across a
833 .Nm VALE
834 switch. Values above 64 generally guarantee good
835 performance.
836 .El
837 .Sh SYSTEM CALLS
838 .Nm
839 uses
840 .Xr select 2 ,
841 .Xr poll 2 ,
842 .Xr epoll
843 and
844 .Xr kqueue
845 to wake up processes when significant events occur, and
846 .Xr mmap 2
847 to map memory.
848 .Xr ioctl 2
849 is used to configure ports and
850 .Nm VALE switches .
851 .Pp
852 Applications may need to create threads and bind them to
853 specific cores to improve performance, using standard
854 OS primitives, see
855 .Xr pthread 3 .
856 In particular,
857 .Xr pthread_setaffinity_np 3
858 may be of use.
859 .Sh CAVEATS
860 No matter how fast the CPU and OS are,
861 achieving line rate on 10G and faster interfaces
862 requires hardware with sufficient performance.
863 Several NICs are unable to sustain line rate with
864 small packet sizes. Insufficient PCIe or memory bandwidth
865 can also cause reduced performance.
866 .Pp
867 Another frequent reason for low performance is the use
868 of flow control on the link: a slow receiver can limit
869 the transmit speed.
870 Be sure to disable flow control when running high
871 speed experiments.
872 .Pp
873 .Ss SPECIAL NIC FEATURES
874 .Nm
875 is orthogonal to some NIC features such as
876 multiqueue, schedulers, packet filters.
877 .Pp
878 Multiple transmit and receive rings are supported natively
879 and can be configured with ordinary OS tools,
880 such as
881 .Xr ethtool
882 or
883 device-specific sysctl variables.
884 The same goes for Receive Packet Steering (RPS)
885 and filtering of incoming traffic.
886 .Pp
887 .Nm
888 .Em does not use
889 features such as
890 .Em checksum offloading , TCP segmentation offloading ,
891 .Em encryption , VLAN encapsulation/decapsulation ,
892 etc. .
893 When using netmap to exchange packets with the host stack,
894 make sure to disable these features.
895 .Sh EXAMPLES
896 .Ss TEST PROGRAMS
897 .Nm
898 comes with a few programs that can be used for testing or
899 simple applications.
900 See the
901 .Va examples/
902 directory in
903 .Nm
904 distributions, or
905 .Va tools/tools/netmap/
906 directory in FreeBSD distributions.
907 .Pp
908 .Xr pkt-gen
909 is a general purpose traffic source/sink.
910 .Pp
911 As an example
912 .Dl pkt-gen -i ix0 -f tx -l 60
913 can generate an infinite stream of minimum size packets, and
914 .Dl pkt-gen -i ix0 -f rx
915 is a traffic sink.
916 Both print traffic statistics, to help monitor
917 how the system performs.
918 .Pp
919 .Xr pkt-gen
920 has many options can be uses to set packet sizes, addresses,
921 rates, and use multiple send/receive threads and cores.
922 .Pp
923 .Xr bridge
924 is another test program which interconnects two
925 .Nm
926 ports. It can be used for transparent forwarding between
927 interfaces, as in
928 .Dl bridge -i ix0 -i ix1
929 or even connect the NIC to the host stack using netmap
930 .Dl bridge -i ix0 -i ix0
931 .Ss USING THE NATIVE API
932 The following code implements a traffic generator
933 .Pp
934 .Bd -literal -compact
935 #include <net/netmap_user.h>
936 ...
937 void sender(void)
938 {
939     struct netmap_if *nifp;
940     struct netmap_ring *ring;
941     struct nmreq nmr;
942     struct pollfd fds;
943
944     fd = open("/dev/netmap", O_RDWR);
945     bzero(&nmr, sizeof(nmr));
946     strcpy(nmr.nr_name, "ix0");
947     nmr.nm_version = NETMAP_API;
948     ioctl(fd, NIOCREGIF, &nmr);
949     p = mmap(0, nmr.nr_memsize, fd);
950     nifp = NETMAP_IF(p, nmr.nr_offset);
951     ring = NETMAP_TXRING(nifp, 0);
952     fds.fd = fd;
953     fds.events = POLLOUT;
954     for (;;) {
955         poll(&fds, 1, -1);
956         while (!nm_ring_empty(ring)) {
957             i = ring->cur;
958             buf = NETMAP_BUF(ring, ring->slot[i].buf_index);
959             ... prepare packet in buf ...
960             ring->slot[i].len = ... packet length ...
961             ring->head = ring->cur = nm_ring_next(ring, i);
962         }
963     }
964 }
965 .Ed
966 .Ss HELPER FUNCTIONS
967 A simple receiver can be implemented using the helper functions
968 .Bd -literal -compact
969 #define NETMAP_WITH_LIBS
970 #include <net/netmap_user.h>
971 ...
972 void receiver(void)
973 {
974     struct nm_desc *d;
975     struct pollfd fds;
976     u_char *buf;
977     struct nm_pkthdr h;
978     ...
979     d = nm_open("netmap:ix0", NULL, 0, 0);
980     fds.fd = NETMAP_FD(d);
981     fds.events = POLLIN;
982     for (;;) {
983         poll(&fds, 1, -1);
984         while ( (buf = nm_nextpkt(d, &h)) )
985             consume_pkt(buf, h->len);
986     }
987     nm_close(d);
988 }
989 .Ed
990 .Ss ZERO-COPY FORWARDING
991 Since physical interfaces share the same memory region,
992 it is possible to do packet forwarding between ports
993 swapping buffers. The buffer from the transmit ring is used
994 to replenish the receive ring:
995 .Bd -literal -compact
996     uint32_t tmp;
997     struct netmap_slot *src, *dst;
998     ...
999     src = &src_ring->slot[rxr->cur];
1000     dst = &dst_ring->slot[txr->cur];
1001     tmp = dst->buf_idx;
1002     dst->buf_idx = src->buf_idx;
1003     dst->len = src->len;
1004     dst->flags = NS_BUF_CHANGED;
1005     src->buf_idx = tmp;
1006     src->flags = NS_BUF_CHANGED;
1007     rxr->head = rxr->cur = nm_ring_next(rxr, rxr->cur);
1008     txr->head = txr->cur = nm_ring_next(txr, txr->cur);
1009     ...
1010 .Ed
1011 .Ss ACCESSING THE HOST STACK
1012 The host stack is for all practical purposes just a regular ring pair,
1013 which you can access with the netmap API (e.g. with
1014 .Dl nm_open("netmap:eth0^", ... ) ;
1015 All packets that the host would send to an interface in
1016 .Nm
1017 mode end up into the RX ring, whereas all packets queued to the
1018 TX ring are send up to the host stack.
1019 .Ss VALE SWITCH
1020 A simple way to test the performance of a
1021 .Nm VALE
1022 switch is to attach a sender and a receiver to it,
1023 e.g. running the following in two different terminals:
1024 .Dl pkt-gen -i vale1:a -f rx # receiver
1025 .Dl pkt-gen -i vale1:b -f tx # sender
1026 The same example can be used to test netmap pipes, by simply
1027 changing port names, e.g.
1028 .Dl pkt-gen -i vale:x{3 -f rx # receiver on the master side
1029 .Dl pkt-gen -i vale:x}3 -f tx # sender on the slave side
1030 .Pp
1031 The following command attaches an interface and the host stack
1032 to a switch:
1033 .Dl vale-ctl -h vale2:em0
1034 Other
1035 .Nm
1036 clients attached to the same switch can now communicate
1037 with the network card or the host.
1038 .Pp
1039 .Sh SEE ALSO
1040 .Pp
1041 http://info.iet.unipi.it/~luigi/netmap/
1042 .Pp
1043 Luigi Rizzo, Revisiting network I/O APIs: the netmap framework,
1044 Communications of the ACM, 55 (3), pp.45-51, March 2012
1045 .Pp
1046 Luigi Rizzo, netmap: a novel framework for fast packet I/O,
1047 Usenix ATC'12, June 2012, Boston
1048 .Pp
1049 Luigi Rizzo, Giuseppe Lettieri,
1050 VALE, a switched ethernet for virtual machines,
1051 ACM CoNEXT'12, December 2012, Nice
1052 .Pp
1053 Luigi Rizzo, Giuseppe Lettieri, Vincenzo Maffione,
1054 Speeding up packet I/O in virtual machines,
1055 ACM/IEEE ANCS'13, October 2013, San Jose
1056 .Sh AUTHORS
1057 .An -nosplit
1058 The
1059 .Nm
1060 framework has been originally designed and implemented at the
1061 Universita` di Pisa in 2011 by
1062 .An Luigi Rizzo ,
1063 and further extended with help from
1064 .An Matteo Landi ,
1065 .An Gaetano Catalli ,
1066 .An Giuseppe Lettieri ,
1067 .An Vincenzo Maffione .
1068 .Pp
1069 .Nm
1070 and
1071 .Nm VALE
1072 have been funded by the European Commission within FP7 Projects
1073 CHANGE (257422) and OPENLAB (287581).