netback.c 41 KB

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