hub.c 8.3 KB
Newer Older
S
Stefan Hajnoczi 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * Hub net client
 *
 * Copyright IBM, Corp. 2012
 *
 * Authors:
 *  Stefan Hajnoczi   <stefanha@linux.vnet.ibm.com>
 *  Zhi Yong Wu       <wuzhy@linux.vnet.ibm.com>
 *
 * This work is licensed under the terms of the GNU LGPL, version 2 or later.
 * See the COPYING.LIB file in the top-level directory.
 *
 */

P
Peter Maydell 已提交
15
#include "qemu/osdep.h"
16
#include "monitor/monitor.h"
P
Paolo Bonzini 已提交
17
#include "net/net.h"
18
#include "clients.h"
S
Stefan Hajnoczi 已提交
19
#include "hub.h"
20
#include "qemu/iov.h"
S
Stefan Hajnoczi 已提交
21 22 23 24 25 26 27 28 29 30

/*
 * A hub broadcasts incoming packets to all its ports except the source port.
 * Hubs can be used to provide independent network segments, also confusingly
 * named the QEMU 'vlan' feature.
 */

typedef struct NetHub NetHub;

typedef struct NetHubPort {
31
    NetClientState nc;
S
Stefan Hajnoczi 已提交
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
    QLIST_ENTRY(NetHubPort) next;
    NetHub *hub;
    int id;
} NetHubPort;

struct NetHub {
    int id;
    QLIST_ENTRY(NetHub) next;
    int num_ports;
    QLIST_HEAD(, NetHubPort) ports;
};

static QLIST_HEAD(, NetHub) hubs = QLIST_HEAD_INITIALIZER(&hubs);

static ssize_t net_hub_receive(NetHub *hub, NetHubPort *source_port,
                               const uint8_t *buf, size_t len)
{
    NetHubPort *port;

    QLIST_FOREACH(port, &hub->ports, next) {
        if (port == source_port) {
            continue;
        }

        qemu_send_packet(&port->nc, buf, len);
    }
    return len;
}

static ssize_t net_hub_receive_iov(NetHub *hub, NetHubPort *source_port,
                                   const struct iovec *iov, int iovcnt)
{
    NetHubPort *port;
65
    ssize_t len = iov_size(iov, iovcnt);
S
Stefan Hajnoczi 已提交
66 67 68 69 70 71

    QLIST_FOREACH(port, &hub->ports, next) {
        if (port == source_port) {
            continue;
        }

72
        qemu_sendv_packet(&port->nc, iov, iovcnt);
S
Stefan Hajnoczi 已提交
73
    }
74
    return len;
S
Stefan Hajnoczi 已提交
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
}

static NetHub *net_hub_new(int id)
{
    NetHub *hub;

    hub = g_malloc(sizeof(*hub));
    hub->id = id;
    hub->num_ports = 0;
    QLIST_INIT(&hub->ports);

    QLIST_INSERT_HEAD(&hubs, hub, next);

    return hub;
}

91 92 93 94 95 96 97 98 99 100 101
static int net_hub_port_can_receive(NetClientState *nc)
{
    NetHubPort *port;
    NetHubPort *src_port = DO_UPCAST(NetHubPort, nc, nc);
    NetHub *hub = src_port->hub;

    QLIST_FOREACH(port, &hub->ports, next) {
        if (port == src_port) {
            continue;
        }

102 103
        if (qemu_can_send_packet(&port->nc)) {
            return 1;
104 105 106
        }
    }

107
    return 0;
108 109
}

110
static ssize_t net_hub_port_receive(NetClientState *nc,
S
Stefan Hajnoczi 已提交
111 112 113 114 115 116 117
                                    const uint8_t *buf, size_t len)
{
    NetHubPort *port = DO_UPCAST(NetHubPort, nc, nc);

    return net_hub_receive(port->hub, port, buf, len);
}

118
static ssize_t net_hub_port_receive_iov(NetClientState *nc,
S
Stefan Hajnoczi 已提交
119 120 121 122 123 124 125
                                        const struct iovec *iov, int iovcnt)
{
    NetHubPort *port = DO_UPCAST(NetHubPort, nc, nc);

    return net_hub_receive_iov(port->hub, port, iov, iovcnt);
}

126
static void net_hub_port_cleanup(NetClientState *nc)
S
Stefan Hajnoczi 已提交
127 128 129 130 131 132 133 134 135
{
    NetHubPort *port = DO_UPCAST(NetHubPort, nc, nc);

    QLIST_REMOVE(port, next);
}

static NetClientInfo net_hub_port_info = {
    .type = NET_CLIENT_OPTIONS_KIND_HUBPORT,
    .size = sizeof(NetHubPort),
136
    .can_receive = net_hub_port_can_receive,
S
Stefan Hajnoczi 已提交
137 138 139 140 141 142 143
    .receive = net_hub_port_receive,
    .receive_iov = net_hub_port_receive_iov,
    .cleanup = net_hub_port_cleanup,
};

static NetHubPort *net_hub_port_new(NetHub *hub, const char *name)
{
144
    NetClientState *nc;
S
Stefan Hajnoczi 已提交
145 146 147 148 149 150 151 152 153 154
    NetHubPort *port;
    int id = hub->num_ports++;
    char default_name[128];

    if (!name) {
        snprintf(default_name, sizeof(default_name),
                 "hub%dport%d", hub->id, id);
        name = default_name;
    }

155
    nc = qemu_new_net_client(&net_hub_port_info, NULL, "hub", name);
S
Stefan Hajnoczi 已提交
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
    port = DO_UPCAST(NetHubPort, nc, nc);
    port->id = id;
    port->hub = hub;

    QLIST_INSERT_HEAD(&hub->ports, port, next);

    return port;
}

/**
 * Create a port on a given hub
 * @name: Net client name or NULL for default name.
 *
 * If there is no existing hub with the given id then a new hub is created.
 */
171
NetClientState *net_hub_add_port(int hub_id, const char *name)
S
Stefan Hajnoczi 已提交
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
{
    NetHub *hub;
    NetHubPort *port;

    QLIST_FOREACH(hub, &hubs, next) {
        if (hub->id == hub_id) {
            break;
        }
    }

    if (!hub) {
        hub = net_hub_new(hub_id);
    }

    port = net_hub_port_new(hub, name);
    return &port->nc;
}

190 191 192
/**
 * Find a specific client on a hub
 */
193
NetClientState *net_hub_find_client_by_name(int hub_id, const char *name)
194 195 196
{
    NetHub *hub;
    NetHubPort *port;
197
    NetClientState *peer;
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212

    QLIST_FOREACH(hub, &hubs, next) {
        if (hub->id == hub_id) {
            QLIST_FOREACH(port, &hub->ports, next) {
                peer = port->nc.peer;

                if (peer && strcmp(peer->name, name) == 0) {
                    return peer;
                }
            }
        }
    }
    return NULL;
}

213 214 215
/**
 * Find a available port on a hub; otherwise create one new port
 */
216
NetClientState *net_hub_port_find(int hub_id)
217 218 219
{
    NetHub *hub;
    NetHubPort *port;
220
    NetClientState *nc;
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237

    QLIST_FOREACH(hub, &hubs, next) {
        if (hub->id == hub_id) {
            QLIST_FOREACH(port, &hub->ports, next) {
                nc = port->nc.peer;
                if (!nc) {
                    return &(port->nc);
                }
            }
            break;
        }
    }

    nc = net_hub_add_port(hub_id, NULL);
    return nc;
}

S
Stefan Hajnoczi 已提交
238 239 240 241 242 243 244 245 246 247 248
/**
 * Print hub configuration
 */
void net_hub_info(Monitor *mon)
{
    NetHub *hub;
    NetHubPort *port;

    QLIST_FOREACH(hub, &hubs, next) {
        monitor_printf(mon, "hub %d\n", hub->id);
        QLIST_FOREACH(port, &hub->ports, next) {
249
            monitor_printf(mon, " \\ %s", port->nc.name);
250
            if (port->nc.peer) {
251
                monitor_printf(mon, ": ");
252
                print_net_client(mon, port->nc.peer);
253 254
            } else {
                monitor_printf(mon, "\n");
255
            }
S
Stefan Hajnoczi 已提交
256 257 258 259 260 261 262
        }
    }
}

/**
 * Get the hub id that a client is connected to
 *
263
 * @id: Pointer for hub id output, may be NULL
S
Stefan Hajnoczi 已提交
264
 */
265
int net_hub_id_for_client(NetClientState *nc, int *id)
S
Stefan Hajnoczi 已提交
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
{
    NetHubPort *port;

    if (nc->info->type == NET_CLIENT_OPTIONS_KIND_HUBPORT) {
        port = DO_UPCAST(NetHubPort, nc, nc);
    } else if (nc->peer != NULL && nc->peer->info->type ==
            NET_CLIENT_OPTIONS_KIND_HUBPORT) {
        port = DO_UPCAST(NetHubPort, nc, nc->peer);
    } else {
        return -ENOENT;
    }

    if (id) {
        *id = port->hub->id;
    }
    return 0;
}

int net_init_hubport(const NetClientOptions *opts, const char *name,
285
                     NetClientState *peer, Error **errp)
S
Stefan Hajnoczi 已提交
286 287 288
{
    const NetdevHubPortOptions *hubport;

E
Eric Blake 已提交
289
    assert(opts->type == NET_CLIENT_OPTIONS_KIND_HUBPORT);
290
    assert(!peer);
291
    hubport = opts->u.hubport.data;
S
Stefan Hajnoczi 已提交
292 293 294 295

    net_hub_add_port(hubport->hubid, name);
    return 0;
}
296 297 298 299 300 301 302 303

/**
 * Warn if hub configurations are likely wrong
 */
void net_hub_check_clients(void)
{
    NetHub *hub;
    NetHubPort *port;
304
    NetClientState *peer;
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324

    QLIST_FOREACH(hub, &hubs, next) {
        int has_nic = 0, has_host_dev = 0;

        QLIST_FOREACH(port, &hub->ports, next) {
            peer = port->nc.peer;
            if (!peer) {
                fprintf(stderr, "Warning: hub port %s has no peer\n",
                        port->nc.name);
                continue;
            }

            switch (peer->info->type) {
            case NET_CLIENT_OPTIONS_KIND_NIC:
                has_nic = 1;
                break;
            case NET_CLIENT_OPTIONS_KIND_USER:
            case NET_CLIENT_OPTIONS_KIND_TAP:
            case NET_CLIENT_OPTIONS_KIND_SOCKET:
            case NET_CLIENT_OPTIONS_KIND_VDE:
325
            case NET_CLIENT_OPTIONS_KIND_VHOST_USER:
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
                has_host_dev = 1;
                break;
            default:
                break;
            }
        }
        if (has_host_dev && !has_nic) {
            fprintf(stderr, "Warning: vlan %d with no nics\n", hub->id);
        }
        if (has_nic && !has_host_dev) {
            fprintf(stderr,
                    "Warning: vlan %d is not connected to host network\n",
                    hub->id);
        }
    }
}
342 343 344 345 346 347 348 349 350

bool net_hub_flush(NetClientState *nc)
{
    NetHubPort *port;
    NetHubPort *source_port = DO_UPCAST(NetHubPort, nc, nc);
    int ret = 0;

    QLIST_FOREACH(port, &source_port->hub->ports, next) {
        if (port != source_port) {
351
            ret += qemu_net_queue_flush(port->nc.incoming_queue);
352 353 354 355
        }
    }
    return ret ? true : false;
}