net.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162
  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/vmalloc.h>
  20. #include <linux/net.h>
  21. #include <linux/if_packet.h>
  22. #include <linux/if_arp.h>
  23. #include <linux/if_tun.h>
  24. #include <linux/if_macvlan.h>
  25. #include <linux/if_vlan.h>
  26. #include <net/sock.h>
  27. #include "vhost.h"
  28. static int experimental_zcopytx = 1;
  29. module_param(experimental_zcopytx, int, 0444);
  30. MODULE_PARM_DESC(experimental_zcopytx, "Enable Zero Copy TX;"
  31. " 1 -Enable; 0 - Disable");
  32. /* Max number of bytes transferred before requeueing the job.
  33. * Using this limit prevents one virtqueue from starving others. */
  34. #define VHOST_NET_WEIGHT 0x80000
  35. /* MAX number of TX used buffers for outstanding zerocopy */
  36. #define VHOST_MAX_PEND 128
  37. #define VHOST_GOODCOPY_LEN 256
  38. /*
  39. * For transmit, used buffer len is unused; we override it to track buffer
  40. * status internally; used for zerocopy tx only.
  41. */
  42. /* Lower device DMA failed */
  43. #define VHOST_DMA_FAILED_LEN 3
  44. /* Lower device DMA done */
  45. #define VHOST_DMA_DONE_LEN 2
  46. /* Lower device DMA in progress */
  47. #define VHOST_DMA_IN_PROGRESS 1
  48. /* Buffer unused */
  49. #define VHOST_DMA_CLEAR_LEN 0
  50. #define VHOST_DMA_IS_DONE(len) ((len) >= VHOST_DMA_DONE_LEN)
  51. enum {
  52. VHOST_NET_FEATURES = VHOST_FEATURES |
  53. (1ULL << VHOST_NET_F_VIRTIO_NET_HDR) |
  54. (1ULL << VIRTIO_NET_F_MRG_RXBUF),
  55. };
  56. enum {
  57. VHOST_NET_VQ_RX = 0,
  58. VHOST_NET_VQ_TX = 1,
  59. VHOST_NET_VQ_MAX = 2,
  60. };
  61. struct vhost_net_ubuf_ref {
  62. /* refcount follows semantics similar to kref:
  63. * 0: object is released
  64. * 1: no outstanding ubufs
  65. * >1: outstanding ubufs
  66. */
  67. atomic_t refcount;
  68. wait_queue_head_t wait;
  69. struct vhost_virtqueue *vq;
  70. };
  71. struct vhost_net_virtqueue {
  72. struct vhost_virtqueue vq;
  73. /* hdr is used to store the virtio header.
  74. * Since each iovec has >= 1 byte length, we never need more than
  75. * header length entries to store the header. */
  76. struct iovec hdr[sizeof(struct virtio_net_hdr_mrg_rxbuf)];
  77. size_t vhost_hlen;
  78. size_t sock_hlen;
  79. /* vhost zerocopy support fields below: */
  80. /* last used idx for outstanding DMA zerocopy buffers */
  81. int upend_idx;
  82. /* first used idx for DMA done zerocopy buffers */
  83. int done_idx;
  84. /* an array of userspace buffers info */
  85. struct ubuf_info *ubuf_info;
  86. /* Reference counting for outstanding ubufs.
  87. * Protected by vq mutex. Writers must also take device mutex. */
  88. struct vhost_net_ubuf_ref *ubufs;
  89. };
  90. struct vhost_net {
  91. struct vhost_dev dev;
  92. struct vhost_net_virtqueue vqs[VHOST_NET_VQ_MAX];
  93. struct vhost_poll poll[VHOST_NET_VQ_MAX];
  94. /* Number of TX recently submitted.
  95. * Protected by tx vq lock. */
  96. unsigned tx_packets;
  97. /* Number of times zerocopy TX recently failed.
  98. * Protected by tx vq lock. */
  99. unsigned tx_zcopy_err;
  100. /* Flush in progress. Protected by tx vq lock. */
  101. bool tx_flush;
  102. };
  103. static unsigned vhost_net_zcopy_mask __read_mostly;
  104. static void vhost_net_enable_zcopy(int vq)
  105. {
  106. vhost_net_zcopy_mask |= 0x1 << vq;
  107. }
  108. static struct vhost_net_ubuf_ref *
  109. vhost_net_ubuf_alloc(struct vhost_virtqueue *vq, bool zcopy)
  110. {
  111. struct vhost_net_ubuf_ref *ubufs;
  112. /* No zero copy backend? Nothing to count. */
  113. if (!zcopy)
  114. return NULL;
  115. ubufs = kmalloc(sizeof(*ubufs), GFP_KERNEL);
  116. if (!ubufs)
  117. return ERR_PTR(-ENOMEM);
  118. atomic_set(&ubufs->refcount, 1);
  119. init_waitqueue_head(&ubufs->wait);
  120. ubufs->vq = vq;
  121. return ubufs;
  122. }
  123. static int vhost_net_ubuf_put(struct vhost_net_ubuf_ref *ubufs)
  124. {
  125. int r = atomic_sub_return(1, &ubufs->refcount);
  126. if (unlikely(!r))
  127. wake_up(&ubufs->wait);
  128. return r;
  129. }
  130. static void vhost_net_ubuf_put_and_wait(struct vhost_net_ubuf_ref *ubufs)
  131. {
  132. vhost_net_ubuf_put(ubufs);
  133. wait_event(ubufs->wait, !atomic_read(&ubufs->refcount));
  134. }
  135. static void vhost_net_ubuf_put_wait_and_free(struct vhost_net_ubuf_ref *ubufs)
  136. {
  137. vhost_net_ubuf_put_and_wait(ubufs);
  138. kfree(ubufs);
  139. }
  140. static void vhost_net_clear_ubuf_info(struct vhost_net *n)
  141. {
  142. int i;
  143. for (i = 0; i < VHOST_NET_VQ_MAX; ++i) {
  144. kfree(n->vqs[i].ubuf_info);
  145. n->vqs[i].ubuf_info = NULL;
  146. }
  147. }
  148. static int vhost_net_set_ubuf_info(struct vhost_net *n)
  149. {
  150. bool zcopy;
  151. int i;
  152. for (i = 0; i < VHOST_NET_VQ_MAX; ++i) {
  153. zcopy = vhost_net_zcopy_mask & (0x1 << i);
  154. if (!zcopy)
  155. continue;
  156. n->vqs[i].ubuf_info = kmalloc(sizeof(*n->vqs[i].ubuf_info) *
  157. UIO_MAXIOV, GFP_KERNEL);
  158. if (!n->vqs[i].ubuf_info)
  159. goto err;
  160. }
  161. return 0;
  162. err:
  163. vhost_net_clear_ubuf_info(n);
  164. return -ENOMEM;
  165. }
  166. static void vhost_net_vq_reset(struct vhost_net *n)
  167. {
  168. int i;
  169. vhost_net_clear_ubuf_info(n);
  170. for (i = 0; i < VHOST_NET_VQ_MAX; i++) {
  171. n->vqs[i].done_idx = 0;
  172. n->vqs[i].upend_idx = 0;
  173. n->vqs[i].ubufs = NULL;
  174. n->vqs[i].vhost_hlen = 0;
  175. n->vqs[i].sock_hlen = 0;
  176. }
  177. }
  178. static void vhost_net_tx_packet(struct vhost_net *net)
  179. {
  180. ++net->tx_packets;
  181. if (net->tx_packets < 1024)
  182. return;
  183. net->tx_packets = 0;
  184. net->tx_zcopy_err = 0;
  185. }
  186. static void vhost_net_tx_err(struct vhost_net *net)
  187. {
  188. ++net->tx_zcopy_err;
  189. }
  190. static bool vhost_net_tx_select_zcopy(struct vhost_net *net)
  191. {
  192. /* TX flush waits for outstanding DMAs to be done.
  193. * Don't start new DMAs.
  194. */
  195. return !net->tx_flush &&
  196. net->tx_packets / 64 >= net->tx_zcopy_err;
  197. }
  198. static bool vhost_sock_zcopy(struct socket *sock)
  199. {
  200. return unlikely(experimental_zcopytx) &&
  201. sock_flag(sock->sk, SOCK_ZEROCOPY);
  202. }
  203. /* Pop first len bytes from iovec. Return number of segments used. */
  204. static int move_iovec_hdr(struct iovec *from, struct iovec *to,
  205. size_t len, int iov_count)
  206. {
  207. int seg = 0;
  208. size_t size;
  209. while (len && seg < iov_count) {
  210. size = min(from->iov_len, len);
  211. to->iov_base = from->iov_base;
  212. to->iov_len = size;
  213. from->iov_len -= size;
  214. from->iov_base += size;
  215. len -= size;
  216. ++from;
  217. ++to;
  218. ++seg;
  219. }
  220. return seg;
  221. }
  222. /* Copy iovec entries for len bytes from iovec. */
  223. static void copy_iovec_hdr(const struct iovec *from, struct iovec *to,
  224. size_t len, int iovcount)
  225. {
  226. int seg = 0;
  227. size_t size;
  228. while (len && seg < iovcount) {
  229. size = min(from->iov_len, len);
  230. to->iov_base = from->iov_base;
  231. to->iov_len = size;
  232. len -= size;
  233. ++from;
  234. ++to;
  235. ++seg;
  236. }
  237. }
  238. /* In case of DMA done not in order in lower device driver for some reason.
  239. * upend_idx is used to track end of used idx, done_idx is used to track head
  240. * of used idx. Once lower device DMA done contiguously, we will signal KVM
  241. * guest used idx.
  242. */
  243. static void vhost_zerocopy_signal_used(struct vhost_net *net,
  244. struct vhost_virtqueue *vq)
  245. {
  246. struct vhost_net_virtqueue *nvq =
  247. container_of(vq, struct vhost_net_virtqueue, vq);
  248. int i, add;
  249. int j = 0;
  250. for (i = nvq->done_idx; i != nvq->upend_idx; i = (i + 1) % UIO_MAXIOV) {
  251. if (vq->heads[i].len == VHOST_DMA_FAILED_LEN)
  252. vhost_net_tx_err(net);
  253. if (VHOST_DMA_IS_DONE(vq->heads[i].len)) {
  254. vq->heads[i].len = VHOST_DMA_CLEAR_LEN;
  255. ++j;
  256. } else
  257. break;
  258. }
  259. while (j) {
  260. add = min(UIO_MAXIOV - nvq->done_idx, j);
  261. vhost_add_used_and_signal_n(vq->dev, vq,
  262. &vq->heads[nvq->done_idx], add);
  263. nvq->done_idx = (nvq->done_idx + add) % UIO_MAXIOV;
  264. j -= add;
  265. }
  266. }
  267. static void vhost_zerocopy_callback(struct ubuf_info *ubuf, bool success)
  268. {
  269. struct vhost_net_ubuf_ref *ubufs = ubuf->ctx;
  270. struct vhost_virtqueue *vq = ubufs->vq;
  271. int cnt;
  272. rcu_read_lock_bh();
  273. /* set len to mark this desc buffers done DMA */
  274. vq->heads[ubuf->desc].len = success ?
  275. VHOST_DMA_DONE_LEN : VHOST_DMA_FAILED_LEN;
  276. cnt = vhost_net_ubuf_put(ubufs);
  277. /*
  278. * Trigger polling thread if guest stopped submitting new buffers:
  279. * in this case, the refcount after decrement will eventually reach 1.
  280. * We also trigger polling periodically after each 16 packets
  281. * (the value 16 here is more or less arbitrary, it's tuned to trigger
  282. * less than 10% of times).
  283. */
  284. if (cnt <= 1 || !(cnt % 16))
  285. vhost_poll_queue(&vq->poll);
  286. rcu_read_unlock_bh();
  287. }
  288. /* Expects to be always run from workqueue - which acts as
  289. * read-size critical section for our kind of RCU. */
  290. static void handle_tx(struct vhost_net *net)
  291. {
  292. struct vhost_net_virtqueue *nvq = &net->vqs[VHOST_NET_VQ_TX];
  293. struct vhost_virtqueue *vq = &nvq->vq;
  294. unsigned out, in, s;
  295. int head;
  296. struct msghdr msg = {
  297. .msg_name = NULL,
  298. .msg_namelen = 0,
  299. .msg_control = NULL,
  300. .msg_controllen = 0,
  301. .msg_flags = MSG_DONTWAIT,
  302. };
  303. size_t len, total_len = 0;
  304. int err;
  305. size_t hdr_size;
  306. struct socket *sock;
  307. struct vhost_net_ubuf_ref *uninitialized_var(ubufs);
  308. bool zcopy, zcopy_used;
  309. mutex_lock(&vq->mutex);
  310. sock = vq->private_data;
  311. if (!sock)
  312. goto out;
  313. vhost_disable_notify(&net->dev, vq);
  314. hdr_size = nvq->vhost_hlen;
  315. zcopy = nvq->ubufs;
  316. for (;;) {
  317. /* Release DMAs done buffers first */
  318. if (zcopy)
  319. vhost_zerocopy_signal_used(net, vq);
  320. /* If more outstanding DMAs, queue the work.
  321. * Handle upend_idx wrap around
  322. */
  323. if (unlikely((nvq->upend_idx + vq->num - VHOST_MAX_PEND)
  324. % UIO_MAXIOV == nvq->done_idx))
  325. break;
  326. head = vhost_get_vq_desc(vq, vq->iov,
  327. ARRAY_SIZE(vq->iov),
  328. &out, &in,
  329. NULL, NULL);
  330. /* On error, stop handling until the next kick. */
  331. if (unlikely(head < 0))
  332. break;
  333. /* Nothing new? Wait for eventfd to tell us they refilled. */
  334. if (head == vq->num) {
  335. if (unlikely(vhost_enable_notify(&net->dev, vq))) {
  336. vhost_disable_notify(&net->dev, vq);
  337. continue;
  338. }
  339. break;
  340. }
  341. if (in) {
  342. vq_err(vq, "Unexpected descriptor format for TX: "
  343. "out %d, int %d\n", out, in);
  344. break;
  345. }
  346. /* Skip header. TODO: support TSO. */
  347. s = move_iovec_hdr(vq->iov, nvq->hdr, hdr_size, out);
  348. len = iov_length(vq->iov, out);
  349. iov_iter_init(&msg.msg_iter, WRITE, vq->iov, out, len);
  350. /* Sanity check */
  351. if (!len) {
  352. vq_err(vq, "Unexpected header len for TX: "
  353. "%zd expected %zd\n",
  354. iov_length(nvq->hdr, s), hdr_size);
  355. break;
  356. }
  357. zcopy_used = zcopy && len >= VHOST_GOODCOPY_LEN
  358. && (nvq->upend_idx + 1) % UIO_MAXIOV !=
  359. nvq->done_idx
  360. && vhost_net_tx_select_zcopy(net);
  361. /* use msg_control to pass vhost zerocopy ubuf info to skb */
  362. if (zcopy_used) {
  363. struct ubuf_info *ubuf;
  364. ubuf = nvq->ubuf_info + nvq->upend_idx;
  365. vq->heads[nvq->upend_idx].id = head;
  366. vq->heads[nvq->upend_idx].len = VHOST_DMA_IN_PROGRESS;
  367. ubuf->callback = vhost_zerocopy_callback;
  368. ubuf->ctx = nvq->ubufs;
  369. ubuf->desc = nvq->upend_idx;
  370. msg.msg_control = ubuf;
  371. msg.msg_controllen = sizeof(ubuf);
  372. ubufs = nvq->ubufs;
  373. atomic_inc(&ubufs->refcount);
  374. nvq->upend_idx = (nvq->upend_idx + 1) % UIO_MAXIOV;
  375. } else {
  376. msg.msg_control = NULL;
  377. ubufs = NULL;
  378. }
  379. /* TODO: Check specific error and bomb out unless ENOBUFS? */
  380. err = sock->ops->sendmsg(NULL, sock, &msg, len);
  381. if (unlikely(err < 0)) {
  382. if (zcopy_used) {
  383. vhost_net_ubuf_put(ubufs);
  384. nvq->upend_idx = ((unsigned)nvq->upend_idx - 1)
  385. % UIO_MAXIOV;
  386. }
  387. vhost_discard_vq_desc(vq, 1);
  388. break;
  389. }
  390. if (err != len)
  391. pr_debug("Truncated TX packet: "
  392. " len %d != %zd\n", err, len);
  393. if (!zcopy_used)
  394. vhost_add_used_and_signal(&net->dev, vq, head, 0);
  395. else
  396. vhost_zerocopy_signal_used(net, vq);
  397. total_len += len;
  398. vhost_net_tx_packet(net);
  399. if (unlikely(total_len >= VHOST_NET_WEIGHT)) {
  400. vhost_poll_queue(&vq->poll);
  401. break;
  402. }
  403. }
  404. out:
  405. mutex_unlock(&vq->mutex);
  406. }
  407. static int peek_head_len(struct sock *sk)
  408. {
  409. struct sk_buff *head;
  410. int len = 0;
  411. unsigned long flags;
  412. spin_lock_irqsave(&sk->sk_receive_queue.lock, flags);
  413. head = skb_peek(&sk->sk_receive_queue);
  414. if (likely(head)) {
  415. len = head->len;
  416. if (vlan_tx_tag_present(head))
  417. len += VLAN_HLEN;
  418. }
  419. spin_unlock_irqrestore(&sk->sk_receive_queue.lock, flags);
  420. return len;
  421. }
  422. /* This is a multi-buffer version of vhost_get_desc, that works if
  423. * vq has read descriptors only.
  424. * @vq - the relevant virtqueue
  425. * @datalen - data length we'll be reading
  426. * @iovcount - returned count of io vectors we fill
  427. * @log - vhost log
  428. * @log_num - log offset
  429. * @quota - headcount quota, 1 for big buffer
  430. * returns number of buffer heads allocated, negative on error
  431. */
  432. static int get_rx_bufs(struct vhost_virtqueue *vq,
  433. struct vring_used_elem *heads,
  434. int datalen,
  435. unsigned *iovcount,
  436. struct vhost_log *log,
  437. unsigned *log_num,
  438. unsigned int quota)
  439. {
  440. unsigned int out, in;
  441. int seg = 0;
  442. int headcount = 0;
  443. unsigned d;
  444. int r, nlogs = 0;
  445. while (datalen > 0 && headcount < quota) {
  446. if (unlikely(seg >= UIO_MAXIOV)) {
  447. r = -ENOBUFS;
  448. goto err;
  449. }
  450. r = vhost_get_vq_desc(vq, vq->iov + seg,
  451. ARRAY_SIZE(vq->iov) - seg, &out,
  452. &in, log, log_num);
  453. if (unlikely(r < 0))
  454. goto err;
  455. d = r;
  456. if (d == vq->num) {
  457. r = 0;
  458. goto err;
  459. }
  460. if (unlikely(out || in <= 0)) {
  461. vq_err(vq, "unexpected descriptor format for RX: "
  462. "out %d, in %d\n", out, in);
  463. r = -EINVAL;
  464. goto err;
  465. }
  466. if (unlikely(log)) {
  467. nlogs += *log_num;
  468. log += *log_num;
  469. }
  470. heads[headcount].id = d;
  471. heads[headcount].len = iov_length(vq->iov + seg, in);
  472. datalen -= heads[headcount].len;
  473. ++headcount;
  474. seg += in;
  475. }
  476. heads[headcount - 1].len += datalen;
  477. *iovcount = seg;
  478. if (unlikely(log))
  479. *log_num = nlogs;
  480. /* Detect overrun */
  481. if (unlikely(datalen > 0)) {
  482. r = UIO_MAXIOV + 1;
  483. goto err;
  484. }
  485. return headcount;
  486. err:
  487. vhost_discard_vq_desc(vq, headcount);
  488. return r;
  489. }
  490. /* Expects to be always run from workqueue - which acts as
  491. * read-size critical section for our kind of RCU. */
  492. static void handle_rx(struct vhost_net *net)
  493. {
  494. struct vhost_net_virtqueue *nvq = &net->vqs[VHOST_NET_VQ_RX];
  495. struct vhost_virtqueue *vq = &nvq->vq;
  496. unsigned uninitialized_var(in), log;
  497. struct vhost_log *vq_log;
  498. struct msghdr msg = {
  499. .msg_name = NULL,
  500. .msg_namelen = 0,
  501. .msg_control = NULL, /* FIXME: get and handle RX aux data. */
  502. .msg_controllen = 0,
  503. .msg_flags = MSG_DONTWAIT,
  504. };
  505. struct virtio_net_hdr_mrg_rxbuf hdr = {
  506. .hdr.flags = 0,
  507. .hdr.gso_type = VIRTIO_NET_HDR_GSO_NONE
  508. };
  509. size_t total_len = 0;
  510. int err, mergeable;
  511. s16 headcount;
  512. size_t vhost_hlen, sock_hlen;
  513. size_t vhost_len, sock_len;
  514. struct socket *sock;
  515. mutex_lock(&vq->mutex);
  516. sock = vq->private_data;
  517. if (!sock)
  518. goto out;
  519. vhost_disable_notify(&net->dev, vq);
  520. vhost_hlen = nvq->vhost_hlen;
  521. sock_hlen = nvq->sock_hlen;
  522. vq_log = unlikely(vhost_has_feature(vq, VHOST_F_LOG_ALL)) ?
  523. vq->log : NULL;
  524. mergeable = vhost_has_feature(vq, VIRTIO_NET_F_MRG_RXBUF);
  525. while ((sock_len = peek_head_len(sock->sk))) {
  526. sock_len += sock_hlen;
  527. vhost_len = sock_len + vhost_hlen;
  528. headcount = get_rx_bufs(vq, vq->heads, vhost_len,
  529. &in, vq_log, &log,
  530. likely(mergeable) ? UIO_MAXIOV : 1);
  531. /* On error, stop handling until the next kick. */
  532. if (unlikely(headcount < 0))
  533. break;
  534. /* On overrun, truncate and discard */
  535. if (unlikely(headcount > UIO_MAXIOV)) {
  536. iov_iter_init(&msg.msg_iter, READ, vq->iov, 1, 1);
  537. err = sock->ops->recvmsg(NULL, sock, &msg,
  538. 1, MSG_DONTWAIT | MSG_TRUNC);
  539. pr_debug("Discarded rx packet: len %zd\n", sock_len);
  540. continue;
  541. }
  542. /* OK, now we need to know about added descriptors. */
  543. if (!headcount) {
  544. if (unlikely(vhost_enable_notify(&net->dev, vq))) {
  545. /* They have slipped one in as we were
  546. * doing that: check again. */
  547. vhost_disable_notify(&net->dev, vq);
  548. continue;
  549. }
  550. /* Nothing new? Wait for eventfd to tell us
  551. * they refilled. */
  552. break;
  553. }
  554. /* We don't need to be notified again. */
  555. if (unlikely((vhost_hlen)))
  556. /* Skip header. TODO: support TSO. */
  557. move_iovec_hdr(vq->iov, nvq->hdr, vhost_hlen, in);
  558. else
  559. /* Copy the header for use in VIRTIO_NET_F_MRG_RXBUF:
  560. * needed because recvmsg can modify msg_iov. */
  561. copy_iovec_hdr(vq->iov, nvq->hdr, sock_hlen, in);
  562. iov_iter_init(&msg.msg_iter, READ, vq->iov, in, sock_len);
  563. err = sock->ops->recvmsg(NULL, sock, &msg,
  564. sock_len, MSG_DONTWAIT | MSG_TRUNC);
  565. /* Userspace might have consumed the packet meanwhile:
  566. * it's not supposed to do this usually, but might be hard
  567. * to prevent. Discard data we got (if any) and keep going. */
  568. if (unlikely(err != sock_len)) {
  569. pr_debug("Discarded rx packet: "
  570. " len %d, expected %zd\n", err, sock_len);
  571. vhost_discard_vq_desc(vq, headcount);
  572. continue;
  573. }
  574. if (unlikely(vhost_hlen) &&
  575. memcpy_toiovecend(nvq->hdr, (unsigned char *)&hdr, 0,
  576. vhost_hlen)) {
  577. vq_err(vq, "Unable to write vnet_hdr at addr %p\n",
  578. vq->iov->iov_base);
  579. break;
  580. }
  581. /* TODO: Should check and handle checksum. */
  582. if (likely(mergeable) &&
  583. memcpy_toiovecend(nvq->hdr, (unsigned char *)&headcount,
  584. offsetof(typeof(hdr), num_buffers),
  585. sizeof hdr.num_buffers)) {
  586. vq_err(vq, "Failed num_buffers write");
  587. vhost_discard_vq_desc(vq, headcount);
  588. break;
  589. }
  590. vhost_add_used_and_signal_n(&net->dev, vq, vq->heads,
  591. headcount);
  592. if (unlikely(vq_log))
  593. vhost_log_write(vq, vq_log, log, vhost_len);
  594. total_len += vhost_len;
  595. if (unlikely(total_len >= VHOST_NET_WEIGHT)) {
  596. vhost_poll_queue(&vq->poll);
  597. break;
  598. }
  599. }
  600. out:
  601. mutex_unlock(&vq->mutex);
  602. }
  603. static void handle_tx_kick(struct vhost_work *work)
  604. {
  605. struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
  606. poll.work);
  607. struct vhost_net *net = container_of(vq->dev, struct vhost_net, dev);
  608. handle_tx(net);
  609. }
  610. static void handle_rx_kick(struct vhost_work *work)
  611. {
  612. struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
  613. poll.work);
  614. struct vhost_net *net = container_of(vq->dev, struct vhost_net, dev);
  615. handle_rx(net);
  616. }
  617. static void handle_tx_net(struct vhost_work *work)
  618. {
  619. struct vhost_net *net = container_of(work, struct vhost_net,
  620. poll[VHOST_NET_VQ_TX].work);
  621. handle_tx(net);
  622. }
  623. static void handle_rx_net(struct vhost_work *work)
  624. {
  625. struct vhost_net *net = container_of(work, struct vhost_net,
  626. poll[VHOST_NET_VQ_RX].work);
  627. handle_rx(net);
  628. }
  629. static int vhost_net_open(struct inode *inode, struct file *f)
  630. {
  631. struct vhost_net *n;
  632. struct vhost_dev *dev;
  633. struct vhost_virtqueue **vqs;
  634. int i;
  635. n = kmalloc(sizeof *n, GFP_KERNEL | __GFP_NOWARN | __GFP_REPEAT);
  636. if (!n) {
  637. n = vmalloc(sizeof *n);
  638. if (!n)
  639. return -ENOMEM;
  640. }
  641. vqs = kmalloc(VHOST_NET_VQ_MAX * sizeof(*vqs), GFP_KERNEL);
  642. if (!vqs) {
  643. kvfree(n);
  644. return -ENOMEM;
  645. }
  646. dev = &n->dev;
  647. vqs[VHOST_NET_VQ_TX] = &n->vqs[VHOST_NET_VQ_TX].vq;
  648. vqs[VHOST_NET_VQ_RX] = &n->vqs[VHOST_NET_VQ_RX].vq;
  649. n->vqs[VHOST_NET_VQ_TX].vq.handle_kick = handle_tx_kick;
  650. n->vqs[VHOST_NET_VQ_RX].vq.handle_kick = handle_rx_kick;
  651. for (i = 0; i < VHOST_NET_VQ_MAX; i++) {
  652. n->vqs[i].ubufs = NULL;
  653. n->vqs[i].ubuf_info = NULL;
  654. n->vqs[i].upend_idx = 0;
  655. n->vqs[i].done_idx = 0;
  656. n->vqs[i].vhost_hlen = 0;
  657. n->vqs[i].sock_hlen = 0;
  658. }
  659. vhost_dev_init(dev, vqs, VHOST_NET_VQ_MAX);
  660. vhost_poll_init(n->poll + VHOST_NET_VQ_TX, handle_tx_net, POLLOUT, dev);
  661. vhost_poll_init(n->poll + VHOST_NET_VQ_RX, handle_rx_net, POLLIN, dev);
  662. f->private_data = n;
  663. return 0;
  664. }
  665. static void vhost_net_disable_vq(struct vhost_net *n,
  666. struct vhost_virtqueue *vq)
  667. {
  668. struct vhost_net_virtqueue *nvq =
  669. container_of(vq, struct vhost_net_virtqueue, vq);
  670. struct vhost_poll *poll = n->poll + (nvq - n->vqs);
  671. if (!vq->private_data)
  672. return;
  673. vhost_poll_stop(poll);
  674. }
  675. static int vhost_net_enable_vq(struct vhost_net *n,
  676. struct vhost_virtqueue *vq)
  677. {
  678. struct vhost_net_virtqueue *nvq =
  679. container_of(vq, struct vhost_net_virtqueue, vq);
  680. struct vhost_poll *poll = n->poll + (nvq - n->vqs);
  681. struct socket *sock;
  682. sock = vq->private_data;
  683. if (!sock)
  684. return 0;
  685. return vhost_poll_start(poll, sock->file);
  686. }
  687. static struct socket *vhost_net_stop_vq(struct vhost_net *n,
  688. struct vhost_virtqueue *vq)
  689. {
  690. struct socket *sock;
  691. mutex_lock(&vq->mutex);
  692. sock = vq->private_data;
  693. vhost_net_disable_vq(n, vq);
  694. vq->private_data = NULL;
  695. mutex_unlock(&vq->mutex);
  696. return sock;
  697. }
  698. static void vhost_net_stop(struct vhost_net *n, struct socket **tx_sock,
  699. struct socket **rx_sock)
  700. {
  701. *tx_sock = vhost_net_stop_vq(n, &n->vqs[VHOST_NET_VQ_TX].vq);
  702. *rx_sock = vhost_net_stop_vq(n, &n->vqs[VHOST_NET_VQ_RX].vq);
  703. }
  704. static void vhost_net_flush_vq(struct vhost_net *n, int index)
  705. {
  706. vhost_poll_flush(n->poll + index);
  707. vhost_poll_flush(&n->vqs[index].vq.poll);
  708. }
  709. static void vhost_net_flush(struct vhost_net *n)
  710. {
  711. vhost_net_flush_vq(n, VHOST_NET_VQ_TX);
  712. vhost_net_flush_vq(n, VHOST_NET_VQ_RX);
  713. if (n->vqs[VHOST_NET_VQ_TX].ubufs) {
  714. mutex_lock(&n->vqs[VHOST_NET_VQ_TX].vq.mutex);
  715. n->tx_flush = true;
  716. mutex_unlock(&n->vqs[VHOST_NET_VQ_TX].vq.mutex);
  717. /* Wait for all lower device DMAs done. */
  718. vhost_net_ubuf_put_and_wait(n->vqs[VHOST_NET_VQ_TX].ubufs);
  719. mutex_lock(&n->vqs[VHOST_NET_VQ_TX].vq.mutex);
  720. n->tx_flush = false;
  721. atomic_set(&n->vqs[VHOST_NET_VQ_TX].ubufs->refcount, 1);
  722. mutex_unlock(&n->vqs[VHOST_NET_VQ_TX].vq.mutex);
  723. }
  724. }
  725. static int vhost_net_release(struct inode *inode, struct file *f)
  726. {
  727. struct vhost_net *n = f->private_data;
  728. struct socket *tx_sock;
  729. struct socket *rx_sock;
  730. vhost_net_stop(n, &tx_sock, &rx_sock);
  731. vhost_net_flush(n);
  732. vhost_dev_stop(&n->dev);
  733. vhost_dev_cleanup(&n->dev, false);
  734. vhost_net_vq_reset(n);
  735. if (tx_sock)
  736. sockfd_put(tx_sock);
  737. if (rx_sock)
  738. sockfd_put(rx_sock);
  739. /* Make sure no callbacks are outstanding */
  740. synchronize_rcu_bh();
  741. /* We do an extra flush before freeing memory,
  742. * since jobs can re-queue themselves. */
  743. vhost_net_flush(n);
  744. kfree(n->dev.vqs);
  745. kvfree(n);
  746. return 0;
  747. }
  748. static struct socket *get_raw_socket(int fd)
  749. {
  750. struct {
  751. struct sockaddr_ll sa;
  752. char buf[MAX_ADDR_LEN];
  753. } uaddr;
  754. int uaddr_len = sizeof uaddr, r;
  755. struct socket *sock = sockfd_lookup(fd, &r);
  756. if (!sock)
  757. return ERR_PTR(-ENOTSOCK);
  758. /* Parameter checking */
  759. if (sock->sk->sk_type != SOCK_RAW) {
  760. r = -ESOCKTNOSUPPORT;
  761. goto err;
  762. }
  763. r = sock->ops->getname(sock, (struct sockaddr *)&uaddr.sa,
  764. &uaddr_len, 0);
  765. if (r)
  766. goto err;
  767. if (uaddr.sa.sll_family != AF_PACKET) {
  768. r = -EPFNOSUPPORT;
  769. goto err;
  770. }
  771. return sock;
  772. err:
  773. sockfd_put(sock);
  774. return ERR_PTR(r);
  775. }
  776. static struct socket *get_tap_socket(int fd)
  777. {
  778. struct file *file = fget(fd);
  779. struct socket *sock;
  780. if (!file)
  781. return ERR_PTR(-EBADF);
  782. sock = tun_get_socket(file);
  783. if (!IS_ERR(sock))
  784. return sock;
  785. sock = macvtap_get_socket(file);
  786. if (IS_ERR(sock))
  787. fput(file);
  788. return sock;
  789. }
  790. static struct socket *get_socket(int fd)
  791. {
  792. struct socket *sock;
  793. /* special case to disable backend */
  794. if (fd == -1)
  795. return NULL;
  796. sock = get_raw_socket(fd);
  797. if (!IS_ERR(sock))
  798. return sock;
  799. sock = get_tap_socket(fd);
  800. if (!IS_ERR(sock))
  801. return sock;
  802. return ERR_PTR(-ENOTSOCK);
  803. }
  804. static long vhost_net_set_backend(struct vhost_net *n, unsigned index, int fd)
  805. {
  806. struct socket *sock, *oldsock;
  807. struct vhost_virtqueue *vq;
  808. struct vhost_net_virtqueue *nvq;
  809. struct vhost_net_ubuf_ref *ubufs, *oldubufs = NULL;
  810. int r;
  811. mutex_lock(&n->dev.mutex);
  812. r = vhost_dev_check_owner(&n->dev);
  813. if (r)
  814. goto err;
  815. if (index >= VHOST_NET_VQ_MAX) {
  816. r = -ENOBUFS;
  817. goto err;
  818. }
  819. vq = &n->vqs[index].vq;
  820. nvq = &n->vqs[index];
  821. mutex_lock(&vq->mutex);
  822. /* Verify that ring has been setup correctly. */
  823. if (!vhost_vq_access_ok(vq)) {
  824. r = -EFAULT;
  825. goto err_vq;
  826. }
  827. sock = get_socket(fd);
  828. if (IS_ERR(sock)) {
  829. r = PTR_ERR(sock);
  830. goto err_vq;
  831. }
  832. /* start polling new socket */
  833. oldsock = vq->private_data;
  834. if (sock != oldsock) {
  835. ubufs = vhost_net_ubuf_alloc(vq,
  836. sock && vhost_sock_zcopy(sock));
  837. if (IS_ERR(ubufs)) {
  838. r = PTR_ERR(ubufs);
  839. goto err_ubufs;
  840. }
  841. vhost_net_disable_vq(n, vq);
  842. vq->private_data = sock;
  843. r = vhost_init_used(vq);
  844. if (r)
  845. goto err_used;
  846. r = vhost_net_enable_vq(n, vq);
  847. if (r)
  848. goto err_used;
  849. oldubufs = nvq->ubufs;
  850. nvq->ubufs = ubufs;
  851. n->tx_packets = 0;
  852. n->tx_zcopy_err = 0;
  853. n->tx_flush = false;
  854. }
  855. mutex_unlock(&vq->mutex);
  856. if (oldubufs) {
  857. vhost_net_ubuf_put_wait_and_free(oldubufs);
  858. mutex_lock(&vq->mutex);
  859. vhost_zerocopy_signal_used(n, vq);
  860. mutex_unlock(&vq->mutex);
  861. }
  862. if (oldsock) {
  863. vhost_net_flush_vq(n, index);
  864. sockfd_put(oldsock);
  865. }
  866. mutex_unlock(&n->dev.mutex);
  867. return 0;
  868. err_used:
  869. vq->private_data = oldsock;
  870. vhost_net_enable_vq(n, vq);
  871. if (ubufs)
  872. vhost_net_ubuf_put_wait_and_free(ubufs);
  873. err_ubufs:
  874. sockfd_put(sock);
  875. err_vq:
  876. mutex_unlock(&vq->mutex);
  877. err:
  878. mutex_unlock(&n->dev.mutex);
  879. return r;
  880. }
  881. static long vhost_net_reset_owner(struct vhost_net *n)
  882. {
  883. struct socket *tx_sock = NULL;
  884. struct socket *rx_sock = NULL;
  885. long err;
  886. struct vhost_memory *memory;
  887. mutex_lock(&n->dev.mutex);
  888. err = vhost_dev_check_owner(&n->dev);
  889. if (err)
  890. goto done;
  891. memory = vhost_dev_reset_owner_prepare();
  892. if (!memory) {
  893. err = -ENOMEM;
  894. goto done;
  895. }
  896. vhost_net_stop(n, &tx_sock, &rx_sock);
  897. vhost_net_flush(n);
  898. vhost_dev_reset_owner(&n->dev, memory);
  899. vhost_net_vq_reset(n);
  900. done:
  901. mutex_unlock(&n->dev.mutex);
  902. if (tx_sock)
  903. sockfd_put(tx_sock);
  904. if (rx_sock)
  905. sockfd_put(rx_sock);
  906. return err;
  907. }
  908. static int vhost_net_set_features(struct vhost_net *n, u64 features)
  909. {
  910. size_t vhost_hlen, sock_hlen, hdr_len;
  911. int i;
  912. hdr_len = (features & (1 << VIRTIO_NET_F_MRG_RXBUF)) ?
  913. sizeof(struct virtio_net_hdr_mrg_rxbuf) :
  914. sizeof(struct virtio_net_hdr);
  915. if (features & (1 << VHOST_NET_F_VIRTIO_NET_HDR)) {
  916. /* vhost provides vnet_hdr */
  917. vhost_hlen = hdr_len;
  918. sock_hlen = 0;
  919. } else {
  920. /* socket provides vnet_hdr */
  921. vhost_hlen = 0;
  922. sock_hlen = hdr_len;
  923. }
  924. mutex_lock(&n->dev.mutex);
  925. if ((features & (1 << VHOST_F_LOG_ALL)) &&
  926. !vhost_log_access_ok(&n->dev)) {
  927. mutex_unlock(&n->dev.mutex);
  928. return -EFAULT;
  929. }
  930. for (i = 0; i < VHOST_NET_VQ_MAX; ++i) {
  931. mutex_lock(&n->vqs[i].vq.mutex);
  932. n->vqs[i].vq.acked_features = features;
  933. n->vqs[i].vhost_hlen = vhost_hlen;
  934. n->vqs[i].sock_hlen = sock_hlen;
  935. mutex_unlock(&n->vqs[i].vq.mutex);
  936. }
  937. mutex_unlock(&n->dev.mutex);
  938. return 0;
  939. }
  940. static long vhost_net_set_owner(struct vhost_net *n)
  941. {
  942. int r;
  943. mutex_lock(&n->dev.mutex);
  944. if (vhost_dev_has_owner(&n->dev)) {
  945. r = -EBUSY;
  946. goto out;
  947. }
  948. r = vhost_net_set_ubuf_info(n);
  949. if (r)
  950. goto out;
  951. r = vhost_dev_set_owner(&n->dev);
  952. if (r)
  953. vhost_net_clear_ubuf_info(n);
  954. vhost_net_flush(n);
  955. out:
  956. mutex_unlock(&n->dev.mutex);
  957. return r;
  958. }
  959. static long vhost_net_ioctl(struct file *f, unsigned int ioctl,
  960. unsigned long arg)
  961. {
  962. struct vhost_net *n = f->private_data;
  963. void __user *argp = (void __user *)arg;
  964. u64 __user *featurep = argp;
  965. struct vhost_vring_file backend;
  966. u64 features;
  967. int r;
  968. switch (ioctl) {
  969. case VHOST_NET_SET_BACKEND:
  970. if (copy_from_user(&backend, argp, sizeof backend))
  971. return -EFAULT;
  972. return vhost_net_set_backend(n, backend.index, backend.fd);
  973. case VHOST_GET_FEATURES:
  974. features = VHOST_NET_FEATURES;
  975. if (copy_to_user(featurep, &features, sizeof features))
  976. return -EFAULT;
  977. return 0;
  978. case VHOST_SET_FEATURES:
  979. if (copy_from_user(&features, featurep, sizeof features))
  980. return -EFAULT;
  981. if (features & ~VHOST_NET_FEATURES)
  982. return -EOPNOTSUPP;
  983. return vhost_net_set_features(n, features);
  984. case VHOST_RESET_OWNER:
  985. return vhost_net_reset_owner(n);
  986. case VHOST_SET_OWNER:
  987. return vhost_net_set_owner(n);
  988. default:
  989. mutex_lock(&n->dev.mutex);
  990. r = vhost_dev_ioctl(&n->dev, ioctl, argp);
  991. if (r == -ENOIOCTLCMD)
  992. r = vhost_vring_ioctl(&n->dev, ioctl, argp);
  993. else
  994. vhost_net_flush(n);
  995. mutex_unlock(&n->dev.mutex);
  996. return r;
  997. }
  998. }
  999. #ifdef CONFIG_COMPAT
  1000. static long vhost_net_compat_ioctl(struct file *f, unsigned int ioctl,
  1001. unsigned long arg)
  1002. {
  1003. return vhost_net_ioctl(f, ioctl, (unsigned long)compat_ptr(arg));
  1004. }
  1005. #endif
  1006. static const struct file_operations vhost_net_fops = {
  1007. .owner = THIS_MODULE,
  1008. .release = vhost_net_release,
  1009. .unlocked_ioctl = vhost_net_ioctl,
  1010. #ifdef CONFIG_COMPAT
  1011. .compat_ioctl = vhost_net_compat_ioctl,
  1012. #endif
  1013. .open = vhost_net_open,
  1014. .llseek = noop_llseek,
  1015. };
  1016. static struct miscdevice vhost_net_misc = {
  1017. .minor = VHOST_NET_MINOR,
  1018. .name = "vhost-net",
  1019. .fops = &vhost_net_fops,
  1020. };
  1021. static int vhost_net_init(void)
  1022. {
  1023. if (experimental_zcopytx)
  1024. vhost_net_enable_zcopy(VHOST_NET_VQ_TX);
  1025. return misc_register(&vhost_net_misc);
  1026. }
  1027. module_init(vhost_net_init);
  1028. static void vhost_net_exit(void)
  1029. {
  1030. misc_deregister(&vhost_net_misc);
  1031. }
  1032. module_exit(vhost_net_exit);
  1033. MODULE_VERSION("0.0.1");
  1034. MODULE_LICENSE("GPL v2");
  1035. MODULE_AUTHOR("Michael S. Tsirkin");
  1036. MODULE_DESCRIPTION("Host kernel accelerator for virtio net");
  1037. MODULE_ALIAS_MISCDEV(VHOST_NET_MINOR);
  1038. MODULE_ALIAS("devname:vhost-net");