Nikolay Nikolaev | d314f58 | 2014-05-27 15:06:29 +0300 | [diff] [blame^] | 1 | /* |
| 2 | * vhost-user.c |
| 3 | * |
| 4 | * Copyright (c) 2013 Virtual Open Systems Sarl. |
| 5 | * |
| 6 | * This work is licensed under the terms of the GNU GPL, version 2 or later. |
| 7 | * See the COPYING file in the top-level directory. |
| 8 | * |
| 9 | */ |
| 10 | |
| 11 | #include "clients.h" |
| 12 | #include "net/vhost_net.h" |
| 13 | #include "net/vhost-user.h" |
| 14 | #include "sysemu/char.h" |
| 15 | #include "qemu/error-report.h" |
| 16 | |
| 17 | typedef struct VhostUserState { |
| 18 | NetClientState nc; |
| 19 | CharDriverState *chr; |
| 20 | bool vhostforce; |
| 21 | VHostNetState *vhost_net; |
| 22 | } VhostUserState; |
| 23 | |
| 24 | VHostNetState *vhost_user_get_vhost_net(NetClientState *nc) |
| 25 | { |
| 26 | VhostUserState *s = DO_UPCAST(VhostUserState, nc, nc); |
| 27 | return s->vhost_net; |
| 28 | } |
| 29 | |
| 30 | static int vhost_user_running(VhostUserState *s) |
| 31 | { |
| 32 | return (s->vhost_net) ? 1 : 0; |
| 33 | } |
| 34 | |
| 35 | static int vhost_user_start(VhostUserState *s) |
| 36 | { |
| 37 | VhostNetOptions options; |
| 38 | |
| 39 | if (vhost_user_running(s)) { |
| 40 | return 0; |
| 41 | } |
| 42 | |
| 43 | options.backend_type = VHOST_BACKEND_TYPE_USER; |
| 44 | options.net_backend = &s->nc; |
| 45 | options.opaque = s->chr; |
| 46 | options.force = s->vhostforce; |
| 47 | |
| 48 | s->vhost_net = vhost_net_init(&options); |
| 49 | |
| 50 | return vhost_user_running(s) ? 0 : -1; |
| 51 | } |
| 52 | |
| 53 | static void vhost_user_stop(VhostUserState *s) |
| 54 | { |
| 55 | if (vhost_user_running(s)) { |
| 56 | vhost_net_cleanup(s->vhost_net); |
| 57 | } |
| 58 | |
| 59 | s->vhost_net = 0; |
| 60 | } |
| 61 | |
| 62 | static void vhost_user_cleanup(NetClientState *nc) |
| 63 | { |
| 64 | VhostUserState *s = DO_UPCAST(VhostUserState, nc, nc); |
| 65 | |
| 66 | vhost_user_stop(s); |
| 67 | qemu_purge_queued_packets(nc); |
| 68 | } |
| 69 | |
| 70 | static bool vhost_user_has_vnet_hdr(NetClientState *nc) |
| 71 | { |
| 72 | assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_VHOST_USER); |
| 73 | |
| 74 | return true; |
| 75 | } |
| 76 | |
| 77 | static bool vhost_user_has_ufo(NetClientState *nc) |
| 78 | { |
| 79 | assert(nc->info->type == NET_CLIENT_OPTIONS_KIND_VHOST_USER); |
| 80 | |
| 81 | return true; |
| 82 | } |
| 83 | |
| 84 | static NetClientInfo net_vhost_user_info = { |
| 85 | .type = 0, |
| 86 | .size = sizeof(VhostUserState), |
| 87 | .cleanup = vhost_user_cleanup, |
| 88 | .has_vnet_hdr = vhost_user_has_vnet_hdr, |
| 89 | .has_ufo = vhost_user_has_ufo, |
| 90 | }; |
| 91 | |
| 92 | static void net_vhost_link_down(VhostUserState *s, bool link_down) |
| 93 | { |
| 94 | s->nc.link_down = link_down; |
| 95 | |
| 96 | if (s->nc.peer) { |
| 97 | s->nc.peer->link_down = link_down; |
| 98 | } |
| 99 | |
| 100 | if (s->nc.info->link_status_changed) { |
| 101 | s->nc.info->link_status_changed(&s->nc); |
| 102 | } |
| 103 | |
| 104 | if (s->nc.peer && s->nc.peer->info->link_status_changed) { |
| 105 | s->nc.peer->info->link_status_changed(s->nc.peer); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | static void net_vhost_user_event(void *opaque, int event) |
| 110 | { |
| 111 | VhostUserState *s = opaque; |
| 112 | |
| 113 | switch (event) { |
| 114 | case CHR_EVENT_OPENED: |
| 115 | vhost_user_start(s); |
| 116 | net_vhost_link_down(s, false); |
| 117 | error_report("chardev \"%s\" went up\n", s->chr->label); |
| 118 | break; |
| 119 | case CHR_EVENT_CLOSED: |
| 120 | net_vhost_link_down(s, true); |
| 121 | vhost_user_stop(s); |
| 122 | error_report("chardev \"%s\" went down\n", s->chr->label); |
| 123 | break; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | static int net_vhost_user_init(NetClientState *peer, const char *device, |
| 128 | const char *name, CharDriverState *chr, |
| 129 | bool vhostforce) |
| 130 | { |
| 131 | NetClientState *nc; |
| 132 | VhostUserState *s; |
| 133 | |
| 134 | nc = qemu_new_net_client(&net_vhost_user_info, peer, device, name); |
| 135 | |
| 136 | snprintf(nc->info_str, sizeof(nc->info_str), "vhost-user to %s", |
| 137 | chr->label); |
| 138 | |
| 139 | s = DO_UPCAST(VhostUserState, nc, nc); |
| 140 | |
| 141 | /* We don't provide a receive callback */ |
| 142 | s->nc.receive_disabled = 1; |
| 143 | s->chr = chr; |
| 144 | s->vhostforce = vhostforce; |
| 145 | |
| 146 | qemu_chr_add_handlers(s->chr, NULL, NULL, net_vhost_user_event, s); |
| 147 | |
| 148 | return 0; |
| 149 | } |
| 150 | |
| 151 | int net_init_vhost_user(const NetClientOptions *opts, const char *name, |
| 152 | NetClientState *peer) |
| 153 | { |
| 154 | return net_vhost_user_init(peer, "vhost_user", 0, 0, 0); |
| 155 | } |