hsa: cleanup sock server test
[vpp.git] / src / plugins / hs_apps / vcl / sock_test.h
1 /*
2  * Copyright (c) 2017-2019 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 __sock_test_h__
17 #define __sock_test_h__
18
19 #include <netdb.h>
20 #include <errno.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <hs_apps/vcl/vcl_test.h>
24
25 #define SOCK_TEST_AF_UNIX_FILENAME    "/tmp/ldp_server_af_unix_socket"
26 #define SOCK_TEST_MIXED_EPOLL_DATA    "Hello, world! (over an AF_UNIX socket)"
27 #define SOCK_TEST_AF_UNIX_ACCEPT_DATA 0xaf0000af
28 #define SOCK_TEST_AF_UNIX_FD_MASK     0x00af0000
29 #define SOCK_TEST_BANNER_STRING \
30   "============================================\n"
31
32 #define stinf(_fmt, _args...)                                           \
33   printf ("st: " _fmt "\n", ##_args)
34 #define stwrn(_fmt, _args...)                                           \
35   printf ("WARNING: " _fmt "\n", ##_args)
36 #define sterr(_fn, _rv)                                                 \
37 {                                                                       \
38   errno = -_rv;                                                         \
39   printf ("\nERROR: " _fn " failed (errno = %d)!\n", -_rv);             \
40 }
41 #define stfail(_fn, _rv)                                                \
42 {                                                                       \
43   errno = -_rv;                                                         \
44   perror ("ERROR when calling " _fn);                                   \
45   printf ("\nERROR: " _fn " failed (errno = %d)!\n", -_rv);             \
46   exit (1);                                                             \
47 }
48
49 static inline int
50 sock_test_read (int fd, uint8_t * buf, uint32_t nbytes,
51                 vcl_test_stats_t * stats)
52 {
53   int rx_bytes;
54
55   do
56     {
57       if (stats)
58         stats->rx_xacts++;
59       rx_bytes = read (fd, buf, nbytes);
60       if (stats)
61         {
62           if ((rx_bytes == 0) ||
63               ((rx_bytes < 0)
64                && ((errno == EAGAIN) || (errno == EWOULDBLOCK))))
65             stats->rx_eagain++;
66           else if (rx_bytes < nbytes)
67             stats->rx_incomp++;
68         }
69     }
70   while ((rx_bytes == 0) ||
71          ((rx_bytes < 0) && ((errno == EAGAIN) || (errno == EWOULDBLOCK))));
72
73   if (rx_bytes < 0)
74     {
75       stfail ("sock_test_read()", -errno);
76     }
77   else if (stats)
78     stats->rx_bytes += rx_bytes;
79
80   return (rx_bytes);
81 }
82
83 static inline int
84 sock_test_write (int fd, uint8_t * buf, uint32_t nbytes,
85                  vcl_test_stats_t * stats, uint32_t verbose)
86 {
87   int tx_bytes = 0, nbytes_left = nbytes, rv;
88
89   do
90     {
91       if (stats)
92         stats->tx_xacts++;
93       rv = write (fd, buf, nbytes_left);
94       if (rv < 0)
95         {
96           if ((errno == EAGAIN) || (errno == EWOULDBLOCK))
97             {
98               if (stats)
99                 stats->tx_eagain++;
100               continue;
101             }
102           else
103             break;
104         }
105       tx_bytes += rv;
106
107       if (tx_bytes != nbytes)
108         {
109           nbytes_left = nbytes_left - rv;
110           if (stats)
111             stats->tx_incomp++;
112           if (verbose)
113             {
114               stinf ("bytes written (%d) != bytes to write (%d)!\n", tx_bytes,
115                      nbytes);
116             }
117         }
118
119     }
120   while (tx_bytes != nbytes);
121
122   if (tx_bytes < 0)
123     {
124       stfail ("sock_test_write()", -errno);
125     }
126   else if (stats)
127     stats->tx_bytes += tx_bytes;
128
129   return (tx_bytes);
130 }
131
132 #endif /* __sock_test_h__ */
133
134 /*
135  * fd.io coding-style-patch-verification: ON
136  *
137  * Local Variables:
138  * eval: (c-set-style "gnu")
139  * End:
140  */