2
0

net.c 40 KB

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