int onlp_sysi_onie_data_get(uint8_t** data, int* size) { uint8_t* rdata = aim_zmalloc(256); int fd,rc_size; char fullpath[20] = {0}; sprintf(fullpath, "/dev/mtd7"); fd=open(fullpath,O_RDWR); if(fd<0){ aim_free(rdata); return ONLP_STATUS_E_INTERNAL ; } rc_size=read(fd,rdata,256); if(rc_size<0||rc_size!=256){ aim_free(rdata); return ONLP_STATUS_E_INTERNAL ; } *data = rdata; return ONLP_STATUS_OK; }
void indigo_core_table_register(uint8_t table_id, const char *name, const indigo_core_table_ops_t *ops, void *priv) { AIM_TRUE_OR_DIE(strlen(name) <= OF_MAX_TABLE_NAME_LEN); list_links_t *cur, *next; ft_entry_t *entry; FT_ITER(ind_core_ft, entry, cur, next) { if (entry->table_id == table_id) { ind_core_flow_entry_delete(entry, OF_FLOW_REMOVED_REASON_DELETE, INDIGO_CXN_ID_UNSPECIFIED); } } ind_core_table_t *table = aim_zmalloc(sizeof(*table)); strncpy(table->name, name, sizeof(table->name)); table->ops = ops; table->priv = priv; table->num_flows = 0; AIM_TRUE_OR_DIE(ind_core_tables[table_id] == NULL); ind_core_tables[table_id] = table; ind_core_num_tables_registered++; AIM_LOG_VERBOSE("Registered flowtable \"%s\" with table id %d", name, table_id); }
static int insert__(bighash_table_t *table, int count, biglist_t** entries) { int c; for(c = 0; c < count; c++) { test_entry_t *te = aim_zmalloc(sizeof(*te)); te->id = c; bighash_insert(table, &te->hash_entry, hash_id(te->id)); if(entries) { *entries = biglist_prepend(*entries, te); } /** Make sure we can find it */ test_entry_t *fe = find_by_id(table, te->id); if(fe == NULL) { AIM_DIE("inserted entry was not found, count=%d/%d", c, count); } if(fe != te) { AIM_DIE("Retreived pointer not equal."); } if(bighash_entry_count(table) != (c+1)) { AIM_DIE("Entry count mismatch: should be %d, reported as %d", (c+1), bighash_entry_count(table)); } } return 0; }
int onlp_sysi_onie_data_get(uint8_t** data, int* size) { int i,re_cnt; uint8_t* rdata = aim_zmalloc(256); if(!rdata){ AIM_LOG_ERROR("Unable to malloc memory \r\n"); return ONLP_STATUS_E_INTERNAL; } for(i=0;i<8;i++){ re_cnt=3; while(re_cnt){ if (i2c_devname_read_block("ID_EEPROM", i * 32, (rdata + i * 32), 32) < 0) { re_cnt--; continue; } break; } if(re_cnt==0){ AIM_LOG_ERROR("Unable to read the %d reg \r\n",i); break; } } *data = rdata; return ONLP_STATUS_OK; }
int onlp_file_vread_all(uint8_t** data, const char* fmt, va_list vargs) { int rv; uint8_t* contents = NULL; char * fname = NULL; int fsize, rsize; if(data == NULL || fmt == NULL) { return ONLP_STATUS_E_PARAM; } fname = aim_vdfstrdup(fmt, vargs); *data = NULL; if((fsize = onlp_file_size(fname)) > 0) { contents = aim_zmalloc(fsize); if((rv = onlp_file_read(contents, fsize, &rsize, fname)) >= 0) { *data = contents; rv = rsize; } } else { rv = fsize; } aim_free(fname); return rv; }
static void listen_callback( int socket_id, void *cookie, int read_ready, int write_ready, int error_seen) { AIM_LOG_TRACE("Accepting CLI client"); int fd; if ((fd = accept(listen_socket, NULL, NULL)) < 0) { AIM_LOG_ERROR("Failed to accept on CLI socket: %s", strerror(errno)); return; } struct client *client = aim_zmalloc(sizeof(*client)); client->fd = fd; client->write_pvs = aim_pvs_buffer_create(); client->ucli = ucli_create("ivs", NULL, NULL); indigo_error_t rv = ind_soc_socket_register(fd, client_callback, client); if (rv < 0) { AIM_LOG_ERROR("Failed to register CLI client socket: %s", indigo_strerror(rv)); return; } }
char* aim_strjoin(const char* string, const char** strings, int count) { int i; int len = 0; int jlen = AIM_STRLEN(string); char* rv; if(count <= 0) { return NULL; } /* Calculate required buffer size. */ for(i = 0; i < count-1; i++) { len += AIM_STRLEN(strings[i]); len += jlen; } len += AIM_STRLEN(strings[i]) + 1; rv = aim_zmalloc(len); for(i = 0; i < count-1; i++) { AIM_STRCAT(rv, strings[i]); AIM_STRCAT(rv, string); } AIM_STRCAT(rv, strings[i]); return rv; }
void test_list(void) { struct histogram *hist1 = histogram_create("hist1"); struct histogram *hist2 = histogram_create("hist2"); struct histogram *hist3 = aim_zmalloc(sizeof(*hist3)); histogram_register(hist3, "hist3"); AIM_ASSERT(!strcmp(hist1->name, "hist1")); AIM_ASSERT(!strcmp(hist2->name, "hist2")); AIM_ASSERT(!strcmp(hist3->name, "hist3")); struct list_head *head = histogram_list(); struct list_links *cur = list_first(head); AIM_ASSERT(container_of(cur, links, struct histogram) == hist1); cur = cur->next; AIM_ASSERT(container_of(cur, links, struct histogram) == hist2); cur = cur->next; AIM_ASSERT(container_of(cur, links, struct histogram) == hist3); AIM_ASSERT(cur->next == &head->links); histogram_destroy(hist1); histogram_destroy(hist2); histogram_unregister(hist3); aim_free(hist3); }
char* aim_strdup(const char* src) { int size = AIM_STRLEN(src)+1; char* ns = aim_zmalloc(size); AIM_STRCPY(ns, src); return ns; }
static vpi_qpacket_t* qpacket_alloc__(unsigned char* data, int size) { vpi_qpacket_t* qp = aim_zmalloc(sizeof(*qp)); qp->data = aim_memdup(data, size); qp->size = size; return qp; }
ucli_module_t* ppe_utm_create(void) { ppe_utm_ctrl_t* ppec = aim_zmalloc(sizeof(*ppec)); ppe_utm_module__.cookie = ppec; ucli_module_init(&ppe_utm_module__); return &ppe_utm_module__; }
bighash_table_t * bighash_table_create(int bucket_count) { bighash_table_t *table = aim_zmalloc(sizeof(*table)); table->flags |= BIGHASH_TABLE_F_TABLE_ALLOCATED; bighash_table_init(table, bucket_count); return table; }
ucli_module_t* fme_utm_create(void) { fme_utm_ctrl_t* fmec = aim_zmalloc(sizeof(*fmec)); fme_create(&fmec->fme, "fme_utm", FME_CONFIG_UTM_ENTRIES); fme_utm_module__.cookie = fmec; ucli_module_init(&fme_utm_module__); return &fme_utm_module__; }
biglist_locked_t* biglist_locked_create(void) { biglist_locked_t* bl = aim_zmalloc(sizeof(*bl)); if(bl) { #if BIGLIST_CONFIG_INCLUDE_LOCKED == 1 sem_init(&bl->lock, 0, 1); #endif } return bl; }
biglist_t* biglist_alloc(void* data, biglist_t* p, biglist_t* n) { biglist_t* ble = aim_zmalloc(sizeof(*ble)); if(ble) { ble->previous = p; ble->next = n; ble->data = data; } return ble; }
ft_instance_t ft_create(ft_config_t *config) { ft_instance_t ft; int bytes; int idx; /* Allocate the flow table itself */ ft = aim_zmalloc(sizeof(*ft)); INDIGO_MEM_COPY(&ft->config, config, sizeof(ft_config_t)); list_init(&ft->all_list); /* Allocate and init buckets for each search type */ bytes = sizeof(list_head_t) * config->strict_match_bucket_count; ft->strict_match_buckets = aim_zmalloc(bytes); for (idx = 0; idx < config->strict_match_bucket_count; idx++) { list_init(&ft->strict_match_buckets[idx]); } bytes = sizeof(list_head_t) * config->flow_id_bucket_count; ft->flow_id_buckets = aim_zmalloc(bytes); for (idx = 0; idx < config->flow_id_bucket_count; idx++) { list_init(&ft->flow_id_buckets[idx]); } bytes = sizeof(list_head_t) * (1 << FT_COOKIE_PREFIX_LEN); ft->cookie_buckets = aim_zmalloc(bytes); for (idx = 0; idx < (1 << FT_COOKIE_PREFIX_LEN); idx++) { list_init(&ft->cookie_buckets[idx]); } for (idx = 0; idx < FT_MAX_TABLES; idx++) { ft_table_t *table = &ft->tables[idx]; table->checksum_buckets_size = 128; table->checksum_shift = 64 - aim_log2_u32(table->checksum_buckets_size); table->checksum_buckets = aim_zmalloc(table->checksum_buckets_size * sizeof(uint64_t)); } return ft; }
uint8_t* ucli_util_data_from_string(const char* string, int* size) { unsigned char* bytes; unsigned char byte; int i; int nibble_count; int string_len; if(string == NULL || size == NULL || string[0] == 0) { return NULL; } if(!UCLI_STRCMP(string, "NULL")) { *size = 0; return NULL; } if(!UCLI_STRCMP(string, "builtin")) { string = builtin__; } string_len = UCLI_STRLEN(string); if( (bytes = aim_zmalloc((string_len+1) / 2)) == NULL) { return NULL; } byte = 0; nibble_count = 0; for(i = 0; i < string_len; i++) { int hv = ucli_hex_val__(string[i]); if(hv >= 0) { byte |= hv; if( (nibble_count % 2) == 0) { byte <<= 4; } else { bytes[nibble_count/2] = byte; byte = 0; } nibble_count++; } } if(nibble_count & 1) { /* Last nibble will be padded as zero */ bytes[nibble_count++/2] = byte; } *size = nibble_count / 2; return bytes; }
int bighash_table_init(bighash_table_t *table, int bucket_count) { if(table->buckets == NULL) { table->buckets = aim_zmalloc(sizeof(table->buckets[0])*bucket_count); table->flags |= BIGHASH_TABLE_F_BUCKETS_ALLOCATED; } table->bucket_count = bucket_count; bighash_table_init_buckets__(table); return 0; }
ucli_tokens_t* ucli_tokens(const char* string, const char* delim) { const char** tokens; char* tok; int count; char* saveptr; char* s; ucli_tokens_t* rv; /* * Determine how many tokens we'll have */ s = aim_strdup(string); count = 0; tok = aim_strtok_r(s, delim, &saveptr); while(tok) { count++; tok = aim_strtok_r(NULL, delim, &saveptr); } tokens = aim_zmalloc((sizeof(char*)*(count+2))); /* reset string */ UCLI_STRCPY(s, string); tok = aim_strtok_r(s, delim, &saveptr); count = 0; while(tok) { tokens[count++] = tok; tok = aim_strtok_r(NULL, delim, &saveptr); } rv = aim_zmalloc(sizeof(*rv)); rv->count = count; rv->tokens = tokens; rv->_string = s; return rv; }
char* aim_bytes_to_string(uint8_t* data, int size, int columns) { int i; if(columns == 0) { char* rv; char* s; /* All on one line */ rv = aim_malloc(10 + /* size */ 1 + /* : */ size*2 + /* bytes */ 1 /* NULL */); s = rv; s += AIM_SNPRINTF(s, 16, "(%d):", size); for(i = 0; i < size; i++) { s += AIM_SNPRINTF(s, 4, "%.2x", data[i]); } return rv; } else { /** * We will need at least 3*size for all bytes. * Each column requires 10 extra characters, including newline. * */ int c; int i; int len = (size*3) + ( (size/columns+1)*10) + 1; char* s = aim_zmalloc(len+1); char* sp = s; for(i = 0; i < size; i++) { if(i % columns == 0) { if(i != 0) { c = AIM_SNPRINTF(sp, len, "\n"); sp += c, len -= c; } if(size > columns) { c = AIM_SNPRINTF(sp, len, " %.4x: ", i); sp += c, len -= c; } } c = AIM_SNPRINTF(sp, len, "%.2x ", data[i]); sp += c, len -= c; } if((size > columns) && (size % columns != 0)) { AIM_SNPRINTF(sp, len, "\n"); } return s; } }
void platform_int_register(int index, char* desc, int value) { oid tree[] = { 1, 3, 6, 1, 4, 1, 42623, 1, 1, 1, 1, 1}; tree[11] = index; int* v = aim_zmalloc(sizeof(value)); *v = value; netsnmp_register_int_instance(desc, tree, OID_LENGTH(tree), v, NULL); }
int onlp_sysi_onie_data_get(uint8_t** data, int* size) { uint8_t* rdata = aim_zmalloc(256); if(onlp_file_read(rdata, 256, size, EEPROM_NODE(eeprom)) == ONLP_STATUS_OK) { if(*size == 256) { *data = rdata; return ONLP_STATUS_OK; } } aim_free(rdata); *size = 0; return ONLP_STATUS_E_INTERNAL; }
aim_log_handler_t aim_log_handler_create(aim_log_handler_config_t* config) { aim_log_handler_t rv; rv = aim_zmalloc(sizeof(*rv)); AIM_MEMCPY(&rv->config, config, sizeof(rv->config)); if(config->debug_log_name) { rv->config.debug_log_name = aim_strdup(config->debug_log_name); rv->debug_fp = fopen(rv->config.debug_log_name, "a"); rv->debug_lock = aim_sem_create(1); } return rv; }
os_sem_t os_sem_create_flags(int count, uint32_t flags) { os_sem_t s = aim_zmalloc(sizeof(*s)); if(flags & OS_SEM_CREATE_F_TRUE_RELATIVE_TIMEOUTS) { s->efd = eventfd(count, EFD_SEMAPHORE | EFD_NONBLOCK); if(s->efd == -1) { AIM_DIE("eventfd() failed - %s", strerror(errno)); } } else { s->efd = EFD_INVALID; sem_init(&s->sem, 0, count); } return s; }
int aim_datatype_register_struct(aim_datatype_t* dt) { __aim_datatype_t* ndt = NULL; if( (dt == NULL) || (dt->c == 0 && dt->type == NULL) || (dt->from_str == NULL && dt->to_str == NULL) ) { return -1; } if(aim_datatype_find(dt->c, dt->type) != NULL) { return -1; } ndt = aim_zmalloc(sizeof(*ndt)); ndt->dt = *dt; list_push(&aim_datatype_list__, &ndt->links); return 0; }
int bighash_table_init(bighash_table_t *table, int bucket_count) { if (bucket_count == BIGHASH_AUTOGROW) { bucket_count = BIGHASH_CONFIG_INITIAL_HASH_BUCKETS_SIZE; table->flags |= BIGHASH_TABLE_F_AUTOGROW; } if(table->buckets == NULL) { table->buckets = aim_zmalloc(sizeof(table->buckets[0])*bucket_count); table->flags |= BIGHASH_TABLE_F_BUCKETS_ALLOCATED; } table->bucket_count = bucket_count; bighash_table_init_buckets__(table); return 0; }
char* ucli_util_data_to_string(ucli_context_t* uc, uint8_t* data, int size, int columns) { int c; int i; /** * We will need at least 3*size for all bytes. * Each column requires 8 extra characters, including newline. * */ int len = (size*3) + (columns*8) + 1; char* s = aim_zmalloc(len+1); char* sp = s; AIM_REFERENCE(uc); for(i = 0; i < size; i++) { if(i % columns == 0) { if(i != 0) { c = aim_snprintf(sp, len, "\n"); sp += c, len -= c; } if(size > columns) { c = aim_snprintf(sp, len, " %.4x: ", i); sp += c, len -= c; } } c = aim_snprintf(sp, len, "%.2x ", data[i]); sp += c, len -= c; } if((size > columns) && (size % columns != 0)) { c = aim_snprintf(sp, len, "\n"); sp += c, len -= c; } return s; }
static ucli_status_t ppe_ucli_utm__get__(ucli_context_t* uc) { ppe_field_info_t* fi; UCLI_COMMAND_INFO(uc, "get", 1, "Get a packet field."); UCLI_ARGPARSE_OR_RETURN(uc, "{ppe_field_info}", &fi); PPE_FIELD_EXISTS_OR_RETURN(uc, fi->field); if(fi->size_bits <= 32) { uint32_t value; PPE_FIELD_GET_OR_RETURN(uc, &ppec->ppep, fi->field, &value); ucli_printf(uc, "%{ppe_field} = 0x%x (%d)\n", fi->field, value, value); return UCLI_STATUS_OK; } else { int rv; int size = fi->size_bits/8; uint8_t* data = aim_zmalloc(size); rv = ppe_wide_field_get(&ppec->ppep, fi->field, data); if(rv < 0) { ucli_e_internal(uc, "ppe_wide_field_get(%{ppe_field})", fi->field); } else { ucli_printf(uc, "%{ppe_field} = %{data}\n", data, size); } aim_free(data); return rv; } }
vpi_ip_endpoint_t* vpi_parse_ip_endpoint(const char* string) { vpi_ip_endpoint_t* ne; char** tokens; int len = 0; char** arg; if( (tokens = vpi_parse_array(string, ":", &len)) == NULL) { return NULL; } if(len != 2 && len != 3) { /* Need between 2 and 3 tokens only */ vpi_parse_arrayFree(tokens); return NULL; } arg = tokens; /* * First token must be 'send', or 's', or 'recv', or 'r' */ if(VPI_STRCMP(*arg, "send") && VPI_STRCMP(*arg, "recv") && VPI_STRCMP(*arg, "r") && VPI_STRCMP(*arg, "s")) { /* None of them */ vpi_parse_arrayFree(tokens); return NULL; } ne = aim_zmalloc(sizeof(*ne)); ne->direction = aim_strdup(*arg); arg++; if(len == 2) { /* The argument must be a port number. */ ne->port = VPI_ATOI(*arg); if(ne->port < 0) { /* Invalid */ goto vpi_parse_ip_endpoint_Error; } else { /* localhost is implied if not specified */ ne->host = aim_strdup("0.0.0.0"); } } else { ne->host = aim_strdup(*arg); arg++; ne->port = VPI_ATOI(*arg); if(ne->port <= 0) { /* Invalid */ goto vpi_parse_ip_endpoint_Error; } } /* Success - 'ne' is now initialized */ vpi_parse_arrayFree(tokens); return ne; vpi_parse_ip_endpoint_Error: vpi_parse_arrayFree(tokens); vpi_free_ip_endpoint(&ne); return NULL; }
int vpi_mmap_interface_create(vpi_interface_t** vi, char* args[], int flags, const char* vpi_name_ptr) { struct sockaddr_ll sockaddr; struct packet_mreq sockparams; struct tpacket_req treq; struct ifreq ifreq; vpi_interface_mmap_t* nvi = aim_zmalloc(sizeof(*nvi)); char** arg = args; int version; int i; frame_control_t* fc; AIM_REFERENCE(flags); if(nvi == NULL) { VPI_MERROR("interface allocation failed for %s.", vpi_name_ptr); return -1; } /* * Point our log_string to our name so we can use it immediately * in log messages. */ nvi->log_string = vpi_name_ptr; /* * The first argument is the type -- skip for now */ arg++; aim_strlcpy(nvi->interface_name, *arg, sizeof(nvi->interface_name)); /* Create RAW socket */ if((nvi->fd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL))) < 0) { VPI_ERROR(nvi, "socket() failed: %s\n", strerror(errno)); aim_free(nvi); return -1; } /* Set version */ version = TPACKET_V2; if(setsockopt(nvi->fd, SOL_PACKET, PACKET_VERSION, &version, sizeof(version)) < 0) { VPI_ERROR(nvi, "setsockopt() version failed: %s\n", strerror(errno)); aim_free(nvi); return -1; } /* * Get the interface index for the requested interface, as specified * in the current argument. */ VPI_MEMSET(&ifreq, 0, sizeof(ifreq)); aim_strlcpy(ifreq.ifr_name, nvi->interface_name, IFNAMSIZ); if(ioctl(nvi->fd, SIOCGIFINDEX, &ifreq) < 0) { VPI_ERROR(nvi, "ioctl() failed: %s", strerror(errno)); close(nvi->fd); aim_free(nvi); return -1; } nvi->ifindex = ifreq.ifr_ifindex; VPI_INFO(nvi, "ifndex is %d", nvi->ifindex); /* Set promisc */ VPI_MEMSET(&sockparams, 0, sizeof(sockparams)); sockparams.mr_type = PACKET_MR_PROMISC; sockparams.mr_ifindex = nvi->ifindex; if(setsockopt(nvi->fd, SOL_PACKET, PACKET_ADD_MEMBERSHIP, (void *)&sockparams, sizeof(sockparams)) < 0) { VPI_ERROR(nvi, "setsockopt() promisc failed. %s\n", strerror(errno)); close(nvi->fd); aim_free(nvi); return -1; } /* Set up rx_ring buffer */ VPI_MEMSET(&treq, 0, sizeof(treq)); treq.tp_block_size = BLOCK_SIZE; treq.tp_frame_size = FRAME_SIZE; treq.tp_block_nr = NUM_BLOCKS; treq.tp_frame_nr = NUM_FRAMES;; if(setsockopt(nvi->fd, SOL_PACKET, PACKET_RX_RING, (void*)&treq , sizeof(treq)) < 0) { VPI_ERROR(nvi, "setsockopt() rx_ring failed. %s\n", strerror(errno)); close(nvi->fd); aim_free(nvi); return -1; } /* If num blocks change, bail! */ if(treq.tp_block_nr != NUM_BLOCKS) { AIM_DIE("Unhandled: RX_RING block number changed!\n"); } /* Set up rx_ring */ nvi->rx_ring.buf = mmap(NULL, BLOCK_SIZE*NUM_BLOCKS, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_LOCKED, nvi->fd, 0); if(nvi->rx_ring.buf == MAP_FAILED) { VPI_ERROR(nvi, "mmap() failed.\n"); close(nvi->fd); aim_free(nvi); return -1; } /* Set up rx_ring and frame controls */ nvi->rx_ring.current_frame = 0; for(i = 0; i < NUM_FRAMES; i++) { fc = &nvi->rx_ring.frames[i]; fc->base = nvi->rx_ring.buf + (i*FRAME_SIZE); } /* Bind */ VPI_MEMSET(&sockaddr, 0, sizeof(sockaddr)); sockaddr.sll_family = AF_PACKET; sockaddr.sll_protocol = htons(ETH_P_ALL); sockaddr.sll_ifindex = nvi->ifindex; if(bind(nvi->fd, (struct sockaddr*)&sockaddr, sizeof(sockaddr)) < 0) { VPI_ERROR(nvi, "bind() failed.\n"); close(nvi->fd); aim_free(nvi); return -1; } nvi->interface.send = vpi_mmap_interface_send; nvi->interface.recv = vpi_mmap_interface_recv; nvi->interface.recv_ready = vpi_mmap_interface_recv_ready; nvi->interface.destroy = vpi_mmap_interface_destroy; nvi->interface.descriptor = vpi_mmap_interface_descriptor; *vi = (vpi_interface_t*)nvi; return 0; }