68750af89a14a956c94a4040c50efbbc5b93ce0b
[vpp.git] / src / vcl / vcl_test.h
1 /*
2  * Copyright (c) 2017 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:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15
16 #ifndef __vcl_test_h__
17 #define __vcl_test_h__
18
19 #include <netdb.h>
20 #include <errno.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <vcl/vppcom.h>
24 #include <vcl/sock_test_common.h>
25
26 #define vtfail(_fn, _rv)                                                \
27 {                                                                       \
28   errno = -_rv;                                                         \
29   perror ("ERROR when calling " _fn);                                   \
30   fprintf (stderr, "\nERROR: " _fn " failed (errno = %d)!\n", -_rv);    \
31   exit (1);                                                             \
32 }
33
34 #define vterr(_fn, _rv)                                                 \
35 {                                                                       \
36   errno = -_rv;                                                         \
37   fprintf (stderr, "\nERROR: " _fn " failed (errno = %d)!\n", -_rv);    \
38 }
39
40 #define vtwrn(_fmt, _args...)                                           \
41   fprintf (stderr, "\nERROR: " _fmt "\n", ##_args)                      \
42
43 #define vtinf(_fmt, _args...)                                           \
44   fprintf (stdout, "vt<w%u>: " _fmt "\n", __wrk_index, ##_args)
45
46 #define vt_atomic_add(_ptr, _val)                                       \
47   __atomic_fetch_add (_ptr, _val, __ATOMIC_RELEASE)
48
49 static inline int
50 vcl_test_read (int fd, uint8_t *buf, uint32_t nbytes,
51                 sock_test_stats_t *stats)
52 {
53   int rx_bytes, errno_val;
54   
55   do
56     {
57       if (stats)
58         stats->rx_xacts++;
59       rx_bytes = vppcom_session_read (fd, buf, nbytes);
60
61       if (rx_bytes < 0)
62         {
63           errno = -rx_bytes;
64           rx_bytes = -1;
65         }
66       if (stats)
67         {
68           if ((rx_bytes == 0) ||
69               ((rx_bytes < 0) && ((errno == EAGAIN) || (errno == EWOULDBLOCK))))
70             stats->rx_eagain++;
71           else if (rx_bytes < nbytes)
72             stats->rx_incomp++;
73         }
74     }
75   while ((rx_bytes == 0) ||
76          ((rx_bytes < 0) && ((errno == EAGAIN) || (errno == EWOULDBLOCK))));
77   
78   if (rx_bytes < 0)
79     {
80       vterr ("vppcom_session_read()", -errno);
81     }
82   else if (stats)
83     stats->rx_bytes += rx_bytes;
84
85   return (rx_bytes);
86 }
87
88 static inline int
89 vcl_test_read_ds (int fd, vppcom_data_segments_t ds, sock_test_stats_t *stats)
90 {
91   int rx_bytes, errno_val;
92
93   do
94     {
95       if (stats)
96         stats->rx_xacts++;
97       rx_bytes = vppcom_session_read_segments (fd, ds);
98
99       if (rx_bytes < 0)
100         {
101           errno = -rx_bytes;
102           rx_bytes = -1;
103         }
104       if (stats)
105         {
106           if ((rx_bytes == 0) ||
107               ((rx_bytes < 0) && ((errno == EAGAIN) || (errno == EWOULDBLOCK))))
108             stats->rx_eagain++;
109         }
110     }
111   while ((rx_bytes == 0) ||
112          ((rx_bytes < 0) && ((errno == EAGAIN) || (errno == EWOULDBLOCK))));
113
114   if (rx_bytes < 0)
115     {
116       vterr ("vppcom_session_read()", -errno);
117     }
118   else if (stats)
119     stats->rx_bytes += rx_bytes;
120
121   return (rx_bytes);
122 }
123
124 static inline int
125 vcl_test_write (int fd, uint8_t *buf, uint32_t nbytes,
126                  sock_test_stats_t *stats, uint32_t verbose)
127 {
128   int tx_bytes = 0, nbytes_left = nbytes, rv;
129
130   do
131     {
132       if (stats)
133         stats->tx_xacts++;
134       rv = vppcom_session_write (fd, buf, nbytes_left);
135       if (rv < 0)
136         {
137           errno = -rv;
138           rv = -1;
139         }
140       if (rv < 0)
141         {
142           if ((errno == EAGAIN) || (errno == EWOULDBLOCK))
143             {
144               if (stats)
145                 stats->tx_eagain++;
146               continue;
147             }
148           else
149             break;
150         }
151       tx_bytes += rv;
152      
153       if (tx_bytes != nbytes)
154         {
155           nbytes_left = nbytes_left - rv;
156           if (stats)
157             stats->tx_incomp++;
158         }
159      
160     } while (tx_bytes != nbytes);
161
162   if (tx_bytes < 0)
163     {
164       vterr ("vpcom_session_write", -errno);
165     }
166   else if (stats)
167     stats->tx_bytes += tx_bytes;
168   
169   return (tx_bytes);
170 }
171
172 #endif /* __vcl_test_h__ */