net.c 27 KB

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