X-Git-Url: https://gerrit.fd.io/r/gitweb?a=blobdiff_plain;f=src%2Fvnet%2Ffib%2Ffib_types.c;h=5c5fd0b9823ff8d6b1e4ab395dbfafa14fac19c1;hb=06923b33a9507ee6a92facb770650fff93d89dff;hp=035f70036ebfb03c8b40331a48a98f7463944706;hpb=e927f9a67e68278bc84741b8acf265e941f3c41c;p=vpp.git diff --git a/src/vnet/fib/fib_types.c b/src/vnet/fib/fib_types.c index 035f70036eb..5c5fd0b9823 100644 --- a/src/vnet/fib/fib_types.c +++ b/src/vnet/fib/fib_types.c @@ -17,6 +17,8 @@ #include #include +#include +#include #include /* @@ -76,17 +78,79 @@ format_fib_mpls_label (u8 *s, va_list *ap) } void -fib_prefix_from_ip46_addr (const ip46_address_t *addr, +fib_prefix_from_ip46_addr (fib_protocol_t fproto, + const ip46_address_t *addr, fib_prefix_t *pfx) { - ASSERT(!ip46_address_is_zero(addr)); + ASSERT(FIB_PROTOCOL_MPLS != fproto); - pfx->fp_proto = ((ip46_address_is_ip4(addr) ? - FIB_PROTOCOL_IP4 : - FIB_PROTOCOL_IP6)); - pfx->fp_len = ((ip46_address_is_ip4(addr) ? - 32 : 128)); + pfx->fp_proto = fproto; + pfx->fp_len = ((FIB_PROTOCOL_IP4 == fproto) ? + 32 : 128); pfx->fp_addr = *addr; + pfx->___fp___pad = 0; +} + +u8 * +format_fib_route_path_flags (u8 *s, va_list *ap) +{ + fib_route_path_flags_t flags = va_arg (*ap, fib_route_path_flags_t); + + if (flags & FIB_ROUTE_PATH_RESOLVE_VIA_HOST) + s = format (s, "via-host"); + if (flags & FIB_ROUTE_PATH_RESOLVE_VIA_ATTACHED) + s = format (s, "via-attached,"); + if (flags & FIB_ROUTE_PATH_LOCAL) + s = format (s, "local,"); + if (flags & FIB_ROUTE_PATH_ATTACHED) + s = format (s, "attached,"); + if (flags & FIB_ROUTE_PATH_DROP) + s = format (s, "drop,"); + if (flags & FIB_ROUTE_PATH_EXCLUSIVE) + s = format (s, "exclusive,"); + if (flags & FIB_ROUTE_PATH_INTF_RX) + s = format (s, "intf-rx,"); + if (flags & FIB_ROUTE_PATH_RPF_ID) + s = format (s, "rpf-id,"); + if (flags & FIB_ROUTE_PATH_SOURCE_LOOKUP) + s = format (s, "src-lkup,"); + if (flags & FIB_ROUTE_PATH_UDP_ENCAP) + s = format (s, "udp-encap,"); + if (flags & FIB_ROUTE_PATH_BIER_FMASK) + s = format (s, "bier-fmask,"); + if (flags & FIB_ROUTE_PATH_BIER_TABLE) + s = format (s, "bier-table,"); + if (flags & FIB_ROUTE_PATH_BIER_IMP) + s = format (s, "bier-imp,"); + if (flags & FIB_ROUTE_PATH_DEAG) + s = format (s, "deag,"); + if (flags & FIB_ROUTE_PATH_DVR) + s = format (s, "dvr,"); + if (flags & FIB_ROUTE_PATH_ICMP_UNREACH) + s = format (s, "imcp-unreach,"); + if (flags & FIB_ROUTE_PATH_ICMP_PROHIBIT) + s = format (s, "icmp-prohibit,"); + if (flags & FIB_ROUTE_PATH_CLASSIFY) + s = format (s, "classify,"); + if (flags & FIB_ROUTE_PATH_POP_PW_CW) + s = format (s, "pop-pw-cw,"); + + return (s); +} + +u8 * +format_fib_route_path (u8 *s, va_list *ap) +{ + fib_route_path_t *rpath = va_arg (*ap, fib_route_path_t*); + + s = format (s, "%U %U, %U, [%U]", + format_dpo_proto, rpath->frp_proto, + format_ip46_address, &rpath->frp_addr, IP46_TYPE_ANY, + format_vnet_sw_if_index_name, vnet_get_main (), + rpath->frp_sw_if_index, + format_fib_route_path_flags, rpath->frp_flags); + + return (s); } void @@ -98,6 +162,14 @@ fib_prefix_from_mpls_label (mpls_label_t label, pfx->fp_len = 21; pfx->fp_label = label; pfx->fp_eos = eos; + pfx->___fp___pad = 0; +} + +void +fib_prefix_copy (fib_prefix_t *dst, + const fib_prefix_t *src) +{ + clib_memcpy(dst, src, sizeof(*dst)); } int @@ -157,6 +229,21 @@ fib_prefix_is_cover (const fib_prefix_t *p1, return (0); } +u8 +fib_prefix_get_host_length (fib_protocol_t proto) +{ + switch (proto) + { + case FIB_PROTOCOL_IP4: + return (32); + case FIB_PROTOCOL_IP6: + return (128); + case FIB_PROTOCOL_MPLS: + return (21); + } + return (0); +} + int fib_prefix_is_host (const fib_prefix_t *prefix) { @@ -172,6 +259,25 @@ fib_prefix_is_host (const fib_prefix_t *prefix) return (0); } +void +fib_prefix_normalize (const fib_prefix_t *p, + fib_prefix_t *out) +{ + fib_prefix_copy (out, p); + + switch (p->fp_proto) + { + case FIB_PROTOCOL_IP4: + ip4_address_normalize(&out->fp_addr.ip4, out->fp_len); + break; + case FIB_PROTOCOL_IP6: + ip6_address_normalize(&out->fp_addr.ip6, out->fp_len); + break; + case FIB_PROTOCOL_MPLS: + break; + } +} + u8 * format_fib_prefix (u8 * s, va_list * args) { @@ -282,6 +388,40 @@ fib_proto_to_link (fib_protocol_t proto) return (0); } +ip46_type_t +fib_proto_to_ip46 (fib_protocol_t fproto) +{ + switch (fproto) + { + case FIB_PROTOCOL_IP4: + return (IP46_TYPE_IP4); + case FIB_PROTOCOL_IP6: + return (IP46_TYPE_IP6); + case FIB_PROTOCOL_MPLS: + return (IP46_TYPE_ANY); + } + ASSERT(0); + return (IP46_TYPE_ANY); +} + +fib_protocol_t +fib_proto_from_ip46 (ip46_type_t iproto) +{ + switch (iproto) + { + case IP46_TYPE_IP4: + return FIB_PROTOCOL_IP4; + case IP46_TYPE_IP6: + return FIB_PROTOCOL_IP6; + case IP46_TYPE_ANY: + ASSERT(0); + return FIB_PROTOCOL_IP4; + } + + ASSERT(0); + return FIB_PROTOCOL_IP4; +} + fib_forward_chain_type_t fib_forw_chain_type_from_dpo_proto (dpo_proto_t proto) { @@ -304,6 +444,22 @@ fib_forw_chain_type_from_dpo_proto (dpo_proto_t proto) return (FIB_FORW_CHAIN_TYPE_UNICAST_IP4); } +fib_forward_chain_type_t +fib_forw_chain_type_from_fib_proto (fib_protocol_t proto) +{ + switch (proto) + { + case FIB_PROTOCOL_IP4: + return (FIB_FORW_CHAIN_TYPE_UNICAST_IP4); + case FIB_PROTOCOL_IP6: + return (FIB_FORW_CHAIN_TYPE_UNICAST_IP6); + case FIB_PROTOCOL_MPLS: + return (FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS); + } + ASSERT(0); + return (FIB_FORW_CHAIN_TYPE_UNICAST_IP4); +} + vnet_link_t fib_forw_chain_type_to_link_type (fib_forward_chain_type_t fct) { @@ -383,13 +539,14 @@ uword unformat_fib_route_path (unformat_input_t * input, va_list * args) { fib_route_path_t *rpath = va_arg (*args, fib_route_path_t *); - u32 *payload_proto = va_arg (*args, u32*); - u32 weight, preference, udp_encap_id; + 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 (); - memset(rpath, 0, sizeof(*rpath)); + clib_memset(rpath, 0, sizeof(*rpath)); rpath->frp_weight = 1; rpath->frp_sw_if_index = ~0; @@ -426,6 +583,14 @@ unformat_fib_route_path (unformat_input_t * input, va_list * args) { 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, @@ -434,6 +599,10 @@ unformat_fib_route_path (unformat_input_t * input, va_list * args) { 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, @@ -474,12 +643,20 @@ unformat_fib_route_path (unformat_input_t * input, va_list * args) { 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", @@ -487,6 +664,10 @@ unformat_fib_route_path (unformat_input_t * input, va_list * args) { 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", @@ -494,6 +675,14 @@ unformat_fib_route_path (unformat_input_t * input, va_list * args) { 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", @@ -519,7 +708,14 @@ unformat_fib_route_path (unformat_input_t * input, va_list * args) rpath->frp_proto = DPO_PROTO_IP4; rpath->frp_flags = FIB_ROUTE_PATH_INTF_RX; } - else if (unformat (input, "out-labels")) + 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)) @@ -530,17 +726,126 @@ unformat_fib_route_path (unformat_input_t * input, va_list * args) 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; + } +}