pg: A Tunnel mode variant of a pg interface
[vpp.git] / src / vnet / pg / cli.c
index 834ec48..e57e725 100644 (file)
@@ -64,9 +64,9 @@ pg_enable_disable (u32 stream_index, int is_enable)
     {
       /* No stream specified: enable/disable all streams. */
       /* *INDENT-OFF* */
-        pool_foreach (s, pg->streams, ({
+        pool_foreach (s, pg->streams)  {
             pg_stream_enable_disable (pg, s, is_enable);
-        }));
+        }
        /* *INDENT-ON* */
     }
   else
@@ -86,13 +86,17 @@ pg_capture (pg_capture_args_t * a)
   if (a->is_enabled == 1)
     {
       struct stat sb;
-      if (stat ((char *) a->pcap_file_name, &sb) != -1)
-       return clib_error_return (0, "Cannot create pcap file");
+      if (stat (a->pcap_file_name, &sb) != -1)
+       return clib_error_return (0, "pcap file '%s' already exists.",
+                                 a->pcap_file_name);
     }
 
   pi = pool_elt_at_index (pg->interfaces, a->dev_instance);
   vec_free (pi->pcap_file_name);
+  if ((pi->pcap_main.flags & PCAP_MAIN_INIT_DONE))
+    pcap_close (&pi->pcap_main);
   clib_memset (&pi->pcap_main, 0, sizeof (pi->pcap_main));
+  pi->pcap_main.file_descriptor = -1;
 
   if (a->is_enabled == 0)
     return 0;
@@ -109,19 +113,26 @@ static clib_error_t *
 enable_disable_stream (vlib_main_t * vm,
                       unformat_input_t * input, vlib_cli_command_t * cmd)
 {
+  unformat_input_t _line_input, *line_input = &_line_input;
   pg_main_t *pg = &pg_main;
   int is_enable = cmd->function_arg != 0;
   u32 stream_index = ~0;
 
-  if (unformat (input, "%U", unformat_eof))
-    ;
-  else if (unformat (input, "%U", unformat_hash_vec_string,
-                    pg->stream_index_by_name, &stream_index))
-    ;
-  else
-    return clib_error_create ("unknown input `%U'",
-                             format_unformat_error, input);
+  if (!unformat_user (input, unformat_line_input, line_input))
+    goto doit;
 
+  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
+    {
+      if (unformat (line_input, "%U", unformat_hash_vec_string,
+                   pg->stream_index_by_name, &stream_index))
+       ;
+      else
+       return clib_error_create ("unknown input `%U'",
+                                 format_unformat_error, line_input);
+    }
+  unformat_free (line_input);
+
+doit:
   pg_enable_disable (stream_index, is_enable);
 
   return 0;
@@ -163,7 +174,7 @@ format_pg_edit_group (u8 * s, va_list * va)
       while (*junk_after_name && *junk_after_name != ' ')
        junk_after_name++;
       *junk_after_name = 0;
-      s = format (s, "edit-funtion %s, ", function_name);
+      s = format (s, "edit-function %s, ", function_name);
       vec_free (function_name);
     }
 
@@ -234,9 +245,9 @@ show_streams (vlib_main_t * vm,
 
   vlib_cli_output (vm, "%U", format_pg_stream, 0, 0);
   /* *INDENT-OFF* */
-  pool_foreach (s, pg->streams, ({
+  pool_foreach (s, pg->streams)  {
       vlib_cli_output (vm, "%U", format_pg_stream, s, verbose);
-    }));
+    }
   /* *INDENT-ON* */
 
 done:
@@ -322,13 +333,30 @@ validate_stream (pg_stream_t * s)
   return 0;
 }
 
+const char *
+pg_interface_get_input_node (pg_interface_t *pi)
+{
+  switch (pi->mode)
+    {
+    case PG_MODE_ETHERNET:
+      return ("ethernet-input");
+    case PG_MODE_IP4:
+      return ("ip4-input");
+    case PG_MODE_IP6:
+      return ("ip6-input");
+    }
+
+  ASSERT (0);
+  return ("ethernet-input");
+}
+
 static clib_error_t *
 new_stream (vlib_main_t * vm,
            unformat_input_t * input, vlib_cli_command_t * cmd)
 {
   clib_error_t *error = 0;
   u8 *tmp = 0;
-  u32 hw_if_index;
+  u32 maxframe, hw_if_index;
   unformat_input_t sub_input = { 0 };
   int sub_input_given = 0;
   vnet_main_t *vnm = vnet_get_main ();
@@ -340,8 +368,10 @@ new_stream (vlib_main_t * vm,
   s.node_index = ~0;
   s.max_packet_bytes = s.min_packet_bytes = 64;
   s.buffer_bytes = vlib_buffer_get_default_data_size (vm);
-  s.if_id = 0;
+  s.if_id = ~0;
+  s.n_max_frame = VLIB_FRAME_SIZE;
   pcap_file_name = 0;
+
   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
     {
       if (unformat (input, "name %v", &tmp))
@@ -363,10 +393,17 @@ new_stream (vlib_main_t * vm,
       else if (unformat (input, "source pg%u", &s.if_id))
        ;
 
+      else if (unformat (input, "buffer-flags %U",
+                        unformat_vnet_buffer_flags, &s.buffer_flags))
+       ;
+      else if (unformat (input, "buffer-offload-flags %U",
+                        unformat_vnet_buffer_offload_flags, &s.buffer_oflags))
+       ;
       else if (unformat (input, "node %U",
                         unformat_vlib_node, vm, &s.node_index))
        ;
-
+      else if (unformat (input, "maxframe %u", &maxframe))
+       s.n_max_frame = s.n_max_frame < maxframe ? s.n_max_frame : maxframe;
       else if (unformat (input, "worker %u", &s.worker_index))
        ;
 
@@ -374,6 +411,10 @@ new_stream (vlib_main_t * vm,
                         unformat_vnet_sw_interface, vnm,
                         &s.sw_if_index[VLIB_RX]))
        ;
+      else if (unformat (input, "tx-interface %U",
+                        unformat_vnet_sw_interface, vnm,
+                        &s.sw_if_index[VLIB_TX]))
+       ;
 
       else if (unformat (input, "pcap %s", &pcap_file_name))
        ;
@@ -403,8 +444,15 @@ new_stream (vlib_main_t * vm,
     {
       if (pcap_file_name != 0)
        {
-         vlib_node_t *n =
-           vlib_get_node_by_name (vm, (u8 *) "ethernet-input");
+         vlib_node_t *n;
+
+         ASSERT (s.if_id != ~0);
+
+         if (s.if_id != ~0)
+           n = vlib_get_node_by_name (vm, (u8 *) pg_interface_get_input_node (
+                                            &pg->interfaces[s.if_id]));
+         else
+           n = vlib_get_node_by_name (vm, (u8 *) "ethernet-input");
          s.node_index = n->index;
        }
       else
@@ -473,7 +521,9 @@ VLIB_CLI_COMMAND (new_stream_cli, static) = {
   "interface STRING     interface for stream output \n"
   "node NODE-NAME       node for stream output\n"
   "data STRING          specifies packet data\n"
-  "pcap FILENAME        read packet data from pcap file\n",
+  "pcap FILENAME        read packet data from pcap file\n"
+  "rate PPS             rate to transfer packet data\n"
+  "maxframe NPKTS       maximum number of packets per frame\n",
 };
 /* *INDENT-ON* */
 
@@ -612,7 +662,7 @@ pg_capture_cmd_fn (vlib_main_t * vm,
   a->hw_if_index = hw_if_index;
   a->dev_instance = hi->dev_instance;
   a->is_enabled = !is_disable;
-  a->pcap_file_name = pcap_file_name;
+  a->pcap_file_name = (char *) pcap_file_name;
   a->count = count;
 
   error = pg_capture (a);
@@ -637,7 +687,7 @@ create_pg_if_cmd_fn (vlib_main_t * vm,
 {
   pg_main_t *pg = &pg_main;
   unformat_input_t _line_input, *line_input = &_line_input;
-  u32 if_id;
+  u32 if_id, gso_enabled = 0, gso_size = 0, coalesce_enabled = 0;
   clib_error_t *error = NULL;
 
   if (!unformat_user (input, unformat_line_input, line_input))
@@ -647,7 +697,19 @@ create_pg_if_cmd_fn (vlib_main_t * vm,
     {
       if (unformat (line_input, "interface pg%u", &if_id))
        ;
-
+      else if (unformat (line_input, "coalesce-enabled"))
+       coalesce_enabled = 1;
+      else if (unformat (line_input, "gso-enabled"))
+       {
+         gso_enabled = 1;
+         if (unformat (line_input, "gso-size %u", &gso_size))
+           ;
+         else
+           {
+             error = clib_error_create ("gso enabled but gso size missing");
+             goto done;
+           }
+       }
       else
        {
          error = clib_error_create ("unknown input `%U'",
@@ -656,7 +718,8 @@ create_pg_if_cmd_fn (vlib_main_t * vm,
        }
     }
 
-  pg_interface_add_or_get (pg, if_id);
+  pg_interface_add_or_get (pg, if_id, gso_enabled, gso_size, coalesce_enabled,
+                          PG_MODE_ETHERNET);
 
 done:
   unformat_free (line_input);
@@ -667,7 +730,8 @@ done:
 /* *INDENT-OFF* */
 VLIB_CLI_COMMAND (create_pg_if_cmd, static) = {
   .path = "create packet-generator",
-  .short_help = "create packet-generator interface <interface name>",
+  .short_help = "create packet-generator interface <interface name>"
+                " [gso-enabled gso-size <size> [coalesce-enabled]]",
   .function = create_pg_if_cmd_fn,
 };
 /* *INDENT-ON* */