int ofp_timer_cancel(odp_timer_t tim) { odp_event_t timeout_event = ODP_EVENT_INVALID; odp_timeout_t tmo; uint32_t t = (uint32_t)tim; struct ofp_timer_internal *bufdata; struct ofp_timer_internal *prev = NULL; if (tim == ODP_TIMER_INVALID) return 0; if (t & 0x80000000) { /* long timeout */ odp_spinlock_lock(&shm->lock); bufdata = shm->long_table[t & TIMER_LONG_MASK]; while (bufdata) { struct ofp_timer_internal *next = bufdata->next; if (bufdata->id == t) { if (prev == NULL) shm->long_table[t & TIMER_LONG_MASK] = next; else prev->next = next; odp_buffer_free(bufdata->buf); odp_spinlock_unlock(&shm->lock); return 0; } prev = bufdata; bufdata = next; } odp_spinlock_unlock(&shm->lock); return -1; } else { if (odp_timer_cancel(tim, &timeout_event) < 0) { OFP_WARN("Timeout already expired or inactive"); return 0; } if (timeout_event != ODP_EVENT_INVALID) { tmo = odp_timeout_from_event(timeout_event); bufdata = odp_timeout_user_ptr(tmo); odp_buffer_free(bufdata->buf); odp_timeout_free(tmo); } else { OFP_WARN("Lost timeout buffer at timer cancel"); return -1; } if (odp_timer_free(tim) != ODP_EVENT_INVALID) { OFP_ERR("odp_timer_free failed"); return -1; } } return 0; }
static void *preallocated_alloc(const char *name, uint64_t size) { struct ofp_shm_block *block; if (!shm) { OFP_DBG_SHM("Not initialized"); return NULL; } block = ofp_shm_block_find(name); if (!block) { OFP_DBG_SHM("Not found in the preallocated memory"); return NULL; } if (!shm->shared_memory) { OFP_ERR("Allocation of preallocated shared memory before " "preallocation phase has finished. Name: %s", name); return NULL; } if (block->allocated) { OFP_ERR("Shared memory (name: %s) already allocated", name); return NULL; } if (block->size < size) { OFP_WARN("Shared memory allocation (name: %s) larger than " "the corresponding preallocation: " "(%" PRIu64 " > %" PRIu64 ")", name, size, block->size); return NULL; } block->allocated = 1; return &shm->shared_memory[block->offset]; }
void ofp_shared_memory_prealloc(const char *name, uint64_t size) { struct ofp_shm_block *block; OFP_DBG_SHM("Shared memory preallocation: name: %s, size: %" PRIu64, name, size); if (!shm) { OFP_ERR("Shared memory preallocated before initialization"); return; } if (shm->shared_memory) { OFP_WARN("Shared memory preallocated too late"); return; } if (ofp_shm_block_find(name) != NULL) { OFP_ERR("Duplicate shared memory preallocation (name: %s)", name); return; } if (shm->next_free >= OFP_SHM_BLOCKS_MAX) { OFP_WARN("Shared memory preallocation table full."); return; } block = &shm->block[shm->next_free++]; block->valid = 1; block->allocated = 0; strncpy(block->name, name, sizeof(block->name)); block->name[sizeof(block->name) - 1] = 0; /* Round up to a multiple of cache line size */ block->size = (size + ODP_CACHE_LINE_SIZE - 1) / ODP_CACHE_LINE_SIZE; block->size = block->size * ODP_CACHE_LINE_SIZE; block->offset = shm->total_size; shm->total_size += block->size; OFP_DBG_SHM("Shared memory blocks preallocated so far: %d", shm->next_free); OFP_DBG_SHM("Shared memory bytes preallocated so far: %" PRIu64, shm->total_size); }
/* Sending function with some debugging. */ static int mysend(int s, char *p, int len) { int n; while (len > 0) { n = ofp_send(s, p, len, 0); if (n < 0) { OFP_ERR("ofp_send failed n=%d, err='%s'", n, ofp_strerror(ofp_errno)); return n; } len -= n; p += n; if (len) { OFP_WARN("Only %d bytes sent", n); } } return len; }