net.c 28 KB

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