summaryrefslogtreecommitdiffstats
path: root/hicn-plugin/src/faces/face.c
blob: 58c1c34c88d3972329a638de16642e7a177a41d2 (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
/*
 * 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 <vnet/fib/fib_entry_track.h>

#include "face.h"
#include "../hicn.h"
#include "../params.h"
#include "../error.h"
#include "../mapme.h"
#include "../mapme_eventmgr.h"

dpo_id_t *face_dpo_vec;
hicn_face_vft_t *face_vft_vec;
char **face_type_names_vec;
u8 pl_index = 1;

hicn_face_t *hicn_dpoi_face_pool;

dpo_type_t first_type = DPO_FIRST;

vlib_combined_counter_main_t *counters;

dpo_type_t hicn_face_type;

fib_node_type_t hicn_face_fib_node_type;

const char *HICN_FACE_CTRX_STRING[] = {
#define _(a, b, c) c,
  foreach_hicn_face_counter
#undef _
};

u8 *
face_show (u8 *s, int face_id, u32 indent)
{
  s = format (s, "%U Faces:\n", format_white_space, indent);
  indent += 4;
  int i;
  vec_foreach_index (i, face_dpo_vec)
    {
      s = format (s, "%U", face_vft_vec[i].format_face,
		  face_dpo_vec[face_id].dpoi_index, indent);
    }

  return (s);
}

mhash_t hicn_face_hashtb;

const static char *const hicn_face6_nodes[] = {
  "hicn6-face-output",	// this is the name you give your node in
			// VLIB_REGISTER_NODE
  NULL,
};

const static char *const hicn_face4_nodes[] = {
  "hicn4-face-output",	// this is the name you give your node in
			// VLIB_REGISTER_NODE
  NULL,
};

const static char *const *const hicn_face_nodes[DPO_PROTO_NUM] = {
  [DPO_PROTO_IP4] = hicn_face4_nodes, [DPO_PROTO_IP6] = hicn_face6_nodes
};

const static dpo_vft_t hicn_face_dpo_vft = {
  .dv_lock = hicn_face_lock,
  .dv_unlock = hicn_face_unlock,
  .dv_format = format_hicn_face,
};

static fib_node_t *
hicn_face_node_get (fib_node_index_t index)
{
  hicn_face_t *face;

  face = hicn_dpoi_get_from_idx (index);

  return (&face->fib_node);
}

static void
hicn_face_last_lock_gone (fib_node_t *node)
{
}

static hicn_face_t *
hicn_face_from_fib_node (fib_node_t *node)
{
  return ((hicn_face_t *) (((char *) node) -
			   STRUCT_OFFSET_OF (hicn_face_t, fib_node)));
}

static fib_node_back_walk_rc_t
hicn_face_back_walk_notify (fib_node_t *node, fib_node_back_walk_ctx_t *ctx)
{

  hicn_face_t *face = hicn_face_from_fib_node (node);

  const dpo_id_t *dpo_loadbalance =
    fib_entry_contribute_ip_forwarding (face->fib_entry_index);
  const load_balance_t *lb0 = load_balance_get (dpo_loadbalance->dpoi_index);

  const dpo_id_t *dpo = load_balance_get_bucket_i (lb0, 0);

  dpo_stack (hicn_face_type, face->dpo.dpoi_proto, &face->dpo, dpo);
  /* if (dpo_is_adj(dpo)) */
  /*   { */
  /*     ip_adjacency_t * adj = adj_get (dpo->dpoi_index); */

  /*     if (dpo->dpoi_type == DPO_ADJACENCY_MIDCHAIN || */
  /*         dpo->dpoi_type == DPO_ADJACENCY_MCAST_MIDCHAIN) */
  /*       { */
  /*         adj_nbr_midchain_stack(dpo->dpoi_index, &face->dpo); */
  /*       } */
  /*     else */
  /*       { */
  /*         dpo_stack(hicn_face_type, face->dpo.dpoi_proto, &face->dpo, dpo);
   */
  /*       } */
  /*   } */

  return (FIB_NODE_BACK_WALK_CONTINUE);
}

static void
hicn_face_show_memory (void)
{
}

static const fib_node_vft_t hicn_face_fib_node_vft = {
  .fnv_get = hicn_face_node_get,
  .fnv_last_lock = hicn_face_last_lock_gone,
  .fnv_back_walk = hicn_face_back_walk_notify,
  .fnv_mem_show = hicn_face_show_memory,
};

// Make this more flexible for future types face
void
hicn_face_module_init (vlib_main_t *vm)
{
  pool_validate (hicn_dpoi_face_pool);
  pool_alloc (hicn_dpoi_face_pool, 1024);
  counters = vec_new (vlib_combined_counter_main_t,
		      HICN_PARAM_FACES_MAX * HICN_N_COUNTER);

  mhash_init (&hicn_face_hashtb, sizeof (hicn_face_id_t) /* value */,
	      sizeof (hicn_face_key_t) /* key */);

  /*
   * How much useful is the following registration?
   * So far it seems that we need it only for setting the dpo_type.
   */
  hicn_face_type = dpo_register_new_type (&hicn_face_dpo_vft, hicn_face_nodes);

  /*
   * We register a new node type to get informed when the adjacency
   * corresponding to a face is updated
   */
  hicn_face_fib_node_type =
    fib_node_register_new_type ("hicn_face_fib_node", &hicn_face_fib_node_vft);
}

u8 *
format_hicn_face (u8 *s, va_list *args)
{
  index_t index = va_arg (*args, index_t);
  u32 indent = va_arg (*args, u32);
  hicn_face_t *face;

  face = hicn_dpoi_get_from_idx (index);

  if (face->flags & HICN_FACE_FLAGS_FACE)
    {
      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, "nat address %U locks %u, path_label %u",
		  format_ip46_address, &face->nat_addr, IP46_TYPE_ANY,
		  face->locks, face->pl_id);

      if ((face->flags & HICN_FACE_FLAGS_APPFACE_PROD))
	s = format (s, " (producer)");
      else if ((face->flags & HICN_FACE_FLAGS_APPFACE_CONS))
	s = format (s, " (consumer)");

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

      s = format (s, "\n%U%U", format_white_space, indent + 2, format_dpo_id,
		  &face->dpo, indent + 3);
    }
  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, "nat address %U locks %u, path_label %u",
		  format_ip46_address, &face->nat_addr, IP46_TYPE_ANY,
		  face->locks, face->pl_id);

      if ((face->flags & HICN_FACE_FLAGS_APPFACE_PROD))
	s = format (s, " (producer)");
      else if ((face->flags & HICN_FACE_FLAGS_APPFACE_CONS))
	s = format (s, " (consumer)");

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

  return s;
}

u8 *
format_hicn_face_all (u8 *s, int n, ...)
{
  va_list ap;
  va_start (ap, n);
  u32 indent = va_arg (ap, u32);

  s = format (s, "%U Faces:\n", format_white_space, indent);

  hicn_face_t *face;

  pool_foreach (face, hicn_dpoi_face_pool)
    {
      s = format (s, "%U\n", format_hicn_face, hicn_dpoi_get_index (face),
		  indent);
    }

  return s;
}

int
hicn_face_del (hicn_face_id_t face_id)
{
  int ret = HICN_ERROR_NONE;

  if (hicn_dpoi_idx_is_valid (face_id))
    {
      hicn_face_t *face = hicn_dpoi_get_from_idx (face_id);
      if (face->locks == 0)
	pool_put_index (hicn_dpoi_face_pool, face_id);
      else
	face->flags |= HICN_FACE_FLAGS_DELETED;
    }
  else
    ret = HICN_ERROR_FACE_NOT_FOUND;

  return ret;
}

static void
hicn_iface_to_face (hicn_face_t *face, const dpo_id_t *dpo)
{
  dpo_stack (hicn_face_type, dpo->dpoi_proto, &face->dpo, dpo);

  face->flags &= ~HICN_FACE_FLAGS_IFACE;
  face->flags |= HICN_FACE_FLAGS_FACE;

  if (dpo_is_adj (dpo))
    {
      fib_node_init (&face->fib_node, hicn_face_fib_node_type);
      fib_node_lock (&face->fib_node);

      if (dpo->dpoi_type != DPO_ADJACENCY_MIDCHAIN &&
	  dpo->dpoi_type != DPO_ADJACENCY_MCAST_MIDCHAIN)
	{
	  ip_adjacency_t *adj = adj_get (dpo->dpoi_index);
	  ip46_address_t *nh = &(adj->sub_type.nbr.next_hop);
	  fib_prefix_t prefix;

	  if (!ip46_address_is_zero (nh))
	    {
	      fib_prefix_from_ip46_addr (nh, &prefix);

	      u32 fib_index = fib_table_find (prefix.fp_proto, HICN_FIB_TABLE);

	      face->fib_entry_index = fib_entry_track (
		fib_index, &prefix, hicn_face_fib_node_type,
		hicn_dpoi_get_index (face), &face->fib_sibling);
	    }
	}
    }
}

/*
 * 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_add (const dpo_id_t *dpo_nh, ip46_address_t *nat_address, int sw_if,
	       hicn_face_id_t *pfaceid)
{

  hicn_face_t *face;

  face =
    hicn_face_get_with_dpo (nat_address, sw_if, dpo_nh, &hicn_face_hashtb);

  if (face != NULL)
    {
      *pfaceid = hicn_dpoi_get_index (face);
      return HICN_ERROR_FACE_ALREADY_CREATED;
    }

  face =
    hicn_face_get (nat_address, sw_if, &hicn_face_hashtb, dpo_nh->dpoi_index);

  dpo_id_t temp_dpo = DPO_INVALID;
  temp_dpo.dpoi_index = dpo_nh->dpoi_index;
  hicn_face_key_t key;
  hicn_face_get_key (nat_address, sw_if, dpo_nh, &key);

  if (face == NULL)
    {

      hicn_iface_add (nat_address, sw_if, pfaceid, dpo_nh->dpoi_index, 0);
      face = hicn_dpoi_get_from_idx (*pfaceid);

      mhash_set_mem (&hicn_face_hashtb, &key, (uword *) pfaceid, 0);

      hicn_face_get_key (nat_address, sw_if, &temp_dpo, &key);
      mhash_set_mem (&hicn_face_hashtb, &key, (uword *) pfaceid, 0);
    }
  else
    {
      /* We found an iface and we convert it to a face */
      *pfaceid = hicn_dpoi_get_index (face);
      mhash_set_mem (&hicn_face_hashtb, &key, (uword *) pfaceid, 0);
    }

  hicn_iface_to_face (face, dpo_nh);

  temp_dpo.dpoi_index = ~0;

  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));

  *retx = (retx_t){
    .face_id = *pfaceid,
  };

  return HICN_ERROR_NONE;
}

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