aboutsummaryrefslogtreecommitdiffstats
path: root/src/nSocket/nstack/nstack.h
blob: 1ec88f030de5deb1d139d7f056ce0208e22a42f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
*
* 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_H_
#define _NSTACK_H_

#include <sys/socket.h>
#include <pthread.h>
#include <time.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include "nstack_log.h"
#include "nstack_module.h"
#include "nstack_fd_mng.h"

#include "nstack_types.h"
#include "nstack_eventpoll.h"
#include "common_mem_api.h"
#include "nstack_select.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "nstack_atomic.h"
#include "common_mem_spinlock.h"
#include "generic/common_mem_rwlock.h"

#include "nsfw_mem_api.h"
#include "nsfw_recycle_api.h"

#ifdef __cplusplus
/* *INDENT-OFF* */
extern "C"{
/* *INDENT-ON* */
#endif

#define NSTACK_PIDINFO "hw_nstack_pidinfo"

typedef enum
{
  NSTACK_MODULE_INIT,
  NSTACK_MODULE_INITING,
  NSTACK_MODULE_SUCCESS,
  NSTACK_MODULE_FAIL
} nstack_module_state;

typedef struct
{
  nstack_module_state hasInited;        /*nsocket inside init status */
  nstack_module_state fwInited; /*framework init status */
  nstack_module_state moduleload;       /*framework init status */
  char pad1[2];

  pthread_mutex_t load_mutex;
  /* if a nstack_fd_Inf contains a valid linux kernel proFD, it will be allocated from this poll.
   * Total size of the poll is NSTACK_MAX_SOCK_NUM, index range is [0, NSTACK_MAX_SOCK_NUM).
   */
  nstack_fd_Inf *lk_sockPoll;

  common_mem_rwlock_t fork_lock;        /* to ensure that there is no fd to create and close when fork */
  uint32_t pid;
  char *nstack_version;
  int checkEpollFD;
  uint32_t ikernelfdmax;
} __attribute__ ((__packed__)) nStack_info_t;

#define ENV_NSTACK_RD_CFG_PATH  "NSTACK_RD_CFG_PATH"

#define nstack_set_errno(no) errno = (no)

extern nStack_info_t g_nStackInfo;
extern __thread int g_tloadflag;

#define NSTACK_KERNEL_FD_MAX (g_nStackInfo.ikernelfdmax)
#define NSTACK_THREAD_LOAD_SET()  (g_tloadflag = 1)
#define NSTACK_THREAD_LOAD_UNSET()  (g_tloadflag = 0)
#define NSTACK_THREAD_LOADING()  (g_tloadflag == 1)

#define nstack_each_protoFDSt(modInx, fdInf, st)  \
        for ((modInx) = 0; ((modInx) < nstack_get_modNum() && ((st) = nstack_get_protoFdSt((fdInf), modInx))); (modInx)++)

extern int nstack_timeval2msec (struct timeval *pTime, u64_t * msec);
extern int nstack_current_time2msec (u64_t * msec);
extern int nstack_sem_timedwait (sem_t * pSem, u64_t abs_timeout /*ms */ );
extern int nstack_epoll_sem_timedwait (sem_t * pSem,
                                       u64_t abs_timeout /*ms */ ,
                                       long wait_time /*us */ );
extern int nstack_app_init (void *ppara);
extern int nstack_fw_init ();

nstack_fd_local_lock_info_t *get_fd_local_lock_info (int fd);
void nstack_fork_fd_local_lock_info (nstack_fd_local_lock_info_t *
                                     local_lock);
void nstack_reset_fd_local_lock_info (nstack_fd_local_lock_info_t *
                                      local_lock);
common_mem_rwlock_t *get_fork_lock ();
int nstack_for_epoll_init ();
int nstack_stack_module_load ();

#ifdef __cplusplus
/* *INDENT-OFF* */
}
/* *INDENT-ON* */
#endif

#endif