net.c 39 KB

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