net.c 44 KB

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