From c398e6ec613c1f90ffe33608c511208100e7372f Mon Sep 17 00:00:00 2001 From: Yalei Wang Date: Mon, 12 Mar 2018 10:07:58 +0800 Subject: Add the nSocket module for dmm Change-Id: I8b97fbe50c9c1c479072510b8d799fe711360da7 Signed-off-by: Yalei Wang --- src/nSocket/include/declare_syscalls.h | 51 +++++ src/nSocket/include/nstack_atomic.h | 57 ++++++ src/nSocket/include/nstack_dmm_api.h | 92 +++++++++ src/nSocket/include/nstack_eventpoll.h | 338 +++++++++++++++++++++++++++++++++ src/nSocket/include/nstack_ip_addr.h | 36 ++++ src/nSocket/include/nstack_rd.h | 47 +++++ src/nSocket/include/nstack_rd_init.h | 45 +++++ src/nSocket/include/nstack_select.h | 153 +++++++++++++++ src/nSocket/include/nstack_sockops.h | 55 ++++++ src/nSocket/include/nstack_types.h | 152 +++++++++++++++ src/nSocket/include/select_adapt.h | 105 ++++++++++ 11 files changed, 1131 insertions(+) create mode 100644 src/nSocket/include/declare_syscalls.h create mode 100644 src/nSocket/include/nstack_atomic.h create mode 100644 src/nSocket/include/nstack_dmm_api.h create mode 100644 src/nSocket/include/nstack_eventpoll.h create mode 100644 src/nSocket/include/nstack_ip_addr.h create mode 100644 src/nSocket/include/nstack_rd.h create mode 100644 src/nSocket/include/nstack_rd_init.h create mode 100644 src/nSocket/include/nstack_select.h create mode 100644 src/nSocket/include/nstack_sockops.h create mode 100644 src/nSocket/include/nstack_types.h create mode 100644 src/nSocket/include/select_adapt.h (limited to 'src/nSocket/include') diff --git a/src/nSocket/include/declare_syscalls.h b/src/nSocket/include/declare_syscalls.h new file mode 100644 index 0000000..c4810f5 --- /dev/null +++ b/src/nSocket/include/declare_syscalls.h @@ -0,0 +1,51 @@ +/* +* +* Copyright (c) 2018 Huawei Technologies Co.,Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at: +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +NSTACK_MK_DECL (int, socket, (int, int, int)); +NSTACK_MK_DECL (int, bind, (int, const struct sockaddr *, socklen_t)); +NSTACK_MK_DECL (int, listen, (int, int)); +NSTACK_MK_DECL (int, shutdown, (int, int)); +NSTACK_MK_DECL (int, getsockname, (int, struct sockaddr *, socklen_t *)); +NSTACK_MK_DECL (int, getpeername, (int, struct sockaddr *, socklen_t *)); +NSTACK_MK_DECL (int, getsockopt, (int, int, int, void *, socklen_t *)); +NSTACK_MK_DECL (int, setsockopt, (int, int, int, const void *, socklen_t)); +NSTACK_MK_DECL (int, accept, (int, struct sockaddr *, socklen_t *)); +NSTACK_MK_DECL (int, accept4, + (int, struct sockaddr *, socklen_t *, int flags)); +NSTACK_MK_DECL (int, connect, (int, const struct sockaddr *, socklen_t)); +NSTACK_MK_DECL (ssize_t, recv, (int, void *, size_t, int)); +NSTACK_MK_DECL (ssize_t, send, (int, const void *, size_t, int)); +NSTACK_MK_DECL (ssize_t, read, (int, void *, size_t)); +NSTACK_MK_DECL (ssize_t, write, (int, const void *, size_t)); +NSTACK_MK_DECL (ssize_t, writev, (int, const struct iovec *, int)); +NSTACK_MK_DECL (ssize_t, readv, (int, const struct iovec *, int)); +NSTACK_MK_DECL (ssize_t, sendto, + (int, const void *, size_t, int, const struct sockaddr *, + socklen_t)); +NSTACK_MK_DECL (ssize_t, recvfrom, + (int, void *, size_t, int, struct sockaddr *, socklen_t *)); +NSTACK_MK_DECL (ssize_t, sendmsg, (int, const struct msghdr *, int flags)); +NSTACK_MK_DECL (ssize_t, recvmsg, (int, struct msghdr *, int flags)); +NSTACK_MK_DECL (int, close, (int)); +NSTACK_MK_DECL (int, select, + (int, fd_set *, fd_set *, fd_set *, struct timeval *)); +NSTACK_MK_DECL (int, ioctl, (int, unsigned long, unsigned long)); +NSTACK_MK_DECL (int, fcntl, (int, int, unsigned long)); +NSTACK_MK_DECL (int, epoll_create, (int)); +NSTACK_MK_DECL (int, epoll_ctl, (int, int, int, struct epoll_event *)); +NSTACK_MK_DECL (int, epoll_wait, (int, struct epoll_event *, int, int)); +NSTACK_MK_DECL (pid_t, fork, (void)); +#undef NSTACK_MK_DECL diff --git a/src/nSocket/include/nstack_atomic.h b/src/nSocket/include/nstack_atomic.h new file mode 100644 index 0000000..37ab451 --- /dev/null +++ b/src/nSocket/include/nstack_atomic.h @@ -0,0 +1,57 @@ +/* +* +* Copyright (c) 2018 Huawei Technologies Co.,Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at: +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#ifndef _NSTACK_ATOMIC_H_ +#define _NSTACK_ATOMIC_H_ +#include +#include + +#include "nstack_types.h" + +#ifdef __cplusplus +/* *INDENT-OFF* */ +extern "C"{ +/* *INDENT-ON* */ +#endif + +typedef struct +{ + volatile ns_int32 counter; +} atomic_t; + +#define atomic_set(v, i) ((v)->counter = (i)) + +#define atomic_fetch_and_add(v, i) __sync_fetch_and_add(&(v)->counter, i) +#define atomic_add_and_fetch(v, i) __sync_add_and_fetch(&(v)->counter, i) + +#define atomic_fetch_and_sub(v, i) __sync_fetch_and_sub(&(v)->counter, i) +#define atomic_sub_and_fetch(v, i) __sync_sub_and_fetch(&(v)->counter, i) + +#define atomic_read(v) atomic_fetch_and_add(v, 0) +#define atomic_inc(v) atomic_add_and_fetch(v, 1) +#define atomic_dec(v)atomic_sub_and_fetch(v, 1) +#define atomic_add(v, i) atomic_add_and_fetch(v, i) +#define atomic_sub(v, i) atomic_sub_and_fetch(v,i) + +#define cas(ptr, oldValue, exchage) __sync_bool_compare_and_swap(ptr, oldValue, exchage) + +#ifdef __cplusplus +/* *INDENT-OFF* */ +} +/* *INDENT-ON* */ +#endif + +#endif diff --git a/src/nSocket/include/nstack_dmm_api.h b/src/nSocket/include/nstack_dmm_api.h new file mode 100644 index 0000000..4caeb5e --- /dev/null +++ b/src/nSocket/include/nstack_dmm_api.h @@ -0,0 +1,92 @@ +/* +* +* Copyright (c) 2018 Huawei Technologies Co.,Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at: +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#ifndef __NSOCKET_DMM_API_H__ +#define __NSOCKET_DMM_API_H__ + +#include +#include +#include +#include +#include +#include +#include +#include "nstack_rd_data.h" + +/* + *Standard api interface definition. + *these interface is provided by Protocol stack to nStack + */ +typedef struct __nstack_socket_ops +{ +#undef NSTACK_MK_DECL +#define NSTACK_MK_DECL(ret, fn, args) ret (*pf##fn) args +#include "declare_syscalls.h" +} nstack_socket_ops; + +typedef enum +{ + STACK_FD_INVALID_CHECK, /*check wether fd is created by this stack */ + STACK_FD_FUNCALL_CHECK, /*check this stack surpport default call */ +} nstack_fd_check; + +typedef enum +{ + nstack_ep_triggle_add, + nstack_ep_triggle_mod, + nstack_ep_triggle_del +} nstack_ep_triggle_ops_t; + +/* + *Interactive interface for Protocol stack and nStack defined here + *these interface is provided by Protocol stack to nStack + */ +typedef struct __nstack_extern_ops +{ + int (*module_init) (void); /*stack module init */ + int (*fork_init_child) (pid_t p, pid_t c); /*after fork, stack child process init again if needed. */ + void (*fork_parent_fd) (int s, pid_t p); /*after fork, stack parent process proc again if needed. */ + void (*fork_child_fd) (int s, pid_t p, pid_t c); /*after fork, child record pid for recycle if needed. */ + void (*fork_free_fd) (int s, pid_t p, pid_t c); /*for SOCK_CLOEXEC when fork if needed. */ + unsigned int (*ep_ctl) (int epFD, int proFD, int ctl_ops, struct epoll_event * event, void *pdata); /*when fd add to epoll fd, triggle stack to proc if need */ + unsigned int (*ep_getevt) (int epFD, int profd, unsigned int events); /*check whether some events exist really */ + int (*ep_prewait_proc) (int epfd); + int (*stack_fd_check) (int s, int flag); /*check whether fd belong to stack, if belong, return 1, else return 0 */ + int (*stack_alloc_fd) (); /*alloc a fd id for epoll */ + int (*peak) (int s); /*used for stack-x , isource maybe no need */ +} nstack_extern_ops; + +/* + *The event notify interface provided to the protocol stack by nStack + *these interface is provided by nStack to Protocol stack + */ +typedef struct __nstack_event_cb +{ + void *handle; /*current so file handler */ + int type; /*nstack is assigned to the protocol stack and needs to be passed to nstack when the event is reported */ + int (*event_cb) (void *pdata, int events); +} nstack_event_cb; + +typedef struct __nstack_proc_cb +{ + nstack_socket_ops socket_ops; /*posix socket api */ + nstack_extern_ops extern_ops; /*other proc callback */ +} nstack_proc_cb; + +typedef int (*nstack_stack_registe_fn) (nstack_proc_cb * proc_fun, + nstack_event_cb * event_ops); + +#endif diff --git a/src/nSocket/include/nstack_eventpoll.h b/src/nSocket/include/nstack_eventpoll.h new file mode 100644 index 0000000..1abc055 --- /dev/null +++ b/src/nSocket/include/nstack_eventpoll.h @@ -0,0 +1,338 @@ +/* +* +* Copyright (c) 2018 Huawei Technologies Co.,Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at: +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#ifndef _NSTACK_EVENTPOOL_H +#define _NSTACK_EVENTPOOL_H + +#include "ephlist.h" +#include "eprb_tree.h" +#include "common_mem_api.h" +#include "types.h" +#include +#include +#include +#include +#include "nstack_securec.h" +#include "nstack_log.h" + +#ifdef __cplusplus +/* *INDENT-OFF* */ +extern "C"{ +/* *INDENT-ON* */ +#endif /* __cplusplus */ + +#define NSTACK_MAX_EPOLL_INFO_NUM 8192 +#define NSTACK_MAX_EPOLL_NUM NSTACK_MAX_EPOLL_INFO_NUM +#define NSTACK_MAX_EPITEM_NUM (NSTACK_MAX_EPOLL_NUM*2) + +#define MP_NSTACK_EPOLL_INFO_NAME "nsep_info" +#define MP_NSTACK_EVENTPOLL_POOL "nsep_eventpoll" /* Pool of struct eventpoll */ +#define MP_NSTACK_EPITEM_POOL "nsep_epitem" /* Pool of struct epitem */ + +#define MP_NSTACK_EPINFO_RING_NAME "nsep_info_ring" +#define MP_NSTACK_EPITEM_RING_NAME "nsep_item_ring" +#define MP_NSTACK_EVENTPOOL_RING_NAME "nsep_event_ring" + +#define NSTACK_FORK_NUM 32 +#define NSTACK_EPOL_FD 1 + +#define NSEP_SMOD_MAX 8 + +#define NSEP_EP_UNACTIVE_PTR ((void *) -1L) + +COMPAT_PROTECT (NSEP_SMOD_MAX, 8); + +typedef struct +{ + u32 pid_used_size; + u32 pid_array[NSTACK_FORK_NUM]; +} nsep_pidinfo; + +struct eventpoll +{ + + /* + * Protect the this structure access + * This is for event add to ready list and poll out + */ + sys_sem_st lock; + + /* + * This semaphore is used to ensure that files are not removed + * while epoll is using them. This is read-held during the event + * processing loop and it is write-held during the file cleanup + * path, the epoll file exit code and the ctl operations. + * When we do epoll_ctl, we write lock + * When we collecting data , we read lock + */ + sys_sem_st sem; + + /* + * This sempaphore is used to block epoll_wait function + */ + sem_t waitSem; + + /* List of ready file descriptors */ + struct ep_hlist rdlist; + + /* When poll data out, we need this list to store tmp epitems */ + struct epitem *ovflist; + + /* RB-Tree root used to store mastered fd structs */ + struct ep_rb_root rbr; + + /* This specifies the file descriptor value of epoll instance, currenlty it is just used for debugging */ + int epfd; + u32 pid; + nsfw_res res_chk; +}; + +struct eventpoll_pool +{ + void *ring; + struct eventpoll *pool; +}; + +typedef struct +{ + int iindex; + int iNext; + int fd; + i32 epaddflag; + i32 fdtype; /*0: socket fd, 1: epoll fd */ + i32 rlfd; /* copy of fdInf->rlfd */ + i32 rmidx; /* copy of fdInf->rmidx */ + i32 protoFD[NSEP_SMOD_MAX]; /* copy of fdInf->protoFD */// Here we need to set router infomations dependency + struct eventpoll *ep; + sys_sem_st epiLock; + sys_sem_st freeLock; + struct ep_list epiList; /* This restore the epitem of this file descriptor */ + u32 sleepTime; //add for NSTACK_SEM_SLEEP + nsep_pidinfo pidinfo; + nsfw_res res_chk; + void *private_data; /*add for degbu, just used to record extern infomation, for example sbr conn */ + i32 reserv[4]; +} nsep_epollInfo_t; + +typedef struct +{ + void *ring; + nsep_epollInfo_t *pool; + char last_reserve[8]; //reserve for update +} nsep_infoPool_t; + +struct epitem +{ + struct ep_rb_node rbn; + struct ep_hlist_node rdllink; + struct ep_hlist_node lkFDllink; + int nwait; + struct eventpoll *ep; + nsep_epollInfo_t *epInfo; + struct epitem *next; + struct epoll_event event; + struct list_node fllink; + unsigned int revents; + unsigned int ovf_revents; + int fd; + u32 pid; + void *private_data; + nsfw_res res_chk; +}; + +struct epitem_pool +{ + void *ring; + struct epitem *pool; +}; + +typedef struct +{ + struct eventpoll_pool epollPool; + struct epitem_pool epitemPool; + nsep_infoPool_t infoPool; + nsep_epollInfo_t **infoSockMap; // The map of epInfo and socket + +} nsep_epollManager_t; +extern nsep_epollManager_t g_epollMng; +#define nsep_getManager() (&g_epollMng) + +extern int nsep_alloc_eventpoll (struct eventpoll **data); +extern int nsep_free_eventpoll (struct eventpoll *ep); +extern int nsep_alloc_epitem (struct epitem **data); +extern int nsep_free_epitem (struct epitem *data); +extern int nsep_alloc_epinfo (nsep_epollInfo_t ** data); +extern int nsep_free_epinfo (nsep_epollInfo_t * info); + +extern int nsep_epitem_remove (nsep_epollInfo_t * pinfo, u32 pid); + +extern struct epitem *nsep_find_ep (struct eventpoll *ep, int fd); +extern int nsep_epctl_add (struct eventpoll *ep, int fd, + struct epoll_event *events); +extern int nsep_epctl_del (struct eventpoll *ep, struct epitem *epi); +extern int nsep_epctl_mod (struct eventpoll *ep, + nsep_epollInfo_t * epInfo, + struct epitem *epi, struct epoll_event *events); +extern int nsep_ep_poll (struct eventpoll *ep, struct epoll_event *events, + int maxevents, int *eventflag, int num); +extern void nsep_remove_epfd (nsep_epollInfo_t * info); +extern void nsep_close_epfd (struct eventpoll *ep); + +/** + * @Function nsep_init_infoSockMap + * @Description initial map of epoll info and socket + * @param none + * @return 0 on success, -1 on error + */ +extern int nsep_init_infoSockMap (); +extern void nsep_set_infoSockMap (int sock, nsep_epollInfo_t * info); +extern nsep_epollInfo_t *nsep_get_infoBySock (int sock); +extern int nsep_alloc_infoWithSock (int nfd); +extern void nsep_set_infoProtoFD (int fd, int modInx, int protoFD); +extern int nsep_get_infoProtoFD (int fd, int modInx); +extern void nsep_set_infomdix (int fd, int rmidx); +extern int nsep_get_infoMidx (int fd); +extern void nsep_set_infoRlfd (int fd, int rlfd); +extern int nsep_get_infoRlfd (int fd); +extern void nsep_set_infoSleepTime (int fd, u32 sleepTime); +extern int nsep_get_infoSleepTime (int fd); +extern void nsep_set_infoEp (int fd, struct eventpoll *ep); +extern struct eventpoll *nsep_get_infoEp (int fd); +extern int nsep_free_infoWithSock (int sock); +extern int nstack_ep_unlink (struct eventpoll *ep, struct epitem *epi); + +/* Attach shared memory */ +extern int nsep_create_memory (); +extern int nsep_attach_memory (); +extern int nsep_ep_fdinfo_release (int sock); +extern int nsep_epoll_close (int sock); +extern void nsep_fork_child_proc (u32_t ppid); +extern void nsep_fork_parent_proc (u32_t ppid, u32_t cpid); + +static inline i32 +nsep_for_pidinfo_init (nsep_pidinfo * pidinfo) +{ + int ret; + ret = MEMSET_S (pidinfo, sizeof (*pidinfo), 0, sizeof (*pidinfo)); + if (EOK != ret) + { + NSSOC_LOGERR ("MEMSET_S failed]ret=%d", ret); + return -1; + } + return 0; +} + +static inline int +nsep_add_pid (nsep_pidinfo * pidinfo, pid_t pid) +{ + int i; + + for (i = 0; i < NSTACK_FORK_NUM; i++) + { + if ((0 == pidinfo->pid_array[i]) + && (__sync_bool_compare_and_swap (&pidinfo->pid_array[i], 0, pid))) + { + if (pidinfo->pid_used_size < i + 1) + { + pidinfo->pid_used_size = i + 1; + } + return 0; + } + } + return -1; +} + +static inline int +nsep_del_pid (nsep_pidinfo * pidinfo, pid_t pid) +{ + int i; + + for (i = 0; i < pidinfo->pid_used_size && i < NSTACK_FORK_NUM; i++) + { + if (pid == pidinfo->pid_array[i]) + { + pidinfo->pid_array[i] = 0; + return 0; + } + } + return -1; +} + +static inline int +nsep_del_last_pid (nsep_pidinfo * pidinfo, pid_t pid) +{ + int i; + int count = 0; + int deleted = 0; + for (i = 0; i < pidinfo->pid_used_size && i < NSTACK_FORK_NUM; i++) + { + if (pid == pidinfo->pid_array[i]) + { + pidinfo->pid_array[i] = 0; + deleted = 1; + continue; + } + + if (pidinfo->pid_array[i] != 0) + { + ++count; + } + } + + if (!deleted) + { + return -1; + } + + return count; +} + +static inline int +nsep_is_pid_exist (nsep_pidinfo * epinf, pid_t pid) +{ + int i; + + for (i = 0; i < epinf->pid_used_size && i < NSTACK_FORK_NUM; i++) + { + if (pid == epinf->pid_array[i]) + { + return 1; + } + } + return 0; +} + +static inline int +nsep_is_pid_array_empty (nsep_pidinfo * epinf) +{ + int i; + for (i = 0; i < epinf->pid_used_size && i < NSTACK_FORK_NUM; i++) + { + if (epinf->pid_array[i] != 0) + { + return 0; + } + } + return 1; +} + +#ifdef __cplusplus +/* *INDENT-OFF* */ +} +/* *INDENT-ON* */ +#endif /* __cplusplus */ + +#endif /* _NSTACK_EVENTPOLL_H */ diff --git a/src/nSocket/include/nstack_ip_addr.h b/src/nSocket/include/nstack_ip_addr.h new file mode 100644 index 0000000..8b15854 --- /dev/null +++ b/src/nSocket/include/nstack_ip_addr.h @@ -0,0 +1,36 @@ +/* +* +* Copyright (c) 2018 Huawei Technologies Co.,Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at: +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#ifndef __NSTACK_IP_ADDR_H__ +#define __NSTACK_IP_ADDR_H__ + +#ifndef ip4_addr1_16 +#include "nstack_types.h" + +/* Get one byte from the 4-byte address */ +#define ip4_addr1(ipaddr) (((u8_t*)(ipaddr))[0]) /* ip4_addr1(ipaddr) */ +#define ip4_addr2(ipaddr) (((u8_t*)(ipaddr))[1]) /* ip4_addr2(ipaddr) */ +#define ip4_addr3(ipaddr) (((u8_t*)(ipaddr))[2]) /* ip4_addr3(ipaddr) */ +#define ip4_addr4(ipaddr) (((u8_t*)(ipaddr))[3]) /* ip4_addr4(ipaddr) */ + +/* These are cast to u16_t, with the intent that they are often arguments + * to printf using the U16_F format from cc.h. */ +#define ip4_addr1_16(ipaddr) ((u16_t)ip4_addr1(ipaddr)) /*ip4_addr1_16(ipaddr) */ +#define ip4_addr2_16(ipaddr) ((u16_t)ip4_addr2(ipaddr)) /*ip4_addr2_16(ipaddr) */ +#define ip4_addr3_16(ipaddr) ((u16_t)ip4_addr3(ipaddr)) /*ip4_addr3_16(ipaddr) */ +#define ip4_addr4_16(ipaddr) ((u16_t)ip4_addr4(ipaddr)) /*ip4_addr4_16(ipaddr) */ +#endif +#endif diff --git a/src/nSocket/include/nstack_rd.h b/src/nSocket/include/nstack_rd.h new file mode 100644 index 0000000..a93f6a6 --- /dev/null +++ b/src/nSocket/include/nstack_rd.h @@ -0,0 +1,47 @@ +/* +* +* Copyright (c) 2018 Huawei Technologies Co.,Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at: +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#ifndef __NSTACK_RD_H +#define __NSTACK_RD_H + +/*look up chose info by key*/ +typedef struct __nstack_rd_key +{ + int type; + union + { + unsigned int ip_addr; + unsigned int proto_type; + }; +} nstack_rd_key; + +#define NSTACK_RD_MAX (1 * 1024 * 1024) + +/* + *rd synchronism + * + */ +int nstack_rd_sys (); +int nstack_rd_age (); + +/* + *get stackid by some info + *if input is ip, the value must be net order + * + */ +int nstack_rd_get_stackid (nstack_rd_key * addr, int *stackid); + +#endif diff --git a/src/nSocket/include/nstack_rd_init.h b/src/nSocket/include/nstack_rd_init.h new file mode 100644 index 0000000..cf5073e --- /dev/null +++ b/src/nSocket/include/nstack_rd_init.h @@ -0,0 +1,45 @@ +/* +* +* Copyright (c) 2018 Huawei Technologies Co.,Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at: +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#ifndef _NSTACK_RD_INIT_H_ +#define _NSTACK_RD_INIT_H_ +#include "nstack_rd_data.h" + +#define NSTACK_RD_CHECK_BELONG (1) +#define NSTACK_RD_CHECK_NOT_BELONG (0) + +typedef struct __nstack_stack_info +{ + /*stack name */ + char name[STACK_NAME_MAX]; + /*stack id */ + int stack_id; + /*when route info not found, high priority stack was chose, same priority chose fist input one */ + int priority; /*0: highest: route info not found choose first */ +} nstack_stack_info; + +/*get rd info. if return ok, data callee alloc memory, caller free, else caller don't free*/ +typedef int (*nstack_get_route_data) (rd_route_data ** data, int *num); + +/* + *rd init + *defualtid: if all module check fail, just return defualt id + *return : 0 success, -1 fail + */ +int nstack_rd_init (nstack_stack_info * pstack, int num, + nstack_get_route_data * pfun, int fun_num); + +#endif diff --git a/src/nSocket/include/nstack_select.h b/src/nSocket/include/nstack_select.h new file mode 100644 index 0000000..bb23105 --- /dev/null +++ b/src/nSocket/include/nstack_select.h @@ -0,0 +1,153 @@ +/* +* +* Copyright (c) 2018 Huawei Technologies Co.,Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at: +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/*==============================================* + * include header files * + *----------------------------------------------*/ + +/*==============================================* + * constants or macros define * + *----------------------------------------------*/ + +/*==============================================* + * project-wide global variables * + *----------------------------------------------*/ + +/*==============================================* + * routines' or functions' implementations * + *----------------------------------------------*/ + +#define NSTACK_SELECT_MODULE + +#ifdef NSTACK_SELECT_MODULE + +#ifndef __NSTACK_SELECT_H__ +#define __NSTACK_SELECT_H__ + +#include +#include +#include +#include +#include "select_adapt.h" + +#ifdef __cplusplus +/* *INDENT-OFF* */ +extern "C" { +/* *INDENT-ON* */ +#endif + +typedef int (*get_select_event) (int nfds, fd_set * readfd, + fd_set * writefd, fd_set * exceptfd, + struct timeval * timeout); +typedef struct +{ + unsigned char fds_bits[(NSTACK_SETSIZE + 7) >> 3]; +} __attribute__ ((packed)) nstack_fd_set; + +#define NSTACK_FD_SET(n, p) ((p)->fds_bits[(n)>>3]|=1U<<((n)&0x07)) +#define NSTACK_FD_ISSET(n,p) (((p)->fds_bits[(n)>>3]&(1U<<((n)&0x07)))?1:0) +#define NSTACK_FD_CLR(n,p) ((p)->fds_bits[(n)>>3]&=~(1U<<((n)&0x07))) +#define NSTACK_FD_ZERO(p) (MEMSET_S((void *)(p), sizeof(*(p)),0,sizeof(*(p)))) +#define NSTACK_FD_OR(p1 ,p2) {\ + int i;\ + for(i = 0; i < (NSTACK_SETSIZE+7)>>3; i++){\ + (p1)->fds_bits[i] |= (p2)->fds_bits[i];\ + }\ +} + +struct select_cb_p +{ + union + { + nstack_fd_set nstack_readset; + fd_set readset; + }; + union + { + nstack_fd_set nstack_writeset; + fd_set writeset; + }; + union + { + nstack_fd_set nstack_exceptset; + fd_set exceptset; + }; + + union + { + i32 count; + i32 readyset; + }; + + i32 inx; + i32 select_errno; +}; + +struct select_entry_info +{ + i32 set_num; //how many select_c_p is set + i32 index; //the frist cb was set +}; + +struct select_entry +{ + struct select_cb_p cb[NSTACK_MAX_MODULE_NUM]; + struct select_cb_p ready; + struct select_entry *next; + struct select_entry *prev; + struct select_entry_info info; + select_sem_t sem; +}; + +struct select_module_info +{ + + struct select_entry *entry_head; + struct select_entry *entry_tail; + get_select_event get_select_fun_nonblock[NSTACK_MAX_MODULE_NUM]; + get_select_event get_select_fun_block[NSTACK_MAX_MODULE_NUM]; + get_select_event default_fun; + i32 default_mod; + volatile i32 inited; + select_spinlock_t lock; + select_sem_t sem; +}; + +extern i32 select_cb_split_by_mod (i32 nfds, + fd_set * readfd, + fd_set * writefd, + fd_set * exceptfd, + struct select_entry *entry); +extern void entry_module_fdset (struct select_entry *entry, + i32 fd_size, + nstack_fd_set * readfd, + nstack_fd_set * writefd, + nstack_fd_set * exceptfd, i32 inx); +extern i32 select_scan (struct select_entry *entry); +extern i32 select_add_cb (struct select_entry *entry); +extern i32 select_rm_cb (struct select_entry *entry); +extern i32 select_entry_reset (struct select_entry *entry); +extern i32 select_module_init (); +extern struct select_module_info *get_select_module (void); +#ifdef __cplusplus +/* *INDENT-OFF* */ +} +/* *INDENT-ON* */ +#endif + +#endif /* __NSTACK_SELECT_H__ */ + +#endif /* NSTACK_SELECT_MODULE */ diff --git a/src/nSocket/include/nstack_sockops.h b/src/nSocket/include/nstack_sockops.h new file mode 100644 index 0000000..cc430d5 --- /dev/null +++ b/src/nSocket/include/nstack_sockops.h @@ -0,0 +1,55 @@ +/* +* +* Copyright (c) 2018 Huawei Technologies Co.,Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at: +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#ifndef _NSTACK_SOCKOPS_H_ +#define _NSTACK_SOCKOPS_H_ + +#include "nstack_log.h" + +#ifdef __cplusplus +/* *INDENT-OFF* */ +extern "C"{ +/* *INDENT-ON* */ +#endif + +#define NSTACK_CAL_FUN(ops, fun, args, retval) \ +{\ + if(NULL != (ops) && NULL != (ops)->pf##fun)\ + {\ + if((retval = ((ops)->pf##fun args)) == -1)\ + {\ + NSSOC_LOGDBG("function=%s excute failed,ret=%d.errno=%d.", #fun, retval, errno); \ + }\ + }\ + else\ + {\ + NSSOC_LOGERR("NULL address erro, ops_pointer=%p,function=%s", (ops), #fun); \ + errno = EBADF; \ + }\ +} + +#define NSTACK_SET_OPS_FUN(ops, fun, destFunc) \ + if(NULL != (ops)){ \ + (ops)->pf##fun = (destFunc);\ + } + +#ifdef __cplusplus +/* *INDENT-OFF* */ +} +/* *INDENT-ON* */ +#endif + +#endif diff --git a/src/nSocket/include/nstack_types.h b/src/nSocket/include/nstack_types.h new file mode 100644 index 0000000..90b538e --- /dev/null +++ b/src/nSocket/include/nstack_types.h @@ -0,0 +1,152 @@ +/* +* +* Copyright (c) 2018 Huawei Technologies Co.,Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at: +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#ifndef _NSTACK_TYPES_H_ +#define _NSTACK_TYPES_H_ +#include +#include +#include +#include +#include + +#ifdef __cplusplus +/* *INDENT-OFF* */ +extern "C"{ +/* *INDENT-ON* */ +#endif + +#ifndef NSTACK_MAX_U32_NUM +#define NSTACK_MAX_U32_NUM ((u32_t)0xffffffff) +#endif + +#ifndef NSTACK_MAX_S32_NUM +#define NSTACK_MAX_S32_NUM ((s32_t)0x7fffffff) +#endif + +#ifndef NSTACK_MAX_U64_NUM +#define NSTACK_MAX_U64_NUM ((u64_t)0xffffffffffffffff) +#endif + +#ifndef NSTACK_MAX_S64_NUM +#define NSTACK_MAX_S64_NUM ((s64_t)0x7fffffffffffffff) +#endif + +typedef uint64_t u64_t; +typedef int64_t s64_t; + +typedef uint32_t u32_t; +typedef int32_t s32_t; + +typedef uint16_t u16_t; +typedef int16_t s16_t; + +typedef uint8_t u8_t; +typedef int8_t s8_t; + +typedef uintptr_t mem_ptr_t; + +typedef s8_t err_t; + +#define ERR_OK 0 /* No error, everything OK. */ +#define ERR_MEM -1 /* Out of memory error. */ + +#ifndef ns_bool +typedef unsigned char ns_bool; +#endif + +#ifndef ns_false +#define ns_false 0 +#endif + +#ifndef ns_true +#define ns_true 1 +#endif + +#ifndef ns_success +#define ns_success 0 +#endif + +#ifndef ns_fail +#define ns_fail -1 +#endif + +#ifndef ns_static +#define ns_static static +#endif + +#ifndef ns_uint32 +typedef unsigned int ns_uint32; +#endif + +#ifndef ns_int32 +typedef signed int ns_int32; +#endif + +#ifndef ns_char +typedef char ns_char; +#endif + +#ifndef ns_uchar +typedef unsigned char ns_uchar; +#endif + +#ifndef ns_int8 +typedef signed char ns_int8; +#endif + +#ifndef ns_uint8 +typedef unsigned char ns_uint8; +#endif + +#ifndef ns_uint16 +typedef unsigned short ns_uint16; +#endif + +#ifndef ns_int16 +typedef signed short ns_int16; +#endif + +#ifndef ns_uint +typedef unsigned int ns_uint; +#endif + +#ifndef ns_int +typedef signed int ns_int; +#endif + +#ifndef ns_ullong +typedef unsigned long long ns_ullong; +#endif + +#ifndef ns_llong +typedef long long ns_llong; +#endif + +#ifdef ns_slong +typedef signed long ns_slong; +#endif + +#ifdef ns_ulong +typedef unsigned long ns_ulong; +#endif + +#ifdef __cplusplus +/* *INDENT-OFF* */ +} +/* *INDENT-ON* */ +#endif + +#endif /* _NSTACK_TYPES_H_ */ diff --git a/src/nSocket/include/select_adapt.h b/src/nSocket/include/select_adapt.h new file mode 100644 index 0000000..2898c76 --- /dev/null +++ b/src/nSocket/include/select_adapt.h @@ -0,0 +1,105 @@ +/* +* +* Copyright (c) 2018 Huawei Technologies Co.,Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at: +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/*==============================================* + * include header files * + *----------------------------------------------*/ + +/*==============================================* + * constants or macros define * + *----------------------------------------------*/ + +/*==============================================* + * project-wide global variables * + *----------------------------------------------*/ + +/*==============================================* + * routines' or functions' implementations * + *----------------------------------------------*/ + +#ifndef __SELECT_ADAPT_H__ +#define __SELECT_ADAPT_H__ + +#include "types.h" +#include "nstack_module.h" +#include "common_mem_spinlock.h" +#include "nstack_securec.h" +#include "common_func.h" + +#define SBR_MAX_FD_NUM MAX_SOCKET_NUM + +#define NSTACK_SELECT_MAX_FD 8192 +#define NSTACK_SETSIZE 8192 + +#define FREE_FD_SET(readfd, writefd, exceptfd) {\ + if(readfd)\ + free(readfd);\ + if(writefd)\ + free(writefd);\ + if(exceptfd)\ + free(exceptfd);\ +} + +typedef sem_t select_sem_t; +typedef common_mem_spinlock_t select_spinlock_t; + +#define select_spin_lock(lock) (common_mem_spinlock_lock((lock))) +#define select_spin_unlock(lock) (common_mem_spinlock_unlock((lock))) +#define select_spin_lock_init(lock) (common_mem_spinlock_init((lock))) + +#define select_sem_wait(sem) (sem_wait((sem))) +#define select_sem_init(sem, share, val) (sem_init((sem), (share), (val))) +#define select_sem_post(sem) (sem_post((sem))) + +/*************input form other modules***************************/ +extern nstack_module_info g_nstack_modules; +#define get_mode_num() (g_nstack_modules.modNum) +#define get_mode_linux_index() (g_nstack_modules.fix_mid) + +struct select_comm_fd_map +{ + i32 mod_fd[NSTACK_MAX_MODULE_NUM]; //save modules fd + i32 index; //-1 mean not select modules +}; + +struct select_mod_fd_map +{ + i32 *comm_fd; //the fd app used +}; + +struct select_fd_map_inf +{ + + struct select_comm_fd_map *fdinf; //NSTACK_MAX_SOCK_NUM + struct select_mod_fd_map modinf[NSTACK_MAX_MODULE_NUM]; + +}; +void *select_alloc (int size); +void select_free (char *p); +void reset_select_fdinf (i32 fd); +i32 select_get_modfd (i32 fd, i32 inx); +i32 select_set_modfd (i32 fd, i32 inx, i32 modfd); +i32 select_get_modindex (i32 fd); +i32 select_get_commfd (i32 modfd, i32 inx); +i32 select_set_commfd (i32 modfd, i32 inx, i32 fd); +i32 fdmapping_init (void); +i32 select_set_index (i32 fd, i32 inx); +i32 select_set_profd (i32 fd, i32 profd); +void nssct_close (i32 cfd, i32 inx); +void nssct_create (i32 cfd, i32 mfd, i32 inx); +void nssct_set_index (i32 fd, i32 inx); + +#endif -- cgit 1.2.3-korg