net.c 28 KB

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