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