[PATCH 3/8] Add Event ID, Server side dispatcher and virsh print function

K Shiva Kiran posted 8 patches 10 months, 3 weeks ago
[PATCH 3/8] Add Event ID, Server side dispatcher and virsh print function
Posted by K Shiva Kiran 10 months, 3 weeks ago
Adds the following for Network Metadata change callback events:

- New Event ID VIR_NETWORK_EVENT_ID_METADATA_CHANGE
- Server side dispatcher

virsh:
- New event type `metadata-change`
- vshEventMetadataChangePrint() to display the above defined
  event type in virsh via `net-event`

Signed-off-by: K Shiva Kiran <shiva_kr@riseup.net>
---
I was unable to split this patch due to static assertions (perfomed
against VIR_NETWORK_EVENT_ID_LAST) in remote_daemon_dispatch.c and
virsh-network.c
Please let me know if there is a way to split patches in such cases.

 include/libvirt/libvirt-network.h   |  1 +
 src/conf/network_event.c            | 12 ++++++++
 src/remote/remote_daemon_dispatch.c | 38 ++++++++++++++++++++++++
 src/remote/remote_protocol.x        | 15 +++++++++-
 src/remote_protocol-structs         |  6 ++++
 tools/virsh-network.c               | 46 ++++++++++++++++++++++++++++-
 6 files changed, 116 insertions(+), 2 deletions(-)

diff --git a/include/libvirt/libvirt-network.h b/include/libvirt/libvirt-network.h
index 4b121ae0e7..58591be7ac 100644
--- a/include/libvirt/libvirt-network.h
+++ b/include/libvirt/libvirt-network.h
@@ -330,6 +330,7 @@ typedef void (*virConnectNetworkEventLifecycleCallback)(virConnectPtr conn,
  */
 typedef enum {
     VIR_NETWORK_EVENT_ID_LIFECYCLE = 0,       /* virConnectNetworkEventLifecycleCallback (Since: 1.2.1) */
+    VIR_NETWORK_EVENT_ID_METADATA_CHANGE = 1,   /* virConnectNetworkEventMetadataChangeCallback (Since: 9.8.0) */
 
 # ifdef VIR_ENUM_SENTINELS
     VIR_NETWORK_EVENT_ID_LAST
diff --git a/src/conf/network_event.c b/src/conf/network_event.c
index 51fa092ffd..d1b3aa5721 100644
--- a/src/conf/network_event.c
+++ b/src/conf/network_event.c
@@ -118,6 +118,18 @@ virNetworkEventDispatchDefaultFunc(virConnectPtr conn,
             return;
         }
 
+    case VIR_NETWORK_EVENT_ID_METADATA_CHANGE:
+        {
+            virNetworkEventMetadataChange *metadataChangeEvent;
+
+            metadataChangeEvent = (virNetworkEventMetadataChange *)event;
+            ((virConnectNetworkEventMetadataChangeCallback)cb)(conn, net,
+                                                               metadataChangeEvent->type,
+                                                               metadataChangeEvent->nsuri,
+                                                               cbopaque);
+            return;
+        }
+
     case VIR_NETWORK_EVENT_ID_LAST:
         break;
     }
diff --git a/src/remote/remote_daemon_dispatch.c b/src/remote/remote_daemon_dispatch.c
index 2bb9e306a4..7daf503b51 100644
--- a/src/remote/remote_daemon_dispatch.c
+++ b/src/remote/remote_daemon_dispatch.c
@@ -1385,8 +1385,46 @@ remoteRelayNetworkEventLifecycle(virConnectPtr conn,
     return 0;
 }
 
+static int
+remoteRelayNetworkEventMetadataChange(virConnectPtr conn,
+                                      virNetworkPtr net,
+                                      int type,
+                                      const char *nsuri,
+                                      void *opaque)
+{
+    daemonClientEventCallback *callback = opaque;
+    remote_network_event_callback_metadata_change_msg data;
+
+    if (callback->callbackID < 0 ||
+        !remoteRelayNetworkEventCheckACL(callback->client, conn, net))
+        return -1;
+
+    VIR_DEBUG("Relaying network metadata change %s %d %s, callback %d",
+              net->name, type, NULLSTR(nsuri), callback->callbackID);
+
+    /* build return data */
+    memset(&data, 0, sizeof(data));
+
+    data.type = type;
+    if (nsuri) {
+        data.nsuri = g_new0(remote_nonnull_string, 1);
+        *(data.nsuri) = g_strdup(nsuri);
+    }
+
+    make_nonnull_network(&data.net, net);
+    data.callbackID = callback->callbackID;
+
+    remoteDispatchObjectEventSend(callback->client, callback->program,
+                                  REMOTE_PROC_NETWORK_EVENT_CALLBACK_METADATA_CHANGE,
+                                  (xdrproc_t)xdr_remote_network_event_callback_metadata_change_msg,
+                                  &data);
+    return 0;
+}
+
+
 static virConnectNetworkEventGenericCallback networkEventCallbacks[] = {
     VIR_NETWORK_EVENT_CALLBACK(remoteRelayNetworkEventLifecycle),
+    VIR_NETWORK_EVENT_CALLBACK(remoteRelayNetworkEventMetadataChange),
 };
 
 G_STATIC_ASSERT(G_N_ELEMENTS(networkEventCallbacks) == VIR_NETWORK_EVENT_ID_LAST);
diff --git a/src/remote/remote_protocol.x b/src/remote/remote_protocol.x
index 7ff059e393..e295b0acc3 100644
--- a/src/remote/remote_protocol.x
+++ b/src/remote/remote_protocol.x
@@ -3323,6 +3323,13 @@ struct remote_network_event_lifecycle_msg {
     int detail;
 };
 
+struct remote_network_event_callback_metadata_change_msg {
+    int callbackID;
+    remote_nonnull_network net;
+    int type;
+    remote_string nsuri;
+};
+
 struct remote_network_set_metadata_args {
     remote_nonnull_network network;
     int type;
@@ -7008,5 +7015,11 @@ enum remote_procedure {
      * @generate: both
      * @acl: network:read
      */
-    REMOTE_PROC_NETWORK_GET_METADATA = 445
+    REMOTE_PROC_NETWORK_GET_METADATA = 445,
+
+    /**
+     * @generate: both
+     * @acl: none
+     */
+    REMOTE_PROC_NETWORK_EVENT_CALLBACK_METADATA_CHANGE = 446
 };
diff --git a/src/remote_protocol-structs b/src/remote_protocol-structs
index c07e0af1e6..e6132bee71 100644
--- a/src/remote_protocol-structs
+++ b/src/remote_protocol-structs
@@ -2687,6 +2687,12 @@ struct remote_network_event_lifecycle_msg {
         int                        event;
         int                        detail;
 };
+struct remote_network_event_callback_metadata_change_msg {
+        int                        callbackID;
+        remote_nonnull_network     net;
+        int                        type;
+        remote_string              nsuri;
+};
 struct remote_network_set_metadata_args {
         remote_nonnull_network     network;
         int                        type;
diff --git a/tools/virsh-network.c b/tools/virsh-network.c
index de5d60593a..8965d87c9c 100644
--- a/tools/virsh-network.c
+++ b/tools/virsh-network.c
@@ -1582,7 +1582,8 @@ typedef struct virshNetEventData virshNetEventData;
 VIR_ENUM_DECL(virshNetworkEventId);
 VIR_ENUM_IMPL(virshNetworkEventId,
               VIR_NETWORK_EVENT_ID_LAST,
-              "lifecycle");
+              "lifecycle",
+              "metadata-change");
 
 static void
 vshEventLifecyclePrint(virConnectPtr conn G_GNUC_UNUSED,
@@ -1615,9 +1616,52 @@ vshEventLifecyclePrint(virConnectPtr conn G_GNUC_UNUSED,
         vshEventDone(data->ctl);
 }
 
+VIR_ENUM_DECL(virshNetworkEventMetadataChangeType);
+VIR_ENUM_IMPL(virshNetworkEventMetadataChangeType,
+              VIR_NETWORK_METADATA_LAST,
+              N_("description"),
+              N_("title"),
+              N_("element"));
+
+#define UNKNOWNSTR(str) (str ? str : N_("unsupported value"))
+
+static void
+vshEventMetadataChangePrint(virConnectPtr conn G_GNUC_UNUSED,
+                            virNetworkPtr net,
+                            int type,
+                            const char *nsuri,
+                            void *opaque)
+{
+    virshNetEventData *data = opaque;
+
+    if (!data->loop && data->count)
+        return;
+
+    if (data->timestamp) {
+        char timestamp[VIR_TIME_STRING_BUFLEN];
+
+        if (virTimeStringNowRaw(timestamp) < 0)
+            timestamp[0] = '\0';
+
+        vshPrint(data->ctl, _("%1$s: event 'metadata-change' for network %2$s: type %3$s, uri %4$s\n"),
+                 timestamp, virNetworkGetName(net),
+                 UNKNOWNSTR(virshNetworkEventMetadataChangeTypeTypeToString(type)), NULLSTR(nsuri));
+    } else {
+        vshPrint(data->ctl, _("event 'metadata-change' for network %1$s: type %2$s, uri %3$s\n"),
+                 virNetworkGetName(net),
+                 UNKNOWNSTR(virshNetworkEventMetadataChangeTypeTypeToString(type)), NULLSTR(nsuri));
+    }
+
+    data->count++;
+    if (!data->loop)
+        vshEventDone(data->ctl);
+}
+
 virshNetworkEventCallback virshNetworkEventCallbacks[] = {
     { "lifecycle",
       VIR_NETWORK_EVENT_CALLBACK(vshEventLifecyclePrint), },
+    { "metadata-change",
+      VIR_NETWORK_EVENT_CALLBACK(vshEventMetadataChangePrint), },
 };
 G_STATIC_ASSERT(VIR_NETWORK_EVENT_ID_LAST == G_N_ELEMENTS(virshNetworkEventCallbacks));
 
-- 
2.42.0
Re: [PATCH 3/8] Add Event ID, Server side dispatcher and virsh print function
Posted by Michal Prívozník 10 months, 1 week ago
On 9/3/23 16:58, K Shiva Kiran wrote:
> Adds the following for Network Metadata change callback events:
> 
> - New Event ID VIR_NETWORK_EVENT_ID_METADATA_CHANGE
> - Server side dispatcher
> 
> virsh:
> - New event type `metadata-change`
> - vshEventMetadataChangePrint() to display the above defined
>   event type in virsh via `net-event`
> 
> Signed-off-by: K Shiva Kiran <shiva_kr@riseup.net>
> ---
> I was unable to split this patch due to static assertions (perfomed
> against VIR_NETWORK_EVENT_ID_LAST) in remote_daemon_dispatch.c and
> virsh-network.c
> Please let me know if there is a way to split patches in such cases.
> 
>  include/libvirt/libvirt-network.h   |  1 +
>  src/conf/network_event.c            | 12 ++++++++
>  src/remote/remote_daemon_dispatch.c | 38 ++++++++++++++++++++++++
>  src/remote/remote_protocol.x        | 15 +++++++++-
>  src/remote_protocol-structs         |  6 ++++
>  tools/virsh-network.c               | 46 ++++++++++++++++++++++++++++-
>  6 files changed, 116 insertions(+), 2 deletions(-)
> 
> diff --git a/include/libvirt/libvirt-network.h b/include/libvirt/libvirt-network.h
> index 4b121ae0e7..58591be7ac 100644
> --- a/include/libvirt/libvirt-network.h
> +++ b/include/libvirt/libvirt-network.h
> @@ -330,6 +330,7 @@ typedef void (*virConnectNetworkEventLifecycleCallback)(virConnectPtr conn,
>   */
>  typedef enum {
>      VIR_NETWORK_EVENT_ID_LIFECYCLE = 0,       /* virConnectNetworkEventLifecycleCallback (Since: 1.2.1) */
> +    VIR_NETWORK_EVENT_ID_METADATA_CHANGE = 1,   /* virConnectNetworkEventMetadataChangeCallback (Since: 9.8.0) */
>  
>  # ifdef VIR_ENUM_SENTINELS
>      VIR_NETWORK_EVENT_ID_LAST
> diff --git a/src/conf/network_event.c b/src/conf/network_event.c
> index 51fa092ffd..d1b3aa5721 100644
> --- a/src/conf/network_event.c
> +++ b/src/conf/network_event.c
> @@ -118,6 +118,18 @@ virNetworkEventDispatchDefaultFunc(virConnectPtr conn,
>              return;
>          }
>  
> +    case VIR_NETWORK_EVENT_ID_METADATA_CHANGE:
> +        {
> +            virNetworkEventMetadataChange *metadataChangeEvent;
> +
> +            metadataChangeEvent = (virNetworkEventMetadataChange *)event;
> +            ((virConnectNetworkEventMetadataChangeCallback)cb)(conn, net,
> +                                                               metadataChangeEvent->type,
> +                                                               metadataChangeEvent->nsuri,
> +                                                               cbopaque);
> +            return;
> +        }
> +
>      case VIR_NETWORK_EVENT_ID_LAST:
>          break;
>      }
> diff --git a/src/remote/remote_daemon_dispatch.c b/src/remote/remote_daemon_dispatch.c
> index 2bb9e306a4..7daf503b51 100644
> --- a/src/remote/remote_daemon_dispatch.c
> +++ b/src/remote/remote_daemon_dispatch.c
> @@ -1385,8 +1385,46 @@ remoteRelayNetworkEventLifecycle(virConnectPtr conn,
>      return 0;
>  }
>  
> +static int
> +remoteRelayNetworkEventMetadataChange(virConnectPtr conn,
> +                                      virNetworkPtr net,
> +                                      int type,
> +                                      const char *nsuri,
> +                                      void *opaque)
> +{
> +    daemonClientEventCallback *callback = opaque;
> +    remote_network_event_callback_metadata_change_msg data;
> +
> +    if (callback->callbackID < 0 ||
> +        !remoteRelayNetworkEventCheckACL(callback->client, conn, net))
> +        return -1;
> +
> +    VIR_DEBUG("Relaying network metadata change %s %d %s, callback %d",
> +              net->name, type, NULLSTR(nsuri), callback->callbackID);
> +
> +    /* build return data */
> +    memset(&data, 0, sizeof(data));

Declaring the variable with = { 0 } initializer is preferred (see
v9.7.0-rc1~160).	

> +
> +    data.type = type;
> +    if (nsuri) {
> +        data.nsuri = g_new0(remote_nonnull_string, 1);
> +        *(data.nsuri) = g_strdup(nsuri);
> +    }
> +
> +    make_nonnull_network(&data.net, net);
> +    data.callbackID = callback->callbackID;
> +
> +    remoteDispatchObjectEventSend(callback->client, callback->program,
> +                                  REMOTE_PROC_NETWORK_EVENT_CALLBACK_METADATA_CHANGE,
> +                                  (xdrproc_t)xdr_remote_network_event_callback_metadata_change_msg,
> +                                  &data);
> +    return 0;
> +}
> +
> +
>  static virConnectNetworkEventGenericCallback networkEventCallbacks[] = {
>      VIR_NETWORK_EVENT_CALLBACK(remoteRelayNetworkEventLifecycle),
> +    VIR_NETWORK_EVENT_CALLBACK(remoteRelayNetworkEventMetadataChange),
>  };
>  
>  G_STATIC_ASSERT(G_N_ELEMENTS(networkEventCallbacks) == VIR_NETWORK_EVENT_ID_LAST);
> diff --git a/src/remote/remote_protocol.x b/src/remote/remote_protocol.x
> index 7ff059e393..e295b0acc3 100644
> --- a/src/remote/remote_protocol.x
> +++ b/src/remote/remote_protocol.x
> @@ -3323,6 +3323,13 @@ struct remote_network_event_lifecycle_msg {
>      int detail;
>  };
>  
> +struct remote_network_event_callback_metadata_change_msg {
> +    int callbackID;
> +    remote_nonnull_network net;
> +    int type;
> +    remote_string nsuri;
> +};
> +
>  struct remote_network_set_metadata_args {
>      remote_nonnull_network network;
>      int type;
> @@ -7008,5 +7015,11 @@ enum remote_procedure {
>       * @generate: both
>       * @acl: network:read
>       */
> -    REMOTE_PROC_NETWORK_GET_METADATA = 445
> +    REMOTE_PROC_NETWORK_GET_METADATA = 445,
> +
> +    /**
> +     * @generate: both
> +     * @acl: none
> +     */
> +    REMOTE_PROC_NETWORK_EVENT_CALLBACK_METADATA_CHANGE = 446
>  };
> diff --git a/src/remote_protocol-structs b/src/remote_protocol-structs
> index c07e0af1e6..e6132bee71 100644
> --- a/src/remote_protocol-structs
> +++ b/src/remote_protocol-structs
> @@ -2687,6 +2687,12 @@ struct remote_network_event_lifecycle_msg {
>          int                        event;
>          int                        detail;
>  };
> +struct remote_network_event_callback_metadata_change_msg {
> +        int                        callbackID;
> +        remote_nonnull_network     net;
> +        int                        type;
> +        remote_string              nsuri;
> +};
>  struct remote_network_set_metadata_args {
>          remote_nonnull_network     network;
>          int                        type;

REMOTE_PROC_NETWORK_EVENT_CALLBACK_METADATA_CHANGE = 446

is missing

Michal