void c_main(void) { // Load in all data address_t address = system_load_sram(); if (!get_packets(region_start(2, address), &start_packets) || !get_packets(region_start(4, address), &end_packets) ) { return; } spin1_set_timer_tick(1000); spin1_callback_on(TIMER_TICK, tick, 2); while(true) { // Wait for data loading, etc. event_wait(); // Determine how long to simulate for config_get_n_ticks(); // Transmit all packets assigned to be sent prior to the start of the // simulation transmit_packet_region(start_packets); // Synchronise with the simulation spin1_start(SYNC_WAIT); } }
size_t casr::State::get_data_size(int process_id1, int process_id2) { PacketQueue &pq = get_packets(process_id1, process_id2); size_t size = 0; for (size_t t = 0; t < pq.size(); t++) { size += pq[t].size; } return size; }
casr::State::PacketQueue casr::State::get_current_packets(int process_id1, int process_id2) { PacketQueue &pq = get_packets(process_id1, process_id2); PacketQueue result; for (size_t t = 0; t < pq.size(); t++) { if (global_time >= pq[t].start_time) { result.push_back(pq[t]); } } return result; }
extern PACKET * do_packets( /* queue a packet list, return finished */ register PACKET *pl ) { register PACKET *p; /* consistency check */ if (nprocs < 1) error(CONSISTENCY, "do_packets called with no active process"); /* queue each new packet */ while (pl != NULL) { p = pl; pl = p->next; p->next = NULL; queue_packet(p); } return(get_packets(slots_avail())); /* return processed packets */ }
int packet_assembly_thread(void * data) { struct client_params * cur; struct pcap_packet * packets, *local_list, *cur_packet_list, *prev, *cur2; _packet_assembly_thread_stopped = 0; local_list = NULL; uint32_t fcs; char * temp_str; int bad_frame; #ifdef DEBUG add_message_to_queue(MESSAGE_TYPE_REG_LOG, NULL, 1, "Packet assembly thread started", 1); #endif while (!_stop_threads && !_stop_packet_assembly_thread) { // Check that the server is initialized if (_sensor_server_params == NULL) { usleep(5000); continue; } // Check if it has clients if (_sensor_server_params->client_list == NULL) { usleep(10000); continue; } // TODO: Check the client isn't dead and thus being removed/free (there is probably a mutex next to the dead check function) // 1. Search in all connected clients (rpcap) ... for (cur = _sensor_server_params->client_list; cur != NULL; cur = cur->next) { if (!(cur->rpcap_server && cur->rpcap_server->client_list && /*&& cur->rpcap_server->client_list->client->connected*/ cur->rpcap_server->client_list->received_packets && cur->rpcap_server->client_list->received_packets->nb_packet && cur->rpcap_server->client_list->received_packets->pcap_header)) { continue; } // Is there any packet in the structure // ... RPCAP is connected, we probably have some packets (take all) ... packets = get_packets(INT_MAX, &(cur->rpcap_server->client_list->received_packets)); if (packets == NULL) { continue; } // Tag all packets with socket id (TODO: Use sensor login) for (cur_packet_list = packets; cur_packet_list != NULL; cur_packet_list = cur_packet_list->next) { cur_packet_list->source = cur->rpcap_server->client_list->client->sock; cur_packet_list->linktype = cur->rpcap_server->client_list->received_packets->pcap_header->linktype; } // ... and add them to our local list if (local_list == NULL) { local_list = packets; } else { for (cur_packet_list = local_list; cur_packet_list->next != NULL; cur_packet_list = cur_packet_list->next); cur_packet_list->next = packets; } packets = NULL; } if (!local_list) { usleep(100); continue; } // 2. Remove bad CRC/FCS prev = NULL; cur_packet_list = local_list; while (cur_packet_list != NULL) { bad_frame = 1; if (cur_packet_list->header.cap_len < MIN_PACKET_SIZE + FCS_SIZE) { temp_str = (char *)calloc(1, 200 * sizeof(char)); sprintf(temp_str, "Received invalid packet - frame too short to be analyzed. Expected %d bytes, received %u.", MIN_PACKET_SIZE + FCS_SIZE, cur_packet_list->header.cap_len); add_message_to_queue(MESSAGE_TYPE_ANOMALY, NULL, 1, temp_str, 0); } else { // Get packet information cur_packet_list->info = parse_packet_basic_info(cur_packet_list); if (cur_packet_list->info != NULL) { // If FCS is bad, discard the frame and log it. if (cur_packet_list->info->bad_fcs) { #ifdef EXTRA_DEBUG add_message_to_queue(MESSAGE_TYPE_DEBUG, NULL, 1, "Invalid FCS flags set. Ignoring frame", 1); #endif } else if (_force_fcs_check && cur_packet_list->info->fcs_present) { // Check FCS before processing frame (ignore if invalid but log it in DB). fcs = crc32(0L, cur_packet_list->info->frame_start, cur_packet_list->header.cap_len - FCS_SIZE - cur_packet_list->info->packet_header_len); if (fcs != cur_packet_list->info->fcs) { #ifdef EXTRA_DEBUG temp_str = (char *)calloc(1, 80); fprintf(stderr, "Invalid FCS: Got 0x%x, expected 0x%x. Ignoring frame", cur_packet_list->info->fcs, fcs); add_message_to_queue(MESSAGE_TYPE_DEBUG, NULL, 1, temp_str, 0); #endif } else bad_frame = 0; } else bad_frame = 0; } } if (!bad_frame) { prev = cur_packet_list; cur_packet_list = cur_packet_list->next; continue; } // Remove frame from list if (prev == NULL) { // First frame of the list local_list = cur_packet_list->next; free_pcap_packet(&cur_packet_list, 0); // Free cur_packet_list = local_list; } else { cur2 = cur_packet_list; prev->next = cur_packet_list->next; cur_packet_list = cur_packet_list->next; free_pcap_packet(&cur2, 0); // Free } } // 3. TODO: Remove duplicates (within 1 second of the last received packet) within several sources // Easy in most cases thanks to the Sequence Number (or FCS if it present) // More problematic with Control packets (can use FCS if present) but also need to check time difference more accurately) // 4. TODO: Re-order (if needed) // 5. Put it on the list add_multiple_packets_to_list(local_list, &_receive_packet_list, 1); local_list = NULL; // Make sure the CPU won't get overloaded usleep(250); } _packet_assembly_thread_stopped = 1; return EXIT_SUCCESS; }
ca::IntTime casr::State::run_process(int process_id) { ThreadInfo &ti = thread_info[process_id]; if (ti.release_time > global_time) { // This process is still working return ti.release_time; } ControlledTimeTraceLog *tracelog = (ControlledTimeTraceLog*) get_tracelog(process_id); if (tracelog) { tracelog->set_basetime(global_time); } // Minimal time when some packet can be received ca::IntTime next_time = ca::MAX_INT_TIME; // Was at least one packet received? bool received = false; // Packet processing for (int p = 0; p < ca::process_count; p++) { PacketQueue &pq = get_packets(process_id, p); if (pq.empty()) { continue; } Packet &packet = pq.front(); if (packet.release_time > global_time) { if (packet.release_time < next_time) { next_time = packet.release_time; } continue; } receive(process_id, p); p--; // Run receive once again received = true; } if (received) { tracelog->event_end(); ti.release_time = tracelog->get_time(); return ti.release_time; } // Transitions ca::Net *n = nets[process_id]; ca::Transition *tr = n->pick_active_transition(); if (tr == NULL) { if (!ti.idle && tracelog) { tracelog->event_idle(); } ti.idle = true; return next_time; } ti.idle = false; if (!fire_transition_full(process_id, tr->get_def())) { tr->set_active(false); } ti.release_time = tracelog->get_time(); if (quit && quit_time == ca::MAX_INT_TIME) { quit_time = ti.release_time; } return ti.release_time; }
int casr::State::get_packets_count(int process_id1, int process_id2) { PacketQueue &pq = get_packets(process_id1, process_id2); return pq.size(); }
/* ARGSUSED */ int fru_get_num_packets(segment_hdl_t segment, door_cred_t *cred) { int pktcnt; int length; uint16_t offset; hash_obj_t *cont_hash_obj; hash_obj_t *seg_hash; hash_obj_t *sec_hash; fru_segdesc_t *segdesc; segment_obj_t *segment_object; seg_hash = lookup_handle_object(segment, SEGMENT_TYPE); if (seg_hash == NULL) { return (-1); } segment_object = seg_hash->u.seg_obj; if (segment_object == NULL) { return (-1); } segdesc = (fru_segdesc_t *)&segment_object->segment.descriptor; if (segdesc->field.opaque) { return (0); } offset = segment_object->segment.offset; length = segment_object->segment.length; cont_hash_obj = get_container_hash_object(SEGMENT_TYPE, segment_object->section_hdl); if (cont_hash_obj == NULL) { return (-1); } if (seg_hash->u.seg_obj->pkt_obj_list != NULL) { return (segment_object->num_of_packets); } /* section hash object */ sec_hash = lookup_handle_object(seg_hash->u.seg_obj->section_hdl, SECTION_TYPE); if (sec_hash == NULL) { return (-1); } /* valid segment header b'cos crc8 already validated */ if (offset < sec_hash->u.sec_obj->section.offset) { return (-1); } segment_object->num_of_packets = 0; pktcnt = get_packets(seg_hash, g_raw, offset, length); if (pktcnt == -1) { free_pkt_object_list(seg_hash); seg_hash->u.seg_obj->pkt_obj_list = NULL; } segment_object->num_of_packets = pktcnt; return (segment_object->num_of_packets); }