netback.c 54 KB

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