net.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137
  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. };
  56. enum {
  57. VHOST_NET_VQ_RX = 0,
  58. VHOST_NET_VQ_TX = 1,
  59. VHOST_NET_VQ_MAX = 2,
  60. };
  61. struct vhost_net_ubuf_ref {
  62. /* refcount follows semantics similar to kref:
  63. * 0: object is released
  64. * 1: no outstanding ubufs
  65. * >1: outstanding ubufs
  66. */
  67. atomic_t refcount;
  68. wait_queue_head_t wait;
  69. struct vhost_virtqueue *vq;
  70. };
  71. struct vhost_net_virtqueue {
  72. struct vhost_virtqueue vq;
  73. size_t vhost_hlen;
  74. size_t sock_hlen;
  75. /* vhost zerocopy support fields below: */
  76. /* last used idx for outstanding DMA zerocopy buffers */
  77. int upend_idx;
  78. /* first used idx for DMA done zerocopy buffers */
  79. int done_idx;
  80. /* an array of userspace buffers info */
  81. struct ubuf_info *ubuf_info;
  82. /* Reference counting for outstanding ubufs.
  83. * Protected by vq mutex. Writers must also take device mutex. */
  84. struct vhost_net_ubuf_ref *ubufs;
  85. };
  86. struct vhost_net {
  87. struct vhost_dev dev;
  88. struct vhost_net_virtqueue vqs[VHOST_NET_VQ_MAX];
  89. struct vhost_poll poll[VHOST_NET_VQ_MAX];
  90. /* Number of TX recently submitted.
  91. * Protected by tx vq lock. */
  92. unsigned tx_packets;
  93. /* Number of times zerocopy TX recently failed.
  94. * Protected by tx vq lock. */
  95. unsigned tx_zcopy_err;
  96. /* Flush in progress. Protected by tx vq lock. */
  97. bool tx_flush;
  98. };
  99. static unsigned vhost_net_zcopy_mask __read_mostly;
  100. static void vhost_net_enable_zcopy(int vq)
  101. {
  102. vhost_net_zcopy_mask |= 0x1 << vq;
  103. }
  104. static struct vhost_net_ubuf_ref *
  105. vhost_net_ubuf_alloc(struct vhost_virtqueue *vq, bool zcopy)
  106. {
  107. struct vhost_net_ubuf_ref *ubufs;
  108. /* No zero copy backend? Nothing to count. */
  109. if (!zcopy)
  110. return NULL;
  111. ubufs = kmalloc(sizeof(*ubufs), GFP_KERNEL);
  112. if (!ubufs)
  113. return ERR_PTR(-ENOMEM);
  114. atomic_set(&ubufs->refcount, 1);
  115. init_waitqueue_head(&ubufs->wait);
  116. ubufs->vq = vq;
  117. return ubufs;
  118. }
  119. static int vhost_net_ubuf_put(struct vhost_net_ubuf_ref *ubufs)
  120. {
  121. int r = atomic_sub_return(1, &ubufs->refcount);
  122. if (unlikely(!r))
  123. wake_up(&ubufs->wait);
  124. return r;
  125. }
  126. static void vhost_net_ubuf_put_and_wait(struct vhost_net_ubuf_ref *ubufs)
  127. {
  128. vhost_net_ubuf_put(ubufs);
  129. wait_event(ubufs->wait, !atomic_read(&ubufs->refcount));
  130. }
  131. static void vhost_net_ubuf_put_wait_and_free(struct vhost_net_ubuf_ref *ubufs)
  132. {
  133. vhost_net_ubuf_put_and_wait(ubufs);
  134. kfree(ubufs);
  135. }
  136. static void vhost_net_clear_ubuf_info(struct vhost_net *n)
  137. {
  138. int i;
  139. for (i = 0; i < VHOST_NET_VQ_MAX; ++i) {
  140. kfree(n->vqs[i].ubuf_info);
  141. n->vqs[i].ubuf_info = NULL;
  142. }
  143. }
  144. static int vhost_net_set_ubuf_info(struct vhost_net *n)
  145. {
  146. bool zcopy;
  147. int i;
  148. for (i = 0; i < VHOST_NET_VQ_MAX; ++i) {
  149. zcopy = vhost_net_zcopy_mask & (0x1 << i);
  150. if (!zcopy)
  151. continue;
  152. n->vqs[i].ubuf_info = kmalloc(sizeof(*n->vqs[i].ubuf_info) *
  153. UIO_MAXIOV, GFP_KERNEL);
  154. if (!n->vqs[i].ubuf_info)
  155. goto err;
  156. }
  157. return 0;
  158. err:
  159. vhost_net_clear_ubuf_info(n);
  160. return -ENOMEM;
  161. }
  162. static void vhost_net_vq_reset(struct vhost_net *n)
  163. {
  164. int i;
  165. vhost_net_clear_ubuf_info(n);
  166. for (i = 0; i < VHOST_NET_VQ_MAX; i++) {
  167. n->vqs[i].done_idx = 0;
  168. n->vqs[i].upend_idx = 0;
  169. n->vqs[i].ubufs = NULL;
  170. n->vqs[i].vhost_hlen = 0;
  171. n->vqs[i].sock_hlen = 0;
  172. }
  173. }
  174. static void vhost_net_tx_packet(struct vhost_net *net)
  175. {
  176. ++net->tx_packets;
  177. if (net->tx_packets < 1024)
  178. return;
  179. net->tx_packets = 0;
  180. net->tx_zcopy_err = 0;
  181. }
  182. static void vhost_net_tx_err(struct vhost_net *net)
  183. {
  184. ++net->tx_zcopy_err;
  185. }
  186. static bool vhost_net_tx_select_zcopy(struct vhost_net *net)
  187. {
  188. /* TX flush waits for outstanding DMAs to be done.
  189. * Don't start new DMAs.
  190. */
  191. return !net->tx_flush &&
  192. net->tx_packets / 64 >= net->tx_zcopy_err;
  193. }
  194. static bool vhost_sock_zcopy(struct socket *sock)
  195. {
  196. return unlikely(experimental_zcopytx) &&
  197. sock_flag(sock->sk, SOCK_ZEROCOPY);
  198. }
  199. /* In case of DMA done not in order in lower device driver for some reason.
  200. * upend_idx is used to track end of used idx, done_idx is used to track head
  201. * of used idx. Once lower device DMA done contiguously, we will signal KVM
  202. * guest used idx.
  203. */
  204. static void vhost_zerocopy_signal_used(struct vhost_net *net,
  205. struct vhost_virtqueue *vq)
  206. {
  207. struct vhost_net_virtqueue *nvq =
  208. container_of(vq, struct vhost_net_virtqueue, vq);
  209. int i, add;
  210. int j = 0;
  211. for (i = nvq->done_idx; i != nvq->upend_idx; i = (i + 1) % UIO_MAXIOV) {
  212. if (vq->heads[i].len == VHOST_DMA_FAILED_LEN)
  213. vhost_net_tx_err(net);
  214. if (VHOST_DMA_IS_DONE(vq->heads[i].len)) {
  215. vq->heads[i].len = VHOST_DMA_CLEAR_LEN;
  216. ++j;
  217. } else
  218. break;
  219. }
  220. while (j) {
  221. add = min(UIO_MAXIOV - nvq->done_idx, j);
  222. vhost_add_used_and_signal_n(vq->dev, vq,
  223. &vq->heads[nvq->done_idx], add);
  224. nvq->done_idx = (nvq->done_idx + add) % UIO_MAXIOV;
  225. j -= add;
  226. }
  227. }
  228. static void vhost_zerocopy_callback(struct ubuf_info *ubuf, bool success)
  229. {
  230. struct vhost_net_ubuf_ref *ubufs = ubuf->ctx;
  231. struct vhost_virtqueue *vq = ubufs->vq;
  232. int cnt;
  233. rcu_read_lock_bh();
  234. /* set len to mark this desc buffers done DMA */
  235. vq->heads[ubuf->desc].len = success ?
  236. VHOST_DMA_DONE_LEN : VHOST_DMA_FAILED_LEN;
  237. cnt = vhost_net_ubuf_put(ubufs);
  238. /*
  239. * Trigger polling thread if guest stopped submitting new buffers:
  240. * in this case, the refcount after decrement will eventually reach 1.
  241. * We also trigger polling periodically after each 16 packets
  242. * (the value 16 here is more or less arbitrary, it's tuned to trigger
  243. * less than 10% of times).
  244. */
  245. if (cnt <= 1 || !(cnt % 16))
  246. vhost_poll_queue(&vq->poll);
  247. rcu_read_unlock_bh();
  248. }
  249. /* Expects to be always run from workqueue - which acts as
  250. * read-size critical section for our kind of RCU. */
  251. static void handle_tx(struct vhost_net *net)
  252. {
  253. struct vhost_net_virtqueue *nvq = &net->vqs[VHOST_NET_VQ_TX];
  254. struct vhost_virtqueue *vq = &nvq->vq;
  255. unsigned out, in;
  256. int head;
  257. struct msghdr msg = {
  258. .msg_name = NULL,
  259. .msg_namelen = 0,
  260. .msg_control = NULL,
  261. .msg_controllen = 0,
  262. .msg_flags = MSG_DONTWAIT,
  263. };
  264. size_t len, total_len = 0;
  265. int err;
  266. size_t hdr_size;
  267. struct socket *sock;
  268. struct vhost_net_ubuf_ref *uninitialized_var(ubufs);
  269. bool zcopy, zcopy_used;
  270. mutex_lock(&vq->mutex);
  271. sock = vq->private_data;
  272. if (!sock)
  273. goto out;
  274. vhost_disable_notify(&net->dev, vq);
  275. hdr_size = nvq->vhost_hlen;
  276. zcopy = nvq->ubufs;
  277. for (;;) {
  278. /* Release DMAs done buffers first */
  279. if (zcopy)
  280. vhost_zerocopy_signal_used(net, vq);
  281. /* If more outstanding DMAs, queue the work.
  282. * Handle upend_idx wrap around
  283. */
  284. if (unlikely((nvq->upend_idx + vq->num - VHOST_MAX_PEND)
  285. % UIO_MAXIOV == nvq->done_idx))
  286. break;
  287. head = vhost_get_vq_desc(vq, vq->iov,
  288. ARRAY_SIZE(vq->iov),
  289. &out, &in,
  290. NULL, NULL);
  291. /* On error, stop handling until the next kick. */
  292. if (unlikely(head < 0))
  293. break;
  294. /* Nothing new? Wait for eventfd to tell us they refilled. */
  295. if (head == vq->num) {
  296. if (unlikely(vhost_enable_notify(&net->dev, vq))) {
  297. vhost_disable_notify(&net->dev, vq);
  298. continue;
  299. }
  300. break;
  301. }
  302. if (in) {
  303. vq_err(vq, "Unexpected descriptor format for TX: "
  304. "out %d, int %d\n", out, in);
  305. break;
  306. }
  307. /* Skip header. TODO: support TSO. */
  308. len = iov_length(vq->iov, out);
  309. iov_iter_init(&msg.msg_iter, WRITE, vq->iov, out, len);
  310. iov_iter_advance(&msg.msg_iter, hdr_size);
  311. /* Sanity check */
  312. if (!msg_data_left(&msg)) {
  313. vq_err(vq, "Unexpected header len for TX: "
  314. "%zd expected %zd\n",
  315. len, hdr_size);
  316. break;
  317. }
  318. len = msg_data_left(&msg);
  319. zcopy_used = zcopy && len >= VHOST_GOODCOPY_LEN
  320. && (nvq->upend_idx + 1) % UIO_MAXIOV !=
  321. nvq->done_idx
  322. && vhost_net_tx_select_zcopy(net);
  323. /* use msg_control to pass vhost zerocopy ubuf info to skb */
  324. if (zcopy_used) {
  325. struct ubuf_info *ubuf;
  326. ubuf = nvq->ubuf_info + nvq->upend_idx;
  327. vq->heads[nvq->upend_idx].id = cpu_to_vhost32(vq, head);
  328. vq->heads[nvq->upend_idx].len = VHOST_DMA_IN_PROGRESS;
  329. ubuf->callback = vhost_zerocopy_callback;
  330. ubuf->ctx = nvq->ubufs;
  331. ubuf->desc = nvq->upend_idx;
  332. msg.msg_control = ubuf;
  333. msg.msg_controllen = sizeof(ubuf);
  334. ubufs = nvq->ubufs;
  335. atomic_inc(&ubufs->refcount);
  336. nvq->upend_idx = (nvq->upend_idx + 1) % UIO_MAXIOV;
  337. } else {
  338. msg.msg_control = NULL;
  339. ubufs = NULL;
  340. }
  341. /* TODO: Check specific error and bomb out unless ENOBUFS? */
  342. err = sock->ops->sendmsg(sock, &msg, len);
  343. if (unlikely(err < 0)) {
  344. if (zcopy_used) {
  345. vhost_net_ubuf_put(ubufs);
  346. nvq->upend_idx = ((unsigned)nvq->upend_idx - 1)
  347. % UIO_MAXIOV;
  348. }
  349. vhost_discard_vq_desc(vq, 1);
  350. break;
  351. }
  352. if (err != len)
  353. pr_debug("Truncated TX packet: "
  354. " len %d != %zd\n", err, len);
  355. if (!zcopy_used)
  356. vhost_add_used_and_signal(&net->dev, vq, head, 0);
  357. else
  358. vhost_zerocopy_signal_used(net, vq);
  359. total_len += len;
  360. vhost_net_tx_packet(net);
  361. if (unlikely(total_len >= VHOST_NET_WEIGHT)) {
  362. vhost_poll_queue(&vq->poll);
  363. break;
  364. }
  365. }
  366. out:
  367. mutex_unlock(&vq->mutex);
  368. }
  369. static int peek_head_len(struct sock *sk)
  370. {
  371. struct sk_buff *head;
  372. int len = 0;
  373. unsigned long flags;
  374. spin_lock_irqsave(&sk->sk_receive_queue.lock, flags);
  375. head = skb_peek(&sk->sk_receive_queue);
  376. if (likely(head)) {
  377. len = head->len;
  378. if (skb_vlan_tag_present(head))
  379. len += VLAN_HLEN;
  380. }
  381. spin_unlock_irqrestore(&sk->sk_receive_queue.lock, flags);
  382. return len;
  383. }
  384. /* This is a multi-buffer version of vhost_get_desc, that works if
  385. * vq has read descriptors only.
  386. * @vq - the relevant virtqueue
  387. * @datalen - data length we'll be reading
  388. * @iovcount - returned count of io vectors we fill
  389. * @log - vhost log
  390. * @log_num - log offset
  391. * @quota - headcount quota, 1 for big buffer
  392. * returns number of buffer heads allocated, negative on error
  393. */
  394. static int get_rx_bufs(struct vhost_virtqueue *vq,
  395. struct vring_used_elem *heads,
  396. int datalen,
  397. unsigned *iovcount,
  398. struct vhost_log *log,
  399. unsigned *log_num,
  400. unsigned int quota)
  401. {
  402. unsigned int out, in;
  403. int seg = 0;
  404. int headcount = 0;
  405. unsigned d;
  406. int r, nlogs = 0;
  407. /* len is always initialized before use since we are always called with
  408. * datalen > 0.
  409. */
  410. u32 uninitialized_var(len);
  411. while (datalen > 0 && headcount < quota) {
  412. if (unlikely(seg >= UIO_MAXIOV)) {
  413. r = -ENOBUFS;
  414. goto err;
  415. }
  416. r = vhost_get_vq_desc(vq, vq->iov + seg,
  417. ARRAY_SIZE(vq->iov) - seg, &out,
  418. &in, log, log_num);
  419. if (unlikely(r < 0))
  420. goto err;
  421. d = r;
  422. if (d == vq->num) {
  423. r = 0;
  424. goto err;
  425. }
  426. if (unlikely(out || in <= 0)) {
  427. vq_err(vq, "unexpected descriptor format for RX: "
  428. "out %d, in %d\n", out, in);
  429. r = -EINVAL;
  430. goto err;
  431. }
  432. if (unlikely(log)) {
  433. nlogs += *log_num;
  434. log += *log_num;
  435. }
  436. heads[headcount].id = cpu_to_vhost32(vq, d);
  437. len = iov_length(vq->iov + seg, in);
  438. heads[headcount].len = cpu_to_vhost32(vq, len);
  439. datalen -= len;
  440. ++headcount;
  441. seg += in;
  442. }
  443. heads[headcount - 1].len = cpu_to_vhost32(vq, len + datalen);
  444. *iovcount = seg;
  445. if (unlikely(log))
  446. *log_num = nlogs;
  447. /* Detect overrun */
  448. if (unlikely(datalen > 0)) {
  449. r = UIO_MAXIOV + 1;
  450. goto err;
  451. }
  452. return headcount;
  453. err:
  454. vhost_discard_vq_desc(vq, headcount);
  455. return r;
  456. }
  457. /* Expects to be always run from workqueue - which acts as
  458. * read-size critical section for our kind of RCU. */
  459. static void handle_rx(struct vhost_net *net)
  460. {
  461. struct vhost_net_virtqueue *nvq = &net->vqs[VHOST_NET_VQ_RX];
  462. struct vhost_virtqueue *vq = &nvq->vq;
  463. unsigned uninitialized_var(in), log;
  464. struct vhost_log *vq_log;
  465. struct msghdr msg = {
  466. .msg_name = NULL,
  467. .msg_namelen = 0,
  468. .msg_control = NULL, /* FIXME: get and handle RX aux data. */
  469. .msg_controllen = 0,
  470. .msg_flags = MSG_DONTWAIT,
  471. };
  472. struct virtio_net_hdr hdr = {
  473. .flags = 0,
  474. .gso_type = VIRTIO_NET_HDR_GSO_NONE
  475. };
  476. size_t total_len = 0;
  477. int err, mergeable;
  478. s16 headcount;
  479. size_t vhost_hlen, sock_hlen;
  480. size_t vhost_len, sock_len;
  481. struct socket *sock;
  482. struct iov_iter fixup;
  483. __virtio16 num_buffers;
  484. mutex_lock(&vq->mutex);
  485. sock = vq->private_data;
  486. if (!sock)
  487. goto out;
  488. vhost_disable_notify(&net->dev, vq);
  489. vhost_hlen = nvq->vhost_hlen;
  490. sock_hlen = nvq->sock_hlen;
  491. vq_log = unlikely(vhost_has_feature(vq, VHOST_F_LOG_ALL)) ?
  492. vq->log : NULL;
  493. mergeable = vhost_has_feature(vq, VIRTIO_NET_F_MRG_RXBUF);
  494. while ((sock_len = peek_head_len(sock->sk))) {
  495. sock_len += sock_hlen;
  496. vhost_len = sock_len + vhost_hlen;
  497. headcount = get_rx_bufs(vq, vq->heads, vhost_len,
  498. &in, vq_log, &log,
  499. likely(mergeable) ? UIO_MAXIOV : 1);
  500. /* On error, stop handling until the next kick. */
  501. if (unlikely(headcount < 0))
  502. break;
  503. /* On overrun, truncate and discard */
  504. if (unlikely(headcount > UIO_MAXIOV)) {
  505. iov_iter_init(&msg.msg_iter, READ, vq->iov, 1, 1);
  506. err = sock->ops->recvmsg(sock, &msg,
  507. 1, MSG_DONTWAIT | MSG_TRUNC);
  508. pr_debug("Discarded rx packet: len %zd\n", sock_len);
  509. continue;
  510. }
  511. /* OK, now we need to know about added descriptors. */
  512. if (!headcount) {
  513. if (unlikely(vhost_enable_notify(&net->dev, vq))) {
  514. /* They have slipped one in as we were
  515. * doing that: check again. */
  516. vhost_disable_notify(&net->dev, vq);
  517. continue;
  518. }
  519. /* Nothing new? Wait for eventfd to tell us
  520. * they refilled. */
  521. break;
  522. }
  523. /* We don't need to be notified again. */
  524. iov_iter_init(&msg.msg_iter, READ, vq->iov, in, vhost_len);
  525. fixup = msg.msg_iter;
  526. if (unlikely((vhost_hlen))) {
  527. /* We will supply the header ourselves
  528. * TODO: support TSO.
  529. */
  530. iov_iter_advance(&msg.msg_iter, vhost_hlen);
  531. }
  532. err = sock->ops->recvmsg(sock, &msg,
  533. sock_len, MSG_DONTWAIT | MSG_TRUNC);
  534. /* Userspace might have consumed the packet meanwhile:
  535. * it's not supposed to do this usually, but might be hard
  536. * to prevent. Discard data we got (if any) and keep going. */
  537. if (unlikely(err != sock_len)) {
  538. pr_debug("Discarded rx packet: "
  539. " len %d, expected %zd\n", err, sock_len);
  540. vhost_discard_vq_desc(vq, headcount);
  541. continue;
  542. }
  543. /* Supply virtio_net_hdr if VHOST_NET_F_VIRTIO_NET_HDR */
  544. if (unlikely(vhost_hlen)) {
  545. if (copy_to_iter(&hdr, sizeof(hdr),
  546. &fixup) != sizeof(hdr)) {
  547. vq_err(vq, "Unable to write vnet_hdr "
  548. "at addr %p\n", vq->iov->iov_base);
  549. break;
  550. }
  551. } else {
  552. /* Header came from socket; we'll need to patch
  553. * ->num_buffers over if VIRTIO_NET_F_MRG_RXBUF
  554. */
  555. iov_iter_advance(&fixup, sizeof(hdr));
  556. }
  557. /* TODO: Should check and handle checksum. */
  558. num_buffers = cpu_to_vhost16(vq, headcount);
  559. if (likely(mergeable) &&
  560. copy_to_iter(&num_buffers, sizeof num_buffers,
  561. &fixup) != sizeof num_buffers) {
  562. vq_err(vq, "Failed num_buffers write");
  563. vhost_discard_vq_desc(vq, headcount);
  564. break;
  565. }
  566. vhost_add_used_and_signal_n(&net->dev, vq, vq->heads,
  567. headcount);
  568. if (unlikely(vq_log))
  569. vhost_log_write(vq, vq_log, log, vhost_len);
  570. total_len += vhost_len;
  571. if (unlikely(total_len >= VHOST_NET_WEIGHT)) {
  572. vhost_poll_queue(&vq->poll);
  573. break;
  574. }
  575. }
  576. out:
  577. mutex_unlock(&vq->mutex);
  578. }
  579. static void handle_tx_kick(struct vhost_work *work)
  580. {
  581. struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
  582. poll.work);
  583. struct vhost_net *net = container_of(vq->dev, struct vhost_net, dev);
  584. handle_tx(net);
  585. }
  586. static void handle_rx_kick(struct vhost_work *work)
  587. {
  588. struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
  589. poll.work);
  590. struct vhost_net *net = container_of(vq->dev, struct vhost_net, dev);
  591. handle_rx(net);
  592. }
  593. static void handle_tx_net(struct vhost_work *work)
  594. {
  595. struct vhost_net *net = container_of(work, struct vhost_net,
  596. poll[VHOST_NET_VQ_TX].work);
  597. handle_tx(net);
  598. }
  599. static void handle_rx_net(struct vhost_work *work)
  600. {
  601. struct vhost_net *net = container_of(work, struct vhost_net,
  602. poll[VHOST_NET_VQ_RX].work);
  603. handle_rx(net);
  604. }
  605. static int vhost_net_open(struct inode *inode, struct file *f)
  606. {
  607. struct vhost_net *n;
  608. struct vhost_dev *dev;
  609. struct vhost_virtqueue **vqs;
  610. int i;
  611. n = kmalloc(sizeof *n, GFP_KERNEL | __GFP_NOWARN | __GFP_REPEAT);
  612. if (!n) {
  613. n = vmalloc(sizeof *n);
  614. if (!n)
  615. return -ENOMEM;
  616. }
  617. vqs = kmalloc(VHOST_NET_VQ_MAX * sizeof(*vqs), GFP_KERNEL);
  618. if (!vqs) {
  619. kvfree(n);
  620. return -ENOMEM;
  621. }
  622. dev = &n->dev;
  623. vqs[VHOST_NET_VQ_TX] = &n->vqs[VHOST_NET_VQ_TX].vq;
  624. vqs[VHOST_NET_VQ_RX] = &n->vqs[VHOST_NET_VQ_RX].vq;
  625. n->vqs[VHOST_NET_VQ_TX].vq.handle_kick = handle_tx_kick;
  626. n->vqs[VHOST_NET_VQ_RX].vq.handle_kick = handle_rx_kick;
  627. for (i = 0; i < VHOST_NET_VQ_MAX; i++) {
  628. n->vqs[i].ubufs = NULL;
  629. n->vqs[i].ubuf_info = NULL;
  630. n->vqs[i].upend_idx = 0;
  631. n->vqs[i].done_idx = 0;
  632. n->vqs[i].vhost_hlen = 0;
  633. n->vqs[i].sock_hlen = 0;
  634. }
  635. vhost_dev_init(dev, vqs, VHOST_NET_VQ_MAX);
  636. vhost_poll_init(n->poll + VHOST_NET_VQ_TX, handle_tx_net, POLLOUT, dev);
  637. vhost_poll_init(n->poll + VHOST_NET_VQ_RX, handle_rx_net, POLLIN, dev);
  638. f->private_data = n;
  639. return 0;
  640. }
  641. static void vhost_net_disable_vq(struct vhost_net *n,
  642. struct vhost_virtqueue *vq)
  643. {
  644. struct vhost_net_virtqueue *nvq =
  645. container_of(vq, struct vhost_net_virtqueue, vq);
  646. struct vhost_poll *poll = n->poll + (nvq - n->vqs);
  647. if (!vq->private_data)
  648. return;
  649. vhost_poll_stop(poll);
  650. }
  651. static int vhost_net_enable_vq(struct vhost_net *n,
  652. struct vhost_virtqueue *vq)
  653. {
  654. struct vhost_net_virtqueue *nvq =
  655. container_of(vq, struct vhost_net_virtqueue, vq);
  656. struct vhost_poll *poll = n->poll + (nvq - n->vqs);
  657. struct socket *sock;
  658. sock = vq->private_data;
  659. if (!sock)
  660. return 0;
  661. return vhost_poll_start(poll, sock->file);
  662. }
  663. static struct socket *vhost_net_stop_vq(struct vhost_net *n,
  664. struct vhost_virtqueue *vq)
  665. {
  666. struct socket *sock;
  667. mutex_lock(&vq->mutex);
  668. sock = vq->private_data;
  669. vhost_net_disable_vq(n, vq);
  670. vq->private_data = NULL;
  671. mutex_unlock(&vq->mutex);
  672. return sock;
  673. }
  674. static void vhost_net_stop(struct vhost_net *n, struct socket **tx_sock,
  675. struct socket **rx_sock)
  676. {
  677. *tx_sock = vhost_net_stop_vq(n, &n->vqs[VHOST_NET_VQ_TX].vq);
  678. *rx_sock = vhost_net_stop_vq(n, &n->vqs[VHOST_NET_VQ_RX].vq);
  679. }
  680. static void vhost_net_flush_vq(struct vhost_net *n, int index)
  681. {
  682. vhost_poll_flush(n->poll + index);
  683. vhost_poll_flush(&n->vqs[index].vq.poll);
  684. }
  685. static void vhost_net_flush(struct vhost_net *n)
  686. {
  687. vhost_net_flush_vq(n, VHOST_NET_VQ_TX);
  688. vhost_net_flush_vq(n, VHOST_NET_VQ_RX);
  689. if (n->vqs[VHOST_NET_VQ_TX].ubufs) {
  690. mutex_lock(&n->vqs[VHOST_NET_VQ_TX].vq.mutex);
  691. n->tx_flush = true;
  692. mutex_unlock(&n->vqs[VHOST_NET_VQ_TX].vq.mutex);
  693. /* Wait for all lower device DMAs done. */
  694. vhost_net_ubuf_put_and_wait(n->vqs[VHOST_NET_VQ_TX].ubufs);
  695. mutex_lock(&n->vqs[VHOST_NET_VQ_TX].vq.mutex);
  696. n->tx_flush = false;
  697. atomic_set(&n->vqs[VHOST_NET_VQ_TX].ubufs->refcount, 1);
  698. mutex_unlock(&n->vqs[VHOST_NET_VQ_TX].vq.mutex);
  699. }
  700. }
  701. static int vhost_net_release(struct inode *inode, struct file *f)
  702. {
  703. struct vhost_net *n = f->private_data;
  704. struct socket *tx_sock;
  705. struct socket *rx_sock;
  706. vhost_net_stop(n, &tx_sock, &rx_sock);
  707. vhost_net_flush(n);
  708. vhost_dev_stop(&n->dev);
  709. vhost_dev_cleanup(&n->dev, false);
  710. vhost_net_vq_reset(n);
  711. if (tx_sock)
  712. sockfd_put(tx_sock);
  713. if (rx_sock)
  714. sockfd_put(rx_sock);
  715. /* Make sure no callbacks are outstanding */
  716. synchronize_rcu_bh();
  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. kvfree(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. sockfd_put(sock);
  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. sockfd_put(oldsock);
  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. sockfd_put(sock);
  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. sockfd_put(tx_sock);
  880. if (rx_sock)
  881. sockfd_put(rx_sock);
  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 & ((1ULL << VIRTIO_NET_F_MRG_RXBUF) |
  889. (1ULL << VIRTIO_F_VERSION_1))) ?
  890. sizeof(struct virtio_net_hdr_mrg_rxbuf) :
  891. sizeof(struct virtio_net_hdr);
  892. if (features & (1 << VHOST_NET_F_VIRTIO_NET_HDR)) {
  893. /* vhost provides vnet_hdr */
  894. vhost_hlen = hdr_len;
  895. sock_hlen = 0;
  896. } else {
  897. /* socket provides vnet_hdr */
  898. vhost_hlen = 0;
  899. sock_hlen = hdr_len;
  900. }
  901. mutex_lock(&n->dev.mutex);
  902. if ((features & (1 << VHOST_F_LOG_ALL)) &&
  903. !vhost_log_access_ok(&n->dev)) {
  904. mutex_unlock(&n->dev.mutex);
  905. return -EFAULT;
  906. }
  907. for (i = 0; i < VHOST_NET_VQ_MAX; ++i) {
  908. mutex_lock(&n->vqs[i].vq.mutex);
  909. n->vqs[i].vq.acked_features = features;
  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. mutex_unlock(&n->dev.mutex);
  915. return 0;
  916. }
  917. static long vhost_net_set_owner(struct vhost_net *n)
  918. {
  919. int r;
  920. mutex_lock(&n->dev.mutex);
  921. if (vhost_dev_has_owner(&n->dev)) {
  922. r = -EBUSY;
  923. goto out;
  924. }
  925. r = vhost_net_set_ubuf_info(n);
  926. if (r)
  927. goto out;
  928. r = vhost_dev_set_owner(&n->dev);
  929. if (r)
  930. vhost_net_clear_ubuf_info(n);
  931. vhost_net_flush(n);
  932. out:
  933. mutex_unlock(&n->dev.mutex);
  934. return r;
  935. }
  936. static long vhost_net_ioctl(struct file *f, unsigned int ioctl,
  937. unsigned long arg)
  938. {
  939. struct vhost_net *n = f->private_data;
  940. void __user *argp = (void __user *)arg;
  941. u64 __user *featurep = argp;
  942. struct vhost_vring_file backend;
  943. u64 features;
  944. int r;
  945. switch (ioctl) {
  946. case VHOST_NET_SET_BACKEND:
  947. if (copy_from_user(&backend, argp, sizeof backend))
  948. return -EFAULT;
  949. return vhost_net_set_backend(n, backend.index, backend.fd);
  950. case VHOST_GET_FEATURES:
  951. features = VHOST_NET_FEATURES;
  952. if (copy_to_user(featurep, &features, sizeof features))
  953. return -EFAULT;
  954. return 0;
  955. case VHOST_SET_FEATURES:
  956. if (copy_from_user(&features, featurep, sizeof features))
  957. return -EFAULT;
  958. if (features & ~VHOST_NET_FEATURES)
  959. return -EOPNOTSUPP;
  960. return vhost_net_set_features(n, features);
  961. case VHOST_RESET_OWNER:
  962. return vhost_net_reset_owner(n);
  963. case VHOST_SET_OWNER:
  964. return vhost_net_set_owner(n);
  965. default:
  966. mutex_lock(&n->dev.mutex);
  967. r = vhost_dev_ioctl(&n->dev, ioctl, argp);
  968. if (r == -ENOIOCTLCMD)
  969. r = vhost_vring_ioctl(&n->dev, ioctl, argp);
  970. else
  971. vhost_net_flush(n);
  972. mutex_unlock(&n->dev.mutex);
  973. return r;
  974. }
  975. }
  976. #ifdef CONFIG_COMPAT
  977. static long vhost_net_compat_ioctl(struct file *f, unsigned int ioctl,
  978. unsigned long arg)
  979. {
  980. return vhost_net_ioctl(f, ioctl, (unsigned long)compat_ptr(arg));
  981. }
  982. #endif
  983. static const struct file_operations vhost_net_fops = {
  984. .owner = THIS_MODULE,
  985. .release = vhost_net_release,
  986. .unlocked_ioctl = vhost_net_ioctl,
  987. #ifdef CONFIG_COMPAT
  988. .compat_ioctl = vhost_net_compat_ioctl,
  989. #endif
  990. .open = vhost_net_open,
  991. .llseek = noop_llseek,
  992. };
  993. static struct miscdevice vhost_net_misc = {
  994. .minor = VHOST_NET_MINOR,
  995. .name = "vhost-net",
  996. .fops = &vhost_net_fops,
  997. };
  998. static int vhost_net_init(void)
  999. {
  1000. if (experimental_zcopytx)
  1001. vhost_net_enable_zcopy(VHOST_NET_VQ_TX);
  1002. return misc_register(&vhost_net_misc);
  1003. }
  1004. module_init(vhost_net_init);
  1005. static void vhost_net_exit(void)
  1006. {
  1007. misc_deregister(&vhost_net_misc);
  1008. }
  1009. module_exit(vhost_net_exit);
  1010. MODULE_VERSION("0.0.1");
  1011. MODULE_LICENSE("GPL v2");
  1012. MODULE_AUTHOR("Michael S. Tsirkin");
  1013. MODULE_DESCRIPTION("Host kernel accelerator for virtio net");
  1014. MODULE_ALIAS_MISCDEV(VHOST_NET_MINOR);
  1015. MODULE_ALIAS("devname:vhost-net");