static void init_state(struct interface *ifp, int argc, char **argv) { struct if_options *ifo; const char *reason = NULL; configure_interface(ifp, argc, argv); ifo = ifp->options; if (ifo->options & DHCPCD_IPV4 && ipv4_init() == -1) { syslog(LOG_ERR, "ipv4_init: %m"); ifo->options &= ~DHCPCD_IPV4; } if (ifo->options & DHCPCD_IPV6 && ipv6_init() == -1) { syslog(LOG_ERR, "ipv6_init: %m"); ifo->options &= ~DHCPCD_IPV6RS; } if (!(options & DHCPCD_TEST)) script_runreason(ifp, "PREINIT"); if (ifo->options & DHCPCD_LINK) { switch (carrier_status(ifp)) { case 0: ifp->carrier = LINK_DOWN; reason = "NOCARRIER"; break; case 1: ifp->carrier = LINK_UP; reason = "CARRIER"; break; default: ifp->carrier = LINK_UNKNOWN; return; } if (reason && !(options & DHCPCD_TEST)) script_runreason(ifp, reason); } else ifp->carrier = LINK_UNKNOWN; }
int main(int argc, char **argv) { char dev_if[16]; int opt; int err = 0; int daemonize = 0; int log_level = LOG_WARN; char *log_file = NULL; memset(dev_if, '\0', sizeof(dev_if)); strncpy(dev_if, PHOST_DEFAULT_TAP_DEVICE, sizeof(dev_if) - 1); phost_set_program_name(basename(argv[0])); /* parse options */ while(1){ opt = getopt(argc, argv, "Dd:i:p:l:n:v"); if(opt < 0){ break; } switch(opt){ case 'D': daemonize = 1; break; case 'd': if(optarg){ if(atoi(optarg) < LOG_LEVEL_MAX){ log_level = atoi(optarg); } else{ phost_print_usage(); exit(1); } } else{ err |= 1; } break; case 'i': if(optarg){ memset(dev_if, '\0', sizeof(dev_if)); strncpy(dev_if, optarg, sizeof(dev_if) - 1); } else{ err |= 1; } break; case 'p': if(optarg){ memset(pid_dir, '\0', sizeof(pid_dir)); strncpy(pid_dir, optarg, sizeof(pid_dir) - 1); } else{ err |= 1; } break; case 'l': if(optarg){ memset(log_dir, '\0', sizeof(log_dir)); strncpy(log_dir, optarg, sizeof(log_dir) - 1); } else{ err |= 1; } break; case 'n': if(optarg){ memset(host_name, '\0', sizeof(host_name)); strncpy(host_name, optarg, sizeof(host_name) - 1); } else{ err |= 1; } break; case 'v': log_file = LOG_OUT_STDOUT; log_level = LOG_DEBUG; break; default: err |= 1; } } if(err){ phost_print_usage(); exit(1); } if(log_file == NULL){ log_file = (char*)malloc(sizeof(char)*(strlen(log_dir)+strlen(PHOST_LOG_FILE)+strlen(dev_if)+3)); sprintf(log_file, "%s/%s.%s", log_dir, PHOST_LOG_FILE, dev_if); } /* check if this process is run with root privilege */ if(getuid() != 0){ fprintf(stderr, "[ERROR] `%s' must be run with root privilege.\n", program_name); exit(1); } /* register signal handler */ signal(SIGINT, phost_handle_signals); signal(SIGTERM, phost_handle_signals); signal(SIGPIPE, SIG_IGN); signal(SIGUSR1, SIG_IGN); signal(SIGUSR2, SIG_IGN); /* initializations */ if(log_init(log_level, log_file) < 0){ fprintf(stderr, "[ERROR] cannot initialize logger.\n"); exit(1); } if(daemonize && (phost_daemonize() < 0)){ fprintf(stderr, "[ERROR] cannot daemonize.\n"); exit(1); } if(strncmp(dev_if, "tap", 3) != 0){ if(ethdev_init(dev_if) < 0){ fprintf(stderr, "[ERROR] cannot initialize Ethernet device (%s).\n", dev_if); exit(1); } trx_init(ethdev_read, ethdev_send); } else{ if(tap_init(dev_if) < 0){ fprintf(stderr, "[ERROR] cannot create tap device (%s).\n", dev_if); exit(1); } trx_init(tap_read, tap_send); } phost_set_global_params(); phost_create_pid_file(host_name); cmdif_init(dev_if, stats_udp_send_update); arp_init(host_mac_addr, host_ip_addr); ipv4_init(host_ip_addr, host_ip_mask); udp_init(stats_udp_recv_update); pkt_dump = (char*)malloc(sizeof(char)*PKT_BUF_SIZE*2); while(run){ phost_run(); cmdif_run(); arp_age_entries(); } free(pkt_dump); cmdif_close(); tap_close(); phost_delete_pid_file(host_name); phost_unset_global_params(); log_close(); return 0; }
/***************************************************************************** * main() ****************************************************************************/ int main(int argc, char **argv) { global_config_t *cfg; int ret; /* * Initialize DPDK infrastructure before we do anything else */ ret = rte_eal_init(argc, argv); if (ret < 0) rte_panic("Cannot init EAL\n"); /* * Initialize RTE timer library */ rte_timer_subsystem_init(); /* * Precalculate the number of cycles per us so we don't do it everytime. */ cycles_per_us = (rte_get_timer_hz() / 1000000); /* * Return value above to be used to scan app specific options */ argc -= ret; argv += ret; /* * General checks */ if (rte_lcore_count() < 3) TPG_ERROR_EXIT(EXIT_FAILURE, "ERROR: %s\n", "WARP17 needs at least three cores!"); /* We only support at most 64 cores right now (to make parsing easier). */ if (rte_lcore_count() > (sizeof(uint64_t) * 8)) TPG_ERROR_EXIT(EXIT_FAILURE, "ERROR: WARP17 supports at most %"PRIu32" cores!\n", (uint32_t)sizeof(uint64_t) * 8); if (rte_eth_dev_count() > TPG_ETH_DEV_MAX) TPG_ERROR_EXIT(EXIT_FAILURE, "ERROR: WARP17 works with at most %u ports!\n", TPG_ETH_DEV_MAX); /* * Initialize various submodules */ if (!cli_init()) TPG_ERROR_EXIT(EXIT_FAILURE, "ERROR: %s!\n", "Failed initializing the command line interface"); if (!rpc_init()) TPG_ERROR_EXIT(EXIT_FAILURE, "ERROR: %s!\n", "Failed initializing the RPC server"); if (!cfg_init()) TPG_ERROR_EXIT(EXIT_FAILURE, "ERROR: %s\n", "Failed initializing default configuration!\n"); if (!cfg_handle_command_line(argc, argv)) exit(EXIT_FAILURE); /* Error reporting is handled by the function itself */ if (!trace_init()) TPG_ERROR_EXIT(EXIT_FAILURE, "ERROR: %s!\n", "Failed initializing the tracing module"); if (!trace_filter_init()) TPG_ERROR_EXIT(EXIT_FAILURE, "ERROR: %s!\n", "Failed initializing the trace filter module"); if (!mem_init()) TPG_ERROR_EXIT(EXIT_FAILURE, "ERROR: %s!\n", "Failed allocating required mbufs"); /* WARNING: Careful when adding code above this point. Up until ports are * initialized DPDK can't know that there might be ring interfaces that * still need to be created. Therefore any call to rte_eth_dev_count() * doesn't include them. */ if (!port_init()) TPG_ERROR_EXIT(EXIT_FAILURE, "ERROR: %s!\n", "Failed initializing the Ethernets ports"); if (!msg_sys_init()) TPG_ERROR_EXIT(EXIT_FAILURE, "ERROR: %s!\n", "Failed initializing the message queues"); if (!test_mgmt_init()) TPG_ERROR_EXIT(EXIT_FAILURE, "ERROR: %s!\n", "Failed initializing test mgmt"); if (!test_init()) TPG_ERROR_EXIT(EXIT_FAILURE, "ERROR: %s!\n", "Failed initializing tests"); if (!eth_init()) TPG_ERROR_EXIT(EXIT_FAILURE, "ERROR: %s!\n", "Failed initializing the Ethernets pkt handler"); if (!arp_init()) TPG_ERROR_EXIT(EXIT_FAILURE, "ERROR: %s!\n", "Failed initializing the ARP pkt handler"); if (!route_init()) TPG_ERROR_EXIT(EXIT_FAILURE, "ERROR: %s!\n", "Failed initializing the ROUTE module"); if (!ipv4_init()) TPG_ERROR_EXIT(EXIT_FAILURE, "ERROR: %s!\n", "Failed initializing the IPv4 pkt handler"); if (!tcp_init()) TPG_ERROR_EXIT(EXIT_FAILURE, "ERROR: %s!\n", "Failed initializing the TCP pkt handler"); if (!udp_init()) TPG_ERROR_EXIT(EXIT_FAILURE, "ERROR: %s!\n", "Failed initializing the UDP pkt handler"); if (!tlkp_init()) TPG_ERROR_EXIT(EXIT_FAILURE, "ERROR: %s!\n", "Failed initializing the Session lookup engine"); if (!tlkp_tcp_init()) TPG_ERROR_EXIT(EXIT_FAILURE, "ERROR: %s!\n", "Failed initializing the TCP lookup engine"); if (!tlkp_udp_init()) TPG_ERROR_EXIT(EXIT_FAILURE, "ERROR: %s!\n", "Failed initializing the UDP lookup engine"); if (!tsm_init()) TPG_ERROR_EXIT(EXIT_FAILURE, "ERROR: %s!\n", "Failed initializing the TSM module"); if (!timer_init()) TPG_ERROR_EXIT(EXIT_FAILURE, "ERROR: %s!\n", "Failed initializing the TCP timers module"); if (!pkt_loop_init()) TPG_ERROR_EXIT(EXIT_FAILURE, "ERROR: %s!\n", "Failed initializing the pkt loop"); if (!raw_init()) TPG_ERROR_EXIT(EXIT_FAILURE, "ERROR: %s!\n", "Failed initializing the RAW Application module"); if (!http_init()) TPG_ERROR_EXIT(EXIT_FAILURE, "ERROR: %s!\n", "Failed initializing the RAW Application module"); start_cores(); /* * Process startup command file, if any. */ cfg = cfg_get_config(); if (cfg != NULL && cfg->gcfg_cmd_file) { if (!cli_run_input_file(cfg->gcfg_cmd_file)) TPG_ERROR_EXIT(EXIT_FAILURE, "Failed to run command file: %s!\n", cfg->gcfg_cmd_file); } /* * Process CLI commands, and other house keeping tasks... */ cli_interact(); tpg_exit = true; /* * Exit!!! */ rte_eal_mp_wait_lcore(); /* * Destroy the CLI. */ cli_exit(); /* * Destroy the mgmt RPC server. */ rpc_destroy(); return 0; }
int main() { //###################################################################### //TODO get this from config file eventually //host interface my_host_mac_addr = 0x080027445566ull; my_host_ip_addr = IP4_ADR_P2H(192,168,1,20); my_host_mask = IP4_ADR_P2H(255,255,255,0); //loopback interface loopback_ip_addr = IP4_ADR_P2H(127,0,0,1); loopback_mask = IP4_ADR_P2H(255,0,0,0); //any any_ip_addr = IP4_ADR_P2H(0,0,0,0); //###################################################################### sem_init(&control_serial_sem, 0, 1); //TODO remove after gen_control_serial_num() converted to RNG signal(SIGINT, termination_handler); //register termination handler // Start the driving thread of each module PRINT_DEBUG("Initialize Modules"); switch_init(); //should always be first daemon_init(); //TODO improve how sets mac/ip interface_init(); arp_init(); arp_register_interface(my_host_mac_addr, my_host_ip_addr); ipv4_init(); set_interface(my_host_ip_addr, my_host_mask); set_loopback(loopback_ip_addr, loopback_mask); icmp_init(); tcp_init(); udp_init(); //rtm_init(); //TODO when updated/fully implemented pthread_attr_t fins_pthread_attr; pthread_attr_init(&fins_pthread_attr); PRINT_DEBUG("Run/start Modules"); switch_run(&fins_pthread_attr); daemon_run(&fins_pthread_attr); interface_run(&fins_pthread_attr); arp_run(&fins_pthread_attr); ipv4_run(&fins_pthread_attr); icmp_run(&fins_pthread_attr); tcp_run(&fins_pthread_attr); udp_run(&fins_pthread_attr); //rtm_run(&fins_pthread_attr); //############################# //TODO custom test, remove later /* if (0) { char recv_data[4000]; while (1) { gets(recv_data); PRINT_DEBUG("Sending ARP req"); metadata *params_req = (metadata *) malloc(sizeof(metadata)); if (params_req == NULL) { PRINT_ERROR("metadata alloc fail"); exit(-1); } metadata_create(params_req); uint32_t dst_ip = IP4_ADR_P2H(192, 168, 1, 11); //uint32_t dst_ip = IP4_ADR_P2H(172, 31, 50, 152); uint32_t src_ip = IP4_ADR_P2H(192, 168, 1, 20); //uint32_t src_ip = IP4_ADR_P2H(172, 31, 50, 160); metadata_writeToElement(params_req, "dst_ip", &dst_ip, META_TYPE_INT32); metadata_writeToElement(params_req, "src_ip", &src_ip, META_TYPE_INT32); struct finsFrame *ff_req = (struct finsFrame*) malloc(sizeof(struct finsFrame)); if (ff_req == NULL) { PRINT_ERROR("todo error"); //metadata_destroy(params_req); exit(-1); } ff_req->dataOrCtrl = CONTROL; ff_req->destinationID.id = ARP_ID; ff_req->destinationID.next = NULL; ff_req->metaData = params_req; ff_req->ctrlFrame.senderID = IP_ID; ff_req->ctrlFrame.serial_num = gen_control_serial_num(); ff_req->ctrlFrame.opcode = CTRL_EXEC; ff_req->ctrlFrame.param_id = EXEC_ARP_GET_ADDR; ff_req->ctrlFrame.data_len = 0; ff_req->ctrlFrame.data = NULL; arp_to_switch(ff_req); //doesn't matter which queue } } //############################# */ while (1) ; return (1); }