aboutsummaryrefslogtreecommitdiffstats
path: root/src/vppinfra/serialize.h
blob: 90d615f60a484bb530e8533271b6dac754335d02 (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
/*
 * Copyright (c) 2015 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.
 */
/*
  Copyright (c) 2005 Eliot Dresselhaus

  Permission is hereby granted, free of charge, to any person obtaining
  a copy of this software and associated documentation files (the
  "Software"), to deal in the Software without restriction, including
  without limitation the rights to use, copy, modify, merge, publish,
  distribute, sublicense, and/or sell copies of the Software, and to
  permit persons to whom the Software is furnished to do so, subject to
  the following conditions:

  The above copyright notice and this permission notice shall be
  included in all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#ifndef included_clib_serialize_h
#define included_clib_serialize_h

#include <stdarg.h>
#include <vppinfra/byte_order.h>
#include <vppinfra/types.h>
#include <vppinfra/vec.h>
#include <vppinfra/longjmp.h>

struct serialize_main_header_t;
struct serialize_stream_t;

typedef void (serialize_data_function_t) (struct serialize_main_header_t * h,
					  struct serialize_stream_t * s);

typedef struct serialize_stream_t
{
  /* Current data buffer being serialized/unserialized. */
  u8 *buffer;

  /* Size of buffer in bytes. */
  u32 n_buffer_bytes;

  /* Current index into buffer. */
  u32 current_buffer_index;

  /* Overflow buffer for when there is not enough room at the end of
     buffer to hold serialized/unserialized data. */
  u8 *overflow_buffer;

  /* Current index in overflow buffer for reads. */
  u32 current_overflow_index;

  u32 flags;
#define SERIALIZE_END_OF_STREAM (1 << 0)

  uword data_function_opaque;

  u32 opaque[64 - 4 * sizeof (u32) - 1 * sizeof (uword) -
	     2 * sizeof (void *)];
} serialize_stream_t;

always_inline void
serialize_stream_set_end_of_stream (serialize_stream_t * s)
{
  s->flags |= SERIALIZE_END_OF_STREAM;
}

always_inline uword
serialize_stream_is_end_of_stream (serialize_stream_t * s)
{
  return (s->flags & SERIALIZE_END_OF_STREAM) != 0;
}

typedef struct serialize_main_header_t
{
  u32 recursion_level;

  /* Data callback function and opaque data. */
  serialize_data_function_t *data_function;

  /* Error if signaled by data function. */
  clib_error_t *error;

  /* Exit unwind point if error occurs. */
  clib_longjmp_t error_longjmp;
} serialize_main_header_t;

always_inline void
serialize_error (serialize_main_header_t * m, clib_error_t * error)
{
  clib_longjmp (&m->error_longjmp, pointer_to_uword (error));
}

#define serialize_error_return(m,args...)			\
  serialize_error (&(m)->header, clib_error_return (0, args))

void *serialize_read_write_not_inline (serialize_main_header_t * m,
				       serialize_stream_t * s,
				       uword n_bytes, uword flags);

#define SERIALIZE_FLAG_IS_READ  (1 << 0)
#define SERIALIZE_FLAG_IS_WRITE (1 << 1)

always_inline void *
serialize_stream_read_write (serialize_main_header_t * header,
			     serialize_stream_t * s,
			     uword n_bytes, uword flags)
{
  uword i, j, l;

  l = vec_len (s->overflow_buffer);
  i = s->current_buffer_index;
  j = i + n_bytes;
  s->current_buffer_index = j;
  if (l == 0 && j <= s->n_buffer_bytes)
    {
      return s->buffer + i;
    }
  else
    {
      s->current_buffer_index = i;
      return serialize_read_write_not_inline (header, s, n_bytes, flags);
    }
}

typedef struct
{
  serialize_main_header_t header;
  serialize_stream_t stream;
} serialize_main_t;

always_inline void
serialize_set_end_of_stream (serialize_main_t * m)
{
  serialize_stream_set_end_of_stream (&m->stream);
}

always_inline uword
serialize_is_end_of_stream (serialize_main_t * m)
{
  return serialize_stream_is_end_of_stream (&m->stream);
}

typedef struct
{
  serialize_main_header_t header;
  serialize_stream_t *streams;
} serialize_multiple_main_t;

typedef void (serialize_function_t) (serialize_main_t * m, va_list * va);

always_inline void *
unserialize_get (serialize_main_t * m, uword n_bytes)
{
  return serialize_stream_read_write (&m->header, &m->stream, n_bytes,
				      SERIALIZE_FLAG_IS_READ);
}

always_inline void *
serialize_get (serialize_main_t * m, uword n_bytes)
{
  return serialize_stream_read_write (&m->header, &m->stream, n_bytes,
				      SERIALIZE_FLAG_IS_WRITE);
}

always_inline void
serialize_integer (serialize_main_t * m, u64 x, u32 n_bytes)
{
  u8 *p = serialize_get (m, n_bytes);
  if (n_bytes == 1)
    p[0] = x;
  else if (n_bytes == 2)
    clib_mem_unaligned (p, u16) = clib_host_to_net_u16 (x);
  else if (n_bytes == 4)
    clib_mem_unaligned (p, u32) = clib_host_to_net_u32 (x);
  else if (n_bytes == 8)
    clib_mem_unaligned (p, u64) = clib_host_to_net_u64 (x);
  else
    ASSERT (0);
}

always_inline void
unserialize_integer (serialize_main_t * m, void *x, u32 n_bytes)
{
  u8 *p = unserialize_get (m, n_bytes);
  if (n_bytes == 1)
    *(u8 *) x = p[0];
  else if (n_bytes == 2)
    *(u16 *) x = clib_net_to_host_unaligned_mem_u16 ((u16 *) p);
  else if (n_bytes == 4)
    *(u32 *) x = clib_net_to_host_unaligned_mem_u32 ((u32 *) p);
  else if (n_bytes == 8)
    *(u64 *) x = clib_net_to_host_unaligned_mem_u64 ((u64 *) p);
  else
    ASSERT (0);
}

/* As above but tries to be more compact. */
always_inline void
serialize_likely_small_unsigned_integer (serialize_main_t * m, u64 x)
{
  u64 r = x;
  u8 *p;

  /* Low bit set means it fits into 1 byte. */
  if (r < (1 << 7))
    {
      p = serialize_get (m, 1);
      p[0] = 1 + 2 * r;
      return;
    }

  /* Low 2 bits 1 0 means it fits into 2 bytes. */
  r -= (1 << 7);
  if (r < (1 << 14))
    {
      p = serialize_get (m, 2);
      clib_mem_unaligned (p, u16) = clib_host_to_little_u16 (4 * r + 2);
      return;
    }

  r -= (1 << 14);
  if (r < (1 << 29))
    {
      p = serialize_get (m, 4);
      clib_mem_unaligned (p, u32) = clib_host_to_little_u32 (8 * r + 4);
      return;
    }

  p = serialize_get (m, 9);
  p[0] = 0;			/* Only low 3 bits are used. */
  clib_mem_unaligned (p + 1, u64) = clib_host_to_little_u64 (x);
}

always_inline u64
unserialize_likely_small_unsigned_integer (serialize_main_t * m)
{
  u8 *p = unserialize_get (m, 1);
  u64 r;
  u32 y = p[0];

  if (y & 1)
    return y / 2;

  r = 1 << 7;
  if (y & 2)
    {
      p = unserialize_get (m, 1);
      r += (y / 4) + (p[0] << 6);
      return r;
    }

  r += 1 << 14;
  if (y & 4)
    {
      p = unserialize_get (m, 3);
      r += ((y / 8)
	    + (p[0] << (5 + 8 * 0))
	    + (p[1] << (5 + 8 * 1)) + (p[2] << (5 + 8 * 2)));
      return r;
    }

  p = unserialize_get (m, 8);
  r = clib_mem_unaligned (p, u64);
  r = clib_little_to_host_u64 (r);

  return r;
}

always_inline void
serialize_likely_small_signed_integer (serialize_main_t * m, i64 s)
{
  u64 u = s < 0 ? -(2 * s + 1) : 2 * s;
  serialize_likely_small_unsigned_integer (m, u);
}

always_inline i64
unserialize_likely_small_signed_integer (serialize_main_t * m)
{
  u64 u = unserialize_likely_small_unsigned_integer (m);
  i64 s = u / 2;
  return (u & 1) ? -s : s;
}

void
serialize_multiple_1 (serialize_main_t * m,
		      void *data, uword data_stride, uword n_data);
void
serialize_multiple_2 (serialize_main_t * m,
		      void *data, uword data_stride, uword n_data);
void
serialize_multiple_4 (serialize_main_t * m,
		      void *data, uword data_stride, uword n_data);

void
unserialize_multiple_1 (serialize_main_t * m,
			void *data, uword data_stride, uword n_data);
void
unserialize_multiple_2 (serialize_main_t * m,
			void *data, uword data_stride, uword n_data);
void
unserialize_multiple_4 (serialize_main_t * m,
			void *data, uword data_stride, uword n_data);

always_inline void
serialize_multiple (serialize_main_t * m,
		    void *data,
		    uword n_data_bytes, uword data_stride, uword n_data)
{
  if (n_data_bytes == 1)
    serialize_multiple_1 (m, data, data_stride, n_data);
  else if (n_data_bytes == 2)
    serialize_multiple_2 (m, data, data_stride, n_data);
  else if (n_data_bytes == 4)
    serialize_multiple_4 (m, data, data_stride, n_data);
  else
    ASSERT (0);
}

always_inline void
unserialize_multiple (serialize_main_t * m,
		      void *data,
		      uword n_data_bytes, uword data_stride, uword n_data)
{
  if (n_data_bytes == 1)
    unserialize_multiple_1 (m, data, data_stride, n_data);
  else if (n_data_bytes == 2)
    unserialize_multiple_2 (m, data, data_stride, n_data);
  else if (n_data_bytes == 4)
    unserialize_multiple_4 (m, data, data_stride, n_data);
  else
    ASSERT (0);
}

/* Basic types. */
serialize_function_t serialize_64, unserialize_64;
serialize_function_t serialize_32, unserialize_32;
serialize_function_t serialize_16, unserialize_16;
serialize_function_t serialize_8, unserialize_8;
serialize_function_t serialize_f64, unserialize_f64;
serialize_function_t serialize_f32, unserialize_f32;

/* Basic vector types. */
serialize_function_t serialize_vec_8, unserialize_vec_8;
serialize_function_t serialize_vec_16, unserialize_vec_16;
serialize_function_t serialize_vec_32, unserialize_vec_32;
serialize_function_t serialize_vec_64, unserialize_vec_64;

/* Serialize generic vectors. */
serialize_function_t serialize_vector, unserialize_vector,
  unserialize_aligned_vector;

#define vec_serialize(m,v,f) \
  serialize ((m), serialize_vector, (v), sizeof ((v)[0]), (f))

#define vec_unserialize(m,v,f) \
  unserialize ((m), unserialize_vector, (v), sizeof ((*(v))[0]), (f))

#define vec_unserialize_aligned(m,v,f) \
  unserialize ((m), unserialize_aligned_vector, (v), sizeof ((*(v))[0]), (f))

/* Serialize pools. */
serialize_function_t serialize_pool, unserialize_pool,
  unserialize_aligned_pool;

#define pool_serialize(m,v,f) \
  serialize ((m), serialize_pool, (v), sizeof ((v)[0]), (f))

#define pool_unserialize(m,v,f) \
  unserialize ((m), unserialize_pool, (v), sizeof ((*(v))[0]), (f))

#define pool_unserialize_aligned(m,v,a,f)				\
  unserialize ((m), unserialize_aligned_pool, (v), sizeof ((*(v))[0]), (a), (f))

/* Serialize heaps. */
serialize_function_t serialize_heap, unserialize_heap;

void serialize_bitmap (serialize_main_t * m, uword * b);
uword *unserialize_bitmap (serialize_main_t * m);

void serialize_cstring (serialize_main_t * m, char *string);
void unserialize_cstring (serialize_main_t * m, char **string);

void serialize_close (serialize_main_t * m);
void unserialize_close (serialize_main_t * m);

void serialize_open_data (serialize_main_t * m, u8 * data,
			  uword n_data_bytes);
void unserialize_open_data (serialize_main_t * m, u8 * data,
			    uword n_data_bytes);

/* Starts serialization with expanding vector as buffer. */
void serialize_open_vector (serialize_main_t * m, u8 * vector);

/* Serialization is done: returns vector buffer to caller. */
void *serialize_close_vector (serialize_main_t * m);

void unserialize_open_vector (serialize_main_t * m, u8 * vector);

#ifdef CLIB_UNIX
clib_error_t *serialize_open_clib_file (serialize_main_t * m, char *file);
clib_error_t *unserialize_open_clib_file (serialize_main_t * m, char *file);

void serialize_open_clib_file_descriptor (serialize_main_t * m, int fd);
void unserialize_open_clib_file_descriptor (serialize_main_t * m, int fd);
#endif /* CLIB_UNIX */

/* Main routines. */
clib_error_t *serialize (serialize_main_t * m, ...);
clib_error_t *unserialize (serialize_main_t * m, ...);
clib_error_t *va_serialize (serialize_main_t * m, va_list * va);

void serialize_magic (serialize_main_t * m, void *magic, u32 magic_bytes);
void unserialize_check_magic (serialize_main_t * m, void *magic,
			      u32 magic_bytes);

#endif /* included_clib_serialize_h */

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */
/span>, "add")) is_add = 1; else if (unformat (line_input, "del")) is_add = 0; else if (unformat (line_input, "ip6")) p.is_ipv6 = 1; else if (unformat (line_input, "spd %u", &p.id)) ; else if (unformat (line_input, "inbound")) is_outbound = 0; else if (unformat (line_input, "outbound")) is_outbound = 1; else if (unformat (line_input, "priority %d", &p.priority)) ; else if (unformat (line_input, "protocol %u", &tmp)) p.protocol = (u8) tmp; else if (unformat (line_input, "action %U", unformat_ipsec_policy_action, &p.policy)) { if (p.policy == IPSEC_POLICY_ACTION_RESOLVE) { error = clib_error_return (0, "unsupported action: 'resolve'"); goto done; } } else if (unformat (line_input, "sa %u", &p.sa_id)) ; else if (unformat (line_input, "local-ip-range %U - %U", unformat_ip4_address, &p.laddr.start.ip4, unformat_ip4_address, &p.laddr.stop.ip4)) local_range_set = 1; else if (unformat (line_input, "remote-ip-range %U - %U", unformat_ip4_address, &p.raddr.start.ip4, unformat_ip4_address, &p.raddr.stop.ip4)) remote_range_set = 1; else if (unformat (line_input, "local-ip-range %U - %U", unformat_ip6_address, &p.laddr.start.ip6, unformat_ip6_address, &p.laddr.stop.ip6)) { p.is_ipv6 = 1; local_range_set = 1; } else if (unformat (line_input, "remote-ip-range %U - %U", unformat_ip6_address, &p.raddr.start.ip6, unformat_ip6_address, &p.raddr.stop.ip6)) { p.is_ipv6 = 1; remote_range_set = 1; } else if (unformat (line_input, "local-port-range %u - %u", &tmp, &tmp2)) { p.lport.start = tmp; p.lport.stop = tmp2; } else if (unformat (line_input, "remote-port-range %u - %u", &tmp, &tmp2)) { p.rport.start = tmp; p.rport.stop = tmp2; } else { error = clib_error_return (0, "parse error: '%U'", format_unformat_error, line_input); goto done; } } if (!remote_range_set) { if (p.is_ipv6) clib_memset (&p.raddr.stop.ip6, 0xff, 16); else clib_memset (&p.raddr.stop.ip4, 0xff, 4); } if (!local_range_set) { if (p.is_ipv6) clib_memset (&p.laddr.stop.ip6, 0xff, 16); else clib_memset (&p.laddr.stop.ip4, 0xff, 4); } rv = ipsec_policy_mk_type (is_outbound, p.is_ipv6, p.policy, &p.type); if (rv) { error = clib_error_return (0, "unsupported policy type for:", " outboud:%s %s action:%U", (is_outbound ? "yes" : "no"), (p.is_ipv6 ? "IPv4" : "IPv6"), format_ipsec_policy_action, p.policy); goto done; } rv = ipsec_add_del_policy (vm, &p, is_add, &stat_index); if (!rv) vlib_cli_output (vm, "policy-index:%d", stat_index); else vlib_cli_output (vm, "error:%d", rv); done: unformat_free (line_input); return error; } /* *INDENT-OFF* */ VLIB_CLI_COMMAND (ipsec_policy_add_del_command, static) = { .path = "ipsec policy", .short_help = "ipsec policy [add|del] spd <id> priority <n> ", .function = ipsec_policy_add_del_command_fn, }; /* *INDENT-ON* */ static void ipsec_sa_show_all (vlib_main_t * vm, ipsec_main_t * im, u8 detail) { u32 sai; /* *INDENT-OFF* */ pool_foreach_index (sai, ipsec_sa_pool) { vlib_cli_output (vm, "%U", format_ipsec_sa, sai, (detail ? IPSEC_FORMAT_DETAIL : IPSEC_FORMAT_BRIEF)); } /* *INDENT-ON* */ } static void ipsec_spd_show_all (vlib_main_t * vm, ipsec_main_t * im) { u32 spdi; /* *INDENT-OFF* */ pool_foreach_index (spdi, im->spds) { vlib_cli_output(vm, "%U", format_ipsec_spd, spdi); } if (im->output_flow_cache_flag) { vlib_cli_output (vm, "%U", format_ipsec_out_spd_flow_cache); } if (im->input_flow_cache_flag) { vlib_cli_output (vm, "%U", format_ipsec_in_spd_flow_cache); } /* *INDENT-ON* */ } static void ipsec_spd_bindings_show_all (vlib_main_t * vm, ipsec_main_t * im) { u32 spd_id, sw_if_index; ipsec_spd_t *spd; vlib_cli_output (vm, "SPD Bindings:"); /* *INDENT-OFF* */ hash_foreach(sw_if_index, spd_id, im->spd_index_by_sw_if_index, ({ spd = pool_elt_at_index (im->spds, spd_id); vlib_cli_output (vm, " %d -> %U", spd->id, format_vnet_sw_if_index_name, im->vnet_main, sw_if_index); })); /* *INDENT-ON* */ } static walk_rc_t ipsec_tun_protect_show_one (index_t itpi, void *ctx) { vlib_cli_output (ctx, "%U", format_ipsec_tun_protect_index, itpi); return (WALK_CONTINUE); } static void ipsec_tunnel_show_all (vlib_main_t * vm) { ipsec_tun_protect_walk (ipsec_tun_protect_show_one, vm); } static clib_error_t * show_ipsec_command_fn (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd) { ipsec_main_t *im = &ipsec_main; ipsec_sa_show_all (vm, im, 0); ipsec_spd_show_all (vm, im); ipsec_spd_bindings_show_all (vm, im); ipsec_tun_protect_walk (ipsec_tun_protect_show_one, vm); vlib_cli_output (vm, "IPSec async mode: %s", (im->async_mode ? "on" : "off")); return 0; } /* *INDENT-OFF* */ VLIB_CLI_COMMAND (show_ipsec_command, static) = { .path = "show ipsec all", .short_help = "show ipsec all", .function = show_ipsec_command_fn, }; /* *INDENT-ON* */ static clib_error_t * show_ipsec_sa_command_fn (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd) { ipsec_main_t *im = &ipsec_main; u32 sai = ~0; u8 detail = 0; while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) { if (unformat (input, "%u", &sai)) ; if (unformat (input, "detail")) detail = 1; else break; } if (~0 == sai) ipsec_sa_show_all (vm, im, detail); else vlib_cli_output (vm, "%U", format_ipsec_sa, sai, IPSEC_FORMAT_DETAIL | IPSEC_FORMAT_INSECURE); return 0; } static clib_error_t * clear_ipsec_sa_command_fn (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd) { u32 sai = ~0; while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) { if (unformat (input, "%u", &sai)) ; else break; } if (~0 == sai) { /* *INDENT-OFF* */ pool_foreach_index (sai, ipsec_sa_pool) { ipsec_sa_clear (sai); } /* *INDENT-ON* */ } else { if (pool_is_free_index (ipsec_sa_pool, sai)) return clib_error_return (0, "unknown SA index: %d", sai); else ipsec_sa_clear (sai); } return 0; } /* *INDENT-OFF* */ VLIB_CLI_COMMAND (show_ipsec_sa_command, static) = { .path = "show ipsec sa", .short_help = "show ipsec sa [index]", .function = show_ipsec_sa_command_fn, }; VLIB_CLI_COMMAND (clear_ipsec_sa_command, static) = { .path = "clear ipsec sa", .short_help = "clear ipsec sa [index]", .function = clear_ipsec_sa_command_fn, }; /* *INDENT-ON* */ static clib_error_t * show_ipsec_spd_command_fn (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd) { ipsec_main_t *im = &ipsec_main; u8 show_bindings = 0; u32 spdi = ~0; while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) { if (unformat (input, "%u", &spdi)) ; else if (unformat (input, "bindings")) show_bindings = 1; else break; } if (show_bindings) ipsec_spd_bindings_show_all (vm, im); else if (~0 != spdi) vlib_cli_output (vm, "%U", format_ipsec_spd, spdi); else ipsec_spd_show_all (vm, im); return 0; } /* *INDENT-OFF* */ VLIB_CLI_COMMAND (show_ipsec_spd_command, static) = { .path = "show ipsec spd", .short_help = "show ipsec spd [index]", .function = show_ipsec_spd_command_fn, }; /* *INDENT-ON* */ static clib_error_t * show_ipsec_tunnel_command_fn (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd) { ipsec_tunnel_show_all (vm); return 0; } /* *INDENT-OFF* */ VLIB_CLI_COMMAND (show_ipsec_tunnel_command, static) = { .path = "show ipsec tunnel", .short_help = "show ipsec tunnel", .function = show_ipsec_tunnel_command_fn, }; /* *INDENT-ON* */ static clib_error_t * ipsec_show_backends_command_fn (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd) { ipsec_main_t *im = &ipsec_main; u32 verbose = 0; (void) unformat (input, "verbose %u", &verbose); vlib_cli_output (vm, "IPsec AH backends available:"); u8 *s = format (NULL, "%=25s %=25s %=10s\n", "Name", "Index", "Active"); ipsec_ah_backend_t *ab; /* *INDENT-OFF* */ pool_foreach (ab, im->ah_backends) { s = format (s, "%=25s %=25u %=10s\n", ab->name, ab - im->ah_backends, ab - im->ah_backends == im->ah_current_backend ? "yes" : "no"); if (verbose) { vlib_node_t *n; n = vlib_get_node (vm, ab->ah4_encrypt_node_index); s = format (s, " enc4 %s (next %d)\n", n->name, ab->ah4_encrypt_next_index); n = vlib_get_node (vm, ab->ah4_decrypt_node_index); s = format (s, " dec4 %s (next %d)\n", n->name, ab->ah4_decrypt_next_index); n = vlib_get_node (vm, ab->ah6_encrypt_node_index); s = format (s, " enc6 %s (next %d)\n", n->name, ab->ah6_encrypt_next_index); n = vlib_get_node (vm, ab->ah6_decrypt_node_index); s = format (s, " dec6 %s (next %d)\n", n->name, ab->ah6_decrypt_next_index); } } /* *INDENT-ON* */ vlib_cli_output (vm, "%v", s); vec_set_len (s, 0); vlib_cli_output (vm, "IPsec ESP backends available:"); s = format (s, "%=25s %=25s %=10s\n", "Name", "Index", "Active"); ipsec_esp_backend_t *eb; /* *INDENT-OFF* */ pool_foreach (eb, im->esp_backends) { s = format (s, "%=25s %=25u %=10s\n", eb->name, eb - im->esp_backends, eb - im->esp_backends == im->esp_current_backend ? "yes" : "no"); if (verbose) { vlib_node_t *n; n = vlib_get_node (vm, eb->esp4_encrypt_node_index); s = format (s, " enc4 %s (next %d)\n", n->name, eb->esp4_encrypt_next_index); n = vlib_get_node (vm, eb->esp4_decrypt_node_index); s = format (s, " dec4 %s (next %d)\n", n->name, eb->esp4_decrypt_next_index); n = vlib_get_node (vm, eb->esp6_encrypt_node_index); s = format (s, " enc6 %s (next %d)\n", n->name, eb->esp6_encrypt_next_index); n = vlib_get_node (vm, eb->esp6_decrypt_node_index); s = format (s, " dec6 %s (next %d)\n", n->name, eb->esp6_decrypt_next_index); } } /* *INDENT-ON* */ vlib_cli_output (vm, "%v", s); vec_free (s); return 0; } /* *INDENT-OFF* */ VLIB_CLI_COMMAND (ipsec_show_backends_command, static) = { .path = "show ipsec backends", .short_help = "show ipsec backends", .function = ipsec_show_backends_command_fn, }; /* *INDENT-ON* */ static clib_error_t * ipsec_select_backend_command_fn (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd) { unformat_input_t _line_input, *line_input = &_line_input; ipsec_main_t *im = &ipsec_main; clib_error_t *error; u32 backend_index; error = ipsec_rsc_in_use (im); if (error) return error; /* Get a line of input. */ if (!unformat_user (input, unformat_line_input, line_input)) return 0; if (unformat (line_input, "ah")) { if (unformat (line_input, "%u", &backend_index)) { if (ipsec_select_ah_backend (im, backend_index) < 0) { return clib_error_return (0, "Invalid AH backend index `%u'", backend_index); } } else { return clib_error_return (0, "Invalid backend index `%U'", format_unformat_error, line_input); } } else if (unformat (line_input, "esp")) { if (unformat (line_input, "%u", &backend_index)) { if (ipsec_select_esp_backend (im, backend_index) < 0) { return clib_error_return (0, "Invalid ESP backend index `%u'", backend_index); } } else { return clib_error_return (0, "Invalid backend index `%U'", format_unformat_error, line_input); } } else { return clib_error_return (0, "Unknown input `%U'", format_unformat_error, line_input); } return 0; } /* *INDENT-OFF* */ VLIB_CLI_COMMAND (ipsec_select_backend_command, static) = { .path = "ipsec select backend", .short_help = "ipsec select backend <ah|esp> <backend index>", .function = ipsec_select_backend_command_fn, }; /* *INDENT-ON* */ static clib_error_t * clear_ipsec_counters_command_fn (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd) { vlib_clear_combined_counters (&ipsec_spd_policy_counters); vlib_clear_combined_counters (&ipsec_sa_counters); vlib_clear_simple_counters (&ipsec_sa_lost_counters); return (NULL); } /* *INDENT-OFF* */ VLIB_CLI_COMMAND (clear_ipsec_counters_command, static) = { .path = "clear ipsec counters", .short_help = "clear ipsec counters", .function = clear_ipsec_counters_command_fn, }; /* *INDENT-ON* */ static clib_error_t * ipsec_tun_protect_cmd (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd) { unformat_input_t _line_input, *line_input = &_line_input; u32 sw_if_index, is_del, sa_in, sa_out, *sa_ins = NULL; ip_address_t peer = { }; vnet_main_t *vnm; is_del = 0; sw_if_index = ~0; vnm = vnet_get_main (); if (!unformat_user (input, unformat_line_input, line_input)) return 0; while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) { if (unformat (line_input, "del")) is_del = 1; else if (unformat (line_input, "add")) is_del = 0; else if (unformat (line_input, "sa-in %d", &sa_in)) vec_add1 (sa_ins, sa_in); else if (unformat (line_input, "sa-out %d", &sa_out)) ; else if (unformat (line_input, "%U", unformat_vnet_sw_interface, vnm, &sw_if_index)) ; else if (unformat (line_input, "%U", unformat_ip_address, &peer)) ; else return (clib_error_return (0, "unknown input '%U'", format_unformat_error, line_input)); } if (!is_del) ipsec_tun_protect_update (sw_if_index, &peer, sa_out, sa_ins); else ipsec_tun_protect_del (sw_if_index, &peer); unformat_free (line_input); return NULL; } /** * Protect tunnel with IPSEC */ /* *INDENT-OFF* */ VLIB_CLI_COMMAND (ipsec_tun_protect_cmd_node, static) = { .path = "ipsec tunnel protect", .function = ipsec_tun_protect_cmd, .short_help = "ipsec tunnel protect <interface> input-sa <SA> output-sa <SA> [add|del]", // this is not MP safe }; /* *INDENT-ON* */ static clib_error_t * ipsec_tun_protect_show (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd) { ipsec_tun_protect_walk (ipsec_tun_protect_show_one, vm); return NULL; } /** * show IPSEC tunnel protection */ /* *INDENT-OFF* */ VLIB_CLI_COMMAND (ipsec_tun_protect_show_node, static) = { .path = "show ipsec protect", .function = ipsec_tun_protect_show, .short_help = "show ipsec protect", }; /* *INDENT-ON* */ static int ipsec_tun_protect4_hash_show_one (clib_bihash_kv_8_16_t * kv, void *arg) { ipsec4_tunnel_kv_t *ikv = (ipsec4_tunnel_kv_t *) kv; vlib_main_t *vm = arg; vlib_cli_output (vm, " %U", format_ipsec4_tunnel_kv, ikv); return (BIHASH_WALK_CONTINUE); } static int ipsec_tun_protect6_hash_show_one (clib_bihash_kv_24_16_t * kv, void *arg) { ipsec6_tunnel_kv_t *ikv = (ipsec6_tunnel_kv_t *) kv; vlib_main_t *vm = arg; vlib_cli_output (vm, " %U", format_ipsec6_tunnel_kv, ikv); return (BIHASH_WALK_CONTINUE); } static clib_error_t * ipsec_tun_protect_hash_show (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd) { ipsec_main_t *im = &ipsec_main; { vlib_cli_output (vm, "IPv4:"); clib_bihash_foreach_key_value_pair_8_16 (&im->tun4_protect_by_key, ipsec_tun_protect4_hash_show_one, vm); vlib_cli_output (vm, "IPv6:"); clib_bihash_foreach_key_value_pair_24_16 (&im->tun6_protect_by_key, ipsec_tun_protect6_hash_show_one, vm); } return NULL; } /** * show IPSEC tunnel protection hash tables */ /* *INDENT-OFF* */ VLIB_CLI_COMMAND (ipsec_tun_protect_hash_show_node, static) = { .path = "show ipsec protect-hash", .function = ipsec_tun_protect_hash_show, .short_help = "show ipsec protect-hash", }; /* *INDENT-ON* */ clib_error_t * ipsec_cli_init (vlib_main_t * vm) { return 0; } VLIB_INIT_FUNCTION (ipsec_cli_init); static clib_error_t * set_async_mode_command_fn (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd) { unformat_input_t _line_input, *line_input = &_line_input; int async_enable = 0; if (!unformat_user (input, unformat_line_input, line_input)) return 0; while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) { if (unformat (line_input, "on")) async_enable = 1; else if (unformat (line_input, "off")) async_enable = 0; else return (clib_error_return (0, "unknown input '%U'", format_unformat_error, line_input)); } ipsec_set_async_mode (async_enable); unformat_free (line_input); return (NULL); } /* *INDENT-OFF* */ VLIB_CLI_COMMAND (set_async_mode_command, static) = { .path = "set ipsec async mode", .short_help = "set ipsec async mode on|off", .function = set_async_mode_command_fn, }; /* *INDENT-ON* */ /* * fd.io coding-style-patch-verification: ON * * Local Variables: * eval: (c-set-style "gnu") * End: */