Fix compile errors reported by clang

For using clang as a compiler it is enough
to specify CC=clang in the make command line

Change-Id: I06f1c1d418b68768f8119de5bdc8748c51f90c02
Signed-off-by: Damjan Marion <damarion@cisco.com>
diff --git a/dpdk/Makefile b/dpdk/Makefile
index 398fc60..fadbb26 100644
--- a/dpdk/Makefile
+++ b/dpdk/Makefile
@@ -31,7 +31,14 @@
 DPDK_2.2.0_TARBALL_MD5_CKSUM := 22e2fd68cd5504f43fe9a5a6fd6dd938
 DPDK_16.04_TARBALL_MD5_CKSUM := 0728d506d7f56eb64233e824fa3c098a
 DPDK_SOURCE := $(B)/dpdk-$(DPDK_VERSION)
-DPDK_TARGET := x86_64-native-linuxapp-gcc
+
+ifneq (,$(findstring clang,$(CC)))
+DPDK_CC=clang
+else
+DPDK_CC=gcc
+endif
+
+DPDK_TARGET := x86_64-native-linuxapp-$(DPDK_CC)
 JOBS := $(shell grep processor /proc/cpuinfo | wc -l)
 
 # compiler/linker custom arguments
@@ -40,9 +47,9 @@
 DPDK_EXTRA_LDFLAGS := -g
 
 ifeq ($(DPDK_DEBUG),n)
-DPDK_EXTRA_CFLAGS := -g 
+DPDK_EXTRA_CFLAGS := -g
 else
-DPDK_EXTRA_CFLAGS := -g -O0 
+DPDK_EXTRA_CFLAGS := -g -O0
 endif
 
 # translate gcc march values to DPDK arch
diff --git a/dpdk/dpdk-16.04_patches/0010-Fix-O0-clang-build.patch b/dpdk/dpdk-16.04_patches/0010-Fix-O0-clang-build.patch
new file mode 100644
index 0000000..2ce0e7c
--- /dev/null
+++ b/dpdk/dpdk-16.04_patches/0010-Fix-O0-clang-build.patch
@@ -0,0 +1,32 @@
+From 2b82c248638bba6e98ecf388c6e0b1f5f0b44028 Mon Sep 17 00:00:00 2001
+From: Damjan Marion <damarion@cisco.com>
+Date: Tue, 26 Apr 2016 12:36:52 +0200
+Subject: [PATCH] Fix -O0 clang build
+
+Signed-off-by: Damjan Marion <damarion@cisco.com>
+---
+ lib/librte_eal/common/include/arch/x86/rte_rtm.h | 3 +++
+ 1 file changed, 3 insertions(+)
+
+diff --git a/lib/librte_eal/common/include/arch/x86/rte_rtm.h b/lib/librte_eal/common/include/arch/x86/rte_rtm.h
+index d935641..30c1969 100644
+--- a/lib/librte_eal/common/include/arch/x86/rte_rtm.h
++++ b/lib/librte_eal/common/include/arch/x86/rte_rtm.h
+@@ -50,11 +50,14 @@ void rte_xend(void)
+ 	 asm volatile(".byte 0x0f,0x01,0xd5" ::: "memory");
+ }
+ 
++#define rte_xabort(x) asm volatile(".byte 0xc6,0xf8,%P0" :: "i" (x) : "memory")
++#if 0
+ static __attribute__((__always_inline__)) inline
+ void rte_xabort(const unsigned int status)
+ {
+ 	asm volatile(".byte 0xc6,0xf8,%P0" :: "i" (status) : "memory");
+ }
++#endif
+ 
+ static __attribute__((__always_inline__)) inline
+ int rte_xtest(void)
+-- 
+2.7.4
+
diff --git a/vlib/vlib/pci/pci_config.h b/vlib/vlib/pci/pci_config.h
index 38215d8..9cada51 100644
--- a/vlib/vlib/pci/pci_config.h
+++ b/vlib/vlib/pci/pci_config.h
@@ -417,7 +417,7 @@
   while (ttl-- && next_offset >= 0x40)
     {
       c = (void *) t + (next_offset &~ 3);
-      if (c->type == 0xff)
+      if ((u8) c->type == 0xff)
 	break;
       if (c->type == cap_type)
 	return c;
diff --git a/vlib/vlib/unix/pci.c b/vlib/vlib/unix/pci.c
index 75241f3..b28b542 100644
--- a/vlib/vlib/unix/pci.c
+++ b/vlib/vlib/unix/pci.c
@@ -420,7 +420,7 @@
 		 l->bus_address.slot, l->bus_address.function);
 }
 
-static inline pci_device_registration_t *
+pci_device_registration_t * __attribute__((unused))
 pci_device_next_registered (pci_device_registration_t * r)
 {
   uword i;
diff --git a/vnet/vnet/ip/adj_alloc.c b/vnet/vnet/ip/adj_alloc.c
index 5610420..3ae7a19 100644
--- a/vnet/vnet/ip/adj_alloc.c
+++ b/vnet/vnet/ip/adj_alloc.c
@@ -51,7 +51,7 @@
   aa_header_t * ah = aa_header (adjs);
   ip_adjacency_t * adj_block;
   u32 freelist_length;
-  int need_barrier_sync;
+  int need_barrier_sync = 0;
   
   ASSERT(os_get_cpu_number() == 0);
   ASSERT (clib_mem_is_heap_object (_vec_find(ah)));
diff --git a/vnet/vnet/lisp-cp/packets.c b/vnet/vnet/lisp-cp/packets.c
index aa246a9..28471b9 100644
--- a/vnet/vnet/lisp-cp/packets.c
+++ b/vnet/vnet/lisp-cp/packets.c
@@ -225,7 +225,7 @@
 
   udpsum = udp_checksum (uh, clib_net_to_host_u16 (uh->length), ih,
                          ip_addr_version(sip));
-  if (udpsum == -1)
+  if (udpsum == (u16) ~0)
     {
       clib_warning("Failed UDP checksum! Discarding");
       return 0;
diff --git a/vnet/vnet/lisp-gpe/decap.c b/vnet/vnet/lisp-gpe/decap.c
index 5d88462..d0848ab 100644
--- a/vnet/vnet/lisp-gpe/decap.c
+++ b/vnet/vnet/lisp-gpe/decap.c
@@ -104,7 +104,7 @@
           u32 bi0, bi1;
           vlib_buffer_t * b0, * b1;
           ip4_udp_lisp_gpe_header_t * iul0, * iul1;
-          u32 next0, next1, error0, error1;
+          u32 next0, next1, error0 = 0, error1 = 0;
           uword * si0, * si1;
 
           next0 = next1 = LISP_GPE_INPUT_NEXT_IP4_INPUT;
diff --git a/vnet/vnet/lisp-gpe/lisp_gpe.c b/vnet/vnet/lisp-gpe/lisp_gpe.c
index fd1e1a4..a1d4b6f 100644
--- a/vnet/vnet/lisp-gpe/lisp_gpe.c
+++ b/vnet/vnet/lisp-gpe/lisp_gpe.c
@@ -721,7 +721,7 @@
       /* TODO insert tunnel that always sends map-request */
     case DROP:
       /* for drop fwd entries, just add route, no need to add encap tunnel */
-      adj.lookup_next_index = LGPE_IP4_LOOKUP_NEXT_DROP;
+      adj.lookup_next_index = (u16) LGPE_IP4_LOOKUP_NEXT_DROP;
 
       /* add/delete route for prefix */
       return ip4_sd_fib_add_del_route (lgm, dpref, spref, a->table_id, &adj,
diff --git a/vnet/vnet/vcgn/cnat_global.c b/vnet/vnet/vcgn/cnat_global.c
index 9ab89ee..7177083 100644
--- a/vnet/vnet/vcgn/cnat_global.c
+++ b/vnet/vnet/vcgn/cnat_global.c
@@ -57,7 +57,7 @@
  */
 
 /* TOBE_PORTED: Following is in cnat_util.c */
-always_inline
+always_inline __attribute__((unused))
 void ipv4_decr_ttl_n_calc_csum(ipv4_header *ipv4)
 {
     u32 checksum;
diff --git a/vnet/vnet/vcgn/cnat_ipv4_udp_outside_input.c b/vnet/vnet/vcgn/cnat_ipv4_udp_outside_input.c
index f6ffd4e..5a24a11 100644
--- a/vnet/vnet/vcgn/cnat_ipv4_udp_outside_input.c
+++ b/vnet/vnet/vcgn/cnat_ipv4_udp_outside_input.c
@@ -137,8 +137,8 @@
     return CNAT_NO_CONFIG;
 }*/
 
-static inline void swap_ip_dst(ipv4_header *ip, 
-                        cnat_main_db_entry_t *db, u16 vrf)
+static inline void __attribute__((unused))
+swap_ip_dst(ipv4_header *ip, cnat_main_db_entry_t *db, u16 vrf)
 {
 
     CNAT_UPDATE_L3_CHECKSUM_DECLARE
diff --git a/vnet/vnet/vcgn/cnat_logging.c b/vnet/vnet/vcgn/cnat_logging.c
index d404c59..50805d1 100644
--- a/vnet/vnet/vcgn/cnat_logging.c
+++ b/vnet/vnet/vcgn/cnat_logging.c
@@ -2668,7 +2668,7 @@
  */
 
 
-static inline
+static inline __attribute__((unused))
 void handle_vrfid_name_mapping(void)
 {
     cnat_nfv9_logging_info_t    *nfv9_logging_info = NULL;
diff --git a/vnet/vnet/vcgn/cnat_ports.c b/vnet/vnet/vcgn/cnat_ports.c
index 4437865..943fb3e 100644
--- a/vnet/vnet/vcgn/cnat_ports.c
+++ b/vnet/vnet/vcgn/cnat_ports.c
@@ -623,7 +623,7 @@
     for (i = 0; i < max_trys_to_find_port;  i++) {
 
     /* start_bit is only a u16.. so it can rollover and become zero */
-    if (PREDICT_FALSE((start_bit >= BITS_PER_INST) ||
+    if (PREDICT_FALSE( /* (start_bit >= BITS_PER_INST) || FIXME u16 cannot be >= 65536 */
                     (start_bit < static_port_range))) {
                     start_bit = static_port_range;
 #ifndef NO_BULK_LOGGING
diff --git a/vnet/vnet/vcgn/cnat_syslog.c b/vnet/vnet/vcgn/cnat_syslog.c
index 65fde4c..91758f1 100644
--- a/vnet/vnet/vcgn/cnat_syslog.c
+++ b/vnet/vnet/vcgn/cnat_syslog.c
@@ -361,7 +361,7 @@
  * in to the space provided and 
  * returns the number of bytes copied
  */
-inline static int 
+inline static int __attribute__((unused))
 copy_ipv4_addr(unsigned char *ptr, u32 ipv4) 
 {
     unsigned char *temp = ptr; 
diff --git a/vpp/vnet/main.c b/vpp/vnet/main.c
index 4659440..2e08aa5 100644
--- a/vpp/vnet/main.c
+++ b/vpp/vnet/main.c
@@ -67,7 +67,7 @@
     u32 size;
     void vlib_set_get_handoff_structure_cb (void *cb);
 
-#if __x86_64__
+#if __x86_64__ && !defined(__clang__)
     __builtin_cpu_init ();
     const char * msg = "ERROR: This binary requires CPU with %s extensions.\n";
 #define _(a,b) \