net.c 28 KB

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