VPP-1576: fix a set of coverity warnings
[vpp.git] / src / vnet / gre / gre.c
index 75b1101..028eefb 100644 (file)
@@ -19,6 +19,7 @@
 #include <vnet/gre/gre.h>
 #include <vnet/adj/adj_midchain.h>
 
+#ifndef CLIB_MARCH_VARIANT
 gre_main_t gre_main;
 
 typedef struct
@@ -38,6 +39,7 @@ typedef struct
     u64 as_u64[3];
   };
 } ip6_and_gre_union_t;
+#endif /* CLIB_MARCH_VARIANT */
 
 
 /* Packet trace structure */
@@ -54,7 +56,7 @@ typedef struct
   ip46_address_t dst;
 } gre_tx_trace_t;
 
-u8 *
+static u8 *
 format_gre_tx_trace (u8 * s, va_list * args)
 {
   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
@@ -68,6 +70,7 @@ format_gre_tx_trace (u8 * s, va_list * args)
   return s;
 }
 
+#ifndef CLIB_MARCH_VARIANT
 u8 *
 format_gre_protocol (u8 * s, va_list * args)
 {
@@ -292,8 +295,8 @@ gre6_fixup (vlib_main_t * vm,
   /* Fixup the payload length field in the GRE tunnel encap that was applied
    * at the midchain node */
   ip0->payload_length =
-    clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0))
-    - sizeof (*ip0);
+    clib_host_to_net_u16 (vlib_buffer_length_in_chain (vm, b0) -
+                         sizeof (*ip0));
 }
 
 void
@@ -301,46 +304,47 @@ gre_update_adj (vnet_main_t * vnm, u32 sw_if_index, adj_index_t ai)
 {
   gre_main_t *gm = &gre_main;
   gre_tunnel_t *t;
-  u32 ti;
+  adj_flags_t af;
   u8 is_ipv6;
+  u32 ti;
 
   ti = gm->tunnel_index_by_sw_if_index[sw_if_index];
   t = pool_elt_at_index (gm->tunnels, ti);
   is_ipv6 = t->tunnel_dst.fp_proto == FIB_PROTOCOL_IP6 ? 1 : 0;
+  af = ADJ_FLAG_MIDCHAIN_IP_STACK;
+
+  if (VNET_LINK_ETHERNET == adj_get_link_type (ai))
+    af |= ADJ_FLAG_MIDCHAIN_NO_COUNT;
 
   adj_nbr_midchain_update_rewrite
-    (ai, !is_ipv6 ? gre4_fixup : gre6_fixup, NULL,
-     (VNET_LINK_ETHERNET == adj_get_link_type (ai) ?
-      ADJ_FLAG_MIDCHAIN_NO_COUNT : ADJ_FLAG_NONE),
+    (ai, !is_ipv6 ? gre4_fixup : gre6_fixup, NULL, af,
      gre_build_rewrite (vnm, sw_if_index, adj_get_link_type (ai), NULL));
 
   gre_tunnel_stack (ai);
 }
+#endif /* CLIB_MARCH_VARIANT */
 
 
 typedef enum
 {
-  GRE_ENCAP_NEXT_DROP,
   GRE_ENCAP_NEXT_L2_MIDCHAIN,
   GRE_ENCAP_N_NEXT,
 } gre_encap_next_t;
 
-#define NEXT_IDX (GRE_ENCAP_NEXT_L2_MIDCHAIN)
-
 /**
  * @brief TX function. Only called for L2 payload including TEB or ERSPAN.
  *        L3 traffic uses the adj-midchains.
  */
-static uword
-gre_interface_tx (vlib_main_t * vm,
-                 vlib_node_runtime_t * node, vlib_frame_t * frame)
+VLIB_NODE_FN (gre_encap_node) (vlib_main_t * vm,
+                              vlib_node_runtime_t * node,
+                              vlib_frame_t * frame)
 {
   gre_main_t *gm = &gre_main;
   vnet_main_t *vnm = gm->vnet_main;
   u32 next_index;
   u32 *from, *to_next, n_left_from, n_left_to_next;
-  u32 sw_if_index0 = 0;
-  u32 sw_if_index1 = 0;
+  u32 sw_if_index0 = ~0;
+  u32 sw_if_index1 = ~0;
   adj_index_t adj_index0 = ADJ_INDEX_INVALID;
   adj_index_t adj_index1 = ADJ_INDEX_INVALID;
   gre_tunnel_t *gt0 = NULL;
@@ -353,7 +357,7 @@ gre_interface_tx (vlib_main_t * vm,
   n_left_from = frame->n_vectors;
 
   /* Speculatively send the first buffer to the last disposition we used */
-  next_index = node->cached_next_index;
+  next_index = GRE_ENCAP_NEXT_L2_MIDCHAIN;
 
   while (n_left_from > 0)
     {
@@ -409,11 +413,11 @@ gre_interface_tx (vlib_main_t * vm,
              /* Encap GRE seq# and ERSPAN type II header */
              vlib_buffer_advance (b0, -sizeof (erspan_t2_t));
              erspan_t2_t *h0 = vlib_buffer_get_current (b0);
-             u32 seq_num = clib_smp_atomic_add (&gt0->gre_sn->seq_num, 1);
-             u64 ver1 = clib_host_to_net_u64 (0x1000000000000000);
+             u32 seq_num = clib_atomic_fetch_add (&gt0->gre_sn->seq_num, 1);
+             u64 hdr = clib_host_to_net_u64 (ERSPAN_HDR2);
              h0->seq_num = clib_host_to_net_u32 (seq_num);
-             h0->t2_u64 = ver1;        /* all 0's except ver=1 */
-             h0->t2.cos_en_t_session =
+             h0->t2_u64 = hdr;
+             h0->t2.cos_en_t_session |=
                clib_host_to_net_u16 (gt0->session_id);
            }
          if (PREDICT_FALSE (gt1->type == GRE_TUNNEL_TYPE_ERSPAN))
@@ -421,11 +425,11 @@ gre_interface_tx (vlib_main_t * vm,
              /* Encap GRE seq# and ERSPAN type II header */
              vlib_buffer_advance (b1, -sizeof (erspan_t2_t));
              erspan_t2_t *h1 = vlib_buffer_get_current (b1);
-             u32 seq_num = clib_smp_atomic_add (&gt1->gre_sn->seq_num, 1);
-             u64 ver1 = clib_host_to_net_u64 (0x1000000000000000);
+             u32 seq_num = clib_atomic_fetch_add (&gt1->gre_sn->seq_num, 1);
+             u64 hdr = clib_host_to_net_u64 (ERSPAN_HDR2);
              h1->seq_num = clib_host_to_net_u32 (seq_num);
-             h1->t2_u64 = ver1;        /* all 0's except ver=1 */
-             h1->t2.cos_en_t_session =
+             h1->t2_u64 = hdr;
+             h1->t2.cos_en_t_session |=
                clib_host_to_net_u16 (gt1->session_id);
            }
 
@@ -447,10 +451,6 @@ gre_interface_tx (vlib_main_t * vm,
              tr1->dst = gt1->tunnel_dst.fp_addr;
              tr1->length = vlib_buffer_length_in_chain (vm, b1);
            }
-
-         vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
-                                          to_next, n_left_to_next,
-                                          bi0, bi1, NEXT_IDX, NEXT_IDX);
        }
 
       while (n_left_from > 0 && n_left_to_next > 0)
@@ -480,11 +480,11 @@ gre_interface_tx (vlib_main_t * vm,
              /* Encap GRE seq# and ERSPAN type II header */
              vlib_buffer_advance (b0, -sizeof (erspan_t2_t));
              erspan_t2_t *h0 = vlib_buffer_get_current (b0);
-             u32 seq_num = clib_smp_atomic_add (&gt0->gre_sn->seq_num, 1);
-             u64 ver1 = clib_host_to_net_u64 (0x1000000000000000);
+             u32 seq_num = clib_atomic_fetch_add (&gt0->gre_sn->seq_num, 1);
+             u64 hdr = clib_host_to_net_u64 (ERSPAN_HDR2);
              h0->seq_num = clib_host_to_net_u32 (seq_num);
-             h0->t2_u64 = ver1;        /* all 0's except ver=1 */
-             h0->t2.cos_en_t_session =
+             h0->t2_u64 = hdr;
+             h0->t2.cos_en_t_session |=
                clib_host_to_net_u16 (gt0->session_id);
            }
 
@@ -497,10 +497,6 @@ gre_interface_tx (vlib_main_t * vm,
              tr->dst = gt0->tunnel_dst.fp_addr;
              tr->length = vlib_buffer_length_in_chain (vm, b0);
            }
-
-         vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
-                                          to_next, n_left_to_next,
-                                          bi0, NEXT_IDX);
        }
 
       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
@@ -521,7 +517,6 @@ static char *gre_error_strings[] = {
 /* *INDENT-OFF* */
 VLIB_REGISTER_NODE (gre_encap_node) =
 {
-  .function = gre_interface_tx,
   .name = "gre-encap",
   .vector_size = sizeof (u32),
   .format_trace = format_gre_tx_trace,
@@ -530,12 +525,9 @@ VLIB_REGISTER_NODE (gre_encap_node) =
   .error_strings = gre_error_strings,
   .n_next_nodes = GRE_ENCAP_N_NEXT,
   .next_nodes = {
-    [GRE_ENCAP_NEXT_DROP] = "error-drop",
     [GRE_ENCAP_NEXT_L2_MIDCHAIN] = "adj-l2-midchain",
   },
 };
-
-VLIB_NODE_FUNCTION_MULTIARCH (gre_encap_node, gre_interface_tx)
 /* *INDENT-ON* */
 
 static u8 *
@@ -574,6 +566,7 @@ VNET_DEVICE_CLASS (gre_device_class) = {
 #endif
 };
 
+#ifndef CLIB_MARCH_VARIANT
 VNET_HW_INTERFACE_CLASS (gre_hw_interface_class) = {
   .name = "GRE",
   .format_header = format_gre_header_with_length,
@@ -609,7 +602,7 @@ gre_init (vlib_main_t * vm)
   ip_main_t *im = &ip_main;
   ip_protocol_info_t *pi;
 
-  memset (gm, 0, sizeof (gm[0]));
+  clib_memset (gm, 0, sizeof (gm[0]));
   gm->vlib_main = vm;
   gm->vnet_main = vnet_get_main ();
 
@@ -644,13 +637,7 @@ gre_init (vlib_main_t * vm)
 
 VLIB_INIT_FUNCTION (gre_init);
 
-gre_main_t *
-gre_get_main (vlib_main_t * vm)
-{
-  vlib_call_init_function (vm, gre_init);
-  return &gre_main;
-}
-
+#endif /* CLIB_MARCH_VARIANT */
 
 /*
  * fd.io coding-style-patch-verification: ON