netback.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049
  1. /*
  2. * Back-end of the driver for virtual network devices. This portion of the
  3. * driver exports a 'unified' network-device interface that can be accessed
  4. * by any operating system that implements a compatible front end. A
  5. * reference front-end implementation can be found in:
  6. * drivers/net/xen-netfront.c
  7. *
  8. * Copyright (c) 2002-2005, K A Fraser
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License version 2
  12. * as published by the Free Software Foundation; or, when distributed
  13. * separately from the Linux kernel or incorporated into other
  14. * software packages, subject to the following license:
  15. *
  16. * Permission is hereby granted, free of charge, to any person obtaining a copy
  17. * of this source file (the "Software"), to deal in the Software without
  18. * restriction, including without limitation the rights to use, copy, modify,
  19. * merge, publish, distribute, sublicense, and/or sell copies of the Software,
  20. * and to permit persons to whom the Software is furnished to do so, subject to
  21. * the following conditions:
  22. *
  23. * The above copyright notice and this permission notice shall be included in
  24. * all copies or substantial portions of the Software.
  25. *
  26. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  27. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  28. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  29. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  30. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  31. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  32. * IN THE SOFTWARE.
  33. */
  34. #include "common.h"
  35. #include <linux/kthread.h>
  36. #include <linux/if_vlan.h>
  37. #include <linux/udp.h>
  38. #include <linux/highmem.h>
  39. #include <net/tcp.h>
  40. #include <xen/xen.h>
  41. #include <xen/events.h>
  42. #include <xen/interface/memory.h>
  43. #include <xen/page.h>
  44. #include <asm/xen/hypercall.h>
  45. /* Provide an option to disable split event channels at load time as
  46. * event channels are limited resource. Split event channels are
  47. * enabled by default.
  48. */
  49. bool separate_tx_rx_irq = true;
  50. module_param(separate_tx_rx_irq, bool, 0644);
  51. /* The time that packets can stay on the guest Rx internal queue
  52. * before they are dropped.
  53. */
  54. unsigned int rx_drain_timeout_msecs = 10000;
  55. module_param(rx_drain_timeout_msecs, uint, 0444);
  56. /* The length of time before the frontend is considered unresponsive
  57. * because it isn't providing Rx slots.
  58. */
  59. unsigned int rx_stall_timeout_msecs = 60000;
  60. module_param(rx_stall_timeout_msecs, uint, 0444);
  61. unsigned int xenvif_max_queues;
  62. module_param_named(max_queues, xenvif_max_queues, uint, 0644);
  63. MODULE_PARM_DESC(max_queues,
  64. "Maximum number of queues per virtual interface");
  65. /*
  66. * This is the maximum slots a skb can have. If a guest sends a skb
  67. * which exceeds this limit it is considered malicious.
  68. */
  69. #define FATAL_SKB_SLOTS_DEFAULT 20
  70. static unsigned int fatal_skb_slots = FATAL_SKB_SLOTS_DEFAULT;
  71. module_param(fatal_skb_slots, uint, 0444);
  72. /* The amount to copy out of the first guest Tx slot into the skb's
  73. * linear area. If the first slot has more data, it will be mapped
  74. * and put into the first frag.
  75. *
  76. * This is sized to avoid pulling headers from the frags for most
  77. * TCP/IP packets.
  78. */
  79. #define XEN_NETBACK_TX_COPY_LEN 128
  80. static void xenvif_idx_release(struct xenvif_queue *queue, u16 pending_idx,
  81. u8 status);
  82. static void make_tx_response(struct xenvif_queue *queue,
  83. struct xen_netif_tx_request *txp,
  84. s8 st);
  85. static void push_tx_responses(struct xenvif_queue *queue);
  86. static inline int tx_work_todo(struct xenvif_queue *queue);
  87. static struct xen_netif_rx_response *make_rx_response(struct xenvif_queue *queue,
  88. u16 id,
  89. s8 st,
  90. u16 offset,
  91. u16 size,
  92. u16 flags);
  93. static inline unsigned long idx_to_pfn(struct xenvif_queue *queue,
  94. u16 idx)
  95. {
  96. return page_to_pfn(queue->mmap_pages[idx]);
  97. }
  98. static inline unsigned long idx_to_kaddr(struct xenvif_queue *queue,
  99. u16 idx)
  100. {
  101. return (unsigned long)pfn_to_kaddr(idx_to_pfn(queue, idx));
  102. }
  103. #define callback_param(vif, pending_idx) \
  104. (vif->pending_tx_info[pending_idx].callback_struct)
  105. /* Find the containing VIF's structure from a pointer in pending_tx_info array
  106. */
  107. static inline struct xenvif_queue *ubuf_to_queue(const struct ubuf_info *ubuf)
  108. {
  109. u16 pending_idx = ubuf->desc;
  110. struct pending_tx_info *temp =
  111. container_of(ubuf, struct pending_tx_info, callback_struct);
  112. return container_of(temp - pending_idx,
  113. struct xenvif_queue,
  114. pending_tx_info[0]);
  115. }
  116. static u16 frag_get_pending_idx(skb_frag_t *frag)
  117. {
  118. return (u16)frag->page_offset;
  119. }
  120. static void frag_set_pending_idx(skb_frag_t *frag, u16 pending_idx)
  121. {
  122. frag->page_offset = pending_idx;
  123. }
  124. static inline pending_ring_idx_t pending_index(unsigned i)
  125. {
  126. return i & (MAX_PENDING_REQS-1);
  127. }
  128. bool xenvif_rx_ring_slots_available(struct xenvif_queue *queue, int needed)
  129. {
  130. RING_IDX prod, cons;
  131. do {
  132. prod = queue->rx.sring->req_prod;
  133. cons = queue->rx.req_cons;
  134. if (prod - cons >= needed)
  135. return true;
  136. queue->rx.sring->req_event = prod + 1;
  137. /* Make sure event is visible before we check prod
  138. * again.
  139. */
  140. mb();
  141. } while (queue->rx.sring->req_prod != prod);
  142. return false;
  143. }
  144. void xenvif_rx_queue_tail(struct xenvif_queue *queue, struct sk_buff *skb)
  145. {
  146. unsigned long flags;
  147. spin_lock_irqsave(&queue->rx_queue.lock, flags);
  148. __skb_queue_tail(&queue->rx_queue, skb);
  149. queue->rx_queue_len += skb->len;
  150. if (queue->rx_queue_len > queue->rx_queue_max)
  151. netif_tx_stop_queue(netdev_get_tx_queue(queue->vif->dev, queue->id));
  152. spin_unlock_irqrestore(&queue->rx_queue.lock, flags);
  153. }
  154. static struct sk_buff *xenvif_rx_dequeue(struct xenvif_queue *queue)
  155. {
  156. struct sk_buff *skb;
  157. spin_lock_irq(&queue->rx_queue.lock);
  158. skb = __skb_dequeue(&queue->rx_queue);
  159. if (skb)
  160. queue->rx_queue_len -= skb->len;
  161. spin_unlock_irq(&queue->rx_queue.lock);
  162. return skb;
  163. }
  164. static void xenvif_rx_queue_maybe_wake(struct xenvif_queue *queue)
  165. {
  166. spin_lock_irq(&queue->rx_queue.lock);
  167. if (queue->rx_queue_len < queue->rx_queue_max)
  168. netif_tx_wake_queue(netdev_get_tx_queue(queue->vif->dev, queue->id));
  169. spin_unlock_irq(&queue->rx_queue.lock);
  170. }
  171. static void xenvif_rx_queue_purge(struct xenvif_queue *queue)
  172. {
  173. struct sk_buff *skb;
  174. while ((skb = xenvif_rx_dequeue(queue)) != NULL)
  175. kfree_skb(skb);
  176. }
  177. static void xenvif_rx_queue_drop_expired(struct xenvif_queue *queue)
  178. {
  179. struct sk_buff *skb;
  180. for(;;) {
  181. skb = skb_peek(&queue->rx_queue);
  182. if (!skb)
  183. break;
  184. if (time_before(jiffies, XENVIF_RX_CB(skb)->expires))
  185. break;
  186. xenvif_rx_dequeue(queue);
  187. kfree_skb(skb);
  188. }
  189. }
  190. struct netrx_pending_operations {
  191. unsigned copy_prod, copy_cons;
  192. unsigned meta_prod, meta_cons;
  193. struct gnttab_copy *copy;
  194. struct xenvif_rx_meta *meta;
  195. int copy_off;
  196. grant_ref_t copy_gref;
  197. };
  198. static struct xenvif_rx_meta *get_next_rx_buffer(struct xenvif_queue *queue,
  199. struct netrx_pending_operations *npo)
  200. {
  201. struct xenvif_rx_meta *meta;
  202. struct xen_netif_rx_request *req;
  203. req = RING_GET_REQUEST(&queue->rx, queue->rx.req_cons++);
  204. meta = npo->meta + npo->meta_prod++;
  205. meta->gso_type = XEN_NETIF_GSO_TYPE_NONE;
  206. meta->gso_size = 0;
  207. meta->size = 0;
  208. meta->id = req->id;
  209. npo->copy_off = 0;
  210. npo->copy_gref = req->gref;
  211. return meta;
  212. }
  213. /*
  214. * Set up the grant operations for this fragment. If it's a flipping
  215. * interface, we also set up the unmap request from here.
  216. */
  217. static void xenvif_gop_frag_copy(struct xenvif_queue *queue, struct sk_buff *skb,
  218. struct netrx_pending_operations *npo,
  219. struct page *page, unsigned long size,
  220. unsigned long offset, int *head)
  221. {
  222. struct gnttab_copy *copy_gop;
  223. struct xenvif_rx_meta *meta;
  224. unsigned long bytes;
  225. int gso_type = XEN_NETIF_GSO_TYPE_NONE;
  226. /* Data must not cross a page boundary. */
  227. BUG_ON(size + offset > PAGE_SIZE<<compound_order(page));
  228. meta = npo->meta + npo->meta_prod - 1;
  229. /* Skip unused frames from start of page */
  230. page += offset >> PAGE_SHIFT;
  231. offset &= ~PAGE_MASK;
  232. while (size > 0) {
  233. struct xen_page_foreign *foreign;
  234. BUG_ON(offset >= PAGE_SIZE);
  235. BUG_ON(npo->copy_off > MAX_BUFFER_OFFSET);
  236. if (npo->copy_off == MAX_BUFFER_OFFSET)
  237. meta = get_next_rx_buffer(queue, npo);
  238. bytes = PAGE_SIZE - offset;
  239. if (bytes > size)
  240. bytes = size;
  241. if (npo->copy_off + bytes > MAX_BUFFER_OFFSET)
  242. bytes = MAX_BUFFER_OFFSET - npo->copy_off;
  243. copy_gop = npo->copy + npo->copy_prod++;
  244. copy_gop->flags = GNTCOPY_dest_gref;
  245. copy_gop->len = bytes;
  246. foreign = xen_page_foreign(page);
  247. if (foreign) {
  248. copy_gop->source.domid = foreign->domid;
  249. copy_gop->source.u.ref = foreign->gref;
  250. copy_gop->flags |= GNTCOPY_source_gref;
  251. } else {
  252. copy_gop->source.domid = DOMID_SELF;
  253. copy_gop->source.u.gmfn =
  254. virt_to_mfn(page_address(page));
  255. }
  256. copy_gop->source.offset = offset;
  257. copy_gop->dest.domid = queue->vif->domid;
  258. copy_gop->dest.offset = npo->copy_off;
  259. copy_gop->dest.u.ref = npo->copy_gref;
  260. npo->copy_off += bytes;
  261. meta->size += bytes;
  262. offset += bytes;
  263. size -= bytes;
  264. /* Next frame */
  265. if (offset == PAGE_SIZE && size) {
  266. BUG_ON(!PageCompound(page));
  267. page++;
  268. offset = 0;
  269. }
  270. /* Leave a gap for the GSO descriptor. */
  271. if (skb_is_gso(skb)) {
  272. if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
  273. gso_type = XEN_NETIF_GSO_TYPE_TCPV4;
  274. else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
  275. gso_type = XEN_NETIF_GSO_TYPE_TCPV6;
  276. }
  277. if (*head && ((1 << gso_type) & queue->vif->gso_mask))
  278. queue->rx.req_cons++;
  279. *head = 0; /* There must be something in this buffer now. */
  280. }
  281. }
  282. /*
  283. * Prepare an SKB to be transmitted to the frontend.
  284. *
  285. * This function is responsible for allocating grant operations, meta
  286. * structures, etc.
  287. *
  288. * It returns the number of meta structures consumed. The number of
  289. * ring slots used is always equal to the number of meta slots used
  290. * plus the number of GSO descriptors used. Currently, we use either
  291. * zero GSO descriptors (for non-GSO packets) or one descriptor (for
  292. * frontend-side LRO).
  293. */
  294. static int xenvif_gop_skb(struct sk_buff *skb,
  295. struct netrx_pending_operations *npo,
  296. struct xenvif_queue *queue)
  297. {
  298. struct xenvif *vif = netdev_priv(skb->dev);
  299. int nr_frags = skb_shinfo(skb)->nr_frags;
  300. int i;
  301. struct xen_netif_rx_request *req;
  302. struct xenvif_rx_meta *meta;
  303. unsigned char *data;
  304. int head = 1;
  305. int old_meta_prod;
  306. int gso_type;
  307. old_meta_prod = npo->meta_prod;
  308. gso_type = XEN_NETIF_GSO_TYPE_NONE;
  309. if (skb_is_gso(skb)) {
  310. if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
  311. gso_type = XEN_NETIF_GSO_TYPE_TCPV4;
  312. else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
  313. gso_type = XEN_NETIF_GSO_TYPE_TCPV6;
  314. }
  315. /* Set up a GSO prefix descriptor, if necessary */
  316. if ((1 << gso_type) & vif->gso_prefix_mask) {
  317. req = RING_GET_REQUEST(&queue->rx, queue->rx.req_cons++);
  318. meta = npo->meta + npo->meta_prod++;
  319. meta->gso_type = gso_type;
  320. meta->gso_size = skb_shinfo(skb)->gso_size;
  321. meta->size = 0;
  322. meta->id = req->id;
  323. }
  324. req = RING_GET_REQUEST(&queue->rx, queue->rx.req_cons++);
  325. meta = npo->meta + npo->meta_prod++;
  326. if ((1 << gso_type) & vif->gso_mask) {
  327. meta->gso_type = gso_type;
  328. meta->gso_size = skb_shinfo(skb)->gso_size;
  329. } else {
  330. meta->gso_type = XEN_NETIF_GSO_TYPE_NONE;
  331. meta->gso_size = 0;
  332. }
  333. meta->size = 0;
  334. meta->id = req->id;
  335. npo->copy_off = 0;
  336. npo->copy_gref = req->gref;
  337. data = skb->data;
  338. while (data < skb_tail_pointer(skb)) {
  339. unsigned int offset = offset_in_page(data);
  340. unsigned int len = PAGE_SIZE - offset;
  341. if (data + len > skb_tail_pointer(skb))
  342. len = skb_tail_pointer(skb) - data;
  343. xenvif_gop_frag_copy(queue, skb, npo,
  344. virt_to_page(data), len, offset, &head);
  345. data += len;
  346. }
  347. for (i = 0; i < nr_frags; i++) {
  348. xenvif_gop_frag_copy(queue, skb, npo,
  349. skb_frag_page(&skb_shinfo(skb)->frags[i]),
  350. skb_frag_size(&skb_shinfo(skb)->frags[i]),
  351. skb_shinfo(skb)->frags[i].page_offset,
  352. &head);
  353. }
  354. return npo->meta_prod - old_meta_prod;
  355. }
  356. /*
  357. * This is a twin to xenvif_gop_skb. Assume that xenvif_gop_skb was
  358. * used to set up the operations on the top of
  359. * netrx_pending_operations, which have since been done. Check that
  360. * they didn't give any errors and advance over them.
  361. */
  362. static int xenvif_check_gop(struct xenvif *vif, int nr_meta_slots,
  363. struct netrx_pending_operations *npo)
  364. {
  365. struct gnttab_copy *copy_op;
  366. int status = XEN_NETIF_RSP_OKAY;
  367. int i;
  368. for (i = 0; i < nr_meta_slots; i++) {
  369. copy_op = npo->copy + npo->copy_cons++;
  370. if (copy_op->status != GNTST_okay) {
  371. netdev_dbg(vif->dev,
  372. "Bad status %d from copy to DOM%d.\n",
  373. copy_op->status, vif->domid);
  374. status = XEN_NETIF_RSP_ERROR;
  375. }
  376. }
  377. return status;
  378. }
  379. static void xenvif_add_frag_responses(struct xenvif_queue *queue, int status,
  380. struct xenvif_rx_meta *meta,
  381. int nr_meta_slots)
  382. {
  383. int i;
  384. unsigned long offset;
  385. /* No fragments used */
  386. if (nr_meta_slots <= 1)
  387. return;
  388. nr_meta_slots--;
  389. for (i = 0; i < nr_meta_slots; i++) {
  390. int flags;
  391. if (i == nr_meta_slots - 1)
  392. flags = 0;
  393. else
  394. flags = XEN_NETRXF_more_data;
  395. offset = 0;
  396. make_rx_response(queue, meta[i].id, status, offset,
  397. meta[i].size, flags);
  398. }
  399. }
  400. void xenvif_kick_thread(struct xenvif_queue *queue)
  401. {
  402. wake_up(&queue->wq);
  403. }
  404. static void xenvif_rx_action(struct xenvif_queue *queue)
  405. {
  406. s8 status;
  407. u16 flags;
  408. struct xen_netif_rx_response *resp;
  409. struct sk_buff_head rxq;
  410. struct sk_buff *skb;
  411. LIST_HEAD(notify);
  412. int ret;
  413. unsigned long offset;
  414. bool need_to_notify = false;
  415. struct netrx_pending_operations npo = {
  416. .copy = queue->grant_copy_op,
  417. .meta = queue->meta,
  418. };
  419. skb_queue_head_init(&rxq);
  420. while (xenvif_rx_ring_slots_available(queue, XEN_NETBK_RX_SLOTS_MAX)
  421. && (skb = xenvif_rx_dequeue(queue)) != NULL) {
  422. queue->last_rx_time = jiffies;
  423. XENVIF_RX_CB(skb)->meta_slots_used = xenvif_gop_skb(skb, &npo, queue);
  424. __skb_queue_tail(&rxq, skb);
  425. }
  426. BUG_ON(npo.meta_prod > ARRAY_SIZE(queue->meta));
  427. if (!npo.copy_prod)
  428. goto done;
  429. BUG_ON(npo.copy_prod > MAX_GRANT_COPY_OPS);
  430. gnttab_batch_copy(queue->grant_copy_op, npo.copy_prod);
  431. while ((skb = __skb_dequeue(&rxq)) != NULL) {
  432. if ((1 << queue->meta[npo.meta_cons].gso_type) &
  433. queue->vif->gso_prefix_mask) {
  434. resp = RING_GET_RESPONSE(&queue->rx,
  435. queue->rx.rsp_prod_pvt++);
  436. resp->flags = XEN_NETRXF_gso_prefix | XEN_NETRXF_more_data;
  437. resp->offset = queue->meta[npo.meta_cons].gso_size;
  438. resp->id = queue->meta[npo.meta_cons].id;
  439. resp->status = XENVIF_RX_CB(skb)->meta_slots_used;
  440. npo.meta_cons++;
  441. XENVIF_RX_CB(skb)->meta_slots_used--;
  442. }
  443. queue->stats.tx_bytes += skb->len;
  444. queue->stats.tx_packets++;
  445. status = xenvif_check_gop(queue->vif,
  446. XENVIF_RX_CB(skb)->meta_slots_used,
  447. &npo);
  448. if (XENVIF_RX_CB(skb)->meta_slots_used == 1)
  449. flags = 0;
  450. else
  451. flags = XEN_NETRXF_more_data;
  452. if (skb->ip_summed == CHECKSUM_PARTIAL) /* local packet? */
  453. flags |= XEN_NETRXF_csum_blank | XEN_NETRXF_data_validated;
  454. else if (skb->ip_summed == CHECKSUM_UNNECESSARY)
  455. /* remote but checksummed. */
  456. flags |= XEN_NETRXF_data_validated;
  457. offset = 0;
  458. resp = make_rx_response(queue, queue->meta[npo.meta_cons].id,
  459. status, offset,
  460. queue->meta[npo.meta_cons].size,
  461. flags);
  462. if ((1 << queue->meta[npo.meta_cons].gso_type) &
  463. queue->vif->gso_mask) {
  464. struct xen_netif_extra_info *gso =
  465. (struct xen_netif_extra_info *)
  466. RING_GET_RESPONSE(&queue->rx,
  467. queue->rx.rsp_prod_pvt++);
  468. resp->flags |= XEN_NETRXF_extra_info;
  469. gso->u.gso.type = queue->meta[npo.meta_cons].gso_type;
  470. gso->u.gso.size = queue->meta[npo.meta_cons].gso_size;
  471. gso->u.gso.pad = 0;
  472. gso->u.gso.features = 0;
  473. gso->type = XEN_NETIF_EXTRA_TYPE_GSO;
  474. gso->flags = 0;
  475. }
  476. xenvif_add_frag_responses(queue, status,
  477. queue->meta + npo.meta_cons + 1,
  478. XENVIF_RX_CB(skb)->meta_slots_used);
  479. RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&queue->rx, ret);
  480. need_to_notify |= !!ret;
  481. npo.meta_cons += XENVIF_RX_CB(skb)->meta_slots_used;
  482. dev_kfree_skb(skb);
  483. }
  484. done:
  485. if (need_to_notify)
  486. notify_remote_via_irq(queue->rx_irq);
  487. }
  488. void xenvif_napi_schedule_or_enable_events(struct xenvif_queue *queue)
  489. {
  490. int more_to_do;
  491. RING_FINAL_CHECK_FOR_REQUESTS(&queue->tx, more_to_do);
  492. if (more_to_do)
  493. napi_schedule(&queue->napi);
  494. }
  495. static void tx_add_credit(struct xenvif_queue *queue)
  496. {
  497. unsigned long max_burst, max_credit;
  498. /*
  499. * Allow a burst big enough to transmit a jumbo packet of up to 128kB.
  500. * Otherwise the interface can seize up due to insufficient credit.
  501. */
  502. max_burst = RING_GET_REQUEST(&queue->tx, queue->tx.req_cons)->size;
  503. max_burst = min(max_burst, 131072UL);
  504. max_burst = max(max_burst, queue->credit_bytes);
  505. /* Take care that adding a new chunk of credit doesn't wrap to zero. */
  506. max_credit = queue->remaining_credit + queue->credit_bytes;
  507. if (max_credit < queue->remaining_credit)
  508. max_credit = ULONG_MAX; /* wrapped: clamp to ULONG_MAX */
  509. queue->remaining_credit = min(max_credit, max_burst);
  510. }
  511. void xenvif_tx_credit_callback(unsigned long data)
  512. {
  513. struct xenvif_queue *queue = (struct xenvif_queue *)data;
  514. tx_add_credit(queue);
  515. xenvif_napi_schedule_or_enable_events(queue);
  516. }
  517. static void xenvif_tx_err(struct xenvif_queue *queue,
  518. struct xen_netif_tx_request *txp, RING_IDX end)
  519. {
  520. RING_IDX cons = queue->tx.req_cons;
  521. unsigned long flags;
  522. do {
  523. spin_lock_irqsave(&queue->response_lock, flags);
  524. make_tx_response(queue, txp, XEN_NETIF_RSP_ERROR);
  525. push_tx_responses(queue);
  526. spin_unlock_irqrestore(&queue->response_lock, flags);
  527. if (cons == end)
  528. break;
  529. txp = RING_GET_REQUEST(&queue->tx, cons++);
  530. } while (1);
  531. queue->tx.req_cons = cons;
  532. }
  533. static void xenvif_fatal_tx_err(struct xenvif *vif)
  534. {
  535. netdev_err(vif->dev, "fatal error; disabling device\n");
  536. vif->disabled = true;
  537. /* Disable the vif from queue 0's kthread */
  538. if (vif->queues)
  539. xenvif_kick_thread(&vif->queues[0]);
  540. }
  541. static int xenvif_count_requests(struct xenvif_queue *queue,
  542. struct xen_netif_tx_request *first,
  543. struct xen_netif_tx_request *txp,
  544. int work_to_do)
  545. {
  546. RING_IDX cons = queue->tx.req_cons;
  547. int slots = 0;
  548. int drop_err = 0;
  549. int more_data;
  550. if (!(first->flags & XEN_NETTXF_more_data))
  551. return 0;
  552. do {
  553. struct xen_netif_tx_request dropped_tx = { 0 };
  554. if (slots >= work_to_do) {
  555. netdev_err(queue->vif->dev,
  556. "Asked for %d slots but exceeds this limit\n",
  557. work_to_do);
  558. xenvif_fatal_tx_err(queue->vif);
  559. return -ENODATA;
  560. }
  561. /* This guest is really using too many slots and
  562. * considered malicious.
  563. */
  564. if (unlikely(slots >= fatal_skb_slots)) {
  565. netdev_err(queue->vif->dev,
  566. "Malicious frontend using %d slots, threshold %u\n",
  567. slots, fatal_skb_slots);
  568. xenvif_fatal_tx_err(queue->vif);
  569. return -E2BIG;
  570. }
  571. /* Xen network protocol had implicit dependency on
  572. * MAX_SKB_FRAGS. XEN_NETBK_LEGACY_SLOTS_MAX is set to
  573. * the historical MAX_SKB_FRAGS value 18 to honor the
  574. * same behavior as before. Any packet using more than
  575. * 18 slots but less than fatal_skb_slots slots is
  576. * dropped
  577. */
  578. if (!drop_err && slots >= XEN_NETBK_LEGACY_SLOTS_MAX) {
  579. if (net_ratelimit())
  580. netdev_dbg(queue->vif->dev,
  581. "Too many slots (%d) exceeding limit (%d), dropping packet\n",
  582. slots, XEN_NETBK_LEGACY_SLOTS_MAX);
  583. drop_err = -E2BIG;
  584. }
  585. if (drop_err)
  586. txp = &dropped_tx;
  587. memcpy(txp, RING_GET_REQUEST(&queue->tx, cons + slots),
  588. sizeof(*txp));
  589. /* If the guest submitted a frame >= 64 KiB then
  590. * first->size overflowed and following slots will
  591. * appear to be larger than the frame.
  592. *
  593. * This cannot be fatal error as there are buggy
  594. * frontends that do this.
  595. *
  596. * Consume all slots and drop the packet.
  597. */
  598. if (!drop_err && txp->size > first->size) {
  599. if (net_ratelimit())
  600. netdev_dbg(queue->vif->dev,
  601. "Invalid tx request, slot size %u > remaining size %u\n",
  602. txp->size, first->size);
  603. drop_err = -EIO;
  604. }
  605. first->size -= txp->size;
  606. slots++;
  607. if (unlikely((txp->offset + txp->size) > PAGE_SIZE)) {
  608. netdev_err(queue->vif->dev, "Cross page boundary, txp->offset: %u, size: %u\n",
  609. txp->offset, txp->size);
  610. xenvif_fatal_tx_err(queue->vif);
  611. return -EINVAL;
  612. }
  613. more_data = txp->flags & XEN_NETTXF_more_data;
  614. if (!drop_err)
  615. txp++;
  616. } while (more_data);
  617. if (drop_err) {
  618. xenvif_tx_err(queue, first, cons + slots);
  619. return drop_err;
  620. }
  621. return slots;
  622. }
  623. struct xenvif_tx_cb {
  624. u16 pending_idx;
  625. };
  626. #define XENVIF_TX_CB(skb) ((struct xenvif_tx_cb *)(skb)->cb)
  627. static inline void xenvif_tx_create_map_op(struct xenvif_queue *queue,
  628. u16 pending_idx,
  629. struct xen_netif_tx_request *txp,
  630. struct gnttab_map_grant_ref *mop)
  631. {
  632. queue->pages_to_map[mop-queue->tx_map_ops] = queue->mmap_pages[pending_idx];
  633. gnttab_set_map_op(mop, idx_to_kaddr(queue, pending_idx),
  634. GNTMAP_host_map | GNTMAP_readonly,
  635. txp->gref, queue->vif->domid);
  636. memcpy(&queue->pending_tx_info[pending_idx].req, txp,
  637. sizeof(*txp));
  638. }
  639. static inline struct sk_buff *xenvif_alloc_skb(unsigned int size)
  640. {
  641. struct sk_buff *skb =
  642. alloc_skb(size + NET_SKB_PAD + NET_IP_ALIGN,
  643. GFP_ATOMIC | __GFP_NOWARN);
  644. if (unlikely(skb == NULL))
  645. return NULL;
  646. /* Packets passed to netif_rx() must have some headroom. */
  647. skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
  648. /* Initialize it here to avoid later surprises */
  649. skb_shinfo(skb)->destructor_arg = NULL;
  650. return skb;
  651. }
  652. static struct gnttab_map_grant_ref *xenvif_get_requests(struct xenvif_queue *queue,
  653. struct sk_buff *skb,
  654. struct xen_netif_tx_request *txp,
  655. struct gnttab_map_grant_ref *gop,
  656. unsigned int frag_overflow,
  657. struct sk_buff *nskb)
  658. {
  659. struct skb_shared_info *shinfo = skb_shinfo(skb);
  660. skb_frag_t *frags = shinfo->frags;
  661. u16 pending_idx = XENVIF_TX_CB(skb)->pending_idx;
  662. int start;
  663. pending_ring_idx_t index;
  664. unsigned int nr_slots;
  665. nr_slots = shinfo->nr_frags;
  666. /* Skip first skb fragment if it is on same page as header fragment. */
  667. start = (frag_get_pending_idx(&shinfo->frags[0]) == pending_idx);
  668. for (shinfo->nr_frags = start; shinfo->nr_frags < nr_slots;
  669. shinfo->nr_frags++, txp++, gop++) {
  670. index = pending_index(queue->pending_cons++);
  671. pending_idx = queue->pending_ring[index];
  672. xenvif_tx_create_map_op(queue, pending_idx, txp, gop);
  673. frag_set_pending_idx(&frags[shinfo->nr_frags], pending_idx);
  674. }
  675. if (frag_overflow) {
  676. shinfo = skb_shinfo(nskb);
  677. frags = shinfo->frags;
  678. for (shinfo->nr_frags = 0; shinfo->nr_frags < frag_overflow;
  679. shinfo->nr_frags++, txp++, gop++) {
  680. index = pending_index(queue->pending_cons++);
  681. pending_idx = queue->pending_ring[index];
  682. xenvif_tx_create_map_op(queue, pending_idx, txp, gop);
  683. frag_set_pending_idx(&frags[shinfo->nr_frags],
  684. pending_idx);
  685. }
  686. skb_shinfo(skb)->frag_list = nskb;
  687. }
  688. return gop;
  689. }
  690. static inline void xenvif_grant_handle_set(struct xenvif_queue *queue,
  691. u16 pending_idx,
  692. grant_handle_t handle)
  693. {
  694. if (unlikely(queue->grant_tx_handle[pending_idx] !=
  695. NETBACK_INVALID_HANDLE)) {
  696. netdev_err(queue->vif->dev,
  697. "Trying to overwrite active handle! pending_idx: 0x%x\n",
  698. pending_idx);
  699. BUG();
  700. }
  701. queue->grant_tx_handle[pending_idx] = handle;
  702. }
  703. static inline void xenvif_grant_handle_reset(struct xenvif_queue *queue,
  704. u16 pending_idx)
  705. {
  706. if (unlikely(queue->grant_tx_handle[pending_idx] ==
  707. NETBACK_INVALID_HANDLE)) {
  708. netdev_err(queue->vif->dev,
  709. "Trying to unmap invalid handle! pending_idx: 0x%x\n",
  710. pending_idx);
  711. BUG();
  712. }
  713. queue->grant_tx_handle[pending_idx] = NETBACK_INVALID_HANDLE;
  714. }
  715. static int xenvif_tx_check_gop(struct xenvif_queue *queue,
  716. struct sk_buff *skb,
  717. struct gnttab_map_grant_ref **gopp_map,
  718. struct gnttab_copy **gopp_copy)
  719. {
  720. struct gnttab_map_grant_ref *gop_map = *gopp_map;
  721. u16 pending_idx = XENVIF_TX_CB(skb)->pending_idx;
  722. /* This always points to the shinfo of the skb being checked, which
  723. * could be either the first or the one on the frag_list
  724. */
  725. struct skb_shared_info *shinfo = skb_shinfo(skb);
  726. /* If this is non-NULL, we are currently checking the frag_list skb, and
  727. * this points to the shinfo of the first one
  728. */
  729. struct skb_shared_info *first_shinfo = NULL;
  730. int nr_frags = shinfo->nr_frags;
  731. const bool sharedslot = nr_frags &&
  732. frag_get_pending_idx(&shinfo->frags[0]) == pending_idx;
  733. int i, err;
  734. /* Check status of header. */
  735. err = (*gopp_copy)->status;
  736. if (unlikely(err)) {
  737. if (net_ratelimit())
  738. netdev_dbg(queue->vif->dev,
  739. "Grant copy of header failed! status: %d pending_idx: %u ref: %u\n",
  740. (*gopp_copy)->status,
  741. pending_idx,
  742. (*gopp_copy)->source.u.ref);
  743. /* The first frag might still have this slot mapped */
  744. if (!sharedslot)
  745. xenvif_idx_release(queue, pending_idx,
  746. XEN_NETIF_RSP_ERROR);
  747. }
  748. (*gopp_copy)++;
  749. check_frags:
  750. for (i = 0; i < nr_frags; i++, gop_map++) {
  751. int j, newerr;
  752. pending_idx = frag_get_pending_idx(&shinfo->frags[i]);
  753. /* Check error status: if okay then remember grant handle. */
  754. newerr = gop_map->status;
  755. if (likely(!newerr)) {
  756. xenvif_grant_handle_set(queue,
  757. pending_idx,
  758. gop_map->handle);
  759. /* Had a previous error? Invalidate this fragment. */
  760. if (unlikely(err)) {
  761. xenvif_idx_unmap(queue, pending_idx);
  762. /* If the mapping of the first frag was OK, but
  763. * the header's copy failed, and they are
  764. * sharing a slot, send an error
  765. */
  766. if (i == 0 && sharedslot)
  767. xenvif_idx_release(queue, pending_idx,
  768. XEN_NETIF_RSP_ERROR);
  769. else
  770. xenvif_idx_release(queue, pending_idx,
  771. XEN_NETIF_RSP_OKAY);
  772. }
  773. continue;
  774. }
  775. /* Error on this fragment: respond to client with an error. */
  776. if (net_ratelimit())
  777. netdev_dbg(queue->vif->dev,
  778. "Grant map of %d. frag failed! status: %d pending_idx: %u ref: %u\n",
  779. i,
  780. gop_map->status,
  781. pending_idx,
  782. gop_map->ref);
  783. xenvif_idx_release(queue, pending_idx, XEN_NETIF_RSP_ERROR);
  784. /* Not the first error? Preceding frags already invalidated. */
  785. if (err)
  786. continue;
  787. /* First error: if the header haven't shared a slot with the
  788. * first frag, release it as well.
  789. */
  790. if (!sharedslot)
  791. xenvif_idx_release(queue,
  792. XENVIF_TX_CB(skb)->pending_idx,
  793. XEN_NETIF_RSP_OKAY);
  794. /* Invalidate preceding fragments of this skb. */
  795. for (j = 0; j < i; j++) {
  796. pending_idx = frag_get_pending_idx(&shinfo->frags[j]);
  797. xenvif_idx_unmap(queue, pending_idx);
  798. xenvif_idx_release(queue, pending_idx,
  799. XEN_NETIF_RSP_OKAY);
  800. }
  801. /* And if we found the error while checking the frag_list, unmap
  802. * the first skb's frags
  803. */
  804. if (first_shinfo) {
  805. for (j = 0; j < first_shinfo->nr_frags; j++) {
  806. pending_idx = frag_get_pending_idx(&first_shinfo->frags[j]);
  807. xenvif_idx_unmap(queue, pending_idx);
  808. xenvif_idx_release(queue, pending_idx,
  809. XEN_NETIF_RSP_OKAY);
  810. }
  811. }
  812. /* Remember the error: invalidate all subsequent fragments. */
  813. err = newerr;
  814. }
  815. if (skb_has_frag_list(skb) && !first_shinfo) {
  816. first_shinfo = skb_shinfo(skb);
  817. shinfo = skb_shinfo(skb_shinfo(skb)->frag_list);
  818. nr_frags = shinfo->nr_frags;
  819. goto check_frags;
  820. }
  821. *gopp_map = gop_map;
  822. return err;
  823. }
  824. static void xenvif_fill_frags(struct xenvif_queue *queue, struct sk_buff *skb)
  825. {
  826. struct skb_shared_info *shinfo = skb_shinfo(skb);
  827. int nr_frags = shinfo->nr_frags;
  828. int i;
  829. u16 prev_pending_idx = INVALID_PENDING_IDX;
  830. for (i = 0; i < nr_frags; i++) {
  831. skb_frag_t *frag = shinfo->frags + i;
  832. struct xen_netif_tx_request *txp;
  833. struct page *page;
  834. u16 pending_idx;
  835. pending_idx = frag_get_pending_idx(frag);
  836. /* If this is not the first frag, chain it to the previous*/
  837. if (prev_pending_idx == INVALID_PENDING_IDX)
  838. skb_shinfo(skb)->destructor_arg =
  839. &callback_param(queue, pending_idx);
  840. else
  841. callback_param(queue, prev_pending_idx).ctx =
  842. &callback_param(queue, pending_idx);
  843. callback_param(queue, pending_idx).ctx = NULL;
  844. prev_pending_idx = pending_idx;
  845. txp = &queue->pending_tx_info[pending_idx].req;
  846. page = virt_to_page(idx_to_kaddr(queue, pending_idx));
  847. __skb_fill_page_desc(skb, i, page, txp->offset, txp->size);
  848. skb->len += txp->size;
  849. skb->data_len += txp->size;
  850. skb->truesize += txp->size;
  851. /* Take an extra reference to offset network stack's put_page */
  852. get_page(queue->mmap_pages[pending_idx]);
  853. }
  854. }
  855. static int xenvif_get_extras(struct xenvif_queue *queue,
  856. struct xen_netif_extra_info *extras,
  857. int work_to_do)
  858. {
  859. struct xen_netif_extra_info extra;
  860. RING_IDX cons = queue->tx.req_cons;
  861. do {
  862. if (unlikely(work_to_do-- <= 0)) {
  863. netdev_err(queue->vif->dev, "Missing extra info\n");
  864. xenvif_fatal_tx_err(queue->vif);
  865. return -EBADR;
  866. }
  867. memcpy(&extra, RING_GET_REQUEST(&queue->tx, cons),
  868. sizeof(extra));
  869. if (unlikely(!extra.type ||
  870. extra.type >= XEN_NETIF_EXTRA_TYPE_MAX)) {
  871. queue->tx.req_cons = ++cons;
  872. netdev_err(queue->vif->dev,
  873. "Invalid extra type: %d\n", extra.type);
  874. xenvif_fatal_tx_err(queue->vif);
  875. return -EINVAL;
  876. }
  877. memcpy(&extras[extra.type - 1], &extra, sizeof(extra));
  878. queue->tx.req_cons = ++cons;
  879. } while (extra.flags & XEN_NETIF_EXTRA_FLAG_MORE);
  880. return work_to_do;
  881. }
  882. static int xenvif_set_skb_gso(struct xenvif *vif,
  883. struct sk_buff *skb,
  884. struct xen_netif_extra_info *gso)
  885. {
  886. if (!gso->u.gso.size) {
  887. netdev_err(vif->dev, "GSO size must not be zero.\n");
  888. xenvif_fatal_tx_err(vif);
  889. return -EINVAL;
  890. }
  891. switch (gso->u.gso.type) {
  892. case XEN_NETIF_GSO_TYPE_TCPV4:
  893. skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
  894. break;
  895. case XEN_NETIF_GSO_TYPE_TCPV6:
  896. skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
  897. break;
  898. default:
  899. netdev_err(vif->dev, "Bad GSO type %d.\n", gso->u.gso.type);
  900. xenvif_fatal_tx_err(vif);
  901. return -EINVAL;
  902. }
  903. skb_shinfo(skb)->gso_size = gso->u.gso.size;
  904. /* gso_segs will be calculated later */
  905. return 0;
  906. }
  907. static int checksum_setup(struct xenvif_queue *queue, struct sk_buff *skb)
  908. {
  909. bool recalculate_partial_csum = false;
  910. /* A GSO SKB must be CHECKSUM_PARTIAL. However some buggy
  911. * peers can fail to set NETRXF_csum_blank when sending a GSO
  912. * frame. In this case force the SKB to CHECKSUM_PARTIAL and
  913. * recalculate the partial checksum.
  914. */
  915. if (skb->ip_summed != CHECKSUM_PARTIAL && skb_is_gso(skb)) {
  916. queue->stats.rx_gso_checksum_fixup++;
  917. skb->ip_summed = CHECKSUM_PARTIAL;
  918. recalculate_partial_csum = true;
  919. }
  920. /* A non-CHECKSUM_PARTIAL SKB does not require setup. */
  921. if (skb->ip_summed != CHECKSUM_PARTIAL)
  922. return 0;
  923. return skb_checksum_setup(skb, recalculate_partial_csum);
  924. }
  925. static bool tx_credit_exceeded(struct xenvif_queue *queue, unsigned size)
  926. {
  927. u64 now = get_jiffies_64();
  928. u64 next_credit = queue->credit_window_start +
  929. msecs_to_jiffies(queue->credit_usec / 1000);
  930. /* Timer could already be pending in rare cases. */
  931. if (timer_pending(&queue->credit_timeout))
  932. return true;
  933. /* Passed the point where we can replenish credit? */
  934. if (time_after_eq64(now, next_credit)) {
  935. queue->credit_window_start = now;
  936. tx_add_credit(queue);
  937. }
  938. /* Still too big to send right now? Set a callback. */
  939. if (size > queue->remaining_credit) {
  940. queue->credit_timeout.data =
  941. (unsigned long)queue;
  942. mod_timer(&queue->credit_timeout,
  943. next_credit);
  944. queue->credit_window_start = next_credit;
  945. return true;
  946. }
  947. return false;
  948. }
  949. static void xenvif_tx_build_gops(struct xenvif_queue *queue,
  950. int budget,
  951. unsigned *copy_ops,
  952. unsigned *map_ops)
  953. {
  954. struct gnttab_map_grant_ref *gop = queue->tx_map_ops;
  955. struct sk_buff *skb, *nskb;
  956. int ret;
  957. unsigned int frag_overflow;
  958. while (skb_queue_len(&queue->tx_queue) < budget) {
  959. struct xen_netif_tx_request txreq;
  960. struct xen_netif_tx_request txfrags[XEN_NETBK_LEGACY_SLOTS_MAX];
  961. struct xen_netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX-1];
  962. u16 pending_idx;
  963. RING_IDX idx;
  964. int work_to_do;
  965. unsigned int data_len;
  966. pending_ring_idx_t index;
  967. if (queue->tx.sring->req_prod - queue->tx.req_cons >
  968. XEN_NETIF_TX_RING_SIZE) {
  969. netdev_err(queue->vif->dev,
  970. "Impossible number of requests. "
  971. "req_prod %d, req_cons %d, size %ld\n",
  972. queue->tx.sring->req_prod, queue->tx.req_cons,
  973. XEN_NETIF_TX_RING_SIZE);
  974. xenvif_fatal_tx_err(queue->vif);
  975. break;
  976. }
  977. work_to_do = RING_HAS_UNCONSUMED_REQUESTS(&queue->tx);
  978. if (!work_to_do)
  979. break;
  980. idx = queue->tx.req_cons;
  981. rmb(); /* Ensure that we see the request before we copy it. */
  982. memcpy(&txreq, RING_GET_REQUEST(&queue->tx, idx), sizeof(txreq));
  983. /* Credit-based scheduling. */
  984. if (txreq.size > queue->remaining_credit &&
  985. tx_credit_exceeded(queue, txreq.size))
  986. break;
  987. queue->remaining_credit -= txreq.size;
  988. work_to_do--;
  989. queue->tx.req_cons = ++idx;
  990. memset(extras, 0, sizeof(extras));
  991. if (txreq.flags & XEN_NETTXF_extra_info) {
  992. work_to_do = xenvif_get_extras(queue, extras,
  993. work_to_do);
  994. idx = queue->tx.req_cons;
  995. if (unlikely(work_to_do < 0))
  996. break;
  997. }
  998. ret = xenvif_count_requests(queue, &txreq, txfrags, work_to_do);
  999. if (unlikely(ret < 0))
  1000. break;
  1001. idx += ret;
  1002. if (unlikely(txreq.size < ETH_HLEN)) {
  1003. netdev_dbg(queue->vif->dev,
  1004. "Bad packet size: %d\n", txreq.size);
  1005. xenvif_tx_err(queue, &txreq, idx);
  1006. break;
  1007. }
  1008. /* No crossing a page as the payload mustn't fragment. */
  1009. if (unlikely((txreq.offset + txreq.size) > PAGE_SIZE)) {
  1010. netdev_err(queue->vif->dev,
  1011. "txreq.offset: %u, size: %u, end: %lu\n",
  1012. txreq.offset, txreq.size,
  1013. (unsigned long)(txreq.offset&~PAGE_MASK) + txreq.size);
  1014. xenvif_fatal_tx_err(queue->vif);
  1015. break;
  1016. }
  1017. index = pending_index(queue->pending_cons);
  1018. pending_idx = queue->pending_ring[index];
  1019. data_len = (txreq.size > XEN_NETBACK_TX_COPY_LEN &&
  1020. ret < XEN_NETBK_LEGACY_SLOTS_MAX) ?
  1021. XEN_NETBACK_TX_COPY_LEN : txreq.size;
  1022. skb = xenvif_alloc_skb(data_len);
  1023. if (unlikely(skb == NULL)) {
  1024. netdev_dbg(queue->vif->dev,
  1025. "Can't allocate a skb in start_xmit.\n");
  1026. xenvif_tx_err(queue, &txreq, idx);
  1027. break;
  1028. }
  1029. skb_shinfo(skb)->nr_frags = ret;
  1030. if (data_len < txreq.size)
  1031. skb_shinfo(skb)->nr_frags++;
  1032. /* At this point shinfo->nr_frags is in fact the number of
  1033. * slots, which can be as large as XEN_NETBK_LEGACY_SLOTS_MAX.
  1034. */
  1035. frag_overflow = 0;
  1036. nskb = NULL;
  1037. if (skb_shinfo(skb)->nr_frags > MAX_SKB_FRAGS) {
  1038. frag_overflow = skb_shinfo(skb)->nr_frags - MAX_SKB_FRAGS;
  1039. BUG_ON(frag_overflow > MAX_SKB_FRAGS);
  1040. skb_shinfo(skb)->nr_frags = MAX_SKB_FRAGS;
  1041. nskb = xenvif_alloc_skb(0);
  1042. if (unlikely(nskb == NULL)) {
  1043. kfree_skb(skb);
  1044. xenvif_tx_err(queue, &txreq, idx);
  1045. if (net_ratelimit())
  1046. netdev_err(queue->vif->dev,
  1047. "Can't allocate the frag_list skb.\n");
  1048. break;
  1049. }
  1050. }
  1051. if (extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].type) {
  1052. struct xen_netif_extra_info *gso;
  1053. gso = &extras[XEN_NETIF_EXTRA_TYPE_GSO - 1];
  1054. if (xenvif_set_skb_gso(queue->vif, skb, gso)) {
  1055. /* Failure in xenvif_set_skb_gso is fatal. */
  1056. kfree_skb(skb);
  1057. kfree_skb(nskb);
  1058. break;
  1059. }
  1060. }
  1061. XENVIF_TX_CB(skb)->pending_idx = pending_idx;
  1062. __skb_put(skb, data_len);
  1063. queue->tx_copy_ops[*copy_ops].source.u.ref = txreq.gref;
  1064. queue->tx_copy_ops[*copy_ops].source.domid = queue->vif->domid;
  1065. queue->tx_copy_ops[*copy_ops].source.offset = txreq.offset;
  1066. queue->tx_copy_ops[*copy_ops].dest.u.gmfn =
  1067. virt_to_mfn(skb->data);
  1068. queue->tx_copy_ops[*copy_ops].dest.domid = DOMID_SELF;
  1069. queue->tx_copy_ops[*copy_ops].dest.offset =
  1070. offset_in_page(skb->data);
  1071. queue->tx_copy_ops[*copy_ops].len = data_len;
  1072. queue->tx_copy_ops[*copy_ops].flags = GNTCOPY_source_gref;
  1073. (*copy_ops)++;
  1074. if (data_len < txreq.size) {
  1075. frag_set_pending_idx(&skb_shinfo(skb)->frags[0],
  1076. pending_idx);
  1077. xenvif_tx_create_map_op(queue, pending_idx, &txreq, gop);
  1078. gop++;
  1079. } else {
  1080. frag_set_pending_idx(&skb_shinfo(skb)->frags[0],
  1081. INVALID_PENDING_IDX);
  1082. memcpy(&queue->pending_tx_info[pending_idx].req, &txreq,
  1083. sizeof(txreq));
  1084. }
  1085. queue->pending_cons++;
  1086. gop = xenvif_get_requests(queue, skb, txfrags, gop,
  1087. frag_overflow, nskb);
  1088. __skb_queue_tail(&queue->tx_queue, skb);
  1089. queue->tx.req_cons = idx;
  1090. if (((gop-queue->tx_map_ops) >= ARRAY_SIZE(queue->tx_map_ops)) ||
  1091. (*copy_ops >= ARRAY_SIZE(queue->tx_copy_ops)))
  1092. break;
  1093. }
  1094. (*map_ops) = gop - queue->tx_map_ops;
  1095. return;
  1096. }
  1097. /* Consolidate skb with a frag_list into a brand new one with local pages on
  1098. * frags. Returns 0 or -ENOMEM if can't allocate new pages.
  1099. */
  1100. static int xenvif_handle_frag_list(struct xenvif_queue *queue, struct sk_buff *skb)
  1101. {
  1102. unsigned int offset = skb_headlen(skb);
  1103. skb_frag_t frags[MAX_SKB_FRAGS];
  1104. int i, f;
  1105. struct ubuf_info *uarg;
  1106. struct sk_buff *nskb = skb_shinfo(skb)->frag_list;
  1107. queue->stats.tx_zerocopy_sent += 2;
  1108. queue->stats.tx_frag_overflow++;
  1109. xenvif_fill_frags(queue, nskb);
  1110. /* Subtract frags size, we will correct it later */
  1111. skb->truesize -= skb->data_len;
  1112. skb->len += nskb->len;
  1113. skb->data_len += nskb->len;
  1114. /* create a brand new frags array and coalesce there */
  1115. for (i = 0; offset < skb->len; i++) {
  1116. struct page *page;
  1117. unsigned int len;
  1118. BUG_ON(i >= MAX_SKB_FRAGS);
  1119. page = alloc_page(GFP_ATOMIC);
  1120. if (!page) {
  1121. int j;
  1122. skb->truesize += skb->data_len;
  1123. for (j = 0; j < i; j++)
  1124. put_page(frags[j].page.p);
  1125. return -ENOMEM;
  1126. }
  1127. if (offset + PAGE_SIZE < skb->len)
  1128. len = PAGE_SIZE;
  1129. else
  1130. len = skb->len - offset;
  1131. if (skb_copy_bits(skb, offset, page_address(page), len))
  1132. BUG();
  1133. offset += len;
  1134. frags[i].page.p = page;
  1135. frags[i].page_offset = 0;
  1136. skb_frag_size_set(&frags[i], len);
  1137. }
  1138. /* Copied all the bits from the frag list -- free it. */
  1139. skb_frag_list_init(skb);
  1140. xenvif_skb_zerocopy_prepare(queue, nskb);
  1141. kfree_skb(nskb);
  1142. /* Release all the original (foreign) frags. */
  1143. for (f = 0; f < skb_shinfo(skb)->nr_frags; f++)
  1144. skb_frag_unref(skb, f);
  1145. uarg = skb_shinfo(skb)->destructor_arg;
  1146. /* increase inflight counter to offset decrement in callback */
  1147. atomic_inc(&queue->inflight_packets);
  1148. uarg->callback(uarg, true);
  1149. skb_shinfo(skb)->destructor_arg = NULL;
  1150. /* Fill the skb with the new (local) frags. */
  1151. memcpy(skb_shinfo(skb)->frags, frags, i * sizeof(skb_frag_t));
  1152. skb_shinfo(skb)->nr_frags = i;
  1153. skb->truesize += i * PAGE_SIZE;
  1154. return 0;
  1155. }
  1156. static int xenvif_tx_submit(struct xenvif_queue *queue)
  1157. {
  1158. struct gnttab_map_grant_ref *gop_map = queue->tx_map_ops;
  1159. struct gnttab_copy *gop_copy = queue->tx_copy_ops;
  1160. struct sk_buff *skb;
  1161. int work_done = 0;
  1162. while ((skb = __skb_dequeue(&queue->tx_queue)) != NULL) {
  1163. struct xen_netif_tx_request *txp;
  1164. u16 pending_idx;
  1165. unsigned data_len;
  1166. pending_idx = XENVIF_TX_CB(skb)->pending_idx;
  1167. txp = &queue->pending_tx_info[pending_idx].req;
  1168. /* Check the remap error code. */
  1169. if (unlikely(xenvif_tx_check_gop(queue, skb, &gop_map, &gop_copy))) {
  1170. /* If there was an error, xenvif_tx_check_gop is
  1171. * expected to release all the frags which were mapped,
  1172. * so kfree_skb shouldn't do it again
  1173. */
  1174. skb_shinfo(skb)->nr_frags = 0;
  1175. if (skb_has_frag_list(skb)) {
  1176. struct sk_buff *nskb =
  1177. skb_shinfo(skb)->frag_list;
  1178. skb_shinfo(nskb)->nr_frags = 0;
  1179. }
  1180. kfree_skb(skb);
  1181. continue;
  1182. }
  1183. data_len = skb->len;
  1184. callback_param(queue, pending_idx).ctx = NULL;
  1185. if (data_len < txp->size) {
  1186. /* Append the packet payload as a fragment. */
  1187. txp->offset += data_len;
  1188. txp->size -= data_len;
  1189. } else {
  1190. /* Schedule a response immediately. */
  1191. xenvif_idx_release(queue, pending_idx,
  1192. XEN_NETIF_RSP_OKAY);
  1193. }
  1194. if (txp->flags & XEN_NETTXF_csum_blank)
  1195. skb->ip_summed = CHECKSUM_PARTIAL;
  1196. else if (txp->flags & XEN_NETTXF_data_validated)
  1197. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1198. xenvif_fill_frags(queue, skb);
  1199. if (unlikely(skb_has_frag_list(skb))) {
  1200. if (xenvif_handle_frag_list(queue, skb)) {
  1201. if (net_ratelimit())
  1202. netdev_err(queue->vif->dev,
  1203. "Not enough memory to consolidate frag_list!\n");
  1204. xenvif_skb_zerocopy_prepare(queue, skb);
  1205. kfree_skb(skb);
  1206. continue;
  1207. }
  1208. }
  1209. skb->dev = queue->vif->dev;
  1210. skb->protocol = eth_type_trans(skb, skb->dev);
  1211. skb_reset_network_header(skb);
  1212. if (checksum_setup(queue, skb)) {
  1213. netdev_dbg(queue->vif->dev,
  1214. "Can't setup checksum in net_tx_action\n");
  1215. /* We have to set this flag to trigger the callback */
  1216. if (skb_shinfo(skb)->destructor_arg)
  1217. xenvif_skb_zerocopy_prepare(queue, skb);
  1218. kfree_skb(skb);
  1219. continue;
  1220. }
  1221. skb_probe_transport_header(skb, 0);
  1222. /* If the packet is GSO then we will have just set up the
  1223. * transport header offset in checksum_setup so it's now
  1224. * straightforward to calculate gso_segs.
  1225. */
  1226. if (skb_is_gso(skb)) {
  1227. int mss = skb_shinfo(skb)->gso_size;
  1228. int hdrlen = skb_transport_header(skb) -
  1229. skb_mac_header(skb) +
  1230. tcp_hdrlen(skb);
  1231. skb_shinfo(skb)->gso_segs =
  1232. DIV_ROUND_UP(skb->len - hdrlen, mss);
  1233. }
  1234. queue->stats.rx_bytes += skb->len;
  1235. queue->stats.rx_packets++;
  1236. work_done++;
  1237. /* Set this flag right before netif_receive_skb, otherwise
  1238. * someone might think this packet already left netback, and
  1239. * do a skb_copy_ubufs while we are still in control of the
  1240. * skb. E.g. the __pskb_pull_tail earlier can do such thing.
  1241. */
  1242. if (skb_shinfo(skb)->destructor_arg) {
  1243. xenvif_skb_zerocopy_prepare(queue, skb);
  1244. queue->stats.tx_zerocopy_sent++;
  1245. }
  1246. netif_receive_skb(skb);
  1247. }
  1248. return work_done;
  1249. }
  1250. void xenvif_zerocopy_callback(struct ubuf_info *ubuf, bool zerocopy_success)
  1251. {
  1252. unsigned long flags;
  1253. pending_ring_idx_t index;
  1254. struct xenvif_queue *queue = ubuf_to_queue(ubuf);
  1255. /* This is the only place where we grab this lock, to protect callbacks
  1256. * from each other.
  1257. */
  1258. spin_lock_irqsave(&queue->callback_lock, flags);
  1259. do {
  1260. u16 pending_idx = ubuf->desc;
  1261. ubuf = (struct ubuf_info *) ubuf->ctx;
  1262. BUG_ON(queue->dealloc_prod - queue->dealloc_cons >=
  1263. MAX_PENDING_REQS);
  1264. index = pending_index(queue->dealloc_prod);
  1265. queue->dealloc_ring[index] = pending_idx;
  1266. /* Sync with xenvif_tx_dealloc_action:
  1267. * insert idx then incr producer.
  1268. */
  1269. smp_wmb();
  1270. queue->dealloc_prod++;
  1271. } while (ubuf);
  1272. wake_up(&queue->dealloc_wq);
  1273. spin_unlock_irqrestore(&queue->callback_lock, flags);
  1274. if (likely(zerocopy_success))
  1275. queue->stats.tx_zerocopy_success++;
  1276. else
  1277. queue->stats.tx_zerocopy_fail++;
  1278. xenvif_skb_zerocopy_complete(queue);
  1279. }
  1280. static inline void xenvif_tx_dealloc_action(struct xenvif_queue *queue)
  1281. {
  1282. struct gnttab_unmap_grant_ref *gop;
  1283. pending_ring_idx_t dc, dp;
  1284. u16 pending_idx, pending_idx_release[MAX_PENDING_REQS];
  1285. unsigned int i = 0;
  1286. dc = queue->dealloc_cons;
  1287. gop = queue->tx_unmap_ops;
  1288. /* Free up any grants we have finished using */
  1289. do {
  1290. dp = queue->dealloc_prod;
  1291. /* Ensure we see all indices enqueued by all
  1292. * xenvif_zerocopy_callback().
  1293. */
  1294. smp_rmb();
  1295. while (dc != dp) {
  1296. BUG_ON(gop - queue->tx_unmap_ops >= MAX_PENDING_REQS);
  1297. pending_idx =
  1298. queue->dealloc_ring[pending_index(dc++)];
  1299. pending_idx_release[gop - queue->tx_unmap_ops] =
  1300. pending_idx;
  1301. queue->pages_to_unmap[gop - queue->tx_unmap_ops] =
  1302. queue->mmap_pages[pending_idx];
  1303. gnttab_set_unmap_op(gop,
  1304. idx_to_kaddr(queue, pending_idx),
  1305. GNTMAP_host_map,
  1306. queue->grant_tx_handle[pending_idx]);
  1307. xenvif_grant_handle_reset(queue, pending_idx);
  1308. ++gop;
  1309. }
  1310. } while (dp != queue->dealloc_prod);
  1311. queue->dealloc_cons = dc;
  1312. if (gop - queue->tx_unmap_ops > 0) {
  1313. int ret;
  1314. ret = gnttab_unmap_refs(queue->tx_unmap_ops,
  1315. NULL,
  1316. queue->pages_to_unmap,
  1317. gop - queue->tx_unmap_ops);
  1318. if (ret) {
  1319. netdev_err(queue->vif->dev, "Unmap fail: nr_ops %tu ret %d\n",
  1320. gop - queue->tx_unmap_ops, ret);
  1321. for (i = 0; i < gop - queue->tx_unmap_ops; ++i) {
  1322. if (gop[i].status != GNTST_okay)
  1323. netdev_err(queue->vif->dev,
  1324. " host_addr: 0x%llx handle: 0x%x status: %d\n",
  1325. gop[i].host_addr,
  1326. gop[i].handle,
  1327. gop[i].status);
  1328. }
  1329. BUG();
  1330. }
  1331. }
  1332. for (i = 0; i < gop - queue->tx_unmap_ops; ++i)
  1333. xenvif_idx_release(queue, pending_idx_release[i],
  1334. XEN_NETIF_RSP_OKAY);
  1335. }
  1336. /* Called after netfront has transmitted */
  1337. int xenvif_tx_action(struct xenvif_queue *queue, int budget)
  1338. {
  1339. unsigned nr_mops, nr_cops = 0;
  1340. int work_done, ret;
  1341. if (unlikely(!tx_work_todo(queue)))
  1342. return 0;
  1343. xenvif_tx_build_gops(queue, budget, &nr_cops, &nr_mops);
  1344. if (nr_cops == 0)
  1345. return 0;
  1346. gnttab_batch_copy(queue->tx_copy_ops, nr_cops);
  1347. if (nr_mops != 0) {
  1348. ret = gnttab_map_refs(queue->tx_map_ops,
  1349. NULL,
  1350. queue->pages_to_map,
  1351. nr_mops);
  1352. BUG_ON(ret);
  1353. }
  1354. work_done = xenvif_tx_submit(queue);
  1355. return work_done;
  1356. }
  1357. static void xenvif_idx_release(struct xenvif_queue *queue, u16 pending_idx,
  1358. u8 status)
  1359. {
  1360. struct pending_tx_info *pending_tx_info;
  1361. pending_ring_idx_t index;
  1362. unsigned long flags;
  1363. pending_tx_info = &queue->pending_tx_info[pending_idx];
  1364. spin_lock_irqsave(&queue->response_lock, flags);
  1365. make_tx_response(queue, &pending_tx_info->req, status);
  1366. /* Release the pending index before pusing the Tx response so
  1367. * its available before a new Tx request is pushed by the
  1368. * frontend.
  1369. */
  1370. index = pending_index(queue->pending_prod++);
  1371. queue->pending_ring[index] = pending_idx;
  1372. push_tx_responses(queue);
  1373. spin_unlock_irqrestore(&queue->response_lock, flags);
  1374. }
  1375. static void make_tx_response(struct xenvif_queue *queue,
  1376. struct xen_netif_tx_request *txp,
  1377. s8 st)
  1378. {
  1379. RING_IDX i = queue->tx.rsp_prod_pvt;
  1380. struct xen_netif_tx_response *resp;
  1381. resp = RING_GET_RESPONSE(&queue->tx, i);
  1382. resp->id = txp->id;
  1383. resp->status = st;
  1384. if (txp->flags & XEN_NETTXF_extra_info)
  1385. RING_GET_RESPONSE(&queue->tx, ++i)->status = XEN_NETIF_RSP_NULL;
  1386. queue->tx.rsp_prod_pvt = ++i;
  1387. }
  1388. static void push_tx_responses(struct xenvif_queue *queue)
  1389. {
  1390. int notify;
  1391. RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&queue->tx, notify);
  1392. if (notify)
  1393. notify_remote_via_irq(queue->tx_irq);
  1394. }
  1395. static struct xen_netif_rx_response *make_rx_response(struct xenvif_queue *queue,
  1396. u16 id,
  1397. s8 st,
  1398. u16 offset,
  1399. u16 size,
  1400. u16 flags)
  1401. {
  1402. RING_IDX i = queue->rx.rsp_prod_pvt;
  1403. struct xen_netif_rx_response *resp;
  1404. resp = RING_GET_RESPONSE(&queue->rx, i);
  1405. resp->offset = offset;
  1406. resp->flags = flags;
  1407. resp->id = id;
  1408. resp->status = (s16)size;
  1409. if (st < 0)
  1410. resp->status = (s16)st;
  1411. queue->rx.rsp_prod_pvt = ++i;
  1412. return resp;
  1413. }
  1414. void xenvif_idx_unmap(struct xenvif_queue *queue, u16 pending_idx)
  1415. {
  1416. int ret;
  1417. struct gnttab_unmap_grant_ref tx_unmap_op;
  1418. gnttab_set_unmap_op(&tx_unmap_op,
  1419. idx_to_kaddr(queue, pending_idx),
  1420. GNTMAP_host_map,
  1421. queue->grant_tx_handle[pending_idx]);
  1422. xenvif_grant_handle_reset(queue, pending_idx);
  1423. ret = gnttab_unmap_refs(&tx_unmap_op, NULL,
  1424. &queue->mmap_pages[pending_idx], 1);
  1425. if (ret) {
  1426. netdev_err(queue->vif->dev,
  1427. "Unmap fail: ret: %d pending_idx: %d host_addr: %llx handle: 0x%x status: %d\n",
  1428. ret,
  1429. pending_idx,
  1430. tx_unmap_op.host_addr,
  1431. tx_unmap_op.handle,
  1432. tx_unmap_op.status);
  1433. BUG();
  1434. }
  1435. }
  1436. static inline int tx_work_todo(struct xenvif_queue *queue)
  1437. {
  1438. if (likely(RING_HAS_UNCONSUMED_REQUESTS(&queue->tx)))
  1439. return 1;
  1440. return 0;
  1441. }
  1442. static inline bool tx_dealloc_work_todo(struct xenvif_queue *queue)
  1443. {
  1444. return queue->dealloc_cons != queue->dealloc_prod;
  1445. }
  1446. void xenvif_unmap_frontend_rings(struct xenvif_queue *queue)
  1447. {
  1448. if (queue->tx.sring)
  1449. xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(queue->vif),
  1450. queue->tx.sring);
  1451. if (queue->rx.sring)
  1452. xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(queue->vif),
  1453. queue->rx.sring);
  1454. }
  1455. int xenvif_map_frontend_rings(struct xenvif_queue *queue,
  1456. grant_ref_t tx_ring_ref,
  1457. grant_ref_t rx_ring_ref)
  1458. {
  1459. void *addr;
  1460. struct xen_netif_tx_sring *txs;
  1461. struct xen_netif_rx_sring *rxs;
  1462. int err = -ENOMEM;
  1463. err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(queue->vif),
  1464. &tx_ring_ref, 1, &addr);
  1465. if (err)
  1466. goto err;
  1467. txs = (struct xen_netif_tx_sring *)addr;
  1468. BACK_RING_INIT(&queue->tx, txs, PAGE_SIZE);
  1469. err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(queue->vif),
  1470. &rx_ring_ref, 1, &addr);
  1471. if (err)
  1472. goto err;
  1473. rxs = (struct xen_netif_rx_sring *)addr;
  1474. BACK_RING_INIT(&queue->rx, rxs, PAGE_SIZE);
  1475. return 0;
  1476. err:
  1477. xenvif_unmap_frontend_rings(queue);
  1478. return err;
  1479. }
  1480. static void xenvif_queue_carrier_off(struct xenvif_queue *queue)
  1481. {
  1482. struct xenvif *vif = queue->vif;
  1483. queue->stalled = true;
  1484. /* At least one queue has stalled? Disable the carrier. */
  1485. spin_lock(&vif->lock);
  1486. if (vif->stalled_queues++ == 0) {
  1487. netdev_info(vif->dev, "Guest Rx stalled");
  1488. netif_carrier_off(vif->dev);
  1489. }
  1490. spin_unlock(&vif->lock);
  1491. }
  1492. static void xenvif_queue_carrier_on(struct xenvif_queue *queue)
  1493. {
  1494. struct xenvif *vif = queue->vif;
  1495. queue->last_rx_time = jiffies; /* Reset Rx stall detection. */
  1496. queue->stalled = false;
  1497. /* All queues are ready? Enable the carrier. */
  1498. spin_lock(&vif->lock);
  1499. if (--vif->stalled_queues == 0) {
  1500. netdev_info(vif->dev, "Guest Rx ready");
  1501. netif_carrier_on(vif->dev);
  1502. }
  1503. spin_unlock(&vif->lock);
  1504. }
  1505. static bool xenvif_rx_queue_stalled(struct xenvif_queue *queue)
  1506. {
  1507. RING_IDX prod, cons;
  1508. prod = queue->rx.sring->req_prod;
  1509. cons = queue->rx.req_cons;
  1510. return !queue->stalled
  1511. && prod - cons < XEN_NETBK_RX_SLOTS_MAX
  1512. && time_after(jiffies,
  1513. queue->last_rx_time + queue->vif->stall_timeout);
  1514. }
  1515. static bool xenvif_rx_queue_ready(struct xenvif_queue *queue)
  1516. {
  1517. RING_IDX prod, cons;
  1518. prod = queue->rx.sring->req_prod;
  1519. cons = queue->rx.req_cons;
  1520. return queue->stalled
  1521. && prod - cons >= XEN_NETBK_RX_SLOTS_MAX;
  1522. }
  1523. static bool xenvif_have_rx_work(struct xenvif_queue *queue)
  1524. {
  1525. return (!skb_queue_empty(&queue->rx_queue)
  1526. && xenvif_rx_ring_slots_available(queue, XEN_NETBK_RX_SLOTS_MAX))
  1527. || (queue->vif->stall_timeout &&
  1528. (xenvif_rx_queue_stalled(queue)
  1529. || xenvif_rx_queue_ready(queue)))
  1530. || kthread_should_stop()
  1531. || queue->vif->disabled;
  1532. }
  1533. static long xenvif_rx_queue_timeout(struct xenvif_queue *queue)
  1534. {
  1535. struct sk_buff *skb;
  1536. long timeout;
  1537. skb = skb_peek(&queue->rx_queue);
  1538. if (!skb)
  1539. return MAX_SCHEDULE_TIMEOUT;
  1540. timeout = XENVIF_RX_CB(skb)->expires - jiffies;
  1541. return timeout < 0 ? 0 : timeout;
  1542. }
  1543. /* Wait until the guest Rx thread has work.
  1544. *
  1545. * The timeout needs to be adjusted based on the current head of the
  1546. * queue (and not just the head at the beginning). In particular, if
  1547. * the queue is initially empty an infinite timeout is used and this
  1548. * needs to be reduced when a skb is queued.
  1549. *
  1550. * This cannot be done with wait_event_timeout() because it only
  1551. * calculates the timeout once.
  1552. */
  1553. static void xenvif_wait_for_rx_work(struct xenvif_queue *queue)
  1554. {
  1555. DEFINE_WAIT(wait);
  1556. if (xenvif_have_rx_work(queue))
  1557. return;
  1558. for (;;) {
  1559. long ret;
  1560. prepare_to_wait(&queue->wq, &wait, TASK_INTERRUPTIBLE);
  1561. if (xenvif_have_rx_work(queue))
  1562. break;
  1563. ret = schedule_timeout(xenvif_rx_queue_timeout(queue));
  1564. if (!ret)
  1565. break;
  1566. }
  1567. finish_wait(&queue->wq, &wait);
  1568. }
  1569. int xenvif_kthread_guest_rx(void *data)
  1570. {
  1571. struct xenvif_queue *queue = data;
  1572. struct xenvif *vif = queue->vif;
  1573. if (!vif->stall_timeout)
  1574. xenvif_queue_carrier_on(queue);
  1575. for (;;) {
  1576. xenvif_wait_for_rx_work(queue);
  1577. if (kthread_should_stop())
  1578. break;
  1579. /* This frontend is found to be rogue, disable it in
  1580. * kthread context. Currently this is only set when
  1581. * netback finds out frontend sends malformed packet,
  1582. * but we cannot disable the interface in softirq
  1583. * context so we defer it here, if this thread is
  1584. * associated with queue 0.
  1585. */
  1586. if (unlikely(vif->disabled && queue->id == 0)) {
  1587. xenvif_carrier_off(vif);
  1588. break;
  1589. }
  1590. if (!skb_queue_empty(&queue->rx_queue))
  1591. xenvif_rx_action(queue);
  1592. /* If the guest hasn't provided any Rx slots for a
  1593. * while it's probably not responsive, drop the
  1594. * carrier so packets are dropped earlier.
  1595. */
  1596. if (vif->stall_timeout) {
  1597. if (xenvif_rx_queue_stalled(queue))
  1598. xenvif_queue_carrier_off(queue);
  1599. else if (xenvif_rx_queue_ready(queue))
  1600. xenvif_queue_carrier_on(queue);
  1601. }
  1602. /* Queued packets may have foreign pages from other
  1603. * domains. These cannot be queued indefinitely as
  1604. * this would starve guests of grant refs and transmit
  1605. * slots.
  1606. */
  1607. xenvif_rx_queue_drop_expired(queue);
  1608. xenvif_rx_queue_maybe_wake(queue);
  1609. cond_resched();
  1610. }
  1611. /* Bin any remaining skbs */
  1612. xenvif_rx_queue_purge(queue);
  1613. return 0;
  1614. }
  1615. static bool xenvif_dealloc_kthread_should_stop(struct xenvif_queue *queue)
  1616. {
  1617. /* Dealloc thread must remain running until all inflight
  1618. * packets complete.
  1619. */
  1620. return kthread_should_stop() &&
  1621. !atomic_read(&queue->inflight_packets);
  1622. }
  1623. int xenvif_dealloc_kthread(void *data)
  1624. {
  1625. struct xenvif_queue *queue = data;
  1626. for (;;) {
  1627. wait_event_interruptible(queue->dealloc_wq,
  1628. tx_dealloc_work_todo(queue) ||
  1629. xenvif_dealloc_kthread_should_stop(queue));
  1630. if (xenvif_dealloc_kthread_should_stop(queue))
  1631. break;
  1632. xenvif_tx_dealloc_action(queue);
  1633. cond_resched();
  1634. }
  1635. /* Unmap anything remaining*/
  1636. if (tx_dealloc_work_todo(queue))
  1637. xenvif_tx_dealloc_action(queue);
  1638. return 0;
  1639. }
  1640. static int __init netback_init(void)
  1641. {
  1642. int rc = 0;
  1643. if (!xen_domain())
  1644. return -ENODEV;
  1645. /* Allow as many queues as there are CPUs, by default */
  1646. xenvif_max_queues = num_online_cpus();
  1647. if (fatal_skb_slots < XEN_NETBK_LEGACY_SLOTS_MAX) {
  1648. pr_info("fatal_skb_slots too small (%d), bump it to XEN_NETBK_LEGACY_SLOTS_MAX (%d)\n",
  1649. fatal_skb_slots, XEN_NETBK_LEGACY_SLOTS_MAX);
  1650. fatal_skb_slots = XEN_NETBK_LEGACY_SLOTS_MAX;
  1651. }
  1652. rc = xenvif_xenbus_init();
  1653. if (rc)
  1654. goto failed_init;
  1655. #ifdef CONFIG_DEBUG_FS
  1656. xen_netback_dbg_root = debugfs_create_dir("xen-netback", NULL);
  1657. if (IS_ERR_OR_NULL(xen_netback_dbg_root))
  1658. pr_warn("Init of debugfs returned %ld!\n",
  1659. PTR_ERR(xen_netback_dbg_root));
  1660. #endif /* CONFIG_DEBUG_FS */
  1661. return 0;
  1662. failed_init:
  1663. return rc;
  1664. }
  1665. module_init(netback_init);
  1666. static void __exit netback_fini(void)
  1667. {
  1668. #ifdef CONFIG_DEBUG_FS
  1669. if (!IS_ERR_OR_NULL(xen_netback_dbg_root))
  1670. debugfs_remove_recursive(xen_netback_dbg_root);
  1671. #endif /* CONFIG_DEBUG_FS */
  1672. xenvif_xenbus_fini();
  1673. }
  1674. module_exit(netback_fini);
  1675. MODULE_LICENSE("Dual BSD/GPL");
  1676. MODULE_ALIAS("xen-backend:vif");