void sr_integ_init(struct sr_instance* sr) { printf(" ** sr_integ_init(..) called \n"); /* Register router state with global instance */ struct sr_router* subsystem = (struct sr_router*)malloc(sizeof(struct sr_router)); assert(subsystem); sr_set_subsystem(get_sr(), subsystem); } /* -- sr_integ_init -- */
void sr_integ_init(struct sr_instance* sr) { printf(" ** sr_integ_init(..) called \n"); sr_router* subsystem = (sr_router*)malloc(sizeof(sr_router)); assert(subsystem); sr_set_subsystem(get_sr(), subsystem); // initialize arp and interfaces and routing table arp_cache_init(subsystem); interface_init(subsystem); rrtable_init(subsystem); hw_rrtable_init(subsystem); //enable ospf by default toggle_ospf_status(subsystem, TRUE); //disable reroute/multipath toggle_reroute_multipath_status(subsystem, FALSE); //flush hw registers #ifdef _CPUMODE_ hw_init(subsystem); writeReg(&subsystem->hw_device, CPCI_REG_CTRL, 0x00010100); usleep(2000); #endif } /* -- sr_integ_init -- */
void init(struct sr_instance* sr) { unsigned int iseed = (unsigned int)time(NULL); srand(iseed+1); router_state* rs = (router_state*)malloc(sizeof(router_state));//router_state,一个结构体,在or_data_types.h定义 assert(rs); bzero(rs, sizeof(router_state)); rs->sr = sr; #ifdef _CPUMODE_ init_rawsockets(rs); #endif /** INITIALIZE LOCKS **/ rs->write_lock = (pthread_mutex_t*)malloc(sizeof(pthread_mutex_t)); if (pthread_mutex_init(rs->write_lock, NULL) != 0) { perror("Lock init error"); exit(1); } rs->arp_cache_lock = (pthread_rwlock_t*)malloc(sizeof(pthread_rwlock_t)); if (pthread_rwlock_init(rs->arp_cache_lock, NULL) != 0) { perror("Lock init error"); exit(1); } rs->arp_queue_lock = (pthread_rwlock_t*)malloc(sizeof(pthread_rwlock_t)); if (pthread_rwlock_init(rs->arp_queue_lock, NULL) != 0) { perror("Lock init error"); exit(1); } rs->if_list_lock = (pthread_rwlock_t*)malloc(sizeof(pthread_rwlock_t)); if (pthread_rwlock_init(rs->if_list_lock, NULL) != 0) { perror("Lock init error"); exit(1); } rs->rtable_lock = (pthread_rwlock_t*)malloc(sizeof(pthread_rwlock_t)); if (pthread_rwlock_init(rs->rtable_lock, NULL) != 0) { perror("Lock init error"); exit(1); } // --- CCDN lock rs->ctable_lock = (pthread_rwlock_t*)malloc(sizeof(pthread_rwlock_t)); if (pthread_rwlock_init(rs->ctable_lock, NULL) != 0) { perror("Lock init error"); exit(1); } rs->cli_commands_lock = (pthread_rwlock_t*)malloc(sizeof(pthread_rwlock_t)); if (pthread_rwlock_init(rs->cli_commands_lock, NULL) != 0) { perror("Lock init error"); exit(1); } rs->nat_table_mutex = (pthread_mutex_t*)malloc(sizeof(pthread_mutex_t)); if (pthread_mutex_init(rs->nat_table_mutex, NULL) != 0) { perror("Mutex init error"); exit(1); } rs->nat_table_cond = (pthread_cond_t*)malloc(sizeof(pthread_cond_t)); if (pthread_cond_init(rs->nat_table_cond, NULL) != 0) { perror("Nat Table cond init error"); exit(1); } rs->local_ip_filter_list_mutex = (pthread_mutex_t*)malloc(sizeof(pthread_mutex_t)); if (pthread_mutex_init(rs->local_ip_filter_list_mutex, NULL) != 0) { perror("Local IP Filter Mutex init error"); exit(1); } rs->log_dumper_mutex = (pthread_mutex_t*)malloc(sizeof(pthread_mutex_t)); if (pthread_mutex_init(rs->log_dumper_mutex, NULL) != 0) { perror("Log dumper mutex init error"); exit(1); } rs->sr = sr; rs->area_id = PWOSPF_AREA_ID; rs->pwospf_hello_interval = PWOSPF_NEIGHBOR_TIMEOUT; rs->pwospf_lsu_interval = PWOSPF_LSUINT; rs->pwospf_lsu_broadcast = 1; rs->arp_ttl = INITIAL_ARP_TIMEOUT; rs->nat_timeout = 120; /* clear stats */ int i, j; for (i = 0; i < 8; ++i) { for (j = 0; j < 4; ++j) { rs->stats_last[i][j] = 0; } for (j = 0; j < 2; ++j) { rs->stats_avg[i][j] = 0.0; } } rs->stats_last_time.tv_sec = 0; rs->stats_last_time.tv_usec = 0; #ifdef _CPUMODE_ rs->is_netfpga = 1; char* name = (char*)calloc(1, 32); strncpy(name, sr->interface, 32); rs->netfpga.device_name = name; rs->netfpga.fd = 0; rs->netfpga.net_iface = 0; if (check_iface(&(rs->netfpga))) { printf("Failure connecting to NETFPGA\n"); exit(1); } if (openDescriptor(&(rs->netfpga))) { printf("Failure connecting to NETFPGA\n"); exit(1); } /* initialize the hardware */ init_hardware(rs); #else rs->is_netfpga = 0; #endif if (rs->is_netfpga) { /* Add 224.0.0.5 as a local IP Filter */ struct in_addr ip; inet_pton(AF_INET, "224.0.0.5", &ip); add_local_ip_filter(rs, &ip, "pwospf"); } /* Initialize SPING data */ rs->sping_mutex = (pthread_mutex_t*)malloc(sizeof(pthread_mutex_t)); if (pthread_mutex_init(rs->sping_mutex, NULL) != 0) { perror("Sping mutex init error"); exit(1); } rs->sping_cond = (pthread_cond_t*)malloc(sizeof(pthread_cond_t)); if (pthread_cond_init(rs->sping_cond, NULL) != 0) { perror("Sping cond init error"); exit(1); } /* Initialize LSU data */ rs->pwospf_router_list_lock = (pthread_mutex_t*)malloc(sizeof(pthread_mutex_t)); if (pthread_mutex_init(rs->pwospf_router_list_lock, NULL) != 0) { perror("Routing list mutex init error"); exit(1); } rs->pwospf_lsu_bcast_mutex = (pthread_mutex_t*)malloc(sizeof(pthread_mutex_t)); if (pthread_mutex_init(rs->pwospf_lsu_bcast_mutex, NULL) != 0) { perror("LSU bcast mutex init error"); exit(1); } rs->pwospf_lsu_bcast_cond = (pthread_cond_t*)malloc(sizeof(pthread_cond_t)); if (pthread_cond_init(rs->pwospf_lsu_bcast_cond, NULL) != 0) { perror("LSU bcast cond init error"); exit(1); } rs->pwospf_lsu_queue_lock = (pthread_mutex_t*)malloc(sizeof(pthread_mutex_t)); if (pthread_mutex_init(rs->pwospf_lsu_queue_lock, NULL) != 0) { perror("Lsu queue mutex init error"); exit(1); } /* Initialize PWOSPF Dijkstra Thread Mutex/Cond Var */ rs->dijkstra_mutex = (pthread_mutex_t*)malloc(sizeof(pthread_mutex_t)); if (pthread_mutex_init(rs->dijkstra_mutex, NULL) != 0) { perror("Dijkstra mutex init error"); exit(1); } rs->dijkstra_cond = (pthread_cond_t*)malloc(sizeof(pthread_cond_t)); if (pthread_cond_init(rs->dijkstra_cond, NULL) != 0) { perror("Dijkstra cond init error"); exit(1); } /* Initialize WWW Mutex/Cond Var */ rs->www_mutex = (pthread_mutex_t*)malloc(sizeof(pthread_mutex_t)); if (pthread_mutex_init(rs->www_mutex, NULL) != 0) { perror("WWW mutex init error"); exit(1); } rs->www_cond = (pthread_cond_t*)malloc(sizeof(pthread_cond_t)); if (pthread_cond_init(rs->www_cond, NULL) != 0) { perror("WWW cond init error"); exit(1); } /* Initialize Stats Mutex */ rs->stats_mutex = (pthread_mutex_t*)malloc(sizeof(pthread_mutex_t)); if (pthread_mutex_init(rs->stats_mutex, NULL) != 0) { perror("Stats mutex init error"); exit(1); } // --- CCDN , sr里面有一个指针是sr->interface_subsystem = rs,而main函数的最前面,rs->sr=sr sr_set_subsystem(sr, (void*)rs); // --- CCDN , pthread_create用于创建一个线程,第一个参数返回线程id,第二个设置线程属性(NULL表默认),第三个指向线程调用的函数,第四个传递参数 /** SPAWN THE ARP QUEUE THREAD **/ rs->arp_thread = (pthread_t*)malloc(sizeof(pthread_t)); if(pthread_create(rs->arp_thread, NULL, arp_thread, (void *)sr) != 0) { perror("Thread create error"); } /** SPAWN THE PWOSPF HELLO BROADCAST THREAD **/ rs->pwospf_hello_thread = (pthread_t*)malloc(sizeof(pthread_t)); if(pthread_create(rs->pwospf_hello_thread, NULL, pwospf_hello_thread, (void *)sr) != 0) { perror("Thread create error"); } /** SPAWN THE PWOSPF LSU BROADCAST THREAD **/ rs->pwospf_lsu_thread = (pthread_t*)malloc(sizeof(pthread_t)); if(pthread_create(rs->pwospf_lsu_thread, NULL, pwospf_lsu_thread, (void *)sr) != 0) { perror("Thread create error"); } /** SPAWN THE PWOSPF LSU BCAST TIMEOUT THREAD **/ rs->pwospf_lsu_timeout_thread = (pthread_t*)malloc(sizeof(pthread_t)); if(pthread_create(rs->pwospf_lsu_timeout_thread, NULL, pwospf_lsu_timeout_thread, (void*)sr) != 0) { perror("Thread create error"); } /** SPAWN THE DIJKSTRA THREAD **/ rs->pwospf_dijkstra_thread = (pthread_t*)malloc(sizeof(pthread_t)); if(pthread_create(rs->pwospf_dijkstra_thread, NULL, dijkstra_thread, (void*)get_router_state(sr)) != 0) { perror("Thread create error"); } /** SPAWN THE PWOSPF LSU BCAST THREAD **/ rs->pwospf_lsu_bcast_thread = (pthread_t*)malloc(sizeof(pthread_t)); if(pthread_create(rs->pwospf_lsu_bcast_thread, NULL, pwospf_lsu_bcast_thread, (void*)sr) != 0) { perror("Thread create error"); } /** Spawn the NAT Maintenance Thread **/ /* rs->nat_maintenance_thread = (pthread_t*)malloc(sizeof(pthread_t)); if(pthread_create(rs->nat_maintenance_thread, NULL, nat_maintenance_thread, (void*)rs) != 0) { perror("Thread create error"); } */ /* if we are on the NETFPGA spawn the stats thread */ if (rs->is_netfpga) { rs->stats_thread = (pthread_t*)malloc(sizeof(pthread_t)); if(pthread_create(rs->stats_thread, NULL, netfpga_stats, (void*)rs) != 0) { perror("Thread create error"); } } }