net.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671
  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 "qemu/osdep.h"
  25. #include "net/net.h"
  26. #include "clients.h"
  27. #include "hub.h"
  28. #include "net/slirp.h"
  29. #include "net/eth.h"
  30. #include "util.h"
  31. #include "monitor/monitor.h"
  32. #include "qemu-common.h"
  33. #include "qemu/help_option.h"
  34. #include "qapi/qmp/qerror.h"
  35. #include "qemu/error-report.h"
  36. #include "qemu/sockets.h"
  37. #include "qemu/cutils.h"
  38. #include "qemu/config-file.h"
  39. #include "qmp-commands.h"
  40. #include "hw/qdev.h"
  41. #include "qemu/iov.h"
  42. #include "qemu/main-loop.h"
  43. #include "qapi-visit.h"
  44. #include "qapi/opts-visitor.h"
  45. #include "sysemu/sysemu.h"
  46. #include "net/filter.h"
  47. #include "qapi/string-output-visitor.h"
  48. /* Net bridge is currently not supported for W32. */
  49. #if !defined(_WIN32)
  50. # define CONFIG_NET_BRIDGE
  51. #endif
  52. static VMChangeStateEntry *net_change_state_entry;
  53. static QTAILQ_HEAD(, NetClientState) net_clients;
  54. const char *host_net_devices[] = {
  55. "tap",
  56. "socket",
  57. "dump",
  58. #ifdef CONFIG_NET_BRIDGE
  59. "bridge",
  60. #endif
  61. #ifdef CONFIG_NETMAP
  62. "netmap",
  63. #endif
  64. #ifdef CONFIG_SLIRP
  65. "user",
  66. #endif
  67. #ifdef CONFIG_VDE
  68. "vde",
  69. #endif
  70. "vhost-user",
  71. NULL,
  72. };
  73. /***********************************************************/
  74. /* network device redirectors */
  75. static int get_str_sep(char *buf, int buf_size, const char **pp, int sep)
  76. {
  77. const char *p, *p1;
  78. int len;
  79. p = *pp;
  80. p1 = strchr(p, sep);
  81. if (!p1)
  82. return -1;
  83. len = p1 - p;
  84. p1++;
  85. if (buf_size > 0) {
  86. if (len > buf_size - 1)
  87. len = buf_size - 1;
  88. memcpy(buf, p, len);
  89. buf[len] = '\0';
  90. }
  91. *pp = p1;
  92. return 0;
  93. }
  94. int parse_host_port(struct sockaddr_in *saddr, const char *str)
  95. {
  96. char buf[512];
  97. struct hostent *he;
  98. const char *p, *r;
  99. int port;
  100. p = str;
  101. if (get_str_sep(buf, sizeof(buf), &p, ':') < 0)
  102. return -1;
  103. saddr->sin_family = AF_INET;
  104. if (buf[0] == '\0') {
  105. saddr->sin_addr.s_addr = 0;
  106. } else {
  107. if (qemu_isdigit(buf[0])) {
  108. if (!inet_aton(buf, &saddr->sin_addr))
  109. return -1;
  110. } else {
  111. if ((he = gethostbyname(buf)) == NULL)
  112. return - 1;
  113. saddr->sin_addr = *(struct in_addr *)he->h_addr;
  114. }
  115. }
  116. port = strtol(p, (char **)&r, 0);
  117. if (r == p)
  118. return -1;
  119. saddr->sin_port = htons(port);
  120. return 0;
  121. }
  122. char *qemu_mac_strdup_printf(const uint8_t *macaddr)
  123. {
  124. return g_strdup_printf("%.2x:%.2x:%.2x:%.2x:%.2x:%.2x",
  125. macaddr[0], macaddr[1], macaddr[2],
  126. macaddr[3], macaddr[4], macaddr[5]);
  127. }
  128. void qemu_format_nic_info_str(NetClientState *nc, uint8_t macaddr[6])
  129. {
  130. snprintf(nc->info_str, sizeof(nc->info_str),
  131. "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
  132. nc->model,
  133. macaddr[0], macaddr[1], macaddr[2],
  134. macaddr[3], macaddr[4], macaddr[5]);
  135. }
  136. static int mac_table[256] = {0};
  137. static void qemu_macaddr_set_used(MACAddr *macaddr)
  138. {
  139. int index;
  140. for (index = 0x56; index < 0xFF; index++) {
  141. if (macaddr->a[5] == index) {
  142. mac_table[index]++;
  143. }
  144. }
  145. }
  146. static void qemu_macaddr_set_free(MACAddr *macaddr)
  147. {
  148. int index;
  149. static const MACAddr base = { .a = { 0x52, 0x54, 0x00, 0x12, 0x34, 0 } };
  150. if (memcmp(macaddr->a, &base.a, (sizeof(base.a) - 1)) != 0) {
  151. return;
  152. }
  153. for (index = 0x56; index < 0xFF; index++) {
  154. if (macaddr->a[5] == index) {
  155. mac_table[index]--;
  156. }
  157. }
  158. }
  159. static int qemu_macaddr_get_free(void)
  160. {
  161. int index;
  162. for (index = 0x56; index < 0xFF; index++) {
  163. if (mac_table[index] == 0) {
  164. return index;
  165. }
  166. }
  167. return -1;
  168. }
  169. void qemu_macaddr_default_if_unset(MACAddr *macaddr)
  170. {
  171. static const MACAddr zero = { .a = { 0,0,0,0,0,0 } };
  172. static const MACAddr base = { .a = { 0x52, 0x54, 0x00, 0x12, 0x34, 0 } };
  173. if (memcmp(macaddr, &zero, sizeof(zero)) != 0) {
  174. if (memcmp(macaddr->a, &base.a, (sizeof(base.a) - 1)) != 0) {
  175. return;
  176. } else {
  177. qemu_macaddr_set_used(macaddr);
  178. return;
  179. }
  180. }
  181. macaddr->a[0] = 0x52;
  182. macaddr->a[1] = 0x54;
  183. macaddr->a[2] = 0x00;
  184. macaddr->a[3] = 0x12;
  185. macaddr->a[4] = 0x34;
  186. macaddr->a[5] = qemu_macaddr_get_free();
  187. qemu_macaddr_set_used(macaddr);
  188. }
  189. /**
  190. * Generate a name for net client
  191. *
  192. * Only net clients created with the legacy -net option and NICs need this.
  193. */
  194. static char *assign_name(NetClientState *nc1, const char *model)
  195. {
  196. NetClientState *nc;
  197. int id = 0;
  198. QTAILQ_FOREACH(nc, &net_clients, next) {
  199. if (nc == nc1) {
  200. continue;
  201. }
  202. if (strcmp(nc->model, model) == 0) {
  203. id++;
  204. }
  205. }
  206. return g_strdup_printf("%s.%d", model, id);
  207. }
  208. static void qemu_net_client_destructor(NetClientState *nc)
  209. {
  210. g_free(nc);
  211. }
  212. static void qemu_net_client_setup(NetClientState *nc,
  213. NetClientInfo *info,
  214. NetClientState *peer,
  215. const char *model,
  216. const char *name,
  217. NetClientDestructor *destructor)
  218. {
  219. nc->info = info;
  220. nc->model = g_strdup(model);
  221. if (name) {
  222. nc->name = g_strdup(name);
  223. } else {
  224. nc->name = assign_name(nc, model);
  225. }
  226. if (peer) {
  227. assert(!peer->peer);
  228. nc->peer = peer;
  229. peer->peer = nc;
  230. }
  231. QTAILQ_INSERT_TAIL(&net_clients, nc, next);
  232. nc->incoming_queue = qemu_new_net_queue(qemu_deliver_packet_iov, nc);
  233. nc->destructor = destructor;
  234. QTAILQ_INIT(&nc->filters);
  235. }
  236. NetClientState *qemu_new_net_client(NetClientInfo *info,
  237. NetClientState *peer,
  238. const char *model,
  239. const char *name)
  240. {
  241. NetClientState *nc;
  242. assert(info->size >= sizeof(NetClientState));
  243. nc = g_malloc0(info->size);
  244. qemu_net_client_setup(nc, info, peer, model, name,
  245. qemu_net_client_destructor);
  246. return nc;
  247. }
  248. NICState *qemu_new_nic(NetClientInfo *info,
  249. NICConf *conf,
  250. const char *model,
  251. const char *name,
  252. void *opaque)
  253. {
  254. NetClientState **peers = conf->peers.ncs;
  255. NICState *nic;
  256. int i, queues = MAX(1, conf->peers.queues);
  257. assert(info->type == NET_CLIENT_DRIVER_NIC);
  258. assert(info->size >= sizeof(NICState));
  259. nic = g_malloc0(info->size + sizeof(NetClientState) * queues);
  260. nic->ncs = (void *)nic + info->size;
  261. nic->conf = conf;
  262. nic->opaque = opaque;
  263. for (i = 0; i < queues; i++) {
  264. qemu_net_client_setup(&nic->ncs[i], info, peers[i], model, name,
  265. NULL);
  266. nic->ncs[i].queue_index = i;
  267. }
  268. return nic;
  269. }
  270. NetClientState *qemu_get_subqueue(NICState *nic, int queue_index)
  271. {
  272. return nic->ncs + queue_index;
  273. }
  274. NetClientState *qemu_get_queue(NICState *nic)
  275. {
  276. return qemu_get_subqueue(nic, 0);
  277. }
  278. NICState *qemu_get_nic(NetClientState *nc)
  279. {
  280. NetClientState *nc0 = nc - nc->queue_index;
  281. return (NICState *)((void *)nc0 - nc->info->size);
  282. }
  283. void *qemu_get_nic_opaque(NetClientState *nc)
  284. {
  285. NICState *nic = qemu_get_nic(nc);
  286. return nic->opaque;
  287. }
  288. static void qemu_cleanup_net_client(NetClientState *nc)
  289. {
  290. QTAILQ_REMOVE(&net_clients, nc, next);
  291. if (nc->info->cleanup) {
  292. nc->info->cleanup(nc);
  293. }
  294. }
  295. static void qemu_free_net_client(NetClientState *nc)
  296. {
  297. if (nc->incoming_queue) {
  298. qemu_del_net_queue(nc->incoming_queue);
  299. }
  300. if (nc->peer) {
  301. nc->peer->peer = NULL;
  302. }
  303. g_free(nc->name);
  304. g_free(nc->model);
  305. if (nc->destructor) {
  306. nc->destructor(nc);
  307. }
  308. }
  309. void qemu_del_net_client(NetClientState *nc)
  310. {
  311. NetClientState *ncs[MAX_QUEUE_NUM];
  312. int queues, i;
  313. NetFilterState *nf, *next;
  314. assert(nc->info->type != NET_CLIENT_DRIVER_NIC);
  315. /* If the NetClientState belongs to a multiqueue backend, we will change all
  316. * other NetClientStates also.
  317. */
  318. queues = qemu_find_net_clients_except(nc->name, ncs,
  319. NET_CLIENT_DRIVER_NIC,
  320. MAX_QUEUE_NUM);
  321. assert(queues != 0);
  322. QTAILQ_FOREACH_SAFE(nf, &nc->filters, next, next) {
  323. object_unparent(OBJECT(nf));
  324. }
  325. /* If there is a peer NIC, delete and cleanup client, but do not free. */
  326. if (nc->peer && nc->peer->info->type == NET_CLIENT_DRIVER_NIC) {
  327. NICState *nic = qemu_get_nic(nc->peer);
  328. if (nic->peer_deleted) {
  329. return;
  330. }
  331. nic->peer_deleted = true;
  332. for (i = 0; i < queues; i++) {
  333. ncs[i]->peer->link_down = true;
  334. }
  335. if (nc->peer->info->link_status_changed) {
  336. nc->peer->info->link_status_changed(nc->peer);
  337. }
  338. for (i = 0; i < queues; i++) {
  339. qemu_cleanup_net_client(ncs[i]);
  340. }
  341. return;
  342. }
  343. for (i = 0; i < queues; i++) {
  344. qemu_cleanup_net_client(ncs[i]);
  345. qemu_free_net_client(ncs[i]);
  346. }
  347. }
  348. void qemu_del_nic(NICState *nic)
  349. {
  350. int i, queues = MAX(nic->conf->peers.queues, 1);
  351. qemu_macaddr_set_free(&nic->conf->macaddr);
  352. /* If this is a peer NIC and peer has already been deleted, free it now. */
  353. if (nic->peer_deleted) {
  354. for (i = 0; i < queues; i++) {
  355. qemu_free_net_client(qemu_get_subqueue(nic, i)->peer);
  356. }
  357. }
  358. for (i = queues - 1; i >= 0; i--) {
  359. NetClientState *nc = qemu_get_subqueue(nic, i);
  360. qemu_cleanup_net_client(nc);
  361. qemu_free_net_client(nc);
  362. }
  363. g_free(nic);
  364. }
  365. void qemu_foreach_nic(qemu_nic_foreach func, void *opaque)
  366. {
  367. NetClientState *nc;
  368. QTAILQ_FOREACH(nc, &net_clients, next) {
  369. if (nc->info->type == NET_CLIENT_DRIVER_NIC) {
  370. if (nc->queue_index == 0) {
  371. func(qemu_get_nic(nc), opaque);
  372. }
  373. }
  374. }
  375. }
  376. bool qemu_has_ufo(NetClientState *nc)
  377. {
  378. if (!nc || !nc->info->has_ufo) {
  379. return false;
  380. }
  381. return nc->info->has_ufo(nc);
  382. }
  383. bool qemu_has_vnet_hdr(NetClientState *nc)
  384. {
  385. if (!nc || !nc->info->has_vnet_hdr) {
  386. return false;
  387. }
  388. return nc->info->has_vnet_hdr(nc);
  389. }
  390. bool qemu_has_vnet_hdr_len(NetClientState *nc, int len)
  391. {
  392. if (!nc || !nc->info->has_vnet_hdr_len) {
  393. return false;
  394. }
  395. return nc->info->has_vnet_hdr_len(nc, len);
  396. }
  397. void qemu_using_vnet_hdr(NetClientState *nc, bool enable)
  398. {
  399. if (!nc || !nc->info->using_vnet_hdr) {
  400. return;
  401. }
  402. nc->info->using_vnet_hdr(nc, enable);
  403. }
  404. void qemu_set_offload(NetClientState *nc, int csum, int tso4, int tso6,
  405. int ecn, int ufo)
  406. {
  407. if (!nc || !nc->info->set_offload) {
  408. return;
  409. }
  410. nc->info->set_offload(nc, csum, tso4, tso6, ecn, ufo);
  411. }
  412. void qemu_set_vnet_hdr_len(NetClientState *nc, int len)
  413. {
  414. if (!nc || !nc->info->set_vnet_hdr_len) {
  415. return;
  416. }
  417. nc->info->set_vnet_hdr_len(nc, len);
  418. }
  419. int qemu_set_vnet_le(NetClientState *nc, bool is_le)
  420. {
  421. #ifdef HOST_WORDS_BIGENDIAN
  422. if (!nc || !nc->info->set_vnet_le) {
  423. return -ENOSYS;
  424. }
  425. return nc->info->set_vnet_le(nc, is_le);
  426. #else
  427. return 0;
  428. #endif
  429. }
  430. int qemu_set_vnet_be(NetClientState *nc, bool is_be)
  431. {
  432. #ifdef HOST_WORDS_BIGENDIAN
  433. return 0;
  434. #else
  435. if (!nc || !nc->info->set_vnet_be) {
  436. return -ENOSYS;
  437. }
  438. return nc->info->set_vnet_be(nc, is_be);
  439. #endif
  440. }
  441. int qemu_can_send_packet(NetClientState *sender)
  442. {
  443. int vm_running = runstate_is_running();
  444. if (!vm_running) {
  445. return 0;
  446. }
  447. if (!sender->peer) {
  448. return 1;
  449. }
  450. if (sender->peer->receive_disabled) {
  451. return 0;
  452. } else if (sender->peer->info->can_receive &&
  453. !sender->peer->info->can_receive(sender->peer)) {
  454. return 0;
  455. }
  456. return 1;
  457. }
  458. static ssize_t filter_receive_iov(NetClientState *nc,
  459. NetFilterDirection direction,
  460. NetClientState *sender,
  461. unsigned flags,
  462. const struct iovec *iov,
  463. int iovcnt,
  464. NetPacketSent *sent_cb)
  465. {
  466. ssize_t ret = 0;
  467. NetFilterState *nf = NULL;
  468. if (direction == NET_FILTER_DIRECTION_TX) {
  469. QTAILQ_FOREACH(nf, &nc->filters, next) {
  470. ret = qemu_netfilter_receive(nf, direction, sender, flags, iov,
  471. iovcnt, sent_cb);
  472. if (ret) {
  473. return ret;
  474. }
  475. }
  476. } else {
  477. QTAILQ_FOREACH_REVERSE(nf, &nc->filters, NetFilterHead, next) {
  478. ret = qemu_netfilter_receive(nf, direction, sender, flags, iov,
  479. iovcnt, sent_cb);
  480. if (ret) {
  481. return ret;
  482. }
  483. }
  484. }
  485. return ret;
  486. }
  487. static ssize_t filter_receive(NetClientState *nc,
  488. NetFilterDirection direction,
  489. NetClientState *sender,
  490. unsigned flags,
  491. const uint8_t *data,
  492. size_t size,
  493. NetPacketSent *sent_cb)
  494. {
  495. struct iovec iov = {
  496. .iov_base = (void *)data,
  497. .iov_len = size
  498. };
  499. return filter_receive_iov(nc, direction, sender, flags, &iov, 1, sent_cb);
  500. }
  501. void qemu_purge_queued_packets(NetClientState *nc)
  502. {
  503. if (!nc->peer) {
  504. return;
  505. }
  506. qemu_net_queue_purge(nc->peer->incoming_queue, nc);
  507. }
  508. static
  509. void qemu_flush_or_purge_queued_packets(NetClientState *nc, bool purge)
  510. {
  511. nc->receive_disabled = 0;
  512. if (nc->peer && nc->peer->info->type == NET_CLIENT_DRIVER_HUBPORT) {
  513. if (net_hub_flush(nc->peer)) {
  514. qemu_notify_event();
  515. }
  516. }
  517. if (qemu_net_queue_flush(nc->incoming_queue)) {
  518. /* We emptied the queue successfully, signal to the IO thread to repoll
  519. * the file descriptor (for tap, for example).
  520. */
  521. qemu_notify_event();
  522. } else if (purge) {
  523. /* Unable to empty the queue, purge remaining packets */
  524. qemu_net_queue_purge(nc->incoming_queue, nc);
  525. }
  526. }
  527. void qemu_flush_queued_packets(NetClientState *nc)
  528. {
  529. qemu_flush_or_purge_queued_packets(nc, false);
  530. }
  531. static ssize_t qemu_send_packet_async_with_flags(NetClientState *sender,
  532. unsigned flags,
  533. const uint8_t *buf, int size,
  534. NetPacketSent *sent_cb)
  535. {
  536. NetQueue *queue;
  537. int ret;
  538. #ifdef DEBUG_NET
  539. printf("qemu_send_packet_async:\n");
  540. qemu_hexdump((const char *)buf, stdout, "net", size);
  541. #endif
  542. if (sender->link_down || !sender->peer) {
  543. return size;
  544. }
  545. /* Let filters handle the packet first */
  546. ret = filter_receive(sender, NET_FILTER_DIRECTION_TX,
  547. sender, flags, buf, size, sent_cb);
  548. if (ret) {
  549. return ret;
  550. }
  551. ret = filter_receive(sender->peer, NET_FILTER_DIRECTION_RX,
  552. sender, flags, buf, size, sent_cb);
  553. if (ret) {
  554. return ret;
  555. }
  556. queue = sender->peer->incoming_queue;
  557. return qemu_net_queue_send(queue, sender, flags, buf, size, sent_cb);
  558. }
  559. ssize_t qemu_send_packet_async(NetClientState *sender,
  560. const uint8_t *buf, int size,
  561. NetPacketSent *sent_cb)
  562. {
  563. return qemu_send_packet_async_with_flags(sender, QEMU_NET_PACKET_FLAG_NONE,
  564. buf, size, sent_cb);
  565. }
  566. void qemu_send_packet(NetClientState *nc, const uint8_t *buf, int size)
  567. {
  568. qemu_send_packet_async(nc, buf, size, NULL);
  569. }
  570. ssize_t qemu_send_packet_raw(NetClientState *nc, const uint8_t *buf, int size)
  571. {
  572. return qemu_send_packet_async_with_flags(nc, QEMU_NET_PACKET_FLAG_RAW,
  573. buf, size, NULL);
  574. }
  575. static ssize_t nc_sendv_compat(NetClientState *nc, const struct iovec *iov,
  576. int iovcnt, unsigned flags)
  577. {
  578. uint8_t *buf = NULL;
  579. uint8_t *buffer;
  580. size_t offset;
  581. ssize_t ret;
  582. if (iovcnt == 1) {
  583. buffer = iov[0].iov_base;
  584. offset = iov[0].iov_len;
  585. } else {
  586. offset = iov_size(iov, iovcnt);
  587. if (offset > NET_BUFSIZE) {
  588. return -1;
  589. }
  590. buf = g_malloc(offset);
  591. buffer = buf;
  592. offset = iov_to_buf(iov, iovcnt, 0, buf, offset);
  593. }
  594. if (flags & QEMU_NET_PACKET_FLAG_RAW && nc->info->receive_raw) {
  595. ret = nc->info->receive_raw(nc, buffer, offset);
  596. } else {
  597. ret = nc->info->receive(nc, buffer, offset);
  598. }
  599. g_free(buf);
  600. return ret;
  601. }
  602. ssize_t qemu_deliver_packet_iov(NetClientState *sender,
  603. unsigned flags,
  604. const struct iovec *iov,
  605. int iovcnt,
  606. void *opaque)
  607. {
  608. NetClientState *nc = opaque;
  609. int ret;
  610. if (nc->link_down) {
  611. return iov_size(iov, iovcnt);
  612. }
  613. if (nc->receive_disabled) {
  614. return 0;
  615. }
  616. if (nc->info->receive_iov && !(flags & QEMU_NET_PACKET_FLAG_RAW)) {
  617. ret = nc->info->receive_iov(nc, iov, iovcnt);
  618. } else {
  619. ret = nc_sendv_compat(nc, iov, iovcnt, flags);
  620. }
  621. if (ret == 0) {
  622. nc->receive_disabled = 1;
  623. }
  624. return ret;
  625. }
  626. ssize_t qemu_sendv_packet_async(NetClientState *sender,
  627. const struct iovec *iov, int iovcnt,
  628. NetPacketSent *sent_cb)
  629. {
  630. NetQueue *queue;
  631. int ret;
  632. if (sender->link_down || !sender->peer) {
  633. return iov_size(iov, iovcnt);
  634. }
  635. /* Let filters handle the packet first */
  636. ret = filter_receive_iov(sender, NET_FILTER_DIRECTION_TX, sender,
  637. QEMU_NET_PACKET_FLAG_NONE, iov, iovcnt, sent_cb);
  638. if (ret) {
  639. return ret;
  640. }
  641. ret = filter_receive_iov(sender->peer, NET_FILTER_DIRECTION_RX, sender,
  642. QEMU_NET_PACKET_FLAG_NONE, iov, iovcnt, sent_cb);
  643. if (ret) {
  644. return ret;
  645. }
  646. queue = sender->peer->incoming_queue;
  647. return qemu_net_queue_send_iov(queue, sender,
  648. QEMU_NET_PACKET_FLAG_NONE,
  649. iov, iovcnt, sent_cb);
  650. }
  651. ssize_t
  652. qemu_sendv_packet(NetClientState *nc, const struct iovec *iov, int iovcnt)
  653. {
  654. return qemu_sendv_packet_async(nc, iov, iovcnt, NULL);
  655. }
  656. NetClientState *qemu_find_netdev(const char *id)
  657. {
  658. NetClientState *nc;
  659. QTAILQ_FOREACH(nc, &net_clients, next) {
  660. if (nc->info->type == NET_CLIENT_DRIVER_NIC)
  661. continue;
  662. if (!strcmp(nc->name, id)) {
  663. return nc;
  664. }
  665. }
  666. return NULL;
  667. }
  668. int qemu_find_net_clients_except(const char *id, NetClientState **ncs,
  669. NetClientDriver type, int max)
  670. {
  671. NetClientState *nc;
  672. int ret = 0;
  673. QTAILQ_FOREACH(nc, &net_clients, next) {
  674. if (nc->info->type == type) {
  675. continue;
  676. }
  677. if (!id || !strcmp(nc->name, id)) {
  678. if (ret < max) {
  679. ncs[ret] = nc;
  680. }
  681. ret++;
  682. }
  683. }
  684. return ret;
  685. }
  686. static int nic_get_free_idx(void)
  687. {
  688. int index;
  689. for (index = 0; index < MAX_NICS; index++)
  690. if (!nd_table[index].used)
  691. return index;
  692. return -1;
  693. }
  694. int qemu_show_nic_models(const char *arg, const char *const *models)
  695. {
  696. int i;
  697. if (!arg || !is_help_option(arg)) {
  698. return 0;
  699. }
  700. fprintf(stderr, "qemu: Supported NIC models: ");
  701. for (i = 0 ; models[i]; i++)
  702. fprintf(stderr, "%s%c", models[i], models[i+1] ? ',' : '\n');
  703. return 1;
  704. }
  705. void qemu_check_nic_model(NICInfo *nd, const char *model)
  706. {
  707. const char *models[2];
  708. models[0] = model;
  709. models[1] = NULL;
  710. if (qemu_show_nic_models(nd->model, models))
  711. exit(0);
  712. if (qemu_find_nic_model(nd, models, model) < 0)
  713. exit(1);
  714. }
  715. int qemu_find_nic_model(NICInfo *nd, const char * const *models,
  716. const char *default_model)
  717. {
  718. int i;
  719. if (!nd->model)
  720. nd->model = g_strdup(default_model);
  721. for (i = 0 ; models[i]; i++) {
  722. if (strcmp(nd->model, models[i]) == 0)
  723. return i;
  724. }
  725. error_report("Unsupported NIC model: %s", nd->model);
  726. return -1;
  727. }
  728. static int net_init_nic(const Netdev *netdev, const char *name,
  729. NetClientState *peer, Error **errp)
  730. {
  731. int idx;
  732. NICInfo *nd;
  733. const NetLegacyNicOptions *nic;
  734. assert(netdev->type == NET_CLIENT_DRIVER_NIC);
  735. nic = &netdev->u.nic;
  736. idx = nic_get_free_idx();
  737. if (idx == -1 || nb_nics >= MAX_NICS) {
  738. error_setg(errp, "too many NICs");
  739. return -1;
  740. }
  741. nd = &nd_table[idx];
  742. memset(nd, 0, sizeof(*nd));
  743. if (nic->has_netdev) {
  744. nd->netdev = qemu_find_netdev(nic->netdev);
  745. if (!nd->netdev) {
  746. error_setg(errp, "netdev '%s' not found", nic->netdev);
  747. return -1;
  748. }
  749. } else {
  750. assert(peer);
  751. nd->netdev = peer;
  752. }
  753. nd->name = g_strdup(name);
  754. if (nic->has_model) {
  755. nd->model = g_strdup(nic->model);
  756. }
  757. if (nic->has_addr) {
  758. nd->devaddr = g_strdup(nic->addr);
  759. }
  760. if (nic->has_macaddr &&
  761. net_parse_macaddr(nd->macaddr.a, nic->macaddr) < 0) {
  762. error_setg(errp, "invalid syntax for ethernet address");
  763. return -1;
  764. }
  765. if (nic->has_macaddr &&
  766. is_multicast_ether_addr(nd->macaddr.a)) {
  767. error_setg(errp,
  768. "NIC cannot have multicast MAC address (odd 1st byte)");
  769. return -1;
  770. }
  771. qemu_macaddr_default_if_unset(&nd->macaddr);
  772. if (nic->has_vectors) {
  773. if (nic->vectors > 0x7ffffff) {
  774. error_setg(errp, "invalid # of vectors: %"PRIu32, nic->vectors);
  775. return -1;
  776. }
  777. nd->nvectors = nic->vectors;
  778. } else {
  779. nd->nvectors = DEV_NVECTORS_UNSPECIFIED;
  780. }
  781. nd->used = 1;
  782. nb_nics++;
  783. return idx;
  784. }
  785. static int (* const net_client_init_fun[NET_CLIENT_DRIVER__MAX])(
  786. const Netdev *netdev,
  787. const char *name,
  788. NetClientState *peer, Error **errp) = {
  789. [NET_CLIENT_DRIVER_NIC] = net_init_nic,
  790. #ifdef CONFIG_SLIRP
  791. [NET_CLIENT_DRIVER_USER] = net_init_slirp,
  792. #endif
  793. [NET_CLIENT_DRIVER_TAP] = net_init_tap,
  794. [NET_CLIENT_DRIVER_SOCKET] = net_init_socket,
  795. #ifdef CONFIG_VDE
  796. [NET_CLIENT_DRIVER_VDE] = net_init_vde,
  797. #endif
  798. #ifdef CONFIG_NETMAP
  799. [NET_CLIENT_DRIVER_NETMAP] = net_init_netmap,
  800. #endif
  801. [NET_CLIENT_DRIVER_DUMP] = net_init_dump,
  802. #ifdef CONFIG_NET_BRIDGE
  803. [NET_CLIENT_DRIVER_BRIDGE] = net_init_bridge,
  804. #endif
  805. [NET_CLIENT_DRIVER_HUBPORT] = net_init_hubport,
  806. #ifdef CONFIG_VHOST_NET_USED
  807. [NET_CLIENT_DRIVER_VHOST_USER] = net_init_vhost_user,
  808. #endif
  809. #ifdef CONFIG_L2TPV3
  810. [NET_CLIENT_DRIVER_L2TPV3] = net_init_l2tpv3,
  811. #endif
  812. };
  813. static int net_client_init1(const void *object, bool is_netdev, Error **errp)
  814. {
  815. Netdev legacy = {0};
  816. const Netdev *netdev;
  817. const char *name;
  818. NetClientState *peer = NULL;
  819. static bool vlan_warned;
  820. if (is_netdev) {
  821. netdev = object;
  822. name = netdev->id;
  823. if (netdev->type == NET_CLIENT_DRIVER_DUMP ||
  824. netdev->type == NET_CLIENT_DRIVER_NIC ||
  825. !net_client_init_fun[netdev->type]) {
  826. error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "type",
  827. "a netdev backend type");
  828. return -1;
  829. }
  830. } else {
  831. const NetLegacy *net = object;
  832. const NetLegacyOptions *opts = net->opts;
  833. legacy.id = net->id;
  834. netdev = &legacy;
  835. /* missing optional values have been initialized to "all bits zero" */
  836. name = net->has_id ? net->id : net->name;
  837. /* Map the old options to the new flat type */
  838. switch (opts->type) {
  839. case NET_LEGACY_OPTIONS_TYPE_NONE:
  840. return 0; /* nothing to do */
  841. case NET_LEGACY_OPTIONS_TYPE_NIC:
  842. legacy.type = NET_CLIENT_DRIVER_NIC;
  843. legacy.u.nic = opts->u.nic;
  844. break;
  845. case NET_LEGACY_OPTIONS_TYPE_USER:
  846. legacy.type = NET_CLIENT_DRIVER_USER;
  847. legacy.u.user = opts->u.user;
  848. break;
  849. case NET_LEGACY_OPTIONS_TYPE_TAP:
  850. legacy.type = NET_CLIENT_DRIVER_TAP;
  851. legacy.u.tap = opts->u.tap;
  852. break;
  853. case NET_LEGACY_OPTIONS_TYPE_L2TPV3:
  854. legacy.type = NET_CLIENT_DRIVER_L2TPV3;
  855. legacy.u.l2tpv3 = opts->u.l2tpv3;
  856. break;
  857. case NET_LEGACY_OPTIONS_TYPE_SOCKET:
  858. legacy.type = NET_CLIENT_DRIVER_SOCKET;
  859. legacy.u.socket = opts->u.socket;
  860. break;
  861. case NET_LEGACY_OPTIONS_TYPE_VDE:
  862. legacy.type = NET_CLIENT_DRIVER_VDE;
  863. legacy.u.vde = opts->u.vde;
  864. break;
  865. case NET_LEGACY_OPTIONS_TYPE_DUMP:
  866. legacy.type = NET_CLIENT_DRIVER_DUMP;
  867. legacy.u.dump = opts->u.dump;
  868. break;
  869. case NET_LEGACY_OPTIONS_TYPE_BRIDGE:
  870. legacy.type = NET_CLIENT_DRIVER_BRIDGE;
  871. legacy.u.bridge = opts->u.bridge;
  872. break;
  873. case NET_LEGACY_OPTIONS_TYPE_NETMAP:
  874. legacy.type = NET_CLIENT_DRIVER_NETMAP;
  875. legacy.u.netmap = opts->u.netmap;
  876. break;
  877. case NET_LEGACY_OPTIONS_TYPE_VHOST_USER:
  878. legacy.type = NET_CLIENT_DRIVER_VHOST_USER;
  879. legacy.u.vhost_user = opts->u.vhost_user;
  880. break;
  881. default:
  882. abort();
  883. }
  884. if (!net_client_init_fun[netdev->type]) {
  885. error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "type",
  886. "a net backend type (maybe it is not compiled "
  887. "into this binary)");
  888. return -1;
  889. }
  890. /* Do not add to a vlan if it's a nic with a netdev= parameter. */
  891. if (netdev->type != NET_CLIENT_DRIVER_NIC ||
  892. !opts->u.nic.has_netdev) {
  893. peer = net_hub_add_port(net->has_vlan ? net->vlan : 0, NULL);
  894. }
  895. if (net->has_vlan && !vlan_warned) {
  896. error_report("'vlan' is deprecated. Please use 'netdev' instead.");
  897. vlan_warned = true;
  898. }
  899. }
  900. if (net_client_init_fun[netdev->type](netdev, name, peer, errp) < 0) {
  901. /* FIXME drop when all init functions store an Error */
  902. if (errp && !*errp) {
  903. error_setg(errp, QERR_DEVICE_INIT_FAILED,
  904. NetClientDriver_lookup[netdev->type]);
  905. }
  906. return -1;
  907. }
  908. return 0;
  909. }
  910. int net_client_init(QemuOpts *opts, bool is_netdev, Error **errp)
  911. {
  912. void *object = NULL;
  913. Error *err = NULL;
  914. int ret = -1;
  915. Visitor *v = opts_visitor_new(opts);
  916. {
  917. /* Parse convenience option format ip6-net=fec0::0[/64] */
  918. const char *ip6_net = qemu_opt_get(opts, "ipv6-net");
  919. if (ip6_net) {
  920. char buf[strlen(ip6_net) + 1];
  921. if (get_str_sep(buf, sizeof(buf), &ip6_net, '/') < 0) {
  922. /* Default 64bit prefix length. */
  923. qemu_opt_set(opts, "ipv6-prefix", ip6_net, &error_abort);
  924. qemu_opt_set_number(opts, "ipv6-prefixlen", 64, &error_abort);
  925. } else {
  926. /* User-specified prefix length. */
  927. unsigned long len;
  928. int err;
  929. qemu_opt_set(opts, "ipv6-prefix", buf, &error_abort);
  930. err = qemu_strtoul(ip6_net, NULL, 10, &len);
  931. if (err) {
  932. error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
  933. "ipv6-prefix", "a number");
  934. } else {
  935. qemu_opt_set_number(opts, "ipv6-prefixlen", len,
  936. &error_abort);
  937. }
  938. }
  939. qemu_opt_unset(opts, "ipv6-net");
  940. }
  941. }
  942. if (is_netdev) {
  943. visit_type_Netdev(v, NULL, (Netdev **)&object, &err);
  944. } else {
  945. visit_type_NetLegacy(v, NULL, (NetLegacy **)&object, &err);
  946. }
  947. if (!err) {
  948. ret = net_client_init1(object, is_netdev, &err);
  949. }
  950. if (is_netdev) {
  951. qapi_free_Netdev(object);
  952. } else {
  953. qapi_free_NetLegacy(object);
  954. }
  955. error_propagate(errp, err);
  956. visit_free(v);
  957. return ret;
  958. }
  959. static int net_host_check_device(const char *device)
  960. {
  961. int i;
  962. for (i = 0; host_net_devices[i]; i++) {
  963. if (!strncmp(host_net_devices[i], device,
  964. strlen(host_net_devices[i]))) {
  965. return 1;
  966. }
  967. }
  968. return 0;
  969. }
  970. void hmp_host_net_add(Monitor *mon, const QDict *qdict)
  971. {
  972. const char *device = qdict_get_str(qdict, "device");
  973. const char *opts_str = qdict_get_try_str(qdict, "opts");
  974. Error *local_err = NULL;
  975. QemuOpts *opts;
  976. if (!net_host_check_device(device)) {
  977. monitor_printf(mon, "invalid host network device %s\n", device);
  978. return;
  979. }
  980. opts = qemu_opts_parse_noisily(qemu_find_opts("net"),
  981. opts_str ? opts_str : "", false);
  982. if (!opts) {
  983. return;
  984. }
  985. qemu_opt_set(opts, "type", device, &error_abort);
  986. net_client_init(opts, false, &local_err);
  987. if (local_err) {
  988. error_report_err(local_err);
  989. monitor_printf(mon, "adding host network device %s failed\n", device);
  990. }
  991. }
  992. void hmp_host_net_remove(Monitor *mon, const QDict *qdict)
  993. {
  994. NetClientState *nc;
  995. int vlan_id = qdict_get_int(qdict, "vlan_id");
  996. const char *device = qdict_get_str(qdict, "device");
  997. nc = net_hub_find_client_by_name(vlan_id, device);
  998. if (!nc) {
  999. error_report("Host network device '%s' on hub '%d' not found",
  1000. device, vlan_id);
  1001. return;
  1002. }
  1003. if (nc->info->type == NET_CLIENT_DRIVER_NIC) {
  1004. error_report("invalid host network device '%s'", device);
  1005. return;
  1006. }
  1007. qemu_del_net_client(nc->peer);
  1008. qemu_del_net_client(nc);
  1009. qemu_opts_del(qemu_opts_find(qemu_find_opts("net"), device));
  1010. }
  1011. void netdev_add(QemuOpts *opts, Error **errp)
  1012. {
  1013. net_client_init(opts, true, errp);
  1014. }
  1015. void qmp_netdev_add(QDict *qdict, QObject **ret, Error **errp)
  1016. {
  1017. Error *local_err = NULL;
  1018. QemuOptsList *opts_list;
  1019. QemuOpts *opts;
  1020. opts_list = qemu_find_opts_err("netdev", &local_err);
  1021. if (local_err) {
  1022. goto out;
  1023. }
  1024. opts = qemu_opts_from_qdict(opts_list, qdict, &local_err);
  1025. if (local_err) {
  1026. goto out;
  1027. }
  1028. netdev_add(opts, &local_err);
  1029. if (local_err) {
  1030. qemu_opts_del(opts);
  1031. goto out;
  1032. }
  1033. out:
  1034. error_propagate(errp, local_err);
  1035. }
  1036. void qmp_netdev_del(const char *id, Error **errp)
  1037. {
  1038. NetClientState *nc;
  1039. QemuOpts *opts;
  1040. nc = qemu_find_netdev(id);
  1041. if (!nc) {
  1042. error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
  1043. "Device '%s' not found", id);
  1044. return;
  1045. }
  1046. opts = qemu_opts_find(qemu_find_opts_err("netdev", NULL), id);
  1047. if (!opts) {
  1048. error_setg(errp, "Device '%s' is not a netdev", id);
  1049. return;
  1050. }
  1051. qemu_del_net_client(nc);
  1052. qemu_opts_del(opts);
  1053. }
  1054. static void netfilter_print_info(Monitor *mon, NetFilterState *nf)
  1055. {
  1056. char *str;
  1057. ObjectProperty *prop;
  1058. ObjectPropertyIterator iter;
  1059. Visitor *v;
  1060. /* generate info str */
  1061. object_property_iter_init(&iter, OBJECT(nf));
  1062. while ((prop = object_property_iter_next(&iter))) {
  1063. if (!strcmp(prop->name, "type")) {
  1064. continue;
  1065. }
  1066. v = string_output_visitor_new(false, &str);
  1067. object_property_get(OBJECT(nf), v, prop->name, NULL);
  1068. visit_complete(v, &str);
  1069. visit_free(v);
  1070. monitor_printf(mon, ",%s=%s", prop->name, str);
  1071. g_free(str);
  1072. }
  1073. monitor_printf(mon, "\n");
  1074. }
  1075. void print_net_client(Monitor *mon, NetClientState *nc)
  1076. {
  1077. NetFilterState *nf;
  1078. monitor_printf(mon, "%s: index=%d,type=%s,%s\n", nc->name,
  1079. nc->queue_index,
  1080. NetClientDriver_lookup[nc->info->type],
  1081. nc->info_str);
  1082. if (!QTAILQ_EMPTY(&nc->filters)) {
  1083. monitor_printf(mon, "filters:\n");
  1084. }
  1085. QTAILQ_FOREACH(nf, &nc->filters, next) {
  1086. char *path = object_get_canonical_path_component(OBJECT(nf));
  1087. monitor_printf(mon, " - %s: type=%s", path,
  1088. object_get_typename(OBJECT(nf)));
  1089. netfilter_print_info(mon, nf);
  1090. g_free(path);
  1091. }
  1092. }
  1093. RxFilterInfoList *qmp_query_rx_filter(bool has_name, const char *name,
  1094. Error **errp)
  1095. {
  1096. NetClientState *nc;
  1097. RxFilterInfoList *filter_list = NULL, *last_entry = NULL;
  1098. QTAILQ_FOREACH(nc, &net_clients, next) {
  1099. RxFilterInfoList *entry;
  1100. RxFilterInfo *info;
  1101. if (has_name && strcmp(nc->name, name) != 0) {
  1102. continue;
  1103. }
  1104. /* only query rx-filter information of NIC */
  1105. if (nc->info->type != NET_CLIENT_DRIVER_NIC) {
  1106. if (has_name) {
  1107. error_setg(errp, "net client(%s) isn't a NIC", name);
  1108. return NULL;
  1109. }
  1110. continue;
  1111. }
  1112. /* only query information on queue 0 since the info is per nic,
  1113. * not per queue
  1114. */
  1115. if (nc->queue_index != 0)
  1116. continue;
  1117. if (nc->info->query_rx_filter) {
  1118. info = nc->info->query_rx_filter(nc);
  1119. entry = g_malloc0(sizeof(*entry));
  1120. entry->value = info;
  1121. if (!filter_list) {
  1122. filter_list = entry;
  1123. } else {
  1124. last_entry->next = entry;
  1125. }
  1126. last_entry = entry;
  1127. } else if (has_name) {
  1128. error_setg(errp, "net client(%s) doesn't support"
  1129. " rx-filter querying", name);
  1130. return NULL;
  1131. }
  1132. if (has_name) {
  1133. break;
  1134. }
  1135. }
  1136. if (filter_list == NULL && has_name) {
  1137. error_setg(errp, "invalid net client name: %s", name);
  1138. }
  1139. return filter_list;
  1140. }
  1141. void hmp_info_network(Monitor *mon, const QDict *qdict)
  1142. {
  1143. NetClientState *nc, *peer;
  1144. NetClientDriver type;
  1145. net_hub_info(mon);
  1146. QTAILQ_FOREACH(nc, &net_clients, next) {
  1147. peer = nc->peer;
  1148. type = nc->info->type;
  1149. /* Skip if already printed in hub info */
  1150. if (net_hub_id_for_client(nc, NULL) == 0) {
  1151. continue;
  1152. }
  1153. if (!peer || type == NET_CLIENT_DRIVER_NIC) {
  1154. print_net_client(mon, nc);
  1155. } /* else it's a netdev connected to a NIC, printed with the NIC */
  1156. if (peer && type == NET_CLIENT_DRIVER_NIC) {
  1157. monitor_printf(mon, " \\ ");
  1158. print_net_client(mon, peer);
  1159. }
  1160. }
  1161. }
  1162. void qmp_set_link(const char *name, bool up, Error **errp)
  1163. {
  1164. NetClientState *ncs[MAX_QUEUE_NUM];
  1165. NetClientState *nc;
  1166. int queues, i;
  1167. queues = qemu_find_net_clients_except(name, ncs,
  1168. NET_CLIENT_DRIVER__MAX,
  1169. MAX_QUEUE_NUM);
  1170. if (queues == 0) {
  1171. error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
  1172. "Device '%s' not found", name);
  1173. return;
  1174. }
  1175. nc = ncs[0];
  1176. for (i = 0; i < queues; i++) {
  1177. ncs[i]->link_down = !up;
  1178. }
  1179. if (nc->info->link_status_changed) {
  1180. nc->info->link_status_changed(nc);
  1181. }
  1182. if (nc->peer) {
  1183. /* Change peer link only if the peer is NIC and then notify peer.
  1184. * If the peer is a HUBPORT or a backend, we do not change the
  1185. * link status.
  1186. *
  1187. * This behavior is compatible with qemu vlans where there could be
  1188. * multiple clients that can still communicate with each other in
  1189. * disconnected mode. For now maintain this compatibility.
  1190. */
  1191. if (nc->peer->info->type == NET_CLIENT_DRIVER_NIC) {
  1192. for (i = 0; i < queues; i++) {
  1193. ncs[i]->peer->link_down = !up;
  1194. }
  1195. }
  1196. if (nc->peer->info->link_status_changed) {
  1197. nc->peer->info->link_status_changed(nc->peer);
  1198. }
  1199. }
  1200. }
  1201. static void net_vm_change_state_handler(void *opaque, int running,
  1202. RunState state)
  1203. {
  1204. NetClientState *nc;
  1205. NetClientState *tmp;
  1206. QTAILQ_FOREACH_SAFE(nc, &net_clients, next, tmp) {
  1207. if (running) {
  1208. /* Flush queued packets and wake up backends. */
  1209. if (nc->peer && qemu_can_send_packet(nc)) {
  1210. qemu_flush_queued_packets(nc->peer);
  1211. }
  1212. } else {
  1213. /* Complete all queued packets, to guarantee we don't modify
  1214. * state later when VM is not running.
  1215. */
  1216. qemu_flush_or_purge_queued_packets(nc, true);
  1217. }
  1218. }
  1219. }
  1220. void net_cleanup(void)
  1221. {
  1222. NetClientState *nc;
  1223. /* We may del multiple entries during qemu_del_net_client(),
  1224. * so QTAILQ_FOREACH_SAFE() is also not safe here.
  1225. */
  1226. while (!QTAILQ_EMPTY(&net_clients)) {
  1227. nc = QTAILQ_FIRST(&net_clients);
  1228. if (nc->info->type == NET_CLIENT_DRIVER_NIC) {
  1229. qemu_del_nic(qemu_get_nic(nc));
  1230. } else {
  1231. qemu_del_net_client(nc);
  1232. }
  1233. }
  1234. qemu_del_vm_change_state_handler(net_change_state_entry);
  1235. }
  1236. void net_check_clients(void)
  1237. {
  1238. NetClientState *nc;
  1239. int i;
  1240. net_hub_check_clients();
  1241. QTAILQ_FOREACH(nc, &net_clients, next) {
  1242. if (!nc->peer) {
  1243. fprintf(stderr, "Warning: %s %s has no peer\n",
  1244. nc->info->type == NET_CLIENT_DRIVER_NIC ?
  1245. "nic" : "netdev", nc->name);
  1246. }
  1247. }
  1248. /* Check that all NICs requested via -net nic actually got created.
  1249. * NICs created via -device don't need to be checked here because
  1250. * they are always instantiated.
  1251. */
  1252. for (i = 0; i < MAX_NICS; i++) {
  1253. NICInfo *nd = &nd_table[i];
  1254. if (nd->used && !nd->instantiated) {
  1255. fprintf(stderr, "Warning: requested NIC (%s, model %s) "
  1256. "was not created (not supported by this machine?)\n",
  1257. nd->name ? nd->name : "anonymous",
  1258. nd->model ? nd->model : "unspecified");
  1259. }
  1260. }
  1261. }
  1262. static int net_init_client(void *dummy, QemuOpts *opts, Error **errp)
  1263. {
  1264. Error *local_err = NULL;
  1265. net_client_init(opts, false, &local_err);
  1266. if (local_err) {
  1267. error_report_err(local_err);
  1268. return -1;
  1269. }
  1270. return 0;
  1271. }
  1272. static int net_init_netdev(void *dummy, QemuOpts *opts, Error **errp)
  1273. {
  1274. Error *local_err = NULL;
  1275. int ret;
  1276. ret = net_client_init(opts, true, &local_err);
  1277. if (local_err) {
  1278. error_report_err(local_err);
  1279. return -1;
  1280. }
  1281. return ret;
  1282. }
  1283. int net_init_clients(void)
  1284. {
  1285. QemuOptsList *net = qemu_find_opts("net");
  1286. net_change_state_entry =
  1287. qemu_add_vm_change_state_handler(net_vm_change_state_handler, NULL);
  1288. QTAILQ_INIT(&net_clients);
  1289. if (qemu_opts_foreach(qemu_find_opts("netdev"),
  1290. net_init_netdev, NULL, NULL)) {
  1291. return -1;
  1292. }
  1293. if (qemu_opts_foreach(net, net_init_client, NULL, NULL)) {
  1294. return -1;
  1295. }
  1296. return 0;
  1297. }
  1298. int net_client_parse(QemuOptsList *opts_list, const char *optarg)
  1299. {
  1300. #if defined(CONFIG_SLIRP)
  1301. int ret;
  1302. if (net_slirp_parse_legacy(opts_list, optarg, &ret)) {
  1303. return ret;
  1304. }
  1305. #endif
  1306. if (!qemu_opts_parse_noisily(opts_list, optarg, true)) {
  1307. return -1;
  1308. }
  1309. return 0;
  1310. }
  1311. /* From FreeBSD */
  1312. /* XXX: optimize */
  1313. unsigned compute_mcast_idx(const uint8_t *ep)
  1314. {
  1315. uint32_t crc;
  1316. int carry, i, j;
  1317. uint8_t b;
  1318. crc = 0xffffffff;
  1319. for (i = 0; i < 6; i++) {
  1320. b = *ep++;
  1321. for (j = 0; j < 8; j++) {
  1322. carry = ((crc & 0x80000000L) ? 1 : 0) ^ (b & 0x01);
  1323. crc <<= 1;
  1324. b >>= 1;
  1325. if (carry) {
  1326. crc = ((crc ^ POLYNOMIAL) | carry);
  1327. }
  1328. }
  1329. }
  1330. return crc >> 26;
  1331. }
  1332. QemuOptsList qemu_netdev_opts = {
  1333. .name = "netdev",
  1334. .implied_opt_name = "type",
  1335. .head = QTAILQ_HEAD_INITIALIZER(qemu_netdev_opts.head),
  1336. .desc = {
  1337. /*
  1338. * no elements => accept any params
  1339. * validation will happen later
  1340. */
  1341. { /* end of list */ }
  1342. },
  1343. };
  1344. QemuOptsList qemu_net_opts = {
  1345. .name = "net",
  1346. .implied_opt_name = "type",
  1347. .head = QTAILQ_HEAD_INITIALIZER(qemu_net_opts.head),
  1348. .desc = {
  1349. /*
  1350. * no elements => accept any params
  1351. * validation will happen later
  1352. */
  1353. { /* end of list */ }
  1354. },
  1355. };
  1356. void net_socket_rs_init(SocketReadState *rs,
  1357. SocketReadStateFinalize *finalize)
  1358. {
  1359. rs->state = 0;
  1360. rs->index = 0;
  1361. rs->packet_len = 0;
  1362. memset(rs->buf, 0, sizeof(rs->buf));
  1363. rs->finalize = finalize;
  1364. }
  1365. /*
  1366. * Returns
  1367. * 0: success
  1368. * -1: error occurs
  1369. */
  1370. int net_fill_rstate(SocketReadState *rs, const uint8_t *buf, int size)
  1371. {
  1372. unsigned int l;
  1373. while (size > 0) {
  1374. /* reassemble a packet from the network */
  1375. switch (rs->state) { /* 0 = getting length, 1 = getting data */
  1376. case 0:
  1377. l = 4 - rs->index;
  1378. if (l > size) {
  1379. l = size;
  1380. }
  1381. memcpy(rs->buf + rs->index, buf, l);
  1382. buf += l;
  1383. size -= l;
  1384. rs->index += l;
  1385. if (rs->index == 4) {
  1386. /* got length */
  1387. rs->packet_len = ntohl(*(uint32_t *)rs->buf);
  1388. rs->index = 0;
  1389. rs->state = 1;
  1390. }
  1391. break;
  1392. case 1:
  1393. l = rs->packet_len - rs->index;
  1394. if (l > size) {
  1395. l = size;
  1396. }
  1397. if (rs->index + l <= sizeof(rs->buf)) {
  1398. memcpy(rs->buf + rs->index, buf, l);
  1399. } else {
  1400. fprintf(stderr, "serious error: oversized packet received,"
  1401. "connection terminated.\n");
  1402. rs->index = rs->state = 0;
  1403. return -1;
  1404. }
  1405. rs->index += l;
  1406. buf += l;
  1407. size -= l;
  1408. if (rs->index >= rs->packet_len) {
  1409. rs->index = 0;
  1410. rs->state = 0;
  1411. assert(rs->finalize);
  1412. rs->finalize(rs);
  1413. }
  1414. break;
  1415. }
  1416. }
  1417. assert(size == 0);
  1418. return 0;
  1419. }