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