net.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156
  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/rcupdate.h>
  18. #include <linux/file.h>
  19. #include <linux/slab.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. struct kref kref;
  63. wait_queue_head_t wait;
  64. struct vhost_virtqueue *vq;
  65. };
  66. struct vhost_net_virtqueue {
  67. struct vhost_virtqueue vq;
  68. /* hdr is used to store the virtio header.
  69. * Since each iovec has >= 1 byte length, we never need more than
  70. * header length entries to store the header. */
  71. struct iovec hdr[sizeof(struct virtio_net_hdr_mrg_rxbuf)];
  72. size_t vhost_hlen;
  73. size_t sock_hlen;
  74. /* vhost zerocopy support fields below: */
  75. /* last used idx for outstanding DMA zerocopy buffers */
  76. int upend_idx;
  77. /* first used idx for DMA done zerocopy buffers */
  78. int done_idx;
  79. /* an array of userspace buffers info */
  80. struct ubuf_info *ubuf_info;
  81. /* Reference counting for outstanding ubufs.
  82. * Protected by vq mutex. Writers must also take device mutex. */
  83. struct vhost_net_ubuf_ref *ubufs;
  84. };
  85. struct vhost_net {
  86. struct vhost_dev dev;
  87. struct vhost_net_virtqueue vqs[VHOST_NET_VQ_MAX];
  88. struct vhost_poll poll[VHOST_NET_VQ_MAX];
  89. /* Number of TX recently submitted.
  90. * Protected by tx vq lock. */
  91. unsigned tx_packets;
  92. /* Number of times zerocopy TX recently failed.
  93. * Protected by tx vq lock. */
  94. unsigned tx_zcopy_err;
  95. /* Flush in progress. Protected by tx vq lock. */
  96. bool tx_flush;
  97. };
  98. static unsigned vhost_net_zcopy_mask __read_mostly;
  99. static void vhost_net_enable_zcopy(int vq)
  100. {
  101. vhost_net_zcopy_mask |= 0x1 << vq;
  102. }
  103. static void vhost_net_zerocopy_done_signal(struct kref *kref)
  104. {
  105. struct vhost_net_ubuf_ref *ubufs;
  106. ubufs = container_of(kref, struct vhost_net_ubuf_ref, kref);
  107. wake_up(&ubufs->wait);
  108. }
  109. static struct vhost_net_ubuf_ref *
  110. vhost_net_ubuf_alloc(struct vhost_virtqueue *vq, bool zcopy)
  111. {
  112. struct vhost_net_ubuf_ref *ubufs;
  113. /* No zero copy backend? Nothing to count. */
  114. if (!zcopy)
  115. return NULL;
  116. ubufs = kmalloc(sizeof(*ubufs), GFP_KERNEL);
  117. if (!ubufs)
  118. return ERR_PTR(-ENOMEM);
  119. kref_init(&ubufs->kref);
  120. init_waitqueue_head(&ubufs->wait);
  121. ubufs->vq = vq;
  122. return ubufs;
  123. }
  124. static void vhost_net_ubuf_put(struct vhost_net_ubuf_ref *ubufs)
  125. {
  126. kref_put(&ubufs->kref, vhost_net_zerocopy_done_signal);
  127. }
  128. static void vhost_net_ubuf_put_and_wait(struct vhost_net_ubuf_ref *ubufs)
  129. {
  130. kref_put(&ubufs->kref, vhost_net_zerocopy_done_signal);
  131. wait_event(ubufs->wait, !atomic_read(&ubufs->kref.refcount));
  132. }
  133. static void vhost_net_ubuf_put_wait_and_free(struct vhost_net_ubuf_ref *ubufs)
  134. {
  135. vhost_net_ubuf_put_and_wait(ubufs);
  136. kfree(ubufs);
  137. }
  138. static void vhost_net_clear_ubuf_info(struct vhost_net *n)
  139. {
  140. int i;
  141. for (i = 0; i < VHOST_NET_VQ_MAX; ++i) {
  142. kfree(n->vqs[i].ubuf_info);
  143. n->vqs[i].ubuf_info = NULL;
  144. }
  145. }
  146. int vhost_net_set_ubuf_info(struct vhost_net *n)
  147. {
  148. bool zcopy;
  149. int i;
  150. for (i = 0; i < VHOST_NET_VQ_MAX; ++i) {
  151. zcopy = vhost_net_zcopy_mask & (0x1 << i);
  152. if (!zcopy)
  153. continue;
  154. n->vqs[i].ubuf_info = kmalloc(sizeof(*n->vqs[i].ubuf_info) *
  155. UIO_MAXIOV, GFP_KERNEL);
  156. if (!n->vqs[i].ubuf_info)
  157. goto err;
  158. }
  159. return 0;
  160. err:
  161. vhost_net_clear_ubuf_info(n);
  162. return -ENOMEM;
  163. }
  164. void vhost_net_vq_reset(struct vhost_net *n)
  165. {
  166. int i;
  167. vhost_net_clear_ubuf_info(n);
  168. for (i = 0; i < VHOST_NET_VQ_MAX; i++) {
  169. n->vqs[i].done_idx = 0;
  170. n->vqs[i].upend_idx = 0;
  171. n->vqs[i].ubufs = NULL;
  172. n->vqs[i].vhost_hlen = 0;
  173. n->vqs[i].sock_hlen = 0;
  174. }
  175. }
  176. static void vhost_net_tx_packet(struct vhost_net *net)
  177. {
  178. ++net->tx_packets;
  179. if (net->tx_packets < 1024)
  180. return;
  181. net->tx_packets = 0;
  182. net->tx_zcopy_err = 0;
  183. }
  184. static void vhost_net_tx_err(struct vhost_net *net)
  185. {
  186. ++net->tx_zcopy_err;
  187. }
  188. static bool vhost_net_tx_select_zcopy(struct vhost_net *net)
  189. {
  190. /* TX flush waits for outstanding DMAs to be done.
  191. * Don't start new DMAs.
  192. */
  193. return !net->tx_flush &&
  194. net->tx_packets / 64 >= net->tx_zcopy_err;
  195. }
  196. static bool vhost_sock_zcopy(struct socket *sock)
  197. {
  198. return unlikely(experimental_zcopytx) &&
  199. sock_flag(sock->sk, SOCK_ZEROCOPY);
  200. }
  201. /* Pop first len bytes from iovec. Return number of segments used. */
  202. static int move_iovec_hdr(struct iovec *from, struct iovec *to,
  203. size_t len, int iov_count)
  204. {
  205. int seg = 0;
  206. size_t size;
  207. while (len && seg < iov_count) {
  208. size = min(from->iov_len, len);
  209. to->iov_base = from->iov_base;
  210. to->iov_len = size;
  211. from->iov_len -= size;
  212. from->iov_base += size;
  213. len -= size;
  214. ++from;
  215. ++to;
  216. ++seg;
  217. }
  218. return seg;
  219. }
  220. /* Copy iovec entries for len bytes from iovec. */
  221. static void copy_iovec_hdr(const struct iovec *from, struct iovec *to,
  222. size_t len, int iovcount)
  223. {
  224. int seg = 0;
  225. size_t size;
  226. while (len && seg < iovcount) {
  227. size = min(from->iov_len, len);
  228. to->iov_base = from->iov_base;
  229. to->iov_len = size;
  230. len -= size;
  231. ++from;
  232. ++to;
  233. ++seg;
  234. }
  235. }
  236. /* In case of DMA done not in order in lower device driver for some reason.
  237. * upend_idx is used to track end of used idx, done_idx is used to track head
  238. * of used idx. Once lower device DMA done contiguously, we will signal KVM
  239. * guest used idx.
  240. */
  241. static int vhost_zerocopy_signal_used(struct vhost_net *net,
  242. struct vhost_virtqueue *vq)
  243. {
  244. struct vhost_net_virtqueue *nvq =
  245. container_of(vq, struct vhost_net_virtqueue, vq);
  246. int i;
  247. int j = 0;
  248. for (i = nvq->done_idx; i != nvq->upend_idx; i = (i + 1) % UIO_MAXIOV) {
  249. if (vq->heads[i].len == VHOST_DMA_FAILED_LEN)
  250. vhost_net_tx_err(net);
  251. if (VHOST_DMA_IS_DONE(vq->heads[i].len)) {
  252. vq->heads[i].len = VHOST_DMA_CLEAR_LEN;
  253. vhost_add_used_and_signal(vq->dev, vq,
  254. vq->heads[i].id, 0);
  255. ++j;
  256. } else
  257. break;
  258. }
  259. if (j)
  260. nvq->done_idx = i;
  261. return j;
  262. }
  263. static void vhost_zerocopy_callback(struct ubuf_info *ubuf, bool success)
  264. {
  265. struct vhost_net_ubuf_ref *ubufs = ubuf->ctx;
  266. struct vhost_virtqueue *vq = ubufs->vq;
  267. int cnt = atomic_read(&ubufs->kref.refcount);
  268. /*
  269. * Trigger polling thread if guest stopped submitting new buffers:
  270. * in this case, the refcount after decrement will eventually reach 1
  271. * so here it is 2.
  272. * We also trigger polling periodically after each 16 packets
  273. * (the value 16 here is more or less arbitrary, it's tuned to trigger
  274. * less than 10% of times).
  275. */
  276. if (cnt <= 2 || !(cnt % 16))
  277. vhost_poll_queue(&vq->poll);
  278. /* set len to mark this desc buffers done DMA */
  279. vq->heads[ubuf->desc].len = success ?
  280. VHOST_DMA_DONE_LEN : VHOST_DMA_FAILED_LEN;
  281. vhost_net_ubuf_put(ubufs);
  282. }
  283. /* Expects to be always run from workqueue - which acts as
  284. * read-size critical section for our kind of RCU. */
  285. static void handle_tx(struct vhost_net *net)
  286. {
  287. struct vhost_net_virtqueue *nvq = &net->vqs[VHOST_NET_VQ_TX];
  288. struct vhost_virtqueue *vq = &nvq->vq;
  289. unsigned out, in, s;
  290. int head;
  291. struct msghdr msg = {
  292. .msg_name = NULL,
  293. .msg_namelen = 0,
  294. .msg_control = NULL,
  295. .msg_controllen = 0,
  296. .msg_iov = vq->iov,
  297. .msg_flags = MSG_DONTWAIT,
  298. };
  299. size_t len, total_len = 0;
  300. int err;
  301. size_t hdr_size;
  302. struct socket *sock;
  303. struct vhost_net_ubuf_ref *uninitialized_var(ubufs);
  304. bool zcopy, zcopy_used;
  305. /* TODO: check that we are running from vhost_worker? */
  306. sock = rcu_dereference_check(vq->private_data, 1);
  307. if (!sock)
  308. return;
  309. mutex_lock(&vq->mutex);
  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. mutex_unlock(&vq->mutex);
  414. }
  415. static int peek_head_len(struct sock *sk)
  416. {
  417. struct sk_buff *head;
  418. int len = 0;
  419. unsigned long flags;
  420. spin_lock_irqsave(&sk->sk_receive_queue.lock, flags);
  421. head = skb_peek(&sk->sk_receive_queue);
  422. if (likely(head)) {
  423. len = head->len;
  424. if (vlan_tx_tag_present(head))
  425. len += VLAN_HLEN;
  426. }
  427. spin_unlock_irqrestore(&sk->sk_receive_queue.lock, flags);
  428. return len;
  429. }
  430. /* This is a multi-buffer version of vhost_get_desc, that works if
  431. * vq has read descriptors only.
  432. * @vq - the relevant virtqueue
  433. * @datalen - data length we'll be reading
  434. * @iovcount - returned count of io vectors we fill
  435. * @log - vhost log
  436. * @log_num - log offset
  437. * @quota - headcount quota, 1 for big buffer
  438. * returns number of buffer heads allocated, negative on error
  439. */
  440. static int get_rx_bufs(struct vhost_virtqueue *vq,
  441. struct vring_used_elem *heads,
  442. int datalen,
  443. unsigned *iovcount,
  444. struct vhost_log *log,
  445. unsigned *log_num,
  446. unsigned int quota)
  447. {
  448. unsigned int out, in;
  449. int seg = 0;
  450. int headcount = 0;
  451. unsigned d;
  452. int r, nlogs = 0;
  453. while (datalen > 0 && headcount < quota) {
  454. if (unlikely(seg >= UIO_MAXIOV)) {
  455. r = -ENOBUFS;
  456. goto err;
  457. }
  458. d = vhost_get_vq_desc(vq->dev, vq, vq->iov + seg,
  459. ARRAY_SIZE(vq->iov) - seg, &out,
  460. &in, log, log_num);
  461. if (d == vq->num) {
  462. r = 0;
  463. goto err;
  464. }
  465. if (unlikely(out || in <= 0)) {
  466. vq_err(vq, "unexpected descriptor format for RX: "
  467. "out %d, in %d\n", out, in);
  468. r = -EINVAL;
  469. goto err;
  470. }
  471. if (unlikely(log)) {
  472. nlogs += *log_num;
  473. log += *log_num;
  474. }
  475. heads[headcount].id = d;
  476. heads[headcount].len = iov_length(vq->iov + seg, in);
  477. datalen -= heads[headcount].len;
  478. ++headcount;
  479. seg += in;
  480. }
  481. heads[headcount - 1].len += datalen;
  482. *iovcount = seg;
  483. if (unlikely(log))
  484. *log_num = nlogs;
  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_iov = vq->iov,
  504. .msg_flags = MSG_DONTWAIT,
  505. };
  506. struct virtio_net_hdr_mrg_rxbuf hdr = {
  507. .hdr.flags = 0,
  508. .hdr.gso_type = VIRTIO_NET_HDR_GSO_NONE
  509. };
  510. size_t total_len = 0;
  511. int err, mergeable;
  512. s16 headcount;
  513. size_t vhost_hlen, sock_hlen;
  514. size_t vhost_len, sock_len;
  515. /* TODO: check that we are running from vhost_worker? */
  516. struct socket *sock = rcu_dereference_check(vq->private_data, 1);
  517. if (!sock)
  518. return;
  519. mutex_lock(&vq->mutex);
  520. vhost_disable_notify(&net->dev, vq);
  521. vhost_hlen = nvq->vhost_hlen;
  522. sock_hlen = nvq->sock_hlen;
  523. vq_log = unlikely(vhost_has_feature(&net->dev, VHOST_F_LOG_ALL)) ?
  524. vq->log : NULL;
  525. mergeable = vhost_has_feature(&net->dev, VIRTIO_NET_F_MRG_RXBUF);
  526. while ((sock_len = peek_head_len(sock->sk))) {
  527. sock_len += sock_hlen;
  528. vhost_len = sock_len + vhost_hlen;
  529. headcount = get_rx_bufs(vq, vq->heads, vhost_len,
  530. &in, vq_log, &log,
  531. likely(mergeable) ? UIO_MAXIOV : 1);
  532. /* On error, stop handling until the next kick. */
  533. if (unlikely(headcount < 0))
  534. break;
  535. /* OK, now we need to know about added descriptors. */
  536. if (!headcount) {
  537. if (unlikely(vhost_enable_notify(&net->dev, vq))) {
  538. /* They have slipped one in as we were
  539. * doing that: check again. */
  540. vhost_disable_notify(&net->dev, vq);
  541. continue;
  542. }
  543. /* Nothing new? Wait for eventfd to tell us
  544. * they refilled. */
  545. break;
  546. }
  547. /* We don't need to be notified again. */
  548. if (unlikely((vhost_hlen)))
  549. /* Skip header. TODO: support TSO. */
  550. move_iovec_hdr(vq->iov, nvq->hdr, vhost_hlen, in);
  551. else
  552. /* Copy the header for use in VIRTIO_NET_F_MRG_RXBUF:
  553. * needed because recvmsg can modify msg_iov. */
  554. copy_iovec_hdr(vq->iov, nvq->hdr, sock_hlen, in);
  555. msg.msg_iovlen = in;
  556. err = sock->ops->recvmsg(NULL, sock, &msg,
  557. sock_len, MSG_DONTWAIT | MSG_TRUNC);
  558. /* Userspace might have consumed the packet meanwhile:
  559. * it's not supposed to do this usually, but might be hard
  560. * to prevent. Discard data we got (if any) and keep going. */
  561. if (unlikely(err != sock_len)) {
  562. pr_debug("Discarded rx packet: "
  563. " len %d, expected %zd\n", err, sock_len);
  564. vhost_discard_vq_desc(vq, headcount);
  565. continue;
  566. }
  567. if (unlikely(vhost_hlen) &&
  568. memcpy_toiovecend(nvq->hdr, (unsigned char *)&hdr, 0,
  569. vhost_hlen)) {
  570. vq_err(vq, "Unable to write vnet_hdr at addr %p\n",
  571. vq->iov->iov_base);
  572. break;
  573. }
  574. /* TODO: Should check and handle checksum. */
  575. if (likely(mergeable) &&
  576. memcpy_toiovecend(nvq->hdr, (unsigned char *)&headcount,
  577. offsetof(typeof(hdr), num_buffers),
  578. sizeof hdr.num_buffers)) {
  579. vq_err(vq, "Failed num_buffers write");
  580. vhost_discard_vq_desc(vq, headcount);
  581. break;
  582. }
  583. vhost_add_used_and_signal_n(&net->dev, vq, vq->heads,
  584. headcount);
  585. if (unlikely(vq_log))
  586. vhost_log_write(vq, vq_log, log, vhost_len);
  587. total_len += vhost_len;
  588. if (unlikely(total_len >= VHOST_NET_WEIGHT)) {
  589. vhost_poll_queue(&vq->poll);
  590. break;
  591. }
  592. }
  593. mutex_unlock(&vq->mutex);
  594. }
  595. static void handle_tx_kick(struct vhost_work *work)
  596. {
  597. struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
  598. poll.work);
  599. struct vhost_net *net = container_of(vq->dev, struct vhost_net, dev);
  600. handle_tx(net);
  601. }
  602. static void handle_rx_kick(struct vhost_work *work)
  603. {
  604. struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
  605. poll.work);
  606. struct vhost_net *net = container_of(vq->dev, struct vhost_net, dev);
  607. handle_rx(net);
  608. }
  609. static void handle_tx_net(struct vhost_work *work)
  610. {
  611. struct vhost_net *net = container_of(work, struct vhost_net,
  612. poll[VHOST_NET_VQ_TX].work);
  613. handle_tx(net);
  614. }
  615. static void handle_rx_net(struct vhost_work *work)
  616. {
  617. struct vhost_net *net = container_of(work, struct vhost_net,
  618. poll[VHOST_NET_VQ_RX].work);
  619. handle_rx(net);
  620. }
  621. static int vhost_net_open(struct inode *inode, struct file *f)
  622. {
  623. struct vhost_net *n = kmalloc(sizeof *n, GFP_KERNEL);
  624. struct vhost_dev *dev;
  625. struct vhost_virtqueue **vqs;
  626. int r, i;
  627. if (!n)
  628. return -ENOMEM;
  629. vqs = kmalloc(VHOST_NET_VQ_MAX * sizeof(*vqs), GFP_KERNEL);
  630. if (!vqs) {
  631. kfree(n);
  632. return -ENOMEM;
  633. }
  634. dev = &n->dev;
  635. vqs[VHOST_NET_VQ_TX] = &n->vqs[VHOST_NET_VQ_TX].vq;
  636. vqs[VHOST_NET_VQ_RX] = &n->vqs[VHOST_NET_VQ_RX].vq;
  637. n->vqs[VHOST_NET_VQ_TX].vq.handle_kick = handle_tx_kick;
  638. n->vqs[VHOST_NET_VQ_RX].vq.handle_kick = handle_rx_kick;
  639. for (i = 0; i < VHOST_NET_VQ_MAX; i++) {
  640. n->vqs[i].ubufs = NULL;
  641. n->vqs[i].ubuf_info = NULL;
  642. n->vqs[i].upend_idx = 0;
  643. n->vqs[i].done_idx = 0;
  644. n->vqs[i].vhost_hlen = 0;
  645. n->vqs[i].sock_hlen = 0;
  646. }
  647. r = vhost_dev_init(dev, vqs, VHOST_NET_VQ_MAX);
  648. if (r < 0) {
  649. kfree(n);
  650. kfree(vqs);
  651. return r;
  652. }
  653. vhost_poll_init(n->poll + VHOST_NET_VQ_TX, handle_tx_net, POLLOUT, dev);
  654. vhost_poll_init(n->poll + VHOST_NET_VQ_RX, handle_rx_net, POLLIN, dev);
  655. f->private_data = n;
  656. return 0;
  657. }
  658. static void vhost_net_disable_vq(struct vhost_net *n,
  659. struct vhost_virtqueue *vq)
  660. {
  661. struct vhost_net_virtqueue *nvq =
  662. container_of(vq, struct vhost_net_virtqueue, vq);
  663. struct vhost_poll *poll = n->poll + (nvq - n->vqs);
  664. if (!vq->private_data)
  665. return;
  666. vhost_poll_stop(poll);
  667. }
  668. static int vhost_net_enable_vq(struct vhost_net *n,
  669. struct vhost_virtqueue *vq)
  670. {
  671. struct vhost_net_virtqueue *nvq =
  672. container_of(vq, struct vhost_net_virtqueue, vq);
  673. struct vhost_poll *poll = n->poll + (nvq - n->vqs);
  674. struct socket *sock;
  675. sock = rcu_dereference_protected(vq->private_data,
  676. lockdep_is_held(&vq->mutex));
  677. if (!sock)
  678. return 0;
  679. return vhost_poll_start(poll, sock->file);
  680. }
  681. static struct socket *vhost_net_stop_vq(struct vhost_net *n,
  682. struct vhost_virtqueue *vq)
  683. {
  684. struct socket *sock;
  685. mutex_lock(&vq->mutex);
  686. sock = rcu_dereference_protected(vq->private_data,
  687. lockdep_is_held(&vq->mutex));
  688. vhost_net_disable_vq(n, vq);
  689. rcu_assign_pointer(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 = rcu_dereference_protected(vq->private_data,
  827. lockdep_is_held(&vq->mutex));
  828. if (sock != oldsock) {
  829. ubufs = vhost_net_ubuf_alloc(vq,
  830. sock && vhost_sock_zcopy(sock));
  831. if (IS_ERR(ubufs)) {
  832. r = PTR_ERR(ubufs);
  833. goto err_ubufs;
  834. }
  835. vhost_net_disable_vq(n, vq);
  836. rcu_assign_pointer(vq->private_data, sock);
  837. r = vhost_init_used(vq);
  838. if (r)
  839. goto err_used;
  840. r = vhost_net_enable_vq(n, vq);
  841. if (r)
  842. goto err_used;
  843. oldubufs = nvq->ubufs;
  844. nvq->ubufs = ubufs;
  845. n->tx_packets = 0;
  846. n->tx_zcopy_err = 0;
  847. n->tx_flush = false;
  848. }
  849. mutex_unlock(&vq->mutex);
  850. if (oldubufs) {
  851. vhost_net_ubuf_put_wait_and_free(oldubufs);
  852. mutex_lock(&vq->mutex);
  853. vhost_zerocopy_signal_used(n, vq);
  854. mutex_unlock(&vq->mutex);
  855. }
  856. if (oldsock) {
  857. vhost_net_flush_vq(n, index);
  858. fput(oldsock->file);
  859. }
  860. mutex_unlock(&n->dev.mutex);
  861. return 0;
  862. err_used:
  863. rcu_assign_pointer(vq->private_data, oldsock);
  864. vhost_net_enable_vq(n, vq);
  865. if (ubufs)
  866. vhost_net_ubuf_put_wait_and_free(ubufs);
  867. err_ubufs:
  868. fput(sock->file);
  869. err_vq:
  870. mutex_unlock(&vq->mutex);
  871. err:
  872. mutex_unlock(&n->dev.mutex);
  873. return r;
  874. }
  875. static long vhost_net_reset_owner(struct vhost_net *n)
  876. {
  877. struct socket *tx_sock = NULL;
  878. struct socket *rx_sock = NULL;
  879. long err;
  880. struct vhost_memory *memory;
  881. mutex_lock(&n->dev.mutex);
  882. err = vhost_dev_check_owner(&n->dev);
  883. if (err)
  884. goto done;
  885. memory = vhost_dev_reset_owner_prepare();
  886. if (!memory) {
  887. err = -ENOMEM;
  888. goto done;
  889. }
  890. vhost_net_stop(n, &tx_sock, &rx_sock);
  891. vhost_net_flush(n);
  892. vhost_dev_reset_owner(&n->dev, memory);
  893. vhost_net_vq_reset(n);
  894. done:
  895. mutex_unlock(&n->dev.mutex);
  896. if (tx_sock)
  897. fput(tx_sock->file);
  898. if (rx_sock)
  899. fput(rx_sock->file);
  900. return err;
  901. }
  902. static int vhost_net_set_features(struct vhost_net *n, u64 features)
  903. {
  904. size_t vhost_hlen, sock_hlen, hdr_len;
  905. int i;
  906. hdr_len = (features & (1 << VIRTIO_NET_F_MRG_RXBUF)) ?
  907. sizeof(struct virtio_net_hdr_mrg_rxbuf) :
  908. sizeof(struct virtio_net_hdr);
  909. if (features & (1 << VHOST_NET_F_VIRTIO_NET_HDR)) {
  910. /* vhost provides vnet_hdr */
  911. vhost_hlen = hdr_len;
  912. sock_hlen = 0;
  913. } else {
  914. /* socket provides vnet_hdr */
  915. vhost_hlen = 0;
  916. sock_hlen = hdr_len;
  917. }
  918. mutex_lock(&n->dev.mutex);
  919. if ((features & (1 << VHOST_F_LOG_ALL)) &&
  920. !vhost_log_access_ok(&n->dev)) {
  921. mutex_unlock(&n->dev.mutex);
  922. return -EFAULT;
  923. }
  924. n->dev.acked_features = features;
  925. smp_wmb();
  926. for (i = 0; i < VHOST_NET_VQ_MAX; ++i) {
  927. mutex_lock(&n->vqs[i].vq.mutex);
  928. n->vqs[i].vhost_hlen = vhost_hlen;
  929. n->vqs[i].sock_hlen = sock_hlen;
  930. mutex_unlock(&n->vqs[i].vq.mutex);
  931. }
  932. vhost_net_flush(n);
  933. mutex_unlock(&n->dev.mutex);
  934. return 0;
  935. }
  936. static long vhost_net_set_owner(struct vhost_net *n)
  937. {
  938. int r;
  939. mutex_lock(&n->dev.mutex);
  940. if (vhost_dev_has_owner(&n->dev)) {
  941. r = -EBUSY;
  942. goto out;
  943. }
  944. r = vhost_net_set_ubuf_info(n);
  945. if (r)
  946. goto out;
  947. r = vhost_dev_set_owner(&n->dev);
  948. if (r)
  949. vhost_net_clear_ubuf_info(n);
  950. vhost_net_flush(n);
  951. out:
  952. mutex_unlock(&n->dev.mutex);
  953. return r;
  954. }
  955. static long vhost_net_ioctl(struct file *f, unsigned int ioctl,
  956. unsigned long arg)
  957. {
  958. struct vhost_net *n = f->private_data;
  959. void __user *argp = (void __user *)arg;
  960. u64 __user *featurep = argp;
  961. struct vhost_vring_file backend;
  962. u64 features;
  963. int r;
  964. switch (ioctl) {
  965. case VHOST_NET_SET_BACKEND:
  966. if (copy_from_user(&backend, argp, sizeof backend))
  967. return -EFAULT;
  968. return vhost_net_set_backend(n, backend.index, backend.fd);
  969. case VHOST_GET_FEATURES:
  970. features = VHOST_NET_FEATURES;
  971. if (copy_to_user(featurep, &features, sizeof features))
  972. return -EFAULT;
  973. return 0;
  974. case VHOST_SET_FEATURES:
  975. if (copy_from_user(&features, featurep, sizeof features))
  976. return -EFAULT;
  977. if (features & ~VHOST_NET_FEATURES)
  978. return -EOPNOTSUPP;
  979. return vhost_net_set_features(n, features);
  980. case VHOST_RESET_OWNER:
  981. return vhost_net_reset_owner(n);
  982. case VHOST_SET_OWNER:
  983. return vhost_net_set_owner(n);
  984. default:
  985. mutex_lock(&n->dev.mutex);
  986. r = vhost_dev_ioctl(&n->dev, ioctl, argp);
  987. if (r == -ENOIOCTLCMD)
  988. r = vhost_vring_ioctl(&n->dev, ioctl, argp);
  989. else
  990. vhost_net_flush(n);
  991. mutex_unlock(&n->dev.mutex);
  992. return r;
  993. }
  994. }
  995. #ifdef CONFIG_COMPAT
  996. static long vhost_net_compat_ioctl(struct file *f, unsigned int ioctl,
  997. unsigned long arg)
  998. {
  999. return vhost_net_ioctl(f, ioctl, (unsigned long)compat_ptr(arg));
  1000. }
  1001. #endif
  1002. static const struct file_operations vhost_net_fops = {
  1003. .owner = THIS_MODULE,
  1004. .release = vhost_net_release,
  1005. .unlocked_ioctl = vhost_net_ioctl,
  1006. #ifdef CONFIG_COMPAT
  1007. .compat_ioctl = vhost_net_compat_ioctl,
  1008. #endif
  1009. .open = vhost_net_open,
  1010. .llseek = noop_llseek,
  1011. };
  1012. static struct miscdevice vhost_net_misc = {
  1013. .minor = VHOST_NET_MINOR,
  1014. .name = "vhost-net",
  1015. .fops = &vhost_net_fops,
  1016. };
  1017. static int vhost_net_init(void)
  1018. {
  1019. if (experimental_zcopytx)
  1020. vhost_net_enable_zcopy(VHOST_NET_VQ_TX);
  1021. return misc_register(&vhost_net_misc);
  1022. }
  1023. module_init(vhost_net_init);
  1024. static void vhost_net_exit(void)
  1025. {
  1026. misc_deregister(&vhost_net_misc);
  1027. }
  1028. module_exit(vhost_net_exit);
  1029. MODULE_VERSION("0.0.1");
  1030. MODULE_LICENSE("GPL v2");
  1031. MODULE_AUTHOR("Michael S. Tsirkin");
  1032. MODULE_DESCRIPTION("Host kernel accelerator for virtio net");
  1033. MODULE_ALIAS_MISCDEV(VHOST_NET_MINOR);
  1034. MODULE_ALIAS("devname:vhost-net");