netback.c 47 KB

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