policer: use ip dscp

Use the common IP definitions of DSCP rather than duplicating in the
policer code.

Type: improvement
Signed-off-by: Brian Russell <brian@graphiant.com>
Change-Id: Iff4bc789356edc290b9c31eca33e93cf5b6211bf
diff --git a/src/vat/api_format.c b/src/vat/api_format.c
index cf9c774..649737b 100644
--- a/src/vat/api_format.c
+++ b/src/vat/api_format.c
@@ -530,11 +530,10 @@
   u8 *r = va_arg (*va, u8 *);
 
   if (0);
-#define _(v,f,str) else if (unformat (input, str)) *r = VNET_DSCP_##f;
-  foreach_vnet_dscp
+#define _(v, f) else if (unformat (input, #f)) *r = IP_DSCP_##f;
+  foreach_ip_dscp
 #undef _
-    else
-    return 0;
+    else return 0;
   return 1;
 }
 
@@ -2751,14 +2750,14 @@
 
   switch (i)
     {
-#define _(v,f,str) case VNET_DSCP_##f: t = str; break;
-      foreach_vnet_dscp
+#define _(v, f)                                                               \
+  case IP_DSCP_##f:                                                           \
+    return (format (s, "%s", #f));
+      foreach_ip_dscp
 #undef _
-    default:
-      return format (s, "ILLEGAL");
     }
   s = format (s, "%s", t);
-  return s;
+  return (format (s, "ILLEGAL"));
 }
 
 static void
diff --git a/src/vnet/policer/police.h b/src/vnet/policer/police.h
index d135e43..6027845 100644
--- a/src/vnet/policer/police.h
+++ b/src/vnet/policer/police.h
@@ -71,7 +71,7 @@
   u32 color_aware;		// for hierarchical policing
   u32 scale;			// power-of-2 shift amount for lower rates
   u8 action[3];
-  u8 mark_dscp[3];
+  ip_dscp_t mark_dscp[3];
   u8 pad[2];
 
   // Fields are marked as 2R if they are only used for a 2-rate policer,
diff --git a/src/vnet/policer/police_inlines.h b/src/vnet/policer/police_inlines.h
index 0ed9730..c3bd324 100644
--- a/src/vnet/policer/police_inlines.h
+++ b/src/vnet/policer/police_inlines.h
@@ -25,7 +25,7 @@
 #define IP6_DSCP_SHIFT    22
 
 static_always_inline void
-vnet_policer_mark (vlib_buffer_t * b, u8 dscp)
+vnet_policer_mark (vlib_buffer_t *b, ip_dscp_t dscp)
 {
   ethernet_header_t *eh;
   ip4_header_t *ip4h;
diff --git a/src/vnet/policer/policer.c b/src/vnet/policer/policer.c
index fbb3055..99c3ff1 100644
--- a/src/vnet/policer/policer.c
+++ b/src/vnet/policer/policer.c
@@ -16,6 +16,7 @@
 #include <vnet/policer/policer.h>
 #include <vnet/policer/police_inlines.h>
 #include <vnet/classify/vnet_classify.h>
+#include <vnet/ip/ip_packet.h>
 
 vnet_policer_main_t vnet_policer_main;
 
@@ -223,24 +224,6 @@
 }
 
 static u8 *
-format_dscp (u8 * s, va_list * va)
-{
-  u32 i = va_arg (*va, u32);
-  char *t = 0;
-
-  switch (i)
-    {
-#define _(v,f,str) case VNET_DSCP_##f: t = str; break;
-      foreach_vnet_dscp
-#undef _
-    default:
-      return format (s, "ILLEGAL");
-    }
-  s = format (s, "%s", t);
-  return s;
-}
-
-static u8 *
 format_policer_action_type (u8 * s, va_list * va)
 {
   sse2_qos_pol_action_params_st *a
@@ -251,7 +234,7 @@
   else if (a->action_type == SSE2_QOS_ACTION_TRANSMIT)
     s = format (s, "transmit");
   else if (a->action_type == SSE2_QOS_ACTION_MARK_AND_TRANSMIT)
-    s = format (s, "mark-and-transmit %U", format_dscp, a->dscp);
+    s = format (s, "mark-and-transmit %U", format_ip_dscp, a->dscp);
   else
     s = format (s, "ILLEGAL");
   return s;
@@ -375,20 +358,6 @@
 }
 
 static uword
-unformat_dscp (unformat_input_t * input, va_list * va)
-{
-  u8 *r = va_arg (*va, u8 *);
-
-  if (0);
-#define _(v,f,str) else if (unformat (input, str)) *r = VNET_DSCP_##f;
-  foreach_vnet_dscp
-#undef _
-    else
-    return 0;
-  return 1;
-}
-
-static uword
 unformat_policer_action_type (unformat_input_t * input, va_list * va)
 {
   sse2_qos_pol_action_params_st *a
@@ -398,7 +367,8 @@
     a->action_type = SSE2_QOS_ACTION_DROP;
   else if (unformat (input, "transmit"))
     a->action_type = SSE2_QOS_ACTION_TRANSMIT;
-  else if (unformat (input, "mark-and-transmit %U", unformat_dscp, &a->dscp))
+  else if (unformat (input, "mark-and-transmit %U", unformat_ip_dscp,
+		     &a->dscp))
     a->action_type = SSE2_QOS_ACTION_MARK_AND_TRANSMIT;
   else
     return 0;
diff --git a/src/vnet/policer/policer.h b/src/vnet/policer/policer.h
index 2e57e46..5253bb6 100644
--- a/src/vnet/policer/policer.h
+++ b/src/vnet/policer/policer.h
@@ -62,36 +62,6 @@
   VNET_POLICER_N_NEXT,
 } vnet_policer_next_t;
 
-#define foreach_vnet_dscp \
-  _(0 , CS0,  "CS0")  \
-  _(8 , CS1,  "CS1")  \
-  _(10, AF11, "AF11") \
-  _(12, AF12, "AF12") \
-  _(14, AF13, "AF13") \
-  _(16, CS2,  "CS2")  \
-  _(18, AF21, "AF21") \
-  _(20, AF22, "AF22") \
-  _(22, AF23, "AF23") \
-  _(24, CS3,  "CS3")  \
-  _(26, AF31, "AF31") \
-  _(28, AF32, "AF32") \
-  _(30, AF33, "AF33") \
-  _(32, CS4,  "CS4")  \
-  _(34, AF41, "AF41") \
-  _(36, AF42, "AF42") \
-  _(38, AF43, "AF43") \
-  _(40, CS5,  "CS5")  \
-  _(46, EF,   "EF")   \
-  _(48, CS6,  "CS6")  \
-  _(50, CS7,  "CS7")
-
-typedef enum
-{
-#define _(v,f,str) VNET_DSCP_##f = v,
-  foreach_vnet_dscp
-#undef _
-} vnet_dscp_t;
-
 u8 *format_policer_instance (u8 * s, va_list * va);
 clib_error_t *policer_add_del (vlib_main_t * vm,
 			       u8 * name,
diff --git a/src/vnet/policer/xlate.h b/src/vnet/policer/xlate.h
index d03930b..5350065 100644
--- a/src/vnet/policer/xlate.h
+++ b/src/vnet/policer/xlate.h
@@ -20,6 +20,7 @@
 #ifndef __included_xlate_h__
 #define __included_xlate_h__
 
+#include <vnet/ip/ip_packet.h>
 #include <vnet/policer/police.h>
 
 /*
@@ -88,7 +89,7 @@
 typedef struct sse2_qos_pol_action_params_st_
 {
   u8 action_type;
-  u8 dscp;
+  ip_dscp_t dscp;
 } sse2_qos_pol_action_params_st;
 
 /*
diff --git a/src/vpp/api/custom_dump.c b/src/vpp/api/custom_dump.c
index cd81a72..8bf2774 100644
--- a/src/vpp/api/custom_dump.c
+++ b/src/vpp/api/custom_dump.c
@@ -2029,7 +2029,6 @@
 {
   u32 action = va_arg (*va, u32);
   u32 dscp = va_arg (*va, u32);
-  char *t = 0;
 
   if (action == SSE2_QOS_ACTION_DROP)
     s = format (s, "drop");
@@ -2040,13 +2039,12 @@
       s = format (s, "mark-and-transmit ");
       switch (dscp)
 	{
-#define _(v,f,str) case VNET_DSCP_##f: t = str; break;
-	  foreach_vnet_dscp
+#define _(v, f)                                                               \
+  case IP_DSCP_##f:                                                           \
+    format (s, "%s", #f);
+	  foreach_ip_dscp
 #undef _
-	default:
-	  break;
 	}
-      s = format (s, "%s", t);
     }
   return s;
 }