2 * Copyright (c) 2019 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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include <vlib/vlib.h>
18 u8 *vlib_validate_buffers (vlib_main_t * vm,
20 uword next_buffer_stride,
22 vlib_buffer_known_state_t known_state,
23 uword follow_buffer_next);
26 test_vlib_command_fn (vlib_main_t * vm,
27 unformat_input_t * input, vlib_cli_command_t * cmd)
33 vlib_buffer_t *last_b;
34 u8 junk[4] = { 1, 2, 3, 4 };
35 vlib_packet_template_t _t, *t = &_t;
38 /* Cover vlib_packet_template_get_packet */
39 t->packet_data = format (0, "silly packet data");
40 t->min_n_buffers_each_alloc = 1;
41 t->name = (u8 *) "test template";
43 if (vlib_packet_template_get_packet (vm, t, &bi))
44 vlib_buffer_free_one (vm, bi);
46 vec_free (t->packet_data);
49 allocated = vlib_buffer_alloc (vm, &bi, 1);
51 return clib_error_return (0, "Buffer allocation failure!");
53 b = vlib_get_buffer (vm, bi);
55 /* Force buffer allocation */
56 b->current_length = 2048;
58 vlib_buffer_chain_append_data_with_alloc (vm, b, &last_b,
59 junk, ARRAY_LEN (junk));
61 /* Cover vlib_buffer_length_in_chain_slow_path(...) */
62 b->flags &= ~(VLIB_BUFFER_TOTAL_LENGTH_VALID);
63 vlib_cli_output (vm, "buffer length %d",
64 vlib_buffer_length_in_chain (vm, b));
65 b->flags &= ~(VLIB_BUFFER_TOTAL_LENGTH_VALID);
66 vlib_cli_output (vm, "%u", vlib_buffer_index_length_in_chain (vm, bi));
68 /* Add more data. Eat Mor Chikin. */
69 vlib_buffer_add_data (vm, &bi, junk, ARRAY_LEN (junk));
71 /* Dump the resulting two-chunk pkt */
72 vlib_cli_output (vm, "%U", format_vlib_buffer_and_data, b);
74 vec_validate (data_copy, vlib_buffer_length_in_chain (vm, b) - 1);
75 vlib_cli_output (vm, "%u", vlib_buffer_contents (vm, bi, data_copy));
78 /* Cover simple functions in buffer.h / buffer_funcs.h */
79 vlib_cli_output (vm, "%llx", vlib_buffer_get_va (b));
80 vlib_cli_output (vm, "%llx", vlib_buffer_get_current_va (b));
81 vlib_cli_output (vm, "%d", vlib_buffer_has_space (b, 100ll));
82 vlib_buffer_reset (b);
83 vlib_cli_output (vm, "%llx", vlib_buffer_get_tail (b));
84 vlib_buffer_put_uninit (b, 0);
85 vlib_buffer_push_uninit (b, 0);
86 vlib_buffer_make_headroom (b, 0);
87 (void) vlib_buffer_pull (b, 0);
88 vlib_cli_output (vm, "%llx", vlib_buffer_get_pa (vm, b));
89 vlib_cli_output (vm, "%llx", vlib_buffer_get_current_pa (vm, b));
91 /* Validate it one way */
92 res = vlib_validate_buffer (vm, bi, 1 /* follow_buffer_next */ );
94 return clib_error_return (0, "%v", res);
96 /* Validate it a different way */
97 res = vlib_validate_buffers (vm, &bi, 0 /* stride */ ,
98 1, VLIB_BUFFER_KNOWN_ALLOCATED,
99 1 /* follow_buffer_next */ );
101 return clib_error_return (0, "%v", res);
104 vlib_buffer_free_one (vm, bi);
105 /* It will be free */
106 res = vlib_validate_buffers (vm, &bi, 0 /* stride */ ,
107 1, VLIB_BUFFER_KNOWN_FREE,
108 1 /* follow_buffer_next */ );
110 return clib_error_return (0, "%v", res);
112 /* It will not be allocated, exercise error path */
113 res = vlib_validate_buffers (vm, &bi, 0 /* stride */ ,
114 1, VLIB_BUFFER_KNOWN_ALLOCATED,
115 1 /* follow_buffer_next */ );
117 return clib_error_return (0, "%v", res);
123 VLIB_CLI_COMMAND (test_vlib_command, static) =
126 .short_help = "vlib code coverate unit test",
127 .function = test_vlib_command_fn,
132 * fd.io coding-style-patch-verification: ON
135 * eval: (c-set-style "gnu")