[ovs-dev] [PATCH] Add build checks for portable OpenFlow structure padding and alignment.

Ben Pfaff blp at nicira.com
Wed Jan 20 01:21:26 UTC 2010


This causes the build to fail with an error message if openflow.h contains
a structure whose members are not aligned in a portable way.
---
 configure.ac                 |    5 ++++-
 include/openflow/.gitignore  |    1 +
 include/openflow/automake.mk |   12 ++++++++++++
 m4/openvswitch.m4            |    7 ++++++-
 tests/atlocal.in             |    6 ++++--
 tests/automake.mk            |    1 +
 tests/check-structs.at       |   41 +++++++++++++++++++++++++++++++++++++++++
 tests/testsuite.at           |    1 +
 8 files changed, 70 insertions(+), 4 deletions(-)
 create mode 100644 include/openflow/.gitignore
 create mode 100644 tests/check-structs.at

diff --git a/configure.ac b/configure.ac
index 92d5ac0..2f5c87f 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1,4 +1,4 @@
-# Copyright (c) 2008, 2009 Nicira Networks
+# Copyright (c) 2008, 2009, 2010 Nicira Networks
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
@@ -88,4 +88,7 @@ datapath/linux-2.6/Makefile
 datapath/linux-2.6/Makefile.main
 tests/atlocal])
 
+dnl This makes sure that include/openflow gets created in the build directory.
+AC_CONFIG_COMMANDS([include/openflow/openflow.h.stamp])
+
 AC_OUTPUT
diff --git a/include/openflow/.gitignore b/include/openflow/.gitignore
new file mode 100644
index 0000000..db736db
--- /dev/null
+++ b/include/openflow/.gitignore
@@ -0,0 +1 @@
+/openflow.h.stamp
diff --git a/include/openflow/automake.mk b/include/openflow/automake.mk
index d473155..c06c39c 100644
--- a/include/openflow/automake.mk
+++ b/include/openflow/automake.mk
@@ -2,3 +2,15 @@ noinst_HEADERS += \
 	include/openflow/openflow-mgmt.h \
 	include/openflow/nicira-ext.h \
 	include/openflow/openflow.h
+
+if HAVE_PYTHON
+all-local: include/openflow/openflow.h.stamp
+include/openflow/openflow.h.stamp: \
+	include/openflow/openflow.h \
+	build-aux/check-structs
+	$(PYTHON) $(srcdir)/build-aux/check-structs < $(srcdir)/include/openflow/openflow.h
+	touch include/openflow/openflow.h.stamp
+endif
+
+EXTRA_DIST += build-aux/check-structs
+DISTCLEANFILES += include/openflow/openflow.h.stamp 
diff --git a/m4/openvswitch.m4 b/m4/openvswitch.m4
index 0890b9f..bdb8b19 100644
--- a/m4/openvswitch.m4
+++ b/m4/openvswitch.m4
@@ -256,7 +256,12 @@ else:
           done
         done
       fi])
+   AC_SUBST([HAVE_PYTHON])
    AM_MISSING_PROG([PYTHON], [python])
    if test $ovs_cv_python != no; then
      PYTHON=$ovs_cv_python
-   fi])
+     HAVE_PYTHON=yes
+   else
+     HAVE_PYTHON=no
+   fi
+   AM_CONDITIONAL([HAVE_PYTHON], [test "$HAVE_PYTHON" = yes])])
diff --git a/tests/atlocal.in b/tests/atlocal.in
index d0149c1..5ff0cd2 100644
--- a/tests/atlocal.in
+++ b/tests/atlocal.in
@@ -1,4 +1,6 @@
 # -*- shell-script -*-
-PERL='@PERL@'
-LCOV='@LCOV@'
 HAVE_OPENSSL='@HAVE_OPENSSL@'
+HAVE_PYTHON='@HAVE_PYTHON@'
+LCOV='@LCOV@'
+PERL='@PERL@'
+PYTHON='@PYTHON@'
diff --git a/tests/automake.mk b/tests/automake.mk
index dc677eb..044a109 100644
--- a/tests/automake.mk
+++ b/tests/automake.mk
@@ -9,6 +9,7 @@ TESTSUITE_AT = \
 	tests/ovsdb-macros.at \
 	tests/lcov-pre.at \
 	tests/library.at \
+	tests/check-structs.at \
 	tests/daemon.at \
 	tests/vconn.at \
 	tests/dir_name.at \
diff --git a/tests/check-structs.at b/tests/check-structs.at
new file mode 100644
index 0000000..3ccae64
--- /dev/null
+++ b/tests/check-structs.at
@@ -0,0 +1,41 @@
+AT_BANNER([struct alignment checker unit tests])
+
+m4_define([check_structs], [$top_srcdir/build-aux/check-structs])
+m4_define([RUN_STRUCT_CHECKER], 
+  [AT_SKIP_IF([test $HAVE_PYTHON = no])
+   AT_DATA([test.h], [$1
+])
+   AT_CHECK_UNQUOTED([$PYTHON check_structs < test.h], [$2], [$3], [$4])])
+
+AT_SETUP([check struct tail padding])
+RUN_STRUCT_CHECKER(
+[struct xyz {
+    uint16_t x;
+};], 
+  [1], [], 
+  [check_structs: line 3: warning: struct xyz needs 2 bytes of tail padding
+])
+AT_CLEANUP
+
+AT_SETUP([check struct internal alignment])
+RUN_STRUCT_CHECKER(
+[struct xyzzy {
+    uint16_t x;
+    uint32_t y;
+};], 
+  [1], [], 
+  [check_structs: line 3: warning: struct xyzzy member y is 2 bytes short of 4-byte alignment
+])
+AT_CLEANUP
+
+AT_SETUP([check struct declared size])
+RUN_STRUCT_CHECKER(
+[struct wibble {
+    uint64_t z;
+};
+OFP_ASSERT(sizeof(struct wibble) == 12);
+], 
+  [1], [], 
+  [check_structs: line 4: warning: struct wibble is 8 bytes long but declared as 12
+])
+AT_CLEANUP
diff --git a/tests/testsuite.at b/tests/testsuite.at
index 93d7e6e..0685892 100644
--- a/tests/testsuite.at
+++ b/tests/testsuite.at
@@ -39,6 +39,7 @@ m4_include([tests/ovsdb-macros.at])
 m4_include([tests/lcov-pre.at])
 
 m4_include([tests/library.at])
+m4_include([tests/check-structs.at])
 m4_include([tests/daemon.at])
 m4_include([tests/vconn.at])
 m4_include([tests/dir_name.at])
-- 
1.6.3.3





More information about the dev mailing list