net.c 29 KB

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