5 Integrating a complete plugin with VPP
6 ======================================
11 This section shows how a VPP plugin developer can modify VPP scripts to add and load their plugin as a node in VPP.
13 As an example we will integrate the **Sample Plugin** found in *vpp/src/examples/sample-plugin/sample* The VPP Sample Plugin is a small plugin that demonstrates simple implementation of a macswap algorithim. Since it is a VPP plugin, it has runtime integration with the VPP graph hierachy, API, and CLI.
15 This section will not go into the details of the plugin itself. For a deeper dive into the sample plugin see the annotations in `sample.c <https://docs.fd.io/vpp/18.11/da/d30/sample_8c.html>`_, or go to the next page for general VPP C API usage.
20 Each plugin has their own automake file (\*.am) used by *configure.ac*, as well as a separate directory containing C files for the plugin. The directory containing these for each plugin is *vpp/src/plugins*
22 To get a basic idea for how a VPP automake plugin file specifies its C files, here is part of the Sample Plugin automake file, *sample.am*
24 .. code-block:: console
26 sample_plugin_la_SOURCES = \
29 sample/sample_plugin.api.h
31 API_FILES += sample/sample.api
33 nobase_apiinclude_HEADERS += \
34 sample/sample_all_api_h.h \
35 sample/sample_msg_enum.h \
39 The Sample Plugin is located in *vpp/src/examples/sample-plugin/sample*, so as mentioned above we will need to copy its contents into *vpp/src/plugins*
41 In your */vpp* directory, or the directory above */src*, run:
43 .. code-block:: console
45 $ cp -r src/examples/sample-plugin/sample src/plugins
46 $ cp src/examples/sample-plugin/sample.am src/plugins
48 Modifying configure.ac and Makefile.am
49 ______________________________________
51 We now need to modify the plugin sections of the VPP automake and configuration scripts so that VPP builds correctly with your new plugin.
53 Using a text editor such as *vi*, add the following entry to the plugins section in *vpp/src/configure.ac*
55 .. code-block:: console
57 PLUGIN_ENABLED(sample)
59 For reference, the plugins section of that file looks like this:
61 .. code-block:: console
63 ###############################################################################
65 ###############################################################################
67 # Please keep alphabetical order
73 PLUGIN_ENABLED(flowprobe)
76 Using a text editor such as *vi*, now add the following entry to the plugins section in *vpp/src/plugins/Makefile.am*
78 .. code-block:: console
80 if ENABLE_SAMPLE_PLUGIN
84 For reference, the plugins section of that file looks something like this:
86 .. code-block:: console
88 vppapitestpluginsdir = ${libdir}/vpp_api_test_plugins
89 vpppluginsdir = ${libdir}/vpp_plugins
107 Build VPP by using the main Makefile found in */vpp/Makefile*
109 .. code-block:: console
115 If you want to have a fresh debug build and compile every VPP file from scratch, you can wipe all compiled files and build VPP with:
117 .. code-block:: console
121 However this will take much longer than just running *make build*
123 Run VPP and make sure the plugin is loaded. Below is the command for running the VPP debug binary, accompanied with sample output.
125 .. code-block:: console
128 vlib_plugin_early_init:361: plugin path /vpp/build-root/install-vpp_debug-native/vpp/lib/vpp_plugins:/vpp/build-root/install-vpp_debug-native/vpp/lib64/vpp_plugins
129 load_one_plugin:189: Loaded plugin: abf_plugin.so (ACL based Forwarding)
130 load_one_plugin:189: Loaded plugin: acl_plugin.so (Access Control Lists)
131 load_one_plugin:189: Loaded plugin: avf_plugin.so (Intel Adaptive Virtual Function (AVF) Device Plugin)
132 load_one_plugin:191: Loaded plugin: cdp_plugin.so
134 load_one_plugin:189: Loaded plugin: sample_plugin.so (Sample of VPP Plugin)
136 load_one_vat_plugin:67: Loaded plugin: avf_test_plugin.so
137 load_one_vat_plugin:67: Loaded plugin: mactime_test_plugin.so
138 load_one_vat_plugin:67: Loaded plugin: sample_test_plugin.so
140 _______ _ _ _____ ___
141 __/ __/ _ \ (_)__ | | / / _ \/ _ \
142 _/ _// // / / / _ \ | |/ / ___/ ___/
143 /_/ /____(_)_/\___/ |___/_/ /_/
149 Notice when running the debug build that (\*_test_plugin.so) is also loaded, which is meant for testing your plugin.
151 To enable the sample plugin, use this command:
153 .. code-block:: console
155 DBGvpp# sample macswap <interface name>
157 To disable the sample plugin, use this command:
159 .. code-block:: console
161 DBGvpp# sample macswap <interface name> disable
164 Great! Now you've successfully added your plugin as a VPP node.
170 How the build process works for plugins is that the (\*.api) plugin file is automatically translated to a JSON file (\*.api.json) in *vpp/build-root/install-vpp_debug-native/vpp/share/vpp/api/plugins*, which the code generator then parses and generates a C header file (\*.api.h) in *vpp/build-root/install-vpp_debug-native/vpp/include/vpp_plugins/\**.
172 After the build process is completed you finally end up with two plugin files (\*_plugin.so and \*_test_plugin.so) found in *vpp/build-root/install-vpp_debug-native/vpp/lib64/vpp_plugins* and *vpp/build-root/install-vpp_debug-native/vpp/lib64/vpp_api_test_plugins* respectively, that are loaded at runtime during a debug binary run of VPP (*make run*).