aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--vnet/vnet/pg/cli.c22
-rw-r--r--vnet/vnet/unix/pcap.c52
-rw-r--r--vnet/vnet/unix/pcap.h2
3 files changed, 44 insertions, 32 deletions
diff --git a/vnet/vnet/pg/cli.c b/vnet/vnet/pg/cli.c
index 201e4fb9d5e..d73add1220d 100644
--- a/vnet/vnet/pg/cli.c
+++ b/vnet/vnet/pg/cli.c
@@ -463,6 +463,7 @@ pg_capture_cmd_fn (vlib_main_t * vm,
pg_interface_t * pi;
u8 * pcap_file_name = 0;
u32 hw_if_index;
+ u32 is_disable = 0;
u32 count = ~0;
if (! unformat_user (input, unformat_line_input, line_input))
@@ -480,6 +481,8 @@ pg_capture_cmd_fn (vlib_main_t * vm,
;
else if (unformat (line_input, "count %u", &count))
;
+ else if (unformat (line_input, "disable"))
+ is_disable = 1;
else
{
@@ -495,21 +498,26 @@ pg_capture_cmd_fn (vlib_main_t * vm,
if (hi->dev_class_index != pg_dev_class.index)
return clib_error_return (0, "Please specify packet-generator interface");
- if (!pcap_file_name)
+ if (!pcap_file_name && is_disable == 0)
return clib_error_return (0, "Please specify pcap file name");
- {
- struct stat sb;
- if (stat ((char *) pcap_file_name, &sb) != -1)
- return clib_error_return (0, "Cannot create pcap file");
- }
+ if (is_disable == 0)
+ {
+ struct stat sb;
+ if (stat ((char *) pcap_file_name, &sb) != -1)
+ return clib_error_return (0, "Cannot create pcap file");
+ }
unformat_free (line_input);
pi = pool_elt_at_index (pg->interfaces, hi->dev_instance);
vec_free (pi->pcap_file_name);
- pi->pcap_file_name = pcap_file_name;
memset (&pi->pcap_main, 0, sizeof (pi->pcap_main));
+
+ if (is_disable)
+ return 0;
+
+ pi->pcap_file_name = pcap_file_name;
pi->pcap_main.file_name = (char *) pi->pcap_file_name;
pi->pcap_main.n_packets_to_capture = count;
pi->pcap_main.packet_type = PCAP_PACKET_TYPE_ethernet;
diff --git a/vnet/vnet/unix/pcap.c b/vnet/vnet/unix/pcap.c
index 16b8443085b..1c5f033ea1f 100644
--- a/vnet/vnet/unix/pcap.c
+++ b/vnet/vnet/unix/pcap.c
@@ -59,6 +59,15 @@ file will be written after n_packets_to_capture or call to pcap_write (&pcap).
*/
clib_error_t *
+pcap_close (pcap_main_t * pm)
+{
+ close (pm->file_descriptor);
+ pm->flags &= ~PCAP_MAIN_INIT_DONE;
+ pm->file_descriptor = -1;
+ return 0;
+}
+
+clib_error_t *
pcap_write (pcap_main_t * pm)
{
clib_error_t * error = 0;
@@ -101,35 +110,30 @@ pcap_write (pcap_main_t * pm)
}
}
- do {
- int n = vec_len (pm->pcap_data) - pm->n_pcap_data_written;
-
- if (n > 0)
- {
- n = write (pm->file_descriptor,
- vec_elt_at_index (pm->pcap_data, pm->n_pcap_data_written),
- n);
- if (n < 0 && unix_error_is_fatal (errno))
- {
- error = clib_error_return_unix (0, "write `%s'", pm->file_name);
- goto done;
- }
- }
- pm->n_pcap_data_written += n;
- if (pm->n_pcap_data_written >= vec_len (pm->pcap_data))
- {
- vec_reset_length (pm->pcap_data);
- break;
+ while (vec_len (pm->pcap_data) > pm->n_pcap_data_written)
+ {
+ int n = vec_len (pm->pcap_data) - pm->n_pcap_data_written;
+
+ n = write (pm->file_descriptor,
+ vec_elt_at_index (pm->pcap_data, pm->n_pcap_data_written), n);
+
+ if (n < 0 && unix_error_is_fatal (errno))
+ {
+ error = clib_error_return_unix (0, "write `%s'", pm->file_name);
+ goto done;
+ }
+ pm->n_pcap_data_written += n;
}
- } while (pm->n_packets_captured >= pm->n_packets_to_capture);
- if (pm->n_packets_captured >= pm->n_packets_to_capture)
+ if (pm->n_pcap_data_written >= vec_len (pm->pcap_data))
{
- close (pm->file_descriptor);
- pm->flags &= ~PCAP_MAIN_INIT_DONE;
- pm->file_descriptor = -1;
+ vec_reset_length (pm->pcap_data);
+ pm->n_pcap_data_written = 0;
}
+ if (pm->n_packets_captured >= pm->n_packets_to_capture)
+ pcap_close(pm);
+
done:
if (error)
{
diff --git a/vnet/vnet/unix/pcap.h b/vnet/vnet/unix/pcap.h
index 563eafc102b..89eca031185 100644
--- a/vnet/vnet/unix/pcap.h
+++ b/vnet/vnet/unix/pcap.h
@@ -166,7 +166,7 @@ pcap_add_buffer (pcap_main_t * pm,
f64 time_now = vlib_time_now (vm);
void * d;
- d = pcap_add_packet (pm, time_now, n_bytes_in_trace, n_left);
+ d = pcap_add_packet (pm, time_now, n_left, n);
while (1)
{
u32 copy_length = clib_min ((u32) n_left, b->current_length);