net.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594
  1. /* Copyright (C) 2009 Red Hat, Inc.
  2. * Author: Michael S. Tsirkin <mst@redhat.com>
  3. *
  4. * This work is licensed under the terms of the GNU GPL, version 2.
  5. *
  6. * virtio-net server in host kernel.
  7. */
  8. #include <linux/compat.h>
  9. #include <linux/eventfd.h>
  10. #include <linux/vhost.h>
  11. #include <linux/virtio_net.h>
  12. #include <linux/miscdevice.h>
  13. #include <linux/module.h>
  14. #include <linux/moduleparam.h>
  15. #include <linux/mutex.h>
  16. #include <linux/workqueue.h>
  17. #include <linux/file.h>
  18. #include <linux/slab.h>
  19. #include <linux/sched/clock.h>
  20. #include <linux/sched/signal.h>
  21. #include <linux/vmalloc.h>
  22. #include <linux/net.h>
  23. #include <linux/if_packet.h>
  24. #include <linux/if_arp.h>
  25. #include <linux/if_tun.h>
  26. #include <linux/if_macvlan.h>
  27. #include <linux/if_tap.h>
  28. #include <linux/if_vlan.h>
  29. #include <linux/skb_array.h>
  30. #include <linux/skbuff.h>
  31. #include <net/sock.h>
  32. #include <net/xdp.h>
  33. #include "vhost.h"
  34. static int experimental_zcopytx = 0;
  35. module_param(experimental_zcopytx, int, 0444);
  36. MODULE_PARM_DESC(experimental_zcopytx, "Enable Zero Copy TX;"
  37. " 1 -Enable; 0 - Disable");
  38. /* Max number of bytes transferred before requeueing the job.
  39. * Using this limit prevents one virtqueue from starving others. */
  40. #define VHOST_NET_WEIGHT 0x80000
  41. /* Max number of packets transferred before requeueing the job.
  42. * Using this limit prevents one virtqueue from starving others with small
  43. * pkts.
  44. */
  45. #define VHOST_NET_PKT_WEIGHT 256
  46. /* MAX number of TX used buffers for outstanding zerocopy */
  47. #define VHOST_MAX_PEND 128
  48. #define VHOST_GOODCOPY_LEN 256
  49. /*
  50. * For transmit, used buffer len is unused; we override it to track buffer
  51. * status internally; used for zerocopy tx only.
  52. */
  53. /* Lower device DMA failed */
  54. #define VHOST_DMA_FAILED_LEN ((__force __virtio32)3)
  55. /* Lower device DMA done */
  56. #define VHOST_DMA_DONE_LEN ((__force __virtio32)2)
  57. /* Lower device DMA in progress */
  58. #define VHOST_DMA_IN_PROGRESS ((__force __virtio32)1)
  59. /* Buffer unused */
  60. #define VHOST_DMA_CLEAR_LEN ((__force __virtio32)0)
  61. #define VHOST_DMA_IS_DONE(len) ((__force u32)(len) >= (__force u32)VHOST_DMA_DONE_LEN)
  62. enum {
  63. VHOST_NET_FEATURES = VHOST_FEATURES |
  64. (1ULL << VHOST_NET_F_VIRTIO_NET_HDR) |
  65. (1ULL << VIRTIO_NET_F_MRG_RXBUF) |
  66. (1ULL << VIRTIO_F_IOMMU_PLATFORM)
  67. };
  68. enum {
  69. VHOST_NET_BACKEND_FEATURES = (1ULL << VHOST_BACKEND_F_IOTLB_MSG_V2)
  70. };
  71. enum {
  72. VHOST_NET_VQ_RX = 0,
  73. VHOST_NET_VQ_TX = 1,
  74. VHOST_NET_VQ_MAX = 2,
  75. };
  76. struct vhost_net_ubuf_ref {
  77. /* refcount follows semantics similar to kref:
  78. * 0: object is released
  79. * 1: no outstanding ubufs
  80. * >1: outstanding ubufs
  81. */
  82. atomic_t refcount;
  83. wait_queue_head_t wait;
  84. struct vhost_virtqueue *vq;
  85. };
  86. #define VHOST_NET_BATCH 64
  87. struct vhost_net_buf {
  88. void **queue;
  89. int tail;
  90. int head;
  91. };
  92. struct vhost_net_virtqueue {
  93. struct vhost_virtqueue vq;
  94. size_t vhost_hlen;
  95. size_t sock_hlen;
  96. /* vhost zerocopy support fields below: */
  97. /* last used idx for outstanding DMA zerocopy buffers */
  98. int upend_idx;
  99. /* For TX, first used idx for DMA done zerocopy buffers
  100. * For RX, number of batched heads
  101. */
  102. int done_idx;
  103. /* an array of userspace buffers info */
  104. struct ubuf_info *ubuf_info;
  105. /* Reference counting for outstanding ubufs.
  106. * Protected by vq mutex. Writers must also take device mutex. */
  107. struct vhost_net_ubuf_ref *ubufs;
  108. struct ptr_ring *rx_ring;
  109. struct vhost_net_buf rxq;
  110. };
  111. struct vhost_net {
  112. struct vhost_dev dev;
  113. struct vhost_net_virtqueue vqs[VHOST_NET_VQ_MAX];
  114. struct vhost_poll poll[VHOST_NET_VQ_MAX];
  115. /* Number of TX recently submitted.
  116. * Protected by tx vq lock. */
  117. unsigned tx_packets;
  118. /* Number of times zerocopy TX recently failed.
  119. * Protected by tx vq lock. */
  120. unsigned tx_zcopy_err;
  121. /* Flush in progress. Protected by tx vq lock. */
  122. bool tx_flush;
  123. };
  124. static unsigned vhost_net_zcopy_mask __read_mostly;
  125. static void *vhost_net_buf_get_ptr(struct vhost_net_buf *rxq)
  126. {
  127. if (rxq->tail != rxq->head)
  128. return rxq->queue[rxq->head];
  129. else
  130. return NULL;
  131. }
  132. static int vhost_net_buf_get_size(struct vhost_net_buf *rxq)
  133. {
  134. return rxq->tail - rxq->head;
  135. }
  136. static int vhost_net_buf_is_empty(struct vhost_net_buf *rxq)
  137. {
  138. return rxq->tail == rxq->head;
  139. }
  140. static void *vhost_net_buf_consume(struct vhost_net_buf *rxq)
  141. {
  142. void *ret = vhost_net_buf_get_ptr(rxq);
  143. ++rxq->head;
  144. return ret;
  145. }
  146. static int vhost_net_buf_produce(struct vhost_net_virtqueue *nvq)
  147. {
  148. struct vhost_net_buf *rxq = &nvq->rxq;
  149. rxq->head = 0;
  150. rxq->tail = ptr_ring_consume_batched(nvq->rx_ring, rxq->queue,
  151. VHOST_NET_BATCH);
  152. return rxq->tail;
  153. }
  154. static void vhost_net_buf_unproduce(struct vhost_net_virtqueue *nvq)
  155. {
  156. struct vhost_net_buf *rxq = &nvq->rxq;
  157. if (nvq->rx_ring && !vhost_net_buf_is_empty(rxq)) {
  158. ptr_ring_unconsume(nvq->rx_ring, rxq->queue + rxq->head,
  159. vhost_net_buf_get_size(rxq),
  160. tun_ptr_free);
  161. rxq->head = rxq->tail = 0;
  162. }
  163. }
  164. static int vhost_net_buf_peek_len(void *ptr)
  165. {
  166. if (tun_is_xdp_frame(ptr)) {
  167. struct xdp_frame *xdpf = tun_ptr_to_xdp(ptr);
  168. return xdpf->len;
  169. }
  170. return __skb_array_len_with_tag(ptr);
  171. }
  172. static int vhost_net_buf_peek(struct vhost_net_virtqueue *nvq)
  173. {
  174. struct vhost_net_buf *rxq = &nvq->rxq;
  175. if (!vhost_net_buf_is_empty(rxq))
  176. goto out;
  177. if (!vhost_net_buf_produce(nvq))
  178. return 0;
  179. out:
  180. return vhost_net_buf_peek_len(vhost_net_buf_get_ptr(rxq));
  181. }
  182. static void vhost_net_buf_init(struct vhost_net_buf *rxq)
  183. {
  184. rxq->head = rxq->tail = 0;
  185. }
  186. static void vhost_net_enable_zcopy(int vq)
  187. {
  188. vhost_net_zcopy_mask |= 0x1 << vq;
  189. }
  190. static struct vhost_net_ubuf_ref *
  191. vhost_net_ubuf_alloc(struct vhost_virtqueue *vq, bool zcopy)
  192. {
  193. struct vhost_net_ubuf_ref *ubufs;
  194. /* No zero copy backend? Nothing to count. */
  195. if (!zcopy)
  196. return NULL;
  197. ubufs = kmalloc(sizeof(*ubufs), GFP_KERNEL);
  198. if (!ubufs)
  199. return ERR_PTR(-ENOMEM);
  200. atomic_set(&ubufs->refcount, 1);
  201. init_waitqueue_head(&ubufs->wait);
  202. ubufs->vq = vq;
  203. return ubufs;
  204. }
  205. static int vhost_net_ubuf_put(struct vhost_net_ubuf_ref *ubufs)
  206. {
  207. int r = atomic_sub_return(1, &ubufs->refcount);
  208. if (unlikely(!r))
  209. wake_up(&ubufs->wait);
  210. return r;
  211. }
  212. static void vhost_net_ubuf_put_and_wait(struct vhost_net_ubuf_ref *ubufs)
  213. {
  214. vhost_net_ubuf_put(ubufs);
  215. wait_event(ubufs->wait, !atomic_read(&ubufs->refcount));
  216. }
  217. static void vhost_net_ubuf_put_wait_and_free(struct vhost_net_ubuf_ref *ubufs)
  218. {
  219. vhost_net_ubuf_put_and_wait(ubufs);
  220. kfree(ubufs);
  221. }
  222. static void vhost_net_clear_ubuf_info(struct vhost_net *n)
  223. {
  224. int i;
  225. for (i = 0; i < VHOST_NET_VQ_MAX; ++i) {
  226. kfree(n->vqs[i].ubuf_info);
  227. n->vqs[i].ubuf_info = NULL;
  228. }
  229. }
  230. static int vhost_net_set_ubuf_info(struct vhost_net *n)
  231. {
  232. bool zcopy;
  233. int i;
  234. for (i = 0; i < VHOST_NET_VQ_MAX; ++i) {
  235. zcopy = vhost_net_zcopy_mask & (0x1 << i);
  236. if (!zcopy)
  237. continue;
  238. n->vqs[i].ubuf_info =
  239. kmalloc_array(UIO_MAXIOV,
  240. sizeof(*n->vqs[i].ubuf_info),
  241. GFP_KERNEL);
  242. if (!n->vqs[i].ubuf_info)
  243. goto err;
  244. }
  245. return 0;
  246. err:
  247. vhost_net_clear_ubuf_info(n);
  248. return -ENOMEM;
  249. }
  250. static void vhost_net_vq_reset(struct vhost_net *n)
  251. {
  252. int i;
  253. vhost_net_clear_ubuf_info(n);
  254. for (i = 0; i < VHOST_NET_VQ_MAX; i++) {
  255. n->vqs[i].done_idx = 0;
  256. n->vqs[i].upend_idx = 0;
  257. n->vqs[i].ubufs = NULL;
  258. n->vqs[i].vhost_hlen = 0;
  259. n->vqs[i].sock_hlen = 0;
  260. vhost_net_buf_init(&n->vqs[i].rxq);
  261. }
  262. }
  263. static void vhost_net_tx_packet(struct vhost_net *net)
  264. {
  265. ++net->tx_packets;
  266. if (net->tx_packets < 1024)
  267. return;
  268. net->tx_packets = 0;
  269. net->tx_zcopy_err = 0;
  270. }
  271. static void vhost_net_tx_err(struct vhost_net *net)
  272. {
  273. ++net->tx_zcopy_err;
  274. }
  275. static bool vhost_net_tx_select_zcopy(struct vhost_net *net)
  276. {
  277. /* TX flush waits for outstanding DMAs to be done.
  278. * Don't start new DMAs.
  279. */
  280. return !net->tx_flush &&
  281. net->tx_packets / 64 >= net->tx_zcopy_err;
  282. }
  283. static bool vhost_sock_zcopy(struct socket *sock)
  284. {
  285. return unlikely(experimental_zcopytx) &&
  286. sock_flag(sock->sk, SOCK_ZEROCOPY);
  287. }
  288. /* In case of DMA done not in order in lower device driver for some reason.
  289. * upend_idx is used to track end of used idx, done_idx is used to track head
  290. * of used idx. Once lower device DMA done contiguously, we will signal KVM
  291. * guest used idx.
  292. */
  293. static void vhost_zerocopy_signal_used(struct vhost_net *net,
  294. struct vhost_virtqueue *vq)
  295. {
  296. struct vhost_net_virtqueue *nvq =
  297. container_of(vq, struct vhost_net_virtqueue, vq);
  298. int i, add;
  299. int j = 0;
  300. for (i = nvq->done_idx; i != nvq->upend_idx; i = (i + 1) % UIO_MAXIOV) {
  301. if (vq->heads[i].len == VHOST_DMA_FAILED_LEN)
  302. vhost_net_tx_err(net);
  303. if (VHOST_DMA_IS_DONE(vq->heads[i].len)) {
  304. vq->heads[i].len = VHOST_DMA_CLEAR_LEN;
  305. ++j;
  306. } else
  307. break;
  308. }
  309. while (j) {
  310. add = min(UIO_MAXIOV - nvq->done_idx, j);
  311. vhost_add_used_and_signal_n(vq->dev, vq,
  312. &vq->heads[nvq->done_idx], add);
  313. nvq->done_idx = (nvq->done_idx + add) % UIO_MAXIOV;
  314. j -= add;
  315. }
  316. }
  317. static void vhost_zerocopy_callback(struct ubuf_info *ubuf, bool success)
  318. {
  319. struct vhost_net_ubuf_ref *ubufs = ubuf->ctx;
  320. struct vhost_virtqueue *vq = ubufs->vq;
  321. int cnt;
  322. rcu_read_lock_bh();
  323. /* set len to mark this desc buffers done DMA */
  324. vq->heads[ubuf->desc].len = success ?
  325. VHOST_DMA_DONE_LEN : VHOST_DMA_FAILED_LEN;
  326. cnt = vhost_net_ubuf_put(ubufs);
  327. /*
  328. * Trigger polling thread if guest stopped submitting new buffers:
  329. * in this case, the refcount after decrement will eventually reach 1.
  330. * We also trigger polling periodically after each 16 packets
  331. * (the value 16 here is more or less arbitrary, it's tuned to trigger
  332. * less than 10% of times).
  333. */
  334. if (cnt <= 1 || !(cnt % 16))
  335. vhost_poll_queue(&vq->poll);
  336. rcu_read_unlock_bh();
  337. }
  338. static inline unsigned long busy_clock(void)
  339. {
  340. return local_clock() >> 10;
  341. }
  342. static bool vhost_can_busy_poll(unsigned long endtime)
  343. {
  344. return likely(!need_resched() && !time_after(busy_clock(), endtime) &&
  345. !signal_pending(current));
  346. }
  347. static void vhost_net_disable_vq(struct vhost_net *n,
  348. struct vhost_virtqueue *vq)
  349. {
  350. struct vhost_net_virtqueue *nvq =
  351. container_of(vq, struct vhost_net_virtqueue, vq);
  352. struct vhost_poll *poll = n->poll + (nvq - n->vqs);
  353. if (!vq->private_data)
  354. return;
  355. vhost_poll_stop(poll);
  356. }
  357. static int vhost_net_enable_vq(struct vhost_net *n,
  358. struct vhost_virtqueue *vq)
  359. {
  360. struct vhost_net_virtqueue *nvq =
  361. container_of(vq, struct vhost_net_virtqueue, vq);
  362. struct vhost_poll *poll = n->poll + (nvq - n->vqs);
  363. struct socket *sock;
  364. sock = vq->private_data;
  365. if (!sock)
  366. return 0;
  367. return vhost_poll_start(poll, sock->file);
  368. }
  369. static void vhost_net_signal_used(struct vhost_net_virtqueue *nvq)
  370. {
  371. struct vhost_virtqueue *vq = &nvq->vq;
  372. struct vhost_dev *dev = vq->dev;
  373. if (!nvq->done_idx)
  374. return;
  375. vhost_add_used_and_signal_n(dev, vq, vq->heads, nvq->done_idx);
  376. nvq->done_idx = 0;
  377. }
  378. static int vhost_net_tx_get_vq_desc(struct vhost_net *net,
  379. struct vhost_net_virtqueue *nvq,
  380. unsigned int *out_num, unsigned int *in_num,
  381. bool *busyloop_intr)
  382. {
  383. struct vhost_virtqueue *vq = &nvq->vq;
  384. unsigned long uninitialized_var(endtime);
  385. int r = vhost_get_vq_desc(vq, vq->iov, ARRAY_SIZE(vq->iov),
  386. out_num, in_num, NULL, NULL);
  387. if (r == vq->num && vq->busyloop_timeout) {
  388. if (!vhost_sock_zcopy(vq->private_data))
  389. vhost_net_signal_used(nvq);
  390. preempt_disable();
  391. endtime = busy_clock() + vq->busyloop_timeout;
  392. while (vhost_can_busy_poll(endtime)) {
  393. if (vhost_has_work(vq->dev)) {
  394. *busyloop_intr = true;
  395. break;
  396. }
  397. if (!vhost_vq_avail_empty(vq->dev, vq))
  398. break;
  399. cpu_relax();
  400. }
  401. preempt_enable();
  402. r = vhost_get_vq_desc(vq, vq->iov, ARRAY_SIZE(vq->iov),
  403. out_num, in_num, NULL, NULL);
  404. }
  405. return r;
  406. }
  407. static bool vhost_exceeds_maxpend(struct vhost_net *net)
  408. {
  409. struct vhost_net_virtqueue *nvq = &net->vqs[VHOST_NET_VQ_TX];
  410. struct vhost_virtqueue *vq = &nvq->vq;
  411. return (nvq->upend_idx + UIO_MAXIOV - nvq->done_idx) % UIO_MAXIOV >
  412. min_t(unsigned int, VHOST_MAX_PEND, vq->num >> 2);
  413. }
  414. static size_t init_iov_iter(struct vhost_virtqueue *vq, struct iov_iter *iter,
  415. size_t hdr_size, int out)
  416. {
  417. /* Skip header. TODO: support TSO. */
  418. size_t len = iov_length(vq->iov, out);
  419. iov_iter_init(iter, WRITE, vq->iov, out, len);
  420. iov_iter_advance(iter, hdr_size);
  421. return iov_iter_count(iter);
  422. }
  423. static int get_tx_bufs(struct vhost_net *net,
  424. struct vhost_net_virtqueue *nvq,
  425. struct msghdr *msg,
  426. unsigned int *out, unsigned int *in,
  427. size_t *len, bool *busyloop_intr)
  428. {
  429. struct vhost_virtqueue *vq = &nvq->vq;
  430. int ret;
  431. ret = vhost_net_tx_get_vq_desc(net, nvq, out, in, busyloop_intr);
  432. if (ret < 0 || ret == vq->num)
  433. return ret;
  434. if (*in) {
  435. vq_err(vq, "Unexpected descriptor format for TX: out %d, int %d\n",
  436. *out, *in);
  437. return -EFAULT;
  438. }
  439. /* Sanity check */
  440. *len = init_iov_iter(vq, &msg->msg_iter, nvq->vhost_hlen, *out);
  441. if (*len == 0) {
  442. vq_err(vq, "Unexpected header len for TX: %zd expected %zd\n",
  443. *len, nvq->vhost_hlen);
  444. return -EFAULT;
  445. }
  446. return ret;
  447. }
  448. static bool tx_can_batch(struct vhost_virtqueue *vq, size_t total_len)
  449. {
  450. return total_len < VHOST_NET_WEIGHT &&
  451. !vhost_vq_avail_empty(vq->dev, vq);
  452. }
  453. static void handle_tx_copy(struct vhost_net *net, struct socket *sock)
  454. {
  455. struct vhost_net_virtqueue *nvq = &net->vqs[VHOST_NET_VQ_TX];
  456. struct vhost_virtqueue *vq = &nvq->vq;
  457. unsigned out, in;
  458. int head;
  459. struct msghdr msg = {
  460. .msg_name = NULL,
  461. .msg_namelen = 0,
  462. .msg_control = NULL,
  463. .msg_controllen = 0,
  464. .msg_flags = MSG_DONTWAIT,
  465. };
  466. size_t len, total_len = 0;
  467. int err;
  468. int sent_pkts = 0;
  469. do {
  470. bool busyloop_intr = false;
  471. head = get_tx_bufs(net, nvq, &msg, &out, &in, &len,
  472. &busyloop_intr);
  473. /* On error, stop handling until the next kick. */
  474. if (unlikely(head < 0))
  475. break;
  476. /* Nothing new? Wait for eventfd to tell us they refilled. */
  477. if (head == vq->num) {
  478. if (unlikely(busyloop_intr)) {
  479. vhost_poll_queue(&vq->poll);
  480. } else if (unlikely(vhost_enable_notify(&net->dev,
  481. vq))) {
  482. vhost_disable_notify(&net->dev, vq);
  483. continue;
  484. }
  485. break;
  486. }
  487. vq->heads[nvq->done_idx].id = cpu_to_vhost32(vq, head);
  488. vq->heads[nvq->done_idx].len = 0;
  489. total_len += len;
  490. if (tx_can_batch(vq, total_len))
  491. msg.msg_flags |= MSG_MORE;
  492. else
  493. msg.msg_flags &= ~MSG_MORE;
  494. /* TODO: Check specific error and bomb out unless ENOBUFS? */
  495. err = sock->ops->sendmsg(sock, &msg, len);
  496. if (unlikely(err < 0)) {
  497. vhost_discard_vq_desc(vq, 1);
  498. vhost_net_enable_vq(net, vq);
  499. break;
  500. }
  501. if (err != len)
  502. pr_debug("Truncated TX packet: len %d != %zd\n",
  503. err, len);
  504. if (++nvq->done_idx >= VHOST_NET_BATCH)
  505. vhost_net_signal_used(nvq);
  506. } while (likely(!vhost_exceeds_weight(vq, ++sent_pkts, total_len)));
  507. vhost_net_signal_used(nvq);
  508. }
  509. static void handle_tx_zerocopy(struct vhost_net *net, struct socket *sock)
  510. {
  511. struct vhost_net_virtqueue *nvq = &net->vqs[VHOST_NET_VQ_TX];
  512. struct vhost_virtqueue *vq = &nvq->vq;
  513. unsigned out, in;
  514. int head;
  515. struct msghdr msg = {
  516. .msg_name = NULL,
  517. .msg_namelen = 0,
  518. .msg_control = NULL,
  519. .msg_controllen = 0,
  520. .msg_flags = MSG_DONTWAIT,
  521. };
  522. size_t len, total_len = 0;
  523. int err;
  524. struct vhost_net_ubuf_ref *uninitialized_var(ubufs);
  525. struct ubuf_info *ubuf;
  526. bool zcopy_used;
  527. int sent_pkts = 0;
  528. do {
  529. bool busyloop_intr;
  530. /* Release DMAs done buffers first */
  531. vhost_zerocopy_signal_used(net, vq);
  532. busyloop_intr = false;
  533. head = get_tx_bufs(net, nvq, &msg, &out, &in, &len,
  534. &busyloop_intr);
  535. /* On error, stop handling until the next kick. */
  536. if (unlikely(head < 0))
  537. break;
  538. /* Nothing new? Wait for eventfd to tell us they refilled. */
  539. if (head == vq->num) {
  540. if (unlikely(busyloop_intr)) {
  541. vhost_poll_queue(&vq->poll);
  542. } else if (unlikely(vhost_enable_notify(&net->dev, vq))) {
  543. vhost_disable_notify(&net->dev, vq);
  544. continue;
  545. }
  546. break;
  547. }
  548. zcopy_used = len >= VHOST_GOODCOPY_LEN
  549. && !vhost_exceeds_maxpend(net)
  550. && vhost_net_tx_select_zcopy(net);
  551. /* use msg_control to pass vhost zerocopy ubuf info to skb */
  552. if (zcopy_used) {
  553. ubuf = nvq->ubuf_info + nvq->upend_idx;
  554. vq->heads[nvq->upend_idx].id = cpu_to_vhost32(vq, head);
  555. vq->heads[nvq->upend_idx].len = VHOST_DMA_IN_PROGRESS;
  556. ubuf->callback = vhost_zerocopy_callback;
  557. ubuf->ctx = nvq->ubufs;
  558. ubuf->desc = nvq->upend_idx;
  559. refcount_set(&ubuf->refcnt, 1);
  560. msg.msg_control = ubuf;
  561. msg.msg_controllen = sizeof(ubuf);
  562. ubufs = nvq->ubufs;
  563. atomic_inc(&ubufs->refcount);
  564. nvq->upend_idx = (nvq->upend_idx + 1) % UIO_MAXIOV;
  565. } else {
  566. msg.msg_control = NULL;
  567. ubufs = NULL;
  568. }
  569. total_len += len;
  570. if (tx_can_batch(vq, total_len) &&
  571. likely(!vhost_exceeds_maxpend(net))) {
  572. msg.msg_flags |= MSG_MORE;
  573. } else {
  574. msg.msg_flags &= ~MSG_MORE;
  575. }
  576. /* TODO: Check specific error and bomb out unless ENOBUFS? */
  577. err = sock->ops->sendmsg(sock, &msg, len);
  578. if (unlikely(err < 0)) {
  579. if (zcopy_used) {
  580. if (vq->heads[ubuf->desc].len == VHOST_DMA_IN_PROGRESS)
  581. vhost_net_ubuf_put(ubufs);
  582. nvq->upend_idx = ((unsigned)nvq->upend_idx - 1)
  583. % UIO_MAXIOV;
  584. }
  585. vhost_discard_vq_desc(vq, 1);
  586. vhost_net_enable_vq(net, vq);
  587. break;
  588. }
  589. if (err != len)
  590. pr_debug("Truncated TX packet: "
  591. " len %d != %zd\n", err, len);
  592. if (!zcopy_used)
  593. vhost_add_used_and_signal(&net->dev, vq, head, 0);
  594. else
  595. vhost_zerocopy_signal_used(net, vq);
  596. vhost_net_tx_packet(net);
  597. } while (likely(!vhost_exceeds_weight(vq, ++sent_pkts, total_len)));
  598. }
  599. /* Expects to be always run from workqueue - which acts as
  600. * read-size critical section for our kind of RCU. */
  601. static void handle_tx(struct vhost_net *net)
  602. {
  603. struct vhost_net_virtqueue *nvq = &net->vqs[VHOST_NET_VQ_TX];
  604. struct vhost_virtqueue *vq = &nvq->vq;
  605. struct socket *sock;
  606. mutex_lock(&vq->mutex);
  607. sock = vq->private_data;
  608. if (!sock)
  609. goto out;
  610. if (!vq_iotlb_prefetch(vq))
  611. goto out;
  612. vhost_disable_notify(&net->dev, vq);
  613. vhost_net_disable_vq(net, vq);
  614. if (vhost_sock_zcopy(sock))
  615. handle_tx_zerocopy(net, sock);
  616. else
  617. handle_tx_copy(net, sock);
  618. out:
  619. mutex_unlock(&vq->mutex);
  620. }
  621. static int peek_head_len(struct vhost_net_virtqueue *rvq, struct sock *sk)
  622. {
  623. struct sk_buff *head;
  624. int len = 0;
  625. unsigned long flags;
  626. if (rvq->rx_ring)
  627. return vhost_net_buf_peek(rvq);
  628. spin_lock_irqsave(&sk->sk_receive_queue.lock, flags);
  629. head = skb_peek(&sk->sk_receive_queue);
  630. if (likely(head)) {
  631. len = head->len;
  632. if (skb_vlan_tag_present(head))
  633. len += VLAN_HLEN;
  634. }
  635. spin_unlock_irqrestore(&sk->sk_receive_queue.lock, flags);
  636. return len;
  637. }
  638. static int sk_has_rx_data(struct sock *sk)
  639. {
  640. struct socket *sock = sk->sk_socket;
  641. if (sock->ops->peek_len)
  642. return sock->ops->peek_len(sock);
  643. return skb_queue_empty(&sk->sk_receive_queue);
  644. }
  645. static int vhost_net_rx_peek_head_len(struct vhost_net *net, struct sock *sk,
  646. bool *busyloop_intr)
  647. {
  648. struct vhost_net_virtqueue *rnvq = &net->vqs[VHOST_NET_VQ_RX];
  649. struct vhost_net_virtqueue *tnvq = &net->vqs[VHOST_NET_VQ_TX];
  650. struct vhost_virtqueue *rvq = &rnvq->vq;
  651. struct vhost_virtqueue *tvq = &tnvq->vq;
  652. unsigned long uninitialized_var(endtime);
  653. int len = peek_head_len(rnvq, sk);
  654. if (!len && tvq->busyloop_timeout) {
  655. /* Flush batched heads first */
  656. vhost_net_signal_used(rnvq);
  657. /* Both tx vq and rx socket were polled here */
  658. mutex_lock_nested(&tvq->mutex, 1);
  659. vhost_disable_notify(&net->dev, tvq);
  660. preempt_disable();
  661. endtime = busy_clock() + tvq->busyloop_timeout;
  662. while (vhost_can_busy_poll(endtime)) {
  663. if (vhost_has_work(&net->dev)) {
  664. *busyloop_intr = true;
  665. break;
  666. }
  667. if ((sk_has_rx_data(sk) &&
  668. !vhost_vq_avail_empty(&net->dev, rvq)) ||
  669. !vhost_vq_avail_empty(&net->dev, tvq))
  670. break;
  671. cpu_relax();
  672. }
  673. preempt_enable();
  674. if (!vhost_vq_avail_empty(&net->dev, tvq)) {
  675. vhost_poll_queue(&tvq->poll);
  676. } else if (unlikely(vhost_enable_notify(&net->dev, tvq))) {
  677. vhost_disable_notify(&net->dev, tvq);
  678. vhost_poll_queue(&tvq->poll);
  679. }
  680. mutex_unlock(&tvq->mutex);
  681. len = peek_head_len(rnvq, sk);
  682. }
  683. return len;
  684. }
  685. /* This is a multi-buffer version of vhost_get_desc, that works if
  686. * vq has read descriptors only.
  687. * @vq - the relevant virtqueue
  688. * @datalen - data length we'll be reading
  689. * @iovcount - returned count of io vectors we fill
  690. * @log - vhost log
  691. * @log_num - log offset
  692. * @quota - headcount quota, 1 for big buffer
  693. * returns number of buffer heads allocated, negative on error
  694. */
  695. static int get_rx_bufs(struct vhost_virtqueue *vq,
  696. struct vring_used_elem *heads,
  697. int datalen,
  698. unsigned *iovcount,
  699. struct vhost_log *log,
  700. unsigned *log_num,
  701. unsigned int quota)
  702. {
  703. unsigned int out, in;
  704. int seg = 0;
  705. int headcount = 0;
  706. unsigned d;
  707. int r, nlogs = 0;
  708. /* len is always initialized before use since we are always called with
  709. * datalen > 0.
  710. */
  711. u32 uninitialized_var(len);
  712. while (datalen > 0 && headcount < quota) {
  713. if (unlikely(seg >= UIO_MAXIOV)) {
  714. r = -ENOBUFS;
  715. goto err;
  716. }
  717. r = vhost_get_vq_desc(vq, vq->iov + seg,
  718. ARRAY_SIZE(vq->iov) - seg, &out,
  719. &in, log, log_num);
  720. if (unlikely(r < 0))
  721. goto err;
  722. d = r;
  723. if (d == vq->num) {
  724. r = 0;
  725. goto err;
  726. }
  727. if (unlikely(out || in <= 0)) {
  728. vq_err(vq, "unexpected descriptor format for RX: "
  729. "out %d, in %d\n", out, in);
  730. r = -EINVAL;
  731. goto err;
  732. }
  733. if (unlikely(log)) {
  734. nlogs += *log_num;
  735. log += *log_num;
  736. }
  737. heads[headcount].id = cpu_to_vhost32(vq, d);
  738. len = iov_length(vq->iov + seg, in);
  739. heads[headcount].len = cpu_to_vhost32(vq, len);
  740. datalen -= len;
  741. ++headcount;
  742. seg += in;
  743. }
  744. heads[headcount - 1].len = cpu_to_vhost32(vq, len + datalen);
  745. *iovcount = seg;
  746. if (unlikely(log))
  747. *log_num = nlogs;
  748. /* Detect overrun */
  749. if (unlikely(datalen > 0)) {
  750. r = UIO_MAXIOV + 1;
  751. goto err;
  752. }
  753. return headcount;
  754. err:
  755. vhost_discard_vq_desc(vq, headcount);
  756. return r;
  757. }
  758. /* Expects to be always run from workqueue - which acts as
  759. * read-size critical section for our kind of RCU. */
  760. static void handle_rx(struct vhost_net *net)
  761. {
  762. struct vhost_net_virtqueue *nvq = &net->vqs[VHOST_NET_VQ_RX];
  763. struct vhost_virtqueue *vq = &nvq->vq;
  764. unsigned uninitialized_var(in), log;
  765. struct vhost_log *vq_log;
  766. struct msghdr msg = {
  767. .msg_name = NULL,
  768. .msg_namelen = 0,
  769. .msg_control = NULL, /* FIXME: get and handle RX aux data. */
  770. .msg_controllen = 0,
  771. .msg_flags = MSG_DONTWAIT,
  772. };
  773. struct virtio_net_hdr hdr = {
  774. .flags = 0,
  775. .gso_type = VIRTIO_NET_HDR_GSO_NONE
  776. };
  777. size_t total_len = 0;
  778. int err, mergeable;
  779. s16 headcount;
  780. size_t vhost_hlen, sock_hlen;
  781. size_t vhost_len, sock_len;
  782. bool busyloop_intr = false;
  783. struct socket *sock;
  784. struct iov_iter fixup;
  785. __virtio16 num_buffers;
  786. int recv_pkts = 0;
  787. mutex_lock_nested(&vq->mutex, 0);
  788. sock = vq->private_data;
  789. if (!sock)
  790. goto out;
  791. if (!vq_iotlb_prefetch(vq))
  792. goto out;
  793. vhost_disable_notify(&net->dev, vq);
  794. vhost_net_disable_vq(net, vq);
  795. vhost_hlen = nvq->vhost_hlen;
  796. sock_hlen = nvq->sock_hlen;
  797. vq_log = unlikely(vhost_has_feature(vq, VHOST_F_LOG_ALL)) ?
  798. vq->log : NULL;
  799. mergeable = vhost_has_feature(vq, VIRTIO_NET_F_MRG_RXBUF);
  800. do {
  801. sock_len = vhost_net_rx_peek_head_len(net, sock->sk,
  802. &busyloop_intr);
  803. if (!sock_len)
  804. break;
  805. sock_len += sock_hlen;
  806. vhost_len = sock_len + vhost_hlen;
  807. headcount = get_rx_bufs(vq, vq->heads + nvq->done_idx,
  808. vhost_len, &in, vq_log, &log,
  809. likely(mergeable) ? UIO_MAXIOV : 1);
  810. /* On error, stop handling until the next kick. */
  811. if (unlikely(headcount < 0))
  812. goto out;
  813. /* OK, now we need to know about added descriptors. */
  814. if (!headcount) {
  815. if (unlikely(busyloop_intr)) {
  816. vhost_poll_queue(&vq->poll);
  817. } else if (unlikely(vhost_enable_notify(&net->dev, vq))) {
  818. /* They have slipped one in as we were
  819. * doing that: check again. */
  820. vhost_disable_notify(&net->dev, vq);
  821. continue;
  822. }
  823. /* Nothing new? Wait for eventfd to tell us
  824. * they refilled. */
  825. goto out;
  826. }
  827. busyloop_intr = false;
  828. if (nvq->rx_ring)
  829. msg.msg_control = vhost_net_buf_consume(&nvq->rxq);
  830. /* On overrun, truncate and discard */
  831. if (unlikely(headcount > UIO_MAXIOV)) {
  832. iov_iter_init(&msg.msg_iter, READ, vq->iov, 1, 1);
  833. err = sock->ops->recvmsg(sock, &msg,
  834. 1, MSG_DONTWAIT | MSG_TRUNC);
  835. pr_debug("Discarded rx packet: len %zd\n", sock_len);
  836. continue;
  837. }
  838. /* We don't need to be notified again. */
  839. iov_iter_init(&msg.msg_iter, READ, vq->iov, in, vhost_len);
  840. fixup = msg.msg_iter;
  841. if (unlikely((vhost_hlen))) {
  842. /* We will supply the header ourselves
  843. * TODO: support TSO.
  844. */
  845. iov_iter_advance(&msg.msg_iter, vhost_hlen);
  846. }
  847. err = sock->ops->recvmsg(sock, &msg,
  848. sock_len, MSG_DONTWAIT | MSG_TRUNC);
  849. /* Userspace might have consumed the packet meanwhile:
  850. * it's not supposed to do this usually, but might be hard
  851. * to prevent. Discard data we got (if any) and keep going. */
  852. if (unlikely(err != sock_len)) {
  853. pr_debug("Discarded rx packet: "
  854. " len %d, expected %zd\n", err, sock_len);
  855. vhost_discard_vq_desc(vq, headcount);
  856. continue;
  857. }
  858. /* Supply virtio_net_hdr if VHOST_NET_F_VIRTIO_NET_HDR */
  859. if (unlikely(vhost_hlen)) {
  860. if (copy_to_iter(&hdr, sizeof(hdr),
  861. &fixup) != sizeof(hdr)) {
  862. vq_err(vq, "Unable to write vnet_hdr "
  863. "at addr %p\n", vq->iov->iov_base);
  864. goto out;
  865. }
  866. } else {
  867. /* Header came from socket; we'll need to patch
  868. * ->num_buffers over if VIRTIO_NET_F_MRG_RXBUF
  869. */
  870. iov_iter_advance(&fixup, sizeof(hdr));
  871. }
  872. /* TODO: Should check and handle checksum. */
  873. num_buffers = cpu_to_vhost16(vq, headcount);
  874. if (likely(mergeable) &&
  875. copy_to_iter(&num_buffers, sizeof num_buffers,
  876. &fixup) != sizeof num_buffers) {
  877. vq_err(vq, "Failed num_buffers write");
  878. vhost_discard_vq_desc(vq, headcount);
  879. goto out;
  880. }
  881. nvq->done_idx += headcount;
  882. if (nvq->done_idx > VHOST_NET_BATCH)
  883. vhost_net_signal_used(nvq);
  884. if (unlikely(vq_log))
  885. vhost_log_write(vq, vq_log, log, vhost_len,
  886. vq->iov, in);
  887. total_len += vhost_len;
  888. } while (likely(!vhost_exceeds_weight(vq, ++recv_pkts, total_len)));
  889. if (unlikely(busyloop_intr))
  890. vhost_poll_queue(&vq->poll);
  891. else if (!sock_len)
  892. vhost_net_enable_vq(net, vq);
  893. out:
  894. vhost_net_signal_used(nvq);
  895. mutex_unlock(&vq->mutex);
  896. }
  897. static void handle_tx_kick(struct vhost_work *work)
  898. {
  899. struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
  900. poll.work);
  901. struct vhost_net *net = container_of(vq->dev, struct vhost_net, dev);
  902. handle_tx(net);
  903. }
  904. static void handle_rx_kick(struct vhost_work *work)
  905. {
  906. struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
  907. poll.work);
  908. struct vhost_net *net = container_of(vq->dev, struct vhost_net, dev);
  909. handle_rx(net);
  910. }
  911. static void handle_tx_net(struct vhost_work *work)
  912. {
  913. struct vhost_net *net = container_of(work, struct vhost_net,
  914. poll[VHOST_NET_VQ_TX].work);
  915. handle_tx(net);
  916. }
  917. static void handle_rx_net(struct vhost_work *work)
  918. {
  919. struct vhost_net *net = container_of(work, struct vhost_net,
  920. poll[VHOST_NET_VQ_RX].work);
  921. handle_rx(net);
  922. }
  923. static int vhost_net_open(struct inode *inode, struct file *f)
  924. {
  925. struct vhost_net *n;
  926. struct vhost_dev *dev;
  927. struct vhost_virtqueue **vqs;
  928. void **queue;
  929. int i;
  930. n = kvmalloc(sizeof *n, GFP_KERNEL | __GFP_RETRY_MAYFAIL);
  931. if (!n)
  932. return -ENOMEM;
  933. vqs = kmalloc_array(VHOST_NET_VQ_MAX, sizeof(*vqs), GFP_KERNEL);
  934. if (!vqs) {
  935. kvfree(n);
  936. return -ENOMEM;
  937. }
  938. queue = kmalloc_array(VHOST_NET_BATCH, sizeof(void *),
  939. GFP_KERNEL);
  940. if (!queue) {
  941. kfree(vqs);
  942. kvfree(n);
  943. return -ENOMEM;
  944. }
  945. n->vqs[VHOST_NET_VQ_RX].rxq.queue = queue;
  946. dev = &n->dev;
  947. vqs[VHOST_NET_VQ_TX] = &n->vqs[VHOST_NET_VQ_TX].vq;
  948. vqs[VHOST_NET_VQ_RX] = &n->vqs[VHOST_NET_VQ_RX].vq;
  949. n->vqs[VHOST_NET_VQ_TX].vq.handle_kick = handle_tx_kick;
  950. n->vqs[VHOST_NET_VQ_RX].vq.handle_kick = handle_rx_kick;
  951. for (i = 0; i < VHOST_NET_VQ_MAX; i++) {
  952. n->vqs[i].ubufs = NULL;
  953. n->vqs[i].ubuf_info = NULL;
  954. n->vqs[i].upend_idx = 0;
  955. n->vqs[i].done_idx = 0;
  956. n->vqs[i].vhost_hlen = 0;
  957. n->vqs[i].sock_hlen = 0;
  958. n->vqs[i].rx_ring = NULL;
  959. vhost_net_buf_init(&n->vqs[i].rxq);
  960. }
  961. vhost_dev_init(dev, vqs, VHOST_NET_VQ_MAX,
  962. UIO_MAXIOV + VHOST_NET_BATCH,
  963. VHOST_NET_PKT_WEIGHT, VHOST_NET_WEIGHT);
  964. vhost_poll_init(n->poll + VHOST_NET_VQ_TX, handle_tx_net, EPOLLOUT, dev);
  965. vhost_poll_init(n->poll + VHOST_NET_VQ_RX, handle_rx_net, EPOLLIN, dev);
  966. f->private_data = n;
  967. return 0;
  968. }
  969. static struct socket *vhost_net_stop_vq(struct vhost_net *n,
  970. struct vhost_virtqueue *vq)
  971. {
  972. struct socket *sock;
  973. struct vhost_net_virtqueue *nvq =
  974. container_of(vq, struct vhost_net_virtqueue, vq);
  975. mutex_lock(&vq->mutex);
  976. sock = vq->private_data;
  977. vhost_net_disable_vq(n, vq);
  978. vq->private_data = NULL;
  979. vhost_net_buf_unproduce(nvq);
  980. nvq->rx_ring = NULL;
  981. mutex_unlock(&vq->mutex);
  982. return sock;
  983. }
  984. static void vhost_net_stop(struct vhost_net *n, struct socket **tx_sock,
  985. struct socket **rx_sock)
  986. {
  987. *tx_sock = vhost_net_stop_vq(n, &n->vqs[VHOST_NET_VQ_TX].vq);
  988. *rx_sock = vhost_net_stop_vq(n, &n->vqs[VHOST_NET_VQ_RX].vq);
  989. }
  990. static void vhost_net_flush_vq(struct vhost_net *n, int index)
  991. {
  992. vhost_poll_flush(n->poll + index);
  993. vhost_poll_flush(&n->vqs[index].vq.poll);
  994. }
  995. static void vhost_net_flush(struct vhost_net *n)
  996. {
  997. vhost_net_flush_vq(n, VHOST_NET_VQ_TX);
  998. vhost_net_flush_vq(n, VHOST_NET_VQ_RX);
  999. if (n->vqs[VHOST_NET_VQ_TX].ubufs) {
  1000. mutex_lock(&n->vqs[VHOST_NET_VQ_TX].vq.mutex);
  1001. n->tx_flush = true;
  1002. mutex_unlock(&n->vqs[VHOST_NET_VQ_TX].vq.mutex);
  1003. /* Wait for all lower device DMAs done. */
  1004. vhost_net_ubuf_put_and_wait(n->vqs[VHOST_NET_VQ_TX].ubufs);
  1005. mutex_lock(&n->vqs[VHOST_NET_VQ_TX].vq.mutex);
  1006. n->tx_flush = false;
  1007. atomic_set(&n->vqs[VHOST_NET_VQ_TX].ubufs->refcount, 1);
  1008. mutex_unlock(&n->vqs[VHOST_NET_VQ_TX].vq.mutex);
  1009. }
  1010. }
  1011. static int vhost_net_release(struct inode *inode, struct file *f)
  1012. {
  1013. struct vhost_net *n = f->private_data;
  1014. struct socket *tx_sock;
  1015. struct socket *rx_sock;
  1016. vhost_net_stop(n, &tx_sock, &rx_sock);
  1017. vhost_net_flush(n);
  1018. vhost_dev_stop(&n->dev);
  1019. vhost_dev_cleanup(&n->dev);
  1020. vhost_net_vq_reset(n);
  1021. if (tx_sock)
  1022. sockfd_put(tx_sock);
  1023. if (rx_sock)
  1024. sockfd_put(rx_sock);
  1025. /* Make sure no callbacks are outstanding */
  1026. synchronize_rcu_bh();
  1027. /* We do an extra flush before freeing memory,
  1028. * since jobs can re-queue themselves. */
  1029. vhost_net_flush(n);
  1030. kfree(n->vqs[VHOST_NET_VQ_RX].rxq.queue);
  1031. kfree(n->dev.vqs);
  1032. kvfree(n);
  1033. return 0;
  1034. }
  1035. static struct socket *get_raw_socket(int fd)
  1036. {
  1037. int r;
  1038. struct socket *sock = sockfd_lookup(fd, &r);
  1039. if (!sock)
  1040. return ERR_PTR(-ENOTSOCK);
  1041. /* Parameter checking */
  1042. if (sock->sk->sk_type != SOCK_RAW) {
  1043. r = -ESOCKTNOSUPPORT;
  1044. goto err;
  1045. }
  1046. if (sock->sk->sk_family != AF_PACKET) {
  1047. r = -EPFNOSUPPORT;
  1048. goto err;
  1049. }
  1050. return sock;
  1051. err:
  1052. sockfd_put(sock);
  1053. return ERR_PTR(r);
  1054. }
  1055. static struct ptr_ring *get_tap_ptr_ring(int fd)
  1056. {
  1057. struct ptr_ring *ring;
  1058. struct file *file = fget(fd);
  1059. if (!file)
  1060. return NULL;
  1061. ring = tun_get_tx_ring(file);
  1062. if (!IS_ERR(ring))
  1063. goto out;
  1064. ring = tap_get_ptr_ring(file);
  1065. if (!IS_ERR(ring))
  1066. goto out;
  1067. ring = NULL;
  1068. out:
  1069. fput(file);
  1070. return ring;
  1071. }
  1072. static struct socket *get_tap_socket(int fd)
  1073. {
  1074. struct file *file = fget(fd);
  1075. struct socket *sock;
  1076. if (!file)
  1077. return ERR_PTR(-EBADF);
  1078. sock = tun_get_socket(file);
  1079. if (!IS_ERR(sock))
  1080. return sock;
  1081. sock = tap_get_socket(file);
  1082. if (IS_ERR(sock))
  1083. fput(file);
  1084. return sock;
  1085. }
  1086. static struct socket *get_socket(int fd)
  1087. {
  1088. struct socket *sock;
  1089. /* special case to disable backend */
  1090. if (fd == -1)
  1091. return NULL;
  1092. sock = get_raw_socket(fd);
  1093. if (!IS_ERR(sock))
  1094. return sock;
  1095. sock = get_tap_socket(fd);
  1096. if (!IS_ERR(sock))
  1097. return sock;
  1098. return ERR_PTR(-ENOTSOCK);
  1099. }
  1100. static long vhost_net_set_backend(struct vhost_net *n, unsigned index, int fd)
  1101. {
  1102. struct socket *sock, *oldsock;
  1103. struct vhost_virtqueue *vq;
  1104. struct vhost_net_virtqueue *nvq;
  1105. struct vhost_net_ubuf_ref *ubufs, *oldubufs = NULL;
  1106. int r;
  1107. mutex_lock(&n->dev.mutex);
  1108. r = vhost_dev_check_owner(&n->dev);
  1109. if (r)
  1110. goto err;
  1111. if (index >= VHOST_NET_VQ_MAX) {
  1112. r = -ENOBUFS;
  1113. goto err;
  1114. }
  1115. vq = &n->vqs[index].vq;
  1116. nvq = &n->vqs[index];
  1117. mutex_lock(&vq->mutex);
  1118. /* Verify that ring has been setup correctly. */
  1119. if (!vhost_vq_access_ok(vq)) {
  1120. r = -EFAULT;
  1121. goto err_vq;
  1122. }
  1123. sock = get_socket(fd);
  1124. if (IS_ERR(sock)) {
  1125. r = PTR_ERR(sock);
  1126. goto err_vq;
  1127. }
  1128. /* start polling new socket */
  1129. oldsock = vq->private_data;
  1130. if (sock != oldsock) {
  1131. ubufs = vhost_net_ubuf_alloc(vq,
  1132. sock && vhost_sock_zcopy(sock));
  1133. if (IS_ERR(ubufs)) {
  1134. r = PTR_ERR(ubufs);
  1135. goto err_ubufs;
  1136. }
  1137. vhost_net_disable_vq(n, vq);
  1138. vq->private_data = sock;
  1139. vhost_net_buf_unproduce(nvq);
  1140. r = vhost_vq_init_access(vq);
  1141. if (r)
  1142. goto err_used;
  1143. r = vhost_net_enable_vq(n, vq);
  1144. if (r)
  1145. goto err_used;
  1146. if (index == VHOST_NET_VQ_RX)
  1147. nvq->rx_ring = get_tap_ptr_ring(fd);
  1148. oldubufs = nvq->ubufs;
  1149. nvq->ubufs = ubufs;
  1150. n->tx_packets = 0;
  1151. n->tx_zcopy_err = 0;
  1152. n->tx_flush = false;
  1153. }
  1154. mutex_unlock(&vq->mutex);
  1155. if (oldubufs) {
  1156. vhost_net_ubuf_put_wait_and_free(oldubufs);
  1157. mutex_lock(&vq->mutex);
  1158. vhost_zerocopy_signal_used(n, vq);
  1159. mutex_unlock(&vq->mutex);
  1160. }
  1161. if (oldsock) {
  1162. vhost_net_flush_vq(n, index);
  1163. sockfd_put(oldsock);
  1164. }
  1165. mutex_unlock(&n->dev.mutex);
  1166. return 0;
  1167. err_used:
  1168. vq->private_data = oldsock;
  1169. vhost_net_enable_vq(n, vq);
  1170. if (ubufs)
  1171. vhost_net_ubuf_put_wait_and_free(ubufs);
  1172. err_ubufs:
  1173. if (sock)
  1174. sockfd_put(sock);
  1175. err_vq:
  1176. mutex_unlock(&vq->mutex);
  1177. err:
  1178. mutex_unlock(&n->dev.mutex);
  1179. return r;
  1180. }
  1181. static long vhost_net_reset_owner(struct vhost_net *n)
  1182. {
  1183. struct socket *tx_sock = NULL;
  1184. struct socket *rx_sock = NULL;
  1185. long err;
  1186. struct vhost_umem *umem;
  1187. mutex_lock(&n->dev.mutex);
  1188. err = vhost_dev_check_owner(&n->dev);
  1189. if (err)
  1190. goto done;
  1191. umem = vhost_dev_reset_owner_prepare();
  1192. if (!umem) {
  1193. err = -ENOMEM;
  1194. goto done;
  1195. }
  1196. vhost_net_stop(n, &tx_sock, &rx_sock);
  1197. vhost_net_flush(n);
  1198. vhost_dev_stop(&n->dev);
  1199. vhost_dev_reset_owner(&n->dev, umem);
  1200. vhost_net_vq_reset(n);
  1201. done:
  1202. mutex_unlock(&n->dev.mutex);
  1203. if (tx_sock)
  1204. sockfd_put(tx_sock);
  1205. if (rx_sock)
  1206. sockfd_put(rx_sock);
  1207. return err;
  1208. }
  1209. static int vhost_net_set_backend_features(struct vhost_net *n, u64 features)
  1210. {
  1211. int i;
  1212. mutex_lock(&n->dev.mutex);
  1213. for (i = 0; i < VHOST_NET_VQ_MAX; ++i) {
  1214. mutex_lock(&n->vqs[i].vq.mutex);
  1215. n->vqs[i].vq.acked_backend_features = features;
  1216. mutex_unlock(&n->vqs[i].vq.mutex);
  1217. }
  1218. mutex_unlock(&n->dev.mutex);
  1219. return 0;
  1220. }
  1221. static int vhost_net_set_features(struct vhost_net *n, u64 features)
  1222. {
  1223. size_t vhost_hlen, sock_hlen, hdr_len;
  1224. int i;
  1225. hdr_len = (features & ((1ULL << VIRTIO_NET_F_MRG_RXBUF) |
  1226. (1ULL << VIRTIO_F_VERSION_1))) ?
  1227. sizeof(struct virtio_net_hdr_mrg_rxbuf) :
  1228. sizeof(struct virtio_net_hdr);
  1229. if (features & (1 << VHOST_NET_F_VIRTIO_NET_HDR)) {
  1230. /* vhost provides vnet_hdr */
  1231. vhost_hlen = hdr_len;
  1232. sock_hlen = 0;
  1233. } else {
  1234. /* socket provides vnet_hdr */
  1235. vhost_hlen = 0;
  1236. sock_hlen = hdr_len;
  1237. }
  1238. mutex_lock(&n->dev.mutex);
  1239. if ((features & (1 << VHOST_F_LOG_ALL)) &&
  1240. !vhost_log_access_ok(&n->dev))
  1241. goto out_unlock;
  1242. if ((features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))) {
  1243. if (vhost_init_device_iotlb(&n->dev, true))
  1244. goto out_unlock;
  1245. }
  1246. for (i = 0; i < VHOST_NET_VQ_MAX; ++i) {
  1247. mutex_lock(&n->vqs[i].vq.mutex);
  1248. n->vqs[i].vq.acked_features = features;
  1249. n->vqs[i].vhost_hlen = vhost_hlen;
  1250. n->vqs[i].sock_hlen = sock_hlen;
  1251. mutex_unlock(&n->vqs[i].vq.mutex);
  1252. }
  1253. mutex_unlock(&n->dev.mutex);
  1254. return 0;
  1255. out_unlock:
  1256. mutex_unlock(&n->dev.mutex);
  1257. return -EFAULT;
  1258. }
  1259. static long vhost_net_set_owner(struct vhost_net *n)
  1260. {
  1261. int r;
  1262. mutex_lock(&n->dev.mutex);
  1263. if (vhost_dev_has_owner(&n->dev)) {
  1264. r = -EBUSY;
  1265. goto out;
  1266. }
  1267. r = vhost_net_set_ubuf_info(n);
  1268. if (r)
  1269. goto out;
  1270. r = vhost_dev_set_owner(&n->dev);
  1271. if (r)
  1272. vhost_net_clear_ubuf_info(n);
  1273. vhost_net_flush(n);
  1274. out:
  1275. mutex_unlock(&n->dev.mutex);
  1276. return r;
  1277. }
  1278. static long vhost_net_ioctl(struct file *f, unsigned int ioctl,
  1279. unsigned long arg)
  1280. {
  1281. struct vhost_net *n = f->private_data;
  1282. void __user *argp = (void __user *)arg;
  1283. u64 __user *featurep = argp;
  1284. struct vhost_vring_file backend;
  1285. u64 features;
  1286. int r;
  1287. switch (ioctl) {
  1288. case VHOST_NET_SET_BACKEND:
  1289. if (copy_from_user(&backend, argp, sizeof backend))
  1290. return -EFAULT;
  1291. return vhost_net_set_backend(n, backend.index, backend.fd);
  1292. case VHOST_GET_FEATURES:
  1293. features = VHOST_NET_FEATURES;
  1294. if (copy_to_user(featurep, &features, sizeof features))
  1295. return -EFAULT;
  1296. return 0;
  1297. case VHOST_SET_FEATURES:
  1298. if (copy_from_user(&features, featurep, sizeof features))
  1299. return -EFAULT;
  1300. if (features & ~VHOST_NET_FEATURES)
  1301. return -EOPNOTSUPP;
  1302. return vhost_net_set_features(n, features);
  1303. case VHOST_GET_BACKEND_FEATURES:
  1304. features = VHOST_NET_BACKEND_FEATURES;
  1305. if (copy_to_user(featurep, &features, sizeof(features)))
  1306. return -EFAULT;
  1307. return 0;
  1308. case VHOST_SET_BACKEND_FEATURES:
  1309. if (copy_from_user(&features, featurep, sizeof(features)))
  1310. return -EFAULT;
  1311. if (features & ~VHOST_NET_BACKEND_FEATURES)
  1312. return -EOPNOTSUPP;
  1313. return vhost_net_set_backend_features(n, features);
  1314. case VHOST_RESET_OWNER:
  1315. return vhost_net_reset_owner(n);
  1316. case VHOST_SET_OWNER:
  1317. return vhost_net_set_owner(n);
  1318. default:
  1319. mutex_lock(&n->dev.mutex);
  1320. r = vhost_dev_ioctl(&n->dev, ioctl, argp);
  1321. if (r == -ENOIOCTLCMD)
  1322. r = vhost_vring_ioctl(&n->dev, ioctl, argp);
  1323. else
  1324. vhost_net_flush(n);
  1325. mutex_unlock(&n->dev.mutex);
  1326. return r;
  1327. }
  1328. }
  1329. #ifdef CONFIG_COMPAT
  1330. static long vhost_net_compat_ioctl(struct file *f, unsigned int ioctl,
  1331. unsigned long arg)
  1332. {
  1333. return vhost_net_ioctl(f, ioctl, (unsigned long)compat_ptr(arg));
  1334. }
  1335. #endif
  1336. static ssize_t vhost_net_chr_read_iter(struct kiocb *iocb, struct iov_iter *to)
  1337. {
  1338. struct file *file = iocb->ki_filp;
  1339. struct vhost_net *n = file->private_data;
  1340. struct vhost_dev *dev = &n->dev;
  1341. int noblock = file->f_flags & O_NONBLOCK;
  1342. return vhost_chr_read_iter(dev, to, noblock);
  1343. }
  1344. static ssize_t vhost_net_chr_write_iter(struct kiocb *iocb,
  1345. struct iov_iter *from)
  1346. {
  1347. struct file *file = iocb->ki_filp;
  1348. struct vhost_net *n = file->private_data;
  1349. struct vhost_dev *dev = &n->dev;
  1350. return vhost_chr_write_iter(dev, from);
  1351. }
  1352. static __poll_t vhost_net_chr_poll(struct file *file, poll_table *wait)
  1353. {
  1354. struct vhost_net *n = file->private_data;
  1355. struct vhost_dev *dev = &n->dev;
  1356. return vhost_chr_poll(file, dev, wait);
  1357. }
  1358. static const struct file_operations vhost_net_fops = {
  1359. .owner = THIS_MODULE,
  1360. .release = vhost_net_release,
  1361. .read_iter = vhost_net_chr_read_iter,
  1362. .write_iter = vhost_net_chr_write_iter,
  1363. .poll = vhost_net_chr_poll,
  1364. .unlocked_ioctl = vhost_net_ioctl,
  1365. #ifdef CONFIG_COMPAT
  1366. .compat_ioctl = vhost_net_compat_ioctl,
  1367. #endif
  1368. .open = vhost_net_open,
  1369. .llseek = noop_llseek,
  1370. };
  1371. static struct miscdevice vhost_net_misc = {
  1372. .minor = VHOST_NET_MINOR,
  1373. .name = "vhost-net",
  1374. .fops = &vhost_net_fops,
  1375. };
  1376. static int vhost_net_init(void)
  1377. {
  1378. if (experimental_zcopytx)
  1379. vhost_net_enable_zcopy(VHOST_NET_VQ_TX);
  1380. return misc_register(&vhost_net_misc);
  1381. }
  1382. module_init(vhost_net_init);
  1383. static void vhost_net_exit(void)
  1384. {
  1385. misc_deregister(&vhost_net_misc);
  1386. }
  1387. module_exit(vhost_net_exit);
  1388. MODULE_VERSION("0.0.1");
  1389. MODULE_LICENSE("GPL v2");
  1390. MODULE_AUTHOR("Michael S. Tsirkin");
  1391. MODULE_DESCRIPTION("Host kernel accelerator for virtio net");
  1392. MODULE_ALIAS_MISCDEV(VHOST_NET_MINOR);
  1393. MODULE_ALIAS("devname:vhost-net");