Skip to content

Commit 0557502

Browse files
committed
Use config.h file to fix use of #ifdef in headers
Ideally we would not use custom #ifdef in headers at all but this is a bigger project for the future... the bmv2 library with BMDEBUG_ON defined but within a separate project (simple_switch_grpc) without BMDEBUG_ON defined, which was causing the bm::Field class to have a different layout in the library and in the application. This was causing different segfaults based on the other preprocessor flags and compiler flags used and was difficult to diagnose (wasn't caught by Valgrind). To avoid this issue, we now rely on the config.h file generated from configure.ac. This file needs to be installed because it is required by installed bmv2 headers. To avoid interfering with other libraries, we use AX_PREFIX_CONFIG_H to install config.h under @includedir@/bm and to prefix all defined macros with BM_. Fixes #715
1 parent cace13e commit 0557502

31 files changed

+330
-82
lines changed

Doxyfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1935,7 +1935,7 @@ INCLUDE_FILE_PATTERNS =
19351935
# recursively expanded use the := operator instead of the = operator.
19361936
# This tag requires that the tag ENABLE_PREPROCESSING is set to YES.
19371937

1938-
PREDEFINED = BMELOG_ON BMLOG_DEBUG_ON BMLOG_TRACE_ON
1938+
PREDEFINED = BM_ELOG_ON BM_LOG_DEBUG_ON BM_LOG_TRACE_ON
19391939

19401940
# If the MACRO_EXPANSION and EXPAND_ONLY_PREDEF tags are set to YES then this
19411941
# tag can be used to specify a list of macro names that should be expanded. The

autogen.sh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
#!/bin/sh
22

3-
exec autoreconf -fi
3+
# generates config.h.in
4+
autoheader
5+
6+
autoreconf -fi

configure.ac

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@ AC_ARG_WITH([pdfixed],
4545

4646
AM_CONDITIONAL([COND_PDFIXED], [test "$want_pdfixed" = yes])
4747

48-
MY_CPPFLAGS=""
49-
5048
AC_ARG_WITH([nanomsg],
5149
AS_HELP_STRING([--with-nanomsg], [Build Nanomsg RPC service, if disabled then you must have some other way of controlling the switch]),
5250
[want_nanomsg="$withval"], [want_nanomsg=yes])
@@ -59,7 +57,7 @@ AC_ARG_ENABLE([debugger],
5957
AS_IF([test "x$enable_debugger" = "xyes"], [
6058
AS_IF([test "$want_nanomsg" = "yes"], [
6159
debugger_enabled=yes
62-
MY_CPPFLAGS="$MY_CPPFLAGS -DBMDEBUG_ON"
60+
AC_DEFINE([DEBUG_ON], [], [Enable debugger])
6361
], [
6462
AC_MSG_ERROR([Cannot use debugger without nanomsg])
6563
])
@@ -71,10 +69,11 @@ AC_ARG_ENABLE([logging_macros],
7169
[Disable compile time debug and trace logging macros]))
7270
AS_IF([test "x$enable_logging_macros" != "xno"], [
7371
logging_macros_enabled=yes
74-
MY_CPPFLAGS="$MY_CPPFLAGS -DBMLOG_DEBUG_ON -DBMLOG_TRACE_ON"
72+
AC_DEFINE([LOG_DEBUG_ON], [], [Enable compile-time macro for debug logging])
73+
AC_DEFINE([LOG_TRACE_ON], [], [Enable compile-time macro for trace logging])
7574
])
7675

77-
# BMELOG_ON is defined by default, since it is required for some tests
76+
# BM_ELOG_ON is defined by default, since it is required for some tests
7877
elogger_enabled=no
7978
AC_ARG_ENABLE([elogger],
8079
AS_HELP_STRING([--disable-elogger],
@@ -83,7 +82,7 @@ AC_ARG_ENABLE([elogger],
8382
AS_IF([test "x$enable_elogger" != "xno"], [
8483
AS_IF([test "$want_nanomsg" = "yes"], [
8584
elogger_enabled=yes
86-
MY_CPPFLAGS="$MY_CPPFLAGS -DBMELOG_ON"
85+
AC_DEFINE([ELOG_ON], [], [Enable nanomsg event logger])
8786
], [
8887
AC_MSG_WARN([Cannot use elogger without nanomsg])
8988
])
@@ -118,7 +117,7 @@ AC_ARG_ENABLE([WP4-16-stacks],
118117
[enable_WP4_16_stacks="$enableval"], [enable_WP4_16_stacks=yes])
119118

120119
AS_IF([test "$enable_WP4_16_stacks" = "yes"],
121-
[MY_CPPFLAGS="$MY_CPPFLAGS -DBM_WP4_16_STACKS"])
120+
[AC_DEFINE([WP4_16_STACKS], [], [Implement stacks as per P4_16 spec])])
122121

123122
# Checks for programs.
124123
AC_PROG_CXX
@@ -150,17 +149,17 @@ AS_IF([test "$want_thrift" = yes], [
150149
AS_IF([test "$want_p4thrift" = yes], [
151150
AC_PATH_PROG([THRIFT], [p4thrift], [])
152151
AC_SUBST([THRIFT_LIB], ["-lp4thrift"])
153-
MY_CPPFLAGS="$MY_CPPFLAGS -DP4THRIFT"
152+
AC_DEFINE([P4THRIFT], [], [Use P4.org Thrift fork])
154153
AC_CHECK_HEADER([p4thrift/P4Thrift.h], [], [AC_MSG_ERROR([P4Thrift headers not found. Install P4Thrift from http://github.com/p4lang/thrift/])])
155154
], [
156155
AC_PATH_PROG([THRIFT], [thrift], [])
157156
AC_SUBST([THRIFT_LIB], ["-lthrift"])
158157
AC_CHECK_HEADER([thrift/Thrift.h], [], [AC_MSG_ERROR([Thrift headers not found. Install Thrift from http://thrift.apache.org/docs/install/])])
159158
])
160159
AS_IF([test x"$THRIFT" = x], [AC_MSG_ERROR([cannot find thrift])])
161-
MY_CPPFLAGS="$MY_CPPFLAGS -DBMTHRIFT_ON"
160+
AC_DEFINE([THRIFT_ON], [], [Enable Thrift support])
162161
AC_CHECK_HEADER([thrift/stdcxx.h], [
163-
MY_CPPFLAGS="$MY_CPPFLAGS -DHAVE_THRIFT_STDCXX_H"
162+
AC_DEFINE([HAVE_THRIFT_STDCXX_H], [], [Found Thrift stdcxx wrapper])
164163
], [])
165164
])
166165

@@ -178,7 +177,7 @@ utility vector], [], [AC_MSG_ERROR([Missing header file])])
178177

179178
AS_IF([test "$want_nanomsg" = yes], [
180179
AC_CHECK_LIB([nanomsg], [nn_errno], [], [AC_MSG_ERROR([Missing libnanomsg])])
181-
MY_CPPFLAGS="$MY_CPPFLAGS -DBMNANOMSG_ON"
180+
AC_DEFINE([NANOMSG_ON], [], [Enable Nanomsg support])
182181
])
183182

184183
# Check for pthread, libjudy, libgmp, libpcap
@@ -222,7 +221,8 @@ AS_IF([test "x$enable_modules" != "xno"], [
222221
AC_MSG_CHECKING(for dlopen())
223222
AC_CHECK_HEADERS(dlfcn.h, [
224223
AC_SEARCH_LIBS([dlopen], [dl], [
225-
MY_CPPFLAGS="$MY_CPPFLAGS -DBM_HAVE_DLOPEN -DBM_ENABLE_MODULES"
224+
AC_DEFINE([HAVE_DLOPEN], [], [Found dlopen])
225+
AC_DEFINE([ENABLE_MODULES], [], [Enable dynamic loading of modules])
226226
modules_enabled=yes
227227
], [
228228
AC_MSG_RESULT(no)
@@ -252,8 +252,8 @@ AC_CHECK_HEADER([boost/program_options.hpp], [], [AC_MSG_ERROR([Missing boost pr
252252
AC_CHECK_HEADER([boost/functional/hash.hpp], [], [AC_MSG_ERROR([Missing boost functional hash header])])
253253
AC_CHECK_HEADER([boost/filesystem.hpp], [], [AC_MSG_ERROR([Missing boost filesystem header])])
254254

255-
AC_SUBST([AM_CPPFLAGS], ["$MY_CPPFLAGS \
256-
-I\$(top_srcdir)/include \
255+
AC_SUBST([AM_CPPFLAGS], ["-I\$(top_srcdir)/include \
256+
-I\$(top_builddir)/include \
257257
-isystem\$(top_srcdir)/third_party/jsoncpp/include \
258258
-isystem\$(top_srcdir)/third_party/spdlog"])
259259
AC_SUBST([AM_CFLAGS], ["$PTHREAD_CFLAGS"])
@@ -308,6 +308,8 @@ AC_CONFIG_FILES([tests/utils.cpp
308308
AC_CONFIG_FILES([targets/simple_switch/tests/CLI_tests/run_one_test.py],
309309
[chmod +x targets/simple_switch/tests/CLI_tests/run_one_test.py])
310310

311+
AX_PREFIX_CONFIG_H([include/bm/config.h], [BM])
312+
311313
AC_OUTPUT
312314

313315
AS_ECHO("")

include/Makefile.am

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
ACLOCAL_AMFLAGS = ${ACLOCAL_FLAGS} -I m4
22

3-
nobase_include_HEADERS =
3+
nobase_include_HEADERS = \
4+
bm/config.h
5+
6+
distclean-local: distclean-ax-prefix-config-h
7+
distclean-ax-prefix-config-h:
8+
rm -f bm/config.h
49

510
if COND_NANOMSG
611
nobase_include_HEADERS += \

include/bm/bm_runtime/bm_runtime.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#ifndef _BM_RUNTIME_BM_RUNTIME_H_
22
#define _BM_RUNTIME_BM_RUNTIME_H_
33

4-
#ifdef P4THRIFT
4+
#include <bm/config.h>
5+
6+
#ifdef BM_P4THRIFT
57
#include <p4thrift/processor/TMultiplexedProcessor.h>
68

79
namespace thrift_provider = p4::thrift;
@@ -36,4 +38,4 @@ int start_server(bm::SwitchWContexts *sw, int port);
3638

3739
}
3840

39-
#endif
41+
#endif // _BM_RUNTIME_BM_RUNTIME_H_

include/bm/bm_sim/debugger.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@
2121
#ifndef BM_BM_SIM_DEBUGGER_H_
2222
#define BM_BM_SIM_DEBUGGER_H_
2323

24+
#include <bm/config.h>
25+
2426
#include <limits>
2527
#include <string>
2628

27-
// #define BMDEBUG_ON
28-
2929
namespace bm {
3030

3131
using device_id_t = uint64_t;
@@ -145,7 +145,7 @@ class DebuggerIface {
145145
virtual std::string get_addr_() const = 0;
146146
};
147147

148-
#ifdef BMDEBUG_ON
148+
#ifdef BM_DEBUG_ON
149149
#define DEBUGGER_NOTIFY_UPDATE(packet_id, id, bytes, nbits) \
150150
Debugger::get()->notify_update(packet_id, id, bytes, nbits);
151151
#define DEBUGGER_NOTIFY_UPDATE_V(packet_id, id, v) \

include/bm/bm_sim/dev_mgr.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
#ifndef BM_BM_SIM_DEV_MGR_H_
3636
#define BM_BM_SIM_DEV_MGR_H_
3737

38+
#include <bm/config.h>
39+
3840
#include <functional>
3941
#include <string>
4042
#include <map>
@@ -170,7 +172,7 @@ class DevMgr : public PacketDispatcherIface {
170172
// wait before starting to process packets.
171173
void set_dev_mgr_files(unsigned wait_time_in_seconds);
172174

173-
#ifdef BMNANOMSG_ON
175+
#ifdef BM_NANOMSG_ON
174176
// if enforce ports is set to true, packets coming in on un-registered ports
175177
// are dropped
176178
void set_dev_mgr_packet_in(

include/bm/bm_sim/event_logger.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
#ifndef BM_BM_SIM_EVENT_LOGGER_H_
2424
#define BM_BM_SIM_EVENT_LOGGER_H_
2525

26+
#include <bm/config.h>
27+
2628
#include <string>
2729
#include <memory>
2830

@@ -124,7 +126,7 @@ class EventLogger {
124126
//! // packet processing
125127
//! BMELOG(packet_out, packet);
126128
//! @endcode
127-
#ifdef BMELOG_ON
129+
#ifdef BM_ELOG_ON
128130
#define BMELOG(fn, ...) bm::EventLogger::get()->fn(__VA_ARGS__)
129131
#else
130132
#define BMELOG(fn, ...)

include/bm/bm_sim/fields.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
#ifndef BM_BM_SIM_FIELDS_H_
2424
#define BM_BM_SIM_FIELDS_H_
2525

26+
#include <bm/config.h>
27+
2628
#include <algorithm> // for std::copy
2729

2830
#include <cassert>
@@ -143,7 +145,7 @@ class Field : public Data {
143145

144146
void reset_VL();
145147

146-
#ifdef BMDEBUG_ON
148+
#ifdef BM_DEBUG_ON
147149
void set_id(uint64_t id) { my_id = id; }
148150
void set_packet_id(const Debugger::PacketId *id) { packet_id = id; }
149151
#else
@@ -189,7 +191,7 @@ class Field : public Data {
189191
Bignum mask{1};
190192
Bignum max{1};
191193
Bignum min{1};
192-
#ifdef BMDEBUG_ON
194+
#ifdef BM_DEBUG_ON
193195
uint64_t my_id{};
194196
const Debugger::PacketId *packet_id{&Debugger::dummy_PacketId};
195197
#endif

include/bm/bm_sim/headers.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
#ifndef BM_BM_SIM_HEADERS_H_
2424
#define BM_BM_SIM_HEADERS_H_
2525

26+
#include <bm/config.h>
27+
2628
#include <vector>
2729
#include <string>
2830
#include <set>
@@ -265,7 +267,7 @@ class Header : public NamedP4Object {
265267
// same value.
266268
bool cmp(const Header &other) const;
267269

268-
#ifdef BMDEBUG_ON
270+
#ifdef BM_DEBUG_ON
269271
void set_packet_id(const Debugger::PacketId *id);
270272
#else
271273
void set_packet_id(const Debugger::PacketId *) { }
@@ -313,7 +315,7 @@ class Header : public NamedP4Object {
313315
int nbytes_packet{0};
314316
std::unique_ptr<ArithExpression> VL_expr;
315317
std::unique_ptr<UnionMembership> union_membership{nullptr};
316-
#ifdef BMDEBUG_ON
318+
#ifdef BM_DEBUG_ON
317319
const Debugger::PacketId *packet_id{&Debugger::dummy_PacketId};
318320
#endif
319321
};

0 commit comments

Comments
 (0)