1a9de470cSBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
2a9de470cSBruce Richardson * Copyright(c) 2010-2014 Intel Corporation
3fa3253c5SPhil Yang * Copyright(c) 2019 Arm Limited
4a9de470cSBruce Richardson */
5a9de470cSBruce Richardson
6a9de470cSBruce Richardson #include <stdio.h>
7a9de470cSBruce Richardson #include <stdint.h>
8a9de470cSBruce Richardson #include <unistd.h>
9e79f49b4SDavid Christensen #include <inttypes.h>
10a9de470cSBruce Richardson #include <sys/queue.h>
11a9de470cSBruce Richardson
12a9de470cSBruce Richardson #include <rte_memory.h>
13a9de470cSBruce Richardson #include <rte_per_lcore.h>
14a9de470cSBruce Richardson #include <rte_launch.h>
15a9de470cSBruce Richardson #include <rte_atomic.h>
16a9de470cSBruce Richardson #include <rte_eal.h>
17a9de470cSBruce Richardson #include <rte_lcore.h>
18e79f49b4SDavid Christensen #include <rte_random.h>
19e79f49b4SDavid Christensen #include <rte_hash_crc.h>
20a9de470cSBruce Richardson
21a9de470cSBruce Richardson #include "test.h"
22a9de470cSBruce Richardson
23a9de470cSBruce Richardson /*
24a9de470cSBruce Richardson * Atomic Variables
25a9de470cSBruce Richardson * ================
26a9de470cSBruce Richardson *
27e79f49b4SDavid Christensen * - The main test function performs several subtests. The first
28a9de470cSBruce Richardson * checks that the usual inc/dec/add/sub functions are working
29a9de470cSBruce Richardson * correctly:
30a9de470cSBruce Richardson *
31a9de470cSBruce Richardson * - Initialize 16-bit, 32-bit and 64-bit atomic variables to specific
32a9de470cSBruce Richardson * values.
33a9de470cSBruce Richardson *
34a9de470cSBruce Richardson * - These variables are incremented and decremented on each core at
35a9de470cSBruce Richardson * the same time in ``test_atomic_usual()``.
36a9de470cSBruce Richardson *
37a9de470cSBruce Richardson * - The function checks that once all lcores finish their function,
38a9de470cSBruce Richardson * the value of the atomic variables are still the same.
39a9de470cSBruce Richardson *
40e79f49b4SDavid Christensen * - Test "test and set" functions.
41a9de470cSBruce Richardson *
42a9de470cSBruce Richardson * - Initialize 16-bit, 32-bit and 64-bit atomic variables to zero.
43a9de470cSBruce Richardson *
44a9de470cSBruce Richardson * - Invoke ``test_atomic_tas()`` on each lcore: before doing anything
45a9de470cSBruce Richardson * else. The cores are waiting a synchro using ``while
46a9de470cSBruce Richardson * (rte_atomic32_read(&val) == 0)`` which is triggered by the main test
47a9de470cSBruce Richardson * function. Then all cores do a
48a9de470cSBruce Richardson * ``rte_atomicXX_test_and_set()`` at the same time. If it is successful,
49a9de470cSBruce Richardson * it increments another atomic counter.
50a9de470cSBruce Richardson *
51a9de470cSBruce Richardson * - The main function checks that the atomic counter was incremented
52a9de470cSBruce Richardson * twice only (one for 16-bit, one for 32-bit and one for 64-bit values).
53a9de470cSBruce Richardson *
54e79f49b4SDavid Christensen * - Test "add/sub and return" functions
55a9de470cSBruce Richardson *
56a9de470cSBruce Richardson * - Initialize 16-bit, 32-bit and 64-bit atomic variables to zero.
57a9de470cSBruce Richardson *
58a9de470cSBruce Richardson * - Invoke ``test_atomic_addsub_return()`` on each lcore. Before doing
59a9de470cSBruce Richardson * anything else, the cores are waiting a synchro. Each lcore does
60a9de470cSBruce Richardson * this operation several times::
61a9de470cSBruce Richardson *
62a9de470cSBruce Richardson * tmp = rte_atomicXX_add_return(&a, 1);
63a9de470cSBruce Richardson * atomic_add(&count, tmp);
64a9de470cSBruce Richardson * tmp = rte_atomicXX_sub_return(&a, 1);
65a9de470cSBruce Richardson * atomic_sub(&count, tmp+1);
66a9de470cSBruce Richardson *
67a9de470cSBruce Richardson * - At the end of the test, the *count* value must be 0.
68fa3253c5SPhil Yang *
69fa3253c5SPhil Yang * - Test "128-bit compare and swap" (aarch64 and x86_64 only)
70fa3253c5SPhil Yang *
71fa3253c5SPhil Yang * - Initialize 128-bit atomic variables to zero.
72fa3253c5SPhil Yang *
73fa3253c5SPhil Yang * - Invoke ``test_atomic128_cmp_exchange()`` on each lcore. Before doing
74fa3253c5SPhil Yang * anything else, the cores are waiting a synchro. Each lcore does
75fa3253c5SPhil Yang * these compare and swap (CAS) operations several times::
76fa3253c5SPhil Yang *
77fa3253c5SPhil Yang * Acquired CAS update counter.val[0] + 2; counter.val[1] + 1;
78fa3253c5SPhil Yang * Released CAS update counter.val[0] + 2; counter.val[1] + 1;
79fa3253c5SPhil Yang * Acquired_Released CAS update counter.val[0] + 2; counter.val[1] + 1;
80fa3253c5SPhil Yang * Relaxed CAS update counter.val[0] + 2; counter.val[1] + 1;
81fa3253c5SPhil Yang *
82fa3253c5SPhil Yang * - At the end of the test, the *count128* first 64-bit value and
83fa3253c5SPhil Yang * second 64-bit value differ by the total iterations.
84e79f49b4SDavid Christensen *
85e79f49b4SDavid Christensen * - Test "atomic exchange" functions
86e79f49b4SDavid Christensen *
87e79f49b4SDavid Christensen * - Create a 64 bit token that can be tested for data integrity
88e79f49b4SDavid Christensen *
89e79f49b4SDavid Christensen * - Invoke ``test_atomic_exchange`` on each lcore. Before doing
90e79f49b4SDavid Christensen * anything else, the cores wait for a synchronization event.
914a6672c2SStephen Hemminger * Each core then does the following for N iterations:
92e79f49b4SDavid Christensen *
93e79f49b4SDavid Christensen * Generate a new token with a data integrity check
94e79f49b4SDavid Christensen * Exchange the new token for previously generated token
95e79f49b4SDavid Christensen * Increment a counter if a corrupt token was received
96e79f49b4SDavid Christensen *
97e79f49b4SDavid Christensen * - At the end of the test, the number of corrupted tokens must be 0.
98a9de470cSBruce Richardson */
99a9de470cSBruce Richardson
100a9de470cSBruce Richardson #define NUM_ATOMIC_TYPES 3
101a9de470cSBruce Richardson
102fa3253c5SPhil Yang #define N 1000000
103a9de470cSBruce Richardson
104a9de470cSBruce Richardson static rte_atomic16_t a16;
105a9de470cSBruce Richardson static rte_atomic32_t a32;
106a9de470cSBruce Richardson static rte_atomic64_t a64;
107a9de470cSBruce Richardson static rte_atomic64_t count;
108a9de470cSBruce Richardson static rte_atomic32_t synchro;
109a9de470cSBruce Richardson
110a9de470cSBruce Richardson static int
test_atomic_usual(__rte_unused void * arg)111f2fc83b4SThomas Monjalon test_atomic_usual(__rte_unused void *arg)
112a9de470cSBruce Richardson {
113a9de470cSBruce Richardson unsigned i;
114a9de470cSBruce Richardson
115a9de470cSBruce Richardson while (rte_atomic32_read(&synchro) == 0)
116a9de470cSBruce Richardson ;
117a9de470cSBruce Richardson
118a9de470cSBruce Richardson for (i = 0; i < N; i++)
119a9de470cSBruce Richardson rte_atomic16_inc(&a16);
120a9de470cSBruce Richardson for (i = 0; i < N; i++)
121a9de470cSBruce Richardson rte_atomic16_dec(&a16);
122a9de470cSBruce Richardson for (i = 0; i < (N / 5); i++)
123a9de470cSBruce Richardson rte_atomic16_add(&a16, 5);
124a9de470cSBruce Richardson for (i = 0; i < (N / 5); i++)
125a9de470cSBruce Richardson rte_atomic16_sub(&a16, 5);
126a9de470cSBruce Richardson
127a9de470cSBruce Richardson for (i = 0; i < N; i++)
128a9de470cSBruce Richardson rte_atomic32_inc(&a32);
129a9de470cSBruce Richardson for (i = 0; i < N; i++)
130a9de470cSBruce Richardson rte_atomic32_dec(&a32);
131a9de470cSBruce Richardson for (i = 0; i < (N / 5); i++)
132a9de470cSBruce Richardson rte_atomic32_add(&a32, 5);
133a9de470cSBruce Richardson for (i = 0; i < (N / 5); i++)
134a9de470cSBruce Richardson rte_atomic32_sub(&a32, 5);
135a9de470cSBruce Richardson
136a9de470cSBruce Richardson for (i = 0; i < N; i++)
137a9de470cSBruce Richardson rte_atomic64_inc(&a64);
138a9de470cSBruce Richardson for (i = 0; i < N; i++)
139a9de470cSBruce Richardson rte_atomic64_dec(&a64);
140a9de470cSBruce Richardson for (i = 0; i < (N / 5); i++)
141a9de470cSBruce Richardson rte_atomic64_add(&a64, 5);
142a9de470cSBruce Richardson for (i = 0; i < (N / 5); i++)
143a9de470cSBruce Richardson rte_atomic64_sub(&a64, 5);
144a9de470cSBruce Richardson
145a9de470cSBruce Richardson return 0;
146a9de470cSBruce Richardson }
147a9de470cSBruce Richardson
148a9de470cSBruce Richardson static int
test_atomic_tas(__rte_unused void * arg)149f2fc83b4SThomas Monjalon test_atomic_tas(__rte_unused void *arg)
150a9de470cSBruce Richardson {
151a9de470cSBruce Richardson while (rte_atomic32_read(&synchro) == 0)
152a9de470cSBruce Richardson ;
153a9de470cSBruce Richardson
154a9de470cSBruce Richardson if (rte_atomic16_test_and_set(&a16))
155a9de470cSBruce Richardson rte_atomic64_inc(&count);
156a9de470cSBruce Richardson if (rte_atomic32_test_and_set(&a32))
157a9de470cSBruce Richardson rte_atomic64_inc(&count);
158a9de470cSBruce Richardson if (rte_atomic64_test_and_set(&a64))
159a9de470cSBruce Richardson rte_atomic64_inc(&count);
160a9de470cSBruce Richardson
161a9de470cSBruce Richardson return 0;
162a9de470cSBruce Richardson }
163a9de470cSBruce Richardson
164a9de470cSBruce Richardson static int
test_atomic_addsub_and_return(__rte_unused void * arg)165f2fc83b4SThomas Monjalon test_atomic_addsub_and_return(__rte_unused void *arg)
166a9de470cSBruce Richardson {
167a9de470cSBruce Richardson uint32_t tmp16;
168a9de470cSBruce Richardson uint32_t tmp32;
169a9de470cSBruce Richardson uint64_t tmp64;
170a9de470cSBruce Richardson unsigned i;
171a9de470cSBruce Richardson
172a9de470cSBruce Richardson while (rte_atomic32_read(&synchro) == 0)
173a9de470cSBruce Richardson ;
174a9de470cSBruce Richardson
175a9de470cSBruce Richardson for (i = 0; i < N; i++) {
176a9de470cSBruce Richardson tmp16 = rte_atomic16_add_return(&a16, 1);
177a9de470cSBruce Richardson rte_atomic64_add(&count, tmp16);
178a9de470cSBruce Richardson
179a9de470cSBruce Richardson tmp16 = rte_atomic16_sub_return(&a16, 1);
180a9de470cSBruce Richardson rte_atomic64_sub(&count, tmp16+1);
181a9de470cSBruce Richardson
182a9de470cSBruce Richardson tmp32 = rte_atomic32_add_return(&a32, 1);
183a9de470cSBruce Richardson rte_atomic64_add(&count, tmp32);
184a9de470cSBruce Richardson
185a9de470cSBruce Richardson tmp32 = rte_atomic32_sub_return(&a32, 1);
186a9de470cSBruce Richardson rte_atomic64_sub(&count, tmp32+1);
187a9de470cSBruce Richardson
188a9de470cSBruce Richardson tmp64 = rte_atomic64_add_return(&a64, 1);
189a9de470cSBruce Richardson rte_atomic64_add(&count, tmp64);
190a9de470cSBruce Richardson
191a9de470cSBruce Richardson tmp64 = rte_atomic64_sub_return(&a64, 1);
192a9de470cSBruce Richardson rte_atomic64_sub(&count, tmp64+1);
193a9de470cSBruce Richardson }
194a9de470cSBruce Richardson
195a9de470cSBruce Richardson return 0;
196a9de470cSBruce Richardson }
197a9de470cSBruce Richardson
198a9de470cSBruce Richardson /*
199a9de470cSBruce Richardson * rte_atomic32_inc_and_test() would increase a 32 bits counter by one and then
200a9de470cSBruce Richardson * test if that counter is equal to 0. It would return true if the counter is 0
201a9de470cSBruce Richardson * and false if the counter is not 0. rte_atomic64_inc_and_test() could do the
202a9de470cSBruce Richardson * same thing but for a 64 bits counter.
203a9de470cSBruce Richardson * Here checks that if the 32/64 bits counter is equal to 0 after being atomically
204a9de470cSBruce Richardson * increased by one. If it is, increase the variable of "count" by one which would
205a9de470cSBruce Richardson * be checked as the result later.
206a9de470cSBruce Richardson *
207a9de470cSBruce Richardson */
208a9de470cSBruce Richardson static int
test_atomic_inc_and_test(__rte_unused void * arg)209f2fc83b4SThomas Monjalon test_atomic_inc_and_test(__rte_unused void *arg)
210a9de470cSBruce Richardson {
211a9de470cSBruce Richardson while (rte_atomic32_read(&synchro) == 0)
212a9de470cSBruce Richardson ;
213a9de470cSBruce Richardson
214a9de470cSBruce Richardson if (rte_atomic16_inc_and_test(&a16)) {
215a9de470cSBruce Richardson rte_atomic64_inc(&count);
216a9de470cSBruce Richardson }
217a9de470cSBruce Richardson if (rte_atomic32_inc_and_test(&a32)) {
218a9de470cSBruce Richardson rte_atomic64_inc(&count);
219a9de470cSBruce Richardson }
220a9de470cSBruce Richardson if (rte_atomic64_inc_and_test(&a64)) {
221a9de470cSBruce Richardson rte_atomic64_inc(&count);
222a9de470cSBruce Richardson }
223a9de470cSBruce Richardson
224a9de470cSBruce Richardson return 0;
225a9de470cSBruce Richardson }
226a9de470cSBruce Richardson
227a9de470cSBruce Richardson /*
228a9de470cSBruce Richardson * rte_atomicXX_dec_and_test() should decrease a 32 bits counter by one and then
229a9de470cSBruce Richardson * test if that counter is equal to 0. It should return true if the counter is 0
230a9de470cSBruce Richardson * and false if the counter is not 0.
231a9de470cSBruce Richardson * This test checks if the counter is equal to 0 after being atomically
232a9de470cSBruce Richardson * decreased by one. If it is, increase the value of "count" by one which is to
233a9de470cSBruce Richardson * be checked as the result later.
234a9de470cSBruce Richardson */
235a9de470cSBruce Richardson static int
test_atomic_dec_and_test(__rte_unused void * arg)236f2fc83b4SThomas Monjalon test_atomic_dec_and_test(__rte_unused void *arg)
237a9de470cSBruce Richardson {
238a9de470cSBruce Richardson while (rte_atomic32_read(&synchro) == 0)
239a9de470cSBruce Richardson ;
240a9de470cSBruce Richardson
241a9de470cSBruce Richardson if (rte_atomic16_dec_and_test(&a16))
242a9de470cSBruce Richardson rte_atomic64_inc(&count);
243a9de470cSBruce Richardson
244a9de470cSBruce Richardson if (rte_atomic32_dec_and_test(&a32))
245a9de470cSBruce Richardson rte_atomic64_inc(&count);
246a9de470cSBruce Richardson
247a9de470cSBruce Richardson if (rte_atomic64_dec_and_test(&a64))
248a9de470cSBruce Richardson rte_atomic64_inc(&count);
249a9de470cSBruce Richardson
250a9de470cSBruce Richardson return 0;
251a9de470cSBruce Richardson }
252a9de470cSBruce Richardson
253fa3253c5SPhil Yang #if defined(RTE_ARCH_X86_64) || defined(RTE_ARCH_ARM64)
254fa3253c5SPhil Yang static rte_int128_t count128;
255fa3253c5SPhil Yang
256fa3253c5SPhil Yang /*
257fa3253c5SPhil Yang * rte_atomic128_cmp_exchange() should update a 128 bits counter's first 64
258fa3253c5SPhil Yang * bits by 2 and the second 64 bits by 1 in this test. It should return true
259fa3253c5SPhil Yang * if the compare exchange operation is successful.
260fa3253c5SPhil Yang * This test repeats 128 bits compare and swap operations N rounds. In each
261fa3253c5SPhil Yang * iteration it runs compare and swap operation with different memory models.
262fa3253c5SPhil Yang */
263fa3253c5SPhil Yang static int
test_atomic128_cmp_exchange(__rte_unused void * arg)264f2fc83b4SThomas Monjalon test_atomic128_cmp_exchange(__rte_unused void *arg)
265fa3253c5SPhil Yang {
266fa3253c5SPhil Yang rte_int128_t expected;
267fa3253c5SPhil Yang int success;
268fa3253c5SPhil Yang unsigned int i;
269fa3253c5SPhil Yang
270fa3253c5SPhil Yang while (rte_atomic32_read(&synchro) == 0)
271fa3253c5SPhil Yang ;
272fa3253c5SPhil Yang
273fa3253c5SPhil Yang expected = count128;
274fa3253c5SPhil Yang
275fa3253c5SPhil Yang for (i = 0; i < N; i++) {
276fa3253c5SPhil Yang do {
277fa3253c5SPhil Yang rte_int128_t desired;
278fa3253c5SPhil Yang
279fa3253c5SPhil Yang desired.val[0] = expected.val[0] + 2;
280fa3253c5SPhil Yang desired.val[1] = expected.val[1] + 1;
281fa3253c5SPhil Yang
282fa3253c5SPhil Yang success = rte_atomic128_cmp_exchange(&count128,
283fa3253c5SPhil Yang &expected, &desired, 1,
284fa3253c5SPhil Yang __ATOMIC_ACQUIRE, __ATOMIC_RELAXED);
285fa3253c5SPhil Yang } while (success == 0);
286fa3253c5SPhil Yang
287fa3253c5SPhil Yang do {
288fa3253c5SPhil Yang rte_int128_t desired;
289fa3253c5SPhil Yang
290fa3253c5SPhil Yang desired.val[0] = expected.val[0] + 2;
291fa3253c5SPhil Yang desired.val[1] = expected.val[1] + 1;
292fa3253c5SPhil Yang
293fa3253c5SPhil Yang success = rte_atomic128_cmp_exchange(&count128,
294fa3253c5SPhil Yang &expected, &desired, 1,
295fa3253c5SPhil Yang __ATOMIC_RELEASE, __ATOMIC_RELAXED);
296fa3253c5SPhil Yang } while (success == 0);
297fa3253c5SPhil Yang
298fa3253c5SPhil Yang do {
299fa3253c5SPhil Yang rte_int128_t desired;
300fa3253c5SPhil Yang
301fa3253c5SPhil Yang desired.val[0] = expected.val[0] + 2;
302fa3253c5SPhil Yang desired.val[1] = expected.val[1] + 1;
303fa3253c5SPhil Yang
304fa3253c5SPhil Yang success = rte_atomic128_cmp_exchange(&count128,
305fa3253c5SPhil Yang &expected, &desired, 1,
306fa3253c5SPhil Yang __ATOMIC_ACQ_REL, __ATOMIC_RELAXED);
307fa3253c5SPhil Yang } while (success == 0);
308fa3253c5SPhil Yang
309fa3253c5SPhil Yang do {
310fa3253c5SPhil Yang rte_int128_t desired;
311fa3253c5SPhil Yang
312fa3253c5SPhil Yang desired.val[0] = expected.val[0] + 2;
313fa3253c5SPhil Yang desired.val[1] = expected.val[1] + 1;
314fa3253c5SPhil Yang
315fa3253c5SPhil Yang success = rte_atomic128_cmp_exchange(&count128,
316fa3253c5SPhil Yang &expected, &desired, 1,
317fa3253c5SPhil Yang __ATOMIC_RELAXED, __ATOMIC_RELAXED);
318fa3253c5SPhil Yang } while (success == 0);
319fa3253c5SPhil Yang }
320fa3253c5SPhil Yang
321fa3253c5SPhil Yang return 0;
322fa3253c5SPhil Yang }
323fa3253c5SPhil Yang #endif
324fa3253c5SPhil Yang
325e79f49b4SDavid Christensen /*
326e79f49b4SDavid Christensen * Helper definitions/variables/functions for
327e79f49b4SDavid Christensen * atomic exchange tests
328e79f49b4SDavid Christensen */
329e79f49b4SDavid Christensen typedef union {
330e79f49b4SDavid Christensen uint16_t u16;
331e79f49b4SDavid Christensen uint8_t u8[2];
332e79f49b4SDavid Christensen } test16_t;
333e79f49b4SDavid Christensen
334e79f49b4SDavid Christensen typedef union {
335e79f49b4SDavid Christensen uint32_t u32;
336e79f49b4SDavid Christensen uint16_t u16[2];
337e79f49b4SDavid Christensen uint8_t u8[4];
338e79f49b4SDavid Christensen } test32_t;
339e79f49b4SDavid Christensen
340e79f49b4SDavid Christensen typedef union {
341e79f49b4SDavid Christensen uint64_t u64;
342e79f49b4SDavid Christensen uint32_t u32[2];
343e79f49b4SDavid Christensen uint16_t u16[4];
344e79f49b4SDavid Christensen uint8_t u8[8];
345e79f49b4SDavid Christensen } test64_t;
346e79f49b4SDavid Christensen
347e79f49b4SDavid Christensen const uint8_t CRC8_POLY = 0x91;
348e79f49b4SDavid Christensen uint8_t crc8_table[256];
349e79f49b4SDavid Christensen
350e79f49b4SDavid Christensen volatile uint16_t token16;
351e79f49b4SDavid Christensen volatile uint32_t token32;
352e79f49b4SDavid Christensen volatile uint64_t token64;
353e79f49b4SDavid Christensen
354e79f49b4SDavid Christensen static void
build_crc8_table(void)355e79f49b4SDavid Christensen build_crc8_table(void)
356e79f49b4SDavid Christensen {
357e79f49b4SDavid Christensen uint8_t val;
358e79f49b4SDavid Christensen int i, j;
359e79f49b4SDavid Christensen
360e79f49b4SDavid Christensen for (i = 0; i < 256; i++) {
361e79f49b4SDavid Christensen val = i;
362e79f49b4SDavid Christensen for (j = 0; j < 8; j++) {
363e79f49b4SDavid Christensen if (val & 1)
364e79f49b4SDavid Christensen val ^= CRC8_POLY;
365e79f49b4SDavid Christensen val >>= 1;
366e79f49b4SDavid Christensen }
367e79f49b4SDavid Christensen crc8_table[i] = val;
368e79f49b4SDavid Christensen }
369e79f49b4SDavid Christensen }
370e79f49b4SDavid Christensen
371e79f49b4SDavid Christensen static uint8_t
get_crc8(uint8_t * message,int length)372e79f49b4SDavid Christensen get_crc8(uint8_t *message, int length)
373e79f49b4SDavid Christensen {
374e79f49b4SDavid Christensen uint8_t crc = 0;
375e79f49b4SDavid Christensen int i;
376e79f49b4SDavid Christensen
377e79f49b4SDavid Christensen for (i = 0; i < length; i++)
378e79f49b4SDavid Christensen crc = crc8_table[crc ^ message[i]];
379e79f49b4SDavid Christensen return crc;
380e79f49b4SDavid Christensen }
381e79f49b4SDavid Christensen
382e79f49b4SDavid Christensen /*
383e79f49b4SDavid Christensen * The atomic exchange test sets up a token in memory and
384e79f49b4SDavid Christensen * then spins up multiple lcores whose job is to generate
385e79f49b4SDavid Christensen * new tokens, exchange that new token for the old one held
386e79f49b4SDavid Christensen * in memory, and then verify that the old token is still
387e79f49b4SDavid Christensen * valid (i.e. the exchange did not corrupt the token).
388e79f49b4SDavid Christensen *
389e79f49b4SDavid Christensen * A token is made up of random data and 8 bits of crc
390e79f49b4SDavid Christensen * covering that random data. The following is an example
391e79f49b4SDavid Christensen * of a 64bit token.
392e79f49b4SDavid Christensen *
393e79f49b4SDavid Christensen * +------------+------------+
394e79f49b4SDavid Christensen * | 63 56 | 55 0 |
395e79f49b4SDavid Christensen * +------------+------------+
396e79f49b4SDavid Christensen * | CRC8 | Data |
397e79f49b4SDavid Christensen * +------------+------------+
398e79f49b4SDavid Christensen */
399e79f49b4SDavid Christensen static int
test_atomic_exchange(__rte_unused void * arg)400f2fc83b4SThomas Monjalon test_atomic_exchange(__rte_unused void *arg)
401e79f49b4SDavid Christensen {
402e79f49b4SDavid Christensen int i;
403e79f49b4SDavid Christensen test16_t nt16, ot16; /* new token, old token */
404e79f49b4SDavid Christensen test32_t nt32, ot32;
405e79f49b4SDavid Christensen test64_t nt64, ot64;
406e79f49b4SDavid Christensen
407e79f49b4SDavid Christensen /* Wait until all of the other threads have been dispatched */
408e79f49b4SDavid Christensen while (rte_atomic32_read(&synchro) == 0)
409e79f49b4SDavid Christensen ;
410e79f49b4SDavid Christensen
411e79f49b4SDavid Christensen /*
412e79f49b4SDavid Christensen * Let the battle begin! Every thread attempts to steal the current
413e79f49b4SDavid Christensen * token with an atomic exchange operation and install its own newly
414e79f49b4SDavid Christensen * generated token. If the old token is valid (i.e. it has the
415e79f49b4SDavid Christensen * appropriate crc32 hash for the data) then the test iteration has
416e79f49b4SDavid Christensen * passed. If the token is invalid, increment the counter.
417e79f49b4SDavid Christensen */
418e79f49b4SDavid Christensen for (i = 0; i < N; i++) {
419e79f49b4SDavid Christensen
420e79f49b4SDavid Christensen /* Test 64bit Atomic Exchange */
421e79f49b4SDavid Christensen nt64.u64 = rte_rand();
422e79f49b4SDavid Christensen nt64.u8[7] = get_crc8(&nt64.u8[0], sizeof(nt64) - 1);
423e79f49b4SDavid Christensen ot64.u64 = rte_atomic64_exchange(&token64, nt64.u64);
424e79f49b4SDavid Christensen if (ot64.u8[7] != get_crc8(&ot64.u8[0], sizeof(ot64) - 1))
425e79f49b4SDavid Christensen rte_atomic64_inc(&count);
426e79f49b4SDavid Christensen
427e79f49b4SDavid Christensen /* Test 32bit Atomic Exchange */
428e79f49b4SDavid Christensen nt32.u32 = (uint32_t)rte_rand();
429e79f49b4SDavid Christensen nt32.u8[3] = get_crc8(&nt32.u8[0], sizeof(nt32) - 1);
430e79f49b4SDavid Christensen ot32.u32 = rte_atomic32_exchange(&token32, nt32.u32);
431e79f49b4SDavid Christensen if (ot32.u8[3] != get_crc8(&ot32.u8[0], sizeof(ot32) - 1))
432e79f49b4SDavid Christensen rte_atomic64_inc(&count);
433e79f49b4SDavid Christensen
434e79f49b4SDavid Christensen /* Test 16bit Atomic Exchange */
435e79f49b4SDavid Christensen nt16.u16 = (uint16_t)rte_rand();
436e79f49b4SDavid Christensen nt16.u8[1] = get_crc8(&nt16.u8[0], sizeof(nt16) - 1);
437e79f49b4SDavid Christensen ot16.u16 = rte_atomic16_exchange(&token16, nt16.u16);
438e79f49b4SDavid Christensen if (ot16.u8[1] != get_crc8(&ot16.u8[0], sizeof(ot16) - 1))
439e79f49b4SDavid Christensen rte_atomic64_inc(&count);
440e79f49b4SDavid Christensen }
441e79f49b4SDavid Christensen
442e79f49b4SDavid Christensen return 0;
443e79f49b4SDavid Christensen }
444a9de470cSBruce Richardson static int
test_atomic(void)445a9de470cSBruce Richardson test_atomic(void)
446a9de470cSBruce Richardson {
447a9de470cSBruce Richardson rte_atomic16_init(&a16);
448a9de470cSBruce Richardson rte_atomic32_init(&a32);
449a9de470cSBruce Richardson rte_atomic64_init(&a64);
450a9de470cSBruce Richardson rte_atomic64_init(&count);
451a9de470cSBruce Richardson rte_atomic32_init(&synchro);
452a9de470cSBruce Richardson
453a9de470cSBruce Richardson rte_atomic16_set(&a16, 1UL << 10);
454a9de470cSBruce Richardson rte_atomic32_set(&a32, 1UL << 10);
455a9de470cSBruce Richardson rte_atomic64_set(&a64, 1ULL << 33);
456a9de470cSBruce Richardson
457a9de470cSBruce Richardson printf("usual inc/dec/add/sub functions\n");
458a9de470cSBruce Richardson
459cb056611SStephen Hemminger rte_eal_mp_remote_launch(test_atomic_usual, NULL, SKIP_MAIN);
460a9de470cSBruce Richardson rte_atomic32_set(&synchro, 1);
461a9de470cSBruce Richardson rte_eal_mp_wait_lcore();
462a9de470cSBruce Richardson rte_atomic32_set(&synchro, 0);
463a9de470cSBruce Richardson
464a9de470cSBruce Richardson if (rte_atomic16_read(&a16) != 1UL << 10) {
465a9de470cSBruce Richardson printf("Atomic16 usual functions failed\n");
466a9de470cSBruce Richardson return -1;
467a9de470cSBruce Richardson }
468a9de470cSBruce Richardson
469a9de470cSBruce Richardson if (rte_atomic32_read(&a32) != 1UL << 10) {
470a9de470cSBruce Richardson printf("Atomic32 usual functions failed\n");
471a9de470cSBruce Richardson return -1;
472a9de470cSBruce Richardson }
473a9de470cSBruce Richardson
474a9de470cSBruce Richardson if (rte_atomic64_read(&a64) != 1ULL << 33) {
475a9de470cSBruce Richardson printf("Atomic64 usual functions failed\n");
476a9de470cSBruce Richardson return -1;
477a9de470cSBruce Richardson }
478a9de470cSBruce Richardson
479a9de470cSBruce Richardson printf("test and set\n");
480a9de470cSBruce Richardson
481a9de470cSBruce Richardson rte_atomic64_set(&a64, 0);
482a9de470cSBruce Richardson rte_atomic32_set(&a32, 0);
483a9de470cSBruce Richardson rte_atomic16_set(&a16, 0);
484a9de470cSBruce Richardson rte_atomic64_set(&count, 0);
485cb056611SStephen Hemminger rte_eal_mp_remote_launch(test_atomic_tas, NULL, SKIP_MAIN);
486a9de470cSBruce Richardson rte_atomic32_set(&synchro, 1);
487a9de470cSBruce Richardson rte_eal_mp_wait_lcore();
488a9de470cSBruce Richardson rte_atomic32_set(&synchro, 0);
489a9de470cSBruce Richardson
490a9de470cSBruce Richardson if (rte_atomic64_read(&count) != NUM_ATOMIC_TYPES) {
491a9de470cSBruce Richardson printf("Atomic test and set failed\n");
492a9de470cSBruce Richardson return -1;
493a9de470cSBruce Richardson }
494a9de470cSBruce Richardson
495a9de470cSBruce Richardson printf("add/sub and return\n");
496a9de470cSBruce Richardson
497a9de470cSBruce Richardson rte_atomic64_set(&a64, 0);
498a9de470cSBruce Richardson rte_atomic32_set(&a32, 0);
499a9de470cSBruce Richardson rte_atomic16_set(&a16, 0);
500a9de470cSBruce Richardson rte_atomic64_set(&count, 0);
501a9de470cSBruce Richardson rte_eal_mp_remote_launch(test_atomic_addsub_and_return, NULL,
502cb056611SStephen Hemminger SKIP_MAIN);
503a9de470cSBruce Richardson rte_atomic32_set(&synchro, 1);
504a9de470cSBruce Richardson rte_eal_mp_wait_lcore();
505a9de470cSBruce Richardson rte_atomic32_set(&synchro, 0);
506a9de470cSBruce Richardson
507a9de470cSBruce Richardson if (rte_atomic64_read(&count) != 0) {
508a9de470cSBruce Richardson printf("Atomic add/sub+return failed\n");
509a9de470cSBruce Richardson return -1;
510a9de470cSBruce Richardson }
511a9de470cSBruce Richardson
512a9de470cSBruce Richardson /*
513cb056611SStephen Hemminger * Set a64, a32 and a16 with the same value of minus "number of worker
514cb056611SStephen Hemminger * lcores", launch all worker lcores to atomically increase by one and
515a9de470cSBruce Richardson * test them respectively.
516a9de470cSBruce Richardson * Each lcore should have only one chance to increase a64 by one and
517a9de470cSBruce Richardson * then check if it is equal to 0, but there should be only one lcore
518a9de470cSBruce Richardson * that finds that it is 0. It is similar for a32 and a16.
519a9de470cSBruce Richardson * Then a variable of "count", initialized to zero, is increased by
520a9de470cSBruce Richardson * one if a64, a32 or a16 is 0 after being increased and tested
521a9de470cSBruce Richardson * atomically.
522cb056611SStephen Hemminger * We can check if "count" is finally equal to 3 to see if all worker
523a9de470cSBruce Richardson * lcores performed "atomic inc and test" right.
524a9de470cSBruce Richardson */
525a9de470cSBruce Richardson printf("inc and test\n");
526a9de470cSBruce Richardson
527a9de470cSBruce Richardson rte_atomic64_clear(&a64);
528a9de470cSBruce Richardson rte_atomic32_clear(&a32);
529a9de470cSBruce Richardson rte_atomic16_clear(&a16);
530a9de470cSBruce Richardson rte_atomic32_clear(&synchro);
531a9de470cSBruce Richardson rte_atomic64_clear(&count);
532a9de470cSBruce Richardson
533a9de470cSBruce Richardson rte_atomic64_set(&a64, (int64_t)(1 - (int64_t)rte_lcore_count()));
534a9de470cSBruce Richardson rte_atomic32_set(&a32, (int32_t)(1 - (int32_t)rte_lcore_count()));
535a9de470cSBruce Richardson rte_atomic16_set(&a16, (int16_t)(1 - (int16_t)rte_lcore_count()));
536cb056611SStephen Hemminger rte_eal_mp_remote_launch(test_atomic_inc_and_test, NULL, SKIP_MAIN);
537a9de470cSBruce Richardson rte_atomic32_set(&synchro, 1);
538a9de470cSBruce Richardson rte_eal_mp_wait_lcore();
539a9de470cSBruce Richardson rte_atomic32_clear(&synchro);
540a9de470cSBruce Richardson
541a9de470cSBruce Richardson if (rte_atomic64_read(&count) != NUM_ATOMIC_TYPES) {
542a9de470cSBruce Richardson printf("Atomic inc and test failed %d\n", (int)count.cnt);
543a9de470cSBruce Richardson return -1;
544a9de470cSBruce Richardson }
545a9de470cSBruce Richardson
546a9de470cSBruce Richardson /*
547cb056611SStephen Hemminger * Same as above, but this time we set the values to "number of worker
548a9de470cSBruce Richardson * lcores", and decrement instead of increment.
549a9de470cSBruce Richardson */
550a9de470cSBruce Richardson printf("dec and test\n");
551a9de470cSBruce Richardson
552a9de470cSBruce Richardson rte_atomic32_clear(&synchro);
553a9de470cSBruce Richardson rte_atomic64_clear(&count);
554a9de470cSBruce Richardson
555a9de470cSBruce Richardson rte_atomic64_set(&a64, (int64_t)(rte_lcore_count() - 1));
556a9de470cSBruce Richardson rte_atomic32_set(&a32, (int32_t)(rte_lcore_count() - 1));
557a9de470cSBruce Richardson rte_atomic16_set(&a16, (int16_t)(rte_lcore_count() - 1));
558cb056611SStephen Hemminger rte_eal_mp_remote_launch(test_atomic_dec_and_test, NULL, SKIP_MAIN);
559a9de470cSBruce Richardson rte_atomic32_set(&synchro, 1);
560a9de470cSBruce Richardson rte_eal_mp_wait_lcore();
561a9de470cSBruce Richardson rte_atomic32_clear(&synchro);
562a9de470cSBruce Richardson
563a9de470cSBruce Richardson if (rte_atomic64_read(&count) != NUM_ATOMIC_TYPES) {
564a9de470cSBruce Richardson printf("Atomic dec and test failed\n");
565a9de470cSBruce Richardson return -1;
566a9de470cSBruce Richardson }
567a9de470cSBruce Richardson
568fa3253c5SPhil Yang #if defined(RTE_ARCH_X86_64) || defined(RTE_ARCH_ARM64)
569fa3253c5SPhil Yang /*
570fa3253c5SPhil Yang * This case tests the functionality of rte_atomic128_cmp_exchange
571fa3253c5SPhil Yang * API. It calls rte_atomic128_cmp_exchange with four kinds of memory
572cb056611SStephen Hemminger * models successively on each worker core. Once each 128-bit atomic
573fa3253c5SPhil Yang * compare and swap operation is successful, it updates the global
574fa3253c5SPhil Yang * 128-bit counter by 2 for the first 64-bit and 1 for the second
575cb056611SStephen Hemminger * 64-bit. Each worker core iterates this test N times.
576fa3253c5SPhil Yang * At the end of test, verify whether the first 64-bits of the 128-bit
577fa3253c5SPhil Yang * counter and the second 64bits is differ by the total iterations. If
578fa3253c5SPhil Yang * it is, the test passes.
579fa3253c5SPhil Yang */
580fa3253c5SPhil Yang printf("128-bit compare and swap test\n");
581fa3253c5SPhil Yang uint64_t iterations = 0;
582fa3253c5SPhil Yang
583fa3253c5SPhil Yang rte_atomic32_clear(&synchro);
584fa3253c5SPhil Yang count128.val[0] = 0;
585fa3253c5SPhil Yang count128.val[1] = 0;
586fa3253c5SPhil Yang
587fa3253c5SPhil Yang rte_eal_mp_remote_launch(test_atomic128_cmp_exchange, NULL,
588cb056611SStephen Hemminger SKIP_MAIN);
589fa3253c5SPhil Yang rte_atomic32_set(&synchro, 1);
590fa3253c5SPhil Yang rte_eal_mp_wait_lcore();
591fa3253c5SPhil Yang rte_atomic32_clear(&synchro);
592fa3253c5SPhil Yang
593fa3253c5SPhil Yang iterations = count128.val[0] - count128.val[1];
59417a04237SDavid Christensen if (iterations != (uint64_t)4*N*(rte_lcore_count()-1)) {
595fa3253c5SPhil Yang printf("128-bit compare and swap failed\n");
596fa3253c5SPhil Yang return -1;
597fa3253c5SPhil Yang }
598fa3253c5SPhil Yang #endif
599fa3253c5SPhil Yang
600e79f49b4SDavid Christensen /*
601e79f49b4SDavid Christensen * Test 16/32/64bit atomic exchange.
602e79f49b4SDavid Christensen */
603e79f49b4SDavid Christensen test64_t t;
604e79f49b4SDavid Christensen
605e79f49b4SDavid Christensen printf("exchange test\n");
606e79f49b4SDavid Christensen
607e79f49b4SDavid Christensen rte_atomic32_clear(&synchro);
608e79f49b4SDavid Christensen rte_atomic64_clear(&count);
609e79f49b4SDavid Christensen
610e79f49b4SDavid Christensen /* Generate the CRC8 lookup table */
611e79f49b4SDavid Christensen build_crc8_table();
612e79f49b4SDavid Christensen
613e79f49b4SDavid Christensen /* Create the initial tokens used by the test */
614e79f49b4SDavid Christensen t.u64 = rte_rand();
615e79f49b4SDavid Christensen token16 = (get_crc8(&t.u8[0], sizeof(token16) - 1) << 8)
616e79f49b4SDavid Christensen | (t.u16[0] & 0x00ff);
617e79f49b4SDavid Christensen token32 = ((uint32_t)get_crc8(&t.u8[0], sizeof(token32) - 1) << 24)
618e79f49b4SDavid Christensen | (t.u32[0] & 0x00ffffff);
619e79f49b4SDavid Christensen token64 = ((uint64_t)get_crc8(&t.u8[0], sizeof(token64) - 1) << 56)
620e79f49b4SDavid Christensen | (t.u64 & 0x00ffffffffffffff);
621e79f49b4SDavid Christensen
622cb056611SStephen Hemminger rte_eal_mp_remote_launch(test_atomic_exchange, NULL, SKIP_MAIN);
623e79f49b4SDavid Christensen rte_atomic32_set(&synchro, 1);
624e79f49b4SDavid Christensen rte_eal_mp_wait_lcore();
625e79f49b4SDavid Christensen rte_atomic32_clear(&synchro);
626e79f49b4SDavid Christensen
627e79f49b4SDavid Christensen if (rte_atomic64_read(&count) > 0) {
628e79f49b4SDavid Christensen printf("Atomic exchange test failed\n");
629e79f49b4SDavid Christensen return -1;
630a9de470cSBruce Richardson }
631a9de470cSBruce Richardson
632e79f49b4SDavid Christensen return 0;
633e79f49b4SDavid Christensen }
634*e0a8442cSBruce Richardson REGISTER_FAST_TEST(atomic_autotest, false, true, test_atomic);
635