aboutsummaryrefslogtreecommitdiffstats
path: root/hicn-plugin/src/route.c
blob: 9202efbd49adfb4ea661b0d5920ee716ecb67a52 (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
/*
 * 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/fib/fib_entry.h>
#include <vnet/fib/fib_table.h>
#include <vnet/ip/ip6_packet.h>
#include <vnet/dpo/dpo.h>
#include <vnet/dpo/load_balance.h>
#include <vlib/global_funcs.h>

#include "strategy_dpo_ctx.h"
#include "strategy_dpo_manager.h"
#include "strategy.h"
#include "faces/face.h"
#include "error.h"
#include "strategies/dpo_mw.h"

int
hicn_route_get_dpo (const ip46_address_t * prefix, u8 plen,
		    const dpo_id_t ** hicn_dpo, u32 * fib_index)
{
  fib_prefix_t fib_pfx;
  const dpo_id_t *load_balance_dpo_id;
  const dpo_id_t *former_dpo_id;
  int found = 0, ret = HICN_ERROR_ROUTE_NOT_FOUND;
  fib_node_index_t fib_entry_index;

  /* At this point the face exists in the face table */
  fib_prefix_from_ip46_addr (prefix, &fib_pfx);
  fib_pfx.fp_len = plen;


  /* Check if the route already exist in the fib */
  /*
   * ASSUMPTION: we use table 0 which is the default table and it is
   * already existing and locked
   */
  *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_exact_match (*fib_index, &fib_pfx);

  if (fib_entry_index != FIB_NODE_INDEX_INVALID)
    {
      /* Route already existing. We need to update the dpo. */
      load_balance_dpo_id =
	fib_entry_contribute_ip_forwarding (fib_entry_index);

      /* The dpo is not a load balance dpo as expected */
      if (load_balance_dpo_id->dpoi_type != DPO_LOAD_BALANCE)
	ret = HICN_ERROR_ROUTE_NO_LD;
      else
	{
	  /* 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. */
	  ret = HICN_ERROR_ROUTE_DPO_NO_HICN;
	  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))
		{
		  *hicn_dpo = former_dpo_id;
		  ret = HICN_ERROR_NONE;
		  found = 1;
		}
	    }
	}
    }
  /*
   * Remove the lock from the table. We keep one lock per route, not
   * per dpo
   */
  fib_table_unlock (*fib_index, fib_pfx.fp_proto, FIB_SOURCE_PLUGIN_HI);

  return ret;
}

/* Add a new route for a name prefix */
int
hicn_route_add (hicn_face_id_t * face_id, u32 len,
		const ip46_address_t * prefix, u8 plen)
{

  fib_prefix_t fib_pfx;
  dpo_id_t dpo = DPO_INVALID;
  const dpo_id_t *hicn_dpo_id;
  int ret = HICN_ERROR_NONE;
  dpo_id_t face_dpo_tmp[HICN_PARAM_FIB_ENTRY_NHOPS_MAX];
  int n_face_dpo = 0;
  index_t dpo_idx;
  u32 fib_index;
  vlib_main_t *vm = vlib_get_main ();
  hicn_face_vft_t *face_vft = NULL;

  if (face_id == NULL)
    {
      return HICN_ERROR_ROUTE_INVAL;
    }
  /*
   * Check is the faces are available, otherwise skip the face
   * id_adjacency existance is not checked. It should be checked before
   * sending a packet out
   */
  for (int i = 0; i < clib_min (HICN_PARAM_FIB_ENTRY_NHOPS_MAX, len); i++)
    {
      hicn_face_t *face = hicn_dpoi_get_from_idx (face_id[i]);
      face_vft = hicn_face_get_vft (face->shared.face_type);
      dpo_id_t face_dpo = DPO_INVALID;
      face_vft->hicn_face_get_dpo (face, &face_dpo);

      if (!dpo_id_is_valid (&face_dpo))
	{
	  vlib_cli_output (vm, "Face %d not found, skip...\n", face_id[i]);
	  return ret;
	}
      else
	{
	  face_dpo_tmp[n_face_dpo++] = face_dpo;
	}
    }

  ret = hicn_route_get_dpo (prefix, plen, &hicn_dpo_id, &fib_index);

  if (ret == HICN_ERROR_ROUTE_NOT_FOUND)
    {
      /* The Fib entry does not exist */
      /* At this point the face exists in the face table */
      fib_prefix_from_ip46_addr (prefix, &fib_pfx);
      fib_pfx.fp_len = plen;

      dpo_id_t nhops[HICN_PARAM_FIB_ENTRY_NHOPS_MAX];
      for (int i = 0; i < n_face_dpo; i++)
	{
	  clib_memcpy (&nhops[i], &face_dpo_tmp[i], sizeof (dpo_id_t));
	}

      ret =
	default_dpo.hicn_dpo_create (fib_pfx.fp_proto, nhops, n_face_dpo,
				     &dpo_idx);

      if (ret)
	{
	  return ret;
	}
      /* the value we got when we registered */
      /*
       * This should be taken from the name?!? the index of the
       * object
       */
      dpo_set (&dpo,
	       default_dpo.hicn_dpo_get_type (),
	       (ip46_address_is_ip4 (prefix) ? DPO_PROTO_IP4 : DPO_PROTO_IP6),
	       dpo_idx);

      /* Here is where we create the "via" like route */
      /*
       * For the moment we use the global one the prefix you want
       * to match Neale suggested -- FIB_SOURCE_HICN the client
       * that is adding them -- no easy explanation at this time…
       */
      fib_node_index_t new_fib_node_index =
	fib_table_entry_special_dpo_add (fib_index,
					 &fib_pfx,
					 FIB_SOURCE_PLUGIN_HI,
					 FIB_ENTRY_FLAG_EXCLUSIVE,
					 &dpo);

      /* We added a route, therefore add one lock to the table */
      fib_table_lock (fib_index, fib_pfx.fp_proto, FIB_SOURCE_PLUGIN_HI);

      dpo_unlock (&dpo);
      ret =
	(new_fib_node_index !=
	 FIB_NODE_INDEX_INVALID) ? HICN_ERROR_NONE :
	HICN_ERROR_ROUTE_NO_INSERT;

      /*
       * TODO: we might want to store the fib index in the face.
       * This will help to update the fib entries when a face is
       * deleted. Fib_index_t is returned from
       * fib_table_entry_special_dpo_add.
       */
    }
  else if (ret == HICN_ERROR_NONE)
    {
      ret = HICN_ERROR_ROUTE_ALREADY_EXISTS;
    }
  return ret;
}

int
hicn_route_add_nhops (hicn_face_id_t * face_id, u32 len,
		      const ip46_address_t * prefix, u8 plen)
{
  const dpo_id_t *hicn_dpo_id;
  int ret = HICN_ERROR_NONE;
  dpo_id_t faces_dpo_tmp[HICN_PARAM_FIB_ENTRY_NHOPS_MAX];
  int n_face_dpo = 0;
  const hicn_dpo_vft_t *dpo_vft;
  u32 fib_index;
  vlib_main_t *vm = vlib_get_main ();
  hicn_face_vft_t *face_vft = NULL;

  if (face_id == NULL)
    {
      return HICN_ERROR_ROUTE_INVAL;
    }
  /*
   * Check is the faces are available, otherwise skip the face
   * id_adjacency existance is not checked. It should be checked before
   * sending a packet out
   */
  for (int i = 0; i < clib_min (HICN_PARAM_FIB_ENTRY_NHOPS_MAX, len); i++)
    {
      hicn_face_t *face = hicn_dpoi_get_from_idx (face_id[i]);
      face_vft = hicn_face_get_vft (face->shared.face_type);
      dpo_id_t face_dpo = DPO_INVALID;
      face_vft->hicn_face_get_dpo (face, &face_dpo);

      if (!dpo_id_is_valid (&face_dpo))
	{
	  vlib_cli_output (vm, "Face %d not found, skip...\n", face_id[i]);
	  return ret;
	}
      else
	{
	  faces_dpo_tmp[n_face_dpo++] = face_dpo;
	}
    }

  ret = hicn_route_get_dpo (prefix, plen, &hicn_dpo_id, &fib_index);

  if (ret == HICN_ERROR_NONE)
    {
      for (int i = 0; i < n_face_dpo && (ret == HICN_ERROR_NONE); i++)
	{
	  u32 vft_id = hicn_dpo_get_vft_id (hicn_dpo_id);
	  dpo_vft = hicn_dpo_get_vft (vft_id);
	  ret = dpo_vft->hicn_dpo_add_update_nh (&faces_dpo_tmp[i],
						 hicn_dpo_id->dpoi_index);
	}
    }
  return ret;
}

int
hicn_route_del (ip46_address_t * prefix, u8 plen)
{
  fib_prefix_t fib_pfx;
  const dpo_id_t *hicn_dpo_id;
  int ret = HICN_ERROR_NONE;
  u32 fib_index;

  /* At this point the face exists in the face table */
  fib_prefix_from_ip46_addr (prefix, &fib_pfx);
  fib_pfx.fp_len = plen;

  /* Remove the fib entry only if the dpo is of type hicn */
  ret = hicn_route_get_dpo (prefix, plen, &hicn_dpo_id, &fib_index);

  if (ret == HICN_ERROR_NONE)
    {
      fib_table_entry_special_remove (HICN_FIB_TABLE, &fib_pfx,
				      FIB_SOURCE_PLUGIN_HI);

      /*
       * Remove the lock from the table. We keep one lock per route
       */
      fib_table_unlock (fib_index, fib_pfx.fp_proto, FIB_SOURCE_PLUGIN_HI);
    }
  //Remember to remove the lock from the table when removing the entry
  return ret;
}

int
hicn_route_del_nhop (ip46_address_t * prefix, u8 plen, hicn_face_id_t face_id)
{

  fib_prefix_t fib_pfx;
  const dpo_id_t *hicn_dpo_id;
  int ret;
  u32 vft_id;
  const hicn_dpo_vft_t *dpo_vft;
  u32 fib_index;

  /* At this point the face exists in the face table */
  fib_prefix_from_ip46_addr (prefix, &fib_pfx);
  fib_pfx.fp_len = plen;

  ret = hicn_route_get_dpo (prefix, plen, &hicn_dpo_id, &fib_index);

  /* Check if the dpo is an hicn_dpo_t */
  if (ret == HICN_ERROR_NONE)
    {
      vft_id = hicn_dpo_get_vft_id (hicn_dpo_id);
      dpo_vft = hicn_dpo_get_vft (vft_id);
      return dpo_vft->hicn_dpo_del_nh (face_id, hicn_dpo_id->dpoi_index,
				       &fib_pfx);
    }
  //Remember to remove the lock from the table when removing the entry
  return ret;
}

int
hicn_route_set_strategy (ip46_address_t * prefix, u8 plen, u8 strategy_id)
{
  fib_prefix_t fib_pfx;
  const dpo_id_t *hicn_dpo_id;
  dpo_id_t new_dpo_id = DPO_INVALID;
  int ret;
  hicn_dpo_ctx_t *old_hicn_dpo_ctx;
  const hicn_dpo_vft_t *old_dpo_vft;
  const hicn_dpo_vft_t *new_dpo_vft;
  index_t new_hicn_dpo_idx;
  u32 fib_index;
  u32 old_vft_id;

  /* At this point the face exists in the face table */
  fib_prefix_from_ip46_addr (prefix, &fib_pfx);
  fib_pfx.fp_len = plen;

  ret = hicn_route_get_dpo (prefix, plen, &hicn_dpo_id, &fib_index);

  if (ret == HICN_ERROR_NONE)
    {
      old_vft_id = hicn_dpo_get_vft_id (hicn_dpo_id);
      old_dpo_vft = hicn_dpo_get_vft (old_vft_id);
      old_hicn_dpo_ctx =
	old_dpo_vft->hicn_dpo_get_ctx (hicn_dpo_id->dpoi_index);

      new_dpo_vft = hicn_dpo_get_vft_from_id (strategy_id);

      if (new_dpo_vft == NULL)
	return HICN_ERROR_STRATEGY_NOT_FOUND;

      /* Create a new dpo for the new strategy */
      new_dpo_vft->hicn_dpo_create (hicn_dpo_id->dpoi_proto,
				    old_hicn_dpo_ctx->next_hops,
				    old_hicn_dpo_ctx->entry_count,
				    &new_hicn_dpo_idx);

      /* the value we got when we registered */
      dpo_set (&new_dpo_id,
	       new_dpo_vft->hicn_dpo_get_type (),
	       (ip46_address_is_ip4 (prefix) ? DPO_PROTO_IP4 :
		DPO_PROTO_IP6), new_hicn_dpo_idx);

      /* Here is where we create the "via" like route */
      /*
       * For the moment we use the global one the prefix you want
       * to match Neale suggested -- FIB_SOURCE_HICN the client
       * that is adding them -- no easy explanation at this time…
       */
      fib_node_index_t new_fib_node_index =
	fib_table_entry_special_dpo_update (fib_index,
					    &fib_pfx,
					    FIB_SOURCE_PLUGIN_HI,
					    FIB_ENTRY_FLAG_EXCLUSIVE,
					    &new_dpo_id);

      dpo_unlock (&new_dpo_id);
      ret =
	(new_fib_node_index !=
	 FIB_NODE_INDEX_INVALID) ? HICN_ERROR_NONE :
	HICN_ERROR_ROUTE_NOT_UPDATED;
    }
  //Remember to remove the lock from the table when removing the entry
  return ret;

}

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