netback.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599
  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 <net/tcp.h>
  39. #include <xen/xen.h>
  40. #include <xen/events.h>
  41. #include <xen/interface/memory.h>
  42. #include <asm/xen/hypercall.h>
  43. #include <asm/xen/page.h>
  44. /* Provide an option to disable split event channels at load time as
  45. * event channels are limited resource. Split event channels are
  46. * enabled by default.
  47. */
  48. bool separate_tx_rx_irq = 1;
  49. module_param(separate_tx_rx_irq, bool, 0644);
  50. /*
  51. * This is the maximum slots a skb can have. If a guest sends a skb
  52. * which exceeds this limit it is considered malicious.
  53. */
  54. #define FATAL_SKB_SLOTS_DEFAULT 20
  55. static unsigned int fatal_skb_slots = FATAL_SKB_SLOTS_DEFAULT;
  56. module_param(fatal_skb_slots, uint, 0444);
  57. /*
  58. * If head != INVALID_PENDING_RING_IDX, it means this tx request is head of
  59. * one or more merged tx requests, otherwise it is the continuation of
  60. * previous tx request.
  61. */
  62. static inline int pending_tx_is_head(struct xenvif *vif, RING_IDX idx)
  63. {
  64. return vif->pending_tx_info[idx].head != INVALID_PENDING_RING_IDX;
  65. }
  66. static void xenvif_idx_release(struct xenvif *vif, u16 pending_idx,
  67. u8 status);
  68. static void make_tx_response(struct xenvif *vif,
  69. struct xen_netif_tx_request *txp,
  70. s8 st);
  71. static inline int tx_work_todo(struct xenvif *vif);
  72. static inline int rx_work_todo(struct xenvif *vif);
  73. static struct xen_netif_rx_response *make_rx_response(struct xenvif *vif,
  74. u16 id,
  75. s8 st,
  76. u16 offset,
  77. u16 size,
  78. u16 flags);
  79. static inline unsigned long idx_to_pfn(struct xenvif *vif,
  80. u16 idx)
  81. {
  82. return page_to_pfn(vif->mmap_pages[idx]);
  83. }
  84. static inline unsigned long idx_to_kaddr(struct xenvif *vif,
  85. u16 idx)
  86. {
  87. return (unsigned long)pfn_to_kaddr(idx_to_pfn(vif, idx));
  88. }
  89. /* This is a miniumum size for the linear area to avoid lots of
  90. * calls to __pskb_pull_tail() as we set up checksum offsets. The
  91. * value 128 was chosen as it covers all IPv4 and most likely
  92. * IPv6 headers.
  93. */
  94. #define PKT_PROT_LEN 128
  95. static u16 frag_get_pending_idx(skb_frag_t *frag)
  96. {
  97. return (u16)frag->page_offset;
  98. }
  99. static void frag_set_pending_idx(skb_frag_t *frag, u16 pending_idx)
  100. {
  101. frag->page_offset = pending_idx;
  102. }
  103. static inline pending_ring_idx_t pending_index(unsigned i)
  104. {
  105. return i & (MAX_PENDING_REQS-1);
  106. }
  107. bool xenvif_rx_ring_slots_available(struct xenvif *vif, int needed)
  108. {
  109. RING_IDX prod, cons;
  110. do {
  111. prod = vif->rx.sring->req_prod;
  112. cons = vif->rx.req_cons;
  113. if (prod - cons >= needed)
  114. return true;
  115. vif->rx.sring->req_event = prod + 1;
  116. /* Make sure event is visible before we check prod
  117. * again.
  118. */
  119. mb();
  120. } while (vif->rx.sring->req_prod != prod);
  121. return false;
  122. }
  123. /*
  124. * Returns true if we should start a new receive buffer instead of
  125. * adding 'size' bytes to a buffer which currently contains 'offset'
  126. * bytes.
  127. */
  128. static bool start_new_rx_buffer(int offset, unsigned long size, int head)
  129. {
  130. /* simple case: we have completely filled the current buffer. */
  131. if (offset == MAX_BUFFER_OFFSET)
  132. return true;
  133. /*
  134. * complex case: start a fresh buffer if the current frag
  135. * would overflow the current buffer but only if:
  136. * (i) this frag would fit completely in the next buffer
  137. * and (ii) there is already some data in the current buffer
  138. * and (iii) this is not the head buffer.
  139. *
  140. * Where:
  141. * - (i) stops us splitting a frag into two copies
  142. * unless the frag is too large for a single buffer.
  143. * - (ii) stops us from leaving a buffer pointlessly empty.
  144. * - (iii) stops us leaving the first buffer
  145. * empty. Strictly speaking this is already covered
  146. * by (ii) but is explicitly checked because
  147. * netfront relies on the first buffer being
  148. * non-empty and can crash otherwise.
  149. *
  150. * This means we will effectively linearise small
  151. * frags but do not needlessly split large buffers
  152. * into multiple copies tend to give large frags their
  153. * own buffers as before.
  154. */
  155. if ((offset + size > MAX_BUFFER_OFFSET) &&
  156. (size <= MAX_BUFFER_OFFSET) && offset && !head)
  157. return true;
  158. return false;
  159. }
  160. struct netrx_pending_operations {
  161. unsigned copy_prod, copy_cons;
  162. unsigned meta_prod, meta_cons;
  163. struct gnttab_copy *copy;
  164. struct xenvif_rx_meta *meta;
  165. int copy_off;
  166. grant_ref_t copy_gref;
  167. };
  168. static struct xenvif_rx_meta *get_next_rx_buffer(struct xenvif *vif,
  169. struct netrx_pending_operations *npo)
  170. {
  171. struct xenvif_rx_meta *meta;
  172. struct xen_netif_rx_request *req;
  173. req = RING_GET_REQUEST(&vif->rx, vif->rx.req_cons++);
  174. meta = npo->meta + npo->meta_prod++;
  175. meta->gso_type = XEN_NETIF_GSO_TYPE_NONE;
  176. meta->gso_size = 0;
  177. meta->size = 0;
  178. meta->id = req->id;
  179. npo->copy_off = 0;
  180. npo->copy_gref = req->gref;
  181. return meta;
  182. }
  183. /*
  184. * Set up the grant operations for this fragment. If it's a flipping
  185. * interface, we also set up the unmap request from here.
  186. */
  187. static void xenvif_gop_frag_copy(struct xenvif *vif, struct sk_buff *skb,
  188. struct netrx_pending_operations *npo,
  189. struct page *page, unsigned long size,
  190. unsigned long offset, int *head)
  191. {
  192. struct gnttab_copy *copy_gop;
  193. struct xenvif_rx_meta *meta;
  194. unsigned long bytes;
  195. int gso_type;
  196. /* Data must not cross a page boundary. */
  197. BUG_ON(size + offset > PAGE_SIZE<<compound_order(page));
  198. meta = npo->meta + npo->meta_prod - 1;
  199. /* Skip unused frames from start of page */
  200. page += offset >> PAGE_SHIFT;
  201. offset &= ~PAGE_MASK;
  202. while (size > 0) {
  203. BUG_ON(offset >= PAGE_SIZE);
  204. BUG_ON(npo->copy_off > MAX_BUFFER_OFFSET);
  205. bytes = PAGE_SIZE - offset;
  206. if (bytes > size)
  207. bytes = size;
  208. if (start_new_rx_buffer(npo->copy_off, bytes, *head)) {
  209. /*
  210. * Netfront requires there to be some data in the head
  211. * buffer.
  212. */
  213. BUG_ON(*head);
  214. meta = get_next_rx_buffer(vif, npo);
  215. }
  216. if (npo->copy_off + bytes > MAX_BUFFER_OFFSET)
  217. bytes = MAX_BUFFER_OFFSET - npo->copy_off;
  218. copy_gop = npo->copy + npo->copy_prod++;
  219. copy_gop->flags = GNTCOPY_dest_gref;
  220. copy_gop->len = bytes;
  221. copy_gop->source.domid = DOMID_SELF;
  222. copy_gop->source.u.gmfn = virt_to_mfn(page_address(page));
  223. copy_gop->source.offset = offset;
  224. copy_gop->dest.domid = vif->domid;
  225. copy_gop->dest.offset = npo->copy_off;
  226. copy_gop->dest.u.ref = npo->copy_gref;
  227. npo->copy_off += bytes;
  228. meta->size += bytes;
  229. offset += bytes;
  230. size -= bytes;
  231. /* Next frame */
  232. if (offset == PAGE_SIZE && size) {
  233. BUG_ON(!PageCompound(page));
  234. page++;
  235. offset = 0;
  236. }
  237. /* Leave a gap for the GSO descriptor. */
  238. if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
  239. gso_type = XEN_NETIF_GSO_TYPE_TCPV4;
  240. else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
  241. gso_type = XEN_NETIF_GSO_TYPE_TCPV6;
  242. else
  243. gso_type = XEN_NETIF_GSO_TYPE_NONE;
  244. if (*head && ((1 << gso_type) & vif->gso_mask))
  245. vif->rx.req_cons++;
  246. *head = 0; /* There must be something in this buffer now. */
  247. }
  248. }
  249. /*
  250. * Prepare an SKB to be transmitted to the frontend.
  251. *
  252. * This function is responsible for allocating grant operations, meta
  253. * structures, etc.
  254. *
  255. * It returns the number of meta structures consumed. The number of
  256. * ring slots used is always equal to the number of meta slots used
  257. * plus the number of GSO descriptors used. Currently, we use either
  258. * zero GSO descriptors (for non-GSO packets) or one descriptor (for
  259. * frontend-side LRO).
  260. */
  261. static int xenvif_gop_skb(struct sk_buff *skb,
  262. struct netrx_pending_operations *npo)
  263. {
  264. struct xenvif *vif = netdev_priv(skb->dev);
  265. int nr_frags = skb_shinfo(skb)->nr_frags;
  266. int i;
  267. struct xen_netif_rx_request *req;
  268. struct xenvif_rx_meta *meta;
  269. unsigned char *data;
  270. int head = 1;
  271. int old_meta_prod;
  272. int gso_type;
  273. int gso_size;
  274. old_meta_prod = npo->meta_prod;
  275. if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4) {
  276. gso_type = XEN_NETIF_GSO_TYPE_TCPV4;
  277. gso_size = skb_shinfo(skb)->gso_size;
  278. } else if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6) {
  279. gso_type = XEN_NETIF_GSO_TYPE_TCPV6;
  280. gso_size = skb_shinfo(skb)->gso_size;
  281. } else {
  282. gso_type = XEN_NETIF_GSO_TYPE_NONE;
  283. gso_size = 0;
  284. }
  285. /* Set up a GSO prefix descriptor, if necessary */
  286. if ((1 << gso_type) & vif->gso_prefix_mask) {
  287. req = RING_GET_REQUEST(&vif->rx, vif->rx.req_cons++);
  288. meta = npo->meta + npo->meta_prod++;
  289. meta->gso_type = gso_type;
  290. meta->gso_size = gso_size;
  291. meta->size = 0;
  292. meta->id = req->id;
  293. }
  294. req = RING_GET_REQUEST(&vif->rx, vif->rx.req_cons++);
  295. meta = npo->meta + npo->meta_prod++;
  296. if ((1 << gso_type) & vif->gso_mask) {
  297. meta->gso_type = gso_type;
  298. meta->gso_size = gso_size;
  299. } else {
  300. meta->gso_type = XEN_NETIF_GSO_TYPE_NONE;
  301. meta->gso_size = 0;
  302. }
  303. meta->size = 0;
  304. meta->id = req->id;
  305. npo->copy_off = 0;
  306. npo->copy_gref = req->gref;
  307. data = skb->data;
  308. while (data < skb_tail_pointer(skb)) {
  309. unsigned int offset = offset_in_page(data);
  310. unsigned int len = PAGE_SIZE - offset;
  311. if (data + len > skb_tail_pointer(skb))
  312. len = skb_tail_pointer(skb) - data;
  313. xenvif_gop_frag_copy(vif, skb, npo,
  314. virt_to_page(data), len, offset, &head);
  315. data += len;
  316. }
  317. for (i = 0; i < nr_frags; i++) {
  318. xenvif_gop_frag_copy(vif, skb, npo,
  319. skb_frag_page(&skb_shinfo(skb)->frags[i]),
  320. skb_frag_size(&skb_shinfo(skb)->frags[i]),
  321. skb_shinfo(skb)->frags[i].page_offset,
  322. &head);
  323. }
  324. return npo->meta_prod - old_meta_prod;
  325. }
  326. /*
  327. * This is a twin to xenvif_gop_skb. Assume that xenvif_gop_skb was
  328. * used to set up the operations on the top of
  329. * netrx_pending_operations, which have since been done. Check that
  330. * they didn't give any errors and advance over them.
  331. */
  332. static int xenvif_check_gop(struct xenvif *vif, int nr_meta_slots,
  333. struct netrx_pending_operations *npo)
  334. {
  335. struct gnttab_copy *copy_op;
  336. int status = XEN_NETIF_RSP_OKAY;
  337. int i;
  338. for (i = 0; i < nr_meta_slots; i++) {
  339. copy_op = npo->copy + npo->copy_cons++;
  340. if (copy_op->status != GNTST_okay) {
  341. netdev_dbg(vif->dev,
  342. "Bad status %d from copy to DOM%d.\n",
  343. copy_op->status, vif->domid);
  344. status = XEN_NETIF_RSP_ERROR;
  345. }
  346. }
  347. return status;
  348. }
  349. static void xenvif_add_frag_responses(struct xenvif *vif, int status,
  350. struct xenvif_rx_meta *meta,
  351. int nr_meta_slots)
  352. {
  353. int i;
  354. unsigned long offset;
  355. /* No fragments used */
  356. if (nr_meta_slots <= 1)
  357. return;
  358. nr_meta_slots--;
  359. for (i = 0; i < nr_meta_slots; i++) {
  360. int flags;
  361. if (i == nr_meta_slots - 1)
  362. flags = 0;
  363. else
  364. flags = XEN_NETRXF_more_data;
  365. offset = 0;
  366. make_rx_response(vif, meta[i].id, status, offset,
  367. meta[i].size, flags);
  368. }
  369. }
  370. struct xenvif_rx_cb {
  371. int meta_slots_used;
  372. };
  373. #define XENVIF_RX_CB(skb) ((struct xenvif_rx_cb *)(skb)->cb)
  374. void xenvif_kick_thread(struct xenvif *vif)
  375. {
  376. wake_up(&vif->wq);
  377. }
  378. static void xenvif_rx_action(struct xenvif *vif)
  379. {
  380. s8 status;
  381. u16 flags;
  382. struct xen_netif_rx_response *resp;
  383. struct sk_buff_head rxq;
  384. struct sk_buff *skb;
  385. LIST_HEAD(notify);
  386. int ret;
  387. unsigned long offset;
  388. bool need_to_notify = false;
  389. struct netrx_pending_operations npo = {
  390. .copy = vif->grant_copy_op,
  391. .meta = vif->meta,
  392. };
  393. skb_queue_head_init(&rxq);
  394. while ((skb = skb_dequeue(&vif->rx_queue)) != NULL) {
  395. RING_IDX max_slots_needed;
  396. int i;
  397. /* We need a cheap worse case estimate for the number of
  398. * slots we'll use.
  399. */
  400. max_slots_needed = DIV_ROUND_UP(offset_in_page(skb->data) +
  401. skb_headlen(skb),
  402. PAGE_SIZE);
  403. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  404. unsigned int size;
  405. size = skb_frag_size(&skb_shinfo(skb)->frags[i]);
  406. max_slots_needed += DIV_ROUND_UP(size, PAGE_SIZE);
  407. }
  408. if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4 ||
  409. skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
  410. max_slots_needed++;
  411. /* If the skb may not fit then bail out now */
  412. if (!xenvif_rx_ring_slots_available(vif, max_slots_needed)) {
  413. skb_queue_head(&vif->rx_queue, skb);
  414. need_to_notify = true;
  415. vif->rx_last_skb_slots = max_slots_needed;
  416. break;
  417. } else
  418. vif->rx_last_skb_slots = 0;
  419. XENVIF_RX_CB(skb)->meta_slots_used = xenvif_gop_skb(skb, &npo);
  420. BUG_ON(XENVIF_RX_CB(skb)->meta_slots_used > max_slots_needed);
  421. __skb_queue_tail(&rxq, skb);
  422. }
  423. BUG_ON(npo.meta_prod > ARRAY_SIZE(vif->meta));
  424. if (!npo.copy_prod)
  425. goto done;
  426. BUG_ON(npo.copy_prod > MAX_GRANT_COPY_OPS);
  427. gnttab_batch_copy(vif->grant_copy_op, npo.copy_prod);
  428. while ((skb = __skb_dequeue(&rxq)) != NULL) {
  429. if ((1 << vif->meta[npo.meta_cons].gso_type) &
  430. vif->gso_prefix_mask) {
  431. resp = RING_GET_RESPONSE(&vif->rx,
  432. vif->rx.rsp_prod_pvt++);
  433. resp->flags = XEN_NETRXF_gso_prefix | XEN_NETRXF_more_data;
  434. resp->offset = vif->meta[npo.meta_cons].gso_size;
  435. resp->id = vif->meta[npo.meta_cons].id;
  436. resp->status = XENVIF_RX_CB(skb)->meta_slots_used;
  437. npo.meta_cons++;
  438. XENVIF_RX_CB(skb)->meta_slots_used--;
  439. }
  440. vif->dev->stats.tx_bytes += skb->len;
  441. vif->dev->stats.tx_packets++;
  442. status = xenvif_check_gop(vif,
  443. XENVIF_RX_CB(skb)->meta_slots_used,
  444. &npo);
  445. if (XENVIF_RX_CB(skb)->meta_slots_used == 1)
  446. flags = 0;
  447. else
  448. flags = XEN_NETRXF_more_data;
  449. if (skb->ip_summed == CHECKSUM_PARTIAL) /* local packet? */
  450. flags |= XEN_NETRXF_csum_blank | XEN_NETRXF_data_validated;
  451. else if (skb->ip_summed == CHECKSUM_UNNECESSARY)
  452. /* remote but checksummed. */
  453. flags |= XEN_NETRXF_data_validated;
  454. offset = 0;
  455. resp = make_rx_response(vif, vif->meta[npo.meta_cons].id,
  456. status, offset,
  457. vif->meta[npo.meta_cons].size,
  458. flags);
  459. if ((1 << vif->meta[npo.meta_cons].gso_type) &
  460. vif->gso_mask) {
  461. struct xen_netif_extra_info *gso =
  462. (struct xen_netif_extra_info *)
  463. RING_GET_RESPONSE(&vif->rx,
  464. vif->rx.rsp_prod_pvt++);
  465. resp->flags |= XEN_NETRXF_extra_info;
  466. gso->u.gso.type = vif->meta[npo.meta_cons].gso_type;
  467. gso->u.gso.size = vif->meta[npo.meta_cons].gso_size;
  468. gso->u.gso.pad = 0;
  469. gso->u.gso.features = 0;
  470. gso->type = XEN_NETIF_EXTRA_TYPE_GSO;
  471. gso->flags = 0;
  472. }
  473. xenvif_add_frag_responses(vif, status,
  474. vif->meta + npo.meta_cons + 1,
  475. XENVIF_RX_CB(skb)->meta_slots_used);
  476. RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&vif->rx, ret);
  477. need_to_notify |= !!ret;
  478. npo.meta_cons += XENVIF_RX_CB(skb)->meta_slots_used;
  479. dev_kfree_skb(skb);
  480. }
  481. done:
  482. if (need_to_notify)
  483. notify_remote_via_irq(vif->rx_irq);
  484. }
  485. void xenvif_check_rx_xenvif(struct xenvif *vif)
  486. {
  487. int more_to_do;
  488. RING_FINAL_CHECK_FOR_REQUESTS(&vif->tx, more_to_do);
  489. if (more_to_do)
  490. napi_schedule(&vif->napi);
  491. }
  492. static void tx_add_credit(struct xenvif *vif)
  493. {
  494. unsigned long max_burst, max_credit;
  495. /*
  496. * Allow a burst big enough to transmit a jumbo packet of up to 128kB.
  497. * Otherwise the interface can seize up due to insufficient credit.
  498. */
  499. max_burst = RING_GET_REQUEST(&vif->tx, vif->tx.req_cons)->size;
  500. max_burst = min(max_burst, 131072UL);
  501. max_burst = max(max_burst, vif->credit_bytes);
  502. /* Take care that adding a new chunk of credit doesn't wrap to zero. */
  503. max_credit = vif->remaining_credit + vif->credit_bytes;
  504. if (max_credit < vif->remaining_credit)
  505. max_credit = ULONG_MAX; /* wrapped: clamp to ULONG_MAX */
  506. vif->remaining_credit = min(max_credit, max_burst);
  507. }
  508. static void tx_credit_callback(unsigned long data)
  509. {
  510. struct xenvif *vif = (struct xenvif *)data;
  511. tx_add_credit(vif);
  512. xenvif_check_rx_xenvif(vif);
  513. }
  514. static void xenvif_tx_err(struct xenvif *vif,
  515. struct xen_netif_tx_request *txp, RING_IDX end)
  516. {
  517. RING_IDX cons = vif->tx.req_cons;
  518. do {
  519. make_tx_response(vif, txp, XEN_NETIF_RSP_ERROR);
  520. if (cons == end)
  521. break;
  522. txp = RING_GET_REQUEST(&vif->tx, cons++);
  523. } while (1);
  524. vif->tx.req_cons = cons;
  525. }
  526. static void xenvif_fatal_tx_err(struct xenvif *vif)
  527. {
  528. netdev_err(vif->dev, "fatal error; disabling device\n");
  529. xenvif_carrier_off(vif);
  530. }
  531. static int xenvif_count_requests(struct xenvif *vif,
  532. struct xen_netif_tx_request *first,
  533. struct xen_netif_tx_request *txp,
  534. int work_to_do)
  535. {
  536. RING_IDX cons = vif->tx.req_cons;
  537. int slots = 0;
  538. int drop_err = 0;
  539. int more_data;
  540. if (!(first->flags & XEN_NETTXF_more_data))
  541. return 0;
  542. do {
  543. struct xen_netif_tx_request dropped_tx = { 0 };
  544. if (slots >= work_to_do) {
  545. netdev_err(vif->dev,
  546. "Asked for %d slots but exceeds this limit\n",
  547. work_to_do);
  548. xenvif_fatal_tx_err(vif);
  549. return -ENODATA;
  550. }
  551. /* This guest is really using too many slots and
  552. * considered malicious.
  553. */
  554. if (unlikely(slots >= fatal_skb_slots)) {
  555. netdev_err(vif->dev,
  556. "Malicious frontend using %d slots, threshold %u\n",
  557. slots, fatal_skb_slots);
  558. xenvif_fatal_tx_err(vif);
  559. return -E2BIG;
  560. }
  561. /* Xen network protocol had implicit dependency on
  562. * MAX_SKB_FRAGS. XEN_NETBK_LEGACY_SLOTS_MAX is set to
  563. * the historical MAX_SKB_FRAGS value 18 to honor the
  564. * same behavior as before. Any packet using more than
  565. * 18 slots but less than fatal_skb_slots slots is
  566. * dropped
  567. */
  568. if (!drop_err && slots >= XEN_NETBK_LEGACY_SLOTS_MAX) {
  569. if (net_ratelimit())
  570. netdev_dbg(vif->dev,
  571. "Too many slots (%d) exceeding limit (%d), dropping packet\n",
  572. slots, XEN_NETBK_LEGACY_SLOTS_MAX);
  573. drop_err = -E2BIG;
  574. }
  575. if (drop_err)
  576. txp = &dropped_tx;
  577. memcpy(txp, RING_GET_REQUEST(&vif->tx, cons + slots),
  578. sizeof(*txp));
  579. /* If the guest submitted a frame >= 64 KiB then
  580. * first->size overflowed and following slots will
  581. * appear to be larger than the frame.
  582. *
  583. * This cannot be fatal error as there are buggy
  584. * frontends that do this.
  585. *
  586. * Consume all slots and drop the packet.
  587. */
  588. if (!drop_err && txp->size > first->size) {
  589. if (net_ratelimit())
  590. netdev_dbg(vif->dev,
  591. "Invalid tx request, slot size %u > remaining size %u\n",
  592. txp->size, first->size);
  593. drop_err = -EIO;
  594. }
  595. first->size -= txp->size;
  596. slots++;
  597. if (unlikely((txp->offset + txp->size) > PAGE_SIZE)) {
  598. netdev_err(vif->dev, "Cross page boundary, txp->offset: %x, size: %u\n",
  599. txp->offset, txp->size);
  600. xenvif_fatal_tx_err(vif);
  601. return -EINVAL;
  602. }
  603. more_data = txp->flags & XEN_NETTXF_more_data;
  604. if (!drop_err)
  605. txp++;
  606. } while (more_data);
  607. if (drop_err) {
  608. xenvif_tx_err(vif, first, cons + slots);
  609. return drop_err;
  610. }
  611. return slots;
  612. }
  613. static struct page *xenvif_alloc_page(struct xenvif *vif,
  614. u16 pending_idx)
  615. {
  616. struct page *page;
  617. page = alloc_page(GFP_ATOMIC|__GFP_COLD);
  618. if (!page)
  619. return NULL;
  620. vif->mmap_pages[pending_idx] = page;
  621. return page;
  622. }
  623. struct xenvif_tx_cb {
  624. u16 pending_idx;
  625. };
  626. #define XENVIF_TX_CB(skb) ((struct xenvif_tx_cb *)(skb)->cb)
  627. static struct gnttab_copy *xenvif_get_requests(struct xenvif *vif,
  628. struct sk_buff *skb,
  629. struct xen_netif_tx_request *txp,
  630. struct gnttab_copy *gop)
  631. {
  632. struct skb_shared_info *shinfo = skb_shinfo(skb);
  633. skb_frag_t *frags = shinfo->frags;
  634. u16 pending_idx = XENVIF_TX_CB(skb)->pending_idx;
  635. u16 head_idx = 0;
  636. int slot, start;
  637. struct page *page;
  638. pending_ring_idx_t index, start_idx = 0;
  639. uint16_t dst_offset;
  640. unsigned int nr_slots;
  641. struct pending_tx_info *first = NULL;
  642. /* At this point shinfo->nr_frags is in fact the number of
  643. * slots, which can be as large as XEN_NETBK_LEGACY_SLOTS_MAX.
  644. */
  645. nr_slots = shinfo->nr_frags;
  646. /* Skip first skb fragment if it is on same page as header fragment. */
  647. start = (frag_get_pending_idx(&shinfo->frags[0]) == pending_idx);
  648. /* Coalesce tx requests, at this point the packet passed in
  649. * should be <= 64K. Any packets larger than 64K have been
  650. * handled in xenvif_count_requests().
  651. */
  652. for (shinfo->nr_frags = slot = start; slot < nr_slots;
  653. shinfo->nr_frags++) {
  654. struct pending_tx_info *pending_tx_info =
  655. vif->pending_tx_info;
  656. page = alloc_page(GFP_ATOMIC|__GFP_COLD);
  657. if (!page)
  658. goto err;
  659. dst_offset = 0;
  660. first = NULL;
  661. while (dst_offset < PAGE_SIZE && slot < nr_slots) {
  662. gop->flags = GNTCOPY_source_gref;
  663. gop->source.u.ref = txp->gref;
  664. gop->source.domid = vif->domid;
  665. gop->source.offset = txp->offset;
  666. gop->dest.domid = DOMID_SELF;
  667. gop->dest.offset = dst_offset;
  668. gop->dest.u.gmfn = virt_to_mfn(page_address(page));
  669. if (dst_offset + txp->size > PAGE_SIZE) {
  670. /* This page can only merge a portion
  671. * of tx request. Do not increment any
  672. * pointer / counter here. The txp
  673. * will be dealt with in future
  674. * rounds, eventually hitting the
  675. * `else` branch.
  676. */
  677. gop->len = PAGE_SIZE - dst_offset;
  678. txp->offset += gop->len;
  679. txp->size -= gop->len;
  680. dst_offset += gop->len; /* quit loop */
  681. } else {
  682. /* This tx request can be merged in the page */
  683. gop->len = txp->size;
  684. dst_offset += gop->len;
  685. index = pending_index(vif->pending_cons++);
  686. pending_idx = vif->pending_ring[index];
  687. memcpy(&pending_tx_info[pending_idx].req, txp,
  688. sizeof(*txp));
  689. /* Poison these fields, corresponding
  690. * fields for head tx req will be set
  691. * to correct values after the loop.
  692. */
  693. vif->mmap_pages[pending_idx] = (void *)(~0UL);
  694. pending_tx_info[pending_idx].head =
  695. INVALID_PENDING_RING_IDX;
  696. if (!first) {
  697. first = &pending_tx_info[pending_idx];
  698. start_idx = index;
  699. head_idx = pending_idx;
  700. }
  701. txp++;
  702. slot++;
  703. }
  704. gop++;
  705. }
  706. first->req.offset = 0;
  707. first->req.size = dst_offset;
  708. first->head = start_idx;
  709. vif->mmap_pages[head_idx] = page;
  710. frag_set_pending_idx(&frags[shinfo->nr_frags], head_idx);
  711. }
  712. BUG_ON(shinfo->nr_frags > MAX_SKB_FRAGS);
  713. return gop;
  714. err:
  715. /* Unwind, freeing all pages and sending error responses. */
  716. while (shinfo->nr_frags-- > start) {
  717. xenvif_idx_release(vif,
  718. frag_get_pending_idx(&frags[shinfo->nr_frags]),
  719. XEN_NETIF_RSP_ERROR);
  720. }
  721. /* The head too, if necessary. */
  722. if (start)
  723. xenvif_idx_release(vif, pending_idx, XEN_NETIF_RSP_ERROR);
  724. return NULL;
  725. }
  726. static int xenvif_tx_check_gop(struct xenvif *vif,
  727. struct sk_buff *skb,
  728. struct gnttab_copy **gopp)
  729. {
  730. struct gnttab_copy *gop = *gopp;
  731. u16 pending_idx = XENVIF_TX_CB(skb)->pending_idx;
  732. struct skb_shared_info *shinfo = skb_shinfo(skb);
  733. struct pending_tx_info *tx_info;
  734. int nr_frags = shinfo->nr_frags;
  735. int i, err, start;
  736. u16 peek; /* peek into next tx request */
  737. /* Check status of header. */
  738. err = gop->status;
  739. if (unlikely(err))
  740. xenvif_idx_release(vif, pending_idx, XEN_NETIF_RSP_ERROR);
  741. /* Skip first skb fragment if it is on same page as header fragment. */
  742. start = (frag_get_pending_idx(&shinfo->frags[0]) == pending_idx);
  743. for (i = start; i < nr_frags; i++) {
  744. int j, newerr;
  745. pending_ring_idx_t head;
  746. pending_idx = frag_get_pending_idx(&shinfo->frags[i]);
  747. tx_info = &vif->pending_tx_info[pending_idx];
  748. head = tx_info->head;
  749. /* Check error status: if okay then remember grant handle. */
  750. do {
  751. newerr = (++gop)->status;
  752. if (newerr)
  753. break;
  754. peek = vif->pending_ring[pending_index(++head)];
  755. } while (!pending_tx_is_head(vif, peek));
  756. if (likely(!newerr)) {
  757. /* Had a previous error? Invalidate this fragment. */
  758. if (unlikely(err))
  759. xenvif_idx_release(vif, pending_idx,
  760. XEN_NETIF_RSP_OKAY);
  761. continue;
  762. }
  763. /* Error on this fragment: respond to client with an error. */
  764. xenvif_idx_release(vif, pending_idx, XEN_NETIF_RSP_ERROR);
  765. /* Not the first error? Preceding frags already invalidated. */
  766. if (err)
  767. continue;
  768. /* First error: invalidate header and preceding fragments. */
  769. pending_idx = XENVIF_TX_CB(skb)->pending_idx;
  770. xenvif_idx_release(vif, pending_idx, XEN_NETIF_RSP_OKAY);
  771. for (j = start; j < i; j++) {
  772. pending_idx = frag_get_pending_idx(&shinfo->frags[j]);
  773. xenvif_idx_release(vif, pending_idx,
  774. XEN_NETIF_RSP_OKAY);
  775. }
  776. /* Remember the error: invalidate all subsequent fragments. */
  777. err = newerr;
  778. }
  779. *gopp = gop + 1;
  780. return err;
  781. }
  782. static void xenvif_fill_frags(struct xenvif *vif, struct sk_buff *skb)
  783. {
  784. struct skb_shared_info *shinfo = skb_shinfo(skb);
  785. int nr_frags = shinfo->nr_frags;
  786. int i;
  787. for (i = 0; i < nr_frags; i++) {
  788. skb_frag_t *frag = shinfo->frags + i;
  789. struct xen_netif_tx_request *txp;
  790. struct page *page;
  791. u16 pending_idx;
  792. pending_idx = frag_get_pending_idx(frag);
  793. txp = &vif->pending_tx_info[pending_idx].req;
  794. page = virt_to_page(idx_to_kaddr(vif, pending_idx));
  795. __skb_fill_page_desc(skb, i, page, txp->offset, txp->size);
  796. skb->len += txp->size;
  797. skb->data_len += txp->size;
  798. skb->truesize += txp->size;
  799. /* Take an extra reference to offset xenvif_idx_release */
  800. get_page(vif->mmap_pages[pending_idx]);
  801. xenvif_idx_release(vif, pending_idx, XEN_NETIF_RSP_OKAY);
  802. }
  803. }
  804. static int xenvif_get_extras(struct xenvif *vif,
  805. struct xen_netif_extra_info *extras,
  806. int work_to_do)
  807. {
  808. struct xen_netif_extra_info extra;
  809. RING_IDX cons = vif->tx.req_cons;
  810. do {
  811. if (unlikely(work_to_do-- <= 0)) {
  812. netdev_err(vif->dev, "Missing extra info\n");
  813. xenvif_fatal_tx_err(vif);
  814. return -EBADR;
  815. }
  816. memcpy(&extra, RING_GET_REQUEST(&vif->tx, cons),
  817. sizeof(extra));
  818. if (unlikely(!extra.type ||
  819. extra.type >= XEN_NETIF_EXTRA_TYPE_MAX)) {
  820. vif->tx.req_cons = ++cons;
  821. netdev_err(vif->dev,
  822. "Invalid extra type: %d\n", extra.type);
  823. xenvif_fatal_tx_err(vif);
  824. return -EINVAL;
  825. }
  826. memcpy(&extras[extra.type - 1], &extra, sizeof(extra));
  827. vif->tx.req_cons = ++cons;
  828. } while (extra.flags & XEN_NETIF_EXTRA_FLAG_MORE);
  829. return work_to_do;
  830. }
  831. static int xenvif_set_skb_gso(struct xenvif *vif,
  832. struct sk_buff *skb,
  833. struct xen_netif_extra_info *gso)
  834. {
  835. if (!gso->u.gso.size) {
  836. netdev_err(vif->dev, "GSO size must not be zero.\n");
  837. xenvif_fatal_tx_err(vif);
  838. return -EINVAL;
  839. }
  840. switch (gso->u.gso.type) {
  841. case XEN_NETIF_GSO_TYPE_TCPV4:
  842. skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
  843. break;
  844. case XEN_NETIF_GSO_TYPE_TCPV6:
  845. skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
  846. break;
  847. default:
  848. netdev_err(vif->dev, "Bad GSO type %d.\n", gso->u.gso.type);
  849. xenvif_fatal_tx_err(vif);
  850. return -EINVAL;
  851. }
  852. skb_shinfo(skb)->gso_size = gso->u.gso.size;
  853. /* gso_segs will be calculated later */
  854. return 0;
  855. }
  856. static int checksum_setup(struct xenvif *vif, struct sk_buff *skb)
  857. {
  858. bool recalculate_partial_csum = false;
  859. /* A GSO SKB must be CHECKSUM_PARTIAL. However some buggy
  860. * peers can fail to set NETRXF_csum_blank when sending a GSO
  861. * frame. In this case force the SKB to CHECKSUM_PARTIAL and
  862. * recalculate the partial checksum.
  863. */
  864. if (skb->ip_summed != CHECKSUM_PARTIAL && skb_is_gso(skb)) {
  865. vif->rx_gso_checksum_fixup++;
  866. skb->ip_summed = CHECKSUM_PARTIAL;
  867. recalculate_partial_csum = true;
  868. }
  869. /* A non-CHECKSUM_PARTIAL SKB does not require setup. */
  870. if (skb->ip_summed != CHECKSUM_PARTIAL)
  871. return 0;
  872. return skb_checksum_setup(skb, recalculate_partial_csum);
  873. }
  874. static bool tx_credit_exceeded(struct xenvif *vif, unsigned size)
  875. {
  876. u64 now = get_jiffies_64();
  877. u64 next_credit = vif->credit_window_start +
  878. msecs_to_jiffies(vif->credit_usec / 1000);
  879. /* Timer could already be pending in rare cases. */
  880. if (timer_pending(&vif->credit_timeout))
  881. return true;
  882. /* Passed the point where we can replenish credit? */
  883. if (time_after_eq64(now, next_credit)) {
  884. vif->credit_window_start = now;
  885. tx_add_credit(vif);
  886. }
  887. /* Still too big to send right now? Set a callback. */
  888. if (size > vif->remaining_credit) {
  889. vif->credit_timeout.data =
  890. (unsigned long)vif;
  891. vif->credit_timeout.function =
  892. tx_credit_callback;
  893. mod_timer(&vif->credit_timeout,
  894. next_credit);
  895. vif->credit_window_start = next_credit;
  896. return true;
  897. }
  898. return false;
  899. }
  900. static unsigned xenvif_tx_build_gops(struct xenvif *vif, int budget)
  901. {
  902. struct gnttab_copy *gop = vif->tx_copy_ops, *request_gop;
  903. struct sk_buff *skb;
  904. int ret;
  905. while (xenvif_tx_pending_slots_available(vif) &&
  906. (skb_queue_len(&vif->tx_queue) < budget)) {
  907. struct xen_netif_tx_request txreq;
  908. struct xen_netif_tx_request txfrags[XEN_NETBK_LEGACY_SLOTS_MAX];
  909. struct page *page;
  910. struct xen_netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX-1];
  911. u16 pending_idx;
  912. RING_IDX idx;
  913. int work_to_do;
  914. unsigned int data_len;
  915. pending_ring_idx_t index;
  916. if (vif->tx.sring->req_prod - vif->tx.req_cons >
  917. XEN_NETIF_TX_RING_SIZE) {
  918. netdev_err(vif->dev,
  919. "Impossible number of requests. "
  920. "req_prod %d, req_cons %d, size %ld\n",
  921. vif->tx.sring->req_prod, vif->tx.req_cons,
  922. XEN_NETIF_TX_RING_SIZE);
  923. xenvif_fatal_tx_err(vif);
  924. continue;
  925. }
  926. work_to_do = RING_HAS_UNCONSUMED_REQUESTS(&vif->tx);
  927. if (!work_to_do)
  928. break;
  929. idx = vif->tx.req_cons;
  930. rmb(); /* Ensure that we see the request before we copy it. */
  931. memcpy(&txreq, RING_GET_REQUEST(&vif->tx, idx), sizeof(txreq));
  932. /* Credit-based scheduling. */
  933. if (txreq.size > vif->remaining_credit &&
  934. tx_credit_exceeded(vif, txreq.size))
  935. break;
  936. vif->remaining_credit -= txreq.size;
  937. work_to_do--;
  938. vif->tx.req_cons = ++idx;
  939. memset(extras, 0, sizeof(extras));
  940. if (txreq.flags & XEN_NETTXF_extra_info) {
  941. work_to_do = xenvif_get_extras(vif, extras,
  942. work_to_do);
  943. idx = vif->tx.req_cons;
  944. if (unlikely(work_to_do < 0))
  945. break;
  946. }
  947. ret = xenvif_count_requests(vif, &txreq, txfrags, work_to_do);
  948. if (unlikely(ret < 0))
  949. break;
  950. idx += ret;
  951. if (unlikely(txreq.size < ETH_HLEN)) {
  952. netdev_dbg(vif->dev,
  953. "Bad packet size: %d\n", txreq.size);
  954. xenvif_tx_err(vif, &txreq, idx);
  955. break;
  956. }
  957. /* No crossing a page as the payload mustn't fragment. */
  958. if (unlikely((txreq.offset + txreq.size) > PAGE_SIZE)) {
  959. netdev_err(vif->dev,
  960. "txreq.offset: %x, size: %u, end: %lu\n",
  961. txreq.offset, txreq.size,
  962. (txreq.offset&~PAGE_MASK) + txreq.size);
  963. xenvif_fatal_tx_err(vif);
  964. break;
  965. }
  966. index = pending_index(vif->pending_cons);
  967. pending_idx = vif->pending_ring[index];
  968. data_len = (txreq.size > PKT_PROT_LEN &&
  969. ret < XEN_NETBK_LEGACY_SLOTS_MAX) ?
  970. PKT_PROT_LEN : txreq.size;
  971. skb = alloc_skb(data_len + NET_SKB_PAD + NET_IP_ALIGN,
  972. GFP_ATOMIC | __GFP_NOWARN);
  973. if (unlikely(skb == NULL)) {
  974. netdev_dbg(vif->dev,
  975. "Can't allocate a skb in start_xmit.\n");
  976. xenvif_tx_err(vif, &txreq, idx);
  977. break;
  978. }
  979. /* Packets passed to netif_rx() must have some headroom. */
  980. skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
  981. if (extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].type) {
  982. struct xen_netif_extra_info *gso;
  983. gso = &extras[XEN_NETIF_EXTRA_TYPE_GSO - 1];
  984. if (xenvif_set_skb_gso(vif, skb, gso)) {
  985. /* Failure in xenvif_set_skb_gso is fatal. */
  986. kfree_skb(skb);
  987. break;
  988. }
  989. }
  990. /* XXX could copy straight to head */
  991. page = xenvif_alloc_page(vif, pending_idx);
  992. if (!page) {
  993. kfree_skb(skb);
  994. xenvif_tx_err(vif, &txreq, idx);
  995. break;
  996. }
  997. gop->source.u.ref = txreq.gref;
  998. gop->source.domid = vif->domid;
  999. gop->source.offset = txreq.offset;
  1000. gop->dest.u.gmfn = virt_to_mfn(page_address(page));
  1001. gop->dest.domid = DOMID_SELF;
  1002. gop->dest.offset = txreq.offset;
  1003. gop->len = txreq.size;
  1004. gop->flags = GNTCOPY_source_gref;
  1005. gop++;
  1006. memcpy(&vif->pending_tx_info[pending_idx].req,
  1007. &txreq, sizeof(txreq));
  1008. vif->pending_tx_info[pending_idx].head = index;
  1009. XENVIF_TX_CB(skb)->pending_idx = pending_idx;
  1010. __skb_put(skb, data_len);
  1011. skb_shinfo(skb)->nr_frags = ret;
  1012. if (data_len < txreq.size) {
  1013. skb_shinfo(skb)->nr_frags++;
  1014. frag_set_pending_idx(&skb_shinfo(skb)->frags[0],
  1015. pending_idx);
  1016. } else {
  1017. frag_set_pending_idx(&skb_shinfo(skb)->frags[0],
  1018. INVALID_PENDING_IDX);
  1019. }
  1020. vif->pending_cons++;
  1021. request_gop = xenvif_get_requests(vif, skb, txfrags, gop);
  1022. if (request_gop == NULL) {
  1023. kfree_skb(skb);
  1024. xenvif_tx_err(vif, &txreq, idx);
  1025. break;
  1026. }
  1027. gop = request_gop;
  1028. __skb_queue_tail(&vif->tx_queue, skb);
  1029. vif->tx.req_cons = idx;
  1030. if ((gop-vif->tx_copy_ops) >= ARRAY_SIZE(vif->tx_copy_ops))
  1031. break;
  1032. }
  1033. return gop - vif->tx_copy_ops;
  1034. }
  1035. static int xenvif_tx_submit(struct xenvif *vif)
  1036. {
  1037. struct gnttab_copy *gop = vif->tx_copy_ops;
  1038. struct sk_buff *skb;
  1039. int work_done = 0;
  1040. while ((skb = __skb_dequeue(&vif->tx_queue)) != NULL) {
  1041. struct xen_netif_tx_request *txp;
  1042. u16 pending_idx;
  1043. unsigned data_len;
  1044. pending_idx = XENVIF_TX_CB(skb)->pending_idx;
  1045. txp = &vif->pending_tx_info[pending_idx].req;
  1046. /* Check the remap error code. */
  1047. if (unlikely(xenvif_tx_check_gop(vif, skb, &gop))) {
  1048. netdev_dbg(vif->dev, "netback grant failed.\n");
  1049. skb_shinfo(skb)->nr_frags = 0;
  1050. kfree_skb(skb);
  1051. continue;
  1052. }
  1053. data_len = skb->len;
  1054. memcpy(skb->data,
  1055. (void *)(idx_to_kaddr(vif, pending_idx)|txp->offset),
  1056. data_len);
  1057. if (data_len < txp->size) {
  1058. /* Append the packet payload as a fragment. */
  1059. txp->offset += data_len;
  1060. txp->size -= data_len;
  1061. } else {
  1062. /* Schedule a response immediately. */
  1063. xenvif_idx_release(vif, pending_idx,
  1064. XEN_NETIF_RSP_OKAY);
  1065. }
  1066. if (txp->flags & XEN_NETTXF_csum_blank)
  1067. skb->ip_summed = CHECKSUM_PARTIAL;
  1068. else if (txp->flags & XEN_NETTXF_data_validated)
  1069. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1070. xenvif_fill_frags(vif, skb);
  1071. if (skb_is_nonlinear(skb) && skb_headlen(skb) < PKT_PROT_LEN) {
  1072. int target = min_t(int, skb->len, PKT_PROT_LEN);
  1073. __pskb_pull_tail(skb, target - skb_headlen(skb));
  1074. }
  1075. skb->dev = vif->dev;
  1076. skb->protocol = eth_type_trans(skb, skb->dev);
  1077. skb_reset_network_header(skb);
  1078. if (checksum_setup(vif, skb)) {
  1079. netdev_dbg(vif->dev,
  1080. "Can't setup checksum in net_tx_action\n");
  1081. kfree_skb(skb);
  1082. continue;
  1083. }
  1084. skb_probe_transport_header(skb, 0);
  1085. /* If the packet is GSO then we will have just set up the
  1086. * transport header offset in checksum_setup so it's now
  1087. * straightforward to calculate gso_segs.
  1088. */
  1089. if (skb_is_gso(skb)) {
  1090. int mss = skb_shinfo(skb)->gso_size;
  1091. int hdrlen = skb_transport_header(skb) -
  1092. skb_mac_header(skb) +
  1093. tcp_hdrlen(skb);
  1094. skb_shinfo(skb)->gso_segs =
  1095. DIV_ROUND_UP(skb->len - hdrlen, mss);
  1096. }
  1097. vif->dev->stats.rx_bytes += skb->len;
  1098. vif->dev->stats.rx_packets++;
  1099. work_done++;
  1100. netif_receive_skb(skb);
  1101. }
  1102. return work_done;
  1103. }
  1104. /* Called after netfront has transmitted */
  1105. int xenvif_tx_action(struct xenvif *vif, int budget)
  1106. {
  1107. unsigned nr_gops;
  1108. int work_done;
  1109. if (unlikely(!tx_work_todo(vif)))
  1110. return 0;
  1111. nr_gops = xenvif_tx_build_gops(vif, budget);
  1112. if (nr_gops == 0)
  1113. return 0;
  1114. gnttab_batch_copy(vif->tx_copy_ops, nr_gops);
  1115. work_done = xenvif_tx_submit(vif);
  1116. return work_done;
  1117. }
  1118. static void xenvif_idx_release(struct xenvif *vif, u16 pending_idx,
  1119. u8 status)
  1120. {
  1121. struct pending_tx_info *pending_tx_info;
  1122. pending_ring_idx_t head;
  1123. u16 peek; /* peek into next tx request */
  1124. BUG_ON(vif->mmap_pages[pending_idx] == (void *)(~0UL));
  1125. /* Already complete? */
  1126. if (vif->mmap_pages[pending_idx] == NULL)
  1127. return;
  1128. pending_tx_info = &vif->pending_tx_info[pending_idx];
  1129. head = pending_tx_info->head;
  1130. BUG_ON(!pending_tx_is_head(vif, head));
  1131. BUG_ON(vif->pending_ring[pending_index(head)] != pending_idx);
  1132. do {
  1133. pending_ring_idx_t index;
  1134. pending_ring_idx_t idx = pending_index(head);
  1135. u16 info_idx = vif->pending_ring[idx];
  1136. pending_tx_info = &vif->pending_tx_info[info_idx];
  1137. make_tx_response(vif, &pending_tx_info->req, status);
  1138. /* Setting any number other than
  1139. * INVALID_PENDING_RING_IDX indicates this slot is
  1140. * starting a new packet / ending a previous packet.
  1141. */
  1142. pending_tx_info->head = 0;
  1143. index = pending_index(vif->pending_prod++);
  1144. vif->pending_ring[index] = vif->pending_ring[info_idx];
  1145. peek = vif->pending_ring[pending_index(++head)];
  1146. } while (!pending_tx_is_head(vif, peek));
  1147. put_page(vif->mmap_pages[pending_idx]);
  1148. vif->mmap_pages[pending_idx] = NULL;
  1149. }
  1150. static void make_tx_response(struct xenvif *vif,
  1151. struct xen_netif_tx_request *txp,
  1152. s8 st)
  1153. {
  1154. RING_IDX i = vif->tx.rsp_prod_pvt;
  1155. struct xen_netif_tx_response *resp;
  1156. int notify;
  1157. resp = RING_GET_RESPONSE(&vif->tx, i);
  1158. resp->id = txp->id;
  1159. resp->status = st;
  1160. if (txp->flags & XEN_NETTXF_extra_info)
  1161. RING_GET_RESPONSE(&vif->tx, ++i)->status = XEN_NETIF_RSP_NULL;
  1162. vif->tx.rsp_prod_pvt = ++i;
  1163. RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&vif->tx, notify);
  1164. if (notify)
  1165. notify_remote_via_irq(vif->tx_irq);
  1166. }
  1167. static struct xen_netif_rx_response *make_rx_response(struct xenvif *vif,
  1168. u16 id,
  1169. s8 st,
  1170. u16 offset,
  1171. u16 size,
  1172. u16 flags)
  1173. {
  1174. RING_IDX i = vif->rx.rsp_prod_pvt;
  1175. struct xen_netif_rx_response *resp;
  1176. resp = RING_GET_RESPONSE(&vif->rx, i);
  1177. resp->offset = offset;
  1178. resp->flags = flags;
  1179. resp->id = id;
  1180. resp->status = (s16)size;
  1181. if (st < 0)
  1182. resp->status = (s16)st;
  1183. vif->rx.rsp_prod_pvt = ++i;
  1184. return resp;
  1185. }
  1186. static inline int rx_work_todo(struct xenvif *vif)
  1187. {
  1188. return !skb_queue_empty(&vif->rx_queue) &&
  1189. xenvif_rx_ring_slots_available(vif, vif->rx_last_skb_slots);
  1190. }
  1191. static inline int tx_work_todo(struct xenvif *vif)
  1192. {
  1193. if (likely(RING_HAS_UNCONSUMED_REQUESTS(&vif->tx)) &&
  1194. xenvif_tx_pending_slots_available(vif))
  1195. return 1;
  1196. return 0;
  1197. }
  1198. void xenvif_unmap_frontend_rings(struct xenvif *vif)
  1199. {
  1200. if (vif->tx.sring)
  1201. xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(vif),
  1202. vif->tx.sring);
  1203. if (vif->rx.sring)
  1204. xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(vif),
  1205. vif->rx.sring);
  1206. }
  1207. int xenvif_map_frontend_rings(struct xenvif *vif,
  1208. grant_ref_t tx_ring_ref,
  1209. grant_ref_t rx_ring_ref)
  1210. {
  1211. void *addr;
  1212. struct xen_netif_tx_sring *txs;
  1213. struct xen_netif_rx_sring *rxs;
  1214. int err = -ENOMEM;
  1215. err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(vif),
  1216. tx_ring_ref, &addr);
  1217. if (err)
  1218. goto err;
  1219. txs = (struct xen_netif_tx_sring *)addr;
  1220. BACK_RING_INIT(&vif->tx, txs, PAGE_SIZE);
  1221. err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(vif),
  1222. rx_ring_ref, &addr);
  1223. if (err)
  1224. goto err;
  1225. rxs = (struct xen_netif_rx_sring *)addr;
  1226. BACK_RING_INIT(&vif->rx, rxs, PAGE_SIZE);
  1227. return 0;
  1228. err:
  1229. xenvif_unmap_frontend_rings(vif);
  1230. return err;
  1231. }
  1232. void xenvif_stop_queue(struct xenvif *vif)
  1233. {
  1234. if (!vif->can_queue)
  1235. return;
  1236. netif_stop_queue(vif->dev);
  1237. }
  1238. static void xenvif_start_queue(struct xenvif *vif)
  1239. {
  1240. if (xenvif_schedulable(vif))
  1241. netif_wake_queue(vif->dev);
  1242. }
  1243. int xenvif_kthread_guest_rx(void *data)
  1244. {
  1245. struct xenvif *vif = data;
  1246. struct sk_buff *skb;
  1247. while (!kthread_should_stop()) {
  1248. wait_event_interruptible(vif->wq,
  1249. rx_work_todo(vif) ||
  1250. kthread_should_stop());
  1251. if (kthread_should_stop())
  1252. break;
  1253. if (!skb_queue_empty(&vif->rx_queue))
  1254. xenvif_rx_action(vif);
  1255. if (skb_queue_empty(&vif->rx_queue) &&
  1256. netif_queue_stopped(vif->dev))
  1257. xenvif_start_queue(vif);
  1258. cond_resched();
  1259. }
  1260. /* Bin any remaining skbs */
  1261. while ((skb = skb_dequeue(&vif->rx_queue)) != NULL)
  1262. dev_kfree_skb(skb);
  1263. return 0;
  1264. }
  1265. static int __init netback_init(void)
  1266. {
  1267. int rc = 0;
  1268. if (!xen_domain())
  1269. return -ENODEV;
  1270. if (fatal_skb_slots < XEN_NETBK_LEGACY_SLOTS_MAX) {
  1271. pr_info("fatal_skb_slots too small (%d), bump it to XEN_NETBK_LEGACY_SLOTS_MAX (%d)\n",
  1272. fatal_skb_slots, XEN_NETBK_LEGACY_SLOTS_MAX);
  1273. fatal_skb_slots = XEN_NETBK_LEGACY_SLOTS_MAX;
  1274. }
  1275. rc = xenvif_xenbus_init();
  1276. if (rc)
  1277. goto failed_init;
  1278. return 0;
  1279. failed_init:
  1280. return rc;
  1281. }
  1282. module_init(netback_init);
  1283. static void __exit netback_fini(void)
  1284. {
  1285. xenvif_xenbus_fini();
  1286. }
  1287. module_exit(netback_fini);
  1288. MODULE_LICENSE("Dual BSD/GPL");
  1289. MODULE_ALIAS("xen-backend:vif");