Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1 | /* |
| 2 | *------------------------------------------------------------------ |
| 3 | * Copyright (c) 2017 Cisco and/or its affiliates. |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at: |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | *------------------------------------------------------------------ |
| 16 | */ |
| 17 | |
| 18 | #include <stdlib.h> |
| 19 | #include <stdint.h> |
| 20 | #include <net/if.h> |
| 21 | #include <sys/types.h> |
| 22 | #include <fcntl.h> |
| 23 | #include <sys/ioctl.h> |
| 24 | #include <sys/socket.h> |
| 25 | #include <sys/un.h> |
| 26 | #include <sys/uio.h> |
| 27 | #include <sys/mman.h> |
| 28 | #include <sys/prctl.h> |
| 29 | #include <inttypes.h> |
| 30 | #include <string.h> |
| 31 | #include <stdio.h> |
| 32 | #include <netdb.h> |
| 33 | #include <linux/ip.h> |
| 34 | #include <linux/icmp.h> |
| 35 | #include <arpa/inet.h> |
| 36 | #include <stdlib.h> |
| 37 | #include <netinet/if_ether.h> |
| 38 | #include <net/if_arp.h> |
| 39 | #include <asm/byteorder.h> |
| 40 | #include <byteswap.h> |
| 41 | #include <string.h> |
| 42 | #include <sys/epoll.h> |
| 43 | #include <errno.h> |
| 44 | #include <unistd.h> |
| 45 | #include <signal.h> |
Jakub Grajciar | e4bb5bc | 2017-11-02 14:33:27 +0100 | [diff] [blame^] | 46 | #include <pthread.h> |
| 47 | |
| 48 | #include <time.h> |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 49 | |
| 50 | #include <libmemif.h> |
| 51 | #include <icmp_proto.h> |
| 52 | |
| 53 | #define APP_NAME "ICMP_Responder" |
| 54 | #define IF_NAME "memif_connection" |
| 55 | |
| 56 | |
| 57 | #ifdef ICMP_DBG |
| 58 | #define DBG(...) do { \ |
| 59 | printf (APP_NAME":%s:%d: ", __func__, __LINE__); \ |
| 60 | printf (__VA_ARGS__); \ |
| 61 | printf ("\n"); \ |
| 62 | } while (0) |
Jakub Grajciar | e4bb5bc | 2017-11-02 14:33:27 +0100 | [diff] [blame^] | 63 | #define LOG(...) do { \ |
| 64 | if (enable_log) { \ |
| 65 | dprintf (out_fd, __VA_ARGS__); \ |
| 66 | dprintf (out_fd, "\n"); \ |
| 67 | } \ |
| 68 | } while (0) |
| 69 | #define LOG_FILE "/tmp/memif_time_test.txt" |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 70 | #else |
| 71 | #define DBG(...) |
Jakub Grajciar | e4bb5bc | 2017-11-02 14:33:27 +0100 | [diff] [blame^] | 72 | #define LOG(...) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 73 | #endif |
| 74 | |
| 75 | #define INFO(...) do { \ |
| 76 | printf ("INFO: "__VA_ARGS__); \ |
| 77 | printf ("\n"); \ |
| 78 | } while (0) |
| 79 | |
Jakub Grajciar | e4bb5bc | 2017-11-02 14:33:27 +0100 | [diff] [blame^] | 80 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 81 | /* maximum tx/rx memif buffers */ |
| 82 | #define MAX_MEMIF_BUFS 256 |
| 83 | #define MAX_CONNS 50 |
| 84 | |
| 85 | int epfd; |
Jakub Grajciar | e4bb5bc | 2017-11-02 14:33:27 +0100 | [diff] [blame^] | 86 | int out_fd; |
| 87 | uint8_t enable_log; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 88 | |
| 89 | typedef struct |
| 90 | { |
| 91 | uint16_t index; |
| 92 | /* memif conenction handle */ |
| 93 | memif_conn_handle_t conn; |
| 94 | /* tx buffers */ |
| 95 | memif_buffer_t *tx_bufs; |
| 96 | /* allocated tx buffers counter */ |
| 97 | /* number of tx buffers pointing to shared memory */ |
| 98 | uint16_t tx_buf_num; |
| 99 | /* rx buffers */ |
| 100 | memif_buffer_t *rx_bufs; |
| 101 | /* allcoated rx buffers counter */ |
| 102 | /* number of rx buffers pointing to shared memory */ |
| 103 | uint16_t rx_buf_num; |
| 104 | /* interface ip address */ |
| 105 | uint8_t ip_addr[4]; |
Jakub Grajciar | e4bb5bc | 2017-11-02 14:33:27 +0100 | [diff] [blame^] | 106 | uint16_t seq; |
| 107 | uint64_t tx_counter, rx_counter, tx_err_counter; |
| 108 | uint64_t t_sec, t_nsec; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 109 | } memif_connection_t; |
| 110 | |
Jakub Grajciar | e4bb5bc | 2017-11-02 14:33:27 +0100 | [diff] [blame^] | 111 | typedef struct |
| 112 | { |
| 113 | uint16_t index; |
| 114 | uint64_t packet_num; |
| 115 | uint8_t ip_daddr[4]; |
| 116 | uint8_t hw_daddr[6]; |
| 117 | } icmpr_thread_data_t; |
| 118 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 119 | memif_connection_t memif_connection[MAX_CONNS]; |
Jakub Grajciar | e4bb5bc | 2017-11-02 14:33:27 +0100 | [diff] [blame^] | 120 | icmpr_thread_data_t icmpr_thread_data[MAX_CONNS]; |
| 121 | pthread_t thread[MAX_CONNS]; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 122 | long ctx[MAX_CONNS]; |
| 123 | |
| 124 | /* print details for all memif connections */ |
| 125 | static void |
| 126 | print_memif_details () |
| 127 | { |
| 128 | memif_details_t md; |
| 129 | ssize_t buflen; |
| 130 | char *buf; |
| 131 | int err, i, e; |
| 132 | buflen = 2048; |
| 133 | buf = malloc (buflen); |
| 134 | printf ("MEMIF DETAILS\n"); |
| 135 | printf ("==============================\n"); |
| 136 | for (i = 0; i < MAX_CONNS; i++) |
| 137 | { |
| 138 | memif_connection_t *c = &memif_connection[i]; |
| 139 | |
| 140 | memset (&md, 0, sizeof (md)); |
| 141 | memset (buf, 0, buflen); |
| 142 | |
| 143 | err = memif_get_details (c->conn, &md, buf, buflen); |
| 144 | if (err != MEMIF_ERR_SUCCESS) |
| 145 | { |
| 146 | if (err != MEMIF_ERR_NOCONN) |
| 147 | INFO ("%s", memif_strerror (err)); |
| 148 | continue; |
| 149 | } |
| 150 | |
| 151 | printf ("interface index: %d\n", i); |
| 152 | |
| 153 | printf ("\tinterface ip: %u.%u.%u.%u\n", |
| 154 | c->ip_addr[0], c->ip_addr[1], c->ip_addr[2], c->ip_addr[3]); |
| 155 | printf ("\tinterface name: %s\n", (char *) md.if_name); |
| 156 | printf ("\tapp name: %s\n", (char *) md.inst_name); |
| 157 | printf ("\tremote interface name: %s\n", (char *) md.remote_if_name); |
| 158 | printf ("\tremote app name: %s\n", (char *) md.remote_inst_name); |
| 159 | printf ("\tid: %u\n", md.id); |
| 160 | printf ("\tsecret: %s\n", (char *) md.secret); |
| 161 | printf ("\trole: "); |
| 162 | if (md.role) |
| 163 | printf ("slave\n"); |
| 164 | else |
| 165 | printf ("master\n"); |
| 166 | printf ("\tmode: "); |
| 167 | switch (md.mode) |
| 168 | { |
| 169 | case 0: |
| 170 | printf ("ethernet\n"); |
| 171 | break; |
| 172 | case 1: |
| 173 | printf ("ip\n"); |
| 174 | break; |
| 175 | case 2: |
| 176 | printf ("punt/inject\n"); |
| 177 | break; |
| 178 | default: |
| 179 | printf ("unknown\n"); |
| 180 | break; |
| 181 | } |
| 182 | printf ("\tsocket filename: %s\n", (char *) md.socket_filename); |
| 183 | printf ("\trx queues:\n"); |
| 184 | for (e = 0; e < md.rx_queues_num; e++) |
| 185 | { |
| 186 | printf ("\t\tqueue id: %u\n", md.rx_queues[e].qid); |
| 187 | printf ("\t\tring size: %u\n", md.rx_queues[e].ring_size); |
| 188 | printf ("\t\tbuffer size: %u\n", md.rx_queues[e].buffer_size); |
| 189 | } |
| 190 | printf ("\ttx queues:\n"); |
| 191 | for (e = 0; e < md.tx_queues_num; e++) |
| 192 | { |
| 193 | printf ("\t\tqueue id: %u\n", md.tx_queues[e].qid); |
| 194 | printf ("\t\tring size: %u\n", md.tx_queues[e].ring_size); |
| 195 | printf ("\t\tbuffer size: %u\n", md.tx_queues[e].buffer_size); |
| 196 | } |
| 197 | printf ("\tlink: "); |
| 198 | if (md.link_up_down) |
| 199 | printf ("up\n"); |
| 200 | else |
| 201 | printf ("down\n"); |
| 202 | } |
| 203 | free (buf); |
| 204 | } |
| 205 | |
| 206 | int |
| 207 | add_epoll_fd (int fd, uint32_t events) |
| 208 | { |
| 209 | if (fd < 0) |
| 210 | { |
| 211 | DBG ("invalid fd %d", fd); |
| 212 | return -1; |
| 213 | } |
| 214 | struct epoll_event evt; |
| 215 | memset (&evt, 0, sizeof (evt)); |
| 216 | evt.events = events; |
| 217 | evt.data.fd = fd; |
| 218 | if (epoll_ctl (epfd, EPOLL_CTL_ADD, fd, &evt) < 0) |
| 219 | { |
| 220 | DBG ("epoll_ctl: %s fd %d", strerror (errno), fd); |
| 221 | return -1; |
| 222 | } |
| 223 | DBG ("fd %d added to epoll", fd); |
| 224 | return 0; |
| 225 | } |
| 226 | |
| 227 | int |
| 228 | mod_epoll_fd (int fd, uint32_t events) |
| 229 | { |
| 230 | if (fd < 0) |
| 231 | { |
| 232 | DBG ("invalid fd %d", fd); |
| 233 | return -1; |
| 234 | } |
| 235 | struct epoll_event evt; |
| 236 | memset (&evt, 0, sizeof (evt)); |
| 237 | evt.events = events; |
| 238 | evt.data.fd = fd; |
| 239 | if (epoll_ctl (epfd, EPOLL_CTL_MOD, fd, &evt) < 0) |
| 240 | { |
| 241 | DBG ("epoll_ctl: %s fd %d", strerror (errno), fd); |
| 242 | return -1; |
| 243 | } |
| 244 | DBG ("fd %d moddified on epoll", fd); |
| 245 | return 0; |
| 246 | } |
| 247 | |
| 248 | int |
| 249 | del_epoll_fd (int fd) |
| 250 | { |
| 251 | if (fd < 0) |
| 252 | { |
| 253 | DBG ("invalid fd %d", fd); |
| 254 | return -1; |
| 255 | } |
| 256 | struct epoll_event evt; |
| 257 | memset (&evt, 0, sizeof (evt)); |
| 258 | if (epoll_ctl (epfd, EPOLL_CTL_DEL, fd, &evt) < 0) |
| 259 | { |
| 260 | DBG ("epoll_ctl: %s fd %d", strerror (errno), fd); |
| 261 | return -1; |
| 262 | } |
| 263 | DBG ("fd %d removed from epoll", fd); |
| 264 | return 0; |
| 265 | } |
| 266 | |
| 267 | /* informs user about connected status. private_ctx is used by user to identify connection |
| 268 | (multiple connections WIP) */ |
| 269 | int |
| 270 | on_connect (memif_conn_handle_t conn, void *private_ctx) |
| 271 | { |
| 272 | INFO ("memif connected!"); |
Jakub Grajciar | e4bb5bc | 2017-11-02 14:33:27 +0100 | [diff] [blame^] | 273 | enable_log = 1; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 274 | return 0; |
| 275 | } |
| 276 | |
| 277 | /* informs user about disconnected status. private_ctx is used by user to identify connection |
| 278 | (multiple connections WIP) */ |
| 279 | int |
| 280 | on_disconnect (memif_conn_handle_t conn, void *private_ctx) |
| 281 | { |
| 282 | INFO ("memif disconnected!"); |
| 283 | return 0; |
| 284 | } |
| 285 | |
| 286 | /* user needs to watch new fd or stop watching fd that is about to be closed. |
| 287 | control fd will be modified during connection establishment to minimize CPU usage */ |
| 288 | int |
| 289 | control_fd_update (int fd, uint8_t events) |
| 290 | { |
| 291 | /* convert memif event definitions to epoll events */ |
| 292 | if (events & MEMIF_FD_EVENT_DEL) |
| 293 | return del_epoll_fd (fd); |
| 294 | |
| 295 | uint32_t evt = 0; |
| 296 | if (events & MEMIF_FD_EVENT_READ) |
| 297 | evt |= EPOLLIN; |
| 298 | if (events & MEMIF_FD_EVENT_WRITE) |
| 299 | evt |= EPOLLOUT; |
| 300 | |
| 301 | if (events & MEMIF_FD_EVENT_MOD) |
| 302 | return mod_epoll_fd (fd, evt); |
| 303 | |
| 304 | return add_epoll_fd (fd, evt); |
| 305 | } |
| 306 | |
| 307 | int |
Jakub Grajciar | e4bb5bc | 2017-11-02 14:33:27 +0100 | [diff] [blame^] | 308 | icmpr_buffer_alloc (long index, long n, uint16_t * r, uint16_t i, |
| 309 | uint16_t qid) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 310 | { |
| 311 | memif_connection_t *c = &memif_connection[index]; |
| 312 | int err; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 313 | /* set data pointer to shared memory and set buffer_len to shared mmeory buffer len */ |
Jakub Grajciar | e4bb5bc | 2017-11-02 14:33:27 +0100 | [diff] [blame^] | 314 | err = memif_buffer_alloc (c->conn, qid, c->tx_bufs + i, n, r, 0); |
| 315 | if ((err != MEMIF_ERR_SUCCESS) && (err != MEMIF_ERR_NOBUF_RING)) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 316 | { |
| 317 | INFO ("memif_buffer_alloc: %s", memif_strerror (err)); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 318 | return -1; |
| 319 | } |
Jakub Grajciar | e4bb5bc | 2017-11-02 14:33:27 +0100 | [diff] [blame^] | 320 | c->tx_buf_num += *r; |
| 321 | DBG ("allocated %d/%ld buffers, %u free buffers", *r, n, |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 322 | MAX_MEMIF_BUFS - c->tx_buf_num); |
| 323 | return 0; |
| 324 | } |
| 325 | |
| 326 | int |
| 327 | icmpr_tx_burst (long index, uint16_t qid) |
| 328 | { |
| 329 | memif_connection_t *c = &memif_connection[index]; |
| 330 | int err; |
| 331 | uint16_t r; |
| 332 | /* inform peer memif interface about data in shared memory buffers */ |
| 333 | /* mark memif buffers as free */ |
| 334 | err = memif_tx_burst (c->conn, qid, c->tx_bufs, c->tx_buf_num, &r); |
| 335 | if (err != MEMIF_ERR_SUCCESS) |
| 336 | INFO ("memif_tx_burst: %s", memif_strerror (err)); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 337 | c->tx_buf_num -= r; |
Jakub Grajciar | e4bb5bc | 2017-11-02 14:33:27 +0100 | [diff] [blame^] | 338 | c->tx_counter += r; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 339 | return 0; |
| 340 | } |
| 341 | |
| 342 | /* called when event is polled on interrupt file descriptor. |
| 343 | there are packets in shared memory ready to be received */ |
| 344 | int |
| 345 | on_interrupt (memif_conn_handle_t conn, void *private_ctx, uint16_t qid) |
| 346 | { |
| 347 | long index = *((long *) private_ctx); |
| 348 | memif_connection_t *c = &memif_connection[index]; |
| 349 | if (c->index != index) |
| 350 | { |
| 351 | INFO ("invalid context: %ld/%u", index, c->index); |
| 352 | return 0; |
| 353 | } |
Jakub Grajciar | e4bb5bc | 2017-11-02 14:33:27 +0100 | [diff] [blame^] | 354 | |
| 355 | int err = MEMIF_ERR_SUCCESS, ret_val; |
| 356 | uint16_t rx, tx; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 357 | uint16_t fb; |
Jakub Grajciar | e4bb5bc | 2017-11-02 14:33:27 +0100 | [diff] [blame^] | 358 | int i; /* rx buffer iterator */ |
| 359 | int j; /* tx bufferiterator */ |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 360 | |
Jakub Grajciar | e4bb5bc | 2017-11-02 14:33:27 +0100 | [diff] [blame^] | 361 | /* loop while there are packets in shm */ |
| 362 | do |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 363 | { |
Jakub Grajciar | e4bb5bc | 2017-11-02 14:33:27 +0100 | [diff] [blame^] | 364 | /* receive data from shared memory buffers */ |
| 365 | err = memif_rx_burst (c->conn, qid, c->rx_bufs, MAX_MEMIF_BUFS, &rx); |
| 366 | ret_val = err; |
| 367 | c->rx_counter += rx; |
| 368 | if ((err != MEMIF_ERR_SUCCESS) && (err != MEMIF_ERR_NOBUF)) |
| 369 | { |
| 370 | INFO ("memif_rx_burst: %s", memif_strerror (err)); |
| 371 | goto error; |
| 372 | } |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 373 | |
Jakub Grajciar | e4bb5bc | 2017-11-02 14:33:27 +0100 | [diff] [blame^] | 374 | i = 0; |
| 375 | |
| 376 | /* try to alloc required number of buffers buffers */ |
| 377 | err = memif_buffer_alloc (c->conn, qid, c->tx_bufs, rx, &tx, 0); |
| 378 | if ((err != MEMIF_ERR_SUCCESS) && (err != MEMIF_ERR_NOBUF_RING)) |
| 379 | { |
| 380 | INFO ("memif_buffer_alloc: %s", memif_strerror (err)); |
| 381 | goto error; |
| 382 | } |
| 383 | j = 0; |
| 384 | c->tx_err_counter = rx - tx; |
| 385 | |
| 386 | /* process bufers */ |
| 387 | while (tx) |
| 388 | { |
| 389 | while (tx > 2) |
| 390 | { |
| 391 | resolve_packet ((void *) (c->rx_bufs + i)->data, |
| 392 | (c->rx_bufs + i)->data_len, |
| 393 | (void *) (c->tx_bufs + j)->data, |
| 394 | &(c->tx_bufs + j)->data_len, c->ip_addr); |
| 395 | resolve_packet ((void *) (c->rx_bufs + i + 1)->data, |
| 396 | (c->rx_bufs + i + 1)->data_len, |
| 397 | (void *) (c->tx_bufs + j + 1)->data, |
| 398 | &(c->tx_bufs + j + 1)->data_len, c->ip_addr); |
| 399 | |
| 400 | i += 2; |
| 401 | j += 2; |
| 402 | tx -= 2; |
| 403 | } |
| 404 | resolve_packet ((void *) (c->rx_bufs + i)->data, |
| 405 | (c->rx_bufs + i)->data_len, |
| 406 | (void *) (c->tx_bufs + j)->data, |
| 407 | &(c->tx_bufs + j)->data_len, c->ip_addr); |
| 408 | i++; |
| 409 | j++; |
| 410 | tx--; |
| 411 | } |
| 412 | /* mark memif buffers and shared memory buffers as free */ |
| 413 | /* free processed buffers */ |
| 414 | err = memif_buffer_free (c->conn, qid, c->rx_bufs, rx, &fb); |
| 415 | if (err != MEMIF_ERR_SUCCESS) |
| 416 | INFO ("memif_buffer_free: %s", memif_strerror (err)); |
| 417 | rx -= fb; |
| 418 | |
| 419 | DBG ("freed %d buffers. %u/%u alloc/free buffers", |
| 420 | fb, rx, MAX_MEMIF_BUFS - rx); |
| 421 | |
| 422 | /* transmit allocated buffers */ |
| 423 | err = memif_tx_burst (c->conn, qid, c->tx_bufs, j, &tx); |
| 424 | if (err != MEMIF_ERR_SUCCESS) |
| 425 | { |
| 426 | INFO ("memif_tx_burst: %s", memif_strerror (err)); |
| 427 | goto error; |
| 428 | } |
| 429 | c->tx_counter += tx; |
| 430 | |
| 431 | } |
| 432 | while (ret_val == MEMIF_ERR_NOBUF); |
| 433 | |
| 434 | return 0; |
| 435 | |
| 436 | error: |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 437 | err = memif_buffer_free (c->conn, qid, c->rx_bufs, rx, &fb); |
| 438 | if (err != MEMIF_ERR_SUCCESS) |
| 439 | INFO ("memif_buffer_free: %s", memif_strerror (err)); |
| 440 | c->rx_buf_num -= fb; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 441 | DBG ("freed %d buffers. %u/%u alloc/free buffers", |
| 442 | fb, c->rx_buf_num, MAX_MEMIF_BUFS - c->rx_buf_num); |
Jakub Grajciar | e4bb5bc | 2017-11-02 14:33:27 +0100 | [diff] [blame^] | 443 | return 0; |
| 444 | } |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 445 | |
Jakub Grajciar | e4bb5bc | 2017-11-02 14:33:27 +0100 | [diff] [blame^] | 446 | /* called when event is polled on interrupt file descriptor. |
| 447 | there are packets in shared memory ready to be received */ |
| 448 | /* dev test modification: loop until TX == RX (don't drop buffers) */ |
| 449 | int |
| 450 | on_interrupt0 (memif_conn_handle_t conn, void *private_ctx, uint16_t qid) |
| 451 | { |
| 452 | long index = *((long *) private_ctx); |
| 453 | memif_connection_t *c = &memif_connection[index]; |
| 454 | if (c->index != index) |
| 455 | { |
| 456 | INFO ("invalid context: %ld/%u", index, c->index); |
| 457 | return 0; |
| 458 | } |
| 459 | |
| 460 | int err = MEMIF_ERR_SUCCESS, ret_val; |
| 461 | uint16_t rx, tx; |
| 462 | uint16_t fb; |
| 463 | int i; /* rx buffer iterator */ |
| 464 | int j; /* tx bufferiterator */ |
| 465 | int prev_i; /* first allocated rx buffer */ |
| 466 | |
| 467 | /* loop while there are packets in shm */ |
| 468 | do |
| 469 | { |
| 470 | /* receive data from shared memory buffers */ |
| 471 | err = memif_rx_burst (c->conn, qid, c->rx_bufs, MAX_MEMIF_BUFS, &rx); |
| 472 | ret_val = err; |
| 473 | c->rx_counter += rx; |
| 474 | if ((err != MEMIF_ERR_SUCCESS) && (err != MEMIF_ERR_NOBUF)) |
| 475 | { |
| 476 | INFO ("memif_rx_burst: %s", memif_strerror (err)); |
| 477 | goto error; |
| 478 | } |
| 479 | |
| 480 | prev_i = i = 0; |
| 481 | |
| 482 | /* loop while there are RX buffers to be processed */ |
| 483 | while (rx) |
| 484 | { |
| 485 | |
| 486 | /* try to alloc required number of buffers buffers */ |
| 487 | err = memif_buffer_alloc (c->conn, qid, c->tx_bufs, rx, &tx, 0); |
| 488 | if ((err != MEMIF_ERR_SUCCESS) && (err != MEMIF_ERR_NOBUF_RING)) |
| 489 | { |
| 490 | INFO ("memif_buffer_alloc: %s", memif_strerror (err)); |
| 491 | goto error; |
| 492 | } |
| 493 | j = 0; |
| 494 | |
| 495 | /* process bufers */ |
| 496 | while (tx) |
| 497 | { |
| 498 | while (tx > 2) |
| 499 | { |
| 500 | resolve_packet ((void *) (c->rx_bufs + i)->data, |
| 501 | (c->rx_bufs + i)->data_len, |
| 502 | (void *) (c->tx_bufs + j)->data, |
| 503 | &(c->tx_bufs + j)->data_len, c->ip_addr); |
| 504 | resolve_packet ((void *) (c->rx_bufs + i + 1)->data, |
| 505 | (c->rx_bufs + i + 1)->data_len, |
| 506 | (void *) (c->tx_bufs + j + 1)->data, |
| 507 | &(c->tx_bufs + j + 1)->data_len, |
| 508 | c->ip_addr); |
| 509 | |
| 510 | i += 2; |
| 511 | j += 2; |
| 512 | tx -= 2; |
| 513 | } |
| 514 | resolve_packet ((void *) (c->rx_bufs + i)->data, |
| 515 | (c->rx_bufs + i)->data_len, |
| 516 | (void *) (c->tx_bufs + j)->data, |
| 517 | &(c->tx_bufs + j)->data_len, c->ip_addr); |
| 518 | i++; |
| 519 | j++; |
| 520 | tx--; |
| 521 | } |
| 522 | /* mark memif buffers and shared memory buffers as free */ |
| 523 | /* free processed buffers */ |
| 524 | err = memif_buffer_free (c->conn, qid, c->rx_bufs + prev_i, j, &fb); |
| 525 | if (err != MEMIF_ERR_SUCCESS) |
| 526 | INFO ("memif_buffer_free: %s", memif_strerror (err)); |
| 527 | rx -= fb; |
| 528 | prev_i = i; |
| 529 | |
| 530 | DBG ("freed %d buffers. %u/%u alloc/free buffers", |
| 531 | fb, rx, MAX_MEMIF_BUFS - rx); |
| 532 | |
| 533 | /* transmit allocated buffers */ |
| 534 | err = memif_tx_burst (c->conn, qid, c->tx_bufs, j, &tx); |
| 535 | if (err != MEMIF_ERR_SUCCESS) |
| 536 | { |
| 537 | INFO ("memif_tx_burst: %s", memif_strerror (err)); |
| 538 | goto error; |
| 539 | } |
| 540 | c->tx_counter += tx; |
| 541 | |
| 542 | } |
| 543 | |
| 544 | } |
| 545 | while (ret_val == MEMIF_ERR_NOBUF); |
| 546 | |
| 547 | return 0; |
| 548 | |
| 549 | error: |
| 550 | err = memif_buffer_free (c->conn, qid, c->rx_bufs, rx, &fb); |
| 551 | if (err != MEMIF_ERR_SUCCESS) |
| 552 | INFO ("memif_buffer_free: %s", memif_strerror (err)); |
| 553 | c->rx_buf_num -= fb; |
| 554 | DBG ("freed %d buffers. %u/%u alloc/free buffers", |
| 555 | fb, c->rx_buf_num, MAX_MEMIF_BUFS - c->rx_buf_num); |
| 556 | return 0; |
| 557 | } |
| 558 | |
| 559 | /* called when event is polled on interrupt file descriptor. |
| 560 | there are packets in shared memory ready to be received */ |
| 561 | /* dev test modification: handle only ARP requests */ |
| 562 | int |
| 563 | on_interrupt1 (memif_conn_handle_t conn, void *private_ctx, uint16_t qid) |
| 564 | { |
| 565 | long index = *((long *) private_ctx); |
| 566 | memif_connection_t *c = &memif_connection[index]; |
| 567 | if (c->index != index) |
| 568 | { |
| 569 | INFO ("invalid context: %ld/%u", index, c->index); |
| 570 | return 0; |
| 571 | } |
| 572 | |
| 573 | int err = MEMIF_ERR_SUCCESS, ret_val; |
| 574 | int i; |
| 575 | uint16_t rx, tx; |
| 576 | uint16_t fb; |
| 577 | uint16_t pck_seq; |
| 578 | |
| 579 | do |
| 580 | { |
| 581 | /* receive data from shared memory buffers */ |
| 582 | err = memif_rx_burst (c->conn, qid, c->rx_bufs, MAX_MEMIF_BUFS, &rx); |
| 583 | ret_val = err; |
| 584 | if ((err != MEMIF_ERR_SUCCESS) && (err != MEMIF_ERR_NOBUF)) |
| 585 | { |
| 586 | INFO ("memif_rx_burst: %s", memif_strerror (err)); |
| 587 | goto error; |
| 588 | } |
| 589 | c->rx_buf_num += rx; |
| 590 | c->rx_counter += rx; |
| 591 | |
| 592 | for (i = 0; i < rx; i++) |
| 593 | { |
| 594 | if (((struct ether_header *) (c->rx_bufs + i)->data)->ether_type == |
| 595 | 0x0608) |
| 596 | { |
| 597 | if (icmpr_buffer_alloc (c->index, 1, &tx, i, qid) < 0) |
| 598 | break; |
| 599 | resolve_packet ((void *) (c->rx_bufs + i)->data, |
| 600 | (c->rx_bufs + i)->data_len, |
| 601 | (void *) (c->tx_bufs + i)->data, |
| 602 | &(c->tx_bufs + i)->data_len, c->ip_addr); |
| 603 | icmpr_tx_burst (c->index, qid); |
| 604 | } |
| 605 | } |
| 606 | |
| 607 | err = memif_buffer_free (c->conn, qid, c->rx_bufs, rx, &fb); |
| 608 | if (err != MEMIF_ERR_SUCCESS) |
| 609 | INFO ("memif_buffer_free: %s", memif_strerror (err)); |
| 610 | c->rx_buf_num -= fb; |
| 611 | |
| 612 | |
| 613 | } |
| 614 | while (ret_val == MEMIF_ERR_NOBUF); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 615 | |
| 616 | return 0; |
| 617 | |
| 618 | error: |
| 619 | err = memif_buffer_free (c->conn, qid, c->rx_bufs, rx, &fb); |
| 620 | if (err != MEMIF_ERR_SUCCESS) |
| 621 | INFO ("memif_buffer_free: %s", memif_strerror (err)); |
| 622 | c->rx_buf_num -= fb; |
| 623 | DBG ("freed %d buffers. %u/%u alloc/free buffers", |
| 624 | fb, c->rx_buf_num, MAX_MEMIF_BUFS - c->rx_buf_num); |
| 625 | return 0; |
| 626 | } |
| 627 | |
| 628 | int |
Jakub Grajciar | e4bb5bc | 2017-11-02 14:33:27 +0100 | [diff] [blame^] | 629 | icmpr_memif_create (long index, long mode, char *s) |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 630 | { |
| 631 | if (index >= MAX_CONNS) |
| 632 | { |
| 633 | INFO ("connection array overflow"); |
| 634 | return 0; |
| 635 | } |
| 636 | if (index < 0) |
| 637 | { |
| 638 | INFO ("don't even try..."); |
| 639 | return 0; |
| 640 | } |
| 641 | memif_connection_t *c = &memif_connection[index]; |
| 642 | |
| 643 | /* setting memif connection arguments */ |
| 644 | memif_conn_args_t args; |
| 645 | int fd = -1; |
| 646 | memset (&args, 0, sizeof (args)); |
| 647 | args.is_master = mode; |
Jakub Grajciar | e4bb5bc | 2017-11-02 14:33:27 +0100 | [diff] [blame^] | 648 | args.log2_ring_size = 11; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 649 | args.buffer_size = 2048; |
| 650 | args.num_s2m_rings = 2; |
| 651 | args.num_m2s_rings = 2; |
| 652 | strncpy ((char *) args.interface_name, IF_NAME, strlen (IF_NAME)); |
| 653 | strncpy ((char *) args.instance_name, APP_NAME, strlen (APP_NAME)); |
| 654 | args.mode = 0; |
| 655 | /* socket filename is not specified, because this app is supposed to |
| 656 | connect to VPP over memif. so default socket filename will be used */ |
| 657 | /* default socketfile = /run/vpp/memif.sock */ |
| 658 | |
| 659 | args.interface_id = index; |
| 660 | /* last argument for memif_create (void * private_ctx) is used by user |
| 661 | to identify connection. this context is returned with callbacks */ |
Jakub Grajciar | e4bb5bc | 2017-11-02 14:33:27 +0100 | [diff] [blame^] | 662 | int err; |
| 663 | /* default interrupt */ |
| 664 | if (s == NULL) |
| 665 | { |
| 666 | err = memif_create (&c->conn, |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 667 | &args, on_connect, on_disconnect, on_interrupt, |
| 668 | &ctx[index]); |
Jakub Grajciar | e4bb5bc | 2017-11-02 14:33:27 +0100 | [diff] [blame^] | 669 | if (err != MEMIF_ERR_SUCCESS) |
| 670 | { |
| 671 | INFO ("memif_create: %s", memif_strerror (err)); |
| 672 | return 0; |
| 673 | } |
| 674 | } |
| 675 | else |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 676 | { |
Jakub Grajciar | e4bb5bc | 2017-11-02 14:33:27 +0100 | [diff] [blame^] | 677 | if (strncmp (s, "0", 1) == 0) |
| 678 | { |
| 679 | err = memif_create (&c->conn, |
| 680 | &args, on_connect, on_disconnect, on_interrupt0, |
| 681 | &ctx[index]); |
| 682 | if (err != MEMIF_ERR_SUCCESS) |
| 683 | { |
| 684 | INFO ("memif_create: %s", memif_strerror (err)); |
| 685 | return 0; |
| 686 | } |
| 687 | } |
| 688 | else if (strncmp (s, "1", 1) == 0) |
| 689 | { |
| 690 | err = memif_create (&c->conn, |
| 691 | &args, on_connect, on_disconnect, on_interrupt1, |
| 692 | &ctx[index]); |
| 693 | if (err != MEMIF_ERR_SUCCESS) |
| 694 | { |
| 695 | INFO ("memif_create: %s", memif_strerror (err)); |
| 696 | return 0; |
| 697 | } |
| 698 | } |
| 699 | else |
| 700 | { |
| 701 | INFO ("Unknown interrupt descriptor"); |
| 702 | goto done; |
| 703 | } |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 704 | } |
| 705 | |
| 706 | c->index = index; |
| 707 | /* alloc memif buffers */ |
| 708 | c->rx_buf_num = 0; |
| 709 | c->rx_bufs = |
| 710 | (memif_buffer_t *) malloc (sizeof (memif_buffer_t) * MAX_MEMIF_BUFS); |
| 711 | c->tx_buf_num = 0; |
| 712 | c->tx_bufs = |
| 713 | (memif_buffer_t *) malloc (sizeof (memif_buffer_t) * MAX_MEMIF_BUFS); |
| 714 | |
| 715 | c->ip_addr[0] = 192; |
| 716 | c->ip_addr[1] = 168; |
| 717 | c->ip_addr[2] = c->index + 1; |
| 718 | c->ip_addr[3] = 2; |
Jakub Grajciar | e4bb5bc | 2017-11-02 14:33:27 +0100 | [diff] [blame^] | 719 | |
| 720 | c->seq = c->tx_err_counter = c->tx_counter = c->rx_counter = 0; |
| 721 | |
| 722 | done: |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 723 | return 0; |
| 724 | } |
| 725 | |
| 726 | int |
| 727 | icmpr_memif_delete (long index) |
| 728 | { |
| 729 | if (index >= MAX_CONNS) |
| 730 | { |
| 731 | INFO ("connection array overflow"); |
| 732 | return 0; |
| 733 | } |
| 734 | if (index < 0) |
| 735 | { |
| 736 | INFO ("don't even try..."); |
| 737 | return 0; |
| 738 | } |
| 739 | memif_connection_t *c = &memif_connection[index]; |
| 740 | |
| 741 | if (c->rx_bufs) |
| 742 | free (c->rx_bufs); |
| 743 | c->rx_bufs = NULL; |
| 744 | c->rx_buf_num = 0; |
| 745 | if (c->tx_bufs) |
| 746 | free (c->tx_bufs); |
| 747 | c->tx_bufs = NULL; |
| 748 | c->tx_buf_num = 0; |
| 749 | |
| 750 | int err; |
| 751 | /* disconenct then delete memif connection */ |
| 752 | err = memif_delete (&c->conn); |
| 753 | if (err != MEMIF_ERR_SUCCESS) |
| 754 | INFO ("memif_delete: %s", memif_strerror (err)); |
| 755 | if (c->conn != NULL) |
| 756 | INFO ("memif delete fail"); |
| 757 | return 0; |
| 758 | } |
| 759 | |
| 760 | void |
| 761 | print_help () |
| 762 | { |
| 763 | printf ("LIBMEMIF EXAMPLE APP: %s", APP_NAME); |
| 764 | #ifdef ICMP_DBG |
| 765 | printf (" (debug)"); |
| 766 | #endif |
| 767 | printf ("\n"); |
| 768 | printf ("==============================\n"); |
| 769 | printf ("libmemif version: %s", LIBMEMIF_VERSION); |
| 770 | #ifdef MEMIF_DBG |
| 771 | printf (" (debug)"); |
| 772 | #endif |
| 773 | printf ("\n"); |
| 774 | printf ("memif version: %d\n", MEMIF_VERSION); |
| 775 | printf ("commands:\n"); |
| 776 | printf ("\thelp - prints this help\n"); |
| 777 | printf ("\texit - exit app\n"); |
| 778 | printf |
Jakub Grajciar | e4bb5bc | 2017-11-02 14:33:27 +0100 | [diff] [blame^] | 779 | ("\tconn <index> <mode> [<interrupt-desc>] - create memif. index is also used as interface id, mode 0 = slave 1 = master, interrupt-desc none = default 0 = if ring is full wait 1 = handle only ARP requests\n"); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 780 | printf ("\tdel <index> - delete memif\n"); |
| 781 | printf ("\tshow - show connection details\n"); |
| 782 | printf ("\tip-set <index> <ip-addr> - set interface ip address\n"); |
| 783 | printf |
| 784 | ("\trx-mode <index> <qid> <polling|interrupt> - set queue rx mode\n"); |
Jakub Grajciar | e4bb5bc | 2017-11-02 14:33:27 +0100 | [diff] [blame^] | 785 | printf ("\tsh-count - print counters\n"); |
| 786 | printf ("\tcl-count - clear counters\n"); |
| 787 | printf ("\tsend <index> <tx> <ip> <mac> - send icmp\n"); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 788 | } |
| 789 | |
| 790 | int |
| 791 | icmpr_free () |
| 792 | { |
| 793 | /* application cleanup */ |
| 794 | int err; |
| 795 | long i; |
Jakub Grajciar | e4bb5bc | 2017-11-02 14:33:27 +0100 | [diff] [blame^] | 796 | if (out_fd > 0) |
| 797 | close (out_fd); |
| 798 | out_fd = -1; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 799 | for (i = 0; i < MAX_CONNS; i++) |
| 800 | { |
| 801 | memif_connection_t *c = &memif_connection[i]; |
| 802 | if (c->conn) |
| 803 | icmpr_memif_delete (i); |
| 804 | } |
| 805 | |
| 806 | err = memif_cleanup (); |
| 807 | if (err != MEMIF_ERR_SUCCESS) |
| 808 | INFO ("memif_delete: %s", memif_strerror (err)); |
| 809 | |
| 810 | return 0; |
| 811 | } |
| 812 | |
| 813 | int |
| 814 | icmpr_set_ip (long index, char *ip) |
| 815 | { |
| 816 | if (index >= MAX_CONNS) |
| 817 | { |
| 818 | INFO ("connection array overflow"); |
| 819 | return 0; |
| 820 | } |
| 821 | if (index < 0) |
| 822 | { |
| 823 | INFO ("don't even try..."); |
| 824 | return 0; |
| 825 | } |
| 826 | memif_connection_t *c = &memif_connection[index]; |
| 827 | if (c->conn == NULL) |
| 828 | { |
| 829 | INFO ("no connection at index %ld", index); |
| 830 | return 0; |
| 831 | } |
| 832 | |
| 833 | char *end; |
| 834 | char *ui; |
| 835 | uint8_t tmp[4]; |
| 836 | ui = strtok (ip, "."); |
| 837 | if (ui == NULL) |
| 838 | goto error; |
| 839 | tmp[0] = strtol (ui, &end, 10); |
| 840 | |
| 841 | ui = strtok (NULL, "."); |
| 842 | if (ui == NULL) |
| 843 | goto error; |
| 844 | tmp[1] = strtol (ui, &end, 10); |
| 845 | |
| 846 | ui = strtok (NULL, "."); |
| 847 | if (ui == NULL) |
| 848 | goto error; |
| 849 | tmp[2] = strtol (ui, &end, 10); |
| 850 | |
| 851 | ui = strtok (NULL, "."); |
| 852 | if (ui == NULL) |
| 853 | goto error; |
| 854 | tmp[3] = strtol (ui, &end, 10); |
| 855 | |
| 856 | c->ip_addr[0] = tmp[0]; |
| 857 | c->ip_addr[1] = tmp[1]; |
| 858 | c->ip_addr[2] = tmp[2]; |
| 859 | c->ip_addr[3] = tmp[3]; |
| 860 | |
| 861 | INFO ("memif %ld ip address set to %u.%u.%u.%u", |
| 862 | index, c->ip_addr[0], c->ip_addr[1], c->ip_addr[2], c->ip_addr[3]); |
| 863 | |
| 864 | return 0; |
| 865 | |
| 866 | error: |
| 867 | INFO ("invalid ip address"); |
| 868 | return 0; |
| 869 | } |
| 870 | |
| 871 | int |
| 872 | icmpr_set_rx_mode (long index, long qid, char *mode) |
| 873 | { |
| 874 | if (index >= MAX_CONNS) |
| 875 | { |
| 876 | INFO ("connection array overflow"); |
| 877 | return 0; |
| 878 | } |
| 879 | if (index < 0) |
| 880 | { |
| 881 | INFO ("don't even try..."); |
| 882 | return 0; |
| 883 | } |
| 884 | memif_connection_t *c = &memif_connection[index]; |
| 885 | |
| 886 | if (c->conn == NULL) |
| 887 | { |
| 888 | INFO ("no connection at index %ld", index); |
| 889 | return 0; |
| 890 | } |
| 891 | |
| 892 | if (strncmp (mode, "interrupt", 9) == 0) |
| 893 | { |
| 894 | memif_set_rx_mode (c->conn, MEMIF_RX_MODE_INTERRUPT, qid); |
| 895 | } |
| 896 | |
| 897 | else if (strncmp (mode, "polling", 7) == 0) |
| 898 | { |
| 899 | memif_set_rx_mode (c->conn, MEMIF_RX_MODE_POLLING, qid); |
| 900 | } |
| 901 | else |
| 902 | INFO ("expected rx mode <interrupt|polling>"); |
| 903 | return 0; |
| 904 | } |
| 905 | |
Jakub Grajciar | e4bb5bc | 2017-11-02 14:33:27 +0100 | [diff] [blame^] | 906 | void |
| 907 | icmpr_print_counters () |
| 908 | { |
| 909 | int i; |
| 910 | for (i = 0; i < MAX_CONNS; i++) |
| 911 | { |
| 912 | memif_connection_t *c = &memif_connection[i]; |
| 913 | if (c->conn == NULL) |
| 914 | continue; |
| 915 | printf ("===============================\n"); |
| 916 | printf ("interface index: %d\n", c->index); |
| 917 | printf ("\trx: %lu\n", c->rx_counter); |
| 918 | printf ("\ttx: %lu\n", c->tx_counter); |
| 919 | printf ("\ttx_err: %lu\n", c->tx_err_counter); |
| 920 | printf ("\tts: %lus %luns\n", c->t_sec, c->t_nsec); |
| 921 | } |
| 922 | } |
| 923 | |
| 924 | void |
| 925 | icmpr_reset_counters () |
| 926 | { |
| 927 | int i; |
| 928 | for (i = 0; i < MAX_CONNS; i++) |
| 929 | { |
| 930 | memif_connection_t *c = &memif_connection[i]; |
| 931 | if (c->conn == NULL) |
| 932 | continue; |
| 933 | c->t_sec = c->t_nsec = c->tx_err_counter = c->tx_counter = |
| 934 | c->rx_counter = 0; |
| 935 | } |
| 936 | } |
| 937 | |
| 938 | void * |
| 939 | icmpr_send_proc (void *data) |
| 940 | { |
| 941 | icmpr_thread_data_t *d = (icmpr_thread_data_t *) data; |
| 942 | int index = d->index; |
| 943 | uint64_t count = d->packet_num; |
| 944 | memif_connection_t *c = &memif_connection[index]; |
| 945 | if (c->conn == NULL) |
| 946 | { |
| 947 | INFO ("No connection at index %d. Thread exiting...\n", index); |
| 948 | pthread_exit (NULL); |
| 949 | } |
| 950 | uint16_t tx, i; |
| 951 | int err = MEMIF_ERR_SUCCESS; |
| 952 | uint32_t seq = 0; |
| 953 | struct timespec start, end; |
| 954 | memset (&start, 0, sizeof (start)); |
| 955 | memset (&end, 0, sizeof (end)); |
| 956 | |
| 957 | timespec_get (&start, TIME_UTC); |
| 958 | while (count) |
| 959 | { |
| 960 | i = 0; |
| 961 | err = memif_buffer_alloc (c->conn, 0, c->tx_bufs, |
| 962 | MAX_MEMIF_BUFS > |
| 963 | count ? count : MAX_MEMIF_BUFS, &tx, 0); |
| 964 | if ((err != MEMIF_ERR_SUCCESS) && (err != MEMIF_ERR_NOBUF_RING)) |
| 965 | { |
| 966 | INFO ("memif_buffer_alloc: %s Thread exiting...\n", |
| 967 | memif_strerror (err)); |
| 968 | pthread_exit (NULL); |
| 969 | } |
| 970 | c->tx_buf_num += tx; |
| 971 | |
| 972 | while (tx) |
| 973 | { |
| 974 | while (tx > 2) |
| 975 | { |
| 976 | generate_packet ((void *) c->tx_bufs[i].data, |
| 977 | &c->tx_bufs[i].data_len, c->ip_addr, |
| 978 | d->ip_daddr, d->hw_daddr, seq++); |
| 979 | generate_packet ((void *) c->tx_bufs[i + 1].data, |
| 980 | &c->tx_bufs[i + 1].data_len, c->ip_addr, |
| 981 | d->ip_daddr, d->hw_daddr, seq++); |
| 982 | i += 2; |
| 983 | tx -= 2; |
| 984 | } |
| 985 | generate_packet ((void *) c->tx_bufs[i].data, |
| 986 | &c->tx_bufs[i].data_len, c->ip_addr, |
| 987 | d->ip_daddr, d->hw_daddr, seq++); |
| 988 | i++; |
| 989 | tx--; |
| 990 | } |
| 991 | err = memif_tx_burst (c->conn, 0, c->tx_bufs, c->tx_buf_num, &tx); |
| 992 | if (err != MEMIF_ERR_SUCCESS) |
| 993 | { |
| 994 | INFO ("memif_tx_burst: %s Thread exiting...\n", |
| 995 | memif_strerror (err)); |
| 996 | pthread_exit (NULL); |
| 997 | } |
| 998 | c->tx_buf_num -= tx; |
| 999 | c->tx_counter += tx; |
| 1000 | count -= tx; |
| 1001 | } |
| 1002 | timespec_get (&end, TIME_UTC); |
| 1003 | INFO ("Pakcet sequence finished!"); |
| 1004 | INFO ("Seq len: %u", seq); |
| 1005 | uint64_t t1 = end.tv_sec - start.tv_sec; |
| 1006 | uint64_t t2; |
| 1007 | if (end.tv_nsec > start.tv_nsec) |
| 1008 | { |
| 1009 | t2 = end.tv_nsec - start.tv_nsec; |
| 1010 | } |
| 1011 | else |
| 1012 | { |
| 1013 | t2 = start.tv_nsec - end.tv_nsec; |
| 1014 | t1--; |
| 1015 | } |
| 1016 | c->t_sec = t1; |
| 1017 | c->t_nsec = t2; |
| 1018 | INFO ("Seq time: %lus %luns", t1, t2); |
| 1019 | double tmp = t1; |
| 1020 | tmp += t2 / 1e+9; |
| 1021 | tmp = seq / tmp; |
| 1022 | INFO ("Average pps: %f", tmp); |
| 1023 | INFO ("Thread exiting..."); |
| 1024 | pthread_exit (NULL); |
| 1025 | } |
| 1026 | |
| 1027 | int |
| 1028 | icmpr_send (long index, long packet_num, char *hw, char *ip) |
| 1029 | { |
| 1030 | memif_connection_t *c = &memif_connection[index]; |
| 1031 | char *end; |
| 1032 | char *ui; |
| 1033 | uint8_t tmp[6]; |
| 1034 | if (c->conn == NULL) |
| 1035 | return -1; |
| 1036 | c->seq = 0; |
| 1037 | icmpr_thread_data_t *data = &icmpr_thread_data[index]; |
| 1038 | data->index = index; |
| 1039 | data->packet_num = packet_num; |
| 1040 | INFO ("PN: %lu", data->packet_num); |
| 1041 | printf ("%s\n", ip); |
| 1042 | printf ("%s\n", hw); |
| 1043 | |
| 1044 | ui = strtok (ip, "."); |
| 1045 | if (ui == NULL) |
| 1046 | goto error; |
| 1047 | tmp[0] = strtol (ui, &end, 10); |
| 1048 | |
| 1049 | ui = strtok (NULL, "."); |
| 1050 | if (ui == NULL) |
| 1051 | goto error; |
| 1052 | tmp[1] = strtol (ui, &end, 10); |
| 1053 | |
| 1054 | ui = strtok (NULL, "."); |
| 1055 | if (ui == NULL) |
| 1056 | goto error; |
| 1057 | tmp[2] = strtol (ui, &end, 10); |
| 1058 | |
| 1059 | ui = strtok (NULL, "."); |
| 1060 | if (ui == NULL) |
| 1061 | goto error; |
| 1062 | tmp[3] = strtol (ui, &end, 10); |
| 1063 | |
| 1064 | data->ip_daddr[0] = tmp[0]; |
| 1065 | data->ip_daddr[1] = tmp[1]; |
| 1066 | data->ip_daddr[2] = tmp[2]; |
| 1067 | data->ip_daddr[3] = tmp[3]; |
| 1068 | |
| 1069 | ui = strtok (hw, ":"); |
| 1070 | if (ui == NULL) |
| 1071 | goto error; |
| 1072 | tmp[0] = strtol (ui, &end, 16); |
| 1073 | ui = strtok (NULL, ":"); |
| 1074 | if (ui == NULL) |
| 1075 | goto error; |
| 1076 | tmp[1] = strtol (ui, &end, 16); |
| 1077 | ui = strtok (NULL, ":"); |
| 1078 | if (ui == NULL) |
| 1079 | goto error; |
| 1080 | tmp[2] = strtol (ui, &end, 16); |
| 1081 | ui = strtok (NULL, ":"); |
| 1082 | if (ui == NULL) |
| 1083 | goto error; |
| 1084 | tmp[3] = strtol (ui, &end, 16); |
| 1085 | ui = strtok (NULL, ":"); |
| 1086 | if (ui == NULL) |
| 1087 | goto error; |
| 1088 | tmp[4] = strtol (ui, &end, 16); |
| 1089 | ui = strtok (NULL, ":"); |
| 1090 | if (ui == NULL) |
| 1091 | goto error; |
| 1092 | tmp[5] = strtol (ui, &end, 16); |
| 1093 | |
| 1094 | data->hw_daddr[0] = tmp[0]; |
| 1095 | data->hw_daddr[1] = tmp[1]; |
| 1096 | data->hw_daddr[2] = tmp[2]; |
| 1097 | data->hw_daddr[3] = tmp[3]; |
| 1098 | data->hw_daddr[4] = tmp[4]; |
| 1099 | data->hw_daddr[5] = tmp[5]; |
| 1100 | |
| 1101 | pthread_create (&thread[index], NULL, icmpr_send_proc, (void *) data); |
| 1102 | return 0; |
| 1103 | |
| 1104 | error: |
| 1105 | INFO ("Invalid input\n"); |
| 1106 | return 0; |
| 1107 | } |
| 1108 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1109 | int |
| 1110 | user_input_handler () |
| 1111 | { |
| 1112 | int i; |
| 1113 | char *in = (char *) malloc (256); |
| 1114 | char *ui = fgets (in, 256, stdin); |
| 1115 | char *end; |
| 1116 | long a; |
| 1117 | if (in[0] == '\n') |
| 1118 | goto done; |
| 1119 | ui = strtok (in, " "); |
| 1120 | if (strncmp (ui, "exit", 4) == 0) |
| 1121 | { |
| 1122 | free (in); |
| 1123 | icmpr_free (); |
| 1124 | exit (EXIT_SUCCESS); |
| 1125 | } |
| 1126 | else if (strncmp (ui, "help", 4) == 0) |
| 1127 | { |
| 1128 | print_help (); |
| 1129 | goto done; |
| 1130 | } |
| 1131 | else if (strncmp (ui, "conn", 4) == 0) |
| 1132 | { |
| 1133 | ui = strtok (NULL, " "); |
| 1134 | if (ui != NULL) |
| 1135 | a = strtol (ui, &end, 10); |
| 1136 | else |
| 1137 | { |
| 1138 | INFO ("expected id"); |
| 1139 | goto done; |
| 1140 | } |
| 1141 | ui = strtok (NULL, " "); |
| 1142 | if (ui != NULL) |
Jakub Grajciar | e4bb5bc | 2017-11-02 14:33:27 +0100 | [diff] [blame^] | 1143 | icmpr_memif_create (a, strtol (ui, &end, 10), strtok (NULL, " ")); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1144 | else |
| 1145 | INFO ("expected mode <0|1>"); |
| 1146 | goto done; |
| 1147 | } |
| 1148 | else if (strncmp (ui, "del", 3) == 0) |
| 1149 | { |
| 1150 | ui = strtok (NULL, " "); |
| 1151 | if (ui != NULL) |
| 1152 | icmpr_memif_delete (strtol (ui, &end, 10)); |
| 1153 | else |
| 1154 | INFO ("expected id"); |
| 1155 | goto done; |
| 1156 | } |
| 1157 | else if (strncmp (ui, "show", 4) == 0) |
| 1158 | { |
| 1159 | print_memif_details (); |
| 1160 | goto done; |
| 1161 | } |
| 1162 | else if (strncmp (ui, "ip-set", 6) == 0) |
| 1163 | { |
| 1164 | ui = strtok (NULL, " "); |
| 1165 | if (ui != NULL) |
| 1166 | icmpr_set_ip (strtol (ui, &end, 10), strtok (NULL, " ")); |
| 1167 | else |
| 1168 | INFO ("expected id"); |
| 1169 | goto done; |
| 1170 | } |
| 1171 | else if (strncmp (ui, "rx-mode", 7) == 0) |
| 1172 | { |
| 1173 | ui = strtok (NULL, " "); |
| 1174 | if (ui != NULL) |
| 1175 | a = strtol (ui, &end, 10); |
| 1176 | else |
| 1177 | { |
| 1178 | INFO ("expected id"); |
| 1179 | goto done; |
| 1180 | } |
| 1181 | ui = strtok (NULL, " "); |
| 1182 | if (ui != NULL) |
| 1183 | icmpr_set_rx_mode (a, strtol (ui, &end, 10), strtok (NULL, " ")); |
| 1184 | else |
| 1185 | INFO ("expected qid"); |
| 1186 | goto done; |
| 1187 | } |
Jakub Grajciar | e4bb5bc | 2017-11-02 14:33:27 +0100 | [diff] [blame^] | 1188 | else if (strncmp (ui, "sh-count", 8) == 0) |
| 1189 | { |
| 1190 | icmpr_print_counters (); |
| 1191 | } |
| 1192 | else if (strncmp (ui, "cl-count", 8) == 0) |
| 1193 | { |
| 1194 | icmpr_reset_counters (); |
| 1195 | } |
| 1196 | else if (strncmp (ui, "send", 4) == 0) |
| 1197 | { |
| 1198 | ui = strtok (NULL, " "); |
| 1199 | if (ui != NULL) |
| 1200 | a = strtol (ui, &end, 10); |
| 1201 | else |
| 1202 | { |
| 1203 | INFO ("expected id"); |
| 1204 | goto done; |
| 1205 | } |
| 1206 | ui = strtok (NULL, " "); |
| 1207 | if (ui != NULL) |
| 1208 | icmpr_send (a, strtol (ui, &end, 10), strtok (NULL, " "), |
| 1209 | strtok (NULL, " ")); |
| 1210 | else |
| 1211 | INFO ("expected count"); |
| 1212 | goto done; |
| 1213 | } |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1214 | else |
| 1215 | { |
| 1216 | DBG ("unknown command: %s", ui); |
| 1217 | goto done; |
| 1218 | } |
| 1219 | |
| 1220 | return 0; |
| 1221 | done: |
| 1222 | free (in); |
| 1223 | return 0; |
| 1224 | } |
| 1225 | |
| 1226 | int |
| 1227 | poll_event (int timeout) |
| 1228 | { |
| 1229 | struct epoll_event evt, *e; |
| 1230 | int app_err = 0, memif_err = 0, en = 0; |
| 1231 | int tmp, nfd; |
| 1232 | uint32_t events = 0; |
Jakub Grajciar | e4bb5bc | 2017-11-02 14:33:27 +0100 | [diff] [blame^] | 1233 | struct timespec start, end; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1234 | memset (&evt, 0, sizeof (evt)); |
| 1235 | evt.events = EPOLLIN | EPOLLOUT; |
| 1236 | sigset_t sigset; |
| 1237 | sigemptyset (&sigset); |
| 1238 | en = epoll_pwait (epfd, &evt, 1, timeout, &sigset); |
Jakub Grajciar | e4bb5bc | 2017-11-02 14:33:27 +0100 | [diff] [blame^] | 1239 | /* id event polled */ |
| 1240 | timespec_get (&start, TIME_UTC); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1241 | if (en < 0) |
| 1242 | { |
| 1243 | DBG ("epoll_pwait: %s", strerror (errno)); |
| 1244 | return -1; |
| 1245 | } |
| 1246 | if (en > 0) |
| 1247 | { |
| 1248 | /* this app does not use any other file descriptors than stds and memif control fds */ |
| 1249 | if (evt.data.fd > 2) |
| 1250 | { |
| 1251 | /* event of memif control fd */ |
| 1252 | /* convert epolle events to memif events */ |
| 1253 | if (evt.events & EPOLLIN) |
| 1254 | events |= MEMIF_FD_EVENT_READ; |
| 1255 | if (evt.events & EPOLLOUT) |
| 1256 | events |= MEMIF_FD_EVENT_WRITE; |
| 1257 | if (evt.events & EPOLLERR) |
| 1258 | events |= MEMIF_FD_EVENT_ERROR; |
| 1259 | memif_err = memif_control_fd_handler (evt.data.fd, events); |
| 1260 | if (memif_err != MEMIF_ERR_SUCCESS) |
| 1261 | INFO ("memif_control_fd_handler: %s", memif_strerror (memif_err)); |
| 1262 | } |
| 1263 | else if (evt.data.fd == 0) |
| 1264 | { |
| 1265 | app_err = user_input_handler (); |
| 1266 | } |
| 1267 | else |
| 1268 | { |
| 1269 | DBG ("unexpected event at memif_epfd. fd %d", evt.data.fd); |
| 1270 | } |
| 1271 | } |
| 1272 | |
Jakub Grajciar | e4bb5bc | 2017-11-02 14:33:27 +0100 | [diff] [blame^] | 1273 | timespec_get (&end, TIME_UTC); |
| 1274 | LOG ("interrupt: %ld", end.tv_nsec - start.tv_nsec); |
| 1275 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1276 | if ((app_err < 0) || (memif_err < 0)) |
| 1277 | { |
| 1278 | if (app_err < 0) |
| 1279 | DBG ("user input handler error"); |
| 1280 | if (memif_err < 0) |
| 1281 | DBG ("memif control fd handler error"); |
| 1282 | return -1; |
| 1283 | } |
| 1284 | |
| 1285 | return 0; |
| 1286 | } |
| 1287 | |
| 1288 | int |
| 1289 | main () |
| 1290 | { |
| 1291 | epfd = epoll_create (1); |
| 1292 | add_epoll_fd (0, EPOLLIN); |
| 1293 | |
Jakub Grajciar | e4bb5bc | 2017-11-02 14:33:27 +0100 | [diff] [blame^] | 1294 | #ifdef LOG_FILE |
| 1295 | remove (LOG_FILE); |
| 1296 | enable_log = 0; |
| 1297 | |
| 1298 | out_fd = open (LOG_FILE, O_WRONLY | O_CREAT, S_IRWXO); |
| 1299 | if (out_fd < 0) |
| 1300 | INFO ("Error opening log file: %s", strerror (errno)); |
| 1301 | #endif /* LOG_FILE */ |
| 1302 | |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 1303 | /* initialize memory interface */ |
| 1304 | int err, i; |
| 1305 | /* if valid callback is passed as argument, fd event polling will be done by user |
| 1306 | all file descriptors and events will be passed to user in this callback */ |
| 1307 | /* if callback is set to NULL libmemif will handle fd event polling */ |
| 1308 | err = memif_init (control_fd_update, APP_NAME); |
| 1309 | if (err != MEMIF_ERR_SUCCESS) |
| 1310 | INFO ("memif_init: %s", memif_strerror (err)); |
| 1311 | |
| 1312 | for (i = 0; i < MAX_CONNS; i++) |
| 1313 | { |
| 1314 | memif_connection[i].conn = NULL; |
| 1315 | ctx[i] = i; |
| 1316 | } |
| 1317 | |
| 1318 | print_help (); |
| 1319 | |
| 1320 | /* main loop */ |
| 1321 | while (1) |
| 1322 | { |
| 1323 | if (poll_event (-1) < 0) |
| 1324 | { |
| 1325 | DBG ("poll_event error!"); |
| 1326 | } |
| 1327 | } |
| 1328 | } |