netback.c 57 KB

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