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> |
| 46 | #include <pthread.h> |
| 47 | |
| 48 | #include <libmemif.h> |
| 49 | #include <icmp_proto.h> |
| 50 | |
| 51 | #define APP_NAME "ICMP_Responder" |
| 52 | #define IF_NAME "memif_connection" |
| 53 | |
| 54 | |
| 55 | #ifdef ICMP_DBG |
| 56 | #define DBG(...) do { \ |
| 57 | printf (APP_NAME":%s:%d: ", __func__, __LINE__); \ |
| 58 | printf (__VA_ARGS__); \ |
| 59 | printf ("\n"); \ |
| 60 | } while (0) |
| 61 | #else |
| 62 | #define DBG(...) |
| 63 | #endif |
| 64 | |
| 65 | #define INFO(...) do { \ |
| 66 | printf ("INFO: "__VA_ARGS__); \ |
| 67 | printf ("\n"); \ |
| 68 | } while (0) |
| 69 | |
| 70 | /* maximum tx/rx memif buffers */ |
| 71 | #define MAX_MEMIF_BUFS 256 |
| 72 | #define MAX_CONNS 50 |
| 73 | #define MAX_QUEUES 2 |
| 74 | #define MAX_THREADS ((MAX_CONNS) * (MAX_QUEUES)) |
| 75 | |
| 76 | int main_epfd; |
| 77 | |
| 78 | typedef struct |
| 79 | { |
| 80 | /* thread id */ |
| 81 | uint8_t id; |
| 82 | /* memif connection index */ |
| 83 | uint16_t index; |
| 84 | /* id of queue to be handled by thread */ |
| 85 | uint8_t qid; |
| 86 | uint8_t isRunning; |
| 87 | |
| 88 | uint16_t rx_buf_num; |
| 89 | uint16_t tx_buf_num; |
| 90 | memif_buffer_t *rx_bufs; |
| 91 | memif_buffer_t *tx_bufs; |
| 92 | } memif_thread_data_t; |
| 93 | |
| 94 | typedef struct |
| 95 | { |
| 96 | uint16_t index; |
| 97 | /* memif conenction handle */ |
| 98 | memif_conn_handle_t conn; |
| 99 | /* interface ip address */ |
| 100 | uint8_t ip_addr[4]; |
| 101 | /* inform pthread about connection termination */ |
| 102 | uint8_t pending_del; |
| 103 | } memif_connection_t; |
| 104 | |
| 105 | memif_connection_t memif_connection[MAX_CONNS]; |
| 106 | long ctx[MAX_CONNS]; |
| 107 | |
| 108 | /* thread data specific for each thread */ |
| 109 | memif_thread_data_t thread_data[MAX_THREADS]; |
| 110 | pthread_t thread[MAX_THREADS]; |
| 111 | |
| 112 | void |
| 113 | user_signal_handler (int sig) |
| 114 | { |
| 115 | sig = sig; |
| 116 | } |
| 117 | |
| 118 | static void |
| 119 | print_memif_details () |
| 120 | { |
| 121 | memif_details_t md; |
| 122 | ssize_t buflen; |
| 123 | char *buf; |
| 124 | int err, i, e, ti; |
| 125 | buflen = 2048; |
| 126 | buf = malloc (buflen); |
| 127 | printf ("MEMIF DETAILS\n"); |
| 128 | printf ("==============================\n"); |
| 129 | for (i = 0; i < MAX_CONNS; i++) |
| 130 | { |
| 131 | memif_connection_t *c = &memif_connection[i]; |
| 132 | |
| 133 | memset (&md, 0, sizeof (md)); |
| 134 | memset (buf, 0, buflen); |
| 135 | |
| 136 | err = memif_get_details (c->conn, &md, buf, buflen); |
| 137 | if (err != MEMIF_ERR_SUCCESS) |
| 138 | { |
| 139 | if (err != MEMIF_ERR_NOCONN) |
| 140 | INFO ("%s", memif_strerror (err)); |
| 141 | continue; |
| 142 | } |
| 143 | |
| 144 | printf ("interface index: %d\n", i); |
| 145 | |
| 146 | printf ("\tinterface ip: %u.%u.%u.%u\n", |
| 147 | c->ip_addr[0], c->ip_addr[1], c->ip_addr[2], c->ip_addr[3]); |
| 148 | printf ("\tinterface name: %s\n", (char *) md.if_name); |
| 149 | printf ("\tapp name: %s\n", (char *) md.inst_name); |
| 150 | printf ("\tremote interface name: %s\n", (char *) md.remote_if_name); |
| 151 | printf ("\tremote app name: %s\n", (char *) md.remote_inst_name); |
| 152 | printf ("\tid: %u\n", md.id); |
| 153 | printf ("\tsecret: %s\n", (char *) md.secret); |
| 154 | printf ("\trole: "); |
| 155 | if (md.role) |
| 156 | printf ("slave\n"); |
| 157 | else |
| 158 | printf ("master\n"); |
| 159 | printf ("\tmode: "); |
| 160 | switch (md.mode) |
| 161 | { |
| 162 | case 0: |
| 163 | printf ("ethernet\n"); |
| 164 | break; |
| 165 | case 1: |
| 166 | printf ("ip\n"); |
| 167 | break; |
| 168 | case 2: |
| 169 | printf ("punt/inject\n"); |
| 170 | break; |
| 171 | default: |
| 172 | printf ("unknown\n"); |
| 173 | break; |
| 174 | } |
| 175 | printf ("\tsocket filename: %s\n", (char *) md.socket_filename); |
| 176 | printf ("\trx queues:\n"); |
| 177 | for (e = 0; e < md.rx_queues_num; e++) |
| 178 | { |
| 179 | ti = (i * MAX_QUEUES) + e; |
| 180 | printf ("\tqueue id: %u\n", md.rx_queues[e].qid); |
| 181 | printf ("\t\tring size: %u\n", md.rx_queues[e].ring_size); |
| 182 | printf ("\t\tbuffer size: %u\n", md.rx_queues[e].buffer_size); |
| 183 | printf ("\t\tthread id: %u\n", thread_data[ti].id); |
| 184 | printf ("\t\tthread connection index: %u\n", thread_data[ti].index); |
| 185 | printf ("\t\tthread running: "); |
| 186 | if (thread_data[ti].isRunning) |
| 187 | printf ("yes\n"); |
| 188 | else |
| 189 | printf ("no"); |
| 190 | } |
| 191 | printf ("\ttx queues:\n"); |
| 192 | for (e = 0; e < md.tx_queues_num; e++) |
| 193 | { |
| 194 | printf ("\tqueue id: %u\n", md.tx_queues[e].qid); |
| 195 | printf ("\t\tring size: %u\n", md.tx_queues[e].ring_size); |
| 196 | printf ("\t\tbuffer size: %u\n", md.tx_queues[e].buffer_size); |
| 197 | } |
| 198 | printf ("\tlink: "); |
| 199 | if (md.link_up_down) |
| 200 | printf ("up\n"); |
| 201 | else |
| 202 | printf ("down\n"); |
| 203 | } |
| 204 | free (buf); |
| 205 | } |
| 206 | |
| 207 | int |
| 208 | add_epoll_fd (int epfd, int fd, uint32_t events) |
| 209 | { |
| 210 | if (fd < 0) |
| 211 | { |
| 212 | DBG ("invalid fd %d", fd); |
| 213 | return -1; |
| 214 | } |
| 215 | struct epoll_event evt; |
| 216 | memset (&evt, 0, sizeof (evt)); |
| 217 | evt.events = events; |
| 218 | evt.data.fd = fd; |
| 219 | if (epoll_ctl (epfd, EPOLL_CTL_ADD, fd, &evt) < 0) |
| 220 | { |
| 221 | DBG ("epoll_ctl: %s fd %d", strerror (errno), fd); |
| 222 | return -1; |
| 223 | } |
| 224 | DBG ("fd %d added to epoll", fd); |
| 225 | return 0; |
| 226 | } |
| 227 | |
| 228 | int |
| 229 | mod_epoll_fd (int epfd, int fd, uint32_t events) |
| 230 | { |
| 231 | if (fd < 0) |
| 232 | { |
| 233 | DBG ("invalid fd %d", fd); |
| 234 | return -1; |
| 235 | } |
| 236 | struct epoll_event evt; |
| 237 | memset (&evt, 0, sizeof (evt)); |
| 238 | evt.events = events; |
| 239 | evt.data.fd = fd; |
| 240 | if (epoll_ctl (epfd, EPOLL_CTL_MOD, fd, &evt) < 0) |
| 241 | { |
| 242 | DBG ("epoll_ctl: %s fd %d", strerror (errno), fd); |
| 243 | return -1; |
| 244 | } |
| 245 | DBG ("fd %d moddified on epoll", fd); |
| 246 | return 0; |
| 247 | } |
| 248 | |
| 249 | int |
| 250 | del_epoll_fd (int epfd, int fd) |
| 251 | { |
| 252 | if (fd < 0) |
| 253 | { |
| 254 | DBG ("invalid fd %d", fd); |
| 255 | return -1; |
| 256 | } |
| 257 | struct epoll_event evt; |
| 258 | memset (&evt, 0, sizeof (evt)); |
| 259 | if (epoll_ctl (epfd, EPOLL_CTL_DEL, fd, &evt) < 0) |
| 260 | { |
| 261 | DBG ("epoll_ctl: %s fd %d", strerror (errno), fd); |
| 262 | return -1; |
| 263 | } |
| 264 | DBG ("fd %d removed from epoll", fd); |
| 265 | return 0; |
| 266 | } |
| 267 | |
| 268 | void * |
| 269 | memif_rx_poll (void *ptr) |
| 270 | { |
| 271 | memif_thread_data_t *data = (memif_thread_data_t *) ptr; |
| 272 | memif_connection_t *c = &memif_connection[data->index]; |
| 273 | int err; |
| 274 | uint16_t rx = 0, tx = 0, fb = 0; |
| 275 | |
| 276 | data->rx_bufs = malloc (sizeof (memif_buffer_t) * MAX_MEMIF_BUFS); |
| 277 | data->tx_bufs = malloc (sizeof (memif_buffer_t) * MAX_MEMIF_BUFS); |
| 278 | data->rx_buf_num = 0; |
| 279 | data->tx_buf_num = 0; |
| 280 | |
| 281 | data->isRunning = 1; |
| 282 | INFO ("pthread id %u starts in polling mode", data->id); |
| 283 | |
| 284 | while (1) |
| 285 | { |
| 286 | if (c->pending_del) |
| 287 | goto close; |
| 288 | |
| 289 | /* receive data from shared memory buffers */ |
| 290 | err = |
| 291 | memif_rx_burst (c->conn, data->qid, data->rx_bufs, MAX_MEMIF_BUFS, |
| 292 | &rx); |
| 293 | if (err != MEMIF_ERR_SUCCESS) |
| 294 | { |
| 295 | INFO ("memif_rx_burst: %s", memif_strerror (err)); |
| 296 | data->rx_buf_num += rx; |
| 297 | goto error; |
| 298 | } |
| 299 | data->rx_buf_num += rx; |
| 300 | if (rx == 0) |
| 301 | { |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 302 | /* if queue is in polling mode, it's not refilled */ |
| 303 | err = memif_refill_queue (c->conn, data->qid, -1); |
| 304 | if (err != MEMIF_ERR_SUCCESS) |
| 305 | INFO ("memif_buffer_free: %s", memif_strerror (err)); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 306 | continue; |
| 307 | } |
| 308 | |
| 309 | DBG ("thread id: %u", data->id); |
| 310 | |
| 311 | DBG ("received %d buffers. %u/%u alloc/free buffers", |
| 312 | rx, data->rx_buf_num, MAX_MEMIF_BUFS - data->rx_buf_num); |
| 313 | |
| 314 | err = |
| 315 | memif_buffer_alloc (c->conn, data->qid, data->tx_bufs, |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 316 | data->rx_buf_num, &tx, 128); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 317 | if (err != MEMIF_ERR_SUCCESS) |
| 318 | { |
| 319 | INFO ("memif_buffer_alloc: %s", memif_strerror (err)); |
| 320 | data->tx_buf_num += tx; |
| 321 | goto error; |
| 322 | } |
| 323 | data->tx_buf_num += tx; |
| 324 | DBG ("allocated %d/%d buffers, %u free buffers", |
| 325 | tx, data->rx_buf_num, MAX_MEMIF_BUFS - data->tx_buf_num); |
| 326 | |
| 327 | int i; |
| 328 | for (i = 0; i < rx; i++) |
| 329 | { |
| 330 | resolve_packet ((void *) (data->rx_bufs + i)->data, |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 331 | (data->rx_bufs + i)->len, |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 332 | (void *) (data->tx_bufs + i)->data, |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 333 | &(data->tx_bufs + i)->len, c->ip_addr); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | /* mark memif buffers and shared memory buffers as free */ |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 337 | err = memif_refill_queue (c->conn, data->qid, -1); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 338 | if (err != MEMIF_ERR_SUCCESS) |
| 339 | INFO ("memif_buffer_free: %s", memif_strerror (err)); |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 340 | data->rx_buf_num -= rx; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 341 | |
| 342 | DBG ("freed %d buffers. %u/%u alloc/free buffers", |
| 343 | fb, data->rx_buf_num, MAX_MEMIF_BUFS - data->rx_buf_num); |
| 344 | |
| 345 | err = |
| 346 | memif_tx_burst (c->conn, data->qid, data->tx_bufs, data->tx_buf_num, |
| 347 | &tx); |
| 348 | if (err != MEMIF_ERR_SUCCESS) |
| 349 | { |
| 350 | INFO ("memif_tx_burst: %s", memif_strerror (err)); |
| 351 | goto error; |
| 352 | } |
| 353 | DBG ("tx: %d/%u", tx, data->tx_buf_num); |
| 354 | data->tx_buf_num -= tx; |
| 355 | } |
| 356 | |
| 357 | error: |
| 358 | INFO ("thread %u error!", data->id); |
| 359 | goto close; |
| 360 | |
| 361 | close: |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 362 | err = memif_refill_queue (c->conn, data->qid, -1); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 363 | if (err != MEMIF_ERR_SUCCESS) |
| 364 | INFO ("memif_buffer_free: %s", memif_strerror (err)); |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 365 | data->rx_buf_num -= rx; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 366 | DBG ("freed %d buffers. %u/%u alloc/free buffers", |
| 367 | fb, data->rx_buf_num, MAX_MEMIF_BUFS - data->rx_buf_num); |
| 368 | free (data->rx_bufs); |
| 369 | free (data->tx_bufs); |
| 370 | data->isRunning = 0; |
| 371 | INFO ("pthread id %u exit", data->id); |
| 372 | pthread_exit (NULL); |
| 373 | } |
| 374 | |
| 375 | void * |
| 376 | memif_rx_interrupt (void *ptr) |
| 377 | { |
| 378 | memif_thread_data_t *data = (memif_thread_data_t *) ptr; |
| 379 | memif_connection_t *c = &memif_connection[data->index]; |
| 380 | int err; |
| 381 | uint16_t rx = 0, tx = 0, fb = 0; |
| 382 | struct epoll_event evt, *e; |
| 383 | int en = 0; |
| 384 | uint32_t events = 0; |
| 385 | sigset_t sigset; |
| 386 | |
| 387 | signal (SIGUSR1, user_signal_handler); |
| 388 | |
| 389 | data->rx_bufs = malloc (sizeof (memif_buffer_t) * MAX_MEMIF_BUFS); |
| 390 | data->tx_bufs = malloc (sizeof (memif_buffer_t) * MAX_MEMIF_BUFS); |
| 391 | data->rx_buf_num = 0; |
| 392 | data->tx_buf_num = 0; |
| 393 | |
| 394 | data->isRunning = 1; |
| 395 | INFO ("pthread id %u starts in interrupt mode", data->id); |
| 396 | int thread_epfd = epoll_create (1); |
| 397 | |
| 398 | /* get interrupt queue id */ |
| 399 | int fd = -1; |
| 400 | err = memif_get_queue_efd (c->conn, data->qid, &fd); |
| 401 | if (err != MEMIF_ERR_SUCCESS) |
| 402 | { |
| 403 | INFO ("memif_get_queue_efd: %s", memif_strerror (err)); |
| 404 | goto error; |
| 405 | } |
| 406 | add_epoll_fd (thread_epfd, fd, EPOLLIN); |
| 407 | |
| 408 | while (1) |
| 409 | { |
| 410 | memset (&evt, 0, sizeof (evt)); |
| 411 | evt.events = EPOLLIN | EPOLLOUT; |
| 412 | sigemptyset (&sigset); |
| 413 | en = epoll_pwait (thread_epfd, &evt, 1, -1, &sigset); |
| 414 | if (en < 0) |
| 415 | { |
| 416 | if (errno == EINTR) |
| 417 | goto close; |
| 418 | DBG ("epoll_pwait: %s", strerror (errno)); |
| 419 | goto error; |
| 420 | } |
| 421 | else if (en > 0) |
| 422 | { |
| 423 | /* receive data from shared memory buffers */ |
| 424 | err = |
| 425 | memif_rx_burst (c->conn, data->qid, data->rx_bufs, MAX_MEMIF_BUFS, |
| 426 | &rx); |
| 427 | if (err != MEMIF_ERR_SUCCESS) |
| 428 | { |
| 429 | INFO ("memif_rx_burst: %s", memif_strerror (err)); |
| 430 | data->rx_buf_num += rx; |
| 431 | goto error; |
| 432 | } |
| 433 | data->rx_buf_num += rx; |
| 434 | if (rx == 0) |
| 435 | { |
| 436 | continue; |
| 437 | } |
| 438 | |
| 439 | DBG ("thread id: %u", data->id); |
| 440 | |
| 441 | DBG ("received %d buffers. %u/%u alloc/free buffers", |
| 442 | rx, data->rx_buf_num, MAX_MEMIF_BUFS - data->rx_buf_num); |
| 443 | |
| 444 | err = |
| 445 | memif_buffer_alloc (c->conn, data->qid, data->tx_bufs, |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 446 | data->rx_buf_num, &tx, 128); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 447 | if (err != MEMIF_ERR_SUCCESS) |
| 448 | { |
| 449 | INFO ("memif_buffer_alloc: %s", memif_strerror (err)); |
| 450 | data->tx_buf_num += tx; |
| 451 | goto error; |
| 452 | } |
| 453 | data->tx_buf_num += tx; |
| 454 | DBG ("allocated %d/%d buffers, %u free buffers", |
| 455 | tx, data->rx_buf_num, MAX_MEMIF_BUFS - data->tx_buf_num); |
| 456 | |
| 457 | int i; |
| 458 | for (i = 0; i < rx; i++) |
| 459 | { |
| 460 | resolve_packet ((void *) (data->rx_bufs + i)->data, |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 461 | (data->rx_bufs + i)->len, |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 462 | (void *) (data->tx_bufs + i)->data, |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 463 | &(data->tx_bufs + i)->len, c->ip_addr); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | /* mark memif buffers and shared memory buffers as free */ |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 467 | err = memif_refill_queue (c->conn, data->qid, rx); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 468 | if (err != MEMIF_ERR_SUCCESS) |
| 469 | INFO ("memif_buffer_free: %s", memif_strerror (err)); |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 470 | data->rx_buf_num -= rx; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 471 | |
| 472 | DBG ("freed %d buffers. %u/%u alloc/free buffers", |
| 473 | fb, data->rx_buf_num, MAX_MEMIF_BUFS - data->rx_buf_num); |
| 474 | |
| 475 | err = |
| 476 | memif_tx_burst (c->conn, data->qid, data->tx_bufs, |
| 477 | data->tx_buf_num, &tx); |
| 478 | if (err != MEMIF_ERR_SUCCESS) |
| 479 | { |
| 480 | INFO ("memif_tx_burst: %s", memif_strerror (err)); |
| 481 | goto error; |
| 482 | } |
| 483 | DBG ("tx: %d/%u", tx, data->tx_buf_num); |
| 484 | data->tx_buf_num -= tx; |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | error: |
| 489 | INFO ("thread %u error!", data->id); |
| 490 | goto close; |
| 491 | |
| 492 | close: |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 493 | err = memif_refill_queue (c->conn, data->qid, rx); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 494 | if (err != MEMIF_ERR_SUCCESS) |
| 495 | INFO ("memif_buffer_free: %s", memif_strerror (err)); |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 496 | data->rx_buf_num -= rx; |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 497 | DBG ("freed %d buffers. %u/%u alloc/free buffers", |
| 498 | fb, data->rx_buf_num, MAX_MEMIF_BUFS - data->rx_buf_num); |
| 499 | free (data->rx_bufs); |
| 500 | free (data->tx_bufs); |
| 501 | data->isRunning = 0; |
| 502 | INFO ("pthread id %u exit", data->id); |
| 503 | pthread_exit (NULL); |
| 504 | |
| 505 | } |
| 506 | |
| 507 | /* informs user about connected status. private_ctx is used by user to identify connection |
| 508 | (multiple connections WIP) */ |
| 509 | int |
| 510 | on_connect (memif_conn_handle_t conn, void *private_ctx) |
| 511 | { |
| 512 | long index = (*(long *) private_ctx); |
| 513 | int err, i, ti; |
| 514 | INFO ("memif connected! index %ld", index); |
| 515 | memif_connection_t *c = &memif_connection[index]; |
| 516 | c->pending_del = 0; |
| 517 | |
| 518 | for (i = 0; i < MAX_QUEUES; i++) |
| 519 | { |
| 520 | err = memif_set_rx_mode (c->conn, MEMIF_RX_MODE_POLLING, i); |
| 521 | if (err != MEMIF_ERR_SUCCESS) |
| 522 | INFO ("memif_set_rx_mode: %s qid: %u", memif_strerror (err), i); |
| 523 | else |
| 524 | { |
| 525 | ti = (index * MAX_QUEUES) + i; |
| 526 | if (thread_data[ti].isRunning) |
| 527 | { |
| 528 | INFO ("thread id: %d already running!", ti); |
| 529 | continue; |
| 530 | } |
| 531 | thread_data[ti].index = index; |
| 532 | thread_data[ti].qid = i; |
| 533 | thread_data[ti].id = ti; |
| 534 | if ((i % 2) == 0) |
| 535 | pthread_create (&thread[ti], |
| 536 | NULL, memif_rx_poll, (void *) &thread_data[ti]); |
| 537 | else |
| 538 | pthread_create (&thread[ti], |
| 539 | NULL, memif_rx_interrupt, |
| 540 | (void *) &thread_data[ti]); |
| 541 | } |
| 542 | |
| 543 | } |
| 544 | return 0; |
| 545 | } |
| 546 | |
| 547 | /* informs user about disconnected status. private_ctx is used by user to identify connection |
| 548 | (multiple connections WIP) */ |
| 549 | int |
| 550 | on_disconnect (memif_conn_handle_t conn, void *private_ctx) |
| 551 | { |
| 552 | void *ptr; |
| 553 | long index = (*(long *) private_ctx); |
| 554 | memif_connection_t *c = &memif_connection[index]; |
| 555 | int i, ti; |
| 556 | INFO ("memif disconnected!"); |
| 557 | /* inform thread in polling mode about memif disconenction */ |
| 558 | c->pending_del = 1; |
| 559 | for (i = 0; i < MAX_QUEUES; i++) |
| 560 | { |
| 561 | ti = (index * MAX_QUEUES) + i; |
| 562 | if (!thread_data[ti].isRunning) |
| 563 | continue; |
| 564 | if ((i % 2) != 0) |
| 565 | pthread_kill (thread[ti], SIGUSR1); /* interrupt thread in interrupt mode */ |
| 566 | pthread_join (thread[ti], &ptr); |
| 567 | } |
| 568 | return 0; |
| 569 | } |
| 570 | |
| 571 | /* user needs to watch new fd or stop watching fd that is about to be closed. |
| 572 | control fd will be modified during connection establishment to minimize CPU usage */ |
| 573 | int |
| 574 | control_fd_update (int fd, uint8_t events) |
| 575 | { |
| 576 | /* convert memif event definitions to epoll events */ |
| 577 | if (events & MEMIF_FD_EVENT_DEL) |
| 578 | return del_epoll_fd (main_epfd, fd); |
| 579 | |
| 580 | uint32_t evt = 0; |
| 581 | if (events & MEMIF_FD_EVENT_READ) |
| 582 | evt |= EPOLLIN; |
| 583 | if (events & MEMIF_FD_EVENT_WRITE) |
| 584 | evt |= EPOLLOUT; |
| 585 | |
| 586 | if (events & MEMIF_FD_EVENT_MOD) |
| 587 | return mod_epoll_fd (main_epfd, fd, evt); |
| 588 | |
| 589 | return add_epoll_fd (main_epfd, fd, evt); |
| 590 | } |
| 591 | |
| 592 | int |
| 593 | icmpr_memif_create (long index) |
| 594 | { |
| 595 | if (index >= MAX_CONNS) |
| 596 | { |
| 597 | INFO ("connection array overflow"); |
| 598 | return 0; |
| 599 | } |
| 600 | if (index < 0) |
| 601 | { |
| 602 | INFO ("don't even try..."); |
| 603 | return 0; |
| 604 | } |
| 605 | memif_connection_t *c = &memif_connection[index]; |
| 606 | |
| 607 | /* setting memif connection arguments */ |
| 608 | memif_conn_args_t args; |
| 609 | int fd = -1; |
| 610 | memset (&args, 0, sizeof (args)); |
| 611 | args.is_master = 0; |
| 612 | args.log2_ring_size = 10; |
| 613 | args.buffer_size = 2048; |
| 614 | args.num_s2m_rings = 2; |
| 615 | args.num_m2s_rings = 2; |
| 616 | strncpy ((char *) args.interface_name, IF_NAME, strlen (IF_NAME)); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 617 | args.mode = 0; |
| 618 | /* socket filename is not specified, because this app is supposed to |
| 619 | connect to VPP over memif. so default socket filename will be used */ |
| 620 | /* default socketfile = /run/vpp/memif.sock */ |
| 621 | |
| 622 | args.interface_id = index; |
| 623 | /* last argument for memif_create (void * private_ctx) is used by user |
| 624 | to identify connection. this context is returned with callbacks */ |
| 625 | int err = memif_create (&c->conn, |
| 626 | &args, on_connect, on_disconnect, NULL, |
| 627 | &ctx[index]); |
| 628 | if (err != MEMIF_ERR_SUCCESS) |
| 629 | { |
| 630 | INFO ("memif_create: %s", memif_strerror (err)); |
| 631 | return 0; |
| 632 | } |
| 633 | |
| 634 | c->index = index; |
| 635 | |
| 636 | c->ip_addr[0] = 192; |
| 637 | c->ip_addr[1] = 168; |
| 638 | c->ip_addr[2] = c->index + 1; |
| 639 | c->ip_addr[3] = 2; |
| 640 | return 0; |
| 641 | } |
| 642 | |
| 643 | int |
| 644 | icmpr_memif_delete (long index) |
| 645 | { |
| 646 | if (index >= MAX_CONNS) |
| 647 | { |
| 648 | INFO ("connection array overflow"); |
| 649 | return 0; |
| 650 | } |
| 651 | if (index < 0) |
| 652 | { |
| 653 | INFO ("don't even try..."); |
| 654 | return 0; |
| 655 | } |
| 656 | memif_connection_t *c = &memif_connection[index]; |
| 657 | |
| 658 | int err; |
| 659 | /* disconenct then delete memif connection */ |
| 660 | err = memif_delete (&c->conn); |
| 661 | if (err != MEMIF_ERR_SUCCESS) |
| 662 | INFO ("memif_delete: %s", memif_strerror (err)); |
| 663 | return 0; |
| 664 | } |
| 665 | |
| 666 | void |
| 667 | print_help () |
| 668 | { |
| 669 | printf ("LIBMEMIF EXAMPLE APP: %s", APP_NAME); |
| 670 | #ifdef ICMP_DBG |
| 671 | printf (" (debug)"); |
| 672 | #endif |
| 673 | printf ("\n"); |
| 674 | printf ("==============================\n"); |
| 675 | printf ("libmemif version: %s", LIBMEMIF_VERSION); |
| 676 | #ifdef MEMIF_DBG |
| 677 | printf (" (debug)"); |
| 678 | #endif |
| 679 | printf ("\n"); |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 680 | printf ("memif version: %d\n", memif_get_version ()); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 681 | printf ("commands:\n"); |
| 682 | printf ("\thelp - prints this help\n"); |
| 683 | printf ("\texit - exit app\n"); |
| 684 | printf ("\tconn <index> - create memif (slave-mode)\n"); |
| 685 | printf ("\tdel <index> - delete memif\n"); |
| 686 | printf ("\tshow - show connection details\n"); |
| 687 | printf ("\tip-set <index> <ip-addr> - set interface ip address\n"); |
| 688 | } |
| 689 | |
| 690 | int |
| 691 | icmpr_free () |
| 692 | { |
| 693 | /* application cleanup */ |
| 694 | int err; |
| 695 | long i; |
| 696 | for (i = 0; i < MAX_CONNS; i++) |
| 697 | { |
| 698 | memif_connection_t *c = &memif_connection[i]; |
| 699 | if (c->conn) |
| 700 | icmpr_memif_delete (i); |
| 701 | } |
| 702 | |
| 703 | err = memif_cleanup (); |
| 704 | if (err != MEMIF_ERR_SUCCESS) |
| 705 | INFO ("memif_delete: %s", memif_strerror (err)); |
| 706 | |
| 707 | return 0; |
| 708 | } |
| 709 | |
| 710 | int |
| 711 | icmpr_set_ip (long index, char *ip) |
| 712 | { |
| 713 | if (index >= MAX_CONNS) |
| 714 | { |
| 715 | INFO ("connection array overflow"); |
| 716 | return 0; |
| 717 | } |
| 718 | if (index < 0) |
| 719 | { |
| 720 | INFO ("don't even try..."); |
| 721 | return 0; |
| 722 | } |
| 723 | memif_connection_t *c = &memif_connection[index]; |
| 724 | if (c->conn == NULL) |
| 725 | { |
| 726 | INFO ("no connection at index %ld", index); |
| 727 | return 0; |
| 728 | } |
| 729 | |
| 730 | char *end; |
| 731 | char *ui; |
| 732 | uint8_t tmp[4]; |
| 733 | ui = strtok (ip, "."); |
| 734 | if (ui == NULL) |
| 735 | goto error; |
| 736 | tmp[0] = strtol (ui, &end, 10); |
| 737 | |
| 738 | ui = strtok (NULL, "."); |
| 739 | if (ui == NULL) |
| 740 | goto error; |
| 741 | tmp[1] = strtol (ui, &end, 10); |
| 742 | |
| 743 | ui = strtok (NULL, "."); |
| 744 | if (ui == NULL) |
| 745 | goto error; |
| 746 | tmp[2] = strtol (ui, &end, 10); |
| 747 | |
| 748 | ui = strtok (NULL, "."); |
| 749 | if (ui == NULL) |
| 750 | goto error; |
| 751 | tmp[3] = strtol (ui, &end, 10); |
| 752 | |
| 753 | c->ip_addr[0] = tmp[0]; |
| 754 | c->ip_addr[1] = tmp[1]; |
| 755 | c->ip_addr[2] = tmp[2]; |
| 756 | c->ip_addr[3] = tmp[3]; |
| 757 | |
| 758 | INFO ("memif %ld ip address set to %u.%u.%u.%u", |
| 759 | index, c->ip_addr[0], c->ip_addr[1], c->ip_addr[2], c->ip_addr[3]); |
| 760 | |
| 761 | return 0; |
| 762 | |
| 763 | error: |
| 764 | INFO ("invalid ip address"); |
| 765 | return 0; |
| 766 | } |
| 767 | |
| 768 | |
| 769 | int |
| 770 | user_input_handler () |
| 771 | { |
| 772 | int i; |
| 773 | char *in = (char *) malloc (256); |
| 774 | char *ui = fgets (in, 256, stdin); |
| 775 | char *end; |
| 776 | if (in[0] == '\n') |
| 777 | goto done; |
| 778 | ui = strtok (in, " "); |
| 779 | if (strncmp (ui, "exit", 4) == 0) |
| 780 | { |
| 781 | free (in); |
| 782 | icmpr_free (); |
| 783 | exit (EXIT_SUCCESS); |
| 784 | } |
| 785 | else if (strncmp (ui, "help", 4) == 0) |
| 786 | { |
| 787 | print_help (); |
| 788 | goto done; |
| 789 | } |
| 790 | else if (strncmp (ui, "conn", 4) == 0) |
| 791 | { |
| 792 | ui = strtok (NULL, " "); |
| 793 | if (ui != NULL) |
| 794 | icmpr_memif_create (strtol (ui, &end, 10)); |
| 795 | else |
| 796 | INFO ("expected id"); |
| 797 | goto done; |
| 798 | } |
| 799 | else if (strncmp (ui, "del", 3) == 0) |
| 800 | { |
| 801 | ui = strtok (NULL, " "); |
| 802 | if (ui != NULL) |
| 803 | icmpr_memif_delete (strtol (ui, &end, 10)); |
| 804 | else |
| 805 | INFO ("expected id"); |
| 806 | goto done; |
| 807 | } |
| 808 | else if (strncmp (ui, "show", 4) == 0) |
| 809 | { |
| 810 | print_memif_details (); |
| 811 | goto done; |
| 812 | } |
| 813 | else if (strncmp (ui, "ip-set", 6) == 0) |
| 814 | { |
| 815 | ui = strtok (NULL, " "); |
| 816 | if (ui != NULL) |
| 817 | icmpr_set_ip (strtol (ui, &end, 10), strtok (NULL, " ")); |
| 818 | else |
| 819 | INFO ("expected id"); |
| 820 | goto done; |
| 821 | } |
| 822 | else |
| 823 | { |
| 824 | DBG ("unknown command: %s", ui); |
| 825 | goto done; |
| 826 | } |
| 827 | |
| 828 | return 0; |
| 829 | done: |
| 830 | free (in); |
| 831 | return 0; |
| 832 | } |
| 833 | |
| 834 | int |
| 835 | poll_event (int timeout) |
| 836 | { |
| 837 | struct epoll_event evt, *e; |
| 838 | int app_err = 0, memif_err = 0, en = 0; |
| 839 | int tmp, nfd; |
| 840 | uint32_t events = 0; |
| 841 | memset (&evt, 0, sizeof (evt)); |
| 842 | evt.events = EPOLLIN | EPOLLOUT; |
| 843 | sigset_t sigset; |
| 844 | sigemptyset (&sigset); |
| 845 | en = epoll_pwait (main_epfd, &evt, 1, timeout, &sigset); |
| 846 | if (en < 0) |
| 847 | { |
| 848 | DBG ("epoll_pwait: %s", strerror (errno)); |
| 849 | return -1; |
| 850 | } |
| 851 | if (en > 0) |
| 852 | { |
| 853 | /* this app does not use any other file descriptors than stds and memif control fds */ |
| 854 | if (evt.data.fd > 2) |
| 855 | { |
| 856 | /* event of memif control fd */ |
| 857 | /* convert epolle events to memif events */ |
| 858 | if (evt.events & EPOLLIN) |
| 859 | events |= MEMIF_FD_EVENT_READ; |
| 860 | if (evt.events & EPOLLOUT) |
| 861 | events |= MEMIF_FD_EVENT_WRITE; |
| 862 | if (evt.events & EPOLLERR) |
| 863 | events |= MEMIF_FD_EVENT_ERROR; |
| 864 | memif_err = memif_control_fd_handler (evt.data.fd, events); |
| 865 | if (memif_err != MEMIF_ERR_SUCCESS) |
| 866 | INFO ("memif_control_fd_handler: %s", memif_strerror (memif_err)); |
| 867 | } |
| 868 | else if (evt.data.fd == 0) |
| 869 | { |
| 870 | app_err = user_input_handler (); |
| 871 | } |
| 872 | else |
| 873 | { |
| 874 | DBG ("unexpected event at memif_epfd. fd %d", evt.data.fd); |
| 875 | } |
| 876 | } |
| 877 | |
| 878 | if ((app_err < 0) || (memif_err < 0)) |
| 879 | { |
| 880 | if (app_err < 0) |
| 881 | DBG ("user input handler error"); |
| 882 | if (memif_err < 0) |
| 883 | DBG ("memif control fd handler error"); |
| 884 | return -1; |
| 885 | } |
| 886 | |
| 887 | return 0; |
| 888 | } |
| 889 | |
| 890 | int |
| 891 | main () |
| 892 | { |
| 893 | main_epfd = epoll_create (1); |
| 894 | add_epoll_fd (main_epfd, 0, EPOLLIN); |
| 895 | |
| 896 | /* initialize memory interface */ |
| 897 | int err, i; |
| 898 | /* if valid callback is passed as argument, fd event polling will be done by user |
| 899 | all file descriptors and events will be passed to user in this callback */ |
| 900 | /* if callback is set to NULL libmemif will handle fd event polling */ |
Jakub Grajciar | ecfa2aa | 2018-03-26 11:26:34 +0200 | [diff] [blame] | 901 | err = memif_init (control_fd_update, APP_NAME, NULL, NULL); |
Jakub Grajciar | 7c5c40d | 2017-08-30 10:13:25 +0200 | [diff] [blame] | 902 | if (err != MEMIF_ERR_SUCCESS) |
| 903 | INFO ("memif_init: %s", memif_strerror (err)); |
| 904 | |
| 905 | for (i = 0; i < MAX_CONNS; i++) |
| 906 | { |
| 907 | memif_connection[i].conn = NULL; |
| 908 | ctx[i] = i; |
| 909 | } |
| 910 | |
| 911 | memset (&thread_data, 0, sizeof (memif_thread_data_t) * MAX_THREADS); |
| 912 | |
| 913 | print_help (); |
| 914 | |
| 915 | /* main loop */ |
| 916 | while (1) |
| 917 | { |
| 918 | if (poll_event (-1) < 0) |
| 919 | { |
| 920 | DBG ("poll_event error!"); |
| 921 | } |
| 922 | } |
| 923 | } |