master
c 336 lines 8.03 KB
Raw
1 /*
2 * Hub net client
3 *
4 * Copyright IBM, Corp. 2012
5 *
6 * Authors:
7 * Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
8 * Zhi Yong Wu <wuzhy@linux.vnet.ibm.com>
9 *
10 * This work is licensed under the terms of the GNU LGPL, version 2 or later.
11 * See the COPYING.LIB file in the top-level directory.
12 *
13 */
14
15 #include "qemu/osdep.h"
16 #include "qapi/error.h"
17 #include "qapi/util.h"
18 #include "net/net.h"
19 #include "clients.h"
20 #include "hub.h"
21 #include "qemu/iov.h"
22 #include "qemu/error-report.h"
23 #include "system/qtest.h"
24
25 /*
26 * A hub broadcasts incoming packets to all its ports except the source port.
27 * Hubs can be used to provide independent emulated network segments.
28 */
29
30 typedef struct NetHub NetHub;
31
32 typedef struct NetHubPort {
33 NetClientState nc;
34 QLIST_ENTRY(NetHubPort) next;
35 NetHub *hub;
36 int id;
37 bool listed;
38 } NetHubPort;
39
40 struct NetHub {
41 int id;
42 QLIST_ENTRY(NetHub) next;
43 int num_ports;
44 QLIST_HEAD(, NetHubPort) ports;
45 };
46
47 static QLIST_HEAD(, NetHub) hubs = QLIST_HEAD_INITIALIZER(&hubs);
48
49 static ssize_t net_hub_receive(NetHub *hub, NetHubPort *source_port,
50 const uint8_t *buf, size_t len)
51 {
52 NetHubPort *port;
53
54 QLIST_FOREACH(port, &hub->ports, next) {
55 if (port == source_port) {
56 continue;
57 }
58
59 qemu_send_packet(&port->nc, buf, len);
60 }
61 return len;
62 }
63
64 static ssize_t net_hub_receive_iov(NetHub *hub, NetHubPort *source_port,
65 const struct iovec *iov, int iovcnt)
66 {
67 NetHubPort *port;
68 ssize_t len = iov_size(iov, iovcnt);
69
70 QLIST_FOREACH(port, &hub->ports, next) {
71 if (port == source_port) {
72 continue;
73 }
74
75 qemu_sendv_packet(&port->nc, iov, iovcnt);
76 }
77 return len;
78 }
79
80 static NetHub *net_hub_new(int id)
81 {
82 NetHub *hub;
83
84 hub = g_malloc(sizeof(*hub));
85 hub->id = id;
86 hub->num_ports = 0;
87 QLIST_INIT(&hub->ports);
88
89 QLIST_INSERT_HEAD(&hubs, hub, next);
90
91 return hub;
92 }
93
94 static bool net_hub_port_can_receive(NetClientState *nc)
95 {
96 NetHubPort *port;
97 NetHubPort *src_port = DO_UPCAST(NetHubPort, nc, nc);
98 NetHub *hub = src_port->hub;
99
100 QLIST_FOREACH(port, &hub->ports, next) {
101 if (port == src_port) {
102 continue;
103 }
104
105 if (qemu_can_send_packet(&port->nc)) {
106 return true;
107 }
108 }
109
110 return false;
111 }
112
113 static ssize_t net_hub_port_receive(NetClientState *nc,
114 const uint8_t *buf, size_t len)
115 {
116 NetHubPort *port = DO_UPCAST(NetHubPort, nc, nc);
117
118 return net_hub_receive(port->hub, port, buf, len);
119 }
120
121 static ssize_t net_hub_port_receive_iov(NetClientState *nc,
122 const struct iovec *iov, int iovcnt)
123 {
124 NetHubPort *port = DO_UPCAST(NetHubPort, nc, nc);
125
126 return net_hub_receive_iov(port->hub, port, iov, iovcnt);
127 }
128
129 static void net_hub_port_cleanup(NetClientState *nc)
130 {
131 NetHubPort *port = DO_UPCAST(NetHubPort, nc, nc);
132
133 if (port->listed) {
134 QLIST_REMOVE(port, next);
135 port->listed = false;
136 }
137 }
138
139 static NetClientInfo net_hub_port_info = {
140 .type = NET_CLIENT_DRIVER_HUBPORT,
141 .size = sizeof(NetHubPort),
142 .can_receive = net_hub_port_can_receive,
143 .receive = net_hub_port_receive,
144 .receive_iov = net_hub_port_receive_iov,
145 .cleanup = net_hub_port_cleanup,
146 };
147
148 static NetHubPort *net_hub_port_new(NetHub *hub, const char *name,
149 NetClientState *hubpeer)
150 {
151 NetClientState *nc;
152 NetHubPort *port;
153 int id = hub->num_ports++;
154 char default_name[128];
155
156 if (!name) {
157 snprintf(default_name, sizeof(default_name),
158 "hub%dport%d", hub->id, id);
159 name = default_name;
160 }
161
162 nc = qemu_new_net_client(&net_hub_port_info, hubpeer, "hub", name);
163 port = DO_UPCAST(NetHubPort, nc, nc);
164 port->id = id;
165 port->hub = hub;
166 port->listed = false;
167
168 QLIST_INSERT_HEAD(&hub->ports, port, next);
169 port->listed = true;
170
171 return port;
172 }
173
174 /**
175 * Create a port on a given hub
176 * @hub_id: Number of the hub
177 * @name: Net client name or NULL for default name.
178 * @hubpeer: Peer to use (if "netdev=id" has been specified)
179 *
180 * If there is no existing hub with the given id then a new hub is created.
181 */
182 NetClientState *net_hub_add_port(int hub_id, const char *name,
183 NetClientState *hubpeer)
184 {
185 NetHub *hub;
186 NetHubPort *port;
187
188 QLIST_FOREACH(hub, &hubs, next) {
189 if (hub->id == hub_id) {
190 break;
191 }
192 }
193
194 if (!hub) {
195 hub = net_hub_new(hub_id);
196 }
197
198 port = net_hub_port_new(hub, name, hubpeer);
199 return &port->nc;
200 }
201
202 NetHubInfoList *net_hub_query_info(void)
203 {
204 NetHubInfoList *head = NULL, **tail = &head;
205 NetHub *hub;
206
207 QLIST_FOREACH(hub, &hubs, next) {
208 NetHubInfo *hi = g_new0(NetHubInfo, 1);
209 NetHubPortInfoList **ptail = &hi->ports;
210 NetHubPort *port;
211
212 hi->id = hub->id;
213
214 QLIST_FOREACH(port, &hub->ports, next) {
215 NetHubPortInfo *pi = g_new0(NetHubPortInfo, 1);
216 pi->name = g_strdup(port->nc.name);
217 if (port->nc.peer) {
218 pi->peer = net_client_info(port->nc.peer);
219 }
220 QAPI_LIST_APPEND(ptail, pi);
221 }
222
223 QAPI_LIST_APPEND(tail, hi);
224 }
225
226 return head;
227 }
228
229 /**
230 * Get the hub id that a client is connected to
231 *
232 * @id: Pointer for hub id output, may be NULL
233 */
234 int net_hub_id_for_client(NetClientState *nc, int *id)
235 {
236 NetHubPort *port;
237
238 if (nc->info->type == NET_CLIENT_DRIVER_HUBPORT) {
239 port = DO_UPCAST(NetHubPort, nc, nc);
240 } else if (nc->peer != NULL && nc->peer->info->type ==
241 NET_CLIENT_DRIVER_HUBPORT) {
242 port = DO_UPCAST(NetHubPort, nc, nc->peer);
243 } else {
244 return -ENOENT;
245 }
246
247 if (id) {
248 *id = port->hub->id;
249 }
250 return 0;
251 }
252
253 int net_init_hubport(const Netdev *netdev, const char *name,
254 NetClientState *peer, Error **errp)
255 {
256 const NetdevHubPortOptions *hubport;
257 NetClientState *hubpeer = NULL;
258
259 assert(netdev->type == NET_CLIENT_DRIVER_HUBPORT);
260 assert(!peer);
261 hubport = &netdev->u.hubport;
262
263 if (hubport->netdev) {
264 hubpeer = qemu_find_netdev(hubport->netdev);
265 if (!hubpeer) {
266 error_setg(errp, "netdev '%s' not found", hubport->netdev);
267 return -1;
268 }
269 }
270
271 net_hub_add_port(hubport->hubid, name, hubpeer);
272
273 return 0;
274 }
275
276 /**
277 * Warn if hub configurations are likely wrong
278 */
279 void net_hub_check_clients(void)
280 {
281 NetHub *hub;
282 NetHubPort *port;
283 NetClientState *peer;
284
285 QLIST_FOREACH(hub, &hubs, next) {
286 int has_nic = 0, has_host_dev = 0;
287
288 QLIST_FOREACH(port, &hub->ports, next) {
289 peer = port->nc.peer;
290 if (!peer) {
291 warn_report("hub port %s has no peer", port->nc.name);
292 continue;
293 }
294
295 switch (peer->info->type) {
296 case NET_CLIENT_DRIVER_NIC:
297 has_nic = 1;
298 break;
299 #ifdef CONFIG_PASST
300 case NET_CLIENT_DRIVER_PASST:
301 #endif
302 case NET_CLIENT_DRIVER_USER:
303 case NET_CLIENT_DRIVER_TAP:
304 case NET_CLIENT_DRIVER_SOCKET:
305 case NET_CLIENT_DRIVER_STREAM:
306 case NET_CLIENT_DRIVER_DGRAM:
307 case NET_CLIENT_DRIVER_VDE:
308 case NET_CLIENT_DRIVER_VHOST_USER:
309 has_host_dev = 1;
310 break;
311 default:
312 break;
313 }
314 }
315 if (has_host_dev && !has_nic) {
316 warn_report("hub %d with no nics", hub->id);
317 }
318 if (has_nic && !has_host_dev && !qtest_enabled()) {
319 warn_report("hub %d is not connected to host network", hub->id);
320 }
321 }
322 }
323
324 bool net_hub_flush(NetClientState *nc)
325 {
326 NetHubPort *port;
327 NetHubPort *source_port = DO_UPCAST(NetHubPort, nc, nc);
328 int ret = 0;
329
330 QLIST_FOREACH(port, &source_port->hub->ports, next) {
331 if (port != source_port) {
332 ret += qemu_net_queue_flush(port->nc.incoming_queue);
333 }
334 }
335 return ret ? true : false;
336 }