virportallocator.c 5.5 KB
Newer Older
D
Daniel P. Berrange 已提交
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
/*
 * virportallocator.c: Allocate & track TCP port allocations
 *
 * Copyright (C) 2013 Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see
 * <http://www.gnu.org/licenses/>.
 *
 */

#include <config.h>

#include <sys/socket.h>
#include <arpa/inet.h>
E
Eric Blake 已提交
26
#include <netinet/in.h>
D
Daniel P. Berrange 已提交
27 28 29 30 31 32 33

#include "viralloc.h"
#include "virbitmap.h"
#include "virportallocator.h"
#include "virthread.h"
#include "virerror.h"
#include "virfile.h"
J
Ján Tomko 已提交
34
#include "virstring.h"
D
Daniel P. Berrange 已提交
35 36 37 38 39 40 41

#define VIR_FROM_THIS VIR_FROM_NONE

struct _virPortAllocator {
    virObjectLockable parent;
    virBitmapPtr bitmap;

J
Ján Tomko 已提交
42 43
    char *name;

D
Daniel P. Berrange 已提交
44 45 46 47 48 49 50 51 52 53 54 55
    unsigned short start;
    unsigned short end;
};

static virClassPtr virPortAllocatorClass;

static void
virPortAllocatorDispose(void *obj)
{
    virPortAllocatorPtr pa = obj;

    virBitmapFree(pa->bitmap);
J
Ján Tomko 已提交
56
    VIR_FREE(pa->name);
D
Daniel P. Berrange 已提交
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
}

static int virPortAllocatorOnceInit(void)
{
    if (!(virPortAllocatorClass = virClassNew(virClassForObjectLockable(),
                                              "virPortAllocator",
                                              sizeof(virPortAllocator),
                                              virPortAllocatorDispose)))
        return -1;

    return 0;
}

VIR_ONCE_GLOBAL_INIT(virPortAllocator)

J
Ján Tomko 已提交
72 73
virPortAllocatorPtr virPortAllocatorNew(const char *name,
                                        unsigned short start,
D
Daniel P. Berrange 已提交
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
                                        unsigned short end)
{
    virPortAllocatorPtr pa;

    if (start >= end) {
        virReportInvalidArg(start, "start port %d must be less than end port %d",
                            start, end);
        return NULL;
    }

    if (virPortAllocatorInitialize() < 0)
        return NULL;

    if (!(pa = virObjectLockableNew(virPortAllocatorClass)))
        return NULL;

    pa->start = start;
    pa->end = end;

J
Ján Tomko 已提交
93 94
    if (!(pa->bitmap = virBitmapNew((end-start)+1)) ||
        VIR_STRDUP(pa->name, name) < 0) {
D
Daniel P. Berrange 已提交
95 96 97 98 99 100 101
        virObjectUnref(pa);
        return NULL;
    }

    return pa;
}

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
static int virPortAllocatorBindToPort(bool *used,
                                      unsigned short port)
{
    struct sockaddr_in addr = {
        .sin_family = AF_INET,
        .sin_port = htons(port),
        .sin_addr.s_addr = htonl(INADDR_ANY)
    };
    int reuse = 1;
    int ret = -1;
    int fd = -1;

    *used = false;

    fd = socket(PF_INET, SOCK_STREAM, 0);
    if (fd < 0) {
        virReportSystemError(errno, "%s", _("Unable to open test socket"));
        goto cleanup;
    }

    if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (void*)&reuse,
                   sizeof(reuse)) < 0) {
        virReportSystemError(errno, "%s",
                             _("Unable to set socket reuse addr flag"));
        goto cleanup;
    }

    if (bind(fd, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
        if (errno == EADDRINUSE) {
            *used = true;
            ret = 0;
        } else {
            virReportSystemError(errno, _("Unable to bind to port %d"), port);
        }
        goto cleanup;
    }

    ret = 0;
cleanup:
    VIR_FORCE_CLOSE(fd);
    return ret;
}

D
Daniel P. Berrange 已提交
145 146 147 148
int virPortAllocatorAcquire(virPortAllocatorPtr pa,
                            unsigned short *port)
{
    int ret = -1;
149
    size_t i;
D
Daniel P. Berrange 已提交
150 151 152 153

    *port = 0;
    virObjectLock(pa);

154
    for (i = pa->start; i <= pa->end && !*port; i++) {
D
Daniel P. Berrange 已提交
155 156 157 158 159
        bool used = false;

        if (virBitmapGetBit(pa->bitmap,
                            i - pa->start, &used) < 0) {
            virReportError(VIR_ERR_INTERNAL_ERROR,
160
                           _("Failed to query port %zu"), i);
D
Daniel P. Berrange 已提交
161 162 163 164 165 166
            goto cleanup;
        }

        if (used)
            continue;

167
        if (virPortAllocatorBindToPort(&used, i) < 0)
D
Daniel P. Berrange 已提交
168 169
            goto cleanup;

170
        if (!used) {
D
Daniel P. Berrange 已提交
171 172 173 174
            /* Add port to bitmap of reserved ports */
            if (virBitmapSetBit(pa->bitmap,
                                i - pa->start) < 0) {
                virReportError(VIR_ERR_INTERNAL_ERROR,
175
                               _("Failed to reserve port %zu"), i);
D
Daniel P. Berrange 已提交
176 177 178
                goto cleanup;
            }
            *port = i;
179
            ret = 0;
D
Daniel P. Berrange 已提交
180 181 182
        }
    }

183 184 185 186 187
    if (*port == 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Unable to find an unused port in range '%s' (%d-%d)"),
                       pa->name, pa->start, pa->end);
    }
D
Daniel P. Berrange 已提交
188 189 190 191 192 193 194 195 196
cleanup:
    virObjectUnlock(pa);
    return ret;
}

int virPortAllocatorRelease(virPortAllocatorPtr pa,
                            unsigned short port)
{
    int ret = -1;
197 198 199 200

    if (!port)
        return 0;

D
Daniel P. Berrange 已提交
201 202 203
    virObjectLock(pa);

    if (port < pa->start ||
204
        port > pa->end) {
D
Daniel P. Berrange 已提交
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
        virReportInvalidArg(port, "port %d must be in range (%d, %d)",
                            port, pa->start, pa->end);
        goto cleanup;
    }

    if (virBitmapClearBit(pa->bitmap,
                          port - pa->start) < 0) {
        virReportError(VIR_ERR_INTERNAL_ERROR,
                       _("Failed to release port %d"),
                       port);
        goto cleanup;
    }

    ret = 0;
cleanup:
    virObjectUnlock(pa);
    return ret;
}