virtio-net.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419
  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/iov.h"
  14. #include "virtio.h"
  15. #include "net/net.h"
  16. #include "net/checksum.h"
  17. #include "net/tap.h"
  18. #include "qemu/error-report.h"
  19. #include "qemu/timer.h"
  20. #include "virtio-net.h"
  21. #include "vhost_net.h"
  22. #define VIRTIO_NET_VM_VERSION 11
  23. #define MAC_TABLE_ENTRIES 64
  24. #define MAX_VLAN (1 << 12) /* Per 802.1Q definition */
  25. typedef struct VirtIONetQueue {
  26. VirtQueue *rx_vq;
  27. VirtQueue *tx_vq;
  28. QEMUTimer *tx_timer;
  29. QEMUBH *tx_bh;
  30. int tx_waiting;
  31. struct {
  32. VirtQueueElement elem;
  33. ssize_t len;
  34. } async_tx;
  35. struct VirtIONet *n;
  36. } VirtIONetQueue;
  37. typedef struct VirtIONet
  38. {
  39. VirtIODevice vdev;
  40. uint8_t mac[ETH_ALEN];
  41. uint16_t status;
  42. VirtIONetQueue *vqs;
  43. VirtQueue *ctrl_vq;
  44. NICState *nic;
  45. uint32_t tx_timeout;
  46. int32_t tx_burst;
  47. uint32_t has_vnet_hdr;
  48. size_t host_hdr_len;
  49. size_t guest_hdr_len;
  50. uint8_t has_ufo;
  51. int mergeable_rx_bufs;
  52. uint8_t promisc;
  53. uint8_t allmulti;
  54. uint8_t alluni;
  55. uint8_t nomulti;
  56. uint8_t nouni;
  57. uint8_t nobcast;
  58. uint8_t vhost_started;
  59. struct {
  60. int in_use;
  61. int first_multi;
  62. uint8_t multi_overflow;
  63. uint8_t uni_overflow;
  64. uint8_t *macs;
  65. } mac_table;
  66. uint32_t *vlans;
  67. DeviceState *qdev;
  68. int multiqueue;
  69. uint16_t max_queues;
  70. uint16_t curr_queues;
  71. size_t config_size;
  72. } VirtIONet;
  73. /*
  74. * Calculate the number of bytes up to and including the given 'field' of
  75. * 'container'.
  76. */
  77. #define endof(container, field) \
  78. (offsetof(container, field) + sizeof(((container *)0)->field))
  79. typedef struct VirtIOFeature {
  80. uint32_t flags;
  81. size_t end;
  82. } VirtIOFeature;
  83. static VirtIOFeature feature_sizes[] = {
  84. {.flags = 1 << VIRTIO_NET_F_MAC,
  85. .end = endof(struct virtio_net_config, mac)},
  86. {.flags = 1 << VIRTIO_NET_F_STATUS,
  87. .end = endof(struct virtio_net_config, status)},
  88. {.flags = 1 << VIRTIO_NET_F_MQ,
  89. .end = endof(struct virtio_net_config, max_virtqueue_pairs)},
  90. {}
  91. };
  92. static VirtIONetQueue *virtio_net_get_subqueue(NetClientState *nc)
  93. {
  94. VirtIONet *n = qemu_get_nic_opaque(nc);
  95. return &n->vqs[nc->queue_index];
  96. }
  97. static int vq2q(int queue_index)
  98. {
  99. return queue_index / 2;
  100. }
  101. /* TODO
  102. * - we could suppress RX interrupt if we were so inclined.
  103. */
  104. static VirtIONet *to_virtio_net(VirtIODevice *vdev)
  105. {
  106. return (VirtIONet *)vdev;
  107. }
  108. static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config)
  109. {
  110. VirtIONet *n = to_virtio_net(vdev);
  111. struct virtio_net_config netcfg;
  112. stw_p(&netcfg.status, n->status);
  113. stw_p(&netcfg.max_virtqueue_pairs, n->max_queues);
  114. memcpy(netcfg.mac, n->mac, ETH_ALEN);
  115. memcpy(config, &netcfg, n->config_size);
  116. }
  117. static void virtio_net_set_config(VirtIODevice *vdev, const uint8_t *config)
  118. {
  119. VirtIONet *n = to_virtio_net(vdev);
  120. struct virtio_net_config netcfg = {};
  121. memcpy(&netcfg, config, n->config_size);
  122. if (!(n->vdev.guest_features >> VIRTIO_NET_F_CTRL_MAC_ADDR & 1) &&
  123. memcmp(netcfg.mac, n->mac, ETH_ALEN)) {
  124. memcpy(n->mac, netcfg.mac, ETH_ALEN);
  125. qemu_format_nic_info_str(qemu_get_queue(n->nic), n->mac);
  126. }
  127. }
  128. static bool virtio_net_started(VirtIONet *n, uint8_t status)
  129. {
  130. return (status & VIRTIO_CONFIG_S_DRIVER_OK) &&
  131. (n->status & VIRTIO_NET_S_LINK_UP) && n->vdev.vm_running;
  132. }
  133. static void virtio_net_vhost_status(VirtIONet *n, uint8_t status)
  134. {
  135. NetClientState *nc = qemu_get_queue(n->nic);
  136. int queues = n->multiqueue ? n->max_queues : 1;
  137. if (!nc->peer) {
  138. return;
  139. }
  140. if (nc->peer->info->type != NET_CLIENT_OPTIONS_KIND_TAP) {
  141. return;
  142. }
  143. if (!tap_get_vhost_net(nc->peer)) {
  144. return;
  145. }
  146. if (!!n->vhost_started == virtio_net_started(n, status) &&
  147. !nc->peer->link_down) {
  148. return;
  149. }
  150. if (!n->vhost_started) {
  151. int r;
  152. if (!vhost_net_query(tap_get_vhost_net(nc->peer), &n->vdev)) {
  153. return;
  154. }
  155. n->vhost_started = 1;
  156. r = vhost_net_start(&n->vdev, n->nic->ncs, queues);
  157. if (r < 0) {
  158. error_report("unable to start vhost net: %d: "
  159. "falling back on userspace virtio", -r);
  160. n->vhost_started = 0;
  161. }
  162. } else {
  163. vhost_net_stop(&n->vdev, n->nic->ncs, queues);
  164. n->vhost_started = 0;
  165. }
  166. }
  167. static void virtio_net_set_status(struct VirtIODevice *vdev, uint8_t status)
  168. {
  169. VirtIONet *n = to_virtio_net(vdev);
  170. VirtIONetQueue *q;
  171. int i;
  172. uint8_t queue_status;
  173. virtio_net_vhost_status(n, status);
  174. for (i = 0; i < n->max_queues; i++) {
  175. q = &n->vqs[i];
  176. if ((!n->multiqueue && i != 0) || i >= n->curr_queues) {
  177. queue_status = 0;
  178. } else {
  179. queue_status = status;
  180. }
  181. if (!q->tx_waiting) {
  182. continue;
  183. }
  184. if (virtio_net_started(n, queue_status) && !n->vhost_started) {
  185. if (q->tx_timer) {
  186. qemu_mod_timer(q->tx_timer,
  187. qemu_get_clock_ns(vm_clock) + n->tx_timeout);
  188. } else {
  189. qemu_bh_schedule(q->tx_bh);
  190. }
  191. } else {
  192. if (q->tx_timer) {
  193. qemu_del_timer(q->tx_timer);
  194. } else {
  195. qemu_bh_cancel(q->tx_bh);
  196. }
  197. }
  198. }
  199. }
  200. static void virtio_net_set_link_status(NetClientState *nc)
  201. {
  202. VirtIONet *n = qemu_get_nic_opaque(nc);
  203. uint16_t old_status = n->status;
  204. if (nc->link_down)
  205. n->status &= ~VIRTIO_NET_S_LINK_UP;
  206. else
  207. n->status |= VIRTIO_NET_S_LINK_UP;
  208. if (n->status != old_status)
  209. virtio_notify_config(&n->vdev);
  210. virtio_net_set_status(&n->vdev, n->vdev.status);
  211. }
  212. static void virtio_net_reset(VirtIODevice *vdev)
  213. {
  214. VirtIONet *n = to_virtio_net(vdev);
  215. /* Reset back to compatibility mode */
  216. n->promisc = 1;
  217. n->allmulti = 0;
  218. n->alluni = 0;
  219. n->nomulti = 0;
  220. n->nouni = 0;
  221. n->nobcast = 0;
  222. /* multiqueue is disabled by default */
  223. n->curr_queues = 1;
  224. /* Flush any MAC and VLAN filter table state */
  225. n->mac_table.in_use = 0;
  226. n->mac_table.first_multi = 0;
  227. n->mac_table.multi_overflow = 0;
  228. n->mac_table.uni_overflow = 0;
  229. memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
  230. memcpy(&n->mac[0], &n->nic->conf->macaddr, sizeof(n->mac));
  231. memset(n->vlans, 0, MAX_VLAN >> 3);
  232. }
  233. static void peer_test_vnet_hdr(VirtIONet *n)
  234. {
  235. NetClientState *nc = qemu_get_queue(n->nic);
  236. if (!nc->peer) {
  237. return;
  238. }
  239. if (nc->peer->info->type != NET_CLIENT_OPTIONS_KIND_TAP) {
  240. return;
  241. }
  242. n->has_vnet_hdr = tap_has_vnet_hdr(nc->peer);
  243. }
  244. static int peer_has_vnet_hdr(VirtIONet *n)
  245. {
  246. return n->has_vnet_hdr;
  247. }
  248. static int peer_has_ufo(VirtIONet *n)
  249. {
  250. if (!peer_has_vnet_hdr(n))
  251. return 0;
  252. n->has_ufo = tap_has_ufo(qemu_get_queue(n->nic)->peer);
  253. return n->has_ufo;
  254. }
  255. static void virtio_net_set_mrg_rx_bufs(VirtIONet *n, int mergeable_rx_bufs)
  256. {
  257. int i;
  258. NetClientState *nc;
  259. n->mergeable_rx_bufs = mergeable_rx_bufs;
  260. n->guest_hdr_len = n->mergeable_rx_bufs ?
  261. sizeof(struct virtio_net_hdr_mrg_rxbuf) : sizeof(struct virtio_net_hdr);
  262. for (i = 0; i < n->max_queues; i++) {
  263. nc = qemu_get_subqueue(n->nic, i);
  264. if (peer_has_vnet_hdr(n) &&
  265. tap_has_vnet_hdr_len(nc->peer, n->guest_hdr_len)) {
  266. tap_set_vnet_hdr_len(nc->peer, n->guest_hdr_len);
  267. n->host_hdr_len = n->guest_hdr_len;
  268. }
  269. }
  270. }
  271. static int peer_attach(VirtIONet *n, int index)
  272. {
  273. NetClientState *nc = qemu_get_subqueue(n->nic, index);
  274. if (!nc->peer) {
  275. return 0;
  276. }
  277. if (nc->peer->info->type != NET_CLIENT_OPTIONS_KIND_TAP) {
  278. return 0;
  279. }
  280. return tap_enable(nc->peer);
  281. }
  282. static int peer_detach(VirtIONet *n, int index)
  283. {
  284. NetClientState *nc = qemu_get_subqueue(n->nic, index);
  285. if (!nc->peer) {
  286. return 0;
  287. }
  288. if (nc->peer->info->type != NET_CLIENT_OPTIONS_KIND_TAP) {
  289. return 0;
  290. }
  291. return tap_disable(nc->peer);
  292. }
  293. static void virtio_net_set_queues(VirtIONet *n)
  294. {
  295. int i;
  296. for (i = 0; i < n->max_queues; i++) {
  297. if (i < n->curr_queues) {
  298. assert(!peer_attach(n, i));
  299. } else {
  300. assert(!peer_detach(n, i));
  301. }
  302. }
  303. }
  304. static void virtio_net_set_multiqueue(VirtIONet *n, int multiqueue, int ctrl);
  305. static uint32_t virtio_net_get_features(VirtIODevice *vdev, uint32_t features)
  306. {
  307. VirtIONet *n = to_virtio_net(vdev);
  308. NetClientState *nc = qemu_get_queue(n->nic);
  309. features |= (1 << VIRTIO_NET_F_MAC);
  310. if (!peer_has_vnet_hdr(n)) {
  311. features &= ~(0x1 << VIRTIO_NET_F_CSUM);
  312. features &= ~(0x1 << VIRTIO_NET_F_HOST_TSO4);
  313. features &= ~(0x1 << VIRTIO_NET_F_HOST_TSO6);
  314. features &= ~(0x1 << VIRTIO_NET_F_HOST_ECN);
  315. features &= ~(0x1 << VIRTIO_NET_F_GUEST_CSUM);
  316. features &= ~(0x1 << VIRTIO_NET_F_GUEST_TSO4);
  317. features &= ~(0x1 << VIRTIO_NET_F_GUEST_TSO6);
  318. features &= ~(0x1 << VIRTIO_NET_F_GUEST_ECN);
  319. }
  320. if (!peer_has_vnet_hdr(n) || !peer_has_ufo(n)) {
  321. features &= ~(0x1 << VIRTIO_NET_F_GUEST_UFO);
  322. features &= ~(0x1 << VIRTIO_NET_F_HOST_UFO);
  323. }
  324. if (!nc->peer || nc->peer->info->type != NET_CLIENT_OPTIONS_KIND_TAP) {
  325. return features;
  326. }
  327. if (!tap_get_vhost_net(nc->peer)) {
  328. return features;
  329. }
  330. return vhost_net_get_features(tap_get_vhost_net(nc->peer), features);
  331. }
  332. static uint32_t virtio_net_bad_features(VirtIODevice *vdev)
  333. {
  334. uint32_t features = 0;
  335. /* Linux kernel 2.6.25. It understood MAC (as everyone must),
  336. * but also these: */
  337. features |= (1 << VIRTIO_NET_F_MAC);
  338. features |= (1 << VIRTIO_NET_F_CSUM);
  339. features |= (1 << VIRTIO_NET_F_HOST_TSO4);
  340. features |= (1 << VIRTIO_NET_F_HOST_TSO6);
  341. features |= (1 << VIRTIO_NET_F_HOST_ECN);
  342. return features;
  343. }
  344. static void virtio_net_set_features(VirtIODevice *vdev, uint32_t features)
  345. {
  346. VirtIONet *n = to_virtio_net(vdev);
  347. int i;
  348. virtio_net_set_multiqueue(n, !!(features & (1 << VIRTIO_NET_F_MQ)),
  349. !!(features & (1 << VIRTIO_NET_F_CTRL_VQ)));
  350. virtio_net_set_mrg_rx_bufs(n, !!(features & (1 << VIRTIO_NET_F_MRG_RXBUF)));
  351. if (n->has_vnet_hdr) {
  352. tap_set_offload(qemu_get_subqueue(n->nic, 0)->peer,
  353. (features >> VIRTIO_NET_F_GUEST_CSUM) & 1,
  354. (features >> VIRTIO_NET_F_GUEST_TSO4) & 1,
  355. (features >> VIRTIO_NET_F_GUEST_TSO6) & 1,
  356. (features >> VIRTIO_NET_F_GUEST_ECN) & 1,
  357. (features >> VIRTIO_NET_F_GUEST_UFO) & 1);
  358. }
  359. for (i = 0; i < n->max_queues; i++) {
  360. NetClientState *nc = qemu_get_subqueue(n->nic, i);
  361. if (!nc->peer || nc->peer->info->type != NET_CLIENT_OPTIONS_KIND_TAP) {
  362. continue;
  363. }
  364. if (!tap_get_vhost_net(nc->peer)) {
  365. continue;
  366. }
  367. vhost_net_ack_features(tap_get_vhost_net(nc->peer), features);
  368. }
  369. }
  370. static int virtio_net_handle_rx_mode(VirtIONet *n, uint8_t cmd,
  371. struct iovec *iov, unsigned int iov_cnt)
  372. {
  373. uint8_t on;
  374. size_t s;
  375. s = iov_to_buf(iov, iov_cnt, 0, &on, sizeof(on));
  376. if (s != sizeof(on)) {
  377. return VIRTIO_NET_ERR;
  378. }
  379. if (cmd == VIRTIO_NET_CTRL_RX_PROMISC) {
  380. n->promisc = on;
  381. } else if (cmd == VIRTIO_NET_CTRL_RX_ALLMULTI) {
  382. n->allmulti = on;
  383. } else if (cmd == VIRTIO_NET_CTRL_RX_ALLUNI) {
  384. n->alluni = on;
  385. } else if (cmd == VIRTIO_NET_CTRL_RX_NOMULTI) {
  386. n->nomulti = on;
  387. } else if (cmd == VIRTIO_NET_CTRL_RX_NOUNI) {
  388. n->nouni = on;
  389. } else if (cmd == VIRTIO_NET_CTRL_RX_NOBCAST) {
  390. n->nobcast = on;
  391. } else {
  392. return VIRTIO_NET_ERR;
  393. }
  394. return VIRTIO_NET_OK;
  395. }
  396. static int virtio_net_handle_mac(VirtIONet *n, uint8_t cmd,
  397. struct iovec *iov, unsigned int iov_cnt)
  398. {
  399. struct virtio_net_ctrl_mac mac_data;
  400. size_t s;
  401. if (cmd == VIRTIO_NET_CTRL_MAC_ADDR_SET) {
  402. if (iov_size(iov, iov_cnt) != sizeof(n->mac)) {
  403. return VIRTIO_NET_ERR;
  404. }
  405. s = iov_to_buf(iov, iov_cnt, 0, &n->mac, sizeof(n->mac));
  406. assert(s == sizeof(n->mac));
  407. qemu_format_nic_info_str(qemu_get_queue(n->nic), n->mac);
  408. return VIRTIO_NET_OK;
  409. }
  410. if (cmd != VIRTIO_NET_CTRL_MAC_TABLE_SET) {
  411. return VIRTIO_NET_ERR;
  412. }
  413. n->mac_table.in_use = 0;
  414. n->mac_table.first_multi = 0;
  415. n->mac_table.uni_overflow = 0;
  416. n->mac_table.multi_overflow = 0;
  417. memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
  418. s = iov_to_buf(iov, iov_cnt, 0, &mac_data.entries,
  419. sizeof(mac_data.entries));
  420. mac_data.entries = ldl_p(&mac_data.entries);
  421. if (s != sizeof(mac_data.entries)) {
  422. return VIRTIO_NET_ERR;
  423. }
  424. iov_discard_front(&iov, &iov_cnt, s);
  425. if (mac_data.entries * ETH_ALEN > iov_size(iov, iov_cnt)) {
  426. return VIRTIO_NET_ERR;
  427. }
  428. if (mac_data.entries <= MAC_TABLE_ENTRIES) {
  429. s = iov_to_buf(iov, iov_cnt, 0, n->mac_table.macs,
  430. mac_data.entries * ETH_ALEN);
  431. if (s != mac_data.entries * ETH_ALEN) {
  432. return VIRTIO_NET_ERR;
  433. }
  434. n->mac_table.in_use += mac_data.entries;
  435. } else {
  436. n->mac_table.uni_overflow = 1;
  437. }
  438. iov_discard_front(&iov, &iov_cnt, mac_data.entries * ETH_ALEN);
  439. n->mac_table.first_multi = n->mac_table.in_use;
  440. s = iov_to_buf(iov, iov_cnt, 0, &mac_data.entries,
  441. sizeof(mac_data.entries));
  442. mac_data.entries = ldl_p(&mac_data.entries);
  443. if (s != sizeof(mac_data.entries)) {
  444. return VIRTIO_NET_ERR;
  445. }
  446. iov_discard_front(&iov, &iov_cnt, s);
  447. if (mac_data.entries * ETH_ALEN != iov_size(iov, iov_cnt)) {
  448. return VIRTIO_NET_ERR;
  449. }
  450. if (n->mac_table.in_use + mac_data.entries <= MAC_TABLE_ENTRIES) {
  451. s = iov_to_buf(iov, iov_cnt, 0, n->mac_table.macs,
  452. mac_data.entries * ETH_ALEN);
  453. if (s != mac_data.entries * ETH_ALEN) {
  454. return VIRTIO_NET_ERR;
  455. }
  456. n->mac_table.in_use += mac_data.entries;
  457. } else {
  458. n->mac_table.multi_overflow = 1;
  459. }
  460. return VIRTIO_NET_OK;
  461. }
  462. static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd,
  463. struct iovec *iov, unsigned int iov_cnt)
  464. {
  465. uint16_t vid;
  466. size_t s;
  467. s = iov_to_buf(iov, iov_cnt, 0, &vid, sizeof(vid));
  468. vid = lduw_p(&vid);
  469. if (s != sizeof(vid)) {
  470. return VIRTIO_NET_ERR;
  471. }
  472. if (vid >= MAX_VLAN)
  473. return VIRTIO_NET_ERR;
  474. if (cmd == VIRTIO_NET_CTRL_VLAN_ADD)
  475. n->vlans[vid >> 5] |= (1U << (vid & 0x1f));
  476. else if (cmd == VIRTIO_NET_CTRL_VLAN_DEL)
  477. n->vlans[vid >> 5] &= ~(1U << (vid & 0x1f));
  478. else
  479. return VIRTIO_NET_ERR;
  480. return VIRTIO_NET_OK;
  481. }
  482. static int virtio_net_handle_mq(VirtIONet *n, uint8_t cmd,
  483. VirtQueueElement *elem)
  484. {
  485. struct virtio_net_ctrl_mq s;
  486. if (elem->out_num != 2 ||
  487. elem->out_sg[1].iov_len != sizeof(struct virtio_net_ctrl_mq)) {
  488. error_report("virtio-net ctrl invalid steering command");
  489. return VIRTIO_NET_ERR;
  490. }
  491. if (cmd != VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET) {
  492. return VIRTIO_NET_ERR;
  493. }
  494. memcpy(&s, elem->out_sg[1].iov_base, sizeof(struct virtio_net_ctrl_mq));
  495. if (s.virtqueue_pairs < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN ||
  496. s.virtqueue_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX ||
  497. s.virtqueue_pairs > n->max_queues ||
  498. !n->multiqueue) {
  499. return VIRTIO_NET_ERR;
  500. }
  501. n->curr_queues = s.virtqueue_pairs;
  502. /* stop the backend before changing the number of queues to avoid handling a
  503. * disabled queue */
  504. virtio_net_set_status(&n->vdev, n->vdev.status);
  505. virtio_net_set_queues(n);
  506. return VIRTIO_NET_OK;
  507. }
  508. static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
  509. {
  510. VirtIONet *n = to_virtio_net(vdev);
  511. struct virtio_net_ctrl_hdr ctrl;
  512. virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
  513. VirtQueueElement elem;
  514. size_t s;
  515. struct iovec *iov;
  516. unsigned int iov_cnt;
  517. while (virtqueue_pop(vq, &elem)) {
  518. if (iov_size(elem.in_sg, elem.in_num) < sizeof(status) ||
  519. iov_size(elem.out_sg, elem.out_num) < sizeof(ctrl)) {
  520. error_report("virtio-net ctrl missing headers");
  521. exit(1);
  522. }
  523. iov = elem.out_sg;
  524. iov_cnt = elem.out_num;
  525. s = iov_to_buf(iov, iov_cnt, 0, &ctrl, sizeof(ctrl));
  526. iov_discard_front(&iov, &iov_cnt, sizeof(ctrl));
  527. if (s != sizeof(ctrl)) {
  528. status = VIRTIO_NET_ERR;
  529. } else if (ctrl.class == VIRTIO_NET_CTRL_RX) {
  530. status = virtio_net_handle_rx_mode(n, ctrl.cmd, iov, iov_cnt);
  531. } else if (ctrl.class == VIRTIO_NET_CTRL_MAC) {
  532. status = virtio_net_handle_mac(n, ctrl.cmd, iov, iov_cnt);
  533. } else if (ctrl.class == VIRTIO_NET_CTRL_VLAN) {
  534. status = virtio_net_handle_vlan_table(n, ctrl.cmd, iov, iov_cnt);
  535. } else if (ctrl.class == VIRTIO_NET_CTRL_MQ) {
  536. status = virtio_net_handle_mq(n, ctrl.cmd, &elem);
  537. }
  538. s = iov_from_buf(elem.in_sg, elem.in_num, 0, &status, sizeof(status));
  539. assert(s == sizeof(status));
  540. virtqueue_push(vq, &elem, sizeof(status));
  541. virtio_notify(vdev, vq);
  542. }
  543. }
  544. /* RX */
  545. static void virtio_net_handle_rx(VirtIODevice *vdev, VirtQueue *vq)
  546. {
  547. VirtIONet *n = to_virtio_net(vdev);
  548. int queue_index = vq2q(virtio_get_queue_index(vq));
  549. qemu_flush_queued_packets(qemu_get_subqueue(n->nic, queue_index));
  550. }
  551. static int virtio_net_can_receive(NetClientState *nc)
  552. {
  553. VirtIONet *n = qemu_get_nic_opaque(nc);
  554. VirtIONetQueue *q = virtio_net_get_subqueue(nc);
  555. if (!n->vdev.vm_running) {
  556. return 0;
  557. }
  558. if (nc->queue_index >= n->curr_queues) {
  559. return 0;
  560. }
  561. if (!virtio_queue_ready(q->rx_vq) ||
  562. !(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK)) {
  563. return 0;
  564. }
  565. return 1;
  566. }
  567. static int virtio_net_has_buffers(VirtIONetQueue *q, int bufsize)
  568. {
  569. VirtIONet *n = q->n;
  570. if (virtio_queue_empty(q->rx_vq) ||
  571. (n->mergeable_rx_bufs &&
  572. !virtqueue_avail_bytes(q->rx_vq, bufsize, 0))) {
  573. virtio_queue_set_notification(q->rx_vq, 1);
  574. /* To avoid a race condition where the guest has made some buffers
  575. * available after the above check but before notification was
  576. * enabled, check for available buffers again.
  577. */
  578. if (virtio_queue_empty(q->rx_vq) ||
  579. (n->mergeable_rx_bufs &&
  580. !virtqueue_avail_bytes(q->rx_vq, bufsize, 0))) {
  581. return 0;
  582. }
  583. }
  584. virtio_queue_set_notification(q->rx_vq, 0);
  585. return 1;
  586. }
  587. /* dhclient uses AF_PACKET but doesn't pass auxdata to the kernel so
  588. * it never finds out that the packets don't have valid checksums. This
  589. * causes dhclient to get upset. Fedora's carried a patch for ages to
  590. * fix this with Xen but it hasn't appeared in an upstream release of
  591. * dhclient yet.
  592. *
  593. * To avoid breaking existing guests, we catch udp packets and add
  594. * checksums. This is terrible but it's better than hacking the guest
  595. * kernels.
  596. *
  597. * N.B. if we introduce a zero-copy API, this operation is no longer free so
  598. * we should provide a mechanism to disable it to avoid polluting the host
  599. * cache.
  600. */
  601. static void work_around_broken_dhclient(struct virtio_net_hdr *hdr,
  602. uint8_t *buf, size_t size)
  603. {
  604. if ((hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && /* missing csum */
  605. (size > 27 && size < 1500) && /* normal sized MTU */
  606. (buf[12] == 0x08 && buf[13] == 0x00) && /* ethertype == IPv4 */
  607. (buf[23] == 17) && /* ip.protocol == UDP */
  608. (buf[34] == 0 && buf[35] == 67)) { /* udp.srcport == bootps */
  609. net_checksum_calculate(buf, size);
  610. hdr->flags &= ~VIRTIO_NET_HDR_F_NEEDS_CSUM;
  611. }
  612. }
  613. static void receive_header(VirtIONet *n, const struct iovec *iov, int iov_cnt,
  614. const void *buf, size_t size)
  615. {
  616. if (n->has_vnet_hdr) {
  617. /* FIXME this cast is evil */
  618. void *wbuf = (void *)buf;
  619. work_around_broken_dhclient(wbuf, wbuf + n->host_hdr_len,
  620. size - n->host_hdr_len);
  621. iov_from_buf(iov, iov_cnt, 0, buf, sizeof(struct virtio_net_hdr));
  622. } else {
  623. struct virtio_net_hdr hdr = {
  624. .flags = 0,
  625. .gso_type = VIRTIO_NET_HDR_GSO_NONE
  626. };
  627. iov_from_buf(iov, iov_cnt, 0, &hdr, sizeof hdr);
  628. }
  629. }
  630. static int receive_filter(VirtIONet *n, const uint8_t *buf, int size)
  631. {
  632. static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
  633. static const uint8_t vlan[] = {0x81, 0x00};
  634. uint8_t *ptr = (uint8_t *)buf;
  635. int i;
  636. if (n->promisc)
  637. return 1;
  638. ptr += n->host_hdr_len;
  639. if (!memcmp(&ptr[12], vlan, sizeof(vlan))) {
  640. int vid = be16_to_cpup((uint16_t *)(ptr + 14)) & 0xfff;
  641. if (!(n->vlans[vid >> 5] & (1U << (vid & 0x1f))))
  642. return 0;
  643. }
  644. if (ptr[0] & 1) { // multicast
  645. if (!memcmp(ptr, bcast, sizeof(bcast))) {
  646. return !n->nobcast;
  647. } else if (n->nomulti) {
  648. return 0;
  649. } else if (n->allmulti || n->mac_table.multi_overflow) {
  650. return 1;
  651. }
  652. for (i = n->mac_table.first_multi; i < n->mac_table.in_use; i++) {
  653. if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
  654. return 1;
  655. }
  656. }
  657. } else { // unicast
  658. if (n->nouni) {
  659. return 0;
  660. } else if (n->alluni || n->mac_table.uni_overflow) {
  661. return 1;
  662. } else if (!memcmp(ptr, n->mac, ETH_ALEN)) {
  663. return 1;
  664. }
  665. for (i = 0; i < n->mac_table.first_multi; i++) {
  666. if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
  667. return 1;
  668. }
  669. }
  670. }
  671. return 0;
  672. }
  673. static ssize_t virtio_net_receive(NetClientState *nc, const uint8_t *buf, size_t size)
  674. {
  675. VirtIONet *n = qemu_get_nic_opaque(nc);
  676. VirtIONetQueue *q = virtio_net_get_subqueue(nc);
  677. struct iovec mhdr_sg[VIRTQUEUE_MAX_SIZE];
  678. struct virtio_net_hdr_mrg_rxbuf mhdr;
  679. unsigned mhdr_cnt = 0;
  680. size_t offset, i, guest_offset;
  681. if (!virtio_net_can_receive(nc)) {
  682. return -1;
  683. }
  684. /* hdr_len refers to the header we supply to the guest */
  685. if (!virtio_net_has_buffers(q, size + n->guest_hdr_len - n->host_hdr_len)) {
  686. return 0;
  687. }
  688. if (!receive_filter(n, buf, size))
  689. return size;
  690. offset = i = 0;
  691. while (offset < size) {
  692. VirtQueueElement elem;
  693. int len, total;
  694. const struct iovec *sg = elem.in_sg;
  695. total = 0;
  696. if (virtqueue_pop(q->rx_vq, &elem) == 0) {
  697. if (i == 0)
  698. return -1;
  699. error_report("virtio-net unexpected empty queue: "
  700. "i %zd mergeable %d offset %zd, size %zd, "
  701. "guest hdr len %zd, host hdr len %zd guest features 0x%x",
  702. i, n->mergeable_rx_bufs, offset, size,
  703. n->guest_hdr_len, n->host_hdr_len, n->vdev.guest_features);
  704. exit(1);
  705. }
  706. if (elem.in_num < 1) {
  707. error_report("virtio-net receive queue contains no in buffers");
  708. exit(1);
  709. }
  710. if (i == 0) {
  711. assert(offset == 0);
  712. if (n->mergeable_rx_bufs) {
  713. mhdr_cnt = iov_copy(mhdr_sg, ARRAY_SIZE(mhdr_sg),
  714. sg, elem.in_num,
  715. offsetof(typeof(mhdr), num_buffers),
  716. sizeof(mhdr.num_buffers));
  717. }
  718. receive_header(n, sg, elem.in_num, buf, size);
  719. offset = n->host_hdr_len;
  720. total += n->guest_hdr_len;
  721. guest_offset = n->guest_hdr_len;
  722. } else {
  723. guest_offset = 0;
  724. }
  725. /* copy in packet. ugh */
  726. len = iov_from_buf(sg, elem.in_num, guest_offset,
  727. buf + offset, size - offset);
  728. total += len;
  729. offset += len;
  730. /* If buffers can't be merged, at this point we
  731. * must have consumed the complete packet.
  732. * Otherwise, drop it. */
  733. if (!n->mergeable_rx_bufs && offset < size) {
  734. #if 0
  735. error_report("virtio-net truncated non-mergeable packet: "
  736. "i %zd mergeable %d offset %zd, size %zd, "
  737. "guest hdr len %zd, host hdr len %zd",
  738. i, n->mergeable_rx_bufs,
  739. offset, size, n->guest_hdr_len, n->host_hdr_len);
  740. #endif
  741. return size;
  742. }
  743. /* signal other side */
  744. virtqueue_fill(q->rx_vq, &elem, total, i++);
  745. }
  746. if (mhdr_cnt) {
  747. stw_p(&mhdr.num_buffers, i);
  748. iov_from_buf(mhdr_sg, mhdr_cnt,
  749. 0,
  750. &mhdr.num_buffers, sizeof mhdr.num_buffers);
  751. }
  752. virtqueue_flush(q->rx_vq, i);
  753. virtio_notify(&n->vdev, q->rx_vq);
  754. return size;
  755. }
  756. static int32_t virtio_net_flush_tx(VirtIONetQueue *q);
  757. static void virtio_net_tx_complete(NetClientState *nc, ssize_t len)
  758. {
  759. VirtIONet *n = qemu_get_nic_opaque(nc);
  760. VirtIONetQueue *q = virtio_net_get_subqueue(nc);
  761. virtqueue_push(q->tx_vq, &q->async_tx.elem, 0);
  762. virtio_notify(&n->vdev, q->tx_vq);
  763. q->async_tx.elem.out_num = q->async_tx.len = 0;
  764. virtio_queue_set_notification(q->tx_vq, 1);
  765. virtio_net_flush_tx(q);
  766. }
  767. /* TX */
  768. static int32_t virtio_net_flush_tx(VirtIONetQueue *q)
  769. {
  770. VirtIONet *n = q->n;
  771. VirtQueueElement elem;
  772. int32_t num_packets = 0;
  773. int queue_index = vq2q(virtio_get_queue_index(q->tx_vq));
  774. if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK)) {
  775. return num_packets;
  776. }
  777. assert(n->vdev.vm_running);
  778. if (q->async_tx.elem.out_num) {
  779. virtio_queue_set_notification(q->tx_vq, 0);
  780. return num_packets;
  781. }
  782. while (virtqueue_pop(q->tx_vq, &elem)) {
  783. ssize_t ret, len;
  784. unsigned int out_num = elem.out_num;
  785. struct iovec *out_sg = &elem.out_sg[0];
  786. struct iovec sg[VIRTQUEUE_MAX_SIZE];
  787. if (out_num < 1) {
  788. error_report("virtio-net header not in first element");
  789. exit(1);
  790. }
  791. /*
  792. * If host wants to see the guest header as is, we can
  793. * pass it on unchanged. Otherwise, copy just the parts
  794. * that host is interested in.
  795. */
  796. assert(n->host_hdr_len <= n->guest_hdr_len);
  797. if (n->host_hdr_len != n->guest_hdr_len) {
  798. unsigned sg_num = iov_copy(sg, ARRAY_SIZE(sg),
  799. out_sg, out_num,
  800. 0, n->host_hdr_len);
  801. sg_num += iov_copy(sg + sg_num, ARRAY_SIZE(sg) - sg_num,
  802. out_sg, out_num,
  803. n->guest_hdr_len, -1);
  804. out_num = sg_num;
  805. out_sg = sg;
  806. }
  807. len = n->guest_hdr_len;
  808. ret = qemu_sendv_packet_async(qemu_get_subqueue(n->nic, queue_index),
  809. out_sg, out_num, virtio_net_tx_complete);
  810. if (ret == 0) {
  811. virtio_queue_set_notification(q->tx_vq, 0);
  812. q->async_tx.elem = elem;
  813. q->async_tx.len = len;
  814. return -EBUSY;
  815. }
  816. len += ret;
  817. virtqueue_push(q->tx_vq, &elem, 0);
  818. virtio_notify(&n->vdev, q->tx_vq);
  819. if (++num_packets >= n->tx_burst) {
  820. break;
  821. }
  822. }
  823. return num_packets;
  824. }
  825. static void virtio_net_handle_tx_timer(VirtIODevice *vdev, VirtQueue *vq)
  826. {
  827. VirtIONet *n = to_virtio_net(vdev);
  828. VirtIONetQueue *q = &n->vqs[vq2q(virtio_get_queue_index(vq))];
  829. /* This happens when device was stopped but VCPU wasn't. */
  830. if (!n->vdev.vm_running) {
  831. q->tx_waiting = 1;
  832. return;
  833. }
  834. if (q->tx_waiting) {
  835. virtio_queue_set_notification(vq, 1);
  836. qemu_del_timer(q->tx_timer);
  837. q->tx_waiting = 0;
  838. virtio_net_flush_tx(q);
  839. } else {
  840. qemu_mod_timer(q->tx_timer,
  841. qemu_get_clock_ns(vm_clock) + n->tx_timeout);
  842. q->tx_waiting = 1;
  843. virtio_queue_set_notification(vq, 0);
  844. }
  845. }
  846. static void virtio_net_handle_tx_bh(VirtIODevice *vdev, VirtQueue *vq)
  847. {
  848. VirtIONet *n = to_virtio_net(vdev);
  849. VirtIONetQueue *q = &n->vqs[vq2q(virtio_get_queue_index(vq))];
  850. if (unlikely(q->tx_waiting)) {
  851. return;
  852. }
  853. q->tx_waiting = 1;
  854. /* This happens when device was stopped but VCPU wasn't. */
  855. if (!n->vdev.vm_running) {
  856. return;
  857. }
  858. virtio_queue_set_notification(vq, 0);
  859. qemu_bh_schedule(q->tx_bh);
  860. }
  861. static void virtio_net_tx_timer(void *opaque)
  862. {
  863. VirtIONetQueue *q = opaque;
  864. VirtIONet *n = q->n;
  865. assert(n->vdev.vm_running);
  866. q->tx_waiting = 0;
  867. /* Just in case the driver is not ready on more */
  868. if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
  869. return;
  870. virtio_queue_set_notification(q->tx_vq, 1);
  871. virtio_net_flush_tx(q);
  872. }
  873. static void virtio_net_tx_bh(void *opaque)
  874. {
  875. VirtIONetQueue *q = opaque;
  876. VirtIONet *n = q->n;
  877. int32_t ret;
  878. assert(n->vdev.vm_running);
  879. q->tx_waiting = 0;
  880. /* Just in case the driver is not ready on more */
  881. if (unlikely(!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK)))
  882. return;
  883. ret = virtio_net_flush_tx(q);
  884. if (ret == -EBUSY) {
  885. return; /* Notification re-enable handled by tx_complete */
  886. }
  887. /* If we flush a full burst of packets, assume there are
  888. * more coming and immediately reschedule */
  889. if (ret >= n->tx_burst) {
  890. qemu_bh_schedule(q->tx_bh);
  891. q->tx_waiting = 1;
  892. return;
  893. }
  894. /* If less than a full burst, re-enable notification and flush
  895. * anything that may have come in while we weren't looking. If
  896. * we find something, assume the guest is still active and reschedule */
  897. virtio_queue_set_notification(q->tx_vq, 1);
  898. if (virtio_net_flush_tx(q) > 0) {
  899. virtio_queue_set_notification(q->tx_vq, 0);
  900. qemu_bh_schedule(q->tx_bh);
  901. q->tx_waiting = 1;
  902. }
  903. }
  904. static void virtio_net_set_multiqueue(VirtIONet *n, int multiqueue, int ctrl)
  905. {
  906. VirtIODevice *vdev = &n->vdev;
  907. int i, max = multiqueue ? n->max_queues : 1;
  908. n->multiqueue = multiqueue;
  909. for (i = 2; i <= n->max_queues * 2 + 1; i++) {
  910. virtio_del_queue(vdev, i);
  911. }
  912. for (i = 1; i < max; i++) {
  913. n->vqs[i].rx_vq = virtio_add_queue(vdev, 256, virtio_net_handle_rx);
  914. if (n->vqs[i].tx_timer) {
  915. n->vqs[i].tx_vq =
  916. virtio_add_queue(vdev, 256, virtio_net_handle_tx_timer);
  917. n->vqs[i].tx_timer = qemu_new_timer_ns(vm_clock,
  918. virtio_net_tx_timer,
  919. &n->vqs[i]);
  920. } else {
  921. n->vqs[i].tx_vq =
  922. virtio_add_queue(vdev, 256, virtio_net_handle_tx_bh);
  923. n->vqs[i].tx_bh = qemu_bh_new(virtio_net_tx_bh, &n->vqs[i]);
  924. }
  925. n->vqs[i].tx_waiting = 0;
  926. n->vqs[i].n = n;
  927. }
  928. if (ctrl) {
  929. n->ctrl_vq = virtio_add_queue(vdev, 64, virtio_net_handle_ctrl);
  930. }
  931. virtio_net_set_queues(n);
  932. }
  933. static void virtio_net_save(QEMUFile *f, void *opaque)
  934. {
  935. int i;
  936. VirtIONet *n = opaque;
  937. /* At this point, backend must be stopped, otherwise
  938. * it might keep writing to memory. */
  939. assert(!n->vhost_started);
  940. virtio_save(&n->vdev, f);
  941. qemu_put_buffer(f, n->mac, ETH_ALEN);
  942. qemu_put_be32(f, n->vqs[0].tx_waiting);
  943. qemu_put_be32(f, n->mergeable_rx_bufs);
  944. qemu_put_be16(f, n->status);
  945. qemu_put_byte(f, n->promisc);
  946. qemu_put_byte(f, n->allmulti);
  947. qemu_put_be32(f, n->mac_table.in_use);
  948. qemu_put_buffer(f, n->mac_table.macs, n->mac_table.in_use * ETH_ALEN);
  949. qemu_put_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
  950. qemu_put_be32(f, n->has_vnet_hdr);
  951. qemu_put_byte(f, n->mac_table.multi_overflow);
  952. qemu_put_byte(f, n->mac_table.uni_overflow);
  953. qemu_put_byte(f, n->alluni);
  954. qemu_put_byte(f, n->nomulti);
  955. qemu_put_byte(f, n->nouni);
  956. qemu_put_byte(f, n->nobcast);
  957. qemu_put_byte(f, n->has_ufo);
  958. if (n->max_queues > 1) {
  959. qemu_put_be16(f, n->max_queues);
  960. qemu_put_be16(f, n->curr_queues);
  961. for (i = 1; i < n->curr_queues; i++) {
  962. qemu_put_be32(f, n->vqs[i].tx_waiting);
  963. }
  964. }
  965. }
  966. static int virtio_net_load(QEMUFile *f, void *opaque, int version_id)
  967. {
  968. VirtIONet *n = opaque;
  969. int ret, i, link_down;
  970. if (version_id < 2 || version_id > VIRTIO_NET_VM_VERSION)
  971. return -EINVAL;
  972. ret = virtio_load(&n->vdev, f);
  973. if (ret) {
  974. return ret;
  975. }
  976. qemu_get_buffer(f, n->mac, ETH_ALEN);
  977. n->vqs[0].tx_waiting = qemu_get_be32(f);
  978. virtio_net_set_mrg_rx_bufs(n, qemu_get_be32(f));
  979. if (version_id >= 3)
  980. n->status = qemu_get_be16(f);
  981. if (version_id >= 4) {
  982. if (version_id < 8) {
  983. n->promisc = qemu_get_be32(f);
  984. n->allmulti = qemu_get_be32(f);
  985. } else {
  986. n->promisc = qemu_get_byte(f);
  987. n->allmulti = qemu_get_byte(f);
  988. }
  989. }
  990. if (version_id >= 5) {
  991. n->mac_table.in_use = qemu_get_be32(f);
  992. /* MAC_TABLE_ENTRIES may be different from the saved image */
  993. if (n->mac_table.in_use <= MAC_TABLE_ENTRIES) {
  994. qemu_get_buffer(f, n->mac_table.macs,
  995. n->mac_table.in_use * ETH_ALEN);
  996. } else if (n->mac_table.in_use) {
  997. uint8_t *buf = g_malloc0(n->mac_table.in_use);
  998. qemu_get_buffer(f, buf, n->mac_table.in_use * ETH_ALEN);
  999. g_free(buf);
  1000. n->mac_table.multi_overflow = n->mac_table.uni_overflow = 1;
  1001. n->mac_table.in_use = 0;
  1002. }
  1003. }
  1004. if (version_id >= 6)
  1005. qemu_get_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
  1006. if (version_id >= 7) {
  1007. if (qemu_get_be32(f) && !peer_has_vnet_hdr(n)) {
  1008. error_report("virtio-net: saved image requires vnet_hdr=on");
  1009. return -1;
  1010. }
  1011. if (n->has_vnet_hdr) {
  1012. tap_set_offload(qemu_get_queue(n->nic)->peer,
  1013. (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_CSUM) & 1,
  1014. (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_TSO4) & 1,
  1015. (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_TSO6) & 1,
  1016. (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_ECN) & 1,
  1017. (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_UFO) & 1);
  1018. }
  1019. }
  1020. if (version_id >= 9) {
  1021. n->mac_table.multi_overflow = qemu_get_byte(f);
  1022. n->mac_table.uni_overflow = qemu_get_byte(f);
  1023. }
  1024. if (version_id >= 10) {
  1025. n->alluni = qemu_get_byte(f);
  1026. n->nomulti = qemu_get_byte(f);
  1027. n->nouni = qemu_get_byte(f);
  1028. n->nobcast = qemu_get_byte(f);
  1029. }
  1030. if (version_id >= 11) {
  1031. if (qemu_get_byte(f) && !peer_has_ufo(n)) {
  1032. error_report("virtio-net: saved image requires TUN_F_UFO support");
  1033. return -1;
  1034. }
  1035. }
  1036. if (n->max_queues > 1) {
  1037. if (n->max_queues != qemu_get_be16(f)) {
  1038. error_report("virtio-net: different max_queues ");
  1039. return -1;
  1040. }
  1041. n->curr_queues = qemu_get_be16(f);
  1042. for (i = 1; i < n->curr_queues; i++) {
  1043. n->vqs[i].tx_waiting = qemu_get_be32(f);
  1044. }
  1045. }
  1046. virtio_net_set_queues(n);
  1047. /* Find the first multicast entry in the saved MAC filter */
  1048. for (i = 0; i < n->mac_table.in_use; i++) {
  1049. if (n->mac_table.macs[i * ETH_ALEN] & 1) {
  1050. break;
  1051. }
  1052. }
  1053. n->mac_table.first_multi = i;
  1054. /* nc.link_down can't be migrated, so infer link_down according
  1055. * to link status bit in n->status */
  1056. link_down = (n->status & VIRTIO_NET_S_LINK_UP) == 0;
  1057. for (i = 0; i < n->max_queues; i++) {
  1058. qemu_get_subqueue(n->nic, i)->link_down = link_down;
  1059. }
  1060. return 0;
  1061. }
  1062. static void virtio_net_cleanup(NetClientState *nc)
  1063. {
  1064. VirtIONet *n = qemu_get_nic_opaque(nc);
  1065. n->nic = NULL;
  1066. }
  1067. static NetClientInfo net_virtio_info = {
  1068. .type = NET_CLIENT_OPTIONS_KIND_NIC,
  1069. .size = sizeof(NICState),
  1070. .can_receive = virtio_net_can_receive,
  1071. .receive = virtio_net_receive,
  1072. .cleanup = virtio_net_cleanup,
  1073. .link_status_changed = virtio_net_set_link_status,
  1074. };
  1075. static bool virtio_net_guest_notifier_pending(VirtIODevice *vdev, int idx)
  1076. {
  1077. VirtIONet *n = to_virtio_net(vdev);
  1078. NetClientState *nc = qemu_get_subqueue(n->nic, vq2q(idx));
  1079. assert(n->vhost_started);
  1080. return vhost_net_virtqueue_pending(tap_get_vhost_net(nc->peer), idx);
  1081. }
  1082. static void virtio_net_guest_notifier_mask(VirtIODevice *vdev, int idx,
  1083. bool mask)
  1084. {
  1085. VirtIONet *n = to_virtio_net(vdev);
  1086. NetClientState *nc = qemu_get_subqueue(n->nic, vq2q(idx));
  1087. assert(n->vhost_started);
  1088. vhost_net_virtqueue_mask(tap_get_vhost_net(nc->peer),
  1089. vdev, idx, mask);
  1090. }
  1091. VirtIODevice *virtio_net_init(DeviceState *dev, NICConf *conf,
  1092. virtio_net_conf *net, uint32_t host_features)
  1093. {
  1094. VirtIONet *n;
  1095. int i, config_size = 0;
  1096. for (i = 0; feature_sizes[i].flags != 0; i++) {
  1097. if (host_features & feature_sizes[i].flags) {
  1098. config_size = MAX(feature_sizes[i].end, config_size);
  1099. }
  1100. }
  1101. n = (VirtIONet *)virtio_common_init("virtio-net", VIRTIO_ID_NET,
  1102. config_size, sizeof(VirtIONet));
  1103. n->config_size = config_size;
  1104. n->vdev.get_config = virtio_net_get_config;
  1105. n->vdev.set_config = virtio_net_set_config;
  1106. n->vdev.get_features = virtio_net_get_features;
  1107. n->vdev.set_features = virtio_net_set_features;
  1108. n->vdev.bad_features = virtio_net_bad_features;
  1109. n->vdev.reset = virtio_net_reset;
  1110. n->vdev.set_status = virtio_net_set_status;
  1111. n->vdev.guest_notifier_mask = virtio_net_guest_notifier_mask;
  1112. n->vdev.guest_notifier_pending = virtio_net_guest_notifier_pending;
  1113. n->max_queues = MAX(conf->queues, 1);
  1114. n->vqs = g_malloc0(sizeof(VirtIONetQueue) * n->max_queues);
  1115. n->vqs[0].rx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_rx);
  1116. n->curr_queues = 1;
  1117. n->vqs[0].n = n;
  1118. n->tx_timeout = net->txtimer;
  1119. if (net->tx && strcmp(net->tx, "timer") && strcmp(net->tx, "bh")) {
  1120. error_report("virtio-net: "
  1121. "Unknown option tx=%s, valid options: \"timer\" \"bh\"",
  1122. net->tx);
  1123. error_report("Defaulting to \"bh\"");
  1124. }
  1125. if (net->tx && !strcmp(net->tx, "timer")) {
  1126. n->vqs[0].tx_vq = virtio_add_queue(&n->vdev, 256,
  1127. virtio_net_handle_tx_timer);
  1128. n->vqs[0].tx_timer = qemu_new_timer_ns(vm_clock, virtio_net_tx_timer,
  1129. &n->vqs[0]);
  1130. } else {
  1131. n->vqs[0].tx_vq = virtio_add_queue(&n->vdev, 256,
  1132. virtio_net_handle_tx_bh);
  1133. n->vqs[0].tx_bh = qemu_bh_new(virtio_net_tx_bh, &n->vqs[0]);
  1134. }
  1135. n->ctrl_vq = virtio_add_queue(&n->vdev, 64, virtio_net_handle_ctrl);
  1136. qemu_macaddr_default_if_unset(&conf->macaddr);
  1137. memcpy(&n->mac[0], &conf->macaddr, sizeof(n->mac));
  1138. n->status = VIRTIO_NET_S_LINK_UP;
  1139. n->nic = qemu_new_nic(&net_virtio_info, conf, object_get_typename(OBJECT(dev)), dev->id, n);
  1140. peer_test_vnet_hdr(n);
  1141. if (peer_has_vnet_hdr(n)) {
  1142. for (i = 0; i < n->max_queues; i++) {
  1143. tap_using_vnet_hdr(qemu_get_subqueue(n->nic, i)->peer, true);
  1144. }
  1145. n->host_hdr_len = sizeof(struct virtio_net_hdr);
  1146. } else {
  1147. n->host_hdr_len = 0;
  1148. }
  1149. qemu_format_nic_info_str(qemu_get_queue(n->nic), conf->macaddr.a);
  1150. n->vqs[0].tx_waiting = 0;
  1151. n->tx_burst = net->txburst;
  1152. virtio_net_set_mrg_rx_bufs(n, 0);
  1153. n->promisc = 1; /* for compatibility */
  1154. n->mac_table.macs = g_malloc0(MAC_TABLE_ENTRIES * ETH_ALEN);
  1155. n->vlans = g_malloc0(MAX_VLAN >> 3);
  1156. n->qdev = dev;
  1157. register_savevm(dev, "virtio-net", -1, VIRTIO_NET_VM_VERSION,
  1158. virtio_net_save, virtio_net_load, n);
  1159. add_boot_device_path(conf->bootindex, dev, "/ethernet-phy@0");
  1160. return &n->vdev;
  1161. }
  1162. void virtio_net_exit(VirtIODevice *vdev)
  1163. {
  1164. VirtIONet *n = DO_UPCAST(VirtIONet, vdev, vdev);
  1165. int i;
  1166. /* This will stop vhost backend if appropriate. */
  1167. virtio_net_set_status(vdev, 0);
  1168. unregister_savevm(n->qdev, "virtio-net", n);
  1169. g_free(n->mac_table.macs);
  1170. g_free(n->vlans);
  1171. for (i = 0; i < n->max_queues; i++) {
  1172. VirtIONetQueue *q = &n->vqs[i];
  1173. NetClientState *nc = qemu_get_subqueue(n->nic, i);
  1174. qemu_purge_queued_packets(nc);
  1175. if (q->tx_timer) {
  1176. qemu_del_timer(q->tx_timer);
  1177. qemu_free_timer(q->tx_timer);
  1178. } else {
  1179. qemu_bh_delete(q->tx_bh);
  1180. }
  1181. }
  1182. g_free(n->vqs);
  1183. qemu_del_nic(n->nic);
  1184. virtio_cleanup(&n->vdev);
  1185. }