diff options
Diffstat (limited to 'src/lib/shm_rbuff.c')
| -rw-r--r-- | src/lib/shm_rbuff.c | 87 |
1 files changed, 46 insertions, 41 deletions
diff --git a/src/lib/shm_rbuff.c b/src/lib/shm_rbuff.c index 453f5183..22cff41c 100644 --- a/src/lib/shm_rbuff.c +++ b/src/lib/shm_rbuff.c @@ -1,10 +1,10 @@ /* - * Ouroboros - Copyright (C) 2016 - 2018 + * Ouroboros - Copyright (C) 2016 - 2024 * - * Ring buffer implementations for incoming SDUs + * Ring buffer implementations for incoming packets * - * Dimitri Staessens <dimitri.staessens@ugent.be> - * Sander Vrijders <sander.vrijders@ugent.be> + * Dimitri Staessens <dimitri@ouroboros.rocks> + * Sander Vrijders <sander@ouroboros.rocks> * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public License @@ -26,33 +26,33 @@ #include <ouroboros/shm_rbuff.h> #include <ouroboros/lockfile.h> -#include <ouroboros/time_utils.h> #include <ouroboros/errno.h> #include <ouroboros/fccntl.h> +#include <ouroboros/pthread.h> +#include <ouroboros/time.h> -#include <pthread.h> -#include <sys/mman.h> +#include <assert.h> #include <fcntl.h> +#include <signal.h> +#include <stdbool.h> +#include <stdint.h> +#include <stdio.h> #include <stdlib.h> #include <string.h> -#include <stdio.h> -#include <stdint.h> #include <unistd.h> -#include <signal.h> +#include <sys/mman.h> #include <sys/stat.h> -#include <assert.h> -#include <stdbool.h> #define FN_MAX_CHARS 255 -#define SHM_RB_FILE_SIZE ((SHM_BUFFER_SIZE) * sizeof(ssize_t) \ +#define SHM_RB_FILE_SIZE ((SHM_RBUFF_SIZE) * sizeof(ssize_t) \ + 3 * sizeof(size_t) \ + sizeof(pthread_mutex_t) \ + 2 * sizeof (pthread_cond_t)) -#define shm_rbuff_used(rb) ((*rb->head + (SHM_BUFFER_SIZE) - *rb->tail) \ - & ((SHM_BUFFER_SIZE) - 1)) -#define shm_rbuff_free(rb) (shm_rbuff_used(rb) + 1 < (SHM_BUFFER_SIZE)) +#define shm_rbuff_used(rb) ((*rb->head + (SHM_RBUFF_SIZE) - *rb->tail) \ + & ((SHM_RBUFF_SIZE) - 1)) +#define shm_rbuff_free(rb) (shm_rbuff_used(rb) + 1 < (SHM_RBUFF_SIZE)) #define shm_rbuff_empty(rb) (*rb->head == *rb->tail) #define head_el_ptr(rb) (rb->shm_base + *rb->head) #define tail_el_ptr(rb) (rb->shm_base + *rb->tail) @@ -63,33 +63,24 @@ struct shm_rbuff { size_t * tail; /* start of ringbuffer tail */ size_t * acl; /* access control */ pthread_mutex_t * lock; /* lock all free space in shm */ - pthread_cond_t * add; /* SDU arrived */ - pthread_cond_t * del; /* SDU removed */ + pthread_cond_t * add; /* packet arrived */ + pthread_cond_t * del; /* packet removed */ pid_t pid; /* pid of the owner */ - int port_id; /* port_id of the flow */ + int flow_id; /* flow_id of the flow */ }; -void shm_rbuff_close(struct shm_rbuff * rb) -{ - assert(rb); - - munmap(rb->shm_base, SHM_RB_FILE_SIZE); - - free(rb); -} - #define MM_FLAGS (PROT_READ | PROT_WRITE) -struct shm_rbuff * rbuff_create(pid_t pid, - int port_id, - int flags) +static struct shm_rbuff * rbuff_create(pid_t pid, + int flow_id, + int flags) { struct shm_rbuff * rb; int fd; ssize_t * shm_base; char fn[FN_MAX_CHARS]; - sprintf(fn, SHM_RBUFF_PREFIX "%d.%d", pid, port_id); + sprintf(fn, SHM_RBUFF_PREFIX "%d.%d", pid, flow_id); rb = malloc(sizeof(*rb)); if (rb == NULL) @@ -99,7 +90,7 @@ struct shm_rbuff * rbuff_create(pid_t pid, if (fd == -1) goto fail_open; - if ((flags & O_CREAT) && ftruncate(fd, SHM_RB_FILE_SIZE - 1) < 0) + if ((flags & O_CREAT) && ftruncate(fd, SHM_RB_FILE_SIZE) < 0) goto fail_truncate; shm_base = mmap(NULL, SHM_RB_FILE_SIZE, MM_FLAGS, MAP_SHARED, fd, 0); @@ -109,14 +100,14 @@ struct shm_rbuff * rbuff_create(pid_t pid, close(fd); rb->shm_base = shm_base; - rb->head = (size_t *) (rb->shm_base + (SHM_BUFFER_SIZE)); + rb->head = (size_t *) (rb->shm_base + (SHM_RBUFF_SIZE)); rb->tail = rb->head + 1; rb->acl = rb->tail + 1; rb->lock = (pthread_mutex_t *) (rb->acl + 1); rb->add = (pthread_cond_t *) (rb->lock + 1); rb->del = rb->add + 1; rb->pid = pid; - rb->port_id = port_id; + rb->flow_id = flow_id; return rb; @@ -130,8 +121,15 @@ struct shm_rbuff * rbuff_create(pid_t pid, return NULL; } +static void rbuff_destroy(struct shm_rbuff * rb) +{ + munmap(rb->shm_base, SHM_RB_FILE_SIZE); + + free(rb); +} + struct shm_rbuff * shm_rbuff_create(pid_t pid, - int port_id) + int flow_id) { struct shm_rbuff * rb; pthread_mutexattr_t mattr; @@ -140,7 +138,7 @@ struct shm_rbuff * shm_rbuff_create(pid_t pid, mask = umask(0); - rb = rbuff_create(pid, port_id, O_CREAT | O_EXCL | O_RDWR); + rb = rbuff_create(pid, flow_id, O_CREAT | O_EXCL | O_RDWR); umask(mask); @@ -174,8 +172,8 @@ struct shm_rbuff * shm_rbuff_create(pid_t pid, *rb->head = 0; *rb->tail = 0; - rb->pid = pid; - rb->port_id = port_id; + rb->pid = pid; + rb->flow_id = flow_id; pthread_mutexattr_destroy(&mattr); pthread_condattr_destroy(&cattr); @@ -197,9 +195,16 @@ struct shm_rbuff * shm_rbuff_create(pid_t pid, } struct shm_rbuff * shm_rbuff_open(pid_t pid, - int port_id) + int flow_id) +{ + return rbuff_create(pid, flow_id, O_RDWR); +} + +void shm_rbuff_close(struct shm_rbuff * rb) { - return rbuff_create(pid, port_id, O_RDWR); + assert(rb); + + rbuff_destroy(rb); } #if (defined(SHM_RBUFF_LOCKLESS) && \ |
