diff mbox

[v4,33/47] ivshmem-server: use a uint16 for client ID

Message ID 1443094669-4144-34-git-send-email-marcandre.lureau@redhat.com
State New
Headers show

Commit Message

Marc-André Lureau Sept. 24, 2015, 11:37 a.m. UTC
From: Marc-André Lureau <marcandre.lureau@redhat.com>

In practice, the number of VM is limited to MAXUINT16 in ivshmem, so use
the same limit on the server (removes a theorical infinite loop)

Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
---
 contrib/ivshmem-server/ivshmem-server.c | 11 ++++++++++-
 contrib/ivshmem-server/ivshmem-server.h |  2 +-
 2 files changed, 11 insertions(+), 2 deletions(-)

Comments

Claudio Fontana Sept. 29, 2015, 1:51 p.m. UTC | #1
On 24.09.2015 13:37, marcandre.lureau@redhat.com wrote:
> From: Marc-André Lureau <marcandre.lureau@redhat.com>
> 
> In practice, the number of VM is limited to MAXUINT16 in ivshmem, so use
> the same limit on the server (removes a theorical infinite loop)
> 
> Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
> ---
>  contrib/ivshmem-server/ivshmem-server.c | 11 ++++++++++-
>  contrib/ivshmem-server/ivshmem-server.h |  2 +-
>  2 files changed, 11 insertions(+), 2 deletions(-)
> 
> diff --git a/contrib/ivshmem-server/ivshmem-server.c b/contrib/ivshmem-server/ivshmem-server.c
> index 16ee583..972fda2 100644
> --- a/contrib/ivshmem-server/ivshmem-server.c
> +++ b/contrib/ivshmem-server/ivshmem-server.c
> @@ -145,9 +145,18 @@ ivshmem_server_handle_new_conn(IvshmemServer *server)
>      peer->sock_fd = newfd;
>  
>      /* get an unused peer id */
> -    while (ivshmem_server_search_peer(server, server->cur_id) != NULL) {
> +    /* XXX: this could use id allocation such as Linux IDA, or simply
> +     * a free-list */
> +    for (i = 0; i < G_MAXUINT16; i++) {
> +        if (ivshmem_server_search_peer(server, server->cur_id) == NULL) {
> +            break;
> +        }
>          server->cur_id++;
>      }
> +    if (i == G_MAXUINT16) {
> +        IVSHMEM_SERVER_DEBUG(server, "cannot allocate new client id\n");
> +        goto fail;
> +    }
>      peer->id = server->cur_id++;
>  
>      /* create eventfd, one per vector */
> diff --git a/contrib/ivshmem-server/ivshmem-server.h b/contrib/ivshmem-server/ivshmem-server.h
> index cd584fc..2176d5e 100644
> --- a/contrib/ivshmem-server/ivshmem-server.h
> +++ b/contrib/ivshmem-server/ivshmem-server.h
> @@ -70,7 +70,7 @@ typedef struct IvshmemServer {
>      size_t shm_size;                 /**< size of shm */
>      int shm_fd;                      /**< shm file descriptor */
>      unsigned n_vectors;              /**< number of vectors */
> -    long cur_id;                     /**< id to be given to next client */
> +    uint16_t cur_id;                 /**< id to be given to next client */
>      bool verbose;                    /**< true in verbose mode */
>      IvshmemServerPeerList peer_list; /**< list of peers */
>  } IvshmemServer;
> 

Reviewed-by: Claudio Fontana <claudio.fontana@huawei.com>
diff mbox

Patch

diff --git a/contrib/ivshmem-server/ivshmem-server.c b/contrib/ivshmem-server/ivshmem-server.c
index 16ee583..972fda2 100644
--- a/contrib/ivshmem-server/ivshmem-server.c
+++ b/contrib/ivshmem-server/ivshmem-server.c
@@ -145,9 +145,18 @@  ivshmem_server_handle_new_conn(IvshmemServer *server)
     peer->sock_fd = newfd;
 
     /* get an unused peer id */
-    while (ivshmem_server_search_peer(server, server->cur_id) != NULL) {
+    /* XXX: this could use id allocation such as Linux IDA, or simply
+     * a free-list */
+    for (i = 0; i < G_MAXUINT16; i++) {
+        if (ivshmem_server_search_peer(server, server->cur_id) == NULL) {
+            break;
+        }
         server->cur_id++;
     }
+    if (i == G_MAXUINT16) {
+        IVSHMEM_SERVER_DEBUG(server, "cannot allocate new client id\n");
+        goto fail;
+    }
     peer->id = server->cur_id++;
 
     /* create eventfd, one per vector */
diff --git a/contrib/ivshmem-server/ivshmem-server.h b/contrib/ivshmem-server/ivshmem-server.h
index cd584fc..2176d5e 100644
--- a/contrib/ivshmem-server/ivshmem-server.h
+++ b/contrib/ivshmem-server/ivshmem-server.h
@@ -70,7 +70,7 @@  typedef struct IvshmemServer {
     size_t shm_size;                 /**< size of shm */
     int shm_fd;                      /**< shm file descriptor */
     unsigned n_vectors;              /**< number of vectors */
-    long cur_id;                     /**< id to be given to next client */
+    uint16_t cur_id;                 /**< id to be given to next client */
     bool verbose;                    /**< true in verbose mode */
     IvshmemServerPeerList peer_list; /**< list of peers */
 } IvshmemServer;