Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 1 | /* |
| 2 | *------------------------------------------------------------------ |
Damjan Marion | 5fec1e8 | 2017-04-13 19:13:47 +0200 | [diff] [blame] | 3 | * test.c |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 4 | * |
| 5 | * Copyright (c) 2016 Cisco and/or its affiliates. |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at: |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | *------------------------------------------------------------------ |
| 18 | */ |
| 19 | #include <stdio.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <sys/types.h> |
| 22 | #include <sys/socket.h> |
| 23 | #include <sys/mman.h> |
| 24 | #include <sys/stat.h> |
| 25 | #include <netinet/in.h> |
| 26 | #include <netdb.h> |
| 27 | |
| 28 | #include <time.h> /* time_t, time (for timestamp in second) */ |
| 29 | #include <sys/timeb.h> /* ftime, timeb (for timestamp in millisecond) */ |
| 30 | #include <sys/time.h> /* gettimeofday, timeval (for timestamp in microsecond) */ |
| 31 | |
| 32 | #include <vnet/vnet.h> |
| 33 | #include <vlib/vlib.h> |
| 34 | #include <vlib/unix/unix.h> |
| 35 | #include <vlibapi/api.h> |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 36 | |
| 37 | #include <vpp/api/vpe_msg_enum.h> |
| 38 | #include <signal.h> |
Damjan Marion | 5fec1e8 | 2017-04-13 19:13:47 +0200 | [diff] [blame] | 39 | #include "vppapiclient.h" |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 40 | |
| 41 | #define vl_typedefs /* define message structures */ |
| 42 | #include <vpp/api/vpe_all_api_h.h> |
| 43 | #undef vl_typedefs |
| 44 | |
| 45 | /* we are not linking with vlib */ |
| 46 | vlib_main_t vlib_global_main; |
| 47 | vlib_main_t **vlib_mains; |
| 48 | |
| 49 | volatile int sigterm_received = 0; |
| 50 | volatile u32 result_ready; |
| 51 | volatile u16 result_msg_id; |
| 52 | |
| 53 | /* M_NOALLOC: construct, but don't yet send a message */ |
| 54 | |
| 55 | #define M_NOALLOC(T,t) \ |
| 56 | do { \ |
| 57 | result_ready = 0; \ |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 58 | clib_memset (mp, 0, sizeof (*mp)); \ |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 59 | mp->_vl_msg_id = ntohs (VL_API_##T); \ |
| 60 | mp->client_index = am->my_client_index; \ |
| 61 | } while(0); |
| 62 | |
| 63 | |
| 64 | |
Andrey "Zed" Zaikin | 334167f | 2018-04-09 16:42:42 +0300 | [diff] [blame] | 65 | void |
| 66 | wrap_vac_callback (unsigned char *data, int len) |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 67 | { |
| 68 | //printf("Callback %d\n", len); |
| 69 | result_ready = 1; |
| 70 | result_msg_id = ntohs(*((u16 *)data)); |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 71 | } |
| 72 | |
Ole Troan | 73710c7 | 2018-06-04 22:27:49 +0200 | [diff] [blame] | 73 | static void |
| 74 | test_connect () |
| 75 | { |
| 76 | static int i; |
| 77 | int rv = vac_connect("vac_client", NULL, wrap_vac_callback, 32 /* rx queue-length*/); |
| 78 | if (rv != 0) { |
| 79 | printf("Connect failed: %d\n", rv); |
| 80 | exit(rv); |
| 81 | } |
| 82 | printf("."); |
| 83 | vac_disconnect(); |
| 84 | i++; |
| 85 | } |
| 86 | |
| 87 | static void |
| 88 | test_messages (void) |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 89 | { |
Dave Barach | 39d6911 | 2019-11-27 11:42:13 -0500 | [diff] [blame] | 90 | api_main_t * am = vlibapi_get_main(); |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 91 | vl_api_show_version_t message; |
| 92 | vl_api_show_version_t *mp; |
| 93 | int async = 1; |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 94 | |
Ole Troan | 73710c7 | 2018-06-04 22:27:49 +0200 | [diff] [blame] | 95 | int rv = vac_connect("vac_client", NULL, wrap_vac_callback, 32 /* rx queue-length*/); |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 96 | if (rv != 0) { |
| 97 | printf("Connect failed: %d\n", rv); |
| 98 | exit(rv); |
| 99 | } |
Ole Troan | 73710c7 | 2018-06-04 22:27:49 +0200 | [diff] [blame] | 100 | struct timeb timer_msec; |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 101 | long long int timestamp_msec_start; /* timestamp in millisecond. */ |
| 102 | if (!ftime(&timer_msec)) { |
| 103 | timestamp_msec_start = ((long long int) timer_msec.time) * 1000ll + |
| 104 | (long long int) timer_msec.millitm; |
| 105 | } |
| 106 | else { |
| 107 | timestamp_msec_start = -1; |
| 108 | } |
| 109 | |
| 110 | |
| 111 | /* |
| 112 | * Test vpe_api_write and vpe_api_read to send and recv message for an |
| 113 | * API |
| 114 | */ |
| 115 | int i; |
| 116 | long int no_msgs = 10000; |
| 117 | mp = &message; |
| 118 | |
| 119 | for (i = 0; i < no_msgs; i++) { |
| 120 | /* Construct the API message */ |
| 121 | M_NOALLOC(SHOW_VERSION, show_version); |
Damjan Marion | 5fec1e8 | 2017-04-13 19:13:47 +0200 | [diff] [blame] | 122 | vac_write((char *)mp, sizeof(*mp)); |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 123 | #ifndef __COVERITY__ |
| 124 | /* As given, async is always 1. Shut up Coverity about it */ |
| 125 | if (!async) |
| 126 | while (result_ready == 0); |
| 127 | #endif |
| 128 | } |
| 129 | if (async) { |
| 130 | vl_api_control_ping_t control; |
| 131 | vl_api_control_ping_t *mp; |
| 132 | mp = &control; |
| 133 | M_NOALLOC(CONTROL_PING, control_ping); |
Damjan Marion | 5fec1e8 | 2017-04-13 19:13:47 +0200 | [diff] [blame] | 134 | vac_write((char *)mp, sizeof(*mp)); |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 135 | |
| 136 | while (result_msg_id != VL_API_CONTROL_PING_REPLY); |
| 137 | } |
| 138 | |
| 139 | long long int timestamp_msec_end; /* timestamp in millisecond. */ |
| 140 | if (!ftime(&timer_msec)) { |
| 141 | timestamp_msec_end = ((long long int) timer_msec.time) * 1000ll + |
| 142 | (long long int) timer_msec.millitm; |
| 143 | } |
| 144 | else { |
| 145 | timestamp_msec_end = -1; |
| 146 | } |
| 147 | |
| 148 | printf("Took %lld msec, %lld msgs/msec \n", (timestamp_msec_end - timestamp_msec_start), |
| 149 | no_msgs/(timestamp_msec_end - timestamp_msec_start)); |
Damjan Marion | 5fec1e8 | 2017-04-13 19:13:47 +0200 | [diff] [blame] | 150 | printf("Exiting...\n"); |
| 151 | vac_disconnect(); |
Ole Troan | 73710c7 | 2018-06-04 22:27:49 +0200 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | int main (int argc, char ** argv) |
| 155 | { |
| 156 | int i; |
| 157 | |
| 158 | for (i = 0; i < 1000; i++) { |
| 159 | test_connect(); |
| 160 | } |
| 161 | test_messages(); |
Damjan Marion | 7cd468a | 2016-12-19 23:05:39 +0100 | [diff] [blame] | 162 | exit (0); |
| 163 | } |