netback.c 47 KB

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