net.c 28 KB

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