net.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147
  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. /* set len to mark this desc buffers done DMA */
  270. vq->heads[ubuf->desc].len = success ?
  271. VHOST_DMA_DONE_LEN : VHOST_DMA_FAILED_LEN;
  272. vhost_net_ubuf_put(ubufs);
  273. /*
  274. * Trigger polling thread if guest stopped submitting new buffers:
  275. * in this case, the refcount after decrement will eventually reach 1
  276. * so here it is 2.
  277. * We also trigger polling periodically after each 16 packets
  278. * (the value 16 here is more or less arbitrary, it's tuned to trigger
  279. * less than 10% of times).
  280. */
  281. if (cnt <= 2 || !(cnt % 16))
  282. vhost_poll_queue(&vq->poll);
  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 + 1) % UIO_MAXIOV !=
  360. nvq->done_idx
  361. && vhost_net_tx_select_zcopy(net);
  362. /* use msg_control to pass vhost zerocopy ubuf info to skb */
  363. if (zcopy_used) {
  364. struct ubuf_info *ubuf;
  365. ubuf = nvq->ubuf_info + nvq->upend_idx;
  366. vq->heads[nvq->upend_idx].id = head;
  367. vq->heads[nvq->upend_idx].len = VHOST_DMA_IN_PROGRESS;
  368. ubuf->callback = vhost_zerocopy_callback;
  369. ubuf->ctx = nvq->ubufs;
  370. ubuf->desc = nvq->upend_idx;
  371. msg.msg_control = ubuf;
  372. msg.msg_controllen = sizeof(ubuf);
  373. ubufs = nvq->ubufs;
  374. kref_get(&ubufs->kref);
  375. nvq->upend_idx = (nvq->upend_idx + 1) % UIO_MAXIOV;
  376. } else {
  377. msg.msg_control = NULL;
  378. ubufs = NULL;
  379. }
  380. /* TODO: Check specific error and bomb out unless ENOBUFS? */
  381. err = sock->ops->sendmsg(NULL, sock, &msg, len);
  382. if (unlikely(err < 0)) {
  383. if (zcopy_used) {
  384. vhost_net_ubuf_put(ubufs);
  385. nvq->upend_idx = ((unsigned)nvq->upend_idx - 1)
  386. % UIO_MAXIOV;
  387. }
  388. vhost_discard_vq_desc(vq, 1);
  389. break;
  390. }
  391. if (err != len)
  392. pr_debug("Truncated TX packet: "
  393. " len %d != %zd\n", err, len);
  394. if (!zcopy_used)
  395. vhost_add_used_and_signal(&net->dev, vq, head, 0);
  396. else
  397. vhost_zerocopy_signal_used(net, vq);
  398. total_len += len;
  399. vhost_net_tx_packet(net);
  400. if (unlikely(total_len >= VHOST_NET_WEIGHT)) {
  401. vhost_poll_queue(&vq->poll);
  402. break;
  403. }
  404. }
  405. out:
  406. mutex_unlock(&vq->mutex);
  407. }
  408. static int peek_head_len(struct sock *sk)
  409. {
  410. struct sk_buff *head;
  411. int len = 0;
  412. unsigned long flags;
  413. spin_lock_irqsave(&sk->sk_receive_queue.lock, flags);
  414. head = skb_peek(&sk->sk_receive_queue);
  415. if (likely(head)) {
  416. len = head->len;
  417. if (vlan_tx_tag_present(head))
  418. len += VLAN_HLEN;
  419. }
  420. spin_unlock_irqrestore(&sk->sk_receive_queue.lock, flags);
  421. return len;
  422. }
  423. /* This is a multi-buffer version of vhost_get_desc, that works if
  424. * vq has read descriptors only.
  425. * @vq - the relevant virtqueue
  426. * @datalen - data length we'll be reading
  427. * @iovcount - returned count of io vectors we fill
  428. * @log - vhost log
  429. * @log_num - log offset
  430. * @quota - headcount quota, 1 for big buffer
  431. * returns number of buffer heads allocated, negative on error
  432. */
  433. static int get_rx_bufs(struct vhost_virtqueue *vq,
  434. struct vring_used_elem *heads,
  435. int datalen,
  436. unsigned *iovcount,
  437. struct vhost_log *log,
  438. unsigned *log_num,
  439. unsigned int quota)
  440. {
  441. unsigned int out, in;
  442. int seg = 0;
  443. int headcount = 0;
  444. unsigned d;
  445. int r, nlogs = 0;
  446. while (datalen > 0 && headcount < quota) {
  447. if (unlikely(seg >= UIO_MAXIOV)) {
  448. r = -ENOBUFS;
  449. goto err;
  450. }
  451. d = vhost_get_vq_desc(vq->dev, vq, vq->iov + seg,
  452. ARRAY_SIZE(vq->iov) - seg, &out,
  453. &in, log, log_num);
  454. if (d == vq->num) {
  455. r = 0;
  456. goto err;
  457. }
  458. if (unlikely(out || in <= 0)) {
  459. vq_err(vq, "unexpected descriptor format for RX: "
  460. "out %d, in %d\n", out, in);
  461. r = -EINVAL;
  462. goto err;
  463. }
  464. if (unlikely(log)) {
  465. nlogs += *log_num;
  466. log += *log_num;
  467. }
  468. heads[headcount].id = d;
  469. heads[headcount].len = iov_length(vq->iov + seg, in);
  470. datalen -= heads[headcount].len;
  471. ++headcount;
  472. seg += in;
  473. }
  474. heads[headcount - 1].len += datalen;
  475. *iovcount = seg;
  476. if (unlikely(log))
  477. *log_num = nlogs;
  478. return headcount;
  479. err:
  480. vhost_discard_vq_desc(vq, headcount);
  481. return r;
  482. }
  483. /* Expects to be always run from workqueue - which acts as
  484. * read-size critical section for our kind of RCU. */
  485. static void handle_rx(struct vhost_net *net)
  486. {
  487. struct vhost_net_virtqueue *nvq = &net->vqs[VHOST_NET_VQ_RX];
  488. struct vhost_virtqueue *vq = &nvq->vq;
  489. unsigned uninitialized_var(in), log;
  490. struct vhost_log *vq_log;
  491. struct msghdr msg = {
  492. .msg_name = NULL,
  493. .msg_namelen = 0,
  494. .msg_control = NULL, /* FIXME: get and handle RX aux data. */
  495. .msg_controllen = 0,
  496. .msg_iov = vq->iov,
  497. .msg_flags = MSG_DONTWAIT,
  498. };
  499. struct virtio_net_hdr_mrg_rxbuf hdr = {
  500. .hdr.flags = 0,
  501. .hdr.gso_type = VIRTIO_NET_HDR_GSO_NONE
  502. };
  503. size_t total_len = 0;
  504. int err, mergeable;
  505. s16 headcount;
  506. size_t vhost_hlen, sock_hlen;
  507. size_t vhost_len, sock_len;
  508. struct socket *sock;
  509. mutex_lock(&vq->mutex);
  510. sock = vq->private_data;
  511. if (!sock)
  512. goto out;
  513. vhost_disable_notify(&net->dev, vq);
  514. vhost_hlen = nvq->vhost_hlen;
  515. sock_hlen = nvq->sock_hlen;
  516. vq_log = unlikely(vhost_has_feature(&net->dev, VHOST_F_LOG_ALL)) ?
  517. vq->log : NULL;
  518. mergeable = vhost_has_feature(&net->dev, VIRTIO_NET_F_MRG_RXBUF);
  519. while ((sock_len = peek_head_len(sock->sk))) {
  520. sock_len += sock_hlen;
  521. vhost_len = sock_len + vhost_hlen;
  522. headcount = get_rx_bufs(vq, vq->heads, vhost_len,
  523. &in, vq_log, &log,
  524. likely(mergeable) ? UIO_MAXIOV : 1);
  525. /* On error, stop handling until the next kick. */
  526. if (unlikely(headcount < 0))
  527. break;
  528. /* OK, now we need to know about added descriptors. */
  529. if (!headcount) {
  530. if (unlikely(vhost_enable_notify(&net->dev, vq))) {
  531. /* They have slipped one in as we were
  532. * doing that: check again. */
  533. vhost_disable_notify(&net->dev, vq);
  534. continue;
  535. }
  536. /* Nothing new? Wait for eventfd to tell us
  537. * they refilled. */
  538. break;
  539. }
  540. /* We don't need to be notified again. */
  541. if (unlikely((vhost_hlen)))
  542. /* Skip header. TODO: support TSO. */
  543. move_iovec_hdr(vq->iov, nvq->hdr, vhost_hlen, in);
  544. else
  545. /* Copy the header for use in VIRTIO_NET_F_MRG_RXBUF:
  546. * needed because recvmsg can modify msg_iov. */
  547. copy_iovec_hdr(vq->iov, nvq->hdr, sock_hlen, in);
  548. msg.msg_iovlen = in;
  549. err = sock->ops->recvmsg(NULL, sock, &msg,
  550. sock_len, MSG_DONTWAIT | MSG_TRUNC);
  551. /* Userspace might have consumed the packet meanwhile:
  552. * it's not supposed to do this usually, but might be hard
  553. * to prevent. Discard data we got (if any) and keep going. */
  554. if (unlikely(err != sock_len)) {
  555. pr_debug("Discarded rx packet: "
  556. " len %d, expected %zd\n", err, sock_len);
  557. vhost_discard_vq_desc(vq, headcount);
  558. continue;
  559. }
  560. if (unlikely(vhost_hlen) &&
  561. memcpy_toiovecend(nvq->hdr, (unsigned char *)&hdr, 0,
  562. vhost_hlen)) {
  563. vq_err(vq, "Unable to write vnet_hdr at addr %p\n",
  564. vq->iov->iov_base);
  565. break;
  566. }
  567. /* TODO: Should check and handle checksum. */
  568. if (likely(mergeable) &&
  569. memcpy_toiovecend(nvq->hdr, (unsigned char *)&headcount,
  570. offsetof(typeof(hdr), num_buffers),
  571. sizeof hdr.num_buffers)) {
  572. vq_err(vq, "Failed num_buffers write");
  573. vhost_discard_vq_desc(vq, headcount);
  574. break;
  575. }
  576. vhost_add_used_and_signal_n(&net->dev, vq, vq->heads,
  577. headcount);
  578. if (unlikely(vq_log))
  579. vhost_log_write(vq, vq_log, log, vhost_len);
  580. total_len += vhost_len;
  581. if (unlikely(total_len >= VHOST_NET_WEIGHT)) {
  582. vhost_poll_queue(&vq->poll);
  583. break;
  584. }
  585. }
  586. out:
  587. mutex_unlock(&vq->mutex);
  588. }
  589. static void handle_tx_kick(struct vhost_work *work)
  590. {
  591. struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
  592. poll.work);
  593. struct vhost_net *net = container_of(vq->dev, struct vhost_net, dev);
  594. handle_tx(net);
  595. }
  596. static void handle_rx_kick(struct vhost_work *work)
  597. {
  598. struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
  599. poll.work);
  600. struct vhost_net *net = container_of(vq->dev, struct vhost_net, dev);
  601. handle_rx(net);
  602. }
  603. static void handle_tx_net(struct vhost_work *work)
  604. {
  605. struct vhost_net *net = container_of(work, struct vhost_net,
  606. poll[VHOST_NET_VQ_TX].work);
  607. handle_tx(net);
  608. }
  609. static void handle_rx_net(struct vhost_work *work)
  610. {
  611. struct vhost_net *net = container_of(work, struct vhost_net,
  612. poll[VHOST_NET_VQ_RX].work);
  613. handle_rx(net);
  614. }
  615. static int vhost_net_open(struct inode *inode, struct file *f)
  616. {
  617. struct vhost_net *n = kmalloc(sizeof *n, GFP_KERNEL);
  618. struct vhost_dev *dev;
  619. struct vhost_virtqueue **vqs;
  620. int r, i;
  621. if (!n)
  622. return -ENOMEM;
  623. vqs = kmalloc(VHOST_NET_VQ_MAX * sizeof(*vqs), GFP_KERNEL);
  624. if (!vqs) {
  625. kfree(n);
  626. return -ENOMEM;
  627. }
  628. dev = &n->dev;
  629. vqs[VHOST_NET_VQ_TX] = &n->vqs[VHOST_NET_VQ_TX].vq;
  630. vqs[VHOST_NET_VQ_RX] = &n->vqs[VHOST_NET_VQ_RX].vq;
  631. n->vqs[VHOST_NET_VQ_TX].vq.handle_kick = handle_tx_kick;
  632. n->vqs[VHOST_NET_VQ_RX].vq.handle_kick = handle_rx_kick;
  633. for (i = 0; i < VHOST_NET_VQ_MAX; i++) {
  634. n->vqs[i].ubufs = NULL;
  635. n->vqs[i].ubuf_info = NULL;
  636. n->vqs[i].upend_idx = 0;
  637. n->vqs[i].done_idx = 0;
  638. n->vqs[i].vhost_hlen = 0;
  639. n->vqs[i].sock_hlen = 0;
  640. }
  641. r = vhost_dev_init(dev, vqs, VHOST_NET_VQ_MAX);
  642. if (r < 0) {
  643. kfree(n);
  644. kfree(vqs);
  645. return r;
  646. }
  647. vhost_poll_init(n->poll + VHOST_NET_VQ_TX, handle_tx_net, POLLOUT, dev);
  648. vhost_poll_init(n->poll + VHOST_NET_VQ_RX, handle_rx_net, POLLIN, dev);
  649. f->private_data = n;
  650. return 0;
  651. }
  652. static void vhost_net_disable_vq(struct vhost_net *n,
  653. struct vhost_virtqueue *vq)
  654. {
  655. struct vhost_net_virtqueue *nvq =
  656. container_of(vq, struct vhost_net_virtqueue, vq);
  657. struct vhost_poll *poll = n->poll + (nvq - n->vqs);
  658. if (!vq->private_data)
  659. return;
  660. vhost_poll_stop(poll);
  661. }
  662. static int vhost_net_enable_vq(struct vhost_net *n,
  663. struct vhost_virtqueue *vq)
  664. {
  665. struct vhost_net_virtqueue *nvq =
  666. container_of(vq, struct vhost_net_virtqueue, vq);
  667. struct vhost_poll *poll = n->poll + (nvq - n->vqs);
  668. struct socket *sock;
  669. sock = vq->private_data;
  670. if (!sock)
  671. return 0;
  672. return vhost_poll_start(poll, sock->file);
  673. }
  674. static struct socket *vhost_net_stop_vq(struct vhost_net *n,
  675. struct vhost_virtqueue *vq)
  676. {
  677. struct socket *sock;
  678. mutex_lock(&vq->mutex);
  679. sock = vq->private_data;
  680. vhost_net_disable_vq(n, vq);
  681. vq->private_data = NULL;
  682. mutex_unlock(&vq->mutex);
  683. return sock;
  684. }
  685. static void vhost_net_stop(struct vhost_net *n, struct socket **tx_sock,
  686. struct socket **rx_sock)
  687. {
  688. *tx_sock = vhost_net_stop_vq(n, &n->vqs[VHOST_NET_VQ_TX].vq);
  689. *rx_sock = vhost_net_stop_vq(n, &n->vqs[VHOST_NET_VQ_RX].vq);
  690. }
  691. static void vhost_net_flush_vq(struct vhost_net *n, int index)
  692. {
  693. vhost_poll_flush(n->poll + index);
  694. vhost_poll_flush(&n->vqs[index].vq.poll);
  695. }
  696. static void vhost_net_flush(struct vhost_net *n)
  697. {
  698. vhost_net_flush_vq(n, VHOST_NET_VQ_TX);
  699. vhost_net_flush_vq(n, VHOST_NET_VQ_RX);
  700. if (n->vqs[VHOST_NET_VQ_TX].ubufs) {
  701. mutex_lock(&n->vqs[VHOST_NET_VQ_TX].vq.mutex);
  702. n->tx_flush = true;
  703. mutex_unlock(&n->vqs[VHOST_NET_VQ_TX].vq.mutex);
  704. /* Wait for all lower device DMAs done. */
  705. vhost_net_ubuf_put_and_wait(n->vqs[VHOST_NET_VQ_TX].ubufs);
  706. mutex_lock(&n->vqs[VHOST_NET_VQ_TX].vq.mutex);
  707. n->tx_flush = false;
  708. kref_init(&n->vqs[VHOST_NET_VQ_TX].ubufs->kref);
  709. mutex_unlock(&n->vqs[VHOST_NET_VQ_TX].vq.mutex);
  710. }
  711. }
  712. static int vhost_net_release(struct inode *inode, struct file *f)
  713. {
  714. struct vhost_net *n = f->private_data;
  715. struct socket *tx_sock;
  716. struct socket *rx_sock;
  717. vhost_net_stop(n, &tx_sock, &rx_sock);
  718. vhost_net_flush(n);
  719. vhost_dev_stop(&n->dev);
  720. vhost_dev_cleanup(&n->dev, false);
  721. vhost_net_vq_reset(n);
  722. if (tx_sock)
  723. fput(tx_sock->file);
  724. if (rx_sock)
  725. fput(rx_sock->file);
  726. /* We do an extra flush before freeing memory,
  727. * since jobs can re-queue themselves. */
  728. vhost_net_flush(n);
  729. kfree(n->dev.vqs);
  730. kfree(n);
  731. return 0;
  732. }
  733. static struct socket *get_raw_socket(int fd)
  734. {
  735. struct {
  736. struct sockaddr_ll sa;
  737. char buf[MAX_ADDR_LEN];
  738. } uaddr;
  739. int uaddr_len = sizeof uaddr, r;
  740. struct socket *sock = sockfd_lookup(fd, &r);
  741. if (!sock)
  742. return ERR_PTR(-ENOTSOCK);
  743. /* Parameter checking */
  744. if (sock->sk->sk_type != SOCK_RAW) {
  745. r = -ESOCKTNOSUPPORT;
  746. goto err;
  747. }
  748. r = sock->ops->getname(sock, (struct sockaddr *)&uaddr.sa,
  749. &uaddr_len, 0);
  750. if (r)
  751. goto err;
  752. if (uaddr.sa.sll_family != AF_PACKET) {
  753. r = -EPFNOSUPPORT;
  754. goto err;
  755. }
  756. return sock;
  757. err:
  758. fput(sock->file);
  759. return ERR_PTR(r);
  760. }
  761. static struct socket *get_tap_socket(int fd)
  762. {
  763. struct file *file = fget(fd);
  764. struct socket *sock;
  765. if (!file)
  766. return ERR_PTR(-EBADF);
  767. sock = tun_get_socket(file);
  768. if (!IS_ERR(sock))
  769. return sock;
  770. sock = macvtap_get_socket(file);
  771. if (IS_ERR(sock))
  772. fput(file);
  773. return sock;
  774. }
  775. static struct socket *get_socket(int fd)
  776. {
  777. struct socket *sock;
  778. /* special case to disable backend */
  779. if (fd == -1)
  780. return NULL;
  781. sock = get_raw_socket(fd);
  782. if (!IS_ERR(sock))
  783. return sock;
  784. sock = get_tap_socket(fd);
  785. if (!IS_ERR(sock))
  786. return sock;
  787. return ERR_PTR(-ENOTSOCK);
  788. }
  789. static long vhost_net_set_backend(struct vhost_net *n, unsigned index, int fd)
  790. {
  791. struct socket *sock, *oldsock;
  792. struct vhost_virtqueue *vq;
  793. struct vhost_net_virtqueue *nvq;
  794. struct vhost_net_ubuf_ref *ubufs, *oldubufs = NULL;
  795. int r;
  796. mutex_lock(&n->dev.mutex);
  797. r = vhost_dev_check_owner(&n->dev);
  798. if (r)
  799. goto err;
  800. if (index >= VHOST_NET_VQ_MAX) {
  801. r = -ENOBUFS;
  802. goto err;
  803. }
  804. vq = &n->vqs[index].vq;
  805. nvq = &n->vqs[index];
  806. mutex_lock(&vq->mutex);
  807. /* Verify that ring has been setup correctly. */
  808. if (!vhost_vq_access_ok(vq)) {
  809. r = -EFAULT;
  810. goto err_vq;
  811. }
  812. sock = get_socket(fd);
  813. if (IS_ERR(sock)) {
  814. r = PTR_ERR(sock);
  815. goto err_vq;
  816. }
  817. /* start polling new socket */
  818. oldsock = vq->private_data;
  819. if (sock != oldsock) {
  820. ubufs = vhost_net_ubuf_alloc(vq,
  821. sock && vhost_sock_zcopy(sock));
  822. if (IS_ERR(ubufs)) {
  823. r = PTR_ERR(ubufs);
  824. goto err_ubufs;
  825. }
  826. vhost_net_disable_vq(n, vq);
  827. vq->private_data = sock;
  828. r = vhost_init_used(vq);
  829. if (r)
  830. goto err_used;
  831. r = vhost_net_enable_vq(n, vq);
  832. if (r)
  833. goto err_used;
  834. oldubufs = nvq->ubufs;
  835. nvq->ubufs = ubufs;
  836. n->tx_packets = 0;
  837. n->tx_zcopy_err = 0;
  838. n->tx_flush = false;
  839. }
  840. mutex_unlock(&vq->mutex);
  841. if (oldubufs) {
  842. vhost_net_ubuf_put_wait_and_free(oldubufs);
  843. mutex_lock(&vq->mutex);
  844. vhost_zerocopy_signal_used(n, vq);
  845. mutex_unlock(&vq->mutex);
  846. }
  847. if (oldsock) {
  848. vhost_net_flush_vq(n, index);
  849. fput(oldsock->file);
  850. }
  851. mutex_unlock(&n->dev.mutex);
  852. return 0;
  853. err_used:
  854. vq->private_data = oldsock;
  855. vhost_net_enable_vq(n, vq);
  856. if (ubufs)
  857. vhost_net_ubuf_put_wait_and_free(ubufs);
  858. err_ubufs:
  859. fput(sock->file);
  860. err_vq:
  861. mutex_unlock(&vq->mutex);
  862. err:
  863. mutex_unlock(&n->dev.mutex);
  864. return r;
  865. }
  866. static long vhost_net_reset_owner(struct vhost_net *n)
  867. {
  868. struct socket *tx_sock = NULL;
  869. struct socket *rx_sock = NULL;
  870. long err;
  871. struct vhost_memory *memory;
  872. mutex_lock(&n->dev.mutex);
  873. err = vhost_dev_check_owner(&n->dev);
  874. if (err)
  875. goto done;
  876. memory = vhost_dev_reset_owner_prepare();
  877. if (!memory) {
  878. err = -ENOMEM;
  879. goto done;
  880. }
  881. vhost_net_stop(n, &tx_sock, &rx_sock);
  882. vhost_net_flush(n);
  883. vhost_dev_reset_owner(&n->dev, memory);
  884. vhost_net_vq_reset(n);
  885. done:
  886. mutex_unlock(&n->dev.mutex);
  887. if (tx_sock)
  888. fput(tx_sock->file);
  889. if (rx_sock)
  890. fput(rx_sock->file);
  891. return err;
  892. }
  893. static int vhost_net_set_features(struct vhost_net *n, u64 features)
  894. {
  895. size_t vhost_hlen, sock_hlen, hdr_len;
  896. int i;
  897. hdr_len = (features & (1 << VIRTIO_NET_F_MRG_RXBUF)) ?
  898. sizeof(struct virtio_net_hdr_mrg_rxbuf) :
  899. sizeof(struct virtio_net_hdr);
  900. if (features & (1 << VHOST_NET_F_VIRTIO_NET_HDR)) {
  901. /* vhost provides vnet_hdr */
  902. vhost_hlen = hdr_len;
  903. sock_hlen = 0;
  904. } else {
  905. /* socket provides vnet_hdr */
  906. vhost_hlen = 0;
  907. sock_hlen = hdr_len;
  908. }
  909. mutex_lock(&n->dev.mutex);
  910. if ((features & (1 << VHOST_F_LOG_ALL)) &&
  911. !vhost_log_access_ok(&n->dev)) {
  912. mutex_unlock(&n->dev.mutex);
  913. return -EFAULT;
  914. }
  915. n->dev.acked_features = features;
  916. smp_wmb();
  917. for (i = 0; i < VHOST_NET_VQ_MAX; ++i) {
  918. mutex_lock(&n->vqs[i].vq.mutex);
  919. n->vqs[i].vhost_hlen = vhost_hlen;
  920. n->vqs[i].sock_hlen = sock_hlen;
  921. mutex_unlock(&n->vqs[i].vq.mutex);
  922. }
  923. vhost_net_flush(n);
  924. mutex_unlock(&n->dev.mutex);
  925. return 0;
  926. }
  927. static long vhost_net_set_owner(struct vhost_net *n)
  928. {
  929. int r;
  930. mutex_lock(&n->dev.mutex);
  931. if (vhost_dev_has_owner(&n->dev)) {
  932. r = -EBUSY;
  933. goto out;
  934. }
  935. r = vhost_net_set_ubuf_info(n);
  936. if (r)
  937. goto out;
  938. r = vhost_dev_set_owner(&n->dev);
  939. if (r)
  940. vhost_net_clear_ubuf_info(n);
  941. vhost_net_flush(n);
  942. out:
  943. mutex_unlock(&n->dev.mutex);
  944. return r;
  945. }
  946. static long vhost_net_ioctl(struct file *f, unsigned int ioctl,
  947. unsigned long arg)
  948. {
  949. struct vhost_net *n = f->private_data;
  950. void __user *argp = (void __user *)arg;
  951. u64 __user *featurep = argp;
  952. struct vhost_vring_file backend;
  953. u64 features;
  954. int r;
  955. switch (ioctl) {
  956. case VHOST_NET_SET_BACKEND:
  957. if (copy_from_user(&backend, argp, sizeof backend))
  958. return -EFAULT;
  959. return vhost_net_set_backend(n, backend.index, backend.fd);
  960. case VHOST_GET_FEATURES:
  961. features = VHOST_NET_FEATURES;
  962. if (copy_to_user(featurep, &features, sizeof features))
  963. return -EFAULT;
  964. return 0;
  965. case VHOST_SET_FEATURES:
  966. if (copy_from_user(&features, featurep, sizeof features))
  967. return -EFAULT;
  968. if (features & ~VHOST_NET_FEATURES)
  969. return -EOPNOTSUPP;
  970. return vhost_net_set_features(n, features);
  971. case VHOST_RESET_OWNER:
  972. return vhost_net_reset_owner(n);
  973. case VHOST_SET_OWNER:
  974. return vhost_net_set_owner(n);
  975. default:
  976. mutex_lock(&n->dev.mutex);
  977. r = vhost_dev_ioctl(&n->dev, ioctl, argp);
  978. if (r == -ENOIOCTLCMD)
  979. r = vhost_vring_ioctl(&n->dev, ioctl, argp);
  980. else
  981. vhost_net_flush(n);
  982. mutex_unlock(&n->dev.mutex);
  983. return r;
  984. }
  985. }
  986. #ifdef CONFIG_COMPAT
  987. static long vhost_net_compat_ioctl(struct file *f, unsigned int ioctl,
  988. unsigned long arg)
  989. {
  990. return vhost_net_ioctl(f, ioctl, (unsigned long)compat_ptr(arg));
  991. }
  992. #endif
  993. static const struct file_operations vhost_net_fops = {
  994. .owner = THIS_MODULE,
  995. .release = vhost_net_release,
  996. .unlocked_ioctl = vhost_net_ioctl,
  997. #ifdef CONFIG_COMPAT
  998. .compat_ioctl = vhost_net_compat_ioctl,
  999. #endif
  1000. .open = vhost_net_open,
  1001. .llseek = noop_llseek,
  1002. };
  1003. static struct miscdevice vhost_net_misc = {
  1004. .minor = VHOST_NET_MINOR,
  1005. .name = "vhost-net",
  1006. .fops = &vhost_net_fops,
  1007. };
  1008. static int vhost_net_init(void)
  1009. {
  1010. if (experimental_zcopytx)
  1011. vhost_net_enable_zcopy(VHOST_NET_VQ_TX);
  1012. return misc_register(&vhost_net_misc);
  1013. }
  1014. module_init(vhost_net_init);
  1015. static void vhost_net_exit(void)
  1016. {
  1017. misc_deregister(&vhost_net_misc);
  1018. }
  1019. module_exit(vhost_net_exit);
  1020. MODULE_VERSION("0.0.1");
  1021. MODULE_LICENSE("GPL v2");
  1022. MODULE_AUTHOR("Michael S. Tsirkin");
  1023. MODULE_DESCRIPTION("Host kernel accelerator for virtio net");
  1024. MODULE_ALIAS_MISCDEV(VHOST_NET_MINOR);
  1025. MODULE_ALIAS("devname:vhost-net");