Line data Source code
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 stabrt(_fmt, _args...) \ 42 : { \ 43 : printf ("\nERROR: " _fmt "\n", ##_args); \ 44 : exit (1); \ 45 : } 46 : #define stfail(_fn) \ 47 : { \ 48 : perror ("ERROR when calling " _fn); \ 49 : printf ("\nERROR: " _fn " failed (errno = %d)!\n", errno); \ 50 : exit (1); \ 51 : } 52 : 53 : static inline int 54 1899 : sock_test_read (int fd, uint8_t * buf, uint32_t nbytes, 55 : vcl_test_stats_t * stats) 56 : { 57 : int rx_bytes; 58 : 59 : do 60 : { 61 1899 : if (stats) 62 562 : stats->rx_xacts++; 63 1899 : rx_bytes = read (fd, buf, nbytes); 64 1899 : if (stats) 65 : { 66 562 : if ((rx_bytes == 0) || 67 : ((rx_bytes < 0) 68 0 : && ((errno == EAGAIN) || (errno == EWOULDBLOCK)))) 69 0 : stats->rx_eagain++; 70 562 : else if (rx_bytes < nbytes) 71 562 : stats->rx_incomp++; 72 : } 73 : } 74 1899 : while ((rx_bytes == 0) || 75 3216 : ((rx_bytes < 0) && ((errno == EAGAIN) || (errno == EWOULDBLOCK)))); 76 : 77 582 : if (rx_bytes < 0) 78 0 : stfail ("sock_test_read()"); 79 : 80 582 : if (stats) 81 562 : stats->rx_bytes += rx_bytes; 82 : 83 582 : return (rx_bytes); 84 : } 85 : 86 : static inline int 87 6276 : sock_test_write (int fd, uint8_t * buf, uint32_t nbytes, 88 : vcl_test_stats_t * stats, uint32_t verbose) 89 : { 90 6276 : int tx_bytes = 0, nbytes_left = nbytes, rv; 91 : 92 : do 93 : { 94 281517 : if (stats) 95 281491 : stats->tx_xacts++; 96 281517 : rv = write (fd, buf, nbytes_left); 97 281517 : if (rv < 0) 98 : { 99 274852 : if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) 100 : { 101 274852 : if (stats) 102 274852 : stats->tx_eagain++; 103 274852 : continue; 104 : } 105 : else 106 : break; 107 : } 108 6665 : tx_bytes += rv; 109 : 110 6665 : if (tx_bytes != nbytes) 111 : { 112 389 : nbytes_left = nbytes_left - rv; 113 389 : if (stats) 114 389 : stats->tx_incomp++; 115 389 : if (verbose) 116 : { 117 0 : stinf ("bytes written (%d) != bytes to write (%d)!\n", tx_bytes, 118 : nbytes); 119 : } 120 : } 121 : 122 : } 123 281517 : while (tx_bytes != nbytes); 124 : 125 6276 : if (tx_bytes < 0) 126 0 : stfail ("sock_test_write()"); 127 : 128 6276 : if (stats) 129 6250 : stats->tx_bytes += tx_bytes; 130 : 131 6276 : return (tx_bytes); 132 : } 133 : 134 : #endif /* __sock_test_h__ */ 135 : 136 : /* 137 : * fd.io coding-style-patch-verification: ON 138 : * 139 : * Local Variables: 140 : * eval: (c-set-style "gnu") 141 : * End: 142 : */