net.c 28 KB

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