Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 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 | |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame^] | 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 | |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 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 | errno_val = errno; |
| 81 | perror ("ERROR in sock_test_read()"); |
| 82 | fprintf (stderr, "SOCK_TEST: ERROR: socket read " |
| 83 | "failed (errno = %d)!\n", errno_val); |
| 84 | errno = errno_val; |
| 85 | } |
| 86 | else if (stats) |
| 87 | stats->rx_bytes += rx_bytes; |
| 88 | |
| 89 | return (rx_bytes); |
| 90 | } |
| 91 | |
| 92 | static inline int |
| 93 | vcl_test_write (int fd, uint8_t *buf, uint32_t nbytes, |
| 94 | sock_test_stats_t *stats, uint32_t verbose) |
| 95 | { |
| 96 | int tx_bytes = 0; |
| 97 | int nbytes_left = nbytes; |
| 98 | int rv, errno_val; |
| 99 | |
| 100 | do |
| 101 | { |
| 102 | if (stats) |
| 103 | stats->tx_xacts++; |
| 104 | rv = vppcom_session_write (fd, buf, nbytes_left); |
| 105 | if (rv < 0) |
| 106 | { |
| 107 | errno = -rv; |
| 108 | rv = -1; |
| 109 | } |
| 110 | if (rv < 0) |
| 111 | { |
| 112 | if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) |
| 113 | { |
| 114 | if (stats) |
| 115 | stats->tx_eagain++; |
| 116 | continue; |
| 117 | } |
| 118 | else |
| 119 | break; |
| 120 | } |
| 121 | tx_bytes += rv; |
| 122 | |
| 123 | if (tx_bytes != nbytes) |
| 124 | { |
| 125 | nbytes_left = nbytes_left - rv; |
| 126 | if (stats) |
| 127 | stats->tx_incomp++; |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | } while (tx_bytes != nbytes); |
| 131 | |
| 132 | if (tx_bytes < 0) |
| 133 | { |
| 134 | errno_val = errno; |
| 135 | perror ("ERROR in sock_test_write()"); |
| 136 | fprintf (stderr, "SOCK_TEST: ERROR: socket write failed " |
| 137 | "(errno = %d)!\n", errno_val); |
| 138 | } |
| 139 | else if (stats) |
| 140 | stats->tx_bytes += tx_bytes; |
| 141 | |
| 142 | return (tx_bytes); |
| 143 | } |
| 144 | |
| 145 | #endif /* __vcl_test_h__ */ |