4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 #include <rte_common.h>
40 #include <rte_random.h>
41 #include <rte_memcpy.h>
46 * Set this to the maximum buffer size you want to test. If it is 0, then the
47 * values in the buf_sizes[] array below will be used.
49 #define TEST_VALUE_RANGE 0
51 /* List of buffer sizes to test */
52 #if TEST_VALUE_RANGE == 0
53 static size_t buf_sizes[] = {
54 0, 1, 7, 8, 9, 15, 16, 17, 31, 32, 33, 63, 64, 65, 127, 128, 129, 255,
55 256, 257, 320, 384, 511, 512, 513, 1023, 1024, 1025, 1518, 1522, 1600,
56 2048, 3072, 4096, 5120, 6144, 7168, 8192
58 /* MUST be as large as largest packet size above */
59 #define SMALL_BUFFER_SIZE 8192
60 #else /* TEST_VALUE_RANGE != 0 */
61 static size_t buf_sizes[TEST_VALUE_RANGE];
62 #define SMALL_BUFFER_SIZE TEST_VALUE_RANGE
63 #endif /* TEST_VALUE_RANGE == 0 */
65 /* Data is aligned on this many bytes (power of 2) */
66 #define ALIGNMENT_UNIT 32
70 * Create two buffers, and initialise one with random values. These are copied
71 * to the second buffer and then compared to see if the copy was successful.
72 * The bytes outside the copied area are also checked to make sure they were not
76 test_single_memcpy(unsigned int off_src, unsigned int off_dst, size_t size)
79 uint8_t dest[SMALL_BUFFER_SIZE + ALIGNMENT_UNIT];
80 uint8_t src[SMALL_BUFFER_SIZE + ALIGNMENT_UNIT];
84 for (i = 0; i < SMALL_BUFFER_SIZE + ALIGNMENT_UNIT; i++) {
86 src[i] = (uint8_t) rte_rand();
90 ret = rte_memcpy(dest + off_dst, src + off_src, size);
91 if (ret != (dest + off_dst)) {
92 printf("rte_memcpy() returned %p, not %p\n",
96 /* Check nothing before offset is affected */
97 for (i = 0; i < off_dst; i++) {
99 printf("rte_memcpy() failed for %u bytes (offsets=%u,%u): "
100 "[modified before start of dst].\n",
101 (unsigned)size, off_src, off_dst);
106 /* Check everything was copied */
107 for (i = 0; i < size; i++) {
108 if (dest[i + off_dst] != src[i + off_src]) {
109 printf("rte_memcpy() failed for %u bytes (offsets=%u,%u): "
110 "[didn't copy byte %u].\n",
111 (unsigned)size, off_src, off_dst, i);
116 /* Check nothing after copy was affected */
117 for (i = size; i < SMALL_BUFFER_SIZE; i++) {
118 if (dest[i + off_dst] != 0) {
119 printf("rte_memcpy() failed for %u bytes (offsets=%u,%u): "
120 "[copied too many].\n",
121 (unsigned)size, off_src, off_dst);
129 * Check functionality for various buffer sizes and data offsets/alignments.
134 unsigned int off_src, off_dst, i;
135 unsigned int num_buf_sizes = sizeof(buf_sizes) / sizeof(buf_sizes[0]);
138 for (off_src = 0; off_src < ALIGNMENT_UNIT; off_src++) {
139 for (off_dst = 0; off_dst < ALIGNMENT_UNIT; off_dst++) {
140 for (i = 0; i < num_buf_sizes; i++) {
141 ret = test_single_memcpy(off_src, off_dst,
162 REGISTER_TEST_COMMAND(memcpy_autotest, test_memcpy);