summaryrefslogtreecommitdiff
path: root/src/lib
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/crypt.c3
-rw-r--r--src/lib/dev.c131
-rw-r--r--src/lib/frct.c2
-rw-r--r--src/lib/shm_flow_set.c12
-rw-r--r--src/lib/shm_rdrbuff.c7
-rw-r--r--src/lib/timerwheel.c2
6 files changed, 87 insertions, 70 deletions
diff --git a/src/lib/crypt.c b/src/lib/crypt.c
index e19981bc..2985fc6a 100644
--- a/src/lib/crypt.c
+++ b/src/lib/crypt.c
@@ -282,8 +282,7 @@ static int openssl_decrypt(struct flow * f,
int in_sz;
int tmp_sz;
- in = shm_du_buff_head(sdb);
- in_sz = shm_du_buff_tail(sdb) - in;
+ in_sz = shm_du_buff_len(sdb);
if (in_sz < IVSZ)
return -ECRYPT;
diff --git a/src/lib/dev.c b/src/lib/dev.c
index d73205e2..5d12beec 100644
--- a/src/lib/dev.c
+++ b/src/lib/dev.c
@@ -93,7 +93,7 @@ struct flow {
struct shm_rbuff * tx_rb;
struct shm_flow_set * set;
int flow_id;
- int oflags;
+ uint16_t oflags;
qosspec_t qs;
ssize_t part_idx;
@@ -120,7 +120,7 @@ struct flow_set_entry {
};
struct flow_set {
- size_t idx;
+ size_t idx;
struct timespec chk; /* Last keepalive check. */
uint32_t min; /* Minimum keepalive time in set. */
@@ -1214,6 +1214,52 @@ ssize_t flow_write(int fd,
return -ENOMEM;
}
+static bool invalid_pkt(struct flow * flow,
+ struct shm_du_buff * sdb)
+{
+ if (shm_du_buff_len(sdb) == 0)
+ return true;
+
+ if (flow->qs.ber == 0 && chk_crc(sdb) != 0)
+ return true;
+
+ if (flow->qs.cypher_s > 0 && crypt_decrypt(flow, sdb) < 0)
+ return true;
+
+ return false;
+}
+
+static ssize_t flow_rx_sdb(struct flow * flow,
+ struct shm_du_buff ** sdb,
+ bool block,
+ struct timespec * abstime)
+{
+ ssize_t idx;
+ struct timespec now;
+
+ idx = block ? shm_rbuff_read_b(flow->rx_rb, abstime) :
+ shm_rbuff_read(flow->rx_rb);
+ if (idx < 0)
+ return idx;
+
+ *sdb = shm_rdrbuff_get(ai.rdrb, idx);
+
+ clock_gettime(PTHREAD_COND_CLOCK, &now);
+
+ pthread_rwlock_wrlock(&ai.lock);
+
+ flow->rcv_act = now;
+
+ pthread_rwlock_unlock(&ai.lock);
+
+ if (invalid_pkt(flow, *sdb)) {
+ shm_rdrbuff_remove(ai.rdrb, idx);
+ return -EAGAIN;
+ }
+
+ return idx;
+}
+
ssize_t flow_read(int fd,
void * buf,
size_t count)
@@ -1221,7 +1267,6 @@ ssize_t flow_read(int fd,
ssize_t idx;
ssize_t n;
uint8_t * packet;
- struct shm_rbuff * rb;
struct shm_du_buff * sdb;
struct timespec abs;
struct timespec now;
@@ -1229,10 +1274,10 @@ ssize_t flow_read(int fd,
struct timespec tictime;
struct timespec * abstime = NULL;
struct flow * flow;
- bool noblock;
+ bool block;
bool partrd;
- if (fd < 0 || fd > PROG_MAX_FLOWS)
+ if (fd < 0 || fd >= PROG_MAX_FLOWS)
return -EBADF;
flow = &ai.flows[fd];
@@ -1241,24 +1286,23 @@ ssize_t flow_read(int fd,
pthread_rwlock_rdlock(&ai.lock);
- if (flow->part_idx == DONE_PART) {
+ if (flow->flow_id < 0) {
pthread_rwlock_unlock(&ai.lock);
- flow->part_idx = NO_PART;
- return 0;
+ return -ENOTALLOC;
}
- if (flow->flow_id < 0) {
+ if (flow->part_idx == DONE_PART) {
pthread_rwlock_unlock(&ai.lock);
- return -ENOTALLOC;
+ flow->part_idx = NO_PART;
+ return 0;
}
- rb = flow->rx_rb;
- noblock = flow->oflags & FLOWFRNOBLOCK;
+ block = !(flow->oflags & FLOWFRNOBLOCK);
partrd = !(flow->oflags & FLOWFRNOPART);
ts_add(&now, &tic, &tictime);
- if (ai.flows[fd].rcv_timesout) {
+ if (flow->rcv_timesout) {
ts_add(&now, &flow->rcv_timeo, &abs);
abstime = &abs;
}
@@ -1268,8 +1312,7 @@ ssize_t flow_read(int fd,
while ((idx = frcti_queued_pdu(flow->frcti)) < 0) {
pthread_rwlock_unlock(&ai.lock);
- idx = noblock ? shm_rbuff_read(rb) :
- shm_rbuff_read_b(rb, &tictime);
+ idx = flow_rx_sdb(flow, &sdb, block, &tictime);
if (idx < 0) {
frcti_tick(flow->frcti);
@@ -1285,45 +1328,31 @@ ssize_t flow_read(int fd,
ts_add(&tictime, &tic, &tictime);
- pthread_rwlock_wrlock(&ai.lock);
- continue;
- }
-
- sdb = shm_rdrbuff_get(ai.rdrb, idx);
-
- pthread_rwlock_wrlock(&ai.lock);
-
- flow->rcv_act = tictime;
-
- if ((flow->qs.ber == 0 && chk_crc(sdb) != 0) ||
- shm_du_buff_head(sdb) == shm_du_buff_tail(sdb)) {
- shm_rdrbuff_remove(ai.rdrb, idx);
- idx = -EAGAIN;
+ pthread_rwlock_rdlock(&ai.lock);
continue;
}
- if (flow->qs.cypher_s > 0
- && crypt_decrypt(flow, sdb) < 0) {
- pthread_rwlock_unlock(&ai.lock);
- shm_rdrbuff_remove(ai.rdrb, idx);
- return -ENOMEM;
- }
+ pthread_rwlock_rdlock(&ai.lock);
frcti_rcv(flow->frcti, sdb);
}
}
+ sdb = shm_rdrbuff_get(ai.rdrb, idx);
+
frcti_tick(flow->frcti);
pthread_rwlock_unlock(&ai.lock);
- n = shm_rdrbuff_read(&packet, ai.rdrb, idx);
+ packet = shm_du_buff_head(sdb);
+
+ n = shm_du_buff_len(sdb);
assert(n >= 0);
if (n <= (ssize_t) count) {
memcpy(buf, packet, n);
- shm_rdrbuff_remove(ai.rdrb, idx);
+ ipcp_sdb_release(sdb);
pthread_rwlock_wrlock(&ai.lock);
@@ -1337,7 +1366,6 @@ ssize_t flow_read(int fd,
} else {
if (partrd) {
memcpy(buf, packet, count);
- sdb = shm_rdrbuff_get(ai.rdrb, idx);
shm_du_buff_head_release(sdb, n);
pthread_rwlock_wrlock(&ai.lock);
flow->part_idx = idx;
@@ -1347,7 +1375,7 @@ ssize_t flow_read(int fd,
pthread_rwlock_unlock(&ai.lock);
return count;
} else {
- shm_rdrbuff_remove(ai.rdrb, idx);
+ ipcp_sdb_release(sdb);
return -EMSGSIZE;
}
}
@@ -1870,10 +1898,8 @@ int ipcp_flow_alloc_reply(int fd,
int ipcp_flow_read(int fd,
struct shm_du_buff ** sdb)
{
- struct timespec now;
- struct flow * flow;
- struct shm_rbuff * rb;
- ssize_t idx = -1;
+ struct flow * flow;
+ ssize_t idx = -1;
assert(fd >= 0 && fd < SYS_MAX_FLOWS);
assert(sdb);
@@ -1884,27 +1910,14 @@ int ipcp_flow_read(int fd,
assert(flow->flow_id >= 0);
- rb = flow->rx_rb;
-
while ((idx = frcti_queued_pdu(flow->frcti)) < 0) {
pthread_rwlock_unlock(&ai.lock);
- idx = shm_rbuff_read(rb);
+ idx = flow_rx_sdb(flow, sdb, false, NULL);
if (idx < 0)
return idx;
- clock_gettime(PTHREAD_COND_CLOCK, &now);
-
- pthread_rwlock_wrlock(&ai.lock);
-
- *sdb = shm_rdrbuff_get(ai.rdrb, idx);
- if ((flow->qs.ber == 0 && chk_crc(*sdb) != 0) ||
- (shm_du_buff_head(*sdb) == shm_du_buff_tail(*sdb))) {
- shm_rdrbuff_remove(ai.rdrb, idx);
- continue;
- }
-
- flow->rcv_act = now;
+ pthread_rwlock_rdlock(&ai.lock);
frcti_rcv(flow->frcti, *sdb);
}
@@ -1913,8 +1926,6 @@ int ipcp_flow_read(int fd,
pthread_rwlock_unlock(&ai.lock);
- *sdb = shm_rdrbuff_get(ai.rdrb, idx);
-
return 0;
}
diff --git a/src/lib/frct.c b/src/lib/frct.c
index 99962868..a93a1006 100644
--- a/src/lib/frct.c
+++ b/src/lib/frct.c
@@ -681,7 +681,7 @@ static int __frcti_snd(struct frcti * frcti,
bool rtx;
assert(frcti);
- assert(shm_du_buff_head(sdb) != shm_du_buff_tail(sdb));
+ assert(shm_du_buff_len(sdb) != 0);
snd_cr = &frcti->snd_cr;
rcv_cr = &frcti->rcv_cr;
diff --git a/src/lib/shm_flow_set.c b/src/lib/shm_flow_set.c
index 5a9bee6c..d325a253 100644
--- a/src/lib/shm_flow_set.c
+++ b/src/lib/shm_flow_set.c
@@ -96,10 +96,8 @@ static struct shm_flow_set * flow_set_create(pid_t pid,
if (shm_fd == -1)
goto fail_shm_open;
- if (ftruncate(shm_fd, SHM_FLOW_SET_FILE_SIZE - 1) < 0) {
- close(shm_fd);
- goto fail_shm_open;
- }
+ if (ftruncate(shm_fd, SHM_FLOW_SET_FILE_SIZE - 1) < 0)
+ goto fail_truncate;
shm_base = mmap(NULL,
SHM_FLOW_SET_FILE_SIZE,
@@ -108,11 +106,11 @@ static struct shm_flow_set * flow_set_create(pid_t pid,
shm_fd,
0);
- close(shm_fd);
-
if (shm_base == MAP_FAILED)
goto fail_mmap;
+ close(shm_fd);
+
set->mtable = shm_base;
set->heads = (size_t *) (set->mtable + SYS_MAX_FLOWS);
set->conds = (pthread_cond_t *)(set->heads + PROG_MAX_FQUEUES);
@@ -125,6 +123,8 @@ static struct shm_flow_set * flow_set_create(pid_t pid,
fail_mmap:
if (flags & O_CREAT)
shm_unlink(fn);
+ fail_truncate:
+ close(shm_fd);
fail_shm_open:
free(set);
fail_malloc:
diff --git a/src/lib/shm_rdrbuff.c b/src/lib/shm_rdrbuff.c
index dfa45af6..e283388f 100644
--- a/src/lib/shm_rdrbuff.c
+++ b/src/lib/shm_rdrbuff.c
@@ -532,6 +532,13 @@ uint8_t * shm_du_buff_tail(struct shm_du_buff * sdb)
return (uint8_t *) (sdb + 1) + sdb->du_tail;
}
+size_t shm_du_buff_len(struct shm_du_buff * sdb)
+{
+ assert(sdb);
+
+ return sdb->du_tail - sdb->du_head;
+}
+
uint8_t * shm_du_buff_head_alloc(struct shm_du_buff * sdb,
size_t size)
{
diff --git a/src/lib/timerwheel.c b/src/lib/timerwheel.c
index 580f838d..c3be08e0 100644
--- a/src/lib/timerwheel.c
+++ b/src/lib/timerwheel.c
@@ -337,7 +337,7 @@ static int timerwheel_rxm(struct frcti * frcti,
r->mul = 0;
r->seqno = seqno;
r->frcti = frcti;
- r->len = shm_du_buff_tail(sdb) - shm_du_buff_head(sdb);
+ r->len = shm_du_buff_len(sdb);
#ifdef RXM_BUFFER_ON_HEAP
r->pkt = malloc(r->len);
if (r->pkt == NULL) {