6e0b22ae81f78e4ee9de9ef9909aad64437dfdd9
[vpp.git] / src / vcl / sock_test.h
1 /*
2  * Copyright (c) 2017-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:
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 <vcl/sock_test_common.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
30 static inline int
31 sock_test_read (int fd, uint8_t *buf, uint32_t nbytes,
32                 sock_test_stats_t *stats)
33 {
34   int rx_bytes, errno_val;
35   
36   do
37     {
38       if (stats)
39         stats->rx_xacts++;
40       rx_bytes = read (fd, buf, nbytes);
41       if (stats)
42         {
43           if ((rx_bytes == 0) ||
44               ((rx_bytes < 0) && ((errno == EAGAIN) || (errno == EWOULDBLOCK))))
45             stats->rx_eagain++;
46           else if (rx_bytes < nbytes)
47             stats->rx_incomp++;
48         }
49     }
50   while ((rx_bytes == 0) ||
51          ((rx_bytes < 0) && ((errno == EAGAIN) || (errno == EWOULDBLOCK))));
52   
53   if (rx_bytes < 0)
54     {
55       errno_val = errno;
56       perror ("ERROR in sock_test_read()");
57       fprintf (stderr, "SOCK_TEST: ERROR: socket read "
58                "failed (errno = %d)!\n", errno_val);
59       errno = errno_val;
60     }
61   else if (stats)
62     stats->rx_bytes += rx_bytes;
63
64   return (rx_bytes);
65 }
66
67 static inline int
68 sock_test_write (int fd, uint8_t *buf, uint32_t nbytes,
69                  sock_test_stats_t *stats, uint32_t verbose)
70 {
71   int tx_bytes = 0;
72   int nbytes_left = nbytes;
73   int rv, errno_val;
74
75   do
76     {
77       if (stats)
78         stats->tx_xacts++;
79       rv = write (fd, buf, nbytes_left);
80       if (rv < 0)
81         {
82           if ((errno == EAGAIN) || (errno == EWOULDBLOCK))
83             {
84               if (stats)
85                 stats->tx_eagain++;
86               continue;
87             }
88           else
89             break;
90         }
91       tx_bytes += rv;
92      
93       if (tx_bytes != nbytes)
94         {
95           nbytes_left = nbytes_left - rv;
96           if (stats)
97             stats->tx_incomp++;
98           if (verbose)
99             {
100               printf ("SOCK_TEST: WARNING: bytes written (%d) "
101                       "!= bytes to write (%d)!\n", tx_bytes, nbytes);
102             }
103         }
104      
105     } while (tx_bytes != nbytes);
106
107   if (tx_bytes < 0)
108     {
109       errno_val = errno;
110       perror ("ERROR in sock_test_write()");
111       fprintf (stderr, "SOCK_TEST: ERROR: socket write failed "
112                "(errno = %d)!\n", errno_val);
113     }
114   else if (stats)
115     stats->tx_bytes += tx_bytes;
116   
117   return (tx_bytes);
118 }
119
120 #endif /* __sock_test_h__ */