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>
17 #include <vnet/vnet.h>
19 u8 *vlib_validate_buffers (vlib_main_t * vm,
21 uword next_buffer_stride,
23 vlib_buffer_known_state_t known_state,
24 uword follow_buffer_next);
27 test_vlib_command_fn (vlib_main_t * vm,
28 unformat_input_t * input, vlib_cli_command_t * cmd)
34 vlib_buffer_t *last_b;
35 u8 junk[4] = { 1, 2, 3, 4 };
36 vlib_packet_template_t _t, *t = &_t;
38 vnet_main_t *vnm = vnet_get_main ();
39 vnet_interface_main_t *im = &vnm->interface_main;
41 /* Cover vlib_packet_template_get_packet */
42 t->packet_data = format (0, "silly packet data");
43 t->min_n_buffers_each_alloc = 1;
44 t->name = (u8 *) "test template";
46 if (vlib_packet_template_get_packet (vm, t, &bi))
47 vlib_buffer_free_one (vm, bi);
49 vec_free (t->packet_data);
52 allocated = vlib_buffer_alloc (vm, &bi, 1);
54 return clib_error_return (0, "Buffer allocation failure!");
56 b = vlib_get_buffer (vm, bi);
58 /* Force buffer allocation */
59 b->current_length = 2048;
61 vlib_buffer_chain_append_data_with_alloc (vm, b, &last_b,
62 junk, ARRAY_LEN (junk));
64 /* Cover vlib_buffer_length_in_chain_slow_path(...) */
65 b->flags &= ~(VLIB_BUFFER_TOTAL_LENGTH_VALID);
66 vlib_cli_output (vm, "buffer length %d",
67 vlib_buffer_length_in_chain (vm, b));
68 b->flags &= ~(VLIB_BUFFER_TOTAL_LENGTH_VALID);
69 vlib_cli_output (vm, "%u", vlib_buffer_index_length_in_chain (vm, bi));
71 /* Add more data. Eat Mor Chikin. */
72 vlib_buffer_add_data (vm, &bi, junk, ARRAY_LEN (junk));
74 /* Dump the resulting two-chunk pkt */
75 vlib_cli_output (vm, "%U", format_vlib_buffer_and_data, b);
76 vlib_cli_output (vm, "%U", format_vlib_buffer_data, b->data, 17);
78 vec_validate (data_copy, vlib_buffer_length_in_chain (vm, b) - 1);
79 vlib_cli_output (vm, "%u", vlib_buffer_contents (vm, bi, data_copy));
82 /* Cover simple functions in buffer.h / buffer_funcs.h */
83 vlib_cli_output (vm, "%llx", vlib_buffer_get_va (b));
84 vlib_cli_output (vm, "%llx", vlib_buffer_get_current_va (b));
85 vlib_cli_output (vm, "%d", vlib_buffer_has_space (b, 100ll));
86 vlib_buffer_reset (b);
87 vlib_cli_output (vm, "%llx", vlib_buffer_get_tail (b));
88 vlib_buffer_put_uninit (b, 0);
89 vlib_buffer_push_uninit (b, 0);
90 vlib_buffer_make_headroom (b, 0);
91 (void) vlib_buffer_pull (b, 0);
92 vlib_cli_output (vm, "%llx", vlib_buffer_get_pa (vm, b));
93 vlib_cli_output (vm, "%llx", vlib_buffer_get_current_pa (vm, b));
95 /* Validate it one way */
96 res = vlib_validate_buffer (vm, bi, 1 /* follow_buffer_next */ );
98 return clib_error_return (0, "%v", res);
100 /* Validate it a different way */
101 res = vlib_validate_buffers (vm, &bi, 0 /* stride */ ,
102 1, VLIB_BUFFER_KNOWN_ALLOCATED,
103 1 /* follow_buffer_next */ );
105 return clib_error_return (0, "%v", res);
108 vlib_buffer_free_one (vm, bi);
109 /* It will be free */
110 res = vlib_validate_buffers (vm, &bi, 0 /* stride */ ,
111 1, VLIB_BUFFER_KNOWN_FREE,
112 1 /* follow_buffer_next */ );
114 return clib_error_return (0, "%v", res);
119 vlib_combined_counter_n_counters (im->combined_sw_if_counters));
121 /* buffer will not be allocated at this point, exercise error path */
122 res = vlib_validate_buffers (vm, &bi, 0 /* stride */ ,
123 1, VLIB_BUFFER_KNOWN_ALLOCATED,
124 1 /* follow_buffer_next */ );
126 return clib_error_return (0, "%v", res);
133 VLIB_CLI_COMMAND (test_vlib_command, static) =
136 .short_help = "vlib code coverage unit test",
137 .function = test_vlib_command_fn,
141 static clib_error_t *
142 test_format_vlib_command_fn (vlib_main_t * vm,
143 unformat_input_t * input,
144 vlib_cli_command_t * cmd)
146 unformat_input_t _i, *i = &_i;
147 int enable = -1, disable = -1;
148 int twenty_seven = -1;;
151 memset (i, 0, sizeof (*i));
152 unformat_init_string (i, "enable disable rx tx 27", 23);
154 while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
156 if (unformat (i, "%U", unformat_vlib_enable_disable, &enable))
158 else if (unformat (i, "%U", unformat_vlib_enable_disable, &disable))
160 else if (unformat (i, "%U", unformat_vlib_number, &twenty_seven))
162 else if (unformat (i, "%U", unformat_vlib_rx_tx, &rxtx))
169 vlib_cli_output (vm, "%U", format_vlib_read_write, rxtx);
170 vlib_cli_output (vm, "%U", format_vlib_rx_tx, rxtx);
173 vlib_cli_output (vm, "%U", format_vlib_read_write, rxtx);
174 vlib_cli_output (vm, "%U", format_vlib_rx_tx, rxtx);
176 vlib_cli_output (vm, "%U", format_vlib_read_write, rxtx);
177 vlib_cli_output (vm, "%U", format_vlib_rx_tx, rxtx);
184 VLIB_CLI_COMMAND (test_format_vlib_command, static) =
186 .path = "test format-vlib",
187 .short_help = "vlib format code coverate unit test",
188 .function = test_format_vlib_command_fn,
192 static clib_error_t *
193 test_vlib2_command_fn (vlib_main_t * vm,
194 unformat_input_t * input, vlib_cli_command_t * cmd)
199 s = format (0, "show ");
200 result = vlib_cli_get_possible_completions (s);
206 result = vlib_cli_get_possible_completions (s);
210 s = format (0, "show ?");
211 result = vlib_cli_get_possible_completions (s);
219 VLIB_CLI_COMMAND (test_vlib2_command, static) =
221 .path = "test vlib2",
222 .short_help = "vlib code coverage unit test #2",
223 .function = test_vlib2_command_fn,
232 * fd.io coding-style-patch-verification: ON
235 * eval: (c-set-style "gnu")