2 * Copyright (c) 2018 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
17 #include <svm/message_queue.h>
19 #define test1_error(_fmt, _args...) \
21 ssvm_pop_heap (oldheap); \
22 error = clib_error_return (0, _fmt, ##_args); \
29 ssvm_private_t _ssvm, *ssvm = &_ssvm;
30 svm_msg_q_cfg_t _cfg, *cfg = &_cfg;
31 svm_msg_q_msg_t msg1, msg2, msg[12];
32 ssvm_shared_header_t *sh;
33 clib_error_t *error = 0;
38 memset (ssvm, 0, sizeof (*ssvm));
40 ssvm->ssvm_size = 1 << 20;
41 ssvm->i_am_master = 1;
42 ssvm->my_pid = getpid ();
43 ssvm->name = format (0, "%s%c", "test", 0);
44 ssvm->requested_va = 0;
46 if (ssvm_master_init (ssvm, SSVM_SEGMENT_SHM))
47 return clib_error_return (0, "failed: segment allocation");
50 svm_msg_q_ring_cfg_t rc[2]= {{8, 8, 0}, {8, 16, 0}};
51 cfg->consumer_pid = ~0;
56 oldheap = ssvm_push_heap (sh);
57 mq = svm_msg_q_alloc (cfg);
59 test1_error ("failed: alloc");
61 if (vec_len (mq->rings) != 2)
62 test1_error ("failed: ring allocation");
64 msg1 = svm_msg_q_alloc_msg (mq, 8);
65 if (mq->rings[0].cursize != 1
66 || msg1.ring_index != 0
67 || msg1.elt_index != 0)
68 test1_error ("failed: msg alloc1");
70 msg2 = svm_msg_q_alloc_msg (mq, 15);
71 if (mq->rings[1].cursize != 1
72 || msg2.ring_index != 1
73 || msg2.elt_index != 0)
74 test1_error ("failed: msg alloc2");
76 svm_msg_q_free_msg (mq, &msg1);
77 if (mq->rings[0].cursize != 0)
78 test1_error("failed: free msg");
80 for (i = 0; i < 12; i++)
82 msg[i] = svm_msg_q_alloc_msg (mq, 7);
83 *(u32 *)svm_msg_q_msg_data (mq, &msg[i]) = i;
86 if (mq->rings[0].cursize != 8
87 || mq->rings[1].cursize != 5)
88 test1_error ("failed: msg alloc3");
90 *(u32 *)svm_msg_q_msg_data (mq, &msg2) = 123;
91 svm_msg_q_add (mq, &msg2, SVM_Q_NOWAIT);
92 for (i = 0; i < 12; i++)
93 svm_msg_q_add (mq, &msg[i], SVM_Q_NOWAIT);
95 if (svm_msg_q_sub (mq, &msg2, SVM_Q_NOWAIT, 0))
96 test1_error ("failed: dequeue1");
98 if (msg2.ring_index != 1 || msg2.elt_index != 0)
99 test1_error ("failed: dequeue1 result");
100 if (*(u32 *)svm_msg_q_msg_data (mq, &msg2) != 123)
101 test1_error ("failed: dequeue1 wrong data");
103 svm_msg_q_free_msg (mq, &msg2);
105 for (i = 0; i < 12; i++)
107 if (svm_msg_q_sub (mq, &msg[i], SVM_Q_NOWAIT, 0))
108 test1_error ("failed: dequeue2");
111 if (msg[i].ring_index != 0 || msg[i].elt_index != (i + 1) % 8)
112 test1_error ("failed: dequeue2 result2");
116 if (msg[i].ring_index != 1 || msg[i].elt_index != (i - 8) + 1)
117 test1_error ("failed: dequeue2 result3");
119 if (*(u32 *)svm_msg_q_msg_data (mq, &msg[i]) != i)
120 test1_error ("failed: dequeue2 wrong data");
121 svm_msg_q_free_msg (mq, &msg[i]);
123 if (mq->rings[0].cursize != 0 || mq->rings[1].cursize != 0)
124 test1_error ("failed: post dequeue");
126 ssvm_pop_heap (oldheap);
134 test_svm_message_queue (unformat_input_t * input)
136 clib_error_t *error = 0;
140 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
142 if (unformat (input, "test1"))
146 error = clib_error_create ("unknown input `%U'\n",
147 format_unformat_error, input);
155 error = test1 (verbose);
159 clib_error_report (error);
161 clib_warning ("success");
167 main (int argc, char *argv[])
172 clib_mem_init_thread_safe (0, 256 << 20);
173 unformat_init_command_line (&i, argv);
174 r = test_svm_message_queue (&i);