forked from apache/nuttx
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
drivers/vhost-rng: add vhost rng driver support for NuttX
vhost-rng is a simple virtio device implementation, it receives the buffer receviced from the virtio-rng driver and fill the random number to this buffer and return to virtio-rng. Signed-off-by: Bowen Wang <[email protected]>
- Loading branch information
1 parent
7afbcc1
commit c42576e
Showing
6 changed files
with
275 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,189 @@ | ||
/**************************************************************************** | ||
* drivers/vhost/vhost-rng.c | ||
* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. The | ||
* ASF licenses this file to you 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. | ||
* | ||
****************************************************************************/ | ||
|
||
/**************************************************************************** | ||
* Included Files | ||
****************************************************************************/ | ||
|
||
#include <debug.h> | ||
#include <errno.h> | ||
#include <stdio.h> | ||
#include <sys/random.h> | ||
|
||
#include <nuttx/kmalloc.h> | ||
#include <nuttx/vhost/vhost.h> | ||
#include <nuttx/wqueue.h> | ||
|
||
/**************************************************************************** | ||
* Pre-processor Definitions | ||
****************************************************************************/ | ||
|
||
/**************************************************************************** | ||
* Private Types | ||
****************************************************************************/ | ||
|
||
struct vhost_rng_priv_s | ||
{ | ||
FAR struct vhost_device *hdev; | ||
struct work_s work; | ||
}; | ||
|
||
/**************************************************************************** | ||
* Private Function Prototypes | ||
****************************************************************************/ | ||
|
||
/* Vhost driver functions */ | ||
|
||
static int vhost_rng_probe(FAR struct vhost_device *hdev); | ||
static void vhost_rng_remove(FAR struct vhost_device *hdev); | ||
|
||
static void vhost_rng_work(FAR void *arg); | ||
static void vhost_rng_handler(FAR struct virtqueue *vq); | ||
|
||
/**************************************************************************** | ||
* Private Data | ||
****************************************************************************/ | ||
|
||
static struct vhost_driver g_vhost_rng_driver = | ||
{ | ||
LIST_INITIAL_VALUE(g_vhost_rng_driver.node), /* Node */ | ||
VIRTIO_ID_ENTROPY, /* Device id */ | ||
vhost_rng_probe, /* Probe */ | ||
vhost_rng_remove, /* Remove */ | ||
}; | ||
|
||
/**************************************************************************** | ||
* Private Functions | ||
****************************************************************************/ | ||
|
||
/**************************************************************************** | ||
* Name: vhost_rng_thread | ||
****************************************************************************/ | ||
|
||
static void vhost_rng_work(FAR void *arg) | ||
{ | ||
FAR struct vhost_rng_priv_s *priv = arg; | ||
FAR struct virtqueue *vq; | ||
FAR void *buf; | ||
uint16_t idx; | ||
uint32_t len; | ||
ssize_t ret; | ||
|
||
vq = priv->hdev->vrings_info[0].vq; | ||
for (; ; ) | ||
{ | ||
buf = virtqueue_get_available_buffer(vq, &idx, &len); | ||
if (buf == NULL) | ||
{ | ||
break; | ||
} | ||
|
||
ret = getrandom(buf, len, 0); | ||
if (ret < 0) | ||
{ | ||
vhosterr("getrandom failed, ret=%zd\n", ret); | ||
ret = 0; | ||
} | ||
|
||
virtqueue_add_consumed_buffer(vq, idx, (uint32_t)ret); | ||
virtqueue_kick(vq); | ||
} | ||
} | ||
|
||
/**************************************************************************** | ||
* Name: vhost_rng_handler | ||
****************************************************************************/ | ||
|
||
static void vhost_rng_handler(FAR struct virtqueue *vq) | ||
{ | ||
FAR struct vhost_rng_priv_s *priv = vq->vq_dev->priv; | ||
|
||
if (work_available(&priv->work)) | ||
{ | ||
work_queue(HPWORK, &priv->work, vhost_rng_work, priv, 0); | ||
} | ||
} | ||
|
||
/**************************************************************************** | ||
* Name: vhost_rng_probe | ||
****************************************************************************/ | ||
|
||
static int vhost_rng_probe(FAR struct vhost_device *hdev) | ||
{ | ||
FAR struct vhost_rng_priv_s *priv; | ||
FAR const char *vqnames[1]; | ||
vq_callback callback[1]; | ||
int ret; | ||
|
||
priv = kmm_zalloc(sizeof(*priv)); | ||
if (priv == NULL) | ||
{ | ||
vhosterr("No enough memory\n"); | ||
return -ENOMEM; | ||
} | ||
|
||
priv->hdev = hdev; | ||
hdev->priv = priv; | ||
|
||
/* Create the virtqueues */ | ||
|
||
vqnames[0] = "virtio_rng"; | ||
callback[0] = vhost_rng_handler; | ||
ret = vhost_create_virtqueues(hdev, 0, 1, vqnames, callback); | ||
if (ret < 0) | ||
{ | ||
vhosterr("virtio_device_create_virtqueue failed, ret=%d\n", ret); | ||
goto err; | ||
} | ||
|
||
virtqueue_enable_cb(hdev->vrings_info[0].vq); | ||
return ret; | ||
|
||
err: | ||
kmm_free(priv); | ||
return ret; | ||
} | ||
|
||
/**************************************************************************** | ||
* Name: vhost_rng_remove | ||
****************************************************************************/ | ||
|
||
static void vhost_rng_remove(FAR struct vhost_device *hdev) | ||
{ | ||
FAR struct vhost_rng_priv_s *priv = hdev->priv; | ||
|
||
virtqueue_disable_cb(hdev->vrings_info[0].vq); | ||
work_cancel_sync(HPWORK, &priv->work); | ||
vhost_delete_virtqueues(hdev); | ||
kmm_free(priv); | ||
} | ||
|
||
/**************************************************************************** | ||
* Public Functions | ||
****************************************************************************/ | ||
|
||
/**************************************************************************** | ||
* Name: vhost_register_rng_driver | ||
****************************************************************************/ | ||
|
||
int vhost_register_rng_driver(void) | ||
{ | ||
return vhost_register_driver(&g_vhost_rng_driver); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/**************************************************************************** | ||
* drivers/vhost/vhost-rng.h | ||
* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. The | ||
* ASF licenses this file to you 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 __DRIVERS_VHOST_VHOST_RNG_H | ||
#define __DRIVERS_VHOST_VHOST_RNG_H | ||
|
||
/**************************************************************************** | ||
* Included Files | ||
****************************************************************************/ | ||
|
||
#include <nuttx/config.h> | ||
|
||
#ifdef CONFIG_DRIVERS_VHOST_RNG | ||
|
||
/**************************************************************************** | ||
* Pre-processor Definitions | ||
****************************************************************************/ | ||
|
||
/**************************************************************************** | ||
* Public Data | ||
****************************************************************************/ | ||
|
||
#ifdef __cplusplus | ||
#define EXTERN extern "C" | ||
extern "C" | ||
{ | ||
#else | ||
#define EXTERN extern | ||
#endif | ||
|
||
/**************************************************************************** | ||
* Public Function Prototypes | ||
****************************************************************************/ | ||
|
||
int vhost_register_rng_driver(void); | ||
|
||
#undef EXTERN | ||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
||
#endif /* CONFIG_DRIVERS_VHOST_RNG */ | ||
|
||
#endif /* __DRIVERS_VHOST_VHOST_RNG_H */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters