+
+uword
+unformat_fib_route_path (unformat_input_t * input, va_list * args)
+{
+ fib_route_path_t *rpath = va_arg (*args, fib_route_path_t *);
+ dpo_proto_t *payload_proto = va_arg (*args, void*);
+ dpo_proto_t explicit_proto = DPO_PROTO_NONE;
+ u32 weight, preference, udp_encap_id, fi;
+ mpls_label_t out_label;
+ vnet_main_t *vnm;
+
+ vnm = vnet_get_main ();
+ clib_memset(rpath, 0, sizeof(*rpath));
+ rpath->frp_weight = 1;
+ rpath->frp_sw_if_index = ~0;
+
+ while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
+ {
+ if (unformat (input, "%U %U",
+ unformat_ip4_address,
+ &rpath->frp_addr.ip4,
+ unformat_vnet_sw_interface, vnm,
+ &rpath->frp_sw_if_index))
+ {
+ rpath->frp_proto = DPO_PROTO_IP4;
+ }
+ else if (unformat (input, "%U %U",
+ unformat_ip6_address,
+ &rpath->frp_addr.ip6,
+ unformat_vnet_sw_interface, vnm,
+ &rpath->frp_sw_if_index))
+ {
+ rpath->frp_proto = DPO_PROTO_IP6;
+ }
+ else if (unformat (input, "weight %u", &weight))
+ {
+ rpath->frp_weight = weight;
+ }
+ else if (unformat (input, "preference %u", &preference))
+ {
+ rpath->frp_preference = preference;
+ }
+ else if (unformat (input, "%U next-hop-table %d",
+ unformat_ip4_address,
+ &rpath->frp_addr.ip4,
+ &rpath->frp_fib_index))
+ {
+ rpath->frp_sw_if_index = ~0;
+ rpath->frp_proto = DPO_PROTO_IP4;
+
+ /*
+ * the user enter table-ids, convert to index
+ */
+ fi = fib_table_find (FIB_PROTOCOL_IP4, rpath->frp_fib_index);
+ if (~0 == fi)
+ return 0;
+ rpath->frp_fib_index = fi;
+ }
+ else if (unformat (input, "%U next-hop-table %d",
+ unformat_ip6_address,
+ &rpath->frp_addr.ip6,
+ &rpath->frp_fib_index))
+ {
+ rpath->frp_sw_if_index = ~0;
+ rpath->frp_proto = DPO_PROTO_IP6;
+ fi = fib_table_find (FIB_PROTOCOL_IP6, rpath->frp_fib_index);
+ if (~0 == fi)
+ return 0;
+ rpath->frp_fib_index = fi;
+ }
+ else if (unformat (input, "%U",
+ unformat_ip4_address,
+ &rpath->frp_addr.ip4))
+ {
+ /*
+ * the recursive next-hops are by default in the default table
+ */
+ rpath->frp_fib_index = 0;
+ rpath->frp_sw_if_index = ~0;
+ rpath->frp_proto = DPO_PROTO_IP4;
+ }
+ else if (unformat (input, "%U",
+ unformat_ip6_address,
+ &rpath->frp_addr.ip6))
+ {
+ rpath->frp_fib_index = 0;
+ rpath->frp_sw_if_index = ~0;
+ rpath->frp_proto = DPO_PROTO_IP6;
+ }
+ else if (unformat (input, "udp-encap %d", &udp_encap_id))
+ {
+ rpath->frp_udp_encap_id = udp_encap_id;
+ rpath->frp_flags |= FIB_ROUTE_PATH_UDP_ENCAP;
+ rpath->frp_proto = *payload_proto;
+ }
+ else if (unformat (input, "lookup in table %d", &rpath->frp_fib_index))
+ {
+ rpath->frp_proto = *payload_proto;
+ rpath->frp_sw_if_index = ~0;
+ rpath->frp_flags |= FIB_ROUTE_PATH_DEAG;
+ }
+ else if (unformat (input, "resolve-via-host"))
+ {
+ rpath->frp_flags |= FIB_ROUTE_PATH_RESOLVE_VIA_HOST;
+ }
+ else if (unformat (input, "resolve-via-attached"))
+ {
+ rpath->frp_flags |= FIB_ROUTE_PATH_RESOLVE_VIA_ATTACHED;
+ }
+ else if (unformat (input, "pop-pw-cw"))
+ {
+ rpath->frp_flags |= FIB_ROUTE_PATH_POP_PW_CW;
+ }
+ else if (unformat (input,
+ "ip4-lookup-in-table %d",
+ &rpath->frp_fib_index))
+ {
+ rpath->frp_proto = DPO_PROTO_IP4;
+ *payload_proto = DPO_PROTO_IP4;
+ fi = fib_table_find (FIB_PROTOCOL_IP4, rpath->frp_fib_index);
+ if (~0 == fi)
+ return 0;
+ rpath->frp_fib_index = fi;
+ }
+ else if (unformat (input,
+ "ip6-lookup-in-table %d",
+ &rpath->frp_fib_index))
+ {
+ rpath->frp_proto = DPO_PROTO_IP6;
+ *payload_proto = DPO_PROTO_IP6;
+ fi = fib_table_find (FIB_PROTOCOL_IP6, rpath->frp_fib_index);
+ if (~0 == fi)
+ return 0;
+ rpath->frp_fib_index = fi;
+ }
+ else if (unformat (input,
+ "mpls-lookup-in-table %d",
+ &rpath->frp_fib_index))
+ {
+ rpath->frp_proto = DPO_PROTO_MPLS;
+ *payload_proto = DPO_PROTO_MPLS;
+ fi = fib_table_find (FIB_PROTOCOL_MPLS, rpath->frp_fib_index);
+ if (~0 == fi)
+ return 0;
+ rpath->frp_fib_index = fi;
+ }
+ else if (unformat (input, "src-lookup"))
+ {
+ rpath->frp_flags |= FIB_ROUTE_PATH_SOURCE_LOOKUP;
+ }
+ else if (unformat (input,
+ "l2-input-on %U",
+ unformat_vnet_sw_interface, vnm,
+ &rpath->frp_sw_if_index))
+ {
+ rpath->frp_proto = DPO_PROTO_ETHERNET;
+ *payload_proto = DPO_PROTO_ETHERNET;
+ rpath->frp_flags |= FIB_ROUTE_PATH_INTF_RX;
+ }
+ else if (unformat (input, "via-label %U",
+ unformat_mpls_unicast_label,
+ &rpath->frp_local_label))
+ {
+ rpath->frp_eos = MPLS_NON_EOS;
+ rpath->frp_proto = DPO_PROTO_MPLS;
+ rpath->frp_sw_if_index = ~0;
+ }
+ else if (unformat (input, "rx-ip4 %U",
+ unformat_vnet_sw_interface, vnm,
+ &rpath->frp_sw_if_index))
+ {
+ rpath->frp_proto = DPO_PROTO_IP4;
+ rpath->frp_flags = FIB_ROUTE_PATH_INTF_RX;
+ }
+ else if (unformat (input, "local"))
+ {
+ clib_memset (&rpath->frp_addr, 0, sizeof (rpath->frp_addr));
+ rpath->frp_sw_if_index = ~0;
+ rpath->frp_weight = 1;
+ rpath->frp_flags |= FIB_ROUTE_PATH_LOCAL;
+ }
+ else if (unformat (input, "out-labels"))
+ {
+ while (unformat (input, "%U",
+ unformat_mpls_unicast_label, &out_label))
+ {
+ fib_mpls_label_t fml = {
+ .fml_value = out_label,
+ };
+ vec_add1(rpath->frp_label_stack, fml);
+ }
+ }
+ else if (unformat (input, "ip4"))
+ {
+ explicit_proto = DPO_PROTO_IP4;
+ }
+ else if (unformat (input, "ip6"))
+ {
+ explicit_proto = DPO_PROTO_IP6;
+ }
+ else if (unformat (input, "%U",
+ unformat_vnet_sw_interface, vnm,
+ &rpath->frp_sw_if_index))
+ {
+ rpath->frp_proto = *payload_proto;
+ }
+ else if (unformat (input, "%U",
+ unformat_mfib_itf_flags, &rpath->frp_mitf_flags))
+ ;
+ else if (unformat (input, "via"))
+ {
+ /* new path, back up and return */
+ unformat_put_input (input);
+ unformat_put_input (input);
+ unformat_put_input (input);
+ unformat_put_input (input);
+ break;
+ }
+ else
+ {
+ return (0);
+ }
+ }
+
+ if (DPO_PROTO_NONE != explicit_proto)
+ *payload_proto = rpath->frp_proto = explicit_proto;
+
+ return (1);
+}
+
+/*
+ * Return true if the path is attached
+ */
+int
+fib_route_path_is_attached (const fib_route_path_t *rpath)
+{
+ /*
+ * DVR paths are not attached, since we are not playing the
+ * L3 game with these
+ */
+ if (rpath->frp_flags & (FIB_ROUTE_PATH_DVR |
+ FIB_ROUTE_PATH_UDP_ENCAP))
+ {
+ return (0);
+ }
+
+ /*
+ * - All zeros next-hop
+ * - a valid interface
+ */
+ if (ip46_address_is_zero(&rpath->frp_addr) &&
+ (~0 != rpath->frp_sw_if_index))
+ {
+ return (!0);
+ }
+ else if (rpath->frp_flags & FIB_ROUTE_PATH_ATTACHED ||
+ rpath->frp_flags & FIB_ROUTE_PATH_GLEAN)
+ {
+ return (!0);
+ }
+ return (0);
+}
+
+static void
+fib_prefix_ip4_addr_increment (fib_prefix_t *pfx)
+{
+ /* Calculate the addend based on the host length of address */
+ u32 incr = 1ULL << (32 - pfx->fp_len);
+ ip4_address_t dst = (pfx->fp_addr).ip4;
+ dst.as_u32 = clib_host_to_net_u32 (incr + clib_net_to_host_u32 (dst.as_u32));
+ pfx->fp_addr.ip4.as_u32 = dst.as_u32;
+}
+
+static void
+fib_prefix_ip6_addr_increment (fib_prefix_t *pfx)
+{
+ /*
+ * Calculate the addend based on the host length of address
+ * and which part(lower 64 bits or higher 64 bits) it lies
+ * in
+ */
+ u32 host_len = 128 - pfx->fp_len;
+ u64 incr = 1ULL << ((host_len > 64) ? (host_len - 64) : host_len);
+ i32 bucket = (host_len < 64 ? 1 : 0);
+ ip6_address_t dst = (pfx->fp_addr).ip6;
+ u64 tmp = incr + clib_net_to_host_u64 (dst.as_u64[bucket]);
+ /* Handle overflow */
+ if (bucket && (tmp < incr))
+ {
+ dst.as_u64[1] = clib_host_to_net_u64 (tmp);
+ dst.as_u64[0] = clib_host_to_net_u64 (1ULL + clib_net_to_host_u64 (dst.as_u64[0]));
+ }
+ else
+ dst.as_u64[bucket] = clib_host_to_net_u64 (tmp);
+
+ pfx->fp_addr.ip6.as_u128 = dst.as_u128;
+}
+
+/*
+ * Increase IPv4/IPv6 address according to the prefix length
+ */
+void fib_prefix_increment (fib_prefix_t *pfx)
+{
+ switch (pfx->fp_proto)
+ {
+ case FIB_PROTOCOL_IP4:
+ fib_prefix_ip4_addr_increment (pfx);
+ break;
+ case FIB_PROTOCOL_IP6:
+ fib_prefix_ip6_addr_increment (pfx);
+ break;
+ case FIB_PROTOCOL_MPLS:
+ break;
+ }
+}