netback.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846
  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. /* Currently only TCPv4 S.O. is supported. */
  890. if (gso->u.gso.type != XEN_NETIF_GSO_TYPE_TCPV4) {
  891. netdev_err(vif->dev, "Bad GSO type %d.\n", gso->u.gso.type);
  892. xenvif_fatal_tx_err(vif);
  893. return -EINVAL;
  894. }
  895. skb_shinfo(skb)->gso_size = gso->u.gso.size;
  896. skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
  897. /* Header must be checked, and gso_segs computed. */
  898. skb_shinfo(skb)->gso_type |= SKB_GSO_DODGY;
  899. skb_shinfo(skb)->gso_segs = 0;
  900. return 0;
  901. }
  902. static inline void maybe_pull_tail(struct sk_buff *skb, unsigned int len)
  903. {
  904. if (skb_is_nonlinear(skb) && skb_headlen(skb) < len) {
  905. /* If we need to pullup then pullup to the max, so we
  906. * won't need to do it again.
  907. */
  908. int target = min_t(int, skb->len, MAX_TCP_HEADER);
  909. __pskb_pull_tail(skb, target - skb_headlen(skb));
  910. }
  911. }
  912. static int checksum_setup_ip(struct xenvif *vif, struct sk_buff *skb,
  913. int recalculate_partial_csum)
  914. {
  915. struct iphdr *iph = (void *)skb->data;
  916. unsigned int header_size;
  917. unsigned int off;
  918. int err = -EPROTO;
  919. off = sizeof(struct iphdr);
  920. header_size = skb->network_header + off + MAX_IPOPTLEN;
  921. maybe_pull_tail(skb, header_size);
  922. off = iph->ihl * 4;
  923. switch (iph->protocol) {
  924. case IPPROTO_TCP:
  925. if (!skb_partial_csum_set(skb, off,
  926. offsetof(struct tcphdr, check)))
  927. goto out;
  928. if (recalculate_partial_csum) {
  929. struct tcphdr *tcph = tcp_hdr(skb);
  930. header_size = skb->network_header +
  931. off +
  932. sizeof(struct tcphdr);
  933. maybe_pull_tail(skb, header_size);
  934. tcph->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr,
  935. skb->len - off,
  936. IPPROTO_TCP, 0);
  937. }
  938. break;
  939. case IPPROTO_UDP:
  940. if (!skb_partial_csum_set(skb, off,
  941. offsetof(struct udphdr, check)))
  942. goto out;
  943. if (recalculate_partial_csum) {
  944. struct udphdr *udph = udp_hdr(skb);
  945. header_size = skb->network_header +
  946. off +
  947. sizeof(struct udphdr);
  948. maybe_pull_tail(skb, header_size);
  949. udph->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr,
  950. skb->len - off,
  951. IPPROTO_UDP, 0);
  952. }
  953. break;
  954. default:
  955. if (net_ratelimit())
  956. netdev_err(vif->dev,
  957. "Attempting to checksum a non-TCP/UDP packet, "
  958. "dropping a protocol %d packet\n",
  959. iph->protocol);
  960. goto out;
  961. }
  962. err = 0;
  963. out:
  964. return err;
  965. }
  966. static int checksum_setup_ipv6(struct xenvif *vif, struct sk_buff *skb,
  967. int recalculate_partial_csum)
  968. {
  969. int err = -EPROTO;
  970. struct ipv6hdr *ipv6h = (void *)skb->data;
  971. u8 nexthdr;
  972. unsigned int header_size;
  973. unsigned int off;
  974. bool fragment;
  975. bool done;
  976. done = false;
  977. off = sizeof(struct ipv6hdr);
  978. header_size = skb->network_header + off;
  979. maybe_pull_tail(skb, header_size);
  980. nexthdr = ipv6h->nexthdr;
  981. while ((off <= sizeof(struct ipv6hdr) + ntohs(ipv6h->payload_len)) &&
  982. !done) {
  983. switch (nexthdr) {
  984. case IPPROTO_DSTOPTS:
  985. case IPPROTO_HOPOPTS:
  986. case IPPROTO_ROUTING: {
  987. struct ipv6_opt_hdr *hp = (void *)(skb->data + off);
  988. header_size = skb->network_header +
  989. off +
  990. sizeof(struct ipv6_opt_hdr);
  991. maybe_pull_tail(skb, header_size);
  992. nexthdr = hp->nexthdr;
  993. off += ipv6_optlen(hp);
  994. break;
  995. }
  996. case IPPROTO_AH: {
  997. struct ip_auth_hdr *hp = (void *)(skb->data + off);
  998. header_size = skb->network_header +
  999. off +
  1000. sizeof(struct ip_auth_hdr);
  1001. maybe_pull_tail(skb, header_size);
  1002. nexthdr = hp->nexthdr;
  1003. off += (hp->hdrlen+2)<<2;
  1004. break;
  1005. }
  1006. case IPPROTO_FRAGMENT:
  1007. fragment = true;
  1008. /* fall through */
  1009. default:
  1010. done = true;
  1011. break;
  1012. }
  1013. }
  1014. if (!done) {
  1015. if (net_ratelimit())
  1016. netdev_err(vif->dev, "Failed to parse packet header\n");
  1017. goto out;
  1018. }
  1019. if (fragment) {
  1020. if (net_ratelimit())
  1021. netdev_err(vif->dev, "Packet is a fragment!\n");
  1022. goto out;
  1023. }
  1024. switch (nexthdr) {
  1025. case IPPROTO_TCP:
  1026. if (!skb_partial_csum_set(skb, off,
  1027. offsetof(struct tcphdr, check)))
  1028. goto out;
  1029. if (recalculate_partial_csum) {
  1030. struct tcphdr *tcph = tcp_hdr(skb);
  1031. header_size = skb->network_header +
  1032. off +
  1033. sizeof(struct tcphdr);
  1034. maybe_pull_tail(skb, header_size);
  1035. tcph->check = ~csum_ipv6_magic(&ipv6h->saddr,
  1036. &ipv6h->daddr,
  1037. skb->len - off,
  1038. IPPROTO_TCP, 0);
  1039. }
  1040. break;
  1041. case IPPROTO_UDP:
  1042. if (!skb_partial_csum_set(skb, off,
  1043. offsetof(struct udphdr, check)))
  1044. goto out;
  1045. if (recalculate_partial_csum) {
  1046. struct udphdr *udph = udp_hdr(skb);
  1047. header_size = skb->network_header +
  1048. off +
  1049. sizeof(struct udphdr);
  1050. maybe_pull_tail(skb, header_size);
  1051. udph->check = ~csum_ipv6_magic(&ipv6h->saddr,
  1052. &ipv6h->daddr,
  1053. skb->len - off,
  1054. IPPROTO_UDP, 0);
  1055. }
  1056. break;
  1057. default:
  1058. if (net_ratelimit())
  1059. netdev_err(vif->dev,
  1060. "Attempting to checksum a non-TCP/UDP packet, "
  1061. "dropping a protocol %d packet\n",
  1062. nexthdr);
  1063. goto out;
  1064. }
  1065. err = 0;
  1066. out:
  1067. return err;
  1068. }
  1069. static int checksum_setup(struct xenvif *vif, struct sk_buff *skb)
  1070. {
  1071. int err = -EPROTO;
  1072. int recalculate_partial_csum = 0;
  1073. /* A GSO SKB must be CHECKSUM_PARTIAL. However some buggy
  1074. * peers can fail to set NETRXF_csum_blank when sending a GSO
  1075. * frame. In this case force the SKB to CHECKSUM_PARTIAL and
  1076. * recalculate the partial checksum.
  1077. */
  1078. if (skb->ip_summed != CHECKSUM_PARTIAL && skb_is_gso(skb)) {
  1079. vif->rx_gso_checksum_fixup++;
  1080. skb->ip_summed = CHECKSUM_PARTIAL;
  1081. recalculate_partial_csum = 1;
  1082. }
  1083. /* A non-CHECKSUM_PARTIAL SKB does not require setup. */
  1084. if (skb->ip_summed != CHECKSUM_PARTIAL)
  1085. return 0;
  1086. if (skb->protocol == htons(ETH_P_IP))
  1087. err = checksum_setup_ip(vif, skb, recalculate_partial_csum);
  1088. else if (skb->protocol == htons(ETH_P_IPV6))
  1089. err = checksum_setup_ipv6(vif, skb, recalculate_partial_csum);
  1090. return err;
  1091. }
  1092. static bool tx_credit_exceeded(struct xenvif *vif, unsigned size)
  1093. {
  1094. unsigned long now = jiffies;
  1095. unsigned long next_credit =
  1096. vif->credit_timeout.expires +
  1097. msecs_to_jiffies(vif->credit_usec / 1000);
  1098. /* Timer could already be pending in rare cases. */
  1099. if (timer_pending(&vif->credit_timeout))
  1100. return true;
  1101. /* Passed the point where we can replenish credit? */
  1102. if (time_after_eq(now, next_credit)) {
  1103. vif->credit_timeout.expires = now;
  1104. tx_add_credit(vif);
  1105. }
  1106. /* Still too big to send right now? Set a callback. */
  1107. if (size > vif->remaining_credit) {
  1108. vif->credit_timeout.data =
  1109. (unsigned long)vif;
  1110. vif->credit_timeout.function =
  1111. tx_credit_callback;
  1112. mod_timer(&vif->credit_timeout,
  1113. next_credit);
  1114. return true;
  1115. }
  1116. return false;
  1117. }
  1118. static unsigned xenvif_tx_build_gops(struct xenvif *vif)
  1119. {
  1120. struct gnttab_copy *gop = vif->tx_copy_ops, *request_gop;
  1121. struct sk_buff *skb;
  1122. int ret;
  1123. while ((nr_pending_reqs(vif) + XEN_NETBK_LEGACY_SLOTS_MAX
  1124. < MAX_PENDING_REQS)) {
  1125. struct xen_netif_tx_request txreq;
  1126. struct xen_netif_tx_request txfrags[XEN_NETBK_LEGACY_SLOTS_MAX];
  1127. struct page *page;
  1128. struct xen_netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX-1];
  1129. u16 pending_idx;
  1130. RING_IDX idx;
  1131. int work_to_do;
  1132. unsigned int data_len;
  1133. pending_ring_idx_t index;
  1134. if (vif->tx.sring->req_prod - vif->tx.req_cons >
  1135. XEN_NETIF_TX_RING_SIZE) {
  1136. netdev_err(vif->dev,
  1137. "Impossible number of requests. "
  1138. "req_prod %d, req_cons %d, size %ld\n",
  1139. vif->tx.sring->req_prod, vif->tx.req_cons,
  1140. XEN_NETIF_TX_RING_SIZE);
  1141. xenvif_fatal_tx_err(vif);
  1142. continue;
  1143. }
  1144. RING_FINAL_CHECK_FOR_REQUESTS(&vif->tx, work_to_do);
  1145. if (!work_to_do)
  1146. break;
  1147. idx = vif->tx.req_cons;
  1148. rmb(); /* Ensure that we see the request before we copy it. */
  1149. memcpy(&txreq, RING_GET_REQUEST(&vif->tx, idx), sizeof(txreq));
  1150. /* Credit-based scheduling. */
  1151. if (txreq.size > vif->remaining_credit &&
  1152. tx_credit_exceeded(vif, txreq.size))
  1153. break;
  1154. vif->remaining_credit -= txreq.size;
  1155. work_to_do--;
  1156. vif->tx.req_cons = ++idx;
  1157. memset(extras, 0, sizeof(extras));
  1158. if (txreq.flags & XEN_NETTXF_extra_info) {
  1159. work_to_do = xenvif_get_extras(vif, extras,
  1160. work_to_do);
  1161. idx = vif->tx.req_cons;
  1162. if (unlikely(work_to_do < 0))
  1163. break;
  1164. }
  1165. ret = xenvif_count_requests(vif, &txreq, txfrags, work_to_do);
  1166. if (unlikely(ret < 0))
  1167. break;
  1168. idx += ret;
  1169. if (unlikely(txreq.size < ETH_HLEN)) {
  1170. netdev_dbg(vif->dev,
  1171. "Bad packet size: %d\n", txreq.size);
  1172. xenvif_tx_err(vif, &txreq, idx);
  1173. break;
  1174. }
  1175. /* No crossing a page as the payload mustn't fragment. */
  1176. if (unlikely((txreq.offset + txreq.size) > PAGE_SIZE)) {
  1177. netdev_err(vif->dev,
  1178. "txreq.offset: %x, size: %u, end: %lu\n",
  1179. txreq.offset, txreq.size,
  1180. (txreq.offset&~PAGE_MASK) + txreq.size);
  1181. xenvif_fatal_tx_err(vif);
  1182. break;
  1183. }
  1184. index = pending_index(vif->pending_cons);
  1185. pending_idx = vif->pending_ring[index];
  1186. data_len = (txreq.size > PKT_PROT_LEN &&
  1187. ret < XEN_NETBK_LEGACY_SLOTS_MAX) ?
  1188. PKT_PROT_LEN : txreq.size;
  1189. skb = alloc_skb(data_len + NET_SKB_PAD + NET_IP_ALIGN,
  1190. GFP_ATOMIC | __GFP_NOWARN);
  1191. if (unlikely(skb == NULL)) {
  1192. netdev_dbg(vif->dev,
  1193. "Can't allocate a skb in start_xmit.\n");
  1194. xenvif_tx_err(vif, &txreq, idx);
  1195. break;
  1196. }
  1197. /* Packets passed to netif_rx() must have some headroom. */
  1198. skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
  1199. if (extras[XEN_NETIF_EXTRA_TYPE_GSO - 1].type) {
  1200. struct xen_netif_extra_info *gso;
  1201. gso = &extras[XEN_NETIF_EXTRA_TYPE_GSO - 1];
  1202. if (xenvif_set_skb_gso(vif, skb, gso)) {
  1203. /* Failure in xenvif_set_skb_gso is fatal. */
  1204. kfree_skb(skb);
  1205. break;
  1206. }
  1207. }
  1208. /* XXX could copy straight to head */
  1209. page = xenvif_alloc_page(vif, pending_idx);
  1210. if (!page) {
  1211. kfree_skb(skb);
  1212. xenvif_tx_err(vif, &txreq, idx);
  1213. break;
  1214. }
  1215. gop->source.u.ref = txreq.gref;
  1216. gop->source.domid = vif->domid;
  1217. gop->source.offset = txreq.offset;
  1218. gop->dest.u.gmfn = virt_to_mfn(page_address(page));
  1219. gop->dest.domid = DOMID_SELF;
  1220. gop->dest.offset = txreq.offset;
  1221. gop->len = txreq.size;
  1222. gop->flags = GNTCOPY_source_gref;
  1223. gop++;
  1224. memcpy(&vif->pending_tx_info[pending_idx].req,
  1225. &txreq, sizeof(txreq));
  1226. vif->pending_tx_info[pending_idx].head = index;
  1227. *((u16 *)skb->data) = pending_idx;
  1228. __skb_put(skb, data_len);
  1229. skb_shinfo(skb)->nr_frags = ret;
  1230. if (data_len < txreq.size) {
  1231. skb_shinfo(skb)->nr_frags++;
  1232. frag_set_pending_idx(&skb_shinfo(skb)->frags[0],
  1233. pending_idx);
  1234. } else {
  1235. frag_set_pending_idx(&skb_shinfo(skb)->frags[0],
  1236. INVALID_PENDING_IDX);
  1237. }
  1238. vif->pending_cons++;
  1239. request_gop = xenvif_get_requests(vif, skb, txfrags, gop);
  1240. if (request_gop == NULL) {
  1241. kfree_skb(skb);
  1242. xenvif_tx_err(vif, &txreq, idx);
  1243. break;
  1244. }
  1245. gop = request_gop;
  1246. __skb_queue_tail(&vif->tx_queue, skb);
  1247. vif->tx.req_cons = idx;
  1248. if ((gop-vif->tx_copy_ops) >= ARRAY_SIZE(vif->tx_copy_ops))
  1249. break;
  1250. }
  1251. return gop - vif->tx_copy_ops;
  1252. }
  1253. static int xenvif_tx_submit(struct xenvif *vif, int budget)
  1254. {
  1255. struct gnttab_copy *gop = vif->tx_copy_ops;
  1256. struct sk_buff *skb;
  1257. int work_done = 0;
  1258. while (work_done < budget &&
  1259. (skb = __skb_dequeue(&vif->tx_queue)) != NULL) {
  1260. struct xen_netif_tx_request *txp;
  1261. u16 pending_idx;
  1262. unsigned data_len;
  1263. pending_idx = *((u16 *)skb->data);
  1264. txp = &vif->pending_tx_info[pending_idx].req;
  1265. /* Check the remap error code. */
  1266. if (unlikely(xenvif_tx_check_gop(vif, skb, &gop))) {
  1267. netdev_dbg(vif->dev, "netback grant failed.\n");
  1268. skb_shinfo(skb)->nr_frags = 0;
  1269. kfree_skb(skb);
  1270. continue;
  1271. }
  1272. data_len = skb->len;
  1273. memcpy(skb->data,
  1274. (void *)(idx_to_kaddr(vif, pending_idx)|txp->offset),
  1275. data_len);
  1276. if (data_len < txp->size) {
  1277. /* Append the packet payload as a fragment. */
  1278. txp->offset += data_len;
  1279. txp->size -= data_len;
  1280. } else {
  1281. /* Schedule a response immediately. */
  1282. xenvif_idx_release(vif, pending_idx,
  1283. XEN_NETIF_RSP_OKAY);
  1284. }
  1285. if (txp->flags & XEN_NETTXF_csum_blank)
  1286. skb->ip_summed = CHECKSUM_PARTIAL;
  1287. else if (txp->flags & XEN_NETTXF_data_validated)
  1288. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1289. xenvif_fill_frags(vif, skb);
  1290. if (skb_is_nonlinear(skb) && skb_headlen(skb) < PKT_PROT_LEN) {
  1291. int target = min_t(int, skb->len, PKT_PROT_LEN);
  1292. __pskb_pull_tail(skb, target - skb_headlen(skb));
  1293. }
  1294. skb->dev = vif->dev;
  1295. skb->protocol = eth_type_trans(skb, skb->dev);
  1296. skb_reset_network_header(skb);
  1297. if (checksum_setup(vif, skb)) {
  1298. netdev_dbg(vif->dev,
  1299. "Can't setup checksum in net_tx_action\n");
  1300. kfree_skb(skb);
  1301. continue;
  1302. }
  1303. skb_probe_transport_header(skb, 0);
  1304. vif->dev->stats.rx_bytes += skb->len;
  1305. vif->dev->stats.rx_packets++;
  1306. work_done++;
  1307. netif_receive_skb(skb);
  1308. }
  1309. return work_done;
  1310. }
  1311. /* Called after netfront has transmitted */
  1312. int xenvif_tx_action(struct xenvif *vif, int budget)
  1313. {
  1314. unsigned nr_gops;
  1315. int work_done;
  1316. if (unlikely(!tx_work_todo(vif)))
  1317. return 0;
  1318. nr_gops = xenvif_tx_build_gops(vif);
  1319. if (nr_gops == 0)
  1320. return 0;
  1321. gnttab_batch_copy(vif->tx_copy_ops, nr_gops);
  1322. work_done = xenvif_tx_submit(vif, nr_gops);
  1323. return work_done;
  1324. }
  1325. static void xenvif_idx_release(struct xenvif *vif, u16 pending_idx,
  1326. u8 status)
  1327. {
  1328. struct pending_tx_info *pending_tx_info;
  1329. pending_ring_idx_t head;
  1330. u16 peek; /* peek into next tx request */
  1331. BUG_ON(vif->mmap_pages[pending_idx] == (void *)(~0UL));
  1332. /* Already complete? */
  1333. if (vif->mmap_pages[pending_idx] == NULL)
  1334. return;
  1335. pending_tx_info = &vif->pending_tx_info[pending_idx];
  1336. head = pending_tx_info->head;
  1337. BUG_ON(!pending_tx_is_head(vif, head));
  1338. BUG_ON(vif->pending_ring[pending_index(head)] != pending_idx);
  1339. do {
  1340. pending_ring_idx_t index;
  1341. pending_ring_idx_t idx = pending_index(head);
  1342. u16 info_idx = vif->pending_ring[idx];
  1343. pending_tx_info = &vif->pending_tx_info[info_idx];
  1344. make_tx_response(vif, &pending_tx_info->req, status);
  1345. /* Setting any number other than
  1346. * INVALID_PENDING_RING_IDX indicates this slot is
  1347. * starting a new packet / ending a previous packet.
  1348. */
  1349. pending_tx_info->head = 0;
  1350. index = pending_index(vif->pending_prod++);
  1351. vif->pending_ring[index] = vif->pending_ring[info_idx];
  1352. peek = vif->pending_ring[pending_index(++head)];
  1353. } while (!pending_tx_is_head(vif, peek));
  1354. put_page(vif->mmap_pages[pending_idx]);
  1355. vif->mmap_pages[pending_idx] = NULL;
  1356. }
  1357. static void make_tx_response(struct xenvif *vif,
  1358. struct xen_netif_tx_request *txp,
  1359. s8 st)
  1360. {
  1361. RING_IDX i = vif->tx.rsp_prod_pvt;
  1362. struct xen_netif_tx_response *resp;
  1363. int notify;
  1364. resp = RING_GET_RESPONSE(&vif->tx, i);
  1365. resp->id = txp->id;
  1366. resp->status = st;
  1367. if (txp->flags & XEN_NETTXF_extra_info)
  1368. RING_GET_RESPONSE(&vif->tx, ++i)->status = XEN_NETIF_RSP_NULL;
  1369. vif->tx.rsp_prod_pvt = ++i;
  1370. RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&vif->tx, notify);
  1371. if (notify)
  1372. notify_remote_via_irq(vif->tx_irq);
  1373. }
  1374. static struct xen_netif_rx_response *make_rx_response(struct xenvif *vif,
  1375. u16 id,
  1376. s8 st,
  1377. u16 offset,
  1378. u16 size,
  1379. u16 flags)
  1380. {
  1381. RING_IDX i = vif->rx.rsp_prod_pvt;
  1382. struct xen_netif_rx_response *resp;
  1383. resp = RING_GET_RESPONSE(&vif->rx, i);
  1384. resp->offset = offset;
  1385. resp->flags = flags;
  1386. resp->id = id;
  1387. resp->status = (s16)size;
  1388. if (st < 0)
  1389. resp->status = (s16)st;
  1390. vif->rx.rsp_prod_pvt = ++i;
  1391. return resp;
  1392. }
  1393. static inline int rx_work_todo(struct xenvif *vif)
  1394. {
  1395. return !skb_queue_empty(&vif->rx_queue);
  1396. }
  1397. static inline int tx_work_todo(struct xenvif *vif)
  1398. {
  1399. if (likely(RING_HAS_UNCONSUMED_REQUESTS(&vif->tx)) &&
  1400. (nr_pending_reqs(vif) + XEN_NETBK_LEGACY_SLOTS_MAX
  1401. < MAX_PENDING_REQS))
  1402. return 1;
  1403. return 0;
  1404. }
  1405. void xenvif_unmap_frontend_rings(struct xenvif *vif)
  1406. {
  1407. if (vif->tx.sring)
  1408. xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(vif),
  1409. vif->tx.sring);
  1410. if (vif->rx.sring)
  1411. xenbus_unmap_ring_vfree(xenvif_to_xenbus_device(vif),
  1412. vif->rx.sring);
  1413. }
  1414. int xenvif_map_frontend_rings(struct xenvif *vif,
  1415. grant_ref_t tx_ring_ref,
  1416. grant_ref_t rx_ring_ref)
  1417. {
  1418. void *addr;
  1419. struct xen_netif_tx_sring *txs;
  1420. struct xen_netif_rx_sring *rxs;
  1421. int err = -ENOMEM;
  1422. err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(vif),
  1423. tx_ring_ref, &addr);
  1424. if (err)
  1425. goto err;
  1426. txs = (struct xen_netif_tx_sring *)addr;
  1427. BACK_RING_INIT(&vif->tx, txs, PAGE_SIZE);
  1428. err = xenbus_map_ring_valloc(xenvif_to_xenbus_device(vif),
  1429. rx_ring_ref, &addr);
  1430. if (err)
  1431. goto err;
  1432. rxs = (struct xen_netif_rx_sring *)addr;
  1433. BACK_RING_INIT(&vif->rx, rxs, PAGE_SIZE);
  1434. vif->rx_req_cons_peek = 0;
  1435. return 0;
  1436. err:
  1437. xenvif_unmap_frontend_rings(vif);
  1438. return err;
  1439. }
  1440. int xenvif_kthread(void *data)
  1441. {
  1442. struct xenvif *vif = data;
  1443. while (!kthread_should_stop()) {
  1444. wait_event_interruptible(vif->wq,
  1445. rx_work_todo(vif) ||
  1446. kthread_should_stop());
  1447. if (kthread_should_stop())
  1448. break;
  1449. if (rx_work_todo(vif))
  1450. xenvif_rx_action(vif);
  1451. cond_resched();
  1452. }
  1453. return 0;
  1454. }
  1455. static int __init netback_init(void)
  1456. {
  1457. int rc = 0;
  1458. if (!xen_domain())
  1459. return -ENODEV;
  1460. if (fatal_skb_slots < XEN_NETBK_LEGACY_SLOTS_MAX) {
  1461. pr_info("fatal_skb_slots too small (%d), bump it to XEN_NETBK_LEGACY_SLOTS_MAX (%d)\n",
  1462. fatal_skb_slots, XEN_NETBK_LEGACY_SLOTS_MAX);
  1463. fatal_skb_slots = XEN_NETBK_LEGACY_SLOTS_MAX;
  1464. }
  1465. rc = xenvif_xenbus_init();
  1466. if (rc)
  1467. goto failed_init;
  1468. return 0;
  1469. failed_init:
  1470. return rc;
  1471. }
  1472. module_init(netback_init);
  1473. static void __exit netback_fini(void)
  1474. {
  1475. xenvif_xenbus_fini();
  1476. }
  1477. module_exit(netback_fini);
  1478. MODULE_LICENSE("Dual BSD/GPL");
  1479. MODULE_ALIAS("xen-backend:vif");