netback.c 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005
  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. struct skb_shared_info *shinfo = skb_shinfo(skb);
  861. int nr_frags = shinfo->nr_frags;
  862. int i, err;
  863. struct sk_buff *first_skb = NULL;
  864. /* Check status of header. */
  865. err = (*gopp_copy)->status;
  866. (*gopp_copy)++;
  867. if (unlikely(err)) {
  868. if (net_ratelimit())
  869. netdev_dbg(queue->vif->dev,
  870. "Grant copy of header failed! status: %d pending_idx: %u ref: %u\n",
  871. (*gopp_copy)->status,
  872. pending_idx,
  873. (*gopp_copy)->source.u.ref);
  874. xenvif_idx_release(queue, pending_idx, XEN_NETIF_RSP_ERROR);
  875. }
  876. check_frags:
  877. for (i = 0; i < nr_frags; i++, gop_map++) {
  878. int j, newerr;
  879. pending_idx = frag_get_pending_idx(&shinfo->frags[i]);
  880. /* Check error status: if okay then remember grant handle. */
  881. newerr = gop_map->status;
  882. if (likely(!newerr)) {
  883. xenvif_grant_handle_set(queue,
  884. pending_idx,
  885. gop_map->handle);
  886. /* Had a previous error? Invalidate this fragment. */
  887. if (unlikely(err))
  888. xenvif_idx_unmap(queue, pending_idx);
  889. continue;
  890. }
  891. /* Error on this fragment: respond to client with an error. */
  892. if (net_ratelimit())
  893. netdev_dbg(queue->vif->dev,
  894. "Grant map of %d. frag failed! status: %d pending_idx: %u ref: %u\n",
  895. i,
  896. gop_map->status,
  897. pending_idx,
  898. gop_map->ref);
  899. xenvif_idx_release(queue, pending_idx, XEN_NETIF_RSP_ERROR);
  900. /* Not the first error? Preceding frags already invalidated. */
  901. if (err)
  902. continue;
  903. /* First error: invalidate preceding fragments. */
  904. for (j = 0; j < i; j++) {
  905. pending_idx = frag_get_pending_idx(&shinfo->frags[j]);
  906. xenvif_idx_unmap(queue, pending_idx);
  907. }
  908. /* Remember the error: invalidate all subsequent fragments. */
  909. err = newerr;
  910. }
  911. if (skb_has_frag_list(skb)) {
  912. first_skb = skb;
  913. skb = shinfo->frag_list;
  914. shinfo = skb_shinfo(skb);
  915. nr_frags = shinfo->nr_frags;
  916. goto check_frags;
  917. }
  918. /* There was a mapping error in the frag_list skb. We have to unmap
  919. * the first skb's frags
  920. */
  921. if (first_skb && err) {
  922. int j;
  923. shinfo = skb_shinfo(first_skb);
  924. for (j = 0; j < shinfo->nr_frags; j++) {
  925. pending_idx = frag_get_pending_idx(&shinfo->frags[j]);
  926. xenvif_idx_unmap(queue, pending_idx);
  927. }
  928. }
  929. *gopp_map = gop_map;
  930. return err;
  931. }
  932. static void xenvif_fill_frags(struct xenvif_queue *queue, struct sk_buff *skb)
  933. {
  934. struct skb_shared_info *shinfo = skb_shinfo(skb);
  935. int nr_frags = shinfo->nr_frags;
  936. int i;
  937. u16 prev_pending_idx = INVALID_PENDING_IDX;
  938. for (i = 0; i < nr_frags; i++) {
  939. skb_frag_t *frag = shinfo->frags + i;
  940. struct xen_netif_tx_request *txp;
  941. struct page *page;
  942. u16 pending_idx;
  943. pending_idx = frag_get_pending_idx(frag);
  944. /* If this is not the first frag, chain it to the previous*/
  945. if (prev_pending_idx == INVALID_PENDING_IDX)
  946. skb_shinfo(skb)->destructor_arg =
  947. &callback_param(queue, pending_idx);
  948. else
  949. callback_param(queue, prev_pending_idx).ctx =
  950. &callback_param(queue, pending_idx);
  951. callback_param(queue, pending_idx).ctx = NULL;
  952. prev_pending_idx = pending_idx;
  953. txp = &queue->pending_tx_info[pending_idx].req;
  954. page = virt_to_page(idx_to_kaddr(queue, pending_idx));
  955. __skb_fill_page_desc(skb, i, page, txp->offset, txp->size);
  956. skb->len += txp->size;
  957. skb->data_len += txp->size;
  958. skb->truesize += txp->size;
  959. /* Take an extra reference to offset network stack's put_page */
  960. get_page(queue->mmap_pages[pending_idx]);
  961. }
  962. /* FIXME: __skb_fill_page_desc set this to true because page->pfmemalloc
  963. * overlaps with "index", and "mapping" is not set. I think mapping
  964. * should be set. If delivered to local stack, it would drop this
  965. * skb in sk_filter unless the socket has the right to use it.
  966. */
  967. skb->pfmemalloc = false;
  968. }
  969. static int xenvif_get_extras(struct xenvif_queue *queue,
  970. struct xen_netif_extra_info *extras,
  971. int work_to_do)
  972. {
  973. struct xen_netif_extra_info extra;
  974. RING_IDX cons = queue->tx.req_cons;
  975. do {
  976. if (unlikely(work_to_do-- <= 0)) {
  977. netdev_err(queue->vif->dev, "Missing extra info\n");
  978. xenvif_fatal_tx_err(queue->vif);
  979. return -EBADR;
  980. }
  981. memcpy(&extra, RING_GET_REQUEST(&queue->tx, cons),
  982. sizeof(extra));
  983. if (unlikely(!extra.type ||
  984. extra.type >= XEN_NETIF_EXTRA_TYPE_MAX)) {
  985. queue->tx.req_cons = ++cons;
  986. netdev_err(queue->vif->dev,
  987. "Invalid extra type: %d\n", extra.type);
  988. xenvif_fatal_tx_err(queue->vif);
  989. return -EINVAL;
  990. }
  991. memcpy(&extras[extra.type - 1], &extra, sizeof(extra));
  992. queue->tx.req_cons = ++cons;
  993. } while (extra.flags & XEN_NETIF_EXTRA_FLAG_MORE);
  994. return work_to_do;
  995. }
  996. static int xenvif_set_skb_gso(struct xenvif *vif,
  997. struct sk_buff *skb,
  998. struct xen_netif_extra_info *gso)
  999. {
  1000. if (!gso->u.gso.size) {
  1001. netdev_err(vif->dev, "GSO size must not be zero.\n");
  1002. xenvif_fatal_tx_err(vif);
  1003. return -EINVAL;
  1004. }
  1005. switch (gso->u.gso.type) {
  1006. case XEN_NETIF_GSO_TYPE_TCPV4:
  1007. skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
  1008. break;
  1009. case XEN_NETIF_GSO_TYPE_TCPV6:
  1010. skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
  1011. break;
  1012. default:
  1013. netdev_err(vif->dev, "Bad GSO type %d.\n", gso->u.gso.type);
  1014. xenvif_fatal_tx_err(vif);
  1015. return -EINVAL;
  1016. }
  1017. skb_shinfo(skb)->gso_size = gso->u.gso.size;
  1018. /* gso_segs will be calculated later */
  1019. return 0;
  1020. }
  1021. static int checksum_setup(struct xenvif_queue *queue, struct sk_buff *skb)
  1022. {
  1023. bool recalculate_partial_csum = false;
  1024. /* A GSO SKB must be CHECKSUM_PARTIAL. However some buggy
  1025. * peers can fail to set NETRXF_csum_blank when sending a GSO
  1026. * frame. In this case force the SKB to CHECKSUM_PARTIAL and
  1027. * recalculate the partial checksum.
  1028. */
  1029. if (skb->ip_summed != CHECKSUM_PARTIAL && skb_is_gso(skb)) {
  1030. queue->stats.rx_gso_checksum_fixup++;
  1031. skb->ip_summed = CHECKSUM_PARTIAL;
  1032. recalculate_partial_csum = true;
  1033. }
  1034. /* A non-CHECKSUM_PARTIAL SKB does not require setup. */
  1035. if (skb->ip_summed != CHECKSUM_PARTIAL)
  1036. return 0;
  1037. return skb_checksum_setup(skb, recalculate_partial_csum);
  1038. }
  1039. static bool tx_credit_exceeded(struct xenvif_queue *queue, unsigned size)
  1040. {
  1041. u64 now = get_jiffies_64();
  1042. u64 next_credit = queue->credit_window_start +
  1043. msecs_to_jiffies(queue->credit_usec / 1000);
  1044. /* Timer could already be pending in rare cases. */
  1045. if (timer_pending(&queue->credit_timeout))
  1046. return true;
  1047. /* Passed the point where we can replenish credit? */
  1048. if (time_after_eq64(now, next_credit)) {
  1049. queue->credit_window_start = now;
  1050. tx_add_credit(queue);
  1051. }
  1052. /* Still too big to send right now? Set a callback. */
  1053. if (size > queue->remaining_credit) {
  1054. queue->credit_timeout.data =
  1055. (unsigned long)queue;
  1056. queue->credit_timeout.function =
  1057. tx_credit_callback;
  1058. mod_timer(&queue->credit_timeout,
  1059. next_credit);
  1060. queue->credit_window_start = next_credit;
  1061. return true;
  1062. }
  1063. return false;
  1064. }
  1065. static void xenvif_tx_build_gops(struct xenvif_queue *queue,
  1066. int budget,
  1067. unsigned *copy_ops,
  1068. unsigned *map_ops)
  1069. {
  1070. struct gnttab_map_grant_ref *gop = queue->tx_map_ops, *request_gop;
  1071. struct sk_buff *skb;
  1072. int ret;
  1073. while (skb_queue_len(&queue->tx_queue) < budget) {
  1074. struct xen_netif_tx_request txreq;
  1075. struct xen_netif_tx_request txfrags[XEN_NETBK_LEGACY_SLOTS_MAX];
  1076. struct xen_netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX-1];
  1077. u16 pending_idx;
  1078. RING_IDX idx;
  1079. int work_to_do;
  1080. unsigned int data_len;
  1081. pending_ring_idx_t index;
  1082. if (queue->tx.sring->req_prod - queue->tx.req_cons >
  1083. XEN_NETIF_TX_RING_SIZE) {
  1084. netdev_err(queue->vif->dev,
  1085. "Impossible number of requests. "
  1086. "req_prod %d, req_cons %d, size %ld\n",
  1087. queue->tx.sring->req_prod, queue->tx.req_cons,
  1088. XEN_NETIF_TX_RING_SIZE);
  1089. xenvif_fatal_tx_err(queue->vif);
  1090. break;
  1091. }
  1092. work_to_do = RING_HAS_UNCONSUMED_REQUESTS(&queue->tx);
  1093. if (!work_to_do)
  1094. break;
  1095. idx = queue->tx.req_cons;
  1096. rmb(); /* Ensure that we see the request before we copy it. */
  1097. memcpy(&txreq, RING_GET_REQUEST(&queue->tx, idx), sizeof(txreq));
  1098. /* Credit-based scheduling. */
  1099. if (txreq.size > queue->remaining_credit &&
  1100. tx_credit_exceeded(queue, txreq.size))
  1101. break;
  1102. queue->remaining_credit -= txreq.size;
  1103. work_to_do--;
  1104. queue->tx.req_cons = ++idx;
  1105. memset(extras, 0, sizeof(extras));
  1106. if (txreq.flags & XEN_NETTXF_extra_info) {
  1107. work_to_do = xenvif_get_extras(queue, extras,
  1108. work_to_do);
  1109. idx = queue->tx.req_cons;
  1110. if (unlikely(work_to_do < 0))
  1111. break;
  1112. }
  1113. ret = xenvif_count_requests(queue, &txreq, txfrags, work_to_do);
  1114. if (unlikely(ret < 0))
  1115. break;
  1116. idx += ret;
  1117. if (unlikely(txreq.size < ETH_HLEN)) {
  1118. netdev_dbg(queue->vif->dev,
  1119. "Bad packet size: %d\n", txreq.size);
  1120. xenvif_tx_err(queue, &txreq, idx);
  1121. break;
  1122. }
  1123. /* No crossing a page as the payload mustn't fragment. */
  1124. if (unlikely((txreq.offset + txreq.size) > PAGE_SIZE)) {
  1125. netdev_err(queue->vif->dev,
  1126. "txreq.offset: %x, size: %u, end: %lu\n",
  1127. txreq.offset, txreq.size,
  1128. (txreq.offset&~PAGE_MASK) + txreq.size);
  1129. xenvif_fatal_tx_err(queue->vif);
  1130. break;
  1131. }
  1132. index = pending_index(queue->pending_cons);
  1133. pending_idx = queue->pending_ring[index];
  1134. data_len = (txreq.size > PKT_PROT_LEN &&
  1135. ret < XEN_NETBK_LEGACY_SLOTS_MAX) ?
  1136. PKT_PROT_LEN : txreq.size;
  1137. skb = xenvif_alloc_skb(data_len);
  1138. if (unlikely(skb == NULL)) {
  1139. netdev_dbg(queue->vif->dev,
  1140. "Can't allocate a skb in start_xmit.\n");
  1141. xenvif_tx_err(queue, &txreq, idx);
  1142. break;
  1143. }
  1144. if (extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].type) {
  1145. struct xen_netif_extra_info *gso;
  1146. gso = &extras[XEN_NETIF_EXTRA_TYPE_GSO - 1];
  1147. if (xenvif_set_skb_gso(queue->vif, skb, gso)) {
  1148. /* Failure in xenvif_set_skb_gso is fatal. */
  1149. kfree_skb(skb);
  1150. break;
  1151. }
  1152. }
  1153. XENVIF_TX_CB(skb)->pending_idx = pending_idx;
  1154. __skb_put(skb, data_len);
  1155. queue->tx_copy_ops[*copy_ops].source.u.ref = txreq.gref;
  1156. queue->tx_copy_ops[*copy_ops].source.domid = queue->vif->domid;
  1157. queue->tx_copy_ops[*copy_ops].source.offset = txreq.offset;
  1158. queue->tx_copy_ops[*copy_ops].dest.u.gmfn =
  1159. virt_to_mfn(skb->data);
  1160. queue->tx_copy_ops[*copy_ops].dest.domid = DOMID_SELF;
  1161. queue->tx_copy_ops[*copy_ops].dest.offset =
  1162. offset_in_page(skb->data);
  1163. queue->tx_copy_ops[*copy_ops].len = data_len;
  1164. queue->tx_copy_ops[*copy_ops].flags = GNTCOPY_source_gref;
  1165. (*copy_ops)++;
  1166. skb_shinfo(skb)->nr_frags = ret;
  1167. if (data_len < txreq.size) {
  1168. skb_shinfo(skb)->nr_frags++;
  1169. frag_set_pending_idx(&skb_shinfo(skb)->frags[0],
  1170. pending_idx);
  1171. xenvif_tx_create_map_op(queue, pending_idx, &txreq, gop);
  1172. gop++;
  1173. } else {
  1174. frag_set_pending_idx(&skb_shinfo(skb)->frags[0],
  1175. INVALID_PENDING_IDX);
  1176. memcpy(&queue->pending_tx_info[pending_idx].req, &txreq,
  1177. sizeof(txreq));
  1178. }
  1179. queue->pending_cons++;
  1180. request_gop = xenvif_get_requests(queue, skb, txfrags, gop);
  1181. if (request_gop == NULL) {
  1182. kfree_skb(skb);
  1183. xenvif_tx_err(queue, &txreq, idx);
  1184. break;
  1185. }
  1186. gop = request_gop;
  1187. __skb_queue_tail(&queue->tx_queue, skb);
  1188. queue->tx.req_cons = idx;
  1189. if (((gop-queue->tx_map_ops) >= ARRAY_SIZE(queue->tx_map_ops)) ||
  1190. (*copy_ops >= ARRAY_SIZE(queue->tx_copy_ops)))
  1191. break;
  1192. }
  1193. (*map_ops) = gop - queue->tx_map_ops;
  1194. return;
  1195. }
  1196. /* Consolidate skb with a frag_list into a brand new one with local pages on
  1197. * frags. Returns 0 or -ENOMEM if can't allocate new pages.
  1198. */
  1199. static int xenvif_handle_frag_list(struct xenvif_queue *queue, struct sk_buff *skb)
  1200. {
  1201. unsigned int offset = skb_headlen(skb);
  1202. skb_frag_t frags[MAX_SKB_FRAGS];
  1203. int i;
  1204. struct ubuf_info *uarg;
  1205. struct sk_buff *nskb = skb_shinfo(skb)->frag_list;
  1206. queue->stats.tx_zerocopy_sent += 2;
  1207. queue->stats.tx_frag_overflow++;
  1208. xenvif_fill_frags(queue, nskb);
  1209. /* Subtract frags size, we will correct it later */
  1210. skb->truesize -= skb->data_len;
  1211. skb->len += nskb->len;
  1212. skb->data_len += nskb->len;
  1213. /* create a brand new frags array and coalesce there */
  1214. for (i = 0; offset < skb->len; i++) {
  1215. struct page *page;
  1216. unsigned int len;
  1217. BUG_ON(i >= MAX_SKB_FRAGS);
  1218. page = alloc_page(GFP_ATOMIC|__GFP_COLD);
  1219. if (!page) {
  1220. int j;
  1221. skb->truesize += skb->data_len;
  1222. for (j = 0; j < i; j++)
  1223. put_page(frags[j].page.p);
  1224. return -ENOMEM;
  1225. }
  1226. if (offset + PAGE_SIZE < skb->len)
  1227. len = PAGE_SIZE;
  1228. else
  1229. len = skb->len - offset;
  1230. if (skb_copy_bits(skb, offset, page_address(page), len))
  1231. BUG();
  1232. offset += len;
  1233. frags[i].page.p = page;
  1234. frags[i].page_offset = 0;
  1235. skb_frag_size_set(&frags[i], len);
  1236. }
  1237. /* swap out with old one */
  1238. memcpy(skb_shinfo(skb)->frags,
  1239. frags,
  1240. i * sizeof(skb_frag_t));
  1241. skb_shinfo(skb)->nr_frags = i;
  1242. skb->truesize += i * PAGE_SIZE;
  1243. /* remove traces of mapped pages and frag_list */
  1244. skb_frag_list_init(skb);
  1245. uarg = skb_shinfo(skb)->destructor_arg;
  1246. uarg->callback(uarg, true);
  1247. skb_shinfo(skb)->destructor_arg = NULL;
  1248. skb_shinfo(nskb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
  1249. kfree_skb(nskb);
  1250. return 0;
  1251. }
  1252. static int xenvif_tx_submit(struct xenvif_queue *queue)
  1253. {
  1254. struct gnttab_map_grant_ref *gop_map = queue->tx_map_ops;
  1255. struct gnttab_copy *gop_copy = queue->tx_copy_ops;
  1256. struct sk_buff *skb;
  1257. int work_done = 0;
  1258. while ((skb = __skb_dequeue(&queue->tx_queue)) != NULL) {
  1259. struct xen_netif_tx_request *txp;
  1260. u16 pending_idx;
  1261. unsigned data_len;
  1262. pending_idx = XENVIF_TX_CB(skb)->pending_idx;
  1263. txp = &queue->pending_tx_info[pending_idx].req;
  1264. /* Check the remap error code. */
  1265. if (unlikely(xenvif_tx_check_gop(queue, skb, &gop_map, &gop_copy))) {
  1266. skb_shinfo(skb)->nr_frags = 0;
  1267. kfree_skb(skb);
  1268. continue;
  1269. }
  1270. data_len = skb->len;
  1271. callback_param(queue, pending_idx).ctx = NULL;
  1272. if (data_len < txp->size) {
  1273. /* Append the packet payload as a fragment. */
  1274. txp->offset += data_len;
  1275. txp->size -= data_len;
  1276. } else {
  1277. /* Schedule a response immediately. */
  1278. xenvif_idx_release(queue, pending_idx,
  1279. XEN_NETIF_RSP_OKAY);
  1280. }
  1281. if (txp->flags & XEN_NETTXF_csum_blank)
  1282. skb->ip_summed = CHECKSUM_PARTIAL;
  1283. else if (txp->flags & XEN_NETTXF_data_validated)
  1284. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1285. xenvif_fill_frags(queue, skb);
  1286. if (unlikely(skb_has_frag_list(skb))) {
  1287. if (xenvif_handle_frag_list(queue, skb)) {
  1288. if (net_ratelimit())
  1289. netdev_err(queue->vif->dev,
  1290. "Not enough memory to consolidate frag_list!\n");
  1291. skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
  1292. kfree_skb(skb);
  1293. continue;
  1294. }
  1295. }
  1296. if (skb_is_nonlinear(skb) && skb_headlen(skb) < PKT_PROT_LEN) {
  1297. int target = min_t(int, skb->len, PKT_PROT_LEN);
  1298. __pskb_pull_tail(skb, target - skb_headlen(skb));
  1299. }
  1300. skb->dev = queue->vif->dev;
  1301. skb->protocol = eth_type_trans(skb, skb->dev);
  1302. skb_reset_network_header(skb);
  1303. if (checksum_setup(queue, skb)) {
  1304. netdev_dbg(queue->vif->dev,
  1305. "Can't setup checksum in net_tx_action\n");
  1306. /* We have to set this flag to trigger the callback */
  1307. if (skb_shinfo(skb)->destructor_arg)
  1308. skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
  1309. kfree_skb(skb);
  1310. continue;
  1311. }
  1312. skb_probe_transport_header(skb, 0);
  1313. /* If the packet is GSO then we will have just set up the
  1314. * transport header offset in checksum_setup so it's now
  1315. * straightforward to calculate gso_segs.
  1316. */
  1317. if (skb_is_gso(skb)) {
  1318. int mss = skb_shinfo(skb)->gso_size;
  1319. int hdrlen = skb_transport_header(skb) -
  1320. skb_mac_header(skb) +
  1321. tcp_hdrlen(skb);
  1322. skb_shinfo(skb)->gso_segs =
  1323. DIV_ROUND_UP(skb->len - hdrlen, mss);
  1324. }
  1325. queue->stats.rx_bytes += skb->len;
  1326. queue->stats.rx_packets++;
  1327. work_done++;
  1328. /* Set this flag right before netif_receive_skb, otherwise
  1329. * someone might think this packet already left netback, and
  1330. * do a skb_copy_ubufs while we are still in control of the
  1331. * skb. E.g. the __pskb_pull_tail earlier can do such thing.
  1332. */
  1333. if (skb_shinfo(skb)->destructor_arg) {
  1334. skb_shinfo(skb)->tx_flags |= SKBTX_DEV_ZEROCOPY;
  1335. queue->stats.tx_zerocopy_sent++;
  1336. }
  1337. netif_receive_skb(skb);
  1338. }
  1339. return work_done;
  1340. }
  1341. void xenvif_zerocopy_callback(struct ubuf_info *ubuf, bool zerocopy_success)
  1342. {
  1343. unsigned long flags;
  1344. pending_ring_idx_t index;
  1345. struct xenvif_queue *queue = ubuf_to_queue(ubuf);
  1346. /* This is the only place where we grab this lock, to protect callbacks
  1347. * from each other.
  1348. */
  1349. spin_lock_irqsave(&queue->callback_lock, flags);
  1350. do {
  1351. u16 pending_idx = ubuf->desc;
  1352. ubuf = (struct ubuf_info *) ubuf->ctx;
  1353. BUG_ON(queue->dealloc_prod - queue->dealloc_cons >=
  1354. MAX_PENDING_REQS);
  1355. index = pending_index(queue->dealloc_prod);
  1356. queue->dealloc_ring[index] = pending_idx;
  1357. /* Sync with xenvif_tx_dealloc_action:
  1358. * insert idx then incr producer.
  1359. */
  1360. smp_wmb();
  1361. queue->dealloc_prod++;
  1362. } while (ubuf);
  1363. wake_up(&queue->dealloc_wq);
  1364. spin_unlock_irqrestore(&queue->callback_lock, flags);
  1365. if (likely(zerocopy_success))
  1366. queue->stats.tx_zerocopy_success++;
  1367. else
  1368. queue->stats.tx_zerocopy_fail++;
  1369. }
  1370. static inline void xenvif_tx_dealloc_action(struct xenvif_queue *queue)
  1371. {
  1372. struct gnttab_unmap_grant_ref *gop;
  1373. pending_ring_idx_t dc, dp;
  1374. u16 pending_idx, pending_idx_release[MAX_PENDING_REQS];
  1375. unsigned int i = 0;
  1376. dc = queue->dealloc_cons;
  1377. gop = queue->tx_unmap_ops;
  1378. /* Free up any grants we have finished using */
  1379. do {
  1380. dp = queue->dealloc_prod;
  1381. /* Ensure we see all indices enqueued by all
  1382. * xenvif_zerocopy_callback().
  1383. */
  1384. smp_rmb();
  1385. while (dc != dp) {
  1386. BUG_ON(gop - queue->tx_unmap_ops > MAX_PENDING_REQS);
  1387. pending_idx =
  1388. queue->dealloc_ring[pending_index(dc++)];
  1389. pending_idx_release[gop-queue->tx_unmap_ops] =
  1390. pending_idx;
  1391. queue->pages_to_unmap[gop-queue->tx_unmap_ops] =
  1392. queue->mmap_pages[pending_idx];
  1393. gnttab_set_unmap_op(gop,
  1394. idx_to_kaddr(queue, pending_idx),
  1395. GNTMAP_host_map,
  1396. queue->grant_tx_handle[pending_idx]);
  1397. xenvif_grant_handle_reset(queue, pending_idx);
  1398. ++gop;
  1399. }
  1400. } while (dp != queue->dealloc_prod);
  1401. queue->dealloc_cons = dc;
  1402. if (gop - queue->tx_unmap_ops > 0) {
  1403. int ret;
  1404. ret = gnttab_unmap_refs(queue->tx_unmap_ops,
  1405. NULL,
  1406. queue->pages_to_unmap,
  1407. gop - queue->tx_unmap_ops);
  1408. if (ret) {
  1409. netdev_err(queue->vif->dev, "Unmap fail: nr_ops %tx ret %d\n",
  1410. gop - queue->tx_unmap_ops, ret);
  1411. for (i = 0; i < gop - queue->tx_unmap_ops; ++i) {
  1412. if (gop[i].status != GNTST_okay)
  1413. netdev_err(queue->vif->dev,
  1414. " host_addr: %llx handle: %x status: %d\n",
  1415. gop[i].host_addr,
  1416. gop[i].handle,
  1417. gop[i].status);
  1418. }
  1419. BUG();
  1420. }
  1421. }
  1422. for (i = 0; i < gop - queue->tx_unmap_ops; ++i)
  1423. xenvif_idx_release(queue, pending_idx_release[i],
  1424. XEN_NETIF_RSP_OKAY);
  1425. }
  1426. /* Called after netfront has transmitted */
  1427. int xenvif_tx_action(struct xenvif_queue *queue, int budget)
  1428. {
  1429. unsigned nr_mops, nr_cops = 0;
  1430. int work_done, ret;
  1431. if (unlikely(!tx_work_todo(queue)))
  1432. return 0;
  1433. xenvif_tx_build_gops(queue, budget, &nr_cops, &nr_mops);
  1434. if (nr_cops == 0)
  1435. return 0;
  1436. gnttab_batch_copy(queue->tx_copy_ops, nr_cops);
  1437. if (nr_mops != 0) {
  1438. ret = gnttab_map_refs(queue->tx_map_ops,
  1439. NULL,
  1440. queue->pages_to_map,
  1441. nr_mops);
  1442. BUG_ON(ret);
  1443. }
  1444. work_done = xenvif_tx_submit(queue);
  1445. return work_done;
  1446. }
  1447. static void xenvif_idx_release(struct xenvif_queue *queue, u16 pending_idx,
  1448. u8 status)
  1449. {
  1450. struct pending_tx_info *pending_tx_info;
  1451. pending_ring_idx_t index;
  1452. unsigned long flags;
  1453. pending_tx_info = &queue->pending_tx_info[pending_idx];
  1454. spin_lock_irqsave(&queue->response_lock, flags);
  1455. make_tx_response(queue, &pending_tx_info->req, status);
  1456. index = pending_index(queue->pending_prod);
  1457. queue->pending_ring[index] = pending_idx;
  1458. /* TX shouldn't use the index before we give it back here */
  1459. mb();
  1460. queue->pending_prod++;
  1461. spin_unlock_irqrestore(&queue->response_lock, flags);
  1462. }
  1463. static void make_tx_response(struct xenvif_queue *queue,
  1464. struct xen_netif_tx_request *txp,
  1465. s8 st)
  1466. {
  1467. RING_IDX i = queue->tx.rsp_prod_pvt;
  1468. struct xen_netif_tx_response *resp;
  1469. int notify;
  1470. resp = RING_GET_RESPONSE(&queue->tx, i);
  1471. resp->id = txp->id;
  1472. resp->status = st;
  1473. if (txp->flags & XEN_NETTXF_extra_info)
  1474. RING_GET_RESPONSE(&queue->tx, ++i)->status = XEN_NETIF_RSP_NULL;
  1475. queue->tx.rsp_prod_pvt = ++i;
  1476. RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&queue->tx, notify);
  1477. if (notify)
  1478. notify_remote_via_irq(queue->tx_irq);
  1479. }
  1480. static struct xen_netif_rx_response *make_rx_response(struct xenvif_queue *queue,
  1481. u16 id,
  1482. s8 st,
  1483. u16 offset,
  1484. u16 size,
  1485. u16 flags)
  1486. {
  1487. RING_IDX i = queue->rx.rsp_prod_pvt;
  1488. struct xen_netif_rx_response *resp;
  1489. resp = RING_GET_RESPONSE(&queue->rx, i);
  1490. resp->offset = offset;
  1491. resp->flags = flags;
  1492. resp->id = id;
  1493. resp->status = (s16)size;
  1494. if (st < 0)
  1495. resp->status = (s16)st;
  1496. queue->rx.rsp_prod_pvt = ++i;
  1497. return resp;
  1498. }
  1499. void xenvif_idx_unmap(struct xenvif_queue *queue, u16 pending_idx)
  1500. {
  1501. int ret;
  1502. struct gnttab_unmap_grant_ref tx_unmap_op;
  1503. gnttab_set_unmap_op(&tx_unmap_op,
  1504. idx_to_kaddr(queue, pending_idx),
  1505. GNTMAP_host_map,
  1506. queue->grant_tx_handle[pending_idx]);
  1507. xenvif_grant_handle_reset(queue, pending_idx);
  1508. ret = gnttab_unmap_refs(&tx_unmap_op, NULL,
  1509. &queue->mmap_pages[pending_idx], 1);
  1510. if (ret) {
  1511. netdev_err(queue->vif->dev,
  1512. "Unmap fail: ret: %d pending_idx: %d host_addr: %llx handle: %x status: %d\n",
  1513. ret,
  1514. pending_idx,
  1515. tx_unmap_op.host_addr,
  1516. tx_unmap_op.handle,
  1517. tx_unmap_op.status);
  1518. BUG();
  1519. }
  1520. xenvif_idx_release(queue, pending_idx, XEN_NETIF_RSP_OKAY);
  1521. }
  1522. static inline int rx_work_todo(struct xenvif_queue *queue)
  1523. {
  1524. return (!skb_queue_empty(&queue->rx_queue) &&
  1525. xenvif_rx_ring_slots_available(queue, queue->rx_last_skb_slots)) ||
  1526. queue->rx_queue_purge;
  1527. }
  1528. static inline int tx_work_todo(struct xenvif_queue *queue)
  1529. {
  1530. if (likely(RING_HAS_UNCONSUMED_REQUESTS(&queue->tx)))
  1531. return 1;
  1532. return 0;
  1533. }
  1534. static inline bool tx_dealloc_work_todo(struct xenvif_queue *queue)
  1535. {
  1536. return queue->dealloc_cons != queue->dealloc_prod;
  1537. }
  1538. void xenvif_unmap_frontend_rings(struct xenvif_queue *queue)
  1539. {
  1540. if (queue->tx.sring)
  1541. xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(queue->vif),
  1542. queue->tx.sring);
  1543. if (queue->rx.sring)
  1544. xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(queue->vif),
  1545. queue->rx.sring);
  1546. }
  1547. int xenvif_map_frontend_rings(struct xenvif_queue *queue,
  1548. grant_ref_t tx_ring_ref,
  1549. grant_ref_t rx_ring_ref)
  1550. {
  1551. void *addr;
  1552. struct xen_netif_tx_sring *txs;
  1553. struct xen_netif_rx_sring *rxs;
  1554. int err = -ENOMEM;
  1555. err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(queue->vif),
  1556. tx_ring_ref, &addr);
  1557. if (err)
  1558. goto err;
  1559. txs = (struct xen_netif_tx_sring *)addr;
  1560. BACK_RING_INIT(&queue->tx, txs, PAGE_SIZE);
  1561. err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(queue->vif),
  1562. rx_ring_ref, &addr);
  1563. if (err)
  1564. goto err;
  1565. rxs = (struct xen_netif_rx_sring *)addr;
  1566. BACK_RING_INIT(&queue->rx, rxs, PAGE_SIZE);
  1567. return 0;
  1568. err:
  1569. xenvif_unmap_frontend_rings(queue);
  1570. return err;
  1571. }
  1572. static void xenvif_start_queue(struct xenvif_queue *queue)
  1573. {
  1574. if (xenvif_schedulable(queue->vif))
  1575. xenvif_wake_queue(queue);
  1576. }
  1577. int xenvif_kthread_guest_rx(void *data)
  1578. {
  1579. struct xenvif_queue *queue = data;
  1580. struct sk_buff *skb;
  1581. while (!kthread_should_stop()) {
  1582. wait_event_interruptible(queue->wq,
  1583. rx_work_todo(queue) ||
  1584. queue->vif->disabled ||
  1585. kthread_should_stop());
  1586. /* This frontend is found to be rogue, disable it in
  1587. * kthread context. Currently this is only set when
  1588. * netback finds out frontend sends malformed packet,
  1589. * but we cannot disable the interface in softirq
  1590. * context so we defer it here, if this thread is
  1591. * associated with queue 0.
  1592. */
  1593. if (unlikely(queue->vif->disabled && netif_carrier_ok(queue->vif->dev) && queue->id == 0))
  1594. xenvif_carrier_off(queue->vif);
  1595. if (kthread_should_stop())
  1596. break;
  1597. if (queue->rx_queue_purge) {
  1598. skb_queue_purge(&queue->rx_queue);
  1599. queue->rx_queue_purge = false;
  1600. }
  1601. if (!skb_queue_empty(&queue->rx_queue))
  1602. xenvif_rx_action(queue);
  1603. if (skb_queue_empty(&queue->rx_queue) &&
  1604. xenvif_queue_stopped(queue)) {
  1605. del_timer_sync(&queue->wake_queue);
  1606. xenvif_start_queue(queue);
  1607. }
  1608. cond_resched();
  1609. }
  1610. /* Bin any remaining skbs */
  1611. while ((skb = skb_dequeue(&queue->rx_queue)) != NULL)
  1612. dev_kfree_skb(skb);
  1613. return 0;
  1614. }
  1615. int xenvif_dealloc_kthread(void *data)
  1616. {
  1617. struct xenvif_queue *queue = data;
  1618. while (!kthread_should_stop()) {
  1619. wait_event_interruptible(queue->dealloc_wq,
  1620. tx_dealloc_work_todo(queue) ||
  1621. kthread_should_stop());
  1622. if (kthread_should_stop())
  1623. break;
  1624. xenvif_tx_dealloc_action(queue);
  1625. cond_resched();
  1626. }
  1627. /* Unmap anything remaining*/
  1628. if (tx_dealloc_work_todo(queue))
  1629. xenvif_tx_dealloc_action(queue);
  1630. return 0;
  1631. }
  1632. static int __init netback_init(void)
  1633. {
  1634. int rc = 0;
  1635. if (!xen_domain())
  1636. return -ENODEV;
  1637. /* Allow as many queues as there are CPUs, by default */
  1638. xenvif_max_queues = num_online_cpus();
  1639. if (fatal_skb_slots < XEN_NETBK_LEGACY_SLOTS_MAX) {
  1640. pr_info("fatal_skb_slots too small (%d), bump it to XEN_NETBK_LEGACY_SLOTS_MAX (%d)\n",
  1641. fatal_skb_slots, XEN_NETBK_LEGACY_SLOTS_MAX);
  1642. fatal_skb_slots = XEN_NETBK_LEGACY_SLOTS_MAX;
  1643. }
  1644. rc = xenvif_xenbus_init();
  1645. if (rc)
  1646. goto failed_init;
  1647. rx_drain_timeout_jiffies = msecs_to_jiffies(rx_drain_timeout_msecs);
  1648. return 0;
  1649. failed_init:
  1650. return rc;
  1651. }
  1652. module_init(netback_init);
  1653. static void __exit netback_fini(void)
  1654. {
  1655. xenvif_xenbus_fini();
  1656. }
  1657. module_exit(netback_fini);
  1658. MODULE_LICENSE("Dual BSD/GPL");
  1659. MODULE_ALIAS("xen-backend:vif");