aboutsummaryrefslogtreecommitdiffstats
path: root/src/adapt/nstack_rd_mng.c
blob: cb6a9b1e3e3ecf44ada602d03e3e3615ff9c9531 (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
/*
*
* 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 <stdlib.h>
#include "nstack_rd_data.h"
#include "nsfw_mem_api.h"
#include "nstack_log.h"
#include "nstack_securec.h"

#define RD_SHMEM_NAME "rd_table"

#define RD_AGE_MAX_TIME   3

rd_route_table *g_rd_table_handle = NULL;

#define RD_IP_ROUTE_CPY(pnode, name, data) {  \
     if (EOK != STRCPY_S((pnode)->data.stack_name, RD_PLANE_NAMELEN, (name))) \
     { \
        NSSOC_LOGERR("STRCPY_S failed]copy_name=%s", name); \
     } \
     (pnode)->data.type = RD_DATA_TYPE_IP; \
     (pnode)->agetime = 0; \
     (pnode)->data.ipdata.addr = (data)->addr; \
     (pnode)->data.ipdata.masklen = (data)->masklen; \
     (pnode)->data.ipdata.resev[0] = 0;  \
     (pnode)->data.ipdata.resev[1] = 0;  \
}

/*****************************************************************************
*   Prototype    : nstack_rd_mng_int
*   Description  : rd mng module init, create a block memory
*   Input        : int flag
*   Output       : None
*   Return Value : int
*   Calls        :
*   Called By    : nStackMain
*****************************************************************************/
int
nstack_rd_mng_int (int flag)
{
  int ret;
  nsfw_mem_zone zname = {
    {NSFW_SHMEM, NSFW_PROC_MAIN, RD_SHMEM_NAME},
    sizeof (rd_route_table),
    NSFW_SOCKET_ANY,
    0
  };
  NSSOC_LOGINF ("nstack rd mng init begin]flag=%d", flag);
  /*nstack main create, app lookup */
  if (0 == flag)
    {
      g_rd_table_handle = (rd_route_table *) nsfw_mem_zone_create (&zname);
      if (!g_rd_table_handle)
        {
          NSSOC_LOGERR ("mem create fail]mem name=%s", RD_SHMEM_NAME);
          return -1;
        }
      ret =
        MEMSET_S (&(g_rd_table_handle->node[0]), sizeof (rd_route_node), 0,
                  sizeof (rd_route_node));
      if (EOK != ret)
        {
          NSSOC_LOGERR ("MEMSET_S failed]ret=%d", ret);
          return -1;
        }
      g_rd_table_handle->size = NSTACK_RD_DATA_MAX;
      g_rd_table_handle->icnt = 0;
    }
  else                          /* static data will not be erased in fault case */
    {
      g_rd_table_handle =
        (rd_route_table *) nsfw_mem_zone_lookup (&(zname.stname));
      if (!g_rd_table_handle)
        {
          NSSOC_LOGERR ("mem lookup fail]mem name=%s", RD_SHMEM_NAME);
          return -1;
        }
    }
  NSSOC_LOGINF ("nstack rd mng init end flag");
  return 0;
}

/*****************************************************************************
*   Prototype    : nstack_rd_ip_node_insert
*   Description  : insert a rd_ip_data into list
*   Input        : char *name
*                  rd_ip_data *data
*   Output       : None
*   Return Value : int
*   Calls        :
*   Called By    : nStackMain
*****************************************************************************/
int
nstack_rd_ip_node_insert (char *name, rd_ip_data * data)
{
  if (!g_rd_table_handle)
    {
      NSSOC_LOGERR ("nstack rd mng not inited");
      return -1;
    }
  int iindex = 0;
  rd_route_node *pnode = NULL;
  int agetime = 0;
  int ageindex = -1;
  int freeindex = -1;
  int repeatflag = 0;

  for (iindex = 0; iindex < NSTACK_RD_DATA_MAX; iindex++)
    {
      pnode = &(g_rd_table_handle->node[iindex]);
      /*record the index of first free element */
      if (RD_NODE_USELESS == pnode->flag)
        {
          if (-1 == freeindex)
            {
              freeindex = iindex;
              NSSOC_LOGINF ("nstack rd ip free element index:%d was found",
                            iindex);
            }
          continue;
        }

      /*if is using, and repeat just set flag */
      if (RD_NODE_USING == pnode->flag)
        {
          if (MASK_V (pnode->data.ipdata.addr, pnode->data.ipdata.masklen) ==
              MASK_V (data->addr, data->masklen))
            {
              NSSOC_LOGWAR
                ("nstack:%s, old_addr:0x%x index:%d new_addr:0x%x, masklen:%u was repeat",
                 name, pnode->data.ipdata.addr, iindex, data->addr,
                 data->masklen);
              repeatflag = 1;
            }
          continue;
        }

      /*if flag is deleting, just update the age time, if agetime is on, just set flag to free */
      if (RD_NODE_DELETING == pnode->flag)
        {
          pnode->agetime++;
          if (pnode->agetime >= RD_AGE_MAX_TIME)
            {
              pnode->flag = RD_NODE_USELESS;
              NSSOC_LOGINF
                ("nstack rd ip element index:%d addr:0x%x, masklen:%u was delete and set to free",
                 iindex, pnode->data.ipdata.addr, pnode->data.ipdata.masklen);
            }
          /*record delete time */
          if (agetime < pnode->agetime)
            {
              agetime = pnode->agetime;
              ageindex = iindex;
            }
          continue;
        }
    }

  /*if repeat, just return */
  if (1 == repeatflag)
    {
      return 0;
    }
  if (-1 == freeindex)
    {
      if (-1 != ageindex)
        {
          freeindex = ageindex;
        }
      else
        {
          NSSOC_LOGERR
            ("the rd table is full,nstack:%s, rd addr:0x%x, masklen:%u can't be inserted",
             name, data->addr, data->masklen);
          return -1;
        }
    }
  pnode = &(g_rd_table_handle->node[freeindex]);
  /*if no free found, just reuse the big agetime */
  RD_IP_ROUTE_CPY (pnode, name, data);
  pnode->flag = RD_NODE_USING;  /*last set */
  g_rd_table_handle->icnt++;
  NSSOC_LOGINF ("nstack:%s, rd addr:0x%x, masklen:%u index was inserted",
                name, data->addr, data->masklen);
  return 0;
}

/*****************************************************************************
*   Prototype    : nstack_rd_ip_node_delete
*   Description  : rd data delete
*   Input        : rd_ip_data *data
*   Output       : None
*   Return Value : int
*   Calls        :
*   Called By    : nStackMain
*                  just set delete flag, because
*****************************************************************************/
int
nstack_rd_ip_node_delete (rd_ip_data * data)
{
  int iindex = 0;
  rd_route_node *pnode = NULL;

  if (!g_rd_table_handle)
    {
      NSSOC_LOGERR ("nstack rd mng not inited");
      return -1;
    }

  for (iindex = 0; iindex < NSTACK_RD_DATA_MAX; iindex++)
    {
      pnode = &(g_rd_table_handle->node[iindex]);
      if ((RD_NODE_USING == pnode->flag)
          && (MASK_V (pnode->data.ipdata.addr, pnode->data.ipdata.masklen) ==
              MASK_V (data->addr, data->masklen)))
        {
          pnode->flag = RD_NODE_DELETING;       /*just set deleting state */
          pnode->agetime = 0;
          g_rd_table_handle->icnt--;
          NSSOC_LOGINF
            ("nstack rd delete:%s, addr:0x%x, masklen:%u index:%d was delete",
             pnode->data.stack_name, data->addr, data->masklen, iindex);
          return 0;
        }
    }
  NSSOC_LOGINF ("nstack rd delete, addr:0x%x, masklen:%u index was not found",
                data->addr, data->masklen);
  return 0;
}

/*****************************************************************************
*   Prototype    : nstack_rd_ip_get
*   Description  : get rd data from rd table
*   Input        : char *planename
*                  rd_route_data **data
*                  int *num
*   Output       : None
*   Return Value : int
*   Calls        :
*   Called By    :
*   Note         : a block memory was alloc and return by data,
*                  this need free by caller if return 0, otherwise no need
*****************************************************************************/
int
nstack_rd_ip_get (rd_route_data ** data, int *num)
{
  rd_route_data *pdata = NULL;
  rd_route_node *pnode = NULL;
  int size = 0;
  int icnt = 0;
  int idex = 0;
  int ret;

  if (!g_rd_table_handle || !data || !num)
    {
      NSSOC_LOGERR ("nstack rd mng not inited or input err");
      return -1;
    }
  size = sizeof (rd_route_data) * g_rd_table_handle->size;
  pdata = (rd_route_data *) malloc (size);
  if (!pdata)
    {
      NSSOC_LOGERR ("rd route data malloc fail");
      return -1;
    }
  ret = MEMSET_S (pdata, size, 0, size);
  if (EOK != ret)
    {
      NSSOC_LOGERR ("MEMSET_S failed]ret=%d", ret);
      free (pdata);
      return -1;
    }
  for (icnt = 0; icnt < g_rd_table_handle->size; icnt++)
    {
      pnode = &(g_rd_table_handle->node[icnt]);
      if (RD_NODE_USING == pnode->flag)
        {
          pdata[idex].type = pnode->data.type;
          pdata[idex].ipdata.addr = pnode->data.ipdata.addr;
          pdata[idex].ipdata.masklen = pnode->data.ipdata.masklen;
          pdata[idex].ipdata.resev[0] = pnode->data.ipdata.resev[0];
          pdata[idex].ipdata.resev[1] = pnode->data.ipdata.resev[1];
          ret =
            STRCPY_S (pdata[idex].stack_name, RD_PLANE_NAMELEN,
                      pnode->data.stack_name);
          if (EOK != ret)
            {
              NSSOC_LOGERR ("STRCPY_S failed]ret=%d", ret);
              free (pdata);
              return -1;
            }
          idex++;
        }
    }
  /*if no data fetched , just return fail */
  if (idex == 0)
    {
      free (pdata);
      return -1;
    }
  *data = pdata;
  *num = idex;
  return 0;
}