summaryrefslogtreecommitdiff
path: root/src/ipcpd/normal/main.c
diff options
context:
space:
mode:
authordimitri staessens <[email protected]>2016-09-18 06:27:43 +0200
committerdimitri staessens <[email protected]>2016-10-04 15:16:00 +0200
commitc96efb13edfaf9b2f2c626bd2a5d5d5afd38155f (patch)
treeacd08f09f5a094e897020e97961b2847209df043 /src/ipcpd/normal/main.c
parent2e561a634ae3e747b293a4e05eaf44726968dc1a (diff)
downloadouroboros-c96efb13edfaf9b2f2c626bd2a5d5d5afd38155f.tar.gz
ouroboros-c96efb13edfaf9b2f2c626bd2a5d5d5afd38155f.zip
lib, ipcp: Revise fast path and flow interfaces
IPCPs can now use ap_init() to initialize the memory. All flows are accessed using flow descriptors, this greatly simplifies IPCP development. Reverts the fast path to a single ap_rbuff per process. Splits lib/ipcp into irmd/ipcp and lib/ipcp-dev. Adds a lib/shim-dev holding tailored functions for shims. Moves the buffer_t to utils.h. Fixes the shim-eth-llc length field. Removes the flow from shared.h. Fixes #4 Fixes #5
Diffstat (limited to 'src/ipcpd/normal/main.c')
-rw-r--r--src/ipcpd/normal/main.c172
1 files changed, 36 insertions, 136 deletions
diff --git a/src/ipcpd/normal/main.c b/src/ipcpd/normal/main.c
index 082973f4..4611408d 100644
--- a/src/ipcpd/normal/main.c
+++ b/src/ipcpd/normal/main.c
@@ -24,10 +24,8 @@
#include <ouroboros/config.h>
#include <ouroboros/logs.h>
-#include <ouroboros/shm_rdrbuff.h>
-#include <ouroboros/shm_ap_rbuff.h>
#include <ouroboros/dev.h>
-#include <ouroboros/ipcp.h>
+#include <ouroboros/ipcp-dev.h>
#include <ouroboros/time_utils.h>
#include <stdbool.h>
@@ -47,26 +45,8 @@
/* global for trapping signal */
int irmd_api;
-struct ipcp * _ipcp;
-
-#define normal_data(type) ((struct normal_ipcp_data *) type->data)
-
-struct normal_ipcp_data {
- /* Keep ipcp_data first for polymorphism. */
- struct ipcp_data ipcp_data;
-
- struct shm_rdrbuff * rdrb;
- struct shm_ap_rbuff * rb;
-
- pthread_t mainloop;
-};
-
void ipcp_sig_handler(int sig, siginfo_t * info, void * c)
{
- sigset_t sigset;
- sigemptyset(&sigset);
- sigaddset(&sigset, SIGINT);
-
switch(sig) {
case SIGINT:
case SIGTERM:
@@ -75,11 +55,11 @@ void ipcp_sig_handler(int sig, siginfo_t * info, void * c)
LOG_DBG("IPCP %d terminating by order of %d. Bye.",
getpid(), info->si_pid);
- pthread_rwlock_wrlock(&_ipcp->state_lock);
+ pthread_rwlock_wrlock(&ipcpi.state_lock);
- ipcp_set_state(_ipcp, IPCP_SHUTDOWN);
+ ipcp_set_state(IPCP_SHUTDOWN);
- pthread_rwlock_unlock(&_ipcp->state_lock);
+ pthread_rwlock_unlock(&ipcpi.state_lock);
}
default:
return;
@@ -88,15 +68,15 @@ void ipcp_sig_handler(int sig, siginfo_t * info, void * c)
static int normal_ipcp_name_reg(char * name)
{
- pthread_rwlock_rdlock(&_ipcp->state_lock);
+ pthread_rwlock_rdlock(&ipcpi.state_lock);
- if (ipcp_data_add_reg_entry(_ipcp->data, name)) {
- pthread_rwlock_unlock(&_ipcp->state_lock);
+ if (ipcp_data_add_reg_entry(ipcpi.data, name)) {
+ pthread_rwlock_unlock(&ipcpi.state_lock);
LOG_ERR("Failed to add %s to local registry.", name);
return -1;
}
- pthread_rwlock_unlock(&_ipcp->state_lock);
+ pthread_rwlock_unlock(&ipcpi.state_lock);
LOG_DBG("Registered %s.", name);
@@ -105,11 +85,11 @@ static int normal_ipcp_name_reg(char * name)
static int normal_ipcp_name_unreg(char * name)
{
- pthread_rwlock_rdlock(&_ipcp->state_lock);
+ pthread_rwlock_rdlock(&ipcpi.state_lock);
- ipcp_data_del_reg_entry(_ipcp->data, name);
+ ipcp_data_del_reg_entry(ipcpi.data, name);
- pthread_rwlock_unlock(&_ipcp->state_lock);
+ pthread_rwlock_unlock(&ipcpi.state_lock);
return 0;
}
@@ -119,59 +99,59 @@ static int normal_ipcp_enroll(char * dif_name)
struct timespec timeout = {(ENROLL_TIMEOUT / 1000),
(ENROLL_TIMEOUT % 1000) * MILLION};
- pthread_rwlock_rdlock(&_ipcp->state_lock);
+ pthread_rwlock_rdlock(&ipcpi.state_lock);
- if (ipcp_get_state(_ipcp) != IPCP_INIT) {
- pthread_rwlock_unlock(&_ipcp->state_lock);
+ if (ipcp_get_state() != IPCP_INIT) {
+ pthread_rwlock_unlock(&ipcpi.state_lock);
LOG_ERR("Won't enroll an IPCP that is not in INIT.");
return -1; /* -ENOTINIT */
}
- pthread_rwlock_unlock(&_ipcp->state_lock);
+ pthread_rwlock_unlock(&ipcpi.state_lock);
if (fmgr_mgmt_flow(dif_name)) {
LOG_ERR("Failed to establish management flow.");
return -1;
}
- if (ipcp_wait_state(_ipcp, IPCP_ENROLLED, &timeout) == -ETIMEDOUT) {
+ if (ipcp_wait_state(IPCP_ENROLLED, &timeout) == -ETIMEDOUT) {
LOG_ERR("Enrollment timed out.");
return -1;
}
- pthread_rwlock_rdlock(&_ipcp->state_lock);
+ pthread_rwlock_rdlock(&ipcpi.state_lock);
- if (ipcp_get_state(_ipcp) != IPCP_ENROLLED) {
- pthread_rwlock_unlock(&_ipcp->state_lock);
+ if (ipcp_get_state() != IPCP_ENROLLED) {
+ pthread_rwlock_unlock(&ipcpi.state_lock);
return -1;
}
- pthread_rwlock_unlock(&_ipcp->state_lock);
+ pthread_rwlock_unlock(&ipcpi.state_lock);
return 0;
}
static int normal_ipcp_bootstrap(struct dif_config * conf)
{
- pthread_rwlock_wrlock(&_ipcp->state_lock);
+ pthread_rwlock_wrlock(&ipcpi.state_lock);
- if (ipcp_get_state(_ipcp) != IPCP_INIT) {
- pthread_rwlock_unlock(&_ipcp->state_lock);
+ if (ipcp_get_state() != IPCP_INIT) {
+ pthread_rwlock_unlock(&ipcpi.state_lock);
LOG_ERR("Won't bootstrap an IPCP that is not in INIT.");
return -1; /* -ENOTINIT */
}
if (ribmgr_bootstrap(conf)) {
- pthread_rwlock_unlock(&_ipcp->state_lock);
+ pthread_rwlock_unlock(&ipcpi.state_lock);
LOG_ERR("Failed to bootstrap RIB manager.");
return -1;
}
- ipcp_set_state(_ipcp, IPCP_ENROLLED);
+ ipcp_set_state(IPCP_ENROLLED);
- _ipcp->data->dif_name = conf->dif_name;
+ ipcpi.data->dif_name = conf->dif_name;
- pthread_rwlock_unlock(&_ipcp->state_lock);
+ pthread_rwlock_unlock(&ipcpi.state_lock);
LOG_DBG("Bootstrapped in DIF %s.", conf->dif_name);
@@ -188,67 +168,6 @@ static struct ipcp_ops normal_ops = {
.ipcp_flow_dealloc = fmgr_flow_dealloc
};
-struct normal_ipcp_data * normal_ipcp_data_create()
-{
- struct normal_ipcp_data * normal_data;
- enum ipcp_type ipcp_type;
-
- normal_data = malloc(sizeof(*normal_data));
- if (normal_data == NULL) {
- LOG_ERR("Failed to allocate.");
- return NULL;
- }
-
- ipcp_type = THIS_TYPE;
- if (ipcp_data_init((struct ipcp_data *) normal_data,
- ipcp_type) == NULL) {
- free(normal_data);
- return NULL;
- }
-
- normal_data->rdrb = shm_rdrbuff_open();
- if (normal_data->rdrb == NULL) {
- free(normal_data);
- return NULL;
- }
-
- normal_data->rb = shm_ap_rbuff_create_n();
- if (normal_data->rb == NULL) {
- shm_rdrbuff_close(normal_data->rdrb);
- free(normal_data);
- return NULL;
- }
-
- return normal_data;
-}
-
-
-void normal_ipcp_data_destroy()
-{
- int idx = 0;
-
- if (_ipcp == NULL)
- return;
-
- pthread_rwlock_rdlock(&_ipcp->state_lock);
-
- if (ipcp_get_state(_ipcp) != IPCP_SHUTDOWN)
- LOG_WARN("Cleaning up while not in shutdown.");
-
- /* remove all remaining sdus */
- while ((idx = shm_ap_rbuff_peek_idx(normal_data(_ipcp)->rb)) >= 0)
- shm_rdrbuff_remove(normal_data(_ipcp)->rdrb, idx);
-
- if (normal_data(_ipcp)->rdrb != NULL)
- shm_rdrbuff_close(normal_data(_ipcp)->rdrb);
- if (normal_data(_ipcp)->rb != NULL)
- shm_ap_rbuff_close(normal_data(_ipcp)->rb);
-
- ipcp_data_destroy(_ipcp->data);
-
- pthread_rwlock_unlock(&_ipcp->state_lock);
-}
-
int main(int argc, char * argv[])
{
struct sigaction sig_act;
@@ -285,56 +204,38 @@ int main(int argc, char * argv[])
sigaction(SIGHUP, &sig_act, NULL);
sigaction(SIGPIPE, &sig_act, NULL);
- _ipcp = ipcp_instance_create();
- if (_ipcp == NULL) {
- LOG_ERR("Failed to create instance.");
- close_logfile();
- exit(EXIT_FAILURE);
- }
+ pthread_sigmask(SIG_BLOCK, &sigset, NULL);
- _ipcp->data = (struct ipcp_data *) normal_ipcp_data_create();
- if (_ipcp->data == NULL) {
- LOG_ERR("Failed to create instance data.");
- free(_ipcp);
+ if (ipcp_init(THIS_TYPE, &normal_ops) < 0) {
+ LOG_ERR("Failed to create instance.");
close_logfile();
exit(EXIT_FAILURE);
}
- _ipcp->ops = &normal_ops;
- _ipcp->state = IPCP_INIT;
+ pthread_sigmask(SIG_UNBLOCK, &sigset, NULL);
if (fmgr_init()) {
- normal_ipcp_data_destroy();
- free(_ipcp);
+ ipcp_fini();
close_logfile();
exit(EXIT_FAILURE);
}
if (ribmgr_init()) {
- normal_ipcp_data_destroy();
fmgr_fini();
- free(_ipcp);
+ ipcp_fini();
close_logfile();
exit(EXIT_FAILURE);
}
- pthread_sigmask(SIG_BLOCK, &sigset, NULL);
-
- pthread_create(&normal_data(_ipcp)->mainloop, NULL,
- ipcp_main_loop, _ipcp);
-
- pthread_sigmask(SIG_UNBLOCK, &sigset, NULL);
-
if (ipcp_create_r(getpid())) {
LOG_ERR("Failed to notify IRMd we are initialized.");
- normal_ipcp_data_destroy();
fmgr_fini();
- free(_ipcp);
+ ipcp_fini();
close_logfile();
exit(EXIT_FAILURE);
}
- pthread_join(normal_data(_ipcp)->mainloop, NULL);
+ ipcp_fini();
if (fmgr_fini())
LOG_ERR("Failed to finalize flow manager.");
@@ -345,10 +246,9 @@ int main(int argc, char * argv[])
if (frct_fini())
LOG_ERR("Failed to finalize FRCT.");
- normal_ipcp_data_destroy();
- free(_ipcp);
close_logfile();
ap_fini();
+
exit(EXIT_SUCCESS);
}