net.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787
  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/sched/clock.h>
  20. #include <linux/sched/signal.h>
  21. #include <linux/vmalloc.h>
  22. #include <linux/net.h>
  23. #include <linux/if_packet.h>
  24. #include <linux/if_arp.h>
  25. #include <linux/if_tun.h>
  26. #include <linux/if_macvlan.h>
  27. #include <linux/if_tap.h>
  28. #include <linux/if_vlan.h>
  29. #include <linux/skb_array.h>
  30. #include <linux/skbuff.h>
  31. #include <net/sock.h>
  32. #include <net/xdp.h>
  33. #include "vhost.h"
  34. static int experimental_zcopytx = 1;
  35. module_param(experimental_zcopytx, int, 0444);
  36. MODULE_PARM_DESC(experimental_zcopytx, "Enable Zero Copy TX;"
  37. " 1 -Enable; 0 - Disable");
  38. /* Max number of bytes transferred before requeueing the job.
  39. * Using this limit prevents one virtqueue from starving others. */
  40. #define VHOST_NET_WEIGHT 0x80000
  41. /* Max number of packets transferred before requeueing the job.
  42. * Using this limit prevents one virtqueue from starving others with small
  43. * pkts.
  44. */
  45. #define VHOST_NET_PKT_WEIGHT 256
  46. /* MAX number of TX used buffers for outstanding zerocopy */
  47. #define VHOST_MAX_PEND 128
  48. #define VHOST_GOODCOPY_LEN 256
  49. /*
  50. * For transmit, used buffer len is unused; we override it to track buffer
  51. * status internally; used for zerocopy tx only.
  52. */
  53. /* Lower device DMA failed */
  54. #define VHOST_DMA_FAILED_LEN ((__force __virtio32)3)
  55. /* Lower device DMA done */
  56. #define VHOST_DMA_DONE_LEN ((__force __virtio32)2)
  57. /* Lower device DMA in progress */
  58. #define VHOST_DMA_IN_PROGRESS ((__force __virtio32)1)
  59. /* Buffer unused */
  60. #define VHOST_DMA_CLEAR_LEN ((__force __virtio32)0)
  61. #define VHOST_DMA_IS_DONE(len) ((__force u32)(len) >= (__force u32)VHOST_DMA_DONE_LEN)
  62. enum {
  63. VHOST_NET_FEATURES = VHOST_FEATURES |
  64. (1ULL << VHOST_NET_F_VIRTIO_NET_HDR) |
  65. (1ULL << VIRTIO_NET_F_MRG_RXBUF) |
  66. (1ULL << VIRTIO_F_IOMMU_PLATFORM)
  67. };
  68. enum {
  69. VHOST_NET_BACKEND_FEATURES = (1ULL << VHOST_BACKEND_F_IOTLB_MSG_V2)
  70. };
  71. enum {
  72. VHOST_NET_VQ_RX = 0,
  73. VHOST_NET_VQ_TX = 1,
  74. VHOST_NET_VQ_MAX = 2,
  75. };
  76. struct vhost_net_ubuf_ref {
  77. /* refcount follows semantics similar to kref:
  78. * 0: object is released
  79. * 1: no outstanding ubufs
  80. * >1: outstanding ubufs
  81. */
  82. atomic_t refcount;
  83. wait_queue_head_t wait;
  84. struct vhost_virtqueue *vq;
  85. };
  86. #define VHOST_NET_BATCH 64
  87. struct vhost_net_buf {
  88. void **queue;
  89. int tail;
  90. int head;
  91. };
  92. struct vhost_net_virtqueue {
  93. struct vhost_virtqueue vq;
  94. size_t vhost_hlen;
  95. size_t sock_hlen;
  96. /* vhost zerocopy support fields below: */
  97. /* last used idx for outstanding DMA zerocopy buffers */
  98. int upend_idx;
  99. /* For TX, first used idx for DMA done zerocopy buffers
  100. * For RX, number of batched heads
  101. */
  102. int done_idx;
  103. /* Number of XDP frames batched */
  104. int batched_xdp;
  105. /* an array of userspace buffers info */
  106. struct ubuf_info *ubuf_info;
  107. /* Reference counting for outstanding ubufs.
  108. * Protected by vq mutex. Writers must also take device mutex. */
  109. struct vhost_net_ubuf_ref *ubufs;
  110. struct ptr_ring *rx_ring;
  111. struct vhost_net_buf rxq;
  112. /* Batched XDP buffs */
  113. struct xdp_buff *xdp;
  114. };
  115. struct vhost_net {
  116. struct vhost_dev dev;
  117. struct vhost_net_virtqueue vqs[VHOST_NET_VQ_MAX];
  118. struct vhost_poll poll[VHOST_NET_VQ_MAX];
  119. /* Number of TX recently submitted.
  120. * Protected by tx vq lock. */
  121. unsigned tx_packets;
  122. /* Number of times zerocopy TX recently failed.
  123. * Protected by tx vq lock. */
  124. unsigned tx_zcopy_err;
  125. /* Flush in progress. Protected by tx vq lock. */
  126. bool tx_flush;
  127. };
  128. static unsigned vhost_net_zcopy_mask __read_mostly;
  129. static void *vhost_net_buf_get_ptr(struct vhost_net_buf *rxq)
  130. {
  131. if (rxq->tail != rxq->head)
  132. return rxq->queue[rxq->head];
  133. else
  134. return NULL;
  135. }
  136. static int vhost_net_buf_get_size(struct vhost_net_buf *rxq)
  137. {
  138. return rxq->tail - rxq->head;
  139. }
  140. static int vhost_net_buf_is_empty(struct vhost_net_buf *rxq)
  141. {
  142. return rxq->tail == rxq->head;
  143. }
  144. static void *vhost_net_buf_consume(struct vhost_net_buf *rxq)
  145. {
  146. void *ret = vhost_net_buf_get_ptr(rxq);
  147. ++rxq->head;
  148. return ret;
  149. }
  150. static int vhost_net_buf_produce(struct vhost_net_virtqueue *nvq)
  151. {
  152. struct vhost_net_buf *rxq = &nvq->rxq;
  153. rxq->head = 0;
  154. rxq->tail = ptr_ring_consume_batched(nvq->rx_ring, rxq->queue,
  155. VHOST_NET_BATCH);
  156. return rxq->tail;
  157. }
  158. static void vhost_net_buf_unproduce(struct vhost_net_virtqueue *nvq)
  159. {
  160. struct vhost_net_buf *rxq = &nvq->rxq;
  161. if (nvq->rx_ring && !vhost_net_buf_is_empty(rxq)) {
  162. ptr_ring_unconsume(nvq->rx_ring, rxq->queue + rxq->head,
  163. vhost_net_buf_get_size(rxq),
  164. tun_ptr_free);
  165. rxq->head = rxq->tail = 0;
  166. }
  167. }
  168. static int vhost_net_buf_peek_len(void *ptr)
  169. {
  170. if (tun_is_xdp_frame(ptr)) {
  171. struct xdp_frame *xdpf = tun_ptr_to_xdp(ptr);
  172. return xdpf->len;
  173. }
  174. return __skb_array_len_with_tag(ptr);
  175. }
  176. static int vhost_net_buf_peek(struct vhost_net_virtqueue *nvq)
  177. {
  178. struct vhost_net_buf *rxq = &nvq->rxq;
  179. if (!vhost_net_buf_is_empty(rxq))
  180. goto out;
  181. if (!vhost_net_buf_produce(nvq))
  182. return 0;
  183. out:
  184. return vhost_net_buf_peek_len(vhost_net_buf_get_ptr(rxq));
  185. }
  186. static void vhost_net_buf_init(struct vhost_net_buf *rxq)
  187. {
  188. rxq->head = rxq->tail = 0;
  189. }
  190. static void vhost_net_enable_zcopy(int vq)
  191. {
  192. vhost_net_zcopy_mask |= 0x1 << vq;
  193. }
  194. static struct vhost_net_ubuf_ref *
  195. vhost_net_ubuf_alloc(struct vhost_virtqueue *vq, bool zcopy)
  196. {
  197. struct vhost_net_ubuf_ref *ubufs;
  198. /* No zero copy backend? Nothing to count. */
  199. if (!zcopy)
  200. return NULL;
  201. ubufs = kmalloc(sizeof(*ubufs), GFP_KERNEL);
  202. if (!ubufs)
  203. return ERR_PTR(-ENOMEM);
  204. atomic_set(&ubufs->refcount, 1);
  205. init_waitqueue_head(&ubufs->wait);
  206. ubufs->vq = vq;
  207. return ubufs;
  208. }
  209. static int vhost_net_ubuf_put(struct vhost_net_ubuf_ref *ubufs)
  210. {
  211. int r = atomic_sub_return(1, &ubufs->refcount);
  212. if (unlikely(!r))
  213. wake_up(&ubufs->wait);
  214. return r;
  215. }
  216. static void vhost_net_ubuf_put_and_wait(struct vhost_net_ubuf_ref *ubufs)
  217. {
  218. vhost_net_ubuf_put(ubufs);
  219. wait_event(ubufs->wait, !atomic_read(&ubufs->refcount));
  220. }
  221. static void vhost_net_ubuf_put_wait_and_free(struct vhost_net_ubuf_ref *ubufs)
  222. {
  223. vhost_net_ubuf_put_and_wait(ubufs);
  224. kfree(ubufs);
  225. }
  226. static void vhost_net_clear_ubuf_info(struct vhost_net *n)
  227. {
  228. int i;
  229. for (i = 0; i < VHOST_NET_VQ_MAX; ++i) {
  230. kfree(n->vqs[i].ubuf_info);
  231. n->vqs[i].ubuf_info = NULL;
  232. }
  233. }
  234. static int vhost_net_set_ubuf_info(struct vhost_net *n)
  235. {
  236. bool zcopy;
  237. int i;
  238. for (i = 0; i < VHOST_NET_VQ_MAX; ++i) {
  239. zcopy = vhost_net_zcopy_mask & (0x1 << i);
  240. if (!zcopy)
  241. continue;
  242. n->vqs[i].ubuf_info =
  243. kmalloc_array(UIO_MAXIOV,
  244. sizeof(*n->vqs[i].ubuf_info),
  245. GFP_KERNEL);
  246. if (!n->vqs[i].ubuf_info)
  247. goto err;
  248. }
  249. return 0;
  250. err:
  251. vhost_net_clear_ubuf_info(n);
  252. return -ENOMEM;
  253. }
  254. static void vhost_net_vq_reset(struct vhost_net *n)
  255. {
  256. int i;
  257. vhost_net_clear_ubuf_info(n);
  258. for (i = 0; i < VHOST_NET_VQ_MAX; i++) {
  259. n->vqs[i].done_idx = 0;
  260. n->vqs[i].upend_idx = 0;
  261. n->vqs[i].ubufs = NULL;
  262. n->vqs[i].vhost_hlen = 0;
  263. n->vqs[i].sock_hlen = 0;
  264. vhost_net_buf_init(&n->vqs[i].rxq);
  265. }
  266. }
  267. static void vhost_net_tx_packet(struct vhost_net *net)
  268. {
  269. ++net->tx_packets;
  270. if (net->tx_packets < 1024)
  271. return;
  272. net->tx_packets = 0;
  273. net->tx_zcopy_err = 0;
  274. }
  275. static void vhost_net_tx_err(struct vhost_net *net)
  276. {
  277. ++net->tx_zcopy_err;
  278. }
  279. static bool vhost_net_tx_select_zcopy(struct vhost_net *net)
  280. {
  281. /* TX flush waits for outstanding DMAs to be done.
  282. * Don't start new DMAs.
  283. */
  284. return !net->tx_flush &&
  285. net->tx_packets / 64 >= net->tx_zcopy_err;
  286. }
  287. static bool vhost_sock_zcopy(struct socket *sock)
  288. {
  289. return unlikely(experimental_zcopytx) &&
  290. sock_flag(sock->sk, SOCK_ZEROCOPY);
  291. }
  292. static bool vhost_sock_xdp(struct socket *sock)
  293. {
  294. return sock_flag(sock->sk, SOCK_XDP);
  295. }
  296. /* In case of DMA done not in order in lower device driver for some reason.
  297. * upend_idx is used to track end of used idx, done_idx is used to track head
  298. * of used idx. Once lower device DMA done contiguously, we will signal KVM
  299. * guest used idx.
  300. */
  301. static void vhost_zerocopy_signal_used(struct vhost_net *net,
  302. struct vhost_virtqueue *vq)
  303. {
  304. struct vhost_net_virtqueue *nvq =
  305. container_of(vq, struct vhost_net_virtqueue, vq);
  306. int i, add;
  307. int j = 0;
  308. for (i = nvq->done_idx; i != nvq->upend_idx; i = (i + 1) % UIO_MAXIOV) {
  309. if (vq->heads[i].len == VHOST_DMA_FAILED_LEN)
  310. vhost_net_tx_err(net);
  311. if (VHOST_DMA_IS_DONE(vq->heads[i].len)) {
  312. vq->heads[i].len = VHOST_DMA_CLEAR_LEN;
  313. ++j;
  314. } else
  315. break;
  316. }
  317. while (j) {
  318. add = min(UIO_MAXIOV - nvq->done_idx, j);
  319. vhost_add_used_and_signal_n(vq->dev, vq,
  320. &vq->heads[nvq->done_idx], add);
  321. nvq->done_idx = (nvq->done_idx + add) % UIO_MAXIOV;
  322. j -= add;
  323. }
  324. }
  325. static void vhost_zerocopy_callback(struct ubuf_info *ubuf, bool success)
  326. {
  327. struct vhost_net_ubuf_ref *ubufs = ubuf->ctx;
  328. struct vhost_virtqueue *vq = ubufs->vq;
  329. int cnt;
  330. rcu_read_lock_bh();
  331. /* set len to mark this desc buffers done DMA */
  332. vq->heads[ubuf->desc].len = success ?
  333. VHOST_DMA_DONE_LEN : VHOST_DMA_FAILED_LEN;
  334. cnt = vhost_net_ubuf_put(ubufs);
  335. /*
  336. * Trigger polling thread if guest stopped submitting new buffers:
  337. * in this case, the refcount after decrement will eventually reach 1.
  338. * We also trigger polling periodically after each 16 packets
  339. * (the value 16 here is more or less arbitrary, it's tuned to trigger
  340. * less than 10% of times).
  341. */
  342. if (cnt <= 1 || !(cnt % 16))
  343. vhost_poll_queue(&vq->poll);
  344. rcu_read_unlock_bh();
  345. }
  346. static inline unsigned long busy_clock(void)
  347. {
  348. return local_clock() >> 10;
  349. }
  350. static bool vhost_can_busy_poll(unsigned long endtime)
  351. {
  352. return likely(!need_resched() && !time_after(busy_clock(), endtime) &&
  353. !signal_pending(current));
  354. }
  355. static void vhost_net_disable_vq(struct vhost_net *n,
  356. struct vhost_virtqueue *vq)
  357. {
  358. struct vhost_net_virtqueue *nvq =
  359. container_of(vq, struct vhost_net_virtqueue, vq);
  360. struct vhost_poll *poll = n->poll + (nvq - n->vqs);
  361. if (!vq->private_data)
  362. return;
  363. vhost_poll_stop(poll);
  364. }
  365. static int vhost_net_enable_vq(struct vhost_net *n,
  366. struct vhost_virtqueue *vq)
  367. {
  368. struct vhost_net_virtqueue *nvq =
  369. container_of(vq, struct vhost_net_virtqueue, vq);
  370. struct vhost_poll *poll = n->poll + (nvq - n->vqs);
  371. struct socket *sock;
  372. sock = vq->private_data;
  373. if (!sock)
  374. return 0;
  375. return vhost_poll_start(poll, sock->file);
  376. }
  377. static void vhost_net_signal_used(struct vhost_net_virtqueue *nvq)
  378. {
  379. struct vhost_virtqueue *vq = &nvq->vq;
  380. struct vhost_dev *dev = vq->dev;
  381. if (!nvq->done_idx)
  382. return;
  383. vhost_add_used_and_signal_n(dev, vq, vq->heads, nvq->done_idx);
  384. nvq->done_idx = 0;
  385. }
  386. static void vhost_tx_batch(struct vhost_net *net,
  387. struct vhost_net_virtqueue *nvq,
  388. struct socket *sock,
  389. struct msghdr *msghdr)
  390. {
  391. struct tun_msg_ctl ctl = {
  392. .type = TUN_MSG_PTR,
  393. .num = nvq->batched_xdp,
  394. .ptr = nvq->xdp,
  395. };
  396. int err;
  397. if (nvq->batched_xdp == 0)
  398. goto signal_used;
  399. msghdr->msg_control = &ctl;
  400. err = sock->ops->sendmsg(sock, msghdr, 0);
  401. if (unlikely(err < 0)) {
  402. vq_err(&nvq->vq, "Fail to batch sending packets\n");
  403. return;
  404. }
  405. signal_used:
  406. vhost_net_signal_used(nvq);
  407. nvq->batched_xdp = 0;
  408. }
  409. static int sock_has_rx_data(struct socket *sock)
  410. {
  411. if (unlikely(!sock))
  412. return 0;
  413. if (sock->ops->peek_len)
  414. return sock->ops->peek_len(sock);
  415. return skb_queue_empty(&sock->sk->sk_receive_queue);
  416. }
  417. static void vhost_net_busy_poll_try_queue(struct vhost_net *net,
  418. struct vhost_virtqueue *vq)
  419. {
  420. if (!vhost_vq_avail_empty(&net->dev, vq)) {
  421. vhost_poll_queue(&vq->poll);
  422. } else if (unlikely(vhost_enable_notify(&net->dev, vq))) {
  423. vhost_disable_notify(&net->dev, vq);
  424. vhost_poll_queue(&vq->poll);
  425. }
  426. }
  427. static void vhost_net_busy_poll(struct vhost_net *net,
  428. struct vhost_virtqueue *rvq,
  429. struct vhost_virtqueue *tvq,
  430. bool *busyloop_intr,
  431. bool poll_rx)
  432. {
  433. unsigned long busyloop_timeout;
  434. unsigned long endtime;
  435. struct socket *sock;
  436. struct vhost_virtqueue *vq = poll_rx ? tvq : rvq;
  437. mutex_lock_nested(&vq->mutex, poll_rx ? VHOST_NET_VQ_TX: VHOST_NET_VQ_RX);
  438. vhost_disable_notify(&net->dev, vq);
  439. sock = rvq->private_data;
  440. busyloop_timeout = poll_rx ? rvq->busyloop_timeout:
  441. tvq->busyloop_timeout;
  442. preempt_disable();
  443. endtime = busy_clock() + busyloop_timeout;
  444. while (vhost_can_busy_poll(endtime)) {
  445. if (vhost_has_work(&net->dev)) {
  446. *busyloop_intr = true;
  447. break;
  448. }
  449. if ((sock_has_rx_data(sock) &&
  450. !vhost_vq_avail_empty(&net->dev, rvq)) ||
  451. !vhost_vq_avail_empty(&net->dev, tvq))
  452. break;
  453. cpu_relax();
  454. }
  455. preempt_enable();
  456. if (poll_rx || sock_has_rx_data(sock))
  457. vhost_net_busy_poll_try_queue(net, vq);
  458. else if (!poll_rx) /* On tx here, sock has no rx data. */
  459. vhost_enable_notify(&net->dev, rvq);
  460. mutex_unlock(&vq->mutex);
  461. }
  462. static int vhost_net_tx_get_vq_desc(struct vhost_net *net,
  463. struct vhost_net_virtqueue *tnvq,
  464. unsigned int *out_num, unsigned int *in_num,
  465. struct msghdr *msghdr, bool *busyloop_intr)
  466. {
  467. struct vhost_net_virtqueue *rnvq = &net->vqs[VHOST_NET_VQ_RX];
  468. struct vhost_virtqueue *rvq = &rnvq->vq;
  469. struct vhost_virtqueue *tvq = &tnvq->vq;
  470. int r = vhost_get_vq_desc(tvq, tvq->iov, ARRAY_SIZE(tvq->iov),
  471. out_num, in_num, NULL, NULL);
  472. if (r == tvq->num && tvq->busyloop_timeout) {
  473. /* Flush batched packets first */
  474. if (!vhost_sock_zcopy(tvq->private_data))
  475. vhost_tx_batch(net, tnvq, tvq->private_data, msghdr);
  476. vhost_net_busy_poll(net, rvq, tvq, busyloop_intr, false);
  477. r = vhost_get_vq_desc(tvq, tvq->iov, ARRAY_SIZE(tvq->iov),
  478. out_num, in_num, NULL, NULL);
  479. }
  480. return r;
  481. }
  482. static bool vhost_exceeds_maxpend(struct vhost_net *net)
  483. {
  484. struct vhost_net_virtqueue *nvq = &net->vqs[VHOST_NET_VQ_TX];
  485. struct vhost_virtqueue *vq = &nvq->vq;
  486. return (nvq->upend_idx + UIO_MAXIOV - nvq->done_idx) % UIO_MAXIOV >
  487. min_t(unsigned int, VHOST_MAX_PEND, vq->num >> 2);
  488. }
  489. static size_t init_iov_iter(struct vhost_virtqueue *vq, struct iov_iter *iter,
  490. size_t hdr_size, int out)
  491. {
  492. /* Skip header. TODO: support TSO. */
  493. size_t len = iov_length(vq->iov, out);
  494. iov_iter_init(iter, WRITE, vq->iov, out, len);
  495. iov_iter_advance(iter, hdr_size);
  496. return iov_iter_count(iter);
  497. }
  498. static bool vhost_exceeds_weight(int pkts, int total_len)
  499. {
  500. return total_len >= VHOST_NET_WEIGHT ||
  501. pkts >= VHOST_NET_PKT_WEIGHT;
  502. }
  503. static int get_tx_bufs(struct vhost_net *net,
  504. struct vhost_net_virtqueue *nvq,
  505. struct msghdr *msg,
  506. unsigned int *out, unsigned int *in,
  507. size_t *len, bool *busyloop_intr)
  508. {
  509. struct vhost_virtqueue *vq = &nvq->vq;
  510. int ret;
  511. ret = vhost_net_tx_get_vq_desc(net, nvq, out, in, msg, busyloop_intr);
  512. if (ret < 0 || ret == vq->num)
  513. return ret;
  514. if (*in) {
  515. vq_err(vq, "Unexpected descriptor format for TX: out %d, int %d\n",
  516. *out, *in);
  517. return -EFAULT;
  518. }
  519. /* Sanity check */
  520. *len = init_iov_iter(vq, &msg->msg_iter, nvq->vhost_hlen, *out);
  521. if (*len == 0) {
  522. vq_err(vq, "Unexpected header len for TX: %zd expected %zd\n",
  523. *len, nvq->vhost_hlen);
  524. return -EFAULT;
  525. }
  526. return ret;
  527. }
  528. static bool tx_can_batch(struct vhost_virtqueue *vq, size_t total_len)
  529. {
  530. return total_len < VHOST_NET_WEIGHT &&
  531. !vhost_vq_avail_empty(vq->dev, vq);
  532. }
  533. #define VHOST_NET_RX_PAD (NET_IP_ALIGN + NET_SKB_PAD)
  534. static int vhost_net_build_xdp(struct vhost_net_virtqueue *nvq,
  535. struct iov_iter *from)
  536. {
  537. struct vhost_virtqueue *vq = &nvq->vq;
  538. struct socket *sock = vq->private_data;
  539. struct page_frag *alloc_frag = &current->task_frag;
  540. struct virtio_net_hdr *gso;
  541. struct xdp_buff *xdp = &nvq->xdp[nvq->batched_xdp];
  542. struct tun_xdp_hdr *hdr;
  543. size_t len = iov_iter_count(from);
  544. int headroom = vhost_sock_xdp(sock) ? XDP_PACKET_HEADROOM : 0;
  545. int buflen = SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
  546. int pad = SKB_DATA_ALIGN(VHOST_NET_RX_PAD + headroom + nvq->sock_hlen);
  547. int sock_hlen = nvq->sock_hlen;
  548. void *buf;
  549. int copied;
  550. if (unlikely(len < nvq->sock_hlen))
  551. return -EFAULT;
  552. if (SKB_DATA_ALIGN(len + pad) +
  553. SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) > PAGE_SIZE)
  554. return -ENOSPC;
  555. buflen += SKB_DATA_ALIGN(len + pad);
  556. alloc_frag->offset = ALIGN((u64)alloc_frag->offset, SMP_CACHE_BYTES);
  557. if (unlikely(!skb_page_frag_refill(buflen, alloc_frag, GFP_KERNEL)))
  558. return -ENOMEM;
  559. buf = (char *)page_address(alloc_frag->page) + alloc_frag->offset;
  560. copied = copy_page_from_iter(alloc_frag->page,
  561. alloc_frag->offset +
  562. offsetof(struct tun_xdp_hdr, gso),
  563. sock_hlen, from);
  564. if (copied != sock_hlen)
  565. return -EFAULT;
  566. hdr = buf;
  567. gso = &hdr->gso;
  568. if ((gso->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) &&
  569. vhost16_to_cpu(vq, gso->csum_start) +
  570. vhost16_to_cpu(vq, gso->csum_offset) + 2 >
  571. vhost16_to_cpu(vq, gso->hdr_len)) {
  572. gso->hdr_len = cpu_to_vhost16(vq,
  573. vhost16_to_cpu(vq, gso->csum_start) +
  574. vhost16_to_cpu(vq, gso->csum_offset) + 2);
  575. if (vhost16_to_cpu(vq, gso->hdr_len) > len)
  576. return -EINVAL;
  577. }
  578. len -= sock_hlen;
  579. copied = copy_page_from_iter(alloc_frag->page,
  580. alloc_frag->offset + pad,
  581. len, from);
  582. if (copied != len)
  583. return -EFAULT;
  584. xdp->data_hard_start = buf;
  585. xdp->data = buf + pad;
  586. xdp->data_end = xdp->data + len;
  587. hdr->buflen = buflen;
  588. get_page(alloc_frag->page);
  589. alloc_frag->offset += buflen;
  590. ++nvq->batched_xdp;
  591. return 0;
  592. }
  593. static void handle_tx_copy(struct vhost_net *net, struct socket *sock)
  594. {
  595. struct vhost_net_virtqueue *nvq = &net->vqs[VHOST_NET_VQ_TX];
  596. struct vhost_virtqueue *vq = &nvq->vq;
  597. unsigned out, in;
  598. int head;
  599. struct msghdr msg = {
  600. .msg_name = NULL,
  601. .msg_namelen = 0,
  602. .msg_control = NULL,
  603. .msg_controllen = 0,
  604. .msg_flags = MSG_DONTWAIT,
  605. };
  606. size_t len, total_len = 0;
  607. int err;
  608. int sent_pkts = 0;
  609. bool sock_can_batch = (sock->sk->sk_sndbuf == INT_MAX);
  610. for (;;) {
  611. bool busyloop_intr = false;
  612. if (nvq->done_idx == VHOST_NET_BATCH)
  613. vhost_tx_batch(net, nvq, sock, &msg);
  614. head = get_tx_bufs(net, nvq, &msg, &out, &in, &len,
  615. &busyloop_intr);
  616. /* On error, stop handling until the next kick. */
  617. if (unlikely(head < 0))
  618. break;
  619. /* Nothing new? Wait for eventfd to tell us they refilled. */
  620. if (head == vq->num) {
  621. if (unlikely(busyloop_intr)) {
  622. vhost_poll_queue(&vq->poll);
  623. } else if (unlikely(vhost_enable_notify(&net->dev,
  624. vq))) {
  625. vhost_disable_notify(&net->dev, vq);
  626. continue;
  627. }
  628. break;
  629. }
  630. total_len += len;
  631. /* For simplicity, TX batching is only enabled if
  632. * sndbuf is unlimited.
  633. */
  634. if (sock_can_batch) {
  635. err = vhost_net_build_xdp(nvq, &msg.msg_iter);
  636. if (!err) {
  637. goto done;
  638. } else if (unlikely(err != -ENOSPC)) {
  639. vhost_tx_batch(net, nvq, sock, &msg);
  640. vhost_discard_vq_desc(vq, 1);
  641. vhost_net_enable_vq(net, vq);
  642. break;
  643. }
  644. /* We can't build XDP buff, go for single
  645. * packet path but let's flush batched
  646. * packets.
  647. */
  648. vhost_tx_batch(net, nvq, sock, &msg);
  649. msg.msg_control = NULL;
  650. } else {
  651. if (tx_can_batch(vq, total_len))
  652. msg.msg_flags |= MSG_MORE;
  653. else
  654. msg.msg_flags &= ~MSG_MORE;
  655. }
  656. /* TODO: Check specific error and bomb out unless ENOBUFS? */
  657. err = sock->ops->sendmsg(sock, &msg, len);
  658. if (unlikely(err < 0)) {
  659. vhost_discard_vq_desc(vq, 1);
  660. vhost_net_enable_vq(net, vq);
  661. break;
  662. }
  663. if (err != len)
  664. pr_debug("Truncated TX packet: len %d != %zd\n",
  665. err, len);
  666. done:
  667. vq->heads[nvq->done_idx].id = cpu_to_vhost32(vq, head);
  668. vq->heads[nvq->done_idx].len = 0;
  669. ++nvq->done_idx;
  670. if (vhost_exceeds_weight(++sent_pkts, total_len)) {
  671. vhost_poll_queue(&vq->poll);
  672. break;
  673. }
  674. }
  675. vhost_tx_batch(net, nvq, sock, &msg);
  676. }
  677. static void handle_tx_zerocopy(struct vhost_net *net, struct socket *sock)
  678. {
  679. struct vhost_net_virtqueue *nvq = &net->vqs[VHOST_NET_VQ_TX];
  680. struct vhost_virtqueue *vq = &nvq->vq;
  681. unsigned out, in;
  682. int head;
  683. struct msghdr msg = {
  684. .msg_name = NULL,
  685. .msg_namelen = 0,
  686. .msg_control = NULL,
  687. .msg_controllen = 0,
  688. .msg_flags = MSG_DONTWAIT,
  689. };
  690. struct tun_msg_ctl ctl;
  691. size_t len, total_len = 0;
  692. int err;
  693. struct vhost_net_ubuf_ref *uninitialized_var(ubufs);
  694. bool zcopy_used;
  695. int sent_pkts = 0;
  696. for (;;) {
  697. bool busyloop_intr;
  698. /* Release DMAs done buffers first */
  699. vhost_zerocopy_signal_used(net, vq);
  700. busyloop_intr = false;
  701. head = get_tx_bufs(net, nvq, &msg, &out, &in, &len,
  702. &busyloop_intr);
  703. /* On error, stop handling until the next kick. */
  704. if (unlikely(head < 0))
  705. break;
  706. /* Nothing new? Wait for eventfd to tell us they refilled. */
  707. if (head == vq->num) {
  708. if (unlikely(busyloop_intr)) {
  709. vhost_poll_queue(&vq->poll);
  710. } else if (unlikely(vhost_enable_notify(&net->dev, vq))) {
  711. vhost_disable_notify(&net->dev, vq);
  712. continue;
  713. }
  714. break;
  715. }
  716. zcopy_used = len >= VHOST_GOODCOPY_LEN
  717. && !vhost_exceeds_maxpend(net)
  718. && vhost_net_tx_select_zcopy(net);
  719. /* use msg_control to pass vhost zerocopy ubuf info to skb */
  720. if (zcopy_used) {
  721. struct ubuf_info *ubuf;
  722. ubuf = nvq->ubuf_info + nvq->upend_idx;
  723. vq->heads[nvq->upend_idx].id = cpu_to_vhost32(vq, head);
  724. vq->heads[nvq->upend_idx].len = VHOST_DMA_IN_PROGRESS;
  725. ubuf->callback = vhost_zerocopy_callback;
  726. ubuf->ctx = nvq->ubufs;
  727. ubuf->desc = nvq->upend_idx;
  728. refcount_set(&ubuf->refcnt, 1);
  729. msg.msg_control = &ctl;
  730. ctl.type = TUN_MSG_UBUF;
  731. ctl.ptr = ubuf;
  732. msg.msg_controllen = sizeof(ctl);
  733. ubufs = nvq->ubufs;
  734. atomic_inc(&ubufs->refcount);
  735. nvq->upend_idx = (nvq->upend_idx + 1) % UIO_MAXIOV;
  736. } else {
  737. msg.msg_control = NULL;
  738. ubufs = NULL;
  739. }
  740. total_len += len;
  741. if (tx_can_batch(vq, total_len) &&
  742. likely(!vhost_exceeds_maxpend(net))) {
  743. msg.msg_flags |= MSG_MORE;
  744. } else {
  745. msg.msg_flags &= ~MSG_MORE;
  746. }
  747. /* TODO: Check specific error and bomb out unless ENOBUFS? */
  748. err = sock->ops->sendmsg(sock, &msg, len);
  749. if (unlikely(err < 0)) {
  750. if (zcopy_used) {
  751. vhost_net_ubuf_put(ubufs);
  752. nvq->upend_idx = ((unsigned)nvq->upend_idx - 1)
  753. % UIO_MAXIOV;
  754. }
  755. vhost_discard_vq_desc(vq, 1);
  756. vhost_net_enable_vq(net, vq);
  757. break;
  758. }
  759. if (err != len)
  760. pr_debug("Truncated TX packet: "
  761. " len %d != %zd\n", err, len);
  762. if (!zcopy_used)
  763. vhost_add_used_and_signal(&net->dev, vq, head, 0);
  764. else
  765. vhost_zerocopy_signal_used(net, vq);
  766. vhost_net_tx_packet(net);
  767. if (unlikely(vhost_exceeds_weight(++sent_pkts, total_len))) {
  768. vhost_poll_queue(&vq->poll);
  769. break;
  770. }
  771. }
  772. }
  773. /* Expects to be always run from workqueue - which acts as
  774. * read-size critical section for our kind of RCU. */
  775. static void handle_tx(struct vhost_net *net)
  776. {
  777. struct vhost_net_virtqueue *nvq = &net->vqs[VHOST_NET_VQ_TX];
  778. struct vhost_virtqueue *vq = &nvq->vq;
  779. struct socket *sock;
  780. mutex_lock_nested(&vq->mutex, VHOST_NET_VQ_TX);
  781. sock = vq->private_data;
  782. if (!sock)
  783. goto out;
  784. if (!vq_iotlb_prefetch(vq))
  785. goto out;
  786. vhost_disable_notify(&net->dev, vq);
  787. vhost_net_disable_vq(net, vq);
  788. if (vhost_sock_zcopy(sock))
  789. handle_tx_zerocopy(net, sock);
  790. else
  791. handle_tx_copy(net, sock);
  792. out:
  793. mutex_unlock(&vq->mutex);
  794. }
  795. static int peek_head_len(struct vhost_net_virtqueue *rvq, struct sock *sk)
  796. {
  797. struct sk_buff *head;
  798. int len = 0;
  799. unsigned long flags;
  800. if (rvq->rx_ring)
  801. return vhost_net_buf_peek(rvq);
  802. spin_lock_irqsave(&sk->sk_receive_queue.lock, flags);
  803. head = skb_peek(&sk->sk_receive_queue);
  804. if (likely(head)) {
  805. len = head->len;
  806. if (skb_vlan_tag_present(head))
  807. len += VLAN_HLEN;
  808. }
  809. spin_unlock_irqrestore(&sk->sk_receive_queue.lock, flags);
  810. return len;
  811. }
  812. static int vhost_net_rx_peek_head_len(struct vhost_net *net, struct sock *sk,
  813. bool *busyloop_intr)
  814. {
  815. struct vhost_net_virtqueue *rnvq = &net->vqs[VHOST_NET_VQ_RX];
  816. struct vhost_net_virtqueue *tnvq = &net->vqs[VHOST_NET_VQ_TX];
  817. struct vhost_virtqueue *rvq = &rnvq->vq;
  818. struct vhost_virtqueue *tvq = &tnvq->vq;
  819. int len = peek_head_len(rnvq, sk);
  820. if (!len && rvq->busyloop_timeout) {
  821. /* Flush batched heads first */
  822. vhost_net_signal_used(rnvq);
  823. /* Both tx vq and rx socket were polled here */
  824. vhost_net_busy_poll(net, rvq, tvq, busyloop_intr, true);
  825. len = peek_head_len(rnvq, sk);
  826. }
  827. return len;
  828. }
  829. /* This is a multi-buffer version of vhost_get_desc, that works if
  830. * vq has read descriptors only.
  831. * @vq - the relevant virtqueue
  832. * @datalen - data length we'll be reading
  833. * @iovcount - returned count of io vectors we fill
  834. * @log - vhost log
  835. * @log_num - log offset
  836. * @quota - headcount quota, 1 for big buffer
  837. * returns number of buffer heads allocated, negative on error
  838. */
  839. static int get_rx_bufs(struct vhost_virtqueue *vq,
  840. struct vring_used_elem *heads,
  841. int datalen,
  842. unsigned *iovcount,
  843. struct vhost_log *log,
  844. unsigned *log_num,
  845. unsigned int quota)
  846. {
  847. unsigned int out, in;
  848. int seg = 0;
  849. int headcount = 0;
  850. unsigned d;
  851. int r, nlogs = 0;
  852. /* len is always initialized before use since we are always called with
  853. * datalen > 0.
  854. */
  855. u32 uninitialized_var(len);
  856. while (datalen > 0 && headcount < quota) {
  857. if (unlikely(seg >= UIO_MAXIOV)) {
  858. r = -ENOBUFS;
  859. goto err;
  860. }
  861. r = vhost_get_vq_desc(vq, vq->iov + seg,
  862. ARRAY_SIZE(vq->iov) - seg, &out,
  863. &in, log, log_num);
  864. if (unlikely(r < 0))
  865. goto err;
  866. d = r;
  867. if (d == vq->num) {
  868. r = 0;
  869. goto err;
  870. }
  871. if (unlikely(out || in <= 0)) {
  872. vq_err(vq, "unexpected descriptor format for RX: "
  873. "out %d, in %d\n", out, in);
  874. r = -EINVAL;
  875. goto err;
  876. }
  877. if (unlikely(log)) {
  878. nlogs += *log_num;
  879. log += *log_num;
  880. }
  881. heads[headcount].id = cpu_to_vhost32(vq, d);
  882. len = iov_length(vq->iov + seg, in);
  883. heads[headcount].len = cpu_to_vhost32(vq, len);
  884. datalen -= len;
  885. ++headcount;
  886. seg += in;
  887. }
  888. heads[headcount - 1].len = cpu_to_vhost32(vq, len + datalen);
  889. *iovcount = seg;
  890. if (unlikely(log))
  891. *log_num = nlogs;
  892. /* Detect overrun */
  893. if (unlikely(datalen > 0)) {
  894. r = UIO_MAXIOV + 1;
  895. goto err;
  896. }
  897. return headcount;
  898. err:
  899. vhost_discard_vq_desc(vq, headcount);
  900. return r;
  901. }
  902. /* Expects to be always run from workqueue - which acts as
  903. * read-size critical section for our kind of RCU. */
  904. static void handle_rx(struct vhost_net *net)
  905. {
  906. struct vhost_net_virtqueue *nvq = &net->vqs[VHOST_NET_VQ_RX];
  907. struct vhost_virtqueue *vq = &nvq->vq;
  908. unsigned uninitialized_var(in), log;
  909. struct vhost_log *vq_log;
  910. struct msghdr msg = {
  911. .msg_name = NULL,
  912. .msg_namelen = 0,
  913. .msg_control = NULL, /* FIXME: get and handle RX aux data. */
  914. .msg_controllen = 0,
  915. .msg_flags = MSG_DONTWAIT,
  916. };
  917. struct virtio_net_hdr hdr = {
  918. .flags = 0,
  919. .gso_type = VIRTIO_NET_HDR_GSO_NONE
  920. };
  921. size_t total_len = 0;
  922. int err, mergeable;
  923. s16 headcount;
  924. size_t vhost_hlen, sock_hlen;
  925. size_t vhost_len, sock_len;
  926. bool busyloop_intr = false;
  927. struct socket *sock;
  928. struct iov_iter fixup;
  929. __virtio16 num_buffers;
  930. int recv_pkts = 0;
  931. mutex_lock_nested(&vq->mutex, VHOST_NET_VQ_RX);
  932. sock = vq->private_data;
  933. if (!sock)
  934. goto out;
  935. if (!vq_iotlb_prefetch(vq))
  936. goto out;
  937. vhost_disable_notify(&net->dev, vq);
  938. vhost_net_disable_vq(net, vq);
  939. vhost_hlen = nvq->vhost_hlen;
  940. sock_hlen = nvq->sock_hlen;
  941. vq_log = unlikely(vhost_has_feature(vq, VHOST_F_LOG_ALL)) ?
  942. vq->log : NULL;
  943. mergeable = vhost_has_feature(vq, VIRTIO_NET_F_MRG_RXBUF);
  944. while ((sock_len = vhost_net_rx_peek_head_len(net, sock->sk,
  945. &busyloop_intr))) {
  946. sock_len += sock_hlen;
  947. vhost_len = sock_len + vhost_hlen;
  948. headcount = get_rx_bufs(vq, vq->heads + nvq->done_idx,
  949. vhost_len, &in, vq_log, &log,
  950. likely(mergeable) ? UIO_MAXIOV : 1);
  951. /* On error, stop handling until the next kick. */
  952. if (unlikely(headcount < 0))
  953. goto out;
  954. /* OK, now we need to know about added descriptors. */
  955. if (!headcount) {
  956. if (unlikely(busyloop_intr)) {
  957. vhost_poll_queue(&vq->poll);
  958. } else if (unlikely(vhost_enable_notify(&net->dev, vq))) {
  959. /* They have slipped one in as we were
  960. * doing that: check again. */
  961. vhost_disable_notify(&net->dev, vq);
  962. continue;
  963. }
  964. /* Nothing new? Wait for eventfd to tell us
  965. * they refilled. */
  966. goto out;
  967. }
  968. busyloop_intr = false;
  969. if (nvq->rx_ring)
  970. msg.msg_control = vhost_net_buf_consume(&nvq->rxq);
  971. /* On overrun, truncate and discard */
  972. if (unlikely(headcount > UIO_MAXIOV)) {
  973. iov_iter_init(&msg.msg_iter, READ, vq->iov, 1, 1);
  974. err = sock->ops->recvmsg(sock, &msg,
  975. 1, MSG_DONTWAIT | MSG_TRUNC);
  976. pr_debug("Discarded rx packet: len %zd\n", sock_len);
  977. continue;
  978. }
  979. /* We don't need to be notified again. */
  980. iov_iter_init(&msg.msg_iter, READ, vq->iov, in, vhost_len);
  981. fixup = msg.msg_iter;
  982. if (unlikely((vhost_hlen))) {
  983. /* We will supply the header ourselves
  984. * TODO: support TSO.
  985. */
  986. iov_iter_advance(&msg.msg_iter, vhost_hlen);
  987. }
  988. err = sock->ops->recvmsg(sock, &msg,
  989. sock_len, MSG_DONTWAIT | MSG_TRUNC);
  990. /* Userspace might have consumed the packet meanwhile:
  991. * it's not supposed to do this usually, but might be hard
  992. * to prevent. Discard data we got (if any) and keep going. */
  993. if (unlikely(err != sock_len)) {
  994. pr_debug("Discarded rx packet: "
  995. " len %d, expected %zd\n", err, sock_len);
  996. vhost_discard_vq_desc(vq, headcount);
  997. continue;
  998. }
  999. /* Supply virtio_net_hdr if VHOST_NET_F_VIRTIO_NET_HDR */
  1000. if (unlikely(vhost_hlen)) {
  1001. if (copy_to_iter(&hdr, sizeof(hdr),
  1002. &fixup) != sizeof(hdr)) {
  1003. vq_err(vq, "Unable to write vnet_hdr "
  1004. "at addr %p\n", vq->iov->iov_base);
  1005. goto out;
  1006. }
  1007. } else {
  1008. /* Header came from socket; we'll need to patch
  1009. * ->num_buffers over if VIRTIO_NET_F_MRG_RXBUF
  1010. */
  1011. iov_iter_advance(&fixup, sizeof(hdr));
  1012. }
  1013. /* TODO: Should check and handle checksum. */
  1014. num_buffers = cpu_to_vhost16(vq, headcount);
  1015. if (likely(mergeable) &&
  1016. copy_to_iter(&num_buffers, sizeof num_buffers,
  1017. &fixup) != sizeof num_buffers) {
  1018. vq_err(vq, "Failed num_buffers write");
  1019. vhost_discard_vq_desc(vq, headcount);
  1020. goto out;
  1021. }
  1022. nvq->done_idx += headcount;
  1023. if (nvq->done_idx > VHOST_NET_BATCH)
  1024. vhost_net_signal_used(nvq);
  1025. if (unlikely(vq_log))
  1026. vhost_log_write(vq, vq_log, log, vhost_len);
  1027. total_len += vhost_len;
  1028. if (unlikely(vhost_exceeds_weight(++recv_pkts, total_len))) {
  1029. vhost_poll_queue(&vq->poll);
  1030. goto out;
  1031. }
  1032. }
  1033. if (unlikely(busyloop_intr))
  1034. vhost_poll_queue(&vq->poll);
  1035. else
  1036. vhost_net_enable_vq(net, vq);
  1037. out:
  1038. vhost_net_signal_used(nvq);
  1039. mutex_unlock(&vq->mutex);
  1040. }
  1041. static void handle_tx_kick(struct vhost_work *work)
  1042. {
  1043. struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
  1044. poll.work);
  1045. struct vhost_net *net = container_of(vq->dev, struct vhost_net, dev);
  1046. handle_tx(net);
  1047. }
  1048. static void handle_rx_kick(struct vhost_work *work)
  1049. {
  1050. struct vhost_virtqueue *vq = container_of(work, struct vhost_virtqueue,
  1051. poll.work);
  1052. struct vhost_net *net = container_of(vq->dev, struct vhost_net, dev);
  1053. handle_rx(net);
  1054. }
  1055. static void handle_tx_net(struct vhost_work *work)
  1056. {
  1057. struct vhost_net *net = container_of(work, struct vhost_net,
  1058. poll[VHOST_NET_VQ_TX].work);
  1059. handle_tx(net);
  1060. }
  1061. static void handle_rx_net(struct vhost_work *work)
  1062. {
  1063. struct vhost_net *net = container_of(work, struct vhost_net,
  1064. poll[VHOST_NET_VQ_RX].work);
  1065. handle_rx(net);
  1066. }
  1067. static int vhost_net_open(struct inode *inode, struct file *f)
  1068. {
  1069. struct vhost_net *n;
  1070. struct vhost_dev *dev;
  1071. struct vhost_virtqueue **vqs;
  1072. void **queue;
  1073. struct xdp_buff *xdp;
  1074. int i;
  1075. n = kvmalloc(sizeof *n, GFP_KERNEL | __GFP_RETRY_MAYFAIL);
  1076. if (!n)
  1077. return -ENOMEM;
  1078. vqs = kmalloc_array(VHOST_NET_VQ_MAX, sizeof(*vqs), GFP_KERNEL);
  1079. if (!vqs) {
  1080. kvfree(n);
  1081. return -ENOMEM;
  1082. }
  1083. queue = kmalloc_array(VHOST_NET_BATCH, sizeof(void *),
  1084. GFP_KERNEL);
  1085. if (!queue) {
  1086. kfree(vqs);
  1087. kvfree(n);
  1088. return -ENOMEM;
  1089. }
  1090. n->vqs[VHOST_NET_VQ_RX].rxq.queue = queue;
  1091. xdp = kmalloc_array(VHOST_NET_BATCH, sizeof(*xdp), GFP_KERNEL);
  1092. if (!xdp) {
  1093. kfree(vqs);
  1094. kvfree(n);
  1095. kfree(queue);
  1096. return -ENOMEM;
  1097. }
  1098. n->vqs[VHOST_NET_VQ_TX].xdp = xdp;
  1099. dev = &n->dev;
  1100. vqs[VHOST_NET_VQ_TX] = &n->vqs[VHOST_NET_VQ_TX].vq;
  1101. vqs[VHOST_NET_VQ_RX] = &n->vqs[VHOST_NET_VQ_RX].vq;
  1102. n->vqs[VHOST_NET_VQ_TX].vq.handle_kick = handle_tx_kick;
  1103. n->vqs[VHOST_NET_VQ_RX].vq.handle_kick = handle_rx_kick;
  1104. for (i = 0; i < VHOST_NET_VQ_MAX; i++) {
  1105. n->vqs[i].ubufs = NULL;
  1106. n->vqs[i].ubuf_info = NULL;
  1107. n->vqs[i].upend_idx = 0;
  1108. n->vqs[i].done_idx = 0;
  1109. n->vqs[i].batched_xdp = 0;
  1110. n->vqs[i].vhost_hlen = 0;
  1111. n->vqs[i].sock_hlen = 0;
  1112. n->vqs[i].rx_ring = NULL;
  1113. vhost_net_buf_init(&n->vqs[i].rxq);
  1114. }
  1115. vhost_dev_init(dev, vqs, VHOST_NET_VQ_MAX);
  1116. vhost_poll_init(n->poll + VHOST_NET_VQ_TX, handle_tx_net, EPOLLOUT, dev);
  1117. vhost_poll_init(n->poll + VHOST_NET_VQ_RX, handle_rx_net, EPOLLIN, dev);
  1118. f->private_data = n;
  1119. return 0;
  1120. }
  1121. static struct socket *vhost_net_stop_vq(struct vhost_net *n,
  1122. struct vhost_virtqueue *vq)
  1123. {
  1124. struct socket *sock;
  1125. struct vhost_net_virtqueue *nvq =
  1126. container_of(vq, struct vhost_net_virtqueue, vq);
  1127. mutex_lock(&vq->mutex);
  1128. sock = vq->private_data;
  1129. vhost_net_disable_vq(n, vq);
  1130. vq->private_data = NULL;
  1131. vhost_net_buf_unproduce(nvq);
  1132. nvq->rx_ring = NULL;
  1133. mutex_unlock(&vq->mutex);
  1134. return sock;
  1135. }
  1136. static void vhost_net_stop(struct vhost_net *n, struct socket **tx_sock,
  1137. struct socket **rx_sock)
  1138. {
  1139. *tx_sock = vhost_net_stop_vq(n, &n->vqs[VHOST_NET_VQ_TX].vq);
  1140. *rx_sock = vhost_net_stop_vq(n, &n->vqs[VHOST_NET_VQ_RX].vq);
  1141. }
  1142. static void vhost_net_flush_vq(struct vhost_net *n, int index)
  1143. {
  1144. vhost_poll_flush(n->poll + index);
  1145. vhost_poll_flush(&n->vqs[index].vq.poll);
  1146. }
  1147. static void vhost_net_flush(struct vhost_net *n)
  1148. {
  1149. vhost_net_flush_vq(n, VHOST_NET_VQ_TX);
  1150. vhost_net_flush_vq(n, VHOST_NET_VQ_RX);
  1151. if (n->vqs[VHOST_NET_VQ_TX].ubufs) {
  1152. mutex_lock(&n->vqs[VHOST_NET_VQ_TX].vq.mutex);
  1153. n->tx_flush = true;
  1154. mutex_unlock(&n->vqs[VHOST_NET_VQ_TX].vq.mutex);
  1155. /* Wait for all lower device DMAs done. */
  1156. vhost_net_ubuf_put_and_wait(n->vqs[VHOST_NET_VQ_TX].ubufs);
  1157. mutex_lock(&n->vqs[VHOST_NET_VQ_TX].vq.mutex);
  1158. n->tx_flush = false;
  1159. atomic_set(&n->vqs[VHOST_NET_VQ_TX].ubufs->refcount, 1);
  1160. mutex_unlock(&n->vqs[VHOST_NET_VQ_TX].vq.mutex);
  1161. }
  1162. }
  1163. static int vhost_net_release(struct inode *inode, struct file *f)
  1164. {
  1165. struct vhost_net *n = f->private_data;
  1166. struct socket *tx_sock;
  1167. struct socket *rx_sock;
  1168. vhost_net_stop(n, &tx_sock, &rx_sock);
  1169. vhost_net_flush(n);
  1170. vhost_dev_stop(&n->dev);
  1171. vhost_dev_cleanup(&n->dev);
  1172. vhost_net_vq_reset(n);
  1173. if (tx_sock)
  1174. sockfd_put(tx_sock);
  1175. if (rx_sock)
  1176. sockfd_put(rx_sock);
  1177. /* Make sure no callbacks are outstanding */
  1178. synchronize_rcu_bh();
  1179. /* We do an extra flush before freeing memory,
  1180. * since jobs can re-queue themselves. */
  1181. vhost_net_flush(n);
  1182. kfree(n->vqs[VHOST_NET_VQ_RX].rxq.queue);
  1183. kfree(n->vqs[VHOST_NET_VQ_TX].xdp);
  1184. kfree(n->dev.vqs);
  1185. kvfree(n);
  1186. return 0;
  1187. }
  1188. static struct socket *get_raw_socket(int fd)
  1189. {
  1190. struct {
  1191. struct sockaddr_ll sa;
  1192. char buf[MAX_ADDR_LEN];
  1193. } uaddr;
  1194. int r;
  1195. struct socket *sock = sockfd_lookup(fd, &r);
  1196. if (!sock)
  1197. return ERR_PTR(-ENOTSOCK);
  1198. /* Parameter checking */
  1199. if (sock->sk->sk_type != SOCK_RAW) {
  1200. r = -ESOCKTNOSUPPORT;
  1201. goto err;
  1202. }
  1203. r = sock->ops->getname(sock, (struct sockaddr *)&uaddr.sa, 0);
  1204. if (r < 0)
  1205. goto err;
  1206. if (uaddr.sa.sll_family != AF_PACKET) {
  1207. r = -EPFNOSUPPORT;
  1208. goto err;
  1209. }
  1210. return sock;
  1211. err:
  1212. sockfd_put(sock);
  1213. return ERR_PTR(r);
  1214. }
  1215. static struct ptr_ring *get_tap_ptr_ring(int fd)
  1216. {
  1217. struct ptr_ring *ring;
  1218. struct file *file = fget(fd);
  1219. if (!file)
  1220. return NULL;
  1221. ring = tun_get_tx_ring(file);
  1222. if (!IS_ERR(ring))
  1223. goto out;
  1224. ring = tap_get_ptr_ring(file);
  1225. if (!IS_ERR(ring))
  1226. goto out;
  1227. ring = NULL;
  1228. out:
  1229. fput(file);
  1230. return ring;
  1231. }
  1232. static struct socket *get_tap_socket(int fd)
  1233. {
  1234. struct file *file = fget(fd);
  1235. struct socket *sock;
  1236. if (!file)
  1237. return ERR_PTR(-EBADF);
  1238. sock = tun_get_socket(file);
  1239. if (!IS_ERR(sock))
  1240. return sock;
  1241. sock = tap_get_socket(file);
  1242. if (IS_ERR(sock))
  1243. fput(file);
  1244. return sock;
  1245. }
  1246. static struct socket *get_socket(int fd)
  1247. {
  1248. struct socket *sock;
  1249. /* special case to disable backend */
  1250. if (fd == -1)
  1251. return NULL;
  1252. sock = get_raw_socket(fd);
  1253. if (!IS_ERR(sock))
  1254. return sock;
  1255. sock = get_tap_socket(fd);
  1256. if (!IS_ERR(sock))
  1257. return sock;
  1258. return ERR_PTR(-ENOTSOCK);
  1259. }
  1260. static long vhost_net_set_backend(struct vhost_net *n, unsigned index, int fd)
  1261. {
  1262. struct socket *sock, *oldsock;
  1263. struct vhost_virtqueue *vq;
  1264. struct vhost_net_virtqueue *nvq;
  1265. struct vhost_net_ubuf_ref *ubufs, *oldubufs = NULL;
  1266. int r;
  1267. mutex_lock(&n->dev.mutex);
  1268. r = vhost_dev_check_owner(&n->dev);
  1269. if (r)
  1270. goto err;
  1271. if (index >= VHOST_NET_VQ_MAX) {
  1272. r = -ENOBUFS;
  1273. goto err;
  1274. }
  1275. vq = &n->vqs[index].vq;
  1276. nvq = &n->vqs[index];
  1277. mutex_lock(&vq->mutex);
  1278. /* Verify that ring has been setup correctly. */
  1279. if (!vhost_vq_access_ok(vq)) {
  1280. r = -EFAULT;
  1281. goto err_vq;
  1282. }
  1283. sock = get_socket(fd);
  1284. if (IS_ERR(sock)) {
  1285. r = PTR_ERR(sock);
  1286. goto err_vq;
  1287. }
  1288. /* start polling new socket */
  1289. oldsock = vq->private_data;
  1290. if (sock != oldsock) {
  1291. ubufs = vhost_net_ubuf_alloc(vq,
  1292. sock && vhost_sock_zcopy(sock));
  1293. if (IS_ERR(ubufs)) {
  1294. r = PTR_ERR(ubufs);
  1295. goto err_ubufs;
  1296. }
  1297. vhost_net_disable_vq(n, vq);
  1298. vq->private_data = sock;
  1299. vhost_net_buf_unproduce(nvq);
  1300. r = vhost_vq_init_access(vq);
  1301. if (r)
  1302. goto err_used;
  1303. r = vhost_net_enable_vq(n, vq);
  1304. if (r)
  1305. goto err_used;
  1306. if (index == VHOST_NET_VQ_RX)
  1307. nvq->rx_ring = get_tap_ptr_ring(fd);
  1308. oldubufs = nvq->ubufs;
  1309. nvq->ubufs = ubufs;
  1310. n->tx_packets = 0;
  1311. n->tx_zcopy_err = 0;
  1312. n->tx_flush = false;
  1313. }
  1314. mutex_unlock(&vq->mutex);
  1315. if (oldubufs) {
  1316. vhost_net_ubuf_put_wait_and_free(oldubufs);
  1317. mutex_lock(&vq->mutex);
  1318. vhost_zerocopy_signal_used(n, vq);
  1319. mutex_unlock(&vq->mutex);
  1320. }
  1321. if (oldsock) {
  1322. vhost_net_flush_vq(n, index);
  1323. sockfd_put(oldsock);
  1324. }
  1325. mutex_unlock(&n->dev.mutex);
  1326. return 0;
  1327. err_used:
  1328. vq->private_data = oldsock;
  1329. vhost_net_enable_vq(n, vq);
  1330. if (ubufs)
  1331. vhost_net_ubuf_put_wait_and_free(ubufs);
  1332. err_ubufs:
  1333. if (sock)
  1334. sockfd_put(sock);
  1335. err_vq:
  1336. mutex_unlock(&vq->mutex);
  1337. err:
  1338. mutex_unlock(&n->dev.mutex);
  1339. return r;
  1340. }
  1341. static long vhost_net_reset_owner(struct vhost_net *n)
  1342. {
  1343. struct socket *tx_sock = NULL;
  1344. struct socket *rx_sock = NULL;
  1345. long err;
  1346. struct vhost_umem *umem;
  1347. mutex_lock(&n->dev.mutex);
  1348. err = vhost_dev_check_owner(&n->dev);
  1349. if (err)
  1350. goto done;
  1351. umem = vhost_dev_reset_owner_prepare();
  1352. if (!umem) {
  1353. err = -ENOMEM;
  1354. goto done;
  1355. }
  1356. vhost_net_stop(n, &tx_sock, &rx_sock);
  1357. vhost_net_flush(n);
  1358. vhost_dev_stop(&n->dev);
  1359. vhost_dev_reset_owner(&n->dev, umem);
  1360. vhost_net_vq_reset(n);
  1361. done:
  1362. mutex_unlock(&n->dev.mutex);
  1363. if (tx_sock)
  1364. sockfd_put(tx_sock);
  1365. if (rx_sock)
  1366. sockfd_put(rx_sock);
  1367. return err;
  1368. }
  1369. static int vhost_net_set_backend_features(struct vhost_net *n, u64 features)
  1370. {
  1371. int i;
  1372. mutex_lock(&n->dev.mutex);
  1373. for (i = 0; i < VHOST_NET_VQ_MAX; ++i) {
  1374. mutex_lock(&n->vqs[i].vq.mutex);
  1375. n->vqs[i].vq.acked_backend_features = features;
  1376. mutex_unlock(&n->vqs[i].vq.mutex);
  1377. }
  1378. mutex_unlock(&n->dev.mutex);
  1379. return 0;
  1380. }
  1381. static int vhost_net_set_features(struct vhost_net *n, u64 features)
  1382. {
  1383. size_t vhost_hlen, sock_hlen, hdr_len;
  1384. int i;
  1385. hdr_len = (features & ((1ULL << VIRTIO_NET_F_MRG_RXBUF) |
  1386. (1ULL << VIRTIO_F_VERSION_1))) ?
  1387. sizeof(struct virtio_net_hdr_mrg_rxbuf) :
  1388. sizeof(struct virtio_net_hdr);
  1389. if (features & (1 << VHOST_NET_F_VIRTIO_NET_HDR)) {
  1390. /* vhost provides vnet_hdr */
  1391. vhost_hlen = hdr_len;
  1392. sock_hlen = 0;
  1393. } else {
  1394. /* socket provides vnet_hdr */
  1395. vhost_hlen = 0;
  1396. sock_hlen = hdr_len;
  1397. }
  1398. mutex_lock(&n->dev.mutex);
  1399. if ((features & (1 << VHOST_F_LOG_ALL)) &&
  1400. !vhost_log_access_ok(&n->dev))
  1401. goto out_unlock;
  1402. if ((features & (1ULL << VIRTIO_F_IOMMU_PLATFORM))) {
  1403. if (vhost_init_device_iotlb(&n->dev, true))
  1404. goto out_unlock;
  1405. }
  1406. for (i = 0; i < VHOST_NET_VQ_MAX; ++i) {
  1407. mutex_lock(&n->vqs[i].vq.mutex);
  1408. n->vqs[i].vq.acked_features = features;
  1409. n->vqs[i].vhost_hlen = vhost_hlen;
  1410. n->vqs[i].sock_hlen = sock_hlen;
  1411. mutex_unlock(&n->vqs[i].vq.mutex);
  1412. }
  1413. mutex_unlock(&n->dev.mutex);
  1414. return 0;
  1415. out_unlock:
  1416. mutex_unlock(&n->dev.mutex);
  1417. return -EFAULT;
  1418. }
  1419. static long vhost_net_set_owner(struct vhost_net *n)
  1420. {
  1421. int r;
  1422. mutex_lock(&n->dev.mutex);
  1423. if (vhost_dev_has_owner(&n->dev)) {
  1424. r = -EBUSY;
  1425. goto out;
  1426. }
  1427. r = vhost_net_set_ubuf_info(n);
  1428. if (r)
  1429. goto out;
  1430. r = vhost_dev_set_owner(&n->dev);
  1431. if (r)
  1432. vhost_net_clear_ubuf_info(n);
  1433. vhost_net_flush(n);
  1434. out:
  1435. mutex_unlock(&n->dev.mutex);
  1436. return r;
  1437. }
  1438. static long vhost_net_ioctl(struct file *f, unsigned int ioctl,
  1439. unsigned long arg)
  1440. {
  1441. struct vhost_net *n = f->private_data;
  1442. void __user *argp = (void __user *)arg;
  1443. u64 __user *featurep = argp;
  1444. struct vhost_vring_file backend;
  1445. u64 features;
  1446. int r;
  1447. switch (ioctl) {
  1448. case VHOST_NET_SET_BACKEND:
  1449. if (copy_from_user(&backend, argp, sizeof backend))
  1450. return -EFAULT;
  1451. return vhost_net_set_backend(n, backend.index, backend.fd);
  1452. case VHOST_GET_FEATURES:
  1453. features = VHOST_NET_FEATURES;
  1454. if (copy_to_user(featurep, &features, sizeof features))
  1455. return -EFAULT;
  1456. return 0;
  1457. case VHOST_SET_FEATURES:
  1458. if (copy_from_user(&features, featurep, sizeof features))
  1459. return -EFAULT;
  1460. if (features & ~VHOST_NET_FEATURES)
  1461. return -EOPNOTSUPP;
  1462. return vhost_net_set_features(n, features);
  1463. case VHOST_GET_BACKEND_FEATURES:
  1464. features = VHOST_NET_BACKEND_FEATURES;
  1465. if (copy_to_user(featurep, &features, sizeof(features)))
  1466. return -EFAULT;
  1467. return 0;
  1468. case VHOST_SET_BACKEND_FEATURES:
  1469. if (copy_from_user(&features, featurep, sizeof(features)))
  1470. return -EFAULT;
  1471. if (features & ~VHOST_NET_BACKEND_FEATURES)
  1472. return -EOPNOTSUPP;
  1473. return vhost_net_set_backend_features(n, features);
  1474. case VHOST_RESET_OWNER:
  1475. return vhost_net_reset_owner(n);
  1476. case VHOST_SET_OWNER:
  1477. return vhost_net_set_owner(n);
  1478. default:
  1479. mutex_lock(&n->dev.mutex);
  1480. r = vhost_dev_ioctl(&n->dev, ioctl, argp);
  1481. if (r == -ENOIOCTLCMD)
  1482. r = vhost_vring_ioctl(&n->dev, ioctl, argp);
  1483. else
  1484. vhost_net_flush(n);
  1485. mutex_unlock(&n->dev.mutex);
  1486. return r;
  1487. }
  1488. }
  1489. #ifdef CONFIG_COMPAT
  1490. static long vhost_net_compat_ioctl(struct file *f, unsigned int ioctl,
  1491. unsigned long arg)
  1492. {
  1493. return vhost_net_ioctl(f, ioctl, (unsigned long)compat_ptr(arg));
  1494. }
  1495. #endif
  1496. static ssize_t vhost_net_chr_read_iter(struct kiocb *iocb, struct iov_iter *to)
  1497. {
  1498. struct file *file = iocb->ki_filp;
  1499. struct vhost_net *n = file->private_data;
  1500. struct vhost_dev *dev = &n->dev;
  1501. int noblock = file->f_flags & O_NONBLOCK;
  1502. return vhost_chr_read_iter(dev, to, noblock);
  1503. }
  1504. static ssize_t vhost_net_chr_write_iter(struct kiocb *iocb,
  1505. struct iov_iter *from)
  1506. {
  1507. struct file *file = iocb->ki_filp;
  1508. struct vhost_net *n = file->private_data;
  1509. struct vhost_dev *dev = &n->dev;
  1510. return vhost_chr_write_iter(dev, from);
  1511. }
  1512. static __poll_t vhost_net_chr_poll(struct file *file, poll_table *wait)
  1513. {
  1514. struct vhost_net *n = file->private_data;
  1515. struct vhost_dev *dev = &n->dev;
  1516. return vhost_chr_poll(file, dev, wait);
  1517. }
  1518. static const struct file_operations vhost_net_fops = {
  1519. .owner = THIS_MODULE,
  1520. .release = vhost_net_release,
  1521. .read_iter = vhost_net_chr_read_iter,
  1522. .write_iter = vhost_net_chr_write_iter,
  1523. .poll = vhost_net_chr_poll,
  1524. .unlocked_ioctl = vhost_net_ioctl,
  1525. #ifdef CONFIG_COMPAT
  1526. .compat_ioctl = vhost_net_compat_ioctl,
  1527. #endif
  1528. .open = vhost_net_open,
  1529. .llseek = noop_llseek,
  1530. };
  1531. static struct miscdevice vhost_net_misc = {
  1532. .minor = VHOST_NET_MINOR,
  1533. .name = "vhost-net",
  1534. .fops = &vhost_net_fops,
  1535. };
  1536. static int vhost_net_init(void)
  1537. {
  1538. if (experimental_zcopytx)
  1539. vhost_net_enable_zcopy(VHOST_NET_VQ_TX);
  1540. return misc_register(&vhost_net_misc);
  1541. }
  1542. module_init(vhost_net_init);
  1543. static void vhost_net_exit(void)
  1544. {
  1545. misc_deregister(&vhost_net_misc);
  1546. }
  1547. module_exit(vhost_net_exit);
  1548. MODULE_VERSION("0.0.1");
  1549. MODULE_LICENSE("GPL v2");
  1550. MODULE_AUTHOR("Michael S. Tsirkin");
  1551. MODULE_DESCRIPTION("Host kernel accelerator for virtio net");
  1552. MODULE_ALIAS_MISCDEV(VHOST_NET_MINOR);
  1553. MODULE_ALIAS("devname:vhost-net");