net.c 28 KB

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