net.c 24 KB

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