netback.c 57 KB

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