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