net.c 39 KB

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