netback.c 46 KB

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