fib: Table Replace
[vpp.git] / src / vnet / mpls / mpls.c
index 5021ac2..4076a89 100644 (file)
@@ -47,6 +47,9 @@ u8 * format_mpls_unicast_label (u8 * s, va_list * args)
   case MPLS_IETF_GAL_LABEL:
       s = format (s, "%s", MPLS_IETF_GAL_STRING);
       break;
+  case MPLS_LABEL_POP:
+      s = format (s, "pop");
+      break;
   default:
       s = format (s, "%d", label);
       break;
@@ -212,23 +215,21 @@ vnet_mpls_local_label (vlib_main_t * vm,
                        vlib_cli_command_t * cmd)
 {
   unformat_input_t _line_input, * line_input = &_line_input;
+  u32 table_id, is_del, is_ip, payload_proto;
   fib_route_path_t *rpaths = NULL, rpath;
-  u32 table_id, is_del, is_ip;
   mpls_label_t local_label;
-  mpls_label_t out_label;
   clib_error_t * error;
   mpls_eos_bit_t eos;
-  vnet_main_t * vnm;
   fib_prefix_t pfx;
 
-  vnm = vnet_get_main();
   error = NULL;
   is_ip = 0;
   table_id = 0;
   eos = MPLS_EOS;
   is_del = 0;
   local_label = MPLS_LABEL_INVALID;
-  memset(&pfx, 0, sizeof(pfx));
+  clib_memset(&pfx, 0, sizeof(pfx));
+  payload_proto = DPO_PROTO_MPLS;
 
    /* Get a line of input. */
   if (! unformat_user (input, unformat_line_input, line_input))
@@ -236,8 +237,6 @@ vnet_mpls_local_label (vlib_main_t * vm,
 
   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
     {
-      memset(&rpath, 0, sizeof(rpath));
-
       if (unformat (line_input, "table %d", &table_id))
        ;
       else if (unformat (line_input, "del"))
@@ -264,161 +263,18 @@ vnet_mpls_local_label (vlib_main_t * vm,
          pfx.fp_proto = FIB_PROTOCOL_IP6;
           is_ip = 1;
       }
-      else if (unformat (line_input, "via %U %U weight %u",
-                        unformat_ip4_address,
-                        &rpath.frp_addr.ip4,
-                        unformat_vnet_sw_interface, vnm,
-                        &rpath.frp_sw_if_index,
-                        &rpath.frp_weight))
-      {
-         rpath.frp_proto = DPO_PROTO_IP4;
-         vec_add1(rpaths, rpath);
-      }
-
-      else if (unformat (line_input, "via %U %U weight %u",
-                        unformat_ip6_address,
-                        &rpath.frp_addr.ip6,
-                        unformat_vnet_sw_interface, vnm,
-                        &rpath.frp_sw_if_index,
-                        &rpath.frp_weight))
-      {
-         rpath.frp_proto = DPO_PROTO_IP6;
-         vec_add1(rpaths, rpath);
-      }
-
-      else if (unformat (line_input, "via %U %U",
-                        unformat_ip4_address,
-                        &rpath.frp_addr.ip4,
-                        unformat_vnet_sw_interface, vnm,
-                        &rpath.frp_sw_if_index))
-      {
-         rpath.frp_weight = 1;
-         rpath.frp_proto = DPO_PROTO_IP4;
-         vec_add1(rpaths, rpath);
-      }
-      else if (unformat (line_input, "rx-ip4 %U",
-                        unformat_vnet_sw_interface, vnm,
-                        &rpath.frp_sw_if_index))
-      {
-         rpath.frp_weight = 1;
-         rpath.frp_proto = DPO_PROTO_IP4;
-          rpath.frp_flags = FIB_ROUTE_PATH_INTF_RX;
-         vec_add1(rpaths, rpath);
-      }
-      else if (unformat (line_input, "via %U %U",
-                        unformat_ip6_address,
-                        &rpath.frp_addr.ip6,
-                        unformat_vnet_sw_interface, vnm,
-                        &rpath.frp_sw_if_index))
-      {
-         rpath.frp_weight = 1;
-         rpath.frp_proto = DPO_PROTO_IP6;
-         vec_add1(rpaths, rpath);
-      }
-      else if (unformat (line_input, "via %U next-hop-table %d",
-                        unformat_ip4_address,
-                        &rpath.frp_addr.ip4,
-                        &rpath.frp_fib_index))
-      {
-         rpath.frp_weight = 1;
-         rpath.frp_sw_if_index = ~0;
-         rpath.frp_proto = DPO_PROTO_IP4;
-         vec_add1(rpaths, rpath);
-      }
-      else if (unformat (line_input, "via %U next-hop-table %d",
-                        unformat_ip6_address,
-                        &rpath.frp_addr.ip6,
-                        &rpath.frp_fib_index))
-      {
-         rpath.frp_weight = 1;
-         rpath.frp_sw_if_index = ~0;
-         rpath.frp_proto = DPO_PROTO_IP6;
-         vec_add1(rpaths, rpath);
-      }
       else if (unformat (line_input, "via %U",
-                        unformat_ip4_address,
-                        &rpath.frp_addr.ip4))
+                        unformat_fib_route_path,
+                        &rpath, &payload_proto))
       {
-         /*
-          * the recursive next-hops are by default in the same table
-          * as the prefix
-          */
-         rpath.frp_fib_index = table_id;
-         rpath.frp_weight = 1;
-         rpath.frp_sw_if_index = ~0;
-         rpath.frp_proto = DPO_PROTO_IP4;
-         vec_add1(rpaths, rpath);
-      }
-      else if (unformat (line_input, "via %U",
-                        unformat_ip6_address,
-                        &rpath.frp_addr.ip6))
-      {
-         rpath.frp_fib_index = table_id;
-         rpath.frp_weight = 1;
-         rpath.frp_sw_if_index = ~0;
-         rpath.frp_proto = DPO_PROTO_IP6;
+          pfx.fp_payload_proto = payload_proto;
          vec_add1(rpaths, rpath);
       }
       else if (unformat (line_input, "%d", &local_label))
        ;
-      else if (unformat (line_input,
-                        "ip4-lookup-in-table %d",
-                        &rpath.frp_fib_index))
-      {
-          rpath.frp_proto = DPO_PROTO_IP4;
-          rpath.frp_sw_if_index = FIB_NODE_INDEX_INVALID;
-         pfx.fp_payload_proto = DPO_PROTO_IP4;
-         vec_add1(rpaths, rpath);
-      }
-      else if (unformat (line_input,
-                        "ip6-lookup-in-table %d",
-                        &rpath.frp_fib_index))
-      {
-          rpath.frp_proto = DPO_PROTO_IP6;
-          rpath.frp_sw_if_index = FIB_NODE_INDEX_INVALID;
-         vec_add1(rpaths, rpath);
-         pfx.fp_payload_proto = DPO_PROTO_IP6;
-      }
-      else if (unformat (line_input,
-                        "mpls-lookup-in-table %d",
-                        &rpath.frp_fib_index))
-      {
-          rpath.frp_proto = DPO_PROTO_MPLS;
-          rpath.frp_sw_if_index = FIB_NODE_INDEX_INVALID;
-         pfx.fp_payload_proto = DPO_PROTO_MPLS;
-         vec_add1(rpaths, rpath);
-      }
-      else if (unformat (line_input,
-                        "l2-input-on %U",
-                        unformat_vnet_sw_interface, vnm,
-                        &rpath.frp_sw_if_index))
-      {
-          rpath.frp_proto = DPO_PROTO_ETHERNET;
-         pfx.fp_payload_proto = DPO_PROTO_ETHERNET;
-          rpath.frp_flags = FIB_ROUTE_PATH_INTF_RX;
-         vec_add1(rpaths, rpath);
-      }
-      else if (unformat (line_input, "out-labels"))
-      {
-         if (vec_len (rpaths) == 0)
-          {
-             error = clib_error_return (0, "Paths then labels");
-             goto done;
-          }
-          else
-          {
-              while (unformat (line_input, "%U",
-                               unformat_mpls_unicast_label,
-                               &out_label))
-              {
-                  vec_add1 (rpaths[vec_len (rpaths) - 1].frp_label_stack,
-                            out_label);
-              }
-          }
-      }
       else
       {
-          error = clib_error_return (0, "unkown input: %U",
+          error = clib_error_return (0, "unknown input: %U",
                                      format_unformat_error, line_input);
           goto done;
       }
@@ -456,7 +312,6 @@ vnet_mpls_local_label (vlib_main_t * vm,
   else
   {
       fib_node_index_t fib_index;
-      u32 fi;
 
       if (NULL == rpaths)
       {
@@ -469,24 +324,6 @@ vnet_mpls_local_label (vlib_main_t * vm,
       pfx.fp_label = local_label;
       pfx.fp_payload_proto = rpaths[0].frp_proto;
 
-      /*
-       * the CLI parsing stored table Ids, swap to FIB indicies
-       */
-      if (FIB_NODE_INDEX_INVALID == rpath.frp_sw_if_index)
-      {
-         fi = fib_table_find(dpo_proto_to_fib(pfx.fp_payload_proto),
-                              rpaths[0].frp_fib_index);
-
-         if (~0 == fi)
-         {
-             error = clib_error_return(0 , "%U Via table %d does not exist",
-                                       format_dpo_proto, pfx.fp_payload_proto,
-                                       rpaths[0].frp_fib_index);
-             goto done;
-         }
-         rpaths[0].frp_fib_index = fi;
-      }
-
       fib_index = mpls_fib_index_from_table_id(table_id);
 
       if (FIB_NODE_INDEX_INVALID == fib_index)
@@ -533,25 +370,84 @@ done:
 VLIB_CLI_COMMAND (mpls_local_label_command, static) = {
   .path = "mpls local-label",
   .function = vnet_mpls_local_label,
-  .short_help = "Create/Delete MPL local labels",
+  .short_help = "mpls local-label [add|del] <label-value> [eos|non-eos] via [next-hop-address] [next-hop-interface] [next-hop-table <value>] [weight <value>] [preference <value>] [udp-encap-id <value>] [ip4-lookup-in-table <value>] [ip6-lookup-in-table <value>] [mpls-lookup-in-table <value>] [resolve-via-host] [resolve-via-attached] [rx-ip4 <interface>] [out-labels <value value value>]",
 };
 
-int
-mpls_fib_reset_labels (u32 fib_id)
+clib_error_t *
+vnet_mpls_table_cmd (vlib_main_t * vm,
+                     unformat_input_t * main_input,
+                     vlib_cli_command_t * cmdo)
 {
-  // FIXME
-  return 0;
+  unformat_input_t _line_input, *line_input = &_line_input;
+  clib_error_t *error = NULL;
+  u32 table_id, is_add;
+  u8 *name = NULL;
+
+  is_add = 1;
+  table_id = ~0;
+
+  /* Get a line of input. */
+  if (!unformat_user (main_input, unformat_line_input, line_input))
+    return 0;
+
+  while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
+    {
+      if (unformat (line_input, "%d", &table_id))
+       ;
+      else if (unformat (line_input, "del"))
+       is_add = 0;
+      else if (unformat (line_input, "add"))
+       is_add = 1;
+      else if (unformat (line_input, "name %s", &name))
+       ;
+      else
+       {
+         error = unformat_parse_error (line_input);
+         goto done;
+       }
+    }
+
+  if (~0 == table_id)
+    {
+      error = clib_error_return (0, "No table id");
+      goto done;
+    }
+  else
+    {
+      if (is_add)
+        {
+            mpls_table_create (table_id, 0, name);
+        }
+      else
+        {
+          mpls_table_delete (table_id, 0);
+        }
+    }
+
+ done:
+  unformat_free (line_input);
+  return error;
 }
 
+/* *INDENT-ON* */
+/*?
+ * This command is used to add or delete MPLS Tables. All
+ * Tables must be explicitly added before that can be used,
+ * Including the default table.
+ ?*/
+/* *INDENT-OFF* */
+VLIB_CLI_COMMAND (mpls_table_command, static) = {
+  .path = "mpls table",
+  .short_help = "mpls table [add|del] <table-id>",
+  .function = vnet_mpls_table_cmd,
+  .is_mp_safe = 1,
+};
+
 static clib_error_t *
 mpls_init (vlib_main_t * vm)
 {
-  mpls_main_t * mm = &mpls_main;
   clib_error_t * error;
 
-  mm->vlib_main = vm;
-  mm->vnet_main = vnet_get_main();
-
   if ((error = vlib_call_init_function (vm, ip_main_init)))
     return error;