summaryrefslogtreecommitdiffstats
path: root/hicn-plugin/src/hicn_api.c
blob: 85279eb0b725cc52fbbd1d714865ca2002311fef (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
/*
 * Copyright (c) 2021 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.
 */

#include <vlib/vlib.h>
#include <vnet/vnet.h>
#include <vppinfra/error.h>
#include <vnet/ip/format.h>
#include <vnet/ip/ip4.h>
#include <vnet/ip/ip6.h>
#include <vlibapi/api.h>
#include <vlibmemory/api.h>
#include <vnet/dpo/load_balance.h>
#include <vnet/ip/ip_types_api.h>
#include <vnet/ip/ip_format_fns.h>

#include "faces/face.h"
#include "udp_tunnels/udp_tunnel.h"
#include "infra.h"
#include "parser.h"
#include "mgmt.h"
#include "strategy_dpo_manager.h"
#include "strategy_dpo_ctx.h"
#include "strategy.h"
#include "pg.h"
#include "error.h"
#include "faces/app/face_prod.h"
#include "faces/app/face_cons.h"
#include "route.h"

/* define message IDs */
#include <vpp_plugins/hicn/hicn.api_enum.h>
#include <vpp_plugins/hicn/hicn.api_types.h>

/* define generated endian-swappers */
#define vl_endianfun
#include <vpp_plugins/hicn/hicn_all_api_h.h>
#undef vl_endianfun

#define REPLY_MSG_ID_BASE sm->msg_id_base
#include <vlibapi/api_helper_macros.h>

/****** SUPPORTING FUNCTION DECLARATIONS ******/

/*
 * Convert a unix return code to a vnet_api return code. Currently stubby:
 * should have more cases.
 */
always_inline vnet_api_error_t hicn_face_api_entry_params_serialize (
  hicn_face_id_t faceid, vl_api_hicn_api_face_params_get_reply_t *reply);

/****************** API MESSAGE HANDLERS ******************/

/****** NODE ******/

static void
vl_api_hicn_api_node_params_set_t_handler (
  vl_api_hicn_api_node_params_set_t *mp)
{
  vl_api_hicn_api_node_params_set_reply_t *rmp;
  int rv;

  hicn_main_t *sm = &hicn_main;

  int pit_max_size = clib_net_to_host_i32 (mp->pit_max_size);
  pit_max_size =
    pit_max_size == -1 ? HICN_PARAM_PIT_ENTRIES_DFLT : pit_max_size;

  f64 pit_max_lifetime_sec = mp->pit_max_lifetime_sec;
  pit_max_lifetime_sec = pit_max_lifetime_sec == -1 ?
				 HICN_PARAM_PIT_LIFETIME_DFLT_MAX_MS / SEC_MS :
				 pit_max_lifetime_sec;

  int cs_max_size = clib_net_to_host_i32 (mp->cs_max_size);
  cs_max_size = cs_max_size == -1 ? HICN_PARAM_CS_ENTRIES_DFLT : cs_max_size;

  rv =
    hicn_infra_plugin_enable_disable ((int) (mp->enable_disable), pit_max_size,
				      pit_max_lifetime_sec, cs_max_size, ~0);

  REPLY_MACRO (VL_API_HICN_API_NODE_PARAMS_SET_REPLY /* , rmp, mp, rv */);
}

static void
vl_api_hicn_api_node_params_get_t_handler (
  vl_api_hicn_api_node_params_get_t *mp)
{
  vl_api_hicn_api_node_params_get_reply_t *rmp;
  int rv = HICN_ERROR_NONE;

  hicn_main_t *sm = &hicn_main;

  REPLY_MACRO2 (VL_API_HICN_API_NODE_PARAMS_GET_REPLY, ({
		  rmp->is_enabled = sm->is_enabled;
		  rmp->feature_cs = HICN_FEATURE_CS;
		  rmp->pit_max_size =
		    clib_host_to_net_u32 (hicn_infra_pit_size);
		  rmp->pit_max_lifetime_sec =
		    ((f64) sm->pit_lifetime_max_ms) / SEC_MS;
		  rmp->cs_max_size = clib_host_to_net_u32 (hicn_infra_cs_size);
		  rmp->retval = clib_host_to_net_i32 (rv);
		}));
}

static void
vl_api_hicn_api_node_stats_get_t_handler (vl_api_hicn_api_node_stats_get_t *mp)
{
  vl_api_hicn_api_node_stats_get_reply_t *rmp;
  int rv = HICN_ERROR_NONE;

  hicn_main_t *sm = &hicn_main;

  REPLY_MACRO2 (VL_API_HICN_API_NODE_STATS_GET_REPLY, ({
		  rv = hicn_mgmt_node_stats_get (rmp);
		  rmp->retval = clib_host_to_net_i32 (rv);
		}));
}

static void
vl_api_hicn_api_face_params_get_t_handler (
  vl_api_hicn_api_face_params_get_t *mp)
{
  vl_api_hicn_api_face_params_get_reply_t *rmp;
  int rv = 0;

  hicn_main_t *sm = &hicn_main;

  hicn_face_id_t faceid = clib_net_to_host_u32 (mp->faceid);

  REPLY_MACRO2 (VL_API_HICN_API_FACE_PARAMS_GET_REPLY, ({
		  rv = hicn_face_api_entry_params_serialize (faceid, rmp);
		  rmp->retval = clib_host_to_net_u32 (rv);
		}));
}

static void
send_face_details (hicn_face_t *face, vl_api_hicn_face_t *mp)
{
  vnet_main_t *vnm = vnet_get_main ();

  ip_address_encode (&face->nat_addr, IP46_TYPE_ANY, &mp->nat_addr);
  mp->flags = clib_host_to_net_u32 (face->flags);
  mp->swif = clib_net_to_host_u32 (face->sw_if);
  vnet_sw_interface_t *sw_interface =
    vnet_get_sw_interface_or_null (vnm, face->sw_if);
  u8 *sbuf = 0;
  if (sw_interface != NULL)
    {
      sbuf =
	format (0, "%U", format_vnet_sw_interface_name, vnm, sw_interface);
      strcpy_s ((char *) (mp->if_name), sizeof (mp->if_name), (char *) sbuf);
    }
}

static void
send_faces_details (vl_api_registration_t *reg, hicn_face_t *face, u32 context)
{
  vl_api_hicn_api_faces_details_t *mp;
  hicn_main_t *hm = &hicn_main;
  mp = vl_msg_api_alloc (sizeof (*mp));
  memset (mp, 0, sizeof (*mp));
  mp->faceid = clib_host_to_net_u32 (hicn_dpoi_get_index (face));
  mp->_vl_msg_id = htons (VL_API_HICN_API_FACES_DETAILS + hm->msg_id_base);
  mp->context = context;

  send_face_details (face, &(mp->face));

  vl_api_send_msg (reg, (u8 *) mp);
}

static void
vl_api_hicn_api_faces_dump_t_handler (vl_api_hicn_api_faces_dump_t *mp)
{
  hicn_face_t *face;
  vl_api_registration_t *reg;

  reg = vl_api_client_index_to_registration (mp->client_index);
  if (!reg)
    return;

  pool_foreach (face, hicn_dpoi_face_pool)
    {
      send_faces_details (reg, face, mp->context);
    }
}

static void
vl_api_hicn_api_face_get_t_handler (vl_api_hicn_api_face_get_t *mp)
{
  vl_api_hicn_api_face_get_reply_t *rmp;
  int rv = 0;

  hicn_main_t *sm = &hicn_main;

  hicn_face_id_t faceid = clib_net_to_host_u32 (mp->faceid);

  REPLY_MACRO2 (VL_API_HICN_API_FACE_GET_REPLY, ({
		  rv = hicn_dpoi_idx_is_valid (faceid);
		  if (rv)
		    {
		      hicn_face_t *face = hicn_dpoi_get_from_idx (faceid);
		      send_face_details (face, &(rmp->face));
		      rv = HICN_ERROR_NONE;
		    }
		  else
		    {
		      rv = HICN_ERROR_FACE_NOT_FOUND;
		    }
		  rmp->retval = clib_host_to_net_u32 (rv);
		}));
}

static void
send_face_stats_details (vl_api_registration_t *reg, hicn_face_t *face,
			 u32 context)
{
  vl_api_hicn_api_face_stats_details_t *mp;
  hicn_main_t *hm = &hicn_main;
  mp = vl_msg_api_alloc (sizeof (*mp));
  memset (mp, 0, sizeof (*mp));

  mp->_vl_msg_id =
    htons (VL_API_HICN_API_FACE_STATS_DETAILS + hm->msg_id_base);
  mp->context = context;

  mp->faceid = htonl (hicn_dpoi_get_index (face));
  vlib_counter_t v;
  vlib_get_combined_counter (
    &counters[hicn_dpoi_get_index (face) * HICN_N_COUNTER],
    HICN_FACE_COUNTERS_INTEREST_RX, &v);
  mp->irx_packets = clib_net_to_host_u64 (v.packets);
  mp->irx_bytes = clib_net_to_host_u64 (v.bytes);

  vlib_get_combined_counter (
    &counters[hicn_dpoi_get_index (face) * HICN_N_COUNTER],
    HICN_FACE_COUNTERS_INTEREST_TX, &v);
  mp->itx_packets = clib_net_to_host_u64 (v.packets);
  mp->itx_bytes = clib_net_to_host_u64 (v.bytes);

  vlib_get_combined_counter (
    &counters[hicn_dpoi_get_index (face) * HICN_N_COUNTER],
    HICN_FACE_COUNTERS_DATA_RX, &v);
  mp->drx_packets = clib_net_to_host_u64 (v.packets);
  mp->drx_bytes = clib_net_to_host_u64 (v.bytes);

  vlib_get_combined_counter (
    &counters[hicn_dpoi_get_index (face) * HICN_N_COUNTER],
    HICN_FACE_COUNTERS_DATA_TX, &v);
  mp->dtx_packets = clib_net_to_host_u64 (v.packets);
  mp->dtx_bytes = clib_net_to_host_u64 (v.bytes);

  vl_api_send_msg (reg, (u8 *) mp);
}

static void
vl_api_hicn_api_face_stats_dump_t_handler (
  vl_api_hicn_api_face_stats_dump_t *mp)
{
  hicn_face_t *face;
  vl_api_registration_t *reg;

  reg = vl_api_client_index_to_registration (mp->client_index);
  if (!reg)
    return;

  pool_foreach (face, hicn_dpoi_face_pool)
    {
      send_face_stats_details (reg, face, mp->context);
    }
}

/****** ROUTE *******/

static void
vl_api_hicn_api_route_get_t_handler (vl_api_hicn_api_route_get_t *mp)
{
  vl_api_hicn_api_route_get_reply_t *rmp;
  int rv = HICN_ERROR_NONE;

  hicn_main_t *sm = &hicn_main;

  fib_prefix_t prefix;
  ip_prefix_decode (&mp->prefix, &prefix);
  const dpo_id_t *hicn_dpo_id;
  hicn_dpo_ctx_t *hicn_dpo_ctx;
  u32 fib_index;

  rv = hicn_route_get_dpo (&prefix, &hicn_dpo_id, &fib_index);

  REPLY_MACRO2 (
    VL_API_HICN_API_ROUTE_GET_REPLY, ({
      if (rv == HICN_ERROR_NONE)
	{
	  hicn_dpo_ctx = hicn_strategy_dpo_ctx_get (hicn_dpo_id->dpoi_index);
	  for (int i = 0;
	       hicn_dpo_ctx != NULL && i < hicn_dpo_ctx->entry_count; i++)
	    {
	      rmp->faceids[i] = hicn_dpo_ctx->next_hops[i];
	    }
	  rmp->strategy_id =
	    clib_host_to_net_u32 (hicn_dpo_get_vft_id (hicn_dpo_id));
	}
    }));
}

static void
send_route_details (vl_api_registration_t *reg, const fib_prefix_t *pfx,
		    u32 context)
{
  vl_api_hicn_api_routes_details_t *mp;
  hicn_main_t *hm = &hicn_main;
  mp = vl_msg_api_alloc (sizeof (*mp));
  memset (mp, 0, sizeof (*mp));

  mp->_vl_msg_id = htons (VL_API_HICN_API_ROUTES_DETAILS + hm->msg_id_base);
  mp->context = context;

  ip_prefix_encode (pfx, &mp->prefix);
  mp->nfaces = 0;

  const dpo_id_t *hicn_dpo_id;
  hicn_dpo_ctx_t *hicn_dpo_ctx;
  u32 fib_index;

  int rv = hicn_route_get_dpo (pfx, &hicn_dpo_id, &fib_index);

  if (rv == HICN_ERROR_NONE)
    {
      hicn_dpo_ctx = hicn_strategy_dpo_ctx_get (hicn_dpo_id->dpoi_index);
      for (int i = 0; hicn_dpo_ctx != NULL && i < hicn_dpo_ctx->entry_count;
	   i++)
	{
	  mp->faceids[i] = clib_host_to_net_u32 (hicn_dpo_ctx->next_hops[i]);
	  mp->nfaces++;
	}
      mp->strategy_id =
	clib_host_to_net_u32 (hicn_dpo_get_vft_id (hicn_dpo_id));
    }

  vl_api_send_msg (reg, (u8 *) mp);
}

typedef struct vl_api_hicn_api_route_dump_walk_ctx_t_
{
  fib_node_index_t *feis;
} vl_api_hicn_api_route_dump_walk_ctx_t;

static fib_table_walk_rc_t
vl_api_hicn_api_route_dump_walk (fib_node_index_t fei, void *arg)
{
  vl_api_hicn_api_route_dump_walk_ctx_t *ctx = arg;
  int found = 0;
  const dpo_id_t *former_dpo_id;

  /* Route already existing. We need to update the dpo. */
  const dpo_id_t *load_balance_dpo_id =
    fib_entry_contribute_ip_forwarding (fei);

  /* The dpo is not a load balance dpo as expected */
  if (load_balance_dpo_id->dpoi_type == DPO_LOAD_BALANCE)
    {
      /* former_dpo_id is a load_balance dpo */
      load_balance_t *lb = load_balance_get (load_balance_dpo_id->dpoi_index);

      /* FIB entry exists but there is no hicn dpo. */
      for (int i = 0; i < lb->lb_n_buckets && !found; i++)
	{
	  former_dpo_id = load_balance_get_bucket_i (lb, i);

	  if (dpo_is_hicn (former_dpo_id))
	    {
	      vec_add1 (ctx->feis, fei);
	    }
	}
    }

  return (FIB_TABLE_WALK_CONTINUE);
}

static void
vl_api_hicn_api_routes_dump_t_handler (vl_api_hicn_api_routes_dump_t *mp)
{
  vl_api_registration_t *reg;
  fib_table_t *fib_table;
  ip4_main_t *im = &ip4_main;
  ip6_main_t *im6 = &ip6_main;
  fib_node_index_t *lfeip;
  const fib_prefix_t *pfx;
  vl_api_hicn_api_route_dump_walk_ctx_t ctx = {
    .feis = NULL,
  };

  reg = vl_api_client_index_to_registration (mp->client_index);
  if (!reg)
    return;

  pool_foreach (fib_table, im->fibs)
    {
      fib_table_walk (fib_table->ft_index, FIB_PROTOCOL_IP4,
		      vl_api_hicn_api_route_dump_walk, &ctx);
    }

  pool_foreach (fib_table, im6->fibs)
    {
      fib_table_walk (fib_table->ft_index, FIB_PROTOCOL_IP6,
		      vl_api_hicn_api_route_dump_walk, &ctx);
    }

  vec_foreach (lfeip, ctx.feis)
    {
      pfx = fib_entry_get_prefix (*lfeip);
      send_route_details (reg, pfx, mp->context);
    }

  vec_free (ctx.feis);
}

static void
vl_api_hicn_api_strategies_get_t_handler (vl_api_hicn_api_strategies_get_t *mp)
{
  vl_api_hicn_api_strategies_get_reply_t *rmp;
  int rv = HICN_ERROR_NONE;

  hicn_main_t *sm = &hicn_main;

  int n_strategies = hicn_strategy_get_all_available ();

  REPLY_MACRO2 (VL_API_HICN_API_STRATEGIES_GET_REPLY /* , rmp, mp, rv */, ({
		  int j = 0;
		  for (u32 i = 0; i < (u32) n_strategies; i++)
		    {
		      if (hicn_dpo_strategy_id_is_valid (i) == HICN_ERROR_NONE)
			{
			  rmp->strategy_id[j] = clib_host_to_net_u32 (i);
			  j++;
			}
		    }
		  rmp->n_strategies = n_strategies;
		}));
}

static void
vl_api_hicn_api_strategy_get_t_handler (vl_api_hicn_api_strategy_get_t *mp)
{
  vl_api_hicn_api_strategy_get_reply_t *rmp;
  int rv = HICN_ERROR_NONE;

  hicn_main_t *sm = &hicn_main;

  u32 strategy_id = clib_net_to_host_u32 (mp->strategy_id);
  rv = hicn_dpo_strategy_id_is_valid (strategy_id);

  REPLY_MACRO2 (VL_API_HICN_API_STRATEGY_GET_REPLY /* , rmp, mp, rv */, ({
		  if (rv == HICN_ERROR_NONE)
		    {
		      const hicn_strategy_vft_t *hicn_strategy_vft =
			hicn_dpo_get_strategy_vft (strategy_id);
		      hicn_strategy_vft->hicn_format_strategy (
			rmp->description, 0);
		    }
		}));
}

/************* APP FACE ****************/

static void
vl_api_hicn_api_register_prod_app_t_handler (
  vl_api_hicn_api_register_prod_app_t *mp)
{
  vl_api_hicn_api_register_prod_app_reply_t *rmp;
  int rv = HICN_ERROR_NONE;

  hicn_main_t *sm = &hicn_main;

  fib_prefix_t prefix;
  ip_prefix_decode (&mp->prefix, &prefix);
  u32 swif = clib_net_to_host_u32 (mp->swif);
  u32 cs_reserved = clib_net_to_host_u32 (mp->cs_reserved);
  u32 faceid;

  ip46_address_t prod_addr;
  ip46_address_reset (&prod_addr);
  rv = hicn_face_prod_add (&prefix, swif, &cs_reserved, &prod_addr, &faceid);

  REPLY_MACRO2 (VL_API_HICN_API_REGISTER_PROD_APP_REPLY, ({
		  ip_address_encode (&prod_addr, IP46_TYPE_ANY,
				     &rmp->prod_addr);
		  rmp->cs_reserved = clib_net_to_host_u32 (cs_reserved);
		  rmp->faceid = clib_net_to_host_u32 (faceid);
		}));
}

static void
vl_api_hicn_api_face_prod_del_t_handler (vl_api_hicn_api_face_prod_del_t *mp)
{
  vl_api_hicn_api_face_prod_del_reply_t *rmp;
  int rv = HICN_ERROR_FACE_NOT_FOUND;

  hicn_main_t *sm = &hicn_main;

  hicn_face_id_t faceid = clib_net_to_host_u32 (mp->faceid);
  rv = hicn_face_prod_del (faceid);

  REPLY_MACRO (VL_API_HICN_API_FACE_PROD_DEL_REPLY /* , rmp, mp, rv */);
}

static void
vl_api_hicn_api_register_cons_app_t_handler (
  vl_api_hicn_api_register_cons_app_t *mp)
{
  vl_api_hicn_api_register_cons_app_reply_t *rmp;
  int rv = HICN_ERROR_NONE;

  hicn_main_t *sm = &hicn_main;
  ip46_address_t src_addr4 = ip46_address_initializer;
  ip46_address_t src_addr6 = ip46_address_initializer;

  u32 swif = clib_net_to_host_u32 (mp->swif);
  u32 faceid1;
  u32 faceid2;

  rv = hicn_face_cons_add (&src_addr4.ip4, &src_addr6.ip6, swif, &faceid1,
			   &faceid2);

  REPLY_MACRO2 (
    VL_API_HICN_API_REGISTER_CONS_APP_REPLY, ({
      ip_address_encode (&src_addr4, IP46_TYPE_ANY, &rmp->src_addr4);
      ip_address_encode (&src_addr6, IP46_TYPE_ANY, &rmp->src_addr6);
      rmp->faceid1 = clib_net_to_host_u32 (faceid1);
      rmp->faceid2 = clib_net_to_host_u32 (faceid2);
    }));
}

static void
vl_api_hicn_api_face_cons_del_t_handler (vl_api_hicn_api_face_cons_del_t *mp)
{
  vl_api_hicn_api_face_cons_del_reply_t *rmp;
  int rv = HICN_ERROR_FACE_NOT_FOUND;

  hicn_main_t *sm = &hicn_main;

  hicn_face_id_t faceid = clib_net_to_host_u32 (mp->faceid);
  rv = hicn_face_cons_del (faceid);

  REPLY_MACRO (VL_API_HICN_API_FACE_CONS_DEL_REPLY /* , rmp, mp, rv */);
}

static void
vl_api_hicn_api_enable_disable_t_handler (vl_api_hicn_api_enable_disable_t *mp)
{
  vl_api_hicn_api_enable_disable_reply_t *rmp;
  int rv = HICN_ERROR_NONE;

  hicn_main_t *sm = &hicn_main;

  fib_prefix_t prefix;
  ip_prefix_decode (&mp->prefix, &prefix);

  hicn_face_id_t *vec_faces = NULL;

  switch (clib_net_to_host_u32 (mp->enable_disable))
    {
    case HICN_ENABLE:
      HICN_DEBUG ("Calling hicn enable from API.");
      rv = hicn_route_enable (&prefix, &vec_faces);
      break;
    case HICN_DISABLE:
      HICN_DEBUG ("Calling hicn disable from API.");
      rv = hicn_route_disable (&prefix);
      break;
    }

  REPLY_MACRO2 (VL_API_HICN_API_ENABLE_DISABLE_REPLY, ({
		  rmp->nfaces = 0;
		  if (vec_faces != NULL)
		    {
		      hicn_face_id_t *face;
		      vec_foreach (face, vec_faces)
			{
			  rmp->faceids[rmp->nfaces++] =
			    clib_host_to_net_u32 (*face);
			}

		      vec_free (vec_faces);
		    }
		}));
}

/*********************************** UDP TUNNELS
 * ************************************/

static void
vl_api_hicn_api_udp_tunnel_add_del_t_handler (
  vl_api_hicn_api_udp_tunnel_add_del_t *mp)
{
  vl_api_hicn_api_udp_tunnel_add_del_reply_t *rmp;
  int rv = HICN_ERROR_NONE;

  hicn_main_t *sm = &hicn_main;

  ip46_address_t src_addr;
  ip46_address_t dst_addr;
  u16 src_port;
  u16 dst_port;
  index_t uei = ~0;

  ip46_type_t type = ip_address_decode (&mp->src_addr, &src_addr);
  if (type != ip_address_decode (&mp->dst_addr, &dst_addr))
    {
      rv = HICN_ERROR_UDP_TUNNEL_SRC_DST_TYPE;
      goto done;
    }

  src_port = clib_net_to_host_u16 (mp->src_port);
  dst_port = clib_net_to_host_u16 (mp->dst_port);

  fib_protocol_t proto =
    ip46_address_is_ip4 (&src_addr) ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6;

  index_t fib_index = fib_table_find (proto, HICN_FIB_TABLE);

  if (mp->is_add)
    {
      uei = udp_tunnel_add (proto, fib_index, &src_addr, &dst_addr, src_port,
			    dst_port, UDP_ENCAP_FIXUP_NONE);
    }
  else
    {
      udp_tunnel_del (proto, fib_index, &src_addr, &dst_addr, src_port,
		      dst_port, UDP_ENCAP_FIXUP_NONE);
    }

done:

  REPLY_MACRO2 (VL_API_HICN_API_UDP_TUNNEL_ADD_DEL_REPLY,
		({ rmp->uei = clib_host_to_net_u32 (uei); }));
}

/************************************************************************************/

#include <vpp_plugins/hicn/hicn.api.c>

/* Set up the API message handling tables */
clib_error_t *
hicn_api_plugin_hookup (vlib_main_t *vm)
{
  hicn_main_t *hm = &hicn_main;

  hm->msg_id_base = setup_message_id_table ();
  return 0;
}

/******************* SUPPORTING FUNCTIONS  *******************/

/*
 * Binary serialization for get face configuration API. for the moment
 * assuming only ip faces here. To be completed with othet types of faces
 */
vnet_api_error_t
hicn_face_api_entry_params_serialize (
  hicn_face_id_t faceid, vl_api_hicn_api_face_params_get_reply_t *reply)
{
  int rv = HICN_ERROR_NONE;

  if (!reply)
    {
      rv = VNET_API_ERROR_INVALID_ARGUMENT;
      goto done;
    }
  hicn_face_t *face = hicn_dpoi_get_from_idx (faceid);

  if (face != NULL)
    {
      ip_address_encode (&face->nat_addr, IP46_TYPE_ANY, &reply->nat_addr);

      reply->swif = clib_host_to_net_u32 (face->sw_if);
      reply->flags = clib_host_to_net_u32 (face->flags);
      reply->faceid = clib_host_to_net_u32 (faceid);
    }
  else
    rv = HICN_ERROR_FACE_IP_ADJ_NOT_FOUND;

done:
  return (rv);
}

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables: eval: (c-set-style "gnu") End:
 */