ip: change ip API enums address_family and ip_proto size to u8
[vpp.git] / src / vnet / ip / ip_types_api.c
index d84c1ff..98fb528 100644 (file)
 #undef vl_printfun
 
 int
-ip_address_family_decode (int _af, ip_address_family_t * out)
+ip_address_family_decode (vl_api_address_family_t af,
+                         ip_address_family_t * out)
 {
-  vl_api_address_family_t af = clib_host_to_net_u32 (_af);
-
   switch (af)
     {
     case ADDRESS_IP4:
@@ -44,56 +43,68 @@ ip_address_family_decode (int _af, ip_address_family_t * out)
       *out = AF_IP6;
       return (0);
     }
-  return (-1);
+  return (VNET_API_ERROR_INVALID_ADDRESS_FAMILY);
 }
 
-int
+vl_api_address_family_t
 ip_address_family_encode (ip_address_family_t af)
 {
   switch (af)
     {
     case AF_IP4:
-      return (clib_host_to_net_u32 (ADDRESS_IP4));
+      return (ADDRESS_IP4);
     case AF_IP6:
-      return (clib_host_to_net_u32 (ADDRESS_IP6));
+      return (ADDRESS_IP6);
     }
 
   ASSERT (0);
-  return (clib_host_to_net_u32 (ADDRESS_IP4));
+  return (ADDRESS_IP4);
 }
 
 int
-ip_proto_decode (int _ipp, ip_protocol_t * out)
+ip_proto_decode (vl_api_ip_proto_t ipp, ip_protocol_t * out)
 {
-  vl_api_ip_proto_t ipp = clib_host_to_net_u32 (_ipp);
-
-  switch (ipp)
+  /* Not all protocol are defined in vl_api_ip_proto_t
+   * so we must cast to a different type.
+   */
+  switch ((u8) ipp)
     {
-    case IP_API_PROTO_TCP:
-      *out = IP_PROTOCOL_TCP;
-      return (0);
-    case IP_API_PROTO_UDP:
-      *out = IP_PROTOCOL_UDP;
-      return (0);
+#define ip_protocol(n,s)                       \
+      case IP_PROTOCOL_##s:                    \
+        *out = IP_PROTOCOL_##s;                \
+        return (0);
+#include "protocols.def"
+#undef ip_protocol
     }
-  return (-1);
+  return (VNET_API_ERROR_INVALID_PROTOCOL);
 }
 
-int
+vl_api_ip_proto_t
 ip_proto_encode (ip_protocol_t ipp)
 {
   switch (ipp)
     {
-    case IP_PROTOCOL_UDP:
-      return (clib_host_to_net_u32 (IP_API_PROTO_UDP));
-    case IP_PROTOCOL_TCP:
-      return (clib_host_to_net_u32 (IP_API_PROTO_TCP));
-    default:
-      break;
+#define ip_protocol(n,s)                                \
+      case IP_PROTOCOL_##s:                             \
+        return ((vl_api_ip_proto_t) IP_PROTOCOL_##s);
+#include "protocols.def"
+#undef ip_protocol
     }
 
   ASSERT (0);
-  return (clib_host_to_net_u32 (IP_API_PROTO_TCP));
+  return (IP_API_PROTO_TCP);
+}
+
+ip_dscp_t
+ip_dscp_decode (vl_api_ip_dscp_t in)
+{
+  return ((ip_dscp_t) in);
+}
+
+vl_api_ip_dscp_t
+ip_dscp_encode (ip_dscp_t dscp)
+{
+  return ((vl_api_ip_dscp_t) dscp);
 }
 
 void
@@ -126,7 +137,7 @@ ip_address_union_decode (const vl_api_address_union_t * in,
 {
   ip46_type_t type;
 
-  switch (clib_net_to_host_u32 (af))
+  switch (af)
     {
     case ADDRESS_IP4:
       clib_memset (out, 0, sizeof (*out));
@@ -138,7 +149,6 @@ ip_address_union_decode (const vl_api_address_union_t * in,
       type = IP46_TYPE_IP6;
       break;
     default:
-      ASSERT (!"Unkown address family in API address type");
       type = IP46_TYPE_ANY;
       break;
     }
@@ -152,12 +162,29 @@ ip_address_decode (const vl_api_address_t * in, ip46_address_t * out)
   return (ip_address_union_decode (&in->un, in->af, out));
 }
 
+void
+ip_address_decode2 (const vl_api_address_t * in, ip_address_t * out)
+{
+  switch (ip_address_union_decode (&in->un, in->af, &out->ip))
+    {
+    case IP46_TYPE_IP4:
+      out->version = AF_IP4;
+      break;
+    case IP46_TYPE_IP6:
+      out->version = AF_IP6;
+      break;
+    default:
+      ;
+      break;
+    }
+}
+
 static void
 ip_address_union_encode (const ip46_address_t * in,
                         vl_api_address_family_t af,
                         vl_api_address_union_t * out)
 {
-  if (ADDRESS_IP6 == clib_net_to_host_u32 (af))
+  if (ADDRESS_IP6 == af)
     ip6_address_encode (&in->ip6, out->ip6);
   else
     ip4_address_encode (&in->ip4, out->ip4);
@@ -170,25 +197,42 @@ ip_address_encode (const ip46_address_t * in,
   switch (type)
     {
     case IP46_TYPE_IP4:
-      out->af = clib_net_to_host_u32 (ADDRESS_IP4);
+      out->af = ADDRESS_IP4;
       break;
     case IP46_TYPE_IP6:
-      out->af = clib_net_to_host_u32 (ADDRESS_IP6);
+      out->af = ADDRESS_IP6;
       break;
     case IP46_TYPE_ANY:
       if (ip46_address_is_ip4 (in))
-       out->af = clib_net_to_host_u32 (ADDRESS_IP4);
+       out->af = ADDRESS_IP4;
       else
-       out->af = clib_net_to_host_u32 (ADDRESS_IP6);
+       out->af = ADDRESS_IP6;
       break;
     }
   ip_address_union_encode (in, out->af, &out->un);
 }
 
+void
+ip_address_encode2 (const ip_address_t * in, vl_api_address_t * out)
+{
+  switch (in->version)
+    {
+    case AF_IP4:
+      out->af = ADDRESS_IP4;
+      ip4_address_encode (&in->ip.ip4, out->un.ip4);
+      break;
+    case AF_IP6:
+      out->af = ADDRESS_IP6;
+      ip6_address_encode (&in->ip.ip6, out->un.ip6);
+      break;
+    }
+  ip_address_union_encode (&in->ip, out->af, &out->un);
+}
+
 void
 ip_prefix_decode (const vl_api_prefix_t * in, fib_prefix_t * out)
 {
-  switch (clib_net_to_host_u32 (in->address.af))
+  switch (in->address.af)
     {
     case ADDRESS_IP4:
       out->fp_proto = FIB_PROTOCOL_IP4;
@@ -197,24 +241,58 @@ ip_prefix_decode (const vl_api_prefix_t * in, fib_prefix_t * out)
       out->fp_proto = FIB_PROTOCOL_IP6;
       break;
     }
-  out->fp_len = in->address_length;
+  out->fp_len = in->len;
   out->___fp___pad = 0;
   ip_address_decode (&in->address, &out->fp_addr);
 }
 
+int
+ip_prefix_decode2 (const vl_api_prefix_t * in, ip_prefix_t * out)
+{
+  out->len = in->len;
+  ip_address_decode2 (&in->address, &out->addr);
+
+  if (!ip_prefix_validate (out))
+    return (VNET_API_ERROR_IP_PREFIX_INVALID);
+  return (0);
+}
+
 void
 ip_prefix_encode (const fib_prefix_t * in, vl_api_prefix_t * out)
 {
-  out->address_length = in->fp_len;
-  ip_address_encode (&in->fp_addr,
-                    fib_proto_to_ip46 (in->fp_proto), &out->address);
+  out->len = in->fp_len;
+  ip46_type_t ip46_type;
+
+  switch (in->fp_proto)
+    {
+    case FIB_PROTOCOL_IP4:
+      ip46_type = (IP46_TYPE_IP4);
+      break;
+    case FIB_PROTOCOL_IP6:
+      ip46_type = (IP46_TYPE_IP6);
+      break;
+    case FIB_PROTOCOL_MPLS:
+      ip46_type = (IP46_TYPE_ANY);
+      break;
+    default:
+      ip46_type = (IP46_TYPE_ANY);
+    }
+
+  ip_address_encode (&in->fp_addr, ip46_type, &out->address);
+}
+
+void
+ip_prefix_encode2 (const ip_prefix_t * in, vl_api_prefix_t * out)
+{
+  out->len = in->len;
+  ip_address_encode2 (&in->addr, &out->address);
 }
 
 void
 ip_mprefix_encode (const mfib_prefix_t * in, vl_api_mprefix_t * out)
 {
   out->af = (FIB_PROTOCOL_IP6 == in->fp_proto ? ADDRESS_IP6 : ADDRESS_IP4);
-  out->af = clib_host_to_net_u32 (out->af);
+  out->af = out->af;
   out->grp_address_length = clib_host_to_net_u16 (in->fp_len);
 
   ip_address_union_encode (&in->fp_grp_addr, out->af, &out->grp_address);
@@ -224,12 +302,16 @@ ip_mprefix_encode (const mfib_prefix_t * in, vl_api_mprefix_t * out)
 void
 ip_mprefix_decode (const vl_api_mprefix_t * in, mfib_prefix_t * out)
 {
-  out->fp_proto = (ADDRESS_IP6 == clib_net_to_host_u32 (in->af) ?
+  out->fp_proto = (ADDRESS_IP6 == in->af ?
                   FIB_PROTOCOL_IP6 : FIB_PROTOCOL_IP4);
   out->fp_len = clib_net_to_host_u16 (in->grp_address_length);
+  out->___fp___pad = 0;
 
   ip_address_union_decode (&in->grp_address, in->af, &out->fp_grp_addr);
   ip_address_union_decode (&in->src_address, in->af, &out->fp_src_addr);
+
+  if (!ip46_address_is_zero (&out->fp_src_addr))
+    out->fp_len = (out->fp_proto == FIB_PROTOCOL_IP6 ? 256 : 64);
 }
 
 /*