net.c 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134
  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 "qobject/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/id.h"
  44. #include "qemu/iov.h"
  45. #include "qemu/qemu-print.h"
  46. #include "qemu/main-loop.h"
  47. #include "qemu/option.h"
  48. #include "qemu/keyval.h"
  49. #include "qapi/error.h"
  50. #include "qapi/opts-visitor.h"
  51. #include "system/runstate.h"
  52. #include "net/colo-compare.h"
  53. #include "net/filter.h"
  54. #include "qapi/string-output-visitor.h"
  55. #include "qapi/qobject-input-visitor.h"
  56. #include "standard-headers/linux/virtio_net.h"
  57. /* Net bridge is currently not supported for W32. */
  58. #if !defined(_WIN32)
  59. # define CONFIG_NET_BRIDGE
  60. #endif
  61. static VMChangeStateEntry *net_change_state_entry;
  62. NetClientStateList net_clients;
  63. typedef struct NetdevQueueEntry {
  64. Netdev *nd;
  65. Location loc;
  66. QSIMPLEQ_ENTRY(NetdevQueueEntry) entry;
  67. } NetdevQueueEntry;
  68. typedef QSIMPLEQ_HEAD(, NetdevQueueEntry) NetdevQueue;
  69. static NetdevQueue nd_queue = QSIMPLEQ_HEAD_INITIALIZER(nd_queue);
  70. static GHashTable *nic_model_help;
  71. static int nb_nics;
  72. static NICInfo nd_table[MAX_NICS];
  73. /***********************************************************/
  74. /* network device redirectors */
  75. int convert_host_port(struct sockaddr_in *saddr, const char *host,
  76. const char *port, Error **errp)
  77. {
  78. struct hostent *he;
  79. const char *r;
  80. long p;
  81. memset(saddr, 0, sizeof(*saddr));
  82. saddr->sin_family = AF_INET;
  83. if (host[0] == '\0') {
  84. saddr->sin_addr.s_addr = 0;
  85. } else {
  86. if (qemu_isdigit(host[0])) {
  87. if (!inet_aton(host, &saddr->sin_addr)) {
  88. error_setg(errp, "host address '%s' is not a valid "
  89. "IPv4 address", host);
  90. return -1;
  91. }
  92. } else {
  93. he = gethostbyname(host);
  94. if (he == NULL) {
  95. error_setg(errp, "can't resolve host address '%s'", host);
  96. return -1;
  97. }
  98. saddr->sin_addr = *(struct in_addr *)he->h_addr;
  99. }
  100. }
  101. if (qemu_strtol(port, &r, 0, &p) != 0) {
  102. error_setg(errp, "port number '%s' is invalid", port);
  103. return -1;
  104. }
  105. saddr->sin_port = htons(p);
  106. return 0;
  107. }
  108. int parse_host_port(struct sockaddr_in *saddr, const char *str,
  109. Error **errp)
  110. {
  111. gchar **substrings;
  112. int ret;
  113. substrings = g_strsplit(str, ":", 2);
  114. if (!substrings || !substrings[0] || !substrings[1]) {
  115. error_setg(errp, "host address '%s' doesn't contain ':' "
  116. "separating host from port", str);
  117. ret = -1;
  118. goto out;
  119. }
  120. ret = convert_host_port(saddr, substrings[0], substrings[1], errp);
  121. out:
  122. g_strfreev(substrings);
  123. return ret;
  124. }
  125. char *qemu_mac_strdup_printf(const uint8_t *macaddr)
  126. {
  127. return g_strdup_printf("%.2x:%.2x:%.2x:%.2x:%.2x:%.2x",
  128. macaddr[0], macaddr[1], macaddr[2],
  129. macaddr[3], macaddr[4], macaddr[5]);
  130. }
  131. void qemu_set_info_str(NetClientState *nc, const char *fmt, ...)
  132. {
  133. va_list ap;
  134. va_start(ap, fmt);
  135. vsnprintf(nc->info_str, sizeof(nc->info_str), fmt, ap);
  136. va_end(ap);
  137. }
  138. void qemu_format_nic_info_str(NetClientState *nc, uint8_t macaddr[6])
  139. {
  140. qemu_set_info_str(nc, "model=%s,macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
  141. nc->model, macaddr[0], macaddr[1], macaddr[2],
  142. macaddr[3], macaddr[4], macaddr[5]);
  143. }
  144. static int mac_table[256] = {0};
  145. static void qemu_macaddr_set_used(MACAddr *macaddr)
  146. {
  147. int index;
  148. for (index = 0x56; index < 0xFF; index++) {
  149. if (macaddr->a[5] == index) {
  150. mac_table[index]++;
  151. }
  152. }
  153. }
  154. static void qemu_macaddr_set_free(MACAddr *macaddr)
  155. {
  156. int index;
  157. static const MACAddr base = { .a = { 0x52, 0x54, 0x00, 0x12, 0x34, 0 } };
  158. if (memcmp(macaddr->a, &base.a, (sizeof(base.a) - 1)) != 0) {
  159. return;
  160. }
  161. for (index = 0x56; index < 0xFF; index++) {
  162. if (macaddr->a[5] == index) {
  163. mac_table[index]--;
  164. }
  165. }
  166. }
  167. static int qemu_macaddr_get_free(void)
  168. {
  169. int index;
  170. for (index = 0x56; index < 0xFF; index++) {
  171. if (mac_table[index] == 0) {
  172. return index;
  173. }
  174. }
  175. return -1;
  176. }
  177. void qemu_macaddr_default_if_unset(MACAddr *macaddr)
  178. {
  179. static const MACAddr zero = { .a = { 0,0,0,0,0,0 } };
  180. static const MACAddr base = { .a = { 0x52, 0x54, 0x00, 0x12, 0x34, 0 } };
  181. if (memcmp(macaddr, &zero, sizeof(zero)) != 0) {
  182. if (memcmp(macaddr->a, &base.a, (sizeof(base.a) - 1)) != 0) {
  183. return;
  184. } else {
  185. qemu_macaddr_set_used(macaddr);
  186. return;
  187. }
  188. }
  189. macaddr->a[0] = 0x52;
  190. macaddr->a[1] = 0x54;
  191. macaddr->a[2] = 0x00;
  192. macaddr->a[3] = 0x12;
  193. macaddr->a[4] = 0x34;
  194. macaddr->a[5] = qemu_macaddr_get_free();
  195. qemu_macaddr_set_used(macaddr);
  196. }
  197. /**
  198. * Generate a name for net client
  199. *
  200. * Only net clients created with the legacy -net option and NICs need this.
  201. */
  202. static char *assign_name(NetClientState *nc1, const char *model)
  203. {
  204. NetClientState *nc;
  205. int id = 0;
  206. QTAILQ_FOREACH(nc, &net_clients, next) {
  207. if (nc == nc1) {
  208. continue;
  209. }
  210. if (strcmp(nc->model, model) == 0) {
  211. id++;
  212. }
  213. }
  214. return g_strdup_printf("%s.%d", model, id);
  215. }
  216. static void qemu_net_client_destructor(NetClientState *nc)
  217. {
  218. g_free(nc);
  219. }
  220. static ssize_t qemu_deliver_packet_iov(NetClientState *sender,
  221. unsigned flags,
  222. const struct iovec *iov,
  223. int iovcnt,
  224. void *opaque);
  225. static void qemu_net_client_setup(NetClientState *nc,
  226. NetClientInfo *info,
  227. NetClientState *peer,
  228. const char *model,
  229. const char *name,
  230. NetClientDestructor *destructor,
  231. bool is_datapath)
  232. {
  233. nc->info = info;
  234. nc->model = g_strdup(model);
  235. if (name) {
  236. nc->name = g_strdup(name);
  237. } else {
  238. nc->name = assign_name(nc, model);
  239. }
  240. if (peer) {
  241. assert(!peer->peer);
  242. nc->peer = peer;
  243. peer->peer = nc;
  244. }
  245. QTAILQ_INSERT_TAIL(&net_clients, nc, next);
  246. nc->incoming_queue = qemu_new_net_queue(qemu_deliver_packet_iov, nc);
  247. nc->destructor = destructor;
  248. nc->is_datapath = is_datapath;
  249. QTAILQ_INIT(&nc->filters);
  250. }
  251. NetClientState *qemu_new_net_client(NetClientInfo *info,
  252. NetClientState *peer,
  253. const char *model,
  254. const char *name)
  255. {
  256. NetClientState *nc;
  257. assert(info->size >= sizeof(NetClientState));
  258. nc = g_malloc0(info->size);
  259. qemu_net_client_setup(nc, info, peer, model, name,
  260. qemu_net_client_destructor, true);
  261. return nc;
  262. }
  263. NetClientState *qemu_new_net_control_client(NetClientInfo *info,
  264. NetClientState *peer,
  265. const char *model,
  266. const char *name)
  267. {
  268. NetClientState *nc;
  269. assert(info->size >= sizeof(NetClientState));
  270. nc = g_malloc0(info->size);
  271. qemu_net_client_setup(nc, info, peer, model, name,
  272. qemu_net_client_destructor, false);
  273. return nc;
  274. }
  275. NICState *qemu_new_nic(NetClientInfo *info,
  276. NICConf *conf,
  277. const char *model,
  278. const char *name,
  279. MemReentrancyGuard *reentrancy_guard,
  280. void *opaque)
  281. {
  282. NetClientState **peers = conf->peers.ncs;
  283. NICState *nic;
  284. int i, queues = MAX(1, conf->peers.queues);
  285. assert(info->type == NET_CLIENT_DRIVER_NIC);
  286. assert(info->size >= sizeof(NICState));
  287. nic = g_malloc0(info->size + sizeof(NetClientState) * queues);
  288. nic->ncs = (void *)nic + info->size;
  289. nic->conf = conf;
  290. nic->reentrancy_guard = reentrancy_guard,
  291. nic->opaque = opaque;
  292. for (i = 0; i < queues; i++) {
  293. qemu_net_client_setup(&nic->ncs[i], info, peers[i], model, name,
  294. NULL, true);
  295. nic->ncs[i].queue_index = i;
  296. }
  297. return nic;
  298. }
  299. NetClientState *qemu_get_subqueue(NICState *nic, int queue_index)
  300. {
  301. return nic->ncs + queue_index;
  302. }
  303. NetClientState *qemu_get_queue(NICState *nic)
  304. {
  305. return qemu_get_subqueue(nic, 0);
  306. }
  307. NICState *qemu_get_nic(NetClientState *nc)
  308. {
  309. NetClientState *nc0 = nc - nc->queue_index;
  310. return (NICState *)((void *)nc0 - nc->info->size);
  311. }
  312. void *qemu_get_nic_opaque(NetClientState *nc)
  313. {
  314. NICState *nic = qemu_get_nic(nc);
  315. return nic->opaque;
  316. }
  317. NetClientState *qemu_get_peer(NetClientState *nc, int queue_index)
  318. {
  319. assert(nc != NULL);
  320. NetClientState *ncs = nc + queue_index;
  321. return ncs->peer;
  322. }
  323. static void qemu_cleanup_net_client(NetClientState *nc,
  324. bool remove_from_net_clients)
  325. {
  326. if (remove_from_net_clients) {
  327. QTAILQ_REMOVE(&net_clients, nc, next);
  328. }
  329. if (nc->info->cleanup) {
  330. nc->info->cleanup(nc);
  331. }
  332. }
  333. static void qemu_free_net_client(NetClientState *nc)
  334. {
  335. if (nc->incoming_queue) {
  336. qemu_del_net_queue(nc->incoming_queue);
  337. }
  338. if (nc->peer) {
  339. nc->peer->peer = NULL;
  340. }
  341. g_free(nc->name);
  342. g_free(nc->model);
  343. if (nc->destructor) {
  344. nc->destructor(nc);
  345. }
  346. }
  347. void qemu_del_net_client(NetClientState *nc)
  348. {
  349. NetClientState *ncs[MAX_QUEUE_NUM];
  350. int queues, i;
  351. NetFilterState *nf, *next;
  352. assert(nc->info->type != NET_CLIENT_DRIVER_NIC);
  353. /* If the NetClientState belongs to a multiqueue backend, we will change all
  354. * other NetClientStates also.
  355. */
  356. queues = qemu_find_net_clients_except(nc->name, ncs,
  357. NET_CLIENT_DRIVER_NIC,
  358. MAX_QUEUE_NUM);
  359. assert(queues != 0);
  360. QTAILQ_FOREACH_SAFE(nf, &nc->filters, next, next) {
  361. object_unparent(OBJECT(nf));
  362. }
  363. /*
  364. * If there is a peer NIC, transfer ownership to it. Delete the client
  365. * from net_client list but do not cleanup nor free. This way NIC can
  366. * still access to members of the backend.
  367. *
  368. * The cleanup and free will be done when the NIC is free.
  369. */
  370. if (nc->peer && nc->peer->info->type == NET_CLIENT_DRIVER_NIC) {
  371. NICState *nic = qemu_get_nic(nc->peer);
  372. if (nic->peer_deleted) {
  373. return;
  374. }
  375. nic->peer_deleted = true;
  376. for (i = 0; i < queues; i++) {
  377. ncs[i]->peer->link_down = true;
  378. QTAILQ_REMOVE(&net_clients, ncs[i], next);
  379. }
  380. if (nc->peer->info->link_status_changed) {
  381. nc->peer->info->link_status_changed(nc->peer);
  382. }
  383. return;
  384. }
  385. for (i = 0; i < queues; i++) {
  386. qemu_cleanup_net_client(ncs[i], true);
  387. qemu_free_net_client(ncs[i]);
  388. }
  389. }
  390. void qemu_del_nic(NICState *nic)
  391. {
  392. int i, queues = MAX(nic->conf->peers.queues, 1);
  393. qemu_macaddr_set_free(&nic->conf->macaddr);
  394. for (i = 0; i < queues; i++) {
  395. NetClientState *nc = qemu_get_subqueue(nic, i);
  396. /*
  397. * If this is a peer NIC and peer has already been deleted, clean it up
  398. * and free it now.
  399. */
  400. if (nic->peer_deleted) {
  401. qemu_cleanup_net_client(nc->peer, false);
  402. qemu_free_net_client(nc->peer);
  403. } else if (nc->peer) {
  404. /* if there are RX packets pending, complete them */
  405. qemu_purge_queued_packets(nc->peer);
  406. }
  407. }
  408. for (i = queues - 1; i >= 0; i--) {
  409. NetClientState *nc = qemu_get_subqueue(nic, i);
  410. qemu_cleanup_net_client(nc, true);
  411. qemu_free_net_client(nc);
  412. }
  413. g_free(nic);
  414. }
  415. void qemu_foreach_nic(qemu_nic_foreach func, void *opaque)
  416. {
  417. NetClientState *nc;
  418. QTAILQ_FOREACH(nc, &net_clients, next) {
  419. if (nc->info->type == NET_CLIENT_DRIVER_NIC) {
  420. if (nc->queue_index == 0) {
  421. func(qemu_get_nic(nc), opaque);
  422. }
  423. }
  424. }
  425. }
  426. bool qemu_has_ufo(NetClientState *nc)
  427. {
  428. if (!nc || !nc->info->has_ufo) {
  429. return false;
  430. }
  431. return nc->info->has_ufo(nc);
  432. }
  433. bool qemu_has_uso(NetClientState *nc)
  434. {
  435. if (!nc || !nc->info->has_uso) {
  436. return false;
  437. }
  438. return nc->info->has_uso(nc);
  439. }
  440. bool qemu_has_vnet_hdr(NetClientState *nc)
  441. {
  442. if (!nc || !nc->info->has_vnet_hdr) {
  443. return false;
  444. }
  445. return nc->info->has_vnet_hdr(nc);
  446. }
  447. bool qemu_has_vnet_hdr_len(NetClientState *nc, int len)
  448. {
  449. if (!nc || !nc->info->has_vnet_hdr_len) {
  450. return false;
  451. }
  452. return nc->info->has_vnet_hdr_len(nc, len);
  453. }
  454. void qemu_set_offload(NetClientState *nc, int csum, int tso4, int tso6,
  455. int ecn, int ufo, int uso4, int uso6)
  456. {
  457. if (!nc || !nc->info->set_offload) {
  458. return;
  459. }
  460. nc->info->set_offload(nc, csum, tso4, tso6, ecn, ufo, uso4, uso6);
  461. }
  462. int qemu_get_vnet_hdr_len(NetClientState *nc)
  463. {
  464. if (!nc) {
  465. return 0;
  466. }
  467. return nc->vnet_hdr_len;
  468. }
  469. void qemu_set_vnet_hdr_len(NetClientState *nc, int len)
  470. {
  471. if (!nc || !nc->info->set_vnet_hdr_len) {
  472. return;
  473. }
  474. assert(len == sizeof(struct virtio_net_hdr_mrg_rxbuf) ||
  475. len == sizeof(struct virtio_net_hdr) ||
  476. len == sizeof(struct virtio_net_hdr_v1_hash));
  477. nc->vnet_hdr_len = len;
  478. nc->info->set_vnet_hdr_len(nc, len);
  479. }
  480. int qemu_set_vnet_le(NetClientState *nc, bool is_le)
  481. {
  482. #if HOST_BIG_ENDIAN
  483. if (!nc || !nc->info->set_vnet_le) {
  484. return -ENOSYS;
  485. }
  486. return nc->info->set_vnet_le(nc, is_le);
  487. #else
  488. return 0;
  489. #endif
  490. }
  491. int qemu_set_vnet_be(NetClientState *nc, bool is_be)
  492. {
  493. #if HOST_BIG_ENDIAN
  494. return 0;
  495. #else
  496. if (!nc || !nc->info->set_vnet_be) {
  497. return -ENOSYS;
  498. }
  499. return nc->info->set_vnet_be(nc, is_be);
  500. #endif
  501. }
  502. int qemu_can_receive_packet(NetClientState *nc)
  503. {
  504. if (nc->receive_disabled) {
  505. return 0;
  506. } else if (nc->info->can_receive &&
  507. !nc->info->can_receive(nc)) {
  508. return 0;
  509. }
  510. return 1;
  511. }
  512. int qemu_can_send_packet(NetClientState *sender)
  513. {
  514. int vm_running = runstate_is_running();
  515. if (!vm_running) {
  516. return 0;
  517. }
  518. if (!sender->peer) {
  519. return 1;
  520. }
  521. return qemu_can_receive_packet(sender->peer);
  522. }
  523. static ssize_t filter_receive_iov(NetClientState *nc,
  524. NetFilterDirection direction,
  525. NetClientState *sender,
  526. unsigned flags,
  527. const struct iovec *iov,
  528. int iovcnt,
  529. NetPacketSent *sent_cb)
  530. {
  531. ssize_t ret = 0;
  532. NetFilterState *nf = NULL;
  533. if (direction == NET_FILTER_DIRECTION_TX) {
  534. QTAILQ_FOREACH(nf, &nc->filters, next) {
  535. ret = qemu_netfilter_receive(nf, direction, sender, flags, iov,
  536. iovcnt, sent_cb);
  537. if (ret) {
  538. return ret;
  539. }
  540. }
  541. } else {
  542. QTAILQ_FOREACH_REVERSE(nf, &nc->filters, next) {
  543. ret = qemu_netfilter_receive(nf, direction, sender, flags, iov,
  544. iovcnt, sent_cb);
  545. if (ret) {
  546. return ret;
  547. }
  548. }
  549. }
  550. return ret;
  551. }
  552. static ssize_t filter_receive(NetClientState *nc,
  553. NetFilterDirection direction,
  554. NetClientState *sender,
  555. unsigned flags,
  556. const uint8_t *data,
  557. size_t size,
  558. NetPacketSent *sent_cb)
  559. {
  560. struct iovec iov = {
  561. .iov_base = (void *)data,
  562. .iov_len = size
  563. };
  564. return filter_receive_iov(nc, direction, sender, flags, &iov, 1, sent_cb);
  565. }
  566. void qemu_purge_queued_packets(NetClientState *nc)
  567. {
  568. if (!nc->peer) {
  569. return;
  570. }
  571. qemu_net_queue_purge(nc->peer->incoming_queue, nc);
  572. }
  573. void qemu_flush_or_purge_queued_packets(NetClientState *nc, bool purge)
  574. {
  575. nc->receive_disabled = 0;
  576. if (nc->peer && nc->peer->info->type == NET_CLIENT_DRIVER_HUBPORT) {
  577. if (net_hub_flush(nc->peer)) {
  578. qemu_notify_event();
  579. }
  580. }
  581. if (qemu_net_queue_flush(nc->incoming_queue)) {
  582. /* We emptied the queue successfully, signal to the IO thread to repoll
  583. * the file descriptor (for tap, for example).
  584. */
  585. qemu_notify_event();
  586. } else if (purge) {
  587. /* Unable to empty the queue, purge remaining packets */
  588. qemu_net_queue_purge(nc->incoming_queue, nc->peer);
  589. }
  590. }
  591. void qemu_flush_queued_packets(NetClientState *nc)
  592. {
  593. qemu_flush_or_purge_queued_packets(nc, false);
  594. }
  595. static ssize_t qemu_send_packet_async_with_flags(NetClientState *sender,
  596. unsigned flags,
  597. const uint8_t *buf, int size,
  598. NetPacketSent *sent_cb)
  599. {
  600. NetQueue *queue;
  601. int ret;
  602. #ifdef DEBUG_NET
  603. printf("qemu_send_packet_async:\n");
  604. qemu_hexdump(stdout, "net", buf, size);
  605. #endif
  606. if (sender->link_down || !sender->peer) {
  607. return size;
  608. }
  609. /* Let filters handle the packet first */
  610. ret = filter_receive(sender, NET_FILTER_DIRECTION_TX,
  611. sender, flags, buf, size, sent_cb);
  612. if (ret) {
  613. return ret;
  614. }
  615. ret = filter_receive(sender->peer, NET_FILTER_DIRECTION_RX,
  616. sender, flags, buf, size, sent_cb);
  617. if (ret) {
  618. return ret;
  619. }
  620. queue = sender->peer->incoming_queue;
  621. return qemu_net_queue_send(queue, sender, flags, buf, size, sent_cb);
  622. }
  623. ssize_t qemu_send_packet_async(NetClientState *sender,
  624. const uint8_t *buf, int size,
  625. NetPacketSent *sent_cb)
  626. {
  627. return qemu_send_packet_async_with_flags(sender, QEMU_NET_PACKET_FLAG_NONE,
  628. buf, size, sent_cb);
  629. }
  630. ssize_t qemu_send_packet(NetClientState *nc, const uint8_t *buf, int size)
  631. {
  632. return qemu_send_packet_async(nc, buf, size, NULL);
  633. }
  634. ssize_t qemu_receive_packet(NetClientState *nc, const uint8_t *buf, int size)
  635. {
  636. if (!qemu_can_receive_packet(nc)) {
  637. return 0;
  638. }
  639. return qemu_net_queue_receive(nc->incoming_queue, buf, size);
  640. }
  641. ssize_t qemu_send_packet_raw(NetClientState *nc, const uint8_t *buf, int size)
  642. {
  643. return qemu_send_packet_async_with_flags(nc, QEMU_NET_PACKET_FLAG_RAW,
  644. buf, size, NULL);
  645. }
  646. static ssize_t nc_sendv_compat(NetClientState *nc, const struct iovec *iov,
  647. int iovcnt, unsigned flags)
  648. {
  649. uint8_t *buf = NULL;
  650. uint8_t *buffer;
  651. size_t offset;
  652. ssize_t ret;
  653. if (iovcnt == 1) {
  654. buffer = iov[0].iov_base;
  655. offset = iov[0].iov_len;
  656. } else {
  657. offset = iov_size(iov, iovcnt);
  658. if (offset > NET_BUFSIZE) {
  659. return -1;
  660. }
  661. buf = g_malloc(offset);
  662. buffer = buf;
  663. offset = iov_to_buf(iov, iovcnt, 0, buf, offset);
  664. }
  665. ret = nc->info->receive(nc, buffer, offset);
  666. g_free(buf);
  667. return ret;
  668. }
  669. static ssize_t qemu_deliver_packet_iov(NetClientState *sender,
  670. unsigned flags,
  671. const struct iovec *iov,
  672. int iovcnt,
  673. void *opaque)
  674. {
  675. MemReentrancyGuard *owned_reentrancy_guard;
  676. NetClientState *nc = opaque;
  677. int ret;
  678. struct virtio_net_hdr_v1_hash vnet_hdr = { };
  679. g_autofree struct iovec *iov_copy = NULL;
  680. if (nc->link_down) {
  681. return iov_size(iov, iovcnt);
  682. }
  683. if (nc->receive_disabled) {
  684. return 0;
  685. }
  686. if (nc->info->type != NET_CLIENT_DRIVER_NIC ||
  687. qemu_get_nic(nc)->reentrancy_guard->engaged_in_io) {
  688. owned_reentrancy_guard = NULL;
  689. } else {
  690. owned_reentrancy_guard = qemu_get_nic(nc)->reentrancy_guard;
  691. owned_reentrancy_guard->engaged_in_io = true;
  692. }
  693. if ((flags & QEMU_NET_PACKET_FLAG_RAW) && nc->vnet_hdr_len) {
  694. iov_copy = g_new(struct iovec, iovcnt + 1);
  695. iov_copy[0].iov_base = &vnet_hdr;
  696. iov_copy[0].iov_len = nc->vnet_hdr_len;
  697. memcpy(&iov_copy[1], iov, iovcnt * sizeof(*iov));
  698. iov = iov_copy;
  699. iovcnt++;
  700. }
  701. if (nc->info->receive_iov) {
  702. ret = nc->info->receive_iov(nc, iov, iovcnt);
  703. } else {
  704. ret = nc_sendv_compat(nc, iov, iovcnt, flags);
  705. }
  706. if (owned_reentrancy_guard) {
  707. owned_reentrancy_guard->engaged_in_io = false;
  708. }
  709. if (ret == 0) {
  710. nc->receive_disabled = 1;
  711. }
  712. return ret;
  713. }
  714. ssize_t qemu_sendv_packet_async(NetClientState *sender,
  715. const struct iovec *iov, int iovcnt,
  716. NetPacketSent *sent_cb)
  717. {
  718. NetQueue *queue;
  719. size_t size = iov_size(iov, iovcnt);
  720. int ret;
  721. if (size > NET_BUFSIZE) {
  722. return size;
  723. }
  724. if (sender->link_down || !sender->peer) {
  725. return size;
  726. }
  727. /* Let filters handle the packet first */
  728. ret = filter_receive_iov(sender, NET_FILTER_DIRECTION_TX, sender,
  729. QEMU_NET_PACKET_FLAG_NONE, iov, iovcnt, sent_cb);
  730. if (ret) {
  731. return ret;
  732. }
  733. ret = filter_receive_iov(sender->peer, NET_FILTER_DIRECTION_RX, sender,
  734. QEMU_NET_PACKET_FLAG_NONE, iov, iovcnt, sent_cb);
  735. if (ret) {
  736. return ret;
  737. }
  738. queue = sender->peer->incoming_queue;
  739. return qemu_net_queue_send_iov(queue, sender,
  740. QEMU_NET_PACKET_FLAG_NONE,
  741. iov, iovcnt, sent_cb);
  742. }
  743. ssize_t
  744. qemu_sendv_packet(NetClientState *nc, const struct iovec *iov, int iovcnt)
  745. {
  746. return qemu_sendv_packet_async(nc, iov, iovcnt, NULL);
  747. }
  748. NetClientState *qemu_find_netdev(const char *id)
  749. {
  750. NetClientState *nc;
  751. QTAILQ_FOREACH(nc, &net_clients, next) {
  752. if (nc->info->type == NET_CLIENT_DRIVER_NIC)
  753. continue;
  754. if (!strcmp(nc->name, id)) {
  755. return nc;
  756. }
  757. }
  758. return NULL;
  759. }
  760. int qemu_find_net_clients_except(const char *id, NetClientState **ncs,
  761. NetClientDriver type, int max)
  762. {
  763. NetClientState *nc;
  764. int ret = 0;
  765. QTAILQ_FOREACH(nc, &net_clients, next) {
  766. if (nc->info->type == type) {
  767. continue;
  768. }
  769. if (!id || !strcmp(nc->name, id)) {
  770. if (ret < max) {
  771. ncs[ret] = nc;
  772. }
  773. ret++;
  774. }
  775. }
  776. return ret;
  777. }
  778. static int nic_get_free_idx(void)
  779. {
  780. int index;
  781. for (index = 0; index < MAX_NICS; index++)
  782. if (!nd_table[index].used)
  783. return index;
  784. return -1;
  785. }
  786. GPtrArray *qemu_get_nic_models(const char *device_type)
  787. {
  788. GPtrArray *nic_models = g_ptr_array_new();
  789. GSList *list = object_class_get_list_sorted(device_type, false);
  790. while (list) {
  791. DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, list->data,
  792. TYPE_DEVICE);
  793. GSList *next;
  794. if (test_bit(DEVICE_CATEGORY_NETWORK, dc->categories) &&
  795. dc->user_creatable) {
  796. const char *name = object_class_get_name(list->data);
  797. /*
  798. * A network device might also be something else than a NIC, see
  799. * e.g. the "rocker" device. Thus we have to look for the "netdev"
  800. * property, too. Unfortunately, some devices like virtio-net only
  801. * create this property during instance_init, so we have to create
  802. * a temporary instance here to be able to check it.
  803. */
  804. Object *obj = object_new_with_class(OBJECT_CLASS(dc));
  805. if (object_property_find(obj, "netdev")) {
  806. g_ptr_array_add(nic_models, (gpointer)name);
  807. }
  808. object_unref(obj);
  809. }
  810. next = list->next;
  811. g_slist_free_1(list);
  812. list = next;
  813. }
  814. g_ptr_array_add(nic_models, NULL);
  815. return nic_models;
  816. }
  817. static int net_init_nic(const Netdev *netdev, const char *name,
  818. NetClientState *peer, Error **errp)
  819. {
  820. int idx;
  821. NICInfo *nd;
  822. const NetLegacyNicOptions *nic;
  823. assert(netdev->type == NET_CLIENT_DRIVER_NIC);
  824. nic = &netdev->u.nic;
  825. idx = nic_get_free_idx();
  826. if (idx == -1 || nb_nics >= MAX_NICS) {
  827. error_setg(errp, "too many NICs");
  828. return -1;
  829. }
  830. nd = &nd_table[idx];
  831. memset(nd, 0, sizeof(*nd));
  832. if (nic->netdev) {
  833. nd->netdev = qemu_find_netdev(nic->netdev);
  834. if (!nd->netdev) {
  835. error_setg(errp, "netdev '%s' not found", nic->netdev);
  836. return -1;
  837. }
  838. } else {
  839. assert(peer);
  840. nd->netdev = peer;
  841. }
  842. nd->name = g_strdup(name);
  843. if (nic->model) {
  844. nd->model = g_strdup(nic->model);
  845. }
  846. if (nic->addr) {
  847. nd->devaddr = g_strdup(nic->addr);
  848. }
  849. if (nic->macaddr &&
  850. net_parse_macaddr(nd->macaddr.a, nic->macaddr) < 0) {
  851. error_setg(errp, "invalid syntax for ethernet address");
  852. return -1;
  853. }
  854. if (nic->macaddr &&
  855. is_multicast_ether_addr(nd->macaddr.a)) {
  856. error_setg(errp,
  857. "NIC cannot have multicast MAC address (odd 1st byte)");
  858. return -1;
  859. }
  860. qemu_macaddr_default_if_unset(&nd->macaddr);
  861. if (nic->has_vectors) {
  862. if (nic->vectors > 0x7ffffff) {
  863. error_setg(errp, "invalid # of vectors: %"PRIu32, nic->vectors);
  864. return -1;
  865. }
  866. nd->nvectors = nic->vectors;
  867. } else {
  868. nd->nvectors = DEV_NVECTORS_UNSPECIFIED;
  869. }
  870. nd->used = 1;
  871. nb_nics++;
  872. return idx;
  873. }
  874. static gboolean add_nic_result(gpointer key, gpointer value, gpointer user_data)
  875. {
  876. GPtrArray *results = user_data;
  877. GPtrArray *alias_list = value;
  878. const char *model = key;
  879. char *result;
  880. if (!alias_list) {
  881. result = g_strdup(model);
  882. } else {
  883. GString *result_str = g_string_new(model);
  884. int i;
  885. g_string_append(result_str, " (aka ");
  886. for (i = 0; i < alias_list->len; i++) {
  887. if (i) {
  888. g_string_append(result_str, ", ");
  889. }
  890. g_string_append(result_str, alias_list->pdata[i]);
  891. }
  892. g_string_append(result_str, ")");
  893. result = result_str->str;
  894. g_string_free(result_str, false);
  895. g_ptr_array_unref(alias_list);
  896. }
  897. g_ptr_array_add(results, result);
  898. return true;
  899. }
  900. static int model_cmp(char **a, char **b)
  901. {
  902. return strcmp(*a, *b);
  903. }
  904. static void show_nic_models(void)
  905. {
  906. GPtrArray *results = g_ptr_array_new();
  907. int i;
  908. g_hash_table_foreach_remove(nic_model_help, add_nic_result, results);
  909. g_ptr_array_sort(results, (GCompareFunc)model_cmp);
  910. printf("Available NIC models for this configuration:\n");
  911. for (i = 0 ; i < results->len; i++) {
  912. printf("%s\n", (char *)results->pdata[i]);
  913. }
  914. g_hash_table_unref(nic_model_help);
  915. nic_model_help = NULL;
  916. }
  917. static void add_nic_model_help(const char *model, const char *alias)
  918. {
  919. GPtrArray *alias_list = NULL;
  920. if (g_hash_table_lookup_extended(nic_model_help, model, NULL,
  921. (gpointer *)&alias_list)) {
  922. /* Already exists, no alias to add: return */
  923. if (!alias) {
  924. return;
  925. }
  926. if (alias_list) {
  927. /* Check if this alias is already in the list. Add if not. */
  928. if (!g_ptr_array_find_with_equal_func(alias_list, alias,
  929. g_str_equal, NULL)) {
  930. g_ptr_array_add(alias_list, g_strdup(alias));
  931. }
  932. return;
  933. }
  934. }
  935. /* Either this model wasn't in the list already, or a first alias added */
  936. if (alias) {
  937. alias_list = g_ptr_array_new();
  938. g_ptr_array_set_free_func(alias_list, g_free);
  939. g_ptr_array_add(alias_list, g_strdup(alias));
  940. }
  941. g_hash_table_replace(nic_model_help, g_strdup(model), alias_list);
  942. }
  943. NICInfo *qemu_find_nic_info(const char *typename, bool match_default,
  944. const char *alias)
  945. {
  946. NICInfo *nd;
  947. int i;
  948. if (nic_model_help) {
  949. add_nic_model_help(typename, alias);
  950. }
  951. for (i = 0; i < nb_nics; i++) {
  952. nd = &nd_table[i];
  953. if (!nd->used || nd->instantiated) {
  954. continue;
  955. }
  956. if ((match_default && !nd->model) || !g_strcmp0(nd->model, typename)
  957. || (alias && !g_strcmp0(nd->model, alias))) {
  958. return nd;
  959. }
  960. }
  961. return NULL;
  962. }
  963. static bool is_nic_model_help_option(const char *model)
  964. {
  965. if (model && is_help_option(model)) {
  966. /*
  967. * Trigger the help output by instantiating the hash table which
  968. * will gather tha available models as they get registered.
  969. */
  970. if (!nic_model_help) {
  971. nic_model_help = g_hash_table_new_full(g_str_hash, g_str_equal,
  972. g_free, NULL);
  973. }
  974. return true;
  975. }
  976. return false;
  977. }
  978. /* "I have created a device. Please configure it if you can" */
  979. bool qemu_configure_nic_device(DeviceState *dev, bool match_default,
  980. const char *alias)
  981. {
  982. NICInfo *nd = qemu_find_nic_info(object_get_typename(OBJECT(dev)),
  983. match_default, alias);
  984. if (nd) {
  985. qdev_set_nic_properties(dev, nd);
  986. return true;
  987. }
  988. return false;
  989. }
  990. /* "Please create a device, if you have a configuration for it" */
  991. DeviceState *qemu_create_nic_device(const char *typename, bool match_default,
  992. const char *alias)
  993. {
  994. NICInfo *nd = qemu_find_nic_info(typename, match_default, alias);
  995. DeviceState *dev;
  996. if (!nd) {
  997. return NULL;
  998. }
  999. dev = qdev_new(typename);
  1000. qdev_set_nic_properties(dev, nd);
  1001. return dev;
  1002. }
  1003. void qemu_create_nic_bus_devices(BusState *bus, const char *parent_type,
  1004. const char *default_model,
  1005. const char *alias, const char *alias_target)
  1006. {
  1007. GPtrArray *nic_models = qemu_get_nic_models(parent_type);
  1008. const char *model;
  1009. DeviceState *dev;
  1010. NICInfo *nd;
  1011. int i;
  1012. if (nic_model_help) {
  1013. if (alias_target) {
  1014. add_nic_model_help(alias_target, alias);
  1015. }
  1016. for (i = 0; i < nic_models->len - 1; i++) {
  1017. add_nic_model_help(nic_models->pdata[i], NULL);
  1018. }
  1019. }
  1020. /* Drop the NULL terminator which would make g_str_equal() unhappy */
  1021. nic_models->len--;
  1022. for (i = 0; i < nb_nics; i++) {
  1023. nd = &nd_table[i];
  1024. if (!nd->used || nd->instantiated) {
  1025. continue;
  1026. }
  1027. model = nd->model ? nd->model : default_model;
  1028. if (!model) {
  1029. continue;
  1030. }
  1031. /* Each bus type is allowed *one* substitution */
  1032. if (g_str_equal(model, alias)) {
  1033. model = alias_target;
  1034. }
  1035. if (!g_ptr_array_find_with_equal_func(nic_models, model,
  1036. g_str_equal, NULL)) {
  1037. /* This NIC does not live on this bus. */
  1038. continue;
  1039. }
  1040. dev = qdev_new(model);
  1041. qdev_set_nic_properties(dev, nd);
  1042. qdev_realize_and_unref(dev, bus, &error_fatal);
  1043. }
  1044. g_ptr_array_free(nic_models, true);
  1045. }
  1046. static int (* const net_client_init_fun[NET_CLIENT_DRIVER__MAX])(
  1047. const Netdev *netdev,
  1048. const char *name,
  1049. NetClientState *peer, Error **errp) = {
  1050. [NET_CLIENT_DRIVER_NIC] = net_init_nic,
  1051. #ifdef CONFIG_SLIRP
  1052. [NET_CLIENT_DRIVER_USER] = net_init_slirp,
  1053. #endif
  1054. [NET_CLIENT_DRIVER_TAP] = net_init_tap,
  1055. [NET_CLIENT_DRIVER_SOCKET] = net_init_socket,
  1056. [NET_CLIENT_DRIVER_STREAM] = net_init_stream,
  1057. [NET_CLIENT_DRIVER_DGRAM] = net_init_dgram,
  1058. #ifdef CONFIG_VDE
  1059. [NET_CLIENT_DRIVER_VDE] = net_init_vde,
  1060. #endif
  1061. #ifdef CONFIG_NETMAP
  1062. [NET_CLIENT_DRIVER_NETMAP] = net_init_netmap,
  1063. #endif
  1064. #ifdef CONFIG_AF_XDP
  1065. [NET_CLIENT_DRIVER_AF_XDP] = net_init_af_xdp,
  1066. #endif
  1067. #ifdef CONFIG_NET_BRIDGE
  1068. [NET_CLIENT_DRIVER_BRIDGE] = net_init_bridge,
  1069. #endif
  1070. [NET_CLIENT_DRIVER_HUBPORT] = net_init_hubport,
  1071. #ifdef CONFIG_VHOST_NET_USER
  1072. [NET_CLIENT_DRIVER_VHOST_USER] = net_init_vhost_user,
  1073. #endif
  1074. #ifdef CONFIG_VHOST_NET_VDPA
  1075. [NET_CLIENT_DRIVER_VHOST_VDPA] = net_init_vhost_vdpa,
  1076. #endif
  1077. #ifdef CONFIG_L2TPV3
  1078. [NET_CLIENT_DRIVER_L2TPV3] = net_init_l2tpv3,
  1079. #endif
  1080. #ifdef CONFIG_VMNET
  1081. [NET_CLIENT_DRIVER_VMNET_HOST] = net_init_vmnet_host,
  1082. [NET_CLIENT_DRIVER_VMNET_SHARED] = net_init_vmnet_shared,
  1083. [NET_CLIENT_DRIVER_VMNET_BRIDGED] = net_init_vmnet_bridged,
  1084. #endif /* CONFIG_VMNET */
  1085. };
  1086. static int net_client_init1(const Netdev *netdev, bool is_netdev, Error **errp)
  1087. {
  1088. NetClientState *peer = NULL;
  1089. NetClientState *nc;
  1090. if (is_netdev) {
  1091. if (netdev->type == NET_CLIENT_DRIVER_NIC ||
  1092. !net_client_init_fun[netdev->type]) {
  1093. error_setg(errp, "network backend '%s' is not compiled into this binary",
  1094. NetClientDriver_str(netdev->type));
  1095. return -1;
  1096. }
  1097. } else {
  1098. if (netdev->type == NET_CLIENT_DRIVER_NONE) {
  1099. return 0; /* nothing to do */
  1100. }
  1101. if (netdev->type == NET_CLIENT_DRIVER_HUBPORT) {
  1102. error_setg(errp, "network backend '%s' is only supported with -netdev/-nic",
  1103. NetClientDriver_str(netdev->type));
  1104. return -1;
  1105. }
  1106. if (!net_client_init_fun[netdev->type]) {
  1107. error_setg(errp, "network backend '%s' is not compiled into this binary",
  1108. NetClientDriver_str(netdev->type));
  1109. return -1;
  1110. }
  1111. /* Do not add to a hub if it's a nic with a netdev= parameter. */
  1112. if (netdev->type != NET_CLIENT_DRIVER_NIC ||
  1113. !netdev->u.nic.netdev) {
  1114. peer = net_hub_add_port(0, NULL, NULL);
  1115. }
  1116. }
  1117. nc = qemu_find_netdev(netdev->id);
  1118. if (nc) {
  1119. error_setg(errp, "Duplicate ID '%s'", netdev->id);
  1120. return -1;
  1121. }
  1122. if (net_client_init_fun[netdev->type](netdev, netdev->id, peer, errp) < 0) {
  1123. /* FIXME drop when all init functions store an Error */
  1124. if (errp && !*errp) {
  1125. error_setg(errp, "Device '%s' could not be initialized",
  1126. NetClientDriver_str(netdev->type));
  1127. }
  1128. return -1;
  1129. }
  1130. if (is_netdev) {
  1131. nc = qemu_find_netdev(netdev->id);
  1132. assert(nc);
  1133. nc->is_netdev = true;
  1134. }
  1135. return 0;
  1136. }
  1137. void show_netdevs(void)
  1138. {
  1139. int idx;
  1140. const char *available_netdevs[] = {
  1141. "socket",
  1142. "stream",
  1143. "dgram",
  1144. "hubport",
  1145. "tap",
  1146. #ifdef CONFIG_SLIRP
  1147. "user",
  1148. #endif
  1149. #ifdef CONFIG_L2TPV3
  1150. "l2tpv3",
  1151. #endif
  1152. #ifdef CONFIG_VDE
  1153. "vde",
  1154. #endif
  1155. #ifdef CONFIG_NET_BRIDGE
  1156. "bridge",
  1157. #endif
  1158. #ifdef CONFIG_NETMAP
  1159. "netmap",
  1160. #endif
  1161. #ifdef CONFIG_AF_XDP
  1162. "af-xdp",
  1163. #endif
  1164. #ifdef CONFIG_POSIX
  1165. "vhost-user",
  1166. #endif
  1167. #ifdef CONFIG_VHOST_VDPA
  1168. "vhost-vdpa",
  1169. #endif
  1170. #ifdef CONFIG_VMNET
  1171. "vmnet-host",
  1172. "vmnet-shared",
  1173. "vmnet-bridged",
  1174. #endif
  1175. };
  1176. qemu_printf("Available netdev backend types:\n");
  1177. for (idx = 0; idx < ARRAY_SIZE(available_netdevs); idx++) {
  1178. qemu_printf("%s\n", available_netdevs[idx]);
  1179. }
  1180. }
  1181. static int net_client_init(QemuOpts *opts, bool is_netdev, Error **errp)
  1182. {
  1183. gchar **substrings = NULL;
  1184. Netdev *object = NULL;
  1185. int ret = -1;
  1186. Visitor *v = opts_visitor_new(opts);
  1187. /* Parse convenience option format ipv6-net=fec0::0[/64] */
  1188. const char *ip6_net = qemu_opt_get(opts, "ipv6-net");
  1189. if (ip6_net) {
  1190. char *prefix_addr;
  1191. unsigned long prefix_len = 64; /* Default 64bit prefix length. */
  1192. substrings = g_strsplit(ip6_net, "/", 2);
  1193. if (!substrings || !substrings[0]) {
  1194. error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "ipv6-net",
  1195. "a valid IPv6 prefix");
  1196. goto out;
  1197. }
  1198. prefix_addr = substrings[0];
  1199. /* Handle user-specified prefix length. */
  1200. if (substrings[1] &&
  1201. qemu_strtoul(substrings[1], NULL, 10, &prefix_len))
  1202. {
  1203. error_setg(errp,
  1204. "parameter 'ipv6-net' expects a number after '/'");
  1205. goto out;
  1206. }
  1207. qemu_opt_set(opts, "ipv6-prefix", prefix_addr, &error_abort);
  1208. qemu_opt_set_number(opts, "ipv6-prefixlen", prefix_len,
  1209. &error_abort);
  1210. qemu_opt_unset(opts, "ipv6-net");
  1211. }
  1212. /* Create an ID for -net if the user did not specify one */
  1213. if (!is_netdev && !qemu_opts_id(opts)) {
  1214. qemu_opts_set_id(opts, id_generate(ID_NET));
  1215. }
  1216. if (visit_type_Netdev(v, NULL, &object, errp)) {
  1217. ret = net_client_init1(object, is_netdev, errp);
  1218. }
  1219. qapi_free_Netdev(object);
  1220. out:
  1221. g_strfreev(substrings);
  1222. visit_free(v);
  1223. return ret;
  1224. }
  1225. void netdev_add(QemuOpts *opts, Error **errp)
  1226. {
  1227. net_client_init(opts, true, errp);
  1228. }
  1229. void qmp_netdev_add(Netdev *netdev, Error **errp)
  1230. {
  1231. if (!id_wellformed(netdev->id)) {
  1232. error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "id", "an identifier");
  1233. return;
  1234. }
  1235. net_client_init1(netdev, true, errp);
  1236. }
  1237. void qmp_netdev_del(const char *id, Error **errp)
  1238. {
  1239. NetClientState *nc;
  1240. QemuOpts *opts;
  1241. nc = qemu_find_netdev(id);
  1242. if (!nc) {
  1243. error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
  1244. "Device '%s' not found", id);
  1245. return;
  1246. }
  1247. if (!nc->is_netdev) {
  1248. error_setg(errp, "Device '%s' is not a netdev", id);
  1249. return;
  1250. }
  1251. qemu_del_net_client(nc);
  1252. /*
  1253. * Wart: we need to delete the QemuOpts associated with netdevs
  1254. * created via CLI or HMP, to avoid bogus "Duplicate ID" errors in
  1255. * HMP netdev_add.
  1256. */
  1257. opts = qemu_opts_find(qemu_find_opts("netdev"), id);
  1258. if (opts) {
  1259. qemu_opts_del(opts);
  1260. }
  1261. }
  1262. static void netfilter_print_info(Monitor *mon, NetFilterState *nf)
  1263. {
  1264. char *str;
  1265. ObjectProperty *prop;
  1266. ObjectPropertyIterator iter;
  1267. Visitor *v;
  1268. /* generate info str */
  1269. object_property_iter_init(&iter, OBJECT(nf));
  1270. while ((prop = object_property_iter_next(&iter))) {
  1271. if (!strcmp(prop->name, "type")) {
  1272. continue;
  1273. }
  1274. v = string_output_visitor_new(false, &str);
  1275. object_property_get(OBJECT(nf), prop->name, v, NULL);
  1276. visit_complete(v, &str);
  1277. visit_free(v);
  1278. monitor_printf(mon, ",%s=%s", prop->name, str);
  1279. g_free(str);
  1280. }
  1281. monitor_printf(mon, "\n");
  1282. }
  1283. void print_net_client(Monitor *mon, NetClientState *nc)
  1284. {
  1285. NetFilterState *nf;
  1286. monitor_printf(mon, "%s: index=%d,type=%s,%s\n", nc->name,
  1287. nc->queue_index,
  1288. NetClientDriver_str(nc->info->type),
  1289. nc->info_str);
  1290. if (!QTAILQ_EMPTY(&nc->filters)) {
  1291. monitor_printf(mon, "filters:\n");
  1292. }
  1293. QTAILQ_FOREACH(nf, &nc->filters, next) {
  1294. monitor_printf(mon, " - %s: type=%s",
  1295. object_get_canonical_path_component(OBJECT(nf)),
  1296. object_get_typename(OBJECT(nf)));
  1297. netfilter_print_info(mon, nf);
  1298. }
  1299. }
  1300. RxFilterInfoList *qmp_query_rx_filter(const char *name, Error **errp)
  1301. {
  1302. NetClientState *nc;
  1303. RxFilterInfoList *filter_list = NULL, **tail = &filter_list;
  1304. QTAILQ_FOREACH(nc, &net_clients, next) {
  1305. RxFilterInfo *info;
  1306. if (name && strcmp(nc->name, name) != 0) {
  1307. continue;
  1308. }
  1309. /* only query rx-filter information of NIC */
  1310. if (nc->info->type != NET_CLIENT_DRIVER_NIC) {
  1311. if (name) {
  1312. error_setg(errp, "net client(%s) isn't a NIC", name);
  1313. assert(!filter_list);
  1314. return NULL;
  1315. }
  1316. continue;
  1317. }
  1318. /* only query information on queue 0 since the info is per nic,
  1319. * not per queue
  1320. */
  1321. if (nc->queue_index != 0)
  1322. continue;
  1323. if (nc->info->query_rx_filter) {
  1324. info = nc->info->query_rx_filter(nc);
  1325. QAPI_LIST_APPEND(tail, info);
  1326. } else if (name) {
  1327. error_setg(errp, "net client(%s) doesn't support"
  1328. " rx-filter querying", name);
  1329. assert(!filter_list);
  1330. return NULL;
  1331. }
  1332. if (name) {
  1333. break;
  1334. }
  1335. }
  1336. if (filter_list == NULL && name) {
  1337. error_setg(errp, "invalid net client name: %s", name);
  1338. }
  1339. return filter_list;
  1340. }
  1341. void colo_notify_filters_event(int event, Error **errp)
  1342. {
  1343. NetClientState *nc;
  1344. NetFilterState *nf;
  1345. NetFilterClass *nfc = NULL;
  1346. Error *local_err = NULL;
  1347. QTAILQ_FOREACH(nc, &net_clients, next) {
  1348. QTAILQ_FOREACH(nf, &nc->filters, next) {
  1349. nfc = NETFILTER_GET_CLASS(OBJECT(nf));
  1350. nfc->handle_event(nf, event, &local_err);
  1351. if (local_err) {
  1352. error_propagate(errp, local_err);
  1353. return;
  1354. }
  1355. }
  1356. }
  1357. }
  1358. void qmp_set_link(const char *name, bool up, Error **errp)
  1359. {
  1360. NetClientState *ncs[MAX_QUEUE_NUM];
  1361. NetClientState *nc;
  1362. int queues, i;
  1363. queues = qemu_find_net_clients_except(name, ncs,
  1364. NET_CLIENT_DRIVER__MAX,
  1365. MAX_QUEUE_NUM);
  1366. if (queues == 0) {
  1367. error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
  1368. "Device '%s' not found", name);
  1369. return;
  1370. }
  1371. nc = ncs[0];
  1372. for (i = 0; i < queues; i++) {
  1373. ncs[i]->link_down = !up;
  1374. }
  1375. if (nc->info->link_status_changed) {
  1376. nc->info->link_status_changed(nc);
  1377. }
  1378. if (nc->peer) {
  1379. /* Change peer link only if the peer is NIC and then notify peer.
  1380. * If the peer is a HUBPORT or a backend, we do not change the
  1381. * link status.
  1382. *
  1383. * This behavior is compatible with qemu hubs where there could be
  1384. * multiple clients that can still communicate with each other in
  1385. * disconnected mode. For now maintain this compatibility.
  1386. */
  1387. if (nc->peer->info->type == NET_CLIENT_DRIVER_NIC) {
  1388. for (i = 0; i < queues; i++) {
  1389. ncs[i]->peer->link_down = !up;
  1390. }
  1391. }
  1392. if (nc->peer->info->link_status_changed) {
  1393. nc->peer->info->link_status_changed(nc->peer);
  1394. }
  1395. }
  1396. }
  1397. static void net_vm_change_state_handler(void *opaque, bool running,
  1398. RunState state)
  1399. {
  1400. NetClientState *nc;
  1401. NetClientState *tmp;
  1402. QTAILQ_FOREACH_SAFE(nc, &net_clients, next, tmp) {
  1403. if (running) {
  1404. /* Flush queued packets and wake up backends. */
  1405. if (nc->peer && qemu_can_send_packet(nc)) {
  1406. qemu_flush_queued_packets(nc->peer);
  1407. }
  1408. } else {
  1409. /* Complete all queued packets, to guarantee we don't modify
  1410. * state later when VM is not running.
  1411. */
  1412. qemu_flush_or_purge_queued_packets(nc, true);
  1413. }
  1414. }
  1415. }
  1416. void net_cleanup(void)
  1417. {
  1418. NetClientState *nc, **p = &QTAILQ_FIRST(&net_clients);
  1419. /*cleanup colo compare module for COLO*/
  1420. colo_compare_cleanup();
  1421. /*
  1422. * Walk the net_clients list and remove the netdevs but *not* any
  1423. * NET_CLIENT_DRIVER_NIC entries. The latter are owned by the device
  1424. * model which created them, and in some cases (e.g. xen-net-device)
  1425. * the device itself may do cleanup at exit and will be upset if we
  1426. * just delete its NIC from underneath it.
  1427. *
  1428. * Since qemu_del_net_client() may delete multiple entries, using
  1429. * QTAILQ_FOREACH_SAFE() is not safe here. The only safe pointer
  1430. * to keep as a bookmark is a NET_CLIENT_DRIVER_NIC entry, so keep
  1431. * 'p' pointing to either the head of the list, or the 'next' field
  1432. * of the latest NET_CLIENT_DRIVER_NIC, and operate on *p as we walk
  1433. * the list.
  1434. *
  1435. * However, the NIC may have peers that trust to be clean beyond this
  1436. * point. For example, if they have been removed with device_del.
  1437. *
  1438. * The 'nc' variable isn't part of the list traversal; it's purely
  1439. * for convenience as too much '(*p)->' has a tendency to make the
  1440. * readers' eyes bleed.
  1441. */
  1442. while (*p) {
  1443. nc = *p;
  1444. if (nc->info->type == NET_CLIENT_DRIVER_NIC) {
  1445. NICState *nic = qemu_get_nic(nc);
  1446. if (nic->peer_deleted) {
  1447. int queues = MAX(nic->conf->peers.queues, 1);
  1448. for (int i = 0; i < queues; i++) {
  1449. nc = qemu_get_subqueue(nic, i);
  1450. qemu_cleanup_net_client(nc->peer, false);
  1451. }
  1452. }
  1453. /* Skip NET_CLIENT_DRIVER_NIC entries */
  1454. p = &QTAILQ_NEXT(nc, next);
  1455. } else {
  1456. qemu_del_net_client(nc);
  1457. }
  1458. }
  1459. qemu_del_vm_change_state_handler(net_change_state_entry);
  1460. }
  1461. void net_check_clients(void)
  1462. {
  1463. NetClientState *nc;
  1464. int i;
  1465. if (nic_model_help) {
  1466. show_nic_models();
  1467. exit(0);
  1468. }
  1469. net_hub_check_clients();
  1470. QTAILQ_FOREACH(nc, &net_clients, next) {
  1471. if (!nc->peer) {
  1472. warn_report("%s %s has no peer",
  1473. nc->info->type == NET_CLIENT_DRIVER_NIC
  1474. ? "nic" : "netdev",
  1475. nc->name);
  1476. }
  1477. }
  1478. /* Check that all NICs requested via -net nic actually got created.
  1479. * NICs created via -device don't need to be checked here because
  1480. * they are always instantiated.
  1481. */
  1482. for (i = 0; i < MAX_NICS; i++) {
  1483. NICInfo *nd = &nd_table[i];
  1484. if (nd->used && !nd->instantiated) {
  1485. warn_report("requested NIC (%s, model %s) "
  1486. "was not created (not supported by this machine?)",
  1487. nd->name ? nd->name : "anonymous",
  1488. nd->model ? nd->model : "unspecified");
  1489. }
  1490. }
  1491. }
  1492. static int net_init_client(void *dummy, QemuOpts *opts, Error **errp)
  1493. {
  1494. const char *model = qemu_opt_get(opts, "model");
  1495. if (is_nic_model_help_option(model)) {
  1496. return 0;
  1497. }
  1498. return net_client_init(opts, false, errp);
  1499. }
  1500. static int net_init_netdev(void *dummy, QemuOpts *opts, Error **errp)
  1501. {
  1502. const char *type = qemu_opt_get(opts, "type");
  1503. if (type && is_help_option(type)) {
  1504. show_netdevs();
  1505. exit(0);
  1506. }
  1507. return net_client_init(opts, true, errp);
  1508. }
  1509. /* For the convenience "--nic" parameter */
  1510. static int net_param_nic(void *dummy, QemuOpts *opts, Error **errp)
  1511. {
  1512. char *mac, *nd_id;
  1513. int idx, ret;
  1514. NICInfo *ni;
  1515. const char *type;
  1516. type = qemu_opt_get(opts, "type");
  1517. if (type) {
  1518. if (g_str_equal(type, "none")) {
  1519. return 0; /* Nothing to do, default_net is cleared in vl.c */
  1520. }
  1521. if (is_help_option(type)) {
  1522. GPtrArray *nic_models = qemu_get_nic_models(TYPE_DEVICE);
  1523. int i;
  1524. show_netdevs();
  1525. printf("\n");
  1526. printf("Available NIC models "
  1527. "(use -nic model=help for a filtered list):\n");
  1528. for (i = 0 ; nic_models->pdata[i]; i++) {
  1529. printf("%s\n", (char *)nic_models->pdata[i]);
  1530. }
  1531. g_ptr_array_free(nic_models, true);
  1532. exit(0);
  1533. }
  1534. }
  1535. idx = nic_get_free_idx();
  1536. if (idx == -1 || nb_nics >= MAX_NICS) {
  1537. error_setg(errp, "no more on-board/default NIC slots available");
  1538. return -1;
  1539. }
  1540. if (!type) {
  1541. qemu_opt_set(opts, "type", "user", &error_abort);
  1542. }
  1543. ni = &nd_table[idx];
  1544. memset(ni, 0, sizeof(*ni));
  1545. ni->model = qemu_opt_get_del(opts, "model");
  1546. if (is_nic_model_help_option(ni->model)) {
  1547. return 0;
  1548. }
  1549. /* Create an ID if the user did not specify one */
  1550. nd_id = g_strdup(qemu_opts_id(opts));
  1551. if (!nd_id) {
  1552. nd_id = id_generate(ID_NET);
  1553. qemu_opts_set_id(opts, nd_id);
  1554. }
  1555. /* Handle MAC address */
  1556. mac = qemu_opt_get_del(opts, "mac");
  1557. if (mac) {
  1558. ret = net_parse_macaddr(ni->macaddr.a, mac);
  1559. g_free(mac);
  1560. if (ret) {
  1561. error_setg(errp, "invalid syntax for ethernet address");
  1562. goto out;
  1563. }
  1564. if (is_multicast_ether_addr(ni->macaddr.a)) {
  1565. error_setg(errp, "NIC cannot have multicast MAC address");
  1566. ret = -1;
  1567. goto out;
  1568. }
  1569. }
  1570. qemu_macaddr_default_if_unset(&ni->macaddr);
  1571. ret = net_client_init(opts, true, errp);
  1572. if (ret == 0) {
  1573. ni->netdev = qemu_find_netdev(nd_id);
  1574. ni->used = true;
  1575. nb_nics++;
  1576. }
  1577. out:
  1578. g_free(nd_id);
  1579. return ret;
  1580. }
  1581. static void netdev_init_modern(void)
  1582. {
  1583. while (!QSIMPLEQ_EMPTY(&nd_queue)) {
  1584. NetdevQueueEntry *nd = QSIMPLEQ_FIRST(&nd_queue);
  1585. QSIMPLEQ_REMOVE_HEAD(&nd_queue, entry);
  1586. loc_push_restore(&nd->loc);
  1587. net_client_init1(nd->nd, true, &error_fatal);
  1588. loc_pop(&nd->loc);
  1589. qapi_free_Netdev(nd->nd);
  1590. g_free(nd);
  1591. }
  1592. }
  1593. void net_init_clients(void)
  1594. {
  1595. net_change_state_entry =
  1596. qemu_add_vm_change_state_handler(net_vm_change_state_handler, NULL);
  1597. QTAILQ_INIT(&net_clients);
  1598. netdev_init_modern();
  1599. qemu_opts_foreach(qemu_find_opts("netdev"), net_init_netdev, NULL,
  1600. &error_fatal);
  1601. qemu_opts_foreach(qemu_find_opts("nic"), net_param_nic, NULL,
  1602. &error_fatal);
  1603. qemu_opts_foreach(qemu_find_opts("net"), net_init_client, NULL,
  1604. &error_fatal);
  1605. }
  1606. /*
  1607. * Does this -netdev argument use modern rather than traditional syntax?
  1608. * Modern syntax is to be parsed with netdev_parse_modern().
  1609. * Traditional syntax is to be parsed with net_client_parse().
  1610. */
  1611. bool netdev_is_modern(const char *optstr)
  1612. {
  1613. QemuOpts *opts;
  1614. bool is_modern;
  1615. const char *type;
  1616. static QemuOptsList dummy_opts = {
  1617. .name = "netdev",
  1618. .implied_opt_name = "type",
  1619. .head = QTAILQ_HEAD_INITIALIZER(dummy_opts.head),
  1620. .desc = { { } },
  1621. };
  1622. if (optstr[0] == '{') {
  1623. /* This is JSON, which means it's modern syntax */
  1624. return true;
  1625. }
  1626. opts = qemu_opts_create(&dummy_opts, NULL, false, &error_abort);
  1627. qemu_opts_do_parse(opts, optstr, dummy_opts.implied_opt_name,
  1628. &error_abort);
  1629. type = qemu_opt_get(opts, "type");
  1630. is_modern = !g_strcmp0(type, "stream") || !g_strcmp0(type, "dgram");
  1631. qemu_opts_reset(&dummy_opts);
  1632. return is_modern;
  1633. }
  1634. /*
  1635. * netdev_parse_modern() uses modern, more expressive syntax than
  1636. * net_client_parse(), but supports only the -netdev option.
  1637. * netdev_parse_modern() appends to @nd_queue, whereas net_client_parse()
  1638. * appends to @qemu_netdev_opts.
  1639. */
  1640. void netdev_parse_modern(const char *optstr)
  1641. {
  1642. Visitor *v;
  1643. NetdevQueueEntry *nd;
  1644. v = qobject_input_visitor_new_str(optstr, "type", &error_fatal);
  1645. nd = g_new(NetdevQueueEntry, 1);
  1646. visit_type_Netdev(v, NULL, &nd->nd, &error_fatal);
  1647. visit_free(v);
  1648. loc_save(&nd->loc);
  1649. QSIMPLEQ_INSERT_TAIL(&nd_queue, nd, entry);
  1650. }
  1651. void net_client_parse(QemuOptsList *opts_list, const char *optstr)
  1652. {
  1653. if (!qemu_opts_parse_noisily(opts_list, optstr, true)) {
  1654. exit(1);
  1655. }
  1656. }
  1657. /* From FreeBSD */
  1658. /* XXX: optimize */
  1659. uint32_t net_crc32(const uint8_t *p, int len)
  1660. {
  1661. uint32_t crc;
  1662. int carry, i, j;
  1663. uint8_t b;
  1664. crc = 0xffffffff;
  1665. for (i = 0; i < len; i++) {
  1666. b = *p++;
  1667. for (j = 0; j < 8; j++) {
  1668. carry = ((crc & 0x80000000L) ? 1 : 0) ^ (b & 0x01);
  1669. crc <<= 1;
  1670. b >>= 1;
  1671. if (carry) {
  1672. crc = ((crc ^ POLYNOMIAL_BE) | carry);
  1673. }
  1674. }
  1675. }
  1676. return crc;
  1677. }
  1678. uint32_t net_crc32_le(const uint8_t *p, int len)
  1679. {
  1680. uint32_t crc;
  1681. int carry, i, j;
  1682. uint8_t b;
  1683. crc = 0xffffffff;
  1684. for (i = 0; i < len; i++) {
  1685. b = *p++;
  1686. for (j = 0; j < 8; j++) {
  1687. carry = (crc & 0x1) ^ (b & 0x01);
  1688. crc >>= 1;
  1689. b >>= 1;
  1690. if (carry) {
  1691. crc ^= POLYNOMIAL_LE;
  1692. }
  1693. }
  1694. }
  1695. return crc;
  1696. }
  1697. QemuOptsList qemu_netdev_opts = {
  1698. .name = "netdev",
  1699. .implied_opt_name = "type",
  1700. .head = QTAILQ_HEAD_INITIALIZER(qemu_netdev_opts.head),
  1701. .desc = {
  1702. /*
  1703. * no elements => accept any params
  1704. * validation will happen later
  1705. */
  1706. { /* end of list */ }
  1707. },
  1708. };
  1709. QemuOptsList qemu_nic_opts = {
  1710. .name = "nic",
  1711. .implied_opt_name = "type",
  1712. .head = QTAILQ_HEAD_INITIALIZER(qemu_nic_opts.head),
  1713. .desc = {
  1714. /*
  1715. * no elements => accept any params
  1716. * validation will happen later
  1717. */
  1718. { /* end of list */ }
  1719. },
  1720. };
  1721. QemuOptsList qemu_net_opts = {
  1722. .name = "net",
  1723. .implied_opt_name = "type",
  1724. .head = QTAILQ_HEAD_INITIALIZER(qemu_net_opts.head),
  1725. .desc = {
  1726. /*
  1727. * no elements => accept any params
  1728. * validation will happen later
  1729. */
  1730. { /* end of list */ }
  1731. },
  1732. };
  1733. void net_socket_rs_init(SocketReadState *rs,
  1734. SocketReadStateFinalize *finalize,
  1735. bool vnet_hdr)
  1736. {
  1737. rs->state = 0;
  1738. rs->vnet_hdr = vnet_hdr;
  1739. rs->index = 0;
  1740. rs->packet_len = 0;
  1741. rs->vnet_hdr_len = 0;
  1742. memset(rs->buf, 0, sizeof(rs->buf));
  1743. rs->finalize = finalize;
  1744. }
  1745. /*
  1746. * Returns
  1747. * 0: success
  1748. * -1: error occurs
  1749. */
  1750. int net_fill_rstate(SocketReadState *rs, const uint8_t *buf, int size)
  1751. {
  1752. unsigned int l;
  1753. while (size > 0) {
  1754. /* Reassemble a packet from the network.
  1755. * 0 = getting length.
  1756. * 1 = getting vnet header length.
  1757. * 2 = getting data.
  1758. */
  1759. switch (rs->state) {
  1760. case 0:
  1761. l = 4 - rs->index;
  1762. if (l > size) {
  1763. l = size;
  1764. }
  1765. memcpy(rs->buf + rs->index, buf, l);
  1766. buf += l;
  1767. size -= l;
  1768. rs->index += l;
  1769. if (rs->index == 4) {
  1770. /* got length */
  1771. rs->packet_len = ntohl(*(uint32_t *)rs->buf);
  1772. rs->index = 0;
  1773. if (rs->vnet_hdr) {
  1774. rs->state = 1;
  1775. } else {
  1776. rs->state = 2;
  1777. rs->vnet_hdr_len = 0;
  1778. }
  1779. }
  1780. break;
  1781. case 1:
  1782. l = 4 - rs->index;
  1783. if (l > size) {
  1784. l = size;
  1785. }
  1786. memcpy(rs->buf + rs->index, buf, l);
  1787. buf += l;
  1788. size -= l;
  1789. rs->index += l;
  1790. if (rs->index == 4) {
  1791. /* got vnet header length */
  1792. rs->vnet_hdr_len = ntohl(*(uint32_t *)rs->buf);
  1793. rs->index = 0;
  1794. rs->state = 2;
  1795. }
  1796. break;
  1797. case 2:
  1798. l = rs->packet_len - rs->index;
  1799. if (l > size) {
  1800. l = size;
  1801. }
  1802. if (rs->index + l <= sizeof(rs->buf)) {
  1803. memcpy(rs->buf + rs->index, buf, l);
  1804. } else {
  1805. fprintf(stderr, "serious error: oversized packet received,"
  1806. "connection terminated.\n");
  1807. rs->index = rs->state = 0;
  1808. return -1;
  1809. }
  1810. rs->index += l;
  1811. buf += l;
  1812. size -= l;
  1813. if (rs->index >= rs->packet_len) {
  1814. rs->index = 0;
  1815. rs->state = 0;
  1816. assert(rs->finalize);
  1817. rs->finalize(rs);
  1818. }
  1819. break;
  1820. }
  1821. }
  1822. assert(size == 0);
  1823. return 0;
  1824. }