net.c 25 KB

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