virtio-net.c 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203
  1. /*
  2. * Virtio Network Device
  3. *
  4. * Copyright IBM, Corp. 2007
  5. *
  6. * Authors:
  7. * Anthony Liguori <aliguori@us.ibm.com>
  8. *
  9. * This work is licensed under the terms of the GNU GPL, version 2. See
  10. * the COPYING file in the top-level directory.
  11. *
  12. */
  13. #include "qemu/osdep.h"
  14. #include "qemu/iov.h"
  15. #include "hw/virtio/virtio.h"
  16. #include "net/net.h"
  17. #include "net/checksum.h"
  18. #include "net/tap.h"
  19. #include "qemu/error-report.h"
  20. #include "qemu/timer.h"
  21. #include "hw/virtio/virtio-net.h"
  22. #include "net/vhost_net.h"
  23. #include "hw/virtio/virtio-bus.h"
  24. #include "qapi/error.h"
  25. #include "qapi/qmp/qjson.h"
  26. #include "qapi-event.h"
  27. #include "hw/virtio/virtio-access.h"
  28. #include "migration/misc.h"
  29. #define VIRTIO_NET_VM_VERSION 11
  30. #define MAC_TABLE_ENTRIES 64
  31. #define MAX_VLAN (1 << 12) /* Per 802.1Q definition */
  32. /* previously fixed value */
  33. #define VIRTIO_NET_RX_QUEUE_DEFAULT_SIZE 256
  34. #define VIRTIO_NET_TX_QUEUE_DEFAULT_SIZE 256
  35. /* for now, only allow larger queues; with virtio-1, guest can downsize */
  36. #define VIRTIO_NET_RX_QUEUE_MIN_SIZE VIRTIO_NET_RX_QUEUE_DEFAULT_SIZE
  37. #define VIRTIO_NET_TX_QUEUE_MIN_SIZE VIRTIO_NET_TX_QUEUE_DEFAULT_SIZE
  38. /*
  39. * Calculate the number of bytes up to and including the given 'field' of
  40. * 'container'.
  41. */
  42. #define endof(container, field) \
  43. (offsetof(container, field) + sizeof(((container *)0)->field))
  44. typedef struct VirtIOFeature {
  45. uint32_t flags;
  46. size_t end;
  47. } VirtIOFeature;
  48. static VirtIOFeature feature_sizes[] = {
  49. {.flags = 1 << VIRTIO_NET_F_MAC,
  50. .end = endof(struct virtio_net_config, mac)},
  51. {.flags = 1 << VIRTIO_NET_F_STATUS,
  52. .end = endof(struct virtio_net_config, status)},
  53. {.flags = 1 << VIRTIO_NET_F_MQ,
  54. .end = endof(struct virtio_net_config, max_virtqueue_pairs)},
  55. {.flags = 1 << VIRTIO_NET_F_MTU,
  56. .end = endof(struct virtio_net_config, mtu)},
  57. {}
  58. };
  59. static VirtIONetQueue *virtio_net_get_subqueue(NetClientState *nc)
  60. {
  61. VirtIONet *n = qemu_get_nic_opaque(nc);
  62. return &n->vqs[nc->queue_index];
  63. }
  64. static int vq2q(int queue_index)
  65. {
  66. return queue_index / 2;
  67. }
  68. /* TODO
  69. * - we could suppress RX interrupt if we were so inclined.
  70. */
  71. static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config)
  72. {
  73. VirtIONet *n = VIRTIO_NET(vdev);
  74. struct virtio_net_config netcfg;
  75. virtio_stw_p(vdev, &netcfg.status, n->status);
  76. virtio_stw_p(vdev, &netcfg.max_virtqueue_pairs, n->max_queues);
  77. virtio_stw_p(vdev, &netcfg.mtu, n->net_conf.mtu);
  78. memcpy(netcfg.mac, n->mac, ETH_ALEN);
  79. memcpy(config, &netcfg, n->config_size);
  80. }
  81. static void virtio_net_set_config(VirtIODevice *vdev, const uint8_t *config)
  82. {
  83. VirtIONet *n = VIRTIO_NET(vdev);
  84. struct virtio_net_config netcfg = {};
  85. memcpy(&netcfg, config, n->config_size);
  86. if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR) &&
  87. !virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1) &&
  88. memcmp(netcfg.mac, n->mac, ETH_ALEN)) {
  89. memcpy(n->mac, netcfg.mac, ETH_ALEN);
  90. qemu_format_nic_info_str(qemu_get_queue(n->nic), n->mac);
  91. }
  92. }
  93. static bool virtio_net_started(VirtIONet *n, uint8_t status)
  94. {
  95. VirtIODevice *vdev = VIRTIO_DEVICE(n);
  96. return (status & VIRTIO_CONFIG_S_DRIVER_OK) &&
  97. (n->status & VIRTIO_NET_S_LINK_UP) && vdev->vm_running;
  98. }
  99. static void virtio_net_announce_timer(void *opaque)
  100. {
  101. VirtIONet *n = opaque;
  102. VirtIODevice *vdev = VIRTIO_DEVICE(n);
  103. n->announce_counter--;
  104. n->status |= VIRTIO_NET_S_ANNOUNCE;
  105. virtio_notify_config(vdev);
  106. }
  107. static void virtio_net_vhost_status(VirtIONet *n, uint8_t status)
  108. {
  109. VirtIODevice *vdev = VIRTIO_DEVICE(n);
  110. NetClientState *nc = qemu_get_queue(n->nic);
  111. int queues = n->multiqueue ? n->max_queues : 1;
  112. if (!get_vhost_net(nc->peer)) {
  113. return;
  114. }
  115. if ((virtio_net_started(n, status) && !nc->peer->link_down) ==
  116. !!n->vhost_started) {
  117. return;
  118. }
  119. if (!n->vhost_started) {
  120. int r, i;
  121. if (n->needs_vnet_hdr_swap) {
  122. error_report("backend does not support %s vnet headers; "
  123. "falling back on userspace virtio",
  124. virtio_is_big_endian(vdev) ? "BE" : "LE");
  125. return;
  126. }
  127. /* Any packets outstanding? Purge them to avoid touching rings
  128. * when vhost is running.
  129. */
  130. for (i = 0; i < queues; i++) {
  131. NetClientState *qnc = qemu_get_subqueue(n->nic, i);
  132. /* Purge both directions: TX and RX. */
  133. qemu_net_queue_purge(qnc->peer->incoming_queue, qnc);
  134. qemu_net_queue_purge(qnc->incoming_queue, qnc->peer);
  135. }
  136. if (virtio_has_feature(vdev->guest_features, VIRTIO_NET_F_MTU)) {
  137. r = vhost_net_set_mtu(get_vhost_net(nc->peer), n->net_conf.mtu);
  138. if (r < 0) {
  139. error_report("%uBytes MTU not supported by the backend",
  140. n->net_conf.mtu);
  141. return;
  142. }
  143. }
  144. n->vhost_started = 1;
  145. r = vhost_net_start(vdev, n->nic->ncs, queues);
  146. if (r < 0) {
  147. error_report("unable to start vhost net: %d: "
  148. "falling back on userspace virtio", -r);
  149. n->vhost_started = 0;
  150. }
  151. } else {
  152. vhost_net_stop(vdev, n->nic->ncs, queues);
  153. n->vhost_started = 0;
  154. }
  155. }
  156. static int virtio_net_set_vnet_endian_one(VirtIODevice *vdev,
  157. NetClientState *peer,
  158. bool enable)
  159. {
  160. if (virtio_is_big_endian(vdev)) {
  161. return qemu_set_vnet_be(peer, enable);
  162. } else {
  163. return qemu_set_vnet_le(peer, enable);
  164. }
  165. }
  166. static bool virtio_net_set_vnet_endian(VirtIODevice *vdev, NetClientState *ncs,
  167. int queues, bool enable)
  168. {
  169. int i;
  170. for (i = 0; i < queues; i++) {
  171. if (virtio_net_set_vnet_endian_one(vdev, ncs[i].peer, enable) < 0 &&
  172. enable) {
  173. while (--i >= 0) {
  174. virtio_net_set_vnet_endian_one(vdev, ncs[i].peer, false);
  175. }
  176. return true;
  177. }
  178. }
  179. return false;
  180. }
  181. static void virtio_net_vnet_endian_status(VirtIONet *n, uint8_t status)
  182. {
  183. VirtIODevice *vdev = VIRTIO_DEVICE(n);
  184. int queues = n->multiqueue ? n->max_queues : 1;
  185. if (virtio_net_started(n, status)) {
  186. /* Before using the device, we tell the network backend about the
  187. * endianness to use when parsing vnet headers. If the backend
  188. * can't do it, we fallback onto fixing the headers in the core
  189. * virtio-net code.
  190. */
  191. n->needs_vnet_hdr_swap = virtio_net_set_vnet_endian(vdev, n->nic->ncs,
  192. queues, true);
  193. } else if (virtio_net_started(n, vdev->status)) {
  194. /* After using the device, we need to reset the network backend to
  195. * the default (guest native endianness), otherwise the guest may
  196. * lose network connectivity if it is rebooted into a different
  197. * endianness.
  198. */
  199. virtio_net_set_vnet_endian(vdev, n->nic->ncs, queues, false);
  200. }
  201. }
  202. static void virtio_net_drop_tx_queue_data(VirtIODevice *vdev, VirtQueue *vq)
  203. {
  204. unsigned int dropped = virtqueue_drop_all(vq);
  205. if (dropped) {
  206. virtio_notify(vdev, vq);
  207. }
  208. }
  209. static void virtio_net_set_status(struct VirtIODevice *vdev, uint8_t status)
  210. {
  211. VirtIONet *n = VIRTIO_NET(vdev);
  212. VirtIONetQueue *q;
  213. int i;
  214. uint8_t queue_status;
  215. virtio_net_vnet_endian_status(n, status);
  216. virtio_net_vhost_status(n, status);
  217. for (i = 0; i < n->max_queues; i++) {
  218. NetClientState *ncs = qemu_get_subqueue(n->nic, i);
  219. bool queue_started;
  220. q = &n->vqs[i];
  221. if ((!n->multiqueue && i != 0) || i >= n->curr_queues) {
  222. queue_status = 0;
  223. } else {
  224. queue_status = status;
  225. }
  226. queue_started =
  227. virtio_net_started(n, queue_status) && !n->vhost_started;
  228. if (queue_started) {
  229. qemu_flush_queued_packets(ncs);
  230. }
  231. if (!q->tx_waiting) {
  232. continue;
  233. }
  234. if (queue_started) {
  235. if (q->tx_timer) {
  236. timer_mod(q->tx_timer,
  237. qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + n->tx_timeout);
  238. } else {
  239. qemu_bh_schedule(q->tx_bh);
  240. }
  241. } else {
  242. if (q->tx_timer) {
  243. timer_del(q->tx_timer);
  244. } else {
  245. qemu_bh_cancel(q->tx_bh);
  246. }
  247. if ((n->status & VIRTIO_NET_S_LINK_UP) == 0 &&
  248. (queue_status & VIRTIO_CONFIG_S_DRIVER_OK) &&
  249. vdev->vm_running) {
  250. /* if tx is waiting we are likely have some packets in tx queue
  251. * and disabled notification */
  252. q->tx_waiting = 0;
  253. virtio_queue_set_notification(q->tx_vq, 1);
  254. virtio_net_drop_tx_queue_data(vdev, q->tx_vq);
  255. }
  256. }
  257. }
  258. }
  259. static void virtio_net_set_link_status(NetClientState *nc)
  260. {
  261. VirtIONet *n = qemu_get_nic_opaque(nc);
  262. VirtIODevice *vdev = VIRTIO_DEVICE(n);
  263. uint16_t old_status = n->status;
  264. if (nc->link_down)
  265. n->status &= ~VIRTIO_NET_S_LINK_UP;
  266. else
  267. n->status |= VIRTIO_NET_S_LINK_UP;
  268. if (n->status != old_status)
  269. virtio_notify_config(vdev);
  270. virtio_net_set_status(vdev, vdev->status);
  271. }
  272. static void rxfilter_notify(NetClientState *nc)
  273. {
  274. VirtIONet *n = qemu_get_nic_opaque(nc);
  275. if (nc->rxfilter_notify_enabled) {
  276. gchar *path = object_get_canonical_path(OBJECT(n->qdev));
  277. qapi_event_send_nic_rx_filter_changed(!!n->netclient_name,
  278. n->netclient_name, path, &error_abort);
  279. g_free(path);
  280. /* disable event notification to avoid events flooding */
  281. nc->rxfilter_notify_enabled = 0;
  282. }
  283. }
  284. static intList *get_vlan_table(VirtIONet *n)
  285. {
  286. intList *list, *entry;
  287. int i, j;
  288. list = NULL;
  289. for (i = 0; i < MAX_VLAN >> 5; i++) {
  290. for (j = 0; n->vlans[i] && j <= 0x1f; j++) {
  291. if (n->vlans[i] & (1U << j)) {
  292. entry = g_malloc0(sizeof(*entry));
  293. entry->value = (i << 5) + j;
  294. entry->next = list;
  295. list = entry;
  296. }
  297. }
  298. }
  299. return list;
  300. }
  301. static RxFilterInfo *virtio_net_query_rxfilter(NetClientState *nc)
  302. {
  303. VirtIONet *n = qemu_get_nic_opaque(nc);
  304. VirtIODevice *vdev = VIRTIO_DEVICE(n);
  305. RxFilterInfo *info;
  306. strList *str_list, *entry;
  307. int i;
  308. info = g_malloc0(sizeof(*info));
  309. info->name = g_strdup(nc->name);
  310. info->promiscuous = n->promisc;
  311. if (n->nouni) {
  312. info->unicast = RX_STATE_NONE;
  313. } else if (n->alluni) {
  314. info->unicast = RX_STATE_ALL;
  315. } else {
  316. info->unicast = RX_STATE_NORMAL;
  317. }
  318. if (n->nomulti) {
  319. info->multicast = RX_STATE_NONE;
  320. } else if (n->allmulti) {
  321. info->multicast = RX_STATE_ALL;
  322. } else {
  323. info->multicast = RX_STATE_NORMAL;
  324. }
  325. info->broadcast_allowed = n->nobcast;
  326. info->multicast_overflow = n->mac_table.multi_overflow;
  327. info->unicast_overflow = n->mac_table.uni_overflow;
  328. info->main_mac = qemu_mac_strdup_printf(n->mac);
  329. str_list = NULL;
  330. for (i = 0; i < n->mac_table.first_multi; i++) {
  331. entry = g_malloc0(sizeof(*entry));
  332. entry->value = qemu_mac_strdup_printf(n->mac_table.macs + i * ETH_ALEN);
  333. entry->next = str_list;
  334. str_list = entry;
  335. }
  336. info->unicast_table = str_list;
  337. str_list = NULL;
  338. for (i = n->mac_table.first_multi; i < n->mac_table.in_use; i++) {
  339. entry = g_malloc0(sizeof(*entry));
  340. entry->value = qemu_mac_strdup_printf(n->mac_table.macs + i * ETH_ALEN);
  341. entry->next = str_list;
  342. str_list = entry;
  343. }
  344. info->multicast_table = str_list;
  345. info->vlan_table = get_vlan_table(n);
  346. if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_VLAN)) {
  347. info->vlan = RX_STATE_ALL;
  348. } else if (!info->vlan_table) {
  349. info->vlan = RX_STATE_NONE;
  350. } else {
  351. info->vlan = RX_STATE_NORMAL;
  352. }
  353. /* enable event notification after query */
  354. nc->rxfilter_notify_enabled = 1;
  355. return info;
  356. }
  357. static void virtio_net_reset(VirtIODevice *vdev)
  358. {
  359. VirtIONet *n = VIRTIO_NET(vdev);
  360. /* Reset back to compatibility mode */
  361. n->promisc = 1;
  362. n->allmulti = 0;
  363. n->alluni = 0;
  364. n->nomulti = 0;
  365. n->nouni = 0;
  366. n->nobcast = 0;
  367. /* multiqueue is disabled by default */
  368. n->curr_queues = 1;
  369. timer_del(n->announce_timer);
  370. n->announce_counter = 0;
  371. n->status &= ~VIRTIO_NET_S_ANNOUNCE;
  372. /* Flush any MAC and VLAN filter table state */
  373. n->mac_table.in_use = 0;
  374. n->mac_table.first_multi = 0;
  375. n->mac_table.multi_overflow = 0;
  376. n->mac_table.uni_overflow = 0;
  377. memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
  378. memcpy(&n->mac[0], &n->nic->conf->macaddr, sizeof(n->mac));
  379. qemu_format_nic_info_str(qemu_get_queue(n->nic), n->mac);
  380. memset(n->vlans, 0, MAX_VLAN >> 3);
  381. }
  382. static void peer_test_vnet_hdr(VirtIONet *n)
  383. {
  384. NetClientState *nc = qemu_get_queue(n->nic);
  385. if (!nc->peer) {
  386. return;
  387. }
  388. n->has_vnet_hdr = qemu_has_vnet_hdr(nc->peer);
  389. }
  390. static int peer_has_vnet_hdr(VirtIONet *n)
  391. {
  392. return n->has_vnet_hdr;
  393. }
  394. static int peer_has_ufo(VirtIONet *n)
  395. {
  396. if (!peer_has_vnet_hdr(n))
  397. return 0;
  398. n->has_ufo = qemu_has_ufo(qemu_get_queue(n->nic)->peer);
  399. return n->has_ufo;
  400. }
  401. static void virtio_net_set_mrg_rx_bufs(VirtIONet *n, int mergeable_rx_bufs,
  402. int version_1)
  403. {
  404. int i;
  405. NetClientState *nc;
  406. n->mergeable_rx_bufs = mergeable_rx_bufs;
  407. if (version_1) {
  408. n->guest_hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
  409. } else {
  410. n->guest_hdr_len = n->mergeable_rx_bufs ?
  411. sizeof(struct virtio_net_hdr_mrg_rxbuf) :
  412. sizeof(struct virtio_net_hdr);
  413. }
  414. for (i = 0; i < n->max_queues; i++) {
  415. nc = qemu_get_subqueue(n->nic, i);
  416. if (peer_has_vnet_hdr(n) &&
  417. qemu_has_vnet_hdr_len(nc->peer, n->guest_hdr_len)) {
  418. qemu_set_vnet_hdr_len(nc->peer, n->guest_hdr_len);
  419. n->host_hdr_len = n->guest_hdr_len;
  420. }
  421. }
  422. }
  423. static int virtio_net_max_tx_queue_size(VirtIONet *n)
  424. {
  425. NetClientState *peer = n->nic_conf.peers.ncs[0];
  426. /*
  427. * Backends other than vhost-user don't support max queue size.
  428. */
  429. if (!peer) {
  430. return VIRTIO_NET_TX_QUEUE_DEFAULT_SIZE;
  431. }
  432. if (peer->info->type != NET_CLIENT_DRIVER_VHOST_USER) {
  433. return VIRTIO_NET_TX_QUEUE_DEFAULT_SIZE;
  434. }
  435. return VIRTQUEUE_MAX_SIZE;
  436. }
  437. static int peer_attach(VirtIONet *n, int index)
  438. {
  439. NetClientState *nc = qemu_get_subqueue(n->nic, index);
  440. if (!nc->peer) {
  441. return 0;
  442. }
  443. if (nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_USER) {
  444. vhost_set_vring_enable(nc->peer, 1);
  445. }
  446. if (nc->peer->info->type != NET_CLIENT_DRIVER_TAP) {
  447. return 0;
  448. }
  449. if (n->max_queues == 1) {
  450. return 0;
  451. }
  452. return tap_enable(nc->peer);
  453. }
  454. static int peer_detach(VirtIONet *n, int index)
  455. {
  456. NetClientState *nc = qemu_get_subqueue(n->nic, index);
  457. if (!nc->peer) {
  458. return 0;
  459. }
  460. if (nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_USER) {
  461. vhost_set_vring_enable(nc->peer, 0);
  462. }
  463. if (nc->peer->info->type != NET_CLIENT_DRIVER_TAP) {
  464. return 0;
  465. }
  466. return tap_disable(nc->peer);
  467. }
  468. static void virtio_net_set_queues(VirtIONet *n)
  469. {
  470. int i;
  471. int r;
  472. if (n->nic->peer_deleted) {
  473. return;
  474. }
  475. for (i = 0; i < n->max_queues; i++) {
  476. if (i < n->curr_queues) {
  477. r = peer_attach(n, i);
  478. assert(!r);
  479. } else {
  480. r = peer_detach(n, i);
  481. assert(!r);
  482. }
  483. }
  484. }
  485. static void virtio_net_set_multiqueue(VirtIONet *n, int multiqueue);
  486. static uint64_t virtio_net_get_features(VirtIODevice *vdev, uint64_t features,
  487. Error **errp)
  488. {
  489. VirtIONet *n = VIRTIO_NET(vdev);
  490. NetClientState *nc = qemu_get_queue(n->nic);
  491. /* Firstly sync all virtio-net possible supported features */
  492. features |= n->host_features;
  493. virtio_add_feature(&features, VIRTIO_NET_F_MAC);
  494. if (!peer_has_vnet_hdr(n)) {
  495. virtio_clear_feature(&features, VIRTIO_NET_F_CSUM);
  496. virtio_clear_feature(&features, VIRTIO_NET_F_HOST_TSO4);
  497. virtio_clear_feature(&features, VIRTIO_NET_F_HOST_TSO6);
  498. virtio_clear_feature(&features, VIRTIO_NET_F_HOST_ECN);
  499. virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_CSUM);
  500. virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_TSO4);
  501. virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_TSO6);
  502. virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_ECN);
  503. }
  504. if (!peer_has_vnet_hdr(n) || !peer_has_ufo(n)) {
  505. virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_UFO);
  506. virtio_clear_feature(&features, VIRTIO_NET_F_HOST_UFO);
  507. }
  508. if (!get_vhost_net(nc->peer)) {
  509. return features;
  510. }
  511. features = vhost_net_get_features(get_vhost_net(nc->peer), features);
  512. vdev->backend_features = features;
  513. if (n->mtu_bypass_backend &&
  514. (n->host_features & 1ULL << VIRTIO_NET_F_MTU)) {
  515. features |= (1ULL << VIRTIO_NET_F_MTU);
  516. }
  517. return features;
  518. }
  519. static uint64_t virtio_net_bad_features(VirtIODevice *vdev)
  520. {
  521. uint64_t features = 0;
  522. /* Linux kernel 2.6.25. It understood MAC (as everyone must),
  523. * but also these: */
  524. virtio_add_feature(&features, VIRTIO_NET_F_MAC);
  525. virtio_add_feature(&features, VIRTIO_NET_F_CSUM);
  526. virtio_add_feature(&features, VIRTIO_NET_F_HOST_TSO4);
  527. virtio_add_feature(&features, VIRTIO_NET_F_HOST_TSO6);
  528. virtio_add_feature(&features, VIRTIO_NET_F_HOST_ECN);
  529. return features;
  530. }
  531. static void virtio_net_apply_guest_offloads(VirtIONet *n)
  532. {
  533. qemu_set_offload(qemu_get_queue(n->nic)->peer,
  534. !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_CSUM)),
  535. !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_TSO4)),
  536. !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_TSO6)),
  537. !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_ECN)),
  538. !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_UFO)));
  539. }
  540. static uint64_t virtio_net_guest_offloads_by_features(uint32_t features)
  541. {
  542. static const uint64_t guest_offloads_mask =
  543. (1ULL << VIRTIO_NET_F_GUEST_CSUM) |
  544. (1ULL << VIRTIO_NET_F_GUEST_TSO4) |
  545. (1ULL << VIRTIO_NET_F_GUEST_TSO6) |
  546. (1ULL << VIRTIO_NET_F_GUEST_ECN) |
  547. (1ULL << VIRTIO_NET_F_GUEST_UFO);
  548. return guest_offloads_mask & features;
  549. }
  550. static inline uint64_t virtio_net_supported_guest_offloads(VirtIONet *n)
  551. {
  552. VirtIODevice *vdev = VIRTIO_DEVICE(n);
  553. return virtio_net_guest_offloads_by_features(vdev->guest_features);
  554. }
  555. static void virtio_net_set_features(VirtIODevice *vdev, uint64_t features)
  556. {
  557. VirtIONet *n = VIRTIO_NET(vdev);
  558. int i;
  559. if (n->mtu_bypass_backend &&
  560. !virtio_has_feature(vdev->backend_features, VIRTIO_NET_F_MTU)) {
  561. features &= ~(1ULL << VIRTIO_NET_F_MTU);
  562. }
  563. virtio_net_set_multiqueue(n,
  564. virtio_has_feature(features, VIRTIO_NET_F_MQ));
  565. virtio_net_set_mrg_rx_bufs(n,
  566. virtio_has_feature(features,
  567. VIRTIO_NET_F_MRG_RXBUF),
  568. virtio_has_feature(features,
  569. VIRTIO_F_VERSION_1));
  570. if (n->has_vnet_hdr) {
  571. n->curr_guest_offloads =
  572. virtio_net_guest_offloads_by_features(features);
  573. virtio_net_apply_guest_offloads(n);
  574. }
  575. for (i = 0; i < n->max_queues; i++) {
  576. NetClientState *nc = qemu_get_subqueue(n->nic, i);
  577. if (!get_vhost_net(nc->peer)) {
  578. continue;
  579. }
  580. vhost_net_ack_features(get_vhost_net(nc->peer), features);
  581. }
  582. if (virtio_has_feature(features, VIRTIO_NET_F_CTRL_VLAN)) {
  583. memset(n->vlans, 0, MAX_VLAN >> 3);
  584. } else {
  585. memset(n->vlans, 0xff, MAX_VLAN >> 3);
  586. }
  587. }
  588. static int virtio_net_handle_rx_mode(VirtIONet *n, uint8_t cmd,
  589. struct iovec *iov, unsigned int iov_cnt)
  590. {
  591. uint8_t on;
  592. size_t s;
  593. NetClientState *nc = qemu_get_queue(n->nic);
  594. s = iov_to_buf(iov, iov_cnt, 0, &on, sizeof(on));
  595. if (s != sizeof(on)) {
  596. return VIRTIO_NET_ERR;
  597. }
  598. if (cmd == VIRTIO_NET_CTRL_RX_PROMISC) {
  599. n->promisc = on;
  600. } else if (cmd == VIRTIO_NET_CTRL_RX_ALLMULTI) {
  601. n->allmulti = on;
  602. } else if (cmd == VIRTIO_NET_CTRL_RX_ALLUNI) {
  603. n->alluni = on;
  604. } else if (cmd == VIRTIO_NET_CTRL_RX_NOMULTI) {
  605. n->nomulti = on;
  606. } else if (cmd == VIRTIO_NET_CTRL_RX_NOUNI) {
  607. n->nouni = on;
  608. } else if (cmd == VIRTIO_NET_CTRL_RX_NOBCAST) {
  609. n->nobcast = on;
  610. } else {
  611. return VIRTIO_NET_ERR;
  612. }
  613. rxfilter_notify(nc);
  614. return VIRTIO_NET_OK;
  615. }
  616. static int virtio_net_handle_offloads(VirtIONet *n, uint8_t cmd,
  617. struct iovec *iov, unsigned int iov_cnt)
  618. {
  619. VirtIODevice *vdev = VIRTIO_DEVICE(n);
  620. uint64_t offloads;
  621. size_t s;
  622. if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)) {
  623. return VIRTIO_NET_ERR;
  624. }
  625. s = iov_to_buf(iov, iov_cnt, 0, &offloads, sizeof(offloads));
  626. if (s != sizeof(offloads)) {
  627. return VIRTIO_NET_ERR;
  628. }
  629. if (cmd == VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET) {
  630. uint64_t supported_offloads;
  631. offloads = virtio_ldq_p(vdev, &offloads);
  632. if (!n->has_vnet_hdr) {
  633. return VIRTIO_NET_ERR;
  634. }
  635. supported_offloads = virtio_net_supported_guest_offloads(n);
  636. if (offloads & ~supported_offloads) {
  637. return VIRTIO_NET_ERR;
  638. }
  639. n->curr_guest_offloads = offloads;
  640. virtio_net_apply_guest_offloads(n);
  641. return VIRTIO_NET_OK;
  642. } else {
  643. return VIRTIO_NET_ERR;
  644. }
  645. }
  646. static int virtio_net_handle_mac(VirtIONet *n, uint8_t cmd,
  647. struct iovec *iov, unsigned int iov_cnt)
  648. {
  649. VirtIODevice *vdev = VIRTIO_DEVICE(n);
  650. struct virtio_net_ctrl_mac mac_data;
  651. size_t s;
  652. NetClientState *nc = qemu_get_queue(n->nic);
  653. if (cmd == VIRTIO_NET_CTRL_MAC_ADDR_SET) {
  654. if (iov_size(iov, iov_cnt) != sizeof(n->mac)) {
  655. return VIRTIO_NET_ERR;
  656. }
  657. s = iov_to_buf(iov, iov_cnt, 0, &n->mac, sizeof(n->mac));
  658. assert(s == sizeof(n->mac));
  659. qemu_format_nic_info_str(qemu_get_queue(n->nic), n->mac);
  660. rxfilter_notify(nc);
  661. return VIRTIO_NET_OK;
  662. }
  663. if (cmd != VIRTIO_NET_CTRL_MAC_TABLE_SET) {
  664. return VIRTIO_NET_ERR;
  665. }
  666. int in_use = 0;
  667. int first_multi = 0;
  668. uint8_t uni_overflow = 0;
  669. uint8_t multi_overflow = 0;
  670. uint8_t *macs = g_malloc0(MAC_TABLE_ENTRIES * ETH_ALEN);
  671. s = iov_to_buf(iov, iov_cnt, 0, &mac_data.entries,
  672. sizeof(mac_data.entries));
  673. mac_data.entries = virtio_ldl_p(vdev, &mac_data.entries);
  674. if (s != sizeof(mac_data.entries)) {
  675. goto error;
  676. }
  677. iov_discard_front(&iov, &iov_cnt, s);
  678. if (mac_data.entries * ETH_ALEN > iov_size(iov, iov_cnt)) {
  679. goto error;
  680. }
  681. if (mac_data.entries <= MAC_TABLE_ENTRIES) {
  682. s = iov_to_buf(iov, iov_cnt, 0, macs,
  683. mac_data.entries * ETH_ALEN);
  684. if (s != mac_data.entries * ETH_ALEN) {
  685. goto error;
  686. }
  687. in_use += mac_data.entries;
  688. } else {
  689. uni_overflow = 1;
  690. }
  691. iov_discard_front(&iov, &iov_cnt, mac_data.entries * ETH_ALEN);
  692. first_multi = in_use;
  693. s = iov_to_buf(iov, iov_cnt, 0, &mac_data.entries,
  694. sizeof(mac_data.entries));
  695. mac_data.entries = virtio_ldl_p(vdev, &mac_data.entries);
  696. if (s != sizeof(mac_data.entries)) {
  697. goto error;
  698. }
  699. iov_discard_front(&iov, &iov_cnt, s);
  700. if (mac_data.entries * ETH_ALEN != iov_size(iov, iov_cnt)) {
  701. goto error;
  702. }
  703. if (mac_data.entries <= MAC_TABLE_ENTRIES - in_use) {
  704. s = iov_to_buf(iov, iov_cnt, 0, &macs[in_use * ETH_ALEN],
  705. mac_data.entries * ETH_ALEN);
  706. if (s != mac_data.entries * ETH_ALEN) {
  707. goto error;
  708. }
  709. in_use += mac_data.entries;
  710. } else {
  711. multi_overflow = 1;
  712. }
  713. n->mac_table.in_use = in_use;
  714. n->mac_table.first_multi = first_multi;
  715. n->mac_table.uni_overflow = uni_overflow;
  716. n->mac_table.multi_overflow = multi_overflow;
  717. memcpy(n->mac_table.macs, macs, MAC_TABLE_ENTRIES * ETH_ALEN);
  718. g_free(macs);
  719. rxfilter_notify(nc);
  720. return VIRTIO_NET_OK;
  721. error:
  722. g_free(macs);
  723. return VIRTIO_NET_ERR;
  724. }
  725. static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd,
  726. struct iovec *iov, unsigned int iov_cnt)
  727. {
  728. VirtIODevice *vdev = VIRTIO_DEVICE(n);
  729. uint16_t vid;
  730. size_t s;
  731. NetClientState *nc = qemu_get_queue(n->nic);
  732. s = iov_to_buf(iov, iov_cnt, 0, &vid, sizeof(vid));
  733. vid = virtio_lduw_p(vdev, &vid);
  734. if (s != sizeof(vid)) {
  735. return VIRTIO_NET_ERR;
  736. }
  737. if (vid >= MAX_VLAN)
  738. return VIRTIO_NET_ERR;
  739. if (cmd == VIRTIO_NET_CTRL_VLAN_ADD)
  740. n->vlans[vid >> 5] |= (1U << (vid & 0x1f));
  741. else if (cmd == VIRTIO_NET_CTRL_VLAN_DEL)
  742. n->vlans[vid >> 5] &= ~(1U << (vid & 0x1f));
  743. else
  744. return VIRTIO_NET_ERR;
  745. rxfilter_notify(nc);
  746. return VIRTIO_NET_OK;
  747. }
  748. static int virtio_net_handle_announce(VirtIONet *n, uint8_t cmd,
  749. struct iovec *iov, unsigned int iov_cnt)
  750. {
  751. if (cmd == VIRTIO_NET_CTRL_ANNOUNCE_ACK &&
  752. n->status & VIRTIO_NET_S_ANNOUNCE) {
  753. n->status &= ~VIRTIO_NET_S_ANNOUNCE;
  754. if (n->announce_counter) {
  755. timer_mod(n->announce_timer,
  756. qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) +
  757. self_announce_delay(n->announce_counter));
  758. }
  759. return VIRTIO_NET_OK;
  760. } else {
  761. return VIRTIO_NET_ERR;
  762. }
  763. }
  764. static int virtio_net_handle_mq(VirtIONet *n, uint8_t cmd,
  765. struct iovec *iov, unsigned int iov_cnt)
  766. {
  767. VirtIODevice *vdev = VIRTIO_DEVICE(n);
  768. struct virtio_net_ctrl_mq mq;
  769. size_t s;
  770. uint16_t queues;
  771. s = iov_to_buf(iov, iov_cnt, 0, &mq, sizeof(mq));
  772. if (s != sizeof(mq)) {
  773. return VIRTIO_NET_ERR;
  774. }
  775. if (cmd != VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET) {
  776. return VIRTIO_NET_ERR;
  777. }
  778. queues = virtio_lduw_p(vdev, &mq.virtqueue_pairs);
  779. if (queues < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN ||
  780. queues > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX ||
  781. queues > n->max_queues ||
  782. !n->multiqueue) {
  783. return VIRTIO_NET_ERR;
  784. }
  785. n->curr_queues = queues;
  786. /* stop the backend before changing the number of queues to avoid handling a
  787. * disabled queue */
  788. virtio_net_set_status(vdev, vdev->status);
  789. virtio_net_set_queues(n);
  790. return VIRTIO_NET_OK;
  791. }
  792. static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
  793. {
  794. VirtIONet *n = VIRTIO_NET(vdev);
  795. struct virtio_net_ctrl_hdr ctrl;
  796. virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
  797. VirtQueueElement *elem;
  798. size_t s;
  799. struct iovec *iov, *iov2;
  800. unsigned int iov_cnt;
  801. for (;;) {
  802. elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
  803. if (!elem) {
  804. break;
  805. }
  806. if (iov_size(elem->in_sg, elem->in_num) < sizeof(status) ||
  807. iov_size(elem->out_sg, elem->out_num) < sizeof(ctrl)) {
  808. virtio_error(vdev, "virtio-net ctrl missing headers");
  809. virtqueue_detach_element(vq, elem, 0);
  810. g_free(elem);
  811. break;
  812. }
  813. iov_cnt = elem->out_num;
  814. iov2 = iov = g_memdup(elem->out_sg, sizeof(struct iovec) * elem->out_num);
  815. s = iov_to_buf(iov, iov_cnt, 0, &ctrl, sizeof(ctrl));
  816. iov_discard_front(&iov, &iov_cnt, sizeof(ctrl));
  817. if (s != sizeof(ctrl)) {
  818. status = VIRTIO_NET_ERR;
  819. } else if (ctrl.class == VIRTIO_NET_CTRL_RX) {
  820. status = virtio_net_handle_rx_mode(n, ctrl.cmd, iov, iov_cnt);
  821. } else if (ctrl.class == VIRTIO_NET_CTRL_MAC) {
  822. status = virtio_net_handle_mac(n, ctrl.cmd, iov, iov_cnt);
  823. } else if (ctrl.class == VIRTIO_NET_CTRL_VLAN) {
  824. status = virtio_net_handle_vlan_table(n, ctrl.cmd, iov, iov_cnt);
  825. } else if (ctrl.class == VIRTIO_NET_CTRL_ANNOUNCE) {
  826. status = virtio_net_handle_announce(n, ctrl.cmd, iov, iov_cnt);
  827. } else if (ctrl.class == VIRTIO_NET_CTRL_MQ) {
  828. status = virtio_net_handle_mq(n, ctrl.cmd, iov, iov_cnt);
  829. } else if (ctrl.class == VIRTIO_NET_CTRL_GUEST_OFFLOADS) {
  830. status = virtio_net_handle_offloads(n, ctrl.cmd, iov, iov_cnt);
  831. }
  832. s = iov_from_buf(elem->in_sg, elem->in_num, 0, &status, sizeof(status));
  833. assert(s == sizeof(status));
  834. virtqueue_push(vq, elem, sizeof(status));
  835. virtio_notify(vdev, vq);
  836. g_free(iov2);
  837. g_free(elem);
  838. }
  839. }
  840. /* RX */
  841. static void virtio_net_handle_rx(VirtIODevice *vdev, VirtQueue *vq)
  842. {
  843. VirtIONet *n = VIRTIO_NET(vdev);
  844. int queue_index = vq2q(virtio_get_queue_index(vq));
  845. qemu_flush_queued_packets(qemu_get_subqueue(n->nic, queue_index));
  846. }
  847. static int virtio_net_can_receive(NetClientState *nc)
  848. {
  849. VirtIONet *n = qemu_get_nic_opaque(nc);
  850. VirtIODevice *vdev = VIRTIO_DEVICE(n);
  851. VirtIONetQueue *q = virtio_net_get_subqueue(nc);
  852. if (!vdev->vm_running) {
  853. return 0;
  854. }
  855. if (nc->queue_index >= n->curr_queues) {
  856. return 0;
  857. }
  858. if (!virtio_queue_ready(q->rx_vq) ||
  859. !(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) {
  860. return 0;
  861. }
  862. return 1;
  863. }
  864. static int virtio_net_has_buffers(VirtIONetQueue *q, int bufsize)
  865. {
  866. VirtIONet *n = q->n;
  867. if (virtio_queue_empty(q->rx_vq) ||
  868. (n->mergeable_rx_bufs &&
  869. !virtqueue_avail_bytes(q->rx_vq, bufsize, 0))) {
  870. virtio_queue_set_notification(q->rx_vq, 1);
  871. /* To avoid a race condition where the guest has made some buffers
  872. * available after the above check but before notification was
  873. * enabled, check for available buffers again.
  874. */
  875. if (virtio_queue_empty(q->rx_vq) ||
  876. (n->mergeable_rx_bufs &&
  877. !virtqueue_avail_bytes(q->rx_vq, bufsize, 0))) {
  878. return 0;
  879. }
  880. }
  881. virtio_queue_set_notification(q->rx_vq, 0);
  882. return 1;
  883. }
  884. static void virtio_net_hdr_swap(VirtIODevice *vdev, struct virtio_net_hdr *hdr)
  885. {
  886. virtio_tswap16s(vdev, &hdr->hdr_len);
  887. virtio_tswap16s(vdev, &hdr->gso_size);
  888. virtio_tswap16s(vdev, &hdr->csum_start);
  889. virtio_tswap16s(vdev, &hdr->csum_offset);
  890. }
  891. /* dhclient uses AF_PACKET but doesn't pass auxdata to the kernel so
  892. * it never finds out that the packets don't have valid checksums. This
  893. * causes dhclient to get upset. Fedora's carried a patch for ages to
  894. * fix this with Xen but it hasn't appeared in an upstream release of
  895. * dhclient yet.
  896. *
  897. * To avoid breaking existing guests, we catch udp packets and add
  898. * checksums. This is terrible but it's better than hacking the guest
  899. * kernels.
  900. *
  901. * N.B. if we introduce a zero-copy API, this operation is no longer free so
  902. * we should provide a mechanism to disable it to avoid polluting the host
  903. * cache.
  904. */
  905. static void work_around_broken_dhclient(struct virtio_net_hdr *hdr,
  906. uint8_t *buf, size_t size)
  907. {
  908. if ((hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && /* missing csum */
  909. (size > 27 && size < 1500) && /* normal sized MTU */
  910. (buf[12] == 0x08 && buf[13] == 0x00) && /* ethertype == IPv4 */
  911. (buf[23] == 17) && /* ip.protocol == UDP */
  912. (buf[34] == 0 && buf[35] == 67)) { /* udp.srcport == bootps */
  913. net_checksum_calculate(buf, size);
  914. hdr->flags &= ~VIRTIO_NET_HDR_F_NEEDS_CSUM;
  915. }
  916. }
  917. static void receive_header(VirtIONet *n, const struct iovec *iov, int iov_cnt,
  918. const void *buf, size_t size)
  919. {
  920. if (n->has_vnet_hdr) {
  921. /* FIXME this cast is evil */
  922. void *wbuf = (void *)buf;
  923. work_around_broken_dhclient(wbuf, wbuf + n->host_hdr_len,
  924. size - n->host_hdr_len);
  925. if (n->needs_vnet_hdr_swap) {
  926. virtio_net_hdr_swap(VIRTIO_DEVICE(n), wbuf);
  927. }
  928. iov_from_buf(iov, iov_cnt, 0, buf, sizeof(struct virtio_net_hdr));
  929. } else {
  930. struct virtio_net_hdr hdr = {
  931. .flags = 0,
  932. .gso_type = VIRTIO_NET_HDR_GSO_NONE
  933. };
  934. iov_from_buf(iov, iov_cnt, 0, &hdr, sizeof hdr);
  935. }
  936. }
  937. static int receive_filter(VirtIONet *n, const uint8_t *buf, int size)
  938. {
  939. static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
  940. static const uint8_t vlan[] = {0x81, 0x00};
  941. uint8_t *ptr = (uint8_t *)buf;
  942. int i;
  943. if (n->promisc)
  944. return 1;
  945. ptr += n->host_hdr_len;
  946. if (!memcmp(&ptr[12], vlan, sizeof(vlan))) {
  947. int vid = lduw_be_p(ptr + 14) & 0xfff;
  948. if (!(n->vlans[vid >> 5] & (1U << (vid & 0x1f))))
  949. return 0;
  950. }
  951. if (ptr[0] & 1) { // multicast
  952. if (!memcmp(ptr, bcast, sizeof(bcast))) {
  953. return !n->nobcast;
  954. } else if (n->nomulti) {
  955. return 0;
  956. } else if (n->allmulti || n->mac_table.multi_overflow) {
  957. return 1;
  958. }
  959. for (i = n->mac_table.first_multi; i < n->mac_table.in_use; i++) {
  960. if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
  961. return 1;
  962. }
  963. }
  964. } else { // unicast
  965. if (n->nouni) {
  966. return 0;
  967. } else if (n->alluni || n->mac_table.uni_overflow) {
  968. return 1;
  969. } else if (!memcmp(ptr, n->mac, ETH_ALEN)) {
  970. return 1;
  971. }
  972. for (i = 0; i < n->mac_table.first_multi; i++) {
  973. if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
  974. return 1;
  975. }
  976. }
  977. }
  978. return 0;
  979. }
  980. static ssize_t virtio_net_receive_rcu(NetClientState *nc, const uint8_t *buf,
  981. size_t size)
  982. {
  983. VirtIONet *n = qemu_get_nic_opaque(nc);
  984. VirtIONetQueue *q = virtio_net_get_subqueue(nc);
  985. VirtIODevice *vdev = VIRTIO_DEVICE(n);
  986. struct iovec mhdr_sg[VIRTQUEUE_MAX_SIZE];
  987. struct virtio_net_hdr_mrg_rxbuf mhdr;
  988. unsigned mhdr_cnt = 0;
  989. size_t offset, i, guest_offset;
  990. if (!virtio_net_can_receive(nc)) {
  991. return -1;
  992. }
  993. /* hdr_len refers to the header we supply to the guest */
  994. if (!virtio_net_has_buffers(q, size + n->guest_hdr_len - n->host_hdr_len)) {
  995. return 0;
  996. }
  997. if (!receive_filter(n, buf, size))
  998. return size;
  999. offset = i = 0;
  1000. while (offset < size) {
  1001. VirtQueueElement *elem;
  1002. int len, total;
  1003. const struct iovec *sg;
  1004. total = 0;
  1005. elem = virtqueue_pop(q->rx_vq, sizeof(VirtQueueElement));
  1006. if (!elem) {
  1007. if (i) {
  1008. virtio_error(vdev, "virtio-net unexpected empty queue: "
  1009. "i %zd mergeable %d offset %zd, size %zd, "
  1010. "guest hdr len %zd, host hdr len %zd "
  1011. "guest features 0x%" PRIx64,
  1012. i, n->mergeable_rx_bufs, offset, size,
  1013. n->guest_hdr_len, n->host_hdr_len,
  1014. vdev->guest_features);
  1015. }
  1016. return -1;
  1017. }
  1018. if (elem->in_num < 1) {
  1019. virtio_error(vdev,
  1020. "virtio-net receive queue contains no in buffers");
  1021. virtqueue_detach_element(q->rx_vq, elem, 0);
  1022. g_free(elem);
  1023. return -1;
  1024. }
  1025. sg = elem->in_sg;
  1026. if (i == 0) {
  1027. assert(offset == 0);
  1028. if (n->mergeable_rx_bufs) {
  1029. mhdr_cnt = iov_copy(mhdr_sg, ARRAY_SIZE(mhdr_sg),
  1030. sg, elem->in_num,
  1031. offsetof(typeof(mhdr), num_buffers),
  1032. sizeof(mhdr.num_buffers));
  1033. }
  1034. receive_header(n, sg, elem->in_num, buf, size);
  1035. offset = n->host_hdr_len;
  1036. total += n->guest_hdr_len;
  1037. guest_offset = n->guest_hdr_len;
  1038. } else {
  1039. guest_offset = 0;
  1040. }
  1041. /* copy in packet. ugh */
  1042. len = iov_from_buf(sg, elem->in_num, guest_offset,
  1043. buf + offset, size - offset);
  1044. total += len;
  1045. offset += len;
  1046. /* If buffers can't be merged, at this point we
  1047. * must have consumed the complete packet.
  1048. * Otherwise, drop it. */
  1049. if (!n->mergeable_rx_bufs && offset < size) {
  1050. virtqueue_unpop(q->rx_vq, elem, total);
  1051. g_free(elem);
  1052. return size;
  1053. }
  1054. /* signal other side */
  1055. virtqueue_fill(q->rx_vq, elem, total, i++);
  1056. g_free(elem);
  1057. }
  1058. if (mhdr_cnt) {
  1059. virtio_stw_p(vdev, &mhdr.num_buffers, i);
  1060. iov_from_buf(mhdr_sg, mhdr_cnt,
  1061. 0,
  1062. &mhdr.num_buffers, sizeof mhdr.num_buffers);
  1063. }
  1064. virtqueue_flush(q->rx_vq, i);
  1065. virtio_notify(vdev, q->rx_vq);
  1066. return size;
  1067. }
  1068. static ssize_t virtio_net_receive(NetClientState *nc, const uint8_t *buf,
  1069. size_t size)
  1070. {
  1071. ssize_t r;
  1072. rcu_read_lock();
  1073. r = virtio_net_receive_rcu(nc, buf, size);
  1074. rcu_read_unlock();
  1075. return r;
  1076. }
  1077. static int32_t virtio_net_flush_tx(VirtIONetQueue *q);
  1078. static void virtio_net_tx_complete(NetClientState *nc, ssize_t len)
  1079. {
  1080. VirtIONet *n = qemu_get_nic_opaque(nc);
  1081. VirtIONetQueue *q = virtio_net_get_subqueue(nc);
  1082. VirtIODevice *vdev = VIRTIO_DEVICE(n);
  1083. virtqueue_push(q->tx_vq, q->async_tx.elem, 0);
  1084. virtio_notify(vdev, q->tx_vq);
  1085. g_free(q->async_tx.elem);
  1086. q->async_tx.elem = NULL;
  1087. virtio_queue_set_notification(q->tx_vq, 1);
  1088. virtio_net_flush_tx(q);
  1089. }
  1090. /* TX */
  1091. static int32_t virtio_net_flush_tx(VirtIONetQueue *q)
  1092. {
  1093. VirtIONet *n = q->n;
  1094. VirtIODevice *vdev = VIRTIO_DEVICE(n);
  1095. VirtQueueElement *elem;
  1096. int32_t num_packets = 0;
  1097. int queue_index = vq2q(virtio_get_queue_index(q->tx_vq));
  1098. if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) {
  1099. return num_packets;
  1100. }
  1101. if (q->async_tx.elem) {
  1102. virtio_queue_set_notification(q->tx_vq, 0);
  1103. return num_packets;
  1104. }
  1105. for (;;) {
  1106. ssize_t ret;
  1107. unsigned int out_num;
  1108. struct iovec sg[VIRTQUEUE_MAX_SIZE], sg2[VIRTQUEUE_MAX_SIZE + 1], *out_sg;
  1109. struct virtio_net_hdr_mrg_rxbuf mhdr;
  1110. elem = virtqueue_pop(q->tx_vq, sizeof(VirtQueueElement));
  1111. if (!elem) {
  1112. break;
  1113. }
  1114. out_num = elem->out_num;
  1115. out_sg = elem->out_sg;
  1116. if (out_num < 1) {
  1117. virtio_error(vdev, "virtio-net header not in first element");
  1118. virtqueue_detach_element(q->tx_vq, elem, 0);
  1119. g_free(elem);
  1120. return -EINVAL;
  1121. }
  1122. if (n->has_vnet_hdr) {
  1123. if (iov_to_buf(out_sg, out_num, 0, &mhdr, n->guest_hdr_len) <
  1124. n->guest_hdr_len) {
  1125. virtio_error(vdev, "virtio-net header incorrect");
  1126. virtqueue_detach_element(q->tx_vq, elem, 0);
  1127. g_free(elem);
  1128. return -EINVAL;
  1129. }
  1130. if (n->needs_vnet_hdr_swap) {
  1131. virtio_net_hdr_swap(vdev, (void *) &mhdr);
  1132. sg2[0].iov_base = &mhdr;
  1133. sg2[0].iov_len = n->guest_hdr_len;
  1134. out_num = iov_copy(&sg2[1], ARRAY_SIZE(sg2) - 1,
  1135. out_sg, out_num,
  1136. n->guest_hdr_len, -1);
  1137. if (out_num == VIRTQUEUE_MAX_SIZE) {
  1138. goto drop;
  1139. }
  1140. out_num += 1;
  1141. out_sg = sg2;
  1142. }
  1143. }
  1144. /*
  1145. * If host wants to see the guest header as is, we can
  1146. * pass it on unchanged. Otherwise, copy just the parts
  1147. * that host is interested in.
  1148. */
  1149. assert(n->host_hdr_len <= n->guest_hdr_len);
  1150. if (n->host_hdr_len != n->guest_hdr_len) {
  1151. unsigned sg_num = iov_copy(sg, ARRAY_SIZE(sg),
  1152. out_sg, out_num,
  1153. 0, n->host_hdr_len);
  1154. sg_num += iov_copy(sg + sg_num, ARRAY_SIZE(sg) - sg_num,
  1155. out_sg, out_num,
  1156. n->guest_hdr_len, -1);
  1157. out_num = sg_num;
  1158. out_sg = sg;
  1159. }
  1160. ret = qemu_sendv_packet_async(qemu_get_subqueue(n->nic, queue_index),
  1161. out_sg, out_num, virtio_net_tx_complete);
  1162. if (ret == 0) {
  1163. virtio_queue_set_notification(q->tx_vq, 0);
  1164. q->async_tx.elem = elem;
  1165. return -EBUSY;
  1166. }
  1167. drop:
  1168. virtqueue_push(q->tx_vq, elem, 0);
  1169. virtio_notify(vdev, q->tx_vq);
  1170. g_free(elem);
  1171. if (++num_packets >= n->tx_burst) {
  1172. break;
  1173. }
  1174. }
  1175. return num_packets;
  1176. }
  1177. static void virtio_net_handle_tx_timer(VirtIODevice *vdev, VirtQueue *vq)
  1178. {
  1179. VirtIONet *n = VIRTIO_NET(vdev);
  1180. VirtIONetQueue *q = &n->vqs[vq2q(virtio_get_queue_index(vq))];
  1181. if (unlikely((n->status & VIRTIO_NET_S_LINK_UP) == 0)) {
  1182. virtio_net_drop_tx_queue_data(vdev, vq);
  1183. return;
  1184. }
  1185. /* This happens when device was stopped but VCPU wasn't. */
  1186. if (!vdev->vm_running) {
  1187. q->tx_waiting = 1;
  1188. return;
  1189. }
  1190. if (q->tx_waiting) {
  1191. virtio_queue_set_notification(vq, 1);
  1192. timer_del(q->tx_timer);
  1193. q->tx_waiting = 0;
  1194. if (virtio_net_flush_tx(q) == -EINVAL) {
  1195. return;
  1196. }
  1197. } else {
  1198. timer_mod(q->tx_timer,
  1199. qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + n->tx_timeout);
  1200. q->tx_waiting = 1;
  1201. virtio_queue_set_notification(vq, 0);
  1202. }
  1203. }
  1204. static void virtio_net_handle_tx_bh(VirtIODevice *vdev, VirtQueue *vq)
  1205. {
  1206. VirtIONet *n = VIRTIO_NET(vdev);
  1207. VirtIONetQueue *q = &n->vqs[vq2q(virtio_get_queue_index(vq))];
  1208. if (unlikely((n->status & VIRTIO_NET_S_LINK_UP) == 0)) {
  1209. virtio_net_drop_tx_queue_data(vdev, vq);
  1210. return;
  1211. }
  1212. if (unlikely(q->tx_waiting)) {
  1213. return;
  1214. }
  1215. q->tx_waiting = 1;
  1216. /* This happens when device was stopped but VCPU wasn't. */
  1217. if (!vdev->vm_running) {
  1218. return;
  1219. }
  1220. virtio_queue_set_notification(vq, 0);
  1221. qemu_bh_schedule(q->tx_bh);
  1222. }
  1223. static void virtio_net_tx_timer(void *opaque)
  1224. {
  1225. VirtIONetQueue *q = opaque;
  1226. VirtIONet *n = q->n;
  1227. VirtIODevice *vdev = VIRTIO_DEVICE(n);
  1228. /* This happens when device was stopped but BH wasn't. */
  1229. if (!vdev->vm_running) {
  1230. /* Make sure tx waiting is set, so we'll run when restarted. */
  1231. assert(q->tx_waiting);
  1232. return;
  1233. }
  1234. q->tx_waiting = 0;
  1235. /* Just in case the driver is not ready on more */
  1236. if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) {
  1237. return;
  1238. }
  1239. virtio_queue_set_notification(q->tx_vq, 1);
  1240. virtio_net_flush_tx(q);
  1241. }
  1242. static void virtio_net_tx_bh(void *opaque)
  1243. {
  1244. VirtIONetQueue *q = opaque;
  1245. VirtIONet *n = q->n;
  1246. VirtIODevice *vdev = VIRTIO_DEVICE(n);
  1247. int32_t ret;
  1248. /* This happens when device was stopped but BH wasn't. */
  1249. if (!vdev->vm_running) {
  1250. /* Make sure tx waiting is set, so we'll run when restarted. */
  1251. assert(q->tx_waiting);
  1252. return;
  1253. }
  1254. q->tx_waiting = 0;
  1255. /* Just in case the driver is not ready on more */
  1256. if (unlikely(!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK))) {
  1257. return;
  1258. }
  1259. ret = virtio_net_flush_tx(q);
  1260. if (ret == -EBUSY || ret == -EINVAL) {
  1261. return; /* Notification re-enable handled by tx_complete or device
  1262. * broken */
  1263. }
  1264. /* If we flush a full burst of packets, assume there are
  1265. * more coming and immediately reschedule */
  1266. if (ret >= n->tx_burst) {
  1267. qemu_bh_schedule(q->tx_bh);
  1268. q->tx_waiting = 1;
  1269. return;
  1270. }
  1271. /* If less than a full burst, re-enable notification and flush
  1272. * anything that may have come in while we weren't looking. If
  1273. * we find something, assume the guest is still active and reschedule */
  1274. virtio_queue_set_notification(q->tx_vq, 1);
  1275. ret = virtio_net_flush_tx(q);
  1276. if (ret == -EINVAL) {
  1277. return;
  1278. } else if (ret > 0) {
  1279. virtio_queue_set_notification(q->tx_vq, 0);
  1280. qemu_bh_schedule(q->tx_bh);
  1281. q->tx_waiting = 1;
  1282. }
  1283. }
  1284. static void virtio_net_add_queue(VirtIONet *n, int index)
  1285. {
  1286. VirtIODevice *vdev = VIRTIO_DEVICE(n);
  1287. n->vqs[index].rx_vq = virtio_add_queue(vdev, n->net_conf.rx_queue_size,
  1288. virtio_net_handle_rx);
  1289. if (n->net_conf.tx && !strcmp(n->net_conf.tx, "timer")) {
  1290. n->vqs[index].tx_vq =
  1291. virtio_add_queue(vdev, n->net_conf.tx_queue_size,
  1292. virtio_net_handle_tx_timer);
  1293. n->vqs[index].tx_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
  1294. virtio_net_tx_timer,
  1295. &n->vqs[index]);
  1296. } else {
  1297. n->vqs[index].tx_vq =
  1298. virtio_add_queue(vdev, n->net_conf.tx_queue_size,
  1299. virtio_net_handle_tx_bh);
  1300. n->vqs[index].tx_bh = qemu_bh_new(virtio_net_tx_bh, &n->vqs[index]);
  1301. }
  1302. n->vqs[index].tx_waiting = 0;
  1303. n->vqs[index].n = n;
  1304. }
  1305. static void virtio_net_del_queue(VirtIONet *n, int index)
  1306. {
  1307. VirtIODevice *vdev = VIRTIO_DEVICE(n);
  1308. VirtIONetQueue *q = &n->vqs[index];
  1309. NetClientState *nc = qemu_get_subqueue(n->nic, index);
  1310. qemu_purge_queued_packets(nc);
  1311. virtio_del_queue(vdev, index * 2);
  1312. if (q->tx_timer) {
  1313. timer_del(q->tx_timer);
  1314. timer_free(q->tx_timer);
  1315. q->tx_timer = NULL;
  1316. } else {
  1317. qemu_bh_delete(q->tx_bh);
  1318. q->tx_bh = NULL;
  1319. }
  1320. q->tx_waiting = 0;
  1321. virtio_del_queue(vdev, index * 2 + 1);
  1322. }
  1323. static void virtio_net_change_num_queues(VirtIONet *n, int new_max_queues)
  1324. {
  1325. VirtIODevice *vdev = VIRTIO_DEVICE(n);
  1326. int old_num_queues = virtio_get_num_queues(vdev);
  1327. int new_num_queues = new_max_queues * 2 + 1;
  1328. int i;
  1329. assert(old_num_queues >= 3);
  1330. assert(old_num_queues % 2 == 1);
  1331. if (old_num_queues == new_num_queues) {
  1332. return;
  1333. }
  1334. /*
  1335. * We always need to remove and add ctrl vq if
  1336. * old_num_queues != new_num_queues. Remove ctrl_vq first,
  1337. * and then we only enter one of the following too loops.
  1338. */
  1339. virtio_del_queue(vdev, old_num_queues - 1);
  1340. for (i = new_num_queues - 1; i < old_num_queues - 1; i += 2) {
  1341. /* new_num_queues < old_num_queues */
  1342. virtio_net_del_queue(n, i / 2);
  1343. }
  1344. for (i = old_num_queues - 1; i < new_num_queues - 1; i += 2) {
  1345. /* new_num_queues > old_num_queues */
  1346. virtio_net_add_queue(n, i / 2);
  1347. }
  1348. /* add ctrl_vq last */
  1349. n->ctrl_vq = virtio_add_queue(vdev, 64, virtio_net_handle_ctrl);
  1350. }
  1351. static void virtio_net_set_multiqueue(VirtIONet *n, int multiqueue)
  1352. {
  1353. int max = multiqueue ? n->max_queues : 1;
  1354. n->multiqueue = multiqueue;
  1355. virtio_net_change_num_queues(n, max);
  1356. virtio_net_set_queues(n);
  1357. }
  1358. static int virtio_net_post_load_device(void *opaque, int version_id)
  1359. {
  1360. VirtIONet *n = opaque;
  1361. VirtIODevice *vdev = VIRTIO_DEVICE(n);
  1362. int i, link_down;
  1363. virtio_net_set_mrg_rx_bufs(n, n->mergeable_rx_bufs,
  1364. virtio_vdev_has_feature(vdev,
  1365. VIRTIO_F_VERSION_1));
  1366. /* MAC_TABLE_ENTRIES may be different from the saved image */
  1367. if (n->mac_table.in_use > MAC_TABLE_ENTRIES) {
  1368. n->mac_table.in_use = 0;
  1369. }
  1370. if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)) {
  1371. n->curr_guest_offloads = virtio_net_supported_guest_offloads(n);
  1372. }
  1373. if (peer_has_vnet_hdr(n)) {
  1374. virtio_net_apply_guest_offloads(n);
  1375. }
  1376. virtio_net_set_queues(n);
  1377. /* Find the first multicast entry in the saved MAC filter */
  1378. for (i = 0; i < n->mac_table.in_use; i++) {
  1379. if (n->mac_table.macs[i * ETH_ALEN] & 1) {
  1380. break;
  1381. }
  1382. }
  1383. n->mac_table.first_multi = i;
  1384. /* nc.link_down can't be migrated, so infer link_down according
  1385. * to link status bit in n->status */
  1386. link_down = (n->status & VIRTIO_NET_S_LINK_UP) == 0;
  1387. for (i = 0; i < n->max_queues; i++) {
  1388. qemu_get_subqueue(n->nic, i)->link_down = link_down;
  1389. }
  1390. if (virtio_vdev_has_feature(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE) &&
  1391. virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ)) {
  1392. n->announce_counter = SELF_ANNOUNCE_ROUNDS;
  1393. timer_mod(n->announce_timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL));
  1394. }
  1395. return 0;
  1396. }
  1397. /* tx_waiting field of a VirtIONetQueue */
  1398. static const VMStateDescription vmstate_virtio_net_queue_tx_waiting = {
  1399. .name = "virtio-net-queue-tx_waiting",
  1400. .fields = (VMStateField[]) {
  1401. VMSTATE_UINT32(tx_waiting, VirtIONetQueue),
  1402. VMSTATE_END_OF_LIST()
  1403. },
  1404. };
  1405. static bool max_queues_gt_1(void *opaque, int version_id)
  1406. {
  1407. return VIRTIO_NET(opaque)->max_queues > 1;
  1408. }
  1409. static bool has_ctrl_guest_offloads(void *opaque, int version_id)
  1410. {
  1411. return virtio_vdev_has_feature(VIRTIO_DEVICE(opaque),
  1412. VIRTIO_NET_F_CTRL_GUEST_OFFLOADS);
  1413. }
  1414. static bool mac_table_fits(void *opaque, int version_id)
  1415. {
  1416. return VIRTIO_NET(opaque)->mac_table.in_use <= MAC_TABLE_ENTRIES;
  1417. }
  1418. static bool mac_table_doesnt_fit(void *opaque, int version_id)
  1419. {
  1420. return !mac_table_fits(opaque, version_id);
  1421. }
  1422. /* This temporary type is shared by all the WITH_TMP methods
  1423. * although only some fields are used by each.
  1424. */
  1425. struct VirtIONetMigTmp {
  1426. VirtIONet *parent;
  1427. VirtIONetQueue *vqs_1;
  1428. uint16_t curr_queues_1;
  1429. uint8_t has_ufo;
  1430. uint32_t has_vnet_hdr;
  1431. };
  1432. /* The 2nd and subsequent tx_waiting flags are loaded later than
  1433. * the 1st entry in the queues and only if there's more than one
  1434. * entry. We use the tmp mechanism to calculate a temporary
  1435. * pointer and count and also validate the count.
  1436. */
  1437. static int virtio_net_tx_waiting_pre_save(void *opaque)
  1438. {
  1439. struct VirtIONetMigTmp *tmp = opaque;
  1440. tmp->vqs_1 = tmp->parent->vqs + 1;
  1441. tmp->curr_queues_1 = tmp->parent->curr_queues - 1;
  1442. if (tmp->parent->curr_queues == 0) {
  1443. tmp->curr_queues_1 = 0;
  1444. }
  1445. return 0;
  1446. }
  1447. static int virtio_net_tx_waiting_pre_load(void *opaque)
  1448. {
  1449. struct VirtIONetMigTmp *tmp = opaque;
  1450. /* Reuse the pointer setup from save */
  1451. virtio_net_tx_waiting_pre_save(opaque);
  1452. if (tmp->parent->curr_queues > tmp->parent->max_queues) {
  1453. error_report("virtio-net: curr_queues %x > max_queues %x",
  1454. tmp->parent->curr_queues, tmp->parent->max_queues);
  1455. return -EINVAL;
  1456. }
  1457. return 0; /* all good */
  1458. }
  1459. static const VMStateDescription vmstate_virtio_net_tx_waiting = {
  1460. .name = "virtio-net-tx_waiting",
  1461. .pre_load = virtio_net_tx_waiting_pre_load,
  1462. .pre_save = virtio_net_tx_waiting_pre_save,
  1463. .fields = (VMStateField[]) {
  1464. VMSTATE_STRUCT_VARRAY_POINTER_UINT16(vqs_1, struct VirtIONetMigTmp,
  1465. curr_queues_1,
  1466. vmstate_virtio_net_queue_tx_waiting,
  1467. struct VirtIONetQueue),
  1468. VMSTATE_END_OF_LIST()
  1469. },
  1470. };
  1471. /* the 'has_ufo' flag is just tested; if the incoming stream has the
  1472. * flag set we need to check that we have it
  1473. */
  1474. static int virtio_net_ufo_post_load(void *opaque, int version_id)
  1475. {
  1476. struct VirtIONetMigTmp *tmp = opaque;
  1477. if (tmp->has_ufo && !peer_has_ufo(tmp->parent)) {
  1478. error_report("virtio-net: saved image requires TUN_F_UFO support");
  1479. return -EINVAL;
  1480. }
  1481. return 0;
  1482. }
  1483. static int virtio_net_ufo_pre_save(void *opaque)
  1484. {
  1485. struct VirtIONetMigTmp *tmp = opaque;
  1486. tmp->has_ufo = tmp->parent->has_ufo;
  1487. return 0;
  1488. }
  1489. static const VMStateDescription vmstate_virtio_net_has_ufo = {
  1490. .name = "virtio-net-ufo",
  1491. .post_load = virtio_net_ufo_post_load,
  1492. .pre_save = virtio_net_ufo_pre_save,
  1493. .fields = (VMStateField[]) {
  1494. VMSTATE_UINT8(has_ufo, struct VirtIONetMigTmp),
  1495. VMSTATE_END_OF_LIST()
  1496. },
  1497. };
  1498. /* the 'has_vnet_hdr' flag is just tested; if the incoming stream has the
  1499. * flag set we need to check that we have it
  1500. */
  1501. static int virtio_net_vnet_post_load(void *opaque, int version_id)
  1502. {
  1503. struct VirtIONetMigTmp *tmp = opaque;
  1504. if (tmp->has_vnet_hdr && !peer_has_vnet_hdr(tmp->parent)) {
  1505. error_report("virtio-net: saved image requires vnet_hdr=on");
  1506. return -EINVAL;
  1507. }
  1508. return 0;
  1509. }
  1510. static int virtio_net_vnet_pre_save(void *opaque)
  1511. {
  1512. struct VirtIONetMigTmp *tmp = opaque;
  1513. tmp->has_vnet_hdr = tmp->parent->has_vnet_hdr;
  1514. return 0;
  1515. }
  1516. static const VMStateDescription vmstate_virtio_net_has_vnet = {
  1517. .name = "virtio-net-vnet",
  1518. .post_load = virtio_net_vnet_post_load,
  1519. .pre_save = virtio_net_vnet_pre_save,
  1520. .fields = (VMStateField[]) {
  1521. VMSTATE_UINT32(has_vnet_hdr, struct VirtIONetMigTmp),
  1522. VMSTATE_END_OF_LIST()
  1523. },
  1524. };
  1525. static const VMStateDescription vmstate_virtio_net_device = {
  1526. .name = "virtio-net-device",
  1527. .version_id = VIRTIO_NET_VM_VERSION,
  1528. .minimum_version_id = VIRTIO_NET_VM_VERSION,
  1529. .post_load = virtio_net_post_load_device,
  1530. .fields = (VMStateField[]) {
  1531. VMSTATE_UINT8_ARRAY(mac, VirtIONet, ETH_ALEN),
  1532. VMSTATE_STRUCT_POINTER(vqs, VirtIONet,
  1533. vmstate_virtio_net_queue_tx_waiting,
  1534. VirtIONetQueue),
  1535. VMSTATE_UINT32(mergeable_rx_bufs, VirtIONet),
  1536. VMSTATE_UINT16(status, VirtIONet),
  1537. VMSTATE_UINT8(promisc, VirtIONet),
  1538. VMSTATE_UINT8(allmulti, VirtIONet),
  1539. VMSTATE_UINT32(mac_table.in_use, VirtIONet),
  1540. /* Guarded pair: If it fits we load it, else we throw it away
  1541. * - can happen if source has a larger MAC table.; post-load
  1542. * sets flags in this case.
  1543. */
  1544. VMSTATE_VBUFFER_MULTIPLY(mac_table.macs, VirtIONet,
  1545. 0, mac_table_fits, mac_table.in_use,
  1546. ETH_ALEN),
  1547. VMSTATE_UNUSED_VARRAY_UINT32(VirtIONet, mac_table_doesnt_fit, 0,
  1548. mac_table.in_use, ETH_ALEN),
  1549. /* Note: This is an array of uint32's that's always been saved as a
  1550. * buffer; hold onto your endiannesses; it's actually used as a bitmap
  1551. * but based on the uint.
  1552. */
  1553. VMSTATE_BUFFER_POINTER_UNSAFE(vlans, VirtIONet, 0, MAX_VLAN >> 3),
  1554. VMSTATE_WITH_TMP(VirtIONet, struct VirtIONetMigTmp,
  1555. vmstate_virtio_net_has_vnet),
  1556. VMSTATE_UINT8(mac_table.multi_overflow, VirtIONet),
  1557. VMSTATE_UINT8(mac_table.uni_overflow, VirtIONet),
  1558. VMSTATE_UINT8(alluni, VirtIONet),
  1559. VMSTATE_UINT8(nomulti, VirtIONet),
  1560. VMSTATE_UINT8(nouni, VirtIONet),
  1561. VMSTATE_UINT8(nobcast, VirtIONet),
  1562. VMSTATE_WITH_TMP(VirtIONet, struct VirtIONetMigTmp,
  1563. vmstate_virtio_net_has_ufo),
  1564. VMSTATE_SINGLE_TEST(max_queues, VirtIONet, max_queues_gt_1, 0,
  1565. vmstate_info_uint16_equal, uint16_t),
  1566. VMSTATE_UINT16_TEST(curr_queues, VirtIONet, max_queues_gt_1),
  1567. VMSTATE_WITH_TMP(VirtIONet, struct VirtIONetMigTmp,
  1568. vmstate_virtio_net_tx_waiting),
  1569. VMSTATE_UINT64_TEST(curr_guest_offloads, VirtIONet,
  1570. has_ctrl_guest_offloads),
  1571. VMSTATE_END_OF_LIST()
  1572. },
  1573. };
  1574. static NetClientInfo net_virtio_info = {
  1575. .type = NET_CLIENT_DRIVER_NIC,
  1576. .size = sizeof(NICState),
  1577. .can_receive = virtio_net_can_receive,
  1578. .receive = virtio_net_receive,
  1579. .link_status_changed = virtio_net_set_link_status,
  1580. .query_rx_filter = virtio_net_query_rxfilter,
  1581. };
  1582. static bool virtio_net_guest_notifier_pending(VirtIODevice *vdev, int idx)
  1583. {
  1584. VirtIONet *n = VIRTIO_NET(vdev);
  1585. NetClientState *nc = qemu_get_subqueue(n->nic, vq2q(idx));
  1586. assert(n->vhost_started);
  1587. return vhost_net_virtqueue_pending(get_vhost_net(nc->peer), idx);
  1588. }
  1589. static void virtio_net_guest_notifier_mask(VirtIODevice *vdev, int idx,
  1590. bool mask)
  1591. {
  1592. VirtIONet *n = VIRTIO_NET(vdev);
  1593. NetClientState *nc = qemu_get_subqueue(n->nic, vq2q(idx));
  1594. assert(n->vhost_started);
  1595. vhost_net_virtqueue_mask(get_vhost_net(nc->peer),
  1596. vdev, idx, mask);
  1597. }
  1598. static void virtio_net_set_config_size(VirtIONet *n, uint64_t host_features)
  1599. {
  1600. int i, config_size = 0;
  1601. virtio_add_feature(&host_features, VIRTIO_NET_F_MAC);
  1602. for (i = 0; feature_sizes[i].flags != 0; i++) {
  1603. if (host_features & feature_sizes[i].flags) {
  1604. config_size = MAX(feature_sizes[i].end, config_size);
  1605. }
  1606. }
  1607. n->config_size = config_size;
  1608. }
  1609. void virtio_net_set_netclient_name(VirtIONet *n, const char *name,
  1610. const char *type)
  1611. {
  1612. /*
  1613. * The name can be NULL, the netclient name will be type.x.
  1614. */
  1615. assert(type != NULL);
  1616. g_free(n->netclient_name);
  1617. g_free(n->netclient_type);
  1618. n->netclient_name = g_strdup(name);
  1619. n->netclient_type = g_strdup(type);
  1620. }
  1621. static void virtio_net_device_realize(DeviceState *dev, Error **errp)
  1622. {
  1623. VirtIODevice *vdev = VIRTIO_DEVICE(dev);
  1624. VirtIONet *n = VIRTIO_NET(dev);
  1625. NetClientState *nc;
  1626. int i;
  1627. if (n->net_conf.mtu) {
  1628. n->host_features |= (0x1 << VIRTIO_NET_F_MTU);
  1629. }
  1630. virtio_net_set_config_size(n, n->host_features);
  1631. virtio_init(vdev, "virtio-net", VIRTIO_ID_NET, n->config_size);
  1632. /*
  1633. * We set a lower limit on RX queue size to what it always was.
  1634. * Guests that want a smaller ring can always resize it without
  1635. * help from us (using virtio 1 and up).
  1636. */
  1637. if (n->net_conf.rx_queue_size < VIRTIO_NET_RX_QUEUE_MIN_SIZE ||
  1638. n->net_conf.rx_queue_size > VIRTQUEUE_MAX_SIZE ||
  1639. !is_power_of_2(n->net_conf.rx_queue_size)) {
  1640. error_setg(errp, "Invalid rx_queue_size (= %" PRIu16 "), "
  1641. "must be a power of 2 between %d and %d.",
  1642. n->net_conf.rx_queue_size, VIRTIO_NET_RX_QUEUE_MIN_SIZE,
  1643. VIRTQUEUE_MAX_SIZE);
  1644. virtio_cleanup(vdev);
  1645. return;
  1646. }
  1647. if (n->net_conf.tx_queue_size < VIRTIO_NET_TX_QUEUE_MIN_SIZE ||
  1648. n->net_conf.tx_queue_size > VIRTQUEUE_MAX_SIZE ||
  1649. !is_power_of_2(n->net_conf.tx_queue_size)) {
  1650. error_setg(errp, "Invalid tx_queue_size (= %" PRIu16 "), "
  1651. "must be a power of 2 between %d and %d",
  1652. n->net_conf.tx_queue_size, VIRTIO_NET_TX_QUEUE_MIN_SIZE,
  1653. VIRTQUEUE_MAX_SIZE);
  1654. virtio_cleanup(vdev);
  1655. return;
  1656. }
  1657. n->max_queues = MAX(n->nic_conf.peers.queues, 1);
  1658. if (n->max_queues * 2 + 1 > VIRTIO_QUEUE_MAX) {
  1659. error_setg(errp, "Invalid number of queues (= %" PRIu32 "), "
  1660. "must be a positive integer less than %d.",
  1661. n->max_queues, (VIRTIO_QUEUE_MAX - 1) / 2);
  1662. virtio_cleanup(vdev);
  1663. return;
  1664. }
  1665. n->vqs = g_malloc0(sizeof(VirtIONetQueue) * n->max_queues);
  1666. n->curr_queues = 1;
  1667. n->tx_timeout = n->net_conf.txtimer;
  1668. if (n->net_conf.tx && strcmp(n->net_conf.tx, "timer")
  1669. && strcmp(n->net_conf.tx, "bh")) {
  1670. error_report("virtio-net: "
  1671. "Unknown option tx=%s, valid options: \"timer\" \"bh\"",
  1672. n->net_conf.tx);
  1673. error_report("Defaulting to \"bh\"");
  1674. }
  1675. n->net_conf.tx_queue_size = MIN(virtio_net_max_tx_queue_size(n),
  1676. n->net_conf.tx_queue_size);
  1677. for (i = 0; i < n->max_queues; i++) {
  1678. virtio_net_add_queue(n, i);
  1679. }
  1680. n->ctrl_vq = virtio_add_queue(vdev, 64, virtio_net_handle_ctrl);
  1681. qemu_macaddr_default_if_unset(&n->nic_conf.macaddr);
  1682. memcpy(&n->mac[0], &n->nic_conf.macaddr, sizeof(n->mac));
  1683. n->status = VIRTIO_NET_S_LINK_UP;
  1684. n->announce_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL,
  1685. virtio_net_announce_timer, n);
  1686. if (n->netclient_type) {
  1687. /*
  1688. * Happen when virtio_net_set_netclient_name has been called.
  1689. */
  1690. n->nic = qemu_new_nic(&net_virtio_info, &n->nic_conf,
  1691. n->netclient_type, n->netclient_name, n);
  1692. } else {
  1693. n->nic = qemu_new_nic(&net_virtio_info, &n->nic_conf,
  1694. object_get_typename(OBJECT(dev)), dev->id, n);
  1695. }
  1696. peer_test_vnet_hdr(n);
  1697. if (peer_has_vnet_hdr(n)) {
  1698. for (i = 0; i < n->max_queues; i++) {
  1699. qemu_using_vnet_hdr(qemu_get_subqueue(n->nic, i)->peer, true);
  1700. }
  1701. n->host_hdr_len = sizeof(struct virtio_net_hdr);
  1702. } else {
  1703. n->host_hdr_len = 0;
  1704. }
  1705. qemu_format_nic_info_str(qemu_get_queue(n->nic), n->nic_conf.macaddr.a);
  1706. n->vqs[0].tx_waiting = 0;
  1707. n->tx_burst = n->net_conf.txburst;
  1708. virtio_net_set_mrg_rx_bufs(n, 0, 0);
  1709. n->promisc = 1; /* for compatibility */
  1710. n->mac_table.macs = g_malloc0(MAC_TABLE_ENTRIES * ETH_ALEN);
  1711. n->vlans = g_malloc0(MAX_VLAN >> 3);
  1712. nc = qemu_get_queue(n->nic);
  1713. nc->rxfilter_notify_enabled = 1;
  1714. n->qdev = dev;
  1715. }
  1716. static void virtio_net_device_unrealize(DeviceState *dev, Error **errp)
  1717. {
  1718. VirtIODevice *vdev = VIRTIO_DEVICE(dev);
  1719. VirtIONet *n = VIRTIO_NET(dev);
  1720. int i, max_queues;
  1721. /* This will stop vhost backend if appropriate. */
  1722. virtio_net_set_status(vdev, 0);
  1723. g_free(n->netclient_name);
  1724. n->netclient_name = NULL;
  1725. g_free(n->netclient_type);
  1726. n->netclient_type = NULL;
  1727. g_free(n->mac_table.macs);
  1728. g_free(n->vlans);
  1729. max_queues = n->multiqueue ? n->max_queues : 1;
  1730. for (i = 0; i < max_queues; i++) {
  1731. virtio_net_del_queue(n, i);
  1732. }
  1733. timer_del(n->announce_timer);
  1734. timer_free(n->announce_timer);
  1735. g_free(n->vqs);
  1736. qemu_del_nic(n->nic);
  1737. virtio_cleanup(vdev);
  1738. }
  1739. static void virtio_net_instance_init(Object *obj)
  1740. {
  1741. VirtIONet *n = VIRTIO_NET(obj);
  1742. /*
  1743. * The default config_size is sizeof(struct virtio_net_config).
  1744. * Can be overriden with virtio_net_set_config_size.
  1745. */
  1746. n->config_size = sizeof(struct virtio_net_config);
  1747. device_add_bootindex_property(obj, &n->nic_conf.bootindex,
  1748. "bootindex", "/ethernet-phy@0",
  1749. DEVICE(n), NULL);
  1750. }
  1751. static int virtio_net_pre_save(void *opaque)
  1752. {
  1753. VirtIONet *n = opaque;
  1754. /* At this point, backend must be stopped, otherwise
  1755. * it might keep writing to memory. */
  1756. assert(!n->vhost_started);
  1757. return 0;
  1758. }
  1759. static const VMStateDescription vmstate_virtio_net = {
  1760. .name = "virtio-net",
  1761. .minimum_version_id = VIRTIO_NET_VM_VERSION,
  1762. .version_id = VIRTIO_NET_VM_VERSION,
  1763. .fields = (VMStateField[]) {
  1764. VMSTATE_VIRTIO_DEVICE,
  1765. VMSTATE_END_OF_LIST()
  1766. },
  1767. .pre_save = virtio_net_pre_save,
  1768. };
  1769. static Property virtio_net_properties[] = {
  1770. DEFINE_PROP_BIT("csum", VirtIONet, host_features, VIRTIO_NET_F_CSUM, true),
  1771. DEFINE_PROP_BIT("guest_csum", VirtIONet, host_features,
  1772. VIRTIO_NET_F_GUEST_CSUM, true),
  1773. DEFINE_PROP_BIT("gso", VirtIONet, host_features, VIRTIO_NET_F_GSO, true),
  1774. DEFINE_PROP_BIT("guest_tso4", VirtIONet, host_features,
  1775. VIRTIO_NET_F_GUEST_TSO4, true),
  1776. DEFINE_PROP_BIT("guest_tso6", VirtIONet, host_features,
  1777. VIRTIO_NET_F_GUEST_TSO6, true),
  1778. DEFINE_PROP_BIT("guest_ecn", VirtIONet, host_features,
  1779. VIRTIO_NET_F_GUEST_ECN, true),
  1780. DEFINE_PROP_BIT("guest_ufo", VirtIONet, host_features,
  1781. VIRTIO_NET_F_GUEST_UFO, true),
  1782. DEFINE_PROP_BIT("guest_announce", VirtIONet, host_features,
  1783. VIRTIO_NET_F_GUEST_ANNOUNCE, true),
  1784. DEFINE_PROP_BIT("host_tso4", VirtIONet, host_features,
  1785. VIRTIO_NET_F_HOST_TSO4, true),
  1786. DEFINE_PROP_BIT("host_tso6", VirtIONet, host_features,
  1787. VIRTIO_NET_F_HOST_TSO6, true),
  1788. DEFINE_PROP_BIT("host_ecn", VirtIONet, host_features,
  1789. VIRTIO_NET_F_HOST_ECN, true),
  1790. DEFINE_PROP_BIT("host_ufo", VirtIONet, host_features,
  1791. VIRTIO_NET_F_HOST_UFO, true),
  1792. DEFINE_PROP_BIT("mrg_rxbuf", VirtIONet, host_features,
  1793. VIRTIO_NET_F_MRG_RXBUF, true),
  1794. DEFINE_PROP_BIT("status", VirtIONet, host_features,
  1795. VIRTIO_NET_F_STATUS, true),
  1796. DEFINE_PROP_BIT("ctrl_vq", VirtIONet, host_features,
  1797. VIRTIO_NET_F_CTRL_VQ, true),
  1798. DEFINE_PROP_BIT("ctrl_rx", VirtIONet, host_features,
  1799. VIRTIO_NET_F_CTRL_RX, true),
  1800. DEFINE_PROP_BIT("ctrl_vlan", VirtIONet, host_features,
  1801. VIRTIO_NET_F_CTRL_VLAN, true),
  1802. DEFINE_PROP_BIT("ctrl_rx_extra", VirtIONet, host_features,
  1803. VIRTIO_NET_F_CTRL_RX_EXTRA, true),
  1804. DEFINE_PROP_BIT("ctrl_mac_addr", VirtIONet, host_features,
  1805. VIRTIO_NET_F_CTRL_MAC_ADDR, true),
  1806. DEFINE_PROP_BIT("ctrl_guest_offloads", VirtIONet, host_features,
  1807. VIRTIO_NET_F_CTRL_GUEST_OFFLOADS, true),
  1808. DEFINE_PROP_BIT("mq", VirtIONet, host_features, VIRTIO_NET_F_MQ, false),
  1809. DEFINE_NIC_PROPERTIES(VirtIONet, nic_conf),
  1810. DEFINE_PROP_UINT32("x-txtimer", VirtIONet, net_conf.txtimer,
  1811. TX_TIMER_INTERVAL),
  1812. DEFINE_PROP_INT32("x-txburst", VirtIONet, net_conf.txburst, TX_BURST),
  1813. DEFINE_PROP_STRING("tx", VirtIONet, net_conf.tx),
  1814. DEFINE_PROP_UINT16("rx_queue_size", VirtIONet, net_conf.rx_queue_size,
  1815. VIRTIO_NET_RX_QUEUE_DEFAULT_SIZE),
  1816. DEFINE_PROP_UINT16("tx_queue_size", VirtIONet, net_conf.tx_queue_size,
  1817. VIRTIO_NET_TX_QUEUE_DEFAULT_SIZE),
  1818. DEFINE_PROP_UINT16("host_mtu", VirtIONet, net_conf.mtu, 0),
  1819. DEFINE_PROP_BOOL("x-mtu-bypass-backend", VirtIONet, mtu_bypass_backend,
  1820. true),
  1821. DEFINE_PROP_END_OF_LIST(),
  1822. };
  1823. static void virtio_net_class_init(ObjectClass *klass, void *data)
  1824. {
  1825. DeviceClass *dc = DEVICE_CLASS(klass);
  1826. VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
  1827. dc->props = virtio_net_properties;
  1828. dc->vmsd = &vmstate_virtio_net;
  1829. set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
  1830. vdc->realize = virtio_net_device_realize;
  1831. vdc->unrealize = virtio_net_device_unrealize;
  1832. vdc->get_config = virtio_net_get_config;
  1833. vdc->set_config = virtio_net_set_config;
  1834. vdc->get_features = virtio_net_get_features;
  1835. vdc->set_features = virtio_net_set_features;
  1836. vdc->bad_features = virtio_net_bad_features;
  1837. vdc->reset = virtio_net_reset;
  1838. vdc->set_status = virtio_net_set_status;
  1839. vdc->guest_notifier_mask = virtio_net_guest_notifier_mask;
  1840. vdc->guest_notifier_pending = virtio_net_guest_notifier_pending;
  1841. vdc->legacy_features |= (0x1 << VIRTIO_NET_F_GSO);
  1842. vdc->vmsd = &vmstate_virtio_net_device;
  1843. }
  1844. static const TypeInfo virtio_net_info = {
  1845. .name = TYPE_VIRTIO_NET,
  1846. .parent = TYPE_VIRTIO_DEVICE,
  1847. .instance_size = sizeof(VirtIONet),
  1848. .instance_init = virtio_net_instance_init,
  1849. .class_init = virtio_net_class_init,
  1850. };
  1851. static void virtio_register_types(void)
  1852. {
  1853. type_register_static(&virtio_net_info);
  1854. }
  1855. type_init(virtio_register_types)