netback.c 59 KB

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