diff options
Diffstat (limited to 'src/ipcpd/ipcp.c')
| -rw-r--r-- | src/ipcpd/ipcp.c | 631 |
1 files changed, 386 insertions, 245 deletions
diff --git a/src/ipcpd/ipcp.c b/src/ipcpd/ipcp.c index c2678fc8..ebb9b1c5 100644 --- a/src/ipcpd/ipcp.c +++ b/src/ipcpd/ipcp.c @@ -68,13 +68,69 @@ #define CLOCK_REALTIME_COARSE CLOCK_REALTIME #endif -char * info[LAYER_NAME_SIZE + 1] = { - "_state", - "_type", - "_layer", - NULL +static char * ipcp_type_str[] = { + "local", + "unicast", + "broadcast", + "eth-llc", + "eth-dix", + "udp4", + "udp6" +}; + +static char * dir_hash_str[] = { + "SHA3-224", + "SHA3-256", + "SHA3-384", + "SHA3-512", + "CRC32", + "MD5" +}; + +static char * ipcp_state_str[] = { + "null", + "init", + "boot", + "bootstrapped", + "enrolled", + "operational", + "shutdown" }; +struct { + pid_t irmd_pid; + char * name; + + enum ipcp_type type; + char layer_name[LAYER_NAME_SIZE + 1]; + + uint64_t dt_addr; + + enum hash_algo dir_hash_algo; + + struct ipcp_ops * ops; + int irmd_fd; + + enum ipcp_state state; + pthread_cond_t state_cond; + pthread_mutex_t state_mtx; + + int sockfd; + char * sock_path; + + struct list_head cmds; + pthread_cond_t cmd_cond; + pthread_mutex_t cmd_lock; + + int alloc_id; + pthread_cond_t alloc_cond; + pthread_mutex_t alloc_lock; + + struct tpm * tpm; + + pthread_t acceptor; +} ipcpd; + struct cmd { struct list_head next; @@ -83,9 +139,38 @@ struct cmd { int fd; }; +enum ipcp_type ipcp_get_type(void) +{ + return ipcpd.type; +} + +const char * ipcp_get_name(void) +{ + return ipcpd.name; +} + +void ipcp_set_dir_hash_algo(enum hash_algo algo) +{ + ipcpd.dir_hash_algo = algo; +} + +size_t ipcp_dir_hash_len(void) +{ + return hash_len(ipcpd.dir_hash_algo); +} + +int ipcp_get_layer_name(char * layer) +{ + if (ipcp_get_state() < IPCP_OPERATIONAL) + return -EIPCPSTATE; + + strcpy(layer, ipcpd.layer_name); + return 0; +} + uint8_t * ipcp_hash_dup(const uint8_t * hash) { - uint8_t * dup = malloc(hash_len(ipcpi.dir_hash_algo)); + uint8_t * dup = malloc(hash_len(ipcpd.dir_hash_algo)); if (dup == NULL) return NULL; @@ -109,6 +194,13 @@ void ipcp_hash_str(char * buf, buf[2 * i] = '\0'; } +static const char * info[] = { + "_state", + "_type", + "_layer", + NULL +}; + static int ipcp_rib_read(const char * path, char * buf, size_t len) @@ -136,18 +228,20 @@ static int ipcp_rib_read(const char * path, } if (strcmp(entry, info[1]) == 0) { /* _type */ - if (ipcpi.type == IPCP_LOCAL) + if (ipcpd.type == IPCP_LOCAL) strcpy(buf, "local\n"); - else if (ipcpi.type == IPCP_UNICAST) + else if (ipcpd.type == IPCP_UNICAST) strcpy(buf, "unicast\n"); - else if (ipcpi.type == IPCP_BROADCAST) + else if (ipcpd.type == IPCP_BROADCAST) strcpy(buf, "broadcast\n"); - else if (ipcpi.type == IPCP_ETH_LLC) + else if (ipcpd.type == IPCP_ETH_LLC) strcpy(buf, "eth-llc\n"); - else if (ipcpi.type == IPCP_ETH_DIX) + else if (ipcpd.type == IPCP_ETH_DIX) strcpy(buf, "eth-dix\n"); - else if (ipcpi.type == IPCP_UDP) - strcpy(buf, "udp\n"); + else if (ipcpd.type == IPCP_UDP4) + strcpy(buf, "udp4\n"); + else if (ipcpd.type == IPCP_UDP6) + strcpy(buf, "udp6\n"); else strcpy(buf, "bug\n"); } @@ -157,7 +251,7 @@ static int ipcp_rib_read(const char * path, if (ipcp_get_state() < IPCP_OPERATIONAL) strcpy(buf, "(null)"); else - strcpy(buf, ipcpi.layer_name); + strcpy(buf, ipcpd.layer_name); buf[strlen(buf)] = '\n'; } @@ -169,12 +263,11 @@ static int ipcp_rib_readdir(char *** buf) { int i = 0; - while (info[i] != NULL) - i++; + while (info[i++] != NULL); *buf = malloc(sizeof(**buf) * i); if (*buf == NULL) - goto fail; + goto fail_entries; i = 0; @@ -187,12 +280,11 @@ static int ipcp_rib_readdir(char *** buf) return i; fail_dup: - while (i > 0) - free((*buf)[--i]); - fail: + while (i-- > 0) + free((*buf)[i]); free(*buf); - - return -1; + fail_entries: + return -ENOMEM; } static int ipcp_rib_getattr(const char * path, @@ -222,10 +314,10 @@ static void * acceptloop(void * o) (void) o; while (ipcp_get_state() != IPCP_SHUTDOWN && - ipcp_get_state() != IPCP_NULL) { + ipcp_get_state() != IPCP_INIT) { struct cmd * cmd; - csockfd = accept(ipcpi.sockfd, 0, 0); + csockfd = accept(ipcpd.sockfd, 0, 0); if (csockfd < 0) continue; @@ -253,13 +345,13 @@ static void * acceptloop(void * o) cmd->fd = csockfd; - pthread_mutex_lock(&ipcpi.cmd_lock); + pthread_mutex_lock(&ipcpd.cmd_lock); - list_add(&cmd->next, &ipcpi.cmds); + list_add(&cmd->next, &ipcpd.cmds); - pthread_cond_signal(&ipcpi.cmd_cond); + pthread_cond_signal(&ipcpd.cmd_cond); - pthread_mutex_unlock(&ipcpi.cmd_lock); + pthread_mutex_unlock(&ipcpd.cmd_lock); } return (void *) 0; @@ -280,34 +372,34 @@ int ipcp_wait_flow_req_arr(const uint8_t * dst, clock_gettime(PTHREAD_COND_CLOCK, &abstime); - pthread_mutex_lock(&ipcpi.alloc_lock); + pthread_mutex_lock(&ipcpd.alloc_lock); - while (ipcpi.alloc_id != -1 && ipcp_get_state() == IPCP_OPERATIONAL) { + while (ipcpd.alloc_id != -1 && ipcp_get_state() == IPCP_OPERATIONAL) { ts_add(&abstime, &ts, &abstime); - pthread_cond_timedwait(&ipcpi.alloc_cond, - &ipcpi.alloc_lock, + pthread_cond_timedwait(&ipcpd.alloc_cond, + &ipcpd.alloc_lock, &abstime); } if (ipcp_get_state() != IPCP_OPERATIONAL) { - pthread_mutex_unlock(&ipcpi.alloc_lock); + pthread_mutex_unlock(&ipcpd.alloc_lock); log_err("Won't allocate over non-operational IPCP."); return -EIPCPSTATE; } - assert(ipcpi.alloc_id == -1); + assert(ipcpd.alloc_id == -1); fd = ipcp_flow_req_arr(&hash, qs, mpl, data); if (fd < 0) { - pthread_mutex_unlock(&ipcpi.alloc_lock); + pthread_mutex_unlock(&ipcpd.alloc_lock); log_err("Failed to get fd for flow."); return fd; } - ipcpi.alloc_id = fd; - pthread_cond_broadcast(&ipcpi.alloc_cond); + ipcpd.alloc_id = fd; + pthread_cond_broadcast(&ipcpd.alloc_cond); - pthread_mutex_unlock(&ipcpi.alloc_lock); + pthread_mutex_unlock(&ipcpd.alloc_lock); return fd; @@ -315,31 +407,31 @@ int ipcp_wait_flow_req_arr(const uint8_t * dst, int ipcp_wait_flow_resp(const int fd) { - struct timespec ts = TIMESPEC_INIT_MS(ALLOC_TIMEOUT); - struct timespec abstime; + struct timespec ts = TIMESPEC_INIT_MS(ALLOC_TIMEOUT); + struct timespec abstime; clock_gettime(PTHREAD_COND_CLOCK, &abstime); - pthread_mutex_lock(&ipcpi.alloc_lock); + pthread_mutex_lock(&ipcpd.alloc_lock); - while (ipcpi.alloc_id != fd && ipcp_get_state() == IPCP_OPERATIONAL) { + while (ipcpd.alloc_id != fd && ipcp_get_state() == IPCP_OPERATIONAL) { ts_add(&abstime, &ts, &abstime); - pthread_cond_timedwait(&ipcpi.alloc_cond, - &ipcpi.alloc_lock, + pthread_cond_timedwait(&ipcpd.alloc_cond, + &ipcpd.alloc_lock, &abstime); } if (ipcp_get_state() != IPCP_OPERATIONAL) { - pthread_mutex_unlock(&ipcpi.alloc_lock); + pthread_mutex_unlock(&ipcpd.alloc_lock); return -1; } - assert(ipcpi.alloc_id == fd); + assert(ipcpd.alloc_id == fd); - ipcpi.alloc_id = -1; - pthread_cond_broadcast(&ipcpi.alloc_cond); + ipcpd.alloc_id = -1; + pthread_cond_broadcast(&ipcpd.alloc_cond); - pthread_mutex_unlock(&ipcpi.alloc_lock); + pthread_mutex_unlock(&ipcpd.alloc_lock); return 0; } @@ -353,30 +445,58 @@ static void free_msg(void * o) static void do_bootstrap(ipcp_config_msg_t * conf_msg, ipcp_msg_t * ret_msg) { - struct ipcp_config conf; + struct ipcp_config conf; + struct layer_info * info; log_info("Bootstrapping..."); - if (ipcpi.ops->ipcp_bootstrap == NULL) { - log_err("Bootstrap unsupported."); + if (ipcpd.ops->ipcp_bootstrap == NULL) { + log_err("Failed to Bootstrap: operation unsupported."); ret_msg->result = -ENOTSUP; - goto finish; + return; } - if (ipcp_get_state() != IPCP_INIT) { - log_err("IPCP in wrong state."); + if (ipcp_get_state() != IPCP_BOOT) { + + log_err("Failed to bootstrap: IPCP in state <%s>, need <%s>.", + ipcp_state_str[ipcp_get_state()], + ipcp_state_str[IPCP_BOOT]); ret_msg->result = -EIPCPSTATE; - goto finish; + return; } conf = ipcp_config_msg_to_s(conf_msg); - ret_msg->result = ipcpi.ops->ipcp_bootstrap(&conf); - if (ret_msg->result == 0) { - ret_msg->layer_info = layer_info_s_to_msg(&conf.layer_info); - ipcp_set_state(IPCP_OPERATIONAL); + switch(conf.type) { /* FIXED algorithms */ + case IPCP_UDP4: + /* FALLTHRU */ + case IPCP_UDP6: + conf.layer_info.dir_hash_algo = (enum pol_dir_hash) HASH_MD5; + break; + case IPCP_BROADCAST: + conf.layer_info.dir_hash_algo = DIR_HASH_SHA3_256; + break; + default: + break; } - finish: - log_info("Finished bootstrapping: %d.", ret_msg->result); + + ret_msg->result = ipcpd.ops->ipcp_bootstrap(&conf); + if (ret_msg->result < 0) { + log_err("Failed to bootstrap IPCP."); + return; + } + + info = &conf.layer_info; + + strcpy(ipcpd.layer_name, info->name); + ipcpd.dir_hash_algo = (enum hash_algo) info->dir_hash_algo; + ret_msg->layer_info = layer_info_s_to_msg(info); + ipcp_set_state(IPCP_OPERATIONAL); + + log_info("Finished bootstrapping in %s.", info->name); + log_info(" type: %s", ipcp_type_str[ipcpd.type]); + log_info(" hash: %s [%zd bytes]", + dir_hash_str[ipcpd.dir_hash_algo], + ipcp_dir_hash_len()); } static void do_enroll(const char * dst, @@ -386,25 +506,36 @@ static void do_enroll(const char * dst, log_info("Enrolling with %s...", dst); - if (ipcpi.ops->ipcp_enroll == NULL) { - log_err("Enroll unsupported."); + if (ipcpd.ops->ipcp_enroll == NULL) { + log_err("Failed to enroll: operation unsupported."); ret_msg->result = -ENOTSUP; - goto finish; + return; } - if (ipcp_get_state() != IPCP_INIT) { - log_err("IPCP in wrong state."); + if (ipcp_get_state() != IPCP_BOOT) { + log_err("Failed to enroll: IPCP in state <%s>, need <%s>.", + ipcp_state_str[ipcp_get_state()], + ipcp_state_str[IPCP_BOOT]); ret_msg->result = -EIPCPSTATE; - goto finish; + return; } - ret_msg->result = ipcpi.ops->ipcp_enroll(dst, &info); - if (ret_msg->result == 0) { - ret_msg->layer_info = layer_info_s_to_msg(&info); - ipcp_set_state(IPCP_OPERATIONAL); + ret_msg->result = ipcpd.ops->ipcp_enroll(dst, &info); + if (ret_msg->result < 0) { + log_err("Failed to bootstrap IPCP."); + return; } - finish: - log_info("Finished enrolling with %s: %d.", dst, ret_msg->result); + + strcpy(ipcpd.layer_name, info.name); + ipcpd.dir_hash_algo = (enum hash_algo) info.dir_hash_algo; + ret_msg->layer_info = layer_info_s_to_msg(&info); + ipcp_set_state(IPCP_OPERATIONAL); + + log_info("Finished enrolling with %s in layer %s.", dst, info.name); + log_info(" type: %s", ipcp_type_str[ipcpd.type]); + log_info(" hash: %s [%zd bytes]", + dir_hash_str[ipcpd.dir_hash_algo], + ipcp_dir_hash_len()); } static void do_connect(const char * dst, @@ -414,15 +545,15 @@ static void do_connect(const char * dst, { log_info("Connecting %s to %s...", comp, dst); - if (ipcpi.ops->ipcp_connect == NULL) { - log_err("Connect unsupported."); + if (ipcpd.ops->ipcp_connect == NULL) { + log_err("Failed to connect: operation unsupported."); ret_msg->result = -ENOTSUP; - goto finish; + return; } - ret_msg->result = ipcpi.ops->ipcp_connect(dst, comp, qs); - finish: - log_info("Finished connecting: %d.", ret_msg->result); + ret_msg->result = ipcpd.ops->ipcp_connect(dst, comp, qs); + + log_info("Finished connecting."); } static void do_disconnect(const char * dst, @@ -431,17 +562,15 @@ static void do_disconnect(const char * dst, { log_info("Disconnecting %s from %s...", comp, dst); - if (ipcpi.ops->ipcp_disconnect == NULL) { - log_err("Disconnect unsupported."); + if (ipcpd.ops->ipcp_disconnect == NULL) { + log_err("Failed to disconnect: operation unsupported."); ret_msg->result = -ENOTSUP; - goto finish; + return; } - ret_msg->result = ipcpi.ops->ipcp_disconnect(dst, comp); + ret_msg->result = ipcpd.ops->ipcp_disconnect(dst, comp); - finish: - log_info("Finished disconnecting %s from %s: %d.", - comp, dst, ret_msg->result); + log_info("Finished disconnecting %s from %s.", comp, dst); } static void do_reg(const uint8_t * hash, @@ -450,16 +579,15 @@ static void do_reg(const uint8_t * hash, log_info("Registering " HASH_FMT32 "...", HASH_VAL32(hash)); - if (ipcpi.ops->ipcp_reg == NULL) { - log_err("Registration unsupported."); + if (ipcpd.ops->ipcp_reg == NULL) { + log_err("Failed to register: operation unsupported."); ret_msg->result = -ENOTSUP; - goto finish; + return; } - ret_msg->result = ipcpi.ops->ipcp_reg(hash); - finish: - log_info("Finished registering " HASH_FMT32 " : %d.", - HASH_VAL32(hash), ret_msg->result); + ret_msg->result = ipcpd.ops->ipcp_reg(hash); + + log_info("Finished registering " HASH_FMT32 ".", HASH_VAL32(hash)); } static void do_unreg(const uint8_t * hash, @@ -467,16 +595,15 @@ static void do_unreg(const uint8_t * hash, { log_info("Unregistering " HASH_FMT32 "...", HASH_VAL32(hash)); - if (ipcpi.ops->ipcp_unreg == NULL) { - log_err("Unregistration unsupported."); + if (ipcpd.ops->ipcp_unreg == NULL) { + log_err("Failed to unregister: operation unsupported."); ret_msg->result = -ENOTSUP; - goto finish; + return; } - ret_msg->result = ipcpi.ops->ipcp_unreg(hash); - finish: - log_info("Finished unregistering " HASH_FMT32 ": %d.", - HASH_VAL32(hash), ret_msg->result); + ret_msg->result = ipcpd.ops->ipcp_unreg(hash); + + log_info("Finished unregistering " HASH_FMT32 ".", HASH_VAL32(hash)); } static void do_query(const uint8_t * hash, @@ -484,19 +611,21 @@ static void do_query(const uint8_t * hash, { /* TODO: Log this operation when IRMd has internal caches. */ - if (ipcpi.ops->ipcp_query == NULL) { - log_err("Directory query unsupported."); + if (ipcpd.ops->ipcp_query == NULL) { + log_err("Failed to query: operation unsupported."); ret_msg->result = -ENOTSUP; return; } if (ipcp_get_state() != IPCP_OPERATIONAL) { - log_err("IPCP in wrong state."); + log_dbg("Failed to query: IPCP in state <%s>, need <%s>.", + ipcp_state_str[ipcp_get_state()], + ipcp_state_str[IPCP_OPERATIONAL]); ret_msg->result = -EIPCPSTATE; return; } - ret_msg->result = ipcpi.ops->ipcp_query(hash); + ret_msg->result = ipcpd.ops->ipcp_query(hash); } static void do_flow_alloc(pid_t pid, @@ -511,16 +640,18 @@ static void do_flow_alloc(pid_t pid, log_info("Allocating flow %d for %d to " HASH_FMT32 ".", flow_id, pid, HASH_VAL32(dst)); - if (ipcpi.ops->ipcp_flow_alloc == NULL) { - log_err("Flow allocation unsupported."); + if (ipcpd.ops->ipcp_flow_alloc == NULL) { + log_err("Flow allocation failed: operation unsupported."); ret_msg->result = -ENOTSUP; - goto finish; + return; } if (ipcp_get_state() != IPCP_OPERATIONAL) { - log_err("IPCP in wrong state."); + log_err("Failed to enroll: IPCP in state <%s>, need <%s>.", + ipcp_state_str[ipcp_get_state()], + ipcp_state_str[IPCP_OPERATIONAL]); ret_msg->result = -EIPCPSTATE; - goto finish; + return; } fd = np1_flow_alloc(pid, flow_id); @@ -528,47 +659,48 @@ static void do_flow_alloc(pid_t pid, log_err("Failed allocating n + 1 fd on flow_id %d: %d", flow_id, fd); ret_msg->result = -EFLOWDOWN; - goto finish; + return; } - ret_msg->result = ipcpi.ops->ipcp_flow_alloc(fd, dst, qs, data); - finish: - log_info("Finished allocating flow %d to " HASH_FMT32 ": %d.", - flow_id, HASH_VAL32(dst), ret_msg->result); + ret_msg->result = ipcpd.ops->ipcp_flow_alloc(fd, dst, qs, data); + + log_info("Finished allocating flow %d to " HASH_FMT32 ".", + flow_id, HASH_VAL32(dst)); } static void do_flow_join(pid_t pid, int flow_id, const uint8_t * dst, - qosspec_t qs, ipcp_msg_t * ret_msg) { int fd; log_info("Joining layer " HASH_FMT32 ".", HASH_VAL32(dst)); - if (ipcpi.ops->ipcp_flow_join == NULL) { - log_err("Broadcast unsupported."); + if (ipcpd.ops->ipcp_flow_join == NULL) { + log_err("Failed to join: operation unsupported."); ret_msg->result = -ENOTSUP; - goto finish; + return; } if (ipcp_get_state() != IPCP_OPERATIONAL) { - log_err("IPCP in wrong state."); + log_err("Failed to join: IPCP in state <%s>, need <%s>.", + ipcp_state_str[ipcp_get_state()], + ipcp_state_str[IPCP_OPERATIONAL]); ret_msg->result = -EIPCPSTATE; - goto finish; + return; } fd = np1_flow_alloc(pid, flow_id); if (fd < 0) { log_err("Failed allocating n + 1 fd on flow_id %d.", flow_id); ret_msg->result = -1; - goto finish; + return; } - ret_msg->result = ipcpi.ops->ipcp_flow_join(fd, dst, qs); - finish: + ret_msg->result = ipcpd.ops->ipcp_flow_join(fd, dst); + log_info("Finished joining layer " HASH_FMT32 ".", HASH_VAL32(dst)); } @@ -581,30 +713,33 @@ static void do_flow_alloc_resp(int resp, log_info("Responding %d to alloc on flow_id %d.", resp, flow_id); - if (ipcpi.ops->ipcp_flow_alloc_resp == NULL) { - log_err("Flow_alloc_resp unsupported."); + if (ipcpd.ops->ipcp_flow_alloc_resp == NULL) { + log_err("Failed to respond on flow %d: operation unsupported.", + flow_id); ret_msg->result = -ENOTSUP; - goto finish; + return; } if (ipcp_get_state() != IPCP_OPERATIONAL) { - log_err("IPCP in wrong state."); + log_err("Failed to respond to flow %d:" + "IPCP in state <%s>, need <%s>.", + flow_id, + ipcp_state_str[ipcp_get_state()], + ipcp_state_str[IPCP_OPERATIONAL]); ret_msg->result = -EIPCPSTATE; - goto finish; + return; } - if (resp == 0) { - fd = np1_flow_resp(flow_id); - if (fd < 0) { - log_warn("Flow_id %d is not known.", flow_id); - ret_msg->result = -1; - goto finish; - } + fd = np1_flow_resp(flow_id, resp); + if (fd < 0) { + log_warn("Flow_id %d is not known.", flow_id); + ret_msg->result = -1; + return; } - ret_msg->result = ipcpi.ops->ipcp_flow_alloc_resp(fd, resp, data); - finish: - log_info("Finished responding to allocation request: %d", + ret_msg->result = ipcpd.ops->ipcp_flow_alloc_resp(fd, resp, data); + + log_info("Finished responding %d to allocation request.", ret_msg->result); } @@ -616,55 +751,56 @@ static void do_flow_dealloc(int flow_id, log_info("Deallocating flow %d.", flow_id); - if (ipcpi.ops->ipcp_flow_dealloc == NULL) { - log_err("Flow deallocation unsupported."); + if (ipcpd.ops->ipcp_flow_dealloc == NULL) { + log_err("Failed to dealloc: operation unsupported."); ret_msg->result = -ENOTSUP; - goto finish; + return; } if (ipcp_get_state() != IPCP_OPERATIONAL) { - log_err("IPCP in wrong state."); + log_err("Failed to enroll: IPCP in state <%s>, need <%s>.", + ipcp_state_str[ipcp_get_state()], + ipcp_state_str[IPCP_OPERATIONAL]); ret_msg->result = -EIPCPSTATE; - goto finish; + return; } fd = np1_flow_dealloc(flow_id, timeo_sec); if (fd < 0) { log_warn("Could not deallocate flow_id %d.", flow_id); ret_msg->result = -1; - goto finish; + return; } - ret_msg->result = ipcpi.ops->ipcp_flow_dealloc(fd); - finish: - log_info("Finished deallocating flow %d: %d.", - flow_id, ret_msg->result); + ret_msg->result = ipcpd.ops->ipcp_flow_dealloc(fd); + + log_info("Finished deallocating flow %d.", flow_id); } static void * mainloop(void * o) { - int sfd; - buffer_t buffer; - ipcp_msg_t * msg; + int sfd; + buffer_t buffer; + ipcp_msg_t * msg; (void) o; while (true) { - ipcp_msg_t ret_msg = IPCP_MSG__INIT; - qosspec_t qs; - struct cmd * cmd; - buffer_t data; + ipcp_msg_t ret_msg = IPCP_MSG__INIT; + qosspec_t qs; + struct cmd * cmd; + buffer_t data; ret_msg.code = IPCP_MSG_CODE__IPCP_REPLY; - pthread_mutex_lock(&ipcpi.cmd_lock); + pthread_mutex_lock(&ipcpd.cmd_lock); - pthread_cleanup_push(__cleanup_mutex_unlock, &ipcpi.cmd_lock); + pthread_cleanup_push(__cleanup_mutex_unlock, &ipcpd.cmd_lock); - while (list_is_empty(&ipcpi.cmds)) - pthread_cond_wait(&ipcpi.cmd_cond, &ipcpi.cmd_lock); + while (list_is_empty(&ipcpd.cmds)) + pthread_cond_wait(&ipcpd.cmd_cond, &ipcpd.cmd_lock); - cmd = list_last_entry(&ipcpi.cmds, struct cmd, next); + cmd = list_last_entry(&ipcpd.cmds, struct cmd, next); list_del(&cmd->next); pthread_cleanup_pop(true); @@ -679,7 +815,7 @@ static void * mainloop(void * o) continue; } - tpm_dec(ipcpi.tpm); + tpm_begin_work(ipcpd.tpm); pthread_cleanup_push(__cleanup_close_ptr, &sfd); pthread_cleanup_push(free_msg, msg); @@ -725,9 +861,8 @@ static void * mainloop(void * o) break; case IPCP_MSG_CODE__IPCP_FLOW_JOIN: assert(msg->hash.len == ipcp_dir_hash_len()); - qs = qos_spec_msg_to_s(msg->qosspec); do_flow_join(msg->pid, msg->flow_id, - msg->hash.data, qs, &ret_msg); + msg->hash.data, &ret_msg); break; case IPCP_MSG_CODE__IPCP_FLOW_ALLOC_RESP: assert(msg->pk.len > 0 ? msg->pk.data != NULL @@ -753,7 +888,7 @@ static void * mainloop(void * o) if (buffer.len == 0) { log_err("Failed to pack reply message"); close(sfd); - tpm_inc(ipcpi.tpm); + tpm_end_work(ipcpd.tpm); continue; } @@ -761,7 +896,7 @@ static void * mainloop(void * o) if (buffer.data == NULL) { log_err("Failed to create reply buffer."); close(sfd); - tpm_inc(ipcpi.tpm); + tpm_end_work(ipcpd.tpm); continue; } @@ -770,16 +905,16 @@ static void * mainloop(void * o) if (ret_msg.layer_info != NULL) layer_info_msg__free_unpacked(ret_msg.layer_info, NULL); - pthread_cleanup_push(__cleanup_close_ptr, &sfd); pthread_cleanup_push(free, buffer.data) + pthread_cleanup_push(__cleanup_close_ptr, &sfd); if (write(sfd, buffer.data, buffer.len) == -1) log_warn("Failed to send reply message"); - pthread_cleanup_pop(true); - pthread_cleanup_pop(true); + pthread_cleanup_pop(true); /* close sfd */ + pthread_cleanup_pop(true); /* free buffer.data */ - tpm_inc(ipcpi.tpm); + tpm_end_work(ipcpd.tpm); } return (void *) 0; @@ -798,10 +933,10 @@ static int parse_args(int argc, if (atoi(argv[1]) == 0) return -1; - ipcpi.irmd_pid = atoi(argv[1]); + ipcpd.irmd_pid = atoi(argv[1]); /* argument 2: IPCP name */ - ipcpi.name = argv[2]; + ipcpd.name = argv[2]; /* argument 3: syslog */ if (argv[3] != NULL) @@ -817,71 +952,69 @@ int ipcp_init(int argc, { bool log; pthread_condattr_t cattr; - int ret = -1; if (parse_args(argc, argv, &log)) return -1; log_init(log); - ipcpi.irmd_fd = -1; - ipcpi.state = IPCP_NULL; - ipcpi.type = type; + ipcpd.type = type; #if defined (__linux__) prctl(PR_SET_TIMERSLACK, IPCP_LINUX_SLACK_NS, 0, 0, 0); #endif - ipcpi.sock_path = ipcp_sock_path(getpid()); - if (ipcpi.sock_path == NULL) + ipcpd.sock_path = sock_path(getpid(), IPCP_SOCK_PATH_PREFIX); + if (ipcpd.sock_path == NULL) goto fail_sock_path; - ipcpi.sockfd = server_socket_open(ipcpi.sock_path); - if (ipcpi.sockfd < 0) { - log_err("Could not open server socket."); + ipcpd.sockfd = server_socket_open(ipcpd.sock_path); + if (ipcpd.sockfd < 0) { + log_err("Failed to open server socket at %s.", + ipcpd.sock_path); goto fail_serv_sock; } - ipcpi.ops = ops; + ipcpd.ops = ops; - if (pthread_mutex_init(&ipcpi.state_mtx, NULL)) { - log_err("Could not create mutex."); + if (pthread_mutex_init(&ipcpd.state_mtx, NULL)) { + log_err("Failed to create mutex."); goto fail_state_mtx; } if (pthread_condattr_init(&cattr)) { - log_err("Could not create condattr."); + log_err("Failed to create condattr."); goto fail_cond_attr; } #ifndef __APPLE__ pthread_condattr_setclock(&cattr, PTHREAD_COND_CLOCK); #endif - if (pthread_cond_init(&ipcpi.state_cond, &cattr)) { - log_err("Could not init condvar."); + if (pthread_cond_init(&ipcpd.state_cond, &cattr)) { + log_err("Failed to init condvar."); goto fail_state_cond; } - if (pthread_mutex_init(&ipcpi.alloc_lock, NULL)) { + if (pthread_mutex_init(&ipcpd.alloc_lock, NULL)) { log_err("Failed to init mutex."); goto fail_alloc_lock; } - if (pthread_cond_init(&ipcpi.alloc_cond, &cattr)) { + if (pthread_cond_init(&ipcpd.alloc_cond, &cattr)) { log_err("Failed to init convar."); goto fail_alloc_cond; } - if (pthread_mutex_init(&ipcpi.cmd_lock, NULL)) { + if (pthread_mutex_init(&ipcpd.cmd_lock, NULL)) { log_err("Failed to init mutex."); goto fail_cmd_lock; } - if (pthread_cond_init(&ipcpi.cmd_cond, &cattr)) { + if (pthread_cond_init(&ipcpd.cmd_cond, &cattr)) { log_err("Failed to init convar."); goto fail_cmd_cond; } - if (rib_init(ipcpi.name)) { + if (rib_init(ipcpd.name)) { log_err("Failed to initialize RIB."); goto fail_rib_init; } @@ -891,21 +1024,24 @@ int ipcp_init(int argc, goto fail_rib_reg; } - ipcpi.tpm = tpm_create(IPCP_MIN_THREADS, IPCP_ADD_THREADS, + list_head_init(&ipcpd.cmds); + + ipcpd.tpm = tpm_create(IPCP_MIN_THREADS, IPCP_ADD_THREADS, mainloop, NULL); - if (ipcpi.tpm == NULL) { + if (ipcpd.tpm == NULL) { log_err("Failed to create threadpool manager."); goto fail_tpm_create; } - list_head_init(&ipcpi.cmds); - - ipcpi.alloc_id = -1; + ipcpd.alloc_id = -1; pthread_condattr_destroy(&cattr); ipcp_set_state(IPCP_INIT); + log_info("IPCP %s %d initialized.", ipcp_type_str[ipcpd.type], + getpid()); + return 0; fail_tpm_create: @@ -913,25 +1049,25 @@ int ipcp_init(int argc, fail_rib_reg: rib_fini(); fail_rib_init: - pthread_cond_destroy(&ipcpi.cmd_cond); + pthread_cond_destroy(&ipcpd.cmd_cond); fail_cmd_cond: - pthread_mutex_destroy(&ipcpi.cmd_lock); + pthread_mutex_destroy(&ipcpd.cmd_lock); fail_cmd_lock: - pthread_cond_destroy(&ipcpi.alloc_cond); + pthread_cond_destroy(&ipcpd.alloc_cond); fail_alloc_cond: - pthread_mutex_destroy(&ipcpi.alloc_lock); + pthread_mutex_destroy(&ipcpd.alloc_lock); fail_alloc_lock: - pthread_cond_destroy(&ipcpi.state_cond); + pthread_cond_destroy(&ipcpd.state_cond); fail_state_cond: pthread_condattr_destroy(&cattr); fail_cond_attr: - pthread_mutex_destroy(&ipcpi.state_mtx); + pthread_mutex_destroy(&ipcpd.state_mtx); fail_state_mtx: - close(ipcpi.sockfd); + close(ipcpd.sockfd); fail_serv_sock: - free(ipcpi.sock_path); + free(ipcpd.sock_path); fail_sock_path: - return ret; + return -1; } int ipcp_start(void) @@ -948,20 +1084,22 @@ int ipcp_start(void) pthread_sigmask(SIG_BLOCK, &sigset, NULL); info.pid = getpid(); - info.type = ipcpi.type; - strcpy(info.name, ipcpi.name); - info.state = IPCP_OPERATIONAL; + info.type = ipcpd.type; + strcpy(info.name, ipcpd.name); + info.state = IPCP_BOOT; + + ipcp_set_state(IPCP_BOOT); - if (tpm_start(ipcpi.tpm)) + if (tpm_start(ipcpd.tpm)) { + log_err("Failed to start threadpool manager."); goto fail_tpm_start; + } - if (pthread_create(&ipcpi.acceptor, NULL, acceptloop, NULL)) { + if (pthread_create(&ipcpd.acceptor, NULL, acceptloop, NULL)) { log_err("Failed to create acceptor thread."); goto fail_acceptor; } - info.state = IPCP_OPERATIONAL; - if (ipcp_create_r(&info)) { log_err("Failed to notify IRMd we are initialized."); goto fail_create_r; @@ -970,14 +1108,13 @@ int ipcp_start(void) return 0; fail_create_r: - pthread_cancel(ipcpi.acceptor); - pthread_join(ipcpi.acceptor, NULL); + pthread_cancel(ipcpd.acceptor); + pthread_join(ipcpd.acceptor, NULL); fail_acceptor: - tpm_stop(ipcpi.tpm); + tpm_stop(ipcpd.tpm); fail_tpm_start: - tpm_destroy(ipcpi.tpm); - ipcp_set_state(IPCP_NULL); - info.state = IPCP_NULL; + tpm_destroy(ipcpd.tpm); + ipcp_set_state(IPCP_INIT); ipcp_create_r(&info); return -1; } @@ -997,7 +1134,7 @@ void ipcp_sigwait(void) sigaddset(&sigset, SIGTERM); sigaddset(&sigset, SIGPIPE); - while(ipcp_get_state() != IPCP_NULL && + while(ipcp_get_state() != IPCP_INIT && ipcp_get_state() != IPCP_SHUTDOWN) { #ifdef __APPLE__ if (sigwait(&sigset, &sig) < 0) { @@ -1011,7 +1148,7 @@ void ipcp_sigwait(void) #ifdef __APPLE__ memset(&info, 0, sizeof(info)); info.si_signo = sig; - info.si_pid = ipcpi.irmd_pid; + info.si_pid = ipcpd.irmd_pid; #endif switch(info.si_signo) { case SIGINT: @@ -1021,9 +1158,9 @@ void ipcp_sigwait(void) case SIGHUP: /* FALLTHRU */ case SIGQUIT: - if (info.si_pid == ipcpi.irmd_pid) { - if (ipcp_get_state() == IPCP_INIT) - ipcp_set_state(IPCP_NULL); + if (info.si_pid == ipcpd.irmd_pid) { + if (ipcp_get_state() == IPCP_BOOT) + ipcp_set_state(IPCP_INIT); if (ipcp_get_state() == IPCP_OPERATIONAL) ipcp_set_state(IPCP_SHUTDOWN); @@ -1042,58 +1179,62 @@ void ipcp_stop(void) { log_info("IPCP %d shutting down.", getpid()); - pthread_cancel(ipcpi.acceptor); - pthread_join(ipcpi.acceptor, NULL); + pthread_cancel(ipcpd.acceptor); + pthread_join(ipcpd.acceptor, NULL); - tpm_stop(ipcpi.tpm); + tpm_stop(ipcpd.tpm); + + ipcp_set_state(IPCP_INIT); } void ipcp_fini(void) { - tpm_destroy(ipcpi.tpm); + tpm_destroy(ipcpd.tpm); rib_unreg(IPCP_INFO); rib_fini(); - close(ipcpi.sockfd); - if (unlink(ipcpi.sock_path)) - log_warn("Could not unlink %s.", ipcpi.sock_path); + close(ipcpd.sockfd); + if (unlink(ipcpd.sock_path)) + log_warn("Could not unlink %s.", ipcpd.sock_path); - free(ipcpi.sock_path); + free(ipcpd.sock_path); - pthread_cond_destroy(&ipcpi.state_cond); - pthread_mutex_destroy(&ipcpi.state_mtx); - pthread_cond_destroy(&ipcpi.alloc_cond); - pthread_mutex_destroy(&ipcpi.alloc_lock); - pthread_cond_destroy(&ipcpi.cmd_cond); - pthread_mutex_destroy(&ipcpi.cmd_lock); + pthread_cond_destroy(&ipcpd.state_cond); + pthread_mutex_destroy(&ipcpd.state_mtx); + pthread_cond_destroy(&ipcpd.alloc_cond); + pthread_mutex_destroy(&ipcpd.alloc_lock); + pthread_cond_destroy(&ipcpd.cmd_cond); + pthread_mutex_destroy(&ipcpd.cmd_lock); log_info("IPCP %d out.", getpid()); log_fini(); + + ipcpd.state = IPCP_NULL; } void ipcp_set_state(enum ipcp_state state) { - pthread_mutex_lock(&ipcpi.state_mtx); + pthread_mutex_lock(&ipcpd.state_mtx); - ipcpi.state = state; + ipcpd.state = state; - pthread_cond_broadcast(&ipcpi.state_cond); - pthread_mutex_unlock(&ipcpi.state_mtx); + pthread_cond_broadcast(&ipcpd.state_cond); + pthread_mutex_unlock(&ipcpd.state_mtx); } enum ipcp_state ipcp_get_state(void) { enum ipcp_state state; - pthread_mutex_lock(&ipcpi.state_mtx); + pthread_mutex_lock(&ipcpd.state_mtx); - state = ipcpi.state; + state = ipcpd.state; - pthread_mutex_unlock(&ipcpi.state_mtx); + pthread_mutex_unlock(&ipcpd.state_mtx); return state; } |
