1 Multi-Architecture Graph Node Cookbook
2 ======================================
4 In the context of graph node dispatch functions, it's easy enough to
5 use the vpp multi-architecture support setup. The point of the scheme
6 is simple: for performance-critical nodes, generate multiple CPU
7 hardware-dependent versions of the node dispatch functions, and pick
8 the best one at runtime.
10 The vpp scheme is simple enough to use, but details matter.
15 We compile entire graph node dispatch function implementation files
16 multiple times. These compilations give rise to multiple versions of
17 the graph node dispatch functions. Per-node constructor-functions
18 interrogate CPU hardware, select the node dispatch function variant to
19 use, and set the vlib_node_registration_t ".function" member to the
20 address of the selected variant.
25 Declare the node dispatch function as shown, using the VLIB\_NODE\_FN macro. The
26 name of the node function **MUST** match the name of the graph node.
30 VLIB_NODE_FN (ip4_sdp_node) (vlib_main_t * vm, vlib_node_runtime_t * node,
33 if (PREDICT_FALSE (node->flags & VLIB_NODE_FLAG_TRACE))
34 return ip46_sdp_inline (vm, node, frame, 1 /* is_ip4 */ ,
37 return ip46_sdp_inline (vm, node, frame, 1 /* is_ip4 */ ,
41 We need to generate *precisely one copy* of the
42 vlib_node_registration_t, error strings, and packet trace decode function.
44 Simply bracket these items with "#ifndef CLIB_MARCH_VARIANT...#endif":
48 #ifndef CLIB_MARCH_VARIANT
50 format_sdp_trace (u8 * s, va_list * args)
58 #ifndef CLIB_MARCH_VARIANT
59 static char *sdp_error_strings[] = {
60 #define _(sym,string) string,
68 #ifndef CLIB_MARCH_VARIANT
69 VLIB_REGISTER_NODE (ip4_sdp_node) =
71 // DO NOT set the .function structure member.
72 // The multiarch selection __attribute__((constructor)) function
73 // takes care of it at runtime
75 .vector_size = sizeof (u32),
76 .format_trace = format_sdp_trace,
77 .type = VLIB_NODE_TYPE_INTERNAL,
79 .n_errors = ARRAY_LEN(sdp_error_strings),
80 .error_strings = sdp_error_strings,
82 .n_next_nodes = SDP_N_NEXT,
84 /* edit / add dispositions here */
87 [SDP_NEXT_DROP] = "ip4-drop",
92 To belabor the point: *do not* set the ".function" member! That's the job of the multi-arch
93 selection \_\_attribute\_\_((constructor)) function
95 Always inline node dispatch functions
96 -------------------------------------
98 It's typical for a graph dispatch function to contain one or more
99 calls to an inline function. See above. If your node dispatch function
100 is structured that way, make *ABSOLUTELY CERTAIN* to use the
101 "always_inline" macro:
106 ip46_sdp_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
107 vlib_frame_t * frame,
108 int is_ip4, int is_trace)
111 Otherwise, the compiler is highly likely NOT to build multiple
112 versions of the guts of your dispatch function.
114 It's fairly easy to spot this mistake in "perf top." If you see, for
115 example, a bunch of functions with names of the form
116 "xxx_node_fn_avx2" in the profile, *BUT* your brand-new node function
117 shows up with a name of the form "xxx_inline.isra.1", it's quite likely
118 that the inline was declared "static inline" instead of "always_inline".
120 Modify CMakeLists.txt
121 ---------------------
123 If the component in question already lists "MULTIARCH_SOURCES", simply
124 add the indicated .c file to the list. Otherwise, add as shown
125 below. Note that the added file "new_multiarch_node.c" should appear in
126 *both* SOURCES and MULTIARCH_SOURCES:
130 add_vpp_plugin(myplugin
136 new_ multiarch_node.c