net.c 25 KB

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