aboutsummaryrefslogtreecommitdiffstats
path: root/src/vnet/ip/punt_api.c
blob: bcbf939f69db46362bf451055f539c95f397dae6 (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
/*
 *------------------------------------------------------------------
 * punt_api.c - Punt api
 *
 * Copyright (c) 2016 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/vnet.h>
#include <vlibmemory/api.h>
#include <vnet/ip/punt.h>
#include <vnet/ip/ip_types_api.h>

#include <vnet/format_fns.h>
#include <vnet/ip/punt.api_enum.h>
#include <vnet/ip/punt.api_types.h>

#define REPLY_MSG_ID_BASE punt_main.msg_id_base
#include <vlibapi/api_helper_macros.h>

static int
vl_api_punt_type_decode (vl_api_punt_type_t in, punt_type_t * out)
{
  in = clib_net_to_host_u32 (in);

  switch (in)
    {
#define _(v, s)                                 \
      case PUNT_API_TYPE_##v:                   \
        *out = PUNT_TYPE_##v;                   \
        return (0);
      foreach_punt_type
#undef _
    }

  return (-1);
}

static vl_api_punt_type_t
vl_api_punt_type_encode (punt_type_t in)
{
  vl_api_punt_type_t pt = PUNT_API_TYPE_L4;

  switch (in)
    {
#define _(v, s)                                   \
      case PUNT_TYPE_##v:                         \
        pt = PUNT_API_TYPE_##v;                   \
        break;
      foreach_punt_type
#undef _
    }

  return (clib_host_to_net_u32 (pt));
}

static int
vl_api_punt_l4_decode (const vl_api_punt_l4_t * in, punt_l4_t * out)
{
  int rv;

  rv = ip_address_family_decode (in->af, &out->af);
  if (rv < 0)
    return (rv);
  rv = ip_proto_decode (in->protocol, &out->protocol);
  if (rv < 0)
    return (rv);
  out->port = clib_net_to_host_u16 (in->port);

  return (rv);
}

static int
vl_api_punt_ip_proto_decode (const vl_api_punt_ip_proto_t * in,
			     punt_ip_proto_t * out)
{
  int rv;

  rv = ip_address_family_decode (in->af, &out->af);
  if (rv < 0)
    return (rv);
  rv = ip_proto_decode (in->protocol, &out->protocol);

  return (rv);
}

static int
vl_api_punt_exception_decode (const vl_api_punt_exception_t * in,
			      punt_exception_t * out)
{
  int rv;

  out->reason = clib_net_to_host_u32 (in->id);
  rv = vlib_punt_reason_validate (out->reason);

  return (rv);
}

static int
vl_api_punt_decode (const vl_api_punt_t * in, punt_reg_t * out)
{
  int rv;

  rv = vl_api_punt_type_decode (in->type, &out->type);

  if (rv)
    return (rv);

  switch (out->type)
    {
    case PUNT_TYPE_L4:
      return (vl_api_punt_l4_decode (&in->punt.l4, &out->punt.l4));
    case PUNT_TYPE_EXCEPTION:
      return (vl_api_punt_exception_decode (&in->punt.exception,
					    &out->punt.exception));
    case PUNT_TYPE_IP_PROTO:
      return (vl_api_punt_ip_proto_decode (&in->punt.ip_proto,
					   &out->punt.ip_proto));
    }

  return (-1);
}

static void
vl_api_punt_l4_encode (const punt_l4_t * in, vl_api_punt_l4_t * out)
{
  out->af = ip_address_family_encode (in->af);
  out->protocol = ip_proto_encode (in->protocol);
  out->port = clib_net_to_host_u16 (in->port);
}

static void
vl_api_punt_ip_proto_encode (const punt_ip_proto_t * in,
			     vl_api_punt_ip_proto_t * out)
{
  out->af = ip_address_family_encode (in->af);
  out->protocol = ip_proto_encode (in->protocol);
}

static void
vl_api_punt_exception_encode (const punt_exception_t * in,
			      vl_api_punt_exception_t * out)
{
  out->id = clib_host_to_net_u32 (in->reason);
}

static void
vl_api_punt_encode (const punt_reg_t * in, vl_api_punt_t * out)
{
  out->type = vl_api_punt_type_encode (in->type);

  switch (in->type)
    {
    case PUNT_TYPE_L4:
      vl_api_punt_l4_encode (&in->punt.l4, &out->punt.l4);
      break;
    case PUNT_TYPE_IP_PROTO:
      vl_api_punt_ip_proto_encode (&in->punt.ip_proto, &out->punt.ip_proto);
      break;
    case PUNT_TYPE_EXCEPTION:
      vl_api_punt_exception_encode (&in->punt.exception,
				    &out->punt.exception);
      break;
    }
}

static void
vl_api_set_punt_t_handler (vl_api_set_punt_t * mp)
{
  vl_api_set_punt_reply_t *rmp;
  vlib_main_t *vm = vlib_get_main ();
  clib_error_t *error;
  punt_reg_t pr;
  int rv;

  rv = vl_api_punt_decode (&mp->punt, &pr);

  if (rv)
    goto out;

  error = vnet_punt_add_del (vm, &pr, mp->is_add);
  if (error)
    {
      rv = -1;
      clib_error_report (error);
    }

out:
  REPLY_MACRO (VL_API_SET_PUNT_REPLY);
}

static void
vl_api_punt_socket_register_t_handler (vl_api_punt_socket_register_t * mp)
{
  vl_api_punt_socket_register_reply_t *rmp;
  vlib_main_t *vm = vlib_get_main ();
  clib_error_t *error;
  punt_reg_t pr;
  int rv;

  rv = vl_api_punt_decode (&mp->punt, &pr);

  if (rv)
    return;

  error = vnet_punt_socket_add (vm, ntohl (mp->header_version),
				&pr, (char *) mp->pathname);
  if (error)
    {
      rv = -1;
      clib_error_report (error);
    }

  char *p = vnet_punt_get_server_pathname ();

  /* *INDENT-OFF* */
  REPLY_MACRO2 (VL_API_PUNT_SOCKET_REGISTER_REPLY,
  ({
    memcpy ((char *) rmp->pathname, p, sizeof (rmp->pathname));
  }));
  /* *INDENT-ON* */
}

typedef struct punt_socket_send_ctx_t_
{
  vl_api_registration_t *reg;
  u32 context;
} punt_socket_send_ctx_t;

static walk_rc_t
vl_api_punt_socket_send_details (const punt_client_t * pc, void *args)
{
  punt_socket_send_ctx_t *ctx = args;
  vl_api_punt_socket_details_t *mp;

  mp = vl_msg_api_alloc (sizeof (*mp));
  if (!mp)
    return (WALK_STOP);

  clib_memset (mp, 0, sizeof (*mp));
  mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_PUNT_SOCKET_DETAILS);
  mp->context = ctx->context;
  vl_api_punt_encode (&pc->reg, &mp->punt);
  memcpy (mp->pathname, pc->caddr.sun_path, sizeof (pc->caddr.sun_path));

  vl_api_send_msg (ctx->reg, (u8 *) mp);

  return (WALK_CONTINUE);
}

static void
vl_api_punt_socket_dump_t_handler (vl_api_punt_socket_dump_t * mp)
{
  vl_api_registration_t *reg;
  punt_type_t pt;

  if (0 != vl_api_punt_type_decode (mp->type, &pt))
    return;

  reg = vl_api_client_index_to_registration (mp->client_index);
  if (!reg)
    return;

  punt_socket_send_ctx_t ctx = {
    .reg = reg,
    .context = mp->context,
  };

  punt_client_walk (pt, vl_api_punt_socket_send_details, &ctx);
}

static void
vl_api_punt_socket_deregister_t_handler (vl_api_punt_socket_deregister_t * mp)
{
  vl_api_punt_socket_deregister_reply_t *rmp;
  vlib_main_t *vm = vlib_get_main ();
  clib_error_t *error;
  punt_reg_t pr;
  int rv;

  rv = vl_api_punt_decode (&mp->punt, &pr);

  if (rv)
    goto out;

  error = vnet_punt_socket_del (vm, &pr);
  if (error)
    {
      rv = -1;
      clib_error_report (error);
    }

out:
  REPLY_MACRO (VL_API_PUNT_SOCKET_DEREGISTER_REPLY);
}

typedef struct punt_reason_dump_walk_ctx_t_
{
  vl_api_registration_t *reg;
  u32 context;
  u8 *name;
} punt_reason_dump_walk_ctx_t;

static int
punt_reason_dump_walk_cb (vlib_punt_reason_t id, const u8 * name, void *args)
{
  punt_reason_dump_walk_ctx_t *ctx = args;
  vl_api_punt_reason_details_t *mp;

  if (ctx->name)
    {
      /* user requested a specific punt-reason */
      if (vec_cmp (name, ctx->name))
	/* not the reasonn we're lookgin for */
	return 1;
    }

  mp = vl_msg_api_alloc (sizeof (*mp) + vec_len (name));
  if (!mp)
    return (0);

  clib_memset (mp, 0, sizeof (*mp));
  mp->_vl_msg_id = ntohs (REPLY_MSG_ID_BASE + VL_API_PUNT_REASON_DETAILS);

  mp->context = ctx->context;
  mp->reason.id = clib_host_to_net_u32 (id);
  vl_api_vec_to_api_string (name, &mp->reason.name);

  vl_api_send_msg (ctx->reg, (u8 *) mp);

  return (1);
}

static void
vl_api_punt_reason_dump_t_handler (vl_api_punt_reason_dump_t * mp)
{
  vl_api_registration_t *reg;

  reg = vl_api_client_index_to_registration (mp->client_index);
  if (!reg)
    return;

  punt_reason_dump_walk_ctx_t ctx = {
    .reg = reg,
    .context = mp->context,
    .name = vl_api_from_api_to_new_vec (mp, &mp->reason.name),
  };

  punt_reason_walk (punt_reason_dump_walk_cb, &ctx);

  vec_free (ctx.name);
}

#include <vnet/ip/punt.api.c>

static clib_error_t *
punt_api_hookup (vlib_main_t * vm)
{
  /*
   * Set up the (msg_name, crc, message-id) table
   */
  REPLY_MSG_ID_BASE = setup_message_id_table ();

  return 0;
}

VLIB_API_INIT_FUNCTION (punt_api_hookup);


/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */
header size in bytes (may be zero) @return 0 or 1 */ uword clib_mem_is_vec_h (void *v, uword header_bytes); /** \brief Predicate function, says whether the supplied vector is a clib heap object @param v pointer to a vector @return 0 or 1 */ always_inline uword clib_mem_is_vec (void *v) { return clib_mem_is_vec_h (v, 0); } /* Local variable naming macro (prevents collisions with other macro naming). */ #define _v(var) _vec_##var /** \brief Resize a vector (general version). Add N elements to end of given vector V, return pointer to start of vector. Vector will have room for H header bytes and will have user's data aligned at alignment A (rounded to next power of 2). @param V pointer to a vector @param N number of elements to add @param H header size in bytes (may be zero) @param A alignment (may be zero) @param S numa_id (may be zero) @return V (value-result macro parameter) */ #define vec_resize_has(V,N,H,A,S) \ do { \ word _v(n) = (N); \ word _v(l) = vec_len (V); \ V = _vec_resize_numa ((V), _v(n), \ (_v(l) + _v(n)) * sizeof ((V)[0]), \ (H), (A),(S)); \ } while (0) /** \brief Resize a vector (less general version). Add N elements to end of given vector V, return pointer to start of vector. Vector will have room for H header bytes and will have user's data aligned at alignment A (rounded to next power of 2). @param V pointer to a vector @param N number of elements to add @param H header size in bytes (may be zero) @param A alignment (may be zero) @return V (value-result macro parameter) */ #define vec_resize_ha(V,N,H,A) vec_resize_has(V,N,H,A,VEC_NUMA_UNSPECIFIED) /** \brief Resize a vector (no header, unspecified alignment) Add N elements to end of given vector V, return pointer to start of vector. Vector will have room for H header bytes and will have user's data aligned at alignment A (rounded to next power of 2). @param V pointer to a vector @param N number of elements to add @return V (value-result macro parameter) */ #define vec_resize(V,N) vec_resize_ha(V,N,0,0) /** \brief Resize a vector (no header, alignment specified). Add N elements to end of given vector V, return pointer to start of vector. Vector will have room for H header bytes and will have user's data aligned at alignment A (rounded to next power of 2). @param V pointer to a vector @param N number of elements to add @param A alignment (may be zero) @return V (value-result macro parameter) */ #define vec_resize_aligned(V,N,A) vec_resize_ha(V,N,0,A) /** \brief Allocate space for N more elements @param V pointer to a vector @param N number of elements to add @param H header size in bytes (may be zero) @param A alignment (may be zero) @return V (value-result macro parameter) */ #define vec_alloc_ha(V,N,H,A) \ do { \ uword _v(l) = vec_len (V); \ vec_resize_ha (V, N, H, A); \ _vec_len (V) = _v(l); \ } while (0) /** \brief Allocate space for N more elements (no header, unspecified alignment) @param V pointer to a vector @param N number of elements to add @return V (value-result macro parameter) */ #define vec_alloc(V,N) vec_alloc_ha(V,N,0,0) /** \brief Allocate space for N more elements (no header, given alignment) @param V pointer to a vector @param N number of elements to add @param A alignment (may be zero) @return V (value-result macro parameter) */ #define vec_alloc_aligned(V,N,A) vec_alloc_ha(V,N,0,A) /** \brief Create new vector of given type and length (general version). @param T type of elements in new vector @param N number of elements to add @param H header size in bytes (may be zero) @param A alignment (may be zero) @return V new vector */ #define vec_new_ha(T,N,H,A) \ ({ \ word _v(n) = (N); \ (T *)_vec_resize ((T *) 0, _v(n), _v(n) * sizeof (T), (H), (A)); \ }) /** \brief Create new vector of given type and length (unspecified alignment, no header). @param T type of elements in new vector @param N number of elements to add @return V new vector */ #define vec_new(T,N) vec_new_ha(T,N,0,0) /** \brief Create new vector of given type and length (alignment specified, no header). @param T type of elements in new vector @param N number of elements to add @param A alignment (may be zero) @return V new vector */ #define vec_new_aligned(T,N,A) vec_new_ha(T,N,0,A) /** \brief Free vector's memory (general version) @param V pointer to a vector @param H size of header in bytes @return V (value-result parameter, V=0) */ #define vec_free_h(V,H) \ do { \ if (V) \ { \ clib_mem_free (vec_header ((V), (H))); \ V = 0; \ } \ } while (0) /** \brief Free vector's memory (no header). @param V pointer to a vector @return V (value-result parameter, V=0) */ #define vec_free(V) vec_free_h(V,0) /**\brief Free vector user header (syntactic sugar) @param h vector header @void */ #define vec_free_header(h) clib_mem_free (h) /** \brief Return copy of vector (general version). @param V pointer to a vector @param H size of header in bytes @param A alignment (may be zero) @param S numa (may be VEC_NUMA_UNSPECIFIED) @return Vdup copy of vector */ #define vec_dup_ha_numa(V,H,A,S) \ ({ \ __typeof__ ((V)[0]) * _v(v) = 0; \ uword _v(l) = vec_len (V); \ if (_v(l) > 0) \ { \ vec_resize_has (_v(v), _v(l), (H), (A), (S)); \ clib_memcpy_fast (_v(v), (V), _v(l) * sizeof ((V)[0]));\ } \ _v(v); \ }) /** \brief Return copy of vector (VEC_NUMA_UNSPECIFIED). @param V pointer to a vector @param H size of header in bytes @param A alignment (may be zero) @return Vdup copy of vector */ #define vec_dup_ha(V,H,A) \ vec_dup_ha_numa(V,H,A,VEC_NUMA_UNSPECIFIED) /** \brief Return copy of vector (no header, no alignment) @param V pointer to a vector @return Vdup copy of vector */ #define vec_dup(V) vec_dup_ha(V,0,0) /** \brief Return copy of vector (no header, alignment specified). @param V pointer to a vector @param A alignment (may be zero) @return Vdup copy of vector */ #define vec_dup_aligned(V,A) vec_dup_ha(V,0,A) /** \brief Copy a vector, memcpy wrapper. Assumes sizeof(SRC[0]) == sizeof(DST[0]) @param DST destination @param SRC source */ #define vec_copy(DST,SRC) clib_memcpy_fast (DST, SRC, vec_len (DST) * \ sizeof ((DST)[0])) /** \brief Clone a vector. Make a new vector with the same size as a given vector but possibly with a different type. @param NEW_V pointer to new vector @param OLD_V pointer to old vector */ #define vec_clone(NEW_V,OLD_V) \ do { \ (NEW_V) = 0; \ (NEW_V) = _vec_resize ((NEW_V), vec_len (OLD_V), \ vec_len (OLD_V) * sizeof ((NEW_V)[0]), (0), (0)); \ } while (0) /** \brief Make sure vector is long enough for given index (general version). @param V (possibly NULL) pointer to a vector. @param I vector index which will be valid upon return @param H header size in bytes (may be zero) @param A alignment (may be zero) @param N numa_id (may be zero) @return V (value-result macro parameter) */ #define vec_validate_han(V,I,H,A,N) \ do { \ void *oldheap; \ STATIC_ASSERT(A==0 || ((A % sizeof(V[0]))==0) \ || ((sizeof(V[0]) % A) == 0), \ "vector validate aligned on incorrectly sized object"); \ word _v(i) = (I); \ word _v(l) = vec_len (V); \ if (_v(i) >= _v(l)) \ { \ /* switch to the per-numa heap if directed */ \ if (PREDICT_FALSE(N != VEC_NUMA_UNSPECIFIED)) \ { \ oldheap = clib_mem_get_per_cpu_heap(); \ clib_mem_set_per_cpu_heap (clib_mem_get_per_numa_heap(N)); \ } \ \ vec_resize_ha ((V), 1 + (_v(i) - _v(l)), (H), (A)); \ /* Must zero new space since user may have previously \ used e.g. _vec_len (v) -= 10 */ \ clib_memset ((V) + _v(l), 0, \ (1 + (_v(i) - _v(l))) * sizeof ((V)[0])); \ /* Switch back to the global heap */ \ if (PREDICT_FALSE (N != VEC_NUMA_UNSPECIFIED)) \ clib_mem_set_per_cpu_heap (oldheap); \ } \ } while (0) #define vec_validate_ha(V,I,H,A) vec_validate_han(V,I,H,A,VEC_NUMA_UNSPECIFIED) /** \brief Make sure vector is long enough for given index (no header, unspecified alignment) @param V (possibly NULL) pointer to a vector. @param I vector index which will be valid upon return @return V (value-result macro parameter) */ #define vec_validate(V,I) vec_validate_ha(V,I,0,0) /** \brief Make sure vector is long enough for given index (no header, specified alignment) @param V (possibly NULL) pointer to a vector. @param I vector index which will be valid upon return @param A alignment (may be zero) @return V (value-result macro parameter) */ #define vec_validate_aligned(V,I,A) vec_validate_ha(V,I,0,A) /** \brief Make sure vector is long enough for given index and initialize empty space (general version) @param V (possibly NULL) pointer to a vector. @param I vector index which will be valid upon return @param INIT initial value (can be a complex expression!) @param H header size in bytes (may be zero) @param A alignment (may be zero) @return V (value-result macro parameter) */ #define vec_validate_init_empty_ha(V,I,INIT,H,A) \ do { \ word _v(i) = (I); \ word _v(l) = vec_len (V); \ if (_v(i) >= _v(l)) \ { \ vec_resize_ha ((V), 1 + (_v(i) - _v(l)), (H), (A)); \ while (_v(l) <= _v(i)) \ { \ (V)[_v(l)] = (INIT); \ _v(l)++; \ } \ } \ } while (0) /** \brief Make sure vector is long enough for given index and initialize empty space (no header, unspecified alignment) @param V (possibly NULL) pointer to a vector. @param I vector index which will be valid upon return @param INIT initial value (can be a complex expression!) @return V (value-result macro parameter) */ #define vec_validate_init_empty(V,I,INIT) \ vec_validate_init_empty_ha(V,I,INIT,0,0) /** \brief Make sure vector is long enough for given index and initialize empty space (no header, alignment alignment) @param V (possibly NULL) pointer to a vector. @param I vector index which will be valid upon return @param INIT initial value (can be a complex expression!) @param A alignment (may be zero) @return V (value-result macro parameter) */ #define vec_validate_init_empty_aligned(V,I,INIT,A) \ vec_validate_init_empty_ha(V,I,INIT,0,A) /** \brief Add 1 element to end of vector (general version). @param V pointer to a vector @param E element to add @param H header size in bytes (may be zero) @param A alignment (may be zero) @return V (value-result macro parameter) */ #define vec_add1_ha(V,E,H,A) \ do { \ word _v(l) = vec_len (V); \ V = _vec_resize ((V), 1, (_v(l) + 1) * sizeof ((V)[0]), (H), (A)); \ (V)[_v(l)] = (E); \ } while (0) /** \brief Add 1 element to end of vector (unspecified alignment). @param V pointer to a vector @param E element to add @return V (value-result macro parameter) */ #define vec_add1(V,E) vec_add1_ha(V,E,0,0) /** \brief Add 1 element to end of vector (alignment specified). @param V pointer to a vector @param E element to add @param A alignment (may be zero) @return V (value-result macro parameter) */ #define vec_add1_aligned(V,E,A) vec_add1_ha(V,E,0,A) /** \brief Add N elements to end of vector V, return pointer to new elements in P. (general version) @param V pointer to a vector @param P pointer to new vector element(s) @param N number of elements to add @param H header size in bytes (may be zero) @param A alignment (may be zero) @return V and P (value-result macro parameters) */ #define vec_add2_ha(V,P,N,H,A) \ do { \ word _v(n) = (N); \ word _v(l) = vec_len (V); \ V = _vec_resize ((V), _v(n), (_v(l) + _v(n)) * sizeof ((V)[0]), (H), (A)); \ P = (V) + _v(l); \ } while (0) /** \brief Add N elements to end of vector V, return pointer to new elements in P. (no header, unspecified alignment) @param V pointer to a vector @param P pointer to new vector element(s) @param N number of elements to add @return V and P (value-result macro parameters) */ #define vec_add2(V,P,N) vec_add2_ha(V,P,N,0,0) /** \brief Add N elements to end of vector V, return pointer to new elements in P. (no header, alignment specified) @param V pointer to a vector @param P pointer to new vector element(s) @param N number of elements to add @param A alignment (may be zero) @return V and P (value-result macro parameters) */ #define vec_add2_aligned(V,P,N,A) vec_add2_ha(V,P,N,0,A) /** \brief Add N elements to end of vector V (general version) @param V pointer to a vector @param E pointer to element(s) to add @param N number of elements to add @param H header size in bytes (may be zero) @param A alignment (may be zero) @return V (value-result macro parameter) */ #define vec_add_ha(V,E,N,H,A) \ do { \ word _v(n) = (N); \ word _v(l) = vec_len (V); \ V = _vec_resize ((V), _v(n), (_v(l) + _v(n)) * sizeof ((V)[0]), (H), (A)); \ clib_memcpy_fast ((V) + _v(l), (E), _v(n) * sizeof ((V)[0])); \ } while (0) /** \brief Add N elements to end of vector V (no header, unspecified alignment) @param V pointer to a vector @param E pointer to element(s) to add @param N number of elements to add @return V (value-result macro parameter) */ #define vec_add(V,E,N) vec_add_ha(V,E,N,0,0) /** \brief Add N elements to end of vector V (no header, specified alignment) @param V pointer to a vector @param E pointer to element(s) to add @param N number of elements to add @param A alignment (may be zero) @return V (value-result macro parameter) */ #define vec_add_aligned(V,E,N,A) vec_add_ha(V,E,N,0,A) /** \brief Returns last element of a vector and decrements its length @param V pointer to a vector @return E element removed from the end of the vector */ #define vec_pop(V) \ ({ \ uword _v(l) = vec_len (V); \ ASSERT (_v(l) > 0); \ _v(l) -= 1; \ _vec_len (V) = _v (l); \ (V)[_v(l)]; \ }) /** \brief Set E to the last element of a vector, decrement vector length @param V pointer to a vector @param E pointer to the last vector element @return E element removed from the end of the vector (value-result macro parameter */ #define vec_pop2(V,E) \ ({ \ uword _v(l) = vec_len (V); \ if (_v(l) > 0) (E) = vec_pop (V); \ _v(l) > 0; \ }) /** \brief Insert N vector elements starting at element M, initialize new elements (general version). @param V (possibly NULL) pointer to a vector. @param N number of elements to insert @param M insertion point @param INIT initial value (can be a complex expression!) @param H header size in bytes (may be zero) @param A alignment (may be zero) @return V (value-result macro parameter) */ #define vec_insert_init_empty_ha(V,N,M,INIT,H,A) \ do { \ word _v(l) = vec_len (V); \ word _v(n) = (N); \ word _v(m) = (M); \ V = _vec_resize ((V), \ _v(n), \ (_v(l) + _v(n))*sizeof((V)[0]), \ (H), (A)); \ ASSERT (_v(m) <= _v(l)); \ memmove ((V) + _v(m) + _v(n), \ (V) + _v(m), \ (_v(l) - _v(m)) * sizeof ((V)[0])); \ clib_memset ((V) + _v(m), INIT, _v(n) * sizeof ((V)[0])); \ } while (0) /** \brief Insert N vector elements starting at element M, initialize new elements to zero (general version) @param V (possibly NULL) pointer to a vector. @param N number of elements to insert @param M insertion point @param H header size in bytes (may be zero) @param A alignment (may be zero) @return V (value-result macro parameter) */ #define vec_insert_ha(V,N,M,H,A) vec_insert_init_empty_ha(V,N,M,0,H,A) /** \brief Insert N vector elements starting at element M, initialize new elements to zero (no header, unspecified alignment) @param V (possibly NULL) pointer to a vector. @param N number of elements to insert @param M insertion point @return V (value-result macro parameter) */ #define vec_insert(V,N,M) vec_insert_ha(V,N,M,0,0) /** \brief Insert N vector elements starting at element M, initialize new elements to zero (no header, alignment specified) @param V (possibly NULL) pointer to a vector. @param N number of elements to insert @param M insertion point @param A alignment (may be zero) @return V (value-result macro parameter) */ #define vec_insert_aligned(V,N,M,A) vec_insert_ha(V,N,M,0,A) /** \brief Insert N vector elements starting at element M, initialize new elements (no header, unspecified alignment) @param V (possibly NULL) pointer to a vector. @param N number of elements to insert @param M insertion point @param INIT initial value (can be a complex expression!) @return V (value-result macro parameter) */ #define vec_insert_init_empty(V,N,M,INIT) \ vec_insert_init_empty_ha(V,N,M,INIT,0,0) /* Resize vector by N elements starting from element M, initialize new elements to INIT (alignment specified, no header). */ /** \brief Insert N vector elements starting at element M, initialize new elements (no header, specified alignment) @param V (possibly NULL) pointer to a vector. @param N number of elements to insert @param M insertion point @param INIT initial value (can be a complex expression!) @param A alignment (may be zero) @return V (value-result macro parameter) */ #define vec_insert_init_empty_aligned(V,N,M,INIT,A) \ vec_insert_init_empty_ha(V,N,M,INIT,0,A) /** \brief Insert N vector elements starting at element M, insert given elements (general version) @param V (possibly NULL) pointer to a vector. @param E element(s) to insert @param N number of elements to insert @param M insertion point @param H header size in bytes (may be zero) @param A alignment (may be zero) @return V (value-result macro parameter) */ #define vec_insert_elts_ha(V,E,N,M,H,A) \ do { \ word _v(l) = vec_len (V); \ word _v(n) = (N); \ word _v(m) = (M); \ V = _vec_resize ((V), \ _v(n), \ (_v(l) + _v(n))*sizeof((V)[0]), \ (H), (A)); \ ASSERT (_v(m) <= _v(l)); \ memmove ((V) + _v(m) + _v(n), \ (V) + _v(m), \ (_v(l) - _v(m)) * sizeof ((V)[0])); \ clib_memcpy_fast ((V) + _v(m), (E), \ _v(n) * sizeof ((V)[0])); \ } while (0) /** \brief Insert N vector elements starting at element M, insert given elements (no header, unspecified alignment) @param V (possibly NULL) pointer to a vector. @param E element(s) to insert @param N number of elements to insert @param M insertion point @return V (value-result macro parameter) */ #define vec_insert_elts(V,E,N,M) vec_insert_elts_ha(V,E,N,M,0,0) /** \brief Insert N vector elements starting at element M, insert given elements (no header, specified alignment) @param V (possibly NULL) pointer to a vector. @param E element(s) to insert @param N number of elements to insert @param M insertion point @param A alignment (may be zero) @return V (value-result macro parameter) */ #define vec_insert_elts_aligned(V,E,N,M,A) vec_insert_elts_ha(V,E,N,M,0,A) /** \brief Delete N elements starting at element M @param V pointer to a vector @param N number of elements to delete @param M first element to delete @return V (value-result macro parameter) */ #define vec_delete(V,N,M) \ do { \ word _v(l) = vec_len (V); \ word _v(n) = (N); \ word _v(m) = (M); \ /* Copy over deleted elements. */ \ if (_v(l) - _v(n) - _v(m) > 0) \ memmove ((V) + _v(m), (V) + _v(m) + _v(n), \ (_v(l) - _v(n) - _v(m)) * sizeof ((V)[0])); \ /* Zero empty space at end (for future re-allocation). */ \ if (_v(n) > 0) \ clib_memset ((V) + _v(l) - _v(n), 0, _v(n) * sizeof ((V)[0])); \ _vec_len (V) -= _v(n); \ CLIB_MEM_POISON(vec_end(V), _v(n) * sizeof ((V)[0])); \ } while (0) /** \brief Delete the element at index I @param V pointer to a vector @param I index to delete */ #define vec_del1(v,i) \ do { \ uword _vec_del_l = _vec_len (v) - 1; \ uword _vec_del_i = (i); \ if (_vec_del_i < _vec_del_l) \ (v)[_vec_del_i] = (v)[_vec_del_l]; \ _vec_len (v) = _vec_del_l; \ CLIB_MEM_POISON(vec_end(v), sizeof ((v)[0])); \ } while (0) /** \brief Append v2 after v1. Result in v1. @param V1 target vector @param V2 vector to append */ #define vec_append(v1,v2) \ do { \ uword _v(l1) = vec_len (v1); \ uword _v(l2) = vec_len (v2); \ \ v1 = _vec_resize ((v1), _v(l2), \ (_v(l1) + _v(l2)) * sizeof ((v1)[0]), 0, 0); \ clib_memcpy_fast ((v1) + _v(l1), (v2), _v(l2) * sizeof ((v2)[0])); \ } while (0) /** \brief Append v2 after v1. Result in v1. Specified alignment. @param V1 target vector @param V2 vector to append @param align required alignment */ #define vec_append_aligned(v1,v2,align) \ do { \ uword _v(l1) = vec_len (v1); \ uword _v(l2) = vec_len (v2); \ \ v1 = _vec_resize ((v1), _v(l2), \ (_v(l1) + _v(l2)) * sizeof ((v1)[0]), 0, align); \ clib_memcpy_fast ((v1) + _v(l1), (v2), _v(l2) * sizeof ((v2)[0])); \ } while (0) /** \brief Prepend v2 before v1. Result in v1. @param V1 target vector @param V2 vector to prepend */ #define vec_prepend(v1,v2) \ do { \ uword _v(l1) = vec_len (v1); \ uword _v(l2) = vec_len (v2); \ \ v1 = _vec_resize ((v1), _v(l2), \ (_v(l1) + _v(l2)) * sizeof ((v1)[0]), 0, 0); \ memmove ((v1) + _v(l2), (v1), _v(l1) * sizeof ((v1)[0])); \ clib_memcpy_fast ((v1), (v2), _v(l2) * sizeof ((v2)[0])); \ } while (0) /** \brief Prepend v2 before v1. Result in v1. Specified alignment @param V1 target vector @param V2 vector to prepend @param align required alignment */ #define vec_prepend_aligned(v1,v2,align) \ do { \ uword _v(l1) = vec_len (v1); \ uword _v(l2) = vec_len (v2); \ \ v1 = _vec_resize ((v1), _v(l2), \ (_v(l1) + _v(l2)) * sizeof ((v1)[0]), 0, align); \ memmove ((v1) + _v(l2), (v1), _v(l1) * sizeof ((v1)[0])); \ clib_memcpy_fast ((v1), (v2), _v(l2) * sizeof ((v2)[0])); \ } while (0) /** \brief Zero all vector elements. Null-pointer tolerant. @param var Vector to zero */ #define vec_zero(var) \ do { \ if (var) \ clib_memset ((var), 0, vec_len (var) * sizeof ((var)[0])); \ } while (0) /** \brief Set all vector elements to given value. Null-pointer tolerant. @param v vector to set @param val value for each vector element */ #define vec_set(v,val) \ do { \ word _v(i); \ __typeof__ ((v)[0]) _val = (val); \ for (_v(i) = 0; _v(i) < vec_len (v); _v(i)++) \ (v)[_v(i)] = _val; \ } while (0) #ifdef CLIB_UNIX #include <stdlib.h> /* for qsort */ #endif /** \brief Compare two vectors, not NULL-pointer tolerant @param v1 Pointer to a vector @param v2 Pointer to a vector @return 1 if equal, 0 if unequal */ #define vec_is_equal(v1,v2) \ (vec_len (v1) == vec_len (v2) && ! memcmp ((v1), (v2), vec_len (v1) * sizeof ((v1)[0]))) /** \brief Compare two vectors (only applicable to vectors of signed numbers). Used in qsort compare functions. @param v1 Pointer to a vector @param v2 Pointer to a vector @return -1, 0, +1 */ #define vec_cmp(v1,v2) \ ({ \ word _v(i), _v(cmp), _v(l); \ _v(l) = clib_min (vec_len (v1), vec_len (v2)); \ _v(cmp) = 0; \ for (_v(i) = 0; _v(i) < _v(l); _v(i)++) { \ _v(cmp) = (v1)[_v(i)] - (v2)[_v(i)]; \ if (_v(cmp)) \ break; \ } \ if (_v(cmp) == 0 && _v(l) > 0) \ _v(cmp) = vec_len(v1) - vec_len(v2); \ (_v(cmp) < 0 ? -1 : (_v(cmp) > 0 ? +1 : 0)); \ }) /** \brief Search a vector for the index of the entry that matches. @param v Pointer to a vector @param E Entry to match @return index of match or ~0 */ #define vec_search(v,E) \ ({ \ word _v(i) = 0; \ while (_v(i) < vec_len(v)) \ { \ if ((v)[_v(i)] == E) \ break; \ _v(i)++; \ } \ if (_v(i) == vec_len(v)) \ _v(i) = ~0; \ _v(i); \ }) /** \brief Search a vector for the index of the entry that matches. @param v Pointer to a vector @param E Pointer to entry to match @param fn Comparison function !0 => match @return index of match or ~0 */ #define vec_search_with_function(v,E,fn) \ ({ \ word _v(i) = 0; \ while (_v(i) < vec_len(v)) \ { \ if (0 != fn(&(v)[_v(i)], (E))) \ break; \ _v(i)++; \ } \ if (_v(i) == vec_len(v)) \ _v(i) = ~0; \ _v(i); \ }) /** \brief Sort a vector using the supplied element comparison function Does not depend on the underlying implementation to deal correctly with null, zero-long, or 1-long vectors @param vec vector to sort @param f comparison function */ #define vec_sort_with_function(vec,f) \ do { \ if (vec_len (vec) > 1) \ qsort (vec, vec_len (vec), sizeof (vec[0]), (void *) (f)); \ } while (0) /** \brief Make a vector containing a NULL terminated c-string. @param V (possibly NULL) pointer to a vector. @param S pointer to string buffer. @param L string length (NOT including the terminating NULL; a la strlen()) */ #define vec_validate_init_c_string(V, S, L) \ do { \ vec_reset_length (V); \ vec_validate ((V), (L)); \ if ((S) && (L)) \ clib_memcpy_fast ((V), (S), (L)); \ (V)[(L)] = 0; \ } while (0) /** \brief Test whether a vector is a NULL terminated c-string. @param V (possibly NULL) pointer to a vector. @return BOOLEAN indicating if the vector c-string is null terminated. */ #define vec_c_string_is_terminated(V) \ (((V) != 0) && (vec_len (V) != 0) && ((V)[vec_len ((V)) - 1] == 0)) /** \brief (If necessary) NULL terminate a vector containing a c-string. @param V (possibly NULL) pointer to a vector. @return V (value-result macro parameter) */ #define vec_terminate_c_string(V) \ do { \ u32 vl = vec_len ((V)); \ if (!vec_c_string_is_terminated(V)) \ { \ vec_validate ((V), vl); \ (V)[vl] = 0; \ } \ } while (0) #endif /* included_vec_h */ /* * fd.io coding-style-patch-verification: ON * * Local Variables: * eval: (c-set-style "gnu") * End: */