virtio-net.c 50 KB

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