net.c 26 KB

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