Dave Barach | b44e9bc | 2016-02-19 09:06:23 -0500 | [diff] [blame] | 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 | |
| 26 | u8 * vlib_node_serialize (vlib_node_main_t *nm, u8 * vector) |
| 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 | |
| 35 | serialize_likely_small_unsigned_integer (sm, vec_len(nm->nodes)); |
| 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); |
| 43 | serialize_likely_small_unsigned_integer (sm, vec_len(node->next_nodes)); |
| 44 | for (j = 0; j < vec_len (node->next_nodes); j++) |
| 45 | serialize_likely_small_unsigned_integer (sm, node->next_nodes[j]); |
| 46 | } |
| 47 | vec_free(cstemp); |
| 48 | |
| 49 | return (serialize_close_vector (sm)); |
| 50 | } |
| 51 | |
| 52 | vlib_node_t ** vlib_node_unserialize (u8 * vector) |
| 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 | |
| 62 | nnodes = unserialize_likely_small_unsigned_integer (sm); |
| 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 | |
| 73 | nnexts = unserialize_likely_small_unsigned_integer (sm); |
| 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] = |
| 78 | unserialize_likely_small_unsigned_integer (sm); |
| 79 | } |
| 80 | return nodes; |
| 81 | } |
| 82 | |
| 83 | |
| 84 | #if CLIB_DEBUG > 0 |
| 85 | |
| 86 | static clib_error_t * |
| 87 | test_node_serialize_command_fn (vlib_main_t * vm, |
| 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 |