2
0

net.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864
  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. static QTAILQ_HEAD(, NetClientState) 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. void qemu_using_vnet_hdr(NetClientState *nc, bool enable)
  431. {
  432. if (!nc || !nc->info->using_vnet_hdr) {
  433. return;
  434. }
  435. nc->info->using_vnet_hdr(nc, enable);
  436. }
  437. void qemu_set_offload(NetClientState *nc, int csum, int tso4, int tso6,
  438. int ecn, int ufo)
  439. {
  440. if (!nc || !nc->info->set_offload) {
  441. return;
  442. }
  443. nc->info->set_offload(nc, csum, tso4, tso6, ecn, ufo);
  444. }
  445. void qemu_set_vnet_hdr_len(NetClientState *nc, int len)
  446. {
  447. if (!nc || !nc->info->set_vnet_hdr_len) {
  448. return;
  449. }
  450. nc->vnet_hdr_len = len;
  451. nc->info->set_vnet_hdr_len(nc, len);
  452. }
  453. int qemu_set_vnet_le(NetClientState *nc, bool is_le)
  454. {
  455. #if HOST_BIG_ENDIAN
  456. if (!nc || !nc->info->set_vnet_le) {
  457. return -ENOSYS;
  458. }
  459. return nc->info->set_vnet_le(nc, is_le);
  460. #else
  461. return 0;
  462. #endif
  463. }
  464. int qemu_set_vnet_be(NetClientState *nc, bool is_be)
  465. {
  466. #if HOST_BIG_ENDIAN
  467. return 0;
  468. #else
  469. if (!nc || !nc->info->set_vnet_be) {
  470. return -ENOSYS;
  471. }
  472. return nc->info->set_vnet_be(nc, is_be);
  473. #endif
  474. }
  475. int qemu_can_receive_packet(NetClientState *nc)
  476. {
  477. if (nc->receive_disabled) {
  478. return 0;
  479. } else if (nc->info->can_receive &&
  480. !nc->info->can_receive(nc)) {
  481. return 0;
  482. }
  483. return 1;
  484. }
  485. int qemu_can_send_packet(NetClientState *sender)
  486. {
  487. int vm_running = runstate_is_running();
  488. if (!vm_running) {
  489. return 0;
  490. }
  491. if (!sender->peer) {
  492. return 1;
  493. }
  494. return qemu_can_receive_packet(sender->peer);
  495. }
  496. static ssize_t filter_receive_iov(NetClientState *nc,
  497. NetFilterDirection direction,
  498. NetClientState *sender,
  499. unsigned flags,
  500. const struct iovec *iov,
  501. int iovcnt,
  502. NetPacketSent *sent_cb)
  503. {
  504. ssize_t ret = 0;
  505. NetFilterState *nf = NULL;
  506. if (direction == NET_FILTER_DIRECTION_TX) {
  507. QTAILQ_FOREACH(nf, &nc->filters, next) {
  508. ret = qemu_netfilter_receive(nf, direction, sender, flags, iov,
  509. iovcnt, sent_cb);
  510. if (ret) {
  511. return ret;
  512. }
  513. }
  514. } else {
  515. QTAILQ_FOREACH_REVERSE(nf, &nc->filters, next) {
  516. ret = qemu_netfilter_receive(nf, direction, sender, flags, iov,
  517. iovcnt, sent_cb);
  518. if (ret) {
  519. return ret;
  520. }
  521. }
  522. }
  523. return ret;
  524. }
  525. static ssize_t filter_receive(NetClientState *nc,
  526. NetFilterDirection direction,
  527. NetClientState *sender,
  528. unsigned flags,
  529. const uint8_t *data,
  530. size_t size,
  531. NetPacketSent *sent_cb)
  532. {
  533. struct iovec iov = {
  534. .iov_base = (void *)data,
  535. .iov_len = size
  536. };
  537. return filter_receive_iov(nc, direction, sender, flags, &iov, 1, sent_cb);
  538. }
  539. void qemu_purge_queued_packets(NetClientState *nc)
  540. {
  541. if (!nc->peer) {
  542. return;
  543. }
  544. qemu_net_queue_purge(nc->peer->incoming_queue, nc);
  545. }
  546. void qemu_flush_or_purge_queued_packets(NetClientState *nc, bool purge)
  547. {
  548. nc->receive_disabled = 0;
  549. if (nc->peer && nc->peer->info->type == NET_CLIENT_DRIVER_HUBPORT) {
  550. if (net_hub_flush(nc->peer)) {
  551. qemu_notify_event();
  552. }
  553. }
  554. if (qemu_net_queue_flush(nc->incoming_queue)) {
  555. /* We emptied the queue successfully, signal to the IO thread to repoll
  556. * the file descriptor (for tap, for example).
  557. */
  558. qemu_notify_event();
  559. } else if (purge) {
  560. /* Unable to empty the queue, purge remaining packets */
  561. qemu_net_queue_purge(nc->incoming_queue, nc->peer);
  562. }
  563. }
  564. void qemu_flush_queued_packets(NetClientState *nc)
  565. {
  566. qemu_flush_or_purge_queued_packets(nc, false);
  567. }
  568. static ssize_t qemu_send_packet_async_with_flags(NetClientState *sender,
  569. unsigned flags,
  570. const uint8_t *buf, int size,
  571. NetPacketSent *sent_cb)
  572. {
  573. NetQueue *queue;
  574. int ret;
  575. #ifdef DEBUG_NET
  576. printf("qemu_send_packet_async:\n");
  577. qemu_hexdump(stdout, "net", buf, size);
  578. #endif
  579. if (sender->link_down || !sender->peer) {
  580. return size;
  581. }
  582. /* Let filters handle the packet first */
  583. ret = filter_receive(sender, NET_FILTER_DIRECTION_TX,
  584. sender, flags, buf, size, sent_cb);
  585. if (ret) {
  586. return ret;
  587. }
  588. ret = filter_receive(sender->peer, NET_FILTER_DIRECTION_RX,
  589. sender, flags, buf, size, sent_cb);
  590. if (ret) {
  591. return ret;
  592. }
  593. queue = sender->peer->incoming_queue;
  594. return qemu_net_queue_send(queue, sender, flags, buf, size, sent_cb);
  595. }
  596. ssize_t qemu_send_packet_async(NetClientState *sender,
  597. const uint8_t *buf, int size,
  598. NetPacketSent *sent_cb)
  599. {
  600. return qemu_send_packet_async_with_flags(sender, QEMU_NET_PACKET_FLAG_NONE,
  601. buf, size, sent_cb);
  602. }
  603. ssize_t qemu_send_packet(NetClientState *nc, const uint8_t *buf, int size)
  604. {
  605. return qemu_send_packet_async(nc, buf, size, NULL);
  606. }
  607. ssize_t qemu_receive_packet(NetClientState *nc, const uint8_t *buf, int size)
  608. {
  609. if (!qemu_can_receive_packet(nc)) {
  610. return 0;
  611. }
  612. return qemu_net_queue_receive(nc->incoming_queue, buf, size);
  613. }
  614. ssize_t qemu_receive_packet_iov(NetClientState *nc, const struct iovec *iov,
  615. int iovcnt)
  616. {
  617. if (!qemu_can_receive_packet(nc)) {
  618. return 0;
  619. }
  620. return qemu_net_queue_receive_iov(nc->incoming_queue, iov, iovcnt);
  621. }
  622. ssize_t qemu_send_packet_raw(NetClientState *nc, const uint8_t *buf, int size)
  623. {
  624. return qemu_send_packet_async_with_flags(nc, QEMU_NET_PACKET_FLAG_RAW,
  625. buf, size, NULL);
  626. }
  627. static ssize_t nc_sendv_compat(NetClientState *nc, const struct iovec *iov,
  628. int iovcnt, unsigned flags)
  629. {
  630. uint8_t *buf = NULL;
  631. uint8_t *buffer;
  632. size_t offset;
  633. ssize_t ret;
  634. if (iovcnt == 1) {
  635. buffer = iov[0].iov_base;
  636. offset = iov[0].iov_len;
  637. } else {
  638. offset = iov_size(iov, iovcnt);
  639. if (offset > NET_BUFSIZE) {
  640. return -1;
  641. }
  642. buf = g_malloc(offset);
  643. buffer = buf;
  644. offset = iov_to_buf(iov, iovcnt, 0, buf, offset);
  645. }
  646. if (flags & QEMU_NET_PACKET_FLAG_RAW && nc->info->receive_raw) {
  647. ret = nc->info->receive_raw(nc, buffer, offset);
  648. } else {
  649. ret = nc->info->receive(nc, buffer, offset);
  650. }
  651. g_free(buf);
  652. return ret;
  653. }
  654. static ssize_t qemu_deliver_packet_iov(NetClientState *sender,
  655. unsigned flags,
  656. const struct iovec *iov,
  657. int iovcnt,
  658. void *opaque)
  659. {
  660. NetClientState *nc = opaque;
  661. int ret;
  662. if (nc->link_down) {
  663. return iov_size(iov, iovcnt);
  664. }
  665. if (nc->receive_disabled) {
  666. return 0;
  667. }
  668. if (nc->info->receive_iov && !(flags & QEMU_NET_PACKET_FLAG_RAW)) {
  669. ret = nc->info->receive_iov(nc, iov, iovcnt);
  670. } else {
  671. ret = nc_sendv_compat(nc, iov, iovcnt, flags);
  672. }
  673. if (ret == 0) {
  674. nc->receive_disabled = 1;
  675. }
  676. return ret;
  677. }
  678. ssize_t qemu_sendv_packet_async(NetClientState *sender,
  679. const struct iovec *iov, int iovcnt,
  680. NetPacketSent *sent_cb)
  681. {
  682. NetQueue *queue;
  683. size_t size = iov_size(iov, iovcnt);
  684. int ret;
  685. if (size > NET_BUFSIZE) {
  686. return size;
  687. }
  688. if (sender->link_down || !sender->peer) {
  689. return size;
  690. }
  691. /* Let filters handle the packet first */
  692. ret = filter_receive_iov(sender, NET_FILTER_DIRECTION_TX, sender,
  693. QEMU_NET_PACKET_FLAG_NONE, iov, iovcnt, sent_cb);
  694. if (ret) {
  695. return ret;
  696. }
  697. ret = filter_receive_iov(sender->peer, NET_FILTER_DIRECTION_RX, sender,
  698. QEMU_NET_PACKET_FLAG_NONE, iov, iovcnt, sent_cb);
  699. if (ret) {
  700. return ret;
  701. }
  702. queue = sender->peer->incoming_queue;
  703. return qemu_net_queue_send_iov(queue, sender,
  704. QEMU_NET_PACKET_FLAG_NONE,
  705. iov, iovcnt, sent_cb);
  706. }
  707. ssize_t
  708. qemu_sendv_packet(NetClientState *nc, const struct iovec *iov, int iovcnt)
  709. {
  710. return qemu_sendv_packet_async(nc, iov, iovcnt, NULL);
  711. }
  712. NetClientState *qemu_find_netdev(const char *id)
  713. {
  714. NetClientState *nc;
  715. QTAILQ_FOREACH(nc, &net_clients, next) {
  716. if (nc->info->type == NET_CLIENT_DRIVER_NIC)
  717. continue;
  718. if (!strcmp(nc->name, id)) {
  719. return nc;
  720. }
  721. }
  722. return NULL;
  723. }
  724. int qemu_find_net_clients_except(const char *id, NetClientState **ncs,
  725. NetClientDriver type, int max)
  726. {
  727. NetClientState *nc;
  728. int ret = 0;
  729. QTAILQ_FOREACH(nc, &net_clients, next) {
  730. if (nc->info->type == type) {
  731. continue;
  732. }
  733. if (!id || !strcmp(nc->name, id)) {
  734. if (ret < max) {
  735. ncs[ret] = nc;
  736. }
  737. ret++;
  738. }
  739. }
  740. return ret;
  741. }
  742. static int nic_get_free_idx(void)
  743. {
  744. int index;
  745. for (index = 0; index < MAX_NICS; index++)
  746. if (!nd_table[index].used)
  747. return index;
  748. return -1;
  749. }
  750. int qemu_show_nic_models(const char *arg, const char *const *models)
  751. {
  752. int i;
  753. if (!arg || !is_help_option(arg)) {
  754. return 0;
  755. }
  756. printf("Supported NIC models:\n");
  757. for (i = 0 ; models[i]; i++) {
  758. printf("%s\n", models[i]);
  759. }
  760. return 1;
  761. }
  762. void qemu_check_nic_model(NICInfo *nd, const char *model)
  763. {
  764. const char *models[2];
  765. models[0] = model;
  766. models[1] = NULL;
  767. if (qemu_show_nic_models(nd->model, models))
  768. exit(0);
  769. if (qemu_find_nic_model(nd, models, model) < 0)
  770. exit(1);
  771. }
  772. int qemu_find_nic_model(NICInfo *nd, const char * const *models,
  773. const char *default_model)
  774. {
  775. int i;
  776. if (!nd->model)
  777. nd->model = g_strdup(default_model);
  778. for (i = 0 ; models[i]; i++) {
  779. if (strcmp(nd->model, models[i]) == 0)
  780. return i;
  781. }
  782. error_report("Unsupported NIC model: %s", nd->model);
  783. return -1;
  784. }
  785. static int net_init_nic(const Netdev *netdev, const char *name,
  786. NetClientState *peer, Error **errp)
  787. {
  788. int idx;
  789. NICInfo *nd;
  790. const NetLegacyNicOptions *nic;
  791. assert(netdev->type == NET_CLIENT_DRIVER_NIC);
  792. nic = &netdev->u.nic;
  793. idx = nic_get_free_idx();
  794. if (idx == -1 || nb_nics >= MAX_NICS) {
  795. error_setg(errp, "too many NICs");
  796. return -1;
  797. }
  798. nd = &nd_table[idx];
  799. memset(nd, 0, sizeof(*nd));
  800. if (nic->netdev) {
  801. nd->netdev = qemu_find_netdev(nic->netdev);
  802. if (!nd->netdev) {
  803. error_setg(errp, "netdev '%s' not found", nic->netdev);
  804. return -1;
  805. }
  806. } else {
  807. assert(peer);
  808. nd->netdev = peer;
  809. }
  810. nd->name = g_strdup(name);
  811. if (nic->model) {
  812. nd->model = g_strdup(nic->model);
  813. }
  814. if (nic->addr) {
  815. nd->devaddr = g_strdup(nic->addr);
  816. }
  817. if (nic->macaddr &&
  818. net_parse_macaddr(nd->macaddr.a, nic->macaddr) < 0) {
  819. error_setg(errp, "invalid syntax for ethernet address");
  820. return -1;
  821. }
  822. if (nic->macaddr &&
  823. is_multicast_ether_addr(nd->macaddr.a)) {
  824. error_setg(errp,
  825. "NIC cannot have multicast MAC address (odd 1st byte)");
  826. return -1;
  827. }
  828. qemu_macaddr_default_if_unset(&nd->macaddr);
  829. if (nic->has_vectors) {
  830. if (nic->vectors > 0x7ffffff) {
  831. error_setg(errp, "invalid # of vectors: %"PRIu32, nic->vectors);
  832. return -1;
  833. }
  834. nd->nvectors = nic->vectors;
  835. } else {
  836. nd->nvectors = DEV_NVECTORS_UNSPECIFIED;
  837. }
  838. nd->used = 1;
  839. nb_nics++;
  840. return idx;
  841. }
  842. static int (* const net_client_init_fun[NET_CLIENT_DRIVER__MAX])(
  843. const Netdev *netdev,
  844. const char *name,
  845. NetClientState *peer, Error **errp) = {
  846. [NET_CLIENT_DRIVER_NIC] = net_init_nic,
  847. #ifdef CONFIG_SLIRP
  848. [NET_CLIENT_DRIVER_USER] = net_init_slirp,
  849. #endif
  850. [NET_CLIENT_DRIVER_TAP] = net_init_tap,
  851. [NET_CLIENT_DRIVER_SOCKET] = net_init_socket,
  852. [NET_CLIENT_DRIVER_STREAM] = net_init_stream,
  853. [NET_CLIENT_DRIVER_DGRAM] = net_init_dgram,
  854. #ifdef CONFIG_VDE
  855. [NET_CLIENT_DRIVER_VDE] = net_init_vde,
  856. #endif
  857. #ifdef CONFIG_NETMAP
  858. [NET_CLIENT_DRIVER_NETMAP] = net_init_netmap,
  859. #endif
  860. #ifdef CONFIG_NET_BRIDGE
  861. [NET_CLIENT_DRIVER_BRIDGE] = net_init_bridge,
  862. #endif
  863. [NET_CLIENT_DRIVER_HUBPORT] = net_init_hubport,
  864. #ifdef CONFIG_VHOST_NET_USER
  865. [NET_CLIENT_DRIVER_VHOST_USER] = net_init_vhost_user,
  866. #endif
  867. #ifdef CONFIG_VHOST_NET_VDPA
  868. [NET_CLIENT_DRIVER_VHOST_VDPA] = net_init_vhost_vdpa,
  869. #endif
  870. #ifdef CONFIG_L2TPV3
  871. [NET_CLIENT_DRIVER_L2TPV3] = net_init_l2tpv3,
  872. #endif
  873. #ifdef CONFIG_VMNET
  874. [NET_CLIENT_DRIVER_VMNET_HOST] = net_init_vmnet_host,
  875. [NET_CLIENT_DRIVER_VMNET_SHARED] = net_init_vmnet_shared,
  876. [NET_CLIENT_DRIVER_VMNET_BRIDGED] = net_init_vmnet_bridged,
  877. #endif /* CONFIG_VMNET */
  878. };
  879. static int net_client_init1(const Netdev *netdev, bool is_netdev, Error **errp)
  880. {
  881. NetClientState *peer = NULL;
  882. NetClientState *nc;
  883. if (is_netdev) {
  884. if (netdev->type == NET_CLIENT_DRIVER_NIC ||
  885. !net_client_init_fun[netdev->type]) {
  886. error_setg(errp, "network backend '%s' is not compiled into this binary",
  887. NetClientDriver_str(netdev->type));
  888. return -1;
  889. }
  890. } else {
  891. if (netdev->type == NET_CLIENT_DRIVER_NONE) {
  892. return 0; /* nothing to do */
  893. }
  894. if (netdev->type == NET_CLIENT_DRIVER_HUBPORT) {
  895. error_setg(errp, "network backend '%s' is only supported with -netdev/-nic",
  896. NetClientDriver_str(netdev->type));
  897. return -1;
  898. }
  899. if (!net_client_init_fun[netdev->type]) {
  900. error_setg(errp, "network backend '%s' is not compiled into this binary",
  901. NetClientDriver_str(netdev->type));
  902. return -1;
  903. }
  904. /* Do not add to a hub if it's a nic with a netdev= parameter. */
  905. if (netdev->type != NET_CLIENT_DRIVER_NIC ||
  906. !netdev->u.nic.netdev) {
  907. peer = net_hub_add_port(0, NULL, NULL);
  908. }
  909. }
  910. nc = qemu_find_netdev(netdev->id);
  911. if (nc) {
  912. error_setg(errp, "Duplicate ID '%s'", netdev->id);
  913. return -1;
  914. }
  915. if (net_client_init_fun[netdev->type](netdev, netdev->id, peer, errp) < 0) {
  916. /* FIXME drop when all init functions store an Error */
  917. if (errp && !*errp) {
  918. error_setg(errp, "Device '%s' could not be initialized",
  919. NetClientDriver_str(netdev->type));
  920. }
  921. return -1;
  922. }
  923. if (is_netdev) {
  924. nc = qemu_find_netdev(netdev->id);
  925. assert(nc);
  926. nc->is_netdev = true;
  927. }
  928. return 0;
  929. }
  930. void show_netdevs(void)
  931. {
  932. int idx;
  933. const char *available_netdevs[] = {
  934. "socket",
  935. "stream",
  936. "dgram",
  937. "hubport",
  938. "tap",
  939. #ifdef CONFIG_SLIRP
  940. "user",
  941. #endif
  942. #ifdef CONFIG_L2TPV3
  943. "l2tpv3",
  944. #endif
  945. #ifdef CONFIG_VDE
  946. "vde",
  947. #endif
  948. #ifdef CONFIG_NET_BRIDGE
  949. "bridge",
  950. #endif
  951. #ifdef CONFIG_NETMAP
  952. "netmap",
  953. #endif
  954. #ifdef CONFIG_POSIX
  955. "vhost-user",
  956. #endif
  957. #ifdef CONFIG_VHOST_VDPA
  958. "vhost-vdpa",
  959. #endif
  960. #ifdef CONFIG_VMNET
  961. "vmnet-host",
  962. "vmnet-shared",
  963. "vmnet-bridged",
  964. #endif
  965. };
  966. qemu_printf("Available netdev backend types:\n");
  967. for (idx = 0; idx < ARRAY_SIZE(available_netdevs); idx++) {
  968. qemu_printf("%s\n", available_netdevs[idx]);
  969. }
  970. }
  971. static int net_client_init(QemuOpts *opts, bool is_netdev, Error **errp)
  972. {
  973. gchar **substrings = NULL;
  974. Netdev *object = NULL;
  975. int ret = -1;
  976. Visitor *v = opts_visitor_new(opts);
  977. /* Parse convenience option format ip6-net=fec0::0[/64] */
  978. const char *ip6_net = qemu_opt_get(opts, "ipv6-net");
  979. if (ip6_net) {
  980. char *prefix_addr;
  981. unsigned long prefix_len = 64; /* Default 64bit prefix length. */
  982. substrings = g_strsplit(ip6_net, "/", 2);
  983. if (!substrings || !substrings[0]) {
  984. error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "ipv6-net",
  985. "a valid IPv6 prefix");
  986. goto out;
  987. }
  988. prefix_addr = substrings[0];
  989. /* Handle user-specified prefix length. */
  990. if (substrings[1] &&
  991. qemu_strtoul(substrings[1], NULL, 10, &prefix_len))
  992. {
  993. error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
  994. "ipv6-prefixlen", "a number");
  995. goto out;
  996. }
  997. qemu_opt_set(opts, "ipv6-prefix", prefix_addr, &error_abort);
  998. qemu_opt_set_number(opts, "ipv6-prefixlen", prefix_len,
  999. &error_abort);
  1000. qemu_opt_unset(opts, "ipv6-net");
  1001. }
  1002. /* Create an ID for -net if the user did not specify one */
  1003. if (!is_netdev && !qemu_opts_id(opts)) {
  1004. qemu_opts_set_id(opts, id_generate(ID_NET));
  1005. }
  1006. if (visit_type_Netdev(v, NULL, &object, errp)) {
  1007. ret = net_client_init1(object, is_netdev, errp);
  1008. }
  1009. qapi_free_Netdev(object);
  1010. out:
  1011. g_strfreev(substrings);
  1012. visit_free(v);
  1013. return ret;
  1014. }
  1015. void netdev_add(QemuOpts *opts, Error **errp)
  1016. {
  1017. net_client_init(opts, true, errp);
  1018. }
  1019. void qmp_netdev_add(Netdev *netdev, Error **errp)
  1020. {
  1021. if (!id_wellformed(netdev->id)) {
  1022. error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "id", "an identifier");
  1023. return;
  1024. }
  1025. net_client_init1(netdev, true, errp);
  1026. }
  1027. void qmp_netdev_del(const char *id, Error **errp)
  1028. {
  1029. NetClientState *nc;
  1030. QemuOpts *opts;
  1031. nc = qemu_find_netdev(id);
  1032. if (!nc) {
  1033. error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
  1034. "Device '%s' not found", id);
  1035. return;
  1036. }
  1037. if (!nc->is_netdev) {
  1038. error_setg(errp, "Device '%s' is not a netdev", id);
  1039. return;
  1040. }
  1041. qemu_del_net_client(nc);
  1042. /*
  1043. * Wart: we need to delete the QemuOpts associated with netdevs
  1044. * created via CLI or HMP, to avoid bogus "Duplicate ID" errors in
  1045. * HMP netdev_add.
  1046. */
  1047. opts = qemu_opts_find(qemu_find_opts("netdev"), id);
  1048. if (opts) {
  1049. qemu_opts_del(opts);
  1050. }
  1051. }
  1052. static void netfilter_print_info(Monitor *mon, NetFilterState *nf)
  1053. {
  1054. char *str;
  1055. ObjectProperty *prop;
  1056. ObjectPropertyIterator iter;
  1057. Visitor *v;
  1058. /* generate info str */
  1059. object_property_iter_init(&iter, OBJECT(nf));
  1060. while ((prop = object_property_iter_next(&iter))) {
  1061. if (!strcmp(prop->name, "type")) {
  1062. continue;
  1063. }
  1064. v = string_output_visitor_new(false, &str);
  1065. object_property_get(OBJECT(nf), prop->name, v, NULL);
  1066. visit_complete(v, &str);
  1067. visit_free(v);
  1068. monitor_printf(mon, ",%s=%s", prop->name, str);
  1069. g_free(str);
  1070. }
  1071. monitor_printf(mon, "\n");
  1072. }
  1073. void print_net_client(Monitor *mon, NetClientState *nc)
  1074. {
  1075. NetFilterState *nf;
  1076. monitor_printf(mon, "%s: index=%d,type=%s,%s\n", nc->name,
  1077. nc->queue_index,
  1078. NetClientDriver_str(nc->info->type),
  1079. nc->info_str);
  1080. if (!QTAILQ_EMPTY(&nc->filters)) {
  1081. monitor_printf(mon, "filters:\n");
  1082. }
  1083. QTAILQ_FOREACH(nf, &nc->filters, next) {
  1084. monitor_printf(mon, " - %s: type=%s",
  1085. object_get_canonical_path_component(OBJECT(nf)),
  1086. object_get_typename(OBJECT(nf)));
  1087. netfilter_print_info(mon, nf);
  1088. }
  1089. }
  1090. RxFilterInfoList *qmp_query_rx_filter(const char *name, Error **errp)
  1091. {
  1092. NetClientState *nc;
  1093. RxFilterInfoList *filter_list = NULL, **tail = &filter_list;
  1094. QTAILQ_FOREACH(nc, &net_clients, next) {
  1095. RxFilterInfo *info;
  1096. if (name && strcmp(nc->name, name) != 0) {
  1097. continue;
  1098. }
  1099. /* only query rx-filter information of NIC */
  1100. if (nc->info->type != NET_CLIENT_DRIVER_NIC) {
  1101. if (name) {
  1102. error_setg(errp, "net client(%s) isn't a NIC", name);
  1103. assert(!filter_list);
  1104. return NULL;
  1105. }
  1106. continue;
  1107. }
  1108. /* only query information on queue 0 since the info is per nic,
  1109. * not per queue
  1110. */
  1111. if (nc->queue_index != 0)
  1112. continue;
  1113. if (nc->info->query_rx_filter) {
  1114. info = nc->info->query_rx_filter(nc);
  1115. QAPI_LIST_APPEND(tail, info);
  1116. } else if (name) {
  1117. error_setg(errp, "net client(%s) doesn't support"
  1118. " rx-filter querying", name);
  1119. assert(!filter_list);
  1120. return NULL;
  1121. }
  1122. if (name) {
  1123. break;
  1124. }
  1125. }
  1126. if (filter_list == NULL && name) {
  1127. error_setg(errp, "invalid net client name: %s", name);
  1128. }
  1129. return filter_list;
  1130. }
  1131. void hmp_info_network(Monitor *mon, const QDict *qdict)
  1132. {
  1133. NetClientState *nc, *peer;
  1134. NetClientDriver type;
  1135. net_hub_info(mon);
  1136. QTAILQ_FOREACH(nc, &net_clients, next) {
  1137. peer = nc->peer;
  1138. type = nc->info->type;
  1139. /* Skip if already printed in hub info */
  1140. if (net_hub_id_for_client(nc, NULL) == 0) {
  1141. continue;
  1142. }
  1143. if (!peer || type == NET_CLIENT_DRIVER_NIC) {
  1144. print_net_client(mon, nc);
  1145. } /* else it's a netdev connected to a NIC, printed with the NIC */
  1146. if (peer && type == NET_CLIENT_DRIVER_NIC) {
  1147. monitor_printf(mon, " \\ ");
  1148. print_net_client(mon, peer);
  1149. }
  1150. }
  1151. }
  1152. void colo_notify_filters_event(int event, Error **errp)
  1153. {
  1154. NetClientState *nc;
  1155. NetFilterState *nf;
  1156. NetFilterClass *nfc = NULL;
  1157. Error *local_err = NULL;
  1158. QTAILQ_FOREACH(nc, &net_clients, next) {
  1159. QTAILQ_FOREACH(nf, &nc->filters, next) {
  1160. nfc = NETFILTER_GET_CLASS(OBJECT(nf));
  1161. nfc->handle_event(nf, event, &local_err);
  1162. if (local_err) {
  1163. error_propagate(errp, local_err);
  1164. return;
  1165. }
  1166. }
  1167. }
  1168. }
  1169. void qmp_set_link(const char *name, bool up, Error **errp)
  1170. {
  1171. NetClientState *ncs[MAX_QUEUE_NUM];
  1172. NetClientState *nc;
  1173. int queues, i;
  1174. queues = qemu_find_net_clients_except(name, ncs,
  1175. NET_CLIENT_DRIVER__MAX,
  1176. MAX_QUEUE_NUM);
  1177. if (queues == 0) {
  1178. error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
  1179. "Device '%s' not found", name);
  1180. return;
  1181. }
  1182. nc = ncs[0];
  1183. for (i = 0; i < queues; i++) {
  1184. ncs[i]->link_down = !up;
  1185. }
  1186. if (nc->info->link_status_changed) {
  1187. nc->info->link_status_changed(nc);
  1188. }
  1189. if (nc->peer) {
  1190. /* Change peer link only if the peer is NIC and then notify peer.
  1191. * If the peer is a HUBPORT or a backend, we do not change the
  1192. * link status.
  1193. *
  1194. * This behavior is compatible with qemu hubs where there could be
  1195. * multiple clients that can still communicate with each other in
  1196. * disconnected mode. For now maintain this compatibility.
  1197. */
  1198. if (nc->peer->info->type == NET_CLIENT_DRIVER_NIC) {
  1199. for (i = 0; i < queues; i++) {
  1200. ncs[i]->peer->link_down = !up;
  1201. }
  1202. }
  1203. if (nc->peer->info->link_status_changed) {
  1204. nc->peer->info->link_status_changed(nc->peer);
  1205. }
  1206. }
  1207. }
  1208. static void net_vm_change_state_handler(void *opaque, bool running,
  1209. RunState state)
  1210. {
  1211. NetClientState *nc;
  1212. NetClientState *tmp;
  1213. QTAILQ_FOREACH_SAFE(nc, &net_clients, next, tmp) {
  1214. if (running) {
  1215. /* Flush queued packets and wake up backends. */
  1216. if (nc->peer && qemu_can_send_packet(nc)) {
  1217. qemu_flush_queued_packets(nc->peer);
  1218. }
  1219. } else {
  1220. /* Complete all queued packets, to guarantee we don't modify
  1221. * state later when VM is not running.
  1222. */
  1223. qemu_flush_or_purge_queued_packets(nc, true);
  1224. }
  1225. }
  1226. }
  1227. void net_cleanup(void)
  1228. {
  1229. NetClientState *nc;
  1230. /*cleanup colo compare module for COLO*/
  1231. colo_compare_cleanup();
  1232. /* We may del multiple entries during qemu_del_net_client(),
  1233. * so QTAILQ_FOREACH_SAFE() is also not safe here.
  1234. */
  1235. while (!QTAILQ_EMPTY(&net_clients)) {
  1236. nc = QTAILQ_FIRST(&net_clients);
  1237. if (nc->info->type == NET_CLIENT_DRIVER_NIC) {
  1238. qemu_del_nic(qemu_get_nic(nc));
  1239. } else {
  1240. qemu_del_net_client(nc);
  1241. }
  1242. }
  1243. qemu_del_vm_change_state_handler(net_change_state_entry);
  1244. }
  1245. void net_check_clients(void)
  1246. {
  1247. NetClientState *nc;
  1248. int i;
  1249. net_hub_check_clients();
  1250. QTAILQ_FOREACH(nc, &net_clients, next) {
  1251. if (!nc->peer) {
  1252. warn_report("%s %s has no peer",
  1253. nc->info->type == NET_CLIENT_DRIVER_NIC
  1254. ? "nic" : "netdev",
  1255. nc->name);
  1256. }
  1257. }
  1258. /* Check that all NICs requested via -net nic actually got created.
  1259. * NICs created via -device don't need to be checked here because
  1260. * they are always instantiated.
  1261. */
  1262. for (i = 0; i < MAX_NICS; i++) {
  1263. NICInfo *nd = &nd_table[i];
  1264. if (nd->used && !nd->instantiated) {
  1265. warn_report("requested NIC (%s, model %s) "
  1266. "was not created (not supported by this machine?)",
  1267. nd->name ? nd->name : "anonymous",
  1268. nd->model ? nd->model : "unspecified");
  1269. }
  1270. }
  1271. }
  1272. static int net_init_client(void *dummy, QemuOpts *opts, Error **errp)
  1273. {
  1274. return net_client_init(opts, false, errp);
  1275. }
  1276. static int net_init_netdev(void *dummy, QemuOpts *opts, Error **errp)
  1277. {
  1278. const char *type = qemu_opt_get(opts, "type");
  1279. if (type && is_help_option(type)) {
  1280. show_netdevs();
  1281. exit(0);
  1282. }
  1283. return net_client_init(opts, true, errp);
  1284. }
  1285. /* For the convenience "--nic" parameter */
  1286. static int net_param_nic(void *dummy, QemuOpts *opts, Error **errp)
  1287. {
  1288. char *mac, *nd_id;
  1289. int idx, ret;
  1290. NICInfo *ni;
  1291. const char *type;
  1292. type = qemu_opt_get(opts, "type");
  1293. if (type && g_str_equal(type, "none")) {
  1294. return 0; /* Nothing to do, default_net is cleared in vl.c */
  1295. }
  1296. idx = nic_get_free_idx();
  1297. if (idx == -1 || nb_nics >= MAX_NICS) {
  1298. error_setg(errp, "no more on-board/default NIC slots available");
  1299. return -1;
  1300. }
  1301. if (!type) {
  1302. qemu_opt_set(opts, "type", "user", &error_abort);
  1303. }
  1304. ni = &nd_table[idx];
  1305. memset(ni, 0, sizeof(*ni));
  1306. ni->model = qemu_opt_get_del(opts, "model");
  1307. /* Create an ID if the user did not specify one */
  1308. nd_id = g_strdup(qemu_opts_id(opts));
  1309. if (!nd_id) {
  1310. nd_id = id_generate(ID_NET);
  1311. qemu_opts_set_id(opts, nd_id);
  1312. }
  1313. /* Handle MAC address */
  1314. mac = qemu_opt_get_del(opts, "mac");
  1315. if (mac) {
  1316. ret = net_parse_macaddr(ni->macaddr.a, mac);
  1317. g_free(mac);
  1318. if (ret) {
  1319. error_setg(errp, "invalid syntax for ethernet address");
  1320. goto out;
  1321. }
  1322. if (is_multicast_ether_addr(ni->macaddr.a)) {
  1323. error_setg(errp, "NIC cannot have multicast MAC address");
  1324. ret = -1;
  1325. goto out;
  1326. }
  1327. }
  1328. qemu_macaddr_default_if_unset(&ni->macaddr);
  1329. ret = net_client_init(opts, true, errp);
  1330. if (ret == 0) {
  1331. ni->netdev = qemu_find_netdev(nd_id);
  1332. ni->used = true;
  1333. nb_nics++;
  1334. }
  1335. out:
  1336. g_free(nd_id);
  1337. return ret;
  1338. }
  1339. static void netdev_init_modern(void)
  1340. {
  1341. while (!QSIMPLEQ_EMPTY(&nd_queue)) {
  1342. NetdevQueueEntry *nd = QSIMPLEQ_FIRST(&nd_queue);
  1343. QSIMPLEQ_REMOVE_HEAD(&nd_queue, entry);
  1344. loc_push_restore(&nd->loc);
  1345. net_client_init1(nd->nd, true, &error_fatal);
  1346. loc_pop(&nd->loc);
  1347. qapi_free_Netdev(nd->nd);
  1348. g_free(nd);
  1349. }
  1350. }
  1351. void net_init_clients(void)
  1352. {
  1353. net_change_state_entry =
  1354. qemu_add_vm_change_state_handler(net_vm_change_state_handler, NULL);
  1355. QTAILQ_INIT(&net_clients);
  1356. netdev_init_modern();
  1357. qemu_opts_foreach(qemu_find_opts("netdev"), net_init_netdev, NULL,
  1358. &error_fatal);
  1359. qemu_opts_foreach(qemu_find_opts("nic"), net_param_nic, NULL,
  1360. &error_fatal);
  1361. qemu_opts_foreach(qemu_find_opts("net"), net_init_client, NULL,
  1362. &error_fatal);
  1363. }
  1364. /*
  1365. * Does this -netdev argument use modern rather than traditional syntax?
  1366. * Modern syntax is to be parsed with netdev_parse_modern().
  1367. * Traditional syntax is to be parsed with net_client_parse().
  1368. */
  1369. bool netdev_is_modern(const char *optarg)
  1370. {
  1371. QemuOpts *opts;
  1372. bool is_modern;
  1373. const char *type;
  1374. static QemuOptsList dummy_opts = {
  1375. .name = "netdev",
  1376. .implied_opt_name = "type",
  1377. .head = QTAILQ_HEAD_INITIALIZER(dummy_opts.head),
  1378. .desc = { { } },
  1379. };
  1380. if (optarg[0] == '{') {
  1381. /* This is JSON, which means it's modern syntax */
  1382. return true;
  1383. }
  1384. opts = qemu_opts_create(&dummy_opts, NULL, false, &error_abort);
  1385. qemu_opts_do_parse(opts, optarg, dummy_opts.implied_opt_name,
  1386. &error_abort);
  1387. type = qemu_opt_get(opts, "type");
  1388. is_modern = !g_strcmp0(type, "stream") || !g_strcmp0(type, "dgram");
  1389. qemu_opts_reset(&dummy_opts);
  1390. return is_modern;
  1391. }
  1392. /*
  1393. * netdev_parse_modern() uses modern, more expressive syntax than
  1394. * net_client_parse(), but supports only the -netdev option.
  1395. * netdev_parse_modern() appends to @nd_queue, whereas net_client_parse()
  1396. * appends to @qemu_netdev_opts.
  1397. */
  1398. void netdev_parse_modern(const char *optarg)
  1399. {
  1400. Visitor *v;
  1401. NetdevQueueEntry *nd;
  1402. v = qobject_input_visitor_new_str(optarg, "type", &error_fatal);
  1403. nd = g_new(NetdevQueueEntry, 1);
  1404. visit_type_Netdev(v, NULL, &nd->nd, &error_fatal);
  1405. visit_free(v);
  1406. loc_save(&nd->loc);
  1407. QSIMPLEQ_INSERT_TAIL(&nd_queue, nd, entry);
  1408. }
  1409. void net_client_parse(QemuOptsList *opts_list, const char *optarg)
  1410. {
  1411. if (!qemu_opts_parse_noisily(opts_list, optarg, true)) {
  1412. exit(1);
  1413. }
  1414. }
  1415. /* From FreeBSD */
  1416. /* XXX: optimize */
  1417. uint32_t net_crc32(const uint8_t *p, int len)
  1418. {
  1419. uint32_t crc;
  1420. int carry, i, j;
  1421. uint8_t b;
  1422. crc = 0xffffffff;
  1423. for (i = 0; i < len; i++) {
  1424. b = *p++;
  1425. for (j = 0; j < 8; j++) {
  1426. carry = ((crc & 0x80000000L) ? 1 : 0) ^ (b & 0x01);
  1427. crc <<= 1;
  1428. b >>= 1;
  1429. if (carry) {
  1430. crc = ((crc ^ POLYNOMIAL_BE) | carry);
  1431. }
  1432. }
  1433. }
  1434. return crc;
  1435. }
  1436. uint32_t net_crc32_le(const uint8_t *p, int len)
  1437. {
  1438. uint32_t crc;
  1439. int carry, i, j;
  1440. uint8_t b;
  1441. crc = 0xffffffff;
  1442. for (i = 0; i < len; i++) {
  1443. b = *p++;
  1444. for (j = 0; j < 8; j++) {
  1445. carry = (crc & 0x1) ^ (b & 0x01);
  1446. crc >>= 1;
  1447. b >>= 1;
  1448. if (carry) {
  1449. crc ^= POLYNOMIAL_LE;
  1450. }
  1451. }
  1452. }
  1453. return crc;
  1454. }
  1455. QemuOptsList qemu_netdev_opts = {
  1456. .name = "netdev",
  1457. .implied_opt_name = "type",
  1458. .head = QTAILQ_HEAD_INITIALIZER(qemu_netdev_opts.head),
  1459. .desc = {
  1460. /*
  1461. * no elements => accept any params
  1462. * validation will happen later
  1463. */
  1464. { /* end of list */ }
  1465. },
  1466. };
  1467. QemuOptsList qemu_nic_opts = {
  1468. .name = "nic",
  1469. .implied_opt_name = "type",
  1470. .head = QTAILQ_HEAD_INITIALIZER(qemu_nic_opts.head),
  1471. .desc = {
  1472. /*
  1473. * no elements => accept any params
  1474. * validation will happen later
  1475. */
  1476. { /* end of list */ }
  1477. },
  1478. };
  1479. QemuOptsList qemu_net_opts = {
  1480. .name = "net",
  1481. .implied_opt_name = "type",
  1482. .head = QTAILQ_HEAD_INITIALIZER(qemu_net_opts.head),
  1483. .desc = {
  1484. /*
  1485. * no elements => accept any params
  1486. * validation will happen later
  1487. */
  1488. { /* end of list */ }
  1489. },
  1490. };
  1491. void net_socket_rs_init(SocketReadState *rs,
  1492. SocketReadStateFinalize *finalize,
  1493. bool vnet_hdr)
  1494. {
  1495. rs->state = 0;
  1496. rs->vnet_hdr = vnet_hdr;
  1497. rs->index = 0;
  1498. rs->packet_len = 0;
  1499. rs->vnet_hdr_len = 0;
  1500. memset(rs->buf, 0, sizeof(rs->buf));
  1501. rs->finalize = finalize;
  1502. }
  1503. /*
  1504. * Returns
  1505. * 0: success
  1506. * -1: error occurs
  1507. */
  1508. int net_fill_rstate(SocketReadState *rs, const uint8_t *buf, int size)
  1509. {
  1510. unsigned int l;
  1511. while (size > 0) {
  1512. /* Reassemble a packet from the network.
  1513. * 0 = getting length.
  1514. * 1 = getting vnet header length.
  1515. * 2 = getting data.
  1516. */
  1517. switch (rs->state) {
  1518. case 0:
  1519. l = 4 - rs->index;
  1520. if (l > size) {
  1521. l = size;
  1522. }
  1523. memcpy(rs->buf + rs->index, buf, l);
  1524. buf += l;
  1525. size -= l;
  1526. rs->index += l;
  1527. if (rs->index == 4) {
  1528. /* got length */
  1529. rs->packet_len = ntohl(*(uint32_t *)rs->buf);
  1530. rs->index = 0;
  1531. if (rs->vnet_hdr) {
  1532. rs->state = 1;
  1533. } else {
  1534. rs->state = 2;
  1535. rs->vnet_hdr_len = 0;
  1536. }
  1537. }
  1538. break;
  1539. case 1:
  1540. l = 4 - rs->index;
  1541. if (l > size) {
  1542. l = size;
  1543. }
  1544. memcpy(rs->buf + rs->index, buf, l);
  1545. buf += l;
  1546. size -= l;
  1547. rs->index += l;
  1548. if (rs->index == 4) {
  1549. /* got vnet header length */
  1550. rs->vnet_hdr_len = ntohl(*(uint32_t *)rs->buf);
  1551. rs->index = 0;
  1552. rs->state = 2;
  1553. }
  1554. break;
  1555. case 2:
  1556. l = rs->packet_len - rs->index;
  1557. if (l > size) {
  1558. l = size;
  1559. }
  1560. if (rs->index + l <= sizeof(rs->buf)) {
  1561. memcpy(rs->buf + rs->index, buf, l);
  1562. } else {
  1563. fprintf(stderr, "serious error: oversized packet received,"
  1564. "connection terminated.\n");
  1565. rs->index = rs->state = 0;
  1566. return -1;
  1567. }
  1568. rs->index += l;
  1569. buf += l;
  1570. size -= l;
  1571. if (rs->index >= rs->packet_len) {
  1572. rs->index = 0;
  1573. rs->state = 0;
  1574. assert(rs->finalize);
  1575. rs->finalize(rs);
  1576. }
  1577. break;
  1578. }
  1579. }
  1580. assert(size == 0);
  1581. return 0;
  1582. }