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 #ifndef NEWRB 447 /* re-adjust tcp_ring_buffer now */ 448 RBRemove(mtcp->rbm_rcv, cur_stream->rcvvar->rcvbuf, 449 cur_stream->rcvvar->rcvbuf->merged_len, 450 AT_MTCP, cur_stream->buffer_mgmt); 451 cur_stream->rcvvar->rcv_wnd = 452 cur_stream->rcvvar->rcvbuf->size 453 - 1 - cur_stream->rcvvar->rcvbuf->last_len; 454 #endif 455 } 456 /* reset the actions now */ 457 cur_stream->actions = 0; 458 } 459 if (mtcpq->start >= mtcpq->size) 460 mtcpq->start = 0; 461 mtcpq->num_events--; 462 } 463 } 464 } 465 /*----------------------------------------------------------------------------*/ 466 static inline void 467 FlushBufferedReadEvents(mtcp_manager_t mtcp) 468 { 469 int i; 470 int offset; 471 struct event_queue *mtcpq; 472 struct tcp_stream *cur_stream; 473 474 if (mtcp->ep == NULL) { 475 TRACE_EPOLL("No epoll socket has been registered yet!\n"); 476 return; 477 } else { 478 /* case when mtcpq exists */ 479 mtcpq = mtcp->ep->mtcp_queue; 480 offset = mtcpq->start; 481 } 482 483 /* we will use queued-up epoll read-in events 484 * to trigger buffered read monitor events */ 485 for (i = 0; i < mtcpq->num_events; i++) { 486 cur_stream = mtcp->smap[mtcpq->events[offset++].sockid].stream; 487 /* only read events */ 488 /* Raise new data callback event */ 489 if (cur_stream != NULL && 490 (cur_stream->socket->events | MOS_EPOLLIN)) { 491 if (cur_stream->rcvvar != NULL && 492 cur_stream->rcvvar->rcvbuf != NULL) { 493 /* no need to pass pkt context */ 494 struct socket_map *walk; 495 SOCKQ_FOREACH_START(walk, &cur_stream->msocks) { 496 HandleCallback(mtcp, MOS_NULL, walk, cur_stream->side, 497 NULL, MOS_ON_CONN_NEW_DATA); 498 } SOCKQ_FOREACH_END; 499 } 500 } 501 if (offset >= mtcpq->size) 502 offset = 0; 503 } 504 } 505 /*----------------------------------------------------------------------------*/ 506 static inline void 507 FlushEpollEvents(mtcp_manager_t mtcp, uint32_t cur_ts) 508 { 509 struct mtcp_epoll *ep = mtcp->ep; 510 struct event_queue *usrq = ep->usr_queue; 511 struct event_queue *mtcpq = ep->mtcp_queue; 512 513 pthread_mutex_lock(&ep->epoll_lock); 514 if (ep->mtcp_queue->num_events > 0) { 515 /* while mtcp_queue have events */ 516 /* and usr_queue is not full */ 517 while (mtcpq->num_events > 0 && usrq->num_events < usrq->size) { 518 /* copy the event from mtcp_queue to usr_queue */ 519 usrq->events[usrq->end++] = mtcpq->events[mtcpq->start++]; 520 521 if (usrq->end >= usrq->size) 522 usrq->end = 0; 523 usrq->num_events++; 524 525 if (mtcpq->start >= mtcpq->size) 526 mtcpq->start = 0; 527 mtcpq->num_events--; 528 } 529 } 530 531 /* if there are pending events, wake up user */ 532 if (ep->waiting && (ep->usr_queue->num_events > 0 || 533 ep->usr_shadow_queue->num_events > 0)) { 534 STAT_COUNT(mtcp->runstat.rounds_epoll); 535 TRACE_EPOLL("Broadcasting events. num: %d, cur_ts: %u, prev_ts: %u\n", 536 ep->usr_queue->num_events, cur_ts, mtcp->ts_last_event); 537 mtcp->ts_last_event = cur_ts; 538 ep->stat.wakes++; 539 pthread_cond_signal(&ep->epoll_cond); 540 } 541 pthread_mutex_unlock(&ep->epoll_lock); 542 } 543 /*----------------------------------------------------------------------------*/ 544 static inline void 545 HandleApplicationCalls(mtcp_manager_t mtcp, uint32_t cur_ts) 546 { 547 tcp_stream *stream; 548 int cnt, max_cnt; 549 int handled, delayed; 550 int control, send, ack; 551 552 /* connect handling */ 553 while ((stream = StreamDequeue(mtcp->connectq))) { 554 if (stream->state != TCP_ST_SYN_SENT) { 555 TRACE_INFO("Got a connection request from app with state: %s", 556 TCPStateToString(stream)); 557 exit(EXIT_FAILURE); 558 } else { 559 stream->cb_events |= MOS_ON_CONN_START | 560 MOS_ON_TCP_STATE_CHANGE; 561 /* if monitor is on... */ 562 if (stream->pair_stream != NULL) 563 stream->pair_stream->cb_events |= 564 MOS_ON_CONN_START; 565 } 566 AddtoControlList(mtcp, stream, cur_ts); 567 } 568 569 /* send queue handling */ 570 while ((stream = StreamDequeue(mtcp->sendq))) { 571 stream->sndvar->on_sendq = FALSE; 572 AddtoSendList(mtcp, stream); 573 } 574 575 /* ack queue handling */ 576 while ((stream = StreamDequeue(mtcp->ackq))) { 577 stream->sndvar->on_ackq = FALSE; 578 EnqueueACK(mtcp, stream, cur_ts, ACK_OPT_AGGREGATE); 579 } 580 581 /* close handling */ 582 handled = delayed = 0; 583 control = send = ack = 0; 584 while ((stream = StreamDequeue(mtcp->closeq))) { 585 struct tcp_send_vars *sndvar = stream->sndvar; 586 sndvar->on_closeq = FALSE; 587 588 if (sndvar->sndbuf) { 589 sndvar->fss = sndvar->sndbuf->head_seq + sndvar->sndbuf->len; 590 } else { 591 sndvar->fss = stream->snd_nxt; 592 } 593 594 if (g_config.mos->tcp_timeout > 0) 595 RemoveFromTimeoutList(mtcp, stream); 596 597 if (stream->have_reset) { 598 handled++; 599 if (stream->state != TCP_ST_CLOSED_RSVD) { 600 stream->close_reason = TCP_RESET; 601 stream->state = TCP_ST_CLOSED_RSVD; 602 stream->cb_events |= MOS_ON_TCP_STATE_CHANGE; 603 TRACE_STATE("Stream %d: TCP_ST_CLOSED_RSVD\n", stream->id); 604 DestroyTCPStream(mtcp, stream); 605 } else { 606 TRACE_ERROR("Stream already closed.\n"); 607 } 608 609 } else if (sndvar->on_control_list) { 610 sndvar->on_closeq_int = TRUE; 611 StreamInternalEnqueue(mtcp->closeq_int, stream); 612 delayed++; 613 if (sndvar->on_control_list) 614 control++; 615 if (sndvar->on_send_list) 616 send++; 617 if (sndvar->on_ack_list) 618 ack++; 619 620 } else if (sndvar->on_send_list || sndvar->on_ack_list) { 621 handled++; 622 if (stream->state == TCP_ST_ESTABLISHED) { 623 stream->state = TCP_ST_FIN_WAIT_1; 624 stream->cb_events |= MOS_ON_TCP_STATE_CHANGE; 625 TRACE_STATE("Stream %d: TCP_ST_FIN_WAIT_1\n", stream->id); 626 627 } else if (stream->state == TCP_ST_CLOSE_WAIT) { 628 stream->state = TCP_ST_LAST_ACK; 629 stream->cb_events |= MOS_ON_TCP_STATE_CHANGE; 630 TRACE_STATE("Stream %d: TCP_ST_LAST_ACK\n", stream->id); 631 } 632 stream->control_list_waiting = TRUE; 633 634 } else if (stream->state != TCP_ST_CLOSED_RSVD) { 635 handled++; 636 if (stream->state == TCP_ST_ESTABLISHED) { 637 stream->state = TCP_ST_FIN_WAIT_1; 638 stream->cb_events |= MOS_ON_TCP_STATE_CHANGE; 639 TRACE_STATE("Stream %d: TCP_ST_FIN_WAIT_1\n", stream->id); 640 641 } else if (stream->state == TCP_ST_CLOSE_WAIT) { 642 stream->state = TCP_ST_LAST_ACK; 643 stream->cb_events |= MOS_ON_TCP_STATE_CHANGE; 644 TRACE_STATE("Stream %d: TCP_ST_LAST_ACK\n", stream->id); 645 } 646 //sndvar->rto = TCP_FIN_RTO; 647 //UpdateRetransmissionTimer(mtcp, stream, mtcp->cur_ts); 648 AddtoControlList(mtcp, stream, cur_ts); 649 } else { 650 TRACE_ERROR("Already closed connection!\n"); 651 } 652 } 653 TRACE_ROUND("Handling close connections. cnt: %d\n", cnt); 654 655 cnt = 0; 656 max_cnt = mtcp->closeq_int->count; 657 while (cnt++ < max_cnt) { 658 stream = StreamInternalDequeue(mtcp->closeq_int); 659 660 if (stream->sndvar->on_control_list) { 661 StreamInternalEnqueue(mtcp->closeq_int, stream); 662 663 } else if (stream->state != TCP_ST_CLOSED_RSVD) { 664 handled++; 665 stream->sndvar->on_closeq_int = FALSE; 666 if (stream->state == TCP_ST_ESTABLISHED) { 667 stream->state = TCP_ST_FIN_WAIT_1; 668 stream->cb_events |= MOS_ON_TCP_STATE_CHANGE; 669 TRACE_STATE("Stream %d: TCP_ST_FIN_WAIT_1\n", stream->id); 670 671 } else if (stream->state == TCP_ST_CLOSE_WAIT) { 672 stream->state = TCP_ST_LAST_ACK; 673 stream->cb_events |= MOS_ON_TCP_STATE_CHANGE; 674 TRACE_STATE("Stream %d: TCP_ST_LAST_ACK\n", stream->id); 675 } 676 AddtoControlList(mtcp, stream, cur_ts); 677 } else { 678 stream->sndvar->on_closeq_int = FALSE; 679 TRACE_ERROR("Already closed connection!\n"); 680 } 681 } 682 683 /* reset handling */ 684 while ((stream = StreamDequeue(mtcp->resetq))) { 685 stream->sndvar->on_resetq = FALSE; 686 687 if (g_config.mos->tcp_timeout > 0) 688 RemoveFromTimeoutList(mtcp, stream); 689 690 if (stream->have_reset) { 691 if (stream->state != TCP_ST_CLOSED_RSVD) { 692 stream->close_reason = TCP_RESET; 693 stream->state = TCP_ST_CLOSED_RSVD; 694 stream->cb_events |= MOS_ON_TCP_STATE_CHANGE; 695 TRACE_STATE("Stream %d: TCP_ST_CLOSED_RSVD\n", stream->id); 696 DestroyTCPStream(mtcp, stream); 697 } else { 698 TRACE_ERROR("Stream already closed.\n"); 699 } 700 701 } else if (stream->sndvar->on_control_list || 702 stream->sndvar->on_send_list || stream->sndvar->on_ack_list) { 703 /* wait until all the queues are flushed */ 704 stream->sndvar->on_resetq_int = TRUE; 705 StreamInternalEnqueue(mtcp->resetq_int, stream); 706 707 } else { 708 if (stream->state != TCP_ST_CLOSED_RSVD) { 709 stream->close_reason = TCP_ACTIVE_CLOSE; 710 stream->state = TCP_ST_CLOSED_RSVD; 711 stream->cb_events |= MOS_ON_TCP_STATE_CHANGE; 712 TRACE_STATE("Stream %d: TCP_ST_CLOSED_RSVD\n", stream->id); 713 AddtoControlList(mtcp, stream, cur_ts); 714 } else { 715 TRACE_ERROR("Stream already closed.\n"); 716 } 717 } 718 } 719 TRACE_ROUND("Handling reset connections. cnt: %d\n", cnt); 720 721 cnt = 0; 722 max_cnt = mtcp->resetq_int->count; 723 while (cnt++ < max_cnt) { 724 stream = StreamInternalDequeue(mtcp->resetq_int); 725 726 if (stream->sndvar->on_control_list || 727 stream->sndvar->on_send_list || stream->sndvar->on_ack_list) { 728 /* wait until all the queues are flushed */ 729 StreamInternalEnqueue(mtcp->resetq_int, stream); 730 731 } else { 732 stream->sndvar->on_resetq_int = FALSE; 733 734 if (stream->state != TCP_ST_CLOSED_RSVD) { 735 stream->close_reason = TCP_ACTIVE_CLOSE; 736 stream->state = TCP_ST_CLOSED_RSVD; 737 stream->cb_events |= MOS_ON_TCP_STATE_CHANGE; 738 TRACE_STATE("Stream %d: TCP_ST_CLOSED_RSVD\n", stream->id); 739 AddtoControlList(mtcp, stream, cur_ts); 740 } else { 741 TRACE_ERROR("Stream already closed.\n"); 742 } 743 } 744 } 745 746 /* destroy streams in destroyq */ 747 while ((stream = StreamDequeue(mtcp->destroyq))) { 748 DestroyTCPStream(mtcp, stream); 749 } 750 751 mtcp->wakeup_flag = FALSE; 752 } 753 /*----------------------------------------------------------------------------*/ 754 static inline void 755 WritePacketsToChunks(mtcp_manager_t mtcp, uint32_t cur_ts) 756 { 757 int thresh = g_config.mos->max_concurrency; 758 int i; 759 760 /* Set the threshold to g_config.mos->max_concurrency to send ACK immediately */ 761 /* Otherwise, set to appropriate value (e.g. thresh) */ 762 assert(mtcp->g_sender != NULL); 763 if (mtcp->g_sender->control_list_cnt) 764 WriteTCPControlList(mtcp, mtcp->g_sender, cur_ts, thresh); 765 if (mtcp->g_sender->ack_list_cnt) 766 WriteTCPACKList(mtcp, mtcp->g_sender, cur_ts, thresh); 767 if (mtcp->g_sender->send_list_cnt) 768 WriteTCPDataList(mtcp, mtcp->g_sender, cur_ts, thresh); 769 770 for (i = 0; i < g_config.mos->netdev_table->num; i++) { 771 assert(mtcp->n_sender[i] != NULL); 772 if (mtcp->n_sender[i]->control_list_cnt) 773 WriteTCPControlList(mtcp, mtcp->n_sender[i], cur_ts, thresh); 774 if (mtcp->n_sender[i]->ack_list_cnt) 775 WriteTCPACKList(mtcp, mtcp->n_sender[i], cur_ts, thresh); 776 if (mtcp->n_sender[i]->send_list_cnt) 777 WriteTCPDataList(mtcp, mtcp->n_sender[i], cur_ts, thresh); 778 } 779 } 780 /*----------------------------------------------------------------------------*/ 781 #if TESTING 782 static int 783 DestroyRemainingFlows(mtcp_manager_t mtcp) 784 { 785 struct hashtable *ht = mtcp->tcp_flow_table; 786 tcp_stream *walk; 787 int cnt, i; 788 789 cnt = 0; 790 791 thread_printf(mtcp, mtcp->log_fp, 792 "CPU %d: Flushing remaining flows.\n", mtcp->ctx->cpu); 793 794 for (i = 0; i < NUM_BINS; i++) { 795 TAILQ_FOREACH(walk, &ht->ht_table[i], rcvvar->he_link) { 796 thread_printf(mtcp, mtcp->log_fp, 797 "CPU %d: Destroying stream %d\n", mtcp->ctx->cpu, walk->id); 798 DumpStream(mtcp, walk); 799 DestroyTCPStream(mtcp, walk); 800 cnt++; 801 } 802 } 803 804 return cnt; 805 } 806 #endif 807 /*----------------------------------------------------------------------------*/ 808 static void 809 InterruptApplication(mtcp_manager_t mtcp) 810 { 811 /* interrupt if the mtcp_epoll_wait() is waiting */ 812 if (mtcp->ep) { 813 pthread_mutex_lock(&mtcp->ep->epoll_lock); 814 if (mtcp->ep->waiting) { 815 pthread_cond_signal(&mtcp->ep->epoll_cond); 816 } 817 pthread_mutex_unlock(&mtcp->ep->epoll_lock); 818 } 819 /* interrupt if the accept() is waiting */ 820 if (mtcp->listener) { 821 if (mtcp->listener->socket) { 822 pthread_mutex_lock(&mtcp->listener->accept_lock); 823 if (!(mtcp->listener->socket->opts & MTCP_NONBLOCK)) { 824 pthread_cond_signal(&mtcp->listener->accept_cond); 825 } 826 pthread_mutex_unlock(&mtcp->listener->accept_lock); 827 } 828 } 829 } 830 /*----------------------------------------------------------------------------*/ 831 void 832 RunPassiveLoop(mtcp_manager_t mtcp) 833 { 834 sem_wait(&g_done_sem[mtcp->ctx->cpu]); 835 sem_destroy(&g_done_sem[mtcp->ctx->cpu]); 836 return; 837 } 838 /*----------------------------------------------------------------------------*/ 839 static void 840 RunMainLoop(struct mtcp_thread_context *ctx) 841 { 842 mtcp_manager_t mtcp = ctx->mtcp_manager; 843 int i; 844 int recv_cnt; 845 846 #if E_PSIO 847 int rx_inf, tx_inf; 848 #endif 849 850 struct timeval cur_ts = {0}; 851 uint32_t ts, ts_prev; 852 853 #if TIME_STAT 854 struct timeval prev_ts, processing_ts, tcheck_ts, 855 epoll_ts, handle_ts, xmit_ts, select_ts; 856 #endif 857 int thresh; 858 859 gettimeofday(&cur_ts, NULL); 860 861 #if E_PSIO 862 /* do nothing */ 863 #else 864 #if !USE_CHUNK_BUF 865 /* create packet write chunk */ 866 InitWriteChunks(handle, ctx->w_chunk); 867 for (i = 0; i < ETH_NUM; i++) { 868 ctx->w_chunk[i].cnt = 0; 869 ctx->w_off[i] = 0; 870 ctx->w_cur_idx[i] = 0; 871 } 872 #endif 873 #endif 874 875 TRACE_DBG("CPU %d: mtcp thread running.\n", ctx->cpu); 876 877 #if TIME_STAT 878 prev_ts = cur_ts; 879 InitStatCounter(&mtcp->rtstat.round); 880 InitStatCounter(&mtcp->rtstat.processing); 881 InitStatCounter(&mtcp->rtstat.tcheck); 882 InitStatCounter(&mtcp->rtstat.epoll); 883 InitStatCounter(&mtcp->rtstat.handle); 884 InitStatCounter(&mtcp->rtstat.xmit); 885 InitStatCounter(&mtcp->rtstat.select); 886 #endif 887 888 ts = ts_prev = 0; 889 while ((!ctx->done || mtcp->flow_cnt) && !ctx->exit) { 890 891 STAT_COUNT(mtcp->runstat.rounds); 892 recv_cnt = 0; 893 894 #if E_PSIO 895 #if 0 896 event.timeout = PS_SELECT_TIMEOUT; 897 NID_ZERO(event.rx_nids); 898 NID_ZERO(event.tx_nids); 899 NID_ZERO(rx_avail); 900 //NID_ZERO(tx_avail); 901 #endif 902 gettimeofday(&cur_ts, NULL); 903 #if TIME_STAT 904 /* measure the inter-round delay */ 905 UpdateStatCounter(&mtcp->rtstat.round, TimeDiffUs(&cur_ts, &prev_ts)); 906 prev_ts = cur_ts; 907 #endif 908 909 ts = TIMEVAL_TO_TS(&cur_ts); 910 mtcp->cur_ts = ts; 911 912 for (rx_inf = 0; rx_inf < g_config.mos->netdev_table->num; rx_inf++) { 913 914 recv_cnt = mtcp->iom->recv_pkts(ctx, rx_inf); 915 STAT_COUNT(mtcp->runstat.rounds_rx_try); 916 917 for (i = 0; i < recv_cnt; i++) { 918 uint16_t len; 919 uint8_t *pktbuf; 920 pktbuf = mtcp->iom->get_rptr(mtcp->ctx, rx_inf, i, &len); 921 ProcessPacket(mtcp, rx_inf, i, ts, pktbuf, len); 922 } 923 924 #ifdef ENABLE_DPDKR 925 mtcp->iom->send_pkts(ctx, rx_inf); 926 continue; 927 #endif 928 } 929 STAT_COUNT(mtcp->runstat.rounds_rx); 930 931 #else /* E_PSIO */ 932 gettimeofday(&cur_ts, NULL); 933 ts = TIMEVAL_TO_TS(&cur_ts); 934 mtcp->cur_ts = ts; 935 /* 936 * Read packets into a chunk from NIC 937 * chk_w_idx : next chunk index to write packets from NIC 938 */ 939 940 STAT_COUNT(mtcp->runstat.rounds_rx_try); 941 chunk.cnt = PS_CHUNK_SIZE; 942 recv_cnt = ps_recv_chunk(handle, &chunk); 943 if (recv_cnt < 0) { 944 if (errno != EAGAIN && errno != EINTR) { 945 perror("ps_recv_chunk"); 946 assert(0); 947 } 948 } 949 950 /* 951 * Handling Packets 952 * chk_r_idx : next chunk index to read and handle 953 */ 954 for (i = 0; i < recv_cnt; i++) { 955 ProcessPacket(mtcp, chunk.queue.ifindex, ts, 956 (u_char *)(chunk.buf + chunk.info[i].offset), 957 chunk.info[i].len); 958 } 959 960 if (recv_cnt > 0) 961 STAT_COUNT(mtcp->runstat.rounds_rx); 962 #endif /* E_PSIO */ 963 #if TIME_STAT 964 gettimeofday(&processing_ts, NULL); 965 UpdateStatCounter(&mtcp->rtstat.processing, 966 TimeDiffUs(&processing_ts, &cur_ts)); 967 #endif /* TIME_STAT */ 968 969 /* Handle user defined timeout */ 970 struct timer *walk, *tmp; 971 for (walk = TAILQ_FIRST(&mtcp->timer_list); walk != NULL; walk = tmp) { 972 tmp = TAILQ_NEXT(walk, timer_link); 973 if (TIMEVAL_LT(&cur_ts, &walk->exp)) 974 break; 975 976 struct mtcp_context mctx = {.cpu = ctx->cpu}; 977 walk->cb(&mctx, walk->id, 0, 0 /* FIXME */, NULL); 978 DelTimer(mtcp, walk); 979 } 980 981 /* interaction with application */ 982 if (mtcp->flow_cnt > 0) { 983 984 /* check retransmission timeout and timewait expire */ 985 #if 0 986 thresh = (int)mtcp->flow_cnt / (TS_TO_USEC(PER_STREAM_TCHECK)); 987 assert(thresh >= 0); 988 if (thresh == 0) 989 thresh = 1; 990 if (recv_cnt > 0 && thresh > recv_cnt) 991 thresh = recv_cnt; 992 #else 993 thresh = g_config.mos->max_concurrency; 994 #endif 995 996 /* Eunyoung, you may fix this later 997 * if there is no rcv packet, we will send as much as possible 998 */ 999 if (thresh == -1) 1000 thresh = g_config.mos->max_concurrency; 1001 1002 CheckRtmTimeout(mtcp, ts, thresh); 1003 CheckTimewaitExpire(mtcp, ts, thresh); 1004 1005 if (g_config.mos->tcp_timeout > 0 && ts != ts_prev) { 1006 CheckConnectionTimeout(mtcp, ts, thresh); 1007 } 1008 1009 #if TIME_STAT 1010 } 1011 gettimeofday(&tcheck_ts, NULL); 1012 UpdateStatCounter(&mtcp->rtstat.tcheck, 1013 TimeDiffUs(&tcheck_ts, &processing_ts)); 1014 1015 if (mtcp->flow_cnt > 0) { 1016 #endif /* TIME_STAT */ 1017 1018 } 1019 1020 /* 1021 * before flushing epoll events, call monitor events for 1022 * all registered `read` events 1023 */ 1024 if (mtcp->num_msp > 0) 1025 /* call this when only a standalone monitor is running */ 1026 FlushMonitorReadEvents(mtcp); 1027 1028 /* if epoll is in use, flush all the queued events */ 1029 if (mtcp->ep) { 1030 FlushBufferedReadEvents(mtcp); 1031 FlushEpollEvents(mtcp, ts); 1032 } 1033 #if TIME_STAT 1034 gettimeofday(&epoll_ts, NULL); 1035 UpdateStatCounter(&mtcp->rtstat.epoll, 1036 TimeDiffUs(&epoll_ts, &tcheck_ts)); 1037 #endif /* TIME_STAT */ 1038 1039 if (end_app_exists && mtcp->flow_cnt > 0) { 1040 /* handle stream queues */ 1041 HandleApplicationCalls(mtcp, ts); 1042 } 1043 1044 #ifdef ENABLE_DPDKR 1045 continue; 1046 #endif 1047 1048 #if TIME_STAT 1049 gettimeofday(&handle_ts, NULL); 1050 UpdateStatCounter(&mtcp->rtstat.handle, 1051 TimeDiffUs(&handle_ts, &epoll_ts)); 1052 #endif /* TIME_STAT */ 1053 1054 WritePacketsToChunks(mtcp, ts); 1055 1056 /* send packets from write buffer */ 1057 #if E_PSIO 1058 /* With E_PSIO, send until tx is available */ 1059 int num_dev = g_config.mos->netdev_table->num; 1060 if (likely(mtcp->iom->send_pkts != NULL)) 1061 for (tx_inf = 0; tx_inf < num_dev; tx_inf++) { 1062 mtcp->iom->send_pkts(ctx, tx_inf); 1063 } 1064 1065 #else /* E_PSIO */ 1066 /* Without E_PSIO, try send chunks immediately */ 1067 for (i = 0; i < g_config.mos->netdev_table->num; i++) { 1068 #if USE_CHUNK_BUF 1069 /* in the case of using ps_send_chunk_buf() without E_PSIO */ 1070 ret = FlushSendChunkBuf(mtcp, i); 1071 #else 1072 /* if not using ps_send_chunk_buf() */ 1073 ret = FlushWriteBuffer(ctx, i); 1074 #endif 1075 if (ret < 0) { 1076 TRACE_ERROR("Failed to send chunks.\n"); 1077 } else if (ret > 0) { 1078 STAT_COUNT(mtcp->runstat.rounds_tx); 1079 } 1080 } 1081 #endif /* E_PSIO */ 1082 1083 #if TIME_STAT 1084 gettimeofday(&xmit_ts, NULL); 1085 UpdateStatCounter(&mtcp->rtstat.xmit, 1086 TimeDiffUs(&xmit_ts, &handle_ts)); 1087 #endif /* TIME_STAT */ 1088 1089 if (ts != ts_prev) { 1090 ts_prev = ts; 1091 #ifdef NETSTAT 1092 if (ctx->cpu == printer) { 1093 #ifdef RUN_ARP 1094 ARPTimer(mtcp, ts); 1095 #endif 1096 PrintNetworkStats(mtcp, ts); 1097 } 1098 #endif /* NETSTAT */ 1099 } 1100 1101 if (mtcp->iom->select) 1102 mtcp->iom->select(ctx); 1103 1104 if (ctx->interrupt) { 1105 InterruptApplication(mtcp); 1106 } 1107 } 1108 1109 #if TESTING 1110 DestroyRemainingFlows(mtcp); 1111 #endif 1112 1113 TRACE_DBG("MTCP thread %d out of main loop.\n", ctx->cpu); 1114 /* flush logs */ 1115 flush_log_data(mtcp); 1116 TRACE_DBG("MTCP thread %d flushed logs.\n", ctx->cpu); 1117 InterruptApplication(mtcp); 1118 TRACE_INFO("MTCP thread %d finished.\n", ctx->cpu); 1119 } 1120 /*----------------------------------------------------------------------------*/ 1121 struct mtcp_sender * 1122 CreateMTCPSender(int ifidx) 1123 { 1124 struct mtcp_sender *sender; 1125 1126 sender = (struct mtcp_sender *)calloc(1, sizeof(struct mtcp_sender)); 1127 if (!sender) { 1128 return NULL; 1129 } 1130 1131 sender->ifidx = ifidx; 1132 1133 TAILQ_INIT(&sender->control_list); 1134 TAILQ_INIT(&sender->send_list); 1135 TAILQ_INIT(&sender->ack_list); 1136 1137 sender->control_list_cnt = 0; 1138 sender->send_list_cnt = 0; 1139 sender->ack_list_cnt = 0; 1140 1141 return sender; 1142 } 1143 /*----------------------------------------------------------------------------*/ 1144 void 1145 DestroyMTCPSender(struct mtcp_sender *sender) 1146 { 1147 free(sender); 1148 } 1149 /*----------------------------------------------------------------------------*/ 1150 static mtcp_manager_t 1151 InitializeMTCPManager(struct mtcp_thread_context* ctx) 1152 { 1153 mtcp_manager_t mtcp; 1154 char log_name[MAX_FILE_NAME]; 1155 int i; 1156 1157 posix_seq_srand((unsigned)pthread_self()); 1158 1159 mtcp = (mtcp_manager_t)calloc(1, sizeof(struct mtcp_manager)); 1160 if (!mtcp) { 1161 perror("malloc"); 1162 CTRACE_ERROR("Failed to allocate mtcp_manager.\n"); 1163 return NULL; 1164 } 1165 g_mtcp[ctx->cpu] = mtcp; 1166 1167 mtcp->tcp_flow_table = CreateHashtable(); 1168 if (!mtcp->tcp_flow_table) { 1169 CTRACE_ERROR("Falied to allocate tcp flow table.\n"); 1170 return NULL; 1171 } 1172 1173 #ifdef HUGEPAGE 1174 #define IS_HUGEPAGE 1 1175 #else 1176 #define IS_HUGEPAGE 0 1177 #endif 1178 if (mon_app_exists) { 1179 /* initialize event callback */ 1180 #ifdef NEWEV 1181 InitEvent(mtcp); 1182 #else 1183 InitEvent(mtcp, NUM_EV_TABLE); 1184 #endif 1185 } 1186 1187 if (!(mtcp->bufseg_pool = MPCreate(sizeof(tcpbufseg_t), 1188 sizeof(tcpbufseg_t) * g_config.mos->max_concurrency * 1189 ((g_config.mos->rmem_size - 1) / UNITBUFSIZE + 1), 0))) { 1190 TRACE_ERROR("Failed to allocate ev_table pool\n"); 1191 exit(0); 1192 } 1193 if (!(mtcp->sockent_pool = MPCreate(sizeof(struct sockent), 1194 sizeof(struct sockent) * g_config.mos->max_concurrency * 3, 0))) { 1195 TRACE_ERROR("Failed to allocate ev_table pool\n"); 1196 exit(0); 1197 } 1198 #ifdef USE_TIMER_POOL 1199 if (!(mtcp->timer_pool = MPCreate(sizeof(struct timer), 1200 sizeof(struct timer) * g_config.mos->max_concurrency * 10, 0))) { 1201 TRACE_ERROR("Failed to allocate ev_table pool\n"); 1202 exit(0); 1203 } 1204 #endif 1205 mtcp->flow_pool = MPCreate(sizeof(tcp_stream), 1206 sizeof(tcp_stream) * g_config.mos->max_concurrency, IS_HUGEPAGE); 1207 if (!mtcp->flow_pool) { 1208 CTRACE_ERROR("Failed to allocate tcp flow pool.\n"); 1209 return NULL; 1210 } 1211 mtcp->rv_pool = MPCreate(sizeof(struct tcp_recv_vars), 1212 sizeof(struct tcp_recv_vars) * g_config.mos->max_concurrency, IS_HUGEPAGE); 1213 if (!mtcp->rv_pool) { 1214 CTRACE_ERROR("Failed to allocate tcp recv variable pool.\n"); 1215 return NULL; 1216 } 1217 mtcp->sv_pool = MPCreate(sizeof(struct tcp_send_vars), 1218 sizeof(struct tcp_send_vars) * g_config.mos->max_concurrency, IS_HUGEPAGE); 1219 if (!mtcp->sv_pool) { 1220 CTRACE_ERROR("Failed to allocate tcp send variable pool.\n"); 1221 return NULL; 1222 } 1223 1224 mtcp->rbm_snd = SBManagerCreate(g_config.mos->wmem_size, g_config.mos->no_ring_buffers, 1225 g_config.mos->max_concurrency); 1226 if (!mtcp->rbm_snd) { 1227 CTRACE_ERROR("Failed to create send ring buffer.\n"); 1228 return NULL; 1229 } 1230 #ifndef NEWRB 1231 mtcp->rbm_rcv = RBManagerCreate(g_config.mos->rmem_size, g_config.mos->no_ring_buffers, 1232 g_config.mos->max_concurrency); 1233 if (!mtcp->rbm_rcv) { 1234 CTRACE_ERROR("Failed to create recv ring buffer.\n"); 1235 return NULL; 1236 } 1237 #endif 1238 1239 mtcp->smap = (socket_map_t)calloc(g_config.mos->max_concurrency, sizeof(struct socket_map)); 1240 if (!mtcp->smap) { 1241 perror("calloc"); 1242 CTRACE_ERROR("Failed to allocate memory for stream map.\n"); 1243 return NULL; 1244 } 1245 1246 if (mon_app_exists) { 1247 mtcp->msmap = (socket_map_t)calloc(g_config.mos->max_concurrency, sizeof(struct socket_map)); 1248 if (!mtcp->msmap) { 1249 perror("calloc"); 1250 CTRACE_ERROR("Failed to allocate memory for monitor stream map.\n"); 1251 return NULL; 1252 } 1253 1254 for (i = 0; i < g_config.mos->max_concurrency; i++) { 1255 mtcp->msmap[i].monitor_stream = calloc(1, sizeof(struct mon_stream)); 1256 if (!mtcp->msmap[i].monitor_stream) { 1257 perror("calloc"); 1258 CTRACE_ERROR("Failed to allocate memory for monitr stream map\n"); 1259 return NULL; 1260 } 1261 } 1262 } 1263 1264 TAILQ_INIT(&mtcp->timer_list); 1265 TAILQ_INIT(&mtcp->monitors); 1266 1267 TAILQ_INIT(&mtcp->free_smap); 1268 for (i = 0; i < g_config.mos->max_concurrency; i++) { 1269 mtcp->smap[i].id = i; 1270 mtcp->smap[i].socktype = MOS_SOCK_UNUSED; 1271 memset(&mtcp->smap[i].saddr, 0, sizeof(struct sockaddr_in)); 1272 mtcp->smap[i].stream = NULL; 1273 TAILQ_INSERT_TAIL(&mtcp->free_smap, &mtcp->smap[i], link); 1274 } 1275 1276 if (mon_app_exists) { 1277 TAILQ_INIT(&mtcp->free_msmap); 1278 for (i = 0; i < g_config.mos->max_concurrency; i++) { 1279 mtcp->msmap[i].id = i; 1280 mtcp->msmap[i].socktype = MOS_SOCK_UNUSED; 1281 memset(&mtcp->msmap[i].saddr, 0, sizeof(struct sockaddr_in)); 1282 TAILQ_INSERT_TAIL(&mtcp->free_msmap, &mtcp->msmap[i], link); 1283 } 1284 } 1285 1286 mtcp->ctx = ctx; 1287 mtcp->ep = NULL; 1288 1289 snprintf(log_name, MAX_FILE_NAME, "%s/"LOG_FILE_NAME"_%d", 1290 g_config.mos->mos_log, ctx->cpu); 1291 mtcp->log_fp = fopen(log_name, "w+"); 1292 if (!mtcp->log_fp) { 1293 perror("fopen"); 1294 CTRACE_ERROR("Failed to create file for logging. (%s)\n", log_name); 1295 return NULL; 1296 } 1297 mtcp->sp_fd = g_logctx[ctx->cpu]->pair_sp_fd; 1298 mtcp->logger = g_logctx[ctx->cpu]; 1299 1300 mtcp->connectq = CreateStreamQueue(BACKLOG_SIZE); 1301 if (!mtcp->connectq) { 1302 CTRACE_ERROR("Failed to create connect queue.\n"); 1303 return NULL; 1304 } 1305 mtcp->sendq = CreateStreamQueue(g_config.mos->max_concurrency); 1306 if (!mtcp->sendq) { 1307 CTRACE_ERROR("Failed to create send queue.\n"); 1308 return NULL; 1309 } 1310 mtcp->ackq = CreateStreamQueue(g_config.mos->max_concurrency); 1311 if (!mtcp->ackq) { 1312 CTRACE_ERROR("Failed to create ack queue.\n"); 1313 return NULL; 1314 } 1315 mtcp->closeq = CreateStreamQueue(g_config.mos->max_concurrency); 1316 if (!mtcp->closeq) { 1317 CTRACE_ERROR("Failed to create close queue.\n"); 1318 return NULL; 1319 } 1320 mtcp->closeq_int = CreateInternalStreamQueue(g_config.mos->max_concurrency); 1321 if (!mtcp->closeq_int) { 1322 CTRACE_ERROR("Failed to create close queue.\n"); 1323 return NULL; 1324 } 1325 mtcp->resetq = CreateStreamQueue(g_config.mos->max_concurrency); 1326 if (!mtcp->resetq) { 1327 CTRACE_ERROR("Failed to create reset queue.\n"); 1328 return NULL; 1329 } 1330 mtcp->resetq_int = CreateInternalStreamQueue(g_config.mos->max_concurrency); 1331 if (!mtcp->resetq_int) { 1332 CTRACE_ERROR("Failed to create reset queue.\n"); 1333 return NULL; 1334 } 1335 mtcp->destroyq = CreateStreamQueue(g_config.mos->max_concurrency); 1336 if (!mtcp->destroyq) { 1337 CTRACE_ERROR("Failed to create destroy queue.\n"); 1338 return NULL; 1339 } 1340 1341 mtcp->g_sender = CreateMTCPSender(-1); 1342 if (!mtcp->g_sender) { 1343 CTRACE_ERROR("Failed to create global sender structure.\n"); 1344 return NULL; 1345 } 1346 for (i = 0; i < g_config.mos->netdev_table->num; i++) { 1347 mtcp->n_sender[i] = CreateMTCPSender(i); 1348 if (!mtcp->n_sender[i]) { 1349 CTRACE_ERROR("Failed to create per-nic sender structure.\n"); 1350 return NULL; 1351 } 1352 } 1353 1354 mtcp->rto_store = InitRTOHashstore(); 1355 TAILQ_INIT(&mtcp->timewait_list); 1356 TAILQ_INIT(&mtcp->timeout_list); 1357 1358 return mtcp; 1359 } 1360 /*----------------------------------------------------------------------------*/ 1361 static void * 1362 MTCPRunThread(void *arg) 1363 { 1364 mctx_t mctx = (mctx_t)arg; 1365 int cpu = mctx->cpu; 1366 int working; 1367 struct mtcp_manager *mtcp; 1368 struct mtcp_thread_context *ctx; 1369 1370 /* affinitize the thread to this core first */ 1371 mtcp_core_affinitize(cpu); 1372 1373 /* memory alloc after core affinitization would use local memory 1374 most time */ 1375 ctx = calloc(1, sizeof(*ctx)); 1376 if (!ctx) { 1377 perror("calloc"); 1378 TRACE_ERROR("Failed to calloc mtcp context.\n"); 1379 exit(-1); 1380 } 1381 ctx->thread = pthread_self(); 1382 ctx->cpu = cpu; 1383 mtcp = ctx->mtcp_manager = InitializeMTCPManager(ctx); 1384 if (!mtcp) { 1385 TRACE_ERROR("Failed to initialize mtcp manager.\n"); 1386 exit(-1); 1387 } 1388 1389 /* assign mtcp context's underlying I/O module */ 1390 mtcp->iom = current_iomodule_func; 1391 1392 /* I/O initializing */ 1393 if (mtcp->iom->init_handle) 1394 mtcp->iom->init_handle(ctx); 1395 1396 if (pthread_mutex_init(&ctx->flow_pool_lock, NULL)) { 1397 perror("pthread_mutex_init of ctx->flow_pool_lock\n"); 1398 exit(-1); 1399 } 1400 1401 if (pthread_mutex_init(&ctx->socket_pool_lock, NULL)) { 1402 perror("pthread_mutex_init of ctx->socket_pool_lock\n"); 1403 exit(-1); 1404 } 1405 1406 SQ_LOCK_INIT(&ctx->connect_lock, "ctx->connect_lock", exit(-1)); 1407 SQ_LOCK_INIT(&ctx->close_lock, "ctx->close_lock", exit(-1)); 1408 SQ_LOCK_INIT(&ctx->reset_lock, "ctx->reset_lock", exit(-1)); 1409 SQ_LOCK_INIT(&ctx->sendq_lock, "ctx->sendq_lock", exit(-1)); 1410 SQ_LOCK_INIT(&ctx->ackq_lock, "ctx->ackq_lock", exit(-1)); 1411 SQ_LOCK_INIT(&ctx->destroyq_lock, "ctx->destroyq_lock", exit(-1)); 1412 1413 /* remember this context pointer for signal processing */ 1414 g_pctx[cpu] = ctx; 1415 mlockall(MCL_CURRENT); 1416 1417 // attach (nic device, queue) 1418 working = AttachDevice(ctx); 1419 if (working != 0) { 1420 sem_post(&g_init_sem[ctx->cpu]); 1421 TRACE_DBG("MTCP thread %d finished. Not attached any device\n", ctx->cpu); 1422 pthread_exit(NULL); 1423 } 1424 1425 TRACE_DBG("CPU %d: initialization finished.\n", cpu); 1426 sem_post(&g_init_sem[ctx->cpu]); 1427 1428 /* start the main loop */ 1429 RunMainLoop(ctx); 1430 1431 TRACE_DBG("MTCP thread %d finished.\n", ctx->cpu); 1432 1433 /* signaling mTCP thread is done */ 1434 sem_post(&g_done_sem[mctx->cpu]); 1435 1436 //pthread_exit(NULL); 1437 return 0; 1438 } 1439 /*----------------------------------------------------------------------------*/ 1440 #ifdef ENABLE_DPDK 1441 static int MTCPDPDKRunThread(void *arg) 1442 { 1443 MTCPRunThread(arg); 1444 return 0; 1445 } 1446 #endif /* !ENABLE_DPDK */ 1447 /*----------------------------------------------------------------------------*/ 1448 mctx_t 1449 mtcp_create_context(int cpu) 1450 { 1451 mctx_t mctx; 1452 int ret; 1453 1454 if (cpu >= g_config.mos->num_cores) { 1455 TRACE_ERROR("Failed initialize new mtcp context. " 1456 "Requested cpu id %d exceed the number of cores %d configured to use.\n", 1457 cpu, g_config.mos->num_cores); 1458 return NULL; 1459 } 1460 1461 /* check if mtcp_create_context() was already initialized */ 1462 if (g_logctx[cpu] != NULL) { 1463 TRACE_ERROR("%s was already initialized before!\n", 1464 __FUNCTION__); 1465 return NULL; 1466 } 1467 1468 ret = sem_init(&g_init_sem[cpu], 0, 0); 1469 if (ret) { 1470 TRACE_ERROR("Failed initialize init_sem.\n"); 1471 return NULL; 1472 } 1473 1474 ret = sem_init(&g_done_sem[cpu], 0, 0); 1475 if (ret) { 1476 TRACE_ERROR("Failed initialize done_sem.\n"); 1477 return NULL; 1478 } 1479 1480 mctx = (mctx_t)calloc(1, sizeof(struct mtcp_context)); 1481 if (!mctx) { 1482 TRACE_ERROR("Failed to allocate memory for mtcp_context.\n"); 1483 return NULL; 1484 } 1485 mctx->cpu = cpu; 1486 g_ctx[cpu] = mctx; 1487 1488 /* initialize logger */ 1489 g_logctx[cpu] = (struct log_thread_context *) 1490 calloc(1, sizeof(struct log_thread_context)); 1491 if (!g_logctx[cpu]) { 1492 perror("malloc"); 1493 TRACE_ERROR("Failed to allocate memory for log thread context.\n"); 1494 return NULL; 1495 } 1496 InitLogThreadContext(g_logctx[cpu], cpu); 1497 if (pthread_create(&log_thread[cpu], 1498 NULL, ThreadLogMain, (void *)g_logctx[cpu])) { 1499 perror("pthread_create"); 1500 TRACE_ERROR("Failed to create log thread\n"); 1501 return NULL; 1502 } 1503 1504 #ifdef ENABLE_DPDK 1505 /* Wake up mTCP threads (wake up I/O threads) */ 1506 if (current_iomodule_func == &dpdk_module_func) { 1507 int master; 1508 master = rte_get_master_lcore(); 1509 if (master == cpu) { 1510 lcore_config[master].ret = 0; 1511 lcore_config[master].state = FINISHED; 1512 if (pthread_create(&g_thread[cpu], 1513 NULL, MTCPRunThread, (void *)mctx) != 0) { 1514 TRACE_ERROR("pthread_create of mtcp thread failed!\n"); 1515 return NULL; 1516 } 1517 } else 1518 rte_eal_remote_launch(MTCPDPDKRunThread, mctx, cpu); 1519 } else 1520 #endif /* !ENABLE_DPDK */ 1521 { 1522 if (pthread_create(&g_thread[cpu], 1523 NULL, MTCPRunThread, (void *)mctx) != 0) { 1524 TRACE_ERROR("pthread_create of mtcp thread failed!\n"); 1525 return NULL; 1526 } 1527 } 1528 1529 sem_wait(&g_init_sem[cpu]); 1530 sem_destroy(&g_init_sem[cpu]); 1531 1532 running[cpu] = TRUE; 1533 1534 #ifdef NETSTAT 1535 #if NETSTAT_TOTAL 1536 if (printer < 0) { 1537 printer = cpu; 1538 TRACE_INFO("CPU %d is in charge of printing stats.\n", printer); 1539 } 1540 #endif 1541 #endif 1542 1543 return mctx; 1544 } 1545 /*----------------------------------------------------------------------------*/ 1546 /** 1547 * TODO: It currently always returns 0. Add appropriate error return values 1548 */ 1549 int 1550 mtcp_destroy_context(mctx_t mctx) 1551 { 1552 struct mtcp_thread_context *ctx = g_pctx[mctx->cpu]; 1553 struct mtcp_manager *mtcp = ctx->mtcp_manager; 1554 struct log_thread_context *log_ctx = mtcp->logger; 1555 int ret, i; 1556 1557 TRACE_DBG("CPU %d: mtcp_destroy_context()\n", mctx->cpu); 1558 1559 /* close all stream sockets that are still open */ 1560 if (!ctx->exit) { 1561 for (i = 0; i < g_config.mos->max_concurrency; i++) { 1562 if (mtcp->smap[i].socktype == MOS_SOCK_STREAM) { 1563 TRACE_DBG("Closing remaining socket %d (%s)\n", 1564 i, TCPStateToString(mtcp->smap[i].stream)); 1565 DumpStream(mtcp, mtcp->smap[i].stream); 1566 mtcp_close(mctx, i); 1567 } 1568 } 1569 } 1570 1571 ctx->done = 1; 1572 1573 //pthread_kill(g_thread[mctx->cpu], SIGINT); 1574 #ifdef ENABLE_DPDK 1575 ctx->exit = 1; 1576 /* XXX - dpdk logic changes */ 1577 if (current_iomodule_func == &dpdk_module_func) { 1578 int master = rte_get_master_lcore(); 1579 if (master == mctx->cpu) 1580 pthread_join(g_thread[mctx->cpu], NULL); 1581 else 1582 rte_eal_wait_lcore(mctx->cpu); 1583 } else 1584 #endif /* !ENABLE_DPDK */ 1585 { 1586 pthread_join(g_thread[mctx->cpu], NULL); 1587 } 1588 1589 TRACE_INFO("MTCP thread %d joined.\n", mctx->cpu); 1590 running[mctx->cpu] = FALSE; 1591 1592 #ifdef NETSTAT 1593 #if NETSTAT_TOTAL 1594 if (printer == mctx->cpu) { 1595 for (i = 0; i < num_cpus; i++) { 1596 if (i != mctx->cpu && running[i]) { 1597 printer = i; 1598 break; 1599 } 1600 } 1601 } 1602 #endif 1603 #endif 1604 1605 log_ctx->done = 1; 1606 ret = write(log_ctx->pair_sp_fd, "F", 1); 1607 if (ret != 1) 1608 TRACE_ERROR("CPU %d: Fail to signal socket pair\n", mctx->cpu); 1609 1610 pthread_join(log_thread[ctx->cpu], NULL); 1611 fclose(mtcp->log_fp); 1612 TRACE_LOG("Log thread %d joined.\n", mctx->cpu); 1613 1614 if (mtcp->connectq) { 1615 DestroyStreamQueue(mtcp->connectq); 1616 mtcp->connectq = NULL; 1617 } 1618 if (mtcp->sendq) { 1619 DestroyStreamQueue(mtcp->sendq); 1620 mtcp->sendq = NULL; 1621 } 1622 if (mtcp->ackq) { 1623 DestroyStreamQueue(mtcp->ackq); 1624 mtcp->ackq = NULL; 1625 } 1626 if (mtcp->closeq) { 1627 DestroyStreamQueue(mtcp->closeq); 1628 mtcp->closeq = NULL; 1629 } 1630 if (mtcp->closeq_int) { 1631 DestroyInternalStreamQueue(mtcp->closeq_int); 1632 mtcp->closeq_int = NULL; 1633 } 1634 if (mtcp->resetq) { 1635 DestroyStreamQueue(mtcp->resetq); 1636 mtcp->resetq = NULL; 1637 } 1638 if (mtcp->resetq_int) { 1639 DestroyInternalStreamQueue(mtcp->resetq_int); 1640 mtcp->resetq_int = NULL; 1641 } 1642 if (mtcp->destroyq) { 1643 DestroyStreamQueue(mtcp->destroyq); 1644 mtcp->destroyq = NULL; 1645 } 1646 1647 DestroyMTCPSender(mtcp->g_sender); 1648 for (i = 0; i < g_config.mos->netdev_table->num; i++) { 1649 DestroyMTCPSender(mtcp->n_sender[i]); 1650 } 1651 1652 MPDestroy(mtcp->rv_pool); 1653 MPDestroy(mtcp->sv_pool); 1654 MPDestroy(mtcp->flow_pool); 1655 1656 if (mtcp->ap) { 1657 DestroyAddressPool(mtcp->ap); 1658 } 1659 1660 SQ_LOCK_DESTROY(&ctx->connect_lock); 1661 SQ_LOCK_DESTROY(&ctx->close_lock); 1662 SQ_LOCK_DESTROY(&ctx->reset_lock); 1663 SQ_LOCK_DESTROY(&ctx->sendq_lock); 1664 SQ_LOCK_DESTROY(&ctx->ackq_lock); 1665 SQ_LOCK_DESTROY(&ctx->destroyq_lock); 1666 1667 //TRACE_INFO("MTCP thread %d destroyed.\n", mctx->cpu); 1668 if (mtcp->iom->destroy_handle) 1669 mtcp->iom->destroy_handle(ctx); 1670 free(ctx); 1671 free(mctx); 1672 1673 return 0; 1674 } 1675 /*----------------------------------------------------------------------------*/ 1676 mtcp_sighandler_t 1677 mtcp_register_signal(int signum, mtcp_sighandler_t handler) 1678 { 1679 mtcp_sighandler_t prev; 1680 1681 if (signum == SIGINT) { 1682 prev = app_signal_handler; 1683 app_signal_handler = handler; 1684 } else { 1685 if ((prev = signal(signum, handler)) == SIG_ERR) { 1686 perror("signal"); 1687 return SIG_ERR; 1688 } 1689 } 1690 1691 return prev; 1692 } 1693 /*----------------------------------------------------------------------------*/ 1694 int 1695 mtcp_getconf(struct mtcp_conf *conf) 1696 { 1697 int i, j; 1698 1699 if (!conf) 1700 return -1; 1701 1702 conf->num_cores = g_config.mos->num_cores; 1703 conf->max_concurrency = g_config.mos->max_concurrency; 1704 conf->cpu_mask = g_config.mos->cpu_mask; 1705 1706 conf->rcvbuf_size = g_config.mos->rmem_size; 1707 conf->sndbuf_size = g_config.mos->wmem_size; 1708 1709 conf->tcp_timewait = g_config.mos->tcp_tw_interval; 1710 conf->tcp_timeout = g_config.mos->tcp_timeout; 1711 1712 i = 0; 1713 struct conf_block *bwalk; 1714 TAILQ_FOREACH(bwalk, &g_config.app_blkh, link) { 1715 struct app_conf *app_conf = (struct app_conf *)bwalk->conf; 1716 for (j = 0; j < app_conf->app_argc; j++) 1717 conf->app_argv[i][j] = app_conf->app_argv[j]; 1718 conf->app_argc[i] = app_conf->app_argc; 1719 conf->app_cpu_mask[i] = app_conf->cpu_mask; 1720 i++; 1721 } 1722 conf->num_app = i; 1723 1724 return 0; 1725 } 1726 /*----------------------------------------------------------------------------*/ 1727 int 1728 mtcp_setconf(const struct mtcp_conf *conf) 1729 { 1730 if (!conf) 1731 return -1; 1732 1733 g_config.mos->num_cores = conf->num_cores; 1734 g_config.mos->max_concurrency = conf->max_concurrency; 1735 1736 g_config.mos->rmem_size = conf->rcvbuf_size; 1737 g_config.mos->wmem_size = conf->sndbuf_size; 1738 1739 g_config.mos->tcp_tw_interval = conf->tcp_timewait; 1740 g_config.mos->tcp_timeout = conf->tcp_timeout; 1741 1742 TRACE_CONFIG("Configuration updated by mtcp_setconf().\n"); 1743 //PrintConfiguration(); 1744 1745 return 0; 1746 } 1747 /*----------------------------------------------------------------------------*/ 1748 int 1749 mtcp_init(const char *config_file) 1750 { 1751 int i; 1752 int ret; 1753 1754 if (geteuid()) { 1755 TRACE_CONFIG("[CAUTION] Run as root if mlock is necessary.\n"); 1756 } 1757 1758 /* getting cpu and NIC */ 1759 num_cpus = GetNumCPUs(); 1760 assert(num_cpus >= 1); 1761 for (i = 0; i < num_cpus; i++) { 1762 g_mtcp[i] = NULL; 1763 running[i] = FALSE; 1764 sigint_cnt[i] = 0; 1765 } 1766 1767 ret = LoadConfigurationUpperHalf(config_file); 1768 if (ret) { 1769 TRACE_CONFIG("Error occured while loading configuration.\n"); 1770 return -1; 1771 } 1772 1773 #if defined(ENABLE_PSIO) 1774 current_iomodule_func = &ps_module_func; 1775 #elif defined(ENABLE_DPDK) 1776 current_iomodule_func = &dpdk_module_func; 1777 #elif defined(ENABLE_PCAP) 1778 current_iomodule_func = &pcap_module_func; 1779 #elif defined(ENABLE_DPDKR) 1780 current_iomodule_func = &dpdkr_module_func; 1781 #elif defined(ENABLE_NETMAP) 1782 current_iomodule_func = &netmap_module_func; 1783 #endif 1784 1785 if (current_iomodule_func->load_module_upper_half) 1786 current_iomodule_func->load_module_upper_half(); 1787 1788 LoadConfigurationLowerHalf(); 1789 1790 //PrintConfiguration(); 1791 1792 /* TODO: this should be fixed */ 1793 ap = CreateAddressPool(g_config.mos->netdev_table->ent[0]->ip_addr, 1); 1794 if (!ap) { 1795 TRACE_CONFIG("Error occured while creating address pool.\n"); 1796 return -1; 1797 } 1798 1799 //PrintInterfaceInfo(); 1800 //PrintRoutingTable(); 1801 //PrintARPTable(); 1802 InitARPTable(); 1803 1804 if (signal(SIGUSR1, HandleSignal) == SIG_ERR) { 1805 perror("signal, SIGUSR1"); 1806 return -1; 1807 } 1808 if (signal(SIGINT, HandleSignal) == SIG_ERR) { 1809 perror("signal, SIGINT"); 1810 return -1; 1811 } 1812 app_signal_handler = NULL; 1813 1814 printf("load_module(): %p\n", current_iomodule_func); 1815 /* load system-wide io module specs */ 1816 if (current_iomodule_func->load_module_lower_half) 1817 current_iomodule_func->load_module_lower_half(); 1818 1819 GlobInitEvent(); 1820 1821 PrintConf(&g_config); 1822 1823 return 0; 1824 } 1825 /*----------------------------------------------------------------------------*/ 1826 int 1827 mtcp_destroy() 1828 { 1829 int i; 1830 1831 /* wait until all threads are closed */ 1832 for (i = 0; i < num_cpus; i++) { 1833 if (running[i]) { 1834 if (pthread_join(g_thread[i], NULL) != 0) 1835 return -1; 1836 } 1837 } 1838 1839 DestroyAddressPool(ap); 1840 1841 TRACE_INFO("All MTCP threads are joined.\n"); 1842 1843 return 0; 1844 } 1845 /*----------------------------------------------------------------------------*/ 1846