aboutsummaryrefslogtreecommitdiffstats
path: root/adapter.c
diff options
context:
space:
mode:
authorTimos Ampelikiotis <t.ampelikiotis@virtualopensystems.com>2022-09-21 15:36:21 +0200
committerAngelos Mouzakitis <a.mouzakitis@virtualopensystems.com>2023-10-03 15:18:54 +0300
commit346f9c0f4a72945795b2bf203d2294ff00f25148 (patch)
tree46107a4c094fefb92ee2362c8297368d7d1e9ac8 /adapter.c
parentc5abd7c9c809e21d8c41353c57049a7ec1c60436 (diff)
Virtio-loopback-adapter Alpha release:
- The virtio-loopback-adapter is part of the Virtio Loopback architecture detailed in the following link: https://git.virtualopensystems.com/virtio-loopback/docs Signed-off-by: Timos Ampelikiotis <t.ampelikiotis@virtualopensystems.com>
Diffstat (limited to 'adapter.c')
-rw-r--r--adapter.c157
1 files changed, 157 insertions, 0 deletions
diff --git a/adapter.c b/adapter.c
new file mode 100644
index 0000000..2aebd9f
--- /dev/null
+++ b/adapter.c
@@ -0,0 +1,157 @@
+/*
+ * Copyright 2022 Virtual Open Systems SAS.
+ *
+ * Authors:
+ * Timos Ampelikiotis <t.ampelikiotis@virtualopensystems.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
+#include <unistd.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <stdint.h>
+#include <string.h>
+#include <sys/eventfd.h>
+#include <fcntl.h>
+#include <sys/ioctl.h>
+#include <sys/mman.h>
+#include <pthread.h>
+#include <stdbool.h>
+#include <sys/param.h>
+#include <assert.h>
+
+/* For socket */
+#include <errno.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+
+/* Project header files */
+#include "virtio_loopback.h"
+#include "vhost_user_loopback.h"
+#include "virtio_rng.h"
+#include "vhost_user_rng.h"
+
+/* Global variables */
+int client_sock;
+struct vhost_dev *dev;
+struct adapter_dev *adev;
+struct vhost_user *vudev;
+
+
+void vhost_user_adapter_init (void)
+{
+ /* Init vhost-user device */
+ vudev = (struct vhost_user*) malloc(sizeof(struct vhost_user));
+
+ /* Init vhost device */
+ dev = (struct vhost_dev*) malloc(sizeof(struct vhost_dev));
+
+ /* Init virtio device */
+ global_vdev = (VirtIODevice*) malloc(sizeof(VirtIODevice));
+
+ /* Init virtio bus */
+ global_vbus = (VirtioBus *) malloc(sizeof(VirtioBus));
+ global_vbus->vdev = global_vdev;
+ global_vdev->vbus = global_vbus;
+
+ /* Store virtio_dev reference into vhost_dev struct*/
+ dev->vdev = global_vdev;
+
+ /* Init adapter device */
+ adev = (struct adapter_dev*) malloc(sizeof(struct adapter_dev));
+ adev->vdev = dev;
+ adev->vudev = vudev;
+ adev->virtio_dev = global_vdev;
+ adev->vbus = global_vbus;
+}
+
+
+void client (char *sock_path)
+{
+ int rc, len;
+ struct sockaddr_un client_sockaddr;
+
+ /* Initialize the struct to zero */
+ memset(&client_sockaddr, 0, sizeof(struct sockaddr_un));
+
+ /*
+ * Create a UNIX socket
+ */
+ client_sock = socket(AF_UNIX, SOCK_STREAM, 0);
+ if (client_sock == -1) {
+ printf("SOCKET ERROR\n");
+ exit(1);
+ }
+
+ /*
+ * Set up the UNIX sockaddr structure
+ * by using AF_UNIX for the family and
+ * giving it a filepath to connect.
+ */
+ client_sockaddr.sun_family = AF_UNIX;
+ strcpy(client_sockaddr.sun_path, sock_path);
+ len = sizeof(client_sockaddr);
+ rc = connect(client_sock, (struct sockaddr *) &client_sockaddr, len);
+ if(rc == -1) {
+ printf("CONNECT ERROR\n");
+ close(client_sock);
+ exit(1);
+ }
+
+}
+
+static void help_args (void) {
+ printf("Run example:\n\t./adapter -s /path_to_socket/rng.sock\n");
+}
+
+int main (int argc, char **argv)
+{
+#ifdef VHOST_USER_RNG_DEV
+ /*
+ * Check if the user has provided a socket path.
+ * If not, print the help messages.
+ */
+ if ((argc <= 2) || (strcmp(argv[1], "-s") != 0)) {
+ goto error_args;
+ }
+
+ /*
+ * Create the socket and connect to the backend.
+ * Enabled on vhost-user case
+ */
+ client(argv[2]);
+#endif
+
+ /* Initialize the adapter data structures */
+ vhost_user_adapter_init();
+
+ /* Initialize the virtio/vhost-user device */
+#ifdef VHOST_USER_RNG_DEV
+ vhost_user_rng_realize(); /* <-- Enable that for vhost-user-rng */
+#else
+ virtio_rng_realize(); /* <-- Enable that for simple rng */
+#endif
+
+ /* Startthe mmio trasnport layer and communiation with the loopback driver */
+ virtio_mmio_start();
+
+ return 0;
+
+error_args:
+ help_args();
+ return 1;
+}