summaryrefslogtreecommitdiffstats
path: root/src/vnet/srv6/sr_pt.c
blob: 28da12e22f4abeefcecf58d07e04c2ba055e2ea6 (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
/* SPDX-License-Identifier: Apache-2.0
 * Copyright(c) 2022 Cisco Systems, Inc.
 */

/**
 * @file
 * @brief SR Path Tracing (PT)
 *
 * PT CLI
 *
 */

#include <vlib/vlib.h>
#include <vnet/vnet.h>
#include <vnet/srv6/sr.h>
#include <vnet/ip/ip.h>
#include <vnet/srv6/sr_packet.h>
#include <vnet/ip/ip6_packet.h>
#include <vnet/fib/ip6_fib.h>
#include <vnet/dpo/dpo.h>
#include <vnet/adj/adj.h>
#include <vnet/srv6/sr_pt.h>

#include <vppinfra/error.h>
#include <vppinfra/elog.h>

sr_pt_main_t sr_pt_main;

void *
sr_pt_find_iface (u32 iface)
{
  sr_pt_main_t *sr_pt = &sr_pt_main;
  uword *p;

  /* Search for the item */
  p = mhash_get (&sr_pt->sr_pt_iface_index_hash, &iface);
  if (p)
    {
      /* Retrieve pt_iface */
      return pool_elt_at_index (sr_pt->sr_pt_iface, p[0]);
    }
  return NULL;
}

int
sr_pt_add_iface (u32 iface, u16 id, u8 ingress_load, u8 egress_load,
		 u8 tts_template)
{
  sr_pt_main_t *sr_pt = &sr_pt_main;
  uword *p;

  sr_pt_iface_t *ls = 0;

  if (iface == (u32) ~0)
    return SR_PT_ERR_IFACE_INVALID;

  /* Search for the item */
  p = mhash_get (&sr_pt->sr_pt_iface_index_hash, &iface);

  if (p)
    return SR_PT_ERR_EXIST;

  if (id > SR_PT_ID_MAX)
    return SR_PT_ERR_ID_INVALID;

  if (ingress_load > SR_PT_LOAD_MAX || egress_load > SR_PT_LOAD_MAX)
    return SR_PT_ERR_LOAD_INVALID;

  if (tts_template > SR_PT_TTS_TEMPLATE_MAX)
    return SR_PT_ERR_TTS_TEMPLATE_INVALID;

  /* Create a new pt_iface */
  pool_get (sr_pt->sr_pt_iface, ls);
  clib_memset (ls, 0, sizeof (*ls));
  ls->iface = iface;
  ls->id = id;
  ls->ingress_load = ingress_load;
  ls->egress_load = egress_load;
  ls->tts_template = tts_template;

  /* Set hash key for searching pt_iface by iface */
  mhash_set (&sr_pt->sr_pt_iface_index_hash, &iface, ls - sr_pt->sr_pt_iface,
	     NULL);
  return 0;
}

int
sr_pt_del_iface (u32 iface)
{
  sr_pt_main_t *sr_pt = &sr_pt_main;
  uword *p;

  sr_pt_iface_t *ls = 0;

  if (iface == (u32) ~0)
    return SR_PT_ERR_IFACE_INVALID;

  /* Search for the item */
  p = mhash_get (&sr_pt->sr_pt_iface_index_hash, &iface);

  if (p)
    {
      /* Retrieve sr_pt_iface */
      ls = pool_elt_at_index (sr_pt->sr_pt_iface, p[0]);
      /* Delete sr_pt_iface */
      pool_put (sr_pt->sr_pt_iface, ls);
      mhash_unset (&sr_pt->sr_pt_iface_index_hash, &iface, NULL);
    }
  else
    {
      return SR_PT_ERR_NOENT;
    }
  return 0;
}

/**
 * @brief "sr pt add iface" CLI function.
 *
 * @see sr_pt_add_iface
 */
static clib_error_t *
sr_pt_add_iface_command_fn (vlib_main_t *vm, unformat_input_t *input,
			    vlib_cli_command_t *cmd)
{
  vnet_main_t *vnm = vnet_get_main ();
  u32 iface = (u32) ~0;
  u32 id = (u32) ~0;
  u32 ingress_load = 0;
  u32 egress_load = 0;
  u32 tts_template = SR_PT_TTS_TEMPLATE_DEFAULT;

  int rv;

  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
    {
      if (unformat (input, "%U", unformat_vnet_sw_interface, vnm, &iface))
	;
      else if (unformat (input, "id %u", &id))
	;
      else if (unformat (input, "ingress-load %u", &ingress_load))
	;
      else if (unformat (input, "egress-load %u", &egress_load))
	;
      else if (unformat (input, "tts-template %u", &tts_template))
	;
      else
	break;
    }

  rv = sr_pt_add_iface (iface, id, ingress_load, egress_load, tts_template);

  switch (rv)
    {
    case 0:
      break;
    case SR_PT_ERR_EXIST:
      return clib_error_return (0, "Error: Identical iface already exists.");
    case SR_PT_ERR_IFACE_INVALID:
      return clib_error_return (0, "Error: The iface name invalid.");
    case SR_PT_ERR_ID_INVALID:
      return clib_error_return (0, "Error: The iface id value invalid.");
    case SR_PT_ERR_LOAD_INVALID:
      return clib_error_return (
	0, "Error: The iface ingress or egress load value invalid.");
    case SR_PT_ERR_TTS_TEMPLATE_INVALID:
      return clib_error_return (
	0, "Error: The iface TTS Template value invalid.");
    default:
      return clib_error_return (0, "Error: unknown error.");
    }
  return 0;
}

/**
 * @brief "sr pt del iface" CLI function.
 *
 * @see sr_pt_del_iface
 */
static clib_error_t *
sr_pt_del_iface_command_fn (vlib_main_t *vm, unformat_input_t *input,
			    vlib_cli_command_t *cmd)
{
  vnet_main_t *vnm = vnet_get_main ();
  u32 iface = (u32) ~0;

  int rv;

  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
    {
      if (unformat (input, "%U", unformat_vnet_sw_interface, vnm, &iface))
	;
      else
	break;
    }

  rv = sr_pt_del_iface (iface);

  switch (rv)
    {
    case 0:
      break;
    case SR_PT_ERR_NOENT:
      return clib_error_return (0, "Error: No such iface.");
    case SR_PT_ERR_IFACE_INVALID:
      return clib_error_return (0, "Error: The iface name is not valid.");
    default:
      return clib_error_return (0, "Error: unknown error.");
    }
  return 0;
}

/**
 * @brief CLI function to show all PT interfcaes
 */
static clib_error_t *
sr_pt_show_iface_command_fn (vlib_main_t *vm, unformat_input_t *input,
			     vlib_cli_command_t *cmd)
{
  vnet_main_t *vnm = vnet_get_main ();
  sr_pt_main_t *sr_pt = &sr_pt_main;
  sr_pt_iface_t **sr_pt_iface_list = 0;
  sr_pt_iface_t *ls;
  int i;

  vlib_cli_output (vm, "SR PT Interfaces");
  vlib_cli_output (vm, "==================================");

  pool_foreach (ls, sr_pt->sr_pt_iface)
    {
      vec_add1 (sr_pt_iface_list, ls);
    };

  for (i = 0; i < vec_len (sr_pt_iface_list); i++)
    {
      ls = sr_pt_iface_list[i];
      vlib_cli_output (
	vm,
	"\tiface       : \t%U\n\tid          : \t%d\n\tingress-load: "
	"\t%d\n\tegress-load : \t%d\n\ttts-template: \t%d  ",
	format_vnet_sw_if_index_name, vnm, ls->iface, ls->id, ls->ingress_load,
	ls->egress_load, ls->tts_template);
      vlib_cli_output (vm, "--------------------------------");
    }

  return 0;
}

VLIB_CLI_COMMAND (sr_pt_add_iface_command, static) = {
  .path = "sr pt add iface",
  .short_help = "sr pt add iface <iface-name> id <pt-iface-id> ingress-load "
		"<ingress-load-value> egress-load <egress-load-value> "
		"tts-template <tts-template-value>",
  .function = sr_pt_add_iface_command_fn,
};

VLIB_CLI_COMMAND (sr_pt_del_iface_command, static) = {
  .path = "sr pt del iface",
  .short_help = "sr pt del iface <iface-name>",
  .function = sr_pt_del_iface_command_fn,
};

VLIB_CLI_COMMAND (sr_pt_show_iface_command, static) = {
  .path = "sr pt show iface",
  .short_help = "sr pt show iface",
  .function = sr_pt_show_iface_command_fn,
};

/**
 *  * @brief SR PT initialization
 *   */
clib_error_t *
sr_pt_init (vlib_main_t *vm)
{
  sr_pt_main_t *pt = &sr_pt_main;
  mhash_init (&pt->sr_pt_iface_index_hash, sizeof (uword), sizeof (u32));
  return 0;
}

VLIB_INIT_FUNCTION (sr_pt_init);