diff mbox series

[ovs-dev,v2,4/6] lib: Add infrastructure for plugging providers.

Message ID 20210811120929.1030798-5-frode.nordahl@canonical.com
State Superseded
Headers show
Series Introduce infrastructure for plugging providers | expand

Checks

Context Check Description
ovsrobot/apply-robot success apply and check: success
ovsrobot/github-robot-_Build_and_Test fail github build: failed
ovsrobot/github-robot-_ovn-kubernetes fail github build: failed

Commit Message

Frode Nordahl Aug. 11, 2021, 12:09 p.m. UTC
New module contains the infrastructure for registering and
instantiating plugging classes which may be hosted inside or
outside the core OVN repository.  The data structures and functions
for interacting with these plugging classes also live there.

Extend build system to allow linking a externally built plugging
provider.

Signed-off-by: Frode Nordahl <frode.nordahl@canonical.com>
---
 Makefile.am            |   2 +
 acinclude.m4           |  25 +++
 configure.ac           |   2 +
 controller/automake.mk |   3 +
 lib/automake.mk        |   7 +-
 lib/plug-dummy.c       | 144 +++++++++++++++++
 lib/plug-dummy.h       |  33 ++++
 lib/plug-provider.h    | 109 +++++++++++++
 lib/plug.c             | 339 +++++++++++++++++++++++++++++++++++++++++
 lib/plug.h             | 101 ++++++++++++
 lib/test-plug.c        |  80 ++++++++++
 tests/automake.mk      |   4 +-
 tests/ovn-plug.at      |   8 +
 13 files changed, 855 insertions(+), 2 deletions(-)
 create mode 100644 lib/plug-dummy.c
 create mode 100644 lib/plug-dummy.h
 create mode 100644 lib/plug-provider.h
 create mode 100644 lib/plug.c
 create mode 100644 lib/plug.h
 create mode 100644 lib/test-plug.c
 create mode 100644 tests/ovn-plug.at

Comments

Frode Nordahl Aug. 18, 2021, 2:32 p.m. UTC | #1
I discovered a few issues with this patch related to linking and
interacting with external code while bootstrapping the ovn-vif
repository.

Will send a v3 shortly.

On Wed, Aug 11, 2021 at 2:10 PM Frode Nordahl
<frode.nordahl@canonical.com> wrote:

[ snip ]

> diff --git a/Makefile.am b/Makefile.am
> index 0169c96ef..b1d9aba35 100644
> --- a/Makefile.am
> +++ b/Makefile.am
> @@ -15,6 +15,8 @@ OVS_LIBDIR=@ovs_libdir@
>  OVSDB_LIBDIR=@ovsdb_libdir@
>  OVS_MANDIR=@ovs_mandir@
>
> +PLUG_PROVIDER_OBJECT=@plug_provider_OBJECT@
> +
>  AM_CPPFLAGS = $(SSL_CFLAGS)
>  AM_LDFLAGS = $(SSL_LDFLAGS)
>  AM_LDFLAGS += $(OVS_LDFLAGS)

Need to also add a -DHAVE_PLUG_PROVIDER to activate the code protected
by preprocessor ifdef's.

> diff --git a/acinclude.m4 b/acinclude.m4
> index e7f829520..0fb8bc97f 100644
> --- a/acinclude.m4
> +++ b/acinclude.m4
> @@ -441,3 +441,28 @@ AC_DEFUN([OVN_CHECK_OVS], [
>    AC_MSG_CHECKING([OVS version])
>    AC_MSG_RESULT([$OVSVERSION])
>  ])
> +
> +dnl OVN_CHECK_PLUG_PROVIDER
> +dnl
> +dnl Check for external plug provider
> +AC_DEFUN([OVN_CHECK_PLUG_PROVIDER], [
> +  AC_ARG_VAR([PLUG_PROVIDER])
> +  AC_ARG_WITH(
> +    [plug-provider],
> +    [AC_HELP_STRING([--with-plug-provider=/path/to/provider.o],
> +                    [Specify path to a built plug provider object])],
> +    [if test "$withval" = yes; then
> +       if test -z "$PLUG_PROVIDER"; then
> +         AC_MSG_ERROR([To build with plug provider, specify the path to a built plug provider library object on --with-plug-provider or in \$PLUG_PROVIDER]),
> +       fi
> +     elif test ! -f "$withval"; then
> +       AC_MSG_ERROR([$withval is not a built plug provider library object])
> +     else
> +       PLUG_PROVIDER="$(realpath $withval)"
> +     fi],
> +    [PLUG_PROVIDER=no])
> +  AC_MSG_CHECKING([for plug provider])
> +  AC_MSG_RESULT([$PLUG_PROVIDER])
> +  AC_SUBST([PLUG_PROVIDER])
> +  AM_CONDITIONAL([HAVE_PLUG_PROVIDER], [test "$PLUG_PROVIDER" != no])
> +])

When linking we need to do `-Lpath/to/repo/lib/.libs
-lname-of-library`, so we need to make ^ a bit more elaborate, and
perhaps make it specific to the ovn-vif project.

[ snip ]
> diff --git a/controller/automake.mk b/controller/automake.mk
> index ad2d68af2..925f3f861 100644
> --- a/controller/automake.mk
> +++ b/controller/automake.mk
> @@ -40,6 +40,9 @@ controller_ovn_controller_SOURCES = \
>         controller/ovsport.c
>
>  controller_ovn_controller_LDADD = lib/libovn.la $(OVS_LIBDIR)/libopenvswitch.la
> +if HAVE_PLUG_PROVIDER
> +controller_ovn_controller_LDADD += $(PLUG_PROVIDER)
> +endif
>  man_MANS += controller/ovn-controller.8
>  EXTRA_DIST += controller/ovn-controller.8.xml
>  CLEANFILES += controller/ovn-controller.8

Linking only for the controller does not make sense as the plugging
interface lives in lib/, move to lib/automake.mk

[ snip ]

> diff --git a/lib/plug.c b/lib/plug.c
> new file mode 100644
> index 000000000..09c290ac7
> --- /dev/null
> +++ b/lib/plug.c

[ snip ]

> +/* Initialize the the plug infrastructure by registering known plug classes */
> +static void
> +plug_initialize(void)
> +{
> +    static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
> +
> +    if (ovsthread_once_start(&once)) {
> +#ifdef HAVE_PLUG_PROVIDER
> +        for (int i = 0; i < ARRAY_SIZE(plug_provider_classes); i++) {
> +            plug_register_provider(plug_provider_classes[i]);
> +        }
> +#endif
> +        ovsthread_once_done(&once);
> +    }
> +}

The ARRAY_SIZE macro will not work here as the array is defined in
code external to the project. Need to update this to use a pointer and
NULL-terminated array instead.
diff mbox series

Patch

diff --git a/Makefile.am b/Makefile.am
index 0169c96ef..b1d9aba35 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -15,6 +15,8 @@  OVS_LIBDIR=@ovs_libdir@
 OVSDB_LIBDIR=@ovsdb_libdir@
 OVS_MANDIR=@ovs_mandir@
 
+PLUG_PROVIDER_OBJECT=@plug_provider_OBJECT@
+
 AM_CPPFLAGS = $(SSL_CFLAGS)
 AM_LDFLAGS = $(SSL_LDFLAGS)
 AM_LDFLAGS += $(OVS_LDFLAGS)
diff --git a/acinclude.m4 b/acinclude.m4
index e7f829520..0fb8bc97f 100644
--- a/acinclude.m4
+++ b/acinclude.m4
@@ -441,3 +441,28 @@  AC_DEFUN([OVN_CHECK_OVS], [
   AC_MSG_CHECKING([OVS version])
   AC_MSG_RESULT([$OVSVERSION])
 ])
+
+dnl OVN_CHECK_PLUG_PROVIDER
+dnl
+dnl Check for external plug provider
+AC_DEFUN([OVN_CHECK_PLUG_PROVIDER], [
+  AC_ARG_VAR([PLUG_PROVIDER])
+  AC_ARG_WITH(
+    [plug-provider],
+    [AC_HELP_STRING([--with-plug-provider=/path/to/provider.o],
+                    [Specify path to a built plug provider object])],
+    [if test "$withval" = yes; then
+       if test -z "$PLUG_PROVIDER"; then
+         AC_MSG_ERROR([To build with plug provider, specify the path to a built plug provider library object on --with-plug-provider or in \$PLUG_PROVIDER]),
+       fi
+     elif test ! -f "$withval"; then
+       AC_MSG_ERROR([$withval is not a built plug provider library object])
+     else
+       PLUG_PROVIDER="$(realpath $withval)"
+     fi],
+    [PLUG_PROVIDER=no])
+  AC_MSG_CHECKING([for plug provider])
+  AC_MSG_RESULT([$PLUG_PROVIDER])
+  AC_SUBST([PLUG_PROVIDER])
+  AM_CONDITIONAL([HAVE_PLUG_PROVIDER], [test "$PLUG_PROVIDER" != no])
+])
diff --git a/configure.ac b/configure.ac
index df0b98295..2d35111db 100644
--- a/configure.ac
+++ b/configure.ac
@@ -172,6 +172,7 @@  OVS_ENABLE_SPARSE
 OVS_CHECK_DDLOG([0.38])
 OVS_CHECK_PRAGMA_MESSAGE
 OVN_CHECK_OVS
+OVN_CHECK_PLUG_PROVIDER
 OVS_CTAGS_IDENTIFIERS
 AC_SUBST([OVS_CFLAGS])
 AC_SUBST([OVS_LDFLAGS])
@@ -184,6 +185,7 @@  AC_SUBST([ovs_builddir], ['${OVSBUILDDIR}'])
 AC_SUBST([ovs_libdir], ['${OVSBUILDDIR}/lib'])
 AC_SUBST([ovsdb_libdir], ['${OVSBUILDDIR}/ovsdb'])
 AC_SUBST([ovs_mandir], ['${OVSDIR}'])
+AC_SUBST([plug_provider_object], ['${PLUG_PROVIDER}'])
 
 AC_CONFIG_FILES(Makefile)
 AC_CONFIG_FILES(tests/atlocal)
diff --git a/controller/automake.mk b/controller/automake.mk
index ad2d68af2..925f3f861 100644
--- a/controller/automake.mk
+++ b/controller/automake.mk
@@ -40,6 +40,9 @@  controller_ovn_controller_SOURCES = \
 	controller/ovsport.c
 
 controller_ovn_controller_LDADD = lib/libovn.la $(OVS_LIBDIR)/libopenvswitch.la
+if HAVE_PLUG_PROVIDER
+controller_ovn_controller_LDADD += $(PLUG_PROVIDER)
+endif
 man_MANS += controller/ovn-controller.8
 EXTRA_DIST += controller/ovn-controller.8.xml
 CLEANFILES += controller/ovn-controller.8
diff --git a/lib/automake.mk b/lib/automake.mk
index ddfe33948..a28ada092 100644
--- a/lib/automake.mk
+++ b/lib/automake.mk
@@ -32,7 +32,12 @@  lib_libovn_la_SOURCES = \
 	lib/inc-proc-eng.h \
 	lib/lb.c \
 	lib/lb.h \
-	lib/stopwatch-names.h
+	lib/stopwatch-names.h \
+	lib/plug-provider.h \
+	lib/plug.h \
+	lib/plug.c \
+	lib/plug-dummy.h \
+	lib/plug-dummy.c
 nodist_lib_libovn_la_SOURCES = \
 	lib/ovn-dirs.c \
 	lib/ovn-nb-idl.c \
diff --git a/lib/plug-dummy.c b/lib/plug-dummy.c
new file mode 100644
index 000000000..efa413b19
--- /dev/null
+++ b/lib/plug-dummy.c
@@ -0,0 +1,144 @@ 
+/*
+ * Copyright (c) 2021 Canonical
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <config.h>
+#include "plug-dummy.h"
+#include "plug-provider.h"
+#include "plug.h"
+
+#include <stdint.h>
+
+#include "openvswitch/vlog.h"
+#include "smap.h"
+#include "sset.h"
+
+#ifndef IFNAMSIZ
+#define IFNAMSIZ 16
+#endif
+
+VLOG_DEFINE_THIS_MODULE(plug_dummy);
+
+struct plug_dummy {
+    struct plug plug;
+};
+
+static struct sset plug_dummy_maintained_iface_options;
+
+static int
+plug_dummy_init(void)
+{
+    sset_init(&plug_dummy_maintained_iface_options);
+    sset_add(&plug_dummy_maintained_iface_options, "plug-dummy-option");
+
+    return 0;
+}
+
+static int
+plug_dummy_destroy(void)
+{
+    sset_destroy(&plug_dummy_maintained_iface_options);
+
+    return 0;
+}
+
+static int
+plug_dummy_open(const struct plug_class *class, struct plug **plugp)
+{
+    struct plug_dummy *plug;
+
+    plug = xmalloc(sizeof *plug);
+    plug->plug.plug_class = class;
+    *plugp = &plug->plug;
+
+    VLOG_DBG("plug_dummy_open(%p)", plug);
+    return 0;
+}
+
+static int
+plug_dummy_close(struct plug *plug)
+{
+    VLOG_DBG("plug_dummy_close(%p)", plug);
+    free(plug);
+
+    return 0;
+}
+
+static bool
+plug_dummy_run(struct plug *plug)
+{
+    VLOG_DBG("plug_dummy_run(%p)", plug);
+
+    return true;
+}
+
+static bool
+plug_dummy_port_prepare(const struct plug_port_ctx_in *ctx_in,
+                       struct plug_port_ctx_out *ctx_out)
+{
+    VLOG_DBG("plug_dummy_port_prepare: %s", ctx_in->lport_name);
+
+    if (ctx_in->op_type == PLUG_OP_CREATE) {
+        size_t lport_name_len = strlen(ctx_in->lport_name);
+        ctx_out->name = xzalloc(IFNAMSIZ);
+        memcpy(ctx_out->name, ctx_in->lport_name,
+               (lport_name_len < IFNAMSIZ) ? lport_name_len : IFNAMSIZ - 1);
+        ctx_out->type = xstrdup("internal");
+        ctx_out->iface_options = xmalloc(sizeof *ctx_out->iface_options);
+        smap_init(ctx_out->iface_options);
+        smap_add(ctx_out->iface_options, "plug-dummy-option", "value");
+    }
+
+    return true;
+}
+
+static void
+plug_dummy_port_finish(const struct plug_port_ctx_in *ctx_in,
+                      struct plug_port_ctx_out *ctx_out OVS_UNUSED)
+{
+    VLOG_DBG("plug_dummy_port_finish: %s", ctx_in->lport_name);
+}
+
+static void
+plug_dummy_port_ctx_destroy(const struct plug_port_ctx_in *ctx_in,
+                           struct plug_port_ctx_out *ctx_out)
+{
+    VLOG_DBG("plug_dummy_port_ctx_destroy: %s", ctx_in->lport_name);
+    ovs_assert(ctx_in->op_type == PLUG_OP_CREATE);
+    free(ctx_out->name);
+    free(ctx_out->type);
+    smap_destroy(ctx_out->iface_options);
+    free(ctx_out->iface_options);
+}
+
+const struct plug_class plug_dummy_class = {
+    .type = "dummy",
+    .maintained_iface_options = &plug_dummy_maintained_iface_options,
+    .init = plug_dummy_init,
+    .destroy = plug_dummy_destroy,
+    .open = plug_dummy_open,
+    .close = plug_dummy_close,
+    .run = plug_dummy_run,
+    .plug_port_prepare = plug_dummy_port_prepare,
+    .plug_port_finish = plug_dummy_port_finish,
+    .plug_port_ctx_destroy = plug_dummy_port_ctx_destroy,
+};
+
+void
+plug_dummy_enable(void)
+{
+    plug_register_provider(&plug_dummy_class);
+}
+
diff --git a/lib/plug-dummy.h b/lib/plug-dummy.h
new file mode 100644
index 000000000..6ea33671e
--- /dev/null
+++ b/lib/plug-dummy.h
@@ -0,0 +1,33 @@ 
+/*
+ * Copyright (c) 2021 Canonical
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef PLUG_DUMMY_H
+#define PLUG_DUMMY_H 1
+
+/*
+ * The dummy plugger, allows for experimenting with plugging in a sandbox */
+
+#ifdef  __cplusplus
+extern "C" {
+#endif
+
+void plug_dummy_enable(void);
+
+#ifdef  __cplusplus
+}
+#endif
+
+#endif /* plug-dummy.h */
diff --git a/lib/plug-provider.h b/lib/plug-provider.h
new file mode 100644
index 000000000..5ffbe088f
--- /dev/null
+++ b/lib/plug-provider.h
@@ -0,0 +1,109 @@ 
+/*
+ * Copyright (c) 2021 Canonical
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef PLUG_PROVIDER_H
+#define PLUG_PROVIDER_H 1
+
+/* Provider interface to pluggers.  A plugger implementation performs lookup
+ * and/or initialization of ports, typically representor ports, using generic
+ * non-blocking hardware interfaces.  This allows the ovn-controller to, upon
+ * the CMS's request, create ports and interfaces in the chassis's Open vSwitch
+ * instances (also known as vif plugging).
+ */
+
+#include <stdbool.h>
+
+#include "plug.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+struct plug {
+    const struct plug_class *plug_class;
+};
+
+struct plug_class {
+    /* Type of plugger in this class. */
+    const char *type;
+
+    /* Interface options this plugger will maintain.  This set is used
+     * to know which items to remove when maintaining the database record. */
+    struct sset *maintained_iface_options;
+
+    /* Called when the plug provider is registered, typically at program
+     * startup.
+     *
+     * This function may be set to null if a plug class needs no
+     * initialization at registration time. */
+    int (*init)(void);
+
+    /* Called when the plug provider is unregistered, typically at program
+     * exit.
+     *
+     * This function may be set to null if a plug class needs no
+     * de-initialization at unregister time.*/
+    int (*destroy)(void);
+
+    /* Creates a new plug class instance.
+     *
+     * If successful, stores a pointer to the plug instance in '*plugp' */
+    int (*open)(const struct plug_class *class, struct plug **plugp);
+
+    /* Closes plug class instance and frees associated memory. */
+    int (*close)(struct plug *plug);
+
+    /* Performs periodic work needed by plugger, if any is necessary.  Returns
+     * true if something changed, false otherwise.
+     *
+     * Note that work performed by plugger in this function must under no
+     * circumstances block. */
+    bool (*run)(struct plug *plug);
+
+    /* Pass plug_port_ctx_in to plug implementation to prepare for port
+     * creation/update.
+     *
+     * The plug implemantation can perform lookup or any per port
+     * initialization and should fill plug_port_ctx_out with data required for
+     * port/interface creation.  The plug implementation should return true if
+     * it wants the caller to create/update a port/interface, false otherwise.
+     *
+     * Data in the plug_port_ctx_out struct is owned by the plugging library,
+     * and a call must be made to the plug_port_ctx_destroy callback to free
+     * up any allocations when done with port creation/update.
+     */
+    bool (*plug_port_prepare)(const struct plug_port_ctx_in *,
+                              struct plug_port_ctx_out *);
+
+    /* Notify plugging library that port update is done. */
+    void (*plug_port_finish)(const struct plug_port_ctx_in *,
+                             struct plug_port_ctx_out *);
+
+    /* Free any allocations made by the plug_port_prepare callback. */
+    void (*plug_port_ctx_destroy)(const struct plug_port_ctx_in *,
+                                  struct plug_port_ctx_out *);
+};
+
+extern const struct plug_class plug_dummy_class;
+#ifdef HAVE_PLUG_PROVIDER
+extern const struct plug_class *plug_provider_classes[];
+#endif
+
+#ifdef  __cplusplus
+}
+#endif
+
+#endif /* plug-provider.h */
diff --git a/lib/plug.c b/lib/plug.c
new file mode 100644
index 000000000..09c290ac7
--- /dev/null
+++ b/lib/plug.c
@@ -0,0 +1,339 @@ 
+/*
+ * Copyright (c) 2021 Canonical
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <config.h>
+#include "plug-provider.h"
+#include "plug.h"
+
+#include <errno.h>
+#include <stdint.h>
+#include <string.h>
+
+#include "openvswitch/vlog.h"
+#include "openvswitch/shash.h"
+#include "smap.h"
+#include "sset.h"
+
+VLOG_DEFINE_THIS_MODULE(plug);
+
+struct registered_plug_class {
+    const struct plug_class *plug_class;
+    int refcount;
+};
+static struct shash plug_classes = SHASH_INITIALIZER(&plug_classes);
+static struct shash plug_instances = SHASH_INITIALIZER(&plug_instances);
+
+/* Protects 'plug_classes', including the refcount. */
+static struct ovs_mutex plug_classes_mutex = OVS_MUTEX_INITIALIZER;
+/* Protects 'plug_instances' */
+static struct ovs_mutex plug_instances_mutex = OVS_MUTEX_INITIALIZER;
+
+/* Initialize the the plug infrastructure by registering known plug classes */
+static void
+plug_initialize(void)
+{
+    static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
+
+    if (ovsthread_once_start(&once)) {
+#ifdef HAVE_PLUG_PROVIDER
+        for (int i = 0; i < ARRAY_SIZE(plug_provider_classes); i++) {
+            plug_register_provider(plug_provider_classes[i]);
+        }
+#endif
+        ovsthread_once_done(&once);
+    }
+}
+
+static int
+plug_register_provider__(const struct plug_class *new_class)
+{
+    struct registered_plug_class *rc;
+    int error;
+
+    if (shash_find(&plug_classes, new_class->type)) {
+        VLOG_WARN("attempted to register duplicate plug provider: %s",
+                  new_class->type);
+        return EEXIST;
+    }
+
+    error = new_class->init ? new_class->init() : 0;
+    if (error) {
+        VLOG_WARN("failed to initialize %s plug class: %s",
+                  new_class->type, ovs_strerror(error));
+        return error;
+    }
+
+    rc = xmalloc(sizeof *rc);
+    rc->plug_class = new_class;
+    rc->refcount = 0;
+
+    shash_add(&plug_classes, new_class->type, rc);
+
+    return 0;
+}
+
+/* Register the new plug provider referred to in 'new_class' and perform any
+ * class level initialization as specified in its plug_class. */
+int
+plug_register_provider(const struct plug_class *new_class)
+{
+    int error;
+
+    ovs_mutex_lock(&plug_classes_mutex);
+    error = plug_register_provider__(new_class);
+    ovs_mutex_unlock(&plug_classes_mutex);
+
+    return error;
+}
+
+static int
+plug_unregister_provider__(const char *type)
+{
+    int error;
+    struct shash_node *node;
+    struct registered_plug_class *rc;
+
+    node = shash_find(&plug_classes, type);
+    if (!node) {
+        return EINVAL;
+    }
+
+    rc = node->data;
+    if (rc->refcount) {
+        VLOG_WARN("attempted to unregister in use plug provider: %s", type);
+        return EBUSY;
+    }
+
+    error = rc->plug_class->destroy ? rc->plug_class->destroy() : 0;
+    if (error) {
+        VLOG_WARN("failed to destroy %s plug class: %s",
+                  rc->plug_class->type, ovs_strerror(error));
+        return error;
+    }
+
+    shash_delete(&plug_classes, node);
+    free(rc);
+
+    return 0;
+}
+
+/* Unregister the plug provider identified by 'type' and perform any class
+ * level de-initialization as specified in its plug_class. */
+int
+plug_unregister_provider(const char *type)
+{
+    int error;
+
+    plug_initialize();
+
+    ovs_mutex_lock(&plug_classes_mutex);
+    error = plug_unregister_provider__(type);
+    ovs_mutex_unlock(&plug_classes_mutex);
+
+    return error;
+}
+
+static void
+plug_class_unref(struct registered_plug_class *rc)
+{
+    ovs_mutex_lock(&plug_classes_mutex);
+    ovs_assert(rc->refcount);
+    rc->refcount--;
+    ovs_mutex_unlock(&plug_classes_mutex);
+}
+
+static struct registered_plug_class *
+plug_class_lookup(const char *type)
+{
+    struct registered_plug_class *rc;
+
+    ovs_mutex_lock(&plug_classes_mutex);
+    rc = shash_find_data(&plug_classes, type);
+    if (rc) {
+        rc->refcount++;
+    }
+    ovs_mutex_unlock(&plug_classes_mutex);
+
+    return rc;
+}
+
+static int
+plug_open__(const char *type, struct plug **plugp)
+{
+    struct plug *plug = NULL;
+    int error;
+    struct registered_plug_class *rc;
+
+    plug_initialize();
+    rc = plug_class_lookup(type);
+    if (!rc) {
+        VLOG_WARN("unable to open plug provider of unknown type: %s", type);
+        error = EINVAL;
+        goto out;
+    }
+
+    error = rc->plug_class->open(rc->plug_class, &plug);
+    if (error) {
+        plug_class_unref(rc);
+    }
+
+out:
+    *plugp = error ? NULL: plug;
+    return error;
+}
+
+/* Create, or retrieve the already created instance of plug class from a
+ * previous call to plug_open, identified by 'type' and store a reference to it
+ * in '*plugp'.
+ *
+ * The plug implementation will perform any initialization and allocations it
+ * needs, and the plug infrastructure will store a reference to it.  Subsequent
+ * calls to this function with the same 'type' parameter will return the same
+ * object, until the instance is removed with a call to plug_close. */
+int
+plug_open(const char *type, struct plug **plugp)
+{
+    struct plug *instance = shash_find_data(&plug_instances, type);
+    int error;
+
+    if (instance) {
+        *plugp = instance;
+        return 0;
+    }
+
+    error = plug_open__(type, plugp);
+    if (error) {
+        return error;
+    }
+
+    ovs_mutex_lock(&plug_instances_mutex);
+    shash_add(&plug_instances, type, *plugp);
+    ovs_mutex_unlock(&plug_instances_mutex);
+
+    return 0;
+}
+
+/* Close the plug class instance previously created by a call to 'plug_open'.
+ *
+ * The plug implementation will perform any destruction of its data and the
+ * plug infrastructure will remove its references to it. */
+void
+plug_close(struct plug *plug)
+{
+    if (plug) {
+        ovs_mutex_lock(&plug_instances_mutex);
+        shash_find_and_delete(&plug_instances, plug->plug_class->type);
+        ovs_mutex_unlock(&plug_instances_mutex);
+
+        struct registered_plug_class *rc;
+        rc = shash_find_data(&plug_classes, plug->plug_class->type);
+        rc->plug_class->close(plug);
+        plug_class_unref(rc);
+    }
+}
+
+/* Close any previously instantiated plug classes and unregister the plug
+ * providers. */
+void
+plug_destroy_all(void)
+{
+    struct shash_node *node, *next;
+
+    SHASH_FOR_EACH_SAFE (node, next, &plug_instances) {
+        struct plug *plug = node->data;
+        plug_close(plug);
+    }
+
+    SHASH_FOR_EACH_SAFE (node, next, &plug_classes) {
+        struct registered_plug_class *rc = node->data;
+        plug_unregister_provider(rc->plug_class->type);
+    }
+}
+
+/* Iterate over previously instantiated plug classes and call their 'run'
+ * function if defined.
+ *
+ * If any of the instances report they have changed something this function
+ * will return 'true', otherwise it will return 'false'. */
+bool
+plug_run_instances(void)
+{
+    struct shash_node *node;
+    bool something_changed = false;
+
+    ovs_mutex_lock(&plug_instances_mutex);
+
+    SHASH_FOR_EACH (node, &plug_instances) {
+        struct plug *instance = node->data;
+        if (instance->plug_class->run(instance)) {
+            something_changed = true;
+        }
+    }
+
+    ovs_mutex_unlock(&plug_instances_mutex);
+
+    return something_changed;
+}
+
+/* Get the class level 'maintained_iface_options' set. */
+struct sset *
+plug_class_get_maintained_iface_options(const struct plug *plug)
+{
+    return plug->plug_class->maintained_iface_options;
+}
+
+/* Prepare the logical port as identified by 'ctx_in' for port creation, update
+ * or removal as specified by 'ctx_in->op_type'.
+ *
+ * When 'ctx_in->op_type' is PLUG_OP_CREATE the plug implementation must fill
+ * 'ctx_out' with data to apply to the interface record maintained by OVN on
+ * its behalf.
+ *
+ * When 'ctx_in_op_type' is PLUG_OP_REMOVE 'ctx_out' should be set to NULL and
+ * the plug implementation must not attempt to use 'ctx_out'.
+ *
+ * The data in 'ctx_out' is owned by the plug implementation, and a call must
+ * be made to plug_port_ctx_destroy when done with it. */
+bool
+plug_port_prepare(const struct plug *plug,
+                  const struct plug_port_ctx_in *ctx_in,
+                  struct plug_port_ctx_out *ctx_out)
+{
+    if (ctx_out) {
+        memset(ctx_out, 0, sizeof(*ctx_out));
+    }
+    return plug->plug_class->plug_port_prepare(ctx_in, ctx_out);
+}
+
+/* Notify the plug implementation that a port creation, update or removal has
+ * been completed */
+void
+plug_port_finish(const struct plug *plug,
+                 const struct plug_port_ctx_in *ctx_in,
+                 struct plug_port_ctx_out *ctx_out)
+{
+    plug->plug_class->plug_port_finish(ctx_in, ctx_out);
+}
+
+/* Free any data allocated to 'ctx_out' in a prevous call to
+ * plug_port_prepare. */
+void
+plug_port_ctx_destroy(const struct plug *plug,
+                      const struct plug_port_ctx_in *ctx_in,
+                      struct plug_port_ctx_out *ctx_out)
+{
+    plug->plug_class->plug_port_ctx_destroy(ctx_in, ctx_out);
+}
diff --git a/lib/plug.h b/lib/plug.h
new file mode 100644
index 000000000..6117bd52a
--- /dev/null
+++ b/lib/plug.h
@@ -0,0 +1,101 @@ 
+/*
+ * Copyright (c) 2021 Canonical
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef PLUG_H
+#define PLUG_H 1
+
+/*
+ * Plug, the plugging interface.  This module contains the infrastructure for
+ * registering and instantiating plugging classes which may be hosted inside
+ * or outside the core OVN repository.  The data structures and functions for
+ * interacting with these plugging classes also live here.
+ */
+
+#include "smap.h"
+
+#ifdef  __cplusplus
+extern "C" {
+#endif
+
+struct plug;
+struct plug_class;
+struct ovsdb_idl_txn;
+struct ovsrec_bridge;
+
+enum plug_op_type {
+    PLUG_OP_CREATE = 1, /* Port is created or updated */
+    PLUG_OP_REMOVE,     /* Port is removed from this chassis */
+};
+
+struct plug_port_ctx_in {
+    /* Operation being performed */
+    enum plug_op_type op_type;
+
+    /* Whether the chassis uses DPDK */
+    bool use_dpdk;
+
+    /* Name of logical port, can be useful for plugging library to track any
+     * per port resource initialization. */
+    const char *lport_name;
+
+    /* Logical port options, while OVN will forward the contents verbatim from
+     * the Southbound database, the convention is for the plugging library to
+     * only make decisions based on the plug-* options. */
+    const struct smap *lport_options;
+
+    /* When OVN knows about an existing interface record associated with this
+     * lport, these will be filled in with information about it. */
+    const char *iface_name;
+    const char *iface_type;
+    const struct smap *iface_options;
+};
+
+struct plug_port_ctx_out {
+    /* The name to use for port and interface record. */
+    char *name;
+
+    /* Type of interface to create. */
+    char *type;
+
+    /* Options to set on the interface record. */
+    struct smap *iface_options;
+};
+
+
+int plug_register_provider(const struct plug_class *);
+int plug_unregister_provider(const char *type);
+void plug_destroy_all(void);
+int plug_open(const char *type, struct plug **);
+void plug_close(struct plug *);
+bool plug_run_instances(void);
+
+struct sset * plug_class_get_maintained_iface_options(const struct plug *);
+
+bool plug_port_prepare(const struct plug *,
+                       const struct plug_port_ctx_in *,
+                       struct plug_port_ctx_out *);
+void plug_port_finish(const struct plug *,
+                      const struct plug_port_ctx_in *,
+                      struct plug_port_ctx_out *);
+void plug_port_ctx_destroy(const struct plug *,
+                           const struct plug_port_ctx_in *,
+                           struct plug_port_ctx_out *);
+
+#ifdef  __cplusplus
+}
+#endif
+
+#endif /* plug.h */
diff --git a/lib/test-plug.c b/lib/test-plug.c
new file mode 100644
index 000000000..b17b08a70
--- /dev/null
+++ b/lib/test-plug.c
@@ -0,0 +1,80 @@ 
+/* Copyright (c) 2021, Canonical
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <config.h>
+#include <errno.h>
+
+#include "plug.h"
+#include "plug-dummy.h"
+#include "plug-provider.h"
+#include "smap.h"
+#include "sset.h"
+#include "tests/ovstest.h"
+
+static void
+test_plug(struct ovs_cmdl_context *ctx OVS_UNUSED)
+{
+    struct plug *plug;
+
+    ovs_assert(plug_unregister_provider("dummy") == EINVAL);
+    ovs_assert(plug_open("dummy", &plug) == EINVAL);
+
+    ovs_assert(!plug_register_provider(&plug_dummy_class));
+    ovs_assert(plug_register_provider(&plug_dummy_class) == EEXIST);
+    ovs_assert(!plug_run_instances());
+
+    ovs_assert(!plug_open("dummy", &plug));
+    ovs_assert(plug_unregister_provider("dummy") == EBUSY);
+
+    ovs_assert(sset_contains(
+            plug_class_get_maintained_iface_options(plug),
+            "plug-dummy-option"));
+    ovs_assert(plug_run_instances());
+
+    struct smap fake_lport_options = SMAP_INITIALIZER(&fake_lport_options);
+    struct plug_port_ctx_in ctx_in = {
+        .op_type = PLUG_OP_CREATE,
+        .use_dpdk = false,
+        .lport_name = "lsp1",
+        .lport_options = &fake_lport_options,
+    };
+    struct plug_port_ctx_out ctx_out;
+    plug_port_prepare(plug, &ctx_in, &ctx_out);
+    ovs_assert(!strcmp(ctx_out.name, "lsp1"));
+    ovs_assert(!strcmp(ctx_out.type, "internal"));
+    ovs_assert(!strcmp(smap_get(
+            ctx_out.iface_options, "plug-dummy-option"), "value"));
+
+    plug_port_finish(plug, &ctx_in, &ctx_out);
+    plug_port_ctx_destroy(plug, &ctx_in, &ctx_out);
+    plug_close(plug);
+    plug_destroy_all();
+}
+
+static void
+test_plug_main(int argc, char *argv[])
+{
+    set_program_name(argv[0]);
+    static const struct ovs_cmdl_command commands[] = {
+        {"run", NULL, 0, 0, test_plug, OVS_RO},
+        {NULL, NULL, 0, 0, NULL, OVS_RO},
+    };
+    struct ovs_cmdl_context ctx;
+    ctx.argc = argc - 1;
+    ctx.argv = argv + 1;
+    ovs_cmdl_run_command(&ctx, commands);
+}
+
+OVSTEST_REGISTER("test-plug", test_plug_main);
diff --git a/tests/automake.mk b/tests/automake.mk
index 5b890d644..ad8978541 100644
--- a/tests/automake.mk
+++ b/tests/automake.mk
@@ -38,7 +38,8 @@  TESTSUITE_AT = \
 	tests/ovn-ipam.at \
 	tests/ovn-features.at \
 	tests/ovn-lflow-cache.at \
-	tests/ovn-ipsec.at
+	tests/ovn-ipsec.at \
+	tests/ovn-plug.at
 
 SYSTEM_KMOD_TESTSUITE_AT = \
 	tests/system-common-macros.at \
@@ -248,6 +249,7 @@  tests_ovstest_SOURCES = \
 	controller/ofctrl-seqno.c \
 	controller/ofctrl-seqno.h \
 	lib/test-ovn-features.c \
+	lib/test-plug.c \
 	northd/test-ipam.c \
 	northd/ipam.c \
 	northd/ipam.h
diff --git a/tests/ovn-plug.at b/tests/ovn-plug.at
new file mode 100644
index 000000000..d5c6a1b6d
--- /dev/null
+++ b/tests/ovn-plug.at
@@ -0,0 +1,8 @@ 
+#
+# Unit tests for the lib/plug.c module.
+#
+AT_BANNER([OVN unit tests - plug])
+
+AT_SETUP([unit test -- plugging infrastructure tests])
+AT_CHECK([ovstest test-plug run], [0], [])
+AT_CLEANUP