net.c 39 KB

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