X-Git-Url: https://gerrit.fd.io/r/gitweb?a=blobdiff_plain;f=src%2Fvcl%2Fsock_test_server.c;h=c0baefa99dea06d00fdbd584bfd47b545c4d33c0;hb=3ee1fe1608cca9f000bed59ba987c864adf37cd6;hp=728a6a7a14e819f05619a1b105a147b8e294fba3;hpb=5917939256af392914d8a648de0c3287042ddbf6;p=vpp.git diff --git a/src/vcl/sock_test_server.c b/src/vcl/sock_test_server.c index 728a6a7a14e..c0baefa99de 100644 --- a/src/vcl/sock_test_server.c +++ b/src/vcl/sock_test_server.c @@ -30,6 +30,9 @@ #if SOCK_SERVER_USE_EPOLL #include +#if !defined(VCL_TEST) +#include +#endif #endif #ifdef VCL_TEST @@ -64,6 +67,13 @@ typedef struct int epfd; struct epoll_event listen_ev; struct epoll_event wait_events[SOCK_SERVER_MAX_EPOLL_EVENTS]; +#if !defined (VCL_TEST) + int af_unix_listen_fd; + int af_unix_fd; + struct epoll_event af_unix_listen_ev; + struct sockaddr_un serveraddr; + uint32_t af_unix_xacts; +#endif #endif size_t num_conn; size_t conn_pool_size; @@ -138,8 +148,8 @@ conn_pool_expand (size_t expand_size) { int errno_val = errno; perror ("ERROR in conn_pool_expand()"); - fprintf (stderr, "ERROR: Memory allocation failed (errno = %d)!\n", - errno_val); + fprintf (stderr, "SERVER: ERROR: Memory allocation " + "failed (errno = %d)!\n", errno_val); } } @@ -282,6 +292,75 @@ stream_test_server (sock_server_conn_t * conn, int rx_bytes) } } +#if SOCK_SERVER_USE_EPOLL && !defined (VCL_TEST) +static inline void +af_unix_echo (void) +{ + sock_server_main_t *ssm = &sock_server_main; + int af_unix_client_fd; + int rv; + int errno_val; + uint8_t buffer[256]; + size_t nbytes = strlen (SOCK_TEST_MIXED_EPOLL_DATA) + 1; + +#if HAVE_ACCEPT4 + af_unix_client_fd = accept4 (ssm->af_unix_listen_fd, + (struct sockaddr *) NULL, NULL, NULL); +#else + af_unix_client_fd = accept (ssm->af_unix_listen_fd, + (struct sockaddr *) NULL, NULL); +#endif + if (af_unix_client_fd < 0) + { + errno_val = errno; + perror ("ERROR in af_unix_accept()"); + fprintf (stderr, "SERVER: ERROR: accept failed " + "(errno = %d)!\n", errno_val); + return; + } + + printf ("SERVER: Got an AF_UNIX connection -- fd = %d (0x%08x)!\n", + af_unix_client_fd, af_unix_client_fd); + + memset (buffer, 0, sizeof (buffer)); + + rv = read (af_unix_client_fd, buffer, nbytes); + if (rv < 0) + { + errno_val = errno; + perror ("ERROR in af_unix_echo(): read() failed"); + fprintf (stderr, "SERVER: ERROR: read(af_unix_client_fd %d (0x%x), " + "\"%s\", nbytes %lu) failed (errno = %d)!\n", + af_unix_client_fd, af_unix_client_fd, buffer, nbytes, + errno_val); + goto done; + } + + printf ("SERVER (AF_UNIX): RX (%d bytes) - '%s'\n", rv, buffer); + + if (!strncmp (SOCK_TEST_MIXED_EPOLL_DATA, (const char *) buffer, nbytes)) + { + rv = write (af_unix_client_fd, buffer, nbytes); + if (rv < 0) + { + errno_val = errno; + perror ("ERROR in af_unix_echo(): write() failed"); + fprintf (stderr, + "SERVER: ERROR: write(af_unix_client_fd %d (0x%x), " + "\"%s\", nbytes %ld) failed (errno = %d)!\n", + af_unix_client_fd, af_unix_client_fd, buffer, nbytes, + errno_val); + goto done; + } + printf ("SERVER (AF_UNIX): TX (%d bytes) - '%s'\n", rv, buffer); + ssm->af_unix_xacts++; + } +done: + close (af_unix_client_fd); +} + +#endif + static inline void new_client (void) { @@ -295,13 +374,12 @@ new_client (void) conn = conn_pool_alloc (); if (!conn) { - fprintf (stderr, "\nERROR: No free connections!\n"); + fprintf (stderr, "\nSERVER: ERROR: No free connections!\n"); return; } #ifdef VCL_TEST - client_fd = vppcom_session_accept (ssm->listen_fd, &conn->endpt, - -1.0 /* wait forever */ ); + client_fd = vppcom_session_accept (ssm->listen_fd, &conn->endpt, 0); if (client_fd < 0) errno = -client_fd; #elif HAVE_ACCEPT4 @@ -314,7 +392,9 @@ new_client (void) int errno_val; errno_val = errno; perror ("ERROR in new_client()"); - fprintf (stderr, "ERROR: accept failed (errno = %d)!\n", errno_val); + fprintf (stderr, "SERVER: ERROR: accept failed " + "(errno = %d)!\n", errno_val); + return; } printf ("SERVER: Got a connection -- fd = %d (0x%08x)!\n", @@ -344,7 +424,7 @@ new_client (void) int errno_val; errno_val = errno; perror ("ERROR in new_client()"); - fprintf (stderr, "ERROR: epoll_ctl failed (errno = %d)!\n", + fprintf (stderr, "SERVER: ERROR: epoll_ctl failed (errno = %d)!\n", errno_val); } else @@ -393,17 +473,61 @@ main (int argc, char **argv) else { ssm->listen_fd = - vppcom_session_create (VPPCOM_VRF_DEFAULT, VPPCOM_PROTO_TCP, - 0 /* is_nonblocking */ ); + vppcom_session_create (VPPCOM_PROTO_TCP, 0 /* is_nonblocking */ ); } #else ssm->listen_fd = socket (AF_INET, SOCK_STREAM, 0); +#if SOCK_SERVER_USE_EPOLL && !defined (VCL_TEST) + unlink ((const char *) SOCK_TEST_AF_UNIX_FILENAME); + ssm->af_unix_listen_fd = socket (AF_UNIX, SOCK_STREAM, 0); + if (ssm->af_unix_listen_fd < 0) + { + errno_val = errno; + perror ("ERROR in main(): socket(AF_UNIX) failed"); + fprintf (stderr, + "SERVER: ERROR: socket(AF_UNIX, SOCK_STREAM, 0) failed " + "(errno = %d)!\n", errno_val); + return ssm->af_unix_listen_fd; + } + + memset (&ssm->serveraddr, 0, sizeof (ssm->serveraddr)); + ssm->serveraddr.sun_family = AF_UNIX; + strcpy (ssm->serveraddr.sun_path, SOCK_TEST_AF_UNIX_FILENAME); + + rv = bind (ssm->af_unix_listen_fd, (struct sockaddr *) &ssm->serveraddr, + SUN_LEN (&ssm->serveraddr)); + if (rv < 0) + { + errno_val = errno; + perror ("ERROR in main(): bind(SOCK_TEST_AF_UNIX_FILENAME) failed"); + fprintf (stderr, "SERVER: ERROR: bind() fd %d, \"%s\": " + "failed (errno = %d)!\n", ssm->af_unix_listen_fd, + SOCK_TEST_AF_UNIX_FILENAME, errno_val); + close (ssm->af_unix_listen_fd); + unlink ((const char *) SOCK_TEST_AF_UNIX_FILENAME); + return rv; + } + + rv = listen (ssm->af_unix_listen_fd, 10); + if (rv < 0) + { + errno_val = errno; + perror ("ERROR in main(): listen(AF_UNIX) failed"); + fprintf (stderr, "SERVER: ERROR: listen() fd %d, \"%s\": " + "failed (errno = %d)!\n", ssm->af_unix_listen_fd, + SOCK_TEST_AF_UNIX_FILENAME, errno_val); + close (ssm->af_unix_listen_fd); + unlink ((const char *) SOCK_TEST_AF_UNIX_FILENAME); + return rv; + } +#endif /* SOCK_SERVER_USE_EPOLL */ #endif if (ssm->listen_fd < 0) { errno_val = errno; perror ("ERROR in main()"); - fprintf (stderr, "ERROR: socket() failed (errno = %d)!\n", errno_val); + fprintf (stderr, "SERVER: ERROR: socket() failed " + "(errno = %d)!\n", errno_val); return ssm->listen_fd; } @@ -414,7 +538,6 @@ main (int argc, char **argv) servaddr.sin_port = htons (port); #ifdef VCL_TEST - endpt.vrf = VPPCOM_VRF_DEFAULT; endpt.is_ip4 = (servaddr.sin_family == AF_INET); endpt.ip = (uint8_t *) & servaddr.sin_addr; endpt.port = (uint16_t) servaddr.sin_port; @@ -425,40 +548,6 @@ main (int argc, char **argv) errno = -rv; rv = -1; } - -#if VPPCOM_SESSION_ATTR_UNIT_TEST - buflen = BUFLEN; - if (vppcom_session_attr (ssm->listen_fd, VPPCOM_ATTR_GET_FLAGS, - buffer, &buflen) != VPPCOM_OK) - printf ("\nGET_FLAGS0: Oh no, Mr. Biiiiiiiiiiiilllllll ! ! ! !\n"); - buflen = BUFLEN; - *flags = O_RDWR | O_NONBLOCK; - if (vppcom_session_attr (ssm->listen_fd, VPPCOM_ATTR_SET_FLAGS, - buffer, &buflen) != VPPCOM_OK) - printf ("\nSET_FLAGS1: Oh no, Mr. Biiiiiiiiiiiilllllll ! ! ! !\n"); - buflen = BUFLEN; - if (vppcom_session_attr (ssm->listen_fd, VPPCOM_ATTR_GET_FLAGS, - buffer, &buflen) != VPPCOM_OK) - printf ("\nGET_FLAGS1:Oh no, Mr. Biiiiiiiiiiiilllllll ! ! ! !\n"); - *flags = O_RDWR; - buflen = BUFLEN; - if (vppcom_session_attr (ssm->listen_fd, VPPCOM_ATTR_SET_FLAGS, - buffer, &buflen) != VPPCOM_OK) - printf ("\nSET_FLAGS2: Oh no, Mr. Biiiiiiiiiiiilllllll ! ! ! !\n"); - buflen = BUFLEN; - if (vppcom_session_attr (ssm->listen_fd, VPPCOM_ATTR_GET_FLAGS, - buffer, &buflen) != VPPCOM_OK) - printf ("\nGET_FLAGS2:Oh no, Mr. Biiiiiiiiiiiilllllll ! ! ! !\n"); - - buflen = BUFLEN; - if (vppcom_session_attr (ssm->listen_fd, VPPCOM_ATTR_GET_PEER_ADDR, - buffer, &buflen) != VPPCOM_OK) - printf ("\nGET_PEER_ADDR: Oh no, Mr. Biiiiiiiiiiiilllllll ! ! ! !\n"); - buflen = BUFLEN; - if (vppcom_session_attr (ssm->listen_fd, VPPCOM_ATTR_GET_LCL_ADDR, - buffer, &buflen) != VPPCOM_OK) - printf ("\nGET_LCL_ADDR: Oh no, Mr. Biiiiiiiiiiiilllllll ! ! ! !\n"); -#endif #else rv = bind (ssm->listen_fd, (struct sockaddr *) &servaddr, sizeof (servaddr)); @@ -467,7 +556,8 @@ main (int argc, char **argv) { errno_val = errno; perror ("ERROR in main()"); - fprintf (stderr, "ERROR: bind failed (errno = %d)!\n", errno_val); + fprintf (stderr, "SERVER: ERROR: bind failed (errno = %d)!\n", + errno_val); return rv; } @@ -485,12 +575,11 @@ main (int argc, char **argv) { errno_val = errno; perror ("ERROR in main()"); - fprintf (stderr, "ERROR: listen failed (errno = %d)!\n", errno_val); + fprintf (stderr, "SERVER: ERROR: listen failed " + "(errno = %d)!\n", errno_val); return rv; } - printf ("\nSERVER: Waiting for a client to connect on port %d...\n", port); - #if ! SOCK_SERVER_USE_EPOLL FD_ZERO (&ssm->wr_fdset); @@ -511,7 +600,7 @@ main (int argc, char **argv) { errno_val = errno; perror ("ERROR in main()"); - fprintf (stderr, "ERROR: epoll_create failed (errno = %d)!\n", + fprintf (stderr, "SERVER: ERROR: epoll_create failed (errno = %d)!\n", errno_val); return ssm->epfd; } @@ -524,17 +613,37 @@ main (int argc, char **argv) if (rv < 0) errno = -rv; #else + ssm->af_unix_listen_ev.events = EPOLLIN; + ssm->af_unix_listen_ev.data.u32 = SOCK_TEST_AF_UNIX_ACCEPT_DATA; + rv = epoll_ctl (ssm->epfd, EPOLL_CTL_ADD, ssm->af_unix_listen_fd, + &ssm->af_unix_listen_ev); + if (rv < 0) + { + errno_val = errno; + perror ("ERROR in main(): mixed epoll_ctl(EPOLL_CTL_ADD)"); + fprintf (stderr, "SERVER: ERROR: mixed epoll_ctl(epfd %d (0x%x), " + "EPOLL_CTL_ADD, af_unix_listen_fd %d (0x%x), EPOLLIN) failed " + "(errno = %d)!\n", ssm->epfd, ssm->epfd, + ssm->af_unix_listen_fd, ssm->af_unix_listen_fd, errno_val); + close (ssm->af_unix_listen_fd); + unlink ((const char *) SOCK_TEST_AF_UNIX_FILENAME); + return rv; + } + rv = epoll_ctl (ssm->epfd, EPOLL_CTL_ADD, ssm->listen_fd, &ssm->listen_ev); #endif if (rv < 0) { errno_val = errno; perror ("ERROR in main()"); - fprintf (stderr, "ERROR: epoll_ctl failed (errno = %d)!\n", errno_val); + fprintf (stderr, "SERVER: ERROR: epoll_ctl failed " + "(errno = %d)!\n", errno_val); return rv; } #endif + printf ("\nSERVER: Waiting for a client to connect on port %d...\n", port); + while (1) { #if ! SOCK_SERVER_USE_EPOLL @@ -553,7 +662,7 @@ main (int argc, char **argv) if (rv < 0) { perror ("select()"); - fprintf (stderr, "\nERROR: select() failed -- aborting!\n"); + fprintf (stderr, "\nSERVER: ERROR: select() failed -- aborting!\n"); main_rv = -1; goto done; } @@ -583,13 +692,14 @@ main (int argc, char **argv) if (num_ev < 0) { perror ("epoll_wait()"); - fprintf (stderr, "\nERROR: epoll_wait() failed -- aborting!\n"); + fprintf (stderr, "\nSERVER: ERROR: epoll_wait() " + "failed -- aborting!\n"); main_rv = -1; goto done; } if (num_ev == 0) { - fprintf (stderr, "\nepoll_wait() timeout!\n"); + fprintf (stderr, "\nSERVER: epoll_wait() timeout!\n"); continue; } for (i = 0; i < num_ev; i++) @@ -599,6 +709,14 @@ main (int argc, char **argv) new_client (); continue; } +#if !defined (VCL_TEST) + else if (ssm->wait_events[i].data.u32 == + SOCK_TEST_AF_UNIX_ACCEPT_DATA) + { + af_unix_echo (); + continue; + } +#endif conn = &ssm->conn_pool[ssm->wait_events[i].data.u32]; #endif client_fd = conn->fd; @@ -609,24 +727,6 @@ main (int argc, char **argv) if (EPOLLIN & ssm->wait_events[i].events) #endif { -#ifdef VCL_TEST -#if VPPCOM_SESSION_ATTR_UNIT_TEST - buflen = BUFLEN; - if (vppcom_session_attr (client_fd, VPPCOM_ATTR_GET_NREAD, - buffer, &buflen) < VPPCOM_OK) - printf ("\nNREAD: Oh no, Mr. Biiiiiiiiiiiilllllll ! ! ! !\n"); - if (vppcom_session_attr (client_fd, - VPPCOM_ATTR_GET_PEER_ADDR, - buffer, &buflen) != VPPCOM_OK) - printf ("\nGET_PEER_ADDR: Oh no, Mr. " - "Biiiiiiiiiiiilllllll ! ! ! !\n"); - buflen = BUFLEN; - if (vppcom_session_attr (client_fd, VPPCOM_ATTR_GET_LCL_ADDR, - buffer, &buflen) != VPPCOM_OK) - printf ("\nGET_LCL_ADDR: Oh no, Mr. " - "Biiiiiiiiiiiilllllll ! ! ! !\n"); -#endif -#endif rx_bytes = sock_test_read (client_fd, conn->buf, conn->buf_size, &conn->stats); if (rx_bytes > 0) @@ -681,6 +781,7 @@ main (int argc, char **argv) close (client_fd); #endif conn_pool_free (conn); + printf ("SERVER: Closed client fd %d\n", client_fd); #if ! SOCK_SERVER_USE_EPOLL if (ssm->nfds == (ssm->listen_fd + 1)) #else @@ -696,7 +797,8 @@ main (int argc, char **argv) break; default: - fprintf (stderr, "ERROR: Unknown test type!\n"); + fprintf (stderr, + "SERVER: ERROR: Unknown test type!\n"); sock_test_cfg_dump (rx_cfg, 0 /* is_client */ ); break; } @@ -734,13 +836,15 @@ main (int argc, char **argv) if (isascii (conn->buf[0])) { - // If it looks vaguely like a string, make sure it's terminated + /* If it looks vaguely like a string, + * make sure it's terminated + */ ((char *) conn->buf)[rx_bytes < conn->buf_size ? rx_bytes : conn->buf_size - 1] = 0; if (xtra) - fprintf (stderr, - "ERROR: FIFO not drained in previous test!\n" + fprintf (stderr, "SERVER: ERROR: " + "FIFO not drained in previous test!\n" " extra chunks %u (0x%x)\n" " extra bytes %lu (0x%lx)\n", xtra, xtra, xtra_bytes, xtra_bytes); @@ -776,6 +880,12 @@ done: vppcom_app_destroy (); #else close (ssm->listen_fd); + +#if SOCK_SERVER_USE_EPOLL && !defined (VCL_TEST) + close (ssm->af_unix_listen_fd); + unlink ((const char *) SOCK_TEST_AF_UNIX_FILENAME); +#endif /* SOCK_SERVER_USE_EPOLL */ + #endif if (ssm->conn_pool) free (ssm->conn_pool);