aboutsummaryrefslogtreecommitdiffstats
path: root/adapter.c
blob: 1dbde9b9c582caf837a7204e8ab10a6ea45b0839 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*
 * Copyright 2022-2023 Virtual Open Systems SAS
 *
 * Authors:
 *  Timos Ampelikiotis <t.ampelikiotis@virtualopensystems.com>
 *  Stefanos Gerangelos <s.gerangelos@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 "adapter.h"

/* Global variables */
int client_sock;
struct vhost_dev *dev;
struct adapter_dev *adev;
struct vhost_user *vudev;

void vhost_user_adapter_init(void)
{

    DBG("Setup adapter data structures\n");

    /* 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;
    global_vdev->vhdev = dev;


    /* 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;

    DBG("Create shared socket with vhost-user-device\n");

    /* 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) {
        DBG("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: Check the \"-s\" parameter\n");
        close(client_sock);
        exit(1);
    }
}

static void help_args(void)
{
    printf("Run example:\n\t./adapter -s /path_to_socket/rng.sock\n"
           "\t\t  -d device_name\n"
           "\t\t  [ -qn number of queues ]\n"
           "\t\t  [ -qs size of queues ]\n"
           "The 'device_name' can be one of the following:\n"
           "\tvrng, vhurng, vhublk, vhuinput, vhusnd, vhugpio, vhucan, vhuconsole\n");
}

int find_arg(int argc, char **argv, char *str)
{
    int i;

    for (i = 0; i < argc; i++) {
        if (!strcmp(argv[i], str)) {
            return i + 1;
        }
    }
    return -1;
}

int val_device_arg(char *str)
{
    int adapter_devices_num = ADAPTER_DEVS, i;

    for (i = 0; i < adapter_devices_num; i++) {
        if (!strcmp(adapter_devices[i], str)) {
            return i + 1;
        }
    }

    return 0;
}

bool check_vhu_device(char *str)
{
    int vhu_devices_num = VHU_DEVS, i;

    for (i = 0; i < vhu_devices_num; i++) {
        if (!strcmp(vhu_devices[i], str)) {
            return true;
        }
    }

    return false;
}

void get_queue_num_size_args(int argc, char **argv,
                             int *eval_queue_num, int *eval_queue_size)
{
    int queue_num, queue_size, queue_num_id, queue_size_id;

    if (argc < 9) {
        return;
    }

    queue_num_id = find_arg(argc, argv, "-qn");
    queue_size_id = find_arg(argc, argv, "-qs");

    /* Check if both qs ans qn exist */
    if (queue_num_id < 0 || queue_size_id < 0) {
        return;
    }

    queue_num = atoi(argv[queue_num_id]);
    queue_size = atoi(argv[queue_size_id]);

    /* Evaluate number of queues */
    if (queue_num <= 0 || queue_num > 16) {
        return;
    }

    /* Evaluate queues' size */
    if (queue_size <= 0 || queue_size > 1024) {
        return;
    }

    *eval_queue_num = queue_num;
    *eval_queue_size = queue_size;
}


int main(int argc, char **argv)
{
    int socket_idx, device_idx, device_id;
    bool vhost_user_enabled;
    /* Assign default queue num and size */
    int queue_num = 1, queue_size = 64;

    /*
     * Check if the user has provided all the required arguments.
     * If not, print the help messages.
     */

    if (argc < 3) {
        goto error_args;
    }

    device_idx = find_arg(argc, argv, "-d");

    if (device_idx < 0) {
        printf("You have not specified parameter \"-d\"\n");
        goto error_args;
    }

    /* Validate the argumetns */
    device_id = val_device_arg(argv[device_idx]);

    if (device_id == 0) {
        goto error_args;
    }

    /* Check if this is a vhost-user device */
    vhost_user_enabled = check_vhu_device(argv[device_idx]);

    /* Check if a socket is needed and provided */
    socket_idx = find_arg(argc, argv, "-s");

    if ((socket_idx  < 0) && (vhost_user_enabled)) {
        printf("You have not specified parameter \"-s\"\n");
        goto error_args;
    }

    /*
     * Create the socket and connect to the backend.
     * Enabled on vhost-user case
     */
    if (vhost_user_enabled) {
        client(argv[socket_idx]);
    }

    /* Initialize the adapter data structures */
    vhost_user_adapter_init();

    /* Get queue number and size from arguments */
    get_queue_num_size_args(argc, argv, &queue_num, &queue_size);

    /* Initialize the virtio/vhost-user device */
    /* TODO: Switch numbers with name defs */
    switch (device_id) {
    case 1:
        virtio_rng_realize(queue_num, queue_size);
        break;
    case 2:
        vhost_user_rng_realize(queue_num, queue_size);
        break;
    case 3:
        printf("Running vhublk with num %d and size %d\n",
                                            queue_num, queue_size);
        vhost_user_blk_realize(queue_num, queue_size);
        break;
    case 4:
        vhost_user_input_init(global_vdev);
        virtio_input_device_realize(queue_num, queue_size);
        break;
    case 5:
        vus_device_realize(queue_num, queue_size);
        break;
    case 6:
        vu_gpio_device_realize(queue_num, queue_size);
        break;
    case 7:
        vhost_user_can_realize(queue_num, queue_size);
        break;
    case 8:
        vhost_user_console_realize(queue_num, queue_size);
        break;
    default:
        exit(1);
    }

    /*
     * Start loopback trasnport layer and communiation with the loopback driver
     */
    virtio_loopback_start();

    return 0;

error_args:
    help_args();
    return 1;
}