aboutsummaryrefslogtreecommitdiffstats
path: root/cicn-plugin/cicn/cicn.c
blob: 9beea5d5e61dc82bfd70fd2a9f35ad0012f60faf (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
/*
 * Copyright (c) 2017 Cisco and/or its affiliates.
 * 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.
 */
/*
 * cicn.c - skeleton vpp engine plug-in
 */

#include <vnet/vnet.h>
#include <vnet/plugin/plugin.h>

#include <cicn/cicn.h>
#include <cicn/cicn_api_handler.h>

static vlib_node_registration_t icn_process_node;

cicn_main_t cicn_main;
/* Module vars */
int cicn_infra_fwdr_initialized = 0;

cicn_face_db_t cicn_face_db;

/* Global forwarder name info */
cicn_infra_fwdr_name_t cicn_infra_fwdr_name;

/* Global generation value, updated for (some? all?) config changes */
cicn_infra_shard_t cicn_infra_gshard;

/* Fixed array for worker threads, to be indexed by worker index */
cicn_infra_shard_t cicn_infra_shards[CICN_INFRA_WORKERS_MAX];

/* Global time counters we're trying out for opportunistic hashtable
 * expiration.
 */
uint16_t cicn_infra_fast_timer;	/* Counts at 1 second intervals */
uint16_t cicn_infra_slow_timer;	/* Counts at 1 minute intervals */

/*
 * Initialize support for cicn_rc_e codes
 * - build hash table mapping codes to printable strings
 */
static void
cicn_rc_strings_init (void)
{
  cicn_main.cicn_rc_strings = hash_create (0, sizeof (uword));

#define _(n,v,s) hash_set (cicn_main.cicn_rc_strings, v, s);
  foreach_cicn_rc;
#undef _
}

/*
 * modify/return supplied vector with printable representation of crc,
 * which is string name if available, otherwise numeric value.
 */
const u8 *
cicn_rc_c_string (u8 * s, cicn_rc_e crc)
{
  uword *p;

  p = hash_get (cicn_main.cicn_rc_strings, crc);

  if (p)
    {
      s = format (s, "%s", p[0]);
    }
  else
    {
      s = format (s, "%d", crc);
    }
  return (s);
}

/*
 * Return printable representation of crc.
 */
const char *
cicn_rc_str (cicn_rc_e crc)
{
  char *crc_str;

  uword *p;

  p = hash_get (cicn_main.cicn_rc_strings, crc);

  if (p)
    {
      crc_str = (char *) p[0];
    }
  else
    {
      crc_str = "unknown";
    }
  return (crc_str);
}

/*
 * Return printable representation of cicn_rd.
 * - if cicn_rc is set to an error, use that code for string
 * - otherwise use ux_rc
 */
const char *
cicn_rd_str (cicn_rd_t * cicn_rd)
{
  const char *str;
  if (cicn_rd->rd_cicn_rc != CICN_RC_OK)
    {
      str = cicn_rc_str (cicn_rd->rd_cicn_rc);
    }
  else
    {
      str = strerror (cicn_rd->rd_ux_rc);
    }
  return (str);
}

/*
 * Init CICN forwarder with configurable FIB, PIT, CS sizes
 */
static int
cicn_infra_fwdr_init (uint32_t fib_size, uint32_t shard_pit_size,
		      uint32_t shard_cs_size)
{
  int ret = 0;

  if (cicn_infra_fwdr_initialized)
    {
      cicn_cli_output ("cicn: already enabled");
      goto done;
    }

  cicn_rc_strings_init ();

  /* Initialize the forwarder's name structure */
  cicn_sstrncpy (cicn_infra_fwdr_name.fn_str, "no-name",
		 sizeof (cicn_infra_fwdr_name.fn_str));
  cicn_infra_fwdr_name.fn_reply_payload_flen = 0;

  /* Init per worker limits */
  cicn_infra_shard_pit_size = shard_pit_size;
  cicn_infra_shard_cs_size = shard_cs_size;

  /* Init face cache */
  cicn_face_db.entry_count = 0;

  /* Init event subscribers' info */
  cicn_main.n_face_event_subscribers = 0;

  /* Init the config generation number values */
  cicn_infra_gshard.cfg_generation = 1LL;
  memset (cicn_infra_shards, 0, sizeof (cicn_infra_shards));

  /* Init the global time-compression counters */
  cicn_infra_fast_timer = 1;
  cicn_infra_slow_timer = 1;

  /* Init global FIB */
  ret = cicn_fib_create (&(cicn_main.fib), fib_size);

done:

  cicn_cli_output ("cicn: fwdr initialize => %d", ret);

  if ((ret == AOK) && !cicn_infra_fwdr_initialized)
    {
      cicn_infra_fwdr_initialized = 1;
    }

  return (ret);
}

/*
 * Action function shared between message handler and debug CLI
 * NOTICE: we're only 'enabling' now
 */
int
cicn_infra_plugin_enable_disable (int enable_disable,
				  int fib_size_req,
				  int pit_size_req,
				  f64 pit_dflt_lifetime_sec_req,
				  f64 pit_min_lifetime_sec_req,
				  f64 pit_max_lifetime_sec_req,
				  int cs_size_req)
{
  int ret = 0;

  cicn_main_t *sm = &cicn_main;
  vlib_thread_main_t *tm = vlib_get_thread_main ();
  vlib_thread_registration_t *tr;
  uword *p;
  uint32_t fib_size, pit_size, cs_size;

  /* Notice if we're already enabled... */
  if (sm->is_enabled)
    {
      vlib_cli_output (sm->vlib_main, "cicn: already enabled");
      ret = 0;
      goto done;
    }

  /* Figure out how many workers will be running */
  p = hash_get_mem (tm->thread_registrations_by_name, "workers");
  tr = (vlib_thread_registration_t *) p[0];
  if (tr)
    {
      sm->worker_count = tr->count;
      sm->worker_first_index = tr->first_index;
      vlib_cli_output (sm->vlib_main,
		       "cicn: worker count %u, first idx %u",
		       sm->worker_count, sm->worker_first_index);
    }
  else
    {
      sm->worker_count = 0;
      sm->worker_first_index = 0;

      vlib_cli_output (sm->vlib_main, "cicn: no worker threads");
    }
  sm->shard_count = (sm->worker_count == 0) ? 1 : sm->worker_count;

  /* Set up params and call fwdr_init set up FIB, PIT/CS, forwarder nodes */

  /* Check the range and assign some globals */
  if (pit_min_lifetime_sec_req < 0)
    {
      sm->pit_lifetime_min_ms = CICN_PARAM_PIT_LIFETIME_DFLT_MIN_MS;
    }
  else
    {
      if (pit_min_lifetime_sec_req < CICN_PARAM_PIT_LIFETIME_BOUND_MIN_SEC ||
	  pit_min_lifetime_sec_req > CICN_PARAM_PIT_LIFETIME_BOUND_MAX_SEC)
	{
	  ret = EINVAL;
	  goto done;
	}
      sm->pit_lifetime_min_ms = pit_min_lifetime_sec_req * SEC_MS;
    }

  if (pit_max_lifetime_sec_req < 0)
    {
      sm->pit_lifetime_max_ms = CICN_PARAM_PIT_LIFETIME_DFLT_MAX_MS;
    }
  else
    {
      if (pit_max_lifetime_sec_req < CICN_PARAM_PIT_LIFETIME_BOUND_MIN_SEC ||
	  pit_max_lifetime_sec_req > CICN_PARAM_PIT_LIFETIME_BOUND_MAX_SEC)
	{
	  ret = EINVAL;
	  goto done;
	}
      sm->pit_lifetime_max_ms = pit_max_lifetime_sec_req * SEC_MS;
    }
  if (sm->pit_lifetime_min_ms > sm->pit_lifetime_max_ms)
    {
      ret = EINVAL;
      goto done;
    }

  if (pit_dflt_lifetime_sec_req < 0)
    {
      sm->pit_lifetime_dflt_ms = CICN_PARAM_PIT_LIFETIME_DFLT_DFLT_MS;
    }
  else
    {
      sm->pit_lifetime_dflt_ms = pit_dflt_lifetime_sec_req * SEC_MS;
    }
  if (sm->pit_lifetime_dflt_ms < sm->pit_lifetime_min_ms ||
      sm->pit_lifetime_dflt_ms > sm->pit_lifetime_max_ms)
    {
      goto done;
    }

  if (fib_size_req < 0)
    {
      fib_size = CICN_PARAM_FIB_ENTRIES_DFLT;
    }
  else
    {
      if (fib_size_req < CICN_PARAM_FIB_ENTRIES_MIN ||
	  fib_size_req > CICN_PARAM_FIB_ENTRIES_MAX)
	{
	  ret = EINVAL;
	  goto done;
	}
      fib_size = (uint32_t) fib_size_req;
    }

  if (pit_size_req < 0)
    {
      pit_size = CICN_PARAM_PIT_ENTRIES_DFLT;
    }
  else
    {
      if (pit_size_req < CICN_PARAM_PIT_ENTRIES_MIN ||
	  pit_size_req > CICN_PARAM_PIT_ENTRIES_MAX)
	{
	  ret = EINVAL;
	  goto done;
	}
      pit_size = (uint32_t) pit_size_req;
    }

  if (cs_size_req < 0)
    {
      cs_size = CICN_PARAM_CS_ENTRIES_DFLT;
    }
  else
    {
      if (cs_size_req > CICN_PARAM_CS_ENTRIES_MAX)
	{
	  ret = EINVAL;
	  goto done;
	}
      cs_size = (uint32_t) cs_size_req;
    }

  pit_size = pit_size / sm->shard_count;
  cs_size = cs_size / sm->shard_count;

  ret = cicn_infra_fwdr_init (fib_size, pit_size, cs_size);
  if (ret != 0)
    {
      vlib_cli_output (sm->vlib_main,
		       "cicn: enable_disable failed => %d", ret);
      goto done;
    }

#if CICN_FEATURE_MULTITHREAD
  /* If we're not running main-thread only, set up a relationship between
   * the dpdk worker handoff node and our forwarding node.
   */
  if (sm->worker_count > 1)
    {
      /* Engage with the worker thread handoff node so that we can dispatch
       * through it from our dist node directly to our forwarder node
       */
      sm->fwd_next_node = vlib_node_add_next (sm->vlib_main,
					      handoff_dispatch_node.index,
					      icnfwd_node.index);
      vlib_cli_output (sm->vlib_main,
		       "cicn: handoff node %u, fwd node next idx %u",
		       handoff_dispatch_node.index, sm->fwd_next_node);
    }
#endif //CICN_FEATURE_MULTITHREAD

  ret = cicn_hello_plugin_activation_init (sm->vlib_main);	//start adj protocol
  if (ret != AOK)
    {
      goto done;
    }

  sm->is_enabled = 1;

done:

  return (ret);
}

/*
 * The entry-point for the ICN background process, which does...
 * background things, like garbage collection, for us.
 */
static uword
icn_process_fn (vlib_main_t * vm, vlib_node_runtime_t * rt, vlib_frame_t * f)
{
#define CICN_PROCESS_WAIT_TIME  1.0	/* 1 second */

  f64 timeout = CICN_PROCESS_WAIT_TIME;
  f64 tnow, tnext = 0.0;
  uword event_type;
  uword *event_data = 0;
  int timer_counter = 0;

  while (1)
    {
      vlib_process_wait_for_event_or_clock (vm, timeout);

      event_type = vlib_process_get_events (vm, &event_data);

      tnow = vlib_time_now (cicn_main.vlib_main);
      if (tnext == 0.0)
	{
	  tnext = tnow + CICN_INFRA_FAST_TIMER_SECS;
	}

      /* Update the timeout compression counters we're trying for
       * opportunistic timeouts in the hashtables.
       */
      if (tnow >= tnext)
	{
	  cicn_infra_fast_timer =
	    cicn_infra_seq16_sum (cicn_infra_fast_timer, 1);

	  if ((++timer_counter % CICN_INFRA_SLOW_TIMER_SECS) == 0)
	    {
	      cicn_infra_slow_timer =
		cicn_infra_seq16_sum (cicn_infra_slow_timer, 1);
	      timer_counter = 0;
	    }

	  tnext = tnow + CICN_INFRA_FAST_TIMER_SECS;
	}

      switch (event_type)
	{
	case ~0:
	default:
	  /* Reset timeout */
	  timeout = CICN_PROCESS_WAIT_TIME;
	  break;

	}			/* End switch() */

      vec_reset_length (event_data);
    }

  /* NOTREACHED */
  return 0;
}

VLIB_REGISTER_NODE (icn_process_node, static) =
{
.function = icn_process_fn,.type = VLIB_NODE_TYPE_PROCESS,.name =
    "icn-process",.process_log2_n_stack_bytes = 16};

/*
 * Init entry-point for the icn plugin
 */
static clib_error_t *
cicn_init (vlib_main_t * vm)
{
  clib_error_t *error = 0;

  cicn_main_t *sm = &cicn_main;

  sm->vlib_main = vm;
  sm->vnet_main = vnet_get_main ();

  /* Init other elements in the 'main' struct */
  sm->is_enabled = 0;
  sm->fwd_next_node = ~0;

  sm->pgen_enabled = 0;
  sm->pgen_clt_src_addr = sm->pgen_clt_dest_addr = 0;
  sm->pgen_clt_src_port = sm->pgen_clt_dest_port = 0;

  sm->pgen_svr_enabled = 0;

  error = cicn_api_plugin_hookup (vm);

  return error;
}

VLIB_INIT_FUNCTION (cicn_init);

/* *INDENT-OFF* */
VLIB_PLUGIN_REGISTER () = {
};
/* *INDENT-ON* */