Merge tag 'pull-loongarch-20241016' of https://gitlab.com/gaosong/qemu into staging
[qemu/armbru.git] / net / hub.c
blob496a3d3b4bee79448e1a0ac0f3ae9ec269b78995
1 /*
2 * Hub net client
4 * Copyright IBM, Corp. 2012
6 * Authors:
7 * Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
8 * Zhi Yong Wu <wuzhy@linux.vnet.ibm.com>
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.
15 #include "qemu/osdep.h"
16 #include "qapi/error.h"
17 #include "monitor/monitor.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 "sysemu/qtest.h"
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.
30 typedef struct NetHub NetHub;
32 typedef struct NetHubPort {
33 NetClientState nc;
34 QLIST_ENTRY(NetHubPort) next;
35 NetHub *hub;
36 int id;
37 } NetHubPort;
39 struct NetHub {
40 int id;
41 QLIST_ENTRY(NetHub) next;
42 int num_ports;
43 QLIST_HEAD(, NetHubPort) ports;
46 static QLIST_HEAD(, NetHub) hubs = QLIST_HEAD_INITIALIZER(&hubs);
48 static ssize_t net_hub_receive(NetHub *hub, NetHubPort *source_port,
49 const uint8_t *buf, size_t len)
51 NetHubPort *port;
53 QLIST_FOREACH(port, &hub->ports, next) {
54 if (port == source_port) {
55 continue;
58 qemu_send_packet(&port->nc, buf, len);
60 return len;
63 static ssize_t net_hub_receive_iov(NetHub *hub, NetHubPort *source_port,
64 const struct iovec *iov, int iovcnt)
66 NetHubPort *port;
67 ssize_t len = iov_size(iov, iovcnt);
69 QLIST_FOREACH(port, &hub->ports, next) {
70 if (port == source_port) {
71 continue;
74 qemu_sendv_packet(&port->nc, iov, iovcnt);
76 return len;
79 static NetHub *net_hub_new(int id)
81 NetHub *hub;
83 hub = g_malloc(sizeof(*hub));
84 hub->id = id;
85 hub->num_ports = 0;
86 QLIST_INIT(&hub->ports);
88 QLIST_INSERT_HEAD(&hubs, hub, next);
90 return hub;
93 static bool net_hub_port_can_receive(NetClientState *nc)
95 NetHubPort *port;
96 NetHubPort *src_port = DO_UPCAST(NetHubPort, nc, nc);
97 NetHub *hub = src_port->hub;
99 QLIST_FOREACH(port, &hub->ports, next) {
100 if (port == src_port) {
101 continue;
104 if (qemu_can_send_packet(&port->nc)) {
105 return true;
109 return false;
112 static ssize_t net_hub_port_receive(NetClientState *nc,
113 const uint8_t *buf, size_t len)
115 NetHubPort *port = DO_UPCAST(NetHubPort, nc, nc);
117 return net_hub_receive(port->hub, port, buf, len);
120 static ssize_t net_hub_port_receive_iov(NetClientState *nc,
121 const struct iovec *iov, int iovcnt)
123 NetHubPort *port = DO_UPCAST(NetHubPort, nc, nc);
125 return net_hub_receive_iov(port->hub, port, iov, iovcnt);
128 static void net_hub_port_cleanup(NetClientState *nc)
130 NetHubPort *port = DO_UPCAST(NetHubPort, nc, nc);
132 QLIST_REMOVE(port, next);
135 static NetClientInfo net_hub_port_info = {
136 .type = NET_CLIENT_DRIVER_HUBPORT,
137 .size = sizeof(NetHubPort),
138 .can_receive = net_hub_port_can_receive,
139 .receive = net_hub_port_receive,
140 .receive_iov = net_hub_port_receive_iov,
141 .cleanup = net_hub_port_cleanup,
144 static NetHubPort *net_hub_port_new(NetHub *hub, const char *name,
145 NetClientState *hubpeer)
147 NetClientState *nc;
148 NetHubPort *port;
149 int id = hub->num_ports++;
150 char default_name[128];
152 if (!name) {
153 snprintf(default_name, sizeof(default_name),
154 "hub%dport%d", hub->id, id);
155 name = default_name;
158 nc = qemu_new_net_client(&net_hub_port_info, hubpeer, "hub", name);
159 port = DO_UPCAST(NetHubPort, nc, nc);
160 port->id = id;
161 port->hub = hub;
163 QLIST_INSERT_HEAD(&hub->ports, port, next);
165 return port;
169 * Create a port on a given hub
170 * @hub_id: Number of the hub
171 * @name: Net client name or NULL for default name.
172 * @hubpeer: Peer to use (if "netdev=id" has been specified)
174 * If there is no existing hub with the given id then a new hub is created.
176 NetClientState *net_hub_add_port(int hub_id, const char *name,
177 NetClientState *hubpeer)
179 NetHub *hub;
180 NetHubPort *port;
182 QLIST_FOREACH(hub, &hubs, next) {
183 if (hub->id == hub_id) {
184 break;
188 if (!hub) {
189 hub = net_hub_new(hub_id);
192 port = net_hub_port_new(hub, name, hubpeer);
193 return &port->nc;
197 * Print hub configuration
199 void net_hub_info(Monitor *mon)
201 NetHub *hub;
202 NetHubPort *port;
204 QLIST_FOREACH(hub, &hubs, next) {
205 monitor_printf(mon, "hub %d\n", hub->id);
206 QLIST_FOREACH(port, &hub->ports, next) {
207 monitor_printf(mon, " \\ %s", port->nc.name);
208 if (port->nc.peer) {
209 monitor_printf(mon, ": ");
210 print_net_client(mon, port->nc.peer);
211 } else {
212 monitor_printf(mon, "\n");
219 * Get the hub id that a client is connected to
221 * @id: Pointer for hub id output, may be NULL
223 int net_hub_id_for_client(NetClientState *nc, int *id)
225 NetHubPort *port;
227 if (nc->info->type == NET_CLIENT_DRIVER_HUBPORT) {
228 port = DO_UPCAST(NetHubPort, nc, nc);
229 } else if (nc->peer != NULL && nc->peer->info->type ==
230 NET_CLIENT_DRIVER_HUBPORT) {
231 port = DO_UPCAST(NetHubPort, nc, nc->peer);
232 } else {
233 return -ENOENT;
236 if (id) {
237 *id = port->hub->id;
239 return 0;
242 int net_init_hubport(const Netdev *netdev, const char *name,
243 NetClientState *peer, Error **errp)
245 const NetdevHubPortOptions *hubport;
246 NetClientState *hubpeer = NULL;
248 assert(netdev->type == NET_CLIENT_DRIVER_HUBPORT);
249 assert(!peer);
250 hubport = &netdev->u.hubport;
252 if (hubport->netdev) {
253 hubpeer = qemu_find_netdev(hubport->netdev);
254 if (!hubpeer) {
255 error_setg(errp, "netdev '%s' not found", hubport->netdev);
256 return -1;
260 net_hub_add_port(hubport->hubid, name, hubpeer);
262 return 0;
266 * Warn if hub configurations are likely wrong
268 void net_hub_check_clients(void)
270 NetHub *hub;
271 NetHubPort *port;
272 NetClientState *peer;
274 QLIST_FOREACH(hub, &hubs, next) {
275 int has_nic = 0, has_host_dev = 0;
277 QLIST_FOREACH(port, &hub->ports, next) {
278 peer = port->nc.peer;
279 if (!peer) {
280 warn_report("hub port %s has no peer", port->nc.name);
281 continue;
284 switch (peer->info->type) {
285 case NET_CLIENT_DRIVER_NIC:
286 has_nic = 1;
287 break;
288 case NET_CLIENT_DRIVER_USER:
289 case NET_CLIENT_DRIVER_TAP:
290 case NET_CLIENT_DRIVER_SOCKET:
291 case NET_CLIENT_DRIVER_STREAM:
292 case NET_CLIENT_DRIVER_DGRAM:
293 case NET_CLIENT_DRIVER_VDE:
294 case NET_CLIENT_DRIVER_VHOST_USER:
295 has_host_dev = 1;
296 break;
297 default:
298 break;
301 if (has_host_dev && !has_nic) {
302 warn_report("hub %d with no nics", hub->id);
304 if (has_nic && !has_host_dev && !qtest_enabled()) {
305 warn_report("hub %d is not connected to host network", hub->id);
310 bool net_hub_flush(NetClientState *nc)
312 NetHubPort *port;
313 NetHubPort *source_port = DO_UPCAST(NetHubPort, nc, nc);
314 int ret = 0;
316 QLIST_FOREACH(port, &source_port->hub->ports, next) {
317 if (port != source_port) {
318 ret += qemu_net_queue_flush(port->nc.incoming_queue);
321 return ret ? true : false;