1 #define _GNU_SOURCE 2 #include <sched.h> 3 #include <unistd.h> 4 #include <sys/time.h> 5 #include <semaphore.h> 6 #include <sys/mman.h> 7 #include <signal.h> 8 #include <assert.h> 9 #include <string.h> 10 11 #include "cpu.h" 12 #include "eth_in.h" 13 #include "fhash.h" 14 #include "tcp_send_buffer.h" 15 #include "tcp_ring_buffer.h" 16 #include "socket.h" 17 #include "eth_out.h" 18 #include "tcp.h" 19 #include "tcp_in.h" 20 #include "tcp_out.h" 21 #include "mtcp_api.h" 22 #include "eventpoll.h" 23 #include "logger.h" 24 #include "config.h" 25 #include "arp.h" 26 #include "ip_out.h" 27 #include "timer.h" 28 #include "debug.h" 29 #include "event_callback.h" 30 #include "tcp_rb.h" 31 #include "tcp_stream.h" 32 #include "io_module.h" 33 34 #ifdef ENABLE_DPDK 35 /* for launching rte thread */ 36 #include <rte_launch.h> 37 #include <rte_lcore.h> 38 #endif /* !ENABLE_DPDK */ 39 #define PS_CHUNK_SIZE 64 40 #define RX_THRESH (PS_CHUNK_SIZE * 0.8) 41 42 #define ROUND_STAT FALSE 43 #define TIME_STAT FALSE 44 #define EVENT_STAT FALSE 45 #define TESTING FALSE 46 47 #define LOG_FILE_NAME "log" 48 #define MAX_FILE_NAME 1024 49 50 #define MAX(a, b) ((a)>(b)?(a):(b)) 51 #define MIN(a, b) ((a)<(b)?(a):(b)) 52 53 #define PER_STREAM_SLICE 0.1 // in ms 54 #define PER_STREAM_TCHECK 1 // in ms 55 #define PS_SELECT_TIMEOUT 100 // in us 56 57 #define GBPS(bytes) (bytes * 8.0 / (1000 * 1000 * 1000)) 58 59 /*----------------------------------------------------------------------------*/ 60 /* handlers for threads */ 61 struct mtcp_thread_context *g_pctx[MAX_CPUS] = {0}; 62 struct log_thread_context *g_logctx[MAX_CPUS] = {0}; 63 /*----------------------------------------------------------------------------*/ 64 static pthread_t g_thread[MAX_CPUS] = {0}; 65 static pthread_t log_thread[MAX_CPUS] = {0}; 66 /*----------------------------------------------------------------------------*/ 67 static sem_t g_init_sem[MAX_CPUS]; 68 static sem_t g_done_sem[MAX_CPUS]; 69 static int running[MAX_CPUS] = {0}; 70 /*----------------------------------------------------------------------------*/ 71 mtcp_sighandler_t app_signal_handler; 72 static int sigint_cnt[MAX_CPUS] = {0}; 73 static struct timespec sigint_ts[MAX_CPUS]; 74 /*----------------------------------------------------------------------------*/ 75 #ifdef NETSTAT 76 #if NETSTAT_TOTAL 77 static int printer = -1; 78 #if ROUND_STAT 79 #endif /* ROUND_STAT */ 80 #endif /* NETSTAT_TOTAL */ 81 #endif /* NETSTAT */ 82 /*----------------------------------------------------------------------------*/ 83 void 84 HandleSignal(int signal) 85 { 86 int i = 0; 87 88 if (signal == SIGINT) { 89 #ifdef DARWIN 90 int core = 0; 91 #else 92 int core = sched_getcpu(); 93 #endif 94 struct timespec cur_ts; 95 96 clock_gettime(CLOCK_REALTIME, &cur_ts); 97 98 if (sigint_cnt[core] > 0 && cur_ts.tv_sec == sigint_ts[core].tv_sec) { 99 for (i = 0; i < g_config.mos->num_cores; i++) { 100 if (running[i]) { 101 exit(0); 102 g_pctx[i]->exit = TRUE; 103 } 104 } 105 } else { 106 for (i = 0; i < g_config.mos->num_cores; i++) { 107 if (g_pctx[i]) 108 g_pctx[i]->interrupt = TRUE; 109 } 110 if (!app_signal_handler) { 111 for (i = 0; i < g_config.mos->num_cores; i++) { 112 if (running[i]) { 113 exit(0); 114 g_pctx[i]->exit = TRUE; 115 } 116 } 117 } 118 } 119 sigint_cnt[core]++; 120 clock_gettime(CLOCK_REALTIME, &sigint_ts[core]); 121 } 122 123 if (signal != SIGUSR1) { 124 if (app_signal_handler) { 125 app_signal_handler(signal); 126 } 127 } 128 } 129 /*----------------------------------------------------------------------------*/ 130 static int 131 AttachDevice(struct mtcp_thread_context* ctx) 132 { 133 int working = -1; 134 mtcp_manager_t mtcp = ctx->mtcp_manager; 135 136 if (mtcp->iom->link_devices) 137 working = mtcp->iom->link_devices(ctx); 138 else 139 return 0; 140 141 return working; 142 } 143 /*----------------------------------------------------------------------------*/ 144 #ifdef TIMESTAT 145 static inline void 146 InitStatCounter(struct stat_counter *counter) 147 { 148 counter->cnt = 0; 149 counter->sum = 0; 150 counter->max = 0; 151 counter->min = 0; 152 } 153 /*----------------------------------------------------------------------------*/ 154 static inline void 155 UpdateStatCounter(struct stat_counter *counter, int64_t value) 156 { 157 counter->cnt++; 158 counter->sum += value; 159 if (value > counter->max) 160 counter->max = value; 161 if (counter->min == 0 || value < counter->min) 162 counter->min = value; 163 } 164 /*----------------------------------------------------------------------------*/ 165 static inline uint64_t 166 GetAverageStat(struct stat_counter *counter) 167 { 168 return counter->cnt ? (counter->sum / counter->cnt) : 0; 169 } 170 /*----------------------------------------------------------------------------*/ 171 static inline int64_t 172 TimeDiffUs(struct timeval *t2, struct timeval *t1) 173 { 174 return (t2->tv_sec - t1->tv_sec) * 1000000 + 175 (int64_t)(t2->tv_usec - t1->tv_usec); 176 } 177 /*----------------------------------------------------------------------------*/ 178 #endif 179 #ifdef NETSTAT 180 static inline void 181 PrintThreadNetworkStats(mtcp_manager_t mtcp, struct net_stat *ns) 182 { 183 int i; 184 185 for (i = 0; i < g_config.mos->netdev_table->num; i++) { 186 ns->rx_packets[i] = mtcp->nstat.rx_packets[i] - mtcp->p_nstat.rx_packets[i]; 187 ns->rx_errors[i] = mtcp->nstat.rx_errors[i] - mtcp->p_nstat.rx_errors[i]; 188 ns->rx_bytes[i] = mtcp->nstat.rx_bytes[i] - mtcp->p_nstat.rx_bytes[i]; 189 ns->tx_packets[i] = mtcp->nstat.tx_packets[i] - mtcp->p_nstat.tx_packets[i]; 190 ns->tx_drops[i] = mtcp->nstat.tx_drops[i] - mtcp->p_nstat.tx_drops[i]; 191 ns->tx_bytes[i] = mtcp->nstat.tx_bytes[i] - mtcp->p_nstat.tx_bytes[i]; 192 #if NETSTAT_PERTHREAD 193 if (g_config.mos->netdev_table->ent[i]->stat_print) { 194 fprintf(stderr, "[CPU%2d] %s flows: %6u, " 195 "RX: %7llu(pps) (err: %5llu), %5.2lf(Gbps), " 196 "TX: %7llu(pps), %5.2lf(Gbps)\n", 197 mtcp->ctx->cpu, 198 g_config.mos->netdev_table->ent[i]->dev_name, 199 (unsigned)mtcp->flow_cnt, 200 (long long unsigned)ns->rx_packets[i], 201 (long long unsigned)ns->rx_errors[i], 202 GBPS(ns->rx_bytes[i]), 203 (long long unsigned)ns->tx_packets[i], 204 GBPS(ns->tx_bytes[i])); 205 } 206 #endif 207 } 208 mtcp->p_nstat = mtcp->nstat; 209 210 } 211 /*----------------------------------------------------------------------------*/ 212 #if ROUND_STAT 213 static inline void 214 PrintThreadRoundStats(mtcp_manager_t mtcp, struct run_stat *rs) 215 { 216 #define ROUND_DIV (1000) 217 rs->rounds = mtcp->runstat.rounds - mtcp->p_runstat.rounds; 218 rs->rounds_rx = mtcp->runstat.rounds_rx - mtcp->p_runstat.rounds_rx; 219 rs->rounds_rx_try = mtcp->runstat.rounds_rx_try - mtcp->p_runstat.rounds_rx_try; 220 rs->rounds_tx = mtcp->runstat.rounds_tx - mtcp->p_runstat.rounds_tx; 221 rs->rounds_tx_try = mtcp->runstat.rounds_tx_try - mtcp->p_runstat.rounds_tx_try; 222 rs->rounds_select = mtcp->runstat.rounds_select - mtcp->p_runstat.rounds_select; 223 rs->rounds_select_rx = mtcp->runstat.rounds_select_rx - mtcp->p_runstat.rounds_select_rx; 224 rs->rounds_select_tx = mtcp->runstat.rounds_select_tx - mtcp->p_runstat.rounds_select_tx; 225 rs->rounds_select_intr = mtcp->runstat.rounds_select_intr - mtcp->p_runstat.rounds_select_intr; 226 rs->rounds_twcheck = mtcp->runstat.rounds_twcheck - mtcp->p_runstat.rounds_twcheck; 227 mtcp->p_runstat = mtcp->runstat; 228 #if NETSTAT_PERTHREAD 229 fprintf(stderr, "[CPU%2d] Rounds: %4lluK, " 230 "rx: %3lluK (try: %4lluK), tx: %3lluK (try: %4lluK), " 231 "ps_select: %4llu (rx: %4llu, tx: %4llu, intr: %3llu)\n", 232 mtcp->ctx->cpu, rs->rounds / ROUND_DIV, 233 rs->rounds_rx / ROUND_DIV, rs->rounds_rx_try / ROUND_DIV, 234 rs->rounds_tx / ROUND_DIV, rs->rounds_tx_try / ROUND_DIV, 235 rs->rounds_select, 236 rs->rounds_select_rx, rs->rounds_select_tx, rs->rounds_select_intr); 237 #endif 238 } 239 #endif /* ROUND_STAT */ 240 /*----------------------------------------------------------------------------*/ 241 #if TIME_STAT 242 static inline void 243 PrintThreadRoundTime(mtcp_manager_t mtcp) 244 { 245 fprintf(stderr, "[CPU%2d] Time: (avg, max) " 246 "round: (%4luus, %4luus), processing: (%4luus, %4luus), " 247 "tcheck: (%4luus, %4luus), epoll: (%4luus, %4luus), " 248 "handle: (%4luus, %4luus), xmit: (%4luus, %4luus), " 249 "select: (%4luus, %4luus)\n", mtcp->ctx->cpu, 250 GetAverageStat(&mtcp->rtstat.round), mtcp->rtstat.round.max, 251 GetAverageStat(&mtcp->rtstat.processing), mtcp->rtstat.processing.max, 252 GetAverageStat(&mtcp->rtstat.tcheck), mtcp->rtstat.tcheck.max, 253 GetAverageStat(&mtcp->rtstat.epoll), mtcp->rtstat.epoll.max, 254 GetAverageStat(&mtcp->rtstat.handle), mtcp->rtstat.handle.max, 255 GetAverageStat(&mtcp->rtstat.xmit), mtcp->rtstat.xmit.max, 256 GetAverageStat(&mtcp->rtstat.select), mtcp->rtstat.select.max); 257 258 InitStatCounter(&mtcp->rtstat.round); 259 InitStatCounter(&mtcp->rtstat.processing); 260 InitStatCounter(&mtcp->rtstat.tcheck); 261 InitStatCounter(&mtcp->rtstat.epoll); 262 InitStatCounter(&mtcp->rtstat.handle); 263 InitStatCounter(&mtcp->rtstat.xmit); 264 InitStatCounter(&mtcp->rtstat.select); 265 } 266 #endif 267 #endif /* NETSTAT */ 268 /*----------------------------------------------------------------------------*/ 269 #if EVENT_STAT 270 static inline void 271 PrintEventStat(int core, struct mtcp_epoll_stat *stat) 272 { 273 fprintf(stderr, "[CPU%2d] calls: %lu, waits: %lu, wakes: %lu, " 274 "issued: %lu, registered: %lu, invalidated: %lu, handled: %lu\n", 275 core, stat->calls, stat->waits, stat->wakes, 276 stat->issued, stat->registered, stat->invalidated, stat->handled); 277 memset(stat, 0, sizeof(struct mtcp_epoll_stat)); 278 } 279 #endif /* EVENT_STAT */ 280 /*----------------------------------------------------------------------------*/ 281 #ifdef NETSTAT 282 static inline void 283 PrintNetworkStats(mtcp_manager_t mtcp, uint32_t cur_ts) 284 { 285 #define TIMEOUT 1 286 int i; 287 struct net_stat ns; 288 bool stat_print = false; 289 #if ROUND_STAT 290 struct run_stat rs; 291 #endif /* ROUND_STAT */ 292 #ifdef NETSTAT_TOTAL 293 static double peak_total_rx_gbps = 0; 294 static double peak_total_tx_gbps = 0; 295 static double avg_total_rx_gbps = 0; 296 static double avg_total_tx_gbps = 0; 297 298 double total_rx_gbps = 0, total_tx_gbps = 0; 299 int j; 300 uint32_t gflow_cnt = 0; 301 struct net_stat g_nstat; 302 #if ROUND_STAT 303 struct run_stat g_runstat; 304 #endif /* ROUND_STAT */ 305 #endif /* NETSTAT_TOTAL */ 306 307 if (TS_TO_MSEC(cur_ts - mtcp->p_nstat_ts) < SEC_TO_MSEC(TIMEOUT)) { 308 return; 309 } 310 311 mtcp->p_nstat_ts = cur_ts; 312 gflow_cnt = 0; 313 memset(&g_nstat, 0, sizeof(struct net_stat)); 314 for (i = 0; i < g_config.mos->num_cores; i++) { 315 if (running[i]) { 316 PrintThreadNetworkStats(g_mtcp[i], &ns); 317 #if NETSTAT_TOTAL 318 gflow_cnt += g_mtcp[i]->flow_cnt; 319 for (j = 0; j < g_config.mos->netdev_table->num; j++) { 320 g_nstat.rx_packets[j] += ns.rx_packets[j]; 321 g_nstat.rx_errors[j] += ns.rx_errors[j]; 322 g_nstat.rx_bytes[j] += ns.rx_bytes[j]; 323 g_nstat.tx_packets[j] += ns.tx_packets[j]; 324 g_nstat.tx_drops[j] += ns.tx_drops[j]; 325 g_nstat.tx_bytes[j] += ns.tx_bytes[j]; 326 } 327 #endif 328 } 329 } 330 #if NETSTAT_TOTAL 331 for (i = 0; i < g_config.mos->netdev_table->num; i++) { 332 if (g_config.mos->netdev_table->ent[i]->stat_print) { 333 fprintf(stderr, "[ ALL ] %s, " 334 "RX: %7llu(pps) (err: %5llu), %5.2lf(Gbps), " 335 "TX: %7llu(pps), %5.2lf(Gbps)\n", 336 g_config.mos->netdev_table->ent[i]->dev_name, 337 (long long unsigned)g_nstat.rx_packets[i], 338 (long long unsigned)g_nstat.rx_errors[i], 339 GBPS(g_nstat.rx_bytes[i]), 340 (long long unsigned)g_nstat.tx_packets[i], 341 GBPS(g_nstat.tx_bytes[i])); 342 total_rx_gbps += GBPS(g_nstat.rx_bytes[i]); 343 total_tx_gbps += GBPS(g_nstat.tx_bytes[i]); 344 stat_print = true; 345 } 346 } 347 if (stat_print) { 348 fprintf(stderr, "[ ALL ] flows: %6u\n", gflow_cnt); 349 if (avg_total_rx_gbps == 0) 350 avg_total_rx_gbps = total_rx_gbps; 351 else 352 avg_total_rx_gbps = avg_total_rx_gbps * 0.6 + total_rx_gbps * 0.4; 353 354 if (avg_total_tx_gbps == 0) 355 avg_total_tx_gbps = total_tx_gbps; 356 else 357 avg_total_tx_gbps = avg_total_tx_gbps * 0.6 + total_tx_gbps * 0.4; 358 359 if (peak_total_rx_gbps < total_rx_gbps) 360 peak_total_rx_gbps = total_rx_gbps; 361 if (peak_total_tx_gbps < total_tx_gbps) 362 peak_total_tx_gbps = total_tx_gbps; 363 364 fprintf(stderr, "[ PEAK ] RX: %5.2lf(Gbps), TX: %5.2lf(Gbps)\n" 365 "[ RECENT AVG ] RX: %5.2lf(Gbps), TX: %5.2lf(Gbps)\n", 366 peak_total_rx_gbps, peak_total_tx_gbps, 367 avg_total_rx_gbps, avg_total_tx_gbps); 368 } 369 #endif 370 371 #if ROUND_STAT 372 memset(&g_runstat, 0, sizeof(struct run_stat)); 373 for (i = 0; i < g_config.mos->num_cores; i++) { 374 if (running[i]) { 375 PrintThreadRoundStats(g_mtcp[i], &rs); 376 #if DBGMSG 377 g_runstat.rounds += rs.rounds; 378 g_runstat.rounds_rx += rs.rounds_rx; 379 g_runstat.rounds_rx_try += rs.rounds_rx_try; 380 g_runstat.rounds_tx += rs.rounds_tx; 381 g_runstat.rounds_tx_try += rs.rounds_tx_try; 382 g_runstat.rounds_select += rs.rounds_select; 383 g_runstat.rounds_select_rx += rs.rounds_select_rx; 384 g_runstat.rounds_select_tx += rs.rounds_select_tx; 385 #endif 386 } 387 } 388 389 TRACE_DBG("[ ALL ] Rounds: %4ldK, " 390 "rx: %3ldK (try: %4ldK), tx: %3ldK (try: %4ldK), " 391 "ps_select: %4ld (rx: %4ld, tx: %4ld)\n", 392 g_runstat.rounds / 1000, g_runstat.rounds_rx / 1000, 393 g_runstat.rounds_rx_try / 1000, g_runstat.rounds_tx / 1000, 394 g_runstat.rounds_tx_try / 1000, g_runstat.rounds_select, 395 g_runstat.rounds_select_rx, g_runstat.rounds_select_tx); 396 #endif /* ROUND_STAT */ 397 398 #if TIME_STAT 399 for (i = 0; i < g_config.mos->num_cores; i++) { 400 if (running[i]) { 401 PrintThreadRoundTime(g_mtcp[i]); 402 } 403 } 404 #endif 405 406 #if EVENT_STAT 407 for (i = 0; i < g_config.mos->num_cores; i++) { 408 if (running[i] && g_mtcp[i]->ep) { 409 PrintEventStat(i, &g_mtcp[i]->ep->stat); 410 } 411 } 412 #endif 413 414 fflush(stderr); 415 } 416 #endif /* NETSTAT */ 417 /*----------------------------------------------------------------------------*/ 418 static inline void 419 FlushMonitorReadEvents(mtcp_manager_t mtcp) 420 { 421 struct event_queue *mtcpq; 422 struct tcp_stream *cur_stream; 423 struct mon_listener *walk; 424 425 /* check if monitor sockets should be passed data */ 426 TAILQ_FOREACH(walk, &mtcp->monitors, link) { 427 if (walk->socket->socktype != MOS_SOCK_MONITOR_STREAM || 428 !(mtcpq = walk->eq)) 429 continue; 430 431 while (mtcpq->num_events > 0) { 432 cur_stream = 433 (struct tcp_stream *)mtcpq->events[mtcpq->start++].ev.data.ptr; 434 /* only read events */ 435 if (cur_stream != NULL && 436 (cur_stream->socket->events | MOS_EPOLLIN)) { 437 if (cur_stream->rcvvar != NULL && 438 cur_stream->rcvvar->rcvbuf != NULL) { 439 /* no need to pass pkt context */ 440 struct socket_map *walk; 441 SOCKQ_FOREACH_START(walk, &cur_stream->msocks) { 442 HandleCallback(mtcp, MOS_NULL, walk, 443 cur_stream->side, NULL, 444 MOS_ON_CONN_NEW_DATA); 445 } SOCKQ_FOREACH_END; 446 } 447 /* reset the actions now */ 448 cur_stream->actions = 0; 449 } 450 if (mtcpq->start >= mtcpq->size) 451 mtcpq->start = 0; 452 mtcpq->num_events--; 453 } 454 } 455 } 456 /*----------------------------------------------------------------------------*/ 457 static inline void 458 FlushBufferedReadEvents(mtcp_manager_t mtcp) 459 { 460 int i; 461 int offset; 462 struct event_queue *mtcpq; 463 struct tcp_stream *cur_stream; 464 465 if (mtcp->ep == NULL) { 466 TRACE_EPOLL("No epoll socket has been registered yet!\n"); 467 return; 468 } else { 469 /* case when mtcpq exists */ 470 mtcpq = mtcp->ep->mtcp_queue; 471 offset = mtcpq->start; 472 } 473 474 /* we will use queued-up epoll read-in events 475 * to trigger buffered read monitor events */ 476 for (i = 0; i < mtcpq->num_events; i++) { 477 cur_stream = mtcp->smap[mtcpq->events[offset++].sockid].stream; 478 /* only read events */ 479 /* Raise new data callback event */ 480 if (cur_stream != NULL && 481 (cur_stream->socket->events | MOS_EPOLLIN)) { 482 if (cur_stream->rcvvar != NULL && 483 cur_stream->rcvvar->rcvbuf != NULL) { 484 /* no need to pass pkt context */ 485 struct socket_map *walk; 486 SOCKQ_FOREACH_START(walk, &cur_stream->msocks) { 487 HandleCallback(mtcp, MOS_NULL, walk, cur_stream->side, 488 NULL, MOS_ON_CONN_NEW_DATA); 489 } SOCKQ_FOREACH_END; 490 } 491 } 492 if (offset >= mtcpq->size) 493 offset = 0; 494 } 495 } 496 /*----------------------------------------------------------------------------*/ 497 static inline void 498 FlushEpollEvents(mtcp_manager_t mtcp, uint32_t cur_ts) 499 { 500 struct mtcp_epoll *ep = mtcp->ep; 501 struct event_queue *usrq = ep->usr_queue; 502 struct event_queue *mtcpq = ep->mtcp_queue; 503 504 pthread_mutex_lock(&ep->epoll_lock); 505 if (ep->mtcp_queue->num_events > 0) { 506 /* while mtcp_queue have events */ 507 /* and usr_queue is not full */ 508 while (mtcpq->num_events > 0 && usrq->num_events < usrq->size) { 509 /* copy the event from mtcp_queue to usr_queue */ 510 usrq->events[usrq->end++] = mtcpq->events[mtcpq->start++]; 511 512 if (usrq->end >= usrq->size) 513 usrq->end = 0; 514 usrq->num_events++; 515 516 if (mtcpq->start >= mtcpq->size) 517 mtcpq->start = 0; 518 mtcpq->num_events--; 519 } 520 } 521 522 /* if there are pending events, wake up user */ 523 if (ep->waiting && (ep->usr_queue->num_events > 0 || 524 ep->usr_shadow_queue->num_events > 0)) { 525 STAT_COUNT(mtcp->runstat.rounds_epoll); 526 TRACE_EPOLL("Broadcasting events. num: %d, cur_ts: %u, prev_ts: %u\n", 527 ep->usr_queue->num_events, cur_ts, mtcp->ts_last_event); 528 mtcp->ts_last_event = cur_ts; 529 ep->stat.wakes++; 530 pthread_cond_signal(&ep->epoll_cond); 531 } 532 pthread_mutex_unlock(&ep->epoll_lock); 533 } 534 /*----------------------------------------------------------------------------*/ 535 static inline void 536 HandleApplicationCalls(mtcp_manager_t mtcp, uint32_t cur_ts) 537 { 538 tcp_stream *stream; 539 int cnt, max_cnt; 540 int handled, delayed; 541 int control, send, ack; 542 543 /* connect handling */ 544 while ((stream = StreamDequeue(mtcp->connectq))) { 545 if (stream->state != TCP_ST_SYN_SENT) { 546 TRACE_INFO("Got a connection request from app with state: %s", 547 TCPStateToString(stream)); 548 exit(EXIT_FAILURE); 549 } else { 550 stream->cb_events |= MOS_ON_CONN_START | 551 MOS_ON_TCP_STATE_CHANGE; 552 /* if monitor is on... */ 553 if (stream->pair_stream != NULL) 554 stream->pair_stream->cb_events |= 555 MOS_ON_CONN_START; 556 } 557 AddtoControlList(mtcp, stream, cur_ts); 558 } 559 560 /* send queue handling */ 561 while ((stream = StreamDequeue(mtcp->sendq))) { 562 stream->sndvar->on_sendq = FALSE; 563 AddtoSendList(mtcp, stream); 564 } 565 566 /* ack queue handling */ 567 while ((stream = StreamDequeue(mtcp->ackq))) { 568 stream->sndvar->on_ackq = FALSE; 569 EnqueueACK(mtcp, stream, cur_ts, ACK_OPT_AGGREGATE); 570 } 571 572 /* close handling */ 573 handled = delayed = 0; 574 control = send = ack = 0; 575 while ((stream = StreamDequeue(mtcp->closeq))) { 576 struct tcp_send_vars *sndvar = stream->sndvar; 577 sndvar->on_closeq = FALSE; 578 579 if (sndvar->sndbuf) { 580 sndvar->fss = sndvar->sndbuf->head_seq + sndvar->sndbuf->len; 581 } else { 582 sndvar->fss = stream->snd_nxt; 583 } 584 585 if (g_config.mos->tcp_timeout > 0) 586 RemoveFromTimeoutList(mtcp, stream); 587 588 if (stream->have_reset) { 589 handled++; 590 if (stream->state != TCP_ST_CLOSED_RSVD) { 591 stream->close_reason = TCP_RESET; 592 stream->state = TCP_ST_CLOSED_RSVD; 593 stream->cb_events |= MOS_ON_TCP_STATE_CHANGE; 594 TRACE_STATE("Stream %d: TCP_ST_CLOSED_RSVD\n", stream->id); 595 DestroyTCPStream(mtcp, stream); 596 } else { 597 TRACE_ERROR("Stream already closed.\n"); 598 } 599 600 } else if (sndvar->on_control_list) { 601 sndvar->on_closeq_int = TRUE; 602 StreamInternalEnqueue(mtcp->closeq_int, stream); 603 delayed++; 604 if (sndvar->on_control_list) 605 control++; 606 if (sndvar->on_send_list) 607 send++; 608 if (sndvar->on_ack_list) 609 ack++; 610 611 } else if (sndvar->on_send_list || sndvar->on_ack_list) { 612 handled++; 613 if (stream->state == TCP_ST_ESTABLISHED) { 614 stream->state = TCP_ST_FIN_WAIT_1; 615 stream->cb_events |= MOS_ON_TCP_STATE_CHANGE; 616 TRACE_STATE("Stream %d: TCP_ST_FIN_WAIT_1\n", stream->id); 617 618 } else if (stream->state == TCP_ST_CLOSE_WAIT) { 619 stream->state = TCP_ST_LAST_ACK; 620 stream->cb_events |= MOS_ON_TCP_STATE_CHANGE; 621 TRACE_STATE("Stream %d: TCP_ST_LAST_ACK\n", stream->id); 622 } 623 stream->control_list_waiting = TRUE; 624 625 } else if (stream->state != TCP_ST_CLOSED_RSVD) { 626 handled++; 627 if (stream->state == TCP_ST_ESTABLISHED) { 628 stream->state = TCP_ST_FIN_WAIT_1; 629 stream->cb_events |= MOS_ON_TCP_STATE_CHANGE; 630 TRACE_STATE("Stream %d: TCP_ST_FIN_WAIT_1\n", stream->id); 631 632 } else if (stream->state == TCP_ST_CLOSE_WAIT) { 633 stream->state = TCP_ST_LAST_ACK; 634 stream->cb_events |= MOS_ON_TCP_STATE_CHANGE; 635 TRACE_STATE("Stream %d: TCP_ST_LAST_ACK\n", stream->id); 636 } 637 //sndvar->rto = TCP_FIN_RTO; 638 //UpdateRetransmissionTimer(mtcp, stream, mtcp->cur_ts); 639 AddtoControlList(mtcp, stream, cur_ts); 640 } else { 641 TRACE_ERROR("Already closed connection!\n"); 642 } 643 } 644 TRACE_ROUND("Handling close connections. cnt: %d\n", cnt); 645 646 cnt = 0; 647 max_cnt = mtcp->closeq_int->count; 648 while (cnt++ < max_cnt) { 649 stream = StreamInternalDequeue(mtcp->closeq_int); 650 651 if (stream->sndvar->on_control_list) { 652 StreamInternalEnqueue(mtcp->closeq_int, stream); 653 654 } else if (stream->state != TCP_ST_CLOSED_RSVD) { 655 handled++; 656 stream->sndvar->on_closeq_int = FALSE; 657 if (stream->state == TCP_ST_ESTABLISHED) { 658 stream->state = TCP_ST_FIN_WAIT_1; 659 stream->cb_events |= MOS_ON_TCP_STATE_CHANGE; 660 TRACE_STATE("Stream %d: TCP_ST_FIN_WAIT_1\n", stream->id); 661 662 } else if (stream->state == TCP_ST_CLOSE_WAIT) { 663 stream->state = TCP_ST_LAST_ACK; 664 stream->cb_events |= MOS_ON_TCP_STATE_CHANGE; 665 TRACE_STATE("Stream %d: TCP_ST_LAST_ACK\n", stream->id); 666 } 667 AddtoControlList(mtcp, stream, cur_ts); 668 } else { 669 stream->sndvar->on_closeq_int = FALSE; 670 TRACE_ERROR("Already closed connection!\n"); 671 } 672 } 673 674 /* reset handling */ 675 while ((stream = StreamDequeue(mtcp->resetq))) { 676 stream->sndvar->on_resetq = FALSE; 677 678 if (g_config.mos->tcp_timeout > 0) 679 RemoveFromTimeoutList(mtcp, stream); 680 681 if (stream->have_reset) { 682 if (stream->state != TCP_ST_CLOSED_RSVD) { 683 stream->close_reason = TCP_RESET; 684 stream->state = TCP_ST_CLOSED_RSVD; 685 stream->cb_events |= MOS_ON_TCP_STATE_CHANGE; 686 TRACE_STATE("Stream %d: TCP_ST_CLOSED_RSVD\n", stream->id); 687 DestroyTCPStream(mtcp, stream); 688 } else { 689 TRACE_ERROR("Stream already closed.\n"); 690 } 691 692 } else if (stream->sndvar->on_control_list || 693 stream->sndvar->on_send_list || stream->sndvar->on_ack_list) { 694 /* wait until all the queues are flushed */ 695 stream->sndvar->on_resetq_int = TRUE; 696 StreamInternalEnqueue(mtcp->resetq_int, stream); 697 698 } else { 699 if (stream->state != TCP_ST_CLOSED_RSVD) { 700 stream->close_reason = TCP_ACTIVE_CLOSE; 701 stream->state = TCP_ST_CLOSED_RSVD; 702 stream->cb_events |= MOS_ON_TCP_STATE_CHANGE; 703 TRACE_STATE("Stream %d: TCP_ST_CLOSED_RSVD\n", stream->id); 704 AddtoControlList(mtcp, stream, cur_ts); 705 } else { 706 TRACE_ERROR("Stream already closed.\n"); 707 } 708 } 709 } 710 TRACE_ROUND("Handling reset connections. cnt: %d\n", cnt); 711 712 cnt = 0; 713 max_cnt = mtcp->resetq_int->count; 714 while (cnt++ < max_cnt) { 715 stream = StreamInternalDequeue(mtcp->resetq_int); 716 717 if (stream->sndvar->on_control_list || 718 stream->sndvar->on_send_list || stream->sndvar->on_ack_list) { 719 /* wait until all the queues are flushed */ 720 StreamInternalEnqueue(mtcp->resetq_int, stream); 721 722 } else { 723 stream->sndvar->on_resetq_int = FALSE; 724 725 if (stream->state != TCP_ST_CLOSED_RSVD) { 726 stream->close_reason = TCP_ACTIVE_CLOSE; 727 stream->state = TCP_ST_CLOSED_RSVD; 728 stream->cb_events |= MOS_ON_TCP_STATE_CHANGE; 729 TRACE_STATE("Stream %d: TCP_ST_CLOSED_RSVD\n", stream->id); 730 AddtoControlList(mtcp, stream, cur_ts); 731 } else { 732 TRACE_ERROR("Stream already closed.\n"); 733 } 734 } 735 } 736 737 /* destroy streams in destroyq */ 738 while ((stream = StreamDequeue(mtcp->destroyq))) { 739 DestroyTCPStream(mtcp, stream); 740 } 741 742 mtcp->wakeup_flag = FALSE; 743 } 744 /*----------------------------------------------------------------------------*/ 745 static inline void 746 WritePacketsToChunks(mtcp_manager_t mtcp, uint32_t cur_ts) 747 { 748 int thresh = g_config.mos->max_concurrency; 749 int i; 750 751 /* Set the threshold to g_config.mos->max_concurrency to send ACK immediately */ 752 /* Otherwise, set to appropriate value (e.g. thresh) */ 753 assert(mtcp->g_sender != NULL); 754 if (mtcp->g_sender->control_list_cnt) 755 WriteTCPControlList(mtcp, mtcp->g_sender, cur_ts, thresh); 756 if (mtcp->g_sender->ack_list_cnt) 757 WriteTCPACKList(mtcp, mtcp->g_sender, cur_ts, thresh); 758 if (mtcp->g_sender->send_list_cnt) 759 WriteTCPDataList(mtcp, mtcp->g_sender, cur_ts, thresh); 760 761 for (i = 0; i < g_config.mos->netdev_table->num; i++) { 762 assert(mtcp->n_sender[i] != NULL); 763 if (mtcp->n_sender[i]->control_list_cnt) 764 WriteTCPControlList(mtcp, mtcp->n_sender[i], cur_ts, thresh); 765 if (mtcp->n_sender[i]->ack_list_cnt) 766 WriteTCPACKList(mtcp, mtcp->n_sender[i], cur_ts, thresh); 767 if (mtcp->n_sender[i]->send_list_cnt) 768 WriteTCPDataList(mtcp, mtcp->n_sender[i], cur_ts, thresh); 769 } 770 } 771 /*----------------------------------------------------------------------------*/ 772 #if TESTING 773 static int 774 DestroyRemainingFlows(mtcp_manager_t mtcp) 775 { 776 struct hashtable *ht = mtcp->tcp_flow_table; 777 tcp_stream *walk; 778 int cnt, i; 779 780 cnt = 0; 781 782 thread_printf(mtcp, mtcp->log_fp, 783 "CPU %d: Flushing remaining flows.\n", mtcp->ctx->cpu); 784 785 for (i = 0; i < NUM_BINS; i++) { 786 TAILQ_FOREACH(walk, &ht->ht_table[i], rcvvar->he_link) { 787 thread_printf(mtcp, mtcp->log_fp, 788 "CPU %d: Destroying stream %d\n", mtcp->ctx->cpu, walk->id); 789 DumpStream(mtcp, walk); 790 DestroyTCPStream(mtcp, walk); 791 cnt++; 792 } 793 } 794 795 return cnt; 796 } 797 #endif 798 /*----------------------------------------------------------------------------*/ 799 static void 800 InterruptApplication(mtcp_manager_t mtcp) 801 { 802 /* interrupt if the mtcp_epoll_wait() is waiting */ 803 if (mtcp->ep) { 804 pthread_mutex_lock(&mtcp->ep->epoll_lock); 805 if (mtcp->ep->waiting) { 806 pthread_cond_signal(&mtcp->ep->epoll_cond); 807 } 808 pthread_mutex_unlock(&mtcp->ep->epoll_lock); 809 } 810 /* interrupt if the accept() is waiting */ 811 if (mtcp->listener) { 812 if (mtcp->listener->socket) { 813 pthread_mutex_lock(&mtcp->listener->accept_lock); 814 if (!(mtcp->listener->socket->opts & MTCP_NONBLOCK)) { 815 pthread_cond_signal(&mtcp->listener->accept_cond); 816 } 817 pthread_mutex_unlock(&mtcp->listener->accept_lock); 818 } 819 } 820 } 821 /*----------------------------------------------------------------------------*/ 822 void 823 RunPassiveLoop(mtcp_manager_t mtcp) 824 { 825 sem_wait(&g_done_sem[mtcp->ctx->cpu]); 826 sem_destroy(&g_done_sem[mtcp->ctx->cpu]); 827 return; 828 } 829 /*----------------------------------------------------------------------------*/ 830 static void 831 RunMainLoop(struct mtcp_thread_context *ctx) 832 { 833 mtcp_manager_t mtcp = ctx->mtcp_manager; 834 int i; 835 int recv_cnt; 836 837 #if E_PSIO 838 int rx_inf, tx_inf; 839 #endif 840 841 struct timeval cur_ts = {0}; 842 uint32_t ts, ts_prev; 843 844 #if TIME_STAT 845 struct timeval prev_ts, processing_ts, tcheck_ts, 846 epoll_ts, handle_ts, xmit_ts, select_ts; 847 #endif 848 int thresh; 849 850 gettimeofday(&cur_ts, NULL); 851 852 #if E_PSIO 853 /* do nothing */ 854 #else 855 #if !USE_CHUNK_BUF 856 /* create packet write chunk */ 857 InitWriteChunks(handle, ctx->w_chunk); 858 for (i = 0; i < ETH_NUM; i++) { 859 ctx->w_chunk[i].cnt = 0; 860 ctx->w_off[i] = 0; 861 ctx->w_cur_idx[i] = 0; 862 } 863 #endif 864 #endif 865 866 TRACE_DBG("CPU %d: mtcp thread running.\n", ctx->cpu); 867 868 #if TIME_STAT 869 prev_ts = cur_ts; 870 InitStatCounter(&mtcp->rtstat.round); 871 InitStatCounter(&mtcp->rtstat.processing); 872 InitStatCounter(&mtcp->rtstat.tcheck); 873 InitStatCounter(&mtcp->rtstat.epoll); 874 InitStatCounter(&mtcp->rtstat.handle); 875 InitStatCounter(&mtcp->rtstat.xmit); 876 InitStatCounter(&mtcp->rtstat.select); 877 #endif 878 879 ts = ts_prev = 0; 880 while ((!ctx->done || mtcp->flow_cnt) && !ctx->exit) { 881 882 STAT_COUNT(mtcp->runstat.rounds); 883 recv_cnt = 0; 884 885 #if E_PSIO 886 #if 0 887 event.timeout = PS_SELECT_TIMEOUT; 888 NID_ZERO(event.rx_nids); 889 NID_ZERO(event.tx_nids); 890 NID_ZERO(rx_avail); 891 //NID_ZERO(tx_avail); 892 #endif 893 gettimeofday(&cur_ts, NULL); 894 #if TIME_STAT 895 /* measure the inter-round delay */ 896 UpdateStatCounter(&mtcp->rtstat.round, TimeDiffUs(&cur_ts, &prev_ts)); 897 prev_ts = cur_ts; 898 #endif 899 900 ts = TIMEVAL_TO_TS(&cur_ts); 901 mtcp->cur_ts = ts; 902 903 for (rx_inf = 0; rx_inf < g_config.mos->netdev_table->num; rx_inf++) { 904 905 recv_cnt = mtcp->iom->recv_pkts(ctx, rx_inf); 906 STAT_COUNT(mtcp->runstat.rounds_rx_try); 907 908 for (i = 0; i < recv_cnt; i++) { 909 uint16_t len; 910 uint8_t *pktbuf; 911 pktbuf = mtcp->iom->get_rptr(mtcp->ctx, rx_inf, i, &len); 912 ProcessPacket(mtcp, rx_inf, i, ts, pktbuf, len); 913 } 914 915 #ifdef ENABLE_DPDKR 916 mtcp->iom->send_pkts(ctx, rx_inf); 917 continue; 918 #endif 919 } 920 STAT_COUNT(mtcp->runstat.rounds_rx); 921 922 #else /* E_PSIO */ 923 gettimeofday(&cur_ts, NULL); 924 ts = TIMEVAL_TO_TS(&cur_ts); 925 mtcp->cur_ts = ts; 926 /* 927 * Read packets into a chunk from NIC 928 * chk_w_idx : next chunk index to write packets from NIC 929 */ 930 931 STAT_COUNT(mtcp->runstat.rounds_rx_try); 932 chunk.cnt = PS_CHUNK_SIZE; 933 recv_cnt = ps_recv_chunk(handle, &chunk); 934 if (recv_cnt < 0) { 935 if (errno != EAGAIN && errno != EINTR) { 936 perror("ps_recv_chunk"); 937 assert(0); 938 } 939 } 940 941 /* 942 * Handling Packets 943 * chk_r_idx : next chunk index to read and handle 944 */ 945 for (i = 0; i < recv_cnt; i++) { 946 ProcessPacket(mtcp, chunk.queue.ifindex, ts, 947 (u_char *)(chunk.buf + chunk.info[i].offset), 948 chunk.info[i].len); 949 } 950 951 if (recv_cnt > 0) 952 STAT_COUNT(mtcp->runstat.rounds_rx); 953 #endif /* E_PSIO */ 954 #if TIME_STAT 955 gettimeofday(&processing_ts, NULL); 956 UpdateStatCounter(&mtcp->rtstat.processing, 957 TimeDiffUs(&processing_ts, &cur_ts)); 958 #endif /* TIME_STAT */ 959 960 /* Handle user defined timeout */ 961 struct timer *walk, *tmp; 962 for (walk = TAILQ_FIRST(&mtcp->timer_list); walk != NULL; walk = tmp) { 963 tmp = TAILQ_NEXT(walk, timer_link); 964 if (TIMEVAL_LT(&cur_ts, &walk->exp)) 965 break; 966 967 struct mtcp_context mctx = {.cpu = ctx->cpu}; 968 walk->cb(&mctx, walk->id, 0, 0 /* FIXME */, NULL); 969 DelTimer(mtcp, walk); 970 } 971 972 /* interaction with application */ 973 if (mtcp->flow_cnt > 0) { 974 975 /* check retransmission timeout and timewait expire */ 976 #if 0 977 thresh = (int)mtcp->flow_cnt / (TS_TO_USEC(PER_STREAM_TCHECK)); 978 assert(thresh >= 0); 979 if (thresh == 0) 980 thresh = 1; 981 if (recv_cnt > 0 && thresh > recv_cnt) 982 thresh = recv_cnt; 983 #else 984 thresh = g_config.mos->max_concurrency; 985 #endif 986 987 /* Eunyoung, you may fix this later 988 * if there is no rcv packet, we will send as much as possible 989 */ 990 if (thresh == -1) 991 thresh = g_config.mos->max_concurrency; 992 993 CheckRtmTimeout(mtcp, ts, thresh); 994 CheckTimewaitExpire(mtcp, ts, thresh); 995 996 if (g_config.mos->tcp_timeout > 0 && ts != ts_prev) { 997 CheckConnectionTimeout(mtcp, ts, thresh); 998 } 999 1000 #if TIME_STAT 1001 } 1002 gettimeofday(&tcheck_ts, NULL); 1003 UpdateStatCounter(&mtcp->rtstat.tcheck, 1004 TimeDiffUs(&tcheck_ts, &processing_ts)); 1005 1006 if (mtcp->flow_cnt > 0) { 1007 #endif /* TIME_STAT */ 1008 1009 } 1010 1011 /* 1012 * before flushing epoll events, call monitor events for 1013 * all registered `read` events 1014 */ 1015 if (mtcp->num_msp > 0) 1016 /* call this when only a standalone monitor is running */ 1017 FlushMonitorReadEvents(mtcp); 1018 1019 /* if epoll is in use, flush all the queued events */ 1020 if (mtcp->ep) { 1021 FlushBufferedReadEvents(mtcp); 1022 FlushEpollEvents(mtcp, ts); 1023 } 1024 #if TIME_STAT 1025 gettimeofday(&epoll_ts, NULL); 1026 UpdateStatCounter(&mtcp->rtstat.epoll, 1027 TimeDiffUs(&epoll_ts, &tcheck_ts)); 1028 #endif /* TIME_STAT */ 1029 1030 if (end_app_exists && mtcp->flow_cnt > 0) { 1031 /* handle stream queues */ 1032 HandleApplicationCalls(mtcp, ts); 1033 } 1034 1035 #ifdef ENABLE_DPDKR 1036 continue; 1037 #endif 1038 1039 #if TIME_STAT 1040 gettimeofday(&handle_ts, NULL); 1041 UpdateStatCounter(&mtcp->rtstat.handle, 1042 TimeDiffUs(&handle_ts, &epoll_ts)); 1043 #endif /* TIME_STAT */ 1044 1045 WritePacketsToChunks(mtcp, ts); 1046 1047 /* send packets from write buffer */ 1048 #if E_PSIO 1049 /* With E_PSIO, send until tx is available */ 1050 int num_dev = g_config.mos->netdev_table->num; 1051 if (likely(mtcp->iom->send_pkts != NULL)) 1052 for (tx_inf = 0; tx_inf < num_dev; tx_inf++) { 1053 mtcp->iom->send_pkts(ctx, tx_inf); 1054 } 1055 1056 #else /* E_PSIO */ 1057 /* Without E_PSIO, try send chunks immediately */ 1058 for (i = 0; i < g_config.mos->netdev_table->num; i++) { 1059 #if USE_CHUNK_BUF 1060 /* in the case of using ps_send_chunk_buf() without E_PSIO */ 1061 ret = FlushSendChunkBuf(mtcp, i); 1062 #else 1063 /* if not using ps_send_chunk_buf() */ 1064 ret = FlushWriteBuffer(ctx, i); 1065 #endif 1066 if (ret < 0) { 1067 TRACE_ERROR("Failed to send chunks.\n"); 1068 } else if (ret > 0) { 1069 STAT_COUNT(mtcp->runstat.rounds_tx); 1070 } 1071 } 1072 #endif /* E_PSIO */ 1073 1074 #if TIME_STAT 1075 gettimeofday(&xmit_ts, NULL); 1076 UpdateStatCounter(&mtcp->rtstat.xmit, 1077 TimeDiffUs(&xmit_ts, &handle_ts)); 1078 #endif /* TIME_STAT */ 1079 1080 if (ts != ts_prev) { 1081 ts_prev = ts; 1082 #ifdef NETSTAT 1083 if (ctx->cpu == printer) { 1084 #ifdef RUN_ARP 1085 ARPTimer(mtcp, ts); 1086 #endif 1087 #ifdef NETSTAT 1088 PrintNetworkStats(mtcp, ts); 1089 #endif 1090 } 1091 #endif /* NETSTAT */ 1092 } 1093 1094 if (mtcp->iom->select) 1095 mtcp->iom->select(ctx); 1096 1097 if (ctx->interrupt) { 1098 InterruptApplication(mtcp); 1099 } 1100 } 1101 1102 #if TESTING 1103 DestroyRemainingFlows(mtcp); 1104 #endif 1105 1106 TRACE_DBG("MTCP thread %d out of main loop.\n", ctx->cpu); 1107 /* flush logs */ 1108 flush_log_data(mtcp); 1109 TRACE_DBG("MTCP thread %d flushed logs.\n", ctx->cpu); 1110 InterruptApplication(mtcp); 1111 TRACE_INFO("MTCP thread %d finished.\n", ctx->cpu); 1112 } 1113 /*----------------------------------------------------------------------------*/ 1114 struct mtcp_sender * 1115 CreateMTCPSender(int ifidx) 1116 { 1117 struct mtcp_sender *sender; 1118 1119 sender = (struct mtcp_sender *)calloc(1, sizeof(struct mtcp_sender)); 1120 if (!sender) { 1121 return NULL; 1122 } 1123 1124 sender->ifidx = ifidx; 1125 1126 TAILQ_INIT(&sender->control_list); 1127 TAILQ_INIT(&sender->send_list); 1128 TAILQ_INIT(&sender->ack_list); 1129 1130 sender->control_list_cnt = 0; 1131 sender->send_list_cnt = 0; 1132 sender->ack_list_cnt = 0; 1133 1134 return sender; 1135 } 1136 /*----------------------------------------------------------------------------*/ 1137 void 1138 DestroyMTCPSender(struct mtcp_sender *sender) 1139 { 1140 free(sender); 1141 } 1142 /*----------------------------------------------------------------------------*/ 1143 static mtcp_manager_t 1144 InitializeMTCPManager(struct mtcp_thread_context* ctx) 1145 { 1146 mtcp_manager_t mtcp; 1147 char log_name[MAX_FILE_NAME]; 1148 int i; 1149 1150 posix_seq_srand((unsigned)pthread_self()); 1151 1152 mtcp = (mtcp_manager_t)calloc(1, sizeof(struct mtcp_manager)); 1153 if (!mtcp) { 1154 perror("malloc"); 1155 CTRACE_ERROR("Failed to allocate mtcp_manager.\n"); 1156 return NULL; 1157 } 1158 g_mtcp[ctx->cpu] = mtcp; 1159 1160 mtcp->tcp_flow_table = CreateHashtable(); 1161 if (!mtcp->tcp_flow_table) { 1162 CTRACE_ERROR("Falied to allocate tcp flow table.\n"); 1163 return NULL; 1164 } 1165 1166 #ifdef HUGEPAGE 1167 #define IS_HUGEPAGE 1 1168 #else 1169 #define IS_HUGEPAGE 0 1170 #endif 1171 if (mon_app_exists) { 1172 /* initialize event callback */ 1173 #ifdef NEWEV 1174 InitEvent(mtcp); 1175 #else 1176 InitEvent(mtcp, NUM_EV_TABLE); 1177 #endif 1178 } 1179 1180 if (!(mtcp->bufseg_pool = MPCreate(sizeof(tcpbufseg_t), 1181 sizeof(tcpbufseg_t) * g_config.mos->max_concurrency * 1182 ((g_config.mos->rmem_size - 1) / UNITBUFSIZE + 1), 0))) { 1183 TRACE_ERROR("Failed to allocate ev_table pool\n"); 1184 exit(0); 1185 } 1186 if (!(mtcp->sockent_pool = MPCreate(sizeof(struct sockent), 1187 sizeof(struct sockent) * g_config.mos->max_concurrency * 3, 0))) { 1188 TRACE_ERROR("Failed to allocate ev_table pool\n"); 1189 exit(0); 1190 } 1191 #ifdef USE_TIMER_POOL 1192 if (!(mtcp->timer_pool = MPCreate(sizeof(struct timer), 1193 sizeof(struct timer) * g_config.mos->max_concurrency * 10, 0))) { 1194 TRACE_ERROR("Failed to allocate ev_table pool\n"); 1195 exit(0); 1196 } 1197 #endif 1198 mtcp->flow_pool = MPCreate(sizeof(tcp_stream), 1199 sizeof(tcp_stream) * g_config.mos->max_concurrency, IS_HUGEPAGE); 1200 if (!mtcp->flow_pool) { 1201 CTRACE_ERROR("Failed to allocate tcp flow pool.\n"); 1202 return NULL; 1203 } 1204 mtcp->rv_pool = MPCreate(sizeof(struct tcp_recv_vars), 1205 sizeof(struct tcp_recv_vars) * g_config.mos->max_concurrency, IS_HUGEPAGE); 1206 if (!mtcp->rv_pool) { 1207 CTRACE_ERROR("Failed to allocate tcp recv variable pool.\n"); 1208 return NULL; 1209 } 1210 mtcp->sv_pool = MPCreate(sizeof(struct tcp_send_vars), 1211 sizeof(struct tcp_send_vars) * g_config.mos->max_concurrency, IS_HUGEPAGE); 1212 if (!mtcp->sv_pool) { 1213 CTRACE_ERROR("Failed to allocate tcp send variable pool.\n"); 1214 return NULL; 1215 } 1216 1217 mtcp->rbm_snd = SBManagerCreate(g_config.mos->wmem_size, g_config.mos->no_ring_buffers, 1218 g_config.mos->max_concurrency); 1219 if (!mtcp->rbm_snd) { 1220 CTRACE_ERROR("Failed to create send ring buffer.\n"); 1221 return NULL; 1222 } 1223 1224 mtcp->smap = (socket_map_t)calloc(g_config.mos->max_concurrency, sizeof(struct socket_map)); 1225 if (!mtcp->smap) { 1226 perror("calloc"); 1227 CTRACE_ERROR("Failed to allocate memory for stream map.\n"); 1228 return NULL; 1229 } 1230 1231 if (mon_app_exists) { 1232 mtcp->msmap = (socket_map_t)calloc(g_config.mos->max_concurrency, sizeof(struct socket_map)); 1233 if (!mtcp->msmap) { 1234 perror("calloc"); 1235 CTRACE_ERROR("Failed to allocate memory for monitor stream map.\n"); 1236 return NULL; 1237 } 1238 1239 for (i = 0; i < g_config.mos->max_concurrency; i++) { 1240 mtcp->msmap[i].monitor_stream = calloc(1, sizeof(struct mon_stream)); 1241 if (!mtcp->msmap[i].monitor_stream) { 1242 perror("calloc"); 1243 CTRACE_ERROR("Failed to allocate memory for monitr stream map\n"); 1244 return NULL; 1245 } 1246 } 1247 } 1248 1249 TAILQ_INIT(&mtcp->timer_list); 1250 TAILQ_INIT(&mtcp->monitors); 1251 1252 TAILQ_INIT(&mtcp->free_smap); 1253 for (i = 0; i < g_config.mos->max_concurrency; i++) { 1254 mtcp->smap[i].id = i; 1255 mtcp->smap[i].socktype = MOS_SOCK_UNUSED; 1256 memset(&mtcp->smap[i].saddr, 0, sizeof(struct sockaddr_in)); 1257 mtcp->smap[i].stream = NULL; 1258 TAILQ_INSERT_TAIL(&mtcp->free_smap, &mtcp->smap[i], link); 1259 } 1260 1261 if (mon_app_exists) { 1262 TAILQ_INIT(&mtcp->free_msmap); 1263 for (i = 0; i < g_config.mos->max_concurrency; i++) { 1264 mtcp->msmap[i].id = i; 1265 mtcp->msmap[i].socktype = MOS_SOCK_UNUSED; 1266 memset(&mtcp->msmap[i].saddr, 0, sizeof(struct sockaddr_in)); 1267 TAILQ_INSERT_TAIL(&mtcp->free_msmap, &mtcp->msmap[i], link); 1268 } 1269 } 1270 1271 mtcp->ctx = ctx; 1272 mtcp->ep = NULL; 1273 1274 snprintf(log_name, MAX_FILE_NAME, "%s/"LOG_FILE_NAME"_%d", 1275 g_config.mos->mos_log, ctx->cpu); 1276 mtcp->log_fp = fopen(log_name, "w+"); 1277 if (!mtcp->log_fp) { 1278 perror("fopen"); 1279 CTRACE_ERROR("Failed to create file for logging. (%s)\n", log_name); 1280 return NULL; 1281 } 1282 mtcp->sp_fd = g_logctx[ctx->cpu]->pair_sp_fd; 1283 mtcp->logger = g_logctx[ctx->cpu]; 1284 1285 mtcp->connectq = CreateStreamQueue(BACKLOG_SIZE); 1286 if (!mtcp->connectq) { 1287 CTRACE_ERROR("Failed to create connect queue.\n"); 1288 return NULL; 1289 } 1290 mtcp->sendq = CreateStreamQueue(g_config.mos->max_concurrency); 1291 if (!mtcp->sendq) { 1292 CTRACE_ERROR("Failed to create send queue.\n"); 1293 return NULL; 1294 } 1295 mtcp->ackq = CreateStreamQueue(g_config.mos->max_concurrency); 1296 if (!mtcp->ackq) { 1297 CTRACE_ERROR("Failed to create ack queue.\n"); 1298 return NULL; 1299 } 1300 mtcp->closeq = CreateStreamQueue(g_config.mos->max_concurrency); 1301 if (!mtcp->closeq) { 1302 CTRACE_ERROR("Failed to create close queue.\n"); 1303 return NULL; 1304 } 1305 mtcp->closeq_int = CreateInternalStreamQueue(g_config.mos->max_concurrency); 1306 if (!mtcp->closeq_int) { 1307 CTRACE_ERROR("Failed to create close queue.\n"); 1308 return NULL; 1309 } 1310 mtcp->resetq = CreateStreamQueue(g_config.mos->max_concurrency); 1311 if (!mtcp->resetq) { 1312 CTRACE_ERROR("Failed to create reset queue.\n"); 1313 return NULL; 1314 } 1315 mtcp->resetq_int = CreateInternalStreamQueue(g_config.mos->max_concurrency); 1316 if (!mtcp->resetq_int) { 1317 CTRACE_ERROR("Failed to create reset queue.\n"); 1318 return NULL; 1319 } 1320 mtcp->destroyq = CreateStreamQueue(g_config.mos->max_concurrency); 1321 if (!mtcp->destroyq) { 1322 CTRACE_ERROR("Failed to create destroy queue.\n"); 1323 return NULL; 1324 } 1325 1326 mtcp->g_sender = CreateMTCPSender(-1); 1327 if (!mtcp->g_sender) { 1328 CTRACE_ERROR("Failed to create global sender structure.\n"); 1329 return NULL; 1330 } 1331 for (i = 0; i < g_config.mos->netdev_table->num; i++) { 1332 mtcp->n_sender[i] = CreateMTCPSender(i); 1333 if (!mtcp->n_sender[i]) { 1334 CTRACE_ERROR("Failed to create per-nic sender structure.\n"); 1335 return NULL; 1336 } 1337 } 1338 1339 mtcp->rto_store = InitRTOHashstore(); 1340 TAILQ_INIT(&mtcp->timewait_list); 1341 TAILQ_INIT(&mtcp->timeout_list); 1342 1343 return mtcp; 1344 } 1345 /*----------------------------------------------------------------------------*/ 1346 static void * 1347 MTCPRunThread(void *arg) 1348 { 1349 mctx_t mctx = (mctx_t)arg; 1350 int cpu = mctx->cpu; 1351 int working; 1352 struct mtcp_manager *mtcp; 1353 struct mtcp_thread_context *ctx; 1354 1355 /* affinitize the thread to this core first */ 1356 mtcp_core_affinitize(cpu); 1357 1358 /* memory alloc after core affinitization would use local memory 1359 most time */ 1360 ctx = calloc(1, sizeof(*ctx)); 1361 if (!ctx) { 1362 perror("calloc"); 1363 TRACE_ERROR("Failed to calloc mtcp context.\n"); 1364 exit(-1); 1365 } 1366 ctx->thread = pthread_self(); 1367 ctx->cpu = cpu; 1368 mtcp = ctx->mtcp_manager = InitializeMTCPManager(ctx); 1369 if (!mtcp) { 1370 TRACE_ERROR("Failed to initialize mtcp manager.\n"); 1371 exit(-1); 1372 } 1373 1374 /* assign mtcp context's underlying I/O module */ 1375 mtcp->iom = current_iomodule_func; 1376 1377 /* I/O initializing */ 1378 if (mtcp->iom->init_handle) 1379 mtcp->iom->init_handle(ctx); 1380 1381 if (pthread_mutex_init(&ctx->flow_pool_lock, NULL)) { 1382 perror("pthread_mutex_init of ctx->flow_pool_lock\n"); 1383 exit(-1); 1384 } 1385 1386 if (pthread_mutex_init(&ctx->socket_pool_lock, NULL)) { 1387 perror("pthread_mutex_init of ctx->socket_pool_lock\n"); 1388 exit(-1); 1389 } 1390 1391 SQ_LOCK_INIT(&ctx->connect_lock, "ctx->connect_lock", exit(-1)); 1392 SQ_LOCK_INIT(&ctx->close_lock, "ctx->close_lock", exit(-1)); 1393 SQ_LOCK_INIT(&ctx->reset_lock, "ctx->reset_lock", exit(-1)); 1394 SQ_LOCK_INIT(&ctx->sendq_lock, "ctx->sendq_lock", exit(-1)); 1395 SQ_LOCK_INIT(&ctx->ackq_lock, "ctx->ackq_lock", exit(-1)); 1396 SQ_LOCK_INIT(&ctx->destroyq_lock, "ctx->destroyq_lock", exit(-1)); 1397 1398 /* remember this context pointer for signal processing */ 1399 g_pctx[cpu] = ctx; 1400 mlockall(MCL_CURRENT); 1401 1402 // attach (nic device, queue) 1403 working = AttachDevice(ctx); 1404 if (working != 0) { 1405 sem_post(&g_init_sem[ctx->cpu]); 1406 TRACE_DBG("MTCP thread %d finished. Not attached any device\n", ctx->cpu); 1407 pthread_exit(NULL); 1408 } 1409 1410 TRACE_DBG("CPU %d: initialization finished.\n", cpu); 1411 sem_post(&g_init_sem[ctx->cpu]); 1412 1413 /* start the main loop */ 1414 RunMainLoop(ctx); 1415 1416 TRACE_DBG("MTCP thread %d finished.\n", ctx->cpu); 1417 1418 /* signaling mTCP thread is done */ 1419 sem_post(&g_done_sem[mctx->cpu]); 1420 1421 //pthread_exit(NULL); 1422 return 0; 1423 } 1424 /*----------------------------------------------------------------------------*/ 1425 #ifdef ENABLE_DPDK 1426 static int MTCPDPDKRunThread(void *arg) 1427 { 1428 MTCPRunThread(arg); 1429 return 0; 1430 } 1431 #endif /* !ENABLE_DPDK */ 1432 /*----------------------------------------------------------------------------*/ 1433 mctx_t 1434 mtcp_create_context(int cpu) 1435 { 1436 mctx_t mctx; 1437 int ret; 1438 1439 if (cpu >= g_config.mos->num_cores) { 1440 TRACE_ERROR("Failed initialize new mtcp context. " 1441 "Requested cpu id %d exceed the number of cores %d configured to use.\n", 1442 cpu, g_config.mos->num_cores); 1443 return NULL; 1444 } 1445 1446 /* check if mtcp_create_context() was already initialized */ 1447 if (g_logctx[cpu] != NULL) { 1448 TRACE_ERROR("%s was already initialized before!\n", 1449 __FUNCTION__); 1450 return NULL; 1451 } 1452 1453 ret = sem_init(&g_init_sem[cpu], 0, 0); 1454 if (ret) { 1455 TRACE_ERROR("Failed initialize init_sem.\n"); 1456 return NULL; 1457 } 1458 1459 ret = sem_init(&g_done_sem[cpu], 0, 0); 1460 if (ret) { 1461 TRACE_ERROR("Failed initialize done_sem.\n"); 1462 return NULL; 1463 } 1464 1465 mctx = (mctx_t)calloc(1, sizeof(struct mtcp_context)); 1466 if (!mctx) { 1467 TRACE_ERROR("Failed to allocate memory for mtcp_context.\n"); 1468 return NULL; 1469 } 1470 mctx->cpu = cpu; 1471 g_ctx[cpu] = mctx; 1472 1473 /* initialize logger */ 1474 g_logctx[cpu] = (struct log_thread_context *) 1475 calloc(1, sizeof(struct log_thread_context)); 1476 if (!g_logctx[cpu]) { 1477 perror("malloc"); 1478 TRACE_ERROR("Failed to allocate memory for log thread context.\n"); 1479 return NULL; 1480 } 1481 InitLogThreadContext(g_logctx[cpu], cpu); 1482 if (pthread_create(&log_thread[cpu], 1483 NULL, ThreadLogMain, (void *)g_logctx[cpu])) { 1484 perror("pthread_create"); 1485 TRACE_ERROR("Failed to create log thread\n"); 1486 return NULL; 1487 } 1488 1489 #ifdef ENABLE_DPDK 1490 /* Wake up mTCP threads (wake up I/O threads) */ 1491 if (current_iomodule_func == &dpdk_module_func) { 1492 int master; 1493 master = rte_get_master_lcore(); 1494 if (master == cpu) { 1495 lcore_config[master].ret = 0; 1496 lcore_config[master].state = FINISHED; 1497 if (pthread_create(&g_thread[cpu], 1498 NULL, MTCPRunThread, (void *)mctx) != 0) { 1499 TRACE_ERROR("pthread_create of mtcp thread failed!\n"); 1500 return NULL; 1501 } 1502 } else 1503 rte_eal_remote_launch(MTCPDPDKRunThread, mctx, cpu); 1504 } else 1505 #endif /* !ENABLE_DPDK */ 1506 { 1507 if (pthread_create(&g_thread[cpu], 1508 NULL, MTCPRunThread, (void *)mctx) != 0) { 1509 TRACE_ERROR("pthread_create of mtcp thread failed!\n"); 1510 return NULL; 1511 } 1512 } 1513 1514 sem_wait(&g_init_sem[cpu]); 1515 sem_destroy(&g_init_sem[cpu]); 1516 1517 running[cpu] = TRUE; 1518 1519 #ifdef NETSTAT 1520 #if NETSTAT_TOTAL 1521 if (printer < 0) { 1522 printer = cpu; 1523 TRACE_INFO("CPU %d is in charge of printing stats.\n", printer); 1524 } 1525 #endif 1526 #endif 1527 1528 return mctx; 1529 } 1530 /*----------------------------------------------------------------------------*/ 1531 /** 1532 * TODO: It currently always returns 0. Add appropriate error return values 1533 */ 1534 int 1535 mtcp_destroy_context(mctx_t mctx) 1536 { 1537 struct mtcp_thread_context *ctx = g_pctx[mctx->cpu]; 1538 struct mtcp_manager *mtcp = ctx->mtcp_manager; 1539 struct log_thread_context *log_ctx = mtcp->logger; 1540 int ret, i; 1541 1542 TRACE_DBG("CPU %d: mtcp_destroy_context()\n", mctx->cpu); 1543 1544 /* close all stream sockets that are still open */ 1545 if (!ctx->exit) { 1546 for (i = 0; i < g_config.mos->max_concurrency; i++) { 1547 if (mtcp->smap[i].socktype == MOS_SOCK_STREAM) { 1548 TRACE_DBG("Closing remaining socket %d (%s)\n", 1549 i, TCPStateToString(mtcp->smap[i].stream)); 1550 DumpStream(mtcp, mtcp->smap[i].stream); 1551 mtcp_close(mctx, i); 1552 } 1553 } 1554 } 1555 1556 ctx->done = 1; 1557 1558 //pthread_kill(g_thread[mctx->cpu], SIGINT); 1559 #ifdef ENABLE_DPDK 1560 ctx->exit = 1; 1561 /* XXX - dpdk logic changes */ 1562 if (current_iomodule_func == &dpdk_module_func) { 1563 int master = rte_get_master_lcore(); 1564 if (master == mctx->cpu) 1565 pthread_join(g_thread[mctx->cpu], NULL); 1566 else 1567 rte_eal_wait_lcore(mctx->cpu); 1568 } else 1569 #endif /* !ENABLE_DPDK */ 1570 { 1571 pthread_join(g_thread[mctx->cpu], NULL); 1572 } 1573 1574 TRACE_INFO("MTCP thread %d joined.\n", mctx->cpu); 1575 running[mctx->cpu] = FALSE; 1576 1577 #ifdef NETSTAT 1578 #if NETSTAT_TOTAL 1579 if (printer == mctx->cpu) { 1580 for (i = 0; i < num_cpus; i++) { 1581 if (i != mctx->cpu && running[i]) { 1582 printer = i; 1583 break; 1584 } 1585 } 1586 } 1587 #endif 1588 #endif 1589 1590 log_ctx->done = 1; 1591 ret = write(log_ctx->pair_sp_fd, "F", 1); 1592 if (ret != 1) 1593 TRACE_ERROR("CPU %d: Fail to signal socket pair\n", mctx->cpu); 1594 1595 pthread_join(log_thread[ctx->cpu], NULL); 1596 fclose(mtcp->log_fp); 1597 TRACE_LOG("Log thread %d joined.\n", mctx->cpu); 1598 1599 if (mtcp->connectq) { 1600 DestroyStreamQueue(mtcp->connectq); 1601 mtcp->connectq = NULL; 1602 } 1603 if (mtcp->sendq) { 1604 DestroyStreamQueue(mtcp->sendq); 1605 mtcp->sendq = NULL; 1606 } 1607 if (mtcp->ackq) { 1608 DestroyStreamQueue(mtcp->ackq); 1609 mtcp->ackq = NULL; 1610 } 1611 if (mtcp->closeq) { 1612 DestroyStreamQueue(mtcp->closeq); 1613 mtcp->closeq = NULL; 1614 } 1615 if (mtcp->closeq_int) { 1616 DestroyInternalStreamQueue(mtcp->closeq_int); 1617 mtcp->closeq_int = NULL; 1618 } 1619 if (mtcp->resetq) { 1620 DestroyStreamQueue(mtcp->resetq); 1621 mtcp->resetq = NULL; 1622 } 1623 if (mtcp->resetq_int) { 1624 DestroyInternalStreamQueue(mtcp->resetq_int); 1625 mtcp->resetq_int = NULL; 1626 } 1627 if (mtcp->destroyq) { 1628 DestroyStreamQueue(mtcp->destroyq); 1629 mtcp->destroyq = NULL; 1630 } 1631 1632 DestroyMTCPSender(mtcp->g_sender); 1633 for (i = 0; i < g_config.mos->netdev_table->num; i++) { 1634 DestroyMTCPSender(mtcp->n_sender[i]); 1635 } 1636 1637 MPDestroy(mtcp->rv_pool); 1638 MPDestroy(mtcp->sv_pool); 1639 MPDestroy(mtcp->flow_pool); 1640 1641 if (mtcp->ap) { 1642 DestroyAddressPool(mtcp->ap); 1643 } 1644 1645 SQ_LOCK_DESTROY(&ctx->connect_lock); 1646 SQ_LOCK_DESTROY(&ctx->close_lock); 1647 SQ_LOCK_DESTROY(&ctx->reset_lock); 1648 SQ_LOCK_DESTROY(&ctx->sendq_lock); 1649 SQ_LOCK_DESTROY(&ctx->ackq_lock); 1650 SQ_LOCK_DESTROY(&ctx->destroyq_lock); 1651 1652 //TRACE_INFO("MTCP thread %d destroyed.\n", mctx->cpu); 1653 if (mtcp->iom->destroy_handle) 1654 mtcp->iom->destroy_handle(ctx); 1655 free(ctx); 1656 free(mctx); 1657 1658 return 0; 1659 } 1660 /*----------------------------------------------------------------------------*/ 1661 mtcp_sighandler_t 1662 mtcp_register_signal(int signum, mtcp_sighandler_t handler) 1663 { 1664 mtcp_sighandler_t prev; 1665 1666 if (signum == SIGINT) { 1667 prev = app_signal_handler; 1668 app_signal_handler = handler; 1669 } else { 1670 if ((prev = signal(signum, handler)) == SIG_ERR) { 1671 perror("signal"); 1672 return SIG_ERR; 1673 } 1674 } 1675 1676 return prev; 1677 } 1678 /*----------------------------------------------------------------------------*/ 1679 int 1680 mtcp_getconf(struct mtcp_conf *conf) 1681 { 1682 int i, j; 1683 1684 if (!conf) 1685 return -1; 1686 1687 conf->num_cores = g_config.mos->num_cores; 1688 conf->max_concurrency = g_config.mos->max_concurrency; 1689 conf->cpu_mask = g_config.mos->cpu_mask; 1690 1691 conf->rcvbuf_size = g_config.mos->rmem_size; 1692 conf->sndbuf_size = g_config.mos->wmem_size; 1693 1694 conf->tcp_timewait = g_config.mos->tcp_tw_interval; 1695 conf->tcp_timeout = g_config.mos->tcp_timeout; 1696 1697 i = 0; 1698 struct conf_block *bwalk; 1699 TAILQ_FOREACH(bwalk, &g_config.app_blkh, link) { 1700 struct app_conf *app_conf = (struct app_conf *)bwalk->conf; 1701 for (j = 0; j < app_conf->app_argc; j++) 1702 conf->app_argv[i][j] = app_conf->app_argv[j]; 1703 conf->app_argc[i] = app_conf->app_argc; 1704 conf->app_cpu_mask[i] = app_conf->cpu_mask; 1705 i++; 1706 } 1707 conf->num_app = i; 1708 1709 return 0; 1710 } 1711 /*----------------------------------------------------------------------------*/ 1712 int 1713 mtcp_setconf(const struct mtcp_conf *conf) 1714 { 1715 if (!conf) 1716 return -1; 1717 1718 g_config.mos->num_cores = conf->num_cores; 1719 g_config.mos->max_concurrency = conf->max_concurrency; 1720 1721 g_config.mos->rmem_size = conf->rcvbuf_size; 1722 g_config.mos->wmem_size = conf->sndbuf_size; 1723 1724 g_config.mos->tcp_tw_interval = conf->tcp_timewait; 1725 g_config.mos->tcp_timeout = conf->tcp_timeout; 1726 1727 TRACE_CONFIG("Configuration updated by mtcp_setconf().\n"); 1728 //PrintConfiguration(); 1729 1730 return 0; 1731 } 1732 /*----------------------------------------------------------------------------*/ 1733 int 1734 mtcp_init(const char *config_file) 1735 { 1736 int i; 1737 int ret; 1738 1739 if (geteuid()) { 1740 TRACE_CONFIG("[CAUTION] Run as root if mlock is necessary.\n"); 1741 } 1742 1743 /* getting cpu and NIC */ 1744 num_cpus = GetNumCPUs(); 1745 assert(num_cpus >= 1); 1746 for (i = 0; i < num_cpus; i++) { 1747 g_mtcp[i] = NULL; 1748 running[i] = FALSE; 1749 sigint_cnt[i] = 0; 1750 } 1751 1752 ret = LoadConfigurationUpperHalf(config_file); 1753 if (ret) { 1754 TRACE_CONFIG("Error occured while loading configuration.\n"); 1755 return -1; 1756 } 1757 1758 #if defined(ENABLE_PSIO) 1759 current_iomodule_func = &ps_module_func; 1760 #elif defined(ENABLE_DPDK) 1761 current_iomodule_func = &dpdk_module_func; 1762 #elif defined(ENABLE_PCAP) 1763 current_iomodule_func = &pcap_module_func; 1764 #elif defined(ENABLE_DPDKR) 1765 current_iomodule_func = &dpdkr_module_func; 1766 #elif defined(ENABLE_NETMAP) 1767 current_iomodule_func = &netmap_module_func; 1768 #endif 1769 1770 if (current_iomodule_func->load_module_upper_half) 1771 current_iomodule_func->load_module_upper_half(); 1772 1773 LoadConfigurationLowerHalf(); 1774 1775 //PrintConfiguration(); 1776 1777 /* TODO: this should be fixed */ 1778 ap = CreateAddressPool(g_config.mos->netdev_table->ent[0]->ip_addr, 1); 1779 if (!ap) { 1780 TRACE_CONFIG("Error occured while creating address pool.\n"); 1781 return -1; 1782 } 1783 1784 //PrintInterfaceInfo(); 1785 //PrintRoutingTable(); 1786 //PrintARPTable(); 1787 InitARPTable(); 1788 1789 if (signal(SIGUSR1, HandleSignal) == SIG_ERR) { 1790 perror("signal, SIGUSR1"); 1791 return -1; 1792 } 1793 if (signal(SIGINT, HandleSignal) == SIG_ERR) { 1794 perror("signal, SIGINT"); 1795 return -1; 1796 } 1797 app_signal_handler = NULL; 1798 1799 printf("load_module(): %p\n", current_iomodule_func); 1800 /* load system-wide io module specs */ 1801 if (current_iomodule_func->load_module_lower_half) 1802 current_iomodule_func->load_module_lower_half(); 1803 1804 GlobInitEvent(); 1805 1806 PrintConf(&g_config); 1807 1808 return 0; 1809 } 1810 /*----------------------------------------------------------------------------*/ 1811 int 1812 mtcp_destroy() 1813 { 1814 int i; 1815 1816 /* wait until all threads are closed */ 1817 for (i = 0; i < num_cpus; i++) { 1818 if (running[i]) { 1819 if (pthread_join(g_thread[i], NULL) != 0) 1820 return -1; 1821 } 1822 } 1823 1824 DestroyAddressPool(ap); 1825 1826 TRACE_INFO("All MTCP threads are joined.\n"); 1827 1828 return 0; 1829 } 1830 /*----------------------------------------------------------------------------*/ 1831