net.c 49 KB

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