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