summaryrefslogtreecommitdiffstats
path: root/hicn-plugin/src/faces/ip/face_ip.c
blob: 23d3900ecbf2ee2719f28b7abf50ea466f77aab3 (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
/*
 * Copyright (c) 2017-2019 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 <vnet/adj/adj_midchain.h>
#include <vnet/adj/adj.h>

#include "face_ip.h"
#include "face_ip_node.h"
#include "dpo_ip.h"
#include "../../strategy_dpo_manager.h"
#include "../face.h"
#include "../../cache_policies/cs_lru.h"
#include "../../infra.h"
#include "../../hicn.h"
#include "../app/face_prod.h"
#include "../app/face_cons.h"

#include "../../mapme.h"	// HICN_MAPME_EVENT_*
#include "../../mapme_eventmgr.h"	// hicn_mapme_eventmgr_process_node

extern vlib_node_registration_t hicn_mapme_eventmgr_process_node;

u32 strategy_face_ip4_vlib_edge;
u32 strategy_face_ip6_vlib_edge;

void
hicn_face_ip_init (vlib_main_t * vm)
{
  int strategy_nodes_n = hicn_strategy_get_all_available ();

  /* Default Strategy has index 0 and it always exists */
  strategy_face_ip4_vlib_edge = vlib_node_add_next (vm,
						    hicn_dpo_get_strategy_vft
						    (default_dpo.
						     hicn_dpo_get_type ())->
						    get_strategy_node_index
						    (),
						    hicn_face_ip4_output_node.
						    index);

  strategy_face_ip6_vlib_edge = vlib_node_add_next (vm,
						    hicn_dpo_get_strategy_vft
						    (default_dpo.
						     hicn_dpo_get_type ())->
						    get_strategy_node_index
						    (),
						    hicn_face_ip6_output_node.
						    index);
  /*
   * Create and edge between al the other strategy nodes and the
   * ip_encap nodes.
   */
  for (int i = 1; i < strategy_nodes_n; i++)
    {
      u32 temp_index4 = vlib_node_add_next (vm,
					    hicn_dpo_get_strategy_vft_from_id
					    (i)->get_strategy_node_index (),
					    hicn_face_ip4_output_node.index);
      u32 temp_index6 = vlib_node_add_next (vm,
					    hicn_dpo_get_strategy_vft_from_id
					    (i)->get_strategy_node_index (),
					    hicn_face_ip6_output_node.index);
      ASSERT (temp_index4 == strategy_face_ip4_vlib_edge);
      ASSERT (temp_index6 == strategy_face_ip6_vlib_edge);
    }

  u32 temp_index4 = vlib_node_add_next (vm,
					hicn_interest_hitpit_node.index,
					hicn_face_ip4_output_node.index);
  u32 temp_index6 = vlib_node_add_next (vm,
					hicn_interest_hitpit_node.index,
					hicn_face_ip6_output_node.index);

  ASSERT (temp_index4 == strategy_face_ip4_vlib_edge);
  ASSERT (temp_index6 == strategy_face_ip6_vlib_edge);


  hicn_dpo_ip_module_init ();

  register_face_type (hicn_face_ip_type, &ip_vft, "ip");
}

int
hicn_face_ip_del (hicn_face_id_t face_id)
{
  hicn_face_t *face = hicn_dpoi_get_from_idx (face_id);
  hicn_face_ip_t *face_ip = (hicn_face_ip_t *) face->data;
  hicn_face_ip_key_t key;
  hicn_face_ip_key_t old_key;
  hicn_face_ip_key_t old_key2;

  if (ip46_address_is_ip4 (&face_ip->local_addr))
    {
      hicn_face_ip4_get_key (&(face_ip->local_addr.ip4), face->shared.sw_if,
			     &key);
      hicn_face_ip_input_faces_t * in_faces_vec = hicn_face_ip4_get_vec(&(face_ip->local_addr.ip4), face->shared.sw_if,
                                                                        &hicn_face_ip_local_hashtb);
      if (in_faces_vec != NULL)
        {
          hicn_face_ip_vec_t * vec = pool_elt_at_index (hicn_vec_pool, in_faces_vec->vec_id);
          u32 index_face = vec_search(*vec, face_id);
          vec_del1(*vec, index_face);

          if (vec_len(*vec) == 0)
            {
              pool_put_index(hicn_vec_pool, in_faces_vec->vec_id);
              mhash_unset (&hicn_face_ip_local_hashtb, &key, (uword *) & old_key);
              vec_free(*vec);
            }
          else
            {
              /* Check if the face we are deleting is the preferred one. */
              /* If so, repleace with another. */
              if (in_faces_vec->face_id == face_id)
                {
                  in_faces_vec->face_id = (*vec)[0];
                }
            }
          hicn_face_ip4_get_key (&(face_ip->remote_addr.ip4), face->shared.sw_if,
                                 &key);
          mhash_unset (&hicn_face_ip_remote_hashtb, &key, (uword *) & old_key2);
        }
    }
  else
    {
      hicn_face_ip6_get_key (&(face_ip->local_addr.ip6), face->shared.sw_if,
			     &key);

      hicn_face_ip_input_faces_t * in_faces_vec = hicn_face_ip6_get_vec(&(face_ip->local_addr.ip6), face->shared.sw_if,
                                                                      &hicn_face_ip_local_hashtb);
      if (in_faces_vec != NULL)
        {
          hicn_face_ip_vec_t * vec = pool_elt_at_index (hicn_vec_pool, in_faces_vec->vec_id);
          u32 index_face = vec_search(*vec, face_id);
          vec_del1(*vec, index_face);

          if (vec_len(*vec) == 0)
            {
              pool_put(hicn_vec_pool, vec);
              mhash_unset (&hicn_face_ip_local_hashtb, &key, (uword *) & old_key);
              vec_free(*vec);
            }
          else
            {
              /* Check if the face we are deleting is the preferred one. */
              /* If so, repleace with another. */
              if (in_faces_vec->face_id == face_id)
                {
                  in_faces_vec->face_id = (*vec)[0];
                }
            }
          hicn_face_ip6_get_key (&(face_ip->remote_addr.ip6), face->shared.sw_if,
                                 &key);
          mhash_unset (&hicn_face_ip_remote_hashtb, &key, (uword *) & old_key);
        }
    }
  return hicn_face_del (face_id);
}

/**
 * @brief Helper for handling midchain adjacencies
 */
void face_midchain_fixup_t (vlib_main_t * vm,
                            struct ip_adjacency_t_ * adj,
                            vlib_buffer_t * b0,
                            const void *data) {
  vnet_buffer (b0)->sw_if_index[VLIB_TX] = 0;
};

/**
 * @brief Build a rewrite string for the face.
 */
static u8*
face_build_rewrite_i (void)
{
  /*
   * passing the adj code a NULL rewrite means 'i don't have one cos
   * t'other end is unresolved'. That's not the case here. For the mpls
   * tunnel there are just no bytes of encap to apply in the adj. We'll impose
   * the label stack once we choose a path. So return a zero length rewrite.
   */
  u8 *rewrite = NULL;

  vec_validate(rewrite, 0);
  vec_reset_length(rewrite);

  return (rewrite);
}

always_inline int
hicn_face_ip_find_adj (const ip46_address_t * remote_addr,
                       int sw_if, adj_index_t * adj)
{
  fib_prefix_t fib_pfx;
  fib_node_index_t fib_entry_index;
  fib_prefix_from_ip46_addr (remote_addr, &fib_pfx);
  fib_pfx.fp_len = ip46_address_is_ip4(remote_addr)? 32 : 128;
  vnet_link_t link_type = ip46_address_is_ip4(&fib_pfx.fp_addr)? VNET_LINK_IP4 : VNET_LINK_IP6;
  *adj = adj_nbr_find(fib_pfx.fp_proto, link_type, &fib_pfx.fp_addr, sw_if);

  if (*adj == ADJ_INDEX_INVALID)
    {
      u32 fib_index = fib_table_find_or_create_and_lock (fib_pfx.fp_proto,
                                                         HICN_FIB_TABLE,
                                                         FIB_SOURCE_PLUGIN_HI);

      fib_entry_index = fib_table_lookup (fib_index, &fib_pfx);

      if (fib_entry_index == (FIB_NODE_INDEX_INVALID))
        return HICN_ERROR_FACE_IP_ADJ_NOT_FOUND;

      *adj = fib_entry_get_adj (fib_entry_index);
      ip_adjacency_t * temp = NULL;
      if (*adj != ~0)
        temp = adj_get(*adj);

      if (temp == NULL || temp->lookup_next_index <= IP_LOOKUP_NEXT_REWRITE)
        {
          if(sw_if != ~0)
            *adj = adj_nbr_add_or_lock(fib_pfx.fp_proto, link_type, remote_addr, sw_if);
          else
            return HICN_ERROR_FACE_IP_ADJ_NOT_FOUND;
        }
      else
        {
          adj_nbr_midchain_update_rewrite(*adj, &face_midchain_fixup_t, NULL, ADJ_FLAG_NONE, face_build_rewrite_i());
          adj_midchain_delegate_stack(*adj, fib_index, &fib_pfx);
        }
    }

  return HICN_ERROR_NONE;
}

/*
 * Utility that adds a new face cache entry. For the moment we assume that
 * the ip_adjacency has already been set up.
 */
int
hicn_face_ip_add (const ip46_address_t * local_addr,
		  const ip46_address_t * remote_addr,
		  int sw_if, hicn_face_id_t * pfaceid,
                  u8 is_app_prod)
{
  dpo_proto_t dpo_proto;

  /* Check if we found at least one ip address */
  if (ip46_address_is_zero (remote_addr))
    return HICN_ERROR_FACE_NO_GLOBAL_IP;

  hicn_face_flags_t flags = (hicn_face_flags_t) 0;
  flags |= HICN_FACE_FLAGS_FACE;

  hicn_face_t *face;
  if (ip46_address_is_ip4 (local_addr))
    {
      face =
	hicn_face_ip4_get (&(remote_addr->ip4), sw_if,
			   &hicn_face_ip_remote_hashtb);

      /* If remote matches the face we need to check if it is an incomplete face */
      if (face == NULL)
	{
	  hicn_iface_ip_add (local_addr, remote_addr, sw_if, pfaceid);
	  face = hicn_dpoi_get_from_idx (*pfaceid);
	}
      else
	{
	  *pfaceid = hicn_dpoi_get_index (face);
	}

      if (!(face->shared.flags & HICN_FACE_FLAGS_IFACE))
        return HICN_ERROR_FACE_ALREADY_CREATED;

      hicn_face_ip_key_t key;
      hicn_face_ip4_get_key (&(local_addr->ip4), sw_if, &key);

      hicn_face_ip_input_faces_t * in_faces =
	hicn_face_ip4_get_vec (&(local_addr->ip4), sw_if,
                               &hicn_face_ip_local_hashtb);

      if (in_faces == NULL)
        {
          adj_index_t adj;
          int ret = hicn_face_ip_find_adj(remote_addr, sw_if, &adj);
          if (ret != HICN_ERROR_NONE)
            return ret;

          hicn_face_ip_input_faces_t in_faces_temp;
          hicn_face_ip_vec_t *vec;
          pool_get(hicn_vec_pool, vec);
          *vec = vec_new(hicn_face_ip_vec_t, 0);
          u32 index = vec - hicn_vec_pool;
          in_faces_temp.vec_id = index;
          vec_add1(*vec, *pfaceid);

          hicn_face_ip_t *ip_face = (hicn_face_ip_t *) face->data;
          clib_memcpy (&ip_face->local_addr, local_addr, sizeof (ip4_address_t));
          clib_memcpy (&ip_face->remote_addr, remote_addr,
                       sizeof (ip4_address_t));
          face->shared.sw_if = sw_if;
          face->shared.flags = flags;
          face->shared.adj = adj;

          dpo_proto = DPO_PROTO_IP4;

          in_faces_temp.face_id = *pfaceid;

          mhash_set_mem (&hicn_face_ip_local_hashtb, &key, (uword *) &in_faces_temp, 0);
        }
      else
        {
          hicn_face_ip_vec_t * vec = pool_elt_at_index(hicn_vec_pool, in_faces->vec_id);

          /* */
          if (vec_search(*vec, *pfaceid) != ~0)
            return HICN_ERROR_FACE_ALREADY_CREATED;

          adj_index_t adj;
          int ret = hicn_face_ip_find_adj(remote_addr, sw_if, &adj);
          if (ret != HICN_ERROR_NONE)
            return ret;

          vec_add1(*vec, *pfaceid);

          hicn_face_ip_t *ip_face = (hicn_face_ip_t *) face->data;
          clib_memcpy (&ip_face->local_addr, local_addr, sizeof (ip4_address_t));
          clib_memcpy (&ip_face->remote_addr, remote_addr,
                       sizeof (ip4_address_t));
          face->shared.sw_if = sw_if;
          face->shared.flags = flags;
          face->shared.adj = adj;

          dpo_proto = DPO_PROTO_IP4;

          mhash_set_mem (&hicn_face_ip_local_hashtb, &key, (uword *) in_faces, 0);

          /* If the face is an application producer face, we set it as the preferred incoming face. */
          /* This is required to handle the CS separation, and the push api in a lightway*/
          if (is_app_prod)
            {
              in_faces->face_id = *pfaceid;
            }
        }
    }
  else
    {
      face =
	hicn_face_ip6_get (&(remote_addr->ip6), sw_if,
			   &hicn_face_ip_remote_hashtb);

      /* If remote matches the face is a iface */
      if (face == NULL)
	{
	  hicn_iface_ip_add (local_addr, remote_addr, sw_if, pfaceid);
	  face = hicn_dpoi_get_from_idx (*pfaceid);
	}
      else
	{
	  *pfaceid = hicn_dpoi_get_index (face);
	}

      if (!(face->shared.flags & HICN_FACE_FLAGS_IFACE))
        return HICN_ERROR_FACE_ALREADY_CREATED;

      hicn_face_ip_key_t key;
      hicn_face_ip6_get_key (&(local_addr->ip6), sw_if, &key);

      hicn_face_ip_input_faces_t * in_faces =
	hicn_face_ip6_get_vec (&(local_addr->ip6), sw_if,
                               &hicn_face_ip_local_hashtb);

      if (in_faces == NULL)
        {
          adj_index_t adj;
          int ret = hicn_face_ip_find_adj(remote_addr, sw_if, &adj);
          if (ret != HICN_ERROR_NONE)
            return ret;

          hicn_face_ip_input_faces_t in_faces_temp;
          hicn_face_ip_vec_t *vec;
          pool_get(hicn_vec_pool, vec);
          vec_alloc(*vec, 1);
          u32 index = vec - hicn_vec_pool;
          in_faces_temp.vec_id = index;
          vec_add1(*vec, *pfaceid);

          hicn_face_ip_t *ip_face = (hicn_face_ip_t *) face->data;
          clib_memcpy (&ip_face->local_addr, local_addr, sizeof (ip6_address_t));
          clib_memcpy (&ip_face->remote_addr, remote_addr,
                       sizeof (ip6_address_t));
          face->shared.sw_if = sw_if;
          face->shared.flags = flags;
          face->shared.adj = adj;

          dpo_proto = DPO_PROTO_IP6;

          in_faces_temp.face_id = *pfaceid;

          mhash_set_mem (&hicn_face_ip_local_hashtb, &key, (uword *) &in_faces_temp, 0);
        }
      else
        {
          hicn_face_ip_vec_t *vec = pool_elt_at_index(hicn_vec_pool, in_faces->vec_id);

          /* */
          if (vec_search(*vec, *pfaceid) != ~0)
            return HICN_ERROR_FACE_ALREADY_CREATED;

          adj_index_t adj;
          int ret = hicn_face_ip_find_adj(remote_addr, sw_if, &adj);
          if (ret != HICN_ERROR_NONE)
            return ret;

          vec_add1(*vec, *pfaceid);

          hicn_face_ip_t *ip_face = (hicn_face_ip_t *) face->data;
          clib_memcpy (&ip_face->local_addr, local_addr, sizeof (ip6_address_t));
          clib_memcpy (&ip_face->remote_addr, remote_addr,
                       sizeof (ip6_address_t));
          face->shared.sw_if = sw_if;
          face->shared.flags = flags;
          face->shared.adj = adj;

          dpo_proto = DPO_PROTO_IP6;

          mhash_set_mem (&hicn_face_ip_local_hashtb, &key, (uword *) in_faces, 0);

          /* If the face is an application producer face, we set it as the preferred incoming face. */
          /* This is required to handle the CS separation, and the push api in a lightway*/
          if (is_app_prod)
            {
              in_faces->face_id = *pfaceid;
            }
        }
    }

  retx_t *retx = vlib_process_signal_event_data (vlib_get_main (),
						 hicn_mapme_eventmgr_process_node.
						 index,
						 HICN_MAPME_EVENT_FACE_ADD, 1,
						 sizeof (retx_t));

  /* *INDENT-OFF* */
  *retx = (retx_t)
  {
    .prefix = 0,
    .dpo = (dpo_id_t)
    {
      .dpoi_type = hicn_face_ip_type,
      .dpoi_proto = dpo_proto,
      .dpoi_next_node = 0,
      .dpoi_index = *pfaceid,
    }
  };
  /* *INDENT-ON* */

  return HICN_ERROR_NONE;
}

u8 *
format_hicn_face_ip (u8 * s, va_list * args)
{
  index_t index = va_arg (*args, index_t);
  CLIB_UNUSED (u32 indent) = va_arg (*args, u32);
  hicn_face_t *face;
  hicn_face_ip_t *ip_face;
  ip_adjacency_t *adj;
  vnet_main_t *vnm = vnet_get_main ();

  face = hicn_dpoi_get_from_idx (index);
  ip_face = (hicn_face_ip_t *) face->data;

  if (face->shared.flags & HICN_FACE_FLAGS_FACE)
    {
      ASSERT (face->shared.adj != (adj_index_t) ~ 0);
      adj = adj_get (face->shared.adj);

      hicn_face_id_t face_id = hicn_dpoi_get_index (face);
      s = format (s, "%U Face %d: ", format_white_space, indent, face_id);
      s = format (s, "type IP local %U ",
		  format_ip46_address, &ip_face->local_addr, IP46_TYPE_ANY);
      s =
	format (s, "remote %U ", format_ip46_address, &ip_face->remote_addr,
		IP46_TYPE_ANY);
      s = format (s, "%U", format_vnet_link, adj->ia_link);

      vnet_sw_interface_t *sw_int =
	vnet_get_sw_interface_or_null (vnm, face->shared.sw_if);
      if (sw_int != NULL)
	s = format (s, " dev %U", format_vnet_sw_interface_name, vnm, sw_int);


      if ((face->shared.flags & HICN_FACE_FLAGS_APPFACE_PROD))
	s = format (s, " %U", format_hicn_face_prod, face_id, 0);
      else if ((face->shared.flags & HICN_FACE_FLAGS_APPFACE_CONS))
	s = format (s, " %U", format_hicn_face_cons, face_id, 0);

      if ((face->shared.flags & HICN_FACE_FLAGS_DELETED))
	s = format (s, " (deleted)");
    }
  else
    {
      hicn_face_id_t face_id = hicn_dpoi_get_index (face);
      s = format (s, "%U iFace %d: ", format_white_space, indent, face_id);
      s = format (s, "type IP local %U remote %U",
		  format_ip46_address, &ip_face->local_addr, IP46_TYPE_ANY,
		  format_ip46_address, &ip_face->remote_addr, IP46_TYPE_ANY);

      vnet_sw_interface_t *sw_int =
	vnet_get_sw_interface_or_null (vnm, face->shared.sw_if);
      if (sw_int != NULL)
	s = format (s, " dev %U", format_vnet_sw_interface_name, vnm, sw_int);

      if ((face->shared.flags & HICN_FACE_FLAGS_APPFACE_PROD))
	s = format (s, " %U", format_hicn_face_prod, face_id, 0);
      else if ((face->shared.flags & HICN_FACE_FLAGS_APPFACE_CONS))
	s = format (s, " %U", format_hicn_face_cons, face_id, 0);

      if ((face->shared.flags & HICN_FACE_FLAGS_DELETED))
	s = format (s, " (deleted)");
    }

  return s;
}

void
hicn_face_ip_get_dpo (hicn_face_t * face, dpo_id_t * dpo)
{

  hicn_face_ip_t *face_ip = (hicn_face_ip_t *) face->data;
  return hicn_dpo_ip_create_from_face (face, dpo,
				       ip46_address_is_ip4
				       (&face_ip->remote_addr) ?
				       strategy_face_ip4_vlib_edge :
				       strategy_face_ip6_vlib_edge);
}

hicn_face_vft_t ip_vft = {
  .format_face = format_hicn_face_ip,
  .hicn_face_del = hicn_face_ip_del,
  .hicn_face_get_dpo = hicn_face_ip_get_dpo,
};

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