netback.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035
  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. spin_lock_irqsave(&queue->response_lock, flags);
  527. make_tx_response(queue, txp, XEN_NETIF_RSP_ERROR);
  528. spin_unlock_irqrestore(&queue->response_lock, flags);
  529. if (cons == end)
  530. break;
  531. txp = RING_GET_REQUEST(&queue->tx, cons++);
  532. } while (1);
  533. queue->tx.req_cons = cons;
  534. }
  535. static void xenvif_fatal_tx_err(struct xenvif *vif)
  536. {
  537. netdev_err(vif->dev, "fatal error; disabling device\n");
  538. vif->disabled = true;
  539. /* Disable the vif from queue 0's kthread */
  540. if (vif->queues)
  541. xenvif_kick_thread(&vif->queues[0]);
  542. }
  543. static int xenvif_count_requests(struct xenvif_queue *queue,
  544. struct xen_netif_tx_request *first,
  545. struct xen_netif_tx_request *txp,
  546. int work_to_do)
  547. {
  548. RING_IDX cons = queue->tx.req_cons;
  549. int slots = 0;
  550. int drop_err = 0;
  551. int more_data;
  552. if (!(first->flags & XEN_NETTXF_more_data))
  553. return 0;
  554. do {
  555. struct xen_netif_tx_request dropped_tx = { 0 };
  556. if (slots >= work_to_do) {
  557. netdev_err(queue->vif->dev,
  558. "Asked for %d slots but exceeds this limit\n",
  559. work_to_do);
  560. xenvif_fatal_tx_err(queue->vif);
  561. return -ENODATA;
  562. }
  563. /* This guest is really using too many slots and
  564. * considered malicious.
  565. */
  566. if (unlikely(slots >= fatal_skb_slots)) {
  567. netdev_err(queue->vif->dev,
  568. "Malicious frontend using %d slots, threshold %u\n",
  569. slots, fatal_skb_slots);
  570. xenvif_fatal_tx_err(queue->vif);
  571. return -E2BIG;
  572. }
  573. /* Xen network protocol had implicit dependency on
  574. * MAX_SKB_FRAGS. XEN_NETBK_LEGACY_SLOTS_MAX is set to
  575. * the historical MAX_SKB_FRAGS value 18 to honor the
  576. * same behavior as before. Any packet using more than
  577. * 18 slots but less than fatal_skb_slots slots is
  578. * dropped
  579. */
  580. if (!drop_err && slots >= XEN_NETBK_LEGACY_SLOTS_MAX) {
  581. if (net_ratelimit())
  582. netdev_dbg(queue->vif->dev,
  583. "Too many slots (%d) exceeding limit (%d), dropping packet\n",
  584. slots, XEN_NETBK_LEGACY_SLOTS_MAX);
  585. drop_err = -E2BIG;
  586. }
  587. if (drop_err)
  588. txp = &dropped_tx;
  589. memcpy(txp, RING_GET_REQUEST(&queue->tx, cons + slots),
  590. sizeof(*txp));
  591. /* If the guest submitted a frame >= 64 KiB then
  592. * first->size overflowed and following slots will
  593. * appear to be larger than the frame.
  594. *
  595. * This cannot be fatal error as there are buggy
  596. * frontends that do this.
  597. *
  598. * Consume all slots and drop the packet.
  599. */
  600. if (!drop_err && txp->size > first->size) {
  601. if (net_ratelimit())
  602. netdev_dbg(queue->vif->dev,
  603. "Invalid tx request, slot size %u > remaining size %u\n",
  604. txp->size, first->size);
  605. drop_err = -EIO;
  606. }
  607. first->size -= txp->size;
  608. slots++;
  609. if (unlikely((txp->offset + txp->size) > PAGE_SIZE)) {
  610. netdev_err(queue->vif->dev, "Cross page boundary, txp->offset: %x, size: %u\n",
  611. txp->offset, txp->size);
  612. xenvif_fatal_tx_err(queue->vif);
  613. return -EINVAL;
  614. }
  615. more_data = txp->flags & XEN_NETTXF_more_data;
  616. if (!drop_err)
  617. txp++;
  618. } while (more_data);
  619. if (drop_err) {
  620. xenvif_tx_err(queue, first, cons + slots);
  621. return drop_err;
  622. }
  623. return slots;
  624. }
  625. struct xenvif_tx_cb {
  626. u16 pending_idx;
  627. };
  628. #define XENVIF_TX_CB(skb) ((struct xenvif_tx_cb *)(skb)->cb)
  629. static inline void xenvif_tx_create_map_op(struct xenvif_queue *queue,
  630. u16 pending_idx,
  631. struct xen_netif_tx_request *txp,
  632. struct gnttab_map_grant_ref *mop)
  633. {
  634. queue->pages_to_map[mop-queue->tx_map_ops] = queue->mmap_pages[pending_idx];
  635. gnttab_set_map_op(mop, idx_to_kaddr(queue, pending_idx),
  636. GNTMAP_host_map | GNTMAP_readonly,
  637. txp->gref, queue->vif->domid);
  638. memcpy(&queue->pending_tx_info[pending_idx].req, txp,
  639. sizeof(*txp));
  640. }
  641. static inline struct sk_buff *xenvif_alloc_skb(unsigned int size)
  642. {
  643. struct sk_buff *skb =
  644. alloc_skb(size + NET_SKB_PAD + NET_IP_ALIGN,
  645. GFP_ATOMIC | __GFP_NOWARN);
  646. if (unlikely(skb == NULL))
  647. return NULL;
  648. /* Packets passed to netif_rx() must have some headroom. */
  649. skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
  650. /* Initialize it here to avoid later surprises */
  651. skb_shinfo(skb)->destructor_arg = NULL;
  652. return skb;
  653. }
  654. static struct gnttab_map_grant_ref *xenvif_get_requests(struct xenvif_queue *queue,
  655. struct sk_buff *skb,
  656. struct xen_netif_tx_request *txp,
  657. struct gnttab_map_grant_ref *gop)
  658. {
  659. struct skb_shared_info *shinfo = skb_shinfo(skb);
  660. skb_frag_t *frags = shinfo->frags;
  661. u16 pending_idx = XENVIF_TX_CB(skb)->pending_idx;
  662. int start;
  663. pending_ring_idx_t index;
  664. unsigned int nr_slots, frag_overflow = 0;
  665. /* At this point shinfo->nr_frags is in fact the number of
  666. * slots, which can be as large as XEN_NETBK_LEGACY_SLOTS_MAX.
  667. */
  668. if (shinfo->nr_frags > MAX_SKB_FRAGS) {
  669. frag_overflow = shinfo->nr_frags - MAX_SKB_FRAGS;
  670. BUG_ON(frag_overflow > MAX_SKB_FRAGS);
  671. shinfo->nr_frags = MAX_SKB_FRAGS;
  672. }
  673. nr_slots = shinfo->nr_frags;
  674. /* Skip first skb fragment if it is on same page as header fragment. */
  675. start = (frag_get_pending_idx(&shinfo->frags[0]) == pending_idx);
  676. for (shinfo->nr_frags = start; shinfo->nr_frags < nr_slots;
  677. shinfo->nr_frags++, txp++, gop++) {
  678. index = pending_index(queue->pending_cons++);
  679. pending_idx = queue->pending_ring[index];
  680. xenvif_tx_create_map_op(queue, pending_idx, txp, gop);
  681. frag_set_pending_idx(&frags[shinfo->nr_frags], pending_idx);
  682. }
  683. if (frag_overflow) {
  684. struct sk_buff *nskb = xenvif_alloc_skb(0);
  685. if (unlikely(nskb == NULL)) {
  686. if (net_ratelimit())
  687. netdev_err(queue->vif->dev,
  688. "Can't allocate the frag_list skb.\n");
  689. return NULL;
  690. }
  691. shinfo = skb_shinfo(nskb);
  692. frags = shinfo->frags;
  693. for (shinfo->nr_frags = 0; shinfo->nr_frags < frag_overflow;
  694. shinfo->nr_frags++, txp++, gop++) {
  695. index = pending_index(queue->pending_cons++);
  696. pending_idx = queue->pending_ring[index];
  697. xenvif_tx_create_map_op(queue, pending_idx, txp, gop);
  698. frag_set_pending_idx(&frags[shinfo->nr_frags],
  699. pending_idx);
  700. }
  701. skb_shinfo(skb)->frag_list = nskb;
  702. }
  703. return gop;
  704. }
  705. static inline void xenvif_grant_handle_set(struct xenvif_queue *queue,
  706. u16 pending_idx,
  707. grant_handle_t handle)
  708. {
  709. if (unlikely(queue->grant_tx_handle[pending_idx] !=
  710. NETBACK_INVALID_HANDLE)) {
  711. netdev_err(queue->vif->dev,
  712. "Trying to overwrite active handle! pending_idx: %x\n",
  713. pending_idx);
  714. BUG();
  715. }
  716. queue->grant_tx_handle[pending_idx] = handle;
  717. }
  718. static inline void xenvif_grant_handle_reset(struct xenvif_queue *queue,
  719. u16 pending_idx)
  720. {
  721. if (unlikely(queue->grant_tx_handle[pending_idx] ==
  722. NETBACK_INVALID_HANDLE)) {
  723. netdev_err(queue->vif->dev,
  724. "Trying to unmap invalid handle! pending_idx: %x\n",
  725. pending_idx);
  726. BUG();
  727. }
  728. queue->grant_tx_handle[pending_idx] = NETBACK_INVALID_HANDLE;
  729. }
  730. static int xenvif_tx_check_gop(struct xenvif_queue *queue,
  731. struct sk_buff *skb,
  732. struct gnttab_map_grant_ref **gopp_map,
  733. struct gnttab_copy **gopp_copy)
  734. {
  735. struct gnttab_map_grant_ref *gop_map = *gopp_map;
  736. u16 pending_idx = XENVIF_TX_CB(skb)->pending_idx;
  737. /* This always points to the shinfo of the skb being checked, which
  738. * could be either the first or the one on the frag_list
  739. */
  740. struct skb_shared_info *shinfo = skb_shinfo(skb);
  741. /* If this is non-NULL, we are currently checking the frag_list skb, and
  742. * this points to the shinfo of the first one
  743. */
  744. struct skb_shared_info *first_shinfo = NULL;
  745. int nr_frags = shinfo->nr_frags;
  746. const bool sharedslot = nr_frags &&
  747. frag_get_pending_idx(&shinfo->frags[0]) == pending_idx;
  748. int i, err;
  749. /* Check status of header. */
  750. err = (*gopp_copy)->status;
  751. if (unlikely(err)) {
  752. if (net_ratelimit())
  753. netdev_dbg(queue->vif->dev,
  754. "Grant copy of header failed! status: %d pending_idx: %u ref: %u\n",
  755. (*gopp_copy)->status,
  756. pending_idx,
  757. (*gopp_copy)->source.u.ref);
  758. /* The first frag might still have this slot mapped */
  759. if (!sharedslot)
  760. xenvif_idx_release(queue, pending_idx,
  761. XEN_NETIF_RSP_ERROR);
  762. }
  763. (*gopp_copy)++;
  764. check_frags:
  765. for (i = 0; i < nr_frags; i++, gop_map++) {
  766. int j, newerr;
  767. pending_idx = frag_get_pending_idx(&shinfo->frags[i]);
  768. /* Check error status: if okay then remember grant handle. */
  769. newerr = gop_map->status;
  770. if (likely(!newerr)) {
  771. xenvif_grant_handle_set(queue,
  772. pending_idx,
  773. gop_map->handle);
  774. /* Had a previous error? Invalidate this fragment. */
  775. if (unlikely(err)) {
  776. xenvif_idx_unmap(queue, pending_idx);
  777. /* If the mapping of the first frag was OK, but
  778. * the header's copy failed, and they are
  779. * sharing a slot, send an error
  780. */
  781. if (i == 0 && sharedslot)
  782. xenvif_idx_release(queue, pending_idx,
  783. XEN_NETIF_RSP_ERROR);
  784. else
  785. xenvif_idx_release(queue, pending_idx,
  786. XEN_NETIF_RSP_OKAY);
  787. }
  788. continue;
  789. }
  790. /* Error on this fragment: respond to client with an error. */
  791. if (net_ratelimit())
  792. netdev_dbg(queue->vif->dev,
  793. "Grant map of %d. frag failed! status: %d pending_idx: %u ref: %u\n",
  794. i,
  795. gop_map->status,
  796. pending_idx,
  797. gop_map->ref);
  798. xenvif_idx_release(queue, pending_idx, XEN_NETIF_RSP_ERROR);
  799. /* Not the first error? Preceding frags already invalidated. */
  800. if (err)
  801. continue;
  802. /* First error: if the header haven't shared a slot with the
  803. * first frag, release it as well.
  804. */
  805. if (!sharedslot)
  806. xenvif_idx_release(queue,
  807. XENVIF_TX_CB(skb)->pending_idx,
  808. XEN_NETIF_RSP_OKAY);
  809. /* Invalidate preceding fragments of this skb. */
  810. for (j = 0; j < i; j++) {
  811. pending_idx = frag_get_pending_idx(&shinfo->frags[j]);
  812. xenvif_idx_unmap(queue, pending_idx);
  813. xenvif_idx_release(queue, pending_idx,
  814. XEN_NETIF_RSP_OKAY);
  815. }
  816. /* And if we found the error while checking the frag_list, unmap
  817. * the first skb's frags
  818. */
  819. if (first_shinfo) {
  820. for (j = 0; j < first_shinfo->nr_frags; j++) {
  821. pending_idx = frag_get_pending_idx(&first_shinfo->frags[j]);
  822. xenvif_idx_unmap(queue, pending_idx);
  823. xenvif_idx_release(queue, pending_idx,
  824. XEN_NETIF_RSP_OKAY);
  825. }
  826. }
  827. /* Remember the error: invalidate all subsequent fragments. */
  828. err = newerr;
  829. }
  830. if (skb_has_frag_list(skb) && !first_shinfo) {
  831. first_shinfo = skb_shinfo(skb);
  832. shinfo = skb_shinfo(skb_shinfo(skb)->frag_list);
  833. nr_frags = shinfo->nr_frags;
  834. goto check_frags;
  835. }
  836. *gopp_map = gop_map;
  837. return err;
  838. }
  839. static void xenvif_fill_frags(struct xenvif_queue *queue, struct sk_buff *skb)
  840. {
  841. struct skb_shared_info *shinfo = skb_shinfo(skb);
  842. int nr_frags = shinfo->nr_frags;
  843. int i;
  844. u16 prev_pending_idx = INVALID_PENDING_IDX;
  845. for (i = 0; i < nr_frags; i++) {
  846. skb_frag_t *frag = shinfo->frags + i;
  847. struct xen_netif_tx_request *txp;
  848. struct page *page;
  849. u16 pending_idx;
  850. pending_idx = frag_get_pending_idx(frag);
  851. /* If this is not the first frag, chain it to the previous*/
  852. if (prev_pending_idx == INVALID_PENDING_IDX)
  853. skb_shinfo(skb)->destructor_arg =
  854. &callback_param(queue, pending_idx);
  855. else
  856. callback_param(queue, prev_pending_idx).ctx =
  857. &callback_param(queue, pending_idx);
  858. callback_param(queue, pending_idx).ctx = NULL;
  859. prev_pending_idx = pending_idx;
  860. txp = &queue->pending_tx_info[pending_idx].req;
  861. page = virt_to_page(idx_to_kaddr(queue, pending_idx));
  862. __skb_fill_page_desc(skb, i, page, txp->offset, txp->size);
  863. skb->len += txp->size;
  864. skb->data_len += txp->size;
  865. skb->truesize += txp->size;
  866. /* Take an extra reference to offset network stack's put_page */
  867. get_page(queue->mmap_pages[pending_idx]);
  868. }
  869. }
  870. static int xenvif_get_extras(struct xenvif_queue *queue,
  871. struct xen_netif_extra_info *extras,
  872. int work_to_do)
  873. {
  874. struct xen_netif_extra_info extra;
  875. RING_IDX cons = queue->tx.req_cons;
  876. do {
  877. if (unlikely(work_to_do-- <= 0)) {
  878. netdev_err(queue->vif->dev, "Missing extra info\n");
  879. xenvif_fatal_tx_err(queue->vif);
  880. return -EBADR;
  881. }
  882. memcpy(&extra, RING_GET_REQUEST(&queue->tx, cons),
  883. sizeof(extra));
  884. if (unlikely(!extra.type ||
  885. extra.type >= XEN_NETIF_EXTRA_TYPE_MAX)) {
  886. queue->tx.req_cons = ++cons;
  887. netdev_err(queue->vif->dev,
  888. "Invalid extra type: %d\n", extra.type);
  889. xenvif_fatal_tx_err(queue->vif);
  890. return -EINVAL;
  891. }
  892. memcpy(&extras[extra.type - 1], &extra, sizeof(extra));
  893. queue->tx.req_cons = ++cons;
  894. } while (extra.flags & XEN_NETIF_EXTRA_FLAG_MORE);
  895. return work_to_do;
  896. }
  897. static int xenvif_set_skb_gso(struct xenvif *vif,
  898. struct sk_buff *skb,
  899. struct xen_netif_extra_info *gso)
  900. {
  901. if (!gso->u.gso.size) {
  902. netdev_err(vif->dev, "GSO size must not be zero.\n");
  903. xenvif_fatal_tx_err(vif);
  904. return -EINVAL;
  905. }
  906. switch (gso->u.gso.type) {
  907. case XEN_NETIF_GSO_TYPE_TCPV4:
  908. skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
  909. break;
  910. case XEN_NETIF_GSO_TYPE_TCPV6:
  911. skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
  912. break;
  913. default:
  914. netdev_err(vif->dev, "Bad GSO type %d.\n", gso->u.gso.type);
  915. xenvif_fatal_tx_err(vif);
  916. return -EINVAL;
  917. }
  918. skb_shinfo(skb)->gso_size = gso->u.gso.size;
  919. /* gso_segs will be calculated later */
  920. return 0;
  921. }
  922. static int checksum_setup(struct xenvif_queue *queue, struct sk_buff *skb)
  923. {
  924. bool recalculate_partial_csum = false;
  925. /* A GSO SKB must be CHECKSUM_PARTIAL. However some buggy
  926. * peers can fail to set NETRXF_csum_blank when sending a GSO
  927. * frame. In this case force the SKB to CHECKSUM_PARTIAL and
  928. * recalculate the partial checksum.
  929. */
  930. if (skb->ip_summed != CHECKSUM_PARTIAL && skb_is_gso(skb)) {
  931. queue->stats.rx_gso_checksum_fixup++;
  932. skb->ip_summed = CHECKSUM_PARTIAL;
  933. recalculate_partial_csum = true;
  934. }
  935. /* A non-CHECKSUM_PARTIAL SKB does not require setup. */
  936. if (skb->ip_summed != CHECKSUM_PARTIAL)
  937. return 0;
  938. return skb_checksum_setup(skb, recalculate_partial_csum);
  939. }
  940. static bool tx_credit_exceeded(struct xenvif_queue *queue, unsigned size)
  941. {
  942. u64 now = get_jiffies_64();
  943. u64 next_credit = queue->credit_window_start +
  944. msecs_to_jiffies(queue->credit_usec / 1000);
  945. /* Timer could already be pending in rare cases. */
  946. if (timer_pending(&queue->credit_timeout))
  947. return true;
  948. /* Passed the point where we can replenish credit? */
  949. if (time_after_eq64(now, next_credit)) {
  950. queue->credit_window_start = now;
  951. tx_add_credit(queue);
  952. }
  953. /* Still too big to send right now? Set a callback. */
  954. if (size > queue->remaining_credit) {
  955. queue->credit_timeout.data =
  956. (unsigned long)queue;
  957. queue->credit_timeout.function =
  958. tx_credit_callback;
  959. mod_timer(&queue->credit_timeout,
  960. next_credit);
  961. queue->credit_window_start = next_credit;
  962. return true;
  963. }
  964. return false;
  965. }
  966. static void xenvif_tx_build_gops(struct xenvif_queue *queue,
  967. int budget,
  968. unsigned *copy_ops,
  969. unsigned *map_ops)
  970. {
  971. struct gnttab_map_grant_ref *gop = queue->tx_map_ops, *request_gop;
  972. struct sk_buff *skb;
  973. int ret;
  974. while (skb_queue_len(&queue->tx_queue) < budget) {
  975. struct xen_netif_tx_request txreq;
  976. struct xen_netif_tx_request txfrags[XEN_NETBK_LEGACY_SLOTS_MAX];
  977. struct xen_netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX-1];
  978. u16 pending_idx;
  979. RING_IDX idx;
  980. int work_to_do;
  981. unsigned int data_len;
  982. pending_ring_idx_t index;
  983. if (queue->tx.sring->req_prod - queue->tx.req_cons >
  984. XEN_NETIF_TX_RING_SIZE) {
  985. netdev_err(queue->vif->dev,
  986. "Impossible number of requests. "
  987. "req_prod %d, req_cons %d, size %ld\n",
  988. queue->tx.sring->req_prod, queue->tx.req_cons,
  989. XEN_NETIF_TX_RING_SIZE);
  990. xenvif_fatal_tx_err(queue->vif);
  991. break;
  992. }
  993. work_to_do = RING_HAS_UNCONSUMED_REQUESTS(&queue->tx);
  994. if (!work_to_do)
  995. break;
  996. idx = queue->tx.req_cons;
  997. rmb(); /* Ensure that we see the request before we copy it. */
  998. memcpy(&txreq, RING_GET_REQUEST(&queue->tx, idx), sizeof(txreq));
  999. /* Credit-based scheduling. */
  1000. if (txreq.size > queue->remaining_credit &&
  1001. tx_credit_exceeded(queue, txreq.size))
  1002. break;
  1003. queue->remaining_credit -= txreq.size;
  1004. work_to_do--;
  1005. queue->tx.req_cons = ++idx;
  1006. memset(extras, 0, sizeof(extras));
  1007. if (txreq.flags & XEN_NETTXF_extra_info) {
  1008. work_to_do = xenvif_get_extras(queue, extras,
  1009. work_to_do);
  1010. idx = queue->tx.req_cons;
  1011. if (unlikely(work_to_do < 0))
  1012. break;
  1013. }
  1014. ret = xenvif_count_requests(queue, &txreq, txfrags, work_to_do);
  1015. if (unlikely(ret < 0))
  1016. break;
  1017. idx += ret;
  1018. if (unlikely(txreq.size < ETH_HLEN)) {
  1019. netdev_dbg(queue->vif->dev,
  1020. "Bad packet size: %d\n", txreq.size);
  1021. xenvif_tx_err(queue, &txreq, idx);
  1022. break;
  1023. }
  1024. /* No crossing a page as the payload mustn't fragment. */
  1025. if (unlikely((txreq.offset + txreq.size) > PAGE_SIZE)) {
  1026. netdev_err(queue->vif->dev,
  1027. "txreq.offset: %x, size: %u, end: %lu\n",
  1028. txreq.offset, txreq.size,
  1029. (txreq.offset&~PAGE_MASK) + txreq.size);
  1030. xenvif_fatal_tx_err(queue->vif);
  1031. break;
  1032. }
  1033. index = pending_index(queue->pending_cons);
  1034. pending_idx = queue->pending_ring[index];
  1035. data_len = (txreq.size > XEN_NETBACK_TX_COPY_LEN &&
  1036. ret < XEN_NETBK_LEGACY_SLOTS_MAX) ?
  1037. XEN_NETBACK_TX_COPY_LEN : txreq.size;
  1038. skb = xenvif_alloc_skb(data_len);
  1039. if (unlikely(skb == NULL)) {
  1040. netdev_dbg(queue->vif->dev,
  1041. "Can't allocate a skb in start_xmit.\n");
  1042. xenvif_tx_err(queue, &txreq, idx);
  1043. break;
  1044. }
  1045. if (extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].type) {
  1046. struct xen_netif_extra_info *gso;
  1047. gso = &extras[XEN_NETIF_EXTRA_TYPE_GSO - 1];
  1048. if (xenvif_set_skb_gso(queue->vif, skb, gso)) {
  1049. /* Failure in xenvif_set_skb_gso is fatal. */
  1050. kfree_skb(skb);
  1051. break;
  1052. }
  1053. }
  1054. XENVIF_TX_CB(skb)->pending_idx = pending_idx;
  1055. __skb_put(skb, data_len);
  1056. queue->tx_copy_ops[*copy_ops].source.u.ref = txreq.gref;
  1057. queue->tx_copy_ops[*copy_ops].source.domid = queue->vif->domid;
  1058. queue->tx_copy_ops[*copy_ops].source.offset = txreq.offset;
  1059. queue->tx_copy_ops[*copy_ops].dest.u.gmfn =
  1060. virt_to_mfn(skb->data);
  1061. queue->tx_copy_ops[*copy_ops].dest.domid = DOMID_SELF;
  1062. queue->tx_copy_ops[*copy_ops].dest.offset =
  1063. offset_in_page(skb->data);
  1064. queue->tx_copy_ops[*copy_ops].len = data_len;
  1065. queue->tx_copy_ops[*copy_ops].flags = GNTCOPY_source_gref;
  1066. (*copy_ops)++;
  1067. skb_shinfo(skb)->nr_frags = ret;
  1068. if (data_len < txreq.size) {
  1069. skb_shinfo(skb)->nr_frags++;
  1070. frag_set_pending_idx(&skb_shinfo(skb)->frags[0],
  1071. pending_idx);
  1072. xenvif_tx_create_map_op(queue, pending_idx, &txreq, gop);
  1073. gop++;
  1074. } else {
  1075. frag_set_pending_idx(&skb_shinfo(skb)->frags[0],
  1076. INVALID_PENDING_IDX);
  1077. memcpy(&queue->pending_tx_info[pending_idx].req, &txreq,
  1078. sizeof(txreq));
  1079. }
  1080. queue->pending_cons++;
  1081. request_gop = xenvif_get_requests(queue, skb, txfrags, gop);
  1082. if (request_gop == NULL) {
  1083. kfree_skb(skb);
  1084. xenvif_tx_err(queue, &txreq, idx);
  1085. break;
  1086. }
  1087. gop = request_gop;
  1088. __skb_queue_tail(&queue->tx_queue, skb);
  1089. queue->tx.req_cons = idx;
  1090. if (((gop-queue->tx_map_ops) >= ARRAY_SIZE(queue->tx_map_ops)) ||
  1091. (*copy_ops >= ARRAY_SIZE(queue->tx_copy_ops)))
  1092. break;
  1093. }
  1094. (*map_ops) = gop - queue->tx_map_ops;
  1095. return;
  1096. }
  1097. /* Consolidate skb with a frag_list into a brand new one with local pages on
  1098. * frags. Returns 0 or -ENOMEM if can't allocate new pages.
  1099. */
  1100. static int xenvif_handle_frag_list(struct xenvif_queue *queue, struct sk_buff *skb)
  1101. {
  1102. unsigned int offset = skb_headlen(skb);
  1103. skb_frag_t frags[MAX_SKB_FRAGS];
  1104. int i;
  1105. struct ubuf_info *uarg;
  1106. struct sk_buff *nskb = skb_shinfo(skb)->frag_list;
  1107. queue->stats.tx_zerocopy_sent += 2;
  1108. queue->stats.tx_frag_overflow++;
  1109. xenvif_fill_frags(queue, nskb);
  1110. /* Subtract frags size, we will correct it later */
  1111. skb->truesize -= skb->data_len;
  1112. skb->len += nskb->len;
  1113. skb->data_len += nskb->len;
  1114. /* create a brand new frags array and coalesce there */
  1115. for (i = 0; offset < skb->len; i++) {
  1116. struct page *page;
  1117. unsigned int len;
  1118. BUG_ON(i >= MAX_SKB_FRAGS);
  1119. page = alloc_page(GFP_ATOMIC);
  1120. if (!page) {
  1121. int j;
  1122. skb->truesize += skb->data_len;
  1123. for (j = 0; j < i; j++)
  1124. put_page(frags[j].page.p);
  1125. return -ENOMEM;
  1126. }
  1127. if (offset + PAGE_SIZE < skb->len)
  1128. len = PAGE_SIZE;
  1129. else
  1130. len = skb->len - offset;
  1131. if (skb_copy_bits(skb, offset, page_address(page), len))
  1132. BUG();
  1133. offset += len;
  1134. frags[i].page.p = page;
  1135. frags[i].page_offset = 0;
  1136. skb_frag_size_set(&frags[i], len);
  1137. }
  1138. /* swap out with old one */
  1139. memcpy(skb_shinfo(skb)->frags,
  1140. frags,
  1141. i * sizeof(skb_frag_t));
  1142. skb_shinfo(skb)->nr_frags = i;
  1143. skb->truesize += i * PAGE_SIZE;
  1144. /* remove traces of mapped pages and frag_list */
  1145. skb_frag_list_init(skb);
  1146. uarg = skb_shinfo(skb)->destructor_arg;
  1147. /* increase inflight counter to offset decrement in callback */
  1148. atomic_inc(&queue->inflight_packets);
  1149. uarg->callback(uarg, true);
  1150. skb_shinfo(skb)->destructor_arg = NULL;
  1151. xenvif_skb_zerocopy_prepare(queue, nskb);
  1152. kfree_skb(nskb);
  1153. return 0;
  1154. }
  1155. static int xenvif_tx_submit(struct xenvif_queue *queue)
  1156. {
  1157. struct gnttab_map_grant_ref *gop_map = queue->tx_map_ops;
  1158. struct gnttab_copy *gop_copy = queue->tx_copy_ops;
  1159. struct sk_buff *skb;
  1160. int work_done = 0;
  1161. while ((skb = __skb_dequeue(&queue->tx_queue)) != NULL) {
  1162. struct xen_netif_tx_request *txp;
  1163. u16 pending_idx;
  1164. unsigned data_len;
  1165. pending_idx = XENVIF_TX_CB(skb)->pending_idx;
  1166. txp = &queue->pending_tx_info[pending_idx].req;
  1167. /* Check the remap error code. */
  1168. if (unlikely(xenvif_tx_check_gop(queue, skb, &gop_map, &gop_copy))) {
  1169. /* If there was an error, xenvif_tx_check_gop is
  1170. * expected to release all the frags which were mapped,
  1171. * so kfree_skb shouldn't do it again
  1172. */
  1173. skb_shinfo(skb)->nr_frags = 0;
  1174. if (skb_has_frag_list(skb)) {
  1175. struct sk_buff *nskb =
  1176. skb_shinfo(skb)->frag_list;
  1177. skb_shinfo(nskb)->nr_frags = 0;
  1178. }
  1179. kfree_skb(skb);
  1180. continue;
  1181. }
  1182. data_len = skb->len;
  1183. callback_param(queue, pending_idx).ctx = NULL;
  1184. if (data_len < txp->size) {
  1185. /* Append the packet payload as a fragment. */
  1186. txp->offset += data_len;
  1187. txp->size -= data_len;
  1188. } else {
  1189. /* Schedule a response immediately. */
  1190. xenvif_idx_release(queue, pending_idx,
  1191. XEN_NETIF_RSP_OKAY);
  1192. }
  1193. if (txp->flags & XEN_NETTXF_csum_blank)
  1194. skb->ip_summed = CHECKSUM_PARTIAL;
  1195. else if (txp->flags & XEN_NETTXF_data_validated)
  1196. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1197. xenvif_fill_frags(queue, skb);
  1198. if (unlikely(skb_has_frag_list(skb))) {
  1199. if (xenvif_handle_frag_list(queue, skb)) {
  1200. if (net_ratelimit())
  1201. netdev_err(queue->vif->dev,
  1202. "Not enough memory to consolidate frag_list!\n");
  1203. xenvif_skb_zerocopy_prepare(queue, skb);
  1204. kfree_skb(skb);
  1205. continue;
  1206. }
  1207. }
  1208. skb->dev = queue->vif->dev;
  1209. skb->protocol = eth_type_trans(skb, skb->dev);
  1210. skb_reset_network_header(skb);
  1211. if (checksum_setup(queue, skb)) {
  1212. netdev_dbg(queue->vif->dev,
  1213. "Can't setup checksum in net_tx_action\n");
  1214. /* We have to set this flag to trigger the callback */
  1215. if (skb_shinfo(skb)->destructor_arg)
  1216. xenvif_skb_zerocopy_prepare(queue, skb);
  1217. kfree_skb(skb);
  1218. continue;
  1219. }
  1220. skb_probe_transport_header(skb, 0);
  1221. /* If the packet is GSO then we will have just set up the
  1222. * transport header offset in checksum_setup so it's now
  1223. * straightforward to calculate gso_segs.
  1224. */
  1225. if (skb_is_gso(skb)) {
  1226. int mss = skb_shinfo(skb)->gso_size;
  1227. int hdrlen = skb_transport_header(skb) -
  1228. skb_mac_header(skb) +
  1229. tcp_hdrlen(skb);
  1230. skb_shinfo(skb)->gso_segs =
  1231. DIV_ROUND_UP(skb->len - hdrlen, mss);
  1232. }
  1233. queue->stats.rx_bytes += skb->len;
  1234. queue->stats.rx_packets++;
  1235. work_done++;
  1236. /* Set this flag right before netif_receive_skb, otherwise
  1237. * someone might think this packet already left netback, and
  1238. * do a skb_copy_ubufs while we are still in control of the
  1239. * skb. E.g. the __pskb_pull_tail earlier can do such thing.
  1240. */
  1241. if (skb_shinfo(skb)->destructor_arg) {
  1242. xenvif_skb_zerocopy_prepare(queue, skb);
  1243. queue->stats.tx_zerocopy_sent++;
  1244. }
  1245. netif_receive_skb(skb);
  1246. }
  1247. return work_done;
  1248. }
  1249. void xenvif_zerocopy_callback(struct ubuf_info *ubuf, bool zerocopy_success)
  1250. {
  1251. unsigned long flags;
  1252. pending_ring_idx_t index;
  1253. struct xenvif_queue *queue = ubuf_to_queue(ubuf);
  1254. /* This is the only place where we grab this lock, to protect callbacks
  1255. * from each other.
  1256. */
  1257. spin_lock_irqsave(&queue->callback_lock, flags);
  1258. do {
  1259. u16 pending_idx = ubuf->desc;
  1260. ubuf = (struct ubuf_info *) ubuf->ctx;
  1261. BUG_ON(queue->dealloc_prod - queue->dealloc_cons >=
  1262. MAX_PENDING_REQS);
  1263. index = pending_index(queue->dealloc_prod);
  1264. queue->dealloc_ring[index] = pending_idx;
  1265. /* Sync with xenvif_tx_dealloc_action:
  1266. * insert idx then incr producer.
  1267. */
  1268. smp_wmb();
  1269. queue->dealloc_prod++;
  1270. } while (ubuf);
  1271. wake_up(&queue->dealloc_wq);
  1272. spin_unlock_irqrestore(&queue->callback_lock, flags);
  1273. if (likely(zerocopy_success))
  1274. queue->stats.tx_zerocopy_success++;
  1275. else
  1276. queue->stats.tx_zerocopy_fail++;
  1277. xenvif_skb_zerocopy_complete(queue);
  1278. }
  1279. static inline void xenvif_tx_dealloc_action(struct xenvif_queue *queue)
  1280. {
  1281. struct gnttab_unmap_grant_ref *gop;
  1282. pending_ring_idx_t dc, dp;
  1283. u16 pending_idx, pending_idx_release[MAX_PENDING_REQS];
  1284. unsigned int i = 0;
  1285. dc = queue->dealloc_cons;
  1286. gop = queue->tx_unmap_ops;
  1287. /* Free up any grants we have finished using */
  1288. do {
  1289. dp = queue->dealloc_prod;
  1290. /* Ensure we see all indices enqueued by all
  1291. * xenvif_zerocopy_callback().
  1292. */
  1293. smp_rmb();
  1294. while (dc != dp) {
  1295. BUG_ON(gop - queue->tx_unmap_ops > MAX_PENDING_REQS);
  1296. pending_idx =
  1297. queue->dealloc_ring[pending_index(dc++)];
  1298. pending_idx_release[gop-queue->tx_unmap_ops] =
  1299. pending_idx;
  1300. queue->pages_to_unmap[gop-queue->tx_unmap_ops] =
  1301. queue->mmap_pages[pending_idx];
  1302. gnttab_set_unmap_op(gop,
  1303. idx_to_kaddr(queue, pending_idx),
  1304. GNTMAP_host_map,
  1305. queue->grant_tx_handle[pending_idx]);
  1306. xenvif_grant_handle_reset(queue, pending_idx);
  1307. ++gop;
  1308. }
  1309. } while (dp != queue->dealloc_prod);
  1310. queue->dealloc_cons = dc;
  1311. if (gop - queue->tx_unmap_ops > 0) {
  1312. int ret;
  1313. ret = gnttab_unmap_refs(queue->tx_unmap_ops,
  1314. NULL,
  1315. queue->pages_to_unmap,
  1316. gop - queue->tx_unmap_ops);
  1317. if (ret) {
  1318. netdev_err(queue->vif->dev, "Unmap fail: nr_ops %tx ret %d\n",
  1319. gop - queue->tx_unmap_ops, ret);
  1320. for (i = 0; i < gop - queue->tx_unmap_ops; ++i) {
  1321. if (gop[i].status != GNTST_okay)
  1322. netdev_err(queue->vif->dev,
  1323. " host_addr: %llx handle: %x status: %d\n",
  1324. gop[i].host_addr,
  1325. gop[i].handle,
  1326. gop[i].status);
  1327. }
  1328. BUG();
  1329. }
  1330. }
  1331. for (i = 0; i < gop - queue->tx_unmap_ops; ++i)
  1332. xenvif_idx_release(queue, pending_idx_release[i],
  1333. XEN_NETIF_RSP_OKAY);
  1334. }
  1335. /* Called after netfront has transmitted */
  1336. int xenvif_tx_action(struct xenvif_queue *queue, int budget)
  1337. {
  1338. unsigned nr_mops, nr_cops = 0;
  1339. int work_done, ret;
  1340. if (unlikely(!tx_work_todo(queue)))
  1341. return 0;
  1342. xenvif_tx_build_gops(queue, budget, &nr_cops, &nr_mops);
  1343. if (nr_cops == 0)
  1344. return 0;
  1345. gnttab_batch_copy(queue->tx_copy_ops, nr_cops);
  1346. if (nr_mops != 0) {
  1347. ret = gnttab_map_refs(queue->tx_map_ops,
  1348. NULL,
  1349. queue->pages_to_map,
  1350. nr_mops);
  1351. BUG_ON(ret);
  1352. }
  1353. work_done = xenvif_tx_submit(queue);
  1354. return work_done;
  1355. }
  1356. static void xenvif_idx_release(struct xenvif_queue *queue, u16 pending_idx,
  1357. u8 status)
  1358. {
  1359. struct pending_tx_info *pending_tx_info;
  1360. pending_ring_idx_t index;
  1361. unsigned long flags;
  1362. pending_tx_info = &queue->pending_tx_info[pending_idx];
  1363. spin_lock_irqsave(&queue->response_lock, flags);
  1364. make_tx_response(queue, &pending_tx_info->req, status);
  1365. index = pending_index(queue->pending_prod);
  1366. queue->pending_ring[index] = pending_idx;
  1367. /* TX shouldn't use the index before we give it back here */
  1368. mb();
  1369. queue->pending_prod++;
  1370. spin_unlock_irqrestore(&queue->response_lock, flags);
  1371. }
  1372. static void make_tx_response(struct xenvif_queue *queue,
  1373. struct xen_netif_tx_request *txp,
  1374. s8 st)
  1375. {
  1376. RING_IDX i = queue->tx.rsp_prod_pvt;
  1377. struct xen_netif_tx_response *resp;
  1378. int notify;
  1379. resp = RING_GET_RESPONSE(&queue->tx, i);
  1380. resp->id = txp->id;
  1381. resp->status = st;
  1382. if (txp->flags & XEN_NETTXF_extra_info)
  1383. RING_GET_RESPONSE(&queue->tx, ++i)->status = XEN_NETIF_RSP_NULL;
  1384. queue->tx.rsp_prod_pvt = ++i;
  1385. RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&queue->tx, notify);
  1386. if (notify)
  1387. notify_remote_via_irq(queue->tx_irq);
  1388. }
  1389. static struct xen_netif_rx_response *make_rx_response(struct xenvif_queue *queue,
  1390. u16 id,
  1391. s8 st,
  1392. u16 offset,
  1393. u16 size,
  1394. u16 flags)
  1395. {
  1396. RING_IDX i = queue->rx.rsp_prod_pvt;
  1397. struct xen_netif_rx_response *resp;
  1398. resp = RING_GET_RESPONSE(&queue->rx, i);
  1399. resp->offset = offset;
  1400. resp->flags = flags;
  1401. resp->id = id;
  1402. resp->status = (s16)size;
  1403. if (st < 0)
  1404. resp->status = (s16)st;
  1405. queue->rx.rsp_prod_pvt = ++i;
  1406. return resp;
  1407. }
  1408. void xenvif_idx_unmap(struct xenvif_queue *queue, u16 pending_idx)
  1409. {
  1410. int ret;
  1411. struct gnttab_unmap_grant_ref tx_unmap_op;
  1412. gnttab_set_unmap_op(&tx_unmap_op,
  1413. idx_to_kaddr(queue, pending_idx),
  1414. GNTMAP_host_map,
  1415. queue->grant_tx_handle[pending_idx]);
  1416. xenvif_grant_handle_reset(queue, pending_idx);
  1417. ret = gnttab_unmap_refs(&tx_unmap_op, NULL,
  1418. &queue->mmap_pages[pending_idx], 1);
  1419. if (ret) {
  1420. netdev_err(queue->vif->dev,
  1421. "Unmap fail: ret: %d pending_idx: %d host_addr: %llx handle: %x status: %d\n",
  1422. ret,
  1423. pending_idx,
  1424. tx_unmap_op.host_addr,
  1425. tx_unmap_op.handle,
  1426. tx_unmap_op.status);
  1427. BUG();
  1428. }
  1429. }
  1430. static inline int tx_work_todo(struct xenvif_queue *queue)
  1431. {
  1432. if (likely(RING_HAS_UNCONSUMED_REQUESTS(&queue->tx)))
  1433. return 1;
  1434. return 0;
  1435. }
  1436. static inline bool tx_dealloc_work_todo(struct xenvif_queue *queue)
  1437. {
  1438. return queue->dealloc_cons != queue->dealloc_prod;
  1439. }
  1440. void xenvif_unmap_frontend_rings(struct xenvif_queue *queue)
  1441. {
  1442. if (queue->tx.sring)
  1443. xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(queue->vif),
  1444. queue->tx.sring);
  1445. if (queue->rx.sring)
  1446. xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(queue->vif),
  1447. queue->rx.sring);
  1448. }
  1449. int xenvif_map_frontend_rings(struct xenvif_queue *queue,
  1450. grant_ref_t tx_ring_ref,
  1451. grant_ref_t rx_ring_ref)
  1452. {
  1453. void *addr;
  1454. struct xen_netif_tx_sring *txs;
  1455. struct xen_netif_rx_sring *rxs;
  1456. int err = -ENOMEM;
  1457. err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(queue->vif),
  1458. tx_ring_ref, &addr);
  1459. if (err)
  1460. goto err;
  1461. txs = (struct xen_netif_tx_sring *)addr;
  1462. BACK_RING_INIT(&queue->tx, txs, PAGE_SIZE);
  1463. err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(queue->vif),
  1464. rx_ring_ref, &addr);
  1465. if (err)
  1466. goto err;
  1467. rxs = (struct xen_netif_rx_sring *)addr;
  1468. BACK_RING_INIT(&queue->rx, rxs, PAGE_SIZE);
  1469. return 0;
  1470. err:
  1471. xenvif_unmap_frontend_rings(queue);
  1472. return err;
  1473. }
  1474. static void xenvif_queue_carrier_off(struct xenvif_queue *queue)
  1475. {
  1476. struct xenvif *vif = queue->vif;
  1477. queue->stalled = true;
  1478. /* At least one queue has stalled? Disable the carrier. */
  1479. spin_lock(&vif->lock);
  1480. if (vif->stalled_queues++ == 0) {
  1481. netdev_info(vif->dev, "Guest Rx stalled");
  1482. netif_carrier_off(vif->dev);
  1483. }
  1484. spin_unlock(&vif->lock);
  1485. }
  1486. static void xenvif_queue_carrier_on(struct xenvif_queue *queue)
  1487. {
  1488. struct xenvif *vif = queue->vif;
  1489. queue->last_rx_time = jiffies; /* Reset Rx stall detection. */
  1490. queue->stalled = false;
  1491. /* All queues are ready? Enable the carrier. */
  1492. spin_lock(&vif->lock);
  1493. if (--vif->stalled_queues == 0) {
  1494. netdev_info(vif->dev, "Guest Rx ready");
  1495. netif_carrier_on(vif->dev);
  1496. }
  1497. spin_unlock(&vif->lock);
  1498. }
  1499. static bool xenvif_rx_queue_stalled(struct xenvif_queue *queue)
  1500. {
  1501. RING_IDX prod, cons;
  1502. prod = queue->rx.sring->req_prod;
  1503. cons = queue->rx.req_cons;
  1504. return !queue->stalled
  1505. && prod - cons < XEN_NETBK_RX_SLOTS_MAX
  1506. && time_after(jiffies,
  1507. queue->last_rx_time + queue->vif->stall_timeout);
  1508. }
  1509. static bool xenvif_rx_queue_ready(struct xenvif_queue *queue)
  1510. {
  1511. RING_IDX prod, cons;
  1512. prod = queue->rx.sring->req_prod;
  1513. cons = queue->rx.req_cons;
  1514. return queue->stalled
  1515. && prod - cons >= XEN_NETBK_RX_SLOTS_MAX;
  1516. }
  1517. static bool xenvif_have_rx_work(struct xenvif_queue *queue)
  1518. {
  1519. return (!skb_queue_empty(&queue->rx_queue)
  1520. && xenvif_rx_ring_slots_available(queue, XEN_NETBK_RX_SLOTS_MAX))
  1521. || (queue->vif->stall_timeout &&
  1522. (xenvif_rx_queue_stalled(queue)
  1523. || xenvif_rx_queue_ready(queue)))
  1524. || kthread_should_stop()
  1525. || queue->vif->disabled;
  1526. }
  1527. static long xenvif_rx_queue_timeout(struct xenvif_queue *queue)
  1528. {
  1529. struct sk_buff *skb;
  1530. long timeout;
  1531. skb = skb_peek(&queue->rx_queue);
  1532. if (!skb)
  1533. return MAX_SCHEDULE_TIMEOUT;
  1534. timeout = XENVIF_RX_CB(skb)->expires - jiffies;
  1535. return timeout < 0 ? 0 : timeout;
  1536. }
  1537. /* Wait until the guest Rx thread has work.
  1538. *
  1539. * The timeout needs to be adjusted based on the current head of the
  1540. * queue (and not just the head at the beginning). In particular, if
  1541. * the queue is initially empty an infinite timeout is used and this
  1542. * needs to be reduced when a skb is queued.
  1543. *
  1544. * This cannot be done with wait_event_timeout() because it only
  1545. * calculates the timeout once.
  1546. */
  1547. static void xenvif_wait_for_rx_work(struct xenvif_queue *queue)
  1548. {
  1549. DEFINE_WAIT(wait);
  1550. if (xenvif_have_rx_work(queue))
  1551. return;
  1552. for (;;) {
  1553. long ret;
  1554. prepare_to_wait(&queue->wq, &wait, TASK_INTERRUPTIBLE);
  1555. if (xenvif_have_rx_work(queue))
  1556. break;
  1557. ret = schedule_timeout(xenvif_rx_queue_timeout(queue));
  1558. if (!ret)
  1559. break;
  1560. }
  1561. finish_wait(&queue->wq, &wait);
  1562. }
  1563. int xenvif_kthread_guest_rx(void *data)
  1564. {
  1565. struct xenvif_queue *queue = data;
  1566. struct xenvif *vif = queue->vif;
  1567. if (!vif->stall_timeout)
  1568. xenvif_queue_carrier_on(queue);
  1569. for (;;) {
  1570. xenvif_wait_for_rx_work(queue);
  1571. if (kthread_should_stop())
  1572. break;
  1573. /* This frontend is found to be rogue, disable it in
  1574. * kthread context. Currently this is only set when
  1575. * netback finds out frontend sends malformed packet,
  1576. * but we cannot disable the interface in softirq
  1577. * context so we defer it here, if this thread is
  1578. * associated with queue 0.
  1579. */
  1580. if (unlikely(vif->disabled && queue->id == 0)) {
  1581. xenvif_carrier_off(vif);
  1582. break;
  1583. }
  1584. if (!skb_queue_empty(&queue->rx_queue))
  1585. xenvif_rx_action(queue);
  1586. /* If the guest hasn't provided any Rx slots for a
  1587. * while it's probably not responsive, drop the
  1588. * carrier so packets are dropped earlier.
  1589. */
  1590. if (vif->stall_timeout) {
  1591. if (xenvif_rx_queue_stalled(queue))
  1592. xenvif_queue_carrier_off(queue);
  1593. else if (xenvif_rx_queue_ready(queue))
  1594. xenvif_queue_carrier_on(queue);
  1595. }
  1596. /* Queued packets may have foreign pages from other
  1597. * domains. These cannot be queued indefinitely as
  1598. * this would starve guests of grant refs and transmit
  1599. * slots.
  1600. */
  1601. xenvif_rx_queue_drop_expired(queue);
  1602. xenvif_rx_queue_maybe_wake(queue);
  1603. cond_resched();
  1604. }
  1605. /* Bin any remaining skbs */
  1606. xenvif_rx_queue_purge(queue);
  1607. return 0;
  1608. }
  1609. static bool xenvif_dealloc_kthread_should_stop(struct xenvif_queue *queue)
  1610. {
  1611. /* Dealloc thread must remain running until all inflight
  1612. * packets complete.
  1613. */
  1614. return kthread_should_stop() &&
  1615. !atomic_read(&queue->inflight_packets);
  1616. }
  1617. int xenvif_dealloc_kthread(void *data)
  1618. {
  1619. struct xenvif_queue *queue = data;
  1620. for (;;) {
  1621. wait_event_interruptible(queue->dealloc_wq,
  1622. tx_dealloc_work_todo(queue) ||
  1623. xenvif_dealloc_kthread_should_stop(queue));
  1624. if (xenvif_dealloc_kthread_should_stop(queue))
  1625. break;
  1626. xenvif_tx_dealloc_action(queue);
  1627. cond_resched();
  1628. }
  1629. /* Unmap anything remaining*/
  1630. if (tx_dealloc_work_todo(queue))
  1631. xenvif_tx_dealloc_action(queue);
  1632. return 0;
  1633. }
  1634. static int __init netback_init(void)
  1635. {
  1636. int rc = 0;
  1637. if (!xen_domain())
  1638. return -ENODEV;
  1639. /* Allow as many queues as there are CPUs, by default */
  1640. xenvif_max_queues = num_online_cpus();
  1641. if (fatal_skb_slots < XEN_NETBK_LEGACY_SLOTS_MAX) {
  1642. pr_info("fatal_skb_slots too small (%d), bump it to XEN_NETBK_LEGACY_SLOTS_MAX (%d)\n",
  1643. fatal_skb_slots, XEN_NETBK_LEGACY_SLOTS_MAX);
  1644. fatal_skb_slots = XEN_NETBK_LEGACY_SLOTS_MAX;
  1645. }
  1646. rc = xenvif_xenbus_init();
  1647. if (rc)
  1648. goto failed_init;
  1649. #ifdef CONFIG_DEBUG_FS
  1650. xen_netback_dbg_root = debugfs_create_dir("xen-netback", NULL);
  1651. if (IS_ERR_OR_NULL(xen_netback_dbg_root))
  1652. pr_warn("Init of debugfs returned %ld!\n",
  1653. PTR_ERR(xen_netback_dbg_root));
  1654. #endif /* CONFIG_DEBUG_FS */
  1655. return 0;
  1656. failed_init:
  1657. return rc;
  1658. }
  1659. module_init(netback_init);
  1660. static void __exit netback_fini(void)
  1661. {
  1662. #ifdef CONFIG_DEBUG_FS
  1663. if (!IS_ERR_OR_NULL(xen_netback_dbg_root))
  1664. debugfs_remove_recursive(xen_netback_dbg_root);
  1665. #endif /* CONFIG_DEBUG_FS */
  1666. xenvif_xenbus_fini();
  1667. }
  1668. module_exit(netback_fini);
  1669. MODULE_LICENSE("Dual BSD/GPL");
  1670. MODULE_ALIAS("xen-backend:vif");