net.c 28 KB

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