netback.c 57 KB

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