FD.io VPP  v16.06
Vector Packet Processing
node_serialize.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include <vlib/vlib.h>
16 
17 #include <vppinfra/serialize.h>
18 
19 /*
20  * Serialize a vlib_node_main_t. Appends the result to vector.
21  * Pass 0 to create a new vector, use vec_reset_length(vector)
22  * to recycle a vector / avoid memory allocation, etc.
23  * Switch heaps before/after to serialize into API client shared memory.
24  */
25 
27 {
28  serialize_main_t _sm, *sm=&_sm;
29  vlib_node_t * node;
30  int i, j;
31  u8 * cstemp = 0;
32 
33  serialize_open_vector (sm, vector);
34 
36  for (i = 0; i < vec_len (nm->nodes); i++)
37  {
38  node = nm->nodes[i];
39  vec_reset_length (cstemp);
40  cstemp = vec_dup(node->name);
41  vec_add1(cstemp, 0);
42  serialize_cstring (sm, (char *)cstemp);
44  for (j = 0; j < vec_len (node->next_nodes); j++)
46  }
47  vec_free(cstemp);
48 
49  return (serialize_close_vector (sm));
50 }
51 
53 {
54  serialize_main_t _sm, *sm=&_sm;
55  u32 nnodes, nnexts;
56  vlib_node_t * node;
57  vlib_node_t ** nodes = 0;
58  int i, j;
59 
60  serialize_open_vector (sm, vector);
61 
63 
64  vec_validate (nodes, nnodes-1);
65 
66  for (i = 0; i < nnodes; i++)
67  {
68  node = 0;
69  vec_validate (node,0);
70  nodes[i] = node;
71  unserialize_cstring (sm, (char **)&node->name);
72 
74  if (nnexts > 0)
75  vec_validate (node->next_nodes, nnexts-1);
76  for (j = 0; j < vec_len (node->next_nodes); j++)
77  node->next_nodes[j] =
79  }
80  return nodes;
81 }
82 
83 
84 #if CLIB_DEBUG > 0
85 
86 static clib_error_t *
88  unformat_input_t * input,
89  vlib_cli_command_t * cmd)
90 {
91  vlib_node_main_t * nm = &vm->node_main;
92  u8 * vector = 0;
93  vlib_node_t ** nodes;
94  vlib_node_t * node;
95  vlib_node_t * next_node;
96  int i, j;
97 
98  /*
99  * Keep the number of memcpy ops to a minimum (e.g. 1).
100  * The current size of the serialized vector is
101  * slightly under 4K.
102  */
103  vec_validate (vector, 4095);
104  vec_reset_length (vector);
105 
106  vector = vlib_node_serialize (nm, vector);
107 
108  nodes = vlib_node_unserialize (vector);
109 
110  vec_free (vector);
111 
112  for (i = 0; i < vec_len(nodes); i++)
113  {
114  node = nodes[i];
115 
116  vlib_cli_output (vm, "[%d] %s", i, node->name);
117  for (j = 0; j < vec_len (node->next_nodes); j++)
118  {
119  if (node->next_nodes[j] != ~0)
120  next_node = nodes[node->next_nodes[j]];
121  vlib_cli_output (vm, " [%d] %s", j, next_node->name);
122  }
123  }
124 
125  for (i = 0; i < vec_len(nodes); i++)
126  {
127  vec_free (nodes[i]->name);
128  vec_free (nodes[i]->next_nodes);
129  vec_free (nodes[i]);
130  }
131  vec_free(nodes);
132 
133  return 0;
134 }
135 
136 VLIB_CLI_COMMAND (test_node_serialize_node, static) = {
137  .path = "test node serialize",
138  .short_help = "test node serialize",
139  .function = test_node_serialize_command_fn,
140 };
141 #endif
u32 * next_nodes
Definition: node.h:254
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:394
sll srl srl sll sra u16x4 i
Definition: vector_sse2.h:267
always_inline void serialize_likely_small_unsigned_integer(serialize_main_t *m, u64 x)
Definition: serialize.h:198
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:480
#define vec_reset_length(v)
Reset vector length to zero NULL-pointer tolerant.
vlib_node_t ** nodes
Definition: node.h:570
u8 * vlib_node_serialize(vlib_node_main_t *nm, u8 *vector)
#define vec_dup(V)
Return copy of vector (no header, no alignment)
Definition: vec.h:332
void vlib_cli_output(vlib_main_t *vm, char *fmt,...)
Definition: cli.c:538
void unserialize_cstring(serialize_main_t *m, char **s)
Definition: serialize.c:148
u8 * name
Definition: node.h:187
void serialize_open_vector(serialize_main_t *m, u8 *vector)
Definition: serialize.c:849
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:298
static clib_error_t * test_node_serialize_command_fn(vlib_main_t *vm, unformat_input_t *input, vlib_cli_command_t *cmd)
#define VLIB_CLI_COMMAND(x,...)
Definition: cli.h:150
unsigned int u32
Definition: types.h:88
void serialize_cstring(serialize_main_t *m, char *s)
Definition: serialize.c:135
vlib_node_t ** vlib_node_unserialize(u8 *vector)
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
unsigned char u8
Definition: types.h:56
vlib_node_main_t node_main
Definition: main.h:115
void * serialize_close_vector(serialize_main_t *m)
Definition: serialize.c:858
always_inline u64 unserialize_likely_small_unsigned_integer(serialize_main_t *m)
Definition: serialize.h:234
struct _unformat_input_t unformat_input_t