virtio-net.c 65 KB

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