net.c 28 KB

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