aboutsummaryrefslogtreecommitdiffstats
path: root/src/nSocket/nstack/event/epoll/nstack_eventpoll.c
blob: a4ce0cc35c7ed725c74cfb5067bc14177a419d56 (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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
/*
*
* 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 "nstack_eventpoll.h"
#include "nstack_log.h"
#include "nsfw_recycle_api.h"
#include "nstack_securec.h"
#include "nstack_module.h"
#include "nstack_sockops.h"
#include "nsfw_mem_api.h"
#include "nstack_fd_mng.h"
#include "nstack.h"
#include "nstack_dmm_adpt.h"

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

#define NSTACK_EP_TRIGGLE(epFD, proFD, modInx, triggle_ops, event, pdata, ret) \
    if (nstack_extern_deal(modInx).ep_ctl != NULL) \
    { \
        ret = nstack_extern_deal(modInx).ep_ctl((epFD), (proFD), (triggle_ops), (event), pdata); \
    }

#define nstack_ep_getEvt(epFD, profd, modInx, events, ret) \
    if (nstack_extern_deal(modInx).ep_getevt != NULL)\
    {   \
        ret |= nstack_extern_deal(modInx).ep_getevt((epFD), (profd), (events)); \
    }

#define NSEP_IS_SOCK_VALID(_sock) ((_sock) >= 0 && (u32_t)(_sock) < NSTACK_KERNEL_FD_MAX)

/*add event to rdlist if event have come in*/
void
nsep_ctl_event_add (struct epitem *epi, struct eventpoll *ep,
                    int triggle_ops, unsigned int events)
{
  switch (triggle_ops)
    {
    case nstack_ep_triggle_add:
      if (events & epi->event.events)
        {
          sys_arch_lock_with_pid (&ep->lock);

          if (!EP_HLIST_NODE_LINKED (&epi->rdllink))
            {
              ep_hlist_add_tail (&ep->rdlist, &epi->rdllink);
            }

          sys_sem_s_signal (&ep->lock);
          sem_post (&ep->waitSem);
        }

      break;

    case nstack_ep_triggle_mod:
      sys_arch_lock_with_pid (&ep->lock);

      if (events & epi->event.events)
        {
          if (!EP_HLIST_NODE_LINKED (&epi->rdllink))
            {
              ep_hlist_add_tail (&ep->rdlist, &epi->rdllink);
              sem_post (&ep->waitSem);
            }
        }
      else
        {
          if (EP_HLIST_NODE_LINKED (&epi->rdllink)
              && (epi->event.events & EPOLLET))
            {
              ep_hlist_del (&ep->rdlist, &epi->rdllink);
            }
        }

      sys_sem_s_signal (&ep->lock);
      break;
    default:
      break;
    }

  return;
}

/*
 *    Triggle epoll events of stack
 *    ep - eventpoll instance
 *    fdInf - file descriptor of stack
 *    triggle_ops - why triggle
 */
NSTACK_STATIC void
nsep_epctl_triggle (struct epitem *epi, nsep_epollInfo_t * info,
                    int triggle_ops)
{
  int modInx;
  int protoFd = -1;
  int epfd = 0;
  int ret = 0;
  struct eventpoll *ep = NULL;
  nsep_epollInfo_t *epInfo = NULL;

  ep = ADDR_SHTOL (epi->ep);
  epfd = ep->epfd;
  epInfo = nsep_get_infoBySock (epfd);

  NSSOC_LOGINF ("info=%p,info->rmidx=%d,triggle_ops=%d", info, info->rmidx,
                triggle_ops);

  /* Now need to triggle userspace network stack events after add operation */
  if (info->rmidx >= 0)
    {
      /* fix overflow type codex issue */
      if ((info->rmidx >= NSEP_SMOD_MAX)
          || (info->rmidx >= NSTACK_MAX_MODULE_NUM))
        {
          return;
        }

      NSTACK_EP_TRIGGLE (epInfo->protoFD[info->rmidx], info->rlfd,
                         info->rmidx, triggle_ops, &(epi->event), info, ret);

      if ((ret >= 0)
          && ((nstack_ep_triggle_add == triggle_ops)
              || (nstack_ep_triggle_mod == triggle_ops)))
        {
          epi->revents = ret;
          nsep_ctl_event_add (epi, ep, triggle_ops, epi->revents);
          info->epaddflag |= (1 << info->rmidx);
        }
      NSSOC_LOGINF ("info=%p,module=%s,protoFd=%d,triggle_ops=%d, ret:%d",
                    info, nstack_get_module_name_by_idx (info->rmidx),
                    info->rlfd, triggle_ops, ret);
    }
  else
    {
      nstack_each_modInx (modInx)
      {
        protoFd = info->protoFD[modInx];

        if (protoFd < 0)
          {
            continue;
          }
        NSTACK_EP_TRIGGLE (epInfo->protoFD[modInx], protoFd, modInx,
                           triggle_ops, &(epi->event), info, ret);
        if ((ret >= 0)
            && ((nstack_ep_triggle_add == triggle_ops)
                || (nstack_ep_triggle_mod == triggle_ops)))
          {
            epi->revents |= ret;
            nsep_ctl_event_add (epi, ep, triggle_ops, epi->revents);
            info->epaddflag |= (1 << modInx);
          }
        NSSOC_LOGINF ("info=%p,module=%s,protoFd=%d,triggle_ops=%d, ret:%d",
                      info, nstack_get_module_name_by_idx (modInx), protoFd,
                      triggle_ops, ret);
      }
    }
  return;
}

NSTACK_STATIC void
nsep_rbtree_insert (struct eventpoll *ep, struct epitem *epi)
{
  struct ep_rb_node **p = &ep->rbr.rb_node, *parent = NULL;
  struct epitem *epic;
  u32_t loopCnt = 0;

  while (*p)
    {
      ++loopCnt;
      if (loopCnt > NSTACK_MAX_EPITEM_NUM)
        {
          NSSOC_LOGERR ("Loop out of range!!!!");
          break;
        }

      parent = (struct ep_rb_node *) ADDR_SHTOL (*p);
      epic = ep_rb_entry (parent, struct epitem, rbn);
      if (epi->fd > epic->fd)
        {
          p = &(parent->rb_right);
        }
      else
        {
          p = &(parent->rb_left);
        }
    }

  ep_rb_link_node (&epi->rbn, parent, p);
  ep_rb_insert_color (&epi->rbn, &ep->rbr);
}

/*
 *    This function is called by epctl_add , it will create one epitem of fd, and insert to eventpoll
 */
NSTACK_STATIC int
nsep_insert_node (struct eventpoll *ep, struct epoll_event *event,
                  nsep_epollInfo_t * fdInfo)
{
  struct epitem *epi;

  if (nsep_alloc_epitem (&epi))
    {
      NSSOC_LOGERR ("Can't alloc epitem");
      errno = ENOMEM;
      return -1;
    }

  EP_HLIST_INIT_NODE (&epi->rdllink);
  EP_HLIST_INIT_NODE (&epi->lkFDllink);

  epi->nwait = 0;
  epi->ep = (struct eventpoll *) ADDR_LTOSH_EXT (ep);
  epi->epInfo = (nsep_epollInfo_t *) ADDR_LTOSH_EXT (fdInfo);
  epi->revents = 0;
  epi->ovf_revents = 0;
  epi->event = *event;
  EP_LIST_INIT_NODE (&epi->fllink);
  epi->revents = 0;
  epi->fd = fdInfo->fd;

  /* Add the current item to the list of active epoll hook for this file
     This should lock because file descriptor may be called by other eventpoll */

  sys_arch_lock_with_pid (&fdInfo->epiLock);
  ep_list_add_tail (&fdInfo->epiList, &epi->fllink);
  epi->private_data = (void *) ADDR_LTOSH_EXT (fdInfo);
  sys_sem_s_signal (&fdInfo->epiLock);

  /* Add epitem to eventpoll fd list, don't need lock here because epoll_ctl will lock before calling this function */
  nsep_rbtree_insert (ep, epi);

  /* Need to poll the events already stored in stack */
  nsep_epctl_triggle (epi, fdInfo, nstack_ep_triggle_add);

  NSSOC_LOGINF ("epfd=%d,ep=%p,fd=%d,epi=%p", ep->epfd, ep, fdInfo->fd, epi);

  return 0;
}

NSTACK_STATIC int
nsep_isAddValid (int fd)
{

  if (nstack_fix_fd_check ())
    {
      return nstack_fix_fd_check ()(fd, STACK_FD_INVALID_CHECK);
    }
  return 0;
}

int
nsep_epctl_add (struct eventpoll *ep, int fd, struct epoll_event *events)
{

  int ret = 0;

  NSSOC_LOGINF ("epfd=%d,fd=%d,events=%u", ep->epfd, fd, events->events);

  nsep_epollInfo_t *fdInfo = nsep_get_infoBySock (fd);

  if (NULL == fdInfo)
    {
      if (0 == nsep_isAddValid (fd))
        {
          NSSOC_LOGERR ("Invalid add check nfd=%d]", fd);
          return -1;
        }
      if (-1 == nsep_alloc_infoWithSock (fd))
        {
          NSSOC_LOGERR ("Can't alloc epInfo for nfd]nfd=%d", fd);
          return -1;
        }
      nsep_set_infoProtoFD (fd, nstack_get_fix_mid (), fd);
      fdInfo = nsep_get_infoBySock (fd);
    }

  ret = nsep_insert_node (ep, events, fdInfo);
  if (0 != ret)
    {
      NSSOC_LOGWAR ("insert fail]epfd=%d,fd=%d ", ep->epfd, fdInfo->fd);
      return -1;
    }

  return 0;
}

int
nsep_epctl_del (struct eventpoll *ep, struct epitem *epi)
{
  int ret = 0;

  nsep_epollInfo_t *epInfo =
    (nsep_epollInfo_t *) ADDR_SHTOL (epi->private_data);
  NSSOC_LOGINF ("epfd=%d,fd=%d,epi=%p", ep->epfd, epi->fd, epi);        // modify log format error

  nsep_epctl_triggle (epi, epInfo, nstack_ep_triggle_del);

  sys_arch_lock_with_pid (&epInfo->epiLock);
  ep_list_del (&epInfo->epiList, &epi->fllink);

  sys_sem_s_signal (&epInfo->epiLock);

  sys_arch_lock_with_pid (&ep->lock);
  ret = nstack_ep_unlink (ep, epi);
  sys_sem_s_signal (&ep->lock);
  nsep_free_epitem (epi);

  return ret;
}

int
nsep_epctl_mod (struct eventpoll *ep,
                nsep_epollInfo_t * epInfo,
                struct epitem *epi, struct epoll_event *events)
{
  if (NULL == epInfo)
    {
      errno = EINVAL;
      NSSOC_LOGWAR ("epfd=%d, intput epInfo is null err", ep->epfd);
      return -1;
    }

  NSSOC_LOGINF ("epfd=%d,fd=%d,events=%u", ep->epfd, epInfo->fd,
                events->events);

  sys_arch_lock_with_pid (&ep->lock);
  epi->event = *events;         /* kernel tells me that I need to modify epi->event in lock context */
  sys_sem_s_signal (&ep->lock);

  nsep_epctl_triggle (epi, epInfo, nstack_ep_triggle_mod);
  return 0;
}

/*
 * Called by nsep_ep_poll
 * proccess the event in the list
 */
static int
nsep_events_proc (struct eventpoll *ep,
                  struct ep_node_list *nhead,
                  struct epoll_event *events, int maxevents, int *eventflag,
                  int num)
{
  struct ep_hlist_node *node = NULL;
  struct epitem *epi = NULL;
  struct ep_hlist_node *head = nhead->head;
  int evt = 0;

  while (head)
    {
      node = head;
      epi = ep_hlist_entry (node, struct epitem, rdllink);
      head = (struct ep_hlist_node *) ADDR_SHTOL (node->next);
      EP_HLIST_INIT_NODE (node);

      nsep_epollInfo_t *fdInfo =
        (nsep_epollInfo_t *) ADDR_SHTOL (epi->private_data);

      if (fdInfo->rmidx != -1)
        {
          nstack_ep_getEvt (ep->epfd, fdInfo->rlfd, fdInfo->rmidx,
                            epi->event.events, (epi->revents));
        }

      if (epi->revents)
        {
          /*set the flag that already have event int the rdlist */
          if (eventflag && (fdInfo->rmidx >= 0) && (fdInfo->rmidx < num))
            {
              eventflag[fdInfo->rmidx] = 1;
            }
          events[evt].events = epi->revents;
          events[evt].data = epi->event.data;
          NSSOC_LOGDBG ("Add event]epfd=%d,fd=%d,events=%u", ep->epfd,
                        epi->fd, events[evt].events);
          evt++;
        }

      if (0 != epi->revents && EPOLLET != (epi->event.events & EPOLLET))
        {
          NSSOC_LOGDBG ("Add epi->rdllink,epfd=%d,fd=%d", ep->epfd, epi->fd);
          ep_hlist_add_tail (&ep->rdlist, &epi->rdllink);
        }
      epi->revents = 0;

      if (evt >= maxevents)
        break;
    }
  nhead->head = head;
  if (!head)
    {
      nhead->tail = NULL;
    }
  return evt;
}

/*
 * Called by epoll_wait
 * Wait until events come or timeout
 */
int
nsep_ep_poll (struct eventpoll *ep, struct epoll_event *events, int maxevents,
              int *eventflag, int num)
{

  struct ep_hlist_node *tail = NULL;
  struct ep_hlist_node *head = NULL;
  struct epitem *epi = NULL;
  struct epitem *nepi = NULL;
  struct ep_node_list nhead = { 0 };
  int evt = 0;
  nsep_epollInfo_t *fdInfo = NULL;

  if (EP_HLIST_EMPTY (&ep->rdlist))
    {
      NSSOC_LOGDBG ("ep->rdlist is Empty, epfd=%d", ep->epfd);
      return 0;
    }

  sys_arch_lock_with_pid (&ep->sem);

  if (EP_HLIST_EMPTY (&ep->rdlist))
    {
      goto out;
    }

  sys_arch_lock_with_pid (&ep->lock);
  head = (struct ep_hlist_node *) ADDR_SHTOL (ep->rdlist.head);
  if (!head)
    {
      sys_sem_s_signal (&ep->lock);
      goto out;
    }
  nhead.head = (struct ep_hlist_node *) ADDR_SHTOL (head->next);
  nhead.tail = (struct ep_hlist_node *) ADDR_SHTOL (ep->rdlist.tail);
  /*unlink from ep->rdlist */
  EP_HLIST_INIT (&(ep->rdlist));
  ep->ovflist = NULL;
  sys_sem_s_signal (&ep->lock);

  /*get event list */
  evt = nsep_events_proc (ep, &nhead, events, maxevents, eventflag, num);

  sys_arch_lock_with_pid (&ep->lock);
  /*put rest epitem back to the rdlist */
  if (nhead.head)
    {
      tail = (struct ep_hlist_node *) ADDR_SHTOL (ep->rdlist.tail);
      tail->next = (struct ep_hlist_node *) ADDR_LTOSH (nhead.head);
      nhead.head->pprev = (struct ep_hlist_node **) ADDR_LTOSH (&tail->next);
      ep->rdlist.tail = (struct ep_hlist_node *) ADDR_LTOSH (nhead.tail);
    }
  /*put the epitem in ep->ovflist to rdlist */
  for (nepi = (struct epitem *) ADDR_SHTOL (ep->ovflist);
       (epi = nepi) != NULL;
       nepi = (struct epitem *) ADDR_SHTOL (epi->next), epi->next =
       NSEP_EP_UNACTIVE_PTR)
    {
      epi->revents |= epi->ovf_revents;
      /*set the flag that already have event int the rdlist */
      fdInfo = (nsep_epollInfo_t *) ADDR_SHTOL (epi->private_data);
      if (eventflag && fdInfo && (fdInfo->rmidx >= 0)
          && (fdInfo->rmidx < num))
        {
          eventflag[fdInfo->rmidx] = 1;
        }
      epi->ovf_revents = 0;
      if (!EP_HLIST_NODE_LINKED (&epi->rdllink))
        {
          ep_hlist_add_tail (&ep->rdlist, &epi->rdllink);
        }
    }
  ep->ovflist = NSEP_EP_UNACTIVE_PTR;
  sys_sem_s_signal (&ep->lock);
out:
  sys_sem_s_signal (&ep->sem);
  NSSOC_LOGDBG ("Return epfd=%d,evt=%d,EP_HLIST_EMPTY(&ep->rdlist)=%d",
                ep->epfd, evt, EP_HLIST_EMPTY (&ep->rdlist));
  return evt;
}

#define FREE_LIST_SIZE 128
#ifdef FREE_LIST_SIZE
struct free_list
{
  struct free_list *next;
  struct list_node *node[FREE_LIST_SIZE];
};
#endif

void
nsep_remove_epfd (nsep_epollInfo_t * pinfo)
{
  pid_t pid = get_sys_pid ();
  struct list_node *prenode = NULL;
  struct list_node *nextnode = NULL;
#ifdef FREE_LIST_SIZE
  struct free_list flist;
  struct free_list *fcurr = &flist;
#else
  struct list_node **node_arry = NULL;
  int length = NSTACK_MAX_EPOLL_INFO_NUM * sizeof (struct list_node *);
#endif
  struct epitem *epi = NULL;
  struct epitem *tepi = NULL;
  struct eventpoll *ep = NULL;
  u32_t i = 0;
  u32_t icnt = 0;

  if (!pinfo)
    {
      return;
    }
  /*malloc a block memory to store epitem node, do not use list for maybe free item */
#ifdef FREE_LIST_SIZE
  flist.next = 0;
#else
  node_arry = (struct list_node **) malloc (length);
  if (!node_arry)
    {
      NSSOC_LOGERR ("remove fd from ep malloc mem fail]fd=%d,ep=%p",
                    pinfo->fd, pinfo->ep);
      return;
    }

  int retVal = MEMSET_S (node_arry, length, 0, length);
  if (EOK != retVal)
    {
      NSSOC_LOGERR ("MEMSET_S failed]retVal=%d", retVal);
      free (node_arry);
      return;
    }
#endif
  sys_arch_lock_with_pid (&pinfo->epiLock);
  /*list head must be not null */
  prenode = (struct list_node *) ADDR_SHTOL (pinfo->epiList.head);
  nextnode = (struct list_node *) ADDR_SHTOL (prenode->next);
  icnt = 0;

  /*find all node that pid is belong to itself */
  while (nextnode)
    {
      if (++i > NSTACK_MAX_EPOLL_INFO_NUM)
        {
          /*record the exception log */
          NSSOC_LOGERR ("error maybe happen]free pinfo=%p", pinfo);
          break;
        }
      epi = ep_list_entry (nextnode, struct epitem, fllink);
      if (pid == epi->pid)
        {
          prenode->next = nextnode->next;
          nextnode->next = NULL;
          /*put into release list */
#ifdef FREE_LIST_SIZE
          if ((icnt % FREE_LIST_SIZE) == 0 && icnt)
            {
              struct free_list *fnext =
                (struct free_list *) malloc (sizeof (struct free_list));
              if (!fnext)
                {
                  NSSOC_LOGERR ("Out of memory for freelist]fd=%d icnt=%u",
                                pinfo->fd, icnt);
                  break;
                }
              fnext->next = NULL;
              fcurr->next = fnext;
              fcurr = fnext;
            }
          fcurr->node[icnt % FREE_LIST_SIZE] = nextnode;
#else
          node_arry[icnt] = nextnode;
#endif
          icnt++;
        }
      else
        {
          prenode = nextnode;
        }
      nextnode = (struct list_node *) ADDR_SHTOL (prenode->next);
    }

  sys_sem_s_signal (&pinfo->epiLock);

  /*free all epitem */
#ifdef FREE_LIST_SIZE
  fcurr = &flist;
#endif
  for (i = 0; i < icnt; i++)
    {
#ifdef FREE_LIST_SIZE
      if ((i % FREE_LIST_SIZE) == 0 && i)
        {
          fcurr = fcurr->next;
          if (fcurr == NULL)
            {
              NSSOC_LOGERR ("freelist invalid NULL, fd=%d icnt=%u i=%u",
                            pinfo->fd, icnt, i);
              break;
            }
        }
      epi =
        ep_list_entry (fcurr->node[i % FREE_LIST_SIZE], struct epitem,
                       fllink);
#else
      epi = ep_list_entry (node_arry[i], struct epitem, fllink);
#endif
      ep = (struct eventpoll *) ADDR_SHTOL (epi->ep);
      if (ep)
        {
          sys_arch_lock_with_pid (&ep->sem);
          /* Here don't use epi you find before, use fd and ep to find the epi again.that is multithread safe */
          tepi = nsep_find_ep (ep, pinfo->fd);
          /*record the exception log */
          if (epi != tepi)
            {
              NSSOC_LOGERR ("remove fd:%d epi:%p tepi:%p erro maybe happen",
                            pinfo->fd, epi, tepi);
            }
          /*if tepi is null, epi maybe free by nsep_close_epfd, so no need to free again */
          if (tepi)
            {
              nsep_epctl_triggle (tepi, pinfo, nstack_ep_triggle_del);
              sys_arch_lock_with_pid (&ep->lock);
              (void) nstack_ep_unlink (ep, tepi);
              sys_sem_s_signal (&ep->lock);

              nsep_free_epitem (epi);
            }
          sys_sem_s_signal (&ep->sem);
        }
    }
#ifdef FREE_LIST_SIZE
  for (fcurr = flist.next; fcurr; fcurr = flist.next)
    {
      flist.next = fcurr->next;
      free (fcurr);
    }
#else
  free (node_arry);
#endif
  return;
}

void
nsep_close_epfd (struct eventpoll *ep)
{

  if (!ep)
    return;

  struct epitem *epi = NULL;
  struct ep_rb_node *node = NULL;

  sys_arch_lock_with_pid (&ep->sem);
  while ((node = ep_rb_first (&ep->rbr)))
    {

      epi = ep_rb_entry (node, struct epitem, rbn);
      int ret = nsep_epctl_del (ep, epi);

      if (ret)
        {
          NSSOC_LOGERR
            ("nstack epctl del fail, will break to avoid dead loop]ep->fd=%d,epi->fd=%d",
             ep->epfd, epi->fd);
          break;
        }
    }
  sys_sem_s_signal (&ep->sem);
  nsep_free_eventpoll (ep);
}

static inline int
nsp_epoll_close_kernel_fd (int sock, nsep_epollInfo_t * epInfo)
{
  NSSOC_LOGINF ("fd=%d,type=%d", sock, epInfo->fdtype);
  int ret = 0;
  nsep_remove_epfd (epInfo);

  u32_t pid = get_sys_pid ();
  sys_arch_lock_with_pid (&epInfo->freeLock);
  int left_count = nsep_del_last_pid (&epInfo->pidinfo, pid);
  sys_sem_s_signal (&epInfo->freeLock);
  if (-1 == left_count)
    {
      NSSOC_LOGERR ("pid not exist]fd=%d,type=%d,pid=%d", sock,
                    epInfo->fdtype, pid);
    }

  if (0 == left_count)
    {
      ret = nsep_free_epinfo (epInfo);
      NSSOC_LOGINF ("epinfo removed]fd=%d,type=%d", sock, epInfo->fdtype);
    }

  return ret;
}

static inline int
nsp_epoll_close_spl_fd (int sock, nsep_epollInfo_t * epInfo)
{
  NSSOC_LOGINF ("fd=%d,type=%d", sock, epInfo->fdtype);
  nsep_remove_epfd (epInfo);
  return 0;
}

static inline int
nsp_epoll_close_ep_fd (int sock, nsep_epollInfo_t * epInfo)
{
  struct eventpoll *ep = ADDR_SHTOL (epInfo->ep);
  u32_t pid = get_sys_pid ();
  NSSOC_LOGINF ("fd:%d is epoll fd ep:%p]", sock, ep);
  sys_arch_lock_with_pid (&epInfo->freeLock);
  int left_count = nsep_del_last_pid (&epInfo->pidinfo, pid);
  sys_sem_s_signal (&epInfo->freeLock);
  if (0 == left_count)
    {
      epInfo->ep = NULL;
      nsep_close_epfd (ep);
      nsep_free_epinfo (epInfo);
    }
  return 0;
}

int
nsep_epoll_close (int sock)
{
  int ret = 0;
  int modInx = 0;
  int flag = 0;
  nsep_epollInfo_t *epInfo = nsep_get_infoBySock (sock);
  if (!epInfo)
    {
      NSSOC_LOGDBG ("epollsock close sock:%d is not exist", sock);
      return 0;
    }

  if (NSTACK_EPOL_FD == epInfo->fdtype)
    {
      ret = nsp_epoll_close_ep_fd (sock, epInfo);
      nsep_set_infoSockMap (sock, NULL);
      return ret;
    }

  nsep_set_infoSockMap (sock, NULL);


  nstack_each_modInx (modInx)
  {
    if (0 == (epInfo->epaddflag & (1 << modInx)))
      {
        flag = 1;
      }
  }
  /*if no stack reference the epinfo, just free epInfo,
   *else wait until the stack closed completely
   */
  if (0 == flag)
    {
      ret = nsp_epoll_close_kernel_fd (sock, epInfo);
    }
  else
    {
      ret = nsp_epoll_close_spl_fd (sock, epInfo);
    }
  return ret;
}

void
nsep_set_infoSockMap (int sock, nsep_epollInfo_t * info)
{
  nsep_epollManager_t *manager = nsep_getManager ();
  if (NULL == manager->infoSockMap)
    return;

  if (sock < 0 || (u32_t) sock >= NSTACK_KERNEL_FD_MAX)
    return;

  manager->infoSockMap[sock] = info;
}

nsep_epollInfo_t *
nsep_get_infoBySock (int sock)
{
  nsep_epollManager_t *manager = nsep_getManager ();
  if ((NULL == manager) || (NULL == manager->infoSockMap))
    return NULL;

  if (sock < 0 || (u32_t) sock >= NSTACK_KERNEL_FD_MAX)
    return NULL;

  return manager->infoSockMap[sock];
}

int
nsep_alloc_infoWithSock (int nfd)
{

  nsep_epollInfo_t *epInfo = NULL;

  if (!NSEP_IS_SOCK_VALID (nfd))
    {
      return -1;
    }

  if (-1 == nsep_alloc_epinfo (&epInfo))
    {
      NSSOC_LOGERR ("Alloc share info fail,[return]");
      return -1;
    }

  epInfo->fd = nfd;

  nsep_set_infoSockMap (nfd, epInfo);

  return 0;
}

void
nsep_set_infoProtoFD (int fd, int modInx, int protoFD)
{
  nsep_epollInfo_t *epInfo = nsep_get_infoBySock (fd);

  if (NULL == epInfo)
    return;

  if (modInx < 0 || modInx >= NSTACK_MAX_MODULE_NUM)
    return;

  epInfo->protoFD[modInx] = protoFD;
}

int
nsep_get_infoProtoFD (int fd, int modInx)
{
  nsep_epollInfo_t *epInfo = nsep_get_infoBySock (fd);

  if (NULL == epInfo)
    return -1;

  return epInfo->protoFD[modInx];
}

void
nsep_set_infomdix (int fd, int rmidx)
{
  nsep_epollInfo_t *epInfo = nsep_get_infoBySock (fd);

  if (NULL == epInfo)
    return;

  epInfo->rmidx = rmidx;
}

int
nsep_get_infoMidx (int fd)
{
  nsep_epollInfo_t *epInfo = nsep_get_infoBySock (fd);

  if (NULL == epInfo)
    return -1;

  return epInfo->rmidx;
}

void
nsep_set_infoRlfd (int fd, int rlfd)
{
  nsep_epollInfo_t *epInfo = nsep_get_infoBySock (fd);

  if (NULL == epInfo)
    return;

  epInfo->rlfd = rlfd;
}

int
nsep_get_infoRlfd (int fd)
{
  nsep_epollInfo_t *epInfo = nsep_get_infoBySock (fd);

  if (NULL == epInfo)
    return -1;

  return epInfo->rlfd;
}

void
nsep_set_infoSleepTime (int fd, u32 sleepTime)
{
  nsep_epollInfo_t *epInfo = nsep_get_infoBySock (fd);

  if (NULL == epInfo)
    return;

  epInfo->sleepTime = sleepTime;
}

int
nsep_get_infoSleepTime (int fd)
{
  nsep_epollInfo_t *epInfo = nsep_get_infoBySock (fd);

  if (NULL == epInfo)
    return -1;

  return epInfo->sleepTime;
}

void
nsep_set_infoEp (int fd, struct eventpoll *ep)
{
  nsep_epollInfo_t *epInfo = nsep_get_infoBySock (fd);

  if (NULL == epInfo)
    return;

  epInfo->ep = (struct eventpoll *) ADDR_LTOSH (ep);
  epInfo->fdtype = NSTACK_EPOL_FD;
}

struct eventpoll *
nsep_get_infoEp (int fd)
{
  nsep_epollInfo_t *epInfo = nsep_get_infoBySock (fd);

  if (NULL == epInfo)
    return NULL;

  return (struct eventpoll *) ADDR_SHTOL (epInfo->ep);
}

int
nsep_free_infoWithSock (int nfd)
{
  if ((u32_t) nfd >= NSTACK_KERNEL_FD_MAX || nfd < 0)
    return -1;

  nsep_epollInfo_t *info = nsep_get_infoBySock (nfd);

  if (NULL == info)
    return 0;

  nsep_set_infoSockMap (nfd, NULL);

  NSSOC_LOGDBG ("nsep_free_infoWithSock info:%p, nfd:%d", info, nfd);
  /* If this not just used by linux, it should be freed in stack-x */
  if (-1 == nsep_free_epinfo (info))
    {
      NSSOC_LOGERR ("Error to free ep info");
      return -1;
    }
  return 0;
}

/**
 * @Function        nsep_init_infoSockMap
 * @Description     initial map of epoll info and socket
 * @param           none
 * @return          0 on success, -1 on error
 */
int
nsep_init_infoSockMap ()
{
  nsep_epollManager_t *manager = nsep_getManager ();
  nsep_epollInfo_t **map =
    (nsep_epollInfo_t **) malloc (NSTACK_KERNEL_FD_MAX *
                                  sizeof (nsep_epollInfo_t *));

  if (!map)
    {
      NSSOC_LOGERR ("malloc epInfoPool fail");
      return -1;
    }

  u32_t iindex;
  for (iindex = 0; iindex < NSTACK_KERNEL_FD_MAX; iindex++)
    {
      map[iindex] = NULL;
    }

  manager->infoSockMap = map;

  return 0;
}

NSTACK_STATIC mzone_handle
nsep_ring_lookup (char *name)
{
  if (NULL == name)
    {
      NSSOC_LOGERR ("param error]name=%p", name);
      return NULL;
    }

  nsfw_mem_name mem_name;
  if (EOK != STRCPY_S (mem_name.aname, sizeof (mem_name.aname), name))
    {
      NSSOC_LOGERR ("Error to lookup ring by name, strcpy fail]name=%s",
                    name);
      return NULL;
    }
  mem_name.enowner = NSFW_PROC_MAIN;
  mem_name.entype = NSFW_SHMEM;

  return nsfw_mem_ring_lookup (&mem_name);
}

NSTACK_STATIC mzone_handle
nsep_attach_shmem (char *name)
{
  if (NULL == name)
    {
      NSSOC_LOGERR ("param error]name=%p", name);
      return NULL;
    }

  nsfw_mem_name mem_name;
  int retVal = STRCPY_S (mem_name.aname, sizeof (mem_name.aname), name);
  if (EOK != retVal)
    {
      NSSOC_LOGERR ("STRCPY_S failed]");
      return NULL;
    }
  mem_name.enowner = NSFW_PROC_MAIN;
  mem_name.entype = NSFW_SHMEM;

  return nsfw_mem_zone_lookup (&mem_name);
}

NSTACK_STATIC int
nsep_attach_infoMem ()
{
  mzone_handle hdl = nsep_attach_shmem (MP_NSTACK_EPOLL_INFO_NAME);
  if (NULL == hdl)
    return -1;

  nsep_epollManager_t *manager = nsep_getManager ();
  manager->infoPool.pool = (nsep_epollInfo_t *) hdl;

  hdl = nsep_ring_lookup (MP_NSTACK_EPINFO_RING_NAME);
  if (NULL == hdl)
    {
      NSSOC_LOGERR ("Fail to lock up epoll info ring]name=%s",
                    MP_NSTACK_EPINFO_RING_NAME);
      return -1;
    }

  manager->infoPool.ring = hdl;

  return 0;
}

NSTACK_STATIC int
nsep_attach_epItemMem ()
{
  mzone_handle hdl = nsep_attach_shmem (MP_NSTACK_EPITEM_POOL);
  if (NULL == hdl)
    return -1;

  nsep_epollManager_t *manager = nsep_getManager ();
  manager->epitemPool.pool = (struct epitem *) hdl;

  hdl = nsep_ring_lookup (MP_NSTACK_EPITEM_RING_NAME);
  if (NULL == hdl)
    {
      NSSOC_LOGERR ("Fail to lock up epoll info ring]name=%s",
                    MP_NSTACK_EPITEM_RING_NAME);
      return -1;
    }

  manager->epitemPool.ring = hdl;

  return 0;
}

NSTACK_STATIC int
nsep_attach_eventpollMem ()
{
  mzone_handle hdl = nsep_attach_shmem (MP_NSTACK_EVENTPOLL_POOL);
  if (NULL == hdl)
    return -1;

  nsep_epollManager_t *manager = nsep_getManager ();
  manager->epollPool.pool = (struct eventpoll *) hdl;

  hdl = nsep_ring_lookup (MP_NSTACK_EVENTPOOL_RING_NAME);
  if (NULL == hdl)
    {
      NSSOC_LOGERR ("Fail to lock up epoll info ring]name=%s",
                    MP_NSTACK_EVENTPOOL_RING_NAME);
      return -1;
    }

  manager->epollPool.ring = hdl;

  return 0;
}

/* epinfo add pid in parent */
void
nsep_fork_parent_proc (u32_t ppid, u32_t cpid)
{
  nsep_epollManager_t *manager = nsep_getManager ();
  if (NULL == manager->infoSockMap)
    {
      NSSOC_LOGERR ("infoSockMap is NULL]ppid=%d,cpid=%d", ppid, cpid);
      return;
    }

  nstack_fd_Inf *fdInf = NULL;
  nsep_epollInfo_t *epinfo = NULL;
  int pos;
  for (pos = 0; (u32_t) pos < NSTACK_KERNEL_FD_MAX; pos++)
    {
      epinfo = manager->infoSockMap[pos];
      if (epinfo)
        {
          fdInf = nstack_getValidInf (pos);
          if (fdInf)
            {
              if (((u32_t) (fdInf->type)) & SOCK_CLOEXEC)
                {
                  NSSOC_LOGINF ("fd is SOCK_CLOEXEC]fd=%d,ppid=%d.cpid=%d",
                                pos, ppid, cpid);
                  continue;
                }
            }

          if (nsep_add_pid (&epinfo->pidinfo, cpid) != 0)
            {
              NSSOC_LOGERR ("epinfo add pid failed]fd=%d,ppid=%d.cpid=%d",
                            pos, ppid, cpid);
            }
          else
            {
              NSSOC_LOGDBG ("epinfo add pid ok]fd=%d,ppid=%d.cpid=%d", pos,
                            ppid, cpid);
            }
        }
    }
}

/* check is pid exist in child,if no,detach epinfo*/
void
nsep_fork_child_proc (u32_t ppid)
{
  nsep_epollManager_t *manager = nsep_getManager ();
  if (NULL == manager->infoSockMap)
    {
      NSSOC_LOGERR ("epinfi sockmap not be create");
      return;
    }

  u32_t cpid = get_sys_pid ();
  nsep_epollInfo_t *epinfo = NULL;
  int pos;
  for (pos = 0; (u32_t) pos < NSTACK_KERNEL_FD_MAX; pos++)
    {
      epinfo = manager->infoSockMap[pos];
      if (epinfo)
        {
          if (!nsep_is_pid_exist (&epinfo->pidinfo, cpid))
            {
              NSSOC_LOGINF
                ("unuse epinfo,happen in SOCK_CLOEXEC,!FD_OPEN,parent coredump]fd=%d,epinfo=%p,ppid=%d,cpid=%d",
                 pos, epinfo, ppid, cpid);
              nsep_set_infoSockMap (pos, NULL);
            }
        }
    }
}

int
nsep_attach_memory ()
{
  typedef int (*nsep_attachMemFunc_t) (void);
  nsep_attachMemFunc_t attachFuncs[] = { nsep_attach_infoMem,
    nsep_attach_epItemMem,
    nsep_attach_eventpollMem
  };

  int i = 0;
  for (i = 0;
       i < (int) (sizeof (attachFuncs) / sizeof (nsep_attachMemFunc_t)); i++)
    {
      if (-1 == attachFuncs[i] ())
        return -1;
    }

  return 0;
}

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