netback.c 45 KB

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