net.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056
  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 "config-host.h"
  25. #include "net/net.h"
  26. #include "clients.h"
  27. #include "hub.h"
  28. #include "net/slirp.h"
  29. #include "util.h"
  30. #include "monitor/monitor.h"
  31. #include "qemu-common.h"
  32. #include "qemu_socket.h"
  33. #include "qemu-config.h"
  34. #include "qmp-commands.h"
  35. #include "hw/qdev.h"
  36. #include "iov.h"
  37. #include "qapi-visit.h"
  38. #include "qapi/opts-visitor.h"
  39. #include "qapi/dealloc-visitor.h"
  40. /* Net bridge is currently not supported for W32. */
  41. #if !defined(_WIN32)
  42. # define CONFIG_NET_BRIDGE
  43. #endif
  44. static QTAILQ_HEAD(, NetClientState) net_clients;
  45. int default_net = 1;
  46. /***********************************************************/
  47. /* network device redirectors */
  48. #if defined(DEBUG_NET)
  49. static void hex_dump(FILE *f, const uint8_t *buf, int size)
  50. {
  51. int len, i, j, c;
  52. for(i=0;i<size;i+=16) {
  53. len = size - i;
  54. if (len > 16)
  55. len = 16;
  56. fprintf(f, "%08x ", i);
  57. for(j=0;j<16;j++) {
  58. if (j < len)
  59. fprintf(f, " %02x", buf[i+j]);
  60. else
  61. fprintf(f, " ");
  62. }
  63. fprintf(f, " ");
  64. for(j=0;j<len;j++) {
  65. c = buf[i+j];
  66. if (c < ' ' || c > '~')
  67. c = '.';
  68. fprintf(f, "%c", c);
  69. }
  70. fprintf(f, "\n");
  71. }
  72. }
  73. #endif
  74. static int get_str_sep(char *buf, int buf_size, const char **pp, int sep)
  75. {
  76. const char *p, *p1;
  77. int len;
  78. p = *pp;
  79. p1 = strchr(p, sep);
  80. if (!p1)
  81. return -1;
  82. len = p1 - p;
  83. p1++;
  84. if (buf_size > 0) {
  85. if (len > buf_size - 1)
  86. len = buf_size - 1;
  87. memcpy(buf, p, len);
  88. buf[len] = '\0';
  89. }
  90. *pp = p1;
  91. return 0;
  92. }
  93. int parse_host_port(struct sockaddr_in *saddr, const char *str)
  94. {
  95. char buf[512];
  96. struct hostent *he;
  97. const char *p, *r;
  98. int port;
  99. p = str;
  100. if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
  101. return -1;
  102. saddr->sin_family = AF_INET;
  103. if (buf[0] == '\0') {
  104. saddr->sin_addr.s_addr = 0;
  105. } else {
  106. if (qemu_isdigit(buf[0])) {
  107. if (!inet_aton(buf, &saddr->sin_addr))
  108. return -1;
  109. } else {
  110. if ((he = gethostbyname(buf)) == NULL)
  111. return - 1;
  112. saddr->sin_addr = *(struct in_addr *)he->h_addr;
  113. }
  114. }
  115. port = strtol(p, (char **)&r, 0);
  116. if (r == p)
  117. return -1;
  118. saddr->sin_port = htons(port);
  119. return 0;
  120. }
  121. void qemu_format_nic_info_str(NetClientState *nc, uint8_t macaddr[6])
  122. {
  123. snprintf(nc->info_str, sizeof(nc->info_str),
  124. "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
  125. nc->model,
  126. macaddr[0], macaddr[1], macaddr[2],
  127. macaddr[3], macaddr[4], macaddr[5]);
  128. }
  129. void qemu_macaddr_default_if_unset(MACAddr *macaddr)
  130. {
  131. static int index = 0;
  132. static const MACAddr zero = { .a = { 0,0,0,0,0,0 } };
  133. if (memcmp(macaddr, &zero, sizeof(zero)) != 0)
  134. return;
  135. macaddr->a[0] = 0x52;
  136. macaddr->a[1] = 0x54;
  137. macaddr->a[2] = 0x00;
  138. macaddr->a[3] = 0x12;
  139. macaddr->a[4] = 0x34;
  140. macaddr->a[5] = 0x56 + index++;
  141. }
  142. /**
  143. * Generate a name for net client
  144. *
  145. * Only net clients created with the legacy -net option need this. Naming is
  146. * mandatory for net clients created with -netdev.
  147. */
  148. static char *assign_name(NetClientState *nc1, const char *model)
  149. {
  150. NetClientState *nc;
  151. char buf[256];
  152. int id = 0;
  153. QTAILQ_FOREACH(nc, &net_clients, next) {
  154. if (nc == nc1) {
  155. continue;
  156. }
  157. /* For compatibility only bump id for net clients on a vlan */
  158. if (strcmp(nc->model, model) == 0 &&
  159. net_hub_id_for_client(nc, NULL) == 0) {
  160. id++;
  161. }
  162. }
  163. snprintf(buf, sizeof(buf), "%s.%d", model, id);
  164. return g_strdup(buf);
  165. }
  166. NetClientState *qemu_new_net_client(NetClientInfo *info,
  167. NetClientState *peer,
  168. const char *model,
  169. const char *name)
  170. {
  171. NetClientState *nc;
  172. assert(info->size >= sizeof(NetClientState));
  173. nc = g_malloc0(info->size);
  174. nc->info = info;
  175. nc->model = g_strdup(model);
  176. if (name) {
  177. nc->name = g_strdup(name);
  178. } else {
  179. nc->name = assign_name(nc, model);
  180. }
  181. if (peer) {
  182. assert(!peer->peer);
  183. nc->peer = peer;
  184. peer->peer = nc;
  185. }
  186. QTAILQ_INSERT_TAIL(&net_clients, nc, next);
  187. nc->send_queue = qemu_new_net_queue(nc);
  188. return nc;
  189. }
  190. NICState *qemu_new_nic(NetClientInfo *info,
  191. NICConf *conf,
  192. const char *model,
  193. const char *name,
  194. void *opaque)
  195. {
  196. NetClientState *nc;
  197. NICState *nic;
  198. assert(info->type == NET_CLIENT_OPTIONS_KIND_NIC);
  199. assert(info->size >= sizeof(NICState));
  200. nc = qemu_new_net_client(info, conf->peer, model, name);
  201. nic = DO_UPCAST(NICState, nc, nc);
  202. nic->conf = conf;
  203. nic->opaque = opaque;
  204. return nic;
  205. }
  206. static void qemu_cleanup_net_client(NetClientState *nc)
  207. {
  208. QTAILQ_REMOVE(&net_clients, nc, next);
  209. if (nc->info->cleanup) {
  210. nc->info->cleanup(nc);
  211. }
  212. }
  213. static void qemu_free_net_client(NetClientState *nc)
  214. {
  215. if (nc->send_queue) {
  216. qemu_del_net_queue(nc->send_queue);
  217. }
  218. if (nc->peer) {
  219. nc->peer->peer = NULL;
  220. }
  221. g_free(nc->name);
  222. g_free(nc->model);
  223. g_free(nc);
  224. }
  225. void qemu_del_net_client(NetClientState *nc)
  226. {
  227. /* If there is a peer NIC, delete and cleanup client, but do not free. */
  228. if (nc->peer && nc->peer->info->type == NET_CLIENT_OPTIONS_KIND_NIC) {
  229. NICState *nic = DO_UPCAST(NICState, nc, nc->peer);
  230. if (nic->peer_deleted) {
  231. return;
  232. }
  233. nic->peer_deleted = true;
  234. /* Let NIC know peer is gone. */
  235. nc->peer->link_down = true;
  236. if (nc->peer->info->link_status_changed) {
  237. nc->peer->info->link_status_changed(nc->peer);
  238. }
  239. qemu_cleanup_net_client(nc);
  240. return;
  241. }
  242. /* If this is a peer NIC and peer has already been deleted, free it now. */
  243. if (nc->peer && nc->info->type == NET_CLIENT_OPTIONS_KIND_NIC) {
  244. NICState *nic = DO_UPCAST(NICState, nc, nc);
  245. if (nic->peer_deleted) {
  246. qemu_free_net_client(nc->peer);
  247. }
  248. }
  249. qemu_cleanup_net_client(nc);
  250. qemu_free_net_client(nc);
  251. }
  252. void qemu_foreach_nic(qemu_nic_foreach func, void *opaque)
  253. {
  254. NetClientState *nc;
  255. QTAILQ_FOREACH(nc, &net_clients, next) {
  256. if (nc->info->type == NET_CLIENT_OPTIONS_KIND_NIC) {
  257. func(DO_UPCAST(NICState, nc, nc), opaque);
  258. }
  259. }
  260. }
  261. int qemu_can_send_packet(NetClientState *sender)
  262. {
  263. if (!sender->peer) {
  264. return 1;
  265. }
  266. if (sender->peer->receive_disabled) {
  267. return 0;
  268. } else if (sender->peer->info->can_receive &&
  269. !sender->peer->info->can_receive(sender->peer)) {
  270. return 0;
  271. }
  272. return 1;
  273. }
  274. ssize_t qemu_deliver_packet(NetClientState *sender,
  275. unsigned flags,
  276. const uint8_t *data,
  277. size_t size,
  278. void *opaque)
  279. {
  280. NetClientState *nc = opaque;
  281. ssize_t ret;
  282. if (nc->link_down) {
  283. return size;
  284. }
  285. if (nc->receive_disabled) {
  286. return 0;
  287. }
  288. if (flags & QEMU_NET_PACKET_FLAG_RAW && nc->info->receive_raw) {
  289. ret = nc->info->receive_raw(nc, data, size);
  290. } else {
  291. ret = nc->info->receive(nc, data, size);
  292. }
  293. if (ret == 0) {
  294. nc->receive_disabled = 1;
  295. };
  296. return ret;
  297. }
  298. void qemu_purge_queued_packets(NetClientState *nc)
  299. {
  300. if (!nc->peer) {
  301. return;
  302. }
  303. qemu_net_queue_purge(nc->peer->send_queue, nc);
  304. }
  305. void qemu_flush_queued_packets(NetClientState *nc)
  306. {
  307. nc->receive_disabled = 0;
  308. if (qemu_net_queue_flush(nc->send_queue)) {
  309. /* We emptied the queue successfully, signal to the IO thread to repoll
  310. * the file descriptor (for tap, for example).
  311. */
  312. qemu_notify_event();
  313. }
  314. }
  315. static ssize_t qemu_send_packet_async_with_flags(NetClientState *sender,
  316. unsigned flags,
  317. const uint8_t *buf, int size,
  318. NetPacketSent *sent_cb)
  319. {
  320. NetQueue *queue;
  321. #ifdef DEBUG_NET
  322. printf("qemu_send_packet_async:\n");
  323. hex_dump(stdout, buf, size);
  324. #endif
  325. if (sender->link_down || !sender->peer) {
  326. return size;
  327. }
  328. queue = sender->peer->send_queue;
  329. return qemu_net_queue_send(queue, sender, flags, buf, size, sent_cb);
  330. }
  331. ssize_t qemu_send_packet_async(NetClientState *sender,
  332. const uint8_t *buf, int size,
  333. NetPacketSent *sent_cb)
  334. {
  335. return qemu_send_packet_async_with_flags(sender, QEMU_NET_PACKET_FLAG_NONE,
  336. buf, size, sent_cb);
  337. }
  338. void qemu_send_packet(NetClientState *nc, const uint8_t *buf, int size)
  339. {
  340. qemu_send_packet_async(nc, buf, size, NULL);
  341. }
  342. ssize_t qemu_send_packet_raw(NetClientState *nc, const uint8_t *buf, int size)
  343. {
  344. return qemu_send_packet_async_with_flags(nc, QEMU_NET_PACKET_FLAG_RAW,
  345. buf, size, NULL);
  346. }
  347. static ssize_t nc_sendv_compat(NetClientState *nc, const struct iovec *iov,
  348. int iovcnt)
  349. {
  350. uint8_t buffer[4096];
  351. size_t offset;
  352. offset = iov_to_buf(iov, iovcnt, 0, buffer, sizeof(buffer));
  353. return nc->info->receive(nc, buffer, offset);
  354. }
  355. ssize_t qemu_deliver_packet_iov(NetClientState *sender,
  356. unsigned flags,
  357. const struct iovec *iov,
  358. int iovcnt,
  359. void *opaque)
  360. {
  361. NetClientState *nc = opaque;
  362. int ret;
  363. if (nc->link_down) {
  364. return iov_size(iov, iovcnt);
  365. }
  366. if (nc->receive_disabled) {
  367. return 0;
  368. }
  369. if (nc->info->receive_iov) {
  370. ret = nc->info->receive_iov(nc, iov, iovcnt);
  371. } else {
  372. ret = nc_sendv_compat(nc, iov, iovcnt);
  373. }
  374. if (ret == 0) {
  375. nc->receive_disabled = 1;
  376. }
  377. return ret;
  378. }
  379. ssize_t qemu_sendv_packet_async(NetClientState *sender,
  380. const struct iovec *iov, int iovcnt,
  381. NetPacketSent *sent_cb)
  382. {
  383. NetQueue *queue;
  384. if (sender->link_down || !sender->peer) {
  385. return iov_size(iov, iovcnt);
  386. }
  387. queue = sender->peer->send_queue;
  388. return qemu_net_queue_send_iov(queue, sender,
  389. QEMU_NET_PACKET_FLAG_NONE,
  390. iov, iovcnt, sent_cb);
  391. }
  392. ssize_t
  393. qemu_sendv_packet(NetClientState *nc, const struct iovec *iov, int iovcnt)
  394. {
  395. return qemu_sendv_packet_async(nc, iov, iovcnt, NULL);
  396. }
  397. NetClientState *qemu_find_netdev(const char *id)
  398. {
  399. NetClientState *nc;
  400. QTAILQ_FOREACH(nc, &net_clients, next) {
  401. if (nc->info->type == NET_CLIENT_OPTIONS_KIND_NIC)
  402. continue;
  403. if (!strcmp(nc->name, id)) {
  404. return nc;
  405. }
  406. }
  407. return NULL;
  408. }
  409. static int nic_get_free_idx(void)
  410. {
  411. int index;
  412. for (index = 0; index < MAX_NICS; index++)
  413. if (!nd_table[index].used)
  414. return index;
  415. return -1;
  416. }
  417. int qemu_show_nic_models(const char *arg, const char *const *models)
  418. {
  419. int i;
  420. if (!arg || !is_help_option(arg)) {
  421. return 0;
  422. }
  423. fprintf(stderr, "qemu: Supported NIC models: ");
  424. for (i = 0 ; models[i]; i++)
  425. fprintf(stderr, "%s%c", models[i], models[i+1] ? ',' : '\n');
  426. return 1;
  427. }
  428. void qemu_check_nic_model(NICInfo *nd, const char *model)
  429. {
  430. const char *models[2];
  431. models[0] = model;
  432. models[1] = NULL;
  433. if (qemu_show_nic_models(nd->model, models))
  434. exit(0);
  435. if (qemu_find_nic_model(nd, models, model) < 0)
  436. exit(1);
  437. }
  438. int qemu_find_nic_model(NICInfo *nd, const char * const *models,
  439. const char *default_model)
  440. {
  441. int i;
  442. if (!nd->model)
  443. nd->model = g_strdup(default_model);
  444. for (i = 0 ; models[i]; i++) {
  445. if (strcmp(nd->model, models[i]) == 0)
  446. return i;
  447. }
  448. error_report("Unsupported NIC model: %s", nd->model);
  449. return -1;
  450. }
  451. static int net_init_nic(const NetClientOptions *opts, const char *name,
  452. NetClientState *peer)
  453. {
  454. int idx;
  455. NICInfo *nd;
  456. const NetLegacyNicOptions *nic;
  457. assert(opts->kind == NET_CLIENT_OPTIONS_KIND_NIC);
  458. nic = opts->nic;
  459. idx = nic_get_free_idx();
  460. if (idx == -1 || nb_nics >= MAX_NICS) {
  461. error_report("Too Many NICs");
  462. return -1;
  463. }
  464. nd = &nd_table[idx];
  465. memset(nd, 0, sizeof(*nd));
  466. if (nic->has_netdev) {
  467. nd->netdev = qemu_find_netdev(nic->netdev);
  468. if (!nd->netdev) {
  469. error_report("netdev '%s' not found", nic->netdev);
  470. return -1;
  471. }
  472. } else {
  473. assert(peer);
  474. nd->netdev = peer;
  475. }
  476. if (name) {
  477. nd->name = g_strdup(name);
  478. }
  479. if (nic->has_model) {
  480. nd->model = g_strdup(nic->model);
  481. }
  482. if (nic->has_addr) {
  483. nd->devaddr = g_strdup(nic->addr);
  484. }
  485. if (nic->has_macaddr &&
  486. net_parse_macaddr(nd->macaddr.a, nic->macaddr) < 0) {
  487. error_report("invalid syntax for ethernet address");
  488. return -1;
  489. }
  490. qemu_macaddr_default_if_unset(&nd->macaddr);
  491. if (nic->has_vectors) {
  492. if (nic->vectors > 0x7ffffff) {
  493. error_report("invalid # of vectors: %"PRIu32, nic->vectors);
  494. return -1;
  495. }
  496. nd->nvectors = nic->vectors;
  497. } else {
  498. nd->nvectors = DEV_NVECTORS_UNSPECIFIED;
  499. }
  500. nd->used = 1;
  501. nb_nics++;
  502. return idx;
  503. }
  504. static int (* const net_client_init_fun[NET_CLIENT_OPTIONS_KIND_MAX])(
  505. const NetClientOptions *opts,
  506. const char *name,
  507. NetClientState *peer) = {
  508. [NET_CLIENT_OPTIONS_KIND_NIC] = net_init_nic,
  509. #ifdef CONFIG_SLIRP
  510. [NET_CLIENT_OPTIONS_KIND_USER] = net_init_slirp,
  511. #endif
  512. [NET_CLIENT_OPTIONS_KIND_TAP] = net_init_tap,
  513. [NET_CLIENT_OPTIONS_KIND_SOCKET] = net_init_socket,
  514. #ifdef CONFIG_VDE
  515. [NET_CLIENT_OPTIONS_KIND_VDE] = net_init_vde,
  516. #endif
  517. [NET_CLIENT_OPTIONS_KIND_DUMP] = net_init_dump,
  518. #ifdef CONFIG_NET_BRIDGE
  519. [NET_CLIENT_OPTIONS_KIND_BRIDGE] = net_init_bridge,
  520. #endif
  521. [NET_CLIENT_OPTIONS_KIND_HUBPORT] = net_init_hubport,
  522. };
  523. static int net_client_init1(const void *object, int is_netdev, Error **errp)
  524. {
  525. union {
  526. const Netdev *netdev;
  527. const NetLegacy *net;
  528. } u;
  529. const NetClientOptions *opts;
  530. const char *name;
  531. if (is_netdev) {
  532. u.netdev = object;
  533. opts = u.netdev->opts;
  534. name = u.netdev->id;
  535. switch (opts->kind) {
  536. #ifdef CONFIG_SLIRP
  537. case NET_CLIENT_OPTIONS_KIND_USER:
  538. #endif
  539. case NET_CLIENT_OPTIONS_KIND_TAP:
  540. case NET_CLIENT_OPTIONS_KIND_SOCKET:
  541. #ifdef CONFIG_VDE
  542. case NET_CLIENT_OPTIONS_KIND_VDE:
  543. #endif
  544. #ifdef CONFIG_NET_BRIDGE
  545. case NET_CLIENT_OPTIONS_KIND_BRIDGE:
  546. #endif
  547. case NET_CLIENT_OPTIONS_KIND_HUBPORT:
  548. break;
  549. default:
  550. error_set(errp, QERR_INVALID_PARAMETER_VALUE, "type",
  551. "a netdev backend type");
  552. return -1;
  553. }
  554. } else {
  555. u.net = object;
  556. opts = u.net->opts;
  557. /* missing optional values have been initialized to "all bits zero" */
  558. name = u.net->has_id ? u.net->id : u.net->name;
  559. }
  560. if (net_client_init_fun[opts->kind]) {
  561. NetClientState *peer = NULL;
  562. /* Do not add to a vlan if it's a -netdev or a nic with a netdev=
  563. * parameter. */
  564. if (!is_netdev &&
  565. (opts->kind != NET_CLIENT_OPTIONS_KIND_NIC ||
  566. !opts->nic->has_netdev)) {
  567. peer = net_hub_add_port(u.net->has_vlan ? u.net->vlan : 0, NULL);
  568. }
  569. if (net_client_init_fun[opts->kind](opts, name, peer) < 0) {
  570. /* TODO push error reporting into init() methods */
  571. error_set(errp, QERR_DEVICE_INIT_FAILED,
  572. NetClientOptionsKind_lookup[opts->kind]);
  573. return -1;
  574. }
  575. }
  576. return 0;
  577. }
  578. static void net_visit(Visitor *v, int is_netdev, void **object, Error **errp)
  579. {
  580. if (is_netdev) {
  581. visit_type_Netdev(v, (Netdev **)object, NULL, errp);
  582. } else {
  583. visit_type_NetLegacy(v, (NetLegacy **)object, NULL, errp);
  584. }
  585. }
  586. int net_client_init(QemuOpts *opts, int is_netdev, Error **errp)
  587. {
  588. void *object = NULL;
  589. Error *err = NULL;
  590. int ret = -1;
  591. {
  592. OptsVisitor *ov = opts_visitor_new(opts);
  593. net_visit(opts_get_visitor(ov), is_netdev, &object, &err);
  594. opts_visitor_cleanup(ov);
  595. }
  596. if (!err) {
  597. ret = net_client_init1(object, is_netdev, &err);
  598. }
  599. if (object) {
  600. QapiDeallocVisitor *dv = qapi_dealloc_visitor_new();
  601. net_visit(qapi_dealloc_get_visitor(dv), is_netdev, &object, NULL);
  602. qapi_dealloc_visitor_cleanup(dv);
  603. }
  604. error_propagate(errp, err);
  605. return ret;
  606. }
  607. static int net_host_check_device(const char *device)
  608. {
  609. int i;
  610. const char *valid_param_list[] = { "tap", "socket", "dump"
  611. #ifdef CONFIG_NET_BRIDGE
  612. , "bridge"
  613. #endif
  614. #ifdef CONFIG_SLIRP
  615. ,"user"
  616. #endif
  617. #ifdef CONFIG_VDE
  618. ,"vde"
  619. #endif
  620. };
  621. for (i = 0; i < sizeof(valid_param_list) / sizeof(char *); i++) {
  622. if (!strncmp(valid_param_list[i], device,
  623. strlen(valid_param_list[i])))
  624. return 1;
  625. }
  626. return 0;
  627. }
  628. void net_host_device_add(Monitor *mon, const QDict *qdict)
  629. {
  630. const char *device = qdict_get_str(qdict, "device");
  631. const char *opts_str = qdict_get_try_str(qdict, "opts");
  632. Error *local_err = NULL;
  633. QemuOpts *opts;
  634. if (!net_host_check_device(device)) {
  635. monitor_printf(mon, "invalid host network device %s\n", device);
  636. return;
  637. }
  638. opts = qemu_opts_parse(qemu_find_opts("net"), opts_str ? opts_str : "", 0);
  639. if (!opts) {
  640. return;
  641. }
  642. qemu_opt_set(opts, "type", device);
  643. net_client_init(opts, 0, &local_err);
  644. if (error_is_set(&local_err)) {
  645. qerror_report_err(local_err);
  646. error_free(local_err);
  647. monitor_printf(mon, "adding host network device %s failed\n", device);
  648. }
  649. }
  650. void net_host_device_remove(Monitor *mon, const QDict *qdict)
  651. {
  652. NetClientState *nc;
  653. int vlan_id = qdict_get_int(qdict, "vlan_id");
  654. const char *device = qdict_get_str(qdict, "device");
  655. nc = net_hub_find_client_by_name(vlan_id, device);
  656. if (!nc) {
  657. return;
  658. }
  659. if (!net_host_check_device(nc->model)) {
  660. monitor_printf(mon, "invalid host network device %s\n", device);
  661. return;
  662. }
  663. qemu_del_net_client(nc);
  664. }
  665. void netdev_add(QemuOpts *opts, Error **errp)
  666. {
  667. net_client_init(opts, 1, errp);
  668. }
  669. int qmp_netdev_add(Monitor *mon, const QDict *qdict, QObject **ret)
  670. {
  671. Error *local_err = NULL;
  672. QemuOptsList *opts_list;
  673. QemuOpts *opts;
  674. opts_list = qemu_find_opts_err("netdev", &local_err);
  675. if (error_is_set(&local_err)) {
  676. goto exit_err;
  677. }
  678. opts = qemu_opts_from_qdict(opts_list, qdict, &local_err);
  679. if (error_is_set(&local_err)) {
  680. goto exit_err;
  681. }
  682. netdev_add(opts, &local_err);
  683. if (error_is_set(&local_err)) {
  684. qemu_opts_del(opts);
  685. goto exit_err;
  686. }
  687. return 0;
  688. exit_err:
  689. qerror_report_err(local_err);
  690. error_free(local_err);
  691. return -1;
  692. }
  693. void qmp_netdev_del(const char *id, Error **errp)
  694. {
  695. NetClientState *nc;
  696. QemuOpts *opts;
  697. nc = qemu_find_netdev(id);
  698. if (!nc) {
  699. error_set(errp, QERR_DEVICE_NOT_FOUND, id);
  700. return;
  701. }
  702. opts = qemu_opts_find(qemu_find_opts_err("netdev", NULL), id);
  703. if (!opts) {
  704. error_setg(errp, "Device '%s' is not a netdev", id);
  705. return;
  706. }
  707. qemu_del_net_client(nc);
  708. qemu_opts_del(opts);
  709. }
  710. void print_net_client(Monitor *mon, NetClientState *nc)
  711. {
  712. monitor_printf(mon, "%s: type=%s,%s\n", nc->name,
  713. NetClientOptionsKind_lookup[nc->info->type], nc->info_str);
  714. }
  715. void do_info_network(Monitor *mon)
  716. {
  717. NetClientState *nc, *peer;
  718. NetClientOptionsKind type;
  719. net_hub_info(mon);
  720. QTAILQ_FOREACH(nc, &net_clients, next) {
  721. peer = nc->peer;
  722. type = nc->info->type;
  723. /* Skip if already printed in hub info */
  724. if (net_hub_id_for_client(nc, NULL) == 0) {
  725. continue;
  726. }
  727. if (!peer || type == NET_CLIENT_OPTIONS_KIND_NIC) {
  728. print_net_client(mon, nc);
  729. } /* else it's a netdev connected to a NIC, printed with the NIC */
  730. if (peer && type == NET_CLIENT_OPTIONS_KIND_NIC) {
  731. monitor_printf(mon, " \\ ");
  732. print_net_client(mon, peer);
  733. }
  734. }
  735. }
  736. void qmp_set_link(const char *name, bool up, Error **errp)
  737. {
  738. NetClientState *nc = NULL;
  739. QTAILQ_FOREACH(nc, &net_clients, next) {
  740. if (!strcmp(nc->name, name)) {
  741. goto done;
  742. }
  743. }
  744. done:
  745. if (!nc) {
  746. error_set(errp, QERR_DEVICE_NOT_FOUND, name);
  747. return;
  748. }
  749. nc->link_down = !up;
  750. if (nc->info->link_status_changed) {
  751. nc->info->link_status_changed(nc);
  752. }
  753. /* Notify peer. Don't update peer link status: this makes it possible to
  754. * disconnect from host network without notifying the guest.
  755. * FIXME: is disconnected link status change operation useful?
  756. *
  757. * Current behaviour is compatible with qemu vlans where there could be
  758. * multiple clients that can still communicate with each other in
  759. * disconnected mode. For now maintain this compatibility. */
  760. if (nc->peer && nc->peer->info->link_status_changed) {
  761. nc->peer->info->link_status_changed(nc->peer);
  762. }
  763. }
  764. void net_cleanup(void)
  765. {
  766. NetClientState *nc, *next_vc;
  767. QTAILQ_FOREACH_SAFE(nc, &net_clients, next, next_vc) {
  768. qemu_del_net_client(nc);
  769. }
  770. }
  771. void net_check_clients(void)
  772. {
  773. NetClientState *nc;
  774. int i;
  775. /* Don't warn about the default network setup that you get if
  776. * no command line -net or -netdev options are specified. There
  777. * are two cases that we would otherwise complain about:
  778. * (1) board doesn't support a NIC but the implicit "-net nic"
  779. * requested one
  780. * (2) CONFIG_SLIRP not set, in which case the implicit "-net nic"
  781. * sets up a nic that isn't connected to anything.
  782. */
  783. if (default_net) {
  784. return;
  785. }
  786. net_hub_check_clients();
  787. QTAILQ_FOREACH(nc, &net_clients, next) {
  788. if (!nc->peer) {
  789. fprintf(stderr, "Warning: %s %s has no peer\n",
  790. nc->info->type == NET_CLIENT_OPTIONS_KIND_NIC ?
  791. "nic" : "netdev", nc->name);
  792. }
  793. }
  794. /* Check that all NICs requested via -net nic actually got created.
  795. * NICs created via -device don't need to be checked here because
  796. * they are always instantiated.
  797. */
  798. for (i = 0; i < MAX_NICS; i++) {
  799. NICInfo *nd = &nd_table[i];
  800. if (nd->used && !nd->instantiated) {
  801. fprintf(stderr, "Warning: requested NIC (%s, model %s) "
  802. "was not created (not supported by this machine?)\n",
  803. nd->name ? nd->name : "anonymous",
  804. nd->model ? nd->model : "unspecified");
  805. }
  806. }
  807. }
  808. static int net_init_client(QemuOpts *opts, void *dummy)
  809. {
  810. Error *local_err = NULL;
  811. net_client_init(opts, 0, &local_err);
  812. if (error_is_set(&local_err)) {
  813. qerror_report_err(local_err);
  814. error_free(local_err);
  815. return -1;
  816. }
  817. return 0;
  818. }
  819. static int net_init_netdev(QemuOpts *opts, void *dummy)
  820. {
  821. Error *local_err = NULL;
  822. int ret;
  823. ret = net_client_init(opts, 1, &local_err);
  824. if (error_is_set(&local_err)) {
  825. qerror_report_err(local_err);
  826. error_free(local_err);
  827. return -1;
  828. }
  829. return ret;
  830. }
  831. int net_init_clients(void)
  832. {
  833. QemuOptsList *net = qemu_find_opts("net");
  834. if (default_net) {
  835. /* if no clients, we use a default config */
  836. qemu_opts_set(net, NULL, "type", "nic");
  837. #ifdef CONFIG_SLIRP
  838. qemu_opts_set(net, NULL, "type", "user");
  839. #endif
  840. }
  841. QTAILQ_INIT(&net_clients);
  842. if (qemu_opts_foreach(qemu_find_opts("netdev"), net_init_netdev, NULL, 1) == -1)
  843. return -1;
  844. if (qemu_opts_foreach(net, net_init_client, NULL, 1) == -1) {
  845. return -1;
  846. }
  847. return 0;
  848. }
  849. int net_client_parse(QemuOptsList *opts_list, const char *optarg)
  850. {
  851. #if defined(CONFIG_SLIRP)
  852. int ret;
  853. if (net_slirp_parse_legacy(opts_list, optarg, &ret)) {
  854. return ret;
  855. }
  856. #endif
  857. if (!qemu_opts_parse(opts_list, optarg, 1)) {
  858. return -1;
  859. }
  860. default_net = 0;
  861. return 0;
  862. }
  863. /* From FreeBSD */
  864. /* XXX: optimize */
  865. unsigned compute_mcast_idx(const uint8_t *ep)
  866. {
  867. uint32_t crc;
  868. int carry, i, j;
  869. uint8_t b;
  870. crc = 0xffffffff;
  871. for (i = 0; i < 6; i++) {
  872. b = *ep++;
  873. for (j = 0; j < 8; j++) {
  874. carry = ((crc & 0x80000000L) ? 1 : 0) ^ (b & 0x01);
  875. crc <<= 1;
  876. b >>= 1;
  877. if (carry) {
  878. crc = ((crc ^ POLYNOMIAL) | carry);
  879. }
  880. }
  881. }
  882. return crc >> 26;
  883. }