summaryrefslogtreecommitdiffstats
path: root/stacks/lwip_stack/release/lwip_helper_files/arch/sys_arch.c
blob: d7aadf7d0db03d4727093b8c0f9c2e74d5475620 (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
#include <sys/queue.h>

#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <stdint.h>

#include <pthread.h>
#include <sys/time.h>
#include <time.h>
#include <linux/sched.h>

#include <asm/unistd.h>
#include <linux/unistd.h>
#include <syscall.h>

#include <lwip/sys.h>
#include <cc.h>
#include <arch/sys_arch.h>
#include <arch/queue.h>
#include "nstack_log.h"
#include "lwip/sockets.h"
#include "spl_timers.h"
#include "nstack_securec.h"
#include "nstack_share_res.h"
#include "nstack_log.h"

#include "stackx_spl_share.h"
#include "lwip/api.h"
#include "lwip/ip.h"
#include <netif/sc_dpdk.h>
#include <netif/sharedmemory.h>

#include "common_mem_common.h"
#include "common_func.h"
#include "common_mem_api.h"

#include "mgr_com.h"
#ifdef HAL_LIB
#else
#include "rte_ring.h"
#endif

#include "timeouts.h"

sys_sem_st g_global_semaphore;

/** Check if an mbox is valid/allocated: return 1 for valid, 0 for invalid */
int
sys_mbox_valid (sys_mbox_t * mbox)
{
  return ((*mbox == SYS_MBOX_NULL) ? 0 : 1);
}

struct lwip_thread
{
  void (*func) (void *arg);
  void *arg;
};

static void *
lwip_thread_entry (void *arg)
{
  struct lwip_thread *lt = arg;

  lt->func (lt->arg);

  free (lt);
  return 0;
}

NSTACK_STATIC inline void
get_thread_policy (pthread_attr_t * attr)
{
  int policy;
  int rs = pthread_attr_getschedpolicy (attr, &policy);
  if (rs != 0)
    {
      NSFW_LOGERR ("pthread_attr_getschedpolicy failed");
      return;
    }
  switch (policy)
    {
    case SCHED_FIFO:
      NSFW_LOGINF ("policy= SCHED_FIFO");
      break;
    case SCHED_RR:
      NSFW_LOGINF ("policy= SCHED_RR");
      break;
    case SCHED_OTHER:
      NSFW_LOGINF ("policy=SCHED_OTHER");
      break;
    default:
      NSFW_LOGINF ("policy=UNKNOWN");
      break;
    }

  return;
}

NSTACK_STATIC inline void
get_thread_priority (pthread_attr_t * attr)
{
  struct sched_param param;
  int rs = pthread_attr_getschedparam (attr, &param);
  if (rs != 0)
    {
      NSFW_LOGERR ("pthread_attr_getschedparam failed");
      return;
    }

  NSFW_LOGINF ("get thread priority] pri=%d", param.sched_priority);
}

/* support thread priority configuration */
void
set_thread_attr (pthread_attr_t * pattr, int stacksize, int pri, int policy)
{
  struct sched_param param;
  (void) pthread_attr_init (pattr);

  if (stacksize > 0)
    {
      (void) pthread_attr_setstacksize (pattr, stacksize);
    }

  param.sched_priority = pri;
  if (SCHED_OTHER != policy)
    {
      (void) pthread_attr_setschedpolicy (pattr, policy);
      (void) pthread_attr_setschedparam (pattr, &param);
      (void) pthread_attr_setinheritsched (pattr, PTHREAD_EXPLICIT_SCHED);
    }
  get_thread_policy (pattr);
  get_thread_priority (pattr);
}

/** The only thread function:
 * Creates a new thread
 * @param name human-readable name for the thread (used for debugging purposes)
 * @param thread thread-function
 * @param arg parameter passed to 'thread'
 * @param stacksize stack size in bytes for the new thread (may be ignored by ports)
 * @param prio priority of the new thread (may be ignored by ports) */
sys_thread_t
sys_thread_new2 (const char *name, lwip_thread_fn thread, void *arg,
                 int stacksize, int prio, int policy)
{
  pthread_attr_t attr;
  set_thread_attr (&attr, stacksize, prio, policy);

  struct lwip_thread *lt = malloc (sizeof (*lt));
  if (lt == NULL)
    {
      NSPOL_LOGERR ("process abort:cannot allocate thread struct");
      abort ();
    }

  lt->func = thread;
  lt->arg = arg;

  pthread_t t;
  int r = pthread_create (&t, &attr, lwip_thread_entry, lt);

  if (r != 0)
    {
      NSPOL_LOGERR ("process abort:lwip:annot create]errno_string=%s",
                    strerror (r));
      abort ();
    }
  else
    {
      NSPOL_LOGINF (SC_DPDK_INFO, "]thread_name=%s.", name);
    }

  (void) pthread_setname_np (t, name);

  return t;
}

/** The only thread function:
 * Creates a new thread
 * @param name human-readable name for the thread (used for debugging purposes)
 * @param thread thread-function
 * @param arg parameter passed to 'thread'
 * @param stacksize stack size in bytes for the new thread (may be ignored by ports)
 * @param prio priority of the new thread (may be ignored by ports) */
sys_thread_t
sys_thread_new (const char *name, lwip_thread_fn thread, void *arg,
                int stacksize, int prio)
{
  pthread_attr_t attr;
  set_thread_attr (&attr, stacksize, prio, 0);

  struct lwip_thread *lt = malloc (sizeof (*lt));
  if (lt == NULL)
    {
      NSPOL_LOGERR ("process abort:cannot allocate thread struct");
      abort ();
    }

  lt->func = thread;
  lt->arg = arg;

  pthread_t t;
  int r = pthread_create (&t, &attr, lwip_thread_entry, lt);

  if (r != 0)
    {
      NSPOL_LOGERR ("process abort:lwip:annot create]errno_string=%s",
                    strerror (r));
      abort ();
    }
  else
    {
      NSPOL_LOGINF (SC_DPDK_INFO, "]thread_name=%s.", name);
    }

  (void) pthread_setname_np (t, name);

  return t;
}

void
stackx_global_lock (void)
{
  sys_arch_lock_with_pid (&g_global_semaphore);
}

void
stackx_global_unlock (void)
{
  sys_sem_s_signal (&g_global_semaphore);
}

void
sys_init (void)
{
  return;
}

void
sys_timeouts_mbox_fetch (sys_mbox_t * mbox, void **msg)
{
  return;
}

void
sys_timeout (u32_t msecs, sys_timeout_handler handler, void *arg)
{
  struct ptimer_node *tmo = arg;

  tmo = malloc (sizeof (struct ptimer_node));   /*lint !e586 */
  if (NULL == tmo)
    {
      NSPOL_LOGERR ("malloc ptimer node failed!");
      return;
    }

  int ret = memset_s (tmo, sizeof (struct ptimer_node), 0,
                      sizeof (struct ptimer_node));
  if (EOK != ret)
    {
      NSPOL_LOGERR ("MEMSET_S failed]ret=%d", ret);
      free (tmo);
      return;
    }

  NSPOL_LOGDBG (TIMERS_DEBUG, "alloc]ptimer_node=%p", tmo);

  tmo->info.msec = msecs;
  tmo->info._phandle = handler;
  tmo->info.ctx = arg;
  tmo->info.flags = PTIMER_ONESHOT;
  tmo->index = 0;
  regedit_ptimer (SYS_PTIMEROUT_MSG, handler, tmo);
}

void
sys_untimeout (sys_timeout_handler handler, void *arg)
{
  regedit_ptimer (SYS_UNPTIMEROUT_MSG, handler, arg);
  return;
}

/**
 * Timer callback function that calls mld6_tmr() and reschedules itself.
 *
 * @param arg unused argument
 */
void
cyclic_timer (void *arg)
{
  const struct lwip_cyclic_timer *cyclic =
    (const struct lwip_cyclic_timer *) arg;
#if LWIP_DEBUG_TIMERNAMES
  LWIP_DEBUGF (TIMERS_DEBUG, ("tcpip: %s()\n", cyclic->handler_name));
#endif
  cyclic->handler ();
  sys_timeout (cyclic->interval_ms, cyclic_timer, arg);
}