netback.c 53 KB

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