netback.c 59 KB

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