root / net.c @ f68b9d67
History | View | Annotate | Download (38.5 kB)
1 | 63a01ef8 | aliguori | /*
|
---|---|---|---|
2 | 63a01ef8 | aliguori | * QEMU System Emulator
|
3 | 63a01ef8 | aliguori | *
|
4 | 63a01ef8 | aliguori | * Copyright (c) 2003-2008 Fabrice Bellard
|
5 | 63a01ef8 | aliguori | *
|
6 | 63a01ef8 | aliguori | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 | 63a01ef8 | aliguori | * of this software and associated documentation files (the "Software"), to deal
|
8 | 63a01ef8 | aliguori | * in the Software without restriction, including without limitation the rights
|
9 | 63a01ef8 | aliguori | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10 | 63a01ef8 | aliguori | * copies of the Software, and to permit persons to whom the Software is
|
11 | 63a01ef8 | aliguori | * furnished to do so, subject to the following conditions:
|
12 | 63a01ef8 | aliguori | *
|
13 | 63a01ef8 | aliguori | * The above copyright notice and this permission notice shall be included in
|
14 | 63a01ef8 | aliguori | * all copies or substantial portions of the Software.
|
15 | 63a01ef8 | aliguori | *
|
16 | 63a01ef8 | aliguori | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17 | 63a01ef8 | aliguori | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18 | 63a01ef8 | aliguori | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
19 | 63a01ef8 | aliguori | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20 | 63a01ef8 | aliguori | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21 | 63a01ef8 | aliguori | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22 | 63a01ef8 | aliguori | * THE SOFTWARE.
|
23 | 63a01ef8 | aliguori | */
|
24 | 1df49e04 | Mark McLoughlin | #include "net.h" |
25 | 63a01ef8 | aliguori | |
26 | d40cdb10 | blueswir1 | #include "config-host.h" |
27 | d40cdb10 | blueswir1 | |
28 | a8ed73f7 | Mark McLoughlin | #include "net/tap.h" |
29 | 42281ac9 | Mark McLoughlin | #include "net/socket.h" |
30 | 1abecf77 | Mark McLoughlin | #include "net/dump.h" |
31 | 68ac40d2 | Mark McLoughlin | #include "net/slirp.h" |
32 | 5c361cc3 | Mark McLoughlin | #include "net/vde.h" |
33 | f1d078c3 | Mark McLoughlin | #include "net/util.h" |
34 | 511d2b14 | blueswir1 | #include "monitor.h" |
35 | 511d2b14 | blueswir1 | #include "sysemu.h" |
36 | 1df49e04 | Mark McLoughlin | #include "qemu-common.h" |
37 | 511d2b14 | blueswir1 | #include "qemu_socket.h" |
38 | 75422b0d | Amit Shah | #include "hw/qdev.h" |
39 | ce053661 | Benjamin Poirier | #include "iov.h" |
40 | 511d2b14 | blueswir1 | |
41 | 5610c3aa | Mark McLoughlin | static QTAILQ_HEAD(, VLANState) vlans;
|
42 | 577c4af9 | Mark McLoughlin | static QTAILQ_HEAD(, VLANClientState) non_vlan_clients;
|
43 | 63a01ef8 | aliguori | |
44 | cb4522cc | Gerd Hoffmann | int default_net = 1; |
45 | cb4522cc | Gerd Hoffmann | |
46 | 63a01ef8 | aliguori | /***********************************************************/
|
47 | 63a01ef8 | aliguori | /* network device redirectors */
|
48 | 63a01ef8 | aliguori | |
49 | 68ac40d2 | Mark McLoughlin | #if defined(DEBUG_NET)
|
50 | 63a01ef8 | aliguori | static void hex_dump(FILE *f, const uint8_t *buf, int size) |
51 | 63a01ef8 | aliguori | { |
52 | 63a01ef8 | aliguori | int len, i, j, c;
|
53 | 63a01ef8 | aliguori | |
54 | 63a01ef8 | aliguori | for(i=0;i<size;i+=16) { |
55 | 63a01ef8 | aliguori | len = size - i; |
56 | 63a01ef8 | aliguori | if (len > 16) |
57 | 63a01ef8 | aliguori | len = 16;
|
58 | 63a01ef8 | aliguori | fprintf(f, "%08x ", i);
|
59 | 63a01ef8 | aliguori | for(j=0;j<16;j++) { |
60 | 63a01ef8 | aliguori | if (j < len)
|
61 | 63a01ef8 | aliguori | fprintf(f, " %02x", buf[i+j]);
|
62 | 63a01ef8 | aliguori | else
|
63 | 63a01ef8 | aliguori | fprintf(f, " ");
|
64 | 63a01ef8 | aliguori | } |
65 | 63a01ef8 | aliguori | fprintf(f, " ");
|
66 | 63a01ef8 | aliguori | for(j=0;j<len;j++) { |
67 | 63a01ef8 | aliguori | c = buf[i+j]; |
68 | 63a01ef8 | aliguori | if (c < ' ' || c > '~') |
69 | 63a01ef8 | aliguori | c = '.';
|
70 | 63a01ef8 | aliguori | fprintf(f, "%c", c);
|
71 | 63a01ef8 | aliguori | } |
72 | 63a01ef8 | aliguori | fprintf(f, "\n");
|
73 | 63a01ef8 | aliguori | } |
74 | 63a01ef8 | aliguori | } |
75 | 63a01ef8 | aliguori | #endif
|
76 | 63a01ef8 | aliguori | |
77 | 63a01ef8 | aliguori | static int get_str_sep(char *buf, int buf_size, const char **pp, int sep) |
78 | 63a01ef8 | aliguori | { |
79 | 63a01ef8 | aliguori | const char *p, *p1; |
80 | 63a01ef8 | aliguori | int len;
|
81 | 63a01ef8 | aliguori | p = *pp; |
82 | 63a01ef8 | aliguori | p1 = strchr(p, sep); |
83 | 63a01ef8 | aliguori | if (!p1)
|
84 | 63a01ef8 | aliguori | return -1; |
85 | 63a01ef8 | aliguori | len = p1 - p; |
86 | 63a01ef8 | aliguori | p1++; |
87 | 63a01ef8 | aliguori | if (buf_size > 0) { |
88 | 63a01ef8 | aliguori | if (len > buf_size - 1) |
89 | 63a01ef8 | aliguori | len = buf_size - 1;
|
90 | 63a01ef8 | aliguori | memcpy(buf, p, len); |
91 | 63a01ef8 | aliguori | buf[len] = '\0';
|
92 | 63a01ef8 | aliguori | } |
93 | 63a01ef8 | aliguori | *pp = p1; |
94 | 63a01ef8 | aliguori | return 0; |
95 | 63a01ef8 | aliguori | } |
96 | 63a01ef8 | aliguori | |
97 | 63a01ef8 | aliguori | int parse_host_port(struct sockaddr_in *saddr, const char *str) |
98 | 63a01ef8 | aliguori | { |
99 | 63a01ef8 | aliguori | char buf[512]; |
100 | 63a01ef8 | aliguori | struct hostent *he;
|
101 | 63a01ef8 | aliguori | const char *p, *r; |
102 | 63a01ef8 | aliguori | int port;
|
103 | 63a01ef8 | aliguori | |
104 | 63a01ef8 | aliguori | p = str; |
105 | 63a01ef8 | aliguori | if (get_str_sep(buf, sizeof(buf), &p, ':') < 0) |
106 | 63a01ef8 | aliguori | return -1; |
107 | 63a01ef8 | aliguori | saddr->sin_family = AF_INET; |
108 | 63a01ef8 | aliguori | if (buf[0] == '\0') { |
109 | 63a01ef8 | aliguori | saddr->sin_addr.s_addr = 0;
|
110 | 63a01ef8 | aliguori | } else {
|
111 | cd390083 | blueswir1 | if (qemu_isdigit(buf[0])) { |
112 | 63a01ef8 | aliguori | if (!inet_aton(buf, &saddr->sin_addr))
|
113 | 63a01ef8 | aliguori | return -1; |
114 | 63a01ef8 | aliguori | } else {
|
115 | 63a01ef8 | aliguori | if ((he = gethostbyname(buf)) == NULL) |
116 | 63a01ef8 | aliguori | return - 1; |
117 | 63a01ef8 | aliguori | saddr->sin_addr = *(struct in_addr *)he->h_addr;
|
118 | 63a01ef8 | aliguori | } |
119 | 63a01ef8 | aliguori | } |
120 | 63a01ef8 | aliguori | port = strtol(p, (char **)&r, 0); |
121 | 63a01ef8 | aliguori | if (r == p)
|
122 | 63a01ef8 | aliguori | return -1; |
123 | 63a01ef8 | aliguori | saddr->sin_port = htons(port); |
124 | 63a01ef8 | aliguori | return 0; |
125 | 63a01ef8 | aliguori | } |
126 | 63a01ef8 | aliguori | |
127 | 7cb7434b | aliguori | void qemu_format_nic_info_str(VLANClientState *vc, uint8_t macaddr[6]) |
128 | 7cb7434b | aliguori | { |
129 | 7cb7434b | aliguori | snprintf(vc->info_str, sizeof(vc->info_str),
|
130 | 4dda4063 | aliguori | "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
|
131 | 4dda4063 | aliguori | vc->model, |
132 | 7cb7434b | aliguori | macaddr[0], macaddr[1], macaddr[2], |
133 | 7cb7434b | aliguori | macaddr[3], macaddr[4], macaddr[5]); |
134 | 7cb7434b | aliguori | } |
135 | 7cb7434b | aliguori | |
136 | 76d32cba | Gerd Hoffmann | void qemu_macaddr_default_if_unset(MACAddr *macaddr)
|
137 | 76d32cba | Gerd Hoffmann | { |
138 | 76d32cba | Gerd Hoffmann | static int index = 0; |
139 | 76d32cba | Gerd Hoffmann | static const MACAddr zero = { .a = { 0,0,0,0,0,0 } }; |
140 | 76d32cba | Gerd Hoffmann | |
141 | 76d32cba | Gerd Hoffmann | if (memcmp(macaddr, &zero, sizeof(zero)) != 0) |
142 | 76d32cba | Gerd Hoffmann | return;
|
143 | 76d32cba | Gerd Hoffmann | macaddr->a[0] = 0x52; |
144 | 76d32cba | Gerd Hoffmann | macaddr->a[1] = 0x54; |
145 | 76d32cba | Gerd Hoffmann | macaddr->a[2] = 0x00; |
146 | 76d32cba | Gerd Hoffmann | macaddr->a[3] = 0x12; |
147 | 76d32cba | Gerd Hoffmann | macaddr->a[4] = 0x34; |
148 | 76d32cba | Gerd Hoffmann | macaddr->a[5] = 0x56 + index++; |
149 | 76d32cba | Gerd Hoffmann | } |
150 | 76d32cba | Gerd Hoffmann | |
151 | 676cff29 | aliguori | static char *assign_name(VLANClientState *vc1, const char *model) |
152 | 676cff29 | aliguori | { |
153 | 676cff29 | aliguori | VLANState *vlan; |
154 | 676cff29 | aliguori | char buf[256]; |
155 | 676cff29 | aliguori | int id = 0; |
156 | 676cff29 | aliguori | |
157 | 5610c3aa | Mark McLoughlin | QTAILQ_FOREACH(vlan, &vlans, next) { |
158 | 676cff29 | aliguori | VLANClientState *vc; |
159 | 676cff29 | aliguori | |
160 | 5610c3aa | Mark McLoughlin | QTAILQ_FOREACH(vc, &vlan->clients, next) { |
161 | 5610c3aa | Mark McLoughlin | if (vc != vc1 && strcmp(vc->model, model) == 0) { |
162 | 676cff29 | aliguori | id++; |
163 | 5610c3aa | Mark McLoughlin | } |
164 | 5610c3aa | Mark McLoughlin | } |
165 | 676cff29 | aliguori | } |
166 | 676cff29 | aliguori | |
167 | 676cff29 | aliguori | snprintf(buf, sizeof(buf), "%s.%d", model, id); |
168 | 676cff29 | aliguori | |
169 | 02374aa0 | Mark McLoughlin | return qemu_strdup(buf);
|
170 | 676cff29 | aliguori | } |
171 | 676cff29 | aliguori | |
172 | 9a6ecb30 | Mark McLoughlin | static ssize_t qemu_deliver_packet(VLANClientState *sender,
|
173 | c0b8e49c | Mark McLoughlin | unsigned flags,
|
174 | 9a6ecb30 | Mark McLoughlin | const uint8_t *data,
|
175 | 9a6ecb30 | Mark McLoughlin | size_t size, |
176 | 9a6ecb30 | Mark McLoughlin | void *opaque);
|
177 | 9a6ecb30 | Mark McLoughlin | static ssize_t qemu_deliver_packet_iov(VLANClientState *sender,
|
178 | c0b8e49c | Mark McLoughlin | unsigned flags,
|
179 | 9a6ecb30 | Mark McLoughlin | const struct iovec *iov, |
180 | 9a6ecb30 | Mark McLoughlin | int iovcnt,
|
181 | 9a6ecb30 | Mark McLoughlin | void *opaque);
|
182 | 9a6ecb30 | Mark McLoughlin | |
183 | 45460d1a | Mark McLoughlin | VLANClientState *qemu_new_net_client(NetClientInfo *info, |
184 | 45460d1a | Mark McLoughlin | VLANState *vlan, |
185 | 45460d1a | Mark McLoughlin | VLANClientState *peer, |
186 | 45460d1a | Mark McLoughlin | const char *model, |
187 | 45460d1a | Mark McLoughlin | const char *name) |
188 | 63a01ef8 | aliguori | { |
189 | 5610c3aa | Mark McLoughlin | VLANClientState *vc; |
190 | 5610c3aa | Mark McLoughlin | |
191 | 45460d1a | Mark McLoughlin | assert(info->size >= sizeof(VLANClientState));
|
192 | 45460d1a | Mark McLoughlin | |
193 | 45460d1a | Mark McLoughlin | vc = qemu_mallocz(info->size); |
194 | 5610c3aa | Mark McLoughlin | |
195 | 665a3b07 | Mark McLoughlin | vc->info = info; |
196 | 02374aa0 | Mark McLoughlin | vc->model = qemu_strdup(model); |
197 | 45460d1a | Mark McLoughlin | if (name) {
|
198 | 02374aa0 | Mark McLoughlin | vc->name = qemu_strdup(name); |
199 | 45460d1a | Mark McLoughlin | } else {
|
200 | 7a9f6e4a | aliguori | vc->name = assign_name(vc, model); |
201 | 45460d1a | Mark McLoughlin | } |
202 | 5610c3aa | Mark McLoughlin | |
203 | d80b9fc6 | Mark McLoughlin | if (vlan) {
|
204 | 283c7c63 | Mark McLoughlin | assert(!peer); |
205 | d80b9fc6 | Mark McLoughlin | vc->vlan = vlan; |
206 | d80b9fc6 | Mark McLoughlin | QTAILQ_INSERT_TAIL(&vc->vlan->clients, vc, next); |
207 | 577c4af9 | Mark McLoughlin | } else {
|
208 | 283c7c63 | Mark McLoughlin | if (peer) {
|
209 | 27f3f8a3 | Markus Armbruster | assert(!peer->peer); |
210 | 283c7c63 | Mark McLoughlin | vc->peer = peer; |
211 | 283c7c63 | Mark McLoughlin | peer->peer = vc; |
212 | 283c7c63 | Mark McLoughlin | } |
213 | 577c4af9 | Mark McLoughlin | QTAILQ_INSERT_TAIL(&non_vlan_clients, vc, next); |
214 | 9a6ecb30 | Mark McLoughlin | |
215 | 9a6ecb30 | Mark McLoughlin | vc->send_queue = qemu_new_net_queue(qemu_deliver_packet, |
216 | 9a6ecb30 | Mark McLoughlin | qemu_deliver_packet_iov, |
217 | 9a6ecb30 | Mark McLoughlin | vc); |
218 | d80b9fc6 | Mark McLoughlin | } |
219 | 63a01ef8 | aliguori | |
220 | 63a01ef8 | aliguori | return vc;
|
221 | 63a01ef8 | aliguori | } |
222 | 63a01ef8 | aliguori | |
223 | ebef2c09 | Mark McLoughlin | NICState *qemu_new_nic(NetClientInfo *info, |
224 | ebef2c09 | Mark McLoughlin | NICConf *conf, |
225 | ebef2c09 | Mark McLoughlin | const char *model, |
226 | ebef2c09 | Mark McLoughlin | const char *name, |
227 | ebef2c09 | Mark McLoughlin | void *opaque)
|
228 | ebef2c09 | Mark McLoughlin | { |
229 | ebef2c09 | Mark McLoughlin | VLANClientState *nc; |
230 | ebef2c09 | Mark McLoughlin | NICState *nic; |
231 | ebef2c09 | Mark McLoughlin | |
232 | ebef2c09 | Mark McLoughlin | assert(info->type == NET_CLIENT_TYPE_NIC); |
233 | ebef2c09 | Mark McLoughlin | assert(info->size >= sizeof(NICState));
|
234 | ebef2c09 | Mark McLoughlin | |
235 | ebef2c09 | Mark McLoughlin | nc = qemu_new_net_client(info, conf->vlan, conf->peer, model, name); |
236 | ebef2c09 | Mark McLoughlin | |
237 | ebef2c09 | Mark McLoughlin | nic = DO_UPCAST(NICState, nc, nc); |
238 | ebef2c09 | Mark McLoughlin | nic->conf = conf; |
239 | ebef2c09 | Mark McLoughlin | nic->opaque = opaque; |
240 | ebef2c09 | Mark McLoughlin | |
241 | ebef2c09 | Mark McLoughlin | return nic;
|
242 | ebef2c09 | Mark McLoughlin | } |
243 | ebef2c09 | Mark McLoughlin | |
244 | a083a89d | Michael S. Tsirkin | static void qemu_cleanup_vlan_client(VLANClientState *vc) |
245 | 63a01ef8 | aliguori | { |
246 | d80b9fc6 | Mark McLoughlin | if (vc->vlan) {
|
247 | d80b9fc6 | Mark McLoughlin | QTAILQ_REMOVE(&vc->vlan->clients, vc, next); |
248 | 577c4af9 | Mark McLoughlin | } else {
|
249 | 577c4af9 | Mark McLoughlin | QTAILQ_REMOVE(&non_vlan_clients, vc, next); |
250 | d80b9fc6 | Mark McLoughlin | } |
251 | 63a01ef8 | aliguori | |
252 | 665a3b07 | Mark McLoughlin | if (vc->info->cleanup) {
|
253 | 665a3b07 | Mark McLoughlin | vc->info->cleanup(vc); |
254 | 5610c3aa | Mark McLoughlin | } |
255 | a083a89d | Michael S. Tsirkin | } |
256 | 5610c3aa | Mark McLoughlin | |
257 | a083a89d | Michael S. Tsirkin | static void qemu_free_vlan_client(VLANClientState *vc) |
258 | a083a89d | Michael S. Tsirkin | { |
259 | a083a89d | Michael S. Tsirkin | if (!vc->vlan) {
|
260 | a083a89d | Michael S. Tsirkin | if (vc->send_queue) {
|
261 | a083a89d | Michael S. Tsirkin | qemu_del_net_queue(vc->send_queue); |
262 | a083a89d | Michael S. Tsirkin | } |
263 | a083a89d | Michael S. Tsirkin | if (vc->peer) {
|
264 | a083a89d | Michael S. Tsirkin | vc->peer->peer = NULL;
|
265 | a083a89d | Michael S. Tsirkin | } |
266 | a083a89d | Michael S. Tsirkin | } |
267 | 5610c3aa | Mark McLoughlin | qemu_free(vc->name); |
268 | 5610c3aa | Mark McLoughlin | qemu_free(vc->model); |
269 | 5610c3aa | Mark McLoughlin | qemu_free(vc); |
270 | 63a01ef8 | aliguori | } |
271 | 63a01ef8 | aliguori | |
272 | a083a89d | Michael S. Tsirkin | void qemu_del_vlan_client(VLANClientState *vc)
|
273 | a083a89d | Michael S. Tsirkin | { |
274 | a083a89d | Michael S. Tsirkin | /* If there is a peer NIC, delete and cleanup client, but do not free. */
|
275 | a083a89d | Michael S. Tsirkin | if (!vc->vlan && vc->peer && vc->peer->info->type == NET_CLIENT_TYPE_NIC) {
|
276 | a083a89d | Michael S. Tsirkin | NICState *nic = DO_UPCAST(NICState, nc, vc->peer); |
277 | a083a89d | Michael S. Tsirkin | if (nic->peer_deleted) {
|
278 | a083a89d | Michael S. Tsirkin | return;
|
279 | a083a89d | Michael S. Tsirkin | } |
280 | a083a89d | Michael S. Tsirkin | nic->peer_deleted = true;
|
281 | a083a89d | Michael S. Tsirkin | /* Let NIC know peer is gone. */
|
282 | a083a89d | Michael S. Tsirkin | vc->peer->link_down = true;
|
283 | a083a89d | Michael S. Tsirkin | if (vc->peer->info->link_status_changed) {
|
284 | a083a89d | Michael S. Tsirkin | vc->peer->info->link_status_changed(vc->peer); |
285 | a083a89d | Michael S. Tsirkin | } |
286 | a083a89d | Michael S. Tsirkin | qemu_cleanup_vlan_client(vc); |
287 | a083a89d | Michael S. Tsirkin | return;
|
288 | a083a89d | Michael S. Tsirkin | } |
289 | a083a89d | Michael S. Tsirkin | |
290 | a083a89d | Michael S. Tsirkin | /* If this is a peer NIC and peer has already been deleted, free it now. */
|
291 | a083a89d | Michael S. Tsirkin | if (!vc->vlan && vc->peer && vc->info->type == NET_CLIENT_TYPE_NIC) {
|
292 | a083a89d | Michael S. Tsirkin | NICState *nic = DO_UPCAST(NICState, nc, vc); |
293 | a083a89d | Michael S. Tsirkin | if (nic->peer_deleted) {
|
294 | a083a89d | Michael S. Tsirkin | qemu_free_vlan_client(vc->peer); |
295 | a083a89d | Michael S. Tsirkin | } |
296 | a083a89d | Michael S. Tsirkin | } |
297 | a083a89d | Michael S. Tsirkin | |
298 | a083a89d | Michael S. Tsirkin | qemu_cleanup_vlan_client(vc); |
299 | a083a89d | Michael S. Tsirkin | qemu_free_vlan_client(vc); |
300 | a083a89d | Michael S. Tsirkin | } |
301 | a083a89d | Michael S. Tsirkin | |
302 | 68ac40d2 | Mark McLoughlin | VLANClientState * |
303 | 1a609520 | Jan Kiszka | qemu_find_vlan_client_by_name(Monitor *mon, int vlan_id,
|
304 | 1a609520 | Jan Kiszka | const char *client_str) |
305 | 1a609520 | Jan Kiszka | { |
306 | 1a609520 | Jan Kiszka | VLANState *vlan; |
307 | 1a609520 | Jan Kiszka | VLANClientState *vc; |
308 | 1a609520 | Jan Kiszka | |
309 | 1a609520 | Jan Kiszka | vlan = qemu_find_vlan(vlan_id, 0);
|
310 | 1a609520 | Jan Kiszka | if (!vlan) {
|
311 | 1a609520 | Jan Kiszka | monitor_printf(mon, "unknown VLAN %d\n", vlan_id);
|
312 | 1a609520 | Jan Kiszka | return NULL; |
313 | 1a609520 | Jan Kiszka | } |
314 | 1a609520 | Jan Kiszka | |
315 | 5610c3aa | Mark McLoughlin | QTAILQ_FOREACH(vc, &vlan->clients, next) { |
316 | 1a609520 | Jan Kiszka | if (!strcmp(vc->name, client_str)) {
|
317 | 1a609520 | Jan Kiszka | break;
|
318 | 1a609520 | Jan Kiszka | } |
319 | 1a609520 | Jan Kiszka | } |
320 | 1a609520 | Jan Kiszka | if (!vc) {
|
321 | 1a609520 | Jan Kiszka | monitor_printf(mon, "can't find device %s on VLAN %d\n",
|
322 | 1a609520 | Jan Kiszka | client_str, vlan_id); |
323 | 1a609520 | Jan Kiszka | } |
324 | 1a609520 | Jan Kiszka | |
325 | 1a609520 | Jan Kiszka | return vc;
|
326 | 1a609520 | Jan Kiszka | } |
327 | 1a609520 | Jan Kiszka | |
328 | 57f9ef17 | Mark McLoughlin | void qemu_foreach_nic(qemu_nic_foreach func, void *opaque) |
329 | 57f9ef17 | Mark McLoughlin | { |
330 | 57f9ef17 | Mark McLoughlin | VLANClientState *nc; |
331 | 57f9ef17 | Mark McLoughlin | VLANState *vlan; |
332 | 57f9ef17 | Mark McLoughlin | |
333 | 57f9ef17 | Mark McLoughlin | QTAILQ_FOREACH(nc, &non_vlan_clients, next) { |
334 | 57f9ef17 | Mark McLoughlin | if (nc->info->type == NET_CLIENT_TYPE_NIC) {
|
335 | 57f9ef17 | Mark McLoughlin | func(DO_UPCAST(NICState, nc, nc), opaque); |
336 | 57f9ef17 | Mark McLoughlin | } |
337 | 57f9ef17 | Mark McLoughlin | } |
338 | 57f9ef17 | Mark McLoughlin | |
339 | 57f9ef17 | Mark McLoughlin | QTAILQ_FOREACH(vlan, &vlans, next) { |
340 | 57f9ef17 | Mark McLoughlin | QTAILQ_FOREACH(nc, &vlan->clients, next) { |
341 | 57f9ef17 | Mark McLoughlin | if (nc->info->type == NET_CLIENT_TYPE_NIC) {
|
342 | 57f9ef17 | Mark McLoughlin | func(DO_UPCAST(NICState, nc, nc), opaque); |
343 | 57f9ef17 | Mark McLoughlin | } |
344 | 57f9ef17 | Mark McLoughlin | } |
345 | 57f9ef17 | Mark McLoughlin | } |
346 | 57f9ef17 | Mark McLoughlin | } |
347 | 57f9ef17 | Mark McLoughlin | |
348 | 2e1e0641 | Mark McLoughlin | int qemu_can_send_packet(VLANClientState *sender)
|
349 | 63a01ef8 | aliguori | { |
350 | 2e1e0641 | Mark McLoughlin | VLANState *vlan = sender->vlan; |
351 | 63a01ef8 | aliguori | VLANClientState *vc; |
352 | 63a01ef8 | aliguori | |
353 | 9a6ecb30 | Mark McLoughlin | if (sender->peer) {
|
354 | 893379ef | Mark McLoughlin | if (sender->peer->receive_disabled) {
|
355 | 893379ef | Mark McLoughlin | return 0; |
356 | 665a3b07 | Mark McLoughlin | } else if (sender->peer->info->can_receive && |
357 | 665a3b07 | Mark McLoughlin | !sender->peer->info->can_receive(sender->peer)) { |
358 | 9a6ecb30 | Mark McLoughlin | return 0; |
359 | 893379ef | Mark McLoughlin | } else {
|
360 | 893379ef | Mark McLoughlin | return 1; |
361 | 9a6ecb30 | Mark McLoughlin | } |
362 | 9a6ecb30 | Mark McLoughlin | } |
363 | 9a6ecb30 | Mark McLoughlin | |
364 | d80b9fc6 | Mark McLoughlin | if (!sender->vlan) {
|
365 | d80b9fc6 | Mark McLoughlin | return 1; |
366 | d80b9fc6 | Mark McLoughlin | } |
367 | d80b9fc6 | Mark McLoughlin | |
368 | 5610c3aa | Mark McLoughlin | QTAILQ_FOREACH(vc, &vlan->clients, next) { |
369 | 2e1e0641 | Mark McLoughlin | if (vc == sender) {
|
370 | 2e1e0641 | Mark McLoughlin | continue;
|
371 | 2e1e0641 | Mark McLoughlin | } |
372 | 2e1e0641 | Mark McLoughlin | |
373 | cda9046b | Mark McLoughlin | /* no can_receive() handler, they can always receive */
|
374 | 60c07d93 | Vincent Palatin | if (vc->info->can_receive && !vc->info->can_receive(vc)) {
|
375 | 60c07d93 | Vincent Palatin | return 0; |
376 | 63a01ef8 | aliguori | } |
377 | 63a01ef8 | aliguori | } |
378 | 60c07d93 | Vincent Palatin | return 1; |
379 | 63a01ef8 | aliguori | } |
380 | 63a01ef8 | aliguori | |
381 | 9a6ecb30 | Mark McLoughlin | static ssize_t qemu_deliver_packet(VLANClientState *sender,
|
382 | c0b8e49c | Mark McLoughlin | unsigned flags,
|
383 | 9a6ecb30 | Mark McLoughlin | const uint8_t *data,
|
384 | 9a6ecb30 | Mark McLoughlin | size_t size, |
385 | 9a6ecb30 | Mark McLoughlin | void *opaque)
|
386 | 9a6ecb30 | Mark McLoughlin | { |
387 | 9a6ecb30 | Mark McLoughlin | VLANClientState *vc = opaque; |
388 | 893379ef | Mark McLoughlin | ssize_t ret; |
389 | 9a6ecb30 | Mark McLoughlin | |
390 | 9a6ecb30 | Mark McLoughlin | if (vc->link_down) {
|
391 | 9a6ecb30 | Mark McLoughlin | return size;
|
392 | 9a6ecb30 | Mark McLoughlin | } |
393 | 9a6ecb30 | Mark McLoughlin | |
394 | 893379ef | Mark McLoughlin | if (vc->receive_disabled) {
|
395 | 893379ef | Mark McLoughlin | return 0; |
396 | 893379ef | Mark McLoughlin | } |
397 | 893379ef | Mark McLoughlin | |
398 | 665a3b07 | Mark McLoughlin | if (flags & QEMU_NET_PACKET_FLAG_RAW && vc->info->receive_raw) {
|
399 | 665a3b07 | Mark McLoughlin | ret = vc->info->receive_raw(vc, data, size); |
400 | 893379ef | Mark McLoughlin | } else {
|
401 | 665a3b07 | Mark McLoughlin | ret = vc->info->receive(vc, data, size); |
402 | 893379ef | Mark McLoughlin | } |
403 | 893379ef | Mark McLoughlin | |
404 | 893379ef | Mark McLoughlin | if (ret == 0) { |
405 | 893379ef | Mark McLoughlin | vc->receive_disabled = 1;
|
406 | 893379ef | Mark McLoughlin | }; |
407 | 893379ef | Mark McLoughlin | |
408 | 893379ef | Mark McLoughlin | return ret;
|
409 | 9a6ecb30 | Mark McLoughlin | } |
410 | 9a6ecb30 | Mark McLoughlin | |
411 | f7105843 | Mark McLoughlin | static ssize_t qemu_vlan_deliver_packet(VLANClientState *sender,
|
412 | c0b8e49c | Mark McLoughlin | unsigned flags,
|
413 | f7105843 | Mark McLoughlin | const uint8_t *buf,
|
414 | f7105843 | Mark McLoughlin | size_t size, |
415 | f7105843 | Mark McLoughlin | void *opaque)
|
416 | 63a01ef8 | aliguori | { |
417 | f7105843 | Mark McLoughlin | VLANState *vlan = opaque; |
418 | 63a01ef8 | aliguori | VLANClientState *vc; |
419 | 893379ef | Mark McLoughlin | ssize_t ret = -1;
|
420 | 63a01ef8 | aliguori | |
421 | f7105843 | Mark McLoughlin | QTAILQ_FOREACH(vc, &vlan->clients, next) { |
422 | 3e021d40 | Mark McLoughlin | ssize_t len; |
423 | 3e021d40 | Mark McLoughlin | |
424 | 3e021d40 | Mark McLoughlin | if (vc == sender) {
|
425 | 3e021d40 | Mark McLoughlin | continue;
|
426 | 764a4d1d | aliguori | } |
427 | 3e021d40 | Mark McLoughlin | |
428 | 3e021d40 | Mark McLoughlin | if (vc->link_down) {
|
429 | 3e021d40 | Mark McLoughlin | ret = size; |
430 | 3e021d40 | Mark McLoughlin | continue;
|
431 | 3e021d40 | Mark McLoughlin | } |
432 | 3e021d40 | Mark McLoughlin | |
433 | 893379ef | Mark McLoughlin | if (vc->receive_disabled) {
|
434 | 893379ef | Mark McLoughlin | ret = 0;
|
435 | 893379ef | Mark McLoughlin | continue;
|
436 | 893379ef | Mark McLoughlin | } |
437 | 893379ef | Mark McLoughlin | |
438 | 665a3b07 | Mark McLoughlin | if (flags & QEMU_NET_PACKET_FLAG_RAW && vc->info->receive_raw) {
|
439 | 665a3b07 | Mark McLoughlin | len = vc->info->receive_raw(vc, buf, size); |
440 | 893379ef | Mark McLoughlin | } else {
|
441 | 665a3b07 | Mark McLoughlin | len = vc->info->receive(vc, buf, size); |
442 | 893379ef | Mark McLoughlin | } |
443 | 893379ef | Mark McLoughlin | |
444 | 893379ef | Mark McLoughlin | if (len == 0) { |
445 | 893379ef | Mark McLoughlin | vc->receive_disabled = 1;
|
446 | 893379ef | Mark McLoughlin | } |
447 | 3e021d40 | Mark McLoughlin | |
448 | 3e021d40 | Mark McLoughlin | ret = (ret >= 0) ? ret : len;
|
449 | 893379ef | Mark McLoughlin | |
450 | 764a4d1d | aliguori | } |
451 | 3e021d40 | Mark McLoughlin | |
452 | 3e021d40 | Mark McLoughlin | return ret;
|
453 | 764a4d1d | aliguori | } |
454 | 764a4d1d | aliguori | |
455 | 8cad5516 | Mark McLoughlin | void qemu_purge_queued_packets(VLANClientState *vc)
|
456 | 8cad5516 | Mark McLoughlin | { |
457 | 9a6ecb30 | Mark McLoughlin | NetQueue *queue; |
458 | 9a6ecb30 | Mark McLoughlin | |
459 | 9a6ecb30 | Mark McLoughlin | if (!vc->peer && !vc->vlan) {
|
460 | d80b9fc6 | Mark McLoughlin | return;
|
461 | 9a6ecb30 | Mark McLoughlin | } |
462 | d80b9fc6 | Mark McLoughlin | |
463 | 9a6ecb30 | Mark McLoughlin | if (vc->peer) {
|
464 | 9a6ecb30 | Mark McLoughlin | queue = vc->peer->send_queue; |
465 | 9a6ecb30 | Mark McLoughlin | } else {
|
466 | 9a6ecb30 | Mark McLoughlin | queue = vc->vlan->send_queue; |
467 | 9a6ecb30 | Mark McLoughlin | } |
468 | 9a6ecb30 | Mark McLoughlin | |
469 | 9a6ecb30 | Mark McLoughlin | qemu_net_queue_purge(queue, vc); |
470 | 8cad5516 | Mark McLoughlin | } |
471 | 8cad5516 | Mark McLoughlin | |
472 | f3b6c7fc | Mark McLoughlin | void qemu_flush_queued_packets(VLANClientState *vc)
|
473 | e94667b9 | Mark McLoughlin | { |
474 | 9a6ecb30 | Mark McLoughlin | NetQueue *queue; |
475 | 9a6ecb30 | Mark McLoughlin | |
476 | 893379ef | Mark McLoughlin | vc->receive_disabled = 0;
|
477 | 893379ef | Mark McLoughlin | |
478 | 9a6ecb30 | Mark McLoughlin | if (vc->vlan) {
|
479 | 9a6ecb30 | Mark McLoughlin | queue = vc->vlan->send_queue; |
480 | 9a6ecb30 | Mark McLoughlin | } else {
|
481 | 9a6ecb30 | Mark McLoughlin | queue = vc->send_queue; |
482 | 9a6ecb30 | Mark McLoughlin | } |
483 | d80b9fc6 | Mark McLoughlin | |
484 | 9a6ecb30 | Mark McLoughlin | qemu_net_queue_flush(queue); |
485 | e94667b9 | Mark McLoughlin | } |
486 | e94667b9 | Mark McLoughlin | |
487 | ca77d175 | Mark McLoughlin | static ssize_t qemu_send_packet_async_with_flags(VLANClientState *sender,
|
488 | ca77d175 | Mark McLoughlin | unsigned flags,
|
489 | ca77d175 | Mark McLoughlin | const uint8_t *buf, int size, |
490 | ca77d175 | Mark McLoughlin | NetPacketSent *sent_cb) |
491 | 764a4d1d | aliguori | { |
492 | 9a6ecb30 | Mark McLoughlin | NetQueue *queue; |
493 | 436e5e53 | aliguori | |
494 | 63a01ef8 | aliguori | #ifdef DEBUG_NET
|
495 | d80b9fc6 | Mark McLoughlin | printf("qemu_send_packet_async:\n");
|
496 | 63a01ef8 | aliguori | hex_dump(stdout, buf, size); |
497 | 63a01ef8 | aliguori | #endif
|
498 | f3b6c7fc | Mark McLoughlin | |
499 | 9a6ecb30 | Mark McLoughlin | if (sender->link_down || (!sender->peer && !sender->vlan)) {
|
500 | 9a6ecb30 | Mark McLoughlin | return size;
|
501 | 9a6ecb30 | Mark McLoughlin | } |
502 | 9a6ecb30 | Mark McLoughlin | |
503 | 9a6ecb30 | Mark McLoughlin | if (sender->peer) {
|
504 | 9a6ecb30 | Mark McLoughlin | queue = sender->peer->send_queue; |
505 | 9a6ecb30 | Mark McLoughlin | } else {
|
506 | 9a6ecb30 | Mark McLoughlin | queue = sender->vlan->send_queue; |
507 | 9a6ecb30 | Mark McLoughlin | } |
508 | 9a6ecb30 | Mark McLoughlin | |
509 | ca77d175 | Mark McLoughlin | return qemu_net_queue_send(queue, sender, flags, buf, size, sent_cb);
|
510 | ca77d175 | Mark McLoughlin | } |
511 | ca77d175 | Mark McLoughlin | |
512 | ca77d175 | Mark McLoughlin | ssize_t qemu_send_packet_async(VLANClientState *sender, |
513 | ca77d175 | Mark McLoughlin | const uint8_t *buf, int size, |
514 | ca77d175 | Mark McLoughlin | NetPacketSent *sent_cb) |
515 | ca77d175 | Mark McLoughlin | { |
516 | ca77d175 | Mark McLoughlin | return qemu_send_packet_async_with_flags(sender, QEMU_NET_PACKET_FLAG_NONE,
|
517 | ca77d175 | Mark McLoughlin | buf, size, sent_cb); |
518 | f3b6c7fc | Mark McLoughlin | } |
519 | f3b6c7fc | Mark McLoughlin | |
520 | f3b6c7fc | Mark McLoughlin | void qemu_send_packet(VLANClientState *vc, const uint8_t *buf, int size) |
521 | f3b6c7fc | Mark McLoughlin | { |
522 | f3b6c7fc | Mark McLoughlin | qemu_send_packet_async(vc, buf, size, NULL);
|
523 | 63a01ef8 | aliguori | } |
524 | 63a01ef8 | aliguori | |
525 | ca77d175 | Mark McLoughlin | ssize_t qemu_send_packet_raw(VLANClientState *vc, const uint8_t *buf, int size) |
526 | ca77d175 | Mark McLoughlin | { |
527 | ca77d175 | Mark McLoughlin | return qemu_send_packet_async_with_flags(vc, QEMU_NET_PACKET_FLAG_RAW,
|
528 | ca77d175 | Mark McLoughlin | buf, size, NULL);
|
529 | ca77d175 | Mark McLoughlin | } |
530 | ca77d175 | Mark McLoughlin | |
531 | fbe78f4f | aliguori | static ssize_t vc_sendv_compat(VLANClientState *vc, const struct iovec *iov, |
532 | fbe78f4f | aliguori | int iovcnt)
|
533 | fbe78f4f | aliguori | { |
534 | fbe78f4f | aliguori | uint8_t buffer[4096];
|
535 | ce053661 | Benjamin Poirier | size_t offset; |
536 | fbe78f4f | aliguori | |
537 | ce053661 | Benjamin Poirier | offset = iov_to_buf(iov, iovcnt, buffer, 0, sizeof(buffer)); |
538 | fbe78f4f | aliguori | |
539 | 665a3b07 | Mark McLoughlin | return vc->info->receive(vc, buffer, offset);
|
540 | fbe78f4f | aliguori | } |
541 | fbe78f4f | aliguori | |
542 | 9a6ecb30 | Mark McLoughlin | static ssize_t qemu_deliver_packet_iov(VLANClientState *sender,
|
543 | c0b8e49c | Mark McLoughlin | unsigned flags,
|
544 | 9a6ecb30 | Mark McLoughlin | const struct iovec *iov, |
545 | 9a6ecb30 | Mark McLoughlin | int iovcnt,
|
546 | 9a6ecb30 | Mark McLoughlin | void *opaque)
|
547 | 9a6ecb30 | Mark McLoughlin | { |
548 | 9a6ecb30 | Mark McLoughlin | VLANClientState *vc = opaque; |
549 | 9a6ecb30 | Mark McLoughlin | |
550 | 9a6ecb30 | Mark McLoughlin | if (vc->link_down) {
|
551 | ce053661 | Benjamin Poirier | return iov_size(iov, iovcnt);
|
552 | 9a6ecb30 | Mark McLoughlin | } |
553 | 9a6ecb30 | Mark McLoughlin | |
554 | 665a3b07 | Mark McLoughlin | if (vc->info->receive_iov) {
|
555 | 665a3b07 | Mark McLoughlin | return vc->info->receive_iov(vc, iov, iovcnt);
|
556 | 9a6ecb30 | Mark McLoughlin | } else {
|
557 | 9a6ecb30 | Mark McLoughlin | return vc_sendv_compat(vc, iov, iovcnt);
|
558 | 9a6ecb30 | Mark McLoughlin | } |
559 | 9a6ecb30 | Mark McLoughlin | } |
560 | 9a6ecb30 | Mark McLoughlin | |
561 | f7105843 | Mark McLoughlin | static ssize_t qemu_vlan_deliver_packet_iov(VLANClientState *sender,
|
562 | c0b8e49c | Mark McLoughlin | unsigned flags,
|
563 | f7105843 | Mark McLoughlin | const struct iovec *iov, |
564 | f7105843 | Mark McLoughlin | int iovcnt,
|
565 | f7105843 | Mark McLoughlin | void *opaque)
|
566 | fbe78f4f | aliguori | { |
567 | f7105843 | Mark McLoughlin | VLANState *vlan = opaque; |
568 | fbe78f4f | aliguori | VLANClientState *vc; |
569 | f7105843 | Mark McLoughlin | ssize_t ret = -1;
|
570 | e94667b9 | Mark McLoughlin | |
571 | f7105843 | Mark McLoughlin | QTAILQ_FOREACH(vc, &vlan->clients, next) { |
572 | e94667b9 | Mark McLoughlin | ssize_t len; |
573 | e94667b9 | Mark McLoughlin | |
574 | e94667b9 | Mark McLoughlin | if (vc == sender) {
|
575 | e94667b9 | Mark McLoughlin | continue;
|
576 | e94667b9 | Mark McLoughlin | } |
577 | e94667b9 | Mark McLoughlin | |
578 | e94667b9 | Mark McLoughlin | if (vc->link_down) {
|
579 | ce053661 | Benjamin Poirier | ret = iov_size(iov, iovcnt); |
580 | e94667b9 | Mark McLoughlin | continue;
|
581 | e94667b9 | Mark McLoughlin | } |
582 | e94667b9 | Mark McLoughlin | |
583 | ca77d175 | Mark McLoughlin | assert(!(flags & QEMU_NET_PACKET_FLAG_RAW)); |
584 | ca77d175 | Mark McLoughlin | |
585 | 665a3b07 | Mark McLoughlin | if (vc->info->receive_iov) {
|
586 | 665a3b07 | Mark McLoughlin | len = vc->info->receive_iov(vc, iov, iovcnt); |
587 | e94667b9 | Mark McLoughlin | } else {
|
588 | e94667b9 | Mark McLoughlin | len = vc_sendv_compat(vc, iov, iovcnt); |
589 | e94667b9 | Mark McLoughlin | } |
590 | e94667b9 | Mark McLoughlin | |
591 | e94667b9 | Mark McLoughlin | ret = (ret >= 0) ? ret : len;
|
592 | e94667b9 | Mark McLoughlin | } |
593 | e94667b9 | Mark McLoughlin | |
594 | e94667b9 | Mark McLoughlin | return ret;
|
595 | e94667b9 | Mark McLoughlin | } |
596 | e94667b9 | Mark McLoughlin | |
597 | f3b6c7fc | Mark McLoughlin | ssize_t qemu_sendv_packet_async(VLANClientState *sender, |
598 | f3b6c7fc | Mark McLoughlin | const struct iovec *iov, int iovcnt, |
599 | f3b6c7fc | Mark McLoughlin | NetPacketSent *sent_cb) |
600 | e94667b9 | Mark McLoughlin | { |
601 | 9a6ecb30 | Mark McLoughlin | NetQueue *queue; |
602 | 9a6ecb30 | Mark McLoughlin | |
603 | 9a6ecb30 | Mark McLoughlin | if (sender->link_down || (!sender->peer && !sender->vlan)) {
|
604 | ce053661 | Benjamin Poirier | return iov_size(iov, iovcnt);
|
605 | e94667b9 | Mark McLoughlin | } |
606 | e94667b9 | Mark McLoughlin | |
607 | 9a6ecb30 | Mark McLoughlin | if (sender->peer) {
|
608 | 9a6ecb30 | Mark McLoughlin | queue = sender->peer->send_queue; |
609 | 9a6ecb30 | Mark McLoughlin | } else {
|
610 | 9a6ecb30 | Mark McLoughlin | queue = sender->vlan->send_queue; |
611 | 9a6ecb30 | Mark McLoughlin | } |
612 | 9a6ecb30 | Mark McLoughlin | |
613 | c0b8e49c | Mark McLoughlin | return qemu_net_queue_send_iov(queue, sender,
|
614 | c0b8e49c | Mark McLoughlin | QEMU_NET_PACKET_FLAG_NONE, |
615 | c0b8e49c | Mark McLoughlin | iov, iovcnt, sent_cb); |
616 | fbe78f4f | aliguori | } |
617 | fbe78f4f | aliguori | |
618 | f3b6c7fc | Mark McLoughlin | ssize_t |
619 | f3b6c7fc | Mark McLoughlin | qemu_sendv_packet(VLANClientState *vc, const struct iovec *iov, int iovcnt) |
620 | f3b6c7fc | Mark McLoughlin | { |
621 | f3b6c7fc | Mark McLoughlin | return qemu_sendv_packet_async(vc, iov, iovcnt, NULL); |
622 | f3b6c7fc | Mark McLoughlin | } |
623 | f3b6c7fc | Mark McLoughlin | |
624 | 63a01ef8 | aliguori | /* find or alloc a new VLAN */
|
625 | 1a609520 | Jan Kiszka | VLANState *qemu_find_vlan(int id, int allocate) |
626 | 63a01ef8 | aliguori | { |
627 | 5610c3aa | Mark McLoughlin | VLANState *vlan; |
628 | 5610c3aa | Mark McLoughlin | |
629 | 5610c3aa | Mark McLoughlin | QTAILQ_FOREACH(vlan, &vlans, next) { |
630 | 5610c3aa | Mark McLoughlin | if (vlan->id == id) {
|
631 | 63a01ef8 | aliguori | return vlan;
|
632 | 5610c3aa | Mark McLoughlin | } |
633 | 63a01ef8 | aliguori | } |
634 | 5610c3aa | Mark McLoughlin | |
635 | 1a609520 | Jan Kiszka | if (!allocate) {
|
636 | 1a609520 | Jan Kiszka | return NULL; |
637 | 1a609520 | Jan Kiszka | } |
638 | 5610c3aa | Mark McLoughlin | |
639 | 63a01ef8 | aliguori | vlan = qemu_mallocz(sizeof(VLANState));
|
640 | 63a01ef8 | aliguori | vlan->id = id; |
641 | 5610c3aa | Mark McLoughlin | QTAILQ_INIT(&vlan->clients); |
642 | f7105843 | Mark McLoughlin | |
643 | f7105843 | Mark McLoughlin | vlan->send_queue = qemu_new_net_queue(qemu_vlan_deliver_packet, |
644 | f7105843 | Mark McLoughlin | qemu_vlan_deliver_packet_iov, |
645 | f7105843 | Mark McLoughlin | vlan); |
646 | 5610c3aa | Mark McLoughlin | |
647 | 5610c3aa | Mark McLoughlin | QTAILQ_INSERT_TAIL(&vlans, vlan, next); |
648 | 5610c3aa | Mark McLoughlin | |
649 | 63a01ef8 | aliguori | return vlan;
|
650 | 63a01ef8 | aliguori | } |
651 | 63a01ef8 | aliguori | |
652 | 2ef924b4 | Gerd Hoffmann | VLANClientState *qemu_find_netdev(const char *id) |
653 | 5869c4d5 | Mark McLoughlin | { |
654 | 5869c4d5 | Mark McLoughlin | VLANClientState *vc; |
655 | 5869c4d5 | Mark McLoughlin | |
656 | 5869c4d5 | Mark McLoughlin | QTAILQ_FOREACH(vc, &non_vlan_clients, next) { |
657 | 5869c4d5 | Mark McLoughlin | if (!strcmp(vc->name, id)) {
|
658 | 5869c4d5 | Mark McLoughlin | return vc;
|
659 | 5869c4d5 | Mark McLoughlin | } |
660 | 5869c4d5 | Mark McLoughlin | } |
661 | 5869c4d5 | Mark McLoughlin | |
662 | 5869c4d5 | Mark McLoughlin | return NULL; |
663 | 5869c4d5 | Mark McLoughlin | } |
664 | 5869c4d5 | Mark McLoughlin | |
665 | 7697079b | aliguori | static int nic_get_free_idx(void) |
666 | 7697079b | aliguori | { |
667 | 7697079b | aliguori | int index;
|
668 | 7697079b | aliguori | |
669 | 7697079b | aliguori | for (index = 0; index < MAX_NICS; index++) |
670 | 7697079b | aliguori | if (!nd_table[index].used)
|
671 | 7697079b | aliguori | return index;
|
672 | 7697079b | aliguori | return -1; |
673 | 7697079b | aliguori | } |
674 | 7697079b | aliguori | |
675 | 07caea31 | Markus Armbruster | int qemu_show_nic_models(const char *arg, const char *const *models) |
676 | 07caea31 | Markus Armbruster | { |
677 | 07caea31 | Markus Armbruster | int i;
|
678 | 07caea31 | Markus Armbruster | |
679 | 07caea31 | Markus Armbruster | if (!arg || strcmp(arg, "?")) |
680 | 07caea31 | Markus Armbruster | return 0; |
681 | 07caea31 | Markus Armbruster | |
682 | 07caea31 | Markus Armbruster | fprintf(stderr, "qemu: Supported NIC models: ");
|
683 | 07caea31 | Markus Armbruster | for (i = 0 ; models[i]; i++) |
684 | 07caea31 | Markus Armbruster | fprintf(stderr, "%s%c", models[i], models[i+1] ? ',' : '\n'); |
685 | 07caea31 | Markus Armbruster | return 1; |
686 | 07caea31 | Markus Armbruster | } |
687 | 07caea31 | Markus Armbruster | |
688 | d07f22c5 | aliguori | void qemu_check_nic_model(NICInfo *nd, const char *model) |
689 | d07f22c5 | aliguori | { |
690 | d07f22c5 | aliguori | const char *models[2]; |
691 | d07f22c5 | aliguori | |
692 | d07f22c5 | aliguori | models[0] = model;
|
693 | d07f22c5 | aliguori | models[1] = NULL; |
694 | d07f22c5 | aliguori | |
695 | 07caea31 | Markus Armbruster | if (qemu_show_nic_models(nd->model, models))
|
696 | 07caea31 | Markus Armbruster | exit(0);
|
697 | 07caea31 | Markus Armbruster | if (qemu_find_nic_model(nd, models, model) < 0) |
698 | 07caea31 | Markus Armbruster | exit(1);
|
699 | d07f22c5 | aliguori | } |
700 | d07f22c5 | aliguori | |
701 | 07caea31 | Markus Armbruster | int qemu_find_nic_model(NICInfo *nd, const char * const *models, |
702 | 07caea31 | Markus Armbruster | const char *default_model) |
703 | d07f22c5 | aliguori | { |
704 | 07caea31 | Markus Armbruster | int i;
|
705 | d07f22c5 | aliguori | |
706 | d07f22c5 | aliguori | if (!nd->model)
|
707 | 32a8e14a | Mark McLoughlin | nd->model = qemu_strdup(default_model); |
708 | d07f22c5 | aliguori | |
709 | 07caea31 | Markus Armbruster | for (i = 0 ; models[i]; i++) { |
710 | 07caea31 | Markus Armbruster | if (strcmp(nd->model, models[i]) == 0) |
711 | 07caea31 | Markus Armbruster | return i;
|
712 | d07f22c5 | aliguori | } |
713 | d07f22c5 | aliguori | |
714 | 1ecda02b | Markus Armbruster | error_report("qemu: Unsupported NIC model: %s", nd->model);
|
715 | 07caea31 | Markus Armbruster | return -1; |
716 | d07f22c5 | aliguori | } |
717 | d07f22c5 | aliguori | |
718 | 5281d757 | Mark McLoughlin | int net_handle_fd_param(Monitor *mon, const char *param) |
719 | c1d6eed7 | Mark McLoughlin | { |
720 | f7c31d63 | Jason Wang | int fd;
|
721 | f7c31d63 | Jason Wang | |
722 | f7c31d63 | Jason Wang | if (!qemu_isdigit(param[0]) && mon) { |
723 | c1d6eed7 | Mark McLoughlin | |
724 | c1d6eed7 | Mark McLoughlin | fd = monitor_get_fd(mon, param); |
725 | c1d6eed7 | Mark McLoughlin | if (fd == -1) { |
726 | 1ecda02b | Markus Armbruster | error_report("No file descriptor named %s found", param);
|
727 | c1d6eed7 | Mark McLoughlin | return -1; |
728 | c1d6eed7 | Mark McLoughlin | } |
729 | c1d6eed7 | Mark McLoughlin | } else {
|
730 | f7c31d63 | Jason Wang | char *endptr = NULL; |
731 | f7c31d63 | Jason Wang | |
732 | f7c31d63 | Jason Wang | fd = strtol(param, &endptr, 10);
|
733 | f7c31d63 | Jason Wang | if (*endptr || (fd == 0 && param == endptr)) { |
734 | f7c31d63 | Jason Wang | return -1; |
735 | f7c31d63 | Jason Wang | } |
736 | c1d6eed7 | Mark McLoughlin | } |
737 | f7c31d63 | Jason Wang | |
738 | f7c31d63 | Jason Wang | return fd;
|
739 | c1d6eed7 | Mark McLoughlin | } |
740 | c1d6eed7 | Mark McLoughlin | |
741 | f6b134ac | Mark McLoughlin | static int net_init_nic(QemuOpts *opts, |
742 | f6b134ac | Mark McLoughlin | Monitor *mon, |
743 | f6b134ac | Mark McLoughlin | const char *name, |
744 | f6b134ac | Mark McLoughlin | VLANState *vlan) |
745 | f83c6e10 | Mark McLoughlin | { |
746 | f83c6e10 | Mark McLoughlin | int idx;
|
747 | f83c6e10 | Mark McLoughlin | NICInfo *nd; |
748 | 5869c4d5 | Mark McLoughlin | const char *netdev; |
749 | f83c6e10 | Mark McLoughlin | |
750 | f83c6e10 | Mark McLoughlin | idx = nic_get_free_idx(); |
751 | f83c6e10 | Mark McLoughlin | if (idx == -1 || nb_nics >= MAX_NICS) { |
752 | 1ecda02b | Markus Armbruster | error_report("Too Many NICs");
|
753 | f83c6e10 | Mark McLoughlin | return -1; |
754 | f83c6e10 | Mark McLoughlin | } |
755 | f83c6e10 | Mark McLoughlin | |
756 | f83c6e10 | Mark McLoughlin | nd = &nd_table[idx]; |
757 | f83c6e10 | Mark McLoughlin | |
758 | f83c6e10 | Mark McLoughlin | memset(nd, 0, sizeof(*nd)); |
759 | f83c6e10 | Mark McLoughlin | |
760 | 5869c4d5 | Mark McLoughlin | if ((netdev = qemu_opt_get(opts, "netdev"))) { |
761 | 5869c4d5 | Mark McLoughlin | nd->netdev = qemu_find_netdev(netdev); |
762 | 5869c4d5 | Mark McLoughlin | if (!nd->netdev) {
|
763 | 1ecda02b | Markus Armbruster | error_report("netdev '%s' not found", netdev);
|
764 | 5869c4d5 | Mark McLoughlin | return -1; |
765 | 5869c4d5 | Mark McLoughlin | } |
766 | 5869c4d5 | Mark McLoughlin | } else {
|
767 | 5869c4d5 | Mark McLoughlin | assert(vlan); |
768 | 5869c4d5 | Mark McLoughlin | nd->vlan = vlan; |
769 | 5869c4d5 | Mark McLoughlin | } |
770 | 6d952ebe | Mark McLoughlin | if (name) {
|
771 | 6d952ebe | Mark McLoughlin | nd->name = qemu_strdup(name); |
772 | 6d952ebe | Mark McLoughlin | } |
773 | f83c6e10 | Mark McLoughlin | if (qemu_opt_get(opts, "model")) { |
774 | f83c6e10 | Mark McLoughlin | nd->model = qemu_strdup(qemu_opt_get(opts, "model"));
|
775 | f83c6e10 | Mark McLoughlin | } |
776 | f83c6e10 | Mark McLoughlin | if (qemu_opt_get(opts, "addr")) { |
777 | f83c6e10 | Mark McLoughlin | nd->devaddr = qemu_strdup(qemu_opt_get(opts, "addr"));
|
778 | f83c6e10 | Mark McLoughlin | } |
779 | f83c6e10 | Mark McLoughlin | |
780 | f83c6e10 | Mark McLoughlin | nd->macaddr[0] = 0x52; |
781 | f83c6e10 | Mark McLoughlin | nd->macaddr[1] = 0x54; |
782 | f83c6e10 | Mark McLoughlin | nd->macaddr[2] = 0x00; |
783 | f83c6e10 | Mark McLoughlin | nd->macaddr[3] = 0x12; |
784 | f83c6e10 | Mark McLoughlin | nd->macaddr[4] = 0x34; |
785 | f83c6e10 | Mark McLoughlin | nd->macaddr[5] = 0x56 + idx; |
786 | f83c6e10 | Mark McLoughlin | |
787 | f83c6e10 | Mark McLoughlin | if (qemu_opt_get(opts, "macaddr") && |
788 | f1d078c3 | Mark McLoughlin | net_parse_macaddr(nd->macaddr, qemu_opt_get(opts, "macaddr")) < 0) { |
789 | 1ecda02b | Markus Armbruster | error_report("invalid syntax for ethernet address");
|
790 | f83c6e10 | Mark McLoughlin | return -1; |
791 | f83c6e10 | Mark McLoughlin | } |
792 | f83c6e10 | Mark McLoughlin | |
793 | 75422b0d | Amit Shah | nd->nvectors = qemu_opt_get_number(opts, "vectors",
|
794 | 75422b0d | Amit Shah | DEV_NVECTORS_UNSPECIFIED); |
795 | 75422b0d | Amit Shah | if (nd->nvectors != DEV_NVECTORS_UNSPECIFIED &&
|
796 | f83c6e10 | Mark McLoughlin | (nd->nvectors < 0 || nd->nvectors > 0x7ffffff)) { |
797 | 1ecda02b | Markus Armbruster | error_report("invalid # of vectors: %d", nd->nvectors);
|
798 | f83c6e10 | Mark McLoughlin | return -1; |
799 | f83c6e10 | Mark McLoughlin | } |
800 | f83c6e10 | Mark McLoughlin | |
801 | f83c6e10 | Mark McLoughlin | nd->used = 1;
|
802 | f83c6e10 | Mark McLoughlin | nb_nics++; |
803 | f83c6e10 | Mark McLoughlin | |
804 | f83c6e10 | Mark McLoughlin | return idx;
|
805 | f83c6e10 | Mark McLoughlin | } |
806 | f83c6e10 | Mark McLoughlin | |
807 | f83c6e10 | Mark McLoughlin | #define NET_COMMON_PARAMS_DESC \
|
808 | f83c6e10 | Mark McLoughlin | { \ |
809 | f83c6e10 | Mark McLoughlin | .name = "type", \
|
810 | f83c6e10 | Mark McLoughlin | .type = QEMU_OPT_STRING, \ |
811 | f83c6e10 | Mark McLoughlin | .help = "net client type (nic, tap etc.)", \
|
812 | f83c6e10 | Mark McLoughlin | }, { \ |
813 | f83c6e10 | Mark McLoughlin | .name = "vlan", \
|
814 | f83c6e10 | Mark McLoughlin | .type = QEMU_OPT_NUMBER, \ |
815 | f83c6e10 | Mark McLoughlin | .help = "vlan number", \
|
816 | f83c6e10 | Mark McLoughlin | }, { \ |
817 | f83c6e10 | Mark McLoughlin | .name = "name", \
|
818 | f83c6e10 | Mark McLoughlin | .type = QEMU_OPT_STRING, \ |
819 | f83c6e10 | Mark McLoughlin | .help = "identifier for monitor commands", \
|
820 | f83c6e10 | Mark McLoughlin | } |
821 | f83c6e10 | Mark McLoughlin | |
822 | 6d952ebe | Mark McLoughlin | typedef int (*net_client_init_func)(QemuOpts *opts, |
823 | 6d952ebe | Mark McLoughlin | Monitor *mon, |
824 | f6b134ac | Mark McLoughlin | const char *name, |
825 | f6b134ac | Mark McLoughlin | VLANState *vlan); |
826 | f83c6e10 | Mark McLoughlin | |
827 | f83c6e10 | Mark McLoughlin | /* magic number, but compiler will warn if too small */
|
828 | f83c6e10 | Mark McLoughlin | #define NET_MAX_DESC 20 |
829 | f83c6e10 | Mark McLoughlin | |
830 | 238431a9 | Blue Swirl | static const struct { |
831 | f83c6e10 | Mark McLoughlin | const char *type; |
832 | f83c6e10 | Mark McLoughlin | net_client_init_func init; |
833 | f83c6e10 | Mark McLoughlin | QemuOptDesc desc[NET_MAX_DESC]; |
834 | f83c6e10 | Mark McLoughlin | } net_client_types[] = { |
835 | f83c6e10 | Mark McLoughlin | { |
836 | f83c6e10 | Mark McLoughlin | .type = "none",
|
837 | f83c6e10 | Mark McLoughlin | .desc = { |
838 | f83c6e10 | Mark McLoughlin | NET_COMMON_PARAMS_DESC, |
839 | f83c6e10 | Mark McLoughlin | { /* end of list */ }
|
840 | f83c6e10 | Mark McLoughlin | }, |
841 | f83c6e10 | Mark McLoughlin | }, { |
842 | f83c6e10 | Mark McLoughlin | .type = "nic",
|
843 | f83c6e10 | Mark McLoughlin | .init = net_init_nic, |
844 | f83c6e10 | Mark McLoughlin | .desc = { |
845 | f83c6e10 | Mark McLoughlin | NET_COMMON_PARAMS_DESC, |
846 | f83c6e10 | Mark McLoughlin | { |
847 | 5869c4d5 | Mark McLoughlin | .name = "netdev",
|
848 | 5869c4d5 | Mark McLoughlin | .type = QEMU_OPT_STRING, |
849 | 5869c4d5 | Mark McLoughlin | .help = "id of -netdev to connect to",
|
850 | 5869c4d5 | Mark McLoughlin | }, |
851 | 5869c4d5 | Mark McLoughlin | { |
852 | f83c6e10 | Mark McLoughlin | .name = "macaddr",
|
853 | f83c6e10 | Mark McLoughlin | .type = QEMU_OPT_STRING, |
854 | f83c6e10 | Mark McLoughlin | .help = "MAC address",
|
855 | f83c6e10 | Mark McLoughlin | }, { |
856 | f83c6e10 | Mark McLoughlin | .name = "model",
|
857 | f83c6e10 | Mark McLoughlin | .type = QEMU_OPT_STRING, |
858 | f83c6e10 | Mark McLoughlin | .help = "device model (e1000, rtl8139, virtio etc.)",
|
859 | f83c6e10 | Mark McLoughlin | }, { |
860 | f83c6e10 | Mark McLoughlin | .name = "addr",
|
861 | f83c6e10 | Mark McLoughlin | .type = QEMU_OPT_STRING, |
862 | f83c6e10 | Mark McLoughlin | .help = "PCI device address",
|
863 | f83c6e10 | Mark McLoughlin | }, { |
864 | f83c6e10 | Mark McLoughlin | .name = "vectors",
|
865 | f83c6e10 | Mark McLoughlin | .type = QEMU_OPT_NUMBER, |
866 | f83c6e10 | Mark McLoughlin | .help = "number of MSI-x vectors, 0 to disable MSI-X",
|
867 | f83c6e10 | Mark McLoughlin | }, |
868 | f83c6e10 | Mark McLoughlin | { /* end of list */ }
|
869 | f83c6e10 | Mark McLoughlin | }, |
870 | ec302ffd | Mark McLoughlin | #ifdef CONFIG_SLIRP
|
871 | ec302ffd | Mark McLoughlin | }, { |
872 | ec302ffd | Mark McLoughlin | .type = "user",
|
873 | ec302ffd | Mark McLoughlin | .init = net_init_slirp, |
874 | ec302ffd | Mark McLoughlin | .desc = { |
875 | ec302ffd | Mark McLoughlin | NET_COMMON_PARAMS_DESC, |
876 | ec302ffd | Mark McLoughlin | { |
877 | ec302ffd | Mark McLoughlin | .name = "hostname",
|
878 | ec302ffd | Mark McLoughlin | .type = QEMU_OPT_STRING, |
879 | ec302ffd | Mark McLoughlin | .help = "client hostname reported by the builtin DHCP server",
|
880 | ec302ffd | Mark McLoughlin | }, { |
881 | ec302ffd | Mark McLoughlin | .name = "restrict",
|
882 | ec302ffd | Mark McLoughlin | .type = QEMU_OPT_STRING, |
883 | ec302ffd | Mark McLoughlin | .help = "isolate the guest from the host (y|yes|n|no)",
|
884 | ec302ffd | Mark McLoughlin | }, { |
885 | ec302ffd | Mark McLoughlin | .name = "ip",
|
886 | ec302ffd | Mark McLoughlin | .type = QEMU_OPT_STRING, |
887 | ec302ffd | Mark McLoughlin | .help = "legacy parameter, use net= instead",
|
888 | ec302ffd | Mark McLoughlin | }, { |
889 | ec302ffd | Mark McLoughlin | .name = "net",
|
890 | ec302ffd | Mark McLoughlin | .type = QEMU_OPT_STRING, |
891 | ec302ffd | Mark McLoughlin | .help = "IP address and optional netmask",
|
892 | ec302ffd | Mark McLoughlin | }, { |
893 | ec302ffd | Mark McLoughlin | .name = "host",
|
894 | ec302ffd | Mark McLoughlin | .type = QEMU_OPT_STRING, |
895 | ec302ffd | Mark McLoughlin | .help = "guest-visible address of the host",
|
896 | ec302ffd | Mark McLoughlin | }, { |
897 | ec302ffd | Mark McLoughlin | .name = "tftp",
|
898 | ec302ffd | Mark McLoughlin | .type = QEMU_OPT_STRING, |
899 | ec302ffd | Mark McLoughlin | .help = "root directory of the built-in TFTP server",
|
900 | ec302ffd | Mark McLoughlin | }, { |
901 | ec302ffd | Mark McLoughlin | .name = "bootfile",
|
902 | ec302ffd | Mark McLoughlin | .type = QEMU_OPT_STRING, |
903 | ec302ffd | Mark McLoughlin | .help = "BOOTP filename, for use with tftp=",
|
904 | ec302ffd | Mark McLoughlin | }, { |
905 | ec302ffd | Mark McLoughlin | .name = "dhcpstart",
|
906 | ec302ffd | Mark McLoughlin | .type = QEMU_OPT_STRING, |
907 | ec302ffd | Mark McLoughlin | .help = "the first of the 16 IPs the built-in DHCP server can assign",
|
908 | ec302ffd | Mark McLoughlin | }, { |
909 | ec302ffd | Mark McLoughlin | .name = "dns",
|
910 | ec302ffd | Mark McLoughlin | .type = QEMU_OPT_STRING, |
911 | ec302ffd | Mark McLoughlin | .help = "guest-visible address of the virtual nameserver",
|
912 | ec302ffd | Mark McLoughlin | }, { |
913 | ec302ffd | Mark McLoughlin | .name = "smb",
|
914 | ec302ffd | Mark McLoughlin | .type = QEMU_OPT_STRING, |
915 | ec302ffd | Mark McLoughlin | .help = "root directory of the built-in SMB server",
|
916 | ec302ffd | Mark McLoughlin | }, { |
917 | ec302ffd | Mark McLoughlin | .name = "smbserver",
|
918 | ec302ffd | Mark McLoughlin | .type = QEMU_OPT_STRING, |
919 | ec302ffd | Mark McLoughlin | .help = "IP address of the built-in SMB server",
|
920 | ec302ffd | Mark McLoughlin | }, { |
921 | ec302ffd | Mark McLoughlin | .name = "hostfwd",
|
922 | ec302ffd | Mark McLoughlin | .type = QEMU_OPT_STRING, |
923 | ec302ffd | Mark McLoughlin | .help = "guest port number to forward incoming TCP or UDP connections",
|
924 | ec302ffd | Mark McLoughlin | }, { |
925 | ec302ffd | Mark McLoughlin | .name = "guestfwd",
|
926 | ec302ffd | Mark McLoughlin | .type = QEMU_OPT_STRING, |
927 | ec302ffd | Mark McLoughlin | .help = "IP address and port to forward guest TCP connections",
|
928 | ec302ffd | Mark McLoughlin | }, |
929 | ec302ffd | Mark McLoughlin | { /* end of list */ }
|
930 | ec302ffd | Mark McLoughlin | }, |
931 | ec302ffd | Mark McLoughlin | #endif
|
932 | 8a1c5235 | Mark McLoughlin | }, { |
933 | 8a1c5235 | Mark McLoughlin | .type = "tap",
|
934 | a8ed73f7 | Mark McLoughlin | .init = net_init_tap, |
935 | 8a1c5235 | Mark McLoughlin | .desc = { |
936 | 8a1c5235 | Mark McLoughlin | NET_COMMON_PARAMS_DESC, |
937 | 8a1c5235 | Mark McLoughlin | { |
938 | 8a1c5235 | Mark McLoughlin | .name = "ifname",
|
939 | 8a1c5235 | Mark McLoughlin | .type = QEMU_OPT_STRING, |
940 | 8a1c5235 | Mark McLoughlin | .help = "interface name",
|
941 | 8a1c5235 | Mark McLoughlin | }, |
942 | a8ed73f7 | Mark McLoughlin | #ifndef _WIN32
|
943 | 8a1c5235 | Mark McLoughlin | { |
944 | 8a1c5235 | Mark McLoughlin | .name = "fd",
|
945 | 8a1c5235 | Mark McLoughlin | .type = QEMU_OPT_STRING, |
946 | 8a1c5235 | Mark McLoughlin | .help = "file descriptor of an already opened tap",
|
947 | 8a1c5235 | Mark McLoughlin | }, { |
948 | 8a1c5235 | Mark McLoughlin | .name = "script",
|
949 | 8a1c5235 | Mark McLoughlin | .type = QEMU_OPT_STRING, |
950 | 8a1c5235 | Mark McLoughlin | .help = "script to initialize the interface",
|
951 | 8a1c5235 | Mark McLoughlin | }, { |
952 | 8a1c5235 | Mark McLoughlin | .name = "downscript",
|
953 | 8a1c5235 | Mark McLoughlin | .type = QEMU_OPT_STRING, |
954 | 8a1c5235 | Mark McLoughlin | .help = "script to shut down the interface",
|
955 | 8a1c5235 | Mark McLoughlin | }, { |
956 | 8a1c5235 | Mark McLoughlin | .name = "sndbuf",
|
957 | 8a1c5235 | Mark McLoughlin | .type = QEMU_OPT_SIZE, |
958 | 8a1c5235 | Mark McLoughlin | .help = "send buffer limit"
|
959 | baf74c95 | Mark McLoughlin | }, { |
960 | baf74c95 | Mark McLoughlin | .name = "vnet_hdr",
|
961 | baf74c95 | Mark McLoughlin | .type = QEMU_OPT_BOOL, |
962 | baf74c95 | Mark McLoughlin | .help = "enable the IFF_VNET_HDR flag on the tap interface"
|
963 | 82b0d80e | Michael S. Tsirkin | }, { |
964 | 82b0d80e | Michael S. Tsirkin | .name = "vhost",
|
965 | 82b0d80e | Michael S. Tsirkin | .type = QEMU_OPT_BOOL, |
966 | 82b0d80e | Michael S. Tsirkin | .help = "enable vhost-net network accelerator",
|
967 | 82b0d80e | Michael S. Tsirkin | }, { |
968 | 82b0d80e | Michael S. Tsirkin | .name = "vhostfd",
|
969 | 82b0d80e | Michael S. Tsirkin | .type = QEMU_OPT_STRING, |
970 | 82b0d80e | Michael S. Tsirkin | .help = "file descriptor of an already opened vhost net device",
|
971 | 96c94b29 | Jason Wang | }, { |
972 | 96c94b29 | Jason Wang | .name = "vhostforce",
|
973 | 96c94b29 | Jason Wang | .type = QEMU_OPT_BOOL, |
974 | 96c94b29 | Jason Wang | .help = "force vhost on for non-MSIX virtio guests",
|
975 | 96c94b29 | Jason Wang | }, |
976 | a8ed73f7 | Mark McLoughlin | #endif /* _WIN32 */ |
977 | 8a1c5235 | Mark McLoughlin | { /* end of list */ }
|
978 | 8a1c5235 | Mark McLoughlin | }, |
979 | 88ce16ca | Mark McLoughlin | }, { |
980 | 88ce16ca | Mark McLoughlin | .type = "socket",
|
981 | 88ce16ca | Mark McLoughlin | .init = net_init_socket, |
982 | 88ce16ca | Mark McLoughlin | .desc = { |
983 | 88ce16ca | Mark McLoughlin | NET_COMMON_PARAMS_DESC, |
984 | 88ce16ca | Mark McLoughlin | { |
985 | 88ce16ca | Mark McLoughlin | .name = "fd",
|
986 | 88ce16ca | Mark McLoughlin | .type = QEMU_OPT_STRING, |
987 | 88ce16ca | Mark McLoughlin | .help = "file descriptor of an already opened socket",
|
988 | 88ce16ca | Mark McLoughlin | }, { |
989 | 88ce16ca | Mark McLoughlin | .name = "listen",
|
990 | 88ce16ca | Mark McLoughlin | .type = QEMU_OPT_STRING, |
991 | 88ce16ca | Mark McLoughlin | .help = "port number, and optional hostname, to listen on",
|
992 | 88ce16ca | Mark McLoughlin | }, { |
993 | 88ce16ca | Mark McLoughlin | .name = "connect",
|
994 | 88ce16ca | Mark McLoughlin | .type = QEMU_OPT_STRING, |
995 | 88ce16ca | Mark McLoughlin | .help = "port number, and optional hostname, to connect to",
|
996 | 88ce16ca | Mark McLoughlin | }, { |
997 | 88ce16ca | Mark McLoughlin | .name = "mcast",
|
998 | 88ce16ca | Mark McLoughlin | .type = QEMU_OPT_STRING, |
999 | 88ce16ca | Mark McLoughlin | .help = "UDP multicast address and port number",
|
1000 | 3a75e74c | Mike Ryan | }, { |
1001 | 3a75e74c | Mike Ryan | .name = "localaddr",
|
1002 | 3a75e74c | Mike Ryan | .type = QEMU_OPT_STRING, |
1003 | 3a75e74c | Mike Ryan | .help = "source address for multicast packets",
|
1004 | 88ce16ca | Mark McLoughlin | }, |
1005 | 88ce16ca | Mark McLoughlin | { /* end of list */ }
|
1006 | 88ce16ca | Mark McLoughlin | }, |
1007 | dd51058d | Mark McLoughlin | #ifdef CONFIG_VDE
|
1008 | dd51058d | Mark McLoughlin | }, { |
1009 | dd51058d | Mark McLoughlin | .type = "vde",
|
1010 | dd51058d | Mark McLoughlin | .init = net_init_vde, |
1011 | dd51058d | Mark McLoughlin | .desc = { |
1012 | dd51058d | Mark McLoughlin | NET_COMMON_PARAMS_DESC, |
1013 | dd51058d | Mark McLoughlin | { |
1014 | dd51058d | Mark McLoughlin | .name = "sock",
|
1015 | dd51058d | Mark McLoughlin | .type = QEMU_OPT_STRING, |
1016 | dd51058d | Mark McLoughlin | .help = "socket path",
|
1017 | dd51058d | Mark McLoughlin | }, { |
1018 | dd51058d | Mark McLoughlin | .name = "port",
|
1019 | dd51058d | Mark McLoughlin | .type = QEMU_OPT_NUMBER, |
1020 | dd51058d | Mark McLoughlin | .help = "port number",
|
1021 | dd51058d | Mark McLoughlin | }, { |
1022 | dd51058d | Mark McLoughlin | .name = "group",
|
1023 | dd51058d | Mark McLoughlin | .type = QEMU_OPT_STRING, |
1024 | dd51058d | Mark McLoughlin | .help = "group owner of socket",
|
1025 | dd51058d | Mark McLoughlin | }, { |
1026 | dd51058d | Mark McLoughlin | .name = "mode",
|
1027 | dd51058d | Mark McLoughlin | .type = QEMU_OPT_NUMBER, |
1028 | dd51058d | Mark McLoughlin | .help = "permissions for socket",
|
1029 | dd51058d | Mark McLoughlin | }, |
1030 | dd51058d | Mark McLoughlin | { /* end of list */ }
|
1031 | dd51058d | Mark McLoughlin | }, |
1032 | dd51058d | Mark McLoughlin | #endif
|
1033 | ed2955c2 | Mark McLoughlin | }, { |
1034 | ed2955c2 | Mark McLoughlin | .type = "dump",
|
1035 | ed2955c2 | Mark McLoughlin | .init = net_init_dump, |
1036 | ed2955c2 | Mark McLoughlin | .desc = { |
1037 | ed2955c2 | Mark McLoughlin | NET_COMMON_PARAMS_DESC, |
1038 | ed2955c2 | Mark McLoughlin | { |
1039 | ed2955c2 | Mark McLoughlin | .name = "len",
|
1040 | ed2955c2 | Mark McLoughlin | .type = QEMU_OPT_SIZE, |
1041 | ed2955c2 | Mark McLoughlin | .help = "per-packet size limit (64k default)",
|
1042 | ed2955c2 | Mark McLoughlin | }, { |
1043 | ed2955c2 | Mark McLoughlin | .name = "file",
|
1044 | ed2955c2 | Mark McLoughlin | .type = QEMU_OPT_STRING, |
1045 | ed2955c2 | Mark McLoughlin | .help = "dump file path (default is qemu-vlan0.pcap)",
|
1046 | ed2955c2 | Mark McLoughlin | }, |
1047 | ed2955c2 | Mark McLoughlin | { /* end of list */ }
|
1048 | ed2955c2 | Mark McLoughlin | }, |
1049 | f83c6e10 | Mark McLoughlin | }, |
1050 | f83c6e10 | Mark McLoughlin | { /* end of list */ }
|
1051 | f83c6e10 | Mark McLoughlin | }; |
1052 | f83c6e10 | Mark McLoughlin | |
1053 | f6b134ac | Mark McLoughlin | int net_client_init(Monitor *mon, QemuOpts *opts, int is_netdev) |
1054 | f83c6e10 | Mark McLoughlin | { |
1055 | 6d952ebe | Mark McLoughlin | const char *name; |
1056 | f83c6e10 | Mark McLoughlin | const char *type; |
1057 | f83c6e10 | Mark McLoughlin | int i;
|
1058 | f83c6e10 | Mark McLoughlin | |
1059 | f83c6e10 | Mark McLoughlin | type = qemu_opt_get(opts, "type");
|
1060 | 5294e2c7 | Markus Armbruster | if (!type) {
|
1061 | 5294e2c7 | Markus Armbruster | qerror_report(QERR_MISSING_PARAMETER, "type");
|
1062 | 5294e2c7 | Markus Armbruster | return -1; |
1063 | 5294e2c7 | Markus Armbruster | } |
1064 | f83c6e10 | Mark McLoughlin | |
1065 | 5294e2c7 | Markus Armbruster | if (is_netdev) {
|
1066 | f6b134ac | Mark McLoughlin | if (strcmp(type, "tap") != 0 && |
1067 | f6b134ac | Mark McLoughlin | #ifdef CONFIG_SLIRP
|
1068 | f6b134ac | Mark McLoughlin | strcmp(type, "user") != 0 && |
1069 | f6b134ac | Mark McLoughlin | #endif
|
1070 | f6b134ac | Mark McLoughlin | #ifdef CONFIG_VDE
|
1071 | f6b134ac | Mark McLoughlin | strcmp(type, "vde") != 0 && |
1072 | f6b134ac | Mark McLoughlin | #endif
|
1073 | f6b134ac | Mark McLoughlin | strcmp(type, "socket") != 0) { |
1074 | 5294e2c7 | Markus Armbruster | qerror_report(QERR_INVALID_PARAMETER_VALUE, "type",
|
1075 | 5294e2c7 | Markus Armbruster | "a netdev backend type");
|
1076 | f6b134ac | Mark McLoughlin | return -1; |
1077 | f6b134ac | Mark McLoughlin | } |
1078 | f6b134ac | Mark McLoughlin | |
1079 | f6b134ac | Mark McLoughlin | if (qemu_opt_get(opts, "vlan")) { |
1080 | 5294e2c7 | Markus Armbruster | qerror_report(QERR_INVALID_PARAMETER, "vlan");
|
1081 | f6b134ac | Mark McLoughlin | return -1; |
1082 | f6b134ac | Mark McLoughlin | } |
1083 | f6b134ac | Mark McLoughlin | if (qemu_opt_get(opts, "name")) { |
1084 | 5294e2c7 | Markus Armbruster | qerror_report(QERR_INVALID_PARAMETER, "name");
|
1085 | f6b134ac | Mark McLoughlin | return -1; |
1086 | f6b134ac | Mark McLoughlin | } |
1087 | f6b134ac | Mark McLoughlin | if (!qemu_opts_id(opts)) {
|
1088 | 5294e2c7 | Markus Armbruster | qerror_report(QERR_MISSING_PARAMETER, "id");
|
1089 | f6b134ac | Mark McLoughlin | return -1; |
1090 | f6b134ac | Mark McLoughlin | } |
1091 | f6b134ac | Mark McLoughlin | } |
1092 | f6b134ac | Mark McLoughlin | |
1093 | 6d952ebe | Mark McLoughlin | name = qemu_opts_id(opts); |
1094 | 6d952ebe | Mark McLoughlin | if (!name) {
|
1095 | 6d952ebe | Mark McLoughlin | name = qemu_opt_get(opts, "name");
|
1096 | 6d952ebe | Mark McLoughlin | } |
1097 | 6d952ebe | Mark McLoughlin | |
1098 | f83c6e10 | Mark McLoughlin | for (i = 0; net_client_types[i].type != NULL; i++) { |
1099 | f83c6e10 | Mark McLoughlin | if (!strcmp(net_client_types[i].type, type)) {
|
1100 | f6b134ac | Mark McLoughlin | VLANState *vlan = NULL;
|
1101 | 50e32ea8 | Amit Shah | int ret;
|
1102 | f6b134ac | Mark McLoughlin | |
1103 | f83c6e10 | Mark McLoughlin | if (qemu_opts_validate(opts, &net_client_types[i].desc[0]) == -1) { |
1104 | f83c6e10 | Mark McLoughlin | return -1; |
1105 | f83c6e10 | Mark McLoughlin | } |
1106 | f83c6e10 | Mark McLoughlin | |
1107 | 5869c4d5 | Mark McLoughlin | /* Do not add to a vlan if it's a -netdev or a nic with a
|
1108 | 5869c4d5 | Mark McLoughlin | * netdev= parameter. */
|
1109 | 5869c4d5 | Mark McLoughlin | if (!(is_netdev ||
|
1110 | 5869c4d5 | Mark McLoughlin | (strcmp(type, "nic") == 0 && qemu_opt_get(opts, "netdev")))) { |
1111 | f6b134ac | Mark McLoughlin | vlan = qemu_find_vlan(qemu_opt_get_number(opts, "vlan", 0), 1); |
1112 | f6b134ac | Mark McLoughlin | } |
1113 | f6b134ac | Mark McLoughlin | |
1114 | 03c71553 | Amit Shah | ret = 0;
|
1115 | f83c6e10 | Mark McLoughlin | if (net_client_types[i].init) {
|
1116 | 50e32ea8 | Amit Shah | ret = net_client_types[i].init(opts, mon, name, vlan); |
1117 | 50e32ea8 | Amit Shah | if (ret < 0) { |
1118 | 5294e2c7 | Markus Armbruster | /* TODO push error reporting into init() methods */
|
1119 | 5294e2c7 | Markus Armbruster | qerror_report(QERR_DEVICE_INIT_FAILED, type); |
1120 | 5294e2c7 | Markus Armbruster | return -1; |
1121 | 5294e2c7 | Markus Armbruster | } |
1122 | f83c6e10 | Mark McLoughlin | } |
1123 | 50e32ea8 | Amit Shah | return ret;
|
1124 | f83c6e10 | Mark McLoughlin | } |
1125 | f83c6e10 | Mark McLoughlin | } |
1126 | f83c6e10 | Mark McLoughlin | |
1127 | 5294e2c7 | Markus Armbruster | qerror_report(QERR_INVALID_PARAMETER_VALUE, "type",
|
1128 | 5294e2c7 | Markus Armbruster | "a network client type");
|
1129 | f83c6e10 | Mark McLoughlin | return -1; |
1130 | f83c6e10 | Mark McLoughlin | } |
1131 | f83c6e10 | Mark McLoughlin | |
1132 | 6f338c34 | aliguori | static int net_host_check_device(const char *device) |
1133 | 6f338c34 | aliguori | { |
1134 | 6f338c34 | aliguori | int i;
|
1135 | bb9ea79e | aliguori | const char *valid_param_list[] = { "tap", "socket", "dump" |
1136 | 6f338c34 | aliguori | #ifdef CONFIG_SLIRP
|
1137 | 6f338c34 | aliguori | ,"user"
|
1138 | 6f338c34 | aliguori | #endif
|
1139 | 6f338c34 | aliguori | #ifdef CONFIG_VDE
|
1140 | 6f338c34 | aliguori | ,"vde"
|
1141 | 6f338c34 | aliguori | #endif
|
1142 | 6f338c34 | aliguori | }; |
1143 | 6f338c34 | aliguori | for (i = 0; i < sizeof(valid_param_list) / sizeof(char *); i++) { |
1144 | 6f338c34 | aliguori | if (!strncmp(valid_param_list[i], device,
|
1145 | 6f338c34 | aliguori | strlen(valid_param_list[i]))) |
1146 | 6f338c34 | aliguori | return 1; |
1147 | 6f338c34 | aliguori | } |
1148 | 6f338c34 | aliguori | |
1149 | 6f338c34 | aliguori | return 0; |
1150 | 6f338c34 | aliguori | } |
1151 | 6f338c34 | aliguori | |
1152 | f18c16de | Luiz Capitulino | void net_host_device_add(Monitor *mon, const QDict *qdict) |
1153 | 6f338c34 | aliguori | { |
1154 | f18c16de | Luiz Capitulino | const char *device = qdict_get_str(qdict, "device"); |
1155 | 7f1c9d20 | Mark McLoughlin | const char *opts_str = qdict_get_try_str(qdict, "opts"); |
1156 | 7f1c9d20 | Mark McLoughlin | QemuOpts *opts; |
1157 | f18c16de | Luiz Capitulino | |
1158 | 6f338c34 | aliguori | if (!net_host_check_device(device)) {
|
1159 | 376253ec | aliguori | monitor_printf(mon, "invalid host network device %s\n", device);
|
1160 | 6f338c34 | aliguori | return;
|
1161 | 6f338c34 | aliguori | } |
1162 | 7f1c9d20 | Mark McLoughlin | |
1163 | 3329f07b | Gerd Hoffmann | opts = qemu_opts_parse(qemu_find_opts("net"), opts_str ? opts_str : "", 0); |
1164 | 7f1c9d20 | Mark McLoughlin | if (!opts) {
|
1165 | 7f1c9d20 | Mark McLoughlin | return;
|
1166 | 7f1c9d20 | Mark McLoughlin | } |
1167 | 7f1c9d20 | Mark McLoughlin | |
1168 | 7f1c9d20 | Mark McLoughlin | qemu_opt_set(opts, "type", device);
|
1169 | 7f1c9d20 | Mark McLoughlin | |
1170 | f6b134ac | Mark McLoughlin | if (net_client_init(mon, opts, 0) < 0) { |
1171 | 5c8be678 | aliguori | monitor_printf(mon, "adding host network device %s failed\n", device);
|
1172 | 5c8be678 | aliguori | } |
1173 | 6f338c34 | aliguori | } |
1174 | 6f338c34 | aliguori | |
1175 | f18c16de | Luiz Capitulino | void net_host_device_remove(Monitor *mon, const QDict *qdict) |
1176 | 6f338c34 | aliguori | { |
1177 | 6f338c34 | aliguori | VLANClientState *vc; |
1178 | f18c16de | Luiz Capitulino | int vlan_id = qdict_get_int(qdict, "vlan_id"); |
1179 | f18c16de | Luiz Capitulino | const char *device = qdict_get_str(qdict, "device"); |
1180 | 6f338c34 | aliguori | |
1181 | 1a609520 | Jan Kiszka | vc = qemu_find_vlan_client_by_name(mon, vlan_id, device); |
1182 | 6f338c34 | aliguori | if (!vc) {
|
1183 | 6f338c34 | aliguori | return;
|
1184 | 6f338c34 | aliguori | } |
1185 | e8f1f9db | aliguori | if (!net_host_check_device(vc->model)) {
|
1186 | e8f1f9db | aliguori | monitor_printf(mon, "invalid host network device %s\n", device);
|
1187 | e8f1f9db | aliguori | return;
|
1188 | e8f1f9db | aliguori | } |
1189 | 6f338c34 | aliguori | qemu_del_vlan_client(vc); |
1190 | 6f338c34 | aliguori | } |
1191 | 6f338c34 | aliguori | |
1192 | ae82d324 | Markus Armbruster | int do_netdev_add(Monitor *mon, const QDict *qdict, QObject **ret_data) |
1193 | ae82d324 | Markus Armbruster | { |
1194 | ae82d324 | Markus Armbruster | QemuOpts *opts; |
1195 | ae82d324 | Markus Armbruster | int res;
|
1196 | ae82d324 | Markus Armbruster | |
1197 | 3329f07b | Gerd Hoffmann | opts = qemu_opts_from_qdict(qemu_find_opts("netdev"), qdict);
|
1198 | ae82d324 | Markus Armbruster | if (!opts) {
|
1199 | ae82d324 | Markus Armbruster | return -1; |
1200 | ae82d324 | Markus Armbruster | } |
1201 | ae82d324 | Markus Armbruster | |
1202 | ae82d324 | Markus Armbruster | res = net_client_init(mon, opts, 1);
|
1203 | 410cbafe | Yoshiaki Tamura | if (res < 0) { |
1204 | 410cbafe | Yoshiaki Tamura | qemu_opts_del(opts); |
1205 | 410cbafe | Yoshiaki Tamura | } |
1206 | 410cbafe | Yoshiaki Tamura | |
1207 | ae82d324 | Markus Armbruster | return res;
|
1208 | ae82d324 | Markus Armbruster | } |
1209 | ae82d324 | Markus Armbruster | |
1210 | ae82d324 | Markus Armbruster | int do_netdev_del(Monitor *mon, const QDict *qdict, QObject **ret_data) |
1211 | ae82d324 | Markus Armbruster | { |
1212 | ae82d324 | Markus Armbruster | const char *id = qdict_get_str(qdict, "id"); |
1213 | ae82d324 | Markus Armbruster | VLANClientState *vc; |
1214 | ae82d324 | Markus Armbruster | |
1215 | ae82d324 | Markus Armbruster | vc = qemu_find_netdev(id); |
1216 | ae82d324 | Markus Armbruster | if (!vc || vc->info->type == NET_CLIENT_TYPE_NIC) {
|
1217 | ae82d324 | Markus Armbruster | qerror_report(QERR_DEVICE_NOT_FOUND, id); |
1218 | ae82d324 | Markus Armbruster | return -1; |
1219 | ae82d324 | Markus Armbruster | } |
1220 | ae82d324 | Markus Armbruster | qemu_del_vlan_client(vc); |
1221 | 3329f07b | Gerd Hoffmann | qemu_opts_del(qemu_opts_find(qemu_find_opts("netdev"), id));
|
1222 | ae82d324 | Markus Armbruster | return 0; |
1223 | ae82d324 | Markus Armbruster | } |
1224 | ae82d324 | Markus Armbruster | |
1225 | 376253ec | aliguori | void do_info_network(Monitor *mon)
|
1226 | 63a01ef8 | aliguori | { |
1227 | 63a01ef8 | aliguori | VLANState *vlan; |
1228 | a0104e0e | Markus Armbruster | VLANClientState *vc; |
1229 | 63a01ef8 | aliguori | |
1230 | 5610c3aa | Mark McLoughlin | QTAILQ_FOREACH(vlan, &vlans, next) { |
1231 | 376253ec | aliguori | monitor_printf(mon, "VLAN %d devices:\n", vlan->id);
|
1232 | 5610c3aa | Mark McLoughlin | |
1233 | 5610c3aa | Mark McLoughlin | QTAILQ_FOREACH(vc, &vlan->clients, next) { |
1234 | 376253ec | aliguori | monitor_printf(mon, " %s: %s\n", vc->name, vc->info_str);
|
1235 | 5610c3aa | Mark McLoughlin | } |
1236 | 63a01ef8 | aliguori | } |
1237 | a0104e0e | Markus Armbruster | monitor_printf(mon, "Devices not on any VLAN:\n");
|
1238 | a0104e0e | Markus Armbruster | QTAILQ_FOREACH(vc, &non_vlan_clients, next) { |
1239 | a0104e0e | Markus Armbruster | monitor_printf(mon, " %s: %s", vc->name, vc->info_str);
|
1240 | a0104e0e | Markus Armbruster | if (vc->peer) {
|
1241 | a0104e0e | Markus Armbruster | monitor_printf(mon, " peer=%s", vc->peer->name);
|
1242 | a0104e0e | Markus Armbruster | } |
1243 | a0104e0e | Markus Armbruster | monitor_printf(mon, "\n");
|
1244 | a0104e0e | Markus Armbruster | } |
1245 | 63a01ef8 | aliguori | } |
1246 | 63a01ef8 | aliguori | |
1247 | 5369e3c0 | Markus Armbruster | int do_set_link(Monitor *mon, const QDict *qdict, QObject **ret_data) |
1248 | 436e5e53 | aliguori | { |
1249 | 436e5e53 | aliguori | VLANState *vlan; |
1250 | 436e5e53 | aliguori | VLANClientState *vc = NULL;
|
1251 | f18c16de | Luiz Capitulino | const char *name = qdict_get_str(qdict, "name"); |
1252 | c9b26a4c | Markus Armbruster | int up = qdict_get_bool(qdict, "up"); |
1253 | 436e5e53 | aliguori | |
1254 | 5610c3aa | Mark McLoughlin | QTAILQ_FOREACH(vlan, &vlans, next) { |
1255 | 5610c3aa | Mark McLoughlin | QTAILQ_FOREACH(vc, &vlan->clients, next) { |
1256 | 5610c3aa | Mark McLoughlin | if (strcmp(vc->name, name) == 0) { |
1257 | dd5de373 | edgar_igl | goto done;
|
1258 | 5610c3aa | Mark McLoughlin | } |
1259 | 5610c3aa | Mark McLoughlin | } |
1260 | 5610c3aa | Mark McLoughlin | } |
1261 | 2583ba97 | Markus Armbruster | vc = qemu_find_netdev(name); |
1262 | dd5de373 | edgar_igl | done:
|
1263 | 436e5e53 | aliguori | |
1264 | 436e5e53 | aliguori | if (!vc) {
|
1265 | 5369e3c0 | Markus Armbruster | qerror_report(QERR_DEVICE_NOT_FOUND, name); |
1266 | 5369e3c0 | Markus Armbruster | return -1; |
1267 | 436e5e53 | aliguori | } |
1268 | 436e5e53 | aliguori | |
1269 | c9b26a4c | Markus Armbruster | vc->link_down = !up; |
1270 | 436e5e53 | aliguori | |
1271 | 665a3b07 | Mark McLoughlin | if (vc->info->link_status_changed) {
|
1272 | 665a3b07 | Mark McLoughlin | vc->info->link_status_changed(vc); |
1273 | 665a3b07 | Mark McLoughlin | } |
1274 | ab1cbe1c | Michael S. Tsirkin | |
1275 | ab1cbe1c | Michael S. Tsirkin | /* Notify peer. Don't update peer link status: this makes it possible to
|
1276 | ab1cbe1c | Michael S. Tsirkin | * disconnect from host network without notifying the guest.
|
1277 | ab1cbe1c | Michael S. Tsirkin | * FIXME: is disconnected link status change operation useful?
|
1278 | ab1cbe1c | Michael S. Tsirkin | *
|
1279 | ab1cbe1c | Michael S. Tsirkin | * Current behaviour is compatible with qemu vlans where there could be
|
1280 | ab1cbe1c | Michael S. Tsirkin | * multiple clients that can still communicate with each other in
|
1281 | ab1cbe1c | Michael S. Tsirkin | * disconnected mode. For now maintain this compatibility. */
|
1282 | ab1cbe1c | Michael S. Tsirkin | if (vc->peer && vc->peer->info->link_status_changed) {
|
1283 | ab1cbe1c | Michael S. Tsirkin | vc->peer->info->link_status_changed(vc->peer); |
1284 | ab1cbe1c | Michael S. Tsirkin | } |
1285 | 5369e3c0 | Markus Armbruster | return 0; |
1286 | 436e5e53 | aliguori | } |
1287 | 436e5e53 | aliguori | |
1288 | 63a01ef8 | aliguori | void net_cleanup(void) |
1289 | 63a01ef8 | aliguori | { |
1290 | 63a01ef8 | aliguori | VLANState *vlan; |
1291 | 577c4af9 | Mark McLoughlin | VLANClientState *vc, *next_vc; |
1292 | 63a01ef8 | aliguori | |
1293 | 5610c3aa | Mark McLoughlin | QTAILQ_FOREACH(vlan, &vlans, next) { |
1294 | 5610c3aa | Mark McLoughlin | QTAILQ_FOREACH_SAFE(vc, &vlan->clients, next, next_vc) { |
1295 | b946a153 | aliguori | qemu_del_vlan_client(vc); |
1296 | 63a01ef8 | aliguori | } |
1297 | 63a01ef8 | aliguori | } |
1298 | 577c4af9 | Mark McLoughlin | |
1299 | 577c4af9 | Mark McLoughlin | QTAILQ_FOREACH_SAFE(vc, &non_vlan_clients, next, next_vc) { |
1300 | 577c4af9 | Mark McLoughlin | qemu_del_vlan_client(vc); |
1301 | 577c4af9 | Mark McLoughlin | } |
1302 | 63a01ef8 | aliguori | } |
1303 | 63a01ef8 | aliguori | |
1304 | 668680f7 | Markus Armbruster | void net_check_clients(void) |
1305 | 63a01ef8 | aliguori | { |
1306 | 63a01ef8 | aliguori | VLANState *vlan; |
1307 | 62112d18 | Markus Armbruster | VLANClientState *vc; |
1308 | f68b9d67 | Peter Maydell | int seen_nics = 0; |
1309 | f68b9d67 | Peter Maydell | |
1310 | f68b9d67 | Peter Maydell | /* Don't warn about the default network setup that you get if
|
1311 | f68b9d67 | Peter Maydell | * no command line -net options are specified. There are two
|
1312 | f68b9d67 | Peter Maydell | * cases that we would otherwise complain about:
|
1313 | f68b9d67 | Peter Maydell | * (1) board doesn't support a NIC but the implicit "-net nic"
|
1314 | f68b9d67 | Peter Maydell | * requested one; we'd otherwise complain about more NICs being
|
1315 | f68b9d67 | Peter Maydell | * specified than we support, and also that the vlan set up by
|
1316 | f68b9d67 | Peter Maydell | * the implicit "-net user" didn't have any NICs connected to it
|
1317 | f68b9d67 | Peter Maydell | * (2) CONFIG_SLIRP not set: we'd otherwise complain about the
|
1318 | f68b9d67 | Peter Maydell | * implicit "-net nic" setting up a nic that wasn't connected to
|
1319 | f68b9d67 | Peter Maydell | * anything.
|
1320 | f68b9d67 | Peter Maydell | */
|
1321 | f68b9d67 | Peter Maydell | if (default_net) {
|
1322 | f68b9d67 | Peter Maydell | return;
|
1323 | f68b9d67 | Peter Maydell | } |
1324 | 63a01ef8 | aliguori | |
1325 | 5610c3aa | Mark McLoughlin | QTAILQ_FOREACH(vlan, &vlans, next) { |
1326 | ac60cc18 | Tristan Gingold | int has_nic = 0, has_host_dev = 0; |
1327 | ac60cc18 | Tristan Gingold | |
1328 | 62112d18 | Markus Armbruster | QTAILQ_FOREACH(vc, &vlan->clients, next) { |
1329 | 62112d18 | Markus Armbruster | switch (vc->info->type) {
|
1330 | 62112d18 | Markus Armbruster | case NET_CLIENT_TYPE_NIC:
|
1331 | f68b9d67 | Peter Maydell | seen_nics++; |
1332 | 62112d18 | Markus Armbruster | has_nic = 1;
|
1333 | 62112d18 | Markus Armbruster | break;
|
1334 | 62112d18 | Markus Armbruster | case NET_CLIENT_TYPE_SLIRP:
|
1335 | 62112d18 | Markus Armbruster | case NET_CLIENT_TYPE_TAP:
|
1336 | 62112d18 | Markus Armbruster | case NET_CLIENT_TYPE_SOCKET:
|
1337 | 62112d18 | Markus Armbruster | case NET_CLIENT_TYPE_VDE:
|
1338 | 62112d18 | Markus Armbruster | has_host_dev = 1;
|
1339 | 62112d18 | Markus Armbruster | break;
|
1340 | 62112d18 | Markus Armbruster | default: ;
|
1341 | 62112d18 | Markus Armbruster | } |
1342 | 62112d18 | Markus Armbruster | } |
1343 | 62112d18 | Markus Armbruster | if (has_host_dev && !has_nic)
|
1344 | 63a01ef8 | aliguori | fprintf(stderr, "Warning: vlan %d with no nics\n", vlan->id);
|
1345 | 62112d18 | Markus Armbruster | if (has_nic && !has_host_dev)
|
1346 | 63a01ef8 | aliguori | fprintf(stderr, |
1347 | 63a01ef8 | aliguori | "Warning: vlan %d is not connected to host network\n",
|
1348 | 63a01ef8 | aliguori | vlan->id); |
1349 | 63a01ef8 | aliguori | } |
1350 | efe32fdd | Markus Armbruster | QTAILQ_FOREACH(vc, &non_vlan_clients, next) { |
1351 | f68b9d67 | Peter Maydell | if (vc->info->type == NET_CLIENT_TYPE_NIC) {
|
1352 | f68b9d67 | Peter Maydell | seen_nics++; |
1353 | f68b9d67 | Peter Maydell | } |
1354 | efe32fdd | Markus Armbruster | if (!vc->peer) {
|
1355 | efe32fdd | Markus Armbruster | fprintf(stderr, "Warning: %s %s has no peer\n",
|
1356 | efe32fdd | Markus Armbruster | vc->info->type == NET_CLIENT_TYPE_NIC ? "nic" : "netdev", |
1357 | efe32fdd | Markus Armbruster | vc->name); |
1358 | efe32fdd | Markus Armbruster | } |
1359 | efe32fdd | Markus Armbruster | } |
1360 | f68b9d67 | Peter Maydell | if (seen_nics != nb_nics) {
|
1361 | f68b9d67 | Peter Maydell | /* Number of NICs requested by user on command line doesn't match
|
1362 | f68b9d67 | Peter Maydell | * the number the model actually registered with us.
|
1363 | f68b9d67 | Peter Maydell | * This will generally only happen for models of embedded boards
|
1364 | f68b9d67 | Peter Maydell | * with no PCI bus or similar. PCI based machines can instantiate
|
1365 | f68b9d67 | Peter Maydell | * all requested NICs as PCI devices but usually embedded boards
|
1366 | f68b9d67 | Peter Maydell | * only have a single NIC.
|
1367 | f68b9d67 | Peter Maydell | */
|
1368 | f68b9d67 | Peter Maydell | fprintf(stderr, "Warning: more nics requested than this machine "
|
1369 | f68b9d67 | Peter Maydell | "supports; some have been ignored\n");
|
1370 | f68b9d67 | Peter Maydell | } |
1371 | 63a01ef8 | aliguori | } |
1372 | dc1c9fe8 | Mark McLoughlin | |
1373 | dc1c9fe8 | Mark McLoughlin | static int net_init_client(QemuOpts *opts, void *dummy) |
1374 | dc1c9fe8 | Mark McLoughlin | { |
1375 | c1671a08 | Mark McLoughlin | if (net_client_init(NULL, opts, 0) < 0) |
1376 | c1671a08 | Mark McLoughlin | return -1; |
1377 | c1671a08 | Mark McLoughlin | return 0; |
1378 | f6b134ac | Mark McLoughlin | } |
1379 | f6b134ac | Mark McLoughlin | |
1380 | f6b134ac | Mark McLoughlin | static int net_init_netdev(QemuOpts *opts, void *dummy) |
1381 | f6b134ac | Mark McLoughlin | { |
1382 | f6b134ac | Mark McLoughlin | return net_client_init(NULL, opts, 1); |
1383 | dc1c9fe8 | Mark McLoughlin | } |
1384 | dc1c9fe8 | Mark McLoughlin | |
1385 | dc1c9fe8 | Mark McLoughlin | int net_init_clients(void) |
1386 | dc1c9fe8 | Mark McLoughlin | { |
1387 | 3329f07b | Gerd Hoffmann | QemuOptsList *net = qemu_find_opts("net");
|
1388 | 3329f07b | Gerd Hoffmann | |
1389 | cb4522cc | Gerd Hoffmann | if (default_net) {
|
1390 | dc1c9fe8 | Mark McLoughlin | /* if no clients, we use a default config */
|
1391 | 3329f07b | Gerd Hoffmann | qemu_opts_set(net, NULL, "type", "nic"); |
1392 | dc1c9fe8 | Mark McLoughlin | #ifdef CONFIG_SLIRP
|
1393 | 3329f07b | Gerd Hoffmann | qemu_opts_set(net, NULL, "type", "user"); |
1394 | dc1c9fe8 | Mark McLoughlin | #endif
|
1395 | dc1c9fe8 | Mark McLoughlin | } |
1396 | dc1c9fe8 | Mark McLoughlin | |
1397 | 5610c3aa | Mark McLoughlin | QTAILQ_INIT(&vlans); |
1398 | 577c4af9 | Mark McLoughlin | QTAILQ_INIT(&non_vlan_clients); |
1399 | 5610c3aa | Mark McLoughlin | |
1400 | 3329f07b | Gerd Hoffmann | if (qemu_opts_foreach(qemu_find_opts("netdev"), net_init_netdev, NULL, 1) == -1) |
1401 | f6b134ac | Mark McLoughlin | return -1; |
1402 | f6b134ac | Mark McLoughlin | |
1403 | 3329f07b | Gerd Hoffmann | if (qemu_opts_foreach(net, net_init_client, NULL, 1) == -1) { |
1404 | dc1c9fe8 | Mark McLoughlin | return -1; |
1405 | dc1c9fe8 | Mark McLoughlin | } |
1406 | dc1c9fe8 | Mark McLoughlin | |
1407 | dc1c9fe8 | Mark McLoughlin | return 0; |
1408 | dc1c9fe8 | Mark McLoughlin | } |
1409 | dc1c9fe8 | Mark McLoughlin | |
1410 | 7f161aae | Mark McLoughlin | int net_client_parse(QemuOptsList *opts_list, const char *optarg) |
1411 | dc1c9fe8 | Mark McLoughlin | { |
1412 | a3a766e7 | Juan Quintela | #if defined(CONFIG_SLIRP)
|
1413 | 68ac40d2 | Mark McLoughlin | int ret;
|
1414 | 68ac40d2 | Mark McLoughlin | if (net_slirp_parse_legacy(opts_list, optarg, &ret)) {
|
1415 | dc1c9fe8 | Mark McLoughlin | return ret;
|
1416 | dc1c9fe8 | Mark McLoughlin | } |
1417 | a3a766e7 | Juan Quintela | #endif
|
1418 | 68ac40d2 | Mark McLoughlin | |
1419 | 8212c64f | Markus Armbruster | if (!qemu_opts_parse(opts_list, optarg, 1)) { |
1420 | dc1c9fe8 | Mark McLoughlin | return -1; |
1421 | dc1c9fe8 | Mark McLoughlin | } |
1422 | dc1c9fe8 | Mark McLoughlin | |
1423 | cb4522cc | Gerd Hoffmann | default_net = 0;
|
1424 | dc1c9fe8 | Mark McLoughlin | return 0; |
1425 | dc1c9fe8 | Mark McLoughlin | } |