net.c 24 KB

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