misc: remove GNU Indent directives
[vpp.git] / src / vnet / ip / ip4_packet.h
index c41a802..2690491 100644 (file)
@@ -41,8 +41,8 @@
 #define included_ip4_packet_h
 
 #include <vnet/ip/ip_packet.h> /* for ip_csum_t */
-#include <vnet/tcp/tcp_packet.h>       /* for tcp_header_t */
 #include <vppinfra/byte_order.h>       /* for clib_net_to_host_u16 */
+#include <vppinfra/warnings.h> /* for WARN_OFF/WARN_ON macro */
 
 /* IP4 address which can be accessed either as 4 bytes
    or as a 32-bit number. */
@@ -64,10 +64,11 @@ typedef struct
 } ip4_address_fib_t;
 
 always_inline void
-ip4_addr_fib_init (ip4_address_fib_t * addr_fib, ip4_address_t * address,
-                  u32 fib_index)
+ip4_addr_fib_init (ip4_address_fib_t * addr_fib,
+                  const ip4_address_t * address, u32 fib_index)
 {
-  clib_memcpy (&addr_fib->ip4_addr, address, sizeof (addr_fib->ip4_addr));
+  clib_memcpy_fast (&addr_fib->ip4_addr, address,
+                   sizeof (addr_fib->ip4_addr));
   addr_fib->fib_index = fib_index;
 }
 
@@ -82,52 +83,6 @@ typedef struct
   ip4_address_t addr, mask;
 } ip4_address_and_mask_t;
 
-/* If address is a valid netmask, return length of mask. */
-always_inline uword
-ip4_address_netmask_length (ip4_address_t * a)
-{
-  uword result = 0;
-  uword i;
-  for (i = 0; i < ARRAY_LEN (a->as_u8); i++)
-    {
-      switch (a->as_u8[i])
-       {
-       case 0xff:
-         result += 8;
-         break;
-       case 0xfe:
-         result += 7;
-         goto done;
-       case 0xfc:
-         result += 6;
-         goto done;
-       case 0xf8:
-         result += 5;
-         goto done;
-       case 0xf0:
-         result += 4;
-         goto done;
-       case 0xe0:
-         result += 3;
-         goto done;
-       case 0xc0:
-         result += 2;
-         goto done;
-       case 0x80:
-         result += 1;
-         goto done;
-       case 0x00:
-         result += 0;
-         goto done;
-       default:
-         /* Not a valid netmask mask. */
-         return ~0;
-       }
-    }
-done:
-  return result;
-}
-
 typedef union
 {
   struct
@@ -137,7 +92,7 @@ typedef union
     u8 ip_version_and_header_length;
 
     /* Type of service. */
-    u8 tos;
+    ip_dscp_t tos;
 
     /* Total layer 3 packet length including this header. */
     u16 length;
@@ -174,19 +129,15 @@ typedef union
 
   /* For checksumming we'll want to access IP header in word sized chunks. */
   /* For 64 bit machines. */
-  /* *INDENT-OFF* */
   CLIB_PACKED (struct {
     u64 checksum_data_64[2];
     u32 checksum_data_64_32[1];
   });
-  /* *INDENT-ON* */
 
   /* For 32 bit machines. */
-  /* *INDENT-OFF* */
   CLIB_PACKED (struct {
     u32 checksum_data_32[5];
   });
-  /* *INDENT-ON* */
 } ip4_header_t;
 
 /* Value of ip_version_and_header_length for packets w/o options. */
@@ -195,28 +146,28 @@ typedef union
 
 #define IP4_ROUTER_ALERT_OPTION 20
 
-always_inline int
-ip4_get_fragment_offset (ip4_header_t * i)
+always_inline u16
+ip4_get_fragment_offset (const ip4_header_t * i)
 {
   return clib_net_to_host_u16 (i->flags_and_fragment_offset) & 0x1fff;
 }
 
-always_inline int
-ip4_get_fragment_more (ip4_header_t * i)
+always_inline u16
+ip4_get_fragment_more (const ip4_header_t * i)
 {
   return clib_net_to_host_u16 (i->flags_and_fragment_offset) &
     IP4_HEADER_FLAG_MORE_FRAGMENTS;
 }
 
 always_inline int
-ip4_is_fragment (ip4_header_t * i)
+ip4_is_fragment (const ip4_header_t * i)
 {
   return (i->flags_and_fragment_offset &
          clib_net_to_host_u16 (0x1fff | IP4_HEADER_FLAG_MORE_FRAGMENTS));
 }
 
 always_inline int
-ip4_is_first_fragment (ip4_header_t * i)
+ip4_is_first_fragment (const ip4_header_t * i)
 {
   return (i->flags_and_fragment_offset &
          clib_net_to_host_u16 (0x1fff | IP4_HEADER_FLAG_MORE_FRAGMENTS)) ==
@@ -225,13 +176,13 @@ ip4_is_first_fragment (ip4_header_t * i)
 
 /* Fragment offset in bytes. */
 always_inline int
-ip4_get_fragment_offset_bytes (ip4_header_t * i)
+ip4_get_fragment_offset_bytes (const ip4_header_t * i)
 {
   return 8 * ip4_get_fragment_offset (i);
 }
 
 always_inline int
-ip4_header_bytes (ip4_header_t * i)
+ip4_header_bytes (const ip4_header_t * i)
 {
   return sizeof (u32) * (i->ip_version_and_header_length & 0xf);
 }
@@ -242,31 +193,200 @@ ip4_next_header (ip4_header_t * i)
   return (void *) i + ip4_header_bytes (i);
 }
 
+/* Turn off array bounds check due to ip4_header_t
+   option field operations. */
+
+WARN_OFF(array-bounds)
+
+static_always_inline u16
+ip4_header_checksum_inline (ip4_header_t * i, int with_checksum)
+{
+  int option_len = (i->ip_version_and_header_length & 0xf) - 5;
+  uword sum = 0;
+#if uword_bits == 64
+  u32 *iphdr = (u32 *) i;
+
+  sum += iphdr[0];
+  sum += iphdr[1];
+  sum += with_checksum ? iphdr[2] : *(u16 *) (iphdr + 2);
+  /* skip checksum */
+  sum += iphdr[3];
+  sum += iphdr[4];
+
+  if (PREDICT_FALSE (option_len > 0))
+    switch (option_len)
+      {
+      case 10:
+       sum += iphdr[14];
+      case 9:
+       sum += iphdr[13];
+      case 8:
+       sum += iphdr[12];
+      case 7:
+       sum += iphdr[11];
+      case 6:
+       sum += iphdr[10];
+      case 5:
+       sum += iphdr[9];
+      case 4:
+       sum += iphdr[8];
+      case 3:
+       sum += iphdr[7];
+      case 2:
+       sum += iphdr[6];
+      case 1:
+       sum += iphdr[5];
+      default:
+       break;
+      }
+
+  sum = ((u32) sum) + (sum >> 32);
+#else
+  u16 *iphdr = (u16 *) i;
+
+  sum += iphdr[0];
+  sum += iphdr[1];
+  sum += iphdr[2];
+  sum += iphdr[3];
+  sum += iphdr[4];
+  if (with_checksum)
+    sum += iphdr[5];
+  sum += iphdr[6];
+  sum += iphdr[7];
+  sum += iphdr[8];
+  sum += iphdr[9];
+
+  if (PREDICT_FALSE (option_len > 0))
+    switch (option_len)
+      {
+      case 10:
+       sum += iphdr[28];
+       sum += iphdr[29];
+      case 9:
+       sum += iphdr[26];
+       sum += iphdr[27];
+      case 8:
+       sum += iphdr[24];
+       sum += iphdr[25];
+      case 7:
+       sum += iphdr[22];
+       sum += iphdr[23];
+      case 6:
+       sum += iphdr[20];
+       sum += iphdr[21];
+      case 5:
+       sum += iphdr[18];
+       sum += iphdr[19];
+      case 4:
+       sum += iphdr[16];
+       sum += iphdr[17];
+      case 3:
+       sum += iphdr[14];
+       sum += iphdr[15];
+      case 2:
+       sum += iphdr[12];
+       sum += iphdr[13];
+      case 1:
+       sum += iphdr[10];
+       sum += iphdr[11];
+      default:
+       break;
+      }
+#endif
+
+  sum = ((u16) sum) + (sum >> 16);
+  sum = ((u16) sum) + (sum >> 16);
+  return ~((u16) sum);
+}
+
+WARN_ON(array-bounds)
+
 always_inline u16
 ip4_header_checksum (ip4_header_t * i)
 {
-  u16 save, csum;
-  ip_csum_t sum;
+  return ip4_header_checksum_inline (i, /* with_checksum */ 0);
+}
+
+always_inline void
+ip4_header_set_dscp (ip4_header_t * ip4, ip_dscp_t dscp)
+{
+  ip4->tos &= ~0xfc;
+  /* not masking the dscp value to save th instruction
+   * it shouldn't b necessary since the argument is an enum
+   * whose range is therefore constrained in the CP. in the
+   * DP it will have been taken from another packet, so again
+   * constrained in  value */
+  ip4->tos |= dscp << IP_PACKET_TC_FIELD_DSCP_BIT_SHIFT;
+}
+
+always_inline void
+ip4_header_set_ecn (ip4_header_t * ip4, ip_ecn_t ecn)
+{
+  ip4->tos &= ~IP_PACKET_TC_FIELD_ECN_MASK;
+  ip4->tos |= ecn;
+}
+
+always_inline void
+ip4_header_set_ecn_w_chksum (ip4_header_t * ip4, ip_ecn_t ecn)
+{
+  ip_csum_t sum = ip4->checksum;
+  u8 old = ip4->tos;
+  u8 new = (old & ~IP_PACKET_TC_FIELD_ECN_MASK) | ecn;
+
+  sum = ip_csum_update (sum, old, new, ip4_header_t, tos);
+  ip4->checksum = ip_csum_fold (sum);
+  ip4->tos = new;
+}
+
+always_inline ip_dscp_t
+ip4_header_get_dscp (const ip4_header_t * ip4)
+{
+  return (ip4->tos >> IP_PACKET_TC_FIELD_DSCP_BIT_SHIFT);
+}
+
+always_inline ip_ecn_t
+ip4_header_get_ecn (const ip4_header_t * ip4)
+{
+  return (ip4->tos & IP_PACKET_TC_FIELD_ECN_MASK);
+}
+
+always_inline u8
+ip4_header_get_ttl (const ip4_header_t *ip4)
+{
+  return (ip4->ttl);
+}
 
-  save = i->checksum;
-  i->checksum = 0;
-  sum = ip_incremental_checksum (0, i, ip4_header_bytes (i));
-  csum = ~ip_csum_fold (sum);
+always_inline void
+ip4_header_set_ttl (ip4_header_t *ip4, u8 ttl)
+{
+  ip4->ttl = ttl;
+}
 
-  i->checksum = save;
+always_inline void
+ip4_header_set_df (ip4_header_t * ip4)
+{
+  ip4->flags_and_fragment_offset |=
+    clib_host_to_net_u16 (IP4_HEADER_FLAG_DONT_FRAGMENT);
+}
 
-  /* Make checksum agree for special case where either
-     0 or 0xffff would give same 1s complement sum. */
-  if (csum == 0 && save == 0xffff)
-    csum = save;
+always_inline void
+ip4_header_clear_df (ip4_header_t * ip4)
+{
+  ip4->flags_and_fragment_offset &=
+    ~clib_host_to_net_u16 (IP4_HEADER_FLAG_DONT_FRAGMENT);
+}
 
-  return csum;
+always_inline u8
+ip4_header_get_df (const ip4_header_t * ip4)
+{
+  return (! !(ip4->flags_and_fragment_offset &
+             clib_host_to_net_u16 (IP4_HEADER_FLAG_DONT_FRAGMENT)));
 }
 
 static inline uword
 ip4_header_checksum_is_valid (ip4_header_t * i)
 {
-  return i->checksum == ip4_header_checksum (i);
+  return ip4_header_checksum_inline (i, /* with_checksum */ 1) == 0;
 }
 
 #define ip4_partial_header_checksum_x1(ip0,sum0)                       \
@@ -314,11 +434,17 @@ do {                                                                      \
 } while (0)
 
 always_inline uword
-ip4_address_is_multicast (ip4_address_t * a)
+ip4_address_is_multicast (const ip4_address_t * a)
 {
   return (a->data[0] & 0xf0) == 0xe0;
 }
 
+always_inline uword
+ip4_address_is_global_broadcast (const ip4_address_t * a)
+{
+  return (a->as_u32) == 0xffffffff;
+}
+
 always_inline void
 ip4_multicast_address_set_for_group (ip4_address_t * a,
                                     ip_multicast_group_t g)
@@ -328,9 +454,10 @@ ip4_multicast_address_set_for_group (ip4_address_t * a,
 }
 
 always_inline void
-ip4_multicast_ethernet_address (u8 * ethernet_address, ip4_address_t * a)
+ip4_multicast_ethernet_address (u8 * ethernet_address,
+                               const ip4_address_t * a)
 {
-  u8 *d = a->as_u8;
+  const u8 *d = a->as_u8;
 
   ethernet_address[0] = 0x01;
   ethernet_address[1] = 0x00;
@@ -340,47 +467,6 @@ ip4_multicast_ethernet_address (u8 * ethernet_address, ip4_address_t * a)
   ethernet_address[5] = d[3];
 }
 
-always_inline void
-ip4_tcp_reply_x1 (ip4_header_t * ip0, tcp_header_t * tcp0)
-{
-  u32 src0, dst0;
-
-  src0 = ip0->src_address.data_u32;
-  dst0 = ip0->dst_address.data_u32;
-  ip0->src_address.data_u32 = dst0;
-  ip0->dst_address.data_u32 = src0;
-
-  src0 = tcp0->src;
-  dst0 = tcp0->dst;
-  tcp0->src = dst0;
-  tcp0->dst = src0;
-}
-
-always_inline void
-ip4_tcp_reply_x2 (ip4_header_t * ip0, ip4_header_t * ip1,
-                 tcp_header_t * tcp0, tcp_header_t * tcp1)
-{
-  u32 src0, dst0, src1, dst1;
-
-  src0 = ip0->src_address.data_u32;
-  src1 = ip1->src_address.data_u32;
-  dst0 = ip0->dst_address.data_u32;
-  dst1 = ip1->dst_address.data_u32;
-  ip0->src_address.data_u32 = dst0;
-  ip1->src_address.data_u32 = dst1;
-  ip0->dst_address.data_u32 = src0;
-  ip1->dst_address.data_u32 = src1;
-
-  src0 = tcp0->src;
-  src1 = tcp1->src;
-  dst0 = tcp0->dst;
-  dst1 = tcp1->dst;
-  tcp0->src = dst0;
-  tcp1->src = dst1;
-  tcp0->dst = src0;
-  tcp1->dst = src1;
-}
-
 #endif /* included_ip4_packet_h */
 
 /*