+/* *INDENT-ON* */
+
+static inline clib_error_t *
+pcap_trace_command_internal (vlib_main_t * vm,
+ unformat_input_t * input,
+ vlib_cli_command_t * cmd, int rx_tx)
+{
+#define PCAP_DEF_PKT_TO_CAPTURE (1000)
+
+ unformat_input_t _line_input, *line_input = &_line_input;
+ u8 *filename;
+ u8 *chroot_filename = 0;
+ u32 max = 0;
+ int enabled = 0;
+ int errorFlag = 0;
+ clib_error_t *error = 0;
+ vnet_main_t *vnm = vnet_get_main ();
+
+ /* Get a line of input. */
+ if (!unformat_user (input, unformat_line_input, line_input))
+ return 0;
+
+ while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
+ {
+ if (unformat (line_input, "on"))
+ {
+ if (vm->pcap[rx_tx].pcap_enable == 0)
+ {
+ enabled = 1;
+ }
+ else
+ {
+ vlib_cli_output (vm, "pcap %s capture already on...",
+ (rx_tx == VLIB_RX) ? "rx" : "tx");
+ errorFlag = 1;
+ break;
+ }
+ }
+ else if (unformat (line_input, "off"))
+ {
+ if (vm->pcap[rx_tx].pcap_enable)
+ {
+ vlib_cli_output
+ (vm, "captured %d pkts...",
+ vm->pcap[rx_tx].pcap_main.n_packets_captured);
+ if (vm->pcap[rx_tx].pcap_main.n_packets_captured)
+ {
+ vm->pcap[rx_tx].pcap_main.n_packets_to_capture =
+ vm->pcap[rx_tx].pcap_main.n_packets_captured;
+ error = pcap_write (&vm->pcap[rx_tx].pcap_main);
+ if (error)
+ clib_error_report (error);
+ else
+ vlib_cli_output (vm, "saved to %s...",
+ vm->pcap[rx_tx].pcap_main.file_name);
+ }
+
+ vm->pcap[rx_tx].pcap_enable = 0;
+ }
+ else
+ {
+ vlib_cli_output (vm, "pcap %s capture already off...",
+ (rx_tx == VLIB_RX) ? "rx" : "tx");
+ errorFlag = 1;
+ break;
+ }
+ }
+ else if (unformat (line_input, "max %d", &max))
+ {
+ if (vm->pcap[rx_tx].pcap_enable)
+ {
+ vlib_cli_output
+ (vm,
+ "can't change max value while pcap tx capture active...");
+ errorFlag = 1;
+ break;
+ }
+ vm->pcap[rx_tx].pcap_main.n_packets_to_capture = max;
+ }
+ else if (unformat (line_input, "intfc %U",
+ unformat_vnet_sw_interface, vnm,
+ &vm->pcap[rx_tx].pcap_sw_if_index))
+ ;
+
+ else if (unformat (line_input, "intfc any"))
+ {
+ vm->pcap[rx_tx].pcap_sw_if_index = 0;
+ }
+ else if (unformat (line_input, "file %s", &filename))
+ {
+ if (vm->pcap[rx_tx].pcap_enable)
+ {
+ vlib_cli_output
+ (vm, "can't change file while pcap tx capture active...");
+ errorFlag = 1;
+ break;
+ }
+
+ /* Brain-police user path input */
+ if (strstr ((char *) filename, "..")
+ || index ((char *) filename, '/'))
+ {
+ vlib_cli_output (vm, "illegal characters in filename '%s'",
+ filename);
+ vlib_cli_output (vm, "Hint: .. and / are not allowed.");
+ vec_free (filename);
+ errorFlag = 1;
+ break;
+ }
+
+ chroot_filename = format (0, "/tmp/%s%c", filename, 0);
+ vec_free (filename);
+ }
+ else if (unformat (line_input, "status"))
+ {
+ if (vm->pcap[rx_tx].pcap_sw_if_index == 0)
+ {
+ vlib_cli_output
+ (vm, "max is %d for any interface to file %s",
+ vm->pcap[rx_tx].pcap_main.n_packets_to_capture ?
+ vm->pcap[rx_tx].pcap_main.n_packets_to_capture
+ : PCAP_DEF_PKT_TO_CAPTURE,
+ vm->pcap[rx_tx].pcap_main.file_name ?
+ (u8 *) vm->pcap[rx_tx].pcap_main.file_name :
+ (u8 *) "/tmp/vpe.pcap");
+ }
+ else
+ {
+ vlib_cli_output (vm, "max is %d for interface %U to file %s",
+ vm->pcap[rx_tx].pcap_main.n_packets_to_capture
+ ? vm->pcap[rx_tx].
+ pcap_main.n_packets_to_capture :
+ PCAP_DEF_PKT_TO_CAPTURE,
+ format_vnet_sw_if_index_name, vnm,
+ vm->pcap_sw_if_index,
+ vm->pcap[rx_tx].
+ pcap_main.file_name ? (u8 *) vm->pcap[rx_tx].
+ pcap_main.file_name : (u8 *) "/tmp/vpe.pcap");
+ }
+
+ if (vm->pcap[rx_tx].pcap_enable == 0)
+ {
+ vlib_cli_output (vm, "pcap %s capture is off...",
+ (rx_tx == VLIB_RX) ? "rx" : "tx");
+ }
+ else
+ {
+ vlib_cli_output (vm, "pcap %s capture is on: %d of %d pkts...",
+ (rx_tx == VLIB_RX) ? "rx" : "tx",
+ vm->pcap[rx_tx].pcap_main.n_packets_captured,
+ vm->pcap[rx_tx].
+ pcap_main.n_packets_to_capture);
+ }
+ break;
+ }
+
+ else
+ {
+ error = clib_error_return (0, "unknown input `%U'",
+ format_unformat_error, line_input);
+ errorFlag = 1;
+ break;
+ }
+ }
+ unformat_free (line_input);
+
+
+ if (errorFlag == 0)
+ {
+ /* Since no error, save configured values. */
+ if (chroot_filename)
+ {
+ if (vm->pcap[rx_tx].pcap_main.file_name)
+ vec_free (vm->pcap[rx_tx].pcap_main.file_name);
+ vec_add1 (chroot_filename, 0);
+ vm->pcap[rx_tx].pcap_main.file_name = (char *) chroot_filename;
+ }
+
+ if (max)
+ vm->pcap[rx_tx].pcap_main.n_packets_to_capture = max;