2
0

net.c 44 KB

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