virtio-net.c 68 KB

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