nat: Include platform specific headers on FreeBSD
[vpp.git] / src / vnet / l2 / l2_rw.c
index 0b5e970..c0e8ec4 100644 (file)
@@ -16,6 +16,7 @@
 #include <vlib/vlib.h>
 #include <vnet/l2/feat_bitmap.h>
 #include <vnet/l2/l2_rw.h>
+#include <vnet/classify/vnet_classify.h>
 
 /**
  * @file
@@ -26,9 +27,9 @@
  */
 
 
+#ifndef CLIB_MARCH_VARIANT
 l2_rw_main_t l2_rw_main;
-
-vlib_node_registration_t l2_rw_node;
+#endif /* CLIB_MARCH_VARIANT */
 
 typedef struct
 {
@@ -91,80 +92,37 @@ l2_rw_get_config (u32 sw_if_index)
 static_always_inline void
 l2_rw_rewrite (l2_rw_entry_t * rwe, u8 * h)
 {
-  if (U32X4_ALIGNED (h))
-    {
-      u32x4 *d = ((u32x4 *) h) + rwe->skip_n_vectors;
-      switch (rwe->rewrite_n_vectors)
-       {
-       case 5:
-         d[4] = (d[4] & ~rwe->mask[4]) | rwe->value[4];
-         /* FALLTHROUGH */
-       case 4:
-         d[3] = (d[3] & ~rwe->mask[3]) | rwe->value[3];
-         /* FALLTHROUGH */
-       case 3:
-         d[2] = (d[2] & ~rwe->mask[2]) | rwe->value[2];
-         /* FALLTHROUGH */
-       case 2:
-         d[1] = (d[1] & ~rwe->mask[1]) | rwe->value[1];
-         /* FALLTHROUGH */
-       case 1:
-         d[0] = (d[0] & ~rwe->mask[0]) | rwe->value[0];
-         break;
-       default:
-         abort ();
-       }
-    }
-  else
+  u32x4u *d = ((u32x4u *) h) + rwe->skip_n_vectors;
+  switch (rwe->rewrite_n_vectors)
     {
-      u64 *d = ((u64 *) h) + rwe->skip_n_vectors * 2;
-      switch (rwe->rewrite_n_vectors)
-       {
-       case 5:
-         d[8] =
-           (d[8] & ~(((u64 *) rwe->mask)[8])) | (((u64 *) rwe->value)[8]);
-         d[9] =
-           (d[9] & ~(((u64 *) rwe->mask)[9])) | (((u64 *) rwe->value)[9]);
-         /* FALLTHROUGH */
-       case 4:
-         d[6] =
-           (d[6] & ~(((u64 *) rwe->mask)[6])) | (((u64 *) rwe->value)[6]);
-         d[7] =
-           (d[7] & ~(((u64 *) rwe->mask)[7])) | (((u64 *) rwe->value)[7]);
-         /* FALLTHROUGH */
-       case 3:
-         d[4] =
-           (d[4] & ~(((u64 *) rwe->mask)[4])) | (((u64 *) rwe->value)[4]);
-         d[5] =
-           (d[5] & ~(((u64 *) rwe->mask)[5])) | (((u64 *) rwe->value)[5]);
-         /* FALLTHROUGH */
-       case 2:
-         d[2] =
-           (d[2] & ~(((u64 *) rwe->mask)[2])) | (((u64 *) rwe->value)[2]);
-         d[3] =
-           (d[3] & ~(((u64 *) rwe->mask)[3])) | (((u64 *) rwe->value)[3]);
-         /* FALLTHROUGH */
-       case 1:
-         d[0] =
-           (d[0] & ~(((u64 *) rwe->mask)[0])) | (((u64 *) rwe->value)[0]);
-         d[1] =
-           (d[1] & ~(((u64 *) rwe->mask)[1])) | (((u64 *) rwe->value)[1]);
-         break;
-       default:
-         abort ();
-       }
+    case 5:
+      d[4] = (d[4] & ~rwe->mask[4]) | rwe->value[4];
+      /* FALLTHROUGH */
+    case 4:
+      d[3] = (d[3] & ~rwe->mask[3]) | rwe->value[3];
+      /* FALLTHROUGH */
+    case 3:
+      d[2] = (d[2] & ~rwe->mask[2]) | rwe->value[2];
+      /* FALLTHROUGH */
+    case 2:
+      d[1] = (d[1] & ~rwe->mask[1]) | rwe->value[1];
+      /* FALLTHROUGH */
+    case 1:
+      d[0] = (d[0] & ~rwe->mask[0]) | rwe->value[0];
+      rwe->hit_count++;
+      break;
+    default:
+      abort ();
     }
 }
 
-static uword
-l2_rw_node_fn (vlib_main_t * vm,
-              vlib_node_runtime_t * node, vlib_frame_t * frame)
+VLIB_NODE_FN (l2_rw_node) (vlib_main_t * vm,
+                          vlib_node_runtime_t * node, vlib_frame_t * frame)
 {
   l2_rw_main_t *rw = &l2_rw_main;
   u32 n_left_from, *from, *to_next, next_index;
   vnet_classify_main_t *vcm = &vnet_classify_main;
   f64 now = vlib_time_now (vlib_get_main ());
-  u32 prefetch_size = 0;
 
   from = vlib_frame_vector_args (frame);
   n_left_from = frame->n_vectors;      /* number of packets to process */
@@ -177,7 +135,7 @@ l2_rw_node_fn (vlib_main_t * vm,
       /* get space to enqueue frame to graph node "next_index" */
       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
 
-      while (n_left_from >= 4 && n_left_to_next >= 2)
+      while (n_left_from >= 6 && n_left_to_next >= 2)
        {
          u32 bi0, next0, sw_if_index0, rwe_index0;
          u32 bi1, next1, sw_if_index1, rwe_index1;
@@ -190,14 +148,16 @@ l2_rw_node_fn (vlib_main_t * vm,
          l2_rw_entry_t *rwe0, *rwe1;
 
          {
-           vlib_buffer_t *p2, *p3;
+           vlib_buffer_t *p2, *p3, *p4, *p5;
            p2 = vlib_get_buffer (vm, from[2]);
            p3 = vlib_get_buffer (vm, from[3]);
+           p4 = vlib_get_buffer (vm, from[4]);
+           p5 = vlib_get_buffer (vm, from[5]);
 
-           vlib_prefetch_buffer_header (p2, LOAD);
-           vlib_prefetch_buffer_header (p3, LOAD);
-           CLIB_PREFETCH (vlib_buffer_get_current (p2), prefetch_size, LOAD);
-           CLIB_PREFETCH (vlib_buffer_get_current (p3), prefetch_size, LOAD);
+           vlib_prefetch_buffer_header (p4, LOAD);
+           vlib_prefetch_buffer_header (p5, LOAD);
+           vlib_prefetch_buffer_data (p2, LOAD);
+           vlib_prefetch_buffer_data (p3, LOAD);
          }
 
          bi0 = from[0];
@@ -220,8 +180,6 @@ l2_rw_node_fn (vlib_main_t * vm,
          config1 = l2_rw_get_config (sw_if_index1);    /*TODO: check sw_if_index0 value */
          t0 = pool_elt_at_index (vcm->tables, config0->table_index);
          t1 = pool_elt_at_index (vcm->tables, config1->table_index);
-         prefetch_size =
-           (t1->skip_n_vectors + t1->match_n_vectors) * sizeof (u32x4);
 
          hash0 = vnet_classify_hash_packet (t0, (u8 *) h0);
          hash1 = vnet_classify_hash_packet (t1, (u8 *) h1);
@@ -348,6 +306,7 @@ l2_rw_node_fn (vlib_main_t * vm,
   return frame->n_vectors;
 }
 
+#ifndef CLIB_MARCH_VARIANT
 int
 l2_rw_mod_entry (u32 * index,
                 u8 * mask, u8 * value, u32 len, u32 skip, u8 is_del)
@@ -368,22 +327,20 @@ l2_rw_mod_entry (u32 * index,
       *index = e - rw->entries;
     }
 
-  if (!e)
-    return -1;
-
   if (is_del)
     {
       pool_put (rw->entries, e);
       return 0;
     }
 
+  e->hit_count = 0;
   e->skip_n_vectors = skip / sizeof (u32x4);
   skip -= e->skip_n_vectors * sizeof (u32x4);
   e->rewrite_n_vectors = (skip + len - 1) / sizeof (u32x4) + 1;
   vec_alloc_aligned (e->mask, e->rewrite_n_vectors, sizeof (u32x4));
-  memset (e->mask, 0, e->rewrite_n_vectors * sizeof (u32x4));
+  clib_memset (e->mask, 0, e->rewrite_n_vectors * sizeof (u32x4));
   vec_alloc_aligned (e->value, e->rewrite_n_vectors, sizeof (u32x4));
-  memset (e->value, 0, e->rewrite_n_vectors * sizeof (u32x4));
+  clib_memset (e->value, 0, e->rewrite_n_vectors * sizeof (u32x4));
 
   clib_memcpy (((u8 *) e->value) + skip, value, len);
   clib_memcpy (((u8 *) e->mask) + skip, mask, len);
@@ -396,6 +353,7 @@ l2_rw_mod_entry (u32 * index,
 
   return 0;
 }
+#endif /* CLIB_MARCH_VARIANT */
 
 static clib_error_t *
 l2_rw_entry_cli_fn (vlib_main_t * vm,
@@ -442,18 +400,21 @@ l2_rw_entry_cli_fn (vlib_main_t * vm,
  * the provisioned mask and value, modifies the packet header.
  *
  * @cliexpar
- * @todo This is incomplete. This needs a detailed description and a
- * practical example.
+ * Example of how to add an l2 rewrite entry to change the destination mac of
+ * the packet to 00:8a:00:0d:0e:02 (where parameter mask is Ethernet header's
+mask,
+ * parameter value is Ethernet header's value):
+ * @cliexcmd{l2 rewrite entry mask ffffffffffff00000000000000000000 value
+008a000d0e0200000000000000000000}
 ?*/
-/* *INDENT-OFF* */
 VLIB_CLI_COMMAND (l2_rw_entry_cli, static) = {
   .path = "l2 rewrite entry",
   .short_help =
   "l2 rewrite entry [index <index>] [mask <hex-mask>] [value <hex-value>] [skip <n_bytes>] [del]",
   .function = l2_rw_entry_cli_fn,
 };
-/* *INDENT-ON* */
 
+#ifndef CLIB_MARCH_VARIANT
 int
 l2_rw_interface_set_table (u32 sw_if_index, u32 table_index, u32 miss_index)
 {
@@ -471,6 +432,7 @@ l2_rw_interface_set_table (u32 sw_if_index, u32 table_index, u32 miss_index)
 
   return 0;
 }
+#endif /* CLIB_MARCH_VARIANT */
 
 static clib_error_t *
 l2_rw_interface_cli_fn (vlib_main_t * vm,
@@ -510,21 +472,36 @@ l2_rw_interface_cli_fn (vlib_main_t * vm,
 }
 
 /*?
- * Layer 2-Rewrite node uses classify tables to match packets. Then, using
- * the provisioned mask and value, modifies the packet header.
+ * Apply the rule to the interface. The following example shows how to use
+classify
+ * entry and Layer 2-Rewrite entry to modify the packet ethernet header on the
+ * interface.
+ *
+ * @cliexpar
+ * Example use the classify to filter packets that do not need to be modified
+(where
+ * 192.168.68.34 is the destination ip of the data packet, 8080 is the
+destination port
+ * of the packet):
+ * @cliexcmd{classify table mask l3 ip4 dst l4 dst_port}
+ * @cliexcmd{classify session acl-hit-next permit table-index 0 match l3 ip4
+dst 192.168.68.34 l4 dst_port 8080}
  *
  * @cliexpar
- * @todo This is incomplete. This needs a detailed description and a
- * practical example.
+ * Example apply classify and l2 rewrite rules to the interface (where
+YusurK2Eth6/0/1/3
+ * is interface, \"table 0\" means Table Id is 0, \"miss 0\" means the packet
+that matches
+ * the classify. miss will be modified according to the l2 rewrite entry with
+index 0):
+ * @cliexcmd{set interface l2 rewrite YusurK2Eth6/0/1/3 table 0 miss-index 0}
 ?*/
-/* *INDENT-OFF* */
 VLIB_CLI_COMMAND (l2_rw_interface_cli, static) = {
   .path = "set interface l2 rewrite",
   .short_help =
   "set interface l2 rewrite <interface> [table <table index>] [miss-index <entry-index>]",
   .function = l2_rw_interface_cli_fn,
 };
-/* *INDENT-ON* */
 
 static clib_error_t *
 l2_rw_show_interfaces_cli_fn (vlib_main_t * vm,
@@ -536,30 +513,27 @@ l2_rw_show_interfaces_cli_fn (vlib_main_t * vm,
     vlib_cli_output (vm, "No interface is currently using l2 rewrite\n");
 
   uword i;
-  /* *INDENT-OFF* */
-  clib_bitmap_foreach(i, rw->configs_bitmap, {
+  clib_bitmap_foreach (i, rw->configs_bitmap) {
       vlib_cli_output (vm, "sw_if_index:%d %U\n", i, format_l2_rw_config, &rw->configs[i]);
-  });
-  /* *INDENT-ON* */
+  }
   return 0;
 }
 
 /*?
- * Layer 2-Rewrite node uses classify tables to match packets. Then, using
- * the provisioned mask and value, modifies the packet header.
+ * This command displays the l2 rewrite entries of the interfaces.
  *
  * @cliexpar
- * @todo This is incomplete. This needs a detailed description and a
- * practical example.
+ * Example of how to display the l2 rewrite rules on the interface:
+ * @cliexstart{show l2 rewrite interfaces}
+ * sw_if_index:4 table-index:0 miss-index:0
+ * @cliexend
 ?*/
-/* *INDENT-OFF* */
 VLIB_CLI_COMMAND (l2_rw_show_interfaces_cli, static) = {
   .path = "show l2 rewrite interfaces",
   .short_help =
   "show l2 rewrite interfaces",
   .function = l2_rw_show_interfaces_cli_fn,
 };
-/* *INDENT-ON* */
 
 static clib_error_t *
 l2_rw_show_entries_cli_fn (vlib_main_t * vm,
@@ -570,32 +544,31 @@ l2_rw_show_entries_cli_fn (vlib_main_t * vm,
   if (pool_elts (rw->entries) == 0)
     vlib_cli_output (vm, "No entries\n");
 
-  /* *INDENT-OFF* */
-  pool_foreach(e, rw->entries, {
+  pool_foreach (e, rw->entries) {
     vlib_cli_output (vm, "%U\n", format_l2_rw_entry, e);
-  });
-  /* *INDENT-ON* */
+  }
   return 0;
 }
 
 /*?
- * Layer 2-Rewrite node uses classify tables to match packets. Then, using
- * the provisioned mask and value, modifies the packet header.
+ * This command displays all l2 rewrite entries.
  *
  * @cliexpar
- * @todo This is incomplete. This needs a detailed description and a
- * practical example.
+ * Example of how to display all l2 rewrite entries:
+ * @cliexstart{show l2 rewrite entries}
+ * 0 -  mask:ffffffffffff00000000000000000000
+value:aabbccddeeff00000000000000000000
+ *    hits:0 skip_bytes:0
+ * @cliexend
 ?*/
-/* *INDENT-OFF* */
 VLIB_CLI_COMMAND (l2_rw_show_entries_cli, static) = {
   .path = "show l2 rewrite entries",
   .short_help =
   "show l2 rewrite entries",
   .function = l2_rw_show_entries_cli_fn,
 };
-/* *INDENT-ON* */
 
-int
+static int
 l2_rw_enable_disable (u32 bridge_domain, u8 disable)
 {
   u32 mask = L2INPUT_FEAT_RW;
@@ -629,21 +602,22 @@ l2_rw_set_cli_fn (vlib_main_t * vm,
 }
 
 /*?
- * Layer 2-Rewrite node uses classify tables to match packets. Then, using
- * the provisioned mask and value, modfies the packet header.
+ * Layer 2 rewrite can be enabled and disabled on each interface and on each
+bridge-domain.
+ * Use this command to manage l2 rewrite on bridge-domain.
  *
  * @cliexpar
- * @todo This is incomplete. This needs a detailed description and a
- * practical example.
+ * Example of how to enable rewrite (where 100 is the bridge-domain-id):
+ * @cliexcmd{set bridge-domain rewrite 100}
+ * Example of how to disable rewrite (where 100 is the bridge-domain-id):
+ * @cliexcmd{set bridge-domain rewrite 100 disable}
 ?*/
-/* *INDENT-OFF* */
 VLIB_CLI_COMMAND (l2_rw_set_cli, static) = {
   .path = "set bridge-domain rewrite",
   .short_help =
   "set bridge-domain rewrite <bridge-domain> [disable]",
   .function = l2_rw_set_cli_fn,
 };
-/* *INDENT-ON* */
 
 static clib_error_t *
 l2_rw_init (vlib_main_t * vm)
@@ -685,9 +659,7 @@ static char *l2_rw_error_strings[] = {
 #undef _
 };
 
-/* *INDENT-OFF* */
 VLIB_REGISTER_NODE (l2_rw_node) = {
-  .function = l2_rw_node_fn,
   .name = "l2-rw",
   .vector_size = sizeof (u32),
   .format_trace = format_l2_rw_trace,
@@ -698,9 +670,7 @@ VLIB_REGISTER_NODE (l2_rw_node) = {
   .n_next_nodes = L2_RW_N_NEXT,
   .next_nodes = { [L2_RW_NEXT_DROP]  = "error-drop"},
 };
-/* *INDENT-ON* */
 
-VLIB_NODE_FUNCTION_MULTIARCH (l2_rw_node, l2_rw_node_fn)
 /*
  * fd.io coding-style-patch-verification: ON
  *