ia64/linux-2.6.18-xen.hg

view Documentation/networking/packet_mmap.txt @ 897:329ea0ccb344

balloon: try harder to balloon up under memory pressure.

Currently if the balloon driver is unable to increase the guest's
reservation it assumes the failure was due to reaching its full
allocation, gives up on the ballooning operation and records the limit
it reached as the "hard limit". The driver will not try again until
the target is set again (even to the same value).

However it is possible that ballooning has in fact failed due to
memory pressure in the host and therefore it is desirable to keep
attempting to reach the target in case memory becomes available. The
most likely scenario is that some guests are ballooning down while
others are ballooning up and therefore there is temporary memory
pressure while things stabilise. You would not expect a well behaved
toolstack to ask a domain to balloon to more than its allocation nor
would you expect it to deliberately over-commit memory by setting
balloon targets which exceed the total host memory.

This patch drops the concept of a hard limit and causes the balloon
driver to retry increasing the reservation on a timer in the same
manner as when decreasing the reservation.

Also if we partially succeed in increasing the reservation
(i.e. receive less pages than we asked for) then we may as well keep
those pages rather than returning them to Xen.

Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
author Keir Fraser <keir.fraser@citrix.com>
date Fri Jun 05 14:01:20 2009 +0100 (2009-06-05)
parents 831230e53067
children
line source
1 --------------------------------------------------------------------------------
2 + ABSTRACT
3 --------------------------------------------------------------------------------
5 This file documents the CONFIG_PACKET_MMAP option available with the PACKET
6 socket interface on 2.4 and 2.6 kernels. This type of sockets is used for
7 capture network traffic with utilities like tcpdump or any other that uses
8 the libpcap library.
10 You can find the latest version of this document at
12 http://pusa.uv.es/~ulisses/packet_mmap/
14 Please send me your comments to
16 Ulisses Alonso Camaró <uaca@i.hate.spam.alumni.uv.es>
18 -------------------------------------------------------------------------------
19 + Why use PACKET_MMAP
20 --------------------------------------------------------------------------------
22 In Linux 2.4/2.6 if PACKET_MMAP is not enabled, the capture process is very
23 inefficient. It uses very limited buffers and requires one system call
24 to capture each packet, it requires two if you want to get packet's
25 timestamp (like libpcap always does).
27 In the other hand PACKET_MMAP is very efficient. PACKET_MMAP provides a size
28 configurable circular buffer mapped in user space. This way reading packets just
29 needs to wait for them, most of the time there is no need to issue a single
30 system call. By using a shared buffer between the kernel and the user
31 also has the benefit of minimizing packet copies.
33 It's fine to use PACKET_MMAP to improve the performance of the capture process,
34 but it isn't everything. At least, if you are capturing at high speeds (this
35 is relative to the cpu speed), you should check if the device driver of your
36 network interface card supports some sort of interrupt load mitigation or
37 (even better) if it supports NAPI, also make sure it is enabled.
39 --------------------------------------------------------------------------------
40 + How to use CONFIG_PACKET_MMAP
41 --------------------------------------------------------------------------------
43 From the user standpoint, you should use the higher level libpcap library, which
44 is a de facto standard, portable across nearly all operating systems
45 including Win32.
47 Said that, at time of this writing, official libpcap 0.8.1 is out and doesn't include
48 support for PACKET_MMAP, and also probably the libpcap included in your distribution.
50 I'm aware of two implementations of PACKET_MMAP in libpcap:
52 http://pusa.uv.es/~ulisses/packet_mmap/ (by Simon Patarin, based on libpcap 0.6.2)
53 http://public.lanl.gov/cpw/ (by Phil Wood, based on lastest libpcap)
55 The rest of this document is intended for people who want to understand
56 the low level details or want to improve libpcap by including PACKET_MMAP
57 support.
59 --------------------------------------------------------------------------------
60 + How to use CONFIG_PACKET_MMAP directly
61 --------------------------------------------------------------------------------
63 From the system calls stand point, the use of PACKET_MMAP involves
64 the following process:
67 [setup] socket() -------> creation of the capture socket
68 setsockopt() ---> allocation of the circular buffer (ring)
69 mmap() ---------> maping of the allocated buffer to the
70 user process
72 [capture] poll() ---------> to wait for incoming packets
74 [shutdown] close() --------> destruction of the capture socket and
75 deallocation of all associated
76 resources.
79 socket creation and destruction is straight forward, and is done
80 the same way with or without PACKET_MMAP:
82 int fd;
84 fd= socket(PF_PACKET, mode, htons(ETH_P_ALL))
86 where mode is SOCK_RAW for the raw interface were link level
87 information can be captured or SOCK_DGRAM for the cooked
88 interface where link level information capture is not
89 supported and a link level pseudo-header is provided
90 by the kernel.
92 The destruction of the socket and all associated resources
93 is done by a simple call to close(fd).
95 Next I will describe PACKET_MMAP settings and it's constraints,
96 also the maping of the circular buffer in the user process and
97 the use of this buffer.
99 --------------------------------------------------------------------------------
100 + PACKET_MMAP settings
101 --------------------------------------------------------------------------------
104 To setup PACKET_MMAP from user level code is done with a call like
106 setsockopt(fd, SOL_PACKET, PACKET_RX_RING, (void *) &req, sizeof(req))
108 The most significant argument in the previous call is the req parameter,
109 this parameter must to have the following structure:
111 struct tpacket_req
112 {
113 unsigned int tp_block_size; /* Minimal size of contiguous block */
114 unsigned int tp_block_nr; /* Number of blocks */
115 unsigned int tp_frame_size; /* Size of frame */
116 unsigned int tp_frame_nr; /* Total number of frames */
117 };
119 This structure is defined in /usr/include/linux/if_packet.h and establishes a
120 circular buffer (ring) of unswappable memory mapped in the capture process.
121 Being mapped in the capture process allows reading the captured frames and
122 related meta-information like timestamps without requiring a system call.
124 Captured frames are grouped in blocks. Each block is a physically contiguous
125 region of memory and holds tp_block_size/tp_frame_size frames. The total number
126 of blocks is tp_block_nr. Note that tp_frame_nr is a redundant parameter because
128 frames_per_block = tp_block_size/tp_frame_size
130 indeed, packet_set_ring checks that the following condition is true
132 frames_per_block * tp_block_nr == tp_frame_nr
135 Lets see an example, with the following values:
137 tp_block_size= 4096
138 tp_frame_size= 2048
139 tp_block_nr = 4
140 tp_frame_nr = 8
142 we will get the following buffer structure:
144 block #1 block #2
145 +---------+---------+ +---------+---------+
146 | frame 1 | frame 2 | | frame 3 | frame 4 |
147 +---------+---------+ +---------+---------+
149 block #3 block #4
150 +---------+---------+ +---------+---------+
151 | frame 5 | frame 6 | | frame 7 | frame 8 |
152 +---------+---------+ +---------+---------+
154 A frame can be of any size with the only condition it can fit in a block. A block
155 can only hold an integer number of frames, or in other words, a frame cannot
156 be spawn accross two blocks so there are some datails you have to take into
157 account when choosing the frame_size. See "Maping and use of the circular
158 buffer (ring)".
161 --------------------------------------------------------------------------------
162 + PACKET_MMAP setting constraints
163 --------------------------------------------------------------------------------
165 In kernel versions prior to 2.4.26 (for the 2.4 branch) and 2.6.5 (2.6 branch),
166 the PACKET_MMAP buffer could hold only 32768 frames in a 32 bit architecture or
167 16384 in a 64 bit architecture. For information on these kernel versions
168 see http://pusa.uv.es/~ulisses/packet_mmap/packet_mmap.pre-2.4.26_2.6.5.txt
170 Block size limit
171 ------------------
173 As stated earlier, each block is a contiguous physical region of memory. These
174 memory regions are allocated with calls to the __get_free_pages() function. As
175 the name indicates, this function allocates pages of memory, and the second
176 argument is "order" or a power of two number of pages, that is
177 (for PAGE_SIZE == 4096) order=0 ==> 4096 bytes, order=1 ==> 8192 bytes,
178 order=2 ==> 16384 bytes, etc. The maximum size of a
179 region allocated by __get_free_pages is determined by the MAX_ORDER macro. More
180 precisely the limit can be calculated as:
182 PAGE_SIZE << MAX_ORDER
184 In a i386 architecture PAGE_SIZE is 4096 bytes
185 In a 2.4/i386 kernel MAX_ORDER is 10
186 In a 2.6/i386 kernel MAX_ORDER is 11
188 So get_free_pages can allocate as much as 4MB or 8MB in a 2.4/2.6 kernel
189 respectively, with an i386 architecture.
191 User space programs can include /usr/include/sys/user.h and
192 /usr/include/linux/mmzone.h to get PAGE_SIZE MAX_ORDER declarations.
194 The pagesize can also be determined dynamically with the getpagesize (2)
195 system call.
198 Block number limit
199 --------------------
201 To understand the constraints of PACKET_MMAP, we have to see the structure
202 used to hold the pointers to each block.
204 Currently, this structure is a dynamically allocated vector with kmalloc
205 called pg_vec, its size limits the number of blocks that can be allocated.
207 +---+---+---+---+
208 | x | x | x | x |
209 +---+---+---+---+
210 | | | |
211 | | | v
212 | | v block #4
213 | v block #3
214 v block #2
215 block #1
218 kmalloc allocates any number of bytes of phisically contiguous memory from
219 a pool of pre-determined sizes. This pool of memory is mantained by the slab
220 allocator which is at the end the responsible for doing the allocation and
221 hence which imposes the maximum memory that kmalloc can allocate.
223 In a 2.4/2.6 kernel and the i386 architecture, the limit is 131072 bytes. The
224 predetermined sizes that kmalloc uses can be checked in the "size-<bytes>"
225 entries of /proc/slabinfo
227 In a 32 bit architecture, pointers are 4 bytes long, so the total number of
228 pointers to blocks is
230 131072/4 = 32768 blocks
233 PACKET_MMAP buffer size calculator
234 ------------------------------------
236 Definitions:
238 <size-max> : is the maximum size of allocable with kmalloc (see /proc/slabinfo)
239 <pointer size>: depends on the architecture -- sizeof(void *)
240 <page size> : depends on the architecture -- PAGE_SIZE or getpagesize (2)
241 <max-order> : is the value defined with MAX_ORDER
242 <frame size> : it's an upper bound of frame's capture size (more on this later)
244 from these definitions we will derive
246 <block number> = <size-max>/<pointer size>
247 <block size> = <pagesize> << <max-order>
249 so, the max buffer size is
251 <block number> * <block size>
253 and, the number of frames be
255 <block number> * <block size> / <frame size>
257 Suppose the following parameters, which apply for 2.6 kernel and an
258 i386 architecture:
260 <size-max> = 131072 bytes
261 <pointer size> = 4 bytes
262 <pagesize> = 4096 bytes
263 <max-order> = 11
265 and a value for <frame size> of 2048 byteas. These parameters will yield
267 <block number> = 131072/4 = 32768 blocks
268 <block size> = 4096 << 11 = 8 MiB.
270 and hence the buffer will have a 262144 MiB size. So it can hold
271 262144 MiB / 2048 bytes = 134217728 frames
274 Actually, this buffer size is not possible with an i386 architecture.
275 Remember that the memory is allocated in kernel space, in the case of
276 an i386 kernel's memory size is limited to 1GiB.
278 All memory allocations are not freed until the socket is closed. The memory
279 allocations are done with GFP_KERNEL priority, this basically means that
280 the allocation can wait and swap other process' memory in order to allocate
281 the nececessary memory, so normally limits can be reached.
283 Other constraints
284 -------------------
286 If you check the source code you will see that what I draw here as a frame
287 is not only the link level frame. At the begining of each frame there is a
288 header called struct tpacket_hdr used in PACKET_MMAP to hold link level's frame
289 meta information like timestamp. So what we draw here a frame it's really
290 the following (from include/linux/if_packet.h):
292 /*
293 Frame structure:
295 - Start. Frame must be aligned to TPACKET_ALIGNMENT=16
296 - struct tpacket_hdr
297 - pad to TPACKET_ALIGNMENT=16
298 - struct sockaddr_ll
299 - Gap, chosen so that packet data (Start+tp_net) alignes to
300 TPACKET_ALIGNMENT=16
301 - Start+tp_mac: [ Optional MAC header ]
302 - Start+tp_net: Packet data, aligned to TPACKET_ALIGNMENT=16.
303 - Pad to align to TPACKET_ALIGNMENT=16
304 */
307 The following are conditions that are checked in packet_set_ring
309 tp_block_size must be a multiple of PAGE_SIZE (1)
310 tp_frame_size must be greater than TPACKET_HDRLEN (obvious)
311 tp_frame_size must be a multiple of TPACKET_ALIGNMENT
312 tp_frame_nr must be exactly frames_per_block*tp_block_nr
314 Note that tp_block_size should be choosed to be a power of two or there will
315 be a waste of memory.
317 --------------------------------------------------------------------------------
318 + Maping and use of the circular buffer (ring)
319 --------------------------------------------------------------------------------
321 The maping of the buffer in the user process is done with the conventional
322 mmap function. Even the circular buffer is compound of several physically
323 discontiguous blocks of memory, they are contiguous to the user space, hence
324 just one call to mmap is needed:
326 mmap(0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
328 If tp_frame_size is a divisor of tp_block_size frames will be
329 contiguosly spaced by tp_frame_size bytes. If not, each
330 tp_block_size/tp_frame_size frames there will be a gap between
331 the frames. This is because a frame cannot be spawn across two
332 blocks.
334 At the beginning of each frame there is an status field (see
335 struct tpacket_hdr). If this field is 0 means that the frame is ready
336 to be used for the kernel, If not, there is a frame the user can read
337 and the following flags apply:
339 from include/linux/if_packet.h
341 #define TP_STATUS_COPY 2
342 #define TP_STATUS_LOSING 4
343 #define TP_STATUS_CSUMNOTREADY 8
346 TP_STATUS_COPY : This flag indicates that the frame (and associated
347 meta information) has been truncated because it's
348 larger than tp_frame_size. This packet can be
349 read entirely with recvfrom().
351 In order to make this work it must to be
352 enabled previously with setsockopt() and
353 the PACKET_COPY_THRESH option.
355 The number of frames than can be buffered to
356 be read with recvfrom is limited like a normal socket.
357 See the SO_RCVBUF option in the socket (7) man page.
359 TP_STATUS_LOSING : indicates there were packet drops from last time
360 statistics where checked with getsockopt() and
361 the PACKET_STATISTICS option.
363 TP_STATUS_CSUMNOTREADY: currently it's used for outgoing IP packets which
364 it's checksum will be done in hardware. So while
365 reading the packet we should not try to check the
366 checksum.
368 for convenience there are also the following defines:
370 #define TP_STATUS_KERNEL 0
371 #define TP_STATUS_USER 1
373 The kernel initializes all frames to TP_STATUS_KERNEL, when the kernel
374 receives a packet it puts in the buffer and updates the status with
375 at least the TP_STATUS_USER flag. Then the user can read the packet,
376 once the packet is read the user must zero the status field, so the kernel
377 can use again that frame buffer.
379 The user can use poll (any other variant should apply too) to check if new
380 packets are in the ring:
382 struct pollfd pfd;
384 pfd.fd = fd;
385 pfd.revents = 0;
386 pfd.events = POLLIN|POLLRDNORM|POLLERR;
388 if (status == TP_STATUS_KERNEL)
389 retval = poll(&pfd, 1, timeout);
391 It doesn't incur in a race condition to first check the status value and
392 then poll for frames.
394 --------------------------------------------------------------------------------
395 + THANKS
396 --------------------------------------------------------------------------------
398 Jesse Brandeburg, for fixing my grammathical/spelling errors