From 346f9c0f4a72945795b2bf203d2294ff00f25148 Mon Sep 17 00:00:00 2001 From: Timos Ampelikiotis Date: Wed, 21 Sep 2022 15:36:21 +0200 Subject: 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 --- adapter.c | 157 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 157 insertions(+) create mode 100644 adapter.c (limited to 'adapter.c') 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 + * + * 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 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* For socket */ +#include +#include +#include +#include + +/* 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; +} -- cgit