diff options
author | charan makkina <charan795m@gmail.com> | 2019-04-30 17:40:53 +0530 |
---|---|---|
committer | charan makkina <charan795m@gmail.com> | 2019-05-20 18:14:40 +0530 |
commit | a826fe833d3f2a8fe2673fa05811fe1a22baf045 (patch) | |
tree | da11a17c46ca9b8a002a52a290628574fa3f5eda /src/nSocket/nstack/nstack_module.c | |
parent | 3e6bf7b64eea418c59959c18750261b815b2892c (diff) |
Feature: 19.04 part 1
Change-Id: Ibba924b8deca1f246b9dcb12d89d085b6fd33046
Signed-off-by: charan makkina <charan795m@gmail.com>
Diffstat (limited to 'src/nSocket/nstack/nstack_module.c')
-rw-r--r-- | src/nSocket/nstack/nstack_module.c | 423 |
1 files changed, 258 insertions, 165 deletions
diff --git a/src/nSocket/nstack/nstack_module.c b/src/nSocket/nstack/nstack_module.c index 9566ab8..fdfea06 100644 --- a/src/nSocket/nstack/nstack_module.c +++ b/src/nSocket/nstack/nstack_module.c @@ -29,13 +29,13 @@ #include <arpa/inet.h> #include <errno.h> #include "nstack_module.h" -#include "common_mem_buf.h" #include "nstack_log.h" +#include "nstack_select.h" #include "nstack_securec.h" -#include "nstack_dmm_api.h" -#include "nstack_dmm_adpt.h" -#include "nstack_rd_init.h" +#include "nstack_rd.h" +#include "nstack_epoll_api.h" #include "nstack_info_parse.h" +#include "nsfw_mem_api.h" /* *INDENT-OFF* */ nstack_module_info g_nstack_modules = { @@ -45,243 +45,336 @@ nstack_module_info g_nstack_modules = { .defMod = NULL, }; -/* *INDENT-ON* */ nstack_module_keys g_nstack_module_desc[NSTACK_MAX_MODULE_NUM]; - ns_uint32 g_module_num = 0; +nstack_proc_ops nstack_fd_deal[NSTACK_MAX_MODULE_NUM]; +/* *INDENT-ON* */ + +extern rd_route_table *g_rd_table_handle[NSTACK_MAX_MODULE_NUM]; -int -nstack_get_deploy_type () +int nstack_get_deploy_type() { - int icnt = 0; - int type = 0; - for (icnt = 0; icnt < g_module_num; icnt++) + int icnt = 0; + int type = 0; + for (icnt = 0; icnt < g_module_num; icnt++) { - if (g_nstack_module_desc[icnt].deploytype > type) + if (g_nstack_module_desc[icnt].deploytype > type) { - type = g_nstack_module_desc[icnt].deploytype; + type = g_nstack_module_desc[icnt].deploytype; } } - return type; + return type; } NSTACK_STATIC inline int -nstack_register_one_module (nstack_module_keys * pKeys) +nstack_register_one_module_forchild(nstack_module_keys * pKeys) { + nstack_module *pmod = NULL; + nstack_stack_register_fn stack_register_fn = NULL; + nstack_event_ops val = { 0 }; + int ret = 0; - nstack_module *pmod = NULL; - nstack_stack_register_fn stack_register_fn = NULL; - nstack_event_cb val = { 0 }; - int retVal; - int ret = 0; - - if (pKeys->modInx >= NSTACK_MAX_MODULE_NUM) + if (pKeys->modInx >= NSTACK_MAX_MODULE_NUM) { - NSSOC_LOGERR ("modeindex is overload]index=%d", pKeys->modInx); - ret = -1; - goto err_return; + NSSOC_LOGERR + ("pKeys->modInx is too large]pKeys->modName=%s,pKeys->modInx=%d", + pKeys->modName, pKeys->modInx); + return -1; } - pmod = nstack_get_module (pKeys->modInx); - - retVal = - STRNCPY_S (pmod->modulename, NSTACK_PLUGIN_NAME_LEN, pKeys->modName, - NSTACK_PLUGIN_NAME_LEN); - if (EOK != retVal) + pmod = nstack_get_module(pKeys->modInx); + /*There are some unsafe function ,need to be replace with safe function */ + ret = + strncpy_s(pmod->modulename, NSTACK_MODULE_NAME_MAX, pKeys->modName, + NSTACK_MODULE_NAME_MAX - 1); + if (EOK != ret) { - NSSOC_LOGERR ("STRNCPY_S failed]ret=%d", retVal); - ret = -1; - goto err_return; + NSSOC_LOGERR("strncpy_s failed]ret=%d", ret); + return -1; } + pmod->modulename[NSTACK_MODULE_NAME_MAX - 1] = '\0'; + pmod->priority = pKeys->priority; + pmod->modInx = pKeys->modInx; + pmod->maxfdid = pKeys->maxfdid; + pmod->minfdid = pKeys->minfdid; - pmod->modulename[NSTACK_PLUGIN_NAME_LEN - 1] = '\0'; - pmod->priority = pKeys->priority; - pmod->modInx = pKeys->modInx; - pmod->maxfdid = pKeys->maxfdid; - pmod->minfdid = pKeys->minfdid; - pmod->ep_free_ref = pKeys->ep_free_ref; - - if (pKeys->libtype == NSTACK_LIB_LOAD_DYN) + if (pKeys->libtype == NSTACK_LIB_LOAD_DYN) { - pmod->handle = dlopen (pKeys->libPath, RTLD_LAZY); - if (!pmod->handle) + pmod->handle = dlopen(pKeys->libPath, RTLD_LAZY); + if (!pmod->handle) { - /*optimize dlopen err print */ - NSSOC_LOGERR ("dlopen lib=%s of module=%s failed, err_string=%s", - pKeys->libPath, pKeys->modName, dlerror ()); - ret = -1; - goto err_return; + NSSOC_LOGERR + ("dlopen failed, lib=%s of module=%s, error string=%s", + pKeys->libPath, pKeys->modName, dlerror()); + return -1; } } - else + else { - pmod->handle = RTLD_DEFAULT; + pmod->handle = RTLD_DEFAULT; } - stack_register_fn = dlsym (pmod->handle, pKeys->register_fn_name); - if (!stack_register_fn) + stack_register_fn = dlsym(pmod->handle, pKeys->register_fn_name); + if (!stack_register_fn) { - /* optimize dlopen err print */ - NSSOC_LOGERR ("register function not found]err_string=%s", dlerror ()); - if (pmod->handle) + /* optimize dlopen err print Begin */ + NSSOC_LOGERR("register function not found]err_string=%s", dlerror()); + /* optimize dlopen err print End */ + if (pmod->handle && RTLD_NEXT != pmod->handle) { - dlclose (pmod->handle); - pmod->handle = NULL; + dlclose(pmod->handle); + pmod->handle = NULL; } - ret = -1; - goto err_return; + return -1; } - val.handle = pmod->handle; - val.type = pKeys->modInx; - val.event_cb = nstack_event_callback; - if (stack_register_fn (&pmod->mops, &val)) + val.handle = pmod->handle; + val.type = pKeys->modInx; + val.event_cb = nstack_epoll_event_enqueue; + if (stack_register_fn(&pmod->ops, &val, &nstack_fd_deal[pmod->modInx])) { - NSSOC_LOGERR ("register function failed"); - if (pmod->handle) + NSSOC_LOGERR("register function failed"); + if (pmod->handle && RTLD_NEXT != pmod->handle) { - dlclose (pmod->handle); - pmod->handle = NULL; + dlclose(pmod->handle); + pmod->handle = NULL; } - ret = -1; - goto err_return; + return -1; } - /* malloc length need protect - malloc parameter type is size_t */ - if (((pmod->maxfdid + 1) < 1) - || (SIZE_MAX / sizeof (ns_int32) < (pmod->maxfdid + 1))) + return 0; +} + +void nstack_register_module_forchild(void) +{ + ns_uint32 idx; + for (idx = 0; idx < g_module_num; idx++) { - NSSOC_LOGERR ("malloc size is wrong]maxfdid=%d", pmod->maxfdid); - if (pmod->handle) + if (0 != + nstack_register_one_module_forchild(&g_nstack_module_desc[idx])) { - dlclose (pmod->handle); - pmod->handle = NULL; + NSSOC_LOGERR + ("can't register module]modInx=%d,modName=%s,libPath=%s", + g_nstack_module_desc[idx].modInx, + g_nstack_module_desc[idx].modName, + g_nstack_module_desc[idx].libPath); + return; } - ret = -1; - goto err_return; } - -err_return: - return ret; } -/*nstack_register_module can't concurrent*/ -int -nstack_register_module () +int nstack_register_one_module(nstack_module_keys * pKeys) { - unsigned int idx = 0; - nstack_stack_info *pstacks = NULL; - int ret = 0; - - nstack_get_route_data rd_fun[] = { - nstack_stack_rd_parse, - }; + nstack_module *pmod = NULL; + nstack_stack_register_fn stack_register_fn = NULL; + nstack_event_ops val = { 0 }; + int ret = 0; - pstacks = - (nstack_stack_info *) malloc (sizeof (nstack_stack_info) * g_module_num); - if (!pstacks) + if (pKeys->modInx >= NSTACK_MAX_MODULE_NUM) { - NSSOC_LOGERR ("malloc failed]"); - return ns_fail; + NSSOC_LOGERR("modeindex overflows]index=%d", pKeys->modInx); + ret = -1; + goto err_return; } - ret = - MEMSET_S (pstacks, sizeof (nstack_stack_info) * g_module_num, 0, - sizeof (nstack_stack_info) * g_module_num); - if (EOK != ret) + + pmod = nstack_get_module(pKeys->modInx); + + /*There are some unsafe function ,need to be replace with safe function */ + ret = + strncpy_s(pmod->modulename, NSTACK_MODULE_NAME_MAX, pKeys->modName, + NSTACK_MODULE_NAME_MAX - 1); + if (EOK != ret) { - NSSOC_LOGERR ("MEMSET_S failed]ret=%d", ret); - free (pstacks); - return ns_fail; + NSSOC_LOGERR("strncpy_s failed]ret=%d", ret); + ret = -1; + goto err_return; } - for (idx = 0; idx < g_module_num; idx++) + pmod->modulename[NSTACK_MODULE_NAME_MAX - 1] = '\0'; + pmod->priority = pKeys->priority; + pmod->modInx = pKeys->modInx; + pmod->maxfdid = pKeys->maxfdid; + pmod->minfdid = pKeys->minfdid; + + if (pKeys->libtype == NSTACK_LIB_LOAD_DYN) { - if (0 != nstack_register_one_module (&g_nstack_module_desc[idx])) + pmod->handle = dlopen(pKeys->libPath, RTLD_LAZY); + if (!pmod->handle) { - NSSOC_LOGERR - ("can't register module]modInx=%d,modName=%s,libPath=%s", - g_nstack_module_desc[idx].modInx, - g_nstack_module_desc[idx].modName, - g_nstack_module_desc[idx].libPath); - free (pstacks); - return ns_fail; + NSSOC_LOGERR + ("dlopen failed, lib=%s of module=%s, error string=%s", + pKeys->libPath, pKeys->modName, dlerror()); + ret = -1; + goto err_return; } - ret = - STRCPY_S (pstacks[idx].name, STACK_NAME_MAX, - g_nstack_module_desc[idx].modName); - if (EOK != ret) + } + else + { + pmod->handle = RTLD_DEFAULT; + } + + stack_register_fn = dlsym(pmod->handle, pKeys->register_fn_name); + if (!stack_register_fn) + { + /* optimize dlopen err print Begin */ + NSSOC_LOGERR("register function not found]err_string=%s", dlerror()); + /* optimize dlopen err print End */ + if (pmod->handle) { - NSSOC_LOGERR ("STRCPY_S fail]idx=%d,modName=%s,ret=%d", idx, - g_nstack_module_desc[idx].modName, ret); - free (pstacks); - return ns_fail; + dlclose(pmod->handle); + pmod->handle = NULL; } - pstacks[idx].priority = g_nstack_module_desc[idx].priority; - pstacks[idx].stack_id = g_nstack_module_desc[idx].modInx; - if (g_nstack_module_desc[idx].default_stack == 1) + ret = -1; + goto err_return; + } + val.handle = pmod->handle; + val.type = pKeys->modInx; + val.event_cb = nstack_epoll_event_enqueue; + if (stack_register_fn(&pmod->ops, &val, &nstack_fd_deal[pmod->modInx])) + { + NSSOC_LOGERR("register function failed, module=%s", pKeys->modName); + if (pmod->handle) { - g_nstack_modules.defMod = - &g_nstack_modules.modules[g_nstack_module_desc[idx].modInx]; - g_nstack_modules.fix_mid = g_nstack_module_desc[idx].modInx; + dlclose(pmod->handle); + pmod->handle = NULL; } + ret = -1; + goto err_return; } - if (g_nstack_modules.fix_mid < 0) + /* malloc length need protect + malloc parameter type is size_t */ + + if (((pmod->maxfdid + 1) < 1) + || (SIZE_MAX / sizeof(ns_int32) < (pmod->maxfdid + 1))) { - free (pstacks); - NSSOC_LOGERR ("nstack fix mid is unknown and return fail"); - return ns_fail; + NSSOC_LOGERR("malloc size is wrong]maxfdid=%d", pmod->maxfdid); + if (pmod->handle) + { + dlclose(pmod->handle); + pmod->handle = NULL; + } + ret = -1; + goto err_return; } - g_nstack_modules.modNum = g_module_num; - /*rd module init */ - if (ns_success != - nstack_rd_init (pstacks, idx, rd_fun, - sizeof (rd_fun) / sizeof (nstack_get_route_data))) + if (nstack_fd_deal[pmod->modInx].module_init_pre) { - free (pstacks); - NSSOC_LOGERR ("nstack rd init fail"); - return ns_fail; + ret = nstack_fd_deal[pmod->modInx].module_init_pre((void *) + g_nsfw_mem_ops, + (void *) + g_ring_ops_arry, + NSFW_MEM_TYPEMAX, + NSFW_MPOOL_TYPEMAX); } - free (pstacks); - return ns_success; + + err_return: + return ret; } -int -nstack_stack_module_init () +/*nstack_register_module can't concurrent*/ +int nstack_register_module() { - ns_uint32 idx; - for (idx = 0; idx < g_module_num; idx++) + unsigned int idx = 0; + nstack_rd_stack_info *pstacks = NULL; + int ret = 0; + + pstacks = + (nstack_rd_stack_info *) malloc(sizeof(nstack_rd_stack_info) * + g_module_num); + if (!pstacks) { - if (g_nstack_modules.modules[idx].mops.extern_ops.module_init) + NSSOC_LOGERR("malloc failed]"); + return ns_fail; + } + /*There are some unsafe function ,need to be replace with safe function */ + ret = + memset_s(&nstack_fd_deal[0], sizeof(nstack_fd_deal), 0, + sizeof(nstack_fd_deal)); + ret |= + memset_s(pstacks, sizeof(nstack_rd_stack_info) * g_module_num, 0, + sizeof(nstack_rd_stack_info) * g_module_num); + if (EOK != ret) + { + NSSOC_LOGERR("memset_s failed]ret=%d", ret); + free(pstacks); /*free() can be used */ + return ns_fail; + } + + for (idx = 0; idx < g_module_num; idx++) + { + if (0 != nstack_register_one_module(&g_nstack_module_desc[idx])) { - if (0 != - g_nstack_modules.modules[idx].mops.extern_ops.module_init ()) - { - NSSOC_LOGERR ("nstack[%s] modx:%d init fail", - g_nstack_modules.modules[idx].modulename, idx); - } + NSSOC_LOGERR + ("can't register module]modInx=%d,modName=%s,libPath=%s", + g_nstack_module_desc[idx].modInx, + g_nstack_module_desc[idx].modName, + g_nstack_module_desc[idx].libPath); + free(pstacks); /*free() can be used */ + return ns_fail; } + ret = + strcpy_s(pstacks[idx].name, STACK_NAME_MAX, + g_nstack_module_desc[idx].modName); + if (EOK != ret) + { + NSSOC_LOGERR("strcpy_s fail]idx=%u,modName=%s,ret=%d", idx, + g_nstack_module_desc[idx].modName, ret); + free(pstacks); /*free() can be used */ + return ns_fail; + } + + pstacks[idx].priority = g_nstack_module_desc[idx].priority; + pstacks[idx].stack_id = g_nstack_module_desc[idx].modInx; + if (g_nstack_module_desc[idx].default_stack == 1) + { + g_nstack_modules.defMod = + &g_nstack_modules.modules[g_nstack_module_desc[idx].modInx]; + g_nstack_modules.fix_mid = g_nstack_module_desc[idx].modInx; + } + if (strcmp(g_nstack_module_desc[idx].modName, RD_KERNEL_NAME) == 0) + { + g_nstack_modules.linuxmid = idx; + } + } + + if (g_nstack_modules.fix_mid < 0) + { + free(pstacks); + NSSOC_LOGERR("nstack fix mid still unknown!"); + return ns_fail; + } + g_nstack_modules.modNum = g_module_num; + + /*rd module init */ + if (ns_success != nstack_rd_init(pstacks, idx)) + { + free(pstacks); /*free() can be used */ + NSSOC_LOGERR("nstack rd init fail"); + return ns_fail; } - return 0; + free(pstacks); /*free() can be used */ + return ns_success; } -int -nstack_stack_module_init_child () +int nstack_stack_module_init() { - ns_uint32 idx; - for (idx = 0; idx < g_module_num; idx++) + int icnt; + for (icnt = 0; icnt < g_module_num; icnt++) { - if (g_nstack_modules.modules[idx].mops.extern_ops.module_init_child) + if (nstack_fd_deal[icnt].module_init) { - if (0 != - g_nstack_modules.modules[idx].mops. - extern_ops.module_init_child ()) + if (nstack_fd_deal[icnt].module_init()) { - NSSOC_LOGERR ("nstack[%s] modx:%d init child fail", - g_nstack_modules.modules[idx].modulename, idx); + NSSOC_LOGERR("stack:%s init failed!", + g_nstack_modules.modules[icnt].modulename); + return -1; } } + if (nstack_fd_deal[icnt].get_ip_shmem) + { + g_rd_table_handle[icnt] = + (rd_route_table *) nstack_fd_deal[icnt].get_ip_shmem(); + } } - return 0; + return 0; } |