aboutsummaryrefslogtreecommitdiffstats
path: root/src/nSocket/kernel/linux_kernel_module.c
blob: 1943b186fd87c5b73f9e005aeb60eb50cdc857d0 (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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/*
*
* 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.
*/

#pragma GCC diagnostic ignored "-Wcpp"
#include <stdio.h>
#include <stdlib.h>
#include <sys/epoll.h>
#include <errno.h>
#include<sys/types.h>
#include<sys/time.h>
#include<unistd.h>
#include <string.h>
#include <pthread.h>
#include "nstack_types.h"
#include "nstack_sockops.h"
#include "nstack_log.h"
#include "linux_kernel_module.h"
#include "linux_kernel_socket.h"
#include "nsfw_base_linux_api.h"
#include "nstack_fd_mng.h"
#include "nstack_dmm_api.h"

#define SK_MAX_EP_EVENT 1024

kernel_stack_info_t g_ksInfo = {.thread_inited = ks_false,.epfd =
    -1,.checkEpollFD = -1
};

/*design ensures that g_ksInfo is not write accessed at the same time.
only read is done simultaneously with no chance of other thread writing it.
so no protection needed.*/
/* Custodial pointer not freed for events at end of this function */
/* This can be ignored as this is a thread and reuns in infinite loop. Hence will never return */
void *
ks_ep_thread (void *arg)
{
  int eventNum = 0;
  int loop = 0;
  struct epoll_event *events =
    (struct epoll_event *) malloc (SK_MAX_EP_EVENT *
                                   sizeof (struct epoll_event));
  struct epoll_event *innerEvt =
    (struct epoll_event *) malloc (SK_MAX_EP_EVENT *
                                   sizeof (struct epoll_event));

  if (NULL == events || NULL == innerEvt)
    {
      NSSOC_LOGERR ("malloc events failed");

      if (events)
        {
          free (events);
          events = NULL;        /* Set NULL to pointer after free */
        }

      if (innerEvt)
        {
          free (innerEvt);
          innerEvt = NULL;      /* Set NULL to pointer after free */
        }
      /* When ks_ep_thread failed, it should set g_ksInfo.thread_inited ks_true, otherwise,it will result kernel_stack_register in dead loop */
      g_ksInfo.thread_inited = ks_true;
      return NULL;
    }

  if (-1 == g_ksInfo.epfd)
    {
      NSTACK_CAL_FUN (&g_ksInfo.libcOps, epoll_create, (1), g_ksInfo.epfd);
    }

  if (-1 == g_ksInfo.epfd)
    {
      g_ksInfo.thread_inited = ks_true;

      if (events)
        {
          free (events);
          events = NULL;        /* Set NULL to pointer after free */
        }

      if (innerEvt)
        {
          free (innerEvt);
          innerEvt = NULL;      /* Set NULL to pointer after free */
        }

      return NULL;
    }

  g_ksInfo.thread_inited = ks_true;

  do
    {

      NSTACK_CAL_FUN (&g_ksInfo.libcOps, epoll_wait,
                      (g_ksInfo.epfd, events, SK_MAX_EP_EVENT, -1), eventNum);

      if (0 == eventNum)
        {
          sys_sleep_ns (0, 100000);

        }

      for (loop = 0; loop < eventNum; loop++)
        {

          NSSOC_LOGDBG ("Epoll]events=%u,epfd=%d", events[loop].events,
                        events[loop].data.fd);

          if (events[loop].events & EPOLLIN)
            {
              int i = 0, num = 0, ret = 0, epfd = events[loop].data.fd;
              NSTACK_CAL_FUN (&g_ksInfo.libcOps, epoll_wait,
                              (epfd, innerEvt, SK_MAX_EP_EVENT, 0), num);

              if (0 == num)
                {
                  NSSOC_LOGWAR ("Num is zero");
                  continue;
                }

              NSTACK_CAL_FUN (&g_ksInfo.libcOps, epoll_ctl,
                              (g_ksInfo.epfd, EPOLL_CTL_DEL, epfd, NULL),
                              ret);

              ret = -1;
              for (i = 0; i < num; i++)
                {
                  ret &=
                    g_ksInfo.regVal.event_cb (innerEvt[i].data.ptr,
                                              innerEvt[i].events);
                  NSSOC_LOGDBG ("Kernel got one event]i=%d,ptr=%d,events=%u",
                                i, innerEvt[i].data.ptr, innerEvt[i].events);
                }

              if (ret)
                {
                  struct epoll_event ev;
                  ev.data.fd = epfd;
                  ev.events = EPOLLIN;
                  NSTACK_CAL_FUN (&g_ksInfo.libcOps, epoll_ctl,
                                  (g_ksInfo.epfd, EPOLL_CTL_ADD, epfd, &ev),
                                  ret);
                }
            }
        }
    }
  while (1);

}

int
kernel_fd_check (int fd, int flag)
{
  struct epoll_event event;
  event.data.fd = fd;
  event.events = EPOLLIN | EPOLLERR;
  if (fd == g_ksInfo.checkEpollFD)
    {
      return 0;
    }

  /*in order to reduce the cost of epoll ctl */
  if (STACK_FD_FUNCALL_CHECK == flag)
    {
      return 1;
    }

  if (-1 ==
      nsfw_base_epoll_ctl (g_ksInfo.checkEpollFD, EPOLL_CTL_ADD, fd, &event))
    {
      return 0;
    }

  nsfw_base_epoll_ctl (g_ksInfo.checkEpollFD, EPOLL_CTL_DEL, fd, NULL);
  return 1;
}

int
kernel_prewait_proc (int epfd)
{
  int ret = 0;
  struct epoll_event ep_event;

  NSSOC_LOGINF ("epfd=%d was added", epfd);
  if (epfd < 0)
    {
      return -1;
    }
  ep_event.data.fd = epfd;
  ep_event.events = EPOLLIN | EPOLLET;
  ret = lk_epollctl (0, EPOLL_CTL_ADD, epfd, &ep_event);
  return ret;
}

unsigned int
kernel_ep_fd_add (int epFD, int proFD, int ctl_ops,
                  struct epoll_event *events, void *pdata)
{
  struct epoll_event tmpEvt;
  int ret = 0;
  tmpEvt.data.ptr = pdata;
  tmpEvt.events = events->events;
  NSSOC_LOGINF ("epfd=%d,fd=%d,ops=%d, events=%u", epFD, proFD, ctl_ops,
                events->events);
  switch (ctl_ops)
    {
    case nstack_ep_triggle_add:
      ret = nsfw_base_epoll_ctl (epFD, EPOLL_CTL_ADD, proFD, &tmpEvt);
      break;
    case nstack_ep_triggle_mod:
      ret = nsfw_base_epoll_ctl (epFD, EPOLL_CTL_MOD, proFD, &tmpEvt);
      break;
    case nstack_ep_triggle_del:
      ret = nsfw_base_epoll_ctl (epFD, EPOLL_CTL_DEL, proFD, &tmpEvt);
      break;
    default:
      ret = -1;
      break;
    }
  return ret;
}

int
kernel_socket (int a, int b, int c)
{

  return nsfw_base_socket (a, b, c);
}

int
kernel_module_init ()
{
  int retval = 0;

  g_ksInfo.thread_inited = ks_false;
  retval = pthread_create (&g_ksInfo.ep_thread, NULL, ks_ep_thread, NULL);
  if (retval != 0)
    {
      NSPOL_LOGERR ("kernel ep thread create fail, errno:%d!", errno);
      return ks_fail;
    }

  /* The earlier thread "ep_thread" created will exit automatically when
     return failure from below if any failure */
  retval = pthread_setname_np (g_ksInfo.ep_thread, K_EPOLL_THREAD_NAME);
  if (retval != 0)
    {
      NSMON_LOGERR
        ("pthread_setname_np failed for ep_thread]retval=%d, errno:%d",
         retval, errno);
      /*set thread name failed no need to return */
    }

  NSSOC_LOGDBG ("New thread started");

  do
    {
      sys_sleep_ns (0, 0);
    }
  while (!g_ksInfo.thread_inited);

  if (-1 == g_ksInfo.epfd)
    {
      NSPOL_LOGERR ("thread epoll create Err!");
      retval |= -1;
    }
  return retval;
}

int
kernel_fd_alloc ()
{
  return nsfw_base_socket (AF_UNIX, SOCK_DGRAM, 0);
}

int
kernel_stack_register (nstack_proc_cb * ops, nstack_event_cb * val)
{
  int retval = 0;
  /*  Input parameter validation */
  if ((NULL == ops) || (NULL == val))
    {
      NSPOL_LOGERR ("input param is NULL");
      return ks_fail;
    }

#undef NSTACK_MK_DECL
#define NSTACK_MK_DECL(ret, fn, args) \
    g_ksInfo.libcOps.pf##fn = nsfw_base_##fn;

#include "declare_syscalls.h"

  g_ksInfo.regVal = *val;

  /*create a efpd to check fd is ok */
  if (g_ksInfo.checkEpollFD == -1)
    {
      g_ksInfo.checkEpollFD = nsfw_base_epoll_create (1);
    }

  NSSOC_LOGDBG ("start to regist stack");

  ops->socket_ops = g_ksInfo.libcOps;
  MEMSET_S (&(ops->extern_ops), sizeof (ops->extern_ops), 0,
            sizeof (ops->extern_ops));
  NSTACK_SET_OPS_FUN (&(ops->socket_ops), listen, lk_listen);
  NSTACK_SET_OPS_FUN (&(ops->socket_ops), epoll_ctl, lk_epollctl);
  NSTACK_SET_OPS_FUN (&(ops->socket_ops), socket, kernel_socket);
  ops->extern_ops.stack_fd_check = kernel_fd_check;
  ops->extern_ops.ep_ctl = kernel_ep_fd_add;
  ops->extern_ops.ep_prewait_proc = kernel_prewait_proc;
  ops->extern_ops.module_init = kernel_module_init;
  ops->extern_ops.stack_alloc_fd = kernel_fd_alloc;

  /* don't close file descriptor */

  return retval ? ks_fail : ks_success;
}