net.c 28 KB

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